Hex Artifact Content
Not logged in

Artifact bb900ede4533282725f88ae33b57b8872b91ece5:

File src/sqlite3.c part of check-in [f3d4a2db53] - Update the SQLite used internally to the first 3.6.21 release candidate. by drh on 2009-11-23 13:21:26.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 32 31 2e 20 20  ersion 3.6.21.  
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c  By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20   the individual 
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74  C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c  o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68  e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61  e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73  n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69   a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69  on.** unit.  Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f  s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70  mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74  timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a   would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65   possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70   files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d    Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66  provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65   5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77   commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f  hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67  mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f  le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79  is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69  ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75  le SQLite.  To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68  se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20  er.** programs, 
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69  you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69  le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69  te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a  le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69  ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  ng interface to 
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20  ry.  (If you do 
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65  not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61   "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64  der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20  , you will find 
0340: 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20  a copy embedded 
0350: 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 74 65  within.** the te
0360: 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e  xt of this file.
0370: 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65    Search for "Be
0380: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33  gin file sqlite3
0390: 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20  .h" to find the 
03a0: 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20  start.** of the 
03b0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33  embedded sqlite3
03c0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29  .h header file.)
03d0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65   Additional code
03e0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65   files may be ne
03f0: 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 20 77  eded.** if you w
0400: 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f  ant a wrapper to
0410: 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74   interface SQLit
0420: 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69  e with your choi
0430: 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69 6e  ce of programmin
0440: 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e 20 54  g.** language. T
0450: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  he code for the 
0460: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e  "sqlite3" comman
0470: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 73 20  d-line shell is 
0480: 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 65 70  also in a.** sep
0490: 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 69 73  arate file. This
04a0: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f   file contains o
04b0: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65  nly code for the
04c0: 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c 69 62   core SQLite lib
04d0: 72 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rary..*/.#define
04e0: 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 0a 23   SQLITE_CORE 1.#
04f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4d  define SQLITE_AM
0500: 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23 69 66  ALGAMATION 1.#if
0510: 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 49 56  ndef SQLITE_PRIV
0520: 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ATE.# define SQL
0530: 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 61 74  ITE_PRIVATE stat
0540: 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  ic.#endif.#ifnde
0550: 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 20 64  f SQLITE_API.# d
0560: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 50 49  efine SQLITE_API
0570: 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a 2a  .#endif./*******
0580: 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
0590: 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  le sqliteInt.h *
05a0: 2a 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 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
05d0: 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
05e0: 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
05f0: 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
0600: 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
0610: 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
0620: 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
0630: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
0640: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
0650: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
0660: 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
0670: 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
0680: 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
0690: 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
06a0: 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
06b0: 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
06c0: 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
06d0: 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
06e0: 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
06f0: 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
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 0a 2a  **************.*
0740: 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74 65 72  * Internal inter
0750: 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f 6e 73  face definitions
0760: 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a   for SQLite..**.
0770: 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49  */.#ifndef _SQLI
0780: 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65  TEINT_H_.#define
0790: 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f 0a 0a   _SQLITEINT_H_..
07a0: 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66  /*.** These #def
07b0: 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62  ines should enab
07c0: 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70  le >2GB file sup
07d0: 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 69 66  port on POSIX if
07e0: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69   the.** underlyi
07f0: 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ng operating sys
0800: 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e  tem supports it.
0810: 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b    If the OS lack
0820: 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20  s.** large file 
0830: 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 20 74  support, or if t
0840: 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f 77 73  he OS is windows
0850: 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62  , these should b
0860: 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  e no-ops..**.** 
0870: 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 20 54  Ticket #2739:  T
0880: 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  he _LARGEFILE_SO
0890: 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 74 20  URCE macro must 
08a0: 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 61 6e  appear before an
08b0: 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 6e 63  y.** system #inc
08c0: 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c 20 74  ludes.  Hence, t
08d0: 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  his block of cod
08e0: 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 76 65  e must be the ve
08f0: 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f 64 65  ry first.** code
0900: 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 20 66   in all source f
0910: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67  iles..**.** Larg
0920: 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63  e file support c
0930: 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75  an be disabled u
0940: 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54  sing the -DSQLIT
0950: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77  E_DISABLE_LFS sw
0960: 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63  itch.** on the c
0970: 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20  ompiler command 
0980: 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e  line.  This is n
0990: 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20  ecessary if you 
09a0: 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a  are compiling.**
09b0: 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63   on a recent mac
09c0: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 48 61  hine (ex: Red Ha
09d0: 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77  t 7.2) but you w
09e0: 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f  ant your code to
09f0: 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f   work.** on an o
0a00: 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78  lder machine (ex
0a10: 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 2e 20  : Red Hat 6.0). 
0a20: 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20   If you compile 
0a30: 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 0a 2a  on Red Hat 7.2.*
0a40: 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f  * without this o
0a50: 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e  ption, LFS is en
0a60: 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64  able.  But LFS d
0a70: 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e  oes not exist in
0a80: 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69   the kernel.** i
0a90: 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c 20 73  n Red Hat 6.0, s
0aa0: 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74  o the code won't
0ab0: 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66   work.  Hence, f
0ac0: 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72  or maximum binar
0ad0: 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79  y.** portability
0ae0: 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74   you should omit
0af0: 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 6d 69   LFS..**.** Simi
0b00: 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f 72 20  lar is true for 
0b10: 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 20 69  Mac OS X.  LFS i
0b20: 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64  s only supported
0b30: 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 20 61   on Mac OS X 9 a
0b40: 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69 66  nd later..*/.#if
0b50: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41  ndef SQLITE_DISA
0b60: 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65  BLE_LFS.# define
0b70: 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20   _LARGE_FILE    
0b80: 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46     1.# ifndef _F
0b90: 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a  ILE_OFFSET_BITS.
0ba0: 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45  #   define _FILE
0bb0: 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a  _OFFSET_BITS 64.
0bc0: 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
0bd0: 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52   _LARGEFILE_SOUR
0be0: 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  CE 1.#endif../*.
0bf0: 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63  ** Include the c
0c00: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61  onfiguration hea
0c10: 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 27 63  der output by 'c
0c20: 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 65 27  onfigure' if we'
0c30: 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  re using the.** 
0c40: 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62  autoconf-based b
0c50: 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f  uild.*/.#ifdef _
0c60: 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46  HAVE_SQLITE_CONF
0c70: 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63  IG_H.#include "c
0c80: 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a  onfig.h".#endif.
0c90: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
0ca0: 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c   Include sqliteL
0cb0: 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69  imit.h in the mi
0cc0: 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
0cd0: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  t.h ***********/
0ce0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
0cf0: 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69   Begin file sqli
0d00: 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a  teLimit.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 2f  ***************/
0d30: 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20  ./*.** 2007 May 
0d40: 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
0d50: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
0d60: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
0d70: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
0d80: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
0d90: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
0da0: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
0db0: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
0dc0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
0dd0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
0de0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
0df0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
0e00: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
0e10: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
0e20: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
0e30: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
0e40: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
0e50: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
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: 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c  .** .** This fil
0eb0: 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75  e defines variou
0ec0: 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74  s limits of what
0ed0: 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63   SQLite can proc
0ee0: 65 73 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ess..*/../*.** T
0ef0: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
0f00: 68 20 6f 66 20 61 20 54 45 58 54 20 6f 72 20 42  h of a TEXT or B
0f10: 4c 4f 42 20 69 6e 20 62 79 74 65 73 2e 20 20 20  LOB in bytes.   
0f20: 54 68 69 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d  This also.** lim
0f30: 69 74 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20  its the size of 
0f40: 61 20 72 6f 77 20 69 6e 20 61 20 74 61 62 6c 65  a row in a table
0f50: 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a   or index..**.**
0f60: 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20   The hard limit 
0f70: 69 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 6f  is the ability o
0f80: 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65  f a 32-bit signe
0f90: 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20  d integer.** to 
0fa0: 63 6f 75 6e 74 20 74 68 65 20 73 69 7a 65 3a 20  count the size: 
0fb0: 32 5e 33 31 2d 31 20 6f 72 20 32 31 34 37 34 38  2^31-1 or 214748
0fc0: 33 36 34 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  3647..*/.#ifndef
0fd0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
0fe0: 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  TH.# define SQLI
0ff0: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30  TE_MAX_LENGTH 10
1000: 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 66 0a  00000000.#endif.
1010: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ./*.** This is t
1020: 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1030: 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  r of.**.**    * 
1040: 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61 62  Columns in a tab
1050: 6c 65 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d  le.**    * Colum
1060: 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a  ns in an index.*
1070: 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69  *    * Columns i
1080: 6e 20 61 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a  n a view.**    *
1090: 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 53 45   Terms in the SE
10a0: 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 55  T clause of an U
10b0: 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 0a  PDATE statement.
10c0: 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e  **    * Terms in
10d0: 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
10e0: 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
10f0: 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65  ement.**    * Te
1100: 72 6d 73 20 69 6e 20 74 68 65 20 47 52 4f 55 50  rms in the GROUP
1110: 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20   BY or ORDER BY 
1120: 63 6c 61 75 73 65 73 20 6f 66 20 61 20 53 45 4c  clauses of a SEL
1130: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
1140: 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20  *    * Terms in 
1150: 74 68 65 20 56 41 4c 55 45 53 20 63 6c 61 75 73  the VALUES claus
1160: 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 73  e of an INSERT s
1170: 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54  tatement.**.** T
1180: 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69  he hard upper li
1190: 6d 69 74 20 68 65 72 65 20 69 73 20 33 32 36 37  mit here is 3267
11a0: 36 2e 20 20 4d 6f 73 74 20 64 61 74 61 62 61 73  6.  Most databas
11b0: 65 20 70 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a  e people will.**
11c0: 20 74 65 6c 6c 20 79 6f 75 20 74 68 61 74 20 69   tell you that i
11d0: 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69  n a well-normali
11e0: 7a 65 64 20 64 61 74 61 62 61 73 65 2c 20 79 6f  zed database, yo
11f0: 75 20 75 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64  u usually should
1200: 0a 2a 2a 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72  .** not have mor
1210: 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f  e than a dozen o
1220: 72 20 73 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r so columns in 
1230: 61 6e 79 20 74 61 62 6c 65 2e 20 20 41 6e 64 20  any table.  And 
1240: 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 74 68  if.** that is th
1250: 65 20 63 61 73 65 2c 20 74 68 65 72 65 20 69 73  e case, there is
1260: 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 68 61 76   no point in hav
1270: 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 61 20  ing more than a 
1280: 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c  few.** dozen val
1290: 75 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68  ues in any of th
12a0: 65 20 6f 74 68 65 72 20 73 69 74 75 61 74 69 6f  e other situatio
12b0: 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 62 6f  ns described abo
12c0: 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ve..*/.#ifndef S
12d0: 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
12e0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
12f0: 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30  _MAX_COLUMN 2000
1300: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1310: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
1320: 68 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 53 51  h of a single SQ
1330: 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 62  L statement in b
1340: 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75  ytes..**.** It u
1350: 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 63 61  sed to be the ca
1360: 73 65 20 74 68 61 74 20 73 65 74 74 69 6e 67 20  se that setting 
1370: 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 7a 65  this value to ze
1380: 72 6f 20 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e  ro would.** turn
1390: 20 74 68 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20   the limit off. 
13a0: 20 54 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67   That is no long
13b0: 65 72 20 74 72 75 65 2e 20 20 49 74 20 69 73 20  er true.  It is 
13c0: 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20  not possible.** 
13d0: 74 6f 20 74 75 72 6e 20 74 68 69 73 20 6c 69 6d  to turn this lim
13e0: 69 74 20 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64  it off..*/.#ifnd
13f0: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  ef SQLITE_MAX_SQ
1400: 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e  L_LENGTH.# defin
1410: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c  e SQLITE_MAX_SQL
1420: 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 30  _LENGTH 10000000
1430: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1440: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70   The maximum dep
1450: 74 68 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  th of an express
1460: 69 6f 6e 20 74 72 65 65 2e 20 54 68 69 73 20 69  ion tree. This i
1470: 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a  s limited to .**
1480: 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62 79 20   some extent by 
1490: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
14a0: 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d 65 74  ENGTH. But somet
14b0: 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a  ime you might .*
14c0: 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63 65 20  * want to place 
14d0: 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69 6d 69  more severe limi
14e0: 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65  ts on the comple
14f0: 78 69 74 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65  xity of an .** e
1500: 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
1510: 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 75 73   A value of 0 us
1520: 65 64 20 74 6f 20 6d 65 61 6e 20 74 68 61 74 20  ed to mean that 
1530: 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f  the limit was no
1540: 74 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42  t enforced..** B
1550: 75 74 20 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f  ut that is no lo
1560: 6e 67 65 72 20 74 72 75 65 2e 20 20 54 68 65 20  nger true.  The 
1570: 6c 69 6d 69 74 20 69 73 20 6e 6f 77 20 73 74 72  limit is now str
1580: 69 63 74 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a  ictly enforced.*
1590: 2a 20 61 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a  * at all times..
15a0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
15b0: 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
15c0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
15d0: 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 20  _MAX_EXPR_DEPTH 
15e0: 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1000.#endif../*.
15f0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1600: 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
1610: 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  n a compound SEL
1620: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
1630: 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72  * The code gener
1640: 61 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e  ator for compoun
1650: 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  d SELECT stateme
1660: 6e 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20  nts does one.** 
1670: 6c 65 76 65 6c 20 6f 66 20 72 65 63 75 72 73 69  level of recursi
1680: 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d  on for each term
1690: 2e 20 20 41 20 73 74 61 63 6b 20 6f 76 65 72 66  .  A stack overf
16a0: 6c 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a  low can result.*
16b0: 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20  * if the number 
16c0: 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f 6f 20  of terms is too 
16d0: 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61 63 74  large.  In pract
16e0: 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a  ice, most SQL.**
16f0: 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 65 20   never has more 
1700: 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65 72 6d  than 3 or 4 term
1710: 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75 65 20  s.  Use a value 
1720: 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c 65 0a  of 0 to disable.
1730: 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20  ** any limit on 
1740: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  the number of te
1750: 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  rms in a compoun
1760: 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66  t SELECT..*/.#if
1770: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1780: 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
1790: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
17a0: 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
17b0: 45 43 54 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a  ECT 500.#endif..
17c0: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
17d0: 6d 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f  m number of opco
17e0: 64 65 73 20 69 6e 20 61 20 56 44 42 45 20 70 72  des in a VDBE pr
17f0: 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75  ogram..** Not cu
1800: 72 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64  rrently enforced
1810: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1820: 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a  ITE_MAX_VDBE_OP.
1830: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1840: 4d 41 58 5f 56 44 42 45 5f 4f 50 20 32 35 30 30  MAX_VDBE_OP 2500
1850: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
1860: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1870: 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
1880: 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  to an SQL functi
1890: 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
18a0: 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49  QLITE_MAX_FUNCTI
18b0: 4f 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e 65 20  ON_ARG.# define 
18c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
18d0: 49 4f 4e 5f 41 52 47 20 31 32 37 0a 23 65 6e 64  ION_ARG 127.#end
18e0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
18f0: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1900: 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20  in-memory pages 
1910: 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 6d  to use for the m
1920: 61 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ain database.** 
1930: 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 74 65  table and for te
1940: 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20  mporary tables. 
1950: 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41   The SQLITE_DEFA
1960: 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a  ULT_CACHE_SIZE.*
1970: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1980: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1990: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  IZE.# define SQL
19a0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  ITE_DEFAULT_CACH
19b0: 45 5f 53 49 5a 45 20 20 32 30 30 30 0a 23 65 6e  E_SIZE  2000.#en
19c0: 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
19d0: 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f  TE_DEFAULT_TEMP_
19e0: 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65 66  CACHE_SIZE.# def
19f0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
1a00: 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49  LT_TEMP_CACHE_SI
1a10: 5a 45 20 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a  ZE  500.#endif..
1a20: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
1a30: 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 74 74 61  m number of atta
1a40: 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 20  ched databases. 
1a50: 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 62 65   This must be be
1a60: 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33  tween 0.** and 3
1a70: 30 2e 20 20 54 68 65 20 75 70 70 65 72 20 62 6f  0.  The upper bo
1a80: 75 6e 64 20 6f 6e 20 33 30 20 69 73 20 62 65 63  und on 30 is bec
1a90: 61 75 73 65 20 61 20 33 32 2d 62 69 74 20 69 6e  ause a 32-bit in
1aa0: 74 65 67 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20  teger bitmap.** 
1ab0: 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  is used internal
1ac0: 6c 79 20 74 6f 20 74 72 61 63 6b 20 61 74 74 61  ly to track atta
1ad0: 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 0a  ched databases..
1ae0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1af0: 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 0a 23  E_MAX_ATTACHED.#
1b00: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
1b10: 41 58 5f 41 54 54 41 43 48 45 44 20 31 30 0a 23  AX_ATTACHED 10.#
1b20: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
1b30: 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20  e maximum value 
1b40: 6f 66 20 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61  of a ?nnn wildca
1b50: 72 64 20 74 68 61 74 20 74 68 65 20 70 61 72 73  rd that the pars
1b60: 65 72 20 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a  er will accept..
1b70: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1b80: 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e  E_MAX_VARIABLE_N
1b90: 55 4d 42 45 52 0a 23 20 64 65 66 69 6e 65 20 53  UMBER.# define S
1ba0: 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42  QLITE_MAX_VARIAB
1bb0: 4c 45 5f 4e 55 4d 42 45 52 20 39 39 39 0a 23 65  LE_NUMBER 999.#e
1bc0: 6e 64 69 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d  ndif../* Maximum
1bd0: 20 70 61 67 65 20 73 69 7a 65 2e 20 20 54 68 65   page size.  The
1be0: 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20   upper bound on 
1bf0: 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 33 32  this value is 32
1c00: 37 36 38 2e 20 20 54 68 69 73 20 61 20 6c 69 6d  768.  This a lim
1c10: 69 74 0a 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79  it.** imposed by
1c20: 20 74 68 65 20 6e 65 63 65 73 73 69 74 79 20 6f   the necessity o
1c30: 66 20 73 74 6f 72 69 6e 67 20 74 68 65 20 76 61  f storing the va
1c40: 6c 75 65 20 69 6e 20 61 20 32 2d 62 79 74 65 20  lue in a 2-byte 
1c50: 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
1c60: 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 61 63 74  .** and the fact
1c70: 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 73   that the page s
1c80: 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f  ize must be a po
1c90: 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20  wer of 2..**.** 
1ca0: 49 66 20 74 68 69 73 20 6c 69 6d 69 74 20 69 73  If this limit is
1cb0: 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74   changed, then t
1cc0: 68 65 20 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72  he compiled libr
1cd0: 61 72 79 20 69 73 20 74 65 63 68 6e 69 63 61 6c  ary is technical
1ce0: 6c 79 0a 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62  ly.** incompatib
1cf0: 6c 65 20 77 69 74 68 20 61 6e 20 53 51 4c 69 74  le with an SQLit
1d00: 65 20 6c 69 62 72 61 72 79 20 63 6f 6d 70 69 6c  e library compil
1d10: 65 64 20 77 69 74 68 20 61 20 64 69 66 66 65 72  ed with a differ
1d20: 65 6e 74 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a  ent limit. If.**
1d30: 20 61 20 70 72 6f 63 65 73 73 20 6f 70 65 72 61   a process opera
1d40: 74 69 6e 67 20 6f 6e 20 61 20 64 61 74 61 62 61  ting on a databa
1d50: 73 65 20 77 69 74 68 20 61 20 70 61 67 65 2d 73  se with a page-s
1d60: 69 7a 65 20 6f 66 20 36 35 35 33 36 20 62 79 74  ize of 65536 byt
1d70: 65 73 20 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20  es .** crashes, 
1d80: 74 68 65 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65  then an instance
1d90: 20 6f 66 20 53 51 4c 69 74 65 20 63 6f 6d 70 69   of SQLite compi
1da0: 6c 65 64 20 77 69 74 68 20 74 68 65 20 64 65 66  led with the def
1db0: 61 75 6c 74 20 70 61 67 65 2d 73 69 7a 65 20 0a  ault page-size .
1dc0: 2a 2a 20 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f  ** limit will no
1dd0: 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c  t be able to rol
1de0: 6c 62 61 63 6b 20 74 68 65 20 61 62 6f 72 74 65  lback the aborte
1df0: 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54  d transaction. T
1e00: 68 69 73 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61  his could.** lea
1e10: 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 6f  d to database co
1e20: 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66  rruption..*/.#if
1e30: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1e40: 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69  PAGE_SIZE.# defi
1e50: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ne SQLITE_MAX_PA
1e60: 47 45 5f 53 49 5a 45 20 33 32 37 36 38 0a 23 65  GE_SIZE 32768.#e
1e70: 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ndif.../*.** The
1e80: 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66   default size of
1e90: 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65   a database page
1ea0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1eb0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  ITE_DEFAULT_PAGE
1ec0: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53  _SIZE.# define S
1ed0: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
1ee0: 47 45 5f 53 49 5a 45 20 31 30 32 34 0a 23 65 6e  GE_SIZE 1024.#en
1ef0: 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 44  dif.#if SQLITE_D
1f00: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1f10: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  >SQLITE_MAX_PAGE
1f20: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51  _SIZE.# undef SQ
1f30: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
1f40: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  E_SIZE.# define 
1f50: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
1f60: 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f  AGE_SIZE SQLITE_
1f70: 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65  MAX_PAGE_SIZE.#e
1f80: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69  ndif../*.** Ordi
1f90: 6e 61 72 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61  narily, if no va
1fa0: 6c 75 65 20 69 73 20 65 78 70 6c 69 63 69 74 6c  lue is explicitl
1fb0: 79 20 70 72 6f 76 69 64 65 64 2c 20 53 51 4c 69  y provided, SQLi
1fc0: 74 65 20 63 72 65 61 74 65 73 20 64 61 74 61 62  te creates datab
1fd0: 61 73 65 73 0a 2a 2a 20 77 69 74 68 20 70 61 67  ases.** with pag
1fe0: 65 20 73 69 7a 65 20 53 51 4c 49 54 45 5f 44 45  e size SQLITE_DE
1ff0: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e  FAULT_PAGE_SIZE.
2000: 20 48 6f 77 65 76 65 72 2c 20 62 61 73 65 64 20   However, based 
2010: 6f 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65  on certain.** de
2020: 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73  vice characteris
2030: 74 69 63 73 20 28 73 65 63 74 6f 72 2d 73 69 7a  tics (sector-siz
2040: 65 20 61 6e 64 20 61 74 6f 6d 69 63 20 77 72 69  e and atomic wri
2050: 74 65 28 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a  te() support),.*
2060: 2a 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68 6f  * SQLite may cho
2070: 6f 73 65 20 61 20 6c 61 72 67 65 72 20 76 61 6c  ose a larger val
2080: 75 65 2e 20 54 68 69 73 20 63 6f 6e 73 74 61 6e  ue. This constan
2090: 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  t is the maximum
20a0: 20 76 61 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65   value.** SQLite
20b0: 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20   will choose on 
20c0: 69 74 73 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e  its own..*/.#ifn
20d0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44  def SQLITE_MAX_D
20e0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
20f0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
2100: 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
2110: 45 5f 53 49 5a 45 20 38 31 39 32 0a 23 65 6e 64  E_SIZE 8192.#end
2120: 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
2130: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
2140: 49 5a 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50  IZE>SQLITE_MAX_P
2150: 41 47 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66  AGE_SIZE.# undef
2160: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41   SQLITE_MAX_DEFA
2170: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20  ULT_PAGE_SIZE.# 
2180: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
2190: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
21a0: 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  IZE SQLITE_MAX_P
21b0: 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a  AGE_SIZE.#endif.
21c0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
21d0: 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
21e0: 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  in one database 
21f0: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  file..**.** This
2200: 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20   is really just 
2210: 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
2220: 65 20 66 6f 72 20 74 68 65 20 6d 61 78 5f 70 61  e for the max_pa
2230: 67 65 5f 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e  ge_count pragma.
2240: 0a 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20 63  .** This value c
2250: 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 28 6f  an be lowered (o
2260: 72 20 72 61 69 73 65 64 29 20 61 74 20 72 75 6e  r raised) at run
2270: 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 61 74  -time using that
2280: 20 74 68 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65   the.** max_page
2290: 5f 63 6f 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f  _count macro..*/
22a0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22b0: 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23  MAX_PAGE_COUNT.#
22c0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
22d0: 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 20 31 30  AX_PAGE_COUNT 10
22e0: 37 33 37 34 31 38 32 33 0a 23 65 6e 64 69 66 0a  73741823.#endif.
22f0: 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c  ./*.** Maximum l
2300: 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 29  ength (in bytes)
2310: 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20   of the pattern 
2320: 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f  in a LIKE or GLO
2330: 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  B.** operator..*
2340: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2350: 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52  _MAX_LIKE_PATTER
2360: 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e  N_LENGTH.# defin
2370: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  e SQLITE_MAX_LIK
2380: 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
2390: 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f   50000.#endif../
23a0: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70  *.** Maximum dep
23b0: 74 68 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20  th of recursion 
23c0: 66 6f 72 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a  for triggers..**
23d0: 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 31  .** A value of 1
23e0: 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 74 72   means that a tr
23f0: 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 77 69  igger program wi
2400: 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
2410: 6f 20 69 74 73 65 6c 66 0a 2a 2a 20 66 69 72 65  o itself.** fire
2420: 20 61 6e 79 20 74 72 69 67 67 65 72 73 2e 20 41   any triggers. A
2430: 20 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e   value of 0 mean
2440: 73 20 74 68 61 74 20 6e 6f 20 74 72 69 67 67 65  s that no trigge
2450: 72 20 70 72 6f 67 72 61 6d 73 20 61 74 20 61 6c  r programs at al
2460: 6c 20 0a 2a 2a 20 6d 61 79 20 62 65 20 65 78 65  l .** may be exe
2470: 63 75 74 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  cuted..*/.#ifnde
2480: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49  f SQLITE_MAX_TRI
2490: 47 47 45 52 5f 44 45 50 54 48 0a 23 20 64 65 66  GGER_DEPTH.# def
24a0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54  ine SQLITE_MAX_T
24b0: 52 49 47 47 45 52 5f 44 45 50 54 48 20 31 30 30  RIGGER_DEPTH 100
24c0: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  0.#endif../*****
24d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
24e0: 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a   sqliteLimit.h *
24f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2500: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2510: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2520: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
2530: 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
2540: 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
2550: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
2560: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69  ********/../* Di
2570: 73 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77  sable nuisance w
2580: 61 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61  arnings on Borla
2590: 6e 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a  nd compilers */.
25a0: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f  #if defined(__BO
25b0: 52 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d  RLANDC__).#pragm
25c0: 61 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75  a warn -rch /* u
25d0: 6e 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20  nreachable code 
25e0: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20  */.#pragma warn 
25f0: 2d 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f  -ccc /* Conditio
2600: 6e 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65  n is always true
2610: 20 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72   or false */.#pr
2620: 61 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f  agma warn -aus /
2630: 2a 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65  * Assigned value
2640: 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a   is never used *
2650: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d  /.#pragma warn -
2660: 63 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67  csu /* Comparing
2670: 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69   signed and unsi
2680: 67 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20  gned */.#pragma 
2690: 77 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73  warn -spa /* Sus
26a0: 70 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20  picious pointer 
26b0: 61 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65  arithmetic */.#e
26c0: 6e 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20  ndif../* Needed 
26d0: 66 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69  for various defi
26e0: 6e 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69  nitions... */.#i
26f0: 66 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43  fndef _GNU_SOURC
2700: 45 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f  E.# define _GNU_
2710: 53 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f  SOURCE.#endif../
2720: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61  *.** Include sta
2730: 6e 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c  ndard header fil
2740: 65 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a  es as necessary.
2750: 2a 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53  */.#ifdef HAVE_S
2760: 54 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65  TDINT_H.#include
2770: 20 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64   <stdint.h>.#end
2780: 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49  if.#ifdef HAVE_I
2790: 4e 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75  NTTYPES_H.#inclu
27a0: 64 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a  de <inttypes.h>.
27b0: 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20  #endif..#define 
27c0: 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d  SQLITE_INDEX_SAM
27d0: 50 4c 45 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54  PLES 10../*.** T
27e0: 68 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65  his macro is use
27f0: 64 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65  d to "hide" some
2800: 20 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73   ugliness in cas
2810: 74 69 6e 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76  ting an int.** v
2820: 61 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61  alue to a ptr va
2830: 6c 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53  lue under the MS
2840: 56 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c  VC 64-bit compil
2850: 65 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a  er.   Casting.**
2860: 20 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75   non 64-bit valu
2870: 65 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20  es to ptr types 
2880: 72 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61  results in a "ha
2890: 72 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a  rd" error with .
28a0: 2a 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62  ** the MSVC 64-b
28b0: 69 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63  it compiler whic
28c0: 68 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20  h this attempts 
28d0: 74 6f 20 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a  to avoid.  .**.*
28e0: 2a 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69  * A simple compi
28f0: 6c 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61  ler pragma or ca
2900: 73 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63  sting sequence c
2910: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e  ould not be foun
2920: 64 0a 2a 2a 20 74 6f 20 63 6f 72 72 65 63 74 20  d.** to correct 
2930: 74 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75  this in all situ
2940: 61 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20  ations, so this 
2950: 6d 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64  macro was introd
2960: 75 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63  uced..**.** It c
2970: 6f 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74  ould be argued t
2980: 68 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74  hat the intptr_t
2990: 20 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75   type could be u
29a0: 73 65 64 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63  sed in this.** c
29b0: 61 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79  ase, but that ty
29c0: 70 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  pe is not availa
29d0: 62 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69  ble on all compi
29e0: 6c 65 72 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71  lers, or .** req
29f0: 75 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75  uires the #inclu
2a00: 64 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68  de of specific h
2a10: 65 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66  eaders which dif
2a20: 66 65 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20  fers between.** 
2a30: 70 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a  platforms..**.**
2a40: 20 54 69 63 6b 65 74 20 23 33 38 36 30 3a 20 20   Ticket #3860:  
2a50: 54 68 65 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32  The llvm-gcc-4.2
2a60: 20 63 6f 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41   compiler from A
2a70: 70 70 6c 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a  pple chokes on.*
2a80: 2a 20 74 68 65 20 28 28 76 6f 69 64 2a 29 26 28  * the ((void*)&(
2a90: 28 63 68 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f  (char*)0)[X]) co
2aa0: 6e 73 74 72 75 63 74 2e 20 20 42 75 74 20 4d 53  nstruct.  But MS
2ab0: 56 43 20 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76  VC chokes on ((v
2ac0: 6f 69 64 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f  oid*)(X))..** So
2ad0: 20 77 65 20 68 61 76 65 20 74 6f 20 64 65 66 69   we have to defi
2ae0: 6e 65 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e  ne the macros in
2af0: 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20   different ways 
2b00: 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
2b10: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f  .** compiler..*/
2b20: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47  .#if defined(__G
2b30: 4e 55 43 5f 5f 29 0a 23 20 69 66 20 64 65 66 69  NUC__).# if defi
2b40: 6e 65 64 28 48 41 56 45 5f 53 54 44 49 4e 54 5f  ned(HAVE_STDINT_
2b50: 48 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  H).#   define SQ
2b60: 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
2b70: 58 29 20 20 28 28 76 6f 69 64 2a 29 28 69 6e 74  X)  ((void*)(int
2b80: 70 74 72 5f 74 29 28 58 29 29 0a 23 20 20 20 64  ptr_t)(X)).#   d
2b90: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52  efine SQLITE_PTR
2ba0: 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e  _TO_INT(X)  ((in
2bb0: 74 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29  t)(intptr_t)(X))
2bc0: 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69  .# else.#   defi
2bd0: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f  ne SQLITE_INT_TO
2be0: 5f 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a  _PTR(X)  ((void*
2bf0: 29 28 58 29 29 0a 23 20 20 20 64 65 66 69 6e 65  )(X)).#   define
2c00: 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
2c10: 4e 54 28 58 29 20 20 28 28 69 6e 74 29 28 58 29  NT(X)  ((int)(X)
2c20: 29 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a  ).# endif.#else.
2c30: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
2c40: 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 20  INT_TO_PTR(X)   
2c50: 28 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a  ((void*)&((char*
2c60: 29 30 29 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65  )0)[X]).# define
2c70: 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
2c80: 4e 54 28 58 29 20 20 20 28 28 69 6e 74 29 28 28  NT(X)   ((int)((
2c90: 28 63 68 61 72 2a 29 58 29 2d 28 63 68 61 72 2a  (char*)X)-(char*
2ca0: 29 30 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  )0)).#endif.../*
2cb0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54  .** The SQLITE_T
2cc0: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20  HREADSAFE macro 
2cd0: 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20  must be defined 
2ce0: 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31  as either 0 or 1
2cf0: 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69  ..** Older versi
2d00: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73  ons of SQLite us
2d10: 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54  ed an optional T
2d20: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e  HREADSAFE macro.
2d30: 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74  .** We support t
2d40: 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a  hat for legacy.*
2d50: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
2d60: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2d70: 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  ).#if defined(TH
2d80: 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69  READSAFE).# defi
2d90: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ne SQLITE_THREAD
2da0: 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a  SAFE THREADSAFE.
2db0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
2dc0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2dd0: 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66   1.#endif.#endif
2de0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
2df0: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
2e00: 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20  ATUS macro must 
2e10: 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69  be defined as ei
2e20: 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20  ther 0 or 1..** 
2e30: 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  It determines wh
2e40: 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
2e50: 20 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65   features relate
2e60: 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  d to .** SQLITE_
2e70: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53  CONFIG_MEMSTATUS
2e80: 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62   are available b
2e90: 79 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74  y default or not
2ea0: 2e 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e  . This value can
2eb0: 0a 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65  .** be overridde
2ec0: 6e 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69  n at runtime usi
2ed0: 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  ng the sqlite3_c
2ee0: 6f 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a  onfig() API..*/.
2ef0: 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2f00: 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53  ITE_DEFAULT_MEMS
2f10: 54 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20  TATUS).# define 
2f20: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d  SQLITE_DEFAULT_M
2f30: 45 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69  EMSTATUS 1.#endi
2f40: 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79  f../*.** Exactly
2f50: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
2f60: 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73  owing macros mus
2f70: 74 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20  t be defined in 
2f80: 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63  order to.** spec
2f90: 69 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79  ify which memory
2fa0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
2fb0: 79 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a  ystem to use..**
2fc0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53  .**     SQLITE_S
2fd0: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20  YSTEM_MALLOC    
2fe0: 20 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72        // Use nor
2ff0: 6d 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f  mal system mallo
3000: 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54  c().**     SQLIT
3010: 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20  E_MEMDEBUG      
3020: 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75           // Debu
3030: 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66  gging version of
3040: 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29   system malloc()
3050: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d  .**     SQLITE_M
3060: 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20  EMORY_SIZE      
3070: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
3080: 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a  l allocator #1.*
3090: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41  *     SQLITE_MMA
30a0: 50 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20  P_HEAP_SIZE     
30b0: 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20      // internal 
30c0: 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72  mmap() allocator
30d0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50  .**     SQLITE_P
30e0: 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20  OW2_MEMORY_SIZE 
30f0: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
3100: 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61  l power-of-two a
3110: 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49  llocator.**.** I
3120: 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62  f none of the ab
3130: 6f 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c  ove are defined,
3140: 20 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45   then set SQLITE
3150: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61  _SYSTEM_MALLOC a
3160: 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  s.** the default
3170: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
3180: 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d  (SQLITE_SYSTEM_M
3190: 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53  ALLOC)+defined(S
31a0: 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b  QLITE_MEMDEBUG)+
31b0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
31c0: 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45  LITE_MEMORY_SIZE
31d0: 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  )+defined(SQLITE
31e0: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29  _MMAP_HEAP_SIZE)
31f0: 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53  +\.    defined(S
3200: 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52  QLITE_POW2_MEMOR
3210: 59 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f  Y_SIZE)>1.# erro
3220: 72 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f  r "At most one o
3230: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3240: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e  compile-time con
3250: 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
3260: 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20  ns\. is allows: 
3270: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3280: 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d  LLOC, SQLITE_MEM
3290: 44 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45  DEBUG, SQLITE_ME
32a0: 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c  MORY_SIZE,\. SQL
32b0: 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49  ITE_MMAP_HEAP_SI
32c0: 5a 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f  ZE, SQLITE_POW2_
32d0: 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e  MEMORY_SIZE".#en
32e0: 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28  dif.#if defined(
32f0: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3300: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51  LLOC)+defined(SQ
3310: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c  LITE_MEMDEBUG)+\
3320: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
3330: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29  ITE_MEMORY_SIZE)
3340: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  +defined(SQLITE_
3350: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b  MMAP_HEAP_SIZE)+
3360: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
3370: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59  LITE_POW2_MEMORY
3380: 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69  _SIZE)==0.# defi
3390: 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ne SQLITE_SYSTEM
33a0: 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66  _MALLOC 1.#endif
33b0: 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ../*.** If SQLIT
33c0: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
33d0: 4d 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c  MIT is not zero,
33e0: 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65   then try to kee
33f0: 70 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f  p the.** sizes o
3400: 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  f memory allocat
3410: 69 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20  ions below this 
3420: 76 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73  value where poss
3430: 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  ible..*/.#if !de
3440: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c  fined(SQLITE_MAL
3450: 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a  LOC_SOFT_LIMIT).
3460: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
3470: 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49  MALLOC_SOFT_LIMI
3480: 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f  T 1024.#endif../
3490: 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20  *.** We need to 
34a0: 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f  define _XOPEN_SO
34b0: 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20  URCE as follows 
34c0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62  in order to enab
34d0: 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20  le.** recursive 
34e0: 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20  mutexes on most 
34f0: 55 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42  Unix systems.  B
3500: 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64  ut Mac OS X is d
3510: 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65  ifferent..** The
3520: 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64   _XOPEN_SOURCE d
3530: 65 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f  efine causes pro
3540: 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53  blems for Mac OS
3550: 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a   X we are told,.
3560: 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74  ** so it is omit
3570: 74 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20  ted there.  See 
3580: 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a  ticket #2673..**
3590: 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61  .** Later we lea
35a0: 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53  rn that _XOPEN_S
35b0: 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20  OURCE is poorly 
35c0: 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a  or incorrectly.*
35d0: 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e  * implemented on
35e0: 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20   some systems.  
35f0: 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69  So we avoid defi
3600: 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a  ning it at all.*
3610: 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61  * if it is alrea
3620: 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66  dy defined or if
3630: 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20   it is unneeded 
3640: 62 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a  because we are.*
3650: 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68  * not doing a th
3660: 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20  readsafe build. 
3670: 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a   Ticket #2681..*
3680: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69  *.** See also ti
3690: 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23  cket #2741..*/.#
36a0: 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50  if !defined(_XOP
36b0: 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64  EN_SOURCE) && !d
36c0: 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f  efined(__DARWIN_
36d0: 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f  _) && !defined(_
36e0: 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
36f0: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23  ITE_THREADSAFE.#
3700: 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f    define _XOPEN_
3710: 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e  SOURCE 500  /* N
3720: 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20  eeded to enable 
3730: 70 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76  pthread recursiv
3740: 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e  e mutexes */.#en
3750: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54  dif../*.** The T
3760: 43 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f  CL headers are o
3770: 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20  nly needed when 
3780: 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43  compiling the TC
3790: 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23  L bindings..*/.#
37a0: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
37b0: 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65  E_TCL) || define
37c0: 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75  d(TCLSH).# inclu
37d0: 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69  de <tcl.h>.#endi
37e0: 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65  f../*.** Many pe
37f0: 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67  ople are failing
3800: 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47   to set -DNDEBUG
3810: 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e  =1 when compilin
3820: 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74  g SQLite..** Set
3830: 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65  ting NDEBUG make
3840: 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c  s the code small
3850: 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65  er and run faste
3860: 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f  r.  So the follo
3870: 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72  wing.** lines ar
3880: 65 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d  e added to autom
3890: 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45  atically set NDE
38a0: 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d  BUG unless the -
38b0: 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a  DSQLITE_DEBUG=1.
38c0: 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74  ** option is set
38d0: 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62  .  Thus NDEBUG b
38e0: 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e  ecomes an opt-in
38f0: 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20   rather than an 
3900: 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75  opt-out.** featu
3910: 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  re..*/.#if !defi
3920: 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21  ned(NDEBUG) && !
3930: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
3940: 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20  EBUG) .# define 
3950: 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a  NDEBUG 1.#endif.
3960: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63  ./*.** The testc
3970: 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75  ase() macro is u
3980: 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f  sed to aid in co
3990: 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20  verage testing. 
39a0: 20 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20   When .** doing 
39b0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
39c0: 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20  , the condition 
39d0: 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d  inside the argum
39e0: 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61  ent to.** testca
39f0: 73 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61  se() must be eva
3a00: 6c 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65  luated both true
3a10: 20 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72   and false in or
3a20: 64 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75  der to.** get fu
3a30: 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61  ll branch covera
3a40: 67 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73  ge.  The testcas
3a50: 65 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73  e() macro is ins
3a60: 65 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70  erted.** to help
3a70: 20 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65   ensure adequate
3a80: 20 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69   test coverage i
3a90: 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73  n places where s
3aa0: 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69  imple.** conditi
3ab0: 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65  on/decision cove
3ac0: 72 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61  rage is inadequa
3ad0: 74 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  te.  For example
3ae0: 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20  , testcase().** 
3af0: 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d  can be used to m
3b00: 61 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72  ake sure boundar
3b10: 79 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73  y values are tes
3b20: 74 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74  ted.  For.** bit
3b30: 6d 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74  mask tests, test
3b40: 63 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73  case() can be us
3b50: 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
3b60: 65 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73  each bit.** is s
3b70: 69 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75  ignificant and u
3b80: 73 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63  sed at least onc
3b90: 65 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74  e.  On switch st
3ba0: 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72  atements.** wher
3bb0: 65 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73  e multiple cases
3bc0: 20 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20   go to the same 
3bd0: 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74  block of code, t
3be0: 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e  estcase().** can
3bf0: 20 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c   insure that all
3c00: 20 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75   cases are evalu
3c10: 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64  ated..**.*/.#ifd
3c20: 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41  ef SQLITE_COVERA
3c30: 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  GE_TEST.SQLITE_P
3c40: 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
3c50: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e  lite3Coverage(in
3c60: 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73  t);.# define tes
3c70: 74 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20  tcase(X)  if( X 
3c80: 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61  ){ sqlite3Covera
3c90: 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a  ge(__LINE__); }.
3ca0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74  #else.# define t
3cb0: 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69  estcase(X).#endi
3cc0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53  f../*.** The TES
3cd0: 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75  TONLY macro is u
3ce0: 73 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76  sed to enclose v
3cf0: 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74  ariable declarat
3d00: 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72  ions or.** other
3d10: 20 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68   bits of code th
3d20: 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f  at are needed to
3d30: 20 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67   support the arg
3d40: 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  uments.** within
3d50: 20 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20   testcase() and 
3d60: 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e  assert() macros.
3d70: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
3d80: 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  (NDEBUG) || defi
3d90: 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52  ned(SQLITE_COVER
3da0: 41 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69  AGE_TEST).# defi
3db0: 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20  ne TESTONLY(X)  
3dc0: 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  X.#else.# define
3dd0: 20 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e   TESTONLY(X).#en
3de0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74  dif../*.** Somet
3df0: 69 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73  imes we need a s
3e00: 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63  mall amount of c
3e10: 6f 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61  ode such as a va
3e20: 72 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a  riable initializ
3e30: 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75  ation.** to setu
3e40: 70 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73  p for a later as
3e50: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
3e60: 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e  .  We do not wan
3e70: 74 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a  t this code to.*
3e80: 2a 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73  * appear when as
3e90: 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c  sert() is disabl
3ea0: 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ed.  The followi
3eb0: 6e 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72  ng macro is ther
3ec0: 65 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f  efore.** used to
3ed0: 20 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65   contain that se
3ee0: 74 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22  tup code.  The "
3ef0: 56 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61  VVA" acronym sta
3f00: 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69  nds for.** "Veri
3f10: 66 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61  fication, Valida
3f20: 74 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64  tion, and Accred
3f30: 69 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74  itation".  In ot
3f40: 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a  her words, the.*
3f50: 2a 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56  * code within VV
3f60: 41 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e  A_ONLY() will on
3f70: 6c 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65  ly run during ve
3f80: 72 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65  rification proce
3f90: 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  sses..*/.#ifndef
3fa0: 20 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65   NDEBUG.# define
3fb0: 20 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a   VVA_ONLY(X)  X.
3fc0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56  #else.# define V
3fd0: 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69  VA_ONLY(X).#endi
3fe0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57  f../*.** The ALW
3ff0: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61  AYS and NEVER ma
4000: 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f  cros surround bo
4010: 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  olean expression
4020: 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20  s which .** are 
4030: 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61  intended to alwa
4040: 79 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61  ys be true or fa
4050: 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c  lse, respectivel
4060: 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72  y.  Such.** expr
4070: 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65  essions could be
4080: 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68   omitted from th
4090: 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c  e code completel
40a0: 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20  y.  But they.** 
40b0: 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  are included in 
40c0: 61 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f  a few cases in o
40d0: 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20  rder to enhance 
40e0: 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a  the resilience.*
40f0: 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75  * of SQLite to u
4100: 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69  nexpected behavi
4110: 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65  or - to make the
4120: 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c   code "self-heal
4130: 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74  ing".** or "duct
4140: 69 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e  ile" rather than
4150: 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22   being "brittle"
4160: 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74   and crashing at
4170: 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69   the first.** hi
4180: 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20  nt of unplanned 
4190: 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
41a0: 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
41b0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52  ALWAYS and NEVER
41c0: 20 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64   are added for d
41d0: 65 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a  efensive code..*
41e0: 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20  *.** When doing 
41f0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
4200: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45   ALWAYS and NEVE
4210: 52 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64  R are hard-coded
4220: 20 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61   to.** be true a
4230: 6e 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74  nd false so that
4240: 20 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65   the unreachable
4250: 20 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69   code then speci
4260: 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62  fy will.** not b
4270: 65 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74  e counted as unt
4280: 65 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23  ested code..*/.#
4290: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
42a0: 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29  E_COVERAGE_TEST)
42b0: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53  .# define ALWAYS
42c0: 28 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64  (X)      (1).# d
42d0: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20  efine NEVER(X)  
42e0: 20 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21       (0).#elif !
42f0: 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a  defined(NDEBUG).
4300: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28  # define ALWAYS(
4310: 58 29 20 20 20 20 20 20 28 28 58 29 3f 31 3a 28  X)      ((X)?1:(
4320: 61 73 73 65 72 74 28 30 29 2c 30 29 29 0a 23 20  assert(0),0)).# 
4330: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20  define NEVER(X) 
4340: 20 20 20 20 20 20 28 28 58 29 3f 28 61 73 73 65        ((X)?(asse
4350: 72 74 28 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73  rt(0),1):0).#els
4360: 65 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59  e.# define ALWAY
4370: 53 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20  S(X)      (X).# 
4380: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20  define NEVER(X) 
4390: 20 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66        (X).#endif
43a0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72  ../*.** The macr
43b0: 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20  o unlikely() is 
43c0: 61 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72  a hint that surr
43d0: 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a  ounds a boolean.
43e0: 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ** expression th
43f0: 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61  at is usually fa
4400: 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65  lse.  Macro like
4410: 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a  ly() surrounds.*
4420: 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72  * a boolean expr
4430: 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75  ession that is u
4440: 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43  sually true.  GC
4450: 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20  C is able to.** 
4460: 75 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20  use these hints 
4470: 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74  to generate bett
4480: 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d  er code, sometim
4490: 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  es..*/.#if defin
44a0: 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
44b0: 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c  0.# define likel
44c0: 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69  y(X)    __builti
44d0: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a  n_expect((X),1).
44e0: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c  # define unlikel
44f0: 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f  y(X)  __builtin_
4500: 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65  expect((X),0).#e
4510: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b  lse.# define lik
4520: 65 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a  ely(X)    !!(X).
4530: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c  # define unlikel
4540: 79 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64  y(X)  !!(X).#end
4550: 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
4560: 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69  *** Include sqli
4570: 74 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  te3.h in the mid
4580: 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
4590: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
45a0: 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
45b0: 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
45c0: 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a  qlite3.h *******
45d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45f0: 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
4600: 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
4610: 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
4620: 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
4630: 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
4640: 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
4650: 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
4660: 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
4670: 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
4680: 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
4690: 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
46a0: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
46b0: 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
46c0: 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
46d0: 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
46e0: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
46f0: 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
4700: 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
4710: 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
4720: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
4730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
4770: 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64  is header file d
4780: 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
4790: 66 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51  face that the SQ
47a0: 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20  Lite library.** 
47b0: 70 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65  presents to clie
47c0: 6e 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66  nt programs.  If
47d0: 20 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73   a C-function, s
47e0: 74 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79  tructure, dataty
47f0: 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61  pe,.** or consta
4800: 6e 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f  nt definition do
4810: 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e  es not appear in
4820: 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e   this file, then
4830: 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20   it is.** not a 
4840: 70 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66  published API of
4850: 20 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a   SQLite, is subj
4860: 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69  ect to change wi
4870: 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c  thout.** notice,
4880: 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20   and should not 
4890: 62 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  be referenced by
48a0: 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75   programs that u
48b0: 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  se SQLite..**.**
48c0: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66   Some of the def
48d0: 69 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72  initions that ar
48e0: 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61  e in this file a
48f0: 72 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20  re marked as.** 
4900: 22 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20  "experimental". 
4910: 20 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e   Experimental in
4920: 74 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72  terfaces are nor
4930: 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61  mally new.** fea
4940: 74 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61  tures recently a
4950: 64 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20  dded to SQLite. 
4960: 20 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63   We do not antic
4970: 69 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a  ipate changes.**
4980: 20 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c   to experimental
4990: 20 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20   interfaces but 
49a0: 72 65 73 65 72 76 65 20 74 68 65 20 72 69 67 68  reserve the righ
49b0: 74 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20  t to make minor 
49c0: 63 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78  changes.** if ex
49d0: 70 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73  perience from us
49e0: 65 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20  e "in the wild" 
49f0: 73 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61  suggest such cha
4a00: 6e 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74  nges are prudent
4a10: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69  ..**.** The offi
4a20: 63 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20  cial C-language 
4a30: 41 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  API documentatio
4a40: 6e 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20  n for SQLite is 
4a50: 64 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20  derived.** from 
4a60: 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73  comments in this
4a70: 20 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c   file.  This fil
4a80: 65 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69  e is the authori
4a90: 74 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a  tative source.**
4aa0: 20 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69   on how SQLite i
4ab0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75  nterfaces are su
4ac0: 70 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65  ppose to operate
4ad0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  ..**.** The name
4ae0: 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e   of this file un
4af0: 64 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  der configuratio
4b00: 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20  n management is 
4b10: 22 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a  "sqlite.h.in"..*
4b20: 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d  * The makefile m
4b30: 61 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20  akes some minor 
4b40: 63 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20  changes to this 
4b50: 66 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e  file (such as in
4b60: 73 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76  serting.** the v
4b70: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61  ersion number) a
4b80: 6e 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e  nd changes its n
4b90: 61 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e  ame to "sqlite3.
4ba0: 68 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66  h" as.** part of
4bb0: 20 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65   the build proce
4bc0: 73 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  ss..*/.#ifndef _
4bd0: 53 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 69  SQLITE3_H_.#defi
4be0: 6e 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23  ne _SQLITE3_H_.#
4bf0: 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e  include <stdarg.
4c00: 68 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 64  h>     /* Needed
4c10: 20 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 74   for the definit
4c20: 69 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a  ion of va_list *
4c30: 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  /../*.** Make su
4c40: 72 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74  re we can call t
4c50: 68 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43  his stuff from C
4c60: 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74  ++..*/.#if 0.ext
4c70: 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66  ern "C" {.#endif
4c80: 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65  .../*.** Add the
4c90: 20 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72   ability to over
4ca0: 72 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f  ride 'extern'.*/
4cb0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
4cc0: 45 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 20  EXTERN.# define 
4cd0: 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 78  SQLITE_EXTERN ex
4ce0: 74 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66  tern.#endif..#if
4cf0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a  ndef SQLITE_API.
4d00: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
4d10: 41 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  API.#endif.../*.
4d20: 2a 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d  ** These no-op m
4d30: 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 69  acros are used i
4d40: 6e 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72  n front of inter
4d50: 66 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74 68  faces to mark th
4d60: 6f 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ose.** interface
4d70: 73 20 61 73 20 65 69 74 68 65 72 20 64 65 70 72  s as either depr
4d80: 65 63 61 74 65 64 20 6f 72 20 65 78 70 65 72 69  ecated or experi
4d90: 6d 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70 70  mental.  New app
4da0: 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f  lications.** sho
4db0: 75 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70 72  uld not use depr
4dc0: 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65  ecated interface
4dd0: 73 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70  s - they are sup
4de0: 70 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72  port for backwar
4df0: 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c  ds.** compatibil
4e00: 69 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69  ity only.  Appli
4e10: 63 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73  cation writers s
4e20: 68 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74  hould be aware t
4e30: 68 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e  hat.** experimen
4e40: 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61  tal interfaces a
4e50: 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  re subject to ch
4e60: 61 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65  ange in point re
4e70: 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  leases..**.** Th
4e80: 65 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20  ese macros used 
4e90: 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61  to resolve to va
4ea0: 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63  rious kinds of c
4eb0: 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68  ompiler magic th
4ec0: 61 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65  at.** would gene
4ed0: 72 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73  rate warning mes
4ee0: 73 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20  sages when they 
4ef0: 77 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20  were used.  But 
4f00: 74 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72  that.** compiler
4f10: 20 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20   magic ended up 
4f20: 67 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20  generating such 
4f30: 61 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20  a flurry of bug 
4f40: 72 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20  reports.** that 
4f50: 77 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74  we have taken it
4f60: 20 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e   all out and gon
4f70: 65 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20  e back to using 
4f80: 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d  simple.** noop m
4f90: 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  acros..*/.#defin
4fa0: 65 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41  e SQLITE_DEPRECA
4fb0: 54 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  TED.#define SQLI
4fc0: 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  TE_EXPERIMENTAL.
4fd0: 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68  ./*.** Ensure th
4fe0: 65 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65  ese symbols were
4ff0: 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20   not defined by 
5000: 73 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65  some previous he
5010: 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69  ader file..*/.#i
5020: 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53  fdef SQLITE_VERS
5030: 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  ION.# undef SQLI
5040: 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69  TE_VERSION.#endi
5050: 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
5060: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23  VERSION_NUMBER.#
5070: 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45   undef SQLITE_VE
5080: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e  RSION_NUMBER.#en
5090: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  dif../*.** CAPI3
50a0: 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d  REF: Compile-Tim
50b0: 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f  e Library Versio
50c0: 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31  n Numbers {H1001
50d0: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S60100>.**.*
50e0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52  * The SQLITE_VER
50f0: 53 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f  SION and SQLITE_
5100: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23  VERSION_NUMBER #
5110: 64 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68  defines in.** th
5120: 65 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65  e sqlite3.h file
5130: 20 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72   specify the ver
5140: 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77  sion of SQLite w
5150: 69 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61  ith which.** tha
5160: 74 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73  t header file is
5170: 20 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a   associated..**.
5180: 2a 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22  ** The "version"
5190: 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20   of SQLite is a 
51a0: 73 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f  string of the fo
51b0: 72 6d 20 22 57 2e 58 2e 59 22 20 6f 72 20 22 57  rm "W.X.Y" or "W
51c0: 2e 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20  .X.Y.Z"..** The 
51d0: 57 20 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72  W value is major
51e0: 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20   version number 
51f0: 61 6e 64 20 69 73 20 61 6c 77 61 79 73 20 33 20  and is always 3 
5200: 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54  in SQLite3..** T
5210: 68 65 20 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20  he W value only 
5220: 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63  changes when bac
5230: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
5240: 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65  lity is.** broke
5250: 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20  n and we intend 
5260: 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62  to never break b
5270: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
5280: 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58  bility..** The X
5290: 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69   value is the mi
52a0: 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  nor version numb
52b0: 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e  er and only chan
52c0: 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72  ges when.** ther
52d0: 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74  e are major feat
52e0: 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73  ure enhancements
52f0: 20 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72   that are forwar
5300: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a  ds compatible.**
5310: 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72   but not backwar
5320: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a  ds compatible..*
5330: 2a 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73  * The Y value is
5340: 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d   the release num
5350: 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65  ber and is incre
5360: 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65  mented with.** e
5370: 61 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20  ach release but 
5380: 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30  resets back to 0
5390: 20 77 68 65 6e 65 76 65 72 20 58 20 69 73 20 69   whenever X is i
53a0: 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54  ncremented..** T
53b0: 68 65 20 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20  he Z value only 
53c0: 61 70 70 65 61 72 73 20 6f 6e 20 62 72 61 6e 63  appears on branc
53d0: 68 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a  h releases..**.*
53e0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52  * The SQLITE_VER
53f0: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61  SION_NUMBER is a
5400: 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69  n integer that i
5410: 73 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a  s computed as.**
5420: 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
5430: 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
5440: 3e 0a 2a 2a 20 53 51 4c 49 54 45 5f 56 45 52 53  >.** SQLITE_VERS
5450: 49 4f 4e 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31  ION_NUMBER = W*1
5460: 30 30 30 30 30 30 20 2b 20 58 2a 31 30 30 30 20  000000 + X*1000 
5470: 2b 20 59 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  + Y.** </pre></b
5480: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
5490: 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 33   Since version 3
54a0: 2e 36 2e 31 38 2c 20 53 51 4c 69 74 65 20 73 6f  .6.18, SQLite so
54b0: 75 72 63 65 20 63 6f 64 65 20 68 61 73 20 62 65  urce code has be
54c0: 65 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  en stored in the
54d0: 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 68 74 74  .** <a href="htt
54e0: 70 3a 2f 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73  p://www.fossil-s
54f0: 63 6d 2e 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20  cm.org/">fossil 
5500: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61  configuration ma
5510: 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74  nagement.** syst
5520: 65 6d 3c 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c  em</a>.  The SQL
5530: 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a  ITE_SOURCE_ID.**
5540: 20 6d 61 63 72 6f 20 69 73 20 61 20 73 74 72 69   macro is a stri
5550: 6e 67 20 77 68 69 63 68 20 69 64 65 6e 74 69 66  ng which identif
5560: 69 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  ies a particular
5570: 20 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c   check-in of SQL
5580: 69 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74  ite.** within it
5590: 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  s configuration 
55a0: 6d 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 74 65  management syste
55b0: 6d 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 63  m.  The string c
55c0: 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64  ontains the.** d
55d0: 61 74 65 20 61 6e 64 20 74 69 6d 65 20 6f 66 20  ate and time of 
55e0: 74 68 65 20 63 68 65 63 6b 2d 69 6e 20 28 55 54  the check-in (UT
55f0: 43 29 20 61 6e 64 20 61 6e 20 53 48 41 31 20 68  C) and an SHA1 h
5600: 61 73 68 20 6f 66 20 74 68 65 20 65 6e 74 69 72  ash of the entir
5610: 65 0a 2a 2a 20 73 6f 75 72 63 65 20 74 72 65 65  e.** source tree
5620: 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
5630: 3a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  : [sqlite3_libve
5640: 72 73 69 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71  rsion()],.** [sq
5650: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
5660: 5f 6e 75 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c  _number()], [sql
5670: 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 5d  ite3_sourceid()]
5680: 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72  ,.** [sqlite_ver
5690: 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c  sion()] and [sql
56a0: 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d  ite_source_id()]
56b0: 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
56c0: 65 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b  ents: [H10011] [
56d0: 48 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69  H10014].*/.#defi
56e0: 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  ne SQLITE_VERSIO
56f0: 4e 20 20 20 20 20 20 20 20 22 33 2e 36 2e 32 31  N        "3.6.21
5700: 22 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ".#define SQLITE
5710: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20  _VERSION_NUMBER 
5720: 33 30 30 36 30 32 31 0a 23 64 65 66 69 6e 65 20  3006021.#define 
5730: 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44  SQLITE_SOURCE_ID
5740: 20 20 20 20 20 20 22 32 30 30 39 2d 31 31 2d 32        "2009-11-2
5750: 33 20 31 33 3a 31 37 3a 32 37 20 33 39 32 31 34  3 13:17:27 39214
5760: 61 65 65 36 35 35 33 64 62 37 36 33 30 39 38 35  aee6553db7630985
5770: 31 65 37 61 61 37 34 66 63 63 30 32 64 34 66 35  1e7aa74fcc02d4f5
5780: 39 62 37 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  9b7"../*.** CAPI
5790: 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c  3REF: Run-Time L
57a0: 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e  ibrary Version N
57b0: 75 6d 62 65 72 73 20 7b 48 31 30 30 32 30 7d 20  umbers {H10020} 
57c0: 3c 53 36 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57  <S60100>.** KEYW
57d0: 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 65  ORDS: sqlite3_ve
57e0: 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  rsion.**.** Thes
57f0: 65 20 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f  e interfaces pro
5800: 76 69 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e  vide the same in
5810: 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65  formation as the
5820: 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e   [SQLITE_VERSION
5830: 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45  ],.** [SQLITE_VE
5840: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61  RSION_NUMBER], a
5850: 6e 64 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43  nd [SQLITE_SOURC
5860: 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 73 20 69  E_ID] #defines i
5870: 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a  n the header,.**
5880: 20 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61   but are associa
5890: 74 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62  ted with the lib
58a0: 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20  rary instead of 
58b0: 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e  the header file.
58c0: 20 20 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72    Cautious.** pr
58d0: 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20  ogrammers might 
58e0: 69 6e 63 6c 75 64 65 20 61 73 73 65 72 74 28 29  include assert()
58f0: 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74   statements in t
5900: 68 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e  heir application
5910: 20 74 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68   to.** verify th
5920: 61 74 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  at values return
5930: 65 64 20 62 79 20 74 68 65 73 65 20 69 6e 74 65  ed by these inte
5940: 72 66 61 63 65 73 20 6d 61 74 63 68 20 74 68 65  rfaces match the
5950: 20 6d 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68   macros in.** th
5960: 65 20 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68  e header, and th
5970: 75 73 20 69 6e 73 75 72 65 20 74 68 61 74 20 74  us insure that t
5980: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69  he application i
5990: 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69  s.** compiled wi
59a0: 74 68 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72  th matching libr
59b0: 61 72 79 20 61 6e 64 20 68 65 61 64 65 72 20 66  ary and header f
59c0: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  iles..**.** <blo
59d0: 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
59e0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
59f0: 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
5a00: 65 72 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52  er()==SQLITE_VER
5a10: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a  SION_NUMBER );.*
5a20: 2a 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70  * assert( strcmp
5a30: 28 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69  (sqlite3_sourcei
5a40: 64 28 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43  d(),SQLITE_SOURC
5a50: 45 5f 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61  E_ID)==0 );.** a
5a60: 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71  ssert( strcmp(sq
5a70: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
5a80: 2c 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29  ,SQLITE_VERSION)
5a90: 3d 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e  ==0 );.** </pre>
5aa0: 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
5ab0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
5ac0: 6c 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e  libversion() fun
5ad0: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
5ae0: 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69  e same informati
5af0: 6f 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74  on as is.** in t
5b00: 68 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69  he sqlite3_versi
5b10: 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73  on[] string cons
5b20: 74 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74  tant.  The funct
5b30: 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a  ion is provided.
5b40: 2a 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c  ** for use in DL
5b50: 4c 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65  Ls since DLL use
5b60: 72 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f  rs usually do no
5b70: 74 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63  t have direct ac
5b80: 63 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a  cess to string.*
5b90: 2a 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68  * constants with
5ba0: 69 6e 20 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d  in the DLL.  Sim
5bb0: 69 6c 61 72 6c 79 2c 20 74 68 65 20 73 71 6c 69  ilarly, the sqli
5bc0: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 20 66  te3_sourceid() f
5bd0: 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72  unction.** retur
5be0: 6e 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f  ns the same info
5bf0: 72 6d 61 74 69 6f 6e 20 61 73 20 69 73 20 69 6e  rmation as is in
5c00: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55   the [SQLITE_SOU
5c10: 52 43 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 20  RCE_ID] #define 
5c20: 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  of.** the header
5c30: 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65   file..**.** See
5c40: 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76   also: [sqlite_v
5c50: 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73  ersion()] and [s
5c60: 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28  qlite_source_id(
5c70: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  )]..**.** Requir
5c80: 65 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d  ements: [H10021]
5c90: 20 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32   [H10022] [H1002
5ca0: 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  3].*/.SQLITE_API
5cb0: 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69   const char sqli
5cc0: 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20  te3_version[] = 
5cd0: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a  SQLITE_VERSION;.
5ce0: 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
5cf0: 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c   char *sqlite3_l
5d00: 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b  ibversion(void);
5d10: 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
5d20: 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
5d30: 73 6f 75 72 63 65 69 64 28 76 6f 69 64 29 3b 0a  sourceid(void);.
5d40: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
5d50: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
5d60: 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a  n_number(void);.
5d70: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
5d80: 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20   Test To See If 
5d90: 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54  The Library Is T
5da0: 68 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30  hreadsafe {H1010
5db0: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S60100>.**.*
5dc0: 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20  * SQLite can be 
5dd0: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72  compiled with or
5de0: 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73   without mutexes
5df0: 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b  .  When.** the [
5e00: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
5e10: 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  E] C preprocesso
5e20: 72 20 6d 61 63 72 6f 20 69 73 20 31 20 6f 72 20  r macro is 1 or 
5e30: 32 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72  2, mutexes.** ar
5e40: 65 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51  e enabled and SQ
5e50: 4c 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61  Lite is threadsa
5e60: 66 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a  fe.  When the.**
5e70: 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53   [SQLITE_THREADS
5e80: 41 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c  AFE] macro is 0,
5e90: 20 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73   .** the mutexes
5ea0: 20 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57   are omitted.  W
5eb0: 69 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78  ithout the mutex
5ec0: 65 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61  es, it is not sa
5ed0: 66 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c  fe.** to use SQL
5ee0: 69 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79  ite concurrently
5ef0: 20 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20   from more than 
5f00: 6f 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a  one thread..**.*
5f10: 2a 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78  * Enabling mutex
5f20: 65 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73  es incurs a meas
5f30: 75 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e  urable performan
5f40: 63 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53  ce penalty..** S
5f50: 6f 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66  o if speed is of
5f60: 20 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e   utmost importan
5f70: 63 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e  ce, it makes sen
5f80: 73 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a  se to disable.**
5f90: 20 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42   the mutexes.  B
5fa0: 75 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73  ut for maximum s
5fb0: 61 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73  afety, mutexes s
5fc0: 68 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64  hould be enabled
5fd0: 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  ..** The default
5fe0: 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72   behavior is for
5ff0: 20 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65   mutexes to be e
6000: 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  nabled..**.** Th
6010: 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e  is interface can
6020: 20 62 65 20 75 73 65 64 20 62 79 20 61 6e 20 61   be used by an a
6030: 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61  pplication to ma
6040: 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
6050: 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20 53  .** version of S
6060: 51 4c 69 74 65 20 74 68 61 74 20 69 74 20 69 73  QLite that it is
6070: 20 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74   linking against
6080: 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69   was compiled wi
6090: 74 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72 65  th.** the desire
60a0: 64 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65  d setting of the
60b0: 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53   [SQLITE_THREADS
60c0: 41 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a  AFE] macro..**.*
60d0: 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
60e0: 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e   only reports on
60f0: 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d   the compile-tim
6100: 65 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67 0a  e mutex setting.
6110: 2a 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54  ** of the [SQLIT
6120: 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66 6c  E_THREADSAFE] fl
6130: 61 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69  ag.  If SQLite i
6140: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a  s compiled with.
6150: 2a 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ** SQLITE_THREAD
6160: 53 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74 65  SAFE=1 then mute
6170: 78 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20  xes are enabled 
6180: 62 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a 2a  by default but.*
6190: 2a 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f  * can be fully o
61a0: 72 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 61  r partially disa
61b0: 62 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 6c  bled using a cal
61c0: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f  l to [sqlite3_co
61d0: 6e 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20  nfig()].** with 
61e0: 74 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 54  the verbs [SQLIT
61f0: 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54  E_CONFIG_SINGLET
6200: 48 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f  HREAD], [SQLITE_
6210: 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45  CONFIG_MULTITHRE
6220: 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49  AD],.** or [SQLI
6230: 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d  TE_CONFIG_MUTEX]
6240: 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
6250: 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63  lue of this func
6260: 74 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e  tion shows.** on
6270: 6c 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  ly the default c
6280: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74  ompile-time sett
6290: 69 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e  ing, not any run
62a0: 2d 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a  -time changes.**
62b0: 20 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67   to that setting
62c0: 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20  ..**.** See the 
62d0: 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
62e0: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
62f0: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
6300: 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
6310: 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b   Requirements: [
6320: 48 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32 5d  H10101] [H10102]
6330: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
6340: 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  nt sqlite3_threa
6350: 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a  dsafe(void);../*
6360: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61  .** CAPI3REF: Da
6370: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
6380: 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 30  n Handle {H12000
6390: 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45  } <S40200>.** KE
63a0: 59 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73  YWORDS: {databas
63b0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64  e connection} {d
63c0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
63d0: 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  ons}.**.** Each 
63e0: 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61  open SQLite data
63f0: 62 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e  base is represen
6400: 74 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72  ted by a pointer
6410: 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
6420: 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65  of.** the opaque
6430: 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64   structure named
6440: 20 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20   "sqlite3".  It 
6450: 69 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69  is useful to thi
6460: 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33  nk of an sqlite3
6470: 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61  .** pointer as a
6480: 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b  n object.  The [
6490: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
64a0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
64b0: 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  ()], and.** [sql
64c0: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20  ite3_open_v2()] 
64d0: 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69  interfaces are i
64e0: 74 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c  ts constructors,
64f0: 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c   and [sqlite3_cl
6500: 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73  ose()].** is its
6510: 20 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 68   destructor.  Th
6520: 65 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 68  ere are many oth
6530: 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 73  er interfaces (s
6540: 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74  uch as.** [sqlit
6550: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
6560: 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  , [sqlite3_creat
6570: 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61  e_function()], a
6580: 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  nd.** [sqlite3_b
6590: 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74  usy_timeout()] t
65a0: 6f 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 65  o name but three
65b0: 29 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 6f  ) that are metho
65c0: 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69  ds on an.** sqli
65d0: 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74  te3 object..*/.t
65e0: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
65f0: 6c 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a  lite3 sqlite3;..
6600: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
6610: 36 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 54  64-Bit Integer T
6620: 79 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c 53  ypes {H10200} <S
6630: 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  10110>.** KEYWOR
6640: 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  DS: sqlite_int64
6650: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a   sqlite_uint64.*
6660: 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65  *.** Because the
6670: 72 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70  re is no cross-p
6680: 6c 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 73  latform way to s
6690: 70 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 6e  pecify 64-bit in
66a0: 74 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 53  teger types.** S
66b0: 51 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 74  QLite includes t
66c0: 79 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d 62  ypedefs for 64-b
66d0: 69 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e  it signed and un
66e0: 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e  signed integers.
66f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
6700: 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c  e3_int64 and sql
6710: 69 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 20  ite3_uint64 are 
6720: 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 79  the preferred ty
6730: 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a  pe definitions..
6740: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 6e  ** The sqlite_in
6750: 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 75  t64 and sqlite_u
6760: 69 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 20  int64 types are 
6770: 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 61  supported for ba
6780: 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61  ckwards.** compa
6790: 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a  tibility only..*
67a0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
67b0: 73 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31 30  s: [H10201] [H10
67c0: 32 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53  202].*/.#ifdef S
67d0: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45  QLITE_INT64_TYPE
67e0: 0a 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54  .  typedef SQLIT
67f0: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c  E_INT64_TYPE sql
6800: 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70  ite_int64;.  typ
6810: 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51  edef unsigned SQ
6820: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20  LITE_INT64_TYPE 
6830: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23  sqlite_uint64;.#
6840: 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
6850: 43 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65  C_VER) || define
6860: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a  d(__BORLANDC__).
6870: 20 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36    typedef __int6
6880: 34 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a  4 sqlite_int64;.
6890: 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e    typedef unsign
68a0: 65 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74  ed __int64 sqlit
68b0: 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a  e_uint64;.#else.
68c0: 20 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c    typedef long l
68d0: 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69  ong int sqlite_i
68e0: 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20  nt64;.  typedef 
68f0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
6900: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69  ng int sqlite_ui
6910: 6e 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70  nt64;.#endif.typ
6920: 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36  edef sqlite_int6
6930: 34 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b  4 sqlite3_int64;
6940: 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f  .typedef sqlite_
6950: 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75  uint64 sqlite3_u
6960: 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66  int64;../*.** If
6970: 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61   compiling for a
6980: 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20   processor that 
6990: 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70  lacks floating p
69a0: 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a  oint support,.**
69b0: 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65   substitute inte
69c0: 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67  ger for floating
69d0: 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65  -point..*/.#ifde
69e0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
69f0: 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64  OATING_POINT.# d
6a00: 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c  efine double sql
6a10: 69 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69  ite3_int64.#endi
6a20: 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  f../*.** CAPI3RE
6a30: 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74  F: Closing A Dat
6a40: 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e  abase Connection
6a50: 20 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30   {H12010} <S3010
6a60: 30 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a  0><S40200>.**.**
6a70: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
6a80: 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
6a90: 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33  for the [sqlite3
6aa0: 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  ] object..**.** 
6ab0: 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73  Applications mus
6ac0: 74 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  t [sqlite3_final
6ad0: 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20  ize | finalize] 
6ae0: 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74  all [prepared st
6af0: 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64  atements].** and
6b00: 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63   [sqlite3_blob_c
6b10: 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c  lose | close] al
6b20: 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d  l [BLOB handles]
6b30: 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
6b40: 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33  .** the [sqlite3
6b50: 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74  ] object prior t
6b60: 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  o attempting to 
6b70: 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74  close the object
6b80: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69  ..**.** If [sqli
6b90: 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20  te3_close()] is 
6ba0: 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20  invoked while a 
6bb0: 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
6bc0: 70 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e  pen,.** the tran
6bd0: 73 61 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d  saction is autom
6be0: 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20  atically rolled 
6bf0: 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  back..**.** The 
6c00: 43 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  C parameter to [
6c10: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29  sqlite3_close(C)
6c20: 5d 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  ] must be either
6c30: 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74   a NULL.** point
6c40: 65 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65  er or an [sqlite
6c50: 33 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65  3] object pointe
6c60: 72 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  r obtained.** fr
6c70: 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  om [sqlite3_open
6c80: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ()], [sqlite3_op
6c90: 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b  en16()], or.** [
6ca0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
6cb0: 29 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76  )], and not prev
6cc0: 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a  iously closed..*
6cd0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
6ce0: 73 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b  s:.** [H12011] [
6cf0: 48 31 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d  H12012] [H12013]
6d00: 20 5b 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31   [H12014] [H1201
6d10: 35 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53  5] [H12019].*/.S
6d20: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
6d30: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69  lite3_close(sqli
6d40: 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54  te3 *);../*.** T
6d50: 68 65 20 74 79 70 65 20 66 6f 72 20 61 20 63 61  he type for a ca
6d60: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e  llback function.
6d70: 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61  .** This is lega
6d80: 63 79 20 61 6e 64 20 64 65 70 72 65 63 61 74 65  cy and deprecate
6d90: 64 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64  d.  It is includ
6da0: 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  ed for historica
6db0: 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69  l.** compatibili
6dc0: 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f  ty and is not do
6dd0: 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70  cumented..*/.typ
6de0: 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74  edef int (*sqlit
6df0: 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69  e3_callback)(voi
6e00: 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63  d*,int,char**, c
6e10: 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  har**);../*.** C
6e20: 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65  API3REF: One-Ste
6e30: 70 20 51 75 65 72 79 20 45 78 65 63 75 74 69 6f  p Query Executio
6e40: 6e 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32  n Interface {H12
6e50: 31 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a  100} <S10000>.**
6e60: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
6e70: 65 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65  exec() interface
6e80: 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74   is a convenient
6e90: 20 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20   way of running 
6ea0: 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53  one or more.** S
6eb0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69  QL statements wi
6ec0: 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
6ed0: 77 72 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43  write a lot of C
6ee0: 20 63 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d   code.  The UTF-
6ef0: 38 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c  8 encoded.** SQL
6f00: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
6f10: 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65  passed in as the
6f20: 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
6f30: 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65  r to sqlite3_exe
6f40: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74  c()..** The stat
6f50: 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75  ements are evalu
6f60: 61 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20  ated one by one 
6f70: 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20  until either an 
6f80: 65 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69  error or.** an i
6f90: 6e 74 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f  nterrupt is enco
6fa0: 75 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69  untered, or unti
6fb0: 6c 20 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64  l they are all d
6fc0: 6f 6e 65 2e 20 20 54 68 65 20 33 72 64 20 70 61  one.  The 3rd pa
6fd0: 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e  rameter.** is an
6fe0: 20 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61   optional callba
6ff0: 63 6b 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b  ck that is invok
7000: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
7010: 20 72 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72   row of any quer
7020: 79 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f  y.** results pro
7030: 64 75 63 65 64 20 62 79 20 74 68 65 20 53 51 4c  duced by the SQL
7040: 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68   statements.  Th
7050: 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 5th parameter 
7060: 74 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74  tells where.** t
7070: 6f 20 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f  o write any erro
7080: 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a  r messages..**.*
7090: 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73  * The error mess
70a0: 61 67 65 20 70 61 73 73 65 64 20 62 61 63 6b 20  age passed back 
70b0: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20  through the 5th 
70c0: 70 61 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c  parameter is hel
70d0: 64 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  d.** in memory o
70e0: 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
70f0: 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
7100: 20 20 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d    To avoid a mem
7110: 6f 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65  ory leak,.** the
7120: 20 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61   calling applica
7130: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c  tion should call
7140: 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
7150: 5d 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a  ] on any error.*
7160: 2a 20 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e  * message return
7170: 65 64 20 74 68 72 6f 75 67 68 20 74 68 65 20 35  ed through the 5
7180: 74 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 65  th parameter whe
7190: 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65  n it has finishe
71a0: 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65  d using.** the e
71b0: 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a  rror message..**
71c0: 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73  .** If the SQL s
71d0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
71e0: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  2nd parameter is
71f0: 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74   NULL or an empt
7200: 79 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61  y string.** or a
7210: 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69   string containi
7220: 6e 67 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61  ng only whitespa
7230: 63 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c  ce and comments,
7240: 20 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20   then no SQL.** 
7250: 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65  statements are e
7260: 76 61 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65  valuated and the
7270: 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74   database is not
7280: 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
7290: 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  The sqlite3_exec
72a0: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
72b0: 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74  implemented in t
72c0: 65 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69  erms of.** [sqli
72d0: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
72e0: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ], [sqlite3_step
72f0: 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
7300: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a  3_finalize()]..*
7310: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78  * The sqlite3_ex
7320: 65 63 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  ec() routine doe
7330: 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65  s nothing to the
7340: 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63   database that c
7350: 61 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a  annot be done.**
7360: 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   by [sqlite3_pre
7370: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c  pare_v2()], [sql
7380: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e  ite3_step()], an
7390: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  d [sqlite3_final
73a0: 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  ize()]..**.** Th
73b0: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
73c0: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78  r to [sqlite3_ex
73d0: 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e  ec()] must be an
73e0: 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a   valid and open.
73f0: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
7400: 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20  nection]..**.** 
7410: 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
7420: 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74  nection must not
7430: 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65   be closed while
7440: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65  .** [sqlite3_exe
7450: 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e  c()] is running.
7460: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  .**.** The calli
7470: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  ng function shou
7480: 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f  ld use [sqlite3_
7490: 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a  free()] to free.
74a0: 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68  ** the memory th
74b0: 61 74 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65  at *errmsg is le
74c0: 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f  ft pointing at o
74d0: 6e 63 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a  nce the error.**
74e0: 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c   message is no l
74f0: 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a  onger needed..**
7500: 0a 2a 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74  .** The SQL stat
7510: 65 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68  ement text in th
7520: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
7530: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  to [sqlite3_exec
7540: 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61  ()].** must rema
7550: 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69  in unchanged whi
7560: 6c 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  le [sqlite3_exec
7570: 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a  ()] is running..
7580: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
7590: 74 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20  ts:.** [H12101] 
75a0: 5b 48 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34  [H12102] [H12104
75b0: 5d 20 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31  ] [H12105] [H121
75c0: 30 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31  07] [H12110] [H1
75d0: 32 31 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a  2113] [H12116].*
75e0: 2a 20 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31  * [H12119] [H121
75f0: 32 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31  22] [H12125] [H1
7600: 32 31 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b  2131] [H12134] [
7610: 48 31 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d  H12137] [H12138]
7620: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
7630: 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  nt sqlite3_exec(
7640: 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20  .  sqlite3*,    
7650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7670: 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73   An open databas
7680: 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
7690: 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20  r *sql,         
76a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76b0: 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65    /* SQL to be e
76c0: 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e  valuated */.  in
76d0: 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f  t (*callback)(vo
76e0: 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63  id*,int,char**,c
76f0: 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c  har**),  /* Call
7700: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  back function */
7710: 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20  .  void *,      
7720: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7740: 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f   1st argument to
7750: 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63   callback */.  c
7760: 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20  har **errmsg    
7770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7780: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
7790: 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68  or msg written h
77a0: 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ere */.);../*.**
77b0: 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c   CAPI3REF: Resul
77c0: 74 20 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d  t Codes {H10210}
77d0: 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59   <S10700>.** KEY
77e0: 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b  WORDS: SQLITE_OK
77f0: 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65   {error code} {e
7800: 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b  rror codes}.** K
7810: 45 59 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74  EYWORDS: {result
7820: 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63   code} {result c
7830: 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79  odes}.**.** Many
7840: 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e   SQLite function
7850: 73 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65  s return an inte
7860: 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20  ger result code 
7870: 66 72 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f  from the set sho
7880: 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72  wn.** here in or
7890: 64 65 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73  der to indicates
78a0: 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c   success or fail
78b0: 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65  ure..**.** New e
78c0: 72 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62  rror codes may b
78d0: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
78e0: 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
78f0: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  Lite..**.** See 
7900: 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f  also: [SQLITE_IO
7910: 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e  ERR_READ | exten
7920: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
7930: 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ].*/.#define SQL
7940: 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20  ITE_OK          
7950: 20 30 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66   0   /* Successf
7960: 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20  ul result */./* 
7970: 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72  beginning-of-err
7980: 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66  or-codes */.#def
7990: 69 6e 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  ine SQLITE_ERROR
79a0: 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53          1   /* S
79b0: 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73  QL error or miss
79c0: 69 6e 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ing database */.
79d0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
79e0: 4e 54 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20  NTERNAL     2   
79f0: 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69  /* Internal logi
7a00: 63 20 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74  c error in SQLit
7a10: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
7a20: 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20  ITE_PERM        
7a30: 20 33 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70   3   /* Access p
7a40: 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64  ermission denied
7a50: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
7a60: 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20  TE_ABORT        
7a70: 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20  4   /* Callback 
7a80: 72 6f 75 74 69 6e 65 20 72 65 71 75 65 73 74 65  routine requeste
7a90: 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64  d an abort */.#d
7aa0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53  efine SQLITE_BUS
7ab0: 59 20 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a  Y         5   /*
7ac0: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
7ad0: 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a  le is locked */.
7ae0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
7af0: 4f 43 4b 45 44 20 20 20 20 20 20 20 36 20 20 20  OCKED       6   
7b00: 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68  /* A table in th
7b10: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f  e database is lo
7b20: 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cked */.#define 
7b30: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20  SQLITE_NOMEM    
7b40: 20 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c      7   /* A mal
7b50: 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a  loc() failed */.
7b60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
7b70: 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20  EADONLY     8   
7b80: 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72  /* Attempt to wr
7b90: 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64  ite a readonly d
7ba0: 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69  atabase */.#defi
7bb0: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  ne SQLITE_INTERR
7bc0: 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70  UPT    9   /* Op
7bd0: 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74  eration terminat
7be0: 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e  ed by sqlite3_in
7bf0: 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66  terrupt()*/.#def
7c00: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
7c10: 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53         10   /* S
7c20: 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b  ome kind of disk
7c30: 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72   I/O error occur
7c40: 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  red */.#define S
7c50: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20  QLITE_CORRUPT   
7c60: 20 20 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61    11   /* The da
7c70: 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67  tabase disk imag
7c80: 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a  e is malformed *
7c90: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7ca0: 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20  _NOTFOUND    12 
7cb0: 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54    /* NOT USED. T
7cc0: 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e  able or record n
7cd0: 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66  ot found */.#def
7ce0: 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  ine SQLITE_FULL 
7cf0: 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49         13   /* I
7d00: 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20  nsertion failed 
7d10: 62 65 63 61 75 73 65 20 64 61 74 61 62 61 73 65  because database
7d20: 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66   is full */.#def
7d30: 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  ine SQLITE_CANTO
7d40: 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55  PEN    14   /* U
7d50: 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68  nable to open th
7d60: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
7d70: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7d80: 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35  E_PROTOCOL    15
7d90: 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20     /* NOT USED. 
7da0: 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72  Database lock pr
7db0: 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a  otocol error */.
7dc0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45  #define SQLITE_E
7dd0: 4d 50 54 59 20 20 20 20 20 20 20 31 36 20 20 20  MPTY       16   
7de0: 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 65  /* Database is e
7df0: 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mpty */.#define 
7e00: 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20  SQLITE_SCHEMA   
7e10: 20 20 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64     17   /* The d
7e20: 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63  atabase schema c
7e30: 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e  hanged */.#defin
7e40: 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20  e SQLITE_TOOBIG 
7e50: 20 20 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72       18   /* Str
7e60: 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65  ing or BLOB exce
7e70: 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a  eds size limit *
7e80: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7e90: 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20  _CONSTRAINT  19 
7ea0: 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74    /* Abort due t
7eb0: 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  o constraint vio
7ec0: 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  lation */.#defin
7ed0: 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43  e SQLITE_MISMATC
7ee0: 48 20 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74  H    20   /* Dat
7ef0: 61 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20  a type mismatch 
7f00: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7f10: 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20 32 31  E_MISUSE      21
7f20: 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73     /* Library us
7f30: 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a  ed incorrectly *
7f40: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7f50: 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20  _NOLFS       22 
7f60: 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61    /* Uses OS fea
7f70: 74 75 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72  tures not suppor
7f80: 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23  ted on host */.#
7f90: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55  define SQLITE_AU
7fa0: 54 48 20 20 20 20 20 20 20 20 32 33 20 20 20 2f  TH        23   /
7fb0: 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  * Authorization 
7fc0: 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e  denied */.#defin
7fd0: 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20  e SQLITE_FORMAT 
7fe0: 20 20 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78       24   /* Aux
7ff0: 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
8000: 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a  format error */.
8010: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
8020: 41 4e 47 45 20 20 20 20 20 20 20 32 35 20 20 20  ANGE       25   
8030: 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  /* 2nd parameter
8040: 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64   to sqlite3_bind
8050: 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f   out of range */
8060: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8070: 4e 4f 54 41 44 42 20 20 20 20 20 20 32 36 20 20  NOTADB      26  
8080: 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20   /* File opened 
8090: 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61  that is not a da
80a0: 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23  tabase file */.#
80b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f  define SQLITE_RO
80c0: 57 20 20 20 20 20 20 20 20 20 31 30 30 20 20 2f  W         100  /
80d0: 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
80e0: 20 68 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77   has another row
80f0: 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e   ready */.#defin
8100: 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20  e SQLITE_DONE   
8110: 20 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c       101  /* sql
8120: 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20  ite3_step() has 
8130: 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69  finished executi
8140: 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d  ng */./* end-of-
8150: 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a  error-codes */..
8160: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
8170: 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20  Extended Result 
8180: 43 6f 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c  Codes {H10220} <
8190: 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S10700>.** KEYWO
81a0: 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65  RDS: {extended e
81b0: 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65  rror code} {exte
81c0: 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73  nded error codes
81d0: 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  }.** KEYWORDS: {
81e0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
81f0: 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20  code} {extended 
8200: 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a  result codes}.**
8210: 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75  .** In its defau
8220: 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  lt configuration
8230: 2c 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75  , SQLite API rou
8240: 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65  tines return one
8250: 20 6f 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a   of 26 integer.*
8260: 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72  * [SQLITE_OK | r
8270: 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48  esult codes].  H
8280: 6f 77 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e  owever, experien
8290: 63 65 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61  ce has shown tha
82a0: 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65  t many of.** the
82b0: 73 65 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  se result codes 
82c0: 61 72 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67  are too coarse-g
82d0: 72 61 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f  rained.  They do
82e0: 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a   not provide as.
82f0: 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74  ** much informat
8300: 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65  ion about proble
8310: 6d 73 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72  ms as programmer
8320: 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49  s might like.  I
8330: 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a  n an effort to.*
8340: 2a 20 61 64 64 72 65 73 73 20 74 68 69 73 2c 20  * address this, 
8350: 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  newer versions o
8360: 66 20 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f  f SQLite (versio
8370: 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65  n 3.3.8 and late
8380: 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75  r) include.** su
8390: 70 70 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69  pport for additi
83a0: 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65  onal result code
83b0: 73 20 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d  s that provide m
83c0: 6f 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66  ore detailed inf
83d0: 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75  ormation.** abou
83e0: 74 20 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78  t errors. The ex
83f0: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
8400: 64 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20  des are enabled 
8410: 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f  or disabled.** o
8420: 6e 20 61 20 70 65 72 20 64 61 74 61 62 61 73 65  n a per database
8430: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69   connection basi
8440: 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b  s using the.** [
8450: 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
8460: 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d  _result_codes()]
8470: 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65   API..**.** Some
8480: 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c   of the availabl
8490: 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  e extended resul
84a0: 74 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74  t codes are list
84b0: 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20  ed here..** One 
84c0: 6d 61 79 20 65 78 70 65 63 74 20 74 68 65 20 6e  may expect the n
84d0: 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65  umber of extende
84e0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77  d result codes w
84f0: 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a  ill be expand.**
8500: 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66   over time.  Sof
8510: 74 77 61 72 65 20 74 68 61 74 20 75 73 65 73 20  tware that uses 
8520: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
8530: 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70  codes should exp
8540: 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65  ect.** to see ne
8550: 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69  w result codes i
8560: 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
8570: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s of SQLite..**.
8580: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b  ** The SQLITE_OK
8590: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c   result code wil
85a0: 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e  l never be exten
85b0: 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c  ded.  It will al
85c0: 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74  ways.** be exact
85d0: 6c 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66  ly zero..*/.#def
85e0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
85f0: 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20  _READ           
8600: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8610: 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69   | (1<<8)).#defi
8620: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
8630: 53 48 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20  SHORT_READ      
8640: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
8650: 7c 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (2<<8)).#defin
8660: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  e SQLITE_IOERR_W
8670: 52 49 54 45 20 20 20 20 20 20 20 20 20 20 20 20  RITE            
8680: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8690: 20 28 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (3<<8)).#define
86a0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53   SQLITE_IOERR_FS
86b0: 59 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20  YNC             
86c0: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
86d0: 28 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (4<<8)).#define 
86e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52  SQLITE_IOERR_DIR
86f0: 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 28  _FSYNC         (
8700: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
8710: 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  5<<8)).#define S
8720: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
8730: 43 41 54 45 20 20 20 20 20 20 20 20 20 20 28 53  CATE          (S
8740: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36  QLITE_IOERR | (6
8750: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
8760: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
8770: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51               (SQ
8780: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c  LITE_IOERR | (7<
8790: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
87a0: 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
87b0: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
87c0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c  ITE_IOERR | (8<<
87d0: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
87e0: 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20  TE_IOERR_RDLOCK 
87f0: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
8800: 54 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38  TE_IOERR | (9<<8
8810: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8820: 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20  E_IOERR_DELETE  
8830: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8840: 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38  E_IOERR | (10<<8
8850: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8860: 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20  E_IOERR_BLOCKED 
8870: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8880: 45 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38  E_IOERR | (11<<8
8890: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
88a0: 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20  E_IOERR_NOMEM   
88b0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
88c0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38  E_IOERR | (12<<8
88d0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
88e0: 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20  E_IOERR_ACCESS  
88f0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8900: 45 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38  E_IOERR | (13<<8
8910: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8920: 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
8930: 45 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54  ERVEDLOCK (SQLIT
8940: 45 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38  E_IOERR | (14<<8
8950: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8960: 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20  E_IOERR_LOCK    
8970: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8980: 45 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38  E_IOERR | (15<<8
8990: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
89a0: 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20  E_IOERR_CLOSE   
89b0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
89c0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38  E_IOERR | (16<<8
89d0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
89e0: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53  E_IOERR_DIR_CLOS
89f0: 45 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  E         (SQLIT
8a00: 45 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38  E_IOERR | (17<<8
8a10: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8a20: 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
8a30: 41 43 48 45 20 20 20 20 20 20 28 53 51 4c 49 54  ACHE      (SQLIT
8a40: 45 5f 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38  E_LOCKED | (1<<8
8a50: 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ) )../*.** CAPI3
8a60: 52 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46  REF: Flags For F
8a70: 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69  ile Open Operati
8a80: 6f 6e 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31  ons {H10230} <H1
8a90: 31 31 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a  1120> <H12700>.*
8aa0: 2a 0a 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76  *.** These bit v
8ab0: 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64  alues are intend
8ac0: 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68  ed for use in th
8ad0: 65 0a 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74  e.** 3rd paramet
8ae0: 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  er to the [sqlit
8af0: 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e  e3_open_v2()] in
8b00: 74 65 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69  terface and.** i
8b10: 6e 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  n the 4th parame
8b20: 74 65 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e  ter to the xOpen
8b30: 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a   method of the.*
8b40: 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  * [sqlite3_vfs] 
8b50: 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69  object..*/.#defi
8b60: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  ne SQLITE_OPEN_R
8b70: 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20  EADONLY         
8b80: 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f  0x00000001  /* O
8b90: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8ba0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8bb0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  ne SQLITE_OPEN_R
8bc0: 45 41 44 57 52 49 54 45 20 20 20 20 20 20 20 20  EADWRITE        
8bd0: 30 78 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f  0x00000002  /* O
8be0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8bf0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8c00: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  ne SQLITE_OPEN_C
8c10: 52 45 41 54 45 20 20 20 20 20 20 20 20 20 20 20  REATE           
8c20: 30 78 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f  0x00000004  /* O
8c30: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8c40: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8c50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44  ne SQLITE_OPEN_D
8c60: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20  ELETEONCLOSE    
8c70: 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56  0x00000008  /* V
8c80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8c90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45  ne SQLITE_OPEN_E
8ca0: 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20  XCLUSIVE        
8cb0: 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56  0x00000010  /* V
8cc0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8cd0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8ce0: 41 49 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20  AIN_DB          
8cf0: 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56  0x00000100  /* V
8d00: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8d10: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8d20: 45 4d 50 5f 44 42 20 20 20 20 20 20 20 20 20 20  EMP_DB          
8d30: 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56  0x00000200  /* V
8d40: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8d50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8d60: 52 41 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20  RANSIENT_DB     
8d70: 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56  0x00000400  /* V
8d80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8d90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8da0: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20  AIN_JOURNAL     
8db0: 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56  0x00000800  /* V
8dc0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8dd0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8de0: 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20  EMP_JOURNAL     
8df0: 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56  0x00001000  /* V
8e00: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8e10: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  ne SQLITE_OPEN_S
8e20: 55 42 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20  UBJOURNAL       
8e30: 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56  0x00002000  /* V
8e40: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8e50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8e60: 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20  ASTER_JOURNAL   
8e70: 30 78 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56  0x00004000  /* V
8e80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8e90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e  ne SQLITE_OPEN_N
8ea0: 4f 4d 55 54 45 58 20 20 20 20 20 20 20 20 20 20  OMUTEX          
8eb0: 30 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f  0x00008000  /* O
8ec0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8ed0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8ee0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46  ne SQLITE_OPEN_F
8ef0: 55 4c 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20  ULLMUTEX        
8f00: 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f  0x00010000  /* O
8f10: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8f20: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8f30: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  ne SQLITE_OPEN_S
8f40: 48 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20  HAREDCACHE      
8f50: 30 78 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f  0x00020000  /* O
8f60: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8f70: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8f80: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50  ne SQLITE_OPEN_P
8f90: 52 49 56 41 54 45 43 41 43 48 45 20 20 20 20 20  RIVATECACHE     
8fa0: 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f  0x00040000  /* O
8fb0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8fc0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a  en_v2() */../*.*
8fd0: 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 76 69  * CAPI3REF: Devi
8fe0: 63 65 20 43 68 61 72 61 63 74 65 72 69 73 74 69  ce Characteristi
8ff0: 63 73 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31  cs {H10240} <H11
9000: 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  120>.**.** The x
9010: 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69  DeviceCapabiliti
9020: 65 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  es method of the
9030: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
9040: 68 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20  hods].** object 
9050: 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67  returns an integ
9060: 65 72 20 77 68 69 63 68 20 69 73 20 61 20 76 65  er which is a ve
9070: 63 74 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73  ctor of the thes
9080: 65 0a 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20  e.** bit values 
9090: 65 78 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63  expressing I/O c
90a0: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f  haracteristics o
90b0: 66 20 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61  f the mass stora
90c0: 67 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61  ge.** device tha
90d0: 74 20 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65  t holds the file
90e0: 20 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74   that the [sqlit
90f0: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a  e3_io_methods].*
9100: 2a 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a  * refers to..**.
9110: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f  ** The SQLITE_IO
9120: 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65  CAP_ATOMIC prope
9130: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61  rty means that a
9140: 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20  ll writes of.** 
9150: 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f  any size are ato
9160: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45  mic.  The SQLITE
9170: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e  _IOCAP_ATOMICnnn
9180: 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20   values.** mean 
9190: 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62  that writes of b
91a0: 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e  locks that are n
91b0: 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  nn bytes in size
91c0: 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67   and.** are alig
91d0: 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73  ned to an addres
91e0: 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e  s which is an in
91f0: 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f  teger multiple o
9200: 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f  f.** nnn are ato
9210: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45  mic.  The SQLITE
9220: 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
9230: 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a  ND value means.*
9240: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61  * that when data
9250: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
9260: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61  a file, the data
9270: 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20   is appended.** 
9280: 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73  first then the s
9290: 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
92a0: 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76  is extended, nev
92b0: 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20  er the other.** 
92c0: 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65  way around.  The
92d0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45   SQLITE_IOCAP_SE
92e0: 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74  QUENTIAL propert
92f0: 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20  y means that.** 
9300: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77  information is w
9310: 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69  ritten to disk i
9320: 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72  n the same order
9330: 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20   as calls.** to 
9340: 78 57 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65  xWrite()..*/.#de
9350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9360: 50 5f 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20  P_ATOMIC        
9370: 20 20 30 78 30 30 30 30 30 30 30 31 0a 23 64 65    0x00000001.#de
9380: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9390: 50 5f 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20  P_ATOMIC512     
93a0: 20 20 30 78 30 30 30 30 30 30 30 32 0a 23 64 65    0x00000002.#de
93b0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
93c0: 50 5f 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20  P_ATOMIC1K      
93d0: 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65    0x00000004.#de
93e0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
93f0: 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20  P_ATOMIC2K      
9400: 20 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 65    0x00000008.#de
9410: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9420: 50 5f 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20  P_ATOMIC4K      
9430: 20 20 30 78 30 30 30 30 30 30 31 30 0a 23 64 65    0x00000010.#de
9440: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9450: 50 5f 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20  P_ATOMIC8K      
9460: 20 20 30 78 30 30 30 30 30 30 32 30 0a 23 64 65    0x00000020.#de
9470: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9480: 50 5f 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20  P_ATOMIC16K     
9490: 20 20 30 78 30 30 30 30 30 30 34 30 0a 23 64 65    0x00000040.#de
94a0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
94b0: 50 5f 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20  P_ATOMIC32K     
94c0: 20 20 30 78 30 30 30 30 30 30 38 30 0a 23 64 65    0x00000080.#de
94d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
94e0: 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20  P_ATOMIC64K     
94f0: 20 20 30 78 30 30 30 30 30 31 30 30 0a 23 64 65    0x00000100.#de
9500: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9510: 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20  P_SAFE_APPEND   
9520: 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 65    0x00000200.#de
9530: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9540: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20  P_SEQUENTIAL    
9550: 20 20 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a    0x00000400../*
9560: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69  .** CAPI3REF: Fi
9570: 6c 65 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c  le Locking Level
9580: 73 20 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31  s {H10250} <H111
9590: 32 30 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a  20> <H11310>.**.
95a0: 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f  ** SQLite uses o
95b0: 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ne of these inte
95c0: 67 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68  ger values as th
95d0: 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75  e second.** argu
95e0: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74  ment to calls it
95f0: 20 6d 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c   makes to the xL
9600: 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63  ock() and xUnloc
9610: 6b 28 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f  k() methods.** o
9620: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f  f an [sqlite3_io
9630: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74  _methods] object
9640: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
9650: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20  ITE_LOCK_NONE   
9660: 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65         0.#define
9670: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41   SQLITE_LOCK_SHA
9680: 52 45 44 20 20 20 20 20 20 20 20 31 0a 23 64 65  RED        1.#de
9690: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  fine SQLITE_LOCK
96a0: 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 32  _RESERVED      2
96b0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
96c0: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20  LOCK_PENDING    
96d0: 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
96e0: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49  ITE_LOCK_EXCLUSI
96f0: 56 45 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20  VE     4../*.** 
9700: 43 41 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72  CAPI3REF: Synchr
9710: 6f 6e 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46  onization Type F
9720: 6c 61 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48  lags {H10260} <H
9730: 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65  11120>.**.** Whe
9740: 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73  n SQLite invokes
9750: 20 74 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74   the xSync() met
9760: 68 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71  hod of an.** [sq
9770: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
9780: 5d 20 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73  ] object it uses
9790: 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f   a combination o
97a0: 66 0a 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67  f.** these integ
97b0: 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65  er values as the
97c0: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
97d0: 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
97e0: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54   SQLITE_SYNC_DAT
97f0: 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73  AONLY flag is us
9800: 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ed, it means tha
9810: 74 20 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70  t the.** sync op
9820: 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65  eration only nee
9830: 64 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61  ds to flush data
9840: 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65   to mass storage
9850: 2e 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f  .  Inode.** info
9860: 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74  rmation need not
9870: 20 62 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20   be flushed. If 
9880: 74 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62  the lower four b
9890: 69 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a  its of the flag.
98a0: 2a 2a 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f  ** equal SQLITE_
98b0: 53 59 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61  SYNC_NORMAL, tha
98c0: 74 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e  t means to use n
98d0: 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65  ormal fsync() se
98e0: 6d 61 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74  mantics..** If t
98f0: 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69  he lower four bi
9900: 74 73 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f  ts equal SQLITE_
9910: 53 59 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20  SYNC_FULL, that 
9920: 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20  means.** to use 
9930: 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66  Mac OS X style f
9940: 75 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20  ullsync instead 
9950: 6f 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23  of fsync()..*/.#
9960: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59  define SQLITE_SY
9970: 4e 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20  NC_NORMAL       
9980: 20 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65   0x00002.#define
9990: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c   SQLITE_SYNC_FUL
99a0: 4c 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  L          0x000
99b0: 30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  03.#define SQLIT
99c0: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20  E_SYNC_DATAONLY 
99d0: 20 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a       0x00010../*
99e0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53  .** CAPI3REF: OS
99f0: 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20   Interface Open 
9a00: 46 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31  File Handle {H11
9a10: 31 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a  110} <S20110>.**
9a20: 0a 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f  .** An [sqlite3_
9a30: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70  file] object rep
9a40: 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20  resents an open 
9a50: 66 69 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20  file in the .** 
9a60: 5b 73 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f  [sqlite3_vfs | O
9a70: 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65  S interface laye
9a80: 72 5d 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20  r].  Individual 
9a90: 4f 53 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  OS interface.** 
9aa0: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
9ab0: 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20  will.** want to 
9ac0: 73 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62  subclass this ob
9ad0: 6a 65 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e  ject by appendin
9ae0: 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65  g additional fie
9af0: 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72  lds.** for their
9b00: 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70   own use.  The p
9b10: 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73  Methods entry is
9b20: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
9b30: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  .** [sqlite3_io_
9b40: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
9b50: 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74  that defines met
9b60: 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  hods for perform
9b70: 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61  ing.** I/O opera
9b80: 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65  tions on the ope
9b90: 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64  n file..*/.typed
9ba0: 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
9bb0: 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66  3_file sqlite3_f
9bc0: 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  ile;.struct sqli
9bd0: 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e  te3_file {.  con
9be0: 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  st struct sqlite
9bf0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
9c00: 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68  ethods;  /* Meth
9c10: 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20  ods for an open 
9c20: 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  file */.};../*.*
9c30: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49  * CAPI3REF: OS I
9c40: 6e 74 65 72 66 61 63 65 20 46 69 6c 65 20 56 69  nterface File Vi
9c50: 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62  rtual Methods Ob
9c60: 6a 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53  ject {H11120} <S
9c70: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65  20110>.**.** Eve
9c80: 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62  ry file opened b
9c90: 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  y the [sqlite3_v
9ca0: 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  fs] xOpen method
9cb0: 20 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a   populates an.**
9cc0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20   [sqlite3_file] 
9cd0: 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65  object (or, more
9ce0: 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62   commonly, a sub
9cf0: 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20  class of the.** 
9d00: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f  [sqlite3_file] o
9d10: 62 6a 65 63 74 29 20 77 69 74 68 20 61 20 70 6f  bject) with a po
9d20: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
9d30: 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
9d40: 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a  ect..** This obj
9d50: 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  ect defines the 
9d60: 6d 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20  methods used to 
9d70: 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20  perform various 
9d80: 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67  operations.** ag
9d90: 61 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66  ainst the open f
9da0: 69 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64 20  ile represented 
9db0: 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  by the [sqlite3_
9dc0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  file] object..**
9dd0: 0a 2a 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e  .** If the xOpen
9de0: 20 6d 65 74 68 6f 64 20 73 65 74 73 20 74 68 65   method sets the
9df0: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d   sqlite3_file.pM
9e00: 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a  ethods element .
9e10: 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c  ** to a non-NULL
9e20: 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74   pointer, then t
9e30: 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
9e40: 74 68 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74  thods.xClose met
9e50: 68 6f 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e  hod.** may be in
9e60: 76 6f 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68  voked even if th
9e70: 65 20 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64  e xOpen reported
9e80: 20 74 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e   that it failed.
9e90: 20 20 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61    The.** only wa
9ea0: 79 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63  y to prevent a c
9eb0: 61 6c 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f  all to xClose fo
9ec0: 6c 6c 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64  llowing a failed
9ed0: 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72   xOpen.** is for
9ee0: 20 74 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65   the xOpen to se
9ef0: 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  t the sqlite3_fi
9f00: 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d  le.pMethods elem
9f10: 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ent to NULL..**.
9f20: 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67  ** The flags arg
9f30: 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d  ument to xSync m
9f40: 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51  ay be one of [SQ
9f50: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
9f60: 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ] or.** [SQLITE_
9f70: 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65  SYNC_FULL].  The
9f80: 20 66 69 72 73 74 20 63 68 6f 69 63 65 20 69 73   first choice is
9f90: 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e   the normal fsyn
9fa0: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f  c()..** The seco
9fb0: 6e 64 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d  nd choice is a M
9fc0: 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75  ac OS X style fu
9fd0: 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51  llsync.  The [SQ
9fe0: 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e  LITE_SYNC_DATAON
9ff0: 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20  LY].** flag may 
a000: 62 65 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e  be ORed in to in
a010: 64 69 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79  dicate that only
a020: 20 74 68 65 20 64 61 74 61 20 6f 66 20 74 68 65   the data of the
a030: 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74   file.** and not
a040: 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73   its inode needs
a050: 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a   to be synced..*
a060: 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72  *.** The integer
a070: 20 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b   values to xLock
a080: 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29  () and xUnlock()
a090: 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c   are one of.** <
a0a0: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  ul>.** <li> [SQL
a0b0: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a  ITE_LOCK_NONE],.
a0c0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a0d0: 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a  LOCK_SHARED],.**
a0e0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f   <li> [SQLITE_LO
a0f0: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a  CK_RESERVED],.**
a100: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f   <li> [SQLITE_LO
a110: 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a  CK_PENDING], or.
a120: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a130: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e  LOCK_EXCLUSIVE].
a140: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f  .** </ul>.** xLo
a150: 63 6b 28 29 20 69 6e 63 72 65 61 73 65 73 20 74  ck() increases t
a160: 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b  he lock. xUnlock
a170: 28 29 20 64 65 63 72 65 61 73 65 73 20 74 68 65  () decreases the
a180: 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43   lock..** The xC
a190: 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
a1a0: 28 29 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73  () method checks
a1b0: 20 77 68 65 74 68 65 72 20 61 6e 79 20 64 61 74   whether any dat
a1c0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
a1d0: 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74  ,.** either in t
a1e0: 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69  his process or i
a1f0: 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  n some other pro
a200: 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67  cess, is holding
a210: 20 61 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20   a RESERVED,.** 
a220: 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c  PENDING, or EXCL
a230: 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
a240: 65 20 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75  e file.  It retu
a250: 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73  rns true.** if s
a260: 75 63 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74  uch a lock exist
a270: 73 20 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65  s and false othe
a280: 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rwise..**.** The
a290: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20   xFileControl() 
a2a0: 6d 65 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65  method is a gene
a2b0: 72 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 68  ric interface th
a2c0: 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d  at allows custom
a2d0: 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  .** VFS implemen
a2e0: 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63  tations to direc
a2f0: 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f  tly control an o
a300: 70 65 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74  pen file using t
a310: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  he.** [sqlite3_f
a320: 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69  ile_control()] i
a330: 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65 20 73  nterface.  The s
a340: 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d  econd "op" argum
a350: 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74  ent is an.** int
a360: 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68  eger opcode.  Th
a370: 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
a380: 20 69 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f   is a generic po
a390: 69 6e 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74  inter intended t
a3a0: 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20  o.** point to a 
a3b0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d  structure that m
a3c0: 61 79 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d  ay contain argum
a3d0: 65 6e 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e  ents or space in
a3e0: 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69   which to.** wri
a3f0: 74 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73  te return values
a400: 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65  .  Potential use
a410: 73 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72  s for xFileContr
a420: 6f 6c 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a  ol() might be.**
a430: 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e   functions to en
a440: 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f  able blocking lo
a450: 63 6b 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74  cks with timeout
a460: 73 2c 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  s, to change the
a470: 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61  .** locking stra
a480: 74 65 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c  tegy (for exampl
a490: 65 20 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c  e to use dot-fil
a4a0: 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71  e locks), to inq
a4b0: 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68  uire.** about th
a4c0: 65 20 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f  e status of a lo
a4d0: 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20  ck, or to break 
a4e0: 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68  stale locks.  Th
a4f0: 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65  e SQLite.** core
a500: 20 72 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70   reserves all op
a510: 63 6f 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20  codes less than 
a520: 31 30 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20  100 for its own 
a530: 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54  use..** A [SQLIT
a540: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
a550: 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f  E | list of opco
a560: 64 65 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31  des] less than 1
a570: 30 30 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e  00 is available.
a580: 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  .** Applications
a590: 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 20 63   that define a c
a5a0: 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72  ustom xFileContr
a5b0: 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64  ol method should
a5c0: 20 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20   use opcodes.** 
a5d0: 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 30 30  greater than 100
a5e0: 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69   to avoid confli
a5f0: 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  cts..**.** The x
a600: 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74  SectorSize() met
a610: 68 6f 64 20 72 65 74 75 72 6e 73 20 74 68 65 20  hod returns the 
a620: 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74  sector size of t
a630: 68 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61  he.** device tha
a640: 74 20 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20  t underlies the 
a650: 66 69 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f  file.  The secto
a660: 72 20 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a  r size is the.**
a670: 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74   minimum write t
a680: 68 61 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f  hat can be perfo
a690: 72 6d 65 64 20 77 69 74 68 6f 75 74 20 64 69 73  rmed without dis
a6a0: 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72  turbing.** other
a6b0: 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 69   bytes in the fi
a6c0: 6c 65 2e 20 20 54 68 65 20 78 44 65 76 69 63 65  le.  The xDevice
a6d0: 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
a6e0: 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75  ).** method retu
a6f0: 72 6e 73 20 61 20 62 69 74 20 76 65 63 74 6f 72  rns a bit vector
a700: 20 64 65 73 63 72 69 62 69 6e 67 20 62 65 68 61   describing beha
a710: 76 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20  viors of the.** 
a720: 75 6e 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63  underlying devic
a730: 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  e:.**.** <ul>.**
a740: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
a750: 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c  CAP_ATOMIC].** <
a760: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a770: 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20  P_ATOMIC512].** 
a780: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a790: 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20  AP_ATOMIC1K].** 
a7a0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a7b0: 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20  AP_ATOMIC2K].** 
a7c0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a7d0: 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20  AP_ATOMIC4K].** 
a7e0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a7f0: 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20  AP_ATOMIC8K].** 
a800: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a810: 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a  AP_ATOMIC16K].**
a820: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
a830: 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a  CAP_ATOMIC32K].*
a840: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
a850: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a  OCAP_ATOMIC64K].
a860: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a870: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
a880: 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  D].** <li> [SQLI
a890: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
a8a0: 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  IAL].** </ul>.**
a8b0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49  .** The SQLITE_I
a8c0: 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70  OCAP_ATOMIC prop
a8d0: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20  erty means that 
a8e0: 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a  all writes of.**
a8f0: 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74   any size are at
a900: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54  omic.  The SQLIT
a910: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e  E_IOCAP_ATOMICnn
a920: 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e  n values.** mean
a930: 20 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20   that writes of 
a940: 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20  blocks that are 
a950: 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a  nnn bytes in siz
a960: 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69  e and.** are ali
a970: 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65  gned to an addre
a980: 73 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69  ss which is an i
a990: 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20  nteger multiple 
a9a0: 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74  of.** nnn are at
a9b0: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54  omic.  The SQLIT
a9c0: 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50  E_IOCAP_SAFE_APP
a9d0: 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a  END value means.
a9e0: 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74  ** that when dat
a9f0: 61 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f  a is appended to
aa00: 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74   a file, the dat
aa10: 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a  a is appended.**
aa20: 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20   first then the 
aa30: 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
aa40: 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65   is extended, ne
aa50: 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a  ver the other.**
aa60: 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68   way around.  Th
aa70: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  e SQLITE_IOCAP_S
aa80: 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72  EQUENTIAL proper
aa90: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a  ty means that.**
aaa0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
aab0: 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20  written to disk 
aac0: 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65  in the same orde
aad0: 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f  r as calls.** to
aae0: 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a   xWrite()..**.**
aaf0: 20 49 66 20 78 52 65 61 64 28 29 20 72 65 74 75   If xRead() retu
ab00: 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  rns SQLITE_IOERR
ab10: 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d  _SHORT_READ it m
ab20: 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a  ust also fill.**
ab30: 20 69 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70   in the unread p
ab40: 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62  ortions of the b
ab50: 75 66 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73  uffer with zeros
ab60: 2e 20 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a  .  A VFS that.**
ab70: 20 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66   fails to zero-f
ab80: 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20  ill short reads 
ab90: 6d 69 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f  might seem to wo
aba0: 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a  rk.  However,.**
abb0: 20 66 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f   failure to zero
abc0: 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64  -fill short read
abd0: 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c  s will eventuall
abe0: 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74  y lead to.** dat
abf0: 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
ac00: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
ac10: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  uct sqlite3_io_m
ac20: 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69  ethods sqlite3_i
ac30: 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63  o_methods;.struc
ac40: 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
ac50: 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65  hods {.  int iVe
ac60: 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78  rsion;.  int (*x
ac70: 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66  Close)(sqlite3_f
ac80: 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  ile*);.  int (*x
ac90: 52 65 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69  Read)(sqlite3_fi
aca0: 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20  le*, void*, int 
acb0: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e  iAmt, sqlite3_in
acc0: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e  t64 iOfst);.  in
acd0: 74 20 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69  t (*xWrite)(sqli
ace0: 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74  te3_file*, const
acf0: 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74   void*, int iAmt
ad00: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
ad10: 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a  iOfst);.  int (*
ad20: 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74  xTruncate)(sqlit
ad30: 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65  e3_file*, sqlite
ad40: 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20  3_int64 size);. 
ad50: 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71   int (*xSync)(sq
ad60: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
ad70: 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28   flags);.  int (
ad80: 2a 78 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69  *xFileSize)(sqli
ad90: 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74  te3_file*, sqlit
ada0: 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29  e3_int64 *pSize)
adb0: 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29  ;.  int (*xLock)
adc0: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
add0: 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55  int);.  int (*xU
ade0: 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66  nlock)(sqlite3_f
adf0: 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e  ile*, int);.  in
ae00: 74 20 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76  t (*xCheckReserv
ae10: 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f  edLock)(sqlite3_
ae20: 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73  file*, int *pRes
ae30: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  Out);.  int (*xF
ae40: 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69  ileControl)(sqli
ae50: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f  te3_file*, int o
ae60: 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a  p, void *pArg);.
ae70: 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53    int (*xSectorS
ae80: 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ize)(sqlite3_fil
ae90: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65  e*);.  int (*xDe
aea0: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
aeb0: 69 63 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ics)(sqlite3_fil
aec0: 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69  e*);.  /* Additi
aed0: 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79  onal methods may
aee0: 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
aef0: 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a  ure releases */.
af00: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  };../*.** CAPI3R
af10: 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c  EF: Standard Fil
af20: 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65  e Control Opcode
af30: 73 20 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38  s {H11310} <S308
af40: 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
af50: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
af60: 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f  s are opcodes fo
af70: 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72  r the xFileContr
af80: 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20  ol method.** of 
af90: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
afa0: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
afb0: 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c  and for the [sql
afc0: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
afd0: 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63  l()].** interfac
afe0: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51  e..**.** The [SQ
aff0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
b000: 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20  TATE] opcode is 
b010: 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69  used for debuggi
b020: 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63  ng.  This.** opc
b030: 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78  ode causes the x
b040: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
b050: 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  od to write the 
b060: 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
b070: 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e  .** the lock (on
b080: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  e of [SQLITE_LOC
b090: 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45  K_NONE], [SQLITE
b0a0: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a  _LOCK_SHARED],.*
b0b0: 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52  * [SQLITE_LOCK_R
b0c0: 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54  ESERVED], [SQLIT
b0d0: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c  E_LOCK_PENDING],
b0e0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b   or [SQLITE_LOCK
b0f0: 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20  _EXCLUSIVE]).** 
b100: 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  into an integer 
b110: 74 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72  that the pArg ar
b120: 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f  gument points to
b130: 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74  . This capabilit
b140: 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72  y.** is used dur
b150: 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20  ing testing and 
b160: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65  only needs to be
b170: 20 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20   supported when 
b180: 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69  SQLITE_TEST.** i
b190: 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64  s defined..*/.#d
b1a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e  efine SQLITE_FCN
b1b0: 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20  TL_LOCKSTATE    
b1c0: 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
b1d0: 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_GET_LOCKPRO
b1e0: 58 59 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64  XYFILE      2.#d
b1f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54  efine SQLITE_SET
b200: 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20  _LOCKPROXYFILE  
b210: 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51      3.#define SQ
b220: 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20  LITE_LAST_ERRNO 
b230: 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f              4../
b240: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d  *.** CAPI3REF: M
b250: 75 74 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37  utex Handle {H17
b260: 31 31 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a  110} <S20130>.**
b270: 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f  .** The mutex mo
b280: 64 75 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69  dule within SQLi
b290: 74 65 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69  te defines [sqli
b2a0: 74 65 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65  te3_mutex] to be
b2b0: 20 61 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20   an.** abstract 
b2c0: 74 79 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78  type for a mutex
b2d0: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51   object.  The SQ
b2e0: 4c 69 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20  Lite core never 
b2f0: 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20  looks.** at the 
b300: 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65  internal represe
b310: 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73  ntation of an [s
b320: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20  qlite3_mutex].  
b330: 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73  It only.** deals
b340: 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
b350: 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  o the [sqlite3_m
b360: 75 74 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  utex] object..**
b370: 0a 2a 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20  .** Mutexes are 
b380: 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73  created using [s
b390: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
b3a0: 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65  oc()]..*/.typede
b3b0: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
b3c0: 5f 6d 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d  _mutex sqlite3_m
b3d0: 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  utex;../*.** CAP
b3e0: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66  I3REF: OS Interf
b3f0: 61 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31  ace Object {H111
b400: 34 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a  40} <S20100>.**.
b410: 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
b420: 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66  f the sqlite3_vf
b430: 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73  s object defines
b440: 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62   the interface b
b450: 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51  etween.** the SQ
b460: 4c 69 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68  Lite core and th
b470: 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  e underlying ope
b480: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20  rating system.  
b490: 54 68 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20  The "vfs".** in 
b4a0: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
b4b0: 6f 62 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f  object stands fo
b4c0: 72 20 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20  r "virtual file 
b4d0: 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54  system"..**.** T
b4e0: 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
b4f0: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69  iVersion field i
b500: 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75  s initially 1 bu
b510: 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20  t may be larger 
b520: 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72  in.** future ver
b530: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e  sions of SQLite.
b540: 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65    Additional fie
b550: 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e  lds may be appen
b560: 64 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f  ded to this.** o
b570: 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 69  bject when the i
b580: 56 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73  Version value is
b590: 20 69 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74   increased.  Not
b5a0: 65 20 74 68 61 74 20 74 68 65 20 73 74 72 75 63  e that the struc
b5b0: 74 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73  ture.** of the s
b5c0: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
b5d0: 74 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65  t changes in the
b5e0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74   transaction bet
b5f0: 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76  ween.** SQLite v
b600: 65 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64  ersion 3.5.9 and
b610: 20 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74   3.6.0 and yet t
b620: 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c  he iVersion fiel
b630: 64 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64  d was not.** mod
b640: 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ified..**.** The
b650: 20 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20   szOsFile field 
b660: 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
b670: 68 65 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73  he subclassed [s
b680: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20  qlite3_file].** 
b690: 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 62  structure used b
b6a0: 79 20 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50  y this VFS.  mxP
b6b0: 61 74 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d  athname is the m
b6c0: 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
b6d0: 0a 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69  .** a pathname i
b6e0: 6e 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a  n this VFS..**.*
b6f0: 2a 20 52 65 67 69 73 74 65 72 65 64 20 73 71 6c  * Registered sql
b700: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73  ite3_vfs objects
b710: 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c   are kept on a l
b720: 69 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65  inked list forme
b730: 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78  d by.** the pNex
b740: 74 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20  t pointer.  The 
b750: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67  [sqlite3_vfs_reg
b760: 69 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20  ister()].** and 
b770: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72  [sqlite3_vfs_unr
b780: 65 67 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72  egister()] inter
b790: 66 61 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69  faces manage thi
b7a0: 73 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74  s list.** in a t
b7b0: 68 72 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20  hread-safe way. 
b7c0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   The [sqlite3_vf
b7d0: 73 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66  s_find()] interf
b7e0: 61 63 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20  ace.** searches 
b7f0: 74 68 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68  the list.  Neith
b800: 65 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  er the applicati
b810: 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20  on code nor the 
b820: 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  VFS.** implement
b830: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65  ation should use
b840: 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74   the pNext point
b850: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e  er..**.** The pN
b860: 65 78 74 20 66 69 65 6c 64 20 69 73 20 74 68 65  ext field is the
b870: 20 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74   only field in t
b880: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a  he sqlite3_vfs.*
b890: 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  * structure that
b8a0: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65   SQLite will eve
b8b0: 72 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74  r modify.  SQLit
b8c0: 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65  e will only acce
b8d0: 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20  ss.** or modify 
b8e0: 74 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65  this field while
b8f0: 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69   holding a parti
b900: 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74  cular static mut
b910: 65 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  ex..** The appli
b920: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65  cation should ne
b930: 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68  ver modify anyth
b940: 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 73  ing within the s
b950: 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62  qlite3_vfs.** ob
b960: 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62  ject once the ob
b970: 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20 72 65  ject has been re
b980: 67 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  gistered..**.** 
b990: 54 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20  The zName field 
b9a0: 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f  holds the name o
b9b0: 66 20 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65  f the VFS module
b9c0: 2e 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74  .  The name must
b9d0: 0a 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63  .** be unique ac
b9e0: 72 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64  ross all VFS mod
b9f0: 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  ules..**.** SQLi
ba00: 74 65 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65  te will guarante
ba10: 65 20 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65  e that the zFile
ba20: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74  name parameter t
ba30: 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69  o xOpen.** is ei
ba40: 74 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  ther a NULL poin
ba50: 74 65 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62  ter or string ob
ba60: 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78  tained.** from x
ba70: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20  FullPathname(). 
ba80: 20 53 51 4c 69 74 65 20 66 75 72 74 68 65 72 20   SQLite further 
ba90: 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a  guarantees that.
baa0: 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 77 69  ** the string wi
bab0: 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20  ll be valid and 
bac0: 75 6e 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20  unchanged until 
bad0: 78 43 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63  xClose() is.** c
bae0: 61 6c 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f  alled. Because o
baf0: 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73  f the previous s
bb00: 65 6e 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20  entence,.** the 
bb10: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63  [sqlite3_file] c
bb20: 61 6e 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20  an safely store 
bb30: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
bb40: 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20  .** filename if 
bb50: 69 74 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65  it needs to reme
bb60: 6d 62 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d  mber the filenam
bb70: 65 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f  e for some reaso
bb80: 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69  n..** If the zFi
bb90: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72  lename parameter
bba0: 20 69 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e   is xOpen is a N
bbb0: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e  ULL pointer then
bbc0: 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69   xOpen.** must i
bbd0: 6e 76 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65  nvent its own te
bbe0: 6d 70 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72  mporary name for
bbf0: 20 74 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e   the file.  When
bc00: 65 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69  ever the .** xFi
bc10: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72  lename parameter
bc20: 20 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c   is NULL it will
bc30: 20 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73   also be the cas
bc40: 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c  e that the.** fl
bc50: 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69  ags parameter wi
bc60: 6c 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49  ll include [SQLI
bc70: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
bc80: 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  CLOSE]..**.** Th
bc90: 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  e flags argument
bca0: 20 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c   to xOpen() incl
bcb0: 75 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65  udes all bits se
bcc0: 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67  t in.** the flag
bcd0: 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73  s argument to [s
bce0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
bcf0: 5d 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74  ].  Or if [sqlit
bd00: 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72  e3_open()].** or
bd10: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
bd20: 28 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65  ()] is used, the
bd30: 6e 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73  n flags includes
bd40: 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51   at least.** [SQ
bd50: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
bd60: 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f  ITE] | [SQLITE_O
bd70: 50 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a  PEN_CREATE]. .**
bd80: 20 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e   If xOpen() open
bd90: 73 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e  s a file read-on
bda0: 6c 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20  ly then it sets 
bdb0: 2a 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a  *pOutFlags to.**
bdc0: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45   include [SQLITE
bdd0: 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e  _OPEN_READONLY].
bde0: 20 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20    Other bits in 
bdf0: 2a 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62  *pOutFlags may b
be00: 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  e set..**.** SQL
be10: 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64  ite will also ad
be20: 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  d one of the fol
be30: 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20  lowing flags to 
be40: 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63  the xOpen().** c
be50: 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f  all, depending o
be60: 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69  n the object bei
be70: 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a  ng opened:.**.**
be80: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b   <ul>.** <li>  [
be90: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
bea0: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  _DB].** <li>  [S
beb0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
bec0: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e  JOURNAL].** <li>
bed0: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54    [SQLITE_OPEN_T
bee0: 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20  EMP_DB].** <li> 
bef0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45   [SQLITE_OPEN_TE
bf00: 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c  MP_JOURNAL].** <
bf10: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45  li>  [SQLITE_OPE
bf20: 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a  N_TRANSIENT_DB].
bf30: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45  ** <li>  [SQLITE
bf40: 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c  _OPEN_SUBJOURNAL
bf50: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49  ].** <li>  [SQLI
bf60: 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a  TE_OPEN_MASTER_J
bf70: 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e  OURNAL].** </ul>
bf80: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  .**.** The file 
bf90: 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  I/O implementati
bfa0: 6f 6e 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f  on can use the o
bfb0: 62 6a 65 63 74 20 74 79 70 65 20 66 6c 61 67 73  bject type flags
bfc0: 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68   to.** change th
bfd0: 65 20 77 61 79 20 69 74 20 64 65 61 6c 73 20 77  e way it deals w
bfe0: 69 74 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20  ith files.  For 
bff0: 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c  example, an appl
c000: 69 63 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20  ication.** that 
c010: 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62  does not care ab
c020: 6f 75 74 20 63 72 61 73 68 20 72 65 63 6f 76 65  out crash recove
c030: 72 79 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d  ry or rollback m
c040: 69 67 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65  ight make.** the
c050: 20 6f 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e   open of a journ
c060: 61 6c 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e  al file a no-op.
c070: 20 20 57 72 69 74 65 73 20 74 6f 20 74 68 69 73    Writes to this
c080: 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a   journal would.*
c090: 2a 20 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73  * also be no-ops
c0a0: 2c 20 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70  , and any attemp
c0b0: 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f  t to read the jo
c0c0: 75 72 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75  urnal would retu
c0d0: 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45  rn.** SQLITE_IOE
c0e0: 52 52 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c  RR.  Or the impl
c0f0: 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74  ementation might
c100: 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20   recognize that 
c110: 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  a database.** fi
c120: 6c 65 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67  le will be doing
c130: 20 70 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65   page-aligned se
c140: 63 74 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77  ctor reads and w
c150: 72 69 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f  rites in a rando
c160: 6d 0a 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73  m.** order and s
c170: 65 74 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75  et up its I/O su
c180: 62 73 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e  bsystem accordin
c190: 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  gly..**.** SQLit
c1a0: 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64  e might also add
c1b0: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
c1c0: 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74  owing flags to t
c1d0: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a  he xOpen method:
c1e0: 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
c1f0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  li> [SQLITE_OPEN
c200: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a  _DELETEONCLOSE].
c210: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c220: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a  OPEN_EXCLUSIVE].
c230: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
c240: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  he [SQLITE_OPEN_
c250: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66  DELETEONCLOSE] f
c260: 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69  lag means the fi
c270: 6c 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  le should be.** 
c280: 64 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20  deleted when it 
c290: 69 73 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20  is closed.  The 
c2a0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c  [SQLITE_OPEN_DEL
c2b0: 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77  ETEONCLOSE].** w
c2c0: 69 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54  ill be set for T
c2d0: 45 4d 50 20 20 64 61 74 61 62 61 73 65 73 2c 20  EMP  databases, 
c2e0: 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72  journals and for
c2f0: 20 73 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a   subjournals..**
c300: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
c310: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20  OPEN_EXCLUSIVE] 
c320: 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 75  flag is always u
c330: 73 65 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69  sed in conjuncti
c340: 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b  on.** with the [
c350: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
c360: 54 45 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20  TE] flag, which 
c370: 61 72 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c  are both directl
c380: 79 0a 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74  y.** analogous t
c390: 6f 20 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64  o the O_EXCL and
c3a0: 20 4f 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f   O_CREAT flags o
c3b0: 66 20 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e  f the POSIX open
c3c0: 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20  ().** API.  The 
c3d0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
c3e0: 55 53 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e  USIVE flag, when
c3f0: 20 70 61 69 72 65 64 20 77 69 74 68 20 74 68 65   paired with the
c400: 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e   .** SQLITE_OPEN
c410: 5f 43 52 45 41 54 45 2c 20 69 73 20 75 73 65 64  _CREATE, is used
c420: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61   to indicate tha
c430: 74 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c  t file should al
c440: 77 61 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74  ways.** be creat
c450: 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20  ed, and that it 
c460: 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69  is an error if i
c470: 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
c480: 2e 0a 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f  ..** It is <i>no
c490: 74 3c 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e  t</i> used to in
c4a0: 64 69 63 61 74 65 20 74 68 65 20 66 69 6c 65 20  dicate the file 
c4b0: 73 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64  should be opened
c4c0: 20 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69   .** for exclusi
c4d0: 76 65 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  ve access..**.**
c4e0: 20 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69   At least szOsFi
c4f0: 6c 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  le bytes of memo
c500: 72 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  ry are allocated
c510: 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f   by SQLite.** to
c520: 20 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69   hold the  [sqli
c530: 74 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74  te3_file] struct
c540: 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68  ure passed as th
c550: 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d  e third.** argum
c560: 65 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54  ent to xOpen.  T
c570: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20  he xOpen method 
c580: 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f  does not have to
c590: 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65  .** allocate the
c5a0: 20 73 74 72 75 63 74 75 72 65 3b 20 69 74 20 73   structure; it s
c5b0: 68 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20  hould just fill 
c5c0: 69 74 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61  it in.  Note tha
c5d0: 74 0a 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d  t.** the xOpen m
c5e0: 65 74 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74  ethod must set t
c5f0: 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e  he sqlite3_file.
c600: 70 4d 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68  pMethods to eith
c610: 65 72 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73  er.** a valid [s
c620: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
c630: 73 5d 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20  s] object or to 
c640: 4e 55 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73  NULL.  xOpen mus
c650: 74 20 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65  t do.** this eve
c660: 6e 20 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61  n if the open fa
c670: 69 6c 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70  ils.  SQLite exp
c680: 65 63 74 73 20 74 68 61 74 20 74 68 65 20 73 71  ects that the sq
c690: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68  lite3_file.pMeth
c6a0: 6f 64 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77  ods.** element w
c6b0: 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74  ill be valid aft
c6c0: 65 72 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73  er xOpen returns
c6d0: 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74   regardless of t
c6e0: 68 65 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  he success.** or
c6f0: 20 66 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20   failure of the 
c700: 78 4f 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a  xOpen call..**.*
c710: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75  * The flags argu
c720: 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28  ment to xAccess(
c730: 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45  ) may be [SQLITE
c740: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a  _ACCESS_EXISTS].
c750: 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74  ** to test for t
c760: 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20  he existence of 
c770: 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49  a file, or [SQLI
c780: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
c790: 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20  ITE] to.** test 
c7a0: 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 69  whether a file i
c7b0: 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77  s readable and w
c7c0: 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c  ritable, or [SQL
c7d0: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d  ITE_ACCESS_READ]
c7e0: 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74  .** to test whet
c7f0: 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 61 74  her a file is at
c800: 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e   least readable.
c810: 20 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20     The file can 
c820: 62 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72  be a.** director
c830: 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  y..**.** SQLite 
c840: 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f  will always allo
c850: 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78  cate at least mx
c860: 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73  Pathname+1 bytes
c870: 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70   for the.** outp
c880: 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50  ut buffer xFullP
c890: 61 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78  athname.  The ex
c8a0: 61 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20  act size of the 
c8b0: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a  output buffer.**
c8c0: 20 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20   is also passed 
c8d0: 61 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 74  as a parameter t
c8e0: 6f 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e  o both  methods.
c8f0: 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62   If the output b
c900: 75 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20  uffer.** is not 
c910: 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53  large enough, [S
c920: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20  QLITE_CANTOPEN] 
c930: 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e  should be return
c940: 65 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69  ed. Since this i
c950: 73 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20  s.** handled as 
c960: 61 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79  a fatal error by
c970: 20 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70   SQLite, vfs imp
c980: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f  lementations sho
c990: 75 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20  uld endeavor.** 
c9a0: 74 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20  to prevent this 
c9b0: 62 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74  by setting mxPat
c9c0: 68 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69  hname to a suffi
c9d0: 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61  ciently large va
c9e0: 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  lue..**.** The x
c9f0: 52 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53  Randomness(), xS
ca00: 6c 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72  leep(), and xCur
ca10: 72 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72  rentTime() inter
ca20: 66 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74  faces.** are not
ca30: 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74   strictly a part
ca40: 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74   of the filesyst
ca50: 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65  em, but they are
ca60: 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  .** included in 
ca70: 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72  the VFS structur
ca80: 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65  e for completene
ca90: 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64  ss..** The xRand
caa0: 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f  omness() functio
cab0: 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65  n attempts to re
cac0: 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65  turn nBytes byte
cad0: 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61  s.** of good-qua
cae0: 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20  lity randomness 
caf0: 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20  into zOut.  The 
cb00: 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a  return value is.
cb10: 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75  ** the actual nu
cb20: 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
cb30: 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61   randomness obta
cb40: 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c  ined..** The xSl
cb50: 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75  eep() method cau
cb60: 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ses the calling 
cb70: 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20  thread to sleep 
cb80: 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20  for at.** least 
cb90: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
cba0: 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e  croseconds given
cbb0: 2e 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54  .  The xCurrentT
cbc0: 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  ime().** method 
cbd0: 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e  returns a Julian
cbe0: 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20   Day Number for 
cbf0: 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65  the current date
cc00: 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f   and time..**.*/
cc10: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
cc20: 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69  sqlite3_vfs sqli
cc30: 74 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20  te3_vfs;.struct 
cc40: 73 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20  sqlite3_vfs {.  
cc50: 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20  int iVersion;   
cc60: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75           /* Stru
cc70: 63 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75  cture version nu
cc80: 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  mber */.  int sz
cc90: 4f 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20  OsFile;         
cca0: 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75     /* Size of su
ccb0: 62 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33  bclassed sqlite3
ccc0: 5f 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d  _file */.  int m
ccd0: 78 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20  xPathname;      
cce0: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66      /* Maximum f
ccf0: 69 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e  ile pathname len
cd00: 67 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  gth */.  sqlite3
cd10: 5f 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20  _vfs *pNext;    
cd20: 20 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74    /* Next regist
cd30: 65 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f  ered VFS */.  co
cd40: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
cd50: 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
cd60: 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 66  f this virtual f
cd70: 69 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20  ile system */.  
cd80: 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20  void *pAppData; 
cd90: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
cda0: 74 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69  ter to applicati
cdb0: 6f 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74 61  on-specific data
cdc0: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65   */.  int (*xOpe
cdd0: 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  n)(sqlite3_vfs*,
cde0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
cdf0: 6d 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  me, sqlite3_file
ce00: 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  *,.             
ce10: 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74    int flags, int
ce20: 20 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20   *pOutFlags);.  
ce30: 69 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73  int (*xDelete)(s
ce40: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
ce50: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
ce60: 69 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20  int syncDir);.  
ce70: 69 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28 73  int (*xAccess)(s
ce80: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
ce90: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
cea0: 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a  int flags, int *
ceb0: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20  pResOut);.  int 
cec0: 28 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  (*xFullPathname)
ced0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63  (sqlite3_vfs*, c
cee0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
cef0: 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72  , int nOut, char
cf00: 20 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20   *zOut);.  void 
cf10: 2a 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69  *(*xDlOpen)(sqli
cf20: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20  te3_vfs*, const 
cf30: 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
cf40: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72  ;.  void (*xDlEr
cf50: 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ror)(sqlite3_vfs
cf60: 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68  *, int nByte, ch
cf70: 61 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20  ar *zErrMsg);.  
cf80: 76 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29  void (*(*xDlSym)
cf90: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f  (sqlite3_vfs*,vo
cfa0: 69 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  id*, const char 
cfb0: 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29  *zSymbol))(void)
cfc0: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c  ;.  void (*xDlCl
cfd0: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ose)(sqlite3_vfs
cfe0: 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74  *, void*);.  int
cff0: 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28   (*xRandomness)(
d000: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e  sqlite3_vfs*, in
d010: 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a  t nByte, char *z
d020: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53  Out);.  int (*xS
d030: 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66  leep)(sqlite3_vf
d040: 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  s*, int microsec
d050: 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78  onds);.  int (*x
d060: 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c  CurrentTime)(sql
d070: 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c  ite3_vfs*, doubl
d080: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65  e*);.  int (*xGe
d090: 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69  tLastError)(sqli
d0a0: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63  te3_vfs*, int, c
d0b0: 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77  har *);.  /* New
d0c0: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61   fields may be a
d0d0: 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72  ppended in figur
d0e0: 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65  e versions.  The
d0f0: 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76   iVersion.  ** v
d100: 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d  alue will increm
d110: 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69  ent whenever thi
d120: 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b  s happens. */.};
d130: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
d140: 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20  : Flags for the 
d150: 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68  xAccess VFS meth
d160: 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31  od {H11190} <H11
d170: 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  140>.**.** These
d180: 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
d190: 74 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 61  ts can be used a
d1a0: 73 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61  s the third para
d1b0: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20  meter to.** the 
d1c0: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f  xAccess method o
d1d0: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66  f an [sqlite3_vf
d1e0: 73 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d  s] object. {END}
d1f0: 20 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65    They determine
d200: 0a 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66  .** what kind of
d210: 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65   permissions the
d220: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20   xAccess method 
d230: 69 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a  is looking for..
d240: 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41  ** With SQLITE_A
d250: 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68  CCESS_EXISTS, th
d260: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64  e xAccess method
d270: 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b  .** simply check
d280: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69  s whether the fi
d290: 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69  le exists..** Wi
d2a0: 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  th SQLITE_ACCESS
d2b0: 5f 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20  _READWRITE, the 
d2c0: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a  xAccess method.*
d2d0: 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72  * checks whether
d2e0: 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74   the file is bot
d2f0: 68 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77  h readable and w
d300: 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68  ritable..** With
d310: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
d320: 45 41 44 2c 20 74 68 65 20 78 41 63 63 65 73 73  EAD, the xAccess
d330: 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b   method.** check
d340: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69  s whether the fi
d350: 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a  le is readable..
d360: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
d370: 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20  E_ACCESS_EXISTS 
d380: 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
d390: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
d3a0: 52 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53  RITE 1.#define S
d3b0: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
d3c0: 44 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20  D      2../*.** 
d3d0: 43 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61  CAPI3REF: Initia
d3e0: 6c 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20  lize The SQLite 
d3f0: 4c 69 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d  Library {H10130}
d400: 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30   <S20000><S30100
d410: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
d420: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
d430: 20 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c   routine initial
d440: 69 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69  izes the.** SQLi
d450: 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65  te library.  The
d460: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
d470: 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64  n() routine.** d
d480: 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72  eallocates any r
d490: 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65  esources that we
d4a0: 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  re allocated by 
d4b0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
d4c0: 7a 65 28 29 2e 0a 2a 2a 20 54 68 65 73 65 20 72  ze()..** These r
d4d0: 6f 75 74 69 6e 65 73 20 61 72 65 20 64 65 73 69  outines are desi
d4e0: 67 6e 65 64 20 74 6f 20 61 69 64 20 69 6e 20 70  gned to aid in p
d4f0: 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a  rocess initializ
d500: 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 68 75  ation and.** shu
d510: 74 64 6f 77 6e 20 6f 6e 20 65 6d 62 65 64 64 65  tdown on embedde
d520: 64 20 73 79 73 74 65 6d 73 2e 20 20 57 6f 72 6b  d systems.  Work
d530: 73 74 61 74 69 6f 6e 20 61 70 70 6c 69 63 61 74  station applicat
d540: 69 6f 6e 73 20 75 73 69 6e 67 0a 2a 2a 20 53 51  ions using.** SQ
d550: 4c 69 74 65 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f  Lite normally do
d560: 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 69 6e 76   not need to inv
d570: 6f 6b 65 20 65 69 74 68 65 72 20 6f 66 20 74 68  oke either of th
d580: 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ese routines..**
d590: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71  .** A call to sq
d5a0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
d5b0: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74  () is an "effect
d5c0: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20  ive" call if it 
d5d0: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  is.** the first 
d5e0: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  time sqlite3_ini
d5f0: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
d600: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20  oked during the 
d610: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74  lifetime of.** t
d620: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69  he process, or i
d630: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73  f it is the firs
d640: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69  t time sqlite3_i
d650: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
d660: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77  nvoked.** follow
d670: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
d680: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
d690: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63  .  Only an effec
d6a0: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20  tive call.** of 
d6b0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
d6c0: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e  ze() does any in
d6d0: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41  itialization.  A
d6e0: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a  ll other calls.*
d6f0: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  * are harmless n
d700: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63  o-ops..**.** A c
d710: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  all to sqlite3_s
d720: 68 75 74 64 6f 77 6e 28 29 20 69 73 20 61 6e 20  hutdown() is an 
d730: 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c  "effective" call
d740: 20 69 66 20 69 74 20 69 73 20 74 68 65 20 66 69   if it is the fi
d750: 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73  rst.** call to s
d760: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d770: 29 20 73 69 6e 63 65 20 74 68 65 20 6c 61 73 74  ) since the last
d780: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d790: 69 7a 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20  ize().  Only.** 
d7a0: 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 6c  an effective cal
d7b0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  l to sqlite3_shu
d7c0: 74 64 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e 79  tdown() does any
d7d0: 20 64 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f   deinitializatio
d7e0: 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20  n..** All other 
d7f0: 76 61 6c 69 64 20 63 61 6c 6c 73 20 74 6f 20 73  valid calls to s
d800: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d810: 29 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  ) are harmless n
d820: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  o-ops..**.** The
d830: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d840: 69 7a 65 28 29 20 69 6e 74 65 72 66 61 63 65 20  ize() interface 
d850: 69 73 20 74 68 72 65 61 64 73 61 66 65 2c 20 62  is threadsafe, b
d860: 75 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  ut sqlite3_shutd
d870: 6f 77 6e 28 29 0a 2a 2a 20 69 73 20 6e 6f 74 2e  own().** is not.
d880: 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 68    The sqlite3_sh
d890: 75 74 64 6f 77 6e 28 29 20 69 6e 74 65 72 66 61  utdown() interfa
d8a0: 63 65 20 6d 75 73 74 20 6f 6e 6c 79 20 62 65 20  ce must only be 
d8b0: 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 0a 2a 2a  called from a.**
d8c0: 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20   single thread. 
d8d0: 20 41 6c 6c 20 6f 70 65 6e 20 5b 64 61 74 61 62   All open [datab
d8e0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d  ase connections]
d8f0: 20 6d 75 73 74 20 62 65 20 63 6c 6f 73 65 64 20   must be closed 
d900: 61 6e 64 20 61 6c 6c 0a 2a 2a 20 6f 74 68 65 72  and all.** other
d910: 20 53 51 4c 69 74 65 20 72 65 73 6f 75 72 63 65   SQLite resource
d920: 73 20 6d 75 73 74 20 62 65 20 64 65 61 6c 6c 6f  s must be deallo
d930: 63 61 74 65 64 20 70 72 69 6f 72 20 74 6f 20 69  cated prior to i
d940: 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  nvoking.** sqlit
d950: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 0a 2a  e3_shutdown()..*
d960: 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68 65 72  *.** Among other
d970: 20 74 68 69 6e 67 73 2c 20 73 71 6c 69 74 65 33   things, sqlite3
d980: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 77 69  _initialize() wi
d990: 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c  ll invoke.** sql
d9a0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20  ite3_os_init(). 
d9b0: 20 53 69 6d 69 6c 61 72 6c 79 2c 20 73 71 6c 69   Similarly, sqli
d9c0: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a  te3_shutdown().*
d9d0: 2a 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 73 71  * will invoke sq
d9e0: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a  lite3_os_end()..
d9f0: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
da00: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72  3_initialize() r
da10: 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b  outine returns [
da20: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75  SQLITE_OK] on su
da30: 63 63 65 73 73 2e 0a 2a 2a 20 49 66 20 66 6f 72  ccess..** If for
da40: 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2c 20 73 71   some reason, sq
da50: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
da60: 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  () is unable to 
da70: 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68  initialize.** th
da80: 65 20 6c 69 62 72 61 72 79 20 28 70 65 72 68 61  e library (perha
da90: 70 73 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20  ps it is unable 
daa0: 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  to allocate a ne
dab0: 65 64 65 64 20 72 65 73 6f 75 72 63 65 20 73 75  eded resource su
dac0: 63 68 0a 2a 2a 20 61 73 20 61 20 6d 75 74 65 78  ch.** as a mutex
dad0: 29 20 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20  ) it returns an 
dae0: 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 74 68  [error code] oth
daf0: 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f  er than [SQLITE_
db00: 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  OK]..**.** The s
db10: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
db20: 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63  e() routine is c
db30: 61 6c 6c 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79  alled internally
db40: 20 62 79 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a   by many other.*
db50: 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  * SQLite interfa
db60: 63 65 73 20 73 6f 20 74 68 61 74 20 61 6e 20 61  ces so that an a
db70: 70 70 6c 69 63 61 74 69 6f 6e 20 75 73 75 61 6c  pplication usual
db80: 6c 79 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  ly does not need
db90: 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71   to.** invoke sq
dba0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
dbb0: 28 29 20 64 69 72 65 63 74 6c 79 2e 20 20 46 6f  () directly.  Fo
dbc0: 72 20 65 78 61 6d 70 6c 65 2c 20 5b 73 71 6c 69  r example, [sqli
dbd0: 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63  te3_open()].** c
dbe0: 61 6c 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e 69  alls sqlite3_ini
dbf0: 74 69 61 6c 69 7a 65 28 29 20 73 6f 20 74 68 65  tialize() so the
dc00: 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
dc10: 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
dc20: 63 61 6c 6c 79 0a 2a 2a 20 69 6e 69 74 69 61 6c  cally.** initial
dc30: 69 7a 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74  ized when [sqlit
dc40: 65 33 5f 6f 70 65 6e 28 29 5d 20 69 73 20 63 61  e3_open()] is ca
dc50: 6c 6c 65 64 20 69 66 20 69 74 20 68 61 73 20 6e  lled if it has n
dc60: 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ot be initialize
dc70: 64 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 20 48  d.** already.  H
dc80: 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74  owever, if SQLit
dc90: 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
dca0: 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f  th the [SQLITE_O
dcb0: 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a  MIT_AUTOINIT].**
dcc0: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70   compile-time op
dcd0: 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 61  tion, then the a
dce0: 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c 73 20 74  utomatic calls t
dcf0: 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  o sqlite3_initia
dd00: 6c 69 7a 65 28 29 0a 2a 2a 20 61 72 65 20 6f 6d  lize().** are om
dd10: 69 74 74 65 64 20 61 6e 64 20 74 68 65 20 61 70  itted and the ap
dd20: 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 63  plication must c
dd30: 61 6c 6c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  all sqlite3_init
dd40: 69 61 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c  ialize() directl
dd50: 79 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 75 73  y.** prior to us
dd60: 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51  ing any other SQ
dd70: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20  Lite interface. 
dd80: 20 46 6f 72 20 6d 61 78 69 6d 75 6d 20 70 6f 72   For maximum por
dd90: 74 61 62 69 6c 69 74 79 2c 0a 2a 2a 20 69 74 20  tability,.** it 
dda0: 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74  is recommended t
ddb0: 68 61 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  hat applications
ddc0: 20 61 6c 77 61 79 73 20 69 6e 76 6f 6b 65 20 73   always invoke s
ddd0: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
dde0: 65 28 29 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20  e().** directly 
ddf0: 70 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61  prior to using a
de00: 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20  ny other SQLite 
de10: 69 6e 74 65 72 66 61 63 65 2e 20 20 46 75 74 75  interface.  Futu
de20: 72 65 20 72 65 6c 65 61 73 65 73 0a 2a 2a 20 6f  re releases.** o
de30: 66 20 53 51 4c 69 74 65 20 6d 61 79 20 72 65 71  f SQLite may req
de40: 75 69 72 65 20 74 68 69 73 2e 20 20 49 6e 20 6f  uire this.  In o
de50: 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
de60: 62 65 68 61 76 69 6f 72 20 65 78 68 69 62 69 74  behavior exhibit
de70: 65 64 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74  ed.** when SQLit
de80: 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
de90: 74 68 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  th [SQLITE_OMIT_
dea0: 41 55 54 4f 49 4e 49 54 5d 20 6d 69 67 68 74 20  AUTOINIT] might 
deb0: 62 65 63 6f 6d 65 20 74 68 65 0a 2a 2a 20 64 65  become the.** de
dec0: 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69  fault behavior i
ded0: 6e 20 73 6f 6d 65 20 66 75 74 75 72 65 20 72 65  n some future re
dee0: 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e  lease of SQLite.
def0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
df00: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 72 6f 75  e3_os_init() rou
df10: 74 69 6e 65 20 64 6f 65 73 20 6f 70 65 72 61 74  tine does operat
df20: 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69  ing-system speci
df30: 66 69 63 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  fic.** initializ
df40: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c  ation of the SQL
df50: 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68  ite library.  Th
df60: 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  e sqlite3_os_end
df70: 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 75 6e  ().** routine un
df80: 64 6f 65 73 20 74 68 65 20 65 66 66 65 63 74 20  does the effect 
df90: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  of sqlite3_os_in
dfa0: 69 74 28 29 2e 20 20 54 79 70 69 63 61 6c 20 74  it().  Typical t
dfb0: 61 73 6b 73 0a 2a 2a 20 70 65 72 66 6f 72 6d 65  asks.** performe
dfc0: 64 20 62 79 20 74 68 65 73 65 20 72 6f 75 74 69  d by these routi
dfd0: 6e 65 73 20 69 6e 63 6c 75 64 65 20 61 6c 6c 6f  nes include allo
dfe0: 63 61 74 69 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f  cation or deallo
dff0: 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61  cation.** of sta
e000: 74 69 63 20 72 65 73 6f 75 72 63 65 73 2c 20 69  tic resources, i
e010: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
e020: 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
e030: 73 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 75 70  s,.** setting up
e040: 20 61 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69   a default [sqli
e050: 74 65 33 5f 76 66 73 5d 20 6d 6f 64 75 6c 65 2c  te3_vfs] module,
e060: 20 6f 72 20 73 65 74 74 69 6e 67 20 75 70 0a 2a   or setting up.*
e070: 2a 20 61 20 64 65 66 61 75 6c 74 20 63 6f 6e 66  * a default conf
e080: 69 67 75 72 61 74 69 6f 6e 20 75 73 69 6e 67 20  iguration using 
e090: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
e0a0: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70  )]..**.** The ap
e0b0: 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64  plication should
e0c0: 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 65 69   never invoke ei
e0d0: 74 68 65 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f  ther sqlite3_os_
e0e0: 69 6e 69 74 28 29 0a 2a 2a 20 6f 72 20 73 71 6c  init().** or sql
e0f0: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 64 69  ite3_os_end() di
e100: 72 65 63 74 6c 79 2e 20 20 54 68 65 20 61 70 70  rectly.  The app
e110: 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20  lication should 
e120: 6f 6e 6c 79 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73  only invoke.** s
e130: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
e140: 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  e() and sqlite3_
e150: 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 54 68 65  shutdown().  The
e160: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
e170: 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ().** interface 
e180: 69 73 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61  is called automa
e190: 74 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74  tically by sqlit
e1a0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
e1b0: 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f  and.** sqlite3_o
e1c0: 73 5f 65 6e 64 28 29 20 69 73 20 63 61 6c 6c 65  s_end() is calle
e1d0: 64 20 62 79 20 73 71 6c 69 74 65 33 5f 73 68 75  d by sqlite3_shu
e1e0: 74 64 6f 77 6e 28 29 2e 20 20 41 70 70 72 6f 70  tdown().  Approp
e1f0: 72 69 61 74 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65  riate.** impleme
e200: 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 73 71 6c  ntations for sql
e210: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61  ite3_os_init() a
e220: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nd sqlite3_os_en
e230: 64 28 29 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74  d().** are built
e240: 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 77 68 65   into SQLite whe
e250: 6e 20 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64  n it is compiled
e260: 20 66 6f 72 20 55 6e 69 78 2c 20 57 69 6e 64 6f   for Unix, Windo
e270: 77 73 2c 20 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20  ws, or OS/2..** 
e280: 57 68 65 6e 20 5b 63 75 73 74 6f 6d 20 62 75 69  When [custom bui
e290: 6c 64 73 20 7c 20 62 75 69 6c 74 20 66 6f 72 20  lds | built for 
e2a0: 6f 74 68 65 72 20 70 6c 61 74 66 6f 72 6d 73 5d  other platforms]
e2b0: 0a 2a 2a 20 28 75 73 69 6e 67 20 74 68 65 20 5b  .** (using the [
e2c0: 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d  SQLITE_OS_OTHER=
e2d0: 31 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a  1] compile-time.
e2e0: 2a 2a 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61  ** option) the a
e2f0: 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20  pplication must 
e300: 73 75 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c  supply a suitabl
e310: 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
e320: 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   for.** sqlite3_
e330: 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71  os_init() and sq
e340: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20  lite3_os_end(). 
e350: 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   An application-
e360: 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c  supplied.** impl
e370: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
e380: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20  lite3_os_init() 
e390: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  or sqlite3_os_en
e3a0: 64 28 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75  d().** must retu
e3b0: 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f  rn [SQLITE_OK] o
e3c0: 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f  n success and so
e3d0: 6d 65 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20  me other [error 
e3e0: 63 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61  code] upon.** fa
e3f0: 69 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ilure..*/.SQLITE
e400: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
e410: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64  _initialize(void
e420: 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
e430: 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  t sqlite3_shutdo
e440: 77 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  wn(void);.SQLITE
e450: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
e460: 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a  _os_init(void);.
e470: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
e480: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f  qlite3_os_end(vo
e490: 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  id);../*.** CAPI
e4a0: 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e  3REF: Configurin
e4b0: 67 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62  g The SQLite Lib
e4c0: 72 61 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53  rary {H14100} <S
e4d0: 32 30 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a  20000><S30200>.*
e4e0: 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
e4f0: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
e500: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66  _config() interf
e510: 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d  ace is used to m
e520: 61 6b 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69  ake global confi
e530: 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e  guration.** chan
e540: 67 65 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e  ges to SQLite in
e550: 20 6f 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53   order to tune S
e560: 51 4c 69 74 65 20 74 6f 20 74 68 65 20 73 70 65  QLite to the spe
e570: 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a  cific needs of.*
e580: 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
e590: 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  n.  The default 
e5a0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73  configuration is
e5b0: 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72   recommended for
e5c0: 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61   most.** applica
e5d0: 74 69 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69  tions and so thi
e5e0: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75  s routine is usu
e5f0: 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61  ally not necessa
e600: 72 79 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72  ry.  It is.** pr
e610: 6f 76 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72  ovided to suppor
e620: 74 20 72 61 72 65 20 61 70 70 6c 69 63 61 74 69  t rare applicati
e630: 6f 6e 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c  ons with unusual
e640: 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   needs..**.** Th
e650: 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  e sqlite3_config
e660: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
e670: 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20  not threadsafe. 
e680: 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
e690: 0a 2a 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20  .** must insure 
e6a0: 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51  that no other SQ
e6b0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20  Lite interfaces 
e6c0: 61 72 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f  are invoked by o
e6d0: 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20  ther.** threads 
e6e0: 77 68 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f  while sqlite3_co
e6f0: 6e 66 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e  nfig() is runnin
e700: 67 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c  g.  Furthermore,
e710: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
e720: 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65  ).** may only be
e730: 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74   invoked prior t
e740: 6f 20 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61  o library initia
e750: 6c 69 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a  lization using.*
e760: 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  * [sqlite3_initi
e770: 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65  alize()] or afte
e780: 72 20 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73  r shutdown by [s
e790: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
e7a0: 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77  )]..** Note, how
e7b0: 65 76 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74  ever, that sqlit
e7c0: 65 33 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20  e3_config() can 
e7d0: 62 65 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72  be called as par
e7e0: 74 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c  t of the.** impl
e7f0: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e  ementation of an
e800: 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
e810: 69 6e 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73  ined [sqlite3_os
e820: 5f 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _init()]..**.** 
e830: 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
e840: 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  nt to sqlite3_co
e850: 6e 66 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74  nfig() is an int
e860: 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  eger.** [SQLITE_
e870: 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52  CONFIG_SINGLETHR
e880: 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74  EAD | configurat
e890: 69 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74  ion option] that
e8a0: 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77   determines.** w
e8b0: 68 61 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20  hat property of 
e8c0: 53 51 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20  SQLite is to be 
e8d0: 63 6f 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62  configured.  Sub
e8e0: 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74  sequent argument
e8f0: 73 0a 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64  s.** vary depend
e900: 69 6e 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49  ing on the [SQLI
e910: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
e920: 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75  THREAD | configu
e930: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a  ration option].*
e940: 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  * in the first a
e950: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57  rgument..**.** W
e960: 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74  hen a configurat
e970: 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65  ion option is se
e980: 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  t, sqlite3_confi
e990: 67 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  g() returns [SQL
e9a0: 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74  ITE_OK]..** If t
e9b0: 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b  he option is unk
e9c0: 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69  nown or SQLite i
e9d0: 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20  s unable to set 
e9e0: 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68  the option.** th
e9f0: 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
ea00: 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65  returns a non-ze
ea10: 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e  ro [error code].
ea20: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
ea30: 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d  nts:.** [H14103]
ea40: 20 5b 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32   [H14106] [H1412
ea50: 30 5d 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34  0] [H14123] [H14
ea60: 31 32 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48  126] [H14129] [H
ea70: 31 34 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a  14132] [H14135].
ea80: 2a 2a 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34  ** [H14138] [H14
ea90: 31 34 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48  141] [H14144] [H
eaa0: 31 34 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20  14147] [H14150] 
eab0: 5b 48 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36  [H14153] [H14156
eac0: 5d 20 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48  ] [H14159].** [H
ead0: 31 34 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20  14162] [H14165] 
eae0: 5b 48 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49  [H14168].*/.SQLI
eaf0: 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
eb00: 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
eb10: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e  qlite3_config(in
eb20: 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t, ...);../*.** 
eb30: 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67  CAPI3REF: Config
eb40: 75 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ure database con
eb50: 6e 65 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30  nections  {H1420
eb60: 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45  0} <S20000>.** E
eb70: 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
eb80: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62  * The sqlite3_db
eb90: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66  _config() interf
eba0: 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d  ace is used to m
ebb0: 61 6b 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ake configuratio
ebc0: 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20  n.** changes to 
ebd0: 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  a [database conn
ebe0: 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e  ection].  The in
ebf0: 74 65 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c  terface is simil
ec00: 61 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  ar to.** [sqlite
ec10: 33 5f 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65  3_config()] exce
ec20: 70 74 20 74 68 61 74 20 74 68 65 20 63 68 61 6e  pt that the chan
ec30: 67 65 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73  ges apply to a s
ec40: 69 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61  ingle.** [databa
ec50: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28  se connection] (
ec60: 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
ec70: 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29   first argument)
ec80: 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65  .  The.** sqlite
ec90: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e  3_db_config() in
eca0: 74 65 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79  terface can only
ecb0: 20 62 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61   be used immedia
ecc0: 74 65 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68  tely after.** th
ecd0: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
ece0: 63 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64  ction is created
ecf0: 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
ed00: 6f 70 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  open()],.** [sql
ed10: 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20  ite3_open16()], 
ed20: 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  or [sqlite3_open
ed30: 5f 76 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20  _v2()].  .**.** 
ed40: 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
ed50: 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64  ent to sqlite3_d
ed60: 62 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e  b_config(D,V,...
ed70: 29 20 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e  )  is the.** con
ed80: 66 69 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20  figuration verb 
ed90: 2d 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64  - an integer cod
eda0: 65 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73  e that indicates
edb0: 20 77 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20   what.** aspect 
edc0: 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  of the [database
edd0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20   connection] is 
ede0: 62 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64  being configured
edf0: 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68  ..** The only ch
ee00: 6f 69 63 65 20 66 6f 72 20 74 68 69 73 20 76 61  oice for this va
ee10: 6c 75 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44  lue is [SQLITE_D
ee20: 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  BCONFIG_LOOKASID
ee30: 45 5d 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73  E]..** New verbs
ee40: 20 61 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62   are likely to b
ee50: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
ee60: 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51  e releases of SQ
ee70: 4c 69 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f  Lite..** Additio
ee80: 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65  nal arguments de
ee90: 70 65 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62  pend on the verb
eea0: 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
eeb0: 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33  ents:.** [H14203
eec0: 5d 20 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32  ] [H14206] [H142
eed0: 30 39 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31  09] [H14212] [H1
eee0: 34 32 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  4215].*/.SQLITE_
eef0: 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
ef00: 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69  IMENTAL int sqli
ef10: 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71  te3_db_config(sq
ef20: 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20  lite3*, int op, 
ef30: 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ...);../*.** CAP
ef40: 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c  I3REF: Memory Al
ef50: 6c 6f 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65  location Routine
ef60: 73 20 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31  s {H10155} <S201
ef70: 32 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  20>.** EXPERIMEN
ef80: 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73  TAL.**.** An ins
ef90: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
efa0: 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65  ject defines the
efb0: 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 65   interface betwe
efc0: 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64  en SQLite.** and
efd0: 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72   low-level memor
efe0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75  y allocation rou
eff0: 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  tines..**.** Thi
f000: 73 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64  s object is used
f010: 20 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61   in only one pla
f020: 63 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65  ce in the SQLite
f030: 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41   interface..** A
f040: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
f050: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
f060: 6f 62 6a 65 63 74 20 69 73 20 74 68 65 20 61 72  object is the ar
f070: 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71  gument to.** [sq
f080: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20  lite3_config()] 
f090: 77 68 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75  when the configu
f0a0: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73  ration option is
f0b0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  .** [SQLITE_CONF
f0c0: 49 47 5f 4d 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53  IG_MALLOC] or [S
f0d0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
f0e0: 4d 41 4c 4c 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79  MALLOC].  .** By
f0f0: 20 63 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73   creating an ins
f100: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
f110: 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73  ject.** and pass
f120: 69 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74  ing it to [sqlit
f130: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49  e3_config]([SQLI
f140: 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43  TE_CONFIG_MALLOC
f150: 5d 29 0a 2a 2a 20 64 75 72 69 6e 67 20 63 6f 6e  ]).** during con
f160: 66 69 67 75 72 61 74 69 6f 6e 2c 20 61 6e 20 61  figuration, an a
f170: 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 73  pplication can s
f180: 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
f190: 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  ative.** memory 
f1a0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
f1b0: 73 74 65 6d 20 66 6f 72 20 53 51 4c 69 74 65 20  stem for SQLite 
f1c0: 74 6f 20 75 73 65 20 66 6f 72 20 61 6c 6c 20 6f  to use for all o
f1d0: 66 20 69 74 73 0a 2a 2a 20 64 79 6e 61 6d 69 63  f its.** dynamic
f1e0: 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 2e 0a 2a   memory needs..*
f1f0: 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 53  *.** Note that S
f200: 51 4c 69 74 65 20 63 6f 6d 65 73 20 77 69 74 68  QLite comes with
f210: 20 73 65 76 65 72 61 6c 20 5b 62 75 69 6c 74 2d   several [built-
f220: 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  in memory alloca
f230: 74 6f 72 73 5d 0a 2a 2a 20 74 68 61 74 20 61 72  tors].** that ar
f240: 65 20 70 65 72 66 65 63 74 6c 79 20 61 64 65 71  e perfectly adeq
f250: 75 61 74 65 20 66 6f 72 20 74 68 65 20 6f 76 65  uate for the ove
f260: 72 77 68 65 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69  rwhelming majori
f270: 74 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f  ty of applicatio
f280: 6e 73 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74  ns.** and that t
f290: 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 6f 6e  his object is on
f2a0: 6c 79 20 75 73 65 66 75 6c 20 74 6f 20 61 20 74  ly useful to a t
f2b0: 69 6e 79 20 6d 69 6e 6f 72 69 74 79 20 6f 66 20  iny minority of 
f2c0: 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20  applications.** 
f2d0: 77 69 74 68 20 73 70 65 63 69 61 6c 69 7a 65 64  with specialized
f2e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f2f0: 6f 6e 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e  on requirements.
f300: 20 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73    This object is
f310: 0a 2a 2a 20 61 6c 73 6f 20 75 73 65 64 20 64 75  .** also used du
f320: 72 69 6e 67 20 74 65 73 74 69 6e 67 20 6f 66 20  ring testing of 
f330: 53 51 4c 69 74 65 20 69 6e 20 6f 72 64 65 72 20  SQLite in order 
f340: 74 6f 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c  to specify an al
f350: 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65 6d  ternative.** mem
f360: 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68  ory allocator th
f370: 61 74 20 73 69 6d 75 6c 61 74 65 73 20 6d 65 6d  at simulates mem
f380: 6f 72 79 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72  ory out-of-memor
f390: 79 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e 0a  y conditions in.
f3a0: 2a 2a 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69  ** order to veri
f3b0: 66 79 20 74 68 61 74 20 53 51 4c 69 74 65 20 72  fy that SQLite r
f3c0: 65 63 6f 76 65 72 73 20 67 72 61 63 65 66 75 6c  ecovers graceful
f3d0: 6c 79 20 66 72 6f 6d 20 73 75 63 68 0a 2a 2a 20  ly from such.** 
f3e0: 63 6f 6e 64 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  conditions..**.*
f3f0: 2a 20 54 68 65 20 78 4d 61 6c 6c 6f 63 20 61 6e  * The xMalloc an
f400: 64 20 78 46 72 65 65 20 6d 65 74 68 6f 64 73 20  d xFree methods 
f410: 6d 75 73 74 20 77 6f 72 6b 20 6c 69 6b 65 20 74  must work like t
f420: 68 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61  he.** malloc() a
f430: 6e 64 20 66 72 65 65 28 29 20 66 75 6e 63 74 69  nd free() functi
f440: 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ons from the sta
f450: 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e  ndard C library.
f460: 0a 2a 2a 20 54 68 65 20 78 52 65 61 6c 6c 6f 63  .** The xRealloc
f470: 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 77 6f 72   method must wor
f480: 6b 20 6c 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29  k like realloc()
f490: 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61   from the standa
f4a0: 72 64 20 43 20 6c 69 62 72 61 72 79 0a 2a 2a 20  rd C library.** 
f4b0: 77 69 74 68 20 74 68 65 20 65 78 63 65 70 74 69  with the excepti
f4c0: 6f 6e 20 74 68 61 74 20 69 66 20 74 68 65 20 73  on that if the s
f4d0: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
f4e0: 6f 20 78 52 65 61 6c 6c 6f 63 20 69 73 20 7a 65  o xRealloc is ze
f4f0: 72 6f 2c 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20  ro,.** xRealloc 
f500: 6d 75 73 74 20 62 65 20 61 20 6e 6f 2d 6f 70 20  must be a no-op 
f510: 2d 20 69 74 20 6d 75 73 74 20 6e 6f 74 20 70 65  - it must not pe
f520: 72 66 6f 72 6d 20 61 6e 79 20 61 6c 6c 6f 63 61  rform any alloca
f530: 74 69 6f 6e 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c  tion or.** deall
f540: 6f 63 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65  ocation.  SQLite
f550: 20 67 75 61 72 61 6e 74 65 65 64 73 20 74 68 61   guaranteeds tha
f560: 74 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  t the second arg
f570: 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 78 52 65 61  ument to.** xRea
f580: 6c 6c 6f 63 20 69 73 20 61 6c 77 61 79 73 20 61  lloc is always a
f590: 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
f5a0: 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20  by a prior call 
f5b0: 74 6f 20 78 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20  to xRoundup..** 
f5c0: 41 6e 64 20 73 6f 20 69 6e 20 63 61 73 65 73 20  And so in cases 
f5d0: 77 68 65 72 65 20 78 52 6f 75 6e 64 75 70 20 61  where xRoundup a
f5e0: 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61 20  lways returns a 
f5f0: 70 6f 73 69 74 69 76 65 20 6e 75 6d 62 65 72 2c  positive number,
f600: 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 63 61 6e  .** xRealloc can
f610: 20 70 65 72 66 6f 72 6d 20 65 78 61 63 74 6c 79   perform exactly
f620: 20 61 73 20 74 68 65 20 73 74 61 6e 64 61 72 64   as the standard
f630: 20 6c 69 62 72 61 72 79 20 72 65 61 6c 6c 6f 63   library realloc
f640: 28 29 20 61 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20  () and.** still 
f650: 62 65 20 69 6e 20 63 6f 6d 70 6c 69 61 6e 63 65  be in compliance
f660: 20 77 69 74 68 20 74 68 69 73 20 73 70 65 63 69   with this speci
f670: 66 69 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  fication..**.** 
f680: 78 53 69 7a 65 20 73 68 6f 75 6c 64 20 72 65 74  xSize should ret
f690: 75 72 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  urn the allocate
f6a0: 64 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f  d size of a memo
f6b0: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ry allocation.**
f6c0: 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61   previously obta
f6d0: 69 6e 65 64 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f  ined from xMallo
f6e0: 63 20 6f 72 20 78 52 65 61 6c 6c 6f 63 2e 20 20  c or xRealloc.  
f6f0: 54 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69  The allocated si
f700: 7a 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20  ze.** is always 
f710: 61 74 20 6c 65 61 73 74 20 61 73 20 62 69 67 20  at least as big 
f720: 61 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64  as the requested
f730: 20 73 69 7a 65 20 62 75 74 20 6d 61 79 20 62 65   size but may be
f740: 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54   larger..**.** T
f750: 68 65 20 78 52 6f 75 6e 64 75 70 20 6d 65 74 68  he xRoundup meth
f760: 6f 64 20 72 65 74 75 72 6e 73 20 77 68 61 74 20  od returns what 
f770: 77 6f 75 6c 64 20 62 65 20 74 68 65 20 61 6c 6c  would be the all
f780: 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 0a 2a  ocated size of.*
f790: 2a 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  * a memory alloc
f7a0: 61 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 70 61  ation given a pa
f7b0: 72 74 69 63 75 6c 61 72 20 72 65 71 75 65 73 74  rticular request
f7c0: 65 64 20 73 69 7a 65 2e 20 20 4d 6f 73 74 20 6d  ed size.  Most m
f7d0: 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
f7e0: 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 6d 65 6d  ors round up mem
f7f0: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ory allocations 
f800: 61 74 20 6c 65 61 73 74 20 74 6f 20 74 68 65 20  at least to the 
f810: 6e 65 78 74 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  next multiple.**
f820: 20 6f 66 20 38 2e 20 20 53 6f 6d 65 20 61 6c 6c   of 8.  Some all
f830: 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70  ocators round up
f840: 20 74 6f 20 61 20 6c 61 72 67 65 72 20 6d 75 6c   to a larger mul
f850: 74 69 70 6c 65 20 6f 72 20 74 6f 20 61 20 70 6f  tiple or to a po
f860: 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 20 45 76 65  wer of 2..** Eve
f870: 72 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ry memory alloca
f880: 74 69 6f 6e 20 72 65 71 75 65 73 74 20 63 6f 6d  tion request com
f890: 69 6e 67 20 69 6e 20 74 68 72 6f 75 67 68 20 5b  ing in through [
f8a0: 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
f8b0: 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33  ].** or [sqlite3
f8c0: 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 66 69 72 73  _realloc()] firs
f8d0: 74 20 63 61 6c 6c 73 20 78 52 6f 75 6e 64 75 70  t calls xRoundup
f8e0: 2e 20 20 49 66 20 78 52 6f 75 6e 64 75 70 20 72  .  If xRoundup r
f8f0: 65 74 75 72 6e 73 20 30 2c 20 0a 2a 2a 20 74 68  eturns 0, .** th
f900: 61 74 20 63 61 75 73 65 73 20 74 68 65 20 63 6f  at causes the co
f910: 72 72 65 73 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f  rresponding memo
f920: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f  ry allocation to
f930: 20 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   fail..**.** The
f940: 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e   xInit method in
f950: 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65  itializes the me
f960: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20  mory allocator. 
f970: 20 28 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a   (For example,.*
f980: 2a 20 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63  * it might alloc
f990: 61 74 65 20 61 6e 79 20 72 65 71 75 69 72 65 20  ate any require 
f9a0: 6d 75 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69  mutexes or initi
f9b0: 61 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64  alize internal d
f9c0: 61 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ata.** structure
f9d0: 73 2e 20 20 54 68 65 20 78 53 68 75 74 64 6f 77  s.  The xShutdow
f9e0: 6e 20 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f  n method is invo
f9f0: 6b 65 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29  ked (indirectly)
fa00: 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   by.** [sqlite3_
fa10: 73 68 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20  shutdown()] and 
fa20: 73 68 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74  should deallocat
fa30: 65 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20  e any resources 
fa40: 61 63 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78  acquired.** by x
fa50: 49 6e 69 74 2e 20 20 54 68 65 20 70 41 70 70 44  Init.  The pAppD
fa60: 61 74 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75  ata pointer is u
fa70: 73 65 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20  sed as the only 
fa80: 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20  parameter to.** 
fa90: 78 49 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64  xInit and xShutd
faa0: 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  own..**.** SQLit
fab0: 65 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c  e holds the [SQL
fac0: 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
fad0: 5f 4d 41 53 54 45 52 5d 20 6d 75 74 65 78 20 77  _MASTER] mutex w
fae0: 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 0a 2a  hen it invokes.*
faf0: 2a 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68  * the xInit meth
fb00: 6f 64 2c 20 73 6f 20 74 68 65 20 78 49 6e 69 74  od, so the xInit
fb10: 20 6d 65 74 68 6f 64 20 6e 65 65 64 20 6e 6f 74   method need not
fb20: 20 62 65 20 74 68 72 65 61 64 73 61 66 65 2e 20   be threadsafe. 
fb30: 20 54 68 65 0a 2a 2a 20 78 53 68 75 74 64 6f 77   The.** xShutdow
fb40: 6e 20 6d 65 74 68 6f 64 20 69 73 20 6f 6e 6c 79  n method is only
fb50: 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 5b 73 71   called from [sq
fb60: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
fb70: 5d 20 73 6f 20 69 74 20 64 6f 65 73 0a 2a 2a 20  ] so it does.** 
fb80: 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 74  not need to be t
fb90: 68 72 65 61 64 73 61 66 65 20 65 69 74 68 65 72  hreadsafe either
fba0: 2e 20 20 46 6f 72 20 61 6c 6c 20 6f 74 68 65 72  .  For all other
fbb0: 20 6d 65 74 68 6f 64 73 2c 20 53 51 4c 69 74 65   methods, SQLite
fbc0: 0a 2a 2a 20 68 6f 6c 64 73 20 74 68 65 20 5b 53  .** holds the [S
fbd0: 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
fbe0: 49 43 5f 4d 45 4d 5d 20 6d 75 74 65 78 20 61 73  IC_MEM] mutex as
fbf0: 20 6c 6f 6e 67 20 61 73 20 74 68 65 0a 2a 2a 20   long as the.** 
fc00: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
fc10: 45 4d 53 54 41 54 55 53 5d 20 63 6f 6e 66 69 67  EMSTATUS] config
fc20: 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69  uration option i
fc30: 73 20 74 75 72 6e 65 64 20 6f 6e 20 28 77 68 69  s turned on (whi
fc40: 63 68 0a 2a 2a 20 69 74 20 69 73 20 62 79 20 64  ch.** it is by d
fc50: 65 66 61 75 6c 74 29 20 61 6e 64 20 73 6f 20 74  efault) and so t
fc60: 68 65 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61  he methods are a
fc70: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 72  utomatically ser
fc80: 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65  ialized..** Howe
fc90: 76 65 72 2c 20 69 66 20 5b 53 51 4c 49 54 45 5f  ver, if [SQLITE_
fca0: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53  CONFIG_MEMSTATUS
fcb0: 5d 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74  ] is disabled, t
fcc0: 68 65 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a  hen the other.**
fcd0: 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65   methods must be
fce0: 20 74 68 72 65 61 64 73 61 66 65 20 6f 72 20 65   threadsafe or e
fcf0: 6c 73 65 20 6d 61 6b 65 20 74 68 65 69 72 20 6f  lse make their o
fd00: 77 6e 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20  wn arrangements 
fd10: 66 6f 72 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 61  for.** serializa
fd20: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  tion..**.** SQLi
fd30: 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e  te will never in
fd40: 76 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72  voke xInit() mor
fd50: 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68  e than once with
fd60: 6f 75 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69  out an interveni
fd70: 6e 67 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53  ng.** call to xS
fd80: 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79  hutdown()..*/.ty
fd90: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
fda0: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
fdb0: 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
fdc0: 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c  hods;.struct sql
fdd0: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
fde0: 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61   {.  void *(*xMa
fdf0: 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20  lloc)(int);     
fe00: 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c      /* Memory al
fe10: 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  location functio
fe20: 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46  n */.  void (*xF
fe30: 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20  ree)(void*);    
fe40: 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20        /* Free a 
fe50: 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
fe60: 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52   */.  void *(*xR
fe70: 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e  ealloc)(void*,in
fe80: 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61  t);  /* Resize a
fe90: 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a  n allocation */.
fea0: 20 20 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76    int (*xSize)(v
feb0: 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20  oid*);          
fec0: 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73   /* Return the s
fed0: 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61  ize of an alloca
fee0: 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  tion */.  int (*
fef0: 78 52 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20  xRoundup)(int); 
ff00: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e           /* Roun
ff10: 64 20 75 70 20 72 65 71 75 65 73 74 20 73 69 7a  d up request siz
ff20: 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e to allocation 
ff30: 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  size */.  int (*
ff40: 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20  xInit)(void*);  
ff50: 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74           /* Init
ff60: 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
ff70: 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20  y allocator */. 
ff80: 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77   void (*xShutdow
ff90: 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  n)(void*);      
ffa0: 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20  /* Deinitialize 
ffb0: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
ffc0: 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ator */.  void *
ffd0: 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20  pAppData;       
ffe0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
fff0: 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20  ment to xInit() 
10000 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20  and xShutdown() 
10010 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.};../*.** CAP
10020 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61  I3REF: Configura
10030 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31  tion Options {H1
10040 30 31 36 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a  0160} <S20000>.*
10050 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
10060 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74  *.** These const
10070 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61  ants are the ava
10080 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63  ilable integer c
10090 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
100a0 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e  ions that.** can
100b0 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68   be passed as th
100c0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
100d0 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
100e0 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72  _config()] inter
100f0 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  face..**.** New 
10100 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
10110 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64  tions may be add
10120 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c  ed in future rel
10130 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e  eases of SQLite.
10140 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e  .** Existing con
10150 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
10160 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63  ns might be disc
10170 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69  ontinued.  Appli
10180 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c  cations.** shoul
10190 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75  d check the retu
101a0 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71  rn code from [sq
101b0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20  lite3_config()] 
101c0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
101d0 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f  t.** the call wo
101e0 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69  rked.  The [sqli
101f0 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e  te3_config()] in
10200 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74  terface will ret
10210 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  urn a.** non-zer
10220 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  o [error code] i
10230 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64  f a discontinued
10240 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20   or unsupported 
10250 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
10260 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  tion.** is invok
10270 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  ed..**.** <dl>.*
10280 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
10290 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
102a0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
102b0 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65  re are no argume
102c0 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69  nts to this opti
102d0 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e  on.  This option
102e0 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c   disables.** all
102f0 20 6d 75 74 65 78 69 6e 67 20 61 6e 64 20 70 75   mutexing and pu
10300 74 73 20 53 51 4c 69 74 65 20 69 6e 74 6f 20 61  ts SQLite into a
10310 20 6d 6f 64 65 20 77 68 65 72 65 20 69 74 20 63   mode where it c
10320 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 0a  an only be used.
10330 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74  ** by a single t
10340 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  hread.</dd>.**.*
10350 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
10360 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3c  FIG_MULTITHREAD<
10370 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72  /dt>.** <dd>Ther
10380 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e  e are no argumen
10390 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f  ts to this optio
103a0 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20  n.  This option 
103b0 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65  disables.** mute
103c0 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61 62 61 73  xing on [databas
103d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e  e connection] an
103e0 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  d [prepared stat
103f0 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a  ement] objects..
10400 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  ** The applicati
10410 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  on is responsibl
10420 65 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e  e for serializin
10430 67 20 61 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b  g access to.** [
10440 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
10450 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61  ions] and [prepa
10460 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e  red statements].
10470 20 20 42 75 74 20 6f 74 68 65 72 20 6d 75 74 65    But other mute
10480 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c  xes.** are enabl
10490 65 64 20 73 6f 20 74 68 61 74 20 53 51 4c 69 74  ed so that SQLit
104a0 65 20 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74  e will be safe t
104b0 6f 20 75 73 65 20 69 6e 20 61 20 6d 75 6c 74 69  o use in a multi
104c0 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76  -threaded.** env
104d0 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67  ironment as long
104e0 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 72 65 61   as no two threa
104f0 64 73 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ds attempt to us
10500 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64  e the same.** [d
10510 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
10520 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 6d 65 20  on] at the same 
10530 74 69 6d 65 2e 20 20 53 65 65 20 74 68 65 20 5b  time.  See the [
10540 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 0a  threading mode].
10550 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  ** documentation
10560 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
10570 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64  information.</dd
10580 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
10590 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c  TE_CONFIG_SERIAL
105a0 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  IZED</dt>.** <dd
105b0 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72  >There are no ar
105c0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
105d0 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70  option.  This op
105e0 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20  tion enables.** 
105f0 61 6c 6c 20 6d 75 74 65 78 65 73 20 69 6e 63 6c  all mutexes incl
10600 75 64 69 6e 67 20 74 68 65 20 72 65 63 75 72 73  uding the recurs
10610 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f  ive.** mutexes o
10620 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
10630 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65  ection] and [pre
10640 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
10650 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20   objects..** In 
10660 74 68 69 73 20 6d 6f 64 65 20 28 77 68 69 63 68  this mode (which
10670 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20   is the default 
10680 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63  when SQLite is c
10690 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20  ompiled with.** 
106a0 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
106b0 46 45 3d 31 5d 29 20 74 68 65 20 53 51 4c 69 74  FE=1]) the SQLit
106c0 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 69  e library will i
106d0 74 73 65 6c 66 20 73 65 72 69 61 6c 69 7a 65 20  tself serialize 
106e0 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61  access.** to [da
106f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
10700 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65  ns] and [prepare
10710 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f  d statements] so
10720 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 70 70   that the.** app
10730 6c 69 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  lication is free
10740 20 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65   to use the same
10750 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
10760 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a  ction] or the.**
10770 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20   same [prepared 
10780 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69  statement] in di
10790 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 73 20  fferent threads 
107a0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
107b0 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68  ..** See the [th
107c0 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f  reading mode] do
107d0 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  cumentation for 
107e0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
107f0 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a  mation.</dd>.**.
10800 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
10810 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e  NFIG_MALLOC</dt>
10820 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
10830 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
10840 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
10850 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
10860 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
10870 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
10880 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74  _mem_methods] st
10890 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72  ructure.  The ar
108a0 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73  gument specifies
108b0 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20  .** alternative 
108c0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
108d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74   allocation rout
108e0 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20  ines to be used 
108f0 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74  in place of.** t
10900 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
10910 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 62 75  tion routines bu
10920 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e  ilt into SQLite.
10930 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
10940 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
10950 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20  TMALLOC</dt>.** 
10960 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
10970 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
10980 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
10990 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
109a0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
109b0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  the [sqlite3_mem
109c0 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74  _methods] struct
109d0 75 72 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74  ure.  The [sqlit
109e0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a  e3_mem_methods].
109f0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ** structure is 
10a00 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  filled with the 
10a10 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65  currently define
10a20 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  d memory allocat
10a30 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ion routines..**
10a40 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e   This option can
10a50 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72   be used to over
10a60 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c 74  load the default
10a70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
10a80 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77  on.** routines w
10a90 69 74 68 20 61 20 77 72 61 70 70 65 72 20 74 68  ith a wrapper th
10aa0 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20 6d  at simulations m
10ab0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
10ac0 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20 74   failure or.** t
10ad0 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73 61  racks memory usa
10ae0 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e  ge, for example.
10af0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
10b00 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45  SQLITE_CONFIG_ME
10b10 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20  MSTATUS</dt>.** 
10b20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
10b30 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67  takes single arg
10b40 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e  ument of type in
10b50 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  t, interpreted a
10b60 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c  s a .** boolean,
10b70 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f   which enables o
10b80 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63  r disables the c
10b90 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d  ollection of mem
10ba0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a  ory allocation .
10bb0 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e 20 57  ** statistics. W
10bc0 68 65 6e 20 64 69 73 61 62 6c 65 64 2c 20 74 68  hen disabled, th
10bd0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 69  e following SQLi
10be0 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 62 65  te interfaces be
10bf0 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65  come .** non-ope
10c00 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c  rational:.**   <
10c10 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73  ul>.**   <li> [s
10c20 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
10c30 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20  ed()].**   <li> 
10c40 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f  [sqlite3_memory_
10c50 68 69 67 68 77 61 74 65 72 28 29 5d 0a 2a 2a 20  highwater()].** 
10c60 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f    <li> [sqlite3_
10c70 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28  soft_heap_limit(
10c80 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71  )].**   <li> [sq
10c90 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 0a  lite3_status()].
10ca0 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f  **   </ul>.** </
10cb0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
10cc0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41  LITE_CONFIG_SCRA
10cd0 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TCH</dt>.** <dd>
10ce0 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63  This option spec
10cf0 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d  ifies a static m
10d00 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61  emory buffer tha
10d10 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65  t SQLite can use
10d20 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68 20   for.** scratch 
10d30 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65 20 61  memory.  There a
10d40 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e  re three argumen
10d50 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72 20 61  ts:  A pointer a
10d60 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 61 6c 69 67  n 8-byte.** alig
10d70 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 65  ned memory buffe
10d80 72 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65  r from which the
10d90 20 73 63 72 61 63 68 20 61 6c 6c 6f 63 61 74 69   scrach allocati
10da0 6f 6e 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64  ons will be.** d
10db0 72 61 77 6e 2c 20 74 68 65 20 73 69 7a 65 20 6f  rawn, the size o
10dc0 66 20 65 61 63 68 20 73 63 72 61 74 63 68 20 61  f each scratch a
10dd0 6c 6c 6f 63 61 74 69 6f 6e 20 28 73 7a 29 2c 0a  llocation (sz),.
10de0 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d  ** and the maxim
10df0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 73 63 72  um number of scr
10e00 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  atch allocations
10e10 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a 2a 2a   (N).  The sz.**
10e20 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62   argument must b
10e30 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20  e a multiple of 
10e40 31 36 2e 20 54 68 65 20 73 7a 20 70 61 72 61 6d  16. The sz param
10e50 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61  eter should be a
10e60 20 66 65 77 20 62 79 74 65 73 0a 2a 2a 20 6c 61   few bytes.** la
10e70 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 61 63  rger than the ac
10e80 74 75 61 6c 20 73 63 72 61 74 63 68 20 73 70 61  tual scratch spa
10e90 63 65 20 72 65 71 75 69 72 65 64 20 64 75 65 20  ce required due 
10ea0 74 6f 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72  to internal over
10eb0 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72  head..** The fir
10ec0 73 74 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75  st argument shou
10ed0 6c 64 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e  ld pointer to an
10ee0 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20   8-byte aligned 
10ef0 62 75 66 66 65 72 0a 2a 2a 20 6f 66 20 61 74 20  buffer.** of at 
10f00 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73  least sz*N bytes
10f10 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53   of memory..** S
10f20 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 6e  QLite will use n
10f30 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  o more than one 
10f40 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 61  scratch buffer a
10f50 74 20 6f 6e 63 65 20 70 65 72 20 74 68 72 65 61  t once per threa
10f60 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c  d, so.** N shoul
10f70 64 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20  d be set to the 
10f80 65 78 70 65 63 74 65 64 20 6d 61 78 69 6d 75 6d  expected maximum
10f90 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61   number of threa
10fa0 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 70  ds.  The sz.** p
10fb0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
10fc0 62 65 20 36 20 74 69 6d 65 73 20 74 68 65 20 73  be 6 times the s
10fd0 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65  ize of the large
10fe0 73 74 20 64 61 74 61 62 61 73 65 20 70 61 67 65  st database page
10ff0 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72 61 74 63   size..** Scratc
11000 68 20 62 75 66 66 65 72 73 20 61 72 65 20 75 73  h buffers are us
11010 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
11020 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 20  e btree balance 
11030 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 0a 2a  operation.  If.*
11040 2a 20 54 68 65 20 62 74 72 65 65 20 62 61 6c 61  * The btree bala
11050 6e 63 65 72 20 6e 65 65 64 73 20 61 64 64 69 74  ncer needs addit
11060 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 62 65 79  ional memory bey
11070 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76  ond what is prov
11080 69 64 65 64 20 62 79 0a 2a 2a 20 73 63 72 61 74  ided by.** scrat
11090 63 68 20 62 75 66 66 65 72 73 20 6f 72 20 69 66  ch buffers or if
110a0 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66   no scratch buff
110b0 65 72 20 73 70 61 63 65 20 69 73 20 73 70 65 63  er space is spec
110c0 69 66 69 65 64 2c 20 74 68 65 6e 20 53 51 4c 69  ified, then SQLi
110d0 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f 20 5b 73  te.** goes to [s
110e0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
110f0 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d   to obtain the m
11100 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 3c  emory it needs.<
11110 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
11120 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
11130 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  ECACHE</dt>.** <
11140 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73  dd>This option s
11150 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69  pecifies a stati
11160 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  c memory buffer 
11170 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20  that SQLite can 
11180 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 64  use for.** the d
11190 61 74 61 62 61 73 65 20 70 61 67 65 20 63 61 63  atabase page cac
111a0 68 65 20 77 69 74 68 20 74 68 65 20 64 65 66 61  he with the defa
111b0 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 69  ult page cache i
111c0 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a  mplemenation.  .
111d0 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69 67 75 72  ** This configur
111e0 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ation should not
111f0 20 62 65 20 75 73 65 64 20 69 66 20 61 6e 20 61   be used if an a
11200 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
11210 65 20 70 61 67 65 0a 2a 2a 20 63 61 63 68 65 20  e page.** cache 
11220 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
11230 73 20 6c 6f 61 64 65 64 20 75 73 69 6e 67 20 74  s loaded using t
11240 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  he SQLITE_CONFIG
11250 5f 50 43 41 43 48 45 20 6f 70 74 69 6f 6e 2e 0a  _PCACHE option..
11260 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72  ** There are thr
11270 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ee arguments to 
11280 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20 41 20 70  this option: A p
11290 6f 69 6e 74 65 72 20 74 6f 20 38 2d 62 79 74 65  ointer to 8-byte
112a0 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f   aligned.** memo
112b0 72 79 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ry, the size of 
112c0 65 61 63 68 20 70 61 67 65 20 62 75 66 66 65 72  each page buffer
112d0 20 28 73 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e   (sz), and the n
112e0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 28  umber of pages (
112f0 4e 29 2e 0a 2a 2a 20 54 68 65 20 73 7a 20 61 72  N)..** The sz ar
11300 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65  gument should be
11310 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
11320 20 6c 61 72 67 65 73 74 20 64 61 74 61 62 61 73   largest databas
11330 65 20 70 61 67 65 0a 2a 2a 20 28 61 20 70 6f 77  e page.** (a pow
11340 65 72 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65  er of two betwee
11350 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 29  n 512 and 32768)
11360 20 70 6c 75 73 20 61 20 6c 69 74 74 6c 65 20 65   plus a little e
11370 78 74 72 61 20 66 6f 72 20 65 61 63 68 0a 2a 2a  xtra for each.**
11380 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 54   page header.  T
11390 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 73  he page header s
113a0 69 7a 65 20 69 73 20 32 30 20 74 6f 20 34 30 20  ize is 20 to 40 
113b0 62 79 74 65 73 20 64 65 70 65 6e 64 69 6e 67 20  bytes depending 
113c0 6f 6e 0a 2a 2a 20 74 68 65 20 68 6f 73 74 20 61  on.** the host a
113d0 72 63 68 69 74 65 63 74 75 72 65 2e 20 20 49 74  rchitecture.  It
113e0 20 69 73 20 68 61 72 6d 6c 65 73 73 2c 20 61 70   is harmless, ap
113f0 61 72 74 20 66 72 6f 6d 20 74 68 65 20 77 61 73  art from the was
11400 74 65 64 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74  ted memory,.** t
11410 6f 20 6d 61 6b 65 20 73 7a 20 61 20 6c 69 74 74  o make sz a litt
11420 6c 65 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 54  le too large.  T
11430 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
11440 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e  ment should poin
11450 74 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69  t to an allocati
11460 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73  on of at least s
11470 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  z*N bytes of mem
11480 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77  ory..** SQLite w
11490 69 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f  ill use the memo
114a0 72 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74  ry provided by t
114b0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
114c0 74 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73  t to satisfy its
114d0 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73  .** memory needs
114e0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e   for the first N
114f0 20 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61   pages that it a
11500 64 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49  dds to cache.  I
11510 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  f additional.** 
11520 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  page cache memor
11530 79 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f  y is needed beyo
11540 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69  nd what is provi
11550 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69  ded by this opti
11560 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69  on, then.** SQLi
11570 74 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69  te goes to [sqli
11580 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f  te3_malloc()] fo
11590 72 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c  r the additional
115a0 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a   storage space..
115b0 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** The implement
115c0 61 74 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20  ation might use 
115d0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
115e0 68 65 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20  he N buffers to 
115f0 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20  hold .** memory 
11600 61 63 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72  accounting infor
11610 6d 61 74 69 6f 6e 2e 20 54 68 65 20 70 6f 69 6e  mation. The poin
11620 74 65 72 20 69 6e 20 74 68 65 20 66 69 72 73 74  ter in the first
11630 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 0a 2a   argument must.*
11640 2a 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20  * be aligned to 
11650 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61  an 8-byte bounda
11660 72 79 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74  ry or subsequent
11670 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53 51 4c   behavior of SQL
11680 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 75  ite.** will be u
11690 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a  ndefined.</dd>.*
116a0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
116b0 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e  CONFIG_HEAP</dt>
116c0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
116d0 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20  ion specifies a 
116e0 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75  static memory bu
116f0 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65  ffer that SQLite
11700 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72   will use.** for
11710 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61   all of its dyna
11720 6d 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  mic memory alloc
11730 61 74 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f  ation needs beyo
11740 6e 64 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65  nd those provide
11750 64 0a 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c  d.** for by [SQL
11760 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
11770 43 48 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f  CH] and [SQLITE_
11780 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
11790 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  ]..** There are 
117a0 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a  three arguments:
117b0 20 41 6e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   An 8-byte align
117c0 65 64 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68  ed pointer to th
117d0 65 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65  e memory,.** the
117e0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
117f0 20 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62   in the memory b
11800 75 66 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d  uffer, and the m
11810 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f  inimum allocatio
11820 6e 20 73 69 7a 65 2e 0a 2a 2a 20 49 66 20 74 68  n size..** If th
11830 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20  e first pointer 
11840 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e  (the memory poin
11850 74 65 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68  ter) is NULL, th
11860 65 6e 20 53 51 4c 69 74 65 20 72 65 76 65 72 74  en SQLite revert
11870 73 0a 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74  s.** to using it
11880 73 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79  s default memory
11890 20 61 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20   allocator (the 
118a0 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20  system malloc() 
118b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c  implementation),
118c0 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20  .** undoing any 
118d0 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e  prior invocation
118e0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46   of [SQLITE_CONF
118f0 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20  IG_MALLOC].  If 
11900 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f  the.** memory po
11910 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c  inter is not NUL
11920 4c 20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51  L and either [SQ
11930 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
11940 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49  YS3] or.** [SQLI
11950 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
11960 35 5d 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  5] are defined, 
11970 74 68 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61  then the alterna
11980 74 69 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  tive memory.** a
11990 6c 6c 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61  llocator is enga
119a0 67 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c  ged to handle al
119b0 6c 20 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d  l of SQLites mem
119c0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e  ory allocation n
119d0 65 65 64 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72  eeds..** The fir
119e0 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 20  st pointer (the 
119f0 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 20  memory pointer) 
11a00 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20  must be aligned 
11a10 74 6f 20 61 6e 20 38 2d 62 79 74 65 0a 2a 2a 20  to an 8-byte.** 
11a20 62 6f 75 6e 64 61 72 79 20 6f 72 20 73 75 62 73  boundary or subs
11a30 65 71 75 65 6e 74 20 62 65 68 61 76 69 6f 72 20  equent behavior 
11a40 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62  of SQLite will b
11a50 65 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64  e undefined.</dd
11a60 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
11a70 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c  TE_CONFIG_MUTEX<
11a80 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
11a90 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
11aa0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
11ab0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
11ac0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
11ad0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c  ance of the [sql
11ae0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
11af0 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20  ds] structure.  
11b00 54 68 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65  The argument spe
11b10 63 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e  cifies.** altern
11b20 61 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20  ative low-level 
11b30 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 74  mutex routines t
11b40 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61  o be used in pla
11b50 63 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20  ce.** the mutex 
11b60 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69  routines built i
11b70 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e  nto SQLite.</dd>
11b80 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
11b90 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45  E_CONFIG_GETMUTE
11ba0 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  X</dt>.** <dd>Th
11bb0 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20  is option takes 
11bc0 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  a single argumen
11bd0 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69  t which is a poi
11be0 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e  nter to an.** in
11bf0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73  stance of the [s
11c00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
11c10 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e  hods] structure.
11c20 20 20 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65    The.** [sqlite
11c30 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d  3_mutex_methods]
11c40 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
11c50 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65   filled with the
11c60 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e   currently defin
11c70 65 64 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65  ed mutex routine
11c80 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f  s..** This optio
11c90 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  n can be used to
11ca0 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65   overload the de
11cb0 66 61 75 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f  fault mutex allo
11cc0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e  cation.** routin
11cd0 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65  es with a wrappe
11ce0 72 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20  r used to track 
11cf0 6d 75 74 65 78 20 75 73 61 67 65 20 66 6f 72 20  mutex usage for 
11d00 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70  performance.** p
11d10 72 6f 66 69 6c 69 6e 67 20 6f 72 20 74 65 73 74  rofiling or test
11d20 69 6e 67 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ing, for example
11d30 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
11d40 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c  >SQLITE_CONFIG_L
11d50 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a  OOKASIDE</dt>.**
11d60 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
11d70 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d   takes two argum
11d80 65 6e 74 73 20 74 68 61 74 20 64 65 74 65 72 6d  ents that determ
11d90 69 6e 65 20 74 68 65 20 64 65 66 61 75 6c 74 0a  ine the default.
11da0 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** memory alloca
11db0 74 69 6f 6e 20 6c 6f 6f 6b 61 73 69 64 65 20 6f  tion lookaside o
11dc0 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68  ptimization.  Th
11dd0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
11de0 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20   is the.** size 
11df0 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64  of each lookasid
11e00 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20 61 6e  e buffer slot an
11e10 64 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20  d the second is 
11e20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
11e30 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64   slots allocated
11e40 20 74 6f 20 65 61 63 68 20 64 61 74 61 62 61 73   to each databas
11e50 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54  e connection.  T
11e60 68 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20  his option sets 
11e70 74 68 65 0a 2a 2a 20 3c 69 3e 64 65 66 61 75 6c  the.** <i>defaul
11e80 74 3c 2f 69 3e 20 6c 6f 6f 6b 61 73 69 64 65 20  t</i> lookaside 
11e90 73 69 7a 65 2e 20 20 54 68 65 20 5b 53 51 4c 49  size.  The [SQLI
11ea0 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  TE_DBCONFIG_LOOK
11eb0 41 53 49 44 45 5d 0a 2a 2a 20 76 65 72 62 20 74  ASIDE].** verb t
11ec0 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f  o [sqlite3_db_co
11ed0 6e 66 69 67 28 29 5d 20 63 61 6e 20 62 65 20 75  nfig()] can be u
11ee0 73 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68  sed to change th
11ef0 65 20 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 63  e lookaside.** c
11f00 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 6e 20  onfiguration on 
11f10 69 6e 64 69 76 69 64 75 61 6c 20 63 6f 6e 6e 65  individual conne
11f20 63 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a  ctions.</dd>.**.
11f30 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
11f40 4e 46 49 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e  NFIG_PCACHE</dt>
11f50 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
11f60 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
11f70 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
11f80 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
11f90 6f 0a 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33  o.** an [sqlite3
11fa0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d  _pcache_methods]
11fb0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 6f   object.  This o
11fc0 62 6a 65 63 74 20 73 70 65 63 69 66 69 65 73 20  bject specifies 
11fd0 74 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  the interface.**
11fe0 20 74 6f 20 61 20 63 75 73 74 6f 6d 20 70 61 67   to a custom pag
11ff0 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
12000 74 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20  tation.  SQLite 
12010 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20  makes a copy of 
12020 74 68 65 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e  the.** object an
12030 64 20 75 73 65 73 20 69 74 20 66 6f 72 20 70 61  d uses it for pa
12040 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  ge cache memory 
12050 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64  allocations.</dd
12060 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
12070 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41  TE_CONFIG_GETPCA
12080 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  CHE</dt>.** <dd>
12090 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
120a0 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  s a single argum
120b0 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70  ent which is a p
120c0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ointer to an.** 
120d0 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f  [sqlite3_pcache_
120e0 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e  methods] object.
120f0 20 20 53 51 4c 69 74 65 20 63 6f 70 69 65 73 20    SQLite copies 
12100 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  of the current.*
12110 2a 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  * page cache imp
12120 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f  lementation into
12130 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64   that object.</d
12140 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a  d>.**.** </dl>.*
12150 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
12160 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
12170 52 45 41 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20  READ  1  /* nil 
12180 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
12190 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48  E_CONFIG_MULTITH
121a0 52 45 41 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c  READ   2  /* nil
121b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
121c0 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c  TE_CONFIG_SERIAL
121d0 49 5a 45 44 20 20 20 20 33 20 20 2f 2a 20 6e 69  IZED    3  /* ni
121e0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  l */.#define SQL
121f0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f  ITE_CONFIG_MALLO
12200 43 20 20 20 20 20 20 20 20 34 20 20 2f 2a 20 73  C        4  /* s
12210 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
12220 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ds* */.#define S
12230 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
12240 4d 41 4c 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a  MALLOC     5  /*
12250 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
12260 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65  hods* */.#define
12270 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53   SQLITE_CONFIG_S
12280 43 52 41 54 43 48 20 20 20 20 20 20 20 36 20 20  CRATCH       6  
12290 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a  /* void*, int sz
122a0 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69  , int N */.#defi
122b0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
122c0 5f 50 41 47 45 43 41 43 48 45 20 20 20 20 20 37  _PAGECACHE     7
122d0 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20    /* void*, int 
122e0 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65  sz, int N */.#de
122f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
12300 49 47 5f 48 45 41 50 20 20 20 20 20 20 20 20 20  IG_HEAP         
12310 20 38 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e   8  /* void*, in
12320 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e  t nByte, int min
12330 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
12340 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41  TE_CONFIG_MEMSTA
12350 54 55 53 20 20 20 20 20 39 20 20 2f 2a 20 62 6f  TUS     9  /* bo
12360 6f 6c 65 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  olean */.#define
12370 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
12380 55 54 45 58 20 20 20 20 20 20 20 20 31 30 20 20  UTEX        10  
12390 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  /* sqlite3_mutex
123a0 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65  _methods* */.#de
123b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
123c0 49 47 5f 47 45 54 4d 55 54 45 58 20 20 20 20 20  IG_GETMUTEX     
123d0 31 31 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d  11  /* sqlite3_m
123e0 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f  utex_methods* */
123f0 0a 2f 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 53  ./* previously S
12400 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55  QLITE_CONFIG_CHU
12410 4e 4b 41 4c 4c 4f 43 20 31 32 20 77 68 69 63 68  NKALLOC 12 which
12420 20 69 73 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20   is now unused. 
12430 2a 2f 20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */ .#define SQLI
12440 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  TE_CONFIG_LOOKAS
12450 49 44 45 20 20 20 20 31 33 20 20 2f 2a 20 69 6e  IDE    13  /* in
12460 74 20 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  t int */.#define
12470 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
12480 43 41 43 48 45 20 20 20 20 20 20 20 31 34 20 20  CACHE       14  
12490 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  /* sqlite3_pcach
124a0 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64  e_methods* */.#d
124b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
124c0 46 49 47 5f 47 45 54 50 43 41 43 48 45 20 20 20  FIG_GETPCACHE   
124d0 20 31 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f   15  /* sqlite3_
124e0 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20  pcache_methods* 
124f0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  */../*.** CAPI3R
12500 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f  EF: Configuratio
12510 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 37  n Options {H1017
12520 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45  0} <S20000>.** E
12530 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
12540 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
12550 73 20 61 72 65 20 74 68 65 20 61 76 61 69 6c 61  s are the availa
12560 62 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66  ble integer conf
12570 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
12580 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65  s that.** can be
12590 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
125a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
125b0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64  o the [sqlite3_d
125c0 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65  b_config()] inte
125d0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  rface..**.** New
125e0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
125f0 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64  ptions may be ad
12600 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65  ded in future re
12610 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
12620 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f  ..** Existing co
12630 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
12640 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73  ons might be dis
12650 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c  continued.  Appl
12660 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75  ications.** shou
12670 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74  ld check the ret
12680 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73  urn code from [s
12690 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
126a0 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ()] to make sure
126b0 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c   that.** the cal
126c0 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b  l worked.  The [
126d0 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
126e0 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77  g()] interface w
126f0 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  ill return a.** 
12700 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20  non-zero [error 
12710 63 6f 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f  code] if a disco
12720 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70  ntinued or unsup
12730 70 6f 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61  ported configura
12740 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69  tion option.** i
12750 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a  s invoked..**.**
12760 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c   <dl>.** <dt>SQL
12770 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f  ITE_DBCONFIG_LOO
12780 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  KASIDE</dt>.** <
12790 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74  dd>This option t
127a0 61 6b 65 73 20 74 68 72 65 65 20 61 64 64 69 74  akes three addit
127b0 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20  ional arguments 
127c0 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74  that determine t
127d0 68 65 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64  he .** [lookasid
127e0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
127f0 6f 72 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  or] configuratio
12800 6e 20 66 6f 72 20 74 68 65 20 5b 64 61 74 61 62  n for the [datab
12810 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
12820 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72  .** The first ar
12830 67 75 6d 65 6e 74 20 28 74 68 65 20 74 68 69 72  gument (the thir
12840 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  d parameter to [
12850 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
12860 67 28 29 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69  g()] is a.** poi
12870 6e 74 65 72 20 74 6f 20 61 6e 20 6d 65 6d 6f 72  nter to an memor
12880 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 65 20  y buffer to use 
12890 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  for lookaside me
128a0 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 66 69 72  mory..** The fir
128b0 73 74 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20  st argument may 
128c0 62 65 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68  be NULL in which
128d0 20 63 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c   case SQLite wil
128e0 6c 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 0a 2a  l allocate the.*
128f0 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66  * lookaside buff
12900 65 72 20 69 74 73 65 6c 66 20 75 73 69 6e 67 20  er itself using 
12910 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
12920 29 5d 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  )].  The second 
12930 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a  argument is the.
12940 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20  ** size of each 
12950 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72  lookaside buffer
12960 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 74 68   slot and the th
12970 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
12980 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
12990 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 73 69 7a   slots.  The siz
129a0 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  e of the buffer 
129b0 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67  in the first arg
129c0 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 67 72  ument must be gr
129d0 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72  eater than.** or
129e0 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 70 72   equal to the pr
129f0 6f 64 75 63 74 20 6f 66 20 74 68 65 20 73 65 63  oduct of the sec
12a00 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 61 72  ond and third ar
12a10 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20 62 75  guments.  The bu
12a20 66 66 65 72 0a 2a 2a 20 6d 75 73 74 20 62 65 20  ffer.** must be 
12a30 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d  aligned to an 8-
12a40 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 20  byte boundary.  
12a50 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
12a60 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a  gument is not.**
12a70 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38   a multiple of 8
12a80 2c 20 69 74 20 69 73 20 69 6e 74 65 72 6e 61 6c  , it is internal
12a90 6c 79 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20  ly rounded down 
12aa0 74 6f 20 74 68 65 20 6e 65 78 74 20 73 6d 61 6c  to the next smal
12ab0 6c 65 72 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  ler.** multiple 
12ac0 6f 66 20 38 2e 20 20 53 65 65 20 61 6c 73 6f 3a  of 8.  See also:
12ad0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
12ae0 4c 4f 4f 4b 41 53 49 44 45 5d 3c 2f 64 64 3e 0a  LOOKASIDE]</dd>.
12af0 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23  **.** </dl>.*/.#
12b00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42  define SQLITE_DB
12b10 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
12b20 20 20 20 20 31 30 30 31 20 20 2f 2a 20 76 6f 69      1001  /* voi
12b30 64 2a 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a  d* int int */...
12b40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
12b50 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c  Enable Or Disabl
12b60 65 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c  e Extended Resul
12b70 74 20 43 6f 64 65 73 20 7b 48 31 32 32 30 30 7d  t Codes {H12200}
12b80 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
12b90 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65  The sqlite3_exte
12ba0 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
12bb0 73 28 29 20 72 6f 75 74 69 6e 65 20 65 6e 61 62  s() routine enab
12bc0 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20  les or disables 
12bd0 74 68 65 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64  the.** [extended
12be0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66   result codes] f
12bf0 65 61 74 75 72 65 20 6f 66 20 53 51 4c 69 74 65  eature of SQLite
12c00 2e 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72  . The extended r
12c10 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61  esult.** codes a
12c20 72 65 20 64 69 73 61 62 6c 65 64 20 62 79 20 64  re disabled by d
12c30 65 66 61 75 6c 74 20 66 6f 72 20 68 69 73 74 6f  efault for histo
12c40 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c  rical compatibil
12c50 69 74 79 20 63 6f 6e 73 69 64 65 72 61 74 69 6f  ity consideratio
12c60 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ns..**.** Requir
12c70 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
12c80 30 31 5d 20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a  01] [H12202].*/.
12c90 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
12ca0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
12cb0 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c  result_codes(sql
12cc0 69 74 65 33 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66  ite3*, int onoff
12cd0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
12ce0 45 46 3a 20 4c 61 73 74 20 49 6e 73 65 72 74 20  EF: Last Insert 
12cf0 52 6f 77 69 64 20 7b 48 31 32 32 32 30 7d 20 3c  Rowid {H12220} <
12d00 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61  S10700>.**.** Ea
12d10 63 68 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53  ch entry in an S
12d20 51 4c 69 74 65 20 74 61 62 6c 65 20 68 61 73 20  QLite table has 
12d30 61 20 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20  a unique 64-bit 
12d40 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65  signed.** intege
12d50 72 20 6b 65 79 20 63 61 6c 6c 65 64 20 74 68 65  r key called the
12d60 20 5b 52 4f 57 49 44 20 7c 20 22 72 6f 77 69 64   [ROWID | "rowid
12d70 22 5d 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73  "]. The rowid is
12d80 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
12d90 65 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63  e.** as an undec
12da0 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  lared column nam
12db0 65 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f  ed ROWID, OID, o
12dc0 72 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e  r _ROWID_ as lon
12dd0 67 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61  g as those.** na
12de0 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f  mes are not also
12df0 20 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69   used by explici
12e00 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c  tly declared col
12e10 75 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20  umns. If.** the 
12e20 74 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75  table has a colu
12e30 6d 6e 20 6f 66 20 74 79 70 65 20 5b 49 4e 54 45  mn of type [INTE
12e40 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d  GER PRIMARY KEY]
12e50 20 74 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d   then that colum
12e60 6e 0a 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20  n.** is another 
12e70 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f  alias for the ro
12e80 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  wid..**.** This 
12e90 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
12ea0 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74  the [rowid] of t
12eb0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a  he most recent.*
12ec0 2a 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e  * successful [IN
12ed0 53 45 52 54 5d 20 69 6e 74 6f 20 74 68 65 20 64  SERT] into the d
12ee0 61 74 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65  atabase from the
12ef0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
12f00 63 74 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65  ction].** in the
12f10 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
12f20 20 20 49 66 20 6e 6f 20 73 75 63 63 65 73 73 66    If no successf
12f30 75 6c 20 5b 49 4e 53 45 52 54 5d 73 0a 2a 2a 20  ul [INSERT]s.** 
12f40 68 61 76 65 20 65 76 65 72 20 6f 63 63 75 72 72  have ever occurr
12f50 65 64 20 6f 6e 20 74 68 61 74 20 64 61 74 61 62  ed on that datab
12f60 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  ase connection, 
12f70 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
12f80 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 5b 49  ..**.** If an [I
12f90 4e 53 45 52 54 5d 20 6f 63 63 75 72 73 20 77 69  NSERT] occurs wi
12fa0 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20  thin a trigger, 
12fb0 74 68 65 6e 20 74 68 65 20 5b 72 6f 77 69 64 5d  then the [rowid]
12fc0 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74 65 64   of the inserted
12fd0 0a 2a 2a 20 72 6f 77 20 69 73 20 72 65 74 75 72  .** row is retur
12fe0 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ned by this rout
12ff0 69 6e 65 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ine as long as t
13000 68 65 20 74 72 69 67 67 65 72 20 69 73 20 72 75  he trigger is ru
13010 6e 6e 69 6e 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e  nning..** But on
13020 63 65 20 74 68 65 20 74 72 69 67 67 65 72 20 74  ce the trigger t
13030 65 72 6d 69 6e 61 74 65 73 2c 20 74 68 65 20 76  erminates, the v
13040 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
13050 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
13060 20 72 65 76 65 72 74 73 20 74 6f 20 74 68 65 20   reverts to the 
13070 6c 61 73 74 20 76 61 6c 75 65 20 69 6e 73 65 72  last value inser
13080 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 74  ted before the t
13090 72 69 67 67 65 72 20 66 69 72 65 64 2e 0a 2a 2a  rigger fired..**
130a0 0a 2a 2a 20 41 6e 20 5b 49 4e 53 45 52 54 5d 20  .** An [INSERT] 
130b0 74 68 61 74 20 66 61 69 6c 73 20 64 75 65 20 74  that fails due t
130c0 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  o a constraint v
130d0 69 6f 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  iolation is not 
130e0 61 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20  a.** successful 
130f0 5b 49 4e 53 45 52 54 5d 20 61 6e 64 20 64 6f 65  [INSERT] and doe
13100 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  s not change the
13110 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
13120 62 79 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  by this.** routi
13130 6e 65 2e 20 20 54 68 75 73 20 49 4e 53 45 52 54  ne.  Thus INSERT
13140 20 4f 52 20 46 41 49 4c 2c 20 49 4e 53 45 52 54   OR FAIL, INSERT
13150 20 4f 52 20 49 47 4e 4f 52 45 2c 20 49 4e 53 45   OR IGNORE, INSE
13160 52 54 20 4f 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a  RT OR ROLLBACK,.
13170 2a 2a 20 61 6e 64 20 49 4e 53 45 52 54 20 4f 52  ** and INSERT OR
13180 20 41 42 4f 52 54 20 6d 61 6b 65 20 6e 6f 20 63   ABORT make no c
13190 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 72 65  hanges to the re
131a0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
131b0 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68  is.** routine wh
131c0 65 6e 20 74 68 65 69 72 20 69 6e 73 65 72 74 69  en their inserti
131d0 6f 6e 20 66 61 69 6c 73 2e 20 20 57 68 65 6e 20  on fails.  When 
131e0 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
131f0 45 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 73 20  E.** encounters 
13200 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  a constraint vio
13210 6c 61 74 69 6f 6e 2c 20 69 74 20 64 6f 65 73 20  lation, it does 
13220 6e 6f 74 20 66 61 69 6c 2e 20 20 54 68 65 0a 2a  not fail.  The.*
13230 2a 20 49 4e 53 45 52 54 20 63 6f 6e 74 69 6e 75  * INSERT continu
13240 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e  es to completion
13250 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20   after deleting 
13260 72 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64  rows that caused
13270 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69  .** the constrai
13280 6e 74 20 70 72 6f 62 6c 65 6d 20 73 6f 20 49 4e  nt problem so IN
13290 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20  SERT OR REPLACE 
132a0 77 69 6c 6c 20 61 6c 77 61 79 73 20 63 68 61 6e  will always chan
132b0 67 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e  ge.** the return
132c0 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 69   value of this i
132d0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
132e0 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
132f0 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
13300 2c 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73  , an [INSERT] is
13310 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a   considered to.*
13320 2a 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 20  * be successful 
13330 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 73 75  even if it is su
13340 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65  bsequently rolle
13350 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65  d back..**.** Re
13360 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
13370 48 31 32 32 32 31 5d 20 5b 48 31 32 32 32 33 5d  H12221] [H12223]
13380 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61  .**.** If a sepa
13390 72 61 74 65 20 74 68 72 65 61 64 20 70 65 72 66  rate thread perf
133a0 6f 72 6d 73 20 61 20 6e 65 77 20 5b 49 4e 53 45  orms a new [INSE
133b0 52 54 5d 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a  RT] on the same.
133c0 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
133d0 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 65  ection while the
133e0 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69   [sqlite3_last_i
133f0 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 0a 2a  nsert_rowid()].*
13400 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75  * function is ru
13410 6e 6e 69 6e 67 20 61 6e 64 20 74 68 75 73 20 63  nning and thus c
13420 68 61 6e 67 65 73 20 74 68 65 20 6c 61 73 74 20  hanges the last 
13430 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2c 0a  insert [rowid],.
13440 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  ** then the valu
13450 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73  e returned by [s
13460 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
13470 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73 0a 2a  rt_rowid()] is.*
13480 2a 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20  * unpredictable 
13490 61 6e 64 20 6d 69 67 68 74 20 6e 6f 74 20 65 71  and might not eq
134a0 75 61 6c 20 65 69 74 68 65 72 20 74 68 65 20 6f  ual either the o
134b0 6c 64 20 6f 72 20 74 68 65 20 6e 65 77 0a 2a 2a  ld or the new.**
134c0 20 6c 61 73 74 20 69 6e 73 65 72 74 20 5b 72 6f   last insert [ro
134d0 77 69 64 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  wid]..*/.SQLITE_
134e0 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
134f0 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  4 sqlite3_last_i
13500 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69  nsert_rowid(sqli
13510 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
13520 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 20 54 68  PI3REF: Count Th
13530 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73  e Number Of Rows
13540 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32 34   Modified {H1224
13550 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a  0} <S10600>.**.*
13560 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
13570 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
13580 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 20 72  er of database r
13590 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63 68  ows that were ch
135a0 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 6e 73 65  anged.** or inse
135b0 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20  rted or deleted 
135c0 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  by the most rece
135d0 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 53  ntly completed S
135e0 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  QL statement.** 
135f0 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  on the [database
13600 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65   connection] spe
13610 63 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69  cified by the fi
13620 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  rst parameter..*
13630 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20 74  * Only changes t
13640 68 61 74 20 61 72 65 20 64 69 72 65 63 74 6c 79  hat are directly
13650 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
13660 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50 44  e [INSERT], [UPD
13670 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c  ATE],.** or [DEL
13680 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 20 61  ETE] statement a
13690 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41 75 78  re counted.  Aux
136a0 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73 20 63  iliary changes c
136b0 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 72 69 67  aused by.** trig
136c0 67 65 72 73 20 6f 72 20 5b 66 6f 72 65 69 67 6e  gers or [foreign
136d0 20 6b 65 79 20 61 63 74 69 6f 6e 73 5d 20 61 72   key actions] ar
136e0 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20 55  e not counted. U
136f0 73 65 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  se the.** [sqlit
13700 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
13710 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ()] function to 
13720 66 69 6e 64 20 74 68 65 20 74 6f 74 61 6c 20 6e  find the total n
13730 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73  umber of changes
13740 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 68  .** including ch
13750 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20  anges caused by 
13760 74 72 69 67 67 65 72 73 20 61 6e 64 20 66 6f 72  triggers and for
13770 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73  eign key actions
13780 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20  ..**.** Changes 
13790 74 6f 20 61 20 76 69 65 77 20 74 68 61 74 20 61  to a view that a
137a0 72 65 20 73 69 6d 75 6c 61 74 65 64 20 62 79 20  re simulated by 
137b0 61 6e 20 5b 49 4e 53 54 45 41 44 20 4f 46 20 74  an [INSTEAD OF t
137c0 72 69 67 67 65 72 5d 0a 2a 2a 20 61 72 65 20 6e  rigger].** are n
137d0 6f 74 20 63 6f 75 6e 74 65 64 2e 20 20 4f 6e 6c  ot counted.  Onl
137e0 79 20 72 65 61 6c 20 74 61 62 6c 65 20 63 68 61  y real table cha
137f0 6e 67 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64  nges are counted
13800 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63  ..**.** A "row c
13810 68 61 6e 67 65 22 20 69 73 20 61 20 63 68 61 6e  hange" is a chan
13820 67 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 72  ge to a single r
13830 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74  ow of a single t
13840 61 62 6c 65 0a 2a 2a 20 63 61 75 73 65 64 20 62  able.** caused b
13850 79 20 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c  y an INSERT, DEL
13860 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73  ETE, or UPDATE s
13870 74 61 74 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20  tatement.  Rows 
13880 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e  that.** are chan
13890 67 65 64 20 61 73 20 73 69 64 65 20 65 66 66 65  ged as side effe
138a0 63 74 73 20 6f 66 20 5b 52 45 50 4c 41 43 45 5d  cts of [REPLACE]
138b0 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f   constraint reso
138c0 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62  lution,.** rollb
138d0 61 63 6b 2c 20 41 42 4f 52 54 20 70 72 6f 63 65  ack, ABORT proce
138e0 73 73 69 6e 67 2c 20 5b 44 52 4f 50 20 54 41 42  ssing, [DROP TAB
138f0 4c 45 5d 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f  LE], or by any o
13900 74 68 65 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73  ther.** mechanis
13910 6d 73 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20  ms do not count 
13920 61 73 20 64 69 72 65 63 74 20 72 6f 77 20 63 68  as direct row ch
13930 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22  anges..**.** A "
13940 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 22  trigger context"
13950 20 69 73 20 61 20 73 63 6f 70 65 20 6f 66 20 65   is a scope of e
13960 78 65 63 75 74 69 6f 6e 20 74 68 61 74 20 62 65  xecution that be
13970 67 69 6e 73 20 61 6e 64 0a 2a 2a 20 65 6e 64 73  gins and.** ends
13980 20 77 69 74 68 20 74 68 65 20 73 63 72 69 70 74   with the script
13990 20 6f 66 20 61 20 5b 43 52 45 41 54 45 20 54 52   of a [CREATE TR
139a0 49 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d  IGGER | trigger]
139b0 2e 20 0a 2a 2a 20 4d 6f 73 74 20 53 51 4c 20 73  . .** Most SQL s
139c0 74 61 74 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a  tatements are.**
139d0 20 65 76 61 6c 75 61 74 65 64 20 6f 75 74 73 69   evaluated outsi
139e0 64 65 20 6f 66 20 61 6e 79 20 74 72 69 67 67 65  de of any trigge
139f0 72 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  r.  This is the 
13a00 22 74 6f 70 20 6c 65 76 65 6c 22 0a 2a 2a 20 74  "top level".** t
13a10 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 20  rigger context. 
13a20 20 49 66 20 61 20 74 72 69 67 67 65 72 20 66 69   If a trigger fi
13a30 72 65 73 20 66 72 6f 6d 20 74 68 65 20 74 6f 70  res from the top
13a40 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77   level, a.** new
13a50 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74   trigger context
13a60 20 69 73 20 65 6e 74 65 72 65 64 20 66 6f 72 20   is entered for 
13a70 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20  the duration of 
13a80 74 68 61 74 20 6f 6e 65 0a 2a 2a 20 74 72 69 67  that one.** trig
13a90 67 65 72 2e 20 20 53 75 62 74 72 69 67 67 65 72  ger.  Subtrigger
13aa0 73 20 63 72 65 61 74 65 20 73 75 62 63 6f 6e 74  s create subcont
13ab0 65 78 74 73 20 66 6f 72 20 74 68 65 69 72 20 64  exts for their d
13ac0 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43  uration..**.** C
13ad0 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
13ae0 65 78 65 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69  exec()] or [sqli
13af0 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65 63 75  te3_step()] recu
13b00 72 73 69 76 65 6c 79 20 64 6f 65 73 0a 2a 2a 20  rsively does.** 
13b10 6e 6f 74 20 63 72 65 61 74 65 20 61 20 6e 65 77  not create a new
13b20 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74   trigger context
13b30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
13b40 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
13b50 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 72 65  e number of dire
13b60 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73 20 69  ct row changes i
13b70 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65  n the.** most re
13b80 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44  cent INSERT, UPD
13b90 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 20 73  ATE, or DELETE s
13ba0 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20  tatement within 
13bb0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 69 67  the same.** trig
13bc0 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a  ger context..**.
13bd0 2a 2a 20 54 68 75 73 2c 20 77 68 65 6e 20 63 61  ** Thus, when ca
13be0 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f  lled from the to
13bf0 70 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 66 75  p level, this fu
13c00 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
13c10 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
13c20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d  changes in the m
13c30 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52  ost recent INSER
13c40 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45  T, UPDATE, or DE
13c50 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 61 6c 73  LETE.** that als
13c60 6f 20 6f 63 63 75 72 72 65 64 20 61 74 20 74 68  o occurred at th
13c70 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20 20 57 69  e top level.  Wi
13c80 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66  thin the body of
13c90 20 61 20 74 72 69 67 67 65 72 2c 0a 2a 2a 20 74   a trigger,.** t
13ca0 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  he sqlite3_chang
13cb0 65 73 28 29 20 69 6e 74 65 72 66 61 63 65 20 63  es() interface c
13cc0 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20  an be called to 
13cd0 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20  find the number 
13ce0 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e  of.** changes in
13cf0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
13d00 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 49 4e 53  ly completed INS
13d10 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
13d20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d  DELETE.** statem
13d30 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65 20 62  ent within the b
13d40 6f 64 79 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ody of the same 
13d50 74 72 69 67 67 65 72 2e 0a 2a 2a 20 48 6f 77 65  trigger..** Howe
13d60 76 65 72 2c 20 74 68 65 20 6e 75 6d 62 65 72 20  ver, the number 
13d70 72 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f  returned does no
13d80 74 20 69 6e 63 6c 75 64 65 20 63 68 61 6e 67 65  t include change
13d90 73 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 73  s.** caused by s
13da0 75 62 74 72 69 67 67 65 72 73 20 73 69 6e 63 65  ubtriggers since
13db0 20 74 68 6f 73 65 20 68 61 76 65 20 74 68 65 69   those have thei
13dc0 72 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a  r own context..*
13dd0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68  *.** See also th
13de0 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  e [sqlite3_total
13df0 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65  _changes()] inte
13e00 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a  rface and the.**
13e10 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20   [count_changes 
13e20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52  pragma]..**.** R
13e30 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
13e40 5b 48 31 32 32 34 31 5d 20 5b 48 31 32 32 34 33  [H12241] [H12243
13e50 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70  ].**.** If a sep
13e60 61 72 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b  arate thread mak
13e70 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68  es changes on th
13e80 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
13e90 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 68  connection.** wh
13ea0 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61  ile [sqlite3_cha
13eb0 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69  nges()] is runni
13ec0 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  ng then the valu
13ed0 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73  e returned.** is
13ee0 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61   unpredictable a
13ef0 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75  nd not meaningfu
13f00 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  l..*/.SQLITE_API
13f10 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61   int sqlite3_cha
13f20 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a  nges(sqlite3*);.
13f30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
13f40 20 54 6f 74 61 6c 20 4e 75 6d 62 65 72 20 4f 66   Total Number Of
13f50 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b   Rows Modified {
13f60 48 31 32 32 36 30 7d 20 3c 53 31 30 36 30 30 3e  H12260} <S10600>
13f70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
13f80 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
13f90 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 20 63   number of row c
13fa0 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79  hanges caused by
13fb0 20 5b 49 4e 53 45 52 54 5d 2c 0a 2a 2a 20 5b 55   [INSERT],.** [U
13fc0 50 44 41 54 45 5d 20 6f 72 20 5b 44 45 4c 45 54  PDATE] or [DELET
13fd0 45 5d 20 73 74 61 74 65 6d 65 6e 74 73 20 73 69  E] statements si
13fe0 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62 61 73  nce the [databas
13ff0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 61  e connection] wa
14000 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54 68 65  s opened..** The
14010 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65 73 20   count includes 
14020 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72 6f 6d  all changes from
14030 20 61 6c 6c 20 5b 43 52 45 41 54 45 20 54 52 49   all [CREATE TRI
14040 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d 20  GGER | trigger] 
14050 0a 2a 2a 20 63 6f 6e 74 65 78 74 73 20 61 6e 64  .** contexts and
14060 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79   changes made by
14070 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63   [foreign key ac
14080 74 69 6f 6e 73 5d 2e 20 48 6f 77 65 76 65 72 2c  tions]. However,
14090 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 6f  .** the count do
140a0 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63  es not include c
140b0 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f 20 69  hanges used to i
140c0 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43  mplement [REPLAC
140d0 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a  E] constraints,.
140e0 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20  ** do rollbacks 
140f0 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73  or ABORT process
14100 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41  ing, or [DROP TA
14110 42 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e  BLE] processing.
14120 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64    The.** count d
14130 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20  oes not include 
14140 72 6f 77 73 20 6f 66 20 76 69 65 77 73 20 74 68  rows of views th
14150 61 74 20 66 69 72 65 20 61 6e 20 5b 49 4e 53 54  at fire an [INST
14160 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 2c  EAD OF trigger],
14170 0a 2a 2a 20 74 68 6f 75 67 68 20 69 66 20 74 68  .** though if th
14180 65 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69  e INSTEAD OF tri
14190 67 67 65 72 20 6d 61 6b 65 73 20 63 68 61 6e 67  gger makes chang
141a0 65 73 20 6f 66 20 69 74 73 20 6f 77 6e 2c 20 74  es of its own, t
141b0 68 6f 73 65 20 63 68 61 6e 67 65 73 20 0a 2a 2a  hose changes .**
141c0 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a   are counted..**
141d0 20 54 68 65 20 63 68 61 6e 67 65 73 20 61 72 65   The changes are
141e0 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e   counted as soon
141f0 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e   as the statemen
14200 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65  t that makes the
14210 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65  m is.** complete
14220 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 61 74  d (when the stat
14230 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 20  ement handle is 
14240 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74  passed to [sqlit
14250 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a  e3_reset()] or.*
14260 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  * [sqlite3_final
14270 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53  ize()])..**.** S
14280 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c  ee also the [sql
14290 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20  ite3_changes()] 
142a0 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 74 68  interface and th
142b0 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e  e.** [count_chan
142c0 67 65 73 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a  ges pragma]..**.
142d0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
142e0 0a 2a 2a 20 5b 48 31 32 32 36 31 5d 20 5b 48 31  .** [H12261] [H1
142f0 32 32 36 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61  2263].**.** If a
14300 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64   separate thread
14310 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f   makes changes o
14320 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  n the same datab
14330 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
14340 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33  * while [sqlite3
14350 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
14360 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65  ] is running the
14370 6e 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72  n the value.** r
14380 65 74 75 72 6e 65 64 20 69 73 20 75 6e 70 72 65  eturned is unpre
14390 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74  dictable and not
143a0 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a   meaningful..*/.
143b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
143c0 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
143d0 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a  nges(sqlite3*);.
143e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
143f0 20 49 6e 74 65 72 72 75 70 74 20 41 20 4c 6f 6e   Interrupt A Lon
14400 67 2d 52 75 6e 6e 69 6e 67 20 51 75 65 72 79 20  g-Running Query 
14410 7b 48 31 32 32 37 30 7d 20 3c 53 33 30 35 30 30  {H12270} <S30500
14420 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  >.**.** This fun
14430 63 74 69 6f 6e 20 63 61 75 73 65 73 20 61 6e 79  ction causes any
14440 20 70 65 6e 64 69 6e 67 20 64 61 74 61 62 61 73   pending databas
14450 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61  e operation to a
14460 62 6f 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75  bort and.** retu
14470 72 6e 20 61 74 20 69 74 73 20 65 61 72 6c 69 65  rn at its earlie
14480 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20  st opportunity. 
14490 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
144a0 74 79 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c  typically.** cal
144b0 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 20  led in response 
144c0 74 6f 20 61 20 75 73 65 72 20 61 63 74 69 6f 6e  to a user action
144d0 20 73 75 63 68 20 61 73 20 70 72 65 73 73 69 6e   such as pressin
144e0 67 20 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72  g "Cancel".** or
144f0 20 43 74 72 6c 2d 43 20 77 68 65 72 65 20 74 68   Ctrl-C where th
14500 65 20 75 73 65 72 20 77 61 6e 74 73 20 61 20 6c  e user wants a l
14510 6f 6e 67 20 71 75 65 72 79 20 6f 70 65 72 61 74  ong query operat
14520 69 6f 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69  ion to halt.** i
14530 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a  mmediately..**.*
14540 2a 20 49 74 20 69 73 20 73 61 66 65 20 74 6f 20  * It is safe to 
14550 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
14560 65 20 66 72 6f 6d 20 61 20 74 68 72 65 61 64 20  e from a thread 
14570 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74  different from t
14580 68 65 0a 2a 2a 20 74 68 72 65 61 64 20 74 68 61  he.** thread tha
14590 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72  t is currently r
145a0 75 6e 6e 69 6e 67 20 74 68 65 20 64 61 74 61 62  unning the datab
145b0 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  ase operation.  
145c0 42 75 74 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74  But it.** is not
145d0 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68   safe to call th
145e0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  is routine with 
145f0 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  a [database conn
14600 65 63 74 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20  ection] that.** 
14610 69 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67  is closed or mig
14620 68 74 20 63 6c 6f 73 65 20 62 65 66 6f 72 65 20  ht close before 
14630 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
14640 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a  t() returns..**.
14650 2a 2a 20 49 66 20 61 6e 20 53 51 4c 20 6f 70 65  ** If an SQL ope
14660 72 61 74 69 6f 6e 20 69 73 20 76 65 72 79 20 6e  ration is very n
14670 65 61 72 6c 79 20 66 69 6e 69 73 68 65 64 20 61  early finished a
14680 74 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 0a  t the time when.
14690 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  ** sqlite3_inter
146a0 72 75 70 74 28 29 20 69 73 20 63 61 6c 6c 65 64  rupt() is called
146b0 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20  , then it might 
146c0 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f 70 70 6f  not have an oppo
146d0 72 74 75 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65  rtunity.** to be
146e0 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 6e 64   interrupted and
146f0 20 6d 69 67 68 74 20 63 6f 6e 74 69 6e 75 65 20   might continue 
14700 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a  to completion..*
14710 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72  *.** An SQL oper
14720 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e  ation that is in
14730 74 65 72 72 75 70 74 65 64 20 77 69 6c 6c 20 72  terrupted will r
14740 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e  eturn [SQLITE_IN
14750 54 45 52 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20  TERRUPT]..** If 
14760 74 68 65 20 69 6e 74 65 72 72 75 70 74 65 64 20  the interrupted 
14770 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  SQL operation is
14780 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41   an INSERT, UPDA
14790 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a  TE, or DELETE.**
147a0 20 74 68 61 74 20 69 73 20 69 6e 73 69 64 65 20   that is inside 
147b0 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e  an explicit tran
147c0 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  saction, then th
147d0 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63  e entire transac
147e0 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  tion.** will be 
147f0 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f  rolled back auto
14800 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a  matically..**.**
14810 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74   The sqlite3_int
14820 65 72 72 75 70 74 28 44 29 20 63 61 6c 6c 20 69  errupt(D) call i
14830 73 20 69 6e 20 65 66 66 65 63 74 20 75 6e 74 69  s in effect unti
14840 6c 20 61 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20  l all currently 
14850 72 75 6e 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73  running.** SQL s
14860 74 61 74 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61  tatements on [da
14870 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14880 6e 5d 20 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20  n] D complete.  
14890 41 6e 79 20 6e 65 77 20 53 51 4c 20 73 74 61 74  Any new SQL stat
148a0 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61  ements.** that a
148b0 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72  re started after
148c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74   the sqlite3_int
148d0 65 72 72 75 70 74 28 29 20 63 61 6c 6c 20 61 6e  errupt() call an
148e0 64 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a  d before the .**
148f0 20 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65   running stateme
14900 6e 74 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f  nts reaches zero
14910 20 61 72 65 20 69 6e 74 65 72 72 75 70 74 65 64   are interrupted
14920 20 61 73 20 69 66 20 74 68 65 79 20 68 61 64 20   as if they had 
14930 62 65 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20  been.** running 
14940 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 71 6c  prior to the sql
14950 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
14960 20 63 61 6c 6c 2e 20 20 4e 65 77 20 53 51 4c 20   call.  New SQL 
14970 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68  statements.** th
14980 61 74 20 61 72 65 20 73 74 61 72 74 65 64 20 61  at are started a
14990 66 74 65 72 20 74 68 65 20 72 75 6e 6e 69 6e 67  fter the running
149a0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74   statement count
149b0 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72   reaches zero ar
149c0 65 0a 2a 2a 20 6e 6f 74 20 65 66 66 65 63 74 65  e.** not effecte
149d0 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  d by the sqlite3
149e0 5f 69 6e 74 65 72 72 75 70 74 28 29 2e 0a 2a 2a  _interrupt()..**
149f0 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   A call to sqlit
14a00 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20  e3_interrupt(D) 
14a10 74 68 61 74 20 6f 63 63 75 72 73 20 77 68 65 6e  that occurs when
14a20 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 75   there are no ru
14a30 6e 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61  nning.** SQL sta
14a40 74 65 6d 65 6e 74 73 20 69 73 20 61 20 6e 6f 2d  tements is a no-
14a50 6f 70 20 61 6e 64 20 68 61 73 20 6e 6f 20 65 66  op and has no ef
14a60 66 65 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 74  fect on SQL stat
14a70 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61  ements.** that a
14a80 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72  re started after
14a90 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74   the sqlite3_int
14aa0 65 72 72 75 70 74 28 29 20 63 61 6c 6c 20 72 65  errupt() call re
14ab0 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  turns..**.** Req
14ac0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
14ad0 31 32 32 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a  12271] [H12272].
14ae0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74  **.** If the dat
14af0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
14b00 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73   closes while [s
14b10 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
14b20 28 29 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e  ()].** is runnin
14b30 67 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67  g then bad thing
14b40 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61  s will likely ha
14b50 70 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ppen..*/.SQLITE_
14b60 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
14b70 5f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74  _interrupt(sqlit
14b80 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  e3*);../*.** CAP
14b90 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65  I3REF: Determine
14ba0 20 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65   If An SQL State
14bb0 6d 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65  ment Is Complete
14bc0 20 7b 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30   {H10510} <S7020
14bd0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
14be0 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 66  outines are usef
14bf0 75 6c 20 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e  ul during comman
14c00 64 2d 6c 69 6e 65 20 69 6e 70 75 74 20 74 6f 20  d-line input to 
14c10 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65  determine if the
14c20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 6e  .** currently en
14c30 74 65 72 65 64 20 74 65 78 74 20 73 65 65 6d 73  tered text seems
14c40 20 74 6f 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c   to form a compl
14c50 65 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ete SQL statemen
14c60 74 20 6f 72 0a 2a 2a 20 69 66 20 61 64 64 69 74  t or.** if addit
14c70 69 6f 6e 61 6c 20 69 6e 70 75 74 20 69 73 20 6e  ional input is n
14c80 65 65 64 65 64 20 62 65 66 6f 72 65 20 73 65 6e  eeded before sen
14c90 64 69 6e 67 20 74 68 65 20 74 65 78 74 20 69 6e  ding the text in
14ca0 74 6f 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72  to.** SQLite for
14cb0 20 70 61 72 73 69 6e 67 2e 20 20 54 68 65 73 65   parsing.  These
14cc0 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
14cd0 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20   1 if the input 
14ce0 73 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72  string.** appear
14cf0 73 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65  s to be a comple
14d00 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  te SQL statement
14d10 2e 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69  .  A statement i
14d20 73 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a  s judged to be.*
14d30 2a 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74  * complete if it
14d40 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d   ends with a sem
14d50 69 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64  icolon token and
14d60 20 69 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78   is not a prefix
14d70 20 6f 66 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f   of a.** well-fo
14d80 72 6d 65 64 20 43 52 45 41 54 45 20 54 52 49 47  rmed CREATE TRIG
14d90 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  GER statement.  
14da0 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20  Semicolons that 
14db0 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 74  are embedded wit
14dc0 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69  hin.** string li
14dd0 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64  terals or quoted
14de0 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65   identifier name
14df0 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72  s or comments ar
14e00 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e  e not.** indepen
14e10 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65  dent tokens (the
14e20 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68  y are part of th
14e30 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68  e token in which
14e40 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62   they are.** emb
14e50 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 20  edded) and thus 
14e60 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20  do not count as 
14e70 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d  a statement term
14e80 69 6e 61 74 6f 72 2e 20 20 57 68 69 74 65 73 70  inator.  Whitesp
14e90 61 63 65 0a 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65  ace.** and comme
14ea0 6e 74 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20  nts that follow 
14eb0 74 68 65 20 66 69 6e 61 6c 20 73 65 6d 69 63 6f  the final semico
14ec0 6c 6f 6e 20 61 72 65 20 69 67 6e 6f 72 65 64 2e  lon are ignored.
14ed0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
14ee0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 30 20 69  tines return 0 i
14ef0 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
14f00 69 73 20 69 6e 63 6f 6d 70 6c 65 74 65 2e 20 20  is incomplete.  
14f10 49 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61  If a.** memory a
14f20 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
14f30 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d   then SQLITE_NOM
14f40 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  EM is returned..
14f50 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
14f60 69 6e 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73  ines do not pars
14f70 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  e the SQL statem
14f80 65 6e 74 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c  ents thus.** wil
14f90 6c 20 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e  l not detect syn
14fa0 74 61 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72  tactically incor
14fb0 72 65 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20  rect SQL..**.** 
14fc0 49 66 20 53 51 4c 69 74 65 20 68 61 73 20 6e 6f  If SQLite has no
14fd0 74 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  t been initializ
14fe0 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ed using [sqlite
14ff0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20  3_initialize()] 
15000 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 69 6e 76  prior .** to inv
15010 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f  oking sqlite3_co
15020 6d 70 6c 65 74 65 31 36 28 29 20 74 68 65 6e 20  mplete16() then 
15030 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
15040 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a  ze() is invoked.
15050 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ** automatically
15060 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70   by sqlite3_comp
15070 6c 65 74 65 31 36 28 29 2e 20 20 49 66 20 74 68  lete16().  If th
15080 61 74 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  at initializatio
15090 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e  n fails,.** then
150a0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
150b0 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63  e from sqlite3_c
150c0 6f 6d 70 6c 65 74 65 31 36 28 29 20 77 69 6c 6c  omplete16() will
150d0 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20   be non-zero.** 
150e0 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
150f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
15100 20 69 6e 70 75 74 20 53 51 4c 20 69 73 20 63 6f   input SQL is co
15110 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65  mplete..**.** Re
15120 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30  quirements: [H10
15130 35 31 31 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a  511] [H10512].**
15140 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f  .** The input to
15150 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65   [sqlite3_comple
15160 74 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20  te()] must be a 
15170 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a  zero-terminated.
15180 2a 2a 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e  ** UTF-8 string.
15190 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  .**.** The input
151a0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d   to [sqlite3_com
151b0 70 6c 65 74 65 31 36 28 29 5d 20 6d 75 73 74 20  plete16()] must 
151c0 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  be a zero-termin
151d0 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73  ated.** UTF-16 s
151e0 74 72 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 20  tring in native 
151f0 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53  byte order..*/.S
15200 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
15210 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63  lite3_complete(c
15220 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 29 3b  onst char *sql);
15230 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
15240 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
15250 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  16(const void *s
15260 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ql);../*.** CAPI
15270 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41  3REF: Register A
15280 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e   Callback To Han
15290 64 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20  dle SQLITE_BUSY 
152a0 45 72 72 6f 72 73 20 7b 48 31 32 33 31 30 7d 20  Errors {H12310} 
152b0 3c 53 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S40400>.**.** T
152c0 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
152d0 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63   a callback func
152e0 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20  tion that might 
152f0 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65  be invoked whene
15300 76 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70  ver.** an attemp
15310 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65  t is made to ope
15320 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62  n a database tab
15330 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20  le that another 
15340 74 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f  thread.** or pro
15350 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e  cess has locked.
15360 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75  .**.** If the bu
15370 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e  sy callback is N
15380 55 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54  ULL, then [SQLIT
15390 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49  E_BUSY] or [SQLI
153a0 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44  TE_IOERR_BLOCKED
153b0 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  ].** is returned
153c0 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 6f   immediately upo
153d0 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74  n encountering t
153e0 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20  he lock. If the 
153f0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  busy callback.**
15400 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
15410 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  en the callback 
15420 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20  will be invoked 
15430 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e  with two argumen
15440 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ts..**.** The fi
15450 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
15460 74 68 65 20 68 61 6e 64 6c 65 72 20 69 73 20 61  the handler is a
15470 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69   copy of the voi
15480 64 2a 20 70 6f 69 6e 74 65 72 20 77 68 69 63 68  d* pointer which
15490 0a 2a 2a 20 69 73 20 74 68 65 20 74 68 69 72 64  .** is the third
154a0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
154b0 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
154c0 72 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  r().  The second
154d0 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20   argument to.** 
154e0 74 68 65 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c  the handler call
154f0 62 61 63 6b 20 69 73 20 74 68 65 20 6e 75 6d 62  back is the numb
15500 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74  er of times that
15510 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
15520 72 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e  r has.** been in
15530 76 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c  voked for this l
15540 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 49  ocking event.  I
15550 66 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61  f the.** busy ca
15560 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30  llback returns 0
15570 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69  , then no additi
15580 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72  onal attempts ar
15590 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63  e made to.** acc
155a0 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
155b0 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53   and [SQLITE_BUS
155c0 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f  Y] or [SQLITE_IO
155d0 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20  ERR_BLOCKED] is 
155e0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20  returned..** If 
155f0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  the callback ret
15600 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
15610 68 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65  hen another atte
15620 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74  mpt.** is made t
15630 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62  o open the datab
15640 61 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20  ase for reading 
15650 61 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72 65  and the cycle re
15660 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  peats..**.** The
15670 20 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62   presence of a b
15680 75 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73  usy handler does
15690 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74   not guarantee t
156a0 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 69  hat it will be i
156b0 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74  nvoked.** when t
156c0 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e  here is lock con
156d0 74 65 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69  tention. If SQLi
156e0 74 65 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68  te determines th
156f0 61 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  at invoking the 
15700 62 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20  busy.** handler 
15710 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 20  could result in 
15720 61 20 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77  a deadlock, it w
15730 69 6c 6c 20 67 6f 20 61 68 65 61 64 20 61 6e 64  ill go ahead and
15740 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
15750 42 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c  BUSY].** or [SQL
15760 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
15770 44 5d 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e  D] instead of in
15780 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20  voking the busy 
15790 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73  handler..** Cons
157a0 69 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20  ider a scenario 
157b0 77 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73  where one proces
157c0 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72  s is holding a r
157d0 65 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a  ead lock that.**
157e0 20 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f   it is trying to
157f0 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65   promote to a re
15800 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a  served lock and.
15810 2a 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63  ** a second proc
15820 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  ess is holding a
15830 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74   reserved lock t
15840 68 61 74 20 69 74 20 69 73 20 74 72 79 69 6e 67  hat it is trying
15850 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  .** to promote t
15860 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  o an exclusive l
15870 6f 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20  ock.  The first 
15880 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70  process cannot p
15890 72 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73  roceed.** becaus
158a0 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20  e it is blocked 
158b0 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e  by the second an
158c0 64 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f  d the second pro
158d0 63 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70  cess cannot.** p
158e0 72 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 69  roceed because i
158f0 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
15900 74 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 62  the first.  If b
15910 6f 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a  oth processes.**
15920 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   invoke the busy
15930 20 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68   handlers, neith
15940 65 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79  er will make any
15950 20 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72   progress.  Ther
15960 65 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65  efore,.** SQLite
15970 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
15980 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66  _BUSY] for the f
15990 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f  irst process, ho
159a0 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a  ping that this.*
159b0 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68  * will induce th
159c0 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20  e first process 
159d0 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 72  to release its r
159e0 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c  ead lock and all
159f0 6f 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  ow.** the second
15a00 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63   process to proc
15a10 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  eed..**.** The d
15a20 65 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c  efault busy call
15a30 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a  back is NULL..**
15a40 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
15a50 42 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63  BUSY] error is c
15a60 6f 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c  onverted to [SQL
15a70 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
15a80 44 5d 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74  D].** when SQLit
15a90 65 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64  e is in the midd
15aa0 6c 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72  le of a large tr
15ab0 61 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20  ansaction where 
15ac0 61 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67  all the.** chang
15ad0 65 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20  es will not fit 
15ae0 69 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  into the in-memo
15af0 72 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74  ry cache.  SQLit
15b00 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64  e will.** alread
15b10 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45  y hold a RESERVE
15b20 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  D lock on the da
15b30 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74  tabase file, but
15b40 20 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20   it needs.** to 
15b50 70 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63  promote this loc
15b60 6b 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73  k to EXCLUSIVE s
15b70 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 70  o that it can sp
15b80 69 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67  ill cache.** pag
15b90 65 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  es into the data
15ba0 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75  base file withou
15bb0 74 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72  t harm to concur
15bc0 72 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e  rent.** readers.
15bd0 20 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c    If it is unabl
15be0 65 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65  e to promote the
15bf0 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20   lock, then the 
15c00 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63  in-memory.** cac
15c10 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20  he will be left 
15c20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
15c30 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20  nt state and so 
15c40 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64  the error.** cod
15c50 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72  e is promoted fr
15c60 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c  om the relativel
15c70 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45  y benign [SQLITE
15c80 5f 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65  _BUSY] to.** the
15c90 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51   more severe [SQ
15ca0 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
15cb0 45 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72  ED].  This error
15cc0 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a   code promotion.
15cd0 2a 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74  ** forces an aut
15ce0 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20  omatic rollback 
15cf0 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20  of the changes. 
15d00 20 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68   See the.** <a h
15d10 72 65 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 69  ref="/cvstrac/wi
15d20 6b 69 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46  ki?p=CorruptionF
15d30 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f  ollowingBusyErro
15d40 72 22 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f  r">.** Corruptio
15d50 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72  nFollowingBusyEr
15d60 72 6f 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67  ror</a> wiki pag
15d70 65 20 66 6f 72 20 61 20 64 69 73 63 75 73 73 69  e for a discussi
15d80 6f 6e 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69  on of why.** thi
15d90 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a  s is important..
15da0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20  **.** There can 
15db0 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65  only be a single
15dc0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 65   busy handler de
15dd0 66 69 6e 65 64 20 66 6f 72 20 65 61 63 68 0a 2a  fined for each.*
15de0 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
15df0 65 63 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e  ection].  Settin
15e00 67 20 61 20 6e 65 77 20 62 75 73 79 20 68 61 6e  g a new busy han
15e10 64 6c 65 72 20 63 6c 65 61 72 73 20 61 6e 79 0a  dler clears any.
15e20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65  ** previously se
15e30 74 20 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65  t handler.  Note
15e40 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73   that calling [s
15e50 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
15e60 6f 75 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61  out()].** will a
15e70 6c 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72  lso set or clear
15e80 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
15e90 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73  r..**.** The bus
15ea0 79 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c  y callback shoul
15eb0 64 20 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61  d not take any a
15ec0 63 74 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64  ctions which mod
15ed0 69 66 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  ify the.** datab
15ee0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
15ef0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
15f00 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41  busy handler.  A
15f10 6e 79 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a  ny such actions.
15f20 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64  ** result in und
15f30 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e  efined behavior.
15f40 0a 2a 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d  .** .** Requirem
15f50 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31  ents:.** [H12311
15f60 5d 20 5b 48 31 32 33 31 32 5d 20 5b 48 31 32 33  ] [H12312] [H123
15f70 31 34 5d 20 5b 48 31 32 33 31 36 5d 20 5b 48 31  14] [H12316] [H1
15f80 32 33 31 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75  2318].**.** A bu
15f90 73 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20  sy handler must 
15fa0 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61  not close the da
15fb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
15fc0 6e 0a 2a 2a 20 6f 72 20 5b 70 72 65 70 61 72 65  n.** or [prepare
15fd0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61  d statement] tha
15fe0 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75  t invoked the bu
15ff0 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53  sy handler..*/.S
16000 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
16010 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
16020 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  er(sqlite3*, int
16030 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20  (*)(void*,int), 
16040 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  void*);../*.** C
16050 41 50 49 33 52 45 46 3a 20 53 65 74 20 41 20 42  API3REF: Set A B
16060 75 73 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32  usy Timeout {H12
16070 33 34 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a  340} <S40410>.**
16080 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16090 20 73 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33   sets a [sqlite3
160a0 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20  _busy_handler | 
160b0 62 75 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68  busy handler] th
160c0 61 74 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72  at sleeps.** for
160d0 20 61 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f   a specified amo
160e0 75 6e 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e  unt of time when
160f0 20 61 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b   a table is lock
16100 65 64 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72  ed.  The handler
16110 0a 2a 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d  .** will sleep m
16120 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e  ultiple times un
16130 74 69 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73  til at least "ms
16140 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f  " milliseconds o
16150 66 20 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61  f sleeping.** ha
16160 76 65 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20  ve accumulated. 
16170 7b 48 31 32 33 34 33 7d 20 41 66 74 65 72 20 22  {H12343} After "
16180 6d 73 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  ms" milliseconds
16190 20 6f 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a   of sleeping,.**
161a0 20 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74   the handler ret
161b0 75 72 6e 73 20 30 20 77 68 69 63 68 20 63 61 75  urns 0 which cau
161c0 73 65 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ses [sqlite3_ste
161d0 70 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a  p()] to return.*
161e0 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  * [SQLITE_BUSY] 
161f0 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  or [SQLITE_IOERR
16200 5f 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a  _BLOCKED]..**.**
16210 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f   Calling this ro
16220 75 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72  utine with an ar
16230 67 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e  gument less than
16240 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72   or equal to zer
16250 6f 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61  o.** turns off a
16260 6c 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73  ll busy handlers
16270 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61  ..**.** There ca
16280 6e 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67  n only be a sing
16290 6c 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  le busy handler 
162a0 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72  for a particular
162b0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f  .** [database co
162c0 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e  nnection] any an
162d0 79 20 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20  y given moment. 
162e0 20 49 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79   If another busy
162f0 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20   handler.** was 
16300 64 65 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20  defined  (using 
16310 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61  [sqlite3_busy_ha
16320 6e 64 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20  ndler()]) prior 
16330 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68  to calling.** th
16340 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74  is routine, that
16350 20 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64   other busy hand
16360 6c 65 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a  ler is cleared..
16370 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
16380 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20  ts:.** [H12341] 
16390 5b 48 31 32 33 34 33 5d 20 5b 48 31 32 33 34 34  [H12343] [H12344
163a0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
163b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
163c0 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33  _timeout(sqlite3
163d0 2a 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a  *, int ms);../*.
163e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e  ** CAPI3REF: Con
163f0 76 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65  venience Routine
16400 73 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75  s For Running Qu
16410 65 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c  eries {H12370} <
16420 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65  S10000>.**.** De
16430 66 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72  finition: A <b>r
16440 65 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20  esult table</b> 
16450 69 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73  is memory data s
16460 74 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64  tructure created
16470 20 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69   by the.** [sqli
16480 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d  te3_get_table()]
16490 20 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72   interface.  A r
164a0 65 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f  esult table reco
164b0 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c  rds the.** compl
164c0 65 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74  ete query result
164d0 73 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f  s from one or mo
164e0 72 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a  re queries..**.*
164f0 2a 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63  * The table conc
16500 65 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e  eptually has a n
16510 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e  umber of rows an
16520 64 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a  d columns.  But.
16530 2a 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73  ** these numbers
16540 20 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66   are not part of
16550 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c   the result tabl
16560 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65  e itself.  These
16570 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20  .** numbers are 
16580 6f 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74  obtained separat
16590 65 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74  ely.  Let N be t
165a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
165b0 73 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68  s.** and M be th
165c0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
165d0 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73  mns..**.** A res
165e0 75 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20  ult table is an 
165f0 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72  array of pointer
16600 73 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  s to zero-termin
16610 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e  ated UTF-8 strin
16620 67 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  gs..** There are
16630 20 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74   (N+1)*M element
16640 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20  s in the array. 
16650 20 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69   The first M poi
16660 6e 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74  nters point.** t
16670 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  o zero-terminate
16680 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20  d strings that  
16690 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65  contain the name
166a0 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73  s of the columns
166b0 2e 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69  ..** The remaini
166c0 6e 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70  ng entries all p
166d0 6f 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65  oint to query re
166e0 73 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c  sults.  NULL val
166f0 75 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e  ues result.** in
16700 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20   NULL pointers. 
16710 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65   All other value
16720 73 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55  s are in their U
16730 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  TF-8 zero-termin
16740 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72  ated.** string r
16750 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73  epresentation as
16760 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
16770 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
16780 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65  t()]..**.** A re
16790 73 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74  sult table might
167a0 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20   consist of one 
167b0 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61  or more memory a
167c0 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49  llocations..** I
167d0 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f  t is not safe to
167e0 20 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74   pass a result t
167f0 61 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f  able directly to
16800 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
16810 5d 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74  ]..** A result t
16820 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64  able should be d
16830 65 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67  eallocated using
16840 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74   [sqlite3_free_t
16850 61 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41  able()]..**.** A
16860 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20  s an example of 
16870 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
16880 20 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65   format, suppose
16890 20 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a   a query result.
168a0 2a 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  ** is as follows
168b0 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
168c0 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20  ote><pre>.**    
168d0 20 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20      Name        
168e0 7c 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20  | Age.**        
168f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16900 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20  -------.**      
16910 20 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20    Alice       | 
16920 34 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62  43.**        Bob
16930 20 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a           | 28.**
16940 20 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20          Cindy   
16950 20 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72      | 21.** </pr
16960 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
16970 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
16980 74 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32  two column (M==2
16990 29 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73  ) and three rows
169a0 20 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74   (N==3).  Thus t
169b0 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62  he.** result tab
169c0 6c 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73  le has 8 entries
169d0 2e 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72  .  Suppose the r
169e0 65 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73  esult table is s
169f0 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61  tored.** in an a
16a00 72 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73  rray names azRes
16a10 75 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73  ult.  Then azRes
16a20 75 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63  ult holds this c
16a30 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62  ontent:.**.** <b
16a40 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
16a50 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
16a60 6c 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d  lt&#91;0] = "Nam
16a70 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a  e";.**        az
16a80 52 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20  Result&#91;1] = 
16a90 22 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20  "Age";.**       
16aa0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d   azResult&#91;2]
16ab0 20 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20   = "Alice";.**  
16ac0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
16ad0 39 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a  91;3] = "43";.**
16ae0 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
16af0 26 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b  &#91;4] = "Bob";
16b00 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
16b10 75 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38  ult&#91;5] = "28
16b20 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
16b30 65 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22  esult&#91;6] = "
16b40 43 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20  Cindy";.**      
16b50 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37    azResult&#91;7
16b60 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70  ] = "21";.** </p
16b70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
16b80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
16b90 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66  e3_get_table() f
16ba0 75 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65  unction evaluate
16bb0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  s one or more.**
16bc0 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72   semicolon-separ
16bd0 61 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  ated SQL stateme
16be0 6e 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d  nts in the zero-
16bf0 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
16c00 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74  .** string of it
16c10 73 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  s 2nd parameter.
16c20 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72    It returns a r
16c30 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74  esult table to t
16c40 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69  he.** pointer gi
16c50 76 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70  ven in its 3rd p
16c60 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  arameter..**.** 
16c70 41 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e  After the callin
16c80 67 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66  g function has f
16c90 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68  inished using th
16ca0 65 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f  e result, it sho
16cb0 75 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20  uld.** pass the 
16cc0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  pointer to the r
16cd0 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73  esult table to s
16ce0 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
16cf0 65 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a  e() in order to.
16d00 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d  ** release the m
16d10 65 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d  emory that was m
16d20 61 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73  alloced.  Becaus
16d30 65 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65  e of the way the
16d40 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  .** [sqlite3_mal
16d50 6c 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77  loc()] happens w
16d60 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65  ithin sqlite3_ge
16d70 74 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63  t_table(), the c
16d80 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
16d90 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20  on must not try 
16da0 74 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33  to call [sqlite3
16db0 5f 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c  _free()] directl
16dc0 79 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c  y.  Only.** [sql
16dd0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
16de0 29 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65  )] is able to re
16df0 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79  lease the memory
16e00 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61   properly and sa
16e10 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fely..**.** The 
16e20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
16e30 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  e() interface is
16e40 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
16e50 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64  a wrapper around
16e60 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65  .** [sqlite3_exe
16e70 63 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74  c()].  The sqlit
16e80 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72  e3_get_table() r
16e90 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20  outine does not 
16ea0 68 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74  have access.** t
16eb0 6f 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64  o any internal d
16ec0 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f  ata structures o
16ed0 66 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73  f SQLite.  It us
16ee0 65 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c  es only the publ
16ef0 69 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ic.** interface 
16f00 64 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41  defined here.  A
16f10 73 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c  s a consequence,
16f20 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63   errors that occ
16f30 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61  ur in the.** wra
16f40 70 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69  pper layer outsi
16f50 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e  de of the intern
16f60 61 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  al [sqlite3_exec
16f70 28 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74  ()] call are not
16f80 0a 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e  .** reflected in
16f90 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
16fa0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72  s to [sqlite3_er
16fb0 72 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c  rcode()] or [sql
16fc0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a  ite3_errmsg()]..
16fd0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
16fe0 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20  ts:.** [H12371] 
16ff0 5b 48 31 32 33 37 33 5d 20 5b 48 31 32 33 37 34  [H12373] [H12374
17000 5d 20 5b 48 31 32 33 37 36 5d 20 5b 48 31 32 33  ] [H12376] [H123
17010 37 39 5d 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a  79] [H12382].*/.
17020 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
17030 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
17040 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
17050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20            /* An 
17060 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f  open database */
17070 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
17080 53 71 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20  Sql,     /* SQL 
17090 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20  to be evaluated 
170a0 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a  */.  char ***paz
170b0 52 65 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65  Result,    /* Re
170c0 73 75 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65  sults of the que
170d0 72 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52  ry */.  int *pnR
170e0 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ow,           /*
170f0 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c   Number of resul
17100 74 20 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68  t rows written h
17110 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ere */.  int *pn
17120 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f  Column,        /
17130 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75  * Number of resu
17140 6c 74 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74  lt columns writt
17150 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61  en here */.  cha
17160 72 20 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20  r **pzErrmsg    
17170 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20     /* Error msg 
17180 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
17190 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
171a0 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  id sqlite3_free_
171b0 74 61 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73  table(char **res
171c0 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ult);../*.** CAP
171d0 49 33 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64  I3REF: Formatted
171e0 20 53 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67   String Printing
171f0 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34   Functions {H174
17200 30 30 7d 20 3c 53 37 30 30 30 30 3e 3c 53 32 30  00} <S70000><S20
17210 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  000>.**.** These
17220 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 77 6f   routines are wo
17230 72 6b 2d 61 6c 69 6b 65 73 20 6f 66 20 74 68 65  rk-alikes of the
17240 20 22 70 72 69 6e 74 66 28 29 22 20 66 61 6d 69   "printf()" fami
17250 6c 79 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a  ly of functions.
17260 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e  ** from the stan
17270 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a  dard C library..
17280 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
17290 33 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20  3_mprintf() and 
172a0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66  sqlite3_vmprintf
172b0 28 29 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74  () routines writ
172c0 65 20 74 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c  e their.** resul
172d0 74 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  ts into memory o
172e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
172f0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
17300 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20  .** The strings 
17310 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73  returned by thes
17320 65 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 20 73  e two routines s
17330 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65  hould be.** rele
17340 61 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  ased by [sqlite3
17350 5f 66 72 65 65 28 29 5d 2e 20 20 42 6f 74 68 20  _free()].  Both 
17360 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
17370 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  a.** NULL pointe
17380 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61  r if [sqlite3_ma
17390 6c 6c 6f 63 28 29 5d 20 69 73 20 75 6e 61 62 6c  lloc()] is unabl
173a0 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e  e to allocate en
173b0 6f 75 67 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74  ough.** memory t
173c0 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c  o hold the resul
173d0 74 69 6e 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  ting string..**.
173e0 2a 2a 20 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e  ** In sqlite3_sn
173f0 70 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65  printf() routine
17400 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 22   is similar to "
17410 73 6e 70 72 69 6e 74 66 28 29 22 20 66 72 6f 6d  snprintf()" from
17420 0a 2a 2a 20 74 68 65 20 73 74 61 6e 64 61 72 64  .** the standard
17430 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65   C library.  The
17440 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74   result is writt
17450 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62  en into the.** b
17460 75 66 66 65 72 20 73 75 70 70 6c 69 65 64 20 61  uffer supplied a
17470 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  s the second par
17480 61 6d 65 74 65 72 20 77 68 6f 73 65 20 73 69 7a  ameter whose siz
17490 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a  e is given by.**
174a0 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   the first param
174b0 65 74 65 72 2e 20 4e 6f 74 65 20 74 68 61 74 20  eter. Note that 
174c0 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65  the order of the
174d0 0a 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61  .** first two pa
174e0 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65  rameters is reve
174f0 72 73 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e  rsed from snprin
17500 74 66 28 29 2e 20 20 54 68 69 73 20 69 73 20 61  tf().  This is a
17510 6e 0a 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c 20  n.** historical 
17520 61 63 63 69 64 65 6e 74 20 74 68 61 74 20 63 61  accident that ca
17530 6e 6e 6f 74 20 62 65 20 66 69 78 65 64 20 77 69  nnot be fixed wi
17540 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a  thout breaking.*
17550 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  * backwards comp
17560 61 74 69 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65  atibility.  Note
17570 20 61 6c 73 6f 20 74 68 61 74 20 73 71 6c 69 74   also that sqlit
17580 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a  e3_snprintf().**
17590 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
175a0 65 72 20 74 6f 20 69 74 73 20 62 75 66 66 65 72  er to its buffer
175b0 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20   instead of the 
175c0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61  number of.** cha
175d0 72 61 63 74 65 72 73 20 61 63 74 75 61 6c 6c 79  racters actually
175e0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
175f0 65 20 62 75 66 66 65 72 2e 20 20 57 65 20 61 64  e buffer.  We ad
17600 6d 69 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  mit that.** the 
17610 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
17620 74 65 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75  ters written wou
17630 6c 64 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65  ld be a more use
17640 66 75 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  ful return.** va
17650 6c 75 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f  lue but we canno
17660 74 20 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70  t change the imp
17670 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73  lementation of s
17680 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
17690 29 0a 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74  ).** now without
176a0 20 62 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74   breaking compat
176b0 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41  ibility..**.** A
176c0 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75  s long as the bu
176d0 66 66 65 72 20 73 69 7a 65 20 69 73 20 67 72 65  ffer size is gre
176e0 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20  ater than zero, 
176f0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
17700 28 29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73  ().** guarantees
17710 20 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72   that the buffer
17720 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d   is always zero-
17730 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65  terminated.  The
17740 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65   first.** parame
17750 74 65 72 20 22 6e 22 20 69 73 20 74 68 65 20 74  ter "n" is the t
17760 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65  otal size of the
17770 20 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 69   buffer, includi
17780 6e 67 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20  ng space for.** 
17790 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  the zero termina
177a0 74 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e  tor.  So the lon
177b0 67 65 73 74 20 73 74 72 69 6e 67 20 74 68 61 74  gest string that
177c0 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65   can be complete
177d0 6c 79 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 69  ly.** written wi
177e0 6c 6c 20 62 65 20 6e 2d 31 20 63 68 61 72 61 63  ll be n-1 charac
177f0 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ters..**.** Thes
17800 65 20 72 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69  e routines all i
17810 6d 70 6c 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64  mplement some ad
17820 64 69 74 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74  ditional formatt
17830 69 6e 67 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74  ing.** options t
17840 68 61 74 20 61 72 65 20 75 73 65 66 75 6c 20 66  hat are useful f
17850 6f 72 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20  or constructing 
17860 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  SQL statements..
17870 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 75 73  ** All of the us
17880 75 61 6c 20 70 72 69 6e 74 66 28 29 20 66 6f 72  ual printf() for
17890 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20  matting options 
178a0 61 70 70 6c 79 2e 20 20 49 6e 20 61 64 64 69 74  apply.  In addit
178b0 69 6f 6e 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73  ion, there.** is
178c0 20 61 72 65 20 22 25 71 22 2c 20 22 25 51 22 2c   are "%q", "%Q",
178d0 20 61 6e 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e   and "%z" option
178e0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20  s..**.** The %q 
178f0 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b  option works lik
17900 65 20 25 73 20 69 6e 20 74 68 61 74 20 69 74 20  e %s in that it 
17910 73 75 62 73 74 69 74 75 74 65 73 20 61 20 6e 75  substitutes a nu
17920 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a  ll-terminated.**
17930 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65   string from the
17940 20 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20   argument list. 
17950 20 42 75 74 20 25 71 20 61 6c 73 6f 20 64 6f 75   But %q also dou
17960 62 6c 65 73 20 65 76 65 72 79 20 27 5c 27 27 20  bles every '\'' 
17970 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71  character..** %q
17980 20 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72   is designed for
17990 20 75 73 65 20 69 6e 73 69 64 65 20 61 20 73 74   use inside a st
179a0 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 20 20 42  ring literal.  B
179b0 79 20 64 6f 75 62 6c 69 6e 67 20 65 61 63 68 20  y doubling each 
179c0 27 5c 27 27 0a 2a 2a 20 63 68 61 72 61 63 74 65  '\''.** characte
179d0 72 20 69 74 20 65 73 63 61 70 65 73 20 74 68 61  r it escapes tha
179e0 74 20 63 68 61 72 61 63 74 65 72 20 61 6e 64 20  t character and 
179f0 61 6c 6c 6f 77 73 20 69 74 20 74 6f 20 62 65 20  allows it to be 
17a00 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a  inserted into.**
17a10 20 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a   the string..**.
17a20 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ** For example, 
17a30 61 73 73 75 6d 65 20 74 68 65 20 73 74 72 69 6e  assume the strin
17a40 67 20 76 61 72 69 61 62 6c 65 20 7a 54 65 78 74  g variable zText
17a50 20 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61   contains text a
17a60 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
17a70 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72   <blockquote><pr
17a80 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65  e>.**  char *zTe
17a90 78 74 20 3d 20 22 49 74 27 73 20 61 20 68 61 70  xt = "It's a hap
17aa0 70 79 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70  py day!";.** </p
17ab0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
17ac0 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75  .**.** One can u
17ad0 73 65 20 74 68 69 73 20 74 65 78 74 20 69 6e 20  se this text in 
17ae0 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
17af0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
17b00 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c  ** <blockquote><
17b10 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a  pre>.**  char *z
17b20 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  SQL = sqlite3_mp
17b30 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e  rintf("INSERT IN
17b40 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28  TO table VALUES(
17b50 27 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a  '%q')", zText);.
17b60 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63  **  sqlite3_exec
17b70 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c  (db, zSQL, 0, 0,
17b80 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33   0);.**  sqlite3
17b90 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20  _free(zSQL);.** 
17ba0 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
17bb0 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73  te>.**.** Becaus
17bc0 65 20 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20  e the %q format 
17bd0 73 74 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20  string is used, 
17be0 74 68 65 20 27 5c 27 27 20 63 68 61 72 61 63 74  the '\'' charact
17bf0 65 72 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69  er in zText.** i
17c00 73 20 65 73 63 61 70 65 64 20 61 6e 64 20 74 68  s escaped and th
17c10 65 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20  e SQL generated 
17c20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
17c30 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
17c40 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52  ><pre>.**  INSER
17c50 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41  T INTO table1 VA
17c60 4c 55 45 53 28 27 49 74 27 27 73 20 61 20 68 61  LUES('It''s a ha
17c70 70 70 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f  ppy day!').** </
17c80 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
17c90 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  >.**.** This is 
17ca0 63 6f 72 72 65 63 74 2e 20 20 48 61 64 20 77 65  correct.  Had we
17cb0 20 75 73 65 64 20 25 73 20 69 6e 73 74 65 61 64   used %s instead
17cc0 20 6f 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65   of %q, the gene
17cd0 72 61 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75  rated SQL.** wou
17ce0 6c 64 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c  ld have looked l
17cf0 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
17d00 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
17d10 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54  >.**  INSERT INT
17d20 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28  O table1 VALUES(
17d30 27 49 74 27 73 20 61 20 68 61 70 70 79 20 64 61  'It's a happy da
17d40 79 21 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  y!');.** </pre><
17d50 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
17d60 2a 2a 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65  ** This second e
17d70 78 61 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c  xample is an SQL
17d80 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20   syntax error.  
17d90 41 73 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c  As a general rul
17da0 65 20 79 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20  e you should.** 
17db0 61 6c 77 61 79 73 20 75 73 65 20 25 71 20 69 6e  always use %q in
17dc0 73 74 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e  stead of %s when
17dd0 20 69 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20   inserting text 
17de0 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69  into a string li
17df0 74 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  teral..**.** The
17e00 20 25 51 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73   %Q option works
17e10 20 6c 69 6b 65 20 25 71 20 65 78 63 65 70 74 20   like %q except 
17e20 69 74 20 61 6c 73 6f 20 61 64 64 73 20 73 69 6e  it also adds sin
17e30 67 6c 65 20 71 75 6f 74 65 73 20 61 72 6f 75 6e  gle quotes aroun
17e40 64 0a 2a 2a 20 74 68 65 20 6f 75 74 73 69 64 65  d.** the outside
17e50 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 73 74   of the total st
17e60 72 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61  ring.  Additiona
17e70 6c 6c 79 2c 20 69 66 20 74 68 65 20 70 61 72 61  lly, if the para
17e80 6d 65 74 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20  meter in the.** 
17e90 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 69 73  argument list is
17ea0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
17eb0 20 25 51 20 73 75 62 73 74 69 74 75 74 65 73 20   %Q substitutes 
17ec0 74 68 65 20 74 65 78 74 20 22 4e 55 4c 4c 22 20  the text "NULL" 
17ed0 28 77 69 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67  (without.** sing
17ee0 6c 65 20 71 75 6f 74 65 73 29 20 69 6e 20 70 6c  le quotes) in pl
17ef0 61 63 65 20 6f 66 20 74 68 65 20 25 51 20 6f 70  ace of the %Q op
17f00 74 69 6f 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65  tion.  So, for e
17f10 78 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c  xample, one coul
17f20 64 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c  d say:.**.** <bl
17f30 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
17f40 2a 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20  *  char *zSQL = 
17f50 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
17f60 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62  "INSERT INTO tab
17f70 6c 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20  le VALUES(%Q)", 
17f80 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69  zText);.**  sqli
17f90 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51  te3_exec(db, zSQ
17fa0 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20  L, 0, 0, 0);.** 
17fb0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53   sqlite3_free(zS
17fc0 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f  QL);.** </pre></
17fd0 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a  blockquote>.**.*
17fe0 2a 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65  * The code above
17ff0 20 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63   will render a c
18000 6f 72 72 65 63 74 20 53 51 4c 20 73 74 61 74 65  orrect SQL state
18010 6d 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c  ment in the zSQL
18020 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65  .** variable eve
18030 6e 20 69 66 20 74 68 65 20 7a 54 65 78 74 20 76  n if the zText v
18040 61 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c  ariable is a NUL
18050 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
18060 20 54 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74   The "%z" format
18070 74 69 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b  ting option work
18080 73 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22  s exactly like "
18090 25 73 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20  %s" with the.** 
180a0 61 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66  addition that af
180b0 74 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68  ter the string h
180c0 61 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64  as been read and
180d0 20 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20   copied into.** 
180e0 74 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c  the result, [sql
180f0 69 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20  ite3_free()] is 
18100 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e  called on the in
18110 70 75 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44  put string. {END
18120 7d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  }.**.** Requirem
18130 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33  ents:.** [H17403
18140 5d 20 5b 48 31 37 34 30 36 5d 20 5b 48 31 37 34  ] [H17406] [H174
18150 30 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  07].*/.SQLITE_AP
18160 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
18170 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  mprintf(const ch
18180 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  ar*,...);.SQLITE
18190 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74  _API char *sqlit
181a0 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73  e3_vmprintf(cons
181b0 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74  t char*, va_list
181c0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  );.SQLITE_API ch
181d0 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72  ar *sqlite3_snpr
181e0 69 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63  intf(int,char*,c
181f0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
18200 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
18210 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61  F: Memory Alloca
18220 74 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b  tion Subsystem {
18230 48 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e  H17300} <S20000>
18240 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74  .**.** The SQLit
18250 65 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65  e core  uses the
18260 73 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65  se three routine
18270 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73  s for all of its
18280 20 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c   own.** internal
18290 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
182a0 6f 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22  on needs. "Core"
182b0 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73   in the previous
182c0 20 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65   sentence.** doe
182d0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70  s not include op
182e0 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73  erating-system s
182f0 70 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c  pecific VFS impl
18300 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65  ementation.  The
18310 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20  .** Windows VFS 
18320 75 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c  uses native mall
18330 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20  oc() and free() 
18340 66 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69  for some operati
18350 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ons..**.** The s
18360 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
18370 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
18380 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62  a pointer to a b
18390 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72  lock.** of memor
183a0 79 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74  y at least N byt
183b0 65 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68  es in length, wh
183c0 65 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72  ere N is the par
183d0 61 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71  ameter..** If sq
183e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69  lite3_malloc() i
183f0 73 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61  s unable to obta
18400 69 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72  in sufficient fr
18410 65 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74  ee.** memory, it
18420 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20   returns a NULL 
18430 70 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65  pointer.  If the
18440 20 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a   parameter N to.
18450 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
18460 63 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e  c() is zero or n
18470 65 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c  egative then sql
18480 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65  ite3_malloc() re
18490 74 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20  turns.** a NULL 
184a0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43  pointer..**.** C
184b0 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66  alling sqlite3_f
184c0 72 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69  ree() with a poi
184d0 6e 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20  nter previously 
184e0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73  returned.** by s
184f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
18500 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  or sqlite3_reall
18510 6f 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68  oc() releases th
18520 61 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20  at memory so.** 
18530 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65  that it might be
18540 20 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71   reused.  The sq
18550 6c 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75  lite3_free() rou
18560 74 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d  tine is.** a no-
18570 6f 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20  op if is called 
18580 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  with a NULL poin
18590 74 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20  ter.  Passing a 
185a0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  NULL pointer.** 
185b0 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  to sqlite3_free(
185c0 29 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20  ) is harmless.  
185d0 41 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65  After being free
185e0 64 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f  d, memory.** sho
185f0 75 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72  uld neither be r
18600 65 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e  ead nor written.
18610 20 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70    Even reading p
18620 72 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a  reviously freed.
18630 2a 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20  ** memory might 
18640 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d  result in a segm
18650 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f  entation fault o
18660 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65  r other severe e
18670 72 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20  rror..** Memory 
18680 63 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65  corruption, a se
18690 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74  gmentation fault
186a0 2c 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72  , or other sever
186b0 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74  e error.** might
186c0 20 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74   result if sqlit
186d0 65 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c  e3_free() is cal
186e0 6c 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e  led with a non-N
186f0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74  ULL pointer that
18700 0a 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61  .** was not obta
18710 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
18720 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71  3_malloc() or sq
18730 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e  lite3_realloc().
18740 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
18750 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74  e3_realloc() int
18760 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20  erface attempts 
18770 74 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70  to resize a.** p
18780 72 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  rior memory allo
18790 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20  cation to be at 
187a0 6c 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77  least N bytes, w
187b0 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a  here N is the.**
187c0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
187d0 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61  r.  The memory a
187e0 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20  llocation to be 
187f0 72 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66  resized is the f
18800 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65  irst.** paramete
18810 72 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74  r.  If the first
18820 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
18830 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a  lite3_realloc().
18840 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  ** is a NULL poi
18850 6e 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65  nter then its be
18860 68 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69  havior is identi
18870 63 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a  cal to calling.*
18880 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  * sqlite3_malloc
18890 28 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74  (N) where N is t
188a0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
188b0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
188c0 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20  ealloc()..** If 
188d0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
188e0 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
188f0 72 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72  realloc() is zer
18900 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65  o or.** negative
18910 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69   then the behavi
18920 6f 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68  or is exactly th
18930 65 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e  e same as callin
18940 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65  g.** sqlite3_fre
18950 65 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20  e(P) where P is 
18960 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  the first parame
18970 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
18980 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c  ealloc()..** sql
18990 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72  ite3_realloc() r
189a0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
189b0 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   to a memory all
189c0 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74  ocation.** of at
189d0 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69   least N bytes i
189e0 6e 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69  n size or NULL i
189f0 66 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d  f sufficient mem
18a00 6f 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62  ory is unavailab
18a10 6c 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74  le..** If M is t
18a20 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70  he size of the p
18a30 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  rior allocation,
18a40 20 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62   then min(N,M) b
18a50 79 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70  ytes.** of the p
18a60 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  rior allocation 
18a70 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  are copied into 
18a80 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
18a90 20 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64   buffer returned
18aa0 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72  .** by sqlite3_r
18ab0 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65  ealloc() and the
18ac0 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f   prior allocatio
18ad0 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49  n is freed..** I
18ae0 66 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  f sqlite3_reallo
18af0 63 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  c() returns NULL
18b00 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72  , then the prior
18b10 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69   allocation.** i
18b20 73 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a  s not freed..**.
18b30 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65  ** The memory re
18b40 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
18b50 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73  3_malloc() and s
18b60 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
18b70 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c  .** is always al
18b80 69 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73  igned to at leas
18b90 74 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e  t an 8 byte boun
18ba0 64 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  dary. {END}.**.*
18bb0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d  * The default im
18bc0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
18bd0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
18be0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
18bf0 75 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c  uses.** the mall
18c00 6f 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20  oc(), realloc() 
18c10 61 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69  and free() provi
18c20 64 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64  ded by the stand
18c30 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a  ard C library..*
18c40 2a 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76  * {H17382} Howev
18c50 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73  er, if SQLite is
18c60 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
18c70 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d  he.** SQLITE_MEM
18c80 4f 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c  ORY_SIZE=<i>NNN<
18c90 2f 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73  /i> C preprocess
18ca0 6f 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20  or macro (where 
18cb0 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73  <i>NNN</i>.** is
18cc0 20 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68   an integer), th
18cd0 65 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65  en SQLite create
18ce0 20 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20   a static array 
18cf0 6f 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c  of at least.** <
18d00 69 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20  i>NNN</i> bytes 
18d10 69 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73  in size and uses
18d20 20 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20   that array for 
18d30 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d  all of its dynam
18d40 69 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c  ic.** memory all
18d50 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b  ocation needs. {
18d60 45 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c  END}  Additional
18d70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
18d80 72 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79  r options.** may
18d90 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
18da0 75 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a  ure releases..**
18db0 0a 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65  .** In SQLite ve
18dc0 72 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20  rsion 3.5.0 and 
18dd0 33 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f  3.5.1, it was po
18de0 73 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65  ssible to define
18df0 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f  .** the SQLITE_O
18e00 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43  MIT_MEMORY_ALLOC
18e10 41 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c  ATION which woul
18e20 64 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c  d cause the buil
18e30 74 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  t-in.** implemen
18e40 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  tation of these 
18e50 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f  routines to be o
18e60 6d 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61  mitted.  That ca
18e70 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e  pability.** is n
18e80 6f 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65  o longer provide
18e90 64 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69  d.  Only built-i
18ea0 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  n memory allocat
18eb0 6f 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e  ors can be used.
18ec0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f  .**.** The Windo
18ed0 77 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20  ws OS interface 
18ee0 6c 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74  layer calls.** t
18ef0 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  he system malloc
18f00 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69  () and free() di
18f10 72 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76  rectly when conv
18f20 65 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61  erting.** filena
18f30 6d 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20  mes between the 
18f40 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75  UTF-8 encoding u
18f50 73 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a  sed by SQLite.**
18f60 20 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69   and whatever fi
18f70 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20  lename encoding 
18f80 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70  is used by the p
18f90 61 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77  articular Window
18fa0 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f  s.** installatio
18fb0 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63  n.  Memory alloc
18fc0 61 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65  ation errors are
18fd0 20 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a   detected, but.*
18fe0 2a 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72  * they are repor
18ff0 74 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c  ted back as [SQL
19000 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72  ITE_CANTOPEN] or
19010 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  .** [SQLITE_IOER
19020 52 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b  R] rather than [
19030 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a  SQLITE_NOMEM]..*
19040 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
19050 73 3a 0a 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b  s:.** [H17303] [
19060 48 31 37 33 30 34 5d 20 5b 48 31 37 33 30 35 5d  H17304] [H17305]
19070 20 5b 48 31 37 33 30 36 5d 20 5b 48 31 37 33 31   [H17306] [H1731
19080 30 5d 20 5b 48 31 37 33 31 32 5d 20 5b 48 31 37  0] [H17312] [H17
19090 33 31 35 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a  315] [H17318].**
190a0 20 5b 48 31 37 33 32 31 5d 20 5b 48 31 37 33 32   [H17321] [H1732
190b0 32 5d 20 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a  2] [H17323].**.*
190c0 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72  * The pointer ar
190d0 67 75 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69  guments to [sqli
190e0 74 65 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20  te3_free()] and 
190f0 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
19100 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  ()].** must be e
19110 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c  ither NULL or el
19120 73 65 20 70 6f 69 6e 74 65 72 73 20 6f 62 74 61  se pointers obta
19130 69 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f  ined from a prio
19140 72 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20  r.** invocation 
19150 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  of [sqlite3_mall
19160 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  oc()] or [sqlite
19170 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61  3_realloc()] tha
19180 74 20 68 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65  t have.** not ye
19190 74 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e  t been released.
191a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  .**.** The appli
191b0 63 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20  cation must not 
191c0 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e  read or write an
191d0 79 20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62  y part of.** a b
191e0 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61  lock of memory a
191f0 66 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e  fter it has been
19200 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a   released using.
19210 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  ** [sqlite3_free
19220 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
19230 72 65 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53  realloc()]..*/.S
19240 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
19250 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69  sqlite3_malloc(i
19260 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
19270 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65  void *sqlite3_re
19280 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74  alloc(void*, int
19290 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
192a0 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  id sqlite3_free(
192b0 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  void*);../*.** C
192c0 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20  API3REF: Memory 
192d0 41 6c 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73  Allocator Statis
192e0 74 69 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53  tics {H17370} <S
192f0 33 30 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c  30210>.**.** SQL
19300 69 74 65 20 70 72 6f 76 69 64 65 73 20 74 68 65  ite provides the
19310 73 65 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65  se two interface
19320 73 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20  s for reporting 
19330 6f 6e 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a  on the status.**
19340 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
19350 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c  _malloc()], [sql
19360 69 74 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e  ite3_free()], an
19370 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  d [sqlite3_reall
19380 6f 63 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65  oc()].** routine
19390 73 2c 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68  s, which form th
193a0 65 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72  e built-in memor
193b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
193c0 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65  system..**.** Re
193d0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
193e0 48 31 37 33 37 31 5d 20 5b 48 31 37 33 37 33 5d  H17371] [H17373]
193f0 20 5b 48 31 37 33 37 34 5d 20 5b 48 31 37 33 37   [H17374] [H1737
19400 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  5].*/.SQLITE_API
19410 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
19420 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
19430 65 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  ed(void);.SQLITE
19440 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74  _API sqlite3_int
19450 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  64 sqlite3_memor
19460 79 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20  y_highwater(int 
19470 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a  resetFlag);../*.
19480 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 73 65  ** CAPI3REF: Pse
19490 75 64 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65  udo-Random Numbe
194a0 72 20 47 65 6e 65 72 61 74 6f 72 20 7b 48 31 37  r Generator {H17
194b0 33 39 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a  390} <S20000>.**
194c0 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61  .** SQLite conta
194d0 69 6e 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69  ins a high-quali
194e0 74 79 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d  ty pseudo-random
194f0 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f   number generato
19500 72 20 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f  r (PRNG) used to
19510 0a 2a 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f  .** select rando
19520 6d 20 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44  m [ROWID | ROWID
19530 73 5d 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e  s] when insertin
19540 67 20 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e  g new records in
19550 74 6f 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a  to a table that.
19560 2a 2a 20 61 6c 72 65 61 64 79 20 75 73 65 73 20  ** already uses 
19570 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73  the largest poss
19580 69 62 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54  ible [ROWID].  T
19590 68 65 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20  he PRNG is also 
195a0 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20  used for.** the 
195b0 62 75 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28  build-in random(
195c0 29 20 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62  ) and randomblob
195d0 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  () SQL functions
195e0 2e 20 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  .  This interfac
195f0 65 20 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c  e allows.** appl
19600 69 63 61 74 69 6f 6e 73 20 74 6f 20 61 63 63 65  ications to acce
19610 73 73 20 74 68 65 20 73 61 6d 65 20 50 52 4e 47  ss the same PRNG
19620 20 66 6f 72 20 6f 74 68 65 72 20 70 75 72 70 6f   for other purpo
19630 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c  ses..**.** A cal
19640 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  l to this routin
19650 65 20 73 74 6f 72 65 73 20 4e 20 62 79 74 65 73  e stores N bytes
19660 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69   of randomness i
19670 6e 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a  nto buffer P..**
19680 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69  .** The first ti
19690 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  me this routine 
196a0 69 73 20 69 6e 76 6f 6b 65 64 20 28 65 69 74 68  is invoked (eith
196b0 65 72 20 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72  er internally or
196c0 20 62 79 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69   by.** the appli
196d0 63 61 74 69 6f 6e 29 20 74 68 65 20 50 52 4e 47  cation) the PRNG
196e0 20 69 73 20 73 65 65 64 65 64 20 75 73 69 6e 67   is seeded using
196f0 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61   randomness obta
19700 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ined.** from the
19710 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74   xRandomness met
19720 68 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61 75  hod of the defau
19730 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  lt [sqlite3_vfs]
19740 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61   object..** On a
19750 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e  ll subsequent in
19760 76 6f 63 61 74 69 6f 6e 73 2c 20 74 68 65 20 70  vocations, the p
19770 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73  seudo-randomness
19780 20 69 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a   is generated.**
19790 20 69 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20   internally and 
197a0 77 69 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65  without recourse
197b0 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
197c0 5f 76 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73  _vfs] xRandomnes
197d0 73 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a  s.** method..**.
197e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
197f0 0a 2a 2a 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a  .** [H17392].*/.
19800 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
19810 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
19820 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a  ss(int N, void *
19830 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  P);../*.** CAPI3
19840 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d  REF: Compile-Tim
19850 65 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e Authorization 
19860 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30  Callbacks {H1250
19870 30 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S70100>.**.*
19880 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
19890 65 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f  egisters a autho
198a0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77  rizer callback w
198b0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
198c0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f  .** [database co
198d0 6e 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c  nnection], suppl
198e0 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74  ied in the first
198f0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68   argument..** Th
19900 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
19910 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
19920 20 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   as SQL statemen
19930 74 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d  ts are being com
19940 70 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c  piled.** by [sql
19950 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
19960 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20  or its variants 
19970 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
19980 5f 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  _v2()],.** [sqli
19990 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
199a0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72   and [sqlite3_pr
199b0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20  epare16_v2()].  
199c0 41 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f  At various.** po
199d0 69 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20  ints during the 
199e0 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63  compilation proc
199f0 65 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73  ess, as logic is
19a00 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a   being created.*
19a10 2a 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72  * to perform var
19a20 69 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68  ious actions, th
19a30 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
19a40 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
19a50 20 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68   to.** see if th
19a60 6f 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20  ose actions are 
19a70 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75  allowed.  The au
19a80 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
19a90 6b 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75  k should.** retu
19aa0 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74  rn [SQLITE_OK] t
19ab0 6f 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69  o allow the acti
19ac0 6f 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f  on, [SQLITE_IGNO
19ad0 52 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20  RE] to disallow 
19ae0 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20  the.** specific 
19af0 61 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77  action but allow
19b00 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
19b10 6e 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74  nt to continue t
19b20 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64  o be.** compiled
19b30 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e  , or [SQLITE_DEN
19b40 59 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20  Y] to cause the 
19b50 65 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65  entire SQL state
19b60 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65  ment to be.** re
19b70 6a 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65  jected with an e
19b80 72 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75  rror.  If the au
19b90 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
19ba0 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79  k returns.** any
19bb0 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61   value other tha
19bc0 6e 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  n [SQLITE_IGNORE
19bd0 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20  ], [SQLITE_OK], 
19be0 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d  or [SQLITE_DENY]
19bf0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71  .** then the [sq
19c00 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
19c10 28 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e  ()] or equivalen
19c20 74 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67  t call that trig
19c30 67 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74  gered.** the aut
19c40 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69  horizer will fai
19c50 6c 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  l with an error 
19c60 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57  message..**.** W
19c70 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  hen the callback
19c80 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
19c90 5f 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73  _OK], that means
19ca0 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a   the operation.*
19cb0 2a 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f  * requested is o
19cc0 6b 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c  k.  When the cal
19cd0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53  lback returns [S
19ce0 51 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65  QLITE_DENY], the
19cf0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
19d00 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71  pare_v2()] or eq
19d10 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68  uivalent call th
19d20 61 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65  at triggered the
19d30 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77  .** authorizer w
19d40 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e  ill fail with an
19d50 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65   error message e
19d60 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a  xplaining that.*
19d70 2a 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69  * access is deni
19d80 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ed. .**.** The f
19d90 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74  irst parameter t
19da0 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  o the authorizer
19db0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63   callback is a c
19dc0 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 72 64  opy of the third
19dd0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f  .** parameter to
19de0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74   the sqlite3_set
19df0 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 20 69 6e  _authorizer() in
19e00 74 65 72 66 61 63 65 2e 20 54 68 65 20 73 65 63  terface. The sec
19e10 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a  ond parameter.**
19e20 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
19e30 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 5b   is an integer [
19e40 53 51 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63  SQLITE_COPY | ac
19e50 74 69 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20  tion code] that 
19e60 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 74 68 65  specifies.** the
19e70 20 70 61 72 74 69 63 75 6c 61 72 20 61 63 74 69   particular acti
19e80 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69  on to be authori
19e90 7a 65 64 2e 20 54 68 65 20 74 68 69 72 64 20 74  zed. The third t
19ea0 68 72 6f 75 67 68 20 73 69 78 74 68 20 70 61 72  hrough sixth par
19eb0 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68  ameters.** to th
19ec0 65 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a  e callback are z
19ed0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  ero-terminated s
19ee0 74 72 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74  trings that cont
19ef0 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a  ain additional.*
19f00 2a 20 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20  * details about 
19f10 74 68 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65  the action to be
19f20 20 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a   authorized..**.
19f30 2a 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e  ** If the action
19f40 20 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45   code is [SQLITE
19f50 5f 52 45 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68  _READ].** and th
19f60 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
19f70 6e 73 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52  ns [SQLITE_IGNOR
19f80 45 5d 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b  E] then the.** [
19f90 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
19fa0 6e 74 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73  nt] statement is
19fb0 20 63 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20   constructed to 
19fc0 73 75 62 73 74 69 74 75 74 65 0a 2a 2a 20 61 20  substitute.** a 
19fd0 4e 55 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c  NULL value in pl
19fe0 61 63 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ace of the table
19ff0 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75   column that wou
1a000 6c 64 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20  ld have.** been 
1a010 72 65 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f  read if [SQLITE_
1a020 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74  OK] had been ret
1a030 75 72 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c  urned.  The [SQL
1a040 49 54 45 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72  ITE_IGNORE].** r
1a050 65 74 75 72 6e 20 63 61 6e 20 62 65 20 75 73 65  eturn can be use
1a060 64 20 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74  d to deny an unt
1a070 72 75 73 74 65 64 20 75 73 65 72 20 61 63 63 65  rusted user acce
1a080 73 73 20 74 6f 20 69 6e 64 69 76 69 64 75 61 6c  ss to individual
1a090 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61  .** columns of a
1a0a0 20 74 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 74 68   table..** If th
1a0b0 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 73  e action code is
1a0c0 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d   [SQLITE_DELETE]
1a0d0 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63   and the callbac
1a0e0 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 51  k returns.** [SQ
1a0f0 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65  LITE_IGNORE] the
1a100 6e 20 74 68 65 20 5b 44 45 4c 45 54 45 5d 20 6f  n the [DELETE] o
1a110 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64  peration proceed
1a120 73 20 62 75 74 20 74 68 65 0a 2a 2a 20 5b 74 72  s but the.** [tr
1a130 75 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74  uncate optimizat
1a140 69 6f 6e 5d 20 69 73 20 64 69 73 61 62 6c 65 64  ion] is disabled
1a150 20 61 6e 64 20 61 6c 6c 20 72 6f 77 73 20 61 72   and all rows ar
1a160 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69 76 69  e deleted indivi
1a170 64 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e  dually..**.** An
1a180 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75   authorizer is u
1a190 73 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65  sed when [sqlite
1a1a0 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 65 70  3_prepare | prep
1a1b0 61 72 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74  aring].** SQL st
1a1c0 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e  atements from an
1a1d0 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63   untrusted sourc
1a1e0 65 2c 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61  e, to ensure tha
1a1f0 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  t the SQL statem
1a200 65 6e 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74  ents.** do not t
1a210 72 79 20 74 6f 20 61 63 63 65 73 73 20 64 61 74  ry to access dat
1a220 61 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61  a they are not a
1a230 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f  llowed to see, o
1a240 72 20 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e  r that they do n
1a250 6f 74 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65  ot.** try to exe
1a260 63 75 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73  cute malicious s
1a270 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64  tatements that d
1a280 61 6d 61 67 65 20 74 68 65 20 64 61 74 61 62 61  amage the databa
1a290 73 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  se.  For.** exam
1a2a0 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74  ple, an applicat
1a2b0 69 6f 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20  ion may allow a 
1a2c0 75 73 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72  user to enter ar
1a2d0 62 69 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71  bitrary.** SQL q
1a2e0 75 65 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75  ueries for evalu
1a2f0 61 74 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62  ation by a datab
1a300 61 73 65 2e 20 20 42 75 74 20 74 68 65 20 61 70  ase.  But the ap
1a310 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a  plication does.*
1a320 2a 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75  * not want the u
1a330 73 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74  ser to be able t
1a340 6f 20 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79  o make arbitrary
1a350 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a   changes to the.
1a360 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e  ** database.  An
1a370 20 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c   authorizer coul
1a380 64 20 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e  d then be put in
1a390 20 70 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65   place while the
1a3a0 0a 2a 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64  .** user-entered
1a3b0 20 53 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73   SQL is being [s
1a3c0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c  qlite3_prepare |
1a3d0 20 70 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a   prepared] that.
1a3e0 2a 2a 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65  ** disallows eve
1a3f0 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b  rything except [
1a400 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e  SELECT] statemen
1a410 74 73 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63  ts..**.** Applic
1a420 61 74 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64  ations that need
1a430 20 74 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20   to process SQL 
1a440 66 72 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73  from untrusted s
1a450 6f 75 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20  ources.** might 
1a460 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f  also consider lo
1a470 77 65 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20  wering resource 
1a480 6c 69 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71  limits using [sq
1a490 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a  lite3_limit()].*
1a4a0 2a 20 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64  * and limiting d
1a4b0 61 74 61 62 61 73 65 20 73 69 7a 65 20 75 73 69  atabase size usi
1a4c0 6e 67 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65  ng the [max_page
1a4d0 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d  _count] [PRAGMA]
1a4e0 0a 2a 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20  .** in addition 
1a4f0 74 6f 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68  to using an auth
1a500 6f 72 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  orizer..**.** On
1a510 6c 79 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68  ly a single auth
1a520 6f 72 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e  orizer can be in
1a530 20 70 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61   place on a data
1a540 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
1a550 2a 2a 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45  ** at a time.  E
1a560 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ach call to sqli
1a570 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
1a580 65 72 20 6f 76 65 72 72 69 64 65 73 20 74 68 65  er overrides the
1a590 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c  .** previous cal
1a5a0 6c 2e 20 20 44 69 73 61 62 6c 65 20 74 68 65 20  l.  Disable the 
1a5b0 61 75 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e  authorizer by in
1a5c0 73 74 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20  stalling a NULL 
1a5d0 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65  callback..** The
1a5e0 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64   authorizer is d
1a5f0 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  isabled by defau
1a600 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  lt..**.** The au
1a610 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1a620 6b 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e  k must not do an
1a630 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c  ything that will
1a640 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64   modify.** the d
1a650 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1a660 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20  on that invoked 
1a670 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  the authorizer c
1a680 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65  allback..** Note
1a690 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70   that [sqlite3_p
1a6a0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64  repare_v2()] and
1a6b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1a6c0 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68  ] both modify th
1a6d0 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  eir.** database 
1a6e0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20  connections for 
1a6f0 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22  the meaning of "
1a700 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20  modify" in this 
1a710 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a  paragraph..**.**
1a720 20 57 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70   When [sqlite3_p
1a730 72 65 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20  repare_v2()] is 
1a740 75 73 65 64 20 74 6f 20 70 72 65 70 61 72 65 20  used to prepare 
1a750 61 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  a statement, the
1a760 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69  .** statement mi
1a770 67 68 74 20 62 65 20 72 65 2d 70 72 65 70 61 72  ght be re-prepar
1a780 65 64 20 64 75 72 69 6e 67 20 5b 73 71 6c 69 74  ed during [sqlit
1a790 65 33 5f 73 74 65 70 28 29 5d 20 64 75 65 20 74  e3_step()] due t
1a7a0 6f 20 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63  o a .** schema c
1a7b0 68 61 6e 67 65 2e 20 20 48 65 6e 63 65 2c 20 74  hange.  Hence, t
1a7c0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73  he application s
1a7d0 68 6f 75 6c 64 20 65 6e 73 75 72 65 20 74 68 61  hould ensure tha
1a7e0 74 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74  t the.** correct
1a7f0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1a800 62 61 63 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20  back remains in 
1a810 70 6c 61 63 65 20 64 75 72 69 6e 67 20 74 68 65  place during the
1a820 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1a830 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  ]..**.** Note th
1a840 61 74 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  at the authorize
1a850 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  r callback is in
1a860 76 6f 6b 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e  voked only durin
1a870 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  g.** [sqlite3_pr
1a880 65 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20  epare()] or its 
1a890 76 61 72 69 61 6e 74 73 2e 20 20 41 75 74 68 6f  variants.  Autho
1a8a0 72 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a  rization is not.
1a8b0 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 64 75 72  ** performed dur
1a8c0 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 76  ing statement ev
1a8d0 61 6c 75 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c  aluation in [sql
1a8e0 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e  ite3_step()], un
1a8f0 6c 65 73 73 0a 2a 2a 20 61 73 20 73 74 61 74 65  less.** as state
1a900 64 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75  d in the previou
1a910 73 20 70 61 72 61 67 72 61 70 68 2c 20 73 71 6c  s paragraph, sql
1a920 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e 76 6f  ite3_step() invo
1a930 6b 65 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70  kes.** sqlite3_p
1a940 72 65 70 61 72 65 5f 76 32 28 29 20 74 6f 20 72  repare_v2() to r
1a950 65 70 72 65 70 61 72 65 20 61 20 73 74 61 74 65  eprepare a state
1a960 6d 65 6e 74 20 61 66 74 65 72 20 61 20 73 63 68  ment after a sch
1a970 65 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a  ema change..**.*
1a980 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
1a990 2a 2a 20 5b 48 31 32 35 30 31 5d 20 5b 48 31 32  ** [H12501] [H12
1a9a0 35 30 32 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48  502] [H12503] [H
1a9b0 31 32 35 30 34 5d 20 5b 48 31 32 35 30 35 5d 20  12504] [H12505] 
1a9c0 5b 48 31 32 35 30 36 5d 20 5b 48 31 32 35 30 37  [H12506] [H12507
1a9d0 5d 20 5b 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48  ] [H12510].** [H
1a9e0 31 32 35 31 31 5d 20 5b 48 31 32 35 31 32 5d 20  12511] [H12512] 
1a9f0 5b 48 31 32 35 32 30 5d 20 5b 48 31 32 35 32 31  [H12520] [H12521
1aa00 5d 20 5b 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51  ] [H12522].*/.SQ
1aa10 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1aa20 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
1aa30 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c  zer(.  sqlite3*,
1aa40 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28  .  int (*xAuth)(
1aa50 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
1aa60 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
1aa70 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
1aa80 6e 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f  nst char*),.  vo
1aa90 69 64 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b  id *pUserData.);
1aaa0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1aab0 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74  : Authorizer Ret
1aac0 75 72 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 39  urn Codes {H1259
1aad0 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a  0} <H12500>.**.*
1aae0 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  * The [sqlite3_s
1aaf0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20  et_authorizer | 
1ab00 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
1ab10 61 63 6b 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75  ack function] mu
1ab20 73 74 0a 2a 2a 20 72 65 74 75 72 6e 20 65 69 74  st.** return eit
1ab30 68 65 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  her [SQLITE_OK] 
1ab40 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20  or one of these 
1ab50 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e  two constants in
1ab60 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67   order.** to sig
1ab70 6e 61 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68  nal SQLite wheth
1ab80 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63  er or not the ac
1ab90 74 69 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65  tion is permitte
1aba0 64 2e 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b  d.  See the.** [
1abb0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
1abc0 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69  orizer | authori
1abd0 7a 65 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  zer documentatio
1abe0 6e 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  n] for additiona
1abf0 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  l.** information
1ac00 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
1ac10 49 54 45 5f 44 45 4e 59 20 20 20 31 20 20 20 2f  ITE_DENY   1   /
1ac20 2a 20 41 62 6f 72 74 20 74 68 65 20 53 51 4c 20  * Abort the SQL 
1ac30 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 61  statement with a
1ac40 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69  n error */.#defi
1ac50 6e 65 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  ne SQLITE_IGNORE
1ac60 20 32 20 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c   2   /* Don't al
1ac70 6c 6f 77 20 61 63 63 65 73 73 2c 20 62 75 74 20  low access, but 
1ac80 64 6f 6e 27 74 20 67 65 6e 65 72 61 74 65 20 61  don't generate a
1ac90 6e 20 65 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a  n error */../*.*
1aca0 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 68  * CAPI3REF: Auth
1acb0 6f 72 69 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f  orizer Action Co
1acc0 64 65 73 20 7b 48 31 32 35 35 30 7d 20 3c 48 31  des {H12550} <H1
1acd0 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  2500>.**.** The 
1ace0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74  [sqlite3_set_aut
1acf0 68 6f 72 69 7a 65 72 28 29 5d 20 69 6e 74 65 72  horizer()] inter
1ad00 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61  face registers a
1ad10 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
1ad20 6f 6e 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e  on.** that is in
1ad30 76 6f 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69  voked to authori
1ad40 7a 65 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73  ze certain SQL s
1ad50 74 61 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73  tatement actions
1ad60 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64  .  The.** second
1ad70 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
1ad80 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e  e callback is an
1ad90 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68   integer code th
1ada0 61 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20  at specifies.** 
1adb0 77 68 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62  what action is b
1adc0 65 69 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e  eing authorized.
1add0 20 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20    These are the 
1ade0 69 6e 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63  integer action c
1adf0 6f 64 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65  odes that.** the
1ae00 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1ae10 62 61 63 6b 20 6d 61 79 20 62 65 20 70 61 73 73  back may be pass
1ae20 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ed..**.** These 
1ae30 61 63 74 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75  action code valu
1ae40 65 73 20 73 69 67 6e 69 66 79 20 77 68 61 74 20  es signify what 
1ae50 6b 69 6e 64 20 6f 66 20 6f 70 65 72 61 74 69 6f  kind of operatio
1ae60 6e 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75  n is to be.** au
1ae70 74 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33  thorized.  The 3
1ae80 72 64 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d  rd and 4th param
1ae90 65 74 65 72 73 20 74 6f 20 74 68 65 20 61 75 74  eters to the aut
1aea0 68 6f 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61  horization.** ca
1aeb0 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
1aec0 77 69 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65  will be paramete
1aed0 72 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e  rs or NULL depen
1aee0 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66  ding on which of
1aef0 20 74 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20   these.** codes 
1af00 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 73  is used as the s
1af10 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  econd parameter.
1af20 20 20 54 68 65 20 35 74 68 20 70 61 72 61 6d 65    The 5th parame
1af30 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75  ter to the.** au
1af40 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1af50 6b 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  k is the name of
1af60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 22   the database ("
1af70 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a  main", "temp",.*
1af80 2a 20 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69  * etc.) if appli
1af90 63 61 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20  cable.  The 6th 
1afa0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
1afb0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1afc0 62 61 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e  back.** is the n
1afd0 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72  ame of the inner
1afe0 2d 6d 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72  -most trigger or
1aff0 20 76 69 65 77 20 74 68 61 74 20 69 73 20 72 65   view that is re
1b000 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a  sponsible for.**
1b010 20 74 68 65 20 61 63 63 65 73 73 20 61 74 74 65   the access atte
1b020 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  mpt or NULL if t
1b030 68 69 73 20 61 63 63 65 73 73 20 61 74 74 65 6d  his access attem
1b040 70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 66  pt is directly f
1b050 72 6f 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c  rom.** top-level
1b060 20 53 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a   SQL code..**.**
1b070 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
1b080 2a 20 5b 48 31 32 35 35 31 5d 20 5b 48 31 32 35  * [H12551] [H125
1b090 35 32 5d 20 5b 48 31 32 35 35 33 5d 20 5b 48 31  52] [H12553] [H1
1b0a0 32 35 35 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  2554].*/./******
1b0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0d0 2a 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a  ***** 3rd ******
1b0e0 2a 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a  ****** 4th *****
1b0f0 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20  ******/.#define 
1b100 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e  SQLITE_CREATE_IN
1b110 44 45 58 20 20 20 20 20 20 20 20 20 20 31 20 20  DEX          1  
1b120 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20   /* Index Name  
1b130 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20      Table Name  
1b140 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b150 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42  QLITE_CREATE_TAB
1b160 4c 45 20 20 20 20 20 20 20 20 20 20 32 20 20 20  LE          2   
1b170 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20  /* Table Name   
1b180 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1b190 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b1a0 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
1b1b0 5f 49 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f  _INDEX     3   /
1b1c0 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20  * Index Name    
1b1d0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
1b1e0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b1f0 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
1b200 54 41 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a  TABLE     4   /*
1b210 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b220 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b230 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b240 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54  TE_CREATE_TEMP_T
1b250 52 49 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20  RIGGER   5   /* 
1b260 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20  Trigger Name    
1b270 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b280 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b290 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49  E_CREATE_TEMP_VI
1b2a0 45 57 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56  EW      6   /* V
1b2b0 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e  iew Name       N
1b2c0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b2d0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b2e0 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 20  _CREATE_TRIGGER 
1b2f0 20 20 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72         7   /* Tr
1b300 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61  igger Name    Ta
1b310 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
1b320 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b330 43 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20  CREATE_VIEW     
1b340 20 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65        8   /* Vie
1b350 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c  w Name       NUL
1b360 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b370 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
1b380 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20  ELETE           
1b390 20 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c       9   /* Tabl
1b3a0 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
1b3b0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1b3c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
1b3d0 4f 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20  OP_INDEX        
1b3e0 20 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78     10   /* Index
1b3f0 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65   Name      Table
1b400 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64   Name      */.#d
1b410 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
1b420 50 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20  P_TABLE         
1b430 20 20 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20    11   /* Table 
1b440 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
1b450 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b460 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
1b470 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20  _TEMP_INDEX     
1b480 20 31 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e   12   /* Index N
1b490 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e  ame      Table N
1b4a0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
1b4b0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
1b4c0 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20  TEMP_TABLE      
1b4d0 31 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  13   /* Table Na
1b4e0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1b4f0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b500 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54  ne SQLITE_DROP_T
1b510 45 4d 50 5f 54 52 49 47 47 45 52 20 20 20 20 31  EMP_TRIGGER    1
1b520 34 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e  4   /* Trigger N
1b530 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  ame    Table Nam
1b540 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
1b550 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  e SQLITE_DROP_TE
1b560 4d 50 5f 56 49 45 57 20 20 20 20 20 20 20 31 35  MP_VIEW       15
1b570 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20     /* View Name 
1b580 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b590 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b5a0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49   SQLITE_DROP_TRI
1b5b0 47 47 45 52 20 20 20 20 20 20 20 20 20 31 36 20  GGER         16 
1b5c0 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d    /* Trigger Nam
1b5d0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20  e    Table Name 
1b5e0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b5f0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57  SQLITE_DROP_VIEW
1b600 20 20 20 20 20 20 20 20 20 20 20 20 31 37 20 20              17  
1b610 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20   /* View Name   
1b620 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b630 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b640 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20  QLITE_INSERT    
1b650 20 20 20 20 20 20 20 20 20 20 20 31 38 20 20 20             18   
1b660 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20  /* Table Name   
1b670 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1b680 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b690 4c 49 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20  LITE_PRAGMA     
1b6a0 20 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f            19   /
1b6b0 2a 20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20  * Pragma Name   
1b6c0 20 20 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c    1st arg or NUL
1b6d0 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  L */.#define SQL
1b6e0 49 54 45 5f 52 45 41 44 20 20 20 20 20 20 20 20  ITE_READ        
1b6f0 20 20 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a           20   /*
1b700 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b710 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20   Column Name    
1b720 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b730 54 45 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20  TE_SELECT       
1b740 20 20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20          21   /* 
1b750 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1b760 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1b770 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b780 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20  E_TRANSACTION   
1b790 20 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f         22   /* O
1b7a0 70 65 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e  peration       N
1b7b0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b7c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b7d0 5f 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20  _UPDATE         
1b7e0 20 20 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61        23   /* Ta
1b7f0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f  ble Name      Co
1b800 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f  lumn Name     */
1b810 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b820 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20  ATTACH          
1b830 20 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c       24   /* Fil
1b840 65 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c  ename        NUL
1b850 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b860 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
1b870 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  ETACH           
1b880 20 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61      25   /* Data
1b890 62 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c  base Name   NULL
1b8a0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1b8b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c  define SQLITE_AL
1b8c0 54 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20  TER_TABLE       
1b8d0 20 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62     26   /* Datab
1b8e0 61 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65  ase Name   Table
1b8f0 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64   Name      */.#d
1b900 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49  efine SQLITE_REI
1b910 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20  NDEX            
1b920 20 20 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20    27   /* Index 
1b930 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
1b940 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b950 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c  fine SQLITE_ANAL
1b960 59 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20  YZE             
1b970 20 32 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e   28   /* Table N
1b980 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
1b990 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1b9a0 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54  ine SQLITE_CREAT
1b9b0 45 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20  E_VTABLE        
1b9c0 32 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  29   /* Table Na
1b9d0 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e  me      Module N
1b9e0 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  ame     */.#defi
1b9f0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56  ne SQLITE_DROP_V
1ba00 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 33  TABLE          3
1ba10 30 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  0   /* Table Nam
1ba20 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61  e      Module Na
1ba30 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  me     */.#defin
1ba40 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f  e SQLITE_FUNCTIO
1ba50 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31  N             31
1ba60 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20     /* NULL      
1ba70 20 20 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e        Function N
1ba80 61 6d 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  ame   */.#define
1ba90 20 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e   SQLITE_SAVEPOIN
1baa0 54 20 20 20 20 20 20 20 20 20 20 20 20 33 32 20  T            32 
1bab0 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20    /* Operation  
1bac0 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e       Savepoint N
1bad0 61 6d 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ame  */.#define 
1bae0 53 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20  SQLITE_COPY     
1baf0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
1bb00 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73   /* No longer us
1bb10 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ed */../*.** CAP
1bb20 49 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41  I3REF: Tracing A
1bb30 6e 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e  nd Profiling Fun
1bb40 63 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20  ctions {H12280} 
1bb50 3c 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45  <S60400>.** EXPE
1bb60 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
1bb70 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65  hese routines re
1bb80 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20  gister callback 
1bb90 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63  functions that c
1bba0 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a  an be used for.*
1bbb0 2a 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72  * tracing and pr
1bbc0 6f 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63  ofiling the exec
1bbd0 75 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61  ution of SQL sta
1bbe0 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  tements..**.** T
1bbf0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
1bc00 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
1bc10 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65  by sqlite3_trace
1bc20 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74  () is invoked at
1bc30 0a 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65  .** various time
1bc40 73 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74  s when an SQL st
1bc50 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67  atement is being
1bc60 20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33   run by [sqlite3
1bc70 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65  _step()]..** The
1bc80 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
1bc90 73 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72  s a UTF-8 render
1bca0 69 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73  ing of the SQL s
1bcb0 74 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a  tatement text.**
1bcc0 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e   as the statemen
1bcd0 74 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65  t first begins e
1bce0 78 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74  xecuting.  Addit
1bcf0 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20  ional callbacks 
1bd00 6f 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68  occur.** as each
1bd10 20 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72   triggered subpr
1bd20 6f 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64  ogram is entered
1bd30 2e 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73  .  The callbacks
1bd40 20 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a   for triggers.**
1bd50 20 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38   contain a UTF-8
1bd60 20 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61   SQL comment tha
1bd70 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  t identifies the
1bd80 20 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20   trigger..**.** 
1bd90 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  The callback fun
1bda0 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
1bdb0 20 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66   by sqlite3_prof
1bdc0 69 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64  ile() is invoked
1bdd0 0a 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20  .** as each SQL 
1bde0 73 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68  statement finish
1bdf0 65 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65  es.  The profile
1be00 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69   callback contai
1be10 6e 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e  ns.** the origin
1be20 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78  al statement tex
1be30 74 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74  t and an estimat
1be40 65 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20  e of wall-clock 
1be50 74 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c  time.** of how l
1be60 6f 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65  ong that stateme
1be70 6e 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a  nt took to run..
1be80 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
1be90 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20  ts:.** [H12281] 
1bea0 5b 48 31 32 32 38 32 5d 20 5b 48 31 32 32 38 33  [H12282] [H12283
1beb0 5d 20 5b 48 31 32 32 38 34 5d 20 5b 48 31 32 32  ] [H12284] [H122
1bec0 38 35 5d 20 5b 48 31 32 32 38 37 5d 20 5b 48 31  85] [H12287] [H1
1bed0 32 32 38 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a  2288] [H12289].*
1bee0 2a 20 5b 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51  * [H12290].*/.SQ
1bef0 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
1bf00 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69  EXPERIMENTAL voi
1bf10 64 20 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65  d *sqlite3_trace
1bf20 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28  (sqlite3*, void(
1bf30 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63  *xTrace)(void*,c
1bf40 6f 6e 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69  onst char*), voi
1bf50 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  d*);.SQLITE_API 
1bf60 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
1bf70 54 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  TAL void *sqlite
1bf80 33 5f 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65  3_profile(sqlite
1bf90 33 2a 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72  3*,.   void(*xPr
1bfa0 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  ofile)(void*,con
1bfb0 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33  st char*,sqlite3
1bfc0 5f 75 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29  _uint64), void*)
1bfd0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
1bfe0 46 3a 20 51 75 65 72 79 20 50 72 6f 67 72 65 73  F: Query Progres
1bff0 73 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32  s Callbacks {H12
1c000 39 31 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a  910} <S60400>.**
1c010 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1c020 20 63 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61   configures a ca
1c030 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
1c040 2d 20 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73  - the.** progres
1c050 73 20 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61  s callback - tha
1c060 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72  t is invoked per
1c070 69 6f 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67  iodically during
1c080 20 6c 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67   long.** running
1c090 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74   calls to [sqlit
1c0a0 65 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c  e3_exec()], [sql
1c0b0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64  ite3_step()] and
1c0c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74  .** [sqlite3_get
1c0d0 5f 74 61 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65  _table()].  An e
1c0e0 78 61 6d 70 6c 65 20 75 73 65 20 66 6f 72 20 74  xample use for t
1c0f0 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  his.** interface
1c100 20 69 73 20 74 6f 20 6b 65 65 70 20 61 20 47 55   is to keep a GU
1c110 49 20 75 70 64 61 74 65 64 20 64 75 72 69 6e 67  I updated during
1c120 20 61 20 6c 61 72 67 65 20 71 75 65 72 79 2e 0a   a large query..
1c130 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f  **.** If the pro
1c140 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72  gress callback r
1c150 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  eturns non-zero,
1c160 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69   the operation i
1c170 73 0a 2a 2a 20 69 6e 74 65 72 72 75 70 74 65 64  s.** interrupted
1c180 2e 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20  .  This feature 
1c190 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69  can be used to i
1c1a0 6d 70 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43  mplement a.** "C
1c1b0 61 6e 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e  ancel" button on
1c1c0 20 61 20 47 55 49 20 70 72 6f 67 72 65 73 73 20   a GUI progress 
1c1d0 64 69 61 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a  dialog box..**.*
1c1e0 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 68  * The progress h
1c1f0 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20  andler must not 
1c200 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74  do anything that
1c210 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20   will modify.** 
1c220 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
1c230 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76  nection that inv
1c240 6f 6b 65 64 20 74 68 65 20 70 72 6f 67 72 65 73  oked the progres
1c250 73 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f  s handler..** No
1c260 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33  te that [sqlite3
1c270 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61  _prepare_v2()] a
1c280 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  nd [sqlite3_step
1c290 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20  ()] both modify 
1c2a0 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73  their.** databas
1c2b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f  e connections fo
1c2c0 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66  r the meaning of
1c2d0 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69   "modify" in thi
1c2e0 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a  s paragraph..**.
1c2f0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
1c300 0a 2a 2a 20 5b 48 31 32 39 31 31 5d 20 5b 48 31  .** [H12911] [H1
1c310 32 39 31 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b  2912] [H12913] [
1c320 48 31 32 39 31 34 5d 20 5b 48 31 32 39 31 35 5d  H12914] [H12915]
1c330 20 5b 48 31 32 39 31 36 5d 20 5b 48 31 32 39 31   [H12916] [H1291
1c340 37 5d 20 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a  7] [H12918].**.*
1c350 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
1c360 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  d sqlite3_progre
1c370 73 73 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74  ss_handler(sqlit
1c380 65 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29  e3*, int, int(*)
1c390 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b  (void*), void*);
1c3a0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1c3b0 3a 20 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20  : Opening A New 
1c3c0 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74  Database Connect
1c3d0 69 6f 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34  ion {H12700} <S4
1c3e0 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  0200>.**.** Thes
1c3f0 65 20 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20  e routines open 
1c400 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  an SQLite databa
1c410 73 65 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61  se file whose na
1c420 6d 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 74  me is given by t
1c430 68 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61  he.** filename a
1c440 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c  rgument. The fil
1c450 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69  ename argument i
1c460 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  s interpreted as
1c470 20 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71   UTF-8 for.** sq
1c480 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64  lite3_open() and
1c490 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
1c4a0 28 29 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36  () and as UTF-16
1c4b0 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62   in the native b
1c4c0 79 74 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72  yte.** order for
1c4d0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
1c4e0 29 2e 20 41 20 5b 64 61 74 61 62 61 73 65 20 63  ). A [database c
1c4f0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c  onnection] handl
1c500 65 20 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20  e is usually.** 
1c510 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44  returned in *ppD
1c520 62 2c 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72  b, even if an er
1c530 72 6f 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65  ror occurs.  The
1c540 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20   only exception 
1c550 69 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51  is that.** if SQ
1c560 4c 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74  Lite is unable t
1c570 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  o allocate memor
1c580 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73  y to hold the [s
1c590 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a  qlite3] object,.
1c5a0 2a 2a 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62  ** a NULL will b
1c5b0 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a  e written into *
1c5c0 70 70 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20  ppDb instead of 
1c5d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1c5e0 20 5b 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62   [sqlite3].** ob
1c5f0 6a 65 63 74 2e 20 49 66 20 74 68 65 20 64 61 74  ject. If the dat
1c600 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20  abase is opened 
1c610 28 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29  (and/or created)
1c620 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74   successfully, t
1c630 68 65 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  hen.** [SQLITE_O
1c640 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  K] is returned. 
1c650 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65   Otherwise an [e
1c660 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
1c670 74 75 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20  turned.  The.** 
1c680 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  [sqlite3_errmsg(
1c690 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65  )] or [sqlite3_e
1c6a0 72 72 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69  rrmsg16()] routi
1c6b0 6e 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  nes can be used 
1c6c0 74 6f 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20  to obtain.** an 
1c6d0 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
1c6e0 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20   description of 
1c6f0 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a  the error..**.**
1c700 20 54 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63   The default enc
1c710 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61  oding for the da
1c720 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55  tabase will be U
1c730 54 46 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74  TF-8 if.** sqlit
1c740 65 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c  e3_open() or sql
1c750 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69  ite3_open_v2() i
1c760 73 20 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20  s called and.** 
1c770 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61  UTF-16 in the na
1c780 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
1c790 69 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  if sqlite3_open1
1c7a0 36 28 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a  6() is used..**.
1c7b0 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f  ** Whether or no
1c7c0 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  t an error occur
1c7d0 73 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65  s when it is ope
1c7e0 6e 65 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a  ned, resources.*
1c7f0 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
1c800 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  h the [database 
1c810 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64  connection] hand
1c820 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c  le should be rel
1c830 65 61 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73  eased by.** pass
1c840 69 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74  ing it to [sqlit
1c850 65 33 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e  e3_close()] when
1c860 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72   it is no longer
1c870 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a   required..**.**
1c880 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65   The sqlite3_ope
1c890 6e 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 65  n_v2() interface
1c8a0 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69   works like sqli
1c8b0 74 65 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78  te3_open().** ex
1c8c0 63 65 70 74 20 74 68 61 74 20 69 74 20 61 63 63  cept that it acc
1c8d0 65 70 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f  epts two additio
1c8e0 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66  nal parameters f
1c8f0 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f  or additional co
1c900 6e 74 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68  ntrol.** over th
1c910 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63  e new database c
1c920 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20  onnection.  The 
1c930 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
1c940 63 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a  can take one of.
1c950 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** the following
1c960 20 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f   three values, o
1c970 70 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e  ptionally combin
1c980 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20  ed with the .** 
1c990 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d  [SQLITE_OPEN_NOM
1c9a0 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f  UTEX], [SQLITE_O
1c9b0 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 20  PEN_FULLMUTEX], 
1c9c0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41  [SQLITE_OPEN_SHA
1c9d0 52 45 44 43 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e  REDCACHE],.** an
1c9e0 64 2f 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45  d/or [SQLITE_OPE
1c9f0 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 5d 20  N_PRIVATECACHE] 
1ca00 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c  flags:.**.** <dl
1ca10 3e 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45  >.** <dt>[SQLITE
1ca20 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c  _OPEN_READONLY]<
1ca30 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
1ca40 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
1ca50 65 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20  ed in read-only 
1ca60 6d 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61  mode.  If the da
1ca70 74 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a  tabase does not.
1ca80 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ** already exist
1ca90 2c 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  , an error is re
1caa0 74 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a  turned.</dd>.**.
1cab0 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f  ** <dt>[SQLITE_O
1cac0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f  PEN_READWRITE]</
1cad0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64  dt>.** <dd>The d
1cae0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
1caf0 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e  d for reading an
1cb00 64 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73  d writing if pos
1cb10 73 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e  sible, or readin
1cb20 67 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65  g.** only if the
1cb30 20 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70   file is write p
1cb40 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20  rotected by the 
1cb50 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
1cb60 2e 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20  .  In either.** 
1cb70 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73  case the databas
1cb80 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65  e must already e
1cb90 78 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20  xist, otherwise 
1cba0 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  an error is retu
1cbb0 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  rned.</dd>.**.**
1cbc0 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45   <dt>[SQLITE_OPE
1cbd0 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b  N_READWRITE] | [
1cbe0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
1cbf0 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TE]</dt>.** <dd>
1cc00 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  The database is 
1cc10 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69  opened for readi
1cc20 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20  ng and writing, 
1cc30 61 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69  and is creates i
1cc40 74 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20  t if.** it does 
1cc50 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
1cc60 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62  t. This is the b
1cc70 65 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20  ehavior that is 
1cc80 61 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a  always used for.
1cc90 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ** sqlite3_open(
1cca0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70  ) and sqlite3_op
1ccb0 65 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20  en16().</dd>.** 
1ccc0 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  </dl>.**.** If t
1ccd0 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72  he 3rd parameter
1cce0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   to sqlite3_open
1ccf0 5f 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65  _v2() is not one
1cd00 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69   of the.** combi
1cd10 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62  nations shown ab
1cd20 6f 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68  ove or one of th
1cd30 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73  e combinations s
1cd40 68 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69  hown above combi
1cd50 6e 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ned.** with the 
1cd60 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d  [SQLITE_OPEN_NOM
1cd70 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f  UTEX], [SQLITE_O
1cd80 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a  PEN_FULLMUTEX],.
1cd90 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ** [SQLITE_OPEN_
1cda0 53 48 41 52 45 44 43 41 43 48 45 5d 20 61 6e 64  SHAREDCACHE] and
1cdb0 2f 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  /or [SQLITE_OPEN
1cdc0 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c  _SHAREDCACHE] fl
1cdd0 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ags,.** then the
1cde0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
1cdf0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  efined..**.** If
1ce00 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45   the [SQLITE_OPE
1ce10 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20  N_NOMUTEX] flag 
1ce20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  is set, then the
1ce30 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1ce40 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e  tion.** opens in
1ce50 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61   the multi-threa
1ce60 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  d [threading mod
1ce70 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68  e] as long as th
1ce80 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a  e single-thread.
1ce90 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20  ** mode has not 
1cea0 62 65 65 6e 20 73 65 74 20 61 74 20 63 6f 6d 70  been set at comp
1ceb0 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72  ile-time or star
1cec0 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 68 65 0a  t-time.  If the.
1ced0 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ** [SQLITE_OPEN_
1cee0 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20  FULLMUTEX] flag 
1cef0 69 73 20 73 65 74 20 74 68 65 6e 20 74 68 65 20  is set then the 
1cf00 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1cf10 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20  ion opens.** in 
1cf20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 5b  the serialized [
1cf30 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20  threading mode] 
1cf40 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d 74 68  unless single-th
1cf50 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 72 65 76  read was.** prev
1cf60 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 65 64 20  iously selected 
1cf70 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  at compile-time 
1cf80 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a  or start-time..*
1cf90 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50  * The [SQLITE_OP
1cfa0 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20  EN_SHAREDCACHE] 
1cfb0 66 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 20  flag causes the 
1cfc0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1cfd0 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69  ion to be.** eli
1cfe0 67 69 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68  gible to use [sh
1cff0 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d  ared cache mode]
1d000 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  , regardless of 
1d010 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73  whether or not s
1d020 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69  hared.** cache i
1d030 73 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20  s enabled using 
1d040 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f  [sqlite3_enable_
1d050 73 68 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e  shared_cache()].
1d060 20 20 54 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45    The.** [SQLITE
1d070 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43  _OPEN_PRIVATECAC
1d080 48 45 5d 20 66 6c 61 67 20 63 61 75 73 65 73 20  HE] flag causes 
1d090 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
1d0a0 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a  nection to not.*
1d0b0 2a 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e  * participate in
1d0c0 20 5b 73 68 61 72 65 64 20 63 61 63 68 65 20 6d   [shared cache m
1d0d0 6f 64 65 5d 20 65 76 65 6e 20 69 66 20 69 74 20  ode] even if it 
1d0e0 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a  is enabled..**.*
1d0f0 2a 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d  * If the filenam
1d100 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c  e is ":memory:",
1d110 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c   then a private,
1d120 20 74 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65   temporary in-me
1d130 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a  mory database.**
1d140 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20   is created for 
1d150 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  the connection. 
1d160 20 54 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20   This in-memory 
1d170 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61  database will va
1d180 6e 69 73 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65  nish when.** the
1d190 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1d1a0 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20  tion is closed. 
1d1b0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
1d1c0 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74   of SQLite might
1d1d0 0a 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20  .** make use of 
1d1e0 61 64 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69  additional speci
1d1f0 61 6c 20 66 69 6c 65 6e 61 6d 65 73 20 74 68 61  al filenames tha
1d200 74 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65  t begin with the
1d210 20 22 3a 22 20 63 68 61 72 61 63 74 65 72 2e 0a   ":" character..
1d220 2a 2a 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65  ** It is recomme
1d230 6e 64 65 64 20 74 68 61 74 20 77 68 65 6e 20 61  nded that when a
1d240 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61   database filena
1d250 6d 65 20 61 63 74 75 61 6c 6c 79 20 64 6f 65 73  me actually does
1d260 20 62 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61   begin with.** a
1d270 20 22 3a 22 20 63 68 61 72 61 63 74 65 72 20 79   ":" character y
1d280 6f 75 20 73 68 6f 75 6c 64 20 70 72 65 66 69 78  ou should prefix
1d290 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69   the filename wi
1d2a0 74 68 20 61 20 70 61 74 68 6e 61 6d 65 20 73 75  th a pathname su
1d2b0 63 68 20 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f  ch as.** "./" to
1d2c0 20 61 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79   avoid ambiguity
1d2d0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  ..**.** If the f
1d2e0 69 6c 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d  ilename is an em
1d2f0 70 74 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e  pty string, then
1d300 20 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70   a private, temp
1d310 6f 72 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b  orary.** on-disk
1d320 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62   database will b
1d330 65 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73  e created.  This
1d340 20 70 72 69 76 61 74 65 20 64 61 74 61 62 61 73   private databas
1d350 65 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74  e will be.** aut
1d360 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74  omatically delet
1d370 65 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68  ed as soon as th
1d380 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1d390 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e  ction is closed.
1d3a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74  .**.** The fourt
1d3b0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  h parameter to s
1d3c0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
1d3d0 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
1d3e0 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  the.** [sqlite3_
1d3f0 76 66 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74  vfs] object that
1d400 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65   defines the ope
1d410 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e  rating system in
1d420 74 65 72 66 61 63 65 20 74 68 61 74 0a 2a 2a 20  terface that.** 
1d430 74 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65  the new database
1d440 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75   connection shou
1d450 6c 64 20 75 73 65 2e 20 20 49 66 20 74 68 65 20  ld use.  If the 
1d460 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
1d470 20 69 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f   is.** a NULL po
1d480 69 6e 74 65 72 20 74 68 65 6e 20 74 68 65 20 64  inter then the d
1d490 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f  efault [sqlite3_
1d4a0 76 66 73 5d 20 6f 62 6a 65 63 74 20 69 73 20 75  vfs] object is u
1d4b0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f  sed..**.** <b>No
1d4c0 74 65 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73  te to Windows us
1d4d0 65 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e  ers:</b>  The en
1d4e0 63 6f 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20  coding used for 
1d4f0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  the filename arg
1d500 75 6d 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69  ument.** of sqli
1d510 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73  te3_open() and s
1d520 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
1d530 20 6d 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20   must be UTF-8, 
1d540 6e 6f 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20  not whatever.** 
1d550 63 6f 64 65 70 61 67 65 20 69 73 20 63 75 72 72  codepage is curr
1d560 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20  ently defined.  
1d570 46 69 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69  Filenames contai
1d580 6e 69 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e  ning internation
1d590 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  al.** characters
1d5a0 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74   must be convert
1d5b0 65 64 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f  ed to UTF-8 prio
1d5c0 72 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65  r to passing the
1d5d0 6d 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  m into.** sqlite
1d5e0 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69  3_open() or sqli
1d5f0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a  te3_open_v2()..*
1d600 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
1d610 73 3a 0a 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b  s:.** [H12701] [
1d620 48 31 32 37 30 32 5d 20 5b 48 31 32 37 30 33 5d  H12702] [H12703]
1d630 20 5b 48 31 32 37 30 34 5d 20 5b 48 31 32 37 30   [H12704] [H1270
1d640 36 5d 20 5b 48 31 32 37 30 37 5d 20 5b 48 31 32  6] [H12707] [H12
1d650 37 30 39 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a  709] [H12711].**
1d660 20 5b 48 31 32 37 31 32 5d 20 5b 48 31 32 37 31   [H12712] [H1271
1d670 33 5d 20 5b 48 31 32 37 31 34 5d 20 5b 48 31 32  3] [H12714] [H12
1d680 37 31 37 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48  717] [H12719] [H
1d690 31 32 37 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a  12721] [H12723].
1d6a0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1d6b0 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a  t sqlite3_open(.
1d6c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69    const char *fi
1d6d0 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74  lename,   /* Dat
1d6e0 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28  abase filename (
1d6f0 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69  UTF-8) */.  sqli
1d700 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20  te3 **ppDb      
1d710 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69      /* OUT: SQLi
1d720 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a  te db handle */.
1d730 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
1d740 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  t sqlite3_open16
1d750 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  (.  const void *
1d760 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44  filename,   /* D
1d770 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
1d780 20 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73   (UTF-16) */.  s
1d790 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20  qlite3 **ppDb   
1d7a0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53         /* OUT: S
1d7b0 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20  QLite db handle 
1d7c0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  */.);.SQLITE_API
1d7d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
1d7e0 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68  n_v2(.  const ch
1d7f0 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20  ar *filename,   
1d800 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65  /* Database file
1d810 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a  name (UTF-8) */.
1d820 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62    sqlite3 **ppDb
1d830 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ,         /* OUT
1d840 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64  : SQLite db hand
1d850 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  le */.  int flag
1d860 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
1d870 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f  /* Flags */.  co
1d880 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20  nst char *zVfs  
1d890 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1d8a0 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75   VFS module to u
1d8b0 73 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  se */.);../*.** 
1d8c0 43 41 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20  CAPI3REF: Error 
1d8d0 43 6f 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67  Codes And Messag
1d8e0 65 73 20 7b 48 31 32 38 30 30 7d 20 3c 53 36 30  es {H12800} <S60
1d8f0 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  200>.**.** The s
1d900 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
1d910 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
1d920 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 5b  ns the numeric [
1d930 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a  result code] or.
1d940 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73  ** [extended res
1d950 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 68  ult code] for th
1d960 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 66 61  e most recent fa
1d970 69 6c 65 64 20 73 71 6c 69 74 65 33 5f 2a 20 41  iled sqlite3_* A
1d980 50 49 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63  PI call.** assoc
1d990 69 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61  iated with a [da
1d9a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1d9b0 6e 5d 2e 20 49 66 20 61 20 70 72 69 6f 72 20 41  n]. If a prior A
1d9c0 50 49 20 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a  PI call failed.*
1d9d0 2a 20 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72  * but the most r
1d9e0 65 63 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73  ecent API call s
1d9f0 75 63 63 65 65 64 65 64 2c 20 74 68 65 20 72 65  ucceeded, the re
1da00 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a  turn value from.
1da10 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  ** sqlite3_errco
1da20 64 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65  de() is undefine
1da30 64 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  d.  The sqlite3_
1da40 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65  extended_errcode
1da50 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ().** interface 
1da60 69 73 20 74 68 65 20 73 61 6d 65 20 65 78 63 65  is the same exce
1da70 70 74 20 74 68 61 74 20 69 74 20 61 6c 77 61 79  pt that it alway
1da80 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 0a 2a  s returns the .*
1da90 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75  * [extended resu
1daa0 6c 74 20 63 6f 64 65 5d 20 65 76 65 6e 20 77 68  lt code] even wh
1dab0 65 6e 20 65 78 74 65 6e 64 65 64 20 72 65 73 75  en extended resu
1dac0 6c 74 20 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20  lt codes are.** 
1dad0 64 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  disabled..**.** 
1dae0 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  The sqlite3_errm
1daf0 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  sg() and sqlite3
1db00 5f 65 72 72 6d 73 67 31 36 28 29 20 72 65 74 75  _errmsg16() retu
1db10 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75  rn English-langu
1db20 61 67 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74  age.** text that
1db30 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65   describes the e
1db40 72 72 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20  rror, as either 
1db50 55 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20  UTF-8 or UTF-16 
1db60 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a  respectively..**
1db70 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20   Memory to hold 
1db80 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
1db90 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61  e string is mana
1dba0 67 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a  ged internally..
1dbb0 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  ** The applicati
1dbc0 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
1dbd0 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20   to worry about 
1dbe0 66 72 65 65 69 6e 67 20 74 68 65 20 72 65 73 75  freeing the resu
1dbf0 6c 74 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  lt..** However, 
1dc00 74 68 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67  the error string
1dc10 20 6d 69 67 68 74 20 62 65 20 6f 76 65 72 77 72   might be overwr
1dc20 69 74 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63  itten or dealloc
1dc30 61 74 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65  ated by.** subse
1dc40 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f  quent calls to o
1dc50 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65  ther SQLite inte
1dc60 72 66 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e  rface functions.
1dc70 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
1dc80 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65  serialized [thre
1dc90 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69  ading mode] is i
1dca0 6e 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74 20  n use, it might 
1dcb0 62 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74  be the.** case t
1dcc0 68 61 74 20 61 20 73 65 63 6f 6e 64 20 65 72 72  hat a second err
1dcd0 6f 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 73  or occurs on a s
1dce0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 69  eparate thread i
1dcf0 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65  n between.** the
1dd00 20 74 69 6d 65 20 6f 66 20 74 68 65 20 66 69 72   time of the fir
1dd10 73 74 20 65 72 72 6f 72 20 61 6e 64 20 74 68 65  st error and the
1dd20 20 63 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 69   call to these i
1dd30 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68  nterfaces..** Wh
1dd40 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c  en that happens,
1dd50 20 74 68 65 20 73 65 63 6f 6e 64 20 65 72 72 6f   the second erro
1dd60 72 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74  r will be report
1dd70 65 64 20 73 69 6e 63 65 20 74 68 65 73 65 0a 2a  ed since these.*
1dd80 2a 20 69 6e 74 65 72 66 61 63 65 73 20 61 6c 77  * interfaces alw
1dd90 61 79 73 20 72 65 70 6f 72 74 20 74 68 65 20 6d  ays report the m
1dda0 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 75 6c  ost recent resul
1ddb0 74 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20  t.  To avoid.** 
1ddc0 74 68 69 73 2c 20 65 61 63 68 20 74 68 72 65 61  this, each threa
1ddd0 64 20 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 63  d can obtain exc
1dde0 6c 75 73 69 76 65 20 75 73 65 20 6f 66 20 74 68  lusive use of th
1ddf0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
1de00 65 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20  ection] D.** by 
1de10 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65  invoking [sqlite
1de20 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b  3_mutex_enter]([
1de30 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78  sqlite3_db_mutex
1de40 5d 28 44 29 29 20 62 65 66 6f 72 65 20 62 65 67  ](D)) before beg
1de50 69 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65  inning.** to use
1de60 20 44 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20   D and invoking 
1de70 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c  [sqlite3_mutex_l
1de80 65 61 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64  eave]([sqlite3_d
1de90 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 61 66 74  b_mutex](D)) aft
1dea0 65 72 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20  er.** all calls 
1deb0 74 6f 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  to the interface
1dec0 73 20 6c 69 73 74 65 64 20 68 65 72 65 20 61 72  s listed here ar
1ded0 65 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a  e completed..**.
1dee0 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 66 61  ** If an interfa
1def0 63 65 20 66 61 69 6c 73 20 77 69 74 68 20 53 51  ce fails with SQ
1df00 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 61  LITE_MISUSE, tha
1df10 74 20 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 65  t means the inte
1df20 72 66 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76  rface.** was inv
1df30 6f 6b 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79  oked incorrectly
1df40 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74   by the applicat
1df50 69 6f 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 61  ion.  In that ca
1df60 73 65 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72  se, the.** error
1df70 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67   code and messag
1df80 65 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74  e may or may not
1df90 20 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52   be set..**.** R
1dfa0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
1dfb0 5b 48 31 32 38 30 31 5d 20 5b 48 31 32 38 30 32  [H12801] [H12802
1dfc0 5d 20 5b 48 31 32 38 30 33 5d 20 5b 48 31 32 38  ] [H12803] [H128
1dfd0 30 37 5d 20 5b 48 31 32 38 30 38 5d 20 5b 48 31  07] [H12808] [H1
1dfe0 32 38 30 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  2809].*/.SQLITE_
1dff0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1e000 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20  errcode(sqlite3 
1e010 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  *db);.SQLITE_API
1e020 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74   int sqlite3_ext
1e030 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71  ended_errcode(sq
1e040 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49  lite3 *db);.SQLI
1e050 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
1e060 72 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  r *sqlite3_errms
1e070 67 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  g(sqlite3*);.SQL
1e080 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1e090 69 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d  id *sqlite3_errm
1e0a0 73 67 31 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a  sg16(sqlite3*);.
1e0b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1e0c0 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f   SQL Statement O
1e0d0 62 6a 65 63 74 20 7b 48 31 33 30 30 30 7d 20 3c  bject {H13000} <
1e0e0 48 31 33 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f  H13010>.** KEYWO
1e0f0 52 44 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73  RDS: {prepared s
1e100 74 61 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61  tatement} {prepa
1e110 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a  red statements}.
1e120 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  **.** An instanc
1e130 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
1e140 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69   represents a si
1e150 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65  ngle SQL stateme
1e160 6e 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65  nt..** This obje
1e170 63 74 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20  ct is variously 
1e180 6b 6e 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70  known as a "prep
1e190 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20  ared statement" 
1e1a0 6f 72 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65  or a.** "compile
1e1b0 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22  d SQL statement"
1e1c0 20 6f 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20   or simply as a 
1e1d0 22 73 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a  "statement"..**.
1e1e0 2a 2a 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61  ** The life of a
1e1f0 20 73 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63   statement objec
1e200 74 20 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67  t goes something
1e210 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
1e220 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43  * <ol>.** <li> C
1e230 72 65 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74  reate the object
1e240 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
1e250 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72  prepare_v2()] or
1e260 20 61 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20   a related.**   
1e270 20 20 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20     function..** 
1e280 3c 6c 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73  <li> Bind values
1e290 20 74 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65   to [host parame
1e2a0 74 65 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20  ters] using the 
1e2b0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29  sqlite3_bind_*()
1e2c0 0a 2a 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61  .**      interfa
1e2d0 63 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e  ces..** <li> Run
1e2e0 20 74 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c   the SQL by call
1e2f0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ing [sqlite3_ste
1e300 70 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  p()] one or more
1e310 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20   times..** <li> 
1e320 52 65 73 65 74 20 74 68 65 20 73 74 61 74 65 6d  Reset the statem
1e330 65 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ent using [sqlit
1e340 65 33 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e  e3_reset()] then
1e350 20 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20   go back.**     
1e360 20 74 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20   to step 2.  Do 
1e370 74 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72  this zero or mor
1e380 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e  e times..** <li>
1e390 20 44 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a   Destroy the obj
1e3a0 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ect using [sqlit
1e3b0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a  e3_finalize()]..
1e3c0 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52  ** </ol>.**.** R
1e3d0 65 66 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74  efer to document
1e3e0 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64  ation on individ
1e3f0 75 61 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76  ual methods abov
1e400 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
1e410 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  .** information.
1e420 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
1e430 63 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ct sqlite3_stmt 
1e440 73 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f  sqlite3_stmt;../
1e450 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
1e460 75 6e 2d 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b  un-time Limits {
1e470 48 31 32 37 36 30 7d 20 3c 53 32 30 36 30 30 3e  H12760} <S20600>
1e480 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
1e490 72 66 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65  rface allows the
1e4a0 20 73 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73   size of various
1e4b0 20 63 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62   constructs to b
1e4c0 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20  e limited.** on 
1e4d0 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20  a connection by 
1e4e0 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73  connection basis
1e4f0 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72  .  The first par
1e500 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a  ameter is the.**
1e510 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
1e520 63 74 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d  ction] whose lim
1e530 69 74 20 69 73 20 74 6f 20 62 65 20 73 65 74 20  it is to be set 
1e540 6f 72 20 71 75 65 72 69 65 64 2e 20 20 54 68 65  or queried.  The
1e550 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  .** second param
1e560 65 74 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74  eter is one of t
1e570 68 65 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f  he [limit catego
1e580 72 69 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e  ries] that defin
1e590 65 20 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20  e a.** class of 
1e5a0 63 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65  constructs to be
1e5b0 20 73 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20   size limited.  
1e5c0 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  The third parame
1e5d0 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65  ter is the.** ne
1e5e0 77 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74  w limit for that
1e5f0 20 63 6f 6e 73 74 72 75 63 74 2e 20 20 54 68 65   construct.  The
1e600 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
1e610 73 20 74 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e  s the old limit.
1e620 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65  .**.** If the ne
1e630 77 20 6c 69 6d 69 74 20 69 73 20 61 20 6e 65 67  w limit is a neg
1e640 61 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68  ative number, th
1e650 65 20 6c 69 6d 69 74 20 69 73 20 75 6e 63 68 61  e limit is uncha
1e660 6e 67 65 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65  nged..** For the
1e670 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20   limit category 
1e680 6f 66 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  of SQLITE_LIMIT_
1e690 58 59 5a 20 74 68 65 72 65 20 69 73 20 61 20 0a  XYZ there is a .
1e6a0 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72  ** [limits | har
1e6b0 64 20 75 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a  d upper bound].*
1e6c0 2a 20 73 65 74 20 62 79 20 61 20 63 6f 6d 70 69  * set by a compi
1e6d0 6c 65 2d 74 69 6d 65 20 43 20 70 72 65 70 72 6f  le-time C prepro
1e6e0 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d  cessor macro nam
1e6f0 65 64 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c  ed .** [limits |
1e700 20 53 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d   SQLITE_MAX_XYZ]
1e710 2e 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49  ..** (The "_LIMI
1e720 54 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20  T_" in the name 
1e730 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f  is changed to "_
1e740 4d 41 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d  MAX_".).** Attem
1e750 70 74 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20  pts to increase 
1e760 61 20 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74  a limit above it
1e770 73 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75  s hard upper bou
1e780 6e 64 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74  nd are.** silent
1e790 6c 79 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20  ly truncated to 
1e7a0 74 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c  the hard upper l
1e7b0 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20  imit..**.** Run 
1e7c0 74 69 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20  time limits are 
1e7d0 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
1e7e0 20 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73   in applications
1e7f0 20 74 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20   that manage.** 
1e800 62 6f 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69  both their own i
1e810 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65  nternal database
1e820 20 61 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61   and also databa
1e830 73 65 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e  ses that are con
1e840 74 72 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e  trolled.** by un
1e850 74 72 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c  trusted external
1e860 20 73 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78   sources.  An ex
1e870 61 6d 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f  ample applicatio
1e880 6e 20 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20  n might be a.** 
1e890 77 65 62 20 62 72 6f 77 73 65 72 20 74 68 61 74  web browser that
1e8a0 20 68 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74   has its own dat
1e8b0 61 62 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69  abases for stori
1e8c0 6e 67 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a  ng history and.*
1e8d0 2a 20 73 65 70 61 72 61 74 65 20 64 61 74 61 62  * separate datab
1e8e0 61 73 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20  ases controlled 
1e8f0 62 79 20 4a 61 76 61 53 63 72 69 70 74 20 61 70  by JavaScript ap
1e900 70 6c 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c  plications downl
1e910 6f 61 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65  oaded.** off the
1e920 20 49 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20   Internet.  The 
1e930 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73  internal databas
1e940 65 73 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20  es can be given 
1e950 74 68 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65  the.** large, de
1e960 66 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44  fault limits.  D
1e970 61 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64  atabases managed
1e980 20 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75   by external sou
1e990 72 63 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67  rces can.** be g
1e9a0 69 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65  iven much smalle
1e9b0 72 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65  r limits designe
1e9c0 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64  d to prevent a d
1e9d0 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65  enial of service
1e9e0 0a 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76  .** attack.  Dev
1e9f0 65 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c  elopers might al
1ea00 73 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74  so want to use t
1ea10 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  he [sqlite3_set_
1ea20 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a  authorizer()].**
1ea30 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75   interface to fu
1ea40 72 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e  rther control un
1ea50 74 72 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68  trusted SQL.  Th
1ea60 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e size of the da
1ea70 74 61 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65  tabase.** create
1ea80 64 20 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65  d by an untruste
1ea90 64 20 73 63 72 69 70 74 20 63 61 6e 20 62 65 20  d script can be 
1eaa0 63 6f 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20  contained using 
1eab0 74 68 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65  the.** [max_page
1eac0 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d  _count] [PRAGMA]
1ead0 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d  ..**.** New run-
1eae0 74 69 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67  time limit categ
1eaf0 6f 72 69 65 73 20 6d 61 79 20 62 65 20 61 64 64  ories may be add
1eb00 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c  ed in future rel
1eb10 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  eases..**.** Req
1eb20 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
1eb30 31 32 37 36 32 5d 20 5b 48 31 32 37 36 36 5d 20  12762] [H12766] 
1eb40 5b 48 31 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49  [H12769].*/.SQLI
1eb50 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1eb60 65 33 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33  e3_limit(sqlite3
1eb70 2a 2c 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e  *, int id, int n
1eb80 65 77 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ewVal);../*.** C
1eb90 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d  API3REF: Run-Tim
1eba0 65 20 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69  e Limit Categori
1ebb0 65 73 20 7b 48 31 32 37 39 30 7d 20 3c 48 31 32  es {H12790} <H12
1ebc0 37 36 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  760>.** KEYWORDS
1ebd0 3a 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72  : {limit categor
1ebe0 79 7d 20 7b 2a 6c 69 6d 69 74 20 63 61 74 65 67  y} {*limit categ
1ebf0 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  ories}.**.** The
1ec00 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 66  se constants def
1ec10 69 6e 65 20 76 61 72 69 6f 75 73 20 70 65 72 66  ine various perf
1ec20 6f 72 6d 61 6e 63 65 20 6c 69 6d 69 74 73 0a 2a  ormance limits.*
1ec30 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 6c 6f  * that can be lo
1ec40 77 65 72 65 64 20 61 74 20 72 75 6e 2d 74 69 6d  wered at run-tim
1ec50 65 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  e using [sqlite3
1ec60 5f 6c 69 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68  _limit()]..** Th
1ec70 65 20 73 79 6e 6f 70 73 69 73 20 6f 66 20 74 68  e synopsis of th
1ec80 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68  e meanings of th
1ec90 65 20 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73  e various limits
1eca0 20 69 73 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e   is shown below.
1ecb0 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69  .** Additional i
1ecc0 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 76  nformation is av
1ecd0 61 69 6c 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69  ailable at [limi
1ece0 74 73 20 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53  ts | Limits in S
1ecf0 51 4c 69 74 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64  QLite]..**.** <d
1ed00 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  l>.** <dt>SQLITE
1ed10 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64  _LIMIT_LENGTH</d
1ed20 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1ed30 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e  ximum size of an
1ed40 79 20 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42  y string or BLOB
1ed50 20 6f 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64   or table row.<d
1ed60 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
1ed70 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45  ITE_LIMIT_SQL_LE
1ed80 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NGTH</dt>.** <dd
1ed90 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e  >The maximum len
1eda0 67 74 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74  gth of an SQL st
1edb0 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a  atement.</dd>.**
1edc0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
1edd0 49 4d 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e  IMIT_COLUMN</dt>
1ede0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1edf0 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  mum number of co
1ee00 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65  lumns in a table
1ee10 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69   definition or i
1ee20 6e 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20  n the.** result 
1ee30 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54  set of a [SELECT
1ee40 5d 20 6f 72 20 74 68 65 20 6d 61 78 69 6d 75 6d  ] or the maximum
1ee50 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1ee60 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a  ns in an index.*
1ee70 2a 20 6f 72 20 69 6e 20 61 6e 20 4f 52 44 45 52  * or in an ORDER
1ee80 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
1ee90 63 6c 61 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a  clause.</dd>.**.
1eea0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
1eeb0 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f  MIT_EXPR_DEPTH</
1eec0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d  dt>.** <dd>The m
1eed0 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20  aximum depth of 
1eee0 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 6f  the parse tree o
1eef0 6e 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e  n any expression
1ef00 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1ef10 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  >SQLITE_LIMIT_CO
1ef20 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64  MPOUND_SELECT</d
1ef30 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1ef40 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1ef50 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f  terms in a compo
1ef60 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65  und SELECT state
1ef70 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ment.</dd>.**.**
1ef80 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49   <dt>SQLITE_LIMI
1ef90 54 5f 56 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a  T_VDBE_OP</dt>.*
1efa0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1efb0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74  m number of inst
1efc0 72 75 63 74 69 6f 6e 73 20 69 6e 20 61 20 76 69  ructions in a vi
1efd0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 72  rtual machine pr
1efe0 6f 67 72 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f  ogram.** used to
1eff0 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51   implement an SQ
1f000 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64  L statement.</dd
1f010 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1f020 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f  TE_LIMIT_FUNCTIO
1f030 4e 5f 41 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  N_ARG</dt>.** <d
1f040 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
1f050 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
1f060 73 20 6f 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e  s on a function.
1f070 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
1f080 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
1f090 41 43 48 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  ACHED</dt>.** <d
1f0a0 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
1f0b0 6d 62 65 72 20 6f 66 20 5b 41 54 54 41 43 48 20  mber of [ATTACH 
1f0c0 7c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  | attached datab
1f0d0 61 73 65 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ases].</dd>.**.*
1f0e0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  * <dt>SQLITE_LIM
1f0f0 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  IT_LIKE_PATTERN_
1f100 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c  LENGTH</dt>.** <
1f110 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c  dd>The maximum l
1f120 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61 74  ength of the pat
1f130 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f  tern argument to
1f140 20 74 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a   the [LIKE] or.*
1f150 2a 20 5b 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f  * [GLOB] operato
1f160 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  rs.</dd>.**.** <
1f170 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1f180 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c  VARIABLE_NUMBER<
1f190 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
1f1a0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1f1b0 66 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 61  f variables in a
1f1c0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
1f1d0 74 68 61 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62  that can.** be b
1f1e0 6f 75 6e 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ound.</dd>.**.**
1f1f0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49   <dt>SQLITE_LIMI
1f200 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 3c  T_TRIGGER_DEPTH<
1f210 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
1f220 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66  maximum depth of
1f230 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 74   recursion for t
1f240 72 69 67 67 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a  riggers.</dd>.**
1f250 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e   </dl>.*/.#defin
1f260 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  e SQLITE_LIMIT_L
1f270 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20  ENGTH           
1f280 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69           0.#defi
1f290 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1f2a0 53 51 4c 5f 4c 45 4e 47 54 48 20 20 20 20 20 20  SQL_LENGTH      
1f2b0 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
1f2c0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
1f2d0 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20  _COLUMN         
1f2e0 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65             2.#de
1f2f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
1f300 54 5f 45 58 50 52 5f 44 45 50 54 48 20 20 20 20  T_EXPR_DEPTH    
1f310 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64              3.#d
1f320 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
1f330 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
1f340 43 54 20 20 20 20 20 20 20 20 20 20 20 34 0a 23  CT           4.#
1f350 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
1f360 4d 49 54 5f 56 44 42 45 5f 4f 50 20 20 20 20 20  MIT_VDBE_OP     
1f370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a                5.
1f380 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
1f390 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  IMIT_FUNCTION_AR
1f3a0 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36  G              6
1f3b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1f3c0 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 20 20  LIMIT_ATTACHED  
1f3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f3e0 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  7.#define SQLITE
1f3f0 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54  _LIMIT_LIKE_PATT
1f400 45 52 4e 5f 4c 45 4e 47 54 48 20 20 20 20 20 20  ERN_LENGTH      
1f410 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   8.#define SQLIT
1f420 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
1f430 5f 4e 55 4d 42 45 52 20 20 20 20 20 20 20 20 20  _NUMBER         
1f440 20 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    9.#define SQLI
1f450 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52  TE_LIMIT_TRIGGER
1f460 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20 20  _DEPTH          
1f470 20 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49    10../*.** CAPI
1f480 33 52 45 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20  3REF: Compiling 
1f490 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74  An SQL Statement
1f4a0 20 7b 48 31 33 30 31 30 7d 20 3c 53 31 30 30 30   {H13010} <S1000
1f4b0 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
1f4c0 7b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63  {SQL statement c
1f4d0 6f 6d 70 69 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54  ompiler}.**.** T
1f4e0 6f 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c  o execute an SQL
1f4f0 20 71 75 65 72 79 2c 20 69 74 20 6d 75 73 74 20   query, it must 
1f500 66 69 72 73 74 20 62 65 20 63 6f 6d 70 69 6c 65  first be compile
1f510 64 20 69 6e 74 6f 20 61 20 62 79 74 65 2d 63 6f  d into a byte-co
1f520 64 65 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73  de.** program us
1f530 69 6e 67 20 6f 6e 65 20 6f 66 20 74 68 65 73 65  ing one of these
1f540 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a   routines..**.**
1f550 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
1f560 65 6e 74 2c 20 22 64 62 22 2c 20 69 73 20 61 20  ent, "db", is a 
1f570 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
1f580 74 69 6f 6e 5d 20 6f 62 74 61 69 6e 65 64 20 66  tion] obtained f
1f590 72 6f 6d 20 61 0a 2a 2a 20 70 72 69 6f 72 20 73  rom a.** prior s
1f5a0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
1f5b0 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  o [sqlite3_open(
1f5c0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
1f5d0 6e 5f 76 32 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73  n_v2()] or.** [s
1f5e0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
1f5f0 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20  .  The database 
1f600 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20  connection must 
1f610 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 63 6c  not have been cl
1f620 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  osed..**.** The 
1f630 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
1f640 20 22 7a 53 71 6c 22 2c 20 69 73 20 74 68 65 20   "zSql", is the 
1f650 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20  statement to be 
1f660 63 6f 6d 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65  compiled, encode
1f670 64 0a 2a 2a 20 61 73 20 65 69 74 68 65 72 20 55  d.** as either U
1f680 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 2e 20  TF-8 or UTF-16. 
1f690 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   The sqlite3_pre
1f6a0 70 61 72 65 28 29 20 61 6e 64 20 73 71 6c 69 74  pare() and sqlit
1f6b0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 0a  e3_prepare_v2().
1f6c0 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 75 73  ** interfaces us
1f6d0 65 20 55 54 46 2d 38 2c 20 61 6e 64 20 73 71 6c  e UTF-8, and sql
1f6e0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29  ite3_prepare16()
1f6f0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65   and sqlite3_pre
1f700 70 61 72 65 31 36 5f 76 32 28 29 0a 2a 2a 20 75  pare16_v2().** u
1f710 73 65 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a  se UTF-16..**.**
1f720 20 49 66 20 74 68 65 20 6e 42 79 74 65 20 61 72   If the nByte ar
1f730 67 75 6d 65 6e 74 20 69 73 20 6c 65 73 73 20 74  gument is less t
1f740 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a  han zero, then z
1f750 53 71 6c 20 69 73 20 72 65 61 64 20 75 70 20 74  Sql is read up t
1f760 6f 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 7a  o the.** first z
1f770 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20  ero terminator. 
1f780 49 66 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d  If nByte is non-
1f790 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 69  negative, then i
1f7a0 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  t is the maximum
1f7b0 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 20 62  .** number of  b
1f7c0 79 74 65 73 20 72 65 61 64 20 66 72 6f 6d 20 7a  ytes read from z
1f7d0 53 71 6c 2e 20 20 57 68 65 6e 20 6e 42 79 74 65  Sql.  When nByte
1f7e0 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   is non-negative
1f7f0 2c 20 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74  , the.** zSql st
1f800 72 69 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74  ring ends at eit
1f810 68 65 72 20 74 68 65 20 66 69 72 73 74 20 27 5c  her the first '\
1f820 30 30 30 27 20 6f 72 20 27 5c 75 30 30 30 30 27  000' or '\u0000'
1f830 20 63 68 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a   character or.**
1f840 20 74 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79   the nByte-th by
1f850 74 65 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f  te, whichever co
1f860 6d 65 73 20 66 69 72 73 74 2e 20 49 66 20 74 68  mes first. If th
1f870 65 20 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a  e caller knows.*
1f880 2a 20 74 68 61 74 20 74 68 65 20 73 75 70 70 6c  * that the suppl
1f890 69 65 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75  ied string is nu
1f8a0 6c 2d 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68  l-terminated, th
1f8b0 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 73 6d  en there is a sm
1f8c0 61 6c 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e  all.** performan
1f8d0 63 65 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20  ce advantage to 
1f8e0 62 65 20 67 61 69 6e 65 64 20 62 79 20 70 61 73  be gained by pas
1f8f0 73 69 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61  sing an nByte pa
1f900 72 61 6d 65 74 65 72 20 74 68 61 74 0a 2a 2a 20  rameter that.** 
1f910 69 73 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  is equal to the 
1f920 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1f930 69 6e 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  in the input str
1f940 69 6e 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67  ing <i>including
1f950 3c 2f 69 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d  </i>.** the nul-
1f960 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73  terminator bytes
1f970 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 7a 54 61 69  ..**.** If pzTai
1f980 6c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68  l is not NULL th
1f990 65 6e 20 2a 70 7a 54 61 69 6c 20 69 73 20 6d 61  en *pzTail is ma
1f9a0 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  de to point to t
1f9b0 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a  he first byte.**
1f9c0 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
1f9d0 20 74 68 65 20 66 69 72 73 74 20 53 51 4c 20 73   the first SQL s
1f9e0 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c  tatement in zSql
1f9f0 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
1fa00 73 20 6f 6e 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c  s only.** compil
1fa10 65 20 74 68 65 20 66 69 72 73 74 20 73 74 61 74  e the first stat
1fa20 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73  ement in zSql, s
1fa30 6f 20 2a 70 7a 54 61 69 6c 20 69 73 20 6c 65 66  o *pzTail is lef
1fa40 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a  t pointing to.**
1fa50 20 77 68 61 74 20 72 65 6d 61 69 6e 73 20 75 6e   what remains un
1fa60 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  compiled..**.** 
1fa70 2a 70 70 53 74 6d 74 20 69 73 20 6c 65 66 74 20  *ppStmt is left 
1fa80 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f  pointing to a co
1fa90 6d 70 69 6c 65 64 20 5b 70 72 65 70 61 72 65 64  mpiled [prepared
1faa0 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74   statement] that
1fab0 20 63 61 6e 20 62 65 0a 2a 2a 20 65 78 65 63 75   can be.** execu
1fac0 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ted using [sqlit
1fad0 65 33 5f 73 74 65 70 28 29 5d 2e 20 20 49 66 20  e3_step()].  If 
1fae0 74 68 65 72 65 20 69 73 20 61 6e 20 65 72 72 6f  there is an erro
1faf0 72 2c 20 2a 70 70 53 74 6d 74 20 69 73 20 73 65  r, *ppStmt is se
1fb00 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 20 20 49  t.** to NULL.  I
1fb10 66 20 74 68 65 20 69 6e 70 75 74 20 74 65 78 74  f the input text
1fb20 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 53 51 4c   contains no SQL
1fb30 20 28 69 66 20 74 68 65 20 69 6e 70 75 74 20 69   (if the input i
1fb40 73 20 61 6e 20 65 6d 70 74 79 0a 2a 2a 20 73 74  s an empty.** st
1fb50 72 69 6e 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e  ring or a commen
1fb60 74 29 20 74 68 65 6e 20 2a 70 70 53 74 6d 74 20  t) then *ppStmt 
1fb70 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a  is set to NULL..
1fb80 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70  ** The calling p
1fb90 72 6f 63 65 64 75 72 65 20 69 73 20 72 65 73 70  rocedure is resp
1fba0 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 6c 65  onsible for dele
1fbb0 74 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 65  ting the compile
1fbc0 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65  d.** SQL stateme
1fbd0 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  nt using [sqlite
1fbe0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 61 66  3_finalize()] af
1fbf0 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69 73  ter it has finis
1fc00 68 65 64 20 77 69 74 68 20 69 74 2e 0a 2a 2a 20  hed with it..** 
1fc10 70 70 53 74 6d 74 20 6d 61 79 20 6e 6f 74 20 62  ppStmt may not b
1fc20 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  e NULL..**.** On
1fc30 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54   success, [SQLIT
1fc40 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65  E_OK] is returne
1fc50 64 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20  d, otherwise an 
1fc60 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20  [error code] is 
1fc70 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
1fc80 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  The sqlite3_prep
1fc90 61 72 65 5f 76 32 28 29 20 61 6e 64 20 73 71 6c  are_v2() and sql
1fca0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
1fcb0 32 28 29 20 69 6e 74 65 72 66 61 63 65 73 20 61  2() interfaces a
1fcc0 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 65  re.** recommende
1fcd0 64 20 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70 72  d for all new pr
1fce0 6f 67 72 61 6d 73 2e 20 54 68 65 20 74 77 6f 20  ograms. The two 
1fcf0 6f 6c 64 65 72 20 69 6e 74 65 72 66 61 63 65 73  older interfaces
1fd00 20 61 72 65 20 72 65 74 61 69 6e 65 64 0a 2a 2a   are retained.**
1fd10 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20 63   for backwards c
1fd20 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 62 75  ompatibility, bu
1fd30 74 20 74 68 65 69 72 20 75 73 65 20 69 73 20 64  t their use is d
1fd40 69 73 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 49  iscouraged..** I
1fd50 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72  n the "v2" inter
1fd60 66 61 63 65 73 2c 20 74 68 65 20 70 72 65 70 61  faces, the prepa
1fd70 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  red statement.**
1fd80 20 74 68 61 74 20 69 73 20 72 65 74 75 72 6e 65   that is returne
1fd90 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  d (the [sqlite3_
1fda0 73 74 6d 74 5d 20 6f 62 6a 65 63 74 29 20 63 6f  stmt] object) co
1fdb0 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20 6f 66  ntains a copy of
1fdc0 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
1fdd0 20 53 51 4c 20 74 65 78 74 2e 20 54 68 69 73 20   SQL text. This 
1fde0 63 61 75 73 65 73 20 74 68 65 20 5b 73 71 6c 69  causes the [sqli
1fdf0 74 65 33 5f 73 74 65 70 28 29 5d 20 69 6e 74 65  te3_step()] inte
1fe00 72 66 61 63 65 20 74 6f 0a 2a 2a 20 62 65 68 61  rface to.** beha
1fe10 76 65 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69  ve differently i
1fe20 6e 20 74 68 72 65 65 20 77 61 79 73 3a 0a 2a 2a  n three ways:.**
1fe30 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ol>.** <li>
1fe40 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62  .** If the datab
1fe50 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ase schema chang
1fe60 65 73 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72  es, instead of r
1fe70 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45  eturning [SQLITE
1fe80 5f 53 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a  _SCHEMA] as it.*
1fe90 2a 20 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f  * always used to
1fea0 20 64 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74   do, [sqlite3_st
1feb0 65 70 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d  ep()] will autom
1fec0 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69  atically recompi
1fed0 6c 65 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74  le the SQL.** st
1fee0 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20  atement and try 
1fef0 74 6f 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e  to run it again.
1ff00 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 20    If the schema 
1ff10 68 61 73 20 63 68 61 6e 67 65 64 20 69 6e 0a 2a  has changed in.*
1ff20 2a 20 61 20 77 61 79 20 74 68 61 74 20 6d 61 6b  * a way that mak
1ff30 65 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  es the statement
1ff40 20 6e 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64   no longer valid
1ff50 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  , [sqlite3_step(
1ff60 29 5d 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a  )] will still.**
1ff70 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
1ff80 53 43 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e  SCHEMA].  But un
1ff90 6c 69 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20  like the legacy 
1ffa0 62 65 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54  behavior, [SQLIT
1ffb0 45 5f 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20  E_SCHEMA] is.** 
1ffc0 6e 6f 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f  now a fatal erro
1ffd0 72 2e 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c  r.  Calling [sql
1ffe0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
1fff0 29 5d 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f  )] again will no
20000 74 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72  t make the.** er
20010 72 6f 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f  ror go away.  No
20020 74 65 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33  te: use [sqlite3
20030 5f 65 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69  _errmsg()] to fi
20040 6e 64 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f  nd the text.** o
20050 66 20 74 68 65 20 70 61 72 73 69 6e 67 20 65 72  f the parsing er
20060 72 6f 72 20 74 68 61 74 20 72 65 73 75 6c 74 73  ror that results
20070 20 69 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53   in an [SQLITE_S
20080 43 48 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a  CHEMA] return..*
20090 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c  * </li>.**.** <l
200a0 69 3e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72  i>.** When an er
200b0 72 6f 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 6c  ror occurs, [sql
200c0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c  ite3_step()] wil
200d0 6c 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20  l return one of 
200e0 74 68 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20  the detailed.** 
200f0 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f 72  [error codes] or
20100 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72   [extended error
20110 20 63 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c 65   codes].  The le
20120 67 61 63 79 20 62 65 68 61 76 69 6f 72 20 77 61  gacy behavior wa
20130 73 20 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 74  s that.** [sqlit
20140 65 33 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64  e3_step()] would
20150 20 6f 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67   only return a g
20160 65 6e 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45  eneric [SQLITE_E
20170 52 52 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64  RROR] result cod
20180 65 0a 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f 75  e.** and you wou
20190 6c 64 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20  ld have to make 
201a0 61 20 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f  a second call to
201b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
201c0 29 5d 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74  )] in order.** t
201d0 6f 20 66 69 6e 64 20 74 68 65 20 75 6e 64 65 72  o find the under
201e0 6c 79 69 6e 67 20 63 61 75 73 65 20 6f 66 20 74  lying cause of t
201f0 68 65 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 68  he problem. With
20200 20 74 68 65 20 22 76 32 22 20 70 72 65 70 61 72   the "v2" prepar
20210 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2c  e.** interfaces,
20220 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
20230 72 65 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 65  reason for the e
20240 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
20250 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a   immediately..**
20260 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69   </li>.**.** <li
20270 3e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 76 61 6c  >.** ^If the val
20280 75 65 20 6f 66 20 61 20 5b 70 61 72 61 6d 65 74  ue of a [paramet
20290 65 72 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65  er | host parame
202a0 74 65 72 5d 20 69 6e 20 74 68 65 20 57 48 45 52  ter] in the WHER
202b0 45 20 63 6c 61 75 73 65 20 6d 69 67 68 74 0a 2a  E clause might.*
202c0 2a 20 63 68 61 6e 67 65 20 74 68 65 20 71 75 65  * change the que
202d0 72 79 20 70 6c 61 6e 20 66 6f 72 20 61 20 73 74  ry plan for a st
202e0 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68  atement, then th
202f0 65 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20  e statement may 
20300 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61  be.** automatica
20310 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 28  lly recompiled (
20320 61 73 20 69 66 20 74 68 65 72 65 20 68 61 64 20  as if there had 
20330 62 65 65 6e 20 61 20 73 63 68 65 6d 61 20 63 68  been a schema ch
20340 61 6e 67 65 29 20 6f 6e 20 74 68 65 20 66 69 72  ange) on the fir
20350 73 74 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  st .** [sqlite3_
20360 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f 6c  step()] call fol
20370 6c 6f 77 69 6e 67 20 61 6e 79 20 63 68 61 6e 67  lowing any chang
20380 65 20 74 6f 20 74 68 65 20 0a 2a 2a 20 5b 73 71  e to the .** [sq
20390 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20  lite3_bind_text 
203a0 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20 74  | bindings] of t
203b0 68 65 20 5b 70 61 72 61 6d 65 74 65 72 5d 2e 20  he [parameter]. 
203c0 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f  .** </li>.** </o
203d0 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  l>.**.** Require
203e0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 31  ments:.** [H1301
203f0 31 5d 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 33  1] [H13012] [H13
20400 30 31 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 48  013] [H13014] [H
20410 31 33 30 31 35 5d 20 5b 48 31 33 30 31 36 5d 20  13015] [H13016] 
20420 5b 48 31 33 30 31 39 5d 20 5b 48 31 33 30 32 31  [H13019] [H13021
20430 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ].**.*/.SQLITE_A
20440 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
20450 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65  repare(.  sqlite
20460 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
20470 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
20480 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
20490 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
204a0 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    /* SQL stateme
204b0 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  nt, UTF-8 encode
204c0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  d */.  int nByte
204d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
204e0 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
204f0 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
20500 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
20510 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
20520 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e  /* OUT: Statemen
20530 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  t handle */.  co
20540 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69  nst char **pzTai
20550 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f  l     /* OUT: Po
20560 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20  inter to unused 
20570 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20  portion of zSql 
20580 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  */.);.SQLITE_API
20590 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
205a0 70 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74  pare_v2(.  sqlit
205b0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
205c0 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
205d0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
205e0 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
205f0 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d     /* SQL statem
20600 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64  ent, UTF-8 encod
20610 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  ed */.  int nByt
20620 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
20630 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
20640 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
20650 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
20660 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
20670 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
20680 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  nt handle */.  c
20690 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
206a0 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50  il     /* OUT: P
206b0 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64  ointer to unused
206c0 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c   portion of zSql
206d0 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50   */.);.SQLITE_AP
206e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72  I int sqlite3_pr
206f0 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74  epare16(.  sqlit
20700 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
20710 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
20720 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
20730 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20   void *zSql,    
20740 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d     /* SQL statem
20750 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f  ent, UTF-16 enco
20760 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  ded */.  int nBy
20770 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
20780 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67   /* Maximum leng
20790 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79  th of zSql in by
207a0 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tes. */.  sqlite
207b0 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c  3_stmt **ppStmt,
207c0 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d    /* OUT: Statem
207d0 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ent handle */.  
207e0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54  const void **pzT
207f0 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  ail     /* OUT: 
20800 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65  Pointer to unuse
20810 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71  d portion of zSq
20820 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  l */.);.SQLITE_A
20830 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
20840 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73  repare16_v2(.  s
20850 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
20860 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
20870 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  se handle */.  c
20880 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c  onst void *zSql,
20890 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74         /* SQL st
208a0 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20  atement, UTF-16 
208b0 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74  encoded */.  int
208c0 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20   nByte,         
208d0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
208e0 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  length of zSql i
208f0 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
20900 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
20910 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74  tmt,  /* OUT: St
20920 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a  atement handle *
20930 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
20940 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f  *pzTail     /* O
20950 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75  UT: Pointer to u
20960 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66  nused portion of
20970 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a   zSql */.);../*.
20980 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 74  ** CAPI3REF: Ret
20990 72 69 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e  rieving Statemen
209a0 74 20 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 3c  t SQL {H13100} <
209b0 48 31 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  H13000>.**.** Th
209c0 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e  is interface can
209d0 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 74 72   be used to retr
209e0 69 65 76 65 20 61 20 73 61 76 65 64 20 63 6f 70  ieve a saved cop
209f0 79 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  y of the origina
20a00 6c 0a 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73  l.** SQL text us
20a10 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b  ed to create a [
20a20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
20a30 6e 74 5d 20 69 66 20 74 68 61 74 20 73 74 61 74  nt] if that stat
20a40 65 6d 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f 6d  ement was.** com
20a50 70 69 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68  piled using eith
20a60 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  er [sqlite3_prep
20a70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71  are_v2()] or [sq
20a80 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
20a90 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  v2()]..**.** Req
20aa0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
20ab0 31 33 31 30 31 5d 20 5b 48 31 33 31 30 32 5d 20  13101] [H13102] 
20ac0 5b 48 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c 49  [H13103].*/.SQLI
20ad0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
20ae0 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73  r *sqlite3_sql(s
20af0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
20b00 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  mt);../*.** CAPI
20b10 33 52 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c  3REF: Dynamicall
20b20 79 20 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62  y Typed Value Ob
20b30 6a 65 63 74 20 7b 48 31 35 30 30 30 7d 20 3c 53  ject {H15000} <S
20b40 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  20200>.** KEYWOR
20b50 44 53 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 73  DS: {protected s
20b60 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75  qlite3_value} {u
20b70 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
20b80 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20  e3_value}.**.** 
20b90 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 65 20  SQLite uses the 
20ba0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
20bb0 6a 65 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e  ject to represen
20bc0 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a 20  t all values.** 
20bd0 74 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72  that can be stor
20be0 65 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 65  ed in a database
20bf0 20 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75   table. SQLite u
20c00 73 65 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69  ses dynamic typi
20c10 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 61  ng.** for the va
20c20 6c 75 65 73 20 69 74 20 73 74 6f 72 65 73 2e 20  lues it stores. 
20c30 56 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e  Values stored in
20c40 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
20c50 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 65  bjects.** can be
20c60 20 69 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 74   integers, float
20c70 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73  ing point values
20c80 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 73  , strings, BLOBs
20c90 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  , or NULL..**.**
20ca0 20 41 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   An sqlite3_valu
20cb0 65 20 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20  e object may be 
20cc0 65 69 74 68 65 72 20 22 70 72 6f 74 65 63 74 65  either "protecte
20cd0 64 22 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 74  d" or "unprotect
20ce0 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e 74  ed"..** Some int
20cf0 65 72 66 61 63 65 73 20 72 65 71 75 69 72 65 20  erfaces require 
20d00 61 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  a protected sqli
20d10 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 65  te3_value.  Othe
20d20 72 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  r interfaces.** 
20d30 77 69 6c 6c 20 61 63 63 65 70 74 20 65 69 74 68  will accept eith
20d40 65 72 20 61 20 70 72 6f 74 65 63 74 65 64 20 6f  er a protected o
20d50 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64  r an unprotected
20d60 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a   sqlite3_value..
20d70 2a 2a 20 45 76 65 72 79 20 69 6e 74 65 72 66 61  ** Every interfa
20d80 63 65 20 74 68 61 74 20 61 63 63 65 70 74 73 20  ce that accepts 
20d90 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 72  sqlite3_value ar
20da0 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65  guments specifie
20db0 73 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20  s.** whether or 
20dc0 6e 6f 74 20 69 74 20 72 65 71 75 69 72 65 73 20  not it requires 
20dd0 61 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  a protected sqli
20de0 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  te3_value..**.**
20df0 20 54 68 65 20 74 65 72 6d 73 20 22 70 72 6f 74   The terms "prot
20e00 65 63 74 65 64 22 20 61 6e 64 20 22 75 6e 70 72  ected" and "unpr
20e10 6f 74 65 63 74 65 64 22 20 72 65 66 65 72 20 74  otected" refer t
20e20 6f 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  o whether or not
20e30 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69 73 20 68  .** a mutex is h
20e40 65 6c 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 6c  eld.  A internal
20e50 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66   mutex is held f
20e60 6f 72 20 61 20 70 72 6f 74 65 63 74 65 64 0a 2a  or a protected.*
20e70 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
20e80 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75  object but no mu
20e90 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20  tex is held for 
20ea0 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a  an unprotected.*
20eb0 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
20ec0 6f 62 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69  object.  If SQLi
20ed0 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74  te is compiled t
20ee0 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65  o be single-thre
20ef0 61 64 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b 53  aded.** (with [S
20f00 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
20f10 3d 30 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 71  =0] and with [sq
20f20 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65  lite3_threadsafe
20f30 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 29  ()] returning 0)
20f40 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 65  .** or if SQLite
20f50 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 6f   is run in one o
20f60 66 20 72 65 64 75 63 65 64 20 6d 75 74 65 78 20  f reduced mutex 
20f70 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 54  modes .** [SQLIT
20f80 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54  E_CONFIG_SINGLET
20f90 48 52 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 54  HREAD] or [SQLIT
20fa0 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48  E_CONFIG_MULTITH
20fb0 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 68  READ].** then th
20fc0 65 72 65 20 69 73 20 6e 6f 20 64 69 73 74 69 6e  ere is no distin
20fd0 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 72  ction between pr
20fe0 6f 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72  otected and unpr
20ff0 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74  otected.** sqlit
21000 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73  e3_value objects
21010 20 61 6e 64 20 74 68 65 79 20 63 61 6e 20 62 65   and they can be
21020 20 75 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67   used interchang
21030 65 61 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c  eably.  However,
21040 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20  .** for maximum 
21050 63 6f 64 65 20 70 6f 72 74 61 62 69 6c 69 74 79  code portability
21060 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64   it is recommend
21070 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74  ed that applicat
21080 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d 61  ions.** still ma
21090 6b 65 20 74 68 65 20 64 69 73 74 69 6e 63 74 69  ke the distincti
210a0 6f 6e 20 62 65 74 77 65 65 6e 20 62 65 74 77 65  on between betwe
210b0 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64  en protected and
210c0 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20   unprotected.** 
210d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
210e0 6a 65 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20  jects even when 
210f0 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 72 65 71  not strictly req
21100 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  uired..**.** The
21110 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
21120 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20  bjects that are 
21130 70 61 73 73 65 64 20 61 73 20 70 61 72 61 6d 65  passed as parame
21140 74 65 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a 2a  ters into the.**
21150 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
21160 6f 66 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d  of [application-
21170 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63  defined SQL func
21180 74 69 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 65  tions] are prote
21190 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  cted..** The sql
211a0 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
211b0 74 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  t returned by.**
211c0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
211d0 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 75 6e 70  _value()] is unp
211e0 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e 70  rotected..** Unp
211f0 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
21200 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 6d  _value objects m
21210 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20  ay only be used 
21220 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  with.** [sqlite3
21230 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d  _result_value()]
21240 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69   and [sqlite3_bi
21250 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20  nd_value()]..** 
21260 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  The [sqlite3_val
21270 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65  ue_blob | sqlite
21280 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 29 5d 20  3_value_type()] 
21290 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e 74  family of.** int
212a0 65 72 66 61 63 65 73 20 72 65 71 75 69 72 65 20  erfaces require 
212b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
212c0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e  3_value objects.
212d0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
212e0 63 74 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76  ct Mem sqlite3_v
212f0 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  alue;../*.** CAP
21300 49 33 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74  I3REF: SQL Funct
21310 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65  ion Context Obje
21320 63 74 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 30  ct {H16001} <S20
21330 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  200>.**.** The c
21340 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20  ontext in which 
21350 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  an SQL function 
21360 65 78 65 63 75 74 65 73 20 69 73 20 73 74 6f 72  executes is stor
21370 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69  ed in an.** sqli
21380 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65  te3_context obje
21390 63 74 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74  ct.  A pointer t
213a0 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6e  o an sqlite3_con
213b0 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 69  text object.** i
213c0 73 20 61 6c 77 61 79 73 20 66 69 72 73 74 20 70  s always first p
213d0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 70  arameter to [app
213e0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
213f0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 2e   SQL functions].
21400 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74  .** The applicat
21410 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20  ion-defined SQL 
21420 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65  function impleme
21430 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 73  ntation will pas
21440 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 65  s this.** pointe
21450 72 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 63  r through into c
21460 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
21470 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 71  _result_int | sq
21480 6c 69 74 65 33 5f 72 65 73 75 6c 74 28 29 5d 2c  lite3_result()],
21490 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 67  .** [sqlite3_agg
214a0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29  regate_context()
214b0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72  ], [sqlite3_user
214c0 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 71  _data()],.** [sq
214d0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
214e0 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 6c  _handle()], [sql
214f0 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
21500 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b  ()],.** and/or [
21510 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
21520 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64  ata()]..*/.typed
21530 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
21540 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 65  3_context sqlite
21550 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a  3_context;../*.*
21560 2a 20 43 41 50 49 33 52 45 46 3a 20 42 69 6e 64  * CAPI3REF: Bind
21570 69 6e 67 20 56 61 6c 75 65 73 20 54 6f 20 50 72  ing Values To Pr
21580 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
21590 73 20 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 33  s {H13500} <S703
215a0 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  00>.** KEYWORDS:
215b0 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72   {host parameter
215c0 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65  } {host paramete
215d0 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65  rs} {host parame
215e0 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 59  ter name}.** KEY
215f0 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 61  WORDS: {SQL para
21600 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 61  meter} {SQL para
21610 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 74  meters} {paramet
21620 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a  er binding}.**.*
21630 2a 20 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72  * In the SQL str
21640 69 6e 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73  ings input to [s
21650 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
21660 32 28 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72  2()] and its var
21670 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 61  iants,.** litera
21680 6c 73 20 6d 61 79 20 62 65 20 72 65 70 6c 61 63  ls may be replac
21690 65 64 20 62 79 20 61 20 5b 70 61 72 61 6d 65 74  ed by a [paramet
216a0 65 72 5d 20 74 68 61 74 20 6d 61 74 63 68 65 73  er] that matches
216b0 20 6f 6e 65 20 6f 66 20 66 6f 6c 6c 6f 77 69 6e   one of followin
216c0 67 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a  g.** templates:.
216d0 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
216e0 69 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f  i>  ?.** <li>  ?
216f0 4e 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56  NNN.** <li>  :VV
21700 56 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a  V.** <li>  @VVV.
21710 2a 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a  ** <li>  $VVV.**
21720 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20   </ul>.**.** In 
21730 74 68 65 20 74 65 6d 70 6c 61 74 65 73 20 61 62  the templates ab
21740 6f 76 65 2c 20 4e 4e 4e 20 72 65 70 72 65 73 65  ove, NNN represe
21750 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c  nts an integer l
21760 69 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56  iteral,.** and V
21770 56 56 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e  VV represents an
21780 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 69 64   alphanumeric id
21790 65 6e 74 69 66 65 72 2e 20 20 54 68 65 20 76 61  entifer.  The va
217a0 6c 75 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a  lues of these.**
217b0 20 70 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73   parameters (als
217c0 6f 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70  o called "host p
217d0 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20  arameter names" 
217e0 6f 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65  or "SQL paramete
217f0 72 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73  rs").** can be s
21800 65 74 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  et using the sql
21810 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f  ite3_bind_*() ro
21820 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68  utines defined h
21830 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ere..**.** The f
21840 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
21850 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   the sqlite3_bin
21860 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69  d_*() routines i
21870 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f  s always.** a po
21880 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71  inter to the [sq
21890 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65  lite3_stmt] obje
218a0 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  ct returned from
218b0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
218c0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74  pare_v2()] or it
218d0 73 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a  s variants..**.*
218e0 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
218f0 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64  ument is the ind
21900 65 78 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61  ex of the SQL pa
21910 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65  rameter to be se
21920 74 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f  t..** The leftmo
21930 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  st SQL parameter
21940 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
21950 20 31 2e 20 20 57 68 65 6e 20 74 68 65 20 73 61   1.  When the sa
21960 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20  me named.** SQL 
21970 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65  parameter is use
21980 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  d more than once
21990 2c 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62  , second and sub
219a0 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72  sequent.** occur
219b0 72 65 6e 63 65 73 20 68 61 76 65 20 74 68 65 20  rences have the 
219c0 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68  same index as th
219d0 65 20 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e  e first occurren
219e0 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78  ce..** The index
219f0 20 66 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d   for named param
21a00 65 74 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f  eters can be loo
21a10 6b 65 64 20 75 70 20 75 73 69 6e 67 20 74 68 65  ked up using the
21a20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
21a30 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
21a40 78 28 29 5d 20 41 50 49 20 69 66 20 64 65 73 69  x()] API if desi
21a50 72 65 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a  red.  The index.
21a60 2a 2a 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61  ** for "?NNN" pa
21a70 72 61 6d 65 74 65 72 73 20 69 73 20 74 68 65 20  rameters is the 
21a80 76 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a  value of NNN..**
21a90 20 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d   The NNN value m
21aa0 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 31  ust be between 1
21ab0 20 61 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65   and the [sqlite
21ac0 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61  3_limit()].** pa
21ad0 72 61 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f  rameter [SQLITE_
21ae0 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
21af0 55 4d 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20  UMBER] (default 
21b00 76 61 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a  value: 999)..**.
21b10 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67  ** The third arg
21b20 75 6d 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c  ument is the val
21b30 75 65 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68  ue to bind to th
21b40 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a  e parameter..**.
21b50 2a 2a 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 74  ** In those rout
21b60 69 6e 65 73 20 74 68 61 74 20 68 61 76 65 20 61  ines that have a
21b70 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74   fourth argument
21b80 2c 20 69 74 73 20 76 61 6c 75 65 20 69 73 20 74  , its value is t
21b90 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
21ba0 62 79 74 65 73 20 69 6e 20 74 68 65 20 70 61 72  bytes in the par
21bb0 61 6d 65 74 65 72 2e 20 20 54 6f 20 62 65 20 63  ameter.  To be c
21bc0 6c 65 61 72 3a 20 74 68 65 20 76 61 6c 75 65 20  lear: the value 
21bd0 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  is the.** number
21be0 20 6f 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e   of <u>bytes</u>
21bf0 20 69 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 6e   in the value, n
21c00 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ot the number of
21c10 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20   characters..** 
21c20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61  If the fourth pa
21c30 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74  rameter is negat
21c40 69 76 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20  ive, the length 
21c50 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  of the string is
21c60 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
21c70 66 20 62 79 74 65 73 20 75 70 20 74 6f 20 74 68  f bytes up to th
21c80 65 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72  e first zero ter
21c90 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  minator..**.** T
21ca0 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e  he fifth argumen
21cb0 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e  t to sqlite3_bin
21cc0 64 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65  d_blob(), sqlite
21cd0 33 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61  3_bind_text(), a
21ce0 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69  nd.** sqlite3_bi
21cf0 6e 64 5f 74 65 78 74 31 36 28 29 20 69 73 20 61  nd_text16() is a
21d00 20 64 65 73 74 72 75 63 74 6f 72 20 75 73 65 64   destructor used
21d10 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74   to dispose of t
21d20 68 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74  he BLOB or.** st
21d30 72 69 6e 67 20 61 66 74 65 72 20 53 51 4c 69 74  ring after SQLit
21d40 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77  e has finished w
21d50 69 74 68 20 69 74 2e 20 49 66 20 74 68 65 20 66  ith it. If the f
21d60 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73  ifth argument is
21d70 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20  .** the special 
21d80 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54  value [SQLITE_ST
21d90 41 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69  ATIC], then SQLi
21da0 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  te assumes that 
21db0 74 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  the.** informati
21dc0 6f 6e 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c  on is in static,
21dd0 20 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65   unmanaged space
21de0 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65   and does not ne
21df0 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a  ed to be freed..
21e00 2a 2a 20 49 66 20 74 68 65 20 66 69 66 74 68 20  ** If the fifth 
21e10 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65  argument has the
21e20 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54   value [SQLITE_T
21e30 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a  RANSIENT], then.
21e40 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20  ** SQLite makes 
21e50 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20  its own private 
21e60 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74 61  copy of the data
21e70 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65   immediately, be
21e80 66 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69  fore.** the sqli
21e90 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75  te3_bind_*() rou
21ea0 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a  tine returns..**
21eb0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
21ec0 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20  bind_zeroblob() 
21ed0 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20  routine binds a 
21ee0 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e  BLOB of length N
21ef0 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c   that.** is fill
21f00 65 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20  ed with zeroes. 
21f10 20 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73   A zeroblob uses
21f20 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20   a fixed amount 
21f30 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75  of memory.** (ju
21f40 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f  st an integer to
21f50 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 20   hold its size) 
21f60 77 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e  while it is bein
21f70 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20  g processed..** 
21f80 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e  Zeroblobs are in
21f90 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20  tended to serve 
21fa0 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20  as placeholders 
21fb0 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a  for BLOBs whose.
21fc0 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61  ** content is la
21fd0 74 65 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e  ter written usin
21fe0 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c  g.** [sqlite3_bl
21ff0 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d  ob_open | increm
22000 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20  ental BLOB I/O] 
22010 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e  routines..** A n
22020 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 66 6f  egative value fo
22030 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72  r the zeroblob r
22040 65 73 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f  esults in a zero
22050 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a  -length BLOB..**
22060 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
22070 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65  bind_*() routine
22080 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  s must be called
22090 20 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74   after.** [sqlit
220a0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
220b0 20 28 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e   (and its varian
220c0 74 73 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ts) or [sqlite3_
220d0 72 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20  reset()] and.** 
220e0 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f  before [sqlite3_
220f0 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64  step()]..** Bind
22100 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65  ings are not cle
22110 61 72 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c  ared by the [sql
22120 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f  ite3_reset()] ro
22130 75 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e  utine..** Unboun
22140 64 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  d parameters are
22150 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
22160 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  NULL..**.** Thes
22170 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
22180 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e  n [SQLITE_OK] on
22190 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65   success or an e
221a0 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20  rror code if.** 
221b0 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72  anything goes wr
221c0 6f 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41  ong.  [SQLITE_RA
221d0 4e 47 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64  NGE] is returned
221e0 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   if the paramete
221f0 72 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75  r.** index is ou
22200 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51  t of range.  [SQ
22210 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72  LITE_NOMEM] is r
22220 65 74 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f  eturned if mallo
22230 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53  c() fails..** [S
22240 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69  QLITE_MISUSE] mi
22250 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64 20  ght be returned 
22260 69 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  if these routine
22270 73 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20  s are called on 
22280 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63  a.** virtual mac
22290 68 69 6e 65 20 74 68 61 74 20 69 73 20 74 68 65  hine that is the
222a0 20 77 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 20   wrong state or 
222b0 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 61 64  which has alread
222c0 79 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64  y been finalized
222d0 2e 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f  ..** Detection o
222e0 66 20 6d 69 73 75 73 65 20 69 73 20 75 6e 72 65  f misuse is unre
222f0 6c 69 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 61  liable.  Applica
22300 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74  tions should not
22310 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51   depend.** on SQ
22320 4c 49 54 45 5f 4d 49 53 55 53 45 20 72 65 74 75  LITE_MISUSE retu
22330 72 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53  rns.  SQLITE_MIS
22340 55 53 45 20 69 73 20 69 6e 74 65 6e 64 65 64 20  USE is intended 
22350 74 6f 20 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a  to indicate a.**
22360 20 61 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69   a logic error i
22370 6e 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  n the applicatio
22380 6e 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69  n.  Future versi
22390 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69  ons of SQLite mi
223a0 67 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74  ght.** panic rat
223b0 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20  her than return 
223c0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a  SQLITE_MISUSE..*
223d0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
223e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
223f0 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c  ameter_count()],
22400 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
22410 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
22420 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
22430 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
22440 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a  _index()]..**.**
22450 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
22460 2a 20 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 35  * [H13506] [H135
22470 30 39 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 31  09] [H13512] [H1
22480 33 35 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 5b  3515] [H13518] [
22490 48 31 33 35 32 31 5d 20 5b 48 31 33 35 32 34 5d  H13521] [H13524]
224a0 20 5b 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 31   [H13527].** [H1
224b0 33 35 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 5b  3530] [H13533] [
224c0 48 31 33 35 33 36 5d 20 5b 48 31 33 35 33 39 5d  H13536] [H13539]
224d0 20 5b 48 31 33 35 34 32 5d 20 5b 48 31 33 35 34   [H13542] [H1354
224e0 35 5d 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 33  5] [H13548] [H13
224f0 35 35 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  551].**.*/.SQLIT
22500 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
22510 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69  3_bind_blob(sqli
22520 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
22530 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
22540 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64   n, void(*)(void
22550 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
22560 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
22570 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
22580 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62  stmt*, int, doub
22590 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  le);.SQLITE_API 
225a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
225b0 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  _int(sqlite3_stm
225c0 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  t*, int, int);.S
225d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
225e0 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
225f0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
22600 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  int, sqlite3_int
22610 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  64);.SQLITE_API 
22620 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
22630 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74  _null(sqlite3_st
22640 6d 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  mt*, int);.SQLIT
22650 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
22660 33 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69  3_bind_text(sqli
22670 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
22680 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
22690 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64   n, void(*)(void
226a0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
226b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
226c0 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f  _text16(sqlite3_
226d0 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  stmt*, int, cons
226e0 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f  t void*, int, vo
226f0 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
22700 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
22710 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65  lite3_bind_value
22720 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
22730 69 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74  int, const sqlit
22740 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
22750 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22760 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
22770 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
22780 69 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a  int, int n);../*
22790 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75  .** CAPI3REF: Nu
227a0 6d 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61  mber Of SQL Para
227b0 6d 65 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20  meters {H13600} 
227c0 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S70300>.**.** T
227d0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20  his routine can 
227e0 62 65 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20  be used to find 
227f0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53  the number of [S
22800 51 4c 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a  QL parameters].*
22810 2a 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64  * in a [prepared
22820 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51   statement].  SQ
22830 4c 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  L parameters are
22840 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a   tokens of the.*
22850 2a 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e  * form "?", "?NN
22860 4e 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41  N", ":AAA", "$AA
22870 41 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68  A", or "@AAA" th
22880 61 74 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70  at serve as.** p
22890 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20  laceholders for 
228a0 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20  values that are 
228b0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
228c0 6f 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74  ob | bound].** t
228d0 6f 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73  o the parameters
228e0 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65   at a later time
228f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
22900 74 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65  tine actually re
22910 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20  turns the index 
22920 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 28  of the largest (
22930 72 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61  rightmost).** pa
22940 72 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c  rameter. For all
22950 20 66 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e   forms except ?N
22960 4e 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f  NN, this will co
22970 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a  rrespond to the.
22980 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69  ** number of uni
22990 71 75 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20  que parameters. 
229a0 20 49 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f   If parameters o
229b0 66 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75  f the ?NNN are u
229c0 73 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61  sed,.** there ma
229d0 79 20 62 65 20 67 61 70 73 20 69 6e 20 74 68 65  y be gaps in the
229e0 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65   list..**.** See
229f0 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
22a00 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65  bind_blob|sqlite
22a10 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73  3_bind()],.** [s
22a20 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
22a30 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61  meter_name()], a
22a40 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  nd.** [sqlite3_b
22a50 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
22a60 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65  dex()]..**.** Re
22a70 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
22a80 48 31 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 54  H13601].*/.SQLIT
22a90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
22aa0 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
22ab0 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73  _count(sqlite3_s
22ac0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tmt*);../*.** CA
22ad0 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20  PI3REF: Name Of 
22ae0 41 20 48 6f 73 74 20 50 61 72 61 6d 65 74 65 72  A Host Parameter
22af0 20 7b 48 31 33 36 32 30 7d 20 3c 53 37 30 33 30   {H13620} <S7030
22b00 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0>.**.** This ro
22b10 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20  utine returns a 
22b20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
22b30 61 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a  ame of the n-th.
22b40 2a 2a 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65  ** [SQL paramete
22b50 72 5d 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65  r] in a [prepare
22b60 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a  d statement]..**
22b70 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20   SQL parameters 
22b80 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e  of the form "?NN
22b90 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20  N" or ":AAA" or 
22ba0 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22  "@AAA" or "$AAA"
22bb0 0a 2a 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 20  .** have a name 
22bc0 77 68 69 63 68 20 69 73 20 74 68 65 20 73 74 72  which is the str
22bd0 69 6e 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a  ing "?NNN" or ":
22be0 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f  AAA" or "@AAA" o
22bf0 72 20 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 70  r "$AAA".** resp
22c00 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20  ectively..** In 
22c10 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
22c20 20 69 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 20   initial ":" or 
22c30 22 24 22 20 6f 72 20 22 40 22 20 6f 72 20 22 3f  "$" or "@" or "?
22c40 22 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64  ".** is included
22c50 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
22c60 6e 61 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74  name..** Paramet
22c70 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ers of the form 
22c80 22 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f  "?" without a fo
22c90 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20  llowing integer 
22ca0 68 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20  have no name.** 
22cb0 61 6e 64 20 61 72 65 20 61 6c 73 6f 20 72 65 66  and are also ref
22cc0 65 72 72 65 64 20 74 6f 20 61 73 20 22 61 6e 6f  erred to as "ano
22cd0 6e 79 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 72  nymous parameter
22ce0 73 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  s"..**.** The fi
22cf0 72 73 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74  rst host paramet
22d00 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  er has an index 
22d10 6f 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a  of 1, not 0..**.
22d20 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
22d30 6e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  n is out of rang
22d40 65 20 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68  e or if the n-th
22d50 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a   parameter is.**
22d60 20 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20   nameless, then 
22d70 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
22d80 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  .  The returned 
22d90 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77  string is.** alw
22da0 61 79 73 20 69 6e 20 55 54 46 2d 38 20 65 6e 63  ays in UTF-8 enc
22db0 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68  oding even if th
22dc0 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65  e named paramete
22dd0 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61  r was.** origina
22de0 6c 6c 79 20 73 70 65 63 69 66 69 65 64 20 61 73  lly specified as
22df0 20 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69   UTF-16 in [sqli
22e00 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
22e10 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
22e20 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e  prepare16_v2()].
22e30 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
22e40 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
22e50 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64  lob|sqlite3_bind
22e60 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ()],.** [sqlite3
22e70 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
22e80 63 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a  count()], and.**
22e90 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
22ea0 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29  arameter_index()
22eb0 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
22ec0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 32  ments:.** [H1362
22ed0 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  1].*/.SQLITE_API
22ee0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
22ef0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
22f00 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  ter_name(sqlite3
22f10 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f  _stmt*, int);../
22f20 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49  *.** CAPI3REF: I
22f30 6e 64 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65  ndex Of A Parame
22f40 74 65 72 20 57 69 74 68 20 41 20 47 69 76 65 6e  ter With A Given
22f50 20 4e 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 3c   Name {H13640} <
22f60 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65  S70300>.**.** Re
22f70 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f  turn the index o
22f80 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74  f an SQL paramet
22f90 65 72 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d  er given its nam
22fa0 65 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 78  e.  The.** index
22fb0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
22fc0 69 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20  is suitable for 
22fd0 75 73 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e  use as the secon
22fe0 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74  d.** parameter t
22ff0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  o [sqlite3_bind_
23000 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e  blob|sqlite3_bin
23010 64 28 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a 2a  d()].  A zero.**
23020 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
23030 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70 61 72 61  no matching para
23040 6d 65 74 65 72 20 69 73 20 66 6f 75 6e 64 2e 20  meter is found. 
23050 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a   The parameter.*
23060 2a 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67  * name must be g
23070 69 76 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76  iven in UTF-8 ev
23080 65 6e 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e  en if the origin
23090 61 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  al statement.** 
230a0 77 61 73 20 70 72 65 70 61 72 65 64 20 66 72 6f  was prepared fro
230b0 6d 20 55 54 46 2d 31 36 20 74 65 78 74 20 75 73  m UTF-16 text us
230c0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  ing [sqlite3_pre
230d0 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a  pare16_v2()]..**
230e0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
230f0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
23100 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d  |sqlite3_bind()]
23110 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  ,.** [sqlite3_bi
23120 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75  nd_parameter_cou
23130 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73  nt()], and.** [s
23140 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
23150 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a  meter_index()]..
23160 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
23170 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d 0a  ts:.** [H13641].
23180 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
23190 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  t sqlite3_bind_p
231a0 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73  arameter_index(s
231b0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f  qlite3_stmt*, co
231c0 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29  nst char *zName)
231d0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
231e0 46 3a 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e  F: Reset All Bin
231f0 64 69 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61  dings On A Prepa
23200 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48  red Statement {H
23210 31 33 36 36 30 7d 20 3c 53 37 30 33 30 30 3e 0a  13660} <S70300>.
23220 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 74  **.** Contrary t
23230 6f 20 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20  o the intuition 
23240 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65  of many, [sqlite
23250 33 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20  3_reset()] does 
23260 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 65  not reset.** the
23270 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
23280 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20  lob | bindings] 
23290 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  on a [prepared s
232a0 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 73  tatement]..** Us
232b0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  e this routine t
232c0 6f 20 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74  o reset all host
232d0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e   parameters to N
232e0 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ULL..**.** Requi
232f0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
23300 36 36 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  661].*/.SQLITE_A
23310 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
23320 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71  lear_bindings(sq
23330 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f  lite3_stmt*);../
23340 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e  *.** CAPI3REF: N
23350 75 6d 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73  umber Of Columns
23360 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74   In A Result Set
23370 20 7b 48 31 33 37 31 30 7d 20 3c 53 31 30 37 30   {H13710} <S1070
23380 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  0>.**.** Return 
23390 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
233a0 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73  lumns in the res
233b0 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 64  ult set returned
233c0 20 62 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70   by the.** [prep
233d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
233e0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
233f0 74 75 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74  turns 0 if pStmt
23400 20 69 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74   is an SQL.** st
23410 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65  atement that doe
23420 73 20 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74  s not return dat
23430 61 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61  a (for example a
23440 6e 20 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a  n [UPDATE])..**.
23450 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
23460 0a 2a 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f 0a  .** [H13711].*/.
23470 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
23480 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
23490 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  unt(sqlite3_stmt
234a0 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a   *pStmt);../*.**
234b0 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d   CAPI3REF: Colum
234c0 6e 20 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 73  n Names In A Res
234d0 75 6c 74 20 53 65 74 20 7b 48 31 33 37 32 30 7d  ult Set {H13720}
234e0 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
234f0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72  These routines r
23500 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61  eturn the name a
23510 73 73 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72  ssigned to a par
23520 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a  ticular column.*
23530 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  * in the result 
23540 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54  set of a [SELECT
23550 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  ] statement.  Th
23560 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
23570 5f 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72  _name().** inter
23580 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70  face returns a p
23590 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f  ointer to a zero
235a0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
235b0 38 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20  8 string.** and 
235c0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
235d0 61 6d 65 31 36 28 29 20 72 65 74 75 72 6e 73 20  ame16() returns 
235e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a  a pointer to a z
235f0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a  ero-terminated.*
23600 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e  * UTF-16 string.
23610 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61    The first para
23620 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 70 72  meter is the [pr
23630 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
23640 5d 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d  ].** that implem
23650 65 6e 74 73 20 74 68 65 20 5b 53 45 4c 45 43 54  ents the [SELECT
23660 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65  ] statement. The
23670 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
23680 72 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75  r is the.** colu
23690 6d 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20  mn number.  The 
236a0 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20  leftmost column 
236b0 69 73 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a  is number 0..**.
236c0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
236d0 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 69  string pointer i
236e0 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 69  s valid until ei
236f0 74 68 65 72 20 74 68 65 20 5b 70 72 65 70 61 72  ther the [prepar
23700 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a  ed statement].**
23710 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79   is destroyed by
23720 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
23730 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74  ze()] or until t
23740 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a  he next call to.
23750 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
23760 6e 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69  n_name() or sqli
23770 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
23780 36 28 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  6() on the same 
23790 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  column..**.** If
237a0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
237b0 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74  ) fails during t
237c0 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66  he processing of
237d0 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a   either routine.
237e0 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20  ** (for example 
237f0 64 75 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73  during a convers
23800 69 6f 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74  ion from UTF-8 t
23810 6f 20 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61  o UTF-16) then a
23820 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  .** NULL pointer
23830 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
23840 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
23850 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20  a result column 
23860 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  is the value of 
23870 74 68 65 20 22 41 53 22 20 63 6c 61 75 73 65 20  the "AS" clause 
23880 66 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75  for.** that colu
23890 6d 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 20  mn, if there is 
238a0 61 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49  an AS clause.  I
238b0 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53  f there is no AS
238c0 20 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20   clause.** then 
238d0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
238e0 63 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63  column is unspec
238f0 69 66 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68  ified and may ch
23900 61 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65  ange from.** one
23910 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69   release of SQLi
23920 74 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a  te to the next..
23930 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
23940 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d 20  ts:.** [H13721] 
23950 5b 48 31 33 37 32 33 5d 20 5b 48 31 33 37 32 34  [H13723] [H13724
23960 5d 20 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 37  ] [H13725] [H137
23970 32 36 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 0a  26] [H13727].*/.
23980 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
23990 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
239a0 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  olumn_name(sqlit
239b0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29  e3_stmt*, int N)
239c0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
239d0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
239e0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73  _column_name16(s
239f0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
23a00 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  t N);../*.** CAP
23a10 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66  I3REF: Source Of
23a20 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79   Data In A Query
23a30 20 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d   Result {H13740}
23a40 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
23a50 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70  These routines p
23a60 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74  rovide a means t
23a70 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74  o determine what
23a80 20 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a   column of what.
23a90 2a 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63  ** table in whic
23aa0 68 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73  h database a res
23ab0 75 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54  ult of a [SELECT
23ac0 5d 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65  ] statement come
23ad0 73 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e  s from..** The n
23ae0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
23af0 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20  ase or table or 
23b00 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65  column can be re
23b10 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74  turned as.** eit
23b20 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55  her a UTF-8 or U
23b30 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54  TF-16 string.  T
23b40 68 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f  he _database_ ro
23b50 75 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a  utines return.**
23b60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
23b70 6d 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20  me, the _table_ 
23b80 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
23b90 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20  the table name, 
23ba0 61 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69  and.** the origi
23bb0 6e 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  n_ routines retu
23bc0 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  rn the column na
23bd0 6d 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  me..** The retur
23be0 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61  ned string is va
23bf0 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70  lid until the [p
23c00 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
23c10 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a  t] is destroyed.
23c20 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ** using [sqlite
23c30 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72  3_finalize()] or
23c40 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20   until the same 
23c50 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72  information is r
23c60 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69  equested.** agai
23c70 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  n in a different
23c80 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a   encoding..**.**
23c90 20 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72   The names retur
23ca0 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67  ned are the orig
23cb0 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20  inal un-aliased 
23cc0 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20  names of the.** 
23cd0 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c  database, table,
23ce0 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a   and column..**.
23cf0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
23d00 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c  ument to the fol
23d10 6c 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20  lowing calls is 
23d20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
23d30 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65  ement]..** These
23d40 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
23d50 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  n information ab
23d60 6f 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75  out the Nth colu
23d70 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  mn returned by.*
23d80 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c  * the statement,
23d90 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
23da0 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20  second function 
23db0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
23dc0 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d  If the Nth colum
23dd0 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  n returned by th
23de0 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  e statement is a
23df0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a  n expression or.
23e00 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  ** subquery and 
23e10 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20  is not a column 
23e20 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20  value, then all 
23e30 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  of these functio
23e40 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c  ns return.** NUL
23e50 4c 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e  L.  These routin
23e60 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74  e might also ret
23e70 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65  urn NULL if a me
23e80 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
23e90 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e  error.** occurs.
23ea0 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65    Otherwise, the
23eb0 79 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  y return the nam
23ec0 65 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65  e of the attache
23ed0 64 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c  d database, tabl
23ee0 65 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20  e.** and column 
23ef0 74 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c  that query resul
23f00 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74  t column was ext
23f10 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a  racted from..**.
23f20 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f  ** As with all o
23f30 74 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73  ther SQLite APIs
23f40 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65  , those postfixe
23f50 64 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75  d with "16" retu
23f60 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63  rn.** UTF-16 enc
23f70 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68  oded strings, th
23f80 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e  e other function
23f90 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20  s return UTF-8. 
23fa0 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73  {END}.**.** Thes
23fb0 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20  e APIs are only 
23fc0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65  available if the
23fd0 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d   library was com
23fe0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a  piled with the.*
23ff0 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  * [SQLITE_ENABLE
24000 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
24010 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72  ] C-preprocessor
24020 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e   symbol defined.
24030 0a 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a  .**.** {A13751}.
24040 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72  ** If two or mor
24050 65 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f  e threads call o
24060 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
24070 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61  ese routines aga
24080 69 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a  inst the same.**
24090 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
240a0 65 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61  ent and column a
240b0 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20  t the same time 
240c0 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  then the results
240d0 20 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65   are.** undefine
240e0 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  d..**.** Require
240f0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 34  ments:.** [H1374
24100 31 5d 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 33  1] [H13742] [H13
24110 37 34 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 48  743] [H13744] [H
24120 31 33 37 34 35 5d 20 5b 48 31 33 37 34 36 5d 20  13745] [H13746] 
24130 5b 48 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 49  [H13748].**.** I
24140 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68  f two or more th
24150 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f  reads call one o
24160 72 20 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74  r more.** [sqlit
24170 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
24180 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e  se_name | column
24190 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66   metadata interf
241a0 61 63 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65  aces].** for the
241b0 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20   same [prepared 
241c0 73 74 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72  statement] and r
241d0 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  esult column.** 
241e0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
241f0 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
24200 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e  s are undefined.
24210 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
24220 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
24230 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
24240 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  se_name(sqlite3_
24250 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  stmt*,int);.SQLI
24260 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
24270 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
24280 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
24290 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
242a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
242b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
242c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
242d0 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  e_name(sqlite3_s
242e0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  tmt*,int);.SQLIT
242f0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
24300 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
24310 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71  _table_name16(sq
24320 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
24330 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
24340 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
24350 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
24360 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
24370 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  *,int);.SQLITE_A
24380 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
24390 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
243a0 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69  igin_name16(sqli
243b0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
243c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
243d0 20 44 65 63 6c 61 72 65 64 20 44 61 74 61 74 79   Declared Dataty
243e0 70 65 20 4f 66 20 41 20 51 75 65 72 79 20 52 65  pe Of A Query Re
243f0 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c 53  sult {H13760} <S
24400 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  10700>.**.** The
24410 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
24420 20 69 73 20 61 20 5b 70 72 65 70 61 72 65 64 20   is a [prepared 
24430 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49  statement]..** I
24440 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  f this statement
24450 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73   is a [SELECT] s
24460 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65  tatement and the
24470 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   Nth column of t
24480 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72  he.** returned r
24490 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61  esult set of tha
244a0 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20  t [SELECT] is a 
244b0 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f  table column (no
244c0 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  t an.** expressi
244d0 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 29 20  on or subquery) 
244e0 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65  then the declare
244f0 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 74 61  d type of the ta
24500 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73  ble.** column is
24510 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74   returned.  If t
24520 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
24530 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
24540 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73  is an.** express
24550 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c  ion or subquery,
24560 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69   then a NULL poi
24570 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
24580 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  ..** The returne
24590 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61  d string is alwa
245a0 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  ys UTF-8 encoded
245b0 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f  . {END}.**.** Fo
245c0 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e  r example, given
245d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
245e0 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41  hema:.**.** CREA
245f0 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 20 56  TE TABLE t1(c1 V
24600 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61  ARIANT);.**.** a
24610 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  nd the following
24620 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65   statement to be
24630 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a   compiled:.**.**
24640 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20   SELECT c1 + 1, 
24650 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a  c1 FROM t1;.**.*
24660 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * this routine w
24670 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20  ould return the 
24680 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22  string "VARIANT"
24690 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20   for the second 
246a0 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e  result.** column
246b0 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e   (i==1), and a N
246c0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20  ULL pointer for 
246d0 74 68 65 20 66 69 72 73 74 20 72 65 73 75 6c 74  the first result
246e0 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 0a   column (i==0)..
246f0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65  **.** SQLite use
24700 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69  s dynamic run-ti
24710 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a  me typing.  So j
24720 75 73 74 20 62 65 63 61 75 73 65 20 61 20 63 6f  ust because a co
24730 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61  lumn.** is decla
24740 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61  red to contain a
24750 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 70 65   particular type
24760 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74   does not mean t
24770 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20  hat the.** data 
24780 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63  stored in that c
24790 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20  olumn is of the 
247a0 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20  declared type.  
247b0 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72  SQLite is.** str
247c0 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74  ongly typed, but
247d0 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 20 64   the typing is d
247e0 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69  ynamic not stati
247f0 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61  c.  Type.** is a
24800 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 69  ssociated with i
24810 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73  ndividual values
24820 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63  , not with the c
24830 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65  ontainers.** use
24840 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20  d to hold those 
24850 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65  values..**.** Re
24860 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
24870 48 31 33 37 36 31 5d 20 5b 48 31 33 37 36 32 5d  H13761] [H13762]
24880 20 5b 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 4c   [H13763].*/.SQL
24890 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
248a0 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
248b0 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69  mn_decltype(sqli
248c0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
248d0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
248e0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
248f0 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36  olumn_decltype16
24900 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
24910 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  nt);../*.** CAPI
24920 33 52 45 46 3a 20 45 76 61 6c 75 61 74 65 20 41  3REF: Evaluate A
24930 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20  n SQL Statement 
24940 7b 48 31 33 32 30 30 7d 20 3c 53 31 30 30 30 30  {H13200} <S10000
24950 3e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20  >.**.** After a 
24960 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
24970 65 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72  ent] has been pr
24980 65 70 61 72 65 64 20 75 73 69 6e 67 20 65 69 74  epared using eit
24990 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  her.** [sqlite3_
249a0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72  prepare_v2()] or
249b0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
249c0 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65  e16_v2()] or one
249d0 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a   of the legacy.*
249e0 2a 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71  * interfaces [sq
249f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d  lite3_prepare()]
24a00 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
24a10 70 61 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20  pare16()], this 
24a20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74  function.** must
24a30 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f   be called one o
24a40 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20  r more times to 
24a50 65 76 61 6c 75 61 74 65 20 74 68 65 20 73 74 61  evaluate the sta
24a60 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  tement..**.** Th
24a70 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65  e details of the
24a80 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65   behavior of the
24a90 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
24aa0 69 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e 64  interface depend
24ab0 0a 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74  .** on whether t
24ac0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  he statement was
24ad0 20 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20   prepared using 
24ae0 74 68 65 20 6e 65 77 65 72 20 22 76 32 22 20 69  the newer "v2" i
24af0 6e 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c  nterface.** [sql
24b00 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
24b10 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
24b20 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20  prepare16_v2()] 
24b30 6f 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67  or the older leg
24b40 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  acy.** interface
24b50 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
24b60 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
24b70 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20  3_prepare16()]. 
24b80 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a   The use of the.
24b90 2a 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65  ** new "v2" inte
24ba0 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65  rface is recomme
24bb0 6e 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70  nded for new app
24bc0 6c 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68  lications but th
24bd0 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65  e legacy.** inte
24be0 72 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69  rface will conti
24bf0 6e 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72  nue to be suppor
24c00 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ted..**.** In th
24c10 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61  e legacy interfa
24c20 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76  ce, the return v
24c30 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74  alue will be eit
24c40 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  her [SQLITE_BUSY
24c50 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f  ],.** [SQLITE_DO
24c60 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57  NE], [SQLITE_ROW
24c70 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ], [SQLITE_ERROR
24c80 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49  ], or [SQLITE_MI
24c90 53 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74  SUSE]..** With t
24ca0 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63  he "v2" interfac
24cb0 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74  e, any of the ot
24cc0 68 65 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 65  her [result code
24cd0 73 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64  s] or.** [extend
24ce0 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d  ed result codes]
24cf0 20 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 6e   might be return
24d00 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a  ed as well..**.*
24d10 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  * [SQLITE_BUSY] 
24d20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64  means that the d
24d30 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77  atabase engine w
24d40 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71  as unable to acq
24d50 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61  uire the.** data
24d60 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65  base locks it ne
24d70 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f  eds to do its jo
24d80 62 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65  b.  If the state
24d90 6d 65 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49  ment is a [COMMI
24da0 54 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20  T].** or occurs 
24db0 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78  outside of an ex
24dc0 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69  plicit transacti
24dd0 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e  on, then you can
24de0 20 72 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74   retry the.** st
24df0 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65  atement.  If the
24e00 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f   statement is no
24e10 74 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64  t a [COMMIT] and
24e20 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61   occurs within a
24e30 0a 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61  .** explicit tra
24e40 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f  nsaction then yo
24e50 75 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63  u should rollbac
24e60 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
24e70 6e 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74  n before.** cont
24e80 69 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53  inuing..**.** [S
24e90 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e  QLITE_DONE] mean
24ea0 73 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65  s that the state
24eb0 6d 65 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65  ment has finishe
24ec0 64 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73  d executing.** s
24ed0 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71  uccessfully.  sq
24ee0 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f  lite3_step() sho
24ef0 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65  uld not be calle
24f00 64 20 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20  d again on this 
24f10 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69  virtual.** machi
24f20 6e 65 20 77 69 74 68 6f 75 74 20 66 69 72 73 74  ne without first
24f30 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65   calling [sqlite
24f40 33 5f 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65  3_reset()] to re
24f50 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a  set the virtual.
24f60 2a 2a 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20  ** machine back 
24f70 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73  to its initial s
24f80 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  tate..**.** If t
24f90 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
24fa0 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 20   being executed 
24fb0 72 65 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61  returns any data
24fc0 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 52  , then [SQLITE_R
24fd0 4f 57 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  OW].** is return
24fe0 65 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e  ed each time a n
24ff0 65 77 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69  ew row of data i
25000 73 20 72 65 61 64 79 20 66 6f 72 20 70 72 6f 63  s ready for proc
25010 65 73 73 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a  essing by the.**
25020 20 63 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c   caller. The val
25030 75 65 73 20 6d 61 79 20 62 65 20 61 63 63 65 73  ues may be acces
25040 73 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b 63  sed using the [c
25050 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e  olumn access fun
25060 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69  ctions]..** sqli
25070 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61  te3_step() is ca
25080 6c 6c 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65  lled again to re
25090 74 72 69 65 76 65 20 74 68 65 20 6e 65 78 74 20  trieve the next 
250a0 72 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a  row of data..**.
250b0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ** [SQLITE_ERROR
250c0 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72  ] means that a r
250d0 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73  un-time error (s
250e0 75 63 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61  uch as a constra
250f0 69 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e  int.** violation
25100 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20  ) has occurred. 
25110 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
25120 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61  should not be ca
25130 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a  lled again on.**
25140 20 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e   the VM. More in
25150 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65  formation may be
25160 20 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e   found by callin
25170 67 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  g [sqlite3_errms
25180 67 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68  g()]..** With th
25190 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61  e legacy interfa
251a0 63 65 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69  ce, a more speci
251b0 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28  fic error code (
251c0 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20  for example,.** 
251d0 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50  [SQLITE_INTERRUP
251e0 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45  T], [SQLITE_SCHE
251f0 4d 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 52  MA], [SQLITE_COR
25200 52 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 6f  RUPT], and so fo
25210 72 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 6f  rth).** can be o
25220 62 74 61 69 6e 65 64 20 62 79 20 63 61 6c 6c 69  btained by calli
25230 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  ng [sqlite3_rese
25240 74 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b  t()] on the.** [
25250 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
25260 6e 74 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 32  nt].  In the "v2
25270 22 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20  " interface,.** 
25280 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69  the more specifi
25290 63 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  c error code is 
252a0 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c  returned directl
252b0 79 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65  y by sqlite3_ste
252c0 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49  p()..**.** [SQLI
252d0 54 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e 73  TE_MISUSE] means
252e0 20 74 68 61 74 20 74 68 65 20 74 68 69 73 20 72   that the this r
252f0 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65  outine was calle
25300 64 20 69 6e 61 70 70 72 6f 70 72 69 61 74 65 6c  d inappropriatel
25310 79 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 74  y..** Perhaps it
25320 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61   was called on a
25330 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
25340 6d 65 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a  ment] that has.*
25350 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b  * already been [
25360 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
25370 20 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72   | finalized] or
25380 20 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64   on one that had
25390 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72  .** previously r
253a0 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f  eturned [SQLITE_
253b0 45 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54  ERROR] or [SQLIT
253c0 45 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20  E_DONE].  Or it 
253d0 63 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 20  could.** be the 
253e0 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 61  case that the sa
253f0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
25400 65 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20  ection is being 
25410 75 73 65 64 20 62 79 20 74 77 6f 20 6f 72 0a 2a  used by two or.*
25420 2a 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61  * more threads a
25430 74 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 6e  t the same momen
25440 74 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  t in time..**.**
25450 20 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66   <b>Goofy Interf
25460 61 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49  ace Alert:</b> I
25470 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74  n the legacy int
25480 65 72 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69  erface, the sqli
25490 74 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50  te3_step().** AP
254a0 49 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  I always returns
254b0 20 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72   a generic error
254c0 20 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45   code, [SQLITE_E
254d0 52 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67  RROR], following
254e0 20 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74   any.** error ot
254f0 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45  her than [SQLITE
25500 5f 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49  _BUSY] and [SQLI
25510 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75  TE_MISUSE].  You
25520 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73   must call.** [s
25530 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
25540 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  or [sqlite3_fina
25550 6c 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72  lize()] in order
25560 20 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20   to find one of 
25570 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20  the.** specific 
25580 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68  [error codes] th
25590 61 74 20 62 65 74 74 65 72 20 64 65 73 63 72 69  at better descri
255a0 62 65 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a  bes the error..*
255b0 2a 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 20  * We admit that 
255c0 74 68 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20  this is a goofy 
255d0 64 65 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f  design.  The pro
255e0 62 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69  blem has been fi
255f0 78 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  xed.** with the 
25600 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20  "v2" interface. 
25610 20 49 66 20 79 6f 75 20 70 72 65 70 61 72 65 20   If you prepare 
25620 61 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20  all of your SQL 
25630 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73  statements.** us
25640 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69  ing either [sqli
25650 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
25660 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72  ] or [sqlite3_pr
25670 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e  epare16_v2()] in
25680 73 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20  stead.** of the 
25690 6c 65 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f  legacy [sqlite3_
256a0 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b  prepare()] and [
256b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
256c0 36 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c  6()] interfaces,
256d0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72  .** then the mor
256e0 65 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f  e specific [erro
256f0 72 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74  r codes] are ret
25700 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a  urned directly.*
25710 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65  * by sqlite3_ste
25720 70 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66  p().  The use of
25730 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66   the "v2" interf
25740 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ace is recommend
25750 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ed..**.** Requir
25760 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 32  ements:.** [H132
25770 30 32 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 31  02] [H15304] [H1
25780 35 33 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 5b  5306] [H15308] [
25790 48 31 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 54  H15310].*/.SQLIT
257a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
257b0 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73  3_step(sqlite3_s
257c0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tmt*);../*.** CA
257d0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f  PI3REF: Number o
257e0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72  f columns in a r
257f0 65 73 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37  esult set {H1377
25800 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a  0} <S10700>.**.*
25810 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75  * Returns the nu
25820 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69  mber of values i
25830 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  n the current ro
25840 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  w of the result 
25850 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  set..**.** Requi
25860 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
25870 37 37 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 2f  771] [H13772].*/
25880 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
25890 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75  sqlite3_data_cou
258a0 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  nt(sqlite3_stmt 
258b0 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
258c0 43 41 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d  CAPI3REF: Fundam
258d0 65 6e 74 61 6c 20 44 61 74 61 74 79 70 65 73 20  ental Datatypes 
258e0 7b 48 31 30 32 36 35 7d 20 3c 53 31 30 31 31 30  {H10265} <S10110
258f0 3e 3c 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 59  ><S10120>.** KEY
25900 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45  WORDS: SQLITE_TE
25910 58 54 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 36  XT.**.** {H10266
25920 7d 20 45 76 65 72 79 20 76 61 6c 75 65 20 69 6e  } Every value in
25930 20 53 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 20   SQLite has one 
25940 6f 66 20 66 69 76 65 20 66 75 6e 64 61 6d 65 6e  of five fundamen
25950 74 61 6c 20 64 61 74 61 74 79 70 65 73 3a 0a 2a  tal datatypes:.*
25960 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
25970 3e 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  > 64-bit signed 
25980 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e 20  integer.** <li> 
25990 36 34 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 61  64-bit IEEE floa
259a0 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65  ting point numbe
259b0 72 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e 67  r.** <li> string
259c0 0a 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a 2a  .** <li> BLOB.**
259d0 20 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f   <li> NULL.** </
259e0 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20  ul> {END}.**.** 
259f0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
25a00 61 72 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61  are codes for ea
25a10 63 68 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65  ch of those type
25a20 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  s..**.** Note th
25a30 61 74 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45  at the SQLITE_TE
25a40 58 54 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20  XT constant was 
25a50 61 6c 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c  also used in SQL
25a60 69 74 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a  ite version 2.**
25a70 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c   for a completel
25a80 79 20 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e  y different mean
25a90 69 6e 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74  ing.  Software t
25aa0 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73  hat links agains
25ab0 74 20 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65  t both.** SQLite
25ac0 20 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53   version 2 and S
25ad0 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20  QLite version 3 
25ae0 73 68 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54  should use SQLIT
25af0 45 33 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a 20  E3_TEXT, not.** 
25b00 53 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a  SQLITE_TEXT..*/.
25b10 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
25b20 4e 54 45 47 45 52 20 20 31 0a 23 64 65 66 69 6e  NTEGER  1.#defin
25b30 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20  e SQLITE_FLOAT  
25b40 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
25b50 54 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 64  TE_BLOB     4.#d
25b60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c  efine SQLITE_NUL
25b70 4c 20 20 20 20 20 35 0a 23 69 66 64 65 66 20 53  L     5.#ifdef S
25b80 51 4c 49 54 45 5f 54 45 58 54 0a 23 20 75 6e 64  QLITE_TEXT.# und
25b90 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23  ef SQLITE_TEXT.#
25ba0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
25bb0 4c 49 54 45 5f 54 45 58 54 20 20 20 20 20 33 0a  LITE_TEXT     3.
25bc0 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53  #endif.#define S
25bd0 51 4c 49 54 45 33 5f 54 45 58 54 20 20 20 20 20  QLITE3_TEXT     
25be0 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  3../*.** CAPI3RE
25bf0 46 3a 20 52 65 73 75 6c 74 20 56 61 6c 75 65 73  F: Result Values
25c00 20 46 72 6f 6d 20 41 20 51 75 65 72 79 20 7b 48   From A Query {H
25c10 31 33 38 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a  13800} <S10700>.
25c20 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f  ** KEYWORDS: {co
25c30 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63  lumn access func
25c40 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  tions}.**.** The
25c50 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d  se routines form
25c60 20 74 68 65 20 22 72 65 73 75 6c 74 20 73 65 74   the "result set
25c70 20 71 75 65 72 79 22 20 69 6e 74 65 72 66 61 63   query" interfac
25c80 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  e..**.** These r
25c90 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69  outines return i
25ca0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
25cb0 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   a single column
25cc0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a   of the current.
25cd0 2a 2a 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66  ** result row of
25ce0 20 61 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76   a query.  In ev
25cf0 65 72 79 20 63 61 73 65 20 74 68 65 20 66 69 72  ery case the fir
25d00 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
25d10 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74   pointer.** to t
25d20 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
25d30 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20  tement] that is 
25d40 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20  being evaluated 
25d50 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74  (the [sqlite3_st
25d60 6d 74 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73  mt*].** that was
25d70 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b   returned from [
25d80 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
25d90 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20  v2()] or one of 
25da0 69 74 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a  its variants).**
25db0 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
25dc0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
25dd0 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c  index of the col
25de0 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e  umn for which in
25df0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f  formation.** sho
25e00 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e  uld be returned.
25e10 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63    The leftmost c
25e20 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73  olumn of the res
25e30 75 6c 74 20 73 65 74 20 68 61 73 20 74 68 65 20  ult set has the 
25e40 69 6e 64 65 78 20 30 2e 0a 2a 2a 20 54 68 65 20  index 0..** The 
25e50 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
25e60 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
25e70 63 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65  can be determine
25e80 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69  d using.** [sqli
25e90 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
25ea0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ()]..**.** If th
25eb0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
25ec0 64 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  does not current
25ed0 6c 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61  ly point to a va
25ee0 6c 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74  lid row, or if t
25ef0 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64  he.** column ind
25f00 65 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e  ex is out of ran
25f10 67 65 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69  ge, the result i
25f20 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20  s undefined..** 
25f30 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d  These routines m
25f40 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ay only be calle
25f50 64 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20  d when the most 
25f60 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a  recent call to.*
25f70 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  * [sqlite3_step(
25f80 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20  )] has returned 
25f90 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64  [SQLITE_ROW] and
25fa0 20 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c   neither.** [sql
25fb0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f  ite3_reset()] no
25fc0 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  r [sqlite3_final
25fd0 69 7a 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e  ize()] have been
25fe0 20 63 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65   called subseque
25ff0 6e 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20  ntly..** If any 
26000 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  of these routine
26010 73 20 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74  s are called aft
26020 65 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  er [sqlite3_rese
26030 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69  t()] or.** [sqli
26040 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20  te3_finalize()] 
26050 6f 72 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65  or after [sqlite
26060 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65  3_step()] has re
26070 74 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68  turned.** someth
26080 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b  ing other than [
26090 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65  SQLITE_ROW], the
260a0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64   results are und
260b0 65 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73  efined..** If [s
260c0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f  qlite3_step()] o
260d0 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  r [sqlite3_reset
260e0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
260f0 66 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61  finalize()].** a
26100 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61  re called from a
26110 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61   different threa
26120 64 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74  d while any of t
26130 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  hese routines.**
26140 20 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68   are pending, th
26150 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  en the results a
26160 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  re undefined..**
26170 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
26180 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f  column_type() ro
26190 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
261a0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54  e.** [SQLITE_INT
261b0 45 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20  EGER | datatype 
261c0 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e  code] for the in
261d0 69 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a  itial data type.
261e0 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ** of the result
261f0 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65   column.  The re
26200 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20  turned value is 
26210 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49  one of [SQLITE_I
26220 4e 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c  NTEGER],.** [SQL
26230 49 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c  ITE_FLOAT], [SQL
26240 49 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49  ITE_TEXT], [SQLI
26250 54 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51  TE_BLOB], or [SQ
26260 4c 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65  LITE_NULL].  The
26270 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e   value.** return
26280 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f  ed by sqlite3_co
26290 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f  lumn_type() is o
262a0 6e 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69  nly meaningful i
262b0 66 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e  f no type.** con
262c0 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63  versions have oc
262d0 63 75 72 72 65 64 20 61 73 20 64 65 73 63 72 69  curred as descri
262e0 62 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65  bed below.  Afte
262f0 72 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73  r a type convers
26300 69 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ion,.** the valu
26310 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
26320 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
26330 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64  e() is undefined
26340 2e 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72  .  Future.** ver
26350 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
26360 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 62  may change the b
26370 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74  ehavior of sqlit
26380 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29  e3_column_type()
26390 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  .** following a 
263a0 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e  type conversion.
263b0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
263c0 73 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f  sult is a BLOB o
263d0 72 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74  r UTF-8 string t
263e0 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f  hen the sqlite3_
263f0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a  column_bytes().*
26400 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  * routine return
26410 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
26420 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c  bytes in that BL
26430 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a  OB or string..**
26440 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   If the result i
26450 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e  s a UTF-16 strin
26460 67 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  g, then sqlite3_
26470 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 63  column_bytes() c
26480 6f 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 73  onverts.** the s
26490 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 20 61  tring to UTF-8 a
264a0 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20  nd then returns 
264b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
264c0 74 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72  tes..** If the r
264d0 65 73 75 6c 74 20 69 73 20 61 20 6e 75 6d 65 72  esult is a numer
264e0 69 63 20 76 61 6c 75 65 20 74 68 65 6e 20 73 71  ic value then sq
264f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
26500 65 73 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71  es() uses.** [sq
26510 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
26520 5d 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61  ] to convert tha
26530 74 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46  t value to a UTF
26540 2d 38 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65  -8 string and re
26550 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d  turns.** the num
26560 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
26570 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20  that string..** 
26580 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
26590 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c  ed does not incl
265a0 75 64 65 20 74 68 65 20 7a 65 72 6f 20 74 65 72  ude the zero ter
265b0 6d 69 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65  minator at the e
265c0 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72  nd.** of the str
265d0 69 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72 69 74  ing.  For clarit
265e0 79 3a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  y: the value ret
265f0 75 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d  urned is the num
26600 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20  ber of.** bytes 
26610 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e  in the string, n
26620 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ot the number of
26630 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
26640 2a 2a 20 53 74 72 69 6e 67 73 20 72 65 74 75 72  ** Strings retur
26650 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  ned by sqlite3_c
26660 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64  olumn_text() and
26670 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
26680 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65  text16(),.** eve
26690 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 73 2c  n empty strings,
266a0 20 61 72 65 20 61 6c 77 61 79 73 20 7a 65 72 6f   are always zero
266b0 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68   terminated.  Th
266c0 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75  e return.** valu
266d0 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63  e from sqlite3_c
266e0 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72  olumn_blob() for
266f0 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42   a zero-length B
26700 4c 4f 42 20 69 73 20 61 6e 20 61 72 62 69 74 72  LOB is an arbitr
26710 61 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20  ary.** pointer, 
26720 70 6f 73 73 69 62 6c 79 20 65 76 65 6e 20 61 20  possibly even a 
26730 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
26740 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
26750 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
26760 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69   routine is simi
26770 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  lar to sqlite3_c
26780 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a  olumn_bytes().**
26790 20 62 75 74 20 6c 65 61 76 65 73 20 74 68 65 20   but leaves the 
267a0 72 65 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36  result in UTF-16
267b0 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20   in native byte 
267c0 6f 72 64 65 72 20 69 6e 73 74 65 61 64 20 6f 66  order instead of
267d0 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a   UTF-8..** The z
267e0 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69  ero terminator i
267f0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69  s not included i
26800 6e 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a  n this count..**
26810 0a 2a 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 72  .** The object r
26820 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69  eturned by [sqli
26830 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65  te3_column_value
26840 28 29 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e  ()] is an.** [un
26850 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
26860 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e  3_value] object.
26870 20 20 41 6e 20 75 6e 70 72 6f 74 65 63 74 65 64    An unprotected
26880 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
26890 62 6a 65 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c  bject.** may onl
268a0 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20 5b  y be used with [
268b0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c  sqlite3_bind_val
268c0 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  ue()] and [sqlit
268d0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
268e0 29 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75  )]..** If the [u
268f0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
26900 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
26910 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20   returned by.** 
26920 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
26930 76 61 6c 75 65 28 29 5d 20 69 73 20 75 73 65 64  value()] is used
26940 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 77 61   in any other wa
26950 79 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c  y, including cal
26960 6c 73 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65  ls.** to routine
26970 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f  s like [sqlite3_
26980 76 61 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73  value_int()], [s
26990 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
269a0 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c  t()],.** or [sql
269b0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
269c0 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 62 65  ()], then the be
269d0 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
269e0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ned..**.** These
269f0 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70   routines attemp
26a00 74 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65  t to convert the
26a10 20 76 61 6c 75 65 20 77 68 65 72 65 20 61 70 70   value where app
26a20 72 6f 70 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a  ropriate.  For.*
26a30 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68  * example, if th
26a40 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65  e internal repre
26a50 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f  sentation is FLO
26a60 41 54 20 61 6e 64 20 61 20 74 65 78 74 20 72 65  AT and a text re
26a70 73 75 6c 74 0a 2a 2a 20 69 73 20 72 65 71 75 65  sult.** is reque
26a80 73 74 65 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73  sted, [sqlite3_s
26a90 6e 70 72 69 6e 74 66 28 29 5d 20 69 73 20 75 73  nprintf()] is us
26aa0 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f  ed internally to
26ab0 20 70 65 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20   perform the.** 
26ac0 63 6f 6e 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d  conversion autom
26ad0 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 66  atically.  The f
26ae0 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64  ollowing table d
26af0 65 74 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65  etails the conve
26b00 72 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61  rsions.** that a
26b10 72 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a  re applied:.**.*
26b20 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  * <blockquote>.*
26b30 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d  * <table border=
26b40 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e  "1">.** <tr><th>
26b50 20 49 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70   Internal<br>Typ
26b60 65 20 3c 74 68 3e 20 52 65 71 75 65 73 74 65 64  e <th> Requested
26b70 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 43  <br>Type <th>  C
26b80 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20  onversion.**.** 
26b90 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20  <tr><td>  NULL  
26ba0 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20    <td> INTEGER  
26bb0 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20   <td> Result is 
26bc0 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e  0.** <tr><td>  N
26bd0 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f  ULL    <td>  FLO
26be0 41 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c  AT    <td> Resul
26bf0 74 20 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e  t is 0.0.** <tr>
26c00 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74  <td>  NULL    <t
26c10 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64  d>   TEXT    <td
26c20 3e 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  > Result is NULL
26c30 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e   pointer.** <tr>
26c40 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74  <td>  NULL    <t
26c50 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64  d>   BLOB    <td
26c60 3e 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  > Result is NULL
26c70 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e   pointer.** <tr>
26c80 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74  <td> INTEGER  <t
26c90 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64  d>  FLOAT    <td
26ca0 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69  > Convert from i
26cb0 6e 74 65 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a  nteger to float.
26cc0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45  ** <tr><td> INTE
26cd0 47 45 52 20 20 3c 74 64 3e 20 20 20 54 45 58 54  GER  <td>   TEXT
26ce0 20 20 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72      <td> ASCII r
26cf0 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20  endering of the 
26d00 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c  integer.** <tr><
26d10 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64  td> INTEGER  <td
26d20 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e  >   BLOB    <td>
26d30 20 53 61 6d 65 20 61 73 20 49 4e 54 45 47 45 52   Same as INTEGER
26d40 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74  ->TEXT.** <tr><t
26d50 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e  d>  FLOAT   <td>
26d60 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20   INTEGER   <td> 
26d70 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f  Convert from flo
26d80 61 74 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a  at to integer.**
26d90 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54   <tr><td>  FLOAT
26da0 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20     <td>   TEXT  
26db0 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e    <td> ASCII ren
26dc0 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c  dering of the fl
26dd0 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  oat.** <tr><td> 
26de0 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20   FLOAT   <td>   
26df0 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d  BLOB    <td> Sam
26e00 65 20 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54  e as FLOAT->TEXT
26e10 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45  .** <tr><td>  TE
26e20 58 54 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47  XT    <td> INTEG
26e30 45 52 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74  ER   <td> Use at
26e40 6f 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  oi().** <tr><td>
26e50 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20    TEXT    <td>  
26e60 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73  FLOAT    <td> Us
26e70 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e  e atof().** <tr>
26e80 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74  <td>  TEXT    <t
26e90 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64  d>   BLOB    <td
26ea0 3e 20 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c  > No change.** <
26eb0 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20  tr><td>  BLOB   
26ec0 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20   <td> INTEGER   
26ed0 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20  <td> Convert to 
26ee0 54 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74  TEXT then use at
26ef0 6f 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  oi().** <tr><td>
26f00 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20    BLOB    <td>  
26f10 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f  FLOAT    <td> Co
26f20 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 68  nvert to TEXT th
26f30 65 6e 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a  en use atof().**
26f40 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20   <tr><td>  BLOB 
26f50 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20     <td>   TEXT  
26f60 20 20 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72    <td> Add a zer
26f70 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20  o terminator if 
26f80 6e 65 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c  needed.** </tabl
26f90 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f  e>.** </blockquo
26fa0 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61  te>.**.** The ta
26fb0 62 6c 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20  ble above makes 
26fc0 72 65 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61  reference to sta
26fd0 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 20  ndard C library 
26fe0 66 75 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29  functions atoi()
26ff0 0a 2a 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20  .** and atof(). 
27000 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74   SQLite does not
27010 20 72 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73   really use thes
27020 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74  e functions.  It
27030 20 68 61 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20   has its.** own 
27040 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 72  equivalent inter
27050 6e 61 6c 20 72 6f 75 74 69 6e 65 73 2e 20 20 54  nal routines.  T
27060 68 65 20 61 74 6f 69 28 29 20 61 6e 64 20 61 74  he atoi() and at
27070 6f 66 28 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a  of() names are.*
27080 2a 20 75 73 65 64 20 69 6e 20 74 68 65 20 74 61  * used in the ta
27090 62 6c 65 20 66 6f 72 20 62 72 65 76 69 74 79 20  ble for brevity 
270a0 61 6e 64 20 62 65 63 61 75 73 65 20 74 68 65 79  and because they
270b0 20 61 72 65 20 66 61 6d 69 6c 69 61 72 20 74 6f   are familiar to
270c0 20 6d 6f 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72   most.** C progr
270d0 61 6d 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  ammers..**.** No
270e0 74 65 20 74 68 61 74 20 77 68 65 6e 20 74 79 70  te that when typ
270f0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63  e conversions oc
27100 63 75 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65  cur, pointers re
27110 74 75 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0a  turned by prior.
27120 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ** calls to sqli
27130 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
27140 29 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ), sqlite3_colum
27150 6e 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72  n_text(), and/or
27160 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  .** sqlite3_colu
27170 6d 6e 5f 74 65 78 74 31 36 28 29 20 6d 61 79 20  mn_text16() may 
27180 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a  be invalidated..
27190 2a 2a 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69  ** Type conversi
271a0 6f 6e 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20  ons and pointer 
271b0 69 6e 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69  invalidations mi
271c0 67 68 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20  ght occur.** in 
271d0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61  the following ca
271e0 73 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  ses:.**.** <ul>.
271f0 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74  ** <li> The init
27200 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61  ial content is a
27210 20 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65   BLOB and sqlite
27220 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20  3_column_text() 
27230 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  or.**      sqlit
27240 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
27250 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41  () is called.  A
27260 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72   zero-terminator
27270 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e   might.**      n
27280 65 65 64 20 74 6f 20 62 65 20 61 64 64 65 64 20  eed to be added 
27290 74 6f 20 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f  to the string.</
272a0 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20  li>.** <li> The 
272b0 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20  initial content 
272c0 69 73 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e  is UTF-8 text an
272d0 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  d sqlite3_column
272e0 5f 62 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a  _bytes16() or.**
272f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
27300 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73  lumn_text16() is
27310 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f   called.  The co
27320 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f  ntent must be co
27330 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20  nverted.**      
27340 74 6f 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a  to UTF-16.</li>.
27350 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74  ** <li> The init
27360 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55  ial content is U
27370 54 46 2d 31 36 20 74 65 78 74 20 61 6e 64 20 73  TF-16 text and s
27380 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
27390 74 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20  tes() or.**     
273a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
273b0 74 65 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64  text() is called
273c0 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d  .  The content m
273d0 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64  ust be converted
273e0 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d  .**      to UTF-
273f0 38 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e  8.</li>.** </ul>
27400 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f  .**.** Conversio
27410 6e 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31  ns between UTF-1
27420 36 62 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65  6be and UTF-16le
27430 20 61 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65   are always done
27440 20 69 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f   in place and do
27450 0a 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61  .** not invalida
27460 74 65 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74  te a prior point
27470 65 72 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f  er, though of co
27480 75 72 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  urse the content
27490 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a   of the buffer.*
274a0 2a 20 74 68 61 74 20 74 68 65 20 70 72 69 6f 72  * that the prior
274b0 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20   pointer points 
274c0 74 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65  to will have bee
274d0 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68  n modified.  Oth
274e0 65 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63  er kinds.** of c
274f0 6f 6e 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f  onversion are do
27500 6e 65 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e  ne in place when
27510 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c   it is possible,
27520 20 62 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74   but sometimes t
27530 68 65 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70  hey.** are not p
27540 6f 73 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74  ossible and in t
27550 68 6f 73 65 20 63 61 73 65 73 20 70 72 69 6f 72  hose cases prior
27560 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e   pointers are in
27570 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  validated..**.**
27580 20 54 68 65 20 73 61 66 65 73 74 20 61 6e 64 20   The safest and 
27590 65 61 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d  easiest to remem
275a0 62 65 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f  ber policy is to
275b0 20 69 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f   invoke these ro
275c0 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65  utines.** in one
275d0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
275e0 67 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75  g ways:.**.** <u
275f0 6c 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74  l>.**  <li>sqlit
27600 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
27610 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c   followed by sql
27620 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
27630 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69  s()</li>.**  <li
27640 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  >sqlite3_column_
27650 62 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20  blob() followed 
27660 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  by sqlite3_colum
27670 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a  n_bytes()</li>.*
27680 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63  *  <li>sqlite3_c
27690 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66  olumn_text16() f
276a0 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74  ollowed by sqlit
276b0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
276c0 36 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c  6()</li>.** </ul
276d0 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72  >.**.** In other
276e0 20 77 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75   words, you shou
276f0 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f  ld call sqlite3_
27700 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a  column_text(),.*
27710 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
27720 5f 62 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69  _blob(), or sqli
27730 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
27740 36 28 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72  6() first to for
27750 63 65 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  ce the result.**
27760 20 69 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65   into the desire
27770 64 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69  d format, then i
27780 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f  nvoke sqlite3_co
27790 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a  lumn_bytes() or.
277a0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
277b0 6e 5f 62 79 74 65 73 31 36 28 29 20 74 6f 20 66  n_bytes16() to f
277c0 69 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ind the size of 
277d0 74 68 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20  the result.  Do 
277e0 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a  not mix calls.**
277f0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
27800 6d 6e 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c  mn_text() or sql
27810 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
27820 28 29 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f  () with calls to
27830 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  .** sqlite3_colu
27840 6d 6e 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e  mn_bytes16(), an
27850 64 20 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c  d do not mix cal
27860 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ls to sqlite3_co
27870 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a  lumn_text16().**
27880 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73   with calls to s
27890 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
278a0 74 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tes()..**.** The
278b0 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e   pointers return
278c0 65 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74  ed are valid unt
278d0 69 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72  il a type conver
278e0 73 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a  sion occurs as.*
278f0 2a 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76  * described abov
27900 65 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c  e, or until [sql
27910 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20  ite3_step()] or 
27920 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
27930 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ] or.** [sqlite3
27940 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20  _finalize()] is 
27950 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d  called.  The mem
27960 6f 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74  ory space used t
27970 6f 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a  o hold strings.*
27980 2a 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66  * and BLOBs is f
27990 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  reed automatical
279a0 6c 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f  ly.  Do <b>not</
279b0 62 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e  b> pass the poin
279c0 74 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a  ters returned.**
279d0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
279e0 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74  _blob()], [sqlit
279f0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
27a00 5d 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20  ], etc. into.** 
27a10 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
27a20 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d  ..**.** If a mem
27a30 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
27a40 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
27a50 6e 67 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f  ng the evaluatio
27a60 6e 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74  n of any.** of t
27a70 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61  hese routines, a
27a80 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69   default value i
27a90 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  s returned.  The
27aa0 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a   default value.*
27ab0 2a 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20  * is either the 
27ac0 69 6e 74 65 67 65 72 20 30 2c 20 74 68 65 20 66  integer 0, the f
27ad0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75  loating point nu
27ae0 6d 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e  mber 0.0, or a N
27af0 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20  ULL.** pointer. 
27b00 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   Subsequent call
27b10 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72  s to [sqlite3_er
27b20 72 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65  rcode()] will re
27b30 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  turn.** [SQLITE_
27b40 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65  NOMEM]..**.** Re
27b50 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
27b60 48 31 33 38 30 33 5d 20 5b 48 31 33 38 30 36 5d  H13803] [H13806]
27b70 20 5b 48 31 33 38 30 39 5d 20 5b 48 31 33 38 31   [H13809] [H1381
27b80 32 5d 20 5b 48 31 33 38 31 35 5d 20 5b 48 31 33  2] [H13815] [H13
27b90 38 31 38 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48  818] [H13821] [H
27ba0 31 33 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32  13824].** [H1382
27bb0 37 5d 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53  7] [H13830].*/.S
27bc0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
27bd0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
27be0 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65  lumn_blob(sqlite
27bf0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
27c00 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
27c10 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
27c20 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f  n_bytes(sqlite3_
27c30 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
27c40 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
27c50 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27c60 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f  bytes16(sqlite3_
27c70 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
27c80 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75  ;.SQLITE_API dou
27c90 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ble sqlite3_colu
27ca0 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  mn_double(sqlite
27cb0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
27cc0 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
27cd0 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
27ce0 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  n_int(sqlite3_st
27cf0 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
27d00 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
27d10 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
27d20 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71  _column_int64(sq
27d30 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
27d40 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
27d50 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  PI const unsigne
27d60 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  d char *sqlite3_
27d70 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69  column_text(sqli
27d80 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
27d90 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  Col);.SQLITE_API
27da0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
27db0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
27dc0 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
27dd0 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c  , int iCol);.SQL
27de0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
27df0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
27e00 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
27e10 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
27e20 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c  _API sqlite3_val
27e30 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ue *sqlite3_colu
27e40 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33  mn_value(sqlite3
27e50 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
27e60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
27e70 45 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72  EF: Destroy A Pr
27e80 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
27e90 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d   Object {H13300}
27ea0 20 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30   <S70300><S30100
27eb0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
27ec0 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66  te3_finalize() f
27ed0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
27ee0 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70  d to delete a [p
27ef0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
27f00 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74  t]..** If the st
27f10 61 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63  atement was exec
27f20 75 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c  uted successfull
27f30 79 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65  y or not execute
27f40 64 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a  d at all, then.*
27f50 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
27f60 65 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63  eturned. If exec
27f70 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61  ution of the sta
27f80 74 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68  tement failed th
27f90 65 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20  en an.** [error 
27fa0 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64  code] or [extend
27fb0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  ed error code] i
27fc0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
27fd0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
27fe0 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20  an be called at 
27ff0 61 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67  any point during
28000 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f   the execution o
28010 66 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72  f the.** [prepar
28020 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20  ed statement].  
28030 49 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  If the virtual m
28040 61 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a  achine has not.*
28050 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63  * completed exec
28060 75 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20  ution when this 
28070 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
28080 64 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a  d, that is like.
28090 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20  ** encountering 
280a0 61 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b  an error or an [
280b0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
280c0 74 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a  t | interrupt]..
280d0 2a 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70  ** Incomplete up
280e0 64 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c  dates may be rol
280f0 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61  led back and tra
28100 6e 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c  nsactions cancel
28110 65 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67  ed,.** depending
28120 20 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74   on the circumst
28130 61 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a  ances, and the.*
28140 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72  * [error code] r
28150 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20  eturned will be 
28160 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a  [SQLITE_ABORT]..
28170 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
28180 74 73 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20  ts:.** [H11302] 
28190 5b 48 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49  [H11304].*/.SQLI
281a0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
281b0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69  e3_finalize(sqli
281c0 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
281d0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
281e0 46 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 61  F: Reset A Prepa
281f0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62  red Statement Ob
28200 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c 53  ject {H13330} <S
28210 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  70300>.**.** The
28220 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29   sqlite3_reset()
28230 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
28240 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61 20 5b  led to reset a [
28250 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
28260 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61  nt].** object ba
28270 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61  ck to its initia
28280 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79 20 74  l state, ready t
28290 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74 65 64  o be re-executed
282a0 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 61  ..** Any SQL sta
282b0 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 73  tement variables
282c0 20 74 68 61 74 20 68 61 64 20 76 61 6c 75 65 73   that had values
282d0 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 75   bound to them u
282e0 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c  sing.** the [sql
282f0 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c  ite3_bind_blob |
28300 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28   sqlite3_bind_*(
28310 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74 68  ) API] retain th
28320 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55  eir values..** U
28330 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61  se [sqlite3_clea
28340 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f  r_bindings()] to
28350 20 72 65 73 65 74 20 74 68 65 20 62 69 6e 64 69   reset the bindi
28360 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33  ngs..**.** {H113
28370 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  32} The [sqlite3
28380 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72  _reset(S)] inter
28390 66 61 63 65 20 72 65 73 65 74 73 20 74 68 65 20  face resets the 
283a0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
283b0 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20  ent] S.**       
283c0 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62     back to the b
283d0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20  eginning of its 
283e0 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b  program..**.** {
283f0 48 31 31 33 33 34 7d 20 49 66 20 74 68 65 20 6d  H11334} If the m
28400 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20  ost recent call 
28410 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  to [sqlite3_step
28420 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20  (S)] for the.** 
28430 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72           [prepar
28440 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
28450 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45  returned [SQLITE
28460 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45  _ROW] or [SQLITE
28470 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20  _DONE],.**      
28480 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 74      or if [sqlit
28490 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20  e3_step(S)] has 
284a0 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65  never before bee
284b0 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a  n called on S,.*
284c0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20  *          then 
284d0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53  [sqlite3_reset(S
284e0 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  )] returns [SQLI
284f0 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  TE_OK]..**.** {H
28500 31 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d 6f  11336} If the mo
28510 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74  st recent call t
28520 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  o [sqlite3_step(
28530 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20  S)] for the.**  
28540 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65          [prepare
28550 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69  d statement] S i
28560 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f  ndicated an erro
28570 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20  r, then.**      
28580 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73      [sqlite3_res
28590 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 61  et(S)] returns a
285a0 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65  n appropriate [e
285b0 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a  rror code]..**.*
285c0 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20 5b  * {H11338} The [
285d0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29  sqlite3_reset(S)
285e0 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73  ] interface does
285f0 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20   not change the 
28600 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20  values.**       
28610 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74     of any [sqlit
28620 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e  e3_bind_blob|bin
28630 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70  dings] on the [p
28640 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
28650 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  t] S..*/.SQLITE_
28660 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
28670 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74  reset(sqlite3_st
28680 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a  mt *pStmt);../*.
28690 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 65  ** CAPI3REF: Cre
286a0 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20  ate Or Redefine 
286b0 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48  SQL Functions {H
286c0 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a  16100} <S20200>.
286d0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75  ** KEYWORDS: {fu
286e0 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20  nction creation 
286f0 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59  routines}.** KEY
28700 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74  WORDS: {applicat
28710 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20  ion-defined SQL 
28720 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59  function}.** KEY
28730 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74  WORDS: {applicat
28740 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20  ion-defined SQL 
28750 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a  functions}.**.**
28760 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74   These two funct
28770 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 65  ions (collective
28780 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e  ly known as "fun
28790 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72  ction creation r
287a0 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65  outines").** are
287b0 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 4c   used to add SQL
287c0 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67   functions or ag
287d0 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 72  gregates or to r
287e0 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 61  edefine the beha
287f0 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74  vior.** of exist
28800 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ing SQL function
28810 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 2e  s or aggregates.
28820 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65    The only diffe
28830 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68  rence between th
28840 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 74  e.** two is that
28850 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
28860 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 20  meter, the name 
28870 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 20  of the (scalar) 
28880 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61  function or.** a
28890 67 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e 63  ggregate, is enc
288a0 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66 6f  oded in UTF-8 fo
288b0 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  r sqlite3_create
288c0 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20  _function() and 
288d0 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71  UTF-16.** for sq
288e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
288f0 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a  ction16()..**.**
28900 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
28910 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 61 74  eter is the [dat
28920 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
28930 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 53  ] to which the S
28940 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69  QL.** function i
28950 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20 20  s to be added.  
28960 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67  If a single prog
28970 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68  ram uses more th
28980 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 0a  an one database.
28990 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e  ** connection in
289a0 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53  ternally, then S
289b0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73  QL functions mus
289c0 74 20 62 65 20 61 64 64 65 64 20 69 6e 64 69 76  t be added indiv
289d0 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61  idually to.** ea
289e0 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ch database conn
289f0 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ection..**.** Th
28a00 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
28a10 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  er is the name o
28a20 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  f the SQL functi
28a30 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  on to be created
28a40 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64   or.** redefined
28a50 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66  .  The length of
28a60 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d   the name is lim
28a70 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65  ited to 255 byte
28a80 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 0a  s, exclusive of.
28a90 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d  ** the zero-term
28aa0 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68  inator.  Note th
28ab0 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67  at the name leng
28ac0 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62  th limit is in b
28ad0 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61  ytes, not.** cha
28ae0 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74  racters.  Any at
28af0 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20  tempt to create 
28b00 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  a function with 
28b10 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a  a longer name.**
28b20 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20   will result in 
28b30 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62  [SQLITE_ERROR] b
28b40 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a  eing returned..*
28b50 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70  *.** The third p
28b60 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29 0a  arameter (nArg).
28b70 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ** is the number
28b80 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68   of arguments th
28b90 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  at the SQL funct
28ba0 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67  ion or.** aggreg
28bb0 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68  ate takes. If th
28bc0 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  is parameter is 
28bd0 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c  -1, then the SQL
28be0 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20   function or.** 
28bf0 61 67 67 72 65 67 61 74 65 20 6d 61 79 20 74 61  aggregate may ta
28c00 6b 65 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66  ke any number of
28c10 20 61 72 67 75 6d 65 6e 74 73 20 62 65 74 77 65   arguments betwe
28c20 65 6e 20 30 20 61 6e 64 20 74 68 65 20 6c 69 6d  en 0 and the lim
28c30 69 74 0a 2a 2a 20 73 65 74 20 62 79 20 5b 73 71  it.** set by [sq
28c40 6c 69 74 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51  lite3_limit]([SQ
28c50 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54  LITE_LIMIT_FUNCT
28c60 49 4f 4e 5f 41 52 47 5d 29 2e 20 20 49 66 20 74  ION_ARG]).  If t
28c70 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61  he third.** para
28c80 6d 65 74 65 72 20 69 73 20 6c 65 73 73 20 74 68  meter is less th
28c90 61 6e 20 2d 31 20 6f 72 20 67 72 65 61 74 65 72  an -1 or greater
28ca0 20 74 68 61 6e 20 31 32 37 20 74 68 65 6e 20 74   than 127 then t
28cb0 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 0a 2a  he behavior is.*
28cc0 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  * undefined..**.
28cd0 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61  ** The fourth pa
28ce0 72 61 6d 65 74 65 72 2c 20 65 54 65 78 74 52 65  rameter, eTextRe
28cf0 70 2c 20 73 70 65 63 69 66 69 65 73 20 77 68 61  p, specifies wha
28d00 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46  t.** [SQLITE_UTF
28d10 38 20 7c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  8 | text encodin
28d20 67 5d 20 74 68 69 73 20 53 51 4c 20 66 75 6e 63  g] this SQL func
28d30 74 69 6f 6e 20 70 72 65 66 65 72 73 20 66 6f 72  tion prefers for
28d40 0a 2a 2a 20 69 74 73 20 70 61 72 61 6d 65 74 65  .** its paramete
28d50 72 73 2e 20 20 41 6e 79 20 53 51 4c 20 66 75 6e  rs.  Any SQL fun
28d60 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ction implementa
28d70 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61  tion should be a
28d80 62 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77  ble to work.** w
28d90 6f 72 6b 20 77 69 74 68 20 55 54 46 2d 38 2c 20  ork with UTF-8, 
28da0 55 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46  UTF-16le, or UTF
28db0 2d 31 36 62 65 2e 20 20 42 75 74 20 73 6f 6d 65  -16be.  But some
28dc0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
28dd0 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20   may be.** more 
28de0 65 66 66 69 63 69 65 6e 74 20 77 69 74 68 20 6f  efficient with o
28df0 6e 65 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e  ne encoding than
28e00 20 61 6e 6f 74 68 65 72 2e 20 20 41 6e 20 61 70   another.  An ap
28e10 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a  plication may.**
28e20 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
28e30 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
28e40 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65  ) or sqlite3_cre
28e50 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
28e60 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d   multiple.** tim
28e70 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  es with the same
28e80 20 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69   function but wi
28e90 74 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c  th different val
28ea0 75 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e  ues of eTextRep.
28eb0 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c  .** When multipl
28ec0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
28ed0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75  s of the same fu
28ee0 6e 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c  nction are avail
28ef0 61 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20  able, SQLite.** 
28f00 77 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e  will pick the on
28f10 65 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20  e that involves 
28f20 74 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74  the least amount
28f30 20 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73   of data convers
28f40 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  ion..** If there
28f50 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c   is only a singl
28f60 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
28f70 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20   which does not 
28f80 63 61 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a  care what text.*
28f90 2a 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73  * encoding is us
28fa0 65 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75  ed, then the fou
28fb0 72 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f  rth argument sho
28fc0 75 6c 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41  uld be [SQLITE_A
28fd0 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  NY]..**.** The f
28fe0 69 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69  ifth parameter i
28ff0 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 70  s an arbitrary p
29000 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70  ointer.  The imp
29010 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
29020 68 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63  he.** function c
29030 61 6e 20 67 61 69 6e 20 61 63 63 65 73 73 20 74  an gain access t
29040 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75  o this pointer u
29050 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73  sing [sqlite3_us
29060 65 72 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a  er_data()]..**.*
29070 2a 20 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65  * The seventh, e
29080 69 67 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20  ighth and ninth 
29090 70 61 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e  parameters, xFun
290a0 63 2c 20 78 53 74 65 70 20 61 6e 64 20 78 46 69  c, xStep and xFi
290b0 6e 61 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e  nal, are.** poin
290c0 74 65 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61  ters to C-langua
290d0 67 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ge functions tha
290e0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  t implement the 
290f0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a  SQL function or.
29100 2a 2a 20 61 67 67 72 65 67 61 74 65 2e 20 41 20  ** aggregate. A 
29110 73 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74  scalar SQL funct
29120 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20  ion requires an 
29130 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
29140 66 20 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63  f the xFunc.** c
29150 61 6c 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55  allback only, NU
29160 4c 4c 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75  LL pointers shou
29170 6c 64 20 62 65 20 70 61 73 73 65 64 20 61 73 20  ld be passed as 
29180 74 68 65 20 78 53 74 65 70 20 61 6e 64 20 78 46  the xStep and xF
29190 69 6e 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65  inal.** paramete
291a0 72 73 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65  rs. An aggregate
291b0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65   SQL function re
291c0 71 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d  quires an implem
291d0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65  entation of xSte
291e0 70 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20  p.** and xFinal 
291f0 61 6e 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20  and NULL should 
29200 62 65 20 70 61 73 73 65 64 20 66 6f 72 20 78 46  be passed for xF
29210 75 6e 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61  unc. To delete a
29220 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51  n existing.** SQ
29230 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67  L function or ag
29240 67 72 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55  gregate, pass NU
29250 4c 4c 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65  LL for all three
29260 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61   function callba
29270 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  cks..**.** It is
29280 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65   permitted to re
29290 67 69 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20  gister multiple 
292a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
292b0 6f 66 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66  of the same.** f
292c0 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68  unctions with th
292d0 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20  e same name but 
292e0 77 69 74 68 20 65 69 74 68 65 72 20 64 69 66 66  with either diff
292f0 65 72 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66  ering numbers of
29300 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72  .** arguments or
29310 20 64 69 66 66 65 72 69 6e 67 20 70 72 65 66 65   differing prefe
29320 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69  rred text encodi
29330 6e 67 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ngs.  SQLite wil
29340 6c 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70  l use.** the imp
29350 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74  lementation that
29360 20 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61   most closely ma
29370 74 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e  tches the way in
29380 20 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51   which the.** SQ
29390 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  L function is us
293a0 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20  ed.  A function 
293b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
293c0 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69  ith a non-negati
293d0 76 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d  ve.** nArg param
293e0 65 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72  eter is a better
293f0 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75   match than a fu
29400 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
29410 61 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20  ation with.** a 
29420 6e 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20  negative nArg.  
29430 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65  A function where
29440 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74   the preferred t
29450 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  ext encoding.** 
29460 6d 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61  matches the data
29470 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73  base encoding is
29480 20 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74   a better.** mat
29490 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69  ch than a functi
294a0 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63  on where the enc
294b0 6f 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65  oding is differe
294c0 6e 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74  nt.  .** A funct
294d0 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e  ion where the en
294e0 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63  coding differenc
294f0 65 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46  e is between UTF
29500 31 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65  16le and UTF16be
29510 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20  .** is a closer 
29520 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e  match than a fun
29530 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20  ction where the 
29540 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65  encoding differe
29550 6e 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65  nce is.** betwee
29560 6e 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36  n UTF8 and UTF16
29570 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e  ..**.** Built-in
29580 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62   functions may b
29590 65 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20  e overloaded by 
295a0 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  new application-
295b0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
295c0 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  s..** The first 
295d0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
295e0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  ned function wit
295f0 68 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f  h a given name o
29600 76 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20  verrides all.** 
29610 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
29620 6e 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b  ns in the same [
29630 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
29640 69 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61  ion] with the sa
29650 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73  me name..** Subs
29660 65 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69  equent applicati
29670 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
29680 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
29690 20 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72   name only overr
296a0 69 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70  ide .** prior ap
296b0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
296c0 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  d functions that
296d0 20 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61   are an exact ma
296e0 74 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e  tch for the.** n
296f0 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74  umber of paramet
29700 65 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65  ers and preferre
29710 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a  d encoding..**.*
29720 2a 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  * An application
29730 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
29740 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74  n is permitted t
29750 6f 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20  o call other.** 
29760 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
29770 73 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63  s.  However, suc
29780 68 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74  h calls must not
29790 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61  .** close the da
297a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
297b0 6e 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f  n nor finalize o
297c0 72 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70  r reset the prep
297d0 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ared.** statemen
297e0 74 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66  t in which the f
297f0 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69  unction is runni
29800 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ng..**.** Requir
29810 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31  ements:.** [H161
29820 30 33 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31  03] [H16106] [H1
29830 36 31 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b  6109] [H16112] [
29840 48 31 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d  H16118] [H16121]
29850 20 5b 48 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31   [H16127].** [H1
29860 36 31 33 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b  6130] [H16133] [
29870 48 31 36 31 33 36 5d 20 5b 48 31 36 31 33 39 5d  H16136] [H16139]
29880 20 5b 48 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c   [H16142].*/.SQL
29890 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
298a0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
298b0 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ion(.  sqlite3 *
298c0 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  db,.  const char
298d0 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c   *zFunctionName,
298e0 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69  .  int nArg,.  i
298f0 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76  nt eTextRep,.  v
29900 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69  oid *pApp,.  voi
29910 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
29920 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
29930 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
29940 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70  ,.  void (*xStep
29950 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
29960 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
29970 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  alue**),.  void 
29980 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65  (*xFinal)(sqlite
29990 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53  3_context*).);.S
299a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
299b0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
299c0 63 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74  ction16(.  sqlit
299d0 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
299e0 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e  void *zFunctionN
299f0 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c  ame,.  int nArg,
29a00 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
29a10 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20  .  void *pApp,. 
29a20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
29a30 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
29a40 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
29a50 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  e**),.  void (*x
29a60 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
29a70 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
29a80 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76  e3_value**),.  v
29a90 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71  oid (*xFinal)(sq
29aa0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a  lite3_context*).
29ab0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
29ac0 45 46 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e  EF: Text Encodin
29ad0 67 73 20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30  gs {H10267} <S50
29ae0 32 30 30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a  200> <H16100>.**
29af0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
29b00 6e 74 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65  nt define intege
29b10 72 20 63 6f 64 65 73 20 74 68 61 74 20 72 65 70  r codes that rep
29b20 72 65 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f  resent the vario
29b30 75 73 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64  us.** text encod
29b40 69 6e 67 73 20 73 75 70 70 6f 72 74 65 64 20 62  ings supported b
29b50 79 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65  y SQLite..*/.#de
29b60 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 38  fine SQLITE_UTF8
29b70 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65             1.#de
29b80 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31  fine SQLITE_UTF1
29b90 36 4c 45 20 20 20 20 20 20 20 20 32 0a 23 64 65  6LE        2.#de
29ba0 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31  fine SQLITE_UTF1
29bb0 36 42 45 20 20 20 20 20 20 20 20 33 0a 23 64 65  6BE        3.#de
29bc0 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31  fine SQLITE_UTF1
29bd0 36 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20  6          4    
29be0 2f 2a 20 55 73 65 20 6e 61 74 69 76 65 20 62 79  /* Use native by
29bf0 74 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66  te order */.#def
29c00 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 59 20 20  ine SQLITE_ANY  
29c10 20 20 20 20 20 20 20 20 20 20 35 20 20 20 20 2f            5    /
29c20 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  * sqlite3_create
29c30 5f 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a  _function only *
29c40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
29c50 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 20  _UTF16_ALIGNED  
29c60 38 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f  8    /* sqlite3_
29c70 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
29c80 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   only */../*.** 
29c90 43 41 50 49 33 52 45 46 3a 20 44 65 70 72 65 63  CAPI3REF: Deprec
29ca0 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a  ated Functions.*
29cb0 2a 20 44 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a  * DEPRECATED.**.
29cc0 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f  ** These functio
29cd0 6e 73 20 61 72 65 20 5b 64 65 70 72 65 63 61 74  ns are [deprecat
29ce0 65 64 5d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74  ed].  In order t
29cf0 6f 20 6d 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61  o maintain.** ba
29d00 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
29d10 69 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72  ility with older
29d20 20 63 6f 64 65 2c 20 74 68 65 73 65 20 66 75 6e   code, these fun
29d30 63 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20  ctions continue 
29d40 0a 2a 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72  .** to be suppor
29d50 74 65 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e  ted.  However, n
29d60 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ew applications 
29d70 73 68 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20  should avoid.** 
29d80 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 73 65  the use of these
29d90 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20   functions.  To 
29da0 68 65 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70  help encourage p
29db0 65 6f 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a  eople to avoid.*
29dc0 2a 20 75 73 69 6e 67 20 74 68 65 73 65 20 66 75  * using these fu
29dd0 6e 63 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20  nctions, we are 
29de0 6e 6f 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c  not going to tel
29df0 6c 20 79 6f 75 20 77 68 61 74 20 74 68 65 79 20  l you what they 
29e00 64 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  do..*/.#ifndef S
29e10 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
29e20 43 41 54 45 44 0a 53 51 4c 49 54 45 5f 41 50 49  CATED.SQLITE_API
29e30 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
29e40 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61  ED int sqlite3_a
29e50 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73  ggregate_count(s
29e60 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
29e70 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  ;.SQLITE_API SQL
29e80 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69  ITE_DEPRECATED i
29e90 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  nt sqlite3_expir
29ea0 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  ed(sqlite3_stmt*
29eb0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  );.SQLITE_API SQ
29ec0 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
29ed0 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e  int sqlite3_tran
29ee0 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71  sfer_bindings(sq
29ef0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c  lite3_stmt*, sql
29f00 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c  ite3_stmt*);.SQL
29f10 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44  ITE_API SQLITE_D
29f20 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71  EPRECATED int sq
29f30 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63  lite3_global_rec
29f40 6f 76 65 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49  over(void);.SQLI
29f50 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45  TE_API SQLITE_DE
29f60 50 52 45 43 41 54 45 44 20 76 6f 69 64 20 73 71  PRECATED void sq
29f70 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65  lite3_thread_cle
29f80 61 6e 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49  anup(void);.SQLI
29f90 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45  TE_API SQLITE_DE
29fa0 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c  PRECATED int sql
29fb0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72  ite3_memory_alar
29fc0 6d 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  m(void(*)(void*,
29fd0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e  sqlite3_int64,in
29fe0 74 29 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  t),void*,sqlite3
29ff0 5f 69 6e 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a  _int64);.#endif.
2a000 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2a010 20 4f 62 74 61 69 6e 69 6e 67 20 53 51 4c 20 46   Obtaining SQL F
2a020 75 6e 63 74 69 6f 6e 20 50 61 72 61 6d 65 74 65  unction Paramete
2a030 72 20 56 61 6c 75 65 73 20 7b 48 31 35 31 30 30  r Values {H15100
2a040 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20200>.**.**
2a050 20 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20   The C-language 
2a060 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
2a070 66 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  f SQL functions 
2a080 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75  and aggregates u
2a090 73 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20  ses.** this set 
2a0a0 6f 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75  of interface rou
2a0b0 74 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20  tines to access 
2a0c0 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61  the parameter va
2a0d0 6c 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66  lues on.** the f
2a0e0 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65  unction or aggre
2a0f0 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  gate..**.** The 
2a100 78 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61  xFunc (for scala
2a110 72 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20  r functions) or 
2a120 78 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65  xStep (for aggre
2a130 67 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72  gates) parameter
2a140 73 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33  s.** to [sqlite3
2a150 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2a160 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
2a170 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2a180 31 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20  16()].** define 
2a190 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69  callbacks that i
2a1a0 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c  mplement the SQL
2a1b0 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61   functions and a
2a1c0 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68  ggregates..** Th
2a1d0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 4th parameter 
2a1e0 74 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63  to these callbac
2a1f0 6b 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f  ks is an array o
2a200 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a  f pointers to.**
2a210 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69   [protected sqli
2a220 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
2a230 74 73 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  ts.  There is on
2a240 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
2a250 5d 20 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20  ] object for.** 
2a260 65 61 63 68 20 70 61 72 61 6d 65 74 65 72 20 74  each parameter t
2a270 6f 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  o the SQL functi
2a280 6f 6e 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69  on.  These routi
2a290 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a  nes are used to.
2a2a0 2a 2a 20 65 78 74 72 61 63 74 20 76 61 6c 75 65  ** extract value
2a2b0 73 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69  s from the [sqli
2a2c0 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
2a2d0 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ts..**.** These 
2a2e0 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e  routines work on
2a2f0 6c 79 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74  ly with [protect
2a300 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2a310 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e  ] objects..** An
2a320 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65  y attempt to use
2a330 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
2a340 6f 6e 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74  on an [unprotect
2a350 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2a360 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75  ].** object resu
2a370 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64  lts in undefined
2a380 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a   behavior..**.**
2a390 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
2a3a0 77 6f 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74  work just like t
2a3b0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2a3c0 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20   [column access 
2a3d0 66 75 6e 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78  functions].** ex
2a3e0 63 65 70 74 20 74 68 61 74 20 20 74 68 65 73 65  cept that  these
2a3f0 20 72 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61   routines take a
2a400 20 73 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74   single [protect
2a410 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2a420 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e  ] object.** poin
2a430 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61  ter instead of a
2a440 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d   [sqlite3_stmt*]
2a450 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20   pointer and an 
2a460 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e  integer column n
2a470 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  umber..**.** The
2a480 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2a490 65 78 74 31 36 28 29 20 69 6e 74 65 72 66 61 63  ext16() interfac
2a4a0 65 20 65 78 74 72 61 63 74 73 20 61 20 55 54 46  e extracts a UTF
2a4b0 2d 31 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e  -16 string.** in
2a4c0 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65   the native byte
2a4d0 2d 6f 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f  -order of the ho
2a4e0 73 74 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 65  st machine.  The
2a4f0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  .** sqlite3_valu
2a500 65 5f 74 65 78 74 31 36 62 65 28 29 20 61 6e 64  e_text16be() and
2a510 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2a520 65 78 74 31 36 6c 65 28 29 20 69 6e 74 65 72 66  ext16le() interf
2a530 61 63 65 73 0a 2a 2a 20 65 78 74 72 61 63 74 20  aces.** extract 
2a540 55 54 46 2d 31 36 20 73 74 72 69 6e 67 73 20 61  UTF-16 strings a
2a550 73 20 62 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64  s big-endian and
2a560 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72   little-endian r
2a570 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a  espectively..**.
2a580 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76  ** The sqlite3_v
2a590 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70  alue_numeric_typ
2a5a0 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74  e() interface at
2a5b0 74 65 6d 70 74 73 20 74 6f 20 61 70 70 6c 79 0a  tempts to apply.
2a5c0 2a 2a 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e  ** numeric affin
2a5d0 69 74 79 20 74 6f 20 74 68 65 20 76 61 6c 75 65  ity to the value
2a5e0 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68  .  This means th
2a5f0 61 74 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  at an attempt is
2a600 0a 2a 2a 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76  .** made to conv
2a610 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f  ert the value to
2a620 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66   an integer or f
2a630 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20  loating point.  
2a640 49 66 0a 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e  If.** such a con
2a650 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69  version is possi
2a660 62 6c 65 20 77 69 74 68 6f 75 74 20 6c 6f 73 73  ble without loss
2a670 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20   of information 
2a680 28 69 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72  (in other.** wor
2a690 64 73 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65  ds, if the value
2a6a0 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61   is a string tha
2a6b0 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e  t looks like a n
2a6c0 75 6d 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74  umber).** then t
2a6d0 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  he conversion is
2a6e0 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68   performed.  Oth
2a6f0 65 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72  erwise no conver
2a700 73 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20  sion occurs..** 
2a710 54 68 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45  The [SQLITE_INTE
2a720 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20  GER | datatype] 
2a730 61 66 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e  after conversion
2a740 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
2a750 0a 2a 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70  .** Please pay p
2a760 61 72 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74  articular attent
2a770 69 6f 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20  ion to the fact 
2a780 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72  that the pointer
2a790 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f   returned.** fro
2a7a0 6d 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  m [sqlite3_value
2a7b0 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74  _blob()], [sqlit
2a7c0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d  e3_value_text()]
2a7d0 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  , or.** [sqlite3
2a7e0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d  _value_text16()]
2a7f0 20 63 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61   can be invalida
2a800 74 65 64 20 62 79 20 61 20 73 75 62 73 65 71 75  ted by a subsequ
2a810 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b  ent call to.** [
2a820 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2a830 74 65 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  tes()], [sqlite3
2a840 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29  _value_bytes16()
2a850 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ], [sqlite3_valu
2a860 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72  e_text()],.** or
2a870 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2a880 74 65 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a  text16()]..**.**
2a890 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
2a8a0 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66  must be called f
2a8b0 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72  rom the same thr
2a8c0 65 61 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51  ead as.** the SQ
2a8d0 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  L function that 
2a8e0 73 75 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71  supplied the [sq
2a8f0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61  lite3_value*] pa
2a900 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  rameters..**.** 
2a910 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
2a920 20 5b 48 31 35 31 30 33 5d 20 5b 48 31 35 31 30   [H15103] [H1510
2a930 36 5d 20 5b 48 31 35 31 30 39 5d 20 5b 48 31 35  6] [H15109] [H15
2a940 31 31 32 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48  112] [H15115] [H
2a950 31 35 31 31 38 5d 20 5b 48 31 35 31 32 31 5d 20  15118] [H15121] 
2a960 5b 48 31 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35  [H15124].** [H15
2a970 31 32 37 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48  127] [H15130] [H
2a980 31 35 31 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a  15133] [H15136].
2a990 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
2a9a0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
2a9b0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c  3_value_blob(sql
2a9c0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
2a9d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2a9e0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2a9f0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2aa00 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2aa10 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
2aa20 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76  ytes16(sqlite3_v
2aa30 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
2aa40 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  PI double sqlite
2aa50 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73  3_value_double(s
2aa60 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2aa70 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2aa80 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
2aa90 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2aaa0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ;.SQLITE_API sql
2aab0 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
2aac0 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73  e3_value_int64(s
2aad0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2aae0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2aaf0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
2ab00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2ab10 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  xt(sqlite3_value
2ab20 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
2ab30 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
2ab40 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
2ab50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2ab60 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
2ab70 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
2ab80 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73  value_text16le(s
2ab90 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2aba0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2abb0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
2abc0 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71  alue_text16be(sq
2abd0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2abe0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2abf0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2ac00 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2ac10 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2ac20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
2ac30 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69  umeric_type(sqli
2ac40 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a  te3_value*);../*
2ac50 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62  .** CAPI3REF: Ob
2ac60 74 61 69 6e 20 41 67 67 72 65 67 61 74 65 20 46  tain Aggregate F
2ac70 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20  unction Context 
2ac80 7b 48 31 36 32 31 30 7d 20 3c 53 32 30 32 30 30  {H16210} <S20200
2ac90 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c  >.**.** The impl
2aca0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67  ementation of ag
2acb0 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63  gregate SQL func
2acc0 74 69 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72  tions use this r
2acd0 6f 75 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61  outine to alloca
2ace0 74 65 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72  te.** a structur
2acf0 65 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68  e for storing th
2ad00 65 69 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  eir state..**.**
2ad10 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
2ad20 74 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72  the sqlite3_aggr
2ad30 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 20  egate_context() 
2ad40 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
2ad50 64 20 66 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69  d for a.** parti
2ad60 63 75 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c  cular aggregate,
2ad70 20 53 51 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65   SQLite allocate
2ad80 73 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f  s nBytes of memo
2ad90 72 79 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74  ry, zeroes out t
2ada0 68 61 74 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61  hat.** memory, a
2adb0 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
2adc0 6e 74 65 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73  nter to it. On s
2add0 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71  econd and subseq
2ade0 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  uent calls to.**
2adf0 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
2ae00 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 66 6f 72  te_context() for
2ae10 20 74 68 65 20 73 61 6d 65 20 61 67 67 72 65 67   the same aggreg
2ae20 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64  ate function ind
2ae30 65 78 2c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  ex,.** the same 
2ae40 62 75 66 66 65 72 20 69 73 20 72 65 74 75 72 6e  buffer is return
2ae50 65 64 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e  ed. The implemen
2ae60 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 67  tation of the ag
2ae70 67 72 65 67 61 74 65 20 63 61 6e 20 75 73 65 0a  gregate can use.
2ae80 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  ** the returned 
2ae90 62 75 66 66 65 72 20 74 6f 20 61 63 63 75 6d 75  buffer to accumu
2aea0 6c 61 74 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  late data..**.**
2aeb0 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69   SQLite automati
2aec0 63 61 6c 6c 79 20 66 72 65 65 73 20 74 68 65 20  cally frees the 
2aed0 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
2aee0 20 77 68 65 6e 20 74 68 65 20 61 67 67 72 65 67   when the aggreg
2aef0 61 74 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e  ate.** query con
2af00 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  cludes..**.** Th
2af10 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
2af20 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f  r should be a co
2af30 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71  py of the.** [sq
2af40 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20  lite3_context | 
2af50 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e  SQL function con
2af60 74 65 78 74 5d 20 74 68 61 74 20 69 73 20 74 68  text] that is th
2af70 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
2af80 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c  r.** to the call
2af90 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 74 68 61  back routine tha
2afa0 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
2afb0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
2afc0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
2afd0 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20  routine must be 
2afe0 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20  called from the 
2aff0 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77  same thread in w
2b000 68 69 63 68 0a 2a 2a 20 74 68 65 20 61 67 67 72  hich.** the aggr
2b010 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69  egate SQL functi
2b020 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a  on is running..*
2b030 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2b040 73 3a 0a 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b  s:.** [H16211] [
2b050 48 31 36 32 31 33 5d 20 5b 48 31 36 32 31 35 5d  H16213] [H16215]
2b060 20 5b 48 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c   [H16217].*/.SQL
2b070 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
2b080 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
2b090 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f  context(sqlite3_
2b0a0 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42  context*, int nB
2b0b0 79 74 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ytes);../*.** CA
2b0c0 50 49 33 52 45 46 3a 20 55 73 65 72 20 44 61 74  PI3REF: User Dat
2b0d0 61 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20  a For Functions 
2b0e0 7b 48 31 36 32 34 30 7d 20 3c 53 32 30 32 30 30  {H16240} <S20200
2b0f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
2b100 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20  te3_user_data() 
2b110 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
2b120 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74  s a copy of.** t
2b130 68 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20  he pointer that 
2b140 77 61 73 20 74 68 65 20 70 55 73 65 72 44 61 74  was the pUserDat
2b150 61 20 70 61 72 61 6d 65 74 65 72 20 28 74 68 65  a parameter (the
2b160 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a   5th parameter).
2b170 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ** of the [sqlit
2b180 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2b190 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71  on()].** and [sq
2b1a0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
2b1b0 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69  ction16()] routi
2b1c0 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61  nes that origina
2b1d0 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65  lly.** registere
2b1e0 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  d the applicatio
2b1f0 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n defined functi
2b200 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20  on. {END}.**.** 
2b210 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73  This routine mus
2b220 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  t be called from
2b230 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
2b240 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65   in which.** the
2b250 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
2b260 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73  ined function is
2b270 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   running..**.** 
2b280 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
2b290 20 5b 48 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c   [H16243].*/.SQL
2b2a0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
2b2b0 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
2b2c0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2b2d0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2b2e0 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e  EF: Database Con
2b2f0 6e 65 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63  nection For Func
2b300 74 69 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c  tions {H16250} <
2b310 53 36 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a  S60600><S20200>.
2b320 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2b330 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
2b340 64 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20  dle() interface 
2b350 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f  returns a copy o
2b360 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72  f.** the pointer
2b370 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73   to the [databas
2b380 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74  e connection] (t
2b390 68 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72  he 1st parameter
2b3a0 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c  ).** of the [sql
2b3b0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
2b3c0 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b  tion()].** and [
2b3d0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2b3e0 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75  unction16()] rou
2b3f0 74 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69  tines that origi
2b400 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65  nally.** registe
2b410 72 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74  red the applicat
2b420 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63  ion defined func
2b430 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  tion..**.** Requ
2b440 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
2b450 36 32 35 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  6253].*/.SQLITE_
2b460 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  API sqlite3 *sql
2b470 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
2b480 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63  handle(sqlite3_c
2b490 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  ontext*);../*.**
2b4a0 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74   CAPI3REF: Funct
2b4b0 69 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61  ion Auxiliary Da
2b4c0 74 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30  ta {H16270} <S20
2b4d0 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  200>.**.** The f
2b4e0 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e  ollowing two fun
2b4f0 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73  ctions may be us
2b500 65 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c  ed by scalar SQL
2b510 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a   functions to.**
2b520 20 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64   associate metad
2b530 61 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e  ata with argumen
2b540 74 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65  t values. If the
2b550 20 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70   same value is p
2b560 61 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74  assed to.** mult
2b570 69 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73  iple invocations
2b580 20 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c   of the same SQL
2b590 20 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67   function during
2b5a0 20 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e   query execution
2b5b0 2c 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20  , under.** some 
2b5c0 63 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68  circumstances th
2b5d0 65 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74  e associated met
2b5e0 61 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65  adata may be pre
2b5f0 73 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79  served. This may
2b600 0a 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72  .** be used, for
2b610 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64   example, to add
2b620 20 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65   a regular-expre
2b630 73 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73  ssion matching s
2b640 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f  calar.** functio
2b650 6e 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20  n. The compiled 
2b660 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72  version of the r
2b670 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
2b680 6e 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a  n is stored as.*
2b690 2a 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63  * metadata assoc
2b6a0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53  iated with the S
2b6b0 51 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20  QL value passed 
2b6c0 61 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65  as the regular e
2b6d0 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74  xpression.** pat
2b6e0 74 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69  tern.  The compi
2b6f0 6c 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72  led regular expr
2b700 65 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65  ession can be re
2b710 75 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65  used on multiple
2b720 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  .** invocations 
2b730 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63  of the same func
2b740 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65  tion so that the
2b750 20 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72   original patter
2b760 6e 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73  n string.** does
2b770 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
2b780 72 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61  recompiled on ea
2b790 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a  ch invocation..*
2b7a0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2b7b0 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69  _get_auxdata() i
2b7c0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
2b7d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2b7e0 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73  e metadata.** as
2b7f0 73 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20  sociated by the 
2b800 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
2b810 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77  ata() function w
2b820 69 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75  ith the Nth argu
2b830 6d 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f  ment.** value to
2b840 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2b850 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2b860 6e 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74  n. If no metadat
2b870 61 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a  a has been ever.
2b880 2a 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20  ** been set for 
2b890 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74  the Nth argument
2b8a0 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
2b8b0 2c 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72  , or if the corr
2b8c0 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e  esponding.** fun
2b8d0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20  ction parameter 
2b8e0 68 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63  has changed sinc
2b8f0 65 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20  e the meta-data 
2b900 77 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e  was set,.** then
2b910 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78   sqlite3_get_aux
2b920 64 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61  data() returns a
2b930 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
2b940 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2b950 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69  _set_auxdata() i
2b960 6e 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74  nterface saves t
2b970 68 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70  he metadata.** p
2b980 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73  ointed to by its
2b990 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61   3rd parameter a
2b9a0 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66  s the metadata f
2b9b0 6f 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61  or the N-th.** a
2b9c0 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  rgument of the a
2b9d0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2b9e0 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75  ed function.  Su
2b9f0 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c  bsequent.** call
2ba00 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74  s to sqlite3_get
2ba10 5f 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74  _auxdata() might
2ba20 20 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74   return this dat
2ba30 61 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20  a, if it has.** 
2ba40 6e 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79  not been destroy
2ba50 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20  ed..** If it is 
2ba60 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65  not NULL, SQLite
2ba70 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65   will invoke the
2ba80 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66   destructor.** f
2ba90 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79  unction given by
2baa0 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74   the 4th paramet
2bab0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65  er to sqlite3_se
2bac0 74 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a  t_auxdata() on.*
2bad0 2a 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77  * the metadata w
2bae0 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f  hen the correspo
2baf0 6e 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70  nding function p
2bb00 61 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73  arameter changes
2bb10 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20  .** or when the 
2bb20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  SQL statement co
2bb30 6d 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76  mpletes, whichev
2bb40 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a  er comes first..
2bb50 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20  **.** SQLite is 
2bb60 66 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65  free to call the
2bb70 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20   destructor and 
2bb80 64 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e  drop metadata on
2bb90 20 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65   any.** paramete
2bba0 72 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f  r of any functio
2bbb0 6e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20  n at any time.  
2bbc0 54 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74  The only guarant
2bbd0 65 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68  ee is that.** th
2bbe0 65 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c  e destructor wil
2bbf0 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f  l be called befo
2bc00 72 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20  re the metadata 
2bc10 69 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a  is dropped..**.*
2bc20 2a 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d  * In practice, m
2bc30 65 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65  etadata is prese
2bc40 72 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e  rved between fun
2bc50 63 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a  ction calls for.
2bc60 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74  ** expressions t
2bc70 68 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74  hat are constant
2bc80 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65   at compile time
2bc90 2e 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20  . This includes 
2bca0 6c 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65  literal.** value
2bcb0 73 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62  s and SQL variab
2bcc0 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  les..**.** These
2bcd0 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62   routines must b
2bce0 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  e called from th
2bcf0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e  e same thread in
2bd00 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51   which.** the SQ
2bd10 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75  L function is ru
2bd20 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  nning..**.** Req
2bd30 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
2bd40 31 36 32 37 32 5d 20 5b 48 31 36 32 37 34 5d 20  16272] [H16274] 
2bd50 5b 48 31 36 32 37 36 5d 20 5b 48 31 36 32 37 37  [H16276] [H16277
2bd60 5d 20 5b 48 31 36 32 37 38 5d 20 5b 48 31 36 32  ] [H16278] [H162
2bd70 37 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  79].*/.SQLITE_AP
2bd80 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
2bd90 67 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69  get_auxdata(sqli
2bda0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e  te3_context*, in
2bdb0 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  t N);.SQLITE_API
2bdc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65   void sqlite3_se
2bdd0 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65  t_auxdata(sqlite
2bde0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20  3_context*, int 
2bdf0 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28  N, void*, void (
2be00 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a  *)(void*));.../*
2be10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f  .** CAPI3REF: Co
2be20 6e 73 74 61 6e 74 73 20 44 65 66 69 6e 69 6e 67  nstants Defining
2be30 20 53 70 65 63 69 61 6c 20 44 65 73 74 72 75 63   Special Destruc
2be40 74 6f 72 20 42 65 68 61 76 69 6f 72 20 7b 48 31  tor Behavior {H1
2be50 30 32 38 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a  0280} <S30100>.*
2be60 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73  *.** These are s
2be70 70 65 63 69 61 6c 20 76 61 6c 75 65 73 20 66 6f  pecial values fo
2be80 72 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  r the destructor
2be90 20 74 68 61 74 20 69 73 20 70 61 73 73 65 64 20   that is passed 
2bea0 69 6e 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e  in as the.** fin
2beb0 61 6c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72  al argument to r
2bec0 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71  outines like [sq
2bed0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
2bee0 62 28 29 5d 2e 20 20 49 66 20 74 68 65 20 64 65  b()].  If the de
2bef0 73 74 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75  structor.** argu
2bf00 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 53  ment is SQLITE_S
2bf10 54 41 54 49 43 2c 20 69 74 20 6d 65 61 6e 73 20  TATIC, it means 
2bf20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
2bf30 20 70 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e 73   pointer is cons
2bf40 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c  tant.** and will
2bf50 20 6e 65 76 65 72 20 63 68 61 6e 67 65 2e 20 20   never change.  
2bf60 49 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  It does not need
2bf70 20 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64   to be destroyed
2bf80 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45  .  The.** SQLITE
2bf90 5f 54 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65  _TRANSIENT value
2bfa0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2bfb0 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b  content will lik
2bfc0 65 6c 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a  ely change in.**
2bfd0 20 74 68 65 20 6e 65 61 72 20 66 75 74 75 72 65   the near future
2bfe0 20 61 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65   and that SQLite
2bff0 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73   should make its
2c000 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70   own private cop
2c010 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  y of.** the cont
2c020 65 6e 74 20 62 65 66 6f 72 65 20 72 65 74 75 72  ent before retur
2c030 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ning..**.** The 
2c040 74 79 70 65 64 65 66 20 69 73 20 6e 65 63 65 73  typedef is neces
2c050 73 61 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f  sary to work aro
2c060 75 6e 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20  und problems in 
2c070 63 65 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63  certain.** C++ c
2c080 6f 6d 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74  ompilers.  See t
2c090 69 63 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a  icket #2191..*/.
2c0a0 74 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73  typedef void (*s
2c0b0 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f  qlite3_destructo
2c0c0 72 5f 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a  r_type)(void*);.
2c0d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
2c0e0 54 41 54 49 43 20 20 20 20 20 20 28 28 73 71 6c  TATIC      ((sql
2c0f0 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f  ite3_destructor_
2c100 74 79 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20  type)0).#define 
2c110 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
2c120 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73     ((sqlite3_des
2c130 74 72 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29  tructor_type)-1)
2c140 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2c150 3a 20 53 65 74 74 69 6e 67 20 54 68 65 20 52 65  : Setting The Re
2c160 73 75 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46  sult Of An SQL F
2c170 75 6e 63 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d  unction {H16400}
2c180 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20200>.**.** 
2c190 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
2c1a0 72 65 20 75 73 65 64 20 62 79 20 74 68 65 20 78  re used by the x
2c1b0 46 75 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63  Func or xFinal c
2c1c0 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a  allbacks that.**
2c1d0 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66   implement SQL f
2c1e0 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67  unctions and agg
2c1f0 72 65 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a  regates.  See.**
2c200 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
2c210 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64  _function()] and
2c220 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
2c230 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a  _function16()].*
2c240 2a 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  * for additional
2c250 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
2c260 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69  .** These functi
2c270 6f 6e 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75  ons work very mu
2c280 63 68 20 6c 69 6b 65 20 74 68 65 20 5b 70 61 72  ch like the [par
2c290 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 5d 20  ameter binding] 
2c2a0 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e  family of.** fun
2c2b0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 62  ctions used to b
2c2c0 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 68 6f  ind values to ho
2c2d0 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e  st parameters in
2c2e0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2c2f0 65 6e 74 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74  ents..** Refer t
2c300 6f 20 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d  o the [SQL param
2c310 65 74 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74  eter] documentat
2c320 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ion for addition
2c330 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
2c340 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2c350 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20  3_result_blob() 
2c360 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74  interface sets t
2c370 68 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a  he result from.*
2c380 2a 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  * an application
2c390 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2c3a0 6e 20 74 6f 20 62 65 20 74 68 65 20 42 4c 4f 42  n to be the BLOB
2c3b0 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69   whose content i
2c3c0 73 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20  s pointed.** to 
2c3d0 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  by the second pa
2c3e0 72 61 6d 65 74 65 72 20 61 6e 64 20 77 68 69 63  rameter and whic
2c3f0 68 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e  h is N bytes lon
2c400 67 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  g where N is the
2c410 0a 2a 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65  .** third parame
2c420 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ter..**.** The s
2c430 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65  qlite3_result_ze
2c440 72 6f 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61  roblob() interfa
2c450 63 65 73 20 73 65 74 20 74 68 65 20 72 65 73 75  ces set the resu
2c460 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70  lt of.** the app
2c470 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
2c480 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
2c490 61 20 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e  a BLOB containin
2c4a0 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79  g all zero.** by
2c4b0 74 65 73 20 61 6e 64 20 4e 20 62 79 74 65 73 20  tes and N bytes 
2c4c0 69 6e 20 73 69 7a 65 2c 20 77 68 65 72 65 20 4e  in size, where N
2c4d0 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66   is the value of
2c4e0 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
2c4f0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  er..**.** The sq
2c500 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
2c510 62 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20  ble() interface 
2c520 73 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20  sets the result 
2c530 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69  from.** an appli
2c540 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
2c550 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20  unction to be a 
2c560 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
2c570 61 6c 75 65 20 73 70 65 63 69 66 69 65 64 0a 2a  alue specified.*
2c580 2a 20 62 79 20 69 74 73 20 32 6e 64 20 61 72 67  * by its 2nd arg
2c590 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ument..**.** The
2c5a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2c5b0 65 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69  error() and sqli
2c5c0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2c5d0 31 36 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  16() functions.*
2c5e0 2a 20 63 61 75 73 65 20 74 68 65 20 69 6d 70 6c  * cause the impl
2c5f0 65 6d 65 6e 74 65 64 20 53 51 4c 20 66 75 6e 63  emented SQL func
2c600 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e  tion to throw an
2c610 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53   exception..** S
2c620 51 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73  QLite uses the s
2c630 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f  tring pointed to
2c640 20 62 79 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70   by the.** 2nd p
2c650 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69  arameter of sqli
2c660 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2c670 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65  () or sqlite3_re
2c680 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a  sult_error16().*
2c690 2a 20 61 73 20 74 68 65 20 74 65 78 74 20 6f 66  * as the text of
2c6a0 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
2c6b0 65 2e 20 20 53 51 4c 69 74 65 20 69 6e 74 65 72  e.  SQLite inter
2c6c0 70 72 65 74 73 20 74 68 65 20 65 72 72 6f 72 0a  prets the error.
2c6d0 2a 2a 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e  ** message strin
2c6e0 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72  g from sqlite3_r
2c6f0 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 73  esult_error() as
2c700 20 55 54 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a   UTF-8. SQLite.*
2c710 2a 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65  * interprets the
2c720 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c   string from sql
2c730 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2c740 72 31 36 28 29 20 61 73 20 55 54 46 2d 31 36 20  r16() as UTF-16 
2c750 69 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74  in native.** byt
2c760 65 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65  e order.  If the
2c770 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
2c780 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75   to sqlite3_resu
2c790 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72  lt_error().** or
2c7a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2c7b0 65 72 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67  error16() is neg
2c7c0 61 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74  ative then SQLit
2c7d0 65 20 74 61 6b 65 73 20 61 73 20 74 68 65 20 65  e takes as the e
2c7e0 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20  rror.** message 
2c7f0 61 6c 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f  all text up thro
2c800 75 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65  ugh the first ze
2c810 72 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a  ro character..**
2c820 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61   If the third pa
2c830 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
2c840 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2c850 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ) or.** sqlite3_
2c860 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29  result_error16()
2c870 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   is non-negative
2c880 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b   then SQLite tak
2c890 65 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20  es that many.** 
2c8a0 62 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61  bytes (not chara
2c8b0 63 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20  cters) from the 
2c8c0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73  2nd parameter as
2c8d0 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
2c8e0 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ge..** The sqlit
2c8f0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2c900 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  ) and sqlite3_re
2c910 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a  sult_error16().*
2c920 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20  * routines make 
2c930 61 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f  a private copy o
2c940 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  f the error mess
2c950 61 67 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a  age text before.
2c960 2a 2a 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20  ** they return. 
2c970 20 48 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c   Hence, the call
2c980 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  ing function can
2c990 20 64 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a   deallocate or.*
2c9a0 2a 20 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78  * modify the tex
2c9b0 74 20 61 66 74 65 72 20 74 68 65 79 20 72 65 74  t after they ret
2c9c0 75 72 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d  urn without harm
2c9d0 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  ..** The sqlite3
2c9e0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  _result_error_co
2c9f0 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 68  de() function ch
2ca00 61 6e 67 65 73 20 74 68 65 20 65 72 72 6f 72 20  anges the error 
2ca10 63 6f 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64  code.** returned
2ca20 20 62 79 20 53 51 4c 69 74 65 20 61 73 20 61 20   by SQLite as a 
2ca30 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 72  result of an err
2ca40 6f 72 20 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e  or in a function
2ca50 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a  .  By default,.*
2ca60 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  * the error code
2ca70 20 69 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   is SQLITE_ERROR
2ca80 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20  .  A subsequent 
2ca90 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
2caa0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a  result_error().*
2cab0 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73  * or sqlite3_res
2cac0 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65  ult_error16() re
2cad0 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 63  sets the error c
2cae0 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52  ode to SQLITE_ER
2caf0 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ROR..**.** The s
2cb00 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f  qlite3_result_to
2cb10 6f 62 69 67 28 29 20 69 6e 74 65 72 66 61 63 65  obig() interface
2cb20 20 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74   causes SQLite t
2cb30 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72  o throw an error
2cb40 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74  .** indicating t
2cb50 68 61 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20  hat a string or 
2cb60 42 4c 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20  BLOB is to long 
2cb70 74 6f 20 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a  to represent..**
2cb80 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2cb90 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69  result_nomem() i
2cba0 6e 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20  nterface causes 
2cbb0 53 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20  SQLite to throw 
2cbc0 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69  an error.** indi
2cbd0 63 61 74 69 6e 67 20 74 68 61 74 20 61 20 6d 65  cating that a me
2cbe0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
2cbf0 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  failed..**.** Th
2cc00 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2cc10 5f 69 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65  _int() interface
2cc20 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e   sets the return
2cc30 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65   value.** of the
2cc40 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
2cc50 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ined function to
2cc60 20 62 65 20 74 68 65 20 33 32 2d 62 69 74 20 73   be the 32-bit s
2cc70 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
2cc80 20 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20   value given in 
2cc90 74 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74  the 2nd argument
2cca0 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  ..** The sqlite3
2ccb0 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20  _result_int64() 
2ccc0 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74  interface sets t
2ccd0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
2cce0 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63  ** of the applic
2ccf0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2cd00 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65  nction to be the
2cd10 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
2cd20 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20  nteger.** value 
2cd30 67 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64  given in the 2nd
2cd40 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
2cd50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2cd60 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72  ult_null() inter
2cd70 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
2cd80 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66  turn value.** of
2cd90 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2cda0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2cdb0 6e 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a  n to be NULL..**
2cdc0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2cdd0 72 65 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73  result_text(), s
2cde0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2cdf0 78 74 31 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74  xt16(),.** sqlit
2ce00 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
2ce10 6c 65 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65  le(), and sqlite
2ce20 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  3_result_text16b
2ce30 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a  e() interfaces.*
2ce40 2a 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e  * set the return
2ce50 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70   value of the ap
2ce60 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2ce70 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
2ce80 0a 2a 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e  .** a text strin
2ce90 67 20 77 68 69 63 68 20 69 73 20 72 65 70 72 65  g which is repre
2cea0 73 65 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c  sented as UTF-8,
2ceb0 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62   UTF-16 native b
2cec0 79 74 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54  yte order,.** UT
2ced0 46 2d 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69  F-16 little endi
2cee0 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69  an, or UTF-16 bi
2cef0 67 20 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63  g endian, respec
2cf00 74 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74  tively..** SQLit
2cf10 65 20 74 61 6b 65 73 20 74 68 65 20 74 65 78 74  e takes the text
2cf20 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65   result from the
2cf30 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f   application fro
2cf40 6d 0a 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72  m.** the 2nd par
2cf50 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 73 71  ameter of the sq
2cf60 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2cf70 74 2a 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a  t* interfaces..*
2cf80 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72  * If the 3rd par
2cf90 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
2cfa0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2cfb0 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  t* interfaces.**
2cfc0 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
2cfd0 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20  en SQLite takes 
2cfe0 72 65 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d  result text from
2cff0 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
2d000 65 72 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68  er.** through th
2d010 65 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61  e first zero cha
2d020 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68  racter..** If th
2d030 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
2d040 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  to the sqlite3_r
2d050 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65  esult_text* inte
2d060 72 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e  rfaces.** is non
2d070 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20  -negative, then 
2d080 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e  as many bytes (n
2d090 6f 74 20 63 68 61 72 61 63 74 65 72 73 29 20 6f  ot characters) o
2d0a0 66 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f  f the text.** po
2d0b0 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20  inted to by the 
2d0c0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 72  2nd parameter ar
2d0d0 65 20 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61  e taken as the a
2d0e0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2d0f0 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72  ed.** function r
2d100 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65  esult..** If the
2d110 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2d120 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
2d130 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
2d140 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69  faces.** or sqli
2d150 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
2d160 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f  is a non-NULL po
2d170 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69  inter, then SQLi
2d180 74 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a  te calls that.**
2d190 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65   function as the
2d1a0 20 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74   destructor on t
2d1b0 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20  he text or BLOB 
2d1c0 72 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68  result when it h
2d1d0 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75  as.** finished u
2d1e0 73 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74  sing that result
2d1f0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20  ..** If the 4th 
2d200 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
2d210 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2d220 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73  text* interfaces
2d230 20 6f 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65   or to.** sqlite
2d240 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73  3_result_blob is
2d250 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e   the special con
2d260 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41  stant SQLITE_STA
2d270 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  TIC, then SQLite
2d280 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74  .** assumes that
2d290 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f   the text or BLO
2d2a0 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63  B result is in c
2d2b0 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e  onstant space an
2d2c0 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f  d does not.** co
2d2d0 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  py the content o
2d2e0 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  f the parameter 
2d2f0 6e 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72  nor call a destr
2d300 75 63 74 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e  uctor on the con
2d310 74 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20  tent.** when it 
2d320 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69  has finished usi
2d330 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a  ng that result..
2d340 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61  ** If the 4th pa
2d350 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73  rameter to the s
2d360 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2d370 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a  xt* interfaces.*
2d380 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73  * or sqlite3_res
2d390 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20  ult_blob is the 
2d3a0 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74  special constant
2d3b0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
2d3c0 54 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 65  T.** then SQLite
2d3d0 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66   makes a copy of
2d3e0 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f   the result into
2d3f0 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20   space obtained 
2d400 66 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71  from.** from [sq
2d410 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20  lite3_malloc()] 
2d420 62 65 66 6f 72 65 20 69 74 20 72 65 74 75 72 6e  before it return
2d430 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  s..**.** The sql
2d440 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
2d450 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  e() interface se
2d460 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  ts the result of
2d470 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
2d480 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2d490 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f 70  tion to be a cop
2d4a0 79 20 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74  y the.** [unprot
2d4b0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
2d4c0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 63  lue] object spec
2d4d0 69 66 69 65 64 20 62 79 20 74 68 65 20 32 6e 64  ified by the 2nd
2d4e0 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65   parameter.  The
2d4f0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75  .** sqlite3_resu
2d500 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72  lt_value() inter
2d510 66 61 63 65 20 6d 61 6b 65 73 20 61 20 63 6f 70  face makes a cop
2d520 79 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  y of the [sqlite
2d530 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74  3_value].** so t
2d540 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33  hat the [sqlite3
2d550 5f 76 61 6c 75 65 5d 20 73 70 65 63 69 66 69 65  _value] specifie
2d560 64 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74  d in the paramet
2d570 65 72 20 6d 61 79 20 63 68 61 6e 67 65 20 6f 72  er may change or
2d580 0a 2a 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 74  .** be deallocat
2d590 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33  ed after sqlite3
2d5a0 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20  _result_value() 
2d5b0 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20  returns without 
2d5c0 68 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74  harm..** A [prot
2d5d0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
2d5e0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20  lue] object may 
2d5f0 61 6c 77 61 79 73 20 62 65 20 75 73 65 64 20 77  always be used w
2d600 68 65 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72  here an.** [unpr
2d610 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
2d620 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73  value] object is
2d630 20 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69   required, so ei
2d640 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20  ther.** kind of 
2d650 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20  [sqlite3_value] 
2d660 6f 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73  object can be us
2d670 65 64 20 77 69 74 68 20 74 68 69 73 20 69 6e 74  ed with this int
2d680 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  erface..**.** If
2d690 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
2d6a0 61 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20  are called from 
2d6b0 77 69 74 68 69 6e 20 74 68 65 20 64 69 66 66 65  within the diffe
2d6c0 72 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 74  rent thread.** t
2d6d0 68 61 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74  han the one cont
2d6e0 61 69 6e 69 6e 67 20 74 68 65 20 61 70 70 6c 69  aining the appli
2d6f0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
2d700 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65 63  unction that rec
2d710 65 69 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71  eived.** the [sq
2d720 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70  lite3_context] p
2d730 6f 69 6e 74 65 72 2c 20 74 68 65 20 72 65 73 75  ointer, the resu
2d740 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65  lts are undefine
2d750 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  d..**.** Require
2d760 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30  ments:.** [H1640
2d770 33 5d 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 36  3] [H16406] [H16
2d780 34 30 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48  409] [H16412] [H
2d790 31 36 34 31 35 5d 20 5b 48 31 36 34 31 38 5d 20  16415] [H16418] 
2d7a0 5b 48 31 36 34 32 31 5d 20 5b 48 31 36 34 32 34  [H16421] [H16424
2d7b0 5d 0a 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48  ].** [H16427] [H
2d7c0 31 36 34 33 30 5d 20 5b 48 31 36 34 33 33 5d 20  16430] [H16433] 
2d7d0 5b 48 31 36 34 33 36 5d 20 5b 48 31 36 34 33 39  [H16436] [H16439
2d7e0 5d 20 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 34  ] [H16442] [H164
2d7f0 34 35 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20  45] [H16448].** 
2d800 5b 48 31 36 34 35 31 5d 20 5b 48 31 36 34 35 34  [H16451] [H16454
2d810 5d 20 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 34  ] [H16457] [H164
2d820 36 30 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a  60] [H16463].*/.
2d830 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2d840 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
2d850 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  lob(sqlite3_cont
2d860 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ext*, const void
2d870 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28  *, int, void(*)(
2d880 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  void*));.SQLITE_
2d890 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2d8a0 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73  _result_double(s
2d8b0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2d8c0 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45   double);.SQLITE
2d8d0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2d8e0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73  3_result_error(s
2d8f0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2d900 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e   const char*, in
2d910 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  t);.SQLITE_API v
2d920 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
2d930 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74  lt_error16(sqlit
2d940 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
2d950 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a  st void*, int);.
2d960 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2d970 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2d980 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69  rror_toobig(sqli
2d990 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53  te3_context*);.S
2d9a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2d9b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2d9c0 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65  ror_nomem(sqlite
2d9d0 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c  3_context*);.SQL
2d9e0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2d9f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2da00 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63  r_code(sqlite3_c
2da10 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53  ontext*, int);.S
2da20 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2da30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
2da40 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  t(sqlite3_contex
2da50 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  t*, int);.SQLITE
2da60 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2da70 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73  3_result_int64(s
2da80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2da90 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b   sqlite3_int64);
2daa0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2dab0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2dac0 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e  null(sqlite3_con
2dad0 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  text*);.SQLITE_A
2dae0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2daf0 72 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69  result_text(sqli
2db00 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
2db10 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20  nst char*, int, 
2db20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
2db30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2db40 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2db50 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63  text16(sqlite3_c
2db60 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76  ontext*, const v
2db70 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28  oid*, int, void(
2db80 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
2db90 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2dba0 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
2dbb0 36 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  6le(sqlite3_cont
2dbc0 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ext*, const void
2dbd0 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  *, int,void(*)(v
2dbe0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
2dbf0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2dc00 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28  result_text16be(
2dc10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2dc20 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
2dc30 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  nt,void(*)(void*
2dc40 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ));.SQLITE_API v
2dc50 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
2dc60 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33  lt_value(sqlite3
2dc70 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74  _context*, sqlit
2dc80 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
2dc90 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2dca0 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62  te3_result_zerob
2dcb0 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  lob(sqlite3_cont
2dcc0 65 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f  ext*, int n);../
2dcd0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
2dce0 65 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74  efine New Collat
2dcf0 69 6e 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48  ing Sequences {H
2dd00 31 36 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a  16600} <S20300>.
2dd10 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63  **.** These func
2dd20 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74  tions are used t
2dd30 6f 20 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74  o add new collat
2dd40 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f  ion sequences to
2dd50 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73   the.** [databas
2dd60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70  e connection] sp
2dd70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20 66  ecified as the f
2dd80 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  irst argument..*
2dd90 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  *.** The name of
2dda0 20 74 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69   the new collati
2ddb0 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 73  on sequence is s
2ddc0 70 65 63 69 66 69 65 64 20 61 73 20 61 20 55 54  pecified as a UT
2ddd0 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f  F-8 string.** fo
2dde0 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  r sqlite3_create
2ddf0 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64  _collation() and
2de00 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2de10 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a  collation_v2().*
2de20 2a 20 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73  * and a UTF-16 s
2de30 74 72 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65  tring for sqlite
2de40 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
2de50 6f 6e 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63  on16(). In all c
2de60 61 73 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65  ases.** the name
2de70 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
2de80 65 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f  e second functio
2de90 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  n argument..**.*
2dea0 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75  * The third argu
2deb0 6d 65 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20  ment may be one 
2dec0 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73  of the constants
2ded0 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a   [SQLITE_UTF8],.
2dee0 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  ** [SQLITE_UTF16
2def0 4c 45 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  LE], or [SQLITE_
2df00 55 54 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 61  UTF16BE], indica
2df10 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 75 73  ting that the us
2df20 65 72 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72  er-supplied.** r
2df30 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73 20 74  outine expects t
2df40 6f 20 62 65 20 70 61 73 73 65 64 20 70 6f 69 6e  o be passed poin
2df50 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20  ters to strings 
2df60 65 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 55 54  encoded using UT
2df70 46 2d 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c  F-8,.** UTF-16 l
2df80 69 74 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72  ittle-endian, or
2df90 20 55 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 69   UTF-16 big-endi
2dfa0 61 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  an, respectively
2dfb0 2e 20 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 61  . The.** third a
2dfc0 72 67 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c  rgument might al
2dfd0 73 6f 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54  so be [SQLITE_UT
2dfe0 46 31 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 65  F16] to indicate
2dff0 20 74 68 61 74 20 74 68 65 20 72 6f 75 74 69 6e   that the routin
2e000 65 0a 2a 2a 20 65 78 70 65 63 74 73 20 70 6f 69  e.** expects poi
2e010 6e 74 65 72 73 20 74 6f 20 62 65 20 55 54 46 2d  nters to be UTF-
2e020 31 36 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68  16 strings in th
2e030 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  e native byte or
2e040 64 65 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61  der, or the.** a
2e050 72 67 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b  rgument can be [
2e060 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
2e070 47 4e 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20  GNED] if the.** 
2e080 74 68 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65  the routine expe
2e090 63 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  cts pointers to 
2e0a0 31 36 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67  16-bit word alig
2e0b0 6e 65 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f  ned strings.** o
2e0c0 66 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20  f UTF-16 in the 
2e0d0 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
2e0e0 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74  r..**.** A point
2e0f0 65 72 20 74 6f 20 74 68 65 20 75 73 65 72 20 73  er to the user s
2e100 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20  upplied routine 
2e110 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 20 61  must be passed a
2e120 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 61  s the fifth.** a
2e130 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 20  rgument.  If it 
2e140 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73  is NULL, this is
2e150 20 74 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c   the same as del
2e160 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74  eting the collat
2e170 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ion.** sequence 
2e180 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20  (so that SQLite 
2e190 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61  cannot call it a
2e1a0 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68  nymore)..** Each
2e1b0 20 74 69 6d 65 20 74 68 65 20 61 70 70 6c 69 63   time the applic
2e1c0 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 66  ation supplied f
2e1d0 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
2e1e0 65 64 2c 20 69 74 20 69 73 20 70 61 73 73 65 64  ed, it is passed
2e1f0 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 72 73 74  .** as its first
2e200 20 70 61 72 61 6d 65 74 65 72 20 61 20 63 6f 70   parameter a cop
2e210 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70  y of the void* p
2e220 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6f 75  assed as the fou
2e230 72 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  rth argument.** 
2e240 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  to sqlite3_creat
2e250 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72  e_collation() or
2e260 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2e270 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a  collation16()..*
2e280 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69  *.** The remaini
2e290 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ng arguments to 
2e2a0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2e2b0 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65  supplied routine
2e2c0 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73   are two strings
2e2d0 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 65 73  ,.** each repres
2e2e0 65 6e 74 65 64 20 62 79 20 61 20 28 6c 65 6e 67  ented by a (leng
2e2f0 74 68 2c 20 64 61 74 61 29 20 70 61 69 72 20 61  th, data) pair a
2e300 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68  nd encoded in th
2e310 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68  e encoding.** th
2e320 61 74 20 77 61 73 20 70 61 73 73 65 64 20 61 73  at was passed as
2e330 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   the third argum
2e340 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63 6f 6c  ent when the col
2e350 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2e360 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65  was.** registere
2e370 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 70  d. {END}  The ap
2e380 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65  plication define
2e390 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74  d collation rout
2e3a0 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65  ine should.** re
2e3b0 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a  turn negative, z
2e3c0 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20  ero or positive 
2e3d0 69 66 20 74 68 65 20 66 69 72 73 74 20 73 74 72  if the first str
2e3e0 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e  ing is less than
2e3f0 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f  ,.** equal to, o
2e400 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  r greater than t
2e410 68 65 20 73 65 63 6f 6e 64 20 73 74 72 69 6e 67  he second string
2e420 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20  . i.e. (STRING1 
2e430 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a  - STRING2)..**.*
2e440 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72  * The sqlite3_cr
2e450 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
2e460 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73  2() works like s
2e470 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2e480 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63  llation().** exc
2e490 65 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65  ept that it take
2e4a0 73 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d  s an extra argum
2e4b0 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 64  ent which is a d
2e4c0 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a  estructor for.**
2e4d0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20   the collation. 
2e4e0 20 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   The destructor 
2e4f0 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  is called when t
2e500 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a  he collation is.
2e510 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64  ** destroyed and
2e520 20 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70   is passed a cop
2e530 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20  y of the fourth 
2e540 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20  parameter void* 
2e550 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68  pointer.** of th
2e560 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
2e570 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e  _collation_v2().
2e580 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61  .** Collations a
2e590 72 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 65  re destroyed whe
2e5a0 6e 20 74 68 65 79 20 61 72 65 20 6f 76 65 72 72  n they are overr
2e5b0 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72 20 63  idden by later c
2e5c0 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63  alls to the.** c
2e5d0 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f  ollation creatio
2e5e0 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77  n functions or w
2e5f0 68 65 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73  hen the [databas
2e600 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73  e connection] is
2e610 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67   closed.** using
2e620 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28   [sqlite3_close(
2e630 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  )]..**.** See al
2e640 73 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f  so:  [sqlite3_co
2e650 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29  llation_needed()
2e660 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  ] and [sqlite3_c
2e670 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
2e680 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  6()]..**.** Requ
2e690 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
2e6a0 36 36 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b  6603] [H16604] [
2e6b0 48 31 36 36 30 36 5d 20 5b 48 31 36 36 30 39 5d  H16606] [H16609]
2e6c0 20 5b 48 31 36 36 31 32 5d 20 5b 48 31 36 36 31   [H16612] [H1661
2e6d0 35 5d 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 36  5] [H16618] [H16
2e6e0 36 32 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d  621].** [H16624]
2e6f0 20 5b 48 31 36 36 32 37 5d 20 5b 48 31 36 36 33   [H16627] [H1663
2e700 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  0].*/.SQLITE_API
2e710 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
2e720 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20  ate_collation(. 
2e730 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f   sqlite3*, .  co
2e740 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
2e750 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70   .  int eTextRep
2e760 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e  , .  void*,.  in
2e770 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69  t(*xCompare)(voi
2e780 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2e790 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2e7a0 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  d*).);.SQLITE_AP
2e7b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
2e7c0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
2e7d0 32 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  2(.  sqlite3*, .
2e7e0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2e7f0 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78  ame, .  int eTex
2e800 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a  tRep, .  void*,.
2e810 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
2e820 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
2e830 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
2e840 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28   void*),.  void(
2e850 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a  *xDestroy)(void*
2e860 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ).);.SQLITE_API 
2e870 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
2e880 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a  te_collation16(.
2e890 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63    sqlite3*, .  c
2e8a0 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65  onst void *zName
2e8b0 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70  ,.  int eTextRep
2e8c0 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e  , .  void*,.  in
2e8d0 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69  t(*xCompare)(voi
2e8e0 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2e8f0 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2e900 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  d*).);../*.** CA
2e910 50 49 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f  PI3REF: Collatio
2e920 6e 20 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63  n Needed Callbac
2e930 6b 73 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30  ks {H16700} <S20
2e940 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76  300>.**.** To av
2e950 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65  oid having to re
2e960 67 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61  gister all colla
2e970 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62  tion sequences b
2e980 65 66 6f 72 65 20 61 20 64 61 74 61 62 61 73 65  efore a database
2e990 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c  .** can be used,
2e9a0 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61   a single callba
2e9b0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20  ck function may 
2e9c0 62 65 20 72 65 67 69 73 74 65 72 65 64 20 77 69  be registered wi
2e9d0 74 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62  th the.** [datab
2e9e0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
2e9f0 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65  to be called whe
2ea00 6e 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e  never an undefin
2ea10 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20  ed collation.** 
2ea20 73 65 71 75 65 6e 63 65 20 69 73 20 72 65 71 75  sequence is requ
2ea30 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ired..**.** If t
2ea40 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72  he function is r
2ea50 65 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20  egistered using 
2ea60 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  the sqlite3_coll
2ea70 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41  ation_needed() A
2ea80 50 49 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69  PI,.** then it i
2ea90 73 20 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d  s passed the nam
2eaa0 65 73 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20  es of undefined 
2eab0 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2eac0 63 65 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a  ces as strings.*
2ead0 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46  * encoded in UTF
2eae0 2d 38 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20  -8. {H16703} If 
2eaf0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
2eb00 6e 5f 6e 65 65 64 65 64 31 36 28 29 20 69 73 20  n_needed16() is 
2eb10 75 73 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d  used,.** the nam
2eb20 65 73 20 61 72 65 20 70 61 73 73 65 64 20 61 73  es are passed as
2eb30 20 55 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69   UTF-16 in machi
2eb40 6e 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  ne native byte o
2eb50 72 64 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20  rder..** A call 
2eb60 74 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74 69  to either functi
2eb70 6f 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20  on replaces any 
2eb80 65 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63  existing callbac
2eb90 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  k..**.** When th
2eba0 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  e callback is in
2ebb0 76 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74  voked, the first
2ebc0 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
2ebd0 20 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66   is a copy.** of
2ebe0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
2ebf0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  ment to sqlite3_
2ec00 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
2ec10 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33  () or.** sqlite3
2ec20 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
2ec30 64 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f  d16().  The seco
2ec40 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nd argument is t
2ec50 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  he database.** c
2ec60 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20  onnection.  The 
2ec70 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69  third argument i
2ec80 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45  s one of [SQLITE
2ec90 5f 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f  _UTF8], [SQLITE_
2eca0 55 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20  UTF16BE],.** or 
2ecb0 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d  [SQLITE_UTF16LE]
2ecc0 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65  , indicating the
2ecd0 20 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20   most desirable 
2ece0 66 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c  form of the coll
2ecf0 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63  ation.** sequenc
2ed00 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69  e function requi
2ed10 72 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68  red.  The fourth
2ed20 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
2ed30 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a  e name of the.**
2ed40 20 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74   required collat
2ed50 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a  ion sequence..**
2ed60 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  .** The callback
2ed70 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
2ed80 20 72 65 67 69 73 74 65 72 20 74 68 65 20 64 65   register the de
2ed90 73 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  sired collation 
2eda0 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65  using.** [sqlite
2edb0 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
2edc0 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  on()], [sqlite3_
2edd0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2ede0 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71  16()], or.** [sq
2edf0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2ee00 6c 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a  lation_v2()]..**
2ee10 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2ee20 3a 0a 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48  :.** [H16702] [H
2ee30 31 36 37 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a  16704] [H16706].
2ee40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2ee50 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  t sqlite3_collat
2ee60 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71  ion_needed(.  sq
2ee70 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a  lite3*, .  void*
2ee80 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69  , .  void(*)(voi
2ee90 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
2eea0 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63  eTextRep,const c
2eeb0 68 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f  har*).);.SQLITE_
2eec0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2eed0 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
2eee0 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  16(.  sqlite3*, 
2eef0 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64  .  void*,.  void
2ef00 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
2ef10 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
2ef20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a  const void*).);.
2ef30 0a 2f 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 74  ./*.** Specify t
2ef40 68 65 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e  he key for an en
2ef50 63 72 79 70 74 65 64 20 64 61 74 61 62 61 73 65  crypted database
2ef60 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
2ef70 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c  should be.** cal
2ef80 6c 65 64 20 72 69 67 68 74 20 61 66 74 65 72 20  led right after 
2ef90 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a  sqlite3_open()..
2efa0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74  **.** The code t
2efb0 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73  o implement this
2efc0 20 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69   API is not avai
2efd0 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62  lable in the pub
2efe0 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f  lic release.** o
2eff0 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c  f SQLite..*/.SQL
2f000 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2f010 74 65 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74  te3_key(.  sqlit
2f020 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2f030 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2f040 61 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65  abase to be reke
2f050 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  yed */.  const v
2f060 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
2f070 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b  Key     /* The k
2f080 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ey */.);../*.** 
2f090 43 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 6f  Change the key o
2f0a0 6e 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61  n an open databa
2f0b0 73 65 2e 20 20 49 66 20 74 68 65 20 63 75 72 72  se.  If the curr
2f0c0 65 6e 74 20 64 61 74 61 62 61 73 65 20 69 73 20  ent database is 
2f0d0 6e 6f 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64  not.** encrypted
2f0e0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  , this routine w
2f0f0 69 6c 6c 20 65 6e 63 72 79 70 74 20 69 74 2e 20  ill encrypt it. 
2f100 20 49 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e   If pNew==0 or n
2f110 4e 65 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64  New==0, the.** d
2f120 61 74 61 62 61 73 65 20 69 73 20 64 65 63 72 79  atabase is decry
2f130 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  pted..**.** The 
2f140 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  code to implemen
2f150 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f  t this API is no
2f160 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74  t available in t
2f170 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73  he public releas
2f180 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a  e.** of SQLite..
2f190 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2f1a0 74 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28  t sqlite3_rekey(
2f1b0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2f1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1d0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f    /* Database to
2f1e0 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20   be rekeyed */. 
2f1f0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
2f200 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20  y, int nKey     
2f210 2f 2a 20 54 68 65 20 6e 65 77 20 6b 65 79 20 2a  /* The new key *
2f220 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.);../*.** CAPI
2f230 33 52 45 46 3a 20 53 75 73 70 65 6e 64 20 45 78  3REF: Suspend Ex
2f240 65 63 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 68  ecution For A Sh
2f250 6f 72 74 20 54 69 6d 65 20 7b 48 31 30 35 33 30  ort Time {H10530
2f260 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a  } <S40410>.**.**
2f270 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c 65   The sqlite3_sle
2f280 65 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61  ep() function ca
2f290 75 73 65 73 20 74 68 65 20 63 75 72 72 65 6e 74  uses the current
2f2a0 20 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 65   thread to suspe
2f2b0 6e 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20  nd execution.** 
2f2c0 66 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e  for at least a n
2f2d0 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65  umber of millise
2f2e0 63 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 20  conds specified 
2f2f0 69 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 72  in its parameter
2f300 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f  ..**.** If the o
2f310 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
2f320 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74  does not support
2f330 20 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20   sleep requests 
2f340 77 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63  with.** millisec
2f350 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74  ond time resolut
2f360 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69  ion, then the ti
2f370 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64  me will be round
2f380 65 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20  ed up to.** the 
2f390 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20  nearest second. 
2f3a0 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  The number of mi
2f3b0 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c  lliseconds of sl
2f3c0 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20  eep actually.** 
2f3d0 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74  requested from t
2f3e0 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
2f3f0 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  tem is returned.
2f400 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d  .**.** SQLite im
2f410 70 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e  plements this in
2f420 74 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69  terface by calli
2f430 6e 67 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a  ng the xSleep().
2f440 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  ** method of the
2f450 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65   default [sqlite
2f460 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a  3_vfs] object..*
2f470 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2f480 73 3a 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 30  s: [H10533] [H10
2f490 35 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  536].*/.SQLITE_A
2f4a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
2f4b0 6c 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  leep(int);../*.*
2f4c0 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65  * CAPI3REF: Name
2f4d0 20 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48   Of The Folder H
2f4e0 6f 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79  olding Temporary
2f4f0 20 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20   Files {H10310} 
2f500 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49  <S20000>.**.** I
2f510 66 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61  f this global va
2f520 72 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74  riable is made t
2f530 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72  o point to a str
2f540 69 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  ing which is.** 
2f550 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f  the name of a fo
2f560 6c 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72  lder (a.k.a. dir
2f570 65 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c  ectory), then al
2f580 6c 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  l temporary file
2f590 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20  s.** created by 
2f5a0 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70  SQLite will be p
2f5b0 6c 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69  laced in that di
2f5c0 72 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69  rectory.  If thi
2f5d0 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73  s variable.** is
2f5e0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
2f5f0 20 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72   then SQLite per
2f600 66 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66  forms a search f
2f610 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  or an appropriat
2f620 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66  e.** temporary f
2f630 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a  ile directory..*
2f640 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73  *.** It is not s
2f650 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20 6d  afe to read or m
2f660 6f 64 69 66 79 20 74 68 69 73 20 76 61 72 69 61  odify this varia
2f670 62 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e  ble in more than
2f680 20 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 61   one.** thread a
2f690 74 20 61 20 74 69 6d 65 2e 20 20 49 74 20 69 73  t a time.  It is
2f6a0 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61   not safe to rea
2f6b0 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73  d or modify this
2f6c0 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20   variable.** if 
2f6d0 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  a [database conn
2f6e0 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67  ection] is being
2f6f0 20 75 73 65 64 20 61 74 20 74 68 65 20 73 61 6d   used at the sam
2f700 65 20 74 69 6d 65 20 69 6e 20 61 20 73 65 70 61  e time in a sepa
2f710 72 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a  rate.** thread..
2f720 2a 2a 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65  ** It is intende
2f730 64 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69  d that this vari
2f740 61 62 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 65  able be set once
2f750 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 70  .** as part of p
2f760 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a  rocess initializ
2f770 61 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65  ation and before
2f780 20 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65   any SQLite inte
2f790 72 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65  rface.** routine
2f7a0 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c  s have been call
2f7b0 65 64 20 61 6e 64 20 74 68 61 74 20 74 68 69 73  ed and that this
2f7c0 20 76 61 72 69 61 62 6c 65 20 72 65 6d 61 69 6e   variable remain
2f7d0 20 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68   unchanged.** th
2f7e0 65 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  ereafter..**.** 
2f7f0 54 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f  The [temp_store_
2f800 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61  directory pragma
2f810 5d 20 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 69  ] may modify thi
2f820 73 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 63  s variable and c
2f830 61 75 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f  ause.** it to po
2f840 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  int to memory ob
2f850 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
2f860 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46  ite3_malloc].  F
2f870 75 72 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74  urthermore,.** t
2f880 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64  he [temp_store_d
2f890 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d  irectory pragma]
2f8a0 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20   always assumes 
2f8b0 74 68 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0a  that any string.
2f8c0 2a 2a 20 74 68 61 74 20 74 68 69 73 20 76 61 72  ** that this var
2f8d0 69 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20  iable points to 
2f8e0 69 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72  is held in memor
2f8f0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
2f900 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  .** [sqlite3_mal
2f910 6c 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61  loc] and the pra
2f920 67 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20  gma may attempt 
2f930 74 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d  to free that mem
2f940 6f 72 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71  ory.** using [sq
2f950 6c 69 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20  lite3_free]..** 
2f960 48 65 6e 63 65 2c 20 69 66 20 74 68 69 73 20 76  Hence, if this v
2f970 61 72 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 66  ariable is modif
2f980 69 65 64 20 64 69 72 65 63 74 6c 79 2c 20 65 69  ied directly, ei
2f990 74 68 65 72 20 69 74 20 73 68 6f 75 6c 64 20 62  ther it should b
2f9a0 65 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f  e.** made NULL o
2f9b0 72 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20  r made to point 
2f9c0 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
2f9d0 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
2f9e0 5f 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65  _malloc].** or e
2f9f0 6c 73 65 20 74 68 65 20 75 73 65 20 6f 66 20 74  lse the use of t
2fa00 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64  he [temp_store_d
2fa10 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d  irectory pragma]
2fa20 20 73 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 64   should be avoid
2fa30 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
2fa40 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
2fa50 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a  temp_directory;.
2fa60 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2fa70 20 54 65 73 74 20 46 6f 72 20 41 75 74 6f 2d 43   Test For Auto-C
2fa80 6f 6d 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 39  ommit Mode {H129
2fa90 33 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20  30} <S60200>.** 
2faa0 4b 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63  KEYWORDS: {autoc
2fab0 6f 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a  ommit mode}.**.*
2fac0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65  * The sqlite3_ge
2fad0 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69  t_autocommit() i
2fae0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
2faf0 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20   non-zero or.** 
2fb00 7a 65 72 6f 20 69 66 20 74 68 65 20 67 69 76 65  zero if the give
2fb10 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  n database conne
2fb20 63 74 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e  ction is or is n
2fb30 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74  ot in autocommit
2fb40 20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63   mode,.** respec
2fb50 74 69 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d  tively.  Autocom
2fb60 6d 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62  mit mode is on b
2fb70 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75  y default..** Au
2fb80 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73  tocommit mode is
2fb90 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b   disabled by a [
2fba0 42 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74  BEGIN] statement
2fbb0 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20  ..** Autocommit 
2fbc0 6d 6f 64 65 20 69 73 20 72 65 2d 65 6e 61 62 6c  mode is re-enabl
2fbd0 65 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d  ed by a [COMMIT]
2fbe0 20 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a   or [ROLLBACK]..
2fbf0 2a 2a 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 6e  **.** If certain
2fc00 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73   kinds of errors
2fc10 20 6f 63 63 75 72 20 6f 6e 20 61 20 73 74 61 74   occur on a stat
2fc20 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20 6d  ement within a m
2fc30 75 6c 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a  ulti-statement.*
2fc40 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 65  * transaction (e
2fc50 72 72 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 20  rrors including 
2fc60 5b 53 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b  [SQLITE_FULL], [
2fc70 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a  SQLITE_IOERR],.*
2fc80 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d  * [SQLITE_NOMEM]
2fc90 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c  , [SQLITE_BUSY],
2fca0 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54   and [SQLITE_INT
2fcb0 45 52 52 55 50 54 5d 29 20 74 68 65 6e 20 74 68  ERRUPT]) then th
2fcc0 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  e.** transaction
2fcd0 20 6d 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 64   might be rolled
2fce0 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61   back automatica
2fcf0 6c 6c 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77  lly.  The only w
2fd00 61 79 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75  ay to.** find ou
2fd10 74 20 77 68 65 74 68 65 72 20 53 51 4c 69 74 65  t whether SQLite
2fd20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72   automatically r
2fd30 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 74  olled back the t
2fd40 72 61 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 72  ransaction after
2fd50 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20  .** an error is 
2fd60 74 6f 20 75 73 65 20 74 68 69 73 20 66 75 6e 63  to use this func
2fd70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  tion..**.** If a
2fd80 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 63 68  nother thread ch
2fd90 61 6e 67 65 73 20 74 68 65 20 61 75 74 6f 63 6f  anges the autoco
2fda0 6d 6d 69 74 20 73 74 61 74 75 73 20 6f 66 20 74  mmit status of t
2fdb0 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  he database.** c
2fdc0 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20  onnection while 
2fdd0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2fde0 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68  running, then th
2fdf0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
2fe00 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  * is undefined..
2fe10 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
2fe20 74 73 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 31  ts: [H12931] [H1
2fe30 32 39 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b  2932] [H12933] [
2fe40 48 31 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H12934].*/.SQLIT
2fe50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2fe60 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
2fe70 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
2fe80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e  ** CAPI3REF: Fin
2fe90 64 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48  d The Database H
2fea0 61 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61  andle Of A Prepa
2feb0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48  red Statement {H
2fec0 31 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a  13120} <S60600>.
2fed0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2fee0 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65  3_db_handle inte
2fef0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68  rface returns th
2ff00 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
2ff10 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a  ection] handle.*
2ff20 2a 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72  * to which a [pr
2ff30 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2ff40 5d 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20  ] belongs.  The 
2ff50 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
2ff60 74 69 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65  tion].** returne
2ff70 64 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f  d by sqlite3_db_
2ff80 68 61 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61  handle is the sa
2ff90 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
2ffa0 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 61  nection] that wa
2ffb0 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
2ffc0 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b  ment.** to the [
2ffd0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
2ffe0 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69  v2()] call (or i
2fff0 74 73 20 76 61 72 69 61 6e 74 73 29 20 74 68 61  ts variants) tha
30000 74 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a  t was used to.**
30010 20 63 72 65 61 74 65 20 74 68 65 20 73 74 61 74   create the stat
30020 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72  ement in the fir
30030 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  st place..**.** 
30040 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48  Requirements: [H
30050 31 33 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13123].*/.SQLITE
30060 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71  _API sqlite3 *sq
30070 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28  lite3_db_handle(
30080 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
30090 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
300a0 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70   Find the next p
300b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
300c0 74 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36  t {H13140} <S606
300d0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  00>.**.** This i
300e0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
300f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
30100 65 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64  e next [prepared
30110 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65   statement] afte
30120 72 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63  r.** pStmt assoc
30130 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b  iated with the [
30140 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
30150 69 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53  ion] pDb.  If pS
30160 74 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74  tmt is NULL.** t
30170 68 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61  hen this interfa
30180 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
30190 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73  nter to the firs
301a0 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  t prepared state
301b0 6d 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74  ment.** associat
301c0 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ed with the data
301d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
301e0 70 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70  pDb.  If no prep
301f0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
30200 2a 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20  * satisfies the 
30210 63 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68  conditions of th
30220 69 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72  is routine, it r
30230 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  eturns NULL..**.
30240 2a 2a 20 54 68 65 20 5b 64 61 74 61 62 61 73 65  ** The [database
30250 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69   connection] poi
30260 6e 74 65 72 20 44 20 69 6e 20 61 20 63 61 6c 6c  nter D in a call
30270 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
30280 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20  next_stmt(D,S)] 
30290 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 6e  must refer to an
302a0 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a 2a   open database.*
302b0 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64  * connection and
302c0 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 6d   in particular m
302d0 75 73 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c  ust not be a NUL
302e0 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
302f0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b   Requirements: [
30300 48 31 33 31 34 33 5d 20 5b 48 31 33 31 34 36 5d  H13143] [H13146]
30310 20 5b 48 31 33 31 34 39 5d 20 5b 48 31 33 31 35   [H13149] [H1315
30320 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  2].*/.SQLITE_API
30330 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
30340 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
30350 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73  (sqlite3 *pDb, s
30360 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
30370 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  mt);../*.** CAPI
30380 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64  3REF: Commit And
30390 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69   Rollback Notifi
303a0 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73  cation Callbacks
303b0 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 34 30   {H12950} <S6040
303c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
303d0 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
303e0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67  () interface reg
303f0 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63  isters a callbac
30400 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f  k.** function to
30410 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   be invoked when
30420 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69  ever a transacti
30430 6f 6e 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20  on is [COMMIT | 
30440 63 6f 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41  committed]..** A
30450 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20  ny callback set 
30460 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61  by a previous ca
30470 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ll to sqlite3_co
30480 6d 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66  mmit_hook().** f
30490 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  or the same data
304a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
304b0 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a  is overridden..*
304c0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f  * The sqlite3_ro
304d0 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e  llback_hook() in
304e0 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72  terface register
304f0 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20  s a callback.** 
30500 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69  function to be i
30510 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20  nvoked whenever 
30520 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
30530 20 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c   [ROLLBACK | rol
30540 6c 65 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e  led back]..** An
30550 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62  y callback set b
30560 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c  y a previous cal
30570 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d  l to sqlite3_com
30580 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f  mit_hook().** fo
30590 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  r the same datab
305a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
305b0 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a  s overridden..**
305c0 20 54 68 65 20 70 41 72 67 20 61 72 67 75 6d 65   The pArg argume
305d0 6e 74 20 69 73 20 70 61 73 73 65 64 20 74 68 72  nt is passed thr
305e0 6f 75 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c  ough to the call
305f0 62 61 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20  back..** If the 
30600 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f  callback on a co
30610 6d 6d 69 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69  mmit hook functi
30620 6f 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  on returns non-z
30630 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ero,.** then the
30640 20 63 6f 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65   commit is conve
30650 72 74 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c  rted into a roll
30660 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  back..**.** If a
30670 6e 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20  nother function 
30680 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72  was previously r
30690 65 67 69 73 74 65 72 65 64 2c 20 69 74 73 0a 2a  egistered, its.*
306a0 2a 20 70 41 72 67 20 76 61 6c 75 65 20 69 73 20  * pArg value is 
306b0 72 65 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72  returned.  Other
306c0 77 69 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74  wise NULL is ret
306d0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
306e0 20 63 61 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d   callback implem
306f0 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f  entation must no
30700 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68  t do anything th
30710 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a  at will modify.*
30720 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  * the database c
30730 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69  onnection that i
30740 6e 76 6f 6b 65 64 20 74 68 65 20 63 61 6c 6c 62  nvoked the callb
30750 61 63 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e  ack.  Any action
30760 73 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74  s.** to modify t
30770 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
30780 65 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64  ection must be d
30790 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66  eferred until af
307a0 74 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c  ter the.** compl
307b0 65 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71  etion of the [sq
307c0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61  lite3_step()] ca
307d0 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65  ll that triggere
307e0 64 20 74 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20  d the commit.** 
307f0 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b  or rollback hook
30800 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c   in the first pl
30810 61 63 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ace..** Note tha
30820 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  t [sqlite3_prepa
30830 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71  re_v2()] and [sq
30840 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f  lite3_step()] bo
30850 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a  th modify their.
30860 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
30870 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20  ections for the 
30880 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69  meaning of "modi
30890 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 61  fy" in this para
308a0 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67  graph..**.** Reg
308b0 69 73 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20  istering a NULL 
308c0 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65  function disable
308d0 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a  s the callback..
308e0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  **.** When the c
308f0 6f 6d 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62  ommit hook callb
30900 61 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 74 75  ack routine retu
30910 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43  rns zero, the [C
30920 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74  OMMIT].** operat
30930 69 6f 6e 20 69 73 20 61 6c 6c 6f 77 65 64 20 74  ion is allowed t
30940 6f 20 63 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61  o continue norma
30950 6c 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6f 6d  lly.  If the com
30960 6d 69 74 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75  mit hook.** retu
30970 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
30980 65 6e 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20  en the [COMMIT] 
30990 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  is converted int
309a0 6f 20 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a  o a [ROLLBACK]..
309b0 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ** The rollback 
309c0 68 6f 6f 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  hook is invoked 
309d0 6f 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68  on a rollback th
309e0 61 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20  at results from 
309f0 61 20 63 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b  a commit.** hook
30a00 20 72 65 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a   returning non-z
30a10 65 72 6f 2c 20 6a 75 73 74 20 61 73 20 69 74 20  ero, just as it 
30a20 77 6f 75 6c 64 20 62 65 20 77 69 74 68 20 61 6e  would be with an
30a30 79 20 6f 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b  y other rollback
30a40 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ..**.** For the 
30a50 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73  purposes of this
30a60 20 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63 74   API, a transact
30a70 69 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20 68  ion is said to h
30a80 61 76 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c  ave been.** roll
30a90 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 78  ed back if an ex
30aa0 70 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b  plicit "ROLLBACK
30ab0 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65  " statement is e
30ac0 78 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61  xecuted, or.** a
30ad0 6e 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74  n error or const
30ae0 72 61 69 6e 74 20 63 61 75 73 65 73 20 61 6e 20  raint causes an 
30af0 69 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63  implicit rollbac
30b00 6b 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54  k to occur..** T
30b10 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c  he rollback call
30b20 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f  back is not invo
30b30 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 63  ked if a transac
30b40 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d  tion is.** autom
30b50 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20  atically rolled 
30b60 62 61 63 6b 20 62 65 63 61 75 73 65 20 74 68 65  back because the
30b70 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
30b80 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a  tion is closed..
30b90 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ** The rollback 
30ba0 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20  callback is not 
30bb0 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61  invoked if a tra
30bc0 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72  nsaction is.** r
30bd0 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75  olled back becau
30be0 73 65 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c  se a commit call
30bf0 62 61 63 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f  back returned no
30c00 6e 2d 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f  n-zero..** <todo
30c10 3e 20 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 20  > Check on this 
30c20 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65  </todo>.**.** Se
30c30 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69  e also the [sqli
30c40 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
30c50 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  )] interface..**
30c60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
30c70 3a 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48  :.** [H12951] [H
30c80 31 32 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20  12952] [H12953] 
30c90 5b 48 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35  [H12954] [H12955
30ca0 5d 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48  ].** [H12961] [H
30cb0 31 32 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20  12962] [H12963] 
30cc0 5b 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49  [H12964].*/.SQLI
30cd0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
30ce0 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
30cf0 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a  (sqlite3*, int(*
30d00 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29  )(void*), void*)
30d10 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
30d20 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  d *sqlite3_rollb
30d30 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33  ack_hook(sqlite3
30d40 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20  *, void(*)(void 
30d50 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a  *), void*);../*.
30d60 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74  ** CAPI3REF: Dat
30d70 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63  a Change Notific
30d80 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20  ation Callbacks 
30d90 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30  {H12970} <S60400
30da0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
30db0 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
30dc0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69  ) interface regi
30dd0 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b  sters a callback
30de0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74   function.** wit
30df0 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  h the [database 
30e00 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e  connection] iden
30e10 74 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69  tified by the fi
30e20 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  rst argument.** 
30e30 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
30e40 65 6e 65 76 65 72 20 61 20 72 6f 77 20 69 73 20  enever a row is 
30e50 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65  updated, inserte
30e60 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a  d or deleted..**
30e70 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65   Any callback se
30e80 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20  t by a previous 
30e90 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e  call to this fun
30ea0 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65  ction.** for the
30eb0 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63   same database c
30ec0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65  onnection is ove
30ed0 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54  rridden..**.** T
30ee0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
30ef0 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  nt is a pointer 
30f00 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
30f10 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61  to invoke when a
30f20 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74  .** row is updat
30f30 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20  ed, inserted or 
30f40 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20  deleted..** The 
30f50 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
30f60 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  o the callback i
30f70 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
30f80 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a  third argument.*
30f90 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64  * to sqlite3_upd
30fa0 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54  ate_hook()..** T
30fb0 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61  he second callba
30fc0 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  ck argument is o
30fd0 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e  ne of [SQLITE_IN
30fe0 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44  SERT], [SQLITE_D
30ff0 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53  ELETE],.** or [S
31000 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64  QLITE_UPDATE], d
31010 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
31020 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63  operation that c
31030 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61  aused the callba
31040 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f  ck.** to be invo
31050 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72  ked..** The thir
31060 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67  d and fourth arg
31070 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61  uments to the ca
31080 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70  llback contain p
31090 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a  ointers to the.*
310a0 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74  * database and t
310b0 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69  able name contai
310c0 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63 74 65  ning the affecte
310d0 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69  d row..** The fi
310e0 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72  nal callback par
310f0 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72  ameter is the [r
31100 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77  owid] of the row
31110 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65  ..** In the case
31120 20 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74   of an update, t
31130 68 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69  his is the [rowi
31140 64 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64  d] after the upd
31150 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e  ate takes place.
31160 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74  .**.** The updat
31170 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e  e hook is not in
31180 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72  voked when inter
31190 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65  nal system table
311a0 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65  s are.** modifie
311b0 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d  d (i.e. sqlite_m
311c0 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65  aster and sqlite
311d0 5f 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a  _sequence)..**.*
311e0 2a 20 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74  * In the current
311f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c   implementation,
31200 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   the update hook
31210 0a 2a 2a 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b  .** is not invok
31220 65 64 20 77 68 65 6e 20 64 75 70 6c 69 63 61 74  ed when duplicat
31230 69 6f 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c  ion rows are del
31240 65 74 65 64 20 62 65 63 61 75 73 65 20 6f 66 20  eted because of 
31250 61 6e 0a 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49  an.** [ON CONFLI
31260 43 54 20 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54  CT | ON CONFLICT
31270 20 52 45 50 4c 41 43 45 5d 20 63 6c 61 75 73 65   REPLACE] clause
31280 2e 20 20 4e 6f 72 20 69 73 20 74 68 65 20 75 70  .  Nor is the up
31290 64 61 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76  date hook.** inv
312a0 6f 6b 65 64 20 77 68 65 6e 20 72 6f 77 73 20 61  oked when rows a
312b0 72 65 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67  re deleted using
312c0 20 74 68 65 20 5b 74 72 75 6e 63 61 74 65 20 6f   the [truncate o
312d0 70 74 69 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a  ptimization]..**
312e0 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e 73 20   The exceptions 
312f0 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20  defined in this 
31300 70 61 72 61 67 72 61 70 68 20 6d 69 67 68 74 20  paragraph might 
31310 63 68 61 6e 67 65 20 69 6e 20 61 20 66 75 74 75  change in a futu
31320 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66  re.** release of
31330 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54   SQLite..**.** T
31340 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69  he update hook i
31350 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75  mplementation mu
31360 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69  st not do anythi
31370 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64  ng that will mod
31380 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  ify.** the datab
31390 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
313a0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
313b0 75 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e  update hook.  An
313c0 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20  y actions.** to 
313d0 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62  modify the datab
313e0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  ase connection m
313f0 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20  ust be deferred 
31400 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a  until after the.
31410 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66  ** completion of
31420 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
31430 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20  ep()] call that 
31440 74 72 69 67 67 65 72 65 64 20 74 68 65 20 75 70  triggered the up
31450 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f  date hook..** No
31460 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33  te that [sqlite3
31470 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61  _prepare_v2()] a
31480 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  nd [sqlite3_step
31490 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20  ()] both modify 
314a0 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73  their.** databas
314b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f  e connections fo
314c0 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66  r the meaning of
314d0 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69   "modify" in thi
314e0 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a  s paragraph..**.
314f0 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75  ** If another fu
31500 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69  nction was previ
31510 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
31520 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c 75 65  , its pArg value
31530 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
31540 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
31550 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
31560 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65  .** See also the
31570 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74   [sqlite3_commit
31580 5f 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71  _hook()] and [sq
31590 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68  lite3_rollback_h
315a0 6f 6f 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66  ook()].** interf
315b0 61 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  aces..**.** Requ
315c0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
315d0 32 39 37 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b  2971] [H12973] [
315e0 48 31 32 39 37 35 5d 20 5b 48 31 32 39 37 37 5d  H12975] [H12977]
315f0 20 5b 48 31 32 39 37 39 5d 20 5b 48 31 32 39 38   [H12979] [H1298
31600 31 5d 20 5b 48 31 32 39 38 33 5d 20 5b 48 31 32  1] [H12983] [H12
31610 39 38 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  986].*/.SQLITE_A
31620 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
31630 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20  _update_hook(.  
31640 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69  sqlite3*, .  voi
31650 64 28 2a 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20  d(*)(void *,int 
31660 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68  ,char const *,ch
31670 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74  ar const *,sqlit
31680 65 33 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69  e3_int64),.  voi
31690 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  d*.);../*.** CAP
316a0 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72  I3REF: Enable Or
316b0 20 44 69 73 61 62 6c 65 20 53 68 61 72 65 64 20   Disable Shared 
316c0 50 61 67 65 72 20 43 61 63 68 65 20 7b 48 31 30  Pager Cache {H10
316d0 33 33 30 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a  330} <S30900>.**
316e0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72   KEYWORDS: {shar
316f0 65 64 20 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20  ed cache}.**.** 
31700 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61  This routine ena
31710 62 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73  bles or disables
31720 20 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20   the sharing of 
31730 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63  the database cac
31740 68 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61  he.** and schema
31750 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
31760 20 62 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61   between [databa
31770 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20  se connection | 
31780 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20  connections].** 
31790 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  to the same data
317a0 62 61 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73  base. Sharing is
317b0 20 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20   enabled if the 
317c0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65  argument is true
317d0 0a 2a 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64  .** and disabled
317e0 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   if the argument
317f0 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a   is false..**.**
31800 20 43 61 63 68 65 20 73 68 61 72 69 6e 67 20 69   Cache sharing i
31810 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69  s enabled and di
31820 73 61 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e  sabled for an en
31830 74 69 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a  tire process..**
31840 20 54 68 69 73 20 69 73 20 61 20 63 68 61 6e 67   This is a chang
31850 65 20 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76  e as of SQLite v
31860 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e  ersion 3.5.0. In
31870 20 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20   prior versions 
31880 6f 66 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68  of SQLite,.** sh
31890 61 72 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65  aring was enable
318a0 64 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f  d or disabled fo
318b0 72 20 65 61 63 68 20 74 68 72 65 61 64 20 73 65  r each thread se
318c0 70 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20  parately..**.** 
318d0 54 68 65 20 63 61 63 68 65 20 73 68 61 72 69 6e  The cache sharin
318e0 67 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68  g mode set by th
318f0 69 73 20 69 6e 74 65 72 66 61 63 65 20 65 66 66  is interface eff
31900 65 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75  ects all subsequ
31910 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20  ent.** calls to 
31920 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
31930 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  , [sqlite3_open_
31940 76 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69  v2()], and [sqli
31950 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a  te3_open16()]..*
31960 2a 20 45 78 69 73 74 69 6e 67 20 64 61 74 61 62  * Existing datab
31970 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
31980 63 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65  continue use the
31990 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a   sharing mode.**
319a0 20 74 68 61 74 20 77 61 73 20 69 6e 20 65 66 66   that was in eff
319b0 65 63 74 20 61 74 20 74 68 65 20 74 69 6d 65 20  ect at the time 
319c0 74 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64  they were opened
319d0 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20  ..**.** Virtual 
319e0 74 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65  tables cannot be
319f0 20 75 73 65 64 20 77 69 74 68 20 61 20 73 68 61   used with a sha
31a00 72 65 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e  red cache.  When
31a10 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65   shared.** cache
31a20 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65   is enabled, the
31a30 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
31a40 5f 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75  _module()] API u
31a50 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a  sed to register.
31a60 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
31a70 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65  s will always re
31a80 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  turn an error..*
31a90 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
31aa0 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  e returns [SQLIT
31ab0 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20  E_OK] if shared 
31ac0 63 61 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65  cache was enable
31ad0 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a  d or disabled.**
31ae0 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20   successfully.  
31af0 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  An [error code] 
31b00 69 73 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65  is returned othe
31b10 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61  rwise..**.** Sha
31b20 72 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73  red cache is dis
31b30 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
31b40 2e 20 42 75 74 20 74 68 69 73 20 6d 69 67 68 74  . But this might
31b50 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75   change in.** fu
31b60 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66  ture releases of
31b70 20 53 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63   SQLite.  Applic
31b80 61 74 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65  ations that care
31b90 20 61 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a   about shared.**
31ba0 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 20 73   cache setting s
31bb0 68 6f 75 6c 64 20 73 65 74 20 69 74 20 65 78 70  hould set it exp
31bc0 6c 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53  licitly..**.** S
31bd0 65 65 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74  ee Also:  [SQLit
31be0 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d  e Shared-Cache M
31bf0 6f 64 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ode].**.** Requi
31c00 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31  rements: [H10331
31c10 5d 20 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33  ] [H10336] [H103
31c20 33 37 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a  37] [H10339].*/.
31c30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
31c40 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68  qlite3_enable_sh
31c50 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b  ared_cache(int);
31c60 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
31c70 3a 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65  : Attempt To Fre
31c80 65 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48  e Heap Memory {H
31c90 31 37 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a  17340} <S30220>.
31ca0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
31cb0 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
31cc0 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74  () interface att
31cd0 65 6d 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20  empts to free N 
31ce0 62 79 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70  bytes.** of heap
31cf0 20 6d 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c   memory by deall
31d00 6f 63 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65  ocating non-esse
31d10 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  ntial memory all
31d20 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64  ocations.** held
31d30 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65   by the database
31d40 20 6c 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20   library. {END} 
31d50 20 4d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20   Memory used to 
31d60 63 61 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a  cache database.*
31d70 2a 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f  * pages to impro
31d80 76 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69  ve performance i
31d90 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20  s an example of 
31da0 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65  non-essential me
31db0 6d 6f 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33  mory..** sqlite3
31dc0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
31dd0 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  ) returns the nu
31de0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63  mber of bytes ac
31df0 74 75 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a  tually freed,.**
31e00 20 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 20   which might be 
31e10 6d 6f 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61  more or less tha
31e20 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71  n the amount req
31e30 75 65 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  uested..**.** Re
31e40 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37  quirements: [H17
31e50 33 34 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f  341] [H17342].*/
31e60 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
31e70 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
31e80 6d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a  memory(int);../*
31e90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d  .** CAPI3REF: Im
31ea0 70 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20  pose A Limit On 
31eb0 48 65 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35  Heap Size {H1735
31ec0 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a  0} <S30220>.**.*
31ed0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f  * The sqlite3_so
31ee0 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20  ft_heap_limit() 
31ef0 69 6e 74 65 72 66 61 63 65 20 70 6c 61 63 65 73  interface places
31f00 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a   a "soft" limit.
31f10 2a 2a 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74  ** on the amount
31f20 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20   of heap memory 
31f30 74 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f  that may be allo
31f40 63 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e  cated by SQLite.
31f50 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e  .** If an intern
31f60 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  al allocation is
31f70 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74 20   requested that 
31f80 77 6f 75 6c 64 20 65 78 63 65 65 64 20 74 68 65  would exceed the
31f90 0a 2a 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69  .** soft heap li
31fa0 6d 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65  mit, [sqlite3_re
31fb0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20  lease_memory()] 
31fc0 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f  is invoked one o
31fd0 72 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20  r.** more times 
31fe0 74 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20  to free up some 
31ff0 73 70 61 63 65 20 62 65 66 6f 72 65 20 74 68 65  space before the
32000 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70   allocation is p
32010 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20  erformed..**.** 
32020 54 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c  The limit is cal
32030 6c 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61  led "soft", beca
32040 75 73 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f  use if [sqlite3_
32050 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
32060 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65  ].** cannot free
32070 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f   sufficient memo
32080 72 79 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68  ry to prevent th
32090 65 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69  e limit from bei
320a0 6e 67 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20  ng exceeded,.** 
320b0 74 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c  the memory is al
320c0 6c 6f 63 61 74 65 64 20 61 6e 79 77 61 79 20 61  located anyway a
320d0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f  nd the current o
320e0 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64  peration proceed
320f0 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74  s..**.** A negat
32100 69 76 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75  ive or zero valu
32110 65 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68  e for N means th
32120 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73  at there is no s
32130 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61  oft heap limit a
32140 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72  nd.** [sqlite3_r
32150 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d  elease_memory()]
32160 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61   will only be ca
32170 6c 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79  lled when memory
32180 20 69 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a   is exhausted..*
32190 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61  * The default va
321a0 6c 75 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74  lue for the soft
321b0 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a   heap limit is z
321c0 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ero..**.** SQLit
321d0 65 20 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65  e makes a best e
321e0 66 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74  ffort to honor t
321f0 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
32200 69 74 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68  it..** But if th
32210 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  e soft heap limi
32220 74 20 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f  t cannot be hono
32230 72 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77  red, execution w
32240 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20  ill.** continue 
32250 77 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72  without error or
32260 20 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20   notification.  
32270 54 68 69 73 20 69 73 20 77 68 79 20 74 68 65 20  This is why the 
32280 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c  limit is.** call
32290 65 64 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69  ed a "soft" limi
322a0 74 2e 20 20 49 74 20 69 73 20 61 64 76 69 73 6f  t.  It is adviso
322b0 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50  ry only..**.** P
322c0 72 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76  rior to SQLite v
322d0 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68  ersion 3.5.0, th
322e0 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
322f0 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20  constrained the 
32300 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61  memory.** alloca
32310 74 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20  ted by a single 
32320 74 68 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d  thread - the sam
32330 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63  e thread in whic
32340 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  h this routine.*
32350 2a 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69  * runs.  Beginni
32360 6e 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 76  ng with SQLite v
32370 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68  ersion 3.5.0, th
32380 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  e soft heap limi
32390 74 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20  t is.** applied 
323a0 74 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20  to all threads. 
323b0 54 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66  The value specif
323c0 69 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74  ied for the soft
323d0 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69   heap limit.** i
323e0 73 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64  s an upper bound
323f0 20 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65   on the total me
32400 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
32410 66 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e  for all threads.
32420 20 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33   In.** version 3
32430 2e 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f  .5.0 there is no
32440 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c   mechanism for l
32450 69 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61 70  imiting the heap
32460 20 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e   usage for.** in
32470 64 69 76 69 64 75 61 6c 20 74 68 72 65 61 64 73  dividual threads
32480 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
32490 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31  ents:.** [H16351
324a0 5d 20 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33  ] [H16352] [H163
324b0 35 33 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31  53] [H16354] [H1
324c0 36 33 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a  6355] [H16358].*
324d0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
324e0 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68  d sqlite3_soft_h
324f0 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a  eap_limit(int);.
32500 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
32510 20 45 78 74 72 61 63 74 20 4d 65 74 61 64 61 74   Extract Metadat
32520 61 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e  a About A Column
32530 20 4f 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32   Of A Table {H12
32540 38 35 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a  850} <S60300>.**
32550 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
32560 20 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74   returns metadat
32570 61 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66  a about a specif
32580 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73  ic column of a s
32590 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62  pecific.** datab
325a0 61 73 65 20 74 61 62 6c 65 20 61 63 63 65 73 73  ase table access
325b0 69 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b  ible using the [
325c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
325d0 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70  ion] handle.** p
325e0 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
325f0 73 74 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  st function argu
32600 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
32610 63 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69  column is identi
32620 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f  fied by the seco
32630 6e 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f  nd, third and fo
32640 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20  urth parameters 
32650 74 6f 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  to.** this funct
32660 69 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  ion. The second 
32670 70 61 72 61 6d 65 74 65 72 20 69 73 20 65 69 74  parameter is eit
32680 68 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  her the name of 
32690 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
326a0 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74  (i.e. "main", "t
326b0 65 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63  emp" or an attac
326c0 68 65 64 20 64 61 74 61 62 61 73 65 29 20 63 6f  hed database) co
326d0 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65  ntaining the spe
326e0 63 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20  cified.** table 
326f0 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69  or NULL. If it i
32700 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c  s NULL, then all
32710 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
32720 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64  ses are searched
32730 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c  .** for the tabl
32740 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65  e using the same
32750 20 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20   algorithm used 
32760 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  by the database 
32770 65 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73  engine to.** res
32780 6f 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64  olve unqualified
32790 20 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65   table reference
327a0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  s..**.** The thi
327b0 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61  rd and fourth pa
327c0 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73  rameters to this
327d0 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68   function are th
327e0 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  e table and colu
327f0 6d 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68  mn.** name of th
32800 65 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e  e desired column
32810 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  , respectively. 
32820 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65  Neither of these
32830 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d   parameters.** m
32840 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  ay be NULL..**.*
32850 2a 20 4d 65 74 61 64 61 74 61 20 69 73 20 72 65  * Metadata is re
32860 74 75 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e  turned by writin
32870 67 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  g to the memory 
32880 6c 6f 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64  locations passed
32890 20 61 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61   as the 5th.** a
328a0 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 70 61  nd subsequent pa
328b0 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73  rameters to this
328c0 20 66 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f   function. Any o
328d0 66 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74  f these argument
328e0 73 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c  s may be.** NULL
328f0 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  , in which case 
32900 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
32910 67 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74  g element of met
32920 61 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64  adata is omitted
32930 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  ..**.** <blockqu
32940 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62  ote>.** <table b
32950 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74  order="1">.** <t
32960 72 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72  r><th> Parameter
32970 20 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e   <th> Output<br>
32980 54 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72  Type <th>  Descr
32990 69 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72  iption.**.** <tr
329a0 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63  ><td> 5th <td> c
329b0 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20  onst char* <td> 
329c0 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72  Data type.** <tr
329d0 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63  ><td> 6th <td> c
329e0 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20  onst char* <td> 
329f0 4e 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20  Name of default 
32a00 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32a10 63 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37  ce.** <tr><td> 7
32a20 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20  th <td> int     
32a30 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66      <td> True if
32a40 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f   column has a NO
32a50 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
32a60 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74  t.** <tr><td> 8t
32a70 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20  h <td> int      
32a80 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20     <td> True if 
32a90 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f  column is part o
32aa0 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  f the PRIMARY KE
32ab0 59 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74  Y.** <tr><td> 9t
32ac0 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20  h <td> int      
32ad0 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20     <td> True if 
32ae0 63 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49  column is [AUTOI
32af0 4e 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74  NCREMENT].** </t
32b00 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b  able>.** </block
32b10 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  quote>.**.** The
32b20 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20   memory pointed 
32b30 74 6f 20 62 79 20 74 68 65 20 63 68 61 72 61 63  to by the charac
32b40 74 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74  ter pointers ret
32b50 75 72 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a  urned for the.**
32b60 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
32b70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  e and collation 
32b80 73 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69  sequence is vali
32b90 64 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65  d only until the
32ba0 20 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f   next.** call to
32bb0 20 61 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20   any SQLite API 
32bc0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
32bd0 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64  If the specified
32be0 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c   table is actual
32bf0 6c 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65  ly a view, an [e
32c00 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
32c10 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
32c20 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63   the specified c
32c30 6f 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22  olumn is "rowid"
32c40 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77  , "oid" or "_row
32c50 69 64 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b  id_" and an.** [
32c60 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
32c70 4b 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20  KEY] column has 
32c80 62 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20  been explicitly 
32c90 64 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74  declared, then t
32ca0 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72  he output.** par
32cb0 61 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20  ameters are set 
32cc0 66 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69 74  for the explicit
32cd0 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75  ly declared colu
32ce0 6d 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  mn. If there is 
32cf0 6e 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79  no.** explicitly
32d00 20 64 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47   declared [INTEG
32d10 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20  ER PRIMARY KEY] 
32d20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65  column, then the
32d30 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d   output.** param
32d40 65 74 65 72 73 20 61 72 65 20 73 65 74 20 61 73  eters are set as
32d50 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
32d60 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74  <pre>.**     dat
32d70 61 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52  a type: "INTEGER
32d80 22 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69  ".**     collati
32d90 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49  on sequence: "BI
32da0 4e 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74  NARY".**     not
32db0 20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20   null: 0.**     
32dc0 70 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a  primary key: 1.*
32dd0 2a 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65  *     auto incre
32de0 6d 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65  ment: 0.** </pre
32df0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  >.**.** This fun
32e00 63 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f  ction may load o
32e10 6e 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d  ne or more schem
32e20 61 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65  as from database
32e30 20 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a   files. If an.**
32e40 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
32e50 72 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73  ring this proces
32e60 73 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65 71  s, or if the req
32e70 75 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20  uested table or 
32e80 63 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74  column.** cannot
32e90 20 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65   be found, an [e
32ea0 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
32eb0 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72  turned and an er
32ec0 72 6f 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74  ror message left
32ed0 0a 2a 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61  .** in the [data
32ee0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
32ef0 20 28 74 6f 20 62 65 20 72 65 74 72 69 65 76 65   (to be retrieve
32f00 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  d using sqlite3_
32f10 65 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a  errmsg())..**.**
32f20 20 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c   This API is onl
32f30 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74  y available if t
32f40 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63  he library was c
32f50 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
32f60 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42  .** [SQLITE_ENAB
32f70 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
32f80 54 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73  TA] C-preprocess
32f90 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65  or symbol define
32fa0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
32fb0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62   int sqlite3_tab
32fc0 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
32fd0 74 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ta(.  sqlite3 *d
32fe0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
32ff0 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20    /* Connection 
33000 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
33010 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c  t char *zDbName,
33020 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
33030 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c  ase name or NULL
33040 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
33050 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20   *zTableName,   
33060 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20    /* Table name 
33070 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
33080 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20  *zColumnName,   
33090 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20   /* Column name 
330a0 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  */.  char const 
330b0 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20  **pzDataType,   
330c0 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c   /* OUTPUT: Decl
330d0 61 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a  ared data type *
330e0 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
330f0 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20  *pzCollSeq,     
33100 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61  /* OUTPUT: Colla
33110 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
33120 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f  me */.  int *pNo
33130 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20  tNull,          
33140 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54      /* OUTPUT: T
33150 72 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20  rue if NOT NULL 
33160 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74  constraint exist
33170 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69  s */.  int *pPri
33180 6d 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20  maryKey,        
33190 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
331a0 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72  ue if column par
331b0 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74  t of PK */.  int
331c0 20 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20   *pAutoinc      
331d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50           /* OUTP
331e0 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75  UT: True if colu
331f0 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65  mn is auto-incre
33200 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  ment */.);../*.*
33210 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64  * CAPI3REF: Load
33220 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48   An Extension {H
33230 31 32 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a  12600} <S20500>.
33240 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
33250 66 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51  face loads an SQ
33260 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c  Lite extension l
33270 69 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20  ibrary from the 
33280 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  named file..**.*
33290 2a 20 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73  * {H12601} The s
332a0 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65  qlite3_load_exte
332b0 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63  nsion() interfac
332c0 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f  e attempts to lo
332d0 61 64 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ad an.**        
332e0 20 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69    SQLite extensi
332f0 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61  on library conta
33300 69 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65  ined in the file
33310 20 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48   zFile..**.** {H
33320 31 32 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79  12602} The entry
33330 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e   point is zProc.
33340 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20  .**.** {H12603} 
33350 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20  zProc may be 0, 
33360 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
33370 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e  e name of the en
33380 74 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20  try point.**    
33390 20 20 20 20 20 20 64 65 66 61 75 6c 74 73 20 74        defaults t
333a0 6f 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  o "sqlite3_exten
333b0 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a  sion_init"..**.*
333c0 2a 20 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73  * {H12604} The s
333d0 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65  qlite3_load_exte
333e0 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63  nsion() interfac
333f0 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a  e shall return.*
33400 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49  *          [SQLI
33410 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73  TE_OK] on succes
33420 73 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52  s and [SQLITE_ER
33430 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e  ROR] if somethin
33440 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a  g goes wrong..**
33450 0a 2a 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20  .** {H12605} If 
33460 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
33470 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20  and pzErrMsg is 
33480 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a  not 0, then the.
33490 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
334a0 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  ite3_load_extens
334b0 69 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65  ion()] interface
334c0 20 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74   shall attempt t
334d0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69  o.**          fi
334e0 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74  ll *pzErrMsg wit
334f0 68 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  h error message 
33500 74 65 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d  text stored in m
33510 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20  emory.**        
33520 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20    obtained from 
33530 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
33540 29 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63  )]. {END}  The c
33550 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a  alling function.
33560 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 6f 75  **          shou
33570 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d  ld free this mem
33580 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b  ory by calling [
33590 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e  sqlite3_free()].
335a0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20  .**.** {H12606} 
335b0 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  Extension loadin
335c0 67 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65  g must be enable
335d0 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20  d using.**      
335e0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61      [sqlite3_ena
335f0 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ble_load_extensi
33600 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63  on()] prior to c
33610 61 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c  alling this API,
33620 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68  .**          oth
33630 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20  erwise an error 
33640 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64  will be returned
33650 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33660 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64  int sqlite3_load
33670 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71  _extension(.  sq
33680 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
33690 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20      /* Load the 
336a0 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74  extension into t
336b0 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e  his database con
336c0 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e  nection */.  con
336d0 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20  st char *zFile, 
336e0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
336f0 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
33700 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65   containing exte
33710 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  nsion */.  const
33720 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20   char *zProc,   
33730 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e   /* Entry point.
33740 20 20 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a    Derived from z
33750 46 69 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63  File if 0 */.  c
33760 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20  har **pzErrMsg  
33770 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f       /* Put erro
33780 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69  r message here i
33790 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f  f not 0 */.);../
337a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
337b0 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65  nable Or Disable
337c0 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69   Extension Loadi
337d0 6e 67 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30  ng {H12620} <S20
337e0 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73  500>.**.** So as
337f0 20 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63   not to open sec
33800 75 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f  urity holes in o
33810 6c 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e  lder application
33820 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e  s that are.** un
33830 70 72 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c  prepared to deal
33840 20 77 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20   with extension 
33850 6c 6f 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20  loading, and as 
33860 61 20 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62  a means of disab
33870 6c 69 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f  ling.** extensio
33880 6e 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20  n loading while 
33890 65 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d  evaluating user-
338a0 65 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65  entered SQL, the
338b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a   following API.*
338c0 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f  * is provided to
338d0 20 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74   turn the [sqlit
338e0 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  e3_load_extensio
338f0 6e 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f  n()] mechanism o
33900 6e 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a  n and off..**.**
33910 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   Extension loadi
33920 6e 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 66  ng is off by def
33930 61 75 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74  ault. See ticket
33940 20 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48   #1863..**.** {H
33950 31 32 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20  12621} Call the 
33960 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c  sqlite3_enable_l
33970 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20  oad_extension() 
33980 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f  routine with ono
33990 66 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20  ff==1.**        
339a0 20 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73    to turn extens
339b0 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61  ion loading on a
339c0 6e 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20  nd call it with 
339d0 6f 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e  onoff==0 to turn
339e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 20  .**          it 
339f0 62 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a  back off again..
33a00 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45  **.** {H12622} E
33a10 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
33a20 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75   is off by defau
33a30 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  lt..*/.SQLITE_AP
33a40 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e  I int sqlite3_en
33a50 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  able_load_extens
33a60 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ion(sqlite3 *db,
33a70 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a   int onoff);../*
33a80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75  .** CAPI3REF: Au
33a90 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64  tomatically Load
33aa0 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b   An Extensions {
33ab0 48 31 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e  H12640} <S20500>
33ac0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20  .**.** This API 
33ad0 63 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61  can be invoked a
33ae0 74 20 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75  t program startu
33af0 70 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65  p in order to re
33b00 67 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72  gister.** one or
33b10 20 6d 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79   more statically
33b20 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f   linked extensio
33b30 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ns that will be 
33b40 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20  available.** to 
33b50 61 6c 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73  all new [databas
33b60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20  e connections]. 
33b70 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73  {END}.**.** This
33b80 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20   routine stores 
33b90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
33ba0 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e   extension in an
33bb0 20 61 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a   array that is.*
33bc0 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  * obtained from 
33bd0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
33be0 29 5d 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20  )].  If you run 
33bf0 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68  a memory leak ch
33c00 65 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72  ecker.** on your
33c10 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20   program and it 
33c20 72 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62  reports a leak b
33c30 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 20 61  ecause of this a
33c40 72 72 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20  rray, invoke.** 
33c50 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61  [sqlite3_reset_a
33c60 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d  uto_extension()]
33c70 20 70 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f   prior to shutdo
33c80 77 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d  wn to free the m
33c90 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  emory..**.** {H1
33ca0 32 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74  2641} This funct
33cb0 69 6f 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e  ion registers an
33cc0 20 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79   extension entry
33cd0 20 70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a   point that is.*
33ce0 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d  *          autom
33cf0 61 74 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64  atically invoked
33d00 20 77 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20   whenever a new 
33d10 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
33d20 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20  tion].**        
33d30 20 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e    is opened usin
33d40 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  g [sqlite3_open(
33d50 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
33d60 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  n16()],.**      
33d70 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f      or [sqlite3_
33d80 6f 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a  open_v2()]..**.*
33d90 2a 20 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69  * {H12642} Dupli
33da0 63 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20  cate extensions 
33db0 61 72 65 20 64 65 74 65 63 74 65 64 20 73 6f 20  are detected so 
33dc0 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
33dd0 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  tine.**         
33de0 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
33df0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 65 78  with the same ex
33e00 74 65 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c  tension is harml
33e10 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ess..**.** {H126
33e20 34 33 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65  43} This routine
33e30 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65   stores a pointe
33e40 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69  r to the extensi
33e50 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a  on in an array.*
33e60 2a 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20  *          that 
33e70 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
33e80 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
33e90 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ()]..**.** {H126
33ea0 34 34 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78  44} Automatic ex
33eb0 74 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61  tensions apply a
33ec0 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64  cross all thread
33ed0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
33ee0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74   int sqlite3_aut
33ef0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64  o_extension(void
33f00 20 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28   (*xEntryPoint)(
33f10 76 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  void));../*.** C
33f20 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41  API3REF: Reset A
33f30 75 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69  utomatic Extensi
33f40 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36  on Loading {H126
33f50 36 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a  60} <S20500>.**.
33f60 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
33f70 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72   disables all pr
33f80 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65  eviously registe
33f90 72 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a  red automatic.**
33fa0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e   extensions. {EN
33fb0 44 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68  D}  It undoes th
33fc0 65 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20  e effect of all 
33fd0 70 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  prior.** [sqlite
33fe0 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e  3_auto_extension
33ff0 28 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a  ()] calls..**.**
34000 20 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66   {H12661} This f
34010 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73  unction disables
34020 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20   all previously 
34030 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20  registered.**   
34040 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63         automatic
34050 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a   extensions..**.
34060 2a 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73  ** {H12662} This
34070 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c   function disabl
34080 65 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74  es automatic ext
34090 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74  ensions in all t
340a0 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54  hreads..*/.SQLIT
340b0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
340c0 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78  e3_reset_auto_ex
340d0 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a  tension(void);..
340e0 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49  /*.****** EXPERI
340f0 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74  MENTAL - subject
34100 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f   to change witho
34110 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a  ut notice ******
34120 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
34130 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  he interface to 
34140 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  the virtual-tabl
34150 65 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63  e mechanism is c
34160 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65  urrently conside
34170 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70  red.** to be exp
34180 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20  erimental.  The 
34190 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20  interface might 
341a0 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70  change in incomp
341b0 61 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20  atible ways..** 
341c0 49 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f  If this is a pro
341d0 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f  blem for you, do
341e0 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74   not use the int
341f0 65 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74  erface at this t
34200 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ime..**.** When 
34210 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  the virtual-tabl
34220 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62  e mechanism stab
34230 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20  ilizes, we will 
34240 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69  declare the.** i
34250 6e 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20  nterface fixed, 
34260 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66  support it indef
34270 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d  initely, and rem
34280 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74  ove this comment
34290 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75  ..*/../*.** Stru
342a0 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74  ctures used by t
342b0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
342c0 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79   interface.*/.ty
342d0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
342e0 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65  ite3_vtab sqlite
342f0 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20  3_vtab;.typedef 
34300 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
34310 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65  ndex_info sqlite
34320 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79  3_index_info;.ty
34330 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
34340 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
34350 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
34360 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74  rsor;.typedef st
34370 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  ruct sqlite3_mod
34380 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  ule sqlite3_modu
34390 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  le;../*.** CAPI3
343a0 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62  REF: Virtual Tab
343b0 6c 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30  le Object {H1800
343c0 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b  0} <S20400>.** K
343d0 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33  EYWORDS: sqlite3
343e0 5f 6d 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c  _module {virtual
343f0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a   table module}.*
34400 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
34410 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74  *.** This struct
34420 75 72 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63  ure, sometimes c
34430 61 6c 6c 65 64 20 61 20 61 20 22 76 69 72 74 75  alled a a "virtu
34440 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22  al table module"
34450 2c 20 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68  , .** defines th
34460 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
34470 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74   of a [virtual t
34480 61 62 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69  ables].  .** Thi
34490 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73  s structure cons
344a0 69 73 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d  ists mostly of m
344b0 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d  ethods for the m
344c0 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76  odule..**.** A v
344d0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
344e0 75 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 62  ule is created b
344f0 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70  y filling in a p
34500 65 72 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73  ersistent.** ins
34510 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
34520 72 75 63 74 75 72 65 20 61 6e 64 20 70 61 73 73  ructure and pass
34530 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ing a pointer to
34540 20 74 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a   that instance.*
34550 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72  * to [sqlite3_cr
34560 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f  eate_module()] o
34570 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  r [sqlite3_creat
34580 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a  e_module_v2()]..
34590 2a 2a 20 54 68 65 20 72 65 67 69 73 74 72 61 74  ** The registrat
345a0 69 6f 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69  ion remains vali
345b0 64 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65  d until it is re
345c0 70 6c 61 63 65 64 20 62 79 20 61 20 64 69 66 66  placed by a diff
345d0 65 72 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20  erent.** module 
345e0 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61  or until the [da
345f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
34600 6e 5d 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20  n] closes.  The 
34610 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68  content.** of th
34620 69 73 20 73 74 72 75 63 74 75 72 65 20 6d 75 73  is structure mus
34630 74 20 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69  t not change whi
34640 6c 65 20 69 74 20 69 73 20 72 65 67 69 73 74 65  le it is registe
34650 72 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20  red with.** any 
34660 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
34670 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73  ion..*/.struct s
34680 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a  qlite3_module {.
34690 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a    int iVersion;.
346a0 20 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29    int (*xCreate)
346b0 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20  (sqlite3*, void 
346c0 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20  *pAux,.         
346d0 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20        int argc, 
346e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
346f0 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20  t*argv,.        
34700 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
34710 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68  tab **ppVTab, ch
34720 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  ar**);.  int (*x
34730 43 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33  Connect)(sqlite3
34740 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20  *, void *pAux,. 
34750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
34760 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68  t argc, const ch
34770 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a  ar *const*argv,.
34780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
34790 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
347a0 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20  VTab, char**);. 
347b0 20 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65   int (*xBestInde
347c0 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  x)(sqlite3_vtab 
347d0 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f  *pVTab, sqlite3_
347e0 69 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20  index_info*);.  
347f0 69 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63  int (*xDisconnec
34800 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  t)(sqlite3_vtab 
34810 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28  *pVTab);.  int (
34820 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74  *xDestroy)(sqlit
34830 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b  e3_vtab *pVTab);
34840 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28  .  int (*xOpen)(
34850 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
34860 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61  Tab, sqlite3_vta
34870 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72  b_cursor **ppCur
34880 73 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43  sor);.  int (*xC
34890 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74  lose)(sqlite3_vt
348a0 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69  ab_cursor*);.  i
348b0 6e 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71  nt (*xFilter)(sq
348c0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
348d0 72 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20  r*, int idxNum, 
348e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53  const char *idxS
348f0 74 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  tr,.            
34900 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71      int argc, sq
34910 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
34920 67 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65  gv);.  int (*xNe
34930 78 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  xt)(sqlite3_vtab
34940 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74  _cursor*);.  int
34950 20 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33   (*xEof)(sqlite3
34960 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a  _vtab_cursor*);.
34970 20 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29    int (*xColumn)
34980 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
34990 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63  rsor*, sqlite3_c
349a0 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20  ontext*, int);. 
349b0 20 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73   int (*xRowid)(s
349c0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
349d0 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  or*, sqlite3_int
349e0 36 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69  64 *pRowid);.  i
349f0 6e 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 71  nt (*xUpdate)(sq
34a00 6c 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e  lite3_vtab *, in
34a10 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t, sqlite3_value
34a20 20 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74   **, sqlite3_int
34a30 36 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  64 *);.  int (*x
34a40 42 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76  Begin)(sqlite3_v
34a50 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69  tab *pVTab);.  i
34a60 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69  nt (*xSync)(sqli
34a70 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29  te3_vtab *pVTab)
34a80 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69  ;.  int (*xCommi
34a90 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  t)(sqlite3_vtab 
34aa0 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28  *pVTab);.  int (
34ab0 2a 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69  *xRollback)(sqli
34ac0 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29  te3_vtab *pVTab)
34ad0 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46  ;.  int (*xFindF
34ae0 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33  unction)(sqlite3
34af0 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e  _vtab *pVtab, in
34b00 74 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68  t nArg, const ch
34b10 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  ar *zName,.     
34b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b30 20 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63    void (**pxFunc
34b40 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
34b50 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
34b60 61 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20  alue**),.       
34b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b80 76 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20  void **ppArg);. 
34b90 20 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28   int (*xRename)(
34ba0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
34bb0 74 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  tab, const char 
34bc0 2a 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  *zNew);.};../*.*
34bd0 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74  * CAPI3REF: Virt
34be0 75 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69  ual Table Indexi
34bf0 6e 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b  ng Information {
34c00 48 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e  H18100} <S20400>
34c10 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71  .** KEYWORDS: sq
34c20 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
34c30 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
34c40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
34c50 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
34c60 72 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20  ructure and its 
34c70 73 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73  substructures is
34c80 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73   used to.** pass
34c90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74   information int
34ca0 6f 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 68  o and receive th
34cb0 65 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65  e reply from the
34cc0 20 5b 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a   [xBestIndex].**
34cd0 20 6d 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69   method of a [vi
34ce0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
34cf0 6c 65 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73  le].  The fields
34d00 20 75 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a   under **Inputs*
34d10 2a 20 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70  * are the.** inp
34d20 75 74 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65  uts to xBestInde
34d30 78 20 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f  x and are read-o
34d40 6e 6c 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78  nly.  xBestIndex
34d50 20 69 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20   inserts its.** 
34d60 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65  results into the
34d70 20 2a 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65   **Outputs** fie
34d80 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  lds..**.** The a
34d90 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72  Constraint[] arr
34da0 61 79 20 72 65 63 6f 72 64 73 20 57 48 45 52 45  ay records WHERE
34db0 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69   clause constrai
34dc0 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a  nts of the form:
34dd0 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75  .**.** <pre>colu
34de0 6d 6e 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e  mn OP expr</pre>
34df0 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20  .**.** where OP 
34e00 69 73 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b  is =, &lt;, &lt;
34e10 3d 2c 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b  =, &gt;, or &gt;
34e20 3d 2e 20 20 54 68 65 20 70 61 72 74 69 63 75 6c  =.  The particul
34e30 61 72 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a  ar operator is.*
34e40 2a 20 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e  * stored in aCon
34e50 73 74 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54  straint[].op.  T
34e60 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
34e70 63 6f 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64  column is stored
34e80 20 69 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69   in.** aConstrai
34e90 6e 74 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61  nt[].iColumn.  a
34ea0 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61  Constraint[].usa
34eb0 62 6c 65 20 69 73 20 54 52 55 45 20 69 66 20 74  ble is TRUE if t
34ec0 68 65 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68  he.** expr on th
34ed0 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
34ee0 65 20 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74  e can be evaluat
34ef0 65 64 20 28 61 6e 64 20 74 68 75 73 20 74 68 65  ed (and thus the
34f00 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69   constraint.** i
34f10 73 20 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61  s usable) and fa
34f20 6c 73 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74  lse if it cannot
34f30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69  ..**.** The opti
34f40 6d 69 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61  mizer automatica
34f50 6c 6c 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d  lly inverts term
34f60 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65  s of the form "e
34f70 78 70 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a  xpr OP column".*
34f80 2a 20 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65  * and makes othe
34f90 72 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e  r simplification
34fa0 73 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63  s to the WHERE c
34fb0 6c 61 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65  lause in an atte
34fc0 6d 70 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73  mpt to.** get as
34fd0 20 6d 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75   many WHERE clau
34fe0 73 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68  se terms into th
34ff0 65 20 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f  e form shown abo
35000 76 65 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a  ve as possible..
35010 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69  ** The aConstrai
35020 6e 74 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20  nt[] array only 
35030 72 65 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c  reports WHERE cl
35040 61 75 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68  ause terms in th
35050 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72  e correct.** for
35060 6d 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20  m that refer to 
35070 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76  the particular v
35080 69 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69  irtual table bei
35090 6e 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a  ng queried..**.*
350a0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * Information ab
350b0 6f 75 74 20 74 68 65 20 4f 52 44 45 52 20 42 59  out the ORDER BY
350c0 20 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65   clause is store
350d0 64 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e  d in aOrderBy[].
350e0 0a 2a 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66  .** Each term of
350f0 20 61 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64   aOrderBy record
35100 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  s a column of th
35110 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
35120 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42  e..**.** The [xB
35130 65 73 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64  estIndex] method
35140 20 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73   must fill aCons
35150 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69  traintUsage[] wi
35160 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  th information.*
35170 2a 20 61 62 6f 75 74 20 77 68 61 74 20 70 61 72  * about what par
35180 61 6d 65 74 65 72 73 20 74 6f 20 70 61 73 73 20  ameters to pass 
35190 74 6f 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20  to xFilter.  If 
351a0 61 72 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e  argvIndex>0 then
351b0 0a 2a 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61  .** the right-ha
351c0 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 63  nd side of the c
351d0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f  orresponding aCo
351e0 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76  nstraint[] is ev
351f0 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62  aluated.** and b
35200 65 63 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49  ecomes the argvI
35210 6e 64 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e  ndex-th entry in
35220 20 61 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73   argv.  If aCons
35230 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d  traintUsage[].om
35240 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74  it.** is true, t
35250 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69  hen the constrai
35260 6e 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  nt is assumed to
35270 20 62 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65   be fully handle
35280 64 20 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74  d by the.** virt
35290 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73  ual table and is
352a0 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61   not checked aga
352b0 69 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a  in by SQLite..**
352c0 0a 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61  .** The idxNum a
352d0 6e 64 20 69 64 78 50 74 72 20 76 61 6c 75 65 73  nd idxPtr values
352e0 20 61 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e   are recorded an
352f0 64 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68  d passed into th
35300 65 0a 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d  e.** [xFilter] m
35310 65 74 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74  ethod..** [sqlit
35320 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73  e3_free()] is us
35330 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 74  ed to free idxPt
35340 72 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66  r if and only if
35350 66 0a 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49  f.** needToFreeI
35360 64 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a  dxPtr is true..*
35370 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79  *.** The orderBy
35380 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74  Consumed means t
35390 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20  hat output from 
353a0 5b 78 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74  [xFilter]/[xNext
353b0 5d 20 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a  ] will occur in.
353c0 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  ** the correct o
353d0 72 64 65 72 20 74 6f 20 73 61 74 69 73 66 79 20  rder to satisfy 
353e0 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
353f0 75 73 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73  use so that no s
35400 65 70 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69  eparate.** sorti
35410 6e 67 20 73 74 65 70 20 69 73 20 72 65 71 75 69  ng step is requi
35420 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  red..**.** The e
35430 73 74 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c  stimatedCost val
35440 75 65 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74  ue is an estimat
35450 65 20 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66  e of the cost of
35460 20 64 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61   doing the.** pa
35470 72 74 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e  rticular lookup.
35480 20 20 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66    A full scan of
35490 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20   a table with N 
354a0 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68  entries should h
354b0 61 76 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66  ave.** a cost of
354c0 20 4e 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65   N.  A binary se
354d0 61 72 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20  arch of a table 
354e0 6f 66 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f  of N entries sho
354f0 75 6c 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f  uld have a.** co
35500 73 74 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74  st of approximat
35510 65 6c 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73  ely log(N)..*/.s
35520 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
35530 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20  dex_info {.  /* 
35540 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20  Inputs */.  int 
35550 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20  nConstraint;    
35560 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
35570 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
35580 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  Constraint */.  
35590 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
355a0 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20  ndex_constraint 
355b0 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75  {.     int iColu
355c0 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
355d0 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65   /* Column on le
355e0 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ft-hand side of 
355f0 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  constraint */.  
35600 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
35610 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   op;         /* 
35620 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61  Constraint opera
35630 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69  tor */.     unsi
35640 67 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c 65  gned char usable
35650 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
35660 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
35670 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20   is usable */.  
35680 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73     int iTermOffs
35690 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  et;          /* 
356a0 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20  Used internally 
356b0 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f  - xBestIndex sho
356c0 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20  uld ignore */.  
356d0 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20  } *aConstraint; 
356e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
356f0 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61  ble of WHERE cla
35700 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  use constraints 
35710 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42  */.  int nOrderB
35720 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
35730 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72  /* Number of ter
35740 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20  ms in the ORDER 
35750 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73  BY clause */.  s
35760 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
35770 64 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20  dex_orderby {.  
35780 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20     int iColumn; 
35790 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
357a0 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f  Column number */
357b0 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  .     unsigned c
357c0 68 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 20  har desc;       
357d0 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43  /* True for DESC
357e0 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43  .  False for ASC
357f0 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72  . */.  } *aOrder
35800 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  By;             
35810 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
35820 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a  Y clause */.  /*
35830 20 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74   Outputs */.  st
35840 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
35850 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73  ex_constraint_us
35860 61 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72  age {.    int ar
35870 67 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  gvIndex;        
35880 20 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e     /* if >0, con
35890 73 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 20  straint is part 
358a0 6f 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74  of argv to xFilt
358b0 65 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  er */.    unsign
358c0 65 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20  ed char omit;   
358d0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64     /* Do not cod
358e0 65 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69  e a test for thi
358f0 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  s constraint */.
35900 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74    } *aConstraint
35910 55 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78  Usage;.  int idx
35920 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Num;            
35930 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73      /* Number us
35940 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74  ed to identify t
35950 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68  he index */.  ch
35960 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20  ar *idxStr;     
35970 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
35980 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74  ng, possibly obt
35990 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
359a0 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69  e3_malloc */.  i
359b0 6e 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78  nt needToFreeIdx
359c0 53 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65  Str;      /* Fre
359d0 65 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 73  e idxStr using s
359e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 66  qlite3_free() if
359f0 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f   true */.  int o
35a00 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20  rderByConsumed; 
35a10 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
35a20 20 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 61   output is alrea
35a30 64 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20  dy ordered */.  
35a40 64 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64  double estimated
35a50 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73  Cost;      /* Es
35a60 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20  timated cost of 
35a70 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78  using this index
35a80 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53   */.};.#define S
35a90 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
35aa0 54 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23  TRAINT_EQ    2.#
35ab0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
35ac0 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47  DEX_CONSTRAINT_G
35ad0 54 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53  T    4.#define S
35ae0 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
35af0 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23  TRAINT_LE    8.#
35b00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
35b10 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c  DEX_CONSTRAINT_L
35b20 54 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20  T    16.#define 
35b30 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
35b40 53 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32  STRAINT_GE    32
35b50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
35b60 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
35b70 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a  _MATCH 64../*.**
35b80 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73   CAPI3REF: Regis
35b90 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61  ter A Virtual Ta
35ba0 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  ble Implementati
35bb0 6f 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30  on {H18200} <S20
35bc0 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  400>.** EXPERIME
35bd0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  NTAL.**.** This 
35be0 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
35bf0 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65  to register a ne
35c00 77 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  w [virtual table
35c10 20 6d 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a   module] name..*
35c20 2a 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d  * Module names m
35c30 75 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65  ust be registere
35c40 64 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61  d before.** crea
35c50 74 69 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74  ting a new [virt
35c60 75 61 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67  ual table] using
35c70 20 74 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20   the module, or 
35c80 62 65 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a  before using a.*
35c90 2a 20 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76  * preexisting [v
35ca0 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f  irtual table] fo
35cb0 72 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a  r the module..**
35cc0 0a 2a 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e  .** The module n
35cd0 61 6d 65 20 69 73 20 72 65 67 69 73 74 65 72 65  ame is registere
35ce0 64 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61  d on the [databa
35cf0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73  se connection] s
35d00 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74  pecified.** by t
35d10 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
35d20 65 72 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66  er.  The name of
35d30 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67   the module is g
35d40 69 76 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20  iven by the .** 
35d50 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
35d60 2e 20 20 54 68 65 20 74 68 69 72 64 20 70 61 72  .  The third par
35d70 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e  ameter is a poin
35d80 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d  ter to.** the im
35d90 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
35da0 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62  the [virtual tab
35db0 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68  le module].   Th
35dc0 65 20 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61  e fourth.** para
35dd0 6d 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69  meter is an arbi
35de0 74 72 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74  trary client dat
35df0 61 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69  a pointer that i
35e00 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68  s passed through
35e10 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43  .** into the [xC
35e20 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e  reate] and [xCon
35e30 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66  nect] methods of
35e40 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
35e50 6c 65 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65  le module.** whe
35e60 6e 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20  n a new virtual 
35e70 74 61 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e  table is be bein
35e80 67 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 69  g created or rei
35e90 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a  nitialized..**.*
35ea0 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
35eb0 20 68 61 73 20 65 78 61 63 74 6c 79 20 74 68 65   has exactly the
35ec0 20 73 61 6d 65 20 65 66 66 65 63 74 20 61 73 20   same effect as 
35ed0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69  calling.** [sqli
35ee0 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
35ef0 65 5f 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e  e_v2()] with a N
35f00 55 4c 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20  ULL client data 
35f10 64 65 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53  destructor..*/.S
35f20 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
35f30 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e  _EXPERIMENTAL in
35f40 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
35f50 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74  _module(.  sqlit
35f60 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
35f70 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20        /* SQLite 
35f80 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65  connection to re
35f90 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69  gister module wi
35fa0 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  th */.  const ch
35fb0 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
35fc0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
35fd0 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f  e module */.  co
35fe0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
35ff0 6c 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68  le *p,   /* Meth
36000 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75  ods for the modu
36010 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43  le */.  void *pC
36020 6c 69 65 6e 74 44 61 74 61 20 20 20 20 20 20 20  lientData       
36030 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74     /* Client dat
36040 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43  a for xCreate/xC
36050 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a  onnect */.);../*
36060 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
36070 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c  gister A Virtual
36080 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74   Table Implement
36090 61 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c  ation {H18210} <
360a0 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S20400>.** EXPER
360b0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
360c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64  is routine is id
360d0 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b  entical to the [
360e0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
360f0 6f 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c  odule()] method,
36100 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20  .** except that 
36110 69 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20  it has an extra 
36120 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65  parameter to spe
36130 63 69 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72  cify .** a destr
36140 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66  uctor function f
36150 6f 72 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61  or the client da
36160 74 61 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c  ta pointer.  SQL
36170 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f  ite will.** invo
36180 6b 65 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  ke the destructo
36190 72 20 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69  r function (if i
361a0 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77  t is not NULL) w
361b0 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f  hen SQLite.** no
361c0 20 6c 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68   longer needs th
361d0 65 20 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f  e pClientData po
361e0 69 6e 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49  inter.  .*/.SQLI
361f0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
36200 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
36210 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
36220 64 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74  dule_v2(.  sqlit
36230 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
36240 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20        /* SQLite 
36250 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65  connection to re
36260 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69  gister module wi
36270 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  th */.  const ch
36280 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
36290 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
362a0 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f  e module */.  co
362b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
362c0 6c 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68  le *p,   /* Meth
362d0 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75  ods for the modu
362e0 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43  le */.  void *pC
362f0 6c 69 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20  lientData,      
36300 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74     /* Client dat
36310 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43  a for xCreate/xC
36320 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64  onnect */.  void
36330 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64  (*xDestroy)(void
36340 2a 29 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65  *)     /* Module
36350 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
36360 74 69 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  tion */.);../*.*
36370 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74  * CAPI3REF: Virt
36380 75 61 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e  ual Table Instan
36390 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31  ce Object {H1801
363a0 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b  0} <S20400>.** K
363b0 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33  EYWORDS: sqlite3
363c0 5f 76 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d  _vtab.** EXPERIM
363d0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72  ENTAL.**.** Ever
363e0 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  y [virtual table
363f0 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65   module] impleme
36400 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73  ntation uses a s
36410 75 62 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68  ubclass.** of th
36420 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
36430 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62  cture to describ
36440 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69  e a particular i
36450 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68  nstance.** of th
36460 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e [virtual table
36470 5d 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73  ].  Each subclas
36480 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69  s will.** be tai
36490 6c 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65  lored to the spe
364a0 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74  cific needs of t
364b0 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  he module implem
364c0 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65  entation..** The
364d0 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73   purpose of this
364e0 20 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74   superclass is t
364f0 6f 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e  o define certain
36500 20 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65   fields that are
36510 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c  .** common to al
36520 6c 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  l module impleme
36530 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  ntations..**.** 
36540 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  Virtual tables m
36550 65 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61  ethods can set a
36560 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
36570 62 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a  by assigning a.*
36580 2a 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65  * string obtaine
36590 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
365a0 6d 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45  mprintf()] to zE
365b0 72 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68  rrMsg.  The meth
365c0 6f 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b  od should.** tak
365d0 65 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20  e care that any 
365e0 70 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20  prior string is 
365f0 66 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20  freed by a call 
36600 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  to [sqlite3_free
36610 28 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20  ()].** prior to 
36620 61 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20  assigning a new 
36630 73 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73  string to zErrMs
36640 67 2e 20 20 41 66 74 65 72 20 74 68 65 20 65 72  g.  After the er
36650 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69  ror message.** i
36660 73 20 64 65 6c 69 76 65 72 65 64 20 75 70 20 74  s delivered up t
36670 6f 20 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70  o the client app
36680 6c 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74  lication, the st
36690 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74  ring will be aut
366a0 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72  omatically.** fr
366b0 65 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66  eed by sqlite3_f
366c0 72 65 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45  ree() and the zE
366d0 72 72 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c  rrMsg field will
366e0 20 62 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73   be zeroed..*/.s
366f0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74  truct sqlite3_vt
36700 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  ab {.  const sql
36710 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
36720 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f  dule;  /* The mo
36730 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69  dule for this vi
36740 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
36750 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
36760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36770 20 2f 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53   /* NO LONGER US
36780 45 44 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45  ED */.  char *zE
36790 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20  rrMsg;          
367a0 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
367b0 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71   message from sq
367c0 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20  lite3_mprintf() 
367d0 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20  */.  /* Virtual 
367e0 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
367f0 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63  tions will typic
36800 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f  ally add additio
36810 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b  nal fields */.};
36820 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
36830 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20  : Virtual Table 
36840 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b  Cursor Object  {
36850 48 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e  H18020} <S20400>
36860 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71  .** KEYWORDS: sq
36870 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
36880 72 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  r {virtual table
36890 20 63 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45   cursor}.** EXPE
368a0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45  RIMENTAL.**.** E
368b0 76 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61  very [virtual ta
368c0 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c  ble module] impl
368d0 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20  ementation uses 
368e0 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68  a subclass of th
368f0 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73  e.** following s
36900 74 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63  tructure to desc
36910 72 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61  ribe cursors tha
36920 74 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65  t point into the
36930 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62  .** [virtual tab
36940 6c 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64  le] and are used
36950 0a 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f  .** to loop thro
36960 75 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20  ugh the virtual 
36970 74 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20  table.  Cursors 
36980 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e  are created usin
36990 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  g the.** [sqlite
369a0 33 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c  3_module.xOpen |
369b0 20 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f   xOpen] method o
369c0 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64  f the module and
369d0 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a   are destroyed.*
369e0 2a 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65  * by the [sqlite
369f0 33 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20  3_module.xClose 
36a00 7c 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64  | xClose] method
36a10 2e 20 20 43 75 73 73 6f 72 73 20 61 72 65 20 75  .  Cussors are u
36a20 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78  sed.** by the [x
36a30 46 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d  Filter], [xNext]
36a40 2c 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75  , [xEof], [xColu
36a50 6d 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64  mn], and [xRowid
36a60 5d 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20  ] methods.** of 
36a70 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63  the module.  Eac
36a80 68 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  h module impleme
36a90 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66  ntation will def
36aa0 69 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65  ine.** the conte
36ab0 6e 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73  nt of a cursor s
36ac0 74 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74  tructure to suit
36ad0 20 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a   its own needs..
36ae0 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72  **.** This super
36af0 63 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20  class exists in 
36b00 6f 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20  order to define 
36b10 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75  fields of the cu
36b20 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65  rsor that.** are
36b30 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69   common to all i
36b40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a  mplementations..
36b50 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
36b60 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a  3_vtab_cursor {.
36b70 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
36b80 70 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56  pVtab;      /* V
36b90 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20  irtual table of 
36ba0 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20  this cursor */. 
36bb0 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
36bc0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
36bd0 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79  s will typically
36be0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
36bf0 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  fields */.};../*
36c00 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
36c10 63 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61  clare The Schema
36c20 20 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61   Of A Virtual Ta
36c30 62 6c 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32  ble {H18280} <S2
36c40 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0400>.** EXPERIM
36c50 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ENTAL.**.** The 
36c60 5b 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78  [xCreate] and [x
36c70 43 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73  Connect] methods
36c80 20 6f 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61   of a.** [virtua
36c90 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20  l table module] 
36ca0 63 61 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66  call this interf
36cb0 61 63 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72  ace.** to declar
36cc0 65 20 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68  e the format (th
36cd0 65 20 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61  e names and data
36ce0 74 79 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c  types of the col
36cf0 75 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20  umns) of.** the 
36d00 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74  virtual tables t
36d10 68 65 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a  hey implement..*
36d20 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
36d30 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
36d40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63   int sqlite3_dec
36d50 6c 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65  lare_vtab(sqlite
36d60 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  3*, const char *
36d70 7a 53 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  zSQL);../*.** CA
36d80 50 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64  PI3REF: Overload
36d90 20 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20   A Function For 
36da0 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20  A Virtual Table 
36db0 7b 48 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30  {H18300} <S20400
36dc0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
36dd0 4c 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20  L.**.** Virtual 
36de0 74 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69  tables can provi
36df0 64 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69  de alternative i
36e00 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
36e10 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75  f functions.** u
36e20 73 69 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46  sing the [xFindF
36e30 75 6e 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20  unction] method 
36e40 6f 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20  of the [virtual 
36e50 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20  table module].  
36e60 0a 2a 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76  .** But global v
36e70 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65  ersions of those
36e80 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75   functions.** mu
36e90 73 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65  st exist in orde
36ea0 72 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64  r to be overload
36eb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41  ed..**.** This A
36ec0 50 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20  PI makes sure a 
36ed0 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f  global version o
36ee0 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  f a function wit
36ef0 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  h a particular.*
36f00 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65  * name and numbe
36f10 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20  r of parameters 
36f20 65 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73  exists.  If no s
36f30 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69  uch function exi
36f40 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68  sts.** before th
36f50 69 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64  is API is called
36f60 2c 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e  , a new function
36f70 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68   is created.  Th
36f80 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
36f90 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66  .** of the new f
36fa0 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63  unction always c
36fb0 61 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69  auses an excepti
36fc0 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e  on to be thrown.
36fd0 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20    So.** the new 
36fe0 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  function is not 
36ff0 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e  good for anythin
37000 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74  g by itself.  It
37010 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73  s only.** purpos
37020 65 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61  e is to be a pla
37030 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f  ceholder functio
37040 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76  n that can be ov
37050 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61  erloaded.** by a
37060 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d   [virtual table]
37070 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
37080 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
37090 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TAL int sqlite3_
370a0 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f  overload_functio
370b0 6e 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73  n(sqlite3*, cons
370c0 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d  t char *zFuncNam
370d0 65 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f  e, int nArg);../
370e0 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61  *.** The interfa
370f0 63 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61  ce to the virtua
37100 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73  l-table mechanis
37110 6d 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20  m defined above 
37120 28 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61  (back up.** to a
37130 20 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61   comment remarka
37140 62 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74  bly similar to t
37150 68 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72  his one) is curr
37160 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64  ently considered
37170 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69  .** to be experi
37180 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74  mental.  The int
37190 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61  erface might cha
371a0 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69  nge in incompati
371b0 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20  ble ways..** If 
371c0 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65  this is a proble
371d0 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f  m for you, do no
371e0 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66  t use the interf
371f0 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65  ace at this time
37200 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
37210 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
37220 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69  echanism stabili
37230 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63  zes, we will dec
37240 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65  lare the.** inte
37250 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70  rface fixed, sup
37260 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69  port it indefini
37270 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65  tely, and remove
37280 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a   this comment..*
37290 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d  *.****** EXPERIM
372a0 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20  ENTAL - subject 
372b0 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75  to change withou
372c0 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a  t notice *******
372d0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a  *******.*/../*.*
372e0 2a 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61  * CAPI3REF: A Ha
372f0 6e 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20  ndle To An Open 
37300 42 4c 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53  BLOB {H17800} <S
37310 33 30 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  30230>.** KEYWOR
37320 44 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65  DS: {BLOB handle
37330 7d 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d  } {BLOB handles}
37340 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  .**.** An instan
37350 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
37360 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  t represents an 
37370 6f 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69  open BLOB on whi
37380 63 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  ch.** [sqlite3_b
37390 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65  lob_open | incre
373a0 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d  mental BLOB I/O]
373b0 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65   can be performe
373c0 64 2e 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66  d..** Objects of
373d0 20 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63   this type are c
373e0 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74  reated by [sqlit
373f0 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a  e3_blob_open()].
37400 2a 2a 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64  ** and destroyed
37410 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f   by [sqlite3_blo
37420 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54  b_close()]..** T
37430 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
37440 5f 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71  _read()] and [sq
37450 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65  lite3_blob_write
37460 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a  ()] interfaces.*
37470 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  * can be used to
37480 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 73   read or write s
37490 6d 61 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73  mall subsections
374a0 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a   of the BLOB..**
374b0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c   The [sqlite3_bl
374c0 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65  ob_bytes()] inte
374d0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68  rface returns th
374e0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c  e size of the BL
374f0 4f 42 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a  OB in bytes..*/.
37500 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
37510 71 6c 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69  qlite3_blob sqli
37520 74 65 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a  te3_blob;../*.**
37530 20 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20   CAPI3REF: Open 
37540 41 20 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65  A BLOB For Incre
37550 6d 65 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38  mental I/O {H178
37560 31 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a  10} <S30230>.**.
37570 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  ** This interfac
37580 65 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42  es opens a [BLOB
37590 20 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65   handle | handle
375a0 5d 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f  ] to the BLOB lo
375b0 63 61 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20  cated.** in row 
375c0 69 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f  iRow, column zCo
375d0 6c 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62  lumn, table zTab
375e0 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a  le in database z
375f0 44 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20  Db;.** in other 
37600 77 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20  words, the same 
37610 42 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20  BLOB that would 
37620 62 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a  be selected by:.
37630 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20  **.** <pre>.**  
37640 20 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d     SELECT zColum
37650 6e 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c  n FROM zDb.zTabl
37660 65 20 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20  e WHERE [rowid] 
37670 3d 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65  = iRow;.** </pre
37680 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66  > {END}.**.** If
37690 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   the flags param
376a0 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  eter is non-zero
376b0 2c 20 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20  , then the BLOB 
376c0 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65  is opened for re
376d0 61 64 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20  ad.** and write 
376e0 61 63 63 65 73 73 2e 20 49 66 20 69 74 20 69 73  access. If it is
376f0 20 7a 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20   zero, the BLOB 
37700 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65  is opened for re
37710 61 64 20 61 63 63 65 73 73 2e 0a 2a 2a 20 49 74  ad access..** It
37720 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
37730 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d   to open a colum
37740 6e 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f  n that is part o
37750 66 20 61 6e 20 69 6e 64 65 78 20 6f 72 20 70 72  f an index or pr
37760 69 6d 61 72 79 20 0a 2a 2a 20 6b 65 79 20 66 6f  imary .** key fo
37770 72 20 77 72 69 74 69 6e 67 2e 20 5e 49 66 20 5b  r writing. ^If [
37780 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
37790 74 72 61 69 6e 74 73 5d 20 61 72 65 20 65 6e 61  traints] are ena
377a0 62 6c 65 64 2c 20 69 74 20 69 73 20 0a 2a 2a 20  bled, it is .** 
377b0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
377c0 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68  open a column th
377d0 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 20  at is part of a 
377e0 5b 63 68 69 6c 64 20 6b 65 79 5d 20 66 6f 72 20  [child key] for 
377f0 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e  writing..**.** N
37800 6f 74 65 20 74 68 61 74 20 74 68 65 20 64 61 74  ote that the dat
37810 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f  abase name is no
37820 74 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74  t the filename t
37830 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  hat contains.** 
37840 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74  the database but
37850 20 72 61 74 68 65 72 20 74 68 65 20 73 79 6d 62   rather the symb
37860 6f 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65  olic name of the
37870 20 64 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a   database that.*
37880 2a 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 68  * is assigned wh
37890 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
378a0 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69  is connected usi
378b0 6e 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20  ng [ATTACH]..** 
378c0 46 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  For the main dat
378d0 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20  abase file, the 
378e0 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73  database name is
378f0 20 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20   "main"..** For 
37900 54 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65  TEMP tables, the
37910 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69   database name i
37920 73 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20  s "temp"..**.** 
37930 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c  On success, [SQL
37940 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72  ITE_OK] is retur
37950 6e 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20  ned and the new 
37960 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73  [BLOB handle] is
37970 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a   written.** to *
37980 70 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73  ppBlob. Otherwis
37990 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  e an [error code
379a0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
379b0 64 20 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 74  d *ppBlob is set
379c0 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c 6c  .** to be a null
379d0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 69   pointer..** Thi
379e0 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20  s function sets 
379f0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
37a00 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20  nnection] error 
37a10 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65  code and message
37a20 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76  .** accessible v
37a30 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63  ia [sqlite3_errc
37a40 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ode()] and [sqli
37a50 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e  te3_errmsg()] an
37a60 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e  d related.** fun
37a70 63 74 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68  ctions.  Note th
37a80 61 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76  at the *ppBlob v
37a90 61 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79  ariable is alway
37aa0 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e  s initialized in
37ab0 20 61 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 6d   a.** way that m
37ac0 61 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f 20  akes it safe to 
37ad0 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f  invoke [sqlite3_
37ae0 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e  blob_close()] on
37af0 20 2a 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61   *ppBlob.** rega
37b00 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75  rdless of the su
37b10 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65  ccess or failure
37b20 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
37b30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
37b40 6f 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68  ow that a BLOB h
37b50 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20  andle points to 
37b60 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61  is modified by a
37b70 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b  n.** [UPDATE], [
37b80 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b  DELETE], or by [
37b90 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64  ON CONFLICT] sid
37ba0 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65  e-effects.** the
37bb0 6e 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c  n the BLOB handl
37bc0 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22  e is marked as "
37bd0 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69  expired"..** Thi
37be0 73 20 69 73 20 74 72 75 65 20 69 66 20 61 6e 79  s is true if any
37bf0 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72   column of the r
37c00 6f 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65  ow is changed, e
37c10 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  ven a column.** 
37c20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f  other than the o
37c30 6e 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64  ne the BLOB hand
37c40 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a  le is open on..*
37c50 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  * Calls to [sqli
37c60 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d  te3_blob_read()]
37c70 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c   and [sqlite3_bl
37c80 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a  ob_write()] for.
37c90 2a 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f  ** a expired BLO
37ca0 42 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69  B handle fail wi
37cb0 74 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64  th an return cod
37cc0 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f  e of [SQLITE_ABO
37cd0 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20  RT]..** Changes 
37ce0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42  written into a B
37cf0 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65  LOB prior to the
37d00 20 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61   BLOB expiring a
37d10 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61  re not.** rollba
37d20 63 6b 20 62 79 20 74 68 65 20 65 78 70 69 72 61  ck by the expira
37d30 74 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42  tion of the BLOB
37d40 2e 20 20 53 75 63 68 20 63 68 61 6e 67 65 73 20  .  Such changes 
37d50 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a  will eventually.
37d60 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65  ** commit if the
37d70 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e   transaction con
37d80 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65  tinues to comple
37d90 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20  tion..**.** Use 
37da0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  the [sqlite3_blo
37db0 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72  b_bytes()] inter
37dc0 66 61 63 65 20 74 6f 20 64 65 74 65 72 6d 69 6e  face to determin
37dd0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a  e the size of.**
37de0 20 74 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62   the opened blob
37df0 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61  .  The size of a
37e00 20 62 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65   blob may not be
37e10 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73   changed by this
37e20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20  .** interface.  
37e30 55 73 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d  Use the [UPDATE]
37e40 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20   SQL command to 
37e50 63 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20  change the size 
37e60 6f 66 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a  of a.** blob..**
37e70 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
37e80 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29  _bind_zeroblob()
37e90 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72  ] and [sqlite3_r
37ea0 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29  esult_zeroblob()
37eb0 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  ] interfaces.** 
37ec0 61 6e 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  and the built-in
37ed0 20 5b 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c 20   [zeroblob] SQL 
37ee0 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 20  function can be 
37ef0 75 73 65 64 2c 20 69 66 20 64 65 73 69 72 65 64  used, if desired
37f00 2c 0a 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 61  ,.** to create a
37f10 6e 20 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 69  n empty, zero-fi
37f20 6c 6c 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 69  lled blob in whi
37f30 63 68 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72  ch to read or wr
37f40 69 74 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69  ite using.** thi
37f50 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  s interface..**.
37f60 2a 2a 20 54 6f 20 61 76 6f 69 64 20 61 20 72 65  ** To avoid a re
37f70 73 6f 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 65  source leak, eve
37f80 72 79 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61  ry open [BLOB ha
37f90 6e 64 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 65  ndle] should eve
37fa0 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 65  ntually.** be re
37fb0 6c 65 61 73 65 64 20 62 79 20 61 20 63 61 6c 6c  leased by a call
37fc0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f   to [sqlite3_blo
37fd0 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a  b_close()]..**.*
37fe0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
37ff0 2a 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 37  ** [H17813] [H17
38000 38 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48  814] [H17816] [H
38010 31 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d 20  17819] [H17821] 
38020 5b 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49  [H17824].*/.SQLI
38030 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
38040 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20  e3_blob_open(.  
38050 73 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73  sqlite3*,.  cons
38060 74 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63  t char *zDb,.  c
38070 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c  onst char *zTabl
38080 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e,.  const char 
38090 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69  *zColumn,.  sqli
380a0 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a  te3_int64 iRow,.
380b0 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73    int flags,.  s
380c0 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70  qlite3_blob **pp
380d0 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  Blob.);../*.** C
380e0 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41  API3REF: Close A
380f0 20 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31   BLOB Handle {H1
38100 37 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a  7830} <S30230>.*
38110 2a 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f  *.** Closes an o
38120 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  pen [BLOB handle
38130 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67  ]..**.** Closing
38140 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61   a BLOB shall ca
38150 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  use the current 
38160 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63  transaction to c
38170 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72  ommit.** if ther
38180 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42  e are no other B
38190 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67  LOBs, no pending
381a0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
381b0 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a  ents, and the.**
381c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
381d0 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f  tion is in [auto
381e0 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a  commit mode]..**
381f0 20 49 66 20 61 6e 79 20 77 72 69 74 65 73 20 77   If any writes w
38200 65 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20  ere made to the 
38210 42 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74  BLOB, they might
38220 20 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68   be held in cach
38230 65 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63  e.** until the c
38240 6c 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69  lose operation i
38250 66 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e  f they will fit.
38260 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74  .**.** Closing t
38270 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f  he BLOB often fo
38280 72 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73  rces the changes
38290 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20  .** out to disk 
382a0 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f  and so if any I/
382b0 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20  O errors occur, 
382c0 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79  they will likely
382d0 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65   occur.** at the
382e0 20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42   time when the B
382f0 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20  LOB is closed.  
38300 41 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 20  Any errors that 
38310 6f 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20  occur during.** 
38320 63 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f  closing are repo
38330 72 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65  rted as a non-ze
38340 72 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e  ro return value.
38350 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20  .**.** The BLOB 
38360 69 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64  is closed uncond
38370 69 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e  itionally.  Even
38380 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   if this routine
38390 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65   returns.** an e
383a0 72 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42  rror code, the B
383b0 4c 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f  LOB is still clo
383c0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69  sed..**.** Calli
383d0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ng this routine 
383e0 77 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e  with a null poin
383f0 74 65 72 20 28 77 68 69 63 68 20 61 73 20 77 6f  ter (which as wo
38400 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 0a  uld be returned.
38410 2a 2a 20 62 79 20 66 61 69 6c 65 64 20 63 61 6c  ** by failed cal
38420 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  l to [sqlite3_bl
38430 6f 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 61  ob_open()]) is a
38440 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e   harmless no-op.
38450 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
38460 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d  nts:.** [H17833]
38470 20 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33   [H17836] [H1783
38480 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  9].*/.SQLITE_API
38490 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
384a0 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  b_close(sqlite3_
384b0 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  blob *);../*.** 
384c0 43 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e  CAPI3REF: Return
384d0 20 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20   The Size Of An 
384e0 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34  Open BLOB {H1784
384f0 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a  0} <S30230>.**.*
38500 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 69  * Returns the si
38510 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
38520 68 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62  he BLOB accessib
38530 6c 65 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 73  le via the .** s
38540 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e  uccessfully open
38550 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  ed [BLOB handle]
38560 20 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67   in its only arg
38570 75 6d 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 69  ument.  The.** i
38580 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20  ncremental blob 
38590 49 2f 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 6e  I/O routines can
385a0 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f 76   only read or ov
385b0 65 72 77 72 69 74 69 6e 67 20 65 78 69 73 74 69  erwriting existi
385c0 6e 67 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 65  ng.** blob conte
385d0 6e 74 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 20  nt; they cannot 
385e0 63 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20  change the size 
385f0 6f 66 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a  of a blob..**.**
38600 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   This routine on
38610 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42  ly works on a [B
38620 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63  LOB handle] whic
38630 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74  h has been creat
38640 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72  ed.** by a prior
38650 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
38660 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f   to [sqlite3_blo
38670 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68  b_open()] and wh
38680 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62  ich has not.** b
38690 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73  een closed by [s
386a0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
386b0 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61  e()].  Passing a
386c0 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72  ny other pointer
386d0 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72   in.** to this r
386e0 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69  outine results i
386f0 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20  n undefined and 
38700 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72  probably undesir
38710 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a  able behavior..*
38720 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
38730 73 3a 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a  s:.** [H17843].*
38740 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
38750 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79   sqlite3_blob_by
38760 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  tes(sqlite3_blob
38770 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49   *);../*.** CAPI
38780 33 52 45 46 3a 20 52 65 61 64 20 44 61 74 61 20  3REF: Read Data 
38790 46 72 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72  From A BLOB Incr
387a0 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35  ementally {H1785
387b0 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a  0} <S30230>.**.*
387c0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
387d0 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20  is used to read 
387e0 64 61 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65  data from an ope
387f0 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20  n [BLOB handle] 
38800 69 6e 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72  into a.** caller
38810 2d 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72  -supplied buffer
38820 2e 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74  . N bytes of dat
38830 61 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74  a are copied int
38840 6f 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72  o buffer Z.** fr
38850 6f 6d 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42  om the open BLOB
38860 2c 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66  , starting at of
38870 66 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a  fset iOffset..**
38880 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f  .** If offset iO
38890 66 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 68  ffset is less th
388a0 61 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20  an N bytes from 
388b0 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42  the end of the B
388c0 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  LOB,.** [SQLITE_
388d0 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e  ERROR] is return
388e0 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69  ed and no data i
388f0 73 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72  s read.  If N or
38900 20 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c   iOffset is.** l
38910 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b  ess than zero, [
38920 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73  SQLITE_ERROR] is
38930 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f   returned and no
38940 20 64 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a   data is read..*
38950 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68  * The size of th
38960 65 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63  e blob (and henc
38970 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61  e the maximum va
38980 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74  lue of N+iOffset
38990 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65  ).** can be dete
389a0 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65  rmined using the
389b0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62   [sqlite3_blob_b
389c0 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63  ytes()] interfac
389d0 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65  e..**.** An atte
389e0 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d  mpt to read from
389f0 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f   an expired [BLO
38a00 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20  B handle] fails 
38a10 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  with an.** error
38a20 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45   code of [SQLITE
38a30 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f  _ABORT]..**.** O
38a40 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54  n success, SQLIT
38a50 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
38a60 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  ..** Otherwise, 
38a70 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  an [error code] 
38a80 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20  or an [extended 
38a90 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72  error code] is r
38aa0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
38ab0 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  his routine only
38ac0 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f   works on a [BLO
38ad0 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20  B handle] which 
38ae0 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64  has been created
38af0 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73  .** by a prior s
38b00 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
38b10 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  o [sqlite3_blob_
38b20 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63  open()] and whic
38b30 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65  h has not.** bee
38b40 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c  n closed by [sql
38b50 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28  ite3_blob_close(
38b60 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79  )].  Passing any
38b70 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69   other pointer i
38b80 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75  n.** to this rou
38b90 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20  tine results in 
38ba0 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72  undefined and pr
38bb0 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62  obably undesirab
38bc0 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a  le behavior..**.
38bd0 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
38be0 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65  lite3_blob_write
38bf0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
38c00 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37  rements:.** [H17
38c10 38 35 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48  853] [H17856] [H
38c20 31 37 38 35 39 5d 20 5b 48 31 37 38 36 32 5d 20  17859] [H17862] 
38c30 5b 48 31 37 38 36 33 5d 20 5b 48 31 37 38 36 35  [H17863] [H17865
38c40 5d 20 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51  ] [H17868].*/.SQ
38c50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
38c60 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73  ite3_blob_read(s
38c70 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76  qlite3_blob *, v
38c80 6f 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69  oid *Z, int N, i
38c90 6e 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a  nt iOffset);../*
38ca0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72  .** CAPI3REF: Wr
38cb0 69 74 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20  ite Data Into A 
38cc0 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c  BLOB Incremental
38cd0 6c 79 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 30  ly {H17870} <S30
38ce0 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  230>.**.** This 
38cf0 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
38d00 20 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 69   to write data i
38d10 6e 74 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f  nto an open [BLO
38d20 42 20 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61  B handle] from a
38d30 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c  .** caller-suppl
38d40 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79  ied buffer. N by
38d50 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20  tes of data are 
38d60 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20  copied from the 
38d70 62 75 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f  buffer Z.** into
38d80 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20   the open BLOB, 
38d90 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73  starting at offs
38da0 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a  et iOffset..**.*
38db0 2a 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68  * If the [BLOB h
38dc0 61 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73  andle] passed as
38dd0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
38de0 65 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e  ent was not open
38df0 65 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e  ed for.** writin
38e00 67 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 72  g (the flags par
38e10 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
38e20 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20  e3_blob_open()] 
38e30 77 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68  was zero),.** th
38e40 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
38e50 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44  rns [SQLITE_READ
38e60 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ONLY]..**.** Thi
38e70 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f  s function may o
38e80 6e 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63  nly modify the c
38e90 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42  ontents of the B
38ea0 4c 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f  LOB; it is.** no
38eb0 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e  t possible to in
38ec0 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20  crease the size 
38ed0 6f 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20  of a BLOB using 
38ee0 74 68 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20  this API..** If 
38ef0 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69  offset iOffset i
38f00 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79  s less than N by
38f10 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64  tes from the end
38f20 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a   of the BLOB,.**
38f30 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
38f40 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
38f50 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74  no data is writt
38f60 65 6e 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20  en.  If N is.** 
38f70 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b  less than zero [
38f80 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73  SQLITE_ERROR] is
38f90 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f   returned and no
38fa0 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e   data is written
38fb0 2e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66  ..** The size of
38fc0 20 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 68   the BLOB (and h
38fd0 65 6e 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d  ence the maximum
38fe0 20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66   value of N+iOff
38ff0 73 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64  set).** can be d
39000 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20  etermined using 
39010 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  the [sqlite3_blo
39020 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72  b_bytes()] inter
39030 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61  face..**.** An a
39040 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
39050 74 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42  to an expired [B
39060 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c  LOB handle] fail
39070 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72  s with an.** err
39080 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49  or code of [SQLI
39090 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74  TE_ABORT].  Writ
390a0 65 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74  es to the BLOB t
390b0 68 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20  hat occurred.** 
390c0 62 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42  before the [BLOB
390d0 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64   handle] expired
390e0 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20   are not rolled 
390f0 62 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65  back by the.** e
39100 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  xpiration of the
39110 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20   handle, though 
39120 6f 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20  of course those 
39130 63 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a  changes might.**
39140 20 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77   have been overw
39150 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74  ritten by the st
39160 61 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70  atement that exp
39170 69 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61  ired the BLOB ha
39180 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74  ndle.** or by ot
39190 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20  her independent 
391a0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
391b0 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51  * On success, SQ
391c0 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
391d0 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ned..** Otherwis
391e0 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f  e, an  [error co
391f0 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e  de] or an [exten
39200 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20  ded error code] 
39210 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
39220 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
39230 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20  only works on a 
39240 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68  [BLOB handle] wh
39250 69 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65  ich has been cre
39260 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69  ated.** by a pri
39270 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  or successful ca
39280 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  ll to [sqlite3_b
39290 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20  lob_open()] and 
392a0 77 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a  which has not.**
392b0 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20   been closed by 
392c0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c  [sqlite3_blob_cl
392d0 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67  ose()].  Passing
392e0 20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74   any other point
392f0 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73  er in.** to this
39300 20 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73   routine results
39310 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e   in undefined an
39320 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73  d probably undes
39330 69 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e  irable behavior.
39340 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
39350 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72   [sqlite3_blob_r
39360 65 61 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65  ead()]..**.** Re
39370 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
39380 48 31 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d  H17873] [H17874]
39390 20 5b 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37   [H17875] [H1787
393a0 36 5d 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37  6] [H17877] [H17
393b0 38 37 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48  879] [H17882] [H
393c0 31 37 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38  17885].** [H1788
393d0 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  8].*/.SQLITE_API
393e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
393f0 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f  b_write(sqlite3_
39400 62 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f  blob *, const vo
39410 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e  id *z, int n, in
39420 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a  t iOffset);../*.
39430 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72  ** CAPI3REF: Vir
39440 74 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d  tual File System
39450 20 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30   Objects {H11200
39460 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20100>.**.**
39470 20 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73   A virtual files
39480 79 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 61  ystem (VFS) is a
39490 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  n [sqlite3_vfs] 
394a0 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53  object.** that S
394b0 51 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e  QLite uses to in
394c0 74 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74  teract.** with t
394d0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70  he underlying op
394e0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20  erating system. 
394f0 20 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69   Most SQLite bui
39500 6c 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a  lds come with a.
39510 2a 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c  ** single defaul
39520 74 20 56 46 53 20 74 68 61 74 20 69 73 20 61 70  t VFS that is ap
39530 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68  propriate for th
39540 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e  e host computer.
39550 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61  .** New VFSes ca
39560 6e 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20  n be registered 
39570 61 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 53  and existing VFS
39580 65 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69  es can be unregi
39590 73 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66  stered..** The f
395a0 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61  ollowing interfa
395b0 63 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 64  ces are provided
395c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
395d0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69  te3_vfs_find() i
395e0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
395f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
39600 56 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61  VFS given its na
39610 6d 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65  me..** Names are
39620 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e   case sensitive.
39630 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65  .** Names are ze
39640 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
39650 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20  F-8 strings..** 
39660 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  If there is no m
39670 61 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69  atch, a NULL poi
39680 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
39690 2e 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65  ..** If zVfsName
396a0 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68   is NULL then th
396b0 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73  e default VFS is
396c0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
396d0 20 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 72   New VFSes are r
396e0 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 73  egistered with s
396f0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
39700 74 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e  ter()..** Each n
39710 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74  ew VFS becomes t
39720 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69  he default VFS i
39730 66 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66  f the makeDflt f
39740 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54  lag is set..** T
39750 68 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20  he same VFS can 
39760 62 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75  be registered mu
39770 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74  ltiple times wit
39780 68 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20  hout injury..** 
39790 54 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74  To make an exist
397a0 69 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65  ing VFS into the
397b0 20 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65   default VFS, re
397c0 67 69 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a  gister it again.
397d0 2a 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65  ** with the make
397e0 44 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20  Dflt flag set.  
397f0 49 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74  If two different
39800 20 56 46 53 65 73 20 77 69 74 68 20 74 68 65 0a   VFSes with the.
39810 2a 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65  ** same name are
39820 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65   registered, the
39830 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
39840 65 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a  efined.  If a.**
39850 20 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72   VFS is register
39860 65 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74  ed with a name t
39870 68 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61  hat is NULL or a
39880 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a  n empty string,.
39890 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 61  ** then the beha
398a0 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
398b0 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73  d..**.** Unregis
398c0 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74  ter a VFS with t
398d0 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75  he sqlite3_vfs_u
398e0 6e 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 65  nregister() inte
398f0 72 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65  rface..** If the
39900 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20   default VFS is 
39910 75 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e  unregistered, an
39920 6f 74 68 65 72 20 56 46 53 20 69 73 20 63 68 6f  other VFS is cho
39930 73 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65  sen as.** the de
39940 66 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69  fault.  The choi
39950 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56  ce for the new V
39960 46 53 20 69 73 20 61 72 62 69 74 72 61 72 79 2e  FS is arbitrary.
39970 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
39980 6e 74 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d  nts:.** [H11203]
39990 20 5b 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30   [H11206] [H1120
399a0 39 5d 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31  9] [H11212] [H11
399b0 32 31 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f  215] [H11218].*/
399c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
399d0 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33  te3_vfs *sqlite3
399e0 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20  _vfs_find(const 
399f0 63 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b  char *zVfsName);
39a00 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
39a10 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
39a20 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73  ster(sqlite3_vfs
39a30 2a 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29  *, int makeDflt)
39a40 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
39a50 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
39a60 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f  egister(sqlite3_
39a70 76 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  vfs*);../*.** CA
39a80 50 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20  PI3REF: Mutexes 
39a90 7b 48 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30  {H17000} <S20000
39aa0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69  >.**.** The SQLi
39ab0 74 65 20 63 6f 72 65 20 75 73 65 73 20 74 68 65  te core uses the
39ac0 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20  se routines for 
39ad0 74 68 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72  thread.** synchr
39ae0 6f 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67  onization. Thoug
39af0 68 20 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e  h they are inten
39b00 64 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  ded for internal
39b10 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
39b20 65 2c 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e  e, code that lin
39b30 6b 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74  ks against SQLit
39b40 65 20 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65  e is.** permitte
39b50 64 20 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20  d to use any of 
39b60 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a  these routines..
39b70 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65  **.** The SQLite
39b80 20 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e   source code con
39b90 74 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69  tains multiple i
39ba0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a  mplementations.*
39bb0 2a 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78  * of these mutex
39bc0 20 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61   routines.  An a
39bd0 70 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65  ppropriate imple
39be0 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20  mentation.** is 
39bf0 73 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74  selected automat
39c00 69 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c  ically at compil
39c10 65 2d 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c  e-time.  The fol
39c20 6c 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d  lowing.** implem
39c30 65 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76  entations are av
39c40 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53  ailable in the S
39c50 51 4c 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a  QLite core:.**.*
39c60 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
39c70 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53   SQLITE_MUTEX_OS
39c80 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49  2.** <li>   SQLI
39c90 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44  TE_MUTEX_PTHREAD
39ca0 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54  .** <li>   SQLIT
39cb0 45 5f 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c  E_MUTEX_W32.** <
39cc0 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  li>   SQLITE_MUT
39cd0 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e  EX_NOOP.** </ul>
39ce0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  .**.** The SQLIT
39cf0 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70  E_MUTEX_NOOP imp
39d00 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
39d10 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
39d20 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  .** that does no
39d30 20 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e   real locking an
39d40 64 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  d is appropriate
39d50 20 66 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61   for use in.** a
39d60 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64   single-threaded
39d70 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54   application.  T
39d80 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  he SQLITE_MUTEX_
39d90 4f 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  OS2,.** SQLITE_M
39da0 55 54 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e  UTEX_PTHREAD, an
39db0 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57  d SQLITE_MUTEX_W
39dc0 33 32 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  32 implementatio
39dd0 6e 73 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70  ns.** are approp
39de0 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e  riate for use on
39df0 20 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64   OS/2, Unix, and
39e00 20 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20   Windows..**.** 
39e10 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
39e20 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53  piled with the S
39e30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44  QLITE_MUTEX_APPD
39e40 45 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a  EF preprocessor.
39e50 2a 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  ** macro defined
39e60 20 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45   (with "-DSQLITE
39e70 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22  _MUTEX_APPDEF=1"
39e80 29 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78  ), then no mutex
39e90 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
39ea0 6f 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77  on is included w
39eb0 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e  ith the library.
39ec0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
39ed0 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  e.** application
39ee0 20 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 63   must supply a c
39ef0 75 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c  ustom mutex impl
39f00 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67  ementation using
39f10 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f   the.** [SQLITE_
39f20 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70  CONFIG_MUTEX] op
39f30 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
39f40 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e  te3_config() fun
39f50 63 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20  ction.** before 
39f60 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f  calling sqlite3_
39f70 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20  initialize() or 
39f80 61 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63  any other public
39f90 20 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e   sqlite3_.** fun
39fa0 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73  ction that calls
39fb0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
39fc0 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ize()..**.** {H1
39fd0 37 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65  7011} The sqlite
39fe0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
39ff0 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
3a000 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78  s a new.** mutex
3a010 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70   and returns a p
3a020 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48  ointer to it. {H
3a030 31 37 30 31 32 7d 20 49 66 20 69 74 20 72 65 74  17012} If it ret
3a040 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61  urns NULL.** tha
3a050 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d  t means that a m
3a060 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62  utex could not b
3a070 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31  e allocated. {H1
3a080 37 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20  7013} SQLite.** 
3a090 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20  will unwind its 
3a0a0 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e  stack and return
3a0b0 20 61 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30   an error. {H170
3a0c0 31 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74  14} The argument
3a0d0 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  .** to sqlite3_m
3a0e0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20  utex_alloc() is 
3a0f0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74  one of these int
3a100 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a  eger constants:.
3a110 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
3a120 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
3a130 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _FAST.** <li>  S
3a140 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
3a150 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53  RSIVE.** <li>  S
3a160 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
3a170 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69  IC_MASTER.** <li
3a180 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
3a190 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c  STATIC_MEM.** <l
3a1a0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
3a1b0 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20  _STATIC_MEM2.** 
3a1c0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
3a1d0 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a  EX_STATIC_PRNG.*
3a1e0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
3a1f0 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a  UTEX_STATIC_LRU.
3a200 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
3a210 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
3a220 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  2.** </ul>.**.**
3a230 20 7b 48 31 37 30 31 35 7d 20 54 68 65 20 66 69   {H17015} The fi
3a240 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74  rst two constant
3a250 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f  s cause sqlite3_
3a260 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f  mutex_alloc() to
3a270 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77   create.** a new
3a280 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77   mutex.  The new
3a290 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73   mutex is recurs
3a2a0 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f  ive when SQLITE_
3a2b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a  MUTEX_RECURSIVE.
3a2c0 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e  ** is used but n
3a2d0 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73  ot necessarily s
3a2e0 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  o when SQLITE_MU
3a2f0 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64  TEX_FAST is used
3a300 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d  . {END}.** The m
3a310 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
3a320 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  ion does not nee
3a330 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74  d to make a dist
3a340 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65  inction.** betwe
3a350 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  en SQLITE_MUTEX_
3a360 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51  RECURSIVE and SQ
3a370 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
3a380 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f  if it does.** no
3a390 74 20 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37  t want to.  {H17
3a3a0 30 31 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20  016} But SQLite 
3a3b0 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73  will only reques
3a3c0 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75  t a recursive mu
3a3d0 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20  tex in.** cases 
3a3e0 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20  where it really 
3a3f0 6e 65 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44  needs one.  {END
3a400 7d 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f  } If a faster no
3a410 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  n-recursive mute
3a420 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  x.** implementat
3a430 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ion is available
3a440 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61   on the host pla
3a450 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78  tform, the mutex
3a460 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69   subsystem.** mi
3a470 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20  ght return such 
3a480 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f  a mutex in respo
3a490 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55  nse to SQLITE_MU
3a4a0 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20  TEX_FAST..**.** 
3a4b0 7b 48 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68  {H17017} The oth
3a4c0 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d  er allowed param
3a4d0 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33  eters to sqlite3
3a4e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65  _mutex_alloc() e
3a4f0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ach return.** a 
3a500 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
3a510 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20  tic preexisting 
3a520 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 69  mutex. {END}  Si
3a530 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  x static mutexes
3a540 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20   are.** used by 
3a550 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73  the current vers
3a560 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ion of SQLite.  
3a570 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
3a580 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79  of SQLite.** may
3a590 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
3a5a0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20  static mutexes. 
3a5b0 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   Static mutexes 
3a5c0 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
3a5d0 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
3a5e0 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61  e only.  Applica
3a5f0 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53  tions that use S
3a600 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68  QLite mutexes sh
3a610 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79  ould.** use only
3a620 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
3a630 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  exes returned by
3a640 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
3a650 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  ST or.** SQLITE_
3a660 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e  MUTEX_RECURSIVE.
3a670 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20  .**.** {H17018} 
3a680 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65  Note that if one
3a690 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20   of the dynamic 
3a6a0 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73  mutex parameters
3a6b0 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46   (SQLITE_MUTEX_F
3a6c0 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45  AST.** or SQLITE
3a6d0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
3a6e0 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73  ) is used then s
3a6f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
3a700 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20  oc().** returns 
3a710 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65  a different mute
3a720 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e  x on every call.
3a730 20 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66    {H17034} But f
3a740 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a  or the static.**
3a750 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68   mutex types, th
3a760 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20  e same mutex is 
3a770 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72  returned on ever
3a780 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a  y call that has.
3a790 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65  ** the same type
3a7a0 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b   number..**.** {
3a7b0 48 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69  H17019} The sqli
3a7c0 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29  te3_mutex_free()
3a7d0 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63   routine dealloc
3a7e0 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c  ates a previousl
3a7f0 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64  y.** allocated d
3a800 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48  ynamic mutex. {H
3a810 31 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73  17020} SQLite is
3a820 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c   careful to deal
3a830 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20  locate every.** 
3a840 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68  dynamic mutex th
3a850 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e  at it allocates.
3a860 20 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79   {A17021} The dy
3a870 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75  namic mutexes mu
3a880 73 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20  st not be in.** 
3a890 75 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72  use when they ar
3a8a0 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b  e deallocated. {
3a8b0 41 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69  A17022} Attempti
3a8c0 6e 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65  ng to deallocate
3a8d0 20 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74   a static.** mut
3a8e0 65 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e  ex results in un
3a8f0 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72  defined behavior
3a900 2e 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74  . {H17023} SQLit
3a910 65 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61  e never dealloca
3a920 74 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20  tes.** a static 
3a930 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  mutex. {END}.**.
3a940 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
3a950 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64  utex_enter() and
3a960 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
3a970 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74  ry() routines at
3a980 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65  tempt.** to ente
3a990 72 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30  r a mutex. {H170
3a9a0 32 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74  24} If another t
3a9b0 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79  hread is already
3a9c0 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65   within the mute
3a9d0 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75  x,.** sqlite3_mu
3a9e0 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c  tex_enter() will
3a9f0 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74   block and sqlit
3aa00 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77  e3_mutex_try() w
3aa10 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  ill return.** SQ
3aa20 4c 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30  LITE_BUSY. {H170
3aa30 32 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33  25}  The sqlite3
3aa40 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74  _mutex_try() int
3aa50 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b  erface returns [
3aa60 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70  SQLITE_OK].** up
3aa70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e  on successful en
3aa80 74 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d  try.  {H17026} M
3aa90 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75  utexes created u
3aaa0 73 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  sing.** SQLITE_M
3aab0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63  UTEX_RECURSIVE c
3aac0 61 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75  an be entered mu
3aad0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20  ltiple times by 
3aae0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
3aaf0 0a 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20  .** {H17027} In 
3ab00 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a  such cases the,.
3ab10 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  ** mutex must be
3ab20 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c   exited an equal
3ab30 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
3ab40 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20   before another 
3ab50 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e  thread.** can en
3ab60 74 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49  ter.  {A17028} I
3ab70 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  f the same threa
3ab80 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72  d tries to enter
3ab90 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69   any other.** ki
3aba0 6e 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65  nd of mutex more
3abb0 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20   than once, the 
3abc0 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
3abd0 66 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32  fined..** {H1702
3abe0 39 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  9} SQLite will n
3abf0 65 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20  ever exhibit.** 
3ac00 73 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e  such behavior in
3ac10 20 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20   its own use of 
3ac20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53  mutexes..**.** S
3ac30 6f 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72  ome systems (for
3ac40 20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77   example, Window
3ac50 73 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70  s 95) do not sup
3ac60 70 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69  port the operati
3ac70 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65  on.** implemente
3ac80 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74  d by sqlite3_mut
3ac90 65 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68  ex_try().  On th
3aca0 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c  ose systems, sql
3acb0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
3acc0 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  .** will always 
3acd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
3ace0 53 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68  SY.  {H17030} Th
3acf0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e  e SQLite core on
3ad00 6c 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20  ly ever uses.** 
3ad10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
3ad20 79 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69  y() as an optimi
3ad30 7a 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69  zation so this i
3ad40 73 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68  s acceptable beh
3ad50 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  avior..**.** {H1
3ad60 37 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65  7031} The sqlite
3ad70 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20  3_mutex_leave() 
3ad80 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20  routine exits a 
3ad90 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a  mutex that was.*
3ada0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74  * previously ent
3adb0 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65  ered by the same
3adc0 20 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33   thread.  {A1703
3add0 32 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a  2} The behavior.
3ade0 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  ** is undefined 
3adf0 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20  if the mutex is 
3ae00 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e  not currently en
3ae10 74 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  tered by the.** 
3ae20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f  calling thread o
3ae30 72 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  r is not current
3ae40 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b  ly allocated.  {
3ae50 48 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77  H17033} SQLite w
3ae60 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20  ill.** never do 
3ae70 65 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a  either. {END}.**
3ae80 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d  .** If the argum
3ae90 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ent to sqlite3_m
3aea0 75 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71  utex_enter(), sq
3aeb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
3aec0 29 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33  ), or.** sqlite3
3aed0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69  _mutex_leave() i
3aee0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
3aef0 2c 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65  , then all three
3af00 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68   routines.** beh
3af10 61 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a  ave as no-ops..*
3af20 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
3af30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
3af40 6c 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  ld()] and [sqlit
3af50 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
3af60 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ()]..*/.SQLITE_A
3af70 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  PI sqlite3_mutex
3af80 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   *sqlite3_mutex_
3af90 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49  alloc(int);.SQLI
3afa0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
3afb0 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73  te3_mutex_free(s
3afc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
3afd0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
3afe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
3aff0 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ter(sqlite3_mute
3b000 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  x*);.SQLITE_API 
3b010 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  int sqlite3_mute
3b020 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75  x_try(sqlite3_mu
3b030 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  tex*);.SQLITE_AP
3b040 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d  I void sqlite3_m
3b050 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74  utex_leave(sqlit
3b060 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a  e3_mutex*);../*.
3b070 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74  ** CAPI3REF: Mut
3b080 65 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63  ex Methods Objec
3b090 74 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31  t {H17120} <S201
3b0a0 33 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  30>.** EXPERIMEN
3b0b0 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73  TAL.**.** An ins
3b0c0 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
3b0d0 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20  ructure defines 
3b0e0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f  the low-level ro
3b0f0 75 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74  utines.** used t
3b100 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75  o allocate and u
3b110 73 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a  se mutexes..**.*
3b120 2a 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64  * Usually, the d
3b130 65 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70  efault mutex imp
3b140 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f  lementations pro
3b150 76 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20  vided by SQLite 
3b160 61 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e  are.** sufficien
3b170 74 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75  t, however the u
3b180 73 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69  ser has the opti
3b190 6f 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69  on of substituti
3b1a0 6e 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69  ng a custom.** i
3b1b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  mplementation fo
3b1c0 72 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65  r specialized de
3b1d0 70 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73  ployments or sys
3b1e0 74 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53  tems for which S
3b1f0 51 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f  QLite.** does no
3b200 74 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74  t provide a suit
3b210 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
3b220 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ion. In this cas
3b230 65 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63  e, the user.** c
3b240 72 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c  reates and popul
3b250 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  ates an instance
3b260 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
3b270 72 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f  re to pass.** to
3b280 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
3b290 29 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65  ) along with the
3b2a0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
3b2b0 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a  MUTEX] option..*
3b2c0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20  * Additionally, 
3b2d0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
3b2e0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61  his structure ca
3b2f0 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a  n be used as an.
3b300 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62  ** output variab
3b310 6c 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67  le when querying
3b320 20 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20   the system for 
3b330 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65  the current mute
3b340 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  x.** implementat
3b350 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b  ion, using the [
3b360 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
3b370 54 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a  TMUTEX] option..
3b380 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78  **.** The xMutex
3b390 49 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69  Init method defi
3b3a0 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75  ned by this stru
3b3b0 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64  cture is invoked
3b3c0 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73   as.** part of s
3b3d0 79 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61  ystem initializa
3b3e0 74 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69  tion by the sqli
3b3f0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
3b400 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48   function..** {H
3b410 31 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65  17001} The xMute
3b420 78 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68  xInit routine sh
3b430 61 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79  all be called by
3b440 20 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72   SQLite once for
3b450 20 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69   each.** effecti
3b460 76 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ve call to [sqli
3b470 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
3b480 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75  ]..**.** The xMu
3b490 74 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65  texEnd method de
3b4a0 66 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74  fined by this st
3b4b0 72 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b  ructure is invok
3b4c0 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66  ed as.** part of
3b4d0 20 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e   system shutdown
3b4e0 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f   by the sqlite3_
3b4f0 73 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74  shutdown() funct
3b500 69 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c  ion. The.** impl
3b510 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
3b520 69 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70  is method is exp
3b530 65 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65  ected to release
3b540 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67   all outstanding
3b550 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62  .** resources ob
3b560 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75  tained by the mu
3b570 74 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c  tex methods impl
3b580 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65  ementation, espe
3b590 63 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20  cially.** those 
3b5a0 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20  obtained by the 
3b5b0 78 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f  xMutexInit metho
3b5c0 64 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20  d. {H17003} The 
3b5d0 78 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69  xMutexEnd().** i
3b5e0 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62  nterface shall b
3b5f0 65 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66  e invoked once f
3b600 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20  or each call to 
3b610 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77  [sqlite3_shutdow
3b620 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  n()]..**.** The 
3b630 72 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20  remaining seven 
3b640 6d 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20  methods defined 
3b650 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72  by this structur
3b660 65 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a  e (xMutexAlloc,.
3b670 2a 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78  ** xMutexFree, x
3b680 4d 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74  MutexEnter, xMut
3b690 65 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61  exTry, xMutexLea
3b6a0 76 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61  ve, xMutexHeld a
3b6b0 6e 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68  nd.** xMutexNoth
3b6c0 65 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74  eld) implement t
3b6d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74  he following int
3b6e0 65 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74  erfaces (respect
3b6f0 69 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75  ively):.**.** <u
3b700 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  l>.**   <li>  [s
3b710 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
3b720 6f 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  oc()] </li>.**  
3b730 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
3b740 6d 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f  mutex_free()] </
3b750 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b  li>.**   <li>  [
3b760 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
3b770 74 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20  ter()] </li>.** 
3b780 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33    <li>  [sqlite3
3b790 5f 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f  _mutex_try()] </
3b7a0 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b  li>.**   <li>  [
3b7b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3b7c0 61 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20  ave()] </li>.** 
3b7d0 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33    <li>  [sqlite3
3b7e0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c  _mutex_held()] <
3b7f0 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20  /li>.**   <li>  
3b800 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e  [sqlite3_mutex_n
3b810 6f 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a  otheld()] </li>.
3b820 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
3b830 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e  he only differen
3b840 63 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70  ce is that the p
3b850 75 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58  ublic sqlite3_XX
3b860 58 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d  X functions enum
3b870 65 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20  erated.** above 
3b880 73 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20  silently ignore 
3b890 61 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  any invocations 
3b8a0 74 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c  that pass a NULL
3b8b0 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64   pointer instead
3b8c0 0a 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d  .** of a valid m
3b8d0 75 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65  utex handle. The
3b8e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
3b8f0 20 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20   of the methods 
3b900 64 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68  defined.** by th
3b910 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
3b920 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f   not required to
3b930 20 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73   handle this cas
3b940 65 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a  e, the results.*
3b950 2a 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e  * of passing a N
3b960 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74  ULL pointer inst
3b970 65 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d  ead of a valid m
3b980 75 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20  utex handle are 
3b990 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e  undefined.** (i.
3b9a0 65 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61  e. it is accepta
3b9b0 62 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61  ble to provide a
3b9c0 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
3b9d0 20 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20   that segfaults 
3b9e0 69 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73  if.** it is pass
3b9f0 65 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ed a NULL pointe
3ba00 72 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d  r)..**.** The xM
3ba10 75 74 65 78 49 6e 69 74 28 29 20 6d 65 74 68 6f  utexInit() metho
3ba20 64 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 64  d must be thread
3ba30 73 61 66 65 2e 20 20 49 74 20 6d 75 73 74 20 62  safe.  It must b
3ba40 65 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a  e harmless to.**
3ba50 20 69 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 6e   invoke xMutexIn
3ba60 69 74 28 29 20 6d 75 74 69 70 6c 65 20 74 69 6d  it() mutiple tim
3ba70 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61  es within the sa
3ba80 6d 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20 77  me process and w
3ba90 69 74 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 76  ithout.** interv
3baa0 65 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 78  ening calls to x
3bab0 4d 75 74 65 78 45 6e 64 28 29 2e 20 20 53 65 63  MutexEnd().  Sec
3bac0 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65  ond and subseque
3bad0 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 78  nt calls to.** x
3bae0 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74  MutexInit() must
3baf0 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a   be no-ops..**.*
3bb00 2a 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d  * xMutexInit() m
3bb10 75 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c 69  ust not use SQLi
3bb20 74 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  te memory alloca
3bb30 74 69 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f 6d  tion ([sqlite3_m
3bb40 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 20  alloc()].** and 
3bb50 69 74 73 20 61 73 73 6f 63 69 61 74 65 73 29 2e  its associates).
3bb60 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d 75    Similarly, xMu
3bb70 74 65 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 20  texAlloc() must 
3bb80 6e 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d  not use SQLite m
3bb90 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
3bba0 69 6f 6e 20 66 6f 72 20 61 20 73 74 61 74 69 63  ion for a static
3bbb0 20 6d 75 74 65 78 2e 20 20 48 6f 77 65 76 65 72   mutex.  However
3bbc0 20 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d   xMutexAlloc() m
3bbd0 61 79 20 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a  ay use SQLite.**
3bbe0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
3bbf0 6f 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72  on for a fast or
3bc00 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
3bc10 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77  ..**.** SQLite w
3bc20 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78  ill invoke the x
3bc30 4d 75 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f  MutexEnd() metho
3bc40 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f  d when [sqlite3_
3bc50 73 68 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a  shutdown()] is.*
3bc60 2a 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e  * called, but on
3bc70 6c 79 20 69 66 20 74 68 65 20 70 72 69 6f 72 20  ly if the prior 
3bc80 63 61 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e  call to xMutexIn
3bc90 69 74 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49  it returned SQLI
3bca0 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75  TE_OK..** If xMu
3bcb0 74 65 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e  texInit fails in
3bcc0 20 61 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20   any way, it is 
3bcd0 65 78 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61  expected to clea
3bce0 6e 20 75 70 20 61 66 74 65 72 20 69 74 73 65 6c  n up after itsel
3bcf0 66 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65  f.** prior to re
3bd00 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65  turning..*/.type
3bd10 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
3bd20 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
3bd30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
3bd40 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73  ethods;.struct s
3bd50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
3bd60 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78  hods {.  int (*x
3bd70 4d 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29  MutexInit)(void)
3bd80 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78  ;.  int (*xMutex
3bd90 45 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71  End)(void);.  sq
3bda0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78  lite3_mutex *(*x
3bdb0 4d 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29  MutexAlloc)(int)
3bdc0 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65  ;.  void (*xMute
3bdd0 78 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d  xFree)(sqlite3_m
3bde0 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20  utex *);.  void 
3bdf0 28 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73  (*xMutexEnter)(s
3be00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b  qlite3_mutex *);
3be10 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54  .  int (*xMutexT
3be20 72 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ry)(sqlite3_mute
3be30 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  x *);.  void (*x
3be40 4d 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69  MutexLeave)(sqli
3be50 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20  te3_mutex *);.  
3be60 69 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64  int (*xMutexHeld
3be70 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  )(sqlite3_mutex 
3be80 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74  *);.  int (*xMut
3be90 65 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74  exNotheld)(sqlit
3bea0 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a  e3_mutex *);.};.
3beb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3bec0 20 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 74   Mutex Verificat
3bed0 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31  ion Routines {H1
3bee0 37 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c  7080} <S20130> <
3bef0 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30800>.**.** Th
3bf00 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
3bf10 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74  held() and sqlit
3bf20 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
3bf30 28 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61  () routines.** a
3bf40 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  re intended for 
3bf50 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72  use inside asser
3bf60 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  t() statements. 
3bf70 7b 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c  {H17081} The SQL
3bf80 69 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65  ite core.** neve
3bf90 72 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75  r uses these rou
3bfa0 74 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73  tines except ins
3bfb0 69 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20  ide an assert() 
3bfc0 61 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  and applications
3bfd0 0a 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20  .** are advised 
3bfe0 74 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65  to follow the le
3bff0 61 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20  ad of the core. 
3c000 20 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f   {H17082} The co
3c010 72 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69  re only.** provi
3c020 64 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  des implementati
3c030 6f 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f  ons for these ro
3c040 75 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69  utines when it i
3c050 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69  s compiled.** wi
3c060 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45  th the SQLITE_DE
3c070 42 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30  BUG flag.  {A170
3c080 38 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74  87} External mut
3c090 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
3c0a0 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72  ns.** are only r
3c0b0 65 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69  equired to provi
3c0c0 64 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  de these routine
3c0d0 73 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55  s if SQLITE_DEBU
3c0e0 47 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20  G is.** defined 
3c0f0 61 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73  and if NDEBUG is
3c100 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a   not defined..**
3c110 0a 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65  .** {H17083} The
3c120 73 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75  se routines shou
3c130 6c 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69  ld return true i
3c140 66 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74  f the mutex in t
3c150 68 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  heir argument.**
3c160 20 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20   is held or not 
3c170 68 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65  held, respective
3c180 6c 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69  ly, by the calli
3c190 6e 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  ng thread..**.**
3c1a0 20 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d   {X17084} The im
3c1b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
3c1c0 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
3c1d0 70 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e  provided version
3c1e0 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f  s of these.** ro
3c1f0 75 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 75  utines that actu
3c200 61 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68  ally work. If th
3c210 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3c220 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64   does not provid
3c230 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72  e working.** ver
3c240 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72  sions of these r
3c250 6f 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75  outines, it shou
3c260 6c 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76  ld at least prov
3c270 69 64 65 20 73 74 75 62 73 20 74 68 61 74 20 61  ide stubs that a
3c280 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20  lways.** return 
3c290 74 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65  true so that one
3c2a0 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70   does not get sp
3c2b0 75 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e  urious assertion
3c2c0 20 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a   failures..**.**
3c2d0 20 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 65   {H17085} If the
3c2e0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
3c2f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
3c300 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  ) is a NULL poin
3c310 74 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ter then.** the 
3c320 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72  routine should r
3c330 65 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20  eturn 1.  {END} 
3c340 54 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74  This seems count
3c350 65 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e  er-intuitive sin
3c360 63 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68  ce.** clearly th
3c370 65 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62  e mutex cannot b
3c380 65 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65  e held if it doe
3c390 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75  s not exist.  Bu
3c3a0 74 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61  t the.** the rea
3c3b0 73 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f  son the mutex do
3c3c0 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20  es not exist is 
3c3d0 62 65 63 61 75 73 65 20 74 68 65 20 62 75 69 6c  because the buil
3c3e0 64 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e  d is not.** usin
3c3f0 67 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20  g mutexes.  And 
3c400 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  we do not want t
3c410 68 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74  he assert() cont
3c420 61 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61  aining the.** ca
3c430 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  ll to sqlite3_mu
3c440 74 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61  tex_held() to fa
3c450 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72  il, so a non-zer
3c460 6f 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74  o return is.** t
3c470 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 74  he appropriate t
3c480 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31  hing to do.  {H1
3c490 37 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65  7086} The sqlite
3c4a0 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
3c4b0 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73  ).** interface s
3c4c0 68 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72  hould also retur
3c4d0 6e 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61  n 1 when given a
3c4e0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
3c4f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
3c500 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
3c510 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
3c520 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  x*);.SQLITE_API 
3c530 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  int sqlite3_mute
3c540 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65  x_notheld(sqlite
3c550 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a  3_mutex*);../*.*
3c560 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
3c570 78 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d  x Types {H17001}
3c580 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20   <H17000>.**.** 
3c590 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  The [sqlite3_mut
3c5a0 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65  ex_alloc()] inte
3c5b0 72 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 69  rface takes a si
3c5c0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  ngle argument.**
3c5d0 20 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66   which is one of
3c5e0 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63   these integer c
3c5f0 6f 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  onstants..**.** 
3c600 54 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 69  The set of stati
3c610 63 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68  c mutexes may ch
3c620 61 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51  ange from one SQ
3c630 4c 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20  Lite release to 
3c640 74 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70  the.** next.  Ap
3c650 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20  plications that 
3c660 6f 76 65 72 72 69 64 65 20 74 68 65 20 62 75 69  override the bui
3c670 6c 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69  lt-in mutex logi
3c680 63 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65  c must be.** pre
3c690 70 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f  pared to accommo
3c6a0 64 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20  date additional 
3c6b0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a  static mutexes..
3c6c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3c6d0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20  E_MUTEX_FAST    
3c6e0 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69           0.#defi
3c6f0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
3c700 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20  RECURSIVE       
3c710 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
3c720 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
3c730 41 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 69  ASTER    2.#defi
3c740 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
3c750 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20  STATIC_MEM      
3c760 20 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d   3  /* sqlite3_m
3c770 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69  alloc() */.#defi
3c780 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
3c790 53 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20  STATIC_MEM2     
3c7a0 20 34 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20   4  /* NOT USED 
3c7b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3c7c0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f  E_MUTEX_STATIC_O
3c7d0 50 45 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 73  PEN      4  /* s
3c7e0 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28  qlite3BtreeOpen(
3c7f0 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ) */.#define SQL
3c800 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
3c810 5f 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a  _PRNG      5  /*
3c820 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28   sqlite3_random(
3c830 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ) */.#define SQL
3c840 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
3c850 5f 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a  _LRU       6  /*
3c860 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a   lru page list *
3c870 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3c880 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
3c890 55 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72  U2      7  /* lr
3c8a0 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a  u page list */..
3c8b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3c8c0 52 65 74 72 69 65 76 65 20 74 68 65 20 6d 75 74  Retrieve the mut
3c8d0 65 78 20 66 6f 72 20 61 20 64 61 74 61 62 61 73  ex for a databas
3c8e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31  e connection {H1
3c8f0 37 30 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a  7002} <H17000>.*
3c900 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
3c910 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ace returns a po
3c920 69 6e 74 65 72 20 74 68 65 20 5b 73 71 6c 69 74  inter the [sqlit
3c930 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74  e3_mutex] object
3c940 20 74 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c   that .** serial
3c950 69 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20 74  izes access to t
3c960 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
3c970 6e 65 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69  nection] given i
3c980 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a  n the argument.*
3c990 2a 20 77 68 65 6e 20 74 68 65 20 5b 74 68 72 65  * when the [thre
3c9a0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53  ading mode] is S
3c9b0 65 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66  erialized..** If
3c9c0 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20   the [threading 
3c9d0 6d 6f 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d  mode] is Single-
3c9e0 74 68 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d  thread or Multi-
3c9f0 74 68 72 65 61 64 20 74 68 65 6e 20 74 68 69 73  thread then this
3ca00 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75  .** routine retu
3ca10 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  rns a NULL point
3ca20 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  er..*/.SQLITE_AP
3ca30 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
3ca40 2a 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65  *sqlite3_db_mute
3ca50 78 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  x(sqlite3*);../*
3ca60 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f  .** CAPI3REF: Lo
3ca70 77 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20  w-Level Control 
3ca80 4f 66 20 44 61 74 61 62 61 73 65 20 46 69 6c 65  Of Database File
3ca90 73 20 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 38  s {H11300} <S308
3caa0 30 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30  00>.**.** {H1130
3cab0 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  1} The [sqlite3_
3cac0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20  file_control()] 
3cad0 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20  interface makes 
3cae0 61 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f  a direct call to
3caf0 20 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e   the.** xFileCon
3cb00 74 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20  trol method for 
3cb10 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
3cb20 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
3cb30 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
3cb40 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  th a particular 
3cb50 64 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 66  database identif
3cb60 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e  ied by the secon
3cb70 64 20 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31  d argument. {H11
3cb80 33 30 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65  302} The.** name
3cb90 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
3cba0 20 69 73 20 74 68 65 20 6e 61 6d 65 20 61 73 73   is the name ass
3cbb0 69 67 6e 65 64 20 74 6f 20 74 68 65 20 64 61 74  igned to the dat
3cbc0 61 62 61 73 65 20 62 79 20 74 68 65 0a 2a 2a 20  abase by the.** 
3cbd0 3c 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74  <a href="lang_at
3cbe0 74 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43  tach.html">ATTAC
3cbf0 48 3c 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e  H</a> SQL comman
3cc00 64 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68  d that opened th
3cc10 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 7b  e.** database. {
3cc20 48 31 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72  H11303} To contr
3cc30 6f 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ol the main data
3cc40 62 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74  base file, use t
3cc50 68 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a  he name "main".*
3cc60 2a 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  * or a NULL poin
3cc70 74 65 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 68  ter. {H11304} Th
3cc80 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  e third and four
3cc90 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
3cca0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
3ccb0 20 61 72 65 20 70 61 73 73 65 64 20 64 69 72 65   are passed dire
3ccc0 63 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20  ctly through to 
3ccd0 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74  the second and t
3cce0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 73 20  hird parameters 
3ccf0 6f 66 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43  of.** the xFileC
3cd00 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20  ontrol method.  
3cd10 7b 48 31 31 33 30 35 7d 20 54 68 65 20 72 65 74  {H11305} The ret
3cd20 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65  urn value of the
3cd30 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a   xFileControl.**
3cd40 20 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20   method becomes 
3cd50 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
3cd60 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
3cd70 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d  ..**.** {H11306}
3cd80 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70   If the second p
3cd90 61 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d  arameter (zDbNam
3cda0 65 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63  e) does not matc
3cdb0 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e  h the name of an
3cdc0 79 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61  y.** open databa
3cdd0 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51  se file, then SQ
3cde0 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65  LITE_ERROR is re
3cdf0 74 75 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d  turned. {H11307}
3ce00 20 54 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63   This error.** c
3ce10 6f 64 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d  ode is not remem
3ce20 62 65 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e  bered and will n
3ce30 6f 74 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62  ot be recalled b
3ce40 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  y [sqlite3_errco
3ce50 64 65 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c  de()].** or [sql
3ce60 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20  ite3_errmsg()]. 
3ce70 7b 41 31 31 33 30 38 7d 20 54 68 65 20 75 6e 64  {A11308} The und
3ce80 65 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e  erlying xFileCon
3ce90 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68  trol method migh
3cea0 74 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e  t.** also return
3ceb0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20   SQLITE_ERROR.  
3cec0 7b 41 31 31 33 30 39 7d 20 54 68 65 72 65 20 69  {A11309} There i
3ced0 73 20 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 74  s no way to dist
3cee0 69 6e 67 75 69 73 68 20 62 65 74 77 65 65 6e 0a  inguish between.
3cef0 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20  ** an incorrect 
3cf00 7a 44 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53  zDbName and an S
3cf10 51 4c 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75  QLITE_ERROR retu
3cf20 72 6e 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65  rn from the unde
3cf30 72 6c 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43  rlying.** xFileC
3cf40 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b  ontrol method. {
3cf50 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  END}.**.** See a
3cf60 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e  lso: [SQLITE_FCN
3cf70 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f  TL_LOCKSTATE].*/
3cf80 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3cf90 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
3cfa0 74 72 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63  trol(sqlite3*, c
3cfb0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61  onst char *zDbNa
3cfc0 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  me, int op, void
3cfd0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
3cfe0 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74  REF: Testing Int
3cff0 65 72 66 61 63 65 20 7b 48 31 31 34 30 30 7d 20  erface {H11400} 
3d000 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30800>.**.** T
3d010 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  he sqlite3_test_
3d020 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66  control() interf
3d030 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72  ace is used to r
3d040 65 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c  ead out internal
3d050 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c  .** state of SQL
3d060 69 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63  ite and to injec
3d070 74 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51  t faults into SQ
3d080 4c 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67  Lite for testing
3d090 0a 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54  .** purposes.  T
3d0a0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
3d0b0 65 72 20 69 73 20 61 6e 20 6f 70 65 72 61 74 69  er is an operati
3d0c0 6f 6e 20 63 6f 64 65 20 74 68 61 74 20 64 65 74  on code that det
3d0d0 65 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e  ermines.** the n
3d0e0 75 6d 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20  umber, meaning, 
3d0f0 61 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  and operation of
3d100 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20   all subsequent 
3d110 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a  parameters..**.*
3d120 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
3d130 20 69 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 20   is not for use 
3d140 62 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e  by applications.
3d150 20 20 49 74 20 65 78 69 73 74 73 20 73 6f 6c 65    It exists sole
3d160 6c 79 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 79  ly.** for verify
3d170 69 6e 67 20 74 68 65 20 63 6f 72 72 65 63 74 20  ing the correct 
3d180 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
3d190 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
3d1a0 20 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f    Depending.** o
3d1b0 6e 20 68 6f 77 20 74 68 65 20 53 51 4c 69 74 65  n how the SQLite
3d1c0 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70   library is comp
3d1d0 69 6c 65 64 2c 20 74 68 69 73 20 69 6e 74 65 72  iled, this inter
3d1e0 66 61 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 65  face might not e
3d1f0 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  xist..**.** The 
3d200 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 6f  details of the o
3d210 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20  peration codes, 
3d220 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20  their meanings, 
3d230 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a  the parameters.*
3d240 2a 20 74 68 65 79 20 74 61 6b 65 2c 20 61 6e 64  * they take, and
3d250 20 77 68 61 74 20 74 68 65 79 20 64 6f 20 61 72   what they do ar
3d260 65 20 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f  e all subject to
3d270 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20   change without 
3d280 6e 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b  notice..** Unlik
3d290 65 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 51  e most of the SQ
3d2a0 4c 69 74 65 20 41 50 49 2c 20 74 68 69 73 20 66  Lite API, this f
3d2b0 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67  unction is not g
3d2c0 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20  uaranteed to.** 
3d2d0 6f 70 65 72 61 74 65 20 63 6f 6e 73 69 73 74 65  operate consiste
3d2e0 6e 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65  ntly from one re
3d2f0 6c 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78  lease to the nex
3d300 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
3d310 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73   int sqlite3_tes
3d320 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70  t_control(int op
3d330 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  , ...);../*.** C
3d340 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67  API3REF: Testing
3d350 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72 61   Interface Opera
3d360 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 34  tion Codes {H114
3d370 31 30 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a  10} <H11400>.**.
3d380 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e  ** These constan
3d390 74 73 20 61 72 65 20 74 68 65 20 76 61 6c 69 64  ts are the valid
3d3a0 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20   operation code 
3d3b0 70 61 72 61 6d 65 74 65 72 73 20 75 73 65 64 0a  parameters used.
3d3c0 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ** as the first 
3d3d0 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c  argument to [sql
3d3e0 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
3d3f0 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  l()]..**.** Thes
3d400 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64  e parameters and
3d410 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 20   their meanings 
3d420 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63  are subject to c
3d430 68 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74  hange.** without
3d440 20 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65 20   notice.  These 
3d450 76 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20 74  values are for t
3d460 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20  esting purposes 
3d470 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61  only..** Applica
3d480 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74  tions should not
3d490 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73   use any of thes
3d4a0 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20  e parameters or 
3d4b0 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  the.** [sqlite3_
3d4c0 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20  test_control()] 
3d4d0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64  interface..*/.#d
3d4e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
3d4f0 54 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20  TCTRL_PRNG_SAVE 
3d500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
3d510 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3d520 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45  TESTCTRL_PRNG_RE
3d530 53 54 4f 52 45 20 20 20 20 20 20 20 20 20 20 20  STORE           
3d540 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    6.#define SQLI
3d550 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47  TE_TESTCTRL_PRNG
3d560 5f 52 45 53 45 54 20 20 20 20 20 20 20 20 20 20  _RESET          
3d570 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53       7.#define S
3d580 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42  QLITE_TESTCTRL_B
3d590 49 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 20  ITVEC_TEST      
3d5a0 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e          8.#defin
3d5b0 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
3d5c0 4c 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20  L_FAULT_INSTALL 
3d5d0 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65             9.#de
3d5e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54  fine SQLITE_TEST
3d5f0 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c  CTRL_BENIGN_MALL
3d600 4f 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a  OC_HOOKS     10.
3d610 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
3d620 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f  ESTCTRL_PENDING_
3d630 42 59 54 45 20 20 20 20 20 20 20 20 20 20 20 20  BYTE            
3d640 31 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  11.#define SQLIT
3d650 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52  E_TESTCTRL_ASSER
3d660 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
3d670 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51     12.#define SQ
3d680 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c  LITE_TESTCTRL_AL
3d690 57 41 59 53 20 20 20 20 20 20 20 20 20 20 20 20  WAYS            
3d6a0 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65        13.#define
3d6b0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3d6c0 5f 52 45 53 45 52 56 45 20 20 20 20 20 20 20 20  _RESERVE        
3d6d0 20 20 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 0a           14../*.
3d6e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c  ** CAPI3REF: SQL
3d6f0 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 74  ite Runtime Stat
3d700 75 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 30  us {H17200} <S60
3d710 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  200>.** EXPERIME
3d720 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  NTAL.**.** This 
3d730 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65  interface is use
3d740 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 75  d to retrieve ru
3d750 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66  ntime status inf
3d760 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75  ormation.** abou
3d770 74 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e 63  t the preformanc
3d780 65 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e 64  e of SQLite, and
3d790 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72   optionally to r
3d7a0 65 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20  eset various.** 
3d7b0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 2e  highwater marks.
3d7c0 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75    The first argu
3d7d0 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67  ment is an integ
3d7e0 65 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74  er code for.** t
3d7f0 68 65 20 73 70 65 63 69 66 69 63 20 70 61 72 61  he specific para
3d800 6d 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 65  meter to measure
3d810 2e 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e  .  Recognized in
3d820 74 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61  teger codes.** a
3d830 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b  re of the form [
3d840 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
3d850 4d 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c 49  MORY_USED | SQLI
3d860 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a  TE_STATUS_...]..
3d870 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76  ** The current v
3d880 61 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 61  alue of the para
3d890 6d 65 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  meter is returne
3d8a0 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74  d into *pCurrent
3d8b0 2e 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 74  ..** The highest
3d8c0 20 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 20   recorded value 
3d8d0 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a  is returned in *
3d8e0 70 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 20  pHighwater.  If 
3d8f0 74 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67  the.** resetFlag
3d900 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
3d910 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f 72  he highest recor
3d920 64 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 74  d value is reset
3d930 20 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68   after.** *pHigh
3d940 77 61 74 65 72 20 69 73 20 77 72 69 74 74 65 6e  water is written
3d950 2e 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 72  . Some parameter
3d960 73 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20  s do not record 
3d970 74 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 76  the highest.** v
3d980 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65  alue.  For those
3d990 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e   parameters.** n
3d9a0 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65  othing is writte
3d9b0 6e 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74  n into *pHighwat
3d9c0 65 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 74  er and the reset
3d9d0 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e  Flag is ignored.
3d9e0 0a 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d 65  .** Other parame
3d9f0 74 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79  ters record only
3da00 20 74 68 65 20 68 69 67 68 77 61 74 65 72 20 6d   the highwater m
3da10 61 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 20  ark and not the 
3da20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65  current.** value
3da30 2e 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 74  .  For these lat
3da40 74 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 6e  ter parameters n
3da50 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65  othing is writte
3da60 6e 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74  n into *pCurrent
3da70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
3da80 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c  tine returns SQL
3da90 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
3daa0 73 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f  s and a non-zero
3dab0 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d  .** [error code]
3dac0 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a   on failure..**.
3dad0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3dae0 69 73 20 74 68 72 65 61 64 73 61 66 65 20 62 75  is threadsafe bu
3daf0 74 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e  t is not atomic.
3db00 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63    This routine c
3db10 61 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20  an be.** called 
3db20 77 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65  while other thre
3db30 61 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20  ads are running 
3db40 74 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66  the same or diff
3db50 65 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20  erent SQLite.** 
3db60 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77  interfaces.  How
3db70 65 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20  ever the values 
3db80 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75  returned in *pCu
3db90 72 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48  rrent and.** *pH
3dba0 69 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74  ighwater reflect
3dbb0 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53   the status of S
3dbc0 51 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65  QLite at differe
3dbd0 6e 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d  nt points in tim
3dbe0 65 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70  e.** and it is p
3dbf0 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f  ossible that ano
3dc00 74 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 68  ther thread migh
3dc10 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 72  t change the par
3dc20 61 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74  ameter.** in bet
3dc30 77 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77  ween the times w
3dc40 68 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e  hen *pCurrent an
3dc50 64 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 72  d *pHighwater ar
3dc60 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a  e written..**.**
3dc70 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
3dc80 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d  te3_db_status()]
3dc90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
3dca0 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
3dcb0 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  AL int sqlite3_s
3dcc0 74 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e  tatus(int op, in
3dcd0 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74  t *pCurrent, int
3dce0 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e   *pHighwater, in
3dcf0 74 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a  t resetFlag);...
3dd00 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3dd10 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72  Status Parameter
3dd20 73 20 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32  s {H17250} <H172
3dd30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
3dd40 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  TAL.**.** These 
3dd50 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
3dd60 73 20 64 65 73 69 67 6e 61 74 65 20 76 61 72 69  s designate vari
3dd70 6f 75 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61  ous run-time sta
3dd80 74 75 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a  tus parameters.*
3dd90 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 72 65  * that can be re
3dda0 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74  turned by [sqlit
3ddb0 65 33 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a  e3_status()]..**
3ddc0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e  .** <dl>.** <dt>
3ddd0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
3dde0 4d 4f 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a  MORY_USED</dt>.*
3ddf0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
3de00 65 74 65 72 20 69 73 20 74 68 65 20 63 75 72 72  eter is the curr
3de10 65 6e 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  ent amount of me
3de20 6d 6f 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74  mory checked out
3de30 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  .** using [sqlit
3de40 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69  e3_malloc()], ei
3de50 74 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72  ther directly or
3de60 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68   indirectly.  Th
3de70 65 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c  e.** figure incl
3de80 75 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20  udes calls made 
3de90 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  to [sqlite3_mall
3dea0 6f 63 28 29 5d 20 62 79 20 74 68 65 20 61 70 70  oc()] by the app
3deb0 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20  lication.** and 
3dec0 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20  internal memory 
3ded0 75 73 61 67 65 20 62 79 20 74 68 65 20 53 51 4c  usage by the SQL
3dee0 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63  ite library.  Sc
3def0 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ratch memory.** 
3df00 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53  controlled by [S
3df10 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52  QLITE_CONFIG_SCR
3df20 41 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69  ATCH] and auxili
3df30 61 72 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a  ary page-cache.*
3df40 2a 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c  * memory control
3df50 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43  led by [SQLITE_C
3df60 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d  ONFIG_PAGECACHE]
3df70 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64   is not included
3df80 20 69 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61   in.** this para
3df90 6d 65 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75  meter.  The amou
3dfa0 6e 74 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  nt returned is t
3dfb0 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c  he sum of the al
3dfc0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65  location.** size
3dfd0 73 20 61 73 20 72 65 70 6f 72 74 65 64 20 62 79  s as reported by
3dfe0 20 74 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f   the xSize metho
3dff0 64 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65  d in [sqlite3_me
3e000 6d 5f 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e  m_methods].</dd>
3e010 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
3e020 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f  E_STATUS_MALLOC_
3e030 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SIZE</dt>.** <dd
3e040 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3e050 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67  records the larg
3e060 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  est memory alloc
3e070 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a  ation request.**
3e080 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69   handed to [sqli
3e090 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72  te3_malloc()] or
3e0a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
3e0b0 63 28 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a  c()] (or their.*
3e0c0 2a 20 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76  * internal equiv
3e0d0 61 6c 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74  alents).  Only t
3e0e0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
3e0f0 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69  d in the.** *pHi
3e100 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65  ghwater paramete
3e110 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  r to [sqlite3_st
3e120 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e  atus()] is of in
3e130 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65  terest.  .** The
3e140 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69   value written i
3e150 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e  nto the *pCurren
3e160 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75  t parameter is u
3e170 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a  ndefined.</dd>.*
3e180 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3e190 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
3e1a0 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  _USED</dt>.** <d
3e1b0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3e1c0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
3e1d0 62 65 72 20 6f 66 20 70 61 67 65 73 20 75 73 65  ber of pages use
3e1e0 64 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20  d out of the.** 
3e1f0 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72  [pagecache memor
3e200 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61  y allocator] tha
3e210 74 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64  t was configured
3e220 20 75 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49   using .** [SQLI
3e230 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
3e240 43 48 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61  CHE].  The.** va
3e250 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
3e260 69 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e  in pages, not in
3e270 20 62 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a   bytes.</dd>.**.
3e280 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
3e290 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f  ATUS_PAGECACHE_O
3e2a0 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20  VERFLOW</dt>.** 
3e2b0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
3e2c0 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  er returns the n
3e2d0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
3e2e0 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20  f page cache.** 
3e2f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68  allocation which
3e300 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74   could not be st
3e310 61 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20  atisfied by the 
3e320 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  [SQLITE_CONFIG_P
3e330 41 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66  AGECACHE].** buf
3e340 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f  fer and where fo
3e350 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77  rced to overflow
3e360 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   to [sqlite3_mal
3e370 6c 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20  loc()].  The.** 
3e380 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
3e390 6e 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69  ncludes allocati
3e3a0 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f  ons that overflo
3e3b0 77 65 64 20 62 65 63 61 75 73 65 20 74 68 65 79  wed because they
3e3c0 0a 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61  .** where too la
3e3d0 72 67 65 20 28 74 68 65 79 20 77 65 72 65 20 6c  rge (they were l
3e3e0 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 22  arger than the "
3e3f0 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f  sz" parameter to
3e400 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  .** [SQLITE_CONF
3e410 49 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61  IG_PAGECACHE]) a
3e420 6e 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74  nd allocations t
3e430 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62  hat overflowed b
3e440 65 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61  ecause.** no spa
3e450 63 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74  ce was left in t
3e460 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f  he page cache.</
3e470 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
3e480 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
3e490 43 41 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a  CACHE_SIZE</dt>.
3e4a0 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
3e4b0 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68  meter records th
3e4c0 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79  e largest memory
3e4d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
3e4e0 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f  est.** handed to
3e4f0 20 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f   [pagecache memo
3e500 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20  ry allocator].  
3e510 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72  Only the value r
3e520 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a  eturned in the.*
3e530 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61  * *pHighwater pa
3e540 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
3e550 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73  te3_status()] is
3e560 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a   of interest.  .
3e570 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 69  ** The value wri
3e580 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70  tten into the *p
3e590 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65  Current paramete
3e5a0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c  r is undefined.<
3e5b0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
3e5c0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
3e5d0 41 54 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a  ATCH_USED</dt>.*
3e5e0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
3e5f0 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65  eter returns the
3e600 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   number of alloc
3e610 61 74 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20  ations used out 
3e620 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74  of the.** [scrat
3e630 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ch memory alloca
3e640 74 6f 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20  tor] configured 
3e650 75 73 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45  using.** [SQLITE
3e660 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d  _CONFIG_SCRATCH]
3e670 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  .  The value ret
3e680 75 72 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f  urned is in allo
3e690 63 61 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20  cations, not.** 
3e6a0 69 6e 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65  in bytes.  Since
3e6b0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64   a single thread
3e6c0 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f   may only have o
3e6d0 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63  ne scratch alloc
3e6e0 61 74 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e  ation.** outstan
3e6f0 64 69 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68  ding at time, th
3e700 69 73 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73  is parameter als
3e710 6f 20 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75  o reports the nu
3e720 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a  mber of threads.
3e730 2a 2a 20 75 73 69 6e 67 20 73 63 72 61 74 63 68  ** using scratch
3e740 20 6d 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73   memory at the s
3e750 61 6d 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a  ame time.</dd>.*
3e760 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3e770 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f  STATUS_SCRATCH_O
3e780 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20  VERFLOW</dt>.** 
3e790 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
3e7a0 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  er returns the n
3e7b0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
3e7c0 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  f scratch memory
3e7d0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77  .** allocation w
3e7e0 68 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62  hich could not b
3e7f0 65 20 73 74 61 74 69 73 66 69 65 64 20 62 79 20  e statisfied by 
3e800 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  the [SQLITE_CONF
3e810 49 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62  IG_SCRATCH].** b
3e820 75 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20  uffer and where 
3e830 66 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c  forced to overfl
3e840 6f 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d  ow to [sqlite3_m
3e850 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76  alloc()].  The v
3e860 61 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65  alues.** returne
3e870 64 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c  d include overfl
3e880 6f 77 73 20 62 65 63 61 75 73 65 20 74 68 65 20  ows because the 
3e890 72 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61  requested alloca
3e8a0 74 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20  tion was too.** 
3e8b0 6c 61 72 67 65 72 20 28 74 68 61 74 20 69 73 2c  larger (that is,
3e8c0 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71   because the req
3e8d0 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f  uested allocatio
3e8e0 6e 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 61  n was larger tha
3e8f0 6e 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61  n the.** "sz" pa
3e900 72 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49  rameter to [SQLI
3e910 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43  TE_CONFIG_SCRATC
3e920 48 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 20  H]) and because 
3e930 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65  no scratch buffe
3e940 72 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20  r.** slots were 
3e950 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f  available..** </
3e960 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
3e970 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
3e980 54 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a  TCH_SIZE</dt>.**
3e990 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3e9a0 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20  ter records the 
3e9b0 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61  largest memory a
3e9c0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
3e9d0 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b  t.** handed to [
3e9e0 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61  scratch memory a
3e9f0 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79  llocator].  Only
3ea00 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
3ea10 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70  ned in the.** *p
3ea20 48 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65  Highwater parame
3ea30 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ter to [sqlite3_
3ea40 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20  status()] is of 
3ea50 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54  interest.  .** T
3ea60 68 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e  he value written
3ea70 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72   into the *pCurr
3ea80 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  ent parameter is
3ea90 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e   undefined.</dd>
3eaa0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
3eab0 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f  E_STATUS_PARSER_
3eac0 53 54 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  STACK</dt>.** <d
3ead0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3eae0 20 72 65 63 6f 72 64 73 20 74 68 65 20 64 65 65   records the dee
3eaf0 70 65 73 74 20 70 61 72 73 65 72 20 73 74 61 63  pest parser stac
3eb00 6b 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a  k.  It is only.*
3eb10 2a 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20  * meaningful if 
3eb20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
3eb30 65 64 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b  ed with [YYTRACK
3eb40 4d 41 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c  MAXSTACKDEPTH].<
3eb50 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a  /dd>.** </dl>.**
3eb60 0a 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70  .** New status p
3eb70 61 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65  arameters may be
3eb80 20 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65   added from time
3eb90 20 74 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65   to time..*/.#de
3eba0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
3ebb0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20  US_MEMORY_USED  
3ebc0 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e          0.#defin
3ebd0 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e SQLITE_STATUS_
3ebe0 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 20 20  PAGECACHE_USED  
3ebf0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53       1.#define S
3ec00 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
3ec10 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20  ECACHE_OVERFLOW 
3ec20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
3ec30 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
3ec40 48 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 33  H_USED         3
3ec50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3ec60 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f  STATUS_SCRATCH_O
3ec70 56 45 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64  VERFLOW     4.#d
3ec80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
3ec90 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20  TUS_MALLOC_SIZE 
3eca0 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69           5.#defi
3ecb0 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ne SQLITE_STATUS
3ecc0 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20  _PARSER_STACK   
3ecd0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
3ece0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
3ecf0 47 45 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20  GECACHE_SIZE    
3ed00 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c     7.#define SQL
3ed10 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
3ed20 43 48 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20  CH_SIZE         
3ed30 38 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  8../*.** CAPI3RE
3ed40 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e  F: Database Conn
3ed50 65 63 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48  ection Status {H
3ed60 31 37 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a  17500} <S60200>.
3ed70 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
3ed80 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
3ed90 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
3eda0 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65  retrieve runtime
3edb0 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
3edc0 69 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20  ion .** about a 
3edd0 73 69 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65  single [database
3ede0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54   connection].  T
3edf0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
3ee00 74 20 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61  t is the.** data
3ee10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
3ee20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74  object to be int
3ee30 65 72 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20  errogated.  The 
3ee40 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a  second argument.
3ee50 2a 2a 20 69 73 20 74 68 65 20 70 61 72 61 6d 65  ** is the parame
3ee60 74 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61  ter to interroga
3ee70 74 65 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20  te.  Currently, 
3ee80 74 68 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64  the only allowed
3ee90 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68   value.** for th
3eea0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
3eeb0 65 72 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42  er is [SQLITE_DB
3eec0 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45  STATUS_LOOKASIDE
3eed0 5f 55 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74  _USED]..** Addit
3eee0 69 6f 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69  ional options wi
3eef0 6c 6c 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72  ll likely appear
3ef00 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
3ef10 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ses of SQLite..*
3ef20 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74  *.** The current
3ef30 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65   value of the re
3ef40 71 75 65 73 74 65 64 20 70 61 72 61 6d 65 74 65  quested paramete
3ef50 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  r is written int
3ef60 6f 20 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74  o *pCur.** and t
3ef70 68 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61  he highest insta
3ef80 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69  ntaneous value i
3ef90 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a  s written into *
3efa0 70 48 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74  pHiwtr.  If.** t
3efb0 68 65 20 72 65 73 65 74 46 6c 67 20 69 73 20 74  he resetFlg is t
3efc0 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69  rue, then the hi
3efd0 67 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65  ghest instantane
3efe0 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20  ous value is.** 
3eff0 72 65 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20  reset back down 
3f000 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  to the current v
3f010 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  alue..**.** See 
3f020 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73  also: [sqlite3_s
3f030 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71  tatus()] and [sq
3f040 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75  lite3_stmt_statu
3f050 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  s()]..*/.SQLITE_
3f060 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
3f070 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69  IMENTAL int sqli
3f080 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71  te3_db_status(sq
3f090 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20  lite3*, int op, 
3f0a0 69 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  int *pCur, int *
3f0b0 70 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65  pHiwtr, int rese
3f0c0 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tFlg);../*.** CA
3f0d0 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50  PI3REF: Status P
3f0e0 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61  arameters for da
3f0f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3f100 6e 73 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37  ns {H17520} <H17
3f110 35 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  500>.** EXPERIME
3f120 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  NTAL.**.** These
3f130 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74   constants are t
3f140 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74  he available int
3f150 65 67 65 72 20 22 76 65 72 62 73 22 20 74 68 61  eger "verbs" tha
3f160 74 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20  t can be passed 
3f170 61 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  as.** the second
3f180 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
3f190 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61   [sqlite3_db_sta
3f1a0 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tus()] interface
3f1b0 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 62  ..**.** New verb
3f1c0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  s may be added i
3f1d0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
3f1e0 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 69  s of SQLite. Exi
3f1f0 73 74 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 6d  sting verbs.** m
3f200 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69  ight be disconti
3f210 6e 75 65 64 2e 20 41 70 70 6c 69 63 61 74 69 6f  nued. Applicatio
3f220 6e 73 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20  ns should check 
3f230 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
3f240 66 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  from.** [sqlite3
3f250 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20 74 6f  _db_status()] to
3f260 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
3f270 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e  the call worked.
3f280 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
3f290 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e  _db_status()] in
3f2a0 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74  terface will ret
3f2b0 75 72 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65  urn a non-zero e
3f2c0 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 20  rror code.** if 
3f2d0 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f  a discontinued o
3f2e0 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65  r unsupported ve
3f2f0 72 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a  rb is invoked..*
3f300 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74  *.** <dl>.** <dt
3f310 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53  >SQLITE_DBSTATUS
3f320 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c  _LOOKASIDE_USED<
3f330 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3f340 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72   parameter retur
3f350 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
3f360 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72   lookaside memor
3f370 79 20 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c  y slots currentl
3f380 79 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74  y.** checked out
3f390 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a  .</dd>.** </dl>.
3f3a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3f3b0 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41  E_DBSTATUS_LOOKA
3f3c0 53 49 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a  SIDE_USED     0.
3f3d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3f3e0 3a 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  : Prepared State
3f3f0 6d 65 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37  ment Status {H17
3f400 35 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a  550} <S60200>.**
3f410 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
3f420 0a 2a 2a 20 45 61 63 68 20 70 72 65 70 61 72 65  .** Each prepare
3f430 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e  d statement main
3f440 74 61 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a  tains various.**
3f450 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41   [SQLITE_STMTSTA
3f460 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74  TUS_SORT | count
3f470 65 72 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72  ers] that measur
3f480 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20  e the number.** 
3f490 6f 66 20 74 69 6d 65 73 20 69 74 20 68 61 73 20  of times it has 
3f4a0 70 65 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66  performed specif
3f4b0 69 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20  ic operations.  
3f4c0 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63  These counters c
3f4d0 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f  an.** be used to
3f4e0 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72   monitor the per
3f4f0 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74  formance charact
3f500 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20  eristics of the 
3f510 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74  prepared.** stat
3f520 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61  ements.  For exa
3f530 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d  mple, if the num
3f540 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65  ber of table ste
3f550 70 73 20 67 72 65 61 74 6c 79 20 65 78 63 65 65  ps greatly excee
3f560 64 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  ds.** the number
3f570 20 6f 66 20 74 61 62 6c 65 20 73 65 61 72 63 68   of table search
3f580 65 73 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77  es or result row
3f590 73 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65  s, that would te
3f5a0 6e 64 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a  nd to indicate.*
3f5b0 2a 20 74 68 61 74 20 74 68 65 20 70 72 65 70 61  * that the prepa
3f5c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73  red statement is
3f5d0 20 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61   using a full ta
3f5e0 62 6c 65 20 73 63 61 6e 20 72 61 74 68 65 72 20  ble scan rather 
3f5f0 74 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78  than.** an index
3f600 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  .  .**.** This i
3f610 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
3f620 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64   to retrieve and
3f630 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76   reset counter v
3f640 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20  alues from.** a 
3f650 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
3f660 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74  ent].  The first
3f670 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
3f680 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
3f690 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f  ent.** object to
3f6a0 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64   be interrogated
3f6b0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  .  The second ar
3f6c0 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20  gument.** is an 
3f6d0 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72  integer code for
3f6e0 20 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c   a specific [SQL
3f6f0 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53  ITE_STMTSTATUS_S
3f700 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a  ORT | counter].*
3f710 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67  * to be interrog
3f720 61 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75  ated. .** The cu
3f730 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74  rrent value of t
3f740 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 75  he requested cou
3f750 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
3f760 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65  ..** If the rese
3f770 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68  tFlg is true, th
3f780 65 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69  en the counter i
3f790 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20  s reset to zero 
3f7a0 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e  after this.** in
3f7b0 74 65 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74  terface call ret
3f7c0 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  urns..**.** See 
3f7d0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73  also: [sqlite3_s
3f7e0 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71  tatus()] and [sq
3f7f0 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
3f800 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
3f810 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
3f820 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
3f830 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71  3_stmt_status(sq
3f840 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
3f850 20 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67   op,int resetFlg
3f860 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3f870 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d  EF: Status Param
3f880 65 74 65 72 73 20 66 6f 72 20 70 72 65 70 61 72  eters for prepar
3f890 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48  ed statements {H
3f8a0 31 37 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a  17570} <H17550>.
3f8b0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
3f8c0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70  **.** These prep
3f8d0 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20  rocessor macros 
3f8e0 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63  define integer c
3f8f0 6f 64 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63  odes that name c
3f900 6f 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73  ounter.** values
3f910 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
3f920 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
3f930 6d 74 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74  mt_status()] int
3f940 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d  erface..** The m
3f950 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76  eanings of the v
3f960 61 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20  arious counters 
3f970 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  are as follows:.
3f980 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64  **.** <dl>.** <d
3f990 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41  t>SQLITE_STMTSTA
3f9a0 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45  TUS_FULLSCAN_STE
3f9b0 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  P</dt>.** <dd>Th
3f9c0 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  is is the number
3f9d0 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 53   of times that S
3f9e0 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 70 65  QLite has steppe
3f9f0 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20  d forward in.** 
3fa00 61 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 20  a table as part 
3fa10 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20  of a full table 
3fa20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d  scan.  Large num
3fa30 62 65 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f  bers for this co
3fa40 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64  unter.** may ind
3fa50 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74  icate opportunit
3fa60 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61  ies for performa
3fa70 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20  nce improvement 
3fa80 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65  through .** care
3fa90 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63  ful use of indic
3faa0 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  es.</dd>.**.** <
3fab0 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54  dt>SQLITE_STMTST
3fac0 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a  ATUS_SORT</dt>.*
3fad0 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68  * <dd>This is th
3fae0 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74  e number of sort
3faf0 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74   operations that
3fb00 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a   have occurred..
3fb10 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  ** A non-zero va
3fb20 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e  lue in this coun
3fb30 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65  ter may indicate
3fb40 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20   an opportunity 
3fb50 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e  to.** improvemen
3fb60 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68  t performance th
3fb70 72 6f 75 67 68 20 63 61 72 65 66 75 6c 20 75 73  rough careful us
3fb80 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64  e of indices.</d
3fb90 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a  d>.**.** </dl>.*
3fba0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3fbb0 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c  _STMTSTATUS_FULL
3fbc0 53 43 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a  SCAN_STEP     1.
3fbd0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3fbe0 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20  TMTSTATUS_SORT  
3fbf0 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f              2../
3fc00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
3fc10 75 73 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65  ustom Page Cache
3fc20 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52   Object.** EXPER
3fc30 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
3fc40 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
3fc50 20 74 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e   type is opaque.
3fc60 20 20 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e    It is implemen
3fc70 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c  ted by.** the pl
3fc80 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20  uggable module. 
3fc90 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65   The SQLite core
3fca0 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67   has no knowledg
3fcb0 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65  e of.** its size
3fcc0 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72   or internal str
3fcd0 75 63 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72  ucture and never
3fce0 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a   deals with the.
3fcf0 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  ** sqlite3_pcach
3fd00 65 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20  e object except 
3fd10 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70  by holding and p
3fd20 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a  assing pointers.
3fd30 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74  ** to the object
3fd40 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c  ..**.** See [sql
3fd50 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
3fd60 6f 64 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f  ods] for additio
3fd70 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
3fd80 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
3fd90 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  ct sqlite3_pcach
3fda0 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
3fdb0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3fdc0 46 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44  F: Application D
3fdd0 65 66 69 6e 65 64 20 50 61 67 65 20 43 61 63 68  efined Page Cach
3fde0 65 2e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  e..** KEYWORDS: 
3fdf0 7b 70 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a 20  {page cache}.** 
3fe00 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
3fe10 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
3fe20 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f  config]([SQLITE_
3fe30 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c 20  CONFIG_PCACHE], 
3fe40 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 20 63  ...) interface c
3fe50 61 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61  an.** register a
3fe60 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61  n alternative pa
3fe70 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ge cache impleme
3fe80 6e 74 61 74 69 6f 6e 20 62 79 20 70 61 73 73 69  ntation by passi
3fe90 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e 73  ng in an .** ins
3fea0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c  tance of the sql
3feb0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
3fec0 6f 64 73 20 73 74 72 75 63 74 75 72 65 2e 20 54  ods structure. T
3fed0 68 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 74  he majority of t
3fee0 68 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d 6f  he .** heap memo
3fef0 72 79 20 75 73 65 64 20 62 79 20 53 51 4c 69 74  ry used by SQLit
3ff00 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  e is used by the
3ff10 20 70 61 67 65 20 63 61 63 68 65 20 74 6f 20 63   page cache to c
3ff20 61 63 68 65 20 64 61 74 61 20 72 65 61 64 20 0a  ache data read .
3ff30 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 64  ** from, or read
3ff40 79 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  y to be written 
3ff50 74 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  to, the database
3ff60 20 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 6d   file. By implem
3ff70 65 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73  enting a .** cus
3ff80 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20 75  tom page cache u
3ff90 73 69 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61  sing this API, a
3ffa0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61  n application ca
3ffb0 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 0a  n control more .
3ffc0 2a 2a 20 70 72 65 63 69 73 65 6c 79 20 74 68 65  ** precisely the
3ffd0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
3ffe0 79 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 51  y consumed by SQ
3fff0 4c 69 74 65 2c 20 74 68 65 20 77 61 79 20 69 6e  Lite, the way in
40000 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 20   which .** that 
40010 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61  memory is alloca
40020 74 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64  ted and released
40030 2c 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 69  , and the polici
40040 65 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 64  es used to .** d
40050 65 74 65 72 6d 69 6e 65 20 65 78 61 63 74 6c 79  etermine exactly
40060 20 77 68 69 63 68 20 70 61 72 74 73 20 6f 66 20   which parts of 
40070 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  a database file 
40080 61 72 65 20 63 61 63 68 65 64 20 61 6e 64 20 66  are cached and f
40090 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 2e  or .** how long.
400a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65  .**.** The conte
400b0 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74  nts of the sqlit
400c0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
400d0 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
400e0 63 6f 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20  copied to an.** 
400f0 69 6e 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20  internal buffer 
40100 62 79 20 53 51 4c 69 74 65 20 77 69 74 68 69 6e  by SQLite within
40110 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71   the call to [sq
40120 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20  lite3_config].  
40130 48 65 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 70  Hence.** the app
40140 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 73  lication may dis
40150 63 61 72 64 20 74 68 65 20 70 61 72 61 6d 65 74  card the paramet
40160 65 72 20 61 66 74 65 72 20 74 68 65 20 63 61 6c  er after the cal
40170 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  l to.** [sqlite3
40180 5f 63 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 72  _config()] retur
40190 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49  ns..**.** The xI
401a0 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20  nit() method is 
401b0 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20  called once for 
401c0 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71  each call to [sq
401d0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
401e0 28 29 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20  ()].** (usually 
401f0 6f 6e 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e 67  only once during
40200 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66   the lifetime of
40210 20 74 68 65 20 70 72 6f 63 65 73 73 29 2e 20 49   the process). I
40220 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 61  t is passed.** a
40230 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 71 6c   copy of the sql
40240 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
40250 6f 64 73 2e 70 41 72 67 20 76 61 6c 75 65 2e 20  ods.pArg value. 
40260 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  It can be used t
40270 6f 20 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f 62  o set.** up glob
40280 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 61 6e  al structures an
40290 64 20 6d 75 74 65 78 65 73 20 72 65 71 75 69 72  d mutexes requir
402a0 65 64 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d  ed by the custom
402b0 20 70 61 67 65 20 63 61 63 68 65 20 0a 2a 2a 20   page cache .** 
402c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
402d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 74  .**.** The xShut
402e0 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73  down() method is
402f0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
40300 68 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75  hin [sqlite3_shu
40310 74 64 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 66  tdown()], .** if
40320 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
40330 20 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50   invokes this AP
40340 49 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65  I. It can be use
40350 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a  d to clean up .*
40360 2a 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e  * any outstandin
40370 67 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f  g resources befo
40380 72 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64  re process shutd
40390 6f 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64  own, if required
403a0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68  ..**.** SQLite h
403b0 6f 6c 64 73 20 61 20 5b 53 51 4c 49 54 45 5f 4d  olds a [SQLITE_M
403c0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 5d 20  UTEX_RECURSIVE] 
403d0 6d 75 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e  mutex when it in
403e0 76 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e  vokes.** the xIn
403f0 69 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68  it method, so th
40400 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e  e xInit method n
40410 65 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61  eed not be threa
40420 64 73 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78  dsafe.  The.** x
40430 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20  Shutdown method 
40440 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66  is only called f
40450 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75  rom [sqlite3_shu
40460 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64  tdown()] so it d
40470 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20  oes.** not need 
40480 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 66 65  to be threadsafe
40490 20 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f 74   either.  All ot
404a0 68 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 74  her methods must
404b0 20 62 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a   be threadsafe.*
404c0 2a 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 64  * in multithread
404d0 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e  ed applications.
404e0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  .**.** SQLite wi
404f0 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20  ll never invoke 
40500 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61  xInit() more tha
40510 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61  n once without a
40520 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a  n intervening.**
40530 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f   call to xShutdo
40540 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  wn()..**.** The 
40550 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64  xCreate() method
40560 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73   is used to cons
40570 74 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68  truct a new cach
40580 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 4c  e instance.  SQL
40590 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 69  ite.** will typi
405a0 63 61 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e 65  cally create one
405b0 20 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 20   cache instance 
405c0 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 64 61  for each open da
405d0 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20  tabase file,.** 
405e0 74 68 6f 75 67 68 20 74 68 69 73 20 69 73 20 6e  though this is n
405f0 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 54  ot guaranteed. T
40600 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 61  he.** first para
40610 6d 65 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 69  meter, szPage, i
40620 73 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79  s the size in by
40630 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 73  tes of the pages
40640 20 74 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65   that must.** be
40650 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68   allocated by th
40660 65 20 63 61 63 68 65 2e 20 20 73 7a 50 61 67 65  e cache.  szPage
40670 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 70   will not be a p
40680 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 7a  ower of two.  sz
40690 50 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65  Page.** will the
406a0 20 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68   page size of th
406b0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
406c0 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 63 61  that is to be ca
406d0 63 68 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a 20  ched plus an.** 
406e0 69 6e 63 72 65 6d 65 6e 74 20 28 68 65 72 65 20  increment (here 
406f0 63 61 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 61  called "R") of a
40700 62 6f 75 74 20 31 30 30 20 6f 72 20 32 30 30 2e  bout 100 or 200.
40710 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73    SQLite will us
40720 65 20 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 52  e the.** extra R
40730 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70   bytes on each p
40740 61 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74  age to store met
40750 61 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 20  adata about the 
40760 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 61  underlying.** da
40770 74 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20 64  tabase page on d
40780 69 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20  isk.  The value 
40790 6f 66 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a 20  of R depends.** 
407a0 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 65  on the SQLite ve
407b0 72 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65  rsion, the targe
407c0 74 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20  t platform, and 
407d0 68 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 63  how SQLite was c
407e0 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 73  ompiled..** R is
407f0 20 63 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 20   constant for a 
40800 70 61 72 74 69 63 75 6c 61 72 20 62 75 69 6c 64  particular build
40810 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68 65   of SQLite.  The
40820 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
40830 20 74 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 29   to.** xCreate()
40840 2c 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73  , bPurgeable, is
40850 20 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63   true if the cac
40860 68 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64  he being created
40870 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64   will.** be used
40880 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61   to cache databa
40890 73 65 20 70 61 67 65 73 20 6f 66 20 61 20 66 69  se pages of a fi
408a0 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73  le stored on dis
408b0 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69  k, or.** false i
408c0 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72  f it is used for
408d0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
408e0 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68  tabase. The cach
408f0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
40900 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76  .** does not hav
40910 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67  e to do anything
40920 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 77   special based w
40930 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ith the value of
40940 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a 20   bPurgeable;.** 
40950 69 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76  it is purely adv
40960 69 73 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 63  isory.  On a cac
40970 68 65 20 77 68 65 72 65 20 62 50 75 72 67 65 61  he where bPurgea
40980 62 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 53 51  ble is false, SQ
40990 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76  Lite will.** nev
409a0 65 72 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e  er invoke xUnpin
409b0 28 29 20 65 78 63 65 70 74 20 74 6f 20 64 65 6c  () except to del
409c0 69 62 65 72 61 74 65 6c 79 20 64 65 6c 65 74 65  iberately delete
409d0 20 61 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 6f   a page..** In o
409e0 74 68 65 72 20 77 6f 72 64 73 2c 20 61 20 63 61  ther words, a ca
409f0 63 68 65 20 63 72 65 61 74 65 64 20 77 69 74 68  che created with
40a00 20 62 50 75 72 67 65 61 62 6c 65 20 73 65 74 20   bPurgeable set 
40a10 74 6f 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a 2a  to false will.**
40a20 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 61   never contain a
40a30 6e 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65  ny unpinned page
40a40 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 61  s..**.** The xCa
40a50 63 68 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64  chesize() method
40a60 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61   may be called a
40a70 74 20 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51  t any time by SQ
40a80 4c 69 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a  Lite to set the.
40a90 2a 2a 20 73 75 67 67 65 73 74 65 64 20 6d 61 78  ** suggested max
40aa0 69 6d 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20  imum cache-size 
40ab0 28 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73  (number of pages
40ac0 20 73 74 6f 72 65 64 20 62 79 29 20 74 68 65 20   stored by) the 
40ad0 63 61 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63  cache.** instanc
40ae0 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
40af0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20  first argument. 
40b00 54 68 69 73 20 69 73 20 74 68 65 20 76 61 6c 75  This is the valu
40b10 65 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69  e configured usi
40b20 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  ng.** the SQLite
40b30 20 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f   "[PRAGMA cache_
40b40 73 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 20  size]" command. 
40b50 41 73 20 77 69 74 68 20 74 68 65 20 62 50 75 72  As with the bPur
40b60 67 65 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72  geable parameter
40b70 2c 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65  ,.** the impleme
40b80 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72  ntation is not r
40b90 65 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61 6e  equired to do an
40ba0 79 74 68 69 6e 67 20 77 69 74 68 20 74 68 69 73  ything with this
40bb0 0a 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 73  .** value; it is
40bc0 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a   advisory only..
40bd0 2a 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 63  **.** The xPagec
40be0 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 68  ount() method sh
40bf0 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20  ould return the 
40c00 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
40c10 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f  currently.** sto
40c20 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65  red in the cache
40c30 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 65  ..** .** The xFe
40c40 74 63 68 28 29 20 6d 65 74 68 6f 64 20 69 73 20  tch() method is 
40c50 75 73 65 64 20 74 6f 20 66 65 74 63 68 20 61 20  used to fetch a 
40c60 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20  page and return 
40c70 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
40c80 20 0a 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 69   .** A 'page', i
40c90 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 2c 20  n this context, 
40ca0 69 73 20 61 20 62 75 66 66 65 72 20 6f 66 20 73  is a buffer of s
40cb0 7a 50 61 67 65 20 62 79 74 65 73 20 61 6c 69 67  zPage bytes alig
40cc0 6e 65 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d 62  ned at an.** 8-b
40cd0 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 68  yte boundary. Th
40ce0 65 20 70 61 67 65 20 74 6f 20 62 65 20 66 65 74  e page to be fet
40cf0 63 68 65 64 20 69 73 20 64 65 74 65 72 6d 69 6e  ched is determin
40d00 65 64 20 62 79 20 74 68 65 20 6b 65 79 2e 20 54  ed by the key. T
40d10 68 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b 65  he.** mimimum ke
40d20 79 20 76 61 6c 75 65 20 69 73 20 31 2e 20 41 66  y value is 1. Af
40d30 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20  ter it has been 
40d40 72 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20  retrieved using 
40d50 78 46 65 74 63 68 2c 20 74 68 65 20 70 61 67 65  xFetch, the page
40d60 20 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72   .** is consider
40d70 65 64 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 64  ed to be "pinned
40d80 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  "..**.** If the 
40d90 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69  requested page i
40da0 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
40db0 20 70 61 67 65 20 63 61 63 68 65 2c 20 74 68 65   page cache, the
40dc0 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
40dd0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
40de0 6f 6e 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61  on must return a
40df0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
40e00 70 61 67 65 20 62 75 66 66 65 72 20 77 69 74 68  page buffer with
40e10 20 69 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20   its content.** 
40e20 69 6e 74 61 63 74 2e 20 20 49 66 20 74 68 65 20  intact.  If the 
40e30 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69  requested page i
40e40 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e  s not already in
40e50 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e   the cache, then
40e60 20 74 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72   the.** behavior
40e70 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 69 6d   of the cache im
40e80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
40e90 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68  determined by th
40ea0 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a  e value of the.*
40eb0 2a 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72  * createFlag par
40ec0 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f  ameter passed to
40ed0 20 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69   xFetch, accordi
40ee0 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  ng to the follow
40ef0 69 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a  ing table:.**.**
40f00 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31   <table border=1
40f10 20 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e   width=85% align
40f20 3d 63 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 3e  =center>.** <tr>
40f30 3c 74 68 3e 20 63 72 65 61 74 65 46 6c 61 67 20  <th> createFlag 
40f40 3c 74 68 3e 20 42 65 68 61 76 69 6f 75 72 20 77  <th> Behaviour w
40f50 68 65 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 20  hen page is not 
40f60 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65  already in cache
40f70 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 3c  .** <tr><td> 0 <
40f80 74 64 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 63  td> Do not alloc
40f90 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20  ate a new page. 
40fa0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a   Return NULL..**
40fb0 20 3c 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 3e   <tr><td> 1 <td>
40fc0 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
40fd0 70 61 67 65 20 69 66 20 69 74 20 65 61 73 79 20  page if it easy 
40fe0 61 6e 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74  and convenient t
40ff0 6f 20 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 20  o do so..**     
41000 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65              Othe
41010 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c  rwise return NUL
41020 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 32  L..** <tr><td> 2
41030 20 3c 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 79   <td> Make every
41040 20 65 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f 63   effort to alloc
41050 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20  ate a new page. 
41060 20 4f 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a 20   Only return.** 
41070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41080 4e 55 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 69  NULL if allocati
41090 6e 67 20 61 20 6e 65 77 20 70 61 67 65 20 69 73  ng a new page is
410a0 20 65 66 66 65 63 74 69 76 65 6c 79 20 69 6d 70   effectively imp
410b0 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 61  ossible..** </ta
410c0 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ble>.**.** SQLit
410d0 65 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20  e will normally 
410e0 69 6e 76 6f 6b 65 20 78 46 65 74 63 68 28 29 20  invoke xFetch() 
410f0 77 69 74 68 20 61 20 63 72 65 61 74 65 46 6c 61  with a createFla
41100 67 20 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 66  g of 0 or 1.  If
41110 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 46  .** a call to xF
41120 65 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61  etch() with crea
41130 74 65 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 6e  teFlag==1 return
41140 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c  s NULL, then SQL
41150 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 65  ite will.** atte
41160 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65  mpt to unpin one
41170 20 6f 72 20 6d 6f 72 65 20 63 61 63 68 65 20 70   or more cache p
41180 61 67 65 73 20 62 79 20 73 70 69 6c 6c 69 6e 67  ages by spilling
41190 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a   the content of.
411a0 2a 2a 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20  ** pinned pages 
411b0 74 6f 20 64 69 73 6b 20 61 6e 64 20 73 79 6e 63  to disk and sync
411c0 68 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69  hing the operati
411d0 6e 67 20 73 79 73 74 65 6d 20 64 69 73 6b 20 63  ng system disk c
411e0 61 63 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 61  ache. After.** a
411f0 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e 70  ttempting to unp
41200 69 6e 20 70 61 67 65 73 2c 20 74 68 65 20 78 46  in pages, the xF
41210 65 74 63 68 28 29 20 6d 65 74 68 6f 64 20 77 69  etch() method wi
41220 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 67  ll be invoked ag
41230 61 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 72  ain with.** a cr
41240 65 61 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a 2a  eateFlag of 2..*
41250 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73  *.** xUnpin() is
41260 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74   called by SQLit
41270 65 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72  e with a pointer
41280 20 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20   to a currently 
41290 70 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61  pinned page.** a
412a0 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67  s its second arg
412b0 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68  ument. If the th
412c0 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64  ird parameter, d
412d0 69 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a  iscard, is non-z
412e0 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ero,.** then the
412f0 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20   page should be 
41300 65 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65  evicted from the
41310 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20   cache. In this 
41320 63 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20  case SQLite .** 
41330 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
41340 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70   next time the p
41350 61 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64  age is retrieved
41360 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20   from the cache 
41370 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65  using.** the xFe
41380 74 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74  tch() method, it
41390 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e   will be zeroed.
413a0 20 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20   If the discard 
413b0 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20  parameter is.** 
413c0 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70  zero, then the p
413d0 61 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65  age is considere
413e0 64 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64  d to be unpinned
413f0 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c  . The cache impl
41400 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61  ementation.** ma
41410 79 20 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 63  y choose to evic
41420 74 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  t unpinned pages
41430 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a   at any time..**
41440 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 73  .** The cache is
41450 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f   not required to
41460 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 66   perform any ref
41470 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 2e  erence counting.
41480 20 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 61   A single .** ca
41490 6c 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 75  ll to xUnpin() u
414a0 6e 70 69 6e 73 20 74 68 65 20 70 61 67 65 20 72  npins the page r
414b0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
414c0 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72   number of prior
414d0 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 46   calls .** to xF
414e0 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  etch()..**.** Th
414f0 65 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68 6f  e xRekey() metho
41500 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61  d is used to cha
41510 6e 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c 75  nge the key valu
41520 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
41530 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 61  h the.** page pa
41540 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
41550 6e 64 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d  nd argument from
41560 20 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b 65   oldKey to newKe
41570 79 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 0a  y. If the cache.
41580 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f  ** previously co
41590 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 20  ntains an entry 
415a0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
415b0 6e 65 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 6c  newKey, it shoul
415c0 64 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65  d be.** discarde
415d0 64 2e 20 41 6e 79 20 70 72 69 6f 72 20 63 61 63  d. Any prior cac
415e0 68 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61  he entry associa
415f0 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 20  ted with newKey 
41600 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f  is guaranteed no
41610 74 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65  t.** to be pinne
41620 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51  d..**.** When SQ
41630 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78  Lite calls the x
41640 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f  Truncate() metho
41650 64 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 73  d, the cache mus
41660 74 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a 2a  t discard all.**
41670 20 65 78 69 73 74 69 6e 67 20 63 61 63 68 65 20   existing cache 
41680 65 6e 74 72 69 65 73 20 77 69 74 68 20 70 61 67  entries with pag
41690 65 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 29  e numbers (keys)
416a0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
416b0 20 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 65   equal.** to the
416c0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 4c   value of the iL
416d0 69 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 70  imit parameter p
416e0 61 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63 61  assed to xTrunca
416f0 74 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a 20  te(). If any.** 
41700 6f 66 20 74 68 65 73 65 20 70 61 67 65 73 20 61  of these pages a
41710 72 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 20  re pinned, they 
41720 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75  are implicitly u
41730 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e 67  npinned, meaning
41740 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 61   that.** they ca
41750 6e 20 62 65 20 73 61 66 65 6c 79 20 64 69 73 63  n be safely disc
41760 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  arded..**.** The
41770 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68   xDestroy() meth
41780 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  od is used to de
41790 6c 65 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c  lete a cache all
417a0 6f 63 61 74 65 64 20 62 79 20 78 43 72 65 61 74  ocated by xCreat
417b0 65 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 6f  e()..** All reso
417c0 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64  urces associated
417d0 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66   with the specif
417e0 69 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c 64  ied cache should
417f0 20 62 65 20 66 72 65 65 64 2e 20 41 66 74 65 72   be freed. After
41800 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 20  .** calling the 
41810 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f  xDestroy() metho
41820 64 2c 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 64  d, SQLite consid
41830 65 72 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ers the [sqlite3
41840 5f 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 6e  _pcache*].** han
41850 64 6c 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e 64  dle invalid, and
41860 20 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 74   will not use it
41870 20 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20   with any other 
41880 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
41890 65 74 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 69  ethods.** functi
418a0 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ons..*/.typedef 
418b0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70  struct sqlite3_p
418c0 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 71  cache_methods sq
418d0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
418e0 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c  hods;.struct sql
418f0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
41900 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 41  ods {.  void *pA
41910 72 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69  rg;.  int (*xIni
41920 74 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  t)(void*);.  voi
41930 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76  d (*xShutdown)(v
41940 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33  oid*);.  sqlite3
41950 5f 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65 61  _pcache *(*xCrea
41960 74 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c 20  te)(int szPage, 
41970 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b  int bPurgeable);
41980 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65  .  void (*xCache
41990 73 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63  size)(sqlite3_pc
419a0 61 63 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68  ache*, int nCach
419b0 65 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a  esize);.  int (*
419c0 78 50 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69  xPagecount)(sqli
419d0 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20  te3_pcache*);.  
419e0 76 6f 69 64 20 2a 28 2a 78 46 65 74 63 68 29 28  void *(*xFetch)(
419f0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c  sqlite3_pcache*,
41a00 20 75 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 69   unsigned key, i
41a10 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 3b 0a  nt createFlag);.
41a20 20 20 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29    void (*xUnpin)
41a30 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
41a40 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73  , void*, int dis
41a50 63 61 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a  card);.  void (*
41a60 78 52 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f  xRekey)(sqlite3_
41a70 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20  pcache*, void*, 
41a80 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c  unsigned oldKey,
41a90 20 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79   unsigned newKey
41aa0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75  );.  void (*xTru
41ab0 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70  ncate)(sqlite3_p
41ac0 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64  cache*, unsigned
41ad0 20 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 64   iLimit);.  void
41ae0 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c   (*xDestroy)(sql
41af0 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 7d  ite3_pcache*);.}
41b00 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
41b10 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70  F: Online Backup
41b20 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52   Object.** EXPER
41b30 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
41b40 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  e sqlite3_backup
41b50 20 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20   object records 
41b60 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  state informatio
41b70 6e 20 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f 69  n about an ongoi
41b80 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 63  ng.** online bac
41b90 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  kup operation.  
41ba0 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  The sqlite3_back
41bb0 75 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65  up object is cre
41bc0 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c  ated by.** a cal
41bd0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61  l to [sqlite3_ba
41be0 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64  ckup_init()] and
41bf0 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79   is destroyed by
41c00 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73   a call to.** [s
41c10 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
41c20 6e 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53  nish()]..**.** S
41c30 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20  ee Also: [Using 
41c40 74 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e  the SQLite Onlin
41c50 65 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f  e Backup API].*/
41c60 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
41c70 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73  sqlite3_backup s
41c80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a  qlite3_backup;..
41c90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
41ca0 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50  Online Backup AP
41cb0 49 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  I..** EXPERIMENT
41cc0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50  AL.**.** This AP
41cd0 49 20 69 73 20 75 73 65 64 20 74 6f 20 6f 76 65  I is used to ove
41ce0 72 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65  rwrite the conte
41cf0 6e 74 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 62  nts of one datab
41d00 61 73 65 20 77 69 74 68 20 74 68 61 74 0a 2a 2a  ase with that.**
41d10 20 6f 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 20   of another. It 
41d20 69 73 20 75 73 65 66 75 6c 20 65 69 74 68 65 72  is useful either
41d30 20 66 6f 72 20 63 72 65 61 74 69 6e 67 20 62 61   for creating ba
41d40 63 6b 75 70 73 20 6f 66 20 64 61 74 61 62 61 73  ckups of databas
41d50 65 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f 70  es or.** for cop
41d60 79 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  ying in-memory d
41d70 61 74 61 62 61 73 65 73 20 74 6f 20 6f 72 20 66  atabases to or f
41d80 72 6f 6d 20 70 65 72 73 69 73 74 65 6e 74 20 66  rom persistent f
41d90 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65  iles. .**.** See
41da0 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68   Also: [Using th
41db0 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20  e SQLite Online 
41dc0 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a  Backup API].**.*
41dd0 2a 20 45 78 63 6c 75 73 69 76 65 20 61 63 63 65  * Exclusive acce
41de0 73 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74  ss is required t
41df0 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
41e00 6e 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 74  n database for t
41e10 68 65 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e 20  he .** duration 
41e20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  of the operation
41e30 2e 20 48 6f 77 65 76 65 72 20 74 68 65 20 73 6f  . However the so
41e40 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73  urce database is
41e50 20 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c 6f   only.** read-lo
41e60 63 6b 65 64 20 77 68 69 6c 65 20 69 74 20 69 73  cked while it is
41e70 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20   actually being 
41e80 72 65 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 20  read, it is not 
41e90 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 6e  locked.** contin
41ea0 75 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20 65  uously for the e
41eb0 6e 74 69 72 65 20 6f 70 65 72 61 74 69 6f 6e 2e  ntire operation.
41ec0 20 54 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75   Thus, the backu
41ed0 70 20 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 66  p may be.** perf
41ee0 6f 72 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 20  ormed on a live 
41ef0 64 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 74  database without
41f00 20 70 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 65   preventing othe
41f10 72 20 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a 20  r users from.** 
41f20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64  writing to the d
41f30 61 74 61 62 61 73 65 20 66 6f 72 20 61 6e 20 65  atabase for an e
41f40 78 74 65 6e 64 65 64 20 70 65 72 69 6f 64 20 6f  xtended period o
41f50 66 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 54  f time..** .** T
41f60 6f 20 70 65 72 66 6f 72 6d 20 61 20 62 61 63 6b  o perform a back
41f70 75 70 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 2a  up operation: .*
41f80 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 20  *   <ol>.**     
41f90 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62  <li><b>sqlite3_b
41fa0 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e  ackup_init()</b>
41fb0 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   is called once 
41fc0 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  to initialize th
41fd0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 62 61 63  e.**         bac
41fe0 6b 75 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c 69  kup, .**     <li
41ff0 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b  ><b>sqlite3_back
42000 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73  up_step()</b> is
42010 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d   called one or m
42020 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61  ore times to tra
42030 6e 73 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 20  nsfer .**       
42040 20 20 74 68 65 20 64 61 74 61 20 62 65 74 77 65    the data betwe
42050 65 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61 62  en the two datab
42060 61 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c  ases, and finall
42070 79 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e  y.**     <li><b>
42080 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
42090 69 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63  inish()</b> is c
420a0 61 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65  alled to release
420b0 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0a   all resources .
420c0 2a 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f 63  **         assoc
420d0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62  iated with the b
420e0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e  ackup operation.
420f0 20 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20   .**   </ol>.** 
42100 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20  There should be 
42110 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c 6c  exactly one call
42120 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
42130 75 70 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 20  up_finish() for 
42140 65 61 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 66  each.** successf
42150 75 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ul call to sqlit
42160 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
42170 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74  ..**.** <b>sqlit
42180 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
42190 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  </b>.**.** The f
421a0 69 72 73 74 20 74 77 6f 20 61 72 67 75 6d 65 6e  irst two argumen
421b0 74 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71  ts passed to [sq
421c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69  lite3_backup_ini
421d0 74 28 29 5d 20 61 72 65 20 74 68 65 20 64 61 74  t()] are the dat
421e0 61 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20  abase.** handle 
421f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
42200 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
42210 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65  database and the
42220 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 0a   database name .
42230 2a 2a 20 75 73 65 64 20 74 6f 20 61 74 74 61 63  ** used to attac
42240 68 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  h the destinatio
42250 6e 20 64 61 74 61 62 61 73 65 20 74 6f 20 74 68  n database to th
42260 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 61  e handle. The da
42270 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 69  tabase name.** i
42280 73 20 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 65  s "main" for the
42290 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20   main database, 
422a0 22 74 65 6d 70 22 20 66 6f 72 20 74 68 65 20 74  "temp" for the t
422b0 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
422c0 65 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 6d  e, or.** the nam
422d0 65 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70  e specified as p
422e0 61 72 74 20 6f 66 20 74 68 65 20 5b 41 54 54 41  art of the [ATTA
422f0 43 48 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 66  CH] statement if
42300 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
42310 20 69 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 68   is.** an attach
42320 65 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  ed database. The
42330 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74   third and fourt
42340 68 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73  h arguments pass
42350 65 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65  ed to .** sqlite
42360 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20  3_backup_init() 
42370 69 64 65 6e 74 69 66 79 20 74 68 65 20 5b 64 61  identify the [da
42380 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
42390 6e 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 61  n].** and databa
423a0 73 65 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a 20  se name used.** 
423b0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 6f  to access the so
423c0 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 54  urce database. T
423d0 68 65 20 76 61 6c 75 65 73 20 70 61 73 73 65 64  he values passed
423e0 20 66 6f 72 20 74 68 65 20 73 6f 75 72 63 65 20   for the source 
423f0 61 6e 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74  and .** destinat
42400 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ion [database co
42410 6e 6e 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d 65  nnection] parame
42420 74 65 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 65  ters must not be
42430 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a   the same..**.**
42440 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
42450 75 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74  urs within sqlit
42460 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
42470 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72  , then NULL is r
42480 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61  eturned.** and a
42490 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  n error code and
424a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
424b0 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
424c0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
424d0 74 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 64  tion] .** passed
424e0 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
424f0 67 75 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 79  gument. They may
42500 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73   be retrieved us
42510 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69  ing the.** [sqli
42520 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20  te3_errcode()], 
42530 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  [sqlite3_errmsg(
42540 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
42550 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 66 75 6e  _errmsg16()] fun
42560 63 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 72  ctions..** Other
42570 77 69 73 65 2c 20 69 66 20 73 75 63 63 65 73 73  wise, if success
42580 66 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74  ful, a pointer t
42590 6f 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61  o an [sqlite3_ba
425a0 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 0a  ckup] object is.
425b0 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  ** returned. Thi
425c0 73 20 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 65  s pointer may be
425d0 20 75 73 65 64 20 77 69 74 68 20 74 68 65 20 73   used with the s
425e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
425f0 65 70 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69  ep() and.** sqli
42600 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73  te3_backup_finis
42610 68 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f  h() functions to
42620 20 70 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65   perform the spe
42630 63 69 66 69 65 64 20 62 61 63 6b 75 70 20 0a 2a  cified backup .*
42640 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a  * operation..**.
42650 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61  ** <b>sqlite3_ba
42660 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a  ckup_step()</b>.
42670 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 5b  **.** Function [
42680 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
42690 74 65 70 28 29 5d 20 69 73 20 75 73 65 64 20 74  tep()] is used t
426a0 6f 20 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 61  o copy up to nPa
426b0 67 65 20 70 61 67 65 73 20 62 65 74 77 65 65 6e  ge pages between
426c0 20 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20   .** the source 
426d0 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  and destination 
426e0 64 61 74 61 62 61 73 65 73 2c 20 77 68 65 72 65  databases, where
426f0 20 6e 50 61 67 65 20 69 73 20 74 68 65 20 76 61   nPage is the va
42700 6c 75 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73  lue of the .** s
42710 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
42720 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
42730 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e  3_backup_step().
42740 20 49 66 20 6e 50 61 67 65 20 69 73 20 61 20 6e   If nPage is a n
42750 65 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65  egative.** value
42760 2c 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20  , all remaining 
42770 73 6f 75 72 63 65 20 70 61 67 65 73 20 61 72 65  source pages are
42780 20 63 6f 70 69 65 64 2e 20 49 66 20 74 68 65 20   copied. If the 
42790 72 65 71 75 69 72 65 64 20 70 61 67 65 73 20 61  required pages a
427a0 72 65 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 6c  re .** succesful
427b0 6c 79 20 63 6f 70 69 65 64 2c 20 62 75 74 20 74  ly copied, but t
427c0 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d  here are still m
427d0 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70  ore pages to cop
427e0 79 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a  y before the .**
427f0 20 62 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 6c   backup is compl
42800 65 74 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20  ete, it returns 
42810 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 20  [SQLITE_OK]. If 
42820 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64  no error occured
42830 20 61 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 61   and there .** a
42840 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73  re no more pages
42850 20 74 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 5b   to copy, then [
42860 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 20  SQLITE_DONE] is 
42870 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20  returned. If an 
42880 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73  error .** occurs
42890 2c 20 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 65  , then an SQLite
428a0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
428b0 65 74 75 72 6e 65 64 2e 20 41 73 20 77 65 6c 6c  eturned. As well
428c0 20 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20   as [SQLITE_OK] 
428d0 61 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44  and.** [SQLITE_D
428e0 4f 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f 20  ONE], a call to 
428f0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
42900 74 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e  tep() may return
42910 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c   [SQLITE_READONL
42920 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e  Y],.** [SQLITE_N
42930 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42  OMEM], [SQLITE_B
42940 55 53 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f  USY], [SQLITE_LO
42950 43 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a 20  CKED], or an.** 
42960 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43  [SQLITE_IOERR_AC
42970 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f  CESS | SQLITE_IO
42980 45 52 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 65  ERR_XXX] extende
42990 64 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a  d error code..**
429a0 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 74  .** As well as t
429b0 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68  he case where th
429c0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
429d0 74 61 62 61 73 65 20 66 69 6c 65 20 77 61 73 20  tabase file was 
429e0 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 65  opened for.** re
429f0 61 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c 20  ad-only access, 
42a00 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
42a10 74 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e  tep() may return
42a20 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c   [SQLITE_READONL
42a30 59 5d 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 73  Y] if.** the des
42a40 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69  tination is an i
42a50 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
42a60 65 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65  e with a differe
42a70 6e 74 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20  nt page size.** 
42a80 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20  from the source 
42a90 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
42aa0 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  If sqlite3_backu
42ab0 70 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 20  p_step() cannot 
42ac0 6f 62 74 61 69 6e 20 61 20 72 65 71 75 69 72 65  obtain a require
42ad0 64 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f  d file-system lo
42ae0 63 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ck, then.** the 
42af0 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61  [sqlite3_busy_ha
42b00 6e 64 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 6e  ndler | busy-han
42b10 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a  dler function].*
42b20 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 66  * is invoked (if
42b30 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65   one is specifie
42b40 64 29 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 62  d). If the .** b
42b50 75 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75  usy-handler retu
42b60 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66  rns non-zero bef
42b70 6f 72 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  ore the lock is 
42b80 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20  available, then 
42b90 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  .** [SQLITE_BUSY
42ba0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f  ] is returned to
42bb0 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 20   the caller. In 
42bc0 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61  this case the ca
42bd0 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ll to.** sqlite3
42be0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63  _backup_step() c
42bf0 61 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61  an be retried la
42c00 74 65 72 2e 20 49 66 20 74 68 65 20 73 6f 75 72  ter. If the sour
42c10 63 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20  ce.** [database 
42c20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69  connection].** i
42c30 73 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20  s being used to 
42c40 77 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75  write to the sou
42c50 72 63 65 20 64 61 74 61 62 61 73 65 20 77 68 65  rce database whe
42c60 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  n sqlite3_backup
42c70 5f 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61  _step().** is ca
42c80 6c 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49  lled, then [SQLI
42c90 54 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65  TE_LOCKED] is re
42ca0 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65  turned immediate
42cb0 6c 79 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 68  ly. Again, in th
42cc0 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63  is.** case the c
42cd0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  all to sqlite3_b
42ce0 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e  ackup_step() can
42cf0 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65   be retried late
42d00 72 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 4c  r on. If.** [SQL
42d10 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53  ITE_IOERR_ACCESS
42d20 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   | SQLITE_IOERR_
42d30 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e 4f  XXX], [SQLITE_NO
42d40 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 4c  MEM], or.** [SQL
42d50 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 73  ITE_READONLY] is
42d60 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20   returned, then 
42d70 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20  .** there is no 
42d80 70 6f 69 6e 74 20 69 6e 20 72 65 74 72 79 69 6e  point in retryin
42d90 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71  g the call to sq
42da0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
42db0 70 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 65  p(). These .** e
42dc0 72 72 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 64  rrors are consid
42dd0 65 72 65 64 20 66 61 74 61 6c 2e 20 41 74 20 74  ered fatal. At t
42de0 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 61 70  his point the ap
42df0 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 61  plication must a
42e00 63 63 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 74  ccept .** that t
42e10 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
42e20 69 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 20 61  ion has failed a
42e30 6e 64 20 70 61 73 73 20 74 68 65 20 62 61 63 6b  nd pass the back
42e40 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 6e  up operation han
42e50 64 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 73  dle .** to the s
42e60 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
42e70 6e 69 73 68 28 29 20 74 6f 20 72 65 6c 65 61 73  nish() to releas
42e80 65 20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73  e associated res
42e90 6f 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f  ources..**.** Fo
42ea0 6c 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 72 73  llowing the firs
42eb0 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  t call to sqlite
42ec0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c  3_backup_step(),
42ed0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
42ee0 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65  ck is.** obtaine
42ef0 64 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61  d on the destina
42f00 74 69 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 73  tion file. It is
42f10 20 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 75 6e   not released un
42f20 74 69 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 73  til either .** s
42f30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
42f40 6e 69 73 68 28 29 20 69 73 20 63 61 6c 6c 65 64  nish() is called
42f50 20 6f 72 20 74 68 65 20 62 61 63 6b 75 70 20 6f   or the backup o
42f60 70 65 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70  peration is comp
42f70 6c 65 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 6c  lete .** and sql
42f80 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
42f90 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  () returns [SQLI
42fa0 54 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 69  TE_DONE]. Additi
42fb0 6f 6e 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 6d  onally, each tim
42fc0 65 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20  e .** a call to 
42fd0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
42fe0 74 65 70 28 29 20 69 73 20 6d 61 64 65 20 61 20  tep() is made a 
42ff0 5b 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 73  [shared lock] is
43000 20 6f 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a 20   obtained on.** 
43010 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
43020 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6c  ase file. This l
43030 6f 63 6b 20 69 73 20 72 65 6c 65 61 73 65 64 20  ock is released 
43040 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 71  before the.** sq
43050 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
43060 70 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  p() call returns
43070 2e 20 42 65 63 61 75 73 65 20 74 68 65 20 73 6f  . Because the so
43080 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73  urce database is
43090 20 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 62   not.** locked b
430a0 65 74 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 20  etween calls to 
430b0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
430c0 74 65 70 28 29 2c 20 69 74 20 6d 61 79 20 62 65  tep(), it may be
430d0 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61   modified mid-wa
430e0 79 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  y.** through the
430f0 20 62 61 63 6b 75 70 20 70 72 6f 63 65 64 75 72   backup procedur
43100 65 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65  e. If the source
43110 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64   database is mod
43120 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 65  ified by an.** e
43130 78 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 20  xternal process 
43140 6f 72 20 76 69 61 20 61 20 64 61 74 61 62 61 73  or via a databas
43150 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 68  e connection oth
43160 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20  er than the one 
43170 62 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 79  being.** used by
43180 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72   the backup oper
43190 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20  ation, then the 
431a0 62 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 74  backup will be t
431b0 72 61 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a 20  ransparently.** 
431c0 72 65 73 74 61 72 74 65 64 20 62 79 20 74 68 65  restarted by the
431d0 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 71   next call to sq
431e0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
431f0 70 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72  p(). If the sour
43200 63 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  ce .** database 
43210 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74  is modified by t
43220 68 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  he using the sam
43230 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
43240 63 74 69 6f 6e 20 61 73 20 69 73 20 75 73 65 64  ction as is used
43250 0a 2a 2a 20 62 79 20 74 68 65 20 62 61 63 6b 75  .** by the backu
43260 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65  p operation, the
43270 6e 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 74  n the backup dat
43280 61 62 61 73 65 20 69 73 20 74 72 61 6e 73 70 61  abase is transpa
43290 72 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 74  rently .** updat
432a0 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ed at the same t
432b0 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71  ime..**.** <b>sq
432c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
432d0 69 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20  ish()</b>.**.** 
432e0 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 62 61 63  Once sqlite3_bac
432f0 6b 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 72  kup_step() has r
43300 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f  eturned [SQLITE_
43310 44 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74  DONE], or when t
43320 68 65 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  he .** applicati
43330 6f 6e 20 77 69 73 68 65 73 20 74 6f 20 61 62 61  on wishes to aba
43340 6e 64 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20  ndon the backup 
43350 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 5b  operation, the [
43360 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 0a  sqlite3_backup].
43370 2a 2a 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64  ** object should
43380 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71   be passed to sq
43390 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
433a0 69 73 68 28 29 2e 20 54 68 69 73 20 72 65 6c 65  ish(). This rele
433b0 61 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 6f  ases all.** reso
433c0 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64  urces associated
433d0 20 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70   with the backup
433e0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 73   operation. If s
433f0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
43400 65 70 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 20  ep().** has not 
43410 79 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51  yet returned [SQ
43420 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e  LITE_DONE], then
43430 20 61 6e 79 20 61 63 74 69 76 65 20 77 72 69 74   any active writ
43440 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e-transaction on
43450 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74   the.** destinat
43460 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20  ion database is 
43470 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 65  rolled back. The
43480 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70   [sqlite3_backup
43490 5d 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 61  ] object is inva
434a0 6c 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 6e  lid.** and may n
434b0 6f 74 20 62 65 20 75 73 65 64 20 66 6f 6c 6c 6f  ot be used follo
434c0 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73  wing a call to s
434d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
434e0 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  nish()..**.** Th
434f0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
43500 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   by sqlite3_back
43510 75 70 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 51  up_finish is [SQ
43520 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 65  LITE_OK] if no e
43530 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 64  rror.** occurred
43540 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20  , regardless or 
43550 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73  whether or not s
43560 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
43570 65 70 28 29 20 77 61 73 20 63 61 6c 6c 65 64 0a  ep() was called.
43580 2a 2a 20 61 20 73 75 66 66 69 63 69 65 6e 74 20  ** a sufficient 
43590 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
435a0 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20  to complete the 
435b0 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
435c0 2e 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 6f  . Or, if.** an o
435d0 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e  ut-of-memory con
435e0 64 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72  dition or IO err
435f0 6f 72 20 6f 63 63 75 72 65 64 20 64 75 72 69 6e  or occured durin
43600 67 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73  g a call to.** s
43610 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
43620 65 70 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 54  ep() then [SQLIT
43630 45 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a 2a  E_NOMEM] or an.*
43640 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  * [SQLITE_IOERR_
43650 41 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f  ACCESS | SQLITE_
43660 49 4f 45 52 52 5f 58 58 58 5d 20 65 72 72 6f 72  IOERR_XXX] error
43670 20 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75   code.** is retu
43680 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  rned. In this ca
43690 73 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  se the error cod
436a0 65 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d  e and an error m
436b0 65 73 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 72  essage are.** wr
436c0 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 65 73  itten to the des
436d0 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61  tination [databa
436e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a  se connection]..
436f0 2a 2a 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 6f  **.** A return o
43700 66 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  f [SQLITE_BUSY] 
43710 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  or [SQLITE_LOCKE
43720 44 5d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  D] from sqlite3_
43730 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 73  backup_step() is
43740 0a 2a 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e  .** not a perman
43750 65 6e 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f  ent error and do
43760 65 73 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68  es not affect th
43770 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
43780 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63  f.** sqlite3_bac
43790 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a  kup_finish()..**
437a0 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62  .** <b>sqlite3_b
437b0 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28  ackup_remaining(
437c0 29 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ), sqlite3_backu
437d0 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f 62  p_pagecount()</b
437e0 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c  >.**.** Each cal
437f0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
43800 6b 75 70 5f 73 74 65 70 28 29 20 73 65 74 73 20  kup_step() sets 
43810 74 77 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 65  two values store
43820 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20  d internally.** 
43830 62 79 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62  by an [sqlite3_b
43840 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 54  ackup] object. T
43850 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
43860 65 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62  es still to be b
43870 61 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 69  acked.** up, whi
43880 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 65  ch may be querie
43890 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63  d by sqlite3_bac
438a0 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c  kup_remaining(),
438b0 20 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a 2a   and the total.*
438c0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * number of page
438d0 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20  s in the source 
438e0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77  database file, w
438f0 68 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 72  hich may be quer
43900 69 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65  ied by.** sqlite
43910 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75  3_backup_pagecou
43920 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nt()..**.** The 
43930 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20  values returned 
43940 62 79 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  by these functio
43950 6e 73 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 61  ns are only upda
43960 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65  ted by.** sqlite
43970 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e  3_backup_step().
43980 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64   If the source d
43990 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66  atabase is modif
439a0 69 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63  ied during a bac
439b0 6b 75 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e  kup.** operation
439c0 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
439d0 73 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65  s are not update
439e0 64 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72  d to account for
439f0 20 61 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 61   any extra.** pa
43a00 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ges that need to
43a10 20 62 65 20 75 70 64 61 74 65 64 20 6f 72 20 74   be updated or t
43a20 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73  he size of the s
43a30 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66  ource database f
43a40 69 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 2e  ile.** changing.
43a50 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 72  .**.** <b>Concur
43a60 72 65 6e 74 20 55 73 61 67 65 20 6f 66 20 44 61  rent Usage of Da
43a70 74 61 62 61 73 65 20 48 61 6e 64 6c 65 73 3c 2f  tabase Handles</
43a80 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 75  b>.**.** The sou
43a90 72 63 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  rce [database co
43aa0 6e 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 65  nnection] may be
43ab0 20 75 73 65 64 20 62 79 20 74 68 65 20 61 70 70   used by the app
43ac0 6c 69 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 68  lication for oth
43ad0 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 77  er.** purposes w
43ae0 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 6f 70  hile a backup op
43af0 65 72 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 72  eration is under
43b00 77 61 79 20 6f 72 20 62 65 69 6e 67 20 69 6e 69  way or being ini
43b10 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20  tialized..** If 
43b20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
43b30 65 64 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 65  ed and configure
43b40 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 72  d to support thr
43b50 65 61 64 73 61 66 65 20 64 61 74 61 62 61 73 65  eadsafe database
43b60 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c  .** connections,
43b70 20 74 68 65 6e 20 74 68 65 20 73 6f 75 72 63 65   then the source
43b80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
43b90 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64  tion may be used
43ba0 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a   concurrently.**
43bb0 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 68   from within oth
43bc0 65 72 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a  er threads..**.*
43bd0 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61  * However, the a
43be0 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20  pplication must 
43bf0 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74  guarantee that t
43c00 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
43c10 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65  atabase.** conne
43c20 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 20  ction handle is 
43c30 6e 6f 74 20 70 61 73 73 65 64 20 74 6f 20 61 6e  not passed to an
43c40 79 20 6f 74 68 65 72 20 41 50 49 20 28 62 79 20  y other API (by 
43c50 61 6e 79 20 74 68 72 65 61 64 29 20 61 66 74 65  any thread) afte
43c60 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61  r .** sqlite3_ba
43c70 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 73 20 63  ckup_init() is c
43c80 61 6c 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 65  alled and before
43c90 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
43ca0 6e 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71  ng call to.** sq
43cb0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
43cc0 69 73 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e 61  ish(). Unfortuna
43cd0 74 65 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 73  tely SQLite does
43ce0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63   not currently c
43cf0 68 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 73  heck.** for this
43d00 2c 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61  , if the applica
43d10 74 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68  tion does use th
43d20 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64  e destination [d
43d30 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
43d40 6f 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 20  on].** for some 
43d50 6f 74 68 65 72 20 70 75 72 70 6f 73 65 20 64 75  other purpose du
43d60 72 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70  ring a backup op
43d70 65 72 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 20  eration, things 
43d80 6d 61 79 20 61 70 70 65 61 72 20 74 6f 0a 2a 2a  may appear to.**
43d90 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20   work correctly 
43da0 62 75 74 20 69 6e 20 66 61 63 74 20 62 65 20 73  but in fact be s
43db0 75 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 6f  ubtly malfunctio
43dc0 6e 69 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 68  ning.  Use of th
43dd0 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e  e.** destination
43de0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
43df0 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63  tion while a bac
43e00 6b 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65  kup is in progre
43e10 73 73 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f  ss might.** also
43e20 20 63 61 75 73 65 20 61 20 6d 75 74 65 78 20 64   cause a mutex d
43e30 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46  eadlock..**.** F
43e40 75 72 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 72  urthermore, if r
43e50 75 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65  unning in [share
43e60 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74  d cache mode], t
43e70 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  he application m
43e80 75 73 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65  ust.** guarantee
43e90 20 74 68 61 74 20 74 68 65 20 73 68 61 72 65 64   that the shared
43ea0 20 63 61 63 68 65 20 75 73 65 64 20 62 79 20 74   cache used by t
43eb0 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
43ec0 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f  atabase.** is no
43ed0 74 20 61 63 63 65 73 73 65 64 20 77 68 69 6c 65  t accessed while
43ee0 20 74 68 65 20 62 61 63 6b 75 70 20 69 73 20 72   the backup is r
43ef0 75 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74  unning. In pract
43f00 69 63 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a  ice this means.*
43f10 2a 20 74 68 61 74 20 74 68 65 20 61 70 70 6c 69  * that the appli
43f20 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72  cation must guar
43f30 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 66  antee that the f
43f40 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 20  ile-system file 
43f50 62 65 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 64  being .** backed
43f60 20 75 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 63   up to is not ac
43f70 63 65 73 73 65 64 20 62 79 20 61 6e 79 20 63 6f  cessed by any co
43f80 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20  nnection within 
43f90 74 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20  the process,.** 
43fa0 6e 6f 74 20 6a 75 73 74 20 74 68 65 20 73 70 65  not just the spe
43fb0 63 69 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f 6e  cific connection
43fc0 20 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64   that was passed
43fd0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
43fe0 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a  up_init()..**.**
43ff0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61   The [sqlite3_ba
44000 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 73  ckup] object its
44010 65 6c 66 20 69 73 20 70 61 72 74 69 61 6c 6c 79  elf is partially
44020 20 74 68 72 65 61 64 73 61 66 65 2e 20 4d 75 6c   threadsafe. Mul
44030 74 69 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 64  tiple .** thread
44040 73 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 6b  s may safely mak
44050 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75  e multiple concu
44060 72 72 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73  rrent calls to s
44070 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
44080 65 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72  ep()..** However
44090 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61  , the sqlite3_ba
440a0 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29  ckup_remaining()
440b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63   and sqlite3_bac
440c0 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0a  kup_pagecount().
440d0 2a 2a 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20  ** APIs are not 
440e0 73 74 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e  strictly speakin
440f0 67 20 74 68 72 65 61 64 73 61 66 65 2e 20 49 66  g threadsafe. If
44100 20 74 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65   they are invoke
44110 64 20 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65  d at the.** same
44120 20 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72   time as another
44130 20 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b   thread is invok
44140 69 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ing sqlite3_back
44150 75 70 5f 73 74 65 70 28 29 20 69 74 20 69 73 0a  up_step() it is.
44160 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74  ** possible that
44170 20 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76   they return inv
44180 61 6c 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a  alid values..*/.
44190 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
441a0 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 74  e3_backup *sqlit
441b0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a  e3_backup_init(.
441c0 20 20 73 71 6c 69 74 65 33 20 2a 70 44 65 73 74    sqlite3 *pDest
441d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
441e0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74           /* Dest
441f0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
44200 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
44210 73 74 20 63 68 61 72 20 2a 7a 44 65 73 74 4e 61  st char *zDestNa
44220 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  me,             
44230 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69      /* Destinati
44240 6f 6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  on database name
44250 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70   */.  sqlite3 *p
44260 53 6f 75 72 63 65 2c 20 20 20 20 20 20 20 20 20  Source,         
44270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
44280 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  Source database 
44290 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
442a0 74 20 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 4e  t char *zSourceN
442b0 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ame             
442c0 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74     /* Source dat
442d0 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b  abase name */.);
442e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
442f0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
44300 74 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  tep(sqlite3_back
44310 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65  up *p, int nPage
44320 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
44330 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t sqlite3_backup
44340 5f 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f  _finish(sqlite3_
44350 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49  backup *p);.SQLI
44360 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
44370 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  e3_backup_remain
44380 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ing(sqlite3_back
44390 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41  up *p);.SQLITE_A
443a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
443b0 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28  ackup_pagecount(
443c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
443d0 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  p);../*.** CAPI3
443e0 52 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69  REF: Unlock Noti
443f0 66 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 45  fication.** EXPE
44400 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 57  RIMENTAL.**.** W
44410 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73  hen running in s
44420 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65  hared-cache mode
44430 2c 20 61 20 64 61 74 61 62 61 73 65 20 6f 70 65  , a database ope
44440 72 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c 20  ration may fail 
44450 77 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c 49  with.** an [SQLI
44460 54 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f 72  TE_LOCKED] error
44470 20 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64   if the required
44480 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 68   locks on the sh
44490 61 72 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a 2a  ared-cache or.**
444a0 20 69 6e 64 69 76 69 64 75 61 6c 20 74 61 62 6c   individual tabl
444b0 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 68  es within the sh
444c0 61 72 65 64 2d 63 61 63 68 65 20 63 61 6e 6e 6f  ared-cache canno
444d0 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 53  t be obtained. S
444e0 65 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 68  ee.** [SQLite Sh
444f0 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d  ared-Cache Mode]
44500 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69   for a descripti
44510 6f 6e 20 6f 66 20 73 68 61 72 65 64 2d 63 61 63  on of shared-cac
44520 68 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a 20  he locking. .** 
44530 54 68 69 73 20 41 50 49 20 6d 61 79 20 62 65 20  This API may be 
44540 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72  used to register
44550 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74   a callback that
44560 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76   SQLite will inv
44570 6f 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65  oke .** when the
44580 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72   connection curr
44590 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 68  ently holding th
445a0 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 20  e required lock 
445b0 72 65 6c 69 6e 71 75 69 73 68 65 73 20 69 74 2e  relinquishes it.
445c0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20  .** This API is 
445d0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
445e0 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 61  f the library wa
445f0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
44600 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45  the.** [SQLITE_E
44610 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54  NABLE_UNLOCK_NOT
44620 49 46 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 73  IFY] C-preproces
44630 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e  sor symbol defin
44640 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c  ed..**.** See Al
44650 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53  so: [Using the S
44660 51 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74  QLite Unlock Not
44670 69 66 69 63 61 74 69 6f 6e 20 46 65 61 74 75 72  ification Featur
44680 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64  e]..**.** Shared
44690 2d 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65  -cache locks are
446a0 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61   released when a
446b0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
446c0 74 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a 2a  tion concludes.*
446d0 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72  * its current tr
446e0 61 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65  ansaction, eithe
446f0 72 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20  r by committing 
44700 69 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74  it or rolling it
44710 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 68   back. .**.** Wh
44720 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  en a connection 
44730 28 6b 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 6c  (known as the bl
44740 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
44750 29 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69  ) fails to obtai
44760 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61  n a.** shared-ca
44770 63 68 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 4c  che lock and SQL
44780 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65  ITE_LOCKED is re
44790 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61  turned to the ca
447a0 6c 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 65  ller, the.** ide
447b0 6e 74 69 74 79 20 6f 66 20 74 68 65 20 64 61 74  ntity of the dat
447c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
447d0 20 28 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63   (the blocking c
447e0 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 0a  onnection) that.
447f0 2a 2a 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 68  ** has locked th
44800 65 20 72 65 71 75 69 72 65 64 20 72 65 73 6f 75  e required resou
44810 72 63 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  rce is stored in
44820 74 65 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 20  ternally. After 
44830 61 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  an .** applicati
44840 6f 6e 20 72 65 63 65 69 76 65 73 20 61 6e 20 53  on receives an S
44850 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72  QLITE_LOCKED err
44860 6f 72 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c 20  or, it may call 
44870 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75  the.** sqlite3_u
44880 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d  nlock_notify() m
44890 65 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 62  ethod with the b
448a0 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
448b0 6e 20 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20  n handle as .** 
448c0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
448d0 6e 74 20 74 6f 20 72 65 67 69 73 74 65 72 20 66  nt to register f
448e0 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68  or a callback th
448f0 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b  at will be invok
44900 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 62  ed.** when the b
44910 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69  locking connecti
44920 6f 6e 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e  ons current tran
44930 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c  saction is concl
44940 75 64 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c  uded. The.** cal
44950 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
44960 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
44970 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20   [sqlite3_step] 
44980 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73  or [sqlite3_clos
44990 65 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20  e].** call that 
449a0 63 6f 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c  concludes the bl
449b0 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
449c0 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  ns transaction..
449d0 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33  **.** If sqlite3
449e0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
449f0 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20   is called in a 
44a00 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61  multi-threaded a
44a10 70 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74  pplication,.** t
44a20 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65  here is a chance
44a30 20 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69   that the blocki
44a40 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69  ng connection wi
44a50 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a  ll have already.
44a60 2a 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73  ** concluded its
44a70 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20   transaction by 
44a80 74 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33  the time sqlite3
44a90 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
44aa0 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20   is invoked..** 
44ab0 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c  If this happens,
44ac0 20 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 66   then the specif
44ad0 69 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ied callback is 
44ae0 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74  invoked immediat
44af0 65 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74  ely,.** from wit
44b00 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  hin the call to 
44b10 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
44b20 6f 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  otify()..**.** I
44b30 66 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f  f the blocked co
44b40 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 74 74 65  nnection is atte
44b50 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e  mpting to obtain
44b60 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e   a write-lock on
44b70 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63   a.** shared-cac
44b80 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d 6f  he table, and mo
44b90 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 65  re than one othe
44ba0 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72  r connection cur
44bb0 72 65 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a 20  rently holds.** 
44bc0 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74  a read-lock on t
44bd0 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 74  he same table, t
44be0 68 65 6e 20 53 51 4c 69 74 65 20 61 72 62 69 74  hen SQLite arbit
44bf0 72 61 72 69 6c 79 20 73 65 6c 65 63 74 73 20 6f  rarily selects o
44c00 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f 74  ne of .** the ot
44c10 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  her connections 
44c20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 62 6c  to use as the bl
44c30 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
44c40 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d  n..**.** There m
44c50 61 79 20 62 65 20 61 74 20 6d 6f 73 74 20 6f 6e  ay be at most on
44c60 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  e unlock-notify 
44c70 63 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65  callback registe
44c80 72 65 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c 6f  red by a .** blo
44c90 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  cked connection.
44ca0 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f   If sqlite3_unlo
44cb0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63  ck_notify() is c
44cc0 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 0a 2a  alled when the.*
44cd0 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  * blocked connec
44ce0 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 73  tion already has
44cf0 20 61 20 72 65 67 69 73 74 65 72 65 64 20 75 6e   a registered un
44d00 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
44d10 62 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  back,.** then th
44d20 65 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 72  e new callback r
44d30 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 2e  eplaces the old.
44d40 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f   If sqlite3_unlo
44d50 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a 2a  ck_notify() is.*
44d60 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20  * called with a 
44d70 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 20  NULL pointer as 
44d80 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  its second argum
44d90 65 6e 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 78  ent, then any ex
44da0 69 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 6b  isting.** unlock
44db0 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
44dc0 20 69 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 54   is cancelled. T
44dd0 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  he blocked conne
44de0 63 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f 63  ctions .** unloc
44df0 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
44e00 6b 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 61  k may also be ca
44e10 6e 63 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 6e  nceled by closin
44e20 67 20 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a 2a  g the blocked.**
44e30 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 6e   connection usin
44e40 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  g [sqlite3_close
44e50 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75  ()]..**.** The u
44e60 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
44e70 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65  lback is not ree
44e80 6e 74 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70  ntrant. If an ap
44e90 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65  plication invoke
44ea0 73 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33  s.** any sqlite3
44eb0 5f 78 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f  _xxx API functio
44ec0 6e 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61  ns from within a
44ed0 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  n unlock-notify 
44ee0 63 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 63  callback, a.** c
44ef0 72 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b  rash or deadlock
44f00 20 6d 61 79 20 62 65 20 74 68 65 20 72 65 73 75   may be the resu
44f10 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73  lt..**.** Unless
44f20 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 74   deadlock is det
44f30 65 63 74 65 64 20 28 73 65 65 20 62 65 6c 6f 77  ected (see below
44f40 29 2c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  ), sqlite3_unloc
44f50 6b 5f 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 79  k_notify() alway
44f60 73 0a 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 4c  s.** returns SQL
44f70 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62  ITE_OK..**.** <b
44f80 3e 43 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 61  >Callback Invoca
44f90 74 69 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 3e  tion Details</b>
44fa0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 75  .**.** When an u
44fb0 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
44fc0 6c 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 65  lback is registe
44fd0 72 65 64 2c 20 74 68 65 20 61 70 70 6c 69 63 61  red, the applica
44fe0 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20  tion provides a 
44ff0 0a 2a 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 2a  .** single void*
45000 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73   pointer that is
45010 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 63   passed to the c
45020 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 20  allback when it 
45030 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 48  is invoked..** H
45040 6f 77 65 76 65 72 2c 20 74 68 65 20 73 69 67 6e  owever, the sign
45050 61 74 75 72 65 20 6f 66 20 74 68 65 20 63 61 6c  ature of the cal
45060 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61  lback function a
45070 6c 6c 6f 77 73 20 53 51 4c 69 74 65 20 74 6f 20  llows SQLite to 
45080 70 61 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 72  pass.** it an ar
45090 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e  ray of void* con
450a0 74 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 54  text pointers. T
450b0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
450c0 74 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 61  t passed to.** a
450d0 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  n unlock-notify 
450e0 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 6f  callback is a po
450f0 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61  inter to an arra
45100 79 20 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e 74  y of void* point
45110 65 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ers,.** and the 
45120 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75  second is the nu
45130 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
45140 69 6e 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a  in the array..**
45150 0a 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b  .** When a block
45160 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ing connections 
45170 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63  transaction is c
45180 6f 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20  oncluded, there 
45190 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 74  may be.** more t
451a0 68 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20  han one blocked 
451b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  connection that 
451c0 68 61 73 20 72 65 67 69 73 74 65 72 65 64 20 66  has registered f
451d0 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  or an unlock-not
451e0 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e  ify.** callback.
451f0 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   If two or more 
45200 73 75 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f 6e  such blocked con
45210 6e 65 63 74 69 6f 6e 73 20 68 61 76 65 20 73 70  nections have sp
45220 65 63 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 73  ecified the.** s
45230 61 6d 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  ame callback fun
45240 63 74 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 74  ction, then inst
45250 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20  ead of invoking 
45260 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
45270 63 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c  ction.** multipl
45280 65 20 74 69 6d 65 73 2c 20 69 74 20 69 73 20 69  e times, it is i
45290 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 68  nvoked once with
452a0 20 74 68 65 20 73 65 74 20 6f 66 20 76 6f 69 64   the set of void
452b0 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  * context pointe
452c0 72 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  rs.** specified 
452d0 62 79 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63  by the blocked c
452e0 6f 6e 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 6c  onnections bundl
452f0 65 64 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f  ed together into
45300 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 68   an array..** Th
45310 69 73 20 67 69 76 65 73 20 74 68 65 20 61 70 70  is gives the app
45320 6c 69 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 6f  lication an oppo
45330 72 74 75 6e 69 74 79 20 74 6f 20 70 72 69 6f 72  rtunity to prior
45340 69 74 69 7a 65 20 61 6e 79 20 61 63 74 69 6f 6e  itize any action
45350 73 20 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 6f  s .** related to
45360 20 74 68 65 20 73 65 74 20 6f 66 20 75 6e 62 6c   the set of unbl
45370 6f 63 6b 65 64 20 64 61 74 61 62 61 73 65 20 63  ocked database c
45380 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  onnections..**.*
45390 2a 20 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 65  * <b>Deadlock De
453a0 74 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a  tection</b>.**.*
453b0 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 61 74 20  * Assuming that 
453c0 61 66 74 65 72 20 72 65 67 69 73 74 65 72 69 6e  after registerin
453d0 67 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d  g for an unlock-
453e0 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20  notify callback 
453f0 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77  a .** database w
45400 61 69 74 73 20 66 6f 72 20 74 68 65 20 63 61 6c  aits for the cal
45410 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 73 73 75  lback to be issu
45420 65 64 20 62 65 66 6f 72 65 20 74 61 6b 69 6e 67  ed before taking
45430 20 61 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a 20   any further.** 
45440 61 63 74 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e  action (a reason
45450 61 62 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 29  able assumption)
45460 2c 20 74 68 65 6e 20 75 73 69 6e 67 20 74 68 69  , then using thi
45470 73 20 41 50 49 20 6d 61 79 20 63 61 75 73 65 20  s API may cause 
45480 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  the.** applicati
45490 6f 6e 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20  on to deadlock. 
454a0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
454b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20  connection X is 
454c0 77 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 63  waiting for.** c
454d0 6f 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72  onnection Y's tr
454e0 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20  ansaction to be 
454f0 63 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73  concluded, and s
45500 69 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74  imilarly connect
45510 69 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 74  ion.** Y is wait
45520 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f  ing on connectio
45530 6e 20 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f  n X's transactio
45540 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20  n, then neither 
45550 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 69  connection.** wi
45560 6c 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20 74  ll proceed and t
45570 68 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72 65  he system may re
45580 6d 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 20  main deadlocked 
45590 69 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a 2a  indefinitely..**
455a0 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 69  .** To avoid thi
455b0 73 20 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 20  s scenario, the 
455c0 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
455d0 6f 74 69 66 79 28 29 20 70 65 72 66 6f 72 6d 73  otify() performs
455e0 20 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 74   deadlock.** det
455f0 65 63 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 76  ection. If a giv
45600 65 6e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  en call to sqlit
45610 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
45620 28 29 20 77 6f 75 6c 64 20 70 75 74 20 74 68 65  () would put the
45630 0a 2a 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 20  .** system in a 
45640 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65  deadlocked state
45650 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c 4f  , then SQLITE_LO
45660 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 64  CKED is returned
45670 20 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63   and no.** unloc
45680 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
45690 6b 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e  k is registered.
456a0 20 54 68 65 20 73 79 73 74 65 6d 20 69 73 20 73   The system is s
456b0 61 69 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a 20  aid to be in.** 
456c0 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61  a deadlocked sta
456d0 74 65 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e  te if connection
456e0 20 41 20 68 61 73 20 72 65 67 69 73 74 65 72 65   A has registere
456f0 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d  d for an unlock-
45700 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61  notify.** callba
45710 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75  ck on the conclu
45720 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69  sion of connecti
45730 6f 6e 20 42 27 73 20 74 72 61 6e 73 61 63 74 69  on B's transacti
45740 6f 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 69  on, and connecti
45750 6f 6e 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 65  on.** B has itse
45760 6c 66 20 72 65 67 69 73 74 65 72 65 64 20 66 6f  lf registered fo
45770 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  r an unlock-noti
45780 66 79 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e  fy callback when
45790 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 41   connection.** A
457a0 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  's transaction i
457b0 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e 64  s concluded. Ind
457c0 69 72 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 69  irect deadlock i
457d0 73 20 61 6c 73 6f 20 64 65 74 65 63 74 65 64 2c  s also detected,
457e0 20 73 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 65   so.** the syste
457f0 6d 20 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64  m is also consid
45800 65 72 65 64 20 74 6f 20 62 65 20 64 65 61 64 6c  ered to be deadl
45810 6f 63 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 74  ocked if connect
45820 69 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 67  ion B has.** reg
45830 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75  istered for an u
45840 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
45850 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e  lback on the con
45860 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65  clusion of conne
45870 63 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 61  ction.** C's tra
45880 6e 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20  nsaction, where 
45890 63 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20  connection C is 
458a0 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65  waiting on conne
458b0 63 74 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a 20  ction A. Any.** 
458c0 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73  number of levels
458d0 20 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e 20   of indirection 
458e0 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a  are allowed..**.
458f0 2a 2a 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 20  ** <b>The "DROP 
45900 54 41 42 4c 45 22 20 45 78 63 65 70 74 69 6f 6e  TABLE" Exception
45910 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  </b>.**.** When 
45920 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
45930 65 33 5f 73 74 65 70 28 29 5d 20 72 65 74 75 72  e3_step()] retur
45940 6e 73 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  ns SQLITE_LOCKED
45950 2c 20 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a  , it is almost .
45960 2a 2a 20 61 6c 77 61 79 73 20 61 70 70 72 6f 70  ** always approp
45970 72 69 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 71  riate to call sq
45980 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
45990 69 66 79 28 29 2e 20 54 68 65 72 65 20 69 73 20  ify(). There is 
459a0 68 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20  however,.** one 
459b0 65 78 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20  exception. When 
459c0 65 78 65 63 75 74 69 6e 67 20 61 20 22 44 52 4f  executing a "DRO
459d0 50 20 54 41 42 4c 45 22 20 6f 72 20 22 44 52 4f  P TABLE" or "DRO
459e0 50 20 49 4e 44 45 58 22 20 73 74 61 74 65 6d 65  P INDEX" stateme
459f0 6e 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68  nt,.** SQLite ch
45a00 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 61 72  ecks if there ar
45a10 65 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20  e any currently 
45a20 65 78 65 63 75 74 69 6e 67 20 53 45 4c 45 43 54  executing SELECT
45a30 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74   statements.** t
45a40 68 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68  hat belong to th
45a50 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f  e same connectio
45a60 6e 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 2c  n. If there are,
45a70 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69   SQLITE_LOCKED i
45a80 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 49  s.** returned. I
45a90 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72  n this case ther
45aa0 65 20 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e  e is no "blockin
45ab0 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73  g connection", s
45ac0 6f 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71  o invoking.** sq
45ad0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
45ae0 69 66 79 28 29 20 72 65 73 75 6c 74 73 20 69 6e  ify() results in
45af0 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69   the unlock-noti
45b00 66 79 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e  fy callback bein
45b10 67 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d  g.** invoked imm
45b20 65 64 69 61 74 65 6c 79 2e 20 49 66 20 74 68 65  ediately. If the
45b30 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 65   application the
45b40 6e 20 72 65 2d 61 74 74 65 6d 70 74 73 20 74 68  n re-attempts th
45b50 65 20 22 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a  e "DROP TABLE".*
45b60 2a 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58  * or "DROP INDEX
45b70 22 20 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 69  " query, an infi
45b80 6e 69 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20  nite loop might 
45b90 62 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a  be the result..*
45ba0 2a 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f  *.** One way aro
45bb0 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d  und this problem
45bc0 20 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65   is to check the
45bd0 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20   extended error 
45be0 63 6f 64 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a  code returned.**
45bf0 20 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73   by an sqlite3_s
45c00 74 65 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 74  tep() call. If t
45c10 68 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69  here is a blocki
45c20 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74  ng connection, t
45c30 68 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e  hen the.** exten
45c40 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ded error code i
45c50 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  s set to SQLITE_
45c60 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
45c70 48 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  HE. Otherwise, i
45c80 6e 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c  n.** the special
45c90 20 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44   "DROP TABLE/IND
45ca0 45 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 78  EX" case, the ex
45cb0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
45cc0 65 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51  e is just .** SQ
45cd0 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a  LITE_LOCKED..*/.
45ce0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
45cf0 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
45d00 74 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20  tify(.  sqlite3 
45d10 2a 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 20  *pBlocked,      
45d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45d30 20 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 63      /* Waiting c
45d40 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76  onnection */.  v
45d50 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76  oid (*xNotify)(v
45d60 6f 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e 74  oid **apArg, int
45d70 20 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 61   nArg),    /* Ca
45d80 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
45d90 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 76  to invoke */.  v
45da0 6f 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 20  oid *pNotifyArg 
45db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45dc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
45dd0 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74  gument to pass t
45de0 6f 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b 0a  o xNotify */.);.
45df0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
45e00 3a 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 69  : String Compari
45e10 73 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  son.** EXPERIMEN
45e20 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73  TAL.**.** The [s
45e30 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28  qlite3_strnicmp(
45e40 29 5d 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70  )] API allows ap
45e50 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 65  plications and e
45e60 78 74 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a 20  xtensions to.** 
45e70 63 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74  compare the cont
45e80 65 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66  ents of two buff
45e90 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 55  ers containing U
45ea0 54 46 2d 38 20 73 74 72 69 6e 67 73 20 69 6e 20  TF-8 strings in 
45eb0 61 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e 64  a.** case-indend
45ec0 65 6e 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 69  ent fashion, usi
45ed0 6e 67 20 74 68 65 20 73 61 6d 65 20 64 65 66 69  ng the same defi
45ee0 6e 69 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 69  nition of case i
45ef0 6e 64 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a 20  ndependence .** 
45f00 74 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 73  that SQLite uses
45f10 20 69 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e   internally when
45f20 20 63 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e 74   comparing ident
45f30 69 66 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ifiers..*/.SQLIT
45f40 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
45f50 33 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74  3_strnicmp(const
45f60 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63   char *, const c
45f70 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a  har *, int);../*
45f80 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63  .** Undo the hac
45f90 6b 20 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20  k that converts 
45fa0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74  floating point t
45fb0 79 70 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20  ypes to integer 
45fc0 66 6f 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e  for.** builds on
45fd0 20 70 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68   processors with
45fe0 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  out floating poi
45ff0 6e 74 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23  nt support..*/.#
46000 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
46010 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
46020 0a 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a  .# undef double.
46030 23 65 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20  #endif..#if 0.} 
46040 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27   /* End of the '
46050 65 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63  extern "C"' bloc
46060 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64  k */.#endif.#end
46070 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if.../**********
46080 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69  **** End of sqli
46090 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te3.h **********
460a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
460b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
460c0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
460d0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
460e0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
460f0 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
46100 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
46110 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
46120 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73  **** Include has
46130 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  h.h in the middl
46140 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
46150 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
46160 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
46170 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
46180 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  hash.h *********
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
461a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
461b0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
461c0 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a  September 22.**.
461d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
461e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
461f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
46200 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
46210 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
46220 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
46230 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
46240 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
46250 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
46260 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
46270 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
46280 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
46290 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
462a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
462b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
462c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
462d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
462e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
462f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
46330 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65  his is the heade
46340 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67  r file for the g
46350 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
46360 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a  e implemenation.
46370 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  ** used in SQLit
46380 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53  e..*/.#ifndef _S
46390 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64  QLITE_HASH_H_.#d
463a0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41  efine _SQLITE_HA
463b0 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72  SH_H_../* Forwar
463c0 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f  d declarations o
463d0 66 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f  f structures. */
463e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
463f0 48 61 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64  Hash Hash;.typed
46400 65 66 20 73 74 72 75 63 74 20 48 61 73 68 45 6c  ef struct HashEl
46410 65 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a  em HashElem;../*
46420 20 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68   A complete hash
46430 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73   table is an ins
46440 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
46450 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
46460 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61  ..** The interna
46470 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ls of this struc
46480 74 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65  ture are intende
46490 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d  d to be opaque -
464a0 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65  - client.** code
464b0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65   should not atte
464c0 6d 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72  mpt to access or
464d0 20 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c   modify the fiel
464e0 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ds of this struc
464f0 74 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79  ture.** directly
46500 2e 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73  .  Change this s
46510 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79  tructure only by
46520 20 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69   using the routi
46530 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f  nes below..** Ho
46540 77 65 76 65 72 2c 20 73 6f 6d 65 20 6f 66 20 74  wever, some of t
46550 68 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20  he "procedures" 
46560 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20  and "functions" 
46570 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e  for modifying an
46580 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74  d.** accessing t
46590 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72  his structure ar
465a0 65 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c  e really macros,
465b0 20 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61   so we can't rea
465c0 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73  lly make.** this
465d0 20 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75   structure opaqu
465e0 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 6c 65  e..**.** All ele
465f0 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 68 61 73  ments of the has
46600 68 20 74 61 62 6c 65 20 61 72 65 20 6f 6e 20 61  h table are on a
46610 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c   single doubly-l
46620 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 48  inked list..** H
46630 61 73 68 2e 66 69 72 73 74 20 70 6f 69 6e 74 73  ash.first points
46640 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20   to the head of 
46650 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  this list..**.**
46660 20 54 68 65 72 65 20 61 72 65 20 48 61 73 68 2e   There are Hash.
46670 68 74 73 69 7a 65 20 62 75 63 6b 65 74 73 2e 20  htsize buckets. 
46680 20 45 61 63 68 20 62 75 63 6b 65 74 20 70 6f 69   Each bucket poi
46690 6e 74 73 20 74 6f 20 61 20 73 70 6f 74 20 69 6e  nts to a spot in
466a0 0a 2a 2a 20 74 68 65 20 67 6c 6f 62 61 6c 20 64  .** the global d
466b0 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73  oubly-linked lis
466c0 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 73  t.  The contents
466d0 20 6f 66 20 74 68 65 20 62 75 63 6b 65 74 20 61   of the bucket a
466e0 72 65 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e  re the.** elemen
466f0 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 70 6c 75  t pointed to plu
46700 73 20 74 68 65 20 6e 65 78 74 20 5f 68 74 2e 63  s the next _ht.c
46710 6f 75 6e 74 2d 31 20 65 6c 65 6d 65 6e 74 73 20  ount-1 elements 
46720 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a  in the list..**.
46730 2a 2a 20 48 61 73 68 2e 68 74 73 69 7a 65 20 61  ** Hash.htsize a
46740 6e 64 20 48 61 73 68 2e 68 74 20 6d 61 79 20 62  nd Hash.ht may b
46750 65 20 7a 65 72 6f 2e 20 20 49 6e 20 74 68 61 74  e zero.  In that
46760 20 63 61 73 65 20 6c 6f 6f 6b 75 70 20 69 73 20   case lookup is 
46770 64 6f 6e 65 0a 2a 2a 20 62 79 20 61 20 6c 69 6e  done.** by a lin
46780 65 61 72 20 73 65 61 72 63 68 20 6f 66 20 74 68  ear search of th
46790 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 2e 20 20  e global list.  
467a0 46 6f 72 20 73 6d 61 6c 6c 20 74 61 62 6c 65 73  For small tables
467b0 2c 20 74 68 65 20 0a 2a 2a 20 48 61 73 68 2e 68  , the .** Hash.h
467c0 74 20 74 61 62 6c 65 20 69 73 20 6e 65 76 65 72  t table is never
467d0 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 63 61 75   allocated becau
467e0 73 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20  se if there are 
467f0 66 65 77 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20  few elements.** 
46800 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 69 74  in the table, it
46810 20 69 73 20 66 61 73 74 65 72 20 74 6f 20 64 6f   is faster to do
46820 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68   a linear search
46830 20 74 68 61 6e 20 74 6f 20 6d 61 6e 61 67 65 0a   than to manage.
46840 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  ** the hash tabl
46850 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73  e..*/.struct Has
46860 68 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  h {.  unsigned i
46870 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20  nt htsize;      
46880 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 63  /* Number of buc
46890 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 73 68  kets in the hash
468a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 69   table */.  unsi
468b0 67 6e 65 64 20 69 6e 74 20 63 6f 75 6e 74 3b 20  gned int count; 
468c0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
468d0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
468e0 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61  is table */.  Ha
468f0 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20  shElem *first;  
46900 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
46910 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
46920 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  the array */.  s
46930 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20 20 20  truct _ht {     
46940 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
46950 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
46960 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20    int count;    
46970 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46980 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
46990 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68  s with this hash
469a0 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c 65 6d   */.    HashElem
469b0 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 20 20   *chain;        
469c0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
469d0 20 66 69 72 73 74 20 65 6e 74 72 79 20 77 69 74   first entry wit
469e0 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20  h this hash */. 
469f0 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45   } *ht;.};../* E
46a00 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74  ach element in t
46a10 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  he hash table is
46a20 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
46a30 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a  the following .*
46a40 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c  * structure.  Al
46a50 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73  l elements are s
46a60 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c  tored on a singl
46a70 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20  e doubly-linked 
46a80 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69  list..**.** Agai
46a90 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74 75 72  n, this structur
46aa0 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  e is intended to
46ab0 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20   be opaque, but 
46ac0 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a  it can't really.
46ad0 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62 65 63  ** be opaque bec
46ae0 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64 20  ause it is used 
46af0 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74  by macros..*/.st
46b00 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 7b 0a  ruct HashElem {.
46b10 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74    HashElem *next
46b20 2c 20 2a 70 72 65 76 3b 20 20 20 20 20 20 20 2f  , *prev;       /
46b30 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69  * Next and previ
46b40 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  ous elements in 
46b50 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76  the table */.  v
46b60 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20  oid *data;      
46b70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
46b80 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
46b90 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74  ith this element
46ba0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
46bb0 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79   *pKey; int nKey
46bc0 3b 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69  ;  /* Key associ
46bd0 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65  ated with this e
46be0 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  lement */.};../*
46bf0 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69  .** Access routi
46c00 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c  nes.  To delete,
46c10 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70   insert a NULL p
46c20 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
46c30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
46c40 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 48  qlite3HashInit(H
46c50 61 73 68 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ash*);.SQLITE_PR
46c60 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
46c70 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 61  te3HashInsert(Ha
46c80 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  sh*, const char 
46c90 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c  *pKey, int nKey,
46ca0 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53   void *pData);.S
46cb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
46cc0 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46  id *sqlite3HashF
46cd0 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c  ind(const Hash*,
46ce0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65   const char *pKe
46cf0 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51  y, int nKey);.SQ
46d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
46d10 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65  d sqlite3HashCle
46d20 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a  ar(Hash*);../*.*
46d30 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f  * Macros for loo
46d40 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c  ping over all el
46d50 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68  ements of a hash
46d60 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69   table.  The idi
46d70 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68  om is.** like th
46d80 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68  is:.**.**   Hash
46d90 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65   h;.**   HashEle
46da0 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a  m *p;.**   ....*
46db0 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65  *   for(p=sqlite
46dc0 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70  HashFirst(&h); p
46dd0 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; p=sqliteHashNe
46de0 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53  xt(p)){.**     S
46df0 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44  omeStructure *pD
46e00 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68  ata = sqliteHash
46e10 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20  Data(p);.**     
46e20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  // do something 
46e30 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20  with pData.**   
46e40 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  }.*/.#define sql
46e50 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29 20  iteHashFirst(H) 
46e60 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64   ((H)->first).#d
46e70 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
46e80 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e  Next(E)   ((E)->
46e90 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71  next).#define sq
46ea0 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29 20  liteHashData(E) 
46eb0 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 2f 2a    ((E)->data)./*
46ec0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48   #define sqliteH
46ed0 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 28 45  ashKey(E)    ((E
46ee0 29 2d 3e 70 4b 65 79 29 20 2f 2f 20 4e 4f 54 20  )->pKey) // NOT 
46ef0 55 53 45 44 20 2a 2f 0a 2f 2a 20 23 64 65 66 69  USED */./* #defi
46f00 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79  ne sqliteHashKey
46f10 73 69 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b  size(E) ((E)->nK
46f20 65 79 29 20 20 2f 2f 20 4e 4f 54 20 55 53 45 44  ey)  // NOT USED
46f30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65   */../*.** Numbe
46f40 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
46f50 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a  a hash table.*/.
46f60 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  /* #define sqlit
46f70 65 48 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28  eHashCount(H)  (
46f80 28 48 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e  (H)->count) // N
46f90 4f 54 20 55 53 45 44 20 2a 2f 0a 0a 23 65 6e 64  OT USED */..#end
46fa0 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 48 41  if /* _SQLITE_HA
46fb0 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  SH_H_ */../*****
46fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
46fd0 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   hash.h ********
46fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47000 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
47010 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
47020 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
47030 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
47040 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
47050 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
47060 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
47070 65 20 70 61 72 73 65 2e 68 20 69 6e 20 74 68 65  e parse.h in the
47080 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
47090 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
470a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
470b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
470c0 66 69 6c 65 20 70 61 72 73 65 2e 68 20 2a 2a 2a  file parse.h ***
470d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e  ********/.#defin
47100 65 20 54 4b 5f 53 45 4d 49 20 20 20 20 20 20 20  e TK_SEMI       
47110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47120 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 54       1.#define T
47130 4b 5f 45 58 50 4c 41 49 4e 20 20 20 20 20 20 20  K_EXPLAIN       
47140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47150 20 20 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 51    2.#define TK_Q
47160 55 45 52 59 20 20 20 20 20 20 20 20 20 20 20 20  UERY            
47170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
47180 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 41 4e  .#define TK_PLAN
47190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471a0 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64              4.#d
471b0 65 66 69 6e 65 20 54 4b 5f 42 45 47 49 4e 20 20  efine TK_BEGIN  
471c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471d0 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69           5.#defi
471e0 6e 65 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f  ne TK_TRANSACTIO
471f0 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
47200 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
47210 54 4b 5f 44 45 46 45 52 52 45 44 20 20 20 20 20  TK_DEFERRED     
47220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47230 20 20 20 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f     7.#define TK_
47240 49 4d 4d 45 44 49 41 54 45 20 20 20 20 20 20 20  IMMEDIATE       
47250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47260 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43  8.#define TK_EXC
47270 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 20 20  LUSIVE          
47280 20 20 20 20 20 20 20 20 20 20 20 20 20 39 0a 23               9.#
47290 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54  define TK_COMMIT
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472b0 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66           10.#def
472c0 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20 20 20  ine TK_END      
472d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472e0 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65        11.#define
472f0 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 20   TK_ROLLBACK    
47300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47310 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 54 4b     12.#define TK
47320 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20  _SAVEPOINT      
47330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47340 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  13.#define TK_RE
47350 4c 45 41 53 45 20 20 20 20 20 20 20 20 20 20 20  LEASE           
47360 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a               14.
47370 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20 20 20  #define TK_TO   
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47390 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 65            15.#de
473a0 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20  fine TK_TABLE   
473b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473c0 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e         16.#defin
473d0 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20 20 20  e TK_CREATE     
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473f0 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 20 54      17.#define T
47400 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20 20 20  K_IF            
47410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47420 20 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e   18.#define TK_N
47430 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OT              
47440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39                19
47450 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 49 53  .#define TK_EXIS
47460 54 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TS              
47470 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 23 64             20.#d
47480 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20 20 20  efine TK_TEMP   
47490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474a0 20 20 20 20 20 20 20 20 32 31 0a 23 64 65 66 69          21.#defi
474b0 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20 20 20  ne TK_LP        
474c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474d0 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20       22.#define 
474e0 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20 20 20  TK_RP           
474f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47500 20 20 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    23.#define TK_
47510 41 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AS              
47520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
47530 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d  4.#define TK_COM
47540 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MA              
47550 20 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 23              25.#
47560 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20  define TK_ID    
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47580 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66           26.#def
47590 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20  ine TK_INDEXED  
475a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475b0 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65        27.#define
475c0 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 20 20   TK_ABORT       
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475e0 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 54 4b     28.#define TK
475f0 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20  _ACTION         
47600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47610 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 46  29.#define TK_AF
47620 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  TER             
47630 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a               30.
47640 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 41 4c 59  #define TK_ANALY
47650 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ZE              
47660 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 65            31.#de
47670 66 69 6e 65 20 54 4b 5f 41 53 43 20 20 20 20 20  fine TK_ASC     
47680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47690 20 20 20 20 20 20 20 33 32 0a 23 64 65 66 69 6e         32.#defin
476a0 65 20 54 4b 5f 41 54 54 41 43 48 20 20 20 20 20  e TK_ATTACH     
476b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476c0 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 54      33.#define T
476d0 4b 5f 42 45 46 4f 52 45 20 20 20 20 20 20 20 20  K_BEFORE        
476e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476f0 20 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42   34.#define TK_B
47700 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
47710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 35                35
47720 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 43  .#define TK_CASC
47730 41 44 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ADE             
47740 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 64             36.#d
47750 65 66 69 6e 65 20 54 4b 5f 43 41 53 54 20 20 20  efine TK_CAST   
47760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47770 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69          37.#defi
47780 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20  ne TK_COLUMNKW  
47790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477a0 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20       38.#define 
477b0 54 4b 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 20  TK_CONFLICT     
477c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477d0 20 20 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    39.#define TK_
477e0 44 41 54 41 42 41 53 45 20 20 20 20 20 20 20 20  DATABASE        
477f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
47800 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53  0.#define TK_DES
47810 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C               
47820 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0a 23              41.#
47830 64 65 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48  define TK_DETACH
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47850 20 20 20 20 20 20 20 20 20 34 32 0a 23 64 65 66           42.#def
47860 69 6e 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20  ine TK_EACH     
47870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47880 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65        43.#define
47890 20 54 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 20   TK_FAIL        
478a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478b0 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 54 4b     44.#define TK
478c0 5f 46 4f 52 20 20 20 20 20 20 20 20 20 20 20 20  _FOR            
478d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478e0 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 47  45.#define TK_IG
478f0 4e 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20  NORE            
47900 20 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a               46.
47910 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 49  #define TK_INITI
47920 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20 20  ALLY            
47930 20 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65            47.#de
47940 66 69 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 20  fine TK_INSTEAD 
47950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47960 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 69 6e         48.#defin
47970 65 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 20  e TK_LIKE_KW    
47980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47990 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65 20 54      49.#define T
479a0 4b 5f 4d 41 54 43 48 20 20 20 20 20 20 20 20 20  K_MATCH         
479b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479c0 20 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e   50.#define TK_N
479d0 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  O               
479e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31                51
479f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59 20  .#define TK_KEY 
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a10 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 64             52.#d
47a20 65 66 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20 20  efine TK_OF     
47a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a40 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 66 69          53.#defi
47a50 6e 65 20 54 4b 5f 4f 46 46 53 45 54 20 20 20 20  ne TK_OFFSET    
47a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a70 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20       54.#define 
47a80 54 4b 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20  TK_PRAGMA       
47a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47aa0 20 20 35 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    55.#define TK_
47ab0 52 41 49 53 45 20 20 20 20 20 20 20 20 20 20 20  RAISE           
47ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
47ad0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 50  6.#define TK_REP
47ae0 4c 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20  LACE            
47af0 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 23              57.#
47b00 64 65 66 69 6e 65 20 54 4b 5f 52 45 53 54 52 49  define TK_RESTRI
47b10 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CT              
47b20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 66           58.#def
47b30 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20 20 20  ine TK_ROW      
47b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b50 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65        59.#define
47b60 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20 20 20   TK_TRIGGER     
47b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b80 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 54 4b     60.#define TK
47b90 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20 20 20  _VACUUM         
47ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bb0 36 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49  61.#define TK_VI
47bc0 45 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EW              
47bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 32 0a               62.
47be0 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 52 54 55  #define TK_VIRTU
47bf0 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AL              
47c00 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65            63.#de
47c10 66 69 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58 20  fine TK_REINDEX 
47c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c30 20 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e         64.#defin
47c40 65 20 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20 20  e TK_RENAME     
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c60 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 20 54      65.#define T
47c70 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 20 20 20 20  K_CTIME_KW      
47c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c90 20 36 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41   66.#define TK_A
47ca0 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NY              
47cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 37                67
47cc0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20 20  .#define TK_OR  
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ce0 20 20 20 20 20 20 20 20 20 20 20 36 38 0a 23 64             68.#d
47cf0 65 66 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20 20  efine TK_AND    
47d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d10 20 20 20 20 20 20 20 20 36 39 0a 23 64 65 66 69          69.#defi
47d20 6e 65 20 54 4b 5f 49 53 20 20 20 20 20 20 20 20  ne TK_IS        
47d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d40 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20       70.#define 
47d50 54 4b 5f 42 45 54 57 45 45 4e 20 20 20 20 20 20  TK_BETWEEN      
47d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d70 20 20 37 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    71.#define TK_
47d80 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IN              
47d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
47da0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e  2.#define TK_ISN
47db0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  ULL             
47dc0 20 20 20 20 20 20 20 20 20 20 20 20 37 33 0a 23              73.#
47dd0 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c  define TK_NOTNUL
47de0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
47df0 20 20 20 20 20 20 20 20 20 37 34 0a 23 64 65 66           74.#def
47e00 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20 20 20  ine TK_NE       
47e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e20 20 20 20 20 20 20 37 35 0a 23 64 65 66 69 6e 65        75.#define
47e30 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20   TK_EQ          
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e50 20 20 20 37 36 0a 23 64 65 66 69 6e 65 20 54 4b     76.#define TK
47e60 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20 20  _GT             
47e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e80 37 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 45  77.#define TK_LE
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 38 0a               78.
47eb0 23 64 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20 20  #define TK_LT   
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ed0 20 20 20 20 20 20 20 20 20 20 37 39 0a 23 64 65            79.#de
47ee0 66 69 6e 65 20 54 4b 5f 47 45 20 20 20 20 20 20  fine TK_GE      
47ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f00 20 20 20 20 20 20 20 38 30 0a 23 64 65 66 69 6e         80.#defin
47f10 65 20 54 4b 5f 45 53 43 41 50 45 20 20 20 20 20  e TK_ESCAPE     
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f30 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 20 54      81.#define T
47f40 4b 5f 42 49 54 41 4e 44 20 20 20 20 20 20 20 20  K_BITAND        
47f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f60 20 38 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42   82.#define TK_B
47f70 49 54 4f 52 20 20 20 20 20 20 20 20 20 20 20 20  ITOR            
47f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33                83
47f90 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48 49  .#define TK_LSHI
47fa0 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  FT              
47fb0 20 20 20 20 20 20 20 20 20 20 20 38 34 0a 23 64             84.#d
47fc0 65 66 69 6e 65 20 54 4b 5f 52 53 48 49 46 54 20  efine TK_RSHIFT 
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fe0 20 20 20 20 20 20 20 20 38 35 0a 23 64 65 66 69          85.#defi
47ff0 6e 65 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 20  ne TK_PLUS      
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48010 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20       86.#define 
48020 54 4b 5f 4d 49 4e 55 53 20 20 20 20 20 20 20 20  TK_MINUS        
48030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48040 20 20 38 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    87.#define TK_
48050 53 54 41 52 20 20 20 20 20 20 20 20 20 20 20 20  STAR            
48060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
48070 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41  8.#define TK_SLA
48080 53 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SH              
48090 20 20 20 20 20 20 20 20 20 20 20 20 38 39 0a 23              89.#
480a0 64 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20  define TK_REM   
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480c0 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 65 66           90.#def
480d0 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20  ine TK_CONCAT   
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480f0 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 6e 65        91.#define
48100 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20 20   TK_COLLATE     
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48120 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 54 4b     92.#define TK
48130 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20 20 20  _BITNOT         
48140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48150 39 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54  93.#define TK_ST
48160 52 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20  RING            
48170 20 20 20 20 20 20 20 20 20 20 20 20 20 39 34 0a               94.
48180 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f  #define TK_JOIN_
48190 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  KW              
481a0 20 20 20 20 20 20 20 20 20 20 39 35 0a 23 64 65            95.#de
481b0 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 41 49  fine TK_CONSTRAI
481c0 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NT              
481d0 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e         96.#defin
481e0 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 20 20 20  e TK_DEFAULT    
481f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48200 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 20 54      97.#define T
48210 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20  K_NULL          
48220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48230 20 39 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50   98.#define TK_P
48240 52 49 4d 41 52 59 20 20 20 20 20 20 20 20 20 20  RIMARY          
48250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 39                99
48260 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 51  .#define TK_UNIQ
48270 55 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  UE              
48280 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23             100.#
48290 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43 4b 20  define TK_CHECK 
482a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
482b0 20 20 20 20 20 20 20 20 20 31 30 31 0a 23 64 65           101.#de
482c0 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45 4e 43  fine TK_REFERENC
482d0 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ES              
482e0 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69         102.#defi
482f0 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52 20 20  ne TK_AUTOINCR  
48300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48310 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65       103.#define
48320 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20 20 20   TK_ON          
48330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48340 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 54     104.#define T
48350 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20  K_DELETE        
48360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48370 20 31 30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f   105.#define TK_
48380 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20  UPDATE          
48390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
483a0 30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45  06.#define TK_SE
483b0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
483c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37               107
483d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45  .#define TK_DEFE
483e0 52 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  RRABLE          
483f0 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23             108.#
48400 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47  define TK_FOREIG
48410 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
48420 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 64 65           109.#de
48430 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20  fine TK_DROP    
48440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48450 20 20 20 20 20 20 20 31 31 30 0a 23 64 65 66 69         110.#defi
48460 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20  ne TK_UNION     
48470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48480 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65       111.#define
48490 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20   TK_ALL         
484a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484b0 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 54     112.#define T
484c0 4b 5f 45 58 43 45 50 54 20 20 20 20 20 20 20 20  K_EXCEPT        
484d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484e0 20 31 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f   113.#define TK_
484f0 49 4e 54 45 52 53 45 43 54 20 20 20 20 20 20 20  INTERSECT       
48500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48510 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45  14.#define TK_SE
48520 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20  LECT            
48530 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 35               115
48540 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54  .#define TK_DIST
48550 49 4e 43 54 20 20 20 20 20 20 20 20 20 20 20 20  INCT            
48560 20 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23             116.#
48570 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20  define TK_DOT   
48580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48590 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 64 65           117.#de
485a0 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20  fine TK_FROM    
485b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485c0 20 20 20 20 20 20 20 31 31 38 0a 23 64 65 66 69         118.#defi
485d0 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20  ne TK_JOIN      
485e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485f0 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65       119.#define
48600 20 54 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20   TK_USING       
48610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48620 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 54     120.#define T
48630 4b 5f 4f 52 44 45 52 20 20 20 20 20 20 20 20 20  K_ORDER         
48640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48650 20 31 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f   121.#define TK_
48660 47 52 4f 55 50 20 20 20 20 20 20 20 20 20 20 20  GROUP           
48670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48680 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41  22.#define TK_HA
48690 56 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20  VING            
486a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33               123
486b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49  .#define TK_LIMI
486c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
486d0 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23             124.#
486e0 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20  define TK_WHERE 
486f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48700 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65           125.#de
48710 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20  fine TK_INTO    
48720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48730 20 20 20 20 20 20 20 31 32 36 0a 23 64 65 66 69         126.#defi
48740 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20  ne TK_VALUES    
48750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48760 20 20 20 20 20 31 32 37 0a 23 64 65 66 69 6e 65       127.#define
48770 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20   TK_INSERT      
48780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48790 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 54     128.#define T
487a0 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20 20 20  K_INTEGER       
487b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487c0 20 31 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f   129.#define TK_
487d0 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20 20 20  FLOAT           
487e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
487f0 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 4c  30.#define TK_BL
48800 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OB              
48810 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 31               131
48820 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 47 49  .#define TK_REGI
48830 53 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20  STER            
48840 20 20 20 20 20 20 20 20 20 20 20 31 33 32 0a 23             132.#
48850 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49 41 42  define TK_VARIAB
48860 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
48870 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 64 65           133.#de
48880 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20 20 20  fine TK_CASE    
48890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488a0 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 66 69         134.#defi
488b0 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20 20 20  ne TK_WHEN      
488c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488d0 20 20 20 20 20 31 33 35 0a 23 64 65 66 69 6e 65       135.#define
488e0 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20 20 20   TK_THEN        
488f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48900 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 54     136.#define T
48910 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20 20 20  K_ELSE          
48920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48930 20 31 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f   137.#define TK_
48940 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20  INDEX           
48950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48960 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c  38.#define TK_AL
48970 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  TER             
48980 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 39               139
48990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 44 20  .#define TK_ADD 
489a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
489b0 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 23             140.#
489c0 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58  define TK_TO_TEX
489d0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
489e0 20 20 20 20 20 20 20 20 20 31 34 31 0a 23 64 65           141.#de
489f0 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20  fine TK_TO_BLOB 
48a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a10 20 20 20 20 20 20 20 31 34 32 0a 23 64 65 66 69         142.#defi
48a20 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43  ne TK_TO_NUMERIC
48a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a40 20 20 20 20 20 31 34 33 0a 23 64 65 66 69 6e 65       143.#define
48a50 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20   TK_TO_INT      
48a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a70 20 20 20 31 34 34 0a 23 64 65 66 69 6e 65 20 54     144.#define T
48a80 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20 20 20  K_TO_REAL       
48a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48aa0 20 31 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f   145.#define TK_
48ab0 49 53 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20  ISNOT           
48ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48ad0 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e  46.#define TK_EN
48ae0 44 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20 20  D_OF_FILE       
48af0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 37               147
48b00 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c 45  .#define TK_ILLE
48b10 47 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  GAL             
48b20 20 20 20 20 20 20 20 20 20 20 20 31 34 38 0a 23             148.#
48b30 64 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45 20  define TK_SPACE 
48b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48b50 20 20 20 20 20 20 20 20 20 31 34 39 0a 23 64 65           149.#de
48b60 66 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45 44  fine TK_UNCLOSED
48b70 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20  _STRING         
48b80 20 20 20 20 20 20 20 31 35 30 0a 23 64 65 66 69         150.#defi
48b90 6e 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20  ne TK_FUNCTION  
48ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48bb0 20 20 20 20 20 31 35 31 0a 23 64 65 66 69 6e 65       151.#define
48bc0 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20   TK_COLUMN      
48bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48be0 20 20 20 31 35 32 0a 23 64 65 66 69 6e 65 20 54     152.#define T
48bf0 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20  K_AGG_FUNCTION  
48c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c10 20 31 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f   153.#define TK_
48c20 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20  AGG_COLUMN      
48c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48c40 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  54.#define TK_CO
48c50 4e 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20  NST_FUNC        
48c60 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 35               155
48c70 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e  .#define TK_UMIN
48c80 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  US              
48c90 20 20 20 20 20 20 20 20 20 20 20 31 35 36 0a 23             156.#
48ca0 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 53 20  define TK_UPLUS 
48cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48cc0 20 20 20 20 20 20 20 20 20 31 35 37 0a 0a 2f 2a           157../*
48cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
48ce0 64 20 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a  d of parse.h ***
48cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
48d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
48d30 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
48d40 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71  e left off in sq
48d50 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
48d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69  ************/.#i
48d70 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e  nclude <stdio.h>
48d80 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69  .#include <stdli
48d90 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  b.h>.#include <s
48da0 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64  tring.h>.#includ
48db0 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e  e <assert.h>.#in
48dc0 63 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e  clude <stddef.h>
48dd0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69  ../*.** If compi
48de0 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65  ling for a proce
48df0 73 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20  ssor that lacks 
48e00 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73  floating point s
48e10 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74  upport,.** subst
48e20 69 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f  itute integer fo
48e30 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74  r floating-point
48e40 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
48e50 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
48e60 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64  POINT.# define d
48e70 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74  ouble sqlite_int
48e80 36 34 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47  64.# define LONG
48e90 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69  DOUBLE_TYPE sqli
48ea0 74 65 5f 69 6e 74 36 34 0a 23 20 69 66 6e 64 65  te_int64.# ifnde
48eb0 66 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  f SQLITE_BIG_DBL
48ec0 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  .#   define SQLI
48ed0 54 45 5f 42 49 47 5f 44 42 4c 20 28 28 28 73 71  TE_BIG_DBL (((sq
48ee0 6c 69 74 65 33 5f 69 6e 74 36 34 29 31 29 3c 3c  lite3_int64)1)<<
48ef0 35 30 29 0a 23 20 65 6e 64 69 66 0a 23 20 64 65  50).# endif.# de
48f00 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fine SQLITE_OMIT
48f10 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 20  _DATETIME_FUNCS 
48f20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  1.# define SQLIT
48f30 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 31 0a 23  E_OMIT_TRACE 1.#
48f40 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 49   undef SQLITE_MI
48f50 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54  XED_ENDIAN_64BIT
48f60 5f 46 4c 4f 41 54 0a 23 20 75 6e 64 65 66 20 53  _FLOAT.# undef S
48f70 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e  QLITE_HAVE_ISNAN
48f80 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
48f90 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23  SQLITE_BIG_DBL.#
48fa0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42   define SQLITE_B
48fb0 49 47 5f 44 42 4c 20 28 31 65 39 39 29 0a 23 65  IG_DBL (1e99).#e
48fc0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54  ndif../*.** OMIT
48fd0 5f 54 45 4d 50 44 42 20 69 73 20 73 65 74 20 74  _TEMPDB is set t
48fe0 6f 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d  o 1 if SQLITE_OM
48ff0 49 54 5f 54 45 4d 50 44 42 20 69 73 20 64 65 66  IT_TEMPDB is def
49000 69 6e 65 64 2c 20 6f 72 20 30 0a 2a 2a 20 61 66  ined, or 0.** af
49010 74 65 72 77 61 72 64 2e 20 48 61 76 69 6e 67 20  terward. Having 
49020 74 68 69 73 20 6d 61 63 72 6f 20 61 6c 6c 6f 77  this macro allow
49030 73 20 75 73 20 74 6f 20 63 61 75 73 65 20 74 68  s us to cause th
49040 65 20 43 20 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a  e C compiler .**
49050 20 74 6f 20 6f 6d 69 74 20 63 6f 64 65 20 75 73   to omit code us
49060 65 64 20 62 79 20 54 45 4d 50 20 74 61 62 6c 65  ed by TEMP table
49070 73 20 77 69 74 68 6f 75 74 20 6d 65 73 73 79 20  s without messy 
49080 23 69 66 6e 64 65 66 20 73 74 61 74 65 6d 65 6e  #ifndef statemen
49090 74 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ts..*/.#ifdef SQ
490a0 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42  LITE_OMIT_TEMPDB
490b0 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45  .#define OMIT_TE
490c0 4d 50 44 42 20 31 0a 23 65 6c 73 65 0a 23 64 65  MPDB 1.#else.#de
490d0 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42  fine OMIT_TEMPDB
490e0 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
490f0 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   If the followin
49100 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20 74  g macro is set t
49110 6f 20 31 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 76  o 1, then NULL v
49120 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64  alues are consid
49130 65 72 65 64 0a 2a 2a 20 64 69 73 74 69 6e 63 74  ered.** distinct
49140 20 77 68 65 6e 20 64 65 74 65 72 6d 69 6e 69 6e   when determinin
49150 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  g whether or not
49160 20 74 77 6f 20 65 6e 74 72 69 65 73 20 61 72 65   two entries are
49170 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 69 6e 20   the same.** in 
49180 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 20  a UNIQUE index. 
49190 20 54 68 69 73 20 69 73 20 74 68 65 20 77 61 79   This is the way
491a0 20 50 6f 73 74 67 72 65 53 51 4c 2c 20 4f 72 61   PostgreSQL, Ora
491b0 63 6c 65 2c 20 44 42 32 2c 20 4d 79 53 51 4c 2c  cle, DB2, MySQL,
491c0 0a 2a 2a 20 4f 43 45 4c 4f 54 2c 20 61 6e 64 20  .** OCELOT, and 
491d0 46 69 72 65 62 69 72 64 20 61 6c 6c 20 77 6f 72  Firebird all wor
491e0 6b 2e 20 20 54 68 65 20 53 51 4c 39 32 20 73 70  k.  The SQL92 sp
491f0 65 63 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61  ec explicitly sa
49200 79 73 20 74 68 69 73 0a 2a 2a 20 69 73 20 74 68  ys this.** is th
49210 65 20 77 61 79 20 74 68 69 6e 67 73 20 61 72 65  e way things are
49220 20 73 75 70 70 6f 73 65 20 74 6f 20 77 6f 72 6b   suppose to work
49230 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  ..**.** If the f
49240 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69  ollowing macro i
49250 73 20 73 65 74 20 74 6f 20 30 2c 20 74 68 65 20  s set to 0, the 
49260 4e 55 4c 4c 73 20 61 72 65 20 69 6e 64 69 73 74  NULLs are indist
49270 69 6e 63 74 20 66 6f 72 0a 2a 2a 20 61 20 55 4e  inct for.** a UN
49280 49 51 55 45 20 69 6e 64 65 78 2e 20 20 49 6e 20  IQUE index.  In 
49290 74 68 69 73 20 6d 6f 64 65 2c 20 79 6f 75 20 63  this mode, you c
492a0 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73  an only have a s
492b0 69 6e 67 6c 65 20 4e 55 4c 4c 20 65 6e 74 72 79  ingle NULL entry
492c0 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6c 75 6d 6e  .** for a column
492d0 20 64 65 63 6c 61 72 65 64 20 55 4e 49 51 55 45   declared UNIQUE
492e0 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 77  .  This is the w
492f0 61 79 20 49 6e 66 6f 72 6d 69 78 20 61 6e 64 20  ay Informix and 
49300 53 51 4c 20 53 65 72 76 65 72 0a 2a 2a 20 77 6f  SQL Server.** wo
49310 72 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e  rk..*/.#define N
49320 55 4c 4c 5f 44 49 53 54 49 4e 43 54 5f 46 4f 52  ULL_DISTINCT_FOR
49330 5f 55 4e 49 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a  _UNIQUE 1../*.**
49340 20 54 68 65 20 22 66 69 6c 65 20 66 6f 72 6d 61   The "file forma
49350 74 22 20 6e 75 6d 62 65 72 20 69 73 20 61 6e 20  t" number is an 
49360 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20  integer that is 
49370 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e  incremented when
49380 65 76 65 72 0a 2a 2a 20 74 68 65 20 56 44 42 45  ever.** the VDBE
49390 2d 6c 65 76 65 6c 20 66 69 6c 65 20 66 6f 72 6d  -level file form
493a0 61 74 20 63 68 61 6e 67 65 73 2e 20 20 54 68 65  at changes.  The
493b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
493c0 73 20 64 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20  s define the.** 
493d0 74 68 65 20 64 65 66 61 75 6c 74 20 66 69 6c 65  the default file
493e0 20 66 6f 72 6d 61 74 20 66 6f 72 20 6e 65 77 20   format for new 
493f0 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 74 68  databases and th
49400 65 20 6d 61 78 69 6d 75 6d 20 66 69 6c 65 20 66  e maximum file f
49410 6f 72 6d 61 74 0a 2a 2a 20 74 68 61 74 20 74 68  ormat.** that th
49420 65 20 6c 69 62 72 61 72 79 20 63 61 6e 20 72 65  e library can re
49430 61 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ad..*/.#define S
49440 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46  QLITE_MAX_FILE_F
49450 4f 52 4d 41 54 20 34 0a 23 69 66 6e 64 65 66 20  ORMAT 4.#ifndef 
49460 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
49470 49 4c 45 5f 46 4f 52 4d 41 54 0a 23 20 64 65 66  ILE_FORMAT.# def
49480 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
49490 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 31  LT_FILE_FORMAT 1
494a0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
494b0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
494c0 52 45 43 55 52 53 49 56 45 5f 54 52 49 47 47 45  RECURSIVE_TRIGGE
494d0 52 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  RS.# define SQLI
494e0 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52  TE_DEFAULT_RECUR
494f0 53 49 56 45 5f 54 52 49 47 47 45 52 53 20 30 0a  SIVE_TRIGGERS 0.
49500 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72  #endif../*.** Pr
49510 6f 76 69 64 65 20 61 20 64 65 66 61 75 6c 74 20  ovide a default 
49520 76 61 6c 75 65 20 66 6f 72 20 53 51 4c 49 54 45  value for SQLITE
49530 5f 54 45 4d 50 5f 53 54 4f 52 45 20 69 6e 20 63  _TEMP_STORE in c
49540 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 73 70  ase it is not sp
49550 65 63 69 66 69 65 64 0a 2a 2a 20 6f 6e 20 74 68  ecified.** on th
49560 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a  e command-line.*
49570 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
49580 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 23 20 64 65  _TEMP_STORE.# de
49590 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50  fine SQLITE_TEMP
495a0 5f 53 54 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a  _STORE 1.#endif.
495b0 0a 2f 2a 0a 2a 2a 20 47 43 43 20 64 6f 65 73 20  ./*.** GCC does 
495c0 6e 6f 74 20 64 65 66 69 6e 65 20 74 68 65 20 6f  not define the o
495d0 66 66 73 65 74 6f 66 28 29 20 6d 61 63 72 6f 20  ffsetof() macro 
495e0 73 6f 20 77 65 27 6c 6c 20 68 61 76 65 20 74 6f  so we'll have to
495f0 20 64 6f 20 69 74 0a 2a 2a 20 6f 75 72 73 65 6c   do it.** oursel
49600 76 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ves..*/.#ifndef 
49610 6f 66 66 73 65 74 6f 66 0a 23 64 65 66 69 6e 65  offsetof.#define
49620 20 6f 66 66 73 65 74 6f 66 28 53 54 52 55 43 54   offsetof(STRUCT
49630 55 52 45 2c 46 49 45 4c 44 29 20 28 28 69 6e 74  URE,FIELD) ((int
49640 29 28 28 63 68 61 72 2a 29 26 28 28 53 54 52 55  )((char*)&((STRU
49650 43 54 55 52 45 2a 29 30 29 2d 3e 46 49 45 4c 44  CTURE*)0)->FIELD
49660 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  )).#endif../*.**
49670 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
49680 20 74 68 69 73 20 6d 61 63 68 69 6e 65 20 75 73   this machine us
49690 65 73 20 45 42 43 44 49 43 2e 20 20 28 59 65 73  es EBCDIC.  (Yes
496a0 2c 20 62 65 6c 69 65 76 65 20 69 74 20 6f 72 0a  , believe it or.
496b0 2a 2a 20 6e 6f 74 2c 20 74 68 65 72 65 20 61 72  ** not, there ar
496c0 65 20 73 74 69 6c 6c 20 6d 61 63 68 69 6e 65 73  e still machines
496d0 20 6f 75 74 20 74 68 65 72 65 20 74 68 61 74 20   out there that 
496e0 75 73 65 20 45 42 43 44 49 43 2e 29 0a 2a 2f 0a  use EBCDIC.).*/.
496f0 23 69 66 20 27 41 27 20 3d 3d 20 27 5c 33 30 31  #if 'A' == '\301
49700 27 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  '.# define SQLIT
49710 45 5f 45 42 43 44 49 43 20 31 0a 23 65 6c 73 65  E_EBCDIC 1.#else
49720 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
49730 5f 41 53 43 49 49 20 31 0a 23 65 6e 64 69 66 0a  _ASCII 1.#endif.
49740 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 67 65 72 73 20  ./*.** Integers 
49750 6f 66 20 6b 6e 6f 77 6e 20 73 69 7a 65 73 2e 20  of known sizes. 
49760 20 54 68 65 73 65 20 74 79 70 65 64 65 66 73 20   These typedefs 
49770 6d 69 67 68 74 20 63 68 61 6e 67 65 20 66 6f 72  might change for
49780 20 61 72 63 68 69 74 65 63 74 75 72 65 73 0a 2a   architectures.*
49790 2a 20 77 68 65 72 65 20 74 68 65 20 73 69 7a 65  * where the size
497a0 73 20 76 65 72 79 2e 20 20 50 72 65 70 72 6f 63  s very.  Preproc
497b0 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 61 72 65  essor macros are
497c0 20 61 76 61 69 6c 61 62 6c 65 20 73 6f 20 74 68   available so th
497d0 61 74 20 74 68 65 0a 2a 2a 20 74 79 70 65 73 20  at the.** types 
497e0 63 61 6e 20 62 65 20 63 6f 6e 76 65 6e 69 65 6e  can be convenien
497f0 74 6c 79 20 72 65 64 65 66 69 6e 65 64 20 61 74  tly redefined at
49800 20 63 6f 6d 70 69 6c 65 2d 74 79 70 65 2e 20 20   compile-type.  
49810 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  Like this:.**.**
49820 20 20 20 20 20 20 20 20 20 63 63 20 27 2d 44 55           cc '-DU
49830 49 4e 54 50 54 52 5f 54 59 50 45 3d 6c 6f 6e 67  INTPTR_TYPE=long
49840 20 6c 6f 6e 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a   long int' ....*
49850 2f 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 33 32  /.#ifndef UINT32
49860 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41  _TYPE.# ifdef HA
49870 56 45 5f 55 49 4e 54 33 32 5f 54 0a 23 20 20 64  VE_UINT32_T.#  d
49880 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 50  efine UINT32_TYP
49890 45 20 75 69 6e 74 33 32 5f 74 0a 23 20 65 6c 73  E uint32_t.# els
498a0 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54  e.#  define UINT
498b0 33 32 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64  32_TYPE unsigned
498c0 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e   int.# endif.#en
498d0 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54  dif.#ifndef UINT
498e0 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20  16_TYPE.# ifdef 
498f0 48 41 56 45 5f 55 49 4e 54 31 36 5f 54 0a 23 20  HAVE_UINT16_T.# 
49900 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f 54   define UINT16_T
49910 59 50 45 20 75 69 6e 74 31 36 5f 74 0a 23 20 65  YPE uint16_t.# e
49920 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49  lse.#  define UI
49930 4e 54 31 36 5f 54 59 50 45 20 75 6e 73 69 67 6e  NT16_TYPE unsign
49940 65 64 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65  ed short int.# e
49950 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e  ndif.#endif.#ifn
49960 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 0a 23  def INT16_TYPE.#
49970 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e 54 31   ifdef HAVE_INT1
49980 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49 4e  6_T.#  define IN
49990 54 31 36 5f 54 59 50 45 20 69 6e 74 31 36 5f 74  T16_TYPE int16_t
499a0 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
499b0 65 20 49 4e 54 31 36 5f 54 59 50 45 20 73 68 6f  e INT16_TYPE sho
499c0 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23  rt int.# endif.#
499d0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49  endif.#ifndef UI
499e0 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66  NT8_TYPE.# ifdef
499f0 20 48 41 56 45 5f 55 49 4e 54 38 5f 54 0a 23 20   HAVE_UINT8_T.# 
49a00 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59   define UINT8_TY
49a10 50 45 20 75 69 6e 74 38 5f 74 0a 23 20 65 6c 73  PE uint8_t.# els
49a20 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54  e.#  define UINT
49a30 38 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20  8_TYPE unsigned 
49a40 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e  char.# endif.#en
49a50 64 69 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 38  dif.#ifndef INT8
49a60 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41  _TYPE.# ifdef HA
49a70 56 45 5f 49 4e 54 38 5f 54 0a 23 20 20 64 65 66  VE_INT8_T.#  def
49a80 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 69 6e  ine INT8_TYPE in
49a90 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64  t8_t.# else.#  d
49aa0 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20  efine INT8_TYPE 
49ab0 73 69 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e  signed char.# en
49ac0 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  dif.#endif.#ifnd
49ad0 65 66 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59  ef LONGDOUBLE_TY
49ae0 50 45 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47  PE.# define LONG
49af0 44 4f 55 42 4c 45 5f 54 59 50 45 20 6c 6f 6e 67  DOUBLE_TYPE long
49b00 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 74   double.#endif.t
49b10 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e  ypedef sqlite_in
49b20 74 36 34 20 69 36 34 3b 20 20 20 20 20 20 20 20  t64 i64;        
49b30 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e    /* 8-byte sign
49b40 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79  ed integer */.ty
49b50 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e  pedef sqlite_uin
49b60 74 36 34 20 75 36 34 3b 20 20 20 20 20 20 20 20  t64 u64;        
49b70 20 2f 2a 20 38 2d 62 79 74 65 20 75 6e 73 69 67   /* 8-byte unsig
49b80 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74  ned integer */.t
49b90 79 70 65 64 65 66 20 55 49 4e 54 33 32 5f 54 59  ypedef UINT32_TY
49ba0 50 45 20 75 33 32 3b 20 20 20 20 20 20 20 20 20  PE u32;         
49bb0 20 20 2f 2a 20 34 2d 62 79 74 65 20 75 6e 73 69    /* 4-byte unsi
49bc0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
49bd0 74 79 70 65 64 65 66 20 55 49 4e 54 31 36 5f 54  typedef UINT16_T
49be0 59 50 45 20 75 31 36 3b 20 20 20 20 20 20 20 20  YPE u16;        
49bf0 20 20 20 2f 2a 20 32 2d 62 79 74 65 20 75 6e 73     /* 2-byte uns
49c00 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
49c10 0a 74 79 70 65 64 65 66 20 49 4e 54 31 36 5f 54  .typedef INT16_T
49c20 59 50 45 20 69 31 36 3b 20 20 20 20 20 20 20 20  YPE i16;        
49c30 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 20 73 69      /* 2-byte si
49c40 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
49c50 74 79 70 65 64 65 66 20 55 49 4e 54 38 5f 54 59  typedef UINT8_TY
49c60 50 45 20 75 38 3b 20 20 20 20 20 20 20 20 20 20  PE u8;          
49c70 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 75 6e 73     /* 1-byte uns
49c80 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
49c90 0a 74 79 70 65 64 65 66 20 49 4e 54 38 5f 54 59  .typedef INT8_TY
49ca0 50 45 20 69 38 3b 20 20 20 20 20 20 20 20 20 20  PE i8;          
49cb0 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 73 69      /* 1-byte si
49cc0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
49cd0 0a 2f 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41  ./*.** SQLITE_MA
49ce0 58 5f 55 33 32 20 69 73 20 61 20 75 36 34 20 63  X_U32 is a u64 c
49cf0 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 69 73 20  onstant that is 
49d00 74 68 65 20 6d 61 78 69 6d 75 6d 20 75 36 34 20  the maximum u64 
49d10 76 61 6c 75 65 0a 2a 2a 20 74 68 61 74 20 63 61  value.** that ca
49d20 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61  n be stored in a
49d30 20 75 33 32 20 77 69 74 68 6f 75 74 20 6c 6f 73   u32 without los
49d40 73 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20  s of data.  The 
49d50 76 61 6c 75 65 0a 2a 2a 20 69 73 20 30 78 30 30  value.** is 0x00
49d60 30 30 30 30 30 30 66 66 66 66 66 66 66 66 2e 20  000000ffffffff. 
49d70 20 42 75 74 20 62 65 63 61 75 73 65 20 6f 66 20   But because of 
49d80 71 75 69 72 6b 73 20 6f 66 20 73 6f 6d 65 20 63  quirks of some c
49d90 6f 6d 70 69 6c 65 72 73 2c 20 77 65 0a 2a 2a 20  ompilers, we.** 
49da0 68 61 76 65 20 74 6f 20 73 70 65 63 69 66 79 20  have to specify 
49db0 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65  the value in the
49dc0 20 6c 65 73 73 20 69 6e 74 75 69 74 69 76 65 20   less intuitive 
49dd0 6d 61 6e 6e 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f  manner shown:.*/
49de0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
49df0 4d 41 58 5f 55 33 32 20 20 28 28 28 28 75 36 34  MAX_U32  ((((u64
49e00 29 31 29 3c 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a  )1)<<32)-1)../*.
49e10 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74  ** Macros to det
49e20 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74  ermine whether t
49e30 68 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69  he machine is bi
49e40 67 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69  g or little endi
49e50 61 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64  an,.** evaluated
49e60 20 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a   at runtime..*/.
49e70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  #ifdef SQLITE_AM
49e80 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54  ALGAMATION.SQLIT
49e90 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
49ea0 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d  int sqlite3one =
49eb0 20 31 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45   1;.#else.SQLITE
49ec0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69  _PRIVATE const i
49ed0 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23  nt sqlite3one;.#
49ee0 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65  endif.#if define
49ef0 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e  d(i386) || defin
49f00 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20  ed(__i386__) || 
49f10 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29  defined(_M_IX86)
49f20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
49f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
49f40 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f  | defined(__x86_
49f50 36 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  64) || defined(_
49f60 5f 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66  _x86_64__).# def
49f70 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e  ine SQLITE_BIGEN
49f80 44 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69  DIAN    0.# defi
49f90 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45  ne SQLITE_LITTLE
49fa0 45 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e  ENDIAN 1.# defin
49fb0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  e SQLITE_UTF16NA
49fc0 54 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46  TIVE  SQLITE_UTF
49fd0 31 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66  16LE.#else.# def
49fe0 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e  ine SQLITE_BIGEN
49ff0 44 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20  DIAN    (*(char 
4a000 2a 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d  *)(&sqlite3one)=
4a010 3d 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  =0).# define SQL
4a020 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e  ITE_LITTLEENDIAN
4a030 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c   (*(char *)(&sql
4a040 69 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64  ite3one)==1).# d
4a050 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46  efine SQLITE_UTF
4a060 31 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45  16NATIVE (SQLITE
4a070 5f 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54  _BIGENDIAN?SQLIT
4a080 45 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45  E_UTF16BE:SQLITE
4a090 5f 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66  _UTF16LE).#endif
4a0a0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74  ../*.** Constant
4a0b0 73 20 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73  s for the larges
4a0c0 74 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70  t and smallest p
4a0d0 6f 73 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73  ossible 64-bit s
4a0e0 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a  igned integers..
4a0f0 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20  ** These macros 
4a100 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20  are designed to 
4a110 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f  work correctly o
4a120 6e 20 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e  n both 32-bit an
4a130 64 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70  d 64-bit.** comp
4a140 69 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ilers..*/.#defin
4a150 65 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20  e LARGEST_INT64 
4a160 20 28 30 78 66 66 66 66 66 66 66 66 7c 28 28 28   (0xffffffff|(((
4a170 69 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c  i64)0x7fffffff)<
4a180 3c 33 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d  <32)).#define SM
4a190 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28  ALLEST_INT64 (((
4a1a0 69 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53  i64)-1) - LARGES
4a1b0 54 5f 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a  T_INT64)../* .**
4a1c0 20 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62   Round up a numb
4a1d0 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c  er to the next l
4a1e0 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f  arger multiple o
4a1f0 66 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73  f 8.  This is us
4a200 65 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38  ed.** to force 8
4a210 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20  -byte alignment 
4a220 6f 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74  on 64-bit archit
4a230 65 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66  ectures..*/.#def
4a240 69 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20  ine ROUND8(x)   
4a250 20 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a    (((x)+7)&~7)..
4a260 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e  /*.** Round down
4a270 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20   to the nearest 
4a280 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f  multiple of 8.*/
4a290 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f  .#define ROUNDDO
4a2a0 57 4e 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a  WN8(x) ((x)&~7).
4a2b0 0a 2f 2a 0a 2a 2a 20 41 73 73 65 72 74 20 74 68  ./*.** Assert th
4a2c0 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 58  at the pointer X
4a2d0 20 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61   is aligned to a
4a2e0 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72  n 8-byte boundar
4a2f0 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 49  y..*/.#define EI
4a300 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
4a310 4e 54 28 58 29 20 20 20 28 28 28 28 63 68 61 72  NT(X)   ((((char
4a320 2a 29 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30  *)(X) - (char*)0
4a330 29 26 37 29 3d 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a  )&7)==0).../*.**
4a340 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
4a350 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
4a360 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20  ructure is used 
4a370 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 75 73  to store the bus
4a380 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c  y-handler.** cal
4a390 6c 62 61 63 6b 20 66 6f 72 20 61 20 67 69 76 65  lback for a give
4a3a0 6e 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 2e  n sqlite handle.
4a3b0 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69   .**.** The sqli
4a3c0 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 20 6d  te.busyHandler m
4a3d0 65 6d 62 65 72 20 6f 66 20 74 68 65 20 73 71 6c  ember of the sql
4a3e0 69 74 65 20 73 74 72 75 63 74 20 63 6f 6e 74 61  ite struct conta
4a3f0 69 6e 73 20 74 68 65 20 62 75 73 79 0a 2a 2a 20  ins the busy.** 
4a400 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65  callback for the
4a410 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
4a420 2e 20 45 61 63 68 20 70 61 67 65 72 20 6f 70 65  . Each pager ope
4a430 6e 65 64 20 76 69 61 20 74 68 65 20 73 71 6c 69  ned via the sqli
4a440 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20  te.** handle is 
4a450 70 61 73 73 65 64 20 61 20 70 6f 69 6e 74 65 72  passed a pointer
4a460 20 74 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 48   to sqlite.busyH
4a470 61 6e 64 6c 65 72 2e 20 54 68 65 20 62 75 73 79  andler. The busy
4a480 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c  -handler.** call
4a490 62 61 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c  back is currentl
4a4a0 79 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 66  y invoked only f
4a4b0 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72  rom within pager
4a4c0 2e 63 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  .c..*/.typedef s
4a4d0 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65  truct BusyHandle
4a4e0 72 20 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 73  r BusyHandler;.s
4a4f0 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65  truct BusyHandle
4a500 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e  r {.  int (*xFun
4a510 63 29 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b 20  c)(void *,int); 
4a520 20 2f 2a 20 54 68 65 20 62 75 73 79 20 63 61 6c   /* The busy cal
4a530 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20  lback */.  void 
4a540 2a 70 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  *pArg;          
4a550 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
4a560 72 67 20 74 6f 20 62 75 73 79 20 63 61 6c 6c 62  rg to busy callb
4a570 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75  ack */.  int nBu
4a580 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sy;             
4a590 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
4a5a0 65 64 20 77 69 74 68 20 65 61 63 68 20 62 75 73  ed with each bus
4a5b0 79 20 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  y call */.};../*
4a5c0 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20  .** Name of the 
4a5d0 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 20  master database 
4a5e0 74 61 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 74  table.  The mast
4a5f0 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  er database tabl
4a600 65 0a 2a 2a 20 69 73 20 61 20 73 70 65 63 69 61  e.** is a specia
4a610 6c 20 74 61 62 6c 65 20 74 68 61 74 20 68 6f 6c  l table that hol
4a620 64 73 20 74 68 65 20 6e 61 6d 65 73 20 61 6e 64  ds the names and
4a630 20 61 74 74 72 69 62 75 74 65 73 20 6f 66 20 61   attributes of a
4a640 6c 6c 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c 65  ll.** user table
4a650 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a 2a  s and indices..*
4a660 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52  /.#define MASTER
4a670 5f 4e 41 4d 45 20 20 20 20 20 20 20 22 73 71 6c  _NAME       "sql
4a680 69 74 65 5f 6d 61 73 74 65 72 22 0a 23 64 65 66  ite_master".#def
4a690 69 6e 65 20 54 45 4d 50 5f 4d 41 53 54 45 52 5f  ine TEMP_MASTER_
4a6a0 4e 41 4d 45 20 20 22 73 71 6c 69 74 65 5f 74 65  NAME  "sqlite_te
4a6b0 6d 70 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a  mp_master"../*.*
4a6c0 2a 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20  * The root-page 
4a6d0 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 61  of the master da
4a6e0 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f  tabase table..*/
4a6f0 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f  .#define MASTER_
4a700 52 4f 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f 2a  ROOT       1../*
4a710 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
4a720 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65  the schema table
4a730 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43 48  ..*/.#define SCH
4a740 45 4d 41 5f 54 41 42 4c 45 28 78 29 20 20 28 28  EMA_TABLE(x)  ((
4a750 21 4f 4d 49 54 5f 54 45 4d 50 44 42 29 26 26 28  !OMIT_TEMPDB)&&(
4a760 78 3d 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54 45  x==1)?TEMP_MASTE
4a770 52 5f 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e 41  R_NAME:MASTER_NA
4a780 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e  ME)../*.** A con
4a790 76 65 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 74  venience macro t
4a7a0 68 61 74 20 72 65 74 75 72 6e 73 20 74 68 65 20  hat returns the 
4a7b0 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
4a7c0 74 73 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72 61  ts in.** an arra
4a7d0 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41 72  y..*/.#define Ar
4a7e0 72 61 79 53 69 7a 65 28 58 29 20 20 20 20 28 28  raySize(X)    ((
4a7f0 69 6e 74 29 28 73 69 7a 65 6f 66 28 58 29 2f 73  int)(sizeof(X)/s
4a800 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a 2f  izeof(X[0])))../
4a810 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
4a820 6e 67 20 76 61 6c 75 65 20 61 73 20 61 20 64 65  ng value as a de
4a830 73 74 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 74  structor means t
4a840 6f 20 75 73 65 20 73 71 6c 69 74 65 33 44 62 46  o use sqlite3DbF
4a850 72 65 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 69  ree()..** This i
4a860 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 78  s an internal ex
4a870 74 65 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 54  tension to SQLIT
4a880 45 5f 53 54 41 54 49 43 20 61 6e 64 20 53 51 4c  E_STATIC and SQL
4a890 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a 2a  ITE_TRANSIENT..*
4a8a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
4a8b0 5f 44 59 4e 41 4d 49 43 20 20 20 28 28 73 71 6c  _DYNAMIC   ((sql
4a8c0 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f  ite3_destructor_
4a8d0 74 79 70 65 29 73 71 6c 69 74 65 33 44 62 46 72  type)sqlite3DbFr
4a8e0 65 65 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  ee)../*.** When 
4a8f0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20  SQLITE_OMIT_WSD 
4a900 69 73 20 64 65 66 69 6e 65 64 2c 20 69 74 20 6d  is defined, it m
4a910 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 61  eans that the ta
4a920 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 6f  rget platform do
4a930 65 73 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72  es.** not suppor
4a940 74 20 57 72 69 74 61 62 6c 65 20 53 74 61 74 69  t Writable Stati
4a950 63 20 44 61 74 61 20 28 57 53 44 29 20 73 75 63  c Data (WSD) suc
4a960 68 20 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20  h as global and 
4a970 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73  static variables
4a980 2e 0a 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c  ..** All variabl
4a990 65 73 20 6d 75 73 74 20 65 69 74 68 65 72 20 62  es must either b
4a9a0 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f  e on the stack o
4a9b0 72 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  r dynamically al
4a9c0 6c 6f 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20  located from.** 
4a9d0 74 68 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20  the heap.  When 
4a9e0 57 53 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74  WSD is unsupport
4a9f0 65 64 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65  ed, the variable
4aa00 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63   declarations sc
4aa10 61 74 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f 75  attered.** throu
4aa20 67 68 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65  ghout the SQLite
4aa30 20 63 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d   code must becom
4aa40 65 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74  e constants inst
4aa50 65 61 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45  ead.  The SQLITE
4aa60 5f 57 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73  _WSD.** macro is
4aa70 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 70   used for this p
4aa80 75 72 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e 73  urpose.  And ins
4aa90 74 65 61 64 20 6f 66 20 72 65 66 65 72 65 6e 63  tead of referenc
4aaa0 69 6e 67 20 74 68 65 20 76 61 72 69 61 62 6c 65  ing the variable
4aab0 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 65  .** directly, we
4aac0 20 75 73 65 20 69 74 73 20 63 6f 6e 73 74 61 6e   use its constan
4aad0 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20 6c 6f  t as a key to lo
4aae0 6f 6b 75 70 20 74 68 65 20 72 75 6e 2d 74 69 6d  okup the run-tim
4aaf0 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62  e allocated.** b
4ab00 75 66 66 65 72 20 74 68 61 74 20 68 6f 6c 64 73  uffer that holds
4ab10 20 72 65 61 6c 20 76 61 72 69 61 62 6c 65 2e 20   real variable. 
4ab20 20 54 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 73   The constant is
4ab30 20 61 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61   also the initia
4ab40 6c 69 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 65  lizer.** for the
4ab50 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61   run-time alloca
4ab60 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a  ted buffer..**.*
4ab70 2a 20 49 6e 20 74 68 65 20 75 73 75 61 6c 20 63  * In the usual c
4ab80 61 73 65 20 77 68 65 72 65 20 57 53 44 20 69 73  ase where WSD is
4ab90 20 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20   supported, the 
4aba0 53 51 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 47  SQLITE_WSD and G
4abb0 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20  LOBAL.** macros 
4abc0 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e  become no-ops an
4abd0 64 20 68 61 76 65 20 7a 65 72 6f 20 70 65 72 66  d have zero perf
4abe0 6f 72 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e 0a  ormance impact..
4abf0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
4ac00 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 66  _OMIT_WSD.  #def
4ac10 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 63  ine SQLITE_WSD c
4ac20 6f 6e 73 74 0a 20 20 23 64 65 66 69 6e 65 20 47  onst.  #define G
4ac30 4c 4f 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 2a  LOBAL(t,v) (*(t*
4ac40 29 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e  )sqlite3_wsd_fin
4ac50 64 28 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 73  d((void*)&(v), s
4ac60 69 7a 65 6f 66 28 76 29 29 29 0a 20 20 23 64 65  izeof(v))).  #de
4ac70 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62  fine sqlite3Glob
4ac80 61 6c 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c 28  alConfig GLOBAL(
4ac90 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f  struct Sqlite3Co
4aca0 6e 66 69 67 2c 20 73 71 6c 69 74 65 33 43 6f 6e  nfig, sqlite3Con
4acb0 66 69 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 20  fig).SQLITE_API 
4acc0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 73    int sqlite3_ws
4acd0 64 5f 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e  d_init(int N, in
4ace0 74 20 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  t J);.SQLITE_API
4acf0 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33     void *sqlite3
4ad00 5f 77 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a  _wsd_find(void *
4ad10 4b 2c 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 65  K, int L);.#else
4ad20 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54  .  #define SQLIT
4ad30 45 5f 57 53 44 20 0a 20 20 23 64 65 66 69 6e 65  E_WSD .  #define
4ad40 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a 20   GLOBAL(t,v) v. 
4ad50 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
4ad60 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 6c  GlobalConfig sql
4ad70 69 74 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 69  ite3Config.#endi
4ad80 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
4ad90 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 61 72  lowing macros ar
4ada0 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72 65  e used to suppre
4adb0 73 73 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e  ss compiler warn
4adc0 69 6e 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d  ings and to.** m
4add0 61 6b 65 20 69 74 20 63 6c 65 61 72 20 74 6f 20  ake it clear to 
4ade0 68 75 6d 61 6e 20 72 65 61 64 65 72 73 20 77 68  human readers wh
4adf0 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 61  en a function pa
4ae00 72 61 6d 65 74 65 72 20 69 73 20 64 65 6c 69 62  rameter is delib
4ae10 65 72 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66 74  erately .** left
4ae20 20 75 6e 75 73 65 64 20 77 69 74 68 69 6e 20 74   unused within t
4ae30 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e  he body of a fun
4ae40 63 74 69 6f 6e 2e 20 54 68 69 73 20 75 73 75 61  ction. This usua
4ae50 6c 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e  lly happens when
4ae60 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20 69  .** a function i
4ae70 73 20 63 61 6c 6c 65 64 20 76 69 61 20 61 20 66  s called via a f
4ae80 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e  unction pointer.
4ae90 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65   For example the
4aea0 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74   .** implementat
4aeb0 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61 67  ion of an SQL ag
4aec0 67 72 65 67 61 74 65 20 73 74 65 70 20 63 61 6c  gregate step cal
4aed0 6c 62 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75 73  lback may not us
4aee0 65 20 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65 74  e the.** paramet
4aef0 65 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  er indicating th
4af00 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  e number of argu
4af10 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20  ments passed to 
4af20 74 68 65 20 61 67 67 72 65 67 61 74 65 2c 0a 2a  the aggregate,.*
4af30 2a 20 69 66 20 69 74 20 6b 6e 6f 77 73 20 74 68  * if it knows th
4af40 61 74 20 74 68 69 73 20 69 73 20 65 6e 66 6f 72  at this is enfor
4af50 63 65 64 20 65 6c 73 65 77 68 65 72 65 2e 0a 2a  ced elsewhere..*
4af60 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e 63  *.** When a func
4af70 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69  tion parameter i
4af80 73 20 6e 6f 74 20 75 73 65 64 20 61 74 20 61 6c  s not used at al
4af90 6c 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64  l within the bod
4afa0 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2c  y of a function,
4afb0 0a 2a 2a 20 69 74 20 69 73 20 67 65 6e 65 72 61  .** it is genera
4afc0 6c 6c 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 73  lly named "NotUs
4afd0 65 64 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 32  ed" or "NotUsed2
4afe0 22 20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 73  " to make things
4aff0 20 65 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a 2a   even clearer..*
4b000 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 73 65  * However, these
4b010 20 6d 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 6f   macros may also
4b020 20 62 65 20 75 73 65 64 20 74 6f 20 73 75 70 70   be used to supp
4b030 72 65 73 73 20 77 61 72 6e 69 6e 67 73 20 72 65  ress warnings re
4b040 6c 61 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72 61  lated to.** para
4b050 6d 65 74 65 72 73 20 74 68 61 74 20 6d 61 79 20  meters that may 
4b060 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73  or may not be us
4b070 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  ed depending on 
4b080 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74 69  compilation opti
4b090 6f 6e 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d  ons..** For exam
4b0a0 70 6c 65 20 74 68 6f 73 65 20 70 61 72 61 6d 65  ple those parame
4b0b0 74 65 72 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  ters only used i
4b0c0 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  n assert() state
4b0d0 6d 65 6e 74 73 2e 20 49 6e 20 74 68 65 73 65 0a  ments. In these.
4b0e0 2a 2a 20 63 61 73 65 73 20 74 68 65 20 70 61 72  ** cases the par
4b0f0 61 6d 65 74 65 72 73 20 61 72 65 20 6e 61 6d 65  ameters are name
4b100 64 20 61 73 20 70 65 72 20 74 68 65 20 75 73 75  d as per the usu
4b110 61 6c 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a  al conventions..
4b120 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45  */.#define UNUSE
4b130 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29 20 28  D_PARAMETER(x) (
4b140 76 6f 69 64 29 28 78 29 0a 23 64 65 66 69 6e 65  void)(x).#define
4b150 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
4b160 52 32 28 78 2c 79 29 20 55 4e 55 53 45 44 5f 50  R2(x,y) UNUSED_P
4b170 41 52 41 4d 45 54 45 52 28 78 29 2c 55 4e 55 53  ARAMETER(x),UNUS
4b180 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 29 0a  ED_PARAMETER(y).
4b190 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 72  ./*.** Forward r
4b1a0 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73 74 72  eferences to str
4b1b0 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 64  uctures.*/.typed
4b1c0 65 66 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  ef struct AggInf
4b1d0 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 64  o AggInfo;.typed
4b1e0 65 66 20 73 74 72 75 63 74 20 41 75 74 68 43 6f  ef struct AuthCo
4b1f0 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74 65 78  ntext AuthContex
4b200 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4b210 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 41 75  t AutoincInfo Au
4b220 74 6f 69 6e 63 49 6e 66 6f 3b 0a 74 79 70 65 64  toincInfo;.typed
4b230 65 66 20 73 74 72 75 63 74 20 42 69 74 76 65 63  ef struct Bitvec
4b240 20 42 69 74 76 65 63 3b 0a 74 79 70 65 64 65 66   Bitvec;.typedef
4b250 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 20 52   struct RowSet R
4b260 6f 77 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73  owSet;.typedef s
4b270 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 43 6f  truct CollSeq Co
4b280 6c 6c 53 65 71 3b 0a 74 79 70 65 64 65 66 20 73  llSeq;.typedef s
4b290 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c  truct Column Col
4b2a0 75 6d 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72  umn;.typedef str
4b2b0 75 63 74 20 44 62 20 44 62 3b 0a 74 79 70 65 64  uct Db Db;.typed
4b2c0 65 66 20 73 74 72 75 63 74 20 53 63 68 65 6d 61  ef struct Schema
4b2d0 20 53 63 68 65 6d 61 3b 0a 74 79 70 65 64 65 66   Schema;.typedef
4b2e0 20 73 74 72 75 63 74 20 45 78 70 72 20 45 78 70   struct Expr Exp
4b2f0 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
4b300 74 20 45 78 70 72 4c 69 73 74 20 45 78 70 72 4c  t ExprList ExprL
4b310 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ist;.typedef str
4b320 75 63 74 20 45 78 70 72 53 70 61 6e 20 45 78 70  uct ExprSpan Exp
4b330 72 53 70 61 6e 3b 0a 74 79 70 65 64 65 66 20 73  rSpan;.typedef s
4b340 74 72 75 63 74 20 46 4b 65 79 20 46 4b 65 79 3b  truct FKey FKey;
4b350 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b360 46 75 6e 63 44 65 66 20 46 75 6e 63 44 65 66 3b  FuncDef FuncDef;
4b370 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b380 46 75 6e 63 44 65 66 48 61 73 68 20 46 75 6e 63  FuncDefHash Func
4b390 44 65 66 48 61 73 68 3b 0a 74 79 70 65 64 65 66  DefHash;.typedef
4b3a0 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 49   struct IdList I
4b3b0 64 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73  dList;.typedef s
4b3c0 74 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64 65  truct Index Inde
4b3d0 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  x;.typedef struc
4b3e0 74 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 49 6e  t IndexSample In
4b3f0 64 65 78 53 61 6d 70 6c 65 3b 0a 74 79 70 65 64  dexSample;.typed
4b400 65 66 20 73 74 72 75 63 74 20 4b 65 79 43 6c 61  ef struct KeyCla
4b410 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a 74 79 70  ss KeyClass;.typ
4b420 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 49  edef struct KeyI
4b430 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 79 70  nfo KeyInfo;.typ
4b440 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b  edef struct Look
4b450 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65 3b  aside Lookaside;
4b460 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b470 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c 6f  LookasideSlot Lo
4b480 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a 74 79 70  okasideSlot;.typ
4b490 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f 64 75  edef struct Modu
4b4a0 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 65 64  le Module;.typed
4b4b0 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65 43 6f  ef struct NameCo
4b4c0 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74 65 78  ntext NameContex
4b4d0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4b4e0 74 20 50 61 72 73 65 20 50 61 72 73 65 3b 0a 74  t Parse Parse;.t
4b4f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 61  ypedef struct Sa
4b500 76 65 70 6f 69 6e 74 20 53 61 76 65 70 6f 69 6e  vepoint Savepoin
4b510 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4b520 74 20 53 65 6c 65 63 74 20 53 65 6c 65 63 74 3b  t Select Select;
4b530 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b540 53 72 63 4c 69 73 74 20 53 72 63 4c 69 73 74 3b  SrcList SrcList;
4b550 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b560 53 74 72 41 63 63 75 6d 20 53 74 72 41 63 63 75  StrAccum StrAccu
4b570 6d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  m;.typedef struc
4b580 74 20 54 61 62 6c 65 20 54 61 62 6c 65 3b 0a 74  t Table Table;.t
4b590 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
4b5a0 62 6c 65 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f 63  bleLock TableLoc
4b5b0 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  k;.typedef struc
4b5c0 74 20 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74  t Token Token;.t
4b5d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72  ypedef struct Tr
4b5e0 69 67 67 65 72 50 72 67 20 54 72 69 67 67 65 72  iggerPrg Trigger
4b5f0 50 72 67 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Prg;.typedef str
4b600 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20  uct TriggerStep 
4b610 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 79 70  TriggerStep;.typ
4b620 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67  edef struct Trig
4b630 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 79 70  ger Trigger;.typ
4b640 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 70 61  edef struct Unpa
4b650 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 61 63  ckedRecord Unpac
4b660 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 65 64  kedRecord;.typed
4b670 65 66 20 73 74 72 75 63 74 20 56 54 61 62 6c 65  ef struct VTable
4b680 20 56 54 61 62 6c 65 3b 0a 74 79 70 65 64 65 66   VTable;.typedef
4b690 20 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20 57   struct Walker W
4b6a0 61 6c 6b 65 72 3b 0a 74 79 70 65 64 65 66 20 73  alker;.typedef s
4b6b0 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20  truct WherePlan 
4b6c0 57 68 65 72 65 50 6c 61 6e 3b 0a 74 79 70 65 64  WherePlan;.typed
4b6d0 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 49  ef struct WhereI
4b6e0 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74  nfo WhereInfo;.t
4b6f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68  ypedef struct Wh
4b700 65 72 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65  ereLevel WhereLe
4b710 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65  vel;../*.** Defe
4b720 72 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e  r sourcing vdbe.
4b730 68 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e  h and btree.h un
4b740 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 22 75  til after the "u
4b750 38 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79  8" and .** "Busy
4b760 48 61 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66  Handler" typedef
4b770 73 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72  s. vdbe.h also r
4b780 65 71 75 69 72 65 73 20 61 20 66 65 77 20 6f 66  equires a few of
4b790 20 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70   the opaque.** p
4b7a0 6f 69 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e  ointer types (i.
4b7b0 65 2e 20 46 75 6e 63 44 65 66 29 20 64 65 66 69  e. FuncDef) defi
4b7c0 6e 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a  ned above..*/./*
4b7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
4b7e0 63 6c 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e  clude btree.h in
4b7f0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
4b800 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
4b810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
4b820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
4b830 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68  gin file btree.h
4b840 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4b850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
4b870 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
4b880 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
4b890 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
4b8a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
4b8b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
4b8c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
4b8d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
4b8e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
4b8f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
4b900 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
4b910 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
4b920 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
4b930 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
4b940 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
4b950 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
4b960 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
4b970 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
4b980 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
4b990 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
4b9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b9e0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  ****.** This hea
4b9f0 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73  der file defines
4ba00 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   the interface t
4ba10 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 42  hat the sqlite B
4ba20 2d 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75  -Tree file.** su
4ba30 62 73 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f  bsystem.  See co
4ba40 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f  mments in the so
4ba50 75 72 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20  urce code for a 
4ba60 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70  detailed descrip
4ba70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20  tion.** of what 
4ba80 65 61 63 68 20 69 6e 74 65 72 66 61 63 65 20 72  each interface r
4ba90 6f 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2f 0a  outine does..*/.
4baa0 23 69 66 6e 64 65 66 20 5f 42 54 52 45 45 5f 48  #ifndef _BTREE_H
4bab0 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54 52 45 45  _.#define _BTREE
4bac0 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68  _H_../* TODO: Th
4bad0 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 73  is definition is
4bae0 20 6a 75 73 74 20 69 6e 63 6c 75 64 65 64 20 73   just included s
4baf0 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20  o other modules 
4bb00 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e  compile. It.** n
4bb10 65 65 64 73 20 74 6f 20 62 65 20 72 65 76 69 73  eeds to be revis
4bb20 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ited..*/.#define
4bb30 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f   SQLITE_N_BTREE_
4bb40 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49  META 10../*.** I
4bb50 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f 6e  f defined as non
4bb60 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 75  -zero, auto-vacu
4bb70 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79  um is enabled by
4bb80 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 77   default. Otherw
4bb90 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 62  ise.** it must b
4bba0 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 20  e turned on for 
4bbb0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 73  each database us
4bbc0 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 6f  ing "PRAGMA auto
4bbd0 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f  _vacuum = 1"..*/
4bbe0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
4bbf0 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
4bc00 55 4d 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c  UM.  #define SQL
4bc10 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
4bc20 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a  VACUUM 0.#endif.
4bc30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41  .#define BTREE_A
4bc40 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 20 30  UTOVACUUM_NONE 0
4bc50 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
4bc60 74 20 64 6f 20 61 75 74 6f 2d 76 61 63 75 75 6d  t do auto-vacuum
4bc70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
4bc80 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c  E_AUTOVACUUM_FUL
4bc90 4c 20 31 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  L 1        /* Do
4bca0 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61 63 75 75   full auto-vacuu
4bcb0 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  m */.#define BTR
4bcc0 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e  EE_AUTOVACUUM_IN
4bcd0 43 52 20 32 20 20 20 20 20 20 20 20 2f 2a 20 49  CR 2        /* I
4bce0 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
4bcf0 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77  m */../*.** Forw
4bd00 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
4bd10 20 6f 66 20 73 74 72 75 63 74 75 72 65 0a 2a 2f   of structure.*/
4bd20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4bd30 42 74 72 65 65 20 42 74 72 65 65 3b 0a 74 79 70  Btree Btree;.typ
4bd40 65 64 65 66 20 73 74 72 75 63 74 20 42 74 43 75  edef struct BtCu
4bd50 72 73 6f 72 20 42 74 43 75 72 73 6f 72 3b 0a 74  rsor BtCursor;.t
4bd60 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74  ypedef struct Bt
4bd70 53 68 61 72 65 64 20 42 74 53 68 61 72 65 64 3b  Shared BtShared;
4bd80 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4bd90 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20  BtreeMutexArray 
4bda0 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3b  BtreeMutexArray;
4bdb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ../*.** This str
4bdc0 75 63 74 75 72 65 20 72 65 63 6f 72 64 73 20 61  ucture records a
4bdd0 6c 6c 20 6f 66 20 74 68 65 20 42 74 72 65 65 73  ll of the Btrees
4bde0 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 68 6f   that need to ho
4bdf0 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78 20 62 65  ld.** a mutex be
4be00 66 6f 72 65 20 77 65 20 65 6e 74 65 72 20 73 71  fore we enter sq
4be10 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e  lite3VdbeExec().
4be20 20 20 54 68 65 20 42 74 72 65 65 73 20 61 72 65    The Btrees are
4be30 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65 64 20 69  .** are placed i
4be40 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e 20 6f 72  n aBtree[] in or
4be50 64 65 72 20 6f 66 20 61 42 74 72 65 65 5b 5d 2d  der of aBtree[]-
4be60 3e 70 42 74 2e 20 20 54 68 61 74 20 77 61 79 2c  >pBt.  That way,
4be70 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c 77 61 79  .** we can alway
4be80 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e 6c 6f 63  s lock and unloc
4be90 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75 69 63 6b  k them all quick
4bea0 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74  ly..*/.struct Bt
4beb0 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 7b 0a  reeMutexArray {.
4bec0 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b 0a 20 20    int nMutex;.  
4bed0 42 74 72 65 65 20 2a 61 42 74 72 65 65 5b 53 51  Btree *aBtree[SQ
4bee0 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45  LITE_MAX_ATTACHE
4bef0 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 54  D+1];.};...SQLIT
4bf00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4bf10 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a  lite3BtreeOpen(.
4bf20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
4bf30 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61  ilename,   /* Na
4bf40 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66  me of database f
4bf50 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20  ile to open */. 
4bf60 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
4bf70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73            /* Ass
4bf80 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65  ociated database
4bf90 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
4bfa0 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65   Btree **ppBtree
4bfb0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74  ,         /* Ret
4bfc0 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a 20  urn open Btree* 
4bfd0 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  here */.  int fl
4bfe0 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
4bff0 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20     /* Flags */. 
4c000 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20   int vfsFlags   
4c010 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
4c020 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67  gs passed throug
4c030 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a 2f  h to VFS open */
4c040 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67  .);../* The flag
4c050 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  s parameter to s
4c060 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 20  qlite3BtreeOpen 
4c070 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69  can be the bitwi
4c080 73 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a 20  se or of the.** 
4c090 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73  following values
4c0a0 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54  ..**.** NOTE:  T
4c0b0 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 74  hese values must
4c0c0 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65   match the corre
4c0d0 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f 20  sponding PAGER_ 
4c0e0 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61 67  values in.** pag
4c0f0 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  er.h..*/.#define
4c100 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52   BTREE_OMIT_JOUR
4c110 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e 6f  NAL  1  /* Do no
4c120 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  t use journal.  
4c130 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23  No argument */.#
4c140 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4e 4f 5f  define BTREE_NO_
4c150 52 45 41 44 4c 4f 43 4b 20 20 20 32 20 20 2f 2a  READLOCK   2  /*
4c160 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20   Omit readlocks 
4c170 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65  on readonly file
4c180 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  s */.#define BTR
4c190 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 20  EE_MEMORY       
4c1a0 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79   4  /* In-memory
4c1b0 20 44 42 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e   DB.  No argumen
4c1c0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  t */.#define BTR
4c1d0 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20  EE_READONLY     
4c1e0 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20   8  /* Open the 
4c1f0 64 61 74 61 62 61 73 65 20 69 6e 20 72 65 61 64  database in read
4c200 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f 0a 23 64  -only mode */.#d
4c210 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44  efine BTREE_READ
4c220 57 52 49 54 45 20 20 20 20 31 36 20 20 2f 2a 20  WRITE    16  /* 
4c230 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68 20 72 65  Open for both re
4c240 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e  ading and writin
4c250 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  g */.#define BTR
4c260 45 45 5f 43 52 45 41 54 45 20 20 20 20 20 20 20  EE_CREATE       
4c270 33 32 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68  32  /* Create th
4c280 65 20 64 61 74 61 62 61 73 65 20 69 66 20 69 74  e database if it
4c290 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
4c2a0 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
4c2b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c2c0 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a 29  reeClose(Btree*)
4c2d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c2e0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c2f0 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74  eSetCacheSize(Bt
4c300 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ree*,int);.SQLIT
4c310 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c320 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66  lite3BtreeSetSaf
4c330 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a 2c  etyLevel(Btree*,
4c340 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
4c350 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c360 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73  ite3BtreeSyncDis
4c370 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a 53  abled(Btree*);.S
4c380 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c390 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
4c3a0 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20  tPageSize(Btree 
4c3b0 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 73 69 7a  *p, int nPagesiz
4c3c0 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 2c  e, int nReserve,
4c3d0 20 69 6e 74 20 65 46 69 78 29 3b 0a 53 51 4c 49   int eFix);.SQLI
4c3e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c3f0 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
4c400 67 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0a  geSize(Btree*);.
4c410 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c420 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
4c430 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65  axPageCount(Btre
4c440 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
4c450 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c460 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72  te3BtreeGetReser
4c470 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  ve(Btree*);.SQLI
4c480 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c490 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
4c4a0 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a  toVacuum(Btree *
4c4b0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
4c4c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4c4d0 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61  e3BtreeGetAutoVa
4c4e0 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 53  cuum(Btree *);.S
4c4f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c500 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  t sqlite3BtreeBe
4c510 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 2c  ginTrans(Btree*,
4c520 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4c530 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c540 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
4c550 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e 73  One(Btree*, cons
4c560 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29  t char *zMaster)
4c570 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c580 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c590 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
4c5a0 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
4c5b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c5c0 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42  te3BtreeCommit(B
4c5d0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
4c5e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4c5f0 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28  e3BtreeRollback(
4c600 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
4c610 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c620 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d  te3BtreeBeginStm
4c630 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53  t(Btree*,int);.S
4c640 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c650 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72  t sqlite3BtreeCr
4c660 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 2a  eateTable(Btree*
4c670 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66 6c 61 67  , int*, int flag
4c680 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  s);.SQLITE_PRIVA
4c690 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c6a0 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72  reeIsInTrans(Btr
4c6b0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4c6c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c6d0 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61  BtreeIsInReadTra
4c6e0 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  ns(Btree*);.SQLI
4c6f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c700 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 42  qlite3BtreeIsInB
4c710 61 63 6b 75 70 28 42 74 72 65 65 2a 29 3b 0a 53  ackup(Btree*);.S
4c720 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4c730 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
4c740 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 2c 20  Schema(Btree *, 
4c750 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
4c760 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  d *));.SQLITE_PR
4c770 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c780 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b  3BtreeSchemaLock
4c790 65 64 28 42 74 72 65 65 20 2a 70 42 74 72 65 65  ed(Btree *pBtree
4c7a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c7b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c7c0 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65  eeLockTable(Btre
4c7d0 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 69  e *pBtree, int i
4c7e0 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c  Tab, u8 isWriteL
4c7f0 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ock);.SQLITE_PRI
4c800 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c810 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42  BtreeSavepoint(B
4c820 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74  tree *, int, int
4c830 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
4c840 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
4c850 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69  qlite3BtreeGetFi
4c860 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b  lename(Btree *);
4c870 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c880 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
4c890 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e  te3BtreeGetJourn
4c8a0 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b  alname(Btree *);
4c8b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c8c0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4c8d0 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a  CopyFile(Btree *
4c8e0 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51 4c  , Btree *);..SQL
4c8f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c900 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72  sqlite3BtreeIncr
4c910 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b  Vacuum(Btree *);
4c920 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70  ../* The flags p
4c930 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
4c940 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61  te3BtreeCreateTa
4c950 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 20 62  ble can be the b
4c960 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66 20  itwise OR.** of 
4c970 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c  the following fl
4c980 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ags:.*/.#define 
4c990 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 20  BTREE_INTKEY    
4c9a0 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68   1    /* Table h
4c9b0 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 73  as only 64-bit s
4c9c0 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b 65  igned integer ke
4c9d0 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  ys */.#define BT
4c9e0 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 32  REE_ZERODATA   2
4c9f0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73      /* Table has
4ca00 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20   keys only - no 
4ca10 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 20  data */.#define 
4ca20 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 20 20  BTREE_LEAFDATA  
4ca30 20 34 20 20 20 20 2f 2a 20 44 61 74 61 20 73 74   4    /* Data st
4ca40 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 73 20 6f  ored in leaves o
4ca50 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 20 49 4e  nly.  Implies IN
4ca60 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f  TKEY */..SQLITE_
4ca70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4ca80 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c  te3BtreeDropTabl
4ca90 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69  e(Btree*, int, i
4caa0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
4cab0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4cac0 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28  BtreeClearTable(
4cad0 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Btree*, int, int
4cae0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4caf0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
4cb00 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f  treeTripAllCurso
4cb10 72 73 28 42 74 72 65 65 2a 2c 20 69 6e 74 29 3b  rs(Btree*, int);
4cb20 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
4cb30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
4cb40 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 20  eeGetMeta(Btree 
4cb50 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 69 64 78  *pBtree, int idx
4cb60 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 29 3b 0a  , u32 *pValue);.
4cb70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4cb80 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 55  nt sqlite3BtreeU
4cb90 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 2a  pdateMeta(Btree*
4cba0 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 76  , int idx, u32 v
4cbb0 61 6c 75 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  alue);../*.** Th
4cbc0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
4cbd0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  er to sqlite3Btr
4cbe0 65 65 47 65 74 4d 65 74 61 20 6f 72 20 73 71 6c  eeGetMeta or sql
4cbf0 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
4cc00 65 74 61 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65  eta.** should be
4cc10 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
4cc20 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 20 54 68  owing values. Th
4cc30 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  e integer values
4cc40 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 0a 2a   are assigned .*
4cc50 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74 73 20 73  * to constants s
4cc60 6f 20 74 68 61 74 20 74 68 65 20 6f 66 66 73 65  o that the offse
4cc70 74 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70  t of the corresp
4cc80 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20 69 6e 20  onding field in 
4cc90 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74  an.** SQLite dat
4cca0 61 62 61 73 65 20 68 65 61 64 65 72 20 6d 61 79  abase header may
4ccb0 20 62 65 20 66 6f 75 6e 64 20 75 73 69 6e 67 20   be found using 
4ccc0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
4ccd0 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 6f  rmula:.**.**   o
4cce0 66 66 73 65 74 20 3d 20 33 36 20 2b 20 28 69 64  ffset = 36 + (id
4ccf0 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a 20 46 6f 72  x * 4).**.** For
4cd00 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 66 72   example, the fr
4cd10 65 65 2d 70 61 67 65 2d 63 6f 75 6e 74 20 66 69  ee-page-count fi
4cd20 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64 20 61  eld is located a
4cd30 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 33 36  t byte offset 36
4cd40 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 62   of.** the datab
4cd50 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 72 2e  ase file header.
4cd60 20 54 68 65 20 69 6e 63 72 2d 76 61 63 75 75 6d   The incr-vacuum
4cd70 2d 66 6c 61 67 20 66 69 65 6c 64 20 69 73 20 6c  -flag field is l
4cd80 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 62 79 74  ocated at.** byt
4cd90 65 20 6f 66 66 73 65 74 20 36 34 20 28 3d 3d 20  e offset 64 (== 
4cda0 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a 23 64 65 66  36+4*7)..*/.#def
4cdb0 69 6e 65 20 42 54 52 45 45 5f 46 52 45 45 5f 50  ine BTREE_FREE_P
4cdc0 41 47 45 5f 43 4f 55 4e 54 20 20 20 20 20 30 0a  AGE_COUNT     0.
4cdd0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 53 43  #define BTREE_SC
4cde0 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 20 20 20  HEMA_VERSION    
4cdf0 20 20 31 0a 23 64 65 66 69 6e 65 20 42 54 52 45    1.#define BTRE
4ce00 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 20 20  E_FILE_FORMAT   
4ce10 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20        2.#define 
4ce20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41  BTREE_DEFAULT_CA
4ce30 43 48 45 5f 53 49 5a 45 20 20 33 0a 23 64 65 66  CHE_SIZE  3.#def
4ce40 69 6e 65 20 42 54 52 45 45 5f 4c 41 52 47 45 53  ine BTREE_LARGES
4ce50 54 5f 52 4f 4f 54 5f 50 41 47 45 20 20 20 34 0a  T_ROOT_PAGE   4.
4ce60 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 54 45  #define BTREE_TE
4ce70 58 54 5f 45 4e 43 4f 44 49 4e 47 20 20 20 20 20  XT_ENCODING     
4ce80 20 20 35 0a 23 64 65 66 69 6e 65 20 42 54 52 45    5.#define BTRE
4ce90 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 20 20  E_USER_VERSION  
4cea0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
4ceb0 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 55  BTREE_INCR_VACUU
4cec0 4d 20 20 20 20 20 20 20 20 20 37 0a 0a 53 51 4c  M         7..SQL
4ced0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4cee0 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
4cef0 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c 20 20 20  or(.  Btree*,   
4cf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cf10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 54             /* BT
4cf20 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ree containing t
4cf30 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  able to open */.
4cf40 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
4cf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cf60 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
4cf70 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a  of root page */.
4cf80 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20    int wrFlag,   
4cf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cfa0 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20         /* 1 for 
4cfb0 77 72 69 74 69 6e 67 2e 20 20 30 20 66 6f 72 20  writing.  0 for 
4cfc0 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73  read-only */.  s
4cfd0 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 2a 2c 20  truct KeyInfo*, 
4cfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cff0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
4d000 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61 72 65  ument to compare
4d010 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42   function */.  B
4d020 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  tCursor *pCursor
4d030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d040 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20      /* Space to 
4d050 77 72 69 74 65 20 63 75 72 73 6f 72 20 73 74 72  write cursor str
4d060 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c  ucture */.);.SQL
4d070 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d080 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
4d090 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51  orSize(void);.SQ
4d0a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4d0b0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  d sqlite3BtreeCu
4d0c0 72 73 6f 72 5a 65 72 6f 28 42 74 43 75 72 73 6f  rsorZero(BtCurso
4d0d0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
4d0e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4d0f0 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72  BtreeCloseCursor
4d100 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c  (BtCursor*);.SQL
4d110 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d120 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
4d130 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 42 74  toUnpacked(.  Bt
4d140 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e 70 61 63  Cursor*,.  Unpac
4d150 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e 4b 65  kedRecord *pUnKe
4d160 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c  y,.  i64 intKey,
4d170 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a 20 20 69  .  int bias,.  i
4d180 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 51 4c 49  nt *pRes.);.SQLI
4d190 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4d1a0 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
4d1b0 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73  rHasMoved(BtCurs
4d1c0 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  or*, int*);.SQLI
4d1d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4d1e0 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
4d1f0 65 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51  e(BtCursor*);.SQ
4d200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d210 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73   sqlite3BtreeIns
4d220 65 72 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 63  ert(BtCursor*, c
4d230 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
4d240 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 20 20 20   i64 nKey,.     
4d250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d260 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
4d270 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20  st void *pData, 
4d280 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
4d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
4d2b0 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 69 61 73   nZero, int bias
4d2c0 2c 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74  , int seekResult
4d2d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4d2e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4d2f0 65 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72  eeFirst(BtCursor
4d300 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53  *, int *pRes);.S
4d310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d320 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61  t sqlite3BtreeLa
4d330 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e  st(BtCursor*, in
4d340 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45  t *pRes);.SQLITE
4d350 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d360 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74  ite3BtreeNext(Bt
4d370 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52  Cursor*, int *pR
4d380 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  es);.SQLITE_PRIV
4d390 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4d3a0 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f 72  treeEof(BtCursor
4d3b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4d3c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4d3d0 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75  reePrevious(BtCu
4d3e0 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73  rsor*, int *pRes
4d3f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4d400 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4d410 65 65 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73  eeKeySize(BtCurs
4d420 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69 7a 65 29  or*, i64 *pSize)
4d430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d440 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4d450 65 4b 65 79 28 42 74 43 75 72 73 6f 72 2a 2c 20  eKey(BtCursor*, 
4d460 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
4d470 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  amt, void*);.SQL
4d480 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
4d490 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42  t void *sqlite3B
4d4a0 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 43  treeKeyFetch(BtC
4d4b0 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d  ursor*, int *pAm
4d4c0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4d4d0 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
4d4e0 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46  qlite3BtreeDataF
4d4f0 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a 2c 20  etch(BtCursor*, 
4d500 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49  int *pAmt);.SQLI
4d510 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4d520 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53  qlite3BtreeDataS
4d530 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 75  ize(BtCursor*, u
4d540 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49  32 *pSize);.SQLI
4d550 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4d560 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
4d570 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f  BtCursor*, u32 o
4d580 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
4d590 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  void*);.SQLITE_P
4d5a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4d5b0 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
4d5c0 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a  dRowid(BtCursor*
4d5d0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  , sqlite3_int64)
4d5e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d5f0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
4d600 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61  qlite3BtreeGetCa
4d610 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73  chedRowid(BtCurs
4d620 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  or*);..SQLITE_PR
4d630 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
4d640 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74  te3BtreeIntegrit
4d650 79 43 68 65 63 6b 28 42 74 72 65 65 2a 2c 20 69  yCheck(Btree*, i
4d660 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 6e 74 20 6e  nt *aRoot, int n
4d670 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 29  Root, int, int*)
4d680 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d690 20 73 74 72 75 63 74 20 50 61 67 65 72 20 2a 73   struct Pager *s
4d6a0 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
4d6b0 28 42 74 72 65 65 2a 29 3b 0a 0a 53 51 4c 49 54  (Btree*);..SQLIT
4d6c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4d6d0 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74  lite3BtreePutDat
4d6e0 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32  a(BtCursor*, u32
4d6f0 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74   offset, u32 amt
4d700 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  , void*);.SQLITE
4d710 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4d720 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f  lite3BtreeCacheO
4d730 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 6f 72  verflow(BtCursor
4d740 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
4d750 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4d760 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72  BtreeClearCursor
4d770 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a 0a 23  (BtCursor *);..#
4d780 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51  ifndef NDEBUG.SQ
4d790 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d7a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
4d7b0 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 75 72  sorIsValid(BtCur
4d7c0 73 6f 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  sor*);.#endif..#
4d7d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
4d7e0 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 53 51  IT_BTREECOUNT.SQ
4d7f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d800 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75   sqlite3BtreeCou
4d810 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 69  nt(BtCursor *, i
4d820 36 34 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  64 *);.#endif..#
4d830 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
4d840 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
4d850 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4d860 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 74 43 75  eCursorInfo(BtCu
4d870 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74  rsor*, int*, int
4d880 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4d890 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
4d8a0 72 65 65 43 75 72 73 6f 72 4c 69 73 74 28 42 74  reeCursorList(Bt
4d8b0 72 65 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f  ree*);.#endif../
4d8c0 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 6e  *.** If we are n
4d8d0 6f 74 20 75 73 69 6e 67 20 73 68 61 72 65 64 20  ot using shared 
4d8e0 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65 72  cache, then ther
4d8f0 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 0a  e is no need to.
4d900 2a 2a 20 75 73 65 20 6d 75 74 65 78 65 73 20 74  ** use mutexes t
4d910 6f 20 61 63 63 65 73 73 20 74 68 65 20 42 74 53  o access the BtS
4d920 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73  hared structures
4d930 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68 65 0a 2a  .  So make the.*
4d940 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c 65 61 76  * Enter and Leav
4d950 65 20 70 72 6f 63 65 64 75 72 65 73 20 6e 6f 2d  e procedures no-
4d960 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ops..*/.#ifndef 
4d970 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
4d980 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f  ED_CACHE.SQLITE_
4d990 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
4d9a0 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
4d9b0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4d9c0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4d9d0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
4d9e0 72 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a  rAll(sqlite3*);.
4d9f0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
4da00 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
4da10 28 58 29 20 0a 23 20 64 65 66 69 6e 65 20 73 71  (X) .# define sq
4da20 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
4da30 6c 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69  ll(X).#endif..#i
4da40 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
4da50 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
4da60 43 48 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54  CHE) && SQLITE_T
4da70 48 52 45 41 44 53 41 46 45 0a 53 51 4c 49 54 45  HREADSAFE.SQLITE
4da80 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4da90 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
4daa0 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  e(Btree*);.SQLIT
4dab0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
4dac0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
4dad0 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  erCursor(BtCurso
4dae0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
4daf0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
4db00 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
4db10 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53  or(BtCursor*);.S
4db20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4db30 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
4db40 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65  eLeaveAll(sqlite
4db50 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
4db60 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
4db70 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
4db80 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65  yEnter(BtreeMute
4db90 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45  xArray*);.SQLITE
4dba0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4dbb0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
4dbc0 78 41 72 72 61 79 4c 65 61 76 65 28 42 74 72 65  xArrayLeave(Btre
4dbd0 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53  eMutexArray*);.S
4dbe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4dbf0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
4dc00 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72  eMutexArrayInser
4dc10 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  t(BtreeMutexArra
4dc20 79 2a 2c 20 42 74 72 65 65 2a 29 3b 0a 23 69 66  y*, Btree*);.#if
4dc30 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
4dc40 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
4dc50 61 72 65 20 75 73 65 64 20 69 6e 73 69 64 65 20  are used inside 
4dc60 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
4dc70 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 51 4c  nts only. */.SQL
4dc80 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
4dc90 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  t sqlite3BtreeHo
4dca0 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 2a 29  ldsMutex(Btree*)
4dcb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4dcc0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74     int sqlite3Bt
4dcd0 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
4dce0 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65  es(sqlite3*);.#e
4dcf0 6e 64 69 66 0a 23 65 6c 73 65 0a 0a 23 20 64 65  ndif.#else..# de
4dd00 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65  fine sqlite3Btre
4dd10 65 4c 65 61 76 65 28 58 29 0a 23 20 64 65 66 69  eLeave(X).# defi
4dd20 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  ne sqlite3BtreeE
4dd30 6e 74 65 72 43 75 72 73 6f 72 28 58 29 0a 23 20  nterCursor(X).# 
4dd40 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
4dd50 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 58  reeLeaveCursor(X
4dd60 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
4dd70 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
4dd80 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  X).# define sqli
4dd90 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
4dda0 61 79 45 6e 74 65 72 28 58 29 0a 23 20 64 65 66  ayEnter(X).# def
4ddb0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
4ddc0 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28  MutexArrayLeave(
4ddd0 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  X).# define sqli
4dde0 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
4ddf0 61 79 49 6e 73 65 72 74 28 58 2c 59 29 0a 0a 23  ayInsert(X,Y)..#
4de00 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4de10 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 58  treeHoldsMutex(X
4de20 29 20 31 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ) 1.# define sql
4de30 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
4de40 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23 65  lMutexes(X) 1.#e
4de50 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a  ndif...#endif /*
4de60 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f   _BTREE_H_ */../
4de70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
4de80 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 20 2a 2a  nd of btree.h **
4de90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4deb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
4dec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
4ded0 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
4dee0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73  we left off in s
4def0 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
4df00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
4df10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
4df20 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 20 69 6e  nclude vdbe.h in
4df30 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
4df40 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
4df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
4df60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
4df70 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e 68  egin file vdbe.h
4df80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
4dfb0 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
4dfc0 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
4dfd0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
4dfe0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
4dff0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
4e000 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
4e010 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
4e020 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
4e030 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
4e040 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
4e050 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
4e060 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
4e070 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
4e080 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
4e090 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
4e0a0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
4e0b0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
4e0c0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
4e0d0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
4e0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e120 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 65 72 20  *****.** Header 
4e130 66 69 6c 65 20 66 6f 72 20 74 68 65 20 56 69 72  file for the Vir
4e140 74 75 61 6c 20 44 61 74 61 42 61 73 65 20 45 6e  tual DataBase En
4e150 67 69 6e 65 20 28 56 44 42 45 29 0a 2a 2a 0a 2a  gine (VDBE).**.*
4e160 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 64 65  * This header de
4e170 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66  fines the interf
4e180 61 63 65 20 74 6f 20 74 68 65 20 76 69 72 74 75  ace to the virtu
4e190 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  al database engi
4e1a0 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 2e 20 20  ne.** or VDBE.  
4e1b0 54 68 65 20 56 44 42 45 20 69 6d 70 6c 65 6d 65  The VDBE impleme
4e1c0 6e 74 73 20 61 6e 20 61 62 73 74 72 61 63 74 20  nts an abstract 
4e1d0 6d 61 63 68 69 6e 65 20 74 68 61 74 20 72 75 6e  machine that run
4e1e0 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 20 70 72  s a.** simple pr
4e1f0 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 73 73 20  ogram to access 
4e200 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 75  and modify the u
4e210 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61  nderlying databa
4e220 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  se..*/.#ifndef _
4e230 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f 0a 23  SQLITE_VDBE_H_.#
4e240 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 56  define _SQLITE_V
4e250 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20  DBE_H_../*.** A 
4e260 73 69 6e 67 6c 65 20 56 44 42 45 20 69 73 20 61  single VDBE is a
4e270 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75  n opaque structu
4e280 72 65 20 6e 61 6d 65 64 20 22 56 64 62 65 22 2e  re named "Vdbe".
4e290 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 0a    Only routines.
4e2a0 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65  ** in the source
4e2b0 20 66 69 6c 65 20 73 71 6c 69 74 65 56 64 62 65   file sqliteVdbe
4e2c0 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74  .c are allowed t
4e2d0 6f 20 73 65 65 20 74 68 65 20 69 6e 73 69 64 65  o see the inside
4e2e0 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72  s.** of this str
4e2f0 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64  ucture..*/.typed
4e300 65 66 20 73 74 72 75 63 74 20 56 64 62 65 20 56  ef struct Vdbe V
4e310 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dbe;../*.** The 
4e320 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 6c  names of the fol
4e330 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64 65 63  lowing types dec
4e340 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49 6e 74  lared in vdbeInt
4e350 2e 68 20 61 72 65 20 72 65 71 75 69 72 65 64 0a  .h are required.
4e360 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 62 65 4f  ** for the VdbeO
4e370 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f  p definition..*/
4e380 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4e390 56 64 62 65 46 75 6e 63 20 56 64 62 65 46 75 6e  VdbeFunc VdbeFun
4e3a0 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  c;.typedef struc
4e3b0 74 20 4d 65 6d 20 4d 65 6d 3b 0a 74 79 70 65 64  t Mem Mem;.typed
4e3c0 65 66 20 73 74 72 75 63 74 20 53 75 62 50 72 6f  ef struct SubPro
4e3d0 67 72 61 6d 20 53 75 62 50 72 6f 67 72 61 6d 3b  gram SubProgram;
4e3e0 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65  ../*.** A single
4e3f0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20   instruction of 
4e400 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
4e410 69 6e 65 20 68 61 73 20 61 6e 20 6f 70 63 6f 64  ine has an opcod
4e420 65 0a 2a 2a 20 61 6e 64 20 61 73 20 6d 61 6e 79  e.** and as many
4e430 20 61 73 20 74 68 72 65 65 20 6f 70 65 72 61 6e   as three operan
4e440 64 73 2e 20 20 54 68 65 20 69 6e 73 74 72 75 63  ds.  The instruc
4e450 74 69 6f 6e 20 69 73 20 72 65 63 6f 72 64 65 64  tion is recorded
4e460 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 73 74 61 6e  .** as an instan
4e470 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
4e480 69 6e 67 20 73 74 72 75 63 74 75 72 65 3a 0a 2a  ing structure:.*
4e490 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70 20  /.struct VdbeOp 
4e4a0 7b 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20  {.  u8 opcode;  
4e4b0 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20          /* What 
4e4c0 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72  operation to per
4e4d0 66 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64  form */.  signed
4e4e0 20 63 68 61 72 20 70 34 74 79 70 65 3b 20 2f 2a   char p4type; /*
4e4f0 20 4f 6e 65 20 6f 66 20 74 68 65 20 50 34 5f 78   One of the P4_x
4e500 78 78 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72  xx constants for
4e510 20 70 34 20 2a 2f 0a 20 20 75 38 20 6f 70 66 6c   p4 */.  u8 opfl
4e520 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ags;         /* 
4e530 4d 61 73 6b 20 6f 66 20 74 68 65 20 4f 50 46 4c  Mask of the OPFL
4e540 47 5f 2a 20 66 6c 61 67 73 20 69 6e 20 6f 70 63  G_* flags in opc
4e550 6f 64 65 73 2e 68 20 2a 2f 0a 20 20 75 38 20 70  odes.h */.  u8 p
4e560 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  5;              
4e570 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 74  /* Fifth paramet
4e580 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65  er is an unsigne
4e590 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  d character */. 
4e5a0 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20   int p1;        
4e5b0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70       /* First op
4e5c0 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70  erand */.  int p
4e5d0 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  2;             /
4e5e0 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  * Second paramet
4e5f0 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75  er (often the ju
4e600 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20  mp destination) 
4e610 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 20  */.  int p3;    
4e620 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
4e630 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
4e640 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20  */.  union {    
4e650 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75 72           /* four
4e660 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a  th parameter */.
4e670 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
4e680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
4e690 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 70  teger value if p
4e6a0 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20  4type==P4_INT32 
4e6b0 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b 20  */.    void *p; 
4e6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4e6d0 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72   Generic pointer
4e6e0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b   */.    char *z;
4e6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4e700 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74  * Pointer to dat
4e710 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 68  a for string (ch
4e720 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73 20  ar array) types 
4e730 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 34  */.    i64 *pI64
4e740 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
4e750 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
4e760 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a 2f  e is P4_INT64 */
4e770 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 65  .    double *pRe
4e780 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55  al;         /* U
4e790 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20  sed when p4type 
4e7a0 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 20  is P4_REAL */.  
4e7b0 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63    FuncDef *pFunc
4e7c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64  ;        /* Used
4e7d0 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
4e7e0 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 20  P4_FUNCDEF */.  
4e7f0 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62    VdbeFunc *pVdb
4e800 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64  eFunc;   /* Used
4e810 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
4e820 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a 20  P4_VDBEFUNC */. 
4e830 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
4e840 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  l;        /* Use
4e850 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4e860 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a 20   P4_COLLSEQ */. 
4e870 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20     Mem *pMem;   
4e880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
4e890 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4e8a0 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 56   P4_MEM */.    V
4e8b0 54 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 20  Table *pVtab;   
4e8c0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68        /* Used wh
4e8d0 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f  en p4type is P4_
4e8e0 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 49  VTAB */.    KeyI
4e8f0 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20  nfo *pKeyInfo;  
4e900 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
4e910 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 59  p4type is P4_KEY
4e920 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 20  INFO */.    int 
4e930 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  *ai;            
4e940 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
4e950 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54  p4type is P4_INT
4e960 41 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75 62  ARRAY */.    Sub
4e970 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61  Program *pProgra
4e980 6d 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e  m;  /* Used when
4e990 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 53 55   p4type is P4_SU
4e9a0 42 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d 20  BPROGRAM */.  } 
4e9b0 70 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  p4;.#ifdef SQLIT
4e9c0 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a  E_DEBUG.  char *
4e9d0 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20  zComment;       
4e9e0 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f     /* Comment to
4e9f0 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69   improve readabi
4ea00 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  lity */.#endif.#
4ea10 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
4ea20 4c 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  LE.  int cnt;   
4ea30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4ea40 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   Number of times
4ea50 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
4ea60 6e 20 77 61 73 20 65 78 65 63 75 74 65 64 20 2a  n was executed *
4ea70 2f 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20  /.  u64 cycles; 
4ea80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4ea90 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74  Total time spent
4eaa0 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20   executing this 
4eab0 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23  instruction */.#
4eac0 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66  endif.};.typedef
4ead0 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 56   struct VdbeOp V
4eae0 64 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41  dbeOp;.../*.** A
4eaf0 20 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 65   sub-routine use
4eb00 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  d to implement a
4eb10 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
4eb20 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62 50  ..*/.struct SubP
4eb30 72 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65 4f  rogram {.  VdbeO
4eb40 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20  p *aOp;         
4eb50 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
4eb60 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72  y of opcodes for
4eb70 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a   sub-program */.
4eb80 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20    int nOp;      
4eb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4eba0 2f 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61  /* Elements in a
4ebb0 4f 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  Op[] */.  int nM
4ebc0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
4ebd0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
4ebe0 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  r of memory cell
4ebf0 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  s required */.  
4ec00 69 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20 20  int nCsr;       
4ec10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4ec20 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f   Number of curso
4ec30 72 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  rs required */. 
4ec40 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
4ec50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4ec60 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e  * Number of poin
4ec70 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72  ters to this str
4ec80 75 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69 64  ucture */.  void
4ec90 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20   *token;        
4eca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 20            /* id 
4ecb0 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64  that may be used
4ecc0 20 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 72   to recursive tr
4ecd0 69 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  iggers */.};../*
4ece0 0a 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65  .** A smaller ve
4ecf0 72 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20  rsion of VdbeOp 
4ed00 75 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62  used for the Vdb
4ed10 65 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e  eAddOpList() fun
4ed20 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a  ction because.**
4ed30 20 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73   it takes up les
4ed40 73 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75  s space..*/.stru
4ed50 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a  ct VdbeOpList {.
4ed60 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20    u8 opcode;    
4ed70 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70        /* What op
4ed80 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f  eration to perfo
4ed90 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63  rm */.  signed c
4eda0 68 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46  har p1;     /* F
4edb0 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  irst operand */.
4edc0 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32    signed char p2
4edd0 3b 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20  ;     /* Second 
4ede0 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e  parameter (often
4edf0 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e   the jump destin
4ee00 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e  ation) */.  sign
4ee10 65 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20  ed char p3;     
4ee20 2f 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74  /* Third paramet
4ee30 65 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66  er */.};.typedef
4ee40 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69   struct VdbeOpLi
4ee50 73 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a  st VdbeOpList;..
4ee60 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
4ee70 6c 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70  lues of VdbeOp.p
4ee80 34 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65  4type.*/.#define
4ee90 20 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30   P4_NOTUSED    0
4eea0 20 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72     /* The P4 par
4eeb0 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73  ameter is not us
4eec0 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  ed */.#define P4
4eed0 5f 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20  _DYNAMIC  (-1)  
4eee0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20  /* Pointer to a 
4eef0 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20  string obtained 
4ef00 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  from sqliteMallo
4ef10 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  c() */.#define P
4ef20 34 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20  4_STATIC   (-2) 
4ef30 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
4ef40 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a   static string *
4ef50 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c  /.#define P4_COL
4ef60 4c 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50  LSEQ  (-4)  /* P
4ef70 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
4ef80 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75  o a CollSeq stru
4ef90 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65  cture */.#define
4efa0 20 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35   P4_FUNCDEF  (-5
4efb0 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f  )  /* P4 is a po
4efc0 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44  inter to a FuncD
4efd0 65 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  ef structure */.
4efe0 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e  #define P4_KEYIN
4eff0 46 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20  FO  (-6)  /* P4 
4f000 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
4f010 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  a KeyInfo struct
4f020 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
4f030 34 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20  4_VDBEFUNC (-7) 
4f040 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
4f050 74 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e  ter to a VdbeFun
4f060 63 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23  c structure */.#
4f070 64 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20  define P4_MEM   
4f080 20 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69     (-8)  /* P4 i
4f090 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
4f0a0 20 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75   Mem*    structu
4f0b0 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  re */.#define P4
4f0c0 5f 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20  _TRANSIENT (-9) 
4f0d0 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  /* P4 is a point
4f0e0 65 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e  er to a transien
4f0f0 74 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66  t string */.#def
4f100 69 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20  ine P4_VTAB     
4f110 28 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61  (-10) /* P4 is a
4f120 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
4f130 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
4f140 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65  cture */.#define
4f150 20 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31   P4_MPRINTF  (-1
4f160 31 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74  1) /* P4 is a st
4f170 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72  ring obtained fr
4f180 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  om sqlite3_mprin
4f190 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tf() */.#define 
4f1a0 50 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32  P4_REAL     (-12
4f1b0 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d  ) /* P4 is a 64-
4f1c0 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  bit floating poi
4f1d0 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66  nt value */.#def
4f1e0 69 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20  ine P4_INT64    
4f1f0 28 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61  (-13) /* P4 is a
4f200 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
4f210 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e  nteger */.#defin
4f220 65 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d  e P4_INT32    (-
4f230 31 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33  14) /* P4 is a 3
4f240 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  2-bit signed int
4f250 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  eger */.#define 
4f260 50 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35  P4_INTARRAY (-15
4f270 29 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63  ) /* P4 is a vec
4f280 74 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e  tor of 32-bit in
4f290 74 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e  tegers */.#defin
4f2a0 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20  e P4_SUBPROGRAM 
4f2b0 20 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 20   (-18) /* P4 is 
4f2c0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53  a pointer to a S
4f2d0 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74  ubProgram struct
4f2e0 75 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20  ure */../* When 
4f2f0 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75  adding a P4 argu
4f300 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45  ment using P4_KE
4f310 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66  YINFO, a copy of
4f320 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
4f330 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64  ucture.** is mad
4f340 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73  e.  That copy is
4f350 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20   freed when the 
4f360 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65  Vdbe is finalize
4f370 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a  d.  But if the.*
4f380 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34  * argument is P4
4f390 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
4f3a0 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20  , the passed in 
4f3b0 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e  pointer is used.
4f3c0 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65    It still.** ge
4f3d0 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68  ts freed when th
4f3e0 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69  e Vdbe is finali
4f3f0 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20  zed so it still 
4f400 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e  should be obtain
4f410 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e  ed.** from a sin
4f420 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  gle sqliteMalloc
4f430 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79  ().  But no copy
4f440 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65   is made and the
4f450 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63   calling.** func
4f460 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74  tion should *not
4f470 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68  * try to free th
4f480 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64  e KeyInfo..*/.#d
4f490 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f  efine P4_KEYINFO
4f4a0 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a 23  _HANDOFF (-16).#
4f4b0 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
4f4c0 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 0a  O_STATIC  (-17).
4f4d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e  ./*.** The Vdbe.
4f4e0 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63  aColName array c
4f4f0 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73  ontains 5n Mem s
4f500 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 65  tructures, where
4f510 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75   n is the .** nu
4f520 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
4f530 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 64  of data returned
4f540 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
4f550 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f  t..*/.#define CO
4f560 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30  LNAME_NAME     0
4f570 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
4f580 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66  _DECLTYPE 1.#def
4f590 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41  ine COLNAME_DATA
4f5a0 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43  BASE 2.#define C
4f5b0 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20  OLNAME_TABLE    
4f5c0 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  3.#define COLNAM
4f5d0 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66  E_COLUMN   4.#if
4f5e0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
4f5f0 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
4f600 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41  A.# define COLNA
4f610 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20  ME_N        5   
4f620 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
4f630 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62  COLNAME_xxx symb
4f640 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69  ols */.#else.# i
4f650 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
4f660 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65  _DECLTYPE.#   de
4f670 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20  fine COLNAME_N  
4f680 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74      1      /* St
4f690 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d  ore only the nam
4f6a0 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20  e */.# else.#   
4f6b0 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e  define COLNAME_N
4f6c0 20 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20        2      /* 
4f6d0 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61  Store the name a
4f6e0 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23  nd decltype */.#
4f6f0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f   endif.#endif../
4f700 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
4f710 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74  ng macro convert
4f720 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 64  s a relative add
4f730 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 66  ress in the p2 f
4f740 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62  ield.** of a Vdb
4f750 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 6e  eOp structure in
4f760 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75  to a negative nu
4f770 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a  mber so that .**
4f780 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
4f790 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68  pList() knows th
4f7a0 61 74 20 74 68 65 20 61 64 64 72 65 73 73 20 69  at the address i
4f7b0 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c  s relative.  Cal
4f7c0 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72  ling.** the macr
4f7d0 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73  o again restores
4f7e0 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f   the address..*/
4f7f0 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 29  .#define ADDR(X)
4f800 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a    (-1-(X))../*.*
4f810 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73  * The makefile s
4f820 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20  cans the vdbe.c 
4f830 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20  source file and 
4f840 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63  creates the "opc
4f850 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65  odes.h".** heade
4f860 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 69  r file that defi
4f870 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72  nes a number for
4f880 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65   each opcode use
4f890 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a  d by the VDBE..*
4f8a0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4f8b0 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65  * Include opcode
4f8c0 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  s.h in the middl
4f8d0 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a  e of vdbe.h ****
4f8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f8f0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4f900 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63  * Begin file opc
4f910 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  odes.h *********
4f920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f940 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c  /./* Automatical
4f950 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44  ly generated.  D
4f960 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a  o not edit */./*
4f970 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64   See the mkopcod
4f980 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f  eh.awk script fo
4f990 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65  r details */.#de
4f9a0 66 69 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20  fine OP_Goto    
4f9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23               1.#
4f9d0 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 20  define OP_Gosub 
4f9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
4fa00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 74 75  .#define OP_Retu
4fa10 72 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rn              
4fa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa30 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59 69   3.#define OP_Yi
4fa40 65 6c 64 20 20 20 20 20 20 20 20 20 20 20 20 20  eld             
4fa50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa60 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f     4.#define OP_
4fa70 48 61 6c 74 49 66 4e 75 6c 6c 20 20 20 20 20 20  HaltIfNull      
4fa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa90 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 4f       5.#define O
4faa0 50 5f 48 61 6c 74 20 20 20 20 20 20 20 20 20 20  P_Halt          
4fab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fac0 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65         6.#define
4fad0 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20 20   OP_Integer     
4fae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4faf0 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69           7.#defi
4fb00 6e 65 20 4f 50 5f 49 6e 74 36 34 20 20 20 20 20  ne OP_Int64     
4fb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65             8.#de
4fb30 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20 20 20 20  fine OP_Real    
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb50 20 20 20 20 20 20 20 20 20 20 20 31 33 30 20 20             130  
4fb60 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 46   /* same as TK_F
4fb70 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64 65 66 69  LOAT    */.#defi
4fb80 6e 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20  ne OP_String8   
4fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fba0 20 20 20 20 20 20 20 20 20 20 39 34 20 20 20 2f            94   /
4fbb0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52  * same as TK_STR
4fbc0 49 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  ING   */.#define
4fbd0 20 4f 50 5f 53 74 72 69 6e 67 20 20 20 20 20 20   OP_String      
4fbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbf0 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69           9.#defi
4fc00 6e 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20 20 20  ne OP_Null      
4fc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65            10.#de
4fc30 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20 20  fine OP_Blob    
4fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc50 20 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23              11.#
4fc60 64 65 66 69 6e 65 20 4f 50 5f 56 61 72 69 61 62  define OP_Variab
4fc70 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  le              
4fc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
4fc90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65  .#define OP_Move
4fca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcc0 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  13.#define OP_Co
4fcd0 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  py              
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcf0 20 20 31 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f    14.#define OP_
4fd00 53 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 20  SCopy           
4fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 4f      15.#define O
4fd30 50 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20 20  P_ResultRow     
4fd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd50 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65        16.#define
4fd60 20 4f 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 20   OP_Concat      
4fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd80 20 20 20 20 20 20 20 20 39 31 20 20 20 2f 2a 20          91   /* 
4fd90 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41  same as TK_CONCA
4fda0 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  T   */.#define O
4fdb0 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 20 20  P_Add           
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fdd0 20 20 20 20 20 20 38 36 20 20 20 2f 2a 20 73 61        86   /* sa
4fde0 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 20 20  me as TK_PLUS   
4fdf0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4fe00 53 75 62 74 72 61 63 74 20 20 20 20 20 20 20 20  Subtract        
4fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe20 20 20 20 20 38 37 20 20 20 2f 2a 20 73 61 6d 65      87   /* same
4fe30 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20   as TK_MINUS    
4fe40 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75  */.#define OP_Mu
4fe50 6c 74 69 70 6c 79 20 20 20 20 20 20 20 20 20 20  ltiply          
4fe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe70 20 20 38 38 20 20 20 2f 2a 20 73 61 6d 65 20 61    88   /* same a
4fe80 73 20 54 4b 5f 53 54 41 52 20 20 20 20 20 2a 2f  s TK_STAR     */
4fe90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 69  .#define OP_Divi
4fea0 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  de              
4feb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fec0 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  89   /* same as 
4fed0 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a 23  TK_SLASH    */.#
4fee0 64 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e  define OP_Remain
4fef0 64 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  der             
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30                90
4ff10 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4ff20 5f 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65  _REM      */.#de
4ff30 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20  fine OP_CollSeq 
4ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff50 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 23              17.#
4ff60 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 63 74 69  define OP_Functi
4ff70 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
4ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 38                18
4ff90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 41  .#define OP_BitA
4ffa0 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nd              
4ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffc0 38 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  82   /* same as 
4ffd0 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0a 23  TK_BITAND   */.#
4ffe0 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20  define OP_BitOr 
4fff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33                83
50010 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50020 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65  _BITOR    */.#de
50030 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 66  fine OP_ShiftLef
50040 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
50050 20 20 20 20 20 20 20 20 20 20 20 20 38 34 20 20              84  
50060 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
50070 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69  SHIFT   */.#defi
50080 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74  ne OP_ShiftRight
50090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500a0 20 20 20 20 20 20 20 20 20 20 38 35 20 20 20 2f            85   /
500b0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48  * same as TK_RSH
500c0 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  IFT   */.#define
500d0 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 20   OP_AddImm      
500e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500f0 20 20 20 20 20 20 20 20 32 30 0a 23 64 65 66 69          20.#defi
50100 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20  ne OP_MustBeInt 
50110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50120 20 20 20 20 20 20 20 20 20 20 32 31 0a 23 64 65            21.#de
50130 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 66 66 69  fine OP_RealAffi
50140 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20  nity            
50150 20 20 20 20 20 20 20 20 20 20 20 20 32 32 0a 23              22.#
50160 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74  define OP_ToText
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50180 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 31               141
50190 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
501a0 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65  _TO_TEXT  */.#de
501b0 66 69 6e 65 20 4f 50 5f 54 6f 42 6c 6f 62 20 20  fine OP_ToBlob  
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501d0 20 20 20 20 20 20 20 20 20 20 20 31 34 32 20 20             142  
501e0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
501f0 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 64 65 66 69  O_BLOB  */.#defi
50200 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20  ne OP_ToNumeric 
50210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50220 20 20 20 20 20 20 20 20 20 31 34 33 20 20 20 2f           143   /
50230 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
50240 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69 6e  NUMERIC*/.#defin
50250 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20  e OP_ToInt      
50260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50270 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a          144   /*
50280 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49   same as TK_TO_I
50290 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NT   */.#define 
502a0 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20 20  OP_ToReal       
502b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
502c0 20 20 20 20 20 20 31 34 35 20 20 20 2f 2a 20 73        145   /* s
502d0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41  ame as TK_TO_REA
502e0 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  L  */.#define OP
502f0 5f 45 71 20 20 20 20 20 20 20 20 20 20 20 20 20  _Eq             
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50310 20 20 20 20 20 37 36 20 20 20 2f 2a 20 73 61 6d       76   /* sam
50320 65 20 61 73 20 54 4b 5f 45 51 20 20 20 20 20 20  e as TK_EQ      
50330 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e   */.#define OP_N
50340 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
50350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50360 20 20 20 37 35 20 20 20 2f 2a 20 73 61 6d 65 20     75   /* same 
50370 61 73 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 2a  as TK_NE       *
50380 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20  /.#define OP_Lt 
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503b0 20 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   79   /* same as
503c0 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a   TK_LT       */.
503d0 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20  #define OP_Le   
503e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
50400 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  8   /* same as T
50410 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_LE       */.#d
50420 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20  efine OP_Gt     
50430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50440 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 20               77 
50450 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
50460 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  GT       */.#def
50470 69 6e 65 20 4f 50 5f 47 65 20 20 20 20 20 20 20  ine OP_Ge       
50480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50490 20 20 20 20 20 20 20 20 20 20 20 38 30 20 20 20             80   
504a0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45  /* same as TK_GE
504b0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
504c0 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e  e OP_Permutation
504d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504e0 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66           23.#def
504f0 69 6e 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 20  ine OP_Compare  
50500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50510 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64             24.#d
50520 65 66 69 6e 65 20 4f 50 5f 4a 75 6d 70 20 20 20  efine OP_Jump   
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50540 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35 0a               25.
50550 23 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20 20  #define OP_And  
50560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
50580 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  9   /* same as T
50590 4b 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23 64  K_AND      */.#d
505a0 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20  efine OP_Or     
505b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505c0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 20               68 
505d0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
505e0 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  OR       */.#def
505f0 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 20  ine OP_Not      
50600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50610 20 20 20 20 20 20 20 20 20 20 20 31 39 20 20 20             19   
50620 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f  /* same as TK_NO
50630 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  T      */.#defin
50640 65 20 4f 50 5f 42 69 74 4e 6f 74 20 20 20 20 20  e OP_BitNot     
50650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50660 20 20 20 20 20 20 20 20 20 39 33 20 20 20 2f 2a           93   /*
50670 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e   same as TK_BITN
50680 4f 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  OT   */.#define 
50690 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 20  OP_If           
506a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506b0 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e         26.#defin
506c0 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20  e OP_IfNot      
506d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506e0 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 66           27.#def
506f0 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20  ine OP_IsNull   
50700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50710 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20             73   
50720 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53  /* same as TK_IS
50730 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e  NULL   */.#defin
50740 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20  e OP_NotNull    
50750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50760 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a           74   /*
50770 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e   same as TK_NOTN
50780 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ULL  */.#define 
50790 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20  OP_Column       
507a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507b0 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e         28.#defin
507c0 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20  e OP_Affinity   
507d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507e0 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66           29.#def
507f0 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  ine OP_MakeRecor
50800 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
50810 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 64             30.#d
50820 65 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20  efine OP_Count  
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50840 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a               31.
50850 23 64 65 66 69 6e 65 20 4f 50 5f 53 61 76 65 70  #define OP_Savep
50860 6f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  oint            
50870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
50880 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 74  2.#define OP_Aut
50890 6f 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20  oCommit         
508a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508b0 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54   33.#define OP_T
508c0 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 20  ransaction      
508d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508e0 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 4f 50     34.#define OP
508f0 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 20  _ReadCookie     
50900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50910 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20       35.#define 
50920 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 20 20 20 20  OP_SetCookie    
50930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50940 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e         36.#defin
50950 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69  e OP_VerifyCooki
50960 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
50970 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 66           37.#def
50980 69 6e 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20  ine OP_OpenRead 
50990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509a0 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64             38.#d
509b0 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 57 72 69  efine OP_OpenWri
509c0 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a               39.
509e0 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45  #define OP_OpenE
509f0 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20  phemeral        
50a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
50a10 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65  0.#define OP_Ope
50a20 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 20  nPseudo         
50a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a40 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43   41.#define OP_C
50a50 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 20  lose            
50a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a70 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 4f 50     42.#define OP
50a80 5f 53 65 65 6b 4c 74 20 20 20 20 20 20 20 20 20  _SeekLt         
50a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50aa0 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20       43.#define 
50ab0 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 20  OP_SeekLe       
50ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ad0 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e         44.#defin
50ae0 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 20 20 20  e OP_SeekGe     
50af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b00 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 66           45.#def
50b10 69 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 20  ine OP_SeekGt   
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b30 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64             46.#d
50b40 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20 20 20  efine OP_Seek   
50b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b60 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a               47.
50b70 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f  #define OP_NotFo
50b80 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  und             
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
50ba0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75  8.#define OP_Fou
50bb0 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nd              
50bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bd0 20 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   49.#define OP_I
50be0 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 20  sUnique         
50bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c00 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50     50.#define OP
50c10 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 20 20  _NotExists      
50c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c30 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20       51.#define 
50c40 4f 50 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20  OP_Sequence     
50c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c60 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e         52.#defin
50c70 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20  e OP_NewRowid   
50c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c90 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 66           53.#def
50ca0 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20 20 20  ine OP_Insert   
50cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cc0 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64             54.#d
50cd0 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 49  efine OP_InsertI
50ce0 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a               55.
50d00 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 65 74  #define OP_Delet
50d10 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
50d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
50d30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73  6.#define OP_Res
50d40 65 74 43 6f 75 6e 74 20 20 20 20 20 20 20 20 20  etCount         
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d60 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52   57.#define OP_R
50d70 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20 20 20  owKey           
50d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d90 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f 50     58.#define OP
50da0 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 20  _RowData        
50db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50dc0 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 20       59.#define 
50dd0 4f 50 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20  OP_Rowid        
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50df0 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69 6e         60.#defin
50e00 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20  e OP_NullRow    
50e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 66           61.#def
50e30 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20  ine OP_Last     
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e50 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23 64             62.#d
50e60 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20  efine OP_Sort   
50e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e80 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33 0a               63.
50e90 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 77 69 6e  #define OP_Rewin
50ea0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
50eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
50ec0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 65  4.#define OP_Pre
50ed0 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v               
50ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ef0 20 36 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e   65.#define OP_N
50f00 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ext             
50f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f20 20 20 20 36 36 0a 23 64 65 66 69 6e 65 20 4f 50     66.#define OP
50f30 5f 49 64 78 49 6e 73 65 72 74 20 20 20 20 20 20  _IdxInsert      
50f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f50 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20       67.#define 
50f60 4f 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20 20  OP_IdxDelete    
50f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f80 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e         70.#defin
50f90 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 20  e OP_IdxRowid   
50fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fb0 20 20 20 20 20 20 20 20 20 37 31 0a 23 64 65 66           71.#def
50fc0 69 6e 65 20 4f 50 5f 49 64 78 4c 54 20 20 20 20  ine OP_IdxLT    
50fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fe0 20 20 20 20 20 20 20 20 20 20 20 37 32 0a 23 64             72.#d
50ff0 65 66 69 6e 65 20 4f 50 5f 49 64 78 47 45 20 20  efine OP_IdxGE  
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51010 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31 0a               81.
51020 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74 72  #define OP_Destr
51030 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  oy              
51040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
51050 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 65  2.#define OP_Cle
51060 61 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ar              
51070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51080 20 39 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43   95.#define OP_C
51090 72 65 61 74 65 49 6e 64 65 78 20 20 20 20 20 20  reateIndex      
510a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510b0 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 4f 50     96.#define OP
510c0 5f 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 20  _CreateTable    
510d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510e0 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 20       97.#define 
510f0 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 20  OP_ParseSchema  
51100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51110 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e         98.#defin
51120 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69  e OP_LoadAnalysi
51130 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
51140 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66           99.#def
51150 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65  ine OP_DropTable
51160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51170 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64            100.#d
51180 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e 64  efine OP_DropInd
51190 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ex              
511a0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 31 0a              101.
511b0 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54  #define OP_DropT
511c0 72 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20  rigger          
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
511e0 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74  2.#define OP_Int
511f0 65 67 72 69 74 79 43 6b 20 20 20 20 20 20 20 20  egrityCk        
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51210 31 30 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52  103.#define OP_R
51220 6f 77 53 65 74 41 64 64 20 20 20 20 20 20 20 20  owSetAdd        
51230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51240 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 4f 50    104.#define OP
51250 5f 52 6f 77 53 65 74 52 65 61 64 20 20 20 20 20  _RowSetRead     
51260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51270 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65 20      105.#define 
51280 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20 20  OP_RowSetTest   
51290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
512a0 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e        106.#defin
512b0 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20  e OP_Program    
512c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
512d0 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66          107.#def
512e0 69 6e 65 20 4f 50 5f 50 61 72 61 6d 20 20 20 20  ine OP_Param    
512f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51300 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64            108.#d
51310 65 66 69 6e 65 20 4f 50 5f 46 6b 43 6f 75 6e 74  efine OP_FkCount
51320 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
51330 20 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a              109.
51340 23 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49 66 5a  #define OP_FkIfZ
51350 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20  ero             
51360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
51370 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d  0.#define OP_Mem
51380 4d 61 78 20 20 20 20 20 20 20 20 20 20 20 20 20  Max             
51390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513a0 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49  111.#define OP_I
513b0 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 20  fPos            
513c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513d0 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50    112.#define OP
513e0 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20 20 20  _IfNeg          
513f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51400 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20      113.#define 
51410 4f 50 5f 49 66 5a 65 72 6f 20 20 20 20 20 20 20  OP_IfZero       
51420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51430 20 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e        114.#defin
51440 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20 20  e OP_AggStep    
51450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51460 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66          115.#def
51470 69 6e 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 20  ine OP_AggFinal 
51480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51490 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64            116.#d
514a0 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20  efine OP_Vacuum 
514b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514c0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a              117.
514d0 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56  #define OP_IncrV
514e0 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20  acuum           
514f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
51500 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70  8.#define OP_Exp
51510 69 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ire             
51520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51530 31 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54  119.#define OP_T
51540 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20 20  ableLock        
51550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51560 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 50    120.#define OP
51570 5f 56 42 65 67 69 6e 20 20 20 20 20 20 20 20 20  _VBegin         
51580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51590 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20      121.#define 
515a0 4f 50 5f 56 43 72 65 61 74 65 20 20 20 20 20 20  OP_VCreate      
515b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515c0 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e        122.#defin
515d0 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 20  e OP_VDestroy   
515e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515f0 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 66          123.#def
51600 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 20  ine OP_VOpen    
51610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51620 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 64            124.#d
51630 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72  efine OP_VFilter
51640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51650 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0a              125.
51660 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c 75  #define OP_VColu
51670 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mn              
51680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
51690 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4e 65  6.#define OP_VNe
516a0 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xt              
516b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
516c0 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56  127.#define OP_V
516d0 52 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20  Rename          
516e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
516f0 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50    128.#define OP
51700 5f 56 55 70 64 61 74 65 20 20 20 20 20 20 20 20  _VUpdate        
51710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51720 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20      129.#define 
51730 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20  OP_Pagecount    
51740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51750 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e        131.#defin
51760 65 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20  e OP_Trace      
51770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51780 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 66          132.#def
51790 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20 20  ine OP_Noop     
517a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517b0 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 64            133.#d
517c0 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e  efine OP_Explain
517d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517e0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 0a              134.
517f0 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ./* The followin
51800 67 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20  g opcode values 
51810 61 72 65 20 6e 65 76 65 72 20 75 73 65 64 20 2a  are never used *
51820 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  /.#define OP_Not
51830 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 20  Used_135        
51840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51850 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  135.#define OP_N
51860 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 20  otUsed_136      
51870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51880 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50    136.#define OP
51890 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20  _NotUsed_137    
518a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
518b0 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20      137.#define 
518c0 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 20  OP_NotUsed_138  
518d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
518e0 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e        138.#defin
518f0 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 39  e OP_NotUsed_139
51900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51910 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66          139.#def
51920 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31  ine OP_NotUsed_1
51930 34 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  40              
51940 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 0a            140...
51950 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 75  /* Properties su
51960 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 20  ch as "out2" or 
51970 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 20  "jump" that are 
51980 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20  specified in.** 
51990 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69  comments followi
519a0 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 66 6f  ng the "case" fo
519b0 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 6e  r each opcode in
519c0 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 61   the vdbe.c.** a
519d0 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f 20  re encoded into 
519e0 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 6f  bitvectors as fo
519f0 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e  llows:.*/.#defin
51a00 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 20  e OPFLG_JUMP    
51a10 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20          0x0001  
51a20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f 6c  /* jump:  P2 hol
51a30 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a 2f  ds jmp target */
51a40 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f  .#define OPFLG_O
51a50 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 30  UT2_PRERELEASE 0
51a60 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 70  x0002  /* out2-p
51a70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 64  rerelease: */.#d
51a80 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20  efine OPFLG_IN1 
51a90 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30              0x00
51aa0 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31  04  /* in1:   P1
51ab0 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a   is an input */.
51ac0 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e  #define OPFLG_IN
51ad0 32 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78  2             0x
51ae0 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20  0008  /* in2:   
51af0 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a  P2 is an input *
51b00 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
51b10 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20  IN3             
51b20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20  0x0010  /* in3: 
51b30 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74    P3 is an input
51b40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c   */.#define OPFL
51b50 47 5f 4f 55 54 32 20 20 20 20 20 20 20 20 20 20  G_OUT2          
51b60 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74    0x0020  /* out
51b70 32 3a 20 20 50 32 20 69 73 20 61 6e 20 6f 75 74  2:  P2 is an out
51b80 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  put */.#define O
51b90 50 46 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 20  PFLG_OUT3       
51ba0 20 20 20 20 20 30 78 30 30 34 30 20 20 2f 2a 20       0x0040  /* 
51bb0 6f 75 74 33 3a 20 20 50 33 20 69 73 20 61 6e 20  out3:  P3 is an 
51bc0 6f 75 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e  output */.#defin
51bd0 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49  e OPFLG_INITIALI
51be0 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f  ZER {\./*   0 */
51bf0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
51c00 35 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30  5, 0x04, 0x04, 0
51c10 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c  x10, 0x00, 0x02,
51c20 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 32  \./*   8 */ 0x02
51c30 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
51c40 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  02, 0x00, 0x00, 
51c50 30 78 32 34 2c 20 30 78 32 34 2c 5c 0a 2f 2a 20  0x24, 0x24,\./* 
51c60 20 31 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 30   16 */ 0x00, 0x0
51c70 30 2c 20 30 78 30 30 2c 20 30 78 32 34 2c 20 30  0, 0x00, 0x24, 0
51c80 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 34 2c  x04, 0x05, 0x04,
51c90 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 32 34 20 2a   0x00,\./*  24 *
51ca0 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78  / 0x00, 0x01, 0x
51cb0 30 35 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 20  05, 0x05, 0x00, 
51cc0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32  0x00, 0x00, 0x02
51cd0 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 30  ,\./*  32 */ 0x0
51ce0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
51cf0 78 30 32 2c 20 30 78 31 30 2c 20 30 78 30 30 2c  x02, 0x10, 0x00,
51d00 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x00, 0x00,\./*
51d10 20 20 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 78    40 */ 0x00, 0x
51d20 30 30 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20  00, 0x00, 0x11, 
51d30 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31  0x11, 0x11, 0x11
51d40 2c 20 30 78 30 38 2c 5c 0a 2f 2a 20 20 34 38 20  , 0x08,\./*  48 
51d50 2a 2f 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30  */ 0x11, 0x11, 0
51d60 78 31 31 2c 20 30 78 31 31 2c 20 30 78 30 32 2c  x11, 0x11, 0x02,
51d70 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30   0x02, 0x00, 0x0
51d80 30 2c 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 78  0,\./*  56 */ 0x
51d90 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
51da0 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 30  0x00, 0x02, 0x00
51db0 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 5c 0a 2f  , 0x01, 0x01,\./
51dc0 2a 20 20 36 34 20 2a 2f 20 30 78 30 31 2c 20 30  *  64 */ 0x01, 0
51dd0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 38 2c  x01, 0x01, 0x08,
51de0 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 30   0x4c, 0x4c, 0x0
51df0 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 37 32  0, 0x02,\./*  72
51e00 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 35 2c 20   */ 0x01, 0x05, 
51e10 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 31 35  0x05, 0x15, 0x15
51e20 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78  , 0x15, 0x15, 0x
51e30 31 35 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 30  15,\./*  80 */ 0
51e40 78 31 35 2c 20 30 78 30 31 2c 20 30 78 34 63 2c  x15, 0x01, 0x4c,
51e50 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34   0x4c, 0x4c, 0x4
51e60 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 5c 0a  c, 0x4c, 0x4c,\.
51e70 2f 2a 20 20 38 38 20 2a 2f 20 30 78 34 63 2c 20  /*  88 */ 0x4c, 
51e80 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63  0x4c, 0x4c, 0x4c
51e90 2c 20 30 78 30 32 2c 20 30 78 32 34 2c 20 30 78  , 0x02, 0x24, 0x
51ea0 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 39  02, 0x00,\./*  9
51eb0 36 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 32 2c  6 */ 0x02, 0x02,
51ec0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
51ed0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
51ee0 78 30 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20  x00,\./* 104 */ 
51ef0 30 78 30 63 2c 20 30 78 34 35 2c 20 30 78 31 35  0x0c, 0x45, 0x15
51f00 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
51f10 30 30 2c 20 30 78 30 31 2c 20 30 78 30 38 2c 5c  00, 0x01, 0x08,\
51f20 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 78 30 35 2c  ./* 112 */ 0x05,
51f30 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 30   0x05, 0x05, 0x0
51f40 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
51f50 78 30 31 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31  x01, 0x00,\./* 1
51f60 32 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30  20 */ 0x00, 0x00
51f70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
51f80 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20  00, 0x01, 0x00, 
51f90 30 78 30 31 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f  0x01,\./* 128 */
51fa0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
51fb0 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  2, 0x02, 0x00, 0
51fc0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
51fd0 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 30  \./* 136 */ 0x00
51fe0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
51ff0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20  00, 0x00, 0x04, 
52000 30 78 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20  0x04, 0x04,\./* 
52010 31 34 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 30  144 */ 0x04, 0x0
52020 34 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  4,}../**********
52030 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f  **** End of opco
52040 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  des.h **********
52050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52070 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
52080 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
52090 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
520a0 66 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a  f in vdbe.h ****
520b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
520c0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74  ***/../*.** Prot
520d0 6f 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 56  otypes for the V
520e0 44 42 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20  DBE interface.  
520f0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20  See comments on 
52100 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
52110 6f 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63  on.** for a desc
52120 72 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20  ription of what 
52130 65 61 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f  each of these ro
52140 75 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a  utines does..*/.
52150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
52160 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65  dbe *sqlite3Vdbe
52170 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29  Create(sqlite3*)
52180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52190 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
521a0 41 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74  AddOp0(Vdbe*,int
521b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
521c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
521d0 65 41 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e  eAddOp1(Vdbe*,in
521e0 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  t,int);.SQLITE_P
521f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
52200 65 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62  e3VdbeAddOp2(Vdb
52210 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b  e*,int,int,int);
52220 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52230 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
52240 64 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c  ddOp3(Vdbe*,int,
52250 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  int,int,int);.SQ
52260 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
52270 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
52280 70 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74  p4(Vdbe*,int,int
52290 2c 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  ,int,int,const c
522a0 68 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53  har *zP4,int);.S
522b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
522c0 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
522d0 4f 70 34 49 6e 74 28 56 64 62 65 2a 2c 69 6e 74  Op4Int(Vdbe*,int
522e0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
522f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52300 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
52310 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65 2a  eAddOpList(Vdbe*
52320 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f  , int nOp, VdbeO
52330 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70  pList const *aOp
52340 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52350 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
52360 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 2a  beChangeP1(Vdbe*
52370 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20  , int addr, int 
52380 50 31 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  P1);.SQLITE_PRIV
52390 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
523a0 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64 62  VdbeChangeP2(Vdb
523b0 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  e*, int addr, in
523c0 74 20 50 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52  t P2);.SQLITE_PR
523d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
523e0 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56  e3VdbeChangeP3(V
523f0 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20  dbe*, int addr, 
52400 69 6e 74 20 50 33 29 3b 0a 53 51 4c 49 54 45 5f  int P3);.SQLITE_
52410 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
52420 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
52430 28 56 64 62 65 2a 2c 20 75 38 20 50 35 29 3b 0a  (Vdbe*, u8 P5);.
52440 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
52450 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4a  oid sqlite3VdbeJ
52460 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c 20 69  umpHere(Vdbe*, i
52470 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 54 45  nt addr);.SQLITE
52480 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
52490 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54  lite3VdbeChangeT
524a0 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69 6e 74  oNoop(Vdbe*, int
524b0 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b 0a 53   addr, int N);.S
524c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
524d0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
524e0 61 6e 67 65 50 34 28 56 64 62 65 2a 2c 20 69 6e  angeP4(Vdbe*, in
524f0 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 68  t addr, const ch
52500 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 4e 29 3b  ar *zP4, int N);
52510 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52520 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
52530 55 73 65 73 42 74 72 65 65 28 56 64 62 65 2a 2c  UsesBtree(Vdbe*,
52540 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
52550 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71  IVATE VdbeOp *sq
52560 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 56  lite3VdbeGetOp(V
52570 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  dbe*, int);.SQLI
52580 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
52590 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
525a0 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  bel(Vdbe*);.SQLI
525b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
525c0 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
525d0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  e(Vdbe*);.SQLITE
525e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
525f0 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
52600 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74  dy(Vdbe*,int,int
52610 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
52620 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52630 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
52640 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 2a 29  eFinalize(Vdbe*)
52650 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52660 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
52670 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64  eResolveLabel(Vd
52680 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  be*, int);.SQLIT
52690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
526a0 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
526b0 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 69 66  Addr(Vdbe*);.#if
526c0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
526d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
526e0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62    int sqlite3Vdb
526f0 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28  eAssertMayAbort(
52700 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  Vdbe *, int);.SQ
52710 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
52720 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54  oid sqlite3VdbeT
52730 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45 2a  race(Vdbe*,FILE*
52740 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  );.#endif.SQLITE
52750 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
52760 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74  lite3VdbeResetSt
52770 65 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29 3b  epResult(Vdbe*);
52780 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52790 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52  int sqlite3VdbeR
527a0 65 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c  eset(Vdbe*);.SQL
527b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
527c0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
527d0 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 6e 74  umCols(Vdbe*,int
527e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
527f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
52800 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62 65  eSetColName(Vdbe
52810 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e  *, int, int, con
52820 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 64 28  st char *, void(
52830 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
52840 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
52850 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74  sqlite3VdbeCount
52860 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b 0a  Changes(Vdbe*);.
52870 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
52880 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56  qlite3 *sqlite3V
52890 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53 51  dbeDb(Vdbe*);.SQ
528a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
528b0 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
528c0 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74  Sql(Vdbe*, const
528d0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
528e0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
528f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
52900 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 2a  e3VdbeSwap(Vdbe*
52910 2c 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  ,Vdbe*);.SQLITE_
52920 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a  PRIVATE VdbeOp *
52930 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65 4f  sqlite3VdbeTakeO
52940 70 41 72 72 61 79 28 56 64 62 65 2a 2c 20 69 6e  pArray(Vdbe*, in
52950 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  t*, int*);.SQLIT
52960 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52970 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67 72 61  qlite3VdbeProgra
52980 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20  mDelete(sqlite3 
52990 2a 2c 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 2c  *, SubProgram *,
529a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
529b0 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61  IVATE sqlite3_va
529c0 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65  lue *sqlite3Vdbe
529d0 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a 2c 20  GetValue(Vdbe*, 
529e0 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45  int, u8);.SQLITE
529f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
52a00 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72 6d  lite3VdbeSetVarm
52a10 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b  ask(Vdbe*, int);
52a20 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
52a30 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
52a40 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f  *sqlite3VdbeReco
52a50 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f  rdUnpack(KeyInfo
52a60 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
52a70 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51  *,char*,int);.SQ
52a80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
52a90 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
52aa0 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
52ab0 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  d(UnpackedRecord
52ac0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
52ad0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
52ae0 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
52af0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
52b00 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29  UnpackedRecord*)
52b10 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  ;...#ifndef NDEB
52b20 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
52b30 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
52b40 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65  VdbeComment(Vdbe
52b50 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
52b60 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56  ...);.# define V
52b70 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73  dbeComment(X)  s
52b80 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e  qlite3VdbeCommen
52b90 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t X.SQLITE_PRIVA
52ba0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
52bb0 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74  3VdbeNoopComment
52bc0 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68  (Vdbe*, const ch
52bd0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66  ar*, ...);.# def
52be0 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ine VdbeNoopComm
52bf0 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 33 56  ent(X)  sqlite3V
52c00 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58  dbeNoopComment X
52c10 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
52c20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23  VdbeComment(X).#
52c30 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70   define VdbeNoop
52c40 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69  Comment(X).#endi
52c50 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  f..#endif../****
52c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
52c70 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.h *******
52c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
52cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
52cc0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
52cd0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
52ce0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
52cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
52d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
52d10 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 74 68  de pager.h in th
52d20 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
52d30 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
52d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
52d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
52d60 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a   file pager.h **
52d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
52da0 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
52db0 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
52dc0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
52dd0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
52de0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
52df0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
52e00 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
52e10 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
52e20 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
52e30 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
52e40 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
52e50 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
52e60 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
52e70 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
52e80 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
52e90 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
52ea0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
52eb0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
52ec0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
52ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52f10 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
52f20 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68   file defines th
52f30 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74  e interface that
52f40 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 67 65   the sqlite page
52f50 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73   cache.** subsys
52f60 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 20 63  tem.  The page c
52f70 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 72  ache subsystem r
52f80 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 20  eads and writes 
52f90 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a  a file a page.**
52fa0 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 70   at a time and p
52fb0 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 6e 61  rovides a journa
52fc0 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a  l for rollback..
52fd0 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47  */..#ifndef _PAG
52fe0 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50  ER_H_.#define _P
52ff0 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44  AGER_H_../*.** D
53000 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73  efault maximum s
53010 69 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 65  ize for persiste
53020 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  nt journal files
53030 2e 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a  . A negative .**
53040 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20   value means no 
53050 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75  limit. This valu
53060 65 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64  e may be overrid
53070 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a  den using the .*
53080 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  * sqlite3PagerJo
53090 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29  urnalSizeLimit()
530a0 20 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22   API. See also "
530b0 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73  PRAGMA journal_s
530c0 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23  ize_limit"..*/.#
530d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
530e0 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49  FAULT_JOURNAL_SI
530f0 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69  ZE_LIMIT.  #defi
53100 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
53110 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c  T_JOURNAL_SIZE_L
53120 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a  IMIT -1.#endif..
53130 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75  /*.** The type u
53140 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74  sed to represent
53150 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20   a page number. 
53160 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20   The first page 
53170 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20  in a file.** is 
53180 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20  called page 1.  
53190 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 70  0 is used to rep
531a0 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61  resent "not a pa
531b0 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ge"..*/.typedef 
531c0 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a  u32 Pgno;../*.**
531d0 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20   Each open file 
531e0 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20  is managed by a 
531f0 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63  separate instanc
53200 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 22  e of the "Pager"
53210 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74   structure..*/.t
53220 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
53230 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a  ger Pager;../*.*
53240 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f  * Handle type fo
53250 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65  r pages..*/.type
53260 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72  def struct PgHdr
53270 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20   DbPage;../*.** 
53280 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45  Page number PAGE
53290 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76  R_MJ_PGNO is nev
532a0 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51  er used in an SQ
532b0 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28 69  Lite database (i
532c0 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64  t is.** reserved
532d0 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f   for working aro
532e0 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f  und a windows/po
532f0 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c  six incompatibil
53300 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75  ity). It is.** u
53310 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  sed in the journ
53320 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68  al to signify th
53330 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72  at the remainder
53340 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
53350 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f  file .** is devo
53360 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61  ted to storing a
53370 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
53380 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65  name - there are
53390 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74   no more pages t
533a0 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20  o.** roll back. 
533b0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72  See comments for
533c0 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d   function writeM
533d0 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69  asterJournal() i
533e0 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f  n pager.c .** fo
533f0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64  r details..*/.#d
53400 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50  efine PAGER_MJ_P
53410 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28  GNO(x) ((Pgno)((
53420 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78  PENDING_BYTE/((x
53430 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29  )->pageSize))+1)
53440 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  )../*.** Allowed
53450 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
53460 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
53470 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  to sqlite3PagerO
53480 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54  pen()..**.** NOT
53490 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 20  E: These values 
534a0 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63  must match the c
534b0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52  orresponding BTR
534c0 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74  EE_ values in bt
534d0 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ree.h..*/.#defin
534e0 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55  e PAGER_OMIT_JOU
534f0 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 20  RNAL  0x0001    
53500 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20  /* Do not use a 
53510 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
53520 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
53530 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20  R_NO_READLOCK   
53540 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69  0x0002    /* Omi
53550 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72  t readlocks on r
53560 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f  eadonly files */
53570 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61  ../*.** Valid va
53580 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63  lues for the sec
53590 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
535a0 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
535b0 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64  ingMode()..*/.#d
535c0 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b  efine PAGER_LOCK
535d0 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20  INGMODE_QUERY   
535e0 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41     -1.#define PA
535f0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
53600 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64  NORMAL      0.#d
53610 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b  efine PAGER_LOCK
53620 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
53630 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c  E   1../*.** Val
53640 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68  id values for th
53650 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
53660 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  t to sqlite3Page
53670 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a  rJournalMode()..
53680 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
53690 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
536a0 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69  RY      -1.#defi
536b0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
536c0 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20  MODE_DELETE     
536d0 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62   0   /* Commit b
536e0 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e  y deleting journ
536f0 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69  al file */.#defi
53700 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
53710 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 20  MODE_PERSIST    
53720 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62   1   /* Commit b
53730 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61  y zeroing journa
53740 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66  l header */.#def
53750 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
53760 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20  LMODE_OFF       
53770 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c    2   /* Journal
53780 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64   omitted.  */.#d
53790 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52  efine PAGER_JOUR
537a0 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
537b0 20 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69      3   /* Commi
537c0 74 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20  t by truncating 
537d0 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69  journal */.#defi
537e0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
537f0 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20  MODE_MEMORY     
53800 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72   4   /* In-memor
53810 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  y journal file *
53820 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d  /../*.** The rem
53830 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66  ainder of this f
53840 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
53850 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66   declarations of
53860 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a   the functions.*
53870 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74  * that make up t
53880 68 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 73  he Pager sub-sys
53890 74 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75  tem API. See sou
538a0 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74  rce code comment
538b0 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61  s for .** a deta
538c0 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e  iled description
538d0 20 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65   of each routine
538e0 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e  ..*/../* Open an
538f0 64 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20  d close a Pager 
53900 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a  connection. */ .
53910 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53920 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  nt sqlite3PagerO
53930 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  pen(.  sqlite3_v
53940 66 73 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70  fs*,.  Pager **p
53950 70 50 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20  pPager,.  const 
53960 63 68 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20  char*,.  int,.  
53970 69 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f  int,.  int,.  vo
53980 69 64 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29  id(*)(DbPage*).)
53990 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
539a0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
539b0 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50  rClose(Pager *pP
539c0 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ager);.SQLITE_PR
539d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
539e0 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65  3PagerReadFilehe
539f0 61 64 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74  ader(Pager*, int
53a00 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  , unsigned char*
53a10 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73  );../* Functions
53a20 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75   used to configu
53a30 72 65 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63  re a Pager objec
53a40 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t. */.SQLITE_PRI
53a50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
53a60 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
53a70 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74  dler(Pager*, int
53a80 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69  (*)(void *), voi
53a90 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  d *);.SQLITE_PRI
53aa0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53ab0 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
53ac0 28 50 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69  (Pager*, u16*, i
53ad0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
53ae0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
53af0 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74  agerMaxPageCount
53b00 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53  (Pager*, int);.S
53b10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
53b20 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
53b30 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65  etCachesize(Page
53b40 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
53b50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
53b60 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66  lite3PagerSetSaf
53b70 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c  etyLevel(Pager*,
53b80 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
53b90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53ba0 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67  ite3PagerLocking
53bb0 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e  Mode(Pager *, in
53bc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
53bd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53be0 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  gerJournalMode(P
53bf0 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  ager *, int);.SQ
53c00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
53c10 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
53c20 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61  rnalSizeLimit(Pa
53c30 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c  ger *, i64);.SQL
53c40 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
53c50 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c  te3_backup **sql
53c60 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50  ite3PagerBackupP
53c70 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20  tr(Pager*);../* 
53c80 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  Functions used t
53c90 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c  o obtain and rel
53ca0 65 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65  ease page refere
53cb0 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45  nces. */ .SQLITE
53cc0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53cd0 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
53ce0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
53cf0 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67  Pgno pgno, DbPag
53d00 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20  e **ppPage, int 
53d10 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e  clrFlag);.#defin
53d20 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  e sqlite3PagerGe
53d30 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33  t(A,B,C) sqlite3
53d40 50 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42  PagerAcquire(A,B
53d50 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49  ,C,0).SQLITE_PRI
53d60 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c  VATE DbPage *sql
53d70 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
53d80 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
53d90 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54  gno pgno);.SQLIT
53da0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
53db0 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44  qlite3PagerRef(D
53dc0 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
53dd0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
53de0 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44  ite3PagerUnref(D
53df0 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65  bPage*);../* Ope
53e00 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20  rations on page 
53e10 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53  references. */.S
53e20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53e30 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  t sqlite3PagerWr
53e40 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  ite(DbPage*);.SQ
53e50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
53e60 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  d sqlite3PagerDo
53e70 6e 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29  ntWrite(DbPage*)
53e80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53e90 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53ea0 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a  rMovepage(Pager*
53eb0 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e  ,DbPage*,Pgno,in
53ec0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
53ed0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53ee0 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
53ef0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
53f00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
53f10 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
53f20 74 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53  ta(DbPage *); .S
53f30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
53f40 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
53f50 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20  GetExtra(DbPage 
53f60 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f  *); ../* Functio
53f70 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67  ns used to manag
53f80 65 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74  e pager transact
53f90 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69  ions and savepoi
53fa0 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts. */.SQLITE_P
53fb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
53fc0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
53fd0 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a  (Pager*, int*);.
53fe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53ff0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42  nt sqlite3PagerB
54000 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74  egin(Pager*, int
54010 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53   exFlag, int);.S
54020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54030 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
54040 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67  mmitPhaseOne(Pag
54050 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a  er*,const char *
54060 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53  zMaster, int);.S
54070 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54080 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  t sqlite3PagerSy
54090 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nc(Pager *pPager
540a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
540b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
540c0 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
540d0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
540e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
540f0 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
54100 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  k(Pager*);.SQLIT
54110 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
54120 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61  lite3PagerOpenSa
54130 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
54140 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53  Pager, int n);.S
54150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54160 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  t sqlite3PagerSa
54170 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
54180 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69  Pager, int op, i
54190 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a  nt iSavepoint);.
541a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
541b0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  nt sqlite3PagerS
541c0 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20  haredLock(Pager 
541d0 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75  *pPager);../* Fu
541e0 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20  nctions used to 
541f0 71 75 65 72 79 20 70 61 67 65 72 20 73 74 61 74  query pager stat
54200 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74  e and configurat
54210 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion. */.SQLITE_P
54220 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65  RIVATE u8 sqlite
54230 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79  3PagerIsreadonly
54240 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
54250 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
54260 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e  ite3PagerRefcoun
54270 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  t(Pager*);.SQLIT
54280 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
54290 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67  char *sqlite3Pag
542a0 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72  erFilename(Pager
542b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
542c0 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  TE const sqlite3
542d0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67  _vfs *sqlite3Pag
542e0 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53  erVfs(Pager*);.S
542f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
54300 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69  lite3_file *sqli
54310 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67  te3PagerFile(Pag
54320 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
54330 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
54340 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75  *sqlite3PagerJou
54350 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29  rnalname(Pager*)
54360 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54370 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54380 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b  rNosync(Pager*);
54390 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
543a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
543b0 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65  erTempSpace(Page
543c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
543d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
543e0 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65  agerIsMemdb(Page
543f0 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f  r*);../* Functio
54400 6e 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63  ns used to trunc
54410 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ate the database
54420 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45   file. */.SQLITE
54430 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
54440 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
54450 74 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50  teImage(Pager*,P
54460 67 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69  gno);../* Functi
54470 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ons to support t
54480 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
54490 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65  ging. */.#if !de
544a0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
544b0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
544c0 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49  TEST).SQLITE_PRI
544d0 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69  VATE   Pgno sqli
544e0 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
544f0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  er(DbPage*);.SQL
54500 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
54510 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
54520 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65  writeable(DbPage
54530 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  *);.#endif.#ifde
54540 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
54550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
54560 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  nt *sqlite3Pager
54570 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53  Stats(Pager*);.S
54580 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
54590 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
545a0 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29  rRefdump(Pager*)
545b0 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65  ;.  void disable
545c0 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
545d0 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f  rors(void);.  vo
545e0 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  id enable_simula
545f0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f  ted_io_errors(vo
54600 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  id);.#else.# def
54610 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  ine disable_simu
54620 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
54630 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c  ).# define enabl
54640 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
54650 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a  rrors().#endif..
54660 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52  #endif /* _PAGER
54670 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
54680 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
54690 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ager.h *********
546a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
546b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
546c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
546d0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
546e0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
546f0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
54700 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
54710 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54720 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
54730 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20  pcache.h in the 
54740 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
54750 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
54760 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54770 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
54780 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a  le pcache.h ****
54790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
547a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
547b0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
547c0 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a  08 August 05.**.
547d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
547e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
547f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
54800 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
54810 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
54820 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
54830 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
54840 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
54850 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
54860 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
54870 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
54880 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
54890 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
548a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
548b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
548c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
548d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
548e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
548f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
54930 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
54940 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  defines the inte
54950 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73  rface that the s
54960 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65  qlite page cache
54970 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a  .** subsystem. .
54980 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 43 41  */..#ifndef _PCA
54990 43 48 45 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20  CHE_H_..typedef 
549a0 73 74 72 75 63 74 20 50 67 48 64 72 20 50 67 48  struct PgHdr PgH
549b0 64 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  dr;.typedef stru
549c0 63 74 20 50 43 61 63 68 65 20 50 43 61 63 68 65  ct PCache PCache
549d0 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72 79 20 70  ;../*.** Every p
549e0 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65  age in the cache
549f0 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62   is controlled b
54a00 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
54a10 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   the following.*
54a20 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  * structure..*/.
54a30 73 74 72 75 63 74 20 50 67 48 64 72 20 7b 0a 20  struct PgHdr {. 
54a40 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 20 20 20   void *pData;   
54a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54a60 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68  /* Content of th
54a70 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 76 6f 69  is page */.  voi
54a80 64 20 2a 70 45 78 74 72 61 3b 20 20 20 20 20 20  d *pExtra;      
54a90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
54aa0 78 74 72 61 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a  xtra content */.
54ab0 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 3b    PgHdr *pDirty;
54ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54ad0 20 2f 2a 20 54 72 61 6e 73 69 65 6e 74 20 6c 69   /* Transient li
54ae0 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65  st of dirty page
54af0 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  s */.  Pgno pgno
54b00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
54b10 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
54b20 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61  mber for this pa
54b30 67 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70  ge */.  Pager *p
54b40 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20  Pager;          
54b50 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
54b60 67 65 72 20 74 68 69 73 20 70 61 67 65 20 69 73  ger this page is
54b70 20 70 61 72 74 20 6f 66 20 2a 2f 0a 23 69 66 64   part of */.#ifd
54b80 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  ef SQLITE_CHECK_
54b90 50 41 47 45 53 0a 20 20 75 33 32 20 70 61 67 65  PAGES.  u32 page
54ba0 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20  Hash;           
54bb0 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 6f         /* Hash o
54bc0 66 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 2a  f page content *
54bd0 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20 66  /.#endif.  u16 f
54be0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
54bf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 47 48            /* PGH
54c00 44 52 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64  DR flags defined
54c10 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20 20 2f 2a 2a   below */..  /**
54c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54c60 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c 65 6d 65 6e  ****.  ** Elemen
54c70 74 73 20 61 62 6f 76 65 20 61 72 65 20 70 75 62  ts above are pub
54c80 6c 69 63 2e 20 20 41 6c 6c 20 74 68 61 74 20 66  lic.  All that f
54c90 6f 6c 6c 6f 77 73 20 69 73 20 70 72 69 76 61 74  ollows is privat
54ca0 65 20 74 6f 20 70 63 61 63 68 65 2e 63 0a 20 20  e to pcache.c.  
54cb0 2a 2a 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f  ** and should no
54cc0 74 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79  t be accessed by
54cd0 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a   other modules..
54ce0 20 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 66 3b    */.  i16 nRef;
54cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d00 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
54d10 6f 66 20 75 73 65 72 73 20 6f 66 20 74 68 69 73  of users of this
54d20 20 70 61 67 65 20 2a 2f 0a 20 20 50 43 61 63 68   page */.  PCach
54d30 65 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20  e *pCache;      
54d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63            /* Cac
54d50 68 65 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69  he that owns thi
54d60 73 20 70 61 67 65 20 2a 2f 0a 0a 20 20 50 67 48  s page */..  PgH
54d70 64 72 20 2a 70 44 69 72 74 79 4e 65 78 74 3b 20  dr *pDirtyNext; 
54d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
54d90 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c  ext element in l
54da0 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67  ist of dirty pag
54db0 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70  es */.  PgHdr *p
54dc0 44 69 72 74 79 50 72 65 76 3b 20 20 20 20 20 20  DirtyPrev;      
54dd0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f         /* Previo
54de0 75 73 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69  us element in li
54df0 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65  st of dirty page
54e00 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 42 69 74 20  s */.};../* Bit 
54e10 76 61 6c 75 65 73 20 66 6f 72 20 50 67 48 64 72  values for PgHdr
54e20 2e 66 6c 61 67 73 20 2a 2f 0a 23 64 65 66 69 6e  .flags */.#defin
54e30 65 20 50 47 48 44 52 5f 44 49 52 54 59 20 20 20  e PGHDR_DIRTY   
54e40 20 20 20 20 20 20 20 20 20 20 30 78 30 30 32 20            0x002 
54e50 20 2f 2a 20 50 61 67 65 20 68 61 73 20 63 68 61   /* Page has cha
54e60 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nged */.#define 
54e70 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
54e80 20 20 20 20 20 20 20 20 30 78 30 30 34 20 20 2f          0x004  /
54e90 2a 20 46 73 79 6e 63 20 74 68 65 20 72 6f 6c 6c  * Fsync the roll
54ea0 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 62 65 66  back journal bef
54eb0 6f 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  ore.            
54ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54ed0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 72             ** wr
54ee0 69 74 69 6e 67 20 74 68 69 73 20 70 61 67 65 20  iting this page 
54ef0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
54f00 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52  */.#define PGHDR
54f10 5f 4e 45 45 44 5f 52 45 41 44 20 20 20 20 20 20  _NEED_READ      
54f20 20 20 20 30 78 30 30 38 20 20 2f 2a 20 43 6f 6e     0x008  /* Con
54f30 74 65 6e 74 20 69 73 20 75 6e 72 65 61 64 20 2a  tent is unread *
54f40 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f  /.#define PGHDR_
54f50 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 20  REUSE_UNLIKELY  
54f60 20 20 30 78 30 31 30 20 20 2f 2a 20 41 20 68 69    0x010  /* A hi
54f70 6e 74 20 74 68 61 74 20 72 65 75 73 65 20 69 73  nt that reuse is
54f80 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f 0a 23 64 65   unlikely */.#de
54f90 66 69 6e 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f  fine PGHDR_DONT_
54fa0 57 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30  WRITE        0x0
54fb0 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72  20  /* Do not wr
54fc0 69 74 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20 64  ite content to d
54fd0 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 6e 69 74 69  isk */../* Initi
54fe0 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f  alize and shutdo
54ff0 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  wn the page cach
55000 65 20 73 75 62 73 79 73 74 65 6d 20 2a 2f 0a 53  e subsystem */.S
55010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
55020 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49  t sqlite3PcacheI
55030 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b  nitialize(void);
55040 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
55050 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
55060 68 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29  heShutdown(void)
55070 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 61 63 68 65  ;../* Page cache
55080 20 62 75 66 66 65 72 20 6d 61 6e 61 67 65 6d 65   buffer manageme
55090 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  nt:.** These rou
550a0 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20  tines implement 
550b0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
550c0 47 45 43 41 43 48 45 2e 0a 2a 2f 0a 53 51 4c 49  GECACHE..*/.SQLI
550d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
550e0 73 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 66  sqlite3PCacheBuf
550f0 66 65 72 53 65 74 75 70 28 76 6f 69 64 20 2a 2c  ferSetup(void *,
55100 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 3b   int sz, int n);
55110 0a 0a 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65  ../* Create a ne
55120 77 20 70 61 67 65 72 20 63 61 63 68 65 2e 0a 2a  w pager cache..*
55130 2a 20 55 6e 64 65 72 20 6d 65 6d 6f 72 79 20 73  * Under memory s
55140 74 72 65 73 73 2c 20 69 6e 76 6f 6b 65 20 78 53  tress, invoke xS
55150 74 72 65 73 73 20 74 6f 20 74 72 79 20 74 6f 20  tress to try to 
55160 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 61 6e  make pages clean
55170 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c 65 61 6e 20  ..** Only clean 
55180 61 6e 64 20 75 6e 70 69 6e 6e 65 64 20 70 61 67  and unpinned pag
55190 65 73 20 63 61 6e 20 62 65 20 72 65 63 6c 61 69  es can be reclai
551a0 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  med..*/.SQLITE_P
551b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
551c0 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20  te3PcacheOpen(. 
551d0 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20   int szPage,    
551e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
551f0 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79  /* Size of every
55200 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73   page */.  int s
55210 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20  zExtra,         
55220 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
55230 72 61 20 73 70 61 63 65 20 61 73 73 6f 63 69 61  ra space associa
55240 74 65 64 20 77 69 74 68 20 65 61 63 68 20 70 61  ted with each pa
55250 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72  ge */.  int bPur
55260 67 65 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  geable,         
55270 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
55280 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62  f pages are on b
55290 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a  acking store */.
552a0 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29    int (*xStress)
552b0 28 76 6f 69 64 2a 2c 20 50 67 48 64 72 2a 29 2c  (void*, PgHdr*),
552c0 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20   /* Call to try 
552d0 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c  to make pages cl
552e0 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ean */.  void *p
552f0 53 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20  Stress,         
55300 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
55310 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a  ent to xStress *
55320 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 54 6f 49  /.  PCache *pToI
55330 6e 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20  nit             
55340 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74     /* Preallocat
55350 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  ed space for the
55360 20 50 43 61 63 68 65 20 2a 2f 0a 29 3b 0a 0a 2f   PCache */.);../
55370 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 70 61 67  * Modify the pag
55380 65 2d 73 69 7a 65 20 61 66 74 65 72 20 74 68 65  e-size after the
55390 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e 20   cache has been 
553a0 63 72 65 61 74 65 64 2e 20 2a 2f 0a 53 51 4c 49  created. */.SQLI
553b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
553c0 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74  sqlite3PcacheSet
553d0 50 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 20  PageSize(PCache 
553e0 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 52 65 74  *, int);../* Ret
553f0 75 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20  urn the size in 
55400 62 79 74 65 73 20 6f 66 20 61 20 50 43 61 63 68  bytes of a PCach
55410 65 20 6f 62 6a 65 63 74 2e 20 20 55 73 65 64 20  e object.  Used 
55420 74 6f 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a  to preallocate.*
55430 2a 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e  * storage space.
55440 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
55450 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
55460 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 3b 0a  acheSize(void);.
55470 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 61 73 65 20  ./* One release 
55480 70 65 72 20 73 75 63 63 65 73 73 66 75 6c 20 66  per successful f
55490 65 74 63 68 2e 20 20 50 61 67 65 20 69 73 20 70  etch.  Page is p
554a0 69 6e 6e 65 64 20 75 6e 74 69 6c 20 72 65 6c 65  inned until rele
554b0 61 73 65 64 2e 0a 2a 2a 20 52 65 66 65 72 65 6e  ased..** Referen
554c0 63 65 20 63 6f 75 6e 74 65 64 2e 20 0a 2a 2f 0a  ce counted. .*/.
554d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
554e0 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
554f0 46 65 74 63 68 28 50 43 61 63 68 65 2a 2c 20 50  Fetch(PCache*, P
55500 67 6e 6f 2c 20 69 6e 74 20 63 72 65 61 74 65 46  gno, int createF
55510 6c 61 67 2c 20 50 67 48 64 72 2a 2a 29 3b 0a 53  lag, PgHdr**);.S
55520 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
55530 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
55540 52 65 6c 65 61 73 65 28 50 67 48 64 72 2a 29 3b  Release(PgHdr*);
55550 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
55560 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55570 63 68 65 44 72 6f 70 28 50 67 48 64 72 2a 29 3b  cheDrop(PgHdr*);
55580 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f           /* Remo
55590 76 65 20 70 61 67 65 20 66 72 6f 6d 20 63 61 63  ve page from cac
555a0 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  he */.SQLITE_PRI
555b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
555c0 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
555d0 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20  (PgHdr*);    /* 
555e0 4d 61 6b 65 20 73 75 72 65 20 70 61 67 65 20 69  Make sure page i
555f0 73 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 2a  s marked dirty *
55600 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55610 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55620 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48  cheMakeClean(PgH
55630 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b  dr*);    /* Mark
55640 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 61   a single page a
55650 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 51 4c 49 54  s clean */.SQLIT
55660 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
55670 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61  qlite3PcacheClea
55680 6e 41 6c 6c 28 50 43 61 63 68 65 2a 29 3b 20 20  nAll(PCache*);  
55690 20 20 2f 2a 20 4d 61 72 6b 20 61 6c 6c 20 64 69    /* Mark all di
556a0 72 74 79 20 6c 69 73 74 20 70 61 67 65 73 20 61  rty list pages a
556b0 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a 2f 2a 20 43  s clean */../* C
556c0 68 61 6e 67 65 20 61 20 70 61 67 65 20 6e 75 6d  hange a page num
556d0 62 65 72 2e 20 20 55 73 65 64 20 62 79 20 69 6e  ber.  Used by in
556e0 63 72 2d 76 61 63 75 75 6d 2e 20 2a 2f 0a 53 51  cr-vacuum. */.SQ
556f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
55700 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d  d sqlite3PcacheM
55710 6f 76 65 28 50 67 48 64 72 2a 2c 20 50 67 6e 6f  ove(PgHdr*, Pgno
55720 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c  );../* Remove al
55730 6c 20 70 61 67 65 73 20 77 69 74 68 20 70 67 6e  l pages with pgn
55740 6f 3e 78 2e 20 20 52 65 73 65 74 20 74 68 65 20  o>x.  Reset the 
55750 63 61 63 68 65 20 69 66 20 78 3d 3d 30 20 2a 2f  cache if x==0 */
55760 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
55770 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
55780 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68  heTruncate(PCach
55790 65 2a 2c 20 50 67 6e 6f 20 78 29 3b 0a 0a 2f 2a  e*, Pgno x);../*
557a0 20 47 65 74 20 61 20 6c 69 73 74 20 6f 66 20 61   Get a list of a
557b0 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 69  ll dirty pages i
557c0 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72  n the cache, sor
557d0 74 65 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62  ted by page numb
557e0 65 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  er */.SQLITE_PRI
557f0 56 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69  VATE PgHdr *sqli
55800 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69  te3PcacheDirtyLi
55810 73 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a  st(PCache*);../*
55820 20 52 65 73 65 74 20 61 6e 64 20 63 6c 6f 73 65   Reset and close
55830 20 74 68 65 20 63 61 63 68 65 20 6f 62 6a 65 63   the cache objec
55840 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  t */.SQLITE_PRIV
55850 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55860 50 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63  PcacheClose(PCac
55870 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c 65 61 72 20  he*);../* Clear 
55880 66 6c 61 67 73 20 66 72 6f 6d 20 70 61 67 65 73  flags from pages
55890 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63   of the page cac
558a0 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  he */.SQLITE_PRI
558b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
558c0 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63  3PcacheClearSync
558d0 46 6c 61 67 73 28 50 43 61 63 68 65 20 2a 29 3b  Flags(PCache *);
558e0 0a 0a 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65  ../* Discard the
558f0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
55900 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45   cache */.SQLITE
55910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
55920 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72  lite3PcacheClear
55930 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52  (PCache*);../* R
55940 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20  eturn the total 
55950 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61  number of outsta
55960 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72  nding page refer
55970 65 6e 63 65 73 20 2a 2f 0a 53 51 4c 49 54 45 5f  ences */.SQLITE_
55980 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
55990 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e  te3PcacheRefCoun
559a0 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20  t(PCache*);../* 
559b0 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65  Increment the re
559c0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66  ference count of
559d0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 70 61 67   an existing pag
559e0 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e */.SQLITE_PRIV
559f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55a00 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 2a  PcacheRef(PgHdr*
55a10 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
55a20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
55a30 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74  achePageRefcount
55a40 28 50 67 48 64 72 2a 29 3b 0a 0a 2f 2a 20 52 65  (PgHdr*);../* Re
55a50 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e  turn the total n
55a60 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73  umber of pages s
55a70 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63  tored in the cac
55a80 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  he */.SQLITE_PRI
55a90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
55aa0 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28  PcachePagecount(
55ab0 50 43 61 63 68 65 2a 29 3b 0a 0a 23 69 66 20 64  PCache*);..#if d
55ac0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 48  efined(SQLITE_CH
55ad0 45 43 4b 5f 50 41 47 45 53 29 20 7c 7c 20 64 65  ECK_PAGES) || de
55ae0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
55af0 55 47 29 0a 2f 2a 20 49 74 65 72 61 74 65 20 74  UG)./* Iterate t
55b00 68 72 6f 75 67 68 20 61 6c 6c 20 64 69 72 74 79  hrough all dirty
55b10 20 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79   pages currently
55b20 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63   stored in the c
55b30 61 63 68 65 2e 20 54 68 69 73 0a 2a 2a 20 69 6e  ache. This.** in
55b40 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20  terface is only 
55b50 61 76 61 69 6c 61 62 6c 65 20 69 66 20 53 51 4c  available if SQL
55b60 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20  ITE_CHECK_PAGES 
55b70 69 73 20 64 65 66 69 6e 65 64 20 77 68 65 6e 20  is defined when 
55b80 74 68 65 20 0a 2a 2a 20 6c 69 62 72 61 72 79 20  the .** library 
55b90 69 73 20 62 75 69 6c 74 2e 0a 2a 2f 0a 53 51 4c  is built..*/.SQL
55ba0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
55bb0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74   sqlite3PcacheIt
55bc0 65 72 61 74 65 44 69 72 74 79 28 50 43 61 63 68  erateDirty(PCach
55bd0 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20  e *pCache, void 
55be0 28 2a 78 49 74 65 72 29 28 50 67 48 64 72 20 2a  (*xIter)(PgHdr *
55bf0 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 53  ));.#endif../* S
55c00 65 74 20 61 6e 64 20 67 65 74 20 74 68 65 20 73  et and get the s
55c10 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73  uggested cache-s
55c20 69 7a 65 20 66 6f 72 20 74 68 65 20 73 70 65 63  ize for the spec
55c30 69 66 69 65 64 20 70 61 67 65 72 2d 63 61 63 68  ified pager-cach
55c40 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 67  e..**.** If no g
55c50 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 69 73  lobal maximum is
55c60 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 74 68 65   configured, the
55c70 6e 20 74 68 65 20 73 79 73 74 65 6d 20 61 74 74  n the system att
55c80 65 6d 70 74 73 20 74 6f 20 6c 69 6d 69 74 0a 2a  empts to limit.*
55c90 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  * the total numb
55ca0 65 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68  er of pages cach
55cb0 65 64 20 62 79 20 70 75 72 67 65 61 62 6c 65 20  ed by purgeable 
55cc0 70 61 67 65 72 2d 63 61 63 68 65 73 20 74 6f 20  pager-caches to 
55cd0 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68  the sum.** of th
55ce0 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68  e suggested cach
55cf0 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a 53 51 4c 49  e-sizes..*/.SQLI
55d00 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
55d10 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74  sqlite3PcacheSet
55d20 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65  Cachesize(PCache
55d30 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 66   *, int);.#ifdef
55d40 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
55d50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
55d60 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74  sqlite3PcacheGet
55d70 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65  Cachesize(PCache
55d80 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66   *);.#endif..#if
55d90 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
55da0 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
55db0 45 4e 54 0a 2f 2a 20 54 72 79 20 74 6f 20 72 65  ENT./* Try to re
55dc0 74 75 72 6e 20 6d 65 6d 6f 72 79 20 75 73 65 64  turn memory used
55dd0 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20 6d   by the pcache m
55de0 6f 64 75 6c 65 20 74 6f 20 74 68 65 20 6d 61 69  odule to the mai
55df0 6e 20 6d 65 6d 6f 72 79 20 68 65 61 70 20 2a 2f  n memory heap */
55e00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
55e10 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
55e20 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69  eReleaseMemory(i
55e30 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  nt);.#endif..#if
55e40 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
55e50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
55e60 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
55e70 65 53 74 61 74 73 28 69 6e 74 2a 2c 69 6e 74 2a  eStats(int*,int*
55e80 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 23 65 6e  ,int*,int*);.#en
55e90 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  dif..SQLITE_PRIV
55ea0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55eb0 50 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74  PCacheSetDefault
55ec0 28 76 6f 69 64 29 3b 0a 0a 23 65 6e 64 69 66 20  (void);..#endif 
55ed0 2f 2a 20 5f 50 43 41 43 48 45 5f 48 5f 20 2a 2f  /* _PCACHE_H_ */
55ee0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
55ef0 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 2e  * End of pcache.
55f00 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
55f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55f30 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
55f40 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
55f50 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
55f60 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
55f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55f80 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
55f90 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20  ** Include os.h 
55fa0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
55fb0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
55fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55fd0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
55fe0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73  ** Begin file os
55ff0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
56000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56020 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
56030 70 74 65 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a  ptember 16.**.**
56040 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
56050 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
56060 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
56070 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
56080 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
56090 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
560a0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
560b0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
560c0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
560d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
560e0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
560f0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
56100 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
56110 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
56120 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
56130 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
56140 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
56150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
561a0 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
561b0 20 66 69 6c 65 20 28 74 6f 67 65 74 68 65 72 20   file (together 
561c0 77 69 74 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f  with is companio
561d0 6e 20 43 20 73 6f 75 72 63 65 2d 63 6f 64 65 20  n C source-code 
561e0 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20  file.** "os.c") 
561f0 61 74 74 65 6d 70 74 20 74 6f 20 61 62 73 74 72  attempt to abstr
56200 61 63 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69  act the underlyi
56210 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ng operating sys
56220 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74  tem so that.** t
56230 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
56240 79 20 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62  y will work on b
56250 6f 74 68 20 50 4f 53 49 58 20 61 6e 64 20 77 69  oth POSIX and wi
56260 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a  ndows systems..*
56270 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
56280 20 66 69 6c 65 20 69 73 20 23 69 6e 63 6c 75 64   file is #includ
56290 65 2d 65 64 20 62 79 20 73 71 6c 69 74 65 49 6e  e-ed by sqliteIn
562a0 74 2e 68 20 61 6e 64 20 74 68 75 73 20 65 6e 64  t.h and thus end
562b0 73 20 75 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e  s up.** being in
562c0 63 6c 75 64 65 64 20 62 79 20 65 76 65 72 79 20  cluded by every 
562d0 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2f 0a  source file..*/.
562e0 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f  #ifndef _SQLITE_
562f0 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53  OS_H_.#define _S
56300 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a  QLITE_OS_H_../*.
56310 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66  ** Figure out if
56320 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
56330 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e 64 6f  with Unix, Windo
56340 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65  ws, or some othe
56350 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73  r.** operating s
56360 79 73 74 65 6d 2e 20 20 41 66 74 65 72 20 74 68  ystem.  After th
56370 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
56380 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 73 73 20  k of preprocess 
56390 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f  macros,.** all o
563a0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  f SQLITE_OS_UNIX
563b0 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c  , SQLITE_OS_WIN,
563c0 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c 20   SQLITE_OS_OS2, 
563d0 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54  and SQLITE_OS_OT
563e0 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 64 65 66  HER .** will def
563f0 69 6e 65 64 20 74 6f 20 65 69 74 68 65 72 20 31  ined to either 1
56400 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66 20 74   or 0.  One of t
56410 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62 65 20  he four will be 
56420 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20 0a 2a  1.  The other .*
56430 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 62 65 20  * three will be 
56440 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  0..*/.#if define
56450 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45  d(SQLITE_OS_OTHE
56460 52 29 0a 23 20 69 66 20 53 51 4c 49 54 45 5f 4f  R).# if SQLITE_O
56470 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20 20 75  S_OTHER==1.#   u
56480 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55  ndef SQLITE_OS_U
56490 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65 20 53  NIX.#   define S
564a0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a  QLITE_OS_UNIX 0.
564b0 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45  #   undef SQLITE
564c0 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 65 66 69  _OS_WIN.#   defi
564d0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  ne SQLITE_OS_WIN
564e0 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c   0.#   undef SQL
564f0 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 64  ITE_OS_OS2.#   d
56500 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
56510 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23 20 20  OS2 0.# else.#  
56520 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53   undef SQLITE_OS
56530 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 66 0a 23  _OTHER.# endif.#
56540 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 69 6e  endif.#if !defin
56550 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  ed(SQLITE_OS_UNI
56560 58 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  X) && !defined(S
56570 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a  QLITE_OS_OTHER).
56580 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
56590 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69 66 6e  OS_OTHER 0.# ifn
565a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  def SQLITE_OS_WI
565b0 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e 65 64  N.#   if defined
565c0 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69  (_WIN32) || defi
565d0 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20 64 65  ned(WIN32) || de
565e0 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f  fined(__CYGWIN__
565f0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4d  ) || defined(__M
56600 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64 65 66  INGW32__) || def
56610 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f  ined(__BORLANDC_
56620 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20  _).#     define 
56630 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 31 0a  SQLITE_OS_WIN 1.
56640 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  #     define SQL
56650 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20  ITE_OS_UNIX 0.# 
56660 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
56670 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 20 65  E_OS_OS2 0.#   e
56680 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 45 4d  lif defined(__EM
56690 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  X__) || defined(
566a0 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 64  _OS2) || defined
566b0 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 64  (OS2) || defined
566c0 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66 69 6e  (_OS2_) || defin
566d0 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 20  ed(__OS2__).#   
566e0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
566f0 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 64  OS_WIN 0.#     d
56700 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
56710 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65 66  UNIX 0.#     def
56720 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53  ine SQLITE_OS_OS
56730 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23 20 20  2 1.#   else.#  
56740 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
56750 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20  _OS_WIN 0.#     
56760 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
56770 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 20 64 65  _UNIX 1.#     de
56780 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  fine SQLITE_OS_O
56790 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 20  S2 0.#  endif.# 
567a0 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 53  else.#  define S
567b0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a  QLITE_OS_UNIX 0.
567c0 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  #  define SQLITE
567d0 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e 64 69  _OS_OS2 0.# endi
567e0 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64 65 66  f.#else.# ifndef
567f0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23   SQLITE_OS_WIN.#
56800 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
56810 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64 69 66  OS_WIN 0.# endif
56820 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44  .#endif../*.** D
56830 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61  etermine if we a
56840 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
56850 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63  WindowsCE - whic
56860 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20  h has a much.** 
56870 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a  reduced API..*/.
56880 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e  #if defined(_WIN
56890 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e 65  32_WCE).# define
568a0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
568b0 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   1.#else.# defin
568c0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  e SQLITE_OS_WINC
568d0 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  E 0.#endif.../*.
568e0 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 6d 61  ** Define the ma
568f0 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 20  ximum size of a 
56900 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 6e 61  temporary filena
56910 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  me.*/.#if SQLITE
56920 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64  _OS_WIN.# includ
56930 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 20  e <windows.h>.# 
56940 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
56950 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 4d 41 58  MPNAME_SIZE (MAX
56960 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c 69 66 20  _PATH+50).#elif 
56970 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20  SQLITE_OS_OS2.# 
56980 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33  if (__GNUC__ > 3
56990 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20   || __GNUC__ == 
569a0 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f  3 && __GNUC_MINO
569b0 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64 65 66  R__ >= 3) && def
569c0 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f 4d 45  ined(OS2_HIGH_ME
569d0 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75 64 65  MORY).#  include
569e0 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f 2a 20   <os2safe.h> /* 
569f0 68 61 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64  has to be includ
56a00 65 64 20 62 65 66 6f 72 65 20 6f 73 32 2e 68 20  ed before os2.h 
56a10 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 77  for linking to w
56a20 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23  ork */.# endif.#
56a30 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53   define INCL_DOS
56a40 44 41 54 45 54 49 4d 45 0a 23 20 64 65 66 69 6e  DATETIME.# defin
56a50 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d 47  e INCL_DOSFILEMG
56a60 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f  R.# define INCL_
56a70 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 65 66 69  DOSERRORS.# defi
56a80 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a  ne INCL_DOSMISC.
56a90 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f  # define INCL_DO
56aa0 53 50 52 4f 43 45 53 53 0a 23 20 64 65 66 69 6e  SPROCESS.# defin
56ab0 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45  e INCL_DOSMODULE
56ac0 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43  MGR.# define INC
56ad0 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 45 53 0a  L_DOSSEMAPHORES.
56ae0 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 2e 68  # include <os2.h
56af0 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 63 6f  >.# include <uco
56b00 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 53  nv.h>.# define S
56b10 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53  QLITE_TEMPNAME_S
56b20 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54 48 43  IZE (CCHMAXPATHC
56b30 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  OMP).#else.# def
56b40 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e  ine SQLITE_TEMPN
56b50 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23 65 6e  AME_SIZE 200.#en
56b60 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53  dif../* If the S
56b70 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 63 72  ET_FULLSYNC macr
56b80 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  o is not defined
56b90 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d 61 6b   above, then mak
56ba0 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a  e it.** a no-op.
56bb0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 54 5f 46  */.#ifndef SET_F
56bc0 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69 6e 65  ULLSYNC.# define
56bd0 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28 78 2c   SET_FULLSYNC(x,
56be0 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  y).#endif../*.**
56bf0 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69 7a   The default siz
56c00 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63 74  e of a disk sect
56c10 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  or.*/.#ifndef SQ
56c20 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
56c30 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e  TOR_SIZE.# defin
56c40 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
56c50 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31 32  _SECTOR_SIZE 512
56c60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
56c70 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 61  emporary files a
56c80 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74 69 6e  re named startin
56c90 67 20 77 69 74 68 20 74 68 69 73 20 70 72 65 66  g with this pref
56ca0 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 31  ix followed by 1
56cb0 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68  6 random.** alph
56cc0 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63 74  anumeric charact
56cd0 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 6c 65  ers, and no file
56ce0 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 65 79   extension. They
56cf0 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74   are stored in t
56d00 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61 6e 64  he.** OS's stand
56d10 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69  ard temporary fi
56d20 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20 61 6e  le directory, an
56d30 64 20 61 72 65 20 64 65 6c 65 74 65 64 20 70 72  d are deleted pr
56d40 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a 2a 20  ior to exit..** 
56d50 49 66 20 73 71 6c 69 74 65 20 69 73 20 62 65 69  If sqlite is bei
56d60 6e 67 20 65 6d 62 65 64 64 65 64 20 69 6e 20 61  ng embedded in a
56d70 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 6d 2c 20  nother program, 
56d80 79 6f 75 20 6d 61 79 20 77 69 73 68 20 74 6f 20  you may wish to 
56d90 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 70 72  change the.** pr
56da0 65 66 69 78 20 74 6f 20 72 65 66 6c 65 63 74 20  efix to reflect 
56db0 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 73 20 6e  your program's n
56dc0 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 69 66 20  ame, so that if 
56dd0 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 65 78 69  your program exi
56de0 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72 65 6c  ts.** prematurel
56df0 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79  y, old temporary
56e00 20 66 69 6c 65 73 20 63 61 6e 20 62 65 20 65 61   files can be ea
56e10 73 69 6c 79 20 69 64 65 6e 74 69 66 69 65 64 2e  sily identified.
56e20 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 6f 6e   This can be don
56e30 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 53 51 4c  e.** using -DSQL
56e40 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52  ITE_TEMP_FILE_PR
56e50 45 46 49 58 3d 6d 79 70 72 65 66 69 78 5f 20 6f  EFIX=myprefix_ o
56e60 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63  n the compiler c
56e70 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a  ommand line..**.
56e80 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 3a 20 20  ** 2006-10-31:  
56e90 54 68 65 20 64 65 66 61 75 6c 74 20 70 72 65 66  The default pref
56ea0 69 78 20 75 73 65 64 20 74 6f 20 62 65 20 22 73  ix used to be "s
56eb0 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 20 74 68  qlite_".  But th
56ec0 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 73 74 61  en.** Mcafee sta
56ed0 72 74 65 64 20 75 73 69 6e 67 20 53 51 4c 69 74  rted using SQLit
56ee0 65 20 69 6e 20 74 68 65 69 72 20 61 6e 74 69 2d  e in their anti-
56ef0 76 69 72 75 73 20 70 72 6f 64 75 63 74 20 61 6e  virus product an
56f00 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 65 64 20  d it.** started 
56f10 70 75 74 74 69 6e 67 20 66 69 6c 65 73 20 77 69  putting files wi
56f20 74 68 20 74 68 65 20 22 73 71 6c 69 74 65 22 20  th the "sqlite" 
56f30 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 3a 2f 74  name in the c:/t
56f40 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54  emp folder..** T
56f50 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d 61 6e 79  his annoyed many
56f60 20 77 69 6e 64 6f 77 73 20 75 73 65 72 73 2e 20   windows users. 
56f70 20 54 68 6f 73 65 20 75 73 65 72 73 20 77 6f 75   Those users wou
56f80 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 0a 2a 2a  ld then do a .**
56f90 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 68 20 66   Google search f
56fa0 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 66 69 6e  or "sqlite", fin
56fb0 64 20 74 68 65 20 74 65 6c 65 70 68 6f 6e 65 20  d the telephone 
56fc0 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 65 0a 2a  numbers of the.*
56fd0 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 61 6e 64  * developers and
56fe0 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 20 74 68   call to wake th
56ff0 65 6d 20 75 70 20 61 74 20 6e 69 67 68 74 20 61  em up at night a
57000 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20  nd complain..** 
57010 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c  For this reason,
57020 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61 6d   the default nam
57030 65 20 70 72 65 66 69 78 20 69 73 20 63 68 61 6e  e prefix is chan
57040 67 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74  ged to be "sqlit
57050 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 62  e" .** spelled b
57060 61 63 6b 77 61 72 64 73 2e 20 20 53 6f 20 74 68  ackwards.  So th
57070 65 20 74 65 6d 70 20 66 69 6c 65 73 20 61 72 65  e temp files are
57080 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 66 69 65   still identifie
57090 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 62 6f 64  d, but.** anybod
570a0 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68 20 74  y smart enough t
570b0 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 65  o figure out the
570c0 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20 6c 69   code is also li
570d0 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a 20 65 6e  kely smart.** en
570e0 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74 68 61  ough to know tha
570f0 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65  t calling the de
57100 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e 6f 74  veloper will not
57110 20 68 65 6c 70 20 67 65 74 20 72 69 64 0a 2a 2a   help get rid.**
57120 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f   of the file..*/
57130 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
57140 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58  TEMP_FILE_PREFIX
57150 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
57160 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49  _TEMP_FILE_PREFI
57170 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 65 6e 64  X "etilqs_".#end
57180 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
57190 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 20 6d  llowing values m
571a0 61 79 20 62 65 20 70 61 73 73 65 64 20 61 73 20  ay be passed as 
571b0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
571c0 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  ent to.** sqlite
571d0 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 65 20 76  3OsLock(). The v
571e0 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 65 78 68  arious locks exh
571f0 69 62 69 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ibit the followi
57200 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a  ng semantics:.**
57210 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 20 20 41  .** SHARED:    A
57220 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 6f  ny number of pro
57230 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20  cesses may hold 
57240 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 73 69  a SHARED lock si
57250 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a  multaneously..**
57260 20 52 45 53 45 52 56 45 44 3a 20 20 41 20 73 69   RESERVED:  A si
57270 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79  ngle process may
57280 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44   hold a RESERVED
57290 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20   lock on a file 
572a0 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  at.**           
572b0 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 68 65 72   any time. Other
572c0 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 68   processes may h
572d0 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 6e 20 6e  old and obtain n
572e0 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 2e  ew SHARED locks.
572f0 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20 20 41  .** PENDING:   A
57300 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20   single process 
57310 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 4e 44 49  may hold a PENDI
57320 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c  NG lock on a fil
57330 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  e at.**         
57340 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d 65 2e     any one time.
57350 20 45 78 69 73 74 69 6e 67 20 53 48 41 52 45 44   Existing SHARED
57360 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72 73 69   locks may persi
57370 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77 0a 2a  st, but no new.*
57380 2a 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41  *            SHA
57390 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 62 65  RED locks may be
573a0 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f 74 68   obtained by oth
573b0 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a  er processes..**
573c0 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e 20 45   EXCLUSIVE: An E
573d0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 70 72  XCLUSIVE lock pr
573e0 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65  ecludes all othe
573f0 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50  r locks..**.** P
57400 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20  ENDING_LOCK may 
57410 6e 6f 74 20 62 65 20 70 61 73 73 65 64 20 64 69  not be passed di
57420 72 65 63 74 6c 79 20 74 6f 20 73 71 6c 69 74 65  rectly to sqlite
57430 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 73 74 65  3OsLock(). Inste
57440 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 65 73 73  ad, a.** process
57450 20 74 68 61 74 20 72 65 71 75 65 73 74 73 20 61   that requests a
57460 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
57470 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 6f 62   may actually ob
57480 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 0a 2a  tain a PENDING.*
57490 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 63 61 6e  * lock. This can
574a0 20 62 65 20 75 70 67 72 61 64 65 64 20 74 6f 20   be upgraded to 
574b0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
574c0 6b 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e  k by a subsequen
574d0 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c  t call to.** sql
574e0 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f  ite3OsLock()..*/
574f0 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b  .#define NO_LOCK
57500 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69           0.#defi
57510 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20  ne SHARED_LOCK  
57520 20 20 20 31 0a 23 64 65 66 69 6e 65 20 52 45 53     1.#define RES
57530 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 32 0a 23  ERVED_LOCK   2.#
57540 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 4c  define PENDING_L
57550 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 69 6e 65  OCK    3.#define
57560 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20   EXCLUSIVE_LOCK 
57570 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c   4../*.** File L
57580 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20 20 28  ocking Notes:  (
57590 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77 69 6e  Mostly about win
575a0 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20 73 6f  dows but also so
575b0 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e 69 78  me info for Unix
575c0 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f  ).**.** We canno
575d0 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65 45 78  t use LockFileEx
575e0 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c 65  () or UnlockFile
575f0 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f 39 38  Ex() on Win95/98
57600 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a 2a 20 74  /ME because.** t
57610 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  hose functions a
57620 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  re not available
57630 2e 20 20 53 6f 20 77 65 20 75 73 65 20 6f 6e 6c  .  So we use onl
57640 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 61 6e 64  y LockFile() and
57650 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 29  .** UnlockFile()
57660 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65  ..**.** LockFile
57670 28 29 20 70 72 65 76 65 6e 74 73 20 6e 6f 74 20  () prevents not 
57680 6a 75 73 74 20 77 72 69 74 69 6e 67 20 62 75 74  just writing but
57690 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 20 62 79   also reading by
576a0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73   other processes
576b0 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 5f 4c 4f  ..** A SHARED_LO
576c0 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62  CK is obtained b
576d0 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 69 6e 67  y locking a sing
576e0 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73  le randomly-chos
576f0 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f 75 74 20  en .** byte out 
57700 6f 66 20 61 20 73 70 65 63 69 66 69 63 20 72 61  of a specific ra
57710 6e 67 65 20 6f 66 20 62 79 74 65 73 2e 20 54 68  nge of bytes. Th
57720 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 73 20 6f  e lock byte is o
57730 62 74 61 69 6e 65 64 20 61 74 20 0a 2a 2a 20 72  btained at .** r
57740 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 73 65 70  andom so two sep
57750 61 72 61 74 65 20 72 65 61 64 65 72 73 20 63 61  arate readers ca
57760 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 63 65 73  n probably acces
57770 73 20 74 68 65 20 66 69 6c 65 20 61 74 20 74 68  s the file at th
57780 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65 2c  e .** same time,
57790 20 75 6e 6c 65 73 73 20 74 68 65 79 20 61 72 65   unless they are
577a0 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 63 68 6f   unlucky and cho
577b0 6f 73 65 20 74 68 65 20 73 61 6d 65 20 6c 6f 63  ose the same loc
577c0 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e 20 45 58  k byte..** An EX
577d0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 69 73 20  CLUSIVE_LOCK is 
577e0 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b  obtained by lock
577f0 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 20 69 6e  ing all bytes in
57800 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2a 20 54   the range..** T
57810 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65  here can only be
57820 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 20 41 20   one writer.  A 
57830 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 69 73  RESERVED_LOCK is
57840 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63   obtained by loc
57850 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e 67 6c 65  king.** a single
57860 20 62 79 74 65 20 6f 66 20 74 68 65 20 66 69 6c   byte of the fil
57870 65 20 74 68 61 74 20 69 73 20 64 65 73 69 67 6e  e that is design
57880 61 74 65 64 20 61 73 20 74 68 65 20 72 65 73 65  ated as the rese
57890 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 65 2e 0a  rved lock byte..
578a0 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f 4c 4f 43  ** A PENDING_LOC
578b0 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79  K is obtained by
578c0 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 73 69 67   locking a desig
578d0 6e 61 74 65 64 20 62 79 74 65 20 64 69 66 66 65  nated byte diffe
578e0 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  rent from.** the
578f0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 62   RESERVED_LOCK b
57900 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69  yte..**.** On Wi
57910 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74 65 6d  nNT/2K/XP system
57920 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20  s, LockFileEx() 
57930 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78  and UnlockFileEx
57940 28 29 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  () are available
57950 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73  ,.** which means
57960 20 77 65 20 63 61 6e 20 75 73 65 20 72 65 61 64   we can use read
57970 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 2e  er/writer locks.
57980 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f 77 72    When reader/wr
57990 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72  iter locks.** ar
579a0 65 20 75 73 65 64 2c 20 74 68 65 20 6c 6f 63 6b  e used, the lock
579b0 20 69 73 20 70 6c 61 63 65 64 20 6f 6e 20 74 68   is placed on th
579c0 65 20 73 61 6d 65 20 72 61 6e 67 65 20 6f 66 20  e same range of 
579d0 62 79 74 65 73 20 74 68 61 74 20 69 73 20 75 73  bytes that is us
579e0 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f 62 61 62  ed.** for probab
579f0 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 6e 67 20  ilistic locking 
57a00 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20  in Win95/98/ME. 
57a10 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f 63 6b   Hence, the lock
57a20 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a 20 77 69  ing scheme.** wi
57a30 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 6f 20 6f  ll support two o
57a40 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 72 65 61  r more Win95 rea
57a50 64 65 72 73 20 6f 72 20 74 77 6f 20 6f 72 20 6d  ders or two or m
57a60 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 64 65 72  ore WinNT reader
57a70 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 69 6e 67  s..** But a sing
57a80 6c 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 20  le Win95 reader 
57a90 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c  will lock out al
57aa0 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73 20  l WinNT readers 
57ab0 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  and a single.** 
57ac0 57 69 6e 4e 54 20 72 65 61 64 65 72 20 77 69 6c  WinNT reader wil
57ad0 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 6f  l lock out all o
57ae0 74 68 65 72 20 57 69 6e 39 35 20 72 65 61 64 65  ther Win95 reade
57af0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  rs..**.** The fo
57b00 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 6e 65 73  llowing #defines
57b10 20 73 70 65 63 69 66 79 20 74 68 65 20 72 61 6e   specify the ran
57b20 67 65 20 6f 66 20 62 79 74 65 73 20 75 73 65 64  ge of bytes used
57b30 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a   for locking..**
57b40 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73 20   SHARED_SIZE is 
57b50 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
57b60 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  tes available in
57b70 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f 6d 20 77   the pool from w
57b80 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d  hich.** a random
57b90 20 62 79 74 65 20 69 73 20 73 65 6c 65 63 74 65   byte is selecte
57ba0 64 20 66 6f 72 20 61 20 73 68 61 72 65 64 20 6c  d for a shared l
57bb0 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f 6c 20 6f  ock.  The pool o
57bc0 66 20 62 79 74 65 73 20 66 6f 72 0a 2a 2a 20 73  f bytes for.** s
57bd0 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 65 67 69  hared locks begi
57be0 6e 73 20 61 74 20 53 48 41 52 45 44 5f 46 49 52  ns at SHARED_FIR
57bf0 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ST. .**.** The s
57c00 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61  ame locking stra
57c10 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79 74 65  tegy and.** byte
57c20 20 72 61 6e 67 65 73 20 61 72 65 20 75 73 65 64   ranges are used
57c30 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69 73   for Unix.  This
57c40 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68 65   leaves open the
57c50 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20 68   possiblity of h
57c60 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 73  aving.** clients
57c70 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e 54   on win95, winNT
57c80 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20 74  , and unix all t
57c90 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 61  alking to the sa
57ca0 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0a 2a  me shared file.*
57cb0 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e  * and all lockin
57cc0 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 6f  g correctly.  To
57cd0 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65 71   do so would req
57ce0 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61 20  uire that samba 
57cf0 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a 2a 20  (or whatever.** 
57d00 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20 75 73  tool is being us
57d10 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68 61 72  ed for file shar
57d20 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74 73 20  ing) implements 
57d30 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c 79 20  locks correctly 
57d40 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f  between.** windo
57d50 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20 49 27  ws and unix.  I'
57d60 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61 74 20  m guessing that 
57d70 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74 6f 20  isn't likely to 
57d80 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79 0a 2a  happen, but by.*
57d90 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65  * using the same
57da0 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 77   locking range w
57db0 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 6f  e are at least o
57dc0 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73 73 69  pen to the possi
57dd0 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f  bility..**.** Lo
57de0 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f 77 73  cking in windows
57df0 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e 20 20   is manditory.  
57e00 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c  For this reason,
57e10 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 72 65   we cannot store
57e20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74 61 20  .** actual data 
57e30 69 6e 20 74 68 65 20 62 79 74 65 73 20 75 73 65  in the bytes use
57e40 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20  d for locking.  
57e50 54 68 65 20 70 61 67 65 72 20 6e 65 76 65 72 20  The pager never 
57e60 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74 68 65  allocates.** the
57e70 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20   pages involved 
57e80 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 72 65  in locking there
57e90 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f 53 49  fore.  SHARED_SI
57ea0 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64 20 73  ZE is selected s
57eb0 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 6c 6f  o.** that all lo
57ec0 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20  cks will fit on 
57ed0 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 65 76  a single page ev
57ee0 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69 6d 75  en at the minimu
57ef0 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20  m page size..** 
57f00 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64 65 66  PENDING_BYTE def
57f10 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e 6e 69  ines the beginni
57f20 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73 2e  ng of the locks.
57f30 20 20 42 79 20 64 65 66 61 75 6c 74 20 50 45 4e    By default PEN
57f40 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69 73 20  DING_BYTE.** is 
57f50 73 65 74 20 68 69 67 68 20 73 6f 20 74 68 61 74  set high so that
57f60 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20 74   we don't have t
57f70 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 75 6e  o allocate an un
57f80 75 73 65 64 20 70 61 67 65 20 65 78 63 65 70 74  used page except
57f90 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c 61 72  .** for very lar
57fa0 67 65 20 64 61 74 61 62 61 73 65 73 2e 20 20 42  ge databases.  B
57fb0 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20 74 65  ut one should te
57fc0 73 74 20 74 68 65 20 70 61 67 65 20 73 6b 69 70  st the page skip
57fd0 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 62  ping logic .** b
57fe0 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44 49 4e  y setting PENDIN
57ff0 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64 20 72  G_BYTE low and r
58000 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69 72  unning the entir
58010 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73 75 69  e regression sui
58020 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69  te..**.** Changi
58030 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ng the value of 
58040 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65 73  PENDING_BYTE res
58050 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c 79  ults in a subtly
58060 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a   incompatible.**
58070 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20 44   file format.  D
58080 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20  epending on how 
58090 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20 79  it is changed, y
580a0 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f 74  ou might not not
580b0 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63 6f 6d  ice.** the incom
580c0 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68 74  patibility right
580d0 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e 6e   away, even runn
580e0 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72 65  ing a full regre
580f0 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 54  ssion test..** T
58100 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 61 74  he default locat
58110 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42  ion of PENDING_B
58120 59 54 45 20 69 73 20 74 68 65 20 66 69 72 73 74  YTE is the first
58130 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a 2a   byte past the.*
58140 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2e 0a  * 1GB boundary..
58150 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 45  **.*/.#define PE
58160 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20  NDING_BYTE      
58170 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79  sqlite3PendingBy
58180 74 65 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52  te.#define RESER
58190 56 45 44 5f 42 59 54 45 20 20 20 20 20 28 50 45  VED_BYTE     (PE
581a0 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 0a 23 64  NDING_BYTE+1).#d
581b0 65 66 69 6e 65 20 53 48 41 52 45 44 5f 46 49 52  efine SHARED_FIR
581c0 53 54 20 20 20 20 20 20 28 50 45 4e 44 49 4e 47  ST      (PENDING
581d0 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 69 6e 65  _BYTE+2).#define
581e0 20 53 48 41 52 45 44 5f 53 49 5a 45 20 20 20 20   SHARED_SIZE    
581f0 20 20 20 35 31 30 0a 0a 2f 2a 0a 2a 2a 20 57 72     510../*.** Wr
58200 61 70 70 65 72 20 61 72 6f 75 6e 64 20 4f 53 20  apper around OS 
58210 73 70 65 63 69 66 69 63 20 73 71 6c 69 74 65 33  specific sqlite3
58220 5f 6f 73 5f 69 6e 69 74 28 29 20 66 75 6e 63 74  _os_init() funct
58230 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
58240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58250 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29 3b 0a  e3OsInit(void);.
58260 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ./* .** Function
58270 73 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  s for accessing 
58280 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74  sqlite3_file met
58290 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hods .*/.SQLITE_
582a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
582b0 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74  te3OsClose(sqlit
582c0 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 51 4c 49 54  e3_file*);.SQLIT
582d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
582e0 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69  lite3OsRead(sqli
582f0 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a  te3_file*, void*
58300 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f  , int amt, i64 o
58310 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50  ffset);.SQLITE_P
58320 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58330 65 33 4f 73 57 72 69 74 65 28 73 71 6c 69 74 65  e3OsWrite(sqlite
58340 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76  3_file*, const v
58350 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69  oid*, int amt, i
58360 36 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49  64 offset);.SQLI
58370 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58380 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
58390 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
583a0 69 36 34 20 73 69 7a 65 29 3b 0a 53 51 4c 49 54  i64 size);.SQLIT
583b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
583c0 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 6c 69  lite3OsSync(sqli
583d0 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b  te3_file*, int);
583e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
583f0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  int sqlite3OsFil
58400 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  eSize(sqlite3_fi
58410 6c 65 2a 2c 20 69 36 34 20 2a 70 53 69 7a 65 29  le*, i64 *pSize)
58420 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
58430 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f   int sqlite3OsLo
58440 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ck(sqlite3_file*
58450 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
58460 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58470 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  e3OsUnlock(sqlit
58480 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
58490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
584a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63  nt sqlite3OsChec
584b0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
584c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
584d0 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53  int *pResOut);.S
584e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
584f0 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  t sqlite3OsFileC
58500 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66  ontrol(sqlite3_f
58510 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b  ile*,int,void*);
58520 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
58530 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47  FCNTL_DB_UNCHANG
58540 45 44 20 30 78 63 61 30 39 33 66 61 30 0a 53 51  ED 0xca093fa0.SQ
58550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
58560 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72   sqlite3OsSector
58570 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
58580 65 20 2a 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50  e *id);.SQLITE_P
58590 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
585a0 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  e3OsDeviceCharac
585b0 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65  teristics(sqlite
585c0 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a  3_file *id);../*
585d0 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66   .** Functions f
585e0 6f 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c  or accessing sql
585f0 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73  ite3_vfs methods
58600 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
58610 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
58620 73 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  sOpen(sqlite3_vf
58630 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  s *, const char 
58640 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  *, sqlite3_file*
58650 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53  , int, int *);.S
58660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
58670 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74  t sqlite3OsDelet
58680 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  e(sqlite3_vfs *,
58690 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
586a0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
586b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
586c0 73 41 63 63 65 73 73 28 73 71 6c 69 74 65 33 5f  sAccess(sqlite3_
586d0 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  vfs *, const cha
586e0 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 70  r *, int, int *p
586f0 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f  ResOut);.SQLITE_
58700 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
58710 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d  te3OsFullPathnam
58720 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  e(sqlite3_vfs *,
58730 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
58740 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 23 69 66  nt, char *);.#if
58750 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
58760 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
58770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
58780 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c  oid *sqlite3OsDl
58790 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73  Open(sqlite3_vfs
587a0 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
587b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
587c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73  E void sqlite3Os
587d0 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  DlError(sqlite3_
587e0 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72  vfs *, int, char
587f0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
58800 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74  ATE void (*sqlit
58810 65 33 4f 73 44 6c 53 79 6d 28 73 71 6c 69 74 65  e3OsDlSym(sqlite
58820 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 2c  3_vfs *, void *,
58830 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 29 28   const char *))(
58840 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
58850 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
58860 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69  e3OsDlClose(sqli
58870 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20  te3_vfs *, void 
58880 2a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  *);.#endif /* SQ
58890 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
588a0 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49  XTENSION */.SQLI
588b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
588c0 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65  qlite3OsRandomne
588d0 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ss(sqlite3_vfs *
588e0 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a  , int, char *);.
588f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
58900 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  nt sqlite3OsSlee
58910 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  p(sqlite3_vfs *,
58920 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
58930 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
58940 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73  3OsCurrentTime(s
58950 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 64 6f  qlite3_vfs *, do
58960 75 62 6c 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  uble*);../*.** C
58970 6f 6e 76 65 6e 69 65 6e 63 65 20 66 75 6e 63 74  onvenience funct
58980 69 6f 6e 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67  ions for opening
58990 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 66 69 6c   and closing fil
589a0 65 73 20 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c  es using .** sql
589b0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f  ite3_malloc() to
589c0 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 6f   obtain space fo
589d0 72 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c  r the file-handl
589e0 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  e structure..*/.
589f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
58a00 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e  nt sqlite3OsOpen
58a10 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76  Malloc(sqlite3_v
58a20 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  fs *, const char
58a30 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   *, sqlite3_file
58a40 20 2a 2a 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a   **, int,int*);.
58a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
58a60 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73  nt sqlite3OsClos
58a70 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 66 69  eFree(sqlite3_fi
58a80 6c 65 20 2a 29 3b 0a 0a 23 65 6e 64 69 66 20 2f  le *);..#endif /
58a90 2a 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20  * _SQLITE_OS_H_ 
58aa0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
58ab0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20  *** End of os.h 
58ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58af0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
58b00 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
58b10 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
58b20 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
58b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58b40 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
58b50 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6d 75 74 65  *** Include mute
58b60 78 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  x.h in the middl
58b70 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
58b80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
58b90 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
58ba0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d  *** Begin file m
58bb0 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  utex.h *********
58bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58be0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41  **/./*.** 2007 A
58bf0 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54  ugust 28.**.** T
58c00 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
58c10 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
58c20 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
58c30 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
58c40 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
58c50 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
58c60 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
58c70 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
58c80 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
58c90 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
58ca0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
58cb0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
58cc0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
58cd0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
58ce0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
58cf0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
58d00 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
58d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58d50 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
58d60 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
58d70 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64   the common head
58d80 65 72 20 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78  er for all mutex
58d90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
58da0 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 49  ..** The sqliteI
58db0 6e 74 2e 68 20 68 65 61 64 65 72 20 23 69 6e 63  nt.h header #inc
58dc0 6c 75 64 65 73 20 74 68 69 73 20 66 69 6c 65 20  ludes this file 
58dd0 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 61 76  so that it is av
58de0 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c  ailable.** to al
58df0 6c 20 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 20  l source files. 
58e00 20 57 65 20 62 72 65 61 6b 20 69 74 20 6f 75 74   We break it out
58e10 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f   in an effort to
58e20 20 6b 65 65 70 20 74 68 65 20 63 6f 64 65 0a 2a   keep the code.*
58e30 2a 20 62 65 74 74 65 72 20 6f 72 67 61 6e 69 7a  * better organiz
58e40 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20  ed..**.** NOTE: 
58e50 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68   source files sh
58e60 6f 75 6c 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c  ould *not* #incl
58e70 75 64 65 20 74 68 69 73 20 68 65 61 64 65 72 20  ude this header 
58e80 66 69 6c 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a  file directly..*
58e90 2a 20 53 6f 75 72 63 65 20 66 69 6c 65 73 20 73  * Source files s
58ea0 68 6f 75 6c 64 20 23 69 6e 63 6c 75 64 65 20 74  hould #include t
58eb0 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 66  he sqliteInt.h f
58ec0 69 6c 65 20 61 6e 64 20 6c 65 74 20 74 68 61 74  ile and let that
58ed0 20 66 69 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65   file.** include
58ee0 20 74 68 69 73 20 6f 6e 65 20 69 6e 64 69 72 65   this one indire
58ef0 63 74 6c 79 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  ctly..*/.../*.**
58f00 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74   Figure out what
58f10 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
58f20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68  code to use.  Th
58f30 65 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a  e choices are.**
58f40 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  .**   SQLITE_MUT
58f50 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 20  EX_OMIT         
58f60 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20  No mutex logic. 
58f70 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 2e   Not even stubs.
58f80 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20    The.**        
58f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58fa0 20 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d 70       mutexes imp
58fb0 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74  lemention cannot
58fc0 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a   be overridden.*
58fd0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
58fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74                at
58ff0 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a   start-time..**.
59000 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  **   SQLITE_MUTE
59010 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 46  X_NOOP         F
59020 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64  or single-thread
59030 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e  ed applications.
59040 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20    No.**         
59050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59060 20 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c 75      mutual exclu
59070 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64  sion is provided
59080 2e 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 20  .  But this.**  
59090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
590a0 20 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c 65             imple
590b0 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 65  mentation can be
590c0 20 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a 2a   overridden at.*
590d0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
590e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
590f0 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  art-time..**.** 
59100 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50    SQLITE_MUTEX_P
59110 54 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20  THREADS     For 
59120 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61  multi-threaded a
59130 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55  pplications on U
59140 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c  nix..**.**   SQL
59150 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20  ITE_MUTEX_W32   
59160 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69         For multi
59170 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63  -threaded applic
59180 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e  ations on Win32.
59190 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
591a0 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20  MUTEX_OS2       
591b0 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72     For multi-thr
591c0 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f  eaded applicatio
591d0 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23  ns on OS/2..*/.#
591e0 69 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 41  if !SQLITE_THREA
591f0 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53  DSAFE.# define S
59200 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54  QLITE_MUTEX_OMIT
59210 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
59220 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
59230 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
59240 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20  _MUTEX_NOOP).#  
59250 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  if SQLITE_OS_UNI
59260 58 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51  X.#    define SQ
59270 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45  LITE_MUTEX_PTHRE
59280 41 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c 49  ADS.#  elif SQLI
59290 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64  TE_OS_WIN.#    d
592a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
592b0 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 53  EX_W32.#  elif S
592c0 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20  QLITE_OS_OS2.#  
592d0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
592e0 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73  MUTEX_OS2.#  els
592f0 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51  e.#    define SQ
59300 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a  LITE_MUTEX_NOOP.
59310 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  #  endif.#endif.
59320 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
59330 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20  UTEX_OMIT./*.** 
59340 49 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d  If this is a no-
59350 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  op implementatio
59360 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65  n, implement eve
59370 72 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 6f  rything as macro
59380 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71  s..*/.#define sq
59390 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
593a0 63 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 65  c(X)    ((sqlite
593b0 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66  3_mutex*)8).#def
593c0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
593d0 78 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 6e  x_free(X).#defin
593e0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
593f0 65 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e 65  enter(X).#define
59400 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
59410 72 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 54  ry(X)      SQLIT
59420 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c  E_OK.#define sql
59430 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
59440 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  (X).#define sqli
59450 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58  te3_mutex_held(X
59460 29 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20  )     1.#define 
59470 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
59480 74 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 66  theld(X)  1.#def
59490 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78  ine sqlite3Mutex
594a0 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 28  Alloc(X)      ((
594b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38  sqlite3_mutex*)8
594c0 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ).#define sqlite
594d0 33 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 20  3MutexInit()    
594e0 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64      SQLITE_OK.#d
594f0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74  efine sqlite3Mut
59500 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f  exEnd().#endif /
59510 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  * defined(SQLITE
59520 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20 2a 2f 0a  _MUTEX_OMIT) */.
59530 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
59540 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 20   End of mutex.h 
59550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
59580 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
59590 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
595a0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
595b0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
595c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
595d0 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61  .../*.** Each da
595e0 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62  tabase file to b
595f0 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68  e accessed by th
59600 65 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69  e system is an i
59610 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68  nstance.** of th
59620 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
59630 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72  cture.  There ar
59640 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f  e normally two o
59650 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
59660 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c  es.** in the sql
59670 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e  ite.aDb[] array.
59680 20 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20    aDb[0] is the 
59690 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
596a0 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d  le and.** aDb[1]
596b0 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
596c0 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f   file used to ho
596d0 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ld temporary tab
596e0 6c 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  les.  Additional
596f0 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61  .** databases ma
59700 79 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a  y be attached..*
59710 2f 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20  /.struct Db {.  
59720 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
59730 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
59740 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
59750 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20  .  Btree *pBt;  
59760 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42          /* The B
59770 2a 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20  *Tree structure 
59780 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73  for this databas
59790 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69  e file */.  u8 i
597a0 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20  nTrans;         
597b0 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61   /* 0: not writa
597c0 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63  ble.  1: Transac
597d0 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70  tion.  2: Checkp
597e0 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66  oint */.  u8 saf
597f0 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f  ety_level;     /
59800 2a 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65  * How aggressive
59810 20 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 61   at syncing data
59820 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 53 63   to disk */.  Sc
59830 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20  hema *pSchema;  
59840 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
59850 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
59860 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61 72 65   (possibly share
59870 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  d) */.};../*.** 
59880 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
59890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
598a0 75 63 74 75 72 65 20 73 74 6f 72 65 73 20 61 20  ucture stores a 
598b0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
598c0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
598d0 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c 20 74  are no virtual t
598e0 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 65 64  ables configured
598f0 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d 61 2c   in this schema,
59900 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64   the.** Schema.d
59910 62 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  b variable is se
59920 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72  t to NULL. After
59930 20 74 68 65 20 66 69 72 73 74 20 76 69 72 74 75   the first virtu
59940 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20  al table.** has 
59950 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74 20 69  been added, it i
59960 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  s set to point t
59970 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  o the database c
59980 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73  onnection .** us
59990 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ed to create the
599a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63   connection. Onc
599b0 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
599c0 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64  e has been.** ad
599d0 64 65 64 20 74 6f 20 74 68 65 20 53 63 68 65 6d  ded to the Schem
599e0 61 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  a structure and 
599f0 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20 76 61  the Schema.db va
59a00 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 65 64  riable populated
59a10 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20  , .** only that 
59a20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
59a30 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 65 20  ion may use the 
59a40 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70 61 72  Schema to prepar
59a50 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73  e .** statements
59a60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 68 65  ..*/.struct Sche
59a70 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d  ma {.  int schem
59a80 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44  a_cookie;   /* D
59a90 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 76  atabase schema v
59aa0 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f  ersion number fo
59ab0 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20  r this file */. 
59ac0 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b 20 20   Hash tblHash;  
59ad0 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62        /* All tab
59ae0 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e  les indexed by n
59af0 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 69 64  ame */.  Hash id
59b00 78 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a  xHash;        /*
59b10 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64   All (named) ind
59b20 69 63 65 73 20 69 6e 64 65 78 65 64 20 62 79 20  ices indexed by 
59b30 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 74  name */.  Hash t
59b40 72 69 67 48 61 73 68 3b 20 20 20 20 20 20 20 2f  rigHash;       /
59b50 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 69  * All triggers i
59b60 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a  ndexed by name *
59b70 2f 0a 20 20 48 61 73 68 20 66 6b 65 79 48 61 73  /.  Hash fkeyHas
59b80 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20  h;       /* All 
59b90 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 62 79 20  foreign keys by 
59ba0 72 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65  referenced table
59bb0 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 62 6c 65   name */.  Table
59bc0 20 2a 70 53 65 71 54 61 62 3b 20 20 20 20 20 20   *pSeqTab;      
59bd0 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 65  /* The sqlite_se
59be0 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65  quence table use
59bf0 64 20 62 79 20 41 55 54 4f 49 4e 43 52 45 4d 45  d by AUTOINCREME
59c00 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f  NT */.  u8 file_
59c10 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20  format;      /* 
59c20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 76 65  Schema format ve
59c30 72 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 66  rsion for this f
59c40 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b  ile */.  u8 enc;
59c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
59c60 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75   Text encoding u
59c70 73 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61  sed by this data
59c80 62 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c  base */.  u16 fl
59c90 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ags;           /
59ca0 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74  * Flags associat
59cb0 65 64 20 77 69 74 68 20 74 68 69 73 20 73 63 68  ed with this sch
59cc0 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63  ema */.  int cac
59cd0 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a  he_size;      /*
59ce0 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
59cf0 20 74 6f 20 75 73 65 20 69 6e 20 74 68 65 20 63   to use in the c
59d00 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20  ache */.#ifndef 
59d10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
59d20 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74  UALTABLE.  sqlit
59d30 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
59d40 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65  /* "Owner" conne
59d50 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65  ction. See comme
59d60 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64  nt above */.#end
59d70 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  if.};../*.** The
59d80 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65  se macros can be
59d90 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73   used to test, s
59da0 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74  et, or clear bit
59db0 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e  s in the .** Db.
59dc0 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a  flags field..*/.
59dd0 23 64 65 66 69 6e 65 20 44 62 48 61 73 50 72 6f  #define DbHasPro
59de0 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20  perty(D,I,P)    
59df0 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70   (((D)->aDb[I].p
59e00 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50  Schema->flags&(P
59e10 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65  ))==(P)).#define
59e20 20 44 62 48 61 73 41 6e 79 50 72 6f 70 65 72 74   DbHasAnyPropert
59e30 79 28 44 2c 49 2c 50 29 20 20 28 28 28 44 29 2d  y(D,I,P)  (((D)-
59e40 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[I].pSchema-
59e50 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a  >flags&(P))!=0).
59e60 23 64 65 66 69 6e 65 20 44 62 53 65 74 50 72 6f  #define DbSetPro
59e70 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20  perty(D,I,P)    
59e80 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63   (D)->aDb[I].pSc
59e90 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29  hema->flags|=(P)
59ea0 0a 23 64 65 66 69 6e 65 20 44 62 43 6c 65 61 72  .#define DbClear
59eb0 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20  Property(D,I,P) 
59ec0 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53    (D)->aDb[I].pS
59ed0 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28  chema->flags&=~(
59ee0 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65  P)../*.** Allowe
59ef0 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65  d values for the
59f00 20 44 42 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e   DB.flags field.
59f10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63  .**.** The DB_Sc
59f20 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 67 20  hemaLoaded flag 
59f30 69 73 20 73 65 74 20 61 66 74 65 72 20 74 68 65  is set after the
59f40 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
59f50 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61   has been.** rea
59f60 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20  d into internal 
59f70 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a  hash tables..**.
59f80 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65  ** DB_UnresetVie
59f90 77 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e  ws means that on
59fa0 65 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20  e or more views 
59fb0 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  have column name
59fc0 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62  s that.** have b
59fd0 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20  een filled out. 
59fe0 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 63   If the schema c
59ff0 68 61 6e 67 65 73 2c 20 74 68 65 73 65 20 63 6f  hanges, these co
5a000 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74  lumn names might
5a010 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e 64 20  .** changes and 
5a020 73 6f 20 74 68 65 20 76 69 65 77 20 77 69 6c 6c  so the view will
5a030 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 73 65   need to be rese
5a040 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42  t..*/.#define DB
5a050 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 20 20  _SchemaLoaded   
5a060 20 30 78 30 30 30 31 20 20 2f 2a 20 54 68 65 20   0x0001  /* The 
5a070 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20  schema has been 
5a080 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e  loaded */.#defin
5a090 65 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  e DB_UnresetView
5a0a0 73 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20  s    0x0002  /* 
5a0b0 53 6f 6d 65 20 76 69 65 77 73 20 68 61 76 65 20  Some views have 
5a0c0 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e  defined column n
5a0d0 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ames */.#define 
5a0e0 44 42 5f 45 6d 70 74 79 20 20 20 20 20 20 20 20  DB_Empty        
5a0f0 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 54 68     0x0004  /* Th
5a100 65 20 66 69 6c 65 20 69 73 20 65 6d 70 74 79 20  e file is empty 
5a110 28 6c 65 6e 67 74 68 20 30 20 62 79 74 65 73 29  (length 0 bytes)
5a120 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e   */../*.** The n
5a130 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65  umber of differe
5a140 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e  nt kinds of thin
5a150 67 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 6c  gs that can be l
5a160 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20  imited.** using 
5a170 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  the sqlite3_limi
5a180 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  t() interface..*
5a190 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5a1a0 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49 54 45  _N_LIMIT (SQLITE
5a1b0 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44  _LIMIT_TRIGGER_D
5a1c0 45 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c  EPTH+1)../*.** L
5a1d0 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20  ookaside malloc 
5a1e0 69 73 20 61 20 73 65 74 20 6f 66 20 66 69 78 65  is a set of fixe
5a1f0 64 2d 73 69 7a 65 20 62 75 66 66 65 72 73 20 74  d-size buffers t
5a200 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 0a  hat can be used.
5a210 2a 2a 20 74 6f 20 73 61 74 69 73 66 79 20 73 6d  ** to satisfy sm
5a220 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 6d 65  all transient me
5a230 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
5a240 72 65 71 75 65 73 74 73 20 66 6f 72 20 6f 62 6a  requests for obj
5a250 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74  ects.** associat
5a260 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63  ed with a partic
5a270 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f  ular database co
5a280 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 75  nnection.  The u
5a290 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69  se of.** lookasi
5a2a0 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64  de malloc provid
5a2b0 65 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74  es a significant
5a2c0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68   performance enh
5a2d0 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70  ancement.** (app
5a2e0 72 6f 78 20 31 30 25 29 20 62 79 20 61 76 6f 69  rox 10%) by avoi
5a2f0 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61  ding numerous ma
5a300 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 65 73  lloc/free reques
5a310 74 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67  ts while parsing
5a320 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  .** SQL statemen
5a330 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f  ts..**.** The Lo
5a340 6f 6b 61 73 69 64 65 20 73 74 72 75 63 74 75 72  okaside structur
5a350 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72  e holds configur
5a360 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ation informatio
5a370 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c  n about the.** l
5a380 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20  ookaside malloc 
5a390 73 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63 68  subsystem.  Each
5a3a0 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72   available memor
5a3b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a  y allocation in.
5a3c0 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65  ** the lookaside
5a3d0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 73 74   subsystem is st
5a3e0 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64  ored on a linked
5a3f0 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69   list of Lookasi
5a400 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74  deSlot.** object
5a410 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69  s..**.** Lookasi
5a420 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61  de allocations a
5a430 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20  re only allowed 
5a440 66 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 61 74  for objects that
5a450 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a   are associated.
5a460 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63  ** with a partic
5a470 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f  ular database co
5a480 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65  nnection.  Hence
5a490 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61  , schema informa
5a4a0 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62  tion cannot.** b
5a4b0 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b  e stored in look
5a4c0 61 73 69 64 65 20 62 65 63 61 75 73 65 20 69 6e  aside because in
5a4d0 20 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f   shared cache mo
5a4e0 64 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e  de the schema in
5a4f0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20  formation.** is 
5a500 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69 70  shared by multip
5a510 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  le database conn
5a520 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 65 66  ections.  Theref
5a530 6f 72 65 2c 20 77 68 69 6c 65 20 70 61 72 73 69  ore, while parsi
5a540 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66  ng.** schema inf
5a550 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f  ormation, the Lo
5a560 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
5a570 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64   flag is cleared
5a580 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b   so that.** look
5a590 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  aside allocation
5a5a0 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 74  s are not used t
5a5b0 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20  o construct the 
5a5c0 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a  schema objects..
5a5d0 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73  */.struct Lookas
5a5e0 69 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20  ide {.  u16 sz; 
5a5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a600 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20  /* Size of each 
5a610 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20  buffer in bytes 
5a620 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 64  */.  u8 bEnabled
5a630 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
5a640 46 61 6c 73 65 20 74 6f 20 64 69 73 61 62 6c 65  False to disable
5a650 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61   new lookaside a
5a660 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20  llocations */.  
5a670 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20  u8 bMalloced;   
5a680 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
5a690 69 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e  if pStart obtain
5a6a0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
5a6b0 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e  malloc() */.  in
5a6c0 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20  t nOut;         
5a6d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
5a6e0 6f 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65  of buffers curre
5a6f0 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74  ntly checked out
5a700 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b   */.  int mxOut;
5a710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5a720 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20   Highwater mark 
5a730 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f  for nOut */.  Lo
5a740 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 72  okasideSlot *pFr
5a750 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ee;   /* List of
5a760 20 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 65   available buffe
5a770 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  rs */.  void *pS
5a780 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20  tart;           
5a790 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66  /* First byte of
5a7a0 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72   available memor
5a7b0 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69  y space */.  voi
5a7c0 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20  d *pEnd;        
5a7d0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79       /* First by
5a7e0 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61  te past end of a
5a7f0 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a  vailable space *
5a800 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b  /.};.struct Look
5a810 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f  asideSlot {.  Lo
5a820 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65  okasideSlot *pNe
5a830 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 62  xt;    /* Next b
5a840 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 73  uffer in the lis
5a850 74 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 72  t of free buffer
5a860 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  s */.};../*.** A
5a870 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20   hash table for 
5a880 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
5a890 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68  ions..**.** Hash
5a8a0 20 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 74   each FuncDef st
5a8b0 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65  ructure into one
5a8c0 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 48   of the FuncDefH
5a8d0 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a  ash.a[] slots..*
5a8e0 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65  * Collisions are
5a8f0 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 2e   on the FuncDef.
5a900 70 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a  pHash chain..*/.
5a910 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61  struct FuncDefHa
5a920 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  sh {.  FuncDef *
5a930 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20  a[23];       /* 
5a940 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66  Hash table for f
5a950 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a  unctions */.};..
5a960 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62  /*.** Each datab
5a970 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ase is an instan
5a980 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
5a990 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
5a9a0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e  *.** The sqlite.
5a9b0 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64  lastRowid record
5a9c0 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72  s the last inser
5a9d0 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65  t rowid generate
5a9e0 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72  d by an.** inser
5a9f0 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e  t statement.  In
5aa00 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64  serts on views d
5aa10 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73  o not affect its
5aa20 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a   value.  Each.**
5aa30 20 74 72 69 67 67 65 72 20 68 61 73 20 69 74 73   trigger has its
5aa40 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f   own context, so
5aa50 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20   that lastRowid 
5aa60 63 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 69  can be updated i
5aa70 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72  nside.** trigger
5aa80 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65  s as usual.  The
5aa90 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
5aaa0 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64  will be restored
5aab0 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65   once the trigge
5aac0 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f  r.** exits.  Upo
5aad0 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66  n entering a bef
5aae0 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f  ore or instead o
5aaf0 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52  f trigger, lastR
5ab00 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f  owid is no.** lo
5ab10 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 65  nger (since afte
5ab20 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32  r version 2.8.12
5ab30 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a  ) reset to -1..*
5ab40 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e  *.** The sqlite.
5ab50 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74  nChange does not
5ab60 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77   count changes w
5ab70 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 61  ithin triggers a
5ab80 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63  nd keeps no.** c
5ab90 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 72  ontext.  It is r
5aba0 65 73 65 74 20 61 74 20 73 74 61 72 74 20 6f 66  eset at start of
5abb0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a   sqlite3_exec..*
5abc0 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43  * The sqlite.lsC
5abd0 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73  hange represents
5abe0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
5abf0 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74  hanges made by t
5ac00 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72  he last.** inser
5ac10 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65  t, update, or de
5ac20 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20  lete statement. 
5ac30 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73   It remains cons
5ac40 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20  tant throughout 
5ac50 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66  the.** length of
5ac60 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64   a statement and
5ac70 20 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 64   is then updated
5ac80 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73   by OP_SetCounts
5ac90 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a  .  It keeps a.**
5aca0 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a   context stack j
5acb0 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77  ust like lastRow
5acc0 69 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  id so that the c
5acd0 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a  ount of changes.
5ace0 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 67  ** within a trig
5acf0 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20  ger is not seen 
5ad00 6f 75 74 73 69 64 65 20 74 68 65 20 74 72 69 67  outside the trig
5ad10 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f  ger.  Changes to
5ad20 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a   views do not.**
5ad30 20 61 66 66 65 63 74 20 74 68 65 20 76 61 6c 75   affect the valu
5ad40 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a  e of lsChange..*
5ad50 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43  * The sqlite.csC
5ad60 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63  hange keeps trac
5ad70 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  k of the number 
5ad80 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67  of current chang
5ad90 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65  es (since.** the
5ada0 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29   last statement)
5adb0 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20   and is used to 
5adc0 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73  update sqlite_ls
5add0 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  Change..**.** Th
5ade0 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c  e member variabl
5adf0 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64  es sqlite.errCod
5ae00 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73  e, sqlite.zErrMs
5ae10 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72  g and sqlite.zEr
5ae20 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20  rMsg16.** store 
5ae30 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
5ae40 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20  error code and, 
5ae50 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73  if applicable, s
5ae60 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e  tring. The.** in
5ae70 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20  ternal function 
5ae80 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69  sqlite3Error() i
5ae90 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68  s used to set th
5aea0 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a  ese variables.**
5aeb0 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a   consistently..*
5aec0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
5aed0 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73   {.  sqlite3_vfs
5aee0 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20   *pVfs;         
5aef0 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61     /* OS Interfa
5af00 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b  ce */.  int nDb;
5af10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
5af30 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72  of backends curr
5af40 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a  ently in use */.
5af50 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20    Db *aDb;      
5af60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af70 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20  /* All backends 
5af80 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20  */.  int flags; 
5af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5afa0 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65     /* Miscellane
5afb0 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62  ous flags. See b
5afc0 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  elow */.  int op
5afd0 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  enFlags;        
5afe0 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
5aff0 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74   passed to sqlit
5b000 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a  e3_vfs.xOpen() *
5b010 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b  /.  int errCode;
5b020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b030 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
5b040 20 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c   error code (SQL
5b050 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20  ITE_*) */.  int 
5b060 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20  errMask;        
5b070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72            /* & r
5b080 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68  esult codes with
5b090 20 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74   this before ret
5b0a0 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61  urning */.  u8 a
5b0b0 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20  utoCommit;      
5b0c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
5b0d0 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61   auto-commit fla
5b0e0 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f  g. */.  u8 temp_
5b0f0 73 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20  store;          
5b100 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65        /* 1: file
5b110 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65   2: memory 0: de
5b120 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61  fault */.  u8 ma
5b130 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20  llocFailed;     
5b140 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
5b150 20 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e   if we have seen
5b160 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
5b170 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f  e */.  u8 dfltLo
5b180 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20  ckMode;         
5b190 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
5b1a0 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72  locking-mode for
5b1b0 20 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f   attached dbs */
5b1c0 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61  .  u8 dfltJourna
5b1d0 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  lMode;          
5b1e0 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72   /* Default jour
5b1f0 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74  nal mode for att
5b200 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73  ached dbs */.  s
5b210 69 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41  igned char nextA
5b220 75 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20  utovac;      /* 
5b230 41 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20  Autovac setting 
5b240 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20  after VACUUM if 
5b250 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78  >=0 */.  int nex
5b260 74 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20  tPagesize;      
5b270 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69         /* Pagesi
5b280 7a 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20  ze after VACUUM 
5b290 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e  if >0 */.  int n
5b2a0 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
5b2b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5b2c0 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
5b2d0 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
5b2e0 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74    CollSeq *pDflt
5b2f0 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Coll;           
5b300 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63  /* The default c
5b310 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
5b320 65 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20  e (BINARY) */.  
5b330 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20  i64 lastRowid;  
5b340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5b350 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72   ROWID of most r
5b360 65 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65  ecent insert (se
5b370 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 75 33  e above) */.  u3
5b380 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20  2 magic;        
5b390 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
5b3a0 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20  agic number for 
5b3b0 64 65 74 65 63 74 20 6c 69 62 72 61 72 79 20 6d  detect library m
5b3c0 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  isuse */.  int n
5b3d0 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20  Change;         
5b3e0 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
5b3f0 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
5b400 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20  lite3_changes() 
5b410 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43  */.  int nTotalC
5b420 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20  hange;          
5b430 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75     /* Value retu
5b440 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
5b450 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20  total_changes() 
5b460 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
5b470 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20  ex *mutex;      
5b480 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e     /* Connection
5b490 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20   mutex */.  int 
5b4a0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4e 5f  aLimit[SQLITE_N_
5b4b0 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d  LIMIT];   /* Lim
5b4c0 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  its */.  struct 
5b4d0 73 71 6c 69 74 65 33 49 6e 69 74 49 6e 66 6f 20  sqlite3InitInfo 
5b4e0 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  {      /* Inform
5b4f0 61 74 69 6f 6e 20 75 73 65 64 20 64 75 72 69 6e  ation used durin
5b500 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  g initialization
5b510 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b   */.    int iDb;
5b520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b530 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b      /* When back
5b540 20 69 73 20 62 65 69 6e 67 20 69 6e 69 74 69 61   is being initia
5b550 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74  lized */.    int
5b560 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20 20 20   newTnum;       
5b570 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
5b580 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 62 65  page of table be
5b590 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  ing initialized 
5b5a0 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 79 3b 20  */.    u8 busy; 
5b5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b5c0 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 63 75     /* TRUE if cu
5b5d0 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69  rrently initiali
5b5e0 7a 69 6e 67 20 2a 2f 0a 20 20 20 20 75 38 20 6f  zing */.    u8 o
5b5f0 72 70 68 61 6e 54 72 69 67 67 65 72 3b 20 20 20  rphanTrigger;   
5b600 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
5b610 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 72 70  statement is orp
5b620 68 61 6e 65 64 20 54 45 4d 50 20 74 72 69 67 67  haned TEMP trigg
5b630 65 72 20 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a  er */.  } init;.
5b640 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e    int nExtension
5b650 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5b660 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61  /* Number of loa
5b670 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a  ded extensions *
5b680 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65  /.  void **aExte
5b690 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20  nsion;          
5b6a0 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68    /* Array of sh
5b6b0 61 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e  ared library han
5b6c0 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  dles */.  struct
5b6d0 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20   Vdbe *pVdbe;   
5b6e0 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
5b6f0 6f 66 20 61 63 74 69 76 65 20 76 69 72 74 75 61  of active virtua
5b700 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20  l machines */.  
5b710 69 6e 74 20 61 63 74 69 76 65 56 64 62 65 43 6e  int activeVdbeCn
5b720 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
5b730 20 4e 75 6d 62 65 72 20 6f 66 20 56 44 42 45 73   Number of VDBEs
5b740 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75   currently execu
5b750 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72  ting */.  int wr
5b760 69 74 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20  iteVdbeCnt;     
5b770 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5b780 72 20 6f 66 20 61 63 74 69 76 65 20 56 44 42 45  r of active VDBE
5b790 73 20 74 68 61 74 20 61 72 65 20 77 72 69 74 69  s that are writi
5b7a0 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  ng */.  void (*x
5b7b0 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
5b7c0 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20 20 20  st char*);      
5b7d0 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e 63 74    /* Trace funct
5b7e0 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ion */.  void *p
5b7f0 54 72 61 63 65 41 72 67 3b 20 20 20 20 20 20 20  TraceArg;       
5b800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b810 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
5b820 6f 20 74 68 65 20 74 72 61 63 65 20 66 75 6e 63  o the trace func
5b830 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tion */.  void (
5b840 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a  *xProfile)(void*
5b850 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 36 34  ,const char*,u64
5b860 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67  );  /* Profiling
5b870 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
5b880 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 41 72 67  oid *pProfileArg
5b890 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5b8a0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
5b8b0 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c 65 20  ment to profile 
5b8c0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
5b8d0 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20  id *pCommitArg; 
5b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b8f0 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78  /* Argument to x
5b900 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29  CommitCallback()
5b910 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28 2a 78   */   .  int (*x
5b920 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28  CommitCallback)(
5b930 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e  void*);    /* In
5b940 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63  voked at every c
5b950 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64  ommit. */.  void
5b960 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20   *pRollbackArg; 
5b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5b980 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f   Argument to xRo
5b990 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29  llbackCallback()
5b9a0 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20 28 2a   */   .  void (*
5b9b0 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
5b9c0 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e  k)(void*); /* In
5b9d0 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63  voked at every c
5b9e0 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64  ommit. */.  void
5b9f0 20 2a 70 55 70 64 61 74 65 41 72 67 3b 0a 20 20   *pUpdateArg;.  
5ba00 76 6f 69 64 20 28 2a 78 55 70 64 61 74 65 43 61  void (*xUpdateCa
5ba10 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
5ba20 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  t, const char*,c
5ba30 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74  onst char*,sqlit
5ba40 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64  e_int64);.  void
5ba50 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76  (*xCollNeeded)(v
5ba60 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
5ba70 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
5ba80 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28   char*);.  void(
5ba90 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28  *xCollNeeded16)(
5baa0 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69  void*,sqlite3*,i
5bab0 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73  nt eTextRep,cons
5bac0 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  t void*);.  void
5bad0 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67   *pCollNeededArg
5bae0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
5baf0 65 20 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20  e *pErr;        
5bb00 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
5bb10 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a   error message *
5bb20 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  /.  char *zErrMs
5bb30 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
5bb40 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
5bb50 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28   error message (
5bb60 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a  UTF-8 encoded) *
5bb70 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  /.  char *zErrMs
5bb80 67 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20  g16;            
5bb90 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
5bba0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28   error message (
5bbb0 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20  UTF-16 encoded) 
5bbc0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20  */.  union {.   
5bbd0 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73   volatile int is
5bbe0 49 6e 74 65 72 72 75 70 74 65 64 3b 20 2f 2a 20  Interrupted; /* 
5bbf0 54 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f  True if sqlite3_
5bc00 69 6e 74 65 72 72 75 70 74 20 68 61 73 20 62 65  interrupt has be
5bc10 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20  en called */.   
5bc20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 64 31   double notUsed1
5bc30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
5bc40 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31  Spacer */.  } u1
5bc50 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f  ;.  Lookaside lo
5bc60 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20  okaside;        
5bc70 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d    /* Lookaside m
5bc80 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 61 74  alloc configurat
5bc90 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ion */.#ifndef S
5bca0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
5bcb0 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28  RIZATION.  int (
5bcc0 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e  *xAuth)(void*,in
5bcd0 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
5bce0 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
5bcf0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
5bd00 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  *);.            
5bd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bd20 20 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75      /* Access au
5bd30 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63  thorization func
5bd40 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
5bd50 70 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20  pAuthArg;       
5bd60 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61          /* 1st a
5bd70 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61  rgument to the a
5bd80 63 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74  ccess auth funct
5bd90 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ion */.#endif.#i
5bda0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
5bdb0 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
5bdc0 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f  ACK.  int (*xPro
5bdd0 67 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20  gress)(void *); 
5bde0 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72      /* The progr
5bdf0 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ess callback */.
5be00 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73    void *pProgres
5be10 73 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  sArg;           
5be20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
5be30 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c  he progress call
5be40 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50  back */.  int nP
5be50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20  rogressOps;     
5be60 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5be70 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72  r of opcodes for
5be80 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
5be90 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  ck */.#endif.#if
5bea0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
5beb0 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
5bec0 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20  Hash aModule;   
5bed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5bee0 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71   populated by sq
5bef0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
5bf00 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65  ule() */.  Table
5bf10 20 2a 70 56 54 61 62 3b 20 20 20 20 20 20 20 20   *pVTab;        
5bf20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 74 61 62           /* vtab
5bf30 20 77 69 74 68 20 61 63 74 69 76 65 20 43 6f 6e   with active Con
5bf40 6e 65 63 74 2f 43 72 65 61 74 65 20 6d 65 74 68  nect/Create meth
5bf50 6f 64 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a  od */.  VTable *
5bf60 2a 61 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20  *aVTrans;       
5bf70 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c        /* Virtual
5bf80 20 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65   tables with ope
5bf90 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a  n transactions *
5bfa0 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b  /.  int nVTrans;
5bfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bfc0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73    /* Allocated s
5bfd0 69 7a 65 20 6f 66 20 61 56 54 72 61 6e 73 20 2a  ize of aVTrans *
5bfe0 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 44 69 73  /.  VTable *pDis
5bff0 63 6f 6e 6e 65 63 74 3b 20 20 20 20 2f 2a 20 44  connect;    /* D
5c000 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 73 65 20  isconnect these 
5c010 69 6e 20 6e 65 78 74 20 73 71 6c 69 74 65 33 5f  in next sqlite3_
5c020 70 72 65 70 61 72 65 28 29 20 2a 2f 0a 23 65 6e  prepare() */.#en
5c030 64 69 66 0a 20 20 46 75 6e 63 44 65 66 48 61 73  dif.  FuncDefHas
5c040 68 20 61 46 75 6e 63 3b 20 20 20 20 20 20 20 20  h aFunc;        
5c050 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c      /* Hash tabl
5c060 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e of connection 
5c070 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 48  functions */.  H
5c080 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20  ash aCollSeq;   
5c090 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5c0a0 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  All collating se
5c0b0 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73  quences */.  Bus
5c0c0 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48 61 6e  yHandler busyHan
5c0d0 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 42 75  dler;      /* Bu
5c0e0 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  sy callback */. 
5c0f0 20 69 6e 74 20 62 75 73 79 54 69 6d 65 6f 75 74   int busyTimeout
5c100 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
5c110 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 72 20 74  * Busy handler t
5c120 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20  imeout, in msec 
5c130 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 61 74 69  */.  Db aDbStati
5c140 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20  c[2];           
5c150 20 20 20 2f 2a 20 53 74 61 74 69 63 20 73 70 61     /* Static spa
5c160 63 65 20 66 6f 72 20 74 68 65 20 32 20 64 65 66  ce for the 2 def
5c170 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 20 2a 2f  ault backends */
5c180 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53  .  Savepoint *pS
5c190 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20  avepoint;       
5c1a0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69   /* List of acti
5c1b0 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f  ve savepoints */
5c1c0 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e  .  int nSavepoin
5c1d0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
5c1e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
5c1f0 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61  n-transaction sa
5c200 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  vepoints */.  in
5c210 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20  t nStatement;   
5c220 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5c230 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20  umber of nested 
5c240 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61  statement-transa
5c250 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20  ctions  */.  u8 
5c260 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76  isTransactionSav
5c270 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72  epoint;    /* Tr
5c280 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d  ue if the outerm
5c290 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73  ost savepoint is
5c2a0 20 61 20 54 53 20 2a 2f 0a 20 20 69 36 34 20 6e   a TS */.  i64 n
5c2b0 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 20  DeferredCons;   
5c2c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 74 20           /* Net 
5c2d0 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61  deferred constra
5c2e0 69 6e 74 73 20 74 68 69 73 20 74 72 61 6e 73 61  ints this transa
5c2f0 63 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69 66 64 65  ction. */..#ifde
5c300 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
5c310 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20  UNLOCK_NOTIFY.  
5c320 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
5c330 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 61   variables are a
5c340 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 20  ll protected by 
5c350 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45  the STATIC_MASTE
5c360 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e  R .  ** mutex, n
5c370 6f 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d 75  ot by sqlite3.mu
5c380 74 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 73  tex. They are us
5c390 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f  ed by code in no
5c3a0 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20  tify.c. .  **.  
5c3b0 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63  ** When X.pUnloc
5c3c0 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20  kConnection==Y, 
5c3d0 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
5c3e0 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72  X is waiting for
5c3f0 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63   Y to.  ** unloc
5c400 6b 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  k so that it can
5c410 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20   proceed..  **. 
5c420 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63   ** When X.pBloc
5c430 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  kingConnection==
5c440 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  Y, that means th
5c450 61 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61  at something tha
5c460 74 20 58 20 74 72 69 65 64 0a 20 20 2a 2a 20 74  t X tried.  ** t
5c470 72 69 65 64 20 74 6f 20 64 6f 20 72 65 63 65 6e  ried to do recen
5c480 74 6c 79 20 66 61 69 6c 65 64 20 77 69 74 68 20  tly failed with 
5c490 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  an SQLITE_LOCKED
5c4a0 20 65 72 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f   error due to lo
5c4b0 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79  cks.  ** held by
5c4c0 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74   Y..  */.  sqlit
5c4d0 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e  e3 *pBlockingCon
5c4e0 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e  nection; /* Conn
5c4f0 65 63 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73  ection that caus
5c500 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  ed SQLITE_LOCKED
5c510 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70   */.  sqlite3 *p
5c520 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
5c530 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  ;           /* C
5c540 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 61 74  onnection to wat
5c550 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f  ch for unlock */
5c560 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b  .  void *pUnlock
5c570 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
5c580 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
5c590 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e  ment to xUnlockN
5c5a0 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20  otify */.  void 
5c5b0 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29  (*xUnlockNotify)
5c5c0 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20  (void **, int); 
5c5d0 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66   /* Unlock notif
5c5e0 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  y callback */.  
5c5f0 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42 6c  sqlite3 *pNextBl
5c600 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f 2a  ocked;        /*
5c610 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f 66   Next in list of
5c620 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e   all blocked con
5c630 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64  nections */.#end
5c640 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d  if.};../*.** A m
5c650 61 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72  acro to discover
5c660 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66   the encoding of
5c670 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a   a database..*/.
5c680 23 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20  #define ENC(db) 
5c690 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53  ((db)->aDb[0].pS
5c6a0 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a  chema->enc)../*.
5c6b0 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75  ** Possible valu
5c6c0 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74  es for the sqlit
5c6d0 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44  e.flags and or D
5c6e0 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a  b.flags fields..
5c6f0 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e  **.** On sqlite.
5c700 66 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54  flags, the SQLIT
5c710 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20  E_InTrans value 
5c720 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61  means that we ha
5c730 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61  ve.** executed a
5c740 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66   BEGIN.  On Db.f
5c750 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54  lags, SQLITE_InT
5c760 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61  rans means a sta
5c770 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61  tement.** transa
5c780 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20  ction is active 
5c790 6f 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c  on that particul
5c7a0 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ar database file
5c7b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5c7c0 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20  ITE_VdbeTrace   
5c7d0 20 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f     0x00000001  /
5c7e0 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20  * True to trace 
5c7f0 56 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a  VDBE execution *
5c800 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5c810 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20  _InTrans        
5c820 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54  0x00000008  /* T
5c830 72 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e  rue if in a tran
5c840 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  saction */.#defi
5c850 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  ne SQLITE_Intern
5c860 43 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30  Changes  0x00000
5c870 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74  010  /* Uncommit
5c880 74 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63  ted Hash table c
5c890 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e  hanges */.#defin
5c8a0 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c  e SQLITE_FullCol
5c8b0 4e 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30  Names   0x000000
5c8c0 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c  20  /* Show full
5c8d0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e   column names on
5c8e0 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69   SELECT */.#defi
5c8f0 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43  ne SQLITE_ShortC
5c900 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30  olNames  0x00000
5c910 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f  040  /* Show sho
5c920 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73  rt columns names
5c930 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5c940 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20  TE_CountRows    
5c950 20 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a    0x00000080  /*
5c960 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e   Count rows chan
5c970 67 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a  ged by INSERT, *
5c980 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
5c990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c9a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
5c9b0 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41   DELETE, or UPDA
5c9c0 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f  TE and return */
5c9d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
5c9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c9f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20             /*   
5ca00 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20  the count using 
5ca10 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23  a callback. */.#
5ca20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75  define SQLITE_Nu
5ca30 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30  llCallback   0x0
5ca40 30 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f  0000100  /* Invo
5ca50 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ke the callback 
5ca60 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20  once if the */. 
5ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ca80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ca90 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65           /*   re
5caa0 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74  sult set is empt
5cab0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  y */.#define SQL
5cac0 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20  ITE_SqlTrace    
5cad0 20 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f     0x00000200  /
5cae0 2a 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51  * Debug print SQ
5caf0 4c 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73  L as it executes
5cb00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5cb10 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20  TE_VdbeListing  
5cb20 20 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a    0x00000400  /*
5cb30 20 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20   Debug listings 
5cb40 6f 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73  of VDBE programs
5cb50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5cb60 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20  TE_WriteSchema  
5cb70 20 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a    0x00000800  /*
5cb80 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51   OK to update SQ
5cb90 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23  LITE_MASTER */.#
5cba0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f  define SQLITE_No
5cbb0 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30  Readlock     0x0
5cbc0 30 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64  0001000  /* Read
5cbd0 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65  locks are omitte
5cbe0 64 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20  d when .        
5cbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cc10 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72    ** accessing r
5cc20 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73  ead-only databas
5cc30 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  es */.#define SQ
5cc40 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b  LITE_IgnoreCheck
5cc50 73 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20  s   0x00002000  
5cc60 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63  /* Do not enforc
5cc70 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69  e check constrai
5cc80 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nts */.#define S
5cc90 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
5cca0 69 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30  itted 0x00004000
5ccb0 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63   /* For shared-c
5ccc0 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65  ache mode */.#de
5ccd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61  fine SQLITE_Lega
5cce0 63 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30  cyFileFmt  0x000
5ccf0 30 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65  08000  /* Create
5cd00 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69   new databases i
5cd10 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64  n format 1 */.#d
5cd20 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c  efine SQLITE_Ful
5cd30 6c 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30  lFSync      0x00
5cd40 30 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66  010000  /* Use f
5cd50 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65  ull fsync on the
5cd60 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66   backend */.#def
5cd70 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45  ine SQLITE_LoadE
5cd80 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32  xtension  0x0002
5cd90 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20  0000  /* Enable 
5cda0 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a  load_extension *
5cdb0 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /..#define SQLIT
5cdc0 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20  E_RecoveryMode  
5cdd0 20 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20   0x00040000  /* 
5cde0 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72  Ignore schema er
5cdf0 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rors */.#define 
5ce00 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72  SQLITE_ReverseOr
5ce10 64 65 72 20 20 20 30 78 30 30 31 30 30 30 30 30  der   0x00100000
5ce20 20 20 2f 2a 20 52 65 76 65 72 73 65 20 75 6e 6f    /* Reverse uno
5ce30 72 64 65 72 65 64 20 53 45 4c 45 43 54 73 20 2a  rdered SELECTs *
5ce40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5ce50 5f 52 65 63 54 72 69 67 67 65 72 73 20 20 20 20  _RecTriggers    
5ce60 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 45  0x00200000  /* E
5ce70 6e 61 62 6c 65 20 72 65 63 75 72 73 69 76 65 20  nable recursive 
5ce80 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 64 65 66  triggers */.#def
5ce90 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f 72 65 69  ine SQLITE_Forei
5cea0 67 6e 4b 65 79 73 20 20 20 20 30 78 30 30 34 30  gnKeys    0x0040
5ceb0 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f 72 63 65  0000  /* Enforce
5cec0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
5ced0 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a 0a 2f 2a  straints  */../*
5cee0 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c  .** Possible val
5cef0 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ues for the sqli
5cf00 74 65 2e 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a  te.magic field..
5cf10 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 73 20 61  ** The numbers a
5cf20 72 65 20 6f 62 74 61 69 6e 65 64 20 61 74 20 72  re obtained at r
5cf30 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e  andom and have n
5cf40 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e  o special meanin
5cf50 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e  g, other.** than
5cf60 20 62 65 69 6e 67 20 64 69 73 74 69 6e 63 74 20   being distinct 
5cf70 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72  from one another
5cf80 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5cf90 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20  ITE_MAGIC_OPEN  
5cfa0 20 20 20 30 78 61 30 32 39 61 36 39 37 20 20 2f     0xa029a697  /
5cfb0 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 6f 70  * Database is op
5cfc0 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  en */.#define SQ
5cfd0 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45  LITE_MAGIC_CLOSE
5cfe0 44 20 20 20 30 78 39 66 33 63 32 64 33 33 20 20  D   0x9f3c2d33  
5cff0 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 63  /* Database is c
5d000 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  losed */.#define
5d010 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49   SQLITE_MAGIC_SI
5d020 43 4b 20 20 20 20 20 30 78 34 62 37 37 31 32 39  CK     0x4b77129
5d030 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20  0  /* Error and 
5d040 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a  awaiting close *
5d050 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5d060 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20 20 20  _MAGIC_BUSY     
5d070 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a 20 44  0xf03b7906  /* D
5d080 61 74 61 62 61 73 65 20 63 75 72 72 65 6e 74 6c  atabase currentl
5d090 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66  y in use */.#def
5d0a0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  ine SQLITE_MAGIC
5d0b0 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35 33 35  _ERROR    0xb535
5d0c0 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49  7930  /* An SQLI
5d0d0 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 20  TE_MISUSE error 
5d0e0 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a  occurred */../*.
5d0f0 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75 6e 63  ** Each SQL func
5d100 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 64 20  tion is defined 
5d110 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
5d120 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  f the following.
5d130 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41  ** structure.  A
5d140 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
5d150 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 74   structure is st
5d160 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69  ored in the sqli
5d170 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68  te.aFunc.** hash
5d180 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75   table.  When mu
5d190 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73  ltiple functions
5d1a0 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e   have the same n
5d1b0 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20 74 61  ame, the hash ta
5d1c0 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f  ble.** points to
5d1d0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
5d1e0 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
5d1f0 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75  es..*/.struct Fu
5d200 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20 6e 41  ncDef {.  i16 nA
5d210 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  rg;            /
5d220 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * Number of argu
5d230 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73  ments.  -1 means
5d240 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20   unlimited */.  
5d250 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20 20 20  u8 iPrefEnc;    
5d260 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72 72 65       /* Preferre
5d270 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  d text encoding 
5d280 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 31 36  (SQLITE_UTF8, 16
5d290 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20 20 75  LE, 16BE) */.  u
5d2a0 38 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  8 flags;        
5d2b0 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62      /* Some comb
5d2c0 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54  ination of SQLIT
5d2d0 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f  E_FUNC_* */.  vo
5d2e0 69 64 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20  id *pUserData;  
5d2f0 20 20 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20     /* User data 
5d300 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46  parameter */.  F
5d310 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20  uncDef *pNext;  
5d320 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63      /* Next func
5d330 74 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e  tion with same n
5d340 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ame */.  void (*
5d350 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
5d360 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
5d370 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a  te3_value**); /*
5d380 20 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f   Regular functio
5d390 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53  n */.  void (*xS
5d3a0 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
5d3b0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
5d3c0 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41  3_value**); /* A
5d3d0 67 67 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f  ggregate step */
5d3e0 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  .  void (*xFinal
5d3f0 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ize)(sqlite3_con
5d400 74 65 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20  text*);         
5d410 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67         /* Aggreg
5d420 61 74 65 20 66 69 6e 61 6c 69 7a 65 72 20 2a 2f  ate finalizer */
5d430 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
5d440 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e          /* SQL n
5d450 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ame of the funct
5d460 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 65  ion. */.  FuncDe
5d470 66 20 2a 70 48 61 73 68 3b 20 20 20 20 20 20 2f  f *pHash;      /
5d480 2a 20 4e 65 78 74 20 77 69 74 68 20 61 20 64 69  * Next with a di
5d490 66 66 65 72 65 6e 74 20 6e 61 6d 65 20 62 75 74  fferent name but
5d4a0 20 74 68 65 20 73 61 6d 65 20 68 61 73 68 20 2a   the same hash *
5d4b0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73  /.};../*.** Poss
5d4c0 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20  ible values for 
5d4d0 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 0a 2a 2f  FuncDef.flags.*/
5d4e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5d4f0 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20 30 78  FUNC_LIKE     0x
5d500 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20  01 /* Candidate 
5d510 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 74  for the LIKE opt
5d520 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 65  imization */.#de
5d530 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43  fine SQLITE_FUNC
5d540 5f 43 41 53 45 20 20 20 20 20 30 78 30 32 20 2f  _CASE     0x02 /
5d550 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69 76 65  * Case-sensitive
5d560 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e 63 74   LIKE-type funct
5d570 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ion */.#define S
5d580 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d  QLITE_FUNC_EPHEM
5d590 20 20 20 20 30 78 30 34 20 2f 2a 20 45 70 68 65      0x04 /* Ephe
5d5a0 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65 20 77  meral.  Delete w
5d5b0 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 64 65 66  ith VDBE */.#def
5d5c0 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  ine SQLITE_FUNC_
5d5d0 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 20 2f 2a  NEEDCOLL 0x08 /*
5d5e0 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43   sqlite3GetFuncC
5d5f0 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 20 62  ollSeq() might b
5d600 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 64 65 66  e called */.#def
5d610 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  ine SQLITE_FUNC_
5d620 50 52 49 56 41 54 45 20 20 30 78 31 30 20 2f 2a  PRIVATE  0x10 /*
5d630 20 41 6c 6c 6f 77 65 64 20 66 6f 72 20 69 6e 74   Allowed for int
5d640 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a  ernal use only *
5d650 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5d660 5f 46 55 4e 43 5f 43 4f 55 4e 54 20 20 20 20 30  _FUNC_COUNT    0
5d670 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20  x20 /* Built-in 
5d680 63 6f 75 6e 74 28 2a 29 20 61 67 67 72 65 67 61  count(*) aggrega
5d690 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  te */.#define SQ
5d6a0 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53  LITE_FUNC_COALES
5d6b0 43 45 20 30 78 34 30 20 2f 2a 20 42 75 69 6c 74  CE 0x40 /* Built
5d6c0 2d 69 6e 20 63 6f 61 6c 65 73 63 65 28 29 20 6f  -in coalesce() o
5d6d0 72 20 69 66 6e 75 6c 6c 28 29 20 66 75 6e 63 74  r ifnull() funct
5d6e0 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ion */../*.** Th
5d6f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65  e following thre
5d700 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43 54 49  e macros, FUNCTI
5d710 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43 28 29  ON(), LIKEFUNC()
5d720 20 61 6e 64 20 41 47 47 52 45 47 41 54 45 28 29   and AGGREGATE()
5d730 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20   are.** used to 
5d740 63 72 65 61 74 65 20 74 68 65 20 69 6e 69 74 69  create the initi
5d750 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68 65 20  alizers for the 
5d760 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
5d770 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e 43  es..**.**   FUNC
5d780 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67  TION(zName, nArg
5d790 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75  , iArg, bNC, xFu
5d7a0 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20  nc).**     Used 
5d7b0 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61 6c  to create a scal
5d7c0 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  ar function defi
5d7d0 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63  nition of a func
5d7e0 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20  tion zName .**  
5d7f0 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62     implemented b
5d800 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 46 75  y C function xFu
5d810 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 73 20  nc that accepts 
5d820 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 2e 20  nArg arguments. 
5d830 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c 75 65  The.**     value
5d840 20 70 61 73 73 65 64 20 61 73 20 69 41 72 67 20   passed as iArg 
5d850 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f  is cast to a (vo
5d860 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20 61 76  id*) and made av
5d870 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 20 61  ailable.**     a
5d880 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 61 20  s the user-data 
5d890 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61  (sqlite3_user_da
5d8a0 74 61 28 29 29 20 66 6f 72 20 74 68 65 20 66 75  ta()) for the fu
5d8b0 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20 20  nction. If .**  
5d8c0 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e 43 20     argument bNC 
5d8d0 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
5d8e0 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  e SQLITE_FUNC_NE
5d8f0 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 73 20 73  EDCOLL flag is s
5d900 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 52  et..**.**   AGGR
5d910 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72  EGATE(zName, nAr
5d920 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 53  g, iArg, bNC, xS
5d930 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a 20  tep, xFinal).** 
5d940 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 61      Used to crea
5d950 74 65 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  te an aggregate 
5d960 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
5d970 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  ion implemented 
5d980 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20 43 20  by.**     the C 
5d990 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65 70 20  functions xStep 
5d9a0 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68 65 20  and xFinal. The 
5d9b0 66 69 72 73 74 20 66 6f 75 72 20 70 61 72 61 6d  first four param
5d9c0 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 72 65  eters.**     are
5d9d0 20 69 6e 74 65 72 70 72 65 74 65 64 20 69 6e 20   interpreted in 
5d9e0 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 73 20  the same way as 
5d9f0 74 68 65 20 66 69 72 73 74 20 34 20 70 61 72 61  the first 4 para
5da00 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20  meters to.**    
5da10 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a 0a   FUNCTION()..**.
5da20 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28 7a 4e  **   LIKEFUNC(zN
5da30 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c  ame, nArg, pArg,
5da40 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20 20 55   flags).**     U
5da50 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
5da60 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
5da70 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20  definition of a 
5da80 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a  function zName .
5da90 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63 63 65  **     that acce
5daa0 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e  pts nArg argumen
5dab0 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d  ts and is implem
5dac0 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c 6c 20  ented by a call 
5dad0 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66 75 6e  to C .**     fun
5dae0 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20  ction likeFunc. 
5daf0 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20 69 73  Argument pArg is
5db00 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64   cast to a (void
5db10 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a 2a 20   *) and made.** 
5db20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 61 73      available as
5db30 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73   the function us
5db40 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33  er-data (sqlite3
5db50 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e 20 54  _user_data()). T
5db60 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44 65  he.**     FuncDe
5db70 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c 65  f.flags variable
5db80 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76   is set to the v
5db90 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74  alue passed as t
5dba0 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20 20 20  he flags.**     
5dbb0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 64  parameter..*/.#d
5dbc0 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e 28 7a  efine FUNCTION(z
5dbd0 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67  Name, nArg, iArg
5dbe0 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a  , bNC, xFunc) \.
5dbf0 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f    {nArg, SQLITE_
5dc00 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45  UTF8, bNC*SQLITE
5dc10 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20  _FUNC_NEEDCOLL, 
5dc20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f  \.   SQLITE_INT_
5dc30 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c  TO_PTR(iArg), 0,
5dc40 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a   xFunc, 0, 0, #z
5dc50 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65  Name, 0}.#define
5dc60 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e   STR_FUNCTION(zN
5dc70 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c  ame, nArg, pArg,
5dc80 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20   bNC, xFunc) \. 
5dc90 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55   {nArg, SQLITE_U
5dca0 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f  TF8, bNC*SQLITE_
5dcb0 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c  FUNC_NEEDCOLL, \
5dcc0 0a 20 20 20 70 41 72 67 2c 20 30 2c 20 78 46 75  .   pArg, 0, xFu
5dcd0 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65  nc, 0, 0, #zName
5dce0 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 4c 49 4b  , 0}.#define LIK
5dcf0 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72  EFUNC(zName, nAr
5dd00 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29 20 5c  g, arg, flags) \
5dd10 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45  .  {nArg, SQLITE
5dd20 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20 28 76  _UTF8, flags, (v
5dd30 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20 6c 69  oid *)arg, 0, li
5dd40 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a  keFunc, 0, 0, #z
5dd50 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65  Name, 0}.#define
5dd60 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d 65   AGGREGATE(zName
5dd70 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 6e 63 2c  , nArg, arg, nc,
5dd80 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 20   xStep, xFinal) 
5dd90 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54  \.  {nArg, SQLIT
5dda0 45 5f 55 54 46 38 2c 20 6e 63 2a 53 51 4c 49 54  E_UTF8, nc*SQLIT
5ddb0 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c  E_FUNC_NEEDCOLL,
5ddc0 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54   \.   SQLITE_INT
5ddd0 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c  _TO_PTR(arg), 0,
5dde0 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c   0, xStep,xFinal
5ddf0 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a  ,#zName,0}../*.*
5de00 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 61  * All current sa
5de10 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 74 6f  vepoints are sto
5de20 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20  red in a linked 
5de30 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74  list starting at
5de40 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 76  .** sqlite3.pSav
5de50 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 73  epoint. The firs
5de60 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65  t element in the
5de70 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d 6f 73   list is the mos
5de80 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70  t recently.** op
5de90 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20  ened savepoint. 
5dea0 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61  Savepoints are a
5deb0 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74  dded to the list
5dec0 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a 2a 20   by the vdbe.** 
5ded0 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73  OP_Savepoint ins
5dee0 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72  truction..*/.str
5def0 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a  uct Savepoint {.
5df00 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
5df10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5df20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70 6f 69        /* Savepoi
5df30 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72  nt name (nul-ter
5df40 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 69 36  minated) */.  i6
5df50 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b  4 nDeferredCons;
5df60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5df70 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64    /* Number of d
5df80 65 66 65 72 72 65 64 20 66 6b 20 76 69 6f 6c 61  eferred fk viola
5df90 74 69 6f 6e 73 20 2a 2f 0a 20 20 53 61 76 65 70  tions */.  Savep
5dfa0 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20 20 20  oint *pNext;    
5dfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5dfc0 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70 6f 69  * Parent savepoi
5dfd0 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a 7d  nt (if any) */.}
5dfe0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
5dff0 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65 64 20  lowing are used 
5e000 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  as the second pa
5e010 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
5e020 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c 0a 2a  e3Savepoint(),.*
5e030 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50 31 20  * and as the P1 
5e040 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
5e050 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73  OP_Savepoint ins
5e060 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65  truction..*/.#de
5e070 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 42  fine SAVEPOINT_B
5e080 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64 65 66  EGIN      0.#def
5e090 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 45  ine SAVEPOINT_RE
5e0a0 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65 66 69  LEASE    1.#defi
5e0b0 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  ne SAVEPOINT_ROL
5e0c0 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a 2a  LBACK   2.../*.*
5e0d0 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f  * Each SQLite mo
5e0e0 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61  dule (virtual ta
5e0f0 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20  ble definition) 
5e100 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e  is defined by an
5e110 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
5e120 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
5e130 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20  ructure, stored 
5e140 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61  in the sqlite3.a
5e150 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74  Module.** hash t
5e160 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  able..*/.struct 
5e170 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74  Module {.  const
5e180 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
5e190 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20  *pModule;       
5e1a0 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e  /* Callback poin
5e1b0 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ters */.  const 
5e1c0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
5e1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5e1e0 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f  * Name passed to
5e1f0 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29   create_module()
5e200 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78   */.  void *pAux
5e210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5e220 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41             /* pA
5e230 75 78 20 70 61 73 73 65 64 20 74 6f 20 63 72 65  ux passed to cre
5e240 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a  ate_module() */.
5e250 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f    void (*xDestro
5e260 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20  y)(void *);     
5e270 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65         /* Module
5e280 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
5e290 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  tion */.};../*.*
5e2a0 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * information ab
5e2b0 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  out each column 
5e2c0 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20  of an SQL table 
5e2d0 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e  is held in an in
5e2e0 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69  stance.** of thi
5e2f0 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  s structure..*/.
5e300 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a  struct Column {.
5e310 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
5e320 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
5e330 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45  is column */.  E
5e340 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20  xpr *pDflt;     
5e350 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65  /* Default value
5e360 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20   of this column 
5e370 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 66 6c 74  */.  char *zDflt
5e380 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61  ;     /* Origina
5e390 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 64 65  l text of the de
5e3a0 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a 20  fault value */. 
5e3b0 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20   char *zType;   
5e3c0 20 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 66    /* Data type f
5e3d0 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a  or this column *
5e3e0 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b  /.  char *zColl;
5e3f0 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e       /* Collatin
5e400 67 20 73 65 71 75 65 6e 63 65 2e 20 20 49 66 20  g sequence.  If 
5e410 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 20 64 65  NULL, use the de
5e420 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6e 6f  fault */.  u8 no
5e430 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 54  tNull;      /* T
5e440 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20  rue if there is 
5e450 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74  a NOT NULL const
5e460 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 20 69 73  raint */.  u8 is
5e470 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f 2a 20 54  PrimKey;    /* T
5e480 72 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c 75  rue if this colu
5e490 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  mn is part of th
5e4a0 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f  e PRIMARY KEY */
5e4b0 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79  .  char affinity
5e4c0 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68  ;   /* One of th
5e4d0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 2e 2e 2e  e SQLITE_AFF_...
5e4e0 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 66 6e 64   values */.#ifnd
5e4f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
5e500 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 38  IRTUALTABLE.  u8
5e510 20 69 73 48 69 64 64 65 6e 3b 20 20 20 20 20 2f   isHidden;     /
5e520 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63  * True if this c
5e530 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 64 65 6e  olumn is 'hidden
5e540 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  ' */.#endif.};..
5e550 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 69  /*.** A "Collati
5e560 6e 67 20 53 65 71 75 65 6e 63 65 22 20 69 73 20  ng Sequence" is 
5e570 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e  defined by an in
5e580 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
5e590 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
5e5a0 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75 61 6c  ture. Conceptual
5e5b0 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20  ly, a collating 
5e5c0 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69 73 74  sequence consist
5e5d0 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64 0a  s of a name and.
5e5e0 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  ** a comparison 
5e5f0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 65 66  routine that def
5e600 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72 20 6f  ines the order o
5e610 66 20 74 68 61 74 20 73 65 71 75 65 6e 63 65 2e  f that sequence.
5e620 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79  .**.** There may
5e630 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 69 6d   two separate im
5e640 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
5e650 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
5e660 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a 20  unction, one.** 
5e670 74 68 61 74 20 70 72 6f 63 65 73 73 65 73 20 74  that processes t
5e680 65 78 74 20 69 6e 20 55 54 46 2d 38 20 65 6e 63  ext in UTF-8 enc
5e690 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 71 2e 78  oding (CollSeq.x
5e6a0 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 68 65 72  Cmp) and another
5e6b0 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 65 73 73   that.** process
5e6c0 65 73 20 74 65 78 74 20 65 6e 63 6f 64 65 64 20  es text encoded 
5e6d0 69 6e 20 55 54 46 2d 31 36 20 28 43 6f 6c 6c 53  in UTF-16 (CollS
5e6e0 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 73 69 6e  eq.xCmp16), usin
5e6f0 67 20 74 68 65 20 6d 61 63 68 69 6e 65 0a 2a 2a  g the machine.**
5e700 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
5e710 65 72 2e 20 57 68 65 6e 20 61 20 63 6f 6c 6c 61  er. When a colla
5e720 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73  tion sequence is
5e730 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 74 65   invoked, SQLite
5e740 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 68 65 20   selects.** the 
5e750 76 65 72 73 69 6f 6e 20 74 68 61 74 20 77 69 6c  version that wil
5e760 6c 20 72 65 71 75 69 72 65 20 74 68 65 20 6c 65  l require the le
5e770 61 73 74 20 65 78 70 65 6e 73 69 76 65 20 65 6e  ast expensive en
5e780 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 6e 73 6c  coding.** transl
5e790 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e 79 2e 0a  ations, if any..
5e7a0 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c 53 65  **.** The CollSe
5e7b0 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 72 20 76  q.pUser member v
5e7c0 61 72 69 61 62 6c 65 20 69 73 20 61 6e 20 65 78  ariable is an ex
5e7d0 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 74 68  tra parameter th
5e7e0 61 74 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a 20  at passed in.** 
5e7f0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
5e800 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 55 54 46  ument to the UTF
5e810 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75  -8 comparison fu
5e820 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a 2a  nction, xCmp..**
5e830 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 31 36   CollSeq.pUser16
5e840 20 69 73 20 74 68 65 20 65 71 75 69 76 61 6c 65   is the equivale
5e850 6e 74 20 66 6f 72 20 74 68 65 20 55 54 46 2d 31  nt for the UTF-1
5e860 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  6 comparison fun
5e870 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31 36  ction,.** xCmp16
5e880 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 20  ..**.** If both 
5e890 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e 64  CollSeq.xCmp and
5e8a0 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 20   CollSeq.xCmp16 
5e8b0 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61  are NULL, it mea
5e8c0 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63  ns that the.** c
5e8d0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
5e8e0 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20  e is undefined. 
5e8f0 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74 20 6f   Indices built o
5e900 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a  n an undefined.*
5e910 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * collating sequ
5e920 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62 65 20  ence may not be 
5e930 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e  read or written.
5e940 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 6c 53  .*/.struct CollS
5e950 65 71 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  eq {.  char *zNa
5e960 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  me;          /* 
5e970 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c  Name of the coll
5e980 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2c 20  ating sequence, 
5e990 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f  UTF-8 encoded */
5e9a0 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20  .  u8 enc;      
5e9b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74           /* Text
5e9c0 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e 64 6c 65   encoding handle
5e9d0 64 20 62 79 20 78 43 6d 70 28 29 20 2a 2f 0a 20  d by xCmp() */. 
5e9e0 20 75 38 20 74 79 70 65 3b 20 20 20 20 20 20 20   u8 type;       
5e9f0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
5ea00 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c   the SQLITE_COLL
5ea10 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 65 6c 6f  _... values belo
5ea20 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73  w */.  void *pUs
5ea30 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er;          /* 
5ea40 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  First argument t
5ea50 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 69 6e  o xCmp() */.  in
5ea60 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 64 2a 2c  t (*xCmp)(void*,
5ea70 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  int, const void*
5ea80 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69  , int, const voi
5ea90 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44  d*);.  void (*xD
5eaa0 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20  el)(void*);  /* 
5eab0 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 70  Destructor for p
5eac0 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  User */.};../*.*
5ead0 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
5eae0 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 79 70 65   of CollSeq.type
5eaf0 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  :.*/.#define SQL
5eb00 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20  ITE_COLL_BINARY 
5eb10 20 31 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75   1  /* The defau
5eb20 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 6f 6c 6c  lt memcmp() coll
5eb30 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a  ating sequence *
5eb40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5eb50 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 20 32 20  _COLL_NOCASE  2 
5eb60 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e   /* The built-in
5eb70 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 74 69 6e   NOCASE collatin
5eb80 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64  g sequence */.#d
5eb90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c  efine SQLITE_COL
5eba0 4c 5f 52 45 56 45 52 53 45 20 33 20 20 2f 2a 20  L_REVERSE 3  /* 
5ebb0 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 52 45 56  The built-in REV
5ebc0 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73  ERSE collating s
5ebd0 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69  equence */.#defi
5ebe0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55  ne SQLITE_COLL_U
5ebf0 53 45 52 20 20 20 20 30 20 20 2f 2a 20 41 6e 79  SER    0  /* Any
5ec00 20 6f 74 68 65 72 20 75 73 65 72 2d 64 65 66 69   other user-defi
5ec10 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ned collating se
5ec20 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  quence */../*.**
5ec30 20 41 20 73 6f 72 74 20 6f 72 64 65 72 20 63 61   A sort order ca
5ec40 6e 20 62 65 20 65 69 74 68 65 72 20 41 53 43 20  n be either ASC 
5ec50 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 64 65 66  or DESC..*/.#def
5ec60 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53  ine SQLITE_SO_AS
5ec70 43 20 20 20 20 20 20 20 30 20 20 2f 2a 20 53 6f  C       0  /* So
5ec80 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  rt in ascending 
5ec90 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  order */.#define
5eca0 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20   SQLITE_SO_DESC 
5ecb0 20 20 20 20 20 31 20 20 2f 2a 20 53 6f 72 74 20       1  /* Sort 
5ecc0 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64  in ascending ord
5ecd0 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c  er */../*.** Col
5ece0 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 79 70  umn affinity typ
5ecf0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  es..**.** These 
5ed00 75 73 65 64 20 74 6f 20 68 61 76 65 20 6d 6e 65  used to have mne
5ed10 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 6b 65 20  monic name like 
5ed20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41  'i' for SQLITE_A
5ed30 46 46 5f 49 4e 54 45 47 45 52 20 61 6e 64 0a 2a  FF_INTEGER and.*
5ed40 2a 20 27 74 27 20 66 6f 72 20 53 51 4c 49 54 45  * 't' for SQLITE
5ed50 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 75 74 20  _AFF_TEXT.  But 
5ed60 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 6c 69  we can save a li
5ed70 74 74 6c 65 20 73 70 61 63 65 20 61 6e 64 20 69  ttle space and i
5ed80 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 20 73 70  mprove.** the sp
5ed90 65 65 64 20 61 20 6c 69 74 74 6c 65 20 62 79 20  eed a little by 
5eda0 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 20 76 61  numbering the va
5edb0 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 69 76 65  lues consecutive
5edc0 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20  ly.  .**.** But 
5edd0 72 61 74 68 65 72 20 74 68 61 6e 20 73 74 61 72  rather than star
5ede0 74 20 77 69 74 68 20 30 20 6f 72 20 31 2c 20 77  t with 0 or 1, w
5edf0 65 20 62 65 67 69 6e 20 77 69 74 68 20 27 61 27  e begin with 'a'
5ee00 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20  .  That way,.** 
5ee10 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 61 66  when multiple af
5ee20 66 69 6e 69 74 79 20 74 79 70 65 73 20 61 72 65  finity types are
5ee30 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 69 6e   concatenated in
5ee40 74 6f 20 61 20 73 74 72 69 6e 67 20 61 6e 64 0a  to a string and.
5ee50 2a 2a 20 75 73 65 64 20 61 73 20 74 68 65 20 50  ** used as the P
5ee60 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 65 79 20  4 operand, they 
5ee70 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 72 65 61  will be more rea
5ee80 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  dable..**.** Not
5ee90 65 20 61 6c 73 6f 20 74 68 61 74 20 74 68 65 20  e also that the 
5eea0 6e 75 6d 65 72 69 63 20 74 79 70 65 73 20 61 72  numeric types ar
5eeb0 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74 68  e grouped togeth
5eec0 65 72 20 73 6f 20 74 68 61 74 20 74 65 73 74 69  er so that testi
5eed0 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e 75 6d 65  ng.** for a nume
5eee0 72 69 63 20 74 79 70 65 20 69 73 20 61 20 73 69  ric type is a si
5eef0 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e  ngle comparison.
5ef00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
5ef10 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 20  TE_AFF_TEXT     
5ef20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  'a'.#define SQLI
5ef30 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 20  TE_AFF_NONE     
5ef40 27 62 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  'b'.#define SQLI
5ef50 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 20  TE_AFF_NUMERIC  
5ef60 27 63 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  'c'.#define SQLI
5ef70 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 20  TE_AFF_INTEGER  
5ef80 27 64 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  'd'.#define SQLI
5ef90 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 20  TE_AFF_REAL     
5efa0 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 73 71 6c  'e'..#define sql
5efb0 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66  ite3IsNumericAff
5efc0 69 6e 69 74 79 28 58 29 20 20 28 28 58 29 3e 3d  inity(X)  ((X)>=
5efd0 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
5efe0 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53  IC)../*.** The S
5eff0 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 76  QLITE_AFF_MASK v
5f000 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f 66 66 20  alues masks off 
5f010 74 68 65 20 73 69 67 6e 69 66 69 63 61 6e 74 20  the significant 
5f020 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a 20 61 66  bits of an.** af
5f030 66 69 6e 69 74 79 20 76 61 6c 75 65 2e 20 0a 2a  finity value. .*
5f040 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5f050 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 20 30 78  _AFF_MASK     0x
5f060 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 69 74 69  67../*.** Additi
5f070 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 65 73 20  onal bit values 
5f080 74 68 61 74 20 63 61 6e 20 62 65 20 4f 52 65 64  that can be ORed
5f090 20 77 69 74 68 20 61 6e 20 61 66 66 69 6e 69 74   with an affinit
5f0a0 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 63 68 61  y without.** cha
5f0b0 6e 67 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69  nging the affini
5f0c0 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ty..*/.#define S
5f0d0 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
5f0e0 20 20 20 30 78 30 38 20 20 2f 2a 20 6a 75 6d 70     0x08  /* jump
5f0f0 73 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 72  s if either oper
5f100 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23  and is NULL */.#
5f110 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
5f120 4f 52 45 50 32 20 20 20 20 20 20 30 78 31 30 20  OREP2      0x10 
5f130 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
5f140 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 61 74 68   in reg[P2] rath
5f150 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a  er than jump */.
5f160 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
5f170 55 4c 4c 45 51 20 20 20 20 20 20 20 30 78 38 30  ULLEQ       0x80
5f180 20 20 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a    /* NULL=NULL *
5f190 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65  /../*.** An obje
5f1a0 63 74 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ct of this type 
5f1b0 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 65  is created for e
5f1c0 61 63 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ach virtual tabl
5f1d0 65 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20  e present in.** 
5f1e0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
5f1f0 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ema. .**.** If t
5f200 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
5f210 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 74 68  ma is shared, th
5f220 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20  en there is one 
5f230 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
5f240 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 66 6f  .** structure fo
5f250 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20  r each database 
5f260 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 73 71 6c 69  connection (sqli
5f270 74 65 33 2a 29 20 74 68 61 74 20 75 73 65 73 20  te3*) that uses 
5f280 74 68 65 20 73 68 61 72 65 64 0a 2a 2a 20 73 63  the shared.** sc
5f290 68 65 6d 61 2e 20 54 68 69 73 20 69 73 20 62 65  hema. This is be
5f2a0 63 61 75 73 65 20 65 61 63 68 20 64 61 74 61 62  cause each datab
5f2b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 72  ase connection r
5f2c0 65 71 75 69 72 65 73 20 69 74 73 20 6f 77 6e 20  equires its own 
5f2d0 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 73 74 61 6e  unique.** instan
5f2e0 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ce of the sqlite
5f2f0 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 75  3_vtab* handle u
5f300 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
5f310 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
5f320 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
5f330 6f 6e 2e 20 73 71 6c 69 74 65 33 5f 76 74 61 62  on. sqlite3_vtab
5f340 2a 20 68 61 6e 64 6c 65 73 20 63 61 6e 20 6e 6f  * handles can no
5f350 74 20 62 65 20 73 68 61 72 65 64 20 62 65 74 77  t be shared betw
5f360 65 65 6e 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  een .** database
5f370 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 65 76   connections, ev
5f380 65 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 74  en when the rest
5f390 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   of the in-memor
5f3a0 79 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 73  y database .** s
5f3b0 63 68 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c  chema is shared,
5f3c0 20 61 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   as the implemen
5f3d0 74 61 74 69 6f 6e 20 6f 66 74 65 6e 20 73 74 6f  tation often sto
5f3e0 72 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65  res the database
5f3f0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  .** connection h
5f400 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 6f 20  andle passed to 
5f410 69 74 20 76 69 61 20 74 68 65 20 78 43 6f 6e 6e  it via the xConn
5f420 65 63 74 28 29 20 6f 72 20 78 43 72 65 61 74 65  ect() or xCreate
5f430 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 64 75 72  () method.** dur
5f440 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ing initializati
5f450 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 54  on internally. T
5f460 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e  his database con
5f470 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 6d  nection handle m
5f480 61 79 0a 2a 2a 20 74 68 65 6e 20 75 73 65 64 20  ay.** then used 
5f490 62 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  by the virtual t
5f4a0 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
5f4b0 69 6f 6e 20 74 6f 20 61 63 63 65 73 73 20 72 65  ion to access re
5f4c0 61 6c 20 74 61 62 6c 65 73 20 0a 2a 2a 20 77 69  al tables .** wi
5f4d0 74 68 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  thin the databas
5f4e0 65 2e 20 53 6f 20 74 68 61 74 20 74 68 65 79 20  e. So that they 
5f4f0 61 70 70 65 61 72 20 61 73 20 70 61 72 74 20 6f  appear as part o
5f500 66 20 74 68 65 20 63 61 6c 6c 65 72 73 20 0a 2a  f the callers .*
5f510 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  * transaction, t
5f520 68 65 73 65 20 61 63 63 65 73 73 65 73 20 6e 65  hese accesses ne
5f530 65 64 20 74 6f 20 62 65 20 6d 61 64 65 20 76 69  ed to be made vi
5f540 61 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  a the same datab
5f550 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  ase .** connecti
5f560 6f 6e 20 61 73 20 74 68 61 74 20 75 73 65 64 20  on as that used 
5f570 74 6f 20 65 78 65 63 75 74 65 20 53 51 4c 20 6f  to execute SQL o
5f580 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65  perations on the
5f590 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
5f5a0 2a 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 6c 65  **.** All VTable
5f5b0 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 63 6f   objects that co
5f5c0 72 72 65 73 70 6f 6e 64 20 74 6f 20 61 20 73 69  rrespond to a si
5f5d0 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 61 20  ngle table in a 
5f5e0 73 68 61 72 65 64 0a 2a 2a 20 64 61 74 61 62 61  shared.** databa
5f5f0 73 65 20 73 63 68 65 6d 61 20 61 72 65 20 69 6e  se schema are in
5f600 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 64 20 69  itially stored i
5f610 6e 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20  n a linked-list 
5f620 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a  pointed to by.**
5f630 20 74 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62   the Table.pVTab
5f640 6c 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62  le member variab
5f650 6c 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73  le of the corres
5f660 70 6f 6e 64 69 6e 67 20 54 61 62 6c 65 20 6f 62  ponding Table ob
5f670 6a 65 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 6e  ject..** When an
5f680 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
5f690 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  () operation is 
5f6a0 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 63 65  required to acce
5f6b0 73 73 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  ss the virtual.*
5f6c0 2a 20 74 61 62 6c 65 2c 20 69 74 20 73 65 61 72  * table, it sear
5f6d0 63 68 65 73 20 74 68 65 20 6c 69 73 74 20 66 6f  ches the list fo
5f6e0 72 20 74 68 65 20 56 54 61 62 6c 65 20 74 68 61  r the VTable tha
5f6f0 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
5f700 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
5f710 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 6e   connection doin
5f720 67 20 74 68 65 20 70 72 65 70 61 72 69 6e 67 20  g the preparing 
5f730 73 6f 20 61 73 20 74 6f 20 75 73 65 20 74 68 65  so as to use the
5f740 20 63 6f 72 72 65 63 74 0a 2a 2a 20 73 71 6c 69   correct.** sqli
5f750 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65  te3_vtab* handle
5f760 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64   in the compiled
5f770 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68   query..**.** Wh
5f780 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  en an in-memory 
5f790 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 69 73 20  Table object is 
5f7a0 64 65 6c 65 74 65 64 20 28 66 6f 72 20 65 78 61  deleted (for exa
5f7b0 6d 70 6c 65 20 77 68 65 6e 20 74 68 65 0a 2a 2a  mple when the.**
5f7c0 20 73 63 68 65 6d 61 20 69 73 20 62 65 69 6e 67   schema is being
5f7d0 20 72 65 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f   reloaded for so
5f7e0 6d 65 20 72 65 61 73 6f 6e 29 2c 20 74 68 65 20  me reason), the 
5f7f0 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61  VTable objects a
5f800 72 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 74  re not .** delet
5f810 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74  ed and the sqlit
5f820 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 73  e3_vtab* handles
5f830 20 61 72 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e   are not xDiscon
5f840 6e 65 63 74 28 29 65 64 20 0a 2a 2a 20 69 6d 6d  nect()ed .** imm
5f850 65 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 61  ediately. Instea
5f860 64 2c 20 74 68 65 79 20 61 72 65 20 6d 6f 76 65  d, they are move
5f870 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65  d from the Table
5f880 2e 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74 6f  .pVTable list to
5f890 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e 6b  .** another link
5f8a0 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62  ed list headed b
5f8b0 79 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44  y the sqlite3.pD
5f8c0 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 72  isconnect member
5f8d0 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65   of the.** corre
5f8e0 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33  sponding sqlite3
5f8f0 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 79   structure. They
5f900 20 61 72 65 20 74 68 65 6e 20 64 65 6c 65 74 65   are then delete
5f910 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65 64 20  d/xDisconnected 
5f920 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65 20 61 20  .** next time a 
5f930 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 72 65  statement is pre
5f940 70 61 72 65 64 20 75 73 69 6e 67 20 73 61 69 64  pared using said
5f950 20 73 71 6c 69 74 65 33 2a 2e 20 54 68 69 73 20   sqlite3*. This 
5f960 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 76  is done.** to av
5f970 6f 69 64 20 64 65 61 64 6c 6f 63 6b 20 69 73 73  oid deadlock iss
5f980 75 65 73 20 69 6e 76 6f 6c 76 69 6e 67 20 6d 75  ues involving mu
5f990 6c 74 69 70 6c 65 20 73 71 6c 69 74 65 33 2e 6d  ltiple sqlite3.m
5f9a0 75 74 65 78 20 6d 75 74 65 78 65 73 2e 0a 2a 2a  utex mutexes..**
5f9b0 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e   Refer to commen
5f9c0 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f  ts above functio
5f9d0 6e 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c  n sqlite3VtabUnl
5f9e0 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72 20 61 6e  ockList() for an
5f9f0 0a 2a 2a 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20  .** explanation 
5fa00 61 73 20 74 6f 20 77 68 79 20 69 74 20 69 73 20  as to why it is 
5fa10 73 61 66 65 20 74 6f 20 61 64 64 20 61 6e 20 65  safe to add an e
5fa20 6e 74 72 79 20 74 6f 20 61 6e 20 73 71 6c 69 74  ntry to an sqlit
5fa30 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 0a 2a  e3.pDisconnect.*
5fa40 2a 20 6c 69 73 74 20 77 69 74 68 6f 75 74 20 68  * list without h
5fa50 6f 6c 64 69 6e 67 20 74 68 65 20 63 6f 72 72 65  olding the corre
5fa60 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33  sponding sqlite3
5fa70 2e 6d 75 74 65 78 20 6d 75 74 65 78 2e 0a 2a 2a  .mutex mutex..**
5fa80 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 66  .** The memory f
5fa90 6f 72 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 68  or objects of th
5faa0 69 73 20 74 79 70 65 20 69 73 20 61 6c 77 61 79  is type is alway
5fab0 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a  s allocated by .
5fac0 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  ** sqlite3DbMall
5fad0 6f 63 28 29 2c 20 75 73 69 6e 67 20 74 68 65 20  oc(), using the 
5fae0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
5faf0 65 20 73 74 6f 72 65 64 20 69 6e 20 56 54 61 62  e stored in VTab
5fb00 6c 65 2e 64 62 20 61 73 20 0a 2a 2a 20 74 68 65  le.db as .** the
5fb10 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
5fb20 0a 2a 2f 0a 73 74 72 75 63 74 20 56 54 61 62 6c  .*/.struct VTabl
5fb30 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e {.  sqlite3 *d
5fb40 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
5fb50 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
5fb60 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65  ection associate
5fb70 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c  d with this tabl
5fb80 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70  e */.  Module *p
5fb90 4d 6f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mod;            
5fba0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d   /* Pointer to m
5fbb0 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
5fbc0 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tion */.  sqlite
5fbd0 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20  3_vtab *pVtab;  
5fbe0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
5fbf0 6f 20 76 74 61 62 20 69 6e 73 74 61 6e 63 65 20  o vtab instance 
5fc00 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
5fc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5fc20 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e  * Number of poin
5fc30 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72  ters to this str
5fc40 75 63 74 75 72 65 20 2a 2f 0a 20 20 56 54 61 62  ucture */.  VTab
5fc50 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  le *pNext;      
5fc60 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e        /* Next in
5fc70 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 73 65   linked list (se
5fc80 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b 0a 0a  e above) */.};..
5fc90 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 74  /*.** Each SQL t
5fca0 61 62 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e  able is represen
5fcb0 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79  ted in memory by
5fcc0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
5fcd0 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  the.** following
5fce0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
5fcf0 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 69 73  * Table.zName is
5fd00 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
5fd10 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63 61 73   table.  The cas
5fd20 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  e of the origina
5fd30 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  l.** CREATE TABL
5fd40 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73  E statement is s
5fd50 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73 65 20  tored, but case 
5fd60 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69 63 61  is not significa
5fd70 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 61 72  nt for.** compar
5fd80 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 61 62  isons..**.** Tab
5fd90 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65 20 6e  le.nCol is the n
5fda0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
5fdb0 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20   in this table. 
5fdc0 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73 20 61   Table.aCol is a
5fdd0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  .** pointer to a
5fde0 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c 75 6d  n array of Colum
5fdf0 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20 6f 6e  n structures, on
5fe00 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  e for each colum
5fe10 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
5fe20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54  table has an INT
5fe30 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
5fe40 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69 50 4b  , then Table.iPK
5fe50 65 79 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  ey is the index 
5fe60 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  of.** the column
5fe70 20 74 68 61 74 20 69 73 20 74 68 61 74 20 6b 65   that is that ke
5fe80 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65 20 54  y.   Otherwise T
5fe90 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 6e 65  able.iPKey is ne
5fea0 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a 2a 2a  gative.  Note.**
5feb0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 74 79   that the dataty
5fec0 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52  pe of the PRIMAR
5fed0 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 49 4e  Y KEY must be IN
5fee0 54 45 47 45 52 20 66 6f 72 20 74 68 69 73 20 66  TEGER for this f
5fef0 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20 73 65  ield to.** be se
5ff00 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52 20 50  t.  An INTEGER P
5ff10 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 75 73  RIMARY KEY is us
5ff20 65 64 20 61 73 20 74 68 65 20 72 6f 77 69 64 20  ed as the rowid 
5ff30 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 0a  for each row of.
5ff40 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49  ** the table.  I
5ff50 66 20 61 20 74 61 62 6c 65 20 68 61 73 20 6e 6f  f a table has no
5ff60 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
5ff70 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72 61 6e   KEY, then a ran
5ff80 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69 73 20  dom rowid.** is 
5ff90 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 65 61  generated for ea
5ffa0 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 74 61  ch row of the ta
5ffb0 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 72 69 6d  ble.  TF_HasPrim
5ffc0 61 72 79 4b 65 79 20 69 73 20 73 65 74 20 69 66  aryKey is set if
5ffd0 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61  .** the table ha
5ffe0 73 20 61 6e 79 20 50 52 49 4d 41 52 59 20 4b 45  s any PRIMARY KE
5fff0 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 20 6f 74  Y, INTEGER or ot
60000 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  herwise..**.** T
60010 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 74 68 65  able.tnum is the
60020 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72   page number for
60030 20 74 68 65 20 72 6f 6f 74 20 42 54 72 65 65 20   the root BTree 
60040 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c  page of the tabl
60050 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61  e in the.** data
60060 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 54  base file.  If T
60070 61 62 6c 65 2e 69 44 62 20 69 73 20 74 68 65 20  able.iDb is the 
60080 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
60090 61 62 61 73 65 20 74 61 62 6c 65 20 62 61 63 6b  abase table back
600a0 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65  end.** in sqlite
600b0 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 20 66 6f  .aDb[].  0 is fo
600c0 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
600d0 61 73 65 20 61 6e 64 20 31 20 69 73 20 66 6f 72  ase and 1 is for
600e0 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 0a 2a   the file that.*
600f0 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72 61 72  * holds temporar
60100 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64  y tables and ind
60110 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45 70 68  ices.  If TF_Eph
60120 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0a 2a 2a  emeral is set.**
60130 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   then the table 
60140 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 66  is stored in a f
60150 69 6c 65 20 74 68 61 74 20 69 73 20 61 75 74 6f  ile that is auto
60160 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65  matically delete
60170 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 56 44  d.** when the VD
60180 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  BE cursor to the
60190 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 64   table is closed
601a0 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  .  In this case 
601b0 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 72  Table.tnum .** r
601c0 65 66 65 72 73 20 56 44 42 45 20 63 75 72 73 6f  efers VDBE curso
601d0 72 20 6e 75 6d 62 65 72 20 74 68 61 74 20 68 6f  r number that ho
601e0 6c 64 73 20 74 68 65 20 74 61 62 6c 65 20 6f 70  lds the table op
601f0 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 65 20 72  en, not to the r
60200 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62  oot.** page numb
60210 65 72 2e 20 20 54 72 61 6e 73 69 65 6e 74 20 74  er.  Transient t
60220 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 74  ables are used t
60230 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c  o hold the resul
60240 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 62 2d 71  ts of a.** sub-q
60250 75 65 72 79 20 74 68 61 74 20 61 70 70 65 61 72  uery that appear
60260 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 72  s instead of a r
60270 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  eal table name i
60280 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
60290 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43  e .** of a SELEC
602a0 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a  T statement..*/.
602b0 73 74 72 75 63 74 20 54 61 62 6c 65 20 7b 0a 20  struct Table {. 
602c0 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d 3b   sqlite3 *dbMem;
602d0 20 20 20 20 20 20 2f 2a 20 44 42 20 63 6f 6e 6e        /* DB conn
602e0 65 63 74 69 6f 6e 20 75 73 65 64 20 66 6f 72 20  ection used for 
602f0 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61  lookaside alloca
60300 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 63 68 61 72  tions. */.  char
60310 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
60320 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
60330 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f  table or view */
60340 0a 20 20 69 6e 74 20 69 50 4b 65 79 3b 20 20 20  .  int iPKey;   
60350 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f          /* If no
60360 74 20 6e 65 67 61 74 69 76 65 2c 20 75 73 65 20  t negative, use 
60370 61 43 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 20 74  aCol[iPKey] as t
60380 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a  he primary key *
60390 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  /.  int nCol;   
603a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
603b0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
603c0 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
603d0 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20 20   Column *aCol;  
603e0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
603f0 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20  tion about each 
60400 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65  column */.  Inde
60410 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20  x *pIndex;      
60420 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c 20   /* List of SQL 
60430 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 69 73 20  indexes on this 
60440 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e 74 20  table. */.  int 
60450 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  tnum;           
60460 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65 65 20 6e   /* Root BTree n
60470 6f 64 65 20 66 6f 72 20 74 68 69 73 20 74 61 62  ode for this tab
60480 6c 65 20 28 73 65 65 20 6e 6f 74 65 20 61 62 6f  le (see note abo
60490 76 65 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ve) */.  Select 
604a0 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a  *pSelect;     /*
604b0 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 6c 65 73   NULL for tables
604c0 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 64 65 66  .  Points to def
604d0 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 76 69 65  inition if a vie
604e0 77 2e 20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 66  w. */.  u16 nRef
604f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
60500 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65  Number of pointe
60510 72 73 20 74 6f 20 74 68 69 73 20 54 61 62 6c 65  rs to this Table
60520 20 2a 2f 0a 20 20 75 38 20 74 61 62 46 6c 61 67   */.  u8 tabFlag
60530 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  s;         /* Ma
60540 73 6b 20 6f 66 20 54 46 5f 2a 20 76 61 6c 75 65  sk of TF_* value
60550 73 20 2a 2f 0a 20 20 75 38 20 6b 65 79 43 6f 6e  s */.  u8 keyCon
60560 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  f;          /* W
60570 68 61 74 20 74 6f 20 64 6f 20 69 6e 20 63 61 73  hat to do in cas
60580 65 20 6f 66 20 75 6e 69 71 75 65 6e 65 73 73 20  e of uniqueness 
60590 63 6f 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65  conflict on iPKe
605a0 79 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b  y */.  FKey *pFK
605b0 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ey;         /* L
605c0 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c  inked list of al
605d0 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 69  l foreign keys i
605e0 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  n this table */.
605f0 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b    char *zColAff;
60600 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67         /* String
60610 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66   defining the af
60620 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63  finity of each c
60630 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 66  olumn */.#ifndef
60640 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
60650 43 4b 0a 20 20 45 78 70 72 20 2a 70 43 68 65 63  CK.  Expr *pChec
60660 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  k;        /* The
60670 20 41 4e 44 20 6f 66 20 61 6c 6c 20 43 48 45 43   AND of all CHEC
60680 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  K constraints */
60690 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
606a0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45  SQLITE_OMIT_ALTE
606b0 52 54 41 42 4c 45 0a 20 20 69 6e 74 20 61 64 64  RTABLE.  int add
606c0 43 6f 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a  ColOffset;    /*
606d0 20 4f 66 66 73 65 74 20 69 6e 20 43 52 45 41 54   Offset in CREAT
606e0 45 20 54 41 42 4c 45 20 73 74 6d 74 20 74 6f 20  E TABLE stmt to 
606f0 61 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e  add a new column
60700 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64   */.#endif.#ifnd
60710 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
60720 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 54  IRTUALTABLE.  VT
60730 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b 20 20  able *pVTable;  
60740 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 56 54     /* List of VT
60750 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e 20 2a 2f  able objects. */
60760 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c 65 41 72  .  int nModuleAr
60770 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  g;      /* Numbe
60780 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
60790 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  o the module */.
607a0 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c    char **azModul
607b0 65 41 72 67 3b 20 20 2f 2a 20 54 65 78 74 20 6f  eArg;  /* Text o
607c0 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 72 67  f all module arg
607d0 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 75 6c 65  s. [0] is module
607e0 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 66 0a   name */.#endif.
607f0 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
60800 67 65 72 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f  ger;   /* List o
60810 66 20 74 72 69 67 67 65 72 73 20 73 74 6f 72 65  f triggers store
60820 64 20 69 6e 20 70 53 63 68 65 6d 61 20 2a 2f 0a  d in pSchema */.
60830 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
60840 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61  a;     /* Schema
60850 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
60860 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54  his table */.  T
60870 61 62 6c 65 20 2a 70 4e 65 78 74 5a 6f 6d 62 69  able *pNextZombi
60880 65 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 74  e;  /* Next on t
60890 68 65 20 50 61 72 73 65 2e 70 5a 6f 6d 62 69 65  he Parse.pZombie
608a0 54 61 62 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a  Tab list */.};..
608b0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
608c0 6c 75 65 73 20 66 6f 72 20 54 61 62 65 2e 74 61  lues for Tabe.ta
608d0 62 46 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 66 69  bFlags..*/.#defi
608e0 6e 65 20 54 46 5f 52 65 61 64 6f 6e 6c 79 20 20  ne TF_Readonly  
608f0 20 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a        0x01    /*
60900 20 52 65 61 64 2d 6f 6e 6c 79 20 73 79 73 74 65   Read-only syste
60910 6d 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69  m table */.#defi
60920 6e 65 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20  ne TF_Ephemeral 
60930 20 20 20 20 20 20 30 78 30 32 20 20 20 20 2f 2a        0x02    /*
60940 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61   An ephemeral ta
60950 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ble */.#define T
60960 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20  F_HasPrimaryKey 
60970 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 61 62    0x04    /* Tab
60980 6c 65 20 68 61 73 20 61 20 70 72 69 6d 61 72 79  le has a primary
60990 20 6b 65 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20   key */.#define 
609a0 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  TF_Autoincrement
609b0 20 20 20 30 78 30 38 20 20 20 20 2f 2a 20 49 6e     0x08    /* In
609c0 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65  teger primary ke
609d0 79 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65  y is autoincreme
609e0 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46  nt */.#define TF
609f0 5f 56 69 72 74 75 61 6c 20 20 20 20 20 20 20 20  _Virtual        
60a00 20 30 78 31 30 20 20 20 20 2f 2a 20 49 73 20 61   0x10    /* Is a
60a10 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
60a20 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 4e 65 65  /.#define TF_Nee
60a30 64 4d 65 74 61 64 61 74 61 20 20 20 20 30 78 32  dMetadata    0x2
60a40 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d 2e 7a  0    /* aCol[].z
60a50 54 79 70 65 20 61 6e 64 20 61 43 6f 6c 5b 5d 2e  Type and aCol[].
60a60 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67 20 2a 2f  pColl missing */
60a70 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74  ..../*.** Test t
60a80 6f 20 73 65 65 20 77 68 65 74 68 65 72 20 6f 72  o see whether or
60a90 20 6e 6f 74 20 61 20 74 61 62 6c 65 20 69 73 20   not a table is 
60aa0 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
60ab0 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 64 6f 6e    This is.** don
60ac0 65 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20  e as a macro so 
60ad0 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20  that it will be 
60ae0 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74 20 77 68  optimized out wh
60af0 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61  en virtual.** ta
60b00 62 6c 65 20 73 75 70 70 6f 72 74 20 69 73 20 6f  ble support is o
60b10 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20  mitted from the 
60b20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  build..*/.#ifnde
60b30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
60b40 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 65  RTUALTABLE.#  de
60b50 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58  fine IsVirtual(X
60b60 29 20 20 20 20 20 20 28 28 28 58 29 2d 3e 74 61  )      (((X)->ta
60b70 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
60b80 75 61 6c 29 21 3d 30 29 0a 23 20 20 64 65 66 69  ual)!=0).#  defi
60b90 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  ne IsHiddenColum
60ba0 6e 28 58 29 20 28 28 58 29 2d 3e 69 73 48 69 64  n(X) ((X)->isHid
60bb0 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20 20 64 65  den).#else.#  de
60bc0 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58  fine IsVirtual(X
60bd0 29 20 20 20 20 20 20 30 0a 23 20 20 64 65 66 69  )      0.#  defi
60be0 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  ne IsHiddenColum
60bf0 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f  n(X) 0.#endif../
60c00 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65 69 67  *.** Each foreig
60c10 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
60c20 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
60c30 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
60c40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
60c50 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  * A foreign key 
60c60 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  is associated wi
60c70 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e 20 20  th two tables.  
60c80 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62 6c 65  The "from" table
60c90 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   is.** the table
60ca0 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
60cb0 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c  he REFERENCES cl
60cc0 61 75 73 65 20 74 68 61 74 20 63 72 65 61 74 65  ause that create
60cd0 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a  s the foreign.**
60ce0 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f 22 20   key.  The "to" 
60cf0 74 61 62 6c 65 20 69 73 20 74 68 65 20 74 61 62  table is the tab
60d00 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65 64  le that is named
60d10 20 69 6e 20 74 68 65 20 52 45 46 45 52 45 4e 43   in the REFERENC
60d20 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 43 6f  ES clause..** Co
60d30 6e 73 69 64 65 72 20 74 68 69 73 20 65 78 61 6d  nsider this exam
60d40 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43  ple:.**.**     C
60d50 52 45 41 54 45 20 54 41 42 4c 45 20 65 78 31 28  REATE TABLE ex1(
60d60 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e 54 45  .**       a INTE
60d70 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
60d80 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e 54 45  .**       b INTE
60d90 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54 20 66  GER CONSTRAINT f
60da0 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20 65 78  k1 REFERENCES ex
60db0 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b 0a 2a  2(x).**     );.*
60dc0 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67 6e  *.** For foreign
60dd0 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68 65 20   key "fk1", the 
60de0 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22 65  from-table is "e
60df0 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f 2d 74  x1" and the to-t
60e00 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e 0a 2a  able is "ex2"..*
60e10 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45 52 45  *.** Each REFERE
60e20 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65 6e 65  NCES clause gene
60e30 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63  rates an instanc
60e40 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
60e50 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ng structure.** 
60e60 77 68 69 63 68 20 69 73 20 61 74 74 61 63 68 65  which is attache
60e70 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74 61  d to the from-ta
60e80 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74 61 62  ble.  The to-tab
60e90 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78 69 73  le need not exis
60ea0 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 66 72  t when.** the fr
60eb0 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72 65 61  om-table is crea
60ec0 74 65 64 2e 20 20 54 68 65 20 65 78 69 73 74 65  ted.  The existe
60ed0 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d 74 61  nce of the to-ta
60ee0 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65 63 6b  ble is not check
60ef0 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 4b  ed..*/.struct FK
60f00 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 46  ey {.  Table *pF
60f10 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c  rom;     /* Tabl
60f20 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
60f30 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75   REFERENCES clau
60f40 73 65 20 28 61 6b 61 3a 20 43 68 69 6c 64 29 20  se (aka: Child) 
60f50 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74  */.  FKey *pNext
60f60 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66  From;  /* Next f
60f70 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46  oreign key in pF
60f80 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  rom */.  char *z
60f90 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  To;        /* Na
60fa0 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74  me of table that
60fb0 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20   the key points 
60fc0 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 6e 74 29  to (aka: Parent)
60fd0 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78   */.  FKey *pNex
60fe0 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20  tTo;    /* Next 
60ff0 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74  foreign key on t
61000 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a  able named zTo *
61010 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 65 76 54  /.  FKey *pPrevT
61020 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75  o;    /* Previou
61030 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e  s foreign key on
61040 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f   table named zTo
61050 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20   */.  int nCol; 
61060 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
61070 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
61080 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 2f 2a  this key */.  /*
61090 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 39   EV: R-30323-219
610a0 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 44 65 66  17 */.  u8 isDef
610b0 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 54 72 75  erred;    /* Tru
610c0 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20  e if constraint 
610d0 63 68 65 63 6b 69 6e 67 20 69 73 20 64 65 66 65  checking is defe
610e0 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54  rred till COMMIT
610f0 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 69 6f 6e   */.  u8 aAction
61100 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  [2];          /*
61110 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f   ON DELETE and O
61120 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73  N UPDATE actions
61130 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 20 2a  , respectively *
61140 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 61 70 54  /.  Trigger *apT
61150 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 54  rigger[2];  /* T
61160 72 69 67 67 65 72 73 20 66 6f 72 20 61 41 63 74  riggers for aAct
61170 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a 2f  ion[] actions */
61180 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c 4d 61  .  struct sColMa
61190 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20  p {  /* Mapping 
611a0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46  of columns in pF
611b0 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 69  rom to columns i
611c0 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 6e 74  n zTo */.    int
611d0 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20   iFrom;         
611e0 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
611f0 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20  mn in pFrom */. 
61200 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20     char *zCol;  
61210 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
61220 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20   column in zTo. 
61230 20 49 66 20 30 20 75 73 65 20 50 52 49 4d 41 52   If 0 use PRIMAR
61240 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 43 6f  Y KEY */.  } aCo
61250 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20  l[1];        /* 
61260 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61  One entry for ea
61270 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d  ch of nCol colum
61280 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  n s */.};../*.**
61290 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 73   SQLite supports
612a0 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 20   many different 
612b0 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 20  ways to resolve 
612c0 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  a constraint.** 
612d0 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b  error.  ROLLBACK
612e0 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e   processing mean
612f0 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 61  s that a constra
61300 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a  int violation.**
61310 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 72   causes the oper
61320 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73  ation in process
61330 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f 72   to fail and for
61340 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
61350 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62  nsaction.** to b
61360 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20  e rolled back.  
61370 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67  ABORT processing
61380 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61   means the opera
61390 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a  tion in process.
613a0 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e 79  ** fails and any
613b0 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 66   prior changes f
613c0 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 65  rom that one ope
613d0 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b 65  ration are backe
613e0 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68  d out,.** but th
613f0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e transaction is
61400 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b   not rolled back
61410 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 69  .  FAIL processi
61420 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a  ng means that.**
61430 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69   the operation i
61440 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73  n progress stops
61450 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20   and returns an 
61460 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74  error code.  But
61470 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65   prior.** change
61480 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d  s due to the sam
61490 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20  e operation are 
614a0 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61  not backed out a
614b0 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a  nd no rollback.*
614c0 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52  * occurs.  IGNOR
614d0 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  E means that the
614e0 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20   particular row 
614f0 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20  that caused the 
61500 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72  constraint.** er
61510 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 72  ror is not inser
61520 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20  ted or updated. 
61530 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74   Processing cont
61540 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72  inues and no err
61550 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  or.** is returne
61560 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 6e  d.  REPLACE mean
61570 73 20 74 68 61 74 20 70 72 65 65 78 69 73 74 69  s that preexisti
61580 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 73  ng database rows
61590 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20   that caused.** 
615a0 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61  a UNIQUE constra
615b0 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72  int violation ar
615c0 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 61  e removed so tha
615d0 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 74  t the new insert
615e0 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 61   or.** update ca
615f0 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f 63  n proceed.  Proc
61600 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73  essing continues
61610 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 73   and no error is
61620 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a   reported..**.**
61630 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e 55   RESTRICT, SETNU
61640 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 20  LL, and CASCADE 
61650 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e  actions apply on
61660 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65  ly to foreign ke
61670 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 20  ys..** RESTRICT 
61680 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 41  is the same as A
61690 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 41  BORT for IMMEDIA
616a0 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20  TE foreign keys 
616b0 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20  and the.** same 
616c0 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20  as ROLLBACK for 
616d0 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 20  DEFERRED keys.  
616e0 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68  SETNULL means th
616f0 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a  at the foreign.*
61700 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f 20  * key is set to 
61710 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 6d  NULL.  CASCADE m
61720 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c 45  eans that a DELE
61730 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 20  TE or UPDATE of 
61740 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65  the.** reference
61750 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 70  d table row is p
61760 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 74  ropagated into t
61770 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c 64  he row that hold
61780 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e  s the.** foreign
61790 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65   key..** .** The
617a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f   following symbo
617b0 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 75  lic values are u
617c0 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68  sed to record wh
617d0 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61  ich type.** of a
617e0 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a  ction to take..*
617f0 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e  /.#define OE_Non
61800 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65  e     0   /* The
61810 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61  re is no constra
61820 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a  int to check */.
61830 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62  #define OE_Rollb
61840 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c 20  ack 1   /* Fail 
61850 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e  the operation an
61860 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74  d rollback the t
61870 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64  ransaction */.#d
61880 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20  efine OE_Abort  
61890 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75    2   /* Back ou
618a0 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 6f  t changes but do
618b0 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61   no rollback tra
618c0 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66  nsaction */.#def
618d0 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 20  ine OE_Fail     
618e0 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20  3   /* Stop the 
618f0 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c 65  operation but le
61900 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 68  ave all prior ch
61910 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  anges */.#define
61920 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 20   OE_Ignore   4  
61930 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 65   /* Ignore the e
61940 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20  rror. Do not do 
61950 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50  the INSERT or UP
61960 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20  DATE */.#define 
61970 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 20  OE_Replace  5   
61980 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 69  /* Delete existi
61990 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 20  ng record, then 
619a0 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44  do INSERT or UPD
619b0 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  ATE */..#define 
619c0 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 20  OE_Restrict 6   
619d0 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20  /* OE_Abort for 
619e0 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f  IMMEDIATE, OE_Ro
619f0 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 52  llback for DEFER
61a00 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  RED */.#define O
61a10 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f  E_SetNull  7   /
61a20 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67  * Set the foreig
61a30 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 4e  n key value to N
61a40 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ULL */.#define O
61a50 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 2f  E_SetDflt  8   /
61a60 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67  * Set the foreig
61a70 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 69  n key value to i
61a80 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64  ts default */.#d
61a90 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 65  efine OE_Cascade
61aa0 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 65    9   /* Cascade
61ab0 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a   the changes */.
61ac0 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 61  .#define OE_Defa
61ad0 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 77  ult  99  /* Do w
61ae0 68 61 74 65 76 65 72 20 74 68 65 20 64 65 66 61  hatever the defa
61af0 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f  ult action is */
61b00 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  .../*.** An inst
61b10 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
61b20 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
61b30 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
61b40 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65   first.** argume
61b50 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  nt to sqlite3Vdb
61b60 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 20  eKeyCompare and 
61b70 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72  is used to contr
61b80 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61  ol the .** compa
61b90 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f  rison of the two
61ba0 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a   index keys..*/.
61bb0 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b  struct KeyInfo {
61bc0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
61bd0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
61be0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
61bf0 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20  n */.  u8 enc;  
61c00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
61c10 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e  xt encoding - on
61c20 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74  e of the TEXT_Ut
61c30 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75  f* values */.  u
61c40 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  16 nField;      
61c50 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
61c60 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c  entries in aColl
61c70 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72  [] */.  u8 *aSor
61c80 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 49  tOrder;     /* I
61c90 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53 6f  f defined an aSo
61ca0 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 72  rtOrder[i] is tr
61cb0 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a 2f  ue, sort DESC */
61cc0 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c  .  CollSeq *aCol
61cd0 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74  l[1];  /* Collat
61ce0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
61cf0 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68   each term of th
61d00 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  e key */.};../*.
61d10 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
61d20 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
61d30 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20  structure holds 
61d40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
61d50 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e  t a.** single in
61d60 64 65 78 20 72 65 63 6f 72 64 20 74 68 61 74 20  dex record that 
61d70 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
61d80 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74 6f   parsed out into
61d90 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76   individual.** v
61da0 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72  alues..**.** A r
61db0 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a 65  ecord is an obje
61dc0 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ct that contains
61dd0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 65   one or more fie
61de0 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20  lds of data..** 
61df0 52 65 63 6f 72 64 73 20 61 72 65 20 75 73 65 64  Records are used
61e00 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f   to store the co
61e10 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65  ntent of a table
61e20 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72   row and to stor
61e30 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20  e.** the key of 
61e40 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f  an index.  A blo
61e50 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20  b encoding of a 
61e60 72 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 65  record is create
61e70 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d  d by.** the OP_M
61e80 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65  akeRecord opcode
61e90 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e 64   of the VDBE and
61ea0 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c 65 64   is disassembled
61eb0 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f   by the.** OP_Co
61ec0 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a  lumn opcode..**.
61ed0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
61ee0 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 64  e holds a record
61ef0 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64   that has alread
61f00 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d 62  y been disassemb
61f10 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20  led.** into its 
61f20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69 65 6c  constituent fiel
61f30 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e  ds..*/.struct Un
61f40 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a 20  packedRecord {. 
61f50 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
61f60 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f  fo;  /* Collatio
61f70 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65 72  n and sort-order
61f80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a   information */.
61f90 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20    u16 nField;   
61fa0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
61fb0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 70  of entries in ap
61fc0 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 66  Mem[] */.  u16 f
61fd0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f  lags;          /
61fe0 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69 6e  * Boolean settin
61ff0 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e 2e  gs.  UNPACKED_..
62000 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 34  . below */.  i64
62010 20 72 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20   rowid;         
62020 20 2f 2a 20 55 73 65 64 20 62 79 20 55 4e 50 41   /* Used by UNPA
62030 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52  CKED_PREFIX_SEAR
62040 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65  CH */.  Mem *aMe
62050 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  m;          /* V
62060 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  alues */.};../*.
62070 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
62080 73 20 6f 66 20 55 6e 70 61 63 6b 65 64 52 65 63  s of UnpackedRec
62090 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65  ord.flags.*/.#de
620a0 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45  fine UNPACKED_NE
620b0 45 44 5f 46 52 45 45 20 20 20 20 20 30 78 30 30  ED_FREE     0x00
620c0 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73  01  /* Memory is
620d0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c   from sqlite3Mal
620e0 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  loc() */.#define
620f0 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44   UNPACKED_NEED_D
62100 45 53 54 52 4f 59 20 20 30 78 30 30 30 32 20 20  ESTROY  0x0002  
62110 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73 68 6f 75  /* apMem[]s shou
62120 6c 64 20 61 6c 6c 20 62 65 20 64 65 73 74 72 6f  ld all be destro
62130 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55  yed */.#define U
62140 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52  NPACKED_IGNORE_R
62150 4f 57 49 44 20 20 30 78 30 30 30 34 20 20 2f 2a  OWID  0x0004  /*
62160 20 49 67 6e 6f 72 65 20 74 72 61 69 6c 69 6e 67   Ignore trailing
62170 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79 31 20 2a   rowid on key1 *
62180 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b  /.#define UNPACK
62190 45 44 5f 49 4e 43 52 4b 45 59 20 20 20 20 20 20  ED_INCRKEY      
621a0 20 30 78 30 30 30 38 20 20 2f 2a 20 4d 61 6b 65   0x0008  /* Make
621b0 20 74 68 69 73 20 6b 65 79 20 61 6e 20 65 70 73   this key an eps
621c0 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a 2f 0a 23  ilon larger */.#
621d0 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f  define UNPACKED_
621e0 50 52 45 46 49 58 5f 4d 41 54 43 48 20 20 30 78  PREFIX_MATCH  0x
621f0 30 30 31 30 20 20 2f 2a 20 41 20 70 72 65 66 69  0010  /* A prefi
62200 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69  x match is consi
62210 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23 64 65 66  dered OK */.#def
62220 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 45  ine UNPACKED_PRE
62230 46 49 58 5f 53 45 41 52 43 48 20 30 78 30 30 32  FIX_SEARCH 0x002
62240 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 20 6d  0  /* A prefix m
62250 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 65 72  atch is consider
62260 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ed OK */../*.** 
62270 45 61 63 68 20 53 51 4c 20 69 6e 64 65 78 20 69  Each SQL index i
62280 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e  s represented in
62290 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 0a 2a 2a   memory by an.**
622a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
622b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
622c0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
622d0 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74  columns of the t
622e0 61 62 6c 65 20 74 68 61 74 20 61 72 65 20 74 6f  able that are to
622f0 20 62 65 20 69 6e 64 65 78 65 64 20 61 72 65 20   be indexed are 
62300 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 20  described.** by 
62310 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66  the aiColumn[] f
62320 69 65 6c 64 20 6f 66 20 74 68 69 73 20 73 74 72  ield of this str
62330 75 63 74 75 72 65 2e 20 20 46 6f 72 20 65 78 61  ucture.  For exa
62340 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 0a 2a 2a  mple, suppose.**
62350 20 77 65 20 68 61 76 65 20 74 68 65 20 66 6f 6c   we have the fol
62360 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 61 6e 64  lowing table and
62370 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20   index:.**.**   
62380 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 45    CREATE TABLE E
62390 78 31 28 63 31 20 69 6e 74 2c 20 63 32 20 69 6e  x1(c1 int, c2 in
623a0 74 2c 20 63 33 20 74 65 78 74 29 3b 0a 2a 2a 20  t, c3 text);.** 
623b0 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58      CREATE INDEX
623c0 20 45 78 32 20 4f 4e 20 45 78 31 28 63 33 2c 63   Ex2 ON Ex1(c3,c
623d0 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  1);.**.** In the
623e0 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
623f0 20 64 65 73 63 72 69 62 69 6e 67 20 45 78 31 2c   describing Ex1,
62400 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65   nCol==3 because
62410 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 74 68   there are.** th
62420 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74  ree columns in t
62430 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 20 74 68  he table.  In th
62440 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72  e Index structur
62450 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20  e describing.** 
62460 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20  Ex2, nColumn==2 
62470 73 69 6e 63 65 20 32 20 6f 66 20 74 68 65 20 33  since 2 of the 3
62480 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 78 31 20   columns of Ex1 
62490 61 72 65 20 69 6e 64 65 78 65 64 2e 0a 2a 2a 20  are indexed..** 
624a0 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61 69 43  The value of aiC
624b0 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20 30 7d 2e  olumn is {2, 0}.
624c0 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32    aiColumn[0]==2
624d0 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 2a 2a   because the .**
624e0 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f   first column to
624f0 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 33 29   be indexed (c3)
62500 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
62510 20 32 20 69 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d   2 in Ex1.aCol[]
62520 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
62530 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64  column to be ind
62540 65 78 65 64 20 28 63 31 29 20 68 61 73 20 61 6e  exed (c1) has an
62550 20 69 6e 64 65 78 20 6f 66 20 30 20 69 6e 0a 2a   index of 0 in.*
62560 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65  * Ex1.aCol[], he
62570 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c 75 6d 6e  nce Ex2.aiColumn
62580 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68  [1]==0..**.** Th
62590 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20  e Index.onError 
625a0 66 69 65 6c 64 20 64 65 74 65 72 6d 69 6e 65 73  field determines
625b0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
625c0 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f 6c 75  the indexed colu
625d0 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62 65 20 75  mns.** must be u
625e0 6e 69 71 75 65 20 61 6e 64 20 77 68 61 74 20 74  nique and what t
625f0 6f 20 64 6f 20 69 66 20 74 68 65 79 20 61 72 65  o do if they are
62600 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49 6e 64 65   not.  When Inde
62610 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e  x.onError=OE_Non
62620 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 74  e,.** it means t
62630 68 69 73 20 69 73 20 6e 6f 74 20 61 20 75 6e 69  his is not a uni
62640 71 75 65 20 69 6e 64 65 78 2e 20 20 4f 74 68 65  que index.  Othe
62650 72 77 69 73 65 20 69 74 20 69 73 20 61 20 75 6e  rwise it is a un
62660 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a 20 61 6e  ique index.** an
62670 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 49  d the value of I
62680 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 69 6e 64  ndex.onError ind
62690 69 63 61 74 65 20 74 68 65 20 77 68 69 63 68 20  icate the which 
626a0 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74  conflict resolut
626b0 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 68  ion .** algorith
626c0 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77 68 65 6e  m to employ when
626d0 65 76 65 72 20 61 6e 20 61 74 74 65 6d 70 74 20  ever an attempt 
626e0 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65 72  is made to inser
626f0 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a  t a non-unique.*
62700 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74  * element..*/.st
62710 72 75 63 74 20 49 6e 64 65 78 20 7b 0a 20 20 63  ruct Index {.  c
62720 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
62730 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20  /* Name of this 
62740 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e  index */.  int n
62750 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e  Column;     /* N
62760 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
62770 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 75 73   in the table us
62780 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78  ed by this index
62790 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c   */.  int *aiCol
627a0 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63 68 20  umn;   /* Which 
627b0 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64  columns are used
627c0 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 2e 20   by this index. 
627d0 20 31 73 74 20 69 73 20 30 20 2a 2f 0a 20 20 75   1st is 0 */.  u
627e0 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f 77 45 73  nsigned *aiRowEs
627f0 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20  t; /* Result of 
62800 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20 72 6f  ANALYZE: Est. ro
62810 77 73 20 73 65 6c 65 63 74 65 64 20 62 79 20 65  ws selected by e
62820 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  ach column */.  
62830 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20  Table *pTable;  
62840 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 61 62 6c   /* The SQL tabl
62850 65 20 62 65 69 6e 67 20 69 6e 64 65 78 65 64 20  e being indexed 
62860 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20  */.  int tnum;  
62870 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f        /* Page co
62880 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6f 66  ntaining root of
62890 20 74 68 69 73 20 69 6e 64 65 78 20 69 6e 20 64   this index in d
628a0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
628b0 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b 20 20 20    u8 onError;   
628c0 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20     /* OE_Abort, 
628d0 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65  OE_Ignore, OE_Re
628e0 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e  place, or OE_Non
628f0 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e  e */.  u8 autoIn
62900 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  dex;    /* True 
62910 69 66 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  if is automatica
62920 6c 6c 79 20 63 72 65 61 74 65 64 20 28 65 78 3a  lly created (ex:
62930 20 62 79 20 55 4e 49 51 55 45 29 20 2a 2f 0a 20   by UNIQUE) */. 
62940 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20   char *zColAff; 
62950 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69    /* String defi
62960 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74  ning the affinit
62970 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e  y of each column
62980 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 4e 65   */.  Index *pNe
62990 78 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 6e 65  xt;    /* The ne
629a0 78 74 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61  xt index associa
629b0 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61 6d  ted with the sam
629c0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 53 63 68  e table */.  Sch
629d0 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 2f 2a  ema *pSchema; /*
629e0 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69   Schema containi
629f0 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f  ng this index */
62a00 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 65  .  u8 *aSortOrde
62a10 72 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20  r;  /* Array of 
62a20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43 6f 6c 75  size Index.nColu
62a30 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53 43 2c 20  mn. True==DESC, 
62a40 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f 0a 20 20  False==ASC */.  
62a50 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 20  char **azColl;  
62a60 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c   /* Array of col
62a70 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
62a80 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64 65 78 20  names for index 
62a90 2a 2f 0a 20 20 49 6e 64 65 78 53 61 6d 70 6c 65  */.  IndexSample
62aa0 20 2a 61 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a   *aSample;    /*
62ab0 20 41 72 72 61 79 20 6f 66 20 53 51 4c 49 54 45   Array of SQLITE
62ac0 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 73  _INDEX_SAMPLES s
62ad0 61 6d 70 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  amples */.};../*
62ae0 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70 6c 65 20  .** Each sample 
62af0 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71  stored in the sq
62b00 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65  lite_stat2 table
62b10 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20   is represented 
62b20 69 6e 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75 73  in memory .** us
62b30 69 6e 67 20 61 20 73 74 72 75 63 74 75 72 65 20  ing a structure 
62b40 6f 66 20 74 68 69 73 20 74 79 70 65 2e 0a 2a 2f  of this type..*/
62b50 0a 73 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d  .struct IndexSam
62b60 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a  ple {.  union {.
62b70 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20      char *z;    
62b80 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 66 20      /* Value if 
62b90 65 54 79 70 65 20 69 73 20 53 51 4c 49 54 45 5f  eType is SQLITE_
62ba0 54 45 58 54 20 6f 72 20 53 51 4c 49 54 45 5f 42  TEXT or SQLITE_B
62bb0 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c  LOB */.    doubl
62bc0 65 20 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 61  e r;       /* Va
62bd0 6c 75 65 20 69 66 20 65 54 79 70 65 20 69 73 20  lue if eType is 
62be0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 6f 72 20  SQLITE_FLOAT or 
62bf0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2a  SQLITE_INTEGER *
62c00 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 38 20 65 54  /.  } u;.  u8 eT
62c10 79 70 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ype;         /* 
62c20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c  SQLITE_NULL, SQL
62c30 49 54 45 5f 49 4e 54 45 47 45 52 20 2e 2e 2e 20  ITE_INTEGER ... 
62c40 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 6e 42 79  etc. */.  u8 nBy
62c50 74 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  te;         /* S
62c60 69 7a 65 20 69 6e 20 62 79 74 65 20 6f 66 20 74  ize in byte of t
62c70 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0a  ext or blob. */.
62c80 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74  };../*.** Each t
62c90 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20  oken coming out 
62ca0 6f 66 20 74 68 65 20 6c 65 78 65 72 20 69 73 20  of the lexer is 
62cb0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a  an instance of.*
62cc0 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  * this structure
62cd0 2e 20 20 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c  .  Tokens are al
62ce0 73 6f 20 75 73 65 64 20 61 73 20 70 61 72 74 20  so used as part 
62cf0 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
62d00 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20  ..**.** Note if 
62d10 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20  Token.z==0 then 
62d20 54 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f  Token.dyn and To
62d30 6b 65 6e 2e 6e 20 61 72 65 20 75 6e 64 65 66 69  ken.n are undefi
62d40 6e 65 64 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63  ned and.** may c
62d50 6f 6e 74 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61  ontain random va
62d60 6c 75 65 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61  lues.  Do not ma
62d70 6b 65 20 61 6e 79 20 61 73 73 75 6d 70 74 69 6f  ke any assumptio
62d80 6e 73 20 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64  ns about Token.d
62d90 79 6e 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e  yn.** and Token.
62da0 6e 20 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d  n when Token.z==
62db0 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b  0..*/.struct Tok
62dc0 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  en {.  const cha
62dd0 72 20 2a 7a 3b 20 20 20 20 20 2f 2a 20 54 65 78  r *z;     /* Tex
62de0 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20  t of the token. 
62df0 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e   Not NULL-termin
62e00 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67  ated! */.  unsig
62e10 6e 65 64 20 69 6e 74 20 6e 3b 20 20 20 20 2f 2a  ned int n;    /*
62e20 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
62e30 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f  cters in this to
62e40 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ken */.};../*.**
62e50 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
62e60 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
62e70 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74  ontains informat
62e80 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
62e90 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
62ea0 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 74  or a SELECT that
62eb0 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67   contains aggreg
62ec0 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ate functions..*
62ed0 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d  *.** If Expr.op=
62ee0 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f  =TK_AGG_COLUMN o
62ef0 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  r TK_AGG_FUNCTIO
62f00 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67  N then Expr.pAgg
62f10 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69  Info is a.** poi
62f20 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
62f30 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 70  ucture.  The Exp
62f40 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20  r.iColumn field 
62f50 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a  is the index in.
62f60 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b  ** AggInfo.aCol[
62f70 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75  ] or AggInfo.aFu
62f80 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74  nc[] of informat
62f90 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
62fa0 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
62fb0 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a  or that node..**
62fc0 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f  .** AggInfo.pGro
62fd0 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f  upBy and AggInfo
62fe0 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69  .aFunc.pExpr poi
62ff0 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74  nt to fields wit
63000 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69  hin the.** origi
63010 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63  nal Select struc
63020 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
63030 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 73  bes the SELECT s
63040 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65  tatement.  These
63050 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f  .** fields do no
63060 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65  t need to be fre
63070 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61  ed when dealloca
63080 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f  ting the AggInfo
63090 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
630a0 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a  truct AggInfo {.
630b0 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b    u8 directMode;
630c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72            /* Dir
630d0 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f  ect rendering mo
630e0 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61  de means take da
630f0 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20  ta directly.    
63100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63110 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f        ** from so
63120 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68  urce tables rath
63130 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63  er than from acc
63140 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75  umulators */.  u
63150 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b  8 useSortingIdx;
63160 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72         /* In dir
63170 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65  ect mode, refere
63180 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20  nce the sorting 
63190 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 20  index rather.   
631a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
631b0 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74         ** than t
631c0 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
631d0 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67  */.  int sorting
631e0 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Idx;         /* 
631f0 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
63200 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
63210 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ex */.  ExprList
63220 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20   *pGroupBy;     
63230 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20  /* The group by 
63240 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
63250 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20  nSortingColumn; 
63260 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
63270 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
63280 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f  sorting index */
63290 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
632a0 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f  o_col {    /* Fo
632b0 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73  r each column us
632c0 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62  ed in source tab
632d0 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65  les */.    Table
632e0 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
632f0 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61      /* Source ta
63300 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ble */.    int i
63310 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
63320 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
63330 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72  mber of the sour
63340 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ce table */.    
63350 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
63360 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
63370 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e  mn number within
63380 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
63390 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f  e */.    int iSo
633a0 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  rterColumn;     
633b0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62    /* Column numb
633c0 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e  er in the sortin
633d0 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69  g index */.    i
633e0 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt iMem;        
633f0 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
63400 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20  y location that 
63410 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61  acts as accumula
63420 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  tor */.    Expr 
63430 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
63440 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69      /* The origi
63450 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  nal expression *
63460 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69  /.  } *aCol;.  i
63470 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nt nColumn;     
63480 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
63490 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 73   of used entries
634a0 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20   in aCol[] */.  
634b0 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63  int nColumnAlloc
634c0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
634d0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
634e0 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20  ated for aCol[] 
634f0 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75  */.  int nAccumu
63500 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20  lator;       /* 
63510 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
63520 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f  s that show thro
63530 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ugh to the outpu
63540 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t..             
63550 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
63560 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d  Additional colum
63570 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79  ns are used only
63580 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74   as parameters t
63590 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o.              
635a0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61              ** a
635b0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
635c0 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41  ns */.  struct A
635d0 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20  ggInfo_func {   
635e0 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 72  /* For each aggr
635f0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a  egate function *
63600 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  /.    Expr *pExp
63610 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  r;             /
63620 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63  * Expression enc
63630 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69  oding the functi
63640 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65  on */.    FuncDe
63650 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
63660 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67     /* The aggreg
63670 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  ate function imp
63680 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
63690 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20     int iMem;    
636a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
636b0 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74  emory location t
636c0 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75  hat acts as accu
636d0 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69  mulator */.    i
636e0 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20  nt iDistinct;   
636f0 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 6d          /* Ephem
63700 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64 20  eral table used 
63710 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54 49  to enforce DISTI
63720 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 6e  NCT */.  } *aFun
63730 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b 20  c;.  int nFunc; 
63740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
63750 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
63760 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a  s in aFunc[] */.
63770 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f 63    int nFuncAlloc
63780 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
63790 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c  ber of slots all
637a0 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e 63  ocated for aFunc
637b0 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  [] */.};../*.** 
637c0 54 68 65 20 64 61 74 61 74 79 70 65 20 79 6e 56  The datatype ynV
637d0 61 72 20 69 73 20 61 20 73 69 67 6e 65 64 20 69  ar is a signed i
637e0 6e 74 65 67 65 72 2c 20 65 69 74 68 65 72 20 31  nteger, either 1
637f0 36 2d 62 69 74 20 6f 72 20 33 32 2d 62 69 74 2e  6-bit or 32-bit.
63800 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 69 74 20 69  .** Usually it i
63810 73 20 31 36 2d 62 69 74 73 2e 20 20 42 75 74 20  s 16-bits.  But 
63820 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41  if SQLITE_MAX_VA
63830 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 69 73  RIABLE_NUMBER is
63840 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e   greater.** than
63850 20 33 32 37 36 37 20 77 65 20 68 61 76 65 20 74   32767 we have t
63860 6f 20 6d 61 6b 65 20 69 74 20 33 32 2d 62 69 74  o make it 32-bit
63870 2e 20 20 31 36 2d 62 69 74 20 69 73 20 70 72 65  .  16-bit is pre
63880 66 65 72 72 65 64 20 62 65 63 61 75 73 65 0a 2a  ferred because.*
63890 2a 20 69 74 20 75 73 65 73 20 6c 65 73 73 20 6d  * it uses less m
638a0 65 6d 6f 72 79 20 69 6e 20 74 68 65 20 45 78 70  emory in the Exp
638b0 72 20 6f 62 6a 65 63 74 2c 20 77 68 69 63 68 20  r object, which 
638c0 69 73 20 61 20 62 69 67 20 6d 65 6d 6f 72 79 20  is a big memory 
638d0 75 73 65 72 0a 2a 2a 20 69 6e 20 73 79 73 74 65  user.** in syste
638e0 6d 73 20 77 69 74 68 20 6c 6f 74 73 20 6f 66 20  ms with lots of 
638f0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
63900 6e 74 73 2e 20 20 41 6e 64 20 66 65 77 20 61 70  nts.  And few ap
63910 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6e 65  plications.** ne
63920 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 61 62 6f  ed more than abo
63930 75 74 20 31 30 20 6f 72 20 32 30 20 76 61 72 69  ut 10 or 20 vari
63940 61 62 6c 65 73 2e 20 20 42 75 74 20 73 6f 6d 65  ables.  But some
63950 20 65 78 74 72 65 6d 65 20 75 73 65 72 73 20 77   extreme users w
63960 61 6e 74 0a 2a 2a 20 74 6f 20 68 61 76 65 20 70  ant.** to have p
63970 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
63980 74 73 20 77 69 74 68 20 6f 76 65 72 20 33 32 37  ts with over 327
63990 36 37 20 76 61 72 69 61 62 6c 65 73 2c 20 61 6e  67 variables, an
639a0 64 20 66 6f 72 20 74 68 65 6d 0a 2a 2a 20 74 68  d for them.** th
639b0 65 20 6f 70 74 69 6f 6e 20 69 73 20 61 76 61 69  e option is avai
639c0 6c 61 62 6c 65 20 28 61 74 20 63 6f 6d 70 69 6c  lable (at compil
639d0 65 2d 74 69 6d 65 29 2e 0a 2a 2f 0a 23 69 66 20  e-time)..*/.#if 
639e0 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
639f0 42 4c 45 5f 4e 55 4d 42 45 52 3c 3d 33 32 37 36  BLE_NUMBER<=3276
63a00 37 0a 74 79 70 65 64 65 66 20 69 31 36 20 79 6e  7.typedef i16 yn
63a10 56 61 72 3b 0a 23 65 6c 73 65 0a 74 79 70 65 64  Var;.#else.typed
63a20 65 66 20 69 6e 74 20 79 6e 56 61 72 3b 0a 23 65  ef int ynVar;.#e
63a30 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  ndif../*.** Each
63a40 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 78 70 72   node of an expr
63a50 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 70 61  ession in the pa
63a60 72 73 65 20 74 72 65 65 20 69 73 20 61 6e 20 69  rse tree is an i
63a70 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68  nstance.** of th
63a80 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  is structure..**
63a90 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73 20 74  .** Expr.op is t
63aa0 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 69  he opcode. The i
63ab0 6e 74 65 67 65 72 20 70 61 72 73 65 72 20 74 6f  nteger parser to
63ac0 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 20 72 65  ken codes are re
63ad0 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 63 6f 64  used.** as opcod
63ae0 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65 78 61  es here. For exa
63af0 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73 65 72  mple, the parser
63b00 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45 20 74   defines TK_GE t
63b10 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 0a  o be an integer.
63b20 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73 65 6e  ** code represen
63b30 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20 6f 70  ting the ">=" op
63b40 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 61 6d  erator. This sam
63b50 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 69  e integer code i
63b60 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 6f 20 72  s reused.** to r
63b70 65 70 72 65 73 65 6e 74 20 74 68 65 20 67 72 65  epresent the gre
63b80 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65 71 75  ater-than-or-equ
63b90 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72 20 69  al-to operator i
63ba0 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
63bb0 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  .** tree..**.** 
63bc0 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
63bd0 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c 69 74 65  n is an SQL lite
63be0 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 45 52 2c  ral (TK_INTEGER,
63bf0 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b 5f 42 4c   TK_FLOAT, TK_BL
63c00 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b 5f 53 54  OB, .** or TK_ST
63c10 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 78 70 72  RING), then Expr
63c20 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20  .token contains 
63c30 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  the text of the 
63c40 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 49 66 0a  SQL literal. If.
63c50 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  ** the expressio
63c60 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 20  n is a variable 
63c70 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 2c 20 74  (TK_VARIABLE), t
63c80 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63  hen Expr.token c
63c90 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a 2a 20  ontains the .** 
63ca0 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2e 20 46  variable name. F
63cb0 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 20 65  inally, if the e
63cc0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20  xpression is an 
63cd0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 28 54 4b  SQL function (TK
63ce0 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a 2a 20 74  _FUNCTION),.** t
63cf0 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63  hen Expr.token c
63d00 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d 65  ontains the name
63d10 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
63d20 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69  ..**.** Expr.pRi
63d30 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65  ght and Expr.pLe
63d40 66 74 20 61 72 65 20 74 68 65 20 6c 65 66 74 20  ft are the left 
63d50 61 6e 64 20 72 69 67 68 74 20 73 75 62 65 78 70  and right subexp
63d60 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 0a 2a 2a  ressions of a.**
63d70 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 6f 72   binary operator
63d80 2e 20 45 69 74 68 65 72 20 6f 72 20 62 6f 74 68  . Either or both
63d90 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a   may be NULL..**
63da0 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c 69 73 74  .** Expr.x.pList
63db0 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 61 72   is a list of ar
63dc0 67 75 6d 65 6e 74 73 20 69 66 20 74 68 65 20 65  guments if the e
63dd0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20  xpression is an 
63de0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a  SQL function,.**
63df0 20 61 20 43 41 53 45 20 65 78 70 72 65 73 73 69   a CASE expressi
63e00 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78 70 72  on or an IN expr
63e10 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f  ession of the fo
63e20 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 3c 79  rm "<lhs> IN (<y
63e30 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a 2a 20  >, <z>...)"..** 
63e40 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 69  Expr.x.pSelect i
63e50 73 20 75 73 65 64 20 69 66 20 74 68 65 20 65 78  s used if the ex
63e60 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75  pression is a su
63e70 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e 20 65  b-select or an e
63e80 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a 2a 2a 20  xpression of.** 
63e90 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20  the form "<lhs> 
63ea0 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22  IN (SELECT ...)"
63eb0 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49 73 53  . If the EP_xIsS
63ec0 65 6c 65 63 74 20 62 69 74 20 69 73 20 73 65 74  elect bit is set
63ed0 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 70 72 2e   in the.** Expr.
63ee0 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e  flags mask, then
63ef0 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20   Expr.x.pSelect 
63f00 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65 72 77  is valid. Otherw
63f10 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c 69 73  ise, Expr.x.pLis
63f20 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 64 2e 0a  t is .** valid..
63f30 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73  **.** An express
63f40 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ion of the form 
63f50 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65  ID or ID.ID refe
63f60 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69  rs to a column i
63f70 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f  n a table..** Fo
63f80 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f  r such expressio
63f90 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73  ns, Expr.op is s
63fa0 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20  et to TK_COLUMN 
63fb0 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20  and Expr.iTable 
63fc0 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65  is.** the intege
63fd0 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  r cursor number 
63fe0 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72  of a VDBE cursor
63ff0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61   pointing to tha
64000 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45  t table and.** E
64010 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74  xpr.iColumn is t
64020 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  he column number
64030 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
64040 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68  c column.  If th
64050 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
64060 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73  is used as a res
64070 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67  ult in an aggreg
64080 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e  ate SELECT, then
64090 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73   the.** value is
640a0 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20   also stored in 
640b0 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f  the Expr.iAgg co
640c0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72  lumn in the aggr
640d0 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a  egate so that.**
640e0 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73   it can be acces
640f0 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67  sed after all ag
64100 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d  gregates are com
64110 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  puted..**.** If 
64120 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
64130 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 76 61 72  s an unbound var
64140 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 28 61 20  iable marker (a 
64150 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b 20 0a 2a  question mark .*
64160 2a 20 63 68 61 72 61 63 74 65 72 20 27 3f 27 20  * character '?' 
64170 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
64180 53 51 4c 29 20 74 68 65 6e 20 74 68 65 20 45 78  SQL) then the Ex
64190 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c 64 73 20  pr.iTable holds 
641a0 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6e 75  the index .** nu
641b0 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 76 61  mber for that va
641c0 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  riable..**.** If
641d0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
641e0 69 73 20 61 20 73 75 62 71 75 65 72 79 20 74 68  is a subquery th
641f0 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20  en Expr.iColumn 
64200 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72  holds an integer
64210 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e 75 6d  .** register num
64220 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ber containing t
64230 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
64240 20 73 75 62 71 75 65 72 79 2e 20 20 49 66 20 74   subquery.  If t
64250 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 67  he.** subquery g
64260 69 76 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20  ives a constant 
64270 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 69 54 61  result, then iTa
64280 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 66 20 74  ble is -1.  If t
64290 68 65 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 67  he subquery.** g
642a0 69 76 65 73 20 61 20 64 69 66 66 65 72 65 6e 74  ives a different
642b0 20 61 6e 73 77 65 72 20 61 74 20 64 69 66 66 65   answer at diffe
642c0 72 65 6e 74 20 74 69 6d 65 73 20 64 75 72 69 6e  rent times durin
642d0 67 20 73 74 61 74 65 6d 65 6e 74 20 70 72 6f 63  g statement proc
642e0 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 6e 20 69  essing.** then i
642f0 54 61 62 6c 65 20 69 73 20 74 68 65 20 61 64 64  Table is the add
64300 72 65 73 73 20 6f 66 20 61 20 73 75 62 72 6f 75  ress of a subrou
64310 74 69 6e 65 20 74 68 61 74 20 63 6f 6d 70 75 74  tine that comput
64320 65 73 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  es the subquery.
64330 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78  .**.** If the Ex
64340 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50  pr is of type OP
64350 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65  _Column, and the
64360 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c   table it is sel
64370 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69  ecting from.** i
64380 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f  s a disk table o
64390 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73  r the "old.*" ps
643a0 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e  eudo-table, then
643b0 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20   pTab points to 
643c0 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  the.** correspon
643d0 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e  ding table defin
643e0 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 4c 4c  ition..**.** ALL
643f0 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 3a 0a 2a  OCATION NOTES:.*
64400 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a 65 63 74  *.** Expr object
64410 73 20 63 61 6e 20 75 73 65 20 61 20 6c 6f 74 20  s can use a lot 
64420 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20  of memory space 
64430 69 6e 20 64 61 74 61 62 61 73 65 20 73 63 68 65  in database sche
64440 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 6c 70 20  ma.  To.** help 
64450 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20 72 65  reduce memory re
64460 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 6f 6d 65  quirements, some
64470 74 69 6d 65 73 20 61 6e 20 45 78 70 72 20 6f 62  times an Expr ob
64480 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20  ject will be.** 
64490 74 72 75 6e 63 61 74 65 64 2e 20 20 41 6e 64 20  truncated.  And 
644a0 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 6e 75  to reduce the nu
644b0 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 61  mber of memory a
644c0 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f 6d 65  llocations, some
644d0 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 6f 72 20  times.** two or 
644e0 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65 63 74  more Expr object
644f0 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64  s will be stored
64500 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d 65 6d   in a single mem
64510 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a  ory allocation,.
64520 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69 74 68  ** together with
64530 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 73 74 72   Expr.zToken str
64540 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ings..**.** If t
64550 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 61 6e  he EP_Reduced an
64560 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66  d EP_TokenOnly f
64570 6c 61 67 73 20 61 72 65 20 73 65 74 20 77 68 65  lags are set whe
64580 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 6f 62 6a  n.** an Expr obj
64590 65 63 74 20 69 73 20 74 72 75 6e 63 61 74 65 64  ect is truncated
645a0 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 64 75 63  .  When EP_Reduc
645b0 65 64 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ed is set, then 
645c0 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 69 6c 64  all.** the child
645d0 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 69 6e   Expr objects in
645e0 20 74 68 65 20 45 78 70 72 2e 70 4c 65 66 74 20   the Expr.pLeft 
645f0 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 20  and Expr.pRight 
64600 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 72 65 20  subtrees.** are 
64610 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
64620 20 74 68 65 20 73 61 6d 65 20 6d 65 6d 6f 72 79   the same memory
64630 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f   allocation.  No
64640 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61  te, however, tha
64650 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 72 65 65  t.** the subtree
64660 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 4c 69 73  s in Expr.x.pLis
64670 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 65 6c  t or Expr.x.pSel
64680 65 63 74 20 61 72 65 20 61 6c 77 61 79 73 20 73  ect are always s
64690 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 61 6c 6c  eparately.** all
646a0 6f 63 61 74 65 64 2c 20 72 65 67 61 72 64 6c 65  ocated, regardle
646b0 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72  ss of whether or
646c0 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 65 64 20   not EP_Reduced 
646d0 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 72 75 63  is set..*/.struc
646e0 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70  t Expr {.  u8 op
646f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
64700 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70    /* Operation p
64710 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73  erformed by this
64720 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20   node */.  char 
64730 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20  affinity;       
64740 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74    /* The affinit
64750 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  y of the column 
64760 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f  or 0 if not a co
64770 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c  lumn */.  u16 fl
64780 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
64790 20 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67   /* Various flag
647a0 73 2e 20 20 45 50 5f 2a 20 53 65 65 20 62 65 6c  s.  EP_* See bel
647b0 6f 77 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a  ow */.  union {.
647c0 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e      char *zToken
647d0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f  ;          /* To
647e0 6b 65 6e 20 76 61 6c 75 65 2e 20 5a 65 72 6f 20  ken value. Zero 
647f0 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 64  terminated and d
64800 65 71 75 6f 74 65 64 20 2a 2f 0a 20 20 20 20 69  equoted */.    i
64810 6e 74 20 69 56 61 6c 75 65 3b 20 20 20 20 20 20  nt iValue;      
64820 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
64830 20 76 61 6c 75 65 20 69 66 20 45 50 5f 49 6e 74   value if EP_Int
64840 56 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a  Value */.  } u;.
64850 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f  .  /* If the EP_
64860 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69  TokenOnly flag i
64870 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 70  s set in the Exp
64880 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68  r.flags mask, th
64890 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65  en no.  ** space
648a0 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f   is allocated fo
648b0 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c  r the fields bel
648c0 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41  ow this point. A
648d0 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a  n attempt to.  *
648e0 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 69  * access them wi
648f0 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73  ll result in a s
64900 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75  egfault or malfu
64910 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a  nction. .  *****
64920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64960 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74  /..  Expr *pLeft
64970 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ;           /* L
64980 65 66 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20  eft subnode */. 
64990 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20 20   Expr *pRight;  
649a0 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74          /* Right
649b0 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 75 6e   subnode */.  un
649c0 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 72 4c 69  ion {.    ExprLi
649d0 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 2f  st *pList;     /
649e0 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  * Function argum
649f0 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c 65 78 70  ents or in "<exp
64a00 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73  r> IN (<expr-lis
64a10 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63  t)" */.    Selec
64a20 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20  t *pSelect;     
64a30 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 75 62 2d  /* Used for sub-
64a40 73 65 6c 65 63 74 73 20 61 6e 64 20 22 3c 65 78  selects and "<ex
64a50 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 63 74 3e  pr> IN (<select>
64a60 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20 43  )" */.  } x;.  C
64a70 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20  ollSeq *pColl;  
64a80 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c        /* The col
64a90 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f 66 20 74  lation type of t
64aa0 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 2a  he column or 0 *
64ab0 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45  /..  /* If the E
64ac0 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 69  P_Reduced flag i
64ad0 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 70  s set in the Exp
64ae0 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68  r.flags mask, th
64af0 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65  en no.  ** space
64b00 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f   is allocated fo
64b10 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c  r the fields bel
64b20 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41  ow this point. A
64b30 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a  n attempt to.  *
64b40 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 69  * access them wi
64b50 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73  ll result in a s
64b60 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75  egfault or malfu
64b70 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a 2a  nction..  ******
64b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
64bc0 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 3b 20  ..  int iTable; 
64bd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b             /* TK
64be0 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f 72 20  _COLUMN: cursor 
64bf0 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20  number of table 
64c00 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e 0a 20  holding column. 
64c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64c20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 52 45          ** TK_RE
64c30 47 49 53 54 45 52 3a 20 72 65 67 69 73 74 65 72  GISTER: register
64c40 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 20 20   number.        
64c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64c60 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 52 3a 20   ** TK_TRIGGER: 
64c70 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d 3e 20 6f  1 -> new, 0 -> o
64c80 6c 64 20 2a 2f 0a 20 20 79 6e 56 61 72 20 69 43  ld */.  ynVar iC
64c90 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f  olumn;         /
64ca0 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 6f 6c  * TK_COLUMN: col
64cb0 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d 31 20 66  umn index.  -1 f
64cc0 6f 72 20 72 6f 77 69 64 2e 0a 20 20 20 20 20 20  or rowid..      
64cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64ce0 20 20 20 2a 2a 20 54 4b 5f 56 41 52 49 41 42 4c     ** TK_VARIABL
64cf0 45 3a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  E: variable numb
64d00 65 72 20 28 61 6c 77 61 79 73 20 3e 3d 20 31 29  er (always >= 1)
64d10 2e 20 2a 2f 0a 20 20 69 31 36 20 69 41 67 67 3b  . */.  i16 iAgg;
64d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
64d30 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20   Which entry in 
64d40 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
64d50 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f   or ->aFunc[] */
64d60 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a 6f 69  .  i16 iRightJoi
64d70 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20  nTable;   /* If 
64d80 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65  EP_FromJoin, the
64d90 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
64da0 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 75 38  the join */.  u8
64db0 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20 20 20   flags2;        
64dc0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 73       /* Second s
64dd0 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20 45 50  et of flags.  EP
64de0 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20 6f 70  2_... */.  u8 op
64df0 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2;              
64e00 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f 52 45 47    /* If a TK_REG
64e10 49 53 54 45 52 2c 20 74 68 65 20 6f 72 69 67 69  ISTER, the origi
64e20 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 45 78 70  nal value of Exp
64e30 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 49 6e 66  r.op */.  AggInf
64e40 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 20  o *pAggInfo;    
64e50 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b 5f 41   /* Used by TK_A
64e60 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 4b  GG_COLUMN and TK
64e70 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a 2f  _AGG_FUNCTION */
64e80 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
64e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
64ea0 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e  le for TK_COLUMN
64eb0 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 2a 2f   expressions. */
64ec0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
64ed0 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 69  EXPR_DEPTH>0.  i
64ee0 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20 20  nt nHeight;     
64ef0 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20        /* Height 
64f00 6f 66 20 74 68 65 20 74 72 65 65 20 68 65 61 64  of the tree head
64f10 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65 20  ed by this node 
64f20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
64f30 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
64f40 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 6e  g are the meanin
64f50 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68  gs of bits in th
64f60 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65  e Expr.flags fie
64f70 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ld..*/.#define E
64f80 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 30  P_FromJoin   0x0
64f90 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74  001  /* Originat
64fa0 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e  ed in ON or USIN
64fb0 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f  G clause of a jo
64fc0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  in */.#define EP
64fd0 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 30 30  _Agg        0x00
64fe0 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20  02  /* Contains 
64ff0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 67 72  one or more aggr
65000 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
65010 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 65  */.#define EP_Re
65020 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 34 20  solved   0x0004 
65030 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 65 65   /* IDs have bee
65040 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f  n resolved to CO
65050 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65  LUMNs */.#define
65060 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 20 30   EP_Error      0
65070 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 65 73  x0008  /* Expres
65080 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  sion contains on
65090 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f 72 73  e or more errors
650a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44   */.#define EP_D
650b0 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 31 30  istinct   0x0010
650c0 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66    /* Aggregate f
650d0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 49 53  unction with DIS
650e0 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f  TINCT keyword */
650f0 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 72 53  .#define EP_VarS
65100 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 20 2f  elect  0x0020  /
65110 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 6f 72  * pSelect is cor
65120 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e  related, not con
65130 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  stant */.#define
65140 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 20 30   EP_DblQuoted  0
65150 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e  x0040  /* token.
65160 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79  z was originally
65170 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65   in "..." */.#de
65180 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e  fine EP_InfixFun
65190 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72  c  0x0080  /* Tr
651a0 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20  ue for an infix 
651b0 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20  function: LIKE, 
651c0 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65  GLOB, etc */.#de
651d0 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61  fine EP_ExpColla
651e0 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f  te 0x0100  /* Co
651f0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
65200 20 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69   specified expli
65210 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65  citly */.#define
65220 20 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30   EP_AnyAff     0
65230 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61  x0200  /* Can ta
65240 6b 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75  ke a cached colu
65250 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69  mn of any affini
65260 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  ty */.#define EP
65270 5f 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34  _FixedDest  0x04
65280 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65  00  /* Result ne
65290 65 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66  eded in a specif
652a0 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23  ic register */.#
652b0 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c  define EP_IntVal
652c0 75 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20  ue   0x0800  /* 
652d0 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f  Integer value co
652e0 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69 56 61  ntained in u.iVa
652f0 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  lue */.#define E
65300 50 5f 78 49 73 53 65 6c 65 63 74 20 20 30 78 31  P_xIsSelect  0x1
65310 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 6c 65 63  000  /* x.pSelec
65320 74 20 69 73 20 76 61 6c 69 64 20 28 6f 74 68 65  t is valid (othe
65330 72 77 69 73 65 20 78 2e 70 4c 69 73 74 20 69 73  rwise x.pList is
65340 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 45 50  ) */..#define EP
65350 5f 52 65 64 75 63 65 64 20 20 20 20 30 78 32 30  _Reduced    0x20
65360 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75  00  /* Expr stru
65370 63 74 20 69 73 20 45 58 50 52 5f 52 45 44 55 43  ct is EXPR_REDUC
65380 45 44 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c  EDSIZE bytes onl
65390 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  y */.#define EP_
653a0 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 30 30  TokenOnly  0x400
653b0 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63  0  /* Expr struc
653c0 74 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 4e 4f  t is EXPR_TOKENO
653d0 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 20 6f 6e  NLYSIZE bytes on
653e0 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  ly */.#define EP
653f0 5f 53 74 61 74 69 63 20 20 20 20 20 30 78 38 30  _Static     0x80
65400 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 20 6d  00  /* Held in m
65410 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 69 6e  emory not obtain
65420 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
65430 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66   */../*.** The f
65440 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65  ollowing are the
65450 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74   meanings of bit
65460 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c  s in the Expr.fl
65470 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  ags2 field..*/.#
65480 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f  define EP2_Mallo
65490 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 30 31  cedToken  0x0001
654a0 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 71 6c    /* Need to sql
654b0 69 74 65 33 44 62 46 72 65 65 28 29 20 45 78 70  ite3DbFree() Exp
654c0 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 66  r.zToken */.#def
654d0 69 6e 65 20 45 50 32 5f 49 72 72 65 64 75 63 69  ine EP2_Irreduci
654e0 62 6c 65 20 20 20 20 30 78 30 30 30 32 20 20 2f  ble    0x0002  /
654f0 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 55 50  * Cannot EXPRDUP
65500 5f 52 45 44 55 43 45 20 74 68 69 73 20 45 78 70  _REDUCE this Exp
65510 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  r */../*.** The 
65520 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73  pseudo-routine s
65530 71 6c 69 74 65 33 45 78 70 72 53 65 74 49 72 72  qlite3ExprSetIrr
65540 65 64 75 63 69 62 6c 65 20 73 65 74 73 20 74 68  educible sets th
65550 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c  e EP2_Irreducibl
65560 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e 20  e.** flag on an 
65570 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63  expression struc
65580 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61 67  ture.  This flag
65590 20 69 73 20 75 73 65 64 20 66 6f 72 20 56 56 26   is used for VV&
655a0 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a 20  A only.  The.** 
655b0 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 6c 65  routine is imple
655c0 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63 72  mented as a macr
655d0 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f 72 6b  o that only work
655e0 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75 67 67  s when in debugg
655f0 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f 20  ing mode,.** so 
65600 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 65 6e  as not to burden
65610 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f 64 65   production code
65620 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
65630 54 45 5f 44 45 42 55 47 0a 23 20 64 65 66 69 6e  TE_DEBUG.# defin
65640 65 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63  e ExprSetIrreduc
65650 69 62 6c 65 28 58 29 20 20 28 58 29 2d 3e 66 6c  ible(X)  (X)->fl
65660 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 72 65  ags2 |= EP2_Irre
65670 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a 23 20  ducible.#else.# 
65680 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49 72  define ExprSetIr
65690 72 65 64 75 63 69 62 6c 65 28 58 29 0a 23 65 6e  reducible(X).#en
656a0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  dif../*.** These
656b0 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75   macros can be u
656c0 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74  sed to test, set
656d0 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20  , or clear bits 
656e0 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e  in the .** Expr.
656f0 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a  flags field..*/.
65700 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50  #define ExprHasP
65710 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20  roperty(E,P)    
65720 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50   (((E)->flags&(P
65730 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65  ))==(P)).#define
65740 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65   ExprHasAnyPrope
65750 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d  rty(E,P)  (((E)-
65760 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a  >flags&(P))!=0).
65770 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50  #define ExprSetP
65780 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20  roperty(E,P)    
65790 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29   (E)->flags|=(P)
657a0 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65  .#define ExprCle
657b0 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20  arProperty(E,P) 
657c0 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28    (E)->flags&=~(
657d0 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  P)../*.** Macros
657e0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
657f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
65800 73 20 72 65 71 75 69 72 65 64 20 62 79 20 61 20  s required by a 
65810 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20  normal Expr .** 
65820 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70 72 20  struct, an Expr 
65830 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20  struct with the 
65840 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20  EP_Reduced flag 
65850 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67  set in Expr.flag
65860 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70  s .** and an Exp
65870 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68  r struct with th
65880 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66  e EP_TokenOnly f
65890 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66  lag set..*/.#def
658a0 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a  ine EXPR_FULLSIZ
658b0 45 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65  E           size
658c0 6f 66 28 45 78 70 72 29 20 20 20 20 20 20 20 20  of(Expr)        
658d0 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20     /* Full size 
658e0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f  */.#define EXPR_
658f0 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 20 20  REDUCEDSIZE     
65900 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72     offsetof(Expr
65910 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d  ,iTable)  /* Com
65920 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a 2f 0a  mon features */.
65930 23 64 65 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b  #define EXPR_TOK
65940 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 20  ENONLYSIZE      
65950 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 70 4c  offsetof(Expr,pL
65960 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65 72 20  eft)   /* Fewer 
65970 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a 0a  features */../*.
65980 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20  ** Flags passed 
65990 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45 78  to the sqlite3Ex
659a0 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f 6e  prDup() function
659b0 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72  . See the header
659c0 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f   comment .** abo
659d0 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 75  ve sqlite3ExprDu
659e0 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  p() for details.
659f0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52  .*/.#define EXPR
65a00 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20 20  DUP_REDUCE      
65a10 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55 73     0x0001  /* Us
65a20 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65 20  ed reduced-size 
65a30 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a 2f  Expr nodes */../
65a40 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 65  *.** A list of e
65a50 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61 63  xpressions.  Eac
65a60 68 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79  h expression may
65a70 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65   optionally have
65a80 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20   a.** name.  An 
65a90 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e  expr/name combin
65aa0 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65  ation can be use
65ab0 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79  d in several way
65ac0 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74 68  s, such.** as th
65ad0 65 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72 20  e list of "expr 
65ae0 41 53 20 49 44 22 20 66 69 65 6c 64 73 20 66 6f  AS ID" fields fo
65af0 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45 43  llowing a "SELEC
65b00 54 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20  T" or in the.** 
65b10 6c 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65 78  list of "ID = ex
65b20 70 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e 20  pr" items in an 
65b30 55 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74 20  UPDATE.  A list 
65b40 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63  of expressions c
65b50 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73  an.** also be us
65b60 65 64 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  ed as the argume
65b70 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  nt to a function
65b80 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  , in which case 
65b90 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66  the a.zName.** f
65ba0 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65 64  ield is not used
65bb0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72  ..*/.struct Expr
65bc0 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78  List {.  int nEx
65bd0 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pr;             
65be0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70  /* Number of exp
65bf0 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20  ressions on the 
65c00 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  list */.  int nA
65c10 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20  lloc;           
65c20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
65c30 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  tries allocated 
65c40 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69  below */.  int i
65c50 45 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20  ECursor;        
65c60 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72    /* VDBE Cursor
65c70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
65c80 20 74 68 69 73 20 45 78 70 72 4c 69 73 74 20 2a   this ExprList *
65c90 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  /.  struct ExprL
65ca0 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 45  ist_item {.    E
65cb0 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20  xpr *pExpr;     
65cc0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73        /* The lis
65cd0 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
65ce0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e   */.    char *zN
65cf0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ame;           /
65d00 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 61 74  * Token associat
65d10 65 64 20 77 69 74 68 20 74 68 69 73 20 65 78 70  ed with this exp
65d20 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 63  ression */.    c
65d30 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20 20 20  har *zSpan;     
65d40 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61        /* Origina
65d50 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 65 78  l text of the ex
65d60 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20  pression */.    
65d70 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20 20 20  u8 sortOrder;   
65d80 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20         /* 1 for 
65d90 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20 41 53  DESC or 0 for AS
65da0 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f 6e 65  C */.    u8 done
65db0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
65dc0 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 6e 64  /* A flag to ind
65dd0 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f 63 65  icate when proce
65de0 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 65  ssing is finishe
65df0 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 43 6f  d */.    u16 iCo
65e00 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
65e10 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42 59 2c  /* For ORDER BY,
65e20 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69   column number i
65e30 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  n result set */.
65e40 20 20 20 20 75 31 36 20 69 41 6c 69 61 73 3b 20      u16 iAlias; 
65e50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
65e60 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65 2e 61  dex into Parse.a
65e70 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e 61 6d  Alias[] for zNam
65e80 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20 20 20  e */.  } *a;    
65e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
65ea0 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   One entry for e
65eb0 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  ach expression *
65ec0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
65ed0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
65ee0 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
65ef0 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
65f00 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 68 0a 2a  to record both.*
65f10 2a 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65  * the parse tree
65f20 20 66 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69   for an expressi
65f30 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 61 6e 20  on and the span 
65f40 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 66 6f  of input text fo
65f50 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  r an.** expressi
65f60 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78  on..*/.struct Ex
65f70 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 70 72 20  prSpan {.  Expr 
65f80 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
65f90 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
65fa0 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20 2a 2f  on parse tree */
65fb0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
65fc0 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69 72 73  Start;   /* Firs
65fd0 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 69  t character of i
65fe0 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 20 63  nput text */.  c
65ff0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 3b  onst char *zEnd;
66000 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68 61 72       /* One char
66010 61 63 74 65 72 20 70 61 73 74 20 74 68 65 20 65  acter past the e
66020 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74  nd of input text
66030 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
66040 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
66050 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20  s structure can 
66060 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c 69  hold a simple li
66070 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72  st of identifier
66080 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 68  s,.** such as th
66090 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 69  e list "a,b,c" i
660a0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
660b0 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  statements:.**.*
660c0 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  *      INSERT IN
660d0 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c 55  TO t(a,b,c) VALU
660e0 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20  ES ...;.**      
660f0 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 78  CREATE INDEX idx
66100 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a   ON t(a,b,c);.**
66110 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49        CREATE TRI
66120 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 45  GGER trig BEFORE
66130 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c 62   UPDATE ON t(a,b
66140 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54  ,c) ...;.**.** T
66150 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 20  he IdList.a.idx 
66160 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 68  field is used wh
66170 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 65  en the IdList re
66180 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 73  presents the lis
66190 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e  t of.** column n
661a0 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 62  ames after a tab
661b0 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e  le name in an IN
661c0 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  SERT statement. 
661d0 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   In the statemen
661e0 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45  t.**.**     INSE
661f0 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29  RT INTO t(a,b,c)
66200 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61   ....**.** If "a
66210 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 6f  " is the k-th co
66220 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 74  lumn of table "t
66230 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e 61  ", then IdList.a
66240 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73  [0].idx==k..*/.s
66250 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a 20  truct IdList {. 
66260 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69   struct IdList_i
66270 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a  tem {.    char *
66280 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e  zName;      /* N
66290 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e 74  ame of the ident
662a0 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  ifier */.    int
662b0 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f   idx;          /
662c0 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20  * Index in some 
662d0 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20  Table.aCol[] of 
662e0 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a  a column named z
662f0 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a  Name */.  } *a;.
66300 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20    int nId;      
66310 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
66320 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 74  identifiers on t
66330 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  he list */.  int
66340 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a   nAlloc;      /*
66350 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
66360 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
66370 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b   a[] below */.};
66380 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d  ../*.** The bitm
66390 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 66  ask datatype def
663a0 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 73  ined below is us
663b0 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 6f  ed for various o
663c0 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a  ptimizations..**
663d0 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 69  .** Changing thi
663e0 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 20  s from a 64-bit 
663f0 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 65  to a 32-bit type
66400 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d 62   limits the numb
66410 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20  er of.** tables 
66420 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20  in a join to 32 
66430 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 20  instead of 64.  
66440 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 75  But it also redu
66450 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20  ces the size.** 
66460 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 62  of the library b
66470 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 69  y 738 bytes on i
66480 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  x86..*/.typedef 
66490 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a  u64 Bitmask;../*
664a0 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  .** The number o
664b0 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d  f bits in a Bitm
664c0 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e  ask.  "BMS" mean
664d0 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 22  s "BitMask Size"
664e0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53  ..*/.#define BMS
664f0 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 28    ((int)(sizeof(
66500 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f 2a  Bitmask)*8))../*
66510 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
66520 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63  g structure desc
66530 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 63  ribes the FROM c
66540 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
66550 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  T statement..** 
66560 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 75  Each table or su
66570 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52  bquery in the FR
66580 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73  OM clause is a s
66590 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 20  eparate element 
665a0 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 73  of.** the SrcLis
665b0 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a  t.a[] array..**.
665c0 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 69  ** With the addi
665d0 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65  tion of multiple
665e0 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
665f0 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
66600 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 61   structure.** ca
66610 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74  n also be used t
66620 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72  o describe a par
66630 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 75  ticular table su
66640 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 20  ch as the table 
66650 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66  that.** is modif
66660 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54  ied by an INSERT
66670 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44  , DELETE, or UPD
66680 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ATE statement.  
66690 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c 2c  In standard SQL,
666a0 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c 65  .** such a table
666b0 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 6c   must be a simpl
666c0 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 74  e name: ID.  But
666d0 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 20   in SQLite, the 
666e0 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77  table can.** now
666f0 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 62   be identified b
66700 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 6d  y a database nam
66710 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 74  e, a dot, then t
66720 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49  he table name: I
66730 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  D.ID..**.** The 
66740 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 20  jointype starts 
66750 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 20  out showing the 
66760 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 65  join type betwee
66770 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  n the current ta
66780 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  ble.** and the n
66790 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 65  ext table on the
667a0 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 73   list.  The pars
667b0 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c 69  er builds the li
667c0 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20  st this way..** 
667d0 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c 69  But sqlite3SrcLi
667e0 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
667f0 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 74  ) later shifts t
66800 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20  he jointypes so 
66810 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69  that each.** joi
66820 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 20  ntype expresses 
66830 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e  the join between
66840 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74   the table and t
66850 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 6c  he previous tabl
66860 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 63  e..*/.struct Src
66870 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 72  List {.  i16 nSr
66880 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  c;        /* Num
66890 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f 72  ber of tables or
668a0 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74   subqueries in t
668b0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
668c0 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20  /.  i16 nAlloc; 
668d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
668e0 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
668f0 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77  ted in a[] below
66900 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
66910 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20  List_item {.    
66920 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b  char *zDatabase;
66930 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
66940 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68  abase holding th
66950 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  is table */.    
66960 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
66970 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
66980 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68   table */.    ch
66990 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20  ar *zAlias;     
669a0 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 20  /* The "B" part 
669b0 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 68  of a "A AS B" ph
669c0 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20  rase.  zName is 
669d0 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 54  the "A" */.    T
669e0 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
669f0 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c 65   /* An SQL table
66a00 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
66a10 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53  o zName */.    S
66a20 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
66a30 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
66a40 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 70  tement used in p
66a50 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20  lace of a table 
66a60 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 69  name */.    u8 i
66a70 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a  sPopulated;   /*
66a80 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   Temporary table
66a90 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
66aa0 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 6c   SELECT is popul
66ab0 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a  ated */.    u8 j
66ac0 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f 2a  ointype;      /*
66ad0 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65   Type of join be
66ae0 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 20  tween this able 
66af0 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73  and the previous
66b00 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49 6e   */.    u8 notIn
66b10 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72 75  dexed;    /* Tru
66b20 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  e if there is a 
66b30 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75  NOT INDEXED clau
66b40 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  se */.    int iC
66b50 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54  ursor;      /* T
66b60 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  he VDBE cursor n
66b70 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63  umber used to ac
66b80 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20  cess this table 
66b90 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e  */.    Expr *pOn
66ba0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
66bb0 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  ON clause of a j
66bc0 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73  oin */.    IdLis
66bd0 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20  t *pUsing;   /* 
66be0 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  The USING clause
66bf0 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20   of a join */.  
66c00 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65    Bitmask colUse
66c10 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c  d;  /* Bit N (1<
66c20 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d  <N) set if colum
66c30 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20 75  n N of pTab is u
66c40 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  sed */.    char 
66c50 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20  *zIndex;     /* 
66c60 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d 20  Identifier from 
66c70 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 6e  "INDEXED BY <zIn
66c80 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f 0a  dex>" clause */.
66c90 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65      Index *pInde
66ca0 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73  x;    /* Index s
66cb0 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 70  tructure corresp
66cc0 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 78  onding to zIndex
66cd0 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d 20  , if any */.  } 
66ce0 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20  a[1];           
66cf0 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66    /* One entry f
66d00 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 69  or each identifi
66d10 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a  er on the list *
66d20 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d  /.};../*.** Perm
66d30 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 20  itted values of 
66d40 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f  the SrcList.a.jo
66d50 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a  intype field.*/.
66d60 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52  #define JT_INNER
66d70 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 2f       0x0001    /
66d80 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e  * Any kind of in
66d90 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69  ner or cross joi
66da0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f  n */.#define JT_
66db0 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 32  CROSS     0x0002
66dc0 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 20      /* Explicit 
66dd0 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 53  use of the CROSS
66de0 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66   keyword */.#def
66df0 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20  ine JT_NATURAL  
66e00 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 72   0x0004    /* Tr
66e10 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 61  ue for a "natura
66e20 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69  l" join */.#defi
66e30 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 20  ne JT_LEFT      
66e40 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 66  0x0008    /* Lef
66e50 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a  t outer join */.
66e60 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 54  #define JT_RIGHT
66e70 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 2f       0x0010    /
66e80 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a 6f  * Right outer jo
66e90 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
66ea0 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 32  _OUTER     0x002
66eb0 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 54  0    /* The "OUT
66ec0 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 70  ER" keyword is p
66ed0 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  resent */.#defin
66ee0 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 30  e JT_ERROR     0
66ef0 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e  x0040    /* unkn
66f00 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  own or unsupport
66f10 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a  ed join type */.
66f20 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 50  ../*.** A WhereP
66f30 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73  lan object holds
66f40 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
66f50 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c 6f  t describes a lo
66f60 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67 79  okup.** strategy
66f70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a  ..**.** This obj
66f80 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ect is intended 
66f90 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75 74  to be opaque out
66fa0 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65 72  side of the wher
66fb0 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 49  e.c module..** I
66fc0 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68 65  t is included he
66fd0 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74 20  re only so that 
66fe0 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77 69  that compiler wi
66ff0 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20  ll know how big 
67000 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 20  it.** is.  None 
67010 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69 6e  of the fields in
67020 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68 6f   this object sho
67030 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74 73  uld be used outs
67040 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77 68  ide of.** the wh
67050 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a  ere.c module..**
67060 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 75  .** Within the u
67070 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e  nion, pIdx is on
67080 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46  ly used when wsF
67090 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 58  lags&WHERE_INDEX
670a0 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 70  ED is true..** p
670b0 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65  Term is only use
670c0 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57  d when wsFlags&W
670d0 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73  HERE_MULTI_OR is
670e0 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 61   true.  And pVta
670f0 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20  bIdx.** is only 
67100 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67  used when wsFlag
67110 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  s&WHERE_VIRTUALT
67120 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20 49  ABLE is true.  I
67130 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a 2a  t is never the.*
67140 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72 65  * case that more
67150 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65   than one of the
67160 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73  se conditions is
67170 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74   true..*/.struct
67180 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20 75   WherePlan {.  u
67190 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20 20  32 wsFlags;     
671a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
671b0 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 74   WHERE_* flags t
671c0 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65  hat describe the
671d0 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 75   strategy */.  u
671e0 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20  32 nEq;         
671f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
67200 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f   Number of == co
67210 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 75  nstraints */.  u
67220 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65 78  nion {.    Index
67230 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
67240 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
67250 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49 4e  ex when WHERE_IN
67260 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a 2f  DEXED is true */
67270 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65 72  .    struct Wher
67280 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20  eTerm *pTerm;   
67290 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61      /* WHERE cla
672a0 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52 2d  use term for OR-
672b0 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73 71  search */.    sq
672c0 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
672d0 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a 20   *pVtabIdx;  /* 
672e0 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  Virtual table in
672f0 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20  dex to use */.  
67300 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  } u;.};../*.** F
67310 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20 6c  or each nested l
67320 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20 63  oop in a WHERE c
67330 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74 61  lause implementa
67340 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65 49  tion, the WhereI
67350 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  nfo.** structure
67360 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67   contains a sing
67370 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  le instance of t
67380 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20  his structure.  
67390 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  This structure.*
673a0 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  * is intended to
673b0 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65 20   be private the 
673c0 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75  the where.c modu
673d0 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f  le and should no
673e0 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20 6f  t be.** access o
673f0 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f 74  r modified by ot
67400 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a  her modules..**.
67410 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20  ** The pIdxInfo 
67420 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f  field is used to
67430 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 62   help pick the b
67440 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a 2a  est index on a.*
67450 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  * virtual table.
67460 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 70    The pIdxInfo p
67470 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 20  ointer contains 
67480 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f  indexing.** info
67490 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  rmation for the 
674a0 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 68  i-th table in th
674b0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 65  e FROM clause be
674c0 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 2e  fore reordering.
674d0 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 78  .** All the pIdx
674e0 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 72  Info pointers ar
674f0 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 65  e freed by where
67500 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 68  InfoFree() in wh
67510 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f 74  ere.c..** All ot
67520 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  her information 
67530 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65 72  in the i-th Wher
67540 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66 6f  eLevel object fo
67550 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65  r the i-th table
67560 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20 63  .** after FROM c
67570 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 0a  lause ordering..
67580 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4c  */.struct WhereL
67590 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50 6c  evel {.  WherePl
675a0 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20 2f  an plan;       /
675b0 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72  * query plan for
675c0 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f 66   this element of
675d0 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
675e0 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 4a   */.  int iLeftJ
675f0 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  oin;        /* M
67600 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20  emory cell used 
67610 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 46  to implement LEF
67620 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f 0a  T OUTER JOIN */.
67630 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 20    int iTabCur;  
67640 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
67650 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20  DBE cursor used 
67660 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 61  to access the ta
67670 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64  ble */.  int iId
67680 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f  xCur;          /
67690 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
676a0 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  r used to access
676b0 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 61   pIdx */.  int a
676c0 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 20  ddrBrk;         
676d0 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
676e0 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68   break out of th
676f0 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  e loop */.  int 
67700 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 20  addrNxt;        
67710 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
67720 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 74  o start the next
67730 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20   IN combination 
67740 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e  */.  int addrCon
67750 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  t;         /* Ju
67760 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69  mp here to conti
67770 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78  nue with the nex
67780 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a  t loop cycle */.
67790 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 3b    int addrFirst;
677a0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
677b0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20   instruction of 
677c0 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 20  interior of the 
677d0 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46 72  loop */.  u8 iFr
677e0 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  om;             
677f0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69  /* Which entry i
67800 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
67810 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70 35  e */.  u8 op, p5
67820 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
67830 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f 66  Opcode and P5 of
67840 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74   the opcode that
67850 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a   ends the loop *
67860 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b 20  /.  int p1, p2; 
67870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
67880 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70 63  rands of the opc
67890 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64 73  ode used to ends
678a0 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75   the loop */.  u
678b0 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20  nion {          
678c0 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
678d0 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64 73  ion that depends
678e0 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73   on plan.wsFlags
678f0 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 7b   */.    struct {
67900 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20  .      int nIn; 
67910 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67920 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
67930 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a  s in aInLoop[] *
67940 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49  /.      struct I
67950 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 20  nLoop {.        
67960 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20  int iCur;       
67970 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
67980 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 62  BE cursor used b
67990 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 74  y this IN operat
679a0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  or */.        in
679b0 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 20  t addrInTop;    
679c0 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74       /* Top of t
679d0 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20  he IN loop */.  
679e0 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20      } *aInLoop; 
679f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66            /* Inf
67a00 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65  ormation about e
67a10 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f 70  ach nested IN op
67a20 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 20  erator */.    } 
67a30 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  in;             
67a40 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
67a50 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48   plan.wsFlags&WH
67a60 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20  ERE_IN_ABLE */. 
67a70 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20   } u;..  /* The 
67a80 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20  following field 
67a90 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61  is really not pa
67aa0 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  rt of the curren
67ab0 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20  t level.  But.  
67ac0 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61  ** we need a pla
67ad0 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 74  ce to cache virt
67ae0 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20  ual table index 
67af0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
67b00 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61  each.  ** virtua
67b10 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  l table in the F
67b20 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74  ROM clause and t
67b30 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74  he WhereLevel st
67b40 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20  ructure is.  ** 
67b50 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61  a convenient pla
67b60 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69  ce since there i
67b70 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c  s one WhereLevel
67b80 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63   for each FROM c
67b90 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65  lause.  ** eleme
67ba0 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  nt..  */.  sqlit
67bb0 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
67bc0 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64  IdxInfo;  /* Ind
67bd0 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68  ex info for n-th
67be0 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
67bf0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73  .};../*.** Flags
67c00 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
67c10 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20   the wctrlFlags 
67c20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c  parameter of sql
67c30 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
67c40 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72  .** and the Wher
67c50 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73  eInfo.wctrlFlags
67c60 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66   member..*/.#def
67c70 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42  ine WHERE_ORDERB
67c80 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30  Y_NORMAL   0x000
67c90 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64  0 /* No-op */.#d
67ca0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
67cb0 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30  RBY_MIN      0x0
67cc0 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20  001 /* ORDER BY 
67cd0 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d  processing for m
67ce0 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65  in() func */.#de
67cf0 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52  fine WHERE_ORDER
67d00 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30  BY_MAX      0x00
67d10 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70  02 /* ORDER BY p
67d20 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61  rocessing for ma
67d30 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66  x() func */.#def
67d40 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53  ine WHERE_ONEPAS
67d50 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30  S_DESIRED  0x000
67d60 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20  4 /* Want to do 
67d70 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f  one-pass UPDATE/
67d80 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e  DELETE */.#defin
67d90 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  e WHERE_DUPLICAT
67da0 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 20  ES_OK    0x0008 
67db0 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20  /* Ok to return 
67dc0 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20  a row more than 
67dd0 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  once */.#define 
67de0 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20  WHERE_OMIT_OPEN 
67df0 20 20 20 20 20 20 20 30 78 30 30 31 30 20 2f 2a         0x0010 /*
67e00 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 72   Table cursor ar
67e10 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a  e already open *
67e20 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
67e30 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 20  OMIT_CLOSE      
67e40 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 74 20   0x0020 /* Omit 
67e50 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20 26  close of table &
67e60 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a   index cursors *
67e70 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
67e80 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 20  FORCE_TABLE     
67e90 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 6e 6f   0x0040 /* Do no
67ea0 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f  t use an index-o
67eb0 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f  nly search */../
67ec0 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 63  *.** The WHERE c
67ed0 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
67ee0 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 6f   routine has two
67ef0 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a 2a   halves.  The.**
67f00 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 73   first part does
67f10 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
67f20 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e 64  e WHERE loop and
67f30 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 68   the second.** h
67f40 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 69  alf does the tai
67f50 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c  l of the WHERE l
67f60 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e 63  oop.  An instanc
67f70 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72  e of.** this str
67f80 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 6e  ucture is return
67f90 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20  ed by the first 
67fa0 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 0a  half and passed.
67fb0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 6f  ** into the seco
67fc0 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 20  nd half to give 
67fd0 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 2e  some continuity.
67fe0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65  .*/.struct Where
67ff0 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 2a  Info {.  Parse *
68000 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a  pParse;       /*
68010 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
68020 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
68030 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 77 63  text */.  u16 wc
68040 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 2f  trlFlags;      /
68050 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61 6c  * Flags original
68060 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  ly passed to sql
68070 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
68080 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 61   */.  u8 okOnePa
68090 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6b  ss;        /* Ok
680a0 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 73 73   to use one-pass
680b0 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 55   algorithm for U
680c0 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
680d0 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
680e0 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20  abList;         
680f0 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
68100 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69  ables in the joi
68110 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b  n */.  int iTop;
68120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68130 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72        /* The ver
68140 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  y beginning of t
68150 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f  he WHERE loop */
68160 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65  .  int iContinue
68170 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
68180 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
68190 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
681a0 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20  next record */. 
681b0 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20   int iBreak;    
681c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
681d0 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
681e0 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
681f0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e   loop */.  int n
68200 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20  Level;          
68210 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
68220 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f  ber of nested lo
68230 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 57  op */.  struct W
68240 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b  hereClause *pWC;
68250 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70         /* Decomp
68260 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57  osition of the W
68270 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
68280 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d   WhereLevel a[1]
68290 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
682a0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
682b0 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 6c  bout each nest l
682c0 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a  oop in WHERE */.
682d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65  };../*.** A Name
682e0 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 20  Context defines 
682f0 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69  a context in whi
68300 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 61  ch to resolve ta
68310 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a  ble and column.*
68320 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f  * names.  The co
68330 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f  ntext consists o
68340 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c  f a list of tabl
68350 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 74  es (the pSrcList
68360 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61  ) field and.** a
68370 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65   list of named e
68380 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73  xpression (pELis
68390 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65  t).  The named e
683a0 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d  xpression list m
683b0 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20  ay.** be NULL.  
683c0 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70  The pSrc corresp
683d0 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d  onds to the FROM
683e0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c   clause of a SEL
683f0 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65  ECT or.** to the
68400 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 65   table being ope
68410 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 45  rated on by INSE
68420 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44  RT, UPDATE, or D
68430 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70  ELETE.  The.** p
68440 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64  EList correspond
68450 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20  s to the result 
68460 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20  set of a SELECT 
68470 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a  and is NULL for.
68480 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65  ** other stateme
68490 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43  nts..**.** NameC
684a0 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 6e  ontexts can be n
684b0 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 73  ested.  When res
684c0 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68  olving names, th
684d0 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a  e inner-most .**
684e0 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 72   context is sear
684f0 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 20  ched first.  If 
68500 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e  no match is foun
68510 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 65  d, the next oute
68520 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20  r.** context is 
68530 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 65  checked.  If the
68540 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d  re is still no m
68550 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 63  atch, the next c
68560 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 65  ontext.** is che
68570 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f 63  cked.  This proc
68580 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e  ess continues un
68590 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 74  til either a mat
685a0 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f  ch is found.** o
685b0 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61  r all contexts a
685c0 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e 20  re check.  When 
685d0 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  a match is found
685e0 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 65  , the nRef membe
685f0 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  r of.** the cont
68600 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ext containing t
68610 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 72  he match is incr
68620 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20  emented. .**.** 
68630 45 61 63 68 20 73 75 62 71 75 65 72 79 20 67 65  Each subquery ge
68640 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e  ts a new NameCon
68650 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 74  text.  The pNext
68660 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f   field points to
68670 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74   the.** NameCont
68680 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 6e  ext in the paren
68690 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 74  t query.  Thus t
686a0 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 63  he process of sc
686b0 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61  anning the.** Na
686c0 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63  meContext list c
686d0 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65  orresponds to se
686e0 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20  arching through 
686f0 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74  successively out
68700 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73  er.** subqueries
68710 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d   looking for a m
68720 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  atch..*/.struct 
68730 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20  NameContext {.  
68740 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
68750 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
68760 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  er */.  SrcList 
68770 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20  *pSrcList;   /* 
68780 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c  One or more tabl
68790 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c  es used to resol
687a0 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78  ve names */.  Ex
687b0 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
687c0 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c     /* Optional l
687d0 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70  ist of named exp
687e0 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ressions */.  in
687f0 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
68800 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
68810 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 62  names resolved b
68820 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 2a  y this context *
68830 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20  /.  int nErr;   
68840 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
68850 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63  er of errors enc
68860 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 72  ountered while r
68870 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a  esolving names *
68880 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b  /.  u8 allowAgg;
68890 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72           /* Aggr
688a0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
688b0 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a  allowed here */.
688c0 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 20    u8 hasAgg;    
688d0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
688e0 66 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65  f aggregates are
688f0 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73   seen */.  u8 is
68900 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 20  Check;          
68910 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f 6c  /* True if resol
68920 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 20  ving names in a 
68930 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74  CHECK constraint
68940 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 68   */.  int nDepth
68950 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65  ;          /* De
68960 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 20  pth of subquery 
68970 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72  recursion. 1 for
68980 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f   no recursion */
68990 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  .  AggInfo *pAgg
689a0 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72  Info;   /* Infor
689b0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 67  mation about agg
689c0 72 65 67 61 74 65 73 20 61 74 20 74 68 69 73 20  regates at this 
689d0 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43  level */.  NameC
689e0 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 20  ontext *pNext;  
689f0 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e 61  /* Next outer na
68a00 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c  me context.  NUL
68a10 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20  L for outermost 
68a20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
68a30 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
68a40 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
68a50 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  ure contains all
68a60 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
68a70 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 61  needed to genera
68a80 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69  te code for a si
68a90 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74  ngle SELECT stat
68aa0 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69  ement..**.** nLi
68ab0 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d 31  mit is set to -1
68ac0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
68ad0 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 6e  LIMIT clause.  n
68ae0 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f  Offset is set to
68af0 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20   0..** If there 
68b00 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  is a LIMIT claus
68b10 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 65  e, the parser se
68b20 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65  ts nLimit to the
68b30 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a   value of the.**
68b40 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73   limit and nOffs
68b50 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  et to the value 
68b60 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 6f  of the offset (o
68b70 72 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20  r 0 if there is 
68b80 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20  not.** offset). 
68b90 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e   But later on, n
68ba0 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65  Limit and nOffse
68bb0 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d  t become the mem
68bc0 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a  ory locations.**
68bd0 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 61   in the VDBE tha
68be0 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 6d  t record the lim
68bf0 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 6f  it and offset co
68c00 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64  unters..**.** ad
68c10 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74  drOpenEphm[] ent
68c20 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65  ries contain the
68c30 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f   address of OP_O
68c40 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63  penEphemeral opc
68c50 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61  odes..** These a
68c60 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 65  ddresses must be
68c70 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 20   stored so that 
68c80 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 61  we can go back a
68c90 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68  nd fill in.** th
68ca0 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64  e P4_KEYINFO and
68cb0 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 6c   P2 parameters l
68cc0 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 74  ater.  Neither t
68cd0 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a  he KeyInfo nor.*
68ce0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
68cf0 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 61  columns in P2 ca
68d00 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74  n be computed at
68d10 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a   the same time.*
68d20 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e  * as the OP_Open
68d30 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  Ephm instruction
68d40 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 73   is coded becaus
68d50 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20  e not.** enough 
68d60 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
68d70 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71  t the compound q
68d80 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74  uery is known at
68d90 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20   that point..** 
68da0 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20  The KeyInfo for 
68db0 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20  addrOpenTran[0] 
68dc0 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73  and [1] contains
68dd0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
68de0 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20  nces.** for the 
68df0 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65  result set.  The
68e00 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64   KeyInfo for add
68e10 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e  rOpenTran[2] con
68e20 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a  tains collating.
68e30 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72  ** sequences for
68e40 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
68e50 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ause..*/.struct 
68e60 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 4c  Select {.  ExprL
68e70 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20  ist *pEList;    
68e80 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 20    /* The fields 
68e90 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f  of the result */
68ea0 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20  .  u8 op;       
68eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
68ec0 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b   of: TK_UNION TK
68ed0 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43  _ALL TK_INTERSEC
68ee0 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20  T TK_EXCEPT */. 
68ef0 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20   char affinity; 
68f00 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52          /* MakeR
68f10 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 20  ecord with this 
68f20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 54  affinity for SRT
68f30 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 65  _Set */.  u16 se
68f40 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  lFlags;         
68f50 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a   /* Various SF_*
68f60 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63   values */.  Src
68f70 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20  List *pSrc;     
68f80 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20      /* The FROM 
68f90 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
68fa0 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20   *pWhere;       
68fb0 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
68fc0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
68fd0 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20  List *pGroupBy; 
68fe0 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20     /* The GROUP 
68ff0 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  BY clause */.  E
69000 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20  xpr *pHaving;   
69010 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56        /* The HAV
69020 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ING clause */.  
69030 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
69040 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  By;    /* The OR
69050 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
69060 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f  .  Select *pPrio
69070 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69  r;        /* Pri
69080 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63  or select in a c
69090 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73  ompound select s
690a0 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65  tatement */.  Se
690b0 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20  lect *pNext;    
690c0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c       /* Next sel
690d0 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20  ect to the left 
690e0 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f  in a compound */
690f0 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68  .  Select *pRigh
69100 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67  tmost;    /* Rig
69110 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69  ht-most select i
69120 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  n a compound sel
69130 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ect statement */
69140 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b  .  Expr *pLimit;
69150 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d            /* LIM
69160 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  IT expression. N
69170 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
69180 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ed. */.  Expr *p
69190 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
691a0 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 73  /* OFFSET expres
691b0 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73  sion. NULL means
691c0 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20   not used. */.  
691d0 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66  int iLimit, iOff
691e0 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79  set;   /* Memory
691f0 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69   registers holdi
69200 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 45  ng LIMIT & OFFSE
69210 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  T counters */.  
69220 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 6d  int addrOpenEphm
69230 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65  [3];   /* OP_Ope
69240 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 72  nEphem opcodes r
69250 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 73  elated to this s
69260 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  elect */.};../*.
69270 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
69280 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c  s for Select.sel
69290 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 22  Flags.  The "SF"
692a0 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 66   prefix stands f
692b0 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c  or.** "Select Fl
692c0 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ag"..*/.#define 
692d0 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20  SF_Distinct     
692e0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75     0x0001  /* Ou
692f0 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44  tput should be D
69300 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69  ISTINCT */.#defi
69310 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20  ne SF_Resolved  
69320 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a        0x0002  /*
69330 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 76   Identifiers hav
69340 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20  e been resolved 
69350 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67  */.#define SF_Ag
69360 67 72 65 67 61 74 65 20 20 20 20 20 20 20 30 78  gregate       0x
69370 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e  0004  /* Contain
69380 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  s aggregate func
69390 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65  tions */.#define
693a0 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61   SF_UsesEphemera
693b0 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55  l   0x0008  /* U
693c0 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65  ses the OpenEphe
693d0 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a  meral opcode */.
693e0 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e  #define SF_Expan
693f0 64 65 64 20 20 20 20 20 20 20 20 30 78 30 30 31  ded        0x001
69400 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c  0  /* sqlite3Sel
69410 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c  ectExpand() call
69420 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64  ed on this */.#d
69430 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65  efine SF_HasType
69440 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 20  Info     0x0020 
69450 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72   /* FROM subquer
69460 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d  ies have Table m
69470 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a  etadata */.../*.
69480 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f  ** The results o
69490 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 62  f a select can b
694a0 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e  e distributed in
694b0 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 20   several ways.  
694c0 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 65  The.** "SRT" pre
694d0 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 43  fix means "SELEC
694e0 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e 0a  T Result Type"..
694f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 55  */.#define SRT_U
69500 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 2f  nion        1  /
69510 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61  * Store result a
69520 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64  s keys in an ind
69530 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  ex */.#define SR
69540 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 32  T_Except       2
69550 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 75    /* Remove resu
69560 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20  lt from a UNION 
69570 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65  index */.#define
69580 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20   SRT_Exists     
69590 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 20    3  /* Store 1 
695a0 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  if the result is
695b0 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64   not empty */.#d
695c0 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72  efine SRT_Discar
695d0 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f 20  d      4  /* Do 
695e0 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 73  not save the res
695f0 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f  ults anywhere */
69600 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42  ../* The ORDER B
69610 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f  Y clause is igno
69620 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74  red for all of t
69630 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66  he above */.#def
69640 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64  ine IgnorableOrd
69650 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 65  erby(X) ((X->eDe
69660 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 64  st)<=SRT_Discard
69670 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f  )..#define SRT_O
69680 75 74 70 75 74 20 20 20 20 20 20 20 35 20 20 2f  utput       5  /
69690 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f  * Output each ro
696a0 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23  w of result */.#
696b0 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20  define SRT_Mem  
696c0 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74          6  /* St
696d0 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ore result in a 
696e0 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23  memory cell */.#
696f0 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20  define SRT_Set  
69700 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74          7  /* St
69710 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b  ore results as k
69720 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20  eys in an index 
69730 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54  */.#define SRT_T
69740 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f  able        8  /
69750 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61  * Store result a
69760 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61  s data with an a
69770 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a  utomatic rowid *
69780 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70  /.#define SRT_Ep
69790 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a  hemTab     9  /*
697a0 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e   Create transien
697b0 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20  t tab and store 
697c0 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a  like SRT_Table *
697d0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 6f  /.#define SRT_Co
697e0 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f 2a  routine   10  /*
697f0 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67   Generate a sing
69800 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74  le row of result
69810 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72   */../*.** A str
69820 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 63  ucture used to c
69830 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 68  ustomize the beh
69840 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33  avior of sqlite3
69850 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a  Select(). See.**
69860 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20   comments above 
69870 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20  sqlite3Select() 
69880 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
69890 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
698a0 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 74  electDest Select
698b0 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 6c  Dest;.struct Sel
698c0 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65  ectDest {.  u8 e
698d0 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Dest;         /*
698e0 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20   How to dispose 
698f0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a  of the results *
69900 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 3b  /.  u8 affinity;
69910 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74        /* Affinit
69920 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 73  y used when eDes
69930 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20  t==SRT_Set */.  
69940 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 20  int iParm;      
69950 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 72    /* A parameter
69960 20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 65   used by the eDe
69970 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68  st disposal meth
69980 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d  od */.  int iMem
69990 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73  ;         /* Bas
699a0 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65  e register where
699b0 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 69   results are wri
699c0 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  tten */.  int nM
699d0 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  em;         /* N
699e0 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65  umber of registe
699f0 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  rs allocated */.
69a00 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67  };../*.** During
69a10 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e   code generation
69a20 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20 74   of statements t
69a30 68 61 74 20 64 6f 20 69 6e 73 65 72 74 73 20 69  hat do inserts i
69a40 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  nto AUTOINCREMEN
69a50 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c 20 74 68  T .** tables, th
69a60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f  e following info
69a70 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 74 61 63  rmation is attac
69a80 68 65 64 20 74 6f 20 74 68 65 20 54 61 62 6c 65  hed to the Table
69a90 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a 2a 2a 20  .u.autoInc.p.** 
69aa0 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 63 68 20  pointer of each 
69ab0 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61  autoincrement ta
69ac0 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 73 6f  ble to record so
69ad0 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 6d 61 74  me side informat
69ae0 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ion that.** the 
69af0 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 6e  code generator n
69b00 65 65 64 73 2e 20 20 57 65 20 68 61 76 65 20 74  eeds.  We have t
69b10 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 62 6c 65  o keep per-table
69b20 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a   autoincrement.*
69b30 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  * information in
69b40 20 63 61 73 65 20 69 6e 73 65 72 74 73 20 61 72   case inserts ar
69b50 65 20 64 6f 77 6e 20 77 69 74 68 69 6e 20 74 72  e down within tr
69b60 69 67 67 65 72 73 2e 20 20 54 72 69 67 67 65 72  iggers.  Trigger
69b70 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d  s do not.** norm
69b80 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 74 65 20  ally coordinate 
69b90 74 68 65 69 72 20 61 63 74 69 76 69 74 69 65 73  their activities
69ba0 2c 20 62 75 74 20 77 65 20 64 6f 20 6e 65 65 64  , but we do need
69bb0 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 74   to coordinate t
69bc0 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 61 6e  he.** loading an
69bd0 64 20 73 61 76 69 6e 67 20 6f 66 20 61 75 74 6f  d saving of auto
69be0 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 6f 72 6d  increment inform
69bf0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ation..*/.struct
69c00 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 7b 0a 20   AutoincInfo {. 
69c10 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 4e   AutoincInfo *pN
69c20 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 69  ext;   /* Next i
69c30 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 61 20 6c  nfo block in a l
69c40 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20  ist of them all 
69c50 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
69c60 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  ;          /* Ta
69c70 62 6c 65 20 74 68 69 73 20 69 6e 66 6f 20 62 6c  ble this info bl
69c80 6f 63 6b 20 72 65 66 65 72 73 20 74 6f 20 2a 2f  ock refers to */
69c90 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
69ca0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
69cb0 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62  x in sqlite3.aDb
69cc0 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 65 20 68  [] of database h
69cd0 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20  olding pTab */. 
69ce0 20 69 6e 74 20 72 65 67 43 74 72 3b 20 20 20 20   int regCtr;    
69cf0 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
69d00 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   register holdin
69d10 67 20 74 68 65 20 72 6f 77 69 64 20 63 6f 75 6e  g the rowid coun
69d20 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ter */.};../*.**
69d30 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 6c   Size of the col
69d40 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a 23 69 66  umn cache.*/.#if
69d50 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f 43 4f  ndef SQLITE_N_CO
69d60 4c 43 41 43 48 45 0a 23 20 64 65 66 69 6e 65 20  LCACHE.# define 
69d70 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
69d80 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  E 10.#endif../*.
69d90 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f 6e 65 20  ** At least one 
69da0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
69db0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
69dc0 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 66  ure is created f
69dd0 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 72 69 67  or each .** trig
69de0 67 65 72 20 74 68 61 74 20 6d 61 79 20 62 65 20  ger that may be 
69df0 66 69 72 65 64 20 77 68 69 6c 65 20 70 61 72 73  fired while pars
69e00 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 2c 20 55  ing an INSERT, U
69e10 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 0a  PDATE or DELETE.
69e20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 41 6c  ** statement. Al
69e30 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 73 20 61  l such objects a
69e40 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  re stored in the
69e50 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61   linked list hea
69e60 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 73 65 2e  ded at.** Parse.
69e70 70 54 72 69 67 67 65 72 50 72 67 20 61 6e 64 20  pTriggerPrg and 
69e80 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 73 74 61  deleted once sta
69e90 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61 74 69  tement compilati
69ea0 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 63  on has been.** c
69eb0 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ompleted..**.** 
69ec0 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f 67 72  A Vdbe sub-progr
69ed0 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  am that implemen
69ee0 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e 64 20  ts the body and 
69ef0 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 74  WHEN clause of t
69f00 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 67 67 65  rigger.** Trigge
69f10 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c 20 61  rPrg.pTrigger, a
69f20 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61 75 6c  ssuming a defaul
69f30 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c  t ON CONFLICT cl
69f40 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 69 67 67  ause of.** Trigg
69f50 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20 69 73  erPrg.orconf, is
69f60 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 54   stored in the T
69f70 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f 67 72  riggerPrg.pProgr
69f80 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 20  am variable..** 
69f90 54 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 67  The Parse.pTrigg
69fa0 65 72 50 72 67 20 6c 69 73 74 20 6e 65 76 65 72  erPrg list never
69fb0 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e   contains two en
69fc0 74 72 69 65 73 20 77 69 74 68 20 74 68 65 20 73  tries with the s
69fd0 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 20 66 6f  ame.** values fo
69fe0 72 20 62 6f 74 68 20 70 54 72 69 67 67 65 72 20  r both pTrigger 
69ff0 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a 2a 0a 2a  and orconf..**.*
6a000 2a 20 54 68 65 20 54 72 69 67 67 65 72 50 72 67  * The TriggerPrg
6a010 2e 6f 6c 64 6d 61 73 6b 20 76 61 72 69 61 62 6c  .oldmask variabl
6a020 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6d 61  e is set to a ma
6a030 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c 75  sk of old.* colu
6a040 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 64 20  mns.** accessed 
6a050 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 6f 72  (or set to 0 for
6a060 20 74 72 69 67 67 65 72 73 20 66 69 72 65 64 20   triggers fired 
6a070 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 49  as a result of I
6a080 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 65 6d  NSERT .** statem
6a090 65 6e 74 73 29 2e 0a 2a 2f 0a 73 74 72 75 63 74  ents)..*/.struct
6a0a0 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 20   TriggerPrg {.  
6a0b0 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
6a0c0 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67  r;      /* Trigg
6a0d0 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d 20  er this program 
6a0e0 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 2a  was coded from *
6a0f0 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20  /.  int orconf; 
6a100 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
6a110 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49  efault ON CONFLI
6a120 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 53  CT policy */.  S
6a130 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67  ubProgram *pProg
6a140 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 72 61  ram;   /* Progra
6a150 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 70  m implementing p
6a160 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 20 2a  Trigger/orconf *
6a170 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b  /.  u32 oldmask;
6a180 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
6a190 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c  ask of old.* col
6a1a0 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20 2a 2f  umns accessed */
6a1b0 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70  .  TriggerPrg *p
6a1c0 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65  Next;      /* Ne
6a1d0 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 72 73  xt entry in Pars
6a1e0 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c 69  e.pTriggerPrg li
6a1f0 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  st */.};../*.** 
6a200 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f  An SQL parser co
6a210 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f  ntext.  A copy o
6a220 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
6a230 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75   is passed throu
6a240 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72  gh.** the parser
6a250 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61   and down into a
6a260 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63  ll the parser ac
6a270 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20  tion routine in 
6a280 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72  order to.** carr
6a290 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61  y around informa
6a2a0 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f  tion that is glo
6a2b0 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72  bal to the entir
6a2c0 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  e parse..**.** T
6a2d0 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  he structure is 
6a2e0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f  divided into two
6a2f0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68   parts.  When th
6a300 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64  e parser and cod
6a310 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61  e.** generate ca
6a320 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65  ll themselves re
6a330 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66  cursively, the f
6a340 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65  irst part of the
6a350 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73   structure.** is
6a360 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68   constant but th
6a370 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73  e second part is
6a380 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65   reset at the be
6a390 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20  ginning and end 
6a3a0 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72  of.** each recur
6a3b0 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sion..**.** The 
6a3c0 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61  nTableLock and a
6a3d0 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62  TableLock variab
6a3e0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  les are only use
6a3f0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d  d if the shared-
6a400 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72  cache .** featur
6a410 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66  e is enabled (if
6a420 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75   sqlite3Tsd()->u
6a430 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20  seSharedData is 
6a440 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a  true). They are.
6a450 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ** used to store
6a460 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
6a470 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64  e-locks required
6a480 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
6a490 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69  t being.** compi
6a4a0 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71  led. Function sq
6a4b0 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29  lite3TableLock()
6a4c0 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
6a4d0 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a  entries to the.*
6a4e0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63  * list..*/.struc
6a4f0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69  t Parse {.  sqli
6a500 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
6a510 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74   /* The main dat
6a520 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20  abase structure 
6a530 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
6a540 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
6a550 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78  urn code from ex
6a560 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61  ecution */.  cha
6a570 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
6a580 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65    /* An error me
6a590 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20  ssage */.  Vdbe 
6a5a0 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20  *pVdbe;         
6a5b0 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72  /* An engine for
6a5c0 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62   executing datab
6a5d0 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a  ase bytecode */.
6a5e0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74    u8 colNamesSet
6a5f0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61  ;      /* TRUE a
6a600 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61  fter OP_ColumnNa
6a610 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75  me has been issu
6a620 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20  ed to pVdbe */. 
6a630 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20   u8 nameClash;  
6a640 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61        /* A perma
6a650 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20  nent table name 
6a660 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d  clashes with tem
6a670 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  p table name */.
6a680 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61    u8 checkSchema
6a690 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73  ;      /* Causes
6a6a0 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
6a6b0 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72  heck after an er
6a6c0 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74  ror */.  u8 nest
6a6d0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ed;           /*
6a6e0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
6a6f0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70  d calls to the p
6a700 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72  arser/code gener
6a710 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72  ator */.  u8 par
6a720 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f  seError;       /
6a730 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70  * True after a p
6a740 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54  arsing error.  T
6a750 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20  icket #1794 */. 
6a760 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20   u8 nTempReg;   
6a770 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6a780 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  of temporary reg
6a790 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52  isters in aTempR
6a7a0 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65  eg[] */.  u8 nTe
6a7b0 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f  mpInUse;       /
6a7c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d  * Number of aTem
6a7d0 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79  pReg[] currently
6a7e0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
6a7f0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38    int aTempReg[8
6a800 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e  ];     /* Holdin
6a810 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f  g area for tempo
6a820 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a  rary registers *
6a830 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65  /.  int nRangeRe
6a840 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  g;       /* Size
6a850 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72   of the temporar
6a860 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  y register block
6a870 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65   */.  int iRange
6a880 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  Reg;       /* Fi
6a890 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20  rst register in 
6a8a0 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
6a8b0 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  er block */.  in
6a8c0 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20  t nErr;         
6a8d0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6a8e0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20  errors seen */. 
6a8f0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20   int nTab;      
6a900 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6a910 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  of previously al
6a920 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72  located VDBE cur
6a930 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  sors */.  int nM
6a940 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
6a950 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
6a960 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f  ry cells used so
6a970 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53   far */.  int nS
6a980 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
6a990 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73  * Number of sets
6a9a0 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a   used so far */.
6a9b0 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20    int ckBase;   
6a9c0 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
6a9d0 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20  egister of data 
6a9e0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e  during check con
6a9f0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  straints */.  in
6aa00 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 20  t iCacheLevel;  
6aa10 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 20 76     /* ColCache v
6aa20 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c 43 61  alid when aColCa
6aa30 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d 69 43  che[].iLevel<=iC
6aa40 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 20 69  acheLevel */.  i
6aa50 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 20 20  nt iCacheCnt;   
6aa60 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 75      /* Counter u
6aa70 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
6aa80 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 75 20  aColCache[].lru 
6aa90 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 6e  values */.  u8 n
6aaa0 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20  ColCache;       
6aab0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
6aac0 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 6c  tries in the col
6aad0 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 75  umn cache */.  u
6aae0 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 20  8 iColCache;    
6aaf0 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72      /* Next entr
6ab00 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 74  y of the cache t
6ab10 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 73  o replace */.  s
6ab20 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
6ab30 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65  {.    int iTable
6ab40 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
6ab50 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  able cursor numb
6ab60 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  er */.    int iC
6ab70 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
6ab80 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  /* Table column 
6ab90 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 38  number */.    u8
6aba0 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20 20   affChange;     
6abb0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
6abc0 68 69 73 20 72 65 67 69 73 74 65 72 20 68 61 73  his register has
6abd0 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 79   had an affinity
6abe0 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 75   change */.    u
6abf0 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 20 20  8 tempReg;      
6ac00 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 73 20       /* iReg is 
6ac10 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 20  a temp register 
6ac20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65  that needs to be
6ac30 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e   freed */.    in
6ac40 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 20  t iLevel;       
6ac50 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 6c      /* Nesting l
6ac60 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20  evel */.    int 
6ac70 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20  iReg;           
6ac80 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 76 61    /* Reg with va
6ac90 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75  lue of this colu
6aca0 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e 65  mn. 0 means none
6acb0 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 75  . */.    int lru
6acc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
6acd0 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 6c 79  * Least recently
6ace0 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 73 20   used entry has 
6acf0 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c  the smallest val
6ad00 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61  ue */.  } aColCa
6ad10 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  che[SQLITE_N_COL
6ad20 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20  CACHE];  /* One 
6ad30 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
6ad40 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20  cache entry */. 
6ad50 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b 3b 20   u32 writeMask; 
6ad60 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 61        /* Start a
6ad70 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
6ad80 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 74 61  on on these data
6ad90 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 20 63  bases */.  u32 c
6ada0 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 20 20  ookieMask;      
6adb0 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 63  /* Bitmask of sc
6adc0 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 64 61  hema verified da
6add0 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 38 20  tabases */.  u8 
6ade0 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20 20 20  isMultiWrite;   
6adf0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61    /* True if sta
6ae00 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66 65 63  tement may affec
6ae10 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69 70 6c  t/insert multipl
6ae20 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38 20 6d  e rows */.  u8 m
6ae30 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20 20 20  ayAbort;        
6ae40 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61 74   /* True if stat
6ae50 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f 77 20  ement may throw 
6ae60 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69  an ABORT excepti
6ae70 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b  on */.  int cook
6ae80 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20  ieGoto;      /* 
6ae90 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f  Address of OP_Go
6aea0 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72  to to cookie ver
6aeb0 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65  ifier subroutine
6aec0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65   */.  int cookie
6aed0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58  Value[SQLITE_MAX
6aee0 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f  _ATTACHED+2];  /
6aef0 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b  * Values of cook
6af00 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f  ies to verify */
6af10 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
6af20 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
6af30 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f  E.  int nTableLo
6af40 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ck;        /* Nu
6af50 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e  mber of locks in
6af60 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20   aTableLock */. 
6af70 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62   TableLock *aTab
6af80 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69  leLock; /* Requi
6af90 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20  red table locks 
6afa0 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65  for shared-cache
6afb0 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a   mode */.#endif.
6afc0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20    int regRowid; 
6afd0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
6afe0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64  er holding rowid
6aff0 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45   of CREATE TABLE
6b000 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20   entry */.  int 
6b010 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20  regRoot;        
6b020 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
6b030 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e  ding root page n
6b040 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62  umber for new ob
6b050 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74 6f 69  jects */.  Autoi
6b060 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b 20 20  ncInfo *pAinc;  
6b070 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
6b080 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45 4d 45  bout AUTOINCREME
6b090 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  NT counters */. 
6b0a0 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20 20 20   int nMaxArg;   
6b0b0 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61 72 67        /* Max arg
6b0c0 73 20 70 61 73 73 65 64 20 74 6f 20 75 73 65 72  s passed to user
6b0d0 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73 75 62   function by sub
6b0e0 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20 20 2f  -program */..  /
6b0f0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73  * Information us
6b100 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e 67 20  ed while coding 
6b110 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73  trigger programs
6b120 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 54  . */.  Parse *pT
6b130 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a 20 50  oplevel;    /* P
6b140 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66  arse structure f
6b150 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20  or main program 
6b160 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 54  (or NULL) */.  T
6b170 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72 54 61  able *pTriggerTa
6b180 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74 72 69  b;  /* Table tri
6b190 67 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20  ggers are being 
6b1a0 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20 20 75  coded for */.  u
6b1b0 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 20 20  32 oldmask;     
6b1c0 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6f      /* Mask of o
6b1d0 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 65 66  ld.* columns ref
6b1e0 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20  erenced */.  u8 
6b1f0 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 20 20  eTriggerOp;     
6b200 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 2c 20    /* TK_UPDATE, 
6b210 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 4b 5f  TK_INSERT or TK_
6b220 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 65  DELETE */.  u8 e
6b230 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20  Orconf;         
6b240 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43   /* Default ON C
6b250 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66  ONFLICT policy f
6b260 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70 73  or trigger steps
6b270 20 2a 2f 0a 20 20 75 38 20 64 69 73 61 62 6c 65   */.  u8 disable
6b280 54 72 69 67 67 65 72 73 3b 20 20 2f 2a 20 54 72  Triggers;  /* Tr
6b290 75 65 20 74 6f 20 64 69 73 61 62 6c 65 20 74 72  ue to disable tr
6b2a0 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20  iggers */..  /* 
6b2b0 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e  Above is constan
6b2c0 74 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73  t between recurs
6b2d0 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20  ions.  Below is 
6b2e0 72 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64  reset before and
6b2f0 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68   after.  ** each
6b300 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20   recursion */.. 
6b310 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20   int nVar;      
6b320 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6b330 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73  of '?' variables
6b340 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c   seen in the SQL
6b350 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74   so far */.  int
6b360 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20   nVarExpr;      
6b370 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75    /* Number of u
6b380 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56  sed slots in apV
6b390 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e  arExpr[] */.  in
6b3a0 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b  t nVarExprAlloc;
6b3b0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6b3c0 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20  allocated slots 
6b3d0 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a  in apVarExpr[] *
6b3e0 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72  /.  Expr **apVar
6b3f0 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e  Expr;    /* Poin
6b400 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64  ters to :aaa and
6b410 20 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20   $aaaa wildcard 
6b420 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20  expressions */. 
6b430 20 56 64 62 65 20 2a 70 52 65 70 72 65 70 61 72   Vdbe *pReprepar
6b440 65 3b 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e  e;    /* VM bein
6b450 67 20 72 65 70 72 65 70 61 72 65 64 20 28 73 71  g reprepared (sq
6b460 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29  lite3Reprepare()
6b470 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 61  ) */.  int nAlia
6b480 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  s;          /* N
6b490 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 73 65 64  umber of aliased
6b4a0 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75   result set colu
6b4b0 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  mns */.  int nAl
6b4c0 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a  iasAlloc;     /*
6b4d0 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   Number of alloc
6b4e0 61 74 65 64 20 73 6c 6f 74 73 20 66 6f 72 20 61  ated slots for a
6b4f0 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 6e 74  Alias[] */.  int
6b500 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 20 20   *aAlias;       
6b510 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73    /* Register us
6b520 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 69 61 73  ed to hold alias
6b530 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75  ed result */.  u
6b540 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20  8 explain;      
6b550 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
6b560 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 20  he EXPLAIN flag 
6b570 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20  is found on the 
6b580 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 6e  query */.  Token
6b590 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20   sNameToken;    
6b5a0 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 75 6e  /* Token with un
6b5b0 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 6d 61  qualified schema
6b5c0 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a   object name */.
6b5d0 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 6f 6b    Token sLastTok
6b5e0 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c 61  en;    /* The la
6b5f0 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65 64 20  st token parsed 
6b600 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
6b610 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c  *zTail;   /* All
6b620 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 20 74   SQL text past t
6b630 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c 6f  he last semicolo
6b640 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54 61  n parsed */.  Ta
6b650 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b 20  ble *pNewTable; 
6b660 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62 65     /* A table be
6b670 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20  ing constructed 
6b680 62 79 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  by CREATE TABLE 
6b690 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e  */.  Trigger *pN
6b6a0 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f  ewTrigger;     /
6b6b0 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 72 20  * Trigger under 
6b6c0 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20 43  construct by a C
6b6d0 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2a 2f  REATE TRIGGER */
6b6e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
6b6f0 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20  AuthContext; /* 
6b700 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65  The 6th paramete
6b710 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63  r to db->xAuth c
6b720 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e  allbacks */.#ifn
6b730 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
6b740 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54  VIRTUALTABLE.  T
6b750 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20  oken sArg;      
6b760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d            /* Com
6b770 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20  plete text of a 
6b780 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20  module argument 
6b790 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56  */.  u8 declareV
6b7a0 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  tab;            
6b7b0 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64  /* True if insid
6b7c0 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  e sqlite3_declar
6b7d0 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e  e_vtab() */.  in
6b7e0 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20  t nVtabLock;    
6b7f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
6b800 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61  er of virtual ta
6b810 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  bles to lock */.
6b820 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62    Table **apVtab
6b830 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
6b840 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75  Pointer to virtu
6b850 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e  al tables needin
6b860 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e  g locking */.#en
6b870 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 68  dif.  int nHeigh
6b880 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
6b890 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   Expression tree
6b8a0 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 65   height of curre
6b8b0 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f  nt sub-select */
6b8c0 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69  .  Table *pZombi
6b8d0 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69  eTab;      /* Li
6b8e0 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65  st of Table obje
6b8f0 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 66  cts to delete af
6b900 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a  ter code gen */.
6b910 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 54    TriggerPrg *pT
6b920 72 69 67 67 65 72 50 72 67 3b 20 20 20 20 2f 2a  riggerPrg;    /*
6b930 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   Linked list of 
6b940 63 6f 64 65 64 20 74 72 69 67 67 65 72 73 20 2a  coded triggers *
6b950 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  /.};..#ifdef SQL
6b960 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
6b970 54 41 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20  TABLE.  #define 
6b980 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
6b990 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  0.#else.  #defin
6b9a0 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41  e IN_DECLARE_VTA
6b9b0 42 20 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61  B (pParse->decla
6b9c0 72 65 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a  reVtab).#endif..
6b9d0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
6b9e0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
6b9f0 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  ng structure can
6ba00 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20   be declared on 
6ba10 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64  a stack and used
6ba20 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20  .** to save the 
6ba30 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65  Parse.zAuthConte
6ba40 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74  xt value so that
6ba50 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f   it can be resto
6ba60 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74  red later..*/.st
6ba70 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74  ruct AuthContext
6ba80 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20   {.  const char 
6ba90 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20  *zAuthContext;  
6baa0 20 2f 2a 20 50 75 74 20 73 61 76 65 64 20 50 61   /* Put saved Pa
6bab0 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74  rse.zAuthContext
6bac0 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65   here */.  Parse
6bad0 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
6bae0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61         /* The Pa
6baf0 72 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  rse structure */
6bb00 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69  .};../*.** Bitfi
6bb10 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 20 50 35  eld flags for P5
6bb20 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73   value in OP_Ins
6bb30 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74  ert and OP_Delet
6bb40 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  e.*/.#define OPF
6bb50 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 20 20 20  LAG_NCHANGE     
6bb60 20 20 30 78 30 31 20 20 20 20 2f 2a 20 53 65 74    0x01    /* Set
6bb70 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e 6e   to update db->n
6bb80 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e  Change */.#defin
6bb90 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57  e OPFLAG_LASTROW
6bba0 49 44 20 20 20 20 20 30 78 30 32 20 20 20 20 2f  ID     0x02    /
6bbb0 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20  * Set to update 
6bbc0 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f  db->lastRowid */
6bbd0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
6bbe0 49 53 55 50 44 41 54 45 20 20 20 20 20 20 30 78  ISUPDATE      0x
6bbf0 30 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f 50  04    /* This OP
6bc00 5f 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73 71  _Insert is an sq
6bc10 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66  l UPDATE */.#def
6bc20 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e  ine OPFLAG_APPEN
6bc30 44 20 20 20 20 20 20 20 20 30 78 30 38 20 20 20  D        0x08   
6bc40 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65   /* This is like
6bc50 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
6bc60 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  nd */.#define OP
6bc70 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55  FLAG_USESEEKRESU
6bc80 4c 54 20 30 78 31 30 20 20 20 20 2f 2a 20 54 72  LT 0x10    /* Tr
6bc90 79 20 74 6f 20 61 76 6f 69 64 20 61 20 73 65 65  y to avoid a see
6bca0 6b 20 69 6e 20 42 74 72 65 65 49 6e 73 65 72 74  k in BtreeInsert
6bcb0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  () */.#define OP
6bcc0 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 20  FLAG_CLEARCACHE 
6bcd0 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 43 6c     0x20    /* Cl
6bce0 65 61 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  ear pseudo-table
6bcf0 20 63 61 63 68 65 20 69 6e 20 4f 50 5f 43 6f 6c   cache in OP_Col
6bd00 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61  umn */../*. * Ea
6bd10 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73 65  ch trigger prese
6bd20 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
6bd30 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74 6f  se schema is sto
6bd40 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 6e  red as an instan
6bd50 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 20  ce of. * struct 
6bd60 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a 20  Trigger. . *. * 
6bd70 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 74  Pointers to inst
6bd80 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20  ances of struct 
6bd90 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f 72  Trigger are stor
6bda0 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e 0a  ed in two ways..
6bdb0 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74 72   * 1. In the "tr
6bdc0 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61 62  igHash" hash tab
6bdd0 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65 20  le (part of the 
6bde0 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72 65  sqlite3* that re
6bdf0 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20 2a  presents the . *
6be00 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20 54      database). T
6be10 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 67  his allows Trigg
6be20 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74 6f  er structures to
6be30 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62 79   be retrieved by
6be40 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c   name.. * 2. All
6be50 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69   triggers associ
6be60 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67  ated with a sing
6be70 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 20  le table form a 
6be80 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 69  linked list, usi
6be90 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e 65  ng the. *    pNe
6bea0 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 72  xt member of str
6beb0 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20 70  uct Trigger. A p
6bec0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69  ointer to the fi
6bed0 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  rst element of t
6bee0 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 20  he. *    linked 
6bef0 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 61  list is stored a
6bf00 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72 22  s the "pTrigger"
6bf10 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 61   member of the a
6bf20 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 20  ssociated. *    
6bf30 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 2a  struct Table.. *
6bf40 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c 69  . * The "step_li
6bf50 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74  st" member point
6bf60 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  s to the first e
6bf70 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e 6b  lement of a link
6bf80 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 61  ed list. * conta
6bf90 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74  ining the SQL st
6bfa0 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 69  atements specifi
6bfb0 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67 65  ed as the trigge
6bfc0 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73  r program.. */.s
6bfd0 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b 0a  truct Trigger {.
6bfe0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
6bff0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6c000 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69   name of the tri
6c010 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20  gger            
6c020 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
6c030 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 20   char *table;   
6c040 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
6c050 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74 6f  table or view to
6c060 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67 67   which the trigg
6c070 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 20  er applies */.  
6c080 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  u8 op;          
6c090 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
6c0a0 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f  f TK_DELETE, TK_
6c0b0 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52  UPDATE, TK_INSER
6c0c0 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 75  T         */.  u
6c0d0 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 20  8 tr_tm;        
6c0e0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
6c0f0 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c   TRIGGER_BEFORE,
6c100 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a   TRIGGER_AFTER *
6c110 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 3b  /.  Expr *pWhen;
6c120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6c130 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f  he WHEN clause o
6c140 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
6c150 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a   (may be NULL) *
6c160 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c  /.  IdList *pCol
6c170 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49  umns;       /* I
6c180 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 44  f this is an UPD
6c190 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c  ATE OF <column-l
6c1a0 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20  ist> trigger,.  
6c1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c1c0 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 3c             the <
6c1d0 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20  column-list> is 
6c1e0 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20  stored here */. 
6c1f0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
6c200 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65  ;        /* Sche
6c210 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ma containing th
6c220 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53  e trigger */.  S
6c230 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d  chema *pTabSchem
6c240 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61  a;     /* Schema
6c250 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
6c260 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67  table */.  Trigg
6c270 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73  erStep *step_lis
6c280 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20  t; /* Link list 
6c290 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  of trigger progr
6c2a0 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 20  am steps        
6c2b0 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65       */.  Trigge
6c2c0 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  r *pNext;       
6c2d0 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65    /* Next trigge
6c2e0 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  r associated wit
6c2f0 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d  h the table */.}
6c300 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67  ;../*.** A trigg
6c310 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 42  er is either a B
6c320 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45  EFORE or an AFTE
6c330 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20  R trigger.  The 
6c340 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61  following consta
6c350 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65  nts.** determine
6c360 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49   which. .**.** I
6c370 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74  f there are mult
6c380 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79  iple triggers, y
6c390 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65  ou might of some
6c3a0 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65   BEFORE and some
6c3b0 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68   AFTER..** In th
6c3c0 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f  at cases, the co
6c3d0 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61  nstants below ca
6c3e0 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68  n be ORed togeth
6c3f0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54  er..*/.#define T
6c400 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31  RIGGER_BEFORE  1
6c410 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52  .#define TRIGGER
6c420 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20  _AFTER   2../*. 
6c430 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
6c440 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53   struct TriggerS
6c450 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 73  tep is used to s
6c460 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51  tore a single SQ
6c470 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74  L statement. * t
6c480 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f 66  hat is a part of
6c490 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72   a trigger-progr
6c4a0 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61  am. . *. * Insta
6c4b0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54  nces of struct T
6c4c0 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 73  riggerStep are s
6c4d0 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c  tored in a singl
6c4e0 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c  y linked list (l
6c4f0 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74  inked. * using t
6c500 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65  he "pNext" membe
6c510 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  r) referenced by
6c520 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22   the "step_list"
6c530 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a   member of the .
6c540 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74   * associated st
6c550 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73  ruct Trigger ins
6c560 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74  tance. The first
6c570 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
6c580 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20  linked list is. 
6c590 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70  * the first step
6c5a0 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2d   of the trigger-
6c5b0 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20  program.. * . * 
6c5c0 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20  The "op" member 
6c5d0 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65  indicates whethe
6c5e0 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 4c  r this is a "DEL
6c5f0 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20  ETE", "INSERT", 
6c600 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22  "UPDATE" or. * "
6c610 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e  SELECT" statemen
6c620 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20  t. The meanings 
6c630 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d  of the other mem
6c640 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e  bers is determin
6c650 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61  ed by the . * va
6c660 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66  lue of "op" as f
6c670 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f  ollows:. *. * (o
6c680 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a  p == TK_INSERT).
6c690 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20   * orconf    -> 
6c6a0 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f  stores the ON CO
6c6b0 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d  NFLICT algorithm
6c6c0 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e  . * pSelect   ->
6c6d0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49   If this is an I
6c6e0 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53  NSERT INTO ... S
6c6f0 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d  ELECT ... statem
6c700 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20  ent, then. *    
6c710 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73            this s
6c720 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20  tores a pointer 
6c730 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  to the SELECT st
6c740 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69  atement. Otherwi
6c750 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67  se NULL.. * targ
6c760 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e  et    -> A token
6c770 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f   holding the quo
6c780 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  ted name of the 
6c790 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20  table to insert 
6c7a0 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c 69  into.. * pExprLi
6c7b0 73 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73  st -> If this is
6c7c0 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20   an INSERT INTO 
6c7d0 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 73  ... VALUES ... s
6c7e0 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20  tatement, then. 
6c7f0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74  *              t
6c800 68 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75 65  his stores value
6c810 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  s to be inserted
6c820 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c  . Otherwise NULL
6c830 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20 2d  .. * pIdList   -
6c840 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  > If this is an 
6c850 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20  INSERT INTO ... 
6c860 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e 29  (<column-names>)
6c870 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a 20   VALUES ... . * 
6c880 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
6c890 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 69  tement, then thi
6c8a0 73 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f 6c  s stores the col
6c8b0 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 0a  umn-names to be.
6c8c0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6c8d0 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a 20  inserted into.. 
6c8e0 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 44  *. * (op == TK_D
6c8f0 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65 74  ELETE). * target
6c900 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68      -> A token h
6c910 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f 74 65  olding the quote
6c920 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  d name of the ta
6c930 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72  ble to delete fr
6c940 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20  om.. * pWhere   
6c950 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c   -> The WHERE cl
6c960 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45  ause of the DELE
6c970 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  TE statement if 
6c980 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
6c990 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  .. *            
6c9a0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
6c9b0 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20  .. * . * (op == 
6c9c0 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61  TK_UPDATE). * ta
6c9d0 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b  rget    -> A tok
6c9e0 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71  en holding the q
6c9f0 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68  uoted name of th
6ca00 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61 74  e table to updat
6ca10 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70 57  e rows of.. * pW
6ca20 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57  here    -> The W
6ca30 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74  HERE clause of t
6ca40 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
6ca50 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70  ent if one is sp
6ca60 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20  ecified.. *     
6ca70 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69           Otherwi
6ca80 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78 70  se NULL.. * pExp
6ca90 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74 20  rList -> A list 
6caa0 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 74  of the columns t
6cab0 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68 65  o update and the
6cac0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20   expressions to 
6cad0 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20 20  update. *       
6cae0 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e 20         them to. 
6caf0 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61 74  See sqlite3Updat
6cb00 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  e() documentatio
6cb10 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22 0a  n of "pChanges".
6cb20 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6cb30 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20 2a  argument.. * . *
6cb40 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72  /.struct Trigger
6cb50 53 74 65 70 20 7b 0a 20 20 75 38 20 6f 70 3b 20  Step {.  u8 op; 
6cb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6cb70 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54   One of TK_DELET
6cb80 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  E, TK_UPDATE, TK
6cb90 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c 45  _INSERT, TK_SELE
6cba0 43 54 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e  CT */.  u8 orcon
6cbb0 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  f;           /* 
6cbc0 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63 2e  OE_Rollback etc.
6cbd0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
6cbe0 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54 68  Trig;      /* Th
6cbf0 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 74  e trigger that t
6cc00 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70 61  his step is a pa
6cc10 72 74 20 6f 66 20 2a 2f 0a 20 20 53 65 6c 65 63  rt of */.  Selec
6cc20 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20  t *pSelect;     
6cc30 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d 65  /* SELECT statme
6cc40 6e 74 20 6f 72 20 52 48 53 20 6f 66 20 49 4e 53  nt or RHS of INS
6cc50 45 52 54 20 49 4e 54 4f 20 2e 2e 20 53 45 4c 45  ERT INTO .. SELE
6cc60 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 54 6f 6b 65  CT ... */.  Toke
6cc70 6e 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 20  n target;       
6cc80 20 2f 2a 20 54 61 72 67 65 74 20 74 61 62 6c 65   /* Target table
6cc90 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44   for DELETE, UPD
6cca0 41 54 45 2c 20 49 4e 53 45 52 54 20 2a 2f 0a 20  ATE, INSERT */. 
6ccb0 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20   Expr *pWhere;  
6ccc0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
6ccd0 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20 44 45  RE clause for DE
6cce0 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 73  LETE or UPDATE s
6ccf0 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69  teps */.  ExprLi
6cd00 73 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20 2f  st *pExprList; /
6cd10 2a 20 53 45 54 20 63 6c 61 75 73 65 20 66 6f 72  * SET clause for
6cd20 20 55 50 44 41 54 45 2e 20 20 56 41 4c 55 45 53   UPDATE.  VALUES
6cd30 20 63 6c 61 75 73 65 20 66 6f 72 20 49 4e 53 45   clause for INSE
6cd40 52 54 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a  RT */.  IdList *
6cd50 70 49 64 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20  pIdList;     /* 
6cd60 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72  Column names for
6cd70 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 54 72 69   INSERT */.  Tri
6cd80 67 67 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b  ggerStep *pNext;
6cd90 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65    /* Next in the
6cda0 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20   link-list */.  
6cdb0 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61  TriggerStep *pLa
6cdc0 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65  st;  /* Last ele
6cdd0 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73  ment in link-lis
6cde0 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74  t. Valid for 1st
6cdf0 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b   elem only */.};
6ce00 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
6ce10 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
6ce20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
6ce30 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65  tion used by the
6ce40 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a   sqliteFix....**
6ce50 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65   routines as the
6ce60 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65  y walk the parse
6ce70 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61   tree to make da
6ce80 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65  tabase reference
6ce90 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20  s.** explicit.  
6cea0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
6ceb0 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78  ct DbFixer DbFix
6cec0 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78  er;.struct DbFix
6ced0 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  er {.  Parse *pP
6cee0 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68  arse;      /* Th
6cef0 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
6cf00 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67  t.  Error messag
6cf10 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  es written here 
6cf20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
6cf30 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65  *zDb;    /* Make
6cf40 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74   sure all object
6cf50 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20  s are contained 
6cf60 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65  in this database
6cf70 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
6cf80 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70   *zType;  /* Typ
6cf90 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e  e of the contain
6cfa0 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72  er - used for er
6cfb0 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
6cfc0 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70    const Token *p
6cfd0 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name; /* Name of
6cfe0 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d   the container -
6cff0 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20   used for error 
6d000 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a  messages */.};..
6d010 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65  /*.** An objecte
6d020 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75  d used to accumu
6d030 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66  late the text of
6d040 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20   a string where 
6d050 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63  we.** do not nec
6d060 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f  essarily know ho
6d070 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67  w big the string
6d080 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20   will be in the 
6d090 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  end..*/.struct S
6d0a0 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69  trAccum {.  sqli
6d0b0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
6d0c0 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74   /* Optional dat
6d0d0 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73  abase for lookas
6d0e0 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c  ide.  Can be NUL
6d0f0 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61  L */.  char *zBa
6d100 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  se;         /* A
6d110 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   base allocation
6d120 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c  .  Not from mall
6d130 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  oc. */.  char *z
6d140 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Text;         /*
6d150 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c   The string coll
6d160 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a  ected so far */.
6d170 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20    int  nChar;   
6d180 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
6d190 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73   of the string s
6d1a0 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20  o far */.  int  
6d1b0 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
6d1c0 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61  /* Amount of spa
6d1d0 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  ce allocated in 
6d1e0 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20  zText */.  int  
6d1f0 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20  mxAlloc;        
6d200 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77  /* Maximum allow
6d210 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68  ed string length
6d220 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f   */.  u8   mallo
6d230 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65  cFailed;   /* Be
6d240 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e  comes true if an
6d250 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  y memory allocat
6d260 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75  ion fails */.  u
6d270 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20  8   useMalloc;  
6d280 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a      /* True if z
6d290 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 65 61  Text is enlargea
6d2a0 62 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f  ble using reallo
6d2b0 63 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42  c */.  u8   tooB
6d2c0 69 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ig;         /* B
6d2d0 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73  ecomes true if s
6d2e0 74 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65  tring size excee
6d2f0 64 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a  ds limits */.};.
6d300 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  ./*.** A pointer
6d310 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
6d320 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  re is used to co
6d330 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d  mmunicate inform
6d340 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71  ation.** from sq
6d350 6c 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50  lite3Init and OP
6d360 5f 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74  _ParseSchema int
6d370 6f 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69  o the sqlite3Ini
6d380 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79  tCallback..*/.ty
6d390 70 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20  pedef struct {. 
6d3a0 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
6d3b0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
6d3c0 62 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69  base being initi
6d3d0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20  alized */.  int 
6d3e0 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
6d3f0 2f 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61  /* 0 for main da
6d400 74 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54  tabase.  1 for T
6d410 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54  EMP, 2.. for ATT
6d420 41 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  ACHed */.  char 
6d430 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f  **pzErrMsg;    /
6d440 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20  * Error message 
6d450 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20  stored here */. 
6d460 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
6d470 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63       /* Result c
6d480 6f 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20  ode stored here 
6d490 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a  */.} InitData;..
6d4a0 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20  /*.** Structure 
6d4b0 63 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61  containing globa
6d4c0 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  l configuration 
6d4d0 64 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c  data for the SQL
6d4e0 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a  ite library..**.
6d4f0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
6d500 65 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20  e also contains 
6d510 73 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72  some state infor
6d520 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63  mation..*/.struc
6d530 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20  t Sqlite3Config 
6d540 7b 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74  {.  int bMemstat
6d550 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6d560 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
6d570 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73   enable memory s
6d580 74 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62  tatus */.  int b
6d590 43 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20  CoreMutex;      
6d5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6d5b0 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63  True to enable c
6d5c0 6f 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a  ore mutexing */.
6d5d0 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78    int bFullMutex
6d5e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6d5f0 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65      /* True to e
6d600 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78  nable full mutex
6d610 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53  ing */.  int mxS
6d620 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20  trlen;          
6d630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
6d640 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e  ximum string len
6d650 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c  gth */.  int szL
6d660 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20  ookaside;       
6d670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
6d680 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20  fault lookaside 
6d690 62 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20  buffer size */. 
6d6a0 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b   int nLookaside;
6d6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d6c0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f     /* Default lo
6d6d0 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 63  okaside buffer c
6d6e0 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ount */.  sqlite
6d6f0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b  3_mem_methods m;
6d700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
6d710 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
6d720 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72  allocation inter
6d730 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  face */.  sqlite
6d740 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
6d750 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c  mutex;      /* L
6d760 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69  ow-level mutex i
6d770 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71  nterface */.  sq
6d780 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
6d790 68 6f 64 73 20 70 63 61 63 68 65 3b 20 20 20 20  hods pcache;    
6d7a0 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 67  /* Low-level pag
6d7b0 65 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61 63  e-cache interfac
6d7c0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 65  e */.  void *pHe
6d7d0 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ap;             
6d7e0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 70           /* Heap
6d7f0 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 2a   storage space *
6d800 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 20  /.  int nHeap;  
6d810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d820 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
6d830 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e   pHeap[] */.  in
6d840 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b 20  t mnReq, mxReq; 
6d850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d860 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 68  /* Min and max h
6d870 65 61 70 20 72 65 71 75 65 73 74 73 20 73 69 7a  eap requests siz
6d880 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  es */.  void *pS
6d890 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20  cratch;         
6d8a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 72            /* Scr
6d8b0 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20  atch memory */. 
6d8c0 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20   int szScratch; 
6d8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d8e0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61     /* Size of ea
6d8f0 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66 65  ch scratch buffe
6d900 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 61  r */.  int nScra
6d910 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  tch;            
6d920 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
6d930 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 62 75  er of scratch bu
6d940 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20  ffers */.  void 
6d950 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  *pPage;         
6d960 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6d970 50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  Page cache memor
6d980 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67  y */.  int szPag
6d990 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
6d9a0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
6d9b0 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 6e   of each page in
6d9c0 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e   pPage[] */.  in
6d9d0 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20  t nPage;        
6d9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d9f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
6da00 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f  es in pPage[] */
6da10 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72 53  .  int mxParserS
6da20 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  tack;           
6da30 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d 20       /* maximum 
6da40 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72  depth of the par
6da50 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69  ser stack */.  i
6da60 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45 6e  nt sharedCacheEn
6da70 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20  abled;          
6da80 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61 72   /* true if shar
6da90 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 6e  ed-cache mode en
6daa0 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54 68  abled */.  /* Th
6dab0 65 20 61 62 6f 76 65 20 6d 69 67 68 74 20 62 65  e above might be
6dac0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
6dad0 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20 66  non-zero.  The f
6dae0 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74 6f  ollowing need to
6daf0 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e 69   always.  ** ini
6db00 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c 20  tially be zero, 
6db10 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69 6e  however. */.  in
6db20 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20  t isInit;       
6db30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6db40 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 69 6e  /* True after in
6db50 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 73  itialization has
6db60 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 69   finished */.  i
6db70 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 3b 20 20  nt inProgress;  
6db80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6db90 20 2f 2a 20 54 72 75 65 20 77 68 69 6c 65 20 69   /* True while i
6dba0 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e  nitialization in
6dbb0 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 69   progress */.  i
6dbc0 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 74 3b 20  nt isMutexInit; 
6dbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dbe0 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d   /* True after m
6dbf0 75 74 65 78 65 73 20 61 72 65 20 69 6e 69 74 69  utexes are initi
6dc00 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20  alized */.  int 
6dc10 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 20  isMallocInit;   
6dc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6dc30 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c   True after mall
6dc40 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  oc is initialize
6dc50 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 43 61  d */.  int isPCa
6dc60 63 68 65 49 6e 69 74 3b 20 20 20 20 20 20 20 20  cheInit;        
6dc70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
6dc80 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73   after malloc is
6dc90 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6dca0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
6dcb0 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 20  *pInitMutex;    
6dcc0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65      /* Mutex use
6dcd0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69  d by sqlite3_ini
6dce0 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 69  tialize() */.  i
6dcf0 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 78  nt nRefInitMutex
6dd00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6dd10 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73   /* Number of us
6dd20 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74 65  ers of pInitMute
6dd30 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  x */.};../*.** C
6dd40 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 70  ontext pointer p
6dd50 61 73 73 65 64 20 64 6f 77 6e 20 74 68 72 6f 75  assed down throu
6dd60 67 68 20 74 68 65 20 74 72 65 65 2d 77 61 6c 6b  gh the tree-walk
6dd70 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c 6b  ..*/.struct Walk
6dd80 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 45 78  er {.  int (*xEx
6dd90 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b  prCallback)(Walk
6dda0 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 20 20 20  er*, Expr*);    
6ddb0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72   /* Callback for
6ddc0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a   expressions */.
6ddd0 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 63 74 43    int (*xSelectC
6dde0 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a  allback)(Walker*
6ddf0 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 43  ,Select*);  /* C
6de00 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 45 4c 45  allback for SELE
6de10 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  CTs */.  Parse *
6de20 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
6de30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6de40 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
6de50 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 6f  text.  */.  unio
6de60 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  n {             
6de70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6de80 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 64        /* Extra d
6de90 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 61 63 6b  ata for callback
6dea0 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74   */.    NameCont
6deb0 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 20 20 20  ext *pNC;       
6dec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ded0 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f 6e     /* Naming con
6dee0 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  text */.    int 
6def0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
6df00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6df10 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
6df20 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75  r value */.  } u
6df30 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  ;.};../* Forward
6df40 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f   declarations */
6df50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6df60 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  int sqlite3WalkE
6df70 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70  xpr(Walker*, Exp
6df80 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6df90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
6dfa0 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b  alkExprList(Walk
6dfb0 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  er*, ExprList*);
6dfc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6dfd0 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  int sqlite3WalkS
6dfe0 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a 2c 20 53  elect(Walker*, S
6dff0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
6e000 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6e010 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70  te3WalkSelectExp
6e020 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63  r(Walker*, Selec
6e030 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6e040 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
6e050 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 61  alkSelectFrom(Wa
6e060 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b  lker*, Select*);
6e070 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63  ../*.** Return c
6e080 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72  ode from the par
6e090 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 6e 67 20  se-tree walking 
6e0a0 70 72 69 6d 69 74 69 76 65 73 20 61 6e 64 20 74  primitives and t
6e0b0 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  heir.** callback
6e0c0 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52  s..*/.#define WR
6e0d0 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 30 20  C_Continue    0 
6e0e0 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 64 6f    /* Continue do
6e0f0 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 72 65 6e  wn into children
6e100 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f   */.#define WRC_
6e110 50 72 75 6e 65 20 20 20 20 20 20 20 31 20 20 20  Prune       1   
6e120 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 72 65 6e  /* Omit children
6e130 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20 77 61   but continue wa
6e140 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 73 20 2a  lking siblings *
6e150 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41 62  /.#define WRC_Ab
6e160 6f 72 74 20 20 20 20 20 20 20 32 20 20 20 2f 2a  ort       2   /*
6e170 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65   Abandon the tre
6e180 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  e walk */../*.**
6e190 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f   Assuming zIn po
6e1a0 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73  ints to the firs
6e1b0 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d  t byte of a UTF-
6e1c0 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20  8 character,.** 
6e1d0 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70  advance zIn to p
6e1e0 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73  oint to the firs
6e1f0 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65  t byte of the ne
6e200 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  xt UTF-8 charact
6e210 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  er..*/.#define S
6e220 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
6e230 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20  zIn) {          
6e240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
6e250 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29    if( (*(zIn++))
6e260 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20  >=0xc0 ){       
6e270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e280 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69         \.    whi
6e290 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30  le( (*zIn & 0xc0
6e2a0 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b  )==0x80 ){ zIn++
6e2b0 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20  ; }             
6e2c0 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20  \.  }           
6e2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e2f0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a           \.}../*
6e300 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43  .** The SQLITE_C
6e310 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72  ORRUPT_BKPT macr
6e320 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  o can be either 
6e330 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20  a constant (for 
6e340 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75  production.** bu
6e350 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74  ilds) or a funct
6e360 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65  ion call (for de
6e370 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74  bugging).  If it
6e380 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63   is a function c
6e390 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77  all,.** it allow
6e3a0 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74  s the operator t
6e3b0 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69  o set a breakpoi
6e3c0 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20 77  nt at the spot w
6e3d0 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  here database.**
6e3e0 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66   corruption is f
6e3f0 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a  irst detected..*
6e400 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
6e410 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
6e420 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6e430 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b  e3Corrupt(void);
6e440 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
6e450 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71  _CORRUPT_BKPT sq
6e460 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23  lite3Corrupt().#
6e470 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
6e480 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
6e490 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  T SQLITE_CORRUPT
6e4a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
6e4b0 68 65 20 63 74 79 70 65 2e 68 20 68 65 61 64 65  he ctype.h heade
6e4c0 72 20 69 73 20 6e 65 65 64 65 64 20 66 6f 72 20  r is needed for 
6e4d0 6e 6f 6e 2d 41 53 43 49 49 20 73 79 73 74 65 6d  non-ASCII system
6e4e0 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 0a 2a  s.  It is also.*
6e4f0 2a 20 6e 65 65 64 65 64 20 62 79 20 46 54 53 33  * needed by FTS3
6e500 20 77 68 65 6e 20 46 54 53 33 20 69 73 20 69 6e   when FTS3 is in
6e510 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 61 6d  cluded in the am
6e520 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23  algamation..*/.#
6e530 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
6e540 54 45 5f 41 53 43 49 49 29 20 7c 7c 20 5c 0a 20  TE_ASCII) || \. 
6e550 20 20 20 28 64 65 66 69 6e 65 64 28 53 51 4c 49     (defined(SQLI
6e560 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
6e570 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
6e580 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 29 29  E_AMALGAMATION))
6e590 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79 70  .# include <ctyp
6e5a0 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e.h>.#endif../*.
6e5b0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
6e5c0 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 74 68   macros mimic th
6e5d0 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61  e standard libra
6e5e0 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 75  ry functions tou
6e5f0 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 70 61  pper(),.** isspa
6e600 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29 2c  ce(), isalnum(),
6e610 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20 69   isdigit() and i
6e620 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 70 65  sxdigit(), respe
6e630 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20  ctively. The.** 
6e640 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73 20  sqlite versions 
6e650 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41 53  only work for AS
6e660 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c 20  CII characters, 
6e670 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f  regardless of lo
6e680 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  cale..*/.#ifdef 
6e690 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 64  SQLITE_ASCII.# d
6e6a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 75  efine sqlite3Tou
6e6b0 70 70 65 72 28 78 29 20 20 28 28 78 29 26 7e 28  pper(x)  ((x)&~(
6e6c0 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
6e6d0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
6e6e0 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64 65 66  x)]&0x20)).# def
6e6f0 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 61  ine sqlite3Isspa
6e700 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 65 33  ce(x)   (sqlite3
6e710 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
6e720 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
6e730 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  1).# define sqli
6e740 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20  te3Isalnum(x)   
6e750 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70  (sqlite3CtypeMap
6e760 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
6e770 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 65 66  (x)]&0x06).# def
6e780 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70  ine sqlite3Isalp
6e790 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 65 33  ha(x)   (sqlite3
6e7a0 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
6e7b0 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
6e7c0 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  2).# define sqli
6e7d0 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 20  te3Isdigit(x)   
6e7e0 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70  (sqlite3CtypeMap
6e7f0 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
6e800 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 65 66  (x)]&0x04).# def
6e810 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 64 69  ine sqlite3Isxdi
6e820 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 65 33  git(x)  (sqlite3
6e830 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
6e840 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
6e850 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  8).# define sqli
6e860 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20 20  te3Tolower(x)   
6e870 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c  (sqlite3UpperToL
6e880 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63  ower[(unsigned c
6e890 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 65 0a  har)(x)]).#else.
6e8a0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6e8b0 54 6f 75 70 70 65 72 28 78 29 20 20 20 74 6f 75  Toupper(x)   tou
6e8c0 70 70 65 72 28 28 75 6e 73 69 67 6e 65 64 20 63  pper((unsigned c
6e8d0 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e  har)(x)).# defin
6e8e0 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  e sqlite3Isspace
6e8f0 28 78 29 20 20 20 69 73 73 70 61 63 65 28 28 75  (x)   isspace((u
6e900 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29  nsigned char)(x)
6e910 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
6e920 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 69  e3Isalnum(x)   i
6e930 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 6e 65 64  salnum((unsigned
6e940 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66   char)(x)).# def
6e950 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70  ine sqlite3Isalp
6e960 68 61 28 78 29 20 20 20 69 73 61 6c 70 68 61 28  ha(x)   isalpha(
6e970 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
6e980 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  x)).# define sql
6e990 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20  ite3Isdigit(x)  
6e9a0 20 69 73 64 69 67 69 74 28 28 75 6e 73 69 67 6e   isdigit((unsign
6e9b0 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
6e9c0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78  efine sqlite3Isx
6e9d0 64 69 67 69 74 28 78 29 20 20 69 73 78 64 69 67  digit(x)  isxdig
6e9e0 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  it((unsigned cha
6e9f0 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20  r)(x)).# define 
6ea00 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78  sqlite3Tolower(x
6ea10 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 75 6e 73  )   tolower((uns
6ea20 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a  igned char)(x)).
6ea30 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
6ea40 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20  ternal function 
6ea50 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51  prototypes.*/.SQ
6ea60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ea70 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
6ea80 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f  const char *, co
6ea90 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nst char *);.SQL
6eaa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6eab0 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28  sqlite3IsNumber(
6eac0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
6ead0 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50  *, u8);.SQLITE_P
6eae0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6eaf0 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74  e3Strlen30(const
6eb00 20 63 68 61 72 2a 29 3b 0a 23 64 65 66 69 6e 65   char*);.#define
6eb10 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
6eb20 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d   sqlite3_strnicm
6eb30 70 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  p..SQLITE_PRIVAT
6eb40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c  E int sqlite3Mal
6eb50 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53  locInit(void);.S
6eb60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6eb70 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  id sqlite3Malloc
6eb80 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  End(void);.SQLIT
6eb90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
6eba0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e  sqlite3Malloc(in
6ebb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6ebc0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6ebd0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29 3b  MallocZero(int);
6ebe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ebf0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d  void *sqlite3DbM
6ec00 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65  allocZero(sqlite
6ec10 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  3*, int);.SQLITE
6ec20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
6ec30 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
6ec40 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29  w(sqlite3*, int)
6ec50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ec60 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62   char *sqlite3Db
6ec70 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  StrDup(sqlite3*,
6ec80 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
6ec90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
6eca0 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e  r *sqlite3DbStrN
6ecb0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  Dup(sqlite3*,con
6ecc0 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a  st char*, int);.
6ecd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6ece0 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c  oid *sqlite3Real
6ecf0 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b  loc(void*, int);
6ed00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ed10 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52  void *sqlite3DbR
6ed20 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c  eallocOrFree(sql
6ed30 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20  ite3 *, void *, 
6ed40 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6ed50 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
6ed60 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69  e3DbRealloc(sqli
6ed70 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69  te3 *, void *, i
6ed80 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6ed90 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6eda0 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 2a 2c  DbFree(sqlite3*,
6edb0 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
6edc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6edd0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f  te3MallocSize(vo
6ede0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
6edf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6ee00 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c  DbMallocSize(sql
6ee10 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53  ite3*, void*);.S
6ee20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6ee30 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 74  id *sqlite3Scrat
6ee40 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53  chMalloc(int);.S
6ee50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6ee60 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63  id sqlite3Scratc
6ee70 68 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51  hFree(void*);.SQ
6ee80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6ee90 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61  d *sqlite3PageMa
6eea0 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
6eeb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6eec0 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76  qlite3PageFree(v
6eed0 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  oid*);.SQLITE_PR
6eee0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6eef0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
6ef00 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
6ef10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6ef20 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f  e3BenignMallocHo
6ef30 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 76 6f 69  oks(void (*)(voi
6ef40 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69  d), void (*)(voi
6ef50 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d));.SQLITE_PRIV
6ef60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
6ef70 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f 69 64 20  emoryAlarm(void 
6ef80 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74  (*)(void*, sqlit
6ef90 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 29 2c 20  e3_int64, int), 
6efa0 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69  void*, sqlite3_i
6efb0 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 6e  nt64);../*.** On
6efc0 20 73 79 73 74 65 6d 73 20 77 69 74 68 20 61 6d   systems with am
6efd0 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63 65 20  ple stack space 
6efe0 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f 72 74  and that support
6eff0 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b 65 0a   alloca(), make.
6f000 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f 63 61  ** use of alloca
6f010 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61  () to obtain spa
6f020 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61 75 74  ce for large aut
6f030 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73 2e 20  omatic objects. 
6f040 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20   By default,.** 
6f050 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f  obtain space fro
6f060 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a  m malloc()..**.*
6f070 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 29 20 72  * The alloca() r
6f080 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 72 65 74  outine never ret
6f090 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 68 69 73  urns NULL.  This
6f0a0 20 77 69 6c 6c 20 63 61 75 73 65 20 63 6f 64 65   will cause code
6f0b0 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 74 20 64   paths.** that d
6f0c0 65 61 6c 20 77 69 74 68 20 73 71 6c 69 74 65 33  eal with sqlite3
6f0d0 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 66 61 69  StackAlloc() fai
6f0e0 6c 75 72 65 73 20 74 6f 20 62 65 20 75 6e 72 65  lures to be unre
6f0f0 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 64  achable..*/.#ifd
6f100 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f 41 4c  ef SQLITE_USE_AL
6f110 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 20 73 71  LOCA.# define sq
6f120 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52  lite3StackAllocR
6f130 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c 6f 63 61  aw(D,N)   alloca
6f140 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (N).# define sql
6f150 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65  ite3StackAllocZe
6f160 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 65 74 28  ro(D,N)  memset(
6f170 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c 20 4e 29  alloca(N), 0, N)
6f180 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6f190 33 53 74 61 63 6b 46 72 65 65 28 44 2c 50 29 20  3StackFree(D,P) 
6f1a0 20 20 20 20 20 20 0a 23 65 6c 73 65 0a 23 20 64        .#else.# d
6f1b0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61  efine sqlite3Sta
6f1c0 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20  ckAllocRaw(D,N) 
6f1d0 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f    sqlite3DbMallo
6f1e0 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 65 66 69  cRaw(D,N).# defi
6f1f0 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41  ne sqlite3StackA
6f200 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 73  llocZero(D,N)  s
6f210 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
6f220 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 69 6e 65  ro(D,N).# define
6f230 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
6f240 65 28 44 2c 50 29 20 20 20 20 20 20 20 73 71 6c  e(D,P)       sql
6f250 69 74 65 33 44 62 46 72 65 65 28 44 2c 50 29 0a  ite3DbFree(D,P).
6f260 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
6f270 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
6f280 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 49 56  SYS3.SQLITE_PRIV
6f290 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
6f2a0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
6f2b0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73  qlite3MemGetMems
6f2c0 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69  ys3(void);.#endi
6f2d0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
6f2e0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 53  ENABLE_MEMSYS5.S
6f2f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
6f300 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
6f310 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
6f320 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f  MemGetMemsys5(vo
6f330 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  id);.#endif...#i
6f340 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  fndef SQLITE_MUT
6f350 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f 50  EX_OMIT.SQLITE_P
6f360 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 33  RIVATE   sqlite3
6f370 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a  _mutex_methods *
6f380 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75  sqlite3DefaultMu
6f390 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  tex(void);.SQLIT
6f3a0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
6f3b0 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74  te3_mutex *sqlit
6f3c0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74  e3MutexAlloc(int
6f3d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f3e0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  E   int sqlite3M
6f3f0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a  utexInit(void);.
6f400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6f410 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65   int sqlite3Mute
6f420 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64  xEnd(void);.#end
6f430 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
6f440 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
6f450 61 74 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a  atusValue(int);.
6f460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f470 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
6f480 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  sAdd(int, int);.
6f490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f4a0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
6f4b0 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  sSet(int, int);.
6f4c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f4d0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  int sqlite3IsNaN
6f4e0 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 54  (double);..SQLIT
6f4f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f500 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 53  qlite3VXPrintf(S
6f510 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 63  trAccum*, int, c
6f520 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c  onst char*, va_l
6f530 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ist);.SQLITE_PRI
6f540 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
6f550 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65  e3MPrintf(sqlite
6f560 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  3*,const char*, 
6f570 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
6f580 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
6f590 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74  e3VMPrintf(sqlit
6f5a0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
6f5b0 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54   va_list);.SQLIT
6f5c0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
6f5d0 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
6f5e0 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63  sqlite3*,char*,c
6f5f0 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
6f600 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
6f610 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66  ITE_TEST) || def
6f620 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
6f630 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  G).SQLITE_PRIVAT
6f640 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
6f650 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73  DebugPrintf(cons
6f660 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23  t char*, ...);.#
6f670 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65  endif.#if define
6f680 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53  d(SQLITE_TEST).S
6f690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6f6a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54 65 73  void *sqlite3Tes
6f6b0 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 74  tTextToPtr(const
6f6c0 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a   char*);.#endif.
6f6d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f6e0 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74  oid sqlite3SetSt
6f6f0 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73 71  ring(char **, sq
6f700 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68  lite3*, const ch
6f710 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54  ar*, ...);.SQLIT
6f720 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f730 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50  qlite3ErrorMsg(P
6f740 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
6f750 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  r*, ...);.SQLITE
6f760 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6f770 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28  lite3ErrorClear(
6f780 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
6f790 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6f7a0 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 2a  te3Dequote(char*
6f7b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f7c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 79  E int sqlite3Key
6f7d0 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 75  wordCode(const u
6f7e0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69  nsigned char*, i
6f7f0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6f800 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
6f810 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 2a 2c  unParser(Parse*,
6f820 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 68   const char*, ch
6f830 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ar **);.SQLITE_P
6f840 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f850 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28  te3FinishCoding(
6f860 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
6f870 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6f880 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 61  te3GetTempReg(Pa
6f890 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rse*);.SQLITE_PR
6f8a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f8b0 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
6f8c0 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Parse*,int);.SQ
6f8d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6f8e0 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
6f8f0 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 29  ange(Parse*,int)
6f900 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f910 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c   void sqlite3Rel
6f920 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50 61  easeTempRange(Pa
6f930 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  rse*,int,int);.S
6f940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
6f950 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41  pr *sqlite3ExprA
6f960 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a 2c 69 6e  lloc(sqlite3*,in
6f970 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 2c 69  t,const Token*,i
6f980 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6f990 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
6f9a0 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a 2c 69  3Expr(sqlite3*,i
6f9b0 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  nt,const char*);
6f9c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f9d0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
6f9e0 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 73  AttachSubtrees(s
6f9f0 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 45 78  qlite3*,Expr*,Ex
6fa00 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49  pr*,Expr*);.SQLI
6fa10 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
6fa20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 50 61  *sqlite3PExpr(Pa
6fa30 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a  rse*, int, Expr*
6fa40 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54  , Expr*, const T
6fa50 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6fa60 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
6fa70 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c 69  ite3ExprAnd(sqli
6fa80 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78 70 72  te3*,Expr*, Expr
6fa90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6faa0 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
6fab0 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72  ExprFunction(Par
6fac0 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54  se*,ExprList*, T
6fad0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6fae0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6faf0 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72  te3ExprAssignVar
6fb00 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45  Number(Parse*, E
6fb10 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6fb20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6fb30 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69  e3ExprClear(sqli
6fb40 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  te3*, Expr*);.SQ
6fb50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6fb60 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c  d sqlite3ExprDel
6fb70 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78  ete(sqlite3*, Ex
6fb80 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
6fb90 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73  VATE ExprList *s
6fba0 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
6fbb0 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72  pend(Parse*,Expr
6fbc0 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b 0a 53 51  List*,Expr*);.SQ
6fbd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6fbe0 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  d sqlite3ExprLis
6fbf0 74 53 65 74 4e 61 6d 65 28 50 61 72 73 65 2a 2c  tSetName(Parse*,
6fc00 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b 65 6e 2a  ExprList*,Token*
6fc10 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
6fc20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6fc30 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 61  e3ExprListSetSpa
6fc40 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73  n(Parse*,ExprLis
6fc50 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a 53  t*,ExprSpan*);.S
6fc60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6fc70 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  id sqlite3ExprLi
6fc80 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  stDelete(sqlite3
6fc90 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53  *, ExprList*);.S
6fca0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6fcb0 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 71  t sqlite3Init(sq
6fcc0 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29 3b  lite3*, char**);
6fcd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fce0 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 43  int sqlite3InitC
6fcf0 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20 69  allback(void*, i
6fd00 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61 72  nt, char**, char
6fd10 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
6fd20 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6fd30 50 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54 6f  Pragma(Parse*,To
6fd40 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65  ken*,Token*,Toke
6fd50 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  n*,int);.SQLITE_
6fd60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fd70 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
6fd80 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 2a  lSchema(sqlite3*
6fd90 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6fda0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6fdb0 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 61  te3BeginParse(Pa
6fdc0 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  rse*,int);.SQLIT
6fdd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6fde0 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65  qlite3CommitInte
6fdf0 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c 69  rnalChanges(sqli
6fe00 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
6fe10 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
6fe20 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
6fe30 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 53 65 6c  elect(Parse*,Sel
6fe40 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ect*);.SQLITE_PR
6fe50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6fe60 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c  e3OpenMasterTabl
6fe70 65 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 29 3b  e(Parse *, int);
6fe80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fe90 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 72  void sqlite3Star
6fea0 74 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f  tTable(Parse*,To
6feb0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c  ken*,Token*,int,
6fec0 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  int,int,int);.SQ
6fed0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6fee0 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75  d sqlite3AddColu
6fef0 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a  mn(Parse*,Token*
6ff00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ff10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
6ff20 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a 2c  dNotNull(Parse*,
6ff30 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6ff40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6ff50 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28  e3AddPrimaryKey(
6ff60 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74  Parse*, ExprList
6ff70 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
6ff80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ff90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
6ffa0 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74  dCheckConstraint
6ffb0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b  (Parse*, Expr*);
6ffc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ffd0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43  void sqlite3AddC
6ffe0 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 2a  olumnType(Parse*
6fff0 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45  ,Token*);.SQLITE
70000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70010 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56  lite3AddDefaultV
70020 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 78 70 72  alue(Parse*,Expr
70030 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Span*);.SQLITE_P
70040 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70050 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70  te3AddCollateTyp
70060 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
70070 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70080 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e  E void sqlite3En
70090 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f  dTable(Parse*,To
700a0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65  ken*,Token*,Sele
700b0 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  ct*);..SQLITE_PR
700c0 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 71  IVATE Bitvec *sq
700d0 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
700e0 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50  e(u32);.SQLITE_P
700f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
70100 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69 74  e3BitvecTest(Bit
70110 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49  vec*, u32);.SQLI
70120 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
70130 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
70140 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53  Bitvec*, u32);.S
70150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
70160 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  id sqlite3Bitvec
70170 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20 75  Clear(Bitvec*, u
70180 33 32 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  32, void*);.SQLI
70190 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
701a0 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
701b0 74 72 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a 53  troy(Bitvec*);.S
701c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
701d0 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  2 sqlite3BitvecS
701e0 69 7a 65 28 42 69 74 76 65 63 2a 29 3b 0a 53 51  ize(Bitvec*);.SQ
701f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
70200 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75   sqlite3BitvecBu
70210 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 2c 69 6e  iltinTest(int,in
70220 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  t*);..SQLITE_PRI
70230 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c  VATE RowSet *sql
70240 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73  ite3RowSetInit(s
70250 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c 20  qlite3*, void*, 
70260 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 53  unsigned int);.S
70270 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
70280 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  id sqlite3RowSet
70290 43 6c 65 61 72 28 52 6f 77 53 65 74 2a 29 3b 0a  Clear(RowSet*);.
702a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
702b0 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65  oid sqlite3RowSe
702c0 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 2a 2c  tInsert(RowSet*,
702d0 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52   i64);.SQLITE_PR
702e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
702f0 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 53  3RowSetTest(RowS
70300 65 74 2a 2c 20 75 38 20 69 42 61 74 63 68 2c 20  et*, u8 iBatch, 
70310 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  i64);.SQLITE_PRI
70320 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
70330 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65  RowSetNext(RowSe
70340 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a 53 51 4c 49  t*, i64*);..SQLI
70350 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70360 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 65  sqlite3CreateVie
70370 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c  w(Parse*,Token*,
70380 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65  Token*,Token*,Se
70390 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  lect*,int,int);.
703a0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
703b0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
703c0 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
703d0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
703e0 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49  ABLE).SQLITE_PRI
703f0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
70400 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
70410 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 61 62 6c  ames(Parse*,Tabl
70420 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  e*);.#else.# def
70430 69 6e 65 20 73 71 6c 69 74 65 33 56 69 65 77 47  ine sqlite3ViewG
70440 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 2c  etColumnNames(A,
70450 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c  B) 0.#endif..SQL
70460 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70470 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c   sqlite3DropTabl
70480 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73  e(Parse*, SrcLis
70490 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  t*, int, int);.S
704a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
704b0 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
704c0 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a 23  Table(Table*);.#
704d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
704e0 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
704f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70500 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75    void sqlite3Au
70510 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e  toincrementBegin
70520 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b  (Parse *pParse);
70530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70540 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75    void sqlite3Au
70550 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 50  toincrementEnd(P
70560 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a 23  arse *pParse);.#
70570 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
70580 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65  lite3Autoincreme
70590 6e 74 42 65 67 69 6e 28 58 29 0a 23 20 64 65 66  ntBegin(X).# def
705a0 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69  ine sqlite3Autoi
705b0 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58 29 0a 23  ncrementEnd(X).#
705c0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
705d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
705e0 33 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c 20  3Insert(Parse*, 
705f0 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69  SrcList*, ExprLi
70600 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49 64  st*, Select*, Id
70610 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  List*, int);.SQL
70620 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70630 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c   *sqlite3ArrayAl
70640 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 33 2a 2c  locate(sqlite3*,
70650 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e  void*,int,int,in
70660 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 53  t*,int*,int*);.S
70670 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64  QLITE_PRIVATE Id
70680 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c  List *sqlite3IdL
70690 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 65  istAppend(sqlite
706a0 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 54 6f 6b  3*, IdList*, Tok
706b0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
706c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
706d0 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 69  IdListIndex(IdLi
706e0 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  st*,const char*)
706f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70700 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
70710 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28  3SrcListEnlarge(
70720 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73  sqlite3*, SrcLis
70730 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  t*, int, int);.S
70740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72  QLITE_PRIVATE Sr
70750 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72  cList *sqlite3Sr
70760 63 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69  cListAppend(sqli
70770 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  te3*, SrcList*, 
70780 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Token*, Token*);
70790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
707a0 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  SrcList *sqlite3
707b0 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f  SrcListAppendFro
707c0 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c 20 53 72  mTerm(Parse*, Sr
707d0 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  cList*, Token*, 
707e0 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20  Token*,.        
707f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f                To
70810 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45  ken*, Select*, E
70820 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a  xpr*, IdList*);.
70830 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70840 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
70850 73 74 49 6e 64 65 78 65 64 42 79 28 50 61 72 73  stIndexedBy(Pars
70860 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20  e *, SrcList *, 
70870 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45  Token *);.SQLITE
70880 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
70890 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f  ite3IndexedByLoo
708a0 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 73 74 72  kup(Parse *, str
708b0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
708c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
708d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
708e0 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e  SrcListShiftJoin
708f0 54 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b 0a  Type(SrcList*);.
70900 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70910 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
70920 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
70930 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
70940 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70950 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64  E void sqlite3Id
70960 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
70970 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53  e3*, IdList*);.S
70980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
70990 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
709a0 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a  tDelete(sqlite3*
709b0 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c  , SrcList*);.SQL
709c0 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65  ITE_PRIVATE Inde
709d0 78 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65  x *sqlite3Create
709e0 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f 6b  Index(Parse*,Tok
709f0 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c 69  en*,Token*,SrcLi
70a00 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e  st*,ExprList*,in
70a10 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20  t,Token*,.      
70a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70a30 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20 69    Token*, int, i
70a40 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
70a50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70a60 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 2a  DropIndex(Parse*
70a70 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29  , SrcList*, int)
70a80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70a90 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65   int sqlite3Sele
70aa0 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  ct(Parse*, Selec
70ab0 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a 29  t*, SelectDest*)
70ac0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70ad0 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33   Select *sqlite3
70ae0 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65 2a  SelectNew(Parse*
70af0 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c 69  ,ExprList*,SrcLi
70b00 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c 69  st*,Expr*,ExprLi
70b10 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  st*,.           
70b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78                Ex
70b30 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e  pr*,ExprList*,in
70b40 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a  t,Expr*,Expr*);.
70b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70b60 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
70b70 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a  tDelete(sqlite3*
70b80 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  , Select*);.SQLI
70b90 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65  TE_PRIVATE Table
70ba0 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
70bb0 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20 53  Lookup(Parse*, S
70bc0 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
70bd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
70be0 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50  ite3IsReadOnly(P
70bf0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
70c00 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
70c10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70c20 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65 2a  OpenTable(Parse*
70c30 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74 20  , int iCur, int 
70c40 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  iDb, Table*, int
70c50 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  );.#if defined(S
70c60 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
70c70 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
70c80 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
70c90 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
70ca0 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  RY).SQLITE_PRIVA
70cb0 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
70cc0 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73 65  LimitWhere(Parse
70cd0 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 45   *, SrcList *, E
70ce0 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74 20  xpr *, ExprList 
70cf0 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20  *, Expr *, Expr 
70d00 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64  *, char *);.#end
70d10 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
70d20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
70d30 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a 2c  leteFrom(Parse*,
70d40 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a   SrcList*, Expr*
70d50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70d60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 70  E void sqlite3Up
70d70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72 63  date(Parse*, Src
70d80 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a  List*, ExprList*
70d90 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53  , Expr*, int);.S
70da0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 68  QLITE_PRIVATE Wh
70db0 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33  ereInfo *sqlite3
70dc0 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73 65  WhereBegin(Parse
70dd0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
70de0 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c 20  r*, ExprList**, 
70df0 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u16);.SQLITE_PRI
70e00 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70e10 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65 49  3WhereEnd(WhereI
70e20 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nfo*);.SQLITE_PR
70e30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
70e40 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75  3ExprCodeGetColu
70e50 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  mn(Parse*, Table
70e60 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
70e70 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70e80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70e90 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
70ea0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Parse*, int, int
70eb0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70ec0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70ed0 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28  te3ExprCodeCopy(
70ee0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Parse*, int, int
70ef0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70f00 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70f10 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72  te3ExprCacheStor
70f20 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69  e(Parse*, int, i
70f30 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
70f40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70f50 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75  lite3ExprCachePu
70f60 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  sh(Parse*);.SQLI
70f70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70f80 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
70f90 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  Pop(Parse*, int)
70fa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70fb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
70fc0 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72  rCacheRemove(Par
70fd0 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  se*, int);.SQLIT
70fe0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70ff0 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
71000 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51  lear(Parse*);.SQ
71010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71020 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
71030 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
71040 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
71050 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
71060 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
71070 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72 73  xprHardCopy(Pars
71080 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  e*,int,int);.SQL
71090 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
710a0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
710b0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
710c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
710d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
710e0 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73  xprCodeTemp(Pars
710f0 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a 29  e*, Expr*, int*)
71100 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71110 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
71120 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 65  CodeTarget(Parse
71130 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a  *, Expr*, int);.
71140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
71150 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
71160 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65  deAndCache(Parse
71170 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a  *, Expr*, int);.
71180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
71190 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
711a0 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72  odeConstants(Par
711b0 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  se*, Expr*);.SQL
711c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
711d0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45  sqlite3ExprCodeE
711e0 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a 2c 20  xprList(Parse*, 
711f0 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20  ExprList*, int, 
71200 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71210 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71220 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73  3ExprIfTrue(Pars
71230 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20  e*, Expr*, int, 
71240 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71250 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71260 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 61 72  3ExprIfFalse(Par
71270 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c  se*, Expr*, int,
71280 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
71290 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
712a0 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71  ite3FindTable(sq
712b0 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
712c0 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29  r*, const char*)
712d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
712e0 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c   Table *sqlite3L
712f0 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73 65  ocateTable(Parse
71300 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 6f 6e  *,int isView,con
71310 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20  st char*, const 
71320 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
71330 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71  RIVATE Index *sq
71340 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 73  lite3FindIndex(s
71350 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
71360 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  ar*, const char*
71370 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71380 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e  E void sqlite3Un
71390 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62  linkAndDeleteTab
713a0 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  le(sqlite3*,int,
713b0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
713c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
713d0 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
713e0 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73 71  ndDeleteIndex(sq
713f0 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74  lite3*,int,const
71400 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
71410 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71420 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73 65  ite3Vacuum(Parse
71430 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71440 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75  TE int sqlite3Ru
71450 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c 20  nVacuum(char**, 
71460 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
71470 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
71480 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
71490 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 54  oken(sqlite3*, T
714a0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
714b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
714c0 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78  e3ExprCompare(Ex
714d0 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  pr*, Expr*);.SQL
714e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
714f0 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
71500 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e 61  yzeAggregates(Na
71510 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72  meContext*, Expr
71520 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71530 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
71540 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73  xprAnalyzeAggLis
71550 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 45  t(NameContext*,E
71560 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  xprList*);.SQLIT
71570 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a  E_PRIVATE Vdbe *
71580 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 50  sqlite3GetVdbe(P
71590 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
715a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
715b0 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65  te3PrngSaveState
715c0 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50  (void);.SQLITE_P
715d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
715e0 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 74  te3PrngRestoreSt
715f0 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ate(void);.SQLIT
71600 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71610 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 53  qlite3PrngResetS
71620 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49  tate(void);.SQLI
71630 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71640 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41  sqlite3RollbackA
71650 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ll(sqlite3*);.SQ
71660 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71670 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72  d sqlite3CodeVer
71680 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65 2a  ifySchema(Parse*
71690 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
716a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
716b0 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74  te3BeginTransact
716c0 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  ion(Parse*, int)
716d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
716e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
716f0 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 50  mitTransaction(P
71700 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
71710 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71720 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73  te3RollbackTrans
71730 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a  action(Parse*);.
71740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
71750 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65 70  oid sqlite3Savep
71760 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e 74  oint(Parse*, int
71770 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
71780 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71790 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70  qlite3CloseSavep
717a0 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 29  oints(sqlite3 *)
717b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
717c0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
717d0 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 2a  IsConstant(Expr*
717e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
717f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
71800 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f  rIsConstantNotJo
71810 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  in(Expr*);.SQLIT
71820 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
71830 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
71840 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78  antOrFunction(Ex
71850 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
71860 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
71870 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78  ExprIsInteger(Ex
71880 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  pr*, int*);.SQLI
71890 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
718a0 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e  qlite3ExprCanBeN
718b0 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 2a 29  ull(const Expr*)
718c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
718d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
718e0 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28  rCodeIsNullJump(
718f0 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 45 78 70  Vdbe*, const Exp
71900 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  r*, int, int);.S
71910 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
71920 74 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65  t sqlite3ExprNee
71930 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e  dsNoAffinityChan
71940 67 65 28 63 6f 6e 73 74 20 45 78 70 72 2a 2c 20  ge(const Expr*, 
71950 63 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  char);.SQLITE_PR
71960 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
71970 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63  3IsRowid(const c
71980 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
71990 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
719a0 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
719b0 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  ete(Parse*, Tabl
719c0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  e*, int, int, in
719d0 74 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e  t, Trigger *, in
719e0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
719f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
71a00 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44  enerateRowIndexD
71a10 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61  elete(Parse*, Ta
71a20 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29  ble*, int, int*)
71a30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71a40 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65   int sqlite3Gene
71a50 72 61 74 65 49 6e 64 65 78 4b 65 79 28 50 61 72  rateIndexKey(Par
71a60 73 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e 74  se*, Index*, int
71a70 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
71a80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
71a90 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
71aa0 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
71ab0 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c 69  (Parse*,Table*,i
71ac0 6e 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 20 20  nt,int,.        
71ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
71af0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  *,int,int,int,in
71b00 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  t,int*);.SQLITE_
71b10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71b20 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65  ite3CompleteInse
71b30 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 54 61  rtion(Parse*, Ta
71b40 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ble*, int, int, 
71b50 69 6e 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  int*, int, int, 
71b60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71b70 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
71b80 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69  OpenTableAndIndi
71b90 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  ces(Parse*, Tabl
71ba0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  e*, int, int);.S
71bb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71bc0 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57  id sqlite3BeginW
71bd0 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61  riteOperation(Pa
71be0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  rse*, int, int);
71bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71c00 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 75 6c 74  void sqlite3Mult
71c10 69 57 72 69 74 65 28 50 61 72 73 65 2a 29 3b 0a  iWrite(Parse*);.
71c20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
71c30 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 79 41 62  oid sqlite3MayAb
71c40 6f 72 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ort(Parse*);.SQL
71c50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
71c60 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73   sqlite3HaltCons
71c70 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 69  traint(Parse*, i
71c80 6e 74 2c 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  nt, char*, int);
71c90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71ca0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
71cb0 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78  rDup(sqlite3*,Ex
71cc0 70 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  pr*,int);.SQLITE
71cd0 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
71ce0 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
71cf0 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45  stDup(sqlite3*,E
71d00 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53  xprList*,int);.S
71d10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72  QLITE_PRIVATE Sr
71d20 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72  cList *sqlite3Sr
71d30 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33  cListDup(sqlite3
71d40 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3b  *,SrcList*,int);
71d50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71d60 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49  IdList *sqlite3I
71d70 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33  dListDup(sqlite3
71d80 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  *,IdList*);.SQLI
71d90 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63  TE_PRIVATE Selec
71da0 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74  t *sqlite3Select
71db0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c  Dup(sqlite3*,Sel
71dc0 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ect*,int);.SQLIT
71dd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71de0 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73  qlite3FuncDefIns
71df0 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 2a  ert(FuncDefHash*
71e00 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c  , FuncDef*);.SQL
71e10 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63  ITE_PRIVATE Func
71e20 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  Def *sqlite3Find
71e30 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
71e40 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
71e50 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a 53  t,int,u8,int);.S
71e60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71e70 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
71e80 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f  erBuiltinFunctio
71e90 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ns(sqlite3*);.SQ
71ea0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71eb0 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
71ec0 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f  rDateTimeFunctio
71ed0 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  ns(void);.SQLITE
71ee0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71ef0 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f  lite3RegisterGlo
71f00 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69  balFunctions(voi
71f10 64 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  d);.#ifdef SQLIT
71f20 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
71f30 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
71f40 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c  ite3SafetyOn(sql
71f50 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
71f60 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
71f70 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 71  ite3SafetyOff(sq
71f80 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23  lite3*);.#else.#
71f90 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53   define sqlite3S
71fa0 61 66 65 74 79 4f 6e 28 41 29 20 30 0a 23 20 64  afetyOn(A) 0.# d
71fb0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66  efine sqlite3Saf
71fc0 65 74 79 4f 66 66 28 41 29 20 30 0a 23 65 6e 64  etyOff(A) 0.#end
71fd0 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
71fe0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
71ff0 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74  etyCheckOk(sqlit
72000 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
72010 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72020 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f  SafetyCheckSickO
72030 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  rOk(sqlite3*);.S
72040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
72050 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65  id sqlite3Change
72060 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 69  Cookie(Parse*, i
72070 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e  nt);..#if !defin
72080 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
72090 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64  IEW) && !defined
720a0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
720b0 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49  GGER).SQLITE_PRI
720c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
720d0 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77  3MaterializeView
720e0 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
720f0 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65   Expr*, int);.#e
72100 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
72110 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
72120 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  R.SQLITE_PRIVATE
72130 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
72140 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72 73  eginTrigger(Pars
72150 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  e*, Token*,Token
72160 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74  *,int,int,IdList
72170 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20  *,SrcList*,.    
72180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72190 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e 74         Expr*,int
721a0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
721b0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
721c0 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67  lite3FinishTrigg
721d0 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67  er(Parse*, Trigg
721e0 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29  erStep*, Token*)
721f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72200 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44     void sqlite3D
72210 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 65  ropTrigger(Parse
72220 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74  *, SrcList*, int
72230 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72240 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
72250 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 50  DropTriggerPtr(P
72260 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a 29  arse*, Trigger*)
72270 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72280 20 20 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69     Trigger *sqli
72290 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74  te3TriggersExist
722a0 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 2a  (Parse *, Table*
722b0 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a  , int, ExprList*
722c0 2c 20 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a 53  , int *pMask);.S
722d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
722e0 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33  Trigger *sqlite3
722f0 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72 73  TriggerList(Pars
72300 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53  e *, Table *);.S
72310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
72320 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65  void sqlite3Code
72330 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73 65  RowTrigger(Parse
72340 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e  *, Trigger *, in
72350 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  t, ExprList*, in
72360 74 2c 20 54 61 62 6c 65 20 2a 2c 0a 20 20 20 20  t, Table *,.    
72370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72380 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74          int, int
72390 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
723a0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
723b0 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
723c0 67 65 72 44 69 72 65 63 74 28 50 61 72 73 65 20  gerDirect(Parse 
723d0 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 54 61  *, Trigger *, Ta
723e0 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  ble *, int, int,
723f0 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 71   int);.  void sq
72400 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73  liteViewTriggers
72410 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
72420 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70   Expr*, int, Exp
72430 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
72440 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
72450 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
72460 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a  gerStep(sqlite3*
72470 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b  , TriggerStep*);
72480 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72490 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73    TriggerStep *s
724a0 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c  qlite3TriggerSel
724b0 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a  ectStep(sqlite3*
724c0 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54  ,Select*);.SQLIT
724d0 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67  E_PRIVATE   Trig
724e0 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
724f0 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65  TriggerInsertSte
72500 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e  p(sqlite3*,Token
72510 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20  *, IdList*,.    
72520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72540 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65      ExprList*,Se
72550 6c 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49 54  lect*,u8);.SQLIT
72560 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67  E_PRIVATE   Trig
72570 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
72580 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65  TriggerUpdateSte
72590 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e  p(sqlite3*,Token
725a0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70  *,ExprList*, Exp
725b0 72 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f  r*, u8);.SQLITE_
725c0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
725d0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
725e0 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28  iggerDeleteStep(
725f0 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c  sqlite3*,Token*,
72600 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
72610 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
72620 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
72630 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72  ger(sqlite3*, Tr
72640 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  igger*);.SQLITE_
72650 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
72660 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
72670 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c  eleteTrigger(sql
72680 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  ite3*,int,const 
72690 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
726a0 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c  RIVATE   u32 sql
726b0 69 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d 61  ite3TriggerOldma
726c0 73 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67 65  sk(Parse*,Trigge
726d0 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 61 62  r*,ExprList*,Tab
726e0 6c 65 2a 2c 69 6e 74 29 3b 0a 23 20 64 65 66 69  le*,int);.# defi
726f0 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54  ne sqlite3ParseT
72700 6f 70 6c 65 76 65 6c 28 70 29 20 28 28 70 29 2d  oplevel(p) ((p)-
72710 3e 70 54 6f 70 6c 65 76 65 6c 20 3f 20 28 70 29  >pToplevel ? (p)
72720 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3a 20 28 70  ->pToplevel : (p
72730 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  )).#else.# defin
72740 65 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  e sqlite3Trigger
72750 73 45 78 69 73 74 28 42 2c 43 2c 44 2c 45 2c 46  sExist(B,C,D,E,F
72760 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ) 0.# define sql
72770 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
72780 72 28 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20  r(A,B).# define 
72790 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
727a0 65 72 50 74 72 28 41 2c 42 29 0a 23 20 64 65 66  erPtr(A,B).# def
727b0 69 6e 65 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  ine sqlite3Unlin
727c0 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65  kAndDeleteTrigge
727d0 72 28 41 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e  r(A,B,C).# defin
727e0 65 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77  e sqlite3CodeRow
727f0 54 72 69 67 67 65 72 28 41 2c 42 2c 43 2c 44 2c  Trigger(A,B,C,D,
72800 45 2c 46 2c 47 2c 48 2c 49 29 0a 23 20 64 65 66  E,F,G,H,I).# def
72810 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64 65 52  ine sqlite3CodeR
72820 6f 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28  owTriggerDirect(
72830 41 2c 42 2c 43 2c 44 2c 45 2c 46 29 0a 23 20 64  A,B,C,D,E,F).# d
72840 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69  efine sqlite3Tri
72850 67 67 65 72 4c 69 73 74 28 58 2c 20 59 29 20 30  ggerList(X, Y) 0
72860 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
72870 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
72880 29 20 70 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ) p.# define sql
72890 69 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d 61  ite3TriggerOldma
728a0 73 6b 28 41 2c 42 2c 43 2c 44 2c 45 29 20 30 0a  sk(A,B,C,D,E) 0.
728b0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
728c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
728d0 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65  e3JoinType(Parse
728e0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
728f0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
72900 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
72910 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72  sqlite3CreateFor
72920 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20  eignKey(Parse*, 
72930 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e  ExprList*, Token
72940 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
72950 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
72960 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
72970 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50  eferForeignKey(P
72980 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66  arse*, int);.#if
72990 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
729a0 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53  _AUTHORIZATION.S
729b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
729c0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
729d0 52 65 61 64 28 50 61 72 73 65 2a 2c 45 78 70 72  Read(Parse*,Expr
729e0 2a 2c 53 63 68 65 6d 61 2a 2c 53 72 63 4c 69 73  *,Schema*,SrcLis
729f0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
72a00 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
72a10 33 41 75 74 68 43 68 65 63 6b 28 50 61 72 73 65  3AuthCheck(Parse
72a20 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  *,int, const cha
72a30 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  r*, const char*,
72a40 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53   const char*);.S
72a50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
72a60 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
72a70 43 6f 6e 74 65 78 74 50 75 73 68 28 50 61 72 73  ContextPush(Pars
72a80 65 2a 2c 20 41 75 74 68 43 6f 6e 74 65 78 74 2a  e*, AuthContext*
72a90 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
72aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
72ab0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
72ac0 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68  hContextPop(Auth
72ad0 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  Context*);.SQLIT
72ae0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
72af0 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 43  sqlite3AuthReadC
72b00 6f 6c 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74  ol(Parse*, const
72b10 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63   char *, const c
72b20 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c  har *, int);.#el
72b30 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  se.# define sqli
72b40 74 65 33 41 75 74 68 52 65 61 64 28 61 2c 62 2c  te3AuthRead(a,b,
72b50 63 2c 64 29 0a 23 20 64 65 66 69 6e 65 20 73 71  c,d).# define sq
72b60 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 61  lite3AuthCheck(a
72b70 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 53 51 4c  ,b,c,d,e)    SQL
72b80 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20  ITE_OK.# define 
72b90 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
72ba0 78 74 50 75 73 68 28 61 2c 62 2c 63 29 0a 23 20  xtPush(a,b,c).# 
72bb0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75  define sqlite3Au
72bc0 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 61 29 20  thContextPop(a) 
72bd0 20 28 28 76 6f 69 64 29 28 61 29 29 0a 23 65 6e   ((void)(a)).#en
72be0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
72bf0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
72c00 74 74 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78  ttach(Parse*, Ex
72c10 70 72 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72  pr*, Expr*, Expr
72c20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72c30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
72c40 65 74 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78  etach(Parse*, Ex
72c50 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
72c60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72c70 42 74 72 65 65 46 61 63 74 6f 72 79 28 73 71 6c  BtreeFactory(sql
72c80 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
72c90 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
72ca0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
72cb0 20 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74          int omit
72cc0 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61  Journal, int nCa
72cd0 63 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20  che, int flags, 
72ce0 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29  Btree **ppBtree)
72cf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72d00 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49   int sqlite3FixI
72d10 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61  nit(DbFixer*, Pa
72d20 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  rse*, int, const
72d30 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f   char*, const To
72d40 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
72d50 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
72d60 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69  3FixSrcList(DbFi
72d70 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b  xer*, SrcList*);
72d80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72d90 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65  int sqlite3FixSe
72da0 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53  lect(DbFixer*, S
72db0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
72dc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72dd0 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78  te3FixExpr(DbFix
72de0 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  er*, Expr*);.SQL
72df0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
72e00 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69  sqlite3FixExprLi
72e10 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70  st(DbFixer*, Exp
72e20 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
72e30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72e40 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
72e50 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67  p(DbFixer*, Trig
72e60 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54  gerStep*);.SQLIT
72e70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
72e80 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20  lite3AtoF(const 
72e90 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a  char *z, double*
72ea0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72eb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
72ec0 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72  Int32(const char
72ed0 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54   *, int*);.SQLIT
72ee0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
72ef0 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74  lite3FitsIn64Bit
72f00 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  s(const char *, 
72f10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
72f20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72f30 55 74 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e  Utf16ByteLen(con
72f40 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20  st void *pData, 
72f50 69 6e 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49  int nChar);.SQLI
72f60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72f70 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
72f80 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  n(const char *pD
72f90 61 74 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b  ata, int nByte);
72fa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72fb0 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 52  int sqlite3Utf8R
72fc0 65 61 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63  ead(const u8*, c
72fd0 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a  onst u8**);../*.
72fe0 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72  ** Routines to r
72ff0 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 61  ead and write va
73000 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
73010 74 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 75  tegers.  These u
73020 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66  sed to.** be def
73030 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75  ined locally, bu
73040 74 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65  t now we use the
73050 20 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73   varint routines
73060 20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a   in the util.c.*
73070 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68  * file.  Code sh
73080 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43  ould use the MAC
73090 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20  RO forms below, 
730a0 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20  as the Varint32 
730b0 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  versions.** are 
730c0 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20  coded to assume 
730d0 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20  the single byte 
730e0 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20  case is already 
730f0 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a  handled (which .
73100 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72  ** the MACRO for
73110 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49  m does)..*/.SQLI
73120 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
73130 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28  qlite3PutVarint(
73140 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20  unsigned char*, 
73150 75 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u64);.SQLITE_PRI
73160 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
73170 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69  PutVarint32(unsi
73180 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29  gned char*, u32)
73190 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
731a0 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61   u8 sqlite3GetVa
731b0 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67  rint(const unsig
731c0 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20  ned char *, u64 
731d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
731e0 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74  TE u8 sqlite3Get
731f0 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75  Varint32(const u
73200 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20  nsigned char *, 
73210 75 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  u32 *);.SQLITE_P
73220 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73230 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20  e3VarintLen(u64 
73240 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  v);../*.** The h
73250 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72  eader of a recor
73260 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  d consists of a 
73270 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c  sequence variabl
73280 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
73290 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65  s..** These inte
732a0 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20  gers are almost 
732b0 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64  always small and
732c0 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20   are encoded as 
732d0 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a  a single byte..*
732e0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
732f0 6d 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61  macros take adva
73300 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20  ntage this fact 
73310 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73  to provide a fas
73320 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20  t encode.** and 
73330 64 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e  decode of the in
73340 74 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f  tegers in a reco
73350 72 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 69  rd header.  It i
73360 73 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 65  s faster for the
73370 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20   common.** case 
73380 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65  where the intege
73390 72 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79  r is a single by
733a0 74 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74  te.  It is a lit
733b0 74 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20  tle slower when 
733c0 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69  the.** integer i
733d0 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79  s two or more by
733e0 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c  tes.  But overal
733f0 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a  l it is faster..
73400 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
73410 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ing expressions 
73420 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a  are equivalent:.
73430 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71  **.**     x = sq
73440 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
73450 28 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20  ( A, &B );.**   
73460 20 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74    x = sqlite3Put
73470 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29  Varint32( A, B )
73480 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20  ;.**.**     x = 
73490 67 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20  getVarint32( A, 
734a0 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20  B );.**     x = 
734b0 70 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20  putVarint32( A, 
734c0 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69  B );.**.*/.#defi
734d0 6e 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 41  ne getVarint32(A
734e0 2c 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c  ,B)  (u8)((*(A)<
734f0 28 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 29  (u8)0x80) ? ((B)
73500 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20   = (u32)*(A)),1 
73510 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  : sqlite3GetVari
73520 6e 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a  nt32((A), (u32 *
73530 29 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20  )&(B))).#define 
73540 70 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29  putVarint32(A,B)
73550 20 20 28 75 38 29 28 28 28 75 33 32 29 28 42 29    (u8)(((u32)(B)
73560 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a  <(u32)0x80) ? (*
73570 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  (A) = (unsigned 
73580 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71  char)(B)),1 : sq
73590 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32  lite3PutVarint32
735a0 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66  ((A), (B))).#def
735b0 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 20  ine getVarint   
735c0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
735d0 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72  t.#define putVar
735e0 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 75  int    sqlite3Pu
735f0 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45  tVarint...SQLITE
73600 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
73610 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65  har *sqlite3Inde
73620 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62  xAffinityStr(Vdb
73630 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53  e *, Index *);.S
73640 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73650 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41  id sqlite3TableA
73660 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20  ffinityStr(Vdbe 
73670 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c  *, Table *);.SQL
73680 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
73690 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
736a0 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45  ffinity(Expr *pE
736b0 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 3b  xpr, char aff2);
736c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
736d0 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78  int sqlite3Index
736e0 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20  AffinityOk(Expr 
736f0 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78  *pExpr, char idx
73700 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49  _affinity);.SQLI
73710 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
73720 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
73730 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29  ity(Expr *pExpr)
73740 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73750 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69   int sqlite3Atoi
73760 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  64(const char*, 
73770 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  i64*);.SQLITE_PR
73780 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
73790 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 2a  e3Error(sqlite3*
737a0 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  , int, const cha
737b0 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  r*,...);.SQLITE_
737c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
737d0 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73  lite3HexToBlob(s
737e0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63  qlite3*, const c
737f0 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a  har *z, int n);.
73800 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
73810 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72  nt sqlite3TwoPar
73820 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 54  tName(Parse *, T
73830 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c  oken *, Token *,
73840 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49   Token **);.SQLI
73850 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
73860 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72   char *sqlite3Er
73870 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54  rStr(int);.SQLIT
73880 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
73890 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
738a0 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a  Parse *pParse);.
738b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
738c0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 46  ollSeq *sqlite3F
738d0 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74  indCollSeq(sqlit
738e0 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73  e3*,u8 enc, cons
738f0 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51  t char*,int);.SQ
73900 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
73910 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63  lSeq *sqlite3Loc
73920 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  ateCollSeq(Parse
73930 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
73940 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a 53 51 4c  char*zName);.SQL
73950 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
73960 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72  Seq *sqlite3Expr
73970 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70  CollSeq(Parse *p
73980 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
73990 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  pr);.SQLITE_PRIV
739a0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
739b0 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72  3ExprSetColl(Par
739c0 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
739d0 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51   *, Token *);.SQ
739e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
739f0 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c   sqlite3CheckCol
73a00 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 43 6f  lSeq(Parse *, Co
73a10 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45  llSeq *);.SQLITE
73a20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
73a30 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e  ite3CheckObjectN
73a40 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e  ame(Parse *, con
73a50 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49  st char *);.SQLI
73a60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73a70 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68  sqlite3VdbeSetCh
73a80 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 2c  anges(sqlite3 *,
73a90 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
73aa0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69  RIVATE const voi
73ab0 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54  d *sqlite3ValueT
73ac0 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ext(sqlite3_valu
73ad0 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f  e*, u8);.SQLITE_
73ae0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73af0 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73 71  te3ValueBytes(sq
73b00 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38  lite3_value*, u8
73b10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73b20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
73b30 6c 75 65 53 65 74 53 74 72 28 73 71 6c 69 74 65  lueSetStr(sqlite
73b40 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63  3_value*, int, c
73b50 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20  onst void *,u8, 
73b60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
73b70 20 20 20 20 20 20 20 20 20 76 6f 69 64 28 2a 29           void(*)
73b80 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45  (void*));.SQLITE
73b90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
73ba0 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 73  lite3ValueFree(s
73bb0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
73bc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
73bd0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71  qlite3_value *sq
73be0 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71  lite3ValueNew(sq
73bf0 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45  lite3 *);.SQLITE
73c00 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
73c10 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 73  qlite3Utf16to8(s
73c20 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20  qlite3 *, const 
73c30 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 23 69 66  void*, int);.#if
73c40 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
73c50 45 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50  E_STAT2.SQLITE_P
73c60 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
73c70 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c  ite3Utf8to16(sql
73c80 69 74 65 33 20 2a 2c 20 75 38 2c 20 63 68 61 72  ite3 *, u8, char
73c90 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b   *, int, int *);
73ca0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
73cb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73cc0 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
73cd0 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20  sqlite3 *, Expr 
73ce0 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74  *, u8, u8, sqlit
73cf0 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51  e3_value **);.SQ
73d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
73d10 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70  d sqlite3ValueAp
73d20 70 6c 79 41 66 66 69 6e 69 74 79 28 73 71 6c 69  plyAffinity(sqli
73d30 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c  te3_value *, u8,
73d40 20 75 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 51   u8);.#ifndef SQ
73d50 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f  LITE_AMALGAMATIO
73d60 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  N.SQLITE_PRIVATE
73d70 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
73d80 63 68 61 72 20 73 71 6c 69 74 65 33 4f 70 63 6f  char sqlite3Opco
73d90 64 65 50 72 6f 70 65 72 74 79 5b 5d 3b 0a 53 51  deProperty[];.SQ
73da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
73db0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
73dc0 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c   sqlite3UpperToL
73dd0 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50  ower[];.SQLITE_P
73de0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73  RIVATE const uns
73df0 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74  igned char sqlit
73e00 65 33 43 74 79 70 65 4d 61 70 5b 5d 3b 0a 53 51  e3CtypeMap[];.SQ
73e10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c  LITE_PRIVATE SQL
73e20 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53  ITE_WSD struct S
73e30 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c  qlite3Config sql
73e40 69 74 65 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49  ite3Config;.SQLI
73e50 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54  TE_PRIVATE SQLIT
73e60 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73  E_WSD FuncDefHas
73e70 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46  h sqlite3GlobalF
73e80 75 6e 63 74 69 6f 6e 73 3b 0a 53 51 4c 49 54 45  unctions;.SQLITE
73e90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
73ea0 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 3b  ite3PendingByte;
73eb0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
73ec0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
73ed0 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64  te3RootPageMoved
73ee0 28 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  (Db*, int, int);
73ef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73f00 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e  void sqlite3Rein
73f10 64 65 78 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  dex(Parse*, Toke
73f20 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  n*, Token*);.SQL
73f30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73f40 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e   sqlite3AlterFun
73f50 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29  ctions(sqlite3*)
73f60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73f70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74   void sqlite3Alt
73f80 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 50 61  erRenameTable(Pa
73f90 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
73fa0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
73fb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73fc0 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73  te3GetToken(cons
73fd0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
73fe0 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54  *, int *);.SQLIT
73ff0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
74000 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
74010 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20  e(Parse*, const 
74020 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c  char*, ...);.SQL
74030 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74040 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
74050 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
74060 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
74070 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
74080 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c  qlite3CodeSubsel
74090 65 63 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70  ect(Parse *, Exp
740a0 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  r *, int, int);.
740b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
740c0 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
740d0 74 50 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65  tPrep(Parse*, Se
740e0 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65  lect*, NameConte
740f0 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  xt*);.SQLITE_PRI
74100 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
74110 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
74120 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45  (NameContext*, E
74130 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
74140 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74150 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e  e3ResolveSelectN
74160 61 6d 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c  ames(Parse*, Sel
74170 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78  ect*, NameContex
74180 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
74190 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
741a0 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
741b0 42 79 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  By(Parse*, Selec
741c0 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63  t*, ExprList*, c
741d0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
741e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
741f0 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
74200 66 61 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61  fault(Vdbe *, Ta
74210 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  ble *, int, int)
74220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74230 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74   void sqlite3Alt
74240 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d  erFinishAddColum
74250 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e  n(Parse *, Token
74260 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
74270 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
74280 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c  AlterBeginAddCol
74290 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63  umn(Parse *, Src
742a0 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  List *);.SQLITE_
742b0 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
742c0 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53  *sqlite3GetCollS
742d0 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c  eq(sqlite3*, u8,
742e0 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73   CollSeq *, cons
742f0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
74300 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
74310 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70  lite3AffinityTyp
74320 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  e(const char*);.
74330 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74340 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  oid sqlite3Analy
74350 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  ze(Parse*, Token
74360 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
74370 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
74380 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79  qlite3InvokeBusy
74390 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64  Handler(BusyHand
743a0 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ler*);.SQLITE_PR
743b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
743c0 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a  3FindDb(sqlite3*
743d0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
743e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
743f0 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28  lite3FindDbName(
74400 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74  sqlite3 *, const
74410 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
74420 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
74430 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64  ite3AnalysisLoad
74440 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44  (sqlite3*,int iD
74450 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  B);.SQLITE_PRIVA
74460 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
74470 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65  eleteIndexSample
74480 73 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54  s(Index*);.SQLIT
74490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
744a0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77  qlite3DefaultRow
744b0 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c  Est(Index*);.SQL
744c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
744d0 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
744e0 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71  LikeFunctions(sq
744f0 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
74500 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
74510 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75   sqlite3IsLikeFu
74520 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c  nction(sqlite3*,
74530 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a  Expr*,int*,char*
74540 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74550 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69  E void sqlite3Mi
74560 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28  nimumFileFormat(
74570 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Parse*, int, int
74580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74590 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63  E void sqlite3Sc
745a0 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29  hemaFree(void *)
745b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
745c0 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33   Schema *sqlite3
745d0 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65  SchemaGet(sqlite
745e0 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53  3 *, Btree *);.S
745f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
74600 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  t sqlite3SchemaT
74610 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a  oIndex(sqlite3 *
74620 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53  db, Schema *);.S
74630 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65  QLITE_PRIVATE Ke
74640 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e  yInfo *sqlite3In
74650 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65  dexKeyinfo(Parse
74660 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51   *, Index *);.SQ
74670 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
74680 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
74690 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f  nc(sqlite3 *, co
746a0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c  nst char *, int,
746b0 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20   int, void *, . 
746c0 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65   void (*)(sqlite
746d0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
746e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
746f0 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c  ,.  void (*)(sql
74700 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
74710 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
74720 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71  **), void (*)(sq
74730 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29  lite3_context*))
74740 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74750 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45   int sqlite3ApiE
74760 78 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  xit(sqlite3 *db,
74770 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
74780 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
74790 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
747a0 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c  e(Parse *);..SQL
747b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
747c0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
747d0 49 6e 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20  Init(StrAccum*, 
747e0 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  char*, int, int)
747f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74800 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
74810 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41  AccumAppend(StrA
74820 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ccum*,const char
74830 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
74840 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
74850 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
74860 73 68 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53  sh(StrAccum*);.S
74870 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74880 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  id sqlite3StrAcc
74890 75 6d 52 65 73 65 74 28 53 74 72 41 63 63 75 6d  umReset(StrAccum
748a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
748b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
748c0 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53 65  electDestInit(Se
748d0 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e  lectDest*,int,in
748e0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
748f0 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
74900 43 72 65 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72  CreateColumnExpr
74910 28 73 71 6c 69 74 65 33 20 2a 2c 20 53 72 63 4c  (sqlite3 *, SrcL
74920 69 73 74 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  ist *, int, int)
74930 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
74940 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61  E void sqlite3Ba
74950 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c 69  ckupRestart(sqli
74960 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53  te3_backup *);.S
74970 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74980 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70  id sqlite3Backup
74990 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f 62  Update(sqlite3_b
749a0 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63  ackup *, Pgno, c
749b0 6f 6e 73 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a  onst u8 *);../*.
749c0 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  ** The interface
749d0 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65   to the LEMON-ge
749e0 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 0a 2a  nerated parser.*
749f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
74a00 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
74a10 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28  rserAlloc(void*(
74a20 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c  *)(size_t));.SQL
74a30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74a40 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
74a50 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a  ee(void*, void(*
74a60 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
74a70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
74a80 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 6f 69  qlite3Parser(voi
74a90 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20  d*, int, Token, 
74aa0 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 66 20  Parse*);.#ifdef 
74ab0 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
74ac0 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56  EPTH.SQLITE_PRIV
74ad0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
74ae0 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b  3ParserStackPeak
74af0 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a  (void*);.#endif.
74b00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74b10 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 6f  void sqlite3Auto
74b20 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73  LoadExtensions(s
74b30 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65  qlite3*);.#ifnde
74b40 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
74b50 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c  AD_EXTENSION.SQL
74b60 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
74b70 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45  id sqlite3CloseE
74b80 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65  xtensions(sqlite
74b90 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  3*);.#else.# def
74ba0 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f 73 65  ine sqlite3Close
74bb0 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65  Extensions(X).#e
74bc0 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
74bd0 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
74be0 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52  _CACHE.SQLITE_PR
74bf0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
74c00 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61  ite3TableLock(Pa
74c10 72 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  rse *, int, int,
74c20 20 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   u8, const char 
74c30 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  *);.#else.  #def
74c40 69 6e 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ine sqlite3Table
74c50 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a  Lock(v,w,x,y,z).
74c60 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
74c70 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
74c80 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
74c90 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75  sqlite3Utf8To8(u
74ca0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a  nsigned char*);.
74cb0 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
74cc0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
74cd0 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e  ALTABLE.#  defin
74ce0 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65  e sqlite3VtabCle
74cf0 61 72 28 59 29 0a 23 20 20 64 65 66 69 6e 65 20  ar(Y).#  define 
74d00 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28  sqlite3VtabSync(
74d10 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23  X,Y) SQLITE_OK.#
74d20 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
74d30 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a  VtabRollback(X).
74d40 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  #  define sqlite
74d50 33 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23  3VtabCommit(X).#
74d60 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
74d70 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 30  VtabInSync(db) 0
74d80 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
74d90 65 33 56 74 61 62 4c 6f 63 6b 28 58 29 20 0a 23  e3VtabLock(X) .#
74da0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
74db0 56 74 61 62 55 6e 6c 6f 63 6b 28 58 29 0a 23 20  VtabUnlock(X).# 
74dc0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
74dd0 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 58 29  tabUnlockList(X)
74de0 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52  .#else.SQLITE_PR
74df0 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71  IVATE    void sq
74e00 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54  lite3VtabClear(T
74e10 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
74e20 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71  RIVATE    int sq
74e30 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71  lite3VtabSync(sq
74e40 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20  lite3 *db, char 
74e50 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
74e60 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74  ATE    int sqlit
74e70 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73  e3VtabRollback(s
74e80 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c  qlite3 *db);.SQL
74e90 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69  ITE_PRIVATE    i
74ea0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f  nt sqlite3VtabCo
74eb0 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  mmit(sqlite3 *db
74ec0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74ed0 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  E    void sqlite
74ee0 33 56 74 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65  3VtabLock(VTable
74ef0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
74f00 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69  ATE    void sqli
74f10 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54  te3VtabUnlock(VT
74f20 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  able *);.SQLITE_
74f30 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20  PRIVATE    void 
74f40 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
74f50 6b 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 29 3b  kList(sqlite3*);
74f60 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
74f70 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29  e3VtabInSync(db)
74f80 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e   ((db)->nVTrans>
74f90 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61  0 && (db)->aVTra
74fa0 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51  ns==0).#endif.SQ
74fb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
74fc0 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b  d sqlite3VtabMak
74fd0 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a  eWritable(Parse*
74fe0 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45  ,Table*);.SQLITE
74ff0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
75000 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61  lite3VtabBeginPa
75010 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  rse(Parse*, Toke
75020 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65  n*, Token*, Toke
75030 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
75040 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
75050 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28  VtabFinishParse(
75060 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Parse*, Token*);
75070 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75080 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
75090 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a 29 3b  ArgInit(Parse*);
750a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
750b0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
750c0 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 65 2a  ArgExtend(Parse*
750d0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
750e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
750f0 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65  lite3VtabCallCre
75100 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ate(sqlite3*, in
75110 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  t, const char *,
75120 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54   char **);.SQLIT
75130 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
75140 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e  lite3VtabCallCon
75150 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54 61 62  nect(Parse*, Tab
75160 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  le*);.SQLITE_PRI
75170 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
75180 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28  VtabCallDestroy(
75190 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63  sqlite3*, int, c
751a0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
751b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
751c0 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
751d0 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 56 54 61  n(sqlite3 *, VTa
751e0 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ble *);.SQLITE_P
751f0 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a  RIVATE FuncDef *
75200 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c  sqlite3VtabOverl
75210 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  oadFunction(sqli
75220 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20  te3 *,FuncDef*, 
75230 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 2a 29  int nArg, Expr*)
75240 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75250 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76   void sqlite3Inv
75260 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c  alidFunction(sql
75270 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
75280 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
75290 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
752a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72  TE int sqlite3Tr
752b0 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73  ansferBindings(s
752c0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73  qlite3_stmt *, s
752d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a  qlite3_stmt *);.
752e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
752f0 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70  nt sqlite3Reprep
75300 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  are(Vdbe*);.SQLI
75310 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
75320 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43  sqlite3ExprListC
75330 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 65  heckLength(Parse
75340 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f  *, ExprList*, co
75350 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
75360 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
75370 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72  eq *sqlite3Binar
75380 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28  yCompareCollSeq(
75390 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c  Parse *, Expr *,
753a0 20 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54 45   Expr *);.SQLITE
753b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
753c0 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79  ite3TempInMemory
753d0 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 2a 29  (const sqlite3*)
753e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
753f0 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33   VTable *sqlite3
75400 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65  GetVTable(sqlite
75410 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 0a 2f 2a  3*, Table*);../*
75420 20 44 65 63 6c 61 72 61 74 69 6f 6e 73 20 66 6f   Declarations fo
75430 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 66  r functions in f
75440 6b 65 79 2e 63 2e 20 41 6c 6c 20 6f 66 20 74 68  key.c. All of th
75450 65 73 65 20 61 72 65 20 72 65 70 6c 61 63 65 64  ese are replaced
75460 20 62 79 0a 2a 2a 20 6e 6f 2d 6f 70 20 6d 61 63   by.** no-op mac
75470 72 6f 73 20 69 66 20 4f 4d 49 54 5f 46 4f 52 45  ros if OMIT_FORE
75480 49 47 4e 5f 4b 45 59 20 69 73 20 64 65 66 69 6e  IGN_KEY is defin
75490 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
754a0 20 6e 6f 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b   no foreign.** k
754b0 65 79 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  ey functionality
754c0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 49   is available. I
754d0 66 20 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 69  f OMIT_TRIGGER i
754e0 73 20 64 65 66 69 6e 65 64 20 62 75 74 0a 2a 2a  s defined but.**
754f0 20 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45   OMIT_FOREIGN_KE
75500 59 20 69 73 20 6e 6f 74 2c 20 6f 6e 6c 79 20 73  Y is not, only s
75510 6f 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ome of the funct
75520 69 6f 6e 73 20 61 72 65 20 6e 6f 2d 6f 70 65 64  ions are no-oped
75530 2e 20 49 6e 0a 2a 2a 20 74 68 69 73 20 63 61 73  . In.** this cas
75540 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61  e foreign keys a
75550 72 65 20 70 61 72 73 65 64 2c 20 62 75 74 20 6e  re parsed, but n
75560 6f 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e  o other function
75570 61 6c 69 74 79 20 69 73 20 0a 2a 2a 20 70 72 6f  ality is .** pro
75580 76 69 64 65 64 20 28 65 6e 66 6f 72 63 65 6d 65  vided (enforceme
75590 6e 74 20 6f 66 20 46 4b 20 63 6f 6e 73 74 72 61  nt of FK constra
755a0 69 6e 74 73 20 72 65 71 75 69 72 65 73 20 74 68  ints requires th
755b0 65 20 74 72 69 67 67 65 72 73 20 73 75 62 2d 73  e triggers sub-s
755c0 79 73 74 65 6d 29 2e 0a 2a 2f 0a 23 69 66 20 21  ystem)..*/.#if !
755d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
755e0 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29  MIT_FOREIGN_KEY)
755f0 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
75600 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
75610 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
75620 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46     void sqlite3F
75630 6b 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 20 54  kCheck(Parse*, T
75640 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  able*, int, int)
75650 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75660 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46     void sqlite3F
75670 6b 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65  kDropTable(Parse
75680 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 54 61  *, SrcList *, Ta
75690 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ble*);.SQLITE_PR
756a0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
756b0 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 50 61  ite3FkActions(Pa
756c0 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78  rse*, Table*, Ex
756d0 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53  prList*, int);.S
756e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
756f0 69 6e 74 20 73 71 6c 69 74 65 33 46 6b 52 65 71  int sqlite3FkReq
75700 75 69 72 65 64 28 50 61 72 73 65 2a 2c 20 54 61  uired(Parse*, Ta
75710 62 6c 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29  ble*, int*, int)
75720 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75730 20 20 20 75 33 32 20 73 71 6c 69 74 65 33 46 6b     u32 sqlite3Fk
75740 4f 6c 64 6d 61 73 6b 28 50 61 72 73 65 2a 2c 20  Oldmask(Parse*, 
75750 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Table*);.SQLITE_
75760 50 52 49 56 41 54 45 20 20 20 46 4b 65 79 20 2a  PRIVATE   FKey *
75770 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e  sqlite3FkReferen
75780 63 65 73 28 54 61 62 6c 65 20 2a 29 3b 0a 23 65  ces(Table *);.#e
75790 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
757a0 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 61  lite3FkActions(a
757b0 2c 62 2c 63 2c 64 29 0a 20 20 23 64 65 66 69 6e  ,b,c,d).  #defin
757c0 65 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b  e sqlite3FkCheck
757d0 28 61 2c 62 2c 63 2c 64 29 0a 20 20 23 64 65 66  (a,b,c,d).  #def
757e0 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 44 72 6f  ine sqlite3FkDro
757f0 70 54 61 62 6c 65 28 61 2c 62 2c 63 29 0a 20 20  pTable(a,b,c).  
75800 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46  #define sqlite3F
75810 6b 4f 6c 64 6d 61 73 6b 28 61 2c 62 29 20 20 20  kOldmask(a,b)   
75820 20 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 73     0.  #define s
75830 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64  qlite3FkRequired
75840 28 61 2c 62 2c 63 2c 64 29 20 30 0a 23 65 6e 64  (a,b,c,d) 0.#end
75850 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
75860 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
75870 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  EY.SQLITE_PRIVAT
75880 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
75890 46 6b 44 65 6c 65 74 65 28 54 61 62 6c 65 2a 29  FkDelete(Table*)
758a0 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  ;.#else.  #defin
758b0 65 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74  e sqlite3FkDelet
758c0 65 28 61 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  e(a).#endif.../*
758d0 0a 2a 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61  .** Available fa
758e0 75 6c 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20  ult injectors.  
758f0 53 68 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72  Should be number
75900 65 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  ed beginning wit
75910 68 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  h 0..*/.#define 
75920 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45  SQLITE_FAULTINJE
75930 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20  CTOR_MALLOC     
75940 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
75950 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43  _FAULTINJECTOR_C
75960 4f 55 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a  OUNT      1../*.
75970 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  ** The interface
75980 20 74 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20   to the code in 
75990 66 61 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72  fault.c used for
759a0 20 69 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65   identifying "be
759b0 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20  nign".** malloc 
759c0 66 61 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69  failures. This i
759d0 73 20 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69  s only present i
759e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
759f0 49 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73  ILTIN_TEST.** is
75a00 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f   not defined..*/
75a10 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
75a20 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
75a30 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
75a40 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
75a50 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
75a60 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50  (void);.SQLITE_P
75a70 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
75a80 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
75a90 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73  lloc(void);.#els
75aa0 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  e.  #define sqli
75ab0 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
75ac0 6c 6c 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65  lloc().  #define
75ad0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
75ae0 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66  nMalloc().#endif
75af0 0a 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44  ..#define IN_IND
75b00 45 58 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20  EX_ROWID        
75b10 20 20 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f     1.#define IN_
75b20 49 4e 44 45 58 5f 45 50 48 20 20 20 20 20 20 20  INDEX_EPH       
75b30 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20        2.#define 
75b40 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20  IN_INDEX_INDEX  
75b50 20 20 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54           3.SQLIT
75b60 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
75b70 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78  lite3FindInIndex
75b80 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a  (Parse *, Expr *
75b90 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66  , int*);..#ifdef
75ba0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
75bb0 54 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49  TOMIC_WRITE.SQLI
75bc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
75bd0 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f   sqlite3JournalO
75be0 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20  pen(sqlite3_vfs 
75bf0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
75c00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c   sqlite3_file *,
75c10 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
75c20 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
75c30 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
75c40 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ize(sqlite3_vfs 
75c50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
75c60 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
75c70 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71  JournalCreate(sq
75c80 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23  lite3_file *);.#
75c90 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73  else.  #define s
75ca0 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a  qlite3JournalSiz
75cb0 65 28 70 56 66 73 29 20 28 28 70 56 66 73 29 2d  e(pVfs) ((pVfs)-
75cc0 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69  >szOsFile).#endi
75cd0 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
75ce0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
75cf0 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c  mJournalOpen(sql
75d00 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51  ite3_file *);.SQ
75d10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
75d20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e   sqlite3MemJourn
75d30 61 6c 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51  alSize(void);.SQ
75d40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
75d50 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75   sqlite3IsMemJou
75d60 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c  rnal(sqlite3_fil
75d70 65 20 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54  e *);..#if SQLIT
75d80 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
75d90 3e 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  >0.SQLITE_PRIVAT
75da0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
75db0 45 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61  ExprSetHeight(Pa
75dc0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
75dd0 72 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52  r *p);.SQLITE_PR
75de0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
75df0 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69  te3SelectExprHei
75e00 67 68 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53  ght(Select *);.S
75e10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
75e20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
75e30 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65  heckHeight(Parse
75e40 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20  *, int);.#else. 
75e50 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
75e60 45 78 70 72 53 65 74 48 65 69 67 68 74 28 78 2c  ExprSetHeight(x,
75e70 79 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  y).  #define sql
75e80 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65  ite3SelectExprHe
75e90 69 67 68 74 28 78 29 20 30 0a 20 20 23 64 65 66  ight(x) 0.  #def
75ea0 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 43  ine sqlite3ExprC
75eb0 68 65 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a  heckHeight(x,y).
75ec0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
75ed0 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
75ee0 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74  e3Get4byte(const
75ef0 20 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52   u8*);.SQLITE_PR
75f00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
75f10 65 33 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20  e3Put4byte(u8*, 
75f20 75 33 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  u32);..#ifdef SQ
75f30 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f  LITE_ENABLE_UNLO
75f40 43 4b 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 54 45  CK_NOTIFY.SQLITE
75f50 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
75f60 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
75f70 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33  nBlocked(sqlite3
75f80 20 2a 2c 20 73 71 6c 69 74 65 33 20 2a 29 3b 0a   *, sqlite3 *);.
75f90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
75fa0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e   void sqlite3Con
75fb0 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
75fc0 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51  sqlite3 *db);.SQ
75fd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
75fe0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  oid sqlite3Conne
75ff0 63 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69  ctionClosed(sqli
76000 74 65 33 20 2a 64 62 29 3b 0a 23 65 6c 73 65 0a  te3 *db);.#else.
76010 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
76020 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b  3ConnectionBlock
76030 65 64 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e  ed(x,y).  #defin
76040 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  e sqlite3Connect
76050 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 0a 20  ionUnlocked(x). 
76060 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
76070 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64  ConnectionClosed
76080 28 78 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  (x).#endif..#ifd
76090 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
760a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
760b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
760c0 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20  serTrace(FILE*, 
760d0 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a  char *);.#endif.
760e0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51  ./*.** If the SQ
760f0 4c 49 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52  LITE_ENABLE IOTR
76100 41 43 45 20 65 78 69 73 74 73 20 74 68 65 6e 20  ACE exists then 
76110 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  the global varia
76120 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f  ble.** sqlite3Io
76130 54 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74  Trace is a point
76140 65 72 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c  er to a printf-l
76150 69 6b 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64  ike routine used
76160 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f   to.** print I/O
76170 20 74 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65   tracing message
76180 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  s. .*/.#ifdef SQ
76190 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52  LITE_ENABLE_IOTR
761a0 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54  ACE.# define IOT
761b0 52 41 43 45 28 41 29 20 20 69 66 28 20 73 71 6c  RACE(A)  if( sql
761c0 69 74 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73  ite3IoTrace ){ s
761d0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b  qlite3IoTrace A;
761e0 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54   }.SQLITE_PRIVAT
761f0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
76200 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56  VdbeIOTraceSql(V
76210 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
76220 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c  IVATE void (*sql
76230 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e  ite3IoTrace)(con
76240 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23  st char*,...);.#
76250 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f  else.# define IO
76260 54 52 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e  TRACE(A).# defin
76270 65 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54  e sqlite3VdbeIOT
76280 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69  raceSql(X).#endi
76290 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  f..#endif../****
762a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
762b0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
762c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
762d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
762e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
762f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
76300 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a   file global.c *
76310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
76340 20 32 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a   2008 June 13.**
76350 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
76360 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
76370 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
76380 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
76390 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
763a0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
763b0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
763c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
763d0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
763e0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
763f0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
76400 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
76410 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
76420 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
76430 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
76440 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
76450 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
76460 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
76470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
764a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
764b0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
764c0 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e  tains definition
764d0 73 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69  s of global vari
764e0 61 62 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e  ables and contan
764f0 74 73 2e 0a 2a 2f 0a 0a 2f 2a 20 41 6e 20 61 72  ts..*/../* An ar
76500 72 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 75  ray to map all u
76510 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 63  pper-case charac
76520 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72 20  ters into their 
76530 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
76540 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61 72   lower-case char
76550 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 51  acter. .**.** SQ
76560 4c 69 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 64  Lite only consid
76570 65 72 73 20 55 53 2d 41 53 43 49 49 20 28 6f 72  ers US-ASCII (or
76580 20 45 42 43 44 49 43 29 20 63 68 61 72 61 63 74   EBCDIC) charact
76590 65 72 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a  ers.  We do not.
765a0 2a 2a 20 68 61 6e 64 6c 65 20 63 61 73 65 20 63  ** handle case c
765b0 6f 6e 76 65 72 73 69 6f 6e 73 20 66 6f 72 20 74  onversions for t
765c0 68 65 20 55 54 46 20 63 68 61 72 61 63 74 65 72  he UTF character
765d0 20 73 65 74 20 73 69 6e 63 65 20 74 68 65 20 74   set since the t
765e0 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76 65  ables.** involve
765f0 64 20 61 72 65 20 6e 65 61 72 6c 79 20 61 73 20  d are nearly as 
76600 62 69 67 20 6f 72 20 62 69 67 67 65 72 20 74 68  big or bigger th
76610 61 6e 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66  an SQLite itself
76620 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
76630 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ATE const unsign
76640 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55  ed char sqlite3U
76650 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20  pperToLower[] = 
76660 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
76670 41 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20 20  ASCII.      0,  
76680 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20  1,  2,  3,  4,  
76690 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20  5,  6,  7,  8,  
766a0 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31  9, 10, 11, 12, 1
766b0 33 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 31  3, 14, 15, 16, 1
766c0 37 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 20  7,.     18, 19, 
766d0 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20  20, 21, 22, 23, 
766e0 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20  24, 25, 26, 27, 
766f0 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20  28, 29, 30, 31, 
76700 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 0a  32, 33, 34, 35,.
76710 20 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38 2c       36, 37, 38,
76720 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c   39, 40, 41, 42,
76730 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c   43, 44, 45, 46,
76740 20 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 2c   47, 48, 49, 50,
76750 20 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 20   51, 52, 53,.   
76760 20 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37    54, 55, 56, 57
76770 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31  , 58, 59, 60, 61
76780 2c 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 37  , 62, 63, 64, 97
76790 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31  , 98, 99,100,101
767a0 2c 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 30  ,102,103,.    10
767b0 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 30  4,105,106,107,10
767c0 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31  8,109,110,111,11
767d0 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31  2,113,114,115,11
767e0 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
767f0 30 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20  0,121,.    122, 
76800 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20  91, 92, 93, 94, 
76810 39 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 20  95, 96, 97, 98, 
76820 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31  99,100,101,102,1
76830 30 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 31  03,104,105,106,1
76840 30 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 2c  07,.    108,109,
76850 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c  110,111,112,113,
76860 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c  114,115,116,117,
76870 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c  118,119,120,121,
76880 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c  122,123,124,125,
76890 0a 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32 38  .    126,127,128
768a0 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32  ,129,130,131,132
768b0 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36  ,133,134,135,136
768c0 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30  ,137,138,139,140
768d0 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 20  ,141,142,143,.  
768e0 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34    144,145,146,14
768f0 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35  7,148,149,150,15
76900 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35  1,152,153,154,15
76910 35 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 35  5,156,157,158,15
76920 39 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 31  9,160,161,.    1
76930 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31  62,163,164,165,1
76940 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31  66,167,168,169,1
76950 37 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 31  70,171,172,173,1
76960 37 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 31  74,175,176,177,1
76970 37 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 2c  78,179,.    180,
76980 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c  181,182,183,184,
76990 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c  185,186,187,188,
769a0 31 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 2c  189,190,191,192,
769b0 31 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 2c  193,194,195,196,
769c0 31 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 39  197,.    198,199
769d0 2c 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 33  ,200,201,202,203
769e0 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37  ,204,205,206,207
769f0 2c 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 31  ,208,209,210,211
76a00 2c 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 35  ,212,213,214,215
76a10 2c 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32 31  ,.    216,217,21
76a20 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32  8,219,220,221,22
76a30 32 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 32  2,223,224,225,22
76a40 36 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 33  6,227,228,229,23
76a50 30 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 20  0,231,232,233,. 
76a60 20 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 32     234,235,236,2
76a70 33 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 32  37,238,239,240,2
76a80 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32  41,242,243,244,2
76a90 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32  45,246,247,248,2
76aa0 34 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 20  49,250,251,.    
76ab0 32 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 0a  252,253,254,255.
76ac0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
76ad0 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20 20  LITE_EBCDIC.    
76ae0 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c    0,  1,  2,  3,
76af0 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c    4,  5,  6,  7,
76b00 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c    8,  9, 10, 11,
76b10 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c   12, 13, 14, 15,
76b20 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 31   /* 0x */.     1
76b30 36 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 32  6, 17, 18, 19, 2
76b40 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32  0, 21, 22, 23, 2
76b50 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32  4, 25, 26, 27, 2
76b60 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 2f  8, 29, 30, 31, /
76b70 2a 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 2c  * 1x */.     32,
76b80 20 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 2c   33, 34, 35, 36,
76b90 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c   37, 38, 39, 40,
76ba0 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c   41, 42, 43, 44,
76bb0 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a 20   45, 46, 47, /* 
76bc0 32 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 34  2x */.     48, 4
76bd0 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35  9, 50, 51, 52, 5
76be0 33 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35  3, 54, 55, 56, 5
76bf0 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36  7, 58, 59, 60, 6
76c00 31 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 78  1, 62, 63, /* 3x
76c10 20 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 2c   */.     64, 65,
76c20 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c   66, 67, 68, 69,
76c30 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c   70, 71, 72, 73,
76c40 20 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 2c   74, 75, 76, 77,
76c50 20 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 2a   78, 79, /* 4x *
76c60 2f 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20 38  /.     80, 81, 8
76c70 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38  2, 83, 84, 85, 8
76c80 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 39  6, 87, 88, 89, 9
76c90 30 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39  0, 91, 92, 93, 9
76ca0 34 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a  4, 95, /* 5x */.
76cb0 20 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 2c       96, 97, 66,
76cc0 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c   67, 68, 69, 70,
76cd0 20 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 2c   71, 72, 73,106,
76ce0 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c  107,108,109,110,
76cf0 31 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 20  111, /* 6x */.  
76d00 20 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 38    112, 81, 82, 8
76d10 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38  3, 84, 85, 86, 8
76d20 37 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 32  7, 88, 89,122,12
76d30 33 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 32  3,124,125,126,12
76d40 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20  7, /* 7x */.    
76d50 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c  128,129,130,131,
76d60 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c  132,133,134,135,
76d70 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c  136,137,138,139,
76d80 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c  140,141,142,143,
76d90 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 34   /* 8x */.    14
76da0 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34  4,145,146,147,14
76db0 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35  8,149,150,151,15
76dc0 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35  2,153,154,155,15
76dd0 36 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 2f  6,157,156,159, /
76de0 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 2c  * 9x */.    160,
76df0 31 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c  161,162,163,164,
76e00 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c  165,166,167,168,
76e10 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 2c  169,170,171,140,
76e20 31 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a 20  141,142,175, /* 
76e30 41 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 37  Ax */.    176,17
76e40 37 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 38  7,178,179,180,18
76e50 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38  1,182,183,184,18
76e60 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38  5,186,187,188,18
76e70 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 78  9,190,191, /* Bx
76e80 20 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 2c   */.    192,129,
76e90 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c  130,131,132,133,
76ea0 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c  134,135,136,137,
76eb0 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c  202,203,204,205,
76ec0 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 2a  206,207, /* Cx *
76ed0 2f 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31 34  /.    208,145,14
76ee0 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35  6,147,148,149,15
76ef0 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 31  0,151,152,153,21
76f00 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32  8,219,220,221,22
76f10 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a  2,223, /* Dx */.
76f20 20 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32 2c      224,225,162,
76f30 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c  163,164,165,166,
76f40 31 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 2c  167,168,169,232,
76f50 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c  203,204,205,206,
76f60 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 20  207, /* Ex */.  
76f70 20 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34    239,240,241,24
76f80 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34  2,243,244,245,24
76f90 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 31  6,247,248,249,21
76fa0 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 35  9,220,221,222,25
76fb0 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 64  5, /* Fx */.#end
76fc0 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  if.};../*.** The
76fd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 32 35 36 20 62   following 256 b
76fe0 79 74 65 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65  yte lookup table
76ff0 20 69 73 20 75 73 65 64 20 74 6f 20 73 75 70 70   is used to supp
77000 6f 72 74 20 53 51 4c 69 74 65 73 20 62 75 69 6c  ort SQLites buil
77010 74 2d 69 6e 0a 2a 2a 20 65 71 75 69 76 61 6c 65  t-in.** equivale
77020 6e 74 73 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  nts to the follo
77030 77 69 6e 67 20 73 74 61 6e 64 61 72 64 20 6c 69  wing standard li
77040 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 3a  brary functions:
77050 0a 2a 2a 0a 2a 2a 20 20 20 69 73 73 70 61 63 65  .**.**   isspace
77060 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ()              
77070 20 20 20 20 20 20 20 20 20 20 30 78 30 31 0a 2a            0x01.*
77080 2a 20 20 20 69 73 61 6c 70 68 61 28 29 20 20 20  *   isalpha()   
77090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
770a0 20 20 20 20 20 30 78 30 32 0a 2a 2a 20 20 20 69       0x02.**   i
770b0 73 64 69 67 69 74 28 29 20 20 20 20 20 20 20 20  sdigit()        
770c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
770d0 30 78 30 34 0a 2a 2a 20 20 20 69 73 61 6c 6e 75  0x04.**   isalnu
770e0 6d 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20  m()             
770f0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 36 0a             0x06.
77100 2a 2a 20 20 20 69 73 78 64 69 67 69 74 28 29 20  **   isxdigit() 
77110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77120 20 20 20 20 20 20 30 78 30 38 0a 2a 2a 20 20 20        0x08.**   
77130 74 6f 75 70 70 65 72 28 29 20 20 20 20 20 20 20  toupper()       
77140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77150 20 30 78 32 30 0a 2a 2a 20 20 20 53 51 4c 69 74   0x20.**   SQLit
77160 65 20 69 64 65 6e 74 69 66 69 65 72 20 63 68 61  e identifier cha
77170 72 61 63 74 65 72 20 20 20 20 20 20 30 78 34 30  racter      0x40
77180 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 78 32 30 20  .**.** Bit 0x20 
77190 69 73 20 73 65 74 20 69 66 20 74 68 65 20 6d 61  is set if the ma
771a0 70 70 65 64 20 63 68 61 72 61 63 74 65 72 20 72  pped character r
771b0 65 71 75 69 72 65 73 20 74 72 61 6e 73 6c 61 74  equires translat
771c0 69 6f 6e 20 74 6f 20 75 70 70 65 72 0a 2a 2a 20  ion to upper.** 
771d0 63 61 73 65 2e 20 69 2e 65 2e 20 69 66 20 74 68  case. i.e. if th
771e0 65 20 63 68 61 72 61 63 74 65 72 20 69 73 20 61  e character is a
771f0 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49   lower-case ASCI
77200 49 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20  I character..** 
77210 49 66 20 78 20 69 73 20 61 20 6c 6f 77 65 72 2d  If x is a lower-
77220 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72 61  case ASCII chara
77230 63 74 65 72 2c 20 74 68 65 6e 20 69 74 73 20 75  cter, then its u
77240 70 70 65 72 2d 63 61 73 65 20 65 71 75 69 76 61  pper-case equiva
77250 6c 65 6e 74 0a 2a 2a 20 69 73 20 28 78 20 2d 20  lent.** is (x - 
77260 30 78 32 30 29 2e 20 54 68 65 72 65 66 6f 72 65  0x20). Therefore
77270 20 74 6f 75 70 70 65 72 28 29 20 63 61 6e 20 62   toupper() can b
77280 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73  e implemented as
77290 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 78 20 26 20 7e  :.**.**   (x & ~
772a0 28 6d 61 70 5b 78 5d 26 30 78 32 30 29 29 0a 2a  (map[x]&0x20)).*
772b0 2a 0a 2a 2a 20 53 74 61 6e 64 61 72 64 20 66 75  *.** Standard fu
772c0 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 29  nction tolower()
772d0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
772e0 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  using the sqlite
772f0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a  3UpperToLower[].
77300 2a 2a 20 61 72 72 61 79 2e 20 74 6f 6c 6f 77 65  ** array. tolowe
77310 72 28 29 20 69 73 20 75 73 65 64 20 6d 6f 72 65  r() is used more
77320 20 6f 66 74 65 6e 20 74 68 61 6e 20 74 6f 75 70   often than toup
77330 70 65 72 28 29 20 62 79 20 53 51 4c 69 74 65 2e  per() by SQLite.
77340 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 78 34 30 20  .**.** Bit 0x40 
77350 69 73 20 73 65 74 20 69 66 20 74 68 65 20 63 68  is set if the ch
77360 61 72 61 63 74 65 72 20 6e 6f 6e 2d 61 6c 70 68  aracter non-alph
77370 61 6e 75 6d 65 72 69 63 20 61 6e 64 20 63 61 6e  anumeric and can
77380 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20 0a   be used in an .
77390 2a 2a 20 53 51 4c 69 74 65 20 69 64 65 6e 74 69  ** SQLite identi
773a0 66 69 65 72 2e 20 20 49 64 65 6e 74 69 66 69 65  fier.  Identifie
773b0 72 73 20 61 72 65 20 61 6c 70 68 61 6e 75 6d 65  rs are alphanume
773c0 72 69 63 73 2c 20 22 5f 22 2c 20 22 24 22 2c 20  rics, "_", "$", 
773d0 61 6e 64 20 61 6e 79 0a 2a 2a 20 6e 6f 6e 2d 41  and any.** non-A
773e0 53 43 49 49 20 55 54 46 20 63 68 61 72 61 63 74  SCII UTF charact
773f0 65 72 2e 20 48 65 6e 63 65 20 74 68 65 20 74 65  er. Hence the te
77400 73 74 20 66 6f 72 20 77 68 65 74 68 65 72 20 6f  st for whether o
77410 72 20 6e 6f 74 20 61 20 63 68 61 72 61 63 74 65  r not a characte
77420 72 20 69 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  r is.** part of 
77430 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 73  an identifier is
77440 20 30 78 34 36 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   0x46..**.** SQL
77450 69 74 65 27 73 20 76 65 72 73 69 6f 6e 73 20 61  ite's versions a
77460 72 65 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20  re identical to 
77470 74 68 65 20 73 74 61 6e 64 61 72 64 20 76 65 72  the standard ver
77480 73 69 6f 6e 73 20 61 73 73 75 6d 69 6e 67 20 61  sions assuming a
77490 0a 2a 2a 20 6c 6f 63 61 6c 65 20 6f 66 20 22 43  .** locale of "C
774a0 22 2e 20 54 68 65 79 20 61 72 65 20 69 6d 70 6c  ". They are impl
774b0 65 6d 65 6e 74 65 64 20 61 73 20 6d 61 63 72 6f  emented as macro
774c0 73 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  s in sqliteInt.h
774d0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
774e0 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f  TE_ASCII.SQLITE_
774f0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e  PRIVATE const un
77500 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69  signed char sqli
77510 74 65 33 43 74 79 70 65 4d 61 70 5b 32 35 36 5d  te3CtypeMap[256]
77520 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30   = {.  0x00, 0x0
77530 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77540 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77550 20 30 78 30 30 2c 20 20 2f 2a 20 30 30 2e 2e 30   0x00,  /* 00..0
77560 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
77570 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  .  0x00, 0x01, 0
77580 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c  x01, 0x01, 0x01,
77590 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30   0x01, 0x00, 0x0
775a0 30 2c 20 20 2f 2a 20 30 38 2e 2e 30 66 20 20 20  0,  /* 08..0f   
775b0 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
775c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
775d0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
775e0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
775f0 2f 2a 20 31 30 2e 2e 31 37 20 20 20 20 2e 2e 2e  /* 10..17    ...
77600 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
77610 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77620 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77630 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31  x00, 0x00,  /* 1
77640 38 2e 2e 31 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..1f    .......
77650 2e 20 2a 2f 0a 20 20 30 78 30 31 2c 20 30 78 30  . */.  0x01, 0x0
77660 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77670 78 34 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x40, 0x00, 0x00,
77680 20 30 78 30 30 2c 20 20 2f 2a 20 32 30 2e 2e 32   0x00,  /* 20..2
77690 37 20 20 20 20 20 21 22 23 24 25 26 27 20 2a 2f  7     !"#$%&' */
776a0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
776b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
776c0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
776d0 30 2c 20 20 2f 2a 20 32 38 2e 2e 32 66 20 20 20  0,  /* 28..2f   
776e0 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f 0a 20 20 30   ()*+,-./ */.  0
776f0 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c  x0c, 0x0c, 0x0c,
77700 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30   0x0c, 0x0c, 0x0
77710 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 20  c, 0x0c, 0x0c,  
77720 2f 2a 20 33 30 2e 2e 33 37 20 20 20 20 30 31 32  /* 30..37    012
77730 33 34 35 36 37 20 2a 2f 0a 20 20 30 78 30 63 2c  34567 */.  0x0c,
77740 20 30 78 30 63 2c 20 30 78 30 30 2c 20 30 78 30   0x0c, 0x00, 0x0
77750 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77760 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 33  x00, 0x00,  /* 3
77770 38 2e 2e 33 66 20 20 20 20 38 39 3a 3b 3c 3d 3e  8..3f    89:;<=>
77780 3f 20 2a 2f 0a 0a 20 20 30 78 30 30 2c 20 30 78  ? */..  0x00, 0x
77790 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20  0a, 0x0a, 0x0a, 
777a0 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61  0x0a, 0x0a, 0x0a
777b0 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 30 2e 2e  , 0x02,  /* 40..
777c0 34 37 20 20 20 20 40 41 42 43 44 45 46 47 20 2a  47    @ABCDEFG *
777d0 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20  /.  0x02, 0x02, 
777e0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
777f0 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
77800 30 32 2c 20 20 2f 2a 20 34 38 2e 2e 34 66 20 20  02,  /* 48..4f  
77810 20 20 48 49 4a 4b 4c 4d 4e 4f 20 2a 2f 0a 20 20    HIJKLMNO */.  
77820 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
77830 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
77840 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
77850 20 2f 2a 20 35 30 2e 2e 35 37 20 20 20 20 50 51   /* 50..57    PQ
77860 52 53 54 55 56 57 20 2a 2f 0a 20 20 30 78 30 32  RSTUVW */.  0x02
77870 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
77880 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77890 30 78 30 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20  0x00, 0x40,  /* 
778a0 35 38 2e 2e 35 66 20 20 20 20 58 59 5a 5b 5c 5d  58..5f    XYZ[\]
778b0 5e 5f 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  ^_ */.  0x00, 0x
778c0 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20  2a, 0x2a, 0x2a, 
778d0 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61  0x2a, 0x2a, 0x2a
778e0 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 30 2e 2e  , 0x22,  /* 60..
778f0 36 37 20 20 20 20 60 61 62 63 64 65 66 67 20 2a  67    `abcdefg *
77900 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20  /.  0x22, 0x22, 
77910 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77920 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
77930 32 32 2c 20 20 2f 2a 20 36 38 2e 2e 36 66 20 20  22,  /* 68..6f  
77940 20 20 68 69 6a 6b 6c 6d 6e 6f 20 2a 2f 0a 20 20    hijklmno */.  
77950 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77960 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
77970 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
77980 20 2f 2a 20 37 30 2e 2e 37 37 20 20 20 20 70 71   /* 70..77    pq
77990 72 73 74 75 76 77 20 2a 2f 0a 20 20 30 78 32 32  rstuvw */.  0x22
779a0 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
779b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
779c0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
779d0 37 38 2e 2e 37 66 20 20 20 20 78 79 7a 7b 7c 7d  78..7f    xyz{|}
779e0 7e 2e 20 2a 2f 0a 0a 20 20 30 78 34 30 2c 20 30  ~. */..  0x40, 0
779f0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77a00 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77a10 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 38 30 2e  0, 0x40,  /* 80.
77a20 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .87    ........ 
77a30 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c  */.  0x40, 0x40,
77a40 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77a50 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77a60 78 34 30 2c 20 20 2f 2a 20 38 38 2e 2e 38 66 20  x40,  /* 88..8f 
77a70 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
77a80 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77a90 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77aa0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77ab0 20 20 2f 2a 20 39 30 2e 2e 39 37 20 20 20 20 2e    /* 90..97    .
77ac0 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34  ....... */.  0x4
77ad0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77ae0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77af0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a   0x40, 0x40,  /*
77b00 20 39 38 2e 2e 39 66 20 20 20 20 2e 2e 2e 2e 2e   98..9f    .....
77b10 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30  ... */.  0x40, 0
77b20 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77b30 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77b40 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 61 30 2e  0, 0x40,  /* a0.
77b50 2e 61 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .a7    ........ 
77b60 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c  */.  0x40, 0x40,
77b70 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77b80 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77b90 78 34 30 2c 20 20 2f 2a 20 61 38 2e 2e 61 66 20  x40,  /* a8..af 
77ba0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
77bb0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77bc0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77bd0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77be0 20 20 2f 2a 20 62 30 2e 2e 62 37 20 20 20 20 2e    /* b0..b7    .
77bf0 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34  ....... */.  0x4
77c00 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77c10 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77c20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a   0x40, 0x40,  /*
77c30 20 62 38 2e 2e 62 66 20 20 20 20 2e 2e 2e 2e 2e   b8..bf    .....
77c40 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 78 34 30 2c 20  ... */..  0x40, 
77c50 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77c60 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
77c70 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 63 30  40, 0x40,  /* c0
77c80 2e 2e 63 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..c7    ........
77c90 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30   */.  0x40, 0x40
77ca0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
77cb0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77cc0 30 78 34 30 2c 20 20 2f 2a 20 63 38 2e 2e 63 66  0x40,  /* c8..cf
77cd0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
77ce0 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78    0x40, 0x40, 0x
77cf0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77d00 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77d10 2c 20 20 2f 2a 20 64 30 2e 2e 64 37 20 20 20 20  ,  /* d0..d7    
77d20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
77d30 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77d40 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77d50 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f  , 0x40, 0x40,  /
77d60 2a 20 64 38 2e 2e 64 66 20 20 20 20 2e 2e 2e 2e  * d8..df    ....
77d70 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20  .... */.  0x40, 
77d80 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77d90 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
77da0 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 65 30  40, 0x40,  /* e0
77db0 2e 2e 65 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..e7    ........
77dc0 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30   */.  0x40, 0x40
77dd0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
77de0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77df0 30 78 34 30 2c 20 20 2f 2a 20 65 38 2e 2e 65 66  0x40,  /* e8..ef
77e00 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
77e10 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78    0x40, 0x40, 0x
77e20 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77e30 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77e40 2c 20 20 2f 2a 20 66 30 2e 2e 66 37 20 20 20 20  ,  /* f0..f7    
77e50 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
77e60 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77e70 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77e80 2c 20 30 78 34 30 2c 20 30 78 34 30 20 20 20 2f  , 0x40, 0x40   /
77e90 2a 20 66 38 2e 2e 66 66 20 20 20 20 2e 2e 2e 2e  * f8..ff    ....
77ea0 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69  .... */.};.#endi
77eb0 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  f..../*.** The f
77ec0 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67 6c 65 74  ollowing singlet
77ed0 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  on contains the 
77ee0 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61  global configura
77ef0 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 65 20  tion for.** the 
77f00 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a  SQLite library..
77f10 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
77f20 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  E SQLITE_WSD str
77f30 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69  uct Sqlite3Confi
77f40 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 20  g sqlite3Config 
77f50 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45 5f 44 45  = {.   SQLITE_DE
77f60 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 2c  FAULT_MEMSTATUS,
77f70 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74 20 2a 2f    /* bMemstat */
77f80 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  .   1,          
77f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
77fa0 2a 20 62 43 6f 72 65 4d 75 74 65 78 20 2a 2f 0a  * bCoreMutex */.
77fb0 20 20 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44     SQLITE_THREAD
77fc0 53 41 46 45 3d 3d 31 2c 20 20 20 20 20 20 2f 2a  SAFE==1,      /*
77fd0 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a 2f 0a 20   bFullMutex */. 
77fe0 20 20 30 78 37 66 66 66 66 66 66 65 2c 20 20 20    0x7ffffffe,   
77ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
78000 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 20 31  mxStrlen */.   1
78010 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  00,             
78020 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 4c            /* szL
78030 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 35  ookaside */.   5
78040 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  00,             
78050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 4c 6f            /* nLo
78060 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 7b 30  okaside */.   {0
78070 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c  ,0,0,0,0,0,0,0},
78080 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 20 2a 2f           /* m */
78090 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30  .   {0,0,0,0,0,0
780a0 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 2f  ,0,0,0},       /
780b0 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 7b 30  * mutex */.   {0
780c0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  ,0,0,0,0,0,0,0,0
780d0 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20 70 63 61 63  ,0,0},   /* pcac
780e0 68 65 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29  he */.   (void*)
780f0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
78100 20 20 20 20 2f 2a 20 70 48 65 61 70 20 2a 2f 0a      /* pHeap */.
78110 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
78120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
78130 20 6e 48 65 61 70 20 2a 2f 0a 20 20 20 30 2c 20   nHeap */.   0, 
78140 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
78150 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e 48 65 61          /* mnHea
78160 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0a 20 20 20  p, mxHeap */.   
78170 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20  (void*)0,       
78180 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 53             /* pS
78190 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20  cratch */.   0, 
781a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
781b0 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 53 63 72          /* szScr
781c0 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20 20 20  atch */.   0,   
781d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
781e0 20 20 20 20 20 20 2f 2a 20 6e 53 63 72 61 74 63        /* nScratc
781f0 68 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30  h */.   (void*)0
78200 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
78210 20 20 20 2f 2a 20 70 50 61 67 65 20 2a 2f 0a 20     /* pPage */. 
78220 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
78230 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
78240 73 7a 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20  szPage */.   0, 
78250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78260 20 20 20 20 20 20 20 20 2f 2a 20 6e 50 61 67 65          /* nPage
78270 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
78280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78290 20 20 2f 2a 20 6d 78 50 61 72 73 65 72 53 74 61    /* mxParserSta
782a0 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ck */.   0,     
782b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
782c0 20 20 20 20 2f 2a 20 73 68 61 72 65 64 43 61 63      /* sharedCac
782d0 68 65 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 20  heEnabled */.   
782e0 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 74 20  /* All the rest 
782f0 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
78300 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
78310 7a 65 72 6f 20 2a 2f 0a 20 20 20 30 2c 20 20 20  zero */.   0,   
78320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78330 20 20 20 20 20 20 2f 2a 20 69 73 49 6e 69 74 20        /* isInit 
78340 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20  */.   0,        
78350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78360 20 2f 2a 20 69 6e 50 72 6f 67 72 65 73 73 20 2a   /* inProgress *
78370 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
78380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78390 2f 2a 20 69 73 4d 75 74 65 78 49 6e 69 74 20 2a  /* isMutexInit *
783a0 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
783b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
783c0 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20  /* isMallocInit 
783d0 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20  */.   0,        
783e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
783f0 20 2f 2a 20 69 73 50 43 61 63 68 65 49 6e 69 74   /* isPCacheInit
78400 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
78410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78420 20 20 2f 2a 20 70 49 6e 69 74 4d 75 74 65 78 20    /* pInitMutex 
78430 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20  */.   0,        
78440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78450 20 2f 2a 20 6e 52 65 66 49 6e 69 74 4d 75 74 65   /* nRefInitMute
78460 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  x */.};.../*.** 
78470 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 67  Hash table for g
78480 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 73 20  lobal functions 
78490 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6d 6d  - functions comm
784a0 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 64 61 74  on to all.** dat
784b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
784c0 73 2e 20 20 41 66 74 65 72 20 69 6e 69 74 69 61  s.  After initia
784d0 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 69 73 20 74  lization, this t
784e0 61 62 6c 65 20 69 73 0a 2a 2a 20 72 65 61 64 2d  able is.** read-
784f0 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  only..*/.SQLITE_
78500 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57  PRIVATE SQLITE_W
78510 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 73  SD FuncDefHash s
78520 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
78530 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  tions;../*.** Th
78540 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22  e value of the "
78550 70 65 6e 64 69 6e 67 22 20 62 79 74 65 20 6d 75  pending" byte mu
78560 73 74 20 62 65 20 30 78 34 30 30 30 30 30 30 30  st be 0x40000000
78570 20 28 31 20 62 79 74 65 20 70 61 73 74 20 74 68   (1 byte past th
78580 65 0a 2a 2a 20 31 2d 67 69 62 61 62 79 74 65 20  e.** 1-gibabyte 
78590 62 6f 75 6e 64 61 72 79 29 20 69 6e 20 61 20 63  boundary) in a c
785a0 6f 6d 70 61 74 69 62 6c 65 20 64 61 74 61 62 61  ompatible databa
785b0 73 65 2e 20 20 53 51 4c 69 74 65 20 6e 65 76 65  se.  SQLite neve
785c0 72 20 75 73 65 73 0a 2a 2a 20 74 68 65 20 64 61  r uses.** the da
785d0 74 61 62 61 73 65 20 70 61 67 65 20 74 68 61 74  tabase page that
785e0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 65   contains the pe
785f0 6e 64 69 6e 67 20 62 79 74 65 2e 20 20 49 74 20  nding byte.  It 
78600 6e 65 76 65 72 20 61 74 74 65 6d 70 74 73 0a 2a  never attempts.*
78610 2a 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  * to read or wri
78620 74 65 20 74 68 61 74 20 70 61 67 65 2e 20 20 54  te that page.  T
78630 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20  he pending byte 
78640 70 61 67 65 20 69 73 20 73 65 74 20 61 73 73 69  page is set assi
78650 67 6e 0a 2a 2a 20 66 6f 72 20 75 73 65 20 62 79  gn.** for use by
78660 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 73 20   the VFS layers 
78670 61 73 20 73 70 61 63 65 20 66 6f 72 20 6d 61 6e  as space for man
78680 61 67 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73  aging file locks
78690 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74  ..**.** During t
786a0 65 73 74 69 6e 67 2c 20 69 74 20 69 73 20 6f 66  esting, it is of
786b0 74 65 6e 20 64 65 73 69 72 61 62 6c 65 20 74 6f  ten desirable to
786c0 20 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 69 6e   move the pendin
786d0 67 20 62 79 74 65 20 74 6f 0a 2a 2a 20 61 20 64  g byte to.** a d
786e0 69 66 66 65 72 65 6e 74 20 70 6f 73 69 74 69 6f  ifferent positio
786f0 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20  n in the file.  
78700 54 68 69 73 20 61 6c 6c 6f 77 73 20 63 6f 64 65  This allows code
78710 20 74 68 61 74 20 68 61 73 20 74 6f 0a 2a 2a 20   that has to.** 
78720 64 65 61 6c 20 77 69 74 68 20 74 68 65 20 70 65  deal with the pe
78730 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20 72 75  nding byte to ru
78740 6e 20 6f 6e 20 66 69 6c 65 73 20 74 68 61 74 20  n on files that 
78750 61 72 65 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72  are much smaller
78760 0a 2a 2a 20 74 68 61 6e 20 31 20 47 69 42 2e 20  .** than 1 GiB. 
78770 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73   The sqlite3_tes
78780 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65  t_control() inte
78790 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 65  rface can be use
787a0 64 20 74 6f 0a 2a 2a 20 6d 6f 76 65 20 74 68 65  d to.** move the
787b0 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e 0a 2a   pending byte..*
787c0 2a 0a 2a 2a 20 49 4d 50 4f 52 54 41 4e 54 3a 20  *.** IMPORTANT: 
787d0 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65   Changing the pe
787e0 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20 61 6e  nding byte to an
787f0 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68  y value other th
78800 61 6e 0a 2a 2a 20 30 78 34 30 30 30 30 30 30 30  an.** 0x40000000
78810 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 69   results in an i
78820 6e 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 74 61  ncompatible data
78830 62 61 73 65 20 66 69 6c 65 20 66 6f 72 6d 61 74  base file format
78840 21 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  !.** Changing th
78850 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 64  e pending byte d
78860 75 72 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  uring operating 
78870 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66  results in undef
78880 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 64 69 6c 65  ined.** and dile
78890 74 65 72 69 6f 75 73 20 62 65 68 61 76 69 6f 72  terious behavior
788a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
788b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
788c0 65 6e 64 69 6e 67 42 79 74 65 20 3d 20 30 78 34  endingByte = 0x4
788d0 30 30 30 30 30 30 30 3b 0a 0a 2f 2a 0a 2a 2a 20  0000000;../*.** 
788e0 50 72 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70  Properties of op
788f0 63 6f 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c  codes.  The OPFL
78900 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61  G_INITIALIZER ma
78910 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65  cro is.** create
78920 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61  d by mkopcodeh.a
78930 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c  wk during compil
78940 61 74 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20  ation.  Data is 
78950 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d  obtained.** from
78960 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f   the comments fo
78970 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73  llowing the "cas
78980 65 20 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74  e OP_xxxx:" stat
78990 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 65  ements in.** the
789a0 20 76 64 62 65 2e 63 20 66 69 6c 65 2e 20 20 0a   vdbe.c file.  .
789b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
789c0 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  E const unsigned
789d0 20 63 68 61 72 20 73 71 6c 69 74 65 33 4f 70 63   char sqlite3Opc
789e0 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20  odeProperty[] = 
789f0 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45  OPFLG_INITIALIZE
78a00 52 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  R;../***********
78a10 2a 2a 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61  *** End of globa
78a20 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
78a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78a50 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
78a60 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
78a70 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  tatus.c ********
78a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78aa0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a  **/./*.** 2008 J
78ab0 75 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65  une 18.**.** The
78ac0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
78ad0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
78ae0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
78af0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
78b00 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
78b10 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
78b20 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
78b30 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
78b40 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
78b50 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
78b60 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
78b70 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
78b80 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
78b90 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
78ba0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
78bb0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
78bc0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
78bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78c10 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
78c20 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
78c30 74 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  ts the sqlite3_s
78c40 74 61 74 75 73 28 29 20 69 6e 74 65 72 66 61 63  tatus() interfac
78c50 65 20 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a  e and related.**
78c60 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a   functionality..
78c70 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62  */../*.** Variab
78c80 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20  les in which to 
78c90 72 65 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e  record status in
78ca0 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79  formation..*/.ty
78cb0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
78cc0 69 74 65 33 53 74 61 74 54 79 70 65 20 73 71 6c  ite3StatType sql
78cd0 69 74 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74  ite3StatType;.st
78ce0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
78cf0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74  struct sqlite3St
78d00 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e  atType {.  int n
78d10 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20  owValue[9];     
78d20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76      /* Current v
78d30 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  alue */.  int mx
78d40 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20  Value[9];       
78d50 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61     /* Maximum va
78d60 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33  lue */.} sqlite3
78d70 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b  Stat = { {0,}, {
78d80 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20  0,} };.../* The 
78d90 22 77 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20  "wsdStat" macro 
78da0 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20  will resolve to 
78db0 74 68 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72  the status infor
78dc0 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20  mation.** state 
78dd0 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74  vector.  If writ
78de0 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
78df0 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20   is unsupported 
78e00 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a  on the target,.*
78e10 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63  * we have to loc
78e20 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65  ate the state ve
78e30 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65  ctor at run-time
78e40 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63  .  In the more c
78e50 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68  ommon.** case wh
78e60 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61  ere writable sta
78e70 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70  tic data is supp
78e80 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63  orted, wsdStat c
78e90 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c  an refer directl
78ea0 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c  y.** to the "sql
78eb0 69 74 65 33 53 74 61 74 22 20 73 74 61 74 65 20  ite3Stat" state 
78ec0 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20  vector declared 
78ed0 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  above..*/.#ifdef
78ee0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
78ef0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61  .# define wsdSta
78f00 74 49 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74  tInit  sqlite3St
78f10 61 74 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f  atType *x = &GLO
78f20 42 41 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54  BAL(sqlite3StatT
78f30 79 70 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29  ype,sqlite3Stat)
78f40 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61  .# define wsdSta
78f50 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64  t x[0].#else.# d
78f60 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69  efine wsdStatIni
78f70 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74  t.# define wsdSt
78f80 61 74 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23  at sqlite3Stat.#
78f90 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
78fa0 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  urn the current 
78fb0 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75  value of a statu
78fc0 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a  s parameter..*/.
78fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
78fe0 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  nt sqlite3Status
78ff0 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20  Value(int op){. 
79000 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20   wsdStatInit;.  
79010 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26  assert( op>=0 &&
79020 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73   op<ArraySize(ws
79030 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20  dStat.nowValue) 
79040 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53  );.  return wsdS
79050 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
79060 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e  ;.}../*.** Add N
79070 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
79080 20 61 20 73 74 61 74 75 73 20 72 65 63 6f 72 64   a status record
79090 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  .  It is assumed
790a0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c   that the.** cal
790b0 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70  ler holds approp
790c0 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a  riate locks..*/.
790d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
790e0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
790f0 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74  sAdd(int op, int
79100 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e   N){.  wsdStatIn
79110 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70  it;.  assert( op
79120 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53  >=0 && op<ArrayS
79130 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56  ize(wsdStat.nowV
79140 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74  alue) );.  wsdSt
79150 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20  at.nowValue[op] 
79160 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53  += N;.  if( wsdS
79170 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
79180 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65  >wsdStat.mxValue
79190 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53  [op] ){.    wsdS
791a0 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20  tat.mxValue[op] 
791b0 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  = wsdStat.nowVal
791c0 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ue[op];.  }.}../
791d0 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c  *.** Set the val
791e0 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 74  ue of a status t
791f0 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  o X..*/.SQLITE_P
79200 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
79210 74 65 33 53 74 61 74 75 73 53 65 74 28 69 6e 74  te3StatusSet(int
79220 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77   op, int X){.  w
79230 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73  sdStatInit;.  as
79240 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f  sert( op>=0 && o
79250 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53  p<ArraySize(wsdS
79260 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b  tat.nowValue) );
79270 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  .  wsdStat.nowVa
79280 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69  lue[op] = X;.  i
79290 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  f( wsdStat.nowVa
792a0 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e  lue[op]>wsdStat.
792b0 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20  mxValue[op] ){. 
792c0 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c     wsdStat.mxVal
792d0 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74  ue[op] = wsdStat
792e0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20  .nowValue[op];. 
792f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72   }.}../*.** Quer
79300 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61  y status informa
79310 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion..**.** This
79320 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
79330 61 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61  assumes that rea
79340 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20  ding or writing 
79350 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32  an aligned.** 32
79360 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20  -bit integer is 
79370 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74  an atomic operat
79380 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73  ion.  If that as
79390 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20  sumption is not 
793a0 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  true,.** then th
793b0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f  is routine is no
793c0 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f  t threadsafe..*/
793d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
793e0 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69  sqlite3_status(i
793f0 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72  nt op, int *pCur
79400 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68  rent, int *pHigh
79410 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74  water, int reset
79420 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74  Flag){.  wsdStat
79430 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30  Init;.  if( op<0
79440 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a   || op>=ArraySiz
79450 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  e(wsdStat.nowVal
79460 75 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ue) ){.    retur
79470 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
79480 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74  .  }.  *pCurrent
79490 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61   = wsdStat.nowVa
794a0 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67  lue[op];.  *pHig
794b0 68 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74  hwater = wsdStat
794c0 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20  .mxValue[op];.  
794d0 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b  if( resetFlag ){
794e0 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56  .    wsdStat.mxV
794f0 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74  alue[op] = wsdSt
79500 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b  at.nowValue[op];
79510 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
79520 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
79530 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69  * Query status i
79540 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61  nformation for a
79550 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
79560 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53   connection.*/.S
79570 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
79580 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
79590 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
795a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
795b0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
795c0 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73  ion whose status
795d0 20 69 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20   is desired */. 
795e0 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
795f0 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73         /* Status
79600 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a   verb */.  int *
79610 70 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20  pCurrent,       
79620 20 2f 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e   /* Write curren
79630 74 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a  t value here */.
79640 20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65    int *pHighwate
79650 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  r,      /* Write
79660 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b   high-water mark
79670 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72   here */.  int r
79680 65 73 65 74 46 6c 61 67 20 20 20 20 20 20 20 20  esetFlag        
79690 20 2f 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77   /* Reset high-w
796a0 61 74 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75  ater mark if tru
796b0 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68  e */.){.  switch
796c0 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
796d0 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53   SQLITE_DBSTATUS
796e0 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a  _LOOKASIDE_USED:
796f0 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65   {.      *pCurre
79700 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  nt = db->lookasi
79710 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a  de.nOut;.      *
79720 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d  pHighwater = db-
79730 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74  >lookaside.mxOut
79740 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65  ;.      if( rese
79750 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20  tFlag ){.       
79760 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d   db->lookaside.m
79770 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  xOut = db->looka
79780 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20  side.nOut;.     
79790 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
797a0 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
797b0 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  t: {.      retur
797c0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
797d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
797e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
797f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
79800 20 45 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63   End of status.c
79810 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
79820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
79840 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
79850 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65   Begin file date
79860 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
79870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
79890 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f  ./*.** 2003 Octo
798a0 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 31.**.** The
798b0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
798c0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
798d0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
798e0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
798f0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
79900 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
79910 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
79920 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
79930 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
79940 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
79950 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
79960 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
79970 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
79980 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
79990 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
799a0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
799b0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
799c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
799d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
799e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
799f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79a00 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
79a10 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
79a20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
79a30 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20   implement date 
79a40 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63  and time.** func
79a50 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65  tions for SQLite
79a60 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  .  .**.** There 
79a70 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f  is only one expo
79a80 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74  rted symbol in t
79a90 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66  his file - the f
79aa0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74  unction.** sqlit
79ab0 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69  e3RegisterDateTi
79ac0 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f  meFunctions() fo
79ad0 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f  und at the botto
79ae0 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a  m of the file..*
79af0 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65  * All other code
79b00 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e   has file scope.
79b10 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72  .**.** SQLite pr
79b20 6f 63 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65  ocesses all time
79b30 73 20 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a  s and dates as J
79b40 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72  ulian Day number
79b50 73 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73  s.  The.** dates
79b60 20 61 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73   and times are s
79b70 74 6f 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d  tored as the num
79b80 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63  ber of days sinc
79b90 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65  e noon.** in Gre
79ba0 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62  enwich on Novemb
79bb0 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e  er 24, 4714 B.C.
79bc0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
79bd0 65 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63  e Gregorian.** c
79be0 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20  alendar system. 
79bf0 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30  .**.** 1970-01-0
79c00 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44  1 00:00:00 is JD
79c10 20 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30   2440587.5.** 20
79c20 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30  00-01-01 00:00:0
79c30 30 20 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e  0 is JD 2451544.
79c40 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  5.**.** This imp
79c50 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72  lemention requir
79c60 65 73 20 79 65 61 72 73 20 74 6f 20 62 65 20 65  es years to be e
79c70 78 70 72 65 73 73 65 64 20 61 73 20 61 20 34 2d  xpressed as a 4-
79c80 64 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20  digit number.** 
79c90 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74  which means that
79ca0 20 6f 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77   only dates betw
79cb0 65 65 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61  een 0000-01-01 a
79cc0 6e 64 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61  nd 9999-12-31 ca
79cd0 6e 0a 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e  n.** be represen
79ce0 74 65 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68  ted, even though
79cf0 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   julian day numb
79d00 65 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68  ers allow a much
79d10 20 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20   wider.** range 
79d20 6f 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  of dates..**.** 
79d30 54 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61  The Gregorian ca
79d40 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73  lendar system is
79d50 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61   used for all da
79d60 74 65 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a  tes and times,.*
79d70 2a 20 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61  * even those tha
79d80 74 20 70 72 65 64 61 74 65 20 74 68 65 20 47 72  t predate the Gr
79d90 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72  egorian calendar
79da0 2e 20 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73  .  Historians us
79db0 75 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65  ually.** use the
79dc0 20 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72   Julian calendar
79dd0 20 66 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72   for dates prior
79de0 20 74 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61   to 1582-10-15 a
79df0 6e 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64  nd for some.** d
79e00 61 74 65 73 20 61 66 74 65 72 77 61 72 64 73 2c  ates afterwards,
79e10 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f   depending on lo
79e20 63 61 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66  cale.  Beware of
79e30 20 74 68 69 73 20 64 69 66 66 65 72 65 6e 63 65   this difference
79e40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76  ..**.** The conv
79e50 65 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d  ersion algorithm
79e60 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65  s are implemente
79e70 64 20 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72  d based on descr
79e80 69 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68  iptions.** in th
79e90 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74  e following text
79ea0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61  :.**.**      Jea
79eb0 6e 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20  n Meeus.**      
79ec0 41 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67  Astronomical Alg
79ed0 6f 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69  orithms, 2nd Edi
79ee0 74 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20  tion, 1998.**   
79ef0 20 20 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36     ISBM 0-943396
79f00 2d 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69  -61-1.**      Wi
79f10 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63  llmann-Bell, Inc
79f20 0a 2a 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e  .**      Richmon
79f30 64 2c 20 56 69 72 67 69 6e 69 61 20 28 55 53 41  d, Virginia (USA
79f40 29 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74  ).*/.#include <t
79f50 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20  ime.h>..#ifndef 
79f60 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45  SQLITE_OMIT_DATE
79f70 54 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a  TIME_FUNCS../*.*
79f80 2a 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64  * On recent Wind
79f90 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74  ows platforms, t
79fa0 68 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29  he localtime_s()
79fb0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61   function is ava
79fc0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72  ilable.** as par
79fd0 74 20 6f 66 20 74 68 65 20 22 53 65 63 75 72 65  t of the "Secure
79fe0 20 43 52 54 22 2e 20 49 74 20 69 73 20 65 73 73   CRT". It is ess
79ff0 65 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c  entially equival
7a000 65 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c  ent to .** local
7a010 74 69 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62  time_r() availab
7a020 6c 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f  le under most PO
7a030 53 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65  SIX platforms, e
7a040 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 0a  xcept that the .
7a050 2a 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20  ** order of the 
7a060 70 61 72 61 6d 65 74 65 72 73 20 69 73 20 72 65  parameters is re
7a070 76 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65  versed..**.** Se
7a080 65 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69  e http://msdn.mi
7a090 63 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75  crosoft.com/en-u
7a0a0 73 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33  s/library/a442x3
7a0b0 79 65 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a  ye(VS.80).aspx..
7a0c0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65  **.** If the use
7a0d0 72 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61  r has not indica
7a0e0 74 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c  ted to use local
7a0f0 74 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61  time_r() or loca
7a100 6c 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72  ltime_s().** alr
7a110 65 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20  eady, check for 
7a120 61 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e  an MSVC build en
7a130 76 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70  vironment that p
7a140 72 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61  rovides .** loca
7a150 6c 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69  ltime_s()..*/.#i
7a160 66 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f  f !defined(HAVE_
7a170 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20  LOCALTIME_R) && 
7a180 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f  !defined(HAVE_LO
7a190 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a  CALTIME_S) && \.
7a1a0 20 20 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53       defined(_MS
7a1b0 43 5f 56 45 52 29 20 26 26 20 64 65 66 69 6e 65  C_VER) && define
7a1c0 64 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f  d(_CRT_INSECURE_
7a1d0 44 45 50 52 45 43 41 54 45 29 0a 23 64 65 66 69  DEPRECATE).#defi
7a1e0 6e 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ne HAVE_LOCALTIM
7a1f0 45 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  E_S 1.#endif../*
7a200 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20  .** A structure 
7a210 66 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69  for holding a si
7a220 6e 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69  ngle date and ti
7a230 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  me..*/.typedef s
7a240 74 72 75 63 74 20 44 61 74 65 54 69 6d 65 20 44  truct DateTime D
7a250 61 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20  ateTime;.struct 
7a260 44 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c  DateTime {.  sql
7a270 69 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20  ite3_int64 iJD; 
7a280 2f 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61  /* The julian da
7a290 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38  y number times 8
7a2a0 36 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74  6400000 */.  int
7a2b0 20 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20   Y, M, D;       
7a2c0 2f 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20  /* Year, month, 
7a2d0 61 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74  and day */.  int
7a2e0 20 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20   h, m;          
7a2f0 2f 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75  /* Hour and minu
7a300 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b  tes */.  int tz;
7a310 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
7a320 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69  imezone offset i
7a330 6e 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64  n minutes */.  d
7a340 6f 75 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20  ouble s;        
7a350 20 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a    /* Seconds */.
7a360 20 20 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b    char validYMD;
7a370 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29       /* True (1)
7a380 20 69 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61   if Y,M,D are va
7a390 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61  lid */.  char va
7a3a0 6c 69 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54  lidHMS;     /* T
7a3b0 72 75 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73  rue (1) if h,m,s
7a3c0 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20   are valid */.  
7a3d0 63 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20  char validJD;   
7a3e0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69     /* True (1) i
7a3f0 66 20 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a  f iJD is valid *
7a400 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a  /.  char validTZ
7a410 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28  ;      /* True (
7a420 31 29 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69  1) if tz is vali
7a430 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  d */.};.../*.** 
7a440 43 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e  Convert zDate in
7a450 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69  to one or more i
7a460 6e 74 65 67 65 72 73 2e 20 20 41 64 64 69 74 69  ntegers.  Additi
7a470 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a  onal arguments.*
7a480 2a 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73  * come in groups
7a490 20 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73   of 5 as follows
7a4a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20  :.**.**       N 
7a4b0 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20        number of 
7a4c0 64 69 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e  digits in the in
7a4d0 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d  teger.**       m
7a4e0 69 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61  in     minimum a
7a4f0 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20  llowed value of 
7a500 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  the integer.**  
7a510 20 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78       max     max
7a520 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c  imum allowed val
7a530 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ue of the intege
7a540 72 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43  r.**       nextC
7a550 20 20 20 66 69 72 73 74 20 63 68 61 72 61 63 74     first charact
7a560 65 72 20 61 66 74 65 72 20 74 68 65 20 69 6e 74  er after the int
7a570 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56  eger.**       pV
7a580 61 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20 77  al    where to w
7a590 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 72  rite the integer
7a5a0 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43  s value..**.** C
7a5b0 6f 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69  onversions conti
7a5c0 6e 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69  nue until one wi
7a5d0 74 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65  th nextC==0 is e
7a5e0 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54  ncountered..** T
7a5f0 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
7a600 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
7a610 66 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e  f successful con
7a620 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61  versions..*/.sta
7a630 74 69 63 20 69 6e 74 20 67 65 74 44 69 67 69 74  tic int getDigit
7a640 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  s(const char *zD
7a650 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  ate, ...){.  va_
7a660 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76  list ap;.  int v
7a670 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69  al;.  int N;.  i
7a680 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61  nt min;.  int ma
7a690 78 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a  x;.  int nextC;.
7a6a0 20 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69    int *pVal;.  i
7a6b0 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61  nt cnt = 0;.  va
7a6c0 5f 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65  _start(ap, zDate
7a6d0 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d  );.  do{.    N =
7a6e0 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
7a6f0 3b 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61  ;.    min = va_a
7a700 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
7a710 20 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70   max = va_arg(ap
7a720 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74  , int);.    next
7a730 43 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  C = va_arg(ap, i
7a740 6e 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20  nt);.    pVal = 
7a750 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29  va_arg(ap, int*)
7a760 3b 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20  ;.    val = 0;. 
7a770 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b     while( N-- ){
7a780 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69  .      if( !sqli
7a790 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74  te3Isdigit(*zDat
7a7a0 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  e) ){.        go
7a7b0 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73  to end_getDigits
7a7c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7a7d0 76 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a  val = val*10 + *
7a7e0 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20  zDate - '0';.   
7a7f0 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
7a800 7d 0a 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69  }.    if( val<mi
7a810 6e 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20  n || val>max || 
7a820 28 6e 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78  (nextC!=0 && nex
7a830 74 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20  tC!=*zDate) ){. 
7a840 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65       goto end_ge
7a850 74 44 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20  tDigits;.    }. 
7a860 20 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a     *pVal = val;.
7a870 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20      zDate++;.   
7a880 20 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65   cnt++;.  }while
7a890 28 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67  ( nextC );.end_g
7a8a0 65 74 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65  etDigits:.  va_e
7a8b0 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e  nd(ap);.  return
7a8c0 20 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   cnt;.}../*.** R
7a8d0 65 61 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b  ead text from z[
7a8e0 5d 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e  ] and convert in
7a8f0 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  to a floating po
7a900 69 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74  int number.  Ret
7a910 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  urn.** the numbe
7a920 72 20 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76  r of digits conv
7a930 65 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  erted..*/.#defin
7a940 65 20 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74  e getValue sqlit
7a950 65 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61  e3AtoF../*.** Pa
7a960 72 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65  rse a timezone e
7a970 78 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20  xtension on the 
7a980 65 6e 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69  end of a date-ti
7a990 6d 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e  me..** The exten
7a9a0 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66  sion is of the f
7a9b0 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  orm:.**.**      
7a9c0 20 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a    (+/-)HH:MM.**.
7a9d0 2a 2a 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22  ** Or the "zulu"
7a9e0 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a   notation:.**.**
7a9f0 20 20 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20          Z.**.** 
7aa00 49 66 20 74 68 65 20 70 61 72 73 65 20 69 73 20  If the parse is 
7aa10 73 75 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74  successful, writ
7aa20 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
7aa30 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68  minutes.** of ch
7aa40 61 6e 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e  ange in p->tz an
7aa50 64 20 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20  d return 0.  If 
7aa60 61 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f  a parser error o
7aa70 63 63 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e  ccurs,.** return
7aa80 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a   non-zero..**.**
7aa90 20 41 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69   A missing speci
7aaa0 66 69 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73  fier is not cons
7aab0 69 64 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e  idered an error.
7aac0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
7aad0 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e  arseTimezone(con
7aae0 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20  st char *zDate, 
7aaf0 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
7ab00 69 6e 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69  int sgn = 0;.  i
7ab10 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69  nt nHr, nMn;.  i
7ab20 6e 74 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73  nt c;.  while( s
7ab30 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a  qlite3Isspace(*z
7ab40 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b  Date) ){ zDate++
7ab50 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b  ; }.  p->tz = 0;
7ab60 0a 20 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20  .  c = *zDate;. 
7ab70 20 69 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20   if( c=='-' ){. 
7ab80 20 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d     sgn = -1;.  }
7ab90 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20  else if( c=='+' 
7aba0 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b  ){.    sgn = +1;
7abb0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d  .  }else if( c==
7abc0 27 5a 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b  'Z' || c=='z' ){
7abd0 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20  .    zDate++;.  
7abe0 20 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65    goto zulu_time
7abf0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
7ac00 65 74 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a  eturn c!=0;.  }.
7ac10 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28    zDate++;.  if(
7ac20 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65   getDigits(zDate
7ac30 2c 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c  , 2, 0, 14, ':',
7ac40 20 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c   &nHr, 2, 0, 59,
7ac50 20 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a   0, &nMn)!=2 ){.
7ac60 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
7ac70 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a  }.  zDate += 5;.
7ac80 20 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e    p->tz = sgn*(n
7ac90 4d 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75  Mn + nHr*60);.zu
7aca0 6c 75 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65  lu_time:.  while
7acb0 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ( sqlite3Isspace
7acc0 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74  (*zDate) ){ zDat
7acd0 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  e++; }.  return 
7ace0 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a  *zDate!=0;.}../*
7acf0 0a 2a 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20  .** Parse times 
7ad00 6f 66 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d  of the form HH:M
7ad10 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72  M or HH:MM:SS or
7ad20 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a   HH:MM:SS.FFFF..
7ad30 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61  ** The HH, MM, a
7ad40 6e 64 20 53 53 20 6d 75 73 74 20 65 61 63 68 20  nd SS must each 
7ad50 62 65 20 65 78 61 63 74 6c 79 20 32 20 64 69 67  be exactly 2 dig
7ad60 69 74 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61  its.  The.** fra
7ad70 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20  ctional seconds 
7ad80 46 46 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20  FFFF can be one 
7ad90 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a  or more digits..
7ada0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69  **.** Return 1 i
7adb0 66 20 74 68 65 72 65 20 69 73 20 61 20 70 61 72  f there is a par
7adc0 73 69 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30  sing error and 0
7add0 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a   on success..*/.
7ade0 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65  static int parse
7adf0 48 68 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61  HhMmSs(const cha
7ae00 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69  r *zDate, DateTi
7ae10 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c  me *p){.  int h,
7ae20 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20   m, s;.  double 
7ae30 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20  ms = 0.0;.  if( 
7ae40 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c  getDigits(zDate,
7ae50 20 32 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20   2, 0, 24, ':', 
7ae60 26 68 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c  &h, 2, 0, 59, 0,
7ae70 20 26 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72   &m)!=2 ){.    r
7ae80 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a  eturn 1;.  }.  z
7ae90 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28  Date += 5;.  if(
7aea0 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a   *zDate==':' ){.
7aeb0 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20      zDate++;.   
7aec0 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a   if( getDigits(z
7aed0 44 61 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20  Date, 2, 0, 59, 
7aee0 30 2c 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20  0, &s)!=1 ){.   
7aef0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
7af00 20 7d 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20   }.    zDate += 
7af10 32 3b 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74  2;.    if( *zDat
7af20 65 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65  e=='.' && sqlite
7af30 33 49 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31  3Isdigit(zDate[1
7af40 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62  ]) ){.      doub
7af50 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b  le rScale = 1.0;
7af60 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a  .      zDate++;.
7af70 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
7af80 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61  ite3Isdigit(*zDa
7af90 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  te) ){.        m
7afa0 73 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a  s = ms*10.0 + *z
7afb0 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20  Date - '0';.    
7afc0 20 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30      rScale *= 10
7afd0 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74  .0;.        zDat
7afe0 65 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  e++;.      }.   
7aff0 20 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b     ms /= rScale;
7b000 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
7b010 20 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20      s = 0;.  }. 
7b020 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b   p->validJD = 0;
7b030 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d  .  p->validHMS =
7b040 20 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a   1;.  p->h = h;.
7b050 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d    p->m = m;.  p-
7b060 3e 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69  >s = s + ms;.  i
7b070 66 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65  f( parseTimezone
7b080 28 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74  (zDate, p) ) ret
7b090 75 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69  urn 1;.  p->vali
7b0a0 64 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29  dTZ = (p->tz!=0)
7b0b0 3f 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30  ?1:0;.  return 0
7b0c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
7b0d0 72 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d  rt from YYYY-MM-
7b0e0 44 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a  DD HH:MM:SS to j
7b0f0 75 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61  ulian day.  We a
7b100 6c 77 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20  lways assume.** 
7b110 74 68 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d  that the YYYY-MM
7b120 2d 44 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67  -DD is according
7b130 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61   to the Gregoria
7b140 6e 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a  n calendar..**.*
7b150 2a 20 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65  * Reference:  Me
7b160 65 75 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73  eus page 61.*/.s
7b170 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
7b180 74 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70  teJD(DateTime *p
7b190 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44  ){.  int Y, M, D
7b1a0 2c 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a  , A, B, X1, X2;.
7b1b0 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a  .  if( p->validJ
7b1c0 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  D ) return;.  if
7b1d0 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b  ( p->validYMD ){
7b1e0 0a 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20  .    Y = p->Y;. 
7b1f0 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20     M = p->M;.   
7b200 20 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c   D = p->D;.  }el
7b210 73 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30  se{.    Y = 2000
7b220 3b 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20  ;  /* If no YMD 
7b230 73 70 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d  specified, assum
7b240 65 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f  e 2000-Jan-01 */
7b250 0a 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20  .    M = 1;.    
7b260 44 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  D = 1;.  }.  if(
7b270 20 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d   M<=2 ){.    Y--
7b280 3b 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20  ;.    M += 12;. 
7b290 20 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a   }.  A = Y/100;.
7b2a0 20 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41    B = 2 - A + (A
7b2b0 2f 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32  /4);.  X1 = 3652
7b2c0 35 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a  5*(Y+4716)/100;.
7b2d0 20 20 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d    X2 = 306001*(M
7b2e0 2b 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e  +1)/10000;.  p->
7b2f0 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  iJD = (sqlite3_i
7b300 6e 74 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b  nt64)((X1 + X2 +
7b310 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20   D + B - 1524.5 
7b320 29 20 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20  ) * 86400000);. 
7b330 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b   p->validJD = 1;
7b340 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48  .  if( p->validH
7b350 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44  MS ){.    p->iJD
7b360 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30   += p->h*3600000
7b370 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20   + p->m*60000 + 
7b380 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
7b390 70 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20  p->s*1000);.    
7b3a0 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29  if( p->validTZ )
7b3b0 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d  {.      p->iJD -
7b3c0 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20  = p->tz*60000;. 
7b3d0 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44       p->validYMD
7b3e0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76   = 0;.      p->v
7b3f0 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20  alidHMS = 0;.   
7b400 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20     p->validTZ = 
7b410 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
7b420 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65  /*.** Parse date
7b430 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a  s of the form.**
7b440 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d  .**     YYYY-MM-
7b450 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a  DD HH:MM:SS.FFF.
7b460 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44  **     YYYY-MM-D
7b470 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20  D HH:MM:SS.**   
7b480 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
7b490 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d  MM.**     YYYY-M
7b4a0 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65  M-DD.**.** Write
7b4b0 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f   the result into
7b4c0 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74   the DateTime st
7b4d0 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75  ructure and retu
7b4e0 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  rn 0.** on succe
7b4f0 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 20  ss and 1 if the 
7b500 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 20  input string is 
7b510 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65  not a well-forme
7b520 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74  d.** date..*/.st
7b530 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 79  atic int parseYy
7b540 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61  yyMmDd(const cha
7b550 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69  r *zDate, DateTi
7b560 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c  me *p){.  int Y,
7b570 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69   M, D, neg;..  i
7b580 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27  f( zDate[0]=='-'
7b590 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b   ){.    zDate++;
7b5a0 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20  .    neg = 1;.  
7b5b0 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d  }else{.    neg =
7b5c0 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65   0;.  }.  if( ge
7b5d0 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c  tDigits(zDate,4,
7b5e0 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c  0,9999,'-',&Y,2,
7b5f0 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c  1,12,'-',&M,2,1,
7b600 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20  31,0,&D)!=3 ){. 
7b610 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
7b620 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a  .  zDate += 10;.
7b630 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
7b640 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20  Isspace(*zDate) 
7b650 7c 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44  || 'T'==*(u8*)zD
7b660 61 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20  ate ){ zDate++; 
7b670 7d 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d  }.  if( parseHhM
7b680 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30  mSs(zDate, p)==0
7b690 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f   ){.    /* We go
7b6a0 74 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20  t the time */.  
7b6b0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65  }else if( *zDate
7b6c0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61  ==0 ){.    p->va
7b6d0 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65  lidHMS = 0;.  }e
7b6e0 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
7b6f0 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69  1;.  }.  p->vali
7b700 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  dJD = 0;.  p->va
7b710 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d  lidYMD = 1;.  p-
7b720 3e 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20  >Y = neg ? -Y : 
7b730 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20  Y;.  p->M = M;. 
7b740 20 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28   p->D = D;.  if(
7b750 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20   p->validTZ ){. 
7b760 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
7b770 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
7b780 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
7b790 65 20 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75  e time to the cu
7b7a0 72 72 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72  rrent time repor
7b7b0 74 65 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a  ted by the VFS.*
7b7c0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
7b7d0 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65  tDateTimeToCurre
7b7e0 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
7b7f0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74  xt *context, Dat
7b800 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75  eTime *p){.  dou
7b810 62 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33  ble r;.  sqlite3
7b820 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
7b830 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
7b840 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c  (context);.  sql
7b850 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
7b860 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b  e(db->pVfs, &r);
7b870 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c  .  p->iJD = (sql
7b880 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36  ite3_int64)(r*86
7b890 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b  400000.0 + 0.5);
7b8a0 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20  .  p->validJD = 
7b8b0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  1;.}../*.** Atte
7b8c0 6d 70 74 20 74 6f 20 70 61 72 73 65 20 74 68 65  mpt to parse the
7b8d0 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e   given string in
7b8e0 74 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20  to a Julian Day 
7b8f0 4e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a  Number.  Return.
7b900 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
7b910 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54   errors..**.** T
7b920 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
7b930 20 61 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d   acceptable form
7b940 73 20 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20  s for the input 
7b950 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  string:.**.**   
7b960 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48     YYYY-MM-DD HH
7b970 3a 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48  :MM:SS.FFF  +/-H
7b980 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44  H:MM.**      DDD
7b990 44 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f  D.DD .**      no
7b9a0 77 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66  w.**.** In the f
7b9b0 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b  irst form, the +
7b9c0 2f 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79  /-HH:MM is alway
7b9d0 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65  s optional.  The
7b9e0 20 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73   fractional.** s
7b9f0 65 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e  econds extension
7ba00 20 28 74 68 65 20 22 2e 46 46 46 22 29 20 69 73   (the ".FFF") is
7ba10 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20   optional.  The 
7ba20 73 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a  seconds portion.
7ba30 2a 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69  ** (":SS.FFF") i
7ba40 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79  s option.  The y
7ba50 65 61 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e  ear and date can
7ba60 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c   be omitted as l
7ba70 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20  ong.** as there 
7ba80 69 73 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67  is a time string
7ba90 2e 20 20 54 68 65 20 74 69 6d 65 20 73 74 72 69  .  The time stri
7baa0 6e 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65  ng can be omitte
7bab0 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20  d as long.** as 
7bac0 74 68 65 72 65 20 69 73 20 61 20 79 65 61 72 20  there is a year 
7bad0 61 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61  and date..*/.sta
7bae0 74 69 63 20 69 6e 74 20 70 61 72 73 65 44 61 74  tic int parseDat
7baf0 65 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74  eOrTime(.  sqlit
7bb00 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
7bb10 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  ext, .  const ch
7bb20 61 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61  ar *zDate, .  Da
7bb30 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69  teTime *p.){.  i
7bb40 6e 74 20 69 73 52 65 61 6c 4e 75 6d 3b 20 20 20  nt isRealNum;   
7bb50 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20   /* Return from 
7bb60 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28  sqlite3IsNumber(
7bb70 29 2e 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a  ).  Not used */.
7bb80 20 20 69 66 28 20 70 61 72 73 65 59 79 79 79 4d    if( parseYyyyM
7bb90 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20  mDd(zDate,p)==0 
7bba0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
7bbb0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72  .  }else if( par
7bbc0 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20  seHhMmSs(zDate, 
7bbd0 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  p)==0 ){.    ret
7bbe0 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 0;.  }else i
7bbf0 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
7bc00 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d  p(zDate,"now")==
7bc10 30 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54  0){.    setDateT
7bc20 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e  imeToCurrent(con
7bc30 74 65 78 74 2c 20 70 29 3b 0a 20 20 20 20 72 65  text, p);.    re
7bc40 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20  turn 0;.  }else 
7bc50 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  if( sqlite3IsNum
7bc60 62 65 72 28 7a 44 61 74 65 2c 20 26 69 73 52 65  ber(zDate, &isRe
7bc70 61 6c 4e 75 6d 2c 20 53 51 4c 49 54 45 5f 55 54  alNum, SQLITE_UT
7bc80 46 38 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c  F8) ){.    doubl
7bc90 65 20 72 3b 0a 20 20 20 20 67 65 74 56 61 6c 75  e r;.    getValu
7bca0 65 28 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 20  e(zDate, &r);.  
7bcb0 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69    p->iJD = (sqli
7bcc0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34  te3_int64)(r*864
7bcd0 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a  00000.0 + 0.5);.
7bce0 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
7bcf0 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   1;.    return 0
7bd00 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  ;.  }.  return 1
7bd10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
7bd20 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e  te the Year, Mon
7bd30 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d  th, and Day from
7bd40 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20   the julian day 
7bd50 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
7bd60 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d  c void computeYM
7bd70 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  D(DateTime *p){.
7bd80 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43    int Z, A, B, C
7bd90 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66  , D, E, X1;.  if
7bda0 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20  ( p->validYMD ) 
7bdb0 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70  return;.  if( !p
7bdc0 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20  ->validJD ){.   
7bdd0 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20   p->Y = 2000;.  
7bde0 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20    p->M = 1;.    
7bdf0 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  p->D = 1;.  }els
7be00 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29  e{.    Z = (int)
7be10 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30  ((p->iJD + 43200
7be20 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a  000)/86400000);.
7be30 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a      A = (int)((Z
7be40 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33   - 1867216.25)/3
7be50 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 20  6524.25);.    A 
7be60 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41  = Z + 1 + A - (A
7be70 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b  /4);.    B = A +
7be80 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28   1524;.    C = (
7be90 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e 31 29  int)((B - 122.1)
7bea0 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 20  /365.25);.    D 
7beb0 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 30 3b  = (36525*C)/100;
7bec0 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 28 28  .    E = (int)((
7bed0 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a 20  B-D)/30.6001);. 
7bee0 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 33 30     X1 = (int)(30
7bef0 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 2d  .6001*E);.    p-
7bf00 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b  >D = B - D - X1;
7bf10 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34  .    p->M = E<14
7bf20 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20   ? E-1 : E-13;. 
7bf30 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32     p->Y = p->M>2
7bf40 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20   ? C - 4716 : C 
7bf50 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d  - 4715;.  }.  p-
7bf60 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d  >validYMD = 1;.}
7bf70 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
7bf80 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65  the Hour, Minute
7bf90 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72  , and Seconds fr
7bfa0 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61  om the julian da
7bfb0 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  y number..*/.sta
7bfc0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65  tic void compute
7bfd0 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29  HMS(DateTime *p)
7bfe0 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 28  {.  int s;.  if(
7bff0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 72   p->validHMS ) r
7c000 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 65  eturn;.  compute
7c010 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 6e  JD(p);.  s = (in
7c020 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32  t)((p->iJD + 432
7c030 30 30 30 30 30 29 20 25 20 38 36 34 30 30 30 30  00000) % 8640000
7c040 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31  0);.  p->s = s/1
7c050 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 6e  000.0;.  s = (in
7c060 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d  t)p->s;.  p->s -
7c070 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f  = s;.  p->h = s/
7c080 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e  3600;.  s -= p->
7c090 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d  h*3600;.  p->m =
7c0a0 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d   s/60;.  p->s +=
7c0b0 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20   s - p->m*60;.  
7c0c0 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b  p->validHMS = 1;
7c0d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  .}../*.** Comput
7c0e0 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48  e both YMD and H
7c0f0 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  MS.*/.static voi
7c100 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53  d computeYMD_HMS
7c110 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20  (DateTime *p){. 
7c120 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a   computeYMD(p);.
7c130 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b    computeHMS(p);
7c140 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
7c150 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20  the YMD and HMS 
7c160 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74  and the TZ.*/.st
7c170 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59  atic void clearY
7c180 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69  MD_HMS_TZ(DateTi
7c190 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c  me *p){.  p->val
7c1a0 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e  idYMD = 0;.  p->
7c1b0 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20  validHMS = 0;.  
7c1c0 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
7c1d0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
7c1e0 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45  E_OMIT_LOCALTIME
7c1f0 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
7c200 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 28 69  he difference (i
7c210 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a  n milliseconds).
7c220 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c  ** between local
7c230 74 69 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e  time and UTC (a.
7c240 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72  k.a. GMT).** for
7c250 20 74 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20   the time value 
7c260 70 20 77 68 65 72 65 20 70 20 69 73 20 69 6e 20  p where p is in 
7c270 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  UTC..*/.static s
7c280 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63  qlite3_int64 loc
7c290 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74  altimeOffset(Dat
7c2a0 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74  eTime *p){.  Dat
7c2b0 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69  eTime x, y;.  ti
7c2c0 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70  me_t t;.  x = *p
7c2d0 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48  ;.  computeYMD_H
7c2e0 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e  MS(&x);.  if( x.
7c2f0 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32  Y<1971 || x.Y>=2
7c300 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d  038 ){.    x.Y =
7c310 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d   2000;.    x.M =
7c320 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b   1;.    x.D = 1;
7c330 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20  .    x.h = 0;.  
7c340 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78    x.m = 0;.    x
7c350 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c  .s = 0.0;.  } el
7c360 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d  se {.    int s =
7c370 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e 35   (int)(x.s + 0.5
7c380 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a  );.    x.s = s;.
7c390 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a    }.  x.tz = 0;.
7c3a0 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b    x.validJD = 0;
7c3b0 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29  .  computeJD(&x)
7c3c0 3b 0a 20 20 74 20 3d 20 28 74 69 6d 65 5f 74 29  ;.  t = (time_t)
7c3d0 28 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 31  (x.iJD/1000 - 21
7c3e0 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30  086676*(i64)1000
7c3f0 30 29 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f  0);.#ifdef HAVE_
7c400 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a  LOCALTIME_R.  {.
7c410 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c      struct tm sL
7c420 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74  ocal;.    localt
7c430 69 6d 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61  ime_r(&t, &sLoca
7c440 6c 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c  l);.    y.Y = sL
7c450 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31  ocal.tm_year + 1
7c460 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73  900;.    y.M = s
7c470 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31  Local.tm_mon + 1
7c480 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63  ;.    y.D = sLoc
7c490 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20  al.tm_mday;.    
7c4a0 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.h = sLocal.tm_
7c4b0 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20  hour;.    y.m = 
7c4c0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20  sLocal.tm_min;. 
7c4d0 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e     y.s = sLocal.
7c4e0 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 69  tm_sec;.  }.#eli
7c4f0 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c  f defined(HAVE_L
7c500 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 48  OCALTIME_S) && H
7c510 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 0a  AVE_LOCALTIME_S.
7c520 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
7c530 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f  m sLocal;.    lo
7c540 63 61 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61  caltime_s(&sLoca
7c550 6c 2c 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20  l, &t);.    y.Y 
7c560 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72  = sLocal.tm_year
7c570 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d   + 1900;.    y.M
7c580 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e   = sLocal.tm_mon
7c590 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20   + 1;.    y.D = 
7c5a0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a  sLocal.tm_mday;.
7c5b0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c      y.h = sLocal
7c5c0 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e  .tm_hour;.    y.
7c5d0 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69  m = sLocal.tm_mi
7c5e0 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f  n;.    y.s = sLo
7c5f0 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a  cal.tm_sec;.  }.
7c600 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74  #else.  {.    st
7c610 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20  ruct tm *pTm;.  
7c620 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
7c630 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74  enter(sqlite3Mut
7c640 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
7c650 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
7c660 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20  ER));.    pTm = 
7c670 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20  localtime(&t);. 
7c680 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d     y.Y = pTm->tm
7c690 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20  _year + 1900;.  
7c6a0 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f    y.M = pTm->tm_
7c6b0 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44  mon + 1;.    y.D
7c6c0 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b   = pTm->tm_mday;
7c6d0 0a 20 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e  .    y.h = pTm->
7c6e0 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d  tm_hour;.    y.m
7c6f0 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a   = pTm->tm_min;.
7c700 20 20 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74      y.s = pTm->t
7c710 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74  m_sec;.    sqlit
7c720 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
7c730 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
7c740 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
7c750 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20  ATIC_MASTER));. 
7c760 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61   }.#endif.  y.va
7c770 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e  lidYMD = 1;.  y.
7c780 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20  validHMS = 1;.  
7c790 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  y.validJD = 0;. 
7c7a0 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a   y.validTZ = 0;.
7c7b0 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b    computeJD(&y);
7c7c0 0a 20 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20  .  return y.iJD 
7c7d0 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69  - x.iJD;.}.#endi
7c7e0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
7c7f0 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f  _LOCALTIME */../
7c800 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d  *.** Process a m
7c810 6f 64 69 66 69 65 72 20 74 6f 20 61 20 64 61 74  odifier to a dat
7c820 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54  e-time stamp.  T
7c830 68 65 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65  he modifiers are
7c840 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  .** as follows:.
7c850 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61  **.**     NNN da
7c860 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f  ys.**     NNN ho
7c870 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d  urs.**     NNN m
7c880 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e  inutes.**     NN
7c890 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a  N.NNNN seconds.*
7c8a0 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73  *     NNN months
7c8b0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72  .**     NNN year
7c8c0 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f  s.**     start o
7c8d0 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73  f month.**     s
7c8e0 74 61 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20  tart of year.** 
7c8f0 20 20 20 20 73 74 61 72 74 20 6f 66 20 77 65 65      start of wee
7c900 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f  k.**     start o
7c910 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65  f day.**     wee
7c920 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e  kday N.**     un
7c930 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c  ixepoch.**     l
7c940 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20  ocaltime.**     
7c950 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  utc.**.** Return
7c960 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e   0 on success an
7c970 64 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20  d 1 if there is 
7c980 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f  any kind of erro
7c990 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
7c9a0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63   parseModifier(c
7c9b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c  onst char *zMod,
7c9c0 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20   DateTime *p){. 
7c9d0 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69   int rc = 1;.  i
7c9e0 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72  nt n;.  double r
7c9f0 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75  ;.  char *z, zBu
7ca00 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75  f[30];.  z = zBu
7ca10 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c  f;.  for(n=0; n<
7ca20 41 72 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d  ArraySize(zBuf)-
7ca30 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b  1 && zMod[n]; n+
7ca40 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 28  +){.    z[n] = (
7ca50 63 68 61 72 29 73 71 6c 69 74 65 33 55 70 70 65  char)sqlite3Uppe
7ca60 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f  rToLower[(u8)zMo
7ca70 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e  d[n]];.  }.  z[n
7ca80 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28  ] = 0;.  switch(
7ca90 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66   z[0] ){.#ifndef
7caa0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43   SQLITE_OMIT_LOC
7cab0 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20  ALTIME.    case 
7cac0 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  'l': {.      /* 
7cad0 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20     localtime.   
7cae0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41     **.      ** A
7caf0 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72  ssuming the curr
7cb00 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69  ent time value i
7cb10 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d  s UTC (a.k.a. GM
7cb20 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a  T), shift it to.
7cb30 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f        ** show lo
7cb40 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20  cal time..      
7cb50 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72  */.      if( str
7cb60 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d  cmp(z, "localtim
7cb70 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  e")==0 ){.      
7cb80 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7cb90 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b          p->iJD +
7cba0 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65  = localtimeOffse
7cbb0 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c  t(p);.        cl
7cbc0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
7cbd0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
7cbe0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7cbf0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
7cc00 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 75 27  dif.    case 'u'
7cc10 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20  : {.      /*.   
7cc20 20 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f     **    unixepo
7cc30 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  ch.      **.    
7cc40 20 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63    ** Treat the c
7cc50 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
7cc60 70 2d 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75  p->iJD as the nu
7cc70 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a  mber of.      **
7cc80 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31   seconds since 1
7cc90 39 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f  970.  Convert to
7cca0 20 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64   a real julian d
7ccb0 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20  ay number..     
7ccc0 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74   */.      if( st
7ccd0 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f  rcmp(z, "unixepo
7cce0 63 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61  ch")==0 && p->va
7ccf0 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20  lidJD ){.       
7cd00 20 70 2d 3e 69 4a 44 20 3d 20 28 70 2d 3e 69 4a   p->iJD = (p->iJ
7cd10 44 20 2b 20 34 33 32 30 30 29 2f 38 36 34 30 30  D + 43200)/86400
7cd20 20 2b 20 32 31 30 38 36 36 37 36 2a 28 69 36 34   + 21086676*(i64
7cd30 29 31 30 30 30 30 30 30 30 3b 0a 20 20 20 20 20  )10000000;.     
7cd40 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f     clearYMD_HMS_
7cd50 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72  TZ(p);.        r
7cd60 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23  c = 0;.      }.#
7cd70 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
7cd80 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20  IT_LOCALTIME.   
7cd90 20 20 20 65 6c 73 65 20 69 66 28 20 73 74 72 63     else if( strc
7cda0 6d 70 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20  mp(z, "utc")==0 
7cdb0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
7cdc0 65 33 5f 69 6e 74 36 34 20 63 31 3b 0a 20 20 20  e3_int64 c1;.   
7cdd0 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70       computeJD(p
7cde0 29 3b 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20  );.        c1 = 
7cdf0 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28  localtimeOffset(
7ce00 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  p);.        p->i
7ce10 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20  JD -= c1;.      
7ce20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54    clearYMD_HMS_T
7ce30 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  Z(p);.        p-
7ce40 3e 69 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63  >iJD += c1 - loc
7ce50 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b  altimeOffset(p);
7ce60 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
7ce70 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
7ce80 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7ce90 20 7d 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a   }.    case 'w':
7cea0 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20   {.      /*.    
7ceb0 20 20 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20    **    weekday 
7cec0 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  N.      **.     
7ced0 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74   ** Move the dat
7cee0 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69  e to the same ti
7cef0 6d 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f  me on the next o
7cf00 63 63 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20  ccurrence of.   
7cf10 20 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20     ** weekday N 
7cf20 77 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c  where 0==Sunday,
7cf30 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20   1==Monday, and 
7cf40 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68  so forth.  If th
7cf50 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20  e.      ** date 
7cf60 69 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68  is already on th
7cf70 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 77 65  e appropriate we
7cf80 65 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20 61  ekday, this is a
7cf90 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f   no-op..      */
7cfa0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63  .      if( strnc
7cfb0 6d 70 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22  mp(z, "weekday "
7cfc0 2c 20 38 29 3d 3d 30 20 26 26 20 67 65 74 56 61  , 8)==0 && getVa
7cfd0 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a  lue(&z[8],&r)>0.
7cfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7cff0 20 26 26 20 28 6e 3d 28 69 6e 74 29 72 29 3d 3d   && (n=(int)r)==
7d000 72 20 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37  r && n>=0 && r<7
7d010 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
7d020 74 65 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20  te3_int64 Z;.   
7d030 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
7d040 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
7d050 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
7d060 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64          p->valid
7d070 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  JD = 0;.        
7d080 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
7d090 20 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69        Z = ((p->i
7d0a0 4a 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f  JD + 129600000)/
7d0b0 38 36 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20  86400000) % 7;. 
7d0c0 20 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29         if( Z>n )
7d0d0 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20   Z -= 7;.       
7d0e0 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20   p->iJD += (n - 
7d0f0 5a 29 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20  Z)*86400000;.   
7d100 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d       clearYMD_HM
7d110 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20  S_TZ(p);.       
7d120 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d   rc = 0;.      }
7d130 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
7d140 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27    }.    case 's'
7d150 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20  : {.      /*.   
7d160 20 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f     **    start o
7d170 66 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a  f TTTTT.      **
7d180 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74  .      ** Move t
7d190 68 65 20 64 61 74 65 20 62 61 63 6b 77 61 72 64  he date backward
7d1a0 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  s to the beginni
7d1b0 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ng of the curren
7d1c0 74 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20  t day,.      ** 
7d1d0 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72  or month or year
7d1e0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
7d1f0 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20   if( strncmp(z, 
7d200 22 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21  "start of ", 9)!
7d210 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
7d220 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20    z += 9;.      
7d230 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20  computeYMD(p);. 
7d240 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53       p->validHMS
7d250 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68   = 1;.      p->h
7d260 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20   = p->m = 0;.   
7d270 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20     p->s = 0.0;. 
7d280 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20       p->validTZ 
7d290 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  = 0;.      p->va
7d2a0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20  lidJD = 0;.     
7d2b0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d   if( strcmp(z,"m
7d2c0 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20  onth")==0 ){.   
7d2d0 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20       p->D = 1;. 
7d2e0 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
7d2f0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73       }else if( s
7d300 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d  trcmp(z,"year")=
7d310 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  =0 ){.        co
7d320 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20  mputeYMD(p);.   
7d330 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20       p->M = 1;. 
7d340 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b         p->D = 1;
7d350 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
7d360 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
7d370 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29   strcmp(z,"day")
7d380 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
7d390 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 0;.      }. 
7d3a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7d3b0 7d 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a  }.    case '+':.
7d3c0 20 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20      case '-':.  
7d3d0 20 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20    case '0':.    
7d3e0 63 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61  case '1':.    ca
7d3f0 73 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65  se '2':.    case
7d400 20 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '3':.    case '
7d410 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27  4':.    case '5'
7d420 3a 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a  :.    case '6':.
7d430 20 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20      case '7':.  
7d440 20 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20    case '8':.    
7d450 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20  case '9': {.    
7d460 20 20 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65    double rRounde
7d470 72 3b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74  r;.      n = get
7d480 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20  Value(z, &r);.  
7d490 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31      assert( n>=1
7d4a0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b   );.      if( z[
7d4b0 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20  n]==':' ){.     
7d4c0 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72     /* A modifier
7d4d0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c   of the form (+|
7d4e0 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61  -)HH:MM:SS.FFF a
7d4f0 64 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74  dds (or subtract
7d500 73 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  s) the.        *
7d510 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62  * specified numb
7d520 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e  er of hours, min
7d530 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61  utes, seconds, a
7d540 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65  nd fractional se
7d550 63 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a  conds.        **
7d560 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54   to the time.  T
7d570 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65  he ".FFF" may be
7d580 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22   omitted.  The "
7d590 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a  :SS.FFF" may be.
7d5a0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74          ** omitt
7d5b0 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ed..        */. 
7d5c0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
7d5d0 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20  r *z2 = z;.     
7d5e0 20 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a     DateTime tx;.
7d5f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
7d600 69 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20  int64 day;.     
7d610 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
7d620 73 64 69 67 69 74 28 2a 7a 32 29 20 29 20 7a 32  sdigit(*z2) ) z2
7d630 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  ++;.        mems
7d640 65 74 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f  et(&tx, 0, sizeo
7d650 66 28 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20  f(tx));.        
7d660 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28  if( parseHhMmSs(
7d670 7a 32 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b  z2, &tx) ) break
7d680 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
7d690 65 4a 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20  eJD(&tx);.      
7d6a0 20 20 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30    tx.iJD -= 4320
7d6b0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61  0000;.        da
7d6c0 79 20 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30  y = tx.iJD/86400
7d6d0 30 30 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e  000;.        tx.
7d6e0 69 4a 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30  iJD -= day*86400
7d6f0 30 30 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  000;.        if(
7d700 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e   z[0]=='-' ) tx.
7d710 69 4a 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20  iJD = -tx.iJD;. 
7d720 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
7d730 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65  (p);.        cle
7d740 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
7d750 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
7d760 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20  += tx.iJD;.     
7d770 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
7d780 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
7d790 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a  }.      z += n;.
7d7a0 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
7d7b0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20  ite3Isspace(*z) 
7d7c0 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d  ) z++;.      n =
7d7d0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
7d7e0 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  (z);.      if( n
7d7f0 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65  >10 || n<3 ) bre
7d800 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  ak;.      if( z[
7d810 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e  n-1]=='s' ){ z[n
7d820 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a  -1] = 0; n--; }.
7d830 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
7d840 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30  p);.      rc = 0
7d850 3b 0a 20 20 20 20 20 20 72 52 6f 75 6e 64 65 72  ;.      rRounder
7d860 20 3d 20 72 3c 30 20 3f 20 2d 30 2e 35 20 3a 20   = r<0 ? -0.5 : 
7d870 2b 30 2e 35 3b 0a 20 20 20 20 20 20 69 66 28 20  +0.5;.      if( 
7d880 6e 3d 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a  n==3 && strcmp(z
7d890 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20  ,"day")==0 ){.  
7d8a0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
7d8b0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
7d8c0 72 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72  r*86400000.0 + r
7d8d0 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20  Rounder);.      
7d8e0 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26  }else if( n==4 &
7d8f0 26 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72  & strcmp(z,"hour
7d900 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7d910 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
7d920 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36  te3_int64)(r*(86
7d930 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b  400000.0/24.0) +
7d940 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20   rRounder);.    
7d950 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36    }else if( n==6
7d960 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69   && strcmp(z,"mi
7d970 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  nute")==0 ){.   
7d980 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
7d990 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
7d9a0 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34  *(86400000.0/(24
7d9b0 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75  .0*60.0)) + rRou
7d9c0 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
7d9d0 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73  se if( n==6 && s
7d9e0 74 72 63 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22  trcmp(z,"second"
7d9f0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
7da00 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
7da10 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34  e3_int64)(r*(864
7da20 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30  00000.0/(24.0*60
7da30 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75  .0*60.0)) + rRou
7da40 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
7da50 73 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73  se if( n==5 && s
7da60 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29  trcmp(z,"month")
7da70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
7da80 6e 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20  nt x, y;.       
7da90 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
7daa0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d  p);.        p->M
7dab0 20 2b 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20   += (int)r;.    
7dac0 20 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f      x = p->M>0 ?
7dad0 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28   (p->M-1)/12 : (
7dae0 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20  p->M-12)/12;.   
7daf0 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a       p->Y += x;.
7db00 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20          p->M -= 
7db10 78 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d  x*12;.        p-
7db20 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20  >validJD = 0;.  
7db30 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
7db40 70 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20  p);.        y = 
7db50 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20  (int)r;.        
7db60 69 66 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20  if( y!=r ){.    
7db70 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
7db80 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
7db90 28 72 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34  (r - y)*30.0*864
7dba0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64  00000.0 + rRound
7dbb0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  er);.        }. 
7dbc0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
7dbd0 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c  ==4 && strcmp(z,
7dbe0 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20  "year")==0 ){.  
7dbf0 20 20 20 20 20 20 69 6e 74 20 79 20 3d 20 28 69        int y = (i
7dc00 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 63 6f  nt)r;.        co
7dc10 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b  mputeYMD_HMS(p);
7dc20 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d  .        p->Y +=
7dc30 20 79 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76   y;.        p->v
7dc40 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20  alidJD = 0;.    
7dc50 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
7dc60 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 21  ;.        if( y!
7dc70 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =r ){.          
7dc80 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
7dc90 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 79  e3_int64)((r - y
7dca0 29 2a 33 36 35 2e 30 2a 38 36 34 30 30 30 30 30  )*365.0*86400000
7dcb0 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  .0 + rRounder);.
7dcc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
7dcd0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
7dce0 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 1;.      }. 
7dcf0 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d       clearYMD_HM
7dd00 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62  S_TZ(p);.      b
7dd10 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
7dd20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
7dd30 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
7dd40 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
7dd50 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20  ../*.** Process 
7dd60 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72  time function ar
7dd70 67 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30  guments.  argv[0
7dd80 5d 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65  ] is a date-time
7dd90 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b   stamp..** argv[
7dda0 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67  1] and following
7ddb0 20 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20   are modifiers. 
7ddc0 20 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20   Parse them all 
7ddd0 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65  and write.** the
7dde0 20 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20   resulting time 
7ddf0 69 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d  into the DateTim
7de00 65 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20  e structure p.  
7de10 52 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73  Return 0.** on s
7de20 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20  uccess and 1 if 
7de30 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72  there are any er
7de40 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rors..**.** If t
7de50 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61  here are zero pa
7de60 72 61 6d 65 74 65 72 73 20 28 69 66 20 65 76 65  rameters (if eve
7de70 6e 20 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64  n argv[0] is und
7de80 65 66 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20  efined).** then 
7de90 61 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74  assume a default
7dea0 20 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20   value of "now" 
7deb0 66 6f 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a  for argv[0]..*/.
7dec0 73 74 61 74 69 63 20 69 6e 74 20 69 73 44 61 74  static int isDat
7ded0 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  e(.  sqlite3_con
7dee0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
7def0 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73    int argc, .  s
7df00 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
7df10 72 67 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65  rgv, .  DateTime
7df20 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *p.){.  int i;.
7df30 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
7df40 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20   char *z;.  int 
7df50 65 54 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28  eType;.  memset(
7df60 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29  p, 0, sizeof(*p)
7df70 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30  );.  if( argc==0
7df80 20 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54   ){.    setDateT
7df90 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e  imeToCurrent(con
7dfa0 74 65 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73  text, p);.  }els
7dfb0 65 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73  e if( (eType = s
7dfc0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
7dfd0 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c  e(argv[0]))==SQL
7dfe0 49 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20  ITE_FLOAT.      
7dff0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
7e000 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e  eType==SQLITE_IN
7e010 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e  TEGER ){.    p->
7e020 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  iJD = (sqlite3_i
7e030 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61  nt64)(sqlite3_va
7e040 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
7e050 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b  0])*86400000.0 +
7e060 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61   0.5);.    p->va
7e070 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c  lidJD = 1;.  }el
7e080 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  se{.    z = sqli
7e090 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
7e0a0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[0]);.    if(
7e0b0 20 21 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65   !z || parseDate
7e0c0 4f 72 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20  OrTime(context, 
7e0d0 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a  (char*)z, p) ){.
7e0e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
7e0f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
7e100 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b  i=1; i<argc; i++
7e110 29 7b 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20  ){.    if( (z = 
7e120 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
7e130 78 74 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20  xt(argv[i]))==0 
7e140 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72  || parseModifier
7e150 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b  ((char*)z, p) ){
7e160 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
7e170 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
7e180 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
7e190 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
7e1a0 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
7e1b0 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 64 61  t the various da
7e1c0 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63  te and time func
7e1d0 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69  tions.** of SQLi
7e1e0 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20  te..*/../*.**   
7e1f0 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45   julianday( TIME
7e200 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
7e210 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
7e220 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  urn the julian d
7e230 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ay number of the
7e240 20 64 61 74 65 20 73 70 65 63 69 66 69 65 64 20   date specified 
7e250 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73  in the arguments
7e260 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
7e270 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20  juliandayFunc(. 
7e280 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
7e290 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
7e2a0 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
7e2b0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
7e2c0 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20  .  DateTime x;. 
7e2d0 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74   if( isDate(cont
7e2e0 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  ext, argc, argv,
7e2f0 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63   &x)==0 ){.    c
7e300 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20  omputeJD(&x);.  
7e310 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
7e320 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c  _double(context,
7e330 20 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e   x.iJD/86400000.
7e340 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  0);.  }.}../*.**
7e350 20 20 20 20 64 61 74 65 74 69 6d 65 28 20 54 49      datetime( TI
7e360 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
7e370 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
7e380 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44  eturn YYYY-MM-DD
7e390 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61   HH:MM:SS.*/.sta
7e3a0 74 69 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d  tic void datetim
7e3b0 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
7e3c0 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7e3d0 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
7e3e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7e3f0 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54  *argv.){.  DateT
7e400 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44  ime x;.  if( isD
7e410 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  ate(context, arg
7e420 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20  c, argv, &x)==0 
7e430 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  ){.    char zBuf
7e440 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75  [100];.    compu
7e450 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20  teYMD_HMS(&x);. 
7e460 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
7e470 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29  ntf(sizeof(zBuf)
7e480 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30  , zBuf, "%04d-%0
7e490 32 64 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32  2d-%02d %02d:%02
7e4a0 64 3a 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20  d:%02d",.       
7e4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 78 2e                x.
7e4c0 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68  Y, x.M, x.D, x.h
7e4d0 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73  , x.m, (int)(x.s
7e4e0 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ));.    sqlite3_
7e4f0 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
7e500 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53  ext, zBuf, -1, S
7e510 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
7e520 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20  ;.  }.}../*.**  
7e530 20 20 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49    time( TIMESTRI
7e540 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e  NG, MOD, MOD, ..
7e550 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  .).**.** Return 
7e560 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74  HH:MM:SS.*/.stat
7e570 69 63 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63  ic void timeFunc
7e580 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7e590 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7e5a0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
7e5b0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
7e5c0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
7e5d0 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63  ;.  if( isDate(c
7e5e0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
7e5f0 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20  gv, &x)==0 ){.  
7e600 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
7e610 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53  ;.    computeHMS
7e620 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (&x);.    sqlite
7e630 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
7e640 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22  f(zBuf), zBuf, "
7e650 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c  %02d:%02d:%02d",
7e660 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29   x.h, x.m, (int)
7e670 78 2e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  x.s);.    sqlite
7e680 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
7e690 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c  ntext, zBuf, -1,
7e6a0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
7e6b0 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  T);.  }.}../*.**
7e6c0 20 20 20 20 64 61 74 65 28 20 54 49 4d 45 53 54      date( TIMEST
7e6d0 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
7e6e0 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
7e6f0 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a  n YYYY-MM-DD.*/.
7e700 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 65  static void date
7e710 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
7e720 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
7e730 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
7e740 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
7e750 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69  argv.){.  DateTi
7e760 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61  me x;.  if( isDa
7e770 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  te(context, argc
7e780 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29  , argv, &x)==0 )
7e790 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b  {.    char zBuf[
7e7a0 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74  100];.    comput
7e7b0 65 59 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71  eYMD(&x);.    sq
7e7c0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
7e7d0 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
7e7e0 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30  f, "%04d-%02d-%0
7e7f0 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78  2d", x.Y, x.M, x
7e800 2e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .D);.    sqlite3
7e810 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
7e820 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20  text, zBuf, -1, 
7e830 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
7e840 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
7e850 20 20 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52     strftime( FOR
7e860 4d 41 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c  MAT, TIMESTRING,
7e870 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a   MOD, MOD, ...).
7e880 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73  **.** Return a s
7e890 74 72 69 6e 67 20 64 65 73 63 72 69 62 65 64 20  tring described 
7e8a0 62 79 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76  by FORMAT.  Conv
7e8b0 65 72 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f  ersions as follo
7e8c0 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20  ws:.**.**   %d  
7e8d0 64 61 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20  day of month.** 
7e8e0 20 20 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f    %f  ** fractio
7e8f0 6e 61 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e  nal seconds  SS.
7e900 53 53 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75  SSS.**   %H  hou
7e910 72 20 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20  r 00-24.**   %j 
7e920 20 64 61 79 20 6f 66 20 79 65 61 72 20 30 30 30   day of year 000
7e930 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a  -366.**   %J  **
7e940 20 4a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   Julian day numb
7e950 65 72 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74  er.**   %m  mont
7e960 68 20 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20  h 01-12.**   %M 
7e970 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a   minute 00-59.**
7e980 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73     %s  seconds s
7e990 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a  ince 1970-01-01.
7e9a0 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73  **   %S  seconds
7e9b0 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20   00-59.**   %w  
7e9c0 64 61 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20  day of week 0-6 
7e9d0 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20   sunday==0.**   
7e9e0 25 57 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72  %W  week of year
7e9f0 20 30 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20   00-53.**   %Y  
7ea00 79 65 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a  year 0000-9999.*
7ea10 2a 20 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61  *   %%  %.*/.sta
7ea20 74 69 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d  tic void strftim
7ea30 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
7ea40 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7ea50 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
7ea60 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7ea70 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54  *argv.){.  DateT
7ea80 69 6d 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a  ime x;.  u64 n;.
7ea90 20 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20    size_t i,j;.  
7eaa0 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74  char *z;.  sqlit
7eab0 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20  e3 *db;.  const 
7eac0 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28 63 6f  char *zFmt = (co
7ead0 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
7eae0 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
7eaf0 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 20 7a 42  v[0]);.  char zB
7eb00 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 28 20 7a  uf[100];.  if( z
7eb10 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 61 74 65  Fmt==0 || isDate
7eb20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2d 31  (context, argc-1
7eb30 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20 29 20  , argv+1, &x) ) 
7eb40 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 73  return;.  db = s
7eb50 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
7eb60 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
7eb70 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d  );.  for(i=0, n=
7eb80 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c  1; zFmt[i]; i++,
7eb90 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a   n++){.    if( z
7eba0 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 7b 0a 20  Fmt[i]=='%' ){. 
7ebb0 20 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d       switch( zFm
7ebc0 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20  t[i+1] ){.      
7ebd0 20 20 63 61 73 65 20 27 64 27 3a 0a 20 20 20 20    case 'd':.    
7ebe0 20 20 20 20 63 61 73 65 20 27 48 27 3a 0a 20 20      case 'H':.  
7ebf0 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a 0a        case 'm':.
7ec00 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d 27          case 'M'
7ec10 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
7ec20 53 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  S':.        case
7ec30 20 27 57 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'W':.          
7ec40 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 2f  n++;.          /
7ec50 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20  * fall thru */. 
7ec60 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a         case 'w':
7ec70 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 25  .        case '%
7ec80 27 3a 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ':.          bre
7ec90 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
7eca0 20 27 66 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'f':.          
7ecb0 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20  n += 8;.        
7ecc0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
7ecd0 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 20 20 20   case 'j':.     
7ece0 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20       n += 3;.   
7ecf0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
7ed00 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 0a        case 'Y':.
7ed10 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38            n += 8
7ed20 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
7ed30 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
7ed40 27 73 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  's':.        cas
7ed50 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 20 20 20  e 'J':.         
7ed60 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 20 20 20   n += 50;.      
7ed70 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
7ed80 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
7ed90 20 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f        return;  /
7eda0 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e  * ERROR.  return
7edb0 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20   a NULL */.     
7edc0 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20   }.      i++;.  
7edd0 20 20 7d 0a 20 20 7d 0a 20 20 74 65 73 74 63 61    }.  }.  testca
7ede0 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42  se( n==sizeof(zB
7edf0 75 66 29 2d 31 20 29 3b 0a 20 20 74 65 73 74 63  uf)-1 );.  testc
7ee00 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a  ase( n==sizeof(z
7ee10 42 75 66 29 20 29 3b 0a 20 20 74 65 73 74 63 61  Buf) );.  testca
7ee20 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e  se( n==(u64)db->
7ee30 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
7ee40 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b  MIT_LENGTH]+1 );
7ee50 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d  .  testcase( n==
7ee60 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b  (u64)db->aLimit[
7ee70 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
7ee80 47 54 48 5d 20 29 3b 0a 20 20 69 66 28 20 6e 3c  GTH] );.  if( n<
7ee90 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 7b 0a  sizeof(zBuf) ){.
7eea0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
7eeb0 7d 65 6c 73 65 20 69 66 28 20 6e 3e 28 75 36 34  }else if( n>(u64
7eec0 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  )db->aLimit[SQLI
7eed0 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
7eee0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
7eef0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
7ef00 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  big(context);.  
7ef10 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73    return;.  }els
7ef20 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  e{.    z = sqlit
7ef30 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
7ef40 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 20 20 69  , (int)n);.    i
7ef50 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( z==0 ){.     
7ef60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
7ef70 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74  error_nomem(cont
7ef80 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ext);.      retu
7ef90 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
7efa0 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20  computeJD(&x);. 
7efb0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
7efc0 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30  &x);.  for(i=j=0
7efd0 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b  ; zFmt[i]; i++){
7efe0 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d  .    if( zFmt[i]
7eff0 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a  !='%' ){.      z
7f000 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b  [j++] = zFmt[i];
7f010 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
7f020 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69    i++;.      swi
7f030 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a  tch( zFmt[i] ){.
7f040 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27          case 'd'
7f050 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
7f060 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30  ntf(3, &z[j],"%0
7f070 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20  2d",x.D); j+=2; 
7f080 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
7f090 61 73 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20  ase 'f': {.     
7f0a0 20 20 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20       double s = 
7f0b0 78 2e 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69  x.s;.          i
7f0c0 66 28 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20  f( s>59.999 ) s 
7f0d0 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20  = 59.999;.      
7f0e0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
7f0f0 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(7, &z[j],"%
7f100 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20  06.3f", s);.    
7f110 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74        j += sqlit
7f120 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d  e3Strlen30(&z[j]
7f130 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
7f140 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
7f150 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 20        case 'H': 
7f160 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
7f170 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
7f180 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72  ",x.h); j+=2; br
7f190 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
7f1a0 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74  e 'W': /* Fall t
7f1b0 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63  hru */.        c
7f1c0 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20  ase 'j': {.     
7f1d0 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20       int nDay;  
7f1e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
7f1f0 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e  mber of days sin
7f200 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79 65  ce 1st day of ye
7f210 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ar */.          
7f220 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a  DateTime y = x;.
7f230 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69            y.vali
7f240 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dJD = 0;.       
7f250 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20     y.M = 1;.    
7f260 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20        y.D = 1;. 
7f270 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65           compute
7f280 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20  JD(&y);.        
7f290 20 20 6e 44 61 79 20 3d 20 28 69 6e 74 29 28 28    nDay = (int)((
7f2a0 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30  x.iJD-y.iJD+4320
7f2b0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b  0000)/86400000);
7f2c0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
7f2d0 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20  Fmt[i]=='W' ){. 
7f2e0 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 77             int w
7f2f0 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79  d;   /* 0=Monday
7f300 2c 20 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e  , 1=Tuesday, ...
7f310 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20   6=Sunday */.   
7f320 20 20 20 20 20 20 20 20 20 77 64 20 3d 20 28 69           wd = (i
7f330 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30  nt)(((x.iJD+4320
7f340 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 25  0000)/86400000)%
7f350 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  7);.            
7f360 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7f370 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  (3, &z[j],"%02d"
7f380 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b  ,(nDay+7-wd)/7);
7f390 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b  .            j +
7f3a0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 2;.          }
7f3b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
7f3c0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7f3d0 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33  tf(4, &z[j],"%03
7f3e0 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20  d",nDay+1);.    
7f3f0 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a          j += 3;.
7f400 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
7f410 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7f420 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
7f430 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20  ase 'J': {.     
7f440 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
7f450 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c  rintf(20, &z[j],
7f460 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 2f 38 36  "%.16g",x.iJD/86
7f470 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 20  400000.0);.     
7f480 20 20 20 20 20 6a 2b 3d 73 71 6c 69 74 65 33 53       j+=sqlite3S
7f490 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a  trlen30(&z[j]);.
7f4a0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7f4b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7f4c0 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71     case 'm':  sq
7f4d0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
7f4e0 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78  , &z[j],"%02d",x
7f4f0 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b  .M); j+=2; break
7f500 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
7f510 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  M':  sqlite3_snp
7f520 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
7f530 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32  %02d",x.m); j+=2
7f540 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
7f550 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20   case 's': {.   
7f560 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
7f570 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d  nprintf(30,&z[j]
7f580 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 20 20 20 20  ,"%lld",.       
7f590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f5a0 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a 44 2f      (i64)(x.iJD/
7f5b0 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a  1000 - 21086676*
7f5c0 28 69 36 34 29 31 30 30 30 30 29 29 3b 0a 20 20  (i64)10000));.  
7f5d0 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c          j += sql
7f5e0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b  ite3Strlen30(&z[
7f5f0 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  j]);.          b
7f600 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
7f610 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27          case 'S'
7f620 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
7f630 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32  ntf(3,&z[j],"%02
7f640 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b  d",(int)x.s); j+
7f650 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
7f660 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20     case 'w': {. 
7f670 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
7f680 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 4a 44  = (char)(((x.iJD
7f690 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30  +129600000)/8640
7f6a0 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 30 27  0000) % 7) + '0'
7f6b0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
7f6c0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
7f6d0 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b       case 'Y': {
7f6e0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
7f6f0 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a  e3_snprintf(5,&z
7f700 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b  [j],"%04d",x.Y);
7f710 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65   j+=sqlite3Strle
7f720 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20  n30(&z[j]);.    
7f730 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7f740 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
7f750 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d  efault:   z[j++]
7f760 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20   = '%'; break;. 
7f770 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
7f780 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73  .  z[j] = 0;.  s
7f790 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
7f7a0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d  xt(context, z, -
7f7b0 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
7f7c0 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66           z==zBuf
7f7d0 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49   ? SQLITE_TRANSI
7f7e0 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e  ENT : SQLITE_DYN
7f7f0 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  AMIC);.}../*.** 
7f800 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a  current_time().*
7f810 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
7f820 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  on returns the s
7f830 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d  ame value as tim
7f840 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61  e('now')..*/.sta
7f850 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75  tic void ctimeFu
7f860 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
7f870 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
7f880 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
7f890 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7f8a0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55  *NotUsed2.){.  U
7f8b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
7f8c0 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
7f8d0 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28  d2);.  timeFunc(
7f8e0 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a  context, 0, 0);.
7f8f0 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74  }../*.** current
7f900 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68  _date().**.** Th
7f910 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
7f920 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c  rns the same val
7f930 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27  ue as date('now'
7f940 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
7f950 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73  d cdateFunc(.  s
7f960 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7f970 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
7f980 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
7f990 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
7f9a0 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  d2.){.  UNUSED_P
7f9b0 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
7f9c0 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
7f9d0 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74  dateFunc(context
7f9e0 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , 0, 0);.}../*.*
7f9f0 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74  * current_timest
7fa00 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73  amp().**.** This
7fa10 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
7fa20 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  s the same value
7fa30 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f   as datetime('no
7fa40 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  w')..*/.static v
7fa50 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75  oid ctimestampFu
7fa60 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
7fa70 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
7fa80 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
7fa90 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7faa0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55  *NotUsed2.){.  U
7fab0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
7fac0 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
7fad0 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46  d2);.  datetimeF
7fae0 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20  unc(context, 0, 
7faf0 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  0);.}.#endif /* 
7fb00 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
7fb10 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55  OMIT_DATETIME_FU
7fb20 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20  NCS) */..#ifdef 
7fb30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45  SQLITE_OMIT_DATE
7fb40 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a  TIME_FUNCS./*.**
7fb50 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   If the library 
7fb60 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f  is compiled to o
7fb70 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61  mit the full-sca
7fb80 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  le date and time
7fb90 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f  .** handling (to
7fba0 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62   get a smaller b
7fbb0 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c  inary), the foll
7fbc0 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65  owing minimal ve
7fbd0 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  rsion.** of the 
7fbe0 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  functions curren
7fbf0 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e  t_time(), curren
7fc00 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 75 72  t_date() and cur
7fc10 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29  rent_timestamp()
7fc20 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64  .** are included
7fc30 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69   instead. This i
7fc40 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c  s to support col
7fc50 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  umn declarations
7fc60 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65   that.** include
7fc70 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e   "DEFAULT CURREN
7fc80 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a  T_TIME" etc..**.
7fc90 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
7fca0 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72   uses the C-libr
7fcb0 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69  ary functions ti
7fcc0 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a  me(), gmtime().*
7fcd0 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29  * and strftime()
7fce0 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72  . The format str
7fcf0 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73  ing to pass to s
7fd00 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75 70  trftime() is sup
7fd10 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20  plied.** as the 
7fd20 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68  user-data for th
7fd30 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  e function..*/.s
7fd40 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 72 65  tatic void curre
7fd50 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71  ntTimeFunc(.  sq
7fd60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
7fd70 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
7fd80 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
7fd90 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
7fda0 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72  time_t t;.  char
7fdb0 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61   *zFormat = (cha
7fdc0 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72  r *)sqlite3_user
7fdd0 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a  _data(context);.
7fde0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
7fdf0 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68   double rT;.  ch
7fe00 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20  ar zBuf[20];..  
7fe10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
7fe20 28 61 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44  (argc);.  UNUSED
7fe30 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 76 29  _PARAMETER(argv)
7fe40 3b 0a 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  ;..  db = sqlite
7fe50 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
7fe60 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
7fe70 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
7fe80 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26  Time(db->pVfs, &
7fe90 72 54 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  rT);.#ifndef SQL
7fea0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
7feb0 47 5f 50 4f 49 4e 54 0a 20 20 74 20 3d 20 38 36  G_POINT.  t = 86
7fec0 34 30 30 2e 30 2a 28 72 54 20 2d 20 32 34 34 30  400.0*(rT - 2440
7fed0 35 38 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23 65  587.5) + 0.5;.#e
7fee0 6c 73 65 0a 20 20 2f 2a 20 77 69 74 68 6f 75 74  lse.  /* without
7fef0 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
7ff00 73 75 70 70 6f 72 74 2c 20 72 54 20 77 69 6c 6c  support, rT will
7ff10 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61   have.  ** alrea
7ff20 64 79 20 6c 6f 73 74 20 66 72 61 63 74 69 6f 6e  dy lost fraction
7ff30 61 6c 20 64 61 79 20 70 72 65 63 69 73 69 6f 6e  al day precision
7ff40 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d 20 38 36 34  ..  */.  t = 864
7ff50 30 30 20 2a 20 28 72 54 20 2d 20 32 34 34 30 35  00 * (rT - 24405
7ff60 38 37 29 20 2d 20 34 33 32 30 30 3b 0a 23 65 6e  87) - 43200;.#en
7ff70 64 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f  dif.#ifdef HAVE_
7ff80 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20  GMTIME_R.  {.   
7ff90 20 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b   struct tm sNow;
7ffa0 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74  .    gmtime_r(&t
7ffb0 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74  , &sNow);.    st
7ffc0 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c  rftime(zBuf, 20,
7ffd0 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29   zFormat, &sNow)
7ffe0 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a  ;.  }.#else.  {.
7fff0 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70      struct tm *p
80000 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  Tm;.    sqlite3_
80010 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69  mutex_enter(sqli
80020 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
80030 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
80040 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20  C_MASTER));.    
80050 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29  pTm = gmtime(&t)
80060 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a  ;.    strftime(z
80070 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74  Buf, 20, zFormat
80080 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69  , pTm);.    sqli
80090 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
800a0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
800b0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
800c0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
800d0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71    }.#endif..  sq
800e0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
800f0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
80100 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
80110 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66  SIENT);.}.#endif
80120 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
80130 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
80140 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76   all of the abov
80150 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73  e C functions as
80160 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e   SQL.** function
80170 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20  s.  This should 
80180 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74  be the only rout
80190 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ine in this file
801a0 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61   with.** externa
801b0 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51  l linkage..*/.SQ
801c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
801d0 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
801e0 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f  rDateTimeFunctio
801f0 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ns(void){.  stat
80200 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75  ic SQLITE_WSD Fu
80210 6e 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46  ncDef aDateTimeF
80220 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64  uncs[] = {.#ifnd
80230 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
80240 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20  ATETIME_FUNCS.  
80250 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61    FUNCTION(julia
80260 6e 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c  nday,        -1,
80270 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79   0, 0, julianday
80280 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43  Func ),.    FUNC
80290 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20  TION(date,      
802a0 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c         -1, 0, 0,
802b0 20 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29   dateFunc      )
802c0 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74  ,.    FUNCTION(t
802d0 69 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ime,            
802e0 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46   -1, 0, 0, timeF
802f0 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20  unc      ),.    
80300 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d  FUNCTION(datetim
80310 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30  e,         -1, 0
80320 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e  , 0, datetimeFun
80330 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49  c  ),.    FUNCTI
80340 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20  ON(strftime,    
80350 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73       -1, 0, 0, s
80360 74 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a  trftimeFunc  ),.
80370 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72      FUNCTION(cur
80380 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20  rent_time,      
80390 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75  0, 0, 0, ctimeFu
803a0 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55  nc     ),.    FU
803b0 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74  NCTION(current_t
803c0 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20  imestamp, 0, 0, 
803d0 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e  0, ctimestampFun
803e0 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  c),.    FUNCTION
803f0 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20  (current_date,  
80400 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61      0, 0, 0, cda
80410 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65  teFunc     ),.#e
80420 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43  lse.    STR_FUNC
80430 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d  TION(current_tim
80440 65 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25  e,      0, "%H:%
80450 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20  M:%S",          
80460 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75  0, currentTimeFu
80470 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e  nc),.    STR_FUN
80480 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69  CTION(current_ti
80490 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d  mestamp, 0, "%Y-
804a0 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20  %m-%d",         
804b0 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46   0, currentTimeF
804c0 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55  unc),.    STR_FU
804d0 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64  NCTION(current_d
804e0 61 74 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59  ate,      0, "%Y
804f0 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22  -%m-%d %H:%M:%S"
80500 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65  , 0, currentTime
80510 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20  Func),.#endif.  
80520 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75  };.  int i;.  Fu
80530 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
80540 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44   = &GLOBAL(FuncD
80550 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47  efHash, sqlite3G
80560 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b  lobalFunctions);
80570 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e  .  FuncDef *aFun
80580 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47  c = (FuncDef*)&G
80590 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61  LOBAL(FuncDef, a
805a0 44 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a  DateTimeFuncs);.
805b0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72  .  for(i=0; i<Ar
805c0 72 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d  raySize(aDateTim
805d0 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20  eFuncs); i++){. 
805e0 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65     sqlite3FuncDe
805f0 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26  fInsert(pHash, &
80600 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d  aFunc[i]);.  }.}
80610 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
80620 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20  * End of date.c 
80630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80660 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
80670 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e  * Begin file os.
80680 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
80690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
806a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
806b0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76  /./*.** 2005 Nov
806c0 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54  ember 29.**.** T
806d0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
806e0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
806f0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
80700 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
80710 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
80720 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
80730 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
80740 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
80750 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
80760 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
80770 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
80780 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
80790 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
807a0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
807b0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
807c0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
807d0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
807e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
807f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
80830 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
80840 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61  tains OS interfa
80850 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ce code that is 
80860 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a  common to all.**
80870 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a   architectures..
80880 2a 2f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49  */.#define _SQLI
80890 54 45 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e 64 65  TE_OS_C_ 1.#unde
808a0 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a  f _SQLITE_OS_C_.
808b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  ./*.** The defau
808c0 6c 74 20 53 51 4c 69 74 65 20 73 71 6c 69 74 65  lt SQLite sqlite
808d0 33 5f 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61  3_vfs implementa
808e0 74 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c  tions do not all
808f0 6f 63 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  ocate.** memory 
80900 28 61 63 74 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e  (actually, os_un
80910 69 78 2e 63 20 61 6c 6c 6f 63 61 74 65 73 20 61  ix.c allocates a
80920 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66   small amount of
80930 20 6d 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20   memory.** from 
80940 77 69 74 68 69 6e 20 4f 73 4f 70 65 6e 28 29 29  within OsOpen())
80950 2c 20 62 75 74 20 73 6f 6d 65 20 74 68 69 72 64  , but some third
80960 2d 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74  -party implement
80970 61 74 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53  ations may..** S
80980 6f 20 77 65 20 74 65 73 74 20 74 68 65 20 65 66  o we test the ef
80990 66 65 63 74 73 20 6f 66 20 61 20 6d 61 6c 6c 6f  fects of a mallo
809a0 63 28 29 20 66 61 69 6c 69 6e 67 20 61 6e 64 20  c() failing and 
809b0 74 68 65 20 73 71 6c 69 74 65 33 4f 73 58 58 58  the sqlite3OsXXX
809c0 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72  ().** function r
809d0 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f  eturning SQLITE_
809e0 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e  IOERR_NOMEM usin
809f0 67 20 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c  g the DO_OS_MALL
80a00 4f 43 5f 54 45 53 54 20 6d 61 63 72 6f 2e 0a 2a  OC_TEST macro..*
80a10 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
80a20 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  ng functions are
80a30 20 69 6e 73 74 72 75 6d 65 6e 74 65 64 20 66 6f   instrumented fo
80a40 72 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  r malloc() failu
80a50 72 65 20 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0a  re .** testing:.
80a60 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  **.**     sqlite
80a70 33 4f 73 4f 70 65 6e 28 29 0a 2a 2a 20 20 20 20  3OsOpen().**    
80a80 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 29   sqlite3OsRead()
80a90 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f  .**     sqlite3O
80aa0 73 57 72 69 74 65 28 29 0a 2a 2a 20 20 20 20 20  sWrite().**     
80ab0 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 0a  sqlite3OsSync().
80ac0 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
80ad0 4c 6f 63 6b 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66  Lock().**.*/.#if
80ae0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
80af0 54 45 53 54 29 20 26 26 20 28 53 51 4c 49 54 45  TEST) && (SQLITE
80b00 5f 4f 53 5f 57 49 4e 3d 3d 30 29 0a 20 20 23 64  _OS_WIN==0).  #d
80b10 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c  efine DO_OS_MALL
80b20 4f 43 5f 54 45 53 54 28 78 29 20 69 66 20 28 21  OC_TEST(x) if (!
80b30 78 20 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d  x || !sqlite3IsM
80b40 65 6d 4a 6f 75 72 6e 61 6c 28 78 29 29 20 7b 20  emJournal(x)) { 
80b50 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a      \.    void *
80b60 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69  pTstAlloc = sqli
80b70 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20  te3Malloc(10);  
80b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80b90 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
80ba0 20 69 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29   if (!pTstAlloc)
80bb0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
80bc0 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20  OERR_NOMEM;     
80bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80be0 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    \.    sqlite3_
80bf0 66 72 65 65 28 70 54 73 74 41 6c 6c 6f 63 29 3b  free(pTstAlloc);
80c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80c20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23           \.  }.#
80c30 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44  else.  #define D
80c40 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
80c50 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (x).#endif../*.*
80c60 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
80c70 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e  routines are con
80c80 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72  venience wrapper
80c90 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73  s around methods
80ca0 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74  .** of the sqlit
80cb0 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20  e3_file object. 
80cc0 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20   This is mostly 
80cd0 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 20 73  just syntactic s
80ce0 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20  ugar. All.** of 
80cf0 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f  this would be co
80d00 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74  mpletely automat
80d10 69 63 20 69 66 20 53 51 4c 69 74 65 20 77 65 72  ic if SQLite wer
80d20 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a  e coded using.**
80d30 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20   C++ instead of 
80d40 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a  plain old C..*/.
80d50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
80d60 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73  nt sqlite3OsClos
80d70 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
80d80 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pId){.  int rc =
80d90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
80da0 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20  ( pId->pMethods 
80db0 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d  ){.    rc = pId-
80dc0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73  >pMethods->xClos
80dd0 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d  e(pId);.    pId-
80de0 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20  >pMethods = 0;. 
80df0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
80e00 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
80e10 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65   int sqlite3OsRe
80e20 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ad(sqlite3_file 
80e30 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c  *id, void *pBuf,
80e40 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66   int amt, i64 of
80e50 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d  fset){.  DO_OS_M
80e60 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a  ALLOC_TEST(id);.
80e70 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
80e80 74 68 6f 64 73 2d 3e 78 52 65 61 64 28 69 64 2c  thods->xRead(id,
80e90 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73   pBuf, amt, offs
80ea0 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  et);.}.SQLITE_PR
80eb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
80ec0 33 4f 73 57 72 69 74 65 28 73 71 6c 69 74 65 33  3OsWrite(sqlite3
80ed0 5f 66 69 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74  _file *id, const
80ee0 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74   void *pBuf, int
80ef0 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74   amt, i64 offset
80f00 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f  ){.  DO_OS_MALLO
80f10 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65  C_TEST(id);.  re
80f20 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
80f30 73 2d 3e 78 57 72 69 74 65 28 69 64 2c 20 70 42  s->xWrite(id, pB
80f40 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29  uf, amt, offset)
80f50 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
80f60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
80f70 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33  Truncate(sqlite3
80f80 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 73  _file *id, i64 s
80f90 69 7a 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69  ize){.  return i
80fa0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 54 72  d->pMethods->xTr
80fb0 75 6e 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29  uncate(id, size)
80fc0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
80fd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
80fe0 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c  Sync(sqlite3_fil
80ff0 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73  e *id, int flags
81000 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f  ){.  DO_OS_MALLO
81010 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65  C_TEST(id);.  re
81020 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
81030 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61  s->xSync(id, fla
81040 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  gs);.}.SQLITE_PR
81050 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
81060 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69  3OsFileSize(sqli
81070 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36  te3_file *id, i6
81080 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f  4 *pSize){.  DO_
81090 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69  OS_MALLOC_TEST(i
810a0 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  d);.  return id-
810b0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65  >pMethods->xFile
810c0 53 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b  Size(id, pSize);
810d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
810e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c  E int sqlite3OsL
810f0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
81100 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79   *id, int lockTy
81110 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  pe){.  DO_OS_MAL
81120 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20  LOC_TEST(id);.  
81130 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
81140 6f 64 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c  ods->xLock(id, l
81150 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49  ockType);.}.SQLI
81160 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
81170 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73  qlite3OsUnlock(s
81180 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
81190 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a   int lockType){.
811a0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
811b0 74 68 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69  thods->xUnlock(i
811c0 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a  d, lockType);.}.
811d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
811e0 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63  nt sqlite3OsChec
811f0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
81200 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
81210 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20  int *pResOut){. 
81220 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
81230 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e  ST(id);.  return
81240 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
81250 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
81260 6b 28 69 64 2c 20 70 52 65 73 4f 75 74 29 3b 0a  k(id, pResOut);.
81270 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
81280 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69   int sqlite3OsFi
81290 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
812a0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
812b0 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
812c0 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
812d0 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e  ethods->xFileCon
812e0 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72  trol(id, op, pAr
812f0 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  g);.}.SQLITE_PRI
81300 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
81310 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  OsSectorSize(sql
81320 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a  ite3_file *id){.
81330 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53    int (*xSectorS
81340 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ize)(sqlite3_fil
81350 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f  e*) = id->pMetho
81360 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b  ds->xSectorSize;
81370 0a 20 20 72 65 74 75 72 6e 20 28 78 53 65 63 74  .  return (xSect
81380 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72  orSize ? xSector
81390 53 69 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54  Size(id) : SQLIT
813a0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
813b0 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49 54 45  _SIZE);.}.SQLITE
813c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
813d0 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72  ite3OsDeviceChar
813e0 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69  acteristics(sqli
813f0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
81400 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
81410 68 6f 64 73 2d 3e 78 44 65 76 69 63 65 43 68 61  hods->xDeviceCha
81420 72 61 63 74 65 72 69 73 74 69 63 73 28 69 64 29  racteristics(id)
81430 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  ;.}../*.** The n
81440 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75  ext group of rou
81450 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e  tines are conven
81460 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 61  ience wrappers a
81470 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53  round the.** VFS
81480 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c   methods..*/.SQL
81490 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
814a0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20  sqlite3OsOpen(. 
814b0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
814c0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
814d0 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c  r *zPath, .  sql
814e0 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65  ite3_file *pFile
814f0 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  , .  int flags, 
81500 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75  .  int *pFlagsOu
81510 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  t.){.  int rc;. 
81520 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
81530 53 54 28 30 29 3b 0a 20 20 2f 2a 20 30 78 37 66  ST(0);.  /* 0x7f
81540 31 66 20 69 73 20 61 20 6d 61 73 6b 20 6f 66 20  1f is a mask of 
81550 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61  SQLITE_OPEN_ fla
81560 67 73 20 74 68 61 74 20 61 72 65 20 76 61 6c 69  gs that are vali
81570 64 20 74 6f 20 62 65 20 70 61 73 73 65 64 0a 20  d to be passed. 
81580 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68   ** down into th
81590 65 20 56 46 53 20 6c 61 79 65 72 2e 20 20 53 6f  e VFS layer.  So
815a0 6d 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20  me SQLITE_OPEN_ 
815b0 66 6c 61 67 73 20 28 66 6f 72 20 65 78 61 6d 70  flags (for examp
815c0 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  le,.  ** SQLITE_
815d0 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 6f  OPEN_FULLMUTEX o
815e0 72 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48  r SQLITE_OPEN_SH
815f0 41 52 45 44 43 41 43 48 45 29 20 61 72 65 20 62  AREDCACHE) are b
81600 6c 6f 63 6b 65 64 20 62 65 66 6f 72 65 0a 20 20  locked before.  
81610 2a 2a 20 72 65 61 63 68 69 6e 67 20 74 68 65 20  ** reaching the 
81620 56 46 53 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 70  VFS. */.  rc = p
81630 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c  Vfs->xOpen(pVfs,
81640 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66   zPath, pFile, f
81650 6c 61 67 73 20 26 20 30 78 37 66 31 66 2c 20 70  lags & 0x7f1f, p
81660 46 6c 61 67 73 4f 75 74 29 3b 0a 20 20 61 73 73  FlagsOut);.  ass
81670 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
81680 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d 65  OK || pFile->pMe
81690 74 68 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 72 65  thods==0 );.  re
816a0 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
816b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
816c0 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71  lite3OsDelete(sq
816d0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
816e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
816f0 74 68 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29  th, int dirSync)
81700 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
81710 3e 78 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a  >xDelete(pVfs, z
81720 50 61 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a  Path, dirSync);.
81730 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
81740 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63   int sqlite3OsAc
81750 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f  cess(.  sqlite3_
81760 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f  vfs *pVfs, .  co
81770 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c  nst char *zPath,
81780 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a   .  int flags, .
81790 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29    int *pResOut.)
817a0 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  {.  DO_OS_MALLOC
817b0 5f 54 45 53 54 28 30 29 3b 0a 20 20 72 65 74 75  _TEST(0);.  retu
817c0 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73  rn pVfs->xAccess
817d0 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c  (pVfs, zPath, fl
817e0 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d  ags, pResOut);.}
817f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
81800 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c  int sqlite3OsFul
81810 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c  lPathname(.  sql
81820 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
81830 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
81840 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61  Path, .  int nPa
81850 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a  thOut, .  char *
81860 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 7a 50  zPathOut.){.  zP
81870 61 74 68 4f 75 74 5b 30 5d 20 3d 20 30 3b 0a 20  athOut[0] = 0;. 
81880 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46   return pVfs->xF
81890 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73  ullPathname(pVfs
818a0 2c 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75  , zPath, nPathOu
818b0 74 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a  t, zPathOut);.}.
818c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
818d0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
818e0 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ON.SQLITE_PRIVAT
818f0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f  E void *sqlite3O
81900 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  sDlOpen(sqlite3_
81910 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
81920 20 63 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20   char *zPath){. 
81930 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44   return pVfs->xD
81940 6c 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74  lOpen(pVfs, zPat
81950 68 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  h);.}.SQLITE_PRI
81960 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
81970 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74  3OsDlError(sqlit
81980 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
81990 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a  t nByte, char *z
819a0 42 75 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d  BufOut){.  pVfs-
819b0 3e 78 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20  >xDlError(pVfs, 
819c0 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b  nByte, zBufOut);
819d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
819e0 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33  E void (*sqlite3
819f0 4f 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f  OsDlSym(sqlite3_
81a00 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20  vfs *pVfs, void 
81a10 2a 70 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68  *pHdle, const ch
81a20 61 72 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29  ar *zSym))(void)
81a30 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
81a40 3e 78 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48  >xDlSym(pVfs, pH
81a50 64 6c 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51  dle, zSym);.}.SQ
81a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
81a70 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f  d sqlite3OsDlClo
81a80 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  se(sqlite3_vfs *
81a90 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e  pVfs, void *pHan
81aa0 64 6c 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44  dle){.  pVfs->xD
81ab0 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61  lClose(pVfs, pHa
81ac0 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  ndle);.}.#endif 
81ad0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  /* SQLITE_OMIT_L
81ae0 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f  OAD_EXTENSION */
81af0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
81b00 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e  int sqlite3OsRan
81b10 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f  domness(sqlite3_
81b20 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
81b30 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66  Byte, char *zBuf
81b40 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  Out){.  return p
81b50 56 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73  Vfs->xRandomness
81b60 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42  (pVfs, nByte, zB
81b70 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45  ufOut);.}.SQLITE
81b80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
81b90 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69  ite3OsSleep(sqli
81ba0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
81bb0 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65  nt nMicro){.  re
81bc0 74 75 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65  turn pVfs->xSlee
81bd0 70 28 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b  p(pVfs, nMicro);
81be0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
81bf0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
81c00 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
81c10 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f  e3_vfs *pVfs, do
81c20 75 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b  uble *pTimeOut){
81c30 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e  .  return pVfs->
81c40 78 43 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66  xCurrentTime(pVf
81c50 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a  s, pTimeOut);.}.
81c60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
81c70 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  int sqlite3OsOpe
81c80 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74  nMalloc(.  sqlit
81c90 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20  e3_vfs *pVfs, . 
81ca0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
81cb0 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66  le, .  sqlite3_f
81cc0 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20  ile **ppFile, . 
81cd0 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e   int flags,.  in
81ce0 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a  t *pOutFlags.){.
81cf0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
81d00 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74  E_NOMEM;.  sqlit
81d10 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a  e3_file *pFile;.
81d20 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74    pFile = (sqlit
81d30 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65  e3_file *)sqlite
81d40 33 4d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a  3Malloc(pVfs->sz
81d50 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70  OsFile);.  if( p
81d60 46 69 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d  File ){.    rc =
81d70 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
81d80 56 66 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c  Vfs, zFile, pFil
81d90 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c  e, flags, pOutFl
81da0 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ags);.    if( rc
81db0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
81dc0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
81dd0 65 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65  e(pFile);.    }e
81de0 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69  lse{.      *ppFi
81df0 6c 65 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20  le = pFile;.    
81e00 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
81e10 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  c;.}.SQLITE_PRIV
81e20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
81e30 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74  sCloseFree(sqlit
81e40 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b  e3_file *pFile){
81e50 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
81e60 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28  TE_OK;.  assert(
81e70 20 70 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d   pFile );.  rc =
81e80 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
81e90 70 46 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65  pFile);.  sqlite
81ea0 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20  3_free(pFile);. 
81eb0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
81ec0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
81ed0 6f 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 20  on is a wrapper 
81ee0 61 72 6f 75 6e 64 20 74 68 65 20 4f 53 20 73 70  around the OS sp
81ef0 65 63 69 66 69 63 20 69 6d 70 6c 65 6d 65 6e 74  ecific implement
81f00 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69  ation of.** sqli
81f10 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 54  te3_os_init(). T
81f20 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68  he purpose of th
81f30 65 20 77 72 61 70 70 65 72 20 69 73 20 74 6f 20  e wrapper is to 
81f40 70 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61  provide the.** a
81f50 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61  bility to simula
81f60 74 65 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  te a malloc fail
81f70 75 72 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65  ure, so that the
81f80 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 61 6e 0a   handling of an.
81f90 2a 2a 20 65 72 72 6f 72 20 69 6e 20 73 71 6c 69  ** error in sqli
81fa0 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 62 79  te3_os_init() by
81fb0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72   the upper layer
81fc0 73 20 63 61 6e 20 62 65 20 74 65 73 74 65 64 2e  s can be tested.
81fd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
81fe0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
81ff0 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 76 6f  Init(void){.  vo
82000 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f  id *p = sqlite3_
82010 6d 61 6c 6c 6f 63 28 31 30 29 3b 0a 20 20 69 66  malloc(10);.  if
82020 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
82030 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
82040 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
82050 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
82060 33 5f 6f 73 5f 69 6e 69 74 28 29 3b 0a 7d 0a 0a  3_os_init();.}..
82070 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f  /*.** The list o
82080 66 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64  f all registered
82090 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74   VFS implementat
820a0 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ions..*/.static 
820b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51  sqlite3_vfs * SQ
820c0 4c 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74  LITE_WSD vfsList
820d0 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66   = 0;.#define vf
820e0 73 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c  sList GLOBAL(sql
820f0 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c  ite3_vfs *, vfsL
82100 69 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61  ist)../*.** Loca
82110 74 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65  te a VFS by name
82120 2e 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73  .  If no name is
82130 20 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72   given, simply r
82140 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72  eturn the.** fir
82150 73 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69  st VFS on the li
82160 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  st..*/.SQLITE_AP
82170 49 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73  I sqlite3_vfs *s
82180 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28  qlite3_vfs_find(
82190 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73  const char *zVfs
821a0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  ){.  sqlite3_vfs
821b0 20 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20   *pVfs = 0;.#if 
821c0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
821d0 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E.  sqlite3_mute
821e0 78 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66  x *mutex;.#endif
821f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
82200 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
82210 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
82220 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
82230 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
82240 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   0;.#endif.#if S
82250 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
82260 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  .  mutex = sqlit
82270 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
82280 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
82290 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66  _MASTER);.#endif
822a0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
822b0 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20  _enter(mutex);. 
822c0 20 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c   for(pVfs = vfsL
822d0 69 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d  ist; pVfs; pVfs=
822e0 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pVfs->pNext){.  
822f0 20 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20    if( zVfs==0 ) 
82300 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73  break;.    if( s
82310 74 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73  trcmp(zVfs, pVfs
82320 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72  ->zName)==0 ) br
82330 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  eak;.  }.  sqlit
82340 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
82350 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
82360 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  pVfs;.}../*.** U
82370 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d  nlink a VFS from
82380 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
82390 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
823a0 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65  vfsUnlink(sqlite
823b0 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20  3_vfs *pVfs){.  
823c0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
823d0 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
823e0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
823f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
82400 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69  _MASTER)) );.  i
82410 66 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20  f( pVfs==0 ){.  
82420 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20    /* No-op */.  
82430 7d 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73  }else if( vfsLis
82440 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76  t==pVfs ){.    v
82450 66 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70  fsList = pVfs->p
82460 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Next;.  }else if
82470 28 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20  ( vfsList ){.   
82480 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20   sqlite3_vfs *p 
82490 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77  = vfsList;.    w
824a0 68 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26  hile( p->pNext &
824b0 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73  & p->pNext!=pVfs
824c0 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d   ){.      p = p-
824d0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
824e0 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d    if( p->pNext==
824f0 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d  pVfs ){.      p-
82500 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70  >pNext = pVfs->p
82510 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Next;.    }.  }.
82520 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
82530 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65  r a VFS with the
82540 20 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20   system.  It is 
82550 68 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69  harmless to regi
82560 73 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a  ster the same.**
82570 20 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69   VFS multiple ti
82580 6d 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46  mes.  The new VF
82590 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65  S becomes the de
825a0 66 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c  fault if makeDfl
825b0 74 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f  t is.** true..*/
825c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
825d0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
825e0 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73  ster(sqlite3_vfs
825f0 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65   *pVfs, int make
82600 44 66 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  Dflt){.  sqlite3
82610 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
82620 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
82630 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
82640 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
82650 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b  e3_initialize();
82660 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
82670 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20  rn rc;.#endif.  
82680 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
82690 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
826a0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
826b0 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33  STER);.  sqlite3
826c0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
826d0 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b  ex);.  vfsUnlink
826e0 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61  (pVfs);.  if( ma
826f0 6b 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73  keDflt || vfsLis
82700 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73  t==0 ){.    pVfs
82710 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73  ->pNext = vfsLis
82720 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d  t;.    vfsList =
82730 20 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a   pVfs;.  }else{.
82740 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20      pVfs->pNext 
82750 3d 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74  = vfsList->pNext
82760 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70  ;.    vfsList->p
82770 4e 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d  Next = pVfs;.  }
82780 0a 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73  .  assert(vfsLis
82790 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  t);.  sqlite3_mu
827a0 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
827b0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
827c0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  E_OK;.}../*.** U
827d0 6e 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20  nregister a VFS 
827e0 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f  so that it is no
827f0 20 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62   longer accessib
82800 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
82810 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66  I int sqlite3_vf
82820 73 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c  s_unregister(sql
82830 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b  ite3_vfs *pVfs){
82840 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
82850 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33  ADSAFE.  sqlite3
82860 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
82870 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
82880 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
82890 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23  TATIC_MASTER);.#
828a0 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
828b0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
828c0 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28  x);.  vfsUnlink(
828d0 70 56 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33  pVfs);.  sqlite3
828e0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
828f0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
82900 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a  LITE_OK;.}../***
82910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
82920 6f 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  of os.c ********
82930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
82960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
82970 6e 20 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a  n file fault.c *
82980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
829a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
829b0 2a 20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a  * 2008 Jan 22.**
829c0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
829d0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
829e0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
829f0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
82a00 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
82a10 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
82a20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
82a30 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
82a40 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
82a50 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
82a60 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
82a70 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
82a80 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
82a90 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
82aa0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
82ab0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
82ac0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
82ad0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
82ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
82b20 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
82b30 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 73 75  tains code to su
82b40 70 70 6f 72 74 20 74 68 65 20 63 6f 6e 63 65 70  pport the concep
82b50 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 20 0a 2a  t of "benign" .*
82b60 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  * malloc failure
82b70 73 20 28 77 68 65 6e 20 74 68 65 20 78 4d 61 6c  s (when the xMal
82b80 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 6c 6c 6f  loc() or xReallo
82b90 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  c() method of th
82ba0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d  e.** sqlite3_mem
82bb0 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 75  _methods structu
82bc0 72 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f  re fails to allo
82bd0 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20  cate a block of 
82be0 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 65  memory.** and re
82bf0 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a  turns 0). .**.**
82c00 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 61 69   Most malloc fai
82c10 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65  lures are non-be
82c20 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 68 65 79  nign. After they
82c30 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 65 0a 2a   occur, SQLite.*
82c40 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 65 20 63  * abandons the c
82c50 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e  urrent operation
82c60 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20   and returns an 
82c70 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61  error code (usua
82c80 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f  lly.** SQLITE_NO
82c90 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 73 65 72  MEM) to the user
82ca0 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 74  . However, somet
82cb0 69 6d 65 73 20 61 20 66 61 75 6c 74 20 69 73 20  imes a fault is 
82cc0 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 0a  not necessarily.
82cd0 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20 65 78  ** fatal. For ex
82ce0 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61 6c 6c  ample, if a mall
82cf0 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 72  oc fails while r
82d00 65 73 69 7a 69 6e 67 20 61 20 68 61 73 68 20 74  esizing a hash t
82d10 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a 20 69  able, this .** i
82d20 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63  s completely rec
82d30 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c 79 20  overable simply 
82d40 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e 67 20  by not carrying 
82d50 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65 2e 20  out the resize. 
82d60 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 74 61 62  The .** hash tab
82d70 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65  le will continue
82d80 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72   to function nor
82d90 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c  mally.  So a mal
82da0 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20  loc failure .** 
82db0 64 75 72 69 6e 67 20 61 20 68 61 73 68 20 74 61  during a hash ta
82dc0 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20 61 20  ble resize is a 
82dd0 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f  benign fault..*/
82de0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
82df0 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
82e00 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61  EST../*.** Globa
82e10 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a  l variables..*/.
82e20 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
82e30 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
82e40 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f   BenignMallocHoo
82e50 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54  ks;.static SQLIT
82e60 45 5f 57 53 44 20 73 74 72 75 63 74 20 42 65 6e  E_WSD struct Ben
82e70 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b  ignMallocHooks {
82e80 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67  .  void (*xBenig
82e90 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 3b 0a 20  nBegin)(void);. 
82ea0 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45   void (*xBenignE
82eb0 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73 71 6c  nd)(void);.} sql
82ec0 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c  ite3Hooks = { 0,
82ed0 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77   0 };../* The "w
82ee0 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77  sdHooks" macro w
82ef0 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74  ill resolve to t
82f00 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 42  he appropriate B
82f10 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
82f20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
82f30 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74  If writable stat
82f40 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70  ic data is unsup
82f50 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61  ported on the ta
82f60 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65  rget,.** we have
82f70 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73   to locate the s
82f80 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 72  tate vector at r
82f90 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65  un-time.  In the
82fa0 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20   more common.** 
82fb0 63 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61  case where writa
82fc0 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20  ble static data 
82fd0 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73  is supported, ws
82fe0 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65 72  dHooks can refer
82ff0 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20   directly.** to 
83000 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f 6f 6b  the "sqlite3Hook
83010 73 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20  s" state vector 
83020 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a  declared above..
83030 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
83040 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69  _OMIT_WSD.# defi
83050 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 20  ne wsdHooksInit 
83060 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  \.  BenignMalloc
83070 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c 4f 42  Hooks *x = &GLOB
83080 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  AL(BenignMallocH
83090 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f 6f 6b  ooks,sqlite3Hook
830a0 73 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48  s).# define wsdH
830b0 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 65 0a  ooks x[0].#else.
830c0 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b  # define wsdHook
830d0 73 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77  sInit.# define w
830e0 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65 33 48  sdHooks sqlite3H
830f0 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  ooks.#endif.../*
83100 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f  .** Register hoo
83110 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20  ks to call when 
83120 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
83130 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a  gnMalloc() and.*
83140 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  * sqlite3EndBeni
83150 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20 63  gnMalloc() are c
83160 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 69 76  alled, respectiv
83170 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ely..*/.SQLITE_P
83180 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
83190 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  te3BenignMallocH
831a0 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28 2a 78  ooks(.  void (*x
831b0 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69  BenignBegin)(voi
831c0 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 42 65  d),.  void (*xBe
831d0 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 0a 29  nignEnd)(void).)
831e0 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74  {.  wsdHooksInit
831f0 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65  ;.  wsdHooks.xBe
83200 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 42 65 6e  nignBegin = xBen
83210 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73 64 48  ignBegin;.  wsdH
83220 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20  ooks.xBenignEnd 
83230 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a  = xBenignEnd;.}.
83240 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73 71 6c  ./*.** This (sql
83250 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
83260 6c 6f 63 28 29 29 20 69 73 20 63 61 6c 6c 65 64  loc()) is called
83270 20 62 79 20 53 51 4c 69 74 65 20 63 6f 64 65 20   by SQLite code 
83280 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  to indicate that
83290 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 6d  .** subsequent m
832a0 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61  alloc failures a
832b0 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c  re benign. A cal
832c0 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 6e 64 42  l to sqlite3EndB
832d0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a  enignMalloc().**
832e0 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
832f0 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f  subsequent mallo
83300 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e  c failures are n
83310 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51  on-benign..*/.SQ
83320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
83330 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65  d sqlite3BeginBe
83340 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29  nignMalloc(void)
83350 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74  {.  wsdHooksInit
83360 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73  ;.  if( wsdHooks
83370 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 29 7b  .xBenignBegin ){
83380 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42  .    wsdHooks.xB
83390 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a 20 20  enignBegin();.  
833a0 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  }.}.SQLITE_PRIVA
833b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
833c0 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76  ndBenignMalloc(v
833d0 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73  oid){.  wsdHooks
833e0 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48  Init;.  if( wsdH
833f0 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20  ooks.xBenignEnd 
83400 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e  ){.    wsdHooks.
83410 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 20  xBenignEnd();.  
83420 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20 2f 2a  }.}..#endif   /*
83430 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
83440 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
83450 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  T */../*********
83460 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75  ***** End of fau
83470 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  lt.c ***********
83480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
834a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
834b0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
834c0 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem0.c ********
834d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
834e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
834f0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
83500 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a   October 28.**.*
83510 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
83520 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
83530 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
83540 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
83550 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
83560 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
83570 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
83580 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
83590 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
835a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
835b0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
835c0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
835d0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
835e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
835f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
83600 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
83610 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
83620 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
83630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
83670 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
83680 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f  ins a no-op memo
83690 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72  ry allocation dr
836a0 69 76 65 72 73 20 66 6f 72 20 75 73 65 20 77 68  ivers for use wh
836b0 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52  en.** SQLITE_ZER
836c0 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69  O_MALLOC is defi
836d0 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61  ned.  The alloca
836e0 74 69 6f 6e 20 64 72 69 76 65 72 73 20 69 6d 70  tion drivers imp
836f0 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 72 65  lemented.** here
83700 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 53   always fail.  S
83710 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f  QLite will not o
83720 70 65 72 61 74 65 20 77 69 74 68 20 74 68 65 73  perate with thes
83730 65 20 64 72 69 76 65 72 73 2e 20 20 54 68 65 73  e drivers.  Thes
83740 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79 20  e.** are merely 
83750 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 20 52  placeholders.  R
83760 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75 73 74  eal drivers must
83770 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20   be substituted 
83780 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  using.** sqlite3
83790 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f 72 65  _config() before
837a0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70 65   SQLite will ope
837b0 72 61 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  rate..*/../*.** 
837c0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
837d0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
837e0 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 61  ator is the defa
837f0 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75  ult.  It is.** u
83800 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65  sed when no othe
83810 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  r memory allocat
83820 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20  or is specified 
83830 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69  using compile-ti
83840 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f  me.** macros..*/
83850 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a  .#ifdef SQLITE_Z
83860 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a  ERO_MALLOC../*.*
83870 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73  * No-op versions
83880 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61   of all memory a
83890 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e  llocation routin
838a0 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  es.*/.static voi
838b0 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  d *sqlite3MemMal
838c0 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 20  loc(int nByte){ 
838d0 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74  return 0; }.stat
838e0 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  ic void sqlite3M
838f0 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72  emFree(void *pPr
83900 69 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a  ior){ return; }.
83910 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c  static void *sql
83920 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76  ite3MemRealloc(v
83930 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74  oid *pPrior, int
83940 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20   nByte){ return 
83950 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  0; }.static int 
83960 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76  sqlite3MemSize(v
83970 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65  oid *pPrior){ re
83980 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63  turn 0; }.static
83990 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52   int sqlite3MemR
839a0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20 72  oundup(int n){ r
839b0 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69  eturn n; }.stati
839c0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
839d0 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73  Init(void *NotUs
839e0 65 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  ed){ return SQLI
839f0 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20  TE_OK; }.static 
83a00 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53  void sqlite3MemS
83a10 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f  hutdown(void *No
83a20 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 3b 20  tUsed){ return; 
83a30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
83a40 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c  utine is the onl
83a50 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69  y routine in thi
83a60 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65  s file with exte
83a70 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a  rnal linkage..**
83a80 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65  .** Populate the
83a90 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72   low-level memor
83aa0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e  y allocation fun
83ab0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69  ction pointers i
83ac0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62  n.** sqlite3Glob
83ad0 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20  alConfig.m with 
83ae0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  pointers to the 
83af0 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73  routines in this
83b00 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   file..*/.SQLITE
83b10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
83b20 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75  lite3MemSetDefau
83b30 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  lt(void){.  stat
83b40 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
83b50 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66  _mem_methods def
83b60 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a  aultMethods = {.
83b70 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d       sqlite3MemM
83b80 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69  alloc,.     sqli
83b90 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20  te3MemFree,.    
83ba0 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c   sqlite3MemReall
83bb0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
83bc0 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71  MemSize,.     sq
83bd0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c  lite3MemRoundup,
83be0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
83bf0 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74  Init,.     sqlit
83c00 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20  e3MemShutdown,. 
83c10 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c      0.  };.  sql
83c20 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49  ite3_config(SQLI
83c30 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43  TE_CONFIG_MALLOC
83c40 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  , &defaultMethod
83c50 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  s);.}..#endif /*
83c60 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c   SQLITE_ZERO_MAL
83c70 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  LOC */../*******
83c80 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
83c90 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em0.c **********
83ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83cc0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
83cd0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
83ce0 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a  le mem1.c ******
83cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83d10 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
83d20 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a  07 August 14.**.
83d30 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
83d40 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
83d50 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
83d60 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
83d70 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
83d80 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
83d90 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
83da0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
83db0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
83dc0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
83dd0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
83de0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
83df0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
83e00 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
83e10 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
83e20 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
83e30 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
83e40 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
83e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
83e90 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
83ea0 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d  ains low-level m
83eb0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
83ec0 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65   drivers for whe
83ed0 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  n.** SQLite will
83ee0 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72   use the standar
83ef0 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c  d C-library mall
83f00 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20  oc/realloc/free 
83f10 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20  interface.** to 
83f20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72  obtain the memor
83f30 79 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a  y it needs..**.*
83f40 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
83f50 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ains implementat
83f60 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d  ions of the low-
83f70 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
83f80 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69  ocation.** routi
83f90 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69 6e  nes specified in
83fa0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d   the sqlite3_mem
83fb0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e  _methods object.
83fc0 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .*/../*.** This 
83fd0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
83fe0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
83ff0 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20  is the default. 
84000 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77   It is.** used w
84010 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d  hen no other mem
84020 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ory allocator is
84030 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67   specified using
84040 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a   compile-time.**
84050 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64   macros..*/.#ifd
84060 65 66 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ef SQLITE_SYSTEM
84070 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c  _MALLOC../*.** L
84080 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75  ike malloc(), bu
84090 74 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73  t remember the s
840a0 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
840b0 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20 74 68 61 74  ation.** so that
840c0 20 77 65 20 63 61 6e 20 66 69 6e 64 20 69 74 20   we can find it 
840d0 6c 61 74 65 72 20 75 73 69 6e 67 20 73 71 6c 69  later using sqli
840e0 74 65 33 4d 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a  te3MemSize()..**
840f0 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77  .** For this low
84100 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20  -level routine, 
84110 77 65 20 61 72 65 20 67 75 61 72 61 6e 74 65 65  we are guarantee
84120 64 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62  d that nByte>0 b
84130 65 63 61 75 73 65 0a 2a 2a 20 63 61 73 65 73 20  ecause.** cases 
84140 6f 66 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c  of nByte<=0 will
84150 20 62 65 20 69 6e 74 65 72 63 65 70 74 65 64 20   be intercepted 
84160 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68 20 62  and dealt with b
84170 79 20 68 69 67 68 65 72 20 6c 65 76 65 6c 0a 2a  y higher level.*
84180 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73  * routines..*/.s
84190 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69  tatic void *sqli
841a0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74  te3MemMalloc(int
841b0 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74   nByte){.  sqlit
841c0 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61  e3_int64 *p;.  a
841d0 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29  ssert( nByte>0 )
841e0 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e  ;.  nByte = ROUN
841f0 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d  D8(nByte);.  p =
84200 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2b 38   malloc( nByte+8
84210 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   );.  if( p ){. 
84220 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b     p[0] = nByte;
84230 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20  .    p++;.  }.  
84240 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 70  return (void *)p
84250 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20  ;.}../*.** Like 
84260 66 72 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73  free() but works
84270 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73   for allocations
84280 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
84290 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28  qlite3MemMalloc(
842a0 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d  ).** or sqlite3M
842b0 65 6d 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a  emRealloc()..**.
842c0 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d  ** For this low-
842d0 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77  level routine, w
842e0 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74  e already know t
842f0 68 61 74 20 70 50 72 69 6f 72 21 3d 30 20 73 69  hat pPrior!=0 si
84300 6e 63 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65  nce.** cases whe
84310 72 65 20 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c  re pPrior==0 wil
84320 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65  l have been inte
84330 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74  cepted and dealt
84340 20 77 69 74 68 0a 2a 2a 20 62 79 20 68 69 67 68   with.** by high
84350 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  er-level routine
84360 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
84370 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65  d sqlite3MemFree
84380 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a  (void *pPrior){.
84390 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
843a0 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  *p = (sqlite3_in
843b0 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61  t64*)pPrior;.  a
843c0 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30  ssert( pPrior!=0
843d0 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 66 72 65   );.  p--;.  fre
843e0 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  e(p);.}../*.** L
843f0 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20  ike realloc().  
84400 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61  Resize an alloca
84410 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20  tion previously 
84420 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
84430 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f   sqlite3MemMallo
84440 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  c()..**.** For t
84450 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e  his low-level in
84460 74 65 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77  terface, we know
84470 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d 30 2e   that pPrior!=0.
84480 20 20 43 61 73 65 73 20 77 68 65 72 65 0a 2a 2a    Cases where.**
84490 20 70 50 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65   pPrior==0 while
844a0 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72   have been inter
844b0 63 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72  cepted by higher
844c0 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 20 61  -level routine a
844d0 6e 64 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64  nd.** redirected
844e0 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69   to xMalloc.  Si
844f0 6d 69 6c 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77  milarly, we know
84500 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65   that nByte>0 be
84510 63 61 75 73 65 73 0a 2a 2a 20 63 61 73 65 73 20  causes.** cases 
84520 77 68 65 72 65 20 6e 42 79 74 65 3c 3d 30 20 77  where nByte<=0 w
84530 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ill have been in
84540 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69 67  tercepted by hig
84550 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75  her-level.** rou
84560 74 69 6e 65 73 20 61 6e 64 20 72 65 64 69 72 65  tines and redire
84570 63 74 65 64 20 74 6f 20 78 46 72 65 65 2e 0a 2a  cted to xFree..*
84580 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73  /.static void *s
84590 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
845a0 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69  (void *pPrior, i
845b0 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c  nt nByte){.  sql
845c0 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20  ite3_int64 *p = 
845d0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
845e0 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74  pPrior;.  assert
845f0 28 20 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e  ( pPrior!=0 && n
84600 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74  Byte>0 );.  nByt
84610 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65  e = ROUND8(nByte
84620 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65  );.  p = (sqlite
84630 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b  3_int64*)pPrior;
84640 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 72 65  .  p--;.  p = re
84650 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 74 65 2b 38  alloc(p, nByte+8
84660 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   );.  if( p ){. 
84670 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b     p[0] = nByte;
84680 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20  .    p++;.  }.  
84690 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b  return (void*)p;
846a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74  .}../*.** Report
846b0 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73   the allocated s
846c0 69 7a 65 20 6f 66 20 61 20 70 72 69 6f 72 20 72  ize of a prior r
846d0 65 74 75 72 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c  eturn from xMall
846e0 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 52 65 61 6c  oc().** or xReal
846f0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
84700 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53   int sqlite3MemS
84710 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ize(void *pPrior
84720 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
84730 36 34 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72  64 *p;.  if( pPr
84740 69 6f 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ior==0 ) return 
84750 30 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65  0;.  p = (sqlite
84760 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b  3_int64*)pPrior;
84770 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e  .  p--;.  return
84780 20 28 69 6e 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f   (int)p[0];.}../
84790 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20  *.** Round up a 
847a0 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20  request size to 
847b0 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61  the next valid a
847c0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a  llocation size..
847d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
847e0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28  lite3MemRoundup(
847f0 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e  int n){.  return
84800 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f   ROUND8(n);.}../
84810 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
84820 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  this module..*/.
84830 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
84840 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a  e3MemInit(void *
84850 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53  NotUsed){.  UNUS
84860 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
84870 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
84880 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
84890 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
848a0 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f   this module..*/
848b0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
848c0 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28  ite3MemShutdown(
848d0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
848e0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
848f0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
84900 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
84910 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
84920 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
84930 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
84940 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b  th external link
84950 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c  age..**.** Popul
84960 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65  ate the low-leve
84970 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
84980 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  ion function poi
84990 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69  nters in.** sqli
849a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
849b0 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  m with pointers 
849c0 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  to the routines 
849d0 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f  in this file..*/
849e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
849f0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53  void sqlite3MemS
84a00 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b  etDefault(void){
84a10 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
84a20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
84a30 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f  ods defaultMetho
84a40 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69  ds = {.     sqli
84a50 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20  te3MemMalloc,.  
84a60 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65     sqlite3MemFre
84a70 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  e,.     sqlite3M
84a80 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  emRealloc,.     
84a90 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a  sqlite3MemSize,.
84aa0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52       sqlite3MemR
84ab0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c  oundup,.     sql
84ac0 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20  ite3MemInit,.   
84ad0 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74    sqlite3MemShut
84ae0 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d  down,.     0.  }
84af0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ;.  sqlite3_conf
84b00 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
84b10 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c  _MALLOC, &defaul
84b20 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65  tMethods);.}..#e
84b30 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53  ndif /* SQLITE_S
84b40 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a  YSTEM_MALLOC */.
84b50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
84b60 20 45 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a   End of mem1.c *
84b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
84ba0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
84bb0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32   Begin file mem2
84bc0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
84bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
84bf0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
84c00 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 15.**.** The 
84c10 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
84c20 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
84c30 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
84c40 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
84c50 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
84c60 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
84c70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
84c80 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
84c90 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
84ca0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
84cb0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
84cc0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
84cd0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
84ce0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
84cf0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
84d00 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
84d10 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
84d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84d60 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
84d70 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f  file contains lo
84d80 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
84d90 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72  llocation driver
84da0 73 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51  s for when.** SQ
84db0 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68  Lite will use th
84dc0 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62  e standard C-lib
84dd0 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c  rary malloc/real
84de0 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61  loc/free interfa
84df0 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20  ce.** to obtain 
84e00 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65  the memory it ne
84e10 65 64 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67  eds while adding
84e20 20 6c 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f   lots of additio
84e30 6e 61 6c 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a  nal debugging.**
84e40 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
84e50 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  each allocation 
84e60 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70  in order to help
84e70 20 64 65 74 65 63 74 20 61 6e 64 20 66 69 78 20   detect and fix 
84e80 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65 61 6b 73 20  memory.** leaks 
84e90 61 6e 64 20 6d 65 6d 6f 72 79 20 75 73 61 67 65  and memory usage
84ea0 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54   errors..**.** T
84eb0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
84ec0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
84ed0 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76  s of the low-lev
84ee0 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
84ef0 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
84f00 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
84f10 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  e sqlite3_mem_me
84f20 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f  thods object..*/
84f30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
84f40 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
84f50 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
84f60 75 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65  used only if the
84f70 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45  .** SQLITE_MEMDE
84f80 42 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66  BUG macro is def
84f90 69 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ined.*/.#ifdef S
84fa0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a  QLITE_MEMDEBUG..
84fb0 2f 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72  /*.** The backtr
84fc0 61 63 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  ace functionalit
84fd0 79 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  y is only availa
84fe0 62 6c 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a  ble with GLIBC.*
84ff0 2f 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43  /.#ifdef __GLIBC
85000 5f 5f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20  __.  extern int 
85010 62 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a  backtrace(void**
85020 2c 69 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20  ,int);.  extern 
85030 76 6f 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73  void backtrace_s
85040 79 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63  ymbols_fd(void*c
85050 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  onst*,int,int);.
85060 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62  #else.# define b
85070 61 63 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a  acktrace(A,B) 1.
85080 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61  # define backtra
85090 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c  ce_symbols_fd(A,
850a0 42 2c 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  B,C).#endif../*.
850b0 2a 2a 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61  ** Each memory a
850c0 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20  llocation looks 
850d0 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
850e0 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
850f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85100 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85110 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85120 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c  ----------.**  |
85130 20 54 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72   Title |  backtr
85140 61 63 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20  ace pointers |  
85150 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61  MemBlockHdr |  a
85160 6c 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64  llocation |  End
85170 47 75 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d  Guard |.**  ----
85180 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85190 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
851a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
851b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
851c0 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ----.**.** The a
851d0 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20  pplication code 
851e0 73 65 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e  sees only a poin
851f0 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63  ter to the alloc
85200 61 74 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a  ation.  We have.
85210 2a 2a 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72  ** to back up fr
85220 6f 6d 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  om the allocatio
85230 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e  n pointer to fin
85240 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64  d the MemBlockHd
85250 72 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c  r.  The.** MemBl
85260 6f 63 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20  ockHdr tells us 
85270 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
85280 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74  allocation and t
85290 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
852a0 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65  backtrace pointe
852b0 72 73 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c  rs.  There is al
852c0 73 6f 20 61 20 67 75 61 72 64 20 77 6f 72 64 20  so a guard word 
852d0 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
852e0 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  e.** MemBlockHdr
852f0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42  ..*/.struct MemB
85300 6c 6f 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20  lockHdr {.  i64 
85310 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  iSize;          
85320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85330 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20  /* Size of this 
85340 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20  allocation */.  
85350 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
85360 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65  dr *pNext, *pPre
85370 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69  v;  /* Linked li
85380 73 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65  st of all unfree
85390 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68  d memory */.  ch
853a0 61 72 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20  ar nBacktrace;  
853b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
853c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
853d0 61 63 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69  acktraces on thi
853e0 73 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61  s alloc */.  cha
853f0 72 20 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  r nBacktraceSlot
85400 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
85410 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61   /* Available ba
85420 63 6b 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f  cktrace slots */
85430 0a 20 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b  .  short nTitle;
85440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85450 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
85460 6f 66 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64  of title; includ
85470 65 73 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74  es '\0' */.  int
85480 20 69 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20   iForeGuard;    
85490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
854a0 20 2f 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66   /* Guard word f
854b0 6f 72 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a  or sanity */.};.
854c0 0a 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72  ./*.** Guard wor
854d0 64 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f  ds.*/.#define FO
854e0 52 45 47 55 41 52 44 20 30 78 38 30 46 35 45 31  REGUARD 0x80F5E1
854f0 35 33 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47  53.#define REARG
85500 55 41 52 44 20 30 78 45 34 36 37 36 42 35 33 0a  UARD 0xE4676B53.
85510 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  ./*.** Number of
85520 20 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63   malloc size inc
85530 72 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b  rements to track
85540 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53  ..*/.#define NCS
85550 49 5a 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a  IZE  1000../*.**
85560 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74   All of the stat
85570 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65  ic variables use
85580 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65  d by this module
85590 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a   are collected.*
855a0 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  * into a single 
855b0 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20  structure named 
855c0 22 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20  "mem".  This is 
855d0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73  to keep the.** s
855e0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  tatic variables 
855f0 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f  organized and to
85600 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63   reduce namespac
85610 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77  e pollution.** w
85620 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  hen this module 
85630 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68  is combined with
85640 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d   other in the am
85650 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  algamation..*/.s
85660 74 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20  tatic struct {. 
85670 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65   .  /*.  ** Mute
85680 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63  x to control acc
85690 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72  ess to the memor
856a0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
856b0 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73  system..  */.  s
856c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
856d0 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  tex;..  /*.  ** 
856e0 48 65 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66  Head and tail of
856f0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
85700 66 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e  f all outstandin
85710 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20  g allocations.  
85720 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  */.  struct MemB
85730 6c 6f 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b  lockHdr *pFirst;
85740 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
85750 63 6b 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20  ckHdr *pLast;.  
85760 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e  .  /*.  ** The n
85770 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20  umber of levels 
85780 6f 66 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20  of backtrace to 
85790 73 61 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f  save in new allo
857a0 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  cations..  */.  
857b0 69 6e 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a  int nBacktrace;.
857c0 20 20 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72    void (*xBacktr
857d0 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76  ace)(int, int, v
857e0 6f 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20  oid **);..  /*. 
857f0 20 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74   ** Title text t
85800 6f 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e  o insert in fron
85810 74 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a  t of each block.
85820 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c    */.  int nTitl
85830 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74  e;        /* Byt
85840 65 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20  es of zTitle to 
85850 73 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20  save.  Includes 
85860 27 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67  '\0' and padding
85870 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c   */.  char zTitl
85880 65 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20  e[100];  /* The 
85890 74 69 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20  title text */.. 
858a0 20 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65   /* .  ** sqlite
858b0 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28  3MallocDisallow(
858c0 29 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65  ) increments the
858d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74   following count
858e0 65 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  er..  ** sqlite3
858f0 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65  MallocAllow() de
85900 63 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a  crements it..  *
85910 2f 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77  /.  int disallow
85920 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f  ; /* Do not allo
85930 77 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  w memory allocat
85940 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a  ion */..  /*.  *
85950 2a 20 47 61 74 68 65 72 20 73 74 61 74 69 73 74  * Gather statist
85960 69 63 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73  ics on the sizes
85970 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   of memory alloc
85980 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c  ations..  ** nAl
85990 6c 6f 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75  loc[i] is the nu
859a0 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69  mber of allocati
859b0 6f 6e 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69  on attempts of i
859c0 2a 38 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20  *8.  ** bytes.  
859d0 69 3d 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65  i==NCSIZE is the
859e0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   number of alloc
859f0 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66  ation attempts f
85a00 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f  or.  ** sizes mo
85a10 72 65 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38  re than NCSIZE*8
85a20 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69   bytes..  */.  i
85a30 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45  nt nAlloc[NCSIZE
85a40 5d 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c  ];      /* Total
85a50 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   number of alloc
85a60 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ations */.  int 
85a70 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d  nCurrent[NCSIZE]
85a80 3b 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20  ;    /* Current 
85a90 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  number of alloca
85aa0 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d  tions */.  int m
85ab0 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d  xCurrent[NCSIZE]
85ac0 3b 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72  ;   /* Highwater
85ad0 20 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65   mark for nCurre
85ae0 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a  nt */..} mem;...
85af0 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d  /*.** Adjust mem
85b00 6f 72 79 20 75 73 61 67 65 20 73 74 61 74 69 73  ory usage statis
85b10 74 69 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tics.*/.static v
85b20 6f 69 64 20 61 64 6a 75 73 74 53 74 61 74 73 28  oid adjustStats(
85b30 69 6e 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69  int iSize, int i
85b40 6e 63 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74  ncrement){.  int
85b50 20 69 20 3d 20 52 4f 55 4e 44 38 28 69 53 69 7a   i = ROUND8(iSiz
85b60 65 29 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43  e)/8;.  if( i>NC
85b70 53 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20  SIZE-1 ){.    i 
85b80 3d 20 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20  = NCSIZE - 1;.  
85b90 7d 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e  }.  if( incremen
85ba0 74 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e  t>0 ){.    mem.n
85bb0 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20  Alloc[i]++;.    
85bc0 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b  mem.nCurrent[i]+
85bd0 2b 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e  +;.    if( mem.n
85be0 43 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d  Current[i]>mem.m
85bf0 78 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20  xCurrent[i] ){. 
85c00 20 20 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65       mem.mxCurre
85c10 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72  nt[i] = mem.nCur
85c20 72 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20  rent[i];.    }. 
85c30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e   }else{.    mem.
85c40 6e 43 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20  nCurrent[i]--;. 
85c50 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e     assert( mem.n
85c60 43 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b  Current[i]>=0 );
85c70 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  .  }.}../*.** Gi
85c80 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f  ven an allocatio
85c90 6e 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42  n, find the MemB
85ca0 6c 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74  lockHdr for that
85cb0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a   allocation..**.
85cc0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
85cd0 63 68 65 63 6b 73 20 74 68 65 20 67 75 61 72 64  checks the guard
85ce0 73 20 61 74 20 65 69 74 68 65 72 20 65 6e 64 20  s at either end 
85cf0 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
85d00 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79  n and.** if they
85d10 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69   are incorrect i
85d20 74 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74  t asserts..*/.st
85d30 61 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42  atic struct MemB
85d40 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33  lockHdr *sqlite3
85d50 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28  MemsysGetHeader(
85d60 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f  void *pAllocatio
85d70 6e 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d  n){.  struct Mem
85d80 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69  BlockHdr *p;.  i
85d90 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a  nt *pInt;.  u8 *
85da0 70 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65  pU8;.  int nRese
85db0 72 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72  rve;..  p = (str
85dc0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a  uct MemBlockHdr*
85dd0 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20  )pAllocation;.  
85de0 70 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p--;.  assert( p
85df0 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 28 69  ->iForeGuard==(i
85e00 6e 74 29 46 4f 52 45 47 55 41 52 44 20 29 3b 0a  nt)FOREGUARD );.
85e10 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55    nReserve = ROU
85e20 4e 44 38 28 70 2d 3e 69 53 69 7a 65 29 3b 0a 20  ND8(p->iSize);. 
85e30 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70 41   pInt = (int*)pA
85e40 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 55 38  llocation;.  pU8
85e50 20 3d 20 28 75 38 2a 29 70 41 6c 6c 6f 63 61 74   = (u8*)pAllocat
85e60 69 6f 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ion;.  assert( p
85e70 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a  Int[nReserve/siz
85e80 65 6f 66 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29  eof(int)]==(int)
85e90 52 45 41 52 47 55 41 52 44 20 29 3b 0a 20 20 2f  REARGUARD );.  /
85ea0 2a 20 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e  * This checks an
85eb0 79 20 6f 66 20 74 68 65 20 22 65 78 74 72 61 22  y of the "extra"
85ec0 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64   bytes allocated
85ed0 20 64 75 65 0a 20 20 2a 2a 20 74 6f 20 72 6f 75   due.  ** to rou
85ee0 6e 64 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38  nding up to an 8
85ef0 20 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74   byte boundary t
85f00 6f 20 65 6e 73 75 72 65 20 0a 20 20 2a 2a 20 74  o ensure .  ** t
85f10 68 65 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e  hey haven't been
85f20 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20   overwritten..  
85f30 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 52 65 73  */.  while( nRes
85f40 65 72 76 65 2d 2d 20 3e 20 70 2d 3e 69 53 69 7a  erve-- > p->iSiz
85f50 65 20 29 20 61 73 73 65 72 74 28 20 70 55 38 5b  e ) assert( pU8[
85f60 6e 52 65 73 65 72 76 65 5d 3d 3d 30 78 36 35 20  nReserve]==0x65 
85f70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
85f80 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
85f90 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
85fa0 65 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c  es currently all
85fb0 6f 63 61 74 65 64 20 61 74 20 61 64 64 72 65 73  ocated at addres
85fc0 73 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  s p..*/.static i
85fd0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a  nt sqlite3MemSiz
85fe0 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74  e(void *p){.  st
85ff0 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  ruct MemBlockHdr
86000 20 2a 70 48 64 72 3b 0a 20 20 69 66 28 20 21 70   *pHdr;.  if( !p
86010 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
86020 3b 0a 20 20 7d 0a 20 20 70 48 64 72 20 3d 20 73  ;.  }.  pHdr = s
86030 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
86040 65 61 64 65 72 28 70 29 3b 0a 20 20 72 65 74 75  eader(p);.  retu
86050 72 6e 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a  rn pHdr->iSize;.
86060 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
86070 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
86080 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
86090 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tem..*/.static i
860a0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69  nt sqlite3MemIni
860b0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
860c0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
860d0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
860e0 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66   assert( (sizeof
860f0 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b  (struct MemBlock
86100 48 64 72 29 26 37 29 20 3d 3d 20 30 20 29 3b 0a  Hdr)&7) == 0 );.
86110 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c    if( !sqlite3Gl
86120 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
86130 74 61 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  tat ){.    /* If
86140 20 6d 65 6d 6f 72 79 20 73 74 61 74 75 73 20 69   memory status i
86150 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20  s enabled, then 
86160 74 68 65 20 6d 61 6c 6c 6f 63 2e 63 20 77 72 61  the malloc.c wra
86170 70 70 65 72 20 77 69 6c 6c 20 61 6c 72 65 61 64  pper will alread
86180 79 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 20 74 68  y.    ** hold th
86190 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74  e STATIC_MEM mut
861a0 65 78 20 77 68 65 6e 20 74 68 65 20 72 6f 75 74  ex when the rout
861b0 69 6e 65 73 20 68 65 72 65 20 61 72 65 20 69 6e  ines here are in
861c0 76 6f 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 6d 65  voked. */.    me
861d0 6d 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  m.mutex = sqlite
861e0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
861f0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
86200 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  MEM);.  }.  retu
86210 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
86220 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c  ./*.** Deinitial
86230 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
86240 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
86250 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tem..*/.static v
86260 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68  oid sqlite3MemSh
86270 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74  utdown(void *Not
86280 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
86290 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
862a0 64 29 3b 0a 20 20 6d 65 6d 2e 6d 75 74 65 78 20  d);.  mem.mutex 
862b0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f  = 0;.}../*.** Ro
862c0 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74  und up a request
862d0 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78   size to the nex
862e0 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69  t valid allocati
862f0 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74  on size..*/.stat
86300 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
86310 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b  mRoundup(int n){
86320 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38  .  return ROUND8
86330 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  (n);.}../*.** Al
86340 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74  locate nByte byt
86350 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f  es of memory..*/
86360 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71  .static void *sq
86370 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69  lite3MemMalloc(i
86380 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72  nt nByte){.  str
86390 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
863a0 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a  *pHdr;.  void **
863b0 70 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  pBt;.  char *z;.
863c0 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76    int *pInt;.  v
863d0 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e  oid *p = 0;.  in
863e0 74 20 74 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69  t totalSize;.  i
863f0 6e 74 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 73  nt nReserve;.  s
86400 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
86410 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20  er(mem.mutex);. 
86420 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73   assert( mem.dis
86430 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52  allow==0 );.  nR
86440 65 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28  eserve = ROUND8(
86450 6e 42 79 74 65 29 3b 0a 20 20 74 6f 74 61 6c 53  nByte);.  totalS
86460 69 7a 65 20 3d 20 6e 52 65 73 65 72 76 65 20 2b  ize = nReserve +
86470 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b   sizeof(*pHdr) +
86480 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 0a 20   sizeof(int) +. 
86490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65                me
864a0 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a  m.nBacktrace*siz
864b0 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 6d 65 6d  eof(void*) + mem
864c0 2e 6e 54 69 74 6c 65 3b 0a 20 20 70 20 3d 20 6d  .nTitle;.  p = m
864d0 61 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29  alloc(totalSize)
864e0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
864f0 20 7a 20 3d 20 70 3b 0a 20 20 20 20 70 42 74 20   z = p;.    pBt 
86500 3d 20 28 76 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d  = (void**)&z[mem
86510 2e 6e 54 69 74 6c 65 5d 3b 0a 20 20 20 20 70 48  .nTitle];.    pH
86520 64 72 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d  dr = (struct Mem
86530 42 6c 6f 63 6b 48 64 72 2a 29 26 70 42 74 5b 6d  BlockHdr*)&pBt[m
86540 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 5d 3b 0a  em.nBacktrace];.
86550 20 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 20      pHdr->pNext 
86560 3d 20 30 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70  = 0;.    pHdr->p
86570 50 72 65 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74  Prev = mem.pLast
86580 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c  ;.    if( mem.pL
86590 61 73 74 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ast ){.      mem
865a0 2e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20  .pLast->pNext = 
865b0 70 48 64 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  pHdr;.    }else{
865c0 0a 20 20 20 20 20 20 6d 65 6d 2e 70 46 69 72 73  .      mem.pFirs
865d0 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 0a  t = pHdr;.    }.
865e0 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20      mem.pLast = 
865f0 70 48 64 72 3b 0a 20 20 20 20 70 48 64 72 2d 3e  pHdr;.    pHdr->
86600 69 46 6f 72 65 47 75 61 72 64 20 3d 20 46 4f 52  iForeGuard = FOR
86610 45 47 55 41 52 44 3b 0a 20 20 20 20 70 48 64 72  EGUARD;.    pHdr
86620 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  ->nBacktraceSlot
86630 73 20 3d 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61  s = mem.nBacktra
86640 63 65 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 54  ce;.    pHdr->nT
86650 69 74 6c 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c  itle = mem.nTitl
86660 65 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e  e;.    if( mem.n
86670 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20  Backtrace ){.   
86680 20 20 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34     void *aAddr[4
86690 30 5d 3b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e  0];.      pHdr->
866a0 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 62 61 63  nBacktrace = bac
866b0 6b 74 72 61 63 65 28 61 41 64 64 72 2c 20 6d 65  ktrace(aAddr, me
866c0 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2b 31 29 2d  m.nBacktrace+1)-
866d0 31 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  1;.      memcpy(
866e0 70 42 74 2c 20 26 61 41 64 64 72 5b 31 5d 2c 20  pBt, &aAddr[1], 
866f0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
86700 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 3b  *sizeof(void*));
86710 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 42  .      assert(pB
86720 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28  t[0]);.      if(
86730 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20   mem.xBacktrace 
86740 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78  ){.        mem.x
86750 42 61 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c  Backtrace(nByte,
86760 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
86770 65 2d 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b  e-1, &aAddr[1]);
86780 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
86790 73 65 7b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e  se{.      pHdr->
867a0 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 30 3b 0a  nBacktrace = 0;.
867b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65      }.    if( me
867c0 6d 2e 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20  m.nTitle ){.    
867d0 20 20 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e    memcpy(z, mem.
867e0 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74  zTitle, mem.nTit
867f0 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  le);.    }.    p
86800 48 64 72 2d 3e 69 53 69 7a 65 20 3d 20 6e 42 79  Hdr->iSize = nBy
86810 74 65 3b 0a 20 20 20 20 61 64 6a 75 73 74 53 74  te;.    adjustSt
86820 61 74 73 28 6e 42 79 74 65 2c 20 2b 31 29 3b 0a  ats(nByte, +1);.
86830 20 20 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a      pInt = (int*
86840 29 26 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20 70  )&pHdr[1];.    p
86850 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a  Int[nReserve/siz
86860 65 6f 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52  eof(int)] = REAR
86870 47 55 41 52 44 3b 0a 20 20 20 20 6d 65 6d 73 65  GUARD;.    memse
86880 74 28 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52  t(pInt, 0x65, nR
86890 65 73 65 72 76 65 29 3b 0a 20 20 20 20 70 20 3d  eserve);.    p =
868a0 20 28 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20   (void*)pInt;.  
868b0 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
868c0 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65  x_leave(mem.mute
868d0 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20  x);.  return p; 
868e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d  .}../*.** Free m
868f0 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  emory..*/.static
86900 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
86910 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f  Free(void *pPrio
86920 72 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d  r){.  struct Mem
86930 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a  BlockHdr *pHdr;.
86940 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20    void **pBt;.  
86950 63 68 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72  char *z;.  asser
86960 74 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  t( sqlite3Global
86970 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20  Config.bMemstat 
86980 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20  || mem.mutex!=0 
86990 29 3b 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69  );.  pHdr = sqli
869a0 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64  te3MemsysGetHead
869b0 65 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 42  er(pPrior);.  pB
869c0 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72  t = (void**)pHdr
869d0 3b 0a 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d  ;.  pBt -= pHdr-
869e0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
869f0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
86a00 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65  x_enter(mem.mute
86a10 78 29 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e  x);.  if( pHdr->
86a20 70 50 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73  pPrev ){.    ass
86a30 65 72 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76  ert( pHdr->pPrev
86a40 2d 3e 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b  ->pNext==pHdr );
86a50 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76  .    pHdr->pPrev
86a60 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e  ->pNext = pHdr->
86a70 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pNext;.  }else{.
86a80 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e      assert( mem.
86a90 70 46 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a  pFirst==pHdr );.
86aa0 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d      mem.pFirst =
86ab0 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20   pHdr->pNext;.  
86ac0 7d 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e  }.  if( pHdr->pN
86ad0 65 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ext ){.    asser
86ae0 74 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e  t( pHdr->pNext->
86af0 70 50 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20  pPrev==pHdr );. 
86b00 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e     pHdr->pNext->
86b10 70 50 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50  pPrev = pHdr->pP
86b20 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  rev;.  }else{.  
86b30 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c    assert( mem.pL
86b40 61 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20  ast==pHdr );.   
86b50 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64   mem.pLast = pHd
86b60 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20  r->pPrev;.  }.  
86b70 7a 20 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a  z = (char*)pBt;.
86b80 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69    z -= pHdr->nTi
86b90 74 6c 65 3b 0a 20 20 61 64 6a 75 73 74 53 74 61  tle;.  adjustSta
86ba0 74 73 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20  ts(pHdr->iSize, 
86bb0 2d 31 29 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c  -1);.  memset(z,
86bc0 20 30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f   0x2b, sizeof(vo
86bd0 69 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b  id*)*pHdr->nBack
86be0 74 72 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a  traceSlots + siz
86bf0 65 6f 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20  eof(*pHdr) +.   
86c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
86c10 48 64 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a  Hdr->iSize + siz
86c20 65 6f 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d  eof(int) + pHdr-
86c30 3e 6e 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65  >nTitle);.  free
86c40 28 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  (z);.  sqlite3_m
86c50 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d  utex_leave(mem.m
86c60 75 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a  utex);  .}../*.*
86c70 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a  * Change the siz
86c80 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67  e of an existing
86c90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
86ca0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  on..**.** For th
86cb0 69 73 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70  is debugging imp
86cc0 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20  lementation, we 
86cd0 2a 61 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20  *always* make a 
86ce0 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61  copy of the.** a
86cf0 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61  llocation into a
86d00 20 6e 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65   new place in me
86d10 6d 6f 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77  mory.  In this w
86d20 61 79 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68  ay, if the .** h
86d30 69 67 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65  igher level code
86d40 20 69 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65   is using pointe
86d50 72 20 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c  r to the old all
86d60 6f 63 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a  ocation, it is .
86d70 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b  ** much more lik
86d80 65 6c 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64  ely to break and
86d90 20 77 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72   we are much mor
86da0 65 20 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64  e liking to find
86db0 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a  .** the error..*
86dc0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73  /.static void *s
86dd0 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
86de0 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69  (void *pPrior, i
86df0 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72  nt nByte){.  str
86e00 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
86e10 2a 70 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69 64  *pOldHdr;.  void
86e20 20 2a 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74   *pNew;.  assert
86e30 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d  ( mem.disallow==
86e40 30 20 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d  0 );.  pOldHdr =
86e50 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65   sqlite3MemsysGe
86e60 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b  tHeader(pPrior);
86e70 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
86e80 33 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65  3MemMalloc(nByte
86e90 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  );.  if( pNew ){
86ea0 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77  .    memcpy(pNew
86eb0 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c  , pPrior, nByte<
86ec0 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f  pOldHdr->iSize ?
86ed0 20 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72   nByte : pOldHdr
86ee0 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66  ->iSize);.    if
86ef0 28 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d  ( nByte>pOldHdr-
86f00 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  >iSize ){.      
86f10 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29  memset(&((char*)
86f20 70 4e 65 77 29 5b 70 4f 6c 64 48 64 72 2d 3e 69  pNew)[pOldHdr->i
86f30 53 69 7a 65 5d 2c 20 30 78 32 62 2c 20 6e 42 79  Size], 0x2b, nBy
86f40 74 65 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53  te - pOldHdr->iS
86f50 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ize);.    }.    
86f60 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 70  sqlite3MemFree(p
86f70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65  Prior);.  }.  re
86f80 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
86f90 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65  .** Populate the
86fa0 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72   low-level memor
86fb0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e  y allocation fun
86fc0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69  ction pointers i
86fd0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62  n.** sqlite3Glob
86fe0 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20  alConfig.m with 
86ff0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  pointers to the 
87000 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73  routines in this
87010 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   file..*/.SQLITE
87020 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
87030 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75  lite3MemSetDefau
87040 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  lt(void){.  stat
87050 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
87060 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66  _mem_methods def
87070 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a  aultMethods = {.
87080 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d       sqlite3MemM
87090 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69  alloc,.     sqli
870a0 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20  te3MemFree,.    
870b0 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c   sqlite3MemReall
870c0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
870d0 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71  MemSize,.     sq
870e0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c  lite3MemRoundup,
870f0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
87100 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74  Init,.     sqlit
87110 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20  e3MemShutdown,. 
87120 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c      0.  };.  sql
87130 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49  ite3_config(SQLI
87140 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43  TE_CONFIG_MALLOC
87150 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  , &defaultMethod
87160 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  s);.}../*.** Set
87170 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
87180 61 63 6b 74 72 61 63 65 20 6c 65 76 65 6c 73 20  acktrace levels 
87190 6b 65 70 74 20 66 6f 72 20 65 61 63 68 20 61 6c  kept for each al
871a0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20 76  location..** A v
871b0 61 6c 75 65 20 6f 66 20 7a 65 72 6f 20 74 75 72  alue of zero tur
871c0 6e 73 20 6f 66 66 20 62 61 63 6b 74 72 61 63 69  ns off backtraci
871d0 6e 67 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20  ng.  The number 
871e0 69 73 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65  is always rounde
871f0 64 0a 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c  d.** up to a mul
87200 74 69 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53  tiple of 2..*/.S
87210 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
87220 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  id sqlite3Memdeb
87230 75 67 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20  ugBacktrace(int 
87240 64 65 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65  depth){.  if( de
87250 70 74 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d  pth<0 ){ depth =
87260 20 30 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74   0; }.  if( dept
87270 68 3e 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20  h>20 ){ depth = 
87280 32 30 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20  20; }.  depth = 
87290 28 64 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a  (depth+1)&0xfe;.
872a0 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65    mem.nBacktrace
872b0 20 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c   = depth;.}..SQL
872c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
872d0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
872e0 42 61 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63  BacktraceCallbac
872f0 6b 28 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72  k(void (*xBacktr
87300 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76  ace)(int, int, v
87310 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e  oid **)){.  mem.
87320 78 42 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61  xBacktrace = xBa
87330 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  cktrace;.}../*.*
87340 2a 20 53 65 74 20 74 68 65 20 74 69 74 6c 65 20  * Set the title 
87350 73 74 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65  string for subse
87360 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e  quent allocation
87370 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
87380 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
87390 33 4d 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c  3MemdebugSettitl
873a0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54  e(const char *zT
873b0 69 74 6c 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65  itle){.  unsigne
873c0 64 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65  d int n = sqlite
873d0 33 53 74 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65  3Strlen30(zTitle
873e0 29 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33  ) + 1;.  sqlite3
873f0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
87400 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e  .mutex);.  if( n
87410 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69  >=sizeof(mem.zTi
87420 74 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f  tle) ) n = sizeo
87430 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b  f(mem.zTitle)-1;
87440 0a 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54  .  memcpy(mem.zT
87450 69 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29  itle, zTitle, n)
87460 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b 6e  ;.  mem.zTitle[n
87470 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 69  ] = 0;.  mem.nTi
87480 74 6c 65 20 3d 20 52 4f 55 4e 44 38 28 6e 29 3b  tle = ROUND8(n);
87490 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
874a0 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
874b0 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
874c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
874d0 33 4d 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b  3MemdebugSync(){
874e0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
874f0 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66  ckHdr *pHdr;.  f
87500 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72  or(pHdr=mem.pFir
87510 73 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70  st; pHdr; pHdr=p
87520 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Hdr->pNext){.   
87530 20 76 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76   void **pBt = (v
87540 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20  oid**)pHdr;.    
87550 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61  pBt -= pHdr->nBa
87560 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20  cktraceSlots;.  
87570 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65    mem.xBacktrace
87580 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48  (pHdr->iSize, pH
87590 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31  dr->nBacktrace-1
875a0 2c 20 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a  , &pBt[1]);.  }.
875b0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68  }../*.** Open th
875c0 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64  e file indicated
875d0 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67   and write a log
875e0 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20   of all unfreed 
875f0 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63  memory .** alloc
87600 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74  ations into that
87610 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   log..*/.SQLITE_
87620 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
87630 69 74 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70  ite3MemdebugDump
87640 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
87650 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20  lename){.  FILE 
87660 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d  *out;.  struct M
87670 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72  emBlockHdr *pHdr
87680 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a  ;.  void **pBt;.
87690 20 20 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d    int i;.  out =
876a0 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65   fopen(zFilename
876b0 2c 20 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75  , "w");.  if( ou
876c0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69  t==0 ){.    fpri
876d0 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20  ntf(stderr, "** 
876e0 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74  Unable to output
876f0 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75   memory debug ou
87700 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c  tput log: %s **\
87710 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
87720 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d          zFilenam
87730 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  e);.    return;.
87740 20 20 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d    }.  for(pHdr=m
87750 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b  em.pFirst; pHdr;
87760 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78   pHdr=pHdr->pNex
87770 74 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20  t){.    char *z 
87780 3d 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20  = (char*)pHdr;. 
87790 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42     z -= pHdr->nB
877a0 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69  acktraceSlots*si
877b0 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48  zeof(void*) + pH
877c0 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20  dr->nTitle;.    
877d0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a  fprintf(out, "**
877e0 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74  ** %lld bytes at
877f0 20 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a   %p from %s ****
87800 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20  \n", .          
87810 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26    pHdr->iSize, &
87820 70 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e  pHdr[1], pHdr->n
87830 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f  Title ? z : "???
87840 22 29 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72  ");.    if( pHdr
87850 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a  ->nBacktrace ){.
87860 20 20 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74        fflush(out
87870 29 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28  );.      pBt = (
87880 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20  void**)pHdr;.   
87890 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e     pBt -= pHdr->
878a0 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b  nBacktraceSlots;
878b0 0a 20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65  .      backtrace
878c0 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c  _symbols_fd(pBt,
878d0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
878e0 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b  e, fileno(out));
878f0 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
87900 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d  ut, "\n");.    }
87910 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f  .  }.  fprintf(o
87920 75 74 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29  ut, "COUNTS:\n")
87930 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e  ;.  for(i=0; i<N
87940 43 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20  CSIZE-1; i++){. 
87950 20 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f     if( mem.nAllo
87960 63 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70  c[i] ){.      fp
87970 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25  rintf(out, "   %
87980 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31  5d: %10d %10d %1
87990 30 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20  0d\n", .        
879a0 20 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c      i*8, mem.nAl
879b0 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72  loc[i], mem.nCur
879c0 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43  rent[i], mem.mxC
879d0 75 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20  urrent[i]);.    
879e0 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e  }.  }.  if( mem.
879f0 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d  nAlloc[NCSIZE-1]
87a00 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28   ){.    fprintf(
87a10 6f 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31  out, "   %5d: %1
87a20 30 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c  0d %10d %10d\n",
87a30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 43  .             NC
87a40 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41  SIZE*8-8, mem.nA
87a50 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a  lloc[NCSIZE-1],.
87a60 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
87a70 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45  .nCurrent[NCSIZE
87a80 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65  -1], mem.mxCurre
87a90 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20  nt[NCSIZE-1]);. 
87aa0 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29   }.  fclose(out)
87ab0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
87ac0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
87ad0 74 69 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d  times sqlite3Mem
87ae0 4d 61 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65  Malloc() has bee
87af0 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c  n called..*/.SQL
87b00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
87b10 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d  sqlite3MemdebugM
87b20 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20  allocCount(){.  
87b30 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f  int i;.  int nTo
87b40 74 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  tal = 0;.  for(i
87b50 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b  =0; i<NCSIZE; i+
87b60 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b  +){.    nTotal +
87b70 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b  = mem.nAlloc[i];
87b80 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54  .  }.  return nT
87b90 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66  otal;.}...#endif
87ba0 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45   /* SQLITE_MEMDE
87bb0 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  BUG */../*******
87bc0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
87bd0 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em2.c **********
87be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87c00 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
87c10 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
87c20 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a  le mem3.c ******
87c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87c50 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
87c60 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a  07 October 14.**
87c70 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
87c80 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
87c90 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
87ca0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
87cb0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
87cc0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
87cd0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
87ce0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
87cf0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
87d00 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
87d10 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
87d20 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
87d30 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
87d40 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
87d50 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
87d60 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
87d70 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
87d80 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
87d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
87dd0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
87de0 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f  ns the C functio
87df0 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ns that implemen
87e00 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  t a memory.** al
87e10 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
87e20 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51  em for use by SQ
87e30 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69  Lite. .**.** Thi
87e40 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  s version of the
87e50 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
87e60 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69  on subsystem omi
87e70 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66  ts all.** use of
87e80 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53   malloc(). The S
87e90 51 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 6c  QLite user suppl
87ea0 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  ies a block of m
87eb0 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20  emory.** before 
87ec0 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f  calling sqlite3_
87ed0 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f  initialize() fro
87ee0 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69  m which allocati
87ef0 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20  ons.** are made 
87f00 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20  and returned by 
87f10 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e  the xMalloc() an
87f20 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a  d xRealloc() .**
87f30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
87f40 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69  . Once sqlite3_i
87f50 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20  nitialize() has 
87f60 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20  been called,.** 
87f70 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  the amount of me
87f80 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74  mory available t
87f90 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 65  o SQLite is fixe
87fa0 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20  d and cannot.** 
87fb0 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a  be changed..**.*
87fc0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
87fd0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
87fe0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
87ff0 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a  m is included.**
88000 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e   in the build on
88010 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ly if SQLITE_ENA
88020 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64  BLE_MEMSYS3 is d
88030 65 66 69 6e 65 64 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  efined..*/../*.*
88040 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
88050 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
88060 6f 63 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62  ocator is only b
88070 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69  uilt into the li
88080 62 72 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f  brary.** SQLITE_
88090 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69  ENABLE_MEMSYS3 i
880a0 73 20 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e  s defined. Defin
880b0 69 6e 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20  ing this symbol 
880c0 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e  does not.** mean
880d0 20 74 68 61 74 20 74 68 65 20 6c 69 62 72 61 72   that the librar
880e0 79 20 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d  y will use a mem
880f0 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61  ory-pool by defa
88100 75 6c 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a  ult, just that.*
88110 2a 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c  * it is availabl
88120 65 2e 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61  e. The mempool a
88130 6c 6c 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69  llocator is acti
88140 76 61 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67  vated by calling
88150 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  .** sqlite3_conf
88160 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ig()..*/.#ifdef 
88170 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
88180 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78  MSYS3../*.** Max
88190 69 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65  imum size (in Me
881a0 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22  m3Blocks) of a "
881b0 73 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f  small" chunk..*/
881c0 0a 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c  .#define MX_SMAL
881d0 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d  L 10.../*.** Num
881e0 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20  ber of freelist 
881f0 68 61 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64  hash slots.*/.#d
88200 65 66 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31  efine N_HASH  61
88210 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79  ../*.** A memory
88220 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73   allocation (als
88230 6f 20 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e  o called a "chun
88240 6b 22 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  k") consists of 
88250 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20  two or .** more 
88260 62 6c 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63  blocks where eac
88270 68 20 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74  h block is 8 byt
88280 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 38  es.  The first 8
88290 20 62 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61   bytes are .** a
882a0 20 68 65 61 64 65 72 20 74 68 61 74 20 69 73 20   header that is 
882b0 6e 6f 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20  not returned to 
882c0 74 68 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20  the user..**.** 
882d0 41 20 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f  A chunk is two o
882e0 72 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68  r more blocks th
882f0 61 74 20 69 73 20 65 69 74 68 65 72 20 63 68 65  at is either che
88300 63 6b 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66  cked out or.** f
88310 72 65 65 2e 20 20 54 68 65 20 66 69 72 73 74 20  ree.  The first 
88320 62 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74  block has format
88330 20 75 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73   u.hdr.  u.hdr.s
88340 69 7a 65 34 78 20 69 73 20 34 20 74 69 6d 65 73  ize4x is 4 times
88350 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20   the.** size of 
88360 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
88370 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20  n blocks if the 
88380 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72  allocation is fr
88390 65 65 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72  ee..** The u.hdr
883a0 2e 73 69 7a 65 34 78 26 31 20 62 69 74 20 69 73  .size4x&1 bit is
883b0 20 74 72 75 65 20 69 66 20 74 68 65 20 63 68 75   true if the chu
883c0 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75  nk is checked ou
883d0 74 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69  t and.** false i
883e0 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f  f the chunk is o
883f0 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20  n the freelist. 
88400 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34   The u.hdr.size4
88410 78 26 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72  x&2 bit.** is tr
88420 75 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f  ue if the previo
88430 75 73 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63  us chunk is chec
88440 6b 65 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73  ked out and fals
88450 65 20 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76  e if the.** prev
88460 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72  ious chunk is fr
88470 65 65 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70  ee.  The u.hdr.p
88480 72 65 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73  revSize field is
88490 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20   the size of.** 
884a0 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75  the previous chu
884b0 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20  nk in blocks if 
884c0 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75  the previous chu
884d0 6e 6b 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20  nk is on the.** 
884e0 66 72 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65  freelist. If the
884f0 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20   previous chunk 
88500 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20  is checked out, 
88510 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72  then.** u.hdr.pr
88520 65 76 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61  evSize can be pa
88530 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66  rt of the data f
88540 6f 72 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e  or that chunk an
88550 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20  d should.** not 
88560 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74  be read or writt
88570 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74  en..**.** We oft
88580 65 6e 20 69 64 65 6e 74 69 66 79 20 61 20 63 68  en identify a ch
88590 75 6e 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78  unk by its index
885a0 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d   in mem3.aPool[]
885b0 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20  .  When.** this 
885c0 69 73 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75  is done, the chu
885d0 6e 6b 20 69 6e 64 65 78 20 72 65 66 65 72 73 20  nk index refers 
885e0 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c  to the second bl
885f0 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68  ock of.** the ch
88600 75 6e 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61  unk.  In this wa
88610 79 2c 20 74 68 65 20 66 69 72 73 74 20 63 68 75  y, the first chu
88620 6e 6b 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  nk has an index 
88630 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b  of 1..** A chunk
88640 20 69 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e   index of 0 mean
88650 73 20 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b  s "no such chunk
88660 22 20 61 6e 64 20 69 73 20 74 68 65 20 65 71 75  " and is the equ
88670 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20  ivalent.** of a 
88680 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
88690 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62  .** The second b
886a0 6c 6f 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75  lock of free chu
886b0 6e 6b 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f  nks is of the fo
886c0 72 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a  rm u.list.  The.
886d0 2a 2a 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f  ** two fields fo
886e0 72 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b  rm a double-link
886f0 65 64 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b  ed list of chunk
88700 73 20 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a  s of related siz
88710 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20  es..** Pointers 
88720 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  to the head of t
88730 68 65 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72  he list are stor
88740 65 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61  ed in mem3.aiSma
88750 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61  ll[] .** for sma
88760 6c 6c 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20  ller chunks and 
88770 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f  mem3.aiHash[] fo
88780 72 20 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e  r larger chunks.
88790 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  .**.** The secon
887a0 64 20 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75  d block of a chu
887b0 6e 6b 20 69 73 20 75 73 65 72 20 64 61 74 61 20  nk is user data 
887c0 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20  if the chunk is 
887d0 63 68 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e  checked .** out.
887e0 20 20 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20    If a chunk is 
887f0 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65  checked out, the
88800 20 75 73 65 72 20 64 61 74 61 20 6d 61 79 20 65   user data may e
88810 78 74 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68  xtend into.** th
88820 65 20 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  e u.hdr.prevSize
88830 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f   value of the fo
88840 6c 6c 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a  llowing chunk..*
88850 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
88860 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42   Mem3Block Mem3B
88870 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d  lock;.struct Mem
88880 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e  3Block {.  union
88890 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a   {.    struct {.
888a0 20 20 20 20 20 20 75 33 32 20 70 72 65 76 53 69        u32 prevSi
888b0 7a 65 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66  ze;   /* Size of
888c0 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20   previous chunk 
888d0 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65  in Mem3Block ele
888e0 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75  ments */.      u
888f0 33 32 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f  32 size4x;     /
88900 2a 20 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66  * 4x the size of
88910 20 63 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69   current chunk i
88920 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d  n Mem3Block elem
88930 65 6e 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64  ents */.    } hd
88940 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a  r;.    struct {.
88950 20 20 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20        u32 next; 
88960 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
88970 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f  n mem3.aPool[] o
88980 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e  f next free chun
88990 6b 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70  k */.      u32 p
889a0 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  rev;       /* In
889b0 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f  dex in mem3.aPoo
889c0 6c 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20  l[] of previous 
889d0 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20  free chunk */.  
889e0 20 20 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b    } list;.  } u;
889f0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f  .};../*.** All o
88a00 66 20 74 68 65 20 73 74 61 74 69 63 20 76 61 72  f the static var
88a10 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 74  iables used by t
88a20 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63  his module are c
88a30 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f  ollected.** into
88a40 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74   a single struct
88a50 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22  ure named "mem3"
88a60 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65  .  This is to ke
88a70 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63  ep the.** static
88a80 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e   variables organ
88a90 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75  ized and to redu
88aa0 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c  ce namespace pol
88ab0 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74  lution.** when t
88ac0 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f  his module is co
88ad0 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65  mbined with othe
88ae0 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d  r in the amalgam
88af0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
88b00 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75   SQLITE_WSD stru
88b10 63 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a  ct Mem3Global {.
88b20 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79    /*.  ** Memory
88b30 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61   available for a
88b40 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c  llocation. nPool
88b50 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   is the size of 
88b60 74 68 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28  the array.  ** (
88b70 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70  in Mem3Blocks) p
88b80 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f  ointed to by aPo
88b90 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a  ol less 2..  */.
88ba0 20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d    u32 nPool;.  M
88bb0 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b  em3Block *aPool;
88bc0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65  ..  /*.  ** True
88bd0 20 69 66 20 77 65 20 61 72 65 20 65 76 61 6c 75   if we are evalu
88be0 61 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d  ating an out-of-
88bf0 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e  memory callback.
88c00 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72  .  */.  int alar
88c10 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20  mBusy;.  .  /*. 
88c20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e   ** Mutex to con
88c30 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74  trol access to t
88c40 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
88c50 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a  tion subsystem..
88c60 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d    */.  sqlite3_m
88c70 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a  utex *mutex;.  .
88c80 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69    /*.  ** The mi
88c90 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20  nimum amount of 
88ca0 66 72 65 65 20 73 70 61 63 65 20 74 68 61 74 20  free space that 
88cb0 77 65 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20  we have seen..  
88cc0 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65  */.  u32 mnMaste
88cd0 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d  r;..  /*.  ** iM
88ce0 61 73 74 65 72 20 69 73 20 74 68 65 20 69 6e 64  aster is the ind
88cf0 65 78 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  ex of the master
88d00 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65   chunk.  Most ne
88d10 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20  w allocations.  
88d20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20  ** occur off of 
88d30 74 68 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d  this chunk.  szM
88d40 61 73 74 65 72 20 69 73 20 74 68 65 20 73 69 7a  aster is the siz
88d50 65 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73  e (in Mem3Blocks
88d60 29 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75  ).  ** of the cu
88d70 72 72 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69  rrent master.  i
88d80 4d 61 73 74 65 72 20 69 73 20 30 20 69 66 20 74  Master is 0 if t
88d90 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74  here is not mast
88da0 65 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54  er chunk..  ** T
88db0 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20  he master chunk 
88dc0 69 73 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72  is not in either
88dd0 20 74 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72   the aiHash[] or
88de0 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f   aiSmall[]..  */
88df0 0a 20 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a  .  u32 iMaster;.
88e00 20 20 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a    u32 szMaster;.
88e10 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79  .  /*.  ** Array
88e20 20 6f 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65   of lists of fre
88e30 65 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69  e blocks accordi
88e40 6e 67 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20  ng to the block 
88e50 73 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73  size .  ** for s
88e60 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f  maller chunks, o
88e70 72 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20  r a hash on the 
88e80 62 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c  block size for l
88e90 61 72 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b  arger.  ** chunk
88ea0 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69  s..  */.  u32 ai
88eb0 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31  Small[MX_SMALL-1
88ec0 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65  ];   /* For size
88ed0 73 20 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53  s 2 through MX_S
88ee0 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20  MALL, inclusive 
88ef0 2a 2f 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b  */.  u32 aiHash[
88f00 4e 5f 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20  N_HASH];        
88f10 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f  /* For sizes MX_
88f20 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67  SMALL+1 and larg
88f30 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b  er */.} mem3 = {
88f40 20 39 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64   97535575 };..#d
88f50 65 66 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41  efine mem3 GLOBA
88f60 4c 28 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f  L(struct Mem3Glo
88f70 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a  bal, mem3)../*.*
88f80 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75  * Unlink the chu
88f90 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c  nk at mem3.aPool
88fa0 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74  [i] from list it
88fb0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a   is currently.**
88fc0 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20   on.  *pRoot is 
88fd0 74 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 20  the list that i 
88fe0 69 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a  is a member of..
88ff0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
89000 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d  emsys3UnlinkFrom
89010 4c 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20  List(u32 i, u32 
89020 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e  *pRoot){.  u32 n
89030 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ext = mem3.aPool
89040 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b  [i].u.list.next;
89050 0a 20 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65  .  u32 prev = me
89060 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69  m3.aPool[i].u.li
89070 73 74 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72  st.prev;.  asser
89080 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
89090 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78  _held(mem3.mutex
890a0 29 20 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d  ) );.  if( prev=
890b0 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74  =0 ){.    *pRoot
890c0 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65   = next;.  }else
890d0 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  {.    mem3.aPool
890e0 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65  [prev].u.list.ne
890f0 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20  xt = next;.  }. 
89100 20 69 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20   if( next ){.   
89110 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74   mem3.aPool[next
89120 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20  ].u.list.prev = 
89130 70 72 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33  prev;.  }.  mem3
89140 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
89150 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d  .next = 0;.  mem
89160 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73  3.aPool[i].u.lis
89170 74 2e 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f  t.prev = 0;.}../
89180 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20  *.** Unlink the 
89190 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69  chunk at index i
891a0 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76   from .** whatev
891b0 65 72 20 6c 69 73 74 20 69 73 20 63 75 72 72 65  er list is curre
891c0 6e 74 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66  ntly a member of
891d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
891e0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75   memsys3Unlink(u
891f0 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a  32 i){.  u32 siz
89200 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72  e, hash;.  asser
89210 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
89220 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78  _held(mem3.mutex
89230 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
89240 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
89250 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31  u.hdr.size4x & 1
89260 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )==0 );.  assert
89270 28 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65  ( i>=1 );.  size
89280 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
89290 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
892a0 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  4;.  assert( siz
892b0 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b  e==mem3.aPool[i+
892c0 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  size-1].u.hdr.pr
892d0 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65  evSize );.  asse
892e0 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20  rt( size>=2 );. 
892f0 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f   if( size <= MX_
89300 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d  SMALL ){.    mem
89310 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69  sys3UnlinkFromLi
89320 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d  st(i, &mem3.aiSm
89330 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20  all[size-2]);.  
89340 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20  }else{.    hash 
89350 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b  = size % N_HASH;
89360 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69  .    memsys3Unli
89370 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d  nkFromList(i, &m
89380 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d  em3.aiHash[hash]
89390 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
893a0 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61  Link the chunk a
893b0 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20  t mem3.aPool[i] 
893c0 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68  so that is on th
893d0 65 20 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a  e list rooted.**
893e0 20 61 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73   at *pRoot..*/.s
893f0 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
89400 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75  s3LinkIntoList(u
89410 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74  32 i, u32 *pRoot
89420 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
89430 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
89440 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20  mem3.mutex) );. 
89450 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75   mem3.aPool[i].u
89460 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52  .list.next = *pR
89470 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  oot;.  mem3.aPoo
89480 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76  l[i].u.list.prev
89490 20 3d 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f   = 0;.  if( *pRo
894a0 6f 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61  ot ){.    mem3.a
894b0 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c  Pool[*pRoot].u.l
894c0 69 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20  ist.prev = i;.  
894d0 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a  }.  *pRoot = i;.
894e0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68  }../*.** Link th
894f0 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78  e chunk at index
89500 20 69 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74   i into either t
89510 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a  he appropriate.*
89520 2a 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69  * small chunk li
89530 73 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20  st, or into the 
89540 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68  large chunk hash
89550 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
89560 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69  c void memsys3Li
89570 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32  nk(u32 i){.  u32
89580 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61   size, hash;.  a
89590 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
895a0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
895b0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
895c0 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73  t( i>=1 );.  ass
895d0 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c  ert( (mem3.aPool
895e0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
895f0 34 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20  4x & 1)==0 );.  
89600 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
89610 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
89620 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28  e4x/4;.  assert(
89630 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f   size==mem3.aPoo
89640 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64  l[i+size-1].u.hd
89650 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20  r.prevSize );.  
89660 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20  assert( size>=2 
89670 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d  );.  if( size <=
89680 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20   MX_SMALL ){.   
89690 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f   memsys3LinkInto
896a0 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69  List(i, &mem3.ai
896b0 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a  Small[size-2]);.
896c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73    }else{.    has
896d0 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53  h = size % N_HAS
896e0 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69  H;.    memsys3Li
896f0 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d  nkIntoList(i, &m
89700 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d  em3.aiHash[hash]
89710 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
89720 49 66 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45  If the STATIC_ME
89730 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61  M mutex is not a
89740 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74  lready held, obt
89750 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20  ain it now. The 
89760 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c  mutex.** will al
89770 72 65 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f  ready be held (o
89780 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20  btained by code 
89790 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a  in malloc.c) if.
897a0 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ** sqlite3Global
897b0 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20  Config.bMemStat 
897c0 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74  is true..*/.stat
897d0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45  ic void memsys3E
897e0 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66  nter(void){.  if
897f0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
89800 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d  onfig.bMemstat==
89810 30 20 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d  0 && mem3.mutex=
89820 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d  =0 ){.    mem3.m
89830 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
89840 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
89850 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
89860 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
89870 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
89880 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74  3.mutex);.}.stat
89890 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c  ic void memsys3L
898a0 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71  eave(void){.  sq
898b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
898c0 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d  e(mem3.mutex);.}
898d0 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77  ../*.** Called w
898e0 68 65 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c  hen we are unabl
898f0 65 20 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20  e to satisfy an 
89900 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42  allocation of nB
89910 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ytes..*/.static 
89920 76 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f  void memsys3OutO
89930 66 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74  fMemory(int nByt
89940 65 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e  e){.  if( !mem3.
89950 61 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20  alarmBusy ){.   
89960 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20   mem3.alarmBusy 
89970 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 1;.    assert(
89980 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
89990 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
899a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
899b0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e  utex_leave(mem3.
899c0 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69  mutex);.    sqli
899d0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
899e0 72 79 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73  ry(nByte);.    s
899f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
89a00 65 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a  er(mem3.mutex);.
89a10 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75      mem3.alarmBu
89a20 73 79 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a  sy = 0;.  }.}...
89a30 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73  /*.** Chunk i is
89a40 20 61 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68   a free chunk th
89a50 61 74 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69  at has been unli
89a60 6e 6b 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74  nked.  Adjust it
89a70 73 20 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d  s .** size param
89a80 65 74 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d  eters for check-
89a90 6f 75 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61  out and return a
89aa0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
89ab0 0a 2a 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e  .** user portion
89ac0 20 6f 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a   of the chunk..*
89ad0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d  /.static void *m
89ae0 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75  emsys3Checkout(u
89af0 33 32 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b  32 i, u32 nBlock
89b00 29 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73  ){.  u32 x;.  as
89b10 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
89b20 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75  tex_held(mem3.mu
89b30 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
89b40 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65  ( i>=1 );.  asse
89b50 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  rt( mem3.aPool[i
89b60 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89b70 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  /4==nBlock );.  
89b80 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f  assert( mem3.aPo
89b90 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75  ol[i+nBlock-1].u
89ba0 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e  .hdr.prevSize==n
89bb0 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d  Block );.  x = m
89bc0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
89bd0 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d  .hdr.size4x;.  m
89be0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
89bf0 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42  .hdr.size4x = nB
89c00 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26  lock*4 | 1 | (x&
89c10 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  2);.  mem3.aPool
89c20 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68  [i+nBlock-1].u.h
89c30 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42  dr.prevSize = nB
89c40 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f  lock;.  mem3.aPo
89c50 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75  ol[i+nBlock-1].u
89c60 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32  .hdr.size4x |= 2
89c70 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33  ;.  return &mem3
89c80 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a  .aPool[i];.}../*
89c90 0a 2a 2a 20 43 61 72 76 65 20 61 20 70 69 65 63  .** Carve a piec
89ca0 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64  e off of the end
89cb0 20 6f 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61   of the mem3.iMa
89cc0 73 74 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e  ster free chunk.
89cd0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
89ce0 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20  nter to the new 
89cf0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c  allocation.  Or,
89d00 20 69 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   if the master c
89d10 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c  hunk.** is not l
89d20 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74  arge enough, ret
89d30 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63  urn 0..*/.static
89d40 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72   void *memsys3Fr
89d50 6f 6d 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c  omMaster(u32 nBl
89d60 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ock){.  assert( 
89d70 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
89d80 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29  ld(mem3.mutex) )
89d90 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33  ;.  assert( mem3
89da0 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63  .szMaster>=nBloc
89db0 6b 20 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63  k );.  if( nBloc
89dc0 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  k>=mem3.szMaster
89dd0 2d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65  -1 ){.    /* Use
89de0 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74   the entire mast
89df0 65 72 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a  er */.    void *
89e00 70 20 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b  p = memsys3Check
89e10 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72  out(mem3.iMaster
89e20 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29  , mem3.szMaster)
89e30 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74  ;.    mem3.iMast
89e40 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33  er = 0;.    mem3
89e50 2e 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20  .szMaster = 0;. 
89e60 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72     mem3.mnMaster
89e70 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
89e80 20 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   p;.  }else{.   
89e90 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61   /* Split the ma
89ea0 73 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74  ster block.  Ret
89eb0 75 72 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f  urn the tail. */
89ec0 0a 20 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78  .    u32 newi, x
89ed0 3b 0a 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d  ;.    newi = mem
89ee0 33 2e 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33  3.iMaster + mem3
89ef0 2e 73 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f  .szMaster - nBlo
89f00 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ck;.    assert( 
89f10 6e 65 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73  newi > mem3.iMas
89f20 74 65 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d  ter+1 );.    mem
89f30 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
89f40 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
89f50 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  er-1].u.hdr.prev
89f60 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20  Size = nBlock;. 
89f70 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
89f80 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
89f90 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
89fa0 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20  r.size4x |= 2;. 
89fb0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65     mem3.aPool[ne
89fc0 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  wi-1].u.hdr.size
89fd0 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20  4x = nBlock*4 + 
89fe0 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  1;.    mem3.szMa
89ff0 73 74 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a  ster -= nBlock;.
8a000 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e      mem3.aPool[n
8a010 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ewi-1].u.hdr.pre
8a020 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d  vSize = mem3.szM
8a030 61 73 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d  aster;.    x = m
8a040 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8a050 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8a060 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20  size4x & 2;.    
8a070 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8a080 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  iMaster-1].u.hdr
8a090 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73  .size4x = mem3.s
8a0a0 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20  zMaster*4 | x;. 
8a0b0 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61     if( mem3.szMa
8a0c0 73 74 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61  ster < mem3.mnMa
8a0d0 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65  ster ){.      me
8a0e0 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65  m3.mnMaster = me
8a0f0 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20  m3.szMaster;.   
8a100 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76   }.    return (v
8a110 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c  oid*)&mem3.aPool
8a120 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f  [newi];.  }.}../
8a130 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74  *.** *pRoot is t
8a140 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73  he head of a lis
8a150 74 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73  t of free chunks
8a160 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a   of the same siz
8a170 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a  e.** or same siz
8a180 65 20 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65  e hash.  In othe
8a190 72 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20  r words, *pRoot 
8a1a0 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65  is an entry in e
8a1b0 69 74 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69  ither.** mem3.ai
8a1c0 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e  Small[] or mem3.
8a1d0 61 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a  aiHash[].  .**.*
8a1e0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65  * This routine e
8a1f0 78 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72  xamines all entr
8a200 69 65 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e  ies on the given
8a210 20 6c 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a   list and tries.
8a220 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65  ** to coalesce e
8a230 61 63 68 20 65 6e 74 72 69 65 73 20 77 69 74 68  ach entries with
8a240 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63   adjacent free c
8a250 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49  hunks.  .**.** I
8a260 66 20 69 74 20 73 65 65 73 20 61 20 63 68 75 6e  f it sees a chun
8a270 6b 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 72  k that is larger
8a280 20 74 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74   than mem3.iMast
8a290 65 72 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20  er, it replaces 
8a2a0 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20  .** the current 
8a2b0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74  mem3.iMaster wit
8a2c0 68 20 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72  h the new larger
8a2d0 20 63 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65   chunk.  In orde
8a2e0 72 20 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65  r for.** this me
8a2f0 6d 33 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61  m3.iMaster repla
8a300 63 65 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20  cement to work, 
8a310 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b  the master chunk
8a320 20 6d 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b   must be.** link
8a330 65 64 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68  ed into the hash
8a340 20 74 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69   tables.  That i
8a350 73 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c  s not the normal
8a360 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66   state of.** aff
8a370 61 69 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e  airs, of course.
8a380 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f    The calling ro
8a390 75 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20  utine must link 
8a3a0 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68  the master.** ch
8a3b0 75 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b  unk before invok
8a3c0 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
8a3d0 2c 20 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69  , then must unli
8a3e0 6e 6b 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79  nk the (possibly
8a3f0 0a 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73  .** changed) mas
8a400 74 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74  ter chunk once t
8a410 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20  his routine has 
8a420 66 69 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61  finished..*/.sta
8a430 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
8a440 4d 65 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74  Merge(u32 *pRoot
8a450 29 7b 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20  ){.  u32 iNext, 
8a460 70 72 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78  prev, size, i, x
8a470 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
8a480 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
8a490 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20  mem3.mutex) );. 
8a4a0 20 66 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69   for(i=*pRoot; i
8a4b0 3e 30 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20  >0; i=iNext){.  
8a4c0 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61    iNext = mem3.a
8a4d0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[i].u.list.n
8a4e0 65 78 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20  ext;.    size = 
8a4f0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
8a500 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20  u.hdr.size4x;.  
8a510 20 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 26    assert( (size&
8a520 31 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  1)==0 );.    if(
8a530 20 28 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a   (size&2)==0 ){.
8a540 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c        memsys3Unl
8a550 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70  inkFromList(i, p
8a560 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73  Root);.      ass
8a570 65 72 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50  ert( i > mem3.aP
8a580 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70  ool[i-1].u.hdr.p
8a590 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20  revSize );.     
8a5a0 20 70 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33   prev = i - mem3
8a5b0 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
8a5c0 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20  r.prevSize;.    
8a5d0 20 20 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78    if( prev==iNex
8a5e0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65  t ){.        iNe
8a5f0 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  xt = mem3.aPool[
8a600 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  prev].u.list.nex
8a610 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
8a620 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70   memsys3Unlink(p
8a630 72 65 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65  rev);.      size
8a640 20 3d 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20   = i + size/4 - 
8a650 70 72 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20  prev;.      x = 
8a660 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d  mem3.aPool[prev-
8a670 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8a680 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  & 2;.      mem3.
8a690 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e  aPool[prev-1].u.
8a6a0 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a  hdr.size4x = siz
8a6b0 65 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d  e*4 | x;.      m
8a6c0 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73  em3.aPool[prev+s
8a6d0 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ize-1].u.hdr.pre
8a6e0 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20  vSize = size;.  
8a6f0 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28      memsys3Link(
8a700 70 72 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d  prev);.      i =
8a710 20 70 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65   prev;.    }else
8a720 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20  {.      size /= 
8a730 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  4;.    }.    if(
8a740 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73   size>mem3.szMas
8a750 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ter ){.      mem
8a760 33 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20  3.iMaster = i;. 
8a770 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74       mem3.szMast
8a780 65 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d  er = size;.    }
8a790 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
8a7a0 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20  turn a block of 
8a7b0 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61  memory of at lea
8a7c0 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a  st nBytes in siz
8a7d0 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c  e..** Return NUL
8a7e0 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a  L if unable..**.
8a7f0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
8a800 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
8a810 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65  e necessary mute
8a820 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65  xes, if any, are
8a830 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64  .** already held
8a840 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
8a850 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a  Hence "Unsafe"..
8a860 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
8a870 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73  memsys3MallocUns
8a880 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  afe(int nByte){.
8a890 20 20 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e    u32 i;.  u32 n
8a8a0 42 6c 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46  Block;.  u32 toF
8a8b0 72 65 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ree;..  assert( 
8a8c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
8a8d0 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29  ld(mem3.mutex) )
8a8e0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
8a8f0 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38  of(Mem3Block)==8
8a900 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c   );.  if( nByte<
8a910 3d 31 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63  =12 ){.    nBloc
8a920 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a  k = 2;.  }else{.
8a930 20 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42      nBlock = (nB
8a940 79 74 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d  yte + 11)/8;.  }
8a950 0a 20 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63  .  assert( nBloc
8a960 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54  k>=2 );..  /* ST
8a970 45 50 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20  EP 1:.  ** Look 
8a980 66 6f 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20  for an entry of 
8a990 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65  the correct size
8a9a0 20 69 6e 20 65 69 74 68 65 72 20 74 68 65 20 73   in either the s
8a9b0 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20  mall.  ** chunk 
8a9c0 74 61 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20  table or in the 
8a9d0 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68  large chunk hash
8a9e0 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73   table.  This is
8a9f0 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c  .  ** successful
8aa00 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d   most of the tim
8aa10 65 20 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73  e (about 9 times
8aa20 20 6f 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a   out of 10)..  *
8aa30 2f 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c  /.  if( nBlock <
8aa40 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20  = MX_SMALL ){.  
8aa50 20 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61    i = mem3.aiSma
8aa60 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20  ll[nBlock-2];.  
8aa70 20 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20    if( i>0 ){.   
8aa80 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
8aa90 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d  FromList(i, &mem
8aaa0 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b  3.aiSmall[nBlock
8aab0 2d 32 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75  -2]);.      retu
8aac0 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f  rn memsys3Checko
8aad0 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20  ut(i, nBlock);. 
8aae0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
8aaf0 20 20 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c    int hash = nBl
8ab00 6f 63 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20  ock % N_HASH;.  
8ab10 20 20 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48    for(i=mem3.aiH
8ab20 61 73 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20  ash[hash]; i>0; 
8ab30 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e  i=mem3.aPool[i].
8ab40 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20  u.list.next){.  
8ab50 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f      if( mem3.aPo
8ab60 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
8ab70 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29  ze4x/4==nBlock )
8ab80 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73  {.        memsys
8ab90 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
8aba0 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b  i, &mem3.aiHash[
8abb0 68 61 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20  hash]);.        
8abc0 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68  return memsys3Ch
8abd0 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b  eckout(i, nBlock
8abe0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
8abf0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20  .  }..  /* STEP 
8ac00 32 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73  2:.  ** Try to s
8ac10 61 74 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63  atisfy the alloc
8ac20 61 74 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67  ation by carving
8ac30 20 61 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20   a piece off of 
8ac40 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20  the end.  ** of 
8ac50 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b  the master chunk
8ac60 2e 20 20 54 68 69 73 20 73 74 65 70 20 75 73 75  .  This step usu
8ac70 61 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74  ally works if st
8ac80 65 70 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f  ep 1 fails..  */
8ac90 0a 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61  .  if( mem3.szMa
8aca0 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a  ster>=nBlock ){.
8acb0 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79      return memsy
8acc0 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c  s3FromMaster(nBl
8acd0 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  ock);.  }...  /*
8ace0 20 53 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20   STEP 3:  .  ** 
8acf0 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  Loop through the
8ad00 20 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70   entire memory p
8ad10 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61  ool.  Coalesce a
8ad20 64 6a 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a  djacent free.  *
8ad30 2a 20 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d  * chunks.  Recom
8ad40 70 75 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  pute the master 
8ad50 63 68 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72  chunk as the lar
8ad60 67 65 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e  gest free chunk.
8ad70 0a 20 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61  .  ** Then try a
8ad80 67 61 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20  gain to satisfy 
8ad90 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62  the allocation b
8ada0 79 20 63 61 72 76 69 6e 67 20 61 20 70 69 65 63  y carving a piec
8adb0 65 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68  e off.  ** of th
8adc0 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73  e end of the mas
8add0 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73  ter chunk.  This
8ade0 20 73 74 65 70 20 68 61 70 70 65 6e 73 20 76 65   step happens ve
8adf0 72 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28  ry.  ** rarely (
8ae00 77 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20  we hope!).  */. 
8ae10 20 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f   for(toFree=nBlo
8ae20 63 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d  ck*16; toFree<(m
8ae30 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74  em3.nPool*16); t
8ae40 6f 46 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20  oFree *= 2){.   
8ae50 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d   memsys3OutOfMem
8ae60 6f 72 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20  ory(toFree);.   
8ae70 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65   if( mem3.iMaste
8ae80 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79  r ){.      memsy
8ae90 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73  s3Link(mem3.iMas
8aea0 74 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33  ter);.      mem3
8aeb0 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  .iMaster = 0;.  
8aec0 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65      mem3.szMaste
8aed0 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  r = 0;.    }.   
8aee0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41   for(i=0; i<N_HA
8aef0 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  SH; i++){.      
8af00 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65  memsys3Merge(&me
8af10 6d 33 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20  m3.aiHash[i]);. 
8af20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30     }.    for(i=0
8af30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20  ; i<MX_SMALL-1; 
8af40 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73  i++){.      mems
8af50 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61  ys3Merge(&mem3.a
8af60 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20  iSmall[i]);.    
8af70 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73  }.    if( mem3.s
8af80 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20  zMaster ){.     
8af90 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d   memsys3Unlink(m
8afa0 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20  em3.iMaster);.  
8afb0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d      if( mem3.szM
8afc0 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b  aster>=nBlock ){
8afd0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
8afe0 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65  memsys3FromMaste
8aff0 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20  r(nBlock);.     
8b000 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
8b010 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68  /* If none of th
8b020 65 20 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20  e above worked, 
8b030 74 68 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f  then we fail. */
8b040 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
8b050 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75  /*.** Free an ou
8b060 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79  tstanding memory
8b070 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a   allocation..**.
8b080 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
8b090 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
8b0a0 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65  e necessary mute
8b0b0 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65  xes, if any, are
8b0c0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64  .** already held
8b0d0 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
8b0e0 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a  Hence "Unsafe"..
8b0f0 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46  */.void memsys3F
8b100 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a  reeUnsafe(void *
8b110 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f  pOld){.  Mem3Blo
8b120 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f  ck *p = (Mem3Blo
8b130 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20  ck*)pOld;.  int 
8b140 69 3b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78  i;.  u32 size, x
8b150 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
8b160 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
8b170 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em3.mutex) );.  
8b180 61 73 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61  assert( p>mem3.a
8b190 50 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e  Pool && p<&mem3.
8b1a0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c  aPool[mem3.nPool
8b1b0 5d 20 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d  ] );.  i = p - m
8b1c0 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73  em3.aPool;.  ass
8b1d0 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c  ert( (mem3.aPool
8b1e0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
8b1f0 34 78 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69  4x&1)==1 );.  si
8b200 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
8b210 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
8b220 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69  x/4;.  assert( i
8b230 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f  +size<=mem3.nPoo
8b240 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50  l+1 );.  mem3.aP
8b250 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
8b260 69 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d  ize4x &= ~1;.  m
8b270 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65  em3.aPool[i+size
8b280 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
8b290 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d  ze = size;.  mem
8b2a0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31  3.aPool[i+size-1
8b2b0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26  ].u.hdr.size4x &
8b2c0 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c  = ~2;.  memsys3L
8b2d0 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72  ink(i);..  /* Tr
8b2e0 79 20 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20  y to expand the 
8b2f0 6d 61 73 74 65 72 20 75 73 69 6e 67 20 74 68 65  master using the
8b300 20 6e 65 77 6c 79 20 66 72 65 65 64 20 63 68 75   newly freed chu
8b310 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33  nk */.  if( mem3
8b320 2e 69 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  .iMaster ){.    
8b330 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f  while( (mem3.aPo
8b340 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d  ol[mem3.iMaster-
8b350 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  1].u.hdr.size4x&
8b360 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  2)==0 ){.      s
8b370 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ize = mem3.aPool
8b380 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d  [mem3.iMaster-1]
8b390 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b  .u.hdr.prevSize;
8b3a0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73  .      mem3.iMas
8b3b0 74 65 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20  ter -= size;.   
8b3c0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72     mem3.szMaster
8b3d0 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20   += size;.      
8b3e0 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65  memsys3Unlink(me
8b3f0 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20  m3.iMaster);.   
8b400 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f     x = mem3.aPoo
8b410 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31  l[mem3.iMaster-1
8b420 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26  ].u.hdr.size4x &
8b430 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61   2;.      mem3.a
8b440 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8b450 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
8b460 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  x = mem3.szMaste
8b470 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d  r*4 | x;.      m
8b480 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8b490 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61  Master+mem3.szMa
8b4a0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  ster-1].u.hdr.pr
8b4b0 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a  evSize = mem3.sz
8b4c0 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20  Master;.    }.  
8b4d0 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c    x = mem3.aPool
8b4e0 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d  [mem3.iMaster-1]
8b4f0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20  .u.hdr.size4x & 
8b500 32 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d  2;.    while( (m
8b510 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8b520 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61  Master+mem3.szMa
8b530 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
8b540 7a 65 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20  ze4x&1)==0 ){.  
8b550 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
8b560 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d  k(mem3.iMaster+m
8b570 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20  em3.szMaster);. 
8b580 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74       mem3.szMast
8b590 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c  er += mem3.aPool
8b5a0 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65  [mem3.iMaster+me
8b5b0 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75  m3.szMaster-1].u
8b5c0 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20  .hdr.size4x/4;. 
8b5d0 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b       mem3.aPool[
8b5e0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
8b5f0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d  u.hdr.size4x = m
8b600 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c  em3.szMaster*4 |
8b610 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61   x;.      mem3.a
8b620 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8b630 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d  r+mem3.szMaster-
8b640 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
8b650 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  e = mem3.szMaste
8b660 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  r;.    }.  }.}..
8b670 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
8b680 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73   size of an outs
8b690 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69  tanding allocati
8b6a0 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54  on, in bytes.  T
8b6b0 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72  he.** size retur
8b6c0 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d  ned omits the 8-
8b6d0 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 65 72  byte header over
8b6e0 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79  head.  This only
8b6f0 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68  .** works for ch
8b700 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 63 75  unks that are cu
8b710 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20  rrently checked 
8b720 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  out..*/.static i
8b730 6e 74 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76  nt memsys3Size(v
8b740 6f 69 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42  oid *p){.  Mem3B
8b750 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20  lock *pBlock;.  
8b760 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
8b770 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20  n 0;.  pBlock = 
8b780 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20  (Mem3Block*)p;. 
8b790 20 61 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b   assert( (pBlock
8b7a0 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  [-1].u.hdr.size4
8b7b0 78 26 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74  x&1)!=0 );.  ret
8b7c0 75 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e  urn (pBlock[-1].
8b7d0 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29  u.hdr.size4x&~3)
8b7e0 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  *2 - 4;.}../*.**
8b7f0 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75   Round up a requ
8b800 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20  est size to the 
8b810 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63  next valid alloc
8b820 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73  ation size..*/.s
8b830 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
8b840 33 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b  3Roundup(int n){
8b850 0a 20 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a  .  if( n<=12 ){.
8b860 20 20 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20      return 12;. 
8b870 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
8b880 72 6e 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d  rn ((n+11)&~7) -
8b890 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   4;.  }.}../*.**
8b8a0 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73   Allocate nBytes
8b8b0 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73   of memory..*/.s
8b8c0 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73  tatic void *mems
8b8d0 79 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42  ys3Malloc(int nB
8b8e0 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  ytes){.  sqlite3
8b8f0 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73  _int64 *p;.  ass
8b900 65 72 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b  ert( nBytes>0 );
8b910 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c            /* mal
8b920 6c 6f 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75  loc.c filters ou
8b930 74 20 30 20 62 79 74 65 20 72 65 71 75 65 73 74  t 0 byte request
8b940 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e  s */.  memsys3En
8b950 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d  ter();.  p = mem
8b960 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65  sys3MallocUnsafe
8b970 28 6e 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73  (nBytes);.  mems
8b980 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65  ys3Leave();.  re
8b990 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a  turn (void*)p; .
8b9a0 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65  }../*.** Free me
8b9b0 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65  mory..*/.void me
8b9c0 6d 73 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a  msys3Free(void *
8b9d0 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72  pPrior){.  asser
8b9e0 74 28 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d  t( pPrior );.  m
8b9f0 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20  emsys3Enter();. 
8ba00 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61   memsys3FreeUnsa
8ba10 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65  fe(pPrior);.  me
8ba20 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a  msys3Leave();.}.
8ba30 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
8ba40 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
8ba50 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
8ba60 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20  ocation.*/.void 
8ba70 2a 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28  *memsys3Realloc(
8ba80 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e  void *pPrior, in
8ba90 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74  t nBytes){.  int
8baa0 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70   nOld;.  void *p
8bab0 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d  ;.  if( pPrior==
8bac0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
8bad0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
8bae0 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66  Bytes);.  }.  if
8baf0 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20  ( nBytes<=0 ){. 
8bb00 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
8bb10 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74  pPrior);.    ret
8bb20 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c  urn 0;.  }.  nOl
8bb30 64 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28  d = memsys3Size(
8bb40 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e  pPrior);.  if( n
8bb50 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e  Bytes<=nOld && n
8bb60 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20  Bytes>=nOld-128 
8bb70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50  ){.    return pP
8bb80 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  rior;.  }.  mems
8bb90 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20  ys3Enter();.  p 
8bba0 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55  = memsys3MallocU
8bbb0 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20  nsafe(nBytes);. 
8bbc0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66   if( p ){.    if
8bbd0 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b  ( nOld<nBytes ){
8bbe0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c  .      memcpy(p,
8bbf0 20 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a   pPrior, nOld);.
8bc00 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8bc10 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f   memcpy(p, pPrio
8bc20 72 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  r, nBytes);.    
8bc30 7d 0a 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65  }.    memsys3Fre
8bc40 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b  eUnsafe(pPrior);
8bc50 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65  .  }.  memsys3Le
8bc60 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20  ave();.  return 
8bc70 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  p;.}../*.** Init
8bc80 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75  ialize this modu
8bc90 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
8bca0 74 20 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f  t memsys3Init(vo
8bcb0 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  id *NotUsed){.  
8bcc0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
8bcd0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28  (NotUsed);.  if(
8bce0 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   !sqlite3GlobalC
8bcf0 6f 6e 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20  onfig.pHeap ){. 
8bd00 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
8bd10 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f  _ERROR;.  }..  /
8bd20 2a 20 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65  * Store a pointe
8bd30 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  r to the memory 
8bd40 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20  block in global 
8bd50 73 74 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20  structure mem3. 
8bd60 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  */.  assert( siz
8bd70 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d  eof(Mem3Block)==
8bd80 38 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  8 );.  mem3.aPoo
8bd90 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a  l = (Mem3Block *
8bda0 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  )sqlite3GlobalCo
8bdb0 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65  nfig.pHeap;.  me
8bdc0 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69  m3.nPool = (sqli
8bdd0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
8bde0 6e 48 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d  nHeap / sizeof(M
8bdf0 65 6d 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a  em3Block)) - 2;.
8be00 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
8be10 20 74 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63   the master bloc
8be20 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d  k. */.  mem3.szM
8be30 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f  aster = mem3.nPo
8be40 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73  ol;.  mem3.mnMas
8be50 74 65 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  ter = mem3.szMas
8be60 74 65 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73  ter;.  mem3.iMas
8be70 74 65 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e  ter = 1;.  mem3.
8be80 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73  aPool[0].u.hdr.s
8be90 69 7a 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a  ize4x = (mem3.sz
8bea0 4d 61 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a  Master<<2) + 2;.
8beb0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d    mem3.aPool[mem
8bec0 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70  3.nPool].u.hdr.p
8bed0 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e  revSize = mem3.n
8bee0 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f  Pool;.  mem3.aPo
8bef0 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75  ol[mem3.nPool].u
8bf00 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b  .hdr.size4x = 1;
8bf10 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
8bf20 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
8bf30 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73  einitialize this
8bf40 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74   module..*/.stat
8bf50 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53  ic void memsys3S
8bf60 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f  hutdown(void *No
8bf70 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
8bf80 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
8bf90 65 64 29 3b 0a 20 20 6d 65 6d 33 2e 6d 75 74 65  ed);.  mem3.mute
8bfa0 78 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 3b  x = 0;.  return;
8bfb0 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  .}..../*.** Open
8bfc0 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61   the file indica
8bfd0 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20  ted and write a 
8bfe0 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65  log of all unfre
8bff0 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c  ed memory .** al
8c000 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74  locations into t
8c010 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49  hat log..*/.SQLI
8c020 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
8c030 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 33 44 75  sqlite3Memsys3Du
8c040 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  mp(const char *z
8c050 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 69 66 64 65  Filename){.#ifde
8c060 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
8c070 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 75 33   FILE *out;.  u3
8c080 32 20 69 2c 20 6a 3b 0a 20 20 75 33 32 20 73 69  2 i, j;.  u32 si
8c090 7a 65 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  ze;.  if( zFilen
8c0a0 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
8c0b0 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  ame[0]==0 ){.   
8c0c0 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20   out = stdout;. 
8c0d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20   }else{.    out 
8c0e0 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  = fopen(zFilenam
8c0f0 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28  e, "w");.    if(
8c100 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   out==0 ){.     
8c110 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
8c120 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
8c130 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
8c140 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
8c150 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20  s **\n",.       
8c160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
8c170 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Filename);.     
8c180 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
8c190 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65   }.  memsys3Ente
8c1a0 72 28 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  r();.  fprintf(o
8c1b0 75 74 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e 22 29  ut, "CHUNKS:\n")
8c1c0 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d  ;.  for(i=1; i<=
8c1d0 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73  mem3.nPool; i+=s
8c1e0 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69 7a 65  ize/4){.    size
8c1f0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
8c200 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b  1].u.hdr.size4x;
8c210 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f 34 3c  .    if( size/4<
8c220 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69  =1 ){.      fpri
8c230 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73 69 7a  ntf(out, "%p siz
8c240 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d  e error\n", &mem
8c250 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20  3.aPool[i]);.   
8c260 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
8c270 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
8c280 20 7d 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65   }.    if( (size
8c290 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 61  &1)==0 && mem3.a
8c2a0 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d  Pool[i+size/4-1]
8c2b0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 21  .u.hdr.prevSize!
8c2c0 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20  =size/4 ){.     
8c2d0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
8c2e0 70 20 74 61 69 6c 20 73 69 7a 65 20 64 6f 65 73  p tail size does
8c2f0 20 6e 6f 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26   not match\n", &
8c300 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a  mem3.aPool[i]);.
8c310 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20        assert( 0 
8c320 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
8c330 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 28      }.    if( ((
8c340 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a  mem3.aPool[i+siz
8c350 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  e/4-1].u.hdr.siz
8c360 65 34 78 26 32 29 3e 3e 31 29 21 3d 28 73 69 7a  e4x&2)>>1)!=(siz
8c370 65 26 31 29 20 29 7b 0a 20 20 20 20 20 20 66 70  e&1) ){.      fp
8c380 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74  rintf(out, "%p t
8c390 61 69 6c 20 63 68 65 63 6b 6f 75 74 20 62 69 74  ail checkout bit
8c3a0 20 69 73 20 69 6e 63 6f 72 72 65 63 74 5c 6e 22   is incorrect\n"
8c3b0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  , &mem3.aPool[i]
8c3c0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
8c3d0 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61   0 );.      brea
8c3e0 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
8c3f0 20 73 69 7a 65 26 31 20 29 7b 0a 20 20 20 20 20   size&1 ){.     
8c400 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
8c410 70 20 25 36 64 20 62 79 74 65 73 20 63 68 65 63  p %6d bytes chec
8c420 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d  ked out\n", &mem
8c430 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a  3.aPool[i], (siz
8c440 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d  e/4)*8-8);.    }
8c450 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70 72 69  else{.      fpri
8c460 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64  ntf(out, "%p %6d
8c470 20 62 79 74 65 73 20 66 72 65 65 25 73 5c 6e 22   bytes free%s\n"
8c480 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  , &mem3.aPool[i]
8c490 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a  , (size/4)*8-8,.
8c4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8c4b0 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74 65    i==mem3.iMaste
8c4c0 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72 2a 2a  r ? " **master**
8c4d0 22 20 3a 20 22 22 29 3b 0a 20 20 20 20 7d 0a 20  " : "");.    }. 
8c4e0 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
8c4f0 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29  MX_SMALL-1; i++)
8c500 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61  {.    if( mem3.a
8c510 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63  iSmall[i]==0 ) c
8c520 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72  ontinue;.    fpr
8c530 69 6e 74 66 28 6f 75 74 2c 20 22 73 6d 61 6c 6c  intf(out, "small
8c540 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20  (%2d):", i);.   
8c550 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69   for(j = mem3.ai
8c560 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a  Small[i]; j>0; j
8c570 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75  =mem3.aPool[j].u
8c580 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20  .list.next){.   
8c590 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
8c5a0 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33  " %p(%d)", &mem3
8c5b0 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20  .aPool[j],.     
8c5c0 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61           (mem3.a
8c5d0 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[j-1].u.hdr.
8c5e0 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a  size4x/4)*8-8);.
8c5f0 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74      }.    fprint
8c600 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20  f(out, "\n"); . 
8c610 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
8c620 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20  N_HASH; i++){.  
8c630 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 48 61 73    if( mem3.aiHas
8c640 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  h[i]==0 ) contin
8c650 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  ue;.    fprintf(
8c660 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64 29 3a  out, "hash(%2d):
8c670 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a  ", i);.    for(j
8c680 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69   = mem3.aiHash[i
8c690 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61  ]; j>0; j=mem3.a
8c6a0 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[j].u.list.n
8c6b0 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69  ext){.      fpri
8c6c0 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64  ntf(out, " %p(%d
8c6d0 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  )", &mem3.aPool[
8c6e0 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  j],.            
8c6f0 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d    (mem3.aPool[j-
8c700 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
8c710 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20  4)*8-8);.    }. 
8c720 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
8c730 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70  "\n"); .  }.  fp
8c740 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 61 73 74  rintf(out, "mast
8c750 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69  er=%d\n", mem3.i
8c760 4d 61 73 74 65 72 29 3b 0a 20 20 66 70 72 69 6e  Master);.  fprin
8c770 74 66 28 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64  tf(out, "nowUsed
8c780 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f  =%d\n", mem3.nPo
8c790 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61  ol*8 - mem3.szMa
8c7a0 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72 69 6e  ster*8);.  fprin
8c7b0 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65 64 3d  tf(out, "mxUsed=
8c7c0 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f  %d\n", mem3.nPoo
8c7d0 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73  l*8 - mem3.mnMas
8c7e0 74 65 72 2a 38 29 3b 0a 20 20 73 71 6c 69 74 65  ter*8);.  sqlite
8c7f0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
8c800 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  m3.mutex);.  if(
8c810 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a   out==stdout ){.
8c820 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75      fflush(stdou
8c830 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
8c840 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20   fclose(out);.  
8c850 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  }.#else.  UNUSED
8c860 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69 6c 65  _PARAMETER(zFile
8c870 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  name);.#endif.}.
8c880 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
8c890 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  ine is the only 
8c8a0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
8c8b0 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e  file with extern
8c8c0 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a  al .** linkage..
8c8d0 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74  **.** Populate t
8c8e0 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  he low-level mem
8c8f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
8c900 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73  unction pointers
8c910 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c   in.** sqlite3Gl
8c920 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74  obalConfig.m wit
8c930 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  h pointers to th
8c940 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  e routines in th
8c950 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a 2a 20  is file. The.** 
8c960 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66  arguments specif
8c970 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d  y the block of m
8c980 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65 2e  emory to manage.
8c990 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
8c9a0 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c  ine is only call
8c9b0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f  ed by sqlite3_co
8c9c0 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68 65 72  nfig(), and ther
8c9d0 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 20  efore.** is not 
8c9e0 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20 74  required to be t
8c9f0 68 72 65 61 64 73 61 66 65 20 28 69 74 20 69 73  hreadsafe (it is
8ca00 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45   not)..*/.SQLITE
8ca10 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
8ca20 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
8ca30 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65  ds *sqlite3MemGe
8ca40 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 7b 0a  tMemsys3(void){.
8ca50 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
8ca60 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
8ca70 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64  ds mempoolMethod
8ca80 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79  s = {.     memsy
8ca90 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d  s3Malloc,.     m
8caa0 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20 20 20  emsys3Free,.    
8cab0 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 2c   memsys3Realloc,
8cac0 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 69 7a  .     memsys3Siz
8cad0 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52  e,.     memsys3R
8cae0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d  oundup,.     mem
8caf0 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20 20 6d  sys3Init,.     m
8cb00 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e 2c 0a  emsys3Shutdown,.
8cb10 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65       0.  };.  re
8cb20 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74  turn &mempoolMet
8cb30 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  hods;.}..#endif 
8cb40 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  /* SQLITE_ENABLE
8cb50 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a  _MEMSYS3 */../**
8cb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
8cb70 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a   of mem3.c *****
8cb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
8cbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
8cbc0 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a  in file mem5.c *
8cbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
8cc00 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20  ** 2007 October 
8cc10 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  14.**.** The aut
8cc20 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
8cc30 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
8cc40 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
8cc50 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
8cc60 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
8cc70 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
8cc80 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
8cc90 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
8cca0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
8ccb0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
8ccc0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
8ccd0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
8cce0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
8ccf0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
8cd00 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
8cd10 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
8cd20 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
8cd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cd70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
8cd80 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75  ontains the C fu
8cd90 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70  nctions that imp
8cda0 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a  lement a memory.
8cdb0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ** allocation su
8cdc0 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20  bsystem for use 
8cdd0 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a  by SQLite. .**.*
8cde0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
8cdf0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
8ce00 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
8ce10 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75  m omits all.** u
8ce20 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20  se of malloc(). 
8ce30 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
8ce40 67 69 76 65 73 20 53 51 4c 69 74 65 20 61 20 62  gives SQLite a b
8ce50 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a  lock of memory.*
8ce60 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  * before calling
8ce70 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
8ce80 69 7a 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68  ize() from which
8ce90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20   allocations.** 
8cea0 61 72 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74  are made and ret
8ceb0 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61  urned by the xMa
8cec0 6c 6c 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c  lloc() and xReal
8ced0 6c 6f 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d  loc() .** implem
8cee0 65 6e 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20  entations. Once 
8cef0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
8cf00 7a 65 28 29 20 68 61 73 20 62 65 65 6e 20 63 61  ze() has been ca
8cf10 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f  lled,.** the amo
8cf20 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76  unt of memory av
8cf30 61 69 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74  ailable to SQLit
8cf40 65 20 69 73 20 66 69 78 65 64 20 61 6e 64 20 63  e is fixed and c
8cf50 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e  annot.** be chan
8cf60 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ged..**.** This 
8cf70 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
8cf80 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
8cf90 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e   subsystem is in
8cfa0 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65  cluded.** in the
8cfb0 20 62 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53   build only if S
8cfc0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
8cfd0 53 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e  SYS5 is defined.
8cfe0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 6d 6f  .**.** This memo
8cff0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 75 73 65  ry allocator use
8d000 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  s the following 
8d010 61 6c 67 6f 72 69 74 68 6d 3a 0a 2a 2a 0a 2a 2a  algorithm:.**.**
8d020 20 20 20 31 2e 20 20 41 6c 6c 20 6d 65 6d 6f 72     1.  All memor
8d030 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 73 69  y allocations si
8d040 7a 65 73 20 61 72 65 20 72 6f 75 6e 64 65 64 20  zes are rounded 
8d050 75 70 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66  up to a power of
8d060 20 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20   2..**.**   2.  
8d070 49 66 20 74 77 6f 20 61 64 6a 61 63 65 6e 74 20  If two adjacent 
8d080 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 72 65 20  free blocks are 
8d090 74 68 65 20 68 61 6c 76 65 73 20 6f 66 20 61 20  the halves of a 
8d0a0 6c 61 72 67 65 72 20 62 6c 6f 63 6b 2c 0a 2a 2a  larger block,.**
8d0b0 20 20 20 20 20 20 20 74 68 65 6e 20 74 68 65 20         then the 
8d0c0 74 77 6f 20 62 6c 6f 63 6b 73 20 61 72 65 20 63  two blocks are c
8d0d0 6f 61 6c 65 73 65 64 20 69 6e 74 6f 20 74 68 65  oalesed into the
8d0e0 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 72 20 62   single larger b
8d0f0 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e  lock..**.**   3.
8d100 20 20 4e 65 77 20 6d 65 6d 6f 72 79 20 69 73 20    New memory is 
8d110 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 20 74  allocated from t
8d120 68 65 20 66 69 72 73 74 20 61 76 61 69 6c 61 62  he first availab
8d130 6c 65 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a  le free block..*
8d140 2a 0a 2a 2a 20 54 68 69 73 20 61 6c 67 6f 72 69  *.** This algori
8d150 74 68 6d 20 69 73 20 64 65 73 63 72 69 62 65 64  thm is described
8d160 20 69 6e 3a 20 4a 2e 20 4d 2e 20 52 6f 62 73 6f   in: J. M. Robso
8d170 6e 2e 20 22 42 6f 75 6e 64 73 20 66 6f 72 20 53  n. "Bounds for S
8d180 6f 6d 65 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ome Functions.**
8d190 20 43 6f 6e 63 65 72 6e 69 6e 67 20 44 79 6e 61   Concerning Dyna
8d1a0 6d 69 63 20 53 74 6f 72 61 67 65 20 41 6c 6c 6f  mic Storage Allo
8d1b0 63 61 74 69 6f 6e 22 2e 20 4a 6f 75 72 6e 61 6c  cation". Journal
8d1c0 20 6f 66 20 74 68 65 20 41 73 73 6f 63 69 61 74   of the Associat
8d1d0 69 6f 6e 20 66 6f 72 0a 2a 2a 20 43 6f 6d 70 75  ion for.** Compu
8d1e0 74 69 6e 67 20 4d 61 63 68 69 6e 65 72 79 2c 20  ting Machinery, 
8d1f0 56 6f 6c 75 6d 65 20 32 31 2c 20 4e 75 6d 62 65  Volume 21, Numbe
8d200 72 20 38 2c 20 4a 75 6c 79 20 31 39 37 34 2c 20  r 8, July 1974, 
8d210 70 61 67 65 73 20 34 39 31 2d 34 39 39 2e 0a 2a  pages 491-499..*
8d220 2a 20 0a 2a 2a 20 4c 65 74 20 6e 20 62 65 20 74  * .** Let n be t
8d230 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c  he size of the l
8d240 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f  argest allocatio
8d250 6e 20 64 69 76 69 64 65 64 20 62 79 20 74 68 65  n divided by the
8d260 20 6d 69 6e 69 6d 75 6d 0a 2a 2a 20 61 6c 6c 6f   minimum.** allo
8d270 63 61 74 69 6f 6e 20 73 69 7a 65 20 28 61 66 74  cation size (aft
8d280 65 72 20 72 6f 75 6e 64 69 6e 67 20 61 6c 6c 20  er rounding all 
8d290 73 69 7a 65 73 20 75 70 20 74 6f 20 61 20 70 6f  sizes up to a po
8d2a0 77 65 72 20 6f 66 20 32 2e 29 20 20 4c 65 74 20  wer of 2.)  Let 
8d2b0 4d 0a 2a 2a 20 62 65 20 74 68 65 20 6d 61 78 69  M.** be the maxi
8d2c0 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  mum amount of me
8d2d0 6d 6f 72 79 20 65 76 65 72 20 6f 75 74 73 74 61  mory ever outsta
8d2e0 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d  nding at one tim
8d2f0 65 2e 20 20 4c 65 74 0a 2a 2a 20 4e 20 62 65 20  e.  Let.** N be 
8d300 74 68 65 20 74 6f 74 61 6c 20 61 6d 6f 75 6e 74  the total amount
8d310 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c   of memory avail
8d320 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74  able for allocat
8d330 69 6f 6e 2e 20 20 52 6f 62 73 6f 6e 0a 2a 2a 20  ion.  Robson.** 
8d340 70 72 6f 76 65 64 20 74 68 61 74 20 74 68 69 73  proved that this
8d350 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
8d360 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 72 65  r will never bre
8d370 61 6b 64 6f 77 6e 20 64 75 65 20 74 6f 20 0a 2a  akdown due to .*
8d380 2a 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20  * fragmentation 
8d390 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 66  as long as the f
8d3a0 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 72 61  ollowing constra
8d3b0 69 6e 74 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a  int holds:.**.**
8d3c0 20 20 20 20 20 20 4e 20 3e 3d 20 20 4d 2a 28 31        N >=  M*(1
8d3d0 20 2b 20 6c 6f 67 32 28 6e 29 2f 32 29 20 2d 20   + log2(n)/2) - 
8d3e0 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  n + 1.**.** The 
8d3f0 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29  sqlite3_status()
8d400 20 6c 6f 67 69 63 20 74 72 61 63 6b 73 20 74 68   logic tracks th
8d410 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 73  e maximum values
8d420 20 6f 66 20 6e 20 61 6e 64 20 4d 20 73 6f 0a 2a   of n and M so.*
8d430 2a 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63  * that an applic
8d440 61 74 69 6f 6e 20 63 61 6e 2c 20 61 74 20 61 6e  ation can, at an
8d450 79 20 74 69 6d 65 2c 20 76 65 72 69 66 79 20 74  y time, verify t
8d460 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a  his constraint..
8d470 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76  */../*.** This v
8d480 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65  ersion of the me
8d490 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69  mory allocator i
8d4a0 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e  s used only when
8d4b0 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42   .** SQLITE_ENAB
8d4c0 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65  LE_MEMSYS5 is de
8d4d0 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  fined..*/.#ifdef
8d4e0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
8d4f0 45 4d 53 59 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20  EMSYS5../*.** A 
8d500 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69  minimum allocati
8d510 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  on is an instanc
8d520 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
8d530 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
8d540 20 4c 61 72 67 65 72 20 61 6c 6c 6f 63 61 74 69   Larger allocati
8d550 6f 6e 73 20 61 72 65 20 61 6e 20 61 72 72 61 79  ons are an array
8d560 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74   of these struct
8d570 75 72 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a  ures where the.*
8d580 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72  * size of the ar
8d590 72 61 79 20 69 73 20 61 20 70 6f 77 65 72 20 6f  ray is a power o
8d5a0 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  f 2..**.** The s
8d5b0 69 7a 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  ize of this obje
8d5c0 63 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77  ct must be a pow
8d5d0 65 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74  er of two.  That
8d5e0 20 66 61 63 74 20 69 73 0a 2a 2a 20 76 65 72 69   fact is.** veri
8d5f0 66 69 65 64 20 69 6e 20 6d 65 6d 73 79 73 35 49  fied in memsys5I
8d600 6e 69 74 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65  nit()..*/.typede
8d610 66 20 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e  f struct Mem5Lin
8d620 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75  k Mem5Link;.stru
8d630 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20  ct Mem5Link {.  
8d640 69 6e 74 20 6e 65 78 74 3b 20 20 20 20 20 20 20  int next;       
8d650 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74  /* Index of next
8d660 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20   free chunk */. 
8d670 20 69 6e 74 20 70 72 65 76 3b 20 20 20 20 20 20   int prev;      
8d680 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 70 72 65   /* Index of pre
8d690 76 69 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b  vious free chunk
8d6a0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61   */.};../*.** Ma
8d6b0 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e  ximum size of an
8d6c0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  y allocation is 
8d6d0 28 28 31 3c 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d  ((1<<LOGMAX)*mem
8d6e0 35 2e 73 7a 41 74 6f 6d 29 2e 20 53 69 6e 63 65  5.szAtom). Since
8d6f0 0a 2a 2a 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20  .** mem5.szAtom 
8d700 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65 61  is always at lea
8d710 73 74 20 38 20 61 6e 64 20 33 32 2d 62 69 74 20  st 8 and 32-bit 
8d720 69 6e 74 65 67 65 72 73 20 61 72 65 20 75 73 65  integers are use
8d730 64 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20  d,.** it is not 
8d740 61 63 74 75 61 6c 6c 79 20 70 6f 73 73 69 62 6c  actually possibl
8d750 65 20 74 6f 20 72 65 61 63 68 20 74 68 69 73 20  e to reach this 
8d760 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  limit..*/.#defin
8d770 65 20 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f 2a 0a  e LOGMAX 30../*.
8d780 2a 2a 20 4d 61 73 6b 73 20 75 73 65 64 20 66 6f  ** Masks used fo
8d790 72 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 65  r mem5.aCtrl[] e
8d7a0 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66  lements..*/.#def
8d7b0 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45  ine CTRL_LOGSIZE
8d7c0 20 20 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f 67    0x1f    /* Log
8d7d0 32 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 62  2 Size of this b
8d7e0 6c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20  lock */.#define 
8d7f0 43 54 52 4c 5f 46 52 45 45 20 20 20 20 20 30 78  CTRL_FREE     0x
8d800 32 30 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  20    /* True if
8d810 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 6f 75 74   not checked out
8d820 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f   */../*.** All o
8d830 66 20 74 68 65 20 73 74 61 74 69 63 20 76 61 72  f the static var
8d840 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 74  iables used by t
8d850 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63  his module are c
8d860 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f  ollected.** into
8d870 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74   a single struct
8d880 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d 35 22  ure named "mem5"
8d890 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65  .  This is to ke
8d8a0 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63  ep the.** static
8d8b0 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e   variables organ
8d8c0 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75  ized and to redu
8d8d0 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c  ce namespace pol
8d8e0 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74  lution.** when t
8d8f0 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f  his module is co
8d900 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65  mbined with othe
8d910 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d  r in the amalgam
8d920 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
8d930 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75   SQLITE_WSD stru
8d940 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a  ct Mem5Global {.
8d950 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79    /*.  ** Memory
8d960 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61   available for a
8d970 6c 6c 6f 63 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20  llocation.  */. 
8d980 20 69 6e 74 20 73 7a 41 74 6f 6d 3b 20 20 20 20   int szAtom;    
8d990 20 20 2f 2a 20 53 6d 61 6c 6c 65 73 74 20 70 6f    /* Smallest po
8d9a0 73 73 69 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f  ssible allocatio
8d9b0 6e 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  n in bytes */.  
8d9c0 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20 20 20 20 20  int nBlock;     
8d9d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 7a   /* Number of sz
8d9e0 41 74 6f 6d 20 73 69 7a 65 64 20 62 6c 6f 63 6b  Atom sized block
8d9f0 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a 2f 0a 20 20  s in zPool */.  
8da00 75 38 20 2a 7a 50 6f 6f 6c 3b 20 20 20 20 20 20  u8 *zPool;      
8da10 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c   /* Memory avail
8da20 61 62 6c 65 20 74 6f 20 62 65 20 61 6c 6c 6f 63  able to be alloc
8da30 61 74 65 64 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a  ated */.  .  /*.
8da40 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f    ** Mutex to co
8da50 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20  ntrol access to 
8da60 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
8da70 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
8da80 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
8da90 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20  mutex *mutex;.. 
8daa0 20 2f 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72 6d   /*.  ** Perform
8dab0 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 0a  ance statistics.
8dac0 20 20 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c 6f    */.  u64 nAllo
8dad0 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f  c;         /* To
8dae0 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 61  tal number of ca
8daf0 6c 6c 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f  lls to malloc */
8db00 0a 20 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c 6f  .  u64 totalAllo
8db10 63 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20  c;     /* Total 
8db20 6f 66 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61  of all malloc ca
8db30 6c 6c 73 20 2d 20 69 6e 63 6c 75 64 65 73 20 69  lls - includes i
8db40 6e 74 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f 0a  nternal frag */.
8db50 20 20 75 36 34 20 74 6f 74 61 6c 45 78 63 65 73    u64 totalExces
8db60 73 3b 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69  s;    /* Total i
8db70 6e 74 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74  nternal fragment
8db80 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63  ation */.  u32 c
8db90 75 72 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f  urrentOut;     /
8dba0 2a 20 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f  * Current checko
8dbb0 75 74 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e  ut, including in
8dbc0 74 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61  ternal fragmenta
8dbd0 74 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75  tion */.  u32 cu
8dbe0 72 72 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a  rrentCount;   /*
8dbf0 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20   Current number 
8dc00 6f 66 20 64 69 73 74 69 6e 63 74 20 63 68 65 63  of distinct chec
8dc10 6b 6f 75 74 73 20 2a 2f 0a 20 20 75 33 32 20 6d  kouts */.  u32 m
8dc20 61 78 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f  axOut;         /
8dc30 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e  * Maximum instan
8dc40 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f  taneous currentO
8dc50 75 74 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 43  ut */.  u32 maxC
8dc60 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4d  ount;       /* M
8dc70 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e  aximum instantan
8dc80 65 6f 75 73 20 63 75 72 72 65 6e 74 43 6f 75 6e  eous currentCoun
8dc90 74 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52 65  t */.  u32 maxRe
8dca0 71 75 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61  quest;     /* La
8dcb0 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e  rgest allocation
8dcc0 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 69   (exclusive of i
8dcd0 6e 74 65 72 6e 61 6c 20 66 72 61 67 29 20 2a 2f  nternal frag) */
8dce0 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69  .  .  /*.  ** Li
8dcf0 73 74 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63  sts of free bloc
8dd00 6b 73 2e 20 20 61 69 46 72 65 65 6c 69 73 74 5b  ks.  aiFreelist[
8dd10 30 5d 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  0] is a list of 
8dd20 66 72 65 65 20 62 6c 6f 63 6b 73 20 6f 66 0a 20  free blocks of. 
8dd30 20 2a 2a 20 73 69 7a 65 20 6d 65 6d 35 2e 73 7a   ** size mem5.sz
8dd40 41 74 6f 6d 2e 20 20 61 69 46 72 65 65 6c 69 73  Atom.  aiFreelis
8dd50 74 5b 31 5d 20 68 6f 6c 64 73 20 62 6c 6f 63 6b  t[1] holds block
8dd60 73 20 6f 66 20 73 69 7a 65 20 73 7a 41 74 6f 6d  s of size szAtom
8dd70 2a 32 2e 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20  *2..  ** and so 
8dd80 66 6f 72 74 68 2e 0a 20 20 2a 2f 0a 20 20 69 6e  forth..  */.  in
8dd90 74 20 61 69 46 72 65 65 6c 69 73 74 5b 4c 4f 47  t aiFreelist[LOG
8dda0 4d 41 58 2b 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20  MAX+1];..  /*.  
8ddb0 2a 2a 20 53 70 61 63 65 20 66 6f 72 20 74 72 61  ** Space for tra
8ddc0 63 6b 69 6e 67 20 77 68 69 63 68 20 62 6c 6f 63  cking which bloc
8ddd0 6b 73 20 61 72 65 20 63 68 65 63 6b 65 64 20 6f  ks are checked o
8dde0 75 74 20 61 6e 64 20 74 68 65 20 73 69 7a 65 0a  ut and the size.
8ddf0 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 62 6c 6f    ** of each blo
8de00 63 6b 2e 20 20 4f 6e 65 20 62 79 74 65 20 70 65  ck.  One byte pe
8de10 72 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  r block..  */.  
8de20 75 38 20 2a 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65  u8 *aCtrl;..} me
8de30 6d 35 20 3d 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 0a  m5 = { 0 };../*.
8de40 2a 2a 20 41 63 63 65 73 73 20 74 68 65 20 73 74  ** Access the st
8de50 61 74 69 63 20 76 61 72 69 61 62 6c 65 20 74 68  atic variable th
8de60 72 6f 75 67 68 20 61 20 6d 61 63 72 6f 20 66 6f  rough a macro fo
8de70 72 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53  r SQLITE_OMIT_WS
8de80 44 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 6d 65 6d  D.*/.#define mem
8de90 35 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20  5 GLOBAL(struct 
8dea0 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65 6d 35  Mem5Global, mem5
8deb0 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e  )../*.** Assumin
8dec0 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 69 73 20  g mem5.zPool is 
8ded0 64 69 76 69 64 65 64 20 75 70 20 69 6e 74 6f 20  divided up into 
8dee0 61 6e 20 61 72 72 61 79 20 6f 66 20 4d 65 6d 35  an array of Mem5
8def0 4c 69 6e 6b 0a 2a 2a 20 73 74 72 75 63 74 75 72  Link.** structur
8df00 65 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  es, return a poi
8df10 6e 74 65 72 20 74 6f 20 74 68 65 20 69 64 78 2d  nter to the idx-
8df20 74 68 20 73 75 63 68 20 6c 69 6b 2e 0a 2a 2f 0a  th such lik..*/.
8df30 23 64 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b  #define MEM5LINK
8df40 28 69 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e 6b  (idx) ((Mem5Link
8df50 20 2a 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b   *)(&mem5.zPool[
8df60 28 69 64 78 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f  (idx)*mem5.szAto
8df70 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69  m]))../*.** Unli
8df80 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20  nk the chunk at 
8df90 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72  mem5.aPool[i] fr
8dfa0 6f 6d 20 6c 69 73 74 20 69 74 20 69 73 20 63 75  om list it is cu
8dfb0 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20  rrently.** on.  
8dfc0 49 74 20 73 68 6f 75 6c 64 20 62 65 20 66 6f 75  It should be fou
8dfd0 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 65  nd on mem5.aiFre
8dfe0 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e  elist[iLogsize].
8dff0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8e000 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 6e  memsys5Unlink(in
8e010 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a  t i, int iLogsiz
8e020 65 29 7b 0a 20 20 69 6e 74 20 6e 65 78 74 2c 20  e){.  int next, 
8e030 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28 20  prev;.  assert( 
8e040 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e  i>=0 && i<mem5.n
8e050 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72  Block );.  asser
8e060 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26  t( iLogsize>=0 &
8e070 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d  & iLogsize<=LOGM
8e080 41 58 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  AX );.  assert( 
8e090 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26  (mem5.aCtrl[i] &
8e0a0 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d   CTRL_LOGSIZE)==
8e0b0 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6e  iLogsize );..  n
8e0c0 65 78 74 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69  ext = MEM5LINK(i
8e0d0 29 2d 3e 6e 65 78 74 3b 0a 20 20 70 72 65 76 20  )->next;.  prev 
8e0e0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70  = MEM5LINK(i)->p
8e0f0 72 65 76 3b 0a 20 20 69 66 28 20 70 72 65 76 3c  rev;.  if( prev<
8e100 30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69  0 ){.    mem5.ai
8e110 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a  Freelist[iLogsiz
8e120 65 5d 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c  e] = next;.  }el
8e130 73 65 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b  se{.    MEM5LINK
8e140 28 70 72 65 76 29 2d 3e 6e 65 78 74 20 3d 20 6e  (prev)->next = n
8e150 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  ext;.  }.  if( n
8e160 65 78 74 3e 3d 30 20 29 7b 0a 20 20 20 20 4d 45  ext>=0 ){.    ME
8e170 4d 35 4c 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 72  M5LINK(next)->pr
8e180 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 7d  ev = prev;.  }.}
8e190 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65  ../*.** Link the
8e1a0 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61   chunk at mem5.a
8e1b0 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20  Pool[i] so that 
8e1c0 69 73 20 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69  is on the iLogsi
8e1d0 7a 65 0a 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e  ze.** free list.
8e1e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8e1f0 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20  memsys5Link(int 
8e200 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29  i, int iLogsize)
8e210 7b 0a 20 20 69 6e 74 20 78 3b 0a 20 20 61 73 73  {.  int x;.  ass
8e220 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
8e230 65 78 5f 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 74  ex_held(mem5.mut
8e240 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
8e250 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e   i>=0 && i<mem5.
8e260 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65  nBlock );.  asse
8e270 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20  rt( iLogsize>=0 
8e280 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47  && iLogsize<=LOG
8e290 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 72 74 28  MAX );.  assert(
8e2a0 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20   (mem5.aCtrl[i] 
8e2b0 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d  & CTRL_LOGSIZE)=
8e2c0 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20  =iLogsize );..  
8e2d0 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d  x = MEM5LINK(i)-
8e2e0 3e 6e 65 78 74 20 3d 20 6d 65 6d 35 2e 61 69 46  >next = mem5.aiF
8e2f0 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65  reelist[iLogsize
8e300 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 29  ];.  MEM5LINK(i)
8e310 2d 3e 70 72 65 76 20 3d 20 2d 31 3b 0a 20 20 69  ->prev = -1;.  i
8e320 66 28 20 78 3e 3d 30 20 29 7b 0a 20 20 20 20 61  f( x>=0 ){.    a
8e330 73 73 65 72 74 28 20 78 3c 6d 65 6d 35 2e 6e 42  ssert( x<mem5.nB
8e340 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 4d 45 4d 35  lock );.    MEM5
8e350 4c 49 4e 4b 28 78 29 2d 3e 70 72 65 76 20 3d 20  LINK(x)->prev = 
8e360 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 69  i;.  }.  mem5.ai
8e370 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a  Freelist[iLogsiz
8e380 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  e] = i;.}../*.**
8e390 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f 4d   If the STATIC_M
8e3a0 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20  EM mutex is not 
8e3b0 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62  already held, ob
8e3c0 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65  tain it now. The
8e3d0 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61   mutex.** will a
8e3e0 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64 20 28  lready be held (
8e3f0 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64 65  obtained by code
8e400 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66   in malloc.c) if
8e410 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  .** sqlite3Globa
8e420 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74  lConfig.bMemStat
8e430 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61   is true..*/.sta
8e440 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35  tic void memsys5
8e450 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 73  Enter(void){.  s
8e460 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
8e470 65 72 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a  er(mem5.mutex);.
8e480 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  }.static void me
8e490 6d 73 79 73 35 4c 65 61 76 65 28 76 6f 69 64 29  msys5Leave(void)
8e4a0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
8e4b0 78 5f 6c 65 61 76 65 28 6d 65 6d 35 2e 6d 75 74  x_leave(mem5.mut
8e4c0 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ex);.}../*.** Re
8e4d0 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  turn the size of
8e4e0 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   an outstanding 
8e4f0 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62  allocation, in b
8e500 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69  ytes.  The.** si
8e510 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74  ze returned omit
8e520 73 20 74 68 65 20 38 2d 62 79 74 65 20 68 65 61  s the 8-byte hea
8e530 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54  der overhead.  T
8e540 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b  his only.** work
8e550 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61  s for chunks tha
8e560 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20  t are currently 
8e570 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a  checked out..*/.
8e580 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
8e590 73 35 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b  s5Size(void *p){
8e5a0 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 30  .  int iSize = 0
8e5b0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
8e5c0 20 69 6e 74 20 69 20 3d 20 28 28 75 38 20 2a 29   int i = ((u8 *)
8e5d0 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65  p-mem5.zPool)/me
8e5e0 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 20 20 61  m5.szAtom;.    a
8e5f0 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
8e600 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a  <mem5.nBlock );.
8e610 20 20 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d 35      iSize = mem5
8e620 2e 73 7a 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 20  .szAtom * (1 << 
8e630 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 43  (mem5.aCtrl[i]&C
8e640 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a 20  TRL_LOGSIZE));. 
8e650 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 53 69 7a   }.  return iSiz
8e660 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  e;.}../*.** Find
8e670 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
8e680 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74   on the freelist
8e690 20 69 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c 69   iLogsize.  Unli
8e6a0 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72 79  nk that.** entry
8e6b0 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 73 20   and return its 
8e6c0 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 69  index. .*/.stati
8e6d0 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c  c int memsys5Unl
8e6e0 69 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c 6f  inkFirst(int iLo
8e6f0 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69 3b  gsize){.  int i;
8e700 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0a 0a  .  int iFirst;..
8e710 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69    assert( iLogsi
8e720 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a  ze>=0 && iLogsiz
8e730 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 69  e<=LOGMAX );.  i
8e740 20 3d 20 69 46 69 72 73 74 20 3d 20 6d 65 6d 35   = iFirst = mem5
8e750 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67  .aiFreelist[iLog
8e760 73 69 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74 28  size];.  assert(
8e770 20 69 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20 20   iFirst>=0 );.  
8e780 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20  while( i>0 ){.  
8e790 20 20 69 66 28 20 69 3c 69 46 69 72 73 74 20 29    if( i<iFirst )
8e7a0 20 69 46 69 72 73 74 20 3d 20 69 3b 0a 20 20 20   iFirst = i;.   
8e7b0 20 69 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29   i = MEM5LINK(i)
8e7c0 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65  ->next;.  }.  me
8e7d0 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72  msys5Unlink(iFir
8e7e0 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20  st, iLogsize);. 
8e7f0 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b 0a   return iFirst;.
8e800 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
8e810 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  a block of memor
8e820 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42  y of at least nB
8e830 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a  ytes in size..**
8e840 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
8e850 75 6e 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20  unable.  Return 
8e860 4e 55 4c 4c 20 69 66 20 6e 42 79 74 65 73 3d 3d  NULL if nBytes==
8e870 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  0..**.** The cal
8e880 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74  ler guarantees t
8e890 68 61 74 20 6e 42 79 74 65 20 70 6f 73 69 74 69  hat nByte positi
8e8a0 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  ve..**.** The ca
8e8b0 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65  ller has obtaine
8e8c0 64 20 61 20 6d 75 74 65 78 20 70 72 69 6f 72 20  d a mutex prior 
8e8d0 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73  to invoking this
8e8e0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 6f 20 74  .** routine so t
8e8f0 68 65 72 65 20 69 73 20 6e 65 76 65 72 20 61 6e  here is never an
8e900 79 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 77  y chance that tw
8e910 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 74 68 72  o or more.** thr
8e920 65 61 64 73 20 63 61 6e 20 62 65 20 69 6e 20 74  eads can be in t
8e930 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 20 74  his routine at t
8e940 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f  he same time..*/
8e950 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
8e960 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66  msys5MallocUnsaf
8e970 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  e(int nByte){.  
8e980 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
8e990 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d   /* Index of a m
8e9a0 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74  em5.aPool[] slot
8e9b0 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20   */.  int iBin; 
8e9c0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
8e9d0 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65  into mem5.aiFree
8e9e0 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  list[] */.  int 
8e9f0 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20  iFullSz;     /* 
8ea00 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69  Size of allocati
8ea10 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f  on rounded up to
8ea20 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20   power of 2 */. 
8ea30 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20   int iLogsize;  
8ea40 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75    /* Log2 of iFu
8ea50 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f  llSz/POW2_MIN */
8ea60 0a 0a 20 20 2f 2a 20 6e 42 79 74 65 20 6d 75 73  ..  /* nByte mus
8ea70 74 20 62 65 20 61 20 70 6f 73 69 74 69 76 65 20  t be a positive 
8ea80 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79  */.  assert( nBy
8ea90 74 65 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 4b 65  te>0 );..  /* Ke
8eaa0 65 70 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20  ep track of the 
8eab0 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69  maximum allocati
8eac0 6f 6e 20 72 65 71 75 65 73 74 2e 20 20 45 76 65  on request.  Eve
8ead0 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64 0a 20 20  n unfulfilled.  
8eae0 2a 2a 20 72 65 71 75 65 73 74 73 20 61 72 65 20  ** requests are 
8eaf0 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28  counted */.  if(
8eb00 20 28 75 33 32 29 6e 42 79 74 65 3e 6d 65 6d 35   (u32)nByte>mem5
8eb10 2e 6d 61 78 52 65 71 75 65 73 74 20 29 7b 0a 20  .maxRequest ){. 
8eb20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65     mem5.maxReque
8eb30 73 74 20 3d 20 6e 42 79 74 65 3b 0a 20 20 7d 0a  st = nByte;.  }.
8eb40 0a 20 20 2f 2a 20 41 62 6f 72 74 20 69 66 20 74  .  /* Abort if t
8eb50 68 65 20 72 65 71 75 65 73 74 65 64 20 61 6c 6c  he requested all
8eb60 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 73 20  ocation size is 
8eb70 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
8eb80 6c 61 72 67 65 73 74 0a 20 20 2a 2a 20 70 6f 77  largest.  ** pow
8eb90 65 72 20 6f 66 20 74 77 6f 20 74 68 61 74 20 77  er of two that w
8eba0 65 20 63 61 6e 20 72 65 70 72 65 73 65 6e 74 20  e can represent 
8ebb0 75 73 69 6e 67 20 33 32 2d 62 69 74 20 73 69 67  using 32-bit sig
8ebc0 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 20 20  ned integers..  
8ebd0 2a 2f 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3e  */.  if( nByte >
8ebe0 20 30 78 34 30 30 30 30 30 30 30 20 29 7b 0a 20   0x40000000 ){. 
8ebf0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
8ec00 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 20 6e 42 79  ..  /* Round nBy
8ec10 74 65 20 75 70 20 74 6f 20 74 68 65 20 6e 65 78  te up to the nex
8ec20 74 20 76 61 6c 69 64 20 70 6f 77 65 72 20 6f 66  t valid power of
8ec30 20 74 77 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 46   two */.  for(iF
8ec40 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f  ullSz=mem5.szAto
8ec50 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20 69  m, iLogsize=0; i
8ec60 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69 46  FullSz<nByte; iF
8ec70 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f 67  ullSz *= 2, iLog
8ec80 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a 20  size++){}..  /* 
8ec90 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e 61  Make sure mem5.a
8eca0 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69  iFreelist[iLogsi
8ecb0 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74 20  ze] contains at 
8ecc0 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65 0a 20  least one free. 
8ecd0 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 6e   ** block.  If n
8ece0 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74 20 61  ot, then split a
8ecf0 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20 6e 65   block of the ne
8ed00 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65 72 20  xt larger power 
8ed10 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 69 6e 20 6f  of.  ** two in o
8ed20 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 61  rder to create a
8ed30 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20   new free block 
8ed40 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73 69 7a 65  of size iLogsize
8ed50 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 42 69  ..  */.  for(iBi
8ed60 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d 35  n=iLogsize; mem5
8ed70 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 42 69 6e  .aiFreelist[iBin
8ed80 5d 3c 30 20 26 26 20 69 42 69 6e 3c 3d 4c 4f 47  ]<0 && iBin<=LOG
8ed90 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d 0a 20  MAX; iBin++){}. 
8eda0 20 69 66 28 20 69 42 69 6e 3e 4c 4f 47 4d 41 58   if( iBin>LOGMAX
8edb0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
8edc0 20 3d 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b   = memsys5Unlink
8edd0 46 69 72 73 74 28 69 42 69 6e 29 3b 0a 20 20 77  First(iBin);.  w
8ede0 68 69 6c 65 28 20 69 42 69 6e 3e 69 4c 6f 67 73  hile( iBin>iLogs
8edf0 69 7a 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ize ){.    int n
8ee00 65 77 53 69 7a 65 3b 0a 0a 20 20 20 20 69 42 69  ewSize;..    iBi
8ee10 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65  n--;.    newSize
8ee20 20 3d 20 31 20 3c 3c 20 69 42 69 6e 3b 0a 20 20   = 1 << iBin;.  
8ee30 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e    mem5.aCtrl[i+n
8ee40 65 77 53 69 7a 65 5d 20 3d 20 43 54 52 4c 5f 46  ewSize] = CTRL_F
8ee50 52 45 45 20 7c 20 69 42 69 6e 3b 0a 20 20 20 20  REE | iBin;.    
8ee60 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 2b 6e 65  memsys5Link(i+ne
8ee70 77 53 69 7a 65 2c 20 69 42 69 6e 29 3b 0a 20 20  wSize, iBin);.  
8ee80 7d 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69  }.  mem5.aCtrl[i
8ee90 5d 20 3d 20 69 4c 6f 67 73 69 7a 65 3b 0a 0a 20  ] = iLogsize;.. 
8eea0 20 2f 2a 20 55 70 64 61 74 65 20 61 6c 6c 6f 63   /* Update alloc
8eeb0 61 74 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65  ator performance
8eec0 20 73 74 61 74 69 73 74 69 63 73 2e 20 2a 2f 0a   statistics. */.
8eed0 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b 3b    mem5.nAlloc++;
8eee0 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c  .  mem5.totalAll
8eef0 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20  oc += iFullSz;. 
8ef00 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73   mem5.totalExces
8ef10 73 20 2b 3d 20 69 46 75 6c 6c 53 7a 20 2d 20 6e  s += iFullSz - n
8ef20 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 63 75 72  Byte;.  mem5.cur
8ef30 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b 0a 20 20 6d  rentCount++;.  m
8ef40 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 2b  em5.currentOut +
8ef50 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 66 28  = iFullSz;.  if(
8ef60 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 3c 6d   mem5.maxCount<m
8ef70 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
8ef80 20 29 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74   ) mem5.maxCount
8ef90 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43   = mem5.currentC
8efa0 6f 75 6e 74 3b 0a 20 20 69 66 28 20 6d 65 6d 35  ount;.  if( mem5
8efb0 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35 2e 63 75 72  .maxOut<mem5.cur
8efc0 72 65 6e 74 4f 75 74 20 29 20 6d 65 6d 35 2e 6d  rentOut ) mem5.m
8efd0 61 78 4f 75 74 20 3d 20 6d 65 6d 35 2e 63 75 72  axOut = mem5.cur
8efe0 72 65 6e 74 4f 75 74 3b 0a 0a 20 20 2f 2a 20 52  rentOut;..  /* R
8eff0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
8f000 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  to the allocated
8f010 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20 72 65   memory. */.  re
8f020 74 75 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d  turn (void*)&mem
8f030 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e 73  5.zPool[i*mem5.s
8f040 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  zAtom];.}../*.**
8f050 20 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e   Free an outstan
8f060 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ding memory allo
8f070 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  cation..*/.stati
8f080 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72  c void memsys5Fr
8f090 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70  eeUnsafe(void *p
8f0a0 4f 6c 64 29 7b 0a 20 20 75 33 32 20 73 69 7a 65  Old){.  u32 size
8f0b0 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69 6e  , iLogsize;.  in
8f0c0 74 20 69 42 6c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20  t iBlock;..  /* 
8f0d0 53 65 74 20 69 42 6c 6f 63 6b 20 74 6f 20 74 68  Set iBlock to th
8f0e0 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 62  e index of the b
8f0f0 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 20 74 6f 20  lock pointed to 
8f100 62 79 20 70 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a  by pOld in .  **
8f110 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6d 65   the array of me
8f120 6d 35 2e 73 7a 41 74 6f 6d 20 62 79 74 65 20 62  m5.szAtom byte b
8f130 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 64 20 74 6f  locks pointed to
8f140 20 62 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a   by mem5.zPool..
8f150 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20 3d 20    */.  iBlock = 
8f160 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35  ((u8 *)pOld-mem5
8f170 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41  .zPool)/mem5.szA
8f180 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  tom;..  /* Check
8f190 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65   that the pointe
8f1a0 72 20 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74 6f  r pOld points to
8f1b0 20 61 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72   a valid, non-fr
8f1c0 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61  ee block. */.  a
8f1d0 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d 30  ssert( iBlock>=0
8f1e0 20 26 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e   && iBlock<mem5.
8f1f0 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65  nBlock );.  asse
8f200 72 74 28 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d  rt( ((u8 *)pOld-
8f210 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35  mem5.zPool)%mem5
8f220 2e 73 7a 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20  .szAtom==0 );.  
8f230 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43  assert( (mem5.aC
8f240 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54  trl[iBlock] & CT
8f250 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a  RL_FREE)==0 );..
8f260 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d    iLogsize = mem
8f270 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
8f280 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a  & CTRL_LOGSIZE;.
8f290 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67    size = 1<<iLog
8f2a0 73 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20  size;.  assert( 
8f2b0 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75  iBlock+size-1<(u
8f2c0 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29  32)mem5.nBlock )
8f2d0 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b  ;..  mem5.aCtrl[
8f2e0 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f  iBlock] |= CTRL_
8f2f0 46 52 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74  FREE;.  mem5.aCt
8f300 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31  rl[iBlock+size-1
8f310 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a  ] |= CTRL_FREE;.
8f320 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63    assert( mem5.c
8f330 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b  urrentCount>0 );
8f340 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e  .  assert( mem5.
8f350 63 75 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a  currentOut>=(siz
8f360 65 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 20 29  e*mem5.szAtom) )
8f370 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  ;.  mem5.current
8f380 43 6f 75 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e  Count--;.  mem5.
8f390 63 75 72 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69  currentOut -= si
8f3a0 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a  ze*mem5.szAtom;.
8f3b0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63    assert( mem5.c
8f3c0 75 72 72 65 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d  urrentOut>0 || m
8f3d0 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
8f3e0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
8f3f0 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75   mem5.currentCou
8f400 6e 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72  nt>0 || mem5.cur
8f410 72 65 6e 74 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20  rentOut==0 );.. 
8f420 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8f430 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  ck] = CTRL_FREE 
8f440 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68  | iLogsize;.  wh
8f450 69 6c 65 28 20 41 4c 57 41 59 53 28 69 4c 6f 67  ile( ALWAYS(iLog
8f460 73 69 7a 65 3c 4c 4f 47 4d 41 58 29 20 29 7b 0a  size<LOGMAX) ){.
8f470 20 20 20 20 69 6e 74 20 69 42 75 64 64 79 3b 0a      int iBuddy;.
8f480 20 20 20 20 69 66 28 20 28 69 42 6c 6f 63 6b 3e      if( (iBlock>
8f490 3e 69 4c 6f 67 73 69 7a 65 29 20 26 20 31 20 29  >iLogsize) & 1 )
8f4a0 7b 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d  {.      iBuddy =
8f4b0 20 69 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a   iBlock - size;.
8f4c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8f4d0 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b   iBuddy = iBlock
8f4e0 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20   + size;.    }. 
8f4f0 20 20 20 61 73 73 65 72 74 28 20 69 42 75 64 64     assert( iBudd
8f500 79 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  y>=0 );.    if( 
8f510 28 69 42 75 64 64 79 2b 28 31 3c 3c 69 4c 6f 67  (iBuddy+(1<<iLog
8f520 73 69 7a 65 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f  size))>mem5.nBlo
8f530 63 6b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ck ) break;.    
8f540 69 66 28 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69  if( mem5.aCtrl[i
8f550 42 75 64 64 79 5d 21 3d 28 43 54 52 4c 5f 46 52  Buddy]!=(CTRL_FR
8f560 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 29 20 29  EE | iLogsize) )
8f570 20 62 72 65 61 6b 3b 0a 20 20 20 20 6d 65 6d 73   break;.    mems
8f580 79 73 35 55 6e 6c 69 6e 6b 28 69 42 75 64 64 79  ys5Unlink(iBuddy
8f590 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 20  , iLogsize);.   
8f5a0 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b 0a 20 20 20   iLogsize++;.   
8f5b0 20 69 66 28 20 69 42 75 64 64 79 3c 69 42 6c 6f   if( iBuddy<iBlo
8f5c0 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35  ck ){.      mem5
8f5d0 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d  .aCtrl[iBuddy] =
8f5e0 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f   CTRL_FREE | iLo
8f5f0 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d  gsize;.      mem
8f600 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
8f610 3d 20 30 3b 0a 20 20 20 20 20 20 69 42 6c 6f 63  = 0;.      iBloc
8f620 6b 20 3d 20 69 42 75 64 64 79 3b 0a 20 20 20 20  k = iBuddy;.    
8f630 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
8f640 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
8f650 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c  = CTRL_FREE | iL
8f660 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65  ogsize;.      me
8f670 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d  m5.aCtrl[iBuddy]
8f680 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
8f690 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20  size *= 2;.  }. 
8f6a0 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 42 6c   memsys5Link(iBl
8f6b0 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a  ock, iLogsize);.
8f6c0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
8f6d0 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f  e nBytes of memo
8f6e0 72 79 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ry.*/.static voi
8f6f0 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63  d *memsys5Malloc
8f700 28 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20  (int nBytes){.  
8f710 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
8f720 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 42 79 74   = 0;.  if( nByt
8f730 65 73 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73  es>0 ){.    mems
8f740 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 20 20  ys5Enter();.    
8f750 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f  p = memsys5Mallo
8f760 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b  cUnsafe(nBytes);
8f770 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76  .    memsys5Leav
8f780 65 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  e();.  }.  retur
8f790 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a  n (void*)p; .}..
8f7a0 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72  /*.** Free memor
8f7b0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74  y..**.** The out
8f7c0 65 72 20 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20  er layer memory 
8f7d0 61 6c 6c 6f 63 61 74 6f 72 20 70 72 65 76 65 6e  allocator preven
8f7e0 74 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ts this routine 
8f7f0 66 72 6f 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61  from.** being ca
8f800 6c 6c 65 64 20 77 69 74 68 20 70 50 72 69 6f 72  lled with pPrior
8f810 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ==0..*/.static v
8f820 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 28  oid memsys5Free(
8f830 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20  void *pPrior){. 
8f840 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21   assert( pPrior!
8f850 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 79 73 35 45  =0 );.  memsys5E
8f860 6e 74 65 72 28 29 3b 0a 20 20 6d 65 6d 73 79 73  nter();.  memsys
8f870 35 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69  5FreeUnsafe(pPri
8f880 6f 72 29 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65  or);.  memsys5Le
8f890 61 76 65 28 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a  ave();  .}../*.*
8f8a0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a  * Change the siz
8f8b0 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67  e of an existing
8f8c0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
8f8d0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75  on..**.** The ou
8f8e0 74 65 72 20 6c 61 79 65 72 20 6d 65 6d 6f 72 79  ter layer memory
8f8f0 20 61 6c 6c 6f 63 61 74 6f 72 20 70 72 65 76 65   allocator preve
8f900 6e 74 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  nts this routine
8f910 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 6e 67 20 63   from.** being c
8f920 61 6c 6c 65 64 20 77 69 74 68 20 70 50 72 69 6f  alled with pPrio
8f930 72 3d 3d 30 2e 20 20 0a 2a 2a 0a 2a 2a 20 6e 42  r==0.  .**.** nB
8f940 79 74 65 73 20 69 73 20 61 6c 77 61 79 73 20 61  ytes is always a
8f950 20 76 61 6c 75 65 20 6f 62 74 61 69 6e 65 64 20   value obtained 
8f960 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 61 6c  from a prior cal
8f970 6c 20 74 6f 0a 2a 2a 20 6d 65 6d 73 79 73 35 52  l to.** memsys5R
8f980 6f 75 6e 64 28 29 2e 20 20 48 65 6e 63 65 20 6e  ound().  Hence n
8f990 42 79 74 65 73 20 69 73 20 61 6c 77 61 79 73 20  Bytes is always 
8f9a0 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 70  a non-negative p
8f9b0 6f 77 65 72 0a 2a 2a 20 6f 66 20 74 77 6f 2e 20  ower.** of two. 
8f9c0 20 49 66 20 6e 42 79 74 65 73 3d 3d 30 20 74 68   If nBytes==0 th
8f9d0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e  at means that an
8f9e0 20 6f 76 65 72 73 69 7a 65 20 61 6c 6c 6f 63 61   oversize alloca
8f9f0 74 69 6f 6e 0a 2a 2a 20 28 61 6e 20 61 6c 6c 6f  tion.** (an allo
8fa00 63 61 74 69 6f 6e 20 6c 61 72 67 65 72 20 74 68  cation larger th
8fa10 61 6e 20 30 78 34 30 30 30 30 30 30 30 29 20 77  an 0x40000000) w
8fa20 61 73 20 72 65 71 75 65 73 74 65 64 20 61 6e 64  as requested and
8fa30 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
8fa40 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 30   should return 0
8fa50 20 77 69 74 68 6f 75 74 20 66 72 65 65 69 6e 67   without freeing
8fa60 20 70 50 72 69 6f 72 2e 0a 2a 2f 0a 73 74 61 74   pPrior..*/.stat
8fa70 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35  ic void *memsys5
8fa80 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
8fa90 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73  rior, int nBytes
8faa0 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20  ){.  int nOld;. 
8fab0 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65   void *p;.  asse
8fac0 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b  rt( pPrior!=0 );
8fad0 0a 20 20 61 73 73 65 72 74 28 20 28 6e 42 79 74  .  assert( (nByt
8fae0 65 73 26 28 6e 42 79 74 65 73 2d 31 29 29 3d 3d  es&(nBytes-1))==
8faf0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
8fb00 42 79 74 65 73 3e 3d 30 20 29 3b 0a 20 20 69 66  Bytes>=0 );.  if
8fb10 28 20 6e 42 79 74 65 73 3d 3d 30 20 29 7b 0a 20  ( nBytes==0 ){. 
8fb20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
8fb30 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73  .  nOld = memsys
8fb40 35 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20  5Size(pPrior);. 
8fb50 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c   if( nBytes<=nOl
8fb60 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
8fb70 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65  pPrior;.  }.  me
8fb80 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20  msys5Enter();.  
8fb90 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f  p = memsys5Mallo
8fba0 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b  cUnsafe(nBytes);
8fbb0 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
8fbc0 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72  memcpy(p, pPrior
8fbd0 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 6d 65 6d  , nOld);.    mem
8fbe0 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 70  sys5FreeUnsafe(p
8fbf0 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65  Prior);.  }.  me
8fc00 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20  msys5Leave();.  
8fc10 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
8fc20 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65  ** Round up a re
8fc30 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68  quest size to th
8fc40 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c  e next valid all
8fc50 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 20 20 49  ocation size.  I
8fc60 66 0a 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61 74  f.** the allocat
8fc70 69 6f 6e 20 69 73 20 74 6f 6f 20 6c 61 72 67 65  ion is too large
8fc80 20 74 6f 20 62 65 20 68 61 6e 64 6c 65 64 20 62   to be handled b
8fc90 79 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f  y this allocatio
8fca0 6e 20 73 79 73 74 65 6d 2c 0a 2a 2a 20 72 65 74  n system,.** ret
8fcb0 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c  urn 0..**.** All
8fcc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6d 75 73   allocations mus
8fcd0 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20  t be a power of 
8fce0 74 77 6f 20 61 6e 64 20 6d 75 73 74 20 62 65 20  two and must be 
8fcf0 65 78 70 72 65 73 73 65 64 20 62 79 20 61 0a 2a  expressed by a.*
8fd00 2a 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20  * 32-bit signed 
8fd10 69 6e 74 65 67 65 72 2e 20 20 48 65 6e 63 65 20  integer.  Hence 
8fd20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f  the largest allo
8fd30 63 61 74 69 6f 6e 20 69 73 20 30 78 34 30 30 30  cation is 0x4000
8fd40 30 30 30 30 0a 2a 2a 20 6f 72 20 31 30 37 33 37  0000.** or 10737
8fd50 34 31 38 32 34 20 62 79 74 65 73 2e 0a 2a 2f 0a  41824 bytes..*/.
8fd60 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
8fd70 73 35 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29  s5Roundup(int n)
8fd80 7b 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b  {.  int iFullSz;
8fd90 0a 20 20 69 66 28 20 6e 20 3e 20 30 78 34 30 30  .  if( n > 0x400
8fda0 30 30 30 30 30 20 29 20 72 65 74 75 72 6e 20 30  00000 ) return 0
8fdb0 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d  ;.  for(iFullSz=
8fdc0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 20 69 46 75  mem5.szAtom; iFu
8fdd0 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20  llSz<n; iFullSz 
8fde0 2a 3d 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  *= 2);.  return 
8fdf0 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a  iFullSz;.}../*.*
8fe00 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 65 69  * Return the cei
8fe10 6c 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 67 61  ling of the loga
8fe20 72 69 74 68 6d 20 62 61 73 65 20 32 20 6f 66 20  rithm base 2 of 
8fe30 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 45 78  iValue..**.** Ex
8fe40 61 6d 70 6c 65 73 3a 20 20 20 6d 65 6d 73 79 73  amples:   memsys
8fe50 35 4c 6f 67 28 31 29 20 2d 3e 20 30 0a 2a 2a 20  5Log(1) -> 0.** 
8fe60 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73              mems
8fe70 79 73 35 4c 6f 67 28 32 29 20 2d 3e 20 31 0a 2a  ys5Log(2) -> 1.*
8fe80 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  *             me
8fe90 6d 73 79 73 35 4c 6f 67 28 34 29 20 2d 3e 20 32  msys5Log(4) -> 2
8fea0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
8feb0 6d 65 6d 73 79 73 35 4c 6f 67 28 35 29 20 2d 3e  memsys5Log(5) ->
8fec0 20 33 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   3.**           
8fed0 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 38 29 20    memsys5Log(8) 
8fee0 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 20 20 20  -> 3.**         
8fef0 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 39      memsys5Log(9
8ff00 29 20 2d 3e 20 34 0a 2a 2f 0a 73 74 61 74 69 63  ) -> 4.*/.static
8ff10 20 69 6e 74 20 6d 65 6d 73 79 73 35 4c 6f 67 28   int memsys5Log(
8ff20 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 69  int iValue){.  i
8ff30 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 6f 72 28 69  nt iLog;.  for(i
8ff40 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 4c 6f 67 29  Log=0; (1<<iLog)
8ff50 3c 69 56 61 6c 75 65 3b 20 69 4c 6f 67 2b 2b 29  <iValue; iLog++)
8ff60 3b 0a 20 20 72 65 74 75 72 6e 20 69 4c 6f 67 3b  ;.  return iLog;
8ff70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
8ff80 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  lize the memory 
8ff90 61 6c 6c 6f 63 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  allocator..**.**
8ffa0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
8ffb0 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e   not threadsafe.
8ffc0 20 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73    The caller mus
8ffd0 74 20 62 65 20 68 6f 6c 64 69 6e 67 20 61 20 6d  t be holding a m
8ffe0 75 74 65 78 0a 2a 2a 20 74 6f 20 70 72 65 76 65  utex.** to preve
8fff0 6e 74 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65  nt multiple thre
90000 61 64 73 20 66 72 6f 6d 20 65 6e 74 65 72 69 6e  ads from enterin
90010 67 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  g at the same ti
90020 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  me..*/.static in
90030 74 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 76 6f  t memsys5Init(vo
90040 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  id *NotUsed){.  
90050 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20  int ii;         
90060 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
90070 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  er */.  int nByt
90080 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  e;         /* Nu
90090 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
900a0 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c   memory availabl
900b0 65 20 74 6f 20 74 68 69 73 20 61 6c 6c 6f 63 61  e to this alloca
900c0 74 6f 72 20 2a 2f 0a 20 20 75 38 20 2a 7a 42 79  tor */.  u8 *zBy
900d0 74 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  te;         /* M
900e0 65 6d 6f 72 79 20 75 73 61 62 6c 65 20 62 79 20  emory usable by 
900f0 74 68 69 73 20 61 6c 6c 6f 63 61 74 6f 72 20 2a  this allocator *
90100 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b  /.  int nMinLog;
90110 20 20 20 20 20 20 20 2f 2a 20 4c 6f 67 20 62 61         /* Log ba
90120 73 65 20 32 20 6f 66 20 6d 69 6e 69 6d 75 6d 20  se 2 of minimum 
90130 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20  allocation size 
90140 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e  in bytes */.  in
90150 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20  t iOffset;      
90160 20 2f 2a 20 41 6e 20 6f 66 66 73 65 74 20 69 6e   /* An offset in
90170 74 6f 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20  to mem5.aCtrl[] 
90180 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  */..  UNUSED_PAR
90190 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
901a0 0a 0a 20 20 2f 2a 20 46 6f 72 20 74 68 65 20 70  ..  /* For the p
901b0 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20  urposes of this 
901c0 72 6f 75 74 69 6e 65 2c 20 64 69 73 61 62 6c 65  routine, disable
901d0 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20   the mutex */.  
901e0 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 30 3b 0a  mem5.mutex = 0;.
901f0 0a 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f  .  /* The size o
90200 66 20 61 20 4d 65 6d 35 4c 69 6e 6b 20 6f 62 6a  f a Mem5Link obj
90210 65 63 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f  ect must be a po
90220 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 56 65 72  wer of two.  Ver
90230 69 66 79 20 74 68 61 74 0a 20 20 2a 2a 20 74 68  ify that.  ** th
90240 69 73 20 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f  is is case..  */
90250 0a 20 20 61 73 73 65 72 74 28 20 28 73 69 7a 65  .  assert( (size
90260 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 26 28 73 69  of(Mem5Link)&(si
90270 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 2d 31  zeof(Mem5Link)-1
90280 29 29 3d 3d 30 20 29 3b 0a 0a 20 20 6e 42 79 74  ))==0 );..  nByt
90290 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  e = sqlite3Globa
902a0 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 3b 0a 20  lConfig.nHeap;. 
902b0 20 7a 42 79 74 65 20 3d 20 28 75 38 2a 29 73 71   zByte = (u8*)sq
902c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
902d0 67 2e 70 48 65 61 70 3b 0a 20 20 61 73 73 65 72  g.pHeap;.  asser
902e0 74 28 20 7a 42 79 74 65 21 3d 30 20 29 3b 20 20  t( zByte!=0 );  
902f0 2f 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  /* sqlite3_confi
90300 67 28 29 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c  g() does not all
90310 6f 77 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a  ow otherwise */.
90320 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d  .  nMinLog = mem
90330 73 79 73 35 4c 6f 67 28 73 71 6c 69 74 65 33 47  sys5Log(sqlite3G
90340 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65  lobalConfig.mnRe
90350 71 29 3b 0a 20 20 6d 65 6d 35 2e 73 7a 41 74 6f  q);.  mem5.szAto
90360 6d 20 3d 20 28 31 3c 3c 6e 4d 69 6e 4c 6f 67 29  m = (1<<nMinLog)
90370 3b 0a 20 20 77 68 69 6c 65 28 20 28 69 6e 74 29  ;.  while( (int)
90380 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29  sizeof(Mem5Link)
90390 3e 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 29 7b 0a  >mem5.szAtom ){.
903a0 20 20 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20      mem5.szAtom 
903b0 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c  = mem5.szAtom <<
903c0 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 35 2e   1;.  }..  mem5.
903d0 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 65 20  nBlock = (nByte 
903e0 2f 20 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2b 73  / (mem5.szAtom+s
903f0 69 7a 65 6f 66 28 75 38 29 29 29 3b 0a 20 20 6d  izeof(u8)));.  m
90400 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79 74  em5.zPool = zByt
90410 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 20  e;.  mem5.aCtrl 
90420 3d 20 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a 50  = (u8 *)&mem5.zP
90430 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 2a  ool[mem5.nBlock*
90440 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 0a 20  mem5.szAtom];.. 
90450 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c   for(ii=0; ii<=L
90460 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20  OGMAX; ii++){.  
90470 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73    mem5.aiFreelis
90480 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a  t[ii] = -1;.  }.
90490 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  .  iOffset = 0;.
904a0 20 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b    for(ii=LOGMAX;
904b0 20 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20   ii>=0; ii--){. 
904c0 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20     int nAlloc = 
904d0 28 31 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28  (1<<ii);.    if(
904e0 20 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63   (iOffset+nAlloc
904f0 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29  )<=mem5.nBlock )
90500 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74  {.      mem5.aCt
90510 72 6c 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69  rl[iOffset] = ii
90520 20 7c 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20   | CTRL_FREE;.  
90530 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28      memsys5Link(
90540 69 4f 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20  iOffset, ii);.  
90550 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e      iOffset += n
90560 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20  Alloc;.    }.   
90570 20 61 73 73 65 72 74 28 28 69 4f 66 66 73 65 74   assert((iOffset
90580 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42  +nAlloc)>mem5.nB
90590 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  lock);.  }..  /*
905a0 20 49 66 20 61 20 6d 75 74 65 78 20 69 73 20 72   If a mutex is r
905b0 65 71 75 69 72 65 64 20 66 6f 72 20 6e 6f 72 6d  equired for norm
905c0 61 6c 20 6f 70 65 72 61 74 69 6f 6e 2c 20 61 6c  al operation, al
905d0 6c 6f 63 61 74 65 20 6f 6e 65 20 2a 2f 0a 20 20  locate one */.  
905e0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
905f0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
90600 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e  ==0 ){.    mem5.
90610 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
90620 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
90630 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
90640 4d 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  M);.  }..  retur
90650 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
90660 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69  /*.** Deinitiali
90670 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a  ze this module..
90680 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
90690 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 28 76  emsys5Shutdown(v
906a0 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  oid *NotUsed){. 
906b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
906c0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65  R(NotUsed);.  me
906d0 6d 35 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20  m5.mutex = 0;.  
906e0 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23 69 66 64 65  return;.}..#ifde
906f0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
90700 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c  .** Open the fil
90710 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20  e indicated and 
90720 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61  write a log of a
90730 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72  ll unfreed memor
90740 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  y .** allocation
90750 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e  s into that log.
90760 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
90770 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
90780 65 6d 73 79 73 35 44 75 6d 70 28 63 6f 6e 73 74  emsys5Dump(const
90790 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
907a0 29 7b 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a  ){.  FILE *out;.
907b0 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20    int i, j, n;. 
907c0 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0a 0a 20   int nMinLog;.. 
907d0 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d   if( zFilename==
907e0 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30  0 || zFilename[0
907f0 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 20  ]==0 ){.    out 
90800 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c 73  = stdout;.  }els
90810 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70  e{.    out = fop
90820 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77  en(zFilename, "w
90830 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 3d  ");.    if( out=
90840 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69  =0 ){.      fpri
90850 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20  ntf(stderr, "** 
90860 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74  Unable to output
90870 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75   memory debug ou
90880 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c  tput log: %s **\
90890 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
908a0 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e            zFilen
908b0 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ame);.      retu
908c0 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
908d0 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a  memsys5Enter();.
908e0 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73    nMinLog = mems
908f0 79 73 35 4c 6f 67 28 6d 65 6d 35 2e 73 7a 41 74  ys5Log(mem5.szAt
90900 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  om);.  for(i=0; 
90910 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 20 69 2b 6e  i<=LOGMAX && i+n
90920 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b  MinLog<32; i++){
90930 0a 20 20 20 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d  .    for(n=0, j=
90940 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
90950 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45  i]; j>=0; j = ME
90960 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c  M5LINK(j)->next,
90970 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 66 70 72 69   n++){}.    fpri
90980 6e 74 66 28 6f 75 74 2c 20 22 66 72 65 65 6c 69  ntf(out, "freeli
90990 73 74 20 69 74 65 6d 73 20 6f 66 20 73 69 7a 65  st items of size
909a0 20 25 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d 35   %d: %d\n", mem5
909b0 2e 73 7a 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29  .szAtom << i, n)
909c0 3b 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28  ;.  }.  fprintf(
909d0 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f  out, "mem5.nAllo
909e0 63 20 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e  c       = %llu\n
909f0 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b  ", mem5.nAlloc);
90a00 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
90a10 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63  "mem5.totalAlloc
90a20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65     = %llu\n", me
90a30 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a  m5.totalAlloc);.
90a40 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
90a50 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73  mem5.totalExcess
90a60 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d    = %llu\n", mem
90a70 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 29 3b 0a  5.totalExcess);.
90a80 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
90a90 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20  mem5.currentOut 
90aa0 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e    = %u\n", mem5.
90ab0 63 75 72 72 65 6e 74 4f 75 74 29 3b 0a 20 20 66  currentOut);.  f
90ac0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d  printf(out, "mem
90ad0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 3d  5.currentCount =
90ae0 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72   %u\n", mem5.cur
90af0 72 65 6e 74 43 6f 75 6e 74 29 3b 0a 20 20 66 70  rentCount);.  fp
90b00 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35  rintf(out, "mem5
90b10 2e 6d 61 78 4f 75 74 20 20 20 20 20 20 20 3d 20  .maxOut       = 
90b20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f  %u\n", mem5.maxO
90b30 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  ut);.  fprintf(o
90b40 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 43 6f 75  ut, "mem5.maxCou
90b50 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20  nt     = %u\n", 
90b60 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a  mem5.maxCount);.
90b70 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
90b80 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20  mem5.maxRequest 
90b90 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e    = %u\n", mem5.
90ba0 6d 61 78 52 65 71 75 65 73 74 29 3b 0a 20 20 6d  maxRequest);.  m
90bb0 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20  emsys5Leave();. 
90bc0 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74   if( out==stdout
90bd0 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73   ){.    fflush(s
90be0 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  tdout);.  }else{
90bf0 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29  .    fclose(out)
90c00 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ;.  }.}.#endif..
90c10 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
90c20 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72  ne is the only r
90c30 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66  outine in this f
90c40 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61  ile with externa
90c50 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 20 49  l .** linkage. I
90c60 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  t returns a poin
90c70 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20  ter to a static 
90c80 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
90c90 6f 64 73 0a 2a 2a 20 73 74 72 75 63 74 20 70 6f  ods.** struct po
90ca0 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65  pulated with the
90cb0 20 6d 65 6d 73 79 73 35 20 6d 65 74 68 6f 64 73   memsys5 methods
90cc0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
90cd0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
90ce0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
90cf0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73  qlite3MemGetMems
90d00 79 73 35 28 76 6f 69 64 29 7b 0a 20 20 73 74 61  ys5(void){.  sta
90d10 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
90d20 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 65  3_mem_methods me
90d30 6d 73 79 73 35 4d 65 74 68 6f 64 73 20 3d 20 7b  msys5Methods = {
90d40 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 4d 61 6c  .     memsys5Mal
90d50 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  loc,.     memsys
90d60 35 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d 73  5Free,.     mems
90d70 79 73 35 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20  ys5Realloc,.    
90d80 20 6d 65 6d 73 79 73 35 53 69 7a 65 2c 0a 20 20   memsys5Size,.  
90d90 20 20 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75     memsys5Roundu
90da0 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 49  p,.     memsys5I
90db0 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  nit,.     memsys
90dc0 35 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20  5Shutdown,.     
90dd0 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20  0.  };.  return 
90de0 26 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 3b  &memsys5Methods;
90df0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
90e00 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
90e10 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  YS5 */../*******
90e20 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
90e30 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em5.c **********
90e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90e60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
90e70 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
90e80 6c 65 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a  le mutex.c *****
90e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90eb0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
90ec0 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a  07 August 14.**.
90ed0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
90ee0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
90ef0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
90f00 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
90f10 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
90f20 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
90f30 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
90f40 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
90f50 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
90f60 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
90f70 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
90f80 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
90f90 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
90fa0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
90fb0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
90fc0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
90fd0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
90fe0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
90ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
91030 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
91040 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
91050 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
91060 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20   mutexes..**.** 
91070 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
91080 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ns code that is 
91090 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73 73 20 61 6c  common across al
910a0 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  l mutex implemen
910b0 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 0a 23 69 66  tations..*/..#if
910c0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
910d0 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e  DEBUG) && !defin
910e0 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ed(SQLITE_MUTEX_
910f0 4f 4d 49 54 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  OMIT)./*.** For 
91100 64 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73  debugging purpos
91110 65 73 2c 20 72 65 63 6f 72 64 20 77 68 65 6e 20  es, record when 
91120 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
91130 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  tem is initializ
91140 65 64 0a 2a 2a 20 61 6e 64 20 75 6e 69 6e 69 74  ed.** and uninit
91150 69 61 6c 69 7a 65 64 20 73 6f 20 74 68 61 74 20  ialized so that 
91160 77 65 20 63 61 6e 20 61 73 73 65 72 74 28 29 20  we can assert() 
91170 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 61  if there is an a
91180 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 61 6c 6c  ttempt to.** all
91190 6f 63 61 74 65 20 61 20 6d 75 74 65 78 20 77 68  ocate a mutex wh
911a0 69 6c 65 20 74 68 65 20 73 79 73 74 65 6d 20 69  ile the system i
911b0 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e  s uninitialized.
911c0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
911d0 45 5f 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49  E_WSD int mutexI
911e0 73 49 6e 69 74 20 3d 20 30 3b 0a 23 65 6e 64 69  sInit = 0;.#endi
911f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55  f /* SQLITE_DEBU
91200 47 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53  G */...#ifndef S
91210 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54  QLITE_MUTEX_OMIT
91220 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
91230 65 20 74 68 65 20 6d 75 74 65 78 20 73 79 73 74  e the mutex syst
91240 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  em..*/.SQLITE_PR
91250 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
91260 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29  3MutexInit(void)
91270 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  { .  int rc = SQ
91280 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73  LITE_OK;.  if( s
91290 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
912a0 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b  ig.bCoreMutex ){
912b0 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
912c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
912d0 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20  tex.xMutexAlloc 
912e0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ){.      /* If t
912f0 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d  he xMutexAlloc m
91300 65 74 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65  ethod has not be
91310 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  en set, then the
91320 20 75 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20   user did not.  
91330 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61      ** install a
91340 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
91350 61 74 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65  ation via sqlite
91360 33 5f 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72  3_config() prior
91370 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71   to .      ** sq
91380 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
91390 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e  () being called.
913a0 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69   This block copi
913b0 65 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20  es pointers to. 
913c0 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61       ** the defa
913d0 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ult implementati
913e0 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  on into the sqli
913f0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20  te3GlobalConfig 
91400 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20  structure..     
91410 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
91420 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
91430 2a 70 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33  *pFrom = sqlite3
91440 44 65 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a  DefaultMutex();.
91450 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
91460 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f  tex_methods *pTo
91470 20 3d 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61   = &sqlite3Globa
91480 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a  lConfig.mutex;..
91490 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f        memcpy(pTo
914a0 2c 20 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f  , pFrom, offseto
914b0 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  f(sqlite3_mutex_
914c0 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41  methods, xMutexA
914d0 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 20 20 6d 65  lloc));.      me
914e0 6d 63 70 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65  mcpy(&pTo->xMute
914f0 78 46 72 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78  xFree, &pFrom->x
91500 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 20  MutexFree,.     
91510 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a          sizeof(*
91520 70 54 6f 29 20 2d 20 6f 66 66 73 65 74 6f 66 28  pTo) - offsetof(
91530 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
91540 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 46 72 65  thods, xMutexFre
91550 65 29 29 3b 0a 20 20 20 20 20 20 70 54 6f 2d 3e  e));.      pTo->
91560 78 4d 75 74 65 78 41 6c 6c 6f 63 20 3d 20 70 46  xMutexAlloc = pF
91570 72 6f 6d 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63  rom->xMutexAlloc
91580 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
91590 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
915a0 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
915b0 78 49 6e 69 74 28 29 3b 0a 20 20 7d 0a 0a 23 69  xInit();.  }..#i
915c0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
915d0 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20  G.  GLOBAL(int, 
915e0 6d 75 74 65 78 49 73 49 6e 69 74 29 20 3d 20 31  mutexIsInit) = 1
915f0 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75  ;.#endif..  retu
91600 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
91610 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74  Shutdown the mut
91620 65 78 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20  ex system. This 
91630 63 61 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75  call frees resou
91640 72 63 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62  rces allocated b
91650 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65  y.** sqlite3Mute
91660 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49  xInit()..*/.SQLI
91670 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
91680 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76  qlite3MutexEnd(v
91690 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  oid){.  int rc =
916a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
916b0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
916c0 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74  onfig.mutex.xMut
916d0 65 78 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20  exEnd ){.    rc 
916e0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
916f0 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74  onfig.mutex.xMut
91700 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 69  exEnd();.  }..#i
91710 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
91720 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20  G.  GLOBAL(int, 
91730 6d 75 74 65 78 49 73 49 6e 69 74 29 20 3d 20 30  mutexIsInit) = 0
91740 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75  ;.#endif..  retu
91750 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
91760 52 65 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74  Retrieve a point
91770 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d  er to a static m
91780 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65  utex or allocate
91790 20 61 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f   a new dynamic o
917a0 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ne..*/.SQLITE_AP
917b0 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
917c0 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
917d0 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 69  lloc(int id){.#i
917e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
917f0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28  T_AUTOINIT.  if(
91800 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
91810 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30  ize() ) return 0
91820 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  ;.#endif.  retur
91830 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  n sqlite3GlobalC
91840 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74  onfig.mutex.xMut
91850 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a  exAlloc(id);.}..
91860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
91870 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
91880 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
91890 69 6e 74 20 69 64 29 7b 0a 20 20 69 66 28 20 21  int id){.  if( !
918a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
918b0 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29  fig.bCoreMutex )
918c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
918d0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 47 4c    }.  assert( GL
918e0 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49  OBAL(int, mutexI
918f0 73 49 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 75  sInit) );.  retu
91900 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
91910 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91920 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a  texAlloc(id);.}.
91930 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79  ./*.** Free a dy
91940 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a  namic mutex..*/.
91950 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
91960 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72  sqlite3_mutex_fr
91970 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ee(sqlite3_mutex
91980 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
91990 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
919a0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
919b0 4d 75 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20  MutexFree(p);.  
919c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69  }.}../*.** Obtai
919d0 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49  n the mutex p. I
919e0 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72  f some other thr
919f0 65 61 64 20 61 6c 72 65 61 64 79 20 68 61 73 20  ead already has 
91a00 74 68 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b  the mutex, block
91a10 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e  .** until it can
91a20 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f   be obtained..*/
91a30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
91a40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
91a50 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74  nter(sqlite3_mut
91a60 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  ex *p){.  if( p 
91a70 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
91a80 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
91a90 2e 78 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b  .xMutexEnter(p);
91aa0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62  .  }.}../*.** Ob
91ab0 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70  tain the mutex p
91ac0 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
91ad0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
91ae0 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  K. Otherwise, if
91af0 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65   another.** thre
91b00 61 64 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74  ad holds the mut
91b10 65 78 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74  ex and it cannot
91b20 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65   be obtained, re
91b30 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
91b40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
91b50 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  int sqlite3_mute
91b60 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75  x_try(sqlite3_mu
91b70 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  tex *p){.  int r
91b80 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
91b90 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65   if( p ){.    re
91ba0 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62  turn sqlite3Glob
91bb0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91bc0 4d 75 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d  MutexTry(p);.  }
91bd0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
91be0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
91bf0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
91c00 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
91c10 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 20   mutex that was 
91c20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e  previously.** en
91c30 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
91c40 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
91c50 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
91c60 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
91c70 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  x .** is not cur
91c80 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20  rently entered. 
91c90 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  If a NULL pointe
91ca0 72 20 69 73 20 70 61 73 73 65 64 20 61 73 20 61  r is passed as a
91cb0 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68  n argument.** th
91cc0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
91cd0 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
91ce0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
91cf0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
91d00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
91d10 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
91d20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
91d30 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
91d40 78 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d  xLeave(p);.  }.}
91d50 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
91d60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
91d70 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
91d80 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
91d90 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74  x_notheld() rout
91da0 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e  ine are.** inten
91db0 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69  ded for use insi
91dc0 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
91dd0 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ements..*/.SQLIT
91de0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
91df0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c  3_mutex_held(sql
91e00 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
91e10 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c    return p==0 ||
91e20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
91e30 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
91e40 78 48 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49  xHeld(p);.}.SQLI
91e50 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
91e60 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
91e70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
91e80 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  p){.  return p==
91e90 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  0 || sqlite3Glob
91ea0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91eb0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b  MutexNotheld(p);
91ec0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  .}.#endif..#endi
91ed0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  f /* SQLITE_MUTE
91ee0 58 5f 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  X_OMIT */../****
91ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
91f00 66 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a  f mutex.c ******
91f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
91f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
91f50 20 66 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70   file mutex_noop
91f60 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
91f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
91f90 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37   2008 October 07
91fa0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
91fb0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
91fc0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
91fd0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
91fe0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
91ff0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
92000 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
92010 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
92020 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
92030 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
92040 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
92050 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
92060 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
92070 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
92080 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
92090 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
920a0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
920b0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
920c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
920d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
920e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
920f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
92100 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
92110 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
92120 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
92130 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a  ment mutexes..**
92140 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  .** This impleme
92150 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20  ntation in this 
92160 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72  file does not pr
92170 6f 76 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c  ovide any mutual
92180 0a 2a 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e  .** exclusion an
92190 64 20 69 73 20 74 68 75 73 20 73 75 69 74 61 62  d is thus suitab
921a0 6c 65 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20  le for use only 
921b0 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  in applications.
921c0 2a 2a 20 74 68 61 74 20 75 73 65 20 53 51 4c 69  ** that use SQLi
921d0 74 65 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74  te in a single t
921e0 68 72 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74  hread.  The rout
921f0 69 6e 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20  ines defined.** 
92200 68 65 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68  here are place-h
92210 6f 6c 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61  olders.  Applica
92220 74 69 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69  tions can substi
92230 74 75 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20  tute working.** 
92240 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61  mutex routines a
92250 74 20 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69  t start-time usi
92260 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20  ng the.**.**    
92270 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
92280 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
92290 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69  TEX,...).**.** i
922a0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
922b0 49 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  If compiled with
922c0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74   SQLITE_DEBUG, t
922d0 68 65 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  hen additional l
922e0 6f 67 69 63 20 69 73 20 69 6e 73 65 72 74 65 64  ogic is inserted
922f0 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72  .** that does er
92300 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20  ror checking on 
92310 6d 75 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20  mutexes to make 
92320 73 75 72 65 20 74 68 65 79 20 61 72 65 20 62 65  sure they are be
92330 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f  ing.** called co
92340 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a 0a 0a 23 69  rrectly..*/...#i
92350 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
92360 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20  _MUTEX_NOOP) && 
92370 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
92380 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75  DEBUG)./*.** Stu
92390 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61  b routines for a
923a0 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73  ll mutex methods
923b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
923c0 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f  tines provide no
923d0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f   mutual exclusio
923e0 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b  n or error check
923f0 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
92400 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64  nt noopMutexHeld
92410 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
92420 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a  p){ return 1; }.
92430 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d  static int noopM
92440 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
92450 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72  te3_mutex *p){ r
92460 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69  eturn 1; }.stati
92470 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49  c int noopMutexI
92480 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72  nit(void){ retur
92490 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73  n SQLITE_OK; }.s
924a0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
924b0 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
924c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
924d0 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  }.static sqlite3
924e0 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65  _mutex *noopMute
924f0 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20  xAlloc(int id){ 
92500 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f  return (sqlite3_
92510 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74  mutex*)8; }.stat
92520 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65  ic void noopMute
92530 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  xFree(sqlite3_mu
92540 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b  tex *p){ return;
92550 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e   }.static void n
92560 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71  oopMutexEnter(sq
92570 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
92580 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69   return; }.stati
92590 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54  c int noopMutexT
925a0 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ry(sqlite3_mutex
925b0 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c   *p){ return SQL
925c0 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
925d0 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c   void noopMutexL
925e0 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
925f0 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20  ex *p){ return; 
92600 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
92610 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  E sqlite3_mutex_
92620 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
92630 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69  DefaultMutex(voi
92640 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c  d){.  static sql
92650 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
92660 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20  ds sMutex = {.  
92670 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c    noopMutexInit,
92680 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e  .    noopMutexEn
92690 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  d,.    noopMutex
926a0 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d  Alloc,.    noopM
926b0 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f  utexFree,.    no
926c0 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20  opMutexEnter,.  
926d0 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a    noopMutexTry,.
926e0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61      noopMutexLea
926f0 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  ve,..    noopMut
92700 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70  exHeld,.    noop
92710 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d  MutexNotheld.  }
92720 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75  ;..  return &sMu
92730 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  tex;.}.#endif /*
92740 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
92750 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21  MUTEX_NOOP) && !
92760 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
92770 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65  EBUG) */..#if de
92780 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54  fined(SQLITE_MUT
92790 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69  EX_NOOP) && defi
927a0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
927b0 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20  )./*.** In this 
927c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
927d0 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69  error checking i
927e0 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74  s provided for t
927f0 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65  esting.** and de
92800 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73  bugging purposes
92810 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73  .  The mutexes s
92820 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76  till do not prov
92830 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61  ide any.** mutua
92840 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a  l exclusion..*/.
92850 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ./*.** The mutex
92860 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63   object.*/.struc
92870 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  t sqlite3_mutex 
92880 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20  {.  int id;     
92890 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70  /* The mutex typ
928a0 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20  e */.  int cnt; 
928b0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
928c0 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20  entries without 
928d0 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65  a matching leave
928e0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
928f0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
92900 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74  held() and sqlit
92910 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
92920 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a  () routine are.*
92930 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  * intended for u
92940 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
92950 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
92960 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62  /.static int deb
92970 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69  ugMutexHeld(sqli
92980 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
92990 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
929a0 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74  p->cnt>0;.}.stat
929b0 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
929c0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
929d0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65  _mutex *p){.  re
929e0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e  turn p==0 || p->
929f0 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  cnt==0;.}../*.**
92a00 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
92a10 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  deinitialize the
92a20 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
92a30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
92a40 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76  debugMutexInit(v
92a50 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
92a60 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
92a70 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45   int debugMutexE
92a80 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  nd(void){ return
92a90 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f   SQLITE_OK; }../
92aa0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
92ab0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
92ac0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
92ad0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
92ae0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
92af0 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
92b00 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
92b10 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
92b20 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
92b30 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
92b40 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ed. .*/.static s
92b50 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65  qlite3_mutex *de
92b60 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  bugMutexAlloc(in
92b70 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  t id){.  static 
92b80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53  sqlite3_mutex aS
92b90 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69  tatic[6];.  sqli
92ba0 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20  te3_mutex *pNew 
92bb0 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69  = 0;.  switch( i
92bc0 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  d ){.    case SQ
92bd0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a  LITE_MUTEX_FAST:
92be0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
92bf0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
92c00 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d  : {.      pNew =
92c10 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73   sqlite3Malloc(s
92c20 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20  izeof(*pNew));. 
92c30 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b       if( pNew ){
92c40 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  .        pNew->i
92c50 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20  d = id;.        
92c60 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20  pNew->cnt = 0;. 
92c70 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
92c80 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
92c90 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61  fault: {.      a
92ca0 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30  ssert( id-2 >= 0
92cb0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
92cc0 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73  ( id-2 < (int)(s
92cd0 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73  izeof(aStatic)/s
92ce0 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d  izeof(aStatic[0]
92cf0 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77  )) );.      pNew
92d00 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32   = &aStatic[id-2
92d10 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  ];.      pNew->i
92d20 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72  d = id;.      br
92d30 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
92d40 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
92d50 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
92d60 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
92d70 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  a previously all
92d80 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f  ocated mutex..*/
92d90 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62  .static void deb
92da0 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69  ugMutexFree(sqli
92db0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
92dc0 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d   assert( p->cnt=
92dd0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
92de0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
92df0 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69  TEX_FAST || p->i
92e00 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
92e10 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73  RECURSIVE );.  s
92e20 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
92e30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
92e40 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
92e50 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
92e60 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69  utex_try() routi
92e70 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  nes attempt.** t
92e80 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e  o enter a mutex.
92e90 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72    If another thr
92ea0 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
92eb0 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
92ec0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
92ed0 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
92ee0 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
92ef0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
92f00 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
92f10 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71  TE_BUSY.  The sq
92f20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
92f30 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
92f40 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  rns SQLITE_OK.**
92f50 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c   upon successful
92f60 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73   entry.  Mutexes
92f70 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53   created using S
92f80 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
92f90 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20  RSIVE can.** be 
92fa0 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  entered multiple
92fb0 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61   times by the sa
92fc0 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73  me thread.  In s
92fd0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
92fe0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
92ff0 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
93000 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
93010 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
93020 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
93030 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65  er.  If the same
93040 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f   thread tries to
93050 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72   enter any other
93060 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a   kind of mutex.*
93070 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
93080 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
93090 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
930a0 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75  static void debu
930b0 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69  gMutexEnter(sqli
930c0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
930d0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
930e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
930f0 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d  URSIVE || debugM
93100 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
93110 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a  ;.  p->cnt++;.}.
93120 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67  static int debug
93130 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33  MutexTry(sqlite3
93140 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
93150 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
93160 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
93170 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65  IVE || debugMute
93180 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20  xNotheld(p) );. 
93190 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74   p->cnt++;.  ret
931a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
931b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
931c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
931d0 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20  ) routine exits 
931e0 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73  a mutex that was
931f0 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65  .** previously e
93200 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61  ntered by the sa
93210 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  me thread.  The 
93220 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75  behavior.** is u
93230 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
93240 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72  mutex is not cur
93250 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f  rently entered o
93260 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72  r.** is not curr
93270 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e  ently allocated.
93280 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65    SQLite will ne
93290 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a  ver do either..*
932a0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65  /.static void de
932b0 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71  bugMutexLeave(sq
932c0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
932d0 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67  .  assert( debug
932e0 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a  MutexHeld(p) );.
932f0 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73    p->cnt--;.  as
93300 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
93310 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
93320 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65  IVE || debugMute
93330 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d  xNotheld(p) );.}
93340 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
93350 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
93360 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44  ethods *sqlite3D
93370 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64  efaultMutex(void
93380 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
93390 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
933a0 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20  s sMutex = {.   
933b0 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c   debugMutexInit,
933c0 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45  .    debugMutexE
933d0 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74  nd,.    debugMut
933e0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62  exAlloc,.    deb
933f0 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20  ugMutexFree,.   
93400 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72   debugMutexEnter
93410 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  ,.    debugMutex
93420 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75  Try,.    debugMu
93430 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64  texLeave,..    d
93440 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20  ebugMutexHeld,. 
93450 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74     debugMutexNot
93460 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74  held.  };..  ret
93470 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23  urn &sMutex;.}.#
93480 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
93490 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f  (SQLITE_MUTEX_NO
934a0 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53  OP) && defined(S
934b0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a  QLITE_DEBUG) */.
934c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
934d0 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f   End of mutex_no
934e0 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  op.c ***********
934f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
93510 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
93520 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
93530 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  x_os2.c ********
93540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
93560 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
93570 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 28.**.** The 
93580 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
93590 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
935a0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
935b0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
935c0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
935d0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
935e0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
935f0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
93600 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
93610 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
93620 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
93630 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
93640 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
93650 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
93660 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
93670 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
93680 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
93690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
936a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
936b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
936c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
936d0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
936e0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
936f0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
93700 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65  implement mutexe
93710 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2f 0a 0a 2f  s for OS/2.*/../
93720 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
93730 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
93740 6c 79 20 75 73 65 64 20 69 66 20 53 51 4c 49 54  ly used if SQLIT
93750 45 5f 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64  E_MUTEX_OS2 is d
93760 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74  efined..** See t
93770 68 65 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20  he mutex.h file 
93780 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
93790 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  #ifdef SQLITE_MU
937a0 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a  TEX_OS2../******
937b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
937c0 20 4f 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c   OS/2 Mutex Impl
937d0 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a  ementation *****
937e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
937f0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  *.**.** This imp
93800 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d  lementation of m
93810 75 74 65 78 65 73 20 69 73 20 62 75 69 6c 74 20  utexes is built 
93820 75 73 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41  using the OS/2 A
93830 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  PI..*/../*.** Th
93840 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a  e mutex object.*
93850 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65  * Each recursive
93860 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73   mutex is an ins
93870 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
93880 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
93890 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
938a0 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d  te3_mutex {.  HM
938b0 54 58 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20  TX mutex;       
938c0 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c  /* Mutex control
938d0 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f  ling the lock */
938e0 0a 20 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20  .  int  id;     
938f0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79       /* Mutex ty
93900 70 65 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65  pe */.  int  nRe
93910 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  f;        /* Num
93920 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
93930 73 20 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65  s */.  TID  owne
93940 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65  r;       /* Thre
93950 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  ad holding this 
93960 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65  mutex */.};..#de
93970 66 69 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49  fine OS2_MUTEX_I
93980 4e 49 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30  NITIALIZER   0,0
93990 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  ,0,0../*.** Init
939a0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69  ialize and deini
939b0 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65  tialize the mute
939c0 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  x subsystem..*/.
939d0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75  static int os2Mu
939e0 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72  texInit(void){ r
939f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
93a00 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73   }.static int os
93a10 32 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b  2MutexEnd(void){
93a20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
93a30 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K; }../*.** The 
93a40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
93a50 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c  loc() routine al
93a60 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
93a70 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72   mutex and retur
93a80 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
93a90 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72  it.  If it retur
93aa0 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
93ab0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
93ac0 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
93ad0 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53  allocated. .** S
93ae0 51 4c 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e  QLite will unwin
93af0 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20  d its stack and 
93b00 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
93b10 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a    The argument.*
93b20 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
93b30 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
93b40 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
93b50 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  er constants:.**
93b60 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
93b70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
93b80 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20  AST             
93b90 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c    0.** <li>  SQL
93ba0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
93bb0 49 56 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a  IVE          1.*
93bc0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
93bd0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
93be0 45 52 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69  ER      2.** <li
93bf0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
93c00 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20  STATIC_MEM      
93c10 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51     3.** <li>  SQ
93c20 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
93c30 43 5f 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a  C_PRNG        4.
93c40 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
93c50 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e  he first two con
93c60 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c  stants cause sql
93c70 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
93c80 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20  () to create.** 
93c90 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68  a new mutex.  Th
93ca0 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72  e new mutex is r
93cb0 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51  ecursive when SQ
93cc0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
93cd0 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20  SIVE.** is used 
93ce0 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72  but not necessar
93cf0 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49  ily so when SQLI
93d00 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73  TE_MUTEX_FAST is
93d10 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75   used..** The mu
93d20 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
93d30 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
93d40 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69   to make a disti
93d50 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65  nction.** betwee
93d60 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
93d70 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c  ECURSIVE and SQL
93d80 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
93d90 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  f it does.** not
93da0 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53   want to.  But S
93db0 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  QLite will only 
93dc0 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73  request a recurs
93dd0 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20  ive mutex in.** 
93de0 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72  cases where it r
93df0 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e  eally needs one.
93e00 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f    If a faster no
93e10 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  n-recursive mute
93e20 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  x.** implementat
93e30 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ion is available
93e40 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61   on the host pla
93e50 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78  tform, the mutex
93e60 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69   subsystem.** mi
93e70 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20  ght return such 
93e80 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f  a mutex in respo
93e90 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55  nse to SQLITE_MU
93ea0 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20  TEX_FAST..**.** 
93eb0 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65  The other allowe
93ec0 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  d parameters to 
93ed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
93ee0 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72  loc() each retur
93ef0 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
93f00 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78  o a static preex
93f10 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54  isting mutex.  T
93f20 68 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 65  hree static mute
93f30 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20  xes are.** used 
93f40 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  by the current v
93f50 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  ersion of SQLite
93f60 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f  .  Future versio
93f70 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20  ns of SQLite.** 
93f80 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e  may add addition
93f90 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  al static mutexe
93fa0 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78  s.  Static mutex
93fb0 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72  es are for inter
93fc0 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51  nal.** use by SQ
93fd0 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c  Lite only.  Appl
93fe0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73  ications that us
93ff0 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73  e SQLite mutexes
94000 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f   should.** use o
94010 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20  nly the dynamic 
94020 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64  mutexes returned
94030 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   by SQLITE_MUTEX
94040 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49  _FAST or.** SQLI
94050 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
94060 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  VE..**.** Note t
94070 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68  hat if one of th
94080 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20  e dynamic mutex 
94090 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49  parameters (SQLI
940a0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a  TE_MUTEX_FAST.**
940b0 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   or SQLITE_MUTEX
940c0 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75  _RECURSIVE) is u
940d0 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33  sed then sqlite3
940e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a  _mutex_alloc().*
940f0 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66  * returns a diff
94100 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65  erent mutex on e
94110 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20  very call.  But 
94120 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a  for the static.*
94130 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74  * mutex types, t
94140 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73  he same mutex is
94150 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65   returned on eve
94160 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73  ry call that has
94170 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70  .** the same typ
94180 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
94190 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tic sqlite3_mute
941a0 78 20 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63  x *os2MutexAlloc
941b0 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73  (int iType){.  s
941c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20  qlite3_mutex *p 
941d0 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68  = NULL;.  switch
941e0 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63  ( iType ){.    c
941f0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
94200 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20  _FAST:.    case 
94210 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
94220 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20  URSIVE: {.      
94230 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  p = sqlite3Mallo
94240 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70  cZero( sizeof(*p
94250 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ) );.      if( p
94260 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
94270 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20  d = iType;.     
94280 20 20 20 69 66 28 20 44 6f 73 43 72 65 61 74 65     if( DosCreate
94290 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d  MutexSem( 0, &p-
942a0 3e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45  >mutex, 0, FALSE
942b0 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29   ) != NO_ERROR )
942c0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
942d0 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20  te3_free( p );. 
942e0 20 20 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c           p = NUL
942f0 4c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  L;.        }.   
94300 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
94310 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
94320 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61  ult: {.      sta
94330 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74  tic volatile int
94340 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20   isInit = 0;.   
94350 20 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65     static sqlite
94360 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75  3_mutex staticMu
94370 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20  texes[] = {.    
94380 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f      { OS2_MUTEX_
94390 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a  INITIALIZER, },.
943a0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
943b0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
943c0 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
943d0 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
943e0 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20  ZER, },.        
943f0 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54  { OS2_MUTEX_INIT
94400 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20  IALIZER, },.    
94410 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f      { OS2_MUTEX_
94420 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a  INITIALIZER, },.
94430 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
94440 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
94450 20 7d 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20   },.      };.   
94460 20 20 20 69 66 20 28 20 21 69 73 49 6e 69 74 20     if ( !isInit 
94470 29 7b 0a 20 20 20 20 20 20 20 20 41 50 49 52 45  ){.        APIRE
94480 54 20 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54  T rc;.        PT
94490 49 42 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20  IB ptib;.       
944a0 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20   PPIB ppib;.    
944b0 20 20 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a      HMTX mutex;.
944c0 20 20 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d          char nam
944d0 65 5b 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44  e[32];.        D
944e0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
944f0 20 26 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b   &ptib, &ppib );
94500 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
94510 5f 73 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f  _snprintf( sizeo
94520 66 28 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22  f(name), name, "
94530 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25  \\SEM32\\SQLITE%
94540 30 34 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20  04x",.          
94550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94560 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20  ppib->pib_ulpid 
94570 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  );.        while
94580 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  ( !isInit ){.   
94590 20 20 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30         mutex = 0
945a0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
945b0 20 44 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53   DosCreateMutexS
945c0 65 6d 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78  em( name, &mutex
945d0 2c 20 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20  , 0, FALSE);.   
945e0 20 20 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d         if( rc ==
945f0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
94600 20 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65           unsigne
94610 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20  d int i;.       
94620 20 20 20 20 20 69 66 28 20 21 69 73 49 6e 69 74       if( !isInit
94630 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
94640 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20    for( i = 0; i 
94650 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d  < sizeof(staticM
94660 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73  utexes)/sizeof(s
94670 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29  taticMutexes[0])
94680 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20  ; i++ ){.       
94690 20 20 20 20 20 20 20 20 20 44 6f 73 43 72 65 61           DosCrea
946a0 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26  teMutexSem( 0, &
946b0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d  staticMutexes[i]
946c0 2e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45  .mutex, 0, FALSE
946d0 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
946e0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
946f0 20 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20    isInit = 1;.  
94700 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
94710 20 20 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65          DosClose
94720 4d 75 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20  MutexSem( mutex 
94730 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
94740 73 65 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52  se if( rc == ERR
94750 4f 52 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d  OR_DUPLICATE_NAM
94760 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  E ){.           
94770 20 44 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a   DosSleep( 1 );.
94780 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
94790 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74  .            ret
947a0 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20  urn p;.         
947b0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
947c0 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
947d0 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20  t( iType-2 >= 0 
947e0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
947f0 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f   iType-2 < sizeo
94800 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29  f(staticMutexes)
94810 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75  /sizeof(staticMu
94820 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20  texes[0]) );.   
94830 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75     p = &staticMu
94840 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a  texes[iType-2];.
94850 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
94860 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ype;.      break
94870 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
94880 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn p;.}.../*.*
94890 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
948a0 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65  eallocates a pre
948b0 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
948c0 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69  d mutex..** SQLi
948d0 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f  te is careful to
948e0 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72   deallocate ever
948f0 79 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20  y mutex that it 
94900 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74  allocates..*/.st
94910 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74  atic void os2Mut
94920 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  exFree(sqlite3_m
94930 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20  utex *p){.  if( 
94940 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
94950 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
94960 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
94970 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
94980 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e  UTEX_FAST || p->
94990 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
949a0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
949b0 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d  DosCloseMutexSem
949c0 28 20 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20  ( p->mutex );.  
949d0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20  sqlite3_free( p 
949e0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
949f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
94a00 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ter() and sqlite
94a10 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
94a20 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a  utines attempt.*
94a30 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74  * to enter a mut
94a40 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20  ex.  If another 
94a50 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64  thread is alread
94a60 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74  y within the mut
94a70 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ex,.** sqlite3_m
94a80 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c  utex_enter() wil
94a90 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69  l block and sqli
94aa0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
94ab0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53  will return.** S
94ac0 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65  QLITE_BUSY.  The
94ad0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
94ae0 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ry() interface r
94af0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
94b00 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73  .** upon success
94b10 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65  ful entry.  Mute
94b20 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e  xes created usin
94b30 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  g SQLITE_MUTEX_R
94b40 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20  ECURSIVE can.** 
94b50 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69  be entered multi
94b60 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65  ple times by the
94b70 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49   same thread.  I
94b80 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65  n such cases the
94b90 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20  ,.** mutex must 
94ba0 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75  be exited an equ
94bb0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  al number of tim
94bc0 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65  es before anothe
94bd0 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20  r thread.** can 
94be0 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73  enter.  If the s
94bf0 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73  ame thread tries
94c00 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74   to enter any ot
94c10 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65  her kind of mute
94c20 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f  x.** more than o
94c30 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f  nce, the behavio
94c40 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  r is undefined..
94c50 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
94c60 73 32 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c  s2MutexEnter(sql
94c70 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
94c80 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44    TID tid;.  PID
94c90 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e   holder1;.  ULON
94ca0 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28  G holder2;.  if(
94cb0 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   p==0 ) return;.
94cc0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
94cd0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
94ce0 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75  CURSIVE || os2Mu
94cf0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
94d00 0a 20 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74  .  DosRequestMut
94d10 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20  exSem(p->mutex, 
94d20 53 45 4d 5f 49 4e 44 45 46 49 4e 49 54 45 5f 57  SEM_INDEFINITE_W
94d30 41 49 54 29 3b 0a 20 20 44 6f 73 51 75 65 72 79  AIT);.  DosQuery
94d40 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
94d50 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69  x, &holder1, &ti
94d60 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20  d, &holder2);.  
94d70 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a  p->owner = tid;.
94d80 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73    p->nRef++;.}.s
94d90 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74  tatic int os2Mut
94da0 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75  exTry(sqlite3_mu
94db0 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  tex *p){.  int r
94dc0 63 3b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20  c;.  TID tid;.  
94dd0 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55  PID holder1;.  U
94de0 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20  LONG holder2;.  
94df0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
94e00 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  n SQLITE_OK;.  a
94e10 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
94e20 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
94e30 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78  SIVE || os2Mutex
94e40 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
94e50 69 66 28 20 44 6f 73 52 65 71 75 65 73 74 4d 75  if( DosRequestMu
94e60 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c  texSem(p->mutex,
94e70 20 53 45 4d 5f 49 4d 4d 45 44 49 41 54 45 5f 52   SEM_IMMEDIATE_R
94e80 45 54 55 52 4e 29 20 3d 3d 20 4e 4f 5f 45 52 52  ETURN) == NO_ERR
94e90 4f 52 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65  OR) {.    DosQue
94ea0 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75  ryMutexSem(p->mu
94eb0 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26  tex, &holder1, &
94ec0 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a  tid, &holder2);.
94ed0 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74      p->owner = t
94ee0 69 64 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b  id;.    p->nRef+
94ef0 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  +;.    rc = SQLI
94f00 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 6c 73 65 20  TE_OK;.  } else 
94f10 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
94f20 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 72  E_BUSY;.  }..  r
94f30 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
94f40 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
94f50 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75  utex_leave() rou
94f60 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74  tine exits a mut
94f70 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70  ex that was.** p
94f80 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65  reviously entere
94f90 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  d by the same th
94fa0 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76  read.  The behav
94fb0 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  ior.** is undefi
94fc0 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78  ned if the mutex
94fd0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
94fe0 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20  y entered or.** 
94ff0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
95000 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c   allocated.  SQL
95010 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64  ite will never d
95020 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61  o either..*/.sta
95030 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65  tic void os2Mute
95040 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d  xLeave(sqlite3_m
95050 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20  utex *p){.  TID 
95060 74 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65  tid;.  PID holde
95070 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64  r1;.  ULONG hold
95080 65 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  er2;.  if( p==0 
95090 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
950a0 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b  rt( p->nRef>0 );
950b0 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78  .  DosQueryMutex
950c0 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68  Sem(p->mutex, &h
950d0 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68  older1, &tid, &h
950e0 6f 6c 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72  older2);.  asser
950f0 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64  t( p->owner==tid
95100 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b   );.  p->nRef--;
95110 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
95120 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d  ef==0 || p->id==
95130 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
95140 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52  URSIVE );.  DosR
95150 65 6c 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70  eleaseMutexSem(p
95160 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66  ->mutex);.}..#if
95170 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
95180 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
95190 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
951a0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
951b0 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74  x_notheld() rout
951c0 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e  ine are.** inten
951d0 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69  ded for use insi
951e0 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
951f0 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  ements..*/.stati
95200 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 48 65  c int os2MutexHe
95210 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
95220 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b   *p){.  TID tid;
95230 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c  .  PID pid;.  UL
95240 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50  ONG ulCount;.  P
95250 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20  TIB ptib;.  if( 
95260 70 21 3d 30 20 29 20 7b 0a 20 20 20 20 44 6f 73  p!=0 ) {.    Dos
95270 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d  QueryMutexSem(p-
95280 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74  >mutex, &pid, &t
95290 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20  id, &ulCount);. 
952a0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f   } else {.    Do
952b0 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26  sGetInfoBlocks(&
952c0 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20  ptib, NULL);.   
952d0 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62   tid = ptib->tib
952e0 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74  _ptib2->tib2_ult
952f0 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  id;.  }.  return
95300 20 70 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65   p==0 || (p->nRe
95310 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72  f!=0 && p->owner
95320 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63  ==tid);.}.static
95330 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74   int os2MutexNot
95340 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
95350 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69  ex *p){.  TID ti
95360 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20  d;.  PID pid;.  
95370 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20  ULONG ulCount;. 
95380 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66   PTIB ptib;.  if
95390 28 20 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20  ( p!= 0 ) {.    
953a0 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d  DosQueryMutexSem
953b0 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c  (p->mutex, &pid,
953c0 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29   &tid, &ulCount)
953d0 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
953e0 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
953f0 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a  s(&ptib, NULL);.
95400 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e      tid = ptib->
95410 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f  tib_ptib2->tib2_
95420 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74  ultid;.  }.  ret
95430 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e  urn p==0 || p->n
95440 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e  Ref==0 || p->own
95450 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69  er!=tid;.}.#endi
95460 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
95470 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  E sqlite3_mutex_
95480 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
95490 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69  DefaultMutex(voi
954a0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c  d){.  static sql
954b0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
954c0 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20  ds sMutex = {.  
954d0 20 20 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a    os2MutexInit,.
954e0 20 20 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c      os2MutexEnd,
954f0 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c  .    os2MutexAll
95500 6f 63 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  oc,.    os2Mutex
95510 46 72 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74  Free,.    os2Mut
95520 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32  exEnter,.    os2
95530 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73  MutexTry,.    os
95540 32 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66  2MutexLeave,.#if
95550 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
95560 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c  .    os2MutexHel
95570 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e  d,.    os2MutexN
95580 6f 74 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20  otheld.#endif.  
95590 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d  };..  return &sM
955a0 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  utex;.}.#endif /
955b0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  * SQLITE_MUTEX_O
955c0 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  S2 */../********
955d0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75  ****** End of mu
955e0 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a  tex_os2.c ******
955f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95610 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
95620 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
95630 65 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a  e mutex_unix.c *
95640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95660 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
95670 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a  7 August 28.**.*
95680 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
95690 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
956a0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
956b0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
956c0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
956d0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
956e0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
956f0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
95700 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
95710 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
95720 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
95730 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
95740 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
95750 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
95760 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
95770 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
95780 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
95790 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
957a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
957b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
957c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
957d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
957e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
957f0 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
95800 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
95810 6d 75 74 65 78 65 73 20 66 6f 72 20 70 74 68 72  mutexes for pthr
95820 65 61 64 73 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  eads.*/../*.** T
95830 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
95840 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65  file is only use
95850 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70  d if we are comp
95860 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66 65  iling threadsafe
95870 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 77  .** under unix w
95880 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a  ith pthreads..**
95890 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68  .** Note that th
958a0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
958b0 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65 72  n requires a ver
958c0 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 73  sion of pthreads
958d0 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74   that.** support
958e0 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  s recursive mute
958f0 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  xes..*/.#ifdef S
95900 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52  QLITE_MUTEX_PTHR
95910 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c  EADS..#include <
95920 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a  pthread.h>.../*.
95930 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76  ** Each recursiv
95940 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e  e mutex is an in
95950 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
95960 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
95970 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c  e..*/.struct sql
95980 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70  ite3_mutex {.  p
95990 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d  thread_mutex_t m
959a0 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74  utex;     /* Mut
959b0 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74  ex controlling t
959c0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  he lock */.  int
959d0 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20   id;            
959e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78          /* Mutex
959f0 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   type */.  int n
95a00 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
95a10 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
95a20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a  of entrances */.
95a30 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e 65    pthread_t owne
95a40 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
95a50 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 77  Thread that is w
95a60 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78  ithin this mutex
95a70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
95a80 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74 72  E_DEBUG.  int tr
95a90 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ace;            
95aa0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
95ab0 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a 2f  trace changes */
95ac0 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65  .#endif.};.#ifde
95ad0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23  f SQLITE_DEBUG.#
95ae0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d  define SQLITE3_M
95af0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
95b00 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58   { PTHREAD_MUTEX
95b10 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c  _INITIALIZER, 0,
95b20 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30   0, (pthread_t)0
95b30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66  , 0 }.#else.#def
95b40 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45  ine SQLITE3_MUTE
95b50 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20  X_INITIALIZER { 
95b60 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e  PTHREAD_MUTEX_IN
95b70 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c  ITIALIZER, 0, 0,
95b80 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d 0a   (pthread_t)0 }.
95b90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
95ba0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
95bb0 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74  held() and sqlit
95bc0 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
95bd0 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a  () routine are.*
95be0 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  * intended for u
95bf0 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61  se only inside a
95c00 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
95c10 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c 61  ts.  On some pla
95c20 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 65  tforms,.** there
95c30 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20 63   might be race c
95c40 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 63  onditions that c
95c50 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20 72  an cause these r
95c60 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 65  outines to.** de
95c70 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74 20  liver incorrect 
95c80 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 72  results.  In par
95c90 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68 72  ticular, if pthr
95ca0 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a 2a  ead_equal() is.*
95cb0 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 20  * not an atomic 
95cc0 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20  operation, then 
95cd0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d  these routines m
95ce0 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a 2a  ight delivery.**
95cf0 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c   incorrect resul
95d00 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c 61  ts.  On most pla
95d10 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 5f  tforms, pthread_
95d20 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a 2a  equal() is a .**
95d30 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74   comparison of t
95d40 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20  wo integers and 
95d50 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74 6f  is therefore ato
95d60 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72 65  mic.  But we are
95d70 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 50  .** told that HP
95d80 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20 61  UX is not such a
95d90 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 73   platform.  If s
95da0 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f  o, then these ro
95db0 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 6e  utines.** will n
95dc0 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 63  ot always work c
95dd0 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 58  orrectly on HPUX
95de0 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65  ..**.** On those
95df0 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 65   platforms where
95e00 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29   pthread_equal()
95e10 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20   is not atomic, 
95e20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c 64  SQLite.** should
95e30 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   be compiled wit
95e40 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 45  hout -DSQLITE_DE
95e50 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44 4e  BUG and with -DN
95e60 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b 65  DEBUG to.** make
95e70 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74 28   sure no assert(
95e80 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  ) statements are
95e90 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 68   evaluated and h
95ea0 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 6f  ence these.** ro
95eb0 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65 72  utines are never
95ec0 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20   called..*/.#if 
95ed0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
95ee0 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
95ef0 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63  TE_DEBUG).static
95f00 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65   int pthreadMute
95f10 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  xHeld(sqlite3_mu
95f20 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  tex *p){.  retur
95f30 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26  n (p->nRef!=0 &&
95f40 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70   pthread_equal(p
95f50 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64  ->owner, pthread
95f60 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 61  _self()));.}.sta
95f70 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d  tic int pthreadM
95f80 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
95f90 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
95fa0 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d   return p->nRef=
95fb0 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65 71  =0 || pthread_eq
95fc0 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74  ual(p->owner, pt
95fd0 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d 30  hread_self())==0
95fe0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
95ff0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  * Initialize and
96000 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   deinitialize th
96010 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
96020 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m..*/.static int
96030 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69   pthreadMutexIni
96040 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  t(void){ return 
96050 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61  SQLITE_OK; }.sta
96060 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d  tic int pthreadM
96070 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72  utexEnd(void){ r
96080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
96090 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71   }../*.** The sq
960a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
960b0 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  c() routine allo
960c0 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d  cates a new.** m
960d0 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73  utex and returns
960e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
960f0 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73  .  If it returns
96100 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65   NULL.** that me
96110 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78  ans that a mutex
96120 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c   could not be al
96130 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65  located.  SQLite
96140 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20  .** will unwind 
96150 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65  its stack and re
96160 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20  turn an error.  
96170 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  The argument.** 
96180 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
96190 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20  _alloc() is one 
961a0 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  of these integer
961b0 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a   constants:.**.*
961c0 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
961d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
961e0 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  T.** <li>  SQLIT
961f0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
96200 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  E.** <li>  SQLIT
96210 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
96220 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53  ASTER.** <li>  S
96230 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
96240 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20  IC_MEM.** <li>  
96250 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
96260 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e  TIC_MEM2.** <li>
96270 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
96280 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c  TATIC_PRNG.** <l
96290 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
962a0 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c  _STATIC_LRU.** <
962b0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
962c0 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a  X_STATIC_LRU2.**
962d0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
962e0 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74   first two const
962f0 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74  ants cause sqlit
96300 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
96310 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20   to create.** a 
96320 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20  new mutex.  The 
96330 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63  new mutex is rec
96340 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49  ursive when SQLI
96350 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
96360 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75  VE.** is used bu
96370 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  t not necessaril
96380 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45  y so when SQLITE
96390 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75  _MUTEX_FAST is u
963a0 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65  sed..** The mute
963b0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
963c0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
963d0 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  o make a distinc
963e0 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20  tion.** between 
963f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
96400 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54  URSIVE and SQLIT
96410 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20  E_MUTEX_FAST if 
96420 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77  it does.** not w
96430 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c  ant to.  But SQL
96440 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65  ite will only re
96450 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76  quest a recursiv
96460 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61  e mutex in.** ca
96470 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61  ses where it rea
96480 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20  lly needs one.  
96490 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
964a0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
964b0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
964c0 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
964d0 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
964e0 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
964f0 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
96500 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
96510 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
96520 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
96530 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68  X_FAST..**.** Th
96540 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20  e other allowed 
96550 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71  parameters to sq
96560 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
96570 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a  c() each return.
96580 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
96590 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73  a static preexis
965a0 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 78  ting mutex.  Six
965b0 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   static mutexes 
965c0 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74  are.** used by t
965d0 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69  he current versi
965e0 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46  on of SQLite.  F
965f0 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
96600 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20  f SQLite.** may 
96610 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  add additional s
96620 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20  tatic mutexes.  
96630 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  Static mutexes a
96640 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a  re for internal.
96650 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  ** use by SQLite
96660 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74   only.  Applicat
96670 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51  ions that use SQ
96680 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f  Lite mutexes sho
96690 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20  uld.** use only 
966a0 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
966b0 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  xes returned by 
966c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
966d0 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  T or.** SQLITE_M
966e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a  UTEX_RECURSIVE..
966f0 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
96700 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79  if one of the dy
96710 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61  namic mutex para
96720 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d  meters (SQLITE_M
96730 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20  UTEX_FAST.** or 
96740 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
96750 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 20  URSIVE) is used 
96760 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74  then sqlite3_mut
96770 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65  ex_alloc().** re
96780 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e  turns a differen
96790 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79  t mutex on every
967a0 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20   call.  But for 
967b0 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d  the static .** m
967c0 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20  utex types, the 
967d0 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65  same mutex is re
967e0 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20  turned on every 
967f0 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a  call that has.**
96800 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e   the same type n
96810 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  umber..*/.static
96820 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
96830 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f  pthreadMutexAllo
96840 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20  c(int iType){.  
96850 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
96860 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 78  utex staticMutex
96870 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51 4c  es[] = {.    SQL
96880 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
96890 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49  ALIZER,.    SQLI
968a0 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
968b0 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54  LIZER,.    SQLIT
968c0 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
968d0 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45  IZER,.    SQLITE
968e0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
968f0 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33  ZER,.    SQLITE3
96900 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
96910 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f  ER,.    SQLITE3_
96920 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
96930 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  R.  };.  sqlite3
96940 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 77 69  _mutex *p;.  swi
96950 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20  tch( iType ){.  
96960 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55    case SQLITE_MU
96970 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b  TEX_RECURSIVE: {
96980 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74  .      p = sqlit
96990 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69  e3MallocZero( si
969a0 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20  zeof(*p) );.    
969b0 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 65    if( p ){.#ifde
969c0 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f  f SQLITE_HOMEGRO
969d0 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54  WN_RECURSIVE_MUT
969e0 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  EX.        /* If
969f0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
96a00 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c  es are not avail
96a10 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68 61  able, we will ha
96a20 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a  ve to.        **
96a30 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e 20   build our own. 
96a40 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20   See below. */. 
96a50 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
96a60 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75  utex_init(&p->mu
96a70 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20  tex, 0);.#else. 
96a80 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20         /* Use a 
96a90 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
96aa0 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62  if it is availab
96ab0 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 74  le */.        pt
96ac0 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f  hread_mutexattr_
96ad0 74 20 72 65 63 75 72 73 69 76 65 41 74 74 72 3b  t recursiveAttr;
96ae0 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
96af0 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74 28  _mutexattr_init(
96b00 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b  &recursiveAttr);
96b10 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
96b20 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74 79  _mutexattr_setty
96b30 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74 74  pe(&recursiveAtt
96b40 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58  r, PTHREAD_MUTEX
96b50 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20  _RECURSIVE);.   
96b60 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74       pthread_mut
96b70 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65  ex_init(&p->mute
96b80 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 74 74  x, &recursiveAtt
96b90 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72  r);.        pthr
96ba0 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 64 65  ead_mutexattr_de
96bb0 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 76 65  stroy(&recursive
96bc0 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20  Attr);.#endif.  
96bd0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
96be0 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ype;.      }.   
96bf0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
96c00 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
96c10 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20 20  MUTEX_FAST: {.  
96c20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
96c30 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
96c40 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
96c50 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
96c60 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
96c70 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
96c80 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75  utex_init(&p->mu
96c90 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  tex, 0);.      }
96ca0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
96cb0 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
96cc0 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
96cd0 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b   iType-2 >= 0 );
96ce0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
96cf0 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53 69  Type-2 < ArraySi
96d00 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  ze(staticMutexes
96d10 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26  ) );.      p = &
96d20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54  staticMutexes[iT
96d30 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d  ype-2];.      p-
96d40 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20  >id = iType;.   
96d50 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
96d60 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
96d70 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  }.../*.** This r
96d80 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74  outine deallocat
96d90 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a  es a previously.
96da0 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74  ** allocated mut
96db0 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63  ex.  SQLite is c
96dc0 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f  areful to deallo
96dd0 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75  cate every.** mu
96de0 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f  tex that it allo
96df0 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  cates..*/.static
96e00 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74   void pthreadMut
96e10 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  exFree(sqlite3_m
96e20 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65  utex *p){.  asse
96e30 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  rt( p->nRef==0 )
96e40 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
96e50 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
96e60 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  FAST || p->id==S
96e70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
96e80 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72 65  RSIVE );.  pthre
96e90 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f 79  ad_mutex_destroy
96ea0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73  (&p->mutex);.  s
96eb0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
96ec0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
96ed0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
96ee0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
96ef0 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69  utex_try() routi
96f00 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  nes attempt.** t
96f10 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e  o enter a mutex.
96f20 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72    If another thr
96f30 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
96f40 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
96f50 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
96f60 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
96f70 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
96f80 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
96f90 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
96fa0 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71  TE_BUSY.  The sq
96fb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
96fc0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
96fd0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  rns SQLITE_OK.**
96fe0 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c   upon successful
96ff0 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73   entry.  Mutexes
97000 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53   created using S
97010 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
97020 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20  RSIVE can.** be 
97030 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  entered multiple
97040 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61   times by the sa
97050 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73  me thread.  In s
97060 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
97070 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
97080 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
97090 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
970a0 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
970b0 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
970c0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65  er.  If the same
970d0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f   thread tries to
970e0 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72   enter any other
970f0 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a   kind of mutex.*
97100 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
97110 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
97120 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
97130 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 72  static void pthr
97140 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73 71  eadMutexEnter(sq
97150 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
97160 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
97170 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
97180 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72  ECURSIVE || pthr
97190 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  eadMutexNotheld(
971a0 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  p) );..#ifdef SQ
971b0 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52  LITE_HOMEGROWN_R
971c0 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20  ECURSIVE_MUTEX. 
971d0 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 65   /* If recursive
971e0 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74   mutexes are not
971f0 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e   available, then
97200 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77   we have to grow
97210 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20  .  ** our own.  
97220 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  This implementat
97230 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74  ion assumes that
97240 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29   pthread_equal()
97250 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20  .  ** is atomic 
97260 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74  - that it cannot
97270 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e 74   be deceived int
97280 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a  o thinking self.
97290 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65    ** and p->owne
972a0 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 70  r are equal if p
972b0 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20  ->owner changes 
972c0 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75  between two valu
972d0 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65  es.  ** that are
972e0 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65   not equal to se
972f0 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d  lf while the com
97300 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e  parison is takin
97310 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68  g place..  ** Th
97320 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
97330 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61  n also assumes a
97340 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20   coherent cache 
97350 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70  - that .  ** sep
97360 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20  arate processes 
97370 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66  cannot read diff
97380 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f  erent values fro
97390 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20  m the same.  ** 
973a0 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 73  address at the s
973b0 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69  ame time.  If ei
973c0 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77  ther of these tw
973d0 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a  o conditions.  *
973e0 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74  * are not met, t
973f0 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20  hen the mutexes 
97400 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72  will fail and pr
97410 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75  oblems will resu
97420 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20  lt..  */.  {.   
97430 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20   pthread_t self 
97440 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
97450 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65  ;.    if( p->nRe
97460 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f 65  f>0 && pthread_e
97470 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73  qual(p->owner, s
97480 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d  elf) ){.      p-
97490 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 6c  >nRef++;.    }el
974a0 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65 61  se{.      pthrea
974b0 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d  d_mutex_lock(&p-
974c0 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 61  >mutex);.      a
974d0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
974e0 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77  0 );.      p->ow
974f0 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20  ner = self;.    
97500 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20    p->nRef = 1;. 
97510 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20     }.  }.#else. 
97520 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c   /* Use the buil
97530 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d  t-in recursive m
97540 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61  utexes if they a
97550 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20  re available..  
97560 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74  */.  pthread_mut
97570 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  ex_lock(&p->mute
97580 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d  x);.  p->owner =
97590 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b   pthread_self();
975a0 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65  .  p->nRef++;.#e
975b0 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
975c0 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20  ITE_DEBUG.  if( 
975d0 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20  p->trace ){.    
975e0 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75  printf("enter mu
975f0 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68  tex %p (%d) with
97600 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20   nRef=%d\n", p, 
97610 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65  p->trace, p->nRe
97620 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  f);.  }.#endif.}
97630 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72  .static int pthr
97640 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 6c 69  eadMutexTry(sqli
97650 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
97660 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
97670 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
97680 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
97690 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78   || pthreadMutex
976a0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23  Notheld(p) );..#
976b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d  ifdef SQLITE_HOM
976c0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45  EGROWN_RECURSIVE
976d0 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72  _MUTEX.  /* If r
976e0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
976f0 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
97700 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65  le, then we have
97710 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75   to grow.  ** ou
97720 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70  r own.  This imp
97730 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75  lementation assu
97740 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64  mes that pthread
97750 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73  _equal().  ** is
97760 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69   atomic - that i
97770 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65  t cannot be dece
97780 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69  ived into thinki
97790 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64  ng self.  ** and
977a0 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71   p->owner are eq
977b0 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20  ual if p->owner 
977c0 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20  changes between 
977d0 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20  two values.  ** 
977e0 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75  that are not equ
977f0 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65  al to self while
97800 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
97810 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e  is taking place.
97820 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  .  ** This imple
97830 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61  mentation also a
97840 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e  ssumes a coheren
97850 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a  t cache - that .
97860 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72    ** separate pr
97870 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72  ocesses cannot r
97880 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61  ead different va
97890 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61  lues from the sa
978a0 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20  me.  ** address 
978b0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
978c0 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20  .  If either of 
978d0 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74  these two condit
978e0 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f  ions.  ** are no
978f0 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20  t met, then the 
97900 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69  mutexes will fai
97910 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77  l and problems w
97920 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f  ill result..  */
97930 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64  .  {.    pthread
97940 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61  _t self = pthrea
97950 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66  d_self();.    if
97960 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70  ( p->nRef>0 && p
97970 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e  thread_equal(p->
97980 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a  owner, self) ){.
97990 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b        p->nRef++;
979a0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
979b0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  TE_OK;.    }else
979c0 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74   if( pthread_mut
979d0 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d  ex_trylock(&p->m
979e0 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20  utex)==0 ){.    
979f0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
97a00 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d  f==0 );.      p-
97a10 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20  >owner = self;. 
97a20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31       p->nRef = 1
97a30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
97a40 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73  ITE_OK;.    }els
97a50 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  e{.      rc = SQ
97a60 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
97a70 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20  .  }.#else.  /* 
97a80 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  Use the built-in
97a90 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
97aa0 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61  es if they are a
97ab0 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20  vailable..  */. 
97ac0 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74   if( pthread_mut
97ad0 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d  ex_trylock(&p->m
97ae0 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20  utex)==0 ){.    
97af0 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65  p->owner = pthre
97b00 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 70  ad_self();.    p
97b10 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63  ->nRef++;.    rc
97b20 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
97b30 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
97b40 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
97b50 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
97b60 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69  SQLITE_DEBUG.  i
97b70 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
97b80 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a   && p->trace ){.
97b90 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65      printf("ente
97ba0 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20  r mutex %p (%d) 
97bb0 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c  with nRef=%d\n",
97bc0 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d   p, p->trace, p-
97bd0 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64  >nRef);.  }.#end
97be0 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  if.  return rc;.
97bf0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
97c00 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
97c10 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
97c20 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
97c30 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
97c40 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
97c50 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
97c60 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20   behavior.** is 
97c70 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
97c80 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75   mutex is not cu
97c90 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
97ca0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  or.** is not cur
97cb0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
97cc0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
97cd0 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a  ever do either..
97ce0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
97cf0 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65  threadMutexLeave
97d00 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
97d10 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 74  p){.  assert( pt
97d20 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 70  hreadMutexHeld(p
97d30 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d  ) );.  p->nRef--
97d40 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
97d50 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d  Ref==0 || p->id=
97d60 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
97d70 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 64  CURSIVE );..#ifd
97d80 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52  ef SQLITE_HOMEGR
97d90 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55  OWN_RECURSIVE_MU
97da0 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65  TEX.  if( p->nRe
97db0 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 72  f==0 ){.    pthr
97dc0 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b  ead_mutex_unlock
97dd0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  (&p->mutex);.  }
97de0 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61 64  .#else.  pthread
97df0 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70  _mutex_unlock(&p
97e00 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 66  ->mutex);.#endif
97e10 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
97e20 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74  DEBUG.  if( p->t
97e30 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e  race ){.    prin
97e40 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20  tf("leave mutex 
97e50 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65  %p (%d) with nRe
97e60 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74  f=%d\n", p, p->t
97e70 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a  race, p->nRef);.
97e80 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51    }.#endif.}..SQ
97e90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
97ea0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
97eb0 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75  ds *sqlite3Defau
97ec0 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20  ltMutex(void){. 
97ed0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
97ee0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d  mutex_methods sM
97ef0 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 74 68  utex = {.    pth
97f00 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a 20  readMutexInit,. 
97f10 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 45     pthreadMutexE
97f20 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d  nd,.    pthreadM
97f30 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 70  utexAlloc,.    p
97f40 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 2c  threadMutexFree,
97f50 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65  .    pthreadMute
97f60 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 68 72  xEnter,.    pthr
97f70 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 20 20  eadMutexTry,.   
97f80 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61   pthreadMutexLea
97f90 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ve,.#ifdef SQLIT
97fa0 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 68 72  E_DEBUG.    pthr
97fb0 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20  eadMutexHeld,.  
97fc0 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f    pthreadMutexNo
97fd0 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20  theld.#else.    
97fe0 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0a  0,.    0.#endif.
97ff0 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26    };..  return &
98000 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 69  sMutex;.}..#endi
98010 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  f /* SQLITE_MUTE
98020 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f 2a  X_PTHREAD */../*
98030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
98040 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e  d of mutex_unix.
98050 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
98060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
98080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
98090 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 77  gin file mutex_w
980a0 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  32.c ***********
980b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
980c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
980d0 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20  .** 2007 August 
980e0 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  14.**.** The aut
980f0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
98100 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
98110 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
98120 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
98130 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
98140 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
98150 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
98160 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
98170 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
98180 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
98190 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
981a0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
981b0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
981c0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
981d0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
981e0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
981f0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
98200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98240 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
98250 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75  ontains the C fu
98260 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70  nctions that imp
98270 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66  lement mutexes f
98280 6f 72 20 77 69 6e 33 32 0a 2a 2f 0a 0a 2f 2a 0a  or win32.*/../*.
98290 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
982a0 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
982b0 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20   used if we are 
982c0 63 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74  compiling multit
982d0 68 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20  hreaded.** on a 
982e0 77 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f  win32 system..*/
982f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
98300 55 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20  UTEX_W32../*.** 
98310 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d  Each recursive m
98320 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61  utex is an insta
98330 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
98340 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
98350 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
98360 33 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54  3_mutex {.  CRIT
98370 49 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74  ICAL_SECTION mut
98380 65 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20  ex;    /* Mutex 
98390 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20  controlling the 
983a0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64  lock */.  int id
983b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
983c0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79       /* Mutex ty
983d0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  pe */.  int nRef
983e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
983f0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
98400 65 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20  enterances */.  
98410 44 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20  DWORD owner;    
98420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
98430 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69  read holding thi
98440 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f  s mutex */.};../
98450 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
98460 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77   (non-zero) if w
98470 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e  e are running un
98480 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b  der WinNT, Win2K
98490 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57  , WinXP,.** or W
984a0 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61  inCE.  Return fa
984b0 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57  lse (zero) for W
984c0 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20  in95, Win98, or 
984d0 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72  WinME..**.** Her
984e0 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74  e is an interest
984f0 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a  ing observation:
98500 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20    Win95, Win98, 
98510 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a  and WinME lack.*
98520 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78  * the LockFileEx
98530 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20  () API.  But we 
98540 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63  can still static
98550 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73  ally link agains
98560 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73  t that.** API as
98570 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27   long as we don'
98580 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75  t call it win ru
98590 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d  nning Win95/98/M
985a0 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a  E.  A call to.**
985b0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
985c0 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
985d0 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69  ne if the host i
985e0 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72  s Win95/98/ME or
985f0 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20  .** WinNT/2K/XP 
98600 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20  so that we will 
98610 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20  know whether or 
98620 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c  not we can safel
98630 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f  y call.** the Lo
98640 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a  ckFileEx() API..
98650 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28  **.** mutexIsNT(
98660 29 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66  ) is only used f
98670 6f 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 43  or the TryEnterC
98680 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29  riticalSection()
98690 20 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68   API call,.** wh
986a0 69 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69  ich is only avai
986b0 6c 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70  lable if your ap
986c0 70 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f  plication was co
986d0 6d 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20  mpiled with .** 
986e0 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66  _WIN32_WINNT def
986f0 69 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20  ined to a value 
98700 3e 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72  >= 0x0400.  Curr
98710 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a  ently, the only.
98720 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e  ** call to TryEn
98730 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
98740 6f 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 65  on() is #ifdef'e
98750 64 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66  d out, so #ifdef
98760 20 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73   .** this out as
98770 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a   well..*/.#if 0.
98780 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
98790 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74  NCE.# define mut
987a0 65 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65  exIsNT()  (1).#e
987b0 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74  lse.  static int
987c0 20 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29   mutexIsNT(void)
987d0 7b 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74  {.    static int
987e0 20 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20   osType = 0;.   
987f0 20 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29   if( osType==0 )
98800 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f  {.      OSVERSIO
98810 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20  NINFO sInfo;.   
98820 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72     sInfo.dwOSVer
98830 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73  sionInfoSize = s
98840 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20  izeof(sInfo);.  
98850 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78      GetVersionEx
98860 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  (&sInfo);.      
98870 6f 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64  osType = sInfo.d
98880 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52  wPlatformId==VER
98890 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f  _PLATFORM_WIN32_
988a0 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20  NT ? 2 : 1;.    
988b0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54  }.    return osT
988c0 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64  ype==2;.  }.#end
988d0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f  if /* SQLITE_OS_
988e0 57 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a  WINCE */.#endif.
988f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
98900 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
98910 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
98920 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
98930 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
98940 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
98950 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
98960 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65  only inside asse
98970 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
98980 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
98990 69 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69  inMutexHeld(sqli
989a0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
989b0 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21   return p->nRef!
989c0 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d  =0 && p->owner==
989d0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64  GetCurrentThread
989e0 49 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  Id();.}.static i
989f0 6e 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65  nt winMutexNothe
98a00 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
98a10 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
98a20 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
98a30 6f 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e  owner!=GetCurren
98a40 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23  tThreadId();.}.#
98a50 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e  endif.../*.** In
98a60 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69  itialize and dei
98a70 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75  nitialize the mu
98a80 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  tex subsystem..*
98a90 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
98aa0 5f 6d 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f  _mutex winMutex_
98ab0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d  staticMutexes[6]
98ac0 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  ;.static int win
98ad0 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30  Mutex_isInit = 0
98ae0 3b 0a 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78  ;./* As winMutex
98af0 49 6e 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75  Init() and winMu
98b00 74 65 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c  texEnd() are cal
98b10 6c 65 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f  led as part.** o
98b20 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  f the sqlite3_in
98b30 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c  itialize and sql
98b40 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a  ite3_shutdown().
98b50 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74  ** processing, t
98b60 68 65 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22  he "interlocked"
98b70 20 6d 61 67 69 63 20 69 73 20 70 72 6f 62 61 62   magic is probab
98b80 6c 79 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74  ly not.** strict
98b90 6c 79 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f  ly necessary..*/
98ba0 0a 73 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e  .static long win
98bb0 4d 75 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a  Mutex_lock = 0;.
98bc0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
98bd0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
98be0 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20  .  /* The first 
98bf0 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20  to increment to 
98c00 31 20 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e  1 does actual in
98c10 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a  itialization */.
98c20 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65    if( Interlocke
98c30 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65  dCompareExchange
98c40 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c  (&winMutex_lock,
98c50 20 31 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20   1, 0)==0 ){.   
98c60 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
98c70 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
98c80 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  (winMutex_static
98c90 4d 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a  Mutexes); i++){.
98ca0 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65        Initialize
98cb0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
98cc0 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  &winMutex_static
98cd0 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78  Mutexes[i].mutex
98ce0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e  );.    }.    win
98cf0 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31  Mutex_isInit = 1
98d00 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
98d10 2a 20 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69  * Someone else i
98d20 73 20 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73  s in the process
98d30 20 6f 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20   of initing the 
98d40 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a  static mutexes *
98d50 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 21 77 69  /.    while( !wi
98d60 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b  nMutex_isInit ){
98d70 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b  .      Sleep(1);
98d80 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
98d90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a  urn SQLITE_OK; .
98da0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  }..static int wi
98db0 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b  nMutexEnd(void){
98dc0 20 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74   .  /* The first
98dd0 20 74 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f   to decrement to
98de0 20 30 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73   0 does actual s
98df0 68 75 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77  hutdown .  ** (w
98e00 68 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74  hich should be t
98e10 68 65 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64  he last to shutd
98e20 6f 77 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49  own.) */.  if( I
98e30 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72  nterlockedCompar
98e40 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75  eExchange(&winMu
98e50 74 65 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d  tex_lock, 0, 1)=
98e60 3d 31 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69  =1 ){.    if( wi
98e70 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31  nMutex_isInit==1
98e80 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
98e90 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
98ea0 69 3c 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d  i<ArraySize(winM
98eb0 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78  utex_staticMutex
98ec0 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  es); i++){.     
98ed0 20 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61     DeleteCritica
98ee0 6c 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74  lSection(&winMut
98ef0 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
98f00 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  [i].mutex);.    
98f10 20 20 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74    }.      winMut
98f20 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20  ex_isInit = 0;. 
98f30 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
98f40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
98f50 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
98f60 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
98f70 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
98f80 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65  es a new.** mute
98f90 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  x and returns a 
98fa0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
98fb0 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  If it returns NU
98fc0 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73  LL.** that means
98fd0 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f   that a mutex co
98fe0 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63  uld not be alloc
98ff0 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a  ated.  SQLite.**
99000 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73   will unwind its
99010 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72   stack and retur
99020 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65  n an error.  The
99030 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
99040 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
99050 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20  loc() is one of 
99060 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  these integer co
99070 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c  nstants:.**.** <
99080 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  ul>.** <li>  SQL
99090 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a  ITE_MUTEX_FAST.*
990a0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
990b0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a  UTEX_RECURSIVE.*
990c0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
990d0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
990e0 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  ER.** <li>  SQLI
990f0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
99100 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  MEM.** <li>  SQL
99110 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
99120 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _MEM2.** <li>  S
99130 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
99140 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20  IC_PRNG.** <li> 
99150 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
99160 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e  ATIC_LRU.** <li>
99170 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
99180 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f  TATIC_LRU2.** </
99190 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ul>.**.** The fi
991a0 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74  rst two constant
991b0 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f  s cause sqlite3_
991c0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f  mutex_alloc() to
991d0 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77   create.** a new
991e0 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77   mutex.  The new
991f0 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73   mutex is recurs
99200 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f  ive when SQLITE_
99210 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a  MUTEX_RECURSIVE.
99220 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e  ** is used but n
99230 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73  ot necessarily s
99240 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  o when SQLITE_MU
99250 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64  TEX_FAST is used
99260 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69  ..** The mutex i
99270 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f  mplementation do
99280 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
99290 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f  ake a distinctio
992a0 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c  n.** between SQL
992b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
992c0 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d  IVE and SQLITE_M
992d0 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20  UTEX_FAST if it 
992e0 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74  does.** not want
992f0 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65   to.  But SQLite
99300 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65   will only reque
99310 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d  st a recursive m
99320 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73  utex in.** cases
99330 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79   where it really
99340 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20   needs one.  If 
99350 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63  a faster non-rec
99360 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20  ursive mutex.** 
99370 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
99380 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74  s available on t
99390 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d  he host platform
993a0 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73  , the mutex subs
993b0 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72  ystem.** might r
993c0 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74  eturn such a mut
993d0 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  ex in response t
993e0 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  o SQLITE_MUTEX_F
993f0 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  AST..**.** The o
99400 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72  ther allowed par
99410 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74  ameters to sqlit
99420 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
99430 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20   each return.** 
99440 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
99450 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e  tatic preexistin
99460 67 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74  g mutex.  Six st
99470 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65  atic mutexes are
99480 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20  .** used by the 
99490 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20  current version 
994a0 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75  of SQLite.  Futu
994b0 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  re versions of S
994c0 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64  QLite.** may add
994d0 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74   additional stat
994e0 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61  ic mutexes.  Sta
994f0 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20  tic mutexes are 
99500 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  for internal.** 
99510 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e  use by SQLite on
99520 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  ly.  Application
99530 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74  s that use SQLit
99540 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64  e mutexes should
99550 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65  .** use only the
99560 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73   dynamic mutexes
99570 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c   returned by SQL
99580 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f  ITE_MUTEX_FAST o
99590 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  r.** SQLITE_MUTE
995a0 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a  X_RECURSIVE..**.
995b0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  ** Note that if 
995c0 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d  one of the dynam
995d0 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74  ic mutex paramet
995e0 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45  ers (SQLITE_MUTE
995f0 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c  X_FAST.** or SQL
99600 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
99610 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65  IVE) is used the
99620 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  n sqlite3_mutex_
99630 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72  alloc().** retur
99640 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d  ns a different m
99650 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61  utex on every ca
99660 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65  ll.  But for the
99670 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65   static .** mute
99680 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d  x types, the sam
99690 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72  e mutex is retur
996a0 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  ned on every cal
996b0 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68  l that has.** th
996c0 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62  e same type numb
996d0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  er..*/.static sq
996e0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e  lite3_mutex *win
996f0 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69  MutexAlloc(int i
99700 54 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  Type){.  sqlite3
99710 5f 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77  _mutex *p;..  sw
99720 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20  itch( iType ){. 
99730 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
99740 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63  UTEX_FAST:.    c
99750 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
99760 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
99770 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
99780 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
99790 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
997a0 66 28 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20  f( p ){  .      
997b0 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
997c0 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c  .        Initial
997d0 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69  izeCriticalSecti
997e0 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  on(&p->mutex);. 
997f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
99800 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
99810 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61  fault: {.      a
99820 73 73 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f  ssert( winMutex_
99830 69 73 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20  isInit==1 );.   
99840 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65     assert( iType
99850 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20  -2 >= 0 );.     
99860 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32   assert( iType-2
99870 20 3c 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e   < ArraySize(win
99880 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65  Mutex_staticMute
99890 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20  xes) );.      p 
998a0 3d 20 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74  = &winMutex_stat
998b0 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d  icMutexes[iType-
998c0 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20  2];.      p->id 
998d0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62  = iType;.      b
998e0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
998f0 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a    return p;.}...
99900 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
99910 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61  ne deallocates a
99920 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61   previously.** a
99930 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20  llocated mutex. 
99940 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66   SQLite is caref
99950 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65  ul to deallocate
99960 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20   every.** mutex 
99970 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65  that it allocate
99980 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
99990 64 20 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73  d winMutexFree(s
999a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
999b0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b  {.  assert( p );
999c0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
999d0 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ef==0 );.  asser
999e0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
999f0 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70  _MUTEX_FAST || p
99a00 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
99a10 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a  EX_RECURSIVE );.
99a20 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c    DeleteCritical
99a30 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
99a40 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  x);.  sqlite3_fr
99a50 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ee(p);.}../*.** 
99a60 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
99a70 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
99a80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
99a90 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
99aa0 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61  pt.** to enter a
99ab0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74   mutex.  If anot
99ac0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
99ad0 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
99ae0 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
99af0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
99b00 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
99b10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
99b20 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
99b30 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
99b40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
99b50 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61  ex_try() interfa
99b60 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ce returns SQLIT
99b70 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  E_OK.** upon suc
99b80 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
99b90 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
99ba0 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54  using SQLITE_MUT
99bb0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
99bc0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
99bd0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
99be0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
99bf0 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
99c00 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d   the,.** mutex m
99c10 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e  ust be exited an
99c20 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66   equal number of
99c30 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e   times before an
99c40 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
99c50 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74  can enter.  If t
99c60 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
99c70 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
99c80 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20  y other kind of 
99c90 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68  mutex.** more th
99ca0 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
99cb0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
99cc0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
99cd0 69 64 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72  id winMutexEnter
99ce0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
99cf0 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
99d00 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
99d10 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77  X_RECURSIVE || w
99d20 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70  inMutexNotheld(p
99d30 29 20 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74  ) );.  EnterCrit
99d40 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e  icalSection(&p->
99d50 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e  mutex);.  p->own
99d60 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54  er = GetCurrentT
99d70 68 72 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d  hreadId(); .  p-
99d80 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69  >nRef++;.}.stati
99d90 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72  c int winMutexTr
99da0 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  y(sqlite3_mutex 
99db0 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  *p){.  int rc = 
99dc0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61  SQLITE_BUSY;.  a
99dd0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
99de0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
99df0 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78  SIVE || winMutex
99e00 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
99e10 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69  /*.  ** The sqli
99e20 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
99e30 72 6f 75 74 69 6e 65 20 69 73 20 76 65 72 79 20  routine is very 
99e40 72 61 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64  rarely used, and
99e50 20 77 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73   when it.  ** is
99e60 20 75 73 65 64 20 69 74 20 69 73 20 6d 65 72 65   used it is mere
99e70 6c 79 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  ly an optimizati
99e80 6f 6e 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b  on.  So it is OK
99e90 20 66 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79   for it to alway
99ea0 73 0a 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20  s.  ** fail.  . 
99eb0 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79   **.  ** The Try
99ec0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
99ed0 74 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65  tion() interface
99ee0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
99ef0 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a  le on WinNT..  *
99f00 2a 20 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f  * And some windo
99f10 77 73 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d  ws compilers com
99f20 70 6c 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79  plain if you try
99f30 20 74 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f   to use it witho
99f40 75 74 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f  ut.  ** first do
99f50 69 6e 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65  ing some #define
99f60 73 20 74 68 61 74 20 70 72 65 76 65 6e 74 20 53  s that prevent S
99f70 51 4c 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64  QLite from build
99f80 69 6e 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20  ing on Win98..  
99f90 2a 2a 20 46 6f 72 20 74 68 61 74 20 72 65 61 73  ** For that reas
99fa0 6f 6e 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74  on, we will omit
99fb0 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69   this optimizati
99fc0 6f 6e 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65  on for now.  See
99fd0 0a 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36  .  ** ticket #26
99fe0 38 35 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20  85..  */.#if 0. 
99ff0 20 69 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29   if( mutexIsNT()
9a000 20 26 26 20 54 72 79 45 6e 74 65 72 43 72 69 74   && TryEnterCrit
9a010 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e  icalSection(&p->
9a020 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d  mutex) ){.    p-
9a030 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72  >owner = GetCurr
9a040 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20  entThreadId();. 
9a050 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20     p->nRef++;.  
9a060 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
9a070 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e  ;.  }.#else.  UN
9a080 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
9a090 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  );.#endif.  retu
9a0a0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
9a0b0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
9a0c0 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e  x_leave() routin
9a0d0 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20  e exits a mutex 
9a0e0 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76  that was.** prev
9a0f0 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62  iously entered b
9a100 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  y the same threa
9a110 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72  d.  The behavior
9a120 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64  .** is undefined
9a130 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73   if the mutex is
9a140 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65   not currently e
9a150 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20  ntered or.** is 
9a160 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c  not currently al
9a170 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65  located.  SQLite
9a180 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65   will never do e
9a190 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  ither..*/.static
9a1a0 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65   void winMutexLe
9a1b0 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
9a1c0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
9a1d0 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20   p->nRef>0 );.  
9a1e0 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72  assert( p->owner
9a1f0 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  ==GetCurrentThre
9a200 61 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e  adId() );.  p->n
9a210 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28  Ref--;.  assert(
9a220 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70   p->nRef==0 || p
9a230 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
9a240 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a  EX_RECURSIVE );.
9a250 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53    LeaveCriticalS
9a260 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78  ection(&p->mutex
9a270 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
9a280 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74  VATE sqlite3_mut
9a290 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  ex_methods *sqli
9a2a0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
9a2b0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
9a2c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
9a2d0 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b  thods sMutex = {
9a2e0 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69  .    winMutexIni
9a2f0 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45  t,.    winMutexE
9a300 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  nd,.    winMutex
9a310 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75  Alloc,.    winMu
9a320 74 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e  texFree,.    win
9a330 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20  MutexEnter,.    
9a340 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20  winMutexTry,.   
9a350 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a   winMutexLeave,.
9a360 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
9a370 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  BUG.    winMutex
9a380 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74  Held,.    winMut
9a390 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a  exNotheld.#else.
9a3a0 20 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e      0,.    0.#en
9a3b0 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75  dif.  };..  retu
9a3c0 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65  rn &sMutex;.}.#e
9a3d0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
9a3e0 55 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a  UTEX_W32 */../**
9a3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
9a400 20 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20   of mutex_w32.c 
9a410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
9a440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
9a450 69 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63  in file malloc.c
9a460 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
9a470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
9a490 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
9a4a0 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
9a4b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
9a4c0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
9a4d0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
9a4e0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
9a4f0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
9a500 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
9a510 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
9a520 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
9a530 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
9a540 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
9a550 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
9a560 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
9a570 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
9a580 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
9a590 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
9a5a0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
9a5b0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
9a5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a600 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79  ***.**.** Memory
9a610 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63   allocation func
9a620 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75  tions used throu
9a630 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2f  ghout sqlite..*/
9a640 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
9a650 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e 20 74  tine runs when t
9a660 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
9a670 74 6f 72 20 73 65 65 73 20 74 68 61 74 20 74 68  tor sees that th
9a680 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72  e.** total memor
9a690 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  y allocation is 
9a6a0 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65 64 20  about to exceed 
9a6b0 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a  the soft heap.**
9a6c0 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69   limit..*/.stati
9a6d0 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c  c void softHeapL
9a6e0 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a 20 20  imitEnforcer(.  
9a6f0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a  void *NotUsed, .
9a700 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
9a710 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 74 20  NotUsed2,.  int 
9a720 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 55  allocSize.){.  U
9a730 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
9a740 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
9a750 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  d2);.  sqlite3_r
9a760 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c  elease_memory(al
9a770 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a  locSize);.}../*.
9a780 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74 20  ** Set the soft 
9a790 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20  heap-size limit 
9a7a0 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e  for the library.
9a7b0 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20   Passing a zero 
9a7c0 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20  or .** negative 
9a7d0 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20  value indicates 
9a7e0 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c  no limit..*/.SQL
9a7f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
9a800 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
9a810 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73  imit(int n){.  s
9a820 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c  qlite3_uint64 iL
9a830 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72  imit;.  int over
9a840 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29  age;.  if( n<0 )
9a850 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30  {.    iLimit = 0
9a860 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
9a870 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 23  Limit = n;.  }.#
9a880 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
9a890 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 73 71  IT_AUTOINIT.  sq
9a8a0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
9a8b0 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  ();.#endif.  if(
9a8c0 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20   iLimit>0 ){.   
9a8d0 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c   sqlite3MemoryAl
9a8e0 61 72 6d 28 73 6f 66 74 48 65 61 70 4c 69 6d 69  arm(softHeapLimi
9a8f0 74 45 6e 66 6f 72 63 65 72 2c 20 30 2c 20 69 4c  tEnforcer, 0, iL
9a900 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  imit);.  }else{.
9a910 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72      sqlite3Memor
9a920 79 41 6c 61 72 6d 28 30 2c 20 30 2c 20 30 29 3b  yAlarm(0, 0, 0);
9a930 0a 20 20 7d 0a 20 20 6f 76 65 72 61 67 65 20 3d  .  }.  overage =
9a940 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 6d   (int)(sqlite3_m
9a950 65 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 28  emory_used() - (
9a960 69 36 34 29 6e 29 3b 0a 20 20 69 66 28 20 6f 76  i64)n);.  if( ov
9a970 65 72 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73  erage>0 ){.    s
9a980 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
9a990 65 6d 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a  emory(overage);.
9a9a0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74    }.}../*.** Att
9a9b0 65 6d 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20  empt to release 
9a9c0 75 70 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66  up to n bytes of
9a9d0 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d   non-essential m
9a9e0 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a  emory currently.
9a9f0 2a 2a 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74  ** held by SQLit
9aa00 65 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66  e. An example of
9aa10 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d   non-essential m
9aa20 65 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20  emory is memory 
9aa30 75 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65  used to.** cache
9aa40 20 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20   database pages 
9aa50 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72  that are not cur
9aa60 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a  rently in use..*
9aa70 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
9aa80 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
9aa90 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a  _memory(int n){.
9aaa0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
9aab0 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
9aac0 47 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65  GEMENT.  int nRe
9aad0 74 20 3d 20 30 3b 0a 20 20 6e 52 65 74 20 2b 3d  t = 0;.  nRet +=
9aae0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
9aaf0 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52  leaseMemory(n-nR
9ab00 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52  et);.  return nR
9ab10 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53  et;.#else.  UNUS
9ab20 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b  ED_PARAMETER(n);
9ab30 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
9ab40 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  _OK;.#endif.}../
9ab50 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66 6f 72  *.** State infor
9ab60 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20  mation local to 
9ab70 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
9ab80 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
9ab90 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
9aba0 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d  E_WSD struct Mem
9abb0 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e  0Global {.  /* N
9abc0 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61  umber of free pa
9abd0 67 65 73 20 66 6f 72 20 73 63 72 61 74 63 68 20  ges for scratch 
9abe0 61 6e 64 20 70 61 67 65 2d 63 61 63 68 65 20 6d  and page-cache m
9abf0 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 32 20 6e  emory */.  u32 n
9ac00 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75  ScratchFree;.  u
9ac10 33 32 20 6e 50 61 67 65 46 72 65 65 3b 0a 0a 20  32 nPageFree;.. 
9ac20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
9ac30 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f  mutex;         /
9ac40 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61  * Mutex to seria
9ac50 6c 69 7a 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a  lize access */..
9ac60 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c    /*.  ** The al
9ac70 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64  arm callback and
9ac80 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20   its arguments. 
9ac90 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20   The mem0.mutex 
9aca0 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62  lock will.  ** b
9acb0 65 20 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65  e held while the
9acc0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e   callback is run
9acd0 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65  ning.  Recursive
9ace0 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a   calls into.  **
9acf0 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73   the memory subs
9ad00 79 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65  ystem are allowe
9ad10 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61  d, but no new ca
9ad20 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a  llbacks will be.
9ad30 20 20 2a 2a 20 69 73 73 75 65 64 2e 0a 20 20 2a    ** issued..  *
9ad40 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
9ad50 34 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64  4 alarmThreshold
9ad60 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d  ;.  void (*alarm
9ad70 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  Callback)(void*,
9ad80 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69   sqlite3_int64,i
9ad90 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61  nt);.  void *ala
9ada0 72 6d 41 72 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  rmArg;..  /*.  *
9adb0 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  * Pointers to th
9adc0 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33  e end of sqlite3
9add0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9ade0 72 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73  ratch and.  ** s
9adf0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ae00 69 67 2e 70 50 61 67 65 20 74 6f 20 61 20 62 6c  ig.pPage to a bl
9ae10 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68  ock of memory th
9ae20 61 74 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20  at records.  ** 
9ae30 77 68 69 63 68 20 70 61 67 65 73 20 61 72 65 20  which pages are 
9ae40 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a  available..  */.
9ae50 20 20 75 33 32 20 2a 61 53 63 72 61 74 63 68 46    u32 *aScratchF
9ae60 72 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 67  ree;.  u32 *aPag
9ae70 65 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20  eFree;.} mem0 = 
9ae80 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  { 0, 0, 0, 0, 0,
9ae90 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 65   0, 0, 0 };..#de
9aea0 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c  fine mem0 GLOBAL
9aeb0 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62  (struct Mem0Glob
9aec0 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a  al, mem0)../*.**
9aed0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
9aee0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
9aef0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  n subsystem..*/.
9af00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
9af10 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  nt sqlite3Malloc
9af20 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 69 66  Init(void){.  if
9af30 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9af40 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d  onfig.m.xMalloc=
9af50 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
9af60 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 29  3MemSetDefault()
9af70 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26  ;.  }.  memset(&
9af80 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28  mem0, 0, sizeof(
9af90 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 73 71  mem0));.  if( sq
9afa0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9afb0 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
9afc0 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d      mem0.mutex =
9afd0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
9afe0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
9aff0 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d  STATIC_MEM);.  }
9b000 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
9b010 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61  obalConfig.pScra
9b020 74 63 68 20 26 26 20 73 71 6c 69 74 65 33 47 6c  tch && sqlite3Gl
9b030 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9b040 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 20 20  atch>=100.      
9b050 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  && sqlite3Global
9b060 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3e  Config.nScratch>
9b070 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  =0 ){.    int i;
9b080 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
9b090 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
9b0a0 63 68 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28  ch = ROUNDDOWN8(
9b0b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b0c0 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2d 34 29  fig.szScratch-4)
9b0d0 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61  ;.    mem0.aScra
9b0e0 74 63 68 46 72 65 65 20 3d 20 28 75 33 32 2a 29  tchFree = (u32*)
9b0f0 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  &((char*)sqlite3
9b100 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9b110 72 61 74 63 68 29 0a 20 20 20 20 20 20 20 20 20  ratch).         
9b120 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
9b130 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
9b140 53 63 72 61 74 63 68 2a 73 71 6c 69 74 65 33 47  Scratch*sqlite3G
9b150 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72  lobalConfig.nScr
9b160 61 74 63 68 5d 3b 0a 20 20 20 20 66 6f 72 28 69  atch];.    for(i
9b170 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f  =0; i<sqlite3Glo
9b180 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74  balConfig.nScrat
9b190 63 68 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61  ch; i++){ mem0.a
9b1a0 53 63 72 61 74 63 68 46 72 65 65 5b 69 5d 20 3d  ScratchFree[i] =
9b1b0 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e   i; }.    mem0.n
9b1c0 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 73 71  ScratchFree = sq
9b1d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b1e0 67 2e 6e 53 63 72 61 74 63 68 3b 0a 20 20 7d 65  g.nScratch;.  }e
9b1f0 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
9b200 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9b210 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 73  ratch = 0;.    s
9b220 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9b230 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 30  ig.szScratch = 0
9b240 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
9b250 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b260 70 50 61 67 65 20 26 26 20 73 71 6c 69 74 65 33  pPage && sqlite3
9b270 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
9b280 61 67 65 3e 3d 35 31 32 0a 20 20 20 20 20 20 26  age>=512.      &
9b290 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  & sqlite3GlobalC
9b2a0 6f 6e 66 69 67 2e 6e 50 61 67 65 3e 3d 31 20 29  onfig.nPage>=1 )
9b2b0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
9b2c0 20 69 6e 74 20 6f 76 65 72 68 65 61 64 3b 0a 20   int overhead;. 
9b2d0 20 20 20 69 6e 74 20 73 7a 20 3d 20 52 4f 55 4e     int sz = ROUN
9b2e0 44 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c  DDOWN8(sqlite3Gl
9b2f0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
9b300 65 29 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  e);.    int n = 
9b310 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b320 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f  fig.nPage;.    o
9b330 76 65 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b  verhead = (4*n +
9b340 20 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20   sz - 1)/sz;.   
9b350 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9b360 6e 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76  nfig.nPage -= ov
9b370 65 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30  erhead;.    mem0
9b380 2e 61 50 61 67 65 46 72 65 65 20 3d 20 28 75 33  .aPageFree = (u3
9b390 32 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69  2*)&((char*)sqli
9b3a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b3b0 70 50 61 67 65 29 0a 20 20 20 20 20 20 20 20 20  pPage).         
9b3c0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
9b3d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
9b3e0 50 61 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62  Page*sqlite3Glob
9b3f0 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b  alConfig.nPage];
9b400 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
9b410 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b420 66 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b  fig.nPage; i++){
9b430 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b   mem0.aPageFree[
9b440 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65  i] = i; }.    me
9b450 6d 30 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 73  m0.nPageFree = s
9b460 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9b470 69 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73  ig.nPage;.  }els
9b480 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  e{.    sqlite3Gl
9b490 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65  obalConfig.pPage
9b4a0 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
9b4b0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
9b4c0 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Page = 0;.  }.  
9b4d0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
9b4e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e  obalConfig.m.xIn
9b4f0 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  it(sqlite3Global
9b500 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74  Config.m.pAppDat
9b510 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69  a);.}../*.** Dei
9b520 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
9b530 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
9b540 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51  subsystem..*/.SQ
9b550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
9b560 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45  d sqlite3MallocE
9b570 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  nd(void){.  if( 
9b580 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b590 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20  fig.m.xShutdown 
9b5a0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
9b5b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68  obalConfig.m.xSh
9b5c0 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c  utdown(sqlite3Gl
9b5d0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70  obalConfig.m.pAp
9b5e0 70 44 61 74 61 29 3b 0a 20 20 7d 0a 20 20 6d 65  pData);.  }.  me
9b5f0 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73  mset(&mem0, 0, s
9b600 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a  izeof(mem0));.}.
9b610 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
9b620 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  e amount of memo
9b630 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65  ry currently che
9b640 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c  cked out..*/.SQL
9b650 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
9b660 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65  int64 sqlite3_me
9b670 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b  mory_used(void){
9b680 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20  .  int n, mx;.  
9b690 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65  sqlite3_int64 re
9b6a0 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61  s;.  sqlite3_sta
9b6b0 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55  tus(SQLITE_STATU
9b6c0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26  S_MEMORY_USED, &
9b6d0 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65  n, &mx, 0);.  re
9b6e0 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  s = (sqlite3_int
9b6f0 36 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61  64)n;  /* Work a
9b700 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72  round bug in Bor
9b710 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23  land C. Ticket #
9b720 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3216 */.  return
9b730 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   res;.}../*.** R
9b740 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75  eturn the maximu
9b750 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  m amount of memo
9b760 72 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72  ry that has ever
9b770 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64   been.** checked
9b780 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65   out since eithe
9b790 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  r the beginning 
9b7a0 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a  of this process.
9b7b0 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20  ** or since the 
9b7c0 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 65  most recent rese
9b7d0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
9b7e0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
9b7f0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
9b800 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65  ghwater(int rese
9b810 74 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c  tFlag){.  int n,
9b820 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69   mx;.  sqlite3_i
9b830 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69  nt64 res;.  sqli
9b840 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54  te3_status(SQLIT
9b850 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9b860 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72  USED, &n, &mx, r
9b870 65 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73  esetFlag);.  res
9b880 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
9b890 34 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61  4)mx;  /* Work a
9b8a0 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72  round bug in Bor
9b8b0 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23  land C. Ticket #
9b8c0 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3216 */.  return
9b8d0 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   res;.}../*.** C
9b8e0 68 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20  hange the alarm 
9b8f0 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49  callback.*/.SQLI
9b900 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
9b910 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72  qlite3MemoryAlar
9b920 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c  m(.  void(*xCall
9b930 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67  back)(void *pArg
9b940 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
9b950 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76  used,int N),.  v
9b960 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c  oid *pArg,.  sql
9b970 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65  ite3_int64 iThre
9b980 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74  shold.){.  sqlit
9b990 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
9b9a0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65  em0.mutex);.  me
9b9b0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
9b9c0 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20   = xCallback;.  
9b9d0 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20  mem0.alarmArg = 
9b9e0 70 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61  pArg;.  mem0.ala
9b9f0 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54  rmThreshold = iT
9ba00 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69  hreshold;.  sqli
9ba10 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9ba20 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72  mem0.mutex);.  r
9ba30 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
9ba40 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
9ba50 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
9ba60 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61  ED./*.** Depreca
9ba70 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74  ted external int
9ba80 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61  erface.  Interna
9ba90 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f  l/core SQLite co
9baa0 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c  de.** should cal
9bab0 6c 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41  l sqlite3MemoryA
9bac0 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  larm..*/.SQLITE_
9bad0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
9bae0 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20  memory_alarm(.  
9baf0 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29  void(*xCallback)
9bb00 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c  (void *pArg, sql
9bb10 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c  ite3_int64 used,
9bb20 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a  int N),.  void *
9bb30 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f  pArg,.  sqlite3_
9bb40 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64  int64 iThreshold
9bb50 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  .){.  return sql
9bb60 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28  ite3MemoryAlarm(
9bb70 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c  xCallback, pArg,
9bb80 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a   iThreshold);.}.
9bb90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72  #endif../*.** Tr
9bba0 69 67 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20  igger the alarm 
9bbb0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
9bbc0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61  sqlite3MallocAla
9bbd0 72 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  rm(int nByte){. 
9bbe0 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63   void (*xCallbac
9bbf0 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  k)(void*,sqlite3
9bc00 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73  _int64,int);.  s
9bc10 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77  qlite3_int64 now
9bc20 55 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41  Used;.  void *pA
9bc30 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61  rg;.  if( mem0.a
9bc40 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20  larmCallback==0 
9bc50 29 20 72 65 74 75 72 6e 3b 0a 20 20 78 43 61 6c  ) return;.  xCal
9bc60 6c 62 61 63 6b 20 3d 20 6d 65 6d 30 2e 61 6c 61  lback = mem0.ala
9bc70 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f  rmCallback;.  no
9bc80 77 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53  wUsed = sqlite3S
9bc90 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54  tatusValue(SQLIT
9bca0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9bcb0 55 53 45 44 29 3b 0a 20 20 70 41 72 67 20 3d 20  USED);.  pArg = 
9bcc0 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 3b 0a 20  mem0.alarmArg;. 
9bcd0 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62   mem0.alarmCallb
9bce0 61 63 6b 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  ack = 0;.  sqlit
9bcf0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
9bd00 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43  em0.mutex);.  xC
9bd10 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f  allback(pArg, no
9bd20 77 55 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20  wUsed, nByte);. 
9bd30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9bd40 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9bd50 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61  ;.  mem0.alarmCa
9bd60 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61  llback = xCallba
9bd70 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d  ck;.  mem0.alarm
9bd80 41 72 67 20 3d 20 70 41 72 67 3b 0a 7d 0a 0a 2f  Arg = pArg;.}../
9bd90 2a 0a 2a 2a 20 44 6f 20 61 20 6d 65 6d 6f 72 79  *.** Do a memory
9bda0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 69 74 68   allocation with
9bdb0 20 73 74 61 74 69 73 74 69 63 73 20 61 6e 64 20   statistics and 
9bdc0 61 6c 61 72 6d 73 2e 20 20 41 73 73 75 6d 65 20  alarms.  Assume 
9bdd0 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61  the.** lock is a
9bde0 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a  lready held..*/.
9bdf0 73 74 61 74 69 63 20 69 6e 74 20 6d 61 6c 6c 6f  static int mallo
9be00 63 57 69 74 68 41 6c 61 72 6d 28 69 6e 74 20 6e  cWithAlarm(int n
9be10 2c 20 76 6f 69 64 20 2a 2a 70 70 29 7b 0a 20 20  , void **pp){.  
9be20 69 6e 74 20 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69  int nFull;.  voi
9be30 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  d *p;.  assert( 
9be40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
9be50 6c 64 28 6d 65 6d 30 2e 6d 75 74 65 78 29 20 29  ld(mem0.mutex) )
9be60 3b 0a 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69  ;.  nFull = sqli
9be70 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9be80 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 29 3b 0a 20  m.xRoundup(n);. 
9be90 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
9bea0 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t(SQLITE_STATUS_
9beb0 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b  MALLOC_SIZE, n);
9bec0 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72  .  if( mem0.alar
9bed0 6d 43 61 6c 6c 62 61 63 6b 21 3d 30 20 29 7b 0a  mCallback!=0 ){.
9bee0 20 20 20 20 69 6e 74 20 6e 55 73 65 64 20 3d 20      int nUsed = 
9bef0 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c  sqlite3StatusVal
9bf00 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ue(SQLITE_STATUS
9bf10 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20  _MEMORY_USED);. 
9bf20 20 20 20 69 66 28 20 6e 55 73 65 64 2b 6e 46 75     if( nUsed+nFu
9bf30 6c 6c 20 3e 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d  ll >= mem0.alarm
9bf40 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20  Threshold ){.   
9bf50 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63     sqlite3Malloc
9bf60 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20  Alarm(nFull);.  
9bf70 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71    }.  }.  p = sq
9bf80 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9bf90 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c  g.m.xMalloc(nFul
9bfa0 6c 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 26  l);.  if( p==0 &
9bfb0 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c  & mem0.alarmCall
9bfc0 62 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69  back ){.    sqli
9bfd0 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e  te3MallocAlarm(n
9bfe0 46 75 6c 6c 29 3b 0a 20 20 20 20 70 20 3d 20 73  Full);.    p = s
9bff0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9c000 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75  ig.m.xMalloc(nFu
9c010 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ll);.  }.  if( p
9c020 20 29 7b 0a 20 20 20 20 6e 46 75 6c 6c 20 3d 20   ){.    nFull = 
9c030 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
9c040 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  e(p);.    sqlite
9c050 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
9c060 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9c070 55 53 45 44 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20  USED, nFull);.  
9c080 7d 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 72  }.  *pp = p;.  r
9c090 65 74 75 72 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a  eturn nFull;.}..
9c0a0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d  /*.** Allocate m
9c0b0 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 72 6f 75  emory.  This rou
9c0c0 74 69 6e 65 20 69 73 20 6c 69 6b 65 20 73 71 6c  tine is like sql
9c0d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 65 78  ite3_malloc() ex
9c0e0 63 65 70 74 20 74 68 61 74 20 69 74 0a 2a 2a 20  cept that it.** 
9c0f0 61 73 73 75 6d 65 73 20 74 68 65 20 6d 65 6d 6f  assumes the memo
9c100 72 79 20 73 75 62 73 79 73 74 65 6d 20 68 61 73  ry subsystem has
9c110 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e   already been in
9c120 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51  itialized..*/.SQ
9c130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
9c140 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  d *sqlite3Malloc
9c150 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20  (int n){.  void 
9c160 2a 70 3b 0a 20 20 69 66 28 20 6e 3c 3d 30 20 7c  *p;.  if( n<=0 |
9c170 7c 20 6e 3e 3d 30 78 37 66 66 66 66 66 30 30 20  | n>=0x7fffff00 
9c180 29 7b 0a 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f  ){.    /* A memo
9c190 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66  ry allocation of
9c1a0 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74   a number of byt
9c1b0 65 73 20 77 68 69 63 68 20 69 73 20 6e 65 61 72  es which is near
9c1c0 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20   the maximum.   
9c1d0 20 2a 2a 20 73 69 67 6e 65 64 20 69 6e 74 65 67   ** signed integ
9c1e0 65 72 20 76 61 6c 75 65 20 6d 69 67 68 74 20 63  er value might c
9c1f0 61 75 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20  ause an integer 
9c200 6f 76 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20  overflow inside 
9c210 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 78 4d  of the.    ** xM
9c220 61 6c 6c 6f 63 28 29 2e 20 20 48 65 6e 63 65 20  alloc().  Hence 
9c230 77 65 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78  we limit the max
9c240 69 6d 75 6d 20 73 69 7a 65 20 74 6f 20 30 78 37  imum size to 0x7
9c250 66 66 66 66 66 30 30 2c 20 67 69 76 69 6e 67 0a  fffff00, giving.
9c260 20 20 20 20 2a 2a 20 32 35 35 20 62 79 74 65 73      ** 255 bytes
9c270 20 6f 66 20 6f 76 65 72 68 65 61 64 2e 20 20 53   of overhead.  S
9c280 51 4c 69 74 65 20 69 74 73 65 6c 66 20 77 69 6c  QLite itself wil
9c290 6c 20 6e 65 76 65 72 20 75 73 65 20 61 6e 79 74  l never use anyt
9c2a0 68 69 6e 67 20 6e 65 61 72 0a 20 20 20 20 2a 2a  hing near.    **
9c2b0 20 74 68 69 73 20 61 6d 6f 75 6e 74 2e 20 20 54   this amount.  T
9c2c0 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 72  he only way to r
9c2d0 65 61 63 68 20 74 68 65 20 6c 69 6d 69 74 20 69  each the limit i
9c2e0 73 20 77 69 74 68 20 73 71 6c 69 74 65 33 5f 6d  s with sqlite3_m
9c2f0 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 70  alloc() */.    p
9c300 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
9c310 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9c320 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
9c330 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
9c340 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
9c350 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f  utex);.    mallo
9c360 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70  cWithAlarm(n, &p
9c370 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
9c380 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9c390 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b  mutex);.  }else{
9c3a0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
9c3b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9c3c0 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20  Malloc(n);.  }. 
9c3d0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
9c3e0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
9c3f0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
9c400 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72  llocation is for
9c410 20 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c   use by the appl
9c420 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73  ication..** Firs
9c430 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  t make sure the 
9c440 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d  memory subsystem
9c450 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c   is initialized,
9c460 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20   then do the.** 
9c470 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  allocation..*/.S
9c480 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
9c490 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69  sqlite3_malloc(i
9c4a0 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53  nt n){.#ifndef S
9c4b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
9c4c0 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  NIT.  if( sqlite
9c4d0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29  3_initialize() )
9c4e0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
9c4f0 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  f.  return sqlit
9c500 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a  e3Malloc(n);.}..
9c510 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61  /*.** Each threa
9c520 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20  d may only have 
9c530 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e  a single outstan
9c540 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  ding allocation 
9c550 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68  from.** xScratch
9c560 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65  Malloc().  We ve
9c570 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72  rify this constr
9c580 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67  aint in the sing
9c590 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63  le-threaded.** c
9c5a0 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 20 73  ase by setting s
9c5b0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74  cratchAllocOut t
9c5c0 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f  o 1 when an allo
9c5d0 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74  cation.** is out
9c5e0 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e  standing clearin
9c5f0 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c  g it when the al
9c600 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
9c610 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  d..*/.#if SQLITE
9c620 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
9c630 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
9c640 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63  G).static int sc
9c650 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20  ratchAllocOut = 
9c660 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  0;.#endif.../*.*
9c670 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
9c680 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  y that is to be 
9c690 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65  used and release
9c6a0 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a  d right away..**
9c6b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
9c6c0 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f   similar to allo
9c6d0 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 74 20  ca() in that it 
9c6e0 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a  is not intended.
9c6f0 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e  ** for situation
9c700 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f  s where the memo
9c710 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64  ry might be held
9c720 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69   long-term.  Thi
9c730 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
9c740 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20  intended to get 
9c750 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61  memory to old la
9c760 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61  rge transient da
9c770 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73  ta.** structures
9c780 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20   that would not 
9c790 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20  normally fit on 
9c7a0 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a  the stack of an.
9c7b0 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63  ** embedded proc
9c7c0 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  essor..*/.SQLITE
9c7d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
9c7e0 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c  qlite3ScratchMal
9c7f0 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  loc(int n){.  vo
9c800 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  id *p;.  assert(
9c810 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c   n>0 );..#if SQL
9c820 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
9c830 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44  0 && !defined(ND
9c840 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66  EBUG).  /* Verif
9c850 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74  y that no more t
9c860 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20  han one scratch 
9c870 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74  allocation per t
9c880 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75  hread.  ** is ou
9c890 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65  tstanding at one
9c8a0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73   time.  (This is
9c8b0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e   only checked in
9c8c0 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65   the.  ** single
9c8d0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73  -threaded case s
9c8e0 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e  ince checking in
9c8f0 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61   the multi-threa
9c900 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f  ded case.  ** wo
9c910 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65  uld be much more
9c920 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a   complicated.) *
9c930 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 72 61  /.  assert( scra
9c940 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29  tchAllocOut==0 )
9c950 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  ;.#endif..  if( 
9c960 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9c970 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20  fig.szScratch<n 
9c980 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61  ){.    goto scra
9c990 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20  tch_overflow;.  
9c9a0 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c  }else{  .    sql
9c9b0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9c9c0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9c9d0 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61    if( mem0.nScra
9c9e0 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20  tchFree==0 ){.  
9c9f0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9ca00 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9ca10 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ex);.      goto 
9ca20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77  scratch_overflow
9ca30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
9ca40 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
9ca50 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63  i = mem0.aScratc
9ca60 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63  hFree[--mem0.nSc
9ca70 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20  ratchFree];.    
9ca80 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c    i *= sqlite3Gl
9ca90 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9caa0 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69  atch;.      sqli
9cab0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9cac0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
9cad0 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20  CH_USED, 1);.   
9cae0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9caf0 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Set(SQLITE_STATU
9cb00 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20  S_SCRATCH_SIZE, 
9cb10 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
9cb20 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9cb30 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
9cb40 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63   p = (void*)&((c
9cb50 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  har*)sqlite3Glob
9cb60 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9cb70 68 29 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73  h)[i];.      ass
9cb80 65 72 74 28 20 20 28 28 28 75 38 2a 29 70 20 2d  ert(  (((u8*)p -
9cb90 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30   (u8*)0) & 7)==0
9cba0 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69   );.    }.  }.#i
9cbb0 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
9cbc0 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e  AFE==0 && !defin
9cbd0 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72  ed(NDEBUG).  scr
9cbe0 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70  atchAllocOut = p
9cbf0 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72  !=0;.#endif..  r
9cc00 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63  eturn p;..scratc
9cc10 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66  h_overflow:.  if
9cc20 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9cc30 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
9cc40 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
9cc50 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
9cc60 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  utex);.    sqlit
9cc70 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49  e3StatusSet(SQLI
9cc80 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
9cc90 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20  H_SIZE, n);.    
9cca0 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c  n = mallocWithAl
9ccb0 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20  arm(n, &p);.    
9ccc0 69 66 28 20 70 20 29 20 73 71 6c 69 74 65 33 53  if( p ) sqlite3S
9ccd0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
9cce0 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f  STATUS_SCRATCH_O
9ccf0 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20  VERFLOW, n);.   
9cd00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
9cd10 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
9cd20 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
9cd30 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
9cd40 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63  Config.m.xMalloc
9cd50 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c  (n);.  }.#if SQL
9cd60 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
9cd70 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44  0 && !defined(ND
9cd80 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 68 41  EBUG).  scratchA
9cd90 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a  llocOut = p!=0;.
9cda0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
9cdb0 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f  p;    .}.SQLITE_
9cdc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
9cdd0 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28  ite3ScratchFree(
9cde0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20  void *p){.  if( 
9cdf0 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45  p ){..#if SQLITE
9ce00 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
9ce10 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
9ce20 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79  G).    /* Verify
9ce30 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68   that no more th
9ce40 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61  an one scratch a
9ce50 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68  llocation per th
9ce60 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f  read.    ** is o
9ce70 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e  utstanding at on
9ce80 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69  e time.  (This i
9ce90 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69  s only checked i
9cea0 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e  n the.    ** sin
9ceb0 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73  gle-threaded cas
9cec0 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67  e since checking
9ced0 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68   in the multi-th
9cee0 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 20 20  readed case.    
9cef0 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68  ** would be much
9cf00 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65   more complicate
9cf10 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  d.) */.    asser
9cf20 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f  t( scratchAllocO
9cf30 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 73 63 72  ut==1 );.    scr
9cf40 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30  atchAllocOut = 0
9cf50 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66  ;.#endif..    if
9cf60 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9cf70 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 3d 3d  onfig.pScratch==
9cf80 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  0.           || 
9cf90 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  p<sqlite3GlobalC
9cfa0 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 0a 20  onfig.pScratch. 
9cfb0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d            || p>=
9cfc0 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72  (void*)mem0.aScr
9cfd0 61 74 63 68 46 72 65 65 20 29 7b 0a 20 20 20 20  atchFree ){.    
9cfe0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
9cff0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
9d000 61 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  at ){.        in
9d010 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  t iSize = sqlite
9d020 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a  3MallocSize(p);.
9d030 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
9d040 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
9d050 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20  .mutex);.       
9d060 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
9d070 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
9d080 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57  SCRATCH_OVERFLOW
9d090 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20  , -iSize);.     
9d0a0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9d0b0 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
9d0c0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d  S_MEMORY_USED, -
9d0d0 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20  iSize);.        
9d0e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d0f0 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
9d100 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
9d110 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9d120 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d  .mutex);.      }
9d130 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
9d140 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9d150 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20  g.m.xFree(p);.  
9d160 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
9d170 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
9d180 20 20 20 20 69 20 3d 20 28 69 6e 74 29 28 28 75      i = (int)((u
9d190 38 2a 29 70 20 2d 20 28 75 38 2a 29 73 71 6c 69  8*)p - (u8*)sqli
9d1a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9d1b0 70 53 63 72 61 74 63 68 29 3b 0a 20 20 20 20 20  pScratch);.     
9d1c0 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f   i /= sqlite3Glo
9d1d0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
9d1e0 74 63 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72  tch;.      asser
9d1f0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c  t( i>=0 && i<sql
9d200 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d210 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20  .nScratch );.   
9d220 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9d230 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
9d240 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  x);.      assert
9d250 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46  ( mem0.nScratchF
9d260 72 65 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33  ree<(u32)sqlite3
9d270 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63  GlobalConfig.nSc
9d280 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d  ratch );.      m
9d290 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65  em0.aScratchFree
9d2a0 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72  [mem0.nScratchFr
9d2b0 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20  ee++] = i;.     
9d2c0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
9d2d0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
9d2e0 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31  SCRATCH_USED, -1
9d2f0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
9d300 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
9d310 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a  0.mutex);.    }.
9d320 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55    }.}../*.** TRU
9d330 45 20 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b  E if p is a look
9d340 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  aside memory all
9d350 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a  ocation from db.
9d360 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
9d370 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45  E_OMIT_LOOKASIDE
9d380 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f  .static int isLo
9d390 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 65 33 20  okaside(sqlite3 
9d3a0 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20  *db, void *p){. 
9d3b0 20 72 65 74 75 72 6e 20 64 62 20 26 26 20 70 20   return db && p 
9d3c0 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73  && p>=db->lookas
9d3d0 69 64 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c  ide.pStart && p<
9d3e0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45  db->lookaside.pE
9d3f0 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66  nd;.}.#else.#def
9d400 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28  ine isLookaside(
9d410 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f  A,B) 0.#endif../
9d420 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
9d430 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79  size of a memory
9d440 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76   allocation prev
9d450 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20  iously obtained 
9d460 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d  from.** sqlite3M
9d470 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74  alloc() or sqlit
9d480 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  e3_malloc()..*/.
9d490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
9d4a0 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  nt sqlite3Malloc
9d4b0 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Size(void *p){. 
9d4c0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47   return sqlite3G
9d4d0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53  lobalConfig.m.xS
9d4e0 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45  ize(p);.}.SQLITE
9d4f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
9d500 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
9d510 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f  (sqlite3 *db, vo
9d520 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  id *p){.  assert
9d530 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ( db==0 || sqlit
9d540 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
9d550 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
9d560 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62  ( isLookaside(db
9d570 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , p) ){.    retu
9d580 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  rn db->lookaside
9d590 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  .sz;.  }else{.  
9d5a0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
9d5b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9d5c0 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  Size(p);.  }.}..
9d5d0 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72  /*.** Free memor
9d5e0 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74  y previously obt
9d5f0 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
9d600 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53  e3Malloc()..*/.S
9d610 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
9d620 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64  qlite3_free(void
9d630 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30   *p){.  if( p==0
9d640 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
9d650 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9d660 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b  nfig.bMemstat ){
9d670 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
9d680 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
9d690 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
9d6a0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
9d6b0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9d6c0 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33 4d 61  USED, -sqlite3Ma
9d6d0 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a 20 20  llocSize(p));.  
9d6e0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
9d6f0 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29  onfig.m.xFree(p)
9d700 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
9d710 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
9d720 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  utex);.  }else{.
9d730 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9d740 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28  lConfig.m.xFree(
9d750 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
9d760 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 61   Free memory tha
9d770 74 20 6d 69 67 68 74 20 62 65 20 61 73 73 6f 63  t might be assoc
9d780 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72  iated with a par
9d790 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
9d7a0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  .** connection..
9d7b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
9d7c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62  E void sqlite3Db
9d7d0 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Free(sqlite3 *db
9d7e0 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73  , void *p){.  as
9d7f0 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73  sert( db==0 || s
9d800 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
9d810 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
9d820 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64    if( isLookasid
9d830 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20  e(db, p) ){.    
9d840 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70  LookasideSlot *p
9d850 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65  Buf = (Lookaside
9d860 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 42 75  Slot*)p;.    pBu
9d870 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c  f->pNext = db->l
9d880 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a  ookaside.pFree;.
9d890 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
9d8a0 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 3b 0a  e.pFree = pBuf;.
9d8b0 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
9d8c0 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73  e.nOut--;.  }els
9d8d0 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  e{.    sqlite3_f
9d8e0 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ree(p);.  }.}../
9d8f0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
9d900 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74  size of an exist
9d910 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
9d920 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ation.*/.SQLITE_
9d930 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
9d940 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69  lite3Realloc(voi
9d950 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79  d *pOld, int nBy
9d960 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64  tes){.  int nOld
9d970 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a  , nNew;.  void *
9d980 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64  pNew;.  if( pOld
9d990 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
9d9a0 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  n sqlite3Malloc(
9d9b0 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69  nBytes);.  }.  i
9d9c0 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a  f( nBytes<=0 ){.
9d9d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
9d9e0 28 70 4f 6c 64 29 3b 0a 20 20 20 20 72 65 74 75  (pOld);.    retu
9d9f0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
9da00 6e 42 79 74 65 73 3e 3d 30 78 37 66 66 66 66 66  nBytes>=0x7fffff
9da10 30 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  00 ){.    /* The
9da20 20 30 78 37 66 66 66 66 30 30 20 6c 69 6d 69 74   0x7ffff00 limit
9da30 20 74 65 72 6d 20 69 73 20 65 78 70 6c 61 69 6e   term is explain
9da40 65 64 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f  ed in comments o
9da50 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  n sqlite3Malloc(
9da60 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  ) */.    return 
9da70 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20  0;.  }.  nOld = 
9da80 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
9da90 65 28 70 4f 6c 64 29 3b 0a 20 20 6e 4e 65 77 20  e(pOld);.  nNew 
9daa0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
9dab0 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70  onfig.m.xRoundup
9dac0 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20  (nBytes);.  if( 
9dad0 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20  nOld==nNew ){.  
9dae0 20 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20    pNew = pOld;. 
9daf0 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
9db00 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
9db10 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73  Memstat ){.    s
9db20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
9db30 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
9db40 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
9db50 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54  sSet(SQLITE_STAT
9db60 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20  US_MALLOC_SIZE, 
9db70 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28  nBytes);.    if(
9db80 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61   sqlite3StatusVa
9db90 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55  lue(SQLITE_STATU
9dba0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 2b 6e  S_MEMORY_USED)+n
9dbb0 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20  New-nOld >= .   
9dbc0 20 20 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72         mem0.alar
9dbd0 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20  mThreshold ){.  
9dbe0 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f      sqlite3Mallo
9dbf0 63 41 6c 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64  cAlarm(nNew-nOld
9dc00 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  );.    }.    pNe
9dc10 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  w = sqlite3Globa
9dc20 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c  lConfig.m.xReall
9dc30 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a  oc(pOld, nNew);.
9dc40 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20      if( pNew==0 
9dc50 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c  && mem0.alarmCal
9dc60 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 73  lback ){.      s
9dc70 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72  qlite3MallocAlar
9dc80 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20  m(nBytes);.     
9dc90 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47   pNew = sqlite3G
9dca0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
9dcb0 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65  ealloc(pOld, nNe
9dcc0 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  w);.    }.    if
9dcd0 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( pNew ){.      
9dce0 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 61  nNew = sqlite3Ma
9dcf0 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 77 29 3b 0a  llocSize(pNew);.
9dd00 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
9dd10 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
9dd20 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
9dd30 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20  , nNew-nOld);.  
9dd40 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
9dd50 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9dd60 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65  .mutex);.  }else
9dd70 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c  {.    pNew = sql
9dd80 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9dd90 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64  .m.xRealloc(pOld
9dda0 2c 20 6e 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 72  , nNew);.  }.  r
9ddb0 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
9ddc0 2a 0a 2a 2a 20 54 68 65 20 70 75 62 6c 69 63 20  *.** The public 
9ddd0 69 6e 74 65 72 66 61 63 65 20 74 6f 20 73 71 6c  interface to sql
9dde0 69 74 65 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61  ite3Realloc.  Ma
9ddf0 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
9de00 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 75 62 73 79   memory.** subsy
9de10 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  stem is initiali
9de20 7a 65 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76  zed prior to inv
9de30 6f 6b 69 6e 67 20 73 71 6c 69 74 65 52 65 61 6c  oking sqliteReal
9de40 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  loc..*/.SQLITE_A
9de50 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
9de60 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70  _realloc(void *p
9de70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66  Old, int n){.#if
9de80 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
9de90 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20  _AUTOINIT.  if( 
9dea0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
9deb0 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ze() ) return 0;
9dec0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
9ded0 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28   sqlite3Realloc(
9dee0 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a  pOld, n);.}.../*
9def0 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64  .** Allocate and
9df00 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f   zero memory..*/
9df10 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
9df20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61   void *sqlite3Ma
9df30 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20 6e 29 7b  llocZero(int n){
9df40 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c  .  void *p = sql
9df50 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20  ite3Malloc(n);. 
9df60 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65   if( p ){.    me
9df70 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20  mset(p, 0, n);. 
9df80 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
9df90 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
9dfa0 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79   and zero memory
9dfb0 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61  .  If the alloca
9dfc0 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65  tion fails, make
9dfd0 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  .** the mallocFa
9dfe0 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65  iled flag in the
9dff0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e   connection poin
9e000 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ter..*/.SQLITE_P
9e010 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
9e020 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
9e030 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
9e040 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20  t n){.  void *p 
9e050 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
9e060 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69  cRaw(db, n);.  i
9e070 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73  f( p ){.    mems
9e080 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d  et(p, 0, n);.  }
9e090 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
9e0a0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
9e0b0 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20  nd zero memory. 
9e0c0 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   If the allocati
9e0d0 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a  on fails, make.*
9e0e0 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c  * the mallocFail
9e0f0 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63  ed flag in the c
9e100 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65  onnection pointe
9e110 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 62 21 3d  r..**.** If db!=
9e120 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63  0 and db->malloc
9e130 46 61 69 6c 65 64 20 69 73 20 74 72 75 65 20 28  Failed is true (
9e140 69 6e 64 69 63 61 74 69 6e 67 20 61 20 70 72 69  indicating a pri
9e150 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69  or malloc.** fai
9e160 6c 75 72 65 20 6f 6e 20 74 68 65 20 73 61 6d 65  lure on the same
9e170 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
9e180 74 69 6f 6e 29 20 74 68 65 6e 20 61 6c 77 61 79  tion) then alway
9e190 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 20 48  s return 0..** H
9e1a0 65 6e 63 65 20 66 6f 72 20 61 20 70 61 72 74 69  ence for a parti
9e1b0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63  cular database c
9e1c0 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 6e 63 65 20  onnection, once 
9e1d0 6d 61 6c 6c 6f 63 20 73 74 61 72 74 73 0a 2a 2a  malloc starts.**
9e1e0 20 66 61 69 6c 69 6e 67 2c 20 69 74 20 66 61 69   failing, it fai
9e1f0 6c 73 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20  ls consistently 
9e200 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c  until mallocFail
9e210 65 64 20 69 73 20 72 65 73 65 74 2e 0a 2a 2a 20  ed is reset..** 
9e220 54 68 69 73 20 69 73 20 61 6e 20 69 6d 70 6f 72  This is an impor
9e230 74 61 6e 74 20 61 73 73 75 6d 70 74 69 6f 6e 2e  tant assumption.
9e240 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79    There are many
9e250 20 70 6c 61 63 65 73 20 69 6e 20 74 68 65 0a 2a   places in the.*
9e260 2a 20 63 6f 64 65 20 74 68 61 74 20 64 6f 20 74  * code that do t
9e270 68 69 6e 67 73 20 6c 69 6b 65 20 74 68 69 73 3a  hings like this:
9e280 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 69  .**.**         i
9e290 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a 29 73 71  nt *a = (int*)sq
9e2a0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
9e2b0 28 64 62 2c 20 31 30 30 29 3b 0a 2a 2a 20 20 20  (db, 100);.**   
9e2c0 20 20 20 20 20 20 69 6e 74 20 2a 62 20 3d 20 28        int *b = (
9e2d0 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  int*)sqlite3DbMa
9e2e0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32 30 30 29  llocRaw(db, 200)
9e2f0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66 28  ;.**         if(
9e300 20 62 20 29 20 61 5b 31 30 5d 20 3d 20 39 3b 0a   b ) a[10] = 9;.
9e310 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
9e320 6f 72 64 73 2c 20 69 66 20 61 20 73 75 62 73 65  ords, if a subse
9e330 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78  quent malloc (ex
9e340 3a 20 22 62 22 29 20 77 6f 72 6b 65 64 2c 20 69  : "b") worked, i
9e350 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20  t is assumed.** 
9e360 74 68 61 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d  that all prior m
9e370 61 6c 6c 6f 63 73 20 28 65 78 3a 20 22 61 22 29  allocs (ex: "a")
9e380 20 77 6f 72 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a   worked too..*/.
9e390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
9e3a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61  oid *sqlite3DbMa
9e3b0 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 20  llocRaw(sqlite3 
9e3c0 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76  *db, int n){.  v
9e3d0 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  oid *p;.  assert
9e3e0 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ( db==0 || sqlit
9e3f0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
9e400 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 6e  ->mutex) );.#ifn
9e410 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
9e420 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 69 66 28 20  LOOKASIDE.  if( 
9e430 64 62 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73  db ){.    Lookas
9e440 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20  ideSlot *pBuf;. 
9e450 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
9e460 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
9e470 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
9e480 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f  .    if( db->loo
9e490 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
9e4a0 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73  && n<=db->lookas
9e4b0 69 64 65 2e 73 7a 0a 20 20 20 20 20 20 20 20 20  ide.sz.         
9e4c0 26 26 20 28 70 42 75 66 20 3d 20 64 62 2d 3e 6c  && (pBuf = db->l
9e4d0 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 29 21  ookaside.pFree)!
9e4e0 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  =0 ){.      db->
9e4f0 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20  lookaside.pFree 
9e500 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74 3b 0a 20  = pBuf->pNext;. 
9e510 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69       db->lookasi
9e520 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 20 20 20  de.nOut++;.     
9e530 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   if( db->lookasi
9e540 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b  de.nOut>db->look
9e550 61 73 69 64 65 2e 6d 78 4f 75 74 20 29 7b 0a 20  aside.mxOut ){. 
9e560 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61         db->looka
9e570 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d  side.mxOut = db-
9e580 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b  >lookaside.nOut;
9e590 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
9e5a0 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 42 75  eturn (void*)pBu
9e5b0 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c  f;.    }.  }.#el
9e5c0 73 65 0a 20 20 69 66 28 20 64 62 20 26 26 20 64  se.  if( db && d
9e5d0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
9e5e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
9e5f0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 20  .  }.#endif.  p 
9e600 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
9e610 6e 29 3b 0a 20 20 69 66 28 20 21 70 20 26 26 20  n);.  if( !p && 
9e620 64 62 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  db ){.    db->ma
9e630 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
9e640 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
9e650 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20  }../*.** Resize 
9e660 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  the block of mem
9e670 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ory pointed to b
9e680 79 20 70 20 74 6f 20 6e 20 62 79 74 65 73 2e 20  y p to n bytes. 
9e690 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 69 7a 65  If the.** resize
9e6a0 20 66 61 69 6c 73 2c 20 73 65 74 20 74 68 65 20   fails, set the 
9e6b0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
9e6c0 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74  g in the connect
9e6d0 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53  ion object..*/.S
9e6e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9e6f0 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61  id *sqlite3DbRea
9e700 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a 64 62  lloc(sqlite3 *db
9e710 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e  , void *p, int n
9e720 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 20  ){.  void *pNew 
9e730 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 64  = 0;.  assert( d
9e740 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  b!=0 );.  assert
9e750 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
9e760 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
9e770 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
9e780 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a  locFailed==0 ){.
9e790 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a      if( p==0 ){.
9e7a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c        return sql
9e7b0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
9e7c0 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20  db, n);.    }.  
9e7d0 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64    if( isLookasid
9e7e0 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20  e(db, p) ){.    
9e7f0 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f    if( n<=db->loo
9e800 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a 20 20 20  kaside.sz ){.   
9e810 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20       return p;. 
9e820 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65       }.      pNe
9e830 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
9e840 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20  locRaw(db, n);. 
9e850 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b       if( pNew ){
9e860 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
9e870 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f  pNew, p, db->loo
9e880 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 20 20 20  kaside.sz);.    
9e890 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
9e8a0 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20  e(db, p);.      
9e8b0 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
9e8c0 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
9e8d0 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b  3_realloc(p, n);
9e8e0 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77  .      if( !pNew
9e8f0 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   ){.        db->
9e900 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
9e910 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
9e920 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
9e930 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  w;.}../*.** Atte
9e940 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74  mpt to reallocat
9e950 65 20 70 2e 20 20 49 66 20 74 68 65 20 72 65 61  e p.  If the rea
9e960 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
9e970 20 74 68 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20   then free p.** 
9e980 61 6e 64 20 73 65 74 20 74 68 65 20 6d 61 6c 6c  and set the mall
9e990 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e  ocFailed flag in
9e9a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
9e9b0 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
9e9c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9e9d0 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c   *sqlite3DbReall
9e9e0 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33  ocOrFree(sqlite3
9e9f0 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69   *db, void *p, i
9ea00 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70  nt n){.  void *p
9ea10 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  New;.  pNew = sq
9ea20 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64  lite3DbRealloc(d
9ea30 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20  b, p, n);.  if( 
9ea40 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 73 71 6c  !pNew ){.    sql
9ea50 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
9ea60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
9ea70 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  pNew;.}../*.** M
9ea80 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61 20  ake a copy of a 
9ea90 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79  string in memory
9eaa0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
9eab0 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54  qliteMalloc(). T
9eac0 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f  hese .** functio
9ead0 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d  ns call sqlite3M
9eae0 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 72 65 63  allocRaw() direc
9eaf0 74 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 20 73  tly instead of s
9eb00 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54  qliteMalloc(). T
9eb10 68 69 73 0a 2a 2a 20 69 73 20 62 65 63 61 75 73  his.** is becaus
9eb20 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 64 65  e when memory de
9eb30 62 75 67 67 69 6e 67 20 69 73 20 74 75 72 6e 65  bugging is turne
9eb40 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 77 6f 20  d on, these two 
9eb50 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a  functions are .*
9eb60 2a 20 63 61 6c 6c 65 64 20 76 69 61 20 6d 61 63  * called via mac
9eb70 72 6f 73 20 74 68 61 74 20 72 65 63 6f 72 64 20  ros that record 
9eb80 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 6c 65  the current file
9eb90 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d 62 65 72   and line number
9eba0 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68 72 65 61   in the.** Threa
9ebb0 64 44 61 74 61 20 73 74 72 75 63 74 75 72 65 2e  dData structure.
9ebc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9ebd0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
9ebe0 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 33  DbStrDup(sqlite3
9ebf0 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
9ec00 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e   *z){.  char *zN
9ec10 65 77 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 3b 0a  ew;.  size_t n;.
9ec20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
9ec30 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
9ec40 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72    n = sqlite3Str
9ec50 6c 65 6e 33 30 28 7a 29 20 2b 20 31 3b 0a 20 20  len30(z) + 1;.  
9ec60 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 66  assert( (n&0x7ff
9ec70 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a  fffff)==n );.  z
9ec80 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
9ec90 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e  allocRaw(db, (in
9eca0 74 29 6e 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77  t)n);.  if( zNew
9ecb0 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a   ){.    memcpy(z
9ecc0 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 7d 0a  New, z, n);.  }.
9ecd0 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d    return zNew;.}
9ece0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9ecf0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53  char *sqlite3DbS
9ed00 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 20 2a  trNDup(sqlite3 *
9ed10 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
9ed20 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61  z, int n){.  cha
9ed30 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 66 28 20 7a  r *zNew;.  if( z
9ed40 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
9ed50 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 0;.  }.  asser
9ed60 74 28 20 28 6e 26 30 78 37 66 66 66 66 66 66 66  t( (n&0x7fffffff
9ed70 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d  )==n );.  zNew =
9ed80 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
9ed90 52 61 77 28 64 62 2c 20 6e 2b 31 29 3b 0a 20 20  Raw(db, n+1);.  
9eda0 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20  if( zNew ){.    
9edb0 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20  memcpy(zNew, z, 
9edc0 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 5b 6e 5d 20  n);.    zNew[n] 
9edd0 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
9ede0 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n zNew;.}../*.**
9edf0 20 43 72 65 61 74 65 20 61 20 73 74 72 69 6e 67   Create a string
9ee00 20 66 72 6f 6d 20 74 68 65 20 7a 46 72 6f 6d 61   from the zFroma
9ee10 74 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74  t argument and t
9ee20 68 65 20 76 61 5f 6c 69 73 74 20 74 68 61 74 20  he va_list that 
9ee30 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 20 53 74 6f 72  follows..** Stor
9ee40 65 20 74 68 65 20 73 74 72 69 6e 67 20 69 6e 20  e the string in 
9ee50 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
9ee60 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  from sqliteMallo
9ee70 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a  c() and make *pz
9ee80 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 61  .** point to tha
9ee90 74 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  t string..*/.SQL
9eea0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9eeb0 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
9eec0 67 28 63 68 61 72 20 2a 2a 70 7a 2c 20 73 71 6c  g(char **pz, sql
9eed0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
9eee0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
9eef0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
9ef00 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20  p;.  char *z;.. 
9ef10 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
9ef20 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71  ormat);.  z = sq
9ef30 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62  lite3VMPrintf(db
9ef40 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
9ef50 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
9ef60 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
9ef70 2c 20 2a 70 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20  , *pz);.  *pz = 
9ef80 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  z;.}.../*.** Thi
9ef90 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  s function must 
9efa0 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
9efb0 20 65 78 69 74 69 6e 67 20 61 6e 79 20 41 50 49   exiting any API
9efc0 20 66 75 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20   function (i.e. 
9efd0 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20 63 6f  .** returning co
9efe0 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65  ntrol to the use
9eff0 72 29 20 74 68 61 74 20 68 61 73 20 63 61 6c 6c  r) that has call
9f000 65 64 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ed sqlite3_mallo
9f010 63 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  c or.** sqlite3_
9f020 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54  realloc..**.** T
9f030 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
9f040 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 20  e is normally a 
9f050 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f  copy of the seco
9f060 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
9f070 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e  his.** function.
9f080 20 48 6f 77 65 76 65 72 2c 20 69 66 20 61 20 6d   However, if a m
9f090 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20  alloc() failure 
9f0a0 68 61 73 20 6f 63 63 75 72 72 65 64 20 73 69 6e  has occurred sin
9f0b0 63 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a  ce the previous.
9f0c0 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 53 51  ** invocation SQ
9f0d0 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65  LITE_NOMEM is re
9f0e0 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 20  turned instead. 
9f0f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
9f100 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 64 62  rst argument, db
9f110 2c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e  , is not NULL an
9f120 64 20 61 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72  d a malloc() err
9f130 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c  or has occurred,
9f140 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e  .** then the con
9f150 6e 65 63 74 69 6f 6e 20 65 72 72 6f 72 2d 63 6f  nection error-co
9f160 64 65 20 28 74 68 65 20 76 61 6c 75 65 20 72 65  de (the value re
9f170 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
9f180 33 5f 65 72 72 63 6f 64 65 28 29 29 0a 2a 2a 20  3_errcode()).** 
9f190 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
9f1a0 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54  _NOMEM..*/.SQLIT
9f1b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
9f1c0 6c 69 74 65 33 41 70 69 45 78 69 74 28 73 71 6c  lite3ApiExit(sql
9f1d0 69 74 65 33 2a 20 64 62 2c 20 69 6e 74 20 72 63  ite3* db, int rc
9f1e0 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64  ){.  /* If the d
9f1f0 62 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20  b handle is not 
9f200 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65 20 6d 75  NULL, then we mu
9f210 73 74 20 68 6f 6c 64 20 74 68 65 20 63 6f 6e 6e  st hold the conn
9f220 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 0a 20 20  ection handle.  
9f230 2a 2a 20 6d 75 74 65 78 20 68 65 72 65 2e 20 4f  ** mutex here. O
9f240 74 68 65 72 77 69 73 65 20 74 68 65 20 72 65 61  therwise the rea
9f250 64 20 28 61 6e 64 20 70 6f 73 73 69 62 6c 65 20  d (and possible 
9f260 77 72 69 74 65 29 20 6f 66 20 64 62 2d 3e 6d 61  write) of db->ma
9f270 6c 6c 6f 63 46 61 69 6c 65 64 20 0a 20 20 2a 2a  llocFailed .  **
9f280 20 69 73 20 75 6e 73 61 66 65 2c 20 61 73 20 69   is unsafe, as i
9f290 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71  s the call to sq
9f2a0 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a 20 20  lite3Error()..  
9f2b0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  */.  assert( !db
9f2c0 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
9f2d0 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
9f2e0 29 20 29 3b 0a 20 20 69 66 28 20 64 62 20 26 26  ) );.  if( db &&
9f2f0 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   (db->mallocFail
9f300 65 64 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45  ed || rc==SQLITE
9f310 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29 20 29 7b  _IOERR_NOMEM) ){
9f320 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
9f330 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  r(db, SQLITE_NOM
9f340 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e  EM, 0);.    db->
9f350 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30  mallocFailed = 0
9f360 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
9f370 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72  E_NOMEM;.  }.  r
9f380 65 74 75 72 6e 20 72 63 20 26 20 28 64 62 20 3f  eturn rc & (db ?
9f390 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3a 20 30   db->errMask : 0
9f3a0 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  xff);.}../******
9f3b0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
9f3c0 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a  malloc.c *******
9f3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f3f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
9f400 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
9f410 69 6c 65 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a  ile printf.c ***
9f420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f440 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54  *******/./*.** T
9f450 68 65 20 22 70 72 69 6e 74 66 22 20 63 6f 64 65  he "printf" code
9f460 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64 61   that follows da
9f470 74 65 73 20 66 72 6f 6d 20 74 68 65 20 31 39 38  tes from the 198
9f480 30 27 73 2e 20 20 49 74 20 69 73 20 69 6e 0a 2a  0's.  It is in.*
9f490 2a 20 74 68 65 20 70 75 62 6c 69 63 20 64 6f 6d  * the public dom
9f4a0 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 67 69 6e  ain.  The origin
9f4b0 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20  al comments are 
9f4c0 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20 66 6f  included here fo
9f4d0 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 6e 65 73  r.** completenes
9f4e0 73 2e 20 20 54 68 65 79 20 61 72 65 20 76 65 72  s.  They are ver
9f4f0 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 62 75  y out-of-date bu
9f500 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 66 75  t might be usefu
9f510 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69 73 74 6f  l as.** an histo
9f520 72 69 63 61 6c 20 72 65 66 65 72 65 6e 63 65 2e  rical reference.
9f530 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 22 65    Most of the "e
9f540 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20 68 61 76  nhancements" hav
9f550 65 20 62 65 65 6e 20 62 61 63 6b 65 64 0a 2a 2a  e been backed.**
9f560 20 6f 75 74 20 73 6f 20 74 68 61 74 20 74 68 65   out so that the
9f570 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69   functionality i
9f580 73 20 6e 6f 77 20 74 68 65 20 73 61 6d 65 20 61  s now the same a
9f590 73 20 73 74 61 6e 64 61 72 64 20 70 72 69 6e 74  s standard print
9f5a0 66 28 29 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  f()..**.********
9f5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f5f0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  **.**.** The fol
9f600 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69  lowing modules i
9f610 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65  s an enhanced re
9f620 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68  placement for th
9f630 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f  e "printf" subro
9f640 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20  utines.** found 
9f650 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  in the standard 
9f660 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  C library.  The 
9f670 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63  following enhanc
9f680 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75  ements are.** su
9f690 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  pported:.**.**  
9f6a0 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61      +  Additiona
9f6b0 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  l functions.  Th
9f6c0 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f  e standard set o
9f6d0 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74  f "printf" funct
9f6e0 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ions.**         
9f6f0 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c  includes printf,
9f700 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74   fprintf, sprint
9f710 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72  f, vprintf, vfpr
9f720 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20  intf, and.**    
9f730 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20       vsprintf.  
9f740 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73  This module adds
9f750 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
9f760 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
9f770 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57  *  snprintf -- W
9f780 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74  orks like sprint
9f790 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65 78  f, but has an ex
9f7a0 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  tra argument.** 
9f7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f7c0 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20 69           which i
9f7d0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
9f7e0 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65 6e  e buffer written
9f7f0 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   to..**.**      
9f800 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20       *  mprintf 
9f810 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73  --  Similar to s
9f820 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20  printf.  Writes 
9f830 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79  output to memory
9f840 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9f850 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62 74               obt
9f860 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
9f870 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  c..**.**        
9f880 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d     *  xprintf --
9f890 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69    Calls a functi
9f8a0 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  on to dispose of
9f8b0 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20   output..**.**  
9f8c0 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69           *  npri
9f8d0 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75  ntf --  No outpu
9f8e0 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20 74  t, but returns t
9f8f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
9f900 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20  racters.**      
9f910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f920 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68      that would h
9f930 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20  ave been output 
9f940 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a  by printf..**.**
9f950 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41 20             *  A 
9f960 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20  v- version (ex: 
9f970 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76  vsnprintf) of ev
9f980 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ery function is 
9f990 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  also.**         
9f9a0 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a       supplied..*
9f9b0 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66  *.**      +  A f
9f9c0 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f  ew extensions to
9f9d0 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20   the formatting 
9f9e0 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70  notation are sup
9f9f0 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  ported:.**.**   
9fa00 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 22          *  The "
9fa10 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72  =" flag (similar
9fa20 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20   to "-") causes 
9fa30 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65  the output to be
9fa40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9fa50 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20   be centered in 
9fa60 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c  the appropriatel
9fa70 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a  y sized field..*
9fa80 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
9fa90 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f    The %b field o
9faa0 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65  utputs an intege
9fab0 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61  r in binary nota
9fac0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  tion..**.**     
9fad0 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63 20        *  The %c 
9fae0 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74  field now accept
9faf0 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20  s a precision.  
9fb00 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f 75  The character ou
9fb10 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  tput.**         
9fb20 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65 64       is repeated
9fb30 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
9fb40 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65 63  f times the prec
9fb50 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e  ision specifies.
9fb60 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
9fb70 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c 64   *  The %' field
9fb80 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20   works like %c, 
9fb90 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74 73  but takes as its
9fba0 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a   character the.*
9fbb0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  *              n
9fbc0 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f 66  ext character of
9fbd0 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69   the format stri
9fbe0 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74  ng, instead of t
9fbf0 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20  he next.**      
9fc00 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74          argument
9fc10 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
9fc20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22   printf("%.78'-"
9fc30 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e  )  prints 78 min
9fc40 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  us.**           
9fc50 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61     signs, the sa
9fc60 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22 25  me as  printf("%
9fc70 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a  .78c",'-')..**.*
9fc80 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63  *      +  When c
9fc90 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43  ompiled using GC
9fca0 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68  C on a SPARC, th
9fcb0 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72  is version of pr
9fcc0 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20  intf is.**      
9fcd0 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20 74     faster than t
9fce0 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74  he library print
9fcf0 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31  f for SUN OS 4.1
9fd00 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20  ..**.**      +  
9fd10 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  All functions ar
9fd20 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e  e fully reentran
9fd30 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  t..**.*/../*.** 
9fd40 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73  Conversion types
9fd50 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f   fall into vario
9fd60 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61 73  us categories as
9fd70 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a   defined by the.
9fd80 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75  ** following enu
9fd90 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65  meration..*/.#de
9fda0 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20 20  fine etRADIX    
9fdb0 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20     1 /* Integer 
9fdc0 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20  types.  %d, %x, 
9fdd0 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  %o, and so forth
9fde0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c   */.#define etFL
9fdf0 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46  OAT       2 /* F
9fe00 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20  loating point.  
9fe10 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %f */.#define et
9fe20 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a  EXP         3 /*
9fe30 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e   Exponentional n
9fe40 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20  otation. %e and 
9fe50 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %E */.#define et
9fe60 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a  GENERIC     4 /*
9fe70 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70   Floating or exp
9fe80 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64  onential, depend
9fe90 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e  ing on exponent.
9fea0 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %g */.#define e
9feb0 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f  tSIZE        5 /
9fec0 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20  * Return number 
9fed0 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70 72  of characters pr
9fee0 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20  ocessed so far. 
9fef0 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %n */.#define et
9ff00 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a  STRING      6 /*
9ff10 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a   Strings. %s */.
9ff20 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52  #define etDYNSTR
9ff30 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d  ING   7 /* Dynam
9ff40 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
9ff50 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a   strings. %z */.
9ff60 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e  #define etPERCEN
9ff70 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65  T     8 /* Perce
9ff80 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f  nt symbol. %% */
9ff90 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52 58  .#define etCHARX
9ffa0 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72         9 /* Char
9ffb0 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a  acters. %c */./*
9ffc0 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65 78   The rest are ex
9ffd0 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f  tensions, not no
9ffe0 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20  rmally found in 
9fff0 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66  printf() */.#def
a0000 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 20  ine etSQLESCAPE 
a0010 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77   10 /* Strings w
a0020 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64  ith '\'' doubled
a0030 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65  .  %q */.#define
a0040 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31 31   etSQLESCAPE2 11
a0050 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68   /* Strings with
a0060 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e   '\'' doubled an
a0070 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27  d enclosed in ''
a0080 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
a0090 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
a00a0 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63   pointers replac
a00b0 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20  ed by SQL NULL. 
a00c0 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %Q */.#define e
a00d0 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f  tTOKEN      12 /
a00e0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
a00f0 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65   Token structure
a0100 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52   */.#define etSR
a0110 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61  CLIST    13 /* a
a0120 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72   pointer to a Sr
a0130 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65  cList */.#define
a0140 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34   etPOINTER    14
a0150 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65   /* The %p conve
a0160 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  rsion */.#define
a0170 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31 35   etSQLESCAPE3 15
a0180 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67   /* %w -> String
a0190 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62  s with '\"' doub
a01a0 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65  led */.#define e
a01b0 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f  tORDINAL    16 /
a01c0 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64  * %r -> 1st, 2nd
a01d0 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e  , 3rd, 4th, etc.
a01e0 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a    English only *
a01f0 2f 0a 0a 23 64 65 66 69 6e 65 20 65 74 49 4e 56  /..#define etINV
a0200 41 4c 49 44 20 20 20 20 20 30 20 2f 2a 20 41 6e  ALID     0 /* An
a0210 79 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 63  y unrecognized c
a0220 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 2a  onversion type *
a0230 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74  /.../*.** An "et
a0240 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69  Byte" is an 8-bi
a0250 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65  t unsigned value
a0260 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73  ..*/.typedef uns
a0270 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79 74  igned char etByt
a0280 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62  e;../*.** Each b
a0290 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f  uiltin conversio
a02a0 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78 3a  n character (ex:
a02b0 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64 22   the 'd' in "%d"
a02c0 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a  ) is described.*
a02d0 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  * by an instance
a02e0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
a02f0 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74  g structure.*/.t
a0300 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65 74  ypedef struct et
a0310 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66  _info {   /* Inf
a0320 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65  ormation about e
a0330 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64  ach format field
a0340 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79   */.  char fmtty
a0350 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  pe;            /
a0360 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65  * The format fie
a0370 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a  ld code letter *
a0380 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65 3b  /.  etByte base;
a0390 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a03a0 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61 64  The base for rad
a03b0 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f  ix conversion */
a03c0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b  .  etByte flags;
a03d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
a03e0 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c  ne or more of FL
a03f0 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65  AG_ constants be
a0400 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  low */.  etByte 
a0410 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  type;           
a0420 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20    /* Conversion 
a0430 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74  paradigm */.  et
a0440 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20 20  Byte charset;   
a0450 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
a0460 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20   into aDigits[] 
a0470 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73 74  of the digits st
a0480 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65  ring */.  etByte
a0490 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20   prefix;        
a04a0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
a04b0 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74  o aPrefix[] of t
a04c0 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e 67  he prefix string
a04d0 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a   */.} et_info;..
a04e0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
a04f0 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f  lues for et_info
a0500 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e  .flags.*/.#defin
a0510 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31  e FLAG_SIGNED  1
a0520 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a0530 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e  the value to con
a0540 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a  vert is signed *
a0550 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49  /.#define FLAG_I
a0560 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20  NTERN  2     /* 
a0570 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65  True if for inte
a0580 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f  rnal use only */
a0590 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54  .#define FLAG_ST
a05a0 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41  RING  4     /* A
a05b0 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72  llow infinity pr
a05c0 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a  ecision */.../*.
a05d0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
a05e0 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63 68   table is search
a05f0 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20  ed linearly, so 
a0600 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75  it is good to pu
a0610 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72  t the.** most fr
a0620 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f  equently used co
a0630 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66  nversion types f
a0640 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  irst..*/.static 
a0650 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67 69  const char aDigi
a0660 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37  ts[] = "01234567
a0670 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36 37  89ABCDEF01234567
a0680 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69  89abcdef";.stati
a0690 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72  c const char aPr
a06a0 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30  efix[] = "-x0\00
a06b0 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e  0X0";.static con
a06c0 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e  st et_info fmtin
a06d0 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64  fo[] = {.  {  'd
a06e0 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49  ', 10, 1, etRADI
a06f0 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  X,      0,  0 },
a0700 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34  .  {  's',  0, 4
a0710 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20  , etSTRING,     
a0720 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67  0,  0 },.  {  'g
a0730 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45  ',  0, 1, etGENE
a0740 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c  RIC,    30, 0 },
a0750 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34  .  {  'z',  0, 4
a0760 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20  , etDYNSTRING,  
a0770 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71  0,  0 },.  {  'q
a0780 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45  ',  0, 4, etSQLE
a0790 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c  SCAPE,  0,  0 },
a07a0 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34  .  {  'Q',  0, 4
a07b0 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20  , etSQLESCAPE2, 
a07c0 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77  0,  0 },.  {  'w
a07d0 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45  ',  0, 4, etSQLE
a07e0 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c  SCAPE3, 0,  0 },
a07f0 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30  .  {  'c',  0, 0
a0800 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20 20  , etCHARX,      
a0810 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f  0,  0 },.  {  'o
a0820 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49  ',  8, 0, etRADI
a0830 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c  X,      0,  2 },
a0840 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30  .  {  'u', 10, 0
a0850 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
a0860 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78  0,  0 },.  {  'x
a0870 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49  ', 16, 0, etRADI
a0880 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c  X,      16, 1 },
a0890 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30  .  {  'X', 16, 0
a08a0 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
a08b0 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66  0,  4 },.#ifndef
a08c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
a08d0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20  ATING_POINT.  { 
a08e0 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46   'f',  0, 1, etF
a08f0 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30  LOAT,      0,  0
a0900 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30   },.  {  'e',  0
a0910 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20  , 1, etEXP,     
a0920 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20     30, 0 },.  { 
a0930 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45   'E',  0, 1, etE
a0940 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30  XP,        14, 0
a0950 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30   },.  {  'G',  0
a0960 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20  , 1, etGENERIC, 
a0970 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64     14, 0 },.#end
a0980 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c  if.  {  'i', 10,
a0990 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20   1, etRADIX,    
a09a0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
a09b0 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49  'n',  0, 0, etSI
a09c0 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20  ZE,       0,  0 
a09d0 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c  },.  {  '%',  0,
a09e0 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20   0, etPERCENT,  
a09f0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
a0a00 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f  'p', 16, 0, etPO
a0a10 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20  INTER,    0,  1 
a0a20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 72  },../* All the r
a0a30 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c 41  est have the FLA
a0a40 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65 74  G_INTERN bit set
a0a50 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66 6f   and are thus fo
a0a60 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
a0a70 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 27  e only */.  {  '
a0a80 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f 4b  T',  0, 2, etTOK
a0a90 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  EN,      0,  0 }
a0aa0 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c 20  ,.  {  'S',  0, 
a0ab0 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20 20  2, etSRCLIST,   
a0ac0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a0ad0 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52 44  r', 10, 3, etORD
a0ae0 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20 7d  INAL,    0,  0 }
a0af0 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53  ,.};../*.** If S
a0b00 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
a0b10 49 4e 47 5f 50 4f 49 4e 54 20 69 73 20 64 65 66  ING_POINT is def
a0b20 69 6e 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20  ined, then none 
a0b30 6f 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20  of the floating 
a0b40 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 73  point.** convers
a0b50 69 6f 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a  ions will work..
a0b60 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
a0b70 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
a0b80 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61  POINT./*.** "*va
a0b90 6c 22 20 69 73 20 61 20 64 6f 75 62 6c 65 20 73  l" is a double s
a0ba0 75 63 68 20 74 68 61 74 20 30 2e 31 20 3c 3d 20  uch that 0.1 <= 
a0bb0 2a 76 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 52  *val < 10.0.** R
a0bc0 65 74 75 72 6e 20 74 68 65 20 61 73 63 69 69 20  eturn the ascii 
a0bd0 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 61  code for the lea
a0be0 64 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a 76  ding digit of *v
a0bf0 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74  al, then.** mult
a0c00 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 31  iply "*val" by 1
a0c10 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69  0.0 to renormali
a0c20 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c  ze..**.** Exampl
a0c30 65 3a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a  e:.**     input:
a0c40 20 20 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 34       *val = 3.14
a0c50 31 35 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75  159.**     outpu
a0c60 74 3a 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e 34  t:    *val = 1.4
a0c70 31 35 39 20 20 20 20 66 75 6e 63 74 69 6f 6e 20  159    function 
a0c80 72 65 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a  return = '3'.**.
a0c90 2a 2a 20 54 68 65 20 63 6f 75 6e 74 65 72 20 2a  ** The counter *
a0ca0 63 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  cnt is increment
a0cb0 65 64 20 65 61 63 68 20 74 69 6d 65 2e 20 20 41  ed each time.  A
a0cc0 66 74 65 72 20 63 6f 75 6e 74 65 72 20 65 78 63  fter counter exc
a0cd0 65 65 64 73 0a 2a 2a 20 31 36 20 28 74 68 65 20  eeds.** 16 (the 
a0ce0 6e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66  number of signif
a0cf0 69 63 61 6e 74 20 64 69 67 69 74 73 20 69 6e 20  icant digits in 
a0d00 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 29 20  a 64-bit float) 
a0d10 27 30 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73  '0' is.** always
a0d20 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
a0d30 61 74 69 63 20 63 68 61 72 20 65 74 5f 67 65 74  atic char et_get
a0d40 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c 45  digit(LONGDOUBLE
a0d50 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74 20  _TYPE *val, int 
a0d60 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69 67  *cnt){.  int dig
a0d70 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45  it;.  LONGDOUBLE
a0d80 5f 54 59 50 45 20 64 3b 0a 20 20 69 66 28 20 28  _TYPE d;.  if( (
a0d90 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20  *cnt)++ >= 16 ) 
a0da0 72 65 74 75 72 6e 20 27 30 27 3b 0a 20 20 64 69  return '0';.  di
a0db0 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b  git = (int)*val;
a0dc0 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 20  .  d = digit;.  
a0dd0 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20  digit += '0';.  
a0de0 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64  *val = (*val - d
a0df0 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e  )*10.0;.  return
a0e00 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a   (char)digit;.}.
a0e10 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
a0e20 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
a0e30 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  OINT */../*.** A
a0e40 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68  ppend N space ch
a0e50 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 20  aracters to the 
a0e60 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66  given string buf
a0e70 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  fer..*/.static v
a0e80 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65 28  oid appendSpace(
a0e90 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d  StrAccum *pAccum
a0ea0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74  , int N){.  stat
a0eb0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53  ic const char zS
a0ec0 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20  paces[] = "     
a0ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0ee0 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69          ";.  whi
a0ef0 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65  le( N>=(int)size
a0f00 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b  of(zSpaces)-1 ){
a0f10 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41  .    sqlite3StrA
a0f20 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
a0f30 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65  m, zSpaces, size
a0f40 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a  of(zSpaces)-1);.
a0f50 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28      N -= sizeof(
a0f60 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a  zSpaces)-1;.  }.
a0f70 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20    if( N>0 ){.   
a0f80 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a0f90 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a  Append(pAccum, z
a0fa0 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a  Spaces, N);.  }.
a0fb0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68  }../*.** On mach
a0fc0 69 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c  ines with a smal
a0fd0 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f  l stack size, yo
a0fe0 75 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74  u can redefine t
a0ff0 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49  he.** SQLITE_PRI
a1000 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62  NT_BUF_SIZE to b
a1010 65 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30 2e  e less than 350.
a1020 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
a1030 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a  TE_PRINT_BUF_SIZ
a1040 45 0a 23 20 69 66 20 64 65 66 69 6e 65 64 28 53  E.# if defined(S
a1050 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43  QLITE_SMALL_STAC
a1060 4b 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  K).#   define SQ
a1070 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53  LITE_PRINT_BUF_S
a1080 49 5a 45 20 35 30 0a 23 20 65 6c 73 65 0a 23 20  IZE 50.# else.# 
a1090 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
a10a0 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33  PRINT_BUF_SIZE 3
a10b0 35 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  50.# endif.#endi
a10c0 66 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46 53  f.#define etBUFS
a10d0 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e 54  IZE SQLITE_PRINT
a10e0 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69  _BUF_SIZE  /* Si
a10f0 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74  ze of the output
a1100 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a   buffer */../*.*
a1110 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 72  * The root progr
a1120 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 69  am.  All variati
a1130 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63 6f  ons call this co
a1140 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53  re..**.** INPUTS
a1150 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 68  :.**   func   Th
a1160 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  is is a pointer 
a1170 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61  to a function ta
a1180 6b 69 6e 67 20 74 68 72 65 65 20 61 72 67 75 6d  king three argum
a1190 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ents.**         
a11a0 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 20     1. A pointer 
a11b0 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 61  to anything.  Sa
a11c0 6d 65 20 61 73 20 74 68 65 20 22 61 72 67 22 20  me as the "arg" 
a11d0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20  parameter..**   
a11e0 20 20 20 20 20 20 20 20 20 32 2e 20 41 20 70 6f           2. A po
a11f0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69 73  inter to the lis
a1200 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  t of characters 
a1210 74 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20  to be output.** 
a1220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4e                (N
a1230 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20 69  ote, this list i
a1240 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69  s NOT null termi
a1250 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20  nated.).**      
a1260 20 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 65        3. An inte
a1270 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  ger number of ch
a1280 61 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f  aracters to be o
a1290 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20  utput..**       
a12a0 20 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 54          (Note: T
a12b0 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 74  his number might
a12c0 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a   be zero.).**.**
a12d0 20 20 20 61 72 67 20 20 20 20 54 68 69 73 20 69     arg    This i
a12e0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  s the pointer to
a12f0 20 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68 20   anything which 
a1300 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 61  will be passed a
a1310 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
a1320 20 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74    first argument
a1330 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 65   to "func".  Use
a1340 20 69 74 20 66 6f 72 20 77 68 61 74 65 76 65 72   it for whatever
a1350 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a   you like..**.**
a1360 20 20 20 66 6d 74 20 20 20 20 54 68 69 73 20 69     fmt    This i
a1370 73 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72  s the format str
a1380 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20 75  ing, as in the u
a1390 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a  sual print..**.*
a13a0 2a 20 20 20 61 70 20 20 20 20 20 54 68 69 73 20  *   ap     This 
a13b0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
a13c0 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65  a list of argume
a13d0 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69 6e  nts.  Same as in
a13e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66 70  .**          vfp
a13f0 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50  rint..**.** OUTP
a1400 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20  UTS:.**         
a1410 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
a1420 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  e is the total n
a1430 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
a1440 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20  ers sent to.**  
a1450 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e 63          the func
a1460 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 65  tion "func".  Re
a1470 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72  turns -1 on a er
a1480 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ror..**.** Note 
a1490 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 69  that the order i
a14a0 6e 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74 69  n which automati
a14b0 63 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  c variables are 
a14c0 64 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a  declared below.*
a14d0 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20  * seems to make 
a14e0 61 20 62 69 67 20 64 69 66 66 65 72 65 6e 63 65  a big difference
a14f0 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20   in determining 
a1500 68 6f 77 20 66 61 73 74 20 74 68 69 73 20 62 65  how fast this be
a1510 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e  ast.** will run.
a1520 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a1530 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
a1540 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41 63  XPrintf(.  StrAc
a1550 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 20  cum *pAccum,    
a1560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a1570 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73 75   Accumulate resu
a1580 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  lts here */.  in
a1590 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20 20  t useExtended,  
a15a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a15b0 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64   /* Allow extend
a15c0 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73  ed %-conversions
a15d0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
a15e0 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20 20   *fmt,          
a15f0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d           /* Form
a1600 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76  at string */.  v
a1610 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20 20  a_list ap       
a1620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1630 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a    /* arguments *
a1640 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20  /.){.  int c;   
a1650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1660 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 63    /* Next charac
a1670 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d 61  ter in the forma
a1680 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68  t string */.  ch
a1690 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20 20  ar *bufpt;      
a16a0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
a16b0 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65  ter to the conve
a16c0 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a  rsion buffer */.
a16d0 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b    int precision;
a16e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a16f0 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 65  Precision of the
a1700 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a   current field *
a1710 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20  /.  int length; 
a1720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a1730 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  * Length of the 
a1740 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69  field */.  int i
a1750 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
a1760 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 72        /* A gener
a1770 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20  al purpose loop 
a1780 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
a1790 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20 20   width;         
a17a0 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74 68          /* Width
a17b0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
a17c0 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74  field */.  etByt
a17d0 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69  e flag_leftjusti
a17e0 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66  fy;   /* True if
a17f0 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65   "-" flag is pre
a1800 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
a1810 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20   flag_plussign; 
a1820 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a1830 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  "+" flag is pres
a1840 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
a1850 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20  flag_blanksign; 
a1860 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22      /* True if "
a1870 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65   " flag is prese
a1880 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
a1890 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
a18a0 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22 23  m; /* True if "#
a18b0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
a18c0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
a18d0 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20  ag_altform2;    
a18e0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21 22    /* True if "!"
a18f0 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
a1900 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a1910 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 20  g_zeropad;      
a1920 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65 6c   /* True if fiel
a1930 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e 74  d width constant
a1940 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65 72   starts with zer
a1950 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  o */.  etByte fl
a1960 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20  ag_long;        
a1970 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c 22    /* True if "l"
a1980 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
a1990 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a19a0 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20  g_longlong;     
a19b0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
a19c0 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65  "ll" flag is pre
a19d0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
a19e0 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20   done;          
a19f0 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72       /* Loop ter
a1a00 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f  mination flag */
a1a10 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  .  sqlite_uint64
a1a20 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a   longvalue;   /*
a1a30 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 67   Value for integ
a1a40 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f  er types */.  LO
a1a50 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 65  NGDOUBLE_TYPE re
a1a60 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75  alvalue; /* Valu
a1a70 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65 73  e for real types
a1a80 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69   */.  const et_i
a1a90 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20  nfo *infop;     
a1aa0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
a1ab0 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69  he appropriate i
a1ac0 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f  nfo structure */
a1ad0 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42 55  .  char buf[etBU
a1ae0 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a  FSIZE];       /*
a1af0 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66   Conversion buff
a1b00 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 65  er */.  char pre
a1b10 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
a1b20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68 61     /* Prefix cha
a1b30 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 20  racter.  "+" or 
a1b40 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27 5c  "-" or " " or '\
a1b50 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  0'. */.  etByte 
a1b60 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20  xtype = 0;      
a1b70 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f      /* Conversio
a1b80 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20  n paradigm */.  
a1b90 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20  char *zExtra;   
a1ba0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
a1bb0 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  tra memory used 
a1bc0 66 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 20  for etTCLESCAPE 
a1bd0 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23  conversions */.#
a1be0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
a1bf0 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
a1c00 54 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32  T.  int  exp, e2
a1c10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a1c20 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65  * exponent of re
a1c30 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20  al numbers */.  
a1c40 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20  double rounder; 
a1c50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
a1c60 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20  ed for rounding 
a1c70 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
a1c80 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74  alues */.  etByt
a1c90 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20  e flag_dp;      
a1ca0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a1cb0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73   decimal point s
a1cc0 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a  hould be shown *
a1cd0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
a1ce0 72 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f  rtz;           /
a1cf0 2a 20 54 72 75 65 20 69 66 20 74 72 61 69 6c 69  * True if traili
a1d00 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20  ng zeros should 
a1d10 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20  be removed */.  
a1d20 65 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b  etByte flag_exp;
a1d30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
a1d40 75 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70  ue to force disp
a1d50 6c 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e  lay of the expon
a1d60 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64  ent */.  int nsd
a1d70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a1d80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
a1d90 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   significant dig
a1da0 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a  its returned */.
a1db0 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68  #endif..  length
a1dc0 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20   = 0;.  bufpt = 
a1dd0 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a  0;.  for(; (c=(*
a1de0 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29  fmt))!=0; ++fmt)
a1df0 7b 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 27  {.    if( c!='%'
a1e00 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d   ){.      int am
a1e10 74 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d  t;.      bufpt =
a1e20 20 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20   (char *)fmt;.  
a1e30 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20      amt = 1;.   
a1e40 20 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b     while( (c=(*+
a1e50 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63  +fmt))!='%' && c
a1e60 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20  !=0 ) amt++;.   
a1e70 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a1e80 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
a1e90 20 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20   bufpt, amt);.  
a1ea0 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62      if( c==0 ) b
a1eb0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
a1ec0 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29  if( (c=(*++fmt))
a1ed0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
a1ee0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a1ef0 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c 20  nd(pAccum, "%", 
a1f00 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  1);.      break;
a1f10 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69  .    }.    /* Fi
a1f20 6e 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61 67  nd out what flag
a1f30 73 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a 2f  s are present */
a1f40 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75  .    flag_leftju
a1f50 73 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75  stify = flag_plu
a1f60 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61  ssign = flag_bla
a1f70 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 66  nksign = .     f
a1f80 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
a1f90 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d  m = flag_altform
a1fa0 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  2 = flag_zeropad
a1fb0 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d   = 0;.    done =
a1fc0 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20   0;.    do{.    
a1fd0 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a 20    switch( c ){. 
a1fe0 20 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a         case '-':
a1ff0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
a2000 69 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72 65  ify = 1;     bre
a2010 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
a2020 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75   '+':   flag_plu
a2030 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20  ssign = 1;      
a2040 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
a2050 20 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c 61   case ' ':   fla
a2060 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b  g_blanksign = 1;
a2070 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a2080 20 20 20 20 20 20 63 61 73 65 20 27 23 27 3a 20        case '#': 
a2090 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65    flag_alternate
a20a0 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 61  form = 1;   brea
a20b0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
a20c0 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66  '!':   flag_altf
a20d0 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 20  orm2 = 1;       
a20e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
a20f0 63 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61 67  case '0':   flag
a2100 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 20  _zeropad = 1;   
a2110 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a2120 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20       default:   
a2130 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 20   done = 1;      
a2140 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
a2150 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77  ;.      }.    }w
a2160 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 28  hile( !done && (
a2170 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29  c=(*++fmt))!=0 )
a2180 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65  ;.    /* Get the
a2190 20 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a   field width */.
a21a0 20 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a 20      width = 0;. 
a21b0 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b     if( c=='*' ){
a21c0 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 76  .      width = v
a21d0 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20  a_arg(ap,int);. 
a21e0 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c 30       if( width<0
a21f0 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67   ){.        flag
a2200 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31  _leftjustify = 1
a2210 3b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20  ;.        width 
a2220 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 20  = -width;.      
a2230 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66  }.      c = *++f
a2240 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  mt;.    }else{. 
a2250 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27       while( c>='
a2260 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a  0' && c<='9' ){.
a2270 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20          width = 
a2280 77 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 27  width*10 + c - '
a2290 30 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20  0';.        c = 
a22a0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a  *++fmt;.      }.
a22b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 69      }.    if( wi
a22c0 64 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45 2d  dth > etBUFSIZE-
a22d0 31 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74  10 ){.      widt
a22e0 68 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 30  h = etBUFSIZE-10
a22f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47  ;.    }.    /* G
a2300 65 74 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e  et the precision
a2310 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27   */.    if( c=='
a2320 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63  .' ){.      prec
a2330 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20  ision = 0;.     
a2340 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20   c = *++fmt;.   
a2350 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b     if( c=='*' ){
a2360 0a 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69  .        precisi
a2370 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69  on = va_arg(ap,i
a2380 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  nt);.        if(
a2390 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70   precision<0 ) p
a23a0 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63  recision = -prec
a23b0 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63  ision;.        c
a23c0 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20   = *++fmt;.     
a23d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a23e0 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26  while( c>='0' &&
a23f0 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20   c<='9' ){.     
a2400 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d       precision =
a2410 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20   precision*10 + 
a2420 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  c - '0';.       
a2430 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
a2440 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
a2450 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
a2460 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31    precision = -1
a2470 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47  ;.    }.    /* G
a2480 65 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  et the conversio
a2490 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72 20  n type modifier 
a24a0 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 6c  */.    if( c=='l
a24b0 27 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f  ' ){.      flag_
a24c0 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  long = 1;.      
a24d0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
a24e0 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a    if( c=='l' ){.
a24f0 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e          flag_lon
a2500 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20  glong = 1;.     
a2510 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
a2520 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a2530 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e      flag_longlon
a2540 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  g = 0;.      }. 
a2550 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a2560 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67  flag_long = flag
a2570 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20  _longlong = 0;. 
a2580 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63     }.    /* Fetc
a2590 68 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 79  h the info entry
a25a0 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20 2a   for the field *
a25b0 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66  /.    infop = &f
a25c0 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 78  mtinfo[0];.    x
a25d0 74 79 70 65 20 3d 20 65 74 49 4e 56 41 4c 49 44  type = etINVALID
a25e0 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 30 3b  ;.    for(idx=0;
a25f0 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 28 66   idx<ArraySize(f
a2600 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 7b  mtinfo); idx++){
a2610 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66 6d  .      if( c==fm
a2620 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79  tinfo[idx].fmtty
a2630 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  pe ){.        in
a2640 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69  fop = &fmtinfo[i
a2650 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28  dx];.        if(
a2660 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c 20   useExtended || 
a2670 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20  (infop->flags & 
a2680 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20  FLAG_INTERN)==0 
a2690 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74 79  ){.          xty
a26a0 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65  pe = infop->type
a26b0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
a26c0 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
a26d0 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  n;.        }.   
a26e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a26f0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45    }.    }.    zE
a2700 78 74 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 20  xtra = 0;...    
a2710 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65  /* Limit the pre
a2720 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e  cision to preven
a2730 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75  t overflowing bu
a2740 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65  f[] during conve
a2750 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28  rsion */.    if(
a2760 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46   precision>etBUF
a2770 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f  SIZE-40 && (info
a2780 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f  p->flags & FLAG_
a2790 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20  STRING)==0 ){.  
a27a0 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20      precision = 
a27b0 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20  etBUFSIZE-40;.  
a27c0 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
a27d0 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  ** At this point
a27e0 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  , variables are 
a27f0 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66  initialized as f
a2800 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20  ollows:.    **. 
a2810 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74     **   flag_alt
a2820 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20  ernateform      
a2830 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 23      TRUE if a '#
a2840 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  ' is present..  
a2850 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66    **   flag_altf
a2860 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20 20  orm2            
a2870 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21 27     TRUE if a '!'
a2880 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20   is present..   
a2890 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73   **   flag_pluss
a28a0 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ign             
a28b0 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27 20    TRUE if a '+' 
a28c0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20  is present..    
a28d0 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75  **   flag_leftju
a28e0 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20 20  stify           
a28f0 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20 69   TRUE if a '-' i
a2900 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20  s present or if 
a2910 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  the.    **      
a2920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2930 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20 77           field w
a2940 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69 76  idth was negativ
a2950 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  e..    **   flag
a2960 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20 20  _zeropad        
a2970 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
a2980 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e 20  the width began 
a2990 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20  with 0..    **  
a29a0 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20   flag_long      
a29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55               TRU
a29c0 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 20  E if the letter 
a29d0 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78  'l' (ell) prefix
a29e0 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  ed.    **       
a29f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2a00 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76          the conv
a2a10 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72  ersion character
a2a20 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f  ..    **   flag_
a2a30 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20  longlong        
a2a40 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74         TRUE if t
a2a50 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28  he letter 'll' (
a2a60 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65  ell ell) prefixe
a2a70 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  d.    **        
a2a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2a90 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65         the conve
a2aa0 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e  rsion character.
a2ab0 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62  .    **   flag_b
a2ac0 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20  lanksign        
a2ad0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20        TRUE if a 
a2ae0 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a  ' ' is present..
a2af0 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20      **   width  
a2b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2b10 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69       The specifi
a2b20 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20  ed field width. 
a2b30 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20   This is.    ** 
a2b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6c                al
a2b60 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  ways non-negativ
a2b70 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20  e.  Zero is the 
a2b80 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20  default..    ** 
a2b90 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20    precision     
a2ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
a2bb0 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65 63  e specified prec
a2bc0 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61  ision.  The defa
a2bd0 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ult.    **      
a2be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2bf0 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a           is -1..
a2c00 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20      **   xtype  
a2c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2c20 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f       The class o
a2c30 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  f the conversion
a2c40 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70  ..    **   infop
a2c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2c60 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74         Pointer t
a2c70 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
a2c80 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20  e info struct.. 
a2c90 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68     */.    switch
a2ca0 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20  ( xtype ){.     
a2cb0 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a   case etPOINTER:
a2cc0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f  .        flag_lo
a2cd0 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28  nglong = sizeof(
a2ce0 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69  char*)==sizeof(i
a2cf0 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61  64);.        fla
a2d00 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28  g_long = sizeof(
a2d10 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c  char*)==sizeof(l
a2d20 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ong int);.      
a2d30 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
a2d40 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20  h into the next 
a2d50 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61  case */.      ca
a2d60 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20  se etORDINAL:.  
a2d70 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49 58      case etRADIX
a2d80 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e  :.        if( in
a2d90 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41  fop->flags & FLA
a2da0 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20  G_SIGNED ){.    
a2db0 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20        i64 v;.   
a2dc0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
a2dd0 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20  longlong ){.    
a2de0 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61          v = va_a
a2df0 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20  rg(ap,i64);.    
a2e00 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
a2e10 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20  flag_long ){.   
a2e20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f           v = va_
a2e30 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29  arg(ap,long int)
a2e40 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
a2e50 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76  e{.            v
a2e60 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
a2e70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
a2e80 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c 30           if( v<0
a2e90 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a2ea0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a  longvalue = -v;.
a2eb0 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66              pref
a2ec0 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20  ix = '-';.      
a2ed0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a2ee0 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65         longvalue
a2ef0 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20   = v;.          
a2f00 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73    if( flag_pluss
a2f10 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72 65  ign )        pre
a2f20 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20  fix = '+';.     
a2f30 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20         else if( 
a2f40 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29  flag_blanksign )
a2f50 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a    prefix = ' ';.
a2f60 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 65              else
a2f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2f80 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20         prefix = 
a2f90 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  0;.          }. 
a2fa0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a2fb0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
a2fc0 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20  _longlong ){.   
a2fd0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c           longval
a2fe0 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75  ue = va_arg(ap,u
a2ff0 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  64);.          }
a3000 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f  else if( flag_lo
a3010 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ng ){.          
a3020 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61    longvalue = va
a3030 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64  _arg(ap,unsigned
a3040 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20   long int);.    
a3050 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a3060 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c           longval
a3070 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75  ue = va_arg(ap,u
a3080 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20  nsigned int);.  
a3090 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a30a0 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a      prefix = 0;.
a30b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a30c0 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d    if( longvalue=
a30d0 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e  =0 ) flag_altern
a30e0 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20  ateform = 0;.   
a30f0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65       if( flag_ze
a3100 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73 69  ropad && precisi
a3110 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69 78  on<width-(prefix
a3120 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  !=0) ){.        
a3130 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 69    precision = wi
a3140 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 3b  dth-(prefix!=0);
a3150 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a3160 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66 5b     bufpt = &buf[
a3170 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20  etBUFSIZE-1];.  
a3180 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d        if( xtype=
a3190 3d 65 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20  =etORDINAL ){.  
a31a0 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63          static c
a31b0 6f 6e 73 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d  onst char zOrd[]
a31c0 20 3d 20 22 74 68 73 74 6e 64 72 64 22 3b 0a 20   = "thstndrd";. 
a31d0 20 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d           int x =
a31e0 20 28 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 65   (int)(longvalue
a31f0 20 25 20 31 30 29 3b 0a 20 20 20 20 20 20 20 20   % 10);.        
a3200 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 6c    if( x>=4 || (l
a3210 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30 3d  ongvalue/10)%10=
a3220 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =1 ){.          
a3230 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20    x = 0;.       
a3240 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
a3250 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d 20  uf[etBUFSIZE-3] 
a3260 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 20  = zOrd[x*2];.   
a3270 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 46         buf[etBUF
a3280 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78  SIZE-2] = zOrd[x
a3290 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 20  *2+1];.         
a32a0 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 20   bufpt -= 2;.   
a32b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7b       }.        {
a32c0 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73  .          regis
a32d0 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ter const char *
a32e0 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 73  cset;      /* Us
a32f0 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20  e registers for 
a3300 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20  speed */.       
a3310 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20     register int 
a3320 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  base;.          
a3330 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73 5b  cset = &aDigits[
a3340 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b  infop->charset];
a3350 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65 20  .          base 
a3360 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a 20  = infop->base;. 
a3370 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20 20           do{    
a3380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a33a0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72         /* Conver
a33b0 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 20  t to ascii */.  
a33c0 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75            *(--bu
a33d0 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e 67  fpt) = cset[long
a33e0 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 20  value%base];.   
a33f0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c           longval
a3400 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f 62  ue = longvalue/b
a3410 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ase;.          }
a3420 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 65  while( longvalue
a3430 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  >0 );.        }.
a3440 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a3450 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55   (int)(&buf[etBU
a3460 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b  FSIZE-1]-bufpt);
a3470 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78  .        for(idx
a3480 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74  =precision-lengt
a3490 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29  h; idx>0; idx--)
a34a0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d  {.          *(--
a34b0 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20 20  bufpt) = '0';   
a34c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a34d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 72            /* Zer
a34e0 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 20  o pad */.       
a34f0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70   }.        if( p
a3500 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 70  refix ) *(--bufp
a3510 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20 20  t) = prefix;    
a3520 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
a3530 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20  d sign */.      
a3540 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 72    if( flag_alter
a3550 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f  nateform && info
a3560 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20 20  p->prefix ){    
a3570 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 20    /* Add "0" or 
a3580 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 20  "0x" */.        
a3590 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72    const char *pr
a35a0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  e;.          cha
a35b0 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 70  r x;.          p
a35c0 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69 6e  re = &aPrefix[in
a35d0 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20  fop->prefix];.  
a35e0 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 28 78          for(; (x
a35f0 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70 72 65  =(*pre))!=0; pre
a3600 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d  ++) *(--bufpt) =
a3610 20 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   x;.        }.  
a3620 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28        length = (
a3630 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 53  int)(&buf[etBUFS
a3640 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20  IZE-1]-bufpt);. 
a3650 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a3660 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41 54      case etFLOAT
a3670 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 45  :.      case etE
a3680 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  XP:.      case e
a3690 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 20  tGENERIC:.      
a36a0 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 61    realvalue = va
a36b0 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b  _arg(ap,double);
a36c0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a36d0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
a36e0 49 4e 54 0a 20 20 20 20 20 20 20 20 69 66 28 20  INT.        if( 
a36f0 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72  precision<0 ) pr
a3700 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20  ecision = 6;    
a3710 20 20 20 20 20 2f 2a 20 53 65 74 20 64 65 66 61       /* Set defa
a3720 75 6c 74 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f  ult precision */
a3730 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65  .        if( pre
a3740 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45  cision>etBUFSIZE
a3750 2f 32 2d 31 30 20 29 20 70 72 65 63 69 73 69 6f  /2-10 ) precisio
a3760 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2f 32 2d  n = etBUFSIZE/2-
a3770 31 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  10;.        if( 
a3780 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29 7b  realvalue<0.0 ){
a3790 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c 76  .          realv
a37a0 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c 75  alue = -realvalu
a37b0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65  e;.          pre
a37c0 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20  fix = '-';.     
a37d0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a37e0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75      if( flag_plu
a37f0 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 20  ssign )         
a3800 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20   prefix = '+';. 
a3810 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66           else if
a3820 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e  ( flag_blanksign
a3830 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20 27   )    prefix = '
a3840 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c   ';.          el
a3850 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  se              
a3860 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69             prefi
a3870 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  x = 0;.        }
a3880 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
a3890 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 26 26  pe==etGENERIC &&
a38a0 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 20 70   precision>0 ) p
a38b0 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20  recision--;.#if 
a38c0 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75  0.        /* Rou
a38d0 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b 65  nding works like
a38e0 20 42 53 44 20 77 68 65 6e 20 74 68 65 20 63 6f   BSD when the co
a38f0 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 20 69 73  nstant 0.4999 is
a3900 20 75 73 65 64 2e 20 20 57 69 65 72 64 21 20 2a   used.  Wierd! *
a3910 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64  /.        for(id
a3920 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75  x=precision, rou
a3930 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 69 64 78  nder=0.4999; idx
a3940 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64  >0; idx--, round
a3950 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 0a  er*=0.1);.#else.
a3960 20 20 20 20 20 20 20 20 2f 2a 20 49 74 20 6d 61          /* It ma
a3970 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 65 20 74  kes more sense t
a3980 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20 20  o use 0.5 */.   
a3990 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65       for(idx=pre
a39a0 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d  cision, rounder=
a39b0 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d  0.5; idx>0; idx-
a39c0 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29  -, rounder*=0.1)
a39d0 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  {}.#endif.      
a39e0 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 46    if( xtype==etF
a39f0 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 6c 75 65  LOAT ) realvalue
a3a00 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20   += rounder;.   
a3a10 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a       /* Normaliz
a3a20 65 20 72 65 61 6c 76 61 6c 75 65 20 74 6f 20 77  e realvalue to w
a3a30 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 72 65 61  ithin 10.0 > rea
a3a40 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a 2f  lvalue >= 1.0 */
a3a50 0a 20 20 20 20 20 20 20 20 65 78 70 20 3d 20 30  .        exp = 0
a3a60 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  ;.        if( sq
a3a70 6c 69 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 62  lite3IsNaN((doub
a3a80 6c 65 29 72 65 61 6c 76 61 6c 75 65 29 20 29 7b  le)realvalue) ){
a3a90 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74  .          bufpt
a3aa0 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20   = "NaN";.      
a3ab0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a      length = 3;.
a3ac0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
a3ad0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a3ae0 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65     if( realvalue
a3af0 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  >0.0 ){.        
a3b00 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c    while( realval
a3b10 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70 3c  ue>=1e32 && exp<
a3b20 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75  =350 ){ realvalu
a3b30 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b  e *= 1e-32; exp+
a3b40 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  =32; }.         
a3b50 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75   while( realvalu
a3b60 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d 33  e>=1e8 && exp<=3
a3b70 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20  50 ){ realvalue 
a3b80 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b  *= 1e-8; exp+=8;
a3b90 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
a3ba0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31  le( realvalue>=1
a3bb0 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30 20  0.0 && exp<=350 
a3bc0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
a3bd0 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20  0.1; exp++; }.  
a3be0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
a3bf0 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29 7b  ealvalue<1e-8 ){
a3c00 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65   realvalue *= 1e
a3c10 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20  8; exp-=8; }.   
a3c20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65         while( re
a3c30 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20 72  alvalue<1.0 ){ r
a3c40 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30  ealvalue *= 10.0
a3c50 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20  ; exp--; }.     
a3c60 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 35 30       if( exp>350
a3c70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a3c80 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d 27 20  if( prefix=='-' 
a3c90 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
a3ca0 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 22 3b   bufpt = "-Inf";
a3cb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
a3cc0 73 65 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27  se if( prefix=='
a3cd0 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  +' ){.          
a3ce0 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b 49 6e      bufpt = "+In
a3cf0 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  f";.            
a3d00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a3d10 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 49 6e       bufpt = "In
a3d20 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  f";.            
a3d30 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65  }.            le
a3d40 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74  ngth = sqlite3St
a3d50 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20  rlen30(bufpt);. 
a3d60 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
a3d70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a3d80 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a3d90 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20  bufpt = buf;.   
a3da0 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 20       /*.        
a3db0 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c 64 20  ** If the field 
a3dc0 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 52 49  type is etGENERI
a3dd0 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20  C, then convert 
a3de0 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 50 0a  to either etEXP.
a3df0 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 65 74          ** or et
a3e00 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 6f 70  FLOAT, as approp
a3e10 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 20 2a  riate..        *
a3e20 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 65  /.        flag_e
a3e30 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 45 58  xp = xtype==etEX
a3e40 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78  P;.        if( x
a3e50 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b  type!=etFLOAT ){
a3e60 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c 76  .          realv
a3e70 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b  alue += rounder;
a3e80 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
a3e90 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29  ealvalue>=10.0 )
a3ea0 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30  { realvalue *= 0
a3eb0 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20  .1; exp++; }.   
a3ec0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a3ed0 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45  f( xtype==etGENE
a3ee0 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20  RIC ){.         
a3ef0 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61   flag_rtz = !fla
a3f00 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b  g_alternateform;
a3f10 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65  .          if( e
a3f20 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65  xp<-4 || exp>pre
a3f30 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  cision ){.      
a3f40 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 74        xtype = et
a3f50 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  EXP;.          }
a3f60 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a3f70 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72    precision = pr
a3f80 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20  ecision - exp;. 
a3f90 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65             xtype
a3fa0 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20   = etFLOAT;.    
a3fb0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a3fc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a3fd0 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20   flag_rtz = 0;. 
a3fe0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a3ff0 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 45 58   if( xtype==etEX
a4000 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65  P ){.          e
a4010 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  2 = 0;.        }
a4020 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a4030 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 20 20  e2 = exp;.      
a4040 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 64 20    }.        nsd 
a4050 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6c 61  = 0;.        fla
a4060 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 69 6f  g_dp = (precisio
a4070 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61 67  n>0 ?1:0) | flag
a4080 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c  _alternateform |
a4090 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a   flag_altform2;.
a40a0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
a40b0 69 67 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20  ign in front of 
a40c0 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  the number */.  
a40d0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78        if( prefix
a40e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28   ){.          *(
a40f0 62 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69  bufpt++) = prefi
a4100 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  x;.        }.   
a4110 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20 70       /* Digits p
a4120 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63 69  rior to the deci
a4130 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20  mal point */.   
a4140 20 20 20 20 20 69 66 28 20 65 32 3c 30 20 29 7b       if( e2<0 ){
a4150 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
a4160 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20  pt++) = '0';.   
a4170 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a4180 20 20 20 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d        for(; e2>=
a4190 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20  0; e2--){.      
a41a0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
a41b0 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74 28 26   = et_getdigit(&
a41c0 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b  realvalue,&nsd);
a41d0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
a41e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f       }.        /
a41f0 2a 20 54 68 65 20 64 65 63 69 6d 61 6c 20 70 6f  * The decimal po
a4200 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  int */.        i
a4210 66 28 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20  f( flag_dp ){.  
a4220 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a4230 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20  +) = '.';.      
a4240 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 22    }.        /* "
a4250 30 22 20 64 69 67 69 74 73 20 61 66 74 65 72 20  0" digits after 
a4260 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e  the decimal poin
a4270 74 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 65  t but before the
a4280 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a   first.        *
a4290 2a 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69  * significant di
a42a0 67 69 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  git of the numbe
a42b0 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72  r */.        for
a42c0 28 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65  (e2++; e2<0; pre
a42d0 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b  cision--, e2++){
a42e0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
a42f0 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29  t( precision>0 )
a4300 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  ;.          *(bu
a4310 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20  fpt++) = '0';.  
a4320 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4330 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e 74 20 64  /* Significant d
a4340 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20  igits after the 
a4350 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f  decimal point */
a4360 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
a4370 28 70 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20  (precision--)>0 
a4380 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  ){.          *(b
a4390 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74  ufpt++) = et_get
a43a0 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65  digit(&realvalue
a43b0 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20  ,&nsd);.        
a43c0 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d  }.        /* Rem
a43d0 6f 76 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 72  ove trailing zer
a43e0 6f 73 20 61 6e 64 20 74 68 65 20 22 2e 22 20 69  os and the "." i
a43f0 66 20 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c  f no digits foll
a4400 6f 77 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20  ow the "." */.  
a4410 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 72        if( flag_r
a4420 74 7a 20 26 26 20 66 6c 61 67 5f 64 70 20 29 7b  tz && flag_dp ){
a4430 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
a4440 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27  ( bufpt[-1]=='0'
a4450 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20   ) *(--bufpt) = 
a4460 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  0;.          ass
a4470 65 72 74 28 20 62 75 66 70 74 3e 62 75 66 20 29  ert( bufpt>buf )
a4480 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
a4490 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29  bufpt[-1]=='.' )
a44a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
a44b0 28 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20  ( flag_altform2 
a44c0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
a44d0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30   *(bufpt++) = '0
a44e0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ';.            }
a44f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a4500 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d      *(--bufpt) =
a4510 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   0;.            
a4520 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
a4530 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4540 2f 2a 20 41 64 64 20 74 68 65 20 22 65 4e 4e 4e  /* Add the "eNNN
a4550 22 20 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20  " suffix */.    
a4560 20 20 20 20 69 66 28 20 66 6c 61 67 5f 65 78 70      if( flag_exp
a4570 20 7c 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 50   || xtype==etEXP
a4580 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28   ){.          *(
a4590 62 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69  bufpt++) = aDigi
a45a0 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65  ts[infop->charse
a45b0 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  t];.          if
a45c0 28 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20  ( exp<0 ){.     
a45d0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a45e0 29 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d  ) = '-'; exp = -
a45f0 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  exp;.          }
a4600 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a4610 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
a4620 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  +';.          }.
a4630 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78            if( ex
a4640 70 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20  p>=100 ){.      
a4650 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
a4660 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f 31   = (char)((exp/1
a4670 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20 20  00)+'0');       
a4680 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74 20   /* 100's digit 
a4690 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 65  */.            e
a46a0 78 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20  xp %= 100;.     
a46b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
a46c0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63   *(bufpt++) = (c
a46d0 68 61 72 29 28 65 78 70 2f 31 30 2b 27 30 27 29  har)(exp/10+'0')
a46e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
a46f0 20 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20   10's digit */. 
a4700 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a4710 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78 70  ++) = (char)(exp
a4720 25 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20  %10+'0');       
a4730 20 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67        /* 1's dig
a4740 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a  it */.        }.
a4750 20 20 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d          *bufpt =
a4760 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20   0;..        /* 
a4770 54 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75  The converted nu
a4780 6d 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d  mber is in buf[]
a4790 20 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e   and zero termin
a47a0 61 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e  ated. Output it.
a47b0 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65  .        ** Note
a47c0 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72   that the number
a47d0 20 69 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c   is in the usual
a47e0 20 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65   order, not reve
a47f0 72 73 65 64 20 61 73 20 77 69 74 68 0a 20 20 20  rsed as with.   
a4800 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20       ** integer 
a4810 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a  conversions. */.
a4820 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a4830 20 28 69 6e 74 29 28 62 75 66 70 74 2d 62 75 66   (int)(bufpt-buf
a4840 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74  );.        bufpt
a4850 20 3d 20 62 75 66 3b 0a 0a 20 20 20 20 20 20 20   = buf;..       
a4860 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65   /* Special case
a4870 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20 7a  :  Add leading z
a4880 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61 67  eros if the flag
a4890 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69 73  _zeropad flag is
a48a0 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20  .        ** set 
a48b0 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 6c  and we are not l
a48c0 65 66 74 20 6a 75 73 74 69 66 69 65 64 20 2a 2f  eft justified */
a48d0 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61  .        if( fla
a48e0 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 21 66 6c  g_zeropad && !fl
a48f0 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 26  ag_leftjustify &
a4900 26 20 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 68  & length < width
a4910 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
a4920 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   i;.          in
a4930 74 20 6e 50 61 64 20 3d 20 77 69 64 74 68 20 2d  t nPad = width -
a4940 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 20   length;.       
a4950 20 20 20 66 6f 72 28 69 3d 77 69 64 74 68 3b 20     for(i=width; 
a4960 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20  i>=nPad; i--){. 
a4970 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74             bufpt
a4980 5b 69 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50  [i] = bufpt[i-nP
a4990 61 64 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ad];.          }
a49a0 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20 70  .          i = p
a49b0 72 65 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 20  refix!=0;.      
a49c0 20 20 20 20 77 68 69 6c 65 28 20 6e 50 61 64 2d      while( nPad-
a49d0 2d 20 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d  - ) bufpt[i++] =
a49e0 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '0';.          
a49f0 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 3b 0a  length = width;.
a4a00 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
a4a10 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
a4a20 20 20 20 20 20 20 63 61 73 65 20 65 74 53 49 5a        case etSIZ
a4a30 45 3a 0a 20 20 20 20 20 20 20 20 2a 28 76 61 5f  E:.        *(va_
a4a40 61 72 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20  arg(ap,int*)) = 
a4a50 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20  pAccum->nChar;. 
a4a60 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a4a70 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20  width = 0;.     
a4a80 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a4a90 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a 0a  case etPERCENT:.
a4aa0 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d          buf[0] =
a4ab0 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62 75   '%';.        bu
a4ac0 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20  fpt = buf;.     
a4ad0 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20     length = 1;. 
a4ae0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a4af0 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52 58      case etCHARX
a4b00 3a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 76 61  :.        c = va
a4b10 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20  _arg(ap,int);.  
a4b20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 28        buf[0] = (
a4b30 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 20  char)c;.        
a4b40 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30  if( precision>=0
a4b50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
a4b60 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72 65  r(idx=1; idx<pre
a4b70 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 62  cision; idx++) b
a4b80 75 66 5b 69 64 78 5d 20 3d 20 28 63 68 61 72 29  uf[idx] = (char)
a4b90 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  c;.          len
a4ba0 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b  gth = precision;
a4bb0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
a4bc0 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
a4bd0 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   =1;.        }. 
a4be0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
a4bf0 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  uf;.        brea
a4c00 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  k;.      case et
a4c10 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61  STRING:.      ca
a4c20 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a  se etDYNSTRING:.
a4c30 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
a4c40 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29  va_arg(ap,char*)
a4c50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75  ;.        if( bu
a4c60 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  fpt==0 ){.      
a4c70 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a      bufpt = "";.
a4c80 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
a4c90 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54  ( xtype==etDYNST
a4ca0 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20  RING ){.        
a4cb0 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74    zExtra = bufpt
a4cc0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a4cd0 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
a4ce0 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n>=0 ){.        
a4cf0 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20    for(length=0; 
a4d00 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e  length<precision
a4d10 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68   && bufpt[length
a4d20 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20  ]; length++){}. 
a4d30 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a4d40 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a4d50 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
a4d60 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20  (bufpt);.       
a4d70 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
a4d80 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53  ;.      case etS
a4d90 51 4c 45 53 43 41 50 45 3a 0a 20 20 20 20 20 20  QLESCAPE:.      
a4da0 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45  case etSQLESCAPE
a4db0 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  2:.      case et
a4dc0 53 51 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20 20  SQLESCAPE3: {.  
a4dd0 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20        int i, j, 
a4de0 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 20  n, isnull;.     
a4df0 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74 65     int needQuote
a4e00 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63  ;.        char c
a4e10 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  h;.        char 
a4e20 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74 53  q = ((xtype==etS
a4e30 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a 27  QLESCAPE3)?'"':'
a4e40 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74 65  \'');   /* Quote
a4e50 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20   character */.  
a4e60 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63 61        char *esca
a4e70 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 63  rg = va_arg(ap,c
a4e80 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 69  har*);.        i
a4e90 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d 3d  snull = escarg==
a4ea0 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  0;.        if( i
a4eb0 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20 3d  snull ) escarg =
a4ec0 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53   (xtype==etSQLES
a4ed0 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20 3a  CAPE2 ? "NULL" :
a4ee0 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 20   "(NULL)");.    
a4ef0 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28      for(i=n=0; (
a4f00 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30  ch=escarg[i])!=0
a4f10 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
a4f20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 6e    if( ch==q )  n
a4f30 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ++;.        }.  
a4f40 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20        needQuote 
a4f50 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 79  = !isnull && xty
a4f60 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32  pe==etSQLESCAPE2
a4f70 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 69  ;.        n += i
a4f80 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 65   + 1 + needQuote
a4f90 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  *2;.        if( 
a4fa0 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a 20  n>etBUFSIZE ){. 
a4fb0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d           bufpt =
a4fc0 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65   zExtra = sqlite
a4fd0 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20  3Malloc( n );.  
a4fe0 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70          if( bufp
a4ff0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
a5000 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c      pAccum->mall
a5010 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
a5020 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
a5030 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a5040 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a5050 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
a5060 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  uf;.        }.  
a5070 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20        j = 0;.   
a5080 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f       if( needQuo
a5090 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20  te ) bufpt[j++] 
a50a0 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  = q;.        for
a50b0 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67  (i=0; (ch=escarg
a50c0 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20  [i])!=0; i++){. 
a50d0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a           bufpt[j
a50e0 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20  ++] = ch;.      
a50f0 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20      if( ch==q ) 
a5100 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b  bufpt[j++] = ch;
a5110 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a5120 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65     if( needQuote
a5130 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20   ) bufpt[j++] = 
a5140 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74  q;.        bufpt
a5150 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  [j] = 0;.       
a5160 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20   length = j;.   
a5170 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 63       /* The prec
a5180 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65 64  ision is ignored
a5190 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a 2f   on %q and %Q */
a51a0 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28 20  .        /* if( 
a51b0 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20  precision>=0 && 
a51c0 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68  precision<length
a51d0 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63   ) length = prec
a51e0 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 20  ision; */.      
a51f0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
a5200 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54 4f  .      case etTO
a5210 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 54  KEN: {.        T
a5220 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 76  oken *pToken = v
a5230 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e 2a  a_arg(ap, Token*
a5240 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
a5250 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20  Token ){.       
a5260 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a5270 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
a5280 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54   (const char*)pT
a5290 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d  oken->z, pToken-
a52a0 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  >n);.        }. 
a52b0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a52c0 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20  width = 0;.     
a52d0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a52e0 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53  }.      case etS
a52f0 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20 20  RCLIST: {.      
a5300 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20    SrcList *pSrc 
a5310 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72 63  = va_arg(ap, Src
a5320 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20 20  List*);.        
a5330 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28 61  int k = va_arg(a
a5340 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  p, int);.       
a5350 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
a5360 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
a5370 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20  Src->a[k];.     
a5380 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30 20     assert( k>=0 
a5390 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 20  && k<pSrc->nSrc 
a53a0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
a53b0 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20  Item->zDatabase 
a53c0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
a53d0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a53e0 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d  nd(pAccum, pItem
a53f0 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31 29  ->zDatabase, -1)
a5400 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
a5410 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a5420 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20 31  d(pAccum, ".", 1
a5430 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
a5440 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
a5450 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
a5460 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c  m, pItem->zName,
a5470 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c 65   -1);.        le
a5480 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30  ngth = width = 0
a5490 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
a54a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
a54b0 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
a54c0 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65 3d    assert( xtype=
a54d0 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20  =etINVALID );.  
a54e0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
a54f0 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e      }.    }/* En
a5500 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 68  d switch over th
a5510 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f  e format type */
a5520 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54  .    /*.    ** T
a5530 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63  he text of the c
a5540 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69  onversion is poi
a5550 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 70  nted to by "bufp
a5560 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a  t" and is.    **
a5570 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63   "length" charac
a5580 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20  ters long.  The 
a5590 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 22  field width is "
a55a0 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20  width".  Do.    
a55b0 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20  ** the output.. 
a55c0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66     */.    if( !f
a55d0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
a55e0 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65  ){.      registe
a55f0 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20  r int nspace;.  
a5600 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64      nspace = wid
a5610 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20  th-length;.     
a5620 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b   if( nspace>0 ){
a5630 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53  .        appendS
a5640 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70  pace(pAccum, nsp
a5650 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ace);.      }.  
a5660 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67    }.    if( leng
a5670 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  th>0 ){.      sq
a5680 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
a5690 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70  end(pAccum, bufp
a56a0 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20  t, length);.    
a56b0 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c  }.    if( flag_l
a56c0 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20  eftjustify ){.  
a56d0 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74      register int
a56e0 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e   nspace;.      n
a56f0 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65  space = width-le
a5700 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20  ngth;.      if( 
a5710 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20  nspace>0 ){.    
a5720 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28      appendSpace(
a5730 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b  pAccum, nspace);
a5740 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
a5750 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b     if( zExtra ){
a5760 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
a5770 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20  ree(zExtra);.   
a5780 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72   }.  }/* End for
a5790 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66   loop over the f
a57a0 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a  ormat string */.
a57b0 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63  } /* End of func
a57c0 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  tion */../*.** A
a57d0 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66  ppend N bytes of
a57e0 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20   text from z to 
a57f0 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a  the StrAccum obj
a5800 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ect..*/.SQLITE_P
a5810 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a5820 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a5830 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63  d(StrAccum *p, c
a5840 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
a5850 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28 20  t N){.  assert( 
a5860 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0a  z!=0 || N==0 );.
a5870 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67 20    if( p->tooBig 
a5880 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  | p->mallocFaile
a5890 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73  d ){.    testcas
a58a0 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 20  e(p->tooBig);.  
a58b0 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d 61    testcase(p->ma
a58c0 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 20  llocFailed);.   
a58d0 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
a58e0 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20  f( N<0 ){.    N 
a58f0 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
a5900 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  0(z);.  }.  if( 
a5910 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a 3d  N==0 || NEVER(z=
a5920 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  =0) ){.    retur
a5930 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  n;.  }.  if( p->
a5940 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e 41  nChar+N >= p->nA
a5950 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 72  lloc ){.    char
a5960 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28 20   *zNew;.    if( 
a5970 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 7b  !p->useMalloc ){
a5980 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67  .      p->tooBig
a5990 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d 20   = 1;.      N = 
a59a0 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e 6e  p->nAlloc - p->n
a59b0 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20 20  Char - 1;.      
a59c0 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 20  if( N<=0 ){.    
a59d0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
a59e0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
a59f0 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20 3d       i64 szNew =
a5a00 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20   p->nChar;.     
a5a10 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 3b   szNew += N + 1;
a5a20 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65 77  .      if( szNew
a5a30 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b   > p->mxAlloc ){
a5a40 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
a5a50 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 29  StrAccumReset(p)
a5a60 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f 6f  ;.        p->too
a5a70 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Big = 1;.       
a5a80 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
a5a90 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d  else{.        p-
a5aa0 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 73  >nAlloc = (int)s
a5ab0 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20  zNew;.      }.  
a5ac0 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74      zNew = sqlit
a5ad0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d  e3DbMallocRaw(p-
a5ae0 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29  >db, p->nAlloc )
a5af0 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 77  ;.      if( zNew
a5b00 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63   ){.        memc
a5b10 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78  py(zNew, p->zTex
a5b20 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20  t, p->nChar);.  
a5b30 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
a5b40 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20  AccumReset(p);. 
a5b50 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20         p->zText 
a5b60 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65  = zNew;.      }e
a5b70 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  lse{.        p->
a5b80 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
a5b90 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
a5ba0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70  3StrAccumReset(p
a5bb0 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
a5bc0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
a5bd0 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70  .  }.  memcpy(&p
a5be0 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72  ->zText[p->nChar
a5bf0 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e  ], z, N);.  p->n
a5c00 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a  Char += N;.}../*
a5c10 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 61  .** Finish off a
a5c20 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e   string by makin
a5c30 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 72  g sure it is zer
a5c40 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a  o-terminated..**
a5c50 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
a5c60 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 69  r to the resulti
a5c70 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75  ng string.  Retu
a5c80 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69  rn a NULL.** poi
a5c90 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e 64  nter if any kind
a5ca0 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 6e   of error was en
a5cb0 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51  countered..*/.SQ
a5cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
a5cd0 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63  r *sqlite3StrAcc
a5ce0 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75  umFinish(StrAccu
a5cf0 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  m *p){.  if( p->
a5d00 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e  zText ){.    p->
a5d10 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20  zText[p->nChar] 
a5d20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  = 0;.    if( p->
a5d30 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e  useMalloc && p->
a5d40 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20  zText==p->zBase 
a5d50 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 78  ){.      p->zTex
a5d60 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
a5d70 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d  locRaw(p->db, p-
a5d80 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 20  >nChar+1 );.    
a5d90 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 29    if( p->zText )
a5da0 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
a5db0 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42  (p->zText, p->zB
a5dc0 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29  ase, p->nChar+1)
a5dd0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
a5de0 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63         p->malloc
a5df0 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
a5e00 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
a5e10 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b  return p->zText;
a5e20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20  .}../*.** Reset 
a5e30 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 69  an StrAccum stri
a5e40 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c  ng.  Reclaim all
a5e50 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79   malloced memory
a5e60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a5e70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
a5e80 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 74  StrAccumReset(St
a5e90 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66  rAccum *p){.  if
a5ea0 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a  ( p->zText!=p->z
a5eb0 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Base ){.    sqli
a5ec0 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c  te3DbFree(p->db,
a5ed0 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a   p->zText);.  }.
a5ee0 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a    p->zText = 0;.
a5ef0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
a5f00 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 63  ize a string acc
a5f10 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49  umulator.*/.SQLI
a5f20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a5f30 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49  sqlite3StrAccumI
a5f40 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 2c  nit(StrAccum *p,
a5f50 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e   char *zBase, in
a5f60 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20  t n, int mx){.  
a5f70 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42  p->zText = p->zB
a5f80 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70  ase = zBase;.  p
a5f90 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e  ->db = 0;.  p->n
a5fa0 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e  Char = 0;.  p->n
a5fb0 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e  Alloc = n;.  p->
a5fc0 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20  mxAlloc = mx;.  
a5fd0 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31  p->useMalloc = 1
a5fe0 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20  ;.  p->tooBig = 
a5ff0 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61  0;.  p->mallocFa
a6000 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  iled = 0;.}../*.
a6010 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65  ** Print into me
a6020 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
a6030 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
a6040 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 65  ).  Use the inte
a6050 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72  rnal.** %-conver
a6060 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e  sion extensions.
a6070 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a6080 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
a6090 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33  VMPrintf(sqlite3
a60a0 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
a60b0 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69   *zFormat, va_li
a60c0 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a  st ap){.  char *
a60d0 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b  z;.  char zBase[
a60e0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
a60f0 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63  _SIZE];.  StrAcc
a6100 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72 74  um acc;.  assert
a6110 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71 6c  ( db!=0 );.  sql
a6120 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
a6130 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 69  (&acc, zBase, si
a6140 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20 20  zeof(zBase),.   
a6150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6160 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51     db->aLimit[SQ
a6170 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
a6180 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d 20  H]);.  acc.db = 
a6190 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50  db;.  sqlite3VXP
a61a0 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20 7a  rintf(&acc, 1, z
a61b0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a  Format, ap);.  z
a61c0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63   = sqlite3StrAcc
a61d0 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a  umFinish(&acc);.
a61e0 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f 63    if( acc.malloc
a61f0 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64 62  Failed ){.    db
a6200 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
a6210 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
a6220 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69   z;.}../*.** Pri
a6230 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  nt into memory o
a6240 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
a6250 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 73  iteMalloc().  Us
a6260 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a  e the internal.*
a6270 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65  * %-conversion e
a6280 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  xtensions..*/.SQ
a6290 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
a62a0 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74  r *sqlite3MPrint
a62b0 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  f(sqlite3 *db, c
a62c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a62d0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
a62e0 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a  ist ap;.  char *
a62f0 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  z;.  va_start(ap
a6300 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20  , zFormat);.  z 
a6310 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
a6320 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  f(db, zFormat, a
a6330 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
a6340 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a  ;.  return z;.}.
a6350 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c 69  ./*.** Like sqli
a6360 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62 75  te3MPrintf(), bu
a6370 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62  t call sqlite3Db
a6380 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20 61  Free() on zStr a
a6390 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67 0a  fter formatting.
a63a0 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61 6e  ** the string an
a63b0 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 6e  d before returnn
a63c0 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ing.  This routi
a63d0 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  ne is intended t
a63e0 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20  o be used.** to 
a63f0 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74 69  modify an existi
a6400 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20  ng string.  For 
a6410 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  example:.**.**  
a6420 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33       x = sqlite3
a6430 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20 22  MPrintf(db, x, "
a6440 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69 78  prefix %s suffix
a6450 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c  ", x);.**.*/.SQL
a6460 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
a6470 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64   *sqlite3MAppend
a6480 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  f(sqlite3 *db, c
a6490 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 74  har *zStr, const
a64a0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
a64b0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
a64c0 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  ap;.  char *z;. 
a64d0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
a64e0 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71  ormat);.  z = sq
a64f0 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62  lite3VMPrintf(db
a6500 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a6510 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
a6520 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
a6530 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75 72  , zStr);.  retur
a6540 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  n z;.}../*.** Pr
a6550 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  int into memory 
a6560 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
a6570 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20  lite3_malloc(). 
a6580 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e   Omit the intern
a6590 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69  al.** %-conversi
a65a0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a  on extensions..*
a65b0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  /.SQLITE_API cha
a65c0 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 69  r *sqlite3_vmpri
a65d0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ntf(const char *
a65e0 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74  zFormat, va_list
a65f0 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b   ap){.  char *z;
a6600 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51  .  char zBase[SQ
a6610 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53  LITE_PRINT_BUF_S
a6620 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d  IZE];.  StrAccum
a6630 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53 51   acc;.#ifndef SQ
a6640 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
a6650 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  IT.  if( sqlite3
a6660 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20  _initialize() ) 
a6670 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
a6680 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
a6690 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 61  umInit(&acc, zBa
a66a0 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 65  se, sizeof(zBase
a66b0 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  ), SQLITE_MAX_LE
a66c0 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65 33  NGTH);.  sqlite3
a66d0 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30  VXPrintf(&acc, 0
a66e0 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a66f0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72    z = sqlite3Str
a6700 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63  AccumFinish(&acc
a6710 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
a6720 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e  ../*.** Print in
a6730 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
a6740 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
a6750 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d 69  malloc()().  Omi
a6760 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a  t the internal.*
a6770 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65  * %-conversion e
a6780 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  xtensions..*/.SQ
a6790 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
a67a0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63  qlite3_mprintf(c
a67b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a67c0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
a67d0 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a  ist ap;.  char *
a67e0 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  z;.#ifndef SQLIT
a67f0 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
a6800 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
a6810 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74  itialize() ) ret
a6820 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 0;.#endif.  
a6830 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
a6840 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c  rmat);.  z = sql
a6850 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a 46  ite3_vmprintf(zF
a6860 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
a6870 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75  _end(ap);.  retu
a6880 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73  rn z;.}../*.** s
a6890 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
a68a0 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e 70  ) works like snp
a68b0 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20 74  rintf() except t
a68c0 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20 74  hat it ignores t
a68d0 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f  he.** current lo
a68e0 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20 20  cale settings.  
a68f0 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e  This is importan
a6900 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65 63  t for SQLite bec
a6910 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20 6e  ause we.** are n
a6920 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20 61  ot able to use a
a6930 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69   "," as the deci
a6940 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c 61  mal point in pla
a6950 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a 20  ce of "." as.** 
a6960 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f 6d  specified by som
a6970 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51  e locales..*/.SQ
a6980 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
a6990 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
a69a0 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 75  int n, char *zBu
a69b0 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  f, const char *z
a69c0 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
a69d0 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69  char *z;.  va_li
a69e0 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 75  st ap;.  StrAccu
a69f0 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e 3c  m acc;..  if( n<
a6a00 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
a6a10 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71 6c   zBuf;.  }.  sql
a6a20 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
a6a30 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c 20  (&acc, zBuf, n, 
a6a40 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c  0);.  acc.useMal
a6a50 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74  loc = 0;.  va_st
a6a60 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b  art(ap,zFormat);
a6a70 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  .  sqlite3VXPrin
a6a80 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72  tf(&acc, 0, zFor
a6a90 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
a6aa0 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71  nd(ap);.  z = sq
a6ab0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
a6ac0 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74  ish(&acc);.  ret
a6ad0 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65  urn z;.}..#if de
a6ae0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
a6af0 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 73  UG)./*.** A vers
a6b00 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29 20  ion of printf() 
a6b10 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64 73  that understands
a6b20 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f 72   %lld.  Used for
a6b30 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 54   debugging..** T
a6b40 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69 6c  he printf() buil
a6b50 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72 73  t into some vers
a6b60 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20  ions of windows 
a6b70 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73 74  does not underst
a6b80 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20  and %lld.** and 
a6b90 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f 75  segfaults if you
a6ba0 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67 20   give it a long 
a6bb0 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c  long int..*/.SQL
a6bc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a6bd0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
a6be0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ntf(const char *
a6bf0 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
a6c00 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53   va_list ap;.  S
a6c10 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 63  trAccum acc;.  c
a6c20 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20  har zBuf[500];. 
a6c30 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a6c40 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66 2c  Init(&acc, zBuf,
a6c50 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 30   sizeof(zBuf), 0
a6c60 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c  );.  acc.useMall
a6c70 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61  oc = 0;.  va_sta
a6c80 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a  rt(ap,zFormat);.
a6c90 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74    sqlite3VXPrint
a6ca0 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d  f(&acc, 0, zForm
a6cb0 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
a6cc0 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  d(ap);.  sqlite3
a6cd0 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
a6ce0 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  acc);.  fprintf(
a6cf0 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42 75  stdout,"%s", zBu
a6d00 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 64  f);.  fflush(std
a6d10 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  out);.}.#endif..
a6d20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a6d30 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63 20  End of printf.c 
a6d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a6d70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a6d80 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64 6f  Begin file rando
a6d90 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m.c ************
a6da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a6dc0 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
a6dd0 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
a6de0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
a6df0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
a6e00 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
a6e10 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
a6e20 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
a6e30 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
a6e40 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
a6e50 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
a6e60 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
a6e70 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
a6e80 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
a6e90 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
a6ea0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
a6eb0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
a6ec0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
a6ed0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
a6ee0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
a6ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6f30 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
a6f40 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
a6f50 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  e to implement a
a6f60 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e   pseudo-random n
a6f70 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74  umber.** generat
a6f80 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53 51  or (PRNG) for SQ
a6f90 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64  Lite..**.** Rand
a6fa0 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75  om numbers are u
a6fb0 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20 74  sed by some of t
a6fc0 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 6b  he database back
a6fd0 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a  ends in order.**
a6fe0 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61 6e   to generate ran
a6ff0 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79 73  dom integer keys
a7000 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20 72   for tables or r
a7010 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 2e  andom filenames.
a7020 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68 72  .*/.../* All thr
a7030 65 61 64 73 20 73 68 61 72 65 20 61 20 73 69 6e  eads share a sin
a7040 67 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  gle random numbe
a7050 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20  r generator..** 
a7060 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  This structure i
a7070 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74  s the current st
a7080 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72  ate of the gener
a7090 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ator..*/.static 
a70a0 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63  SQLITE_WSD struc
a70b0 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70  t sqlite3PrngTyp
a70c0 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  e {.  unsigned c
a70d0 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20 20 20  har isInit;     
a70e0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a70f0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
a7100 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69   unsigned char i
a7110 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  , j;            
a7120 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 6c  /* State variabl
a7130 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  es */.  unsigned
a7140 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20 20   char s[256];   
a7150 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20         /* State 
a7160 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d 20 73  variables */.} s
a7170 71 6c 69 74 65 33 50 72 6e 67 3b 0a 0a 2f 2a 0a  qlite3Prng;../*.
a7180 2a 2a 20 47 65 74 20 61 20 73 69 6e 67 6c 65 20  ** Get a single 
a7190 38 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c  8-bit random val
a71a0 75 65 20 66 72 6f 6d 20 74 68 65 20 52 43 34 20  ue from the RC4 
a71b0 50 52 4e 47 2e 20 20 54 68 65 20 4d 75 74 65 78  PRNG.  The Mutex
a71c0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 68 65 6c 64  .** must be held
a71d0 20 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e 67   while executing
a71e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
a71f0 2a 0a 2a 2a 20 57 68 79 20 6e 6f 74 20 6a 75 73  *.** Why not jus
a7200 74 20 75 73 65 20 61 20 6c 69 62 72 61 72 79 20  t use a library 
a7210 72 61 6e 64 6f 6d 20 67 65 6e 65 72 61 74 6f 72  random generator
a7220 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 20   like lrand48() 
a7230 66 6f 72 20 74 68 69 73 3f 0a 2a 2a 20 42 65 63  for this?.** Bec
a7240 61 75 73 65 20 74 68 65 20 4f 50 5f 4e 65 77 52  ause the OP_NewR
a7250 6f 77 69 64 20 6f 70 63 6f 64 65 20 69 6e 20 74  owid opcode in t
a7260 68 65 20 56 44 42 45 20 64 65 70 65 6e 64 73 20  he VDBE depends 
a7270 6f 6e 20 68 61 76 69 6e 67 20 61 20 76 65 72 79  on having a very
a7280 0a 2a 2a 20 67 6f 6f 64 20 73 6f 75 72 63 65 20  .** good source 
a7290 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  of random number
a72a0 73 2e 20 20 54 68 65 20 6c 72 61 6e 64 34 38 28  s.  The lrand48(
a72b0 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69  ) library functi
a72c0 6f 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 62  on may.** well b
a72d0 65 20 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20  e good enough.  
a72e0 42 75 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20  But maybe not.  
a72f0 4f 72 20 6d 61 79 62 65 20 6c 72 61 6e 64 34 38  Or maybe lrand48
a7300 28 29 20 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 73  () has some.** s
a7310 75 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f  ubtle problems o
a7320 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74  n some systems t
a7330 68 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20  hat could cause 
a7340 70 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69 73  problems.  It is
a7350 20 68 61 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f 77   hard.** to know
a7360 2e 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74  .  To minimize t
a7370 68 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c  he risk of probl
a7380 65 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20 6c  ems due to bad l
a7390 72 61 6e 64 34 38 28 29 0a 2a 2a 20 69 6d 70 6c  rand48().** impl
a73a0 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51 4c  ementations, SQL
a73b0 69 74 65 20 75 73 65 73 20 74 68 69 73 20 72 61  ite uses this ra
a73c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
a73d0 72 61 74 6f 72 20 62 61 73 65 64 0a 2a 2a 20 6f  rator based.** o
a73e0 6e 20 52 43 34 2c 20 77 68 69 63 68 20 77 65 20  n RC4, which we 
a73f0 6b 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72 79 20  know works very 
a7400 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 74  well..**.** (Lat
a7410 65 72 29 3a 20 20 41 63 74 75 61 6c 6c 79 2c 20  er):  Actually, 
a7420 4f 50 5f 4e 65 77 52 6f 77 69 64 20 64 6f 65 73  OP_NewRowid does
a7430 20 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20 61   not depend on a
a7440 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 0a   good source of.
a7450 2a 2a 20 72 61 6e 64 6f 6d 6e 65 73 73 20 61 6e  ** randomness an
a7460 79 20 6d 6f 72 65 2e 20 20 42 75 74 20 77 65 20  y more.  But we 
a7470 77 69 6c 6c 20 6c 65 61 76 65 20 74 68 69 73 20  will leave this 
a7480 63 6f 64 65 20 69 6e 20 61 6c 6c 20 74 68 65 20  code in all the 
a7490 73 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  same..*/.static 
a74a0 75 38 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 6f  u8 randomByte(vo
a74b0 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  id){.  unsigned 
a74c0 63 68 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54  char t;...  /* T
a74d0 68 65 20 22 77 73 64 50 72 6e 67 22 20 6d 61 63  he "wsdPrng" mac
a74e0 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20  ro will resolve 
a74f0 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61  to the pseudo-ra
a7500 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
a7510 72 61 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65  rator.  ** state
a7520 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69   vector.  If wri
a7530 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74  table static dat
a7540 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64  a is unsupported
a7550 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a   on the target,.
a7560 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20    ** we have to 
a7570 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65  locate the state
a7580 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74   vector at run-t
a7590 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72  ime.  In the mor
a75a0 65 20 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61  e common.  ** ca
a75b0 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c  se where writabl
a75c0 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
a75d0 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50   supported, wsdP
a75e0 72 6e 67 20 63 61 6e 20 72 65 66 65 72 20 64 69  rng can refer di
a75f0 72 65 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 74  rectly.  ** to t
a7600 68 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22  he "sqlite3Prng"
a7610 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65   state vector de
a7620 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 20  clared above..  
a7630 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
a7640 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 75  _OMIT_WSD.  stru
a7650 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
a7660 70 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c 28  pe *p = &GLOBAL(
a7670 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
a7680 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50  ngType, sqlite3P
a7690 72 6e 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 77  rng);.# define w
a76a0 73 64 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c 73  sdPrng p[0].#els
a76b0 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72  e.# define wsdPr
a76c0 6e 67 20 73 71 6c 69 74 65 33 50 72 6e 67 0a 23  ng sqlite3Prng.#
a76d0 65 6e 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69  endif...  /* Ini
a76e0 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74  tialize the stat
a76f0 65 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20  e of the random 
a7700 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
a7710 20 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20   once,.  ** the 
a7720 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20  first time this 
a7730 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
a7740 64 2e 20 20 54 68 65 20 73 65 65 64 20 76 61 6c  d.  The seed val
a7750 75 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74  ue does.  ** not
a7760 20 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e   need to contain
a7770 20 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d   a lot of random
a7780 6e 65 73 73 20 73 69 6e 63 65 20 77 65 20 61 72  ness since we ar
a7790 65 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e  e not.  ** tryin
a77a0 67 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65  g to do secure e
a77b0 6e 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79  ncryption or any
a77c0 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e  thing like that.
a77d0 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74  ...  **.  ** Not
a77e0 68 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c  hing in this fil
a77f0 65 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c  e or anywhere el
a7800 73 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65  se in SQLite doe
a7810 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20  s any kind of.  
a7820 2a 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20  ** encryption.  
a7830 54 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68  The RC4 algorith
a7840 6d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20  m is being used 
a7850 61 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 64  as a PRNG (pseud
a7860 6f 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75  o-random.  ** nu
a7870 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20  mber generator) 
a7880 6e 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70  not as an encryp
a7890 74 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a  tion device..  *
a78a0 2f 0a 20 20 69 66 28 20 21 77 73 64 50 72 6e 67  /.  if( !wsdPrng
a78b0 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 69  .isInit ){.    i
a78c0 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 6b  nt i;.    char k
a78d0 5b 32 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 72  [256];.    wsdPr
a78e0 6e 67 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 73  ng.j = 0;.    ws
a78f0 64 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 20  dPrng.i = 0;.   
a7900 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d   sqlite3OsRandom
a7910 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73  ness(sqlite3_vfs
a7920 5f 66 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 6b  _find(0), 256, k
a7930 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
a7940 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20  i<256; i++){.   
a7950 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20     wsdPrng.s[i] 
a7960 3d 20 28 75 38 29 69 3b 0a 20 20 20 20 7d 0a 20  = (u8)i;.    }. 
a7970 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35     for(i=0; i<25
a7980 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77  6; i++){.      w
a7990 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 77 73 64 50  sdPrng.j += wsdP
a79a0 72 6e 67 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d 3b  rng.s[i] + k[i];
a79b0 0a 20 20 20 20 20 20 74 20 3d 20 77 73 64 50 72  .      t = wsdPr
a79c0 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b  ng.s[wsdPrng.j];
a79d0 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73  .      wsdPrng.s
a79e0 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77 73  [wsdPrng.j] = ws
a79f0 64 50 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 20  dPrng.s[i];.    
a7a00 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d    wsdPrng.s[i] =
a7a10 20 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 73   t;.    }.    ws
a7a20 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20 31  dPrng.isInit = 1
a7a30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65  ;.  }..  /* Gene
a7a40 72 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  rate and return 
a7a50 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62 79  single random by
a7a60 74 65 0a 20 20 2a 2f 0a 20 20 77 73 64 50 72 6e  te.  */.  wsdPrn
a7a70 67 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 77 73 64  g.i++;.  t = wsd
a7a80 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69  Prng.s[wsdPrng.i
a7a90 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b  ];.  wsdPrng.j +
a7aa0 3d 20 74 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73  = t;.  wsdPrng.s
a7ab0 5b 77 73 64 50 72 6e 67 2e 69 5d 20 3d 20 77 73  [wsdPrng.i] = ws
a7ac0 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e  dPrng.s[wsdPrng.
a7ad0 6a 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 5b  j];.  wsdPrng.s[
a7ae0 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b 0a  wsdPrng.j] = t;.
a7af0 20 20 74 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73    t += wsdPrng.s
a7b00 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 72  [wsdPrng.i];.  r
a7b10 65 74 75 72 6e 20 77 73 64 50 72 6e 67 2e 73 5b  eturn wsdPrng.s[
a7b20 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  t];.}../*.** Ret
a7b30 75 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 74  urn N random byt
a7b40 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
a7b50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
a7b60 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c  andomness(int N,
a7b70 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20   void *pBuf){.  
a7b80 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
a7b90 42 75 66 20 3d 20 70 42 75 66 3b 0a 23 69 66 20  Buf = pBuf;.#if 
a7ba0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
a7bb0 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E.  sqlite3_mute
a7bc0 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  x *mutex = sqlit
a7bd0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
a7be0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
a7bf0 5f 50 52 4e 47 29 3b 0a 23 65 6e 64 69 66 0a 20  _PRNG);.#endif. 
a7c00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
a7c10 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 77  nter(mutex);.  w
a7c20 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20  hile( N-- ){.   
a7c30 20 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 72 61 6e   *(zBuf++) = ran
a7c40 64 6f 6d 42 79 74 65 28 29 3b 0a 20 20 7d 0a 20  domByte();.  }. 
a7c50 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
a7c60 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a 0a  eave(mutex);.}..
a7c70 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
a7c80 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
a7c90 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 65 73 74 69  ./*.** For testi
a7ca0 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65 20  ng purposes, we 
a7cb0 73 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20 74  sometimes want t
a7cc0 6f 20 70 72 65 73 65 72 76 65 20 74 68 65 20 73  o preserve the s
a7cd0 74 61 74 65 20 6f 66 0a 2a 2a 20 50 52 4e 47 20  tate of.** PRNG 
a7ce0 61 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65 20  and restore the 
a7cf0 50 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76 65  PRNG to its save
a7d00 64 20 73 74 61 74 65 20 61 74 20 61 20 6c 61 74  d state at a lat
a7d10 65 72 20 74 69 6d 65 2c 20 6f 72 0a 2a 2a 20 74  er time, or.** t
a7d20 6f 20 72 65 73 65 74 20 74 68 65 20 50 52 4e 47  o reset the PRNG
a7d30 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20   to its initial 
a7d40 73 74 61 74 65 2e 20 20 54 68 65 73 65 20 72 6f  state.  These ro
a7d50 75 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c 69 73  utines accomplis
a7d60 68 0a 2a 2a 20 74 68 6f 73 65 20 74 61 73 6b 73  h.** those tasks
a7d70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
a7d80 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
a7d90 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c  () interface cal
a7da0 6c 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  ls these routine
a7db0 73 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20  s to.** control 
a7dc0 74 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74 61  the PRNG..*/.sta
a7dd0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
a7de0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a7df0 67 54 79 70 65 20 73 71 6c 69 74 65 33 53 61 76  gType sqlite3Sav
a7e00 65 64 50 72 6e 67 3b 0a 53 51 4c 49 54 45 5f 50  edPrng;.SQLITE_P
a7e10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a7e20 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65  te3PrngSaveState
a7e30 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79  (void){.  memcpy
a7e40 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74  (.    &GLOBAL(st
a7e50 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
a7e60 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76  Type, sqlite3Sav
a7e70 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c  edPrng),.    &GL
a7e80 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69  OBAL(struct sqli
a7e90 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c  te3PrngType, sql
a7ea0 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 73  ite3Prng),.    s
a7eb0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e  izeof(sqlite3Prn
a7ec0 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45  g).  );.}.SQLITE
a7ed0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a7ee0 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65  lite3PrngRestore
a7ef0 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d  State(void){.  m
a7f00 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42  emcpy(.    &GLOB
a7f10 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  AL(struct sqlite
a7f20 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74  3PrngType, sqlit
a7f30 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c  e3Prng),.    &GL
a7f40 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69  OBAL(struct sqli
a7f50 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c  te3PrngType, sql
a7f60 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a  ite3SavedPrng),.
a7f70 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74      sizeof(sqlit
a7f80 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53  e3Prng).  );.}.S
a7f90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a7fa0 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65  id sqlite3PrngRe
a7fb0 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b 0a  setState(void){.
a7fc0 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20    GLOBAL(struct 
a7fd0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c  sqlite3PrngType,
a7fe0 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69 73   sqlite3Prng).is
a7ff0 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64  Init = 0;.}.#end
a8000 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
a8010 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a  T_BUILTIN_TEST *
a8020 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
a8030 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f 6d  ** End of random
a8040 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
a8050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8070 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
a8080 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74  ** Begin file ut
a8090 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.c ************
a80a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a80b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a80c0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70  */./*.** 2004 Ap
a80d0 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65  ril 13.**.** The
a80e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
a80f0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
a8100 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
a8110 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
a8120 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
a8130 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
a8140 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
a8150 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
a8160 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
a8170 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
a8180 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
a8190 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
a81a0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
a81b0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
a81c0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
a81d0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
a81e0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
a81f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8230 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
a8240 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74  le contains rout
a8250 69 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72 61  ines used to tra
a8260 6e 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20 55  nslate between U
a8270 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31 36  TF-8, .** UTF-16
a8280 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64 20  , UTF-16BE, and 
a8290 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20  UTF-16LE..**.** 
a82a0 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a 0a  Notes on UTF-8:.
a82b0 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20  **.**   Byte-0  
a82c0 20 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 65    Byte-1    Byte
a82d0 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 20  -2    Byte-3    
a82e0 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 78  Value.**  0xxxxx
a82f0 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xx              
a8300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8310 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30     00000000 0000
a8320 30 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 2a  0000 0xxxxxxx.**
a8330 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78    110yyyyy  10xx
a8340 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20  xxxx            
a8350 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30             00000
a8360 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78  000 00000yyy yyx
a8370 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a 7a  xxxxx.**  1110zz
a8380 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 30  zz  10yyyyyy  10
a8390 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20  xxxxxx          
a83a0 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a     00000000 zzzz
a83b0 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a  yyyy yyxxxxxx.**
a83c0 20 20 31 31 31 31 30 75 75 75 20 20 31 30 75 75    11110uuu  10uu
a83d0 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20  zzzz  10yyyyyy  
a83e0 31 30 78 78 78 78 78 78 20 20 20 30 30 30 75 75  10xxxxxx   000uu
a83f0 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78  uuu zzzzyyyy yyx
a8400 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e  xxxxx.**.**.** N
a8410 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a 20  otes on UTF-16: 
a8420 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d 75   (with wwww+1==u
a8430 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20  uuuu).**.**     
a8440 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 20   Word-0         
a8450 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 20        Word-1    
a8460 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20        Value.**  
a8470 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 79  110110ww wwzzzzy
a8480 79 20 20 20 31 31 30 31 31 31 79 79 20 79 79 78  y   110111yy yyx
a8490 78 78 78 78 78 20 20 20 20 30 30 30 75 75 75 75  xxxxx    000uuuu
a84a0 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78  u zzzzyyyy yyxxx
a84b0 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79  xxx.**  zzzzyyyy
a84c0 20 79 79 78 78 78 78 78 78 20 20 20 20 20 20 20   yyxxxxxx       
a84d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a84e0 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79   00000000 zzzzyy
a84f0 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a  yy yyxxxxxx.**.*
a8500 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 65  *.** BOM or Byte
a8510 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20   Order Mark:.** 
a8520 20 20 20 20 30 78 66 66 20 30 78 66 65 20 20 20      0xff 0xfe   
a8530 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74  little-endian ut
a8540 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20  f-16 follows.** 
a8550 20 20 20 20 30 78 66 65 20 30 78 66 66 20 20 20      0xfe 0xff   
a8560 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31  big-endian utf-1
a8570 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a  6 follows.**.*/.
a8580 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a8590 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 2e  Include vdbeInt.
a85a0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
a85b0 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a  of utf.c *******
a85c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a85d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a85e0 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 49  Begin file vdbeI
a85f0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
a8600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a8620 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65  /*.** 2003 Septe
a8630 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  mber 6.**.** The
a8640 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
a8650 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
a8660 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
a8670 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
a8680 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
a8690 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
a86a0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
a86b0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
a86c0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
a86d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
a86e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
a86f0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
a8700 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
a8710 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
a8720 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
a8730 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
a8740 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
a8750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8790 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  *****.** This is
a87a0 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65   the header file
a87b0 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   for information
a87c0 20 74 68 61 74 20 69 73 20 70 72 69 76 61 74 65   that is private
a87d0 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e   to the.** VDBE.
a87e0 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69    This informati
a87f0 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 62  on used to all b
a8800 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20  e at the top of 
a8810 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f  the single.** so
a8820 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 22  urce code file "
a8830 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 74  vdbe.c".  When t
a8840 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 20  hat file became 
a8850 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a 2a  too big (over.**
a8860 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e 67   6000 lines long
a8870 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 75  ) it was split u
a8880 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 73  p into several s
a8890 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e 64  maller files and
a88a0 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 20  .** this header 
a88b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20  information was 
a88c0 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a 2f  factored out..*/
a88d0 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e  .#ifndef _VDBEIN
a88e0 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 44  T_H_.#define _VD
a88f0 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  BEINT_H_../*.** 
a8900 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 65  SQL is translate
a8910 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63  d into a sequenc
a8920 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  e of instruction
a8930 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 75  s to be.** execu
a8940 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61 6c  ted by a virtual
a8950 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 20   machine.  Each 
a8960 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61  instruction is a
a8970 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
a8980 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
a8990 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70  tructure..*/.typ
a89a0 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
a89b0 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f  Op Op;../*.** Bo
a89c0 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a  olean values.*/.
a89d0 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
a89e0 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a   char Bool;../*.
a89f0 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61  ** A cursor is a
a8a00 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20   pointer into a 
a8a10 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74  single BTree wit
a8a20 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  hin a database f
a8a30 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73  ile..** The curs
a8a40 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61  or can seek to a
a8a50 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74   BTree entry wit
a8a60 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b  h a particular k
a8a70 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f  ey, or.** loop o
a8a80 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  ver all entries 
a8a90 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59  of the Btree.  Y
a8aa0 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65  ou can also inse
a8ab0 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20  rt new BTree.** 
a8ac0 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69  entries or retri
a8ad0 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64  eve the key or d
a8ae0 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74  ata from the ent
a8af0 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73  ry that the curs
a8b00 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74  or.** is current
a8b10 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a  ly pointing to..
a8b20 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72  ** .** Every cur
a8b30 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72  sor that the vir
a8b40 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73  tual machine has
a8b50 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65   open is represe
a8b60 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e  nted by an.** in
a8b70 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
a8b80 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
a8b90 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
a8ba0 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72 69  VdbeCursor.isTri
a8bb0 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73 20  ggerRow flag is 
a8bc0 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68 61  set it means tha
a8bd0 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73  t this cursor is
a8be0 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69 6e  .** really a sin
a8bf0 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65 70  gle row that rep
a8c00 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57 20  resents the NEW 
a8c10 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61  or OLD pseudo-ta
a8c20 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20  ble of.** a row 
a8c30 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64 61  trigger.  The da
a8c40 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 69  ta for the row i
a8c50 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65  s stored in Vdbe
a8c60 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e 64  Cursor.pData and
a8c70 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 73  .** the rowid is
a8c80 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 69   in VdbeCursor.i
a8c90 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  Key..*/.struct V
a8ca0 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42 74  dbeCursor {.  Bt
a8cb0 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b  Cursor *pCursor;
a8cc0 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
a8cd0 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74  r structure of t
a8ce0 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20  he backend */.  
a8cf0 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
a8d00 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
a8d10 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 73  f cursor databas
a8d20 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28  e in db->aDb[] (
a8d30 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20  or -1) */.  i64 
a8d40 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20  lastRowid;      
a8d50 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 20    /* Last rowid 
a8d60 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 4e  from a Next or N
a8d70 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f 6e  extIdx operation
a8d80 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f 65   */.  Bool zeroe
a8d90 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  d;          /* T
a8da0 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f 75  rue if zeroed ou
a8db0 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 20  t and ready for 
a8dc0 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c 20  reuse */.  Bool 
a8dd0 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 20  rowidIsValid;   
a8de0 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 74   /* True if last
a8df0 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 2a  Rowid is valid *
a8e00 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 74  /.  Bool atFirst
a8e10 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
a8e20 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f  e if pointing to
a8e30 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a   first entry */.
a8e40 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f 6d    Bool useRandom
a8e50 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 72  Rowid;  /* Gener
a8e60 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  ate new record n
a8e70 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e 64  umbers semi-rand
a8e80 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e  omly */.  Bool n
a8e90 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 20  ullRow;         
a8ea0 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74  /* True if point
a8eb0 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 74  ing to a row wit
a8ec0 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 42  h no data */.  B
a8ed0 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65  ool deferredMove
a8ee0 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74  to;  /* A call t
a8ef0 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  o sqlite3BtreeMo
a8f00 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64  veto() is needed
a8f10 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62   */.  Bool isTab
a8f20 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  le;         /* T
a8f30 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72  rue if a table r
a8f40 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72  equiring integer
a8f50 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20   keys */.  Bool 
a8f60 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  isIndex;        
a8f70 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69   /* True if an i
a8f80 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ndex containing 
a8f90 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64  keys only - no d
a8fa0 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76  ata */.  i64 mov
a8fb0 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f  etoTarget;     /
a8fc0 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
a8fd0 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74  e deferred sqlit
a8fe0 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20  e3BtreeMoveto() 
a8ff0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b  */.  Btree *pBt;
a9000 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
a9010 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64  parate file hold
a9020 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ing temporary ta
a9030 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 70 73 65  ble */.  int pse
a9040 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 2f  udoTableReg;   /
a9050 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
a9060 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 63  ng pseudotable c
a9070 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 4b 65 79  ontent. */.  Key
a9080 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
a9090 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74     /* Info about
a90a0 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65 64   index keys need
a90b0 65 64 20 62 79 20 69 6e 64 65 78 20 63 75 72 73  ed by index curs
a90c0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69  ors */.  int nFi
a90d0 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f  eld;           /
a90e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * Number of fiel
a90f0 64 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  ds in the header
a9100 20 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f 75   */.  i64 seqCou
a9110 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  nt;         /* S
a9120 65 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20  equence counter 
a9130 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  */.  sqlite3_vta
a9140 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
a9150 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 63  ursor;  /* The c
a9160 75 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72 74  ursor for a virt
a9170 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63  ual table */.  c
a9180 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
a9190 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20  ule *pModule;   
a91a0 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 20    /* Module for 
a91b0 63 75 72 73 6f 72 20 70 56 74 61 62 43 75 72 73  cursor pVtabCurs
a91c0 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73 75  or */..  /* Resu
a91d0 6c 74 20 6f 66 20 6c 61 73 74 20 73 71 6c 69 74  lt of last sqlit
a91e0 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20  e3BtreeMoveto() 
a91f0 64 6f 6e 65 20 62 79 20 61 6e 20 4f 50 5f 4e 6f  done by an OP_No
a9200 74 45 78 69 73 74 73 20 6f 72 20 0a 20 20 2a 2a  tExists or .  **
a9210 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 63   OP_IsUnique opc
a9220 6f 64 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73  ode on this curs
a9230 6f 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65  or. */.  int see
a9240 6b 52 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 43  kResult;..  /* C
a9250 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f  ached informatio
a9260 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61 64  n about the head
a9270 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 20  er for the data 
a9280 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65 0a  record that the.
a9290 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 20 63    ** cursor is c
a92a0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
a92b0 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 6c 69  g to.  Only vali
a92c0 64 20 69 66 20 63 61 63 68 65 53 74 61 74 75 73  d if cacheStatus
a92d0 20 6d 61 74 63 68 65 73 0a 20 20 2a 2a 20 56 64   matches.  ** Vd
a92e0 62 65 2e 63 61 63 68 65 43 74 72 2e 20 20 56 64  be.cacheCtr.  Vd
a92f0 62 65 2e 63 61 63 68 65 43 74 72 20 77 69 6c 6c  be.cacheCtr will
a9300 20 6e 65 76 65 72 20 74 61 6b 65 20 6f 6e 20 74   never take on t
a9310 68 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a  he value of.  **
a9320 20 43 41 43 48 45 5f 53 54 41 4c 45 20 61 6e 64   CACHE_STALE and
a9330 20 73 6f 20 73 65 74 74 69 6e 67 20 63 61 63 68   so setting cach
a9340 65 53 74 61 74 75 73 3d 43 41 43 48 45 5f 53 54  eStatus=CACHE_ST
a9350 41 4c 45 20 67 75 61 72 61 6e 74 65 65 73 20 74  ALE guarantees t
a9360 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 63 61 63  hat.  ** the cac
a9370 68 65 20 69 73 20 6f 75 74 20 6f 66 20 64 61 74  he is out of dat
a9380 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 52 6f  e..  **.  ** aRo
a9390 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f  w might point to
a93a0 20 28 65 70 68 65 6d 65 72 61 6c 29 20 64 61 74   (ephemeral) dat
a93b0 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  a for the curren
a93c0 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 6d 69 67  t row, or it mig
a93d0 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 4c 4c 2e  ht.  ** be NULL.
a93e0 0a 20 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 68  .  */.  u32 cach
a93f0 65 53 74 61 74 75 73 3b 20 20 20 20 20 20 2f 2a  eStatus;      /*
a9400 20 43 61 63 68 65 20 69 73 20 76 61 6c 69 64 20   Cache is valid 
a9410 69 66 20 74 68 69 73 20 6d 61 74 63 68 65 73 20  if this matches 
a9420 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 2a 2f  Vdbe.cacheCtr */
a9430 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53 69  .  int payloadSi
a9440 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61  ze;      /* Tota
a9450 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  l number of byte
a9460 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
a9470 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 3b  */.  u32 *aType;
a9480 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79             /* Ty
a9490 70 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61 6c  pe values for al
a94a0 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  l entries in the
a94b0 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32   record */.  u32
a94c0 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20   *aOffset;      
a94d0 20 20 20 2f 2a 20 43 61 63 68 65 64 20 6f 66 66     /* Cached off
a94e0 73 65 74 73 20 74 6f 20 74 68 65 20 73 74 61 72  sets to the star
a94f0 74 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e  t of each column
a9500 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a  s data */.  u8 *
a9510 61 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20  aRow;           
a9520 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 74 68    /* Data for th
a9530 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 69  e current row, i
a9540 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 67  f all on one pag
a9550 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  e */.};.typedef 
a9560 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 6f  struct VdbeCurso
a9570 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0a 0a 2f  r VdbeCursor;../
a9580 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 75 62 2d  *.** When a sub-
a9590 70 72 6f 67 72 61 6d 20 69 73 20 65 78 65 63 75  program is execu
a95a0 74 65 64 20 28 4f 50 5f 50 72 6f 67 72 61 6d 29  ted (OP_Program)
a95b0 2c 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66  , a structure of
a95c0 20 74 68 69 73 20 74 79 70 65 0a 2a 2a 20 69 73   this type.** is
a95d0 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73 74   allocated to st
a95e0 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ore the current 
a95f0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 72 6f  value of the pro
a9600 67 72 61 6d 20 63 6f 75 6e 74 65 72 2c 20 61 73  gram counter, as
a9610 0a 2a 2a 20 77 65 6c 6c 20 61 73 20 74 68 65 20  .** well as the 
a9620 63 75 72 72 65 6e 74 20 6d 65 6d 6f 72 79 20 63  current memory c
a9630 65 6c 6c 20 61 72 72 61 79 20 61 6e 64 20 76 61  ell array and va
a9640 72 69 6f 75 73 20 6f 74 68 65 72 20 66 72 61 6d  rious other fram
a9650 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 76 61  e specific.** va
a9660 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74  lues stored in t
a9670 68 65 20 56 64 62 65 20 73 74 72 75 63 74 2e 20  he Vdbe struct. 
a9680 57 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 6f  When the sub-pro
a9690 67 72 61 6d 20 69 73 20 66 69 6e 69 73 68 65 64  gram is finished
a96a0 2c 20 0a 2a 2a 20 74 68 65 73 65 20 76 61 6c 75  , .** these valu
a96b0 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 62 61  es are copied ba
a96c0 63 6b 20 74 6f 20 74 68 65 20 56 64 62 65 20 66  ck to the Vdbe f
a96d0 72 6f 6d 20 74 68 65 20 56 64 62 65 46 72 61 6d  rom the VdbeFram
a96e0 65 20 73 74 72 75 63 74 75 72 65 2c 0a 2a 2a 20  e structure,.** 
a96f0 72 65 73 74 6f 72 69 6e 67 20 74 68 65 20 73 74  restoring the st
a9700 61 74 65 20 6f 66 20 74 68 65 20 56 4d 20 74 6f  ate of the VM to
a9710 20 61 73 20 69 74 20 77 61 73 20 62 65 66 6f 72   as it was befor
a9720 65 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61  e the sub-progra
a9730 6d 0a 2a 2a 20 62 65 67 61 6e 20 65 78 65 63 75  m.** began execu
a9740 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 72 61 6d  ting..**.** Fram
a9750 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e  es are stored in
a9760 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68   a linked list h
a9770 65 61 64 65 64 20 61 74 20 56 64 62 65 2e 70 50  eaded at Vdbe.pP
a9780 61 72 65 6e 74 2e 20 56 64 62 65 2e 70 50 61 72  arent. Vdbe.pPar
a9790 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70 61  ent.** is the pa
a97a0 72 65 6e 74 20 6f 66 20 74 68 65 20 63 75 72 72  rent of the curr
a97b0 65 6e 74 20 66 72 61 6d 65 2c 20 6f 72 20 7a 65  ent frame, or ze
a97c0 72 6f 20 69 66 20 74 68 65 20 63 75 72 72 65 6e  ro if the curren
a97d0 74 20 66 72 61 6d 65 0a 2a 2a 20 69 73 20 74 68  t frame.** is th
a97e0 65 20 6d 61 69 6e 20 56 64 62 65 20 70 72 6f 67  e main Vdbe prog
a97f0 72 61 6d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ram..*/.typedef 
a9800 73 74 72 75 63 74 20 56 64 62 65 46 72 61 6d 65  struct VdbeFrame
a9810 20 56 64 62 65 46 72 61 6d 65 3b 0a 73 74 72 75   VdbeFrame;.stru
a9820 63 74 20 56 64 62 65 46 72 61 6d 65 20 7b 0a 20  ct VdbeFrame {. 
a9830 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
a9840 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 74           /* VM t
a9850 68 69 73 20 66 72 61 6d 65 20 62 65 6c 6f 6e 67  his frame belong
a9860 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 70 63  s to */.  int pc
a9870 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a9880 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f 75    /* Program Cou
a9890 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f  nter */.  Op *aO
a98a0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
a98b0 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6e 73    /* Program ins
a98c0 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  tructions */.  i
a98d0 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 20  nt nOp;         
a98e0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
a98f0 66 20 61 4f 70 20 61 72 72 61 79 20 2a 2f 0a 20  f aOp array */. 
a9900 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20   Mem *aMem;     
a9910 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
a9920 79 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  y of memory cell
a9930 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b  s */.  int nMem;
a9940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a9950 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
a9960 69 65 73 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a 20  ies in aMem */. 
a9970 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70   VdbeCursor **ap
a9980 43 73 72 3b 20 20 20 20 20 2f 2a 20 45 6c 65 6d  Csr;     /* Elem
a9990 65 6e 74 20 6f 66 20 56 64 62 65 20 63 75 72 73  ent of Vdbe curs
a99a0 6f 72 73 20 2a 2f 0a 20 20 75 31 36 20 6e 43 75  ors */.  u16 nCu
a99b0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  rsor;           
a99c0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
a99d0 74 72 69 65 73 20 69 6e 20 61 70 43 73 72 20 2a  tries in apCsr *
a99e0 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e 3b  /.  void *token;
a99f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
a9a00 6f 70 79 20 6f 66 20 53 75 62 50 72 6f 67 72 61  opy of SubProgra
a9a10 6d 2e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  m.token */.  int
a9a20 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20 20   nChildMem;     
a9a30 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
a9a40 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66  f memory cells f
a9a50 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20 2a  or child frame *
a9a60 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43 73  /.  int nChildCs
a9a70 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  r;          /* N
a9a80 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73  umber of cursors
a9a90 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65   for child frame
a9aa0 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f   */.  i64 lastRo
a9ab0 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  wid;          /*
a9ac0 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77   Last insert row
a9ad0 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 74  id (sqlite3.last
a9ae0 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20  Rowid) */.  int 
a9af0 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20  nChange;        
a9b00 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74      /* Statement
a9b10 20 63 68 61 6e 67 65 73 20 28 56 64 62 65 2e 6e   changes (Vdbe.n
a9b20 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a 2f 0a  Changes)     */.
a9b30 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50 61    VdbeFrame *pPa
a9b40 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61 72  rent;     /* Par
a9b50 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61 6d  ent of this fram
a9b60 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65  e */.};..#define
a9b70 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29   VdbeFrameMem(p)
a9b80 20 28 28 4d 65 6d 20 2a 29 26 28 28 75 38 20 2a   ((Mem *)&((u8 *
a9b90 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f  )p)[ROUND8(sizeo
a9ba0 66 28 56 64 62 65 46 72 61 6d 65 29 29 5d 29 0a  f(VdbeFrame))]).
a9bb0 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20 66  ./*.** A value f
a9bc0 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63 61  or VdbeCursor.ca
a9bd0 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d 65  cheValid that me
a9be0 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69 73  ans the cache is
a9bf0 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64 2e   always invalid.
a9c00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43 48  .*/.#define CACH
a9c10 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a 2a  E_STALE 0../*.**
a9c20 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65   Internally, the
a9c30 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74 65   vdbe manipulate
a9c40 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51 4c  s nearly all SQL
a9c50 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a 2a   values as Mem.*
a9c60 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45 61  * structures. Ea
a9c70 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d 61  ch Mem struct ma
a9c80 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c 65  y cache multiple
a9c90 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73   representations
a9ca0 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e 74   (string,.** int
a9cb0 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74 68  eger etc.) of th
a9cc0 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20 41  e same value.  A
a9cd0 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65 72   value (and ther
a9ce0 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63 74  efore Mem struct
a9cf0 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65 20  ure).** has the 
a9d00 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65 72  following proper
a9d10 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63 68  ties:.**.** Each
a9d20 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61 6e   value has a man
a9d30 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65 20  ifest type. The 
a9d40 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f 66  manifest type of
a9d50 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   the value store
a9d60 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73 74  d.** in a Mem st
a9d70 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65 64  ruct is returned
a9d80 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65 28   by the MemType(
a9d90 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68 65  Mem*) macro. The
a9da0 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65 20   type is.** one 
a9db0 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20  of SQLITE_NULL, 
a9dc0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20  SQLITE_INTEGER, 
a9dd0 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51 4c  SQLITE_REAL, SQL
a9de0 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20 53  ITE_TEXT or.** S
a9df0 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a 73  QLITE_BLOB..*/.s
a9e00 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75 6e  truct Mem {.  un
a9e10 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 3b  ion {.    i64 i;
a9e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a9e30 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20   Integer value. 
a9e40 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f  */.    int nZero
a9e50 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ;          /* Us
a9e60 65 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d 5f  ed when bit MEM_
a9e70 5a 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20 66  Zero is set in f
a9e80 6c 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e 63  lags */.    Func
a9e90 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20  Def *pDef;      
a9ea0 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 65  /* Used only whe
a9eb0 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67  n flags==MEM_Agg
a9ec0 20 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20 2a   */.    RowSet *
a9ed0 70 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20 55  pRowSet;    /* U
a9ee0 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c  sed only when fl
a9ef0 61 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74 20  ags==MEM_RowSet 
a9f00 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65  */.    VdbeFrame
a9f10 20 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55 73   *pFrame;  /* Us
a9f20 65 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d  ed when flags==M
a9f30 45 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d 20  EM_Frame */.  } 
a9f40 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20  u;.  double r;  
a9f50 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c           /* Real
a9f60 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69   value */.  sqli
a9f70 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
a9f80 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65  /* The associate
a9f90 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  d database conne
a9fa0 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20  ction */.  char 
a9fb0 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  *z;            /
a9fc0 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42  * String or BLOB
a9fd0 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20   value */.  int 
a9fe0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
a9ff0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
aa000 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e  racters in strin
aa010 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69  g value, excludi
aa020 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36  ng '\0' */.  u16
aa030 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
aa040 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61   /* Some combina
aa050 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c  tion of MEM_Null
aa060 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44  , MEM_Str, MEM_D
aa070 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38  yn, etc. */.  u8
aa080 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20    type;         
aa090 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49    /* One of SQLI
aa0a0 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f  TE_NULL, SQLITE_
aa0b0 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54  TEXT, SQLITE_INT
aa0c0 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75  EGER, etc */.  u
aa0d0 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20  8  enc;         
aa0e0 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46     /* SQLITE_UTF
aa0f0 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  8, SQLITE_UTF16B
aa100 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  E, SQLITE_UTF16L
aa110 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  E */.  void (*xD
aa120 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a  el)(void *);  /*
aa130 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61   If not null, ca
aa140 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
aa150 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a   to delete Mem.z
aa160 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c   */.  char *zMal
aa170 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e  loc;      /* Dyn
aa180 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f  amic buffer allo
aa190 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
aa1a0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a  _malloc() */.};.
aa1b0 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20  ./* One or more 
aa1c0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
aa1d0 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 74   flags are set t
aa1e0 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 76  o indicate the v
aa1f0 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73  alidOK.** repres
aa200 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
aa210 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
aa220 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e   the Mem struct.
aa230 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45  .**.** If the ME
aa240 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73  M_Null flag is s
aa250 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  et, then the val
aa260 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c  ue is an SQL NUL
aa270 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f  L value..** No o
aa280 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62  ther flags may b
aa290 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61  e set in this ca
aa2a0 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
aa2b0 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73   MEM_Str flag is
aa2c0 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20   set then Mem.z 
aa2d0 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 69  points at a stri
aa2e0 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
aa2f0 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68  n..** Usually th
aa300 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e  is is encoded in
aa310 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64   the same unicod
aa320 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68  e encoding as th
aa330 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61  e main.** databa
aa340 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f  se (see below fo
aa350 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49  r exceptions). I
aa360 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66  f the MEM_Term f
aa370 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73  lag is also.** s
aa380 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72  et, then the str
aa390 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69  ing is nul termi
aa3a0 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49  nated. The MEM_I
aa3b0 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20  nt and MEM_Real 
aa3c0 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f  .** flags may co
aa3d0 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 4d  exist with the M
aa3e0 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a  EM_Str flag..**.
aa3f0 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74  ** Multiple of t
aa400 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20  hese values can 
aa410 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c  appear in Mem.fl
aa420 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f  ags.  But only o
aa430 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20  ne.** at a time 
aa440 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65  can appear in Me
aa450 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69  m.type..*/.#defi
aa460 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20  ne MEM_Null     
aa470 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c   0x0001   /* Val
aa480 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64  ue is NULL */.#d
aa490 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20  efine MEM_Str   
aa4a0 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20      0x0002   /* 
aa4b0 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  Value is a strin
aa4c0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  g */.#define MEM
aa4d0 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30  _Int       0x000
aa4e0 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  4   /* Value is 
aa4f0 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64  an integer */.#d
aa500 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20  efine MEM_Real  
aa510 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20      0x0008   /* 
aa520 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20  Value is a real 
aa530 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e  number */.#defin
aa540 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20  e MEM_Blob      
aa550 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75  0x0010   /* Valu
aa560 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23  e is a BLOB */.#
aa570 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65  define MEM_RowSe
aa580 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a  t    0x0020   /*
aa590 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53   Value is a RowS
aa5a0 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65  et object */.#de
aa5b0 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20 20  fine MEM_Frame  
aa5c0 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20 56     0x0040   /* V
aa5d0 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46 72  alue is a VdbeFr
aa5e0 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64  ame object */.#d
aa5f0 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61  efine MEM_TypeMa
aa600 73 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a 20  sk  0x00ff   /* 
aa610 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74  Mask of type bit
aa620 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 65  s */../* Wheneve
aa630 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 61  r Mem contains a
aa640 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f 72   valid string or
aa650 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 61   blob representa
aa660 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20  tion, one of.** 
aa670 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c  the following fl
aa680 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 20  ags must be set 
aa690 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
aa6a0 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65   memory manageme
aa6b0 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f 72  nt.** policy for
aa6c0 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 4d   Mem.z.  The MEM
aa6d0 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c 73  _Term flag tells
aa6e0 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e   us whether or n
aa6f0 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67  ot the.** string
aa700 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 30   is \000 or \u00
aa710 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2f  00 terminated.*/
aa720 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 72  .#define MEM_Ter
aa730 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20 20  m      0x0200   
aa740 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 73  /* String rep is
aa750 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20   nul terminated 
aa760 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44  */.#define MEM_D
aa770 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 20  yn       0x0400 
aa780 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c    /* Need to cal
aa790 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 6f  l sqliteFree() o
aa7a0 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69  n Mem.z */.#defi
aa7b0 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20  ne MEM_Static   
aa7c0 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d   0x0800   /* Mem
aa7d0 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73  .z points to a s
aa7e0 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a  tatic string */.
aa7f0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65  #define MEM_Ephe
aa800 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f  m     0x1000   /
aa810 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74  * Mem.z points t
aa820 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73  o an ephemeral s
aa830 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65  tring */.#define
aa840 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30   MEM_Agg       0
aa850 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a  x2000   /* Mem.z
aa860 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67   points to an ag
aa870 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65  g function conte
aa880 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  xt */.#define ME
aa890 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 30  M_Zero      0x40
aa8a0 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f  00   /* Mem.i co
aa8b0 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 20  ntains count of 
aa8c0 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 62  0s appended to b
aa8d0 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  lob */..#ifdef S
aa8e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
aa8f0 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d  LOB.  #undef MEM
aa900 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20  _Zero.  #define 
aa910 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a  MEM_Zero 0x0000.
aa920 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43  #endif.../*.** C
aa930 6c 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e  lear any existin
aa940 67 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f  g type flags fro
aa950 6d 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c  m a Mem and repl
aa960 61 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a  ace them with f.
aa970 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65  */.#define MemSe
aa980 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20  tTypeFlag(p, f) 
aa990 5c 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73  \.   ((p)->flags
aa9a0 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e   = ((p)->flags&~
aa9b0 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45  (MEM_TypeMask|ME
aa9c0 4d 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f 2a  M_Zero))|f).../*
aa9d0 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a   A VdbeFunc is j
aa9e0 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64  ust a FuncDef (d
aa9f0 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65  efined in sqlite
aaa00 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74  Int.h) that cont
aaa10 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e  ains.** addition
aaa20 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  al information a
aaa30 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69  bout auxiliary i
aaa40 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64  nformation bound
aaa50 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a   to arguments.**
aaa60 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
aaa70 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
aaa80 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
aaa90 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78   sqlite3_get_aux
aaaa0 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71  data().** and sq
aaab0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
aaac0 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22  a() APIs.  The "
aaad0 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65  auxdata" is some
aaae0 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a   auxiliary data.
aaaf0 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61  ** that can be a
aab00 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
aab10 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65   constant argume
aab20 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  nt to a function
aab30 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77  .  This.** allow
aab40 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68  s functions such
aab50 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20   as "regexp" to 
aab60 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f  compile their co
aab70 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a  nstant regular.*
aab80 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67  * expression arg
aab90 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72  ument once and r
aaba0 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c  eused the compil
aabb0 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74  ed code for mult
aabc0 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  iple.** invocati
aabd0 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  ons..*/.struct V
aabe0 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63  dbeFunc {.  Func
aabf0 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20  Def *pFunc;     
aac00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
aac10 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74   definition of t
aac20 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
aac30 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20   int nAux;      
aac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
aac50 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
aac60 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ies allocated fo
aac70 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73  r apAux[] */.  s
aac80 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a  truct AuxData {.
aac90 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20      void *pAux; 
aaca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aacb0 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f    /* Aux data fo
aacc0 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d  r the i-th argum
aacd0 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20  ent */.    void 
aace0 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20  (*xDelete)(void 
aacf0 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74  *);      /* Dest
aad00 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61  ructor for the a
aad10 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61  ux data */.  } a
aad20 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20  pAux[1];        
aad30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
aad40 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20  e slot for each 
aad50 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
aad60 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  t */.};../*.** T
aad70 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67  he "context" arg
aad80 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74  ument for a inst
aad90 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e  allable function
aada0 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  .  A pointer to 
aadb0 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
aadc0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
aadd0 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72   is the first ar
aade0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f  gument to the ro
aadf0 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69  utines used.** i
aae00 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c  mplement the SQL
aae10 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a   functions..**.*
aae20 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79 70  * There is a typ
aae30 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 74  edef for this st
aae40 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74  ructure in sqlit
aae50 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75  e.h.  So all rou
aae60 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74  tines,.** even t
aae70 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66  he public interf
aae80 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63  ace to SQLite, c
aae90 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72  an use a pointer
aaea0 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
aaeb0 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20  re..** But this 
aaec0 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  file is the only
aaed0 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 65   place where the
aaee0 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c   internal detail
aaef0 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72  s of this.** str
aaf00 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e  ucture are known
aaf10 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ..**.** This str
aaf20 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e 65  ucture is define
aaf30 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65  d inside of vdbe
aaf40 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 74  Int.h because it
aaf50 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74 75   uses substructu
aaf60 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69  res.** (Mem) whi
aaf70 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69  ch are only defi
aaf80 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74  ned there..*/.st
aaf90 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e  ruct sqlite3_con
aafa0 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66  text {.  FuncDef
aafb0 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f   *pFunc;       /
aafc0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e  * Pointer to fun
aafd0 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ction informatio
aafe0 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53  n.  MUST BE FIRS
aaff0 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20  T */.  VdbeFunc 
ab000 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20  *pVdbeFunc;  /* 
ab010 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69  Auxilary data, i
ab020 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20  f created. */.  
ab030 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20  Mem s;          
ab040 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74        /* The ret
ab050 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f  urn value is sto
ab060 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65  red here */.  Me
ab070 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *pMem;        
ab080 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
ab090 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ll used to store
ab0a0 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65   aggregate conte
ab0b0 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72  xt */.  int isEr
ab0c0 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ror;          /*
ab0d0 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75   Error code retu
ab0e0 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e 63  rned by the func
ab0f0 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53  tion. */.  CollS
ab100 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20  eq *pColl;      
ab110 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
ab120 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  quence */.};../*
ab130 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 74  .** A Set struct
ab140 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ure is used for 
ab150 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f  quick testing to
ab160 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a   see if a value.
ab170 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 20  ** is part of a 
ab180 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73  small set.  Sets
ab190 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70   are used to imp
ab1a0 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65  lement code like
ab1b0 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20  .** this:.**    
ab1c0 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28          x.y IN (
ab1d0 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27  'hi','hoo','hum'
ab1e0 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ).*/.typedef str
ab1f0 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72  uct Set Set;.str
ab200 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68  uct Set {.  Hash
ab210 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 20   hash;          
ab220 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a     /* A set is j
ab230 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c 65  ust a hash table
ab240 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
ab250 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20  prev;        /* 
ab260 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73  Previously acces
ab270 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20  sed hash elemen 
ab280 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
ab290 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
ab2a0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
ab2b0 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
ab2c0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f   contains the co
ab2d0 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65 20  mplete.** state 
ab2e0 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  of the virtual m
ab2f0 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  achine..**.** Th
ab300 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22  e "sqlite3_stmt"
ab310 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74   structure point
ab320 65 72 20 74 68 61 74 20 69 73 20 72 65 74 75 72  er that is retur
ab330 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  ned by sqlite3_c
ab340 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20 72  ompile().** is r
ab350 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20  eally a pointer 
ab360 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
ab370 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
ab380 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65  ..**.** The Vdbe
ab390 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76 61  .inVtabMethod va
ab3a0 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  riable is set to
ab3b0 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 68   non-zero for th
ab3c0 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a  e duration of.**
ab3d0 20 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 62   any virtual tab
ab3e0 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63 61  le method invoca
ab3f0 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74 68  tions made by th
ab400 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20  e vdbe program. 
ab410 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20  It is.** set to 
ab420 32 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20 6d  2 for xDestroy m
ab430 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64 20  ethod calls and 
ab440 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20  1 for all other 
ab450 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a 2a  methods. This.**
ab460 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
ab470 64 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f 73  d for two purpos
ab480 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44 65  es: to allow xDe
ab490 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74 6f  stroy methods to
ab4a0 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52 4f   execute.** "DRO
ab4b0 50 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d 65  P TABLE" stateme
ab4c0 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76 65  nts and to preve
ab4d0 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73 69  nt some nasty si
ab4e0 64 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a 2a  de effects of.**
ab4f0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
ab500 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69  when SQLite is i
ab510 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76 65  nvoked recursive
ab520 6c 79 20 62 79 20 61 20 76 69 72 74 75 61 6c 20  ly by a virtual 
ab530 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f 64  table .** method
ab540 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
ab550 72 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73 71  ruct Vdbe {.  sq
ab560 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
ab570 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
ab580 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
ab590 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 20   that owns this 
ab5a0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56  statement */.  V
ab5b0 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e 65 78  dbe *pPrev,*pNex
ab5c0 74 3b 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64  t;     /* Linked
ab5d0 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 77   list of VDBEs w
ab5e0 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 62  ith the same Vdb
ab5f0 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  e.db */.  int nO
ab600 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
ab610 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
ab620 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74  nstructions in t
ab630 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  he program */.  
ab640 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20  int nOpAlloc;   
ab650 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ab660 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
ab670 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 2a  ated for aOp[] *
ab680 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20  /.  Op *aOp;    
ab690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
ab6a0 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
ab6b0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
ab6c0 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  's program */.  
ab6d0 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20 20  int nLabel;     
ab6e0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ab6f0 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64  r of labels used
ab700 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c   */.  int nLabel
ab710 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a  Alloc;        /*
ab720 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   Number of slots
ab730 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c   allocated in aL
ab740 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  abel[] */.  int 
ab750 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20  *aLabel;        
ab760 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20      /* Space to 
ab770 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73 20  hold the labels 
ab780 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67  */.  Mem **apArg
ab790 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
ab7a0 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 75 72  Arguments to cur
ab7b0 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67  rently executing
ab7c0 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a   user function *
ab7d0 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d  /.  Mem *aColNam
ab7e0 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  e;          /* C
ab7f0 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72  olumn names to r
ab800 65 74 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a  eturn */.  Mem *
ab810 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20  pResultSet;     
ab820 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
ab830 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73   an array of res
ab840 75 6c 74 73 20 2a 2f 0a 20 20 75 31 36 20 6e 52  ults */.  u16 nR
ab850 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  esColumn;       
ab860 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
ab870 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f  olumns in one ro
ab880 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  w of the result 
ab890 73 65 74 20 2a 2f 0a 20 20 75 31 36 20 6e 43 75  set */.  u16 nCu
ab8a0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  rsor;           
ab8b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c   /* Number of sl
ab8c0 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a  ots in apCsr[] *
ab8d0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
ab8e0 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 4f  *apCsr;     /* O
ab8f0 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  ne element of th
ab900 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61 63  is array for eac
ab910 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a 2f  h open cursor */
ab920 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69 6f  .  u8 errorActio
ab930 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  n;         /* Re
ab940 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74 6f  covery action to
ab950 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 61   do in case of a
ab960 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20  n error */.  u8 
ab970 6f 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20 20  okVar;          
ab980 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
ab990 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e  azVar[] has been
ab9a0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
ab9b0 20 20 79 6e 56 61 72 20 6e 56 61 72 3b 20 20 20    ynVar nVar;   
ab9c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ab9d0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
ab9e0 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d 65  n aVar[] */.  Me
ab9f0 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20 20  m *aVar;        
aba00 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20        /* Values 
aba10 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69 61  for the OP_Varia
aba20 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a 20  ble opcode. */. 
aba30 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 20   char **azVar;  
aba40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
aba50 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a 2f   of variables */
aba60 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20  .  u32 magic;   
aba70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
aba80 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73  gic number for s
aba90 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
abaa0 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20  /.  int nMem;   
abab0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
abac0 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
abad0 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e  locations curren
abae0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  tly allocated */
abaf0 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20  .  Mem *aMem;   
abb00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
abb10 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
abb20 6e 73 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 68  ns */.  u32 cach
abb30 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 20  eCtr;           
abb40 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72 6f  /* VdbeCursor ro
abb50 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74 69  w cache generati
abb60 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  on counter */.  
abb70 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20  int pc;         
abb80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
abb90 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a  rogram counter *
abba0 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
abbb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
abbc0 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a  alue to return *
abbd0 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  /.  char *zErrMs
abbe0 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  g;          /* E
abbf0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 69  rror message wri
abc00 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 75  tten here */.  u
abc10 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20  8 explain;      
abc20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
abc30 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e  f EXPLAIN presen
abc40 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  t on SQL command
abc50 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43   */.  u8 changeC
abc60 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  ntOn;         /*
abc70 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20   True to update 
abc80 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
abc90 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72  er */.  u8 expir
abca0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
abcb0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56  /* True if the V
abcc0 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  M needs to be re
abcd0 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38  compiled */.  u8
abce0 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72   minWriteFileFor
abcf0 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d  mat;  /* Minimum
abd00 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72   file format for
abd10 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61   writable databa
abd20 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38  se files */.  u8
abd30 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20   inVtabMethod;  
abd40 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d        /* See com
abd50 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20  ments above */. 
abd60 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 72   u8 usesStmtJour
abd70 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  nal;     /* True
abd80 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 65   if uses a state
abd90 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  ment journal */.
abda0 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20    u8 readOnly;  
abdb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
abdc0 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20  e for read-only 
abdd0 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  statements */.  
abde0 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b 20  u8 isPrepareV2; 
abdf0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
abe00 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 68  if prepared with
abe10 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a 2f   prepare_v2() */
abe20 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20  .  int nChange; 
abe30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
abe40 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67  mber of db chang
abe50 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61  es made since la
abe60 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 6e  st reset */.  in
abe70 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 20  t btreeMask;    
abe80 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
abe90 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e   of db->aDb[] en
abea0 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65 64  tries referenced
abeb0 20 2a 2f 0a 20 20 69 36 34 20 73 74 61 72 74 54   */.  i64 startT
abec0 69 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ime;          /*
abed0 20 54 69 6d 65 20 77 68 65 6e 20 71 75 65 72 79   Time when query
abee0 20 73 74 61 72 74 65 64 20 2d 20 75 73 65 64 20   started - used 
abef0 66 6f 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a 2f  for profiling */
abf00 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  .  BtreeMutexArr
abf10 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e  ay aMutex; /* An
abf20 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20   array of Btree 
abf30 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65  used here and ne
abf40 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20  eding locks */. 
abf50 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d   int aCounter[2]
abf60 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
abf70 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c  ters used by sql
abf80 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
abf90 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  () */.  char *zS
abfa0 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ql;             
abfb0 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 53  /* Text of the S
abfc0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  QL statement tha
abfd0 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 69 73  t generated this
abfe0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 65   */.  void *pFre
abff0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
ac000 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e 20   Free this when 
ac010 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62  deleting the vdb
ac020 65 20 2a 2f 0a 20 20 69 36 34 20 6e 46 6b 43 6f  e */.  i64 nFkCo
ac030 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 2f  nstraint;      /
ac040 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6d 6d 2e  * Number of imm.
ac050 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   FK constraints 
ac060 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 36 34  this VM */.  i64
ac070 20 6e 53 74 6d 74 44 65 66 43 6f 6e 73 3b 20 20   nStmtDefCons;  
ac080 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
ac090 66 20 64 65 66 2e 20 63 6f 6e 73 74 72 61 69 6e  f def. constrain
ac0a0 74 73 20 77 68 65 6e 20 73 74 6d 74 20 73 74 61  ts when stmt sta
ac0b0 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 53  rted */.  int iS
ac0c0 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20  tatement;       
ac0d0 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e    /* Statement n
ac0e0 75 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20 68  umber (or 0 if h
ac0f0 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74  as not opened st
ac100 6d 74 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  mt) */.#ifdef SQ
ac110 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c  LITE_DEBUG.  FIL
ac120 45 20 2a 74 72 61 63 65 3b 20 20 20 20 20 20 20  E *trace;       
ac130 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e       /* Write an
ac140 20 65 78 65 63 75 74 69 6f 6e 20 74 72 61 63 65   execution trace
ac150 20 68 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55   here, if not NU
ac160 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56  LL */.#endif.  V
ac170 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
ac180 3b 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74  ;      /* Parent
ac190 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20   frame */.  int 
ac1a0 6e 46 72 61 6d 65 3b 20 20 20 20 20 20 20 20 20  nFrame;         
ac1b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
ac1c0 20 66 72 61 6d 65 73 20 69 6e 20 70 46 72 61 6d   frames in pFram
ac1d0 65 20 6c 69 73 74 20 2a 2f 0a 20 20 75 33 32 20  e list */.  u32 
ac1e0 65 78 70 6d 61 73 6b 3b 20 20 20 20 20 20 20 20  expmask;        
ac1f0 20 20 20 20 2f 2a 20 42 69 6e 64 69 6e 67 20 74      /* Binding t
ac200 6f 20 74 68 65 73 65 20 76 61 72 73 20 69 6e 76  o these vars inv
ac210 61 6c 69 64 61 74 65 73 20 56 4d 20 2a 2f 0a 7d  alidates VM */.}
ac220 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
ac230 6c 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77  lowing are allow
ac240 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56 64  ed values for Vd
ac250 62 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66  be.magic.*/.#def
ac260 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  ine VDBE_MAGIC_I
ac270 4e 49 54 20 20 20 20 20 30 78 32 36 62 63 65 61  NIT     0x26bcea
ac280 61 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e  a5    /* Buildin
ac290 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d  g a VDBE program
ac2a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45   */.#define VDBE
ac2b0 5f 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20 20  _MAGIC_RUN      
ac2c0 30 78 62 64 66 32 30 64 61 33 20 20 20 20 2f 2a  0xbdf20da3    /*
ac2d0 20 56 44 42 45 20 69 73 20 72 65 61 64 79 20 74   VDBE is ready t
ac2e0 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64 65  o execute */.#de
ac2f0 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f  fine VDBE_MAGIC_
ac300 48 41 4c 54 20 20 20 20 20 30 78 35 31 39 63 32  HALT     0x519c2
ac310 39 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20 68  973    /* VDBE h
ac320 61 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65  as completed exe
ac330 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  cution */.#defin
ac340 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41  e VDBE_MAGIC_DEA
ac350 44 20 20 20 20 20 30 78 62 36 30 36 63 33 63 38  D     0xb606c3c8
ac360 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
ac370 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63  has been dealloc
ac380 61 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46  ated */../*.** F
ac390 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70  unction prototyp
ac3a0 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  es.*/.SQLITE_PRI
ac3b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
ac3c0 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28  3VdbeFreeCursor(
ac3d0 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72 73  Vdbe *, VdbeCurs
ac3e0 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74  or*);.void sqlit
ac3f0 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28 56 64  eVdbePopStack(Vd
ac400 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  be*,int);.SQLITE
ac410 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ac420 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f  ite3VdbeCursorMo
ac430 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 2a  veto(VdbeCursor*
ac440 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  );.#if defined(S
ac450 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20  QLITE_DEBUG) || 
ac460 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f  defined(VDBE_PRO
ac470 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49  FILE).SQLITE_PRI
ac480 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
ac490 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c  3VdbePrintOp(FIL
ac4a0 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23  E*, int, Op*);.#
ac4b0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
ac4c0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
ac4d0 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
ac4e0 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50  n(u32);.SQLITE_P
ac4f0 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
ac500 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
ac510 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  (Mem*, int);.SQL
ac520 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
ac530 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
ac540 6c 50 75 74 28 75 6e 73 69 67 6e 65 64 20 63 68  lPut(unsigned ch
ac550 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20  ar*, int, Mem*, 
ac560 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
ac570 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
ac580 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 63 6f  VdbeSerialGet(co
ac590 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
ac5a0 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a  r*, u32, Mem*);.
ac5b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
ac5c0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
ac5d0 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62  eleteAuxData(Vdb
ac5e0 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69  eFunc*, int);..i
ac5f0 6e 74 20 73 71 6c 69 74 65 32 42 74 72 65 65 4b  nt sqlite2BtreeK
ac600 65 79 43 6f 6d 70 61 72 65 28 42 74 43 75 72 73  eyCompare(BtCurs
ac610 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  or *, const void
ac620 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e   *, int, int, in
ac630 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t *);.SQLITE_PRI
ac640 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ac650 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72  VdbeIdxKeyCompar
ac660 65 28 56 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e  e(VdbeCursor*,Un
ac670 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e  packedRecord*,in
ac680 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
ac690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ac6a0 64 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c 69  dbeIdxRowid(sqli
ac6b0 74 65 33 2a 2c 20 42 74 43 75 72 73 6f 72 20 2a  te3*, BtCursor *
ac6c0 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45  , i64 *);.SQLITE
ac6d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ac6e0 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63  ite3MemCompare(c
ac6f0 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74  onst Mem*, const
ac700 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c   Mem*, const Col
ac710 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lSeq*);.SQLITE_P
ac720 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ac730 65 33 56 64 62 65 45 78 65 63 28 56 64 62 65 2a  e3VdbeExec(Vdbe*
ac740 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac750 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
ac760 65 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53 51  eList(Vdbe*);.SQ
ac770 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac780 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
ac790 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
ac7a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac7b0 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
ac7c0 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74  oding(Mem *, int
ac7d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac7e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
ac7f0 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29  eMemTooBig(Mem*)
ac800 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ac810 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
ac820 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f  MemCopy(Mem*, co
ac830 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  nst Mem*);.SQLIT
ac840 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
ac850 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
ac860 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63  llowCopy(Mem*, c
ac870 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b  onst Mem*, int);
ac880 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac890 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ac8a0 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65  MemMove(Mem*, Me
ac8b0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
ac8c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ac8d0 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
ac8e0 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  te(Mem*);.SQLITE
ac8f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ac900 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
ac910 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63 68  r(Mem*, const ch
ac920 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76 6f  ar*, int, u8, vo
ac930 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
ac940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ac950 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
ac960 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c 20  mSetInt64(Mem*, 
ac970 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  i64);.SQLITE_PRI
ac980 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
ac990 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c  3VdbeMemSetDoubl
ac9a0 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b  e(Mem*, double);
ac9b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac9c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ac9d0 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29  MemSetNull(Mem*)
ac9e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ac9f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
aca00 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28  eMemSetZeroBlob(
aca10 4d 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  Mem*,int);.SQLIT
aca20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
aca30 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
aca40 52 6f 77 53 65 74 28 4d 65 6d 2a 29 3b 0a 53 51  RowSet(Mem*);.SQ
aca50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
aca60 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
aca70 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d 65 6d  akeWriteable(Mem
aca80 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
aca90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acaa0 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 4d  beMemStringify(M
acab0 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  em*, int);.SQLIT
acac0 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71  E_PRIVATE i64 sq
acad0 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
acae0 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  e(Mem*);.SQLITE_
acaf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
acb00 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
acb10 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  rify(Mem*);.SQLI
acb20 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c  TE_PRIVATE doubl
acb30 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  e sqlite3VdbeRea
acb40 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51  lValue(Mem*);.SQ
acb50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
acb60 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  d sqlite3VdbeInt
acb70 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65 6d  egerAffinity(Mem
acb80 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
acb90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acba0 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d  beMemRealify(Mem
acbb0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
acbc0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acbd0 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65  beMemNumerify(Me
acbe0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
acbf0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
acc00 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
acc10 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e  BtCursor*,int,in
acc20 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c  t,int,Mem*);.SQL
acc30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
acc40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
acc50 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 3b 0a  elease(Mem *p);.
acc60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
acc70 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
acc80 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
acc90 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54  l(Mem *p);.SQLIT
acca0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
accb0 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61  lite3VdbeMemFina
accc0 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e 63 44  lize(Mem*, FuncD
accd0 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ef*);.SQLITE_PRI
acce0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
accf0 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61  *sqlite3OpcodeNa
acd00 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  me(int);.SQLITE_
acd10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
acd20 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d  te3VdbeMemGrow(M
acd30 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c  em *pMem, int n,
acd40 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 3b 0a   int preserve);.
acd50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
acd60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c  nt sqlite3VdbeCl
acd70 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62  oseStatement(Vdb
acd80 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e *, int);.SQLIT
acd90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
acda0 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 44  qlite3VdbeFrameD
acdb0 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 2a  elete(VdbeFrame*
acdc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
acdd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
acde0 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64  eFrameRestore(Vd
acdf0 62 65 46 72 61 6d 65 20 2a 29 3b 0a 53 51 4c 49  beFrame *);.SQLI
ace00 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ace10 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
ace20 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65  oreType(Mem *pMe
ace30 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  m);..#ifndef SQL
ace40 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
ace50 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 56  _KEY.SQLITE_PRIV
ace60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ace70 64 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65 20  dbeCheckFk(Vdbe 
ace80 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23  *, int);.#else.#
ace90 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
acea0 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 69 29 20  dbeCheckFk(p,i) 
aceb0 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  0.#endif..#ifnde
acec0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
aced0 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54  ARED_CACHE.SQLIT
acee0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
acef0 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41  qlite3VdbeMutexA
acf00 72 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a  rrayEnter(Vdbe *
acf10 70 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  p);.#else.# defi
acf20 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75  ne sqlite3VdbeMu
acf30 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 29  texArrayEnter(p)
acf40 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f  .#endif..SQLITE_
acf50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
acf60 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c  te3VdbeMemTransl
acf70 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23  ate(Mem*, u8);.#
acf80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
acf90 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
acfa0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
acfb0 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62  VdbePrintSql(Vdb
acfc0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
acfd0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
acfe0 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
acff0 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  rint(Mem *pMem, 
ad000 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e  char *zBuf);.#en
ad010 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
ad020 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
ad030 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d  beMemHandleBom(M
ad040 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e  em *pMem);..#ifn
ad050 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
ad060 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f  INCRBLOB.SQLITE_
ad070 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
ad080 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
ad090 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23  ndBlob(Mem *);.#
ad0a0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73  else.  #define s
ad0b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
ad0c0 61 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 54  andBlob(x) SQLIT
ad0d0 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  E_OK.#endif..#en
ad0e0 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
ad0f0 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a  _VDBEINT_H_) */.
ad100 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
ad110 20 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e   End of vdbeInt.
ad120 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
ad130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
ad150 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
ad160 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
ad170 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
ad180 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a   utf.c *********
ad190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
ad1a0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
ad1b0 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a  _AMALGAMATION./*
ad1c0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
ad1d0 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65  g constant value
ad1e0 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
ad1f0 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
ad200 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c   and.** SQLITE_L
ad210 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72  ITTLEENDIAN macr
ad220 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  os..*/.SQLITE_PR
ad230 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20  IVATE const int 
ad240 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a  sqlite3one = 1;.
ad250 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
ad260 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f  _AMALGAMATION */
ad270 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f  ../*.** This loo
ad280 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 65  kup table is use
ad290 64 20 74 6f 20 68 65 6c 70 20 64 65 63 6f 64 65  d to help decode
ad2a0 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
ad2b0 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 79  of.** a multi-by
ad2c0 74 65 20 55 54 46 38 20 63 68 61 72 61 63 74 65  te UTF8 characte
ad2d0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  r..*/.static con
ad2e0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
ad2f0 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e   sqlite3Utf8Tran
ad300 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c  s1[] = {.  0x00,
ad310 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30   0x01, 0x02, 0x0
ad320 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30  3, 0x04, 0x05, 0
ad330 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30  x06, 0x07,.  0x0
ad340 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30  8, 0x09, 0x0a, 0
ad350 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c  x0b, 0x0c, 0x0d,
ad360 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30   0x0e, 0x0f,.  0
ad370 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32 2c  x10, 0x11, 0x12,
ad380 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78 31   0x13, 0x14, 0x1
ad390 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 20  5, 0x16, 0x17,. 
ad3a0 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78 31   0x18, 0x19, 0x1
ad3b0 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20 30  a, 0x1b, 0x1c, 0
ad3c0 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66 2c  x1d, 0x1e, 0x1f,
ad3d0 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  .  0x00, 0x01, 0
ad3e0 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c  x02, 0x03, 0x04,
ad3f0 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30   0x05, 0x06, 0x0
ad400 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c  7,.  0x08, 0x09,
ad410 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30   0x0a, 0x0b, 0x0
ad420 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30  c, 0x0d, 0x0e, 0
ad430 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30  x0f,.  0x00, 0x0
ad440 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30  1, 0x02, 0x03, 0
ad450 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c  x04, 0x05, 0x06,
ad460 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 30   0x07,.  0x00, 0
ad470 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c  x01, 0x02, 0x03,
ad480 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
ad490 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64  0, 0x00,.};...#d
ad4a0 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 38  efine WRITE_UTF8
ad4b0 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20  (zOut, c) {     
ad4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad4d0 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 30       \.  if( c<0
ad4e0 78 30 30 30 38 30 20 29 7b 20 20 20 20 20 20 20  x00080 ){       
ad4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad510 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
ad520 38 29 28 63 26 30 78 46 46 29 3b 20 20 20 20 20  8)(c&0xFF);     
ad530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad540 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20         \.  }    
ad550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad580 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30  \.  else if( c<0
ad590 78 30 30 38 30 30 20 29 7b 20 20 20 20 20 20 20  x00800 ){       
ad5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad5b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
ad5c0 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20  zOut++ = 0xC0 + 
ad5d0 28 75 38 29 28 28 63 3e 3e 36 29 26 30 78 31 46  (u8)((c>>6)&0x1F
ad5e0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ad5f0 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ad600 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20  = 0x80 + (u8)(c 
ad610 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
ad620 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
ad630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad660 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28      \.  else if(
ad670 20 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20 20   c<0x10000 ){   
ad680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad690 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ad6a0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45     *zOut++ = 0xE
ad6b0 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29  0 + (u8)((c>>12)
ad6c0 26 30 78 30 46 29 3b 20 20 20 20 20 20 20 20 20  &0x0F);         
ad6d0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
ad6e0 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38  t++ = 0x80 + (u8
ad6f0 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29  )((c>>6) & 0x3F)
ad700 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c  ;              \
ad710 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
ad720 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30  x80 + (u8)(c & 0
ad730 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
ad740 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73          \.  }els
ad750 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e{              
ad760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad780 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
ad790 20 30 78 46 30 20 2b 20 28 75 38 29 28 28 63 3e   0xF0 + (u8)((c>
ad7a0 3e 31 38 29 20 26 20 30 78 30 37 29 3b 20 20 20  >18) & 0x07);   
ad7b0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ad7c0 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b  *zOut++ = 0x80 +
ad7d0 20 28 75 38 29 28 28 63 3e 3e 31 32 29 20 26 20   (u8)((c>>12) & 
ad7e0 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20  0x3F);          
ad7f0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ad800 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28   = 0x80 + (u8)((
ad810 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20  c>>6) & 0x3F);  
ad820 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ad830 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30    *zOut++ = 0x80
ad840 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46   + (u8)(c & 0x3F
ad850 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ad860 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
ad870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad8a0 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45  }..#define WRITE
ad8b0 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63  _UTF16LE(zOut, c
ad8c0 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  ) {             
ad8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8e0 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63         \.  if( c
ad8f0 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20  <=0xFFFF ){     
ad900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad920 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ad930 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
ad940 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20  )(c&0x00FF);    
ad950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad970 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ad980 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30   = (u8)((c>>8)&0
ad990 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
ad9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c           \.  }el
ad9c0 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  se{             
ad9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ada00 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
ada10 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30  u8)(((c>>10)&0x0
ada20 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30  03F) + (((c-0x10
ada30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30  000)>>10)&0x00C0
ada40 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74  ));  \.    *zOut
ada50 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 38  ++ = (u8)(0x00D8
ada60 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29   + (((c-0x10000)
ada70 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20  >>18)&0x03));   
ada80 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ada90 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
adaa0 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  c&0x00FF);      
adab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adad0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
adae0 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28   (u8)(0x00DC + (
adaf0 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20  (c>>8)&0x03));  
adb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb10 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20         \.  }    
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
adb60 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f  ..#define WRITE_
adb70 55 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29  UTF16BE(zOut, c)
adb80 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
adb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adba0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c        \.  if( c<
adbb0 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20  =0xFFFF ){      
adbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbe0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
adbf0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
adc00 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b  ((c>>8)&0x00FF);
adc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc30 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
adc40 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29  = (u8)(c&0x00FF)
adc50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
adc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc70 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73          \.  }els
adc80 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e{              
adc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
adcc0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
adcd0 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 28 63  8)(0x00D8 + (((c
adce0 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26 30  -0x10000)>>18)&0
adcf0 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20  x03));          
add00 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
add10 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30  + = (u8)(((c>>10
add20 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28 63  )&0x003F) + (((c
add30 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30  -0x10000)>>10)&0
add40 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20  x00C0));  \.    
add50 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30  *zOut++ = (u8)(0
add60 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26  x00DC + ((c>>8)&
add70 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20  0x03));         
add80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add90 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
adda0 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20  (u8)(c&0x00FF); 
addb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addd0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
adde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade10 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
ade20 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54  .#define READ_UT
ade30 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45 52 4d 2c  F16LE(zIn, TERM,
ade40 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20 20   c){            
ade50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade60 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28         \.  c = (
ade70 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20  *zIn++);        
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adeb0 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b  .  c += ((*zIn++
adec0 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20  )<<8);          
aded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adef0 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63         \.  if( c
adf00 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78  >=0xD800 && c<0x
adf10 45 30 30 30 20 26 26 20 54 45 52 4d 20 29 7b 20  E000 && TERM ){ 
adf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adf40 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a  .    int c2 = (*
adf50 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20  zIn++);         
adf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf80 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20         \.    c2 
adf90 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29  += ((*zIn++)<<8)
adfa0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
adfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adfd0 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30  .    c = (c2&0x0
adfe0 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33  3FF) + ((c&0x003
adff0 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30  F)<<10) + (((c&0
ae000 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c  x03C0)+0x0040)<<
ae010 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20  10);   \.  }    
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ae060 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44  .}..#define READ
ae070 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 54 45  _UTF16BE(zIn, TE
ae080 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20  RM, c){         
ae090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0a0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20            \.  c 
ae0b0 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b  = ((*zIn++)<<8);
ae0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0f0 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e    \.  c += (*zIn
ae100 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  ++);            
ae110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae130 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
ae140 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63  ( c>=0xD800 && c
ae150 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d 20  <0xE000 && TERM 
ae160 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae180 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d    \.    int c2 =
ae190 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20   ((*zIn++)<<8); 
ae1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1c0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ae1d0 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20  c2 += (*zIn++); 
ae1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae210 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26    \.    c = (c2&
ae220 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 78  0x03FF) + ((c&0x
ae230 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28  003F)<<10) + (((
ae240 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 30  c&0x03C0)+0x0040
ae250 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20  )<<10);   \.  } 
ae260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae2a0 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61    \.}../*.** Tra
ae2b0 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20  nslate a single 
ae2c0 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e  UTF-8 character.
ae2d0 20 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69    Return the uni
ae2e0 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  code value..**.*
ae2f0 2a 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c 61  * During transla
ae300 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 61  tion, assume tha
ae310 74 20 74 68 65 20 62 79 74 65 20 74 68 61 74 20  t the byte that 
ae320 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20  zTerm points.** 
ae330 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a  is a 0x00..**.**
ae340 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72   Write a pointer
ae350 20 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e 72   to the next unr
ae360 65 61 64 20 62 79 74 65 20 62 61 63 6b 20 69 6e  ead byte back in
ae370 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a  to *pzNext..**.*
ae380 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c  * Notes On Inval
ae390 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20  id UTF-8:.**.** 
ae3a0 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65   *  This routine
ae3b0 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20   never allows a 
ae3c0 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 20  7-bit character 
ae3d0 28 30 78 30 30 20 74 68 72 6f 75 67 68 20 30 78  (0x00 through 0x
ae3e0 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65  7f) to.**     be
ae3f0 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d 75   encoded as a mu
ae400 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74  lti-byte charact
ae410 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62  er.  Any multi-b
ae420 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74 68  yte character th
ae430 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d 70  at.**     attemp
ae440 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76  ts to encode a v
ae450 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78 30  alue between 0x0
ae460 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72 65  0 and 0x7f is re
ae470 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66 64  ndered as 0xfffd
ae480 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73  ..**.**  *  This
ae490 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61   routine never a
ae4a0 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73 75  llows a UTF16 su
ae4b0 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74 6f  rrogate value to
ae4c0 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20   be encoded..** 
ae4d0 20 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d 62      If a multi-b
ae4e0 79 74 65 20 63 68 61 72 61 63 74 65 72 20 61 74  yte character at
ae4f0 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 65  tempts to encode
ae500 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e   a value between
ae510 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20 61  .**     0xd800 a
ae520 6e 64 20 30 78 65 30 30 30 20 74 68 65 6e 20 69  nd 0xe000 then i
ae530 74 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 73  t is rendered as
ae540 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20   0xfffd..**.**  
ae550 2a 20 20 42 79 74 65 73 20 69 6e 20 74 68 65 20  *  Bytes in the 
ae560 72 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74 68  range of 0x80 th
ae570 72 6f 75 67 68 20 30 78 62 66 20 77 68 69 63 68  rough 0xbf which
ae580 20 6f 63 63 75 72 20 61 73 20 74 68 65 20 66 69   occur as the fi
ae590 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 20  rst.**     byte 
ae5a0 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20 61  of a character a
ae5b0 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  re interpreted a
ae5c0 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 68  s single-byte ch
ae5d0 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20  aracters.**     
ae5e0 61 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73 20  and rendered as 
ae5f0 74 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e 20  themselves even 
ae600 74 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 20  though they are 
ae610 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20  technically.**  
ae620 20 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72 61     invalid chara
ae630 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20  cters..**.**  * 
ae640 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63   This routine ac
ae650 63 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 74  cepts an infinit
ae660 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66  e number of diff
ae670 65 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f 64  erent UTF8 encod
ae680 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20  ings.**     for 
ae690 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20 30  unicode values 0
ae6a0 78 38 30 20 61 6e 64 20 67 72 65 61 74 65 72 2e  x80 and greater.
ae6b0 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e    It do not chan
ae6c0 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a  ge over-length.*
ae6d0 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 20  *     encodings 
ae6e0 74 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f 6d  to 0xfffd as som
ae6f0 65 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d  e systems recomm
ae700 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  end..*/.#define 
ae710 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a  READ_UTF8(zIn, z
ae720 54 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20 20  Term, c)        
ae730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae740 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e     \.  c = *(zIn
ae750 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  ++);            
ae760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae780 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20  \.  if( c>=0xc0 
ae790 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
ae7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ae7c0 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74     c = sqlite3Ut
ae7d0 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d  f8Trans1[c-0xc0]
ae7e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ae7f0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ae800 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72  while( zIn!=zTer
ae810 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 63  m && (*zIn & 0xc
ae820 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 20  0)==0x80 ){     
ae830 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 63         \.      c
ae840 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33   = (c<<6) + (0x3
ae850 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20  f & *(zIn++));  
ae860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae870 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 20      \.    }     
ae880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae8b0 20 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38   \.    if( c<0x8
ae8c0 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
ae8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ae8f0 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78          || (c&0x
ae900 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30  FFFFF800)==0xD80
ae910 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
ae920 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ae930 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46       || (c&0xFFF
ae940 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29  FFFFE)==0xFFFE )
ae950 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d  {  c = 0xFFFD; }
ae960 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51          \.  }.SQ
ae970 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ae980 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
ae990 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  (.  const unsign
ae9a0 65 64 20 63 68 61 72 20 2a 7a 49 6e 2c 20 20 20  ed char *zIn,   
ae9b0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
ae9c0 65 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61  e of UTF-8 chara
ae9d0 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  cter */.  const 
ae9e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a  unsigned char **
ae9f0 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 69  pzNext    /* Wri
aea00 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61  te first byte pa
aea10 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68 65  st UTF-8 char he
aea20 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63  re */.){.  int c
aea30 3b 0a 0a 20 20 2f 2a 20 53 61 6d 65 20 61 73 20  ;..  /* Same as 
aea40 52 45 41 44 5f 55 54 46 38 28 29 20 61 62 6f 76  READ_UTF8() abov
aea50 65 20 62 75 74 20 77 69 74 68 6f 75 74 20 74 68  e but without th
aea60 65 20 7a 54 65 72 6d 20 70 61 72 61 6d 65 74 65  e zTerm paramete
aea70 72 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73  r..  ** For this
aea80 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 73 73   routine, we ass
aea90 75 6d 65 20 74 68 65 20 55 54 46 38 20 73 74 72  ume the UTF8 str
aeaa0 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20 7a 65  ing is always ze
aeab0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 20  ro-terminated.. 
aeac0 20 2a 2f 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b   */.  c = *(zIn+
aead0 2b 29 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78 63  +);.  if( c>=0xc
aeae0 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c  0 ){.    c = sql
aeaf0 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 63  ite3Utf8Trans1[c
aeb00 2d 30 78 63 30 5d 3b 0a 20 20 20 20 77 68 69 6c  -0xc0];.    whil
aeb10 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29  e( (*zIn & 0xc0)
aeb20 3d 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20 20  ==0x80 ){.      
aeb30 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78  c = (c<<6) + (0x
aeb40 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0a  3f & *(zIn++));.
aeb50 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 3c      }.    if( c<
aeb60 30 78 38 30 0a 20 20 20 20 20 20 20 20 7c 7c 20  0x80.        || 
aeb70 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d  (c&0xFFFFF800)==
aeb80 30 78 44 38 30 30 0a 20 20 20 20 20 20 20 20 7c  0xD800.        |
aeb90 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45 29  | (c&0xFFFFFFFE)
aeba0 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20 3d  ==0xFFFE ){  c =
aebb0 20 30 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a 20   0xFFFD; }.  }. 
aebc0 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b 0a   *pzNext = zIn;.
aebd0 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a    return c;.}...
aebe0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54  ../*.** If the T
aebf0 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 6d  RANSLATE_TRACE m
aec00 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c  acro is defined,
aec10 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65 61   the value of ea
aec20 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69  ch Mem is.** pri
aec30 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72 20 6f  nted on stderr o
aec40 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f 20 61  n the way into a
aec50 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65  nd out of sqlite
aec60 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74  3VdbeMemTranslat
aec70 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66  e()..*/ ./* #def
aec80 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 52  ine TRANSLATE_TR
aec90 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65  ACE 1 */..#ifnde
aeca0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
aecb0 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  F16./*.** This r
aecc0 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d  outine transform
aecd0 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74  s the internal t
aece0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65  ext encoding use
aecf0 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20  d by pMem to.** 
aed00 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74 20 69  desiredEnc. It i
aed10 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68  s an error if th
aed20 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c 72 65  e string is alre
aed30 61 64 79 20 6f 66 20 74 68 65 20 64 65 73 69 72  ady of the desir
aed40 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20  ed.** encoding, 
aed50 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73  or if *pMem does
aed60 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73   not contain a s
aed70 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a  tring value..*/.
aed80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
aed90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
aeda0 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a  mTranslate(Mem *
aedb0 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72 65 64  pMem, u8 desired
aedc0 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b  Enc){.  int len;
aedd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aede0 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
aedf0 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75 74 20  ength of output 
aee00 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 73 20  string in bytes 
aee10 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
aee20 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20  ar *zOut;       
aee30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
aee40 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20  tput buffer */. 
aee50 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
aee60 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  zIn;            
aee70 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20         /* Input 
aee80 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e  iterator */.  un
aee90 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65  signed char *zTe
aeea0 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
aeeb0 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e      /* End of in
aeec0 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  put */.  unsigne
aeed0 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20  d char *z;      
aeee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
aeef0 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f  * Output iterato
aef00 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  r */.  unsigned 
aef10 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 74  int c;..  assert
aef20 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
aef30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
aef40 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
aef50 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
aef60 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
aef70 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73 65 72  M_Str );.  asser
aef80 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65  t( pMem->enc!=de
aef90 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 73  siredEnc );.  as
aefa0 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21  sert( pMem->enc!
aefb0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
aefc0 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23  pMem->n>=0 );..#
aefd0 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53  if defined(TRANS
aefe0 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 64  LATE_TRACE) && d
aeff0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
af000 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61  BUG).  {.    cha
af010 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20  r zBuf[100];.   
af020 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50   sqlite3VdbeMemP
af030 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c  rettyPrint(pMem,
af040 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69   zBuf);.    fpri
af050 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49 4e 50  ntf(stderr, "INP
af060 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66  UT:  %s\n", zBuf
af070 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
af080 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73   /* If the trans
af090 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65  lation is betwee
af0a0 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20  n UTF-16 little 
af0b0 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20  and big endian, 
af0c0 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74  then .  ** all t
af0d0 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 20  hat is required 
af0e0 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 20 62  is to swap the b
af0f0 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20  yte order. This 
af100 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0a  case is handled.
af110 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c 79    ** differently
af120 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73   from the others
af130 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65  ..  */.  if( pMe
af140 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55  m->enc!=SQLITE_U
af150 54 46 38 20 26 26 20 64 65 73 69 72 65 64 45 6e  TF8 && desiredEn
af160 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c!=SQLITE_UTF8 )
af170 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b 0a 20  {.    u8 temp;. 
af180 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72     int rc;.    r
af190 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
af1a0 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
af1b0 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72  pMem);.    if( r
af1c0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
af1d0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
af1e0 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
af1f0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
af200 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
af210 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 38   }.    zIn = (u8
af220 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a  *)pMem->z;.    z
af230 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d  Term = &zIn[pMem
af240 2d 3e 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68 69  ->n&~1];.    whi
af250 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b  le( zIn<zTerm ){
af260 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a  .      temp = *z
af270 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d  In;.      *zIn =
af280 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20   *(zIn+1);.     
af290 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a   zIn++;.      *z
af2a0 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20  In++ = temp;.   
af2b0 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63   }.    pMem->enc
af2c0 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20   = desiredEnc;. 
af2d0 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74     goto translat
af2e0 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e_out;.  }..  /*
af2f0 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68 65 20   Set len to the 
af300 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
af310 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
af320 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62   in the output b
af330 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20  uffer. */.  if( 
af340 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49  desiredEnc==SQLI
af350 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 2f  TE_UTF8 ){.    /
af360 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e  * When convertin
af370 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74  g from UTF-16, t
af380 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74  he maximum growt
af390 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20  h results from. 
af3a0 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e     ** translatin
af3b0 67 20 61 20 32 2d 62 79 74 65 20 63 68 61 72 61  g a 2-byte chara
af3c0 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79 74 65  cter to a 4-byte
af3d0 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
af3e0 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c  ..    ** A singl
af3f0 65 20 62 79 74 65 20 69 73 20 72 65 71 75 69 72  e byte is requir
af400 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75  ed for the outpu
af410 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20  t string.    ** 
af420 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a  nul-terminator..
af430 20 20 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d      */.    pMem-
af440 3e 6e 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c 65  >n &= ~1;.    le
af450 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20  n = pMem->n * 2 
af460 2b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  + 1;.  }else{.  
af470 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72    /* When conver
af480 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 20  ting from UTF-8 
af490 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20 6d 61  to UTF-16 the ma
af4a0 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 20  ximum growth is 
af4b0 63 61 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 68  caused.    ** wh
af4c0 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54 46 2d  en a 1-byte UTF-
af4d0 38 20 63 68 61 72 61 63 74 65 72 20 69 73 20 74  8 character is t
af4e0 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61  ranslated into a
af4f0 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36 0a 20   2-byte UTF-16. 
af500 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72 2e     ** character.
af510 20 54 77 6f 20 62 79 74 65 73 20 61 72 65 20 72   Two bytes are r
af520 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f  equired in the o
af530 75 74 70 75 74 20 62 75 66 66 65 72 20 66 6f 72  utput buffer for
af540 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d   the.    ** nul-
af550 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20  terminator..    
af560 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65  */.    len = pMe
af570 6d 2d 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20  m->n * 2 + 2;.  
af580 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20  }..  /* Set zIn 
af590 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
af5a0 73 74 61 72 74 20 6f 66 20 74 68 65 20 69 6e 70  start of the inp
af5b0 75 74 20 62 75 66 66 65 72 20 61 6e 64 20 7a 54  ut buffer and zT
af5c0 65 72 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20  erm to point 1. 
af5d0 20 2a 2a 20 62 79 74 65 20 70 61 73 74 20 74 68   ** byte past th
af5e0 65 20 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  e end..  **.  **
af5f0 20 56 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 69   Variable zOut i
af600 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61  s set to point a
af610 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  t the output buf
af620 66 65 72 2c 20 73 70 61 63 65 20 6f 62 74 61 69  fer, space obtai
af630 6e 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71  ned.  ** from sq
af640 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a  lite3_malloc()..
af650 20 20 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 38    */.  zIn = (u8
af660 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65  *)pMem->z;.  zTe
af670 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e  rm = &zIn[pMem->
af680 6e 5d 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c  n];.  zOut = sql
af690 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
af6a0 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a  pMem->db, len);.
af6b0 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20    if( !zOut ){. 
af6c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
af6d0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20  _NOMEM;.  }.  z 
af6e0 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20 70  = zOut;..  if( p
af6f0 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45  Mem->enc==SQLITE
af700 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 69 66 28  _UTF8 ){.    if(
af710 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
af720 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20  ITE_UTF16LE ){. 
af730 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e       /* UTF-8 ->
af740 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65   UTF-16 Little-e
af750 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77  ndian */.      w
af760 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20  hile( zIn<zTerm 
af770 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20  ){.        /* c 
af780 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
af790 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63  d(zIn, zTerm, (c
af7a0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b  onst u8**)&zIn);
af7b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44   */.        READ
af7c0 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d  _UTF8(zIn, zTerm
af7d0 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52  , c);.        WR
af7e0 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63  ITE_UTF16LE(z, c
af7f0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
af800 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
af810 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d  rt( desiredEnc==
af820 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29  SQLITE_UTF16BE )
af830 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38  ;.      /* UTF-8
af840 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 65   -> UTF-16 Big-e
af850 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77  ndian */.      w
af860 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20  hile( zIn<zTerm 
af870 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20  ){.        /* c 
af880 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
af890 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63  d(zIn, zTerm, (c
af8a0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b  onst u8**)&zIn);
af8b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44   */.        READ
af8c0 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d  _UTF8(zIn, zTerm
af8d0 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52  , c);.        WR
af8e0 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 63  ITE_UTF16BE(z, c
af8f0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
af900 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28  .    pMem->n = (
af910 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a  int)(z - zOut);.
af920 20 20 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20      *z++ = 0;.  
af930 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
af940 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53  t( desiredEnc==S
af950 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20  QLITE_UTF8 );.  
af960 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d    if( pMem->enc=
af970 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
af980 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d  ){.      /* UTF-
af990 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  16 Little-endian
af9a0 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20   -> UTF-8 */.   
af9b0 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54     while( zIn<zT
af9c0 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52  erm ){.        R
af9d0 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c  EAD_UTF16LE(zIn,
af9e0 20 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b 20   zIn<zTerm, c); 
af9f0 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
afa00 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20  TF8(z, c);.     
afa10 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
afa20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69      /* UTF-16 Bi
afa30 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d  g-endian -> UTF-
afa40 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  8 */.      while
afa50 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
afa60 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31         READ_UTF1
afa70 36 42 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 65  6BE(zIn, zIn<zTe
afa80 72 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20  rm, c); .       
afa90 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63   WRITE_UTF8(z, c
afaa0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
afab0 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28  .    pMem->n = (
afac0 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a  int)(z - zOut);.
afad0 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20 20    }.  *z = 0;.  
afae0 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 6e  assert( (pMem->n
afaf0 2b 28 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51  +(desiredEnc==SQ
afb00 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 29 3c  LITE_UTF8?1:2))<
afb10 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 71 6c 69 74  =len );..  sqlit
afb20 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
afb30 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e  (pMem);.  pMem->
afb40 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 53  flags &= ~(MEM_S
afb50 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45  tatic|MEM_Dyn|ME
afb60 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 70 4d 65 6d  M_Ephem);.  pMem
afb70 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64 45  ->enc = desiredE
afb80 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  nc;.  pMem->flag
afb90 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c 4d  s |= (MEM_Term|M
afba0 45 4d 5f 44 79 6e 29 3b 0a 20 20 70 4d 65 6d 2d  EM_Dyn);.  pMem-
afbb0 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75 74  >z = (char*)zOut
afbc0 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  ;.  pMem->zMallo
afbd0 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 0a 74 72  c = pMem->z;..tr
afbe0 61 6e 73 6c 61 74 65 5f 6f 75 74 3a 0a 23 69 66  anslate_out:.#if
afbf0 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41   defined(TRANSLA
afc00 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66  TE_TRACE) && def
afc10 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
afc20 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20  G).  {.    char 
afc30 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73  zBuf[100];.    s
afc40 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65  qlite3VdbeMemPre
afc50 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a  ttyPrint(pMem, z
afc60 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  Buf);.    fprint
afc70 66 28 73 74 64 65 72 72 2c 20 22 4f 55 54 50 55  f(stderr, "OUTPU
afc80 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b  T: %s\n", zBuf);
afc90 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
afca0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
afcb0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
afcc0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 66 6f 72  utine checks for
afcd0 20 61 20 62 79 74 65 2d 6f 72 64 65 72 20 6d 61   a byte-order ma
afce0 72 6b 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  rk at the beginn
afcf0 69 6e 67 20 6f 66 20 74 68 65 20 0a 2a 2a 20 55  ing of the .** U
afd00 54 46 2d 31 36 20 73 74 72 69 6e 67 20 73 74 6f  TF-16 string sto
afd10 72 65 64 20 69 6e 20 2a 70 4d 65 6d 2e 20 49 66  red in *pMem. If
afd20 20 6f 6e 65 20 69 73 20 70 72 65 73 65 6e 74 2c   one is present,
afd30 20 69 74 20 69 73 20 72 65 6d 6f 76 65 64 20 61   it is removed a
afd40 6e 64 0a 2a 2a 20 74 68 65 20 65 6e 63 6f 64 69  nd.** the encodi
afd50 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 61 64  ng of the Mem ad
afd60 6a 75 73 74 65 64 2e 20 54 68 69 73 20 72 6f 75  justed. This rou
afd70 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 64 6f  tine does not do
afd80 20 61 6e 79 0a 2a 2a 20 62 79 74 65 2d 73 77 61   any.** byte-swa
afd90 70 70 69 6e 67 2c 20 69 74 20 6a 75 73 74 20 73  pping, it just s
afda0 65 74 73 20 4d 65 6d 2e 65 6e 63 20 61 70 70 72  ets Mem.enc appr
afdb0 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a  opriately..**.**
afdc0 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   The allocation 
afdd0 28 73 74 61 74 69 63 2c 20 64 79 6e 61 6d 69 63  (static, dynamic
afde0 20 65 74 63 2e 29 20 61 6e 64 20 65 6e 63 6f 64   etc.) and encod
afdf0 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 6d  ing of the Mem m
afe00 61 79 20 62 65 0a 2a 2a 20 63 68 61 6e 67 65 64  ay be.** changed
afe10 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
afe20 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
afe30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
afe40 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d  VdbeMemHandleBom
afe50 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69  (Mem *pMem){.  i
afe60 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
afe70 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 3d 20 30 3b  K;.  u8 bom = 0;
afe80 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ..  assert( pMem
afe90 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  ->n>=0 );.  if( 
afea0 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 20  pMem->n>1 ){.   
afeb0 20 75 38 20 62 31 20 3d 20 2a 28 75 38 20 2a 29   u8 b1 = *(u8 *)
afec0 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 20  pMem->z;.    u8 
afed0 62 32 20 3d 20 2a 28 28 28 75 38 20 2a 29 70 4d  b2 = *(((u8 *)pM
afee0 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 20  em->z) + 1);.   
afef0 20 69 66 28 20 62 31 3d 3d 30 78 46 45 20 26 26   if( b1==0xFE &&
aff00 20 62 32 3d 3d 30 78 46 46 20 29 7b 0a 20 20 20   b2==0xFF ){.   
aff10 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f     bom = SQLITE_
aff20 55 54 46 31 36 42 45 3b 0a 20 20 20 20 7d 0a 20  UTF16BE;.    }. 
aff30 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 46 20     if( b1==0xFF 
aff40 26 26 20 62 32 3d 3d 30 78 46 45 20 29 7b 0a 20  && b2==0xFE ){. 
aff50 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54       bom = SQLIT
aff60 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 7d  E_UTF16LE;.    }
aff70 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 62 6f  .  }.  .  if( bo
aff80 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  m ){.    rc = sq
aff90 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
affa0 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b  Writeable(pMem);
affb0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
affc0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
affd0 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 20  pMem->n -= 2;.  
affe0 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 6d      memmove(pMem
afff0 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 5d  ->z, &pMem->z[2]
b0000 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20  , pMem->n);.    
b0010 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
b0020 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  n] = '\0';.     
b0030 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e   pMem->z[pMem->n
b0040 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  +1] = '\0';.    
b0050 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
b0060 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20   MEM_Term;.     
b0070 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f 6d   pMem->enc = bom
b0080 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
b0090 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
b00a0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
b00b0 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _UTF16 */../*.**
b00c0 20 70 5a 20 69 73 20 61 20 55 54 46 2d 38 20 65   pZ is a UTF-8 e
b00d0 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20 73  ncoded unicode s
b00e0 74 72 69 6e 67 2e 20 49 66 20 6e 42 79 74 65 20  tring. If nByte 
b00f0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72  is less than zer
b0100 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 65  o,.** return the
b0110 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f   number of unico
b0120 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  de characters in
b0130 20 70 5a 20 75 70 20 74 6f 20 28 62 75 74 20 6e   pZ up to (but n
b0140 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 0a 2a 2a  ot including).**
b0150 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30 20   the first 0x00 
b0160 62 79 74 65 2e 20 49 66 20 6e 42 79 74 65 20 69  byte. If nByte i
b0170 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 20  s not less than 
b0180 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20 74 68 65  zero, return the
b0190 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e  .** number of un
b01a0 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73  icode characters
b01b0 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e 42   in the first nB
b01c0 79 74 65 20 6f 66 20 70 5a 20 28 6f 72 20 75 70  yte of pZ (or up
b01d0 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66 69 72 73   to .** the firs
b01e0 74 20 30 78 30 30 2c 20 77 68 69 63 68 65 76 65  t 0x00, whicheve
b01f0 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a  r comes first)..
b0200 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b0210 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66  E int sqlite3Utf
b0220 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63  8CharLen(const c
b0230 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 42  har *zIn, int nB
b0240 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20  yte){.  int r = 
b0250 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  0;.  const u8 *z
b0260 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a 49   = (const u8*)zI
b0270 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  n;.  const u8 *z
b0280 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e 42 79 74  Term;.  if( nByt
b0290 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a 54 65 72  e>=0 ){.    zTer
b02a0 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a 20  m = &z[nByte];. 
b02b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 54 65 72   }else{.    zTer
b02c0 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 28  m = (const u8*)(
b02d0 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  -1);.  }.  asser
b02e0 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a 20  t( z<=zTerm );. 
b02f0 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30 20 26 26   while( *z!=0 &&
b0300 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20   z<zTerm ){.    
b0310 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
b0320 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 20  (z);.    r++;.  
b0330 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a  }.  return r;.}.
b0340 0a 2f 2a 20 54 68 69 73 20 74 65 73 74 20 66 75  ./* This test fu
b0350 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75  nction is not cu
b0360 72 72 65 6e 74 6c 79 20 75 73 65 64 20 62 79 20  rrently used by 
b0370 74 68 65 20 61 75 74 6f 6d 61 74 65 64 20 74 65  the automated te
b0380 73 74 2d 73 75 69 74 65 2e 20 0a 2a 2a 20 48 65  st-suite. .** He
b0390 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c 79 20 61  nce it is only a
b03a0 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 65 62 75  vailable in debu
b03b0 67 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  g builds..*/.#if
b03c0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
b03d0 54 45 53 54 29 20 26 26 20 64 65 66 69 6e 65 64  TEST) && defined
b03e0 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f  (SQLITE_DEBUG)./
b03f0 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 55  *.** Translate U
b0400 54 46 2d 38 20 74 6f 20 55 54 46 2d 38 2e 0a 2a  TF-8 to UTF-8..*
b0410 2a 0a 2a 2a 20 54 68 69 73 20 68 61 73 20 74 68  *.** This has th
b0420 65 20 65 66 66 65 63 74 20 6f 66 20 6d 61 6b 69  e effect of maki
b0430 6e 67 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ng sure that the
b0440 20 73 74 72 69 6e 67 20 69 73 20 77 65 6c 6c 2d   string is well-
b0450 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 2e  formed.** UTF-8.
b0460 20 20 4d 69 73 63 6f 64 65 64 20 63 68 61 72 61    Miscoded chara
b0470 63 74 65 72 73 20 61 72 65 20 72 65 6d 6f 76 65  cters are remove
b0480 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 61  d..**.** The tra
b0490 6e 73 6c 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65  nslation is done
b04a0 20 69 6e 2d 70 6c 61 63 65 20 28 73 69 6e 63 65   in-place (since
b04b0 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69 62 6c   it is impossibl
b04c0 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 72  e for the.** cor
b04d0 72 65 63 74 20 55 54 46 2d 38 20 65 6e 63 6f 64  rect UTF-8 encod
b04e0 69 6e 67 20 74 6f 20 62 65 20 6c 6f 6e 67 65 72  ing to be longer
b04f0 20 74 68 61 6e 20 61 20 6d 61 6c 66 6f 72 6d 65   than a malforme
b0500 64 20 65 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f 0a  d encoding)..*/.
b0510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b0520 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 54 6f  nt sqlite3Utf8To
b0530 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  8(unsigned char 
b0540 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65  *zIn){.  unsigne
b0550 64 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20 7a  d char *zOut = z
b0560 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  In;.  unsigned c
b0570 68 61 72 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49  har *zStart = zI
b0580 6e 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20 77  n;.  u32 c;..  w
b0590 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b 0a  hile( zIn[0] ){.
b05a0 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
b05b0 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 28 63 6f  tf8Read(zIn, (co
b05c0 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a  nst u8**)&zIn);.
b05d0 20 20 20 20 69 66 28 20 63 21 3d 30 78 66 66 66      if( c!=0xfff
b05e0 64 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54 45  d ){.      WRITE
b05f0 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a  _UTF8(zOut, c);.
b0600 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75      }.  }.  *zOu
b0610 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  t = 0;.  return 
b0620 28 69 6e 74 29 28 7a 4f 75 74 20 2d 20 7a 53 74  (int)(zOut - zSt
b0630 61 72 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  art);.}.#endif..
b0640 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
b0650 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20  MIT_UTF16./*.** 
b0660 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31 36  Convert a UTF-16
b0670 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6e   string in the n
b0680 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 69  ative encoding i
b0690 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 69  nto a UTF-8 stri
b06a0 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f  ng..** Memory to
b06b0 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38 20   hold the UTF-8 
b06c0 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e  string is obtain
b06d0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
b06e0 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 0a  malloc and must.
b06f0 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20 74  ** be freed by t
b0700 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
b0710 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c 20  ion..**.** NULL 
b0720 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
b0730 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f 63  here is an alloc
b0740 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  ation error..*/.
b0750 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
b0760 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31  har *sqlite3Utf1
b0770 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 62  6to8(sqlite3 *db
b0780 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c  , const void *z,
b0790 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d   int nByte){.  M
b07a0 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 26  em m;.  memset(&
b07b0 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29  m, 0, sizeof(m))
b07c0 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a 20  ;.  m.db = db;. 
b07d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
b07e0 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 79  etStr(&m, z, nBy
b07f0 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  te, SQLITE_UTF16
b0800 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53  NATIVE, SQLITE_S
b0810 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65  TATIC);.  sqlite
b0820 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
b0830 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f 55  ing(&m, SQLITE_U
b0840 54 46 38 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  TF8);.  if( db->
b0850 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
b0860 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
b0870 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20  emRelease(&m);. 
b0880 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d 0a     m.z = 0;.  }.
b0890 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61    assert( (m.fla
b08a0 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d  gs & MEM_Term)!=
b08b0 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
b08c0 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72  ailed );.  asser
b08d0 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45  t( (m.flags & ME
b08e0 4d 5f 53 74 72 29 21 3d 30 20 7c 7c 20 64 62 2d  M_Str)!=0 || db-
b08f0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
b0900 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c 61  .  return (m.fla
b0910 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30  gs & MEM_Dyn)!=0
b0920 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65 33   ? m.z : sqlite3
b0930 44 62 53 74 72 44 75 70 28 64 62 2c 20 6d 2e 7a  DbStrDup(db, m.z
b0940 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  );.}../*.** Conv
b0950 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 69  ert a UTF-8 stri
b0960 6e 67 20 74 6f 20 74 68 65 20 55 54 46 2d 31 36  ng to the UTF-16
b0970 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66   encoding specif
b0980 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
b0990 0a 2a 2a 20 65 6e 63 2e 20 41 20 70 6f 69 6e 74  .** enc. A point
b09a0 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 74  er to the new st
b09b0 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65 64  ring is returned
b09c0 2c 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20  , and the value 
b09d0 6f 66 20 2a 70 6e 4f 75 74 0a 2a 2a 20 69 73 20  of *pnOut.** is 
b09e0 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74  set to the lengt
b09f0 68 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65  h of the returne
b0a00 64 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65  d string in byte
b0a10 73 2e 20 54 68 65 20 63 61 6c 6c 20 73 68 6f 75  s. The call shou
b0a20 6c 64 0a 2a 2a 20 61 72 72 61 6e 67 65 20 74 6f  ld.** arrange to
b0a30 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46   call sqlite3DbF
b0a40 72 65 65 28 29 20 6f 6e 20 74 68 65 20 72 65 74  ree() on the ret
b0a50 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 77 68  urned pointer wh
b0a60 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20 6c  en it is.** no l
b0a70 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e 0a  onger required..
b0a80 2a 2a 20 0a 2a 2a 20 49 66 20 61 20 6d 61 6c 6c  ** .** If a mall
b0a90 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  oc failure occur
b0aa0 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  s, NULL is retur
b0ab0 6e 65 64 20 61 6e 64 20 74 68 65 20 64 62 2e 6d  ned and the db.m
b0ac0 61 6c 6c 6f 63 46 61 69 6c 65 64 0a 2a 2a 20 66  allocFailed.** f
b0ad0 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 69 66 64  lag set..*/.#ifd
b0ae0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
b0af0 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 52  _STAT2.SQLITE_PR
b0b00 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
b0b10 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c 69  te3Utf8to16(sqli
b0b20 74 65 33 20 2a 64 62 2c 20 75 38 20 65 6e 63 2c  te3 *db, u8 enc,
b0b30 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
b0b40 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20   int *pnOut){.  
b0b50 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28  Mem m;.  memset(
b0b60 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29  &m, 0, sizeof(m)
b0b70 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a  );.  m.db = db;.
b0b80 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
b0b90 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 2c  SetStr(&m, z, n,
b0ba0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
b0bb0 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
b0bc0 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
b0bd0 65 6d 54 72 61 6e 73 6c 61 74 65 28 26 6d 2c 20  emTranslate(&m, 
b0be0 65 6e 63 29 20 29 7b 0a 20 20 20 20 61 73 73 65  enc) ){.    asse
b0bf0 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  rt( db->mallocFa
b0c00 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75  iled );.    retu
b0c10 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65  rn 0;.  }.  asse
b0c20 72 74 28 20 6d 2e 7a 3d 3d 6d 2e 7a 4d 61 6c 6c  rt( m.z==m.zMall
b0c30 6f 63 20 29 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d  oc );.  *pnOut =
b0c40 20 6d 2e 6e 3b 0a 20 20 72 65 74 75 72 6e 20 6d   m.n;.  return m
b0c50 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  .z;.}.#endif../*
b0c60 0a 2a 2a 20 7a 49 6e 20 69 73 20 61 20 55 54 46  .** zIn is a UTF
b0c70 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 63  -16 encoded unic
b0c80 6f 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c 65  ode string at le
b0c90 61 73 74 20 6e 43 68 61 72 20 63 68 61 72 61 63  ast nChar charac
b0ca0 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65  ters long..** Re
b0cb0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
b0cc0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
b0cd0 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 63  first nChar unic
b0ce0 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a  ode characters.*
b0cf0 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72 20  * in pZ.  nChar 
b0d00 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67 61  must be non-nega
b0d10 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tive..*/.SQLITE_
b0d20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b0d30 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28  te3Utf16ByteLen(
b0d40 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e 2c  const void *zIn,
b0d50 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 69   int nChar){.  i
b0d60 6e 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64  nt c;.  unsigned
b0d70 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 3d   char const *z =
b0d80 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d 20   zIn;.  int n = 
b0d90 30 3b 0a 20 20 0a 20 20 69 66 28 20 53 51 4c 49  0;.  .  if( SQLI
b0da0 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d  TE_UTF16NATIVE==
b0db0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29  SQLITE_UTF16BE )
b0dc0 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e  {.    while( n<n
b0dd0 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45  Char ){.      RE
b0de0 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31 2c  AD_UTF16BE(z, 1,
b0df0 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a   c);.      n++;.
b0e00 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
b0e10 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68 61     while( n<nCha
b0e20 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f  r ){.      READ_
b0e30 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 29  UTF16LE(z, 1, c)
b0e40 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20  ;.      n++;.   
b0e50 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
b0e60 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e 65  (int)(z-(unsigne
b0e70 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29 7a  d char const *)z
b0e80 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69  In);.}..#if defi
b0e90 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
b0ea0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
b0eb0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 72  ine is called fr
b0ec0 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74 20  om the TCL test 
b0ed0 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73 6c  function "transl
b0ee0 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a 2a  ate_selftest"..*
b0ef0 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61 74  * It checks that
b0f00 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73 20   the primitives 
b0f10 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20  for serializing 
b0f20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69 6e  and deserializin
b0f30 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20  g.** characters 
b0f40 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e 67  in each encoding
b0f50 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f 66   are inverses of
b0f60 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a   each other..*/.
b0f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
b0f80 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53 65  oid sqlite3UtfSe
b0f90 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20 20  lfTest(void){.  
b0fa0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c 20  unsigned int i, 
b0fb0 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  t;.  unsigned ch
b0fc0 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20 75  ar zBuf[20];.  u
b0fd0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b  nsigned char *z;
b0fe0 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69  .  int n;.  unsi
b0ff0 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 66  gned int c;..  f
b1000 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31  or(i=0; i<0x0011
b1010 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  0000; i++){.    
b1020 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52  z = zBuf;.    WR
b1030 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b 0a  ITE_UTF8(z, i);.
b1040 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d      n = (int)(z-
b1050 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72  zBuf);.    asser
b1060 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29  t( n>0 && n<=4 )
b1070 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a  ;.    z[0] = 0;.
b1080 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
b1090 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66    c = sqlite3Utf
b10a0 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 74 20  8Read(z, (const 
b10b0 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20  u8**)&z);.    t 
b10c0 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d  = i;.    if( i>=
b10d0 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44  0xD800 && i<=0xD
b10e0 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 44  FFF ) t = 0xFFFD
b10f0 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 46  ;.    if( (i&0xF
b1100 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45  FFFFFFE)==0xFFFE
b1110 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20   ) t = 0xFFFD;. 
b1120 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20     assert( c==t 
b1130 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
b1140 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20  z-zBuf)==n );.  
b1150 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30  }.  for(i=0; i<0
b1160 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b  x00110000; i++){
b1170 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38  .    if( i>=0xD8
b1180 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 29  00 && i<0xE000 )
b1190 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a   continue;.    z
b11a0 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49   = zBuf;.    WRI
b11b0 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29  TE_UTF16LE(z, i)
b11c0 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28  ;.    n = (int)(
b11d0 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73  z-zBuf);.    ass
b11e0 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34  ert( n>0 && n<=4
b11f0 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30   );.    z[0] = 0
b1200 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a  ;.    z = zBuf;.
b1210 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45      READ_UTF16LE
b1220 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 61  (z, 1, c);.    a
b1230 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20  ssert( c==i );. 
b1240 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42     assert( (z-zB
b1250 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20  uf)==n );.  }.  
b1260 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
b1270 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
b1280 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26   if( i>=0xD800 &
b1290 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e  & i<0xE000 ) con
b12a0 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a  tinue;.    z = z
b12b0 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55  Buf;.    WRITE_U
b12c0 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20  TF16BE(z, i);.  
b12d0 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42    n = (int)(z-zB
b12e0 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  uf);.    assert(
b12f0 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a   n>0 && n<=4 );.
b1300 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20      z[0] = 0;.  
b1310 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20    z = zBuf;.    
b1320 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20  READ_UTF16BE(z, 
b1330 31 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72  1, c);.    asser
b1340 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61  t( c==i );.    a
b1350 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d  ssert( (z-zBuf)=
b1360 3d 6e 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  =n );.  }.}.#end
b1370 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53  if /* SQLITE_TES
b1380 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53  T */.#endif /* S
b1390 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
b13a0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
b13b0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 66 2e  **** End of utf.
b13c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
b13d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b13e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b13f0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
b1400 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
b1410 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  util.c *********
b1420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1440 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
b1450 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
b1460 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
b1470 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
b1480 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
b1490 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
b14a0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
b14b0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
b14c0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
b14d0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
b14e0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
b14f0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
b1500 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
b1510 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
b1520 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
b1530 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
b1540 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
b1550 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
b1560 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
b1570 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
b1580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55  ***********.** U
b15c0 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73  tility functions
b15d0 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74   used throughout
b15e0 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54   sqlite..**.** T
b15f0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
b1600 73 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  s functions for 
b1610 61 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f 72  allocating memor
b1620 79 2c 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20  y, comparing.** 
b1630 73 74 72 69 6e 67 73 2c 20 61 6e 64 20 73 74 75  strings, and stu
b1640 66 66 20 6c 69 6b 65 20 74 68 61 74 2e 0a 2a 2a  ff like that..**
b1650 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
b1660 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 20 69  E_HAVE_ISNAN.# i
b1670 6e 63 6c 75 64 65 20 3c 6d 61 74 68 2e 68 3e 0a  nclude <math.h>.
b1680 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f  #endif../*.** Ro
b1690 75 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f 20  utine needed to 
b16a0 73 75 70 70 6f 72 74 20 74 68 65 20 74 65 73 74  support the test
b16b0 63 61 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f  case() macro..*/
b16c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
b16d0 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c  OVERAGE_TEST.SQL
b16e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b16f0 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65   sqlite3Coverage
b1700 28 69 6e 74 20 78 29 7b 0a 20 20 73 74 61 74 69  (int x){.  stati
b1710 63 20 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30 3b  c int dummy = 0;
b1720 0a 20 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d  .  dummy += x;.}
b1730 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
b1740 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
b1750 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  e floating point
b1760 20 76 61 6c 75 65 20 69 73 20 4e 6f 74 20 61 20   value is Not a 
b1770 4e 75 6d 62 65 72 20 28 4e 61 4e 29 2e 0a 2a 2a  Number (NaN)..**
b1780 0a 2a 2a 20 55 73 65 20 74 68 65 20 6d 61 74 68  .** Use the math
b1790 20 6c 69 62 72 61 72 79 20 69 73 6e 61 6e 28 29   library isnan()
b17a0 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f 6d   function if com
b17b0 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54  piled with SQLIT
b17c0 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a 2a  E_HAVE_ISNAN..**
b17d0 20 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20 68   Otherwise, we h
b17e0 61 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70 6c  ave our own impl
b17f0 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20  ementation that 
b1800 77 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73 79  works on most sy
b1810 73 74 65 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stems..*/.SQLITE
b1820 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b1830 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65  ite3IsNaN(double
b1840 20 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20   x){.  int rc;  
b1850 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65   /* The value re
b1860 74 75 72 6e 20 2a 2f 0a 23 69 66 20 21 64 65 66  turn */.#if !def
b1870 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45  ined(SQLITE_HAVE
b1880 5f 49 53 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20 2a  _ISNAN).  /*.  *
b1890 2a 20 53 79 73 74 65 6d 73 20 74 68 61 74 20 73  * Systems that s
b18a0 75 70 70 6f 72 74 20 74 68 65 20 69 73 6e 61 6e  upport the isnan
b18b0 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74  () library funct
b18c0 69 6f 6e 20 73 68 6f 75 6c 64 20 70 72 6f 62 61  ion should proba
b18d0 62 6c 79 0a 20 20 2a 2a 20 6d 61 6b 65 20 75 73  bly.  ** make us
b18e0 65 20 6f 66 20 69 74 20 62 79 20 63 6f 6d 70 69  e of it by compi
b18f0 6c 69 6e 67 20 77 69 74 68 20 2d 44 53 51 4c 49  ling with -DSQLI
b1900 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 20 20  TE_HAVE_ISNAN.  
b1910 42 75 74 20 77 65 20 68 61 76 65 0a 20 20 2a 2a  But we have.  **
b1920 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 6e 79   found that many
b1930 20 73 79 73 74 65 6d 73 20 64 6f 20 6e 6f 74 20   systems do not 
b1940 68 61 76 65 20 61 20 77 6f 72 6b 69 6e 67 20 69  have a working i
b1950 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20  snan() function 
b1960 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 69 6d 70  so.  ** this imp
b1970 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 70  lementation is p
b1980 72 6f 76 69 64 65 64 20 61 73 20 61 6e 20 61 6c  rovided as an al
b1990 74 65 72 6e 61 74 69 76 65 2e 0a 20 20 2a 2a 0a  ternative..  **.
b19a0 20 20 2a 2a 20 54 68 69 73 20 4e 61 4e 20 74 65    ** This NaN te
b19b0 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61 69  st sometimes fai
b19c0 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 6f  ls if compiled o
b19d0 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61 73  n GCC with -ffas
b19e0 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e 20  t-math..  ** On 
b19f0 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20  the other hand, 
b1a00 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61 73  the use of -ffas
b1a10 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69 74  t-math comes wit
b1a20 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  h the following.
b1a30 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 20    ** warning:.  
b1a40 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 69  **.  **      Thi
b1a50 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73 74  s option [-ffast
b1a60 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e 65  -math] should ne
b1a70 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f 6e  ver be turned on
b1a80 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20 20   by any.  **    
b1a90 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e 63    -O option sinc
b1aa0 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74 20  e it can result 
b1ab0 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75 74  in incorrect out
b1ac0 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d 73  put for programs
b1ad0 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63 68  .  **      which
b1ae0 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65 78   depend on an ex
b1af0 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  act implementati
b1b00 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49 53  on of IEEE or IS
b1b10 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75 6c  O .  **      rul
b1b20 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f 6e  es/specification
b1b30 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63 74  s for math funct
b1b40 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ions..  **.  ** 
b1b50 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69 73  Under MSVC, this
b1b60 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66 61   NaN test may fa
b1b70 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 77  il if compiled w
b1b80 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d 0a  ith a floating-.
b1b90 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63 69    ** point preci
b1ba0 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72 20  sion mode other 
b1bb0 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73 65  than /fp:precise
b1bc0 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44 4e  .  From the MSDN
b1bd0 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 61   .  ** documenta
b1be0 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  tion:.  **.  ** 
b1bf0 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c 65       The compile
b1c00 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65 63  r [with /fp:prec
b1c10 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65 72  ise] will proper
b1c20 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61 72  ly handle compar
b1c30 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20 20  isons .  **     
b1c40 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e 20   involving NaN. 
b1c50 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20 21  For example, x !
b1c60 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74 6f  = x evaluates to
b1c70 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e 61   true if x is Na
b1c80 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e 2e  N .  **      ...
b1c90 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 46  .  */.#ifdef __F
b1ca0 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72 72  AST_MATH__.# err
b1cb0 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  or SQLite will n
b1cc0 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c  ot work correctl
b1cd0 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61 73  y with the -ffas
b1ce0 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f 66  t-math option of
b1cf0 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20 76   GCC..#endif.  v
b1d00 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 79  olatile double y
b1d10 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c 65   = x;.  volatile
b1d20 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a 20   double z = y;. 
b1d30 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0a 23 65   rc = (y!=z);.#e
b1d40 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69 6e  lse  /* if defin
b1d50 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f 49  ed(SQLITE_HAVE_I
b1d60 53 4e 41 4e 29 20 2a 2f 0a 20 20 72 63 20 3d 20  SNAN) */.  rc = 
b1d70 69 73 6e 61 6e 28 78 29 3b 0a 23 65 6e 64 69 66  isnan(x);.#endif
b1d80 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 56 45 5f   /* SQLITE_HAVE_
b1d90 49 53 4e 41 4e 20 2a 2f 0a 20 20 74 65 73 74 63  ISNAN */.  testc
b1da0 61 73 65 28 20 72 63 20 29 3b 0a 20 20 72 65 74  ase( rc );.  ret
b1db0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
b1dc0 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69 6e   Compute a strin
b1dd0 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 69 73  g length that is
b1de0 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 61 74   limited to what
b1df0 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 69   can be stored i
b1e00 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 62 69  n.** lower 30 bi
b1e10 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 20 73  ts of a 32-bit s
b1e20 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a  igned integer..*
b1e30 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72  *.** The value r
b1e40 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 65 76  eturned will nev
b1e50 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 2e 20  er be negative. 
b1e60 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 76 65   Nor will it eve
b1e70 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a 2a 20  r be greater.** 
b1e80 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c 20  than the actual 
b1e90 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74  length of the st
b1ea0 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 79 20  ring.  For very 
b1eb0 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 67 72  long strings (gr
b1ec0 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 31 47  eater.** than 1G
b1ed0 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 72 65  iB) the value re
b1ee0 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 65 20  turned might be 
b1ef0 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74 72  less than the tr
b1f00 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68  ue string length
b1f10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b1f20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
b1f30 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63 68  trlen30(const ch
b1f40 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74 20  ar *z){.  const 
b1f50 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20  char *z2 = z;.  
b1f60 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
b1f70 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a  n 0;.  while( *z
b1f80 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72  2 ){ z2++; }.  r
b1f90 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 66 66  eturn 0x3fffffff
b1fa0 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29   & (int)(z2 - z)
b1fb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
b1fc0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65  he most recent e
b1fd0 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72  rror code and er
b1fe0 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 74  ror string for t
b1ff0 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e  he sqlite.** han
b2000 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20 65 72  dle "db". The er
b2010 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74 20  ror code is set 
b2020 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a  to "err_code"..*
b2030 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f  *.** If it is no
b2040 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a  t NULL, string z
b2050 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69 65 73  Format specifies
b2060 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74   the format of t
b2070 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 69  he.** error stri
b2080 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c 65 20  ng in the style 
b2090 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20 66 75  of the printf fu
b20a0 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c  nctions: The fol
b20b0 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74  lowing.** format
b20c0 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20   characters are 
b20d0 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  allowed:.**.**  
b20e0 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65      %s      Inse
b20f0 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20  rt a string.**  
b2100 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74      %z      A st
b2110 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64  ring that should
b2120 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20   be freed after 
b2130 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20  use.**      %d  
b2140 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e      Insert an in
b2150 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54  teger.**      %T
b2160 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74        Insert a t
b2170 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20  oken.**      %S 
b2180 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20       Insert the 
b2190 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
b21a0 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a   a SrcList.**.**
b21b0 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79   zFormat and any
b21c0 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74   string tokens t
b21d0 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72  hat follow it ar
b21e0 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a  e assumed to be.
b21f0 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54  ** encoded in UT
b2200 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c  F-8..**.** To cl
b2210 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63  ear the most rec
b2220 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73 71  ent error for sq
b2230 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62 22  lite handle "db"
b2240 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a  , sqlite3Error.*
b2250 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c  * should be call
b2260 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f 64 65  ed with err_code
b2270 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f   set to SQLITE_O
b2280 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65  K and zFormat se
b2290 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f  t.** to NULL..*/
b22a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b22b0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f  void sqlite3Erro
b22c0 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  r(sqlite3 *db, i
b22d0 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e  nt err_code, con
b22e0 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
b22f0 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62  , ...){.  if( db
b2300 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c   && (db->pErr ||
b2310 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c   (db->pErr = sql
b2320 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29  ite3ValueNew(db)
b2330 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 2d  )!=0) ){.    db-
b2340 3e 65 72 72 43 6f 64 65 20 3d 20 65 72 72 5f 63  >errCode = err_c
b2350 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f  ode;.    if( zFo
b2360 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 63 68  rmat ){.      ch
b2370 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f  ar *z;.      va_
b2380 6c 69 73 74 20 61 70 3b 0a 20 20 20 20 20 20 76  list ap;.      v
b2390 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
b23a0 6d 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20  mat);.      z = 
b23b0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
b23c0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
b23d0 3b 0a 20 20 20 20 20 20 76 61 5f 65 6e 64 28 61  ;.      va_end(a
b23e0 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
b23f0 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d  3ValueSetStr(db-
b2400 3e 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51  >pErr, -1, z, SQ
b2410 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
b2420 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  E_DYNAMIC);.    
b2430 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
b2440 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
b2450 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20  db->pErr, 0, 0, 
b2460 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
b2470 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
b2480 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
b2490 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  Add an error mes
b24a0 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 2d 3e  sage to pParse->
b24b0 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e 63 72  zErrMsg and incr
b24c0 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45  ement pParse->nE
b24d0 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  rr..** The follo
b24e0 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67 20  wing formatting 
b24f0 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 61  characters are a
b2500 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  llowed:.**.**   
b2510 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 72     %s      Inser
b2520 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20  t a string.**   
b2530 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 72     %z      A str
b2540 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ing that should 
b2550 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20 75  be freed after u
b2560 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20  se.**      %d   
b2570 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74     Insert an int
b2580 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20  eger.**      %T 
b2590 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f       Insert a to
b25a0 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20  ken.**      %S  
b25b0 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 66      Insert the f
b25c0 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
b25d0 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20  a SrcList.**.** 
b25e0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68  This function sh
b25f0 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74 6f 20  ould be used to 
b2600 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f 72  report any error
b2610 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 68 69   that occurs whi
b2620 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67  lst.** compiling
b2630 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
b2640 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 73  t (i.e. within s
b2650 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
b2660 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74  ). The.** last t
b2670 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  hing the sqlite3
b2680 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e 63 74  _prepare() funct
b2690 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f 70 79  ion does is copy
b26a0 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74   the error.** st
b26b0 6f 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  ored by this fun
b26c0 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64  ction into the d
b26d0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 75  atabase handle u
b26e0 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72 72 6f  sing sqlite3Erro
b26f0 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  r()..** Function
b2700 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20   sqlite3Error() 
b2710 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 64  should be used d
b2720 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20  uring statement 
b2730 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71  execution.** (sq
b2740 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 74 63  lite3_step() etc
b2750 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  .)..*/.SQLITE_PR
b2760 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
b2770 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65  e3ErrorMsg(Parse
b2780 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
b2790 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
b27a0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
b27b0 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  p;.  sqlite3 *db
b27c0 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
b27d0 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
b27e0 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
b27f0 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72  (db, pParse->zEr
b2800 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61 72  rMsg);.  va_star
b2810 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
b2820 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73    pParse->zErrMs
b2830 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  g = sqlite3VMPri
b2840 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
b2850 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
b2860 70 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72 63  p);.  pParse->rc
b2870 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
b2880 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
b2890 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
b28a0 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66 20  e in pParse, if 
b28b0 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  any.*/.SQLITE_PR
b28c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
b28d0 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72  e3ErrorClear(Par
b28e0 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73  se *pParse){.  s
b28f0 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61  qlite3DbFree(pPa
b2900 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d  rse->db, pParse-
b2910 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50 61  >zErrMsg);.  pPa
b2920 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30  rse->zErrMsg = 0
b2930 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  ;.  pParse->nErr
b2940 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 0;.}../*.** C
b2950 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 74  onvert an SQL-st
b2960 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 6e  yle quoted strin
b2970 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20  g into a normal 
b2980 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 69  string by removi
b2990 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 20  ng.** the quote 
b29a0 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 65  characters.  The
b29b0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 64   conversion is d
b29c0 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49  one in-place.  I
b29d0 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64  f the.** input d
b29e0 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69  oes not begin wi
b29f0 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 61  th a quote chara
b2a00 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20  cter, then this 
b2a10 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20  routine.** is a 
b2a20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  no-op..**.** The
b2a30 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d 75   input string mu
b2a40 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69  st be zero-termi
b2a50 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a 65  nated.  A new ze
b2a60 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0a 2a 2a  ro-terminator.**
b2a70 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65   is added to the
b2a80 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e 67   dequoted string
b2a90 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
b2aa0 72 6e 20 76 61 6c 75 65 20 69 73 20 2d 31 20 69  rn value is -1 i
b2ab0 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20 6f  f no dequoting o
b2ac0 63 63 75 72 73 20 6f 72 20 74 68 65 20 6c 65 6e  ccurs or the len
b2ad0 67 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65  gth of the.** de
b2ae0 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2c 20 65  quoted string, e
b2af0 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 20  xclusive of the 
b2b00 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2c  zero terminator,
b2b10 20 69 66 20 64 65 71 75 6f 74 69 6e 67 20 64 6f   if dequoting do
b2b20 65 73 0a 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a 0a  es.** occur..**.
b2b30 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34 3a 20  ** 2002-Feb-14: 
b2b40 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
b2b50 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65 6d 6f  extended to remo
b2b60 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73 74 79  ve MS-Access sty
b2b70 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73 20 66  le.** brackets f
b2b80 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65 6e 74  rom around ident
b2b90 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78 61 6d  ifers.  For exam
b2ba0 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 20  ple:  "[a-b-c]" 
b2bb0 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 2d  becomes.** "a-b-
b2bc0 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  c"..*/.SQLITE_PR
b2bd0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b2be0 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a 7a  3Dequote(char *z
b2bf0 29 7b 0a 20 20 63 68 61 72 20 71 75 6f 74 65 3b  ){.  char quote;
b2c00 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69  .  int i, j;.  i
b2c10 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
b2c20 20 2d 31 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a   -1;.  quote = z
b2c30 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 71  [0];.  switch( q
b2c40 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 65  uote ){.    case
b2c50 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20   '\'':  break;. 
b2c60 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 62     case '"':   b
b2c70 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27  reak;.    case '
b2c80 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 20  `':   break;    
b2c90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
b2ca0 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69  or MySQL compati
b2cb0 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 61  bility */.    ca
b2cc0 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 20  se '[':   quote 
b2cd0 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20  = ']';  break;  
b2ce0 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 72  /* For MS SqlSer
b2cf0 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  ver compatibilit
b2d00 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74  y */.    default
b2d10 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a  :    return -1;.
b2d20 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a    }.  for(i=1, j
b2d30 3d 30 3b 20 41 4c 57 41 59 53 28 7a 5b 69 5d 29  =0; ALWAYS(z[i])
b2d40 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
b2d50 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20  z[i]==quote ){. 
b2d60 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d       if( z[i+1]=
b2d70 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20  =quote ){.      
b2d80 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65    z[j++] = quote
b2d90 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20  ;.        i++;. 
b2da0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
b2db0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
b2dc0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
b2dd0 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69      z[j++] = z[i
b2de0 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a  ];.    }.  }.  z
b2df0 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  [j] = 0;.  retur
b2e00 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65  n j;.}../* Conve
b2e10 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e 64  nient short-hand
b2e20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70 65   */.#define Uppe
b2e30 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65 33  rToLower sqlite3
b2e40 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a  UpperToLower../*
b2e50 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d 73  .** Some systems
b2e60 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29 2e   have stricmp().
b2e70 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73 74    Others have st
b2e80 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65 63  rcasecmp().  Bec
b2e90 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69 73  ause.** there is
b2ea0 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79 2c   no consistency,
b2eb0 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65 20   we will define 
b2ec0 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49  our own..*/.SQLI
b2ed0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b2ee0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 6f  qlite3StrICmp(co
b2ef0 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c  nst char *zLeft,
b2f00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69   const char *zRi
b2f10 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65 72  ght){.  register
b2f20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
b2f30 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e  a, *b;.  a = (un
b2f40 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c  signed char *)zL
b2f50 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69  eft;.  b = (unsi
b2f60 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 67  gned char *)zRig
b2f70 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61 21  ht;.  while( *a!
b2f80 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f 77  =0 && UpperToLow
b2f90 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c  er[*a]==UpperToL
b2fa0 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20  ower[*b]){ a++; 
b2fb0 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  b++; }.  return 
b2fc0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d  UpperToLower[*a]
b2fd0 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b   - UpperToLower[
b2fe0 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  *b];.}.SQLITE_AP
b2ff0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  I int sqlite3_st
b3000 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61  rnicmp(const cha
b3010 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20  r *zLeft, const 
b3020 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e  char *zRight, in
b3030 74 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65 72  t N){.  register
b3040 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
b3050 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e  a, *b;.  a = (un
b3060 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c  signed char *)zL
b3070 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69  eft;.  b = (unsi
b3080 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 67  gned char *)zRig
b3090 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d  ht;.  while( N--
b30a0 20 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26 26   > 0 && *a!=0 &&
b30b0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
b30c0 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  ]==UpperToLower[
b30d0 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20  *b]){ a++; b++; 
b30e0 7d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f  }.  return N<0 ?
b30f0 20 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65   0 : UpperToLowe
b3100 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c  r[*a] - UpperToL
b3110 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a  ower[*b];.}../*.
b3120 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
b3130 66 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e 75  f z is a pure nu
b3140 6d 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20 52  meric string.  R
b3150 65 74 75 72 6e 20 46 41 4c 53 45 20 61 6e 64 20  eturn FALSE and 
b3160 6c 65 61 76 65 0a 2a 2a 20 2a 72 65 61 6c 6e 75  leave.** *realnu
b3170 6d 20 75 6e 63 68 61 6e 67 65 64 20 69 66 20 74  m unchanged if t
b3180 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69  he string contai
b3190 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 65 72  ns any character
b31a0 20 77 68 69 63 68 20 69 73 20 6e 6f 74 0a 2a 2a   which is not.**
b31b0 20 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62 65   part of a numbe
b31c0 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
b31d0 73 74 72 69 6e 67 20 69 73 20 70 75 72 65 20 6e  string is pure n
b31e0 75 6d 65 72 69 63 2c 20 73 65 74 20 2a 72 65 61  umeric, set *rea
b31f0 6c 6e 75 6d 20 74 6f 20 54 52 55 45 20 69 66 20  lnum to TRUE if 
b3200 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f  the string.** co
b3210 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27 20 63  ntains the '.' c
b3220 68 61 72 61 63 74 65 72 20 6f 72 20 61 6e 20 22  haracter or an "
b3230 45 2b 30 30 30 22 20 73 74 79 6c 65 20 65 78 70  E+000" style exp
b3240 6f 6e 65 6e 74 69 61 74 69 6f 6e 20 73 75 66 66  onentiation suff
b3250 69 78 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  ix..** Otherwise
b3260 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f   set *realnum to
b3270 20 46 41 4c 53 45 2e 20 20 4e 6f 74 65 20 74 68   FALSE.  Note th
b3280 61 74 20 6a 75 73 74 20 62 65 63 61 75 65 20 2a  at just becaue *
b3290 72 65 61 6c 6e 75 6d 20 69 73 0a 2a 2a 20 66 61  realnum is.** fa
b32a0 6c 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61  lse does not mea
b32b0 6e 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65  n that the numbe
b32c0 72 20 63 61 6e 20 62 65 20 73 75 63 63 65 73 73  r can be success
b32d0 66 75 6c 6c 79 20 63 6f 6e 76 65 72 74 65 64 20  fully converted 
b32e0 69 6e 74 6f 0a 2a 2a 20 61 6e 20 69 6e 74 65 67  into.** an integ
b32f0 65 72 20 2d 20 69 74 20 6d 69 67 68 74 20 62 65  er - it might be
b3300 20 74 6f 6f 20 62 69 67 2e 0a 2a 2a 0a 2a 2a 20   too big..**.** 
b3310 41 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20  An empty string 
b3320 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f  is considered no
b3330 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51  n-numeric..*/.SQ
b3340 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b3350 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72   sqlite3IsNumber
b3360 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
b3370 69 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38  int *realnum, u8
b3380 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63   enc){.  int inc
b3390 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45  r = (enc==SQLITE
b33a0 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66  _UTF8?1:2);.  if
b33b0 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ( enc==SQLITE_UT
b33c0 46 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69  F16BE ) z++;.  i
b33d0 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a  f( *z=='-' || *z
b33e0 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63  =='+' ) z += inc
b33f0 72 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  r;.  if( !sqlite
b3400 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a  3Isdigit(*z) ){.
b3410 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
b3420 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20  }.  z += incr;. 
b3430 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20   *realnum = 0;. 
b3440 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
b3450 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20  sdigit(*z) ){ z 
b3460 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28  += incr; }.  if(
b3470 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20   *z=='.' ){.    
b3480 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69  z += incr;.    i
b3490 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67  f( !sqlite3Isdig
b34a0 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e 20  it(*z) ) return 
b34b0 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71  0;.    while( sq
b34c0 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29  lite3Isdigit(*z)
b34d0 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d   ){ z += incr; }
b34e0 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20  .    *realnum = 
b34f0 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d  1;.  }.  if( *z=
b3500 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20  ='e' || *z=='E' 
b3510 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72  ){.    z += incr
b3520 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2b  ;.    if( *z=='+
b3530 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a  ' || *z=='-' ) z
b3540 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66   += incr;.    if
b3550 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69  ( !sqlite3Isdigi
b3560 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 30  t(*z) ) return 0
b3570 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c  ;.    while( sql
b3580 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20  ite3Isdigit(*z) 
b3590 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a  ){ z += incr; }.
b35a0 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31      *realnum = 1
b35b0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a  ;.  }.  return *
b35c0 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  z==0;.}../*.** T
b35d0 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20 69 73  he string z[] is
b35e0 20 61 6e 20 41 53 43 49 49 20 72 65 70 72 65 73   an ASCII repres
b35f0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 72 65  entation of a re
b3600 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43 6f  al number..** Co
b3610 6e 76 65 72 74 20 74 68 69 73 20 73 74 72 69 6e  nvert this strin
b3620 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a  g to a double..*
b3630 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
b3640 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 7a  e assumes that z
b3650 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61 20 76  [] really is a v
b3660 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20 49 66  alid number.  If
b3670 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 74   it.** is not, t
b3680 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64  he result is und
b3690 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  efined..**.** Th
b36a0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
b36b0 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ed instead of th
b36c0 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29  e library atof()
b36d0 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61 75 73   function becaus
b36e0 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79  e.** the library
b36f0 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20 77 61   atof() might wa
b3700 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20 61 73  nt to use "," as
b3710 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69   the decimal poi
b3720 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66  nt instead.** of
b3730 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67 20 6f   "." depending o
b3740 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69 73 20  n how locale is 
b3750 73 65 74 2e 20 20 42 75 74 20 74 68 61 74 20 77  set.  But that w
b3760 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c  ould cause probl
b3770 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e 20  ems.** for SQL. 
b3780 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   So this routine
b3790 20 61 6c 77 61 79 73 20 75 73 65 73 20 22 2e 22   always uses "."
b37a0 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c   regardless of l
b37b0 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ocale..*/.SQLITE
b37c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b37d0 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63  ite3AtoF(const c
b37e0 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 20 2a  har *z, double *
b37f0 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e 64 65  pResult){.#ifnde
b3800 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
b3810 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 63  OATING_POINT.  c
b3820 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65 67 69  onst char *zBegi
b3830 6e 20 3d 20 7a 3b 0a 20 20 2f 2a 20 73 69 67 6e  n = z;.  /* sign
b3840 20 2a 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a   * significand *
b3850 20 28 31 30 20 5e 20 28 65 73 69 67 6e 20 2a 20   (10 ^ (esign * 
b3860 65 78 70 6f 6e 65 6e 74 29 29 20 2a 2f 0a 20 20  exponent)) */.  
b3870 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 20 20 20  int sign = 1;   
b3880 2f 2a 20 73 69 67 6e 20 6f 66 20 73 69 67 6e 69  /* sign of signi
b3890 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 36 34 20  ficand */.  i64 
b38a0 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 73  s = 0;      /* s
b38b0 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20  ignificand */.  
b38c0 69 6e 74 20 64 20 3d 20 30 3b 20 20 20 20 20 20  int d = 0;      
b38d0 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65  /* adjust expone
b38e0 6e 74 20 66 6f 72 20 73 68 69 66 74 69 6e 67 20  nt for shifting 
b38f0 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f  decimal point */
b3900 0a 20 20 69 6e 74 20 65 73 69 67 6e 20 3d 20 31  .  int esign = 1
b3910 3b 20 20 2f 2a 20 73 69 67 6e 20 6f 66 20 65 78  ;  /* sign of ex
b3920 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  ponent */.  int 
b3930 65 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 65  e = 0;      /* e
b3940 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 64 6f 75  xponent */.  dou
b3950 62 6c 65 20 72 65 73 75 6c 74 3b 0a 20 20 69 6e  ble result;.  in
b3960 74 20 6e 44 69 67 69 74 73 20 3d 20 30 3b 0a 0a  t nDigits = 0;..
b3970 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e    /* skip leadin
b3980 67 20 73 70 61 63 65 73 20 2a 2f 0a 20 20 77 68  g spaces */.  wh
b3990 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70  ile( sqlite3Issp
b39a0 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20  ace(*z) ) z++;. 
b39b0 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f 66 20   /* get sign of 
b39c0 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20  significand */. 
b39d0 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a   if( *z=='-' ){.
b39e0 20 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a 20      sign = -1;. 
b39f0 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20     z++;.  }else 
b3a00 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20  if( *z=='+' ){. 
b3a10 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a     z++;.  }.  /*
b3a20 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65   skip leading ze
b3a30 72 6f 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28  roes */.  while(
b3a40 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 2b 2b   z[0]=='0' ) z++
b3a50 2c 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 0a 20 20  , nDigits++;..  
b3a60 2f 2a 20 63 6f 70 79 20 6d 61 78 20 73 69 67 6e  /* copy max sign
b3a70 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 74  ificant digits t
b3a80 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f  o significand */
b3a90 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65  .  while( sqlite
b3aa0 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26 20  3Isdigit(*z) && 
b3ab0 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54 36  s<((LARGEST_INT6
b3ac0 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20 20  4-9)/10) ){.    
b3ad0 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a 7a 20 2d  s = s*10 + (*z -
b3ae0 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 2c 20   '0');.    z++, 
b3af0 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a 20  nDigits++;.  }. 
b3b00 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67   /* skip non-sig
b3b10 6e 69 66 69 63 61 6e 74 20 73 69 67 6e 69 66 69  nificant signifi
b3b20 63 61 6e 64 20 64 69 67 69 74 73 0a 20 20 2a 2a  cand digits.  **
b3b30 20 28 69 6e 63 72 65 61 73 65 20 65 78 70 6f 6e   (increase expon
b3b40 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69 66  ent by d to shif
b3b50 74 20 64 65 63 69 6d 61 6c 20 6c 65 66 74 29 20  t decimal left) 
b3b60 2a 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69  */.  while( sqli
b3b70 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29  te3Isdigit(*z) )
b3b80 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c   z++, nDigits++,
b3b90 20 64 2b 2b 3b 0a 0a 20 20 2f 2a 20 69 66 20 64   d++;..  /* if d
b3ba0 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 73 20  ecimal point is 
b3bb0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28  present */.  if(
b3bc0 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20   *z=='.' ){.    
b3bd0 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 63 6f 70 79  z++;.    /* copy
b3be0 20 64 69 67 69 74 73 20 66 72 6f 6d 20 61 66 74   digits from aft
b3bf0 65 72 20 64 65 63 69 6d 61 6c 20 74 6f 20 73 69  er decimal to si
b3c00 67 6e 69 66 69 63 61 6e 64 0a 20 20 20 20 2a 2a  gnificand.    **
b3c10 20 28 64 65 63 72 65 61 73 65 20 65 78 70 6f 6e   (decrease expon
b3c20 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69 66  ent by d to shif
b3c30 74 20 64 65 63 69 6d 61 6c 20 72 69 67 68 74 29  t decimal right)
b3c40 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 73   */.    while( s
b3c50 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
b3c60 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 53 54  ) && s<((LARGEST
b3c70 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 29 7b  _INT64-9)/10) ){
b3c80 0a 20 20 20 20 20 20 73 20 3d 20 73 2a 31 30 20  .      s = s*10 
b3c90 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20  + (*z - '0');.  
b3ca0 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73      z++, nDigits
b3cb0 2b 2b 2c 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a 20  ++, d--;.    }. 
b3cc0 20 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73     /* skip non-s
b3cd0 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74  ignificant digit
b3ce0 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  s */.    while( 
b3cf0 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
b3d00 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74  z) ) z++, nDigit
b3d10 73 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69  s++;.  }..  /* i
b3d20 66 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 70 72  f exponent is pr
b3d30 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a  esent */.  if( *
b3d40 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45  z=='e' || *z=='E
b3d50 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20  ' ){.    z++;.  
b3d60 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f 66    /* get sign of
b3d70 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20   exponent */.   
b3d80 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a   if( *z=='-' ){.
b3d90 20 20 20 20 20 20 65 73 69 67 6e 20 3d 20 2d 31        esign = -1
b3da0 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  ;.      z++;.   
b3db0 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27   }else if( *z=='
b3dc0 2b 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b  +' ){.      z++;
b3dd0 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63 6f  .    }.    /* co
b3de0 70 79 20 64 69 67 69 74 73 20 74 6f 20 65 78 70  py digits to exp
b3df0 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 77 68 69  onent */.    whi
b3e00 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67  le( sqlite3Isdig
b3e10 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20  it(*z) ){.      
b3e20 65 20 3d 20 65 2a 31 30 20 2b 20 28 2a 7a 20 2d  e = e*10 + (*z -
b3e30 20 27 30 27 29 3b 0a 20 20 20 20 20 20 7a 2b 2b   '0');.      z++
b3e40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
b3e50 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e  * adjust exponen
b3e60 74 20 62 79 20 64 2c 20 61 6e 64 20 75 70 64 61  t by d, and upda
b3e70 74 65 20 73 69 67 6e 20 2a 2f 0a 20 20 65 20 3d  te sign */.  e =
b3e80 20 28 65 2a 65 73 69 67 6e 29 20 2b 20 64 3b 0a   (e*esign) + d;.
b3e90 20 20 69 66 28 20 65 3c 30 20 29 20 7b 0a 20 20    if( e<0 ) {.  
b3ea0 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20    esign = -1;.  
b3eb0 20 20 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 20 65    e *= -1;.  } e
b3ec0 6c 73 65 20 7b 0a 20 20 20 20 65 73 69 67 6e 20  lse {.    esign 
b3ed0 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69  = 1;.  }..  /* i
b3ee0 66 20 30 20 73 69 67 6e 69 66 69 63 61 6e 64 20  f 0 significand 
b3ef0 2a 2f 0a 20 20 69 66 28 20 21 73 20 29 20 7b 0a  */.  if( !s ) {.
b3f00 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 20 49 45      /* In the IE
b3f10 45 45 20 37 35 34 20 73 74 61 6e 64 61 72 64 2c  EE 754 standard,
b3f20 20 7a 65 72 6f 20 69 73 20 73 69 67 6e 65 64 2e   zero is signed.
b3f30 0a 20 20 20 20 2a 2a 20 41 64 64 20 74 68 65 20  .    ** Add the 
b3f40 73 69 67 6e 20 69 66 20 77 65 27 76 65 20 73 65  sign if we've se
b3f50 65 6e 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  en at least one 
b3f60 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 72 65 73  digit */.    res
b3f70 75 6c 74 20 3d 20 28 73 69 67 6e 3c 30 20 26 26  ult = (sign<0 &&
b3f80 20 6e 44 69 67 69 74 73 29 20 3f 20 2d 28 64 6f   nDigits) ? -(do
b3f90 75 62 6c 65 29 30 20 3a 20 28 64 6f 75 62 6c 65  uble)0 : (double
b3fa0 29 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20  )0;.  } else {. 
b3fb0 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f     /* attempt to
b3fc0 20 72 65 64 75 63 65 20 65 78 70 6f 6e 65 6e 74   reduce exponent
b3fd0 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 73 69 67   */.    if( esig
b3fe0 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 77 68 69  n>0 ){.      whi
b3ff0 6c 65 28 20 73 3c 28 4c 41 52 47 45 53 54 5f 49  le( s<(LARGEST_I
b4000 4e 54 36 34 2f 31 30 29 20 26 26 20 65 3e 30 20  NT64/10) && e>0 
b4010 29 20 65 2d 2d 2c 73 2a 3d 31 30 3b 0a 20 20 20  ) e--,s*=10;.   
b4020 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68   }else{.      wh
b4030 69 6c 65 28 20 21 28 73 25 31 30 29 20 26 26 20  ile( !(s%10) && 
b4040 65 3e 30 20 29 20 65 2d 2d 2c 73 2f 3d 31 30 3b  e>0 ) e--,s/=10;
b4050 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 61  .    }..    /* a
b4060 64 6a 75 73 74 20 74 68 65 20 73 69 67 6e 20 6f  djust the sign o
b4070 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f  f significand */
b4080 0a 20 20 20 20 73 20 3d 20 73 69 67 6e 3c 30 20  .    s = sign<0 
b4090 3f 20 2d 73 20 3a 20 73 3b 0a 0a 20 20 20 20 2f  ? -s : s;..    /
b40a0 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74 2c 20 73  * if exponent, s
b40b0 63 61 6c 65 20 73 69 67 6e 69 66 69 63 61 6e 64  cale significand
b40c0 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 0a   as appropriate.
b40d0 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72 65      ** and store
b40e0 20 69 6e 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20   in result. */. 
b40f0 20 20 20 69 66 28 20 65 20 29 7b 0a 20 20 20 20     if( e ){.    
b4100 20 20 64 6f 75 62 6c 65 20 73 63 61 6c 65 20 3d    double scale =
b4110 20 31 2e 30 3b 0a 20 20 20 20 20 20 2f 2a 20 61   1.0;.      /* a
b4120 74 74 65 6d 70 74 20 74 6f 20 68 61 6e 64 6c 65  ttempt to handle
b4130 20 65 78 74 72 65 6d 65 6c 79 20 73 6d 61 6c 6c   extremely small
b4140 2f 6c 61 72 67 65 20 6e 75 6d 62 65 72 73 20 62  /large numbers b
b4150 65 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69  etter */.      i
b4160 66 28 20 65 3e 33 30 37 20 26 26 20 65 3c 33 34  f( e>307 && e<34
b4170 32 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69  2 ){.        whi
b4180 6c 65 28 20 65 25 33 30 38 20 29 20 7b 20 73 63  le( e%308 ) { sc
b4190 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65  ale *= 1.0e+1; e
b41a0 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20 20   -= 1; }.       
b41b0 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a   if( esign<0 ){.
b41c0 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74            result
b41d0 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20   = s / scale;.  
b41e0 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 2f          result /
b41f0 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20 20  = 1.0e+308;.    
b4200 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b4210 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20       result = s 
b4220 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20  * scale;.       
b4230 20 20 20 72 65 73 75 6c 74 20 2a 3d 20 31 2e 30     result *= 1.0
b4240 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20 7d  e+308;.        }
b4250 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
b4260 20 20 20 20 20 20 2f 2a 20 31 2e 30 65 2b 32 32        /* 1.0e+22
b4270 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20   is the largest 
b4280 70 6f 77 65 72 20 6f 66 20 31 30 20 74 68 61 6e  power of 10 than
b4290 20 63 61 6e 20 62 65 20 0a 20 20 20 20 20 20 20   can be .       
b42a0 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64 20   ** represented 
b42b0 65 78 61 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20  exactly. */.    
b42c0 20 20 20 20 77 68 69 6c 65 28 20 65 25 32 32 20      while( e%22 
b42d0 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30  ) { scale *= 1.0
b42e0 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 20  e+1; e -= 1; }. 
b42f0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65 3e         while( e>
b4300 30 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31  0 ) { scale *= 1
b4310 2e 30 65 2b 32 32 3b 20 65 20 2d 3d 20 32 32 3b  .0e+22; e -= 22;
b4320 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 65   }.        if( e
b4330 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20  sign<0 ){.      
b4340 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2f      result = s /
b4350 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20   scale;.        
b4360 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
b4370 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 63   result = s * sc
b4380 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ale;.        }. 
b4390 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73       }.    } els
b43a0 65 20 7b 0a 20 20 20 20 20 20 72 65 73 75 6c 74  e {.      result
b43b0 20 3d 20 28 64 6f 75 62 6c 65 29 73 3b 0a 20 20   = (double)s;.  
b43c0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 73 74    }.  }..  /* st
b43d0 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 2a  ore the result *
b43e0 2f 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 72  /.  *pResult = r
b43f0 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 72 65 74  esult;..  /* ret
b4400 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  urn number of ch
b4410 61 72 61 63 74 65 72 73 20 75 73 65 64 20 2a 2f  aracters used */
b4420 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28  .  return (int)(
b4430 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 65 6c  z - zBegin);.#el
b4440 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  se.  return sqli
b4450 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70 52 65  te3Atoi64(z, pRe
b4460 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  sult);.#endif /*
b4470 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
b4480 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d  ATING_POINT */.}
b4490 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ../*.** Compare 
b44a0 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65 72  the 19-character
b44b0 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61   string zNum aga
b44c0 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72 65  inst the text re
b44d0 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  presentation.** 
b44e0 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 32  value 2^63:  922
b44f0 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38  3372036854775808
b4500 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 74 69  .  Return negati
b4510 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73  ve, zero, or pos
b4520 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d  itive.** if zNum
b4530 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65   is less than, e
b4540 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61  qual to, or grea
b4550 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 72  ter than the str
b4560 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b  ing..**.** Unlik
b4570 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69 73 20  e memcmp() this 
b4580 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61 72 61  routine is guara
b4590 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20  nteed to return 
b45a0 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a  the difference.*
b45b0 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65 73 20  * in the values 
b45c0 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69 67 69  of the last digi
b45d0 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 64 69  t if the only di
b45e0 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e 20 74  fference is in t
b45f0 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69 74  he.** last digit
b4600 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70  .  So, for examp
b4610 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63  le,.**.**      c
b4620 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22 39 32  ompare2pow63("92
b4630 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30  2337203685477580
b4640 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72  0").**.** will r
b4650 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61  eturn -8..*/.sta
b4660 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72 65 32  tic int compare2
b4670 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68 61 72  pow63(const char
b4680 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63   *zNum){.  int c
b4690 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a  ;.  c = memcmp(z
b46a0 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33 36 38  Num,"92233720368
b46b0 35 34 37 37 35 38 30 22 2c 31 38 29 2a 31 30 3b  5477580",18)*10;
b46c0 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20  .  if( c==0 ){. 
b46d0 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20     c = zNum[18] 
b46e0 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74  - '8';.  }.  ret
b46f0 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn c;.}.../*.**
b4700 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
b4710 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74  zNum is a 64-bit
b4720 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
b4730 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65  and write.** the
b4740 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e   value of the in
b4750 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d  teger into *pNum
b4760 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f  .  If zNum is no
b4770 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  t an integer.** 
b4780 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  or is an integer
b4790 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72   that is too lar
b47a0 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73  ge to be express
b47b0 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c  ed with 64 bits,
b47c0 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20  .** then return 
b47d0 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  false..**.** Whe
b47e0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  n this routine w
b47f0 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72  as originally wr
b4800 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77  itten it dealt w
b4810 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62  ith only.** 32-b
b4820 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20  it numbers.  At 
b4830 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61  that time, it wa
b4840 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68  s much faster th
b4850 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29  an the.** atoi()
b4860 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65   library routine
b4870 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a   in RedHat 7.2..
b4880 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b4890 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f  E int sqlite3Ato
b48a0 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  i64(const char *
b48b0 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29  zNum, i64 *pNum)
b48c0 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20  {.  i64 v = 0;. 
b48d0 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20   int neg;.  int 
b48e0 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68  i, c;.  const ch
b48f0 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68  ar *zStart;.  wh
b4900 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70  ile( sqlite3Issp
b4910 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75  ace(*zNum) ) zNu
b4920 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d  m++;.  if( *zNum
b4930 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67  =='-' ){.    neg
b4940 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b   = 1;.    zNum++
b4950 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a  ;.  }else if( *z
b4960 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20  Num=='+' ){.    
b4970 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75  neg = 0;.    zNu
b4980 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  m++;.  }else{.  
b4990 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20    neg = 0;.  }. 
b49a0 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a   zStart = zNum;.
b49b0 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d    while( zNum[0]
b49c0 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b  =='0' ){ zNum++;
b49d0 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20   } /* Skip over 
b49e0 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54  leading zeros. T
b49f0 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20  icket #2454 */. 
b4a00 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75   for(i=0; (c=zNu
b4a10 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c  m[i])>='0' && c<
b4a20 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ='9'; i++){.    
b4a30 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27  v = v*10 + c - '
b4a40 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20  0';.  }.  *pNum 
b4a50 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a  = neg ? -v : v;.
b4a60 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 69    if( c!=0 || (i
b4a70 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a  ==0 && zStart==z
b4a80 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a  Num) || i>19 ){.
b4a90 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65      /* zNum is e
b4aa0 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73  mpty or contains
b4ab0 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78   non-numeric tex
b4ac0 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20  t or is longer. 
b4ad0 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69     ** than 19 di
b4ae0 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72 61  gits (thus guara
b4af0 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 73  nting that it is
b4b00 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20   too large) */. 
b4b10 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
b4b20 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b  else if( i<19 ){
b4b30 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61  .    /* Less tha
b4b40 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20  n 19 digits, so 
b4b50 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20  we know that it 
b4b60 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 20  fits in 64 bits 
b4b70 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  */.    return 1;
b4b80 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
b4b90 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72   19-digit number
b4ba0 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72  s must be no lar
b4bb0 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32  ger than 9223372
b4bc0 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66 20  036854775807 if 
b4bd0 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20  positive.    ** 
b4be0 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35 34  or 9223372036854
b4bf0 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 69  775808 if negati
b4c00 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39  ve.  Note that 9
b4c10 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35 38  2233720368546658
b4c20 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36  08.    ** is 2^6
b4c30 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  3. */.    return
b4c40 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a   compare2pow63(z
b4c50 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a  Num)<neg;.  }.}.
b4c60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e  ./*.** The strin
b4c70 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74  g zNum represent
b4c80 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e  s an unsigned in
b4c90 74 65 67 65 72 2e 20 20 54 68 65 20 7a 4e 75 6d  teger.  The zNum
b4ca0 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 69   string.** consi
b4cb0 73 74 73 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f  sts of one or mo
b4cc0 72 65 20 64 69 67 69 74 20 63 68 61 72 61 63 74  re digit charact
b4cd0 65 72 73 20 61 6e 64 20 69 73 20 74 65 72 6d 69  ers and is termi
b4ce0 6e 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 7a 65  nated by.** a ze
b4cf0 72 6f 20 63 68 61 72 61 63 74 65 72 2e 20 20 41  ro character.  A
b4d00 6e 79 20 73 74 72 61 79 20 63 68 61 72 61 63 74  ny stray charact
b4d10 65 72 73 20 69 6e 20 7a 4e 75 6d 20 72 65 73 75  ers in zNum resu
b4d20 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a  lt in undefined.
b4d30 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a  ** behavior..**.
b4d40 2a 2a 20 49 66 20 74 68 65 20 75 6e 73 69 67 6e  ** If the unsign
b4d50 65 64 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  ed integer that 
b4d60 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20  zNum represents 
b4d70 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a 2a  will fit in a.**
b4d80 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
b4d90 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 54  nteger, return T
b4da0 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 65 20  RUE.  Otherwise 
b4db0 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a  return FALSE..**
b4dc0 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 67 46 6c  .** If the negFl
b4dd0 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ag parameter is 
b4de0 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e 73  true, that means
b4df0 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 61 6c 6c   that zNum reall
b4e00 79 20 72 65 70 72 65 73 65 6e 74 73 0a 2a 2a 20  y represents.** 
b4e10 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
b4e20 72 2e 20 20 28 54 68 65 20 6c 65 61 64 69 6e 67  r.  (The leading
b4e30 20 22 2d 22 20 69 73 20 6f 6d 69 74 74 65 64 20   "-" is omitted 
b4e40 66 72 6f 6d 20 7a 4e 75 6d 2e 29 20 20 54 68 69  from zNum.)  Thi
b4e50 73 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  s.** parameter i
b4e60 73 20 6e 65 65 64 65 64 20 74 6f 20 64 65 74 65  s needed to dete
b4e70 72 6d 69 6e 65 20 61 20 62 6f 75 6e 64 61 72 79  rmine a boundary
b4e80 20 63 61 73 65 2e 20 20 41 20 73 74 72 69 6e 67   case.  A string
b4e90 0a 2a 2a 20 6f 66 20 22 39 32 32 33 33 37 33 30  .** of "92233730
b4ea0 33 36 38 35 34 37 37 35 38 30 38 22 20 72 65 74  36854775808" ret
b4eb0 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 6e 65  urns false if ne
b4ec0 67 46 6c 61 67 20 69 73 20 66 61 6c 73 65 20 6f  gFlag is false o
b4ed0 72 20 74 72 75 65 0a 2a 2a 20 69 66 20 6e 65 67  r true.** if neg
b4ee0 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a 2a  Flag is true..**
b4ef0 0a 2a 2a 20 4c 65 61 64 69 6e 67 20 7a 65 72 6f  .** Leading zero
b4f00 73 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a  s are ignored..*
b4f10 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b4f20 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 73   int sqlite3Fits
b4f30 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 63  In64Bits(const c
b4f40 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 6e  har *zNum, int n
b4f50 65 67 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 69  egFlag){.  int i
b4f60 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b  ;.  int neg = 0;
b4f70 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 75 6d  ..  assert( zNum
b4f80 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d  [0]>='0' && zNum
b4f90 5b 30 5d 3c 3d 27 39 27 20 29 3b 20 2f 2a 20 7a  [0]<='9' ); /* z
b4fa0 4e 75 6d 20 69 73 20 61 6e 20 75 6e 73 69 67 6e  Num is an unsign
b4fb0 65 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20  ed number */..  
b4fc0 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 6e 65  if( negFlag ) ne
b4fd0 67 20 3d 20 31 2d 6e 65 67 3b 0a 20 20 77 68 69  g = 1-neg;.  whi
b4fe0 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29  le( *zNum=='0' )
b4ff0 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20  {.    zNum++;   
b5000 2f 2a 20 53 6b 69 70 20 6c 65 61 64 69 6e 67 20  /* Skip leading 
b5010 7a 65 72 6f 73 2e 20 20 54 69 63 6b 65 74 20 23  zeros.  Ticket #
b5020 32 34 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f  2454 */.  }.  fo
b5030 72 28 69 3d 30 3b 20 7a 4e 75 6d 5b 69 5d 3b 20  r(i=0; zNum[i]; 
b5040 69 2b 2b 29 7b 20 61 73 73 65 72 74 28 20 7a 4e  i++){ assert( zN
b5050 75 6d 5b 69 5d 3e 3d 27 30 27 20 26 26 20 7a 4e  um[i]>='0' && zN
b5060 75 6d 5b 69 5d 3c 3d 27 39 27 20 29 3b 20 7d 0a  um[i]<='9' ); }.
b5070 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20    if( i<19 ){.  
b5080 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20    /* Guaranteed 
b5090 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20 74  to fit if less t
b50a0 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f  han 19 digits */
b50b0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
b50c0 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39 20   }else if( i>19 
b50d0 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e  ){.    /* Guaran
b50e0 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 62  teed to be too b
b50f0 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74 68  ig if greater th
b5100 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a  an 19 digits */.
b5110 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
b5120 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f  }else{.    /* Co
b5130 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32 5e  mpare against 2^
b5140 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  63. */.    retur
b5150 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28  n compare2pow63(
b5160 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d  zNum)<neg;.  }.}
b5170 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20  ../*.** If zNum 
b5180 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e  represents an in
b5190 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c 20  teger that will 
b51a0 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c 20  fit in 32-bits, 
b51b0 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61  then set.** *pVa
b51c0 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74 65  lue to that inte
b51d0 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ger and return t
b51e0 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20  rue.  Otherwise 
b51f0 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a  return false..**
b5200 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65  .** Any non-nume
b5210 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20 74  ric characters t
b5220 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e  hat following zN
b5230 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  um are ignored..
b5240 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66 65  ** This is diffe
b5250 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 65  rent from sqlite
b5260 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68 20  3Atoi64() which 
b5270 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20  requires the.** 
b5280 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f 20  input number to 
b5290 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  be zero-terminat
b52a0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
b52b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b52c0 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20  3GetInt32(const 
b52d0 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20  char *zNum, int 
b52e0 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69  *pValue){.  sqli
b52f0 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a  te_int64 v = 0;.
b5300 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e    int i, c;.  in
b5310 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28  t neg = 0;.  if(
b5320 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b   zNum[0]=='-' ){
b5330 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20  .    neg = 1;.  
b5340 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73    zNum++;.  }els
b5350 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27  e if( zNum[0]=='
b5360 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b  +' ){.    zNum++
b5370 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a  ;.  }.  while( z
b5380 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e  Num[0]=='0' ) zN
b5390 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  um++;.  for(i=0;
b53a0 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a 4e   i<11 && (c = zN
b53b0 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20  um[i] - '0')>=0 
b53c0 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20  && c<=9; i++){. 
b53d0 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b     v = v*10 + c;
b53e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c  .  }..  /* The l
b53f0 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20 72  ongest decimal r
b5400 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
b5410 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67 65   a 32 bit intege
b5420 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a 0a  r is 10 digits:.
b5430 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
b5440 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39 30        1234567890
b5450 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d  .  **     2^31 -
b5460 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20 2a  > 2147483648.  *
b5470 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b 0a  /.  if( i>10 ){.
b5480 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
b5490 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 31  }.  if( v-neg>21
b54a0 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20 20  47483647 ){.    
b54b0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
b54c0 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 76  if( neg ){.    v
b54d0 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56   = -v;.  }.  *pV
b54e0 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20  alue = (int)v;. 
b54f0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
b5500 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c 65  .** The variable
b5510 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
b5520 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20 66  encoding is as f
b5530 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45  ollows:.**.** KE
b5540 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20  Y:.**         A 
b5550 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 20  = 0xxxxxxx    7 
b5560 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
b5570 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
b5580 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78           B = 1xx
b5590 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20  xxxxx    7 bits 
b55a0 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20  of data and one 
b55b0 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20  flag bit.**     
b55c0 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78 78      C = xxxxxxxx
b55d0 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64 61      8 bits of da
b55e0 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73  ta.**.**  7 bits
b55f0 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20   - A.** 14 bits 
b5600 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20  - BA.** 21 bits 
b5610 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 73  - BBA.** 28 bits
b5620 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 69   - BBBA.** 35 bi
b5630 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32  ts - BBBBA.** 42
b5640 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2a   bits - BBBBBA.*
b5650 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42 42  * 49 bits - BBBB
b5660 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d  BBA.** 56 bits -
b5670 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 20   BBBBBBBA.** 64 
b5680 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42 43  bits - BBBBBBBBC
b5690 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  .*/../*.** Write
b56a0 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62   a 64-bit variab
b56b0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
b56c0 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72  r to memory star
b56d0 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a  ting at p[0]..**
b56e0 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 64   The length of d
b56f0 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20 62  ata write will b
b5700 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  e between 1 and 
b5710 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e 75  9 bytes.  The nu
b5720 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73  mber.** of bytes
b5730 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74 75   written is retu
b5740 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61  rned..**.** A va
b5750 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
b5760 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f  teger consists o
b5770 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69  f the lower 7 bi
b5780 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65 0a  ts of each byte.
b5790 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73  ** for all bytes
b57a0 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20 38   that have the 8
b57b0 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20 6f  th bit set and o
b57c0 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68 65  ne byte with the
b57d0 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61   8th.** bit clea
b57e0 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 77  r.  Except, if w
b57f0 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74 68  e get to the 9th
b5800 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65 73   byte, it stores
b5810 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62   the full.** 8 b
b5820 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20 6c  its and is the l
b5830 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c  ast byte..*/.SQL
b5840 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b5850 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
b5860 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
b5870 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e 74  p, u64 v){.  int
b5880 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62   i, j, n;.  u8 b
b5890 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 20  uf[10];.  if( v 
b58a0 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30 30  & (((u64)0xff000
b58b0 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20  000)<<32) ){.   
b58c0 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0a 20   p[8] = (u8)v;. 
b58d0 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20     v >>= 8;.    
b58e0 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69  for(i=7; i>=0; i
b58f0 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20  --){.      p[i] 
b5900 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37 66  = (u8)((v & 0x7f
b5910 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 20  ) | 0x80);.     
b5920 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d 0a   v >>= 7;.    }.
b5930 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a 20 20      return 9;.  
b5940 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b 0a 20  }    .  n = 0;. 
b5950 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e 2b 2b   do{.    buf[n++
b5960 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78  ] = (u8)((v & 0x
b5970 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20  7f) | 0x80);.   
b5980 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69   v >>= 7;.  }whi
b5990 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 75  le( v!=0 );.  bu
b59a0 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20  f[0] &= 0x7f;.  
b59b0 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a  assert( n<=9 );.
b59c0 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31    for(i=0, j=n-1
b59d0 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b  ; j>=0; j--, i++
b59e0 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 75  ){.    p[i] = bu
b59f0 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  f[j];.  }.  retu
b5a00 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn n;.}../*.** T
b5a10 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
b5a20 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e 20   faster version 
b5a30 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  of sqlite3PutVar
b5a40 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a  int() that only.
b5a50 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d  ** works for 32-
b5a60 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e 74  bit positive int
b5a70 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68 20  egers and which 
b5a80 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72  is optimized for
b5a90 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63  .** the common c
b5aa0 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74  ase of small int
b5ab0 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f 20  egers.  A MACRO 
b5ac0 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 69  version, putVari
b5ad0 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76  nt32,.** is prov
b5ae0 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e  ided which inlin
b5af0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79  es the single-by
b5b00 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f  te case.  All co
b5b10 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a  de should use.**
b5b20 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69   the MACRO versi
b5b30 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63 74  on as this funct
b5b40 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20  ion assumes the 
b5b50 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65  single-byte case
b5b60 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20   has.** already 
b5b70 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f  been handled..*/
b5b80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b5b90 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61  int sqlite3PutVa
b5ba0 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20  rint32(unsigned 
b5bb0 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b  char *p, u32 v){
b5bc0 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72 69  .#ifndef putVari
b5bd0 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26 20  nt32.  if( (v & 
b5be0 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20  ~0x7f)==0 ){.   
b5bf0 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72   p[0] = v;.    r
b5c00 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e  eturn 1;.  }.#en
b5c10 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20 7e  dif.  if( (v & ~
b5c20 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20  0x3fff)==0 ){.  
b5c30 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 28 76    p[0] = (u8)((v
b5c40 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b 0a 20 20  >>7) | 0x80);.  
b5c50 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 20    p[1] = (u8)(v 
b5c60 26 20 30 78 37 66 29 3b 0a 20 20 20 20 72 65 74  & 0x7f);.    ret
b5c70 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 2;.  }.  ret
b5c80 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 56 61  urn sqlite3PutVa
b5c90 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a 2f  rint(p, v);.}../
b5ca0 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d 62  *.** Read a 64-b
b5cb0 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67  it variable-leng
b5cc0 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20  th integer from 
b5cd0 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20  memory starting 
b5ce0 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75  at p[0]..** Retu
b5cf0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
b5d00 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68   bytes read.  Th
b5d10 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65  e value is store
b5d20 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49  d in *v..*/.SQLI
b5d30 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71  TE_PRIVATE u8 sq
b5d40 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 63  lite3GetVarint(c
b5d50 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
b5d60 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b 0a  ar *p, u64 *v){.
b5d70 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 20    u32 a,b,s;..  
b5d80 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20  a = *p;.  /* a: 
b5d90 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p0 (unmasked) */
b5da0 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29  .  if (!(a&0x80)
b5db0 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 61  ).  {.    *v = a
b5dc0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
b5dd0 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20    }..  p++;.  b 
b5de0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31  = *p;.  /* b: p1
b5df0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b5e00 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a   if (!(b&0x80)).
b5e10 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 37    {.    a &= 0x7
b5e20 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b  f;.    a = a<<7;
b5e30 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
b5e40 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74   *v = a;.    ret
b5e50 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b  urn 2;.  }..  p+
b5e60 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a  +;.  a = a<<14;.
b5e70 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    a |= *p;.  /* 
b5e80 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28  a: p0<<14 | p2 (
b5e90 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
b5ea0 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
b5eb0 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66  {.    a &= (0x7f
b5ec0 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b5ed0 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20    b &= 0x7f;.   
b5ee0 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61   b = b<<7;.    a
b5ef0 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20   |= b;.    *v = 
b5f00 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b  a;.    return 3;
b5f10 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 20  .  }..  /* CSE1 
b5f20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  from below */.  
b5f30 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
b5f40 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20  (0x7f);.  p++;. 
b5f50 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20   b = b<<14;.  b 
b5f60 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70  |= *p;.  /* b: p
b5f70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61  1<<14 | p3 (unma
b5f80 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b5f90 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (b&0x80)).  {.  
b5fa0 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    b &= (0x7f<<14
b5fb0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a  )|(0x7f);.    /*
b5fc0 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a   moved CSE1 up *
b5fd0 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30  /.    /* a &= (0
b5fe0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b5ff0 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37   */.    a = a<<7
b6000 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
b6010 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
b6020 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 4;.  }..  /
b6030 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32  * a: p0<<14 | p2
b6040 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f   (masked) */.  /
b6050 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33  * b: p1<<14 | p3
b6060 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b6070 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 66 20 70   /* 1:save off p
b6080 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c  0<<21 | p1<<14 |
b6090 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73   p2<<7 | p3 (mas
b60a0 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76  ked) */.  /* mov
b60b0 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20  ed CSE1 up */.  
b60c0 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31  /* a &= (0x7f<<1
b60d0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20  4)|(0x7f); */.  
b60e0 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  b &= (0x7f<<14)|
b60f0 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61 3b  (0x7f);.  s = a;
b6100 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 31 34 20  .  /* s: p0<<14 
b6110 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f  | p2 (masked) */
b6120 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61  ..  p++;.  a = a
b6130 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b  <<14;.  a |= *p;
b6140 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20  .  /* a: p0<<28 
b6150 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75  | p2<<14 | p4 (u
b6160 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
b6170 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b   (!(a&0x80)).  {
b6180 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73  .    /* we can s
b6190 6b 69 70 20 74 68 65 73 65 20 63 61 75 73 65 20  kip these cause 
b61a0 74 68 65 79 20 77 65 72 65 20 28 65 66 66 65 63  they were (effec
b61b0 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f  tively) done abo
b61c0 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 73  ve in calc'ing s
b61d0 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20   */.    /* a &= 
b61e0 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66  (0x7f<<28)|(0x7f
b61f0 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f  <<14)|(0x7f); */
b6200 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78  .    /* b &= (0x
b6210 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20  7f<<14)|(0x7f); 
b6220 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b  */.    b = b<<7;
b6230 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
b6240 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20   s = s>>18;.    
b6250 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33  *v = ((u64)s)<<3
b6260 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72  2 | a;.    retur
b6270 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 32  n 5;.  }..  /* 2
b6280 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 31  :save off p0<<21
b6290 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c   | p1<<14 | p2<<
b62a0 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20  7 | p3 (masked) 
b62b0 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a 20  */.  s = s<<7;. 
b62c0 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73 3a   s |= b;.  /* s:
b62d0 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34   p0<<21 | p1<<14
b62e0 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d   | p2<<7 | p3 (m
b62f0 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b  asked) */..  p++
b6300 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20  ;.  b = b<<14;. 
b6310 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62   b |= *p;.  /* b
b6320 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c 31  : p1<<28 | p3<<1
b6330 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65 64  4 | p5 (unmasked
b6340 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30  ) */.  if (!(b&0
b6350 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a  x80)).  {.    /*
b6360 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 69   we can skip thi
b6370 73 20 63 61 75 73 65 20 69 74 20 77 61 73 20 28  s cause it was (
b6380 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e  effectively) don
b6390 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27  e above in calc'
b63a0 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20  ing s */.    /* 
b63b0 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c  b &= (0x7f<<28)|
b63c0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b63d0 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28  ); */.    a &= (
b63e0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b63f0 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a  ;.    a = a<<7;.
b6400 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20      a |= b;.    
b6410 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a  s = s>>18;.    *
b6420 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32  v = ((u64)s)<<32
b6430 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   | a;.    return
b6440 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a   6;.  }..  p++;.
b6450 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61    a = a<<14;.  a
b6460 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20   |= *p;.  /* a: 
b6470 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c 31 34 20  p2<<28 | p4<<14 
b6480 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p6 (unmasked) 
b6490 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  */.  if (!(a&0x8
b64a0 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d  0)).  {.    a &=
b64b0 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37   (0x1f<<28)|(0x7
b64c0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b64d0 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31     b &= (0x7f<<1
b64e0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
b64f0 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = b<<7;.    a |
b6500 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e  = b;.    s = s>>
b6510 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75  11;.    *v = ((u
b6520 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20  64)s)<<32 | a;. 
b6530 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d     return 7;.  }
b6540 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f 6d  ..  /* CSE2 from
b6550 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d   below */.  a &=
b6560 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b6570 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d  f);.  p++;.  b =
b6580 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a   b<<14;.  b |= *
b6590 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 3c 3c 32  p;.  /* b: p3<<2
b65a0 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 70 37 20  8 | p5<<14 | p7 
b65b0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b65c0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
b65d0 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 31   {.    b &= (0x1
b65e0 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34  f<<28)|(0x7f<<14
b65f0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a  )|(0x7f);.    /*
b6600 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a   moved CSE2 up *
b6610 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30  /.    /* a &= (0
b6620 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6630 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37   */.    a = a<<7
b6640 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
b6650 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 20 20 20    s = s>>4;.    
b6660 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33  *v = ((u64)s)<<3
b6670 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72  2 | a;.    retur
b6680 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b  n 8;.  }..  p++;
b6690 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0a 20 20  .  a = a<<15;.  
b66a0 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a  a |= *p;.  /* a:
b66b0 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31 35   p4<<29 | p6<<15
b66c0 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64 29   | p8 (unmasked)
b66d0 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64 20   */..  /* moved 
b66e0 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 2f 2a 20  CSE2 up */.  /* 
b66f0 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 39 29 7c  a &= (0x7f<<29)|
b6700 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 78 66 66  (0x7f<<15)|(0xff
b6710 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78  ); */.  b &= (0x
b6720 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b6730 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 20 61 20    b = b<<8;.  a 
b6740 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 73 3c 3c  |= b;..  s = s<<
b6750 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b 0a  4;.  b = p[-4];.
b6760 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 62    b &= 0x7f;.  b
b6770 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d 20   = b>>3;.  s |= 
b6780 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 75 36 34  b;..  *v = ((u64
b6790 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20 20  )s)<<32 | a;..  
b67a0 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a  return 9;.}../*.
b67b0 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74  ** Read a 32-bit
b67c0 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
b67d0 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65   integer from me
b67e0 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74  mory starting at
b67f0 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e   p[0]..** Return
b6800 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
b6810 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65 20  ytes read.  The 
b6820 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20  value is stored 
b6830 69 6e 20 2a 76 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  in *v..**.** If 
b6840 74 68 65 20 76 61 72 69 6e 74 20 73 74 6f 72 65  the varint store
b6850 64 20 69 6e 20 70 5b 30 5d 20 69 73 20 6c 61 72  d in p[0] is lar
b6860 67 65 72 20 74 68 61 6e 20 63 61 6e 20 66 69 74  ger than can fit
b6870 20 69 6e 20 61 20 33 32 2d 62 69 74 20 75 6e 73   in a 32-bit uns
b6880 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72  igned.** integer
b6890 2c 20 74 68 65 6e 20 73 65 74 20 2a 76 20 74 6f  , then set *v to
b68a0 20 30 78 66 66 66 66 66 66 66 66 2e 0a 2a 2a 0a   0xffffffff..**.
b68b0 2a 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69  ** A MACRO versi
b68c0 6f 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32 2c  on, getVarint32,
b68d0 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69   is provided whi
b68e0 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a  ch inlines the .
b68f0 2a 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63  ** single-byte c
b6900 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73  ase.  All code s
b6910 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41  hould use the MA
b6920 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 0a  CRO version as .
b6930 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
b6940 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e   assumes the sin
b6950 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61  gle-byte case ha
b6960 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68  s already been h
b6970 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  andled..*/.SQLIT
b6980 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c  E_PRIVATE u8 sql
b6990 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28  ite3GetVarint32(
b69a0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
b69b0 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b  har *p, u32 *v){
b69c0 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 2f  .  u32 a,b;..  /
b69d0 2a 20 54 68 65 20 31 2d 62 79 74 65 20 63 61 73  * The 1-byte cas
b69e0 65 2e 20 20 4f 76 65 72 77 68 65 6c 6d 69 6e 67  e.  Overwhelming
b69f0 6c 79 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d  ly the most comm
b6a00 6f 6e 2e 20 20 48 61 6e 64 6c 65 64 20 69 6e 6c  on.  Handled inl
b6a10 69 6e 65 0a 20 20 2a 2a 20 62 79 20 74 68 65 20  ine.  ** by the 
b6a20 67 65 74 56 61 72 69 6e 33 32 28 29 20 6d 61 63  getVarin32() mac
b6a30 72 6f 20 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b 0a  ro */.  a = *p;.
b6a40 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61    /* a: p0 (unma
b6a50 73 6b 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65 66  sked) */.#ifndef
b6a60 20 67 65 74 56 61 72 69 6e 74 33 32 0a 20 20 69   getVarint32.  i
b6a70 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
b6a80 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20  {.    /* Values 
b6a90 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31 32  between 0 and 12
b6aa0 37 20 2a 2f 0a 20 20 20 20 2a 76 20 3d 20 61 3b  7 */.    *v = a;
b6ab0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
b6ac0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
b6ad0 54 68 65 20 32 2d 62 79 74 65 20 63 61 73 65 20  The 2-byte case 
b6ae0 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20  */.  p++;.  b = 
b6af0 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28  *p;.  /* b: p1 (
b6b00 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
b6b10 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20  f (!(b&0x80)).  
b6b20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20  {.    /* Values 
b6b30 62 65 74 77 65 65 6e 20 31 32 38 20 61 6e 64 20  between 128 and 
b6b40 31 36 33 38 33 20 2a 2f 0a 20 20 20 20 61 20 26  16383 */.    a &
b6b50 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20  = 0x7f;.    a = 
b6b60 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61  a<<7;.    *v = a
b6b70 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e   | b;.    return
b6b80 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68   2;.  }..  /* Th
b6b90 65 20 33 2d 62 79 74 65 20 63 61 73 65 20 2a 2f  e 3-byte case */
b6ba0 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c  .  p++;.  a = a<
b6bb0 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a  <14;.  a |= *p;.
b6bc0 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c    /* a: p0<<14 |
b6bd0 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p2 (unmasked) *
b6be0 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30  /.  if (!(a&0x80
b6bf0 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61  )).  {.    /* Va
b6c00 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 36 33  lues between 163
b6c10 38 34 20 61 6e 64 20 32 30 39 37 31 35 31 20 2a  84 and 2097151 *
b6c20 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66  /.    a &= (0x7f
b6c30 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b6c40 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20    b &= 0x7f;.   
b6c50 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a   b = b<<7;.    *
b6c60 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72  v = a | b;.    r
b6c70 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20  eturn 3;.  }..  
b6c80 2f 2a 20 41 20 33 32 2d 62 69 74 20 76 61 72 69  /* A 32-bit vari
b6c90 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  nt is used to st
b6ca0 6f 72 65 20 73 69 7a 65 20 69 6e 66 6f 72 6d 61  ore size informa
b6cb0 74 69 6f 6e 20 69 6e 20 62 74 72 65 65 73 2e 0a  tion in btrees..
b6cc0 20 20 2a 2a 20 4f 62 6a 65 63 74 73 20 61 72 65    ** Objects are
b6cd0 20 72 61 72 65 6c 79 20 6c 61 72 67 65 72 20 74   rarely larger t
b6ce0 68 61 6e 20 32 4d 69 42 20 6c 69 6d 69 74 20 6f  han 2MiB limit o
b6cf0 66 20 61 20 33 2d 62 79 74 65 20 76 61 72 69 6e  f a 3-byte varin
b6d00 74 2e 0a 20 20 2a 2a 20 41 20 33 2d 62 79 74 65  t..  ** A 3-byte
b6d10 20 76 61 72 69 6e 74 20 69 73 20 73 75 66 66 69   varint is suffi
b6d20 63 69 65 6e 74 2c 20 66 6f 72 20 65 78 61 6d 70  cient, for examp
b6d30 6c 65 2c 20 74 6f 20 72 65 63 6f 72 64 20 74 68  le, to record th
b6d40 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 61  e size.  ** of a
b6d50 20 31 30 34 38 35 36 39 2d 62 79 74 65 20 42 4c   1048569-byte BL
b6d60 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 20 20  OB or string..  
b6d70 2a 2a 0a 20 20 2a 2a 20 57 65 20 6f 6e 6c 79 20  **.  ** We only 
b6d80 75 6e 72 6f 6c 6c 20 74 68 65 20 66 69 72 73 74  unroll the first
b6d90 20 31 2d 2c 20 32 2d 2c 20 61 6e 64 20 33 2d 20   1-, 2-, and 3- 
b6da0 62 79 74 65 20 63 61 73 65 73 2e 20 20 54 68 65  byte cases.  The
b6db0 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 20   very.  ** rare 
b6dc0 6c 61 72 67 65 72 20 63 61 73 65 73 20 63 61 6e  larger cases can
b6dd0 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 74   be handled by t
b6de0 68 65 20 73 6c 6f 77 65 72 20 36 34 2d 62 69 74  he slower 64-bit
b6df0 20 76 61 72 69 6e 74 0a 20 20 2a 2a 20 72 6f 75   varint.  ** rou
b6e00 74 69 6e 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 31  tine..  */.#if 1
b6e10 0a 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34  .  {.    u64 v64
b6e20 3b 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20  ;.    u8 n;..   
b6e30 20 70 20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20 3d   p -= 2;.    n =
b6e40 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
b6e50 74 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20  t(p, &v64);.    
b6e60 61 73 73 65 72 74 28 20 6e 3e 33 20 26 26 20 6e  assert( n>3 && n
b6e70 3c 3d 39 20 29 3b 0a 20 20 20 20 69 66 28 20 28  <=9 );.    if( (
b6e80 76 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41 58  v64 & SQLITE_MAX
b6e90 5f 55 33 32 29 21 3d 76 36 34 20 29 7b 0a 20 20  _U32)!=v64 ){.  
b6ea0 20 20 20 20 2a 76 20 3d 20 30 78 66 66 66 66 66      *v = 0xfffff
b6eb0 66 66 66 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  fff;.    }else{.
b6ec0 20 20 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29        *v = (u32)
b6ed0 76 36 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  v64;.    }.    r
b6ee0 65 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 65  eturn n;.  }..#e
b6ef0 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 20 66 6f 6c  lse.  /* For fol
b6f00 6c 6f 77 69 6e 67 20 63 6f 64 65 20 28 6b 65 70  lowing code (kep
b6f10 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c  t for historical
b6f20 20 72 65 63 6f 72 64 20 6f 6e 6c 79 29 20 73 68   record only) sh
b6f30 6f 77 73 20 61 6e 0a 20 20 2a 2a 20 75 6e 72 6f  ows an.  ** unro
b6f40 6c 6c 69 6e 67 20 66 6f 72 20 74 68 65 20 33 2d  lling for the 3-
b6f50 20 61 6e 64 20 34 2d 62 79 74 65 20 76 61 72 69   and 4-byte vari
b6f60 6e 74 20 63 61 73 65 73 2e 20 20 54 68 69 73 20  nt cases.  This 
b6f70 63 6f 64 65 20 69 73 0a 20 20 2a 2a 20 73 6c 69  code is.  ** sli
b6f80 67 68 74 6c 79 20 66 61 73 74 65 72 2c 20 62 75  ghtly faster, bu
b6f90 74 20 69 74 20 69 73 20 61 6c 73 6f 20 6c 61 72  t it is also lar
b6fa0 67 65 72 20 61 6e 64 20 6d 75 63 68 20 68 61 72  ger and much har
b6fb0 64 65 72 20 74 6f 20 74 65 73 74 2e 0a 20 20 2a  der to test..  *
b6fc0 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62  /.  p++;.  b = b
b6fd0 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b  <<14;.  b |= *p;
b6fe0 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20  .  /* b: p1<<14 
b6ff0 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p3 (unmasked) 
b7000 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38  */.  if (!(b&0x8
b7010 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56  0)).  {.    /* V
b7020 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 32 30  alues between 20
b7030 39 37 31 35 32 20 61 6e 64 20 32 36 38 34 33 35  97152 and 268435
b7040 34 35 35 20 2a 2f 0a 20 20 20 20 62 20 26 3d 20  455 */.    b &= 
b7050 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b7060 29 3b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37  );.    a &= (0x7
b7070 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b7080 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20     a = a<<7;.   
b7090 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20   *v = a | b;.   
b70a0 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a   return 4;.  }..
b70b0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c    p++;.  a = a<<
b70c0 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20  14;.  a |= *p;. 
b70d0 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20   /* a: p0<<28 | 
b70e0 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d  p2<<14 | p4 (unm
b70f0 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b7100 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(a&0x80)).  {. 
b7110 20 20 20 2f 2a 20 57 61 6c 75 65 73 20 20 62 65     /* Walues  be
b7120 74 77 65 65 6e 20 32 36 38 34 33 35 34 35 36 20  tween 268435456 
b7130 61 6e 64 20 33 34 33 35 39 37 33 38 33 36 37 20  and 34359738367 
b7140 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 31  */.    a &= (0x1
b7150 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34  f<<28)|(0x7f<<14
b7160 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20  )|(0x7f);.    b 
b7170 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30  &= (0x1f<<28)|(0
b7180 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b7190 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20  .    b = b<<7;. 
b71a0 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20     *v = a | b;. 
b71b0 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d     return 5;.  }
b71c0 0a 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20 6f 6e  ..  /* We can on
b71d0 6c 79 20 72 65 61 63 68 20 74 68 69 73 20 70 6f  ly reach this po
b71e0 69 6e 74 20 77 68 65 6e 20 72 65 61 64 69 6e 67  int when reading
b71f0 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
b7200 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 20  ase.  ** file.  
b7210 49 6e 20 74 68 61 74 20 63 61 73 65 20 77 65 20  In that case we 
b7220 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79 20 68  are not in any h
b7230 75 72 72 79 2e 20 20 55 73 65 20 74 68 65 20 28  urry.  Use the (
b7240 72 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a 2a 20  relatively.  ** 
b7250 73 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d 70 75  slow) general-pu
b7260 72 70 6f 73 65 20 73 71 6c 69 74 65 33 47 65 74  rpose sqlite3Get
b7270 56 61 72 69 6e 74 28 29 20 72 6f 75 74 69 6e 65  Varint() routine
b7280 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 0a   to extract the.
b7290 20 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a 20    ** value. */. 
b72a0 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a   {.    u64 v64;.
b72b0 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70      u8 n;..    p
b72c0 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d 20 73   -= 4;.    n = s
b72d0 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
b72e0 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73  p, &v64);.    as
b72f0 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e 3c 3d  sert( n>5 && n<=
b7300 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75  9 );.    *v = (u
b7310 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74 75  32)v64;.    retu
b7320 72 6e 20 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  rn n;.  }.#endif
b7330 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
b7340 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
b7350 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20 62  ytes that will b
b7360 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f 72  e needed to stor
b7370 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 36  e the given.** 6
b7380 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a  4-bit integer..*
b7390 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b73a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69   int sqlite3Vari
b73b0 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20 20  ntLen(u64 v){.  
b73c0 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b  int i = 0;.  do{
b73d0 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20  .    i++;.    v 
b73e0 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28  >>= 7;.  }while(
b73f0 20 76 21 3d 30 20 26 26 20 41 4c 57 41 59 53 28   v!=0 && ALWAYS(
b7400 69 3c 39 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  i<9) );.  return
b7410 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65   i;.}.../*.** Re
b7420 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 66 6f  ad or write a fo
b7430 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69  ur-byte big-endi
b7440 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  an integer value
b7450 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b7460 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 47  ATE u32 sqlite3G
b7470 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75 38  et4byte(const u8
b7480 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28   *p){.  return (
b7490 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 70 5b 31  p[0]<<24) | (p[1
b74a0 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32 5d 3c 3c  ]<<16) | (p[2]<<
b74b0 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c  8) | p[3];.}.SQL
b74c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b74d0 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65   sqlite3Put4byte
b74e0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
b74f0 70 2c 20 75 33 32 20 76 29 7b 0a 20 20 70 5b 30  p, u32 v){.  p[0
b7500 5d 20 3d 20 28 75 38 29 28 76 3e 3e 32 34 29 3b  ] = (u8)(v>>24);
b7510 0a 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76  .  p[1] = (u8)(v
b7520 3e 3e 31 36 29 3b 0a 20 20 70 5b 32 5d 20 3d 20  >>16);.  p[2] = 
b7530 28 75 38 29 28 76 3e 3e 38 29 3b 0a 20 20 70 5b  (u8)(v>>8);.  p[
b7540 33 5d 20 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a 0a  3] = (u8)v;.}...
b7550 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
b7560 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
b7570 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e  ITERAL) || defin
b7580 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  ed(SQLITE_HAS_CO
b7590 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  DEC)./*.** Trans
b75a0 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 62 79  late a single by
b75b0 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f 20 61  te of Hex into a
b75c0 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68  n integer..** Th
b75d0 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
b75e0 77 6f 72 6b 73 20 69 66 20 68 20 72 65 61 6c 6c  works if h reall
b75f0 79 20 69 73 20 61 20 76 61 6c 69 64 20 68 65 78  y is a valid hex
b7600 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63 68 61 72  adecimal.** char
b7610 61 63 74 65 72 3a 20 20 30 2e 2e 39 61 2e 2e 66  acter:  0..9a..f
b7620 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 69 63 20 75  A..F.*/.static u
b7630 38 20 68 65 78 54 6f 49 6e 74 28 69 6e 74 20 68  8 hexToInt(int h
b7640 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 68 3e  ){.  assert( (h>
b7650 3d 27 30 27 20 26 26 20 68 3c 3d 27 39 27 29 20  ='0' && h<='9') 
b7660 7c 7c 20 20 28 68 3e 3d 27 61 27 20 26 26 20 68  ||  (h>='a' && h
b7670 3c 3d 27 66 27 29 20 7c 7c 20 20 28 68 3e 3d 27  <='f') ||  (h>='
b7680 41 27 20 26 26 20 68 3c 3d 27 46 27 29 20 29 3b  A' && h<='F') );
b7690 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41  .#ifdef SQLITE_A
b76a0 53 43 49 49 0a 20 20 68 20 2b 3d 20 39 2a 28 31  SCII.  h += 9*(1
b76b0 26 28 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 66  &(h>>6));.#endif
b76c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
b76d0 42 43 44 49 43 0a 20 20 68 20 2b 3d 20 39 2a 28  BCDIC.  h += 9*(
b76e0 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 6e 64  1&~(h>>4));.#end
b76f0 69 66 0a 20 20 72 65 74 75 72 6e 20 28 75 38 29  if.  return (u8)
b7700 28 68 20 26 20 30 78 66 29 3b 0a 7d 0a 23 65 6e  (h & 0xf);.}.#en
b7710 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f  dif /* !SQLITE_O
b7720 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c  MIT_BLOB_LITERAL
b7730 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43   || SQLITE_HAS_C
b7740 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64 65  ODEC */..#if !de
b7750 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
b7760 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20  T_BLOB_LITERAL) 
b7770 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
b7780 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a  E_HAS_CODEC)./*.
b7790 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 42 4c 4f  ** Convert a BLO
b77a0 42 20 6c 69 74 65 72 61 6c 20 6f 66 20 74 68 65  B literal of the
b77b0 20 66 6f 72 6d 20 22 78 27 68 68 68 68 68 68 27   form "x'hhhhhh'
b77c0 22 20 69 6e 74 6f 20 69 74 73 20 62 69 6e 61 72  " into its binar
b77d0 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65 74  y.** value.  Ret
b77e0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
b77f0 20 69 74 73 20 62 69 6e 61 72 79 20 76 61 6c 75   its binary valu
b7800 65 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  e.  Space to hol
b7810 64 20 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79 20  d the.** binary 
b7820 76 61 6c 75 65 20 68 61 73 20 62 65 65 6e 20 6f  value has been o
b7830 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c  btained from mal
b7840 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20  loc and must be 
b7850 66 72 65 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  freed by.** the 
b7860 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e  calling routine.
b7870 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b7880 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
b7890 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65  HexToBlob(sqlite
b78a0 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
b78b0 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *z, int n){.  
b78c0 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69  char *zBlob;.  i
b78d0 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d  nt i;..  zBlob =
b78e0 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
b78f0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
b7900 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b  n/2 + 1);.  n--;
b7910 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b 0a  .  if( zBlob ){.
b7920 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
b7930 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 7a  ; i+=2){.      z
b7940 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65 78  Blob[i/2] = (hex
b7950 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20  ToInt(z[i])<<4) 
b7960 7c 20 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b 31  | hexToInt(z[i+1
b7970 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42  ]);.    }.    zB
b7980 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 20  lob[i/2] = 0;.  
b7990 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f 62  }.  return zBlob
b79a0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
b79b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
b79c0 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54  LITERAL || SQLIT
b79d0 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a  E_HAS_CODEC */..
b79e0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
b79f0 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 20 66  e sqlite.magic f
b7a00 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  rom SQLITE_MAGIC
b7a10 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49 54 45 5f  _OPEN to SQLITE_
b7a20 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 52  MAGIC_BUSY..** R
b7a30 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28  eturn an error (
b7a40 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65  non-zero) if the
b7a50 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53   magic was not S
b7a60 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b7a70 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f  .** when this ro
b7a80 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
b7a90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
b7aa0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ine is called wh
b7ab0 65 6e 20 65 6e 74 65 72 69 6e 67 20 61 6e 20 53  en entering an S
b7ac0 51 4c 69 74 65 20 41 50 49 2e 20 20 54 68 65 20  QLite API.  The 
b7ad0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
b7ae0 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 63  N.** value indic
b7af0 61 74 65 73 20 74 68 61 74 20 74 68 65 20 64 61  ates that the da
b7b00 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
b7b10 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68  n passed into th
b7b20 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65 6e  e API is.** open
b7b30 20 61 6e 64 20 69 73 20 6e 6f 74 20 62 65 69 6e   and is not bein
b7b40 67 20 75 73 65 64 20 62 79 20 61 6e 6f 74 68 65  g used by anothe
b7b50 72 20 74 68 72 65 61 64 2e 20 20 42 79 20 63 68  r thread.  By ch
b7b60 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65  anging the value
b7b70 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41  .** to SQLITE_MA
b7b80 47 49 43 5f 42 55 53 59 20 77 65 20 69 6e 64 69  GIC_BUSY we indi
b7b90 63 61 74 65 20 74 68 61 74 20 74 68 65 20 63 6f  cate that the co
b7ba0 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 75  nnection is in u
b7bb0 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61  se..** sqlite3Sa
b7bc0 66 65 74 79 4f 66 66 28 29 20 62 65 6c 6f 77 20  fetyOff() below 
b7bd0 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 68 65 20  will change the 
b7be0 76 61 6c 75 65 20 62 61 63 6b 20 74 6f 20 53 51  value back to SQ
b7bf0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a  LITE_MAGIC_OPEN.
b7c00 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 50 49 20  ** when the API 
b7c10 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  exits. .**.** Th
b7c20 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
b7c30 61 74 74 65 6d 70 74 20 74 6f 20 64 65 74 65 63  attempt to detec
b7c40 74 20 69 66 20 74 77 6f 20 74 68 72 65 61 64 73  t if two threads
b7c50 20 75 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65   use the.** same
b7c60 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72   sqlite* pointer
b7c70 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
b7c80 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 72  e.  There is a r
b7c90 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f  ace .** conditio
b7ca0 6e 20 73 6f 20 69 74 20 69 73 20 70 6f 73 73 69  n so it is possi
b7cb0 62 6c 65 20 74 68 61 74 20 74 68 65 20 65 72 72  ble that the err
b7cc0 6f 72 20 69 73 20 6e 6f 74 20 64 65 74 65 63 74  or is not detect
b7cd0 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61 6c  ed..** But usual
b7ce0 6c 79 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 77  ly the problem w
b7cf0 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20 20 54 68  ill be seen.  Th
b7d00 65 20 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 65  e result will be
b7d10 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68 69   an.** error whi
b7d20 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ch can be used t
b7d30 6f 20 64 65 62 75 67 20 74 68 65 20 61 70 70 6c  o debug the appl
b7d40 69 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 0a  ication that is.
b7d50 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20  ** using SQLite 
b7d60 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a  incorrectly..**.
b7d70 2a 2a 20 54 69 63 6b 65 74 20 23 32 30 32 3a 20  ** Ticket #202: 
b7d80 20 49 66 20 64 62 2d 3e 6d 61 67 69 63 20 69 73   If db->magic is
b7d90 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6f 70 65   not a valid ope
b7da0 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65 20 63 61  n value, take ca
b7db0 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64  re not.** to mod
b7dc0 69 66 79 20 74 68 65 20 64 62 20 73 74 72 75 63  ify the db struc
b7dd0 74 75 72 65 20 61 74 20 61 6c 6c 2e 20 20 49 74  ture at all.  It
b7de0 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 64   could be that d
b7df0 62 20 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a 20  b is a stale.** 
b7e00 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74 68  pointer.  In oth
b7e10 65 72 20 77 6f 72 64 73 2c 20 69 74 20 63 6f 75  er words, it cou
b7e20 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 72 65  ld be that there
b7e30 20 68 61 73 20 62 65 65 6e 20 61 20 70 72 69 6f   has been a prio
b7e40 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c  r.** call to sql
b7e50 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 61  ite3_close(db) a
b7e60 6e 64 20 64 62 20 68 61 73 20 62 65 65 6e 20 64  nd db has been d
b7e70 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e 64  eallocated.  And
b7e80 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61   we do.** not wa
b7e90 6e 74 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f  nt to write into
b7ea0 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d   deallocated mem
b7eb0 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ory..*/.#ifdef S
b7ec0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
b7ed0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b7ee0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73  qlite3SafetyOn(s
b7ef0 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
b7f00 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  f( db->magic==SQ
b7f10 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
b7f20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63  ){.    db->magic
b7f30 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
b7f40 42 55 53 59 3b 0a 20 20 20 20 61 73 73 65 72 74  BUSY;.    assert
b7f50 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
b7f60 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
b7f70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
b7f80 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d  .  }else if( db-
b7f90 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d  >magic==SQLITE_M
b7fa0 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20  AGIC_BUSY ){.   
b7fb0 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
b7fc0 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b  ITE_MAGIC_ERROR;
b7fd0 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e  .    db->u1.isIn
b7fe0 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20  terrupted = 1;. 
b7ff0 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
b8000 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
b8010 68 61 6e 67 65 20 74 68 65 20 6d 61 67 69 63 20  hange the magic 
b8020 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49  from SQLITE_MAGI
b8030 43 5f 42 55 53 59 20 74 6f 20 53 51 4c 49 54 45  C_BUSY to SQLITE
b8040 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20  _MAGIC_OPEN..** 
b8050 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
b8060 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68  (non-zero) if th
b8070 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20  e magic was not 
b8080 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
b8090 59 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72  Y.** when this r
b80a0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
b80b0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
b80c0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
b80d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b80e0 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c  te3SafetyOff(sql
b80f0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28  ite3 *db){.  if(
b8100 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49   db->magic==SQLI
b8110 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b  TE_MAGIC_BUSY ){
b8120 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  .    db->magic =
b8130 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
b8140 45 4e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  EN;.    assert( 
b8150 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
b8160 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
b8170 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b8180 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e   }else{.    db->
b8190 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d  magic = SQLITE_M
b81a0 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20  AGIC_ERROR;.    
b81b0 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
b81c0 70 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65  pted = 1;.    re
b81d0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65  turn 1;.  }.}.#e
b81e0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ndif../*.** Chec
b81f0 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77  k to make sure w
b8200 65 20 68 61 76 65 20 61 20 76 61 6c 69 64 20 64  e have a valid d
b8210 62 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 69 73  b pointer.  This
b8220 20 74 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a 20   test is not.** 
b8230 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69 74  foolproof but it
b8240 20 64 6f 65 73 20 70 72 6f 76 69 64 65 20 73 6f   does provide so
b8250 6d 65 20 6d 65 61 73 75 72 65 20 6f 66 20 70 72  me measure of pr
b8260 6f 74 65 63 74 69 6f 6e 20 61 67 61 69 6e 73 74  otection against
b8270 0a 2a 2a 20 6d 69 73 75 73 65 20 6f 66 20 74 68  .** misuse of th
b8280 65 20 69 6e 74 65 72 66 61 63 65 20 73 75 63 68  e interface such
b8290 20 61 73 20 70 61 73 73 69 6e 67 20 69 6e 20 64   as passing in d
b82a0 62 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20  b pointers that 
b82b0 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77  are.** NULL or w
b82c0 68 69 63 68 20 68 61 76 65 20 62 65 65 6e 20 70  hich have been p
b82d0 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64  reviously closed
b82e0 2e 20 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  .  If this routi
b82f0 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31 20  ne returns.** 1 
b8300 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
b8310 65 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20  e db pointer is 
b8320 76 61 6c 69 64 20 61 6e 64 20 30 20 69 66 20 69  valid and 0 if i
b8330 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a  t should not be.
b8340 2a 2a 20 64 65 72 65 66 65 72 65 6e 63 65 64 20  ** dereferenced 
b8350 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20  for any reason. 
b8360 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   The calling fun
b8370 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76  ction should inv
b8380 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49  oke.** SQLITE_MI
b8390 53 55 53 45 20 69 6d 6d 65 64 69 61 74 65 6c 79  SUSE immediately
b83a0 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 53  ..**.** sqlite3S
b83b0 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 72  afetyCheckOk() r
b83c0 65 71 75 69 72 65 73 20 74 68 61 74 20 74 68 65  equires that the
b83d0 20 64 62 20 70 6f 69 6e 74 65 72 20 62 65 20 76   db pointer be v
b83e0 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65 2e  alid for.** use.
b83f0 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43    sqlite3SafetyC
b8400 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 61  heckSickOrOk() a
b8410 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f 69 6e 74  llows a db point
b8420 65 72 20 74 68 61 74 20 66 61 69 6c 65 64 20 74  er that failed t
b8430 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65 72  o.** open proper
b8440 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 66 69  ly and is not fi
b8450 74 20 66 6f 72 20 67 65 6e 65 72 61 6c 20 75 73  t for general us
b8460 65 20 62 75 74 20 77 68 69 63 68 20 63 61 6e 20  e but which can 
b8470 62 65 0a 2a 2a 20 75 73 65 64 20 61 73 20 61 6e  be.** used as an
b8480 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
b8490 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 6f 72  ite3_errmsg() or
b84a0 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29   sqlite3_close()
b84b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b84c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
b84d0 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c  afetyCheckOk(sql
b84e0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32  ite3 *db){.  u32
b84f0 20 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62   magic;.  if( db
b8500 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
b8510 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61    magic = db->ma
b8520 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63  gic;.  if( magic
b8530 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  !=SQLITE_MAGIC_O
b8540 50 45 4e 20 0a 23 69 66 64 65 66 20 53 51 4c 49  PEN .#ifdef SQLI
b8550 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 26 26  TE_DEBUG.     &&
b8560 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d   magic!=SQLITE_M
b8570 41 47 49 43 5f 42 55 53 59 0a 23 65 6e 64 69 66  AGIC_BUSY.#endif
b8580 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
b8590 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
b85a0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d   return 1;.  }.}
b85b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b85c0 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
b85d0 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73  yCheckSickOrOk(s
b85e0 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75  qlite3 *db){.  u
b85f0 33 32 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67 69  32 magic;.  magi
b8600 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20  c = db->magic;. 
b8610 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49   if( magic!=SQLI
b8620 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 26  TE_MAGIC_SICK &&
b8630 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51  .      magic!=SQ
b8640 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
b8650 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d  &&.      magic!=
b8660 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
b8670 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  Y ) return 0;.  
b8680 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a  return 1;.}../**
b8690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
b86a0 20 6f 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a   of util.c *****
b86b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b86c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b86d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
b86e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
b86f0 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 63 20 2a  in file hash.c *
b8700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
b8730 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
b8740 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 22.**.** The a
b8750 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
b8760 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
b8770 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
b8780 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
b8790 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
b87a0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
b87b0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
b87c0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
b87d0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
b87e0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
b87f0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
b8800 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
b8810 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
b8820 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
b8830 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
b8840 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
b8850 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
b8860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b88a0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ***.** This is t
b88b0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
b88c0 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73  n of generic has
b88d0 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65 64  h-tables.** used
b88e0 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a   in SQLite..*/..
b88f0 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d  /* Turn bulk mem
b8900 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68 20  ory into a hash 
b8910 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 20  table object by 
b8920 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
b8930 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 68  .** fields of th
b8940 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72 65  e Hash structure
b8950 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69  ..**.** "pNew" i
b8960 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
b8970 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 68  he hash table th
b8980 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69 74  at is to be init
b8990 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ialized..*/.SQLI
b89a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
b89b0 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
b89c0 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a 20 20 61  Hash *pNew){.  a
b89d0 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29  ssert( pNew!=0 )
b89e0 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20  ;.  pNew->first 
b89f0 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75  = 0;.  pNew->cou
b8a00 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  nt = 0;.  pNew->
b8a10 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e  htsize = 0;.  pN
b8a20 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  ew->ht = 0;.}../
b8a30 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74  * Remove all ent
b8a40 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68  ries from a hash
b8a50 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d   table.  Reclaim
b8a60 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20   all memory..** 
b8a70 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  Call this routin
b8a80 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61  e to delete a ha
b8a90 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72  sh table or to r
b8aa0 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 6c  eset a hash tabl
b8ab0 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74  e.** to the empt
b8ac0 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49  y state..*/.SQLI
b8ad0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
b8ae0 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
b8af0 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 61  (Hash *pH){.  Ha
b8b00 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20  shElem *elem;   
b8b10 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
b8b20 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c  ping over all el
b8b30 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61  ements of the ta
b8b40 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ble */..  assert
b8b50 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65  ( pH!=0 );.  ele
b8b60 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20  m = pH->first;. 
b8b70 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a   pH->first = 0;.
b8b80 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
b8b90 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74  H->ht);.  pH->ht
b8ba0 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69   = 0;.  pH->htsi
b8bb0 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  ze = 0;.  while(
b8bc0 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73   elem ){.    Has
b8bd0 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d  hElem *next_elem
b8be0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
b8bf0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
b8c00 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20  elem);.    elem 
b8c10 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d  = next_elem;.  }
b8c20 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30  .  pH->count = 0
b8c30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  ;.}../*.** The h
b8c40 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e  ashing function.
b8c50 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 67  .*/.static unsig
b8c60 6e 65 64 20 69 6e 74 20 73 74 72 48 61 73 68 28  ned int strHash(
b8c70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
b8c80 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20  nt nKey){.  int 
b8c90 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  h = 0;.  assert(
b8ca0 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 77 68   nKey>=0 );.  wh
b8cb0 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 20 29  ile( nKey > 0  )
b8cc0 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29  {.    h = (h<<3)
b8cd0 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33 55 70   ^ h ^ sqlite3Up
b8ce0 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69  perToLower[(unsi
b8cf0 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b  gned char)*z++];
b8d00 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d  .    nKey--;.  }
b8d10 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a 7d 0a 0a  .  return h;.}..
b8d20 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 65 6c  ./* Link pNew el
b8d30 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68  ement into the h
b8d40 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 49  ash table pH.  I
b8d50 66 20 70 45 6e 74 72 79 21 3d 30 20 74 68 65 6e  f pEntry!=0 then
b8d60 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 74 20   also.** insert 
b8d70 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 20 70 45  pNew into the pE
b8d80 6e 74 72 79 20 68 61 73 68 20 62 75 63 6b 65 74  ntry hash bucket
b8d90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
b8da0 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a   insertElement(.
b8db0 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20    Hash *pH,     
b8dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
b8dd0 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61  complete hash ta
b8de0 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ble */.  struct 
b8df0 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 20 20  _ht *pEntry,    
b8e00 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69 6e 74  /* The entry int
b8e10 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69 73 20  o which pNew is 
b8e20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 48 61  inserted */.  Ha
b8e30 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20 20  shElem *pNew    
b8e40 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d       /* The elem
b8e50 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ent to be insert
b8e60 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45  ed */.){.  HashE
b8e70 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20  lem *pHead;     
b8e80 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65    /* First eleme
b8e90 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 70 45  nt already in pE
b8ea0 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 20 70 45  ntry */.  if( pE
b8eb0 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 48 65 61  ntry ){.    pHea
b8ec0 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  d = pEntry->coun
b8ed0 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  t ? pEntry->chai
b8ee0 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 6e 74 72  n : 0;.    pEntr
b8ef0 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20  y->count++;.    
b8f00 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20  pEntry->chain = 
b8f10 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pNew;.  }else{. 
b8f20 20 20 20 70 48 65 61 64 20 3d 20 30 3b 0a 20 20     pHead = 0;.  
b8f30 7d 0a 20 20 69 66 28 20 70 48 65 61 64 20 29 7b  }.  if( pHead ){
b8f40 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20  .    pNew->next 
b8f50 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e 65  = pHead;.    pNe
b8f60 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64 2d  w->prev = pHead-
b8f70 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20 70  >prev;.    if( p
b8f80 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70 48  Head->prev ){ pH
b8f90 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20  ead->prev->next 
b8fa0 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c  = pNew; }.    el
b8fb0 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  se             {
b8fc0 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65   pH->first = pNe
b8fd0 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e  w; }.    pHead->
b8fe0 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  prev = pNew;.  }
b8ff0 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  else{.    pNew->
b9000 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73 74  next = pH->first
b9010 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66 69  ;.    if( pH->fi
b9020 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73 74  rst ){ pH->first
b9030 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d  ->prev = pNew; }
b9040 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20  .    pNew->prev 
b9050 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72  = 0;.    pH->fir
b9060 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d  st = pNew;.  }.}
b9070 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65  .../* Resize the
b9080 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74   hash table so t
b9090 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20  hat it cantains 
b90a0 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65  "new_size" bucke
b90b0 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61  ts..**.** The ha
b90c0 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66  sh table might f
b90d0 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 20 69 66  ail to resize if
b90e0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
b90f0 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 69 66  ) fails or.** if
b9100 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 69 73   the new size is
b9110 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
b9120 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20   prior size..** 
b9130 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
b9140 68 65 20 72 65 73 69 7a 65 20 6f 63 63 75 72 73  he resize occurs
b9150 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e 6f   and false if no
b9160 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
b9170 20 72 65 68 61 73 68 28 48 61 73 68 20 2a 70 48   rehash(Hash *pH
b9180 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e  , unsigned int n
b9190 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75  ew_size){.  stru
b91a0 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20  ct _ht *new_ht; 
b91b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
b91c0 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65  e new hash table
b91d0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
b91e0 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d  elem, *next_elem
b91f0 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70  ;    /* For loop
b9200 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e  ing over existin
b9210 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23  g elements */..#
b9220 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43  if SQLITE_MALLOC
b9230 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a 20 20  _SOFT_LIMIT>0.  
b9240 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a  if( new_size*siz
b9250 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e  eof(struct _ht)>
b9260 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f  SQLITE_MALLOC_SO
b9270 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20  FT_LIMIT ){.    
b9280 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54  new_size = SQLIT
b9290 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
b92a0 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63  MIT/sizeof(struc
b92b0 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66  t _ht);.  }.  if
b92c0 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e  ( new_size==pH->
b92d0 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e 20  htsize ) return 
b92e0 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  0;.#endif..  /* 
b92f0 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20 74 6f  The inability to
b9300 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 65   allocates space
b9310 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 68 61   for a larger ha
b9320 73 68 20 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a  sh table is.  **
b9330 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68   a performance h
b9340 69 74 20 62 75 74 20 69 74 20 69 73 20 6e 6f 74  it but it is not
b9350 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20   a fatal error. 
b9360 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a 20 20 2a   So mark the.  *
b9370 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 73 20  * allocation as 
b9380 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a 2f 0a 20  a benign..  */. 
b9390 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
b93a0 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 6e  ignMalloc();.  n
b93b0 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74 20  ew_ht = (struct 
b93c0 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  _ht *)sqlite3Mal
b93d0 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69  loc( new_size*si
b93e0 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29  zeof(struct _ht)
b93f0 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64   );.  sqlite3End
b9400 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
b9410 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30  .  if( new_ht==0
b9420 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73   ) return 0;.  s
b9430 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e  qlite3_free(pH->
b9440 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20  ht);.  pH->ht = 
b9450 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74  new_ht;.  pH->ht
b9460 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 20  size = new_size 
b9470 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
b9480 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a 65  ize(new_ht)/size
b9490 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a  of(struct _ht);.
b94a0 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74 2c    memset(new_ht,
b94b0 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a   0, new_size*siz
b94c0 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 29  eof(struct _ht))
b94d0 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d  ;.  for(elem=pH-
b94e0 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73  >first, pH->firs
b94f0 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20  t=0; elem; elem 
b9500 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20  = next_elem){.  
b9510 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
b9520 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d 2d   = strHash(elem-
b9530 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65  >pKey, elem->nKe
b9540 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b 0a 20  y) % new_size;. 
b9550 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65     next_elem = e
b9560 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69  lem->next;.    i
b9570 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c  nsertElement(pH,
b9580 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65   &new_ht[h], ele
b9590 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  m);.  }.  return
b95a0 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66   1;.}../* This f
b95b0 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74  unction (for int
b95c0 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20  ernal use only) 
b95d0 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d 65  locates an eleme
b95e0 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68  nt in an.** hash
b95f0 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63   table that matc
b9600 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b 65  hes the given ke
b9610 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f 72  y.  The hash for
b9620 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a   this key has.**
b9630 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f   already been co
b9640 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70 61  mputed and is pa
b9650 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68 20  ssed as the 4th 
b9660 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74  parameter..*/.st
b9670 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a 66  atic HashElem *f
b9680 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48  indElementGivenH
b9690 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61 73  ash(.  const Has
b96a0 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54 68  h *pH,     /* Th
b96b0 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 63  e pH to be searc
b96c0 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  hed */.  const c
b96d0 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20  har *pKey,   /* 
b96e0 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 73  The key we are s
b96f0 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a  earching for */.
b9700 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20    int nKey,     
b9710 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69        /* Bytes i
b9720 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e 74  n key (not count
b9730 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  ing zero termina
b9740 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  tor) */.  unsign
b9750 65 64 20 69 6e 74 20 68 20 20 20 20 20 20 2f 2a  ed int h      /*
b9760 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68   The hash for th
b9770 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20  is key. */.){.  
b9780 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
b9790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
b97a0 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74  * Used to loop t
b97b0 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  hru the element 
b97c0 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f  list */.  int co
b97d0 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  unt;            
b97e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
b97f0 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c  er of elements l
b9800 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a 0a  eft to test */..
b9810 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a    if( pH->ht ){.
b9820 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a      struct _ht *
b9830 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
b9840 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20  [h];.    elem = 
b9850 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20  pEntry->chain;. 
b9860 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72     count = pEntr
b9870 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c 73  y->count;.  }els
b9880 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 48  e{.    elem = pH
b9890 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 6f 75  ->first;.    cou
b98a0 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b 0a  nt = pH->count;.
b98b0 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 6f 75    }.  while( cou
b98c0 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28 65  nt-- && ALWAYS(e
b98d0 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 28 20  lem) ){.    if( 
b98e0 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 79  elem->nKey==nKey
b98f0 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
b9900 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 70  Cmp(elem->pKey,p
b9910 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20  Key,nKey)==0 ){ 
b9920 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 6c  .      return el
b9930 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c  em;.    }.    el
b9940 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  em = elem->next;
b9950 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
b9960 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20  .}../* Remove a 
b9970 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f  single entry fro
b9980 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
b9990 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72   given a pointer
b99a0 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d   to that.** elem
b99b0 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 6f  ent and a hash o
b99c0 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 20  n the element's 
b99d0 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  key..*/.static v
b99e0 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e  oid removeElemen
b99f0 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48 61  tGivenHash(.  Ha
b9a00 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20  sh *pH,         
b9a10 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69  /* The pH contai
b9a20 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20  ning "elem" */. 
b9a30 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c   HashElem* elem,
b9a40 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e     /* The elemen
b9a50 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 20  t to be removed 
b9a60 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20  from the pH */. 
b9a70 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20   unsigned int h 
b9a80 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65     /* Hash value
b9a90 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e 74   for the element
b9aa0 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
b9ab0 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69  _ht *pEntry;.  i
b9ac0 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b  f( elem->prev ){
b9ad0 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d  .    elem->prev-
b9ae0 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65  >next = elem->ne
b9af0 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  xt; .  }else{.  
b9b00 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c    pH->first = el
b9b10 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20  em->next;.  }.  
b9b20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29  if( elem->next )
b9b30 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74  {.    elem->next
b9b40 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70  ->prev = elem->p
b9b50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  rev;.  }.  if( p
b9b60 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 45 6e  H->ht ){.    pEn
b9b70 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d  try = &pH->ht[h]
b9b80 3b 0a 20 20 20 20 69 66 28 20 70 45 6e 74 72 79  ;.    if( pEntry
b9b90 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b  ->chain==elem ){
b9ba0 0a 20 20 20 20 20 20 70 45 6e 74 72 79 2d 3e 63  .      pEntry->c
b9bb0 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  hain = elem->nex
b9bc0 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 6e  t;.    }.    pEn
b9bd0 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20  try->count--;.  
b9be0 20 20 61 73 73 65 72 74 28 20 70 45 6e 74 72 79    assert( pEntry
b9bf0 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20  ->count>=0 );.  
b9c00 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
b9c10 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e  ( elem );.  pH->
b9c20 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70  count--;.  if( p
b9c30 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20  H->count<=0 ){. 
b9c40 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66     assert( pH->f
b9c50 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61  irst==0 );.    a
b9c60 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74  ssert( pH->count
b9c70 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
b9c80 65 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b  e3HashClear(pH);
b9c90 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d  .  }.}../* Attem
b9ca0 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20  pt to locate an 
b9cb0 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68  element of the h
b9cc0 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74  ash table pH wit
b9cd0 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20  h a key.** that 
b9ce0 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65  matches pKey,nKe
b9cf0 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 64  y.  Return the d
b9d00 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65  ata for this ele
b9d10 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a  ment if it is.**
b9d20 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20   found, or NULL 
b9d30 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
b9d40 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  atch..*/.SQLITE_
b9d50 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
b9d60 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f  lite3HashFind(co
b9d70 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63 6f  nst Hash *pH, co
b9d80 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20  nst char *pKey, 
b9d90 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61 73  int nKey){.  Has
b9da0 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
b9db0 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
b9dc0 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20  hat matches key 
b9dd0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
b9de0 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61 73  t h;    /* A has
b9df0 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20 61  h on key */..  a
b9e00 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a  ssert( pH!=0 );.
b9e10 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 21 3d    assert( pKey!=
b9e20 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
b9e30 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  Key>=0 );.  if( 
b9e40 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 68 20  pH->ht ){.    h 
b9e50 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c 20  = strHash(pKey, 
b9e60 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73 69  nKey) % pH->htsi
b9e70 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ze;.  }else{.   
b9e80 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c   h = 0;.  }.  el
b9e90 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74  em = findElement
b9ea0 47 69 76 65 6e 48 61 73 68 28 70 48 2c 20 70 4b  GivenHash(pH, pK
b9eb0 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a 20 20  ey, nKey, h);.  
b9ec0 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c  return elem ? el
b9ed0 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a  em->data : 0;.}.
b9ee0 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c  ./* Insert an el
b9ef0 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68  ement into the h
b9f00 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54  ash table pH.  T
b9f10 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e  he key is pKey,n
b9f20 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64  Key.** and the d
b9f30 61 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a 2a  ata is "data"..*
b9f40 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65  *.** If no eleme
b9f50 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20 61  nt exists with a
b9f60 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 74   matching key, t
b9f70 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65  hen a new.** ele
b9f80 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 20  ment is created 
b9f90 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  and NULL is retu
b9fa0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rned..**.** If a
b9fb0 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 61  nother element a
b9fc0 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 77 69  lready exists wi
b9fd0 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79 2c  th the same key,
b9fe0 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77   then the.** new
b9ff0 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 20 74   data replaces t
ba000 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 20  he old data and 
ba010 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20  the old data is 
ba020 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65  returned..** The
ba030 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 69   key is not copi
ba040 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 61  ed in this insta
ba050 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f  nce.  If a mallo
ba060 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a  c fails, then.**
ba070 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 73   the new data is
ba080 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
ba090 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
ba0a0 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  unchanged..**.**
ba0b0 20 49 66 20 74 68 65 20 22 64 61 74 61 22 20 70   If the "data" p
ba0c0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73  arameter to this
ba0d0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c   function is NUL
ba0e0 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65  L, then the.** e
ba0f0 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e  lement correspon
ba100 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 73  ding to "key" is
ba110 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
ba120 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f  e hash table..*/
ba130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ba140 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73  void *sqlite3Has
ba150 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a 70 48  hInsert(Hash *pH
ba160 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b  , const char *pK
ba170 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f  ey, int nKey, vo
ba180 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 6e 73  id *data){.  uns
ba190 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 20  igned int h;    
ba1a0 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 6f     /* the hash o
ba1b0 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f  f the key modulo
ba1c0 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a 65   hash table size
ba1d0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
ba1e0 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55  elem;       /* U
ba1f0 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75  sed to loop thru
ba200 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73   the element lis
ba210 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  t */.  HashElem 
ba220 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20  *new_elem;   /* 
ba230 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65  New element adde
ba240 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 0a  d to the pH */..
ba250 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20    assert( pH!=0 
ba260 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65  );.  assert( pKe
ba270 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  y!=0 );.  assert
ba280 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69  ( nKey>=0 );.  i
ba290 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b  f( pH->htsize ){
ba2a0 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68  .    h = strHash
ba2b0 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70  (pKey, nKey) % p
ba2c0 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c  H->htsize;.  }el
ba2d0 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20  se{.    h = 0;. 
ba2e0 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64   }.  elem = find
ba2f0 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68  ElementGivenHash
ba300 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29  (pH,pKey,nKey,h)
ba310 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a  ;.  if( elem ){.
ba320 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61      void *old_da
ba330 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b  ta = elem->data;
ba340 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30  .    if( data==0
ba350 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76 65   ){.      remove
ba360 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68  ElementGivenHash
ba370 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20  (pH,elem,h);.   
ba380 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c   }else{.      el
ba390 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b  em->data = data;
ba3a0 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65  .      elem->pKe
ba3b0 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20  y = pKey;.      
ba3c0 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c 65  assert(nKey==ele
ba3d0 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 7d 0a  m->nKey);.    }.
ba3e0 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64      return old_d
ba3f0 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64  ata;.  }.  if( d
ba400 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ata==0 ) return 
ba410 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20  0;.  new_elem = 
ba420 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c 69 74  (HashElem*)sqlit
ba430 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  e3Malloc( sizeof
ba440 28 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20  (HashElem) );.  
ba450 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20  if( new_elem==0 
ba460 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20  ) return data;. 
ba470 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20   new_elem->pKey 
ba480 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c  = pKey;.  new_el
ba490 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b  em->nKey = nKey;
ba4a0 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74  .  new_elem->dat
ba4b0 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 2d 3e  a = data;.  pH->
ba4c0 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70  count++;.  if( p
ba4d0 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 26 20  H->count>=10 && 
ba4e0 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a 70 48  pH->count > 2*pH
ba4f0 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 20  ->htsize ){.    
ba500 69 66 28 20 72 65 68 61 73 68 28 70 48 2c 20 70  if( rehash(pH, p
ba510 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 7b 0a 20  H->count*2) ){. 
ba520 20 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d       assert( pH-
ba530 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 20  >htsize>0 );.   
ba540 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70     h = strHash(p
ba550 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d  Key, nKey) % pH-
ba560 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20  >htsize;.    }. 
ba570 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20   }.  if( pH->ht 
ba580 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65  ){.    insertEle
ba590 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74  ment(pH, &pH->ht
ba5a0 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a  [h], new_elem);.
ba5b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 73    }else{.    ins
ba5c0 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 30  ertElement(pH, 0
ba5d0 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 7d  , new_elem);.  }
ba5e0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
ba5f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
ba600 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a  End of hash.c **
ba610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
ba640 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
ba650 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64  Begin file opcod
ba660 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.c ***********
ba670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
ba690 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79  /* Automatically
ba6a0 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20   generated.  Do 
ba6b0 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53  not edit */./* S
ba6c0 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63  ee the mkopcodec
ba6d0 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20  .awk script for 
ba6e0 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20  details. */.#if 
ba6f0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
ba700 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c  OMIT_EXPLAIN) ||
ba710 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
ba720 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42  ) || defined(VDB
ba730 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65  E_PROFILE) || de
ba740 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
ba750 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  UG).SQLITE_PRIVA
ba760 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
ba770 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65  qlite3OpcodeName
ba780 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 63  (int i){. static
ba790 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
ba7a0 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20  st azName[] = { 
ba7b0 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31  "?",.     /*   1
ba7c0 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20   */ "Goto",.    
ba7d0 20 2f 2a 20 20 20 32 20 2a 2f 20 22 47 6f 73 75   /*   2 */ "Gosu
ba7e0 62 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 33 20  b",.     /*   3 
ba7f0 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 20  */ "Return",.   
ba800 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 59 69 65    /*   4 */ "Yie
ba810 6c 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35  ld",.     /*   5
ba820 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22   */ "HaltIfNull"
ba830 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36 20 2a 2f  ,.     /*   6 */
ba840 20 22 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a   "Halt",.     /*
ba850 20 20 20 37 20 2a 2f 20 22 49 6e 74 65 67 65 72     7 */ "Integer
ba860 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 38 20 2a  ",.     /*   8 *
ba870 2f 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 20 20  / "Int64",.     
ba880 2f 2a 20 20 20 39 20 2a 2f 20 22 53 74 72 69 6e  /*   9 */ "Strin
ba890 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20  g",.     /*  10 
ba8a0 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20  */ "Null",.     
ba8b0 2f 2a 20 20 31 31 20 2a 2f 20 22 42 6c 6f 62 22  /*  11 */ "Blob"
ba8c0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f  ,.     /*  12 */
ba8d0 20 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20 20   "Variable",.   
ba8e0 20 20 2f 2a 20 20 31 33 20 2a 2f 20 22 4d 6f 76    /*  13 */ "Mov
ba8f0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20  e",.     /*  14 
ba900 2a 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20  */ "Copy",.     
ba910 2f 2a 20 20 31 35 20 2a 2f 20 22 53 43 6f 70 79  /*  15 */ "SCopy
ba920 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 2a  ",.     /*  16 *
ba930 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20  / "ResultRow",. 
ba940 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 43      /*  17 */ "C
ba950 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a  ollSeq",.     /*
ba960 20 20 31 38 20 2a 2f 20 22 46 75 6e 63 74 69 6f    18 */ "Functio
ba970 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39 20  n",.     /*  19 
ba980 2a 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f  */ "Not",.     /
ba990 2a 20 20 32 30 20 2a 2f 20 22 41 64 64 49 6d 6d  *  20 */ "AddImm
ba9a0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 31 20 2a  ",.     /*  21 *
ba9b0 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20  / "MustBeInt",. 
ba9c0 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 52      /*  22 */ "R
ba9d0 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 20  ealAffinity",.  
ba9e0 20 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 50 65     /*  23 */ "Pe
ba9f0 72 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 20 20  rmutation",.    
baa00 20 2f 2a 20 20 32 34 20 2a 2f 20 22 43 6f 6d 70   /*  24 */ "Comp
baa10 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32  are",.     /*  2
baa20 35 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 20  5 */ "Jump",.   
baa30 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 49 66 22    /*  26 */ "If"
baa40 2c 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f  ,.     /*  27 */
baa50 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f   "IfNot",.     /
baa60 2a 20 20 32 38 20 2a 2f 20 22 43 6f 6c 75 6d 6e  *  28 */ "Column
baa70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a  ",.     /*  29 *
baa80 2f 20 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 20  / "Affinity",.  
baa90 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 4d 61     /*  30 */ "Ma
baaa0 6b 65 52 65 63 6f 72 64 22 2c 0a 20 20 20 20 20  keRecord",.     
baab0 2f 2a 20 20 33 31 20 2a 2f 20 22 43 6f 75 6e 74  /*  31 */ "Count
baac0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 2a  ",.     /*  32 *
baad0 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20  / "Savepoint",. 
baae0 20 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22 41      /*  33 */ "A
baaf0 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20  utoCommit",.    
bab00 20 2f 2a 20 20 33 34 20 2a 2f 20 22 54 72 61 6e   /*  34 */ "Tran
bab10 73 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f  saction",.     /
bab20 2a 20 20 33 35 20 2a 2f 20 22 52 65 61 64 43 6f  *  35 */ "ReadCo
bab30 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  okie",.     /*  
bab40 33 36 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65  36 */ "SetCookie
bab50 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 37 20 2a  ",.     /*  37 *
bab60 2f 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 22  / "VerifyCookie"
bab70 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f  ,.     /*  38 */
bab80 20 22 4f 70 65 6e 52 65 61 64 22 2c 0a 20 20 20   "OpenRead",.   
bab90 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 4f 70 65    /*  39 */ "Ope
baba0 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a  nWrite",.     /*
babb0 20 20 34 30 20 2a 2f 20 22 4f 70 65 6e 45 70 68    40 */ "OpenEph
babc0 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20 2f 2a  emeral",.     /*
babd0 20 20 34 31 20 2a 2f 20 22 4f 70 65 6e 50 73 65    41 */ "OpenPse
babe0 75 64 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  udo",.     /*  4
babf0 32 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20  2 */ "Close",.  
bac00 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 53 65     /*  43 */ "Se
bac10 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ekLt",.     /*  
bac20 34 34 20 2a 2f 20 22 53 65 65 6b 4c 65 22 2c 0a  44 */ "SeekLe",.
bac30 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f 20 22       /*  45 */ "
bac40 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 2f 2a  SeekGe",.     /*
bac50 20 20 34 36 20 2a 2f 20 22 53 65 65 6b 47 74 22    46 */ "SeekGt"
bac60 2c 0a 20 20 20 20 20 2f 2a 20 20 34 37 20 2a 2f  ,.     /*  47 */
bac70 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a   "Seek",.     /*
bac80 20 20 34 38 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e    48 */ "NotFoun
bac90 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 39 20  d",.     /*  49 
baca0 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20  */ "Found",.    
bacb0 20 2f 2a 20 20 35 30 20 2a 2f 20 22 49 73 55 6e   /*  50 */ "IsUn
bacc0 69 71 75 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ique",.     /*  
bacd0 35 31 20 2a 2f 20 22 4e 6f 74 45 78 69 73 74 73  51 */ "NotExists
bace0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 32 20 2a  ",.     /*  52 *
bacf0 2f 20 22 53 65 71 75 65 6e 63 65 22 2c 0a 20 20  / "Sequence",.  
bad00 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 4e 65     /*  53 */ "Ne
bad10 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a  wRowid",.     /*
bad20 20 20 35 34 20 2a 2f 20 22 49 6e 73 65 72 74 22    54 */ "Insert"
bad30 2c 0a 20 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f  ,.     /*  55 */
bad40 20 22 49 6e 73 65 72 74 49 6e 74 22 2c 0a 20 20   "InsertInt",.  
bad50 20 20 20 2f 2a 20 20 35 36 20 2a 2f 20 22 44 65     /*  56 */ "De
bad60 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  lete",.     /*  
bad70 35 37 20 2a 2f 20 22 52 65 73 65 74 43 6f 75 6e  57 */ "ResetCoun
bad80 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 20  t",.     /*  58 
bad90 2a 2f 20 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20  */ "RowKey",.   
bada0 20 20 2f 2a 20 20 35 39 20 2a 2f 20 22 52 6f 77    /*  59 */ "Row
badb0 44 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Data",.     /*  
badc0 36 30 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20  60 */ "Rowid",. 
badd0 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 4e      /*  61 */ "N
bade0 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a  ullRow",.     /*
badf0 20 20 36 32 20 2a 2f 20 22 4c 61 73 74 22 2c 0a    62 */ "Last",.
bae00 20 20 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22       /*  63 */ "
bae10 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Sort",.     /*  
bae20 36 34 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a  64 */ "Rewind",.
bae30 20 20 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22       /*  65 */ "
bae40 50 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Prev",.     /*  
bae50 36 36 20 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 20  66 */ "Next",.  
bae60 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22 49 64     /*  67 */ "Id
bae70 78 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f  xInsert",.     /
bae80 2a 20 20 36 38 20 2a 2f 20 22 4f 72 22 2c 0a 20  *  68 */ "Or",. 
bae90 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22 41      /*  69 */ "A
baea0 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 30  nd",.     /*  70
baeb0 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22 2c   */ "IdxDelete",
baec0 0a 20 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20  .     /*  71 */ 
baed0 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20  "IdxRowid",.    
baee0 20 2f 2a 20 20 37 32 20 2a 2f 20 22 49 64 78 4c   /*  72 */ "IdxL
baef0 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 20  T",.     /*  73 
baf00 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20  */ "IsNull",.   
baf10 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e 6f 74    /*  74 */ "Not
baf20 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Null",.     /*  
baf30 37 35 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20  75 */ "Ne",.    
baf40 20 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71 22 2c   /*  76 */ "Eq",
baf50 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20  .     /*  77 */ 
baf60 22 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  "Gt",.     /*  7
baf70 38 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20  8 */ "Le",.     
baf80 2f 2a 20 20 37 39 20 2a 2f 20 22 4c 74 22 2c 0a  /*  79 */ "Lt",.
baf90 20 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22       /*  80 */ "
bafa0 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 31  Ge",.     /*  81
bafb0 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20   */ "IdxGE",.   
bafc0 20 20 2f 2a 20 20 38 32 20 2a 2f 20 22 42 69 74    /*  82 */ "Bit
bafd0 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  And",.     /*  8
bafe0 33 20 2a 2f 20 22 42 69 74 4f 72 22 2c 0a 20 20  3 */ "BitOr",.  
baff0 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 22 53 68     /*  84 */ "Sh
bb000 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 20 20 2f  iftLeft",.     /
bb010 2a 20 20 38 35 20 2a 2f 20 22 53 68 69 66 74 52  *  85 */ "ShiftR
bb020 69 67 68 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ight",.     /*  
bb030 38 36 20 2a 2f 20 22 41 64 64 22 2c 0a 20 20 20  86 */ "Add",.   
bb040 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 53 75 62    /*  87 */ "Sub
bb050 74 72 61 63 74 22 2c 0a 20 20 20 20 20 2f 2a 20  tract",.     /* 
bb060 20 38 38 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 79   88 */ "Multiply
bb070 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a  ",.     /*  89 *
bb080 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 20 20 20  / "Divide",.    
bb090 20 2f 2a 20 20 39 30 20 2a 2f 20 22 52 65 6d 61   /*  90 */ "Rema
bb0a0 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  inder",.     /* 
bb0b0 20 39 31 20 2a 2f 20 22 43 6f 6e 63 61 74 22 2c   91 */ "Concat",
bb0c0 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20  .     /*  92 */ 
bb0d0 22 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20  "Destroy",.     
bb0e0 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e 6f  /*  93 */ "BitNo
bb0f0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 20  t",.     /*  94 
bb100 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 20  */ "String8",.  
bb110 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 43 6c     /*  95 */ "Cl
bb120 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  ear",.     /*  9
bb130 36 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 64 65  6 */ "CreateInde
bb140 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37 20  x",.     /*  97 
bb150 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 65 22  */ "CreateTable"
bb160 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 2a 2f  ,.     /*  98 */
bb170 20 22 50 61 72 73 65 53 63 68 65 6d 61 22 2c 0a   "ParseSchema",.
bb180 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22       /*  99 */ "
bb190 4c 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20  LoadAnalysis",. 
bb1a0 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 44      /* 100 */ "D
bb1b0 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 20  ropTable",.     
bb1c0 2f 2a 20 31 30 31 20 2a 2f 20 22 44 72 6f 70 49  /* 101 */ "DropI
bb1d0 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 31  ndex",.     /* 1
bb1e0 30 32 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 67  02 */ "DropTrigg
bb1f0 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33  er",.     /* 103
bb200 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43 6b   */ "IntegrityCk
bb210 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 34 20 2a  ",.     /* 104 *
bb220 2f 20 22 52 6f 77 53 65 74 41 64 64 22 2c 0a 20  / "RowSetAdd",. 
bb230 20 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 52      /* 105 */ "R
bb240 6f 77 53 65 74 52 65 61 64 22 2c 0a 20 20 20 20  owSetRead",.    
bb250 20 2f 2a 20 31 30 36 20 2a 2f 20 22 52 6f 77 53   /* 106 */ "RowS
bb260 65 74 54 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a  etTest",.     /*
bb270 20 31 30 37 20 2a 2f 20 22 50 72 6f 67 72 61 6d   107 */ "Program
bb280 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a  ",.     /* 108 *
bb290 2f 20 22 50 61 72 61 6d 22 2c 0a 20 20 20 20 20  / "Param",.     
bb2a0 2f 2a 20 31 30 39 20 2a 2f 20 22 46 6b 43 6f 75  /* 109 */ "FkCou
bb2b0 6e 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31  nter",.     /* 1
bb2c0 31 30 20 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22  10 */ "FkIfZero"
bb2d0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f  ,.     /* 111 */
bb2e0 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20 20   "MemMax",.     
bb2f0 2f 2a 20 31 31 32 20 2a 2f 20 22 49 66 50 6f 73  /* 112 */ "IfPos
bb300 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 33 20 2a  ",.     /* 113 *
bb310 2f 20 22 49 66 4e 65 67 22 2c 0a 20 20 20 20 20  / "IfNeg",.     
bb320 2f 2a 20 31 31 34 20 2a 2f 20 22 49 66 5a 65 72  /* 114 */ "IfZer
bb330 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20  o",.     /* 115 
bb340 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a 20 20  */ "AggStep",.  
bb350 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22 41 67     /* 116 */ "Ag
bb360 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a  gFinal",.     /*
bb370 20 31 31 37 20 2a 2f 20 22 56 61 63 75 75 6d 22   117 */ "Vacuum"
bb380 2c 0a 20 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f  ,.     /* 118 */
bb390 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a 20   "IncrVacuum",. 
bb3a0 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22 45      /* 119 */ "E
bb3b0 78 70 69 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20  xpire",.     /* 
bb3c0 31 32 30 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 63  120 */ "TableLoc
bb3d0 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 31 20  k",.     /* 121 
bb3e0 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20 20 20  */ "VBegin",.   
bb3f0 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 56 43 72    /* 122 */ "VCr
bb400 65 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31  eate",.     /* 1
bb410 32 33 20 2a 2f 20 22 56 44 65 73 74 72 6f 79 22  23 */ "VDestroy"
bb420 2c 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f  ,.     /* 124 */
bb430 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20 2f   "VOpen",.     /
bb440 2a 20 31 32 35 20 2a 2f 20 22 56 46 69 6c 74 65  * 125 */ "VFilte
bb450 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 20  r",.     /* 126 
bb460 2a 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20  */ "VColumn",.  
bb470 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 4e     /* 127 */ "VN
bb480 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32  ext",.     /* 12
bb490 38 20 2a 2f 20 22 56 52 65 6e 61 6d 65 22 2c 0a  8 */ "VRename",.
bb4a0 20 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22       /* 129 */ "
bb4b0 56 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20 2f  VUpdate",.     /
bb4c0 2a 20 31 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c  * 130 */ "Real",
bb4d0 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20  .     /* 131 */ 
bb4e0 22 50 61 67 65 63 6f 75 6e 74 22 2c 0a 20 20 20  "Pagecount",.   
bb4f0 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 54 72 61    /* 132 */ "Tra
bb500 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33  ce",.     /* 133
bb510 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20   */ "Noop",.    
bb520 20 2f 2a 20 31 33 34 20 2a 2f 20 22 45 78 70 6c   /* 134 */ "Expl
bb530 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  ain",.     /* 13
bb540 35 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33  5 */ "NotUsed_13
bb550 35 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 36 20  5",.     /* 136 
bb560 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 36 22  */ "NotUsed_136"
bb570 2c 0a 20 20 20 20 20 2f 2a 20 31 33 37 20 2a 2f  ,.     /* 137 */
bb580 20 22 4e 6f 74 55 73 65 64 5f 31 33 37 22 2c 0a   "NotUsed_137",.
bb590 20 20 20 20 20 2f 2a 20 31 33 38 20 2a 2f 20 22       /* 138 */ "
bb5a0 4e 6f 74 55 73 65 64 5f 31 33 38 22 2c 0a 20 20  NotUsed_138",.  
bb5b0 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20 22 4e 6f     /* 139 */ "No
bb5c0 74 55 73 65 64 5f 31 33 39 22 2c 0a 20 20 20 20  tUsed_139",.    
bb5d0 20 2f 2a 20 31 34 30 20 2a 2f 20 22 4e 6f 74 55   /* 140 */ "NotU
bb5e0 73 65 64 5f 31 34 30 22 2c 0a 20 20 20 20 20 2f  sed_140",.     /
bb5f0 2a 20 31 34 31 20 2a 2f 20 22 54 6f 54 65 78 74  * 141 */ "ToText
bb600 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 32 20 2a  ",.     /* 142 *
bb610 2f 20 22 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 20  / "ToBlob",.    
bb620 20 2f 2a 20 31 34 33 20 2a 2f 20 22 54 6f 4e 75   /* 143 */ "ToNu
bb630 6d 65 72 69 63 22 2c 0a 20 20 20 20 20 2f 2a 20  meric",.     /* 
bb640 31 34 34 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 0a  144 */ "ToInt",.
bb650 20 20 20 20 20 2f 2a 20 31 34 35 20 2a 2f 20 22       /* 145 */ "
bb660 54 6f 52 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 20  ToReal",.  };.  
bb670 72 65 74 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 5d  return azName[i]
bb680 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
bb690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
bb6a0 6f 66 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a  of opcodes.c ***
bb6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
bb6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
bb6f0 6e 20 66 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 20  n file os_os2.c 
bb700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
bb730 2a 20 32 30 30 36 20 46 65 62 20 31 34 0a 2a 2a  * 2006 Feb 14.**
bb740 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
bb750 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
bb760 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
bb770 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
bb780 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
bb790 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
bb7a0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
bb7b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
bb7c0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
bb7d0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
bb7e0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
bb7f0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
bb800 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
bb810 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
bb820 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
bb830 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
bb840 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
bb850 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
bb860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb8a0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
bb8b0 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
bb8c0 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69 63  that is specific
bb8d0 20 74 6f 20 4f 53 2f 32 2e 0a 2a 2f 0a 0a 0a 23   to OS/2..*/...#
bb8e0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  if SQLITE_OS_OS2
bb8f0 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41  ../*.** A Note A
bb900 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f  bout Memory Allo
bb910 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68  cation:.**.** Th
bb920 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20 6d  is driver uses m
bb930 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64  alloc()/free() d
bb940 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74  irectly rather t
bb950 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67  han going throug
bb960 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d  h.** the SQLite-
bb970 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 33  wrappers sqlite3
bb980 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65  _malloc()/sqlite
bb990 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65  3_free().  Those
bb9a0 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 65   wrappers.** are
bb9b0 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73   designed for us
bb9c0 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79  e on embedded sy
bb9d0 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f  stems where memo
bb9e0 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e 64  ry is scarce and
bb9f0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  .** malloc failu
bba00 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 75  res happen frequ
bba10 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f 65  ently.  OS/2 doe
bba20 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20  s not typically 
bba30 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64  run on.** embedd
bba40 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20  ed systems, and 
bba50 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65  when it does the
bba60 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d   developers norm
bba70 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65 72  ally have bigger
bba80 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20  .** problems to 
bba90 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e  worry about than
bbaa0 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20   running out of 
bbab0 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72  memory.  So ther
bbac0 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f  e is not.** a co
bbad0 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f  mpelling need to
bbae0 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72   use the wrapper
bbaf0 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65  s..**.** But the
bbb00 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61  re is a good rea
bbb10 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74  son to not use t
bbb20 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 49 66  he wrappers.  If
bbb30 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77   we use the.** w
bbb40 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 65 20  rappers then we 
bbb50 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74  will get simulat
bbb60 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c  ed malloc() fail
bbb70 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 69 73  ures within this
bbb80 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64  .** driver.  And
bbb90 20 74 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c   that causes all
bbba0 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65   kinds of proble
bbbb0 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73  ms for our tests
bbbc0 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65  .  We.** could e
bbbd0 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f  nhance SQLite to
bbbe0 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c   deal with simul
bbbf0 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  ated malloc fail
bbc00 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74  ures within.** t
bbc10 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75  he OS driver, bu
bbc20 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65  t the code to de
bbc30 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66 61  al with those fa
bbc40 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a  ilure would not.
bbc50 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 64 20  ** be exercised 
bbc60 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20  on Linux (which 
bbc70 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
bbc80 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65   malloc() in the
bbc90 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20   driver).** and 
bbca0 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65  so we would have
bbcb0 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 69 74   difficulty writ
bbcc0 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73  ing coverage tes
bbcd0 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63  ts for that.** c
bbce0 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20  ode.  Better to 
bbcf0 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f  leave the code o
bbd00 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a  ut, we think..**
bbd10 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66  .** The point of
bbd20 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e   this discussion
bbd30 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20   is as follows: 
bbd40 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61   When creating a
bbd50 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72   new.** OS layer
bbd60 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64   for an embedded
bbd70 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20   system, if you 
bbd80 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61 73  use this file as
bbd90 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20   an example,.** 
bbda0 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66  avoid the use of
bbdb0 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29   malloc()/free()
bbdc0 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65  .  Those routine
bbdd0 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53 2f  s work ok on OS/
bbde0 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75  2.** desktops bu
bbdf0 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e  t not so well in
bbe00 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
bbe10 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  s..*/../*.** Mac
bbe20 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65  ros used to dete
bbe30 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72  rmine whether or
bbe40 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65   not to use thre
bbe50 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ads..*/.#if defi
bbe60 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41  ned(SQLITE_THREA
bbe70 44 53 41 46 45 29 20 26 26 20 53 51 4c 49 54 45  DSAFE) && SQLITE
bbe80 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65  _THREADSAFE.# de
bbe90 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32 5f  fine SQLITE_OS2_
bbea0 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66  THREADS 1.#endif
bbeb0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20  ../*.** Include 
bbec0 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d  code that is com
bbed0 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e  mon to all os_*.
bbee0 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a  c files.*/./****
bbef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
bbf00 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69  de os_common.h i
bbf10 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
bbf20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_os2.c *******
bbf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
bbf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
bbf50 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e   file os_common.
bbf60 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
bbf70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbf80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
bbf90 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a   2004 May 22.**.
bbfa0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
bbfb0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
bbfc0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
bbfd0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
bbfe0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
bbff0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
bc000 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
bc010 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
bc020 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
bc030 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bc040 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
bc050 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
bc060 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
bc070 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bc080 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
bc090 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
bc0a0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
bc0b0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
bc0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc100 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
bc110 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73   contains macros
bc120 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69   and a little bi
bc130 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69  t of code that i
bc140 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61  s common to.** a
bc150 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f  ll of the platfo
bc160 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65  rm-specific file
bc170 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69  s (os_*.c) and i
bc180 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f  s #included into
bc190 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e   those.** files.
bc1a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
bc1b0 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c   should be #incl
bc1c0 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a  uded by the os_*
bc1d0 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20  .c files only.  
bc1e0 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67  It is not a.** g
bc1f0 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68  eneral purpose h
bc200 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23  eader file..*/.#
bc210 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f  ifndef _OS_COMMO
bc220 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53  N_H_.#define _OS
bc230 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a  _COMMON_H_../*.*
bc240 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62  * At least two b
bc250 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64  ugs have slipped
bc260 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63   in because we c
bc270 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52  hanged the MEMOR
bc280 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f  Y_DEBUG.** macro
bc290 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47   to SQLITE_DEBUG
bc2a0 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20   and some older 
bc2b0 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e  makefiles have n
bc2c0 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a  ot yet made the.
bc2d0 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20  ** switch.  The 
bc2e0 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73  following code s
bc2f0 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73  hould catch this
bc300 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70   problem at comp
bc310 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66  ile-time..*/.#if
bc320 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  def MEMORY_DEBUG
bc330 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45  .# error "The ME
bc340 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f  MORY_DEBUG macro
bc350 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55   is obsolete.  U
bc360 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  se SQLITE_DEBUG 
bc370 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66  instead.".#endif
bc380 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
bc390 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
bc3a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
bc3b0 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65  OSTrace = 0;.#de
bc3c0 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29  fine OSTRACE1(X)
bc3d0 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
bc3e0 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
bc3f0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
bc400 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  (X).#define OSTR
bc410 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20  ACE2(X,Y)       
bc420 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
bc430 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
bc440 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65  gPrintf(X,Y).#de
bc450 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
bc460 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c  Y,Z)     if( sql
bc470 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
bc480 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
bc490 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
bc4a0 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
bc4b0 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f  )   if( sqlite3O
bc4c0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
bc4d0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
bc4e0 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Z,A).#define OST
bc4f0 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29  RACE5(X,Y,Z,A,B)
bc500 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
bc510 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
bc520 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
bc530 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
bc540 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
bc550 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
bc560 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
bc570 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
bc580 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69  Y,Z,A,B,C).#defi
bc590 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c  ne OSTRACE7(X,Y,
bc5a0 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20  Z,A,B,C,D) \.   
bc5b0 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61   if(sqlite3OSTra
bc5c0 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
bc5d0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
bc5e0 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66  ,C,D).#else.#def
bc5f0 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a  ine OSTRACE1(X).
bc600 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
bc610 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53  (X,Y).#define OS
bc620 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64  TRACE3(X,Y,Z).#d
bc630 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58  efine OSTRACE4(X
bc640 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20  ,Y,Z,A).#define 
bc650 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41  OSTRACE5(X,Y,Z,A
bc660 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
bc670 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
bc680 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
bc690 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
bc6a0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
bc6b0 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f  Macros for perfo
bc6c0 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20  rmance tracing. 
bc6d0 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64   Normally turned
bc6e0 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b   off.  Only work
bc6f0 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72  s.** on i486 har
bc700 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  dware..*/.#ifdef
bc710 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41   SQLITE_PERFORMA
bc720 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a  NCE_TRACE../* .*
bc730 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61  * hwtime.h conta
bc740 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d  ins inline assem
bc750 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d  bler code for im
bc760 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68  plementing .** h
bc770 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20  igh-performance 
bc780 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e  timing routines.
bc790 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
bc7a0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
bc7b0 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
bc7c0 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  le of os_common.
bc7d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
bc7e0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
bc7f0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
bc800 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
bc810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc830 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d  **/./*.** 2008 M
bc840 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 27.**.** The 
bc850 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
bc860 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
bc870 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
bc880 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
bc890 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
bc8a0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
bc8b0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
bc8c0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
bc8d0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
bc8e0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
bc8f0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
bc900 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
bc910 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
bc920 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
bc930 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
bc940 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
bc950 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
bc960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
bc9b0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
bc9c0 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f  ns inline asm co
bc9d0 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e  de for retrievin
bc9e0 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61  g "high-performa
bc9f0 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73  nce".** counters
bca00 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43   for x86 class C
bca10 50 55 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  PUs..*/.#ifndef 
bca20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69  _HWTIME_H_.#defi
bca30 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f  ne _HWTIME_H_../
bca40 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
bca50 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  ng routine only 
bca60 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d  works on pentium
bca70 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72  -class (or newer
bca80 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a  ) processors..**
bca90 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54   It uses the RDT
bcaa0 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61  SC opcode to rea
bcab0 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e  d the cycle coun
bcac0 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74  t value out of t
bcad0 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20  he.** processor 
bcae0 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74  and returns that
bcaf0 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61   value.  This ca
bcb00 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69  n be used for hi
bcb10 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c  gh-res.** profil
bcb20 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66  ing..*/.#if (def
bcb30 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c  ined(__GNUC__) |
bcb40 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56  | defined(_MSC_V
bcb50 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20  ER)) && \.      
bcb60 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c  (defined(i386) |
bcb70 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36  | defined(__i386
bcb80 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
bcb90 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20  M_IX86))..  #if 
bcba0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
bcbb0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  )..  __inline__ 
bcbc0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
bcbd0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
bcbe0 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64  ){.     unsigned
bcbf0 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20   int lo, hi;.   
bcc00 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61    __asm__ __vola
bcc10 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20  tile__ ("rdtsc" 
bcc20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64  : "=a" (lo), "=d
bcc30 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65  " (hi));.     re
bcc40 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e  turn (sqlite_uin
bcc50 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c  t64)hi << 32 | l
bcc60 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20  o;.  }..  #elif 
bcc70 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52  defined(_MSC_VER
bcc80 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28  )..  __declspec(
bcc90 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20  naked) __inline 
bcca0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f  sqlite_uint64 __
bccb0 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74  cdecl sqlite3Hwt
bccc0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
bccd0 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20  __asm {.        
bcce0 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65  rdtsc.        re
bccf0 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e  t       ; return
bcd00 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41   value at EDX:EA
bcd10 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  X.     }.  }..  
bcd20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64  #endif..#elif (d
bcd30 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
bcd40 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38   && defined(__x8
bcd50 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e  6_64__))..  __in
bcd60 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
bcd70 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
bcd80 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20  me(void){.      
bcd90 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61  unsigned long va
bcda0 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f  l;.      __asm__
bcdb0 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
bcdc0 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76  rdtsc" : "=A" (v
bcdd0 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  al));.      retu
bcde0 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65  rn val;.  }. .#e
bcdf0 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  lif (defined(__G
bce00 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65  NUC__) && define
bce10 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f  d(__ppc__))..  _
bce20 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
bce30 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
bce40 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
bce50 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
bce60 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20   long retval;.  
bce70 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
bce80 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f  g junk;.      __
bce90 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
bcea0 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20  __ ("\n\.       
bceb0 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75     1:      mftbu
bcec0 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20     %1\n\.       
bced0 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20             mftb 
bcee0 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20     %L0\n\.      
bcef0 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
bcf00 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20  u   %0\n\.      
bcf10 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77              cmpw
bcf20 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20      %0,%1\n\.   
bcf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
bcf40 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20  ne     1b".     
bcf50 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22               : "
bcf60 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d  =r" (retval), "=
bcf70 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20  r" (junk));.    
bcf80 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b    return retval;
bcf90 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23  .  }..#else..  #
bcfa0 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65  error Need imple
bcfb0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c  mentation of sql
bcfc0 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72  ite3Hwtime() for
bcfd0 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a   your platform..
bcfe0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f  .  /*.  ** To co
bcff0 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d  mpile without im
bd000 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74  plementing sqlit
bd010 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79  e3Hwtime() for y
bd020 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20  our platform,.  
bd030 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76  ** you can remov
bd040 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72  e the above #err
bd050 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66  or and use the f
bd060 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74  ollowing.  ** st
bd070 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f  ub function.  Yo
bd080 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69  u will lose timi
bd090 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d  ng support for m
bd0a0 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  any.  ** of the 
bd0b0 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65  debugging and te
bd0c0 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c  sting utilities,
bd0d0 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61   but it should a
bd0e0 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d  t.  ** least com
bd0f0 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20  pile and run..  
bd100 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
bd110 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  E   sqlite_uint6
bd120 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
bd130 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28  void){ return ((
bd140 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29  sqlite_uint64)0)
bd150 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  ; }..#endif..#en
bd160 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
bd170 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a  _HWTIME_H_) */..
bd180 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
bd190 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20  End of hwtime.h 
bd1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bd1d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
bd1e0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
bd1f0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
bd200 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
bd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bd220 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75  .static sqlite_u
bd230 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73  int64 g_start;.s
bd240 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e  tatic sqlite_uin
bd250 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23  t64 g_elapsed;.#
bd260 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41  define TIMER_STA
bd270 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74  RT       g_start
bd280 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29  =sqlite3Hwtime()
bd290 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
bd2a0 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61  ND         g_ela
bd2b0 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69  psed=sqlite3Hwti
bd2c0 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65  me()-g_start.#de
bd2d0 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53  fine TIMER_ELAPS
bd2e0 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64  ED     g_elapsed
bd2f0 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54  .#else.#define T
bd300 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69  IMER_START.#defi
bd310 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65  ne TIMER_END.#de
bd320 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53  fine TIMER_ELAPS
bd330 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f  ED     ((sqlite_
bd340 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66  uint64)0).#endif
bd350 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f  ../*.** If we co
bd360 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53  mpile with the S
bd370 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f  QLITE_TEST macro
bd380 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66   set, then the f
bd390 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a  ollowing block.*
bd3a0 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67  * of code will g
bd3b0 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69  ive us the abili
bd3c0 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61  ty to simulate a
bd3d0 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e   disk I/O error.
bd3e0 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65    This.** is use
bd3f0 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68  d for testing th
bd400 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c  e I/O recovery l
bd410 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ogic..*/.#ifdef 
bd420 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
bd430 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
bd440 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20  e3_io_error_hit 
bd450 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
bd460 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
bd470 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f  of I/O Errors */
bd480 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
bd490 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
bd4a0 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20  _hardhit = 0;   
bd4b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
bd4c0 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72  f non-benign err
bd4d0 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ors */.SQLITE_AP
bd4e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
bd4f0 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d  _error_pending =
bd500 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f   0;        /* Co
bd510 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73  unt down to firs
bd520 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53  t I/O error */.S
bd530 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
bd540 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
bd550 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20  ersist = 0;     
bd560 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f     /* True if I/
bd570 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74  O errors persist
bd580 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
bd590 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
bd5a0 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20  ror_benign = 0; 
bd5b0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
bd5c0 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65  if errors are be
bd5d0 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  nign */.SQLITE_A
bd5e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
bd5f0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20  iskfull_pending 
bd600 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  = 0;.SQLITE_API 
bd610 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  int sqlite3_disk
bd620 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e  full = 0;.#defin
bd630 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
bd640 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74  rBenign(X) sqlit
bd650 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
bd660 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53  gn=(X).#define S
bd670 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43  imulateIOError(C
bd680 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73  ODE)  \.  if( (s
bd690 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bd6a0 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74  persist && sqlit
bd6b0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29  e3_io_error_hit)
bd6c0 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c   \.       || sql
bd6d0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
bd6e0 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20  nding-- == 1 )  
bd6f0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
bd700 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b  { local_ioerr();
bd710 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20   CODE; }.static 
bd720 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72  void local_ioerr
bd730 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22  (){.  IOTRACE(("
bd740 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71  IOERR\n"));.  sq
bd750 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
bd760 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c  it++;.  if( !sql
bd770 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65  ite3_io_error_be
bd780 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69  nign ) sqlite3_i
bd790 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b  o_error_hardhit+
bd7a0 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d  +;.}.#define Sim
bd7b0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
bd7c0 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66  or(CODE) \.   if
bd7d0 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  ( sqlite3_diskfu
bd7e0 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a  ll_pending ){ \.
bd7f0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
bd800 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
bd810 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20  g == 1 ){ \.    
bd820 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29     local_ioerr()
bd830 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  ; \.       sqlit
bd840 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b  e3_diskfull = 1;
bd850 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65   \.       sqlite
bd860 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d  3_io_error_hit =
bd870 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44   1; \.       COD
bd880 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b  E; \.     }else{
bd890 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65   \.       sqlite
bd8a0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
bd8b0 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c  ng--; \.     } \
bd8c0 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66  .   }.#else.#def
bd8d0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
bd8e0 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65  rorBenign(X).#de
bd8f0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
bd900 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20  rror(A).#define 
bd910 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
bd920 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a  Error(A).#endif.
bd930 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74  ./*.** When test
bd940 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e  ing, keep a coun
bd950 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  t of the number 
bd960 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a  of open files..*
bd970 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
bd980 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
bd990 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
bd9a0 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b  _file_count = 0;
bd9b0 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
bd9c0 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33  nter(X)  sqlite3
bd9d0 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74  _open_file_count
bd9e0 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66  +=(X).#else.#def
bd9f0 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  ine OpenCounter(
bda00 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  X).#endif..#endi
bda10 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f  f /* !defined(_O
bda20 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a  S_COMMON_H_) */.
bda30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bda40 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f   End of os_commo
bda50 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
bda60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bda70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bda80 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bda90 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
bdaa0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
bdab0 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a   os_os2.c ******
bdac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bdad0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46  ../*.** The os2F
bdae0 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73  ile structure is
bdaf0 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c   subclass of sql
bdb00 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66  ite3_file specif
bdb10 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a  ic for the OS/2.
bdb20 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c  ** protability l
bdb30 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ayer..*/.typedef
bdb40 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20   struct os2File 
bdb50 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 20  os2File;.struct 
bdb60 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73  os2File {.  cons
bdb70 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
bdb80 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20  hods *pMethod;  
bdb90 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69  /* Always the fi
bdba0 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48  rst entry */.  H
bdbb0 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20  FILE h;         
bdbc0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
bdbd0 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67  le for accessing
bdbe0 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63   the file */.  c
bdbf0 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20  har* pathToDel; 
bdc00 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
bdc10 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65   of file to dele
bdc20 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c  te on close, NUL
bdc30 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e  L if not */.  un
bdc40 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b  signed char lock
bdc50 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 20  type;   /* Type 
bdc60 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c  of lock currentl
bdc70 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66  y held on this f
bdc80 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69  ile */.};..#defi
bdc90 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20  ne LOCK_TIMEOUT 
bdca0 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 75  10L /* the defau
bdcb0 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f  lt locking timeo
bdcc0 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ut */../********
bdcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd10 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78  *****.** The nex
bdd20 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69  t group of routi
bdd30 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  nes implement th
bdd40 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70  e I/O methods sp
bdd50 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68  ecified.** by th
bdd60 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  e sqlite3_io_met
bdd70 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a  hods object..***
bdd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bddb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bddc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
bddd0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
bdde0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
bddf0 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 65  os2Close( sqlite
bde00 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20  3_file *id ){.  
bde10 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
bde20 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20  RROR;.  os2File 
bde30 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 64  *pFile;.  if( id
bde40 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f 73   && (pFile = (os
bde50 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 20  2File*)id) != 0 
bde60 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28  ){.    OSTRACE2(
bde70 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70   "CLOSE %d\n", p
bde80 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72  File->h );.    r
bde90 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46  c = DosClose( pF
bdea0 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46  ile->h );.    pF
bdeb0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
bdec0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28  NO_LOCK;.    if(
bded0 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
bdee0 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20  l != NULL ){.   
bdef0 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65     rc = DosForce
bdf00 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69  Delete( (PSZ)pFi
bdf10 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b  le->pathToDel );
bdf20 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69  .      free( pFi
bdf30 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b  le->pathToDel );
bdf40 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61  .      pFile->pa
bdf50 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a  thToDel = NULL;.
bdf60 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 30      }.    id = 0
bdf70 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65  ;.    OpenCounte
bdf80 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20  r( -1 );.  }..  
bdf90 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f  return rc == NO_
bdfa0 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f  ERROR ? SQLITE_O
bdfb0 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  K : SQLITE_IOERR
bdfc0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
bdfd0 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65  data from a file
bdfe0 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20   into a buffer. 
bdff0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
be000 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65  K if all.** byte
be010 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63  s were read succ
be020 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c  essfully and SQL
be030 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79  ITE_IOERR if any
be040 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72  thing goes.** wr
be050 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ong..*/.static i
be060 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71  nt os2Read(.  sq
be070 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
be080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
be090 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72   File to read fr
be0a0 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42  om */.  void *pB
be0b0 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  uf,             
be0c0 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
be0d0 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68   content into th
be0e0 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69  is buffer */.  i
be0f0 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
be100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
be110 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
be120 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73  s to read */.  s
be130 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66  qlite3_int64 off
be140 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f  set            /
be150 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20  * Begin reading 
be160 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a  at this offset *
be170 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c  /.){.  ULONG fil
be180 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a  eLocation = 0L;.
be190 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f    ULONG got;.  o
be1a0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
be1b0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
be1c0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
be1d0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
be1e0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
be1f0 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a  E_IOERR_READ );.
be200 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41    OSTRACE3( "REA
be210 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c  D %d lock=%d\n",
be220 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
be230 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
be240 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74  if( DosSetFilePt
be250 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73  r(pFile->h, offs
be260 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20  et, FILE_BEGIN, 
be270 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21  &fileLocation) !
be280 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
be290 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
be2a0 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28  IOERR;.  }.  if(
be2b0 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d   DosRead( pFile-
be2c0 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26  >h, pBuf, amt, &
be2d0 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f  got ) != NO_ERRO
be2e0 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  R ){.    return 
be2f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
be300 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74  D;.  }.  if( got
be310 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29   == (ULONG)amt )
be320 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
be330 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a  TE_OK;.  else {.
be340 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f      /* Unread po
be350 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e  rtions of the in
be360 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 20  put buffer must 
be370 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a  be zero-filled *
be380 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28  /.    memset(&((
be390 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d  char*)pBuf)[got]
be3a0 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20  , 0, amt-got);. 
be3b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
be3c0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
be3d0 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  D;.  }.}../*.** 
be3e0 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20  Write data from 
be3f0 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20  a buffer into a 
be400 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51  file.  Return SQ
be410 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
be420 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74  ss.** or some ot
be430 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f  her error code o
be440 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74  n failure..*/.st
be450 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74  atic int os2Writ
be460 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  e(.  sqlite3_fil
be470 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20  e *id,          
be480 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20       /* File to 
be490 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20  write into */.  
be4a0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66  const void *pBuf
be4b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
be4c0 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20  /* The bytes to 
be4d0 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  be written */.  
be4e0 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
be4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be500 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
be510 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20  es to write */. 
be520 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f   sqlite3_int64 o
be530 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20  ffset           
be540 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
be550 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69  the file to begi
be560 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a  n writing at */.
be570 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c  ){.  ULONG fileL
be580 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20  ocation = 0L;.  
be590 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
be5a0 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72  RROR;.  ULONG wr
be5b0 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  ote;.  os2File *
be5c0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
be5d0 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20  *)id;.  assert( 
be5e0 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c  id!=0 );.  Simul
be5f0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
be600 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
be610 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c  WRITE );.  Simul
be620 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
be630 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
be640 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43  FULL );.  OSTRAC
be650 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f  E3( "WRITE %d lo
be660 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
be670 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
be680 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73  ype );.  if( Dos
be690 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65  SetFilePtr(pFile
be6a0 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c  ->h, offset, FIL
be6b0 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f  E_BEGIN, &fileLo
be6c0 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52  cation) != NO_ER
be6d0 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ROR ){.    retur
be6e0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a  n SQLITE_IOERR;.
be6f0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d    }.  assert( am
be700 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  t>0 );.  while( 
be710 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20  amt > 0 &&.     
be720 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 72      ( rc = DosWr
be730 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28  ite( pFile->h, (
be740 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c  PVOID)pBuf, amt,
be750 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e   &wrote ) ) == N
be760 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 20  O_ERROR &&.     
be770 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 20      wrote > 0.  
be780 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72  ){.    amt -= wr
be790 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20  ote;.    pBuf = 
be7a0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77  &((char*)pBuf)[w
be7b0 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65  rote];.  }..  re
be7c0 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f  turn ( rc != NO_
be7d0 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28  ERROR || amt > (
be7e0 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 51  int)wrote ) ? SQ
be7f0 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49  LITE_FULL : SQLI
be800 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
be810 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e  Truncate an open
be820 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69   file to a speci
be830 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61  fied size.*/.sta
be840 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63  tic int os2Trunc
be850 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c  ate( sqlite3_fil
be860 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65  e *id, i64 nByte
be870 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20   ){.  APIRET rc 
be880 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73  = NO_ERROR;.  os
be890 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
be8a0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f  os2File*)id;.  O
be8b0 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 41  STRACE3( "TRUNCA
be8c0 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70  TE %d %lld\n", p
be8d0 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29  File->h, nByte )
be8e0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
be8f0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
be900 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
be910 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53  E );.  rc = DosS
be920 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c  etFileSize( pFil
be930 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20  e->h, nByte );. 
be940 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
be950 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
be960 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
be970 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23  R_TRUNCATE;.}..#
be980 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
be990 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68  T./*.** Count th
be9a0 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c  e number of full
be9b0 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c  syncs and normal
be9c0 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73   syncs.  This is
be9d0 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a   used to test.**
be9e0 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20   that syncs and 
be9f0 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63  fullsyncs are oc
bea00 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69  curing at the ri
bea10 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51  ght times..*/.SQ
bea20 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
bea30 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20  ite3_sync_count 
bea40 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  = 0;.SQLITE_API 
bea50 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c  int sqlite3_full
bea60 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  sync_count = 0;.
bea70 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
bea80 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74  ke sure all writ
bea90 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c  es to a particul
beaa0 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d  ar file are comm
beab0 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a  itted to disk..*
beac0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
bead0 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69  Sync( sqlite3_fi
beae0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67  le *id, int flag
beaf0 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  s ){.  os2File *
beb00 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
beb10 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33  *)id;.  OSTRACE3
beb20 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d  ( "SYNC %d lock=
beb30 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
beb40 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
beb50 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   );.#ifdef SQLIT
beb60 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61  E_TEST.  if( fla
beb70 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43  gs & SQLITE_SYNC
beb80 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69  _FULL){.    sqli
beb90 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
beba0 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  nt++;.  }.  sqli
bebb0 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  te3_sync_count++
bebc0 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66  ;.#endif.  /* If
bebd0 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   we compiled wit
bebe0 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f  h the SQLITE_NO_
bebf0 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20  SYNC flag, then 
bec00 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a  syncing is a.  *
bec10 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66  * no-op.  */.#if
bec20 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  def SQLITE_NO_SY
bec30 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  NC.  UNUSED_PARA
bec40 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 20  METER(pFile);.  
bec50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
bec60 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e  ;.#else.  return
bec70 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28   DosResetBuffer(
bec80 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e   pFile->h ) == N
bec90 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45  O_ERROR ? SQLITE
beca0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
becb0 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  RR;.#endif.}../*
becc0 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68  .** Determine th
becd0 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
bece0 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65  f a file in byte
becf0 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  s.*/.static int 
bed00 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 6c  os2FileSize( sql
bed10 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73  ite3_file *id, s
bed20 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53  qlite3_int64 *pS
bed30 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20  ize ){.  APIRET 
bed40 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20  rc = NO_ERROR;. 
bed50 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74   FILESTATUS3 fst
bed60 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65  s3FileInfo;.  me
bed70 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 49  mset(&fsts3FileI
bed80 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66  nfo, 0, sizeof(f
bed90 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a  sts3FileInfo));.
beda0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
bedb0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
bedc0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
bedd0 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20  ITE_IOERR_FSTAT 
bede0 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65  );.  rc = DosQue
bedf0 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73  ryFileInfo( ((os
bee00 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46  2File*)id)->h, F
bee10 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73  IL_STANDARD, &fs
bee20 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a  ts3FileInfo, siz
bee30 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29  eof(FILESTATUS3)
bee40 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20   );.  if( rc == 
bee50 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
bee60 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 69  *pSize = fsts3Fi
bee70 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20  leInfo.cbFile;. 
bee80 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
bee90 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
beea0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
beeb0 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d  IOERR_FSTAT;.  }
beec0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72  .}../*.** Acquir
beed0 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e  e a reader lock.
beee0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
beef0 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46  etReadLock( os2F
bef00 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20  ile *pFile ){.  
bef10 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72  FILELOCK  LockAr
bef20 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ea,.            
bef30 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50  UnlockArea;.  AP
bef40 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73  IRET res;.  mems
bef50 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c  et(&LockArea, 0,
bef60 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61   sizeof(LockArea
bef70 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e  ));.  memset(&Un
bef80 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  lockArea, 0, siz
bef90 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29  eof(UnlockArea))
befa0 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66  ;.  LockArea.lOf
befb0 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49  fset = SHARED_FI
befc0 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e  RST;.  LockArea.
befd0 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f  lRange = SHARED_
befe0 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72  SIZE;.  UnlockAr
beff0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
bf000 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52  .  UnlockArea.lR
bf010 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73  ange = 0L;.  res
bf020 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
bf030 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55  ks( pFile->h, &U
bf040 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b  nlockArea, &Lock
bf050 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f  Area, LOCK_TIMEO
bf060 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52  UT, 1L );.  OSTR
bf070 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c 4f  ACE3( "GETREADLO
bf080 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c  CK %d res=%d\n",
bf090 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29   pFile->h, res )
bf0a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  ;.  return res;.
bf0b0 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20  }../*.** Undo a 
bf0c0 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74  readlock.*/.stat
bf0d0 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61  ic int unlockRea
bf0e0 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a  dLock( os2File *
bf0f0 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b  id ){.  FILELOCK
bf100 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20    LockArea,.    
bf110 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72          UnlockAr
bf120 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73  ea;.  APIRET res
bf130 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b  ;.  memset(&Lock
bf140 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
bf150 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65  LockArea));.  me
bf160 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
bf170 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
bf180 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b  ckArea));.  Lock
bf190 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30  Area.lOffset = 0
bf1a0 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  L;.  LockArea.lR
bf1b0 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c  ange = 0L;.  Unl
bf1c0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
bf1d0 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a  = SHARED_FIRST;.
bf1e0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
bf1f0 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a  nge = SHARED_SIZ
bf200 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65  E;.  res = DosSe
bf210 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e  tFileLocks( id->
bf220 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
bf230 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
bf240 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20  TIMEOUT, 1L );. 
bf250 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f   OSTRACE3( "UNLO
bf260 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65  CK-READLOCK file
bf270 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25   handle=%d res=%
bf280 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65  d?\n", id->h, re
bf290 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65  s );.  return re
bf2a0 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  s;.}../*.** Lock
bf2b0 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
bf2c0 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
bf2d0 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c  d by parameter l
bf2e0 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a  ocktype - one.**
bf2f0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
bf300 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  g:.**.**     (1)
bf310 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20   SHARED_LOCK.** 
bf320 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
bf330 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29  _LOCK.**     (3)
bf340 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a   PENDING_LOCK.**
bf350 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
bf360 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f  VE_LOCK.**.** So
bf370 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
bf380 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
bf390 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
bf3a0 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a  al lock states.*
bf3b0 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
bf3c0 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
bf3d0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
bf3e0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
bf3f0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69   later.** transi
bf400 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68  tions leaving th
bf410 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66  e lock state dif
bf420 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74  ferent from what
bf430 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a   it started but.
bf440 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
bf450 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
bf460 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
bf470 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
bf480 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  ed.** transition
bf490 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74  s and the insert
bf4a0 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ed intermediate 
bf4b0 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
bf4c0 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41   UNLOCKED -> SHA
bf4d0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  RED.**    SHARED
bf4e0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20   -> RESERVED.** 
bf4f0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
bf500 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
bf510 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  IVE.**    RESERV
bf520 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
bf530 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
bf540 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58     PENDING -> EX
bf550 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68  CLUSIVE.**.** Th
bf560 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
bf570 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
bf580 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e  lock.  The os2Un
bf590 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a  lock() routine.*
bf5a0 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63  * erases all loc
bf5b0 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72  ks at once and r
bf5c0 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69  eturns us immedi
bf5d0 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67  ately to locking
bf5e0 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20   level 0..** It 
bf5f0 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
bf600 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63  to lower the loc
bf610 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73  king level one s
bf620 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20  tep at a time.  
bf630 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73  You.** must go s
bf640 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69  traight to locki
bf650 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73  ng level 0..*/.s
bf660 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63  tatic int os2Loc
bf670 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  k( sqlite3_file 
bf680 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
bf690 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  e ){.  int rc = 
bf6a0 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
bf6b0 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
bf6c0 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73  from subroutines
bf6d0 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73   */.  APIRET res
bf6e0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20   = NO_ERROR;    
bf6f0 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20  /* Result of an 
bf700 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a  OS/2 lock call *
bf710 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74  /.  int newLockt
bf720 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65  ype;       /* Se
bf730 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  t pFile->locktyp
bf740 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20  e to this value 
bf750 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a  before exiting *
bf760 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69  /.  int gotPendi
bf770 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72  ngLock = 0;/* Tr
bf780 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65  ue if we acquire
bf790 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  d a PENDING lock
bf7a0 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20   this time */.  
bf7b0 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72  FILELOCK  LockAr
bf7c0 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ea,.            
bf7d0 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73  UnlockArea;.  os
bf7e0 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
bf7f0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d  os2File*)id;.  m
bf800 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c  emset(&LockArea,
bf810 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41   0, sizeof(LockA
bf820 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  rea));.  memset(
bf830 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  &UnlockArea, 0, 
bf840 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65  sizeof(UnlockAre
bf850 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  a));.  assert( p
bf860 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54  File!=0 );.  OST
bf870 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20  RACE4( "LOCK %d 
bf880 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46  %d was %d\n", pF
bf890 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
bf8a0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
bf8b0 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  e );..  /* If th
bf8c0 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
bf8d0 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79   lock of this ty
bf8e0 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72  pe or more restr
bf8f0 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20  ictive on the.  
bf900 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e  ** os2File, do n
bf910 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73  othing. Don't us
bf920 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20  e the end_lock: 
bf930 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20  exit path, as.  
bf940 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ** sqlite3_mutex
bf950 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20  _enter() hasn't 
bf960 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e  been called yet.
bf970 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
bf980 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63  e->locktype>=loc
bf990 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54  ktype ){.    OST
bf9a0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20  RACE3( "LOCK %d 
bf9b0 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68  %d ok (already h
bf9c0 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  eld)\n", pFile->
bf9d0 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  h, locktype );. 
bf9e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
bf9f0 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  _OK;.  }..  /* M
bfa00 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
bfa10 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
bfa20 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20   correct.  */.  
bfa30 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
bfa40 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
bfa50 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   || locktype==SH
bfa60 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
bfa70 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
bfa80 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b  =PENDING_LOCK );
bfa90 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
bfaa0 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype!=RESERVED_LO
bfab0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK || pFile->loc
bfac0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
bfad0 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b  CK );..  /* Lock
bfae0 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   the PENDING_LOC
bfaf0 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65  K byte if we nee
bfb00 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50  d to acquire a P
bfb10 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20  ENDING lock or. 
bfb20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63   ** a SHARED loc
bfb30 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63  k.  If we are ac
bfb40 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44  quiring a SHARED
bfb50 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69   lock, the acqui
bfb60 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74  sition of.  ** t
bfb70 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  he PENDING_LOCK 
bfb80 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72  byte is temporar
bfb90 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63  y..  */.  newLoc
bfba0 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c  ktype = pFile->l
bfbb0 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70  ocktype;.  if( p
bfbc0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
bfbd0 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c  NO_LOCK.      ||
bfbe0 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c   (locktype==EXCL
bfbf0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46  USIVE_LOCK && pF
bfc00 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52  ile->locktype==R
bfc10 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20  ESERVED_LOCK).  
bfc20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  ){.    LockArea.
bfc30 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e  lOffset = PENDIN
bfc40 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b  G_BYTE;.    Lock
bfc50 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c  Area.lRange = 1L
bfc60 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
bfc70 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20  .lOffset = 0L;. 
bfc80 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52     UnlockArea.lR
bfc90 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20  ange = 0L;..    
bfca0 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74  /* wait longer t
bfcb0 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  han LOCK_TIMEOUT
bfcc0 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76   here not to hav
bfcd0 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c  e to try multipl
bfce0 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72  e times */.    r
bfcf0 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  es = DosSetFileL
bfd00 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
bfd10 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
bfd20 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c  ckArea, 100L, 0L
bfd30 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20   );.    if( res 
bfd40 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
bfd50 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c       gotPendingL
bfd60 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f  ock = 1;.      O
bfd70 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25  STRACE3( "LOCK %
bfd80 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62  d pending lock b
bfd90 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73  oolean set.  res
bfda0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
bfdb0 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20  , res );.    }. 
bfdc0 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65   }..  /* Acquire
bfdd0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20   a shared lock. 
bfde0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79   */.  if( lockty
bfdf0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
bfe00 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  && res == NO_ERR
bfe10 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OR ){.    assert
bfe20 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
bfe30 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20  e==NO_LOCK );.  
bfe40 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c    res = getReadL
bfe50 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
bfe60 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52  if( res == NO_ER
bfe70 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77  ROR ){.      new
bfe80 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  Locktype = SHARE
bfe90 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  D_LOCK;.    }.  
bfea0 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43    OSTRACE3( "LOC
bfeb0 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68 61  K %d acquire sha
bfec0 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64  red lock. res=%d
bfed0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
bfee0 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  es );.  }..  /* 
bfef0 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56  Acquire a RESERV
bff00 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  ED lock.  */.  i
bff10 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  f( locktype==RES
bff20 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65  ERVED_LOCK && re
bff30 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
bff40 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69  .    assert( pFi
bff50 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  le->locktype==SH
bff60 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20  ARED_LOCK );.   
bff70 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
bff80 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
bff90 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  E;.    LockArea.
bffa0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20  lRange = 1L;.   
bffb0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
bffc0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  set = 0L;.    Un
bffd0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
bffe0 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 0L;.    res = 
bfff0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c0000 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c0010 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c0020 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c0030 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72   0L );.    if( r
c0040 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
c0050 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74  {.      newLockt
c0060 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c  ype = RESERVED_L
c0070 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f  OCK;.    }.    O
c0080 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25  STRACE3( "LOCK %
c0090 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72 76  d acquire reserv
c00a0 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c  ed lock. res=%d\
c00b0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
c00c0 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  s );.  }..  /* A
c00d0 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47  cquire a PENDING
c00e0 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
c00f0 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
c0100 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
c0110 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
c0120 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
c0130 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a  = PENDING_LOCK;.
c0140 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f      gotPendingLo
c0150 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52  ck = 0;.    OSTR
c0160 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61  ACE2( "LOCK %d a
c0170 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c  cquire pending l
c0180 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63  ock. pending loc
c0190 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e  k boolean unset.
c01a0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b  \n", pFile->h );
c01b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
c01c0 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  re an EXCLUSIVE 
c01d0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
c01e0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
c01f0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  IVE_LOCK && res 
c0200 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
c0210 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
c0220 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52  ->locktype>=SHAR
c0230 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72  ED_LOCK );.    r
c0240 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c  es = unlockReadL
c0250 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
c0260 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 61  OSTRACE2( "unrea
c0270 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72  dlock = %d\n", r
c0280 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  es );.    LockAr
c0290 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41  ea.lOffset = SHA
c02a0 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c  RED_FIRST;.    L
c02b0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c02c0 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20   SHARED_SIZE;.  
c02d0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
c02e0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
c02f0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c0300 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 0L;.    res =
c0310 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
c0320 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
c0330 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
c0340 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
c0350 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20  , 0L );.    if( 
c0360 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
c0370 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
c0380 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45  type = EXCLUSIVE
c0390 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
c03a0 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  {.      OSTRACE2
c03b0 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f  ( "OS/2 error-co
c03c0 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20  de = %d\n", res 
c03d0 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64  );.      getRead
c03e0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
c03f0 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28   }.    OSTRACE3(
c0400 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72   "LOCK %d acquir
c0410 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
c0420 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  .  res=%d\n", pF
c0430 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20  ile->h, res );. 
c0440 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61   }..  /* If we a
c0450 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e  re holding a PEN
c0460 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f  DING lock that o
c0470 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61  ught to be relea
c0480 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72  sed, then.  ** r
c0490 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20  elease it now.. 
c04a0 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e   */.  if( gotPen
c04b0 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b  dingLock && lock
c04c0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
c04d0 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a  K ){.    int r;.
c04e0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66      LockArea.lOf
c04f0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c  fset = 0L;.    L
c0500 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c0510 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41   0L;.    UnlockA
c0520 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45  rea.lOffset = PE
c0530 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20  NDING_BYTE;.    
c0540 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
c0550 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20  e = 1L;.    r = 
c0560 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c0570 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c0580 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c0590 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c05a0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
c05b0 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e  CE3( "LOCK %d un
c05c0 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f  locking pending/
c05d0 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c  is shared. r=%d\
c05e0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20  n", pFile->h, r 
c05f0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64  );.  }..  /* Upd
c0600 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  ate the state of
c0610 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65   the lock has he
c0620 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64  ld in the file d
c0630 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20  escriptor then. 
c0640 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61   ** return the a
c0650 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c  ppropriate resul
c0660 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69  t code..  */.  i
c0670 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  f( res == NO_ERR
c0680 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  OR ){.    rc = S
c0690 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
c06a0 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28  e{.    OSTRACE4(
c06b0 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64   "LOCK FAILED %d
c06c0 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62   trying for %d b
c06d0 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46  ut got %d\n", pF
c06e0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
c06f0 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20        locktype, 
c0700 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  newLocktype );. 
c0710 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
c0720 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65  USY;.  }.  pFile
c0730 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77  ->locktype = new
c0740 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52  Locktype;.  OSTR
c0750 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e  ACE3( "LOCK %d n
c0760 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ow %d\n", pFile-
c0770 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
c0780 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ype );.  return 
c0790 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
c07a0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
c07b0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
c07c0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
c07d0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
c07e0 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  ed.** file by th
c07f0 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
c0800 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
c0810 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
c0820 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a   return.** non-z
c0830 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a  ero, otherwise z
c0840 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ero..*/.static i
c0850 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72  nt os2CheckReser
c0860 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33  vedLock( sqlite3
c0870 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a  _file *id, int *
c0880 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 20  pOut ){.  int r 
c0890 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  = 0;.  os2File *
c08a0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
c08b0 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20  *)id;.  assert( 
c08c0 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66  pFile!=0 );.  if
c08d0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
c08e0 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
c08f0 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20   ){.    r = 1;. 
c0900 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45     OSTRACE3( "TE
c0910 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
c0920 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69   (local)\n", pFi
c0930 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65  le->h, r );.  }e
c0940 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43  lse{.    FILELOC
c0950 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20  K  LockArea,.   
c0960 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63             Unloc
c0970 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 45  kArea;.    APIRE
c0980 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b  T rc = NO_ERROR;
c0990 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  .    memset(&Loc
c09a0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
c09b0 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20  (LockArea));.   
c09c0 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41   memset(&UnlockA
c09d0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55  rea, 0, sizeof(U
c09e0 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20  nlockArea));.   
c09f0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
c0a00 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
c0a10 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  E;.    LockArea.
c0a20 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20  lRange = 1L;.   
c0a30 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
c0a40 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  set = 0L;.    Un
c0a50 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c0a60 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44  = 0L;.    rc = D
c0a70 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
c0a80 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
c0a90 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
c0aa0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
c0ab0 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  0L );.    OSTRAC
c0ac0 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43  E3( "TEST WR-LOC
c0ad0 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76  K %d lock reserv
c0ae0 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22  ed byte rc=%d\n"
c0af0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29  , pFile->h, rc )
c0b00 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20  ;.    if( rc == 
c0b10 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
c0b20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 4e    APIRET rcu = N
c0b30 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75  O_ERROR; /* retu
c0b40 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f  rn code for unlo
c0b50 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c  cking */.      L
c0b60 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c0b70 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b  = 0L;.      Lock
c0b80 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
c0b90 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72  ;.      UnlockAr
c0ba0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53  ea.lOffset = RES
c0bb0 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20  ERVED_BYTE;.    
c0bc0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
c0bd0 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20  nge = 1L;.      
c0be0 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  rcu = DosSetFile
c0bf0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
c0c00 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
c0c10 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49  ockArea, LOCK_TI
c0c20 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20  MEOUT, 0L );.   
c0c30 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45     OSTRACE3( "TE
c0c40 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e  ST WR-LOCK %d un
c0c50 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79  lock reserved by
c0c60 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c  te r=%d\n", pFil
c0c70 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 20  e->h, rcu );.   
c0c80 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 20   }.    r = !(rc 
c0c90 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20  == NO_ERROR);.  
c0ca0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53    OSTRACE3( "TES
c0cb0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
c0cc0 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69  (remote)\n", pFi
c0cd0 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a  le->h, r );.  }.
c0ce0 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72    *pOut = r;.  r
c0cf0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c0d00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  .}../*.** Lower 
c0d10 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
c0d20 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
c0d30 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74  ptor id to lockt
c0d40 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a  ype.  locktype.*
c0d50 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  * must be either
c0d60 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52   NO_LOCK or SHAR
c0d70 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49  ED_LOCK..**.** I
c0d80 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  f the locking le
c0d90 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20  vel of the file 
c0da0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c  descriptor is al
c0db0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f  ready at or belo
c0dc0 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  w.** the request
c0dd0 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
c0de0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
c0df0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
c0e00 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   It is not possi
c0e10 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75  ble for this rou
c0e20 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20  tine to fail if 
c0e30 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
c0e40 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43  ent.** is NO_LOC
c0e50 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e  K.  If the secon
c0e60 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48  d argument is SH
c0e70 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74  ARED_LOCK then t
c0e80 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d  his routine.** m
c0e90 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49  ight return SQLI
c0ea0 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61  TE_IOERR;.*/.sta
c0eb0 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63  tic int os2Unloc
c0ec0 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  k( sqlite3_file 
c0ed0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
c0ee0 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b  e ){.  int type;
c0ef0 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
c0f00 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
c0f10 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
c0f20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49  SQLITE_OK;.  API
c0f30 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52  RET res = NO_ERR
c0f40 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20  OR;.  FILELOCK  
c0f50 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20  LockArea,.      
c0f60 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61        UnlockArea
c0f70 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b  ;.  memset(&Lock
c0f80 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
c0f90 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65  LockArea));.  me
c0fa0 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
c0fb0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
c0fc0 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65  ckArea));.  asse
c0fd0 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a  rt( pFile!=0 );.
c0fe0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
c0ff0 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
c1000 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22  );.  OSTRACE4( "
c1010 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20  UNLOCK %d to %d 
c1020 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  was %d\n", pFile
c1030 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70  ->h, locktype, p
c1040 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
c1050 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65  ;.  type = pFile
c1060 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66  ->locktype;.  if
c1070 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56  ( type>=EXCLUSIV
c1080 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f  E_LOCK ){.    Lo
c1090 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c10a0 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65   0L;.    LockAre
c10b0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
c10c0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
c10d0 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
c10e0 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  IRST;.    Unlock
c10f0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48  Area.lRange = SH
c1100 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72  ARED_SIZE;.    r
c1110 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  es = DosSetFileL
c1120 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
c1130 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
c1140 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
c1150 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20  EOUT, 0L );.    
c1160 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43  OSTRACE3( "UNLOC
c1170 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c  K %d exclusive l
c1180 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  ock res=%d\n", p
c1190 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
c11a0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65      if( locktype
c11b0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26  ==SHARED_LOCK &&
c11c0 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69   getReadLock(pFi
c11d0 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20  le) != NO_ERROR 
c11e0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ){.      /* This
c11f0 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61   should never ha
c1200 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64  ppen.  We should
c1210 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20   always be able 
c1220 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63  to.      ** reac
c1230 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c  quire the read l
c1240 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54  ock */.      OST
c1250 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE3( "UNLOCK %
c1260 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c  d to %d getReadL
c1270 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c  ock() failed\n",
c1280 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
c1290 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20  ype );.      rc 
c12a0 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  = SQLITE_IOERR_U
c12b0 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  NLOCK;.    }.  }
c12c0 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53  .  if( type>=RES
c12d0 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  ERVED_LOCK ){.  
c12e0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
c12f0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
c1300 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
c1310 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
c1320 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45  a.lOffset = RESE
c1330 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55  RVED_BYTE;.    U
c1340 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c1350 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 1L;.    res =
c1360 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
c1370 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
c1380 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
c1390 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
c13a0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
c13b0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
c13c0 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64   reserved res=%d
c13d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
c13e0 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  es );.  }.  if( 
c13f0 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
c1400 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45  K && type>=SHARE
c1410 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65  D_LOCK ){.    re
c1420 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  s = unlockReadLo
c1430 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f  ck(pFile);.    O
c1440 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b  STRACE5( "UNLOCK
c1450 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25   %d is %d want %
c1460 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69  d res=%d\n", pFi
c1470 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63  le->h, type, loc
c1480 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20  ktype, res );.  
c1490 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45  }.  if( type>=PE
c14a0 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20  NDING_LOCK ){.  
c14b0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
c14c0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
c14d0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
c14e0 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
c14f0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44  a.lOffset = PEND
c1500 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e  ING_BYTE;.    Un
c1510 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c1520 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 1L;.    res = 
c1530 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c1540 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c1550 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c1560 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c1570 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
c1580 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20  CE3( "UNLOCK %d 
c1590 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e  pending res=%d\n
c15a0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73  ", pFile->h, res
c15b0 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d   );.  }.  pFile-
c15c0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
c15d0 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33  type;.  OSTRACE3
c15e0 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77  ( "UNLOCK %d now
c15f0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
c1600 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
c1610 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  e );.  return rc
c1620 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72  ;.}../*.** Contr
c1630 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20  ol and query of 
c1640 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61  the open file ha
c1650 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ndle..*/.static 
c1660 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72  int os2FileContr
c1670 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ol(sqlite3_file 
c1680 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
c1690 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74  d *pArg){.  swit
c16a0 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
c16b0 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  se SQLITE_FCNTL_
c16c0 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20  LOCKSTATE: {.   
c16d0 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d     *(int*)pArg =
c16e0 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d   ((os2File*)id)-
c16f0 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20  >locktype;.     
c1700 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54   OSTRACE3( "FCNT
c1710 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c  L_LOCKSTATE %d l
c1720 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32  ock=%d\n", ((os2
c1730 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28  File*)id)->h, ((
c1740 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  os2File*)id)->lo
c1750 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20  cktype );.      
c1760 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
c1770 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
c1780 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
c1790 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  R;.}../*.** Retu
c17a0 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  rn the sector si
c17b0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
c17c0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c  he underlying bl
c17d0 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a  ock device for.*
c17e0 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  * the specified 
c17f0 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c  file. This is al
c1800 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20  most always 512 
c1810 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62  bytes, but may b
c1820 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20  e.** larger for 
c1830 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a  some devices..**
c1840 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20  .** SQLite code 
c1850 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e  assumes this fun
c1860 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69  ction cannot fai
c1870 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d  l. It also assum
c1880 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77  es that.** if tw
c1890 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61  o files are crea
c18a0 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ted in the same 
c18b0 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65  file-system dire
c18c0 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61  ctory (i.e..** a
c18d0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74   database and it
c18e0 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20  s journal file) 
c18f0 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20  that the sector 
c1900 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65  size will be the
c1910 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74  .** same for bot
c1920 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h..*/.static int
c1930 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73   os2SectorSize(s
c1940 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
c1950 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  {.  return SQLIT
c1960 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
c1970 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  _SIZE;.}../*.** 
c1980 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20  Return a vector 
c1990 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63  of device charac
c19a0 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74  teristics..*/.st
c19b0 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69  atic int os2Devi
c19c0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
c19d0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
c19e0 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b  id){.  return 0;
c19f0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61  .}.../*.** Chara
c1a00 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 73  cter set convers
c1a10 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 64  ion objects used
c1a20 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72   by conversion r
c1a30 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74  outines..*/.stat
c1a40 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75  ic UconvObject u
c1a50 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a  cUtf8 = NULL; /*
c1a60 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e   convert between
c1a70 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d 32   UTF-8 and UCS-2
c1a80 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76   */.static Uconv
c1a90 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 4e  Object uclCp = N
c1aa0 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74  ULL;  /* convert
c1ab0 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63   between local c
c1ac0 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 2d  odepage and UCS-
c1ad0 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70  2 */../*.** Help
c1ae0 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69  er function to i
c1af0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f  nitialize the co
c1b00 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73  nversion objects
c1b10 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46   from and to UTF
c1b20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  -8..*/.static vo
c1b30 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65  id initUconvObje
c1b40 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69  cts( void ){.  i
c1b50 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e  f( UniCreateUcon
c1b60 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c 20  vObject( UTF_8, 
c1b70 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c 53  &ucUtf8 ) != ULS
c1b80 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75  _SUCCESS ).    u
c1b90 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20  cUtf8 = NULL;.  
c1ba0 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 63  if ( UniCreateUc
c1bb0 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 43  onvObject( (UniC
c1bc0 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65  har *)L"@path=ye
c1bd0 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d 20  s", &uclCp ) != 
c1be0 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20  ULS_SUCCESS ).  
c1bf0 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a    uclCp = NULL;.
c1c00 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  }../*.** Helper 
c1c10 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 65  function to free
c1c20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
c1c30 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64  objects from and
c1c40 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74   to UTF-8..*/.st
c1c50 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 63  atic void freeUc
c1c60 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64  onvObjects( void
c1c70 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 66   ){.  if ( ucUtf
c1c80 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55  8 ).    UniFreeU
c1c90 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 74  convObject( ucUt
c1ca0 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 6c  f8 );.  if ( ucl
c1cb0 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65  Cp ).    UniFree
c1cc0 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c  UconvObject( ucl
c1cd0 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 3d  Cp );.  ucUtf8 =
c1ce0 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d   NULL;.  uclCp =
c1cf0 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   NULL;.}../*.** 
c1d00 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
c1d10 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38  to convert UTF-8
c1d20 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f   filenames to lo
c1d30 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67  cal OS/2 codepag
c1d40 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74  e..** The two-st
c1d50 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73  ep process: firs
c1d60 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e  t convert the in
c1d70 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72  coming UTF-8 str
c1d80 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d  ing.** into UCS-
c1d90 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20  2 and then from 
c1da0 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72  UCS-2 to the cur
c1db0 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a  rent codepage..*
c1dc0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63  * The returned c
c1dd0 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20  har pointer has 
c1de0 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a  to be freed..*/.
c1df0 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f 6e  static char *con
c1e00 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70  vertUtf8PathToCp
c1e10 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e  ( const char *in
c1e20 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65   ){.  UniChar te
c1e30 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54  mpPath[CCHMAXPAT
c1e40 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20  H];.  char *out 
c1e50 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63  = (char *)calloc
c1e60 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20  ( CCHMAXPATH, 1 
c1e70 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29  );..  if( !out )
c1e80 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c  .    return NULL
c1e90 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38  ;..  if( !ucUtf8
c1ea0 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20   || !uclCp ).   
c1eb0 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74   initUconvObject
c1ec0 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72  s();..  /* deter
c1ed0 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20  mine string for 
c1ee0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f  the conversion o
c1ef0 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 73  f UTF-8 which is
c1f00 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 28   CP1208 */.  if(
c1f10 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63   UniStrToUcs( uc
c1f20 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20  Utf8, tempPath, 
c1f30 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d  (char *)in, CCHM
c1f40 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f  AXPATH ) != ULS_
c1f50 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65  SUCCESS ).    re
c1f60 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20  turn out; /* if 
c1f70 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73  conversion fails
c1f80 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70  , return the emp
c1f90 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20  ty string */..  
c1fa0 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f  /* conversion fo
c1fb0 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  r current codepa
c1fc0 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  ge which can be 
c1fd0 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a  used for paths *
c1fe0 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63  /.  UniStrFromUc
c1ff0 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74  s( uclCp, out, t
c2000 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50  empPath, CCHMAXP
c2010 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e  ATH );..  return
c2020 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48   out;.}../*.** H
c2030 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74  elper function t
c2040 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61  o convert filena
c2050 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63  mes from local c
c2060 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38  odepage to UTF-8
c2070 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65  ..** The two-ste
c2080 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74  p process: first
c2090 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63   convert the inc
c20a0 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73  oming codepage-s
c20b0 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e  pecific.** strin
c20c0 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64  g into UCS-2 and
c20d0 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32   then from UCS-2
c20e0 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65   to the codepage
c20f0 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68   of UTF-8..** Th
c2100 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20  e returned char 
c2110 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62  pointer has to b
c2120 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  e freed..**.** T
c2130 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
c2140 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 65  non-static to be
c2150 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 69   able to use thi
c2160 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64  s in shell.c and
c2170 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c  .** similar appl
c2180 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61  ications that ta
c2190 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20  ke command line 
c21a0 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68  arguments..*/.ch
c21b0 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74  ar *convertCpPat
c21c0 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63  hToUtf8( const c
c21d0 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69  har *in ){.  Uni
c21e0 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43  Char tempPath[CC
c21f0 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61  HMAXPATH];.  cha
c2200 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a  r *out = (char *
c2210 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50  )calloc( CCHMAXP
c2220 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28  ATH, 1 );..  if(
c2230 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75   !out ).    retu
c2240 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20  rn NULL;..  if( 
c2250 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43  !ucUtf8 || !uclC
c2260 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e  p ).    initUcon
c2270 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f  vObjects();..  /
c2280 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72  * conversion for
c2290 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67   current codepag
c22a0 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75  e which can be u
c22b0 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f  sed for paths */
c22c0 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55  .  if( UniStrToU
c22d0 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 50  cs( uclCp, tempP
c22e0 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c  ath, (char *)in,
c22f0 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d   CCHMAXPATH ) !=
c2300 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20   ULS_SUCCESS ). 
c2310 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f     return out; /
c2320 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20  * if conversion 
c2330 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68  fails, return th
c2340 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a  e empty string *
c2350 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e  /..  /* determin
c2360 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  e string for the
c2370 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55   conversion of U
c2380 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50  TF-8 which is CP
c2390 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72  1208 */.  UniStr
c23a0 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c  FromUcs( ucUtf8,
c23b0 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20   out, tempPath, 
c23c0 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20  CCHMAXPATH );.. 
c23d0 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a   return out;.}..
c23e0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f  /*.** This vecto
c23f0 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68  r defines all th
c2400 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63  e methods that c
c2410 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e  an operate on an
c2420 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  .** sqlite3_file
c2430 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61   for os2..*/.sta
c2440 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
c2450 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32  3_io_methods os2
c2460 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31  IoMethod = {.  1
c2470 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
c2480 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
c2490 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f  sion */.  os2Clo
c24a0 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20  se,.  os2Read,. 
c24b0 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 32   os2Write,.  os2
c24c0 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53  Truncate,.  os2S
c24d0 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69  ync,.  os2FileSi
c24e0 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20  ze,.  os2Lock,. 
c24f0 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73   os2Unlock,.  os
c2500 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  2CheckReservedLo
c2510 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e  ck,.  os2FileCon
c2520 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f  trol,.  os2Secto
c2530 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69  rSize,.  os2Devi
c2540 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
c2550 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.};../*********
c2560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c25a0 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20  **.** Here ends 
c25b0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20  the I/O methods 
c25c0 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71  that form the sq
c25d0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
c25e0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54   object..**.** T
c25f0 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66  he next block of
c2600 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73   code implements
c2610 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73   the VFS methods
c2620 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
c2630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c2670 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
c2680 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
c2690 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42  ame in zBuf.  zB
c26a0 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65  uf must be big e
c26b0 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64  nough to.** hold
c26c0 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68   at pVfs->mxPath
c26d0 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e  name characters.
c26e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
c26f0 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e  etTempname(int n
c2700 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20  Buf, char *zBuf 
c2710 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
c2720 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
c2730 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22  zChars[] =.    "
c2740 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70  abcdefghijklmnop
c2750 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20  qrstuvwxyz".    
c2760 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  "ABCDEFGHIJKLMNO
c2770 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20  PQRSTUVWXYZ".   
c2780 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20   "0123456789";. 
c2790 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61   int i, j;.  cha
c27a0 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b 33  r zTempPathBuf[3
c27b0 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61  ];.  PSZ zTempPa
c27c0 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70  th = (PSZ)&zTemp
c27d0 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 73  PathBuf;.  if( s
c27e0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
c27f0 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 65  ctory ){.    zTe
c2800 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33  mpPath = sqlite3
c2810 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b  _temp_directory;
c2820 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
c2830 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50  ( DosScanEnv( (P
c2840 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d  SZ)"TEMP", &zTem
c2850 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20  pPath ) ){.     
c2860 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28   if( DosScanEnv(
c2870 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54   (PSZ)"TMP", &zT
c2880 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20  empPath ) ){.   
c2890 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e       if( DosScan
c28a0 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49  Env( (PSZ)"TMPDI
c28b0 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29  R", &zTempPath )
c28c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55   ){.           U
c28d0 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20  LONG ulDriveNum 
c28e0 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20  = 0, ulDriveMap 
c28f0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
c2900 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44  DosQueryCurrentD
c2910 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d  isk( &ulDriveNum
c2920 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b  , &ulDriveMap );
c2930 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69  .           spri
c2940 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d  ntf( (char*)zTem
c2950 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63  pPath, "%c:", (c
c2960 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72  har)( 'A' + ulDr
c2970 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a  iveNum - 1 ) );.
c2980 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
c2990 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  }.    }.  }.  /*
c29a0 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61   Strip off a tra
c29b0 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72  iling slashes or
c29c0 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74   backslashes, ot
c29d0 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64  herwise we would
c29e0 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74   get *.   * mult
c29f0 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68  iple (back)slash
c2a00 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20  es which causes 
c2a10 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69  DosOpen() to fai
c2a20 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l.              
c2a30 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 20  *.   * Trailing 
c2a40 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 61  spaces are not a
c2a50 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e 20  llowed, either. 
c2a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2a70 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
c2a80 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  j = sqlite3Strle
c2a90 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a  n30(zTempPath);.
c2aa0 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 26    while( j > 0 &
c2ab0 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d  & ( zTempPath[j-
c2ac0 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54  1] == '\\' || zT
c2ad0 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20  empPath[j-1] == 
c2ae0 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 20  '/'.            
c2af0 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70          || zTemp
c2b00 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27  Path[j-1] == ' '
c2b10 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20   ) ){.    j--;. 
c2b20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a   }.  zTempPath[j
c2b30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20  ] = '\0';.  if( 
c2b40 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69  !sqlite3_temp_di
c2b50 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 63  rectory ){.    c
c2b60 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 54  har *zTempPathUT
c2b70 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74  F = convertCpPat
c2b80 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 61  hToUtf8( zTempPa
c2b90 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  th );.    sqlite
c2ba0 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66  3_snprintf( nBuf
c2bb0 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20  -30, zBuf,.     
c2bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2bd0 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45   "%s\\"SQLITE_TE
c2be0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20  MP_FILE_PREFIX, 
c2bf0 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a  zTempPathUTF );.
c2c00 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50      free( zTempP
c2c10 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73  athUTF );.  }els
c2c20 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  e{.    sqlite3_s
c2c30 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30  nprintf( nBuf-30
c2c40 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20  , zBuf,.        
c2c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25                "%
c2c60 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f  s\\"SQLITE_TEMP_
c2c70 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65  FILE_PREFIX, zTe
c2c80 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20  mpPath );.  }.  
c2c90 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  j = sqlite3Strle
c2ca0 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 73  n30( zBuf );.  s
c2cb0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
c2cc0 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20  s( 20, &zBuf[j] 
c2cd0 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 3b  );.  for( i = 0;
c2ce0 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b   i < 20; i++, j+
c2cf0 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d  + ){.    zBuf[j]
c2d00 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b   = (char)zChars[
c2d10 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72   ((unsigned char
c2d20 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f  )zBuf[j])%(sizeo
c2d30 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a  f(zChars)-1) ];.
c2d40 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20    }.  zBuf[j] = 
c2d50 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22  0;.  OSTRACE2( "
c2d60 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25  TEMP FILENAME: %
c2d70 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20  s\n", zBuf );.  
c2d80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
c2d90 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e  ;.}.../*.** Turn
c2da0 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68   a relative path
c2db0 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c  name into a full
c2dc0 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74   pathname.  Writ
c2dd0 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61  e the full.** pa
c2de0 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c  thname into zFul
c2df0 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69  l[].  zFull[] wi
c2e00 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70  ll be at least p
c2e10 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a  Vfs->mxPathname.
c2e20 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ** bytes in size
c2e30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c2e40 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  os2FullPathname(
c2e50 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
c2e60 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f  pVfs,          /
c2e70 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73  * Pointer to vfs
c2e80 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e   object */.  con
c2e90 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69  st char *zRelati
c2ea0 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73  ve,      /* Poss
c2eb0 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e  ibly relative in
c2ec0 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e  put path */.  in
c2ed0 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20  t nFull,        
c2ee0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
c2ef0 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66  e of output buff
c2f00 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  er in bytes */. 
c2f10 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20   char *zFull    
c2f20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c2f30 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f  Output buffer */
c2f40 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c  .){.  char *zRel
c2f50 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 72  ativeCp = conver
c2f60 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a  tUtf8PathToCp( z
c2f70 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 68  Relative );.  ch
c2f80 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41  ar zFullCp[CCHMA
c2f90 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20  XPATH] = "\0";. 
c2fa0 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b   char *zFullUTF;
c2fb0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44  .  APIRET rc = D
c2fc0 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28  osQueryPathInfo(
c2fd0 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 49   zRelativeCp, FI
c2fe0 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c  L_QUERYFULLNAME,
c2ff0 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20   zFullCp,.      
c3000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3010 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 58            CCHMAX
c3020 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 20  PATH );.  free( 
c3030 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a 20  zRelativeCp );. 
c3040 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76   zFullUTF = conv
c3050 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28  ertCpPathToUtf8(
c3060 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71   zFullCp );.  sq
c3070 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20  lite3_snprintf( 
c3080 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46  nFull, zFull, zF
c3090 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 65  ullUTF );.  free
c30a0 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20  ( zFullUTF );.  
c30b0 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f  return rc == NO_
c30c0 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f  ERROR ? SQLITE_O
c30d0 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  K : SQLITE_IOERR
c30e0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  ;.}.../*.** Open
c30f0 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74   a file..*/.stat
c3100 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a  ic int os2Open(.
c3110 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
c3120 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  Vfs,            
c3130 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20  /* Not used */. 
c3140 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
c3150 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  me,            /
c3160 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69  * Name of the fi
c3170 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  le */.  sqlite3_
c3180 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20  file *id,       
c3190 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
c31a0 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68  he SQLite file h
c31b0 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20  andle here */.  
c31c0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
c31d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c31e0 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73   Open mode flags
c31f0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46   */.  int *pOutF
c3200 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20  lags            
c3210 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65      /* Status re
c3220 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b  turn flags */.){
c3230 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c  .  HFILE h;.  UL
c3240 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 62  ONG ulFileAttrib
c3250 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41  ute = FILE_NORMA
c3260 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65  L;.  ULONG ulOpe
c3270 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c  nFlags = 0;.  UL
c3280 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d  ONG ulOpenMode =
c3290 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70   0;.  os2File *p
c32a0 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
c32b0 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63  )id;.  APIRET rc
c32c0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55   = NO_ERROR;.  U
c32d0 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20  LONG ulAction;. 
c32e0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a   char *zNameCp;.
c32f0 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b    char zTmpname[
c3300 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20  CCHMAXPATH+1];  
c3310 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 68    /* Buffer to h
c3320 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70  old name of temp
c3330 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49   file */..  /* I
c3340 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
c3350 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
c3360 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20  nction is NULL, 
c3370 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a  generate a .  **
c3380 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
c3390 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a  name to use .  *
c33a0 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29  /.  if( !zName )
c33b0 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67  {.    int rc = g
c33c0 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41  etTempname(CCHMA
c33d0 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d  XPATH+1, zTmpnam
c33e0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
c33f0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
c3400 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
c3410 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20    }.    zName = 
c3420 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a  zTmpname;.  }...
c3430 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 2c    memset( pFile,
c3440 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c   0, sizeof(*pFil
c3450 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45  e) );..  OSTRACE
c3460 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 64  2( "OPEN want %d
c3470 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20  \n", flags );.. 
c3480 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
c3490 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
c34a0 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e  TE ){.    ulOpen
c34b0 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43  Mode |= OPEN_ACC
c34c0 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a 20  ESS_READWRITE;. 
c34d0 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50     OSTRACE1( "OP
c34e0 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22  EN read/write\n"
c34f0 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
c3500 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f   ulOpenMode |= O
c3510 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f  PEN_ACCESS_READO
c3520 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  NLY;.    OSTRACE
c3530 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e  1( "OPEN read on
c3540 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20  ly\n" );.  }..  
c3550 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
c3560 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29  TE_OPEN_CREATE )
c3570 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67  {.    ulOpenFlag
c3580 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e  s |= OPEN_ACTION
c3590 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20  _OPEN_IF_EXISTS 
c35a0 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52  | OPEN_ACTION_CR
c35b0 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20  EATE_IF_NEW;.   
c35c0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
c35d0 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 65   open new/create
c35e0 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  \n" );.  }else{.
c35f0 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20      ulOpenFlags 
c3600 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f  |= OPEN_ACTION_O
c3610 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20  PEN_IF_EXISTS | 
c3620 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c  OPEN_ACTION_FAIL
c3630 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54  _IF_NEW;.    OST
c3640 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65  RACE1( "OPEN ope
c3650 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b  n existing\n" );
c3660 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67  .  }..  if( flag
c3670 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
c3680 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75  MAIN_DB ){.    u
c3690 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45  lOpenMode |= OPE
c36a0 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45  N_SHARE_DENYNONE
c36b0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20  ;.    OSTRACE1( 
c36c0 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64  "OPEN share read
c36d0 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d  /write\n" );.  }
c36e0 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e  else{.    ulOpen
c36f0 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41  Mode |= OPEN_SHA
c3700 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 20  RE_DENYWRITE;.  
c3710 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
c3720 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e 6c  N share read onl
c3730 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  y\n" );.  }..  i
c3740 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
c3750 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
c3760 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72  LOSE ){.    char
c3770 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 58   pathUtf8[CCHMAX
c3780 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44  PATH];.#ifdef ND
c3790 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 62  EBUG /* when deb
c37a0 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 74  ugging we want t
c37b0 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69  o make sure it i
c37c0 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20  s deleted */.   
c37d0 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65   ulFileAttribute
c37e0 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a   = FILE_HIDDEN;.
c37f0 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 75  #endif.    os2Fu
c3800 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 73  llPathname( pVfs
c3810 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50  , zName, CCHMAXP
c3820 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 3b  ATH, pathUtf8 );
c3830 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68  .    pFile->path
c3840 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55  ToDel = convertU
c3850 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 74  tf8PathToCp( pat
c3860 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 54  hUtf8 );.    OST
c3870 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 64  RACE1( "OPEN hid
c3880 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c  den/delete on cl
c3890 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 75  ose file attribu
c38a0 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73  tes\n" );.  }els
c38b0 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61  e{.    pFile->pa
c38c0 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a  thToDel = NULL;.
c38d0 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f      OSTRACE1( "O
c38e0 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20  PEN normal file 
c38f0 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a  attribute\n" );.
c3900 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73    }..  /* always
c3910 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20   open in random 
c3920 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20  access mode for 
c3930 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20  possibly better 
c3940 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65  speed */.  ulOpe
c3950 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c  nMode |= OPEN_FL
c3960 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c  AGS_RANDOM;.  ul
c3970 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e  OpenMode |= OPEN
c3980 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45  _FLAGS_FAIL_ON_E
c3990 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f  RROR;.  ulOpenMo
c39a0 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53  de |= OPEN_FLAGS
c39b0 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a  _NOINHERIT;..  z
c39c0 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74  NameCp = convert
c39d0 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 4e  Utf8PathToCp( zN
c39e0 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f  ame );.  rc = Do
c39f0 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d  sOpen( (PSZ)zNam
c3a00 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20  eCp,.           
c3a10 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 20       &h,.       
c3a20 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 69           &ulActi
c3a30 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  on,.            
c3a40 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20      0L,.        
c3a50 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 74          ulFileAt
c3a60 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 20  tribute,.       
c3a70 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 46           ulOpenF
c3a80 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lags,.          
c3a90 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65        ulOpenMode
c3aa0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c3ab0 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29    (PEAOP2)NULL )
c3ac0 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 43  ;.  free( zNameC
c3ad0 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d  p );.  if( rc !=
c3ae0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c3af0 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 4e   OSTRACE7( "OPEN
c3b00 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 20   Invalid handle 
c3b10 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c  rc=%d: zName=%s,
c3b20 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20   ulAction=%#lx, 
c3b30 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46  ulAttr=%#lx, ulF
c3b40 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64  lags=%#lx, ulMod
c3b50 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20  e=%#lx\n",.     
c3b60 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e 61           rc, zNa
c3b70 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c  me, ulAction, ul
c3b80 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 75  FileAttribute, u
c3b90 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70  lOpenFlags, ulOp
c3ba0 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 66  enMode );.    if
c3bb0 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
c3bc0 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 28  el ).      free(
c3bd0 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
c3be0 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  l );.    pFile->
c3bf0 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c  pathToDel = NULL
c3c00 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20  ;.    if( flags 
c3c10 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  & SQLITE_OPEN_RE
c3c20 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20  ADWRITE ){.     
c3c30 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e   OSTRACE2( "OPEN
c3c40 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e 64   %d Invalid hand
c3c50 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c  le\n", ((flags |
c3c60 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
c3c70 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45  DONLY) & ~SQLITE
c3c80 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29  _OPEN_READWRITE)
c3c90 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
c3ca0 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c 20   os2Open( pVfs, 
c3cb0 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20  zName, id,.     
c3cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3cd0 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54   ((flags | SQLIT
c3ce0 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29  E_OPEN_READONLY)
c3cf0 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   & ~SQLITE_OPEN_
c3d00 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 20  READWRITE),.    
c3d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3d20 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20    pOutFlags );. 
c3d30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
c3d40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
c3d50 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20  NTOPEN;.    }.  
c3d60 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61  }..  if( pOutFla
c3d70 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46  gs ){.    *pOutF
c3d80 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 53  lags = flags & S
c3d90 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
c3da0 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50  RITE ? SQLITE_OP
c3db0 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 53  EN_READWRITE : S
c3dc0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
c3dd0 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c  NLY;.  }..  pFil
c3de0 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73  e->pMethod = &os
c3df0 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69  2IoMethod;.  pFi
c3e00 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65  le->h = h;.  Ope
c3e10 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20  nCounter(+1);.  
c3e20 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e 20  OSTRACE3( "OPEN 
c3e30 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c  %d pOutFlags=%d\
c3e40 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f  n", pFile->h, pO
c3e50 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 74  utFlags );.  ret
c3e60 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
c3e70 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74  ../*.** Delete t
c3e80 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a  he named file..*
c3e90 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
c3ea0 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65  Delete(.  sqlite
c3eb0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
c3ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3ed0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20   /* Not used on 
c3ee0 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  os2 */.  const c
c3ef0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
c3f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3f10 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20  /* Name of file 
c3f20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69  to delete */.  i
c3f30 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20  nt syncDir      
c3f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3f50 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65        /* Not use
c3f60 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20  d on os2 */.){. 
c3f70 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
c3f80 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a  ERROR;.  char *z
c3f90 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e  FilenameCp = con
c3fa0 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70  vertUtf8PathToCp
c3fb0 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20  ( zFilename );. 
c3fc0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
c3fd0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
c3fe0 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a  IOERR_DELETE );.
c3ff0 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 65    rc = DosDelete
c4000 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65  ( (PSZ)zFilename
c4010 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46  Cp );.  free( zF
c4020 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f  ilenameCp );.  O
c4030 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 45  STRACE2( "DELETE
c4040 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c   \"%s\"\n", zFil
c4050 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72  ename );.  retur
c4060 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52  n rc == NO_ERROR
c4070 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
c4080 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
c4090 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  TE;.}../*.** Che
c40a0 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  ck the existance
c40b0 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61   and status of a
c40c0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
c40d0 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0a   int os2Access(.
c40e0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
c40f0 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e  Vfs,        /* N
c4100 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a  ot used on os2 *
c4110 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
c4120 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a  zFilename,    /*
c4130 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f   Name of file to
c4140 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   check */.  int 
c4150 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
c4160 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66        /* Type of
c4170 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e   test to make on
c4180 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
c4190 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 20  int *pOut       
c41a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
c41b0 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  te results here 
c41c0 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54  */.){.  FILESTAT
c41d0 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49  US3 fsts3ConfigI
c41e0 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63  nfo;.  APIRET rc
c41f0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63   = NO_ERROR;.  c
c4200 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70  har *zFilenameCp
c4210 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61   = convertUtf8Pa
c4220 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d  thToCp( zFilenam
c4230 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20  e );..  memset( 
c4240 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f  &fsts3ConfigInfo
c4250 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73  , 0, sizeof(fsts
c4260 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a  3ConfigInfo) );.
c4270 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50    rc = DosQueryP
c4280 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46  athInfo( (PSZ)zF
c4290 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53  ilenameCp, FIL_S
c42a0 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20  TANDARD,.       
c42b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c42c0 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e    &fsts3ConfigIn
c42d0 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53  fo, sizeof(FILES
c42e0 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65  TATUS3) );.  fre
c42f0 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29  e( zFilenameCp )
c4300 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41  ;.  OSTRACE4( "A
c4310 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69  CCESS fsts3Confi
c4320 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25  gInfo.attrFile=%
c4330 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64  d flags=%d rc=%d
c4340 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
c4350 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f   fsts3ConfigInfo
c4360 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73  .attrFile, flags
c4370 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68  , rc );.  switch
c4380 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63  ( flags ){.    c
c4390 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ase SQLITE_ACCES
c43a0 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65  S_READ:.    case
c43b0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
c43c0 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20  XISTS:.      rc 
c43d0 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  = (rc == NO_ERRO
c43e0 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43  R);.      OSTRAC
c43f0 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61  E3( "ACCESS %s a
c4400 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e  ccess of read an
c4410 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c  d exists  rc=%d\
c4420 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72  n", zFilename, r
c4430 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  c );.      break
c4440 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
c4450 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
c4460 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28  TE:.      rc = (
c4470 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20  rc == NO_ERROR) 
c4480 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 69  && ( (fsts3Confi
c4490 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26  gInfo.attrFile &
c44a0 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20   FILE_READONLY) 
c44b0 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f 53  == 0 );.      OS
c44c0 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20  TRACE3( "ACCESS 
c44d0 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61  %s access of rea
c44e0 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c 6e  d/write  rc=%d\n
c44f0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63  ", zFilename, rc
c4500 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
c4510 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20  .    default:.  
c4520 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49 6e      assert( !"In
c4530 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75  valid flags argu
c4540 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a  ment" );.  }.  *
c4550 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74  pOut = rc;.  ret
c4560 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
c4570 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
c4580 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
c4590 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  NSION./*.** Inte
c45a0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
c45b0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
c45c0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
c45d0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
c45e0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
c45f0 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
c4600 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
c4610 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a  ibrary..*/./*.**
c4620 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20   Interfaces for 
c4630 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64  opening a shared
c4640 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e   library, findin
c4650 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a  g entry points.*
c4660 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61  * within the sha
c4670 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64  red library, and
c4680 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61   closing the sha
c4690 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  red library..*/.
c46a0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32  static void *os2
c46b0 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  DlOpen(sqlite3_v
c46c0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20  fs *pVfs, const 
c46d0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
c46e0 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72  {.  UCHAR loadEr
c46f0 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c  r[256];.  HMODUL
c4700 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54  E hmod;.  APIRET
c4710 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69   rc;.  char *zFi
c4720 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65  lenameCp = conve
c4730 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a  rtUtf8PathToCp(z
c4740 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20  Filename);.  rc 
c4750 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28  = DosLoadModule(
c4760 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69  (PSZ)loadErr, si
c4770 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a  zeof(loadErr), z
c4780 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f  FilenameCp, &hmo
c4790 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65  d);.  free(zFile
c47a0 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72  nameCp);.  retur
c47b0 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  n rc != NO_ERROR
c47c0 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d   ? 0 : (void*)hm
c47d0 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f  od;.}./*.** A no
c47e0 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 72  -op since the er
c47f0 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
c4800 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c  rned on the DosL
c4810 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a  oadModule call..
c4820 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74  ** os2Dlopen ret
c4830 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73  urns zero if Dos
c4840 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f  LoadModule is no
c4850 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f  t successful..*/
c4860 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
c4870 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  DlError(sqlite3_
c4880 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
c4890 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f  Buf, char *zBufO
c48a0 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f  ut){./* no-op */
c48b0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  .}.static void *
c48c0 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 33  os2DlSym(sqlite3
c48d0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
c48e0 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74   *pHandle, const
c48f0 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b   char *zSymbol){
c4900 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50  .  PFN pfn;.  AP
c4910 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20  IRET rc;.  rc = 
c4920 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72  DosQueryProcAddr
c4930 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c  ((HMODULE)pHandl
c4940 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20  e, 0L, zSymbol, 
c4950 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 20  &pfn);.  if( rc 
c4960 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  != NO_ERROR ){. 
c4970 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 6d     /* if the sym
c4980 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 6e  bol itself was n
c4990 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68  ot found, search
c49a0 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 73   again for the s
c49b0 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f  ame.     * symbo
c49c0 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 20  l with an extra 
c49d0 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 74  underscore, that
c49e0 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 64   might be needed
c49f0 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 20   depending.     
c4a00 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67  * on the calling
c4a10 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20   convention */. 
c4a20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c     char _zSymbol
c4a30 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20  [256] = "_";.   
c4a40 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f   strncat(_zSymbo
c4a50 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29  l, zSymbol, 255)
c4a60 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 75  ;.    rc = DosQu
c4a70 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f  eryProcAddr((HMO
c4a80 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c  DULE)pHandle, 0L
c4a90 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e  , _zSymbol, &pfn
c4aa0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
c4ab0 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f  rc != NO_ERROR ?
c4ac0 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b   0 : (void*)pfn;
c4ad0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  .}.static void o
c4ae0 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  s2DlClose(sqlite
c4af0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
c4b00 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44  d *pHandle){.  D
c4b10 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d  osFreeModule((HM
c4b20 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a  ODULE)pHandle);.
c4b30 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51  }.#else /* if SQ
c4b40 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
c4b50 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69  XTENSION is defi
c4b60 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e  ned: */.  #defin
c4b70 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20  e os2DlOpen 0.  
c4b80 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72  #define os2DlErr
c4b90 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f  or 0.  #define o
c4ba0 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66  s2DlSym 0.  #def
c4bb0 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30  ine os2DlClose 0
c4bc0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
c4bd0 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66  Write up to nBuf
c4be0 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d   bytes of random
c4bf0 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a  ness into zBuf..
c4c00 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c4c10 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  2Randomness(sqli
c4c20 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
c4c30 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
c4c40 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d  Buf ){.  int n =
c4c50 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28   0;.#if defined(
c4c60 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e  SQLITE_TEST).  n
c4c70 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65   = nBuf;.  memse
c4c80 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29  t(zBuf, 0, nBuf)
c4c90 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 69  ;.#else.  int si
c4ca0 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65  zeofULong = size
c4cb0 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28  of(ULONG);.  if(
c4cc0 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 54   (int)sizeof(DAT
c4cd0 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d  ETIME) <= nBuf -
c4ce0 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 49   n ){.    DATETI
c4cf0 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 74  ME x;.    DosGet
c4d00 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 20  DateTime(&x);.  
c4d10 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
c4d20 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29  ], &x, sizeof(x)
c4d30 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
c4d40 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  of(x);.  }..  if
c4d50 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d  ( sizeofULong <=
c4d60 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20   nBuf - n ){.   
c4d70 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20   PPIB ppib;.    
c4d80 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73  DosGetInfoBlocks
c4d90 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20  (NULL, &ppib);. 
c4da0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
c4db0 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f 75  n], &ppib->pib_u
c4dc0 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  lpid, sizeofULon
c4dd0 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  g);.    n += siz
c4de0 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20  eofULong;.  }.. 
c4df0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
c4e00 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
c4e10 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20      PTIB ptib;. 
c4e20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f     DosGetInfoBlo
c4e30 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29  cks(&ptib, NULL)
c4e40 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42  ;.    memcpy(&zB
c4e50 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69  uf[n], &ptib->ti
c4e60 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c  b_ptib2->tib2_ul
c4e70 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  tid, sizeofULong
c4e80 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
c4e90 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20  ofULong;.  }..  
c4ea0 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 68  /* if we still h
c4eb0 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 68  aven't filled th
c4ec0 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68 65  e buffer yet the
c4ed0 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20   following will 
c4ee0 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 65  */.  /* grab eve
c4ef0 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73  rything once ins
c4f00 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73  tead of making s
c4f10 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72  everal calls for
c4f20 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a   a single item *
c4f30 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c  /.  if( sizeofUL
c4f40 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ong <= nBuf - n 
c4f50 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53  ){.    ULONG ulS
c4f60 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b  ysInfo[QSV_MAX];
c4f70 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 73  .    DosQuerySys
c4f80 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58  Info(1L, QSV_MAX
c4f90 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a  , ulSysInfo, siz
c4fa0 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d  eofULong * QSV_M
c4fb0 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79  AX);..    memcpy
c4fc0 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79  (&zBuf[n], &ulSy
c4fd0 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55  sInfo[QSV_MS_COU
c4fe0 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55  NT - 1], sizeofU
c4ff0 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  Long);.    n += 
c5000 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20  sizeofULong;..  
c5010 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
c5020 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
c5030 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  .      memcpy(&z
c5040 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e  Buf[n], &ulSysIn
c5050 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54  fo[QSV_TIMER_INT
c5060 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65  ERVAL - 1], size
c5070 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20  ofULong);.      
c5080 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  n += sizeofULong
c5090 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
c50a0 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e  sizeofULong <= n
c50b0 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20  Buf - n ){.     
c50c0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
c50d0 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56  , &ulSysInfo[QSV
c50e0 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20  _TIME_LOW - 1], 
c50f0 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20  sizeofULong);.  
c5100 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
c5110 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Long;.    }.    
c5120 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  if( sizeofULong 
c5130 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
c5140 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75       memcpy(&zBu
c5150 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f  f[n], &ulSysInfo
c5160 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d  [QSV_TIME_HIGH -
c5170 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   1], sizeofULong
c5180 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69  );.      n += si
c5190 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d  zeofULong;.    }
c51a0 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55  .    if( sizeofU
c51b0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
c51c0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
c51d0 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79  (&zBuf[n], &ulSy
c51e0 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41  sInfo[QSV_TOTAVA
c51f0 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65  ILMEM - 1], size
c5200 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20  ofULong);.      
c5210 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  n += sizeofULong
c5220 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
c5230 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a  if..  return n;.
c5240 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66  }../*.** Sleep f
c5250 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c  or a little whil
c5260 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61  e.  Return the a
c5270 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c  mount of time sl
c5280 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75  ept..** The argu
c5290 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62  ment is the numb
c52a0 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e  er of microsecon
c52b0 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c  ds we want to sl
c52c0 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75  eep..** The retu
c52d0 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
c52e0 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73  number of micros
c52f0 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20  econds of sleep 
c5300 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75  actually.** requ
c5310 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75  ested from the u
c5320 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
c5330 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75  ing system, a nu
c5340 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69  mber which.** mi
c5350 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74  ght be greater t
c5360 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
c5370 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75  the argument, bu
c5380 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68  t not less.** th
c5390 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e  an the argument.
c53a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c53b0 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33  s2Sleep( sqlite3
c53c0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
c53d0 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f  microsec ){.  Do
c53e0 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 65  sSleep( (microse
c53f0 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 74  c/1000) );.  ret
c5400 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a  urn microsec;.}.
c5410 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
c5420 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69  wing variable, i
c5430 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a  f set to a non-z
c5440 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d  ero value, becom
c5450 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  es the result.**
c5460 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73   returned from s
c5470 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54  qlite3OsCurrentT
c5480 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20  ime().  This is 
c5490 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
c54a0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
c54b0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
c54c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
c54d0 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b  urrent_time = 0;
c54e0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
c54f0 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  ind the current 
c5500 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73  time (in Univers
c5510 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54  al Coordinated T
c5520 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65  ime).  Write the
c5530 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65  .** current time
c5540 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a   and date as a J
c5550 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72  ulian Day number
c5560 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64   into *prNow and
c5570 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52  .** return 0.  R
c5580 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74  eturn 1 if the t
c5590 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e  ime and date can
c55a0 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f  not be found..*/
c55b0 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 54  .int os2CurrentT
c55c0 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73  ime( sqlite3_vfs
c55d0 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a   *pVfs, double *
c55e0 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c  prNow ){.  doubl
c55f0 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d  e now;.  SHORT m
c5600 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20  inute; /* needs 
c5610 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f  to be able to co
c5620 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 65  pe with negative
c5630 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74   timezone offset
c5640 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 63   */.  USHORT sec
c5650 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20  ond, hour,.     
c5660 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20      day, month, 
c5670 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d 45  year;.  DATETIME
c5680 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 74   dt;.  DosGetDat
c5690 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 20  eTime( &dt );.  
c56a0 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54  second = (USHORT
c56b0 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d  )dt.seconds;.  m
c56c0 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 64  inute = (SHORT)d
c56d0 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e 74  t.minutes + dt.t
c56e0 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20  imezone;.  hour 
c56f0 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f 75  = (USHORT)dt.hou
c5700 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 48  rs;.  day = (USH
c5710 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f  ORT)dt.day;.  mo
c5720 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 74  nth = (USHORT)dt
c5730 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d  .month;.  year =
c5740 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 72   (USHORT)dt.year
c5750 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74  ;..  /* Calculat
c5760 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f  ions from http:/
c5770 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65  /www.astro.keele
c5780 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72  .ac.uk/~rno/Astr
c5790 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20  onomy/hjd.html. 
c57a0 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61      http://www.a
c57b0 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b  stro.keele.ac.uk
c57c0 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f  /~rno/Astronomy/
c57d0 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f  hjd-0.1.c */.  /
c57e0 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20  * Calculate the 
c57f0 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20  Julian days */. 
c5800 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 30   now = day - 320
c5810 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 79  76 +.    1461*(y
c5820 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f  ear + 4800 + (mo
c5830 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20  nth - 14)/12)/4 
c5840 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68  +.    367*(month
c5850 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20   - 2 - (month - 
c5860 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a  14)/12*12)/12 -.
c5870 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 34      3*((year + 4
c5880 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31  900 + (month - 1
c5890 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a  4)/12)/100)/4;..
c58a0 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 61    /* Add the fra
c58b0 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d  ctional hours, m
c58c0 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 20  ins and seconds 
c58d0 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75  */.  now += (hou
c58e0 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a  r + 12.0)/24.0;.
c58f0 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f    now += minute/
c5900 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d  1440.0;.  now +=
c5910 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b   second/86400.0;
c5920 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b  .  *prNow = now;
c5930 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
c5940 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  EST.  if( sqlite
c5950 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29  3_current_time )
c5960 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73  {.    *prNow = s
c5970 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
c5980 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34  ime/86400.0 + 24
c5990 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e  40587.5;.  }.#en
c59a0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  dif.  return 0;.
c59b0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  }..static int os
c59c0 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71  2GetLastError(sq
c59d0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
c59e0 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20   int nBuf, char 
c59f0 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e  *zBuf){.  return
c5a00 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69   0;.}../*.** Ini
c5a10 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
c5a20 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65  itialize the ope
c5a30 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e  rating system in
c5a40 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49  terface..*/.SQLI
c5a50 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c5a60 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29  e3_os_init(void)
c5a70 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
c5a80 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d 20  e3_vfs os2Vfs = 
c5a90 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20  {.    1,        
c5aa0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
c5ab0 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65  sion */.    size
c5ac0 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f  of(os2File),   /
c5ad0 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20  * szOsFile */.  
c5ae0 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 20    CCHMAXPATH,   
c5af0 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61       /* mxPathna
c5b00 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  me */.    0,    
c5b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c5b20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73  pNext */.    "os
c5b30 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  2",             
c5b40 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  /* zName */.    
c5b50 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
c5b60 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a     /* pAppData *
c5b70 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20  /..    os2Open, 
c5b80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70            /* xOp
c5b90 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c  en */.    os2Del
c5ba0 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ete,         /* 
c5bb0 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f  xDelete */.    o
c5bc0 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20  s2Access,       
c5bd0 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a    /* xAccess */.
c5be0 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e      os2FullPathn
c5bf0 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50  ame,   /* xFullP
c5c00 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f  athname */.    o
c5c10 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20  s2DlOpen,       
c5c20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a    /* xDlOpen */.
c5c30 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20      os2DlError, 
c5c40 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72         /* xDlErr
c5c50 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53  or */.    os2DlS
c5c60 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ym,          /* 
c5c70 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73  xDlSym */.    os
c5c80 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  2DlClose,       
c5c90 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a   /* xDlClose */.
c5ca0 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73      os2Randomnes
c5cb0 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f  s,     /* xRando
c5cc0 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32  mness */.    os2
c5cd0 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20  Sleep,          
c5ce0 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20  /* xSleep */.   
c5cf0 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 2c   os2CurrentTime,
c5d00 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54      /* xCurrentT
c5d10 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65  ime */.    os2Ge
c5d20 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a  tLastError    /*
c5d30 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a   xGetLastError *
c5d40 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  /.  };.  sqlite3
c5d50 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 6f  _vfs_register(&o
c5d60 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69  s2Vfs, 1);.  ini
c5d70 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b  tUconvObjects();
c5d80 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
c5d90 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  _OK;.}.SQLITE_AP
c5da0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  I int sqlite3_os
c5db0 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 72  _end(void){.  fr
c5dc0 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29  eeUconvObjects()
c5dd0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
c5de0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  E_OK;.}..#endif 
c5df0 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  /* SQLITE_OS_OS2
c5e00 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
c5e10 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f  **** End of os_o
c5e20 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s2.c ***********
c5e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e50 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c5e60 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
c5e70 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a  os_unix.c ******
c5e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5ea0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
c5eb0 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65  May 22.**.** The
c5ec0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
c5ed0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
c5ee0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
c5ef0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
c5f00 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
c5f10 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
c5f20 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
c5f30 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
c5f40 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
c5f50 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
c5f60 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
c5f70 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
c5f80 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
c5f90 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
c5fa0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
c5fb0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
c5fc0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
c5fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
c6020 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
c6030 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 6c  ins the VFS impl
c6040 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75  ementation for u
c6050 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69  nix-like operati
c6060 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e  ng systems.** in
c6070 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63  clude Linux, Mac
c6080 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20  OSX, *BSD, QNX, 
c6090 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50  VxWorks, AIX, HP
c60a0 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a  UX, and others..
c60b0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
c60c0 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 6c  actually several
c60d0 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 69   different VFS i
c60e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69  mplementations i
c60f0 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20  n this file..** 
c6100 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20  The differences 
c6110 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 74  are in the way t
c6120 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  hat file locking
c6130 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64   is done.  The d
c6140 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d  efault.** implem
c6150 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f  entation uses Po
c6160 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63  six Advisory Loc
c6170 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65  ks.  Alternative
c6180 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
c6190 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c  .** use flock(),
c61a0 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69   dot-files, vari
c61b0 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20  ous proprietary 
c61c0 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c  locking schemas,
c61d0 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b   or simply.** sk
c61e0 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74  ip locking all t
c61f0 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  ogether..**.** T
c6200 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20  his source file 
c6210 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74  is organized int
c6220 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 72  o divisions wher
c6230 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 20  e the logic for 
c6240 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75  various.** subfu
c6250 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61  nctions is conta
c6260 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  ined within the 
c6270 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 69  appropriate divi
c6280 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a  sion.  PLEASE.**
c6290 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 54   KEEP THE STRUCT
c62a0 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c 45  URE OF THIS FILE
c62b0 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 6f   INTACT.  New co
c62c0 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c 61  de should be pla
c62d0 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f  ced.** in the co
c62e0 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 61  rrect division a
c62f0 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 65  nd should be cle
c6300 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a  arly labeled..**
c6310 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 6f  .** The layout o
c6320 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 61  f divisions is a
c6330 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
c6340 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75     *  General-pu
c6350 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 6f  rpose declaratio
c6360 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 66  ns and utility f
c6370 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a  unctions..**   *
c6380 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44    Unique file ID
c6390 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 56   logic used by V
c63a0 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20  xWorks..**   *  
c63b0 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20  Various locking 
c63c0 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d  primitive implem
c63d0 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65  entations (all e
c63e0 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b  xcept proxy lock
c63f0 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20  ing):.**      + 
c6400 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 6f  for Posix Adviso
c6410 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20  ry Locks.**     
c6420 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63   + for no-op loc
c6430 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72  ks.**      + for
c6440 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a   dot-file locks.
c6450 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c  **      + for fl
c6460 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a  ock() locking.**
c6470 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65        + for name
c6480 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b  d semaphore lock
c6490 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29  s (VxWorks only)
c64a0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 41  .**      + for A
c64b0 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f  FP filesystem lo
c64c0 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79  cks (MacOSX only
c64d0 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65  ).**   *  sqlite
c64e0 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e  3_file methods n
c64f0 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ot associated wi
c6500 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20  th locking..**  
c6510 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20   *  Definitions 
c6520 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  of sqlite3_io_me
c6530 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f  thods objects fo
c6540 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a  r all locking.**
c6550 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 6c        methods pl
c6560 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63  us "finder" func
c6570 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 6c  tions for each l
c6580 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a  ocking method..*
c6590 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 76  *   *  sqlite3_v
c65a0 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d  fs method implem
c65b0 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20  entations..**   
c65c0 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69  *  Locking primi
c65d0 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 72  tives for the pr
c65e0 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67  oxy uber-locking
c65f0 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58  -method. (MacOSX
c6600 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44   only).**   *  D
c6610 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71  efinitions of sq
c6620 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74  lite3_vfs object
c6630 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e  s for all lockin
c6640 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20  g methods.**    
c6650 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74    plus implement
c6660 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65  ations of sqlite
c6670 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20  3_os_init() and 
c6680 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29  sqlite3_os_end()
c6690 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
c66a0 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 20  OS_UNIX         
c66b0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c       /* This fil
c66c0 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e 69  e is used on uni
c66d0 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  x only */../*.**
c66e0 20 54 68 65 72 65 20 61 72 65 20 76 61 72 69 6f   There are vario
c66f0 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 66  us methods for f
c6700 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64  ile locking used
c6710 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 79   for concurrency
c6720 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a  .** control:.**.
c6730 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c 6f  **   1. POSIX lo
c6740 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 75  cking (the defau
c6750 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20  lt),.**   2. No 
c6760 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e  locking,.**   3.
c6770 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e   Dot-file lockin
c6780 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 6b  g,.**   4. flock
c6790 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20  () locking,.**  
c67a0 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20   5. AFP locking 
c67b0 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20  (OSX only),.**  
c67c0 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58 20   6. Named POSIX 
c67d0 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57 6f  semaphores (VXWo
c67e0 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20  rks only),.**   
c67f0 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  7. proxy locking
c6800 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a  . (OSX only).**.
c6810 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c 20  ** Styles 4, 5, 
c6820 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20 61  and 7 are only a
c6830 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c 49  vailable of SQLI
c6840 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
c6850 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64 65  G_STYLE.** is de
c6860 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68 65  fined to 1.  The
c6870 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
c6880 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c 73  OCKING_STYLE als
c6890 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d 61  o enables automa
c68a0 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f 6e  tic.** selection
c68b0 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72 69   of the appropri
c68c0 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c  ate locking styl
c68d0 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66  e based on the f
c68e0 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68 65  ilesystem.** whe
c68f0 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
c6900 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a 2f  is located.  .*/
c6910 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
c6920 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
c6930 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66  ING_STYLE).#  if
c6940 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
c6950 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20  __).#    define 
c6960 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
c6970 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20  CKING_STYLE 1.# 
c6980 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e   else.#    defin
c6990 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
c69a0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a  LOCKING_STYLE 0.
c69b0 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  #  endif.#endif.
c69c0 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68  ./*.** Define th
c69d0 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72 65  e OS_VXWORKS pre
c69e0 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f  -processor macro
c69f0 20 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69 6e   to 1 if buildin
c6a00 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b 73  g on .** vxworks
c6a10 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65  , or 0 otherwise
c6a20 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53 5f  ..*/.#ifndef OS_
c6a30 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64 65  VXWORKS.#  if de
c6a40 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20 7c  fined(__RTP__) |
c6a50 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f 4b  | defined(_WRS_K
c6a60 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66 69  ERNEL).#    defi
c6a70 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31 0a  ne OS_VXWORKS 1.
c6a80 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66  #  else.#    def
c6a90 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30  ine OS_VXWORKS 0
c6aa0 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  .#  endif.#endif
c6ab0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
c6ac0 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
c6ad0 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
c6ae0 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20  upport on Posix 
c6af0 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
c6b00 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
c6b10 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
c6b20 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
c6b30 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
c6b40 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 65  e support, these
c6b50 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70   should be no-op
c6b60 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66  s..**.** Large f
c6b70 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20  ile support can 
c6b80 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e  be disabled usin
c6b90 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44  g the -DSQLITE_D
c6ba0 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63  ISABLE_LFS switc
c6bb0 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70  h.** on the comp
c6bc0 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e  iler command lin
c6bd0 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65  e.  This is nece
c6be0 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 65  ssary if you are
c6bf0 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e   compiling.** on
c6c00 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e   a recent machin
c6c10 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e  e (ex: RedHat 7.
c6c20 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20  2) but you want 
c6c30 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72  your code to wor
c6c40 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72  k.** on an older
c6c50 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65   machine (ex: Re
c6c60 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79  dHat 6.0).  If y
c6c70 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65  ou compile on Re
c6c80 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68  dHat 7.2.** with
c6c90 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c  out this option,
c6ca0 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20   LFS is enable. 
c6cb0 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f   But LFS does no
c6cc0 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b  t exist in the k
c6cd0 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48  ernel.** in RedH
c6ce0 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63  at 6.0, so the c
c6cf0 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20  ode won't work. 
c6d00 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69   Hence, for maxi
c6d10 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f  mum binary.** po
c6d20 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68  rtability you sh
c6d30 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a  ould omit LFS..*
c6d40 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f 75  *.** The previou
c6d50 73 20 70 61 72 61 67 72 61 70 68 20 77 61 73 20  s paragraph was 
c6d60 77 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 2e  written in 2005.
c6d70 20 20 28 54 68 69 73 20 70 61 72 61 67 72 61 70    (This paragrap
c6d80 68 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  h is written.** 
c6d90 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 20  on 2008-11-28.) 
c6da0 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c 20  These days, all 
c6db0 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 75  Linux kernels su
c6dc0 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c 65  pport large file
c6dd0 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f  s, so.** you sho
c6de0 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65 61  uld probably lea
c6df0 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e 20  ve LFS enabled. 
c6e00 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64 64   But some embedd
c6e10 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69 67  ed platforms mig
c6e20 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 69  ht.** lack LFS i
c6e30 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
c6e40 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f   SQLITE_DISABLE_
c6e50 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74 20  LFS macro might 
c6e60 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2e  still be useful.
c6e70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
c6e80 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23  TE_DISABLE_LFS.#
c6e90 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46   define _LARGE_F
c6ea0 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 66  ILE       1.# if
c6eb0 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45  ndef _FILE_OFFSE
c6ec0 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e  T_BITS.#   defin
c6ed0 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42  e _FILE_OFFSET_B
c6ee0 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23  ITS 64.# endif.#
c6ef0 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49   define _LARGEFI
c6f00 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64  LE_SOURCE 1.#end
c6f10 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61  if../*.** standa
c6f20 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73  rd include files
c6f30 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73  ..*/.#include <s
c6f40 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63  ys/types.h>.#inc
c6f50 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68  lude <sys/stat.h
c6f60 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74  >.#include <fcnt
c6f70 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75  l.h>.#include <u
c6f80 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64  nistd.h>.#includ
c6f90 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23  e <sys/time.h>.#
c6fa0 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68  include <errno.h
c6fb0 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  >..#if SQLITE_EN
c6fc0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
c6fd0 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79  LE.# include <sy
c6fe0 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 20  s/ioctl.h>.# if 
c6ff0 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 6e  OS_VXWORKS.#  in
c7000 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72 65  clude <semaphore
c7010 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c  .h>.#  include <
c7020 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73 65  limits.h>.# else
c7030 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73  .#  include <sys
c7040 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 6c  /file.h>.#  incl
c7050 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68  ude <sys/param.h
c7060 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79  >.#  include <sy
c7070 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64  s/mount.h>.# end
c7080 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  if.#endif /* SQL
c7090 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
c70a0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a  NG_STYLE */../*.
c70b0 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f 20  ** If we are to 
c70c0 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c 20  be thread-safe, 
c70d0 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 72  include the pthr
c70e0 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 20  eads header and 
c70f0 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51  define.** the SQ
c7100 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44  LITE_UNIX_THREAD
c7110 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20  S macro..*/.#if 
c7120 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
c7130 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
c7140 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 31  E_UNIX_THREADS 1
c7150 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44  .#endif../*.** D
c7160 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f  efault permissio
c7170 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67  ns when creating
c7180 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23   a new file.*/.#
c7190 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
c71a0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
c71b0 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20  SSIONS.# define 
c71c0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
c71d0 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20  ILE_PERMISSIONS 
c71e0 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0644.#endif../*.
c71f0 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d   ** Default perm
c7200 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65  issions when cre
c7210 61 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 79  ating auto proxy
c7220 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 66   dir. */.#ifndef
c7230 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
c7240 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53  PROXYDIR_PERMISS
c7250 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51  IONS.# define SQ
c7260 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f  LITE_DEFAULT_PRO
c7270 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e  XYDIR_PERMISSION
c7280 53 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a 2f  S 0755.#endif../
c7290 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70  *.** Maximum sup
c72a0 70 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e 67  ported path-leng
c72b0 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  th..*/.#define M
c72c0 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 0a  AX_PATHNAME 512.
c72d0 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 20  ./*.** Only set 
c72e0 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69 66  the lastErrno if
c72f0 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
c7300 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72 20  is a real error 
c7310 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e 6f  and not .** a no
c7320 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72 65  rmal expected re
c7330 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c  turn code of SQL
c7340 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c 49  ITE_BUSY or SQLI
c7350 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e 65  TE_OK.*/.#define
c7360 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78   IS_LOCK_ERROR(x
c7370 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54 45  )  ((x != SQLITE
c7380 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53 51  _OK) && (x != SQ
c7390 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f 2a  LITE_BUSY)).../*
c73a0 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20 61  .** Sometimes, a
c73b0 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e 64  fter a file hand
c73c0 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79 20  le is closed by 
c73d0 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c 65  SQLite, the file
c73e0 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 63   descriptor.** c
c73f0 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20  annot be closed 
c7400 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 20  immediately. In 
c7410 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e 73  these cases, ins
c7420 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66 6f  tances of the fo
c7430 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
c7440 74 75 72 65 20 61 72 65 20 75 73 65 64 20 74 6f  ture are used to
c7450 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 20   store the file 
c7460 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c 65  descriptor while
c7470 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e 0a   waiting for an.
c7480 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74  ** opportunity t
c7490 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20 6f  o either close o
c74a0 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a 74  r reuse it..*/.t
c74b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e  ypedef struct Un
c74c0 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78 55  ixUnusedFd UnixU
c74d0 6e 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74 20  nusedFd;.struct 
c74e0 55 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a 20  UnixUnusedFd {. 
c74f0 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20   int fd;        
c7500 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
c7510 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
c7520 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20   close */.  int 
c7530 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
c7540 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74        /* Flags t
c7550 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69 70  his file descrip
c7560 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 77  tor was opened w
c7570 69 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75  ith */.  UnixUnu
c7580 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20 20  sedFd *pNext;   
c7590 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65     /* Next unuse
c75a0 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
c75b0 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a  r on same file *
c75c0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
c75d0 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
c75e0 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f  re is subclass o
c75f0 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73  f sqlite3_file s
c7600 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 75  pecific to the u
c7610 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65  nix.** VFS imple
c7620 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74  mentations..*/.t
c7630 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e  ypedef struct un
c7640 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b  ixFile unixFile;
c7650 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65  .struct unixFile
c7660 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f   {.  sqlite3_io_
c7670 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70  methods const *p
c7680 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61  Method;  /* Alwa
c7690 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74  ys the first ent
c76a0 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  ry */.  struct u
c76b0 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65  nixOpenCnt *pOpe
c76c0 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  n;       /* Info
c76d0 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20   about all open 
c76e0 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f  fd's on this ino
c76f0 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  de */.  struct u
c7700 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f  nixLockInfo *pLo
c7710 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  ck;      /* Info
c7720 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20   about locks on 
c7730 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20  this inode */.  
c7740 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
c7750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7760 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73   /* The file des
c7770 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  criptor */.  int
c7780 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 20   dirfd;         
c7790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c77a0 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   File descriptor
c77b0 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f   for the directo
c77c0 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ry */.  unsigned
c77d0 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20   char locktype; 
c77e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
c77f0 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c  type of lock hel
c7800 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a  d on this fd */.
c7810 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b    int lastErrno;
c7820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7830 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65     /* The unix e
c7840 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61  rrno from the la
c7850 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a  st I/O error */.
c7860 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43    void *lockingC
c7870 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20  ontext;         
c7880 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74     /* Locking st
c7890 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61  yle specific sta
c78a0 74 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73  te */.  UnixUnus
c78b0 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20  edFd *pUnused;  
c78c0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 2d           /* Pre-
c78d0 61 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 6e  allocated UnixUn
c78e0 75 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74 20  usedFd */.  int 
c78f0 66 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20 20  fileFlags;      
c7900 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c7910 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61  Miscellanous fla
c7920 67 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  gs */.#if SQLITE
c7930 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
c7940 53 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e  STYLE.  int open
c7950 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
c7960 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
c7970 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 20  flags specified 
c7980 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e  at open() */.#en
c7990 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  dif.#if SQLITE_T
c79a0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
c79b0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
c79c0 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b    pthread_t tid;
c79d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c79e0 20 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64     /* The thread
c79f0 20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69   that "owns" thi
c7a00 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65  s unixFile */.#e
c7a10 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f  ndif.#if OS_VXWO
c7a20 52 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65  RKS.  int isDele
c7a30 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
c7a40 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65         /* Delete
c7a50 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 75   on close if tru
c7a60 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78  e */.  struct vx
c7a70 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64  worksFileId *pId
c7a80 3b 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75  ;       /* Uniqu
c7a90 65 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e  e file ID */.#en
c7aa0 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  dif.#ifndef NDEB
c7ab0 55 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74  UG.  /* The next
c7ac0 20 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62   group of variab
c7ad0 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  les are used to 
c7ae0 74 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72  track whether or
c7af0 20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72   not the.  ** tr
c7b00 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65  ansaction counte
c7b10 72 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37  r in bytes 24-27
c7b20 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
c7b30 65 73 20 61 72 65 20 75 70 64 61 74 65 64 0a 20  es are updated. 
c7b40 20 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79   ** whenever any
c7b50 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
c7b60 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20  abase changes.  
c7b70 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75  An assertion fau
c7b80 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63  lt will.  ** occ
c7b90 75 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 20  ur if a file is 
c7ba0 75 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 20  updated without 
c7bb0 61 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68  also updating th
c7bc0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  e transaction.  
c7bd0 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69  ** counter.  Thi
c7be0 73 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 74  s test is made t
c7bf0 6f 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62  o avoid new prob
c7c00 6c 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20  lems similar to 
c7c10 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73  the.  ** one des
c7c20 63 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 74  cribed by ticket
c7c30 20 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20   #3584. .  */.  
c7c40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 72  unsigned char tr
c7c50 61 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f  ansCntrChng;   /
c7c60 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 74 72  * True if the tr
c7c70 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65  ansaction counte
c7c80 72 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75  r changed */.  u
c7c90 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 55  nsigned char dbU
c7ca0 70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a  pdate;        /*
c7cb0 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 72   True if any par
c7cc0 74 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  t of database fi
c7cd0 6c 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20  le changed */.  
c7ce0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e  unsigned char in
c7cf0 4e 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f  NormalWrite;   /
c7d00 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e  * True if in a n
c7d10 6f 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72  ormal write oper
c7d20 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a  ation */.#endif.
c7d30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
c7d40 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20  ST.  /* In test 
c7d50 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74  mode, increase t
c7d60 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20  he size of this 
c7d70 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20  structure a bit 
c7d80 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74  so that .  ** it
c7d90 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
c7da0 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68  the struct Crash
c7db0 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20  File defined in 
c7dc0 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20  test6.c..  */.  
c7dd0 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32  char aPadding[32
c7de0 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  ];.#endif.};../*
c7df0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
c7e00 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20  g macros define 
c7e10 62 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c 65  bits in unixFile
c7e20 2e 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 64  .fileFlags.*/.#d
c7e30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48 4f  efine SQLITE_WHO
c7e40 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20  LE_FILE_LOCKING 
c7e50 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73 65   0x0001   /* Use
c7e60 20 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 6b   whole-file lock
c7e70 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ing */../*.** In
c7e80 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20  clude code that 
c7e90 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  is common to all
c7ea0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f   os_*.c files.*/
c7eb0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c7ec0 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d   Include os_comm
c7ed0 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  on.h in the midd
c7ee0 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20  le of os_unix.c 
c7ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c7f00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c7f10 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63   Begin file os_c
c7f20 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
c7f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c7f50 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
c7f60 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
c7f70 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
c7f80 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
c7f90 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
c7fa0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
c7fb0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
c7fc0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
c7fd0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
c7fe0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
c7ff0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
c8000 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
c8010 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
c8020 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
c8030 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
c8040 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
c8050 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
c8060 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
c8070 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
c8080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c80a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c80b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c80c0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
c80d0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
c80e0 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74  macros and a lit
c80f0 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20  tle bit of code 
c8100 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
c8110 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20  o.** all of the 
c8120 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69  platform-specifi
c8130 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29  c files (os_*.c)
c8140 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65   and is #include
c8150 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20  d into those.** 
c8160 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  files..**.** Thi
c8170 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  s file should be
c8180 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68   #included by th
c8190 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f  e os_*.c files o
c81a0 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  nly.  It is not 
c81b0 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72  a.** general pur
c81c0 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65  pose header file
c81d0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53  ..*/.#ifndef _OS
c81e0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69  _COMMON_H_.#defi
c81f0 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ne _OS_COMMON_H_
c8200 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74  ../*.** At least
c8210 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73   two bugs have s
c8220 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73  lipped in becaus
c8230 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65  e we changed the
c8240 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a   MEMORY_DEBUG.**
c8250 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45   macro to SQLITE
c8260 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20  _DEBUG and some 
c8270 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20  older makefiles 
c8280 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64  have not yet mad
c8290 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e  e the.** switch.
c82a0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
c82b0 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63  code should catc
c82c0 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61  h this problem a
c82d0 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a  t compile-time..
c82e0 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59  */.#ifdef MEMORY
c82f0 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22  _DEBUG.# error "
c8300 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  The MEMORY_DEBUG
c8310 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65   macro is obsole
c8320 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f  te.  Use SQLITE_
c8330 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a  DEBUG instead.".
c8340 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
c8350 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
c8360 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
c8370 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20  qlite3OSTrace = 
c8380 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  0;.#define OSTRA
c8390 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69  CE1(X)         i
c83a0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
c83b0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
c83c0 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e  Printf(X).#defin
c83d0 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20  e OSTRACE2(X,Y) 
c83e0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
c83f0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
c8400 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
c8410 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Y).#define OSTRA
c8420 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69  CE3(X,Y,Z)     i
c8430 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
c8440 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
c8450 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64  Printf(X,Y,Z).#d
c8460 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58  efine OSTRACE4(X
c8470 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71  ,Y,Z,A)   if( sq
c8480 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
c8490 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c84a0 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69  f(X,Y,Z,A).#defi
c84b0 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c  ne OSTRACE5(X,Y,
c84c0 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74  Z,A,B) if( sqlit
c84d0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c84e0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c84f0 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e  ,Y,Z,A,B).#defin
c8500 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a  e OSTRACE6(X,Y,Z
c8510 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66  ,A,B,C) \.    if
c8520 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29  (sqlite3OSTrace)
c8530 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
c8540 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29  ntf(X,Y,Z,A,B,C)
c8550 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c8560 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  7(X,Y,Z,A,B,C,D)
c8570 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65   \.    if(sqlite
c8580 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65  3OSTrace) sqlite
c8590 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
c85a0 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73  ,Z,A,B,C,D).#els
c85b0 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  e.#define OSTRAC
c85c0 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53  E1(X).#define OS
c85d0 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66  TRACE2(X,Y).#def
c85e0 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59  ine OSTRACE3(X,Y
c85f0 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Z).#define OSTR
c8600 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64  ACE4(X,Y,Z,A).#d
c8610 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58  efine OSTRACE5(X
c8620 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e  ,Y,Z,A,B).#defin
c8630 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a  e OSTRACE6(X,Y,Z
c8640 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20  ,A,B,C).#define 
c8650 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41  OSTRACE7(X,Y,Z,A
c8660 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a  ,B,C,D).#endif..
c8670 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72  /*.** Macros for
c8680 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61   performance tra
c8690 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20  cing.  Normally 
c86a0 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c  turned off.  Onl
c86b0 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34  y works.** on i4
c86c0 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a  86 hardware..*/.
c86d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45  #ifdef SQLITE_PE
c86e0 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a  RFORMANCE_TRACE.
c86f0 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68  ./* .** hwtime.h
c8700 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
c8710 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20   assembler code 
c8720 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  for implementing
c8730 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72   .** high-perfor
c8740 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75  mance timing rou
c8750 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  tines..*/./*****
c8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
c8770 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68  e hwtime.h in th
c8780 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63  e middle of os_c
c8790 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
c87a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
c87b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
c87c0 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a  file hwtime.h **
c87d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c87e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c87f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
c8800 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a  2008 May 27.**.*
c8810 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
c8820 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
c8830 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
c8840 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
c8850 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
c8860 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
c8870 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
c8880 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
c8890 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
c88a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
c88b0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
c88c0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
c88d0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
c88e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
c88f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
c8900 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
c8910 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
c8920 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
c8930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
c8970 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
c8980 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20  contains inline 
c8990 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74  asm code for ret
c89a0 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65  rieving "high-pe
c89b0 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f  rformance".** co
c89c0 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63  unters for x86 c
c89d0 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a 23 69  lass CPUs..*/.#i
c89e0 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f  fndef _HWTIME_H_
c89f0 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45  .#define _HWTIME
c8a00 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  _H_../*.** The f
c8a10 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
c8a20 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70   only works on p
c8a30 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72  entium-class (or
c8a40 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f   newer) processo
c8a50 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74  rs..** It uses t
c8a60 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20  he RDTSC opcode 
c8a70 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c  to read the cycl
c8a80 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75  e count value ou
c8a90 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63  t of the.** proc
c8aa0 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e  essor and return
c8ab0 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54  s that value.  T
c8ac0 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
c8ad0 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20  for high-res.** 
c8ae0 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69  profiling..*/.#i
c8af0 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  f (defined(__GNU
c8b00 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  C__) || defined(
c8b10 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a  _MSC_VER)) && \.
c8b20 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69        (defined(i
c8b30 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  386) || defined(
c8b40 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66  __i386__) || def
c8b50 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a  ined(_M_IX86))..
c8b60 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f    #if defined(__
c8b70 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c  GNUC__)..  __inl
c8b80 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
c8b90 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
c8ba0 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e  e(void){.     un
c8bb0 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68  signed int lo, h
c8bc0 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  i;.     __asm__ 
c8bd0 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72  __volatile__ ("r
c8be0 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f  dtsc" : "=a" (lo
c8bf0 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20  ), "=d" (hi));. 
c8c00 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69      return (sqli
c8c10 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20  te_uint64)hi << 
c8c20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20  32 | lo;.  }..  
c8c30 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d  #elif defined(_M
c8c40 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63  SC_VER)..  __dec
c8c50 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69  lspec(naked) __i
c8c60 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e  nline sqlite_uin
c8c70 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69  t64 __cdecl sqli
c8c80 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
c8c90 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20  .     __asm {.  
c8ca0 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20        rdtsc.    
c8cb0 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20      ret       ; 
c8cc0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20  return value at 
c8cd0 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20  EDX:EAX.     }. 
c8ce0 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65   }..  #endif..#e
c8cf0 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  lif (defined(__G
c8d00 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65  NUC__) && define
c8d10 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a  d(__x86_64__))..
c8d20 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
c8d30 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
c8d40 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
c8d50 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c        unsigned l
c8d60 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f  ong val;.      _
c8d70 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
c8d80 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22  e__ ("rdtsc" : "
c8d90 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20  =A" (val));.    
c8da0 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20    return val;.  
c8db0 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  }. .#elif (defin
c8dc0 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
c8dd0 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29  defined(__ppc__)
c8de0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  )..  __inline__ 
c8df0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
c8e00 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
c8e10 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
c8e20 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76  d long long retv
c8e30 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  al;.      unsign
c8e40 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20  ed long junk;.  
c8e50 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
c8e60 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20  latile__ ("\n\. 
c8e70 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20           1:     
c8e80 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20   mftbu   %1\n\. 
c8e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c8ea0 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a   mftb    %L0\n\.
c8eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c8ec0 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a    mftbu   %0\n\.
c8ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c8ee0 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c    cmpw    %0,%1\
c8ef0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
c8f00 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22       bne     1b"
c8f10 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c8f20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61     : "=r" (retva
c8f30 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29  l), "=r" (junk))
c8f40 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
c8f50 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73  etval;.  }..#els
c8f60 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64  e..  #error Need
c8f70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
c8f80 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  of sqlite3Hwtime
c8f90 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
c8fa0 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  form...  /*.  **
c8fb0 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68   To compile with
c8fc0 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  out implementing
c8fd0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
c8fe0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f   for your platfo
c8ff0 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e  rm,.  ** you can
c9000 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76   remove the abov
c9010 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65  e #error and use
c9020 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
c9030 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f   ** stub functio
c9040 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73  n.  You will los
c9050 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74  e timing support
c9060 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f   for many.  ** o
c9070 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20  f the debugging 
c9080 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c  and testing util
c9090 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68  ities, but it sh
c90a0 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61  ould at.  ** lea
c90b0 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72  st compile and r
c90c0 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f  un..  */.SQLITE_
c90d0 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65  PRIVATE   sqlite
c90e0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
c90f0 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74  wtime(void){ ret
c9100 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e  urn ((sqlite_uin
c9110 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69  t64)0); }..#endi
c9120 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
c9130 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f  fined(_HWTIME_H_
c9140 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
c9150 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74  ***** End of hwt
c9160 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
c9170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9190 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
c91a0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
c91b0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
c91c0 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  ff in os_common.
c91d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
c91e0 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71  ****/..static sq
c91f0 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74  lite_uint64 g_st
c9200 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69  art;.static sqli
c9210 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70  te_uint64 g_elap
c9220 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d  sed;.#define TIM
c9230 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67  ER_START       g
c9240 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77  _start=sqlite3Hw
c9250 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54  time().#define T
c9260 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20  IMER_END        
c9270 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74   g_elapsed=sqlit
c9280 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61  e3Hwtime()-g_sta
c9290 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  rt.#define TIMER
c92a0 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65  _ELAPSED     g_e
c92b0 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65  lapsed.#else.#de
c92c0 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54  fine TIMER_START
c92d0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
c92e0 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  ND.#define TIMER
c92f0 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73  _ELAPSED     ((s
c9300 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a  qlite_uint64)0).
c9310 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
c9320 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68   we compile with
c9330 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54   the SQLITE_TEST
c9340 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e   macro set, then
c9350 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   the following b
c9360 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20  lock.** of code 
c9370 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65  will give us the
c9380 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75   ability to simu
c9390 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20  late a disk I/O 
c93a0 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20  error.  This.** 
c93b0 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
c93c0 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f  ing the I/O reco
c93d0 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23  very logic..*/.#
c93e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
c93f0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
c9400 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c9410 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20  r_hit = 0;      
c9420 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
c9430 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72  umber of I/O Err
c9440 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ors */.SQLITE_AP
c9450 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
c9460 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d  _error_hardhit =
c9470 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75   0;        /* Nu
c9480 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69  mber of non-beni
c9490 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c  gn errors */.SQL
c94a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c94b0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
c94c0 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20  ding = 0;       
c94d0 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74   /* Count down t
c94e0 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f  o first I/O erro
c94f0 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  r */.SQLITE_API 
c9500 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
c9510 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30  rror_persist = 0
c9520 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
c9530 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70   if I/O errors p
c9540 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45  ersist */.SQLITE
c9550 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
c9560 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e  _io_error_benign
c9570 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
c9580 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20   True if errors 
c9590 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51  are benign */.SQ
c95a0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
c95b0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
c95c0 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54  nding = 0;.SQLIT
c95d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c95e0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a  3_diskfull = 0;.
c95f0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
c9600 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29  IOErrorBenign(X)
c9610 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c9620 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65  r_benign=(X).#de
c9630 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
c9640 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20  rror(CODE)  \.  
c9650 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f  if( (sqlite3_io_
c9660 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26  error_persist &&
c9670 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c9680 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20  r_hit) \.       
c9690 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  || sqlite3_io_er
c96a0 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d  ror_pending-- ==
c96b0 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20   1 )  \.        
c96c0 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f        { local_io
c96d0 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73  err(); CODE; }.s
c96e0 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c  tatic void local
c96f0 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52  _ioerr(){.  IOTR
c9700 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29  ACE(("IOERR\n"))
c9710 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  ;.  sqlite3_io_e
c9720 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66  rror_hit++;.  if
c9730 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  ( !sqlite3_io_er
c9740 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c  ror_benign ) sql
c9750 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61  ite3_io_error_ha
c9760 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69  rdhit++;.}.#defi
c9770 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  ne SimulateDiskf
c9780 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c  ullError(CODE) \
c9790 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  .   if( sqlite3_
c97a0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
c97b0 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73   ){ \.     if( s
c97c0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
c97d0 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20  pending == 1 ){ 
c97e0 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69  \.       local_i
c97f0 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20  oerr(); \.      
c9800 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
c9810 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20  l = 1; \.       
c9820 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c9830 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20  _hit = 1; \.    
c9840 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20     CODE; \.     
c9850 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20  }else{ \.       
c9860 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
c9870 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20  _pending--; \.  
c9880 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73     } \.   }.#els
c9890 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  e.#define Simula
c98a0 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
c98b0 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  X).#define Simul
c98c0 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64  ateIOError(A).#d
c98d0 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69  efine SimulateDi
c98e0 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23  skfullError(A).#
c98f0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  endif../*.** Whe
c9900 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20  n testing, keep 
c9910 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e  a count of the n
c9920 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69  umber of open fi
c9930 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  les..*/.#ifdef S
c9940 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
c9950 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c9960 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e  3_open_file_coun
c9970 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f  t = 0;.#define O
c9980 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73  penCounter(X)  s
c9990 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65  qlite3_open_file
c99a0 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73  _count+=(X).#els
c99b0 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f  e.#define OpenCo
c99c0 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a  unter(X).#endif.
c99d0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
c99e0 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ned(_OS_COMMON_H
c99f0 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _) */../********
c9a00 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
c9a10 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
c9a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9a40 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
c9a50 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
c9a60 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
c9a70 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63  off in os_unix.c
c9a80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c9a90 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65  *****/../*.** De
c9aa0 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63  fine various mac
c9ab0 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 73  ros that are mis
c9ac0 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73  sing from some s
c9ad0 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64  ystems..*/.#ifnd
c9ae0 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23  ef O_LARGEFILE.#
c9af0 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46   define O_LARGEF
c9b00 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66  ILE 0.#endif.#if
c9b10 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42  def SQLITE_DISAB
c9b20 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f  LE_LFS.# undef O
c9b30 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66  _LARGEFILE.# def
c9b40 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20  ine O_LARGEFILE 
c9b50 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  0.#endif.#ifndef
c9b60 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65   O_NOFOLLOW.# de
c9b70 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20  fine O_NOFOLLOW 
c9b80 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  0.#endif.#ifndef
c9b90 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69   O_BINARY.# defi
c9ba0 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65  ne O_BINARY 0.#e
c9bb0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
c9bc0 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65  DJGPP compiler e
c9bd0 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73  nvironment looks
c9be0 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69   mostly like Uni
c9bf0 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63  x, but it.** lac
c9c00 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73  ks the fcntl() s
c9c10 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20  ystem call.  So 
c9c20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29  redefine fcntl()
c9c30 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67   to be something
c9c40 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20  .** that always 
c9c50 73 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20  succeeds.  This 
c9c60 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69  means that locki
c9c70 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75  ng does not occu
c9c80 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50  r under.** DJGPP
c9c90 2e 20 20 42 75 74 20 69 74 20 69 73 20 44 4f 53  .  But it is DOS
c9ca0 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 20   - what did you 
c9cb0 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65  expect?.*/.#ifde
c9cc0 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65  f __DJGPP__.# de
c9cd0 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43  fine fcntl(A,B,C
c9ce0 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ) 0.#endif../*.*
c9cf0 2a 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d  * The threadid m
c9d00 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f  acro resolves to
c9d10 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f   the thread-id o
c9d20 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f  r to 0.  Used fo
c9d30 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64  r.** testing and
c9d40 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
c9d50 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  .*/.#if SQLITE_T
c9d60 48 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e  HREADSAFE.#defin
c9d70 65 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65  e threadid pthre
c9d80 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a  ad_self().#else.
c9d90 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64  #define threadid
c9da0 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a   0.#endif.../*.*
c9db0 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f  * Helper functio
c9dc0 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64  ns to obtain and
c9dd0 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20   relinquish the 
c9de0 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20 54 68  global mutex. Th
c9df0 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 65  e.** global mute
c9e00 78 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f  x is used to pro
c9e10 74 65 63 74 20 74 68 65 20 75 6e 69 78 4f 70 65  tect the unixOpe
c9e20 6e 43 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 6e  nCnt, unixLockIn
c9e30 66 6f 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b  fo and.** vxwork
c9e40 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 20  sFileId objects 
c9e50 75 73 65 64 20 62 79 20 74 68 69 73 20 66 69 6c  used by this fil
c9e60 65 2c 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 20  e, all of which 
c9e70 6d 61 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 65  may be .** share
c9e80 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68  d by multiple th
c9e90 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e  reads..**.** Fun
c9ea0 63 74 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48  ction unixMutexH
c9eb0 65 6c 64 28 29 20 69 73 20 75 73 65 64 20 74 6f  eld() is used to
c9ec0 20 61 73 73 65 72 74 28 29 20 74 68 61 74 20 74   assert() that t
c9ed0 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20  he global mutex 
c9ee0 0a 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e  .** is held when
c9ef0 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20   required. This 
c9f00 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  function is only
c9f10 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66   used as part of
c9f20 20 61 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 74   assert() .** st
c9f30 61 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a  atements. e.g..*
c9f40 2a 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72  *.**   unixEnter
c9f50 4d 75 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 61  Mutex().**     a
c9f60 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78  ssert( unixMutex
c9f70 48 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75  Held() );.**   u
c9f80 6e 69 78 45 6e 74 65 72 4c 65 61 76 65 28 29 0a  nixEnterLeave().
c9f90 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
c9fa0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f  nixEnterMutex(vo
c9fb0 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id){.  sqlite3_m
c9fc0 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
c9fd0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
c9fe0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
c9ff0 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61  _MASTER));.}.sta
ca000 74 69 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61  tic void unixLea
ca010 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20  veMutex(void){. 
ca020 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
ca030 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65  eave(sqlite3Mute
ca040 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
ca050 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
ca060 52 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51  R));.}.#ifdef SQ
ca070 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69  LITE_DEBUG.stati
ca080 63 20 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 48  c int unixMutexH
ca090 65 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 65  eld(void) {.  re
ca0a0 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74  turn sqlite3_mut
ca0b0 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d  ex_held(sqlite3M
ca0c0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
ca0d0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
ca0e0 53 54 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66  STER));.}.#endif
ca0f0 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
ca100 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c  _DEBUG./*.** Hel
ca110 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  per function for
ca120 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 72   printing out tr
ca130 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ace information 
ca140 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a  from debugging.*
ca150 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 69 73  * binaries. This
ca160 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 74 72   returns the str
ca170 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 69 6f  ing represetatio
ca180 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65  n of the supplie
ca190 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63  d.** integer loc
ca1a0 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69  k-type..*/.stati
ca1b0 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f  c const char *lo
ca1c0 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c  cktypeName(int l
ca1d0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 74  ocktype){.  swit
ca1e0 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  ch( locktype ){.
ca1f0 20 20 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b      case NO_LOCK
ca200 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b  : return "NONE";
ca210 0a 20 20 20 20 63 61 73 65 20 53 48 41 52 45 44  .    case SHARED
ca220 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53  _LOCK: return "S
ca230 48 41 52 45 44 22 3b 0a 20 20 20 20 63 61 73 65  HARED";.    case
ca240 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20   RESERVED_LOCK: 
ca250 72 65 74 75 72 6e 20 22 52 45 53 45 52 56 45 44  return "RESERVED
ca260 22 3b 0a 20 20 20 20 63 61 73 65 20 50 45 4e 44  ";.    case PEND
ca270 49 4e 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e  ING_LOCK: return
ca280 20 22 50 45 4e 44 49 4e 47 22 3b 0a 20 20 20 20   "PENDING";.    
ca290 63 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c  case EXCLUSIVE_L
ca2a0 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 45 58 43  OCK: return "EXC
ca2b0 4c 55 53 49 56 45 22 3b 0a 20 20 7d 0a 20 20 72  LUSIVE";.  }.  r
ca2c0 65 74 75 72 6e 20 22 45 52 52 4f 52 22 3b 0a 7d  eturn "ERROR";.}
ca2d0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
ca2e0 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43  SQLITE_LOCK_TRAC
ca2f0 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75  E./*.** Print ou
ca300 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  t information ab
ca310 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20  out all locking 
ca320 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  operations..**.*
ca330 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
ca340 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62  s used for troub
ca350 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73  leshooting locks
ca360 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65   on multithreade
ca370 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20  d.** platforms. 
ca380 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69   Enable by compi
ca390 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44  ling with the -D
ca3a0 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43  SQLITE_LOCK_TRAC
ca3b0 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e  E.** command-lin
ca3c0 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20  e option on the 
ca3d0 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20  compiler.  This 
ca3e0 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79  code is normally
ca3f0 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a  .** turned off..
ca400 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f  */.static int lo
ca410 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20  ckTrace(int fd, 
ca420 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66  int op, struct f
ca430 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72  lock *p){.  char
ca440 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70   *zOpName, *zTyp
ca450 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e  e;.  int s;.  in
ca460 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20  t savedErrno;.  
ca470 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20  if( op==F_GETLK 
ca480 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d  ){.    zOpName =
ca490 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73   "GETLK";.  }els
ca4a0 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c  e if( op==F_SETL
ca4b0 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65  K ){.    zOpName
ca4c0 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65   = "SETLK";.  }e
ca4d0 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e  lse{.    s = fcn
ca4e0 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20  tl(fd, op, p);. 
ca4f0 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
ca500 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b  rintf("fcntl unk
ca510 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22  nown %d %d %d\n"
ca520 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20  , fd, op, s);.  
ca530 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a    return s;.  }.
ca540 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d    if( p->l_type=
ca550 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_RDLCK ){.    
ca560 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b  zType = "RDLCK";
ca570 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
ca580 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20  l_type==F_WRLCK 
ca590 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22  ){.    zType = "
ca5a0 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20  WRLCK";.  }else 
ca5b0 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46  if( p->l_type==F
ca5c0 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54  _UNLCK ){.    zT
ca5d0 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20  ype = "UNLCK";. 
ca5e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
ca5f0 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61  rt( 0 );.  }.  a
ca600 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e  ssert( p->l_when
ca610 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a  ce==SEEK_SET );.
ca620 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20    s = fcntl(fd, 
ca630 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45  op, p);.  savedE
ca640 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
ca650 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
ca660 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20  tf("fcntl %d %d 
ca670 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25  %s %s %d %d %d %
ca680 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61  d\n",.     threa
ca690 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65  did, fd, zOpName
ca6a0 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d  , zType, (int)p-
ca6b0 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70  >l_start, (int)p
ca6c0 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69  ->l_len,.     (i
ca6d0 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b  nt)p->l_pid, s);
ca6e0 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26  .  if( s==(-1) &
ca6f0 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26  & op==F_SETLK &&
ca700 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52   (p->l_type==F_R
ca710 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70  DLCK || p->l_typ
ca720 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20  e==F_WRLCK) ){. 
ca730 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20     struct flock 
ca740 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b  l2;.    l2 = *p;
ca750 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46  .    fcntl(fd, F
ca760 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20  _GETLK, &l2);.  
ca770 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d    if( l2.l_type=
ca780 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_RDLCK ){.    
ca790 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b    zType = "RDLCK
ca7a0 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ";.    }else if(
ca7b0 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52   l2.l_type==F_WR
ca7c0 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79  LCK ){.      zTy
ca7d0 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20  pe = "WRLCK";.  
ca7e0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c    }else if( l2.l
ca7f0 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29  _type==F_UNLCK )
ca800 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20  {.      zType = 
ca810 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c  "UNLCK";.    }el
ca820 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
ca830 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ( 0 );.    }.   
ca840 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
ca850 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75  ntf("fcntl-failu
ca860 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64  re-reason: %s %d
ca870 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20   %d %d\n",.     
ca880 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32    zType, (int)l2
ca890 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c  .l_start, (int)l
ca8a0 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32  2.l_len, (int)l2
ca8b0 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65  .l_pid);.  }.  e
ca8c0 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e  rrno = savedErrn
ca8d0 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d  o;.  return s;.}
ca8e0 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c  .#define fcntl l
ca8f0 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20  ockTrace.#endif 
ca900 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54  /* SQLITE_LOCK_T
ca910 52 41 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a  RACE */..../*.**
ca920 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72   This routine tr
ca930 61 6e 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64  anslates a stand
ca940 61 72 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20  ard POSIX errno 
ca950 63 6f 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68  code into someth
ca960 69 6e 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f  ing.** useful to
ca970 20 74 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20   the clients of 
ca980 74 68 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63  the sqlite3 func
ca990 74 69 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63  tions.  Specific
ca9a0 61 6c 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69  ally, it is.** i
ca9b0 6e 74 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73  ntended to trans
ca9c0 6c 61 74 65 20 61 20 76 61 72 69 65 74 79 20 6f  late a variety o
ca9d0 66 20 22 74 72 79 20 61 67 61 69 6e 22 20 65 72  f "try again" er
ca9e0 72 6f 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45  rors into SQLITE
ca9f0 5f 42 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76  _BUSY.** and a v
caa00 61 72 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73  ariety of "pleas
caa10 65 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  e close the file
caa20 20 64 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22   descriptor NOW"
caa30 20 65 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a   errors into .**
caa40 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a   SQLITE_IOERR.**
caa50 20 0a 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69   .** Errors duri
caa60 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ng initializatio
caa70 6e 20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66  n of locks, or f
caa80 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f  ile system suppo
caa90 72 74 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a  rt for locks,.**
caaa0 20 73 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45   should handle E
caab0 4e 4f 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20  NOLCK, ENOTSUP, 
caac0 45 4f 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72  EOPNOTSUPP separ
caad0 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ately..*/.static
caae0 20 69 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72   int sqliteError
caaf0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69  FromPosixError(i
cab00 6e 74 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69  nt posixError, i
cab10 6e 74 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20  nt sqliteIOErr) 
cab20 7b 0a 20 20 73 77 69 74 63 68 20 28 70 6f 73 69  {.  switch (posi
cab30 78 45 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 65  xError) {.  case
cab40 20 30 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20   0: .    return 
cab50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a  SQLITE_OK;.    .
cab60 20 20 63 61 73 65 20 45 41 47 41 49 4e 3a 0a 20    case EAGAIN:. 
cab70 20 63 61 73 65 20 45 54 49 4d 45 44 4f 55 54 3a   case ETIMEDOUT:
cab80 0a 20 20 63 61 73 65 20 45 42 55 53 59 3a 0a 20  .  case EBUSY:. 
cab90 20 63 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 63   case EINTR:.  c
caba0 61 73 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20  ase ENOLCK:  .  
cabb0 20 20 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20    /* random NFS 
cabc0 72 65 74 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c  retry error, unl
cabd0 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20  ess during file 
cabe0 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a  system support .
cabf0 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63       * introspec
cac00 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69  tion, in which i
cac10 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73  t actually means
cac20 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f   what it says */
cac30 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
cac40 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 20  TE_BUSY;.    .  
cac50 63 61 73 65 20 45 41 43 43 45 53 3a 20 0a 20 20  case EACCES: .  
cac60 20 20 2f 2a 20 45 41 43 43 45 53 20 69 73 20 6c    /* EACCES is l
cac70 69 6b 65 20 45 41 47 41 49 4e 20 64 75 72 69 6e  ike EAGAIN durin
cac80 67 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74  g locking operat
cac90 69 6f 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e  ions, but not an
caca0 79 20 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20  y other time*/. 
cacb0 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 49 4f     if( (sqliteIO
cacc0 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f  Err == SQLITE_IO
cacd0 45 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28  ERR_LOCK) || ..(
cace0 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53  sqliteIOErr == S
cacf0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
cad00 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65  CK) || ..(sqlite
cad10 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f  IOErr == SQLITE_
cad20 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c  IOERR_RDLOCK) ||
cad30 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d  ..(sqliteIOErr =
cad40 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  = SQLITE_IOERR_C
cad50 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b  HECKRESERVEDLOCK
cad60 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
cad70 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
cad80 20 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65     }.    /* else
cad90 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f   fall through */
cada0 0a 20 20 63 61 73 65 20 45 50 45 52 4d 3a 20 0a  .  case EPERM: .
cadb0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
cadc0 45 5f 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63  E_PERM;.    .  c
cadd0 61 73 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20  ase EDEADLK:.   
cade0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
cadf0 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20  OERR_BLOCKED;.  
cae00 20 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 50    .#if EOPNOTSUP
cae10 50 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 73  P!=ENOTSUP.  cas
cae20 65 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20  e EOPNOTSUPP: . 
cae30 20 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20     /* something 
cae40 77 65 6e 74 20 74 65 72 72 69 62 6c 79 20 61 77  went terribly aw
cae50 72 79 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e  ry, unless durin
cae60 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75  g file system su
cae70 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e  pport .     * in
cae80 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20  trospection, in 
cae90 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c  which it actuall
caea0 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20  y means what it 
caeb0 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  says */.#endif.#
caec0 69 66 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 20  ifdef ENOTSUP.  
caed0 63 61 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20  case ENOTSUP: . 
caee0 20 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64     /* invalid fd
caef0 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20  , unless during 
caf00 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70  file system supp
caf10 6f 72 74 20 69 6e 74 72 6f 73 70 65 63 74 69 6f  ort introspectio
caf20 6e 2c 20 69 6e 20 77 68 69 63 68 20 0a 20 20 20  n, in which .   
caf30 20 20 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 20    * it actually 
caf40 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61  means what it sa
caf50 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63  ys */.#endif.  c
caf60 61 73 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 20  ase EIO:.  case 
caf70 45 42 41 44 46 3a 0a 20 20 63 61 73 65 20 45 49  EBADF:.  case EI
caf80 4e 56 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f  NVAL:.  case ENO
caf90 54 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e  TCONN:.  case EN
cafa0 4f 44 45 56 3a 0a 20 20 63 61 73 65 20 45 4e 58  ODEV:.  case ENX
cafb0 49 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e  IO:.  case ENOEN
cafc0 54 3a 0a 20 20 63 61 73 65 20 45 53 54 41 4c 45  T:.  case ESTALE
cafd0 3a 0a 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a  :.  case ENOSYS:
cafe0 0a 20 20 20 20 2f 2a 20 74 68 65 73 65 20 73 68  .    /* these sh
caff0 6f 75 6c 64 20 66 6f 72 63 65 20 74 68 65 20 63  ould force the c
cb000 6c 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74  lient to close t
cb010 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65 63 6f  he file and reco
cb020 6e 6e 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20  nnect */.    .  
cb030 64 65 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 65  default: .    re
cb040 74 75 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 72  turn sqliteIOErr
cb050 3b 0a 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a  ;.  }.}..../****
cb060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
cb0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
cb0c0 67 69 6e 20 55 6e 69 71 75 65 20 46 69 6c 65 20  gin Unique File 
cb0d0 49 44 20 55 74 69 6c 69 74 79 20 55 73 65 64 20  ID Utility Used 
cb0e0 42 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a  By VxWorks *****
cb0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
cb100 20 4f 6e 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e   On most version
cb110 73 20 6f 66 20 75 6e 69 78 2c 20 77 65 20 63 61  s of unix, we ca
cb120 6e 20 67 65 74 20 61 20 75 6e 69 71 75 65 20 49  n get a unique I
cb130 44 20 66 6f 72 20 61 20 66 69 6c 65 20 62 79 20  D for a file by 
cb140 63 6f 6e 63 61 74 65 6e 61 74 69 6e 67 0a 2a 2a  concatenating.**
cb150 20 74 68 65 20 64 65 76 69 63 65 20 6e 75 6d 62   the device numb
cb160 65 72 20 61 6e 64 20 74 68 65 20 69 6e 6f 64 65  er and the inode
cb170 20 6e 75 6d 62 65 72 2e 20 20 42 75 74 20 74 68   number.  But th
cb180 69 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  is does not work
cb190 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20   on VxWorks..** 
cb1a0 4f 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e  On VxWorks, a un
cb1b0 69 71 75 65 20 66 69 6c 65 20 69 64 20 6d 75 73  ique file id mus
cb1c0 74 20 62 65 20 62 61 73 65 64 20 6f 6e 20 74 68  t be based on th
cb1d0 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65  e canonical file
cb1e0 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f  name..**.** A po
cb1f0 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
cb200 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
cb210 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
cb220 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 61  can be used as a
cb230 0a 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c 65 20  .** unique file 
cb240 49 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e 20 20  ID in VxWorks.  
cb250 45 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66  Each instance of
cb260 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
cb270 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 63 6f  contains.** a co
cb280 70 79 20 6f 66 20 74 68 65 20 63 61 6e 6f 6e 69  py of the canoni
cb290 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 20 54  cal filename.  T
cb2a0 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20 72  here is also a r
cb2b0 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20  eference count. 
cb2c0 20 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74 75   .** The structu
cb2d0 72 65 20 69 73 20 72 65 63 6c 61 69 6d 65 64 20  re is reclaimed 
cb2e0 77 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  when the number 
cb2f0 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 69  of pointers to i
cb300 74 20 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65  t drops to.** ze
cb310 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  ro..**.** There 
cb320 61 72 65 20 6e 65 76 65 72 20 76 65 72 79 20 6d  are never very m
cb330 61 6e 79 20 66 69 6c 65 73 20 6f 70 65 6e 20 61  any files open a
cb340 74 20 6f 6e 65 20 74 69 6d 65 20 61 6e 64 20 6c  t one time and l
cb350 6f 6f 6b 75 70 73 20 61 72 65 20 6e 6f 74 0a 2a  ookups are not.*
cb360 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 2d  * a performance-
cb370 63 72 69 74 69 63 61 6c 20 70 61 74 68 2c 20 73  critical path, s
cb380 6f 20 69 74 20 69 73 20 73 75 66 66 69 63 69 65  o it is sufficie
cb390 6e 74 20 74 6f 20 70 75 74 20 74 68 65 73 65 0a  nt to put these.
cb3a0 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 6f 6e  ** structures on
cb3b0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a   a linked list..
cb3c0 2a 2f 0a 73 74 72 75 63 74 20 76 78 77 6f 72 6b  */.struct vxwork
cb3d0 73 46 69 6c 65 49 64 20 7b 0a 20 20 73 74 72 75  sFileId {.  stru
cb3e0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  ct vxworksFileId
cb3f0 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78   *pNext;  /* Nex
cb400 74 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 74  t in a list of t
cb410 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74  hem all */.  int
cb420 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
cb430 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
cb440 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63  mber of referenc
cb450 65 73 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a  es to this one *
cb460 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20  /.  int nName;  
cb470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb480 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74    /* Length of t
cb490 68 65 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d  he zCanonicalNam
cb4a0 65 5b 5d 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  e[] string */.  
cb4b0 63 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c  char *zCanonical
cb4c0 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Name;         /*
cb4d0 20 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e   Canonical filen
cb4e0 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f  ame */.};..#if O
cb4f0 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a  S_VXWORKS./* .**
cb500 20 41 6c 6c 20 75 6e 69 71 75 65 20 66 69 6c 65   All unique file
cb510 6e 61 6d 65 73 20 61 72 65 20 68 65 6c 64 20 6f  names are held o
cb520 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
cb530 68 65 61 64 65 64 20 62 79 20 74 68 69 73 0a 2a  headed by this.*
cb540 2a 20 76 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73  * variable:.*/.s
cb550 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77  tatic struct vxw
cb560 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f  orksFileId *vxwo
cb570 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 30 3b  rksFileList = 0;
cb580 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 79  ../*.** Simplify
cb590 20 61 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f   a filename into
cb5a0 20 69 74 73 20 63 61 6e 6f 6e 69 63 61 6c 20 66   its canonical f
cb5b0 6f 72 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67  orm.** by making
cb5c0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
cb5d0 68 61 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a  hanges:.**.**  *
cb5e0 20 72 65 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72   removing any tr
cb5f0 61 69 6c 69 6e 67 20 61 6e 64 20 64 75 70 6c 69  ailing and dupli
cb600 63 61 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e  cate /.**  * con
cb610 76 65 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75  vert /./ into ju
cb620 73 74 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65  st /.**  * conve
cb630 72 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20  rt /A/../ where 
cb640 41 20 69 73 20 61 6e 79 20 73 69 6d 70 6c 65 20  A is any simple 
cb650 6e 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f  name into just /
cb660 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 61  .**.** Changes a
cb670 72 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63 65  re made in-place
cb680 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 65  .  Return the ne
cb690 77 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a  w name length..*
cb6a0 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61  *.** The origina
cb6b0 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e  l filename is in
cb6c0 20 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74   z[0..n-1].  Ret
cb6d0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
cb6e0 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20  f.** characters 
cb6f0 69 6e 20 74 68 65 20 73 69 6d 70 6c 69 66 69 65  in the simplifie
cb700 64 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69  d name..*/.stati
cb710 63 20 69 6e 74 20 76 78 77 6f 72 6b 73 53 69 6d  c int vxworksSim
cb720 70 6c 69 66 79 4e 61 6d 65 28 63 68 61 72 20 2a  plifyName(char *
cb730 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74  z, int n){.  int
cb740 20 69 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 28 20   i, j;.  while( 
cb750 6e 3e 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27  n>1 && z[n-1]=='
cb760 2f 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66  /' ){ n--; }.  f
cb770 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69  or(i=j=0; i<n; i
cb780 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69  ++){.    if( z[i
cb790 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20  ]=='/' ){.      
cb7a0 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20  if( z[i+1]=='/' 
cb7b0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
cb7c0 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e    if( z[i+1]=='.
cb7d0 27 20 26 26 20 69 2b 32 3c 6e 20 26 26 20 7a 5b  ' && i+2<n && z[
cb7e0 69 2b 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20  i+2]=='/' ){.   
cb7f0 20 20 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20       i += 1;.   
cb800 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
cb810 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
cb820 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20   z[i+1]=='.' && 
cb830 69 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d  i+3<n && z[i+2]=
cb840 3d 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d  ='.' && z[i+3]==
cb850 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77  '/' ){.        w
cb860 68 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b 6a  hile( j>0 && z[j
cb870 2d 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b  -1]!='/' ){ j--;
cb880 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a   }.        if( j
cb890 3e 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20  >0 ){ j--; }.   
cb8a0 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20       i += 2;.   
cb8b0 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
cb8c0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
cb8d0 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a   z[j++] = z[i];.
cb8e0 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a    }.  z[j] = 0;.
cb8f0 20 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f    return j;.}../
cb900 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69 71  *.** Find a uniq
cb910 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 74  ue file ID for t
cb920 68 65 20 67 69 76 65 6e 20 61 62 73 6f 6c 75 74  he given absolut
cb930 65 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 74  e pathname.  Ret
cb940 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
cb950 20 74 6f 20 74 68 65 20 76 78 77 6f 72 6b 73 46   to the vxworksF
cb960 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 54  ileId object.  T
cb970 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20 74  his pointer is t
cb980 68 65 20 75 6e 69 71 75 65 0a 2a 2a 20 66 69 6c  he unique.** fil
cb990 65 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  e ID..**.** The 
cb9a0 6e 52 65 66 20 66 69 65 6c 64 20 6f 66 20 74 68  nRef field of th
cb9b0 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  e vxworksFileId 
cb9c0 6f 62 6a 65 63 74 20 69 73 20 69 6e 63 72 65 6d  object is increm
cb9d0 65 6e 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20  ented before.** 
cb9e0 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 72 65  the object is re
cb9f0 74 75 72 6e 65 64 2e 20 20 41 20 6e 65 77 20 76  turned.  A new v
cba00 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a  xworksFileId obj
cba10 65 63 74 20 69 73 20 63 72 65 61 74 65 64 0a 2a  ect is created.*
cba20 2a 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74  * and added to t
cba30 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20 69  he global list i
cba40 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a  f necessary..**.
cba50 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61  ** If a memory a
cba60 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20  llocation error 
cba70 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e  occurs, return N
cba80 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ULL..*/.static s
cba90 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c  truct vxworksFil
cbaa0 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6e 64  eId *vxworksFind
cbab0 46 69 6c 65 49 64 28 63 6f 6e 73 74 20 63 68 61  FileId(const cha
cbac0 72 20 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65  r *zAbsoluteName
cbad0 29 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f  ){.  struct vxwo
cbae0 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 77 3b  rksFileId *pNew;
cbaf0 20 20 20 20 20 20 20 20 20 2f 2a 20 73 65 61 72           /* sear
cbb00 63 68 20 6b 65 79 20 61 6e 64 20 6e 65 77 20 66  ch key and new f
cbb10 69 6c 65 20 49 44 20 2a 2f 0a 20 20 73 74 72 75  ile ID */.  stru
cbb20 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  ct vxworksFileId
cbb30 20 2a 70 43 61 6e 64 69 64 61 74 65 3b 20 20 20   *pCandidate;   
cbb40 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
cbb50 76 65 72 20 65 78 69 73 74 69 6e 67 20 66 69 6c  ver existing fil
cbb60 65 20 49 44 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  e IDs */.  int n
cbb70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cbb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
cbb90 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 41 62 73  * Length of zAbs
cbba0 6f 6c 75 74 65 4e 61 6d 65 20 73 74 72 69 6e 67  oluteName string
cbbb0 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 7a   */..  assert( z
cbbc0 41 62 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d  AbsoluteName[0]=
cbbd0 3d 27 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 28 69  ='/' );.  n = (i
cbbe0 6e 74 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c  nt)strlen(zAbsol
cbbf0 75 74 65 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 77  uteName);.  pNew
cbc00 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
cbc10 63 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  c( sizeof(*pNew)
cbc20 20 2b 20 28 6e 2b 31 29 20 29 3b 0a 20 20 69 66   + (n+1) );.  if
cbc30 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
cbc40 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43  rn 0;.  pNew->zC
cbc50 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 28  anonicalName = (
cbc60 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a  char*)&pNew[1];.
cbc70 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a    memcpy(pNew->z
cbc80 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a  CanonicalName, z
cbc90 41 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b  AbsoluteName, n+
cbca0 31 29 3b 0a 20 20 6e 20 3d 20 76 78 77 6f 72 6b  1);.  n = vxwork
cbcb0 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 70 4e  sSimplifyName(pN
cbcc0 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61  ew->zCanonicalNa
cbcd0 6d 65 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65  me, n);..  /* Se
cbce0 61 72 63 68 20 66 6f 72 20 61 6e 20 65 78 69 73  arch for an exis
cbcf0 74 69 6e 67 20 65 6e 74 72 79 20 74 68 61 74 20  ting entry that 
cbd00 6d 61 74 63 68 69 6e 67 20 74 68 65 20 63 61 6e  matching the can
cbd10 6f 6e 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a  onical name..  *
cbd20 2a 20 49 66 20 66 6f 75 6e 64 2c 20 69 6e 63 72  * If found, incr
cbd30 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65  ement the refere
cbd40 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 65  nce count and re
cbd50 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
cbd60 6f 0a 20 20 2a 2a 20 74 68 65 20 65 78 69 73 74  o.  ** the exist
cbd70 69 6e 67 20 66 69 6c 65 20 49 44 2e 0a 20 20 2a  ing file ID..  *
cbd80 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  /.  unixEnterMut
cbd90 65 78 28 29 3b 0a 20 20 66 6f 72 28 70 43 61 6e  ex();.  for(pCan
cbda0 64 69 64 61 74 65 3d 76 78 77 6f 72 6b 73 46 69  didate=vxworksFi
cbdb0 6c 65 4c 69 73 74 3b 20 70 43 61 6e 64 69 64 61  leList; pCandida
cbdc0 74 65 3b 20 70 43 61 6e 64 69 64 61 74 65 3d 70  te; pCandidate=p
cbdd0 43 61 6e 64 69 64 61 74 65 2d 3e 70 4e 65 78 74  Candidate->pNext
cbde0 29 7b 0a 20 20 20 20 69 66 28 20 70 43 61 6e 64  ){.    if( pCand
cbdf0 69 64 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20  idate->nName==n 
cbe00 0a 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28  .     && memcmp(
cbe10 70 43 61 6e 64 69 64 61 74 65 2d 3e 7a 43 61 6e  pCandidate->zCan
cbe20 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 77  onicalName, pNew
cbe30 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65  ->zCanonicalName
cbe40 2c 20 6e 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20  , n)==0.    ){. 
cbe50 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
cbe60 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 20  ee(pNew);.      
cbe70 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65   pCandidate->nRe
cbe80 66 2b 2b 3b 0a 20 20 20 20 20 20 20 75 6e 69 78  f++;.       unix
cbe90 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
cbea0 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 61 6e       return pCan
cbeb0 64 69 64 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20  didate;.    }.  
cbec0 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 68  }..  /* No match
cbed0 20 77 61 73 20 66 6f 75 6e 64 2e 20 20 57 65 20   was found.  We 
cbee0 77 69 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 77 20  will make a new 
cbef0 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 70 4e 65  file ID */.  pNe
cbf00 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70  w->nRef = 1;.  p
cbf10 4e 65 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a  New->nName = n;.
cbf20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20    pNew->pNext = 
cbf30 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b  vxworksFileList;
cbf40 0a 20 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69  .  vxworksFileLi
cbf50 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 75 6e 69  st = pNew;.  uni
cbf60 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
cbf70 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
cbf80 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74  ./*.** Decrement
cbf90 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
cbfa0 6f 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b  ount on a vxwork
cbfb0 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20  sFileId object. 
cbfc0 20 46 72 65 65 0a 2a 2a 20 74 68 65 20 6f 62 6a   Free.** the obj
cbfd0 65 63 74 20 77 68 65 6e 20 74 68 65 20 72 65 66  ect when the ref
cbfe0 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61  erence count rea
cbff0 63 68 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74  ches zero..*/.st
cc000 61 74 69 63 20 76 6f 69 64 20 76 78 77 6f 72 6b  atic void vxwork
cc010 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 73  sReleaseFileId(s
cc020 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c  truct vxworksFil
cc030 65 49 64 20 2a 70 49 64 29 7b 0a 20 20 75 6e 69  eId *pId){.  uni
cc040 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
cc050 20 61 73 73 65 72 74 28 20 70 49 64 2d 3e 6e 52   assert( pId->nR
cc060 65 66 3e 30 20 29 3b 0a 20 20 70 49 64 2d 3e 6e  ef>0 );.  pId->n
cc070 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 49 64  Ref--;.  if( pId
cc080 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ->nRef==0 ){.   
cc090 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46   struct vxworksF
cc0a0 69 6c 65 49 64 20 2a 2a 70 70 3b 0a 20 20 20 20  ileId **pp;.    
cc0b0 66 6f 72 28 70 70 3d 26 76 78 77 6f 72 6b 73 46  for(pp=&vxworksF
cc0c0 69 6c 65 4c 69 73 74 3b 20 2a 70 70 20 26 26 20  ileList; *pp && 
cc0d0 2a 70 70 21 3d 70 49 64 3b 20 70 70 20 3d 20 26  *pp!=pId; pp = &
cc0e0 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 7b  ((*pp)->pNext)){
cc0f0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70  }.    assert( *p
cc100 70 3d 3d 70 49 64 20 29 3b 0a 20 20 20 20 2a 70  p==pId );.    *p
cc110 70 20 3d 20 70 49 64 2d 3e 70 4e 65 78 74 3b 0a  p = pId->pNext;.
cc120 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
cc130 28 70 49 64 29 3b 0a 20 20 7d 0a 20 20 75 6e 69  (pId);.  }.  uni
cc140 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d  xLeaveMutex();.}
cc150 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58  .#endif /* OS_VX
cc160 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  WORKS */./******
cc170 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
cc180 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20   Unique File ID 
cc190 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20  Utility Used By 
cc1a0 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a  VxWorks ********
cc1b0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
cc1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc200 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a  *******/.../****
cc210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
cc260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc270 2a 2a 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 64 76  ****** Posix Adv
cc280 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a  isory Locking **
cc290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
cc2b0 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20   POSIX advisory 
cc2c0 6c 6f 63 6b 73 20 61 72 65 20 62 72 6f 6b 65 6e  locks are broken
cc2d0 20 62 79 20 64 65 73 69 67 6e 2e 20 20 41 4e 53   by design.  ANS
cc2e0 49 20 53 54 44 20 31 30 30 33 2e 31 20 28 31 39  I STD 1003.1 (19
cc2f0 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36  96).** section 6
cc300 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 33  .5.2.2 lines 483
cc310 20 74 68 72 6f 75 67 68 20 34 39 30 20 73 70 65   through 490 spe
cc320 63 69 66 79 20 74 68 61 74 20 77 68 65 6e 20 61  cify that when a
cc330 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73   process.** sets
cc340 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c 6f 63   or clears a loc
cc350 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f  k, that operatio
cc360 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20  n overrides any 
cc370 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a  prior locks set.
cc380 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65 20 70  ** by the same p
cc390 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f 65 73  rocess.  It does
cc3a0 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 20   not explicitly 
cc3b0 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68 69 73  say so, but this
cc3c0 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74   implies.** that
cc3d0 20 69 74 20 6f 76 65 72 72 69 64 65 73 20 6c 6f   it overrides lo
cc3e0 63 6b 73 20 73 65 74 20 62 79 20 74 68 65 20 73  cks set by the s
cc3f0 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73 69 6e  ame process usin
cc400 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  g a different.**
cc410 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
cc420 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73  .  Consider this
cc430 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a   test case:.**.*
cc440 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 31 20  *       int fd1 
cc450 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22  = open("./file1"
cc460 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54  , O_RDWR|O_CREAT
cc470 2c 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20 20 20  , 0644);.**     
cc480 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e    int fd2 = open
cc490 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44  ("./file2", O_RD
cc4a0 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34  WR|O_CREAT, 0644
cc4b0 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65  );.**.** Suppose
cc4c0 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f 66   ./file1 and ./f
cc4d0 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79 20  ile2 are really 
cc4e0 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28 62  the same file (b
cc4f0 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 73  ecause.** one is
cc500 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d 62 6f   a hard or symbo
cc510 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65 20  lic link to the 
cc520 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66 20 79  other) then if y
cc530 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78 63  ou set.** an exc
cc540 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 66  lusive lock on f
cc550 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20  d1, then try to 
cc560 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69 76 65  get an exclusive
cc570 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c   lock.** on fd2,
cc580 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 77 6f   it works.  I wo
cc590 75 6c 64 20 68 61 76 65 20 65 78 70 65 63 74 65  uld have expecte
cc5a0 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f 63  d the second loc
cc5b0 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 6e  k to.** fail sin
cc5c0 63 65 20 74 68 65 72 65 20 77 61 73 20 61 6c 72  ce there was alr
cc5d0 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  eady a lock on t
cc5e0 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f 20 66  he file due to f
cc5f0 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 73  d1..** But not s
cc600 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 20 6c  o.  Since both l
cc610 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20 74  ocks came from t
cc620 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2c  he same process,
cc630 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f   the.** second o
cc640 76 65 72 72 69 64 65 73 20 74 68 65 20 66 69 72  verrides the fir
cc650 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20  st, even though 
cc660 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64 69 66  they were on dif
cc670 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64  ferent.** file d
cc680 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65  escriptors opene
cc690 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 20 66  d on different f
cc6a0 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a  ile names..**.**
cc6b0 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74   This means that
cc6c0 20 77 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 50   we cannot use P
cc6d0 4f 53 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79  OSIX locks to sy
cc6e0 6e 63 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61  nchronize file a
cc6f0 63 63 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63  ccess.** among c
cc700 6f 6d 70 65 74 69 6e 67 20 74 68 72 65 61 64 73  ompeting threads
cc710 20 6f 66 20 74 68 65 20 73 61 6d 65 20 70 72 6f   of the same pro
cc720 63 65 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63  cess.  POSIX loc
cc730 6b 73 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e  ks will work fin
cc740 65 0a 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e  e.** to synchron
cc750 69 7a 65 20 61 63 63 65 73 73 20 66 6f 72 20 74  ize access for t
cc760 68 72 65 61 64 73 20 69 6e 20 73 65 70 61 72 61  hreads in separa
cc770 74 65 20 70 72 6f 63 65 73 73 65 73 2c 20 62 75  te processes, bu
cc780 74 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73  t not.** threads
cc790 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
cc7a0 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   process..**.** 
cc7b0 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74  To work around t
cc7c0 68 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69  he problem, SQLi
cc7d0 74 65 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65  te has to manage
cc7e0 20 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65   file locks inte
cc7f0 72 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73  rnally.** on its
cc800 20 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20   own.  Whenever 
cc810 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 69  a new database i
cc820 73 20 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76  s opened, we hav
cc830 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a  e to find the.**
cc840 20 73 70 65 63 69 66 69 63 20 69 6e 6f 64 65 20   specific inode 
cc850 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
cc860 66 69 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 20  file (the inode 
cc870 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
cc880 20 74 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61   the.** st_dev a
cc890 6e 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73  nd st_ino fields
cc8a0 20 6f 66 20 74 68 65 20 73 74 61 74 20 73 74 72   of the stat str
cc8b0 75 63 74 75 72 65 20 74 68 61 74 20 66 73 74 61  ucture that fsta
cc8c0 74 28 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a  t() fills in).**
cc8d0 20 61 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 6c   and check for l
cc8e0 6f 63 6b 73 20 61 6c 72 65 61 64 79 20 65 78 69  ocks already exi
cc8f0 73 74 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e  sting on that in
cc900 6f 64 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73  ode.  When locks
cc910 20 61 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 20   are.** created 
cc920 6f 72 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 68  or removed, we h
cc930 61 76 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f  ave to look at o
cc940 75 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20  ur own internal 
cc950 72 65 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a  record of the.**
cc960 20 6c 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 66   locks to see if
cc970 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20   another thread 
cc980 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73  has previously s
cc990 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61  et a lock on tha
cc9a0 74 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e  t same.** inode.
cc9b0 0a 2a 2a 0a 2a 2a 20 28 41 73 69 64 65 3a 20 54  .**.** (Aside: T
cc9c0 68 65 20 75 73 65 20 6f 66 20 69 6e 6f 64 65 20  he use of inode 
cc9d0 6e 75 6d 62 65 72 73 20 61 73 20 75 6e 69 71 75  numbers as uniqu
cc9e0 65 20 49 44 73 20 64 6f 65 73 20 6e 6f 74 20 77  e IDs does not w
cc9f0 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a  ork on VxWorks..
cca00 2a 2a 20 46 6f 72 20 56 78 57 6f 72 6b 73 2c 20  ** For VxWorks, 
cca10 77 65 20 68 61 76 65 20 74 6f 20 75 73 65 20 74  we have to use t
cca20 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 75  he alternative u
cca30 6e 69 71 75 65 20 49 44 20 73 79 73 74 65 6d 20  nique ID system 
cca40 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f  based on.** cano
cca50 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 61  nical filename a
cca60 6e 64 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69  nd implemented i
cca70 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 64  n the previous d
cca80 69 76 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20  ivision.).**.** 
cca90 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  The sqlite3_file
ccaa0 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 50   structure for P
ccab0 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  OSIX is no longe
ccac0 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65  r just an intege
ccad0 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69  r file.** descri
ccae0 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f 77  ptor.  It is now
ccaf0 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61   a structure tha
ccb00 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 74 65  t holds the inte
ccb10 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63  ger file.** desc
ccb20 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69  riptor and a poi
ccb30 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 74  nter to a struct
ccb40 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62  ure that describ
ccb50 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  es the internal.
ccb60 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20  ** locks on the 
ccb70 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e  corresponding in
ccb80 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f  ode.  There is o
ccb90 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63  ne locking struc
ccba0 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f 64  ture.** per inod
ccbb0 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61 6d  e, so if the sam
ccbc0 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65  e inode is opene
ccbd0 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75 6e  d twice, both un
ccbe0 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65  ixFile structure
ccbf0 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68  s.** point to th
ccc00 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73  e same locking s
ccc10 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 6c  tructure.  The l
ccc20 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65  ocking structure
ccc30 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 66 65   keeps.** a refe
ccc40 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f 20  rence count (so 
ccc50 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65  we will know whe
ccc60 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74 29 20  n to delete it) 
ccc70 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20 66  and a "cnt".** f
ccc80 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c 73 20  ield that tells 
ccc90 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61 6c 20  us its internal 
ccca0 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 63 6e  lock status.  cn
cccb0 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a 2a  t==0 means the.*
cccc0 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b  * file is unlock
cccd0 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 61  ed.  cnt==-1 mea
ccce0 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61 73 20  ns the file has 
cccf0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
ccd00 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 6e  k..** cnt>0 mean
ccd10 73 20 74 68 65 72 65 20 61 72 65 20 63 6e 74 20  s there are cnt 
ccd20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20  shared locks on 
ccd30 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  the file..**.** 
ccd40 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6c  Any attempt to l
ccd50 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 20  ock or unlock a 
ccd60 66 69 6c 65 20 66 69 72 73 74 20 63 68 65 63 6b  file first check
ccd70 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a  s the locking.**
ccd80 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
ccd90 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d 20   fcntl() system 
ccda0 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e 76  call is only inv
ccdb0 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20 0a 2a  oked to set a .*
ccdc0 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66 20  * POSIX lock if 
ccdd0 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63  the internal loc
ccde0 6b 20 73 74 72 75 63 74 75 72 65 20 74 72 61 6e  k structure tran
ccdf0 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 0a  sitions between.
cce00 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64 20  ** a locked and 
cce10 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 74  an unlocked stat
cce20 65 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 69  e..**.** But wai
cce30 74 3a 20 20 74 68 65 72 65 20 61 72 65 20 79 65  t:  there are ye
cce40 74 20 6d 6f 72 65 20 70 72 6f 62 6c 65 6d 73 20  t more problems 
cce50 77 69 74 68 20 50 4f 53 49 58 20 61 64 76 69 73  with POSIX advis
cce60 6f 72 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a  ory locks..**.**
cce70 20 49 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 20   If you close a 
cce80 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
cce90 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  that points to a
ccea0 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 6c   file that has l
cceb0 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63  ocks,.** all loc
ccec0 6b 73 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20  ks on that file 
cced0 74 68 61 74 20 61 72 65 20 6f 77 6e 65 64 20 62  that are owned b
ccee0 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72  y the current pr
ccef0 6f 63 65 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c  ocess are.** rel
ccf00 65 61 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20  eased.  To work 
ccf10 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62  around this prob
ccf20 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69 78 46 69  lem, each unixFi
ccf30 6c 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  le structure con
ccf40 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74  tains.** a point
ccf50 65 72 20 74 6f 20 61 6e 20 75 6e 69 78 4f 70 65  er to an unixOpe
ccf60 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20  nCnt structure. 
ccf70 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 75 6e   There is one un
ccf80 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74  ixOpenCnt struct
ccf90 75 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20  ure.** per open 
ccfa0 69 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 61  inode, which mea
ccfb0 6e 73 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65  ns that multiple
ccfc0 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 6f   unixFile can po
ccfd0 69 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a  int to a single.
ccfe0 2a 2a 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2e 20  ** unixOpenCnt. 
ccff0 20 57 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74   When an attempt
cd000 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73   is made to clos
cd010 65 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69  e an unixFile, i
cd020 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f  f there are.** o
cd030 74 68 65 72 20 75 6e 69 78 46 69 6c 65 20 6f 70  ther unixFile op
cd040 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 69  en on the same i
cd050 6e 6f 64 65 20 74 68 61 74 20 61 72 65 20 68 6f  node that are ho
cd060 6c 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65  lding locks, the
cd070 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73   call.** to clos
cd080 65 28 29 20 74 68 65 20 66 69 6c 65 20 64 65 73  e() the file des
cd090 63 72 69 70 74 6f 72 20 69 73 20 64 65 66 65 72  criptor is defer
cd0a0 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66  red until all of
cd0b0 20 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72   the locks clear
cd0c0 2e 0a 2a 2a 20 54 68 65 20 75 6e 69 78 4f 70 65  ..** The unixOpe
cd0d0 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 6b  nCnt structure k
cd0e0 65 65 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66  eeps a list of f
cd0f0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20  ile descriptors 
cd100 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20  that need to.** 
cd110 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68  be closed and th
cd120 61 74 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65  at list is walke
cd130 64 20 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20  d (and cleared) 
cd140 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f  when the last lo
cd150 63 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a  ck.** clears..**
cd160 0a 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20  .** Yet another 
cd170 70 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54  problem:  LinuxT
cd180 68 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c  hreads do not pl
cd190 61 79 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73  ay well with pos
cd1a0 69 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  ix locks..**.** 
cd1b0 4d 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69  Many older versi
cd1c0 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65  ons of linux use
cd1d0 20 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64   the LinuxThread
cd1e0 73 20 6c 69 62 72 61 72 79 20 77 68 69 63 68 20  s library which 
cd1f0 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20  is.** not posix 
cd200 63 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65  compliant.  Unde
cd210 72 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20  r LinuxThreads, 
cd220 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62  a lock created b
cd230 79 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 61  y thread.** A ca
cd240 6e 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64  nnot be modified
cd250 20 6f 72 20 6f 76 65 72 72 69 64 64 65 6e 20 62   or overridden b
cd260 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68  y a different th
cd270 72 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20  read B..** Only 
cd280 74 68 72 65 61 64 20 41 20 63 61 6e 20 6d 6f 64  thread A can mod
cd290 69 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c  ify the lock.  L
cd2a0 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 20  ocking behavior 
cd2b0 69 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66  is correct.** if
cd2c0 20 74 68 65 20 61 70 70 6c 69 61 74 69 6f 6e 20   the appliation 
cd2d0 75 73 65 73 20 74 68 65 20 6e 65 77 65 72 20 4e  uses the newer N
cd2e0 61 74 69 76 65 20 50 6f 73 69 78 20 54 68 72 65  ative Posix Thre
cd2f0 61 64 20 4c 69 62 72 61 72 79 20 28 4e 50 54 4c  ad Library (NPTL
cd300 29 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20  ).** on linux - 
cd310 77 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b  with NPTL a lock
cd320 20 63 72 65 61 74 65 64 20 62 79 20 74 68 72 65   created by thre
cd330 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64  ad A can overrid
cd340 65 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68  e locks.** in th
cd350 72 65 61 64 20 42 2e 20 20 42 75 74 20 74 68 65  read B.  But the
cd360 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20  re is no way to 
cd370 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d  know at compile-
cd380 74 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 68  time which.** th
cd390 72 65 61 64 69 6e 67 20 6c 69 62 72 61 72 79 20  reading library 
cd3a0 69 73 20 62 65 69 6e 67 20 75 73 65 64 2e 20 20  is being used.  
cd3b0 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77  So there is no w
cd3c0 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a  ay to know at.**
cd3d0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68   compile-time wh
cd3e0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72  ether or not thr
cd3f0 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69  ead A can overri
cd400 64 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65  de locks on thre
cd410 61 64 20 42 2e 0a 2a 2a 20 57 65 20 68 61 76 65  ad B..** We have
cd420 20 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d   to do a run-tim
cd430 65 20 63 68 65 63 6b 20 74 6f 20 64 69 73 63 6f  e check to disco
cd440 76 65 72 20 74 68 65 20 62 65 68 61 76 69 6f 72  ver the behavior
cd450 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   of the.** curre
cd460 6e 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a  nt process..**.*
cd470 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 68 65  * On systems whe
cd480 72 65 20 74 68 72 65 61 64 20 41 20 69 73 20 75  re thread A is u
cd490 6e 61 62 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20  nable to modify 
cd4a0 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20 62 79  locks created by
cd4b0 0a 2a 2a 20 74 68 72 65 61 64 20 42 2c 20 77 65  .** thread B, we
cd4c0 20 68 61 76 65 20 74 6f 20 6b 65 65 70 20 74 72   have to keep tr
cd4d0 61 63 6b 20 6f 66 20 77 68 69 63 68 20 74 68 72  ack of which thr
cd4e0 65 61 64 20 63 72 65 61 74 65 64 20 65 61 63 68  ead created each
cd4f0 0a 2a 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 65  .** lock.  Hence
cd500 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 78 74   there is an ext
cd510 72 61 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20  ra field in the 
cd520 6b 65 79 20 74 6f 20 74 68 65 20 75 6e 69 78 4c  key to the unixL
cd530 6f 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63  ockInfo.** struc
cd540 74 75 72 65 20 74 6f 20 72 65 63 6f 72 64 20 74  ture to record t
cd550 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  his information.
cd560 20 20 41 6e 64 20 6f 6e 20 74 68 6f 73 65 20 73    And on those s
cd570 79 73 74 65 6d 73 20 69 74 0a 2a 2a 20 69 73 20  ystems it.** is 
cd580 69 6c 6c 65 67 61 6c 20 74 6f 20 62 65 67 69 6e  illegal to begin
cd590 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
cd5a0 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 61 6e 64  n one thread and
cd5b0 20 66 69 6e 69 73 68 20 69 74 0a 2a 2a 20 69 6e   finish it.** in
cd5c0 20 61 6e 6f 74 68 65 72 2e 20 20 46 6f 72 20 74   another.  For t
cd5d0 68 69 73 20 6c 61 74 74 65 72 20 72 65 73 74 72  his latter restr
cd5e0 69 63 74 69 6f 6e 2c 20 74 68 65 72 65 20 69 73  iction, there is
cd5f0 20 6e 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e   no work-around.
cd600 0a 2a 2a 20 49 74 20 69 73 20 61 20 6c 69 6d 69  .** It is a limi
cd610 74 61 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 54  tation of LinuxT
cd620 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  hreads..*/../*.*
cd630 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b 20 74  * Set or check t
cd640 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 20  he unixFile.tid 
cd650 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66 69 65  field.  This fie
cd660 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e 20 61  ld is set when a
cd670 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73  n unixFile.** is
cd680 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 20   first opened.  
cd690 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 75  All subsequent u
cd6a0 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46  ses of the unixF
cd6b0 69 6c 65 20 76 65 72 69 66 79 20 74 68 61 74 20  ile verify that 
cd6c0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65  the.** same thre
cd6d0 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20  ad is operating 
cd6e0 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e  on the unixFile.
cd6f0 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67    Some operating
cd700 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e   systems do.** n
cd710 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74  ot allow locks t
cd720 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20  o be overridden 
cd730 62 79 20 6f 74 68 65 72 20 74 68 72 65 61 64 73  by other threads
cd740 20 61 6e 64 20 74 68 61 74 20 72 65 73 74 72 69   and that restri
cd750 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74  ction.** means t
cd760 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64 61 74  hat sqlite3* dat
cd770 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 63 61  abase handles ca
cd780 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 72  nnot be moved fr
cd790 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a  om one thread.**
cd7a0 20 74 6f 20 61 6e 6f 74 68 65 72 20 77 68 69 6c   to another whil
cd7b0 65 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64  e locks are held
cd7c0 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20  ..**.** Version 
cd7d0 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 31  3.3.1 (2006-01-1
cd7e0 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 61  5):  unixFile ca
cd7f0 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20  n be moved from 
cd800 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a  one thread to.**
cd810 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67   another as long
cd820 20 61 73 20 77 65 20 61 72 65 20 72 75 6e 6e 69   as we are runni
cd830 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74  ng on a system t
cd840 68 61 74 20 73 75 70 70 6f 72 74 73 20 74 68 72  hat supports thr
cd850 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69  eads.** overridi
cd860 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c  ng each others l
cd870 6f 63 6b 73 20 28 77 68 69 63 68 20 69 73 20 6e  ocks (which is n
cd880 6f 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d  ow the most comm
cd890 6f 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20  on behavior).** 
cd8a0 6f 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61  or if no locks a
cd8b0 72 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68  re held.  But th
cd8c0 65 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b  e unixFile.pLock
cd8d0 20 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20   field needs to 
cd8e0 62 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64  be.** recomputed
cd8f0 20 62 65 63 61 75 73 65 20 69 74 73 20 6b 65 79   because its key
cd900 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68   includes the th
cd910 72 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68  read-id.  See th
cd920 65 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77  e .** transferOw
cd930 6e 65 72 73 68 69 70 28 29 20 66 75 6e 63 74 69  nership() functi
cd940 6f 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64  on below for add
cd950 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
cd960 69 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ion.*/.#if SQLIT
cd970 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
cd980 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
cd990 5f 29 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f  _).# define SET_
cd9a0 54 48 52 45 41 44 49 44 28 58 29 20 20 20 28 58  THREADID(X)   (X
cd9b0 29 2d 3e 74 69 64 20 3d 20 70 74 68 72 65 61 64  )->tid = pthread
cd9c0 5f 73 65 6c 66 28 29 0a 23 20 64 65 66 69 6e 65  _self().# define
cd9d0 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28   CHECK_THREADID(
cd9e0 58 29 20 28 74 68 72 65 61 64 73 4f 76 65 72 72  X) (threadsOverr
cd9f0 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
cda00 6b 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20 20  ks==0 && \.     
cda10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cda20 20 20 20 20 20 20 20 21 70 74 68 72 65 61 64 5f         !pthread_
cda30 65 71 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c 20  equal((X)->tid, 
cda40 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 29  pthread_self()))
cda50 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
cda60 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 29 0a  SET_THREADID(X).
cda70 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54  # define CHECK_T
cda80 48 52 45 41 44 49 44 28 58 29 20 30 0a 23 65 6e  HREADID(X) 0.#en
cda90 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  dif../*.** An in
cdaa0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
cdab0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
cdac0 65 20 73 65 72 76 65 73 20 61 73 20 74 68 65 20  e serves as the 
cdad0 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c  key used.** to l
cdae0 6f 63 61 74 65 20 61 20 70 61 72 74 69 63 75 6c  ocate a particul
cdaf0 61 72 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73  ar unixOpenCnt s
cdb00 74 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69  tructure given i
cdb10 74 73 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 0a  ts inode.  This.
cdb20 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ** is the same a
cdb30 73 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65  s the unixLockKe
cdb40 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  y except that th
cdb50 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20 6f  e thread ID is o
cdb60 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  mitted..*/.struc
cdb70 74 20 75 6e 69 78 46 69 6c 65 49 64 20 7b 0a 20  t unixFileId {. 
cdb80 20 64 65 76 5f 74 20 64 65 76 3b 20 20 20 20 20   dev_t dev;     
cdb90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
cdba0 44 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f  Device number */
cdbb0 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
cdbc0 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73    struct vxworks
cdbd0 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 2f 2a  FileId *pId;  /*
cdbe0 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20   Unique file ID 
cdbf0 66 6f 72 20 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a  for vxworks. */.
cdc00 23 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e  #else.  ino_t in
cdc10 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o;              
cdc20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d      /* Inode num
cdc30 62 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  ber */.#endif.};
cdc40 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
cdc50 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
cdc60 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73  wing structure s
cdc70 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79  erves as the key
cdc80 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61   used.** to loca
cdc90 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  te a particular 
cdca0 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72  unixLockInfo str
cdcb0 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 73  ucture given its
cdcc0 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   inode..**.** If
cdcd0 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20   threads cannot 
cdce0 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74  override each ot
cdcf0 68 65 72 73 20 6c 6f 63 6b 73 20 28 4c 69 6e 75  hers locks (Linu
cdd00 78 54 68 72 65 61 64 73 29 2c 20 74 68 65 6e 20  xThreads), then 
cdd10 77 65 0a 2a 2a 20 73 65 74 20 74 68 65 20 75 6e  we.** set the un
cdd20 69 78 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69  ixLockKey.tid fi
cdd30 65 6c 64 20 74 6f 20 74 68 65 20 74 68 72 65 61  eld to the threa
cdd40 64 20 49 44 2e 20 20 49 66 20 74 68 72 65 61 64  d ID.  If thread
cdd50 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a  s can override.*
cdd60 2a 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  * each others lo
cdd70 63 6b 73 20 28 50 6f 73 69 78 20 61 6e 64 20 4e  cks (Posix and N
cdd80 50 54 4c 29 20 74 68 65 6e 20 74 69 64 20 69 73  PTL) then tid is
cdd90 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a   always set to z
cdda0 65 72 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 20 6f  ero..** tid is o
cddb0 6d 69 74 74 65 64 20 69 66 20 77 65 20 63 6f 6d  mitted if we com
cddc0 70 69 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72  pile without thr
cddd0 65 61 64 69 6e 67 20 73 75 70 70 6f 72 74 20 6f  eading support o
cdde0 72 20 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74  r on an OS.** ot
cddf0 68 65 72 20 74 68 61 6e 20 6c 69 6e 75 78 2e 0a  her than linux..
cde00 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  */.struct unixLo
cde10 63 6b 4b 65 79 20 7b 0a 20 20 73 74 72 75 63 74  ckKey {.  struct
cde20 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 64 3b   unixFileId fid;
cde30 20 20 2f 2a 20 55 6e 69 71 75 65 20 69 64 65 6e    /* Unique iden
cde40 74 69 66 69 65 72 20 66 6f 72 20 74 68 65 20 66  tifier for the f
cde50 69 6c 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ile */.#if SQLIT
cde60 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
cde70 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
cde80 5f 29 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74  _).  pthread_t t
cde90 69 64 3b 20 20 2f 2a 20 54 68 72 65 61 64 20 49  id;  /* Thread I
cdea0 44 20 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e  D of lock owner.
cdeb0 20 5a 65 72 6f 20 69 66 20 6e 6f 74 20 75 73 69   Zero if not usi
cdec0 6e 67 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20  ng LinuxThreads 
cded0 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
cdee0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
cdef0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
cdf00 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c   structure is al
cdf10 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68  located for each
cdf20 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20   open.** inode. 
cdf30 20 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72   Or, on LinuxThr
cdf40 65 61 64 73 2c 20 74 68 65 72 65 20 69 73 20 6f  eads, there is o
cdf50 6e 65 20 6f 66 20 74 68 65 73 65 20 73 74 72 75  ne of these stru
cdf60 63 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61  ctures for.** ea
cdf70 63 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20  ch inode opened 
cdf80 62 79 20 65 61 63 68 20 74 68 72 65 61 64 2e 0a  by each thread..
cdf90 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69  **.** A single i
cdfa0 6e 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75  node can have mu
cdfb0 6c 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63  ltiple file desc
cdfc0 72 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68  riptors, so each
cdfd0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72   unixFile.** str
cdfe0 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
cdff0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
ce000 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
ce010 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73   object and this
ce020 0a 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73  .** object keeps
ce030 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20   a count of the 
ce040 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69  number of unixFi
ce050 6c 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69  le pointing to i
ce060 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69  t..*/.struct uni
ce070 78 4c 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 74  xLockInfo {.  st
ce080 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79  ruct unixLockKey
ce090 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 20 2f 2a   lockKey;     /*
ce0a0 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20   The lookup key 
ce0b0 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  */.  int cnt;   
ce0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce0d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
ce0e0 66 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68  f SHARED locks h
ce0f0 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63  eld */.  int loc
ce100 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20  ktype;          
ce110 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
ce120 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20  of SHARED_LOCK, 
ce130 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74  RESERVED_LOCK et
ce140 63 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  c. */.  int nRef
ce150 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ce160 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ce170 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  r of pointers to
ce180 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
ce190 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  */.  struct unix
ce1a0 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 3b  LockInfo *pNext;
ce1b0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
ce1c0 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  all unixLockInfo
ce1d0 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74   objects */.  st
ce1e0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66  ruct unixLockInf
ce1f0 6f 20 2a 70 50 72 65 76 3b 20 20 20 20 20 2f 2a  o *pPrev;     /*
ce200 20 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20      .... doubly 
ce210 6c 69 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  linked */.};../*
ce220 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
ce230 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
ce240 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c   structure is al
ce250 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68  located for each
ce260 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20   open.** inode. 
ce270 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
ce280 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74  keeps track of t
ce290 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63  he number of loc
ce2a0 6b 73 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e  ks on that.** in
ce2b0 6f 64 65 2e 20 20 49 66 20 61 20 63 6c 6f 73 65  ode.  If a close
ce2c0 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 61 67   is attempted ag
ce2d0 61 69 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20 74  ainst an inode t
ce2e0 68 61 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a  hat is holding.*
ce2f0 2a 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f  * locks, the clo
ce300 73 65 20 69 73 20 64 65 66 65 72 72 65 64 20 75  se is deferred u
ce310 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63  ntil all locks c
ce320 6c 65 61 72 20 62 79 20 61 64 64 69 6e 67 20 74  lear by adding t
ce330 68 65 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72  he.** file descr
ce340 69 70 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73  iptor to be clos
ce350 65 64 20 74 6f 20 74 68 65 20 70 65 6e 64 69 6e  ed to the pendin
ce360 67 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f  g list..**.** TO
ce370 44 4f 3a 20 20 43 6f 6e 73 69 64 65 72 20 63 68  DO:  Consider ch
ce380 61 6e 67 69 6e 67 20 74 68 69 73 20 73 6f 20 74  anging this so t
ce390 68 61 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c  hat there is onl
ce3a0 79 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 0a  y a single file.
ce3b0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f  ** descriptor fo
ce3c0 72 20 65 61 63 68 20 6f 70 65 6e 20 66 69 6c 65  r each open file
ce3d0 2c 20 65 76 65 6e 20 77 68 65 6e 20 69 74 20 69  , even when it i
ce3e0 73 20 6f 70 65 6e 65 64 20 6d 75 6c 74 69 70 6c  s opened multipl
ce3f0 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 20  e times..** The 
ce400 63 6c 6f 73 65 28 29 20 73 79 73 74 65 6d 20 63  close() system c
ce410 61 6c 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f  all would only o
ce420 63 63 75 72 20 77 68 65 6e 20 74 68 65 20 6c 61  ccur when the la
ce430 73 74 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 75  st database.** u
ce440 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 63 6c  sing the file cl
ce450 6f 73 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  oses..*/.struct 
ce460 75 6e 69 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 20  unixOpenCnt {.  
ce470 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49  struct unixFileI
ce480 64 20 66 69 6c 65 49 64 3b 20 20 20 2f 2a 20 54  d fileId;   /* T
ce490 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f  he lookup key */
ce4a0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
ce4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ce4c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e  * Number of poin
ce4d0 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72  ters to this str
ce4e0 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20  ucture */.  int 
ce4f0 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20  nLock;          
ce500 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ce510 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67  r of outstanding
ce520 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78   locks */.  Unix
ce530 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65  UnusedFd *pUnuse
ce540 64 3b 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65  d;      /* Unuse
ce550 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
ce560 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 23  rs to close */.#
ce570 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
ce580 73 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20  sem_t *pSem;    
ce590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce5a0 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20   /* Named POSIX 
ce5b0 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63  semaphore */.  c
ce5c0 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58  har aSemName[MAX
ce5d0 5f 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 20  _PATHNAME+2];   
ce5e0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20  /* Name of that 
ce5f0 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e  semaphore */.#en
ce600 64 69 66 0a 20 20 73 74 72 75 63 74 20 75 6e 69  dif.  struct uni
ce610 78 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c  xOpenCnt *pNext,
ce620 20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69   *pPrev;   /* Li
ce630 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70  st of all unixOp
ce640 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f  enCnt objects */
ce650 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73  .};../*.** Lists
ce660 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b   of all unixLock
ce670 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65  Info and unixOpe
ce680 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 54  nCnt objects.  T
ce690 68 65 73 65 20 75 73 65 64 20 74 6f 20 62 65 20  hese used to be 
ce6a0 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20  hash.** tables. 
ce6b0 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20   But the number 
ce6c0 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72 61  of objects is ra
ce6d0 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 61  rely more than a
ce6e0 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65   dozen and.** ne
ce6f0 76 65 72 20 65 78 63 65 65 64 73 20 61 20 66 65  ver exceeds a fe
ce700 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64  w thousand.  And
ce710 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f   lookup is not o
ce720 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20  n a critical.** 
ce730 70 61 74 68 20 73 6f 20 61 20 73 69 6d 70 6c 65  path so a simple
ce740 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c   linked list wil
ce750 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74  l suffice..*/.st
ce760 61 74 69 63 20 73 74 72 75 63 74 20 75 6e 69 78  atic struct unix
ce770 4c 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69  LockInfo *lockLi
ce780 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 73  st = 0;.static s
ce790 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
ce7a0 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b  t *openList = 0;
ce7b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72  ../*.** This var
ce7c0 69 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 20  iable remembers 
ce7d0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
ce7e0 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72  hreads can overr
ce7f0 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 0a  ide each others.
ce800 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  ** locks..**.** 
ce810 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65     0:  No.  Thre
ce820 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72  ads cannot overr
ce830 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20  ide each others 
ce840 6c 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68  locks.  (LinuxTh
ce850 72 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20  reads).**    1: 
ce860 20 59 65 73 2e 20 20 54 68 72 65 61 64 73 20 63   Yes.  Threads c
ce870 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68  an override each
ce880 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20   others locks.  
ce890 28 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a  (Posix & NLPT).*
ce8a0 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27  *   -1:  We don'
ce8b0 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a  t know yet..**.*
ce8c0 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d  * On some system
ce8d0 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f  s, we know at co
ce8e0 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 68  mpile-time if th
ce8f0 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69  reads can overri
ce900 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72  de each.** other
ce910 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f  s locks.  On tho
ce920 73 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 20  se systems, the 
ce930 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56  SQLITE_THREAD_OV
ce940 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72  ERRIDE_LOCK macr
ce950 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74  o.** will be set
ce960 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20   appropriately. 
ce970 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 65 6d   On other system
ce980 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 63 68  s, we have to ch
ce990 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d  eck at.** runtim
ce9a0 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 74  e.  On these lat
ce9b0 74 65 72 20 73 79 73 74 65 6d 73 2c 20 53 51 4c  ter systems, SQL
ce9c0 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52  TIE_THREAD_OVERR
ce9d0 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75  IDE_LOCK is.** u
ce9e0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
ce9f0 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6e 6f  This variable no
cea00 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 20  rmally has file 
cea10 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74  scope only.  But
cea20 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c   during testing,
cea30 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61   we make.** it a
cea40 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 20   global so that 
cea50 74 68 65 20 74 65 73 74 20 63 6f 64 65 20 63 61  the test code ca
cea60 6e 20 63 68 61 6e 67 65 20 69 74 73 20 76 61 6c  n change its val
cea70 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 76  ue in order to v
cea80 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 68  erify.** that th
cea90 65 20 72 69 67 68 74 20 73 74 75 66 66 20 68 61  e right stuff ha
ceaa0 70 70 65 6e 73 20 69 6e 20 65 69 74 68 65 72 20  ppens in either 
ceab0 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  case..*/.#if SQL
ceac0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
cead0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75  & defined(__linu
ceae0 78 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 53  x__).#  ifndef S
ceaf0 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45  QLITE_THREAD_OVE
ceb00 52 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20  RRIDE_LOCK.#    
ceb10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48  define SQLITE_TH
ceb20 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
ceb30 43 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23  CK -1.#  endif.#
ceb40 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54    ifdef SQLITE_T
ceb50 45 53 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f  EST.int threadsO
ceb60 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
ceb70 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f  sLocks = SQLITE_
ceb80 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f  THREAD_OVERRIDE_
ceb90 4c 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74  LOCK;.#  else.st
ceba0 61 74 69 63 20 69 6e 74 20 74 68 72 65 61 64 73  atic int threads
cebb0 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
cebc0 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45  rsLocks = SQLITE
cebd0 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
cebe0 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a  _LOCK;.#  endif.
cebf0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
cec00 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  is structure hol
cec10 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70  ds information p
cec20 61 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 76  assed into indiv
cec30 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68  idual test.** th
cec40 72 65 61 64 73 20 62 79 20 74 68 65 20 74 65 73  reads by the tes
cec50 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65  tThreadLockingBe
cec60 68 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65  havior() routine
cec70 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 65  ..*/.struct thre
cec80 61 64 54 65 73 74 44 61 74 61 20 7b 0a 20 20 69  adTestData {.  i
cec90 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20  nt fd;          
ceca0 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f        /* File to
cecb0 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20   be locked */.  
cecc0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
cecd0 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f  k;     /* The lo
cece0 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20  cking operation 
cecf0 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 3b  */.  int result;
ced00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
ced10 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63  esult of the loc
ced20 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a  king operation *
ced30 2f 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45  /.};..#if SQLITE
ced40 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
ced50 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
ced60 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  )./*.** This fun
ced70 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73  ction is used as
ced80 20 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e   the main routin
ced90 65 20 66 6f 72 20 61 20 74 68 72 65 61 64 20 6c  e for a thread l
ceda0 61 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 65  aunched by.** te
cedb0 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42  stThreadLockingB
cedc0 65 68 61 76 69 6f 72 28 29 2e 20 49 74 20 74 65  ehavior(). It te
cedd0 73 74 73 20 77 68 65 74 68 65 72 20 74 68 65 20  sts whether the 
cede0 73 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61  shared-lock obta
cedf0 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d  ined.** by the m
cee00 61 69 6e 20 74 68 72 65 61 64 20 69 6e 20 74 65  ain thread in te
cee10 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42  stThreadLockingB
cee20 65 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69  ehavior() confli
cee30 63 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 79  cts with a.** hy
cee40 70 6f 74 68 65 74 69 63 61 6c 20 77 72 69 74 65  pothetical write
cee50 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 62  -lock obtained b
cee60 79 20 74 68 69 73 20 74 68 72 65 61 64 20 6f 6e  y this thread on
cee70 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a   the same file..
cee80 2a 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 2d  **.** The write-
cee90 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 75  lock is not actu
ceea0 61 6c 6c 79 20 61 63 71 75 69 72 65 64 2c 20 61  ally acquired, a
ceeb0 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f  s this is not po
ceec0 73 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68  ssible if .** th
ceed0 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 69  e file is open i
ceee0 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65  n read-only mode
ceef0 20 28 73 65 65 20 74 69 63 6b 65 74 20 23 33 34   (see ticket #34
cef00 37 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20  72)..*/ .static 
cef10 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b  void *threadLock
cef20 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41  ingTest(void *pA
cef30 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68  rg){.  struct th
cef40 72 65 61 64 54 65 73 74 44 61 74 61 20 2a 70 44  readTestData *pD
cef50 61 74 61 20 3d 20 28 73 74 72 75 63 74 20 74 68  ata = (struct th
cef60 72 65 61 64 54 65 73 74 44 61 74 61 2a 29 70 41  readTestData*)pA
cef70 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73  rg;.  pData->res
cef80 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74  ult = fcntl(pDat
cef90 61 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20  a->fd, F_GETLK, 
cefa0 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20  &pData->lock);. 
cefb0 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a   return pArg;.}.
cefc0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
cefd0 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
cefe0 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
ceff0 29 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54  ) */...#if SQLIT
cf000 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
cf010 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
cf020 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72  _)./*.** This pr
cf030 6f 63 65 64 75 72 65 20 61 74 74 65 6d 70 74 73  ocedure attempts
cf040 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68   to determine wh
cf050 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72  ether or not thr
cf060 65 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72  eads.** can over
cf070 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
cf080 20 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73   locks then sets
cf090 20 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73   the .** threads
cf0a0 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
cf0b0 72 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65  rsLocks variable
cf0c0 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a   appropriately..
cf0d0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
cf0e0 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67  estThreadLocking
cf0f0 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f  Behavior(int fd_
cf100 6f 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b  orig){.  int fd;
cf110 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72  .  int rc;.  str
cf120 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44 61  uct threadTestDa
cf130 74 61 20 64 3b 0a 20 20 73 74 72 75 63 74 20 66  ta d;.  struct f
cf140 6c 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61  lock l;.  pthrea
cf150 64 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64  d_t t;..  fd = d
cf160 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69  up(fd_orig);.  i
cf170 66 28 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e  f( fd<0 ) return
cf180 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30  ;.  memset(&l, 0
cf190 2c 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20  , sizeof(l));.  
cf1a0 6c 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c  l.l_type = F_RDL
cf1b0 43 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20  CK;.  l.l_len = 
cf1c0 31 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d  1;.  l.l_start =
cf1d0 20 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65   0;.  l.l_whence
cf1e0 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72   = SEEK_SET;.  r
cf1f0 63 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69  c = fcntl(fd_ori
cf200 67 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b  g, F_SETLK, &l);
cf210 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 72  .  if( rc!=0 ) r
cf220 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28  eturn;.  memset(
cf230 26 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29  &d, 0, sizeof(d)
cf240 29 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a  );.  d.fd = fd;.
cf250 20 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20    d.lock = l;.  
cf260 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20  d.lock.l_type = 
cf270 46 5f 57 52 4c 43 4b 3b 0a 20 20 69 66 28 20 70  F_WRLCK;.  if( p
cf280 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74  thread_create(&t
cf290 2c 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69  , 0, threadLocki
cf2a0 6e 67 54 65 73 74 2c 20 26 64 29 3d 3d 30 20 29  ngTest, &d)==0 )
cf2b0 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6a 6f  {.    pthread_jo
cf2c0 69 6e 28 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  in(t, 0);.  }.  
cf2d0 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28  close(fd);.  if(
cf2e0 20 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72   d.result!=0 ) r
cf2f0 65 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73  eturn;.  threads
cf300 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
cf310 72 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63  rsLocks = (d.loc
cf320 6b 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43  k.l_type==F_UNLC
cf330 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  K);.}.#endif /* 
cf340 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
cf350 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c  E && defined(__l
cf360 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a  inux__) */../*.*
cf370 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78  * Release a unix
cf380 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75  LockInfo structu
cf390 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  re previously al
cf3a0 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c  located by findL
cf3b0 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a  ockInfo()..**.**
cf3c0 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72   The mutex enter
cf3d0 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69  ed using the uni
cf3e0 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75  xEnterMutex() fu
cf3f0 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68  nction must be h
cf400 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73  eld.** when this
cf410 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
cf420 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  led..*/.static v
cf430 6f 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49  oid releaseLockI
cf440 6e 66 6f 28 73 74 72 75 63 74 20 75 6e 69 78 4c  nfo(struct unixL
cf450 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b  ockInfo *pLock){
cf460 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d  .  assert( unixM
cf470 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20  utexHeld() );.  
cf480 69 66 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20  if( pLock ){.   
cf490 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a   pLock->nRef--;.
cf4a0 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e      if( pLock->n
cf4b0 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Ref==0 ){.      
cf4c0 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76  if( pLock->pPrev
cf4d0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
cf4e0 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76  rt( pLock->pPrev
cf4f0 2d 3e 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29  ->pNext==pLock )
cf500 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d  ;.        pLock-
cf510 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
cf520 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20  pLock->pNext;.  
cf530 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
cf540 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c     assert( lockL
cf550 69 73 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20  ist==pLock );.  
cf560 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d        lockList =
cf570 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20   pLock->pNext;. 
cf580 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
cf590 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b   pLock->pNext ){
cf5a0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
cf5b0 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70   pLock->pNext->p
cf5c0 50 72 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20  Prev==pLock );. 
cf5d0 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e         pLock->pN
cf5e0 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f  ext->pPrev = pLo
cf5f0 63 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20  ck->pPrev;.     
cf600 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
cf610 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20  _free(pLock);.  
cf620 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
cf630 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4f   Release a unixO
cf640 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65  penCnt structure
cf650 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
cf660 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63  cated by findLoc
cf670 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  kInfo()..**.** T
cf680 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64  he mutex entered
cf690 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45   using the unixE
cf6a0 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63  nterMutex() func
cf6b0 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c  tion must be hel
cf6c0 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66  d.** when this f
cf6d0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
cf6e0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
cf6f0 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74  d releaseOpenCnt
cf700 28 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e  (struct unixOpen
cf710 43 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61  Cnt *pOpen){.  a
cf720 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78  ssert( unixMutex
cf730 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20  Held() );.  if( 
cf740 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70  pOpen ){.    pOp
cf750 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20  en->nRef--;.    
cf760 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d  if( pOpen->nRef=
cf770 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
cf780 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a  pOpen->pPrev ){.
cf790 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
cf7a0 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e  pOpen->pPrev->pN
cf7b0 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20  ext==pOpen );.  
cf7c0 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72        pOpen->pPr
cf7d0 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65  ev->pNext = pOpe
cf7e0 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  n->pNext;.      
cf7f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
cf800 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d  ssert( openList=
cf810 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20  =pOpen );.      
cf820 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70    openList = pOp
cf830 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  en->pNext;.     
cf840 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70   }.      if( pOp
cf850 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  en->pNext ){.   
cf860 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
cf870 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76  en->pNext->pPrev
cf880 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20  ==pOpen );.     
cf890 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d     pOpen->pNext-
cf8a0 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e  >pPrev = pOpen->
cf8b0 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 23  pPrev;.      }.#
cf8c0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
cf8d0 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28  SAFE && defined(
cf8e0 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 20 20 20  __linux__).     
cf8f0 20 61 73 73 65 72 74 28 20 21 70 4f 70 65 6e 2d   assert( !pOpen-
cf900 3e 70 55 6e 75 73 65 64 20 7c 7c 20 74 68 72 65  >pUnused || thre
cf910 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
cf920 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 29 3b  thersLocks==0 );
cf930 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f  .#endif..      /
cf940 2a 20 49 66 20 70 4f 70 65 6e 2d 3e 70 55 6e 75  * If pOpen->pUnu
cf950 73 65 64 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c  sed is not null,
cf960 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 61 6e 64   then memory and
cf970 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72   file-descriptor
cf980 73 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20 6c  s.      ** are l
cf990 65 61 6b 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a  eaked..      **.
cf9a0 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 77 69        ** This wi
cf9b0 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69  ll only happen i
cf9c0 66 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 74 68  f, under Linuxth
cf9d0 72 65 61 64 73 2c 20 74 68 65 20 75 73 65 72 20  reads, the user 
cf9e0 68 61 73 20 6f 70 65 6e 65 64 0a 20 20 20 20 20  has opened.     
cf9f0 20 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f   ** a transactio
cfa00 6e 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 2c  n in one thread,
cfa10 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74   then attempts t
cfa20 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61  o close the data
cfa30 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61  base.      ** ha
cfa40 6e 64 6c 65 20 66 72 6f 6d 20 61 6e 6f 74 68 65  ndle from anothe
cfa50 72 20 74 68 72 65 61 64 20 28 77 69 74 68 6f 75  r thread (withou
cfa60 74 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 69 6e  t first unlockin
cfa70 67 20 74 68 65 20 64 62 20 66 69 6c 65 29 2e 0a  g the db file)..
cfa80 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73        ** This is
cfa90 20 61 20 6d 69 73 75 73 65 2e 20 20 2a 2f 0a 20   a misuse.  */. 
cfaa0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
cfab0 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a  e(pOpen);.    }.
cfac0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76    }.}../*.** Giv
cfad0 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69  en a file descri
cfae0 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 6e 69  ptor, locate uni
cfaf0 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e  xLockInfo and un
cfb00 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74  ixOpenCnt struct
cfb10 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 73  ures that.** des
cfb20 63 72 69 62 65 73 20 74 68 61 74 20 66 69 6c 65  cribes that file
cfb30 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 72   descriptor.  Cr
cfb40 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 66  eate new ones if
cfb50 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65   necessary.  The
cfb60 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65  .** return value
cfb70 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69  s might be unini
cfb80 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 65  tialized if an e
cfb90 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a  rror occurs..**.
cfba0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74  ** The mutex ent
cfbb0 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75  ered using the u
cfbc0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20  nixEnterMutex() 
cfbd0 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  function must be
cfbe0 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68   held.** when th
cfbf0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
cfc00 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  alled..**.** Ret
cfc10 75 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61  urn an appropria
cfc20 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  te error code..*
cfc30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e  /.static int fin
cfc40 64 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69  dLockInfo(.  uni
cfc50 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20  xFile *pFile,   
cfc60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
cfc70 6e 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 69  nix file with fi
cfc80 6c 65 20 64 65 73 63 20 75 73 65 64 20 69 6e 20  le desc used in 
cfc90 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72  the key */.  str
cfca0 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  uct unixLockInfo
cfcb0 20 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52   **ppLock,  /* R
cfcc0 65 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f  eturn the unixLo
cfcd0 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ckInfo structure
cfce0 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63   here */.  struc
cfcf0 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a  t unixOpenCnt **
cfd00 70 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 74  ppOpen    /* Ret
cfd10 75 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e  urn the unixOpen
cfd20 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65  Cnt structure he
cfd30 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
cfd40 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
cfd50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 73            /* Sys
cfd60 74 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e 20  tem call return 
cfd70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64  code */.  int fd
cfd80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cfd90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
cfda0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
cfdb0 66 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73  for pFile */.  s
cfdc0 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65  truct unixLockKe
cfdd0 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a  y lockKey;    /*
cfde0 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20   Lookup key for 
cfdf0 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  the unixLockInfo
cfe00 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
cfe10 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49  struct unixFileI
cfe20 64 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 2f  d fileId;      /
cfe30 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72  * Lookup key for
cfe40 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74   the unixOpenCnt
cfe50 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 72   struct */.  str
cfe60 75 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66  uct stat statbuf
cfe70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ;           /* L
cfe80 6f 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e  ow-level file in
cfe90 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73  formation */.  s
cfea0 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e  truct unixLockIn
cfeb0 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f 2a  fo *pLock = 0;/*
cfec0 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4c   Candidate unixL
cfed0 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a  ockInfo object *
cfee0 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f  /.  struct unixO
cfef0 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20  penCnt *pOpen;  
cff00 20 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20     /* Candidate 
cff10 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65  unixOpenCnt obje
cff20 63 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ct */..  assert(
cff30 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29   unixMutexHeld()
cff40 20 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f   );..  /* Get lo
cff50 77 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74  w-level informat
cff60 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 69  ion about the fi
cff70 6c 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 75  le that we can u
cff80 73 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61  sed to.  ** crea
cff90 74 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d 65  te a unique name
cffa0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a 20   for the file.. 
cffb0 20 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c 65   */.  fd = pFile
cffc0 2d 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 61  ->h;.  rc = fsta
cffd0 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b  t(fd, &statbuf);
cffe0 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a  .  if( rc!=0 ){.
cfff0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d0000 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69  rrno = errno;.#i
d0010 66 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20  fdef EOVERFLOW. 
d0020 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61     if( pFile->la
d0030 73 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c  stErrno==EOVERFL
d0040 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
d0050 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66  TE_NOLFS;.#endif
d0060 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d0070 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23  TE_IOERR;.  }..#
d0080 69 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a  ifdef __APPLE__.
d0090 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20    /* On OS X on 
d00a0 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73  an msdos filesys
d00b0 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e  tem, the inode n
d00c0 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 65  umber is reporte
d00d0 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74  d.  ** incorrect
d00e0 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65  ly for zero-size
d00f0 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63   files.  See tic
d0100 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77  ket #3260.  To w
d0110 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20  ork.  ** around 
d0120 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65  this problem (we
d0130 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62   consider it a b
d0140 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20  ug in OS X, not 
d0150 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20  SQLite).  ** we 
d0160 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 20  always increase 
d0170 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f  the file size to
d0180 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 20   1 by writing a 
d0190 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a  single byte.  **
d01a0 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73   prior to access
d01b0 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75  ing the inode nu
d01c0 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62  mber.  The one b
d01d0 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20  yte written is. 
d01e0 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27   ** an ASCII 'S'
d01f0 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63 68   character which
d0200 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f   also happens to
d0210 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62 79   be the first by
d0220 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68  te.  ** in the h
d0230 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 53  eader of every S
d0240 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 20  QLite database. 
d0250 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66   In this way, if
d0260 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61   there.  ** is a
d0270 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20   race condition 
d0280 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65  such that anothe
d0290 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c 72  r thread has alr
d02a0 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20  eady populated. 
d02b0 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 61   ** the first pa
d02c0 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
d02d0 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73  se, no damage is
d02e0 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   done..  */.  if
d02f0 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a  ( statbuf.st_siz
d0300 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  e==0 ){.    rc =
d0310 20 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20   write(fd, "S", 
d0320 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  1);.    if( rc!=
d0330 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  1 ){.      retur
d0340 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a  n SQLITE_IOERR;.
d0350 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66      }.    rc = f
d0360 73 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75  stat(fd, &statbu
d0370 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  f);.    if( rc!=
d0380 30 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  0 ){.      pFile
d0390 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
d03a0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72  rno;.      retur
d03b0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a  n SQLITE_IOERR;.
d03c0 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
d03d0 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b  ..  memset(&lock
d03e0 4b 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c  Key, 0, sizeof(l
d03f0 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b  ockKey));.  lock
d0400 4b 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 74  Key.fid.dev = st
d0410 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69  atbuf.st_dev;.#i
d0420 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c  f OS_VXWORKS.  l
d0430 6f 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d  ockKey.fid.pId =
d0440 20 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c   pFile->pId;.#el
d0450 73 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64  se.  lockKey.fid
d0460 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73  .ino = statbuf.s
d0470 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69  t_ino;.#endif.#i
d0480 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
d0490 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  AFE && defined(_
d04a0 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20  _linux__).  if( 
d04b0 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
d04c0 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30  achOthersLocks<0
d04d0 20 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72 65   ){.    testThre
d04e0 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f  adLockingBehavio
d04f0 72 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63  r(fd);.  }.  loc
d0500 6b 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65 61  kKey.tid = threa
d0510 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
d0520 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20  hersLocks ? 0 : 
d0530 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a  pthread_self();.
d0540 23 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 20  #endif.  fileId 
d0550 3d 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20  = lockKey.fid;. 
d0560 20 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29   if( ppLock!=0 )
d0570 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f  {.    pLock = lo
d0580 63 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c  ckList;.    whil
d0590 65 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63  e( pLock && memc
d05a0 6d 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c  mp(&lockKey, &pL
d05b0 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69  ock->lockKey, si
d05c0 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29  zeof(lockKey)) )
d05d0 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20  {.      pLock = 
d05e0 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20  pLock->pNext;.  
d05f0 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 63    }.    if( pLoc
d0600 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c  k==0 ){.      pL
d0610 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ock = sqlite3_ma
d0620 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c  lloc( sizeof(*pL
d0630 6f 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 66  ock) );.      if
d0640 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20  ( pLock==0 ){.  
d0650 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
d0660 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20  E_NOMEM;.       
d0670 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c   goto exit_findl
d0680 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d  ockinfo;.      }
d0690 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70  .      memcpy(&p
d06a0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 26 6c  Lock->lockKey,&l
d06b0 6f 63 6b 4b 65 79 2c 73 69 7a 65 6f 66 28 6c 6f  ockKey,sizeof(lo
d06c0 63 6b 4b 65 79 29 29 3b 0a 20 20 20 20 20 20 70  ckKey));.      p
d06d0 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Lock->nRef = 1;.
d06e0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74        pLock->cnt
d06f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63   = 0;.      pLoc
d0700 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 30 3b  k->locktype = 0;
d0710 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e  .      pLock->pN
d0720 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b 0a  ext = lockList;.
d0730 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 72        pLock->pPr
d0740 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  ev = 0;.      if
d0750 28 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c 6f 63  ( lockList ) loc
d0760 6b 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70  kList->pPrev = p
d0770 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b  Lock;.      lock
d0780 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20  List = pLock;.  
d0790 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
d07a0 4c 6f 63 6b 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  Lock->nRef++;.  
d07b0 20 20 7d 0a 20 20 20 20 2a 70 70 4c 6f 63 6b 20    }.    *ppLock 
d07c0 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 69  = pLock;.  }.  i
d07d0 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 7b 0a  f( ppOpen!=0 ){.
d07e0 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70 65 6e      pOpen = open
d07f0 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28  List;.    while(
d0800 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63 6d 70   pOpen && memcmp
d0810 28 26 66 69 6c 65 49 64 2c 20 26 70 4f 70 65 6e  (&fileId, &pOpen
d0820 2d 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66  ->fileId, sizeof
d0830 28 66 69 6c 65 49 64 29 29 20 29 7b 0a 20 20 20  (fileId)) ){.   
d0840 20 20 20 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e     pOpen = pOpen
d0850 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  ->pNext;.    }. 
d0860 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20     if( pOpen==0 
d0870 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d  ){.      pOpen =
d0880 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
d0890 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 20   sizeof(*pOpen) 
d08a0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  );.      if( pOp
d08b0 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  en==0 ){.       
d08c0 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f   releaseLockInfo
d08d0 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20  (pLock);.       
d08e0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
d08f0 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  EM;.        goto
d0900 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e   exit_findlockin
d0910 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  fo;.      }.    
d0920 20 20 6d 65 6d 73 65 74 28 70 4f 70 65 6e 2c 20    memset(pOpen, 
d0930 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e  0, sizeof(*pOpen
d0940 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  ));.      pOpen-
d0950 3e 66 69 6c 65 49 64 20 3d 20 66 69 6c 65 49 64  >fileId = fileId
d0960 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e  ;.      pOpen->n
d0970 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  Ref = 1;.      p
d0980 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 3d 20 6f 70  Open->pNext = op
d0990 65 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 69 66  enList;.      if
d09a0 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 6f 70 65  ( openList ) ope
d09b0 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70  nList->pPrev = p
d09c0 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f 70 65 6e  Open;.      open
d09d0 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b 0a 20 20  List = pOpen;.  
d09e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
d09f0 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  Open->nRef++;.  
d0a00 20 20 7d 0a 20 20 20 20 2a 70 70 4f 70 65 6e 20    }.    *ppOpen 
d0a10 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a 0a 65 78  = pOpen;.  }..ex
d0a20 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3a  it_findlockinfo:
d0a30 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d0a40 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65  ./*.** If we are
d0a50 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20   currently in a 
d0a60 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
d0a70 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61 64   than the thread
d0a80 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e 69   that the.** uni
d0a90 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20 62  xFile argument b
d0aa0 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e 20  elongs to, then 
d0ab0 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68  transfer ownersh
d0ac0 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69  ip of the unixFi
d0ad0 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68  le.** over to th
d0ae0 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64  e current thread
d0af0 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 69  ..**.** A unixFi
d0b00 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 64  le is only owned
d0b10 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e 20   by a thread on 
d0b20 73 79 73 74 65 6d 73 20 74 68 61 74 20 75 73 65  systems that use
d0b30 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a 2a   LinuxThreads..*
d0b40 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 69 70 20 74  *.** Ownership t
d0b50 72 61 6e 73 66 65 72 20 69 73 20 6f 6e 6c 79 20  ransfer is only 
d0b60 61 6c 6c 6f 77 65 64 20 69 66 20 74 68 65 20 75  allowed if the u
d0b70 6e 69 78 46 69 6c 65 20 69 73 20 63 75 72 72 65  nixFile is curre
d0b80 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a  ntly unlocked..*
d0b90 2a 20 49 66 20 74 68 65 20 75 6e 69 78 46 69 6c  * If the unixFil
d0ba0 65 20 69 73 20 6c 6f 63 6b 65 64 20 61 6e 64 20  e is locked and 
d0bb0 61 6e 20 6f 77 6e 65 72 73 68 69 70 20 69 73 20  an ownership is 
d0bc0 77 72 6f 6e 67 2c 20 74 68 65 6e 20 72 65 74 75  wrong, then retu
d0bd0 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53  rn.** SQLITE_MIS
d0be0 55 53 45 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 20  USE.  SQLITE_OK 
d0bf0 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 65  is returned if e
d0c00 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2e  verything works.
d0c10 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  .*/.#if SQLITE_T
d0c20 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
d0c30 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
d0c40 73 74 61 74 69 63 20 69 6e 74 20 74 72 61 6e 73  static int trans
d0c50 66 65 72 4f 77 6e 65 72 73 68 69 70 28 75 6e 69  ferOwnership(uni
d0c60 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  xFile *pFile){. 
d0c70 20 69 6e 74 20 72 63 3b 0a 20 20 70 74 68 72 65   int rc;.  pthre
d0c80 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20 20 69 66  ad_t hSelf;.  if
d0c90 28 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64  ( threadsOverrid
d0ca0 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73  eEachOthersLocks
d0cb0 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 72   ){.    /* Owner
d0cc0 73 68 69 70 20 74 72 61 6e 73 66 65 72 73 20 6e  ship transfers n
d0cd0 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 69  ot needed on thi
d0ce0 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20  s system */.    
d0cf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d0d00 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66 20 3d 20  ;.  }.  hSelf = 
d0d10 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a  pthread_self();.
d0d20 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 65 71    if( pthread_eq
d0d30 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69 64 2c 20  ual(pFile->tid, 
d0d40 68 53 65 6c 66 29 20 29 7b 0a 20 20 20 20 2f 2a  hSelf) ){.    /*
d0d50 20 57 65 20 61 72 65 20 73 74 69 6c 6c 20 69 6e   We are still in
d0d60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
d0d70 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 31   */.    OSTRACE1
d0d80 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72 2c 20 73  ("No-transfer, s
d0d90 61 6d 65 20 74 68 72 65 61 64 5c 6e 22 29 3b 0a  ame thread\n");.
d0da0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d0db0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  E_OK;.  }.  if( 
d0dc0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21  pFile->locktype!
d0dd0 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  =NO_LOCK ){.    
d0de0 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 63 68 61  /* We cannot cha
d0df0 6e 67 65 20 6f 77 6e 65 72 73 68 69 70 20 77 68  nge ownership wh
d0e00 69 6c 65 20 77 65 20 61 72 65 20 68 6f 6c 64 69  ile we are holdi
d0e10 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 20  ng a lock! */.  
d0e20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d0e30 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 4f 53  MISUSE;.  }.  OS
d0e40 54 52 41 43 45 34 28 22 54 72 61 6e 73 66 65 72  TRACE4("Transfer
d0e50 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 25 64   ownership of %d
d0e60 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 5c 6e   from %d to %d\n
d0e70 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ",.            p
d0e80 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
d0e90 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a 20 20 70  tid, hSelf);.  p
d0ea0 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68 53 65 6c  File->tid = hSel
d0eb0 66 3b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  f;.  if (pFile->
d0ec0 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 7b  pLock != NULL) {
d0ed0 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b  .    releaseLock
d0ee0 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63  Info(pFile->pLoc
d0ef0 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e  k);.    rc = fin
d0f00 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2c  dLockInfo(pFile,
d0f10 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2c 20   &pFile->pLock, 
d0f20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 35  0);.    OSTRACE5
d0f30 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69 73 20  ("LOCK    %d is 
d0f40 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c 6e 22  now %s(%s,%d)\n"
d0f50 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
d0f60 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e         locktypeN
d0f70 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  ame(pFile->lockt
d0f80 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ype),.          
d0f90 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46   locktypeName(pF
d0fa0 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  ile->pLock->lock
d0fb0 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e 70 4c  type), pFile->pL
d0fc0 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20 20 72  ock->cnt);.    r
d0fd0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c  eturn rc;.  } el
d0fe0 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  se {.    return 
d0ff0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d  SQLITE_OK;.  }.}
d1000 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 20 6e 6f  .#else  /* if no
d1010 74 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  t SQLITE_THREADS
d1020 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 4f 6e 20 73  AFE */.  /* On s
d1030 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 62  ingle-threaded b
d1040 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68 69 70  uilds, ownership
d1050 20 74 72 61 6e 73 66 65 72 20 69 73 20 61 20 6e   transfer is a n
d1060 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 6e 65  o-op */.# define
d1070 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68   transferOwnersh
d1080 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f 4b 0a  ip(X) SQLITE_OK.
d1090 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
d10a0 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 0a  _THREADSAFE */..
d10b0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
d10c0 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68  ine checks if th
d10d0 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45  ere is a RESERVE
d10e0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74  D lock held on t
d10f0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  he specified.** 
d1100 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20  file by this or 
d1110 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
d1120 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63  s. If such a loc
d1130 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a  k is held, set *
d1140 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20  pResOut.** to a 
d1150 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f  non-zero value o
d1160 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75  therwise *pResOu
d1170 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  t is set to zero
d1180 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
d1190 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  lue.** is set to
d11a0 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73   SQLITE_OK unles
d11b0 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f  s an I/O error o
d11c0 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63  ccurs during loc
d11d0 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73  k checking..*/.s
d11e0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 68  tatic int unixCh
d11f0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
d1200 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d1210 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b  , int *pResOut){
d1220 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d1230 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73  TE_OK;.  int res
d1240 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69  erved = 0;.  uni
d1250 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
d1260 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  unixFile*)id;.. 
d1270 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d1280 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
d1290 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
d12a0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a 20 20 61  VEDLOCK; );..  a
d12b0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
d12c0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
d12d0 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 20 70  (); /* Because p
d12e0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73  File->pLock is s
d12f0 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72  hared across thr
d1300 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68  eads */..  /* Ch
d1310 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20  eck if a thread 
d1320 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
d1330 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63  holds such a loc
d1340 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  k */.  if( pFile
d1350 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70  ->pLock->locktyp
d1360 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  e>SHARED_LOCK ){
d1370 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20  .    reserved = 
d1380 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 68  1;.  }..  /* Oth
d1390 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f  erwise see if so
d13a0 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
d13b0 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a 2f 0a   holds it..  */.
d13c0 23 69 66 6e 64 65 66 20 5f 5f 44 4a 47 50 50 5f  #ifndef __DJGPP_
d13d0 5f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65  _.  if( !reserve
d13e0 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  d ){.    struct 
d13f0 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 20  flock lock;.    
d1400 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
d1410 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f  SEEK_SET;.    lo
d1420 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53  ck.l_start = RES
d1430 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20  ERVED_BYTE;.    
d1440 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a  lock.l_len = 1;.
d1450 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
d1460 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 69  = F_WRLCK;.    i
d1470 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 70  f (-1 == fcntl(p
d1480 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b  File->h, F_GETLK
d1490 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 20  , &lock)) {.    
d14a0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
d14b0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d  rrno;.      rc =
d14c0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d14d0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d14e0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d14f0 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43  CHECKRESERVEDLOC
d1500 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  K);.      pFile-
d1510 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d1520 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20  rno;.    } else 
d1530 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21  if( lock.l_type!
d1540 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_UNLCK ){.    
d1550 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
d1560 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
d1570 0a 20 20 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d  .  .  unixLeaveM
d1580 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43  utex();.  OSTRAC
d1590 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E4("TEST WR-LOCK
d15a0 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46   %d %d %d\n", pF
d15b0 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65  ile->h, rc, rese
d15c0 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f  rved);..  *pResO
d15d0 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20  ut = reserved;. 
d15e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
d15f0 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 66  *.** Perform a f
d1600 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72  ile locking oper
d1610 61 74 69 6f 6e 20 6f 6e 20 61 20 72 61 6e 67 65  ation on a range
d1620 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 66   of bytes in a f
d1630 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 22 6f 70 22  ile..** The "op"
d1640 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c   parameter shoul
d1650 64 20 62 65 20 6f 6e 65 20 6f 66 20 46 5f 52 44  d be one of F_RD
d1660 4c 43 4b 2c 20 46 5f 57 52 4c 43 4b 2c 20 6f 72  LCK, F_WRLCK, or
d1670 20 46 5f 55 4e 4c 43 4b 2e 0a 2a 2a 20 52 65 74   F_UNLCK..** Ret
d1680 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73  urn 0 on success
d1690 20 6f 72 20 2d 31 20 66 6f 72 20 66 61 69 6c 75   or -1 for failu
d16a0 72 65 2e 20 20 4f 6e 20 66 61 69 6c 75 72 65 2c  re.  On failure,
d16b0 20 77 72 69 74 65 20 74 68 65 20 65 72 72 6f 72   write the error
d16c0 0a 2a 2a 20 63 6f 64 65 20 69 6e 74 6f 20 2a 70  .** code into *p
d16d0 45 72 72 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49  Errcode..**.** I
d16e0 66 20 74 68 65 20 53 51 4c 49 54 45 5f 57 48 4f  f the SQLITE_WHO
d16f0 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20  LE_FILE_LOCKING 
d1700 62 69 74 20 69 73 20 63 6c 65 61 72 2c 20 74 68  bit is clear, th
d1710 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 0a 2a 2a 20  en only lock.** 
d1720 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74  the range of byt
d1730 65 73 20 6f 6e 20 74 68 65 20 6c 6f 63 6b 69 6e  es on the lockin
d1740 67 20 70 61 67 65 20 62 65 74 77 65 65 6e 20 53  g page between S
d1750 48 41 52 45 44 5f 46 49 52 53 54 20 61 6e 64 0a  HARED_FIRST and.
d1760 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 2e 20  ** SHARED_SIZE. 
d1770 20 49 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45   If SQLITE_WHOLE
d1780 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73  _FILE_LOCKING is
d1790 20 73 65 74 2c 20 74 68 65 6e 20 6c 6f 63 6b 20   set, then lock 
d17a0 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 66 72 6f  all.** bytes fro
d17b0 6d 20 30 20 75 70 20 74 6f 20 62 75 74 20 6e 6f  m 0 up to but no
d17c0 74 20 69 6e 63 6c 75 64 69 6e 67 20 50 45 4e 44  t including PEND
d17d0 49 4e 47 5f 42 59 54 45 2c 20 61 6e 64 20 61 6c  ING_BYTE, and al
d17e0 6c 20 62 79 74 65 73 0a 2a 2a 20 74 68 61 74 20  l bytes.** that 
d17f0 66 6f 6c 6c 6f 77 20 53 48 41 52 45 44 5f 46 49  follow SHARED_FI
d1800 52 53 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74  RST..**.** In ot
d1810 68 65 72 20 77 6f 72 64 73 2c 20 6f 66 20 53 51  her words, of SQ
d1820 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f  LITE_WHOLE_FILE_
d1830 4c 4f 43 4b 49 4e 47 20 69 66 20 66 61 6c 73 65  LOCKING if false
d1840 20 28 74 68 65 20 68 69 73 74 6f 72 69 63 61 6c   (the historical
d1850 0a 2a 2a 20 64 65 66 61 75 6c 74 20 63 61 73 65  .** default case
d1860 29 20 74 68 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b  ) then only lock
d1870 20 61 20 73 6d 61 6c 6c 20 72 61 6e 67 65 20 6f   a small range o
d1880 66 20 62 79 74 65 73 20 66 72 6f 6d 20 53 48 41  f bytes from SHA
d1890 52 45 44 5f 46 49 52 53 54 0a 2a 2a 20 74 68 72  RED_FIRST.** thr
d18a0 6f 75 67 68 20 53 48 41 52 45 44 5f 46 49 52 53  ough SHARED_FIRS
d18b0 54 2b 53 48 41 52 45 44 5f 53 49 5a 45 2d 31 2e  T+SHARED_SIZE-1.
d18c0 20 20 42 75 74 20 69 66 20 53 51 4c 49 54 45 5f    But if SQLITE_
d18d0 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49  WHOLE_FILE_LOCKI
d18e0 4e 47 20 69 73 0a 2a 2a 20 74 72 75 65 20 74 68  NG is.** true th
d18f0 65 6e 20 6c 6f 63 6b 20 65 76 65 72 79 20 62 79  en lock every by
d1900 74 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 65  te in the file e
d1910 78 63 65 70 74 20 66 6f 72 20 50 45 4e 44 49 4e  xcept for PENDIN
d1920 47 5f 42 59 54 45 20 61 6e 64 0a 2a 2a 20 52 45  G_BYTE and.** RE
d1930 53 45 52 56 45 44 5f 42 59 54 45 2e 0a 2a 2a 0a  SERVED_BYTE..**.
d1940 2a 2a 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f  ** SQLITE_WHOLE_
d1950 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75  FILE_LOCKING=tru
d1960 65 20 6f 76 65 72 6c 61 70 73 20 53 51 4c 49 54  e overlaps SQLIT
d1970 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
d1980 4b 49 4e 47 3d 66 61 6c 73 65 0a 2a 2a 20 61 6e  KING=false.** an
d1990 64 20 73 6f 20 74 68 65 20 6c 6f 63 6b 69 6e 67  d so the locking
d19a0 20 73 63 68 65 6d 65 73 20 61 72 65 20 63 6f 6d   schemes are com
d19b0 70 61 74 69 62 6c 65 2e 20 20 4f 6e 65 20 74 79  patible.  One ty
d19c0 70 65 20 6f 66 20 6c 6f 63 6b 20 77 69 6c 6c 0a  pe of lock will.
d19d0 2a 2a 20 65 66 66 65 63 74 69 76 65 6c 79 20 65  ** effectively e
d19e0 78 63 6c 75 64 65 20 74 68 65 20 6f 74 68 65 72  xclude the other
d19f0 20 74 79 70 65 2e 20 20 54 68 65 20 72 65 61 73   type.  The reas
d1a00 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 74 68 65  on for using the
d1a10 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45  .** SQLITE_WHOLE
d1a20 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72  _FILE_LOCKING=tr
d1a30 75 65 20 69 73 20 74 68 61 74 20 62 79 20 69 6e  ue is that by in
d1a40 64 69 63 61 74 69 6e 67 20 74 68 65 20 66 75 6c  dicating the ful
d1a50 6c 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 62 79  l range.** of by
d1a60 74 65 73 20 74 6f 20 62 65 20 72 65 61 64 20 6f  tes to be read o
d1a70 72 20 77 72 69 74 74 65 6e 2c 20 77 65 20 67 69  r written, we gi
d1a80 76 65 20 68 69 6e 74 73 20 74 6f 20 4e 46 53 20  ve hints to NFS 
d1a90 74 6f 20 68 65 6c 70 20 69 74 0a 2a 2a 20 6d 61  to help it.** ma
d1aa0 69 6e 74 61 69 6e 20 63 61 63 68 65 20 63 6f 68  intain cache coh
d1ab0 65 72 65 6e 63 79 2e 20 20 4f 6e 20 74 68 65 20  erency.  On the 
d1ac0 6f 74 68 65 72 20 68 61 6e 64 2c 20 77 68 6f 6c  other hand, whol
d1ad0 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a  e file locking.*
d1ae0 2a 20 69 73 20 73 6c 6f 77 65 72 2c 20 73 6f 20  * is slower, so 
d1af0 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f  we don't want to
d1b00 20 75 73 65 20 69 74 20 65 78 63 65 70 74 20 66   use it except f
d1b10 6f 72 20 4e 46 53 2e 0a 2a 2f 0a 73 74 61 74 69  or NFS..*/.stati
d1b20 63 20 69 6e 74 20 72 61 6e 67 65 4c 6f 63 6b 28  c int rangeLock(
d1b30 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c  unixFile *pFile,
d1b40 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 45   int op, int *pE
d1b50 72 72 63 6f 64 65 29 7b 0a 20 20 73 74 72 75 63  rrcode){.  struc
d1b60 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20  t flock lock;.  
d1b70 69 6e 74 20 72 63 3b 0a 20 20 6c 6f 63 6b 2e 6c  int rc;.  lock.l
d1b80 5f 74 79 70 65 20 3d 20 6f 70 3b 0a 20 20 6c 6f  _type = op;.  lo
d1b90 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41  ck.l_start = SHA
d1ba0 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 6c 6f 63  RED_FIRST;.  loc
d1bb0 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  k.l_whence = SEE
d1bc0 4b 5f 53 45 54 3b 0a 20 20 69 66 28 20 28 70 46  K_SET;.  if( (pF
d1bd0 69 6c 65 2d 3e 66 69 6c 65 46 6c 61 67 73 20 26  ile->fileFlags &
d1be0 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49   SQLITE_WHOLE_FI
d1bf0 4c 45 5f 4c 4f 43 4b 49 4e 47 29 3d 3d 30 20 29  LE_LOCKING)==0 )
d1c00 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e  {.    lock.l_len
d1c10 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a   = SHARED_SIZE;.
d1c20 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70      rc = fcntl(p
d1c30 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b  File->h, F_SETLK
d1c40 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70  , &lock);.    *p
d1c50 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b  Errcode = errno;
d1c60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f  .  }else{.    lo
d1c70 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20  ck.l_len = 0;.  
d1c80 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69    rc = fcntl(pFi
d1c90 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  le->h, F_SETLK, 
d1ca0 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72  &lock);.    *pEr
d1cb0 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20  rcode = errno;. 
d1cc0 20 20 20 69 66 28 20 4e 45 56 45 52 28 6f 70 3d     if( NEVER(op=
d1cd0 3d 46 5f 55 4e 4c 43 4b 29 20 7c 7c 20 72 63 21  =F_UNLCK) || rc!
d1ce0 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 6c  =(-1) ){.      l
d1cf0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b  ock.l_start = 0;
d1d00 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65  .      lock.l_le
d1d10 6e 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  n = PENDING_BYTE
d1d20 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 63 6e  ;.      rc = fcn
d1d30 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53  tl(pFile->h, F_S
d1d40 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20  ETLK, &lock);.  
d1d50 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 6f      if( ALWAYS(o
d1d60 70 21 3d 46 5f 55 4e 4c 43 4b 29 20 26 26 20 72  p!=F_UNLCK) && r
d1d70 63 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  c==(-1) ){.     
d1d80 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 65     *pErrcode = e
d1d90 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 6c 6f  rrno;.        lo
d1da0 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e  ck.l_type = F_UN
d1db0 4c 43 4b 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63  LCK;.        loc
d1dc0 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52  k.l_start = SHAR
d1dd0 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20  ED_FIRST;.      
d1de0 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30    lock.l_len = 0
d1df0 3b 0a 20 20 20 20 20 20 20 20 66 63 6e 74 6c 28  ;.        fcntl(
d1e00 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c  pFile->h, F_SETL
d1e10 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20  K, &lock);.     
d1e20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
d1e30 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d1e40 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
d1e50 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
d1e60 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
d1e70 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d  meter locktype -
d1e80 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
d1e90 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
d1ea0 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
d1eb0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
d1ec0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
d1ed0 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
d1ee0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
d1ef0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
d1f00 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
d1f10 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
d1f20 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
d1f30 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
d1f40 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
d1f50 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
d1f60 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
d1f70 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
d1f80 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
d1f90 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
d1fa0 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
d1fb0 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
d1fc0 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
d1fd0 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
d1fe0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
d1ff0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
d2000 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
d2010 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
d2020 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
d2030 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
d2040 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
d2050 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
d2060 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
d2070 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
d2080 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
d2090 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d20a0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d20b0 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
d20c0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
d20d0 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
d20e0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
d20f0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
d2100 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72  e will only incr
d2110 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73  ease a lock.  Us
d2120 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55  e the sqlite3OsU
d2130 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69  nlock().** routi
d2140 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f  ne to lower a lo
d2150 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a  cking level..*/.
d2160 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4c  static int unixL
d2170 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d2180 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
d2190 70 65 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66 6f  pe){.  /* The fo
d21a0 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69 62 65  llowing describe
d21b0 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
d21c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 72 69  tion of the vari
d21d0 6f 75 73 20 6c 6f 63 6b 73 20 61 6e 64 0a 20 20  ous locks and.  
d21e0 2a 2a 20 6c 6f 63 6b 20 74 72 61 6e 73 69 74 69  ** lock transiti
d21f0 6f 6e 73 20 69 6e 20 74 65 72 6d 73 20 6f 66 20  ons in terms of 
d2200 74 68 65 20 50 4f 53 49 58 20 61 64 76 69 73 6f  the POSIX adviso
d2210 72 79 20 73 68 61 72 65 64 20 61 6e 64 20 65 78  ry shared and ex
d2220 63 6c 75 73 69 76 65 0a 20 20 2a 2a 20 6c 6f 63  clusive.  ** loc
d2230 6b 20 70 72 69 6d 69 74 69 76 65 73 20 28 63 61  k primitives (ca
d2240 6c 6c 65 64 20 72 65 61 64 2d 6c 6f 63 6b 73 20  lled read-locks 
d2250 61 6e 64 20 77 72 69 74 65 2d 6c 6f 63 6b 73 20  and write-locks 
d2260 62 65 6c 6f 77 2c 20 74 6f 20 61 76 6f 69 64 0a  below, to avoid.
d2270 20 20 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 77    ** confusion w
d2280 69 74 68 20 53 51 4c 69 74 65 20 6c 6f 63 6b 20  ith SQLite lock 
d2290 6e 61 6d 65 73 29 2e 20 54 68 65 20 61 6c 67 6f  names). The algo
d22a0 72 69 74 68 6d 73 20 61 72 65 20 63 6f 6d 70 6c  rithms are compl
d22b0 69 63 61 74 65 64 0a 20 20 2a 2a 20 73 6c 69 67  icated.  ** slig
d22c0 68 74 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f  htly in order to
d22d0 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77   be compatible w
d22e0 69 74 68 20 77 69 6e 64 6f 77 73 20 73 79 73 74  ith windows syst
d22f0 65 6d 73 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73  ems simultaneous
d2300 6c 79 0a 20 20 2a 2a 20 61 63 63 65 73 73 69 6e  ly.  ** accessin
d2310 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  g the same datab
d2320 61 73 65 20 66 69 6c 65 2c 20 69 6e 20 63 61 73  ase file, in cas
d2330 65 20 74 68 61 74 20 69 73 20 65 76 65 72 20 72  e that is ever r
d2340 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a 20 20  equired..  **.  
d2350 2a 2a 20 53 79 6d 62 6f 6c 73 20 64 65 66 69 6e  ** Symbols defin
d2360 65 64 20 69 6e 20 6f 73 2e 68 20 69 6e 64 65 6e  ed in os.h inden
d2370 74 69 66 79 20 74 68 65 20 27 70 65 6e 64 69 6e  tify the 'pendin
d2380 67 20 62 79 74 65 27 20 61 6e 64 20 74 68 65 20  g byte' and the 
d2390 27 72 65 73 65 72 76 65 64 0a 20 20 2a 2a 20 62  'reserved.  ** b
d23a0 79 74 65 27 2c 20 65 61 63 68 20 73 69 6e 67 6c  yte', each singl
d23b0 65 20 62 79 74 65 73 20 61 74 20 77 65 6c 6c 20  e bytes at well 
d23c0 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 73 2c 20 61  known offsets, a
d23d0 6e 64 20 74 68 65 20 27 73 68 61 72 65 64 20 62  nd the 'shared b
d23e0 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2c  yte.  ** range',
d23f0 20 61 20 72 61 6e 67 65 20 6f 66 20 35 31 30 20   a range of 510 
d2400 62 79 74 65 73 20 61 74 20 61 20 77 65 6c 6c 20  bytes at a well 
d2410 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e 0a 20 20  known offset..  
d2420 2a 2a 0a 20 20 2a 2a 20 54 6f 20 6f 62 74 61 69  **.  ** To obtai
d2430 6e 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c  n a SHARED lock,
d2440 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20   a read-lock is 
d2450 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20  obtained on the 
d2460 27 70 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 62 79  'pending.  ** by
d2470 74 65 27 2e 20 20 49 66 20 74 68 69 73 20 69 73  te'.  If this is
d2480 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 20 72   successful, a r
d2490 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20  andom byte from 
d24a0 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65  the 'shared byte
d24b0 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20  .  ** range' is 
d24c0 72 65 61 64 2d 6c 6f 63 6b 65 64 20 61 6e 64 20  read-locked and 
d24d0 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  the lock on the 
d24e0 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 72  'pending byte' r
d24f0 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2a 0a 20 20  eleased..  **.  
d2500 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d 61 79  ** A process may
d2510 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 52   only obtain a R
d2520 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 66 74  ESERVED lock aft
d2530 65 72 20 69 74 20 68 61 73 20 61 20 53 48 41 52  er it has a SHAR
d2540 45 44 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 41 20  ED lock..  ** A 
d2550 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73  RESERVED lock is
d2560 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
d2570 67 72 61 62 62 69 6e 67 20 61 20 77 72 69 74 65  grabbing a write
d2580 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a  -lock on the.  *
d2590 2a 20 27 72 65 73 65 72 76 65 64 20 62 79 74 65  * 'reserved byte
d25a0 27 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20  '. .  **.  ** A 
d25b0 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79  process may only
d25c0 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e   obtain a PENDIN
d25d0 47 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20  G lock after it 
d25e0 68 61 73 20 6f 62 74 61 69 6e 65 64 20 61 0a 20  has obtained a. 
d25f0 20 2a 2a 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e   ** SHARED lock.
d2600 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   A PENDING lock 
d2610 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  is implemented b
d2620 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 72  y obtaining a wr
d2630 69 74 65 2d 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e  ite-lock.  ** on
d2640 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79   the 'pending by
d2650 74 65 27 2e 20 54 68 69 73 20 65 6e 73 75 72 65  te'. This ensure
d2660 73 20 74 68 61 74 20 6e 6f 20 6e 65 77 20 53 48  s that no new SH
d2670 41 52 45 44 20 6c 6f 63 6b 73 20 63 61 6e 20 62  ARED locks can b
d2680 65 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 65 64 2c  e.  ** obtained,
d2690 20 62 75 74 20 65 78 69 73 74 69 6e 67 20 53 48   but existing SH
d26a0 41 52 45 44 20 6c 6f 63 6b 73 20 61 72 65 20 61  ARED locks are a
d26b0 6c 6c 6f 77 65 64 20 74 6f 20 70 65 72 73 69 73  llowed to persis
d26c0 74 2e 20 41 20 70 72 6f 63 65 73 73 0a 20 20 2a  t. A process.  *
d26d0 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  * does not have 
d26e0 74 6f 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45  to obtain a RESE
d26f0 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  RVED lock on the
d2700 20 77 61 79 20 74 6f 20 61 20 50 45 4e 44 49 4e   way to a PENDIN
d2710 47 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 54 68 69  G lock..  ** Thi
d2720 73 20 70 72 6f 70 65 72 74 79 20 69 73 20 75 73  s property is us
d2730 65 64 20 62 79 20 74 68 65 20 61 6c 67 6f 72 69  ed by the algori
d2740 74 68 6d 20 66 6f 72 20 72 6f 6c 6c 69 6e 67 20  thm for rolling 
d2750 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66  back a journal f
d2760 69 6c 65 0a 20 20 2a 2a 20 61 66 74 65 72 20 61  ile.  ** after a
d2770 20 63 72 61 73 68 2e 0a 20 20 2a 2a 0a 20 20 2a   crash..  **.  *
d2780 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  * An EXCLUSIVE l
d2790 6f 63 6b 2c 20 6f 62 74 61 69 6e 65 64 20 61 66  ock, obtained af
d27a0 74 65 72 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  ter a PENDING lo
d27b0 63 6b 20 69 73 20 68 65 6c 64 2c 20 69 73 0a 20  ck is held, is. 
d27c0 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   ** implemented 
d27d0 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 20 77  by obtaining a w
d27e0 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite-lock on the
d27f0 20 65 6e 74 69 72 65 20 27 73 68 61 72 65 64 20   entire 'shared 
d2800 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27  byte.  ** range'
d2810 2e 20 53 69 6e 63 65 20 61 6c 6c 20 6f 74 68 65  . Since all othe
d2820 72 20 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 20  r locks require 
d2830 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 6f  a read-lock on o
d2840 6e 65 20 6f 66 20 74 68 65 20 62 79 74 65 73 0a  ne of the bytes.
d2850 20 20 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73    ** within this
d2860 20 72 61 6e 67 65 2c 20 74 68 69 73 20 65 6e 73   range, this ens
d2870 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6f 74 68  ures that no oth
d2880 65 72 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c  er locks are hel
d2890 64 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61  d on the.  ** da
d28a0 74 61 62 61 73 65 2e 20 0a 20 20 2a 2a 0a 20 20  tabase. .  **.  
d28b0 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 61 20  ** The reason a 
d28c0 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61 6e 6e  single byte cann
d28d0 6f 74 20 62 65 20 75 73 65 64 20 69 6e 73 74 65  ot be used inste
d28e0 61 64 20 6f 66 20 74 68 65 20 27 73 68 61 72 65  ad of the 'share
d28f0 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67  d byte.  ** rang
d2900 65 27 20 69 73 20 74 68 61 74 20 73 6f 6d 65 20  e' is that some 
d2910 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64  versions of wind
d2920 6f 77 73 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f  ows do not suppo
d2930 72 74 20 72 65 61 64 2d 6c 6f 63 6b 73 2e 20 42  rt read-locks. B
d2940 79 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61  y.  ** locking a
d2950 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f   random byte fro
d2960 6d 20 61 20 72 61 6e 67 65 2c 20 63 6f 6e 63 75  m a range, concu
d2970 72 72 65 6e 74 20 53 48 41 52 45 44 20 6c 6f 63  rrent SHARED loc
d2980 6b 73 20 6d 61 79 20 65 78 69 73 74 0a 20 20 2a  ks may exist.  *
d2990 2a 20 65 76 65 6e 20 69 66 20 74 68 65 20 6c 6f  * even if the lo
d29a0 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 20  cking primitive 
d29b0 75 73 65 64 20 69 73 20 61 6c 77 61 79 73 20 61  used is always a
d29c0 20 77 72 69 74 65 2d 6c 6f 63 6b 2e 0a 20 20 2a   write-lock..  *
d29d0 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
d29e0 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69  ITE_OK;.  unixFi
d29f0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d2a00 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 74 72  xFile*)id;.  str
d2a10 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  uct unixLockInfo
d2a20 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d   *pLock = pFile-
d2a30 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74  >pLock;.  struct
d2a40 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69   flock lock;.  i
d2a50 6e 74 20 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20  nt s = 0;.  int 
d2a60 74 45 72 72 6e 6f 3b 0a 0a 20 20 61 73 73 65 72  tErrno;..  asser
d2a70 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53  t( pFile );.  OS
d2a80 54 52 41 43 45 37 28 22 4c 4f 43 4b 20 20 20 20  TRACE7("LOCK    
d2a90 25 64 20 25 73 20 77 61 73 20 25 73 28 25 73 2c  %d %s was %s(%s,
d2aa0 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70  %d) pid=%d\n", p
d2ab0 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 6c  File->h,.      l
d2ac0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b  ocktypeName(lock
d2ad0 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e  type), locktypeN
d2ae0 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  ame(pFile->lockt
d2af0 79 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63 6b  ype),.      lock
d2b00 74 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e  typeName(pLock->
d2b10 6c 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63 6b  locktype), pLock
d2b20 2d 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28 29  ->cnt , getpid()
d2b30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  );..  /* If ther
d2b40 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c  e is already a l
d2b50 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65  ock of this type
d2b60 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63   or more restric
d2b70 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a  tive on the.  **
d2b80 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f   unixFile, do no
d2b90 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65  thing. Don't use
d2ba0 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65   the end_lock: e
d2bb0 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a  xit path, as.  *
d2bc0 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78  * unixEnterMutex
d2bd0 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63  () hasn't been c
d2be0 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a  alled yet..  */.
d2bf0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
d2c00 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20  ktype>=locktype 
d2c10 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  ){.    OSTRACE3(
d2c20 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f  "LOCK    %d %s o
d2c30 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29  k (already held)
d2c40 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20  \n", pFile->h,. 
d2c50 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74             lockt
d2c60 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65  ypeName(locktype
d2c70 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ));.    return S
d2c80 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
d2c90 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
d2ca0 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e  e locking sequen
d2cb0 63 65 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 20  ce is correct.. 
d2cc0 20 2a 2a 20 20 28 31 29 20 57 65 20 6e 65 76 65   **  (1) We neve
d2cd0 72 20 6d 6f 76 65 20 66 72 6f 6d 20 75 6e 6c 6f  r move from unlo
d2ce0 63 6b 65 64 20 74 6f 20 61 6e 79 74 68 69 6e 67  cked to anything
d2cf0 20 68 69 67 68 65 72 20 74 68 61 6e 20 73 68 61   higher than sha
d2d00 72 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20  red lock..  **  
d2d10 28 32 29 20 53 51 4c 69 74 65 20 6e 65 76 65 72  (2) SQLite never
d2d20 20 65 78 70 6c 69 63 69 74 6c 79 20 72 65 71 75   explicitly requ
d2d30 65 73 74 73 20 61 20 70 65 6e 64 69 67 20 6c 6f  ests a pendig lo
d2d40 63 6b 2e 0a 20 20 2a 2a 20 20 28 33 29 20 41 20  ck..  **  (3) A 
d2d50 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 73 20 61  shared lock is a
d2d60 6c 77 61 79 73 20 68 65 6c 64 20 77 68 65 6e 20  lways held when 
d2d70 61 20 72 65 73 65 72 76 65 20 6c 6f 63 6b 20 69  a reserve lock i
d2d80 73 20 72 65 71 75 65 73 74 65 64 2e 0a 20 20 2a  s requested..  *
d2d90 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  /.  assert( pFil
d2da0 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f  e->locktype!=NO_
d2db0 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65  LOCK || locktype
d2dc0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
d2dd0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
d2de0 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype!=PENDING_LOC
d2df0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  K );.  assert( l
d2e00 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45  ocktype!=RESERVE
d2e10 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d  D_LOCK || pFile-
d2e20 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
d2e30 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20  D_LOCK );..  /* 
d2e40 54 68 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65  This mutex is ne
d2e50 65 64 65 64 20 62 65 63 61 75 73 65 20 70 46 69  eded because pFi
d2e60 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61  le->pLock is sha
d2e70 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61  red across threa
d2e80 64 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e  ds.  */.  unixEn
d2e90 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f  terMutex();..  /
d2ea0 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
d2eb0 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6f  current thread o
d2ec0 77 6e 73 20 74 68 65 20 70 46 69 6c 65 2e 0a 20  wns the pFile.. 
d2ed0 20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73   */.  rc = trans
d2ee0 66 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69  ferOwnership(pFi
d2ef0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  le);.  if( rc!=S
d2f00 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
d2f10 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
d2f20 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
d2f30 0a 20 20 7d 0a 20 20 70 4c 6f 63 6b 20 3d 20 70  .  }.  pLock = p
d2f40 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20  File->pLock;..  
d2f50 2f 2a 20 49 66 20 73 6f 6d 65 20 74 68 72 65 61  /* If some threa
d2f60 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44  d using this PID
d2f70 20 68 61 73 20 61 20 6c 6f 63 6b 20 76 69 61 20   has a lock via 
d2f80 61 20 64 69 66 66 65 72 65 6e 74 20 75 6e 69 78  a different unix
d2f90 46 69 6c 65 2a 0a 20 20 2a 2a 20 68 61 6e 64 6c  File*.  ** handl
d2fa0 65 20 74 68 61 74 20 70 72 65 63 6c 75 64 65 73  e that precludes
d2fb0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
d2fc0 6f 63 6b 2c 20 72 65 74 75 72 6e 20 42 55 53 59  ock, return BUSY
d2fd0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 46  ..  */.  if( (pF
d2fe0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 70  ile->locktype!=p
d2ff0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 26  Lock->locktype &
d3000 26 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 4c  & .          (pL
d3010 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50  ock->locktype>=P
d3020 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 6c  ENDING_LOCK || l
d3030 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
d3040 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20 72  OCK)).  ){.    r
d3050 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
d3060 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f  .    goto end_lo
d3070 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  ck;.  }..  /* If
d3080 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 69   a SHARED lock i
d3090 73 20 72 65 71 75 65 73 74 65 64 2c 20 61 6e 64  s requested, and
d30a0 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73 69   some thread usi
d30b0 6e 67 20 74 68 69 73 20 50 49 44 20 61 6c 72 65  ng this PID alre
d30c0 61 64 79 0a 20 20 2a 2a 20 68 61 73 20 61 20 53  ady.  ** has a S
d30d0 48 41 52 45 44 20 6f 72 20 52 45 53 45 52 56 45  HARED or RESERVE
d30e0 44 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 69 6e 63  D lock, then inc
d30f0 72 65 6d 65 6e 74 20 72 65 66 65 72 65 6e 63 65  rement reference
d3100 20 63 6f 75 6e 74 73 20 61 6e 64 0a 20 20 2a 2a   counts and.  **
d3110 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d3120 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  K..  */.  if( lo
d3130 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
d3140 4f 43 4b 20 26 26 20 0a 20 20 20 20 20 20 28 70  OCK && .      (p
d3150 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  Lock->locktype==
d3160 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70  SHARED_LOCK || p
d3170 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  Lock->locktype==
d3180 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 29  RESERVED_LOCK) )
d3190 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 6f  {.    assert( lo
d31a0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
d31b0 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72  OCK );.    asser
d31c0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
d31d0 70 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  pe==0 );.    ass
d31e0 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e  ert( pLock->cnt>
d31f0 30 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  0 );.    pFile->
d3200 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  locktype = SHARE
d3210 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f 63  D_LOCK;.    pLoc
d3220 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 70 46  k->cnt++;.    pF
d3230 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63  ile->pOpen->nLoc
d3240 6b 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e  k++;.    goto en
d3250 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 0a 20 20  d_lock;.  }...  
d3260 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63  /* A PENDING loc
d3270 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f  k is needed befo
d3280 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53  re acquiring a S
d3290 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62  HARED lock and b
d32a0 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75 69  efore.  ** acqui
d32b0 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56  ring an EXCLUSIV
d32c0 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65  E lock.  For the
d32d0 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
d32e0 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20  e PENDING will. 
d32f0 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 2e   ** be released.
d3300 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c  .  */.  lock.l_l
d3310 65 6e 20 3d 20 31 4c 3b 0a 20 20 6c 6f 63 6b 2e  en = 1L;.  lock.
d3320 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
d3330 53 45 54 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 74  SET;.  if( lockt
d3340 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d3350 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b   .      || (lock
d3360 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
d3370 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c  LOCK && pFile->l
d3380 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f  ocktype<PENDING_
d3390 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 6c  LOCK).  ){.    l
d33a0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 28 6c 6f  ock.l_type = (lo
d33b0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
d33c0 4f 43 4b 3f 46 5f 52 44 4c 43 4b 3a 46 5f 57 52  OCK?F_RDLCK:F_WR
d33d0 4c 43 4b 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  LCK);.    lock.l
d33e0 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47  _start = PENDING
d33f0 5f 42 59 54 45 3b 0a 20 20 20 20 73 20 3d 20 66  _BYTE;.    s = f
d3400 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46  cntl(pFile->h, F
d3410 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a  _SETLK, &lock);.
d3420 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20      if( s==(-1) 
d3430 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f 20  ){.      tErrno 
d3440 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72  = errno;.      r
d3450 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d3460 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d3470 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d3480 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  RR_LOCK);.      
d3490 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
d34a0 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20  R(rc) ){.       
d34b0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d34c0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d34d0 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65    }.      goto e
d34e0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20  nd_lock;.    }. 
d34f0 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e   }...  /* If con
d3500 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69  trol gets to thi
d3510 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63  s point, then ac
d3520 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20  tually go ahead 
d3530 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70  and make.  ** op
d3540 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63  erating system c
d3550 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65  alls for the spe
d3560 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a  cified lock..  *
d3570 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  /.  if( locktype
d3580 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
d3590 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f  .    assert( pLo
d35a0 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20  ck->cnt==0 );.  
d35b0 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d    assert( pLock-
d35c0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a  >locktype==0 );.
d35d0 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20  .    /* Now get 
d35e0 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f  the read-lock */
d35f0 0a 20 20 20 20 73 20 3d 20 72 61 6e 67 65 4c 6f  .    s = rangeLo
d3600 63 6b 28 70 46 69 6c 65 2c 20 46 5f 52 44 4c 43  ck(pFile, F_RDLC
d3610 4b 2c 20 26 74 45 72 72 6e 6f 29 3b 0a 0a 20 20  K, &tErrno);..  
d3620 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 65    /* Drop the te
d3630 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47 20  mporary PENDING 
d3640 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b  lock */.    lock
d3650 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49  .l_start = PENDI
d3660 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63  NG_BYTE;.    loc
d3670 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20  k.l_len = 1L;.  
d3680 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
d3690 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69 66 28  F_UNLCK;.    if(
d36a0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
d36b0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d36c0 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  !=0 ){.      if(
d36d0 20 73 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20   s != -1 ){.    
d36e0 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f 75 6c      /* This coul
d36f0 64 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 20  d happen with a 
d3700 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20 2a 2f  network mount */
d3710 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f 20  .        tErrno 
d3720 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 20 20 20 20  = errno; .      
d3730 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d3740 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d3750 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d3760 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a  IOERR_UNLOCK); .
d3770 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c          if( IS_L
d3780 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
d3790 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  .          pFile
d37a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d37b0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rrno;.        }.
d37c0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
d37d0 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  _lock;.      }. 
d37e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 3d 3d     }.    if( s==
d37f0 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72 63  (-1) ){.      rc
d3800 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
d3810 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
d3820 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52  rno, SQLITE_IOER
d3830 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69  R_LOCK);.      i
d3840 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d3850 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (rc) ){.        
d3860 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d3870 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20   = tErrno;.     
d3880 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
d3890 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d38a0 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43  ype = SHARED_LOC
d38b0 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  K;.      pFile->
d38c0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a  pOpen->nLock++;.
d38d0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74        pLock->cnt
d38e0 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   = 1;.    }.  }e
d38f0 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  lse if( locktype
d3900 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
d3910 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 31   && pLock->cnt>1
d3920 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72   ){.    /* We ar
d3930 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 6e 20  e trying for an 
d3940 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62  exclusive lock b
d3950 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  ut another threa
d3960 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a  d in this.    **
d3970 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 69 73   same process is
d3980 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61   still holding a
d3990 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f   shared lock. */
d39a0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
d39b0 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _BUSY;.  }else{.
d39c0 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65      /* The reque
d39d0 73 74 20 77 61 73 20 66 6f 72 20 61 20 52 45 53  st was for a RES
d39e0 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49  ERVED or EXCLUSI
d39f0 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a  VE lock.  It is.
d3a00 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74      ** assumed t
d3a10 68 61 74 20 74 68 65 72 65 20 69 73 20 61 20 53  hat there is a S
d3a20 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 65 72  HARED or greater
d3a30 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c   lock on the fil
d3a40 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79  e.    ** already
d3a50 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
d3a60 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c  ert( 0!=pFile->l
d3a70 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 6c  ocktype );.    l
d3a80 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57  ock.l_type = F_W
d3a90 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69 74 63 68  RLCK;.    switch
d3aa0 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  ( locktype ){.  
d3ab0 20 20 20 20 63 61 73 65 20 52 45 53 45 52 56 45      case RESERVE
d3ac0 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 20 20  D_LOCK:.        
d3ad0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52  lock.l_start = R
d3ae0 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20  ESERVED_BYTE;.  
d3af0 20 20 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28        s = fcntl(
d3b00 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c  pFile->h, F_SETL
d3b10 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20  K, &lock);.     
d3b20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e     tErrno = errn
d3b30 6f 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  o;.        break
d3b40 3b 0a 20 20 20 20 20 20 63 61 73 65 20 45 58 43  ;.      case EXC
d3b50 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20  LUSIVE_LOCK:.   
d3b60 20 20 20 20 20 73 20 3d 20 72 61 6e 67 65 4c 6f       s = rangeLo
d3b70 63 6b 28 70 46 69 6c 65 2c 20 46 5f 57 52 4c 43  ck(pFile, F_WRLC
d3b80 4b 2c 20 26 74 45 72 72 6e 6f 29 3b 0a 20 20 20  K, &tErrno);.   
d3b90 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
d3ba0 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
d3bb0 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20     assert(0);.  
d3bc0 20 20 7d 0a 20 20 20 20 69 66 28 20 73 3d 3d 28    }.    if( s==(
d3bd0 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  -1) ){.      rc 
d3be0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d3bf0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d3c00 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
d3c10 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66  _LOCK);.      if
d3c20 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d3c30 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  rc) ){.        p
d3c40 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d3c50 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
d3c60 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a  }.    }.  }.  ..
d3c70 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
d3c80 20 2f 2a 20 53 65 74 20 75 70 20 74 68 65 20 74   /* Set up the t
d3c90 72 61 6e 73 61 63 74 69 6f 6e 2d 63 6f 75 6e 74  ransaction-count
d3ca0 65 72 20 63 68 61 6e 67 65 20 63 68 65 63 6b 69  er change checki
d3cb0 6e 67 20 66 6c 61 67 73 20 77 68 65 6e 0a 20 20  ng flags when.  
d3cc0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67  ** transitioning
d3cd0 20 66 72 6f 6d 20 61 20 53 48 41 52 45 44 20 74   from a SHARED t
d3ce0 6f 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  o a RESERVED loc
d3cf0 6b 2e 20 20 54 68 65 20 63 68 61 6e 67 65 0a 20  k.  The change. 
d3d00 20 2a 2a 20 66 72 6f 6d 20 53 48 41 52 45 44 20   ** from SHARED 
d3d10 74 6f 20 52 45 53 45 52 56 45 44 20 6d 61 72 6b  to RESERVED mark
d3d20 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  s the beginning 
d3d30 6f 66 20 61 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a  of a normal.  **
d3d40 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e   write operation
d3d50 20 28 6e 6f 74 20 61 20 68 6f 74 20 6a 6f 75 72   (not a hot jour
d3d60 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20  nal rollback).. 
d3d70 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
d3d80 4c 49 54 45 5f 4f 4b 0a 20 20 20 26 26 20 70 46  LITE_OK.   && pF
d3d90 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53  ile->locktype<=S
d3da0 48 41 52 45 44 5f 4c 4f 43 4b 0a 20 20 20 26 26  HARED_LOCK.   &&
d3db0 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52   locktype==RESER
d3dc0 56 45 44 5f 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20  VED_LOCK.  ){.  
d3dd0 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e    pFile->transCn
d3de0 74 72 43 68 6e 67 20 3d 20 30 3b 0a 20 20 20 20  trChng = 0;.    
d3df0 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 20  pFile->dbUpdate 
d3e00 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  = 0;.    pFile->
d3e10 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20  inNormalWrite = 
d3e20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a  1;.  }.#endif...
d3e30 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
d3e40 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  _OK ){.    pFile
d3e50 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
d3e60 6b 74 79 70 65 3b 0a 20 20 20 20 70 4c 6f 63 6b  ktype;.    pLock
d3e70 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
d3e80 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69  ktype;.  }else i
d3e90 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
d3ea0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20  LUSIVE_LOCK ){. 
d3eb0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d3ec0 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43  pe = PENDING_LOC
d3ed0 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f  K;.    pLock->lo
d3ee0 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47  cktype = PENDING
d3ef0 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f  _LOCK;.  }..end_
d3f00 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76  lock:.  unixLeav
d3f10 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52  eMutex();.  OSTR
d3f20 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE4("LOCK    %d
d3f30 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65   %s %s\n", pFile
d3f40 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d  ->h, locktypeNam
d3f50 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20  e(locktype), .  
d3f60 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f      rc==SQLITE_O
d3f70 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c  K ? "ok" : "fail
d3f80 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ed");.  return r
d3f90 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  c;.}../*.** Clos
d3fa0 65 20 61 6c 6c 20 66 69 6c 65 20 64 65 73 63 72  e all file descr
d3fb0 69 70 74 6f 72 73 20 61 63 63 75 6d 75 61 74 65  iptors accumuate
d3fc0 64 20 69 6e 20 74 68 65 20 75 6e 69 78 4f 70 65  d in the unixOpe
d3fd0 6e 43 6e 74 2d 3e 70 55 6e 75 73 65 64 20 6c 69  nCnt->pUnused li
d3fe0 73 74 2e 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75  st..** If all su
d3ff0 63 68 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ch file descript
d4000 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 64 20 77  ors are closed w
d4010 69 74 68 6f 75 74 20 65 72 72 6f 72 2c 20 74 68  ithout error, th
d4020 65 20 6c 69 73 74 20 69 73 0a 2a 2a 20 63 6c 65  e list is.** cle
d4030 61 72 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f  ared and SQLITE_
d4040 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  OK returned..**.
d4050 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  ** Otherwise, if
d4060 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
d4070 2c 20 74 68 65 6e 20 73 75 63 63 65 73 73 66 75  , then successfu
d4080 6c 6c 79 20 63 6c 6f 73 65 64 20 66 69 6c 65 20  lly closed file 
d4090 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 65 6e  descriptor.** en
d40a0 74 72 69 65 73 20 61 72 65 20 72 65 6d 6f 76 65  tries are remove
d40b0 64 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 2c  d from the list,
d40c0 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52   and SQLITE_IOER
d40d0 52 5f 43 4c 4f 53 45 20 72 65 74 75 72 6e 65 64  R_CLOSE returned
d40e0 2e 20 0a 2a 2a 20 6e 6f 74 20 64 65 6c 65 74 65  . .** not delete
d40f0 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45  d and SQLITE_IOE
d4100 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 72 6e 65  RR_CLOSE returne
d4110 64 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 69 6e  d..*/ .static in
d4120 74 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64  t closePendingFd
d4130 73 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  s(unixFile *pFil
d4140 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
d4150 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 74 72 75  QLITE_OK;.  stru
d4160 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
d4170 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70  pOpen = pFile->p
d4180 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73  Open;.  UnixUnus
d4190 65 64 46 64 20 2a 70 45 72 72 6f 72 20 3d 20 30  edFd *pError = 0
d41a0 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64  ;.  UnixUnusedFd
d41b0 20 2a 70 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65   *p;.  UnixUnuse
d41c0 64 46 64 20 2a 70 4e 65 78 74 3b 0a 20 20 66 6f  dFd *pNext;.  fo
d41d0 72 28 70 3d 70 4f 70 65 6e 2d 3e 70 55 6e 75 73  r(p=pOpen->pUnus
d41e0 65 64 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b  ed; p; p=pNext){
d41f0 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e  .    pNext = p->
d4200 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 63  pNext;.    if( c
d4210 6c 6f 73 65 28 70 2d 3e 66 64 29 20 29 7b 0a 20  lose(p->fd) ){. 
d4220 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d4230 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d4240 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
d4250 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20  _IOERR_CLOSE;.  
d4260 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70      p->pNext = p
d4270 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 70 45 72  Error;.      pEr
d4280 72 6f 72 20 3d 20 70 3b 0a 20 20 20 20 7d 65 6c  ror = p;.    }el
d4290 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
d42a0 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 7d  3_free(p);.    }
d42b0 0a 20 20 7d 0a 20 20 70 4f 70 65 6e 2d 3e 70 55  .  }.  pOpen->pU
d42c0 6e 75 73 65 64 20 3d 20 70 45 72 72 6f 72 3b 0a  nused = pError;.
d42d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
d42e0 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 66 69  /*.** Add the fi
d42f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 75 73  le descriptor us
d4300 65 64 20 62 79 20 66 69 6c 65 20 68 61 6e 64 6c  ed by file handl
d4310 65 20 70 46 69 6c 65 20 74 6f 20 74 68 65 20 63  e pFile to the c
d4320 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
d4330 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f  pUnused list..*/
d4340 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
d4350 50 65 6e 64 69 6e 67 46 64 28 75 6e 69 78 46 69  PendingFd(unixFi
d4360 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 73 74  le *pFile){.  st
d4370 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74  ruct unixOpenCnt
d4380 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d   *pOpen = pFile-
d4390 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e  >pOpen;.  UnixUn
d43a0 75 73 65 64 46 64 20 2a 70 20 3d 20 70 46 69 6c  usedFd *p = pFil
d43b0 65 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 70 2d  e->pUnused;.  p-
d43c0 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e  >pNext = pOpen->
d43d0 70 55 6e 75 73 65 64 3b 0a 20 20 70 4f 70 65 6e  pUnused;.  pOpen
d43e0 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 3b 0a 20  ->pUnused = p;. 
d43f0 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a   pFile->h = -1;.
d4400 20 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64    pFile->pUnused
d4410 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c   = 0;.}../*.** L
d4420 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
d4430 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
d4440 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
d4450 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
d4460 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
d4470 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
d4480 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
d4490 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
d44a0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
d44b0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
d44c0 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
d44d0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
d44e0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
d44f0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
d4500 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
d4510 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
d4520 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69   unixUnlock(sqli
d4530 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
d4540 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 75  t locktype){.  u
d4550 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d4560 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 20   (unixFile*)id; 
d4570 2f 2a 20 54 68 65 20 6f 70 65 6e 20 66 69 6c 65  /* The open file
d4580 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
d4590 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b  xLockInfo *pLock
d45a0 3b 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74  ;      /* Struct
d45b0 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 20 63  ure describing c
d45c0 75 72 72 65 6e 74 20 6c 6f 63 6b 20 73 74 61 74  urrent lock stat
d45d0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 66 6c  e */.  struct fl
d45e0 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20  ock lock;       
d45f0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72          /* Infor
d4600 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e  mation passed in
d4610 74 6f 20 66 63 6e 74 6c 28 29 20 2a 2f 0a 20 20  to fcntl() */.  
d4620 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
d4630 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
d4640 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
d4650 66 72 6f 6d 20 74 68 69 73 20 69 6e 74 65 72 66  from this interf
d4660 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20  ace */.  int h; 
d4670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d4680 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
d4690 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65   underlying file
d46a0 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
d46b0 20 69 6e 74 20 74 45 72 72 6e 6f 3b 20 20 20 20   int tErrno;    
d46c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d46d0 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20    /* Error code 
d46e0 66 72 6f 6d 20 73 79 73 74 65 6d 20 63 61 6c 6c  from system call
d46f0 20 65 72 72 6f 72 73 20 2a 2f 0a 0a 20 20 61 73   errors */..  as
d4700 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
d4710 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c 4f 43   OSTRACE7("UNLOC
d4720 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 28  K  %d %d was %d(
d4730 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22  %d,%d) pid=%d\n"
d4740 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
d4750 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46 69 6c  type,.      pFil
d4760 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69  e->locktype, pFi
d4770 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74  le->pLock->lockt
d4780 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63  ype, pFile->pLoc
d4790 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64 28 29  k->cnt, getpid()
d47a0 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6c 6f  );..  assert( lo
d47b0 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
d47c0 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 46 69  OCK );.  if( pFi
d47d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f  le->locktype<=lo
d47e0 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65  cktype ){.    re
d47f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d4800 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43 4b 5f    }.  if( CHECK_
d4810 54 48 52 45 41 44 49 44 28 70 46 69 6c 65 29 20  THREADID(pFile) 
d4820 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
d4830 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
d4840 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65  .  unixEnterMute
d4850 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65  x();.  h = pFile
d4860 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70  ->h;.  pLock = p
d4870 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61  File->pLock;.  a
d4880 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e  ssert( pLock->cn
d4890 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46  t!=0 );.  if( pF
d48a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48  ile->locktype>SH
d48b0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d48c0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
d48d0 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d  locktype==pFile-
d48e0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  >locktype );.   
d48f0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d4900 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 53  Benign(1);.    S
d4910 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
d4920 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 69 6d  h=(-1) ).    Sim
d4930 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
d4940 67 6e 28 30 29 3b 0a 0a 23 69 66 6e 64 65 66 20  gn(0);..#ifndef 
d4950 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 57 68  NDEBUG.    /* Wh
d4960 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20 6c 6f  en reducing a lo
d4970 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f 74 68  ck such that oth
d4980 65 72 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e  er processes can
d4990 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 72 65   start.    ** re
d49a0 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
d49b0 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c 20 6d  se file again, m
d49c0 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
d49d0 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63  e.    ** transac
d49e0 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 73  tion counter was
d49f0 20 75 70 64 61 74 65 64 20 69 66 20 61 6e 79 20   updated if any 
d4a00 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
d4a10 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65  base.    ** file
d4a20 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 74 68   changed.  If th
d4a30 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
d4a40 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 75 70 64  unter is not upd
d4a50 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 6f 74 68  ated,.    ** oth
d4a60 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  er connections t
d4a70 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20  o the same file 
d4a80 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 6c 69 7a  might not realiz
d4a90 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 68  e that.    ** th
d4aa0 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67  e file has chang
d4ab0 65 64 20 61 6e 64 20 68 65 6e 63 65 20 6d 69 67  ed and hence mig
d4ac0 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 6f 20 66  ht not know to f
d4ad0 6c 75 73 68 20 74 68 65 69 72 0a 20 20 20 20 2a  lush their.    *
d4ae0 2a 20 63 61 63 68 65 2e 20 20 54 68 65 20 75 73  * cache.  The us
d4af0 65 20 6f 66 20 61 20 73 74 61 6c 65 20 63 61 63  e of a stale cac
d4b00 68 65 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 64  he can lead to d
d4b10 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
d4b20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  on..    */.    a
d4b30 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e  ssert( pFile->in
d4b40 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d 30 0a 20  NormalWrite==0. 
d4b50 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65          || pFile
d4b60 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0a 20 20  ->dbUpdate==0.  
d4b70 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d         || pFile-
d4b80 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d  >transCntrChng==
d4b90 31 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  1 );.    pFile->
d4ba0 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20  inNormalWrite = 
d4bb0 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20  0;.#endif...    
d4bc0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
d4bd0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d4be0 20 20 20 69 66 28 20 72 61 6e 67 65 4c 6f 63 6b     if( rangeLock
d4bf0 28 70 46 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c  (pFile, F_RDLCK,
d4c00 20 26 74 45 72 72 6e 6f 29 3d 3d 28 2d 31 29 20   &tErrno)==(-1) 
d4c10 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
d4c20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d4c30 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d4c40 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  , SQLITE_IOERR_R
d4c50 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20  DLOCK);.        
d4c60 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
d4c70 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20  R(rc) ){.       
d4c80 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
d4c90 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
d4ca0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
d4cb0 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b  goto end_unlock;
d4cc0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d4cd0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
d4ce0 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f   F_UNLCK;.    lo
d4cf0 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45  ck.l_whence = SE
d4d00 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b  EK_SET;.    lock
d4d10 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49  .l_start = PENDI
d4d20 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63  NG_BYTE;.    loc
d4d30 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61  k.l_len = 2L;  a
d4d40 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47 5f 42  ssert( PENDING_B
d4d50 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f  YTE+1==RESERVED_
d4d60 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66 28 20  BYTE );.    if( 
d4d70 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b  fcntl(h, F_SETLK
d4d80 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29  , &lock)!=(-1) )
d4d90 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c  {.      pLock->l
d4da0 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
d4db0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
d4dc0 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d  {.      tErrno =
d4dd0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63   errno;.      rc
d4de0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
d4df0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
d4e00 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52  rno, SQLITE_IOER
d4e10 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  R_UNLOCK);.     
d4e20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d4e30 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20  OR(rc) ){.      
d4e40 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d4e50 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d4e60 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20     }.      goto 
d4e70 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20  end_unlock;.    
d4e80 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b  }.  }.  if( lock
d4e90 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  type==NO_LOCK ){
d4ea0 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78  .    struct unix
d4eb0 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a  OpenCnt *pOpen;.
d4ec0 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e  .    /* Decremen
d4ed0 74 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  t the shared loc
d4ee0 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 6c 65  k counter.  Rele
d4ef0 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 73 69  ase the lock usi
d4f00 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f 53 20  ng an.    ** OS 
d4f10 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61  call only when a
d4f20 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20 74 68  ll threads in th
d4f30 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20  is same process 
d4f40 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a 20 20  have released.  
d4f50 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 20    ** the lock.. 
d4f60 20 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d     */.    pLock-
d4f70 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20  >cnt--;.    if( 
d4f80 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b  pLock->cnt==0 ){
d4f90 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79  .      lock.l_ty
d4fa0 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20  pe = F_UNLCK;.  
d4fb0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63      lock.l_whenc
d4fc0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
d4fd0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
d4fe0 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20   = lock.l_len = 
d4ff0 30 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61  0L;.      Simula
d5000 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
d5010 31 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61  1);.      Simula
d5020 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31  teIOError( h=(-1
d5030 29 20 29 0a 20 20 20 20 20 20 53 69 6d 75 6c 61  ) ).      Simula
d5040 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
d5050 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 63  0);.      if( fc
d5060 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  ntl(h, F_SETLK, 
d5070 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a  &lock)!=(-1) ){.
d5080 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c          pLock->l
d5090 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
d50a0 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  K;.      }else{.
d50b0 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d          tErrno =
d50c0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
d50d0 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
d50e0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
d50f0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
d5100 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20  ERR_UNLOCK);.   
d5110 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d5120 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d5130 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
d5140 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d5150 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  o;.        }.   
d5160 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b       pLock->lock
d5170 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
d5180 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
d5190 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
d51a0 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  K;.      }.    }
d51b0 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65  ..    /* Decreme
d51c0 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  nt the count of 
d51d0 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68  locks against th
d51e0 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57  is same file.  W
d51f0 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63  hen the.    ** c
d5200 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72  ount reaches zer
d5210 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68  o, close any oth
d5220 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  er file descript
d5230 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a  ors whose close.
d5240 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72      ** was defer
d5250 72 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f  red because of o
d5260 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73  utstanding locks
d5270 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70  ..    */.    pOp
d5280 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  en = pFile->pOpe
d5290 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c  n;.    pOpen->nL
d52a0 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72  ock--;.    asser
d52b0 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e  t( pOpen->nLock>
d52c0 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  =0 );.    if( pO
d52d0 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b  pen->nLock==0 ){
d52e0 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 20 3d  .      int rc2 =
d52f0 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73   closePendingFds
d5300 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69  (pFile);.      i
d5310 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
d5320 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
d5330 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20   rc2;.      }.  
d5340 20 20 7d 0a 20 20 7d 0a 09 0a 65 6e 64 5f 75 6e    }.  }...end_un
d5350 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76  lock:.  unixLeav
d5360 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20  eMutex();.  if( 
d5370 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc==SQLITE_OK ) 
d5380 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d5390 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65  = locktype;.  re
d53a0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
d53b0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
d53c0 70 65 72 66 6f 72 6d 73 20 74 68 65 20 70 61 72  performs the par
d53d0 74 73 20 6f 66 20 74 68 65 20 22 63 6c 6f 73 65  ts of the "close
d53e0 20 66 69 6c 65 22 20 6f 70 65 72 61 74 69 6f 6e   file" operation
d53f0 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61   .** common to a
d5400 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d  ll locking schem
d5410 65 73 2e 20 49 74 20 63 6c 6f 73 65 73 20 74 68  es. It closes th
d5420 65 20 64 69 72 65 63 74 6f 72 79 20 61 6e 64 20  e directory and 
d5430 66 69 6c 65 0a 2a 2a 20 68 61 6e 64 6c 65 73 2c  file.** handles,
d5440 20 69 66 20 74 68 65 79 20 61 72 65 20 76 61 6c   if they are val
d5450 69 64 2c 20 61 6e 64 20 73 65 74 73 20 61 6c 6c  id, and sets all
d5460 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 75   fields of the u
d5470 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63  nixFile.** struc
d5480 74 75 72 65 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a  ture to 0..**.**
d5490 20 49 74 20 69 73 20 2a 6e 6f 74 2a 20 6e 65 63   It is *not* nec
d54a0 65 73 73 61 72 79 20 74 6f 20 68 6f 6c 64 20 74  essary to hold t
d54b0 68 65 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68  he mutex when th
d54c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
d54d0 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65 6e 20 6f 6e  lled,.** even on
d54e0 20 56 78 57 6f 72 6b 73 2e 20 20 41 20 6d 75 74   VxWorks.  A mut
d54f0 65 78 20 77 69 6c 6c 20 62 65 20 61 63 71 75 69  ex will be acqui
d5500 72 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 20 62  red on VxWorks b
d5510 79 20 74 68 65 0a 2a 2a 20 76 78 77 6f 72 6b 73  y the.** vxworks
d5520 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 29 20  ReleaseFileId() 
d5530 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74  routine..*/.stat
d5540 69 63 20 69 6e 74 20 63 6c 6f 73 65 55 6e 69 78  ic int closeUnix
d5550 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  File(sqlite3_fil
d5560 65 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 46 69  e *id){.  unixFi
d5570 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d5580 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 66 28  xFile*)id;.  if(
d5590 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 66   pFile ){.    if
d55a0 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d  ( pFile->dirfd>=
d55b0 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65  0 ){.      int e
d55c0 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65  rr = close(pFile
d55d0 2d 3e 64 69 72 66 64 29 3b 0a 20 20 20 20 20 20  ->dirfd);.      
d55e0 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20 20  if( err ){.     
d55f0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
d5600 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d5610 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
d5620 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f  TE_IOERR_DIR_CLO
d5630 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  SE;.      }else{
d5640 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
d5650 64 69 72 66 64 3d 2d 31 3b 0a 20 20 20 20 20 20  dirfd=-1;.      
d5660 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
d5670 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20  pFile->h>=0 ){. 
d5680 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 63       int err = c
d5690 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a  lose(pFile->h);.
d56a0 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29 7b        if( err ){
d56b0 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
d56c0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
d56d0 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  o;.        retur
d56e0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  n SQLITE_IOERR_C
d56f0 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  LOSE;.      }.  
d5700 20 20 7d 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52    }.#if OS_VXWOR
d5710 4b 53 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65  KS.    if( pFile
d5720 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 20 20 69  ->pId ){.      i
d5730 66 28 20 70 46 69 6c 65 2d 3e 69 73 44 65 6c 65  f( pFile->isDele
d5740 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e  te ){.        un
d5750 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70 49 64 2d  link(pFile->pId-
d5760 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29  >zCanonicalName)
d5770 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
d5780 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69  vxworksReleaseFi
d5790 6c 65 49 64 28 70 46 69 6c 65 2d 3e 70 49 64 29  leId(pFile->pId)
d57a0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70  ;.      pFile->p
d57b0 49 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 65  Id = 0;.    }.#e
d57c0 6e 64 69 66 0a 20 20 20 20 4f 53 54 52 41 43 45  ndif.    OSTRACE
d57d0 32 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c  2("CLOSE   %-3d\
d57e0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20  n", pFile->h);. 
d57f0 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d     OpenCounter(-
d5800 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  1);.    sqlite3_
d5810 66 72 65 65 28 70 46 69 6c 65 2d 3e 70 55 6e 75  free(pFile->pUnu
d5820 73 65 64 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  sed);.    memset
d5830 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f  (pFile, 0, sizeo
d5840 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20  f(unixFile));.  
d5850 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
d5860 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
d5870 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  lose a file..*/.
d5880 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43  static int unixC
d5890 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
d58a0 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63  e *id){.  int rc
d58b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d58c0 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e  if( id ){.    un
d58d0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
d58e0 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a  (unixFile *)id;.
d58f0 20 20 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 69      unixUnlock(i
d5900 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20  d, NO_LOCK);.   
d5910 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
d5920 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65  );.    if( pFile
d5930 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c 65  ->pOpen && pFile
d5940 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29  ->pOpen->nLock )
d5950 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
d5960 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64  ere are outstand
d5970 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f  ing locks, do no
d5980 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65  t actually close
d5990 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20   the file just. 
d59a0 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 61       ** yet beca
d59b0 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 63  use that would c
d59c0 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73  lear those locks
d59d0 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 20  .  Instead, add 
d59e0 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a  the file.      *
d59f0 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20  * descriptor to 
d5a00 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 6c  pOpen->pUnused l
d5a10 69 73 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65  ist.  It will be
d5a20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
d5a30 6c 6f 73 65 64 20 0a 20 20 20 20 20 20 2a 2a 20  losed .      ** 
d5a40 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f  when the last lo
d5a50 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 20  ck is cleared.. 
d5a60 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 65       */.      se
d5a70 74 50 65 6e 64 69 6e 67 46 64 28 70 46 69 6c 65  tPendingFd(pFile
d5a80 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6c  );.    }.    rel
d5a90 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69  easeLockInfo(pFi
d5aa0 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20  le->pLock);.    
d5ab0 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70  releaseOpenCnt(p
d5ac0 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20  File->pOpen);.  
d5ad0 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78    rc = closeUnix
d5ae0 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e  File(id);.    un
d5af0 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
d5b00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
d5b10 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
d5b20 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70  *** End of the p
d5b30 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f  osix advisory lo
d5b40 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
d5b50 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
d5b60 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
d5b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5bb0 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
d5bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5c00 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
d5c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5c20 2a 2a 2a 20 4e 6f 2d 6f 70 20 4c 6f 63 6b 69 6e  *** No-op Lockin
d5c30 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  g **************
d5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5c50 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20 74 68  ****.**.** Of th
d5c60 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e  e various lockin
d5c70 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  g implementation
d5c80 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69  s available, thi
d5c90 73 20 69 73 20 62 79 20 66 61 72 20 74 68 65 0a  s is by far the.
d5ca0 2a 2a 20 73 69 6d 70 6c 65 73 74 3a 20 20 6c 6f  ** simplest:  lo
d5cb0 63 6b 69 6e 67 20 69 73 20 69 67 6e 6f 72 65 64  cking is ignored
d5cc0 2e 20 20 4e 6f 20 61 74 74 65 6d 70 74 20 69 73  .  No attempt is
d5cd0 20 6d 61 64 65 20 74 6f 20 6c 6f 63 6b 20 74 68   made to lock th
d5ce0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  e database.** fi
d5cf0 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f  le for reading o
d5d00 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  r writing..**.**
d5d10 20 54 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f   This locking mo
d5d20 64 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  de is appropriat
d5d30 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 72 65 61  e for use on rea
d5d40 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 73  d-only databases
d5d50 0a 2a 2a 20 28 65 78 3a 20 64 61 74 61 62 61 73  .** (ex: databas
d5d60 65 73 20 74 68 61 74 20 61 72 65 20 62 75 72 6e  es that are burn
d5d70 65 64 20 69 6e 74 6f 20 43 44 2d 52 4f 4d 2c 20  ed into CD-ROM, 
d5d80 66 6f 72 20 65 78 61 6d 70 6c 65 2e 29 20 20 49  for example.)  I
d5d90 74 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65  t can.** also be
d5da0 20 75 73 65 64 20 69 66 20 74 68 65 20 61 70 70   used if the app
d5db0 6c 69 63 61 74 69 6f 6e 20 65 6d 70 6c 6f 79 73  lication employs
d5dc0 20 73 6f 6d 65 20 65 78 74 65 72 6e 61 6c 20 6d   some external m
d5dd0 65 63 68 61 6e 69 73 6d 20 74 6f 0a 2a 2a 20 70  echanism to.** p
d5de0 72 65 76 65 6e 74 20 73 69 6d 75 6c 74 61 6e 65  revent simultane
d5df0 6f 75 73 20 61 63 63 65 73 73 20 6f 66 20 74 68  ous access of th
d5e00 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
d5e10 62 79 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a  by two or more.*
d5e20 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
d5e30 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 74 68 65  ctions.  But the
d5e40 72 65 20 69 73 20 61 20 73 65 72 69 6f 75 73 20  re is a serious 
d5e50 72 69 73 6b 20 6f 66 20 64 61 74 61 62 61 73 65  risk of database
d5e60 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69  .** corruption i
d5e70 66 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d  f this locking m
d5e80 6f 64 65 20 69 73 20 75 73 65 64 20 69 6e 20 73  ode is used in s
d5e90 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20  ituations where 
d5ea0 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 64 61 74 61  multiple.** data
d5eb0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
d5ec0 20 61 72 65 20 61 63 63 65 73 73 69 6e 67 20 74   are accessing t
d5ed0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
d5ee0 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d   file at the sam
d5ef0 65 0a 2a 2a 20 74 69 6d 65 20 61 6e 64 20 6f 6e  e.** time and on
d5f00 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 6f  e or more of tho
d5f10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61  se connections a
d5f20 72 65 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 0a  re writing..*/..
d5f30 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63  static int noloc
d5f40 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  kCheckReservedLo
d5f50 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d5f60 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 2a 70  *NotUsed, int *p
d5f70 52 65 73 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45  ResOut){.  UNUSE
d5f80 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
d5f90 73 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74  sed);.  *pResOut
d5fa0 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53   = 0;.  return S
d5fb0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74  QLITE_OK;.}.stat
d5fc0 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63  ic int nolockLoc
d5fd0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d5fe0 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74  NotUsed, int Not
d5ff0 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44  Used2){.  UNUSED
d6000 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
d6010 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
d6020 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d6030 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  OK;.}.static int
d6040 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71   nolockUnlock(sq
d6050 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55  lite3_file *NotU
d6060 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64  sed, int NotUsed
d6070 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  2){.  UNUSED_PAR
d6080 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
d6090 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65   NotUsed2);.  re
d60a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d60b0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74  }../*.** Close t
d60c0 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
d60d0 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 6c 6f  ic int nolockClo
d60e0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
d60f0 2a 69 64 29 20 7b 0a 20 20 72 65 74 75 72 6e 20  *id) {.  return 
d6100 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64  closeUnixFile(id
d6110 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
d6120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
d6130 66 20 74 68 65 20 6e 6f 2d 6f 70 20 6c 6f 63 6b  f the no-op lock
d6140 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
d6150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6160 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
d6170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61b0 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
d61c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6200 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
d6210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6220 20 42 65 67 69 6e 20 64 6f 74 2d 66 69 6c 65 20   Begin dot-file 
d6230 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a  Locking ********
d6240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6250 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  ******.**.** The
d6260 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67   dotfile locking
d6270 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
d6280 75 73 65 73 20 74 68 65 20 65 78 69 73 74 61 6e  uses the existan
d6290 63 65 20 6f 66 20 73 65 70 61 72 61 74 65 20 6c  ce of separate l
d62a0 6f 63 6b 0a 2a 2a 20 66 69 6c 65 73 20 69 6e 20  ock.** files in 
d62b0 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 72 6f 6c  order to control
d62c0 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 64   access to the d
d62d0 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 77  atabase.  This w
d62e0 6f 72 6b 73 20 6f 6e 20 6a 75 73 74 0a 2a 2a 20  orks on just.** 
d62f0 61 62 6f 75 74 20 65 76 65 72 79 20 66 69 6c 65  about every file
d6300 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61 62 6c  system imaginabl
d6310 65 2e 20 20 42 75 74 20 74 68 65 72 65 20 61 72  e.  But there ar
d6320 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e 73 69  e serious downsi
d6330 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31  des:.**.**    (1
d6340 29 20 20 54 68 65 72 65 20 69 73 20 7a 65 72 6f  )  There is zero
d6350 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e 20 20 41   concurrency.  A
d6360 20 73 69 6e 67 6c 65 20 72 65 61 64 65 72 20 62   single reader b
d6370 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68 65 72 0a  locks all other.
d6380 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e 6e 65  **         conne
d6390 63 74 69 6f 6e 73 20 66 72 6f 6d 20 72 65 61 64  ctions from read
d63a0 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74  ing or writing t
d63b0 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
d63c0 2a 2a 20 20 20 20 28 32 29 20 20 41 6e 20 61 70  **    (2)  An ap
d63d0 70 6c 69 63 61 74 69 6f 6e 20 63 72 61 73 68 20  plication crash 
d63e0 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73 20 63 61  or power loss ca
d63f0 6e 20 6c 65 61 76 65 20 73 74 61 6c 65 20 6c 6f  n leave stale lo
d6400 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 20 20 20 20  ck files.**     
d6410 20 20 20 20 73 69 74 74 69 6e 67 20 61 72 6f 75      sitting arou
d6420 6e 64 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20  nd that need to 
d6430 62 65 20 63 6c 65 61 72 65 64 20 6d 61 6e 75 61  be cleared manua
d6440 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 65 72  lly..**.** Never
d6450 74 68 65 6c 65 73 73 2c 20 61 20 64 6f 74 6c 6f  theless, a dotlo
d6460 63 6b 20 69 73 20 61 6e 20 61 70 70 72 6f 70 72  ck is an appropr
d6470 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64  iate locking mod
d6480 65 20 66 6f 72 20 75 73 65 20 69 66 20 6e 6f 0a  e for use if no.
d6490 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67  ** other locking
d64a0 20 73 74 72 61 74 65 67 79 20 69 73 20 61 76 61   strategy is ava
d64b0 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f  ilable..**.** Do
d64c0 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 77 6f  tfile locking wo
d64d0 72 6b 73 20 62 79 20 63 72 65 61 74 69 6e 67 20  rks by creating 
d64e0 61 20 66 69 6c 65 20 69 6e 20 74 68 65 20 73 61  a file in the sa
d64f0 6d 65 20 64 69 72 65 63 74 6f 72 79 20 61 73 20  me directory as 
d6500 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
d6510 61 6e 64 20 77 69 74 68 20 74 68 65 20 73 61 6d  and with the sam
d6520 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74 68 20  e name but with 
d6530 61 20 22 2e 6c 6f 63 6b 22 20 65 78 74 65 6e 73  a ".lock" extens
d6540 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2a 20 54 68  ion added..** Th
d6550 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 61  e existance of a
d6560 20 6c 6f 63 6b 20 66 69 6c 65 20 69 6d 70 6c 69   lock file impli
d6570 65 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  es an EXCLUSIVE 
d6580 6c 6f 63 6b 2e 20 20 41 6c 6c 20 6f 74 68 65 72  lock.  All other
d6590 20 6c 6f 63 6b 0a 2a 2a 20 74 79 70 65 73 20 28   lock.** types (
d65a0 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44  SHARED, RESERVED
d65b0 2c 20 50 45 4e 44 49 4e 47 29 20 61 72 65 20 6d  , PENDING) are m
d65c0 61 70 70 65 64 20 69 6e 74 6f 20 45 58 43 4c 55  apped into EXCLU
d65d0 53 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  SIVE..*/../*.** 
d65e0 54 68 65 20 66 69 6c 65 20 73 75 66 66 69 78 20  The file suffix 
d65f0 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 61 74  added to the dat
d6600 61 20 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  a base filename 
d6610 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61  in order to crea
d6620 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 66  te the.** lock f
d6630 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ile..*/.#define 
d6640 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 20 22  DOTLOCK_SUFFIX "
d6650 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20 54 68  .lock"../*.** Th
d6660 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
d6670 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
d6680 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
d6690 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
d66a0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
d66b0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
d66c0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
d66d0 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
d66e0 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a  , set *pResOut.*
d66f0 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  * to a non-zero 
d6700 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20  value otherwise 
d6710 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20  *pResOut is set 
d6720 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65  to zero.  The re
d6730 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73  turn value.** is
d6740 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f   set to SQLITE_O
d6750 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20  K unless an I/O 
d6760 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
d6770 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e  ing lock checkin
d6780 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 64 6f 74 66  g..**.** In dotf
d6790 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 65 69 74  ile locking, eit
d67a0 68 65 72 20 61 20 6c 6f 63 6b 20 65 78 69 73 74  her a lock exist
d67b0 73 20 6f 72 20 69 74 20 64 6f 65 73 20 6e 6f 74  s or it does not
d67c0 2e 20 20 53 6f 20 69 6e 20 74 68 69 73 0a 2a 2a  .  So in this.**
d67d0 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 43 68   variation of Ch
d67e0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
d67f0 29 2c 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73  ), *pResOut is s
d6800 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 6e  et to true if an
d6810 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 68 65 6c  y lock.** is hel
d6820 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 61 6e  d on the file an
d6830 64 20 66 61 6c 73 65 20 69 66 20 74 68 65 20 66  d false if the f
d6840 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e  ile is unlocked.
d6850 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
d6860 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72  otlockCheckReser
d6870 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
d6880 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
d6890 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20  ResOut) {.  int 
d68a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d68b0 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d    int reserved =
d68c0 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   0;.  unixFile *
d68d0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d68e0 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61  e*)id;..  Simula
d68f0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
d6900 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  n SQLITE_IOERR_C
d6910 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b  HECKRESERVEDLOCK
d6920 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  ; );.  .  assert
d6930 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a  ( pFile );..  /*
d6940 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65   Check if a thre
d6950 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65  ad in this proce
d6960 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20  ss holds such a 
d6970 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46  lock */.  if( pF
d6980 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48  ile->locktype>SH
d6990 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d69a0 20 2f 2a 20 45 69 74 68 65 72 20 74 68 69 73 20   /* Either this 
d69b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 73 6f  connection or so
d69c0 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  me other connect
d69d0 69 6f 6e 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ion in the same 
d69e0 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 68  process.    ** h
d69f0 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  olds a lock on t
d6a00 68 65 20 66 69 6c 65 2e 20 20 4e 6f 20 6e 65 65  he file.  No nee
d6a10 64 20 74 6f 20 63 68 65 63 6b 20 66 75 72 74 68  d to check furth
d6a20 65 72 2e 20 2a 2f 0a 20 20 20 20 72 65 73 65 72  er. */.    reser
d6a30 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  ved = 1;.  }else
d6a40 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63  {.    /* The loc
d6a50 6b 20 69 73 20 68 65 6c 64 20 69 66 20 61 6e 64  k is held if and
d6a60 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 6c 6f 63   only if the loc
d6a70 6b 66 69 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a  kfile exists */.
d6a80 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
d6a90 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 6f 6e  zLockFile = (con
d6aa0 73 74 20 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e  st char*)pFile->
d6ab0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
d6ac0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 61      reserved = a
d6ad0 63 63 65 73 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c  ccess(zLockFile,
d6ae0 20 30 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20 4f 53   0)==0;.  }.  OS
d6af0 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d  TRACE4("TEST WR-
d6b00 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22  LOCK %d %d %d\n"
d6b10 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20  , pFile->h, rc, 
d6b20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 2a 70 52  reserved);.  *pR
d6b30 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64  esOut = reserved
d6b40 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
d6b50 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65  ../*.** Lock the
d6b60 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c   file with the l
d6b70 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79  ock specified by
d6b80 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74   parameter lockt
d6b90 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20  ype - one.** of 
d6ba0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
d6bb0 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41  *.**     (1) SHA
d6bc0 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  RED_LOCK.**     
d6bd0 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  (2) RESERVED_LOC
d6be0 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e  K.**     (3) PEN
d6bf0 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  DING_LOCK.**    
d6c00 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c   (4) EXCLUSIVE_L
d6c10 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  OCK.**.** Someti
d6c20 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74  mes when request
d6c30 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61  ing one lock sta
d6c40 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  te, additional l
d6c50 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72  ock states.** ar
d6c60 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65  e inserted in be
d6c70 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b  tween.  The lock
d6c80 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f  ing might fail o
d6c90 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74  n one of the lat
d6ca0 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  er.** transition
d6cb0 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f  s leaving the lo
d6cc0 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65  ck state differe
d6cd0 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20  nt from what it 
d6ce0 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73  started but.** s
d6cf0 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74  till short of it
d6d00 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c  s goal.  The fol
d6d10 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f  lowing chart sho
d6d20 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a  ws the allowed.*
d6d30 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e  * transitions an
d6d40 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69  d the inserted i
d6d50 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74  ntermediate stat
d6d60 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c  es:.**.**    UNL
d6d70 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a  OCKED -> SHARED.
d6d80 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
d6d90 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53  RESERVED.**    S
d6da0 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  HARED -> (PENDIN
d6db0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
d6dc0 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d  **    RESERVED -
d6dd0 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
d6de0 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50  XCLUSIVE.**    P
d6df0 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53  ENDING -> EXCLUS
d6e00 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  IVE.**.** This r
d6e10 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79  outine will only
d6e20 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b   increase a lock
d6e30 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74  .  Use the sqlit
d6e40 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20  e3OsUnlock().** 
d6e50 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72  routine to lower
d6e60 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c   a locking level
d6e70 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 64 6f 74  ..**.** With dot
d6e80 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 77 65  file locking, we
d6e90 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20 73 75 70   really only sup
d6ea0 70 6f 72 74 20 73 74 61 74 65 20 28 34 29 3a 20  port state (4): 
d6eb0 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a 20 42 75  EXCLUSIVE..** Bu
d6ec0 74 20 77 65 20 74 72 61 63 6b 20 74 68 65 20 6f  t we track the o
d6ed0 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  ther locking lev
d6ee0 65 6c 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a  els internally..
d6ef0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f  */.static int do
d6f00 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65  tlockLock(sqlite
d6f10 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
d6f20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e  locktype) {.  un
d6f30 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
d6f40 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
d6f50 20 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20   int fd;.  char 
d6f60 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68  *zLockFile = (ch
d6f70 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ar *)pFile->lock
d6f80 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e  ingContext;.  in
d6f90 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
d6fa0 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68  ;...  /* If we h
d6fb0 61 76 65 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68  ave any lock, th
d6fc0 65 6e 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65  en the lock file
d6fd0 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e   already exists.
d6fe0 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20    All we have.  
d6ff0 2a 2a 20 74 6f 20 64 6f 20 69 73 20 61 64 6a 75  ** to do is adju
d7000 73 74 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20  st our internal 
d7010 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f  record of the lo
d7020 63 6b 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20  ck level..  */. 
d7030 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d7040 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 29  type > NO_LOCK )
d7050 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
d7060 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
d7070 3b 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b  ;.#if !OS_VXWORK
d7080 53 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20  S.    /* Always 
d7090 75 70 64 61 74 65 20 74 68 65 20 74 69 6d 65 73  update the times
d70a0 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20  tamp on the old 
d70b0 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d  file */.    utim
d70c0 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55  es(zLockFile, NU
d70d0 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  LL);.#endif.    
d70e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d70f0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72  ;.  }.  .  /* gr
d7100 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ab an exclusive 
d7110 6c 6f 63 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f  lock */.  fd = o
d7120 70 65 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f  pen(zLockFile,O_
d7130 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f  RDONLY|O_CREAT|O
d7140 5f 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69  _EXCL,0600);.  i
d7150 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f  f( fd<0 ){.    /
d7160 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e  * failed to open
d7170 2f 63 72 65 61 74 65 20 74 68 65 20 66 69 6c 65  /create the file
d7180 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d  , someone else m
d7190 61 79 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74  ay have stolen t
d71a0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69  he lock */.    i
d71b0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d71c0 6f 3b 0a 20 20 20 20 69 66 28 20 45 45 58 49 53  o;.    if( EEXIS
d71d0 54 20 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20  T == tErrno ){. 
d71e0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
d71f0 5f 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73  _BUSY;.    } els
d7200 65 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  e {.      rc = s
d7210 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d7220 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d7230 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d7240 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
d7250 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d7260 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d7270 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d7280 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
d7290 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
d72a0 72 63 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 63  rc;.  } .  if( c
d72b0 6c 6f 73 65 28 66 64 29 20 29 7b 0a 20 20 20 20  lose(fd) ){.    
d72c0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d72d0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63   = errno;.    rc
d72e0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
d72f0 43 4c 4f 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20  CLOSE;.  }.  .  
d7300 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74  /* got it, set t
d7310 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 75  he type and retu
d7320 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65  rn ok */.  pFile
d7330 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
d7340 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20  ktype;.  return 
d7350 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77  rc;.}../*.** Low
d7360 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
d7370 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
d7380 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f  criptor pFile to
d7390 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b   locktype.  lock
d73a0 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20  type.** must be 
d73b0 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f  either NO_LOCK o
d73c0 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a  r SHARED_LOCK..*
d73d0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b  *.** If the lock
d73e0 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  ing level of the
d73f0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d7400 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f   is already at o
d7410 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72  r below.** the r
d7420 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67  equested locking
d7430 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75   level, this rou
d7440 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
d7450 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
d7460 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 72 65  locking level re
d7470 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c 20 64  aches NO_LOCK, d
d7480 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66  elete the lock f
d7490 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
d74a0 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b  nt dotlockUnlock
d74b0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d74c0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
d74d0 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
d74e0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d74f0 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c  *)id;.  char *zL
d7500 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20  ockFile = (char 
d7510 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
d7520 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65  Context;..  asse
d7530 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
d7540 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20  STRACE5("UNLOCK 
d7550 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69   %d %d was %d pi
d7560 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
d7570 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20  h, locktype,..  
d7580 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d7590 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61  , getpid());.  a
d75a0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
d75b0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
d75c0 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66    .  /* no-op if
d75d0 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69   possible */.  i
d75e0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d75f0 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe==locktype ){.
d7600 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d7610 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
d7620 54 6f 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20  To downgrade to 
d7630 73 68 61 72 65 64 2c 20 73 69 6d 70 6c 79 20 75  shared, simply u
d7640 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e  pdate our intern
d7650 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65  al notion of the
d7660 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65  .  ** lock state
d7670 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65  .  No need to me
d7680 73 73 20 77 69 74 68 20 74 68 65 20 66 69 6c 65  ss with the file
d7690 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20   on disk..  */. 
d76a0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
d76b0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
d76c0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d76d0 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b  e = SHARED_LOCK;
d76e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d76f0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d7700 2f 2a 20 54 6f 20 66 75 6c 6c 79 20 75 6e 6c 6f  /* To fully unlo
d7710 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  ck the database,
d7720 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b   delete the lock
d7730 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72   file */.  asser
d7740 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  t( locktype==NO_
d7750 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 75 6e  LOCK );.  if( un
d7760 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 20  link(zLockFile) 
d7770 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
d7780 30 3b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e  0;.    int tErrn
d7790 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69  o = errno;.    i
d77a0 66 28 20 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72  f( ENOENT != tEr
d77b0 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20  rno ){.      rc 
d77c0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d77d0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d77e0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
d77f0 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a  _UNLOCK);.    }.
d7800 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
d7810 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20  ERROR(rc) ){.   
d7820 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
d7830 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
d7840 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72    }.    return r
d7850 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d  c; .  }.  pFile-
d7860 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c  >locktype = NO_L
d7870 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  OCK;.  return SQ
d7880 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
d7890 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20  * Close a file. 
d78a0 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c   Make sure the l
d78b0 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 72 65 6c  ock has been rel
d78c0 65 61 73 65 64 20 62 65 66 6f 72 65 20 63 6c 6f  eased before clo
d78d0 73 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sing..*/.static 
d78e0 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65  int dotlockClose
d78f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d7900 64 29 20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  d) {.  int rc;. 
d7910 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75   if( id ){.    u
d7920 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d7930 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d7940 20 20 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63      dotlockUnloc
d7950 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  k(id, NO_LOCK);.
d7960 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
d7970 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  (pFile->lockingC
d7980 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72  ontext);.  }.  r
d7990 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c  c = closeUnixFil
d79a0 65 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  e(id);.  return 
d79b0 72 63 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  rc;.}./*********
d79c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
d79d0 20 74 68 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f   the dot-file lo
d79e0 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
d79f0 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
d7a00 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
d7a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a50 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
d7a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7aa0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
d7ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7ac0 2a 20 42 65 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f  * Begin flock Lo
d7ad0 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cking **********
d7ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7af0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65  ******.**.** Use
d7b00 20 74 68 65 20 66 6c 6f 63 6b 28 29 20 73 79 73   the flock() sys
d7b10 74 65 6d 20 63 61 6c 6c 20 74 6f 20 64 6f 20 66  tem call to do f
d7b20 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a  ile locking..**.
d7b30 2a 2a 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69  ** flock() locki
d7b40 6e 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d 66  ng is like dot-f
d7b50 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74  ile locking in t
d7b60 68 61 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a  hat the various.
d7b70 2a 2a 20 66 69 6e 65 2d 67 72 61 69 6e 20 6c 6f  ** fine-grain lo
d7b80 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 73 75 70  cking levels sup
d7b90 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65  ported by SQLite
d7ba0 20 61 72 65 20 63 6f 6c 6c 61 70 73 65 64 20 69   are collapsed i
d7bb0 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20  nto.** a single 
d7bc0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20  exclusive lock. 
d7bd0 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
d7be0 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45   SHARED, RESERVE
d7bf0 44 2c 20 61 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e  D, and.** PENDIN
d7c00 47 20 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20  G locks are the 
d7c10 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 61 6e  same thing as an
d7c20 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
d7c30 20 20 53 51 4c 69 74 65 0a 2a 2a 20 73 74 69 6c    SQLite.** stil
d7c40 6c 20 77 6f 72 6b 73 20 77 68 65 6e 20 79 6f 75  l works when you
d7c50 20 64 6f 20 74 68 69 73 2c 20 62 75 74 20 63 6f   do this, but co
d7c60 6e 63 75 72 72 65 6e 63 79 20 69 73 20 72 65 64  ncurrency is red
d7c70 75 63 65 64 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e  uced since.** on
d7c80 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63  ly a single proc
d7c90 65 73 73 20 63 61 6e 20 62 65 20 72 65 61 64 69  ess can be readi
d7ca0 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
d7cb0 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  at a time..**.**
d7cc0 20 4f 6d 69 74 20 74 68 69 73 20 73 65 63 74 69   Omit this secti
d7cd0 6f 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  on if SQLITE_ENA
d7ce0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
d7cf0 45 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 20  E is turned off 
d7d00 6f 72 20 69 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69  or if.** compili
d7d10 6e 67 20 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0a  ng for VXWORKS..
d7d20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  */.#if SQLITE_EN
d7d30 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
d7d40 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b  LE && !OS_VXWORK
d7d50 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  S../*.** This ro
d7d60 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
d7d70 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
d7d80 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
d7d90 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
d7da0 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
d7db0 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
d7dc0 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
d7dd0 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74  ock is held, set
d7de0 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20   *pResOut.** to 
d7df0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
d7e00 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73   otherwise *pRes
d7e10 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65  Out is set to ze
d7e20 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  ro.  The return 
d7e30 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20  value.** is set 
d7e40 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c  to SQLITE_OK unl
d7e50 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72  ess an I/O error
d7e60 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c   occurs during l
d7e70 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f  ock checking..*/
d7e80 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63  .static int floc
d7e90 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  kCheckReservedLo
d7ea0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d7eb0 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
d7ec0 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
d7ed0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
d7ee0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20  reserved = 0;.  
d7ef0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d7f00 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d7f10 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  .  .  SimulateIO
d7f20 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
d7f30 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
d7f40 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b  RESERVEDLOCK; );
d7f50 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46  .  .  assert( pF
d7f60 69 6c 65 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43  ile );.  .  /* C
d7f70 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64  heck if a thread
d7f80 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73   in this process
d7f90 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f   holds such a lo
d7fa0 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  ck */.  if( pFil
d7fb0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52  e->locktype>SHAR
d7fc0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
d7fd0 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d  eserved = 1;.  }
d7fe0 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69  .  .  /* Otherwi
d7ff0 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f  se see if some o
d8000 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c  ther process hol
d8010 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20  ds it. */.  if( 
d8020 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20  !reserved ){.   
d8030 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 67   /* attempt to g
d8040 65 74 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20  et the lock */. 
d8050 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f     int lrc = flo
d8060 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43  ck(pFile->h, LOC
d8070 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b  K_EX | LOCK_NB);
d8080 0a 20 20 20 20 69 66 28 20 21 6c 72 63 20 29 7b  .    if( !lrc ){
d8090 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 74 68  .      /* got th
d80a0 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69  e lock, unlock i
d80b0 74 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d  t */.      lrc =
d80c0 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c   flock(pFile->h,
d80d0 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20   LOCK_UN);.     
d80e0 20 69 66 20 28 20 6c 72 63 20 29 20 7b 0a 20 20   if ( lrc ) {.  
d80f0 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f        int tErrno
d8100 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d8110 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c    /* unlock fail
d8120 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ed with an error
d8130 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 72 63 20   */.        lrc 
d8140 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d8150 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d8160 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
d8170 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20  _UNLOCK); .     
d8180 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d8190 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20  RROR(lrc) ){.   
d81a0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d81b0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d81c0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
d81d0 20 6c 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a   lrc;.        }.
d81e0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c        }.    } el
d81f0 73 65 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74  se {.      int t
d8200 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d8210 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20       reserved = 
d8220 31 3b 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65  1;.      /* some
d8230 6f 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68  one else might h
d8240 61 76 65 20 69 74 20 72 65 73 65 72 76 65 64 20  ave it reserved 
d8250 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 73  */.      lrc = s
d8260 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d8270 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d8280 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d8290 43 4b 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20  CK); .      if( 
d82a0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72  IS_LOCK_ERROR(lr
d82b0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46  c) ){.        pF
d82c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d82d0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20   tErrno;.       
d82e0 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20   rc = lrc;.     
d82f0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f   }.    }.  }.  O
d8300 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52  STRACE4("TEST WR
d8310 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e  -LOCK %d %d %d\n
d8320 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c  ", pFile->h, rc,
d8330 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 23 69 66   reserved);..#if
d8340 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  def SQLITE_IGNOR
d8350 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52  E_FLOCK_LOCK_ERR
d8360 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20  ORS.  if( (rc & 
d8370 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d  SQLITE_IOERR) ==
d8380 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b   SQLITE_IOERR ){
d8390 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
d83a0 5f 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 76 65  _OK;.    reserve
d83b0 64 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  d=1;.  }.#endif 
d83c0 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  /* SQLITE_IGNORE
d83d0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
d83e0 52 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74  RS */.  *pResOut
d83f0 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72   = reserved;.  r
d8400 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d8410 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
d8420 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
d8430 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
d8440 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d  meter locktype -
d8450 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
d8460 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
d8470 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
d8480 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
d8490 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
d84a0 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
d84b0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
d84c0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
d84d0 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
d84e0 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
d84f0 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
d8500 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
d8510 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
d8520 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
d8530 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
d8540 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
d8550 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
d8560 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
d8570 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
d8580 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
d8590 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
d85a0 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
d85b0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
d85c0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
d85d0 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
d85e0 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
d85f0 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
d8600 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
d8610 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
d8620 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
d8630 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
d8640 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
d8650 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
d8660 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d8670 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d8680 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
d8690 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
d86a0 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
d86b0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
d86c0 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c  *.** flock() onl
d86d0 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74  y really support
d86e0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73   EXCLUSIVE locks
d86f0 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65  .  We track inte
d8700 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b  rmediate.** lock
d8710 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73   states in the s
d8720 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75  qlite3_file stru
d8730 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c  cture, but all l
d8740 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a  ocks SHARED or.*
d8750 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c  * above are real
d8760 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  ly EXCLUSIVE loc
d8770 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61  ks and exclude a
d8780 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ll other process
d8790 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73  es from.** acces
d87a0 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  s the file..**.*
d87b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
d87c0 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73  ill only increas
d87d0 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74  e a lock.  Use t
d87e0 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  he sqlite3OsUnlo
d87f0 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  ck().** routine 
d8800 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69  to lower a locki
d8810 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61  ng level..*/.sta
d8820 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63  tic int flockLoc
d8830 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d8840 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
d8850 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ) {.  int rc = S
d8860 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78  QLITE_OK;.  unix
d8870 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d8880 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20  nixFile*)id;..  
d8890 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d88a0 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72  ..  /* if we alr
d88b0 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b  eady have a lock
d88c0 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76  , it is exclusiv
d88d0 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61  e.  .  ** Just a
d88e0 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20  djust level and 
d88f0 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65  punt on outta he
d8900 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69  re. */.  if (pFi
d8910 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e  le->locktype > N
d8920 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46  O_LOCK) {.    pF
d8930 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
d8940 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65  locktype;.    re
d8950 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d8960 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62    }.  .  /* grab
d8970 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
d8980 63 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66  ck */.  .  if (f
d8990 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
d89a0 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42  OCK_EX | LOCK_NB
d89b0 29 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 45 72  )) {.    int tEr
d89c0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d89d0 20 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20   /* didn't get, 
d89e0 6d 75 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a  must be busy */.
d89f0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d8a00 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d8a10 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d8a20 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20  E_IOERR_LOCK);. 
d8a30 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d8a40 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
d8a50 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d8a60 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d8a70 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20   }.  } else {.  
d8a80 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74    /* got it, set
d8a90 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65   the type and re
d8aa0 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70  turn ok */.    p
d8ab0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d8ac0 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20   locktype;.  }. 
d8ad0 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20   OSTRACE4("LOCK 
d8ae0 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20     %d %s %s\n", 
d8af0 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
d8b00 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29  peName(locktype)
d8b10 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 72 63  , .           rc
d8b20 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f  ==SQLITE_OK ? "o
d8b30 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a  k" : "failed");.
d8b40 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47  #ifdef SQLITE_IG
d8b50 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f  NORE_FLOCK_LOCK_
d8b60 45 52 52 4f 52 53 0a 20 20 69 66 28 20 28 72 63  ERRORS.  if( (rc
d8b70 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29   & SQLITE_IOERR)
d8b80 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52   == SQLITE_IOERR
d8b90 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
d8ba0 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65  ITE_BUSY;.  }.#e
d8bb0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49  ndif /* SQLITE_I
d8bc0 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b  GNORE_FLOCK_LOCK
d8bd0 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 72 65 74  _ERRORS */.  ret
d8be0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
d8bf0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  * Lower the lock
d8c00 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c  ing level on fil
d8c10 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69  e descriptor pFi
d8c20 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20  le to locktype. 
d8c30 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73   locktype.** mus
d8c40 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c  t be either NO_L
d8c50 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f  OCK or SHARED_LO
d8c60 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  CK..**.** If the
d8c70 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
d8c80 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  f the file descr
d8c90 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79  iptor is already
d8ca0 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20   at or below.** 
d8cb0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f  the requested lo
d8cc0 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69  cking level, thi
d8cd0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
d8ce0 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
d8cf0 69 6e 74 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28  int flockUnlock(
d8d00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d8d10 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
d8d20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
d8d30 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d8d40 29 69 64 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  )id;.  .  assert
d8d50 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54  ( pFile );.  OST
d8d60 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25  RACE5("UNLOCK  %
d8d70 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d  d %d was %d pid=
d8d80 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
d8d90 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20   locktype,.     
d8da0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
d8db0 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 29  ktype, getpid())
d8dc0 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
d8dd0 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43  type<=SHARED_LOC
d8de0 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d  K );.  .  /* no-
d8df0 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a  op if possible *
d8e00 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d8e10 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70  ocktype==locktyp
d8e20 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
d8e30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
d8e40 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61   .  /* shared ca
d8e50 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65  n just be set be
d8e60 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20  cause we always 
d8e70 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76  have an exclusiv
d8e80 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74  e */.  if (lockt
d8e90 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d8ea0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ) {.    pFile->l
d8eb0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d8ec0 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  pe;.    return S
d8ed0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
d8ee0 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79  .  /* no, really
d8ef0 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69  , unlock. */.  i
d8f00 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46  nt rc = flock(pF
d8f10 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29  ile->h, LOCK_UN)
d8f20 3b 0a 20 20 69 66 20 28 72 63 29 20 7b 0a 20 20  ;.  if (rc) {.  
d8f30 20 20 69 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20    int r, tErrno 
d8f40 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d  = errno;.    r =
d8f50 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d8f60 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d8f70 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d8f80 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28  UNLOCK);.    if(
d8f90 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72   IS_LOCK_ERROR(r
d8fa0 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  ) ){.      pFile
d8fb0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d8fc0 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 23 69 66 64  rrno;.    }.#ifd
d8fd0 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  ef SQLITE_IGNORE
d8fe0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
d8ff0 52 53 0a 20 20 20 20 69 66 28 20 28 72 20 26 20  RS.    if( (r & 
d9000 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d  SQLITE_IOERR) ==
d9010 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b   SQLITE_IOERR ){
d9020 0a 20 20 20 20 20 20 72 20 3d 20 53 51 4c 49 54  .      r = SQLIT
d9030 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 23 65  E_BUSY;.    }.#e
d9040 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49  ndif /* SQLITE_I
d9050 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b  GNORE_FLOCK_LOCK
d9060 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 0a  _ERRORS */.    .
d9070 20 20 20 20 72 65 74 75 72 6e 20 72 3b 0a 20 20      return r;.  
d9080 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 46 69  } else {.    pFi
d9090 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e  le->locktype = N
d90a0 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75  O_LOCK;.    retu
d90b0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d90c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  }.}../*.** Close
d90d0 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74   a file..*/.stat
d90e0 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73  ic int flockClos
d90f0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
d9100 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29  id) {.  if( id )
d9110 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63  {.    flockUnloc
d9120 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  k(id, NO_LOCK);.
d9130 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f    }.  return clo
d9140 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a  seUnixFile(id);.
d9150 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
d9160 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
d9170 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f  NG_STYLE && !OS_
d9180 56 58 57 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  VXWORK */../****
d9190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
d91a0 45 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b  End of the flock
d91b0 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61   lock implementa
d91c0 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tion ***********
d91d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
d91e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d91f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a  *********/../***
d9230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
d9280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9290 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64  **** Begin Named
d92a0 20 53 65 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69   Semaphore Locki
d92b0 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
d92c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
d92d0 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72  * Named semaphor
d92e0 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c  e locking is onl
d92f0 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 56  y supported on V
d9300 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  xWorks..**.** Se
d9310 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20  maphore locking 
d9320 69 73 20 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b  is like dot-lock
d9330 20 61 6e 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68   and flock in th
d9340 61 74 20 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c  at it really onl
d9350 79 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 45 58  y.** supports EX
d9360 43 4c 55 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e  CLUSIVE locking.
d9370 20 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20    Only a single 
d9380 70 72 6f 63 65 73 73 20 63 61 6e 20 72 65 61 64  process can read
d9390 20 6f 72 20 77 72 69 74 65 0a 2a 2a 20 74 68 65   or write.** the
d93a0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
d93b0 74 20 61 20 74 69 6d 65 2e 20 20 54 68 69 73 20  t a time.  This 
d93c0 72 65 64 75 63 65 73 20 70 6f 74 65 6e 74 69 61  reduces potentia
d93d0 6c 20 63 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62  l concurrency, b
d93e0 75 74 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20  ut.** makes the 
d93f0 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
d9400 69 6f 6e 20 6d 75 63 68 20 65 61 73 69 65 72 2e  ion much easier.
d9410 0a 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  .*/.#if OS_VXWOR
d9420 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  KS../*.** This r
d9430 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
d9440 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45   there is a RESE
d9450 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  RVED lock held o
d9460 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  n the specified.
d9470 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
d9480 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
d9490 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
d94a0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65  lock is held, se
d94b0 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f  t *pResOut.** to
d94c0 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75   a non-zero valu
d94d0 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65  e otherwise *pRe
d94e0 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a  sOut is set to z
d94f0 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ero.  The return
d9500 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74   value.** is set
d9510 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e   to SQLITE_OK un
d9520 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f  less an I/O erro
d9530 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
d9540 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a  lock checking..*
d9550 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d  /.static int sem
d9560 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d9570 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d9580 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
d9590 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ) {.  int rc = S
d95a0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
d95b0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20  reserved = 0;.  
d95c0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d95d0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d95e0 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ..  SimulateIOEr
d95f0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
d9600 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
d9610 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20  SERVEDLOCK; );. 
d9620 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c   .  assert( pFil
d9630 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  e );..  /* Check
d9640 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
d9650 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
d9660 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
d9670 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d9680 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
d9690 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72  OCK ){.    reser
d96a0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a  ved = 1;.  }.  .
d96b0 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73    /* Otherwise s
d96c0 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  ee if some other
d96d0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69   process holds i
d96e0 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73  t. */.  if( !res
d96f0 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 65 6d  erved ){.    sem
d9700 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65  _t *pSem = pFile
d9710 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20  ->pOpen->pSem;. 
d9720 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73     struct stat s
d9730 74 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 28  tatBuf;..    if(
d9740 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65   sem_trywait(pSe
d9750 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20  m)==-1 ){.      
d9760 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
d9770 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 41  no;.      if( EA
d9780 47 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29  GAIN != tErrno )
d9790 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
d97a0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d97b0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d97c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48   SQLITE_IOERR_CH
d97d0 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29  ECKRESERVEDLOCK)
d97e0 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ;.        pFile-
d97f0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d9800 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73  rno;.      } els
d9810 65 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73  e {.        /* s
d9820 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20  omeone else has 
d9830 74 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65  the lock when we
d9840 20 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20   are in NO_LOCK 
d9850 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 72  */.        reser
d9860 76 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f  ved = (pFile->lo
d9870 63 6b 74 79 70 65 20 3c 20 53 48 41 52 45 44 5f  cktype < SHARED_
d9880 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20  LOCK);.      }. 
d9890 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d98a0 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65  /* we could have
d98b0 20 69 74 20 69 66 20 77 65 20 77 61 6e 74 20 69   it if we want i
d98c0 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70  t */.      sem_p
d98d0 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d  ost(pSem);.    }
d98e0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28  .  }.  OSTRACE4(
d98f0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
d9900 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65   %d %d\n", pFile
d9910 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65  ->h, rc, reserve
d9920 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20  d);..  *pResOut 
d9930 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65  = reserved;.  re
d9940 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
d9950 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20  * Lock the file 
d9960 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70  with the lock sp
d9970 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ecified by param
d9980 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20  eter locktype - 
d9990 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f  one.** of the fo
d99a0 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
d99b0 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f     (1) SHARED_LO
d99c0 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45  CK.**     (2) RE
d99d0 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SERVED_LOCK.**  
d99e0 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c     (3) PENDING_L
d99f0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45  OCK.**     (4) E
d9a00 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a  XCLUSIVE_LOCK.**
d9a10 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68  .** Sometimes wh
d9a20 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e  en requesting on
d9a30 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64  e lock state, ad
d9a40 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74  ditional lock st
d9a50 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65  ates.** are inse
d9a60 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e  rted in between.
d9a70 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69    The locking mi
d9a80 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20  ght fail on one 
d9a90 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20  of the later.** 
d9aa0 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76  transitions leav
d9ab0 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61  ing the lock sta
d9ac0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  te different fro
d9ad0 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65  m what it starte
d9ae0 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73  d but.** still s
d9af0 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c  hort of its goal
d9b00 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
d9b10 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65   chart shows the
d9b20 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e   allowed.** tran
d9b30 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20  sitions and the 
d9b40 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65  inserted interme
d9b50 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a  diate states:.**
d9b60 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20  .**    UNLOCKED 
d9b70 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20  -> SHARED.**    
d9b80 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56  SHARED -> RESERV
d9b90 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
d9ba0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
d9bb0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
d9bc0 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e  RESERVED -> (PEN
d9bd0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
d9be0 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47  VE.**    PENDING
d9bf0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
d9c00 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f  .** Semaphore lo
d9c10 63 6b 73 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20  cks only really 
d9c20 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56  support EXCLUSIV
d9c30 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61  E locks.  We tra
d9c40 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a  ck intermediate.
d9c50 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69  ** lock states i
d9c60 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  n the sqlite3_fi
d9c70 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75  le structure, bu
d9c80 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52  t all locks SHAR
d9c90 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61  ED or.** above a
d9ca0 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53  re really EXCLUS
d9cb0 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78  IVE locks and ex
d9cc0 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20  clude all other 
d9cd0 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a  processes from.*
d9ce0 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c  * access the fil
d9cf0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
d9d00 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  utine will only 
d9d10 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e  increase a lock.
d9d20 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65    Use the sqlite
d9d30 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72  3OsUnlock().** r
d9d40 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20  outine to lower 
d9d50 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e  a locking level.
d9d60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
d9d70 65 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  emLock(sqlite3_f
d9d80 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
d9d90 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46  ktype) {.  unixF
d9da0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d9db0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e  ixFile*)id;.  in
d9dc0 74 20 66 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70  t fd;.  sem_t *p
d9dd0 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70  Sem = pFile->pOp
d9de0 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 69 6e 74 20  en->pSem;.  int 
d9df0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d9e00 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65  .  /* if we alre
d9e10 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c  ady have a lock,
d9e20 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65   it is exclusive
d9e30 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64  .  .  ** Just ad
d9e40 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70  just level and p
d9e50 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72  unt on outta her
d9e60 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c  e. */.  if (pFil
d9e70 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f  e->locktype > NO
d9e80 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69  _LOCK) {.    pFi
d9e90 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d9ea0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 63 20  ocktype;.    rc 
d9eb0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
d9ec0 20 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f   goto sem_end_lo
d9ed0 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  ck;.  }.  .  /* 
d9ee0 6c 6f 63 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e  lock semaphore n
d9ef0 6f 77 20 62 75 74 20 62 61 69 6c 20 6f 75 74 20  ow but bail out 
d9f00 77 68 65 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63  when already loc
d9f10 6b 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65  ked. */.  if( se
d9f20 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d  m_trywait(pSem)=
d9f30 3d 2d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  =-1 ){.    rc = 
d9f40 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
d9f50 20 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f   goto sem_end_lo
d9f60 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f  ck;.  }..  /* go
d9f70 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79  t it, set the ty
d9f80 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b  pe and return ok
d9f90 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63   */.  pFile->loc
d9fa0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
d9fb0 3b 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b  ;.. sem_end_lock
d9fc0 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  :.  return rc;.}
d9fd0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ../*.** Lower th
d9fe0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
d9ff0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
da000 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b  or pFile to lock
da010 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
da020 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
da030 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
da040 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
da050 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
da060 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
da070 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
da080 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
da090 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
da0a0 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
da0b0 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
da0c0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73  is a no-op..*/.s
da0d0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 55 6e 6c  tatic int semUnl
da0e0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
da0f0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
da100 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  pe) {.  unixFile
da110 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
da120 69 6c 65 2a 29 69 64 3b 0a 20 20 73 65 6d 5f 74  ile*)id;.  sem_t
da130 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e   *pSem = pFile->
da140 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20  pOpen->pSem;..  
da150 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
da160 0a 20 20 61 73 73 65 72 74 28 20 70 53 65 6d 20  .  assert( pSem 
da170 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55  );.  OSTRACE5("U
da180 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73  NLOCK  %d %d was
da190 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %d pid=%d\n", p
da1a0 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
da1b0 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  e,..   pFile->lo
da1c0 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 29  cktype, getpid()
da1d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63  );.  assert( loc
da1e0 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f  ktype<=SHARED_LO
da1f0 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f  CK );.  .  /* no
da200 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20  -op if possible 
da210 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
da220 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79  locktype==lockty
da230 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  pe ){.    return
da240 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
da250 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63    .  /* shared c
da260 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62  an just be set b
da270 65 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73  ecause we always
da280 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69   have an exclusi
da290 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b  ve */.  if (lock
da2a0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
da2b0 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K) {.    pFile->
da2c0 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
da2d0 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ype;.    return 
da2e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
da2f0 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c   .  /* no, reall
da300 79 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69  y unlock. */.  i
da310 66 20 28 20 73 65 6d 5f 70 6f 73 74 28 70 53 65  f ( sem_post(pSe
da320 6d 29 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69  m)==-1 ) {.    i
da330 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20  nt rc, tErrno = 
da340 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20  errno;.    rc = 
da350 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
da360 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
da370 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
da380 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20  NLOCK);.    if( 
da390 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
da3a0 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  ) ){.      pFile
da3b0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
da3c0 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rrno;.    }.    
da3d0 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a  return rc; .  }.
da3e0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
da3f0 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72  e = NO_LOCK;.  r
da400 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
da410 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65  .}../*. ** Close
da420 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61   a file.. */.sta
da430 74 69 63 20 69 6e 74 20 73 65 6d 43 6c 6f 73 65  tic int semClose
da440 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
da450 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b  d) {.  if( id ){
da460 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
da470 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
da480 2a 29 69 64 3b 0a 20 20 20 20 73 65 6d 55 6e 6c  *)id;.    semUnl
da490 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29  ock(id, NO_LOCK)
da4a0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  ;.    assert( pF
da4b0 69 6c 65 20 29 3b 0a 20 20 20 20 75 6e 69 78 45  ile );.    unixE
da4c0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
da4d0 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f   releaseLockInfo
da4e0 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a  (pFile->pLock);.
da4f0 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43      releaseOpenC
da500 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29  nt(pFile->pOpen)
da510 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d  ;.    unixLeaveM
da520 75 74 65 78 28 29 3b 0a 20 20 20 20 63 6c 6f 73  utex();.    clos
da530 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20  eUnixFile(id);. 
da540 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
da550 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66  TE_OK;.}..#endif
da560 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a   /* OS_VXWORKS *
da570 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65  /./*.** Named se
da580 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20  maphore locking 
da590 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
da5a0 65 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a  e on VxWorks..**
da5b0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
da5c0 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61 6d 65   End of the name
da5d0 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b  d semaphore lock
da5e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
da5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da600 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
da610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
da650 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
da660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da6a0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
da6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
da6c0 65 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69 6e 67  egin AFP Locking
da6d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
da6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da6f0 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69 73 20  **.**.** AFP is 
da700 74 68 65 20 41 70 70 6c 65 20 46 69 6c 69 6e 67  the Apple Filing
da710 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46 50 20   Protocol.  AFP 
da720 69 73 20 61 20 6e 65 74 77 6f 72 6b 20 66 69 6c  is a network fil
da730 65 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a  esystem found.**
da740 20 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69 6e 74   on Apple Macint
da750 6f 73 68 20 63 6f 6d 70 75 74 65 72 73 20 2d 20  osh computers - 
da760 62 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f 53 58  both OS9 and OSX
da770 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d 70 61  ..**.** Third-pa
da780 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  rty implementati
da790 6f 6e 73 20 6f 66 20 41 46 50 20 61 72 65 20 61  ons of AFP are a
da7a0 76 61 69 6c 61 62 6c 65 2e 20 20 42 75 74 20 74  vailable.  But t
da7b0 68 69 73 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a  his code here.**
da7c0 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 4f   only works on O
da7d0 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69  SX..*/..#if defi
da7e0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26  ned(__APPLE__) &
da7f0 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
da800 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a  LOCKING_STYLE./*
da810 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b 69  .** The afpLocki
da820 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74  ngContext struct
da830 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  ure contains all
da840 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 69 66   afp lock specif
da850 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79 70 65  ic state.*/.type
da860 64 65 66 20 73 74 72 75 63 74 20 61 66 70 4c 6f  def struct afpLo
da870 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 66 70  ckingContext afp
da880 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  LockingContext;.
da890 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e  struct afpLockin
da8a0 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73  gContext {.  uns
da8b0 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
da8c0 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 63 6f  sharedByte;.  co
da8d0 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 68  nst char *dbPath
da8e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
da8f0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65   Name of the ope
da900 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74  n file */.};..st
da910 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f  ruct ByteRangeLo
da920 63 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e  ckPB2.{.  unsign
da930 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66  ed long long off
da940 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f  set;        /* o
da950 66 66 73 65 74 20 74 6f 20 66 69 72 73 74 20 62  ffset to first b
da960 79 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20  yte to lock */. 
da970 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
da980 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20 20 20  ong length;     
da990 20 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74     /* nbr of byt
da9a0 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20  es to lock */.  
da9b0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
da9c0 6e 67 20 72 65 74 52 61 6e 67 65 53 74 61 72 74  ng retRangeStart
da9d0 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73 74 20  ; /* nbr of 1st 
da9e0 62 79 74 65 20 6c 6f 63 6b 65 64 20 69 66 20 73  byte locked if s
da9f0 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75  uccessful */.  u
daa00 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 6e 4c  nsigned char unL
daa10 6f 63 6b 46 6c 61 67 3b 20 20 20 20 20 20 20 20  ockFlag;        
daa20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20   /* 1 = unlock, 
daa30 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e  0 = lock */.  un
daa40 73 69 67 6e 65 64 20 63 68 61 72 20 73 74 61 72  signed char star
daa50 74 45 6e 64 46 6c 61 67 3b 20 20 20 20 20 20 20  tEndFlag;       
daa60 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20  /* 1=rel to end 
daa70 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74  of fork, 0=rel t
daa80 6f 20 73 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74  o start */.  int
daa90 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20   fd;            
daaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
daab0 2a 20 66 69 6c 65 20 64 65 73 63 20 74 6f 20 61  * file desc to a
daac0 73 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b 20 77  ssoc this lock w
daad0 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69  ith */.};..#defi
daae0 6e 65 20 61 66 70 66 73 42 79 74 65 52 61 6e 67  ne afpfsByteRang
daaf0 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20 20 20  eLock2FSCTL     
dab00 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33     _IOWR('z', 23
dab10 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e  , struct ByteRan
dab20 67 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a  geLockPB2)../*.*
dab30 2a 20 54 68 69 73 20 69 73 20 61 20 75 74 69 6c  * This is a util
dab40 69 74 79 20 66 6f 72 20 73 65 74 74 69 6e 67 20  ity for setting 
dab50 6f 72 20 63 6c 65 61 72 69 6e 67 20 61 20 62 69  or clearing a bi
dab60 74 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20  t-range lock on 
dab70 61 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65 73 79  an.** AFP filesy
dab80 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74  stem..** .** Ret
dab90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  urn SQLITE_OK on
daba0 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45   success, SQLITE
dabb0 5f 42 55 53 59 20 6f 6e 20 66 61 69 6c 75 72 65  _BUSY on failure
dabc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
dabd0 61 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20 63 6f  afpSetLock(.  co
dabe0 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20  nst char *path, 
dabf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
dac00 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  Name of the file
dac10 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 6f 72   to be locked or
dac20 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75   unlocked */.  u
dac30 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20  nixFile *pFile, 
dac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
dac50 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72   Open file descr
dac60 69 70 74 6f 72 20 6f 6e 20 70 61 74 68 20 2a 2f  iptor on path */
dac70 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  .  unsigned long
dac80 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 20 20   long offset,   
dac90 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
daca0 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a  to be locked */.
dacb0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
dacc0 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20 20 20  long length,    
dacd0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
dace0 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20  tes to lock */. 
dacf0 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c 61 67   int setLockFlag
dad00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dad10 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 6c  /* True to set l
dad20 6f 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f 20 63  ock.  False to c
dad30 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a  lear lock */.){.
dad40 20 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e    struct ByteRan
dad50 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a 20 20  geLockPB2 pb;.  
dad60 69 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20 70 62  int err;.  .  pb
dad70 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 65  .unLockFlag = se
dad80 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a 20  tLockFlag ? 0 : 
dad90 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e 64  1;.  pb.startEnd
dada0 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e 6f  Flag = 0;.  pb.o
dadb0 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b 0a  ffset = offset;.
dadc0 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c 65    pb.length = le
dadd0 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20 3d  ngth; .  pb.fd =
dade0 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20   pFile->h;.  .  
dadf0 4f 53 54 52 41 43 45 36 28 22 41 46 50 53 45 54  OSTRACE6("AFPSET
dae00 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20 25 64  LOCK [%s] for %d
dae10 25 73 20 69 6e 20 72 61 6e 67 65 20 25 6c 6c 78  %s in range %llx
dae20 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 28  :%llx\n", .    (
dae30 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22  setLockFlag?"ON"
dae40 3a 22 4f 46 46 22 29 2c 20 70 46 69 6c 65 2d 3e  :"OFF"), pFile->
dae50 68 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b  h, (pb.fd==-1?"[
dae60 74 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22 29 2c  testval-1]":""),
dae70 0a 20 20 20 20 6f 66 66 73 65 74 2c 20 6c 65 6e  .    offset, len
dae80 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20 66 73  gth);.  err = fs
dae90 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66 73 42  ctl(path, afpfsB
daea0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43  yteRangeLock2FSC
daeb0 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 20 69  TL, &pb, 0);.  i
daec0 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 7b 0a  f ( err==-1 ) {.
daed0 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
daee0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
daef0 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 34  no;.    OSTRACE4
daf00 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66 61 69  ("AFPSETLOCK fai
daf10 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29 20 27  led to fsctl() '
daf20 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20  %s' %d %s\n",.  
daf30 20 20 20 20 20 20 20 20 20 20 20 70 61 74 68 2c             path,
daf40 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72 72 6f   tErrno, strerro
daf50 72 28 74 45 72 72 6e 6f 29 29 3b 0a 23 69 66 64  r(tErrno));.#ifd
daf60 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  ef SQLITE_IGNORE
daf70 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53  _AFP_LOCK_ERRORS
daf80 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
daf90 5f 42 55 53 59 3b 0a 23 65 6c 73 65 0a 20 20 20  _BUSY;.#else.   
dafa0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
dafb0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
dafc0 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20  tErrno,.        
dafd0 20 20 20 20 20 20 20 20 20 20 20 20 73 65 74 4c              setL
dafe0 6f 63 6b 46 6c 61 67 20 3f 20 53 51 4c 49 54 45  ockFlag ? SQLITE
daff0 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51  _IOERR_LOCK : SQ
db000 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
db010 4b 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  K);.#endif /* SQ
db020 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f  LITE_IGNORE_AFP_
db030 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20  LOCK_ERRORS */. 
db040 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
db050 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
db060 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
db070 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
db080 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63   }.    return rc
db090 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
db0a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
db0b0 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  K;.  }.}../*.** 
db0c0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
db0d0 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20  cks if there is 
db0e0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
db0f0 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63  held on the spec
db100 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79  ified.** file by
db110 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
db120 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73  er process. If s
db130 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65  uch a lock is he
db140 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74  ld, set *pResOut
db150 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  .** to a non-zer
db160 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73  o value otherwis
db170 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65  e *pResOut is se
db180 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20  t to zero.  The 
db190 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
db1a0 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
db1b0 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f  _OK unless an I/
db1c0 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  O error occurs d
db1d0 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b  uring lock check
db1e0 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
db1f0 6e 74 20 61 66 70 43 68 65 63 6b 52 65 73 65 72  nt afpCheckReser
db200 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
db210 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
db220 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72  ResOut){.  int r
db230 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
db240 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20   int reserved = 
db250 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  0;.  unixFile *p
db260 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
db270 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c  *)id;.  .  Simul
db280 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
db290 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
db2a0 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43  CHECKRESERVEDLOC
db2b0 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72  K; );.  .  asser
db2c0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 66  t( pFile );.  af
db2d0 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  pLockingContext 
db2e0 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c  *context = (afpL
db2f0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
db300 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43   pFile->lockingC
db310 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 2f 2a 20  ontext;.  .  /* 
db320 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61  Check if a threa
db330 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73  d in this proces
db340 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c  s holds such a l
db350 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ock */.  if( pFi
db360 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41  le->locktype>SHA
db370 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
db380 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
db390 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77  }.  .  /* Otherw
db3a0 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20  ise see if some 
db3b0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f  other process ho
db3c0 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a 20 20  lds it..   */.  
db3d0 69 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b  if( !reserved ){
db3e0 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 74 68 65  .    /* lock the
db3f0 20 52 45 53 45 52 56 45 44 20 62 79 74 65 20 2a   RESERVED byte *
db400 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 3d 20  /.    int lrc = 
db410 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65  afpSetLock(conte
db420 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  xt->dbPath, pFil
db430 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  e, RESERVED_BYTE
db440 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 69 66  , 1,1);  .    if
db450 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63  ( SQLITE_OK==lrc
db460 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20   ){.      /* if 
db470 77 65 20 73 75 63 63 65 65 64 65 64 20 69 6e 20  we succeeded in 
db480 74 61 6b 69 6e 67 20 74 68 65 20 72 65 73 65 72  taking the reser
db490 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b  ved lock, unlock
db4a0 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 0a 20   it to restore. 
db4b0 20 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 69 67       ** the orig
db4c0 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a 20 20  inal state */.  
db4d0 20 20 20 20 6c 72 63 20 3d 20 61 66 70 53 65 74      lrc = afpSet
db4e0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
db4f0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53  Path, pFile, RES
db500 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30  ERVED_BYTE, 1, 0
db510 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  );.    } else {.
db520 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 66        /* if we f
db530 61 69 6c 65 64 20 74 6f 20 67 65 74 20 74 68 65  ailed to get the
db540 20 6c 6f 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f   lock then someo
db550 6e 65 20 65 6c 73 65 20 6d 75 73 74 20 68 61 76  ne else must hav
db560 65 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 72 65  e it */.      re
db570 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20  served = 1;.    
db580 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43  }.    if( IS_LOC
db590 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a  K_ERROR(lrc) ){.
db5a0 20 20 20 20 20 20 72 63 3d 6c 72 63 3b 0a 20 20        rc=lrc;.  
db5b0 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54    }.  }.  .  OST
db5c0 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c  RACE4("TEST WR-L
db5d0 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c  OCK %d %d %d\n",
db5e0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72   pFile->h, rc, r
db5f0 65 73 65 72 76 65 64 29 3b 0a 20 20 0a 20 20 2a  eserved);.  .  *
db600 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76  pResOut = reserv
db610 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ed;.  return rc;
db620 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
db630 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
db640 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
db650 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
db660 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
db670 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
db680 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
db690 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
db6a0 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
db6b0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
db6c0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
db6d0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
db6e0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
db6f0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
db700 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
db710 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
db720 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
db730 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
db740 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
db750 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
db760 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
db770 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
db780 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
db790 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
db7a0 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
db7b0 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
db7c0 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
db7d0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
db7e0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
db7f0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
db800 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
db810 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
db820 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
db830 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
db840 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
db850 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
db860 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
db870 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
db880 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
db890 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
db8a0 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
db8b0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
db8c0 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
db8d0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
db8e0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
db8f0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
db900 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
db910 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a  ite3OsUnlock().*
db920 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77  * routine to low
db930 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  er a locking lev
db940 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  el..*/.static in
db950 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65  t afpLock(sqlite
db960 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
db970 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74  locktype){.  int
db980 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
db990 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
db9a0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
db9b0 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67  id;.  afpLocking
db9c0 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  Context *context
db9d0 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f   = (afpLockingCo
db9e0 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e  ntext *) pFile->
db9f0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
dba00 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
dba10 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35  le );.  OSTRACE5
dba20 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
dba30 77 61 73 20 25 73 20 70 69 64 3d 25 64 5c 6e 22  was %s pid=%d\n"
dba40 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
dba50 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d       locktypeNam
dba60 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63  e(locktype), loc
dba70 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d  ktypeName(pFile-
dba80 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 67 65 74 70  >locktype), getp
dba90 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  id());..  /* If 
dbaa0 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79  there is already
dbab0 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20   a lock of this 
dbac0 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73  type or more res
dbad0 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a  trictive on the.
dbae0 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64    ** unixFile, d
dbaf0 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74  o nothing. Don't
dbb00 20 75 73 65 20 74 68 65 20 61 66 70 5f 65 6e 64   use the afp_end
dbb10 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68  _lock: exit path
dbb20 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e  , as.  ** unixEn
dbb30 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27  terMutex() hasn'
dbb40 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65  t been called ye
dbb50 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46  t..  */.  if( pF
dbb60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c  ile->locktype>=l
dbb70 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f  ocktype ){.    O
dbb80 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20  STRACE3("LOCK   
dbb90 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61   %d %s ok (alrea
dbba0 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69  dy held)\n", pFi
dbbb0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
dbbc0 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c    locktypeName(l
dbbd0 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72  ocktype));.    r
dbbe0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
dbbf0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
dbc00 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  sure the locking
dbc10 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72   sequence is cor
dbc20 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65  rect.  */.  asse
dbc30 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
dbc40 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20  ype!=NO_LOCK || 
dbc50 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
dbc60 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
dbc70 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e  t( locktype!=PEN
dbc80 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  DING_LOCK );.  a
dbc90 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
dbca0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c  =RESERVED_LOCK |
dbcb0 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  | pFile->locktyp
dbcc0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
dbcd0 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 6d  ;.  .  /* This m
dbce0 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 62  utex is needed b
dbcf0 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c  ecause pFile->pL
dbd00 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63  ock is shared ac
dbd10 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a  ross threads.  *
dbd20 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  /.  unixEnterMut
dbd30 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ex();..  /* Make
dbd40 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e   sure the curren
dbd50 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68  t thread owns th
dbd60 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20  e pFile..  */.  
dbd70 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e  rc = transferOwn
dbd80 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20  ership(pFile);. 
dbd90 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
dbda0 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65  OK ){.    unixLe
dbdb0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  aveMutex();.    
dbdc0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
dbdd0 20 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49     .  /* A PENDI
dbde0 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65  NG lock is neede
dbdf0 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69  d before acquiri
dbe00 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ng a SHARED lock
dbe10 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a   and before.  **
dbe20 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58   acquiring an EX
dbe30 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46  CLUSIVE lock.  F
dbe40 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f  or the SHARED lo
dbe50 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20  ck, the PENDING 
dbe60 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c  will.  ** be rel
dbe70 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  eased..  */.  if
dbe80 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
dbe90 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c  ED_LOCK .      |
dbea0 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  | (locktype==EXC
dbeb0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
dbec0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50  File->locktype<P
dbed0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29  ENDING_LOCK).  )
dbee0 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64  {.    int failed
dbef0 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61  ;.    failed = a
dbf00 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  fpSetLock(contex
dbf10 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  t->dbPath, pFile
dbf20 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
dbf30 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66  1, 1);.    if (f
dbf40 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72  ailed) {.      r
dbf50 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  c = failed;.    
dbf60 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c    goto afp_end_l
dbf70 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ock;.    }.  }. 
dbf80 20 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f   .  /* If contro
dbf90 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70  l gets to this p
dbfa0 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61  oint, then actua
dbfb0 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64  lly go ahead and
dbfc0 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61   make.  ** opera
dbfd0 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c  ting system call
dbfe0 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66  s for the specif
dbff0 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20  ied lock..  */. 
dc000 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
dc010 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
dc020 20 20 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20    int lk, lrc1, 
dc030 6c 72 63 32 3b 0a 20 20 20 20 69 6e 74 20 6c 72  lrc2;.    int lr
dc040 63 31 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 20  c1Errno = 0;.   
dc050 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74   .    /* Now get
dc060 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53   the read-lock S
dc070 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20  HARED_LOCK */.  
dc080 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74    /* note that t
dc090 68 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68  he quality of th
dc0a0 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65  e randomness doe
dc0b0 73 6e 27 74 20 6d 61 74 74 65 72 20 74 68 61 74  sn't matter that
dc0c0 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20   much */.    lk 
dc0d0 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20  = random(); .   
dc0e0 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64   context->shared
dc0f0 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37  Byte = (lk & 0x7
dc100 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44  fffffff)%(SHARED
dc110 5f 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20  _SIZE - 1);.    
dc120 6c 72 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63  lrc1 = afpSetLoc
dc130 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
dc140 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20  h, pFile, .     
dc150 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53       SHARED_FIRS
dc160 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65  T+context->share
dc170 64 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20  dByte, 1, 1);.  
dc180 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
dc190 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20  ROR(lrc1) ){.   
dc1a0 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70     lrc1Errno = p
dc1b0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b  File->lastErrno;
dc1c0 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72  .    }.    /* Dr
dc1d0 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  op the temporary
dc1e0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f   PENDING lock */
dc1f0 0a 20 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53  .    lrc2 = afpS
dc200 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
dc210 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50  dbPath, pFile, P
dc220 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20  ENDING_BYTE, 1, 
dc230 30 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28  0);.    .    if(
dc240 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
dc250 72 63 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70  rc1) ) {.      p
dc260 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
dc270 3d 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20  = lrc1Errno;.   
dc280 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20     rc = lrc1;.  
dc290 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64      goto afp_end
dc2a0 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73  _lock;.    } els
dc2b0 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52  e if( IS_LOCK_ER
dc2c0 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20  ROR(lrc2) ){.   
dc2d0 20 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20     rc = lrc2;.  
dc2e0 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64      goto afp_end
dc2f0 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73  _lock;.    } els
dc300 65 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51  e if( lrc1 != SQ
dc310 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20  LITE_OK ) {.    
dc320 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20    rc = lrc1;.   
dc330 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
dc340 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
dc350 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
dc360 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65       pFile->pOpe
dc370 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20  n->nLock++;.    
dc380 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  }.  }else{.    /
dc390 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61  * The request wa
dc3a0 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44  s for a RESERVED
dc3b0 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   or EXCLUSIVE lo
dc3c0 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a  ck.  It is.    *
dc3d0 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  * assumed that t
dc3e0 68 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44  here is a SHARED
dc3f0 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
dc400 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20   on the file.   
dc410 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20   ** already..   
dc420 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c   */.    int fail
dc430 65 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65  ed = 0;.    asse
dc440 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f  rt( 0!=pFile->lo
dc450 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66  cktype );.    if
dc460 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45   (locktype >= RE
dc470 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70  SERVED_LOCK && p
dc480 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c  File->locktype <
dc490 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20   RESERVED_LOCK) 
dc4a0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71  {.        /* Acq
dc4b0 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20  uire a RESERVED 
dc4c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20  lock */.        
dc4d0 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c  failed = afpSetL
dc4e0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
dc4f0 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45  ath, pFile, RESE
dc500 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b  RVED_BYTE, 1,1);
dc510 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21  .    }.    if (!
dc520 66 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79  failed && lockty
dc530 70 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f  pe == EXCLUSIVE_
dc540 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a  LOCK) {.      /*
dc550 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c   Acquire an EXCL
dc560 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20  USIVE lock */.  
dc570 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20        .      /* 
dc580 52 65 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65  Remove the share
dc590 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72  d lock before tr
dc5a0 79 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20  ying the range. 
dc5b0 20 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a   we'll need to .
dc5c0 20 20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62        ** reestab
dc5d0 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20  lish the shared 
dc5e0 6c 6f 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74  lock if we can't
dc5f0 20 67 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c   get the  afpUnl
dc600 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ock.      */.   
dc610 20 20 20 69 66 28 20 21 28 66 61 69 6c 65 64 20     if( !(failed 
dc620 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e  = afpSetLock(con
dc630 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46  text->dbPath, pF
dc640 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ile, SHARED_FIRS
dc650 54 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20  T +.            
dc660 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
dc670 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65  text->sharedByte
dc680 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20  , 1, 0)) ){.    
dc690 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20      int failed2 
dc6a0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
dc6b0 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65       /* now atte
dc6c0 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20  mmpt to get the 
dc6d0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72  exclusive lock r
dc6e0 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  ange */.        
dc6f0 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c  failed = afpSetL
dc700 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
dc710 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52  ath, pFile, SHAR
dc720 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20  ED_FIRST, .     
dc730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dc740 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44            SHARED
dc750 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20  _SIZE, 1);.     
dc760 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 26 26     if( failed &&
dc770 20 28 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53   (failed2 = afpS
dc780 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
dc790 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a  dbPath, pFile, .
dc7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dc7b0 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49         SHARED_FI
dc7c0 52 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73  RST + context->s
dc7d0 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29  haredByte, 1, 1)
dc7e0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  ) ){.          /
dc7f0 2a 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c  * Can't reestabl
dc800 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c  ish the shared l
dc810 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e  ock.  Sqlite can
dc820 27 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73  't deal, this is
dc830 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20  .          ** a 
dc840 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72  critical I/O err
dc850 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  or.          */.
dc860 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 28            rc = (
dc870 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45  (failed & SQLITE
dc880 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54  _IOERR) == SQLIT
dc890 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65  E_IOERR) ? faile
dc8a0 64 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20  d2 : .          
dc8b0 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52       SQLITE_IOER
dc8c0 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20  R_LOCK;.        
dc8d0 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c    goto afp_end_l
dc8e0 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a  ock;.        } .
dc8f0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
dc900 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64       rc = failed
dc910 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ; .      }.    }
dc920 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20  .    if( failed 
dc930 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61  ){.      rc = fa
dc940 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  iled;.    }.  }.
dc950 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c    .  if( rc==SQL
dc960 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46  ITE_OK ){.    pF
dc970 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
dc980 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73  locktype;.  }els
dc990 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d  e if( locktype==
dc9a0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
dc9b0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
dc9c0 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f  ktype = PENDING_
dc9d0 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70  LOCK;.  }.  .afp
dc9e0 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69  _end_lock:.  uni
dc9f0 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
dca00 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20   OSTRACE4("LOCK 
dca10 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20     %d %s %s\n", 
dca20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
dca30 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29  peName(locktype)
dca40 2c 20 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d  , .         rc==
dca50 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22  SQLITE_OK ? "ok"
dca60 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20   : "failed");.  
dca70 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
dca80 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
dca90 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
dcaa0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
dcab0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
dcac0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
dcad0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
dcae0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
dcaf0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
dcb00 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
dcb10 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
dcb20 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
dcb30 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
dcb40 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
dcb50 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
dcb60 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
dcb70 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
dcb80 63 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28  c int afpUnlock(
dcb90 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
dcba0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
dcbb0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
dcbc0 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69  ITE_OK;.  unixFi
dcbd0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
dcbe0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70  xFile*)id;.  afp
dcbf0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
dcc00 70 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69  pCtx = (afpLocki
dcc10 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
dcc20 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
dcc30 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  xt;..  assert( p
dcc40 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
dcc50 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25  E5("UNLOCK  %d %
dcc60 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c  d was %d pid=%d\
dcc70 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
dcc80 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20  cktype,.        
dcc90 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dcca0 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
dccb0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
dccc0 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
dccd0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
dcce0 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65  cktype<=locktype
dccf0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
dcd00 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
dcd10 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44  if( CHECK_THREAD
dcd20 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20  ID(pFile) ){.   
dcd30 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
dcd40 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69  ISUSE;.  }.  uni
dcd50 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
dcd60 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
dcd70 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
dcd80 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28   ){.    .    if(
dcd90 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dcda0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
dcdb0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
dcdc0 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e  fpSetLock(pCtx->
dcdd0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53  dbPath, pFile, S
dcde0 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41  HARED_FIRST, SHA
dcdf0 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20  RED_SIZE, 0);.  
dce00 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
dce10 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70  TE_OK && locktyp
dce20 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
dce30 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c  {.        /* onl
dce40 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74  y re-establish t
dce50 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69  he shared lock i
dce60 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20  f necessary */. 
dce70 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65         int share
dce80 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52  dLockByte = SHAR
dce90 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73  ED_FIRST+pCtx->s
dcea0 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20  haredByte;.     
dceb0 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f     rc = afpSetLo
dcec0 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c  ck(pCtx->dbPath,
dced0 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f   pFile, sharedLo
dcee0 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20  ckByte, 1, 1);. 
dcef0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
dcf00 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
dcf10 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  OK && pFile->loc
dcf20 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c  ktype>=PENDING_L
dcf30 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  OCK ){.      rc 
dcf40 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74  = afpSetLock(pCt
dcf50 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
dcf60 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
dcf70 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20  1, 0);.    } .  
dcf80 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
dcf90 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f  _OK && pFile->lo
dcfa0 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44  cktype>=RESERVED
dcfb0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72  _LOCK ){.      r
dcfc0 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70  c = afpSetLock(p
dcfd0 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  Ctx->dbPath, pFi
dcfe0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
dcff0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  E, 1, 0);.    }.
dd000 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b    }else if( lock
dd010 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  type==NO_LOCK ){
dd020 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68  .    /* clear th
dd030 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f  e shared lock */
dd040 0a 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c  .    int sharedL
dd050 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44  ockByte = SHARED
dd060 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61  _FIRST+pCtx->sha
dd070 72 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20  redByte;.    rc 
dd080 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74  = afpSetLock(pCt
dd090 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
dd0a0 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  , sharedLockByte
dd0b0 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  , 1, 0);.  }..  
dd0c0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
dd0d0 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63  K ){.    if( loc
dd0e0 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29  ktype==NO_LOCK )
dd0f0 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75  {.      struct u
dd100 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65  nixOpenCnt *pOpe
dd110 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  n = pFile->pOpen
dd120 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e  ;.      pOpen->n
dd130 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73  Lock--;.      as
dd140 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f  sert( pOpen->nLo
dd150 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  ck>=0 );.      i
dd160 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d  f( pOpen->nLock=
dd170 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
dd180 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46   = closePendingF
dd190 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20  ds(pFile);.     
dd1a0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75   }.    }.  }.  u
dd1b0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
dd1c0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
dd1d0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c  E_OK ){.    pFil
dd1e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
dd1f0 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65  cktype;.  }.  re
dd200 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
dd210 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26  * Close a file &
dd220 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65   cleanup AFP spe
dd230 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f  cific locking co
dd240 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63  ntext .*/.static
dd250 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71   int afpClose(sq
dd260 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20  lite3_file *id) 
dd270 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20  {.  if( id ){.  
dd280 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
dd290 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
dd2a0 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b  d;.    afpUnlock
dd2b0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  (id, NO_LOCK);. 
dd2c0 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65     unixEnterMute
dd2d0 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69  x();.    if( pFi
dd2e0 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69  le->pOpen && pFi
dd2f0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b  le->pOpen->nLock
dd300 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
dd310 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61  there are outsta
dd320 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20  nding locks, do 
dd330 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f  not actually clo
dd340 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74  se the file just
dd350 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65  .      ** yet be
dd360 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64  cause that would
dd370 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63   clear those loc
dd380 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64  ks.  Instead, ad
dd390 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20  d the file.     
dd3a0 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74   ** descriptor t
dd3b0 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e  o pOpen->aPendin
dd3c0 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61  g.  It will be a
dd3d0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f  utomatically clo
dd3e0 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a  sed when.      *
dd3f0 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20  * the last lock 
dd400 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20  is cleared..    
dd410 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65    */.      setPe
dd420 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a  ndingFd(pFile);.
dd430 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73      }.    releas
dd440 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e  eOpenCnt(pFile->
dd450 70 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69  pOpen);.    sqli
dd460 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e  te3_free(pFile->
dd470 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
dd480 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69  .    closeUnixFi
dd490 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78  le(id);.    unix
dd4a0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
dd4b0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
dd4c0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  E_OK;.}..#endif 
dd4d0 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  /* defined(__APP
dd4e0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
dd4f0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
dd500 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68  TYLE */./*.** Th
dd510 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20  e code above is 
dd520 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70  the AFP lock imp
dd530 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
dd540 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66  e code is specif
dd550 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20  ic.** to MacOSX 
dd560 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  and does not wor
dd570 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20  k on other unix 
dd580 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61  platforms.  No a
dd590 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73  lternative.** is
dd5a0 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20   available.  If 
dd5b0 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c  you don't compil
dd5c0 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65  e for a mac, the
dd5d0 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22  n the "unix-afp"
dd5e0 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61  .** VFS is not a
dd5f0 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a  vailable..**.***
dd600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd610 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46  ** End of the AF
dd620 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  P lock implement
dd630 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ation **********
dd640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
dd650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f  ***********/.../
dd6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
dd6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
dd700 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69  Non-locking sqli
dd710 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
dd720 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
dd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
dd740 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 64 69  *.** The next di
dd750 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  vision contains 
dd760 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
dd770 66 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20  for all methods 
dd780 6f 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  of the .** sqlit
dd790 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f  e3_file object o
dd7a0 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6c 6f  ther than the lo
dd7b0 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20  cking methods.  
dd7c0 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d  The locking.** m
dd7d0 65 74 68 6f 64 73 20 77 65 72 65 20 64 65 66 69  ethods were defi
dd7e0 6e 65 64 20 69 6e 20 64 69 76 69 73 69 6f 6e 73  ned in divisions
dd7f0 20 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b   above (one lock
dd800 69 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a  ing method per.*
dd810 2a 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 54 68  * division).  Th
dd820 6f 73 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74  ose methods that
dd830 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61   are common to a
dd840 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73  ll locking modes
dd850 0a 2a 2a 20 61 72 65 20 67 61 74 68 65 72 20 74  .** are gather t
dd860 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 74 68 69  ogether into thi
dd870 73 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a  s division..*/..
dd880 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68  /*.** Seek to th
dd890 65 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20  e offset passed 
dd8a0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
dd8b0 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61  gument, then rea
dd8c0 64 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20  d cnt .** bytes 
dd8d0 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72  into pBuf. Retur
dd8e0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
dd8f0 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72  bytes actually r
dd900 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20  ead..**.** NB:  
dd910 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20 55 53  If you define US
dd920 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45 5f 50  E_PREAD or USE_P
dd930 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69 74 20  READ64, then it 
dd940 6d 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65  might also.** be
dd950 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 65   necessary to de
dd960 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52  fine _XOPEN_SOUR
dd970 43 45 20 74 6f 20 62 65 20 35 30 30 2e 20 20 54  CE to be 500.  T
dd980 68 69 73 20 76 61 72 69 65 73 20 66 72 6f 6d 0a  his varies from.
dd990 2a 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f  ** one system to
dd9a0 20 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e 63 65   another.  Since
dd9b0 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74   SQLite does not
dd9c0 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41   define USE_PREA
dd9d0 44 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72  D.** any any for
dd9e0 6d 20 62 79 20 64 65 66 61 75 6c 74 2c 20 77 65  m by default, we
dd9f0 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70   will not attemp
dda00 74 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50  t to define _XOP
dda10 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65  EN_SOURCE..** Se
dda20 65 20 74 69 63 6b 65 74 73 20 23 32 37 34 31 20  e tickets #2741 
dda30 61 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a  and #2681..**.**
dda40 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69   To avoid stompi
dda50 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c  ng the errno val
dda60 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 72  ue on a failed r
dda70 65 61 64 20 74 68 65 20 6c 61 73 74 45 72 72 6e  ead the lastErrn
dda80 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  o value.** is se
dda90 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  t before returni
ddaa0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
ddab0 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e  t seekAndRead(un
ddac0 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  ixFile *id, sqli
ddad0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
ddae0 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e  , void *pBuf, in
ddaf0 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f  t cnt){.  int go
ddb00 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73  t;.  i64 newOffs
ddb10 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52  et;.  TIMER_STAR
ddb20 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55  T;.#if defined(U
ddb30 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20  SE_PREAD).  got 
ddb40 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70  = pread(id->h, p
ddb50 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
ddb60 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
ddb70 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29  rror( got = -1 )
ddb80 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28  ;.#elif defined(
ddb90 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67  USE_PREAD64).  g
ddba0 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d  ot = pread64(id-
ddbb0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
ddbc0 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61  ffset);.  Simula
ddbd0 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d  teIOError( got =
ddbe0 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e   -1 );.#else.  n
ddbf0 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b  ewOffset = lseek
ddc00 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20  (id->h, offset, 
ddc10 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d  SEEK_SET);.  Sim
ddc20 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65  ulateIOError( ne
ddc30 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69  wOffset-- );.  i
ddc40 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66  f( newOffset!=of
ddc50 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20  fset ){.    if( 
ddc60 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20  newOffset == -1 
ddc70 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46  ){.      ((unixF
ddc80 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
ddc90 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
ddca0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28   }else{.      ((
ddcb0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
ddcc0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09  astErrno = 0;...
ddcd0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
ddce0 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20  n -1;.  }.  got 
ddcf0 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42  = read(id->h, pB
ddd00 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66  uf, cnt);.#endif
ddd10 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20  .  TIMER_END;.  
ddd20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20  if( got<0 ){.   
ddd30 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
ddd40 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
ddd50 72 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41  rno;.  }.  OSTRA
ddd60 43 45 35 28 22 52 45 41 44 20 20 20 20 25 2d 33  CE5("READ    %-3
ddd70 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75  d %5d %7lld %llu
ddd80 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c  \n", id->h, got,
ddd90 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45   offset, TIMER_E
ddda0 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72  LAPSED);.  retur
dddb0 6e 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n got;.}../*.** 
dddc0 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  Read data from a
dddd0 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66   file into a buf
ddde0 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  fer.  Return SQL
dddf0 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a  ITE_OK if all.**
dde00 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64   bytes were read
dde10 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e   successfully an
dde20 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69  d SQLITE_IOERR i
dde30 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
dde40 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61  ** wrong..*/.sta
dde50 74 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64  tic int unixRead
dde60 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
dde70 20 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70   *id, .  void *p
dde80 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c  Buf, .  int amt,
dde90 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
ddea0 20 6f 66 66 73 65 74 0a 29 7b 0a 20 20 75 6e 69   offset.){.  uni
ddeb0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
ddec0 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20  unixFile *)id;. 
dded0 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73 73 65   int got;.  asse
ddee0 72 74 28 20 69 64 20 29 3b 0a 0a 20 20 2f 2a 20  rt( id );..  /* 
ddef0 49 66 20 74 68 69 73 20 69 73 20 61 20 64 61 74  If this is a dat
ddf00 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20  abase file (not 
ddf10 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 74 65  a journal, maste
ddf20 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 65 6d  r-journal or tem
ddf30 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 74 68  p.  ** file), th
ddf40 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6c  e bytes in the l
ddf50 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 68 6f  ocking range sho
ddf60 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72 65 61  uld never be rea
ddf70 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 2a 2f  d or written. */
ddf80 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
ddf90 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20 20 20  ->pUnused==0.   
ddfa0 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50      || offset>=P
ddfb0 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a  ENDING_BYTE+512.
ddfc0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74         || offset
ddfd0 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f 42 59  +amt<=PENDING_BY
ddfe0 54 45 20 0a 20 20 29 3b 0a 0a 20 20 67 6f 74 20  TE .  );..  got 
ddff0 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46  = seekAndRead(pF
de000 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 42 75  ile, offset, pBu
de010 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 20 67  f, amt);.  if( g
de020 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72  ot==amt ){.    r
de030 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
de040 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74  .  }else if( got
de050 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 61 73  <0 ){.    /* las
de060 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65  tErrno set by se
de070 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20  ekAndRead */.   
de080 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
de090 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 65 6c  OERR_READ;.  }el
de0a0 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  se{.    pFile->l
de0b0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a  astErrno = 0; /*
de0c0 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72   not a system er
de0d0 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e  ror */.    /* Un
de0e0 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 68  read parts of th
de0f0 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65  e buffer must be
de100 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a   zero-filled */.
de110 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68      memset(&((ch
de120 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20  ar*)pBuf)[got], 
de130 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20  0, amt-got);.   
de140 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
de150 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b  OERR_SHORT_READ;
de160 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
de170 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74  ek to the offset
de180 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74   in id->offset t
de190 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74  hen read cnt byt
de1a0 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a  es into pBuf..**
de1b0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
de1c0 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75  er of bytes actu
de1d0 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61  ally read.  Upda
de1e0 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a  te the offset..*
de1f0 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74  *.** To avoid st
de200 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f  omping the errno
de210 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c   value on a fail
de220 65 64 20 77 72 69 74 65 20 74 68 65 20 6c 61 73  ed write the las
de230 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20  tErrno value.** 
de240 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 65  is set before re
de250 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  turning..*/.stat
de260 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72  ic int seekAndWr
de270 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64  ite(unixFile *id
de280 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f  , i64 offset, co
de290 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  nst void *pBuf, 
de2a0 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20  int cnt){.  int 
de2b0 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66  got;.  i64 newOf
de2c0 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54  fset;.  TIMER_ST
de2d0 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  ART;.#if defined
de2e0 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f  (USE_PREAD).  go
de2f0 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68  t = pwrite(id->h
de300 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66  , pBuf, cnt, off
de310 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69  set);.#elif defi
de320 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29  ned(USE_PREAD64)
de330 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36  .  got = pwrite6
de340 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  4(id->h, pBuf, c
de350 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c  nt, offset);.#el
de360 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d  se.  newOffset =
de370 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66   lseek(id->h, of
de380 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b  fset, SEEK_SET);
de390 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74  .  if( newOffset
de3a0 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  !=offset ){.    
de3b0 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d  if( newOffset ==
de3c0 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75   -1 ){.      ((u
de3d0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
de3e0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
de3f0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
de400 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64    ((unixFile*)id
de410 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  )->lastErrno = 0
de420 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72  ;....    }.    r
de430 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20  eturn -1;.  }.  
de440 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e  got = write(id->
de450 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23  h, pBuf, cnt);.#
de460 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e  endif.  TIMER_EN
de470 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 29  D;.  if( got<0 )
de480 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65  {.    ((unixFile
de490 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  *)id)->lastErrno
de4a0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20   = errno;.  }.. 
de4b0 20 4f 53 54 52 41 43 45 35 28 22 57 52 49 54 45   OSTRACE5("WRITE
de4c0 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c     %-3d %5d %7ll
de4d0 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68  d %llu\n", id->h
de4e0 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54  , got, offset, T
de4f0 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20  IMER_ELAPSED);. 
de500 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a   return got;.}..
de510 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74  ./*.** Write dat
de520 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20  a from a buffer 
de530 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65  into a file.  Re
de540 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
de550 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20  n success.** or 
de560 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72  some other error
de570 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65   code on failure
de580 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
de590 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73 71 6c  unixWrite(.  sql
de5a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a  ite3_file *id, .
de5b0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42    const void *pB
de5c0 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a  uf, .  int amt,.
de5d0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
de5e0 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 75 6e 69  offset .){.  uni
de5f0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
de600 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
de610 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20  int wrote = 0;. 
de620 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20   assert( id );. 
de630 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29   assert( amt>0 )
de640 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
de650 69 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69  is a database fi
de660 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61  le (not a journa
de670 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  l, master-journa
de680 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66  l or temp.  ** f
de690 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65 73 20  ile), the bytes 
de6a0 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72  in the locking r
de6b0 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65  ange should neve
de6c0 72 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69  r be read or wri
de6d0 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72  tten. */.  asser
de6e0 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65  t( pFile->pUnuse
de6f0 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f  d==0.       || o
de700 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42  ffset>=PENDING_B
de710 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20 7c  YTE+512.       |
de720 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45  | offset+amt<=PE
de730 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b  NDING_BYTE .  );
de740 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
de750 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
de760 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77  doing a normal w
de770 72 69 74 65 20 74 6f 20 61 20 64 61 74 61 62 61  rite to a databa
de780 73 65 20 66 69 6c 65 20 28 61 73 20 6f 70 70 6f  se file (as oppo
de790 73 65 64 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e  sed to.  ** doin
de7a0 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  g a hot-journal 
de7b0 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72  rollback or a wr
de7c0 69 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65  ite to some file
de7d0 20 6f 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20   other than a.  
de7e0 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61  ** normal databa
de7f0 73 65 20 66 69 6c 65 29 20 74 68 65 6e 20 72 65  se file) then re
de800 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68  cord the fact th
de810 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  at the database.
de820 20 20 2a 2a 20 68 61 73 20 63 68 61 6e 67 65 64    ** has changed
de830 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61  .  If the transa
de840 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73  ction counter is
de850 20 6d 6f 64 69 66 69 65 64 2c 20 72 65 63 6f 72   modified, recor
de860 64 20 74 68 61 74 0a 20 20 2a 2a 20 66 61 63 74  d that.  ** fact
de870 20 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   too..  */.  if(
de880 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c   pFile->inNormal
de890 57 72 69 74 65 20 29 7b 0a 20 20 20 20 70 46 69  Write ){.    pFi
de8a0 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 31  le->dbUpdate = 1
de8b0 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61  ;  /* The databa
de8c0 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69  se has been modi
de8d0 66 69 65 64 20 2a 2f 0a 20 20 20 20 69 66 28 20  fied */.    if( 
de8e0 6f 66 66 73 65 74 3c 3d 32 34 20 26 26 20 6f 66  offset<=24 && of
de8f0 66 73 65 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a  fset+amt>=27 ){.
de900 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20        int rc;.  
de910 20 20 20 20 63 68 61 72 20 6f 6c 64 43 6e 74 72      char oldCntr
de920 5b 34 5d 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c  [4];.      Simul
de930 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
de940 28 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  (1);.      rc = 
de950 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c  seekAndRead(pFil
de960 65 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20  e, 24, oldCntr, 
de970 34 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61  4);.      Simula
de980 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
de990 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
de9a0 21 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c  !=4 || memcmp(ol
de9b0 64 43 6e 74 72 2c 20 26 28 28 63 68 61 72 2a 29  dCntr, &((char*)
de9c0 70 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d  pBuf)[24-offset]
de9d0 2c 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  , 4)!=0 ){.     
de9e0 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43     pFile->transC
de9f0 6e 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a  ntrChng = 1;  /*
dea00 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   The transaction
dea10 20 63 6f 75 6e 74 65 72 20 68 61 73 20 63 68 61   counter has cha
dea20 6e 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  nged */.      }.
dea30 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
dea40 0a 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30  ..  while( amt>0
dea50 20 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65   && (wrote = see
dea60 6b 41 6e 64 57 72 69 74 65 28 70 46 69 6c 65 2c  kAndWrite(pFile,
dea70 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61   offset, pBuf, a
dea80 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d  mt))>0 ){.    am
dea90 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20  t -= wrote;.    
deaa0 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b  offset += wrote;
deab0 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63  .    pBuf = &((c
deac0 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65  har*)pBuf)[wrote
dead0 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74  ];.  }.  Simulat
deae0 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65  eIOError(( wrote
deaf0 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b  =(-1), amt=1 ));
deb00 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  .  SimulateDiskf
deb10 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65  ullError(( wrote
deb20 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20  =0, amt=1 ));.  
deb30 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20  if( amt>0 ){.   
deb40 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a   if( wrote<0 ){.
deb50 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72        /* lastErr
deb60 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e  no set by seekAn
deb70 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20  dWrite */.      
deb80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
deb90 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d  ERR_WRITE;.    }
deba0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c  else{.      pFil
debb0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  e->lastErrno = 0
debc0 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65  ; /* not a syste
debd0 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20  m error */.     
debe0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46   return SQLITE_F
debf0 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ULL;.    }.  }. 
dec00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
dec10 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  K;.}..#ifdef SQL
dec20 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43  ITE_TEST./*.** C
dec30 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
dec40 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64  of fullsyncs and
dec50 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20   normal syncs.  
dec60 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
dec70 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e  test.** that syn
dec80 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73  cs and fullsyncs
dec90 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61   are occurring a
deca0 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65  t the right time
decb0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
decc0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e   int sqlite3_syn
decd0 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c  c_count = 0;.SQL
dece0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
decf0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
ded00 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
ded10 2f 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20  /*.** We do not 
ded20 74 72 75 73 74 20 73 79 73 74 65 6d 73 20 74 6f  trust systems to
ded30 20 70 72 6f 76 69 64 65 20 61 20 77 6f 72 6b 69   provide a worki
ded40 6e 67 20 66 64 61 74 61 73 79 6e 63 28 29 2e 20  ng fdatasync(). 
ded50 20 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68   Some do..** Oth
ded60 65 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62  ers do no.  To b
ded70 65 20 73 61 66 65 2c 20 77 65 20 77 69 6c 6c 20  e safe, we will 
ded80 73 74 69 63 6b 20 77 69 74 68 20 74 68 65 20 28  stick with the (
ded90 73 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 29 2e  slower) fsync().
deda0 0a 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20  .** If you know 
dedb0 74 68 61 74 20 79 6f 75 72 20 73 79 73 74 65 6d  that your system
dedc0 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 66 64   does support fd
dedd0 61 74 61 73 79 6e 63 28 29 20 63 6f 72 72 65 63  atasync() correc
dede0 74 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d  tly,.** then sim
dedf0 70 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 74 68  ply compile with
dee00 20 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 64 61   -Dfdatasync=fda
dee10 74 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64  tasync.*/.#if !d
dee20 65 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e 63  efined(fdatasync
dee30 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f  ) && !defined(__
dee40 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e  linux__).# defin
dee50 65 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e  e fdatasync fsyn
dee60 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  c.#endif../*.** 
dee70 44 65 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c  Define HAVE_FULL
dee80 46 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 31 20  FSYNC to 0 or 1 
dee90 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65  depending on whe
deea0 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74  ther or not.** t
deeb0 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d  he F_FULLFSYNC m
deec0 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e  acro is defined.
deed0 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73    F_FULLFSYNC is
deee0 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e   currently.** on
deef0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  ly available on 
def00 4d 61 63 20 4f 53 20 58 2e 20 20 42 75 74 20 74  Mac OS X.  But t
def10 68 61 74 20 63 6f 75 6c 64 20 63 68 61 6e 67 65  hat could change
def20 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f 46 55  ..*/.#ifdef F_FU
def30 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 6e 65  LLFSYNC.# define
def40 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20   HAVE_FULLFSYNC 
def50 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  1.#else.# define
def60 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20   HAVE_FULLFSYNC 
def70 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
def80 20 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73   The fsync() sys
def90 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f  tem call does no
defa0 74 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74  t work as advert
defb0 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20  ised on many.** 
defc0 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54  unix systems.  T
defd0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f  he following pro
defe0 63 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74  cedure is an att
deff0 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20  empt to make.** 
df000 69 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a  it work better..
df010 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
df020 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64  _NO_SYNC macro d
df030 69 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e  isables all fsyn
df040 63 28 29 73 2e 20 20 54 68 69 73 20 69 73 20 75  c()s.  This is u
df050 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73  seful.** for tes
df060 74 69 6e 67 20 77 68 65 6e 20 77 65 20 77 61 6e  ting when we wan
df070 74 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 67 68  t to run through
df080 20 74 68 65 20 74 65 73 74 20 73 75 69 74 65 20   the test suite 
df090 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20  quickly..** You 
df0a0 61 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 64 76  are strongly adv
df0b0 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65  ised *not* to de
df0c0 70 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 54 45  ploy with SQLITE
df0d0 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62  _NO_SYNC.** enab
df0e0 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 73 69  led, however, si
df0f0 6e 63 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f  nce with SQLITE_
df100 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c  NO_SYNC enabled,
df110 20 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20   an OS crash.** 
df120 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65  or power failure
df130 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72   will likely cor
df140 72 75 70 74 20 74 68 65 20 64 61 74 61 62 61 73  rupt the databas
df150 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51  e file..**.** SQ
df160 4c 69 74 65 20 73 65 74 73 20 74 68 65 20 64 61  Lite sets the da
df170 74 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 20 74  taOnly flag if t
df180 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  he size of the f
df190 69 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ile is unchanged
df1a0 2e 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 62 65  ..** The idea be
df1b0 68 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 69 73  hind dataOnly is
df1c0 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20   that it should 
df1d0 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 65 20 66  only write the f
df1e0 69 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74  ile content.** t
df1f0 6f 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20  o disk, not the 
df200 69 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20  inode.  We only 
df210 73 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 66 20  set dataOnly if 
df220 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73  the file size is
df230 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 73   .** unchanged s
df240 69 6e 63 65 20 74 68 65 20 66 69 6c 65 20 73 69  ince the file si
df250 7a 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  ze is part of th
df260 65 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65  e inode.  Howeve
df270 72 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20  r, .** Ted Ts'o 
df280 74 65 6c 6c 73 20 75 73 20 74 68 61 74 20 66 64  tells us that fd
df290 61 74 61 73 79 6e 63 28 29 20 77 69 6c 6c 20 61  atasync() will a
df2a0 6c 73 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e  lso write the in
df2b0 6f 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 66 69  ode if the.** fi
df2c0 6c 65 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e  le size has chan
df2d0 67 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72  ged.  The only r
df2e0 65 61 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62  eal difference b
df2f0 65 74 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63  etween fdatasync
df300 28 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28  ().** and fsync(
df310 29 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c  ), Ted tells us,
df320 20 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79   is that fdatasy
df330 6e 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c  nc() will not fl
df340 75 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65  ush the.** inode
df350 20 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72   if the mtime or
df360 20 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20   owner or other 
df370 69 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73  inode attributes
df380 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a   have changed..*
df390 2a 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20 61  * We only care a
df3a0 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73 69  bout the file si
df3b0 7a 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68 65  ze, not the othe
df3c0 72 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65  r file attribute
df3d0 73 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20  s, so.** as far 
df3e0 61 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6e  as SQLite is con
df3f0 63 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 74 61  cerned, an fdata
df400 73 79 6e 63 28 29 20 69 73 20 61 6c 77 61 79 73  sync() is always
df410 20 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f   adequate..** So
df420 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20  , we always use 
df430 66 64 61 74 61 73 79 6e 63 28 29 20 69 66 20 69  fdatasync() if i
df440 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  t is available, 
df450 72 65 67 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a  regardless of.**
df460 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
df470 65 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e  e dataOnly flag.
df480 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
df490 75 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66 64  ull_fsync(int fd
df4a0 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20  , int fullSync, 
df4b0 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20  int dataOnly){. 
df4c0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54   int rc;..  /* T
df4d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66  he following "if
df4e0 64 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20  def/elif/else/" 
df4f0 62 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 73 61  block has the sa
df500 6d 65 20 73 74 72 75 63 74 75 72 65 20 61 73 0a  me structure as.
df510 20 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c    ** the one bel
df520 6f 77 2e 20 49 74 20 69 73 20 72 65 70 6c 69 63  ow. It is replic
df530 61 74 65 64 20 68 65 72 65 20 73 6f 6c 65 6c 79  ated here solely
df540 20 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 74 65   to avoid clutte
df550 72 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 74 68  ring .  ** up th
df560 65 20 72 65 61 6c 20 63 6f 64 65 20 77 69 74 68  e real code with
df570 20 74 68 65 20 55 4e 55 53 45 44 5f 50 41 52 41   the UNUSED_PARA
df580 4d 45 54 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a  METER() macros..
df590 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
df5a0 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55  TE_NO_SYNC.  UNU
df5b0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64  SED_PARAMETER(fd
df5c0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
df5d0 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b  METER(fullSync);
df5e0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
df5f0 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23  TER(dataOnly);.#
df600 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53  elif HAVE_FULLFS
df610 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  YNC.  UNUSED_PAR
df620 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29  AMETER(dataOnly)
df630 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  ;.#else.  UNUSED
df640 5f 50 41 52 41 4d 45 54 45 52 28 66 75 6c 6c 53  _PARAMETER(fullS
df650 79 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ync);.  UNUSED_P
df660 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c  ARAMETER(dataOnl
df670 79 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  y);.#endif..  /*
df680 20 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62   Record the numb
df690 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74  er of times that
df6a0 20 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20   we do a normal 
df6b0 66 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a  fsync() and .  *
df6c0 2a 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69  * FULLSYNC.  Thi
df6d0 73 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67  s is used during
df6e0 20 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69   testing to veri
df6f0 66 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f  fy that this pro
df700 63 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73  cedure.  ** gets
df710 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65   called with the
df720 20 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e   correct argumen
df730 74 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  ts..  */.#ifdef 
df740 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66  SQLITE_TEST.  if
df750 28 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c  ( fullSync ) sql
df760 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
df770 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  unt++;.  sqlite3
df780 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  _sync_count++;.#
df790 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77  endif..  /* If w
df7a0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  e compiled with 
df7b0 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  the SQLITE_NO_SY
df7c0 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79  NC flag, then sy
df7d0 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20  ncing is a.  ** 
df7e0 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65  no-op.  */.#ifde
df7f0 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  f SQLITE_NO_SYNC
df800 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  .  rc = SQLITE_O
df810 4b 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55  K;.#elif HAVE_FU
df820 4c 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75  LLFSYNC.  if( fu
df830 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63  llSync ){.    rc
df840 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46   = fcntl(fd, F_F
df850 55 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20  ULLFSYNC, 0);.  
df860 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
df870 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74  1;.  }.  /* If t
df880 68 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69  he FULLFSYNC fai
df890 6c 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74  led, fall back t
df8a0 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20  o attempting an 
df8b0 66 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74  fsync()..  ** It
df8c0 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f   shouldn't be po
df8d0 73 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66  ssible for fullf
df8e0 73 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20  sync to fail on 
df8f0 74 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20  the local .  ** 
df900 66 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20  file system (on 
df910 4f 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65  OSX), so failure
df920 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
df930 46 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69  FULLFSYNC.  ** i
df940 73 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66  sn't supported f
df950 6f 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73  or this file sys
df960 74 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74  tem. So, attempt
df970 20 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20   an fsync .  ** 
df980 61 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67  and (for now) ig
df990 6e 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61  nore the overhea
df9a0 64 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f  d of a superfluo
df9b0 75 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20  us fcntl call.  
df9c0 0a 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65  .  ** It'd be be
df9d0 74 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66  tter to detect f
df9e0 75 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74  ullfsync support
df9f0 20 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20   once and avoid 
dfa00 0a 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20  .  ** the fcntl 
dfa10 63 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20  call every time 
dfa20 73 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a  sync is called..
dfa30 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20    */.  if( rc ) 
dfa40 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a  rc = fsync(fd);.
dfa50 0a 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66  .#else .  rc = f
dfa60 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69  datasync(fd);.#i
dfa70 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69  f OS_VXWORKS.  i
dfa80 66 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72  f( rc==-1 && err
dfa90 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20  no==ENOTSUP ){. 
dfaa0 20 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64     rc = fsync(fd
dfab0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
dfac0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23   OS_VXWORKS */.#
dfad0 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53  endif /* ifdef S
dfae0 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c  QLITE_NO_SYNC el
dfaf0 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e  if HAVE_FULLFSYN
dfb00 43 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56  C */..  if( OS_V
dfb10 58 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d  XWORKS && rc!= -
dfb20 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b  1 ){.    rc = 0;
dfb30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
dfb40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
dfb50 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20  sure all writes 
dfb60 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  to a particular 
dfb70 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74  file are committ
dfb80 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a  ed to disk..**.*
dfb90 2a 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30  * If dataOnly==0
dfba0 20 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66   then both the f
dfbb0 69 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69  ile itself and i
dfbc0 74 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c  ts metadata (fil
dfbd0 65 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73  e.** size, acces
dfbe0 73 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65  s time, etc) are
dfbf0 20 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74   synced.  If dat
dfc00 61 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e  aOnly!=0 then on
dfc10 6c 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64  ly the.** file d
dfc20 61 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a  ata is synced..*
dfc30 2a 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c  *.** Under Unix,
dfc40 20 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20   also make sure 
dfc50 74 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f  that the directo
dfc60 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ry entry for the
dfc70 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65   file.** has bee
dfc80 6e 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79  n created by fsy
dfc90 6e 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63  nc-ing the direc
dfca0 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69  tory that contai
dfcb0 6e 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20  ns the file..** 
dfcc0 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20  If we do not do 
dfcd0 74 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f  this and we enco
dfce0 75 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61  unter a power fa
dfcf0 69 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63  ilure, the direc
dfd00 74 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f  tory.** entry fo
dfd10 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69  r the journal mi
dfd20 67 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66  ght not exist af
dfd30 74 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20  ter we reboot.  
dfd40 54 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69  The next.** SQLi
dfd50 74 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  te to access the
dfd60 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b   file will not k
dfd70 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75  now that the jou
dfd80 72 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63  rnal exists (bec
dfd90 61 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65  ause.** the dire
dfda0 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20  ctory entry for 
dfdb0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  the journal was 
dfdc0 6e 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61  never created) a
dfdd0 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  nd the transacti
dfde0 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72  on.** will not r
dfdf0 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69  oll back - possi
dfe00 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64  bly leading to d
dfe10 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
dfe20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
dfe30 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74  t unixSync(sqlit
dfe40 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
dfe50 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72   flags){.  int r
dfe60 63 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  c;.  unixFile *p
dfe70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
dfe80 2a 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44  *)id;..  int isD
dfe90 61 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73  ataOnly = (flags
dfea0 26 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54  &SQLITE_SYNC_DAT
dfeb0 41 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73  AONLY);.  int is
dfec0 46 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67  Fullsync = (flag
dfed0 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f  s&0x0F)==SQLITE_
dfee0 53 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a  SYNC_FULL;..  /*
dfef0 20 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20   Check that one 
dff00 6f 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  of SQLITE_SYNC_N
dff10 4f 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61  ORMAL or FULL wa
dff20 73 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73  s passed */.  as
dff30 73 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46  sert((flags&0x0F
dff40 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  )==SQLITE_SYNC_N
dff50 4f 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28  ORMAL.      || (
dff60 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c  flags&0x0F)==SQL
dff70 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20  ITE_SYNC_FULL.  
dff80 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61  );..  /* Unix ca
dff90 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73  nnot, but some s
dffa0 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72  ystems may retur
dffb0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72  n SQLITE_FULL fr
dffc0 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20  om here. This.  
dffd0 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65  ** line is to te
dffe0 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f  st that doing so
dfff0 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20   does not cause 
e0000 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20  any problems..  
e0010 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73  */.  SimulateDis
e0020 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75  kfullError( retu
e0030 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29  rn SQLITE_FULL )
e0040 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  ;..  assert( pFi
e0050 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32  le );.  OSTRACE2
e0060 28 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e  ("SYNC    %-3d\n
e0070 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  ", pFile->h);.  
e0080 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28  rc = full_fsync(
e0090 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c  pFile->h, isFull
e00a0 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79  sync, isDataOnly
e00b0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
e00c0 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20  rror( rc=1 );.  
e00d0 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46  if( rc ){.    pF
e00e0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
e00f0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75   errno;.    retu
e0100 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
e0110 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28  FSYNC;.  }.  if(
e0120 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30   pFile->dirfd>=0
e0130 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3b   ){.    int err;
e0140 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 44  .    OSTRACE4("D
e0150 49 52 53 59 4e 43 20 25 2d 33 64 20 28 68 61 76  IRSYNC %-3d (hav
e0160 65 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66  e_fullfsync=%d f
e0170 75 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20  ullsync=%d)\n", 
e0180 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20  pFile->dirfd,.  
e0190 20 20 20 20 20 20 20 20 20 20 48 41 56 45 5f 46            HAVE_F
e01a0 55 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c  ULLFSYNC, isFull
e01b0 73 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 20 53  sync);.#ifndef S
e01c0 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49  QLITE_DISABLE_DI
e01d0 52 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65  RSYNC.    /* The
e01e0 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20   directory sync 
e01f0 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65  is only attempte
e0200 64 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20  d if full_fsync 
e0210 69 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e 65 64  is.    ** turned
e0220 20 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 6c 61   off or unavaila
e0230 62 6c 65 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f  ble.  If a full_
e0240 66 73 79 6e 63 20 6f 63 63 75 72 72 65 64 20 61  fsync occurred a
e0250 62 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65  bove,.    ** the
e0260 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  n the directory 
e0270 73 79 6e 63 20 69 73 20 73 75 70 65 72 66 6c 75  sync is superflu
e0280 6f 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ous..    */.    
e0290 69 66 28 20 28 21 48 41 56 45 5f 46 55 4c 4c 46  if( (!HAVE_FULLF
e02a0 53 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73  SYNC || !isFulls
e02b0 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79  ync) && full_fsy
e02c0 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c  nc(pFile->dirfd,
e02d0 30 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f  0,0) ){.       /
e02e0 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68  *.       ** We h
e02f0 61 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c  ave received mul
e0300 74 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66  tiple reports of
e0310 20 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69   fsync() returni
e0320 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72  ng.       ** err
e0330 6f 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64  ors when applied
e0340 20 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20   to directories 
e0350 6f 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20  on certain file 
e0360 73 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20  systems..       
e0370 2a 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65  ** A failed dire
e0380 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f  ctory sync is no
e0390 74 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53  t a big deal.  S
e03a0 6f 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20  o it seems.     
e03b0 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69    ** better to i
e03c0 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e  gnore the error.
e03d0 20 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20    Ticket #1657. 
e03e0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
e03f0 2f 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72  /* pFile->lastEr
e0400 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a  rno = errno; */.
e0410 20 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e         /* return
e0420 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a   SQLITE_IOERR; *
e0430 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  /.    }.#endif. 
e0440 20 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70     err = close(p
e0450 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a  File->dirfd); /*
e0460 20 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79   Only need to sy
e0470 6e 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73  nc once, so clos
e0480 65 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 66 28  e the */.    if(
e0490 20 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 20 20   err==0 ){      
e04a0 20 20 20 20 20 20 20 20 2f 2a 20 64 69 72 65 63          /* direc
e04b0 74 6f 72 79 20 77 68 65 6e 20 77 65 20 61 72 65  tory when we are
e04c0 20 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70   done */.      p
e04d0 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31  File->dirfd = -1
e04e0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
e04f0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
e0500 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
e0510 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
e0520 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a  OERR_DIR_CLOSE;.
e0530 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
e0540 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
e0550 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e  Truncate an open
e0560 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69   file to a speci
e0570 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61  fied size.*/.sta
e0580 74 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e  tic int unixTrun
e0590 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  cate(sqlite3_fil
e05a0 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65  e *id, i64 nByte
e05b0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  a
e05c0 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53  ssert( id );.  S
e05d0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
e05e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
e05f0 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a  ERR_TRUNCATE );.
e0600 20 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65    rc = ftruncate
e0610 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29  (((unixFile*)id)
e0620 2d 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74  ->h, (off_t)nByt
e0630 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  e);.  if( rc ){.
e0640 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
e0650 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  id)->lastErrno =
e0660 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75   errno;.    retu
e0670 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
e0680 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73  TRUNCATE;.  }els
e0690 65 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  e{.#ifndef NDEBU
e06a0 47 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61  G.    /* If we a
e06b0 72 65 20 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61  re doing a norma
e06c0 6c 20 77 72 69 74 65 20 74 6f 20 61 20 64 61 74  l write to a dat
e06d0 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 6f  abase file (as o
e06e0 70 70 6f 73 65 64 20 74 6f 0a 20 20 20 20 2a 2a  pposed to.    **
e06f0 20 64 6f 69 6e 67 20 61 20 68 6f 74 2d 6a 6f 75   doing a hot-jou
e0700 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f 72  rnal rollback or
e0710 20 61 20 77 72 69 74 65 20 74 6f 20 73 6f 6d 65   a write to some
e0720 20 66 69 6c 65 20 6f 74 68 65 72 20 74 68 61 6e   file other than
e0730 20 61 0a 20 20 20 20 2a 2a 20 6e 6f 72 6d 61 6c   a.    ** normal
e0740 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20   database file) 
e0750 61 6e 64 20 77 65 20 74 72 75 6e 63 61 74 65 20  and we truncate 
e0760 74 68 65 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f  the file to zero
e0770 20 6c 65 6e 67 74 68 2c 0a 20 20 20 20 2a 2a 20   length,.    ** 
e0780 74 68 61 74 20 65 66 66 65 63 74 69 76 65 6c 79  that effectively
e0790 20 75 70 64 61 74 65 73 20 74 68 65 20 63 68 61   updates the cha
e07a0 6e 67 65 20 63 6f 75 6e 74 65 72 2e 20 20 54 68  nge counter.  Th
e07b0 69 73 20 6d 69 67 68 74 20 68 61 70 70 65 6e 0a  is might happen.
e07c0 20 20 20 20 2a 2a 20 77 68 65 6e 20 72 65 73 74      ** when rest
e07d0 6f 72 69 6e 67 20 61 20 64 61 74 61 62 61 73 65  oring a database
e07e0 20 75 73 69 6e 67 20 74 68 65 20 62 61 63 6b 75   using the backu
e07f0 70 20 41 50 49 20 66 72 6f 6d 20 61 20 7a 65 72  p API from a zer
e0800 6f 2d 6c 65 6e 67 74 68 0a 20 20 20 20 2a 2a 20  o-length.    ** 
e0810 73 6f 75 72 63 65 2e 0a 20 20 20 20 2a 2f 0a 20  source..    */. 
e0820 20 20 20 69 66 28 20 28 28 75 6e 69 78 46 69 6c     if( ((unixFil
e0830 65 2a 29 69 64 29 2d 3e 69 6e 4e 6f 72 6d 61 6c  e*)id)->inNormal
e0840 57 72 69 74 65 20 26 26 20 6e 42 79 74 65 3d 3d  Write && nByte==
e0850 30 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69  0 ){.      ((uni
e0860 78 46 69 6c 65 2a 29 69 64 29 2d 3e 74 72 61 6e  xFile*)id)->tran
e0870 73 43 6e 74 72 43 68 6e 67 20 3d 20 31 3b 0a 20  sCntrChng = 1;. 
e0880 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
e0890 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
e08a0 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  K;.  }.}../*.** 
e08b0 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75  Determine the cu
e08c0 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20  rrent size of a 
e08d0 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f  file in bytes.*/
e08e0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e08f0 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33  FileSize(sqlite3
e0900 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a  _file *id, i64 *
e0910 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63  pSize){.  int rc
e0920 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20  ;.  struct stat 
e0930 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 69  buf;.  assert( i
e0940 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74 61  d );.  rc = fsta
e0950 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  t(((unixFile*)id
e0960 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 53  )->h, &buf);.  S
e0970 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
e0980 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 63  rc=1 );.  if( rc
e0990 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69  !=0 ){.    ((uni
e09a0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
e09b0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e09c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e09d0 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20  _IOERR_FSTAT;.  
e09e0 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75 66  }.  *pSize = buf
e09f0 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20  .st_size;..  /* 
e0a00 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 7a  When opening a z
e0a10 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61 73  ero-size databas
e0a20 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b 49  e, the findLockI
e0a30 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65 0a  nfo() procedure.
e0a40 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73 69    ** writes a si
e0a50 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20 74  ngle byte into t
e0a60 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64 65  hat file in orde
e0a70 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64  r to work around
e0a80 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20 74   a bug.  ** in t
e0a90 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 69  he OS-X msdos fi
e0aa0 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f 72  lesystem.  In or
e0ab0 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72 6f  der to avoid pro
e0ac0 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65 72  blems with upper
e0ad0 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77 65  .  ** layers, we
e0ae0 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74 20   need to report 
e0af0 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20 61  this file size a
e0b00 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f 75  s zero even thou
e0b10 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72 65  gh it is.  ** re
e0b20 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65 74  ally 1.   Ticket
e0b30 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 69   #3260..  */.  i
e0b40 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20 2a  f( *pSize==1 ) *
e0b50 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 72  pSize = 0;...  r
e0b60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e0b70 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  .}..#if SQLITE_E
e0b80 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e0b90 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  YLE && defined(_
e0ba0 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20  _APPLE__)./*.** 
e0bb0 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f 78  Handler for prox
e0bc0 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d 63  y-locking file-c
e0bd0 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20 44  ontrol verbs.  D
e0be0 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e 20  efined below in 
e0bf0 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 20  the.** proxying 
e0c00 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f 6e  locking division
e0c10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e0c20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c  proxyFileControl
e0c30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69  (sqlite3_file*,i
e0c40 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69  nt,void*);.#endi
e0c50 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d  f.../*.** Inform
e0c60 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f  ation and contro
e0c70 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c  l of an open fil
e0c80 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61  e handle..*/.sta
e0c90 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65  tic int unixFile
e0ca0 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f  Control(sqlite3_
e0cb0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70  file *id, int op
e0cc0 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20  , void *pArg){. 
e0cd0 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20   switch( op ){. 
e0ce0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
e0cf0 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20  CNTL_LOCKSTATE: 
e0d00 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70  {.      *(int*)p
e0d10 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65  Arg = ((unixFile
e0d20 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b  *)id)->locktype;
e0d30 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
e0d40 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
e0d50 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c     case SQLITE_L
e0d60 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20  AST_ERRNO: {.   
e0d70 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d     *(int*)pArg =
e0d80 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
e0d90 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20  ->lastErrno;.   
e0da0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e0db0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  _OK;.    }.#ifnd
e0dc0 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a  ef NDEBUG.    /*
e0dd0 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c 73   The pager calls
e0de0 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f 20   this method to 
e0df0 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 68  signal that it h
e0e00 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 61  as done.    ** a
e0e10 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68   rollback and th
e0e20 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  at the database 
e0e30 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e 63  is therefore unc
e0e40 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20 2a  hanged and.    *
e0e50 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69 73  * it hence it is
e0e60 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61 6e   OK for the tran
e0e70 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20 63  saction change c
e0e80 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20 20  ounter to be.   
e0e90 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20   ** unchanged.. 
e0ea0 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
e0eb0 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55  QLITE_FCNTL_DB_U
e0ec0 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20 20  NCHANGED: {.    
e0ed0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64    ((unixFile*)id
e0ee0 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b  )->dbUpdate = 0;
e0ef0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
e0f00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23  LITE_OK;.    }.#
e0f10 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
e0f20 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e0f30 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64  STYLE && defined
e0f40 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20  (__APPLE__).    
e0f50 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 5f  case SQLITE_SET_
e0f60 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a 20  LOCKPROXYFILE:. 
e0f70 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 47     case SQLITE_G
e0f80 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45  ET_LOCKPROXYFILE
e0f90 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  : {.      return
e0fa0 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f   proxyFileContro
e0fb0 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a 20  l(id,op,pArg);. 
e0fc0 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
e0fd0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e0fe0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
e0ff0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e1000 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   */.  }.  return
e1010 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d   SQLITE_ERROR;.}
e1020 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
e1030 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69  he sector size i
e1040 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75  n bytes of the u
e1050 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20  nderlying block 
e1060 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68  device for.** th
e1070 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65  e specified file
e1080 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74  . This is almost
e1090 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65   always 512 byte
e10a0 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a  s, but may be.**
e10b0 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65   larger for some
e10c0 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20   devices..**.** 
e10d0 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75  SQLite code assu
e10e0 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  mes this functio
e10f0 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49  n cannot fail. I
e1100 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74  t also assumes t
e1110 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69  hat.** if two fi
e1120 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20  les are created 
e1130 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  in the same file
e1140 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72  -system director
e1150 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74  y (i.e..** a dat
e1160 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f  abase and its jo
e1170 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74  urnal file) that
e1180 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
e1190 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20   will be the.** 
e11a0 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a  same for both..*
e11b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e11c0 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69  xSectorSize(sqli
e11d0 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65  te3_file *NotUse
e11e0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
e11f0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e1200 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
e1210 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
e1220 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  SIZE;.}../*.** R
e1230 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 65  eturn the device
e1240 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73   characteristics
e1250 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 54   for the file. T
e1260 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 20  his is always 0 
e1270 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 61  for unix..*/.sta
e1280 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76 69  tic int unixDevi
e1290 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
e12a0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
e12b0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53  NotUsed){.  UNUS
e12c0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e12d0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
e12e0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65  0;.}../*.** Here
e12f0 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65 6d   ends the implem
e1300 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20  entation of all 
e1310 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74  sqlite3_file met
e1320 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  hods..**.*******
e1330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
e1340 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  End sqlite3_file
e1350 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a   Methods *******
e1360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1370 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
e1380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e13a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e13b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e13c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
e13d0 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f  This division co
e13e0 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f  ntains definitio
e13f0 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f  ns of sqlite3_io
e1400 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73  _methods objects
e1410 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65   that.** impleme
e1420 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65 20  nt various file 
e1430 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 69  locking strategi
e1440 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f 6e  es.  It also con
e1450 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e  tains definition
e1460 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72 22  s.** of "finder"
e1470 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20 66   functions.  A f
e1480 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69  inder-function i
e1490 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  s used to locate
e14a0 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
e14b0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  .** sqlite3_io_m
e14c0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66 6f  ethods object fo
e14d0 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  r a particular d
e14e0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
e14f0 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20 66  he pAppData.** f
e1500 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69  ield of the sqli
e1510 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a 65  te3_vfs VFS obje
e1520 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69  cts are initiali
e1530 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74 65  zed to be pointe
e1540 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f 72  rs to.** the cor
e1550 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e 63  rect finder-func
e1560 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56 46  tion for that VF
e1570 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 69  S..**.** Most fi
e1580 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72  nder functions r
e1590 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
e15a0 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74  to a fixed sqlit
e15b0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a  e3_io_methods.**
e15c0 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f 6e   object.  The on
e15d0 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20 66  ly interesting f
e15e0 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69  inder-function i
e15f0 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64  s autolockIoFind
e1600 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f 6f  er, which.** loo
e1610 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 79  ks at the filesy
e1620 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74 72  stem type and tr
e1630 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68 65  ies to guess the
e1640 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a 2a   best locking.**
e1650 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20 74   strategy from t
e1660 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66  hat..**.** For f
e1670 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46 2c  inder-funtion F,
e1680 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72 65   two objects are
e1690 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20   created:.**.** 
e16a0 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c 20     (1) The real 
e16b0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
e16c0 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22 2e  named "FImpt()".
e16d0 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41 20  .**.**    (2) A 
e16e0 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65 72  constant pointer
e16f0 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
e1700 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 22  n named just "F"
e1710 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69  ..**.**.** A poi
e1720 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 6f  nter to the F po
e1730 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73  inter is used as
e1740 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 61   the pAppData va
e1750 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 6f  lue for VFS.** o
e1760 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 65  bjects.  We have
e1770 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 74   to do this inst
e1780 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 70  ead of letting p
e1790 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a 2a  AppData point.**
e17a0 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 65   directly at the
e17b0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
e17c0 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 73   since C90 rules
e17d0 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 2a   prevent a void*
e17e0 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 74  .** from be cast
e17f0 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e   into a function
e1800 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a   pointer..**.**.
e1810 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65  ** Each instance
e1820 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 67   of this macro g
e1830 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 6a  enerates two obj
e1840 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  ects:.**.**   * 
e1850 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c 69   A constant sqli
e1860 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
e1870 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 4f  bject call METHO
e1880 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 69  D that has locki
e1890 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f  ng.**      metho
e18a0 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20  ds CLOSE, LOCK, 
e18b0 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f 43  UNLOCK, CKRESLOC
e18c0 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e  K..**.**   *  An
e18d0 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64   I/O method find
e18e0 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c  er function call
e18f0 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 72  ed FINDER that r
e1900 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
e1910 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20  .**      to the 
e1920 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 6e  METHOD object in
e1930 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 75   the previous bu
e1940 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  llet..*/.#define
e1950 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 45   IOMETHODS(FINDE
e1960 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 45  R, METHOD, CLOSE
e1970 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20  , LOCK, UNLOCK, 
e1980 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 20  CKLOCK)         
e1990 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63        \.static c
e19a0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
e19b0 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 3d  methods METHOD =
e19c0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
e19d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e19e0 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 20       \.   1,    
e19f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a00 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
e1a10 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  n */            
e1a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a30 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c 20      \.   CLOSE, 
e1a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a50 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a       /* xClose *
e1a60 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e1a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a80 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61 64     \.   unixRead
e1a90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1aa0 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 20      /* xRead */ 
e1ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ad0 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 65    \.   unixWrite
e1ae0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1af0 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 20     /* xWrite */ 
e1b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 61   \.   unixTrunca
e1b30 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
e1b40 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a    /* xTruncate *
e1b50 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e1b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b70 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 20  \.   unixSync,  
e1b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b90 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 20   /* xSync */    
e1ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e1bc0 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65  .   unixFileSize
e1bd0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1be0 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 20  /* xFileSize */ 
e1bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e1c10 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20     LOCK,        
e1c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e1c30 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20  * xLock */      
e1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1c50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e1c60 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 20    UNLOCK,       
e1c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e1c80 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 20   xUnlock */     
e1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ca0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e1cb0 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20   CKLOCK,        
e1cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e1cd0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e1ce0 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  ck */           
e1cf0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e1d00 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c  unixFileControl,
e1d10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e1d20 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20  FileControl */  
e1d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d40 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75            \.   u
e1d50 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 20  nixSectorSize,  
e1d60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
e1d70 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 20  ectorSize */    
e1d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d90 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e           \.   un
e1da0 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65  ixDeviceCharacte
e1db0 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 65  ristics   /* xDe
e1dc0 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73  viceCapabilities
e1dd0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e1de0 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 20          \.};    
e1df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e30 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20         \.static 
e1e40 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e1e50 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 52  _methods *FINDER
e1e60 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61  ##Impl(const cha
e1e70 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 20 2a  r *z, unixFile *
e1e80 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 53 45 44  p){   \.  UNUSED
e1e90 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 55  _PARAMETER(z); U
e1ea0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e1eb0 70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p);             
e1ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ed0 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e 20       \.  return 
e1ee0 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 20  &METHOD;        
e1ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 20      \.}         
e1f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f70 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73     \.static cons
e1f80 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e1f90 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 49  hods *(*const FI
e1fa0 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 72  NDER)(const char
e1fb0 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 29 20 20  *,unixFile *p)  
e1fc0 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 52    \.    = FINDER
e1fd0 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 48  ##Impl;../*.** H
e1fe0 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 74  ere are all of t
e1ff0 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
e2000 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f  thods objects fo
e2010 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a 2a  r each of the.**
e2020 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
e2030 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 20  ies.  Functions 
e2040 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 6e  that return poin
e2050 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d 65  ters to these me
e2060 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c 73  thods.** are als
e2070 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 4f  o created..*/.IO
e2080 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 78  METHODS(.  posix
e2090 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
e20a0 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
e20b0 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
e20c0 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73    posixIoMethods
e20d0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  ,           /* s
e20e0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e20f0 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  s object name */
e2100 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20  .  unixClose,   
e2110 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2120 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f  xClose method */
e2130 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20  .  unixLock,    
e2140 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2150 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
e2160 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 20    unixUnlock,   
e2170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e2180 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
e2190 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65  .  unixCheckRese
e21a0 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a 20  rvedLock     /* 
e21b0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e21c0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49  ck method */.).I
e21d0 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f  OMETHODS(.  nolo
e21e0 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20  ckIoFinder,     
e21f0 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
e2200 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
e2210 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f  .  nolockIoMetho
e2220 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ds,          /* 
e2230 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e2240 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
e2250 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c  /.  nolockClose,
e2260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e2270 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
e2280 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20  /.  nolockLock, 
e2290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e22a0 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
e22b0 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c  .  nolockUnlock,
e22c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e22d0 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
e22e0 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52  /.  nolockCheckR
e22f0 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f 2a  eservedLock   /*
e2300 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e2310 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
e2320 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f 74  IOMETHODS(.  dot
e2330 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20  lockIoFinder,   
e2340 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72         /* Finder
e2350 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
e2360 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74  /.  dotlockIoMet
e2370 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f 2a  hods,         /*
e2380 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e2390 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ods object name 
e23a0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73  */.  dotlockClos
e23b0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e,             /
e23c0 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
e23d0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b  */.  dotlockLock
e23e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e23f0 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  * xLock method *
e2400 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63  /.  dotlockUnloc
e2410 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  k,            /*
e2420 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20   xUnlock method 
e2430 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 63  */.  dotlockChec
e2440 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 2f  kReservedLock  /
e2450 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
e2460 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29  Lock method */.)
e2470 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
e2480 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e2490 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53  E && !OS_VXWORKS
e24a0 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66 6c  .IOMETHODS(.  fl
e24b0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  ockIoFinder,    
e24c0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
e24d0 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
e24e0 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 68  */.  flockIoMeth
e24f0 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ods,           /
e2500 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
e2510 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
e2520 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65   */.  flockClose
e2530 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e2540 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
e2550 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c   */.  flockLock,
e2560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2570 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
e2580 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b  */.  flockUnlock
e2590 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e25a0 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
e25b0 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63 6b   */.  flockCheck
e25c0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20  ReservedLock    
e25d0 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e25e0 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
e25f0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f 53  ).#endif..#if OS
e2600 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f  _VXWORKS.IOMETHO
e2610 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64 65  DS(.  semIoFinde
e2620 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
e2630 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
e2640 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d  on name */.  sem
e2650 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20  IoMethods,      
e2660 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
e2670 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
e2680 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65  ect name */.  se
e2690 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20  mClose,         
e26a0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
e26b0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65  e method */.  se
e26c0 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  mLock,          
e26d0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
e26e0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d   method */.  sem
e26f0 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
e2700 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63         /* xUnloc
e2710 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65  k method */.  se
e2720 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  mCheckReservedLo
e2730 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63  ck      /* xChec
e2740 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65  kReservedLock me
e2750 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66  thod */.).#endif
e2760 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  ..#if defined(__
e2770 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49  APPLE__) && SQLI
e2780 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
e2790 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44  G_STYLE.IOMETHOD
e27a0 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 72  S(.  afpIoFinder
e27b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e27c0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f  * Finder functio
e27d0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 49  n name */.  afpI
e27e0 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
e27f0 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
e2800 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
e2810 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70  ct name */.  afp
e2820 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
e2830 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
e2840 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70   method */.  afp
e2850 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
e2860 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
e2870 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55  method */.  afpU
e2880 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  nlock,          
e2890 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
e28a0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70   method */.  afp
e28b0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
e28c0 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b  k      /* xCheck
e28d0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74  ReservedLock met
e28e0 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a  hod */.).#endif.
e28f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 68 6f 6c  ./*.** The "Whol
e2900 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 22 20  e File Locking" 
e2910 66 69 6e 64 65 72 20 72 65 74 75 72 6e 73 20 74  finder returns t
e2920 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20 6d  he same set of m
e2930 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 74 68 65  ethods as.** the
e2940 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e 67 20 66   posix locking f
e2950 69 6e 64 65 72 2e 20 20 42 75 74 20 69 74 20 61  inder.  But it a
e2960 6c 73 6f 20 73 65 74 73 20 74 68 65 20 53 51 4c  lso sets the SQL
e2970 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c  ITE_WHOLE_FILE_L
e2980 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 67 20 74  OCKING.** flag t
e2990 6f 20 66 6f 72 63 65 20 74 68 65 20 70 6f 73 69  o force the posi
e29a0 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  x advisory locks
e29b0 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 77 68   to cover the wh
e29c0 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 65 61 64  ole file instead
e29d0 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 20 73 6d  .** of just a sm
e29e0 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 79 74 65  all span of byte
e29f0 73 20 6e 65 61 72 20 74 68 65 20 31 47 69 42 20  s near the 1GiB 
e2a00 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 6f 6c 65  boundary.  Whole
e2a10 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 0a 2a 2a   File Locking.**
e2a20 20 69 73 20 75 73 65 66 75 6c 20 6f 6e 20 4e 46   is useful on NF
e2a30 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c 65 73 20  S-mounted files 
e2a40 73 69 6e 63 65 20 69 74 20 68 65 6c 70 73 20 4e  since it helps N
e2a50 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 63  FS to maintain c
e2a60 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 65 6e 63  ache.** coherenc
e2a70 79 2e 20 20 42 75 74 20 69 74 20 69 73 20 61 20  y.  But it is a 
e2a80 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 6f 74 68  detriment to oth
e2a90 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 20 73  er filesystems s
e2aa0 69 6e 63 65 20 69 74 20 72 75 6e 73 0a 2a 2a 20  ince it runs.** 
e2ab0 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  slower..*/.stati
e2ac0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
e2ad0 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 6f 73 69  io_methods *posi
e2ae0 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70 6c  xWflIoFinderImpl
e2af0 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a 2c 20 75  (const char*z, u
e2b00 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 20 55 4e  nixFile*p){.  UN
e2b10 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a  USED_PARAMETER(z
e2b20 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 6c 61 67  );.  p->fileFlag
e2b30 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45  s = SQLITE_WHOLE
e2b40 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20  _FILE_LOCKING;. 
e2b50 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f   return &posixIo
e2b60 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 61 74 69  Methods;.}.stati
e2b70 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
e2b80 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28  io_methods .  *(
e2b90 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 66 6c 49  *const posixWflI
e2ba0 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63  oFinder)(const c
e2bb0 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70  har*,unixFile *p
e2bc0 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 6f 46 69  ) = posixWflIoFi
e2bd0 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a  nderImpl;../*.**
e2be0 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69   The proxy locki
e2bf0 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 61 20 22  ng method is a "
e2c00 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 20 69 6e  super-method" in
e2c10 20 74 68 65 20 73 65 6e 73 65 20 74 68 61 74 20   the sense that 
e2c20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63 6f  it.** opens seco
e2c30 6e 64 61 72 79 20 66 69 6c 65 20 64 65 73 63 72  ndary file descr
e2c40 69 70 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63  iptors for the c
e2c50 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66 69  onch and lock fi
e2c60 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 20 75 73  les and.** it us
e2c70 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66 69  es proxy, dot-fi
e2c80 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c 6f  le, AFP, and flo
e2c90 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65 74  ck() locking met
e2ca0 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0a 2a 2a  hods on those.**
e2cb0 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 73   secondary files
e2cc0 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 73  .  For this reas
e2cd0 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f 6e  on, the division
e2ce0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
e2cf0 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  .** proxy lockin
e2d00 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75 63  g is located muc
e2d10 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 69  h further down i
e2d20 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75 74  n the file.  But
e2d30 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 67   we need.** to g
e2d40 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65 66 69  o ahead and defi
e2d50 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ne the sqlite3_i
e2d60 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20 66 69  o_methods and fi
e2d70 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  nder function.**
e2d80 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69   for proxy locki
e2d90 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 77 65 20  ng here.  So we 
e2da0 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 65 20  forward declare 
e2db0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 2e  the I/O methods.
e2dc0 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
e2dd0 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51  __APPLE__) && SQ
e2de0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
e2df0 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69 63  ING_STYLE.static
e2e00 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28   int proxyClose(
e2e10 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a  sqlite3_file*);.
e2e20 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
e2e30 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
e2e40 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63  e*, int);.static
e2e50 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b   int proxyUnlock
e2e60 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
e2e70 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69 6e 74  int);.static int
e2e80 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 72   proxyCheckReser
e2e90 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
e2ea0 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b 0a 49 4f  file*, int*);.IO
e2eb0 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 6f 78 79  METHODS(.  proxy
e2ec0 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
e2ed0 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
e2ee0 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
e2ef0 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73    proxyIoMethods
e2f00 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  ,           /* s
e2f10 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e2f20 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  s object name */
e2f30 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20 20  .  proxyClose,  
e2f40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2f50 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f  xClose method */
e2f60 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20 20  .  proxyLock,   
e2f70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2f80 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
e2f90 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20 20    proxyUnlock,  
e2fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e2fb0 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
e2fc0 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73  .  proxyCheckRes
e2fd0 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20  ervedLock    /* 
e2fe0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e2ff0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23  ck method */.).#
e3000 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 65 66 69  endif...#if defi
e3010 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26  ned(__APPLE__) &
e3020 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
e3030 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a  LOCKING_STYLE./*
e3040 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64 65   .** This "finde
e3050 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65  r" function atte
e3060 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69 6e  mpts to determin
e3070 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69  e the best locki
e3080 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a 20  ng strategy .** 
e3090 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
e30a0 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68 22   file "filePath"
e30b0 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75 72  .  It then retur
e30c0 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ns the sqlite3_i
e30d0 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a  o_methods.** obj
e30e0 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ect that impleme
e30f0 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65 67  nts that strateg
e3100 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  y..**.** This is
e3110 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c 79   for MacOSX only
e3120 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
e3130 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e3140 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 6f  hods *autolockIo
e3150 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 6f  FinderImpl(.  co
e3160 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50 61  nst char *filePa
e3170 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 6f  th,    /* name o
e3180 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
e3190 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c  ile */.  unixFil
e31a0 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20  e *pNew         
e31b0 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20 6f    /* open file o
e31c0 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64 61  bject for the da
e31d0 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 29  tabase file */.)
e31e0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
e31f0 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 20   struct Mapping 
e3200 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
e3210 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 20   *zFilesystem;  
e3220 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
e3230 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 6e  ilesystem type n
e3240 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  ame */.    const
e3250 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e3260 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20  ods *pMethods;  
e3270 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65 20   /* Appropriate 
e3280 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 2a  locking method *
e3290 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 7b  /.  } aMap[] = {
e32a0 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20 20  .    { "hfs",   
e32b0 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73   &posixIoMethods
e32c0 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22 2c   },.    { "ufs",
e32d0 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68      &posixIoMeth
e32e0 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61 66  ods },.    { "af
e32f0 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74  pfs",  &afpIoMet
e3300 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 66 20 53  hods },.#ifdef S
e3310 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 50  QLITE_ENABLE_AFP
e3320 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 20  _LOCKING_SMB.   
e3330 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 61 66   { "smbfs",  &af
e3340 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65  pIoMethods },.#e
e3350 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d 62 66 73  lse.    { "smbfs
e3360 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d 65 74 68  ",  &flockIoMeth
e3370 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20  ods },.#endif.  
e3380 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 26 6e    { "webdav", &n
e3390 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d  olockIoMethods }
e33a0 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20  ,.    { 0, 0 }. 
e33b0 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73   };.  int i;.  s
e33c0 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49  truct statfs fsI
e33d0 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 66 6c  nfo;.  struct fl
e33e0 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20  ock lockInfo;.. 
e33f0 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29   if( !filePath )
e3400 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65  {.    /* If file
e3410 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20  Path==NULL that 
e3420 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61  means we are dea
e3430 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e  ling with a tran
e3440 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a  sient file.    *
e3450 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  * that does not 
e3460 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65  need to be locke
e3470 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  d. */.    return
e3480 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64   &nolockIoMethod
e3490 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 61  s;.  }.  if( sta
e34a0 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 26 66  tfs(filePath, &f
e34b0 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b 0a  sInfo) != -1 ){.
e34c0 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f 2e 66      if( fsInfo.f
e34d0 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44 4f  _flags & MNT_RDO
e34e0 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 65 74  NLY ){.      ret
e34f0 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74  urn &nolockIoMet
e3500 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20  hods;.    }.    
e3510 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69 5d  for(i=0; aMap[i]
e3520 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 69 2b  .zFilesystem; i+
e3530 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74  +){.      if( st
e3540 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73  rcmp(fsInfo.f_fs
e3550 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b 69  typename, aMap[i
e3560 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d 3d  ].zFilesystem)==
e3570 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  0 ){.        ret
e3580 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 74  urn aMap[i].pMet
e3590 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20  hods;.      }.  
e35a0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65    }.  }..  /* De
e35b0 66 61 75 6c 74 20 63 61 73 65 2e 20 48 61 6e 64  fault case. Hand
e35c0 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 68  les, amongst oth
e35d0 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a 2a  ers, "nfs"..  **
e35e0 20 54 65 73 74 20 62 79 74 65 2d 72 61 6e 67 65   Test byte-range
e35f0 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e 74   lock using fcnt
e3600 6c 28 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c  l(). If the call
e3610 20 73 75 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a   succeeds, .  **
e3620 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65   assume that the
e3630 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70   file-system sup
e3640 70 6f 72 74 73 20 50 4f 53 49 58 20 73 74 79 6c  ports POSIX styl
e3650 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20  e locks. .  */. 
e3660 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20   lockInfo.l_len 
e3670 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e  = 1;.  lockInfo.
e3680 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c  l_start = 0;.  l
e3690 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65  ockInfo.l_whence
e36a0 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c   = SEEK_SET;.  l
e36b0 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d  ockInfo.l_type =
e36c0 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20   F_RDLCK;.  if( 
e36d0 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46  fcntl(pNew->h, F
e36e0 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66  _GETLK, &lockInf
e36f0 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 70  o)!=-1 ) {.    p
e3700 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d  New->fileFlags =
e3710 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49   SQLITE_WHOLE_FI
e3720 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 20 20 20  LE_LOCKING;.    
e3730 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f 4d  return &posixIoM
e3740 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b  ethods;.  }else{
e3750 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f 74  .    return &dot
e3760 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  lockIoMethods;. 
e3770 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73   }.}.static cons
e3780 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e3790 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74  hods .  *(*const
e37a0 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65   autolockIoFinde
e37b0 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75  r)(const char*,u
e37c0 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f  nixFile*) = auto
e37d0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c  lockIoFinderImpl
e37e0 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  ;..#endif /* def
e37f0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
e3800 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
e3810 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a  _LOCKING_STYLE *
e3820 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  /..#if OS_VXWORK
e3830 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42  S && SQLITE_ENAB
e3840 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e3850 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69  ./* .** This "fi
e3860 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61  nder" function a
e3870 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72  ttempts to deter
e3880 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f  mine the best lo
e3890 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a  cking strategy .
e38a0 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  ** for the datab
e38b0 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61  ase file "filePa
e38c0 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65  th".  It then re
e38d0 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65  turns the sqlite
e38e0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20  3_io_methods.** 
e38f0 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c  object that impl
e3900 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61  ements that stra
e3910 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tegy..**.** This
e3920 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73 20   is for VXWorks 
e3930 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  only..*/.static 
e3940 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e3950 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f  _methods *autolo
e3960 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a  ckIoFinderImpl(.
e3970 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69    const char *fi
e3980 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61  lePath,    /* na
e3990 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
e39a0 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69  se file */.  uni
e39b0 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20  xFile *pNew     
e39c0 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70 65        /* the ope
e39d0 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a 2f  n file object */
e39e0 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f  .){.  struct flo
e39f0 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20  ck lockInfo;..  
e3a00 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b  if( !filePath ){
e3a10 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50  .    /* If fileP
e3a20 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d  ath==NULL that m
e3a30 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c  eans we are deal
e3a40 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e 73  ing with a trans
e3a50 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a  ient file.    **
e3a60 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e   that does not n
e3a70 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  eed to be locked
e3a80 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
e3a90 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73  &nolockIoMethods
e3aa0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74  ;.  }..  /* Test
e3ab0 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20 73   if fcntl() is s
e3ac0 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73 65  upported and use
e3ad0 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63   POSIX style loc
e3ae0 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69  ks..  ** Otherwi
e3af0 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20  se fall back to 
e3b00 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68  the named semaph
e3b10 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2f  ore method..  */
e3b20 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65  .  lockInfo.l_le
e3b30 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66  n = 1;.  lockInf
e3b40 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20  o.l_start = 0;. 
e3b50 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e   lockInfo.l_when
e3b60 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
e3b70 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65   lockInfo.l_type
e3b80 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66   = F_RDLCK;.  if
e3b90 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c  ( fcntl(pNew->h,
e3ba0 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49   F_GETLK, &lockI
e3bb0 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20  nfo)!=-1 ) {.   
e3bc0 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f   return &posixIo
e3bd0 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65  Methods;.  }else
e3be0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73 65  {.    return &se
e3bf0 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a  mIoMethods;.  }.
e3c00 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  }.static const s
e3c10 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e3c20 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61 75  s .  *(*const au
e3c30 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28  tolockIoFinder)(
e3c40 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78  const char*,unix
e3c50 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f 63  File*) = autoloc
e3c60 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a  kIoFinderImpl;..
e3c70 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57  #endif /* OS_VXW
e3c80 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45  ORKS && SQLITE_E
e3c90 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e3ca0 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e  YLE */../*.** An
e3cb0 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66   abstract type f
e3cc0 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  or a pointer to 
e3cd0 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64  a IO method find
e3ce0 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f 0a  er function:.*/.
e3cf0 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 71  typedef const sq
e3d00 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e3d10 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 29   *(*finder_type)
e3d20 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69  (const char*,uni
e3d30 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a 2a  xFile*);.../****
e3d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3d80 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
e3d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3da0 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66  ***** sqlite3_vf
e3db0 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a  s methods ******
e3dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3dd0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
e3de0 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61  s division conta
e3df0 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ins the implemen
e3e00 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64  tation of method
e3e10 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69  s on the.** sqli
e3e20 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a  te3_vfs object..
e3e30 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  */../*.** Initia
e3e40 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  lize the content
e3e50 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c  s of the unixFil
e3e60 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e  e structure poin
e3e70 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a 2a  ted to by pId..*
e3e80 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c  /.static int fil
e3e90 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 73  lInUnixFile(.  s
e3ea0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
e3eb0 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  ,      /* Pointe
e3ec0 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20  r to vfs object 
e3ed0 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20  */.  int h,     
e3ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e3ef0 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69  Open file descri
e3f00 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 69  ptor of file bei
e3f10 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69  ng opened */.  i
e3f20 6e 74 20 64 69 72 66 64 2c 20 20 20 20 20 20 20  nt dirfd,       
e3f30 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74         /* Direct
e3f40 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70  ory file descrip
e3f50 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tor */.  sqlite3
e3f60 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 20  _file *pId,     
e3f70 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 65   /* Write to the
e3f80 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74   unixFile struct
e3f90 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f  ure here */.  co
e3fa0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
e3fb0 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ame,  /* Name of
e3fc0 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20   the file being 
e3fd0 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20  opened */.  int 
e3fe0 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  noLock,         
e3ff0 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63 6b      /* Omit lock
e4000 69 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a 20  ing if true */. 
e4010 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20   int isDelete   
e4020 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65           /* Dele
e4030 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74  te on close if t
e4040 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  rue */.){.  cons
e4050 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e4060 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53 74  hods *pLockingSt
e4070 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  yle;.  unixFile 
e4080 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 6c  *pNew = (unixFil
e4090 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 72  e *)pId;.  int r
e40a0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
e40b0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e    assert( pNew->
e40c0 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20  pLock==NULL );. 
e40d0 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70   assert( pNew->p
e40e0 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20  Open==NULL );.. 
e40f0 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69 73   /* Parameter is
e4100 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20 75  Delete is only u
e4110 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20  sed on vxworks. 
e4120 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78 70  Express this exp
e4130 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 68 65  licitly .  ** he
e4140 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 63 6f  re to prevent co
e4150 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20  mpiler warnings 
e4160 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61 72  about unused par
e4170 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20  ameters..  */.  
e4180 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e4190 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a 20 20 4f  (isDelete);..  O
e41a0 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20 20  STRACE3("OPEN   
e41b0 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c 20   %-3d %s\n", h, 
e41c0 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20 0a  zFilename);    .
e41d0 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a 20    pNew->h = h;. 
e41e0 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 64   pNew->dirfd = d
e41f0 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52 45  irfd;.  SET_THRE
e4200 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 20 70 4e  ADID(pNew);.  pN
e4210 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d 20  ew->fileFlags = 
e4220 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  0;..#if OS_VXWOR
e4230 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20 3d  KS.  pNew->pId =
e4240 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65   vxworksFindFile
e4250 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  Id(zFilename);. 
e4260 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d 3d   if( pNew->pId==
e4270 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b 20  0 ){.    noLock 
e4280 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  = 1;.    rc = SQ
e4290 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
e42a0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f  #endif..  if( no
e42b0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63  Lock ){.    pLoc
e42c0 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f 6c  kingStyle = &nol
e42d0 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20  ockIoMethods;.  
e42e0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b  }else{.    pLock
e42f0 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28 66  ingStyle = (**(f
e4300 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66 73  inder_type*)pVfs
e4310 2d 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69 6c  ->pAppData)(zFil
e4320 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b 0a 23 69  ename, pNew);.#i
e4330 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
e4340 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20  LOCKING_STYLE.  
e4350 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c 65    /* Cache zFile
e4360 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63 6b  name in the lock
e4370 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46 50  ing context (AFP
e4380 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76 65   and dotlock ove
e4390 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20 2a  rride) for.    *
e43a0 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74 69  * proxyLock acti
e43b0 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69 62  vation is possib
e43c0 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78 79  le (remote proxy
e43d0 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62 20   is based on db 
e43e0 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46 69  name).    ** zFi
e43f0 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20 76  lename remains v
e4400 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65 20  alid until file 
e4410 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73 75  is closed, to su
e4420 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e 65  pport */.    pNe
e4430 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  w->lockingContex
e4440 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c 65  t = (void*)zFile
e4450 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  name;.#endif.  }
e4460 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67  ..  if( pLocking
e4470 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78 49  Style == &posixI
e4480 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
e4490 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
e44a0 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
e44b0 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70  ockInfo(pNew, &p
e44c0 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65  New->pLock, &pNe
e44d0 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69  w->pOpen);.    i
e44e0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e44f0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
e4500 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64  an error occured
e4510 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f   in findLockInfo
e4520 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 69  (), close the fi
e4530 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20 20  le descriptor.  
e4540 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65      ** immediate
e4550 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65 61  ly, before relea
e4560 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e 20  sing the mutex. 
e4570 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 6d  findLockInfo() m
e4580 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a 2a  ay fail.      **
e4590 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69 6f   in two scenario
e45a0 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  s:.      **.    
e45b0 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61 6c    **   (a) A cal
e45c0 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61 69  l to fstat() fai
e45d0 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20 20  led..      **   
e45e0 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61 69  (b) A malloc fai
e45f0 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  led..      **.  
e4600 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f 20      ** Scenario 
e4610 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63 63  (b) may only occ
e4620 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65 73  ur if the proces
e4630 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f 20  s is holding no 
e4640 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 66  other.      ** f
e4650 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20  ile descriptors 
e4660 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  open on the same
e4670 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20   file. If there 
e4680 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65 0a  were other file.
e4690 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70        ** descrip
e46a0 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69 6c  tors on this fil
e46b0 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c 6f  e, then no mallo
e46c0 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75 69  c would be requi
e46d0 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20  red by.      ** 
e46e0 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 20  findLockInfo(). 
e46f0 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
e4700 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 74 65  ase, it is quite
e4710 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 0a 20   safe to close. 
e4720 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 68       ** handle h
e4730 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 61 72   - as it is guar
e4740 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f 20 70  anteed that no p
e4750 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c 6c 20  osix locks will 
e4760 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20  be released.    
e4770 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 73 6f    ** by doing so
e4780 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
e4790 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f 20   ** If scenario 
e47a0 28 61 29 20 63 61 75 73 65 64 20 74 68 65 20 65  (a) caused the e
e47b0 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67 73  rror then things
e47c0 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66 65   are not so safe
e47d0 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 69  . The.      ** i
e47e0 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74 69  mplicit assumpti
e47f0 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 20  on here is that 
e4800 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c 73  if fstat() fails
e4810 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e 0a  , things are in.
e4820 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62 61        ** such ba
e4830 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72 6f  d shape that dro
e4840 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72 20  pping a lock or 
e4850 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74 74  two doesn't matt
e4860 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 20 2a  er much..      *
e4870 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 68 29  /.      close(h)
e4880 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b 0a  ;.      h = -1;.
e4890 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65      }.    unixLe
e48a0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
e48b0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
e48c0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e48d0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   && defined(__AP
e48e0 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 66  PLE__).  else if
e48f0 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  ( pLockingStyle 
e4900 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73  == &afpIoMethods
e4910 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 6c   ){.    /* AFP l
e4920 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20  ocking uses the 
e4930 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20  file path so it 
e4940 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c  needs to be incl
e4950 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74  uded in.    ** t
e4960 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  he afpLockingCon
e4970 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  text..    */.   
e4980 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
e4990 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 4e  xt *pCtx;.    pN
e49a0 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
e49b0 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c 69  xt = pCtx = sqli
e49c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
e49d0 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 20  of(*pCtx) );.   
e49e0 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a   if( pCtx==0 ){.
e49f0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e4a00 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c  E_NOMEM;.    }el
e4a10 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a  se{.      /* NB:
e4a20 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74   zFilename exist
e4a30 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61  s and remains va
e4a40 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69  lid until the fi
e4a50 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20  le is closed.   
e4a60 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20     ** according 
e4a70 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46  to requirement F
e4a80 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f  11141.  So we do
e4a90 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b   not need to mak
e4aa0 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70  e a.      ** cop
e4ab0 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d  y of the filenam
e4ac0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78  e. */.      pCtx
e4ad0 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65  ->dbPath = zFile
e4ae0 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 6e  name;.      sran
e4af0 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 20  domdev();.      
e4b00 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
e4b10 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e  ;.      rc = fin
e4b20 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20  dLockInfo(pNew, 
e4b30 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f 70  NULL, &pNew->pOp
e4b40 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 4c  en);.      unixL
e4b50 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20  eaveMutex();    
e4b60 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23      .    }.  }.#
e4b70 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 66  endif..  else if
e4b80 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  ( pLockingStyle 
e4b90 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74  == &dotlockIoMet
e4ba0 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44  hods ){.    /* D
e4bb0 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75  otfile locking u
e4bc0 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74  ses the file pat
e4bd0 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  h so it needs to
e4be0 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a   be included in.
e4bf0 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f      ** the dotlo
e4c00 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ckLockingContext
e4c10 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61   .    */.    cha
e4c20 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20  r *zLockFile;.  
e4c30 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b    int nFilename;
e4c40 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 3d  .    nFilename =
e4c50 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 69   (int)strlen(zFi
e4c60 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 20  lename) + 6;.   
e4c70 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68   zLockFile = (ch
e4c80 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar *)sqlite3_mal
e4c90 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a  loc(nFilename);.
e4ca0 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 6c      if( zLockFil
e4cb0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  e==0 ){.      rc
e4cc0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
e4cd0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
e4ce0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
e4cf0 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a 4c  tf(nFilename, zL
e4d00 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 4f  ockFile, "%s" DO
e4d10 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a 46  TLOCK_SUFFIX, zF
e4d20 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ilename);.    }.
e4d30 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e      pNew->lockin
e4d40 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 6b  gContext = zLock
e4d50 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 4f  File;.  }..#if O
e4d60 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 65  S_VXWORKS.  else
e4d70 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79   if( pLockingSty
e4d80 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 68  le == &semIoMeth
e4d90 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 61  ods ){.    /* Na
e4da0 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f  med semaphore lo
e4db0 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66  cking uses the f
e4dc0 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e  ile path so it n
e4dd0 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a  eeds to be.    *
e4de0 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  * included in th
e4df0 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e 74  e semLockingCont
e4e00 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75  ext.    */.    u
e4e10 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
e4e20 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f  .    rc = findLo
e4e30 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e  ckInfo(pNew, &pN
e4e40 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77  ew->pLock, &pNew
e4e50 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 66  ->pOpen);.    if
e4e60 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  ( (rc==SQLITE_OK
e4e70 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 65  ) && (pNew->pOpe
e4e80 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 29  n->pSem==NULL) )
e4e90 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53  {.      char *zS
e4ea0 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e 70  emName = pNew->p
e4eb0 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b 0a  Open->aSemName;.
e4ec0 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20        int n;.   
e4ed0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
e4ee0 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ntf(MAX_PATHNAME
e4ef0 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25 73  , zSemName, "/%s
e4f00 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20  .sem",.         
e4f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
e4f20 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69  ew->pId->zCanoni
e4f30 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  calName);.      
e4f40 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e 61  for( n=1; zSemNa
e4f50 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20  me[n]; n++ ).   
e4f60 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d       if( zSemNam
e4f70 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d  e[n]=='/' ) zSem
e4f80 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20  Name[n] = '_';. 
e4f90 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e       pNew->pOpen
e4fa0 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65  ->pSem = sem_ope
e4fb0 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52  n(zSemName, O_CR
e4fc0 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20  EAT, 0666, 1);. 
e4fd0 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70       if( pNew->p
e4fe0 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 45  Open->pSem == SE
e4ff0 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 20  M_FAILED ){.    
e5000 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e5010 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70  NOMEM;.        p
e5020 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d  New->pOpen->aSem
e5030 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a  Name[0] = '\0';.
e5040 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
e5050 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
e5060 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ();.  }.#endif. 
e5070 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 72   .  pNew->lastEr
e5080 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 5f  rno = 0;.#if OS_
e5090 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63  VXWORKS.  if( rc
e50a0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
e50b0 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e     unlink(zFilen
e50c0 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65  ame);.    isDele
e50d0 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e  te = 0;.  }.  pN
e50e0 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 69  ew->isDelete = i
e50f0 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 0a  sDelete;.#endif.
e5100 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
e5110 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 64  _OK ){.    if( d
e5120 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28  irfd>=0 ) close(
e5130 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e  dirfd); /* silen
e5140 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20  t leak if fail, 
e5150 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72  already in error
e5160 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 3e 3d 30   */.    if( h>=0
e5170 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 7d   ) close(h);.  }
e5180 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  else{.    pNew->
e5190 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b 69  pMethod = pLocki
e51a0 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70 65  ngStyle;.    Ope
e51b0 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20  nCounter(+1);.  
e51c0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
e51d0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66  ../*.** Open a f
e51e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
e51f0 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  o the directory 
e5200 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20  containing file 
e5210 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66  zFilename..** If
e5220 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46   successful, *pF
e5230 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  d is set to the 
e5240 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63  opened file desc
e5250 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51  riptor and.** SQ
e5260 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
e5270 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ned. If an error
e5280 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20   occurs, either 
e5290 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20  SQLITE_NOMEM.** 
e52a0 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  or SQLITE_CANTOP
e52b0 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  EN is returned a
e52c0 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 74  nd *pFd is set t
e52d0 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a  o an undefined.*
e52e0 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49  * value..**.** I
e52f0 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  f SQLITE_OK is r
e5300 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61 6c  eturned, the cal
e5310 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ler is responsib
e5320 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a 2a  le for closing.*
e5330 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  * the file descr
e5340 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e 67  iptor *pFd using
e5350 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 61   close()..*/.sta
e5360 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 65  tic int openDire
e5370 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 72  ctory(const char
e5380 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e 74   *zFilename, int
e5390 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 69   *pFd){.  int ii
e53a0 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b  ;.  int fd = -1;
e53b0 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d 65  .  char zDirname
e53c0 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d  [MAX_PATHNAME+1]
e53d0 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  ;..  sqlite3_snp
e53e0 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41  rintf(MAX_PATHNA
e53f0 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 25  ME, zDirname, "%
e5400 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  s", zFilename);.
e5410 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 74    for(ii=(int)st
e5420 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20  rlen(zDirname); 
e5430 69 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d 65  ii>1 && zDirname
e5440 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29  [ii]!='/'; ii--)
e5450 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a  ;.  if( ii>0 ){.
e5460 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d      zDirname[ii]
e5470 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20   = '\0';.    fd 
e5480 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c  = open(zDirname,
e5490 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41   O_RDONLY|O_BINA
e54a0 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  RY, 0);.    if( 
e54b0 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20  fd>=0 ){.#ifdef 
e54c0 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20  FD_CLOEXEC.     
e54d0 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54   fcntl(fd, F_SET
e54e0 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f  FD, fcntl(fd, F_
e54f0 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43  GETFD, 0) | FD_C
e5500 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a  LOEXEC);.#endif.
e5510 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22        OSTRACE3("
e5520 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c  OPENDIR %-3d %s\
e5530 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65  n", fd, zDirname
e5540 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a  );.    }.  }.  *
e5550 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75  pFd = fd;.  retu
e5560 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45  rn (fd>=0?SQLITE
e5570 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f  _OK:SQLITE_CANTO
e5580 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  PEN);.}../*.** C
e5590 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72  reate a temporar
e55a0 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a  y file name in z
e55b0 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20  Buf.  zBuf must 
e55c0 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20  be allocated.** 
e55d0 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70  by the calling p
e55e0 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20  rocess and must 
e55f0 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  be big enough to
e5600 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a   hold at least.*
e5610 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  * pVfs->mxPathna
e5620 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  me bytes..*/.sta
e5630 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e  tic int getTempn
e5640 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68  ame(int nBuf, ch
e5650 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61  ar *zBuf){.  sta
e5660 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
e5670 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20  azDirs[] = {.   
e5680 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20 20    0,.     0,.   
e5690 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20 20    "/var/tmp",.  
e56a0 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a 20     "/usr/tmp",. 
e56b0 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20 20      "/tmp",.    
e56c0 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74 61   ".",.  };.  sta
e56d0 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
e56e0 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d  ed char zChars[]
e56f0 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68   =.    "abcdefgh
e5700 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78  ijklmnopqrstuvwx
e5710 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47  yz".    "ABCDEFG
e5720 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57  HIJKLMNOPQRSTUVW
e5730 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35  XYZ".    "012345
e5740 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 65  6789";.  unsigne
e5750 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74  d int i, j;.  st
e5760 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20  ruct stat buf;. 
e5770 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69   const char *zDi
e5780 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49  r = ".";..  /* I
e5790 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c  t's odd to simul
e57a0 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20  ate an io-error 
e57b0 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79  here, but really
e57c0 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20   this is just.  
e57d0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d  ** using the io-
e57e0 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63  error infrastruc
e57f0 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 61  ture to test tha
e5800 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73  t SQLite handles
e5810 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74   this.  ** funct
e5820 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20  ion failing. .  
e5830 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  */.  SimulateIOE
e5840 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
e5850 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20  ITE_IOERR );..  
e5860 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69  azDirs[0] = sqli
e5870 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
e5880 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20 3d  ry;.  if (NULL =
e5890 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a 20  = azDirs[1]) {. 
e58a0 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20 67     azDirs[1] = g
e58b0 65 74 65 6e 76 28 22 54 4d 50 44 49 52 22 29 3b  etenv("TMPDIR");
e58c0 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69 3d  .  }.  .  for(i=
e58d0 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 69  0; i<sizeof(azDi
e58e0 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 72  rs)/sizeof(azDir
e58f0 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  s[0]); i++){.   
e5900 20 69 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d 3d   if( azDirs[i]==
e5910 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
e5920 20 20 69 66 28 20 73 74 61 74 28 61 7a 44 69 72    if( stat(azDir
e5930 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 20 63 6f  s[i], &buf) ) co
e5940 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
e5950 21 53 5f 49 53 44 49 52 28 62 75 66 2e 73 74 5f  !S_ISDIR(buf.st_
e5960 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 65  mode) ) continue
e5970 3b 0a 20 20 20 20 69 66 28 20 61 63 63 65 73 73  ;.    if( access
e5980 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 20  (azDirs[i], 07) 
e5990 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
e59a0 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 5d  zDir = azDirs[i]
e59b0 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  ;.    break;.  }
e59c0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
e59d0 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  t the output buf
e59e0 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e 6f  fer is large eno
e59f0 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d 70  ugh for the temp
e5a00 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a 2a  orary file .  **
e5a10 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73 20   name. If it is 
e5a20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  not, return SQLI
e5a30 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a 20  TE_ERROR..  */. 
e5a40 20 69 66 28 20 28 73 74 72 6c 65 6e 28 7a 44 69   if( (strlen(zDi
e5a50 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c 49  r) + strlen(SQLI
e5a60 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
e5a70 46 49 58 29 20 2b 20 31 37 29 20 3e 3d 20 28 73  FIX) + 17) >= (s
e5a80 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20 20  ize_t)nBuf ){.  
e5a90 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e5aa0 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f  ERROR;.  }..  do
e5ab0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
e5ac0 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c 20  printf(nBuf-17, 
e5ad0 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 54  zBuf, "%s/"SQLIT
e5ae0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
e5af0 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 6a  IX, zDir);.    j
e5b00 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a   = (int)strlen(z
e5b10 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Buf);.    sqlite
e5b20 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 2c  3_randomness(15,
e5b30 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20 20   &zBuf[j]);.    
e5b40 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20 69  for(i=0; i<15; i
e5b50 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ++, j++){.      
e5b60 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29  zBuf[j] = (char)
e5b70 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e  zChars[ ((unsign
e5b80 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29  ed char)zBuf[j])
e5b90 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29  %(sizeof(zChars)
e5ba0 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  -1) ];.    }.   
e5bb0 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20   zBuf[j] = 0;.  
e5bc0 7d 77 68 69 6c 65 28 20 61 63 63 65 73 73 28 7a  }while( access(z
e5bd0 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 72  Buf,0)==0 );.  r
e5be0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e5bf0 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  .}..#if SQLITE_E
e5c00 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e5c10 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  YLE && defined(_
e5c20 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20  _APPLE__)./*.** 
e5c30 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e 73  Routine to trans
e5c40 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 20  form a unixFile 
e5c50 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f 63  into a proxy-loc
e5c60 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a 2a  king unixFile..*
e5c70 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
e5c80 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c 6f   in the proxy-lo
e5c90 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 74  ck division, but
e5ca0 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70 65   used by unixOpe
e5cb0 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45  n().** if SQLITE
e5cc0 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f  _PREFER_PROXY_LO
e5cd0 43 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65 64  CKING is defined
e5ce0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e5cf0 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e  proxyTransformUn
e5d00 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 2a  ixFile(unixFile*
e5d10 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
e5d20 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65  #endif../*.** Se
e5d30 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73  arch for an unus
e5d40 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
e5d50 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e  or that was open
e5d60 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ed on the databa
e5d70 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f 74  se .** file (not
e5d80 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d 61   a journal or ma
e5d90 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster-journal fil
e5da0 65 29 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  e) identified by
e5db0 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50 61   pathname.** zPa
e5dc0 74 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f  th with SQLITE_O
e5dd0 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d 61  PEN_XXX flags ma
e5de0 74 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61 73  tching those pas
e5df0 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
e5e00 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  d.** argument to
e5e10 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
e5e20 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 6c  **.** Such a fil
e5e30 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79  e descriptor may
e5e40 20 65 78 69 73 74 20 69 66 20 61 20 64 61 74 61   exist if a data
e5e50 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
e5e60 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 75  was closed.** bu
e5e70 74 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  t the associated
e5e80 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
e5e90 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c   could not be cl
e5ea0 6f 73 65 64 20 62 65 63 61 75 73 65 20 73 6f 6d  osed because som
e5eb0 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 20  e.** other file 
e5ec0 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20  descriptor open 
e5ed0 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  on the same file
e5ee0 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 69   is holding a fi
e5ef0 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66 65  le-lock..** Refe
e5f00 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69 6e  r to comments in
e5f10 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28 29   the unixClose()
e5f20 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74 68   function and th
e5f30 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65 6e  e lengthy commen
e5f40 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20  t.** describing 
e5f50 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20  "Posix Advisory 
e5f60 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65 20  Locking" at the 
e5f70 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 66 69  start of this fi
e5f80 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74 68  le for .** furth
e5f90 65 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73 6f  er details. Also
e5fa0 2c 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e 0a  , ticket #4018..
e5fb0 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74 61  **.** If a suita
e5fc0 62 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  ble file descrip
e5fd0 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68  tor is found, th
e5fe0 65 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e 65  en it is returne
e5ff0 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63 68  d. If no.** such
e6000 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
e6010 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31 20   is located, -1 
e6020 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
e6030 73 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73 65  static UnixUnuse
e6040 64 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62 6c  dFd *findReusabl
e6050 65 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  eFd(const char *
e6060 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 73  zPath, int flags
e6070 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46  ){.  UnixUnusedF
e6080 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a  d *pUnused = 0;.
e6090 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65 61  .  /* Do not sea
e60a0 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 65  rch for an unuse
e60b0 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
e60c0 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e 6f  r on vxworks. No
e60d0 74 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 76  t because.  ** v
e60e0 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f 74  xworks would not
e60f0 20 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74 68   benefit from th
e6100 65 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69 67  e change (it mig
e6110 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73 75  ht, we're not su
e6120 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62 65  re),.  ** but be
e6130 63 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f 20  cause no way to 
e6140 74 65 73 74 20 69 74 20 69 73 20 63 75 72 72 65  test it is curre
e6150 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e 20  ntly available. 
e6160 49 74 20 69 73 20 62 65 74 74 65 72 20 0a 20 20  It is better .  
e6170 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 62  ** not to risk b
e6180 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 20  reaking vxworks 
e6190 73 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65 20  support for the 
e61a0 73 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e 20  sake of such an 
e61b0 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66 65  obscure .  ** fe
e61c0 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20 21  ature.  */.#if !
e61d0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72  OS_VXWORKS.  str
e61e0 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b 20  uct stat sStat; 
e61f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6200 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20    /* Results of 
e6210 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a 0a  stat() call */..
e6220 20 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63 61    /* A stat() ca
e6230 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72 20  ll may fail for 
e6240 76 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73 2e  various reasons.
e6250 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73   If this happens
e6260 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c 6d  , it is.  ** alm
e6270 6f 73 74 20 63 65 72 74 61 69 6e 20 74 68 61 74  ost certain that
e6280 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c 20   an open() call 
e6290 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 68  on the same path
e62a0 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c 2e   will also fail.
e62b0 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72  .  ** For this r
e62c0 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72 72  eason, if an err
e62d0 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65  or occurs in the
e62e0 20 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65 72   stat() call her
e62f0 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69 67  e, it is.  ** ig
e6300 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73 20  nored and -1 is 
e6310 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 61  returned. The ca
e6320 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74 6f  ller will try to
e6330 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c 65   open a new file
e6340 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72  .  ** descriptor
e6350 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74   on the same pat
e6360 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65 74  h, fail, and ret
e6370 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f 20  urn an error to 
e6380 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a  SQLite..  **.  *
e6390 2a 20 45 76 65 6e 20 69 66 20 61 20 73 75 62 73  * Even if a subs
e63a0 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63 61  equent open() ca
e63b0 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64 2c  ll does succeed,
e63c0 20 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 65   the consequence
e63d0 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73 65  s of.  ** not se
e63e0 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72 65  arching for a re
e63f0 73 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65 73  susable file des
e6400 63 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74 20  criptor are not 
e6410 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20  dire.  */.  if( 
e6420 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 2c 20 26  0==stat(zPath, &
e6430 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 73 74  sStat) ){.    st
e6440 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74  ruct unixOpenCnt
e6450 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 75 63 74   *pO;.    struct
e6460 20 75 6e 69 78 46 69 6c 65 49 64 20 69 64 3b 0a   unixFileId id;.
e6470 20 20 20 20 69 64 2e 64 65 76 20 3d 20 73 53 74      id.dev = sSt
e6480 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 20 20 69  at.st_dev;.    i
e6490 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 2e 73 74  d.ino = sStat.st
e64a0 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e 69 78 45  _ino;..    unixE
e64b0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
e64c0 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c 69 73 74   for(pO=openList
e64d0 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d 70 28 26  ; pO && memcmp(&
e64e0 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 49 64 2c  id, &pO->fileId,
e64f0 20 73 69 7a 65 6f 66 28 69 64 29 29 3b 20 70 4f   sizeof(id)); pO
e6500 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 20  =pO->pNext);.   
e6510 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 20 20 20   if( pO ){.     
e6520 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 2a   UnixUnusedFd **
e6530 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 70  pp;.      for(pp
e6540 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 3b 20 2a  =&pO->pUnused; *
e6550 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c 61  pp && (*pp)->fla
e6560 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26 28  gs!=flags; pp=&(
e6570 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b 0a  (*pp)->pNext));.
e6580 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d 20        pUnused = 
e6590 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70  *pp;.      if( p
e65a0 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20 20  Unused ){.      
e65b0 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 64 2d    *pp = pUnused-
e65c0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  >pNext;.      }.
e65d0 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65      }.    unixLe
e65e0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
e65f0 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 66 20  #endif    /* if 
e6600 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 20  !OS_VXWORKS */. 
e6610 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 64 3b   return pUnused;
e6620 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74  .}../*.** Open t
e6630 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a 2a  he file zPath..*
e6640 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c 79  * .** Previously
e6650 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53 20  , the SQLite OS 
e6660 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65 65  layer used three
e6670 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 6c   functions in pl
e6680 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f  ace of this.** o
e6690 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71  ne:.**.**     sq
e66a0 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 57  lite3OsOpenReadW
e66b0 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 73  rite();.**     s
e66c0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64  qlite3OsOpenRead
e66d0 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 73  Only();.**     s
e66e0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c  qlite3OsOpenExcl
e66f0 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 54  usive();.**.** T
e6700 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 65  hese calls corre
e6710 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c  spond to the fol
e6720 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69  lowing combinati
e6730 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a  ons of flags:.**
e6740 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69 74  .**     ReadWrit
e6750 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41 44  e() ->     (READ
e6760 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29 0a  WRITE | CREATE).
e6770 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 28  **     ReadOnly(
e6780 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44 4f  )  ->     (READO
e6790 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 65  NLY) .**     Ope
e67a0 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e 20  nExclusive() -> 
e67b0 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 45  (READWRITE | CRE
e67c0 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45 29  ATE | EXCLUSIVE)
e67d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 4f  .**.** The old O
e67e0 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 61  penExclusive() a
e67f0 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 61  ccepted a boolea
e6800 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 65  n argument - "de
e6810 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 72  lFlag". If.** tr
e6820 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61 73  ue, the file was
e6830 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 62   configured to b
e6840 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  e automatically 
e6850 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65  deleted when the
e6860 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  .** file handle 
e6870 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 65  closed. To achie
e6880 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66 65  ve the same effe
e6890 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e 65  ct using this ne
e68a0 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c  w .** interface,
e68b0 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45 4f   add the DELETEO
e68c0 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 74  NCLOSE flag to t
e68d0 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20 61  hose specified a
e68e0 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 65  bove for .** Ope
e68f0 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a 2f  nExclusive()..*/
e6900 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e6910 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  Open(.  sqlite3_
e6920 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
e6930 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20       /* The VFS 
e6940 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 69  for which this i
e6950 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68  s the xOpen meth
e6960 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  od */.  const ch
e6970 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20  ar *zPath,      
e6980 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d 65       /* Pathname
e6990 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 6f   of file to be o
e69a0 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  pened */.  sqlit
e69b0 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20  e3_file *pFile, 
e69c0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
e69d0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
e69e0 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 2a  o be filled in *
e69f0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
e6a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6a10 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 20   /* Input flags 
e6a20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6f  to control the o
e6a30 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  pening */.  int 
e6a40 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20  *pOutFlags      
e6a50 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
e6a60 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65  ut flags returne
e6a70 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 65  d to SQLite core
e6a80 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c   */.){.  unixFil
e6a90 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c 65  e *p = (unixFile
e6aa0 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74 20   *)pFile;.  int 
e6ab0 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20  fd = -1;        
e6ac0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
e6ad0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 65  le descriptor re
e6ae0 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 29  turned by open()
e6af0 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 20   */.  int dirfd 
e6b00 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20  = -1;           
e6b10 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f 72       /* Director
e6b20 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  y file descripto
e6b30 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46  r */.  int openF
e6b40 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20  lags = 0;       
e6b50 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74        /* Flags t
e6b60 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 29  o pass to open()
e6b70 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20   */.  int eType 
e6b80 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 46  = flags&0xFFFFFF
e6b90 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 20  00;  /* Type of 
e6ba0 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  file to open */.
e6bb0 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20    int noLock;   
e6bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6bd0 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74   /* True to omit
e6be0 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69   locking primiti
e6bf0 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ves */.  int rc 
e6c00 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
e6c10 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74          /* Funct
e6c20 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 20  ion Return Code 
e6c30 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 6c  */..  int isExcl
e6c40 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 20  usive  = (flags 
e6c50 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58  & SQLITE_OPEN_EX
e6c60 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 20  CLUSIVE);.  int 
e6c70 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 28  isDelete     = (
e6c80 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
e6c90 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
e6ca0 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 61  E);.  int isCrea
e6cb0 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20  te     = (flags 
e6cc0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  & SQLITE_OPEN_CR
e6cd0 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 52  EATE);.  int isR
e6ce0 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c 61  eadonly   = (fla
e6cf0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
e6d00 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 6e  _READONLY);.  in
e6d10 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20 3d  t isReadWrite  =
e6d20 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
e6d30 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29  _OPEN_READWRITE)
e6d40 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74  ;..  /* If creat
e6d50 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20  ing a master or 
e6d60 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61  main-file journa
e6d70 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  l, this function
e6d80 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a 20   will open.  ** 
e6d90 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f  a file-descripto
e6da0 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f  r on the directo
e6db0 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73  ry too. The firs
e6dc0 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28  t time unixSync(
e6dd0 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ).  ** is called
e6de0 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 66   the directory f
e6df0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77  ile descriptor w
e6e00 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64  ill be fsync()ed
e6e10 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a 20   and close()d.. 
e6e20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e   */.  int isOpen
e6e30 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 43  Directory = (isC
e6e40 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 20  reate && .      
e6e50 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f  (eType==SQLITE_O
e6e60 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e  PEN_MASTER_JOURN
e6e70 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c  AL || eType==SQL
e6e80 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
e6e90 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 2f  URNAL).  );..  /
e6ea0 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 50  * If argument zP
e6eb0 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  ath is a NULL po
e6ec0 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63  inter, this func
e6ed0 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64  tion is required
e6ee0 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20   to open.  ** a 
e6ef0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20  temporary file. 
e6f00 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72 20  Use this buffer 
e6f10 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c  to store the fil
e6f20 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f 0a  e name in..  */.
e6f30 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b    char zTmpname[
e6f40 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b  MAX_PATHNAME+1];
e6f50 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
e6f60 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a 20  Name = zPath;.. 
e6f70 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 6f   /* Check the fo
e6f80 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e  llowing statemen
e6f90 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 20  ts are true: .  
e6fa0 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 78  **.  **   (a) Ex
e6fb0 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65  actly one of the
e6fc0 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 52   READWRITE and R
e6fd0 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d 75  EADONLY flags mu
e6fe0 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 0a  st be set, and .
e6ff0 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 52    **   (b) if CR
e7000 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 65  EATE is set, the
e7010 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 74  n READWRITE must
e7020 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 6e   also be set, an
e7030 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 20  d.  **   (c) if 
e7040 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 74  EXCLUSIVE is set
e7050 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75  , then CREATE mu
e7060 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a  st also be set..
e7070 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 45    **   (d) if DE
e7080 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 73  LETEONCLOSE is s
e7090 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20  et, then CREATE 
e70a0 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74  must also be set
e70b0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
e70c0 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c  (isReadonly==0 |
e70d0 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d 30  | isReadWrite==0
e70e0 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69 74  ) && (isReadWrit
e70f0 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 29  e || isReadonly)
e7100 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 72  );.  assert(isCr
e7110 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 61  eate==0 || isRea
e7120 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 72  dWrite);.  asser
e7130 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30  t(isExclusive==0
e7140 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 20   || isCreate);. 
e7150 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65   assert(isDelete
e7160 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29  ==0 || isCreate)
e7170 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e  ;..  /* The main
e7180 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   DB, main journa
e7190 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a 6f  l, and master jo
e71a0 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 20  urnal are never 
e71b0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 20  automatically.  
e71c0 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72 20  ** deleted. Nor 
e71d0 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74 65  are they ever te
e71e0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 20  mporary files.  
e71f0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 21 69  */.  assert( (!i
e7200 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65  sDelete && zName
e7210 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49  ) || eType!=SQLI
e7220 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
e7230 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 69  );.  assert( (!i
e7240 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65  sDelete && zName
e7250 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49  ) || eType!=SQLI
e7260 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
e7270 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72 74  RNAL );.  assert
e7280 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20  ( (!isDelete && 
e7290 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21  zName) || eType!
e72a0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  =SQLITE_OPEN_MAS
e72b0 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a 0a  TER_JOURNAL );..
e72c0 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
e72d0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72   the upper layer
e72e0 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20   has set one of 
e72f0 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20  the "file-type" 
e7300 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65  flags. */.  asse
e7310 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  rt( eType==SQLIT
e7320 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20  E_OPEN_MAIN_DB  
e7330 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51      || eType==SQ
e7340 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44  LITE_OPEN_TEMP_D
e7350 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79  B .       || eTy
e7360 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
e7370 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20  MAIN_JOURNAL || 
e7380 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
e7390 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20  EN_TEMP_JOURNAL 
e73a0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
e73b0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55  ==SQLITE_OPEN_SU
e73c0 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54  BJOURNAL   || eT
e73d0 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
e73e0 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
e73f0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
e7400 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52  ==SQLITE_OPEN_TR
e7410 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a  ANSIENT_DB.  );.
e7420 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
e7430 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29  sizeof(unixFile)
e7440 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d  );..  if( eType=
e7450 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
e7460 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e 69 78  N_DB ){.    Unix
e7470 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65  UnusedFd *pUnuse
e7480 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 20 3d  d;.    pUnused =
e7490 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64 28   findReusableFd(
e74a0 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b 0a 20  zName, flags);. 
e74b0 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 29     if( pUnused )
e74c0 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 55 6e  {.      fd = pUn
e74d0 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 7d 65  used->fd;.    }e
e74e0 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e 75 73  lse{.      pUnus
e74f0 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ed = sqlite3_mal
e7500 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e 75  loc(sizeof(*pUnu
e7510 73 65 64 29 29 3b 0a 20 20 20 20 20 20 69 66 28  sed));.      if(
e7520 20 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20   !pUnused ){.   
e7530 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
e7540 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
e7550 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70  }.    }.    p->p
e7560 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65 64  Unused = pUnused
e7570 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 7a  ;.  }else if( !z
e7580 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Name ){.    /* I
e7590 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 2c  f zName is NULL,
e75a0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72   the upper layer
e75b0 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61   is requesting a
e75c0 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a 20   temp file. */. 
e75d0 20 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65     assert(isDele
e75e0 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69 72  te && !isOpenDir
e75f0 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63 20  ectory);.    rc 
e7600 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41  = getTempname(MA
e7610 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a 54  X_PATHNAME+1, zT
e7620 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28  mpname);.    if(
e7630 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
e7640 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
e7650 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e 61  c;.    }.    zNa
e7660 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20  me = zTmpname;. 
e7670 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69   }..  /* Determi
e7680 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ne the value of 
e7690 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
e76a0 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 50 4f  ter passed to PO
e76b0 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a  SIX function.  *
e76c0 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 73 65 20  * open(). These 
e76d0 6d 75 73 74 20 62 65 20 63 61 6c 63 75 6c 61 74  must be calculat
e76e0 65 64 20 65 76 65 6e 20 69 66 20 6f 70 65 6e 28  ed even if open(
e76f0 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2c  ) is not called,
e7700 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 20 6d 61   as.  ** they ma
e7710 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 70  y be stored as p
e7720 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20  art of the file 
e7730 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 65 64 20  handle and used 
e7740 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 27 63 6f  by the .  ** 'co
e7750 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 6b 69 6e  nch file' lockin
e7760 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 74 65  g functions late
e7770 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 66 28 20  r on.  */.  if( 
e7780 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f 70  isReadonly )  op
e7790 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f  enFlags |= O_RDO
e77a0 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65 61  NLY;.  if( isRea
e77b0 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c 61  dWrite ) openFla
e77c0 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20  gs |= O_RDWR;.  
e77d0 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 20  if( isCreate )  
e77e0 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f    openFlags |= O
e77f0 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69 73  _CREAT;.  if( is
e7800 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65 6e  Exclusive ) open
e7810 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c  Flags |= (O_EXCL
e7820 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 20  |O_NOFOLLOW);.  
e7830 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f  openFlags |= (O_
e7840 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41  LARGEFILE|O_BINA
e7850 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 64 3c 30  RY);..  if( fd<0
e7860 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f 74 20 6f   ){.    mode_t o
e7870 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 44 65 6c  penMode = (isDel
e7880 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f  ete?0600:SQLITE_
e7890 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52  DEFAULT_FILE_PER
e78a0 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 66  MISSIONS);.    f
e78b0 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20  d = open(zName, 
e78c0 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d  openFlags, openM
e78d0 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  ode);.    OSTRAC
e78e0 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 64  E4("OPENX   %-3d
e78f0 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c 20   %s 0%o\n", fd, 
e7900 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73  zName, openFlags
e7910 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20  );.    if( fd<0 
e7920 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 52  && errno!=EISDIR
e7930 20 26 26 20 69 73 52 65 61 64 57 72 69 74 65 20   && isReadWrite 
e7940 26 26 20 21 69 73 45 78 63 6c 75 73 69 76 65 20  && !isExclusive 
e7950 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 61 69 6c  ){.      /* Fail
e7960 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 66  ed to open the f
e7970 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72 69  ile for read/wri
e7980 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20 72  te access. Try r
e7990 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20  ead-only. */.   
e79a0 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 51     flags &= ~(SQ
e79b0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
e79c0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ITE|SQLITE_OPEN_
e79d0 43 52 45 41 54 45 29 3b 0a 20 20 20 20 20 20 6f  CREATE);.      o
e79e0 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f 5f  penFlags &= ~(O_
e79f0 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a 20  RDWR|O_CREAT);. 
e7a00 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51       flags |= SQ
e7a10 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
e7a20 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c  LY;.      openFl
e7a30 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b  ags |= O_RDONLY;
e7a40 0a 20 20 20 20 20 20 66 64 20 3d 20 6f 70 65 6e  .      fd = open
e7a50 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67  (zName, openFlag
e7a60 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20  s, openMode);.  
e7a70 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 3c 30    }.    if( fd<0
e7a80 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
e7a90 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
e7aa0 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f        goto open_
e7ab0 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a  finished;.    }.
e7ac0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 66 64    }.  assert( fd
e7ad0 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 75  >=0 );.  if( pOu
e7ae0 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70  tFlags ){.    *p
e7af0 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73  OutFlags = flags
e7b00 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e  ;.  }..  if( p->
e7b10 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 70  pUnused ){.    p
e7b20 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 20 3d 20  ->pUnused->fd = 
e7b30 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73  fd;.    p->pUnus
e7b40 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67  ed->flags = flag
e7b50 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73  s;.  }..  if( is
e7b60 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f 53  Delete ){.#if OS
e7b70 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50 61  _VXWORKS.    zPa
e7b80 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c 73  th = zName;.#els
e7b90 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61  e.    unlink(zNa
e7ba0 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  me);.#endif.  }.
e7bb0 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
e7bc0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
e7bd0 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6f    else{.    p->o
e7be0 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46  penFlags = openF
e7bf0 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  lags;.  }.#endif
e7c00 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44 69  ..  if( isOpenDi
e7c10 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 72  rectory ){.    r
e7c20 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f 72  c = openDirector
e7c30 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64 29  y(zPath, &dirfd)
e7c40 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
e7c50 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
e7c60 20 2f 2a 20 49 74 20 69 73 20 73 61 66 65 20 74   /* It is safe t
e7c70 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 20 74 68  o close fd at th
e7c80 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 61 75 73  is point, becaus
e7c90 65 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  e it is guarante
e7ca0 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20  ed not.      ** 
e7cb0 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e 20 61 20  to be open on a 
e7cc0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
e7cd0 66 20 69 74 20 77 65 72 65 20 6f 70 65 6e 20 6f  f it were open o
e7ce0 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  n a database fil
e7cf0 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 77  e,.      ** it w
e7d00 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 66 65  ould not be safe
e7d10 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 74 68 69   to close as thi
e7d20 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 73 65 20  s would release 
e7d30 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 0a 20  any locks held. 
e7d40 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 66       ** on the f
e7d50 69 6c 65 20 62 79 20 74 68 69 73 20 70 72 6f 63  ile by this proc
e7d60 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61  ess.  */.      a
e7d70 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 51  ssert( eType!=SQ
e7d80 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
e7d90 42 20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65  B );.      close
e7da0 28 66 64 29 3b 20 20 20 20 20 20 20 20 20 20 20  (fd);           
e7db0 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65    /* silently le
e7dc0 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c 72 65  ak if fail, alre
e7dd0 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a  ady in error */.
e7de0 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f        goto open_
e7df0 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a  finished;.    }.
e7e00 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f 43    }..#ifdef FD_C
e7e10 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 66  LOEXEC.  fcntl(f
e7e20 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74  d, F_SETFD, fcnt
e7e30 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30  l(fd, F_GETFD, 0
e7e40 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b  ) | FD_CLOEXEC);
e7e50 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63  .#endif..  noLoc
e7e60 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 54  k = eType!=SQLIT
e7e70 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a  E_OPEN_MAIN_DB;.
e7e80 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46  .#if SQLITE_PREF
e7e90 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47  ER_PROXY_LOCKING
e7ea0 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d 4e 55  .  if( zPath!=NU
e7eb0 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 26 26  LL && !noLock &&
e7ec0 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 7b 0a   pVfs->xOpen ){.
e7ed0 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 6f 72      char *envfor
e7ee0 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 51 4c  ce = getenv("SQL
e7ef0 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f  ITE_FORCE_PROXY_
e7f00 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20 69  LOCKING");.    i
e7f10 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30 3b  nt useProxy = 0;
e7f20 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ..    /* SQLITE_
e7f30 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b  FORCE_PROXY_LOCK
e7f40 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f 72  ING==1 means for
e7f50 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70 72  ce always use pr
e7f60 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a 20 20  oxy, 0 means .  
e7f70 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 20 70    ** never use p
e7f80 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e 73  roxy, NULL means
e7f90 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 20 6e   use proxy for n
e7fa0 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20 6f  on-local files o
e7fb0 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  nly.  */.    if(
e7fc0 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c 20   envforce!=NULL 
e7fd0 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78  ){.      useProx
e7fe0 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72 63  y = atoi(envforc
e7ff0 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  e)>0;.    }else{
e8000 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74  .      struct st
e8010 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 20  atfs fsInfo;.   
e8020 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a 50     if( statfs(zP
e8030 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d  ath, &fsInfo) ==
e8040 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f   -1 ){.        /
e8050 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65  * In theory, the
e8060 20 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c 20   close(fd) call 
e8070 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e 20  is sub-optimal. 
e8080 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e  If the file open
e8090 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69  ed.        ** wi
e80a0 74 68 20 66 64 20 69 73 20 61 20 64 61 74 61 62  th fd is a datab
e80b0 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74 68  ase file, and th
e80c0 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 63 6f  ere are other co
e80d0 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a 20  nnections open. 
e80e0 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 61         ** on tha
e80f0 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20  t file that are 
e8100 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e  currently holdin
e8110 67 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  g advisory locks
e8120 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20 20   on it,.        
e8130 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c  ** then the call
e8140 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c 6c   to close() will
e8150 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c 6f   cancel those lo
e8160 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63 65  cks. In practice
e8170 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 27  ,.        ** we'
e8180 72 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74  re assuming that
e8190 20 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e 27   statfs() doesn'
e81a0 74 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 65  t fail very ofte
e81b0 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20 20  n. At least.    
e81c0 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c 65      ** not while
e81d0 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63   other file desc
e81e0 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 62  riptors opened b
e81f0 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  y the same proce
e8200 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  ss on.        **
e8210 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 61   the same file a
e8220 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f 0a  re working.  */.
e8230 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74 45          p->lastE
e8240 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
e8250 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64 3e        if( dirfd>
e8260 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
e8270 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a  close(dirfd); /*
e8280 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69   silently leak i
e8290 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72  f fail, in error
e82a0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
e82b0 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b        close(fd);
e82c0 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61   /* silently lea
e82d0 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72  k if fail, in er
e82e0 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  ror */.        r
e82f0 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
e8300 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 20 20  _ACCESS;.       
e8310 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73   goto open_finis
e8320 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  hed;.      }.   
e8330 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 21 28     useProxy = !(
e8340 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 4d  fsInfo.f_flags&M
e8350 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 7d  NT_LOCAL);.    }
e8360 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 6f 78  .    if( useProx
e8370 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  y ){.      rc = 
e8380 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70  fillInUnixFile(p
e8390 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20  Vfs, fd, dirfd, 
e83a0 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f  pFile, zPath, no
e83b0 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b  Lock, isDelete);
e83c0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
e83d0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e83e0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72      rc = proxyTr
e83f0 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28  ansformUnixFile(
e8400 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c 65  (unixFile*)pFile
e8410 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 20  , ":auto:");.   
e8420 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20     }.      goto 
e8430 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20  open_finished;. 
e8440 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
e8450 20 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e    .  rc = fillIn
e8460 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66  UnixFile(pVfs, f
e8470 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c  d, dirfd, pFile,
e8480 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20   zPath, noLock, 
e8490 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 65 6e 5f  isDelete);.open_
e84a0 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 20  finished:.  if( 
e84b0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
e84c0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
e84d0 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20  e(p->pUnused);. 
e84e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
e84f0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  }.../*.** Delete
e8500 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61   the file at zPa
e8510 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 79  th. If the dirSy
e8520 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nc argument is t
e8530 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a 20  rue, fsync().** 
e8540 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 66  the directory af
e8550 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65  ter deleting the
e8560 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
e8570 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 28   int unixDelete(
e8580 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
e8590 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a 20  NotUsed,     /* 
e85a0 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  VFS containing t
e85b0 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c 65  his as the xDele
e85c0 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63  te method */.  c
e85d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
e85e0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
e85f0 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 64   of file to be d
e8600 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  eleted */.  int 
e8610 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20 20  dirSync         
e8620 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
e8630 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63 74  , fsync() direct
e8640 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69  ory after deleti
e8650 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20  ng file */.){.  
e8660 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
e8670 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  OK;.  UNUSED_PAR
e8680 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e8690 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
e86a0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
e86b0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a  _IOERR_DELETE);.
e86c0 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b    unlink(zPath);
e86d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
e86e0 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a  DISABLE_DIRSYNC.
e86f0 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b    if( dirSync ){
e8700 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20  .    int fd;.   
e8710 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74   rc = openDirect
e8720 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b  ory(zPath, &fd);
e8730 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
e8740 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f 53  ITE_OK ){.#if OS
e8750 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69  _VXWORKS.      i
e8760 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31  f( fsync(fd)==-1
e8770 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69   ).#else.      i
e8780 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23  f( fsync(fd) ).#
e8790 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20  endif.      {.  
e87a0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e87b0 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e  E_IOERR_DIR_FSYN
e87c0 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  C;.      }.     
e87d0 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26 26   if( close(fd)&&
e87e0 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72  !rc ){.        r
e87f0 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
e8800 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20  _DIR_CLOSE;.    
e8810 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65    }.    }.  }.#e
e8820 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63  ndif.  return rc
e8830 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  ;.}../*.** Test 
e8840 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66  the existance of
e8850 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69   or access permi
e8860 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a  ssions of file z
e8870 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73  Path. The.** tes
e8880 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65  t performed depe
e8890 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  nds on the value
e88a0 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a   of flags:.**.**
e88b0 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45       SQLITE_ACCE
e88c0 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72  SS_EXISTS: Retur
e88d0 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20  n 1 if the file 
e88e0 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51  exists.**     SQ
e88f0 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
e8900 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20  WRITE: Return 1 
e8910 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72  if the file is r
e8920 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65  ead and writable
e8930 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f  ..**     SQLITE_
e8940 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a  ACCESS_READONLY:
e8950 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
e8960 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c   file is readabl
e8970 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  e..**.** Otherwi
e8980 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  se return 0..*/.
e8990 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41  static int unixA
e89a0 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33  ccess(.  sqlite3
e89b0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20  _vfs *NotUsed,  
e89c0 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e 74   /* The VFS cont
e89d0 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63 63  aining this xAcc
e89e0 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ess method */.  
e89f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
e8a00 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20  h,      /* Path 
e8a10 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 65  of the file to e
e8a20 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  xamine */.  int 
e8a30 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
e8a40 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 77      /* What do w
e8a50 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e 20  e want to learn 
e8a60 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68 20  about the zPath 
e8a70 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 2a  file? */.  int *
e8a80 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20  pResOut         
e8a90 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75     /* Write resu
e8aa0 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 20  lt boolean here 
e8ab0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64  */.){.  int amod
e8ac0 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f  e = 0;.  UNUSED_
e8ad0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e8ae0 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  d);.  SimulateIO
e8af0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
e8b00 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
e8b10 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20  S; );.  switch( 
e8b20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73  flags ){.    cas
e8b30 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
e8b40 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d  EXISTS:.      am
e8b50 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20  ode = F_OK;.    
e8b60 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
e8b70 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
e8b80 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20  READWRITE:.     
e8b90 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f   amode = W_OK|R_
e8ba0 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OK;.      break;
e8bb0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
e8bc0 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20  _ACCESS_READ:.  
e8bd0 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b      amode = R_OK
e8be0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  ;.      break;..
e8bf0 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
e8c00 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61     assert(!"Inva
e8c10 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65  lid flags argume
e8c20 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65  nt");.  }.  *pRe
e8c30 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a  sOut = (access(z
e8c40 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29  Path, amode)==0)
e8c50 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
e8c60 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
e8c70 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20  Turn a relative 
e8c80 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20  pathname into a 
e8c90 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54  full pathname. T
e8ca0 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68  he relative path
e8cb0 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73  .** is stored as
e8cc0 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65   a nul-terminate
e8cd0 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20  d string in the 
e8ce0 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
e8cf0 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a  o by.** zPath. .
e8d00 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74  **.** zOut point
e8d10 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66  s to a buffer of
e8d20 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65   at least sqlite
e8d30 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65  3_vfs.mxPathname
e8d40 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74   bytes .** (in t
e8d50 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41  his case, MAX_PA
e8d60 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54  THNAME bytes). T
e8d70 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20  he full-path is 
e8d80 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68  written to.** th
e8d90 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 65  is buffer before
e8da0 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73   returning..*/.s
e8db0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75  tatic int unixFu
e8dc0 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71  llPathname(.  sq
e8dd0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
e8de0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
e8df0 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62  ointer to vfs ob
e8e00 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ject */.  const 
e8e10 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20  char *zPath,    
e8e20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69          /* Possi
e8e30 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70  bly relative inp
e8e40 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74  ut path */.  int
e8e50 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20   nOut,          
e8e60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
e8e70 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
e8e80 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
e8e90 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20    char *zOut    
e8ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8eb0 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72  /* Output buffer
e8ec0 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27   */.){..  /* It'
e8ed0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74  s odd to simulat
e8ee0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65  e an io-error he
e8ef0 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74  re, but really t
e8f00 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a  his is just.  **
e8f10 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72   using the io-er
e8f20 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75  ror infrastructu
e8f30 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  re to test that 
e8f40 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74  SQLite handles t
e8f50 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
e8f60 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20  n failing. This 
e8f70 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66  function could f
e8f80 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d  ail if, for exam
e8f90 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75  ple, the.  ** cu
e8fa0 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69  rrent working di
e8fb0 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e  rectory has been
e8fc0 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a   unlinked..  */.
e8fd0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
e8fe0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
e8ff0 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73  _ERROR );..  ass
e9000 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74  ert( pVfs->mxPat
e9010 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e  hname==MAX_PATHN
e9020 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  AME );.  UNUSED_
e9030 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
e9040 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d  ..  zOut[nOut-1]
e9050 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 7a   = '\0';.  if( z
e9060 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b 0a  Path[0]=='/' ){.
e9070 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
e9080 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 2c  intf(nOut, zOut,
e9090 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20   "%s", zPath);. 
e90a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
e90b0 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67 65  nCwd;.    if( ge
e90c0 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d  tcwd(zOut, nOut-
e90d0 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  1)==0 ){.      r
e90e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e  eturn SQLITE_CAN
e90f0 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20  TOPEN;.    }.   
e9100 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74 72   nCwd = (int)str
e9110 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73  len(zOut);.    s
e9120 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
e9130 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74  nOut-nCwd, &zOut
e9140 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a  [nCwd], "/%s", z
e9150 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Path);.  }.  ret
e9160 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
e9170 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
e9180 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
e9190 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  NSION./*.** Inte
e91a0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
e91b0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
e91c0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
e91d0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
e91e0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
e91f0 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
e9200 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
e9210 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c  ibrary..*/.#incl
e9220 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 74  ude <dlfcn.h>.st
e9230 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 44  atic void *unixD
e9240 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
e9250 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e 73  s *NotUsed, cons
e9260 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
e9270 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e){.  UNUSED_PAR
e9280 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e9290 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 6e  .  return dlopen
e92a0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c 44  (zFilename, RTLD
e92b0 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f 42  _NOW | RTLD_GLOB
e92c0 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51  AL);.}../*.** SQ
e92d0 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20  Lite calls this 
e92e0 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 61  function immedia
e92f0 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61 6c  tely after a cal
e9300 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 29  l to unixDlSym()
e9310 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 65   or.** unixDlOpe
e9320 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75 72  n() fails (retur
e9330 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65  ns a null pointe
e9340 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 65  r). If a more de
e9350 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a 20  tailed error.** 
e9360 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 6c  message is avail
e9370 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69 74  able, it is writ
e9380 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e 20  ten to zBufOut. 
e9390 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73  If no error mess
e93a0 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61  age.** is availa
e93b0 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 20  ble, zBufOut is 
e93c0 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 20  left unmodified 
e93d0 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73 20  and SQLite uses 
e93e0 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 72  a default.** err
e93f0 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73  or message..*/.s
e9400 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44  tatic void unixD
e9410 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76  lError(sqlite3_v
e9420 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74  fs *NotUsed, int
e9430 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
e9440 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a  fOut){.  char *z
e9450 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  Err;.  UNUSED_PA
e9460 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e9470 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  ;.  unixEnterMut
e9480 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64  ex();.  zErr = d
e9490 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20  lerror();.  if( 
e94a0 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  zErr ){.    sqli
e94b0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
e94c0 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22  f, zBufOut, "%s"
e94d0 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75  , zErr);.  }.  u
e94e0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
e94f0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28  .}.static void (
e9500 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74  *unixDlSym(sqlit
e9510 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e9520 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20   void *p, const 
e9530 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64  char*zSym))(void
e9540 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43  ){.  /* .  ** GC
e9550 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63  C with -pedantic
e9560 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61  -errors says tha
e9570 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61  t C90 does not a
e9580 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20  llow a void* to 
e9590 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74  be.  ** cast int
e95a0 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  o a pointer to a
e95b0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20   function.  And 
e95c0 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  yet the library 
e95d0 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a  dlsym() routine.
e95e0 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76    ** returns a v
e95f0 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65  oid* which is re
e9600 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74  ally a pointer t
e9610 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53  o a function.  S
e9620 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a  o how do we.  **
e9630 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74   use dlsym() wit
e9640 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f  h -pedantic-erro
e9650 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61  rs?.  **.  ** Va
e9660 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69  riable x below i
e9670 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20  s defined to be 
e9680 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
e9690 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20  unction taking. 
e96a0 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76   ** parameters v
e96b0 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63  oid* and const c
e96c0 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69  har* and returni
e96d0 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
e96e0 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a  a function..  **
e96f0 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78   We initialize x
e9700 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74   by assigning it
e9710 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
e9720 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69  e dlsym() functi
e9730 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61  on..  ** (That a
e9740 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72  ssignment requir
e9750 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65  es a cast.)  The
e9760 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75  n we call the fu
e9770 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a  nction that.  **
e9780 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a   x points to.  .
e9790 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77    **.  ** This w
e97a0 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e  ork-around is un
e97b0 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63  likely to work c
e97c0 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20  orrectly on any 
e97d0 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a  system where.  *
e97e0 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e  * you really can
e97f0 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74  not cast a funct
e9800 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f  ion pointer into
e9810 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65   void*.  But the
e9820 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f  n, on the.  ** o
e9830 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d  ther hand, dlsym
e9840 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b  () will not work
e9850 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65   on such a syste
e9860 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20  m either, so we 
e9870 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65  have.  ** not re
e9880 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69  ally lost anythi
e9890 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20  ng..  */.  void 
e98a0 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e  (*(*x)(void*,con
e98b0 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29  st char*))(void)
e98c0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
e98d0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e98e0 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28   x = (void(*(*)(
e98f0 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
e9900 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b  *))(void))dlsym;
e9910 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70  .  return (*x)(p
e9920 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69  , zSym);.}.stati
e9930 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f  c void unixDlClo
e9940 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  se(sqlite3_vfs *
e9950 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70  NotUsed, void *p
e9960 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45  Handle){.  UNUSE
e9970 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
e9980 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28  sed);.  dlclose(
e9990 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73  pHandle);.}.#els
e99a0 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f  e /* if SQLITE_O
e99b0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
e99c0 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a  ON is defined: *
e99d0 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78  /.  #define unix
e99e0 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66  DlOpen  0.  #def
e99f0 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20  ine unixDlError 
e9a00 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78  0.  #define unix
e9a10 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66  DlSym   0.  #def
e9a20 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20  ine unixDlClose 
e9a30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
e9a40 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73  Write nBuf bytes
e9a50 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20   of random data 
e9a60 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  to the supplied 
e9a70 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a  buffer zBuf..*/.
e9a80 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52  static int unixR
e9a90 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65  andomness(sqlite
e9aa0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
e9ab0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
e9ac0 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f  zBuf){.  UNUSED_
e9ad0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e9ae0 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69  d);.  assert((si
e9af0 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65  ze_t)nBuf>=(size
e9b00 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f  of(time_t)+sizeo
e9b10 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20  f(int)));..  /* 
e9b20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69  We have to initi
e9b30 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72  alize zBuf to pr
e9b40 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66  event valgrind f
e9b50 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20  rom reporting.  
e9b60 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20  ** errors.  The 
e9b70 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62  reports issued b
e9b80 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69  y valgrind are i
e9b90 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f  ncorrect - we wo
e9ba0 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20  uld.  ** prefer 
e9bb0 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e  that the randomn
e9bc0 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64  ess be increased
e9bd0 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f   by making use o
e9be0 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69  f the.  ** unini
e9bf0 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69  tialized space i
e9c00 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c  n zBuf - but val
e9c10 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e  grind errors ten
e9c20 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20  d to worry.  ** 
e9c30 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74  some users.  Rat
e9c40 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20  her than argue, 
e9c50 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20  it seems easier 
e9c60 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69  just to initiali
e9c70 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c  ze.  ** the whol
e9c80 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65  e array and sile
e9c90 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76  nce valgrind, ev
e9ca0 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73  en if that means
e9cb0 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73   less randomness
e9cc0 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e  .  ** in the ran
e9cd0 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20  dom seed..  **. 
e9ce0 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67   ** When testing
e9cf0 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a  , initializing z
e9d00 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73  Buf[] to zero is
e9d10 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61   all we do.  Tha
e9d20 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61  t means.  ** tha
e9d30 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20  t we always use 
e9d40 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20  the same random 
e9d50 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e  number sequence.
e9d60 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65    This makes the
e9d70 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65  .  ** tests repe
e9d80 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d  atable..  */.  m
e9d90 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e  emset(zBuf, 0, n
e9da0 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e  Buf);.#if !defin
e9db0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
e9dc0 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c    {.    int pid,
e9dd0 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70   fd;.    fd = op
e9de0 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d  en("/dev/urandom
e9df0 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20  ", O_RDONLY);.  
e9e00 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20    if( fd<0 ){.  
e9e10 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20      time_t t;.  
e9e20 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20      time(&t);.  
e9e30 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c      memcpy(zBuf,
e9e40 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b   &t, sizeof(t));
e9e50 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 74  .      pid = get
e9e60 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d  pid();.      mem
e9e70 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66  cpy(&zBuf[sizeof
e9e80 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65  (t)], &pid, size
e9e90 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20  of(pid));.      
e9ea0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74  assert( sizeof(t
e9eb0 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28  )+sizeof(pid)<=(
e9ec0 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20  size_t)nBuf );. 
e9ed0 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65       nBuf = size
e9ee0 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70  of(t) + sizeof(p
e9ef0 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  id);.    }else{.
e9f00 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65 61        nBuf = rea
e9f10 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66  d(fd, zBuf, nBuf
e9f20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66  );.      close(f
e9f30 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  d);.    }.  }.#e
e9f40 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 42  ndif.  return nB
e9f50 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c  uf;.}.../*.** Sl
e9f60 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65  eep for a little
e9f70 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20   while.  Return 
e9f80 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69  the amount of ti
e9f90 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65  me slept..** The
e9fa0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
e9fb0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f   number of micro
e9fc0 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20  seconds we want 
e9fd0 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65  to sleep..** The
e9fe0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
e9ff0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   the number of m
ea000 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73  icroseconds of s
ea010 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a  leep actually.**
ea020 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20   requested from 
ea030 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  the underlying o
ea040 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c  perating system,
ea050 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a   a number which.
ea060 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 61  ** might be grea
ea070 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
ea080 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e  l to the argumen
ea090 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a  t, but not less.
ea0a0 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 75  ** than the argu
ea0b0 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ment..*/.static 
ea0c0 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73 71  int unixSleep(sq
ea0d0 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73  lite3_vfs *NotUs
ea0e0 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  ed, int microsec
ea0f0 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 58  onds){.#if OS_VX
ea100 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74  WORKS.  struct t
ea110 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 73  imespec sp;..  s
ea120 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f  p.tv_sec = micro
ea130 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 30  seconds / 100000
ea140 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 20  0;.  sp.tv_nsec 
ea150 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  = (microseconds 
ea160 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30 30  % 1000000) * 100
ea170 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26  0;.  nanosleep(&
ea180 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e 55  sp, NULL);.  UNU
ea190 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
ea1a0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e  tUsed);.  return
ea1b0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23   microseconds;.#
ea1c0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56  elif defined(HAV
ea1d0 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56  E_USLEEP) && HAV
ea1e0 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 65  E_USLEEP.  uslee
ea1f0 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b  p(microseconds);
ea200 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ea210 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
ea220 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f  return microseco
ea230 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74  nds;.#else.  int
ea240 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72   seconds = (micr
ea250 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29  oseconds+999999)
ea260 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65  /1000000;.  slee
ea270 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e  p(seconds);.  UN
ea280 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
ea290 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
ea2a0 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 30  n seconds*100000
ea2b0 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  0;.#endif.}../*.
ea2c0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
ea2d0 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65   variable, if se
ea2e0 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  t to a non-zero 
ea2f0 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 70  value, is interp
ea300 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65 20  reted as.** the 
ea310 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e 64  number of second
ea320 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e 64  s since 1970 and
ea330 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20   is used to set 
ea340 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
ea350 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
ea360 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 74  tTime() during t
ea370 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65  esting..*/.#ifde
ea380 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
ea390 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
ea3a0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d  ite3_current_tim
ea3b0 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 20  e = 0;  /* Fake 
ea3c0 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 73  system time in s
ea3d0 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37  econds since 197
ea3e0 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a  0. */.#endif../*
ea3f0 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72  .** Find the cur
ea400 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e  rent time (in Un
ea410 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61  iversal Coordina
ea420 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74  ted Time).  Writ
ea430 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  e the.** current
ea440 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61   time and date a
ea450 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e  s a Julian Day n
ea460 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f  umber into *prNo
ea470 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  w and.** return 
ea480 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  0.  Return 1 if 
ea490 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74  the time and dat
ea4a0 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  e cannot be foun
ea4b0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
ea4c0 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65   unixCurrentTime
ea4d0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f  (sqlite3_vfs *No
ea4e0 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a 70  tUsed, double *p
ea4f0 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69 6e  rNow){.#if defin
ea500 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
ea510 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20  LOATING_POINT). 
ea520 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d   time_t t;.  tim
ea530 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20  e(&t);.  *prNow 
ea540 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74  = (((sqlite3_int
ea550 36 34 29 74 29 2f 38 36 34 30 20 2b 20 32 34 34  64)t)/8640 + 244
ea560 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 6c 69 66  05875)/10;.#elif
ea570 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54   defined(NO_GETT
ea580 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a  OD).  time_t t;.
ea590 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70    time(&t);.  *p
ea5a0 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e 30  rNow = t/86400.0
ea5b0 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 65   + 2440587.5;.#e
ea5c0 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  lif OS_VXWORKS. 
ea5d0 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63   struct timespec
ea5e0 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f 67   sNow;.  clock_g
ea5f0 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 41  ettime(CLOCK_REA
ea600 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a 20  LTIME, &sNow);. 
ea610 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38   *prNow = 244058
ea620 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65  7.5 + sNow.tv_se
ea630 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77  c/86400.0 + sNow
ea640 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 30  .tv_nsec/8640000
ea650 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 65  0000000.0;.#else
ea660 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76 61  .  struct timeva
ea670 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 6d  l sNow;.  gettim
ea680 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 29  eofday(&sNow, 0)
ea690 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34  ;.  *prNow = 244
ea6a0 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76  0587.5 + sNow.tv
ea6b0 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73  _sec/86400.0 + s
ea6c0 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 30  Now.tv_usec/8640
ea6d0 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 69  0000000.0;.#endi
ea6e0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
ea6f0 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69  _TEST.  if( sqli
ea700 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
ea710 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d   ){.    *prNow =
ea720 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
ea730 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20  _time/86400.0 + 
ea740 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23  2440587.5;.  }.#
ea750 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50  endif.  UNUSED_P
ea760 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
ea770 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  );.  return 0;.}
ea780 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 64  ../*.** We added
ea790 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 72   the xGetLastErr
ea7a0 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68  or() method with
ea7b0 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 6f   the intention o
ea7c0 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 62  f providing.** b
ea7d0 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c 20  etter low-level 
ea7e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77  error messages w
ea7f0 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 79  hen operating-sy
ea800 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f  stem problems co
ea810 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20  me up.** during 
ea820 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e  SQLite operation
ea830 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 6e  .  But so far, n
ea840 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 20  one of that has 
ea850 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64  been implemented
ea860 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 2e  .** in the core.
ea870 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e    So this routin
ea880 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
ea890 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 20  d.  For now, it 
ea8a0 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 70  is merely.** a p
ea8b0 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f 0a  lace-holder..*/.
ea8c0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47  static int unixG
ea8d0 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69  etLastError(sqli
ea8e0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
ea8f0 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c 20  , int NotUsed2, 
ea900 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 7b  char *NotUsed3){
ea910 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ea920 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
ea930 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
ea940 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 4e  (NotUsed2);.  UN
ea950 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
ea960 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 75  otUsed3);.  retu
ea970 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a  rn 0;.}../*.****
ea980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea990 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69  **** End of sqli
ea9a0 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20  te3_vfs methods 
ea9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
ea9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a  **********/../**
eaa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
eaa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa80 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 72  ******* Begin Pr
eaa90 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a  oxy Locking ****
eaaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
eaac0 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  ** Proxy locking
eaad0 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 6b   is a "uber-lock
eaae0 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74  ing-method" in t
eaaf0 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20 75  his sense:  It u
eab00 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 72  ses the.** other
eab10 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73   locking methods
eab20 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c 6f   on secondary lo
eab30 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 79  ck files.  Proxy
eab40 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a 2a   locking is a.**
eab50 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 72   meta-layer over
eab60 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69 6d   top of the prim
eab70 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 6d  itive locking im
eab80 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 2e  plemented above.
eab90 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 65    For.** this re
eaba0 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69  ason, the divisi
eabb0 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  on that implemen
eabc0 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b  ts of proxy lock
eabd0 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 0a  ing is deferred.
eabe0 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e  ** until late in
eabf0 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 65 29   the file (here)
eac00 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68   after all of th
eac10 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68  e other I/O meth
eac20 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e  ods have.** been
eac30 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68   defined - so th
eac40 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65  at the primitive
eac50 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73   locking methods
eac60 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a   are available.*
eac70 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74 6f  * as services to
eac80 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 69   help with the i
eac90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
eaca0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a   proxy locking..
eacb0 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  **.****.**.** Th
eacc0 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e  e default lockin
eacd0 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51 4c  g schemes in SQL
eace0 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 61 6e  ite use byte-ran
eacf0 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 0a  ge locks on the.
ead00 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
ead10 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 73   to coordinate s
ead20 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20  afe, concurrent 
ead30 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70  access by multip
ead40 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 6e  le readers.** an
ead50 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 70 3a  d writers [http:
ead60 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f 63  //sqlite.org/loc
ead70 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 54  kingv3.html].  T
ead80 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f 63  he five file loc
ead90 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 28  king.** states (
eada0 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 4e  UNLOCKED, PENDIN
eadb0 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 52  G, SHARED, RESER
eadc0 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29 20  VED, EXCLUSIVE) 
eadd0 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a  are implemented.
eade0 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61 64  ** as POSIX read
eadf0 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 6f   & write locks o
eae00 76 65 72 20 66 69 78 65 64 20 73 65 74 20 6f 66  ver fixed set of
eae10 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 20   locations (via 
eae20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 46  fsctl),.** on AF
eae30 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 65  P and SMB only e
eae40 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61  xclusive byte-ra
eae50 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 76  nge locks are av
eae60 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63 74  ailable via fsct
eae70 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 28  l.** with _IOWR(
eae80 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20  'z', 23, struct 
eae90 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
eaea0 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20 73  ) to track the s
eaeb0 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a 2a  ame 5 states..**
eaec0 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 46   To simulate a F
eaed0 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 68  _RDLCK on the sh
eaee0 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 41  ared range, on A
eaef0 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 65  FP a randomly se
eaf00 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 73  lected.** addres
eaf10 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  s in the shared 
eaf20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66  range is taken f
eaf30 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  or a SHARED lock
eaf40 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20  , the entire.** 
eaf50 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 20  shared range is 
eaf60 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 43  taken for an EXC
eaf70 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a 2a  LUSIVE lock):.**
eaf80 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47  .**      PENDING
eaf90 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 78 34  _BYTE        0x4
eafa0 30 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a 2a  0000000..   ..**
eafb0 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42        RESERVED_B
eafc0 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30 30  YTE       0x4000
eafd0 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 41  0001.**      SHA
eafe0 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20 20  RED_RANGE       
eaff0 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20 30   0x40000002 -> 0
eb000 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20  x40000200.**.** 
eb010 54 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c 20  This works well 
eb020 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c  on the local fil
eb030 65 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73 68  e system, but sh
eb040 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30 30  ows a nearly 100
eb050 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 6e  x.** slowdown in
eb060 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e 63   read performanc
eb070 65 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73 65  e on AFP because
eb080 20 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74 20   the AFP client 
eb090 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 20  disables.** the 
eb0a0 72 65 61 64 20 63 61 63 68 65 20 77 68 65 6e 20  read cache when 
eb0b0 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73  byte-range locks
eb0c0 20 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20 45   are present.  E
eb0d0 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61 64  nabling the read
eb0e0 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73 65  .** cache expose
eb0f0 73 20 61 20 63 61 63 68 65 20 63 6f 68 65 72 65  s a cache cohere
eb100 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 74  ncy problem that
eb110 20 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 61   is present on a
eb120 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 6f  ll OS X.** suppo
eb130 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 6c  rted network fil
eb140 65 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 20  e systems.  NFS 
eb150 61 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62 73  and AFP both obs
eb160 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f 73  erve the.** clos
eb170 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e 74  e-to-open semant
eb180 69 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e 67  ics for ensuring
eb190 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79   cache coherency
eb1a0 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e  .** [http://nfs.
eb1b0 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 2f  sourceforge.net/
eb1c0 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 20  #faq_a8], which 
eb1d0 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74 69  does not effecti
eb1e0 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 20  vely.** address 
eb1f0 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73  the requirements
eb200 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 20   for concurrent 
eb210 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20  database access 
eb220 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 72  by multiple.** r
eb230 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65  eaders and write
eb240 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 77  rs.** [http://ww
eb250 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c  w.nabble.com/SQL
eb260 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 65  ite-on-NFS-cache
eb270 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 36  -coherency-td156
eb280 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a  55701.html]..**.
eb290 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74 68  ** To address th
eb2a0 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e  e performance an
eb2b0 64 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63  d cache coherenc
eb2c0 79 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20  y issues, proxy 
eb2d0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20  file locking.** 
eb2e0 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 20  changes the way 
eb2f0 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20  database access 
eb300 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79  is controlled by
eb310 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73   limiting access
eb320 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20   to a.** single 
eb330 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 61  host at a time a
eb340 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c  nd moving file l
eb350 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20  ocks off of the 
eb360 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a  database file.**
eb370 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f 78   and onto a prox
eb380 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c 6f  y file on the lo
eb390 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2e  cal file system.
eb3a0 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e    .**.**.** Usin
eb3b0 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a  g proxy locks.**
eb3c0 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
eb3d0 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 0a  --.**.** C APIs.
eb3e0 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66  **.**  sqlite3_f
eb3f0 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20  ile_control(db, 
eb400 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 53  dbname, SQLITE_S
eb410 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45  ET_LOCKPROXYFILE
eb420 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ,.**            
eb430 20 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f 78             <prox
eb440 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 6f  y_path> | ":auto
eb450 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33  :");.**  sqlite3
eb460 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62  _file_control(db
eb470 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45  , dbname, SQLITE
eb480 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _GET_LOCKPROXYFI
eb490 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 68  LE, &<proxy_path
eb4a0 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c  >);.**.**.** SQL
eb4b0 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20   pragmas.**.**  
eb4c0 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
eb4d0 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c  .]lock_proxy_fil
eb4e0 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c  e=<proxy_path> |
eb4f0 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 47   :auto:.**  PRAG
eb500 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
eb510 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a  ck_proxy_file.**
eb520 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20 22  .** Specifying "
eb530 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 68  :auto:" means th
eb540 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61  at if there is a
eb550 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 68   conch file with
eb560 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 68   a matching.** h
eb570 6f 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74 68  ost ID in it, th
eb580 65 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e 20  e proxy path in 
eb590 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77  the conch file w
eb5a0 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 68  ill be used, oth
eb5b0 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f 78  erwise.** a prox
eb5c0 79 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e 20  y path based on 
eb5d0 74 68 65 20 75 73 65 72 27 73 20 74 65 6d 70 20  the user's temp 
eb5e0 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e 66  dir.** (via conf
eb5f0 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55  str(_CS_DARWIN_U
eb600 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e  SER_TEMP_DIR,...
eb610 29 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  )) will be used 
eb620 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 61  and the.** actua
eb630 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 6d  l proxy file nam
eb640 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66  e is generated f
eb650 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e 64  rom the name and
eb660 20 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20   path of the.** 
eb670 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
eb680 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  For example:.**.
eb690 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61 74  **       For dat
eb6a0 61 62 61 73 65 20 70 61 74 68 20 22 2f 55 73 65  abase path "/Use
eb6b0 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a  rs/me/foo.db" .*
eb6c0 2a 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63 6b  *       The lock
eb6d0 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22 3c   path will be "<
eb6e0 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 6c  tmpdir>/sqlitepl
eb6f0 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f 66  ocks/_Users_me_f
eb700 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a  oo.db:auto:").**
eb710 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20  .** Once a lock 
eb720 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 75  proxy is configu
eb730 72 65 64 20 66 6f 72 20 61 20 64 61 74 61 62 61  red for a databa
eb740 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69  se connection, i
eb750 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20  t can not.** be 
eb760 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72  removed, however
eb770 20 69 74 20 6d 61 79 20 62 65 20 73 77 69 74 63   it may be switc
eb780 68 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 65  hed to a differe
eb790 6e 74 20 70 72 6f 78 79 20 70 61 74 68 20 76 69  nt proxy path vi
eb7a0 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41  a.** the above A
eb7b0 50 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68  PIs (assuming th
eb7c0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20  e conch file is 
eb7d0 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62  not being held b
eb7e0 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e  y another.** con
eb7f0 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65  nection or proce
eb800 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48  ss). .**.**.** H
eb810 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  ow proxy locking
eb820 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d   works.** ------
eb830 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb840 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 69  -.**.** Proxy fi
eb850 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 65  le locking relie
eb860 73 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 74  s primarily on t
eb870 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69 6e  wo new supportin
eb880 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20  g files: .**.** 
eb890 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 20    *  conch file 
eb8a0 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73 20  to limit access 
eb8b0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
eb8c0 66 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c 65  file to a single
eb8d0 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 74   host.**      at
eb8e0 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20   a time.**.**   
eb8f0 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74 6f  *  proxy file to
eb900 20 61 63 74 20 61 73 20 61 20 70 72 6f 78 79 20   act as a proxy 
eb910 66 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72 79  for the advisory
eb920 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a   locks normally.
eb930 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f 6e  **      taken on
eb940 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
eb950 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69  .** The conch fi
eb960 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72  le - to use a pr
eb970 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65  oxy file, sqlite
eb980 20 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c   must first "hol
eb990 64 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a 20  d the conch".** 
eb9a0 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c  by taking an sql
eb9b0 69 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64  ite-style shared
eb9c0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e   lock on the con
eb9d0 63 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67  ch file, reading
eb9e0 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73   the.** contents
eb9f0 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74   and comparing t
eba00 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65  he host's unique
eba10 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 65   host ID (see be
eba20 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a  low) and lock.**
eba30 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61 69   proxy path agai
eba40 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20 73  nst the values s
eba50 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e  tored in the con
eba60 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 66  ch.  The conch f
eba70 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64  ile is.** stored
eba80 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72   in the same dir
eba90 65 63 74 6f 72 79 20 61 73 20 74 68 65 20 64 61  ectory as the da
ebaa0 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
ebab0 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a  the file name.**
ebac0 20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61 66   is patterned af
ebad0 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65  ter the database
ebae0 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 2e   file name as ".
ebaf0 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d 63  <databasename>-c
ebb00 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 65  onch"..** If the
ebb10 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 73   conch file does
ebb20 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 69   not exist, or i
ebb30 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f 20  t's contents do 
ebb40 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a 2a  not match the.**
ebb50 20 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 20   host ID and/or 
ebb60 70 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65 6e  proxy path, then
ebb70 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 63   the lock is esc
ebb80 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78 63  alated to an exc
ebb90 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61  lusive.** lock a
ebba0 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  nd the conch fil
ebbb0 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 70  e contents is up
ebbc0 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 68  dated with the h
ebbd0 6f 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78 79  ost ID and proxy
ebbe0 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68 65  .** path and the
ebbf0 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 61   lock is downgra
ebc00 64 65 64 20 74 6f 20 61 20 73 68 61 72 65 64 20  ded to a shared 
ebc10 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 20  lock again.  If 
ebc20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 20  the conch.** is 
ebc30 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20  held by another 
ebc40 70 72 6f 63 65 73 73 20 28 77 69 74 68 20 61 20  process (with a 
ebc50 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 68  shared lock), th
ebc60 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
ebc70 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e  .** will fail an
ebc80 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  d SQLITE_BUSY is
ebc90 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
ebca0 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20   The proxy file 
ebcb0 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  - a single-byte 
ebcc0 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61 6c  file used for al
ebcd0 6c 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65 20  l advisory file 
ebce0 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c  locks.** normall
ebcf0 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64  y taken on the d
ebd00 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 20  atabase file.   
ebd10 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 20  This allows for 
ebd20 73 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20  safe sharing.** 
ebd30 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
ebd40 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c  file for multipl
ebd50 65 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72  e readers and wr
ebd60 69 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61 6d  iters on the sam
ebd70 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63  e.** host (the c
ebd80 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68 61  onch ensures tha
ebd90 74 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74  t they all use t
ebda0 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f  he same local lo
ebdb0 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20  ck file)..**.** 
ebdc0 54 68 65 72 65 20 69 73 20 61 20 74 68 69 72 64  There is a third
ebdd0 20 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 74   file - the host
ebde0 20 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64 20   ID file - used 
ebdf0 61 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 20  as a persistent 
ebe00 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 75  record.** of a u
ebe10 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72  nique identifier
ebe20 20 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 61   for the host, a
ebe30 20 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75 65   128-byte unique
ebe40 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a   host id file.**
ebe50 20 69 6e 20 74 68 65 20 70 61 74 68 20 64 65 66   in the path def
ebe60 69 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53 54  ined by the HOST
ebe70 49 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64 65  IDPATH macro (de
ebe80 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a 2a  fault value is.*
ebe90 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65  * /Library/Cache
ebea0 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c  s/.com.apple.sql
ebeb0 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 2e  iteConchHostId).
ebec0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 6e  .**.** Requestin
ebed0 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 79  g the lock proxy
ebee0 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 69   does not immedi
ebef0 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20 63  ately take the c
ebf00 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 6f  onch, it is.** o
ebf10 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 74  nly taken when t
ebf20 68 65 20 66 69 72 73 74 20 72 65 71 75 65 73 74  he first request
ebf30 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 73   to lock databas
ebf40 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e 20  e file is made. 
ebf50 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 65   .** This matche
ebf60 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 20  s the semantics 
ebf70 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 6f 6e  of the tradition
ebf80 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 76  al locking behav
ebf90 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f 70  ior, where.** op
ebfa0 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 69  ening a connecti
ebfb0 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65  on to a database
ebfc0 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 74   file does not t
ebfd0 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 74  ake a lock on it
ebfe0 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20  ..** The shared 
ebff0 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e  lock and an open
ec000 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
ec010 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 20   are maintained 
ec020 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 6f  until .** the co
ec030 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20  nnection to the 
ec040 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73  database is clos
ec050 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ed. .**.** The p
ec060 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68  roxy file and th
ec070 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20  e lock file are 
ec080 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f  never deleted so
ec090 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0a   they only need.
ec0a0 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  ** to be created
ec0b0 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
ec0c0 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a 2a  they are used..*
ec0d0 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69  *.** Configurati
ec0e0 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d 2d  on options.** --
ec0f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ec100 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54  ---.**.**  SQLIT
ec110 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c  E_PREFER_PROXY_L
ec120 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 20  OCKING.**.**    
ec130 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65     Database file
ec140 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e 6f  s accessed on no
ec150 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73  n-local file sys
ec160 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 20  tems are.**     
ec170 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20    automatically 
ec180 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 70  configured for p
ec190 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c 6f  roxy locking, lo
ec1a0 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20  ck files are.** 
ec1b0 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74 6f        named auto
ec1c0 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 20  matically using 
ec1d0 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 61  the same logic a
ec1e0 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47 4d  s.**       PRAGM
ec1f0 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c  A lock_proxy_fil
ec200 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 20  e=":auto:".**   
ec210 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f   .**  SQLITE_PRO
ec220 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 20  XY_DEBUG.**.**  
ec230 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65       Enables the
ec240 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f   logging of erro
ec250 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e  r messages durin
ec260 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a  g host id file.*
ec270 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76 61  *       retrieva
ec280 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a 2a  l and creation.*
ec290 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 48  *.**  HOSTIDPATH
ec2a0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65  .**.**       Ove
ec2b0 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75  rrides the defau
ec2c0 6c 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20  lt host ID file 
ec2d0 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  path location.**
ec2e0 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 49  .**  LOCKPROXYDI
ec2f0 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76  R.**.**       Ov
ec300 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61  errides the defa
ec310 75 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75 73  ult directory us
ec320 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f 78  ed for lock prox
ec330 79 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a 20  y files that.** 
ec340 20 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64 20        are named 
ec350 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 69  automatically vi
ec360 61 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 73  a the ":auto:" s
ec370 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 51  etting.**.**  SQ
ec380 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f  LITE_DEFAULT_PRO
ec390 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e  XYDIR_PERMISSION
ec3a0 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 65  S.**.**       Pe
ec3b0 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 65  rmissions to use
ec3c0 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61   when creating a
ec3d0 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20 73   directory for s
ec3e0 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20  toring the.**   
ec3f0 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66      lock proxy f
ec400 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 20  iles, only used 
ec410 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 49  when LOCKPROXYDI
ec420 52 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a  R is not set..**
ec430 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 41      .**    .** A
ec440 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76  s mentioned abov
ec450 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64  e, when compiled
ec460 20 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45   with SQLITE_PRE
ec470 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  FER_PROXY_LOCKIN
ec480 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68  G,.** setting th
ec490 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61  e environment va
ec4a0 72 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f  riable SQLITE_FO
ec4b0 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  RCE_PROXY_LOCKIN
ec4c0 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 66  G to 1 will.** f
ec4d0 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69  orce proxy locki
ec4e0 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f  ng to be used fo
ec4f0 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65  r every database
ec500 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e   file opened, an
ec510 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 63  d 0.** will forc
ec520 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f 78  e automatic prox
ec530 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20  y locking to be 
ec540 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c 6c  disabled for all
ec550 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
ec560 65 73 20 28 65 78 70 6c 69 63 69 74 79 20 63 61  es (explicity ca
ec570 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45  lling the SQLITE
ec580 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
ec590 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a 20  LE pragma or.** 
ec5a0 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e 74  sqlite_file_cont
ec5b0 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 61  rol API is not a
ec5c0 66 66 65 63 74 65 64 20 62 79 20 53 51 4c 49 54  ffected by SQLIT
ec5d0 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f  E_FORCE_PROXY_LO
ec5e0 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  CKING)..*/../*.*
ec5f0 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  * Proxy locking 
ec600 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
ec610 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f 0a  e on MacOSX .*/.
ec620 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
ec630 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
ec640 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
ec650 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 51  STYLE..#ifdef SQ
ec660 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 6d  LITE_TEST./* sim
ec670 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68  ulate multiple h
ec680 6f 73 74 73 20 62 79 20 63 72 65 61 74 69 6e 67  osts by creating
ec690 20 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66   unique hostid f
ec6a0 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 4c  ile paths */.SQL
ec6b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
ec6c0 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d  te3_hostid_num =
ec6d0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
ec6e0 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b 69  * The proxyLocki
ec6f0 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 68  ngContext has th
ec700 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 20  e path and file 
ec710 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 74  structures for t
ec720 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 6e  he remote .** an
ec730 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 69  d local proxy fi
ec740 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 70  les in it.*/.typ
ec750 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f 78  edef struct prox
ec760 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
ec770 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
ec780 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f 78  ext;.struct prox
ec790 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
ec7a0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f  {.  unixFile *co
ec7b0 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20  nchFile;        
ec7c0 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 66   /* Open conch f
ec7d0 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63  ile */.  char *c
ec7e0 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 20  onchFilePath;   
ec7f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
ec800 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
ec810 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c  */.  unixFile *l
ec820 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20  ockProxy;       
ec830 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20    /* Open proxy 
ec840 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63  lock file */.  c
ec850 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61  har *lockProxyPa
ec860 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  th;         /* N
ec870 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79  ame of the proxy
ec880 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20   lock file */.  
ec890 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20  char *dbPath;   
ec8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ec8b0 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e  Name of the open
ec8c0 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63   file */.  int c
ec8d0 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 20  onchHeld;       
ec8e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
ec8f0 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20  if the conch is 
ec900 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 2a  currently held *
ec910 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63  /.  void *oldLoc
ec920 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20  kingContext;    
ec930 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63   /* Original loc
ec940 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 72  kingcontext to r
ec950 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20  estore on close 
ec960 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  */.  sqlite3_io_
ec970 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70  methods const *p
ec980 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f  OldMethod;     /
ec990 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d  * Original I/O m
ec9a0 65 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65  ethods for close
ec9b0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 49   */.};../* HOSTI
ec9c0 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c 45  DLEN and CONCHLE
ec9d0 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 73  N both include s
ec9e0 70 61 63 65 20 66 6f 72 20 74 68 65 20 73 74 72  pace for the str
ec9f0 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 74  ing .** terminat
eca00 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 66  ing nul .*/.#def
eca10 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 20  ine HOSTIDLEN   
eca20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e        128.#defin
eca30 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 20  e CONCHLEN      
eca40 20 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e 2b      (MAXPATHLEN+
eca50 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 66  HOSTIDLEN+1).#if
eca60 6e 64 65 66 20 48 4f 53 54 49 44 50 41 54 48 0a  ndef HOSTIDPATH.
eca70 23 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44 50  # define HOSTIDP
eca80 41 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62 72  ATH       "/Libr
eca90 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e  ary/Caches/.com.
ecaa0 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63  apple.sqliteConc
ecab0 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 0a  hHostId".#endif.
ecac0 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 20  ./* basically a 
ecad0 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e 64  copy of unixRand
ecae0 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66 66  omness with diff
ecaf0 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 65  erent.** test be
ecb00 68 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e 20  havior built in 
ecb10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
ecb20 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49  oxyGenerateHostI
ecb30 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 29  D(char *pHostID)
ecb40 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64 2c  {.  int pid, fd,
ecb50 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64   len;.  unsigned
ecb60 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 6e   char *key = (un
ecb70 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 48  signed char *)pH
ecb80 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d 73  ostID;.  .  mems
ecb90 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 49  et(key, 0, HOSTI
ecba0 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 30  DLEN);.  len = 0
ecbb0 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f  ;.  fd = open("/
ecbc0 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f  dev/urandom", O_
ecbd0 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 66  RDONLY);.  if( f
ecbe0 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e 20  d>=0 ){.    len 
ecbf0 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c 20  = read(fd, key, 
ecc00 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20  HOSTIDLEN);.    
ecc10 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69  close(fd); /* si
ecc20 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20  lently leak the 
ecc30 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a  fd if it fails *
ecc40 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e 20  /.  }.  if( len 
ecc50 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20  < HOSTIDLEN ){. 
ecc60 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20     time_t t;.   
ecc70 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 6d   time(&t);.    m
ecc80 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 73  emcpy(key, &t, s
ecc90 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 70  izeof(t));.    p
ecca0 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20  id = getpid();. 
eccb0 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b 73     memcpy(&key[s
eccc0 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c  izeof(t)], &pid,
eccd0 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20   sizeof(pid));. 
ecce0 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 4b   }.  .#ifdef MAK
eccf0 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44 0a  E_PRETTY_HOSTID.
ecd00 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20    {.    int i;. 
ecd10 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 65     /* filter the
ecd20 20 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69 6e   bytes into prin
ecd30 74 61 62 6c 65 20 61 73 63 69 69 20 63 68 61 72  table ascii char
ecd40 61 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 74  acters and NUL t
ecd50 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 20  erminate */.    
ecd60 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d 31  key[(HOSTIDLEN-1
ecd70 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 66  )] = 0x00;.    f
ecd80 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 54  or( i=0; i<(HOST
ecd90 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 7b  IDLEN-1); i++ ){
ecda0 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
ecdb0 63 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 5d  char pa = key[i]
ecdc0 26 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66 28  &0x7F;.      if(
ecdd0 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 20   pa<0x20 ){.    
ecde0 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65      key[i] = (ke
ecdf0 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38  y[i]&0x80 == 0x8
ece00 30 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 70  0) ? pa+0x40 : p
ece10 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d 65  a+0x20;.      }e
ece20 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37 46  lse if( pa==0x7F
ece30 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b   ){.        key[
ece40 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38  i] = (key[i]&0x8
ece50 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 3d  0 == 0x80) ? pa=
ece60 30 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b 0a  0x20 : pa+0x7E;.
ece70 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
ece80 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  }.#endif.  retur
ece90 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
ecea0 2f 2a 20 77 72 69 74 65 73 20 74 68 65 20 68 6f  /* writes the ho
eceb0 73 74 20 69 64 20 70 61 74 68 20 74 6f 20 70 61  st id path to pa
ecec0 74 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 20  th, path should 
eced0 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 61  be an pre-alloca
ecee0 74 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 69  ted buffer.** wi
ecef0 74 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20  th enough space 
ecf00 66 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a 73  for a path .*/.s
ecf10 74 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78 79  tatic void proxy
ecf20 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 63 68  GetHostIDPath(ch
ecf30 61 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f 74  ar *path, size_t
ecf40 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 79   len){.  strlcpy
ecf50 28 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41 54  (path, HOSTIDPAT
ecf60 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 20  H, len);.#ifdef 
ecf70 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66  SQLITE_TEST.  if
ecf80 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64  ( sqlite3_hostid
ecf90 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 68  _num>0 ){.    ch
ecfa0 61 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 22  ar suffix[2] = "
ecfb0 31 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b 30  1";.    suffix[0
ecfc0 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 20  ] = suffix[0] + 
ecfd0 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e  sqlite3_hostid_n
ecfe0 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 28  um;.    strlcat(
ecff0 70 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c 65  path, suffix, le
ed000 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  n);.  }.#endif. 
ed010 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f   OSTRACE3("GETHO
ed020 53 54 49 44 50 41 54 48 20 20 25 73 20 70 69 64  STIDPATH  %s pid
ed030 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 65  =%d\n", path, ge
ed040 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 67  tpid());.}../* g
ed050 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 20 66  et the host ID f
ed060 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f 73  rom a sqlite hos
ed070 74 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64 20  tid file stored 
ed080 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 2d  in the .** user-
ed090 73 70 65 63 69 66 69 63 20 74 6d 70 20 64 69 72  specific tmp dir
ed0a0 65 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20 74  ectory, create t
ed0b0 68 65 20 49 44 20 69 66 20 69 74 27 73 20 6e 6f  he ID if it's no
ed0c0 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 20  t there already 
ed0d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
ed0e0 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63 68  roxyGetHostID(ch
ed0f0 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74  ar *pHostID, int
ed100 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e 74   *pError){.  int
ed110 20 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74 68   fd;.  char path
ed120 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a 20  [MAXPATHLEN]; . 
ed130 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 69   size_t len;.  i
ed140 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b  nt rc=SQLITE_OK;
ed150 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73 74  ..  proxyGetHost
ed160 49 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41 58  IDPath(path, MAX
ed170 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 74  PATHLEN);.  /* t
ed180 72 79 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ry to create the
ed190 20 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 69   host ID file, i
ed1a0 66 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69  f it already exi
ed1b0 73 74 73 20 72 65 61 64 20 74 68 65 20 63 6f 6e  sts read the con
ed1c0 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d 20  tents */.  fd = 
ed1d0 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 45  open(path, O_CRE
ed1e0 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 58  AT|O_WRONLY|O_EX
ed1f0 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 28  CL, 0644);.  if(
ed200 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74   fd<0 ){.    int
ed210 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a 20   err=errno;.... 
ed220 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49     if( err!=EEXI
ed230 53 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  ST ){.#ifdef SQL
ed240 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20  ITE_PROXY_DEBUG 
ed250 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74  /* set the sqlit
ed260 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
ed270 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20  instead */.     
ed280 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
ed290 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 63   "sqlite error c
ed2a0 72 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44 20  reating host ID 
ed2b0 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a  file %s: %s\n",.
ed2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
ed2d0 74 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72  th, strerror(err
ed2e0 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ));.#endif.     
ed2f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50   return SQLITE_P
ed300 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  ERM;.    }.    /
ed310 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74  * couldn't creat
ed320 65 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61 64  e the file, read
ed330 20 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20   it instead */. 
ed340 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74     fd = open(pat
ed350 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 58  h, O_RDONLY|O_EX
ed360 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c  CL);.    if( fd<
ed370 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  0 ){.#ifdef SQLI
ed380 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f  TE_PROXY_DEBUG /
ed390 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65  * set the sqlite
ed3a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
ed3b0 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20  nstead */.      
ed3c0 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b  int err = errno;
ed3d0 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73  .      fprintf(s
ed3e0 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65  tderr, "sqlite e
ed3f0 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f 73  rror opening hos
ed400 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73  t ID file %s: %s
ed410 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
ed420 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f     path, strerro
ed430 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a  r(err));.#endif.
ed440 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
ed450 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a  ITE_PERM;.    }.
ed460 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 28      len = pread(
ed470 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53  fd, pHostID, HOS
ed480 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20  TIDLEN, 0);.    
ed490 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20  if( len<0 ){.   
ed4a0 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72     *pError = err
ed4b0 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  no;.      rc = S
ed4c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
ed4d0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
ed4e0 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b  len<HOSTIDLEN ){
ed4f0 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d  .      *pError =
ed500 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   0;.      rc = S
ed510 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
ed520 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 20  T_READ;.    }.  
ed530 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20    close(fd); /* 
ed540 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68  silently leak th
ed550 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73  e fd if it fails
ed560 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33   */.    OSTRACE3
ed570 28 22 47 45 54 48 4f 53 54 49 44 20 20 72 65 61  ("GETHOSTID  rea
ed580 64 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20  d %s pid=%d\n", 
ed590 70 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28  pHostID, getpid(
ed5a0 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ));.    return r
ed5b0 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  c;.  }else{.    
ed5c0 2f 2a 20 77 65 27 72 65 20 63 72 65 61 74 69 6e  /* we're creatin
ed5d0 67 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69  g the host ID fi
ed5e0 6c 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f 6d  le (use a random
ed5f0 20 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65 73   string of bytes
ed600 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 65  ) */.    proxyGe
ed610 6e 65 72 61 74 65 48 6f 73 74 49 44 28 70 48 6f  nerateHostID(pHo
ed620 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 3d  stID);.    len =
ed630 20 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f 73   pwrite(fd, pHos
ed640 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20  tID, HOSTIDLEN, 
ed650 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c  0);.    if( len<
ed660 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72  0 ){.      *pErr
ed670 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  or = errno;.    
ed680 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
ed690 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d  ERR_WRITE;.    }
ed6a0 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53  else if( len<HOS
ed6b0 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20  TIDLEN ){.      
ed6c0 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20  *pError = 0;.   
ed6d0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
ed6e0 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20  OERR_WRITE;.    
ed6f0 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b  }.    close(fd);
ed700 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61   /* silently lea
ed710 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66  k the fd if it f
ed720 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52  ails */.    OSTR
ed730 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20  ACE3("GETHOSTID 
ed740 20 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25 64   wrote %s pid=%d
ed750 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65  \n", pHostID, ge
ed760 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74  tpid());.    ret
ed770 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 73  urn rc;.  }.}..s
ed780 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47  tatic int proxyG
ed790 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 74  etLockPath(const
ed7a0 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63   char *dbPath, c
ed7b0 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a 65  har *lPath, size
ed7c0 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 6e  _t maxLen){.  in
ed7d0 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 4c  t len;.  int dbL
ed7e0 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 69  en;.  int i;..#i
ed7f0 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49  fdef LOCKPROXYDI
ed800 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70  R.  len = strlcp
ed810 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 4f  y(lPath, LOCKPRO
ed820 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b 0a  XYDIR, maxLen);.
ed830 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f 43  #else.# ifdef _C
ed840 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45  S_DARWIN_USER_TE
ed850 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 63  MP_DIR.  {.    c
ed860 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49  onfstr(_CS_DARWI
ed870 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c  N_USER_TEMP_DIR,
ed880 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 3b   lPath, maxLen);
ed890 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63  .    len = strlc
ed8a0 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74  at(lPath, "sqlit
ed8b0 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e  eplocks", maxLen
ed8c0 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 72  );.    if( mkdir
ed8d0 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f 44  (lPath, SQLITE_D
ed8e0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
ed8f0 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b 0a  PERMISSIONS) ){.
ed900 20 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 69        /* if mkdi
ed910 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 20  r fails, handle 
ed920 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 65  as lock file cre
ed930 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a 2f  ation failure */
ed940 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45  .#  ifdef SQLITE
ed950 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 6e 74  _DEBUG.      int
ed960 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20   err = errno;.  
ed970 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58      if( err!=EEX
ed980 49 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20 66  IST ){.        f
ed990 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
ed9a0 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68  proxyGetLockPath
ed9b0 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 20  : mkdir(%s,0%o) 
ed9c0 65 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c 20  error %d %s\n", 
ed9d0 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 20  lPath,.         
ed9e0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45         SQLITE_DE
ed9f0 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50  FAULT_PROXYDIR_P
eda00 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 2c  ERMISSIONS, err,
eda10 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b   strerror(err));
eda20 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 69  .      }.#  endi
eda30 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  f.    }else{.   
eda40 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54     OSTRACE3("GET
eda50 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 20  LOCKPATH  mkdir 
eda60 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50  %s pid=%d\n", lP
eda70 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a  ath, getpid());.
eda80 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a 23      }.    .  }.#
eda90 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74   else.  len = st
edaa0 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74  rlcpy(lPath, "/t
edab0 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23  mp/", maxLen);.#
edac0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20   endif.#endif.. 
edad0 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31   if( lPath[len-1
edae0 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65  ]!='/' ){.    le
edaf0 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74  n = strlcat(lPat
edb00 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b  h, "/", maxLen);
edb10 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61  .  }.  .  /* tra
edb20 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61  nsform the db pa
edb30 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63  th to a unique c
edb40 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64  ache name */.  d
edb50 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c  bLen = (int)strl
edb60 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f  en(dbPath);.  fo
edb70 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20  r( i=0; i<dbLen 
edb80 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 78  && (i+len+7)<max
edb90 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  Len; i++){.    c
edba0 68 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69  har c = dbPath[i
edbb0 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c  ];.    lPath[i+l
edbc0 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27  en] = (c=='/')?'
edbd0 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74  _':c;.  }.  lPat
edbe0 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20  h[i+len]='\0';. 
edbf0 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20   strlcat(lPath, 
edc00 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e  ":auto:", maxLen
edc10 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
edc20 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
edc30 43 72 65 61 74 65 20 61 20 6e 65 77 20 56 46 53  Create a new VFS
edc40 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
edc50 20 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f   (stored in memo
edc60 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
edc70 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  .** sqlite3_mall
edc80 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 65  oc) and open the
edc90 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 74   file named "pat
edca0 68 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64  h" in the file d
edcb0 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a  escriptor..**.**
edcc0 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   The caller is r
edcd0 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f  esponsible not o
edce0 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20  nly for closing 
edcf0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
edd00 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20  tor.** but also 
edd10 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20  for freeing the 
edd20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
edd30 64 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20  d with the file 
edd40 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a 73  descriptor..*/.s
edd50 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43  tatic int proxyC
edd60 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63 6f  reateUnixFile(co
edd70 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20  nst char *path, 
edd80 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 6c  unixFile **ppFil
edd90 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  e) {.  unixFile 
edda0 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 66 6c 61  *pNew;.  int fla
eddb0 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs = SQLITE_OPEN
eddc0 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 54 45 5f  _MAIN_DB|SQLITE_
eddd0 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49  OPEN_CREATE|SQLI
edde0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
eddf0 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  E;.  int rc = SQ
ede00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74  LITE_OK;.  sqlit
ede10 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b  e3_vfs dummyVfs;
ede20 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69 78  ..  pNew = (unix
ede30 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d  File *)sqlite3_m
ede40 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e 69  alloc(sizeof(uni
ede50 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20 21  xFile));.  if( !
ede60 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74 75  pNew ){.    retu
ede70 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
ede80 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e  .  }.  memset(pN
ede90 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e  ew, 0, sizeof(un
edea0 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 2f 2a 20  ixFile));..  /* 
edeb0 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 28 29 20  Call unixOpen() 
edec0 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 72 6f 78  to open the prox
eded0 79 20 66 69 6c 65 2e 20 54 68 65 20 66 6c 61 67  y file. The flag
edee0 73 20 70 61 73 73 65 64 20 74 6f 20 75 6e 69 78  s passed to unix
edef0 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 75 67 67  Open().  ** sugg
edf00 65 73 74 20 74 68 61 74 20 74 68 65 20 66 69 6c  est that the fil
edf10 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 69  e being opened i
edf20 73 20 61 20 22 6d 61 69 6e 20 64 61 74 61 62 61  s a "main databa
edf30 73 65 22 2e 20 54 68 69 73 20 69 73 0a 20 20 2a  se". This is.  *
edf40 2a 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 6f  * necessary as o
edf50 74 68 65 72 20 66 69 6c 65 20 74 79 70 65 73 20  ther file types 
edf60 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  do not necessari
edf70 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f 63 6b 69  ly support locki
edf80 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 62  ng. It.  ** is b
edf90 65 74 74 65 72 20 74 6f 20 75 73 65 20 75 6e 69  etter to use uni
edfa0 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 61 64 20  xOpen() instead 
edfb0 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66  of opening the f
edfc0 69 6c 65 20 64 69 72 65 63 74 6c 79 20 77 69 74  ile directly wit
edfd0 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2c 20 61  h.  ** open(), a
edfe0 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 73 65 74  s unixOpen() set
edff0 73 20 75 70 20 74 68 65 20 76 61 72 69 6f 75 73  s up the various
ee000 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 65 71 75   mechanisms requ
ee010 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 6d 61 6b  ired to.  ** mak
ee020 65 20 73 75 72 65 20 61 20 63 61 6c 6c 20 74 6f  e sure a call to
ee030 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 20 6e 6f   close() does no
ee040 74 20 63 61 75 73 65 20 74 68 65 20 73 79 73 74  t cause the syst
ee050 65 6d 20 74 6f 20 64 69 73 63 61 72 64 0a 20 20  em to discard.  
ee060 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 70  ** POSIX locks p
ee070 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 20 2a 2a  rematurely..  **
ee080 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f  .  ** It is impo
ee090 72 74 61 6e 74 20 74 68 61 74 20 74 68 65 20 78  rtant that the x
ee0a0 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f 66 20 74  Open member of t
ee0b0 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 70 61  he VFS object pa
ee0c0 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a 20 75 6e  ssed to .  ** un
ee0d0 69 78 4f 70 65 6e 28 29 20 69 73 20 4e 55 4c 4c  ixOpen() is NULL
ee0e0 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 75 6e 69  . This tells uni
ee0f0 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 72 79 20  xOpen() may try 
ee100 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f 78 79 2d  to open a proxy-
ee110 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f 72 20 74  file .  ** for t
ee120 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 20 28 63  he proxy-file (c
ee130 72 65 61 74 69 6e 67 20 61 20 70 6f 74 65 6e 74  reating a potent
ee140 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f  ial infinite loo
ee150 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 6d 6d 79  p)..  */.  dummy
ee160 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20 28  Vfs.pAppData = (
ee170 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b 49  void*)&autolockI
ee180 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 6d 6d 79  oFinder;.  dummy
ee190 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 3b 0a 20  Vfs.xOpen = 0;. 
ee1a0 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e 28 26   rc = unixOpen(&
ee1b0 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 68 2c 20  dummyVfs, path, 
ee1c0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
ee1d0 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 26 66 6c  pNew, flags, &fl
ee1e0 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ags);.  if( rc==
ee1f0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 66 6c  SQLITE_OK && (fl
ee200 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ags&SQLITE_OPEN_
ee210 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a 20 20 20  READONLY) ){.   
ee220 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 2d 3e   pNew->pMethod->
ee230 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f  xClose((sqlite3_
ee240 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b 0a 20 20  file *)pNew);.  
ee250 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41    rc = SQLITE_CA
ee260 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 69  NTOPEN;.  }..  i
ee270 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
ee280 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
ee290 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20  free(pNew);.    
ee2a0 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  pNew = 0;.  }.. 
ee2b0 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 77 3b   *ppFile = pNew;
ee2c0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
ee2d0 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63 6f  ./* takes the co
ee2e0 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61 20  nch by taking a 
ee2f0 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20  shared lock and 
ee300 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  read the content
ee310 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a 20  s conch, if .** 
ee320 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e 2d  lockPath is non-
ee330 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20 49  NULL, the host I
ee340 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 20  D and lock file 
ee350 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68 2e  path must match.
ee360 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f 63    A NULL .** loc
ee370 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61 74  kPath means that
ee380 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69 6e   the lockPath in
ee390 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
ee3a0 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66 20  will be used if 
ee3b0 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44 73  the .** host IDs
ee3c0 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65 77   match, or a new
ee3d0 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20   lock path will 
ee3e0 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75 74  be generated aut
ee3f0 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 61  omatically .** a
ee400 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  nd written to th
ee410 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2f  e conch file..*/
ee420 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
ee430 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78 46  yTakeConch(unixF
ee440 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 70  ile *pFile){.  p
ee450 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
ee460 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
ee470 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
ee480 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
ee490 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20 69  Context; .  .  i
ee4a0 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65  f( pCtx->conchHe
ee4b0 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75  ld>0 ){.    retu
ee4c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
ee4d0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46  }else{.    unixF
ee4e0 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d  ile *conchFile =
ee4f0 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65   pCtx->conchFile
ee500 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74 56  ;.    char testV
ee510 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a  alue[CONCHLEN];.
ee520 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56 61      char conchVa
ee530 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20  lue[CONCHLEN];. 
ee540 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74 68     char lockPath
ee550 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20  [MAXPATHLEN];.  
ee560 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61 74    char *tLockPat
ee570 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e  h = NULL;.    in
ee580 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
ee590 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52 63  ;.    int readRc
ee5a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
ee5b0 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73 20    int syncPerms 
ee5c0 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41 43  = 0;..    OSTRAC
ee5d0 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 25  E4("TAKECONCH  %
ee5e0 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c  d for %s pid=%d\
ee5f0 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68  n", conchFile->h
ee600 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28  ,.             (
ee610 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
ee620 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b  ath ? pCtx->lock
ee630 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 75  ProxyPath : ":au
ee640 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29  to:"), getpid())
ee650 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63  ;..    rc = conc
ee660 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e  hFile->pMethod->
ee670 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66  xLock((sqlite3_f
ee680 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20  ile*)conchFile, 
ee690 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SHARED_LOCK);.  
ee6a0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
ee6b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  _OK ){.      int
ee6c0 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20   pError = 0;.   
ee6d0 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56 61     memset(testVa
ee6e0 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 4e  lue, 0, CONCHLEN
ee6f0 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20 66  ); /* conch is f
ee700 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20 20  ixed size */.   
ee710 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65 74     rc = proxyGet
ee720 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75 65  HostID(testValue
ee730 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20 20  , &pError);.    
ee740 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d    if( (rc&0xff)=
ee750 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b  =SQLITE_IOERR ){
ee760 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
ee770 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72 72  lastErrno = pErr
ee780 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  or;.      }.    
ee790 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b    if( pCtx->lock
ee7a0 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20  ProxyPath ){.   
ee7b0 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 65       strlcpy(&te
ee7c0 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45  stValue[HOSTIDLE
ee7d0 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  N], pCtx->lockPr
ee7e0 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48  oxyPath, MAXPATH
ee7f0 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  LEN);.      }.  
ee800 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
ee810 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
ee820 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65     goto end_take
ee830 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20  conch;.    }.   
ee840 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20 75   .    readRc = u
ee850 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65 33  nixRead((sqlite3
ee860 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c  _file *)conchFil
ee870 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43  e, conchValue, C
ee880 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20  ONCHLEN, 0);.   
ee890 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c   if( readRc!=SQL
ee8a0 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
ee8b0 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69 66  READ ){.      if
ee8c0 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 45  ( readRc!=SQLITE
ee8d0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69  _OK ){.        i
ee8e0 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51  f( (rc&0xff)==SQ
ee8f0 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20  LITE_IOERR ){.  
ee900 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
ee910 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 68  astErrno = conch
ee920 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b  File->lastErrno;
ee930 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
ee940 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b 0a     rc = readRc;.
ee950 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
ee960 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20  _takeconch;.    
ee970 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66 20    }.      /* if 
ee980 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64 61  the conch has da
ee990 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63  ta compare the c
ee9a0 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  ontents */.     
ee9b0 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b   if( !pCtx->lock
ee9c0 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20  ProxyPath ){.   
ee9d0 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 6f       /* for auto
ee9e0 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f 63  -named local loc
ee9f0 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 65  k file, just che
eea00 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 61  ck the host ID a
eea10 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 20  nd we'll.       
eea20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f 63    ** use the loc
eea30 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74  al lock file pat
eea40 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64 79  h that's already
eea50 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20 20   in there */.   
eea60 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d 70       if( !memcmp
eea70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e 63  (testValue, conc
eea80 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c 45  hValue, HOSTIDLE
eea90 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  N) ){.          
eeaa0 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68 61  tLockPath = (cha
eeab0 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65 5b  r *)&conchValue[
eeac0 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 20  HOSTIDLEN];.    
eead0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74        goto end_t
eeae0 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20  akeconch;.      
eeaf0 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
eeb00 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27 76  .        /* we'v
eeb10 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68 20  e got the conch 
eeb20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d 61  if conchValue ma
eeb30 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20 61  tches our path a
eeb40 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20 20  nd host ID */.  
eeb50 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d        if( !memcm
eeb60 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e  p(testValue, con
eeb70 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45  chValue, CONCHLE
eeb80 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  N) ){.          
eeb90 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e  goto end_takecon
eeba0 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ch;.        }.  
eebb0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
eebc0 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f 72  .      /* a shor
eebd0 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65 27  t read means we'
eebe0 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74 68  re "creating" th
eebf0 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74 68  e conch (even th
eec00 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a 20  ough it could . 
eec10 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65 65       ** have bee
eec20 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e 74  n user-intervent
eec30 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71 75  ion), if we acqu
eec40 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69 76  ire the exclusiv
eec50 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a 2a  e lock,.      **
eec60 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d 61   we'll try to ma
eec70 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  tch the current 
eec80 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73 69  on-disk permissi
eec90 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  ons of the datab
eeca0 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ase.      */.   
eecb0 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 31     syncPerms = 1
eecc0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20  ;.    }.    .   
eecd0 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63 68   /* either conch
eece0 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64 69   was emtpy or di
eecf0 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20  dn't match */.  
eed00 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63    if( !pCtx->loc
eed10 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
eed20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b      proxyGetLock
eed30 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61 74  Path(pCtx->dbPat
eed40 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58  h, lockPath, MAX
eed50 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20  PATHLEN);.      
eed60 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63 6b  tLockPath = lock
eed70 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72 6c  Path;.      strl
eed80 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b 48  cpy(&testValue[H
eed90 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b 50  OSTIDLEN], lockP
eeda0 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
eedb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20  ;.    }.    .   
eedc0 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63 68   /* update conch
eedd0 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20 70   with host and p
eede0 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20 66  ath (this will f
eedf0 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72 6f  ail if other pro
eee00 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61 73  cess.     ** has
eee10 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61   a shared lock a
eee20 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20 72  lready) */.    r
eee30 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70  c = conchFile->p
eee40 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73  Method->xLock((s
eee50 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
eee60 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49 56  chFile, EXCLUSIV
eee70 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28  E_LOCK);.    if(
eee80 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
eee90 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69  {.      rc = uni
eeea0 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33 5f  xWrite((sqlite3_
eeeb0 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65  file *)conchFile
eeec0 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f 4e  , testValue, CON
eeed0 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 20  CHLEN, 0);.     
eeee0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
eeef0 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73 20  OK && syncPerms 
eef00 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63  ){.        struc
eef10 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 20  t stat buf;.    
eef20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66 73      int err = fs
eef30 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62  tat(pFile->h, &b
eef40 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  uf);.        if(
eef50 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20   err==0 ){.     
eef60 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20 6d       /* try to m
eef70 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61 73  atch the databas
eef80 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69 6f  e file permissio
eef90 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c 75  ns, ignore failu
eefa0 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  re */.#ifndef SQ
eefb0 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47  LITE_PROXY_DEBUG
eefc0 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d 6f  .          fchmo
eefd0 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20  d(conchFile->h, 
eefe0 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 65  buf.st_mode);.#e
eeff0 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69 66  lse.          if
ef000 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69  ( fchmod(conchFi
ef010 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f  le->h, buf.st_mo
ef020 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  de)!=0 ){.      
ef030 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d        int code =
ef040 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
ef050 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
ef060 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 46  rr, "fchmod %o F
ef070 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25 73  AILED with %d %s
ef080 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
ef090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef0a0 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 63    buf.st_mode, c
ef0b0 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f  ode, strerror(co
ef0c0 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  de));.          
ef0d0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20  } else {.       
ef0e0 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
ef0f0 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20  err, "fchmod %o 
ef100 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66 2e  SUCCEDED\n",buf.
ef110 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20 20  st_mode);.      
ef120 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
ef130 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69  lse{.          i
ef140 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b  nt code = errno;
ef150 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e  .          fprin
ef160 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41 54  tf(stderr, "STAT
ef170 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74 68   FAILED[%d] with
ef180 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20 20   %d %s\n", .    
ef190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef1a0 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65 2c        err, code,
ef1b0 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29 29   strerror(code))
ef1c0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
ef1d0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
ef1e0 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  .    conchFile->
ef1f0 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b  pMethod->xUnlock
ef200 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
ef210 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45  conchFile, SHARE
ef220 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 5f  D_LOCK);.  .end_
ef230 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20 4f  takeconch:.    O
ef240 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 52  STRACE2("TRANSPR
ef250 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c 6e  OXY: CLOSE  %d\n
ef260 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  ", pFile->h);.  
ef270 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
ef280 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f 70  _OK && pFile->op
ef290 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 20  enFlags ){.     
ef2a0 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30   if( pFile->h>=0
ef2b0 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49 43   ){.#ifdef STRIC
ef2c0 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 20  T_CLOSE_ERROR.  
ef2d0 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28        if( close(
ef2e0 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20  pFile->h) ){.   
ef2f0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
ef300 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
ef310 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
ef320 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  n SQLITE_IOERR_C
ef330 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  LOSE;.        }.
ef340 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63 6c  #else.        cl
ef350 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20 2f  ose(pFile->h); /
ef360 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20  * silently leak 
ef370 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23 65  fd if fail */.#e
ef380 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20  ndif.      }.   
ef390 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31     pFile->h = -1
ef3a0 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20 3d  ;.      int fd =
ef3b0 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50 61   open(pCtx->dbPa
ef3c0 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46  th, pFile->openF
ef3d0 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lags,.          
ef3e0 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
ef3f0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45  _DEFAULT_FILE_PE
ef400 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20  RMISSIONS);.    
ef410 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e    OSTRACE2("TRAN
ef420 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25 64  SPROXY: OPEN  %d
ef430 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20 20  \n", fd);.      
ef440 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20  if( fd>=0 ){.   
ef450 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20       pFile->h = 
ef460 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  fd;.      }else{
ef470 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c 49  .        rc=SQLI
ef480 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a 20  TE_CANTOPEN; /* 
ef490 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72 6f  SQLITE_BUSY? pro
ef4a0 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c 6c  xyTakeConch call
ef4b0 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ed.             
ef4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef4d0 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67    during locking
ef4e0 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   */.      }.    
ef4f0 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
ef500 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74 78  LITE_OK && !pCtx
ef510 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20  ->lockProxy ){. 
ef520 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68 20       char *path 
ef530 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74 4c  = tLockPath ? tL
ef540 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d 3e  ockPath : pCtx->
ef550 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20  lockProxyPath;. 
ef560 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65 65       /* ACS: Nee
ef570 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79  d to make a copy
ef580 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69 6d   of path sometim
ef590 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d  es */.      rc =
ef5a0 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78   proxyCreateUnix
ef5b0 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74 78  File(path, &pCtx
ef5c0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20  ->lockProxy);.  
ef5d0 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
ef5e0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
ef5f0 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65     pCtx->conchHe
ef600 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69  ld = 1;..      i
ef610 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b 0a  f( tLockPath ){.
ef620 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f          pCtx->lo
ef630 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71  ckProxyPath = sq
ef640 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
ef650 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 20   tLockPath);.   
ef660 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c       if( pCtx->l
ef670 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f  ockProxy->pMetho
ef680 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f  d == &afpIoMetho
ef690 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ds ){.          
ef6a0 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  ((afpLockingCont
ef6b0 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 6b  ext *)pCtx->lock
ef6c0 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  Proxy->lockingCo
ef6d0 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20 3d  ntext)->dbPath =
ef6e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ef6f0 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b        pCtx->lock
ef700 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20  ProxyPath;.     
ef710 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
ef720 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
ef730 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68  conchFile->pMeth
ef740 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c  od->xUnlock((sql
ef750 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68  ite3_file*)conch
ef760 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  File, NO_LOCK);.
ef770 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43      }.    OSTRAC
ef780 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 25  E3("TAKECONCH  %
ef790 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69  d %s\n", conchFi
ef7a0 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 54  le->h, rc==SQLIT
ef7b0 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c 65  E_OK?"ok":"faile
ef7c0 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  d");.    return 
ef7d0 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  rc;.  }.}../*.**
ef7e0 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73 20   If pFile holds 
ef7f0 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e 63  a lock on a conc
ef800 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65 6c  h file, then rel
ef810 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e 0a  ease that lock..
ef820 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
ef830 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 28  oxyReleaseConch(
ef840 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29  unixFile *pFile)
ef850 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
ef860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef870 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72 65  /* Subroutine re
ef880 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 70  turn code */.  p
ef890 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
ef8a0 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54 68  xt *pCtx;  /* Th
ef8b0 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78  e locking contex
ef8c0 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 20  t for the proxy 
ef8d0 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 69  lock */.  unixFi
ef8e0 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20  le *conchFile;  
ef8f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
ef900 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
ef910 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70 72  */..  pCtx = (pr
ef920 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
ef930 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
ef940 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f 6e  ngContext;.  con
ef950 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63  chFile = pCtx->c
ef960 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54 52  onchFile;.  OSTR
ef970 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f 4e  ACE4("RELEASECON
ef980 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 69  CH  %d for %s pi
ef990 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69  d=%d\n", conchFi
ef9a0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
ef9b0 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f    (pCtx->lockPro
ef9c0 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c  xyPath ? pCtx->l
ef9d0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22  ockProxyPath : "
ef9e0 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20 20  :auto:"), .     
ef9f0 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 3b        getpid());
efa00 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65  .  pCtx->conchHe
efa10 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 63  ld = 0;.  rc = c
efa20 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  onchFile->pMetho
efa30 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69  d->xUnlock((sqli
efa40 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46  te3_file*)conchF
efa50 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  ile, NO_LOCK);. 
efa60 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45 41   OSTRACE3("RELEA
efa70 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e  SECONCH  %d %s\n
efa80 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  ", conchFile->h,
efa90 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 3d  .           (rc=
efaa0 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b  =SQLITE_OK ? "ok
efab0 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a  " : "failed"));.
efac0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
efad0 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20  /*.** Given the 
efae0 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61  name of a databa
efaf0 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74 65  se file, compute
efb00 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74 73   the name of its
efb10 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a 20   conch file..** 
efb20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68 20  Store the conch 
efb30 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d 6f  filename in memo
efb40 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
efb50 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
efb60 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e  )..** Make *pCon
efb70 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f 20  chPath point to 
efb80 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20 52  the new name.  R
efb90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
efba0 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  on success.** or
efbb0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
efbc0 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69   unable to obtai
efbd0 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  n memory..**.** 
efbe0 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65  The caller is re
efbf0 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e  sponsible for en
efc00 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 20  suring that the 
efc10 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79  allocated memory
efc20 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76 65  .** space is eve
efc30 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a  ntually freed..*
efc40 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74 68  *.** *pConchPath
efc50 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20   is set to NULL 
efc60 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  if a memory allo
efc70 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
efc80 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  urs..*/.static i
efc90 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43 6f  nt proxyCreateCo
efca0 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61 72  nchPathname(char
efcb0 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a   *dbPath, char *
efcc0 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20 20  *pConchPath){.  
efcd0 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
efce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
efcf0 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
efd00 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69 6e  .  int len = (in
efd10 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 29  t)strlen(dbPath)
efd20 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 64  ; /* Length of d
efd30 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
efd40 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20 63   - dbPath */.  c
efd50 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b 20  har *conchPath; 
efd60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
efd70 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 20  buffer in which 
efd80 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f 6e  to construct con
efd90 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f 2a  ch name */..  /*
efda0 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   Allocate space 
efdb0 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66 69  for the conch fi
efdc0 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74 69  lename and initi
efdd0 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20 74  alize the name t
efde0 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20  o.  ** the name 
efdf0 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  of the original 
efe00 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a  database file. *
efe10 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61 74  /  .  *pConchPat
efe20 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d 20  h = conchPath = 
efe30 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
efe40 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 3b  malloc(len + 8);
efe50 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 68  .  if( conchPath
efe60 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
efe70 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
efe80 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f 6e    }.  memcpy(con
efe90 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c 20  chPath, dbPath, 
efea0 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a 20  len+1);.  .  /* 
efeb0 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e 22  now insert a "."
efec0 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74   before the last
efed0 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a   / character */.
efee0 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 29    for( i=(len-1)
efef0 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a 20  ; i>=0; i-- ){. 
eff00 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 68     if( conchPath
eff10 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20  [i]=='/' ){.    
eff20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72 65    i++;.      bre
eff30 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
eff40 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e 27  conchPath[i]='.'
eff50 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c 65  ;.  while ( i<le
eff60 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50 61  n ){.    conchPa
eff70 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b 69  th[i+1]=dbPath[i
eff80 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a  ];.    i++;.  }.
eff90 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68 65  .  /* append the
effa0 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69 78   "-conch" suffix
effb0 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a   to the file */.
effc0 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68 50    memcpy(&conchP
effd0 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63  ath[i+1], "-conc
effe0 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72 74  h", 7);.  assert
efff0 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63 6f  ( (int)strlen(co
f0000 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e 2b  nchPath) == len+
f0010 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  7 );..  return S
f0020 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
f0030 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20 63   Takes a fully c
f0040 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79 20  onfigured proxy 
f0050 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75 6e  locking-style un
f0060 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69 74  ix file and swit
f0070 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 61  ches.** the loca
f0080 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68  l lock file path
f0090 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
f00a0 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50  switchLockProxyP
f00b0 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  ath(unixFile *pF
f00c0 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ile, const char 
f00d0 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79  *path) {.  proxy
f00e0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
f00f0 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63  pCtx = (proxyLoc
f0100 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69  kingContext*)pFi
f0110 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f0120 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64 50  xt;.  char *oldP
f0130 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b  ath = pCtx->lock
f0140 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e 74  ProxyPath;.  int
f0150 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
f0160 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ..  if( pFile->l
f0170 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
f0180 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
f0190 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 20  QLITE_BUSY;.  } 
f01a0 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20   ..  /* nothing 
f01b0 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61 74  to do if the pat
f01c0 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 6f  h is NULL, :auto
f01d0 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68 65  : or matches the
f01e0 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20 2a   existing path *
f01f0 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c  /.  if( !path ||
f0200 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c   path[0]=='\0' |
f0210 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20  | !strcmp(path, 
f0220 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 20  ":auto:") ||.   
f0230 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73 74   (oldPath && !st
f0240 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20 70  rncmp(oldPath, p
f0250 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
f0260 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
f0270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
f0280 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65  se{.    unixFile
f0290 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43   *lockProxy = pC
f02a0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20  tx->lockProxy;. 
f02b0 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f     pCtx->lockPro
f02c0 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 74  xy=NULL;.    pCt
f02d0 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30  x->conchHeld = 0
f02e0 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 72  ;.    if( lockPr
f02f0 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  oxy!=NULL ){.   
f0300 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d     rc=lockProxy-
f0310 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65  >pMethod->xClose
f0320 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ((sqlite3_file *
f0330 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20  )lockProxy);.   
f0340 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
f0350 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c  rn rc;.      sql
f0360 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72  ite3_free(lockPr
f0370 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oxy);.    }.    
f0380 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c 64  sqlite3_free(old
f0390 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 2d  Path);.    pCtx-
f03a0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d  >lockProxyPath =
f03b0 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
f03c0 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a 20  (0, path);.  }. 
f03d0 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d   .  return rc;.}
f03e0 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 73  ../*.** pFile is
f03f0 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 73   a file that has
f0400 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20   been opened by 
f0410 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63 61  a prior xOpen ca
f0420 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20 69  ll.  dbPath.** i
f0430 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65  s a string buffe
f0440 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50 41  r at least MAXPA
f0450 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74 65  THLEN+1 characte
f0460 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a  rs in size..**.*
f0470 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66  * This routine f
f0480 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  ind the filename
f0490 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
f04a0 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74 65   pFile and write
f04b0 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50 61  s it.** int dbPa
f04c0 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th..*/.static in
f04d0 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74 68  t proxyGetDbPath
f04e0 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69 78  ForUnixFile(unix
f04f0 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68 61  File *pFile, cha
f0500 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66 20  r *dbPath){.#if 
f0510 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
f0520 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  _).  if( pFile->
f0530 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49  pMethod == &afpI
f0540 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
f0550 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65 65  /* afp style kee
f0560 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  ps a reference t
f0570 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69 6e  o the db path in
f0580 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66 69   the filePath fi
f0590 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74  eld .    ** of t
f05a0 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 20  he struct */.   
f05b0 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 74   assert( (int)st
f05c0 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c  rlen((char*)pFil
f05d0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f05e0 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29  t)<=MAXPATHLEN )
f05f0 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50  ;.    strcpy(dbP
f0600 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 6e  ath, ((afpLockin
f0610 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65  gContext *)pFile
f0620 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f0630 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d 65  )->dbPath);.  }e
f0640 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  lse.#endif.  if(
f0650 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20   pFile->pMethod 
f0660 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74  == &dotlockIoMet
f0670 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 64  hods ){.    /* d
f0680 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75 73  ot lock style us
f0690 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63  es the locking c
f06a0 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20  ontext to store 
f06b0 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 20  the dot lock.   
f06c0 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a 2f   ** file path */
f06d0 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 73  .    int len = s
f06e0 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70 46  trlen((char *)pF
f06f0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
f0700 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44 4f  ext) - strlen(DO
f0710 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a 20  TLOCK_SUFFIX);. 
f0720 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74 68     memcpy(dbPath
f0730 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d  , (char *)pFile-
f0740 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c  >lockingContext,
f0750 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 6c   len + 1);.  }el
f0760 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6f  se{.    /* all o
f0770 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65 20  ther styles use 
f0780 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  the locking cont
f0790 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ext to store the
f07a0 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a 2f   db file path */
f07b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 72  .    assert( str
f07c0 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c 65  len((char*)pFile
f07d0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f07e0 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b  )<=MAXPATHLEN );
f07f0 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 61  .    strcpy(dbPa
f0800 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c  th, (char *)pFil
f0810 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f0820 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
f0830 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
f0840 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61 6c  *.** Takes an al
f0850 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e 20  ready filled in 
f0860 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61 6c  unix file and al
f0870 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20 66  ters it so all f
f0880 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20  ile locking .** 
f0890 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d 65  will be performe
f08a0 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70  d on the local p
f08b0 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e 20  roxy lock file. 
f08c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
f08d0 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72 65  ields.** are pre
f08e0 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c 6f  served in the lo
f08f0 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73 6f  cking context so
f0900 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 62   that they can b
f0910 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 0a  e restored and .
f0920 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72 75  ** the unix stru
f0930 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20 63  cture properly c
f0940 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c 6f  leaned up at clo
f0950 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e 6c  se time:.**  ->l
f0960 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a 2a  ockingContext.**
f0970 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a 73    ->pMethod.*/.s
f0980 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54  tatic int proxyT
f0990 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65  ransformUnixFile
f09a0 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
f09b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61  , const char *pa
f09c0 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63  th) {.  proxyLoc
f09d0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74  kingContext *pCt
f09e0 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74 68  x;.  char dbPath
f09f0 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b 20  [MAXPATHLEN+1]; 
f0a00 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
f0a10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
f0a20 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c 6f  le */.  char *lo
f0a30 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 69  ckPath=NULL;.  i
f0a40 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
f0a50 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69 6c  K;.  .  if( pFil
f0a60 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f  e->locktype!=NO_
f0a70 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  LOCK ){.    retu
f0a80 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
f0a90 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44 62    }.  proxyGetDb
f0aa0 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 28  PathForUnixFile(
f0ab0 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b 0a  pFile, dbPath);.
f0ac0 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 70    if( !path || p
f0ad0 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20  ath[0]=='\0' || 
f0ae0 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 3a  !strcmp(path, ":
f0af0 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20 6c  auto:") ){.    l
f0b00 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20  ockPath=NULL;.  
f0b10 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 50  }else{.    lockP
f0b20 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74 68  ath=(char *)path
f0b30 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41  ;.  }.  .  OSTRA
f0b40 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59 20  CE4("TRANSPROXY 
f0b50 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25   %d for %s pid=%
f0b60 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
f0b70 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63 6b             (lock
f0b80 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68 20  Path ? lockPath 
f0b90 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74  : ":auto:"), get
f0ba0 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78 20  pid());..  pCtx 
f0bb0 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
f0bc0 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20  ( sizeof(*pCtx) 
f0bd0 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d 30  );.  if( pCtx==0
f0be0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
f0bf0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
f0c00 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c 20  .  memset(pCtx, 
f0c10 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29  0, sizeof(*pCtx)
f0c20 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78 79  );..  rc = proxy
f0c30 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 6e  CreateConchPathn
f0c40 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43 74  ame(dbPath, &pCt
f0c50 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68  x->conchFilePath
f0c60 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
f0c70 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
f0c80 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e   = proxyCreateUn
f0c90 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f 6e  ixFile(pCtx->con
f0ca0 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43 74  chFilePath, &pCt
f0cb0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20  x->conchFile);. 
f0cc0 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53   }  .  if( rc==S
f0cd0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b  QLITE_OK && lock
f0ce0 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74 78  Path ){.    pCtx
f0cf0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
f0d00 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
f0d10 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b 0a  p(0, lockPath);.
f0d20 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53    }..  if( rc==S
f0d30 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f0d40 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 73  /* all memory is
f0d50 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f 78   allocated, prox
f0d60 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 61  ys are created a
f0d70 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 20  nd assigned, .  
f0d80 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 20    ** switch the 
f0d90 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20  locking context 
f0da0 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 6e  and pMethod then
f0db0 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f 0a   return..    */.
f0dc0 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 68      pCtx->dbPath
f0dd0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
f0de0 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a 20  up(0, dbPath);. 
f0df0 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b     pCtx->oldLock
f0e00 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 69  ingContext = pFi
f0e10 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f0e20 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  xt;.    pFile->l
f0e30 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
f0e40 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d 3e  pCtx;.    pCtx->
f0e50 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46 69  pOldMethod = pFi
f0e60 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 20  le->pMethod;.   
f0e70 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20   pFile->pMethod 
f0e80 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64  = &proxyIoMethod
f0e90 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  s;.  }else{.    
f0ea0 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46  if( pCtx->conchF
f0eb0 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72 63  ile ){ .      rc
f0ec0 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69   = pCtx->conchFi
f0ed0 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c  le->pMethod->xCl
f0ee0 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ose((sqlite3_fil
f0ef0 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68 46  e *)pCtx->conchF
f0f00 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ile);.      if( 
f0f10 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
f0f20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
f0f30 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69  ee(pCtx->conchFi
f0f40 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  le);.    }.    s
f0f50 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78  qlite3_free(pCtx
f0f60 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29  ->conchFilePath)
f0f70 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ; .    sqlite3_f
f0f80 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20  ree(pCtx);.  }. 
f0f90 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e 53   OSTRACE3("TRANS
f0fa0 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22 2c  PROXY  %d %s\n",
f0fb0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
f0fc0 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 54        (rc==SQLIT
f0fd0 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66  E_OK ? "ok" : "f
f0fe0 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75  ailed"));.  retu
f0ff0 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
f1000 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61   This routine ha
f1010 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 69  ndles sqlite3_fi
f1020 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 6c  le_control() cal
f1030 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65 63  ls that are spec
f1040 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 79  ific.** to proxy
f1050 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61   locking..*/.sta
f1060 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c  tic int proxyFil
f1070 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  eControl(sqlite3
f1080 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f  _file *id, int o
f1090 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  p, void *pArg){.
f10a0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
f10b0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
f10c0 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  GET_LOCKPROXYFIL
f10d0 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46  E: {.      unixF
f10e0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
f10f0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20  ixFile*)id;.    
f1100 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65    if( pFile->pMe
f1110 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f  thod == &proxyIo
f1120 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20  Methods ){.     
f1130 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43     proxyLockingC
f1140 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28  ontext *pCtx = (
f1150 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f1160 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ext*)pFile->lock
f1170 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
f1180 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e      proxyTakeCon
f1190 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20  ch(pFile);.     
f11a0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63     if( pCtx->loc
f11b0 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
f11c0 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20          *(const 
f11d0 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 70  char **)pArg = p
f11e0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
f11f0 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  th;.        }els
f1200 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63  e{.          *(c
f1210 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72  onst char **)pAr
f1220 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74  g = ":auto: (not
f1230 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 20   held)";.       
f1240 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20   }.      } else 
f1250 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73  {.        *(cons
f1260 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d  t char **)pArg =
f1270 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20   NULL;.      }. 
f1280 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
f1290 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  TE_OK;.    }.   
f12a0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54   case SQLITE_SET
f12b0 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20  _LOCKPROXYFILE: 
f12c0 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c 65  {.      unixFile
f12d0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
f12e0 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 69  ile*)id;.      i
f12f0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
f1300 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 50  K;.      int isP
f1310 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46 69  roxyStyle = (pFi
f1320 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26  le->pMethod == &
f1330 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 3b  proxyIoMethods);
f1340 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67 3d  .      if( pArg=
f1350 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 20  =NULL || (const 
f1360 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 29  char *)pArg==0 )
f1370 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73  {.        if( is
f1380 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 20  ProxyStyle ){.  
f1390 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20          /* turn 
f13a0 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  off proxy lockin
f13b0 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 65  g - not supporte
f13c0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72  d */.          r
f13d0 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
f13e0 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43   /*SQLITE_PROTOC
f13f0 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  OL? SQLITE_MISUS
f1400 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d 65  E?*/;.        }e
f1410 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
f1420 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79  * turn off proxy
f1430 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61   locking - alrea
f1440 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f  dy off - NOOP */
f1450 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
f1460 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
f1470 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
f1480 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  {.        const 
f1490 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68 20  char *proxyPath 
f14a0 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
f14b0 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69 66  pArg;.        if
f14c0 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29  ( isProxyStyle )
f14d0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f 78  {.          prox
f14e0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f14f0 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20 20  *pCtx = .       
f1500 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b 69       (proxyLocki
f1510 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65  ngContext*)pFile
f1520 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f1530 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
f1540 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22 3a  !strcmp(pArg, ":
f1550 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 20  auto:") .       
f1560 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c 6f      || (pCtx->lo
f1570 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a 20  ckProxyPath &&. 
f1580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 73                !s
f1590 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f 63  trncmp(pCtx->loc
f15a0 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f 78  kProxyPath, prox
f15b0 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45  yPath, MAXPATHLE
f15c0 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b  N)).          ){
f15d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
f15e0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
f15f0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
f1600 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
f1610 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61  witchLockProxyPa
f1620 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50  th(pFile, proxyP
f1630 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ath);.          
f1640 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
f1650 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75  .          /* tu
f1660 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65  rn on proxy file
f1670 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20   locking */.    
f1680 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
f1690 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c  TransformUnixFil
f16a0 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61  e(pFile, proxyPa
f16b0 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  th);.        }. 
f16c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
f16d0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
f16e0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
f16f0 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 20     assert( 0 ); 
f1700 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 73   /* The call ass
f1710 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 76  ures that only v
f1720 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72 65  alid opcodes are
f1730 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a 20   sent */.    }. 
f1740 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45   }.  /*NOTREACHE
f1750 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  D*/.  return SQL
f1760 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a  ITE_ERROR;.}../*
f1770 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20  .** Within this 
f1780 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70 72  division (the pr
f1790 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69  oxying locking i
f17a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74  mplementation) t
f17b0 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a  he procedures.**
f17c0 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e   above this poin
f17d0 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 74  t are all utilit
f17e0 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72  ies.  The lock-r
f17f0 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20 6f  elated methods o
f1800 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d 6c  f the.** proxy-l
f1810 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  ocking sqlite3_i
f1820 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 20  o_method object 
f1830 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a  follow..*/.../*.
f1840 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
f1850 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20  checks if there 
f1860 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  is a RESERVED lo
f1870 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73  ck held on the s
f1880 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65  pecified.** file
f1890 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20   by this or any 
f18a0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49  other process. I
f18b0 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73  f such a lock is
f18c0 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73   held, set *pRes
f18d0 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d  Out.** to a non-
f18e0 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72  zero value other
f18f0 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73  wise *pResOut is
f1900 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54   set to zero.  T
f1910 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
f1920 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c  ** is set to SQL
f1930 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e  ITE_OK unless an
f1940 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72   I/O error occur
f1950 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68  s during lock ch
f1960 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  ecking..*/.stati
f1970 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b  c int proxyCheck
f1980 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
f1990 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
f19a0 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20  nt *pResOut) {. 
f19b0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
f19c0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
f19d0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f  ;.  int rc = pro
f19e0 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c  xyTakeConch(pFil
f19f0 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
f1a00 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
f1a10 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
f1a20 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
f1a30 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f1a40 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
f1a50 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69  Context;.    uni
f1a60 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70  xFile *proxy = p
f1a70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
f1a80 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79      return proxy
f1a90 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63  ->pMethod->xChec
f1aa0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 73  kReservedLock((s
f1ab0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f  qlite3_file*)pro
f1ac0 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 20  xy, pResOut);.  
f1ad0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
f1ae0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65  ../*.** Lock the
f1af0 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c   file with the l
f1b00 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79  ock specified by
f1b10 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74   parameter lockt
f1b20 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20  ype - one.** of 
f1b30 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
f1b40 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41  *.**     (1) SHA
f1b50 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  RED_LOCK.**     
f1b60 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  (2) RESERVED_LOC
f1b70 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e  K.**     (3) PEN
f1b80 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  DING_LOCK.**    
f1b90 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c   (4) EXCLUSIVE_L
f1ba0 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  OCK.**.** Someti
f1bb0 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74  mes when request
f1bc0 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61  ing one lock sta
f1bd0 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  te, additional l
f1be0 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72  ock states.** ar
f1bf0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65  e inserted in be
f1c00 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b  tween.  The lock
f1c10 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f  ing might fail o
f1c20 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74  n one of the lat
f1c30 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  er.** transition
f1c40 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f  s leaving the lo
f1c50 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65  ck state differe
f1c60 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20  nt from what it 
f1c70 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73  started but.** s
f1c80 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74  till short of it
f1c90 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c  s goal.  The fol
f1ca0 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f  lowing chart sho
f1cb0 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a  ws the allowed.*
f1cc0 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e  * transitions an
f1cd0 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69  d the inserted i
f1ce0 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74  ntermediate stat
f1cf0 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c  es:.**.**    UNL
f1d00 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a  OCKED -> SHARED.
f1d10 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
f1d20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53  RESERVED.**    S
f1d30 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  HARED -> (PENDIN
f1d40 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
f1d50 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d  **    RESERVED -
f1d60 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
f1d70 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50  XCLUSIVE.**    P
f1d80 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53  ENDING -> EXCLUS
f1d90 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  IVE.**.** This r
f1da0 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79  outine will only
f1db0 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b   increase a lock
f1dc0 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74  .  Use the sqlit
f1dd0 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20  e3OsUnlock().** 
f1de0 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72  routine to lower
f1df0 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c   a locking level
f1e00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f1e10 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65  proxyLock(sqlite
f1e20 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
f1e30 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e  locktype) {.  un
f1e40 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
f1e50 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
f1e60 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54   int rc = proxyT
f1e70 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b  akeConch(pFile);
f1e80 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
f1e90 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78  E_OK ){.    prox
f1ea0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f1eb0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
f1ec0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70  ckingContext *)p
f1ed0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
f1ee0 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69  text;.    unixFi
f1ef0 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78  le *proxy = pCtx
f1f00 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20  ->lockProxy;.   
f1f10 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65   rc = proxy->pMe
f1f20 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c  thod->xLock((sql
f1f30 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79  ite3_file*)proxy
f1f40 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20  , locktype);.   
f1f50 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
f1f60 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79   = proxy->lockty
f1f70 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  pe;.  }.  return
f1f80 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c   rc;.}.../*.** L
f1f90 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
f1fa0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
f1fb0 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
f1fc0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
f1fd0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
f1fe0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
f1ff0 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
f2000 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
f2010 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
f2020 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
f2030 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
f2040 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
f2050 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
f2060 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
f2070 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
f2080 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
f2090 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c   proxyUnlock(sql
f20a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
f20b0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
f20c0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
f20d0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
f20e0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f  ;.  int rc = pro
f20f0 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c  xyTakeConch(pFil
f2100 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
f2110 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
f2120 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
f2130 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
f2140 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f2150 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
f2160 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69  Context;.    uni
f2170 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70  xFile *proxy = p
f2180 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
f2190 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e      rc = proxy->
f21a0 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b  pMethod->xUnlock
f21b0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
f21c0 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29  proxy, locktype)
f21d0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ;.    pFile->loc
f21e0 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c  ktype = proxy->l
f21f0 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72  ocktype;.  }.  r
f2200 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
f2210 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20  ** Close a file 
f2220 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 20  that uses proxy 
f2230 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  locks..*/.static
f2240 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28   int proxyClose(
f2250 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
f2260 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a  ) {.  if( id ){.
f2270 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46      unixFile *pF
f2280 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
f2290 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c 6f  )id;.    proxyLo
f22a0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
f22b0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
f22c0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
f22d0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f22e0 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  t;.    unixFile 
f22f0 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74  *lockProxy = pCt
f2300 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20  x->lockProxy;.  
f2310 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63    unixFile *conc
f2320 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f  hFile = pCtx->co
f2330 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74  nchFile;.    int
f2340 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
f2350 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c 6f  .    .    if( lo
f2360 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20  ckProxy ){.     
f2370 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d   rc = lockProxy-
f2380 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
f2390 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
f23a0 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f 4c  )lockProxy, NO_L
f23b0 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
f23c0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
f23d0 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50        rc = lockP
f23e0 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  roxy->pMethod->x
f23f0 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66  Close((sqlite3_f
f2400 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b  ile*)lockProxy);
f2410 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
f2420 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
f2430 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f   sqlite3_free(lo
f2440 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20  ckProxy);.      
f2450 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20  pCtx->lockProxy 
f2460 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
f2470 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0a  f( conchFile ){.
f2480 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e        if( pCtx->
f2490 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 20  conchHeld ){.   
f24a0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 52       rc = proxyR
f24b0 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 6c  eleaseConch(pFil
f24c0 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
f24d0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
f24e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
f24f0 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d   = conchFile->pM
f2500 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73  ethod->xClose((s
f2510 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
f2520 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69  chFile);.      i
f2530 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
f2540 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
f2550 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 29  _free(conchFile)
f2560 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
f2570 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 6c  te3_free(pCtx->l
f2580 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a 20  ockProxyPath);. 
f2590 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
f25a0 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50  pCtx->conchFileP
f25b0 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ath);.    sqlite
f25c0 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62 50  3_free(pCtx->dbP
f25d0 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65 73  ath);.    /* res
f25e0 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61  tore the origina
f25f0 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78  l locking contex
f2600 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68  t and pMethod th
f2610 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a 20  en close it */. 
f2620 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e     pFile->lockin
f2630 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 2d  gContext = pCtx-
f2640 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  >oldLockingConte
f2650 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70  xt;.    pFile->p
f2660 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e 70  Method = pCtx->p
f2670 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20 73  OldMethod;.    s
f2680 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78  qlite3_free(pCtx
f2690 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 46  );.    return pF
f26a0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43  ile->pMethod->xC
f26b0 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20  lose(id);.  }.  
f26c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f26d0 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a  ;.}....#endif /*
f26e0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
f26f0 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e  __) && SQLITE_EN
f2700 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
f2710 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20  LE */./*.** The 
f2720 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73 74  proxy locking st
f2730 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  yle is intended 
f2740 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46 50  for use with AFP
f2750 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a 2a   filesystems..**
f2760 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20 69   And since AFP i
f2770 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64  s only supported
f2780 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65 20   on MacOSX, the 
f2790 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73  proxy locking is
f27a0 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69 63   also.** restric
f27b0 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a 2a  ted to MacOSX..*
f27c0 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  * .**.**********
f27d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
f27e0 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20   the proxy lock 
f27f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
f2800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2810 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
f2820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2860 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  ****/../*.** Ini
f2870 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72  tialize the oper
f2880 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74  ating system int
f2890 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  erface..**.** Th
f28a0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 73  is routine regis
f28b0 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d 70  ters all VFS imp
f28c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72  lementations for
f28d0 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61   unix-like opera
f28e0 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73 2e  ting.** systems.
f28f0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c 20    This routine, 
f2900 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f  and the sqlite3_
f2910 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e 65  os_end() routine
f2920 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a 2a   that follows,.*
f2930 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20  * should be the 
f2940 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69 6e  only routines in
f2950 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74 20   this file that 
f2960 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f 6d  are visible from
f2970 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73 2e   other.** files.
f2980 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
f2990 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  ine is called on
f29a0 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65  ce during SQLite
f29b0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
f29c0 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e 67  and by a.** sing
f29d0 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  le thread.  The 
f29e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
f29f0 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 73  n and mutex subs
f2a00 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74 0a  ystems have not.
f2a10 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20 62  ** necessarily b
f2a20 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  een initialized 
f2a30 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
f2a40 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e 64  e is called, and
f2a50 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75   so they.** shou
f2a60 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 0a  ld not be used..
f2a70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
f2a80 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  t sqlite3_os_ini
f2a90 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 0a  t(void){ .  /* .
f2aa0 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    ** The followi
f2ab0 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 73  ng macro defines
f2ac0 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72 20   an initializer 
f2ad0 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f 76  for an sqlite3_v
f2ae0 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20  fs object..  ** 
f2af0 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
f2b00 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54 68  VFS is NAME.  Th
f2b10 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61 20  e pAppData is a 
f2b20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f 69  pointer to a poi
f2b30 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 65  nter.  ** to the
f2b40 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69   "finder" functi
f2b50 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20 69  on.  (pAppData i
f2b60 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
f2b70 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73 65   pointer because
f2b80 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30 20  .  ** silly C90 
f2b90 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20 61  rules prohibit a
f2ba0 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69 6e   void* from bein
f2bb0 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e 63  g cast to a func
f2bc0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20 2a  tion pointer.  *
f2bd0 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76 65  * and so we have
f2be0 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 74   to go through t
f2bf0 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  he intermediate 
f2c00 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69 64  pointer to avoid
f2c10 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 77   problems.  ** w
f2c20 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69  hen compiling wi
f2c30 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72  th -pedantic-err
f2c40 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20 2a  ors on GCC.).  *
f2c50 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44 45  *.  ** The FINDE
f2c60 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  R parameter to t
f2c70 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68 65  his macro is the
f2c80 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f 69   name of the poi
f2c90 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a  nter to the.  **
f2ca0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
f2cb0 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66 75  .  The finder-fu
f2cc0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61  nction returns a
f2cd0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a   pointer to the.
f2ce0 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f 6d    ** sqlite_io_m
f2cf0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74 68  ethods object th
f2d00 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
f2d10 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69 6e  e desired lockin
f2d20 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 73  g.  ** behaviors
f2d30 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69 73  .  See the divis
f2d40 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20 63  ion above that c
f2d50 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d 45  ontains the IOME
f2d60 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72 6f  THODS.  ** macro
f2d70 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69 6e   for addition in
f2d80 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 6e  formation on fin
f2d90 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a 20  der-functions.. 
f2da0 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 69   **.  ** Most fi
f2db0 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65 74  nders simply ret
f2dc0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
f2dd0 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 33   a fixed sqlite3
f2de0 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 2a  _io_methods.  **
f2df0 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74 68   object.  But th
f2e00 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e  e "autolockIoFin
f2e10 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20 6f  der" available o
f2e20 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61 20  n MacOSX does a 
f2e30 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 65  little.  ** more
f2e40 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20 6c   than that; it l
f2e50 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65  ooks at the file
f2e60 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61 74  system type that
f2e70 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a 2a   hosts the .  **
f2e80 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
f2e90 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f 6f  nd tries to choo
f2ea0 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d 65  se an locking me
f2eb0 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74 65  thod appropriate
f2ec0 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20 66   for.  ** that f
f2ed0 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e 0a  ilesystem time..
f2ee0 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 55    */.  #define U
f2ef0 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c 20  NIXVFS(VFSNAME, 
f2f00 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20 20  FINDER) {       
f2f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2f20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   \.    1,       
f2f30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f2f40 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20  iVersion */     
f2f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f2f60 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 78  .    sizeof(unix
f2f70 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a  File),     /* sz
f2f80 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20 20  OsFile */       
f2f90 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f2fa0 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c     MAX_PATHNAME,
f2fb0 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61           /* mxPa
f2fc0 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20  thname */       
f2fd0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f2fe0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
f2ff0 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20         /* pNext 
f3000 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f3010 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 56           \.    V
f3020 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20  FSNAME,         
f3030 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f       /* zName */
f3040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3050 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76 6f         \.    (vo
f3060 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20 20  id*)&FINDER,    
f3070 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a     /* pAppData *
f3080 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f3090 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 4f       \.    unixO
f30a0 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  pen,            
f30b0 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20   /* xOpen */    
f30c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f30d0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65 6c     \.    unixDel
f30e0 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ete,           /
f30f0 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20 20  * xDelete */    
f3100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3110 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65 73   \.    unixAcces
f3120 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
f3130 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20 20  xAccess */      
f3140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f3150 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61 74  .    unixFullPat
f3160 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46  hname,     /* xF
f3170 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20  ullPathname */  
f3180 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f3190 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 20     unixDlOpen,  
f31a0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f           /* xDlO
f31b0 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20  pen */          
f31c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f31d0 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20 20   unixDlError,   
f31e0 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72         /* xDlErr
f31f0 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  or */           
f3200 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
f3210 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20 20  nixDlSym,       
f3220 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a       /* xDlSym *
f3230 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f3240 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
f3250 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  xDlClose,       
f3260 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a     /* xDlClose *
f3270 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f3280 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 52       \.    unixR
f3290 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20  andomness,      
f32a0 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20   /* xRandomness 
f32b0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f32c0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c 65     \.    unixSle
f32d0 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ep,            /
f32e0 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20 20  * xSleep */     
f32f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3300 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72 65   \.    unixCurre
f3310 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20  ntTime,      /* 
f3320 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 20  xCurrentTime */ 
f3330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f3340 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73 74  .    unixGetLast
f3350 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78 47  Error      /* xG
f3360 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20 20  etLastError */  
f3370 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f3380 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c   }..  /*.  ** Al
f3390 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 20  l default VFSes 
f33a0 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f 6e  for unix are con
f33b0 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 6f  tained in the fo
f33c0 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a 20  llowing array.. 
f33d0 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68   **.  ** Note th
f33e0 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  at the sqlite3_v
f33f0 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 6f  fs.pNext field o
f3400 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74  f the VFS object
f3410 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 2a   is modified.  *
f3420 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20  * by the SQLite 
f3430 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 46  core when the VF
f3440 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e  S is registered.
f3450 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69    So the followi
f3460 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 61  ng.  ** array ca
f3470 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a 20  nnot be const.. 
f3480 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 6c   */.  static sql
f3490 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d 20  ite3_vfs aVfs[] 
f34a0 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  = {.#if SQLITE_E
f34b0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
f34c0 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f 52  YLE && (OS_VXWOR
f34d0 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  KS || defined(__
f34e0 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55 4e  APPLE__)).    UN
f34f0 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 20  IXVFS("unix",   
f3500 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b 49         autolockI
f3510 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 65  oFinder ),.#else
f3520 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e  .    UNIXVFS("un
f3530 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 6f  ix",          po
f3540 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23  sixIoFinder ),.#
f3550 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 46  endif.    UNIXVF
f3560 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20  S("unix-none",  
f3570 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65     nolockIoFinde
f3580 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
f3590 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c  ("unix-dotfile",
f35a0 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65    dotlockIoFinde
f35b0 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
f35c0 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 20 20 20  ("unix-wfl",    
f35d0 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64    posixWflIoFind
f35e0 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 57  er ),.#if OS_VXW
f35f0 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 53  ORKS.    UNIXVFS
f3600 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22  ("unix-namedsem"
f3610 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 2c  , semIoFinder ),
f3620 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
f3630 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
f3640 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 58  G_STYLE.    UNIX
f3650 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 22  VFS("unix-posix"
f3660 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64  ,    posixIoFind
f3670 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56 58  er ),.#if !OS_VX
f3680 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46  WORKS.    UNIXVF
f3690 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20  S("unix-flock", 
f36a0 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72     flockIoFinder
f36b0 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69   ),.#endif.#endi
f36c0 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  f.#if SQLITE_ENA
f36d0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
f36e0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41  E && defined(__A
f36f0 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 58  PPLE__).    UNIX
f3700 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c 20  VFS("unix-afp", 
f3710 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65 72       afpIoFinder
f3720 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28   ),.    UNIXVFS(
f3730 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20 20  "unix-proxy",   
f3740 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20 29   proxyIoFinder )
f3750 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20  ,.#endif.  };.  
f3760 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 20  unsigned int i; 
f3770 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
f3780 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 2f   counter */..  /
f3790 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 56  * Register all V
f37a0 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e 20  FSes defined in 
f37b0 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61 79  the aVfs[] array
f37c0 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
f37d0 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f 73  <(sizeof(aVfs)/s
f37e0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 66  izeof(sqlite3_vf
f37f0 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  s)); i++){.    s
f3800 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
f3810 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69 3d  ter(&aVfs[i], i=
f3820 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  =0);.  }.  retur
f3830 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
f3840 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20  ./*.** Shutdown 
f3850 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
f3860 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a  stem interface..
f3870 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72 61  **.** Some opera
f3880 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69 67  ting systems mig
f3890 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73 6f  ht need to do so
f38a0 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74 68  me cleanup in th
f38b0 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 74  is routine,.** t
f38c0 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d 69  o release dynami
f38d0 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
f38e0 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e 6f  objects.  But no
f38f0 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54 68  t on unix..** Th
f3900 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
f3910 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e 0a  no-op for unix..
f3920 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
f3930 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  t sqlite3_os_end
f3940 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 72  (void){ .  retur
f3950 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
f3960 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49   .#endif /* SQLI
f3970 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a 2f  TE_OS_UNIX */../
f3980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
f3990 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20  nd of os_unix.c 
f39a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f39b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f39c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f39d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
f39e0 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69 6e  egin file os_win
f39f0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
f3a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f3a20 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32  *.** 2004 May 22
f3a30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
f3a40 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
f3a50 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
f3a60 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
f3a70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
f3a80 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
f3a90 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
f3aa0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
f3ab0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
f3ac0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
f3ad0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
f3ae0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
f3af0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
f3b00 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
f3b10 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
f3b20 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
f3b30 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
f3b40 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
f3b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3b90 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
f3ba0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
f3bb0 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63 69  de that is speci
f3bc0 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e 0a  fic to windows..
f3bd0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f3be0 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  _WIN            
f3bf0 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20     /* This file 
f3c00 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e 64  is used for wind
f3c10 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a  ows only */.../*
f3c20 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 74  .** A Note About
f3c30 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69   Memory Allocati
f3c40 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64  on:.**.** This d
f3c50 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f  river uses mallo
f3c60 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 63  c()/free() direc
f3c70 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e 20  tly rather than 
f3c80 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a  going through.**
f3c90 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 70   the SQLite-wrap
f3ca0 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c  pers sqlite3_mal
f3cb0 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 72  loc()/sqlite3_fr
f3cc0 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 61  ee().  Those wra
f3cd0 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 73  ppers.** are des
f3ce0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f 6e  igned for use on
f3cf0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
f3d00 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 69  s where memory i
f3d10 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20  s scarce and.** 
f3d20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20  malloc failures 
f3d30 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 6c  happen frequentl
f3d40 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 6e  y.  Win32 does n
f3d50 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 6e  ot typically run
f3d60 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20   on.** embedded 
f3d70 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 65  systems, and whe
f3d80 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 65  n it does the de
f3d90 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c  velopers normall
f3da0 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a 2a  y have bigger.**
f3db0 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72   problems to wor
f3dc0 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 75  ry about than ru
f3dd0 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d  nning out of mem
f3de0 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 69  ory.  So there i
f3df0 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65  s not.** a compe
f3e00 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 73  lling need to us
f3e10 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 0a  e the wrappers..
f3e20 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 20  **.** But there 
f3e30 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e  is a good reason
f3e40 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20   to not use the 
f3e50 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 65  wrappers.  If we
f3e60 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 70   use the.** wrap
f3e70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 6c  pers then we wil
f3e80 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 20  l get simulated 
f3e90 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
f3ea0 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a  s within this.**
f3eb0 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 68   driver.  And th
f3ec0 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b 69  at causes all ki
f3ed0 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20  nds of problems 
f3ee0 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 20  for our tests.  
f3ef0 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61  We.** could enha
f3f00 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 65  nce SQLite to de
f3f10 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 65  al with simulate
f3f20 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  d malloc failure
f3f30 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20  s within.** the 
f3f40 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 74  OS driver, but t
f3f50 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c 20  he code to deal 
f3f60 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c 75  with those failu
f3f70 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20  re would not.** 
f3f80 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e 20  be exercised on 
f3f90 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f 65  Linux (which doe
f3fa0 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61  s not need to ma
f3fb0 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 72  lloc() in the dr
f3fc0 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20  iver).** and so 
f3fd0 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 69  we would have di
f3fe0 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e 67  fficulty writing
f3ff0 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 20   coverage tests 
f4000 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65  for that.** code
f4010 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 61  .  Better to lea
f4020 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 2c  ve the code out,
f4030 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a   we think..**.**
f4040 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68   The point of th
f4050 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 73  is discussion is
f4060 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68   as follows:  Wh
f4070 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65  en creating a ne
f4080 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f  w.** OS layer fo
f4090 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 79  r an embedded sy
f40a0 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 65  stem, if you use
f40b0 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 6e   this file as an
f40c0 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f   example,.** avo
f40d0 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d 61  id the use of ma
f40e0 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 20  lloc()/free().  
f40f0 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 77  Those routines w
f4100 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77  ork ok on window
f4110 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75  s.** desktops bu
f4120 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e  t not so well in
f4130 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
f4140 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20  s..*/..#include 
f4150 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 66  <winbase.h>..#if
f4160 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23  def __CYGWIN__.#
f4170 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 79   include <sys/cy
f4180 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a  gwin.h>.#endif..
f4190 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65  /*.** Macros use
f41a0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  d to determine w
f41b0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f  hether or not to
f41c0 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f   use threads..*/
f41d0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52  .#if defined(THR
f41e0 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 45  EADSAFE) && THRE
f41f0 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20  ADSAFE.# define 
f4200 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 41  SQLITE_W32_THREA
f4210 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  DS 1.#endif../*.
f4220 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20  ** Include code 
f4230 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
f4240 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c  o all os_*.c fil
f4250 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.*/./*********
f4260 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73  ***** Include os
f4270 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65  _common.h in the
f4280 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 69   middle of os_wi
f4290 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
f42a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
f42b0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
f42c0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
f42d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f42e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f42f0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
f4300 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68   May 22.**.** Th
f4310 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
f4320 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
f4330 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
f4340 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
f4350 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
f4360 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
f4370 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
f4380 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
f4390 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
f43a0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
f43b0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
f43c0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
f43d0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
f43e0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
f43f0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
f4400 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
f4410 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
f4420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
f4470 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
f4480 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20  ains macros and 
f4490 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20  a little bit of 
f44a0 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d  code that is com
f44b0 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66  mon to.** all of
f44c0 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70   the platform-sp
f44d0 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73  ecific files (os
f44e0 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e  _*.c) and is #in
f44f0 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73  cluded into thos
f4500 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  e.** files..**.*
f4510 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75  * This file shou
f4520 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20  ld be #included 
f4530 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69  by the os_*.c fi
f4540 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73  les only.  It is
f4550 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61   not a.** genera
f4560 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72  l purpose header
f4570 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65   file..*/.#ifnde
f4580 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a  f _OS_COMMON_H_.
f4590 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d  #define _OS_COMM
f45a0 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20  ON_H_../*.** At 
f45b0 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68  least two bugs h
f45c0 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62  ave slipped in b
f45d0 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65  ecause we change
f45e0 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42  d the MEMORY_DEB
f45f0 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53  UG.** macro to S
f4600 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20  QLITE_DEBUG and 
f4610 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66  some older makef
f4620 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65  iles have not ye
f4630 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77  t made the.** sw
f4640 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  itch.  The follo
f4650 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64  wing code should
f4660 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62   catch this prob
f4670 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lem at compile-t
f4680 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d  ime..*/.#ifdef M
f4690 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72  EMORY_DEBUG.# er
f46a0 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f  ror "The MEMORY_
f46b0 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f  DEBUG macro is o
f46c0 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51  bsolete.  Use SQ
f46d0 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65  LITE_DEBUG inste
f46e0 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ad.".#endif..#if
f46f0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
f4700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f4710 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61  int sqlite3OSTra
f4720 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  ce = 0;.#define 
f4730 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20  OSTRACE1(X)     
f4740 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
f4750 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
f4760 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23  DebugPrintf(X).#
f4770 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28  define OSTRACE2(
f4780 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73  X,Y)       if( s
f4790 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
f47a0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
f47b0 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  tf(X,Y).#define 
f47c0 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20  OSTRACE3(X,Y,Z) 
f47d0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
f47e0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
f47f0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
f4800 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
f4810 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69  CE4(X,Y,Z,A)   i
f4820 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
f4830 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
f4840 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a  Printf(X,Y,Z,A).
f4850 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35  #define OSTRACE5
f4860 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20  (X,Y,Z,A,B) if( 
f4870 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
f4880 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
f4890 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  ntf(X,Y,Z,A,B).#
f48a0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
f48b0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20  X,Y,Z,A,B,C) \. 
f48c0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54     if(sqlite3OST
f48d0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
f48e0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
f48f0 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,B,C).#define OS
f4900 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE7(X,Y,Z,A,B
f4910 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73  ,C,D) \.    if(s
f4920 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73  qlite3OSTrace) s
f4930 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
f4940 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  f(X,Y,Z,A,B,C,D)
f4950 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
f4960 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69  STRACE1(X).#defi
f4970 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
f4980 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
f4990 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65  3(X,Y,Z).#define
f49a0 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c   OSTRACE4(X,Y,Z,
f49b0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
f49c0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  CE5(X,Y,Z,A,B).#
f49d0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
f49e0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
f49f0 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
f4a00 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e  Y,Z,A,B,C,D).#en
f4a10 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  dif../*.** Macro
f4a20 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  s for performanc
f4a30 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d  e tracing.  Norm
f4a40 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e  ally turned off.
f4a50 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20    Only works.** 
f4a60 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65  on i486 hardware
f4a70 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
f4a80 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54  TE_PERFORMANCE_T
f4a90 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74  RACE../* .** hwt
f4aa0 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69  ime.h contains i
f4ab0 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20  nline assembler 
f4ac0 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65  code for impleme
f4ad0 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70  nting .** high-p
f4ae0 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e  erformance timin
f4af0 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f  g routines..*/./
f4b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
f4b10 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20  nclude hwtime.h 
f4b20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
f4b30 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
f4b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f4b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
f4b60 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65  egin file hwtime
f4b70 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
f4b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f4ba0 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37  *.** 2008 May 27
f4bb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
f4bc0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
f4bd0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
f4be0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
f4bf0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
f4c00 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
f4c10 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
f4c20 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
f4c30 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
f4c40 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
f4c50 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
f4c60 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
f4c70 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
f4c80 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
f4c90 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
f4ca0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
f4cb0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
f4cc0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
f4cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4d10 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
f4d20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e  file contains in
f4d30 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f  line asm code fo
f4d40 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69  r retrieving "hi
f4d50 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a  gh-performance".
f4d60 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20  ** counters for 
f4d70 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a  x86 class CPUs..
f4d80 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49  */.#ifndef _HWTI
f4d90 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48  ME_H_.#define _H
f4da0 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  WTIME_H_../*.** 
f4db0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
f4dc0 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73  utine only works
f4dd0 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73   on pentium-clas
f4de0 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f  s (or newer) pro
f4df0 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75  cessors..** It u
f4e00 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70  ses the RDTSC op
f4e10 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65  code to read the
f4e20 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c   cycle count val
f4e30 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a  ue out of the.**
f4e40 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72   processor and r
f4e50 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75  eturns that valu
f4e60 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  e.  This can be 
f4e70 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65  used for high-re
f4e80 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a  s.** profiling..
f4e90 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28  */.#if (defined(
f4ea0 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66  __GNUC__) || def
f4eb0 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20  ined(_MSC_VER)) 
f4ec0 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69  && \.      (defi
f4ed0 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66  ned(i386) || def
f4ee0 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c  ined(__i386__) |
f4ef0 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38  | defined(_M_IX8
f4f00 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e  6))..  #if defin
f4f10 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20  ed(__GNUC__)..  
f4f20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
f4f30 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
f4f40 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
f4f50 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
f4f60 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61  lo, hi;.     __a
f4f70 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
f4f80 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61  _ ("rdtsc" : "=a
f4f90 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69  " (lo), "=d" (hi
f4fa0 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20  ));.     return 
f4fb0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68  (sqlite_uint64)h
f4fc0 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20  i << 32 | lo;.  
f4fd0 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e  }..  #elif defin
f4fe0 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20  ed(_MSC_VER)..  
f4ff0 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64  __declspec(naked
f5000 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74  ) __inline sqlit
f5010 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c  e_uint64 __cdecl
f5020 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
f5030 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d  oid){.     __asm
f5040 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63   {.        rdtsc
f5050 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20  .        ret    
f5060 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75     ; return valu
f5070 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20  e at EDX:EAX.   
f5080 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69    }.  }..  #endi
f5090 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65  f..#elif (define
f50a0 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
f50b0 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f  efined(__x86_64_
f50c0 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
f50d0 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
f50e0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
f50f0 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
f5100 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20  ned long val;.  
f5110 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
f5120 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63  latile__ ("rdtsc
f5130 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b  " : "=A" (val));
f5140 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61  .      return va
f5150 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28  l;.  }. .#elif (
f5160 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
f5170 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70  ) && defined(__p
f5180 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69  pc__))..  __inli
f5190 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
f51a0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
f51b0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e  (void){.      un
f51c0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
f51d0 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75   retval;.      u
f51e0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e  nsigned long jun
f51f0 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f  k;.      __asm__
f5200 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
f5210 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a  \n\.          1:
f5220 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31        mftbu   %1
f5230 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
f5240 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c        mftb    %L
f5250 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  0\n\.           
f5260 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25         mftbu   %
f5270 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  0\n\.           
f5280 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25         cmpw    %
f5290 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20  0,%1\n\.        
f52a0 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20            bne   
f52b0 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20    1b".          
f52c0 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28          : "=r" (
f52d0 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a  retval), "=r" (j
f52e0 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74  unk));.      ret
f52f0 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a  urn retval;.  }.
f5300 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72  .#else..  #error
f5310 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61   Need implementa
f5320 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48  tion of sqlite3H
f5330 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72  wtime() for your
f5340 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a   platform...  /*
f5350 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65  .  ** To compile
f5360 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65   without impleme
f5370 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74  nting sqlite3Hwt
f5380 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70  ime() for your p
f5390 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f  latform,.  ** yo
f53a0 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65  u can remove the
f53b0 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e   above #error an
f53c0 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77  d use the follow
f53d0 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75  ing.  ** stub fu
f53e0 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c  nction.  You wil
f53f0 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75  l lose timing su
f5400 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20  pport for many. 
f5410 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67   ** of the debug
f5420 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67  ging and testing
f5430 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20   utilities, but 
f5440 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a  it should at.  *
f5450 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20  * least compile 
f5460 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51  and run..  */.SQ
f5470 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
f5480 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
f5490 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
f54a0 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74  { return ((sqlit
f54b0 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a  e_uint64)0); }..
f54c0 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
f54d0 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49  * !defined(_HWTI
f54e0 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ME_H_) */../****
f54f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f5500 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a  f hwtime.h *****
f5510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f5540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
f5550 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
f5560 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f  eft off in os_co
f5570 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
f5580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74  *********/..stat
f5590 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ic sqlite_uint64
f55a0 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63   g_start;.static
f55b0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67   sqlite_uint64 g
f55c0 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e  _elapsed;.#defin
f55d0 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20  e TIMER_START   
f55e0 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69      g_start=sqli
f55f0 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66  te3Hwtime().#def
f5600 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20  ine TIMER_END   
f5610 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d        g_elapsed=
f5620 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d  sqlite3Hwtime()-
f5630 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20  g_start.#define 
f5640 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20  TIMER_ELAPSED   
f5650 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73    g_elapsed.#els
f5660 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  e.#define TIMER_
f5670 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49  START.#define TI
f5680 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20  MER_END.#define 
f5690 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20  TIMER_ELAPSED   
f56a0 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36    ((sqlite_uint6
f56b0 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  4)0).#endif../*.
f56c0 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65  ** If we compile
f56d0 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
f56e0 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c  _TEST macro set,
f56f0 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77   then the follow
f5700 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20  ing block.** of 
f5710 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75  code will give u
f5720 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f  s the ability to
f5730 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b   simulate a disk
f5740 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69   I/O error.  Thi
f5750 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72  s.** is used for
f5760 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f   testing the I/O
f5770 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e   recovery logic.
f5780 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
f5790 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
f57a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
f57b0 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20  _error_hit = 0; 
f57c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
f57d0 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f  tal number of I/
f57e0 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49  O Errors */.SQLI
f57f0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
f5800 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64  e3_io_error_hard
f5810 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  hit = 0;        
f5820 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  /* Number of non
f5830 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a  -benign errors *
f5840 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
f5850 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
f5860 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20  r_pending = 0;  
f5870 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64        /* Count d
f5880 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f  own to first I/O
f5890 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45   error */.SQLITE
f58a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
f58b0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73  _io_error_persis
f58c0 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  t = 0;        /*
f58d0 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72   True if I/O err
f58e0 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53  ors persist */.S
f58f0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
f5900 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
f5910 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20  enign = 0;      
f5920 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72     /* True if er
f5930 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20  rors are benign 
f5940 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
f5950 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  t sqlite3_diskfu
f5960 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a  ll_pending = 0;.
f5970 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
f5980 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20  qlite3_diskfull 
f5990 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d  = 0;.#define Sim
f59a0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
f59b0 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f  gn(X) sqlite3_io
f59c0 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58  _error_benign=(X
f59d0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
f59e0 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20  teIOError(CODE) 
f59f0 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65   \.  if( (sqlite
f5a00 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69  3_io_error_persi
f5a10 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f  st && sqlite3_io
f5a20 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20  _error_hit) \.  
f5a30 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f       || sqlite3_
f5a40 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67  io_error_pending
f5a50 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20  -- == 1 )  \.   
f5a60 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63             { loc
f5a70 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45  al_ioerr(); CODE
f5a80 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ; }.static void 
f5a90 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20  local_ioerr(){. 
f5aa0 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52   IOTRACE(("IOERR
f5ab0 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33  \n"));.  sqlite3
f5ac0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b  _io_error_hit++;
f5ad0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f  .  if( !sqlite3_
f5ae0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20  io_error_benign 
f5af0 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ) sqlite3_io_err
f5b00 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a  or_hardhit++;.}.
f5b10 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
f5b20 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f  DiskfullError(CO
f5b30 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c  DE) \.   if( sql
f5b40 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
f5b50 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20  nding ){ \.     
f5b60 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  if( sqlite3_disk
f5b70 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20  full_pending == 
f5b80 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f  1 ){ \.       lo
f5b90 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20  cal_ioerr(); \. 
f5ba0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69        sqlite3_di
f5bb0 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20  skfull = 1; \.  
f5bc0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f       sqlite3_io_
f5bd0 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c  error_hit = 1; \
f5be0 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a  .       CODE; \.
f5bf0 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20       }else{ \.  
f5c00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
f5c10 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b  kfull_pending--;
f5c20 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d   \.     } \.   }
f5c30 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53  .#else.#define S
f5c40 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
f5c50 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20  nign(X).#define 
f5c60 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
f5c70 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  A).#define Simul
f5c80 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
f5c90 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (A).#endif../*.*
f5ca0 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20  * When testing, 
f5cb0 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20  keep a count of 
f5cc0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70  the number of op
f5cd0 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66  en files..*/.#if
f5ce0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
f5cf0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
f5d00 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65  qlite3_open_file
f5d10 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66  _count = 0;.#def
f5d20 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  ine OpenCounter(
f5d30 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  X)  sqlite3_open
f5d40 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29  _file_count+=(X)
f5d50 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
f5d60 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65  penCounter(X).#e
f5d70 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
f5d80 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d  !defined(_OS_COM
f5d90 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  MON_H_) */../***
f5da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
f5db0 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a  of os_common.h *
f5dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
f5df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
f5e00 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
f5e10 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77  left off in os_w
f5e20 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
f5e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
f5e40 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66  ** Some microsof
f5e50 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b  t compilers lack
f5e60 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e   this definition
f5e70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56  ..*/.#ifndef INV
f5e80 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
f5e90 55 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e  UTES.# define IN
f5ea0 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49  VALID_FILE_ATTRI
f5eb0 42 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31  BUTES ((DWORD)-1
f5ec0 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  ) .#endif../*.**
f5ed0 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65   Determine if we
f5ee0 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
f5ef0 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68  h WindowsCE - wh
f5f00 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a  ich has a much.*
f5f10 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a  * reduced API..*
f5f20 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
f5f30 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41  WINCE.# define A
f5f40 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29  reFileApisANSI()
f5f50 20 31 0a 23 20 64 65 66 69 6e 65 20 46 6f 72 6d   1.# define Form
f5f60 61 74 4d 65 73 73 61 67 65 57 28 61 2c 62 2c 63  atMessageW(a,b,c
f5f70 2c 64 2c 65 2c 66 2c 67 29 20 30 0a 23 65 6e 64  ,d,e,f,g) 0.#end
f5f80 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45 20  if../*.** WinCE 
f5f90 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 70  lacks native sup
f5fa0 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c 6f  port for file lo
f5fb0 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 65  cking so we have
f5fc0 20 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20 77   to fake it.** w
f5fd0 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f 66  ith some code of
f5fe0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66   our own..*/.#if
f5ff0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f6000 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
f6010 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e  winceLock {.  in
f6020 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 20  t nReaders;     
f6030 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
f6040 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 61  eader locks obta
f6050 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62  ined */.  BOOL b
f6060 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f 2a  Pending;      /*
f6070 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 6e   Indicates a pen
f6080 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65  ding lock has be
f6090 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20  en obtained */. 
f60a0 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 3b   BOOL bReserved;
f60b0 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65       /* Indicate
f60c0 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  s a reserved loc
f60d0 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69  k has been obtai
f60e0 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 45  ned */.  BOOL bE
f60f0 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a 20  xclusive;    /* 
f6100 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 63  Indicates an exc
f6110 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 20  lusive lock has 
f6120 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f  been obtained */
f6130 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 65  .} winceLock;.#e
f6140 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
f6150 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 72  winFile structur
f6160 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20  e is a subclass 
f6170 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  of sqlite3_file*
f6180 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65   specific to the
f6190 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 62   win32.** portab
f61a0 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a  ility layer..*/.
f61b0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77  typedef struct w
f61c0 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b 0a  inFile winFile;.
f61d0 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 7b  struct winFile {
f61e0 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
f61f0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65  _io_methods *pMe
f6200 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65 20  thod;/* Must be 
f6210 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44 4c  first */.  HANDL
f6220 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  E h;            
f6230 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72     /* Handle for
f6240 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 66   accessing the f
f6250 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ile */.  unsigne
f6260 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b  d char locktype;
f6270 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b   /* Type of lock
f6280 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20   currently held 
f6290 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  on this file */.
f62a0 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c 6f    short sharedLo
f62b0 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61 6e  ckByte;   /* Ran
f62c0 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79 74  domly chosen byt
f62d0 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61 72  e used as a shar
f62e0 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 44 57 4f  ed lock */.  DWO
f62f0 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20  RD lastErrno;   
f6300 20 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e 64       /* The Wind
f6310 6f 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74  ows errno from t
f6320 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f  he last I/O erro
f6330 72 20 2a 2f 0a 20 20 44 57 4f 52 44 20 73 65 63  r */.  DWORD sec
f6340 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 2f  torSize;       /
f6350 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 6f 66  * Sector size of
f6360 20 74 68 65 20 64 65 76 69 63 65 20 66 69 6c 65   the device file
f6370 20 69 73 20 6f 6e 20 2a 2f 0a 23 69 66 20 53 51   is on */.#if SQ
f6380 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
f6390 57 43 48 41 52 20 2a 7a 44 65 6c 65 74 65 4f 6e  WCHAR *zDeleteOn
f63a0 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20  Close;  /* Name 
f63b0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74  of file to delet
f63c0 65 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 2a  e when closing *
f63d0 2f 0a 20 20 48 41 4e 44 4c 45 20 68 4d 75 74 65  /.  HANDLE hMute
f63e0 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  x;          /* M
f63f0 75 74 65 78 20 75 73 65 64 20 74 6f 20 63 6f 6e  utex used to con
f6400 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 73  trol access to s
f6410 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 20 20 0a  hared lock */  .
f6420 20 20 48 41 4e 44 4c 45 20 68 53 68 61 72 65 64    HANDLE hShared
f6430 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61  ;         /* Sha
f6440 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d 65  red memory segme
f6450 6e 74 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  nt used for lock
f6460 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f  ing */.  winceLo
f6470 63 6b 20 6c 6f 63 61 6c 3b 20 20 20 20 20 20 20  ck local;       
f6480 20 2f 2a 20 4c 6f 63 6b 73 20 6f 62 74 61 69 6e   /* Locks obtain
f6490 65 64 20 62 79 20 74 68 69 73 20 69 6e 73 74 61  ed by this insta
f64a0 6e 63 65 20 6f 66 20 77 69 6e 46 69 6c 65 20 2a  nce of winFile *
f64b0 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 2a 73  /.  winceLock *s
f64c0 68 61 72 65 64 3b 20 20 20 20 20 20 2f 2a 20 47  hared;      /* G
f64d0 6c 6f 62 61 6c 20 73 68 61 72 65 64 20 6c 6f 63  lobal shared loc
f64e0 6b 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65  k memory for the
f64f0 20 66 69 6c 65 20 20 2a 2f 0a 23 65 6e 64 69 66   file  */.#endif
f6500 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61  .};../*.** Forwa
f6510 72 64 20 70 72 6f 74 6f 74 79 70 65 73 2e 0a 2a  rd prototypes..*
f6520 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
f6530 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20  SectorSize(.    
f6540 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
f6550 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  s,.    const cha
f6560 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20  r *zRelative    
f6570 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e   /* UTF-8 file n
f6580 61 6d 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ame */.);../*.**
f6590 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
f65a0 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72 6d  ariable is (norm
f65b0 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20 61  ally) set once a
f65c0 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65 73  nd never changes
f65d0 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e 20  .** thereafter. 
f65e0 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65 74   It records whet
f65f0 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 6e  her the operatin
f6600 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39  g system is Win9
f6610 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a  5.** or WinNT..*
f6620 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61 74  *.** 0:   Operat
f6630 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e 6f  ing system unkno
f6640 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65 72  wn..** 1:   Oper
f6650 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20  ating system is 
f6660 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20 4f  Win95..** 2:   O
f6670 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
f6680 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20  is WinNT..**.** 
f6690 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63 69  In order to faci
f66a0 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20 6f  litate testing o
f66b0 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65 6d  n a WinNT system
f66c0 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74 75  , the test fixtu
f66d0 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61 6c  re.** can manual
f66e0 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c 75  ly set this valu
f66f0 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61 74  e to 1 to emulat
f6700 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f 72  e Win98 behavior
f6710 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
f6720 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
f6730 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
f6740 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c 73  s_type = 0;.#els
f6750 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  e.static int sql
f6760 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30  ite3_os_type = 0
f6770 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
f6780 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e  Return true (non
f6790 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65  -zero) if we are
f67a0 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57   running under W
f67b0 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e  inNT, Win2K, Win
f67c0 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e  XP,.** or WinCE.
f67d0 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28    Return false (
f67e0 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c  zero) for Win95,
f67f0 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45   Win98, or WinME
f6800 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20  ..**.** Here is 
f6810 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f  an interesting o
f6820 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e  bservation:  Win
f6830 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57  95, Win98, and W
f6840 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65  inME lack.** the
f6850 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50   LockFileEx() AP
f6860 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73  I.  But we can s
f6870 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20  till statically 
f6880 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61  link against tha
f6890 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67  t.** API as long
f68a0 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c   as we don't cal
f68b0 6c 20 69 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e  l it when runnin
f68c0 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20  g Win95/98/ME.  
f68d0 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69  A call to.** thi
f68e0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
f68f0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  d to determine i
f6900 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69  f the host is Wi
f6910 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20  n95/98/ME or.** 
f6920 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74  WinNT/2K/XP so t
f6930 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77  hat we will know
f6940 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
f6950 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61  we can safely ca
f6960 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69  ll.** the LockFi
f6970 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23  leEx() API..*/.#
f6980 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f6990 43 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e 54  CE.# define isNT
f69a0 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20  ()  (1).#else.  
f69b0 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54 28  static int isNT(
f69c0 76 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20 73  void){.    if( s
f69d0 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d  qlite3_os_type==
f69e0 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52  0 ){.      OSVER
f69f0 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a  SIONINFO sInfo;.
f6a00 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53        sInfo.dwOS
f6a10 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20  VersionInfoSize 
f6a20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b  = sizeof(sInfo);
f6a30 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69 6f  .      GetVersio
f6a40 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20  nEx(&sInfo);.   
f6a50 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79     sqlite3_os_ty
f6a60 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61  pe = sInfo.dwPla
f6a70 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41  tformId==VER_PLA
f6a80 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f  TFORM_WIN32_NT ?
f6a90 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20   2 : 1;.    }.  
f6aa0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
f6ab0 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d  _os_type==2;.  }
f6ac0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
f6ad0 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f  E_OS_WINCE */../
f6ae0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55  *.** Convert a U
f6af0 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 6d  TF-8 string to m
f6b00 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
f6b10 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a   (UTF-16?). .**.
f6b20 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  ** Space to hold
f6b30 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
f6b40 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64  ring is obtained
f6b50 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f   from malloc..*/
f6b60 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a 75  .static WCHAR *u
f6b70 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e  tf8ToUnicode(con
f6b80 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
f6b90 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 72  me){.  int nChar
f6ba0 3b 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64 65  ;.  WCHAR *zWide
f6bb0 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 68  Filename;..  nCh
f6bc0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f  ar = MultiByteTo
f6bd0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38  WideChar(CP_UTF8
f6be0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
f6bf0 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 20  -1, NULL, 0);.  
f6c00 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20  zWideFilename = 
f6c10 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73 69  malloc( nChar*si
f6c20 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e 61  zeof(zWideFilena
f6c30 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  me[0]) );.  if( 
f6c40 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d 30  zWideFilename==0
f6c50 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
f6c60 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20  ;.  }.  nChar = 
f6c70 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43  MultiByteToWideC
f6c80 68 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20  har(CP_UTF8, 0, 
f6c90 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a  zFilename, -1, z
f6ca0 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e 43  WideFilename, nC
f6cb0 68 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68 61  har);.  if( nCha
f6cc0 72 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65  r==0 ){.    free
f6cd0 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 3b  (zWideFilename);
f6ce0 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e 61  .    zWideFilena
f6cf0 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  me = 0;.  }.  re
f6d00 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e 61  turn zWideFilena
f6d10 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  me;.}../*.** Con
f6d20 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 75  vert microsoft u
f6d30 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38 2e  nicode to UTF-8.
f6d40 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
f6d50 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  the returned str
f6d60 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e  ing is.** obtain
f6d70 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
f6d80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
f6d90 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28   *unicodeToUtf8(
f6da0 63 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69  const WCHAR *zWi
f6db0 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69  deFilename){.  i
f6dc0 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72  nt nByte;.  char
f6dd0 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20   *zFilename;..  
f6de0 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72  nByte = WideChar
f6df0 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55  ToMultiByte(CP_U
f6e00 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c  TF8, 0, zWideFil
f6e10 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c  ename, -1, 0, 0,
f6e20 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e   0, 0);.  zFilen
f6e30 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42  ame = malloc( nB
f6e40 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69  yte );.  if( zFi
f6e50 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  lename==0 ){.   
f6e60 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
f6e70 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61   nByte = WideCha
f6e80 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f  rToMultiByte(CP_
f6e90 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69  UTF8, 0, zWideFi
f6ea0 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c  lename, -1, zFil
f6eb0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20  ename, nByte,.  
f6ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6ed0 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30              0, 0
f6ee0 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d  );.  if( nByte =
f6ef0 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28  = 0 ){.    free(
f6f00 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20  zFilename);.    
f6f10 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20  zFilename = 0;. 
f6f20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c   }.  return zFil
f6f30 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ename;.}../*.** 
f6f40 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69 20  Convert an ansi 
f6f50 73 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73  string to micros
f6f60 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61 73  oft unicode, bas
f6f70 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72  ed on the.** cur
f6f80 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73 65  rent codepage se
f6f90 74 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65 20  ttings for file 
f6fa0 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61  apis..** .** Spa
f6fb0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
f6fc0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
f6fd0 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  s obtained.** fr
f6fe0 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74  om malloc..*/.st
f6ff0 61 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63 73  atic WCHAR *mbcs
f7000 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20  ToUnicode(const 
f7010 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
f7020 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  {.  int nByte;. 
f7030 20 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69 6c   WCHAR *zMbcsFil
f7040 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64  ename;.  int cod
f7050 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41  epage = AreFileA
f7060 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41  pisANSI() ? CP_A
f7070 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a  CP : CP_OEMCP;..
f7080 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42    nByte = MultiB
f7090 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f  yteToWideChar(co
f70a0 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65  depage, 0, zFile
f70b0 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 30  name, -1, NULL,0
f70c0 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29 3b  )*sizeof(WCHAR);
f70d0 0a 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65  .  zMbcsFilename
f70e0 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65   = malloc( nByte
f70f0 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 6c  *sizeof(zMbcsFil
f7100 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69  ename[0]) );.  i
f7110 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65  f( zMbcsFilename
f7120 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f7130 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65  n 0;.  }.  nByte
f7140 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69   = MultiByteToWi
f7150 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c  deChar(codepage,
f7160 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d   0, zFilename, -
f7170 31 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65  1, zMbcsFilename
f7180 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20  , nByte);.  if( 
f7190 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20  nByte==0 ){.    
f71a0 66 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e 61  free(zMbcsFilena
f71b0 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 69  me);.    zMbcsFi
f71c0 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a  lename = 0;.  }.
f71d0 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 69    return zMbcsFi
f71e0 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  lename;.}../*.**
f71f0 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f   Convert microso
f7200 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d 75  ft unicode to mu
f7210 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65  ltibyte characte
f7220 72 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64 20  r string, based 
f7230 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27 73  on the.** user's
f7240 20 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e 0a   Ansi codepage..
f7250 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68  **.** Space to h
f7260 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64  old the returned
f7270 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
f7280 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c  ned from.** mall
f7290 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  oc()..*/.static 
f72a0 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 4d  char *unicodeToM
f72b0 62 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52 20  bcs(const WCHAR 
f72c0 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b  *zWideFilename){
f72d0 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20  .  int nByte;.  
f72e0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b  char *zFilename;
f72f0 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20  .  int codepage 
f7300 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53  = AreFileApisANS
f7310 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43  I() ? CP_ACP : C
f7320 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74  P_OEMCP;..  nByt
f7330 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75  e = WideCharToMu
f7340 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 65  ltiByte(codepage
f7350 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61  , 0, zWideFilena
f7360 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c  me, -1, 0, 0, 0,
f7370 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65   0);.  zFilename
f7380 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65   = malloc( nByte
f7390 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e   );.  if( zFilen
f73a0 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ame==0 ){.    re
f73b0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42  turn 0;.  }.  nB
f73c0 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f  yte = WideCharTo
f73d0 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61  MultiByte(codepa
f73e0 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65  ge, 0, zWideFile
f73f0 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e  name, -1, zFilen
f7400 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 20  ame, nByte,.    
f7410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7420 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 3b            0, 0);
f7430 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20  .  if( nByte == 
f7440 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 46  0 ){.    free(zF
f7450 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46  ilename);.    zF
f7460 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
f7470 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e  .  return zFilen
f7480 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ame;.}../*.** Co
f7490 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65 20  nvert multibyte 
f74a0 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67  character string
f74b0 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63   to UTF-8.  Spac
f74c0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a  e to hold the.**
f74d0 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67   returned string
f74e0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
f74f0 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53  m malloc()..*/.S
f7500 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
f7510 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62  sqlite3_win32_mb
f7520 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 74  cs_to_utf8(const
f7530 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f7540 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  ){.  char *zFile
f7550 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 41  nameUtf8;.  WCHA
f7560 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20  R *zTmpWide;..  
f7570 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54  zTmpWide = mbcsT
f7580 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61  oUnicode(zFilena
f7590 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57  me);.  if( zTmpW
f75a0 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ide==0 ){.    re
f75b0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46  turn 0;.  }.  zF
f75c0 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e  ilenameUtf8 = un
f75d0 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70  icodeToUtf8(zTmp
f75e0 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54  Wide);.  free(zT
f75f0 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72  mpWide);.  retur
f7600 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b  n zFilenameUtf8;
f7610 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
f7620 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 69  t UTF-8 to multi
f7630 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73  byte character s
f7640 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f  tring.  Space to
f7650 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 65   hold the .** re
f7660 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
f7670 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
f7680 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
f7690 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f 4d  ic char *utf8ToM
f76a0 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  bcs(const char *
f76b0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68  zFilename){.  ch
f76c0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 63  ar *zFilenameMbc
f76d0 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70  s;.  WCHAR *zTmp
f76e0 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64  Wide;..  zTmpWid
f76f0 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64  e = utf8ToUnicod
f7700 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
f7710 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20  if( zTmpWide==0 
f7720 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
f7730 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65  .  }.  zFilename
f7740 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 6f  Mbcs = unicodeTo
f7750 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b 0a  Mbcs(zTmpWide);.
f7760 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29    free(zTmpWide)
f7770 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65  ;.  return zFile
f7780 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 66  nameMbcs;.}..#if
f7790 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f77a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
f77b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f77c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f77d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f77e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
f77f0 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e 74  his section cont
f7800 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57 69  ains code for Wi
f7810 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a 0a  nCE only..*/./*.
f7820 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f 65  ** WindowsCE doe
f7830 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f 63  s not have a loc
f7840 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 6f  altime() functio
f7850 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 0a  n.  So create a.
f7860 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a 2a  ** substitute..*
f7870 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f 63  /.struct tm *__c
f7880 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 63  decl localtime(c
f7890 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29 0a  onst time_t *t).
f78a0 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63  {.  static struc
f78b0 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54 49  t tm y;.  FILETI
f78c0 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53  ME uTm, lTm;.  S
f78d0 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a 20  YSTEMTIME pTm;. 
f78e0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74   sqlite3_int64 t
f78f0 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b 0a  64;.  t64 = *t;.
f7900 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 31    t64 = (t64 + 1
f7910 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 30  1644473600)*1000
f7920 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f  0000;.  uTm.dwLo
f7930 77 44 61 74 65 54 69 6d 65 20 3d 20 28 44 57 4f  wDateTime = (DWO
f7940 52 44 29 28 74 36 34 20 26 20 30 78 46 46 46 46  RD)(t64 & 0xFFFF
f7950 46 46 46 46 29 3b 0a 20 20 75 54 6d 2e 64 77 48  FFFF);.  uTm.dwH
f7960 69 67 68 44 61 74 65 54 69 6d 65 3d 20 28 44 57  ighDateTime= (DW
f7970 4f 52 44 29 28 74 36 34 20 3e 3e 20 33 32 29 3b  ORD)(t64 >> 32);
f7980 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63  .  FileTimeToLoc
f7990 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c  alFileTime(&uTm,
f79a0 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d  &lTm);.  FileTim
f79b0 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c  eToSystemTime(&l
f79c0 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d  Tm,&pTm);.  y.tm
f79d0 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61  _year = pTm.wYea
f79e0 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d  r - 1900;.  y.tm
f79f0 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74  _mon = pTm.wMont
f7a00 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64  h - 1;.  y.tm_wd
f7a10 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57  ay = pTm.wDayOfW
f7a20 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79  eek;.  y.tm_mday
f7a30 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79   = pTm.wDay;.  y
f7a40 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77  .tm_hour = pTm.w
f7a50 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e  Hour;.  y.tm_min
f7a60 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a   = pTm.wMinute;.
f7a70 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d    y.tm_sec = pTm
f7a80 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75  .wSecond;.  retu
f7a90 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  rn &y;.}../* Thi
f7aa0 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  s will never be 
f7ab0 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 69  called, but defi
f7ac0 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  ned to make the 
f7ad0 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a  code compile */.
f7ae0 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 50  #define GetTempP
f7af0 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 69  athA(a,b)..#defi
f7b00 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c  ne LockFile(a,b,
f7b10 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 6e  c,d,e)       win
f7b20 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62  ceLockFile(&a, b
f7b30 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69  , c, d, e).#defi
f7b40 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c  ne UnlockFile(a,
f7b50 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 6e  b,c,d,e)     win
f7b60 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c  ceUnlockFile(&a,
f7b70 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65   b, c, d, e).#de
f7b80 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28  fine LockFileEx(
f7b90 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77  a,b,c,d,e,f)   w
f7ba0 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26  inceLockFileEx(&
f7bb0 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66  a, b, c, d, e, f
f7bc0 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 4c  )..#define HANDL
f7bd0 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20  E_TO_WINFILE(a) 
f7be0 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 61  (winFile*)&((cha
f7bf0 72 2a 29 61 29 5b 2d 28 69 6e 74 29 6f 66 66 73  r*)a)[-(int)offs
f7c00 65 74 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d  etof(winFile,h)]
f7c10 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20  ../*.** Acquire 
f7c20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61  a lock on the ha
f7c30 6e 64 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63  ndle h.*/.static
f7c40 20 76 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 78   void winceMutex
f7c50 41 63 71 75 69 72 65 28 48 41 4e 44 4c 45 20 68  Acquire(HANDLE h
f7c60 29 7b 0a 20 20 20 44 57 4f 52 44 20 64 77 45 72  ){.   DWORD dwEr
f7c70 72 3b 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20  r;.   do {.     
f7c80 64 77 45 72 72 20 3d 20 57 61 69 74 46 6f 72 53  dwErr = WaitForS
f7c90 69 6e 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49  ingleObject(h, I
f7ca0 4e 46 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77  NFINITE);.   } w
f7cb0 68 69 6c 65 20 28 64 77 45 72 72 20 21 3d 20 57  hile (dwErr != W
f7cc0 41 49 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20  AIT_OBJECT_0 && 
f7cd0 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 41 42  dwErr != WAIT_AB
f7ce0 41 4e 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a  ANDONED);.}./*.*
f7cf0 2a 20 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b  * Release a lock
f7d00 20 61 63 71 75 69 72 65 64 20 62 79 20 77 69 6e   acquired by win
f7d10 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 29  ceMutexAcquire()
f7d20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e 63  .*/.#define winc
f7d30 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 68 29  eMutexRelease(h)
f7d40 20 52 65 6c 65 61 73 65 4d 75 74 65 78 28 68 29   ReleaseMutex(h)
f7d50 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74  ../*.** Create t
f7d60 68 65 20 6d 75 74 65 78 20 61 6e 64 20 73 68 61  he mutex and sha
f7d70 72 65 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  red memory used 
f7d80 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74  for locking in t
f7d90 68 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72  he file.** descr
f7da0 69 70 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73  iptor pFile.*/.s
f7db0 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65  tatic BOOL wince
f7dc0 43 72 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74  CreateLock(const
f7dd0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f7de0 2c 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65  , winFile *pFile
f7df0 29 7b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b  ){.  WCHAR *zTok
f7e00 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65  ;.  WCHAR *zName
f7e10 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65   = utf8ToUnicode
f7e20 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42  (zFilename);.  B
f7e30 4f 4f 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45  OOL bInit = TRUE
f7e40 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ;..  /* Initiali
f7e50 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63  ze the local loc
f7e60 6b 64 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d  kdata */.  ZeroM
f7e70 65 6d 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f  emory(&pFile->lo
f7e80 63 61 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c  cal, sizeof(pFil
f7e90 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f  e->local));..  /
f7ea0 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 62 61  * Replace the ba
f7eb0 63 6b 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 74  ckslashes from t
f7ec0 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20  he filename and 
f7ed0 6c 6f 77 65 72 63 61 73 65 20 69 74 0a 20 20 2a  lowercase it.  *
f7ee0 2a 20 74 6f 20 64 65 72 69 76 65 20 61 20 6d 75  * to derive a mu
f7ef0 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a  tex name. */.  z
f7f00 54 6f 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 57  Tok = CharLowerW
f7f10 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28  (zName);.  for (
f7f20 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a  ;*zTok;zTok++){.
f7f30 20 20 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d      if (*zTok ==
f7f40 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27   '\\') *zTok = '
f7f50 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72  _';.  }..  /* Cr
f7f60 65 61 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61  eate/open the na
f7f70 6d 65 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70  med mutex */.  p
f7f80 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43  File->hMutex = C
f7f90 72 65 61 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c  reateMutexW(NULL
f7fa0 2c 20 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b  , FALSE, zName);
f7fb0 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68  .  if (!pFile->h
f7fc0 4d 75 74 65 78 29 7b 0a 20 20 20 20 70 46 69 6c  Mutex){.    pFil
f7fd0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
f7fe0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
f7ff0 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a     free(zName);.
f8000 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45      return FALSE
f8010 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
f8020 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62 65  ire the mutex be
f8030 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20  fore continuing 
f8040 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41  */.  winceMutexA
f8050 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d  cquire(pFile->hM
f8060 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53  utex);.  .  /* S
f8070 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f  ince the names o
f8080 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c  f named mutexes,
f8090 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c   semaphores, fil
f80a0 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61  e mappings etc a
f80b0 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65  re .  ** case-se
f80c0 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64  nsitive, take ad
f80d0 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20  vantage of that 
f80e0 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20 74  by uppercasing t
f80f0 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20  he mutex name.  
f8100 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61  ** and using tha
f8110 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64 20  t as the shared 
f8120 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65  filemapping name
f8130 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70  ..  */.  CharUpp
f8140 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46  erW(zName);.  pF
f8150 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43  ile->hShared = C
f8160 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67  reateFileMapping
f8170 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45  W(INVALID_HANDLE
f8180 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20  _VALUE, NULL,.  
f8190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f81a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f81b0 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57 52       PAGE_READWR
f81c0 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77  ITE, 0, sizeof(w
f81d0 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20  inceLock),.     
f81e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f81f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8200 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f    zName);  ..  /
f8210 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68 61  * Set a flag tha
f8220 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27 72  t indicates we'r
f8230 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20 63  e the first to c
f8240 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79  reate the memory
f8250 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73   so it .  ** mus
f8260 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61  t be zero-initia
f8270 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47  lized */.  if (G
f8280 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d  etLastError() ==
f8290 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45   ERROR_ALREADY_E
f82a0 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69  XISTS){.    bIni
f82b0 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a  t = FALSE;.  }..
f82c0 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a    free(zName);..
f82d0 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65    /* If we succe
f82e0 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74  eded in making t
f82f0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
f8300 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e   handle, map it.
f8310 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d   */.  if (pFile-
f8320 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70  >hShared){.    p
f8330 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28  File->shared = (
f8340 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69  winceLock*)MapVi
f8350 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e  ewOfFile(pFile->
f8360 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20  hShared, .      
f8370 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f         FILE_MAP_
f8380 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52  READ|FILE_MAP_WR
f8390 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f  ITE, 0, 0, sizeo
f83a0 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20  f(winceLock));. 
f83b0 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67     /* If mapping
f83c0 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74   failed, close t
f83d0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
f83e0 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73   handle and eras
f83f0 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28  e it */.    if (
f8400 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b  !pFile->shared){
f8410 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
f8420 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73  stErrno = GetLas
f8430 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20  tError();.      
f8440 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c  CloseHandle(pFil
f8450 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20  e->hShared);.   
f8460 20 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65     pFile->hShare
f8470 64 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a  d = NULL;.    }.
f8480 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61    }..  /* If sha
f8490 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64  red memory could
f84a0 20 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c   not be created,
f84b0 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20   then close the 
f84c0 6d 75 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a  mutex and fail *
f84d0 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68  /.  if (pFile->h
f84e0 53 68 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b  Shared == NULL){
f84f0 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52  .    winceMutexR
f8500 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
f8510 75 74 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65  utex);.    Close
f8520 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d  Handle(pFile->hM
f8530 75 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65  utex);.    pFile
f8540 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b  ->hMutex = NULL;
f8550 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53  .    return FALS
f8560 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49  E;.  }.  .  /* I
f8570 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68  nitialize the sh
f8580 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77  ared memory if w
f8590 65 27 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f  e're supposed to
f85a0 20 2a 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29   */.  if (bInit)
f85b0 20 7b 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72   {.    ZeroMemor
f85c0 79 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c  y(pFile->shared,
f85d0 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63   sizeof(winceLoc
f85e0 6b 29 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63  k));.  }..  winc
f85f0 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46  eMutexRelease(pF
f8600 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20  ile->hMutex);.  
f8610 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a  return TRUE;.}..
f8620 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68  /*.** Destroy th
f8630 65 20 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c  e part of winFil
f8640 65 20 74 68 61 74 20 64 65 61 6c 73 20 77 69 74  e that deals wit
f8650 68 20 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f  h wince locks.*/
f8660 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e  .static void win
f8670 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69  ceDestroyLock(wi
f8680 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  nFile *pFile){. 
f8690 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74   if (pFile->hMut
f86a0 65 78 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75  ex){.    /* Acqu
f86b0 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f  ire the mutex */
f86c0 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41  .    winceMutexA
f86d0 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d  cquire(pFile->hM
f86e0 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54  utex);..    /* T
f86f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
f8700 63 6b 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61  cks should proba
f8710 62 6c 79 20 61 73 73 65 72 74 20 69 6e 20 64 65  bly assert in de
f8720 62 75 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68  bug mode, but th
f8730 65 79 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f  ey.       are to
f8740 20 63 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65   cleanup in case
f8750 20 61 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69   any locks remai
f8760 6e 65 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20  ned open */.    
f8770 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
f8780 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20  .nReaders){.    
f8790 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f87a0 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20  >nReaders --;.  
f87b0 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c    }.    if (pFil
f87c0 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76  e->local.bReserv
f87d0 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  ed){.      pFile
f87e0 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72  ->shared->bReser
f87f0 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ved = FALSE;.   
f8800 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65   }.    if (pFile
f8810 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67  ->local.bPending
f8820 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
f8830 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67  shared->bPending
f8840 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a   = FALSE;.    }.
f8850 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c      if (pFile->l
f8860 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29  ocal.bExclusive)
f8870 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  {.      pFile->s
f8880 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76  hared->bExclusiv
f8890 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d  e = FALSE;.    }
f88a0 0a 0a 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65  ..    /* De-refe
f88b0 72 65 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20  rence and close 
f88c0 6f 75 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20  our copy of the 
f88d0 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61  shared memory ha
f88e0 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61  ndle */.    Unma
f88f0 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c  pViewOfFile(pFil
f8900 65 2d 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20  e->shared);.    
f8910 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c  CloseHandle(pFil
f8920 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20  e->hShared);..  
f8930 20 20 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74    /* Done with t
f8940 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20  he mutex */.    
f8950 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73  winceMutexReleas
f8960 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  e(pFile->hMutex)
f8970 3b 20 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48  ;    .    CloseH
f8980 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75  andle(pFile->hMu
f8990 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  tex);.    pFile-
f89a0 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a  >hMutex = NULL;.
f89b0 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e    }.}../* .** An
f89c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
f89d0 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28  of the LockFile(
f89e0 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73  ) API of windows
f89f0 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74   for wince.*/.st
f8a00 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c  atic BOOL winceL
f8a10 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c  ockFile(.  HANDL
f8a20 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f  E *phFile,.  DWO
f8a30 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  RD dwFileOffsetL
f8a40 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69  ow,.  DWORD dwFi
f8a50 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20  leOffsetHigh,.  
f8a60 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42  DWORD nNumberOfB
f8a70 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20  ytesToLockLow,. 
f8a80 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66   DWORD nNumberOf
f8a90 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a  BytesToLockHigh.
f8aa0 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  ){.  winFile *pF
f8ab0 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f  ile = HANDLE_TO_
f8ac0 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b  WINFILE(phFile);
f8ad0 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20  .  BOOL bReturn 
f8ae0 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 53  = FALSE;..  UNUS
f8af0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77 46  ED_PARAMETER(dwF
f8b00 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b 0a  ileOffsetHigh);.
f8b10 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
f8b20 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  ER(nNumberOfByte
f8b30 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20  sToLockHigh);.. 
f8b40 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75   if (!pFile->hMu
f8b50 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 45  tex) return TRUE
f8b60 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63  ;.  winceMutexAc
f8b70 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75  quire(pFile->hMu
f8b80 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57 61 6e 74  tex);..  /* Want
f8b90 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ing an exclusive
f8ba0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69 66 20 28   lock? */.  if (
f8bb0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
f8bc0 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44  == (DWORD)SHARED
f8bd0 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 26  _FIRST.       &&
f8be0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
f8bf0 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f  oLockLow == (DWO
f8c00 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 7b  RD)SHARED_SIZE){
f8c10 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f8c20 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73  shared->nReaders
f8c30 20 3d 3d 20 30 20 26 26 20 70 46 69 6c 65 2d 3e   == 0 && pFile->
f8c40 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69  shared->bExclusi
f8c50 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 20  ve == 0){.      
f8c60 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
f8c70 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55  bExclusive = TRU
f8c80 45 3b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d  E;.       pFile-
f8c90 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76  >local.bExclusiv
f8ca0 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20  e = TRUE;.      
f8cb0 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b   bReturn = TRUE;
f8cc0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
f8cd0 20 57 61 6e 74 20 61 20 72 65 61 64 2d 6f 6e 6c   Want a read-onl
f8ce0 79 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73  y lock? */.  els
f8cf0 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73  e if (dwFileOffs
f8d00 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29  etLow == (DWORD)
f8d10 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a  SHARED_FIRST &&.
f8d20 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62             nNumb
f8d30 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
f8d40 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66  ow == 1){.    if
f8d50 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d   (pFile->shared-
f8d60 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30  >bExclusive == 0
f8d70 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
f8d80 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b  local.nReaders +
f8d90 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69  +;.      if (pFi
f8da0 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65  le->local.nReade
f8db0 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20  rs == 1){.      
f8dc0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f8dd0 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20  >nReaders ++;.  
f8de0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74      }.      bRet
f8df0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
f8e00 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
f8e10 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f   a pending lock?
f8e20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
f8e30 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f8e40 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e 47  = (DWORD)PENDING
f8e50 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72  _BYTE && nNumber
f8e60 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77  OfBytesToLockLow
f8e70 20 3d 3d 20 31 29 7b 0a 20 20 20 20 2f 2a 20 49   == 1){.    /* I
f8e80 66 20 6e 6f 20 70 65 6e 64 69 6e 67 20 6c 6f 63  f no pending loc
f8e90 6b 20 68 61 73 20 62 65 65 6e 20 61 63 71 75 69  k has been acqui
f8ea0 72 65 64 2c 20 74 68 65 6e 20 61 63 71 75 69 72  red, then acquir
f8eb0 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28  e it */.    if (
f8ec0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f8ed0 50 65 6e 64 69 6e 67 20 3d 3d 20 30 29 20 7b 0a  Pending == 0) {.
f8ee0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f8ef0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20  red->bPending = 
f8f00 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 69 6c  TRUE;.      pFil
f8f10 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e  e->local.bPendin
f8f20 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20  g = TRUE;.      
f8f30 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a  bReturn = TRUE;.
f8f40 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
f8f50 57 61 6e 74 20 61 20 72 65 73 65 72 76 65 64 20  Want a reserved 
f8f60 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20  lock? */.  else 
f8f70 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74  if (dwFileOffset
f8f80 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45  Low == (DWORD)RE
f8f90 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e  SERVED_BYTE && n
f8fa0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
f8fb0 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20  ockLow == 1){.  
f8fc0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61    if (pFile->sha
f8fd0 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d  red->bReserved =
f8fe0 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69  = 0) {.      pFi
f8ff0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73  le->shared->bRes
f9000 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20  erved = TRUE;.  
f9010 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
f9020 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55  .bReserved = TRU
f9030 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
f9040 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
f9050 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78   }..  winceMutex
f9060 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68  Release(pFile->h
f9070 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  Mutex);.  return
f9080 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a   bReturn;.}../*.
f9090 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ** An implementa
f90a0 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c 6f  tion of the Unlo
f90b0 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77 69  ckFile API of wi
f90c0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a  ndows for wince.
f90d0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77  */.static BOOL w
f90e0 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 0a  inceUnlockFile(.
f90f0 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65    HANDLE *phFile
f9100 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65  ,.  DWORD dwFile
f9110 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f  OffsetLow,.  DWO
f9120 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48  RD dwFileOffsetH
f9130 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75  igh,.  DWORD nNu
f9140 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c  mberOfBytesToUnl
f9150 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20  ockLow,.  DWORD 
f9160 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f9170 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20  UnlockHigh.){.  
f9180 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
f9190 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49   HANDLE_TO_WINFI
f91a0 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f  LE(phFile);.  BO
f91b0 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c  OL bReturn = FAL
f91c0 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41  SE;..  UNUSED_PA
f91d0 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66  RAMETER(dwFileOf
f91e0 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e 55  fsetHigh);.  UNU
f91f0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e  SED_PARAMETER(nN
f9200 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
f9210 6c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 69 66  lockHigh);..  if
f9220 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78   (!pFile->hMutex
f9230 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20  ) return TRUE;. 
f9240 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69   winceMutexAcqui
f9250 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  re(pFile->hMutex
f9260 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69  );..  /* Releasi
f9270 6e 67 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b  ng a reader lock
f9280 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65   or an exclusive
f9290 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64   lock */.  if (d
f92a0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f92b0 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f  = (DWORD)SHARED_
f92c0 46 49 52 53 54 29 7b 0a 20 20 20 20 2f 2a 20 44  FIRST){.    /* D
f92d0 69 64 20 77 65 20 68 61 76 65 20 61 6e 20 65 78  id we have an ex
f92e0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f  clusive lock? */
f92f0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f9300 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65  local.bExclusive
f9310 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
f9320 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f9330 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57  UnlockLow == (DW
f9340 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29  ORD)SHARED_SIZE)
f9350 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
f9360 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20  ocal.bExclusive 
f9370 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70  = FALSE;.      p
f9380 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45  File->shared->bE
f9390 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45  xclusive = FALSE
f93a0 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  ;.      bReturn 
f93b0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20  = TRUE;.    }.. 
f93c0 20 20 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73     /* Did we jus
f93d0 74 20 68 61 76 65 20 61 20 72 65 61 64 65 72 20  t have a reader 
f93e0 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73  lock? */.    els
f93f0 65 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63  e if (pFile->loc
f9400 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20  al.nReaders){.  
f9410 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62      assert(nNumb
f9420 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63  erOfBytesToUnloc
f9430 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53  kLow == (DWORD)S
f9440 48 41 52 45 44 5f 53 49 5a 45 20 7c 7c 20 6e 4e  HARED_SIZE || nN
f9450 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
f9460 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0a 20  lockLow == 1);. 
f9470 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
f9480 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20  l.nReaders --;. 
f9490 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e       if (pFile->
f94a0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d  local.nReaders =
f94b0 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 20 20 20  = 0).      {.   
f94c0 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
f94d0 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b  ed->nReaders --;
f94e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
f94f0 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20  Return = TRUE;. 
f9500 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52     }.  }..  /* R
f9510 65 6c 65 61 73 69 6e 67 20 61 20 70 65 6e 64 69  eleasing a pendi
f9520 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73  ng lock */.  els
f9530 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73  e if (dwFileOffs
f9540 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29  etLow == (DWORD)
f9550 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20  PENDING_BYTE && 
f9560 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f9570 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  UnlockLow == 1){
f9580 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f9590 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b  local.bPending){
f95a0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
f95b0 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 46  cal.bPending = F
f95c0 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c  ALSE;.      pFil
f95d0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
f95e0 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ing = FALSE;.   
f95f0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
f9600 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  E;.    }.  }.  /
f9610 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65  * Releasing a re
f9620 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20  served lock */. 
f9630 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65   else if (dwFile
f9640 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57  OffsetLow == (DW
f9650 4f 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 54  ORD)RESERVED_BYT
f9660 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
f9670 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d  tesToUnlockLow =
f9680 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46  = 1){.    if (pF
f9690 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65  ile->local.bRese
f96a0 72 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46  rved) {.      pF
f96b0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65  ile->local.bRese
f96c0 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20  rved = FALSE;.  
f96d0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
f96e0 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46  d->bReserved = F
f96f0 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74  ALSE;.      bRet
f9700 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
f9710 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75  }.  }..  winceMu
f9720 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65  texRelease(pFile
f9730 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74  ->hMutex);.  ret
f9740 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a  urn bReturn;.}..
f9750 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65  /*.** An impleme
f9760 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c  ntation of the L
f9770 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20  ockFileEx() API 
f9780 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77  of windows for w
f9790 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42  ince.*/.static B
f97a0 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c  OOL winceLockFil
f97b0 65 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70  eEx(.  HANDLE *p
f97c0 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64  hFile,.  DWORD d
f97d0 77 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20  wFlags,.  DWORD 
f97e0 64 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57  dwReserved,.  DW
f97f0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
f9800 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44  esToLockLow,.  D
f9810 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79  WORD nNumberOfBy
f9820 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20  tesToLockHigh,. 
f9830 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70   LPOVERLAPPED lp
f9840 4f 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20  Overlapped.){.  
f9850 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f9860 28 64 77 52 65 73 65 72 76 65 64 29 3b 0a 20 20  (dwReserved);.  
f9870 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f9880 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54  (nNumberOfBytesT
f9890 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 2f  oLockHigh);..  /
f98a0 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
f98b0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72  wants a shared r
f98c0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72  ead lock, forwar
f98d0 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a  d this call.  **
f98e0 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c   to winceLockFil
f98f0 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65  e */.  if (lpOve
f9900 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20  rlapped->Offset 
f9910 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44  == (DWORD)SHARED
f9920 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20  _FIRST &&.      
f9930 64 77 46 6c 61 67 73 20 3d 3d 20 31 20 26 26 0a  dwFlags == 1 &&.
f9940 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42        nNumberOfB
f9950 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d  ytesToLockLow ==
f9960 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53   (DWORD)SHARED_S
f9970 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  IZE){.    return
f9980 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70   winceLockFile(p
f9990 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  hFile, SHARED_FI
f99a0 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  RST, 0, 1, 0);. 
f99b0 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53   }.  return FALS
f99c0 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f  E;.}./*.** End o
f99d0 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f  f the special co
f99e0 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a  de for wince.***
f99f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
f9a40 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f  if /* SQLITE_OS_
f9a50 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  WINCE */../*****
f9a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9aa0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
f9ab0 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f  next group of ro
f9ac0 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74  utines implement
f9ad0 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
f9ae0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79   specified.** by
f9af0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f   the sqlite3_io_
f9b00 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a  methods object..
f9b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
f9b60 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66  ./*.** Close a f
f9b70 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  ile..**.** It is
f9b80 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 61   reported that a
f9b90 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c 6f  n attempt to clo
f9ba0 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 68  se a handle migh
f9bb0 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 66  t sometimes.** f
f9bc0 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 20  ail.  This is a 
f9bd0 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c  very unreasonabl
f9be0 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77 69  e result, but wi
f9bf0 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f  ndows is notorio
f9c00 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20  us.** for being 
f9c10 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20  unreasonable so 
f9c20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74  I do not doubt t
f9c30 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 70  hat it might hap
f9c40 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20  pen.  If.** the 
f9c50 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20  close fails, we 
f9c60 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69  pause for 100 mi
f9c70 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74  lliseconds and t
f9c80 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a 2a  ry again.  As.**
f9c90 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53   many as MX_CLOS
f9ca0 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70  E_ATTEMPT attemp
f9cb0 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20  ts to close the 
f9cc0 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20  handle are made 
f9cd0 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e 67  before.** giving
f9ce0 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e   up and returnin
f9cf0 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23  g an error..*/.#
f9d00 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f  define MX_CLOSE_
f9d10 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 63  ATTEMPT 3.static
f9d20 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 71   int winClose(sq
f9d30 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
f9d40 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d  .  int rc, cnt =
f9d50 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70   0;.  winFile *p
f9d60 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
f9d70 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  )id;..  assert( 
f9d80 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41  id!=0 );.  OSTRA
f9d90 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22  CE2("CLOSE %d\n"
f9da0 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64  , pFile->h);.  d
f9db0 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f 73  o{.    rc = Clos
f9dc0 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68  eHandle(pFile->h
f9dd0 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d  );.  }while( rc=
f9de0 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d 58  =0 && ++cnt < MX
f9df0 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 26  _CLOSE_ATTEMPT &
f9e00 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31  & (Sleep(100), 1
f9e10 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  ) );.#if SQLITE_
f9e20 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69 6e 65  OS_WINCE.#define
f9e30 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f   WINCE_DELETION_
f9e40 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77 69 6e  ATTEMPTS 3.  win
f9e50 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46  ceDestroyLock(pF
f9e60 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c  ile);.  if( pFil
f9e70 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73  e->zDeleteOnClos
f9e80 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74  e ){.    int cnt
f9e90 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28   = 0;.    while(
f9ea0 0a 20 20 20 20 20 20 20 20 20 20 20 44 65 6c 65  .           Dele
f9eb0 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e 7a  teFileW(pFile->z
f9ec0 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3d 3d  DeleteOnClose)==
f9ed0 30 0a 20 20 20 20 20 20 20 20 26 26 20 47 65 74  0.        && Get
f9ee0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28  FileAttributesW(
f9ef0 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e  pFile->zDeleteOn
f9f00 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66 66 66  Close)!=0xffffff
f9f10 66 66 20 0a 20 20 20 20 20 20 20 20 26 26 20 63  ff .        && c
f9f20 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 45 4c  nt++ < WINCE_DEL
f9f30 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 0a 20  ETION_ATTEMPTS. 
f9f40 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53 6c 65     ){.       Sle
f9f50 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57 61 69  ep(100);  /* Wai
f9f60 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66 6f 72  t a little befor
f9f70 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e 20 2a  e trying again *
f9f80 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65  /.    }.    free
f9f90 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f  (pFile->zDeleteO
f9fa0 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 6e  nClose);.  }.#en
f9fb0 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65  dif.  OpenCounte
f9fc0 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e 20  r(-1);.  return 
f9fd0 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  rc ? SQLITE_OK :
f9fe0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d   SQLITE_IOERR;.}
f9ff0 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63  ../*.** Some mic
fa000 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73  rosoft compilers
fa010 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e   lack this defin
fa020 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ition..*/.#ifnde
fa030 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  f INVALID_SET_FI
fa040 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 66  LE_POINTER.# def
fa050 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f  ine INVALID_SET_
fa060 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 44  FILE_POINTER ((D
fa070 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a  WORD)-1).#endif.
fa080 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
fa090 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74   from a file int
fa0a0 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74  o a buffer.  Ret
fa0b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
fa0c0 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65   all.** bytes we
fa0d0 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66  re read successf
fa0e0 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f  ully and SQLITE_
fa0f0 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e  IOERR if anythin
fa100 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e  g goes.** wrong.
fa110 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
fa120 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65  inRead(.  sqlite
fa130 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20  3_file *id,     
fa140 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20       /* File to 
fa150 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76  read from */.  v
fa160 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20  oid *pBuf,      
fa170 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
fa180 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20  te content into 
fa190 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20  this buffer */. 
fa1a0 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20   int amt,       
fa1b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
fa1c0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
fa1d0 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69  o read */.  sqli
fa1e0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
fa1f0 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20         /* Begin 
fa200 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20  reading at this 
fa210 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4c  offset */.){.  L
fa220 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d 20  ONG upperBits = 
fa230 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e  (LONG)((offset>>
fa240 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66  32) & 0x7fffffff
fa250 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42  );.  LONG lowerB
fa260 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66  its = (LONG)(off
fa270 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66 66  set & 0xffffffff
fa280 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20  );.  DWORD rc;. 
fa290 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
fa2a0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
fa2b0 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20    DWORD error;. 
fa2c0 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 20 61   DWORD got;..  a
fa2d0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
fa2e0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
fa2f0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
fa300 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 4f  IOERR_READ);.  O
fa310 53 54 52 41 43 45 33 28 22 52 45 41 44 20 25 64  STRACE3("READ %d
fa320 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69   lock=%d\n", pFi
fa330 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f  le->h, pFile->lo
fa340 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20  cktype);.  rc = 
fa350 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70  SetFilePointer(p
fa360 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69  File->h, lowerBi
fa370 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20  ts, &upperBits, 
fa380 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69  FILE_BEGIN);.  i
fa390 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53  f( rc==INVALID_S
fa3a0 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20  ET_FILE_POINTER 
fa3b0 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73  && (error=GetLas
fa3c0 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52  tError())!=NO_ER
fa3d0 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  ROR ){.    pFile
fa3e0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
fa3f0 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ror;.    return 
fa400 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d  SQLITE_FULL;.  }
fa410 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c 65  .  if( !ReadFile
fa420 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c  (pFile->h, pBuf,
fa430 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20 29   amt, &got, 0) )
fa440 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
fa450 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
fa460 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74  Error();.    ret
fa470 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fa480 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28  _READ;.  }.  if(
fa490 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74   got==(DWORD)amt
fa4a0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
fa4b0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
fa4c0 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64  e{.    /* Unread
fa4d0 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75   parts of the bu
fa4e0 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72  ffer must be zer
fa4f0 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20  o-filled */.    
fa500 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29  memset(&((char*)
fa510 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61  pBuf)[got], 0, a
fa520 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74  mt-got);.    ret
fa530 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fa540 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d  _SHORT_READ;.  }
fa550 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
fa560 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66  data from a buff
fa570 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20  er into a file. 
fa580 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
fa590 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20  K on success.** 
fa5a0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72  or some other er
fa5b0 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c  ror code on fail
fa5c0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
fa5d0 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73  nt winWrite(.  s
fa5e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fa5f0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
fa600 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a   to write into *
fa610 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
fa620 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a  pBuf,         /*
fa630 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 65   The bytes to be
fa640 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e   written */.  in
fa650 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20  t amt,          
fa660 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
fa670 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72  r of bytes to wr
fa680 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ite */.  sqlite3
fa690 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20  _int64 offset   
fa6a0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
fa6b0 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65  o the file to be
fa6c0 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a  gin writing at *
fa6d0 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65  /.){.  LONG uppe
fa6e0 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28  rBits = (LONG)((
fa6f0 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78  offset>>32) & 0x
fa700 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e  7fffffff);.  LON
fa710 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c  G lowerBits = (L
fa720 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 78  ONG)(offset & 0x
fa730 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f  ffffffff);.  DWO
fa740 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65  RD rc;.  winFile
fa750 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
fa760 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20  le*)id;.  DWORD 
fa770 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 77  error;.  DWORD w
fa780 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73  rote = 0;..  ass
fa790 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
fa7a0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
fa7b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
fa7c0 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69  ERR_WRITE);.  Si
fa7d0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
fa7e0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
fa7f0 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41  E_FULL);.  OSTRA
fa800 43 45 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f  CE3("WRITE %d lo
fa810 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
fa820 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
fa830 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74  ype);.  rc = Set
fa840 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c  FilePointer(pFil
fa850 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c  e->h, lowerBits,
fa860 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c   &upperBits, FIL
fa870 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20  E_BEGIN);.  if( 
fa880 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f  rc==INVALID_SET_
fa890 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20  FILE_POINTER && 
fa8a0 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72  (error=GetLastEr
fa8b0 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52  ror())!=NO_ERROR
fa8c0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
fa8d0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72  astErrno = error
fa8e0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
fa8f0 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20  ITE_FULL;.  }.  
fa900 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b  assert( amt>0 );
fa910 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 61  .  while(.     a
fa920 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 72 63  mt>0.     && (rc
fa930 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 46 69   = WriteFile(pFi
fa940 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74  le->h, pBuf, amt
fa950 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 3d 30  , &wrote, 0))!=0
fa960 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 3e 30  .     && wrote>0
fa970 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d  .  ){.    amt -=
fa980 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66   wrote;.    pBuf
fa990 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66   = &((char*)pBuf
fa9a0 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20  )[wrote];.  }.  
fa9b0 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e 28  if( !rc || amt>(
fa9c0 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 20 20  int)wrote ){.   
fa9d0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
fa9e0 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  o = GetLastError
fa9f0 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ();.    return S
faa00 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a  QLITE_FULL;.  }.
faa10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
faa20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
faa30 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69  ncate an open fi
faa40 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65  le to a specifie
faa50 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63  d size.*/.static
faa60 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65   int winTruncate
faa70 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
faa80 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
faa90 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 20   nByte){.  LONG 
faaa0 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e  upperBits = (LON
faab0 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 26  G)((nByte>>32) &
faac0 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20   0x7fffffff);.  
faad0 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d  LONG lowerBits =
faae0 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 20   (LONG)(nByte & 
faaf0 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44  0xffffffff);.  D
fab00 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69  WORD rc;.  winFi
fab10 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
fab20 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
fab30 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65  D error;..  asse
fab40 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f  rt( id!=0 );.  O
fab50 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41 54  STRACE3("TRUNCAT
fab60 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46  E %d %lld\n", pF
fab70 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b 0a  ile->h, nByte);.
fab80 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
fab90 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
faba0 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b  IOERR_TRUNCATE);
fabb0 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50  .  rc = SetFileP
fabc0 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c  ointer(pFile->h,
fabd0 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70   lowerBits, &upp
fabe0 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47  erBits, FILE_BEG
fabf0 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49  IN);.  if( rc==I
fac00 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f  NVALID_SET_FILE_
fac10 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f  POINTER && (erro
fac20 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  r=GetLastError()
fac30 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  )!=NO_ERROR ){. 
fac40 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
fac50 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20  rno = error;.   
fac60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
fac70 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20  OERR_TRUNCATE;. 
fac80 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f 66   }.  /* SetEndOf
fac90 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c 20 69  File will fail i
faca0 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67 61 74  f nByte is negat
facb0 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21 53 65  ive */.  if( !Se
facc0 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69 6c 65  tEndOfFile(pFile
facd0 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46 69 6c  ->h) ){.    pFil
face0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
facf0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fad00 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fad10 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b  _IOERR_TRUNCATE;
fad20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
fad30 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64  LITE_OK;.}..#ifd
fad40 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f  ef SQLITE_TEST./
fad50 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e  *.** Count the n
fad60 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e  umber of fullsyn
fad70 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79  cs and normal sy
fad80 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 73  ncs.  This is us
fad90 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68  ed to test.** th
fada0 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c  at syncs and ful
fadb0 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72  lsyncs are occur
fadc0 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 74  ing at the right
fadd0 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   times..*/.SQLIT
fade0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
fadf0 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30  3_sync_count = 0
fae00 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
fae10 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e   sqlite3_fullsyn
fae20 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  c_count = 0;.#en
fae30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  dif../*.** Make 
fae40 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20  sure all writes 
fae50 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  to a particular 
fae60 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74  file are committ
fae70 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73  ed to disk..*/.s
fae80 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e  tatic int winSyn
fae90 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  c(sqlite3_file *
faea0 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  id, int flags){.
faeb0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e  #ifndef SQLITE_N
faec0 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c 65  O_SYNC.  winFile
faed0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
faee0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72  le*)id;..  asser
faef0 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53  t( id!=0 );.  OS
faf00 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 64 20  TRACE3("SYNC %d 
faf10 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c  lock=%d\n", pFil
faf20 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  e->h, pFile->loc
faf30 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a 20 20  ktype);.#else.  
faf40 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
faf50 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  (id);.#endif.#if
faf60 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  ndef SQLITE_TEST
faf70 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
faf80 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65 6c 73  TER(flags);.#els
faf90 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  e.  if( flags & 
fafa0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
fafb0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
fafc0 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  fullsync_count++
fafd0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
fafe0 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  sync_count++;.#e
faff0 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 20  ndif.  /* If we 
fb000 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
fb010 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  e SQLITE_NO_SYNC
fb020 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63   flag, then sync
fb030 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f  ing is a.  ** no
fb040 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  -op.  */.#ifdef 
fb050 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20  SQLITE_NO_SYNC. 
fb060 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fb070 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28  _OK;.#else.  if(
fb080 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72   FlushFileBuffer
fb090 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20  s(pFile->h) ){. 
fb0a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fb0b0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
fb0c0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
fb0d0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  no = GetLastErro
fb0e0 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  r();.    return 
fb0f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20  SQLITE_IOERR;.  
fb100 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
fb110 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
fb120 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  current size of 
fb130 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a  a file in bytes.
fb140 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
fb150 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  nFileSize(sqlite
fb160 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  3_file *id, sqli
fb170 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65  te3_int64 *pSize
fb180 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70 65 72  ){.  DWORD upper
fb190 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20 6c 6f  Bits;.  DWORD lo
fb1a0 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e 46 69  werBits;.  winFi
fb1b0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
fb1c0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
fb1d0 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65  D error;..  asse
fb1e0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53  rt( id!=0 );.  S
fb1f0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72  imulateIOError(r
fb200 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
fb210 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f 77  RR_FSTAT);.  low
fb220 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 6c 65  erBits = GetFile
fb230 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20 26  Size(pFile->h, &
fb240 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 69 66  upperBits);.  if
fb250 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 20 3d  (   (lowerBits =
fb260 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 53  = INVALID_FILE_S
fb270 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28 28 65  IZE).     && ((e
fb280 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72  rror = GetLastEr
fb290 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45 52 52  ror()) != NO_ERR
fb2a0 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20 70 46  OR) ).  {.    pF
fb2b0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
fb2c0 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75   error;.    retu
fb2d0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
fb2e0 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53  FSTAT;.  }.  *pS
fb2f0 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 65 33  ize = (((sqlite3
fb300 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 74 73  _int64)upperBits
fb310 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 42 69  )<<32) + lowerBi
fb320 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ts;.  return SQL
fb330 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
fb340 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
fb350 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 75 6e  MMEDIATELY is un
fb360 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20  defined on some 
fb370 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e  Windows systems.
fb380 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b  .*/.#ifndef LOCK
fb390 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49  FILE_FAIL_IMMEDI
fb3a0 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c  ATELY.# define L
fb3b0 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d  OCKFILE_FAIL_IMM
fb3c0 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 69  EDIATELY 1.#endi
fb3d0 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65  f../*.** Acquire
fb3e0 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a   a reader lock..
fb3f0 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 50 49  ** Different API
fb400 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61   routines are ca
fb410 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f  lled depending o
fb420 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  n whether or not
fb430 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 6e 39   this.** is Win9
fb440 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 73  5 or WinNT..*/.s
fb450 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 61  tatic int getRea
fb460 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70  dLock(winFile *p
fb470 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73  File){.  int res
fb480 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29  ;.  if( isNT() )
fb490 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44  {.    OVERLAPPED
fb4a0 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c 70 2e   ovlp;.    ovlp.
fb4b0 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f  Offset = SHARED_
fb4c0 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c 70 2e  FIRST;.    ovlp.
fb4d0 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 3b 0a  OffsetHigh = 0;.
fb4e0 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e 74 20      ovlp.hEvent 
fb4f0 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c  = 0;.    res = L
fb500 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c 65 2d  ockFileEx(pFile-
fb510 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49  >h, LOCKFILE_FAI
fb520 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c 0a 20  L_IMMEDIATELY,. 
fb530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb540 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 53 49      0, SHARED_SI
fb550 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0a 2f  ZE, 0, &ovlp);./
fb560 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66  * isNT() is 1 if
fb570 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
fb580 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73  ==1, so this els
fb590 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75  e is never execu
fb5a0 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c  ted. .*/.#if SQL
fb5b0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
fb5c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
fb5d0 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   lk;.    sqlite3
fb5e0 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65  _randomness(size
fb5f0 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20  of(lk), &lk);.  
fb600 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c    pFile->sharedL
fb610 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f 72 74  ockByte = (short
fb620 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66  )((lk & 0x7fffff
fb630 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45  ff)%(SHARED_SIZE
fb640 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65 73 20   - 1));.    res 
fb650 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
fb660 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
fb670 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c  T+pFile->sharedL
fb680 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30  ockByte, 0, 1, 0
fb690 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
fb6a0 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a  if( res == 0 ){.
fb6b0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
fb6c0 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72  rrno = GetLastEr
fb6d0 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ror();.  }.  ret
fb6e0 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn res;.}../*.*
fb6f0 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63  * Undo a readloc
fb700 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  k.*/.static int 
fb710 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 77  unlockReadLock(w
fb720 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  inFile *pFile){.
fb730 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28    int res;.  if(
fb740 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 72   isNT() ){.    r
fb750 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  es = UnlockFile(
fb760 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fb770 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45  _FIRST, 0, SHARE
fb780 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69  D_SIZE, 0);./* i
fb790 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51  sNT() is 1 if SQ
fb7a0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31  LITE_OS_WINCE==1
fb7b0 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69  , so this else i
fb7c0 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64  s never executed
fb7d0 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  . .*/.#if SQLITE
fb7e0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d  _OS_WINCE==0.  }
fb7f0 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20  else{.    res = 
fb800 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  UnlockFile(pFile
fb810 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
fb820 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 72 65  T + pFile->share
fb830 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c  dLockByte, 0, 1,
fb840 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a   0);.#endif.  }.
fb850 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29    if( res == 0 )
fb860 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
fb870 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
fb880 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72  Error();.  }.  r
fb890 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a  eturn res;.}../*
fb8a0 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c  .** Lock the fil
fb8b0 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20  e with the lock 
fb8c0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72  specified by par
fb8d0 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20  ameter locktype 
fb8e0 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20  - one.** of the 
fb8f0 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
fb900 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f       (1) SHARED_
fb910 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20  LOCK.**     (2) 
fb920 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a  RESERVED_LOCK.**
fb930 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47       (3) PENDING
fb940 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29  _LOCK.**     (4)
fb950 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a   EXCLUSIVE_LOCK.
fb960 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20  **.** Sometimes 
fb970 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20  when requesting 
fb980 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20  one lock state, 
fb990 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20  additional lock 
fb9a0 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e  states.** are in
fb9b0 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65  serted in betwee
fb9c0 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20  n.  The locking 
fb9d0 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e  might fail on on
fb9e0 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a  e of the later.*
fb9f0 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65  * transitions le
fba00 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73  aving the lock s
fba10 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66  tate different f
fba20 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72  rom what it star
fba30 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c  ted but.** still
fba40 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f   short of its go
fba50 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  al.  The followi
fba60 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74  ng chart shows t
fba70 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72  he allowed.** tr
fba80 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68  ansitions and th
fba90 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72  e inserted inter
fbaa0 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a  mediate states:.
fbab0 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45  **.**    UNLOCKE
fbac0 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20  D -> SHARED.**  
fbad0 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45    SHARED -> RESE
fbae0 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  RVED.**    SHARE
fbaf0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
fbb00 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
fbb10 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50    RESERVED -> (P
fbb20 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
fbb30 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49  SIVE.**    PENDI
fbb40 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  NG -> EXCLUSIVE.
fbb50 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
fbb60 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63  ne will only inc
fbb70 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54  rease a lock.  T
fbb80 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72  he winUnlock() r
fbb90 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73  outine.** erases
fbba0 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e   all locks at on
fbbb0 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75  ce and returns u
fbbc0 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  s immediately to
fbbd0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30   locking level 0
fbbe0 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70  ..** It is not p
fbbf0 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72  ossible to lower
fbc00 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
fbc10 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61  el one step at a
fbc20 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d   time.  You.** m
fbc30 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20  ust go straight 
fbc40 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  to locking level
fbc50 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   0..*/.static in
fbc60 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65  t winLock(sqlite
fbc70 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
fbc80 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74  locktype){.  int
fbc90 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
fbca0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
fbcb0 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69  de from subrouti
fbcc0 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73  nes */.  int res
fbcd0 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20   = 1;           
fbce0 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77  /* Result of a w
fbcf0 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c  indows lock call
fbd00 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63   */.  int newLoc
fbd10 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20  ktype;       /* 
fbd20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  Set pFile->lockt
fbd30 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75  ype to this valu
fbd40 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67  e before exiting
fbd50 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e   */.  int gotPen
fbd60 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20  dingLock = 0;/* 
fbd70 54 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69  True if we acqui
fbd80 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  red a PENDING lo
fbd90 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a  ck this time */.
fbda0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
fbdb0 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
fbdc0 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d  .  DWORD error =
fbdd0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61 73   NO_ERROR;..  as
fbde0 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
fbdf0 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20   OSTRACE5("LOCK 
fbe00 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 29  %d %d was %d(%d)
fbe10 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  \n",.          p
fbe20 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
fbe30 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  e, pFile->lockty
fbe40 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65  pe, pFile->share
fbe50 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f  dLockByte);..  /
fbe60 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c  * If there is al
fbe70 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20  ready a lock of 
fbe80 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72  this type or mor
fbe90 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e  e restrictive on
fbea0 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65   the.  ** OsFile
fbeb0 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f  , do nothing. Do
fbec0 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f  n't use the end_
fbed0 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c  lock: exit path,
fbee0 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33   as.  ** sqlite3
fbef0 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68  OsEnterMutex() h
fbf00 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65  asn't been calle
fbf10 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  d yet..  */.  if
fbf20 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
fbf30 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  e>=locktype ){. 
fbf40 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fbf50 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  _OK;.  }..  /* M
fbf60 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
fbf70 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
fbf80 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20   correct.  */.  
fbf90 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
fbfa0 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
fbfb0 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   || locktype==SH
fbfc0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
fbfd0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
fbfe0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b  =PENDING_LOCK );
fbff0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
fc000 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype!=RESERVED_LO
fc010 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK || pFile->loc
fc020 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
fc030 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b  CK );..  /* Lock
fc040 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   the PENDING_LOC
fc050 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65  K byte if we nee
fc060 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50  d to acquire a P
fc070 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20  ENDING lock or. 
fc080 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63   ** a SHARED loc
fc090 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63  k.  If we are ac
fc0a0 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44  quiring a SHARED
fc0b0 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69   lock, the acqui
fc0c0 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74  sition of.  ** t
fc0d0 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  he PENDING_LOCK 
fc0e0 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72  byte is temporar
fc0f0 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63  y..  */.  newLoc
fc100 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c  ktype = pFile->l
fc110 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 20  ocktype;.  if(  
fc120 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
fc130 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 20 20  e==NO_LOCK).    
fc140 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74 79 70   || (   (locktyp
fc150 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
fc160 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  K).         && (
fc170 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
fc180 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 29  =RESERVED_LOCK))
fc190 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e  .  ){.    int cn
fc1a0 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 6c 65  t = 3;.    while
fc1b0 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72 65  ( cnt-->0 && (re
fc1c0 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69  s = LockFile(pFi
fc1d0 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42  le->h, PENDING_B
fc1e0 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d 3d  YTE, 0, 1, 0))==
fc1f0 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72  0 ){.      /* Tr
fc200 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74  y 3 times to get
fc210 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63   the pending loc
fc220 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20  k.  The pending 
fc230 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a 20 20  lock might be.  
fc240 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 61      ** held by a
fc250 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 70 72  nother reader pr
fc260 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c 20 72  ocess who will r
fc270 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e 74  elease it moment
fc280 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a  arily..      */.
fc290 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22        OSTRACE2("
fc2a0 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20  could not get a 
fc2b0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e  PENDING lock. cn
fc2c0 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a 20  t=%d\n", cnt);. 
fc2d0 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20       Sleep(1);. 
fc2e0 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e 64     }.    gotPend
fc2f0 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a 20  ingLock = res;. 
fc300 20 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a 20     if( !res ){. 
fc310 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74       error = Get
fc320 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
fc330 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71   }.  }..  /* Acq
fc340 75 69 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f  uire a shared lo
fc350 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
fc360 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
fc370 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20  OCK && res ){.  
fc380 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
fc390 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f  >locktype==NO_LO
fc3a0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20  CK );.    res = 
fc3b0 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
fc3c0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20  e);.    if( res 
fc3d0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
fc3e0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f  type = SHARED_LO
fc3f0 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  CK;.    }else{. 
fc400 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74       error = Get
fc410 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
fc420 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71   }.  }..  /* Acq
fc430 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20  uire a RESERVED 
fc440 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
fc450 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56  locktype==RESERV
fc460 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29  ED_LOCK && res )
fc470 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
fc480 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
fc490 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
fc4a0 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65    res = LockFile
fc4b0 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52  (pFile->h, RESER
fc4c0 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20  VED_BYTE, 0, 1, 
fc4d0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20  0);.    if( res 
fc4e0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
fc4f0 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f  type = RESERVED_
fc500 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
fc510 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47  .      error = G
fc520 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fc530 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
fc540 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47  cquire a PENDING
fc550 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
fc560 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
fc570 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
fc580 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74   ){.    newLockt
fc590 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ype = PENDING_LO
fc5a0 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69  CK;.    gotPendi
fc5b0 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a  ngLock = 0;.  }.
fc5c0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e  .  /* Acquire an
fc5d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a   EXCLUSIVE lock.
fc5e0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
fc5f0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
fc600 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20  OCK && res ){.  
fc610 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
fc620 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45  >locktype>=SHARE
fc630 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65  D_LOCK );.    re
fc640 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  s = unlockReadLo
fc650 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f  ck(pFile);.    O
fc660 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 64 6c  STRACE2("unreadl
fc670 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73  ock = %d\n", res
fc680 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63  );.    res = Loc
fc690 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fc6a0 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
fc6b0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29   SHARED_SIZE, 0)
fc6c0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
fc6d0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
fc6e0 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c  pe = EXCLUSIVE_L
fc6f0 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  OCK;.    }else{.
fc700 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65        error = Ge
fc710 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
fc720 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 65 72      OSTRACE2("er
fc730 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22  ror-code = %d\n"
fc740 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20 20 20  , error);.      
fc750 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
fc760 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  e);.    }.  }.. 
fc770 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f   /* If we are ho
fc780 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20  lding a PENDING 
fc790 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20  lock that ought 
fc7a0 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20  to be released, 
fc7b0 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73  then.  ** releas
fc7c0 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20  e it now..  */. 
fc7d0 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c   if( gotPendingL
fc7e0 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d  ock && locktype=
fc7f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
fc800 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70      UnlockFile(p
fc810 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
fc820 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
fc830 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
fc840 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
fc850 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64  he lock has held
fc860 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73   in the file des
fc870 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a  criptor then.  *
fc880 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70  * return the app
fc890 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20  ropriate result 
fc8a0 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  code..  */.  if(
fc8b0 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 20 3d   res ){.    rc =
fc8c0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
fc8d0 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45  lse{.    OSTRACE
fc8e0 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25  4("LOCK FAILED %
fc8f0 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20  d trying for %d 
fc900 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70  but got %d\n", p
fc910 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
fc920 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65      locktype, ne
fc930 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20  wLocktype);.    
fc940 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
fc950 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 63   = error;.    rc
fc960 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
fc970 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63    }.  pFile->loc
fc980 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65 77 4c  ktype = (u8)newL
fc990 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72  ocktype;.  retur
fc9a0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
fc9b0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
fc9c0 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ks if there is a
fc9d0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68   RESERVED lock h
fc9e0 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69  eld on the speci
fc9f0 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20  fied.** file by 
fca00 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65  this or any othe
fca10 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75  r process. If su
fca20 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c  ch a lock is hel
fca30 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e  d, return.** non
fca40 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65  -zero, otherwise
fca50 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63   zero..*/.static
fca60 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 73   int winCheckRes
fca70 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
fca80 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
fca90 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74  *pResOut){.  int
fcaa0 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a   rc;.  winFile *
fcab0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
fcac0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28  *)id;..  assert(
fcad0 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20   id!=0 );.  if( 
fcae0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
fcaf0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29  =RESERVED_LOCK )
fcb00 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  {.    rc = 1;.  
fcb10 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 54    OSTRACE3("TEST
fcb20 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28   WR-LOCK %d %d (
fcb30 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65  local)\n", pFile
fcb40 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73  ->h, rc);.  }els
fcb50 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b  e{.    rc = Lock
fcb60 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52  File(pFile->h, R
fcb70 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c  ESERVED_BYTE, 0,
fcb80 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   1, 0);.    if( 
fcb90 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f  rc ){.      Unlo
fcba0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
fcbb0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
fcbc0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  0, 1, 0);.    }.
fcbd0 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a 20 20      rc = !rc;.  
fcbe0 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 54    OSTRACE3("TEST
fcbf0 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28   WR-LOCK %d %d (
fcc00 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c  remote)\n", pFil
fcc10 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20  e->h, rc);.  }. 
fcc20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a   *pResOut = rc;.
fcc30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fcc40 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77  OK;.}../*.** Low
fcc50 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
fcc60 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
fcc70 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f  criptor id to lo
fcc80 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
fcc90 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
fcca0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
fccb0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
fccc0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
fccd0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
fcce0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
fccf0 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
fcd00 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
fcd10 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
fcd20 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
fcd30 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  e is a no-op..**
fcd40 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f  .** It is not po
fcd50 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20  ssible for this 
fcd60 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20  routine to fail 
fcd70 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  if the second ar
fcd80 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f  gument.** is NO_
fcd90 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65  LOCK.  If the se
fcda0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
fcdb0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65   SHARED_LOCK the
fcdc0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
fcdd0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53  * might return S
fcde0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a  QLITE_IOERR;.*/.
fcdf0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 55 6e  static int winUn
fce00 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
fce10 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
fce20 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79 70 65  ype){.  int type
fce30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  ;.  winFile *pFi
fce40 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
fce50 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  d;.  int rc = SQ
fce60 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72  LITE_OK;.  asser
fce70 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20  t( pFile!=0 );. 
fce80 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
fce90 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
fcea0 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e  ;.  OSTRACE5("UN
fceb0 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61  LOCK %d to %d wa
fcec0 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70 46 69  s %d(%d)\n", pFi
fced0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
fcee0 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  .          pFile
fcef0 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  ->locktype, pFil
fcf00 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e->sharedLockByt
fcf10 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69  e);.  type = pFi
fcf20 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  le->locktype;.  
fcf30 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53  if( type>=EXCLUS
fcf40 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  IVE_LOCK ){.    
fcf50 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  UnlockFile(pFile
fcf60 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
fcf70 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a  T, 0, SHARED_SIZ
fcf80 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c  E, 0);.    if( l
fcf90 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
fcfa0 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65 61 64  LOCK && !getRead
fcfb0 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20  Lock(pFile) ){. 
fcfc0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f       /* This sho
fcfd0 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
fcfe0 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77  .  We should alw
fcff0 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20  ays be able to. 
fd000 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72       ** reacquir
fd010 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20  e the read lock 
fd020 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  */.      rc = SQ
fd030 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
fd040 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  K;.    }.  }.  i
fd050 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45  f( type>=RESERVE
fd060 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e  D_LOCK ){.    Un
fd070 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
fd080 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  h, RESERVED_BYTE
fd090 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  , 0, 1, 0);.  }.
fd0a0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
fd0b0 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e  NO_LOCK && type>
fd0c0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
fd0d0 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f      unlockReadLo
fd0e0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20  ck(pFile);.  }. 
fd0f0 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49   if( type>=PENDI
fd100 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55  NG_LOCK ){.    U
fd110 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d  nlockFile(pFile-
fd120 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  >h, PENDING_BYTE
fd130 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  , 0, 1, 0);.  }.
fd140 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
fd150 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65  e = (u8)locktype
fd160 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
fd170 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20  ../*.** Control 
fd180 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65  and query of the
fd190 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c   open file handl
fd1a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
fd1b0 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 28   winFileControl(
fd1c0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fd1d0 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
fd1e0 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28  pArg){.  switch(
fd1f0 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
fd200 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
fd210 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  KSTATE: {.      
fd220 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28  *(int*)pArg = ((
fd230 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  winFile*)id)->lo
fd240 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65  cktype;.      re
fd250 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
fd260 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
fd270 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f  QLITE_LAST_ERRNO
fd280 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a  : {.      *(int*
fd290 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28 28 77  )pArg = (int)((w
fd2a0 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  inFile*)id)->las
fd2b0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65  tErrno;.      re
fd2c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
fd2d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
fd2e0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
fd2f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
fd300 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
fd310 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65   in bytes of the
fd320 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63   underlying bloc
fd330 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20  k device for.** 
fd340 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69  the specified fi
fd350 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f  le. This is almo
fd360 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79  st always 512 by
fd370 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a  tes, but may be.
fd380 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f  ** larger for so
fd390 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a  me devices..**.*
fd3a0 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73  * SQLite code as
fd3b0 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74  sumes this funct
fd3c0 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e  ion cannot fail.
fd3d0 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73   It also assumes
fd3e0 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20   that.** if two 
fd3f0 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65  files are create
fd400 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69  d in the same fi
fd410 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74  le-system direct
fd420 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64  ory (i.e..** a d
fd430 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20  atabase and its 
fd440 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68  journal file) th
fd450 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  at the sector si
fd460 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a  ze will be the.*
fd470 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e  * same for both.
fd480 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
fd490 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  inSectorSize(sql
fd4a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a  ite3_file *id){.
fd4b0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
fd4c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  );.  return (int
fd4d0 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29  )(((winFile*)id)
fd4e0 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 7d  ->sectorSize);.}
fd4f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
fd500 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63   vector of devic
fd510 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
fd520 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
fd530 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63   winDeviceCharac
fd540 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65  teristics(sqlite
fd550 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 55  3_file *id){.  U
fd560 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
fd570 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  id);.  return 0;
fd580 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76  .}../*.** This v
fd590 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c  ector defines al
fd5a0 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68  l the methods th
fd5b0 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f  at can operate o
fd5c0 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  n an.** sqlite3_
fd5d0 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e 0a  file for win32..
fd5e0 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
fd5f0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
fd600 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 3d  ds winIoMethod =
fd610 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20   {.  1,         
fd620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
fd630 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20  * iVersion */.  
fd640 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e 52  winClose,.  winR
fd650 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 2c  ead,.  winWrite,
fd660 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c 0a  .  winTruncate,.
fd670 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 6e    winSync,.  win
fd680 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e 4c  FileSize,.  winL
fd690 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 6b  ock,.  winUnlock
fd6a0 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73 65  ,.  winCheckRese
fd6b0 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e 46  rvedLock,.  winF
fd6c0 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 69  ileControl,.  wi
fd6d0 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 77  nSectorSize,.  w
fd6e0 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65  inDeviceCharacte
fd6f0 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a  ristics.};../***
fd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd740 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65  ********.** Here
fd750 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65   ends the I/O me
fd760 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20  thods that form 
fd770 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  the sqlite3_io_m
fd780 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a  ethods object..*
fd790 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c  *.** The next bl
fd7a0 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c  ock of code impl
fd7b0 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d  ements the VFS m
fd7c0 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a  ethods..********
fd7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd810 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  ****/../*.** Con
fd820 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69 6c  vert a UTF-8 fil
fd830 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 65  ename into whate
fd840 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e 64  ver form the und
fd850 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 61  erlying.** opera
fd860 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e 74  ting system want
fd870 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e 20  s filenames in. 
fd880 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
fd890 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20  he result.** is 
fd8a0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
fd8b0 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65  lloc and must be
fd8c0 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61   freed by the ca
fd8d0 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f  lling.** functio
fd8e0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
fd8f0 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 69  d *convertUtf8Fi
fd900 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 61  lename(const cha
fd910 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
fd920 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
fd930 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 4e  d = 0;.  if( isN
fd940 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e 76  T() ){.    zConv
fd950 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55 6e  erted = utf8ToUn
fd960 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29  icode(zFilename)
fd970 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31  ;./* isNT() is 1
fd980 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   if SQLITE_OS_WI
fd990 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20  NCE==1, so this 
fd9a0 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78  else is never ex
fd9b0 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20  ecuted. .*/.#if 
fd9c0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
fd9d0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
fd9e0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66  zConverted = utf
fd9f0 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 6d  8ToMbcs(zFilenam
fda00 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20  e);.#endif.  }. 
fda10 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20   /* caller will 
fda20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d 65  handle out of me
fda30 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 72 6e  mory */.  return
fda40 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a 0a   zConverted;.}..
fda50 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74  /*.** Create a t
fda60 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61  emporary file na
fda70 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75  me in zBuf.  zBu
fda80 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e  f must be big en
fda90 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20  ough to.** hold 
fdaa0 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  at pVfs->mxPathn
fdab0 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a  ame characters..
fdac0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
fdad0 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42  tTempname(int nB
fdae0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b  uf, char *zBuf){
fdaf0 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a  .  static char z
fdb00 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61  Chars[] =.    "a
fdb10 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71  bcdefghijklmnopq
fdb20 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22  rstuvwxyz".    "
fdb30 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50  ABCDEFGHIJKLMNOP
fdb40 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20  QRSTUVWXYZ".    
fdb50 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20  "0123456789";.  
fdb60 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20 63  size_t i, j;.  c
fdb70 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d 41  har zTempPath[MA
fdb80 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66 28  X_PATH+1];.  if(
fdb90 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
fdba0 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 73  rectory ){.    s
fdbb0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
fdbc0 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65  MAX_PATH-30, zTe
fdbd0 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 73 71  mpPath, "%s", sq
fdbe0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
fdbf0 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20 69  tory);.  }else i
fdc00 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
fdc10 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20   char *zMulti;. 
fdc20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 50 61     WCHAR zWidePa
fdc30 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20  th[MAX_PATH];.  
fdc40 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 28 4d    GetTempPathW(M
fdc50 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 64  AX_PATH-30, zWid
fdc60 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c  ePath);.    zMul
fdc70 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74  ti = unicodeToUt
fdc80 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20  f8(zWidePath);. 
fdc90 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b     if( zMulti ){
fdca0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
fdcb0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48  nprintf(MAX_PATH
fdcc0 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20  -30, zTempPath, 
fdcd0 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20  "%s", zMulti);. 
fdce0 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c 74 69       free(zMulti
fdcf0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
fdd00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
fdd10 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 2f  E_NOMEM;.    }./
fdd20 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66  * isNT() is 1 if
fdd30 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
fdd40 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73  ==1, so this els
fdd50 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75  e is never execu
fdd60 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74  ted. .** Since t
fdd70 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e  he ASCII version
fdd80 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77   of these Window
fdd90 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69  s API do not exi
fdda0 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a  st for WINCE,.**
fddb0 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20   it's important 
fddc0 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65  to not reference
fddd0 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20   them for WINCE 
fdde0 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53  builds..*/.#if S
fddf0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
fde00 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  0.  }else{.    c
fde10 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 20  har *zUtf8;.    
fde20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b 4d  char zMbcsPath[M
fde30 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65  AX_PATH];.    Ge
fde40 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f 50  tTempPathA(MAX_P
fde50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74  ATH-30, zMbcsPat
fde60 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d 20  h);.    zUtf8 = 
fde70 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62  sqlite3_win32_mb
fde80 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63 73  cs_to_utf8(zMbcs
fde90 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a  Path);.    if( z
fdea0 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73 71  Utf8 ){.      sq
fdeb0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d  lite3_snprintf(M
fdec0 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d  AX_PATH-30, zTem
fded0 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 74  pPath, "%s", zUt
fdee0 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28  f8);.      free(
fdef0 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c 73  zUtf8);.    }els
fdf00 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
fdf10 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
fdf20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
fdf30 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 53 74   for(i=sqlite3St
fdf40 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68  rlen30(zTempPath
fdf50 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50  ); i>0 && zTempP
fdf60 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20  ath[i-1]=='\\'; 
fdf70 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61  i--){}.  zTempPa
fdf80 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c  th[i] = 0;.  sql
fdf90 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42  ite3_snprintf(nB
fdfa0 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20  uf-30, zBuf,.   
fdfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fdfc0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d  "%s\\"SQLITE_TEM
fdfd0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
fdfe0 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d  TempPath);.  j =
fdff0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
fe000 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 65  (zBuf);.  sqlite
fe010 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 2c  3_randomness(20,
fe020 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 6f   &zBuf[j]);.  fo
fe030 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b 2b  r(i=0; i<20; i++
fe040 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66  , j++){.    zBuf
fe050 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61  [j] = (char)zCha
fe060 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63  rs[ ((unsigned c
fe070 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69  har)zBuf[j])%(si
fe080 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20  zeof(zChars)-1) 
fe090 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d  ];.  }.  zBuf[j]
fe0a0 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32   = 0;.  OSTRACE2
fe0b0 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a  ("TEMP FILENAME:
fe0c0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20   %s\n", zBuf);. 
fe0d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
fe0e0 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  K; .}../*.** The
fe0f0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
fe100 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67   getLastErrorMsg
fe110 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74  .** is zero if t
fe120 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
fe130 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66   fits in the buf
fe140 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f  fer, or non-zero
fe150 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69  .** otherwise (i
fe160 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61  f the message wa
fe170 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f  s truncated)..*/
fe180 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c  .static int getL
fe190 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 20  astErrorMsg(int 
fe1a0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
fe1b0 29 7b 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65  ){.  /* FormatMe
fe1c0 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 20  ssage returns 0 
fe1d0 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68  on failure.  Oth
fe1e0 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72  erwise it.  ** r
fe1f0 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
fe200 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 74  r of TCHARs writ
fe210 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ten to the outpu
fe220 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65  t.  ** buffer, e
fe230 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72  xcluding the ter
fe240 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68  minating null ch
fe250 61 72 2e 0a 20 20 2a 2f 0a 20 20 44 57 4f 52 44  ar..  */.  DWORD
fe260 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74   error = GetLast
fe270 45 72 72 6f 72 28 29 3b 0a 20 20 44 57 4f 52 44  Error();.  DWORD
fe280 20 64 77 4c 65 6e 20 3d 20 30 3b 0a 20 20 63 68   dwLen = 0;.  ch
fe290 61 72 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 0a 20  ar *zOut = 0;.. 
fe2a0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
fe2b0 20 20 20 57 43 48 41 52 20 2a 7a 54 65 6d 70 57     WCHAR *zTempW
fe2c0 69 64 65 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ide = NULL;.    
fe2d0 64 77 4c 65 6e 20 3d 20 46 6f 72 6d 61 74 4d 65  dwLen = FormatMe
fe2e0 73 73 61 67 65 57 28 46 4f 52 4d 41 54 5f 4d 45  ssageW(FORMAT_ME
fe2f0 53 53 41 47 45 5f 41 4c 4c 4f 43 41 54 45 5f 42  SSAGE_ALLOCATE_B
fe300 55 46 46 45 52 20 7c 20 46 4f 52 4d 41 54 5f 4d  UFFER | FORMAT_M
fe310 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54  ESSAGE_FROM_SYST
fe320 45 4d 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53  EM | FORMAT_MESS
fe330 41 47 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52  AGE_IGNORE_INSER
fe340 54 53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  TS,.            
fe350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e                 N
fe360 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ULL,.           
fe370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe380 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  error,.         
fe390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe3a0 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20    0,.           
fe3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe3c0 28 4c 50 57 53 54 52 29 20 26 7a 54 65 6d 70 57  (LPWSTR) &zTempW
fe3d0 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ide,.           
fe3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe3f0 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
fe400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29                0)
fe410 3b 0a 20 20 20 20 69 66 28 20 64 77 4c 65 6e 20  ;.    if( dwLen 
fe420 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  > 0 ){.      /* 
fe430 61 6c 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65  allocate a buffe
fe440 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f  r and convert to
fe450 20 55 54 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a   UTF8 */.      z
fe460 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55  Out = unicodeToU
fe470 74 66 38 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a  tf8(zTempWide);.
fe480 20 20 20 20 20 20 2f 2a 20 66 72 65 65 20 74 68        /* free th
fe490 65 20 73 79 73 74 65 6d 20 62 75 66 66 65 72 20  e system buffer 
fe4a0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 46 6f 72  allocated by For
fe4b0 6d 61 74 4d 65 73 73 61 67 65 20 2a 2f 0a 20 20  matMessage */.  
fe4c0 20 20 20 20 4c 6f 63 61 6c 46 72 65 65 28 7a 54      LocalFree(zT
fe4d0 65 6d 70 57 69 64 65 29 3b 0a 20 20 20 20 7d 0a  empWide);.    }.
fe4e0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
fe4f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
fe500 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
fe510 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
fe520 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20  uted. .** Since 
fe530 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f  the ASCII versio
fe540 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f  n of these Windo
fe550 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78  ws API do not ex
fe560 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a  ist for WINCE,.*
fe570 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74  * it's important
fe580 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63   to not referenc
fe590 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45  e them for WINCE
fe5a0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20   builds..*/.#if 
fe5b0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
fe5c0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
fe5d0 63 68 61 72 20 2a 7a 54 65 6d 70 20 3d 20 4e 55  char *zTemp = NU
fe5e0 4c 4c 3b 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20  LL;.    dwLen = 
fe5f0 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46  FormatMessageA(F
fe600 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c  ORMAT_MESSAGE_AL
fe610 4c 4f 43 41 54 45 5f 42 55 46 46 45 52 20 7c 20  LOCATE_BUFFER | 
fe620 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46  FORMAT_MESSAGE_F
fe630 52 4f 4d 5f 53 59 53 54 45 4d 20 7c 20 46 4f 52  ROM_SYSTEM | FOR
fe640 4d 41 54 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f  MAT_MESSAGE_IGNO
fe650 52 45 5f 49 4e 53 45 52 54 53 2c 0a 20 20 20 20  RE_INSERTS,.    
fe660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe670 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20         NULL,.   
fe680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe690 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c 0a 20          error,. 
fe6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe6b0 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
fe6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe6d0 20 20 20 20 20 20 20 20 28 4c 50 53 54 52 29 20          (LPSTR) 
fe6e0 26 7a 54 65 6d 70 2c 0a 20 20 20 20 20 20 20 20  &zTemp,.        
fe6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe700 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20     0,.          
fe710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe720 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64 77 4c   0);.    if( dwL
fe730 65 6e 20 3e 20 30 20 29 7b 0a 20 20 20 20 20 20  en > 0 ){.      
fe740 2f 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 75  /* allocate a bu
fe750 66 66 65 72 20 61 6e 64 20 63 6f 6e 76 65 72 74  ffer and convert
fe760 20 74 6f 20 55 54 46 38 20 2a 2f 0a 20 20 20 20   to UTF8 */.    
fe770 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33    zOut = sqlite3
fe780 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75  _win32_mbcs_to_u
fe790 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20  tf8(zTemp);.    
fe7a0 20 20 2f 2a 20 66 72 65 65 20 74 68 65 20 73 79    /* free the sy
fe7b0 73 74 65 6d 20 62 75 66 66 65 72 20 61 6c 6c 6f  stem buffer allo
fe7c0 63 61 74 65 64 20 62 79 20 46 6f 72 6d 61 74 4d  cated by FormatM
fe7d0 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20  essage */.      
fe7e0 4c 6f 63 61 6c 46 72 65 65 28 7a 54 65 6d 70 29  LocalFree(zTemp)
fe7f0 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
fe800 20 7d 0a 20 20 69 66 28 20 30 20 3d 3d 20 64 77   }.  if( 0 == dw
fe810 4c 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Len ){.    sqlit
fe820 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
fe830 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72  , zBuf, "OsError
fe840 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 72   0x%x (%u)", err
fe850 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d 65  or, error);.  }e
fe860 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 63 6f 70 79  lse{.    /* copy
fe870 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 6e 42   a maximum of nB
fe880 75 66 20 63 68 61 72 73 20 74 6f 20 6f 75 74 70  uf chars to outp
fe890 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20  ut buffer */.   
fe8a0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
fe8b0 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 25  f(nBuf, zBuf, "%
fe8c0 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2f  s", zOut);.    /
fe8d0 2a 20 66 72 65 65 20 74 68 65 20 55 54 46 38 20  * free the UTF8 
fe8e0 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 66 72  buffer */.    fr
fe8f0 65 65 28 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20  ee(zOut);.  }.  
fe900 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
fe910 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a  ** Open a file..
fe920 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
fe930 6e 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  nOpen(.  sqlite3
fe940 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
fe950 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a     /* Not used *
fe960 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
fe970 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a  zName,        /*
fe980 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
fe990 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73  e (UTF-8) */.  s
fe9a0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fe9b0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
fe9c0 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c  e the SQLite fil
fe9d0 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f  e handle here */
fe9e0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
fe9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fea00 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20  Open mode flags 
fea10 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  */.  int *pOutFl
fea20 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
fea30 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20  * Status return 
fea40 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41  flags */.){.  HA
fea50 4e 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20  NDLE h;.  DWORD 
fea60 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b  dwDesiredAccess;
fea70 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65  .  DWORD dwShare
fea80 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77  Mode;.  DWORD dw
fea90 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
feaa0 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46  ion;.  DWORD dwF
feab0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
feac0 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  s = 0;.#if SQLIT
fead0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74  E_OS_WINCE.  int
feae0 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e   isTemp = 0;.#en
feaf0 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  dif.  winFile *p
feb00 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
feb10 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  )id;.  void *zCo
feb20 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20  nverted;        
feb30 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
feb40 6e 61 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64  name in OS encod
feb50 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ing */.  const c
feb60 68 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d  har *zUtf8Name =
feb70 20 7a 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69   zName;    /* Fi
feb80 6c 65 6e 61 6d 65 20 69 6e 20 55 54 46 2d 38 20  lename in UTF-8 
feb90 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68  encoding */.  ch
feba0 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f  ar zTmpname[MAX_
febb0 50 41 54 48 2b 31 5d 3b 20 20 20 20 20 20 20 20  PATH+1];        
febc0 2f 2a 20 42 75 66 66 65 72 20 75 73 65 64 20 74  /* Buffer used t
febd0 6f 20 63 72 65 61 74 65 20 74 65 6d 70 20 66 69  o create temp fi
febe0 6c 65 6e 61 6d 65 20 2a 2f 0a 0a 20 20 61 73 73  lename */..  ass
febf0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
fec00 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
fec10 28 70 56 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66  (pVfs);..  /* If
fec20 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
fec30 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
fec40 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67  ction is NULL, g
fec50 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20  enerate a .  ** 
fec60 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
fec70 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f  ame to use .  */
fec80 0a 20 20 69 66 28 20 21 7a 55 74 66 38 4e 61 6d  .  if( !zUtf8Nam
fec90 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  e ){.    int rc 
feca0 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41  = getTempname(MA
fecb0 58 5f 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61  X_PATH+1, zTmpna
fecc0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  me);.    if( rc!
fecd0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
fece0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
fecf0 20 20 20 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61     }.    zUtf8Na
fed00 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20  me = zTmpname;. 
fed10 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74   }..  /* Convert
fed20 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f   the filename to
fed30 20 74 68 65 20 73 79 73 74 65 6d 20 65 6e 63 6f   the system enco
fed40 64 69 6e 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76  ding. */.  zConv
fed50 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55  erted = convertU
fed60 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66  tf8Filename(zUtf
fed70 38 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43  8Name);.  if( zC
fed80 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20  onverted==0 ){. 
fed90 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
feda0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69  _NOMEM;.  }..  i
fedb0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
fedc0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
fedd0 20 29 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65   ){.    dwDesire
fede0 64 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49  dAccess = GENERI
fedf0 43 5f 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43  C_READ | GENERIC
fee00 5f 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b  _WRITE;.  }else{
fee10 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63  .    dwDesiredAc
fee20 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52  cess = GENERIC_R
fee30 45 41 44 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 51  EAD;.  }.  /* SQ
fee40 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
fee50 49 56 45 20 69 73 20 75 73 65 64 20 74 6f 20 6d  IVE is used to m
fee60 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 20  ake sure that a 
fee70 6e 65 77 20 66 69 6c 65 20 69 73 20 0a 20 20 2a  new file is .  *
fee80 2a 20 63 72 65 61 74 65 64 2e 20 53 51 4c 69 74  * created. SQLit
fee90 65 20 64 6f 65 73 6e 27 74 20 75 73 65 20 69 74  e doesn't use it
feea0 20 74 6f 20 69 6e 64 69 63 61 74 65 20 22 65 78   to indicate "ex
feeb0 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 22 20  clusive access" 
feec0 0a 20 20 2a 2a 20 61 73 20 69 74 20 69 73 20 75  .  ** as it is u
feed0 73 75 61 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f  sually understoo
feee0 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  d..  */.  assert
feef0 28 21 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  (!(flags & SQLIT
fef00 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
fef10 29 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 53 51  ) || (flags & SQ
fef20 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
fef30 29 29 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20  ));.  if( flags 
fef40 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58  & SQLITE_OPEN_EX
fef50 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 2f  CLUSIVE ){.    /
fef60 2a 20 43 72 65 61 74 65 73 20 61 20 6e 65 77 20  * Creates a new 
fef70 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74  file, only if it
fef80 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64   does not alread
fef90 79 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20  y exist. */.    
fefa0 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 65  /* If the file e
fefb0 78 69 73 74 73 2c 20 69 74 20 66 61 69 6c 73 2e  xists, it fails.
fefc0 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69   */.    dwCreati
fefd0 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20  onDisposition = 
fefe0 43 52 45 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65  CREATE_NEW;.  }e
feff0 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
ff000 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
ff010 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65  TE ){.    /* Ope
ff020 6e 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c  n existing file,
ff030 20 6f 72 20 63 72 65 61 74 65 20 69 66 20 69 74   or create if it
ff040 20 64 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a   doesn't exist *
ff050 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e  /.    dwCreation
ff060 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50  Disposition = OP
ff070 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c  EN_ALWAYS;.  }el
ff080 73 65 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73  se{.    /* Opens
ff090 20 61 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66   a file, only if
ff0a0 20 69 74 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20   it exists. */. 
ff0b0 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73     dwCreationDis
ff0c0 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f  position = OPEN_
ff0d0 45 58 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20  EXISTING;.  }.  
ff0e0 64 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49  dwShareMode = FI
ff0f0 4c 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20  LE_SHARE_READ | 
ff100 46 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45  FILE_SHARE_WRITE
ff110 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
ff120 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
ff130 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 66  TEONCLOSE ){.#if
ff140 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ff150 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41  .    dwFlagsAndA
ff160 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45  ttributes = FILE
ff170 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 45  _ATTRIBUTE_HIDDE
ff180 4e 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d 20  N;.    isTemp = 
ff190 31 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77 46  1;.#else.    dwF
ff1a0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
ff1b0 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55  s = FILE_ATTRIBU
ff1c0 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20 20  TE_TEMPORARY.   
ff1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff1e0 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 49              | FI
ff1f0 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44  LE_ATTRIBUTE_HID
ff200 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20  DEN.            
ff210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff220 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44     | FILE_FLAG_D
ff230 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a  ELETE_ON_CLOSE;.
ff240 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a  #endif.  }else{.
ff250 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74      dwFlagsAndAt
ff260 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f  tributes = FILE_
ff270 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c  ATTRIBUTE_NORMAL
ff280 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f 72  ;.  }.  /* Repor
ff290 74 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65  ts from the inte
ff2a0 72 6e 65 74 20 61 72 65 20 74 68 61 74 20 70 65  rnet are that pe
ff2b0 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c 77  rformance is alw
ff2c0 61 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72 20  ays.  ** better 
ff2d0 69 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e  if FILE_FLAG_RAN
ff2e0 44 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75 73  DOM_ACCESS is us
ff2f0 65 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 39  ed.  Ticket #269
ff300 39 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  9. */.#if SQLITE
ff310 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46 6c  _OS_WINCE.  dwFl
ff320 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73  agsAndAttributes
ff330 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41   |= FILE_FLAG_RA
ff340 4e 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65 6e  NDOM_ACCESS;.#en
ff350 64 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28 29  dif.  if( isNT()
ff360 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61   ){.    h = Crea
ff370 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29  teFileW((WCHAR*)
ff380 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20  zConverted,.    
ff390 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65     dwDesiredAcce
ff3a0 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61  ss,.       dwSha
ff3b0 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e  reMode,.       N
ff3c0 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72  ULL,.       dwCr
ff3d0 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
ff3e0 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67  n,.       dwFlag
ff3f0 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a  sAndAttributes,.
ff400 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20         NULL.    
ff410 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
ff420 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
ff430 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
ff440 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
ff450 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
ff460 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
ff470 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
ff480 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
ff490 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
ff4a0 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
ff4b0 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
ff4c0 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
ff4d0 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
ff4e0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ff4f0 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
ff500 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c     h = CreateFil
ff510 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  eA((char*)zConve
ff520 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44  rted,.       dwD
ff530 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20  esiredAccess,.  
ff540 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65       dwShareMode
ff550 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20  ,.       NULL,. 
ff560 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e        dwCreation
ff570 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20  Disposition,.   
ff580 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74      dwFlagsAndAt
ff590 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20  tributes,.      
ff5a0 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e   NULL.    );.#en
ff5b0 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68 3d  dif.  }.  if( h=
ff5c0 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f  =INVALID_HANDLE_
ff5d0 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72 65  VALUE ){.    fre
ff5e0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
ff5f0 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53     if( flags & S
ff600 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
ff610 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 65  RITE ){.      re
ff620 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66  turn winOpen(pVf
ff630 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20  s, zName, id, . 
ff640 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66 6c              ((fl
ff650 61 67 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ags|SQLITE_OPEN_
ff660 52 45 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54  READONLY)&~SQLIT
ff670 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
ff680 29 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20  ), pOutFlags);. 
ff690 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
ff6a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
ff6b0 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20  NTOPEN;.    }.  
ff6c0 7d 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67  }.  if( pOutFlag
ff6d0 73 20 29 7b 0a 20 20 20 20 69 66 28 20 66 6c 61  s ){.    if( fla
ff6e0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
ff6f0 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20  _READWRITE ){.  
ff700 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d      *pOutFlags =
ff710 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
ff720 44 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73  DWRITE;.    }els
ff730 65 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c  e{.      *pOutFl
ff740 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45  ags = SQLITE_OPE
ff750 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20  N_READONLY;.    
ff760 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70  }.  }.  memset(p
ff770 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  File, 0, sizeof(
ff780 2a 70 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c  *pFile));.  pFil
ff790 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 69  e->pMethod = &wi
ff7a0 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69  nIoMethod;.  pFi
ff7b0 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 46 69  le->h = h;.  pFi
ff7c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
ff7d0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 70 46 69 6c  NO_ERROR;.  pFil
ff7e0 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20  e->sectorSize = 
ff7f0 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 56  getSectorSize(pV
ff800 66 73 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b 0a  fs, zUtf8Name);.
ff810 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
ff820 4e 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73  NCE.  if( (flags
ff830 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   & (SQLITE_OPEN_
ff840 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45  READWRITE|SQLITE
ff850 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20  _OPEN_MAIN_DB)) 
ff860 3d 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ==.             
ff870 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52    (SQLITE_OPEN_R
ff880 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f  EADWRITE|SQLITE_
ff890 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20  OPEN_MAIN_DB).  
ff8a0 20 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 72       && !winceCr
ff8b0 65 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20  eateLock(zName, 
ff8c0 70 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20  pFile).  ){.    
ff8d0 43 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a  CloseHandle(h);.
ff8e0 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
ff8f0 74 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ted);.    return
ff900 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
ff910 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65  ;.  }.  if( isTe
ff920 6d 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  mp ){.    pFile-
ff930 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20  >zDeleteOnClose 
ff940 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20  = zConverted;.  
ff950 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b  }else.#endif.  {
ff960 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65  .    free(zConve
ff970 72 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65  rted);.  }.  Ope
ff980 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20  nCounter(+1);.  
ff990 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
ff9a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
ff9b0 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65  e the named file
ff9c0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
ff9d0 74 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e  t windows does n
ff9e0 6f 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20  ot allow a file 
ff9f0 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 66  to be deleted if
ffa00 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70   some other.** p
ffa10 72 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f 70  rocess has it op
ffa20 65 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61  en.  Sometimes a
ffa30 20 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f   virus scanner o
ffa40 72 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72  r indexing progr
ffa50 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20  am.** will open 
ffa60 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73  a journal file s
ffa70 68 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 20  hortly after it 
ffa80 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72  is created in or
ffa90 64 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61  der to do.** wha
ffaa0 74 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20  tever it does.  
ffab0 57 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72  While this other
ffac0 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
ffad0 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20  ing the.** file 
ffae0 6f 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65  open, we will be
ffaf0 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74   unable to delet
ffb00 65 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61  e it.  To work a
ffb10 72 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72  round this.** pr
ffb20 6f 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20  oblem, we delay 
ffb30 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  100 milliseconds
ffb40 20 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65   and try to dele
ffb50 74 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a  te again.  Up.**
ffb60 20 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f   to MX_DELETION_
ffb70 41 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f  ATTEMPTs deletio
ffb80 6e 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 72  n attempts are r
ffb90 75 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67  un before giving
ffba0 0a 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72  .** up and retur
ffbb0 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  ning an error..*
ffbc0 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c  /.#define MX_DEL
ffbd0 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35  ETION_ATTEMPTS 5
ffbe0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44  .static int winD
ffbf0 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33  elete(.  sqlite3
ffc00 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
ffc10 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64       /* Not used
ffc20 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63   on win32 */.  c
ffc30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
ffc40 6e 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61  name,      /* Na
ffc50 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65  me of file to de
ffc60 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79  lete */.  int sy
ffc70 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20  ncDir           
ffc80 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65        /* Not use
ffc90 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b  d on win32 */.){
ffca0 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a  .  int cnt = 0;.
ffcb0 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57    DWORD rc;.  DW
ffcc0 4f 52 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20  ORD error = 0;. 
ffcd0 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
ffce0 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46  d = convertUtf8F
ffcf0 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d  ilename(zFilenam
ffd00 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e);.  UNUSED_PAR
ffd10 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
ffd20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
ffd30 28 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 28  (syncDir);.  if(
ffd40 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29   zConverted==0 )
ffd50 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
ffd60 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
ffd70 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
ffd80 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49  (return SQLITE_I
ffd90 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20  OERR_DELETE);.  
ffda0 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
ffdb0 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65    do{.      Dele
ffdc0 74 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74  teFileW(zConvert
ffdd0 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  ed);.    }while(
ffde0 20 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65     (   ((rc = Ge
ffdf0 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57  tFileAttributesW
ffe00 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d  (zConverted)) !=
ffe10 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54   INVALID_FILE_AT
ffe20 54 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20  TRIBUTES).      
ffe30 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72           || ((er
ffe40 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72  ror = GetLastErr
ffe50 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41  or()) == ERROR_A
ffe60 43 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20  CCESS_DENIED)). 
ffe70 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b            && (++
ffe80 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f  cnt < MX_DELETIO
ffe90 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20  N_ATTEMPTS).    
ffea0 20 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70         && (Sleep
ffeb0 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20  (100), 1) );./* 
ffec0 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
ffed0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
ffee0 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
ffef0 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
fff00 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65  d. .** Since the
fff10 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f   ASCII version o
fff20 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20  f these Windows 
fff30 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  API do not exist
fff40 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69   for WINCE,.** i
fff50 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  t's important to
fff60 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74   not reference t
fff70 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75  hem for WINCE bu
fff80 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ilds..*/.#if SQL
fff90 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
fffa0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b    }else{.    do{
fffb0 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c  .      DeleteFil
fffc0 65 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  eA(zConverted);.
fffd0 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20      }while(   ( 
fffe0 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65    ((rc = GetFile
ffff0 41 74 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e  AttributesA(zCon
10000 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41  verted)) != INVA
10001 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
10002 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20  TES).           
10003 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d      || ((error =
10004 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29   GetLastError())
10005 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53   == ERROR_ACCESS
10006 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20  _DENIED)).      
10007 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c       && (++cnt <
10008 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54   MX_DELETION_ATT
10009 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20  EMPTS).         
1000a 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29    && (Sleep(100)
1000b 2c 20 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20  , 1) );.#endif. 
1000c 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65   }.  free(zConve
1000d 72 74 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45  rted);.  OSTRACE
1000e 32 28 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22  2("DELETE \"%s\"
1000f 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b  \n", zFilename);
10010 0a 20 20 72 65 74 75 72 6e 20 28 20 20 20 28 72  .  return (   (r
10011 63 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c  c == INVALID_FIL
10012 45 5f 41 54 54 52 49 42 55 54 45 53 29 20 0a 20  E_ATTRIBUTES) . 
10013 20 20 20 20 20 20 20 20 20 26 26 20 28 65 72 72           && (err
10014 6f 72 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45  or == ERROR_FILE
10015 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53  _NOT_FOUND)) ? S
10016 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
10017 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a  E_IOERR_DELETE;.
10018 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  }../*.** Check t
10019 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64  he existance and
1001a 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c   status of a fil
1001b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1001c 20 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71   winAccess(.  sq
1001d 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
1001e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20           /* Not 
1001f 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f  used on win32 */
10020 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
10021 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a  Filename,     /*
10022 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f   Name of file to
10023 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   check */.  int 
10024 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
10025 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f         /* Type o
10026 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f  f test to make o
10027 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20  n this file */. 
10028 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20   int *pResOut   
10029 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1002a 55 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b  UT: Result */.){
1002b 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20  .  DWORD attr;. 
1002c 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 76   int rc = 0;.  v
1002d 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20  oid *zConverted 
1002e 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
1002f 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29  ename(zFilename)
10030 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
10031 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66  ETER(pVfs);.  if
10032 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20  ( zConverted==0 
10033 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
10034 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
10035 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
10036 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69      attr = GetFi
10037 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 28 57  leAttributesW((W
10038 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
10039 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
1003a 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
1003b 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
1003c 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
1003d 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
1003e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
1003f 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
10040 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
10041 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
10042 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
10043 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
10044 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
10045 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
10046 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
10047 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
10048 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c     attr = GetFil
10049 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68  eAttributesA((ch
1004a 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ar*)zConverted);
1004b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72  .#endif.  }.  fr
1004c 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
1004d 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20    switch( flags 
1004e 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
1004f 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a  TE_ACCESS_READ:.
10050 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
10051 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20  ACCESS_EXISTS:. 
10052 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d       rc = attr!=
10053 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
10054 52 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62  RIBUTES;.      b
10055 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
10056 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
10057 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63  DWRITE:.      rc
10058 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f   = (attr & FILE_
10059 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e  ATTRIBUTE_READON
1005a 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72  LY)==0;.      br
1005b 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
1005c 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21  :.      assert(!
1005d 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61  "Invalid flags a
1005e 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20  rgument");.  }. 
1005f 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a   *pResOut = rc;.
10060 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10061 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75  OK;.}.../*.** Tu
10062 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  rn a relative pa
10063 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75  thname into a fu
10064 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72  ll pathname.  Wr
10065 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20  ite the full.** 
10066 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f  pathname into zO
10067 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69  ut[].  zOut[] wi
10068 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70  ll be at least p
10069 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a  Vfs->mxPathname.
1006a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ** bytes in size
1006b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1006c 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  winFullPathname(
1006d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
1006e 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
1006f 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
10070 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63  fs object */.  c
10071 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61  onst char *zRela
10072 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  tive,        /* 
10073 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76  Possibly relativ
10074 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a  e input path */.
10075 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20    int nFull,    
10076 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10077 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75  /* Size of outpu
10078 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65  t buffer in byte
10079 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75  s */.  char *zFu
1007a 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
1007b 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62       /* Output b
1007c 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23  uffer */.){.  .#
1007d 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47  if defined(__CYG
1007e 57 49 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f  WIN__).  UNUSED_
1007f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29  PARAMETER(nFull)
10080 3b 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f  ;.  cygwin_conv_
10081 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61  to_full_win32_pa
10082 74 68 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46  th(zRelative, zF
10083 75 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ull);.  return S
10084 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66  QLITE_OK;.#endif
10085 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
10086 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50  WINCE.  UNUSED_P
10087 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b  ARAMETER(nFull);
10088 0a 20 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20  .  /* WinCE has 
10089 6e 6f 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20  no concept of a 
1008a 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d  relative pathnam
1008b 65 2c 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f  e, or so I am to
1008c 6c 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ld. */.  sqlite3
1008d 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e  _snprintf(pVfs->
1008e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c  mxPathname, zFul
1008f 6c 2c 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69  l, "%s", zRelati
10090 76 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ve);.  return SQ
10091 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a  LITE_OK;.#endif.
10092 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
10093 57 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65  WINCE && !define
10094 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20  d(__CYGWIN__).  
10095 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69  int nByte;.  voi
10096 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20  d *zConverted;. 
10097 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55   char *zOut;.  U
10098 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
10099 6e 46 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65  nFull);.  zConve
1009a 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74  rted = convertUt
1009b 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61  f8Filename(zRela
1009c 74 69 76 65 29 3b 0a 20 20 69 66 28 20 69 73 4e  tive);.  if( isN
1009d 54 28 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52  T() ){.    WCHAR
1009e 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79   *zTemp;.    nBy
1009f 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68  te = GetFullPath
100a0 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43  NameW((WCHAR*)zC
100a1 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20  onverted, 0, 0, 
100a2 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d  0) + 3;.    zTem
100a3 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74  p = malloc( nByt
100a4 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30  e*sizeof(zTemp[0
100a5 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54  ]) );.    if( zT
100a6 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  emp==0 ){.      
100a7 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
100a8 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
100a9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
100aa 20 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61   }.    GetFullPa
100ab 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29  thNameW((WCHAR*)
100ac 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74  zConverted, nByt
100ad 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20  e, zTemp, 0);.  
100ae 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
100af 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75  d);.    zOut = u
100b0 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65  nicodeToUtf8(zTe
100b1 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54  mp);.    free(zT
100b2 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20  emp);./* isNT() 
100b3 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f  is 1 if SQLITE_O
100b4 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74  S_WINCE==1, so t
100b5 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65  his else is neve
100b6 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20  r executed. .** 
100b7 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20  Since the ASCII 
100b8 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65  version of these
100b9 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20   Windows API do 
100ba 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49  not exist for WI
100bb 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70  NCE,.** it's imp
100bc 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65  ortant to not re
100bd 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72  ference them for
100be 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a   WINCE builds..*
100bf 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
100c0 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
100c1 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d  {.    char *zTem
100c2 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47  p;.    nByte = G
100c3 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28  etFullPathNameA(
100c4 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
100c5 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b  d, 0, 0, 0) + 3;
100c6 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c  .    zTemp = mal
100c7 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f  loc( nByte*sizeo
100c8 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20  f(zTemp[0]) );. 
100c9 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20     if( zTemp==0 
100ca 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43  ){.      free(zC
100cb 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20  onverted);.     
100cc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
100cd 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
100ce 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41  GetFullPathNameA
100cf 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74  ((char*)zConvert
100d0 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70  ed, nByte, zTemp
100d1 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a  , 0);.    free(z
100d2 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
100d3 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77  zOut = sqlite3_w
100d4 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66  in32_mbcs_to_utf
100d5 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72  8(zTemp);.    fr
100d6 65 65 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69  ee(zTemp);.#endi
100d7 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74  f.  }.  if( zOut
100d8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
100d9 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d  snprintf(pVfs->m
100da 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c  xPathname, zFull
100db 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20  , "%s", zOut);. 
100dc 20 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20     free(zOut);. 
100dd 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
100de 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
100df 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
100e0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69  NOMEM;.  }.#endi
100e1 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74  f.}../*.** Get t
100e2 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f  he sector size o
100e3 66 20 74 68 65 20 64 65 76 69 63 65 20 75 73 65  f the device use
100e4 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 69  d to store.** fi
100e5 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
100e6 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28  t getSectorSize(
100e7 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
100e8 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73   *pVfs,.    cons
100e9 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
100ea 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66  e     /* UTF-8 f
100eb 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20  ile name */.){. 
100ec 20 44 57 4f 52 44 20 62 79 74 65 73 50 65 72 53   DWORD bytesPerS
100ed 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44  ector = SQLITE_D
100ee 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49  EFAULT_SECTOR_SI
100ef 5a 45 3b 0a 20 20 2f 2a 20 47 65 74 44 69 73 6b  ZE;.  /* GetDisk
100f0 46 72 65 65 53 70 61 63 65 20 69 73 20 6e 6f 74  FreeSpace is not
100f1 20 73 75 70 70 6f 72 74 65 64 20 75 6e 64 65 72   supported under
100f2 20 57 49 4e 43 45 20 2a 2f 0a 23 69 66 20 53 51   WINCE */.#if SQ
100f3 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
100f4 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
100f5 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44  (pVfs);.  UNUSED
100f6 5f 50 41 52 41 4d 45 54 45 52 28 7a 52 65 6c 61  _PARAMETER(zRela
100f7 74 69 76 65 29 3b 0a 23 65 6c 73 65 0a 20 20 63  tive);.#else.  c
100f8 68 61 72 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41  har zFullpath[MA
100f9 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74  X_PATH+1];.  int
100fa 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 64 77 52   rc;.  DWORD dwR
100fb 65 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20  et = 0;.  DWORD 
100fc 64 77 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 0a 20  dwDummy;..  /*. 
100fd 20 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 67   ** We need to g
100fe 65 74 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  et the full path
100ff 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   name of the fil
10100 65 0a 20 20 2a 2a 20 74 6f 20 67 65 74 20 74 68  e.  ** to get th
10101 65 20 64 72 69 76 65 20 6c 65 74 74 65 72 20 74  e drive letter t
10102 6f 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65  o look up the se
10103 63 74 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a  ctor.  ** size..
10104 20 20 2a 2f 0a 20 20 72 63 20 3d 20 77 69 6e 46    */.  rc = winF
10105 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73  ullPathname(pVfs
10106 2c 20 7a 52 65 6c 61 74 69 76 65 2c 20 4d 41 58  , zRelative, MAX
10107 5f 50 41 54 48 2c 20 7a 46 75 6c 6c 70 61 74 68  _PATH, zFullpath
10108 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 53  );.  if( rc == S
10109 51 4c 49 54 45 5f 4f 4b 20 29 0a 20 20 7b 0a 20  QLITE_OK ).  {. 
1010a 20 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72     void *zConver
1010b 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  ted = convertUtf
1010c 38 46 69 6c 65 6e 61 6d 65 28 7a 46 75 6c 6c 70  8Filename(zFullp
1010d 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 43  ath);.    if( zC
1010e 6f 6e 76 65 72 74 65 64 20 29 7b 0a 20 20 20 20  onverted ){.    
1010f 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
10110 20 20 20 20 20 20 20 20 2f 2a 20 74 72 69 6d 20          /* trim 
10111 70 61 74 68 20 74 6f 20 6a 75 73 74 20 64 72 69  path to just dri
10112 76 65 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a  ve reference */.
10113 20 20 20 20 20 20 20 20 57 43 48 41 52 20 2a 70          WCHAR *p
10114 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20   = zConverted;. 
10115 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70         for(;*p;p
10116 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
10117 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b  f( *p == '\\' ){
10118 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 20  .            *p 
10119 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20  = '\0';.        
1011a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1011b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1011c 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 20 3d  .        dwRet =
1011d 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63   GetDiskFreeSpac
1011e 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  eW((WCHAR*)zConv
1011f 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20  erted,.         
10120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10121 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d           &dwDumm
10122 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
10123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10124 20 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65       &bytesPerSe
10125 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  ctor,.          
10126 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10127 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79          &dwDummy
10128 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
10129 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1012a 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20      &dwDummy);. 
1012b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1012c 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68      /* trim path
1012d 20 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72   to just drive r
1012e 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20  eference */.    
1012f 20 20 20 20 63 68 61 72 20 2a 70 20 3d 20 28 63      char *p = (c
10130 68 61 72 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64  har *)zConverted
10131 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a  ;.        for(;*
10132 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  p;p++){.        
10133 20 20 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27    if( *p == '\\'
10134 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
10135 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  *p = '\0';.     
10136 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
10137 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
10138 20 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65    }.        dwRe
10139 74 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53  t = GetDiskFreeS
1013a 70 61 63 65 41 28 28 63 68 61 72 2a 29 7a 43 6f  paceA((char*)zCo
1013b 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20  nverted,.       
1013c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1013d 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75             &dwDu
1013e 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  mmy,.           
1013f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10140 20 20 20 20 20 20 20 26 62 79 74 65 73 50 65 72         &bytesPer
10141 53 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20  Sector,.        
10142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10143 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d            &dwDum
10144 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  my,.            
10145 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10146 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b        &dwDummy);
10147 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
10148 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
10149 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
1014a 64 77 52 65 74 20 29 7b 0a 20 20 20 20 20 20 62  dwRet ){.      b
1014b 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20  ytesPerSector = 
1014c 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53  SQLITE_DEFAULT_S
1014d 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 20  ECTOR_SIZE;.    
1014e 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  }.  }.#endif.  r
1014f 65 74 75 72 6e 20 28 69 6e 74 29 20 62 79 74 65  eturn (int) byte
10150 73 50 65 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a  sPerSector; .}..
10151 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
10152 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
10153 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  ON./*.** Interfa
10154 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
10155 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
10156 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
10157 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
10158 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
10159 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
1015a 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
1015b 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e  ary..*/./*.** In
1015c 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65  terfaces for ope
1015d 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69  ning a shared li
1015e 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65  brary, finding e
1015f 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77  ntry points.** w
10160 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64  ithin the shared
10161 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c   library, and cl
10162 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64  osing the shared
10163 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61   library..*/.sta
10164 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f  tic void *winDlO
10165 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20  pen(sqlite3_vfs 
10166 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61  *pVfs, const cha
10167 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
10168 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69   HANDLE h;.  voi
10169 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  d *zConverted = 
1016a 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
1016b 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ame(zFilename);.
1016c 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
1016d 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20  ER(pVfs);.  if( 
1016e 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b  zConverted==0 ){
1016f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
10170 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20   }.  if( isNT() 
10171 29 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c  ){.    h = LoadL
10172 69 62 72 61 72 79 57 28 28 57 43 48 41 52 2a 29  ibraryW((WCHAR*)
10173 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20  zConverted);./* 
10174 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
10175 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
10176 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
10177 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
10178 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65  d. .** Since the
10179 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f   ASCII version o
1017a 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20  f these Windows 
1017b 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  API do not exist
1017c 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69   for WINCE,.** i
1017d 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  t's important to
1017e 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74   not reference t
1017f 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75  hem for WINCE bu
10180 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ilds..*/.#if SQL
10181 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
10182 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d    }else{.    h =
10183 20 4c 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63   LoadLibraryA((c
10184 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29  har*)zConverted)
10185 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66  ;.#endif.  }.  f
10186 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
10187 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
10188 29 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  )h;.}.static voi
10189 64 20 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c  d winDlError(sql
1018a 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
1018b 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
1018c 7a 42 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53  zBufOut){.  UNUS
1018d 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
1018e 73 29 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72  s);.  getLastErr
1018f 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66  orMsg(nBuf, zBuf
10190 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77  Out);.}.void (*w
10191 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f  inDlSym(sqlite3_
10192 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20  vfs *pVfs, void 
10193 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20  *pHandle, const 
10194 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28  char *zSymbol))(
10195 76 6f 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  void){.  UNUSED_
10196 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
10197 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
10198 49 4e 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65  INCE.  /* The Ge
10199 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 29 20  tProcAddressA() 
1019a 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20  routine is only 
1019b 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e  available on win
1019c 63 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ce. */.  return 
1019d 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47  (void(*)(void))G
1019e 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 28  etProcAddressA((
1019f 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20  HANDLE)pHandle, 
101a0 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a  zSymbol);.#else.
101a1 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77    /* All other w
101a2 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73  indows platforms
101a3 20 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41   expect GetProcA
101a4 64 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65  ddress() to take
101a5 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74  .  ** an Ansi st
101a6 72 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20  ring regardless 
101a7 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20  of the _UNICODE 
101a8 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74  setting */.  ret
101a9 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69  urn (void(*)(voi
101aa 64 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73  d))GetProcAddres
101ab 73 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c  s((HANDLE)pHandl
101ac 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e  e, zSymbol);.#en
101ad 64 69 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c  dif.}.void winDl
101ae 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66  Close(sqlite3_vf
101af 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
101b0 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45  Handle){.  UNUSE
101b1 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
101b2 29 3b 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79  );.  FreeLibrary
101b3 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65  ((HANDLE)pHandle
101b4 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66  );.}.#else /* if
101b5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
101b6 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64  D_EXTENSION is d
101b7 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65  efined: */.  #de
101b8 66 69 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20  fine winDlOpen  
101b9 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44  0.  #define winD
101ba 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69  lError 0.  #defi
101bb 6e 65 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a  ne winDlSym   0.
101bc 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43    #define winDlC
101bd 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a  lose 0.#endif...
101be 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74  /*.** Write up t
101bf 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20  o nBuf bytes of 
101c0 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20  randomness into 
101c1 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  zBuf..*/.static 
101c2 69 6e 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73  int winRandomnes
101c3 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  s(sqlite3_vfs *p
101c4 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63  Vfs, int nBuf, c
101c5 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e  har *zBuf){.  in
101c6 74 20 6e 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45  t n = 0;.  UNUSE
101c7 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
101c8 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  );.#if defined(S
101c9 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20  QLITE_TEST).  n 
101ca 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74  = nBuf;.  memset
101cb 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b  (zBuf, 0, nBuf);
101cc 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 73 69 7a  .#else.  if( siz
101cd 65 6f 66 28 53 59 53 54 45 4d 54 49 4d 45 29 3c  eof(SYSTEMTIME)<
101ce 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53  =nBuf-n ){.    S
101cf 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20  YSTEMTIME x;.   
101d0 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26   GetSystemTime(&
101d1 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  x);.    memcpy(&
101d2 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a  zBuf[n], &x, siz
101d3 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b  eof(x));.    n +
101d4 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d  = sizeof(x);.  }
101d5 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57  .  if( sizeof(DW
101d6 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a  ORD)<=nBuf-n ){.
101d7 20 20 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20      DWORD pid = 
101d8 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73  GetCurrentProces
101d9 73 49 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70  sId();.    memcp
101da 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64  y(&zBuf[n], &pid
101db 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a  , sizeof(pid));.
101dc 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28      n += sizeof(
101dd 70 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  pid);.  }.  if( 
101de 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e  sizeof(DWORD)<=n
101df 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f  Buf-n ){.    DWO
101e0 52 44 20 63 6e 74 20 3d 20 47 65 74 54 69 63 6b  RD cnt = GetTick
101e1 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d  Count();.    mem
101e2 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63  cpy(&zBuf[n], &c
101e3 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 29  nt, sizeof(cnt))
101e4 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ;.    n += sizeo
101e5 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66  f(cnt);.  }.  if
101e6 28 20 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49  ( sizeof(LARGE_I
101e7 4e 54 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20  NTEGER)<=nBuf-n 
101e8 29 7b 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e 54  ){.    LARGE_INT
101e9 45 47 45 52 20 69 3b 0a 20 20 20 20 51 75 65 72  EGER i;.    Quer
101ea 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e  yPerformanceCoun
101eb 74 65 72 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d  ter(&i);.    mem
101ec 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69  cpy(&zBuf[n], &i
101ed 2c 20 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20  , sizeof(i));.  
101ee 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29    n += sizeof(i)
101ef 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
101f0 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn n;.}.../*.
101f1 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c  ** Sleep for a l
101f2 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65  ittle while.  Re
101f3 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  turn the amount 
101f4 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a  of time slept..*
101f5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
101f6 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66  Sleep(sqlite3_vf
101f7 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63  s *pVfs, int mic
101f8 72 6f 73 65 63 29 7b 0a 20 20 53 6c 65 65 70 28  rosec){.  Sleep(
101f9 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31  (microsec+999)/1
101fa 30 30 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  000);.  UNUSED_P
101fb 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
101fc 20 20 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f    return ((micro
101fd 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31  sec+999)/1000)*1
101fe 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  000;.}../*.** Th
101ff 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
10200 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20  able, if set to 
10201 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
10202 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65  , becomes the re
10203 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64  sult.** returned
10204 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43   from sqlite3OsC
10205 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54  urrentTime().  T
10206 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
10207 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64  testing..*/.#ifd
10208 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
10209 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1020a 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
1020b 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  me = 0;.#endif..
1020c 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63  /*.** Find the c
1020d 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20  urrent time (in 
1020e 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69  Universal Coordi
1020f 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72  nated Time).  Wr
10210 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65  ite the.** curre
10211 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65  nt time and date
10212 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79   as a Julian Day
10213 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72   number into *pr
10214 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72  Now and.** retur
10215 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69  n 0.  Return 1 i
10216 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64  f the time and d
10217 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  ate cannot be fo
10218 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43  und..*/.int winC
10219 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
1021a 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f  e3_vfs *pVfs, do
1021b 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20  uble *prNow){.  
1021c 46 49 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f  FILETIME ft;.  /
1021d 2a 20 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63  * FILETIME struc
1021e 74 75 72 65 20 69 73 20 61 20 36 34 2d 62 69 74  ture is a 64-bit
1021f 20 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74   value represent
10220 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ing the number o
10221 66 20 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f  f .     100-nano
10222 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73  second intervals
10223 20 73 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31   since January 1
10224 2c 20 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30  , 1601 (= JD 230
10225 35 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20  5813.5). .  */. 
10226 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74   sqlite3_int64 t
10227 69 6d 65 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65  imeW;   /* Whole
10228 20 64 61 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74   days */.  sqlit
10229 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 46 3b 20  e3_int64 timeF; 
1022a 20 20 2f 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20    /* Fractional 
1022b 44 61 79 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75  Days */..  /* Nu
1022c 6d 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f  mber of 100-nano
1022d 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73  second intervals
1022e 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 79   in a single day
1022f 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
10230 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  st sqlite3_int64
10231 20 6e 74 75 50 65 72 44 61 79 20 3d 20 0a 20 20   ntuPerDay = .  
10232 20 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71      10000000*(sq
10233 6c 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30  lite3_int64)8640
10234 30 3b 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20  0;..  /* Number 
10235 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e  of 100-nanosecon
10236 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 68  d intervals in h
10237 61 6c 66 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a  alf of a day */.
10238 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
10239 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75  qlite3_int64 ntu
1023a 50 65 72 48 61 6c 66 44 61 79 20 3d 20 0a 20 20  PerHalfDay = .  
1023b 20 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71      10000000*(sq
1023c 6c 69 74 65 33 5f 69 6e 74 36 34 29 34 33 32 30  lite3_int64)4320
1023d 30 3b 0a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20  0;..  /* 2^32 - 
1023e 74 6f 20 61 76 6f 69 64 20 75 73 65 20 6f 66 20  to avoid use of 
1023f 4c 4c 20 61 6e 64 20 77 61 72 6e 69 6e 67 73 20  LL and warnings 
10240 69 6e 20 67 63 63 20 2a 2f 0a 20 20 73 74 61 74  in gcc */.  stat
10241 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
10242 5f 69 6e 74 36 34 20 6d 61 78 33 32 42 69 74 56  _int64 max32BitV
10243 61 6c 75 65 20 3d 20 0a 20 20 20 20 20 20 28 73  alue = .      (s
10244 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30  qlite3_int64)200
10245 30 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74  0000000 + (sqlit
10246 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30  e3_int64)2000000
10247 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69  000 + (sqlite3_i
10248 6e 74 36 34 29 32 39 34 39 36 37 32 39 36 3b 0a  nt64)294967296;.
10249 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
1024a 49 4e 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d  INCE.  SYSTEMTIM
1024b 45 20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73  E time;.  GetSys
1024c 74 65 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a  temTime(&time);.
1024d 20 20 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69    /* if SystemTi
1024e 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66  meToFileTime() f
1024f 61 69 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73  ails, it returns
10250 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28   zero. */.  if (
10251 21 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c  !SystemTimeToFil
10252 65 54 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29  eTime(&time,&ft)
10253 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
10254 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74  .  }.#else.  Get
10255 53 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65  SystemTimeAsFile
10256 54 69 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e  Time( &ft );.#en
10257 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  dif.  UNUSED_PAR
10258 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
10259 74 69 6d 65 57 20 3d 20 28 28 28 73 71 6c 69 74  timeW = (((sqlit
1025a 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 48 69  e3_int64)ft.dwHi
1025b 67 68 44 61 74 65 54 69 6d 65 29 2a 6d 61 78 33  ghDateTime)*max3
1025c 32 42 69 74 56 61 6c 75 65 29 20 2b 20 28 73 71  2BitValue) + (sq
1025d 6c 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64  lite3_int64)ft.d
1025e 77 4c 6f 77 44 61 74 65 54 69 6d 65 3b 0a 20 20  wLowDateTime;.  
1025f 74 69 6d 65 46 20 3d 20 74 69 6d 65 57 20 25 20  timeF = timeW % 
10260 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20 20 20  ntuPerDay;      
10261 20 20 20 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61      /* fractiona
10262 6c 20 64 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f  l days (100-nano
10263 73 65 63 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69  seconds) */.  ti
10264 6d 65 57 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74  meW = timeW / nt
10265 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20  uPerDay;        
10266 20 20 2f 2a 20 77 68 6f 6c 65 20 64 61 79 73 20    /* whole days 
10267 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d  */.  timeW = tim
10268 65 57 20 2b 20 32 33 30 35 38 31 33 3b 20 20 20  eW + 2305813;   
10269 20 20 20 20 20 20 20 20 20 2f 2a 20 61 64 64 20           /* add 
1026a 77 68 6f 6c 65 20 64 61 79 73 20 28 66 72 6f 6d  whole days (from
1026b 20 32 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20   2305813.5) */. 
1026c 20 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b   timeF = timeF +
1026d 20 6e 74 75 50 65 72 48 61 6c 66 44 61 79 3b 20   ntuPerHalfDay; 
1026e 20 20 20 20 20 2f 2a 20 61 64 64 20 68 61 6c 66       /* add half
1026f 20 61 20 64 61 79 20 28 66 72 6f 6d 20 32 33 30   a day (from 230
10270 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d  5813.5) */.  tim
10271 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 28 74 69  eW = timeW + (ti
10272 6d 65 46 2f 6e 74 75 50 65 72 44 61 79 29 3b 20  meF/ntuPerDay); 
10273 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61   /* add whole da
10274 79 20 69 66 20 68 61 6c 66 20 64 61 79 20 6d 61  y if half day ma
10275 64 65 20 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65  de one */.  time
10276 46 20 3d 20 74 69 6d 65 46 20 25 20 6e 74 75 50  F = timeF % ntuP
10277 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20  erDay;          
10278 2f 2a 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 66  /* compute new f
10279 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a  ractional days *
1027a 2f 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f  /.  *prNow = (do
1027b 75 62 6c 65 29 74 69 6d 65 57 20 2b 20 28 28 64  uble)timeW + ((d
1027c 6f 75 62 6c 65 29 74 69 6d 65 46 20 2f 20 28 64  ouble)timeF / (d
1027d 6f 75 62 6c 65 29 6e 74 75 50 65 72 44 61 79 29  ouble)ntuPerDay)
1027e 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
1027f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74  TEST.  if( sqlit
10280 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
10281 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20  ){.    *prNow = 
10282 28 28 64 6f 75 62 6c 65 29 73 71 6c 69 74 65 33  ((double)sqlite3
10283 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20  _current_time + 
10284 28 64 6f 75 62 6c 65 29 34 33 32 30 30 29 20 2f  (double)43200) /
10285 20 28 64 6f 75 62 6c 65 29 38 36 34 30 30 20 2b   (double)86400 +
10286 20 28 64 6f 75 62 6c 65 29 32 34 34 30 35 38 37   (double)2440587
10287 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
10288 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
10289 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74 68  * The idea is th
1028a 61 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  at this function
1028b 20 77 6f 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f   works like a co
1028c 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20  mbination of.** 
1028d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 61  GetLastError() a
1028e 6e 64 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65  nd FormatMessage
1028f 28 29 20 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f  () on windows (o
10290 72 20 65 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73  r errno and.** s
10291 74 72 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75  trerror_r() on u
10292 6e 69 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65  nix). After an e
10293 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
10294 20 62 79 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e   by an OS.** fun
10295 63 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61  ction, SQLite ca
10296 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  lls this functio
10297 6e 20 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e  n with zBuf poin
10298 74 69 6e 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66  ting to.** a buf
10299 66 65 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65  fer of nBuf byte
1029a 73 2e 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20  s. The OS layer 
1029b 73 68 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20  should populate 
1029c 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 77 69  the.** buffer wi
1029d 74 68 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  th a nul-termina
1029e 74 65 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  ted UTF-8 encode
1029f 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a  d error message.
102a0 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 74 68  ** describing th
102a1 65 20 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20  e last IO error 
102a2 74 6f 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  to have occurred
102a3 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c   within the call
102a4 69 6e 67 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a  ing.** thread..*
102a5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f  *.** If the erro
102a6 72 20 6d 65 73 73 61 67 65 20 69 73 20 74 6f 6f  r message is too
102a7 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20 73   large for the s
102a8 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2c 0a  upplied buffer,.
102a9 2a 2a 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20  ** it should be 
102aa 74 72 75 6e 63 61 74 65 64 2e 20 54 68 65 20 72  truncated. The r
102ab 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 78  eturn value of x
102ac 47 65 74 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20  GetLastError.** 
102ad 69 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65  is zero if the e
102ae 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74  rror message fit
102af 73 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c  s in the buffer,
102b0 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20   or non-zero.** 
102b1 6f 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68  otherwise (if th
102b2 65 20 6d 65 73 73 61 67 65 20 77 61 73 20 74 72  e message was tr
102b3 75 6e 63 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e  uncated). If non
102b4 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65  -zero is returne
102b5 64 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73  d,.** then it is
102b6 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74   not necessary t
102b7 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75  o include the nu
102b8 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 63 68 61  l-terminator cha
102b9 72 61 63 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65  racter.** in the
102ba 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 0a   output buffer..
102bb 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79  **.** Not supply
102bc 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ing an error mes
102bd 73 61 67 65 20 77 69 6c 6c 20 68 61 76 65 20 6e  sage will have n
102be 6f 20 61 64 76 65 72 73 65 20 65 66 66 65 63 74  o adverse effect
102bf 0a 2a 2a 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49  .** on SQLite. I
102c0 74 20 69 73 20 66 69 6e 65 20 74 6f 20 68 61 76  t is fine to hav
102c1 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e an implementat
102c2 69 6f 6e 20 74 68 61 74 20 6e 65 76 65 72 0a 2a  ion that never.*
102c3 2a 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  * returns an err
102c4 6f 72 20 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a  or message:.**.*
102c5 2a 20 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74  *   int xGetLast
102c6 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
102c7 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
102c8 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
102c9 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 7a 42  **     assert(zB
102ca 75 66 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a  uf[0]=='\0');.**
102cb 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a       return 0;.*
102cc 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65  *   }.**.** Howe
102cd 76 65 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ver if an error 
102ce 6d 65 73 73 61 67 65 20 69 73 20 73 75 70 70 6c  message is suppl
102cf 69 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20  ied, it will be 
102d0 69 6e 63 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20  incorporated.** 
102d1 62 79 20 73 71 6c 69 74 65 20 69 6e 74 6f 20 74  by sqlite into t
102d2 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
102d3 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68   available to th
102d4 65 20 75 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20  e user using.** 
102d5 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
102d6 2c 20 70 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e  , possibly makin
102d7 67 20 49 4f 20 65 72 72 6f 72 73 20 65 61 73 69  g IO errors easi
102d8 65 72 20 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a  er to debug..*/.
102d9 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 47 65  static int winGe
102da 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74  tLastError(sqlit
102db 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
102dc 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
102dd 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  uf){.  UNUSED_PA
102de 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20  RAMETER(pVfs);. 
102df 20 72 65 74 75 72 6e 20 67 65 74 4c 61 73 74 45   return getLastE
102e0 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42  rrorMsg(nBuf, zB
102e1 75 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  uf);.}../*.** In
102e2 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69  itialize and dei
102e3 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70  nitialize the op
102e4 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
102e5 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c  nterface..*/.SQL
102e6 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
102e7 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64  te3_os_init(void
102e8 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
102e9 74 65 33 5f 76 66 73 20 77 69 6e 56 66 73 20 3d  te3_vfs winVfs =
102ea 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   {.    1,       
102eb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
102ec 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a  rsion */.    siz
102ed 65 6f 66 28 77 69 6e 46 69 6c 65 29 2c 20 20 20  eof(winFile),   
102ee 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20  /* szOsFile */. 
102ef 20 20 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20     MAX_PATH,    
102f0 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e        /* mxPathn
102f1 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
102f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
102f3 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 77   pNext */.    "w
102f4 69 6e 33 32 22 2c 20 20 20 20 20 20 20 20 20 20  in32",          
102f5 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20   /* zName */.   
102f6 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
102f7 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20      /* pAppData 
102f8 2a 2f 0a 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e  */. .    winOpen
102f9 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  ,           /* x
102fa 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44  Open */.    winD
102fb 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f  elete,         /
102fc 2a 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20  * xDelete */.   
102fd 20 77 69 6e 41 63 63 65 73 73 2c 20 20 20 20 20   winAccess,     
102fe 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a      /* xAccess *
102ff 2f 0a 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74  /.    winFullPat
10300 68 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c  hname,   /* xFul
10301 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20  lPathname */.   
10302 20 77 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20   winDlOpen,     
10303 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a      /* xDlOpen *
10304 2f 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72  /.    winDlError
10305 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45  ,        /* xDlE
10306 72 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44  rror */.    winD
10307 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f  lSym,          /
10308 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20  * xDlSym */.    
10309 77 69 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20  winDlClose,     
1030a 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a     /* xDlClose *
1030b 2f 0a 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e  /.    winRandomn
1030c 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e  ess,     /* xRan
1030d 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77  domness */.    w
1030e 69 6e 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20  inSleep,        
1030f 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20    /* xSleep */. 
10310 20 20 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d     winCurrentTim
10311 65 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e  e,    /* xCurren
10312 74 54 69 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e  tTime */.    win
10313 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20  GetLastError    
10314 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  /* xGetLastError
10315 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 73 71 6c 69   */.  };..  sqli
10316 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72  te3_vfs_register
10317 28 26 77 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20  (&winVfs, 1);.  
10318 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
10319 3b 20 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  ; .}.SQLITE_API 
1031a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65  int sqlite3_os_e
1031b 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74  nd(void){ .  ret
1031c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1031d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
1031e 54 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a  TE_OS_WIN */../*
1031f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
10320 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a  d of os_win.c **
10321 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10322 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10323 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10324 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
10325 67 69 6e 20 66 69 6c 65 20 62 69 74 76 65 63 2e  gin file bitvec.
10326 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
10327 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10328 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10329 0a 2a 2a 20 32 30 30 38 20 46 65 62 72 75 61 72  .** 2008 Februar
1032a 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 16.**.** The a
1032b 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
1032c 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
1032d 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
1032e 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
1032f 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
10330 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
10331 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
10332 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
10333 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
10334 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
10335 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
10336 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
10337 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
10338 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
10339 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
1033a 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
1033b 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
1033c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1033d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1033e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1033f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10340 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
10341 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f   implements an o
10342 62 6a 65 63 74 20 74 68 61 74 20 72 65 70 72 65  bject that repre
10343 73 65 6e 74 73 20 61 20 66 69 78 65 64 2d 6c 65  sents a fixed-le
10344 6e 67 74 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20  ngth.** bitmap. 
10345 20 42 69 74 73 20 61 72 65 20 6e 75 6d 62 65 72   Bits are number
10346 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68  ed starting with
10347 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d   1..**.** A bitm
10348 61 70 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ap is used to re
10349 63 6f 72 64 20 77 68 69 63 68 20 70 61 67 65 73  cord which pages
1034a 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66   of a database f
1034b 69 6c 65 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a  ile have been.**
1034c 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 75 72 69   journalled duri
1034d 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ng a transaction
1034e 2c 20 6f 72 20 77 68 69 63 68 20 70 61 67 65 73  , or which pages
1034f 20 68 61 76 65 20 74 68 65 20 22 64 6f 6e 74 2d   have the "dont-
10350 77 72 69 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72  write".** proper
10351 74 79 2e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c  ty.  Usually onl
10352 79 20 61 20 66 65 77 20 70 61 67 65 73 20 61 72  y a few pages ar
10353 65 20 6d 65 65 74 20 65 69 74 68 65 72 20 63 6f  e meet either co
10354 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74  ndition..** So t
10355 68 65 20 62 69 74 6d 61 70 20 69 73 20 75 73 75  he bitmap is usu
10356 61 6c 6c 79 20 73 70 61 72 73 65 20 61 6e 64 20  ally sparse and 
10357 68 61 73 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c  has low cardinal
10358 69 74 79 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65  ity..** But some
10359 74 69 6d 65 73 20 28 66 6f 72 20 65 78 61 6d 70  times (for examp
1035a 6c 65 20 77 68 65 6e 20 64 75 72 69 6e 67 20 61  le when during a
1035b 20 44 52 4f 50 20 6f 66 20 61 20 6c 61 72 67 65   DROP of a large
1035c 20 74 61 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20   table) most.** 
1035d 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 61  or all of the pa
1035e 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73  ges in a databas
1035f 65 20 63 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61  e can get journa
10360 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f 73 65 20  lled.  In those 
10361 63 61 73 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62  cases, .** the b
10362 69 74 6d 61 70 20 62 65 63 6f 6d 65 73 20 64 65  itmap becomes de
10363 6e 73 65 20 77 69 74 68 20 68 69 67 68 20 63 61  nse with high ca
10364 72 64 69 6e 61 6c 69 74 79 2e 20 20 54 68 65 20  rdinality.  The 
10365 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65 64 73 20  algorithm needs 
10366 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f  .** to handle bo
10367 74 68 20 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a  th cases well..*
10368 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66  *.** The size of
10369 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 66   the bitmap is f
1036a 69 78 65 64 20 77 68 65 6e 20 74 68 65 20 6f 62  ixed when the ob
1036b 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 2e  ject is created.
1036c 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20  .**.** All bits 
1036d 61 72 65 20 63 6c 65 61 72 20 77 68 65 6e 20 74  are clear when t
1036e 68 65 20 62 69 74 6d 61 70 20 69 73 20 63 72 65  he bitmap is cre
1036f 61 74 65 64 2e 20 20 49 6e 64 69 76 69 64 75 61  ated.  Individua
10370 6c 20 62 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65  l bits.** may be
10371 20 73 65 74 20 6f 72 20 63 6c 65 61 72 65 64 20   set or cleared 
10372 6f 6e 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a  one at a time..*
10373 2a 0a 2a 2a 20 54 65 73 74 20 6f 70 65 72 61 74  *.** Test operat
10374 69 6f 6e 73 20 61 72 65 20 61 62 6f 75 74 20 31  ions are about 1
10375 30 30 20 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f  00 times more co
10376 6d 6d 6f 6e 20 74 68 61 74 20 73 65 74 20 6f 70  mmon that set op
10377 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65  erations..** Cle
10378 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72  ar operations ar
10379 65 20 65 78 63 65 65 64 69 6e 67 6c 79 20 72 61  e exceedingly ra
1037a 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 20 75  re.  There are u
1037b 73 75 61 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a  sually between.*
1037c 2a 20 35 20 61 6e 64 20 35 30 30 20 73 65 74 20  * 5 and 500 set 
1037d 6f 70 65 72 61 74 69 6f 6e 73 20 70 65 72 20 42  operations per B
1037e 69 74 76 65 63 20 6f 62 6a 65 63 74 2c 20 74 68  itvec object, th
1037f 6f 75 67 68 20 74 68 65 20 6e 75 6d 62 65 72 20  ough the number 
10380 6f 66 20 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73  of sets can.** s
10381 6f 6d 65 74 69 6d 65 73 20 67 72 6f 77 20 69 6e  ometimes grow in
10382 74 6f 20 74 65 6e 73 20 6f 66 20 74 68 6f 75 73  to tens of thous
10383 61 6e 64 73 20 6f 72 20 6c 61 72 67 65 72 2e 20  ands or larger. 
10384 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   The size of the
10385 0a 2a 2a 20 42 69 74 76 65 63 20 6f 62 6a 65 63  .** Bitvec objec
10386 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
10387 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
10388 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74  database file at
10389 20 74 68 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66   the.** start of
1038a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20   a transaction, 
1038b 61 6e 64 20 69 73 20 74 68 75 73 20 75 73 75 61  and is thus usua
1038c 6c 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 61 20  lly less than a 
1038d 66 65 77 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a  few thousand,.**
1038e 20 62 75 74 20 63 61 6e 20 62 65 20 61 73 20 6c   but can be as l
1038f 61 72 67 65 20 61 73 20 32 20 62 69 6c 6c 69 6f  arge as 2 billio
10390 6e 20 66 6f 72 20 61 20 72 65 61 6c 6c 79 20 62  n for a really b
10391 69 67 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  ig database..*/.
10392 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20  ./* Size of the 
10393 42 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65  Bitvec structure
10394 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 23 64   in bytes. */.#d
10395 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 20  efine BITVEC_SZ 
10396 20 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 76         (sizeof(v
10397 6f 69 64 2a 29 2a 31 32 38 29 20 20 2f 2a 20 35  oid*)*128)  /* 5
10398 31 32 20 6f 6e 20 33 32 62 69 74 2e 20 20 31 30  12 on 32bit.  10
10399 32 34 20 6f 6e 20 36 34 62 69 74 20 2a 2f 0a 0a  24 on 64bit */..
1039a 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69  /* Round the uni
1039b 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20  on size down to 
1039c 74 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e  the nearest poin
1039d 74 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69  ter boundary, si
1039e 6e 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0a  nce that's how .
1039f 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61 6c  ** it will be al
103a0 69 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65  igned within the
103a1 20 42 69 74 76 65 63 20 73 74 72 75 63 74 2e 20   Bitvec struct. 
103a2 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
103a3 43 5f 55 53 49 5a 45 20 20 20 20 20 28 28 28 42  C_USIZE     (((B
103a4 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65  ITVEC_SZ-(3*size
103a5 6f 66 28 75 33 32 29 29 29 2f 73 69 7a 65 6f 66  of(u32)))/sizeof
103a6 28 42 69 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f  (Bitvec*))*sizeo
103a7 66 28 42 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20  f(Bitvec*))../* 
103a8 54 79 70 65 20 6f 66 20 74 68 65 20 61 72 72 61  Type of the arra
103a9 79 20 22 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20  y "element" for 
103aa 74 68 65 20 62 69 74 6d 61 70 20 72 65 70 72 65  the bitmap repre
103ab 73 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53  sentation. .** S
103ac 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 77 65 72  hould be a power
103ad 20 6f 66 20 32 2c 20 61 6e 64 20 69 64 65 61 6c   of 2, and ideal
103ae 6c 79 2c 20 65 76 65 6e 6c 79 20 64 69 76 69 64  ly, evenly divid
103af 65 20 69 6e 74 6f 20 42 49 54 56 45 43 5f 55 53  e into BITVEC_US
103b0 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67  IZE. .** Setting
103b1 20 74 68 69 73 20 74 6f 20 74 68 65 20 22 6e 61   this to the "na
103b2 74 75 72 61 6c 20 77 6f 72 64 22 20 73 69 7a 65  tural word" size
103b3 20 6f 66 20 79 6f 75 72 20 43 50 55 20 6d 61 79   of your CPU may
103b4 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66   improve.** perf
103b5 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66  ormance. */.#def
103b6 69 6e 65 20 42 49 54 56 45 43 5f 54 45 4c 45 4d  ine BITVEC_TELEM
103b7 20 20 20 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c       u8./* Size,
103b8 20 69 6e 20 62 69 74 73 2c 20 6f 66 20 74 68 65   in bits, of the
103b9 20 62 69 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e   bitmap element.
103ba 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56   */.#define BITV
103bb 45 43 5f 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f  EC_SZELEM    8./
103bc 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
103bd 65 6e 74 73 20 69 6e 20 61 20 62 69 74 6d 61 70  ents in a bitmap
103be 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69   array. */.#defi
103bf 6e 65 20 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20  ne BITVEC_NELEM 
103c0 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a      (BITVEC_USIZ
103c1 45 2f 73 69 7a 65 6f 66 28 42 49 54 56 45 43 5f  E/sizeof(BITVEC_
103c2 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65  TELEM))./* Numbe
103c3 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65  r of bits in the
103c4 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a   bitmap array. *
103c5 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
103c6 5f 4e 42 49 54 20 20 20 20 20 20 28 42 49 54 56  _NBIT      (BITV
103c7 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f  EC_NELEM*BITVEC_
103c8 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62  SZELEM)../* Numb
103c9 65 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73  er of u32 values
103ca 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20   in hash table. 
103cb 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
103cc 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54  C_NINT      (BIT
103cd 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66  VEC_USIZE/sizeof
103ce 28 75 33 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75  (u32))./* Maximu
103cf 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  m number of entr
103d0 69 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c  ies in hash tabl
103d1 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62  e before .** sub
103d2 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65  -dividing and re
103d3 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65  -hashing. */.#de
103d4 66 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41  fine BITVEC_MXHA
103d5 53 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49  SH    (BITVEC_NI
103d6 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67  NT/2)./* Hashing
103d7 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68   function for th
103d8 65 20 61 48 61 73 68 20 72 65 70 72 65 73 65 6e  e aHash represen
103d9 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72  tation..** Empir
103da 69 63 61 6c 20 74 65 73 74 69 6e 67 20 73 68 6f  ical testing sho
103db 77 65 64 20 74 68 61 74 20 74 68 65 20 2a 33 37  wed that the *37
103dc 20 6d 75 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20   multiplier .** 
103dd 28 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 72  (an arbitrary pr
103de 69 6d 65 29 69 6e 20 74 68 65 20 68 61 73 68 20  ime)in the hash 
103df 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69 64 65  function provide
103e0 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63  d .** no fewer c
103e1 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 61 6e 20 74  ollisions than t
103e2 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a  he no-op *1. */.
103e3 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48  #define BITVEC_H
103e4 41 53 48 28 58 29 20 20 20 28 28 28 58 29 2a 31  ASH(X)   (((X)*1
103e5 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a  )%BITVEC_NINT)..
103e6 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e  #define BITVEC_N
103e7 50 54 52 20 20 20 20 20 20 28 42 49 54 56 45 43  PTR      (BITVEC
103e8 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69  _USIZE/sizeof(Bi
103e9 74 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a  tvec *)).../*.**
103ea 20 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20   A bitmap is an 
103eb 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
103ec 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
103ed 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ure..**.** This 
103ee 62 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20 74  bitmap records t
103ef 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20  he existance of 
103f0 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74  zero or more bit
103f1 73 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75 65 73  s.** with values
103f2 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69   between 1 and i
103f3 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e  Size, inclusive.
103f4 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
103f5 20 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20   three possible 
103f6 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20  representations 
103f7 6f 66 20 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a  of the bitmap..*
103f8 2a 20 49 66 20 69 53 69 7a 65 3c 3d 42 49 54 56  * If iSize<=BITV
103f9 45 43 5f 4e 42 49 54 2c 20 74 68 65 6e 20 42 69  EC_NBIT, then Bi
103fa 74 76 65 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d  tvec.u.aBitmap[]
103fb 20 69 73 20 61 20 73 74 72 61 69 67 68 74 0a 2a   is a straight.*
103fc 2a 20 62 69 74 6d 61 70 2e 20 20 54 68 65 20 6c  * bitmap.  The l
103fd 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74  east significant
103fe 20 62 69 74 20 69 73 20 62 69 74 20 31 2e 0a 2a   bit is bit 1..*
103ff 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3e 42 49  *.** If iSize>BI
10400 54 56 45 43 5f 4e 42 49 54 20 61 6e 64 20 69 44  TVEC_NBIT and iD
10401 69 76 69 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42  ivisor==0 then B
10402 69 74 76 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20  itvec.u.aHash[] 
10403 69 73 0a 2a 2a 20 61 20 68 61 73 68 20 74 61 62  is.** a hash tab
10404 6c 65 20 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c  le that will hol
10405 64 20 75 70 20 74 6f 20 42 49 54 56 45 43 5f 4d  d up to BITVEC_M
10406 58 48 41 53 48 20 64 69 73 74 69 6e 63 74 20 76  XHASH distinct v
10407 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  alues..**.** Oth
10408 65 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75  erwise, the valu
10409 65 20 69 20 69 73 20 72 65 64 69 72 65 63 74 65  e i is redirecte
1040a 64 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49  d into one of BI
1040b 54 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62  TVEC_NPTR.** sub
1040c 2d 62 69 74 6d 61 70 73 20 70 6f 69 6e 74 65 64  -bitmaps pointed
1040d 20 74 6f 20 62 79 20 42 69 74 76 65 63 2e 75 2e   to by Bitvec.u.
1040e 61 70 53 75 62 5b 5d 2e 20 20 45 61 63 68 20 73  apSub[].  Each s
1040f 75 62 62 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64  ubbitmap.** hand
10410 6c 65 73 20 75 70 20 74 6f 20 69 44 69 76 69 73  les up to iDivis
10411 6f 72 20 73 65 70 61 72 61 74 65 20 76 61 6c 75  or separate valu
10412 65 73 20 6f 66 20 69 2e 20 20 61 70 53 75 62 5b  es of i.  apSub[
10413 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75  0] holds.** valu
10414 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  es between 1 and
10415 20 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75   iDivisor.  apSu
10416 62 5b 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65  b[1] holds value
10417 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69  s between.** iDi
10418 76 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44  visor+1 and 2*iD
10419 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e  ivisor.  apSub[N
1041a 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62  ] holds values b
1041b 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76  etween.** N*iDiv
1041c 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29  isor+1 and (N+1)
1041d 2a 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68  *iDivisor.  Each
1041e 20 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f   subbitmap is no
1041f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68  rmalized.** to h
10420 6f 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76 61  old deal with va
10421 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61  lues between 1 a
10422 6e 64 20 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a  nd iDivisor..*/.
10423 73 74 72 75 63 74 20 42 69 74 76 65 63 20 7b 0a  struct Bitvec {.
10424 20 20 75 33 32 20 69 53 69 7a 65 3b 20 20 20 20    u32 iSize;    
10425 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74    /* Maximum bit
10426 20 69 6e 64 65 78 2e 20 20 4d 61 78 20 69 53 69   index.  Max iSi
10427 7a 65 20 69 73 20 34 2c 32 39 34 2c 39 36 37 2c  ze is 4,294,967,
10428 32 39 36 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53  296. */.  u32 nS
10429 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  et;       /* Num
1042a 62 65 72 20 6f 66 20 62 69 74 73 20 74 68 61 74  ber of bits that
1042b 20 61 72 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20   are set - only 
1042c 76 61 6c 69 64 20 66 6f 72 20 61 48 61 73 68 0a  valid for aHash.
1042d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1042e 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e 20 20 4d    ** element.  M
1042f 61 78 20 69 73 20 42 49 54 56 45 43 5f 4e 49 4e  ax is BITVEC_NIN
10430 54 2e 20 20 46 6f 72 20 42 49 54 56 45 43 5f 53  T.  For BITVEC_S
10431 5a 20 6f 66 20 35 31 32 2c 0a 20 20 20 20 20 20  Z of 512,.      
10432 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
10433 68 69 73 20 77 6f 75 6c 64 20 62 65 20 31 32 35  his would be 125
10434 2e 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 69  . */.  u32 iDivi
10435 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72  sor;   /* Number
10436 20 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 64   of bits handled
10437 20 62 79 20 65 61 63 68 20 61 70 53 75 62 5b 5d   by each apSub[]
10438 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 20   entry. */.     
10439 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1043a 53 68 6f 75 6c 64 20 3e 3d 30 20 66 6f 72 20 61  Should >=0 for a
1043b 70 53 75 62 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f  pSub element. */
1043c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1043d 20 20 20 2f 2a 20 4d 61 78 20 69 44 69 76 69 73     /* Max iDivis
1043e 6f 72 20 69 73 20 6d 61 78 28 75 33 32 29 20 2f  or is max(u32) /
1043f 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2b 20 31   BITVEC_NPTR + 1
10440 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  .  */.          
10441 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61          /* For a
10442 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31   BITVEC_SZ of 51
10443 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65  2, this would be
10444 20 33 34 2c 33 35 39 2c 37 33 39 2e 20 2a 2f 0a   34,359,739. */.
10445 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 49    union {.    BI
10446 54 56 45 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d  TVEC_TELEM aBitm
10447 61 70 5b 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d  ap[BITVEC_NELEM]
10448 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 72  ;    /* Bitmap r
10449 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f  epresentation */
1044a 0a 20 20 20 20 75 33 32 20 61 48 61 73 68 5b 42  .    u32 aHash[B
1044b 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20  ITVEC_NINT];    
1044c 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20    /* Hash table 
1044d 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a  representation *
1044e 2f 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70  /.    Bitvec *ap
1044f 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d  Sub[BITVEC_NPTR]
10450 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20  ;  /* Recursive 
10451 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a  representation *
10452 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a  /.  } u;.};../*.
10453 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
10454 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 62  bitmap object ab
10455 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 74  le to handle bit
10456 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  s between 0 and 
10457 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73  iSize,.** inclus
10458 69 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70  ive.  Return a p
10459 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
1045a 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 72  w object.  Retur
1045b 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61  n NULL if .** ma
1045c 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53  lloc fails..*/.S
1045d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69  QLITE_PRIVATE Bi
1045e 74 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74  tvec *sqlite3Bit
1045f 76 65 63 43 72 65 61 74 65 28 75 33 32 20 69 53  vecCreate(u32 iS
10460 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 2a  ize){.  Bitvec *
10461 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  p;.  assert( siz
10462 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f  eof(*p)==BITVEC_
10463 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69  SZ );.  p = sqli
10464 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
10465 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69  izeof(*p) );.  i
10466 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69  f( p ){.    p->i
10467 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20  Size = iSize;.  
10468 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
10469 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
1046a 73 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 20  see if the i-th 
1046b 62 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 74  bit is set.  Ret
1046c 75 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c 73  urn true or fals
1046d 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e 55  e..** If p is NU
1046e 4c 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d 61  LL (if the bitma
1046f 70 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 63  p has not been c
10470 72 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a  reated) or if.**
10471 20 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e   i is out of ran
10472 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  ge, then return 
10473 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  false..*/.SQLITE
10474 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10475 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42  ite3BitvecTest(B
10476 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29  itvec *p, u32 i)
10477 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
10478 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69  eturn 0;.  if( i
10479 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d  >p->iSize || i==
1047a 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
1047b 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d  i--;.  while( p-
1047c 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20  >iDivisor ){.   
1047d 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e   u32 bin = i/p->
1047e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20  iDivisor;.    i 
1047f 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b  = i%p->iDivisor;
10480 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70  .    p = p->u.ap
10481 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66  Sub[bin];.    if
10482 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65   (!p) {.      re
10483 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
10484 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65  }.  if( p->iSize
10485 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b  <=BITVEC_NBIT ){
10486 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 2d 3e  .    return (p->
10487 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56  u.aBitmap[i/BITV
10488 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 28 31 3c  EC_SZELEM] & (1<
10489 3c 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c  <(i&(BITVEC_SZEL
1048a 45 4d 2d 31 29 29 29 29 21 3d 30 3b 0a 20 20 7d  EM-1))))!=0;.  }
1048b 20 65 6c 73 65 7b 0a 20 20 20 20 75 33 32 20 68   else{.    u32 h
1048c 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69   = BITVEC_HASH(i
1048d 2b 2b 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ++);.    while( 
1048e 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b  p->u.aHash[h] ){
1048f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e  .      if( p->u.
10490 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65  aHash[h]==i ) re
10491 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68 20  turn 1;.      h 
10492 3d 20 28 68 2b 31 29 20 25 20 42 49 54 56 45 43  = (h+1) % BITVEC
10493 5f 4e 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _NINT;.    }.   
10494 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d   return 0;.  }.}
10495 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
10496 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72  i-th bit.  Retur
10497 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61  n 0 on success a
10498 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  nd an error code
10499 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20   if.** anything 
1049a 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a  goes wrong..**.*
1049b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
1049c 69 67 68 74 20 63 61 75 73 65 20 73 75 62 2d 62  ight cause sub-b
1049d 69 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c 6c  itmaps to be all
1049e 6f 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e 67  ocated.  Failing
1049f 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 6d  .** to get the m
104a0 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f 20  emory needed to 
104a1 68 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 74  hold the sub-bit
104a2 6d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a  map is the only.
104a3 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 77  ** that can go w
104a4 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 73  rong with an ins
104a5 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 20  ert, assuming p 
104a6 61 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 2e  and i are valid.
104a7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  .**.** The calli
104a8 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  ng function must
104a9 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 69   ensure that p i
104aa 73 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 63  s a valid Bitvec
104ab 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74   object.** and t
104ac 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 6f  hat the value fo
104ad 72 20 22 69 22 20 69 73 20 77 69 74 68 69 6e 20  r "i" is within 
104ae 72 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 74  range of the Bit
104af 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f  vec object..** O
104b0 74 68 65 72 77 69 73 65 20 74 68 65 20 62 65 68  therwise the beh
104b1 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
104b2 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
104b3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
104b4 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65  3BitvecSet(Bitve
104b5 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20  c *p, u32 i){.  
104b6 75 33 32 20 68 3b 0a 20 20 69 66 28 20 70 3d 3d  u32 h;.  if( p==
104b7 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
104b8 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
104b9 69 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  i>0 );.  assert(
104ba 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a   i<=p->iSize );.
104bb 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 28    i--;.  while((
104bc 70 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54 56 45  p->iSize > BITVE
104bd 43 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e 69 44  C_NBIT) && p->iD
104be 69 76 69 73 6f 72 29 20 7b 0a 20 20 20 20 75 33  ivisor) {.    u3
104bf 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69  2 bin = i/p->iDi
104c0 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69  visor;.    i = i
104c1 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  %p->iDivisor;.  
104c2 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62    if( p->u.apSub
104c3 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  [bin]==0 ){.    
104c4 20 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e    p->u.apSub[bin
104c5 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  ] = sqlite3Bitve
104c6 63 43 72 65 61 74 65 28 20 70 2d 3e 69 44 69 76  cCreate( p->iDiv
104c7 69 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 69 66  isor );.      if
104c8 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  ( p->u.apSub[bin
104c9 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  ]==0 ) return SQ
104ca 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
104cb 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61  }.    p = p->u.a
104cc 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 7d 0a 20  pSub[bin];.  }. 
104cd 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42   if( p->iSize<=B
104ce 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20  ITVEC_NBIT ){.  
104cf 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69    p->u.aBitmap[i
104d0 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20  /BITVEC_SZELEM] 
104d1 7c 3d 20 31 20 3c 3c 20 28 69 26 28 42 49 54 56  |= 1 << (i&(BITV
104d2 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 3b 0a 20  EC_SZELEM-1));. 
104d3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
104d4 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 42  _OK;.  }.  h = B
104d5 49 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b  ITVEC_HASH(i++);
104d6 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 77  .  /* if there w
104d7 61 73 6e 27 74 20 61 20 68 61 73 68 20 63 6f 6c  asn't a hash col
104d8 6c 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73  lision, and this
104d9 20 64 6f 65 73 6e 27 74 20 2a 2f 0a 20 20 2f 2a   doesn't */.  /*
104da 20 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c   completely fill
104db 20 74 68 65 20 68 61 73 68 2c 20 74 68 65 6e 20   the hash, then 
104dc 6a 75 73 74 20 61 64 64 20 69 74 20 77 69 74 68  just add it with
104dd 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20 77 6f 72 72  out */.  /* worr
104de 69 6e 67 20 61 62 6f 75 74 20 73 75 62 2d 64 69  ing about sub-di
104df 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61  viding and re-ha
104e0 73 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20  shing. */.  if( 
104e1 21 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29  !p->u.aHash[h] )
104e2 7b 0a 20 20 20 20 69 66 20 28 70 2d 3e 6e 53 65  {.    if (p->nSe
104e3 74 3c 28 42 49 54 56 45 43 5f 4e 49 4e 54 2d 31  t<(BITVEC_NINT-1
104e4 29 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  )) {.      goto 
104e5 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3b 0a  bitvec_set_end;.
104e6 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
104e7 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73     goto bitvec_s
104e8 65 74 5f 72 65 68 61 73 68 3b 0a 20 20 20 20 7d  et_rehash;.    }
104e9 0a 20 20 7d 0a 20 20 2f 2a 20 74 68 65 72 65 20  .  }.  /* there 
104ea 77 61 73 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 2c  was a collision,
104eb 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
104ec 20 69 74 27 73 20 61 6c 72 65 61 64 79 20 2a 2f   it's already */
104ed 0a 20 20 2f 2a 20 69 6e 20 68 61 73 68 2c 20 69  .  /* in hash, i
104ee 66 20 6e 6f 74 2c 20 74 72 79 20 74 6f 20 66 69  f not, try to fi
104ef 6e 64 20 61 20 73 70 6f 74 20 66 6f 72 20 69 74  nd a spot for it
104f0 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69   */.  do {.    i
104f1 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  f( p->u.aHash[h]
104f2 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==i ) return SQL
104f3 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b  ITE_OK;.    h++;
104f4 0a 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56  .    if( h>=BITV
104f5 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b  EC_NINT ) h = 0;
104f6 0a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75  .  } while( p->u
104f7 2e 61 48 61 73 68 5b 68 5d 20 29 3b 0a 20 20 2f  .aHash[h] );.  /
104f8 2a 20 77 65 20 64 69 64 6e 27 74 20 66 69 6e 64  * we didn't find
104f9 20 69 74 20 69 6e 20 74 68 65 20 68 61 73 68 2e   it in the hash.
104fa 20 20 68 20 70 6f 69 6e 74 73 20 74 6f 20 74 68    h points to th
104fb 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 2f 2a 20  e first */.  /* 
104fc 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65 20 73  available free s
104fd 70 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20 73 65  pot. check to se
104fe 65 20 69 66 20 74 68 69 73 20 69 73 20 67 6f 69  e if this is goi
104ff 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f 2a 20 6d 61  ng to */.  /* ma
10500 6b 65 20 6f 75 72 20 68 61 73 68 20 74 6f 6f 20  ke our hash too 
10501 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a 62 69 74 76  "full".  */.bitv
10502 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3a 0a 20  ec_set_rehash:. 
10503 20 69 66 28 20 70 2d 3e 6e 53 65 74 3e 3d 42 49   if( p->nSet>=BI
10504 54 56 45 43 5f 4d 58 48 41 53 48 20 29 7b 0a 20  TVEC_MXHASH ){. 
10505 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
10506 6a 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  j;.    int rc;. 
10507 20 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73     u32 *aiValues
10508 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41   = sqlite3StackA
10509 6c 6c 6f 63 52 61 77 28 30 2c 20 73 69 7a 65 6f  llocRaw(0, sizeo
1050a 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a  f(p->u.aHash));.
1050b 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73      if( aiValues
1050c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
1050d 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1050e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1050f 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75     memcpy(aiValu
10510 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20  es, p->u.aHash, 
10511 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73  sizeof(p->u.aHas
10512 68 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65  h));.      memse
10513 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c  t(p->u.apSub, 0,
10514 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53   sizeof(p->u.apS
10515 75 62 29 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69  ub));.      p->i
10516 44 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53  Divisor = (p->iS
10517 69 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54  ize + BITVEC_NPT
10518 52 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50  R - 1)/BITVEC_NP
10519 54 52 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  TR;.      rc = s
1051a 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
1051b 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 66 6f 72  p, i);.      for
1051c 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e  (j=0; j<BITVEC_N
1051d 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  INT; j++){.     
1051e 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b     if( aiValues[
1051f 6a 5d 20 29 20 72 63 20 7c 3d 20 73 71 6c 69 74  j] ) rc |= sqlit
10520 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 61  e3BitvecSet(p, a
10521 69 56 61 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20  iValues[j]);.   
10522 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
10523 65 33 53 74 61 63 6b 46 72 65 65 28 30 2c 20 61  e3StackFree(0, a
10524 69 56 61 6c 75 65 73 29 3b 0a 20 20 20 20 20 20  iValues);.      
10525 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
10526 0a 20 20 7d 0a 62 69 74 76 65 63 5f 73 65 74 5f  .  }.bitvec_set_
10527 65 6e 64 3a 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b  end:.  p->nSet++
10528 3b 0a 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  ;.  p->u.aHash[h
10529 5d 20 3d 20 69 3b 0a 20 20 72 65 74 75 72 6e 20  ] = i;.  return 
1052a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
1052b 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69 2d  .** Clear the i-
1052c 74 68 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 70 42  th bit..**.** pB
1052d 75 66 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69  uf must be a poi
1052e 6e 74 65 72 20 74 6f 20 61 74 20 6c 65 61 73 74  nter to at least
1052f 20 42 49 54 56 45 43 5f 53 5a 20 62 79 74 65 73   BITVEC_SZ bytes
10530 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 73 74   of temporary st
10531 6f 72 61 67 65 0a 2a 2a 20 74 68 61 74 20 42 69  orage.** that Bi
10532 74 76 65 63 43 6c 65 61 72 20 63 61 6e 20 75 73  tvecClear can us
10533 65 20 74 6f 20 72 65 62 75 69 6c 74 20 69 74 73  e to rebuilt its
10534 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a   hash table..*/.
10535 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10536 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65  oid sqlite3Bitve
10537 63 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70  cClear(Bitvec *p
10538 2c 20 75 33 32 20 69 2c 20 76 6f 69 64 20 2a 70  , u32 i, void *p
10539 42 75 66 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30  Buf){.  if( p==0
1053a 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
1053b 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d  ert( i>0 );.  i-
1053c 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69  -;.  while( p->i
1053d 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75  Divisor ){.    u
1053e 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44  32 bin = i/p->iD
1053f 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20  ivisor;.    i = 
10540 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20  i%p->iDivisor;. 
10541 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75     p = p->u.apSu
10542 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28  b[bin];.    if (
10543 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75  !p) {.      retu
10544 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
10545 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49  if( p->iSize<=BI
10546 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20  TVEC_NBIT ){.   
10547 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f   p->u.aBitmap[i/
10548 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26  BITVEC_SZELEM] &
10549 3d 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54  = ~(1 << (i&(BIT
1054a 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b  VEC_SZELEM-1)));
1054b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e  .  }else{.    un
1054c 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20  signed int j;.  
1054d 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20    u32 *aiValues 
1054e 3d 20 70 42 75 66 3b 0a 20 20 20 20 6d 65 6d 63  = pBuf;.    memc
1054f 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e  py(aiValues, p->
10550 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28  u.aHash, sizeof(
10551 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20  p->u.aHash));.  
10552 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48    memset(p->u.aH
10553 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70  ash, 0, sizeof(p
10554 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20  ->u.aHash));.   
10555 20 70 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20   p->nSet = 0;.  
10556 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54    for(j=0; j<BIT
10557 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a  VEC_NINT; j++){.
10558 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c 75        if( aiValu
10559 65 73 5b 6a 5d 20 26 26 20 61 69 56 61 6c 75 65  es[j] && aiValue
1055a 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 7b 0a 20  s[j]!=(i+1) ){. 
1055b 20 20 20 20 20 20 20 75 33 32 20 68 20 3d 20 42         u32 h = B
1055c 49 54 56 45 43 5f 48 41 53 48 28 61 69 56 61 6c  ITVEC_HASH(aiVal
1055d 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 20  ues[j]-1);.     
1055e 20 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20     p->nSet++;.  
1055f 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e        while( p->
10560 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20  u.aHash[h] ){.  
10561 20 20 20 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20          h++;.   
10562 20 20 20 20 20 20 20 69 66 28 20 68 3e 3d 42 49         if( h>=BI
10563 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20  TVEC_NINT ) h = 
10564 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
10565 20 20 20 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b       p->u.aHash[
10566 68 5d 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d  h] = aiValues[j]
10567 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
10568 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73    }.}../*.** Des
10569 74 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f 62  troy a bitmap ob
1056a 6a 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20 61  ject.  Reclaim a
1056b 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a  ll memory used..
1056c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1056d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69  E void sqlite3Bi
1056e 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76  tvecDestroy(Bitv
1056f 65 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  ec *p){.  if( p=
10570 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
10571 66 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29  f( p->iDivisor )
10572 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69  {.    unsigned i
10573 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
10574 30 3b 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 52  0; i<BITVEC_NPTR
10575 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
10576 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
10577 6f 79 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d  oy(p->u.apSub[i]
10578 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
10579 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
1057a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1057b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1057c 20 69 53 69 7a 65 20 70 61 72 61 6d 65 74 65 72   iSize parameter
1057d 20 73 70 65 63 69 66 69 65 64 20 77 68 65 6e 20   specified when 
1057e 42 69 74 76 65 63 20 2a 70 0a 2a 2a 20 77 61 73  Bitvec *p.** was
1057f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c   created..*/.SQL
10580 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
10581 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a  sqlite3BitvecSiz
10582 65 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20  e(Bitvec *p){.  
10583 72 65 74 75 72 6e 20 70 2d 3e 69 53 69 7a 65 3b  return p->iSize;
10584 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
10585 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
10586 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56  TEST./*.** Let V
10587 5b 5d 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f  [] be an array o
10588 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61  f unsigned chara
10589 63 74 65 72 73 20 73 75 66 66 69 63 69 65 6e 74  cters sufficient
1058a 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74   to hold.** up t
1058b 6f 20 4e 20 62 69 74 73 2e 20 20 4c 65 74 20 49  o N bits.  Let I
1058c 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62   be an integer b
1058d 65 74 77 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20  etween 0 and N. 
1058e 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e   0<=I<N..** Then
1058f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
10590 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65  acros can be use
10591 64 20 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c  d to set, clear,
10592 20 6f 72 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69   or test.** indi
10593 76 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68  vidual bits with
10594 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  in V..*/.#define
10595 20 53 45 54 42 49 54 28 56 2c 49 29 20 20 20 20   SETBIT(V,I)    
10596 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c    V[I>>3] |= (1<
10597 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20  <(I&7)).#define 
10598 43 4c 45 41 52 42 49 54 28 56 2c 49 29 20 20 20  CLEARBIT(V,I)   
10599 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c   V[I>>3] &= ~(1<
1059a 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20  <(I&7)).#define 
1059b 54 45 53 54 42 49 54 28 56 2c 49 29 20 20 20 20  TESTBIT(V,I)    
1059c 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49   (V[I>>3]&(1<<(I
1059d 26 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20  &7)))!=0../*.** 
1059e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e  This routine run
1059f 73 20 61 6e 20 65 78 74 65 6e 73 69 76 65 20 74  s an extensive t
105a0 65 73 74 20 6f 66 20 74 68 65 20 42 69 74 76 65  est of the Bitve
105a1 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  c code..**.** Th
105a2 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 61 72  e input is an ar
105a3 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20  ray of integers 
105a4 74 68 61 74 20 61 63 74 73 20 61 73 20 61 20 70  that acts as a p
105a5 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73  rogram.** to tes
105a6 74 20 74 68 65 20 42 69 74 76 65 63 2e 20 20 54  t the Bitvec.  T
105a7 68 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20  he integers are 
105a8 6f 70 63 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64  opcodes followed
105a9 0a 2a 2a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20  .** by 0, 1, or 
105aa 33 20 6f 70 65 72 61 6e 64 73 2c 20 64 65 70 65  3 operands, depe
105ab 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63  nding on the opc
105ac 6f 64 65 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a  ode.  Another.**
105ad 20 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20   opcode follows 
105ae 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
105af 72 20 74 68 65 20 6c 61 73 74 20 6f 70 65 72 61  r the last opera
105b0 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  nd..**.** There 
105b1 61 72 65 20 36 20 6f 70 63 6f 64 65 73 20 6e 75  are 6 opcodes nu
105b2 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 20 74 68  mbered from 0 th
105b3 72 6f 75 67 68 20 35 2e 20 20 30 20 69 73 20 74  rough 5.  0 is t
105b4 68 65 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63  he.** "halt" opc
105b5 6f 64 65 20 61 6e 64 20 63 61 75 73 65 73 20 74  ode and causes t
105b6 68 65 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a  he test to end..
105b7 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20  **.**    0      
105b8 20 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74      Halt and ret
105b9 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
105ba 66 20 65 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31  f errors.**    1
105bb 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20   N S X    Set N 
105bc 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77  bits beginning w
105bd 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d  ith S and increm
105be 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20  enting by X.**  
105bf 20 20 32 20 4e 20 53 20 58 20 20 20 20 43 6c 65    2 N S X    Cle
105c0 61 72 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e  ar N bits beginn
105c1 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69  ing with S and i
105c2 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58  ncrementing by X
105c3 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20 20 20 20  .**    3 N      
105c4 20 20 53 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79    Set N randomly
105c5 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20   chosen bits.** 
105c6 20 20 20 34 20 4e 20 20 20 20 20 20 20 20 43 6c     4 N        Cl
105c7 65 61 72 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63  ear N randomly c
105c8 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20  hosen bits.**   
105c9 20 35 20 4e 20 53 20 58 20 20 20 20 53 65 74 20   5 N S X    Set 
105ca 4e 20 62 69 74 73 20 66 72 6f 6d 20 53 20 69 6e  N bits from S in
105cb 63 72 65 6d 65 6e 74 20 58 20 69 6e 20 61 72 72  crement X in arr
105cc 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20  ay only, not in 
105cd 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65  bitvec.**.** The
105ce 20 6f 70 63 6f 64 65 73 20 31 20 74 68 72 6f 75   opcodes 1 throu
105cf 67 68 20 34 20 70 65 72 66 6f 72 6d 20 73 65 74  gh 4 perform set
105d0 20 61 6e 64 20 63 6c 65 61 72 20 6f 70 65 72 61   and clear opera
105d1 74 69 6f 6e 73 20 61 72 65 20 70 65 72 66 6f 72  tions are perfor
105d2 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61  med.** on both a
105d3 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 61   Bitvec object a
105d4 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61  nd on a linear a
105d5 72 72 61 79 20 6f 66 20 62 69 74 73 20 6f 62 74  rray of bits obt
105d6 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
105d7 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77  c..** Opcode 5 w
105d8 6f 72 6b 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65  orks on the line
105d9 61 72 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e  ar array only, n
105da 6f 74 20 6f 6e 20 74 68 65 20 42 69 74 76 65 63  ot on the Bitvec
105db 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 69 73  ..** Opcode 5 is
105dc 20 75 73 65 64 20 74 6f 20 64 65 6c 69 62 65 72   used to deliber
105dd 61 74 65 6c 79 20 69 6e 64 75 63 65 20 61 20 66  ately induce a f
105de 61 75 6c 74 20 69 6e 20 6f 72 64 65 72 20 74 6f  ault in order to
105df 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 68 61 74  .** confirm that
105e0 20 65 72 72 6f 72 20 64 65 74 65 63 74 69 6f 6e   error detection
105e1 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74   works..**.** At
105e2 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
105e3 6f 66 20 74 68 65 20 74 65 73 74 20 74 68 65 20  of the test the 
105e4 6c 69 6e 65 61 72 20 61 72 72 61 79 20 69 73 20  linear array is 
105e5 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 67 61 69  compared.** agai
105e6 6e 73 74 20 74 68 65 20 42 69 74 76 65 63 20 6f  nst the Bitvec o
105e7 62 6a 65 63 74 2e 20 20 49 66 20 74 68 65 72 65  bject.  If there
105e8 20 61 72 65 20 61 6e 79 20 64 69 66 66 65 72 65   are any differe
105e9 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f  nces,.** an erro
105ea 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  r is returned.  
105eb 49 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20  If they are the 
105ec 73 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65  same, zero is re
105ed 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
105ee 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
105ef 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
105f0 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f  s, return -1..*/
105f1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
105f2 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65  int sqlite3Bitve
105f3 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74  cBuiltinTest(int
105f4 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a   sz, int *aOp){.
105f5 20 20 42 69 74 76 65 63 20 2a 70 42 69 74 76 65    Bitvec *pBitve
105f6 63 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65  c = 0;.  unsigne
105f7 64 20 63 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a  d char *pV = 0;.
105f8 20 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20    int rc = -1;. 
105f9 20 69 6e 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20   int i, nx, pc, 
105fa 6f 70 3b 0a 20 20 76 6f 69 64 20 2a 70 54 6d 70  op;.  void *pTmp
105fb 53 70 61 63 65 3b 0a 0a 20 20 2f 2a 20 41 6c 6c  Space;..  /* All
105fc 6f 63 61 74 65 20 74 68 65 20 42 69 74 76 65 63  ocate the Bitvec
105fd 20 74 6f 20 62 65 20 74 65 73 74 65 64 20 61 6e   to be tested an
105fe 64 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79  d a linear array
105ff 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f   of.  ** bits to
10600 20 61 63 74 20 61 73 20 74 68 65 20 72 65 66 65   act as the refe
10601 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42 69 74 76  rence */.  pBitv
10602 65 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  ec = sqlite3Bitv
10603 65 63 43 72 65 61 74 65 28 20 73 7a 20 29 3b 0a  ecCreate( sz );.
10604 20 20 70 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d    pV = sqlite3_m
10605 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20  alloc( (sz+7)/8 
10606 2b 20 31 20 29 3b 0a 20 20 70 54 6d 70 53 70 61  + 1 );.  pTmpSpa
10607 63 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ce = sqlite3_mal
10608 6c 6f 63 28 42 49 54 56 45 43 5f 53 5a 29 3b 0a  loc(BITVEC_SZ);.
10609 20 20 69 66 28 20 70 42 69 74 76 65 63 3d 3d 30    if( pBitvec==0
1060a 20 7c 7c 20 70 56 3d 3d 30 20 7c 7c 20 70 54 6d   || pV==0 || pTm
1060b 70 53 70 61 63 65 3d 3d 30 20 20 29 20 67 6f 74  pSpace==0  ) got
1060c 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20  o bitvec_end;.  
1060d 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c 20 28 73  memset(pV, 0, (s
1060e 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0a 0a 20 20  z+7)/8 + 1);..  
1060f 2f 2a 20 4e 55 4c 4c 20 70 42 69 74 76 65 63 20  /* NULL pBitvec 
10610 74 65 73 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74  tests */.  sqlit
10611 65 33 42 69 74 76 65 63 53 65 74 28 30 2c 20 31  e3BitvecSet(0, 1
10612 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76  );.  sqlite3Bitv
10613 65 63 43 6c 65 61 72 28 30 2c 20 31 2c 20 70 54  ecClear(0, 1, pT
10614 6d 70 53 70 61 63 65 29 3b 0a 0a 20 20 2f 2a 20  mpSpace);..  /* 
10615 52 75 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20  Run the program 
10616 2a 2f 0a 20 20 70 63 20 3d 20 30 3b 0a 20 20 77  */.  pc = 0;.  w
10617 68 69 6c 65 28 20 28 6f 70 20 3d 20 61 4f 70 5b  hile( (op = aOp[
10618 70 63 5d 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  pc])!=0 ){.    s
10619 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
1061a 20 20 20 63 61 73 65 20 31 3a 0a 20 20 20 20 20     case 1:.     
1061b 20 63 61 73 65 20 32 3a 0a 20 20 20 20 20 20 63   case 2:.      c
1061c 61 73 65 20 35 3a 20 7b 0a 20 20 20 20 20 20 20  ase 5: {.       
1061d 20 6e 78 20 3d 20 34 3b 0a 20 20 20 20 20 20 20   nx = 4;.       
1061e 20 69 20 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d   i = aOp[pc+2] -
1061f 20 31 3b 0a 20 20 20 20 20 20 20 20 61 4f 70 5b   1;.        aOp[
10620 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b  pc+2] += aOp[pc+
10621 33 5d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  3];.        brea
10622 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
10623 20 63 61 73 65 20 33 3a 0a 20 20 20 20 20 20 63   case 3:.      c
10624 61 73 65 20 34 3a 20 0a 20 20 20 20 20 20 64 65  ase 4: .      de
10625 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
10626 20 6e 78 20 3d 20 32 3b 0a 20 20 20 20 20 20 20   nx = 2;.       
10627 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
10628 65 73 73 28 73 69 7a 65 6f 66 28 69 29 2c 20 26  ess(sizeof(i), &
10629 69 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  i);.        brea
1062a 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1062b 0a 20 20 20 20 69 66 28 20 28 2d 2d 61 4f 70 5b  .    if( (--aOp[
1062c 70 63 2b 31 5d 29 20 3e 20 30 20 29 20 6e 78 20  pc+1]) > 0 ) nx 
1062d 3d 20 30 3b 0a 20 20 20 20 70 63 20 2b 3d 20 6e  = 0;.    pc += n
1062e 78 3b 0a 20 20 20 20 69 20 3d 20 28 69 20 26 20  x;.    i = (i & 
1062f 30 78 37 66 66 66 66 66 66 66 29 25 73 7a 3b 0a  0x7fffffff)%sz;.
10630 20 20 20 20 69 66 28 20 28 6f 70 20 26 20 31 29      if( (op & 1)
10631 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 45 54  !=0 ){.      SET
10632 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a  BIT(pV, (i+1));.
10633 20 20 20 20 20 20 69 66 28 20 6f 70 21 3d 35 20        if( op!=5 
10634 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
10635 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
10636 70 42 69 74 76 65 63 2c 20 69 2b 31 29 20 29 20  pBitvec, i+1) ) 
10637 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b  goto bitvec_end;
10638 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
10639 73 65 7b 0a 20 20 20 20 20 20 43 4c 45 41 52 42  se{.      CLEARB
1063a 49 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20  IT(pV, (i+1));. 
1063b 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
1063c 65 63 43 6c 65 61 72 28 70 42 69 74 76 65 63 2c  ecClear(pBitvec,
1063d 20 69 2b 31 2c 20 70 54 6d 70 53 70 61 63 65 29   i+1, pTmpSpace)
1063e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
1063f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 73  * Test to make s
10640 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20 61  ure the linear a
10641 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61 74  rray exactly mat
10642 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42 69  ches the.  ** Bi
10643 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53 74  tvec object.  St
10644 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73 73  art with the ass
10645 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68 65  umption that the
10646 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 20  y do.  ** match 
10647 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67 65  (rc==0).  Change
10648 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20   rc to non-zero 
10649 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63 79  if a discrepancy
1064a 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e 0a  .  ** is found..
1064b 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
1064c 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30 2c  te3BitvecTest(0,
1064d 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76  0) + sqlite3Bitv
1064e 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 20  ecTest(pBitvec, 
1064f 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20 20  sz+1).          
10650 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  + sqlite3BitvecT
10651 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29 0a  est(pBitvec, 0).
10652 20 20 20 20 20 20 20 20 20 20 2b 20 28 73 71 6c            + (sql
10653 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70  ite3BitvecSize(p
10654 42 69 74 76 65 63 29 20 2d 20 73 7a 29 3b 0a 20  Bitvec) - sz);. 
10655 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b   for(i=1; i<=sz;
10656 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 20   i++){.    if(  
10657 28 54 45 53 54 42 49 54 28 70 56 2c 69 29 29 21  (TESTBIT(pV,i))!
10658 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65  =sqlite3BitvecTe
10659 73 74 28 70 42 69 74 76 65 63 2c 69 29 20 29 7b  st(pBitvec,i) ){
1065a 0a 20 20 20 20 20 20 72 63 20 3d 20 69 3b 0a 20  .      rc = i;. 
1065b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1065c 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65  }.  }..  /* Free
1065d 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 75 63   allocated struc
1065e 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65  ture */.bitvec_e
1065f 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  nd:.  sqlite3_fr
10660 65 65 28 70 54 6d 70 53 70 61 63 65 29 3b 0a 20  ee(pTmpSpace);. 
10661 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56   sqlite3_free(pV
10662 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76  );.  sqlite3Bitv
10663 65 63 44 65 73 74 72 6f 79 28 70 42 69 74 76 65  ecDestroy(pBitve
10664 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
10665 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
10666 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
10667 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  _TEST */../*****
10668 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
10669 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a   bitvec.c ******
1066a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1066b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1066c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1066d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1066e 66 69 6c 65 20 70 63 61 63 68 65 2e 63 20 2a 2a  file pcache.c **
1066f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10671 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
10672 32 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a  2008 August 05.*
10673 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
10674 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
10675 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
10676 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
10677 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
10678 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
10679 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1067a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1067b 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1067c 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1067d 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1067e 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1067f 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
10680 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
10681 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
10682 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
10683 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
10684 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
10685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10686 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
10689 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
1068a 6d 65 6e 74 73 20 74 68 61 74 20 70 61 67 65 20  ments that page 
1068b 63 61 63 68 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  cache..*/../*.**
1068c 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65   A complete page
1068d 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73   cache is an ins
1068e 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
1068f 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
10690 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67  ct PCache {.  Pg
10691 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44  Hdr *pDirty, *pD
10692 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20  irtyTail;       
10693 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72    /* List of dir
10694 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20  ty pages in LRU 
10695 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72  order */.  PgHdr
10696 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20   *pSynced;      
10697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10698 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61  * Last synced pa
10699 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65  ge in dirty page
1069a 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e   list */.  int n
1069b 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
1069c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1069d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  * Number of refe
1069e 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a  renced pages */.
1069f 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20    int nMax;     
106a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106a1 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
106a2 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a  red cache size *
106a3 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20  /.  int szPage; 
106a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106a5 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
106a6 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 69 6e  of every page in
106a7 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20   this cache */. 
106a8 20 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20   int szExtra;   
106a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106aa 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
106ab 65 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 20  extra space for 
106ac 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69  each page */.  i
106ad 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20  nt bPurgeable;  
106ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106af 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61     /* True if pa
106b0 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69  ges are on backi
106b1 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e  ng store */.  in
106b2 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69  t (*xStress)(voi
106b3 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 20  d*,PgHdr*);     
106b4 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79    /* Call to try
106b5 20 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65   make a page cle
106b6 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  an */.  void *pS
106b7 74 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20  tress;          
106b8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
106b9 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65  rgument to xStre
106ba 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ss */.  sqlite3_
106bb 70 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20  pcache *pCache; 
106bc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
106bd 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d  luggable cache m
106be 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 72  odule */.  PgHdr
106bf 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 20   *pPage1;       
106c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
106c1 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70  * Reference to p
106c2 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  age 1 */.};../*.
106c3 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61  ** Some of the a
106c4 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 69  ssert() macros i
106c5 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 20  n this code are 
106c6 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 6f  too expensive to
106c7 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 72   run.** even dur
106c8 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 67  ing normal debug
106c9 67 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d 20  ging.  Use them 
106ca 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 6c  only rarely on l
106cb 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 74  ong-running.** t
106cc 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 68  ests.  Enable th
106cd 65 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65  e expensive asse
106ce 72 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  rts using the.**
106cf 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45   -DSQLITE_ENABLE
106d0 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52  _EXPENSIVE_ASSER
106d1 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  T=1 compile-time
106d2 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64   option..*/.#ifd
106d3 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
106d4 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52  _EXPENSIVE_ASSER
106d5 54 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e  T.# define expen
106d6 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 20  sive_assert(X)  
106d7 61 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 0a  assert(X).#else.
106d8 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 69  # define expensi
106d9 76 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 6e  ve_assert(X).#en
106da 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
106db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106dc 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20  ******** Linked 
106dd 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20  List Management 
106de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106df 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  ****/..#if !defi
106e0 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64  ned(NDEBUG) && d
106e1 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
106e2 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41  ABLE_EXPENSIVE_A
106e3 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 65  SSERT)./*.** Che
106e4 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 63  ck that the pCac
106e5 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 69  he->pSynced vari
106e6 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 72  able is set corr
106e7 65 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a 20  ectly. If it.** 
106e8 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 66  is not, either f
106e9 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f 72  ail an assert or
106ea 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f 74   return zero. Ot
106eb 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0a  herwise, return.
106ec 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 69  ** non-zero. Thi
106ed 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  s is only used i
106ee 6e 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c  n debugging buil
106ef 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ds, as follows:.
106f0 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 76  **.**   expensiv
106f1 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65  e_assert( pcache
106f2 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63  CheckSynced(pCac
106f3 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63  he) );.*/.static
106f4 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 6b   int pcacheCheck
106f5 53 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a 70  Synced(PCache *p
106f6 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20  Cache){.  PgHdr 
106f7 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63  *p;.  for(p=pCac
106f8 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20  he->pDirtyTail; 
106f9 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63  p!=pCache->pSync
106fa 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 50  ed; p=p->pDirtyP
106fb 72 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 74  rev){.    assert
106fc 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d  ( p->nRef || (p-
106fd 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
106fe 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a 20  D_SYNC) );.  }. 
106ff 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c 7c   return (p==0 ||
10700 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e   p->nRef || (p->
10701 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
10702 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 65  _SYNC)==0);.}.#e
10703 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20  ndif /* !NDEBUG 
10704 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
10705 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52  _EXPENSIVE_ASSER
10706 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  T */../*.** Remo
10707 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72  ve page pPage fr
10708 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 64  om the list of d
10709 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 73  irty pages..*/.s
1070a 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
1070b 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79  eRemoveFromDirty
1070c 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 67  List(PgHdr *pPag
1070d 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20  e){.  PCache *p 
1070e 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b  = pPage->pCache;
1070f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
10710 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c 7c  e->pDirtyNext ||
10711 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74   pPage==p->pDirt
10712 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 72  yTail );.  asser
10713 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79  t( pPage->pDirty
10714 50 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d 70  Prev || pPage==p
10715 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 2f  ->pDirty );..  /
10716 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61  * Update the PCa
10717 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72  che1.pSynced var
10718 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61  iable if necessa
10719 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ry. */.  if( p->
1071a 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 29  pSynced==pPage )
1071b 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 79  {.    PgHdr *pSy
1071c 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44  nced = pPage->pD
1071d 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 68  irtyPrev;.    wh
1071e 69 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 20  ile( pSynced && 
1071f 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 26  (pSynced->flags&
10720 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
10721 20 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 65   ){.      pSynce
10722 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 69  d = pSynced->pDi
10723 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20  rtyPrev;.    }. 
10724 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20     p->pSynced = 
10725 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 20  pSynced;.  }..  
10726 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74  if( pPage->pDirt
10727 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 61  yNext ){.    pPa
10728 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e  ge->pDirtyNext->
10729 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61  pDirtyPrev = pPa
1072a 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a  ge->pDirtyPrev;.
1072b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1072c 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70  ert( pPage==p->p
1072d 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 20  DirtyTail );.   
1072e 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d   p->pDirtyTail =
1072f 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
10730 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  ev;.  }.  if( pP
10731 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20  age->pDirtyPrev 
10732 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44  ){.    pPage->pD
10733 69 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 79  irtyPrev->pDirty
10734 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 44  Next = pPage->pD
10735 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c 73  irtyNext;.  }els
10736 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
10737 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20  Page==p->pDirty 
10738 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79  );.    p->pDirty
10739 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79   = pPage->pDirty
1073a 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67  Next;.  }.  pPag
1073b 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20  e->pDirtyNext = 
1073c 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72  0;.  pPage->pDir
1073d 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 65  tyPrev = 0;..  e
1073e 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28  xpensive_assert(
1073f 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63   pcacheCheckSync
10740 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ed(p) );.}../*.*
10741 2a 20 41 64 64 20 70 61 67 65 20 70 50 61 67 65  * Add page pPage
10742 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20   to the head of 
10743 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 28  the dirty list (
10744 50 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 69  PCache1.pDirty i
10745 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 67  s set to.** pPag
10746 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e)..*/.static vo
10747 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 69  id pcacheAddToDi
10748 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70  rtyList(PgHdr *p
10749 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20  Page){.  PCache 
1074a 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63  *p = pPage->pCac
1074b 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  he;..  assert( p
1074c 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
1074d 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 44  ==0 && pPage->pD
1074e 69 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 70  irtyPrev==0 && p
1074f 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 20  ->pDirty!=pPage 
10750 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 69  );..  pPage->pDi
10751 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69  rtyNext = p->pDi
10752 72 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65  rty;.  if( pPage
10753 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a  ->pDirtyNext ){.
10754 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
10755 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70  e->pDirtyNext->p
10756 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a  DirtyPrev==0 );.
10757 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74      pPage->pDirt
10758 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65  yNext->pDirtyPre
10759 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20  v = pPage;.  }. 
1075a 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61   p->pDirty = pPa
1075b 67 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 44  ge;.  if( !p->pD
1075c 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 20  irtyTail ){.    
1075d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20  p->pDirtyTail = 
1075e 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28  pPage;.  }.  if(
1075f 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 20   !p->pSynced && 
10760 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 73  0==(pPage->flags
10761 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
10762 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 6e  ) ){.    p->pSyn
10763 63 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d  ced = pPage;.  }
10764 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73  .  expensive_ass
10765 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b  ert( pcacheCheck
10766 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a  Synced(p) );.}..
10767 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72  /*.** Wrapper ar
10768 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62  ound the pluggab
10769 6c 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e  le caches xUnpin
1076a 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20   method. If the 
1076b 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e  cache is.** bein
1076c 67 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e  g used for an in
1076d 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
1076e 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
1076f 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73  is a no-op..*/.s
10770 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10771 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29  eUnpin(PgHdr *p)
10772 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63  {.  PCache *pCac
10773 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a  he = p->pCache;.
10774 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50    if( pCache->bP
10775 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20  urgeable ){.    
10776 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29  if( p->pgno==1 )
10777 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e  {.      pCache->
10778 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20  pPage1 = 0;.    
10779 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  }.    sqlite3Glo
1077a 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
1077b 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e  .xUnpin(pCache->
1077c 70 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a 20  pCache, p, 0);. 
1077d 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
1077e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1077f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72  ********** Gener
10781 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a  al Interfaces **
10782 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69  ****.**.** Initi
10783 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f  alize and shutdo
10784 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  wn the page cach
10785 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69  e subsystem. Nei
10786 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a  ther of these .*
10787 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  * functions are 
10788 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53  threadsafe..*/.S
10789 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1078a 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49  t sqlite3PcacheI
1078b 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b  nitialize(void){
1078c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
1078d 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
1078e 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20  e.xInit==0 ){.  
1078f 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53    sqlite3PCacheS
10790 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d  etDefault();.  }
10791 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
10792 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
10793 61 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 74  ache.xInit(sqlit
10794 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
10795 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a 53  cache.pArg);.}.S
10796 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
10797 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
10798 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a  Shutdown(void){.
10799 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
1079a 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
1079b 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20  .xShutdown ){.  
1079c 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
1079d 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68  onfig.pcache.xSh
1079e 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c  utdown(sqlite3Gl
1079f 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
107a0 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a  e.pArg);.  }.}..
107a1 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
107a2 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
107a3 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 63  f a PCache objec
107a4 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
107a5 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
107a6 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29  PcacheSize(void)
107a7 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28  { return sizeof(
107a8 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a  PCache); }../*.*
107a9 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 50  * Create a new P
107aa 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 74  Cache object. St
107ab 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 68  orage space to h
107ac 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a 2a  old the object.*
107ad 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  * has already be
107ae 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  en allocated and
107af 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73   is passed in as
107b0 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e 20   the p pointer. 
107b1 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64  .** The caller d
107b2 69 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 63  iscovers how muc
107b3 68 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 6f  h space needs to
107b4 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79   be allocated by
107b5 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 6c   .** calling sql
107b6 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29  ite3PcacheSize()
107b7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
107b8 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
107b9 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e  PcacheOpen(.  in
107ba 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20  t szPage,       
107bb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
107bc 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65  ze of every page
107bd 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72   */.  int szExtr
107be 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a,              
107bf 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63     /* Extra spac
107c0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
107c1 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20  h each page */. 
107c2 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c   int bPurgeable,
107c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
107c4 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61   True if pages a
107c5 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74  re on backing st
107c6 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  ore */.  int (*x
107c7 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67  Stress)(void*,Pg
107c8 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f  Hdr*),/* Call to
107c9 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67   try to make pag
107ca 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f  es clean */.  vo
107cb 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20  id *pStress,    
107cc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
107cd 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73  gument to xStres
107ce 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70  s */.  PCache *p
107cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
107d0 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61      /* Prealloca
107d1 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68  ted space for th
107d2 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20  e PCache */.){. 
107d3 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69   memset(p, 0, si
107d4 7a 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a 20  zeof(PCache));. 
107d5 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50   p->szPage = szP
107d6 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72  age;.  p->szExtr
107d7 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 70  a = szExtra;.  p
107d8 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 62  ->bPurgeable = b
107d9 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e  Purgeable;.  p->
107da 78 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 73  xStress = xStres
107db 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 20  s;.  p->pStress 
107dc 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e  = pStress;.  p->
107dd 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a 2f  nMax = 100;.}../
107de 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
107df 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43  page size for PC
107e0 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65  ache object. The
107e1 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73   caller must ens
107e2 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0a 2a  ure that there.*
107e3 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e  * are no outstan
107e4 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65  ding page refere
107e5 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 66  nces when this f
107e6 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
107e7 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
107e8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
107e9 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69  3PcacheSetPageSi
107ea 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68  ze(PCache *pCach
107eb 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b 0a  e, int szPage){.
107ec 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65    assert( pCache
107ed 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 61  ->nRef==0 && pCa
107ee 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 29  che->pDirty==0 )
107ef 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e  ;.  if( pCache->
107f0 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71  pCache ){.    sq
107f1 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
107f2 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f  g.pcache.xDestro
107f3 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  y(pCache->pCache
107f4 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70  );.    pCache->p
107f5 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  Cache = 0;.  }. 
107f6 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20   pCache->szPage 
107f7 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a  = szPage;.}../*.
107f8 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e  ** Try to obtain
107f9 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
107fa 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54   cache..*/.SQLIT
107fb 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
107fc 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
107fd 28 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63  (.  PCache *pCac
107fe 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74  he,       /* Obt
107ff 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f  ain the page fro
10800 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a  m this cache */.
10801 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20    Pgno pgno,    
10802 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
10803 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e  number to obtain
10804 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65   */.  int create
10805 46 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49  Flag,       /* I
10806 66 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70  f true, create p
10807 61 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e  age if it does n
10808 6f 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79  ot exist already
10809 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70   */.  PgHdr **pp
1080a 50 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57  Page        /* W
1080b 72 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65  rite the page he
1080c 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72  re */.){.  PgHdr
1080d 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69   *pPage = 0;.  i
1080e 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 61  nt eCreate;..  a
1080f 73 73 65 72 74 28 20 70 43 61 63 68 65 21 3d 30  ssert( pCache!=0
10810 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 72   );.  assert( cr
10811 65 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 63  eateFlag==1 || c
10812 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b 0a  reateFlag==0 );.
10813 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30    assert( pgno>0
10814 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
10815 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65   pluggable cache
10816 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65   (sqlite3_pcache
10817 2a 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  *) has not been 
10818 61 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20  allocated,.  ** 
10819 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e  allocate it now.
1081a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61  .  */.  if( !pCa
1081b 63 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63  che->pCache && c
1081c 72 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20  reateFlag ){.   
1081d 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
1081e 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74  *p;.    int nByt
1081f 65 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70  e;.    nByte = p
10820 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20  Cache->szPage + 
10821 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20  pCache->szExtra 
10822 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b  + sizeof(PgHdr);
10823 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
10824 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
10825 63 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74  che.xCreate(nByt
10826 65 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  e, pCache->bPurg
10827 65 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20  eable);.    if( 
10828 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  !p ){.      retu
10829 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1082a 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1082b 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
1082c 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65  cache.xCachesize
1082d 28 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  (p, pCache->nMax
1082e 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70  );.    pCache->p
1082f 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a  Cache = p;.  }..
10830 20 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61    eCreate = crea
10831 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 21  teFlag * (1 + (!
10832 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
10833 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70  le || !pCache->p
10834 44 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 70  Dirty));.  if( p
10835 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b  Cache->pCache ){
10836 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c  .    pPage = sql
10837 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
10838 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70  .pcache.xFetch(p
10839 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
1083a 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20  gno, eCreate);. 
1083b 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65   }..  if( !pPage
1083c 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29   && eCreate==1 )
1083d 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67  {.    PgHdr *pPg
1083e 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  ;..    /* Find a
1083f 20 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 77   dirty page to w
10840 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63  rite-out and rec
10841 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20  ycle. First try 
10842 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a  to find a .    *
10843 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73  * page that does
10844 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a   not require a j
10845 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65  ournal-sync (one
10846 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44   with PGHDR_NEED
10847 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65  _SYNC.    ** cle
10848 61 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 68  ared), but if th
10849 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  at is not possib
1084a 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e  le settle for an
1084b 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20  y other .    ** 
1084c 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 72  unreferenced dir
1084d 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a  ty page..    */.
1084e 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73      expensive_as
1084f 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63  sert( pcacheChec
10850 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20  kSynced(pCache) 
10851 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70  );.    for(pPg=p
10852 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20  Cache->pSynced; 
10853 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20  .        pPg && 
10854 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70  (pPg->nRef || (p
10855 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
10856 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20  NEED_SYNC)); .  
10857 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70        pPg=pPg->p
10858 44 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b  DirtyPrev.    );
10859 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b  .    if( !pPg ){
1085a 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70  .      for(pPg=p
1085b 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69  Cache->pDirtyTai
1085c 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e  l; pPg && pPg->n
1085d 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44  Ref; pPg=pPg->pD
1085e 69 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d  irtyPrev);.    }
1085f 0a 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a  .    if( pPg ){.
10860 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20        int rc;.  
10861 20 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d      rc = pCache-
10862 3e 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d  >xStress(pCache-
10863 3e 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a  >pStress, pPg);.
10864 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
10865 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53  LITE_OK && rc!=S
10866 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
10867 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
10868 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
10869 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69      pPage = sqli
1086a 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
1086b 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43  pcache.xFetch(pC
1086c 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67  ache->pCache, pg
1086d 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69  no, 2);.  }..  i
1086e 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20  f( pPage ){.    
1086f 69 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74  if( !pPage->pDat
10870 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65  a ){.      memse
10871 74 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65  t(pPage, 0, size
10872 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 63  of(PgHdr) + pCac
10873 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20  he->szExtra);.  
10874 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72      pPage->pExtr
10875 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67  a = (void*)&pPag
10876 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67  e[1];.      pPag
10877 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64  e->pData = (void
10878 20 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 61   *)&((char *)pPa
10879 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72  ge)[sizeof(PgHdr
1087a 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78  ) + pCache->szEx
1087b 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67  tra];.      pPag
1087c 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63  e->pCache = pCac
1087d 68 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d  he;.      pPage-
1087e 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20  >pgno = pgno;.  
1087f 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
10880 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70  pPage->pCache==p
10881 43 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73  Cache );.    ass
10882 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ert( pPage->pgno
10883 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73  ==pgno );.    as
10884 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78  sert( pPage->pEx
10885 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50  tra==(void *)&pP
10886 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69  age[1] );..    i
10887 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65  f( 0==pPage->nRe
10888 66 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68  f ){.      pCach
10889 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d  e->nRef++;.    }
1088a 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66  .    pPage->nRef
1088b 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f  ++;.    if( pgno
1088c 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61  ==1 ){.      pCa
1088d 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50  che->pPage1 = pP
1088e 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  age;.    }.  }. 
1088f 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65   *ppPage = pPage
10890 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67  ;.  return (pPag
10891 65 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 29  e==0 && eCreate)
10892 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20   ? SQLITE_NOMEM 
10893 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  : SQLITE_OK;.}..
10894 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20  /*.** Decrement 
10895 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
10896 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49  unt on a page. I
10897 66 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c  f the page is cl
10898 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72  ean and the.** r
10899 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64  eference count d
1089a 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20  rops to 0, then 
1089b 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c  it is made elibl
1089c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e  e for recycling.
1089d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1089e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
1089f 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48  cacheRelease(PgH
108a0 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  dr *p){.  assert
108a1 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
108a2 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66   p->nRef--;.  if
108a3 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ( p->nRef==0 ){.
108a4 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 63      PCache *pCac
108a5 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a  he = p->pCache;.
108a6 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66      pCache->nRef
108a7 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e  --;.    if( (p->
108a8 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
108a9 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  Y)==0 ){.      p
108aa 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20  cacheUnpin(p);. 
108ab 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
108ac 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65  /* Move the page
108ad 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20   to the head of 
108ae 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20  the dirty list. 
108af 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 52  */.      pcacheR
108b0 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69  emoveFromDirtyLi
108b1 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61  st(p);.      pca
108b2 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73  cheAddToDirtyLis
108b3 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  t(p);.    }.  }.
108b4 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73  }../*.** Increas
108b5 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  e the reference 
108b6 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c  count of a suppl
108b7 69 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a  ied page by 1..*
108b8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
108b9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
108ba 63 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 29  cheRef(PgHdr *p)
108bb 7b 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52  {.  assert(p->nR
108bc 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66  ef>0);.  p->nRef
108bd 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f  ++;.}../*.** Dro
108be 70 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  p a page from th
108bf 65 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d  e cache. There m
108c0 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f  ust be exactly o
108c1 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  ne reference to 
108c2 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69  the.** page. Thi
108c3 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74  s function delet
108c4 65 73 20 74 68 61 74 20 72 65 66 65 72 65 6e 63  es that referenc
108c5 65 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 72  e, so after it r
108c6 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61  eturns the.** pa
108c7 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ge pointed to by
108c8 20 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a   p is invalid..*
108c9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
108ca 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
108cb 63 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70  cheDrop(PgHdr *p
108cc 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  ){.  PCache *pCa
108cd 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  che;.  assert( p
108ce 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69  ->nRef==1 );.  i
108cf 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44  f( p->flags&PGHD
108d0 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70  R_DIRTY ){.    p
108d1 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44  cacheRemoveFromD
108d2 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d  irtyList(p);.  }
108d3 0a 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70  .  pCache = p->p
108d4 43 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d  Cache;.  pCache-
108d5 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70  >nRef--;.  if( p
108d6 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20  ->pgno==1 ){.   
108d7 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20   pCache->pPage1 
108d8 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
108d9 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
108da 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61  cache.xUnpin(pCa
108db 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20  che->pCache, p, 
108dc 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  1);.}../*.** Mak
108dd 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20  e sure the page 
108de 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72  is marked as dir
108df 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20  ty. If it isn't 
108e0 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a  dirty already,.*
108e1 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f  * make it so..*/
108e2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
108e3 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
108e4 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64  heMakeDirty(PgHd
108e5 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67  r *p){.  p->flag
108e6 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54  s &= ~PGHDR_DONT
108e7 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74  _WRITE;.  assert
108e8 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
108e9 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67   if( 0==(p->flag
108ea 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29  s & PGHDR_DIRTY)
108eb 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73   ){.    p->flags
108ec 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b   |= PGHDR_DIRTY;
108ed 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f  .    pcacheAddTo
108ee 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20  DirtyList( p);. 
108ef 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
108f0 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69   sure the page i
108f1 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61  s marked as clea
108f2 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63  n. If it isn't c
108f3 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a  lean already,.**
108f4 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a   make it so..*/.
108f5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
108f6 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
108f7 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72  eMakeClean(PgHdr
108f8 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e   *p){.  if( (p->
108f9 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49  flags & PGHDR_DI
108fa 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63  RTY) ){.    pcac
108fb 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
108fc 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d  yList(p);.    p-
108fd 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44  >flags &= ~(PGHD
108fe 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45  R_DIRTY|PGHDR_NE
108ff 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66  ED_SYNC);.    if
10900 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ( p->nRef==0 ){.
10901 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69        pcacheUnpi
10902 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n(p);.    }.  }.
10903 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76  }../*.** Make ev
10904 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
10905 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a  cache clean..*/.
10906 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10907 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
10908 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65  eCleanAll(PCache
10909 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48   *pCache){.  PgH
1090a 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20  dr *p;.  while( 
1090b 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69  (p = pCache->pDi
1090c 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  rty)!=0 ){.    s
1090d 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
1090e 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a  Clean(p);.  }.}.
1090f 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
10910 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
10911 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64   flag from all d
10912 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53  irty pages..*/.S
10913 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
10914 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
10915 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50  ClearSyncFlags(P
10916 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
10917 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f    PgHdr *p;.  fo
10918 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72  r(p=pCache->pDir
10919 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72  ty; p; p=p->pDir
1091a 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e  tyNext){.    p->
1091b 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f  flags &= ~PGHDR_
1091c 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20  NEED_SYNC;.  }. 
1091d 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64   pCache->pSynced
1091e 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74   = pCache->pDirt
1091f 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  yTail;.}../*.** 
10920 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20  Change the page 
10921 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70  number of page p
10922 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f   to newPgno. .*/
10923 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10924 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
10925 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c  heMove(PgHdr *p,
10926 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a   Pgno newPgno){.
10927 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65    PCache *pCache
10928 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20   = p->pCache;.  
10929 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
1092a 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
1092b 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71  ewPgno>0 );.  sq
1092c 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
1092d 67 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28  g.pcache.xRekey(
1092e 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
1092f 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50  p, p->pgno, newP
10930 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20  gno);.  p->pgno 
10931 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28  = newPgno;.  if(
10932 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52   (p->flags&PGHDR
10933 5f 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66  _DIRTY) && (p->f
10934 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f  lags&PGHDR_NEED_
10935 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61  SYNC) ){.    pca
10936 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72  cheRemoveFromDir
10937 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70  tyList(p);.    p
10938 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c  cacheAddToDirtyL
10939 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ist(p);.  }.}../
1093a 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20  *.** Drop every 
1093b 63 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73  cache entry whos
1093c 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73  e page number is
1093d 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70   greater than "p
1093e 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c  gno". The.** cal
1093f 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20  ler must ensure 
10940 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e  that there are n
10941 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65  o outstanding re
10942 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20  ferences to any 
10943 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74  pages.** other t
10944 68 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20  han page 1 with 
10945 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72  a page number gr
10946 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e  eater than pgno.
10947 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
10948 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  is a reference t
10949 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65  o page 1 and the
1094a 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20   pgno parameter 
1094b 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a  passed to this.*
1094c 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c  * function is 0,
1094d 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61   then the data a
1094e 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  rea associated w
1094f 69 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65  ith page 1 is ze
10950 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65  roed, but.** the
10951 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20   page object is 
10952 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a  not dropped..*/.
10953 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10954 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
10955 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65  eTruncate(PCache
10956 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70   *pCache, Pgno p
10957 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63  gno){.  if( pCac
10958 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20  he->pCache ){.  
10959 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20    PgHdr *p;.    
1095a 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20  PgHdr *pNext;.  
1095b 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e    for(p=pCache->
1095c 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65  pDirty; p; p=pNe
1095d 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74  xt){.      pNext
1095e 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74   = p->pDirtyNext
1095f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70  ;.      if( p->p
10960 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20  gno>pgno ){.    
10961 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66      assert( p->f
10962 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
10963 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
10964 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
10965 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20  an(p);.      }. 
10966 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e     }.    if( pgn
10967 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e  o==0 && pCache->
10968 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20  pPage1 ){.      
10969 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70  memset(pCache->p
1096a 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c  Page1->pData, 0,
1096b 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29   pCache->szPage)
1096c 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31  ;.      pgno = 1
1096d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1096e 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
1096f 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65  pcache.xTruncate
10970 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
10971 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a   pgno+1);.  }.}.
10972 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63  ./*.** Close a c
10973 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ache..*/.SQLITE_
10974 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10975 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28  ite3PcacheClose(
10976 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
10977 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70  .  if( pCache->p
10978 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c  Cache ){.    sql
10979 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
1097a 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79  .pcache.xDestroy
1097b 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29  (pCache->pCache)
1097c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  ;.  }.}../* .** 
1097d 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74  Discard the cont
1097e 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68  ents of the cach
1097f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
10980 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
10981 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 61  3PcacheClear(PCa
10982 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  che *pCache){.  
10983 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75  sqlite3PcacheTru
10984 6e 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 29  ncate(pCache, 0)
10985 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65  ;.}../*.** Merge
10986 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61   two lists of pa
10987 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  ges connected by
10988 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70   pDirty and in p
10989 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f  gno order..** Do
1098a 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67   not both fixing
1098b 20 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20   the pDirtyPrev 
1098c 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61  pointers..*/.sta
1098d 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68  tic PgHdr *pcach
1098e 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28  eMergeDirtyList(
1098f 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72  PgHdr *pA, PgHdr
10990 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72   *pB){.  PgHdr r
10991 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20  esult, *pTail;. 
10992 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74   pTail = &result
10993 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26  ;.  while( pA &&
10994 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70   pB ){.    if( p
10995 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f  A->pgno<pB->pgno
10996 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d   ){.      pTail-
10997 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20  >pDirty = pA;.  
10998 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a      pTail = pA;.
10999 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70        pA = pA->p
1099a 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65  Dirty;.    }else
1099b 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70  {.      pTail->p
1099c 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20  Dirty = pB;.    
1099d 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20    pTail = pB;.  
1099e 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69      pB = pB->pDi
1099f 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  rty;.    }.  }. 
109a0 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70   if( pA ){.    p
109a1 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70  Tail->pDirty = p
109a2 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  A;.  }else if( p
109a3 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  B ){.    pTail->
109a4 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d  pDirty = pB;.  }
109a5 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d  else{.    pTail-
109a6 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d  >pDirty = 0;.  }
109a7 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74  .  return result
109a8 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a  .pDirty;.}../*.*
109a9 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20  * Sort the list 
109aa 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65  of pages in acce
109ab 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70  nding order by p
109ac 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a  gno.  Pages are.
109ad 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ** connected by 
109ae 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e  pDirty pointers.
109af 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76    The pDirtyPrev
109b0 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a   pointers are.**
109b1 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68   corrupted by th
109b2 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 53  is sort..**.** S
109b3 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e 6f  ince there canno
109b4 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 32  t be more than 2
109b5 5e 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 67  ^31 distinct pag
109b6 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65  es in a database
109b7 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e 6f  ,.** there canno
109b8 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 33  t be more than 3
109b9 31 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 72  1 buckets requir
109ba 65 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 20  ed by the merge 
109bb 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 65  sorter..** One e
109bc 78 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 61  xtra bucket is a
109bd 64 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76  dded to catch ov
109be 65 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 73  erflow in case s
109bf 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 72  omething.** ever
109c0 20 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65   changes to make
109c1 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
109c2 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74  ntence incorrect
109c3 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53  ..*/.#define N_S
109c4 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a 73  ORT_BUCKET  32.s
109c5 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61  tatic PgHdr *pca
109c6 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74  cheSortDirtyList
109c7 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20  (PgHdr *pIn){.  
109c8 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f  PgHdr *a[N_SORT_
109c9 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 69  BUCKET], *p;.  i
109ca 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61  nt i;.  memset(a
109cb 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b  , 0, sizeof(a));
109cc 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 7b  .  while( pIn ){
109cd 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20  .    p = pIn;.  
109ce 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74    pIn = p->pDirt
109cf 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79  y;.    p->pDirty
109d0 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d   = 0;.    for(i=
109d1 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 4f  0; ALWAYS(i<N_SO
109d2 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 2b  RT_BUCKET-1); i+
109d3 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b  +){.      if( a[
109d4 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  i]==0 ){.       
109d5 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20   a[i] = p;.     
109d6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
109d7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
109d8 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69   = pcacheMergeDi
109d9 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29  rtyList(a[i], p)
109da 3b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d  ;.        a[i] =
109db 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
109dc 7d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  }.    if( NEVER(
109dd 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  i==N_SORT_BUCKET
109de 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  -1) ){.      /* 
109df 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65  To get here, the
109e0 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e  re need to be 2^
109e1 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20  (N_SORT_BUCKET) 
109e2 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 20  elements in.    
109e3 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c    ** the input l
109e4 69 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 69  ist.  But that i
109e5 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 20  s impossible..  
109e6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69      */.      a[i
109e7 5d 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44  ] = pcacheMergeD
109e8 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70  irtyList(a[i], p
109e9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  );.    }.  }.  p
109ea 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69   = a[0];.  for(i
109eb 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43  =1; i<N_SORT_BUC
109ec 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  KET; i++){.    p
109ed 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69   = pcacheMergeDi
109ee 72 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29  rtyList(p, a[i])
109ef 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
109f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
109f1 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
109f2 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74  dirty pages in t
109f3 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64  he cache, sorted
109f4 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e   by page number.
109f5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
109f6 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65  TE PgHdr *sqlite
109f7 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74  3PcacheDirtyList
109f8 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
109f9 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20  {.  PgHdr *p;.  
109fa 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44  for(p=pCache->pD
109fb 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44  irty; p; p=p->pD
109fc 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70  irtyNext){.    p
109fd 2d 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44  ->pDirty = p->pD
109fe 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  irtyNext;.  }.  
109ff 72 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72  return pcacheSor
10a00 74 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 68  tDirtyList(pCach
10a01 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f  e->pDirty);.}../
10a02 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  * .** Return the
10a03 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
10a04 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65   referenced page
10a05 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61  s held by the ca
10a06 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  che..*/.SQLITE_P
10a07 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
10a08 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
10a09 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
10a0a 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68  {.  return pCach
10a0b 65 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a  e->nRef;.}../*.*
10a0c 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
10a0d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
10a0e 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 75  s to the page su
10a0f 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67  pplied as an arg
10a10 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ument..*/.SQLITE
10a11 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10a12 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65  ite3PcachePageRe
10a13 66 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29  fcount(PgHdr *p)
10a14 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52  {.  return p->nR
10a15 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65  ef;.}../* .** Re
10a16 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e  turn the total n
10a17 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
10a18 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a  n the cache..*/.
10a19 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
10a1a 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
10a1b 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65  Pagecount(PCache
10a1c 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74   *pCache){.  int
10a1d 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66   nPage = 0;.  if
10a1e 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  ( pCache->pCache
10a1f 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20   ){.    nPage = 
10a20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
10a21 66 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65  fig.pcache.xPage
10a22 63 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43  count(pCache->pC
10a23 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ache);.  }.  ret
10a24 75 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69  urn nPage;.}..#i
10a25 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
10a26 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73  ./*.** Get the s
10a27 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73  uggested cache-s
10a28 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51  ize value..*/.SQ
10a29 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10a2a 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65   sqlite3PcacheGe
10a2b 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68  tCachesize(PCach
10a2c 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65  e *pCache){.  re
10a2d 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61  turn pCache->nMa
10a2e 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  x;.}.#endif../*.
10a2f 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 65  ** Set the sugge
10a30 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20  sted cache-size 
10a31 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  value..*/.SQLITE
10a32 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10a33 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61  lite3PcacheSetCa
10a34 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a  chesize(PCache *
10a35 70 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61  pCache, int mxPa
10a36 67 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e  ge){.  pCache->n
10a37 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20  Max = mxPage;.  
10a38 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63  if( pCache->pCac
10a39 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  he ){.    sqlite
10a3a 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
10a3b 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28  ache.xCachesize(
10a3c 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
10a3d 6d 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a  mxPage);.  }.}..
10a3e 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
10a3f 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20  TE_CHECK_PAGES) 
10a40 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
10a41 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 46  E_DEBUG)./*.** F
10a42 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  or all dirty pag
10a43 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  es currently in 
10a44 74 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b  the cache, invok
10a45 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  e the specified.
10a46 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69  ** callback. Thi
10a47 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  s is only used i
10a48 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45  f the SQLITE_CHE
10a49 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69  CK_PAGES macro i
10a4a 73 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f  s.** defined..*/
10a4b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10a4c 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
10a4d 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50  heIterateDirty(P
10a4e 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76  Cache *pCache, v
10a4f 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48  oid (*xIter)(PgH
10a50 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20  dr *)){.  PgHdr 
10a51 2a 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70  *pDirty;.  for(p
10a52 44 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44  Dirty=pCache->pD
10a53 69 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44  irty; pDirty; pD
10a54 69 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69  irty=pDirty->pDi
10a55 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49  rtyNext){.    xI
10a56 74 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d  ter(pDirty);.  }
10a57 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  .}.#endif../****
10a58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
10a59 66 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a  f pcache.c *****
10a5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
10a5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
10a5e 20 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20   file pcache1.c 
10a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
10a62 20 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30   2008 November 0
10a63 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
10a64 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
10a65 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
10a66 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
10a67 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
10a68 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
10a69 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
10a6a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
10a6b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
10a6c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
10a6d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
10a6e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
10a6f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
10a70 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
10a71 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
10a72 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
10a73 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
10a74 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
10a75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a79 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
10a7a 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
10a7b 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63  default page cac
10a7c 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
10a7d 6e 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  n (the.** sqlite
10a7e 33 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61  3_pcache interfa
10a7f 63 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e  ce). It also con
10a80 74 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68  tains part of th
10a81 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
10a82 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54  .** of the SQLIT
10a83 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
10a84 48 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72  HE and sqlite3_r
10a85 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
10a86 66 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20  features..** If 
10a87 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  the default page
10a88 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
10a89 61 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64  ation is overrid
10a8a 65 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72  en, then neither
10a8b 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f   of.** these two
10a8c 20 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76   features are av
10a8d 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 0a 0a 74 79  ailable..*/...ty
10a8e 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61  pedef struct PCa
10a8f 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79  che1 PCache1;.ty
10a90 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48  pedef struct PgH
10a91 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65  dr1 PgHdr1;.type
10a92 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72 65  def struct PgFre
10a93 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74  eslot PgFreeslot
10a94 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74  ;../* Pointers t
10a95 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20  o structures of 
10a96 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 61  this type are ca
10a97 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20  st and returned 
10a98 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71  as .** opaque sq
10a99 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61  lite3_pcache* ha
10a9a 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20  ndles.*/.struct 
10a9b 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43  PCache1 {.  /* C
10a9c 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69  ache configurati
10a9d 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50  on parameters. P
10a9e 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65  age size (szPage
10a9f 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 61  ) and the purgea
10aa0 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62  ble.  ** flag (b
10aa1 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 73  Purgeable) are s
10aa2 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 68  et when the cach
10aa3 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d  e is created. nM
10aa4 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20  ax may be .  ** 
10aa5 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 20  modified at any 
10aa6 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74  time by a call t
10aa7 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 61 63  o the pcache1Cac
10aa8 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e  heSize() method.
10aa9 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c  .  ** The global
10aaa 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68   mutex must be h
10aab 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69  eld when accessi
10aac 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20  ng nMax..  */.  
10aad 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20  int szPage;     
10aae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10aaf 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
10ab0 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20 69  llocated pages i
10ab1 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74  n bytes */.  int
10ab2 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20   bPurgeable;    
10ab3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ab4 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68   /* True if cach
10ab5 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a  e is purgeable *
10ab6 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
10ab7 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20   nMin;          
10ab8 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
10ab9 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  um number of pag
10aba 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20  es reserved */. 
10abb 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d   unsigned int nM
10abc 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ax;             
10abd 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
10abe 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20  ed "cache_size" 
10abf 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48  value */..  /* H
10ac0 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c  ash table of all
10ac1 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c   pages. The foll
10ac2 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20  owing variables 
10ac3 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65  may only be acce
10ac4 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74  ssed.  ** when t
10ac5 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 20 68  he accessor is h
10ac6 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61  olding the globa
10ac7 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 63 61  l mutex (see pca
10ac8 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10ac9 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68   .  ** and pcach
10aca 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e  e1LeaveMutex()).
10acb 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  .  */.  unsigned
10acc 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65   int nRecyclable
10acd 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
10ace 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
10acf 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a  n the LRU list *
10ad0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
10ad1 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20   nPage;         
10ad2 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
10ad3 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10ad4 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20   in apHash */.  
10ad5 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61  unsigned int nHa
10ad6 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sh;             
10ad7 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
10ad8 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68   slots in apHash
10ad9 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a  [] */.  PgHdr1 *
10ada 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 20 20  *apHash;        
10adb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
10adc 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61  ash table for fa
10add 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79  st lookup by key
10ade 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20   */..  unsigned 
10adf 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20  int iMaxKey;    
10ae0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
10ae1 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73  rgest key seen s
10ae2 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28 29  ince xTruncate()
10ae3 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   */.};../*.** Ea
10ae4 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 69  ch cache entry i
10ae5 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79  s represented by
10ae6 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
10ae7 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a  the following .*
10ae8 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 20 62  * structure. A b
10ae9 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e  uffer of PgHdr1.
10aea 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62  pCache->szPage b
10aeb 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65  ytes is allocate
10aec 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62  d .** directly b
10aed 65 66 6f 72 65 20 74 68 69 73 20 73 74 72 75 63  efore this struc
10aee 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28  ture in memory (
10aef 73 65 65 20 74 68 65 20 50 47 48 44 52 31 5f 54  see the PGHDR1_T
10af0 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63  O_PAGE() .** mac
10af1 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74  ro below)..*/.st
10af2 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20  ruct PgHdr1 {.  
10af3 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65  unsigned int iKe
10af4 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  y;             /
10af5 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67  * Key value (pag
10af6 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50  e number) */.  P
10af7 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20  gHdr1 *pNext;   
10af8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10af9 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 61   Next in hash ta
10afa 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50  ble chain */.  P
10afb 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20  Cache1 *pCache; 
10afc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10afd 20 43 61 63 68 65 20 74 68 61 74 20 63 75 72 72   Cache that curr
10afe 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20  ently owns this 
10aff 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31  page */.  PgHdr1
10b00 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20   *pLruNext;     
10b01 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
10b02 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20   in LRU list of 
10b03 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a  unpinned pages *
10b04 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75  /.  PgHdr1 *pLru
10b05 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20  Prev;           
10b06 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69     /* Previous i
10b07 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e  n LRU list of un
10b08 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a  pinned pages */.
10b09 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73  };../*.** Free s
10b0a 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f  lots in the allo
10b0b 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69  cator used to di
10b0c 76 69 64 65 20 75 70 20 74 68 65 20 62 75 66 66  vide up the buff
10b0d 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e  er provided usin
10b0e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f  g.** the SQLITE_
10b0f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10b10 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73   mechanism..*/.s
10b11 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74  truct PgFreeslot
10b12 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20   {.  PgFreeslot 
10b13 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74  *pNext;  /* Next
10b14 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b   free slot */.};
10b15 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64  ../*.** Global d
10b16 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69 73  ata used by this
10b17 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69   cache..*/.stati
10b18 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
10b19 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c  uct PCacheGlobal
10b1a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   {.  sqlite3_mut
10b1b 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20  ex *mutex;      
10b1c 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74           /* stat
10b1d 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53  ic mutex MUTEX_S
10b1e 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20  TATIC_LRU */..  
10b1f 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20  int nMaxPage;   
10b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b21 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d      /* Sum of nM
10b22 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 65  axPage for purge
10b23 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20  able caches */. 
10b24 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20   int nMinPage;  
10b25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b26 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e       /* Sum of n
10b27 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67  MinPage for purg
10b28 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a  eable caches */.
10b29 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61    int nCurrentPa
10b2a 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
10b2b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
10b2c 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67  of purgeable pag
10b2d 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  es allocated */.
10b2e 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65    PgHdr1 *pLruHe
10b2f 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20  ad, *pLruTail;  
10b30 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73        /* LRU lis
10b31 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61  t of unpinned pa
10b32 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72  ges */..  /* Var
10b33 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74  iables related t
10b34 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  o SQLITE_CONFIG_
10b35 50 41 47 45 43 41 43 48 45 20 73 65 74 74 69 6e  PAGECACHE settin
10b36 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53  gs. */.  int szS
10b37 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  lot;            
10b38 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b39 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65  Size of each fre
10b3a 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64  e slot */.  void
10b3b 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b   *pStart, *pEnd;
10b3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b3d 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67  /* Bounds of pag
10b3e 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61  ecache malloc ra
10b3f 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73  nge */.  PgFrees
10b40 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20  lot *pFree;     
10b41 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b42 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73  Free page blocks
10b43 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74   */.  int isInit
10b44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10b45 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
10b46 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64  e if initialized
10b47 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b   */.} pcache1_g;
10b48 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65  ../*.** All code
10b49 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68   in this file sh
10b4a 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20  ould access the 
10b4b 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65  global structure
10b4c 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a   above via the.*
10b4d 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31  * alias "pcache1
10b4e 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20  ". This ensures 
10b4f 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75  that the WSD emu
10b50 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77  lation is used w
10b51 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67  hen.** compiling
10b52 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61   for systems tha
10b53 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74  t do not support
10b54 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64   real WSD..*/.#d
10b55 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 47  efine pcache1 (G
10b56 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 61  LOBAL(struct PCa
10b57 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68  cheGlobal, pcach
10b58 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68  e1_g))../*.** Wh
10b59 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 75  en a PgHdr1 stru
10b5a 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74  cture is allocat
10b5b 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74  ed, the associat
10b5c 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67  ed PCache1.szPag
10b5d 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61  e.** bytes of da
10b5e 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64  ta are located d
10b5f 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69  irectly before i
10b60 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65  t in memory (i.e
10b61 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73  . the total.** s
10b62 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
10b63 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28  ation is sizeof(
10b64 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e  PgHdr1)+PCache1.
10b65 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 68  szPage byte). Th
10b66 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50  e.** PGHDR1_TO_P
10b67 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65  AGE() macro take
10b68 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
10b69 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 72   PgHdr1 structur
10b6a 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d  e as.** an argum
10b6b 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ent and returns 
10b6c 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
10b6d 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63   associated bloc
10b6e 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62  k of szPage.** b
10b6f 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54  ytes. The PAGE_T
10b70 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f  O_PGHDR1() macro
10b71 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69   does the opposi
10b72 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74  te: its argument
10b73 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72   is.** a pointer
10b74 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73   to a block of s
10b75 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 64  zPage bytes of d
10b76 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 75  ata and the retu
10b77 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61  rn value is.** a
10b78 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
10b79 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 72  associated PgHdr
10b7a 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  1 structure..**.
10b7b 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 48  **   assert( PGH
10b7c 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45  DR1_TO_PAGE(PAGE
10b7d 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68  _TO_PGHDR1(pCach
10b7e 65 2c 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a  e, X))==X );.*/.
10b7f 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54  #define PGHDR1_T
10b80 4f 5f 50 41 47 45 28 70 29 20 20 20 20 28 76 6f  O_PAGE(p)    (vo
10b81 69 64 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20  id*)(((char*)p) 
10b82 2d 20 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50  - p->pCache->szP
10b83 61 67 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47  age).#define PAG
10b84 45 5f 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70  E_TO_PGHDR1(c, p
10b85 29 20 28 50 67 48 64 72 31 2a 29 28 28 28 63 68  ) (PgHdr1*)(((ch
10b86 61 72 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61  ar*)p) + c->szPa
10b87 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  ge)../*.** Macro
10b88 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c  s to enter and l
10b89 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20  eave the global 
10b8a 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64  LRU mutex..*/.#d
10b8b 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74  efine pcache1Ent
10b8c 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65  erMutex() sqlite
10b8d 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63  3_mutex_enter(pc
10b8e 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65  ache1.mutex).#de
10b8f 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76  fine pcache1Leav
10b90 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33  eMutex() sqlite3
10b91 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61  _mutex_leave(pca
10b92 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a  che1.mutex)../**
10b93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10b98 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c  ******* Page All
10b99 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43  ocation/SQLITE_C
10b9a 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c  ONFIG_PCACHE Rel
10b9b 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a  ated Functions *
10b9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
10b9d 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
10b9e 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75  ion is called du
10b9f 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
10ba0 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 63 20  ion if a static 
10ba1 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75  buffer is .** su
10ba2 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f  pplied to use fo
10ba3 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65  r the page-cache
10ba4 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20   by passing the 
10ba5 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
10ba6 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20  GECACHE.** verb 
10ba7 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  to sqlite3_confi
10ba8 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70  g(). Parameter p
10ba9 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  Buf points to an
10baa 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67   allocation larg
10bab 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63  e.** enough to c
10bac 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65  ontain 'n' buffe
10bad 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73  rs of 'sz' bytes
10bae 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45   each..*/.SQLITE
10baf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10bb0 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65  lite3PCacheBuffe
10bb1 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75  rSetup(void *pBu
10bb2 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e  f, int sz, int n
10bb3 29 7b 0a 20 20 69 66 28 20 70 63 61 63 68 65 31  ){.  if( pcache1
10bb4 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50  .isInit ){.    P
10bb5 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20  gFreeslot *p;.  
10bb6 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e    sz = ROUNDDOWN
10bb7 38 28 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68  8(sz);.    pcach
10bb8 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a  e1.szSlot = sz;.
10bb9 20 20 20 20 70 63 61 63 68 65 31 2e 70 53 74 61      pcache1.pSta
10bba 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70  rt = pBuf;.    p
10bbb 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30  cache1.pFree = 0
10bbc 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d  ;.    while( n--
10bbd 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50   ){.      p = (P
10bbe 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b  gFreeslot*)pBuf;
10bbf 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20  .      p->pNext 
10bc0 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b  = pcache1.pFree;
10bc1 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70  .      pcache1.p
10bc2 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20  Free = p;.      
10bc3 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28  pBuf = (void*)&(
10bc4 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d  (char*)pBuf)[sz]
10bc5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63  ;.    }.    pcac
10bc6 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b  he1.pEnd = pBuf;
10bc7 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  .  }.}../*.** Ma
10bc8 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73  lloc function us
10bc9 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 20 66  ed within this f
10bca 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ile to allocate 
10bcb 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62  space from the b
10bcc 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75  uffer.** configu
10bcd 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  red using sqlite
10bce 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
10bcf 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10bd0 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  ) option. If no 
10bd1 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 72 20  .** such buffer 
10bd2 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 65 20  exists or there 
10bd3 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74  is no space left
10bd4 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e   in it, this fun
10bd5 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20  ction falls .** 
10bd6 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d  back to sqlite3M
10bd7 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
10bd8 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31  ic void *pcache1
10bd9 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29  Alloc(int nByte)
10bda 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61  {.  void *p;.  a
10bdb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
10bdc 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65  utex_held(pcache
10bdd 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  1.mutex) );.  if
10bde 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31  ( nByte<=pcache1
10bdf 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68  .szSlot && pcach
10be0 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20  e1.pFree ){.    
10be1 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e  assert( pcache1.
10be2 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20  isInit );.    p 
10be3 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63  = (PgHdr1 *)pcac
10be4 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70  he1.pFree;.    p
10be5 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70  cache1.pFree = p
10be6 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e  cache1.pFree->pN
10be7 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
10be8 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
10be9 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
10bea 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a  E_SIZE, nByte);.
10beb 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
10bec 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
10bed 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45  US_PAGECACHE_USE
10bee 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  D, 1);.  }else{.
10bef 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
10bf0 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 75 73   a new buffer us
10bf1 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  ing sqlite3Mallo
10bf2 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  c. Before doing 
10bf3 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20  so, exit the.   
10bf4 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68   ** global pcach
10bf5 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f  e mutex and unlo
10bf6 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  ck the pager-cac
10bf7 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65  he object pCache
10bf8 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a  . This is .    *
10bf9 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65  * so that if the
10bfa 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f   attempt to allo
10bfb 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65  cate a new buffe
10bfc 72 20 63 61 75 73 65 73 20 74 68 65 20 74 68 65  r causes the the
10bfd 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75   .    ** configu
10bfe 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69  red soft-heap-li
10bff 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 63 68  mit to be breach
10c00 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70  ed, it will be p
10c01 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a  ossible to.    *
10c02 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79  * reclaim memory
10c03 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72   from this pager
10c04 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20  -cache..    */. 
10c05 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d     pcache1LeaveM
10c06 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20  utex();.    p = 
10c07 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42  sqlite3Malloc(nB
10c08 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65  yte);.    pcache
10c09 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10c0a 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
10c0b 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74    int sz = sqlit
10c0c 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b  e3MallocSize(p);
10c0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
10c0e 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
10c0f 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
10c10 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20  OVERFLOW, sz);. 
10c11 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
10c12 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  n p;.}../*.** Fr
10c13 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20  ee an allocated 
10c14 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20  buffer obtained 
10c15 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f  from pcache1Allo
10c16 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  c()..*/.static v
10c17 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 28  oid pcache1Free(
10c18 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65  void *p){.  asse
10c19 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
10c1a 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d  x_held(pcache1.m
10c1b 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
10c1c 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
10c1d 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70  if( p>=pcache1.p
10c1e 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68  Start && p<pcach
10c1f 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50  e1.pEnd ){.    P
10c20 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74  gFreeslot *pSlot
10c21 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
10c22 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
10c23 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
10c24 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53  SED, -1);.    pS
10c25 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f  lot = (PgFreeslo
10c26 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d  t*)p;.    pSlot-
10c27 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31  >pNext = pcache1
10c28 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63  .pFree;.    pcac
10c29 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f  he1.pFree = pSlo
10c2a 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
10c2b 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69  int iSize = sqli
10c2c 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
10c2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
10c2e 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
10c2f 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f  ATUS_PAGECACHE_O
10c30 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29  VERFLOW, -iSize)
10c31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
10c32 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ee(p);.  }.}../*
10c33 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
10c34 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  ew page object i
10c35 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61  nitially associa
10c36 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70  ted with cache p
10c37 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  Cache..*/.static
10c38 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31   PgHdr1 *pcache1
10c39 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65  AllocPage(PCache
10c3a 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e  1 *pCache){.  in
10c3b 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66  t nByte = sizeof
10c3c 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68  (PgHdr1) + pCach
10c3d 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69  e->szPage;.  voi
10c3e 64 20 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31  d *pPg = pcache1
10c3f 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  Alloc(nByte);.  
10c40 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28  PgHdr1 *p;.  if(
10c41 20 70 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20   pPg ){.    p = 
10c42 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70  PAGE_TO_PGHDR1(p
10c43 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20  Cache, pPg);.   
10c44 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
10c45 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  rgeable ){.     
10c46 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10c47 74 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20  tPage++;.    }. 
10c48 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20   }else{.    p = 
10c49 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
10c4a 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  p;.}../*.** Free
10c4b 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61   a page object a
10c4c 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63  llocated by pcac
10c4d 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a  he1AllocPage()..
10c4e 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65  **.** The pointe
10c4f 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20  r is allowed to 
10c50 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69  be NULL, which i
10c51 73 20 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20  s prudent.  But 
10c52 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20  it turns out.** 
10c53 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74  that the current
10c54 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
10c55 68 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72  happens to never
10c56 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
10c57 6e 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c  ne.** with a NUL
10c58 4c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65  L pointer, so we
10c59 20 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74   mark the NULL t
10c5a 65 73 74 20 77 69 74 68 20 41 4c 57 41 59 53 28  est with ALWAYS(
10c5b 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
10c5c 64 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67  d pcache1FreePag
10c5d 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20  e(PgHdr1 *p){.  
10c5e 69 66 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b  if( ALWAYS(p) ){
10c5f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63  .    if( p->pCac
10c60 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29  he->bPurgeable )
10c61 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  {.      pcache1.
10c62 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a  nCurrentPage--;.
10c63 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
10c64 31 46 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f  1Free(PGHDR1_TO_
10c65 50 41 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a  PAGE(p));.  }.}.
10c66 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75  ./*.** Malloc fu
10c67 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53  nction used by S
10c68 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20  QLite to obtain 
10c69 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62  space from the b
10c6a 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64  uffer configured
10c6b 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65  .** using sqlite
10c6c 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
10c6d 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10c6e 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  ) option. If no 
10c6f 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65  such buffer.** e
10c70 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63  xists, this func
10c71 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20  tion falls back 
10c72 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  to sqlite3Malloc
10c73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
10c74 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
10c75 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e  te3PageMalloc(in
10c76 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70  t sz){.  void *p
10c77 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10c78 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70  Mutex();.  p = p
10c79 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b  cache1Alloc(sz);
10c7a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
10c7b 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e  utex();.  return
10c7c 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   p;.}../*.** Fre
10c7d 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  e an allocated b
10c7e 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66  uffer obtained f
10c7f 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d  rom sqlite3PageM
10c80 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
10c81 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10c82 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
10c83 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63  void *p){.  pcac
10c84 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
10c85 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70  .  pcache1Free(p
10c86 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  );.  pcache1Leav
10c87 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a  eMutex();.}../**
10c88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10c8d 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20  ******* General 
10c8e 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46  Implementation F
10c8f 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a  unctions *******
10c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
10c92 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
10c93 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
10c94 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74  esize the hash t
10c95 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65  able used by the
10c96 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a   cache passed.**
10c97 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
10c98 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
10c99 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d  e global mutex m
10c9a 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e  ust be held when
10c9b 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
10c9c 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
10c9d 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52  tic int pcache1R
10c9e 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65  esizeHash(PCache
10c9f 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20  1 *p){.  PgHdr1 
10ca0 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67  **apNew;.  unsig
10ca1 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20  ned int nNew;.  
10ca2 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a  unsigned int i;.
10ca3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10ca4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10ca5 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10ca6 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61  .  nNew = p->nHa
10ca7 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77  sh*2;.  if( nNew
10ca8 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77  <256 ){.    nNew
10ca9 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70   = 256;.  }..  p
10caa 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10cab 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61  ();.  if( p->nHa
10cac 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67  sh ){ sqlite3Beg
10cad 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
10cae 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50  ; }.  apNew = (P
10caf 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33  gHdr1 **)sqlite3
10cb0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50  _malloc(sizeof(P
10cb1 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a  gHdr1 *)*nNew);.
10cb2 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29    if( p->nHash )
10cb3 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  { sqlite3EndBeni
10cb4 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20  gnMalloc(); }.  
10cb5 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10cb6 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77  x();.  if( apNew
10cb7 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61   ){.    memset(a
10cb8 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pNew, 0, sizeof(
10cb9 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b  PgHdr1 *)*nNew);
10cba 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
10cbb 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a  p->nHash; i++){.
10cbc 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 50        PgHdr1 *pP
10cbd 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72  age;.      PgHdr
10cbe 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70  1 *pNext = p->ap
10cbf 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77  Hash[i];.      w
10cc0 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70  hile( (pPage = p
10cc1 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20  Next)!=0 ){.    
10cc2 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
10cc3 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79   h = pPage->iKey
10cc4 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20   % nNew;.       
10cc5 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e   pNext = pPage->
10cc6 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70  pNext;.        p
10cc7 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70  Page->pNext = ap
10cc8 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20  New[h];.        
10cc9 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65  apNew[h] = pPage
10cca 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
10ccb 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
10ccc 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20  (p->apHash);.   
10ccd 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e   p->apHash = apN
10cce 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68  ew;.    p->nHash
10ccf 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20   = nNew;.  }..  
10cd0 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73  return (p->apHas
10cd1 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  h ? SQLITE_OK : 
10cd2 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d  SQLITE_NOMEM);.}
10cd3 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
10cd4 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e  ction is used in
10cd5 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f  ternally to remo
10cd6 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67  ve the page pPag
10cd7 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67  e from the .** g
10cd8 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20  lobal LRU list, 
10cd9 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74  if is part of it
10cda 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f  . If pPage is no
10cdb 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c  t part of the gl
10cdc 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74  obal.** LRU list
10cdd 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
10cde 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
10cdf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10ce0 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10ce1 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10ce2 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10ce3 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
10ce4 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65  d pcache1PinPage
10ce5 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b  (PgHdr1 *pPage){
10ce6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10ce7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10ce8 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10ce9 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28    if( pPage && (
10cea 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
10ceb 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65  || pPage==pcache
10cec 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20  1.pLruTail) ){. 
10ced 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c     if( pPage->pL
10cee 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20  ruPrev ){.      
10cef 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d  pPage->pLruPrev-
10cf0 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67  >pLruNext = pPag
10cf1 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20  e->pLruNext;.   
10cf2 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65   }.    if( pPage
10cf3 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20  ->pLruNext ){.  
10cf4 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e      pPage->pLruN
10cf5 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20  ext->pLruPrev = 
10cf6 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b  pPage->pLruPrev;
10cf7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
10cf8 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d  cache1.pLruHead=
10cf9 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20  =pPage ){.      
10cfa 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
10cfb 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65   = pPage->pLruNe
10cfc 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  xt;.    }.    if
10cfd 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  ( pcache1.pLruTa
10cfe 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20  il==pPage ){.   
10cff 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54     pcache1.pLruT
10d00 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72  ail = pPage->pLr
10d01 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20  uPrev;.    }.   
10d02 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10d03 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d   = 0;.    pPage-
10d04 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20  >pLruPrev = 0;. 
10d05 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65     pPage->pCache
10d06 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b  ->nRecyclable--;
10d07 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  .  }.}.../*.** R
10d08 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73  emove the page s
10d09 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72  upplied as an ar
10d0a 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  gument from the 
10d0b 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28  hash table .** (
10d0c 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73  PCache1.apHash s
10d0d 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69  tructure) that i
10d0e 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73  t is currently s
10d0f 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20  tored in..**.** 
10d10 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  The global mutex
10d11 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
10d12 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
10d13 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
10d14 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10d15 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  e1RemoveFromHash
10d16 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b  (PgHdr1 *pPage){
10d17 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10d18 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  h;.  PCache1 *pC
10d19 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43  ache = pPage->pC
10d1a 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a  ache;.  PgHdr1 *
10d1b 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67  *pp;..  h = pPag
10d1c 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65  e->iKey % pCache
10d1d 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70  ->nHash;.  for(p
10d1e 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73  p=&pCache->apHas
10d1f 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61  h[h]; (*pp)!=pPa
10d20 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70  ge; pp=&(*pp)->p
10d21 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28  Next);.  *pp = (
10d22 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20  *pp)->pNext;..  
10d23 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b  pCache->nPage--;
10d24 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
10d25 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  re are currently
10d26 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68   more than pcach
10d27 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73  e.nMaxPage pages
10d28 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a   allocated, try.
10d29 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61  ** to recycle pa
10d2a 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68  ges to reduce th
10d2b 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74  e number allocat
10d2c 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61  ed to pcache.nMa
10d2d 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  xPage..*/.static
10d2e 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66   void pcache1Enf
10d2f 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64  orceMaxPage(void
10d30 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
10d31 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10d32 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
10d33 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68  ;.  while( pcach
10d34 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e  e1.nCurrentPage>
10d35 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65  pcache1.nMaxPage
10d36 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75   && pcache1.pLru
10d37 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64  Tail ){.    PgHd
10d38 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e  r1 *p = pcache1.
10d39 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63  pLruTail;.    pc
10d3a 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b  ache1PinPage(p);
10d3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f  .    pcache1Remo
10d3c 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20  veFromHash(p);. 
10d3d 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61     pcache1FreePa
10d3e 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ge(p);.  }.}../*
10d3f 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20  .** Discard all 
10d40 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65  pages from cache
10d41 20 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70   pCache with a p
10d42 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20  age number (key 
10d43 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74  value) .** great
10d44 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
10d45 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20   to iLimit. Any 
10d46 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61  pinned pages tha
10d47 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20  t meet this .** 
10d48 63 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70  criteria are unp
10d49 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65  inned before the
10d4a 79 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e  y are discarded.
10d4b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10d4c 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10d4d 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10d4e 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10d4f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
10d50 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74  d pcache1Truncat
10d51 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68  eUnsafe(.  PCach
10d52 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75  e1 *pCache, .  u
10d53 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d  nsigned int iLim
10d54 69 74 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c  it .){.  TESTONL
10d55 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  Y( unsigned int 
10d56 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20  nPage = 0; )    
10d57 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73    /* Used to ass
10d58 65 72 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67  ert pCache->nPag
10d59 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a  e is correct */.
10d5a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
10d5b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
10d5c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
10d5d 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b  cache1.mutex) );
10d5e 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43  .  for(h=0; h<pC
10d5f 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b  ache->nHash; h++
10d60 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a  ){.    PgHdr1 **
10d61 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70  pp = &pCache->ap
10d62 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67  Hash[h]; .    Pg
10d63 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20  Hdr1 *pPage;.   
10d64 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d   while( (pPage =
10d65 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20   *pp)!=0 ){.    
10d66 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65    if( pPage->iKe
10d67 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  y>=iLimit ){.   
10d68 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61       pCache->nPa
10d69 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70  ge--;.        *p
10d6a 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74  p = pPage->pNext
10d6b 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65  ;.        pcache
10d6c 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b  1PinPage(pPage);
10d6d 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31  .        pcache1
10d6e 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  FreePage(pPage);
10d6f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
10d70 20 20 20 20 20 20 70 70 20 3d 20 26 70 50 61 67        pp = &pPag
10d71 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  e->pNext;.      
10d72 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67    TESTONLY( nPag
10d73 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20  e++; ).      }. 
10d74 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
10d75 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65  t( pCache->nPage
10d76 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a  ==nPage );.}../*
10d77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
10d7c 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
10d7d 5f 70 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20  _pcache Methods 
10d7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
10d81 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10d82 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10d83 69 74 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69  ite3_pcache.xIni
10d84 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  t method..*/.sta
10d85 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 49  tic int pcache1I
10d86 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  nit(void *NotUse
10d87 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
10d88 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
10d89 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68  .  assert( pcach
10d8a 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a  e1.isInit==0 );.
10d8b 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65    memset(&pcache
10d8c 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61  1, 0, sizeof(pca
10d8d 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71  che1));.  if( sq
10d8e 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
10d8f 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
10d90 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65      pcache1.mute
10d91 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  x = sqlite3_mute
10d92 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  x_alloc(SQLITE_M
10d93 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29  UTEX_STATIC_LRU)
10d94 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e  ;.  }.  pcache1.
10d95 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65  isInit = 1;.  re
10d96 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
10d97 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
10d98 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
10d99 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53  qlite3_pcache.xS
10d9a 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a  hutdown method..
10d9b 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
10d9c 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c   static mutex al
10d9d 6c 6f 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74  located in xInit
10d9e 20 64 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65   does .** not ne
10d9f 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a  ed to be freed..
10da0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10da1 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76  cache1Shutdown(v
10da2 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  oid *NotUsed){. 
10da3 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
10da4 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73  R(NotUsed);.  as
10da5 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73  sert( pcache1.is
10da6 49 6e 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d  Init!=0 );.  mem
10da7 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c  set(&pcache1, 0,
10da8 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29   sizeof(pcache1)
10da9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
10daa 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
10dab 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
10dac 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e  .xCreate method.
10dad 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  .**.** Allocate 
10dae 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a  a new cache..*/.
10daf 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70  static sqlite3_p
10db0 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 43 72  cache *pcache1Cr
10db1 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c  eate(int szPage,
10db2 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29   int bPurgeable)
10db3 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  {.  PCache1 *pCa
10db4 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d  che;..  pCache =
10db5 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69   (PCache1 *)sqli
10db6 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
10db7 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69  f(PCache1));.  i
10db8 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20  f( pCache ){.   
10db9 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20   memset(pCache, 
10dba 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65  0, sizeof(PCache
10dbb 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d  1));.    pCache-
10dbc 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65  >szPage = szPage
10dbd 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50  ;.    pCache->bP
10dbe 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72  urgeable = (bPur
10dbf 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b  geable ? 1 : 0);
10dc0 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 65 61  .    if( bPurgea
10dc1 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61  ble ){.      pCa
10dc2 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a  che->nMin = 10;.
10dc3 20 20 20 20 20 20 70 63 61 63 68 65 31 45 6e 74        pcache1Ent
10dc4 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20  erMutex();.     
10dc5 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67   pcache1.nMinPag
10dc6 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69  e += pCache->nMi
10dc7 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  n;.      pcache1
10dc8 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
10dc9 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
10dca 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65   (sqlite3_pcache
10dcb 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a   *)pCache;.}../*
10dcc 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10dcd 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10dce 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 65 73  3_pcache.xCaches
10dcf 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a  ize method. .**.
10dd0 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  ** Configure the
10dd1 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69   cache_size limi
10dd2 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a  t for a cache..*
10dd3 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
10dd4 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 28 73  ache1Cachesize(s
10dd5 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70  qlite3_pcache *p
10dd6 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50  , int nMax){.  P
10dd7 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
10dd8 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20   (PCache1 *)p;. 
10dd9 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
10dda 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70  rgeable ){.    p
10ddb 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10ddc 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e  ();.    pcache1.
10ddd 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61  nMaxPage += (nMa
10dde 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  x - pCache->nMax
10ddf 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e  );.    pCache->n
10de0 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20  Max = nMax;.    
10de1 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61  pcache1EnforceMa
10de2 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61  xPage();.    pca
10de3 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10de4 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
10de5 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
10de6 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
10de7 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d  che.xPagecount m
10de8 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69  ethod. .*/.stati
10de9 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61 67  c int pcache1Pag
10dea 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70  ecount(sqlite3_p
10deb 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74  cache *p){.  int
10dec 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74   n;.  pcache1Ent
10ded 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d  erMutex();.  n =
10dee 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d   ((PCache1 *)p)-
10def 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65  >nPage;.  pcache
10df0 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
10df1 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a   return n;.}../*
10df2 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10df3 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10df4 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 68 20  3_pcache.xFetch 
10df5 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46  method. .**.** F
10df6 65 74 63 68 20 61 20 70 61 67 65 20 62 79 20 6b  etch a page by k
10df7 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  ey value..**.** 
10df8 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  Whether or not a
10df9 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 62 65   new page may be
10dfa 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68   allocated by th
10dfb 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65  is function depe
10dfc 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61  nds on.** the va
10dfd 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61 74  lue of the creat
10dfe 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20  eFlag argument. 
10dff 20 30 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20   0 means do not 
10e00 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a  allocate a new.*
10e01 2a 20 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73  * page.  1 means
10e02 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
10e03 70 61 67 65 20 69 66 20 73 70 61 63 65 20 69 73  page if space is
10e04 20 65 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c   easily availabl
10e05 65 2e 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20  e.  2 .** means 
10e06 74 6f 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61  to try really ha
10e07 72 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  rd to allocate a
10e08 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   new page..**.**
10e09 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65   For a non-purge
10e0a 61 62 6c 65 20 63 61 63 68 65 20 28 61 20 63 61  able cache (a ca
10e0b 63 68 65 20 75 73 65 64 20 61 73 20 74 68 65 20  che used as the 
10e0c 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69  storage for an i
10e0d 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61  n-memory.** data
10e0e 62 61 73 65 29 20 74 68 65 72 65 20 69 73 20 72  base) there is r
10e0f 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65  eally no differe
10e10 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72 65 61  nce between crea
10e11 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20  teFlag 1 and 2. 
10e12 20 53 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69   So.** the calli
10e13 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61  ng function (pca
10e14 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65  che.c) will neve
10e15 72 20 68 61 76 65 20 61 20 63 72 65 61 74 65 46  r have a createF
10e16 6c 61 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61  lag of 1 on.** a
10e17 20 6e 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61   non-purgable ca
10e18 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  che..**.** There
10e19 20 61 72 65 20 74 68 72 65 65 20 64 69 66 66 65   are three diffe
10e1a 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20  rent approaches 
10e1b 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61  to obtaining spa
10e1c 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a  ce for a page,.*
10e1d 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  * depending on t
10e1e 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61  he value of para
10e1f 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c 61 67  meter createFlag
10e20 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 30   (which may be 0
10e21 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a  , 1 or 2)..**.**
10e22 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73     1. Regardless
10e23 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66   of the value of
10e24 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65   createFlag, the
10e25 20 63 61 63 68 65 20 69 73 20 73 65 61 72 63 68   cache is search
10e26 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20  ed for a .**    
10e27 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65    copy of the re
10e28 71 75 65 73 74 65 64 20 70 61 67 65 2e 20 49 66  quested page. If
10e29 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69   one is found, i
10e2a 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  t is returned..*
10e2b 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65  *.**   2. If cre
10e2c 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74  ateFlag==0 and t
10e2d 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
10e2e 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61  lready in the ca
10e2f 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20  che, NULL is.** 
10e30 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a       returned..*
10e31 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65  *.**   3. If cre
10e32 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e  ateFlag is 1, an
10e33 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  d the page is no
10e34 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  t already in the
10e35 20 63 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20   cache,.**      
10e36 61 6e 64 20 69 66 20 65 69 74 68 65 72 20 6f 66  and if either of
10e37 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
10e38 72 65 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20  re true, return 
10e39 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  NULL:.**.**     
10e3a 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72    (a) the number
10e3b 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64   of pages pinned
10e3c 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 73   by the cache is
10e3d 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a   greater than.**
10e3e 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68             PCach
10e3f 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20  e1.nMax, or.**  
10e40 20 20 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d       (b) the num
10e41 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e  ber of pages pin
10e42 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65  ned by the cache
10e43 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
10e44 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68  .**           th
10e45 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f  e sum of nMax fo
10e46 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20  r all purgeable 
10e47 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65  caches, less the
10e48 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20   sum of .**     
10e49 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61        nMin for a
10e4a 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 61 62  ll other purgeab
10e4b 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a  le caches. .**.*
10e4c 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f  *   4. If none o
10e4d 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65  f the first thre
10e4e 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70  e conditions app
10e4f 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 68 65  ly and the cache
10e50 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20   is marked.**   
10e51 20 20 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c     as purgeable,
10e52 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74   and if one of t
10e53 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
10e54 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  true:.**.**     
10e55 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72    (a) The number
10e56 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61   of pages alloca
10e57 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63 68  ted for the cach
10e58 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a  e is already .**
10e59 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68             PCach
10e5a 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a  e1.nMax, or.**.*
10e5b 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 65 20  *       (b) The 
10e5c 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
10e5d 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c  allocated for al
10e5e 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68  l purgeable cach
10e5f 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  es is.**        
10e60 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c     already equal
10e61 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74   to or greater t
10e62 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e  han the sum of n
10e63 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20  Max for all.**  
10e64 20 20 20 20 20 20 20 20 20 70 75 72 67 65 61 62           purgeab
10e65 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a  le caches,.**.**
10e66 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d        then attem
10e67 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20  pt to recycle a 
10e68 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52  page from the LR
10e69 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73  U list. If it is
10e6a 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20   the right.**   
10e6b 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20     size, return 
10e6c 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 75 66  the recycled buf
10e6d 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  fer. Otherwise, 
10e6e 66 72 65 65 20 74 68 65 20 62 75 66 66 65 72 20  free the buffer 
10e6f 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63  and.**      proc
10e70 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a  eed to step 5. .
10e71 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72  **.**   5. Other
10e72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61  wise, allocate a
10e73 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20  nd return a new 
10e74 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a  page buffer..*/.
10e75 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61  static void *pca
10e76 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 74 65  che1Fetch(sqlite
10e77 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73  3_pcache *p, uns
10e78 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20  igned int iKey, 
10e79 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b  int createFlag){
10e7a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10e7b 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68  nPinned;.  PCach
10e7c 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43  e1 *pCache = (PC
10e7d 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48  ache1 *)p;.  PgH
10e7e 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  dr1 *pPage = 0;.
10e7f 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68  .  assert( pCach
10e80 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c  e->bPurgeable ||
10e81 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29   createFlag!=1 )
10e82 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10e83 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63  Mutex();.  if( c
10e84 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73  reateFlag==1 ) s
10e85 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
10e86 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a  nMalloc();..  /*
10e87 20 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68   Search the hash
10e88 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78   table for an ex
10e89 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f  isting entry. */
10e8a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e  .  if( pCache->n
10e8b 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e  Hash>0 ){.    un
10e8c 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69  signed int h = i
10e8d 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48  Key % pCache->nH
10e8e 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61  ash;.    for(pPa
10e8f 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73  ge=pCache->apHas
10e90 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61  h[h]; pPage&&pPa
10e91 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20  ge->iKey!=iKey; 
10e92 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65  pPage=pPage->pNe
10e93 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  xt);.  }..  if( 
10e94 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46  pPage || createF
10e95 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63  lag==0 ){.    pc
10e96 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61  ache1PinPage(pPa
10e97 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65  ge);.    goto fe
10e98 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  tch_out;.  }..  
10e99 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 65 61  /* Step 3 of hea
10e9a 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a  der comment. */.
10e9b 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63    nPinned = pCac
10e9c 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63  he->nPage - pCac
10e9d 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b  he->nRecyclable;
10e9e 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61  .  if( createFla
10e9f 67 3d 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20  g==1 && (.      
10ea0 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63    nPinned>=(pcac
10ea1 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61  he1.nMaxPage+pCa
10ea2 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65  che->nMin-pcache
10ea3 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20  1.nMinPage).    
10ea4 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43   || nPinned>=(pC
10ea5 61 63 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f  ache->nMax * 9 /
10ea6 20 31 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67   10).  )){.    g
10ea7 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20  oto fetch_out;. 
10ea8 20 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65   }..  if( pCache
10ea9 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d  ->nPage>=pCache-
10eaa 3e 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65  >nHash && pcache
10eab 31 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63  1ResizeHash(pCac
10eac 68 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  he) ){.    goto 
10ead 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  fetch_out;.  }..
10eae 20 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79    /* Step 4. Try
10eaf 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61   to recycle a pa
10eb0 67 65 20 62 75 66 66 65 72 20 69 66 20 61 70 70  ge buffer if app
10eb1 72 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69  ropriate. */.  i
10eb2 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  f( pCache->bPurg
10eb3 65 61 62 6c 65 20 26 26 20 70 63 61 63 68 65 31  eable && pcache1
10eb4 2e 70 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20  .pLruTail && (. 
10eb5 20 20 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61      (pCache->nPa
10eb6 67 65 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d  ge+1>=pCache->nM
10eb7 61 78 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e  ax) || pcache1.n
10eb8 43 75 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61  CurrentPage>=pca
10eb9 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20  che1.nMaxPage.  
10eba 29 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20  )){.    pPage = 
10ebb 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
10ebc 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d  ;.    pcache1Rem
10ebd 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67  oveFromHash(pPag
10ebe 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50  e);.    pcache1P
10ebf 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  inPage(pPage);. 
10ec0 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43     if( pPage->pC
10ec1 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43  ache->szPage!=pC
10ec2 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a  ache->szPage ){.
10ec3 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65        pcache1Fre
10ec4 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
10ec5 20 20 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20      pPage = 0;. 
10ec6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
10ec7 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74  pcache1.nCurrent
10ec8 50 61 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e  Page -= (pPage->
10ec9 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
10eca 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75  le - pCache->bPu
10ecb 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a  rgeable);.    }.
10ecc 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35    }..  /* Step 5
10ecd 2e 20 49 66 20 61 20 75 73 61 62 6c 65 20 70 61  . If a usable pa
10ece 67 65 20 62 75 66 66 65 72 20 68 61 73 20 73 74  ge buffer has st
10ecf 69 6c 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75  ill not been fou
10ed0 6e 64 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70  nd, .  ** attemp
10ed1 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  t to allocate a 
10ed2 6e 65 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20  new one. .  */. 
10ed3 20 69 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20   if( !pPage ){. 
10ed4 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68     pPage = pcach
10ed5 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63  e1AllocPage(pCac
10ed6 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  he);.  }..  if( 
10ed7 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73  pPage ){.    uns
10ed8 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b  igned int h = iK
10ed9 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61  ey % pCache->nHa
10eda 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  sh;.    pCache->
10edb 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61  nPage++;.    pPa
10edc 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b  ge->iKey = iKey;
10edd 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78  .    pPage->pNex
10ede 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61  t = pCache->apHa
10edf 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65  sh[h];.    pPage
10ee0 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68  ->pCache = pCach
10ee1 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c  e;.    pPage->pL
10ee2 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  ruPrev = 0;.    
10ee3 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
10ee4 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20  = 0;.    *(void 
10ee5 2a 2a 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41  **)(PGHDR1_TO_PA
10ee6 47 45 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a  GE(pPage)) = 0;.
10ee7 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61      pCache->apHa
10ee8 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20  sh[h] = pPage;. 
10ee9 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20   }..fetch_out:. 
10eea 20 69 66 28 20 70 50 61 67 65 20 26 26 20 69 4b   if( pPage && iK
10eeb 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b  ey>pCache->iMaxK
10eec 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65  ey ){.    pCache
10eed 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79  ->iMaxKey = iKey
10eee 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61  ;.  }.  if( crea
10eef 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69  teFlag==1 ) sqli
10ef0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
10ef1 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c  oc();.  pcache1L
10ef2 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
10ef3 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50  eturn (pPage ? P
10ef4 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50  GHDR1_TO_PAGE(pP
10ef5 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f  age) : 0);.}.../
10ef6 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10ef7 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10ef8 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e  e3_pcache.xUnpin
10ef9 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d   method..**.** M
10efa 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e  ark a page as un
10efb 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65  pinned (eligible
10efc 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75   for asynchronou
10efd 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f  s recycling)..*/
10efe 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10eff 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65  che1Unpin(sqlite
10f00 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69  3_pcache *p, voi
10f01 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73  d *pPg, int reus
10f02 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43  eUnlikely){.  PC
10f03 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20  ache1 *pCache = 
10f04 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20  (PCache1 *)p;.  
10f05 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20  PgHdr1 *pPage = 
10f06 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70  PAGE_TO_PGHDR1(p
10f07 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20  Cache, pPg);. . 
10f08 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
10f09 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29  pCache==pCache )
10f0a 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10f0b 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49  Mutex();..  /* I
10f0c 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f  t is an error to
10f0d 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
10f0e 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20  ion if the page 
10f0f 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a  is already .  **
10f10 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f   part of the glo
10f11 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20  bal LRU list..  
10f12 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
10f13 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20  ge->pLruPrev==0 
10f14 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65  && pPage->pLruNe
10f15 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  xt==0 );.  asser
10f16 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48  t( pcache1.pLruH
10f17 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 63  ead!=pPage && pc
10f18 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d  ache1.pLruTail!=
10f19 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20  pPage );..  if( 
10f1a 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c  reuseUnlikely ||
10f1b 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10f1c 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d  tPage>pcache1.nM
10f1d 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63  axPage ){.    pc
10f1e 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
10f1f 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ash(pPage);.    
10f20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10f21 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  pPage);.  }else{
10f22 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20  .    /* Add the 
10f23 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62  page to the glob
10f24 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72  al LRU list. Nor
10f25 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20  mally, the page 
10f26 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20  is added to.    
10f27 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  ** the head of t
10f28 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61  he list (last pa
10f29 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65  ge to be recycle
10f2a 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20  d). However, if 
10f2b 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73  the .    ** reus
10f2c 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70  eUnlikely flag p
10f2d 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75  assed to this fu
10f2e 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20  nction is true, 
10f2f 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65  the page is adde
10f30 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  d.    ** to the 
10f31 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74  tail of the list
10f32 20 28 66 69 72 73 74 20 70 61 67 65 20 74 6f 20   (first page to 
10f33 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20  be recycled)..  
10f34 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61    */.    if( pca
10f35 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b  che1.pLruHead ){
10f36 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70  .      pcache1.p
10f37 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65  LruHead->pLruPre
10f38 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20  v = pPage;.     
10f39 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10f3a 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48   = pcache1.pLruH
10f3b 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68  ead;.      pcach
10f3c 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50  e1.pLruHead = pP
10f3d 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  age;.    }else{.
10f3e 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10f3f 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a  ruTail = pPage;.
10f40 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10f41 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a  ruHead = pPage;.
10f42 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65      }.    pCache
10f43 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b  ->nRecyclable++;
10f44 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c  .  }..  pcache1L
10f45 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a  eaveMutex();.}..
10f46 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
10f47 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
10f48 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65  te3_pcache.xReke
10f49 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74  y method. .*/.st
10f4a 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10f4b 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  1Rekey(.  sqlite
10f4c 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76  3_pcache *p,.  v
10f4d 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69  oid *pPg,.  unsi
10f4e 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20  gned int iOld,. 
10f4f 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e   unsigned int iN
10f50 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20  ew.){.  PCache1 
10f51 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
10f52 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31  e1 *)p;.  PgHdr1
10f53 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54   *pPage = PAGE_T
10f54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c  O_PGHDR1(pCache,
10f55 20 70 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20   pPg);.  PgHdr1 
10f56 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64  **pp;.  unsigned
10f57 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72   int h; .  asser
10f58 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d  t( pPage->iKey==
10f59 69 4f 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74  iOld );.  assert
10f5a 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d  ( pPage->pCache=
10f5b 3d 70 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63  =pCache );..  pc
10f5c 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10f5d 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70  );..  h = iOld%p
10f5e 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20  Cache->nHash;.  
10f5f 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70  pp = &pCache->ap
10f60 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65  Hash[h];.  while
10f61 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29  ( (*pp)!=pPage )
10f62 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70  {.    pp = &(*pp
10f63 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  )->pNext;.  }.  
10f64 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65  *pp = pPage->pNe
10f65 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25  xt;..  h = iNew%
10f66 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20  pCache->nHash;. 
10f67 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69   pPage->iKey = i
10f68 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e  New;.  pPage->pN
10f69 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70  ext = pCache->ap
10f6a 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68  Hash[h];.  pCach
10f6b 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70  e->apHash[h] = p
10f6c 50 61 67 65 3b 0a 20 20 69 66 28 20 69 4e 65 77  Page;.  if( iNew
10f6d 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79  >pCache->iMaxKey
10f6e 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e   ){.    pCache->
10f6f 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65 77 3b 0a  iMaxKey = iNew;.
10f70 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65    }..  pcache1Le
10f71 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f  aveMutex();.}../
10f72 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10f73 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10f74 65 33 5f 70 63 61 63 68 65 2e 78 54 72 75 6e 63  e3_pcache.xTrunc
10f75 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a  ate method. .**.
10f76 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75  ** Discard all u
10f77 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e  npinned pages in
10f78 20 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20   the cache with 
10f79 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71  a page number eq
10f7a 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 67 72 65  ual to.** or gre
10f7b 61 74 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65  ater than parame
10f7c 74 65 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20  ter iLimit. Any 
10f7d 70 69 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74  pinned pages wit
10f7e 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a  h a page number.
10f7f 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67  ** equal to or g
10f80 72 65 61 74 65 72 20 74 68 61 6e 20 69 4c 69 6d  reater than iLim
10f81 69 74 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c  it are implicitl
10f82 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73  y unpinned..*/.s
10f83 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10f84 65 31 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  e1Truncate(sqlit
10f85 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e  e3_pcache *p, un
10f86 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69  signed int iLimi
10f87 74 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70  t){.  PCache1 *p
10f88 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31  Cache = (PCache1
10f89 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45   *)p;.  pcache1E
10f8a 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69  nterMutex();.  i
10f8b 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43 61 63 68  f( iLimit<=pCach
10f8c 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20  e->iMaxKey ){.  
10f8d 20 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74    pcache1Truncat
10f8e 65 55 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20  eUnsafe(pCache, 
10f8f 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 70 43 61  iLimit);.    pCa
10f90 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69  che->iMaxKey = i
10f91 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a 20 20 70  Limit-1;.  }.  p
10f92 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10f93 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ();.}../*.** Imp
10f94 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10f95 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
10f96 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f  e.xDestroy metho
10f97 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f  d. .**.** Destro
10f98 79 20 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61  y a cache alloca
10f99 74 65 64 20 75 73 69 6e 67 20 70 63 61 63 68 65  ted using pcache
10f9a 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a 73 74  1Create()..*/.st
10f9b 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10f9c 31 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  1Destroy(sqlite3
10f9d 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 50  _pcache *p){.  P
10f9e 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
10f9f 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20   (PCache1 *)p;. 
10fa0 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
10fa1 65 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 54  ex();.  pcache1T
10fa2 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43  runcateUnsafe(pC
10fa3 61 63 68 65 2c 20 30 29 3b 0a 20 20 70 63 61 63  ache, 0);.  pcac
10fa4 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20  he1.nMaxPage -= 
10fa5 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20  pCache->nMax;.  
10fa6 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65  pcache1.nMinPage
10fa7 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e   -= pCache->nMin
10fa8 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72  ;.  pcache1Enfor
10fa9 63 65 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 70  ceMaxPage();.  p
10faa 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10fab 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  ();.  sqlite3_fr
10fac 65 65 28 70 43 61 63 68 65 2d 3e 61 70 48 61 73  ee(pCache->apHas
10fad 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  h);.  sqlite3_fr
10fae 65 65 28 70 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f  ee(pCache);.}../
10faf 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
10fb0 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72  on is called dur
10fb1 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ing initializati
10fb2 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69 6e 69 74  on (sqlite3_init
10fb3 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a 2a 2a 20  ialize()) to.** 
10fb4 69 6e 73 74 61 6c 6c 20 74 68 65 20 64 65 66 61  install the defa
10fb5 75 6c 74 20 70 6c 75 67 67 61 62 6c 65 20 63 61  ult pluggable ca
10fb6 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61 73 73 75  che module, assu
10fb7 6d 69 6e 67 20 74 68 65 20 75 73 65 72 20 68 61  ming the user ha
10fb8 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79  s not.** already
10fb9 20 70 72 6f 76 69 64 65 64 20 61 6e 20 61 6c 74   provided an alt
10fba 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c  ernative..*/.SQL
10fbb 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
10fbc 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65   sqlite3PCacheSe
10fbd 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a  tDefault(void){.
10fbe 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
10fbf 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20  _pcache_methods 
10fc0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d  defaultMethods =
10fc1 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20   {.    0,       
10fc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10fc3 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20 20 20 70  /* pArg */.    p
10fc4 63 61 63 68 65 31 49 6e 69 74 2c 20 20 20 20 20  cache1Init,     
10fc5 20 20 20 20 20 20 20 20 2f 2a 20 78 49 6e 69 74          /* xInit
10fc6 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 53   */.    pcache1S
10fc7 68 75 74 64 6f 77 6e 2c 20 20 20 20 20 20 20 20  hutdown,        
10fc8 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f   /* xShutdown */
10fc9 0a 20 20 20 20 70 63 61 63 68 65 31 43 72 65 61  .    pcache1Crea
10fca 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  te,           /*
10fcb 20 78 43 72 65 61 74 65 20 2a 2f 0a 20 20 20 20   xCreate */.    
10fcc 70 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65  pcache1Cachesize
10fcd 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 43 61 63  ,        /* xCac
10fce 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63  hesize */.    pc
10fcf 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74 2c 20  ache1Pagecount, 
10fd0 20 20 20 20 20 20 20 2f 2a 20 78 50 61 67 65 63         /* xPagec
10fd1 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70 63 61 63  ount */.    pcac
10fd2 68 65 31 46 65 74 63 68 2c 20 20 20 20 20 20 20  he1Fetch,       
10fd3 20 20 20 20 20 2f 2a 20 78 46 65 74 63 68 20 2a       /* xFetch *
10fd4 2f 0a 20 20 20 20 70 63 61 63 68 65 31 55 6e 70  /.    pcache1Unp
10fd5 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  in,            /
10fd6 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20  * xUnpin */.    
10fd7 70 63 61 63 68 65 31 52 65 6b 65 79 2c 20 20 20  pcache1Rekey,   
10fd8 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6b           /* xRek
10fd9 65 79 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  ey */.    pcache
10fda 31 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20  1Truncate,      
10fdb 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20     /* xTruncate 
10fdc 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 44 65  */.    pcache1De
10fdd 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20  stroy           
10fde 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20  /* xDestroy */. 
10fdf 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f   };.  sqlite3_co
10fe0 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46  nfig(SQLITE_CONF
10fe1 49 47 5f 50 43 41 43 48 45 2c 20 26 64 65 66 61  IG_PCACHE, &defa
10fe2 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a  ultMethods);.}..
10fe3 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
10fe4 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
10fe5 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69  GEMENT./*.** Thi
10fe6 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
10fe7 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 73 75 70  lled to free sup
10fe8 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63  erfluous dynamic
10fe9 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d  ally allocated m
10fea 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62 79  emory.** held by
10feb 20 74 68 65 20 70 61 67 65 72 20 73 79 73 74 65   the pager syste
10fec 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73 65  m. Memory in use
10fed 20 62 79 20 61 6e 79 20 53 51 4c 69 74 65 20 70   by any SQLite p
10fee 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 2a  ager allocated.*
10fef 2a 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74  * by the current
10ff0 20 74 68 72 65 61 64 20 6d 61 79 20 62 65 20 73   thread may be s
10ff1 71 6c 69 74 65 33 5f 66 72 65 65 28 29 65 64 2e  qlite3_free()ed.
10ff2 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74  .**.** nReq is t
10ff3 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
10ff4 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71  es of memory req
10ff5 75 69 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73  uired. Once this
10ff6 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65   much has.** bee
10ff7 6e 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20  n released, the 
10ff8 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
10ff9 2e 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  . The return val
10ffa 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  ue is the total 
10ffb 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 20 62 79  number .** of by
10ffc 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65  tes of memory re
10ffd 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  leased..*/.SQLIT
10ffe 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
10fff 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61  lite3PcacheRelea
11000 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65  seMemory(int nRe
11001 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20  q){.  int nFree 
11002 3d 20 30 3b 0a 20 20 69 66 28 20 70 63 61 63 68  = 0;.  if( pcach
11003 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b 0a  e1.pStart==0 ){.
11004 20 20 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20      PgHdr1 *p;. 
11005 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d     pcache1EnterM
11006 75 74 65 78 28 29 3b 0a 20 20 20 20 77 68 69 6c  utex();.    whil
11007 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46  e( (nReq<0 || nF
11008 72 65 65 3c 6e 52 65 71 29 20 26 26 20 28 70 3d  ree<nReq) && (p=
11009 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
1100a 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65  ) ){.      nFree
1100b 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   += sqlite3Mallo
1100c 63 53 69 7a 65 28 50 47 48 44 52 31 5f 54 4f 5f  cSize(PGHDR1_TO_
1100d 50 41 47 45 28 70 29 29 3b 0a 20 20 20 20 20 20  PAGE(p));.      
1100e 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70  pcache1PinPage(p
1100f 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  );.      pcache1
11010 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70  RemoveFromHash(p
11011 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  );.      pcache1
11012 46 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 20  FreePage(p);.   
11013 20 7d 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65   }.    pcache1Le
11014 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
11015 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a    return nFree;.
11016 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
11017 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  TE_ENABLE_MEMORY
11018 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a  _MANAGEMENT */..
11019 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1101a 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ST./*.** This fu
1101b 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  nction is used b
1101c 79 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65  y test procedure
1101d 73 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65  s to inspect the
1101e 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0a   internal state.
1101f 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c  ** of the global
11020 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54   cache..*/.SQLIT
11021 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
11022 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74  qlite3PcacheStat
11023 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72  s(.  int *pnCurr
11024 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54  ent,      /* OUT
11025 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  : Total number o
11026 66 20 70 61 67 65 73 20 63 61 63 68 65 64 20 2a  f pages cached *
11027 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c 20  /.  int *pnMax, 
11028 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
11029 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20   Global maximum 
1102a 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20  cache size */.  
1102b 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 20 20 20  int *pnMin,     
1102c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 75 6d       /* OUT: Sum
1102d 20 6f 66 20 50 43 61 63 68 65 31 2e 6e 4d 69 6e   of PCache1.nMin
1102e 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63   for purgeable c
1102f 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  aches */.  int *
11030 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 20 20 20  pnRecyclable    
11031 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75  /* OUT: Total nu
11032 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 76  mber of pages av
11033 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 63 79  ailable for recy
11034 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 67  cling */.){.  Pg
11035 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e 74 20 6e  Hdr1 *p;.  int n
11036 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 30 3b 0a  Recyclable = 0;.
11037 20 20 66 6f 72 28 70 3d 70 63 61 63 68 65 31 2e    for(p=pcache1.
11038 70 4c 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70  pLruHead; p; p=p
11039 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20  ->pLruNext){.   
1103a 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a   nRecyclable++;.
1103b 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72 65 6e 74    }.  *pnCurrent
1103c 20 3d 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72   = pcache1.nCurr
1103d 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61  entPage;.  *pnMa
1103e 78 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 61 78  x = pcache1.nMax
1103f 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d  Page;.  *pnMin =
11040 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67   pcache1.nMinPag
11041 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62  e;.  *pnRecyclab
11042 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65  le = nRecyclable
11043 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
11044 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
11045 6f 66 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a  of pcache1.c ***
11046 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11047 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11048 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
11049 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1104a 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20  n file rowset.c 
1104b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1104c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1104d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1104e 2a 20 32 30 30 38 20 44 65 63 65 6d 62 65 72 20  * 2008 December 
1104f 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
11050 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
11051 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
11052 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
11053 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
11054 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
11055 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
11056 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
11057 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
11058 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
11059 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1105a 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1105b 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1105c 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1105d 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1105e 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1105f 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
11060 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
11061 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11062 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11063 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11064 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11065 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  .**.** This modu
11066 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e  le implements an
11067 20 6f 62 6a 65 63 74 20 77 65 20 63 61 6c 6c 20   object we call 
11068 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a 2a 0a 2a  a "RowSet"..**.*
11069 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a  * The RowSet obj
1106a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c 65 63 74  ect is a collect
1106b 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 2e 20 20  ion of rowids.  
1106c 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 20 69 6e  Rowids.** are in
1106d 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20  serted into the 
1106e 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 61 72 62  RowSet in an arb
1106f 69 74 72 61 72 79 20 6f 72 64 65 72 2e 20 20 49  itrary order.  I
11070 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e 20 62 65  nserts.** can be
11071 20 69 6e 74 65 72 6d 69 78 65 64 20 77 69 74 68   intermixed with
11072 20 74 65 73 74 73 20 74 6f 20 73 65 65 20 69 66   tests to see if
11073 20 61 20 67 69 76 65 6e 20 72 6f 77 69 64 20 68   a given rowid h
11074 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 76 69  as been.** previ
11075 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64 20 69  ously inserted i
11076 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a  nto the RowSet..
11077 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20  **.** After all 
11078 69 6e 73 65 72 74 73 20 61 72 65 20 66 69 6e 69  inserts are fini
11079 73 68 65 64 2c 20 69 74 20 69 73 20 70 6f 73 73  shed, it is poss
1107a 69 62 6c 65 20 74 6f 20 65 78 74 72 61 63 74 20  ible to extract 
1107b 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20  the.** elements 
1107c 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e  of the RowSet in
1107d 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20   sorted order.  
1107e 4f 6e 63 65 20 74 68 69 73 20 65 78 74 72 61 63  Once this extrac
1107f 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 20  tion.** process 
11080 68 61 73 20 73 74 61 72 74 65 64 2c 20 6e 6f 20  has started, no 
11081 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 6d 61 79  new elements may
11082 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a   be inserted..**
11083 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 65 20 70  .** Hence, the p
11084 72 69 6d 69 74 69 76 65 20 6f 70 65 72 61 74 69  rimitive operati
11085 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 53 65 74  ons for a RowSet
11086 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 43   are:.**.**    C
11087 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 4e 53 45  REATE.**    INSE
11088 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 0a 2a 2a  RT.**    TEST.**
11089 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20      SMALLEST.** 
1108a 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a 0a 2a 2a     DESTROY.**.**
1108b 20 54 68 65 20 43 52 45 41 54 45 20 61 6e 64 20   The CREATE and 
1108c 44 45 53 54 52 4f 59 20 70 72 69 6d 69 74 69 76  DESTROY primitiv
1108d 65 73 20 61 72 65 20 74 68 65 20 63 6f 6e 73 74  es are the const
1108e 72 75 63 74 6f 72 20 61 6e 64 20 64 65 73 74 72  ructor and destr
1108f 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 69 6f 75  uctor,.** obviou
11090 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 45 52 54  sly.  The INSERT
11091 20 70 72 69 6d 69 74 69 76 65 20 61 64 64 73 20   primitive adds 
11092 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f  a new element to
11093 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20   the RowSet..** 
11094 54 45 53 54 20 63 68 65 63 6b 73 20 74 6f 20 73  TEST checks to s
11095 65 65 20 69 66 20 61 6e 20 65 6c 65 6d 65 6e 74  ee if an element
11096 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
11097 68 65 20 52 6f 77 53 65 74 2e 20 20 53 4d 41 4c  he RowSet.  SMAL
11098 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 63 74 73  LEST.** extracts
11099 20 74 68 65 20 6c 65 61 73 74 20 76 61 6c 75 65   the least value
1109a 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74   from the RowSet
1109b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 4e 53 45  ..**.** The INSE
1109c 52 54 20 70 72 69 6d 69 74 69 76 65 20 6d 69 67  RT primitive mig
1109d 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 64 64 69  ht allocate addi
1109e 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 2e 20 20  tional memory.  
1109f 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 61 6c 6c  Memory is.** all
110a0 6f 63 61 74 65 64 20 69 6e 20 63 68 75 6e 6b 73  ocated in chunks
110a1 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 52 54 73   so most INSERTs
110a2 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 74 69 6f   do no allocatio
110a3 6e 2e 20 20 54 68 65 72 65 20 69 73 20 61 6e 20  n.  There is an 
110a4 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 6e 64 20  .** upper bound 
110a5 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  on the size of a
110a6 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e  llocated memory.
110a7 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 73 20 66    No memory is f
110a8 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 44 45  reed.** until DE
110a9 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  STROY..**.** The
110aa 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 20   TEST primitive 
110ab 69 6e 63 6c 75 64 65 73 20 61 20 22 62 61 74 63  includes a "batc
110ac 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20  h" number.  The 
110ad 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 0a 2a  TEST primitive.*
110ae 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 65 65 20  * will only see 
110af 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 65  elements that we
110b0 72 65 20 69 6e 73 65 72 74 65 64 20 62 65 66 6f  re inserted befo
110b1 72 65 20 74 68 65 20 6c 61 73 74 20 63 68 61 6e  re the last chan
110b2 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 62 61 74  ge.** in the bat
110b3 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 6e 20 6f  ch number.  In o
110b4 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61  ther words, if a
110b5 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 72 73 20  n INSERT occurs 
110b6 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 6f 20 54  between.** two T
110b7 45 53 54 73 20 77 68 65 72 65 20 74 68 65 20 54  ESTs where the T
110b8 45 53 54 73 20 68 61 76 65 20 74 68 65 20 73 61  ESTs have the sa
110b9 6d 65 20 62 61 74 63 68 20 6e 75 62 6d 65 72 2c  me batch nubmer,
110ba 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c   then the.** val
110bb 75 65 20 61 64 64 65 64 20 62 79 20 74 68 65 20  ue added by the 
110bc 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e 6f 74 20  INSERT will not 
110bd 62 65 20 76 69 73 69 62 6c 65 20 74 6f 20 74 68  be visible to th
110be 65 20 73 65 63 6f 6e 64 20 54 45 53 54 2e 0a 2a  e second TEST..*
110bf 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20 62 61  * The initial ba
110c0 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 7a 65  tch number is ze
110c1 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 20 76 65  ro, so if the ve
110c2 72 79 20 66 69 72 73 74 20 54 45 53 54 20 63 6f  ry first TEST co
110c3 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e 6f 6e 2d  ntains.** a non-
110c4 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 6d 62 65  zero batch numbe
110c5 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 20 61  r, it will see a
110c6 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 52 54 73  ll prior INSERTs
110c7 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e 53 45 52  ..**.** No INSER
110c8 54 73 20 6d 61 79 20 6f 63 63 75 72 73 20 61 66  Ts may occurs af
110c9 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 54 2e 20  ter a SMALLEST. 
110ca 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 77 69   An assertion wi
110cb 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a 20 74 68  ll fail if.** th
110cc 61 74 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e  at is attempted.
110cd 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20  .**.** The cost 
110ce 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 69 73 20  of an INSERT is 
110cf 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 61 6e 74  roughly constant
110d0 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 6e 65 77  .  (Sometime new
110d1 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 73 20 74   memory.** has t
110d2 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f  o be allocated o
110d3 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 20 20 54  n an INSERT.)  T
110d4 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53  he cost of a TES
110d5 54 20 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20  T with a new.** 
110d6 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20  batch number is 
110d7 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 65 20 4e  O(NlogN) where N
110d8 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
110d9 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  f elements in th
110da 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 68 65  e RowSet..** The
110db 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 54 20   cost of a TEST 
110dc 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 62  using the same b
110dd 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 4f  atch number is O
110de 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 63 6f 73  (logN).  The cos
110df 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73  t.** of the firs
110e0 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 20 4f 28  t SMALLEST is O(
110e1 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f 6e 64 20  NlogN).  Second 
110e2 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 53  and subsequent S
110e3 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 69 6d 69  MALLEST.** primi
110e4 74 69 76 65 73 20 61 72 65 20 63 6f 6e 73 74 61  tives are consta
110e5 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 20 63 6f  nt time.  The co
110e6 73 74 20 6f 66 20 44 45 53 54 52 4f 59 20 69 73  st of DESTROY is
110e7 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   O(N)..**.** The
110e8 72 65 20 69 73 20 61 6e 20 61 64 64 65 64 20 63  re is an added c
110e9 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 68 65 6e  ost of O(N) when
110ea 20 73 77 69 74 63 68 69 6e 67 20 62 65 74 77 65   switching betwe
110eb 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a 2a 20 53  en TEST and.** S
110ec 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 74 69 76  MALLEST primitiv
110ed 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  es..*/.../*.** T
110ee 61 72 67 65 74 20 73 69 7a 65 20 66 6f 72 20 61  arget size for a
110ef 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73  llocation chunks
110f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57  ..*/.#define ROW
110f1 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53  SET_ALLOCATION_S
110f2 49 5a 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20  IZE 1024../*.** 
110f3 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  The number of ro
110f4 77 73 65 74 20 65 6e 74 72 69 65 73 20 70 65 72  wset entries per
110f5 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e   allocation chun
110f6 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f  k..*/.#define RO
110f7 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43  WSET_ENTRY_PER_C
110f8 48 55 4e 4b 20 20 5c 0a 20 20 20 20 20 20 20 20  HUNK  \.        
110f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
110fa 28 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49  (ROWSET_ALLOCATI
110fb 4f 4e 5f 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f  ON_SIZE-8)/sizeo
110fc 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45  f(struct RowSetE
110fd 6e 74 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61  ntry))../*.** Ea
110fe 63 68 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f  ch entry in a Ro
110ff 77 53 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61  wSet is an insta
11100 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
11101 77 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  wing object..*/.
11102 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
11103 72 79 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  ry {            
11104 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20  .  i64 v;       
11105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11106 20 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20   /* ROWID value 
11107 66 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a  for this entry *
11108 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  /.  struct RowSe
11109 74 45 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20  tEntry *pRight; 
1110a 20 20 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72    /* Right subtr
1110b 65 65 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69  ee (larger entri
1110c 65 73 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20  es) or list */. 
1110d 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
1110e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f  try *pLeft;    /
1110f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 28  * Left subtree (
11110 73 6d 61 6c 6c 65 72 20 65 6e 74 72 69 65 73 29  smaller entries)
11111 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f   */.};../*.** Ro
11112 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74  wSetEntry object
11113 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20  s are allocated 
11114 69 6e 20 6c 61 72 67 65 20 63 68 75 6e 6b 73 20  in large chunks 
11115 28 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68  (instances of th
11116 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73  e.** following s
11117 74 72 75 63 74 75 72 65 29 20 74 6f 20 72 65 64  tructure) to red
11118 75 63 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  uce memory alloc
11119 61 74 69 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20  ation overhead. 
1111a 20 54 68 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61   The.** chunks a
1111b 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e  re kept on a lin
1111c 6b 65 64 20 6c 69 73 74 20 73 6f 20 74 68 61 74  ked list so that
1111d 20 74 68 65 79 20 63 61 6e 20 62 65 20 64 65 61   they can be dea
1111e 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e  llocated.** when
1111f 20 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 64   the RowSet is d
11120 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72  estroyed..*/.str
11121 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20  uct RowSetChunk 
11122 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  {.  struct RowSe
11123 74 43 68 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75  tChunk *pNextChu
11124 6e 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  nk;        /* Ne
11125 78 74 20 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74  xt chunk on list
11126 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a   of them all */.
11127 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11128 6e 74 72 79 20 61 45 6e 74 72 79 5b 52 4f 57 53  ntry aEntry[ROWS
11129 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55  ET_ENTRY_PER_CHU
1112a 4e 4b 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  NK]; /* Allocate
1112b 64 20 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a  d entries */.};.
1112c 0a 2f 2a 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20  ./*.** A RowSet 
1112d 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  in an instance o
1112e 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
1112f 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
11130 20 41 20 74 79 70 65 64 65 66 20 6f 66 20 74 68   A typedef of th
11131 69 73 20 73 74 72 75 63 74 75 72 65 20 69 66 20  is structure if 
11132 66 6f 75 6e 64 20 69 6e 20 73 71 6c 69 74 65 49  found in sqliteI
11133 6e 74 2e 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  nt.h..*/.struct 
11134 52 6f 77 53 65 74 20 7b 0a 20 20 73 74 72 75 63  RowSet {.  struc
11135 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70  t RowSetChunk *p
11136 43 68 75 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73  Chunk;    /* Lis
11137 74 20 6f 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61  t of all chunk a
11138 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20  llocations */.  
11139 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
1113a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1113b 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1113c 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73  onnection */.  s
1113d 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
1113e 79 20 2a 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a  y *pEntry;    /*
1113f 20 4c 69 73 74 20 6f 66 20 65 6e 74 72 69 65 73   List of entries
11140 20 75 73 69 6e 67 20 70 52 69 67 68 74 20 2a 2f   using pRight */
11141 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
11142 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20  Entry *pLast;   
11143 20 20 2f 2a 20 4c 61 73 74 20 65 6e 74 72 79 20    /* Last entry 
11144 6f 6e 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69  on the pEntry li
11145 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  st */.  struct R
11146 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 46 72 65  owSetEntry *pFre
11147 73 68 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65  sh;    /* Source
11148 20 6f 66 20 6e 65 77 20 65 6e 74 72 79 20 6f 62   of new entry ob
11149 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63  jects */.  struc
1114a 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
1114b 54 72 65 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e  Tree;     /* Bin
1114c 61 72 79 20 74 72 65 65 20 6f 66 20 65 6e 74 72  ary tree of entr
1114d 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72  ies */.  u16 nFr
1114e 65 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  esh;            
1114f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
11150 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20  r of objects on 
11151 70 46 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 69  pFresh */.  u8 i
11152 73 53 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20  sSorted;        
11153 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
11154 75 65 20 69 66 20 70 45 6e 74 72 79 20 69 73 20  ue if pEntry is 
11155 73 6f 72 74 65 64 20 2a 2f 0a 20 20 75 38 20 69  sorted */.  u8 i
11156 42 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20  Batch;          
11157 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
11158 72 72 65 6e 74 20 69 6e 73 65 72 74 20 62 61 74  rrent insert bat
11159 63 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ch */.};../*.** 
1115a 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79  Turn bulk memory
1115b 20 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f   into a RowSet o
1115c 62 6a 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20  bject.  N bytes 
1115d 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65  of memory.** are
1115e 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53   available at pS
1115f 70 61 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f  pace.  The db po
11160 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73  inter is used as
11161 20 61 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78   a memory contex
11162 74 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62  t.** for any sub
11163 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69  sequent allocati
11164 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ons that need to
11165 20 6f 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72   occur..** Retur
11166 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
11167 68 65 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62  he new RowSet ob
11168 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d  ject..**.** It m
11169 75 73 74 20 62 65 20 74 68 65 20 63 61 73 65 20  ust be the case 
1116a 74 68 61 74 20 4e 20 69 73 20 73 75 66 66 69 63  that N is suffic
1116b 69 65 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52  ient to make a R
1116c 6f 77 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a  owset.  If not.*
1116d 2a 20 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66  * an assertion f
1116e 61 75 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20  ault occurs..** 
1116f 0a 2a 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67  .** If N is larg
11170 65 72 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69  er than the mini
11171 6d 75 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72  mum, use the sur
11172 70 6c 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69  plus as an initi
11173 61 6c 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  al.** allocation
11174 20 6f 66 20 65 6e 74 72 69 65 73 20 61 76 61 69   of entries avai
11175 6c 61 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c  lable to be fill
11176 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
11177 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71  IVATE RowSet *sq
11178 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28  lite3RowSetInit(
11179 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69  sqlite3 *db, voi
1117a 64 20 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67  d *pSpace, unsig
1117b 6e 65 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f  ned int N){.  Ro
1117c 77 53 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72  wSet *p;.  asser
1117d 74 28 20 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73  t( N >= ROUND8(s
1117e 69 7a 65 6f 66 28 2a 70 29 29 20 29 3b 0a 20 20  izeof(*p)) );.  
1117f 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 70 2d  p = pSpace;.  p-
11180 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70  >pChunk = 0;.  p
11181 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e  ->db = db;.  p->
11182 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d  pEntry = 0;.  p-
11183 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d  >pLast = 0;.  p-
11184 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d  >pTree = 0;.  p-
11185 3e 70 46 72 65 73 68 20 3d 20 28 73 74 72 75 63  >pFresh = (struc
11186 74 20 52 6f 77 53 65 74 45 6e 74 72 79 2a 29 28  t RowSetEntry*)(
11187 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70  ROUND8(sizeof(*p
11188 29 29 20 2b 20 28 63 68 61 72 2a 29 70 29 3b 0a  )) + (char*)p);.
11189 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 28 75    p->nFresh = (u
1118a 31 36 29 28 28 4e 20 2d 20 52 4f 55 4e 44 38 28  16)((N - ROUND8(
1118b 73 69 7a 65 6f 66 28 2a 70 29 29 29 2f 73 69 7a  sizeof(*p)))/siz
1118c 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 65  eof(struct RowSe
1118d 74 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e 69  tEntry));.  p->i
1118e 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20 70  sSorted = 1;.  p
1118f 2d 3e 69 42 61 74 63 68 20 3d 20 30 3b 0a 20 20  ->iBatch = 0;.  
11190 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
11191 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c  ** Deallocate al
11192 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20  l chunks from a 
11193 52 6f 77 53 65 74 2e 20 20 54 68 69 73 20 66 72  RowSet.  This fr
11194 65 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74  ees all memory t
11195 68 61 74 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65  hat.** the RowSe
11196 74 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20  t has allocated 
11197 6f 76 65 72 20 69 74 73 20 6c 69 66 65 74 69 6d  over its lifetim
11198 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
11199 20 69 73 0a 2a 2a 20 74 68 65 20 64 65 73 74 72   is.** the destr
1119a 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52 6f  uctor for the Ro
1119b 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  wSet..*/.SQLITE_
1119c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1119d 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28  ite3RowSetClear(
1119e 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 73 74  RowSet *p){.  st
1119f 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b  ruct RowSetChunk
111a0 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 74   *pChunk, *pNext
111a1 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 68  Chunk;.  for(pCh
111a2 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 70  unk=p->pChunk; p
111a3 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d 20  Chunk; pChunk = 
111a4 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 20 20  pNextChunk){.   
111a5 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 43   pNextChunk = pC
111a6 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 75 6e 6b  hunk->pNextChunk
111a7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
111a8 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e  ree(p->db, pChun
111a9 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68  k);.  }.  p->pCh
111aa 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46  unk = 0;.  p->nF
111ab 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70  resh = 0;.  p->p
111ac 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e  Entry = 0;.  p->
111ad 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e  pLast = 0;.  p->
111ae 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  pTree = 0;.  p->
111af 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a  isSorted = 1;.}.
111b0 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
111b1 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61  new value into a
111b2 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54   RowSet..**.** T
111b3 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
111b4 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 61  flag of the data
111b5 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
111b6 69 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d  is set if a.** m
111b7 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
111b8 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54   fails..*/.SQLIT
111b9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
111ba 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65  qlite3RowSetInse
111bb 72 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36  rt(RowSet *p, i6
111bc 34 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 75  4 rowid){.  stru
111bd 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
111be 70 45 6e 74 72 79 3b 20 20 2f 2a 20 54 68 65 20  pEntry;  /* The 
111bf 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73  new entry */.  s
111c0 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
111c1 79 20 2a 70 4c 61 73 74 3b 20 20 20 2f 2a 20 54  y *pLast;   /* T
111c2 68 65 20 6c 61 73 74 20 70 72 69 6f 72 20 65 6e  he last prior en
111c3 74 72 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  try */.  assert(
111c4 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70   p!=0 );.  if( p
111c5 2d 3e 6e 46 72 65 73 68 3d 3d 30 20 29 7b 0a 20  ->nFresh==0 ){. 
111c6 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74     struct RowSet
111c7 43 68 75 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 20  Chunk *pNew;.   
111c8 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
111c9 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62  bMallocRaw(p->db
111ca 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29  , sizeof(*pNew))
111cb 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d  ;.    if( pNew==
111cc 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
111cd 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  n;.    }.    pNe
111ce 77 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20  w->pNextChunk = 
111cf 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70  p->pChunk;.    p
111d0 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b  ->pChunk = pNew;
111d1 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 3d  .    p->pFresh =
111d2 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20   pNew->aEntry;. 
111d3 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52     p->nFresh = R
111d4 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f  OWSET_ENTRY_PER_
111d5 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e  CHUNK;.  }.  pEn
111d6 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b  try = p->pFresh+
111d7 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d  +;.  p->nFresh--
111d8 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20  ;.  pEntry->v = 
111d9 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d  rowid;.  pEntry-
111da 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70  >pRight = 0;.  p
111db 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b  Last = p->pLast;
111dc 0a 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b 0a  .  if( pLast ){.
111dd 20 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f 72      if( p->isSor
111de 74 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c  ted && rowid<=pL
111df 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20  ast->v ){.      
111e0 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 3b  p->isSorted = 0;
111e1 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74  .    }.    pLast
111e2 2d 3e 70 52 69 67 68 74 20 3d 20 70 45 6e 74 72  ->pRight = pEntr
111e3 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  y;.  }else{.    
111e4 61 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72  assert( p->pEntr
111e5 79 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65 73  y==0 ); /* Fires
111e6 20 69 66 20 49 4e 53 45 52 54 20 61 66 74 65 72   if INSERT after
111e7 20 53 4d 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 20   SMALLEST */.   
111e8 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e   p->pEntry = pEn
111e9 74 72 79 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c  try;.  }.  p->pL
111ea 61 73 74 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a  ast = pEntry;.}.
111eb 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f  ./*.** Merge two
111ec 20 6c 69 73 74 73 20 6f 66 20 52 6f 77 53 65 74   lists of RowSet
111ed 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 20 20  Entry objects.  
111ee 52 65 6d 6f 76 65 20 64 75 70 6c 69 63 61 74 65  Remove duplicate
111ef 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  s..**.** The inp
111f0 75 74 20 6c 69 73 74 73 20 61 72 65 20 63 6f 6e  ut lists are con
111f1 6e 65 63 74 65 64 20 76 69 61 20 70 52 69 67 68  nected via pRigh
111f2 74 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 61  t pointers and a
111f3 72 65 20 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74  re .** assumed t
111f4 6f 20 65 61 63 68 20 61 6c 72 65 61 64 79 20 62  o each already b
111f5 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  e in sorted orde
111f6 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  r..*/.static str
111f7 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
111f8 2a 72 6f 77 53 65 74 4d 65 72 67 65 28 0a 20 20  *rowSetMerge(.  
111f9 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
111fa 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69  ry *pA,    /* Fi
111fb 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74 20  rst sorted list 
111fc 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a  to be merged */.
111fd 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
111fe 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a 20  ntry *pB     /* 
111ff 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69  Second sorted li
11200 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20  st to be merged 
11201 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52  */.){.  struct R
11202 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b  owSetEntry head;
11203 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
11204 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20  Entry *pTail;.. 
11205 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a   pTail = &head;.
11206 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70    while( pA && p
11207 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  B ){.    assert(
11208 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c   pA->pRight==0 |
11209 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69  | pA->v<=pA->pRi
1120a 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73  ght->v );.    as
1120b 73 65 72 74 28 20 70 42 2d 3e 70 52 69 67 68 74  sert( pB->pRight
1120c 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42  ==0 || pB->v<=pB
1120d 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20  ->pRight->v );. 
1120e 20 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d     if( pA->v<pB-
1120f 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69  >v ){.      pTai
11210 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a  l->pRight = pA;.
11211 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70        pA = pA->p
11212 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 61  Right;.      pTa
11213 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67  il = pTail->pRig
11214 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ht;.    }else if
11215 28 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 7b  ( pB->v<pA->v ){
11216 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52  .      pTail->pR
11217 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 20 20  ight = pB;.     
11218 20 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 68 74   pB = pB->pRight
11219 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20  ;.      pTail = 
1121a 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20  pTail->pRight;. 
1121b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1121c 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b  pA = pA->pRight;
1121d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
1121e 20 70 41 20 29 7b 0a 20 20 20 20 61 73 73 65 72   pA ){.    asser
1121f 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30  t( pA->pRight==0
11220 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70   || pA->v<=pA->p
11221 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20  Right->v );.    
11222 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20  pTail->pRight = 
11223 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  pA;.  }else{.   
11224 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 7c   assert( pB==0 |
11225 7c 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20  | pB->pRight==0 
11226 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52  || pB->v<=pB->pR
11227 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70  ight->v );.    p
11228 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70  Tail->pRight = p
11229 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  B;.  }.  return 
1122a 68 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d 0a 0a  head.pRight;.}..
1122b 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65  /*.** Sort all e
1122c 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70  lements on the p
1122d 45 6e 74 72 79 20 6c 69 73 74 20 6f 66 20 74 68  Entry list of th
1122e 65 20 52 6f 77 53 65 74 20 69 6e 74 6f 20 61 73  e RowSet into as
1122f 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a  cending order..*
11230 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  / .static void r
11231 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74  owSetSort(RowSet
11232 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64   *p){.  unsigned
11233 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
11234 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45   RowSetEntry *pE
11235 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52  ntry;.  struct R
11236 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 63  owSetEntry *aBuc
11237 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65  ket[40];..  asse
11238 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d  rt( p->isSorted=
11239 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61  =0 );.  memset(a
1123a 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f  Bucket, 0, sizeo
1123b 66 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 77  f(aBucket));.  w
1123c 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 20  hile( p->pEntry 
1123d 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20  ){.    pEntry = 
1123e 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 70  p->pEntry;.    p
1123f 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72  ->pEntry = pEntr
11240 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70  y->pRight;.    p
11241 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20  Entry->pRight = 
11242 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
11243 61 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29  aBucket[i]; i++)
11244 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20 3d  {.      pEntry =
11245 20 72 6f 77 53 65 74 4d 65 72 67 65 28 61 42 75   rowSetMerge(aBu
11246 63 6b 65 74 5b 69 5d 2c 20 70 45 6e 74 72 79 29  cket[i], pEntry)
11247 3b 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74 5b  ;.      aBucket[
11248 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  i] = 0;.    }.  
11249 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70    aBucket[i] = p
1124a 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e  Entry;.  }.  pEn
1124b 74 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  try = 0;.  for(i
1124c 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75  =0; i<sizeof(aBu
1124d 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75  cket)/sizeof(aBu
1124e 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  cket[0]); i++){.
1124f 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77      pEntry = row
11250 53 65 74 4d 65 72 67 65 28 70 45 6e 74 72 79 2c  SetMerge(pEntry,
11251 20 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20   aBucket[i]);.  
11252 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20  }.  p->pEntry = 
11253 70 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61  pEntry;.  p->pLa
11254 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53  st = 0;.  p->isS
11255 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f  orted = 1;.}.../
11256 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 2c 20  *.** The input, 
11257 70 49 6e 2c 20 69 73 20 61 20 62 69 6e 61 72 79  pIn, is a binary
11258 20 74 72 65 65 20 28 6f 72 20 73 75 62 74 72 65   tree (or subtre
11259 65 29 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72  e) of RowSetEntr
1125a 79 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 6f  y objects..** Co
1125b 6e 76 65 72 74 20 74 68 69 73 20 74 72 65 65 20  nvert this tree 
1125c 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69  into a linked li
1125d 73 74 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  st connected by 
1125e 74 68 65 20 70 52 69 67 68 74 20 70 6f 69 6e 74  the pRight point
1125f 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72  ers.** and retur
11260 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  n pointers to th
11261 65 20 66 69 72 73 74 20 61 6e 64 20 6c 61 73 74  e first and last
11262 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
11263 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74   new list..*/.st
11264 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74  atic void rowSet
11265 54 72 65 65 54 6f 4c 69 73 74 28 0a 20 20 73 74  TreeToList(.  st
11266 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11267 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20 2f   *pIn,         /
11268 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e  * Root of the in
11269 70 75 74 20 74 72 65 65 20 2a 2f 0a 20 20 73 74  put tree */.  st
1126a 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
1126b 20 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20 2f   **ppFirst,    /
1126c 2a 20 57 72 69 74 65 20 68 65 61 64 20 6f 66 20  * Write head of 
1126d 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20  the output list 
1126e 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  here */.  struct
1126f 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70   RowSetEntry **p
11270 70 4c 61 73 74 20 20 20 20 20 20 2f 2a 20 57 72  pLast      /* Wr
11271 69 74 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20  ite tail of the 
11272 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65  output list here
11273 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   */.){.  assert(
11274 20 70 49 6e 21 3d 30 20 29 3b 0a 20 20 69 66 28   pIn!=0 );.  if(
11275 20 70 49 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20   pIn->pLeft ){. 
11276 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74     struct RowSet
11277 45 6e 74 72 79 20 2a 70 3b 0a 20 20 20 20 72 6f  Entry *p;.    ro
11278 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70  wSetTreeToList(p
11279 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 46 69 72  In->pLeft, ppFir
1127a 73 74 2c 20 26 70 29 3b 0a 20 20 20 20 70 2d 3e  st, &p);.    p->
1127b 70 52 69 67 68 74 20 3d 20 70 49 6e 3b 0a 20 20  pRight = pIn;.  
1127c 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 46 69  }else{.    *ppFi
1127d 72 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20  rst = pIn;.  }. 
1127e 20 69 66 28 20 70 49 6e 2d 3e 70 52 69 67 68 74   if( pIn->pRight
1127f 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 54 72   ){.    rowSetTr
11280 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52  eeToList(pIn->pR
11281 69 67 68 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67  ight, &pIn->pRig
11282 68 74 2c 20 70 70 4c 61 73 74 29 3b 0a 20 20 7d  ht, ppLast);.  }
11283 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 4c 61 73  else{.    *ppLas
11284 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 61  t = pIn;.  }.  a
11285 73 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74 29  ssert( (*ppLast)
11286 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 7d  ->pRight==0 );.}
11287 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  .../*.** Convert
11288 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f   a sorted list o
11289 66 20 65 6c 65 6d 65 6e 74 73 20 28 63 6f 6e 6e  f elements (conn
1128a 65 63 74 65 64 20 62 79 20 70 52 69 67 68 74 29  ected by pRight)
1128b 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 0a 2a   into a binary.*
1128c 2a 20 74 72 65 65 20 77 69 74 68 20 64 65 70 74  * tree with dept
1128d 68 20 6f 66 20 69 44 65 70 74 68 2e 20 20 41 20  h of iDepth.  A 
1128e 64 65 70 74 68 20 6f 66 20 31 20 6d 65 61 6e 73  depth of 1 means
1128f 20 74 68 65 20 74 72 65 65 20 63 6f 6e 74 61 69   the tree contai
11290 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6e  ns a single.** n
11291 6f 64 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74  ode taken from t
11292 68 65 20 68 65 61 64 20 6f 66 20 2a 70 70 4c 69  he head of *ppLi
11293 73 74 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20  st.  A depth of 
11294 32 20 6d 65 61 6e 73 20 61 20 74 72 65 65 20 77  2 means a tree w
11295 69 74 68 0a 2a 2a 20 74 68 72 65 65 20 6e 6f 64  ith.** three nod
11296 65 73 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74  es.  And so fort
11297 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 20  h..**.** Use as 
11298 6d 61 6e 79 20 65 6e 74 72 69 65 73 20 66 72 6f  many entries fro
11299 6d 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 74  m the input list
1129a 20 61 73 20 72 65 71 75 69 72 65 64 20 61 6e 64   as required and
1129b 20 75 70 64 61 74 65 20 74 68 65 0a 2a 2a 20 2a   update the.** *
1129c 70 70 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74 20  ppList to point 
1129d 74 6f 20 74 68 65 20 75 6e 75 73 65 64 20 65 6c  to the unused el
1129e 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69  ements of the li
1129f 73 74 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75  st.  If the inpu
112a0 74 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69  t.** list contai
112a1 6e 73 20 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65  ns too few eleme
112a2 6e 74 73 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72  nts, then constr
112a3 75 63 74 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74  uct an incomplet
112a4 65 20 74 72 65 65 0a 2a 2a 20 61 6e 64 20 6c 65  e tree.** and le
112a5 61 76 65 20 2a 70 70 4c 69 73 74 20 73 65 74 20  ave *ppList set 
112a6 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52  to NULL..**.** R
112a7 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
112a8 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  to the root of t
112a9 68 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  he constructed b
112aa 69 6e 61 72 79 20 74 72 65 65 2e 0a 2a 2f 0a 73  inary tree..*/.s
112ab 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f 77  tatic struct Row
112ac 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 74  SetEntry *rowSet
112ad 4e 44 65 65 70 54 72 65 65 28 0a 20 20 73 74 72  NDeepTree(.  str
112ae 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
112af 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20  **ppList,.  int 
112b0 69 44 65 70 74 68 0a 29 7b 0a 20 20 73 74 72 75  iDepth.){.  stru
112b1 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
112b2 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  p;         /* Ro
112b3 6f 74 20 6f 66 20 74 68 65 20 6e 65 77 20 74 72  ot of the new tr
112b4 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ee */.  struct R
112b5 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 66  owSetEntry *pLef
112b6 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73  t;     /* Left s
112b7 75 62 74 72 65 65 20 2a 2f 0a 20 20 69 66 28 20  ubtree */.  if( 
112b8 2a 70 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  *ppList==0 ){.  
112b9 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
112ba 20 20 69 66 28 20 69 44 65 70 74 68 3d 3d 31 20    if( iDepth==1 
112bb 29 7b 0a 20 20 20 20 70 20 3d 20 2a 70 70 4c 69  ){.    p = *ppLi
112bc 73 74 3b 0a 20 20 20 20 2a 70 70 4c 69 73 74 20  st;.    *ppList 
112bd 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  = p->pRight;.   
112be 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70   p->pLeft = p->p
112bf 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 72  Right = 0;.    r
112c0 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70  eturn p;.  }.  p
112c1 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44 65  Left = rowSetNDe
112c2 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69  epTree(ppList, i
112c3 44 65 70 74 68 2d 31 29 3b 0a 20 20 70 20 3d 20  Depth-1);.  p = 
112c4 2a 70 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  *ppList;.  if( p
112c5 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
112c6 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 70  n pLeft;.  }.  p
112c7 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b  ->pLeft = pLeft;
112c8 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e  .  *ppList = p->
112c9 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 52 69  pRight;.  p->pRi
112ca 67 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65  ght = rowSetNDee
112cb 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44  pTree(ppList, iD
112cc 65 70 74 68 2d 31 29 3b 0a 20 20 72 65 74 75 72  epth-1);.  retur
112cd 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n p;.}../*.** Co
112ce 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c  nvert a sorted l
112cf 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ist of elements 
112d0 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 20 74 72  into a binary tr
112d1 65 65 2e 20 4d 61 6b 65 20 74 68 65 20 74 72 65  ee. Make the tre
112d2 65 0a 2a 2a 20 61 73 20 64 65 65 70 20 61 73 20  e.** as deep as 
112d3 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69  it needs to be i
112d4 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 61  n order to conta
112d5 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69  in the entire li
112d6 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  st..*/.static st
112d7 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
112d8 20 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72   *rowSetListToTr
112d9 65 65 28 73 74 72 75 63 74 20 52 6f 77 53 65 74  ee(struct RowSet
112da 45 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0a 20  Entry *pList){. 
112db 20 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20   int iDepth;    
112dc 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20         /* Depth 
112dd 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 20 66  of the tree so f
112de 61 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ar */.  struct R
112df 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20  owSetEntry *p;  
112e0 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
112e1 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 73  tree root */.  s
112e2 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
112e3 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 4c  y *pLeft;   /* L
112e4 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 0a  eft subtree */..
112e5 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21    assert( pList!
112e6 3d 30 20 29 3b 0a 20 20 70 20 3d 20 70 4c 69 73  =0 );.  p = pLis
112e7 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e  t;.  pList = p->
112e8 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 4c 65  pRight;.  p->pLe
112e9 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d  ft = p->pRight =
112ea 20 30 3b 0a 20 20 66 6f 72 28 69 44 65 70 74 68   0;.  for(iDepth
112eb 3d 31 3b 20 70 4c 69 73 74 3b 20 69 44 65 70 74  =1; pList; iDept
112ec 68 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 66 74 20  h++){.    pLeft 
112ed 3d 20 70 3b 0a 20 20 20 20 70 20 3d 20 70 4c 69  = p;.    p = pLi
112ee 73 74 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20  st;.    pList = 
112ef 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70  p->pRight;.    p
112f0 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b  ->pLeft = pLeft;
112f1 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d  .    p->pRight =
112f2 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65   rowSetNDeepTree
112f3 28 26 70 4c 69 73 74 2c 20 69 44 65 70 74 68 29  (&pList, iDepth)
112f4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
112f5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
112f6 72 74 20 74 68 65 20 6c 69 73 74 20 69 6e 20 70  rt the list in p
112f7 2d 3e 70 45 6e 74 72 79 20 69 6e 74 6f 20 61 20  ->pEntry into a 
112f8 73 6f 72 74 65 64 20 6c 69 73 74 20 69 66 20 69  sorted list if i
112f9 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 74  t is not.** sort
112fa 65 64 20 61 6c 72 65 61 64 79 2e 20 20 49 66 20  ed already.  If 
112fb 74 68 65 72 65 20 69 73 20 61 20 62 69 6e 61 72  there is a binar
112fc 79 20 74 72 65 65 20 6f 6e 20 70 2d 3e 70 54 72  y tree on p->pTr
112fd 65 65 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76  ee, then.** conv
112fe 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6c 69  ert it into a li
112ff 73 74 20 74 6f 6f 20 61 6e 64 20 6d 65 72 67 65  st too and merge
11300 20 69 74 20 69 6e 74 6f 20 74 68 65 20 70 2d 3e   it into the p->
11301 70 45 6e 74 72 79 20 6c 69 73 74 2e 0a 2a 2f 0a  pEntry list..*/.
11302 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53  static void rowS
11303 65 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 20  etToList(RowSet 
11304 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e 69  *p){.  if( !p->i
11305 73 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20 72  sSorted ){.    r
11306 6f 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 20  owSetSort(p);.  
11307 7d 0a 20 20 69 66 28 20 70 2d 3e 70 54 72 65 65  }.  if( p->pTree
11308 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52   ){.    struct R
11309 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 48 65 61  owSetEntry *pHea
1130a 64 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 20 20 72  d, *pTail;.    r
1130b 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28  owSetTreeToList(
1130c 70 2d 3e 70 54 72 65 65 2c 20 26 70 48 65 61 64  p->pTree, &pHead
1130d 2c 20 26 70 54 61 69 6c 29 3b 0a 20 20 20 20 70  , &pTail);.    p
1130e 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 20  ->pTree = 0;.   
1130f 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77   p->pEntry = row
11310 53 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74  SetMerge(p->pEnt
11311 72 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 7d 0a  ry, pHead);.  }.
11312 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
11313 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c   the smallest el
11314 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52  ement from the R
11315 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20  owSet..** Write 
11316 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f  the element into
11317 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72   *pRowid.  Retur
11318 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  n 1 on success. 
11319 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20   Return.** 0 if 
1131a 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c  the RowSet is al
1131b 72 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a  ready empty..**.
1131c 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  ** After this ro
1131d 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63  utine has been c
1131e 61 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 74  alled, the sqlit
1131f 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 29  e3RowSetInsert()
11320 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 20  .** routine may 
11321 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67  not be called ag
11322 61 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45  ain.  .*/.SQLITE
11323 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
11324 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52  ite3RowSetNext(R
11325 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a 70  owSet *p, i64 *p
11326 52 6f 77 69 64 29 7b 0a 20 20 72 6f 77 53 65 74  Rowid){.  rowSet
11327 54 6f 4c 69 73 74 28 70 29 3b 0a 20 20 69 66 28  ToList(p);.  if(
11328 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20   p->pEntry ){.  
11329 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70    *pRowid = p->p
1132a 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 70 2d  Entry->v;.    p-
1132b 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e  >pEntry = p->pEn
1132c 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  try->pRight;.   
1132d 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d   if( p->pEntry==
1132e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1132f 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 29  e3RowSetClear(p)
11330 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
11331 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rn 1;.  }else{. 
11332 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
11333 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
11334 74 6f 20 73 65 65 20 69 66 20 65 6c 65 6d 65 6e  to see if elemen
11335 74 20 69 52 6f 77 69 64 20 77 61 73 20 69 6e 73  t iRowid was ins
11336 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74  erted into the t
11337 68 65 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20  he rowset as.** 
11338 70 61 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 65  part of any inse
11339 72 74 20 62 61 74 63 68 20 70 72 69 6f 72 20 74  rt batch prior t
1133a 6f 20 69 42 61 74 63 68 2e 20 20 52 65 74 75 72  o iBatch.  Retur
1133b 6e 20 31 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c  n 1 or 0..*/.SQL
1133c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1133d 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73  sqlite3RowSetTes
1133e 74 28 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65  t(RowSet *pRowSe
1133f 74 2c 20 75 38 20 69 42 61 74 63 68 2c 20 73 71  t, u8 iBatch, sq
11340 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
11341 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f  id){.  struct Ro
11342 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20  wSetEntry *p;.  
11343 69 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f 77  if( iBatch!=pRow
11344 53 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0a 20  Set->iBatch ){. 
11345 20 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d 3e     if( pRowSet->
11346 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 20 20  pEntry ){.      
11347 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 52 6f  rowSetToList(pRo
11348 77 53 65 74 29 3b 0a 20 20 20 20 20 20 70 52 6f  wSet);.      pRo
11349 77 53 65 74 2d 3e 70 54 72 65 65 20 3d 20 72 6f  wSet->pTree = ro
1134a 77 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 70  wSetListToTree(p
1134b 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 29 3b  RowSet->pEntry);
1134c 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e  .      pRowSet->
1134d 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 20 20  pEntry = 0;.    
1134e 20 20 70 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74    pRowSet->pLast
1134f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
11350 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 20  pRowSet->iBatch 
11351 3d 20 69 42 61 74 63 68 3b 0a 20 20 7d 0a 20 20  = iBatch;.  }.  
11352 70 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 54 72  p = pRowSet->pTr
11353 65 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29  ee;.  while( p )
11354 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 3c 69  {.    if( p->v<i
11355 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 70  Rowid ){.      p
11356 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20   = p->pRight;.  
11357 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 76    }else if( p->v
11358 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  >iRowid ){.     
11359 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20   p = p->pLeft;. 
1135a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1135b 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1135c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
1135d 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
1135e 2a 2a 20 45 6e 64 20 6f 66 20 72 6f 77 73 65 74  ** End of rowset
1135f 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
11360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11362 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
11363 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61  ** Begin file pa
11364 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ger.c **********
11365 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11366 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11367 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
11368 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
11369 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1136a 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1136b 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1136c 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1136d 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1136e 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1136f 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
11370 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
11371 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
11372 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
11373 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
11374 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
11375 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
11376 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
11377 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
11378 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
11379 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1137a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1137b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1137c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1137d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1137e 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
1137f 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  s is the impleme
11380 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70  ntation of the p
11381 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73  age cache subsys
11382 74 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a  tem or "pager"..
11383 2a 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  ** .** The pager
11384 20 69 73 20 75 73 65 64 20 74 6f 20 61 63 63 65   is used to acce
11385 73 73 20 61 20 64 61 74 61 62 61 73 65 20 64 69  ss a database di
11386 73 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70  sk file.  It imp
11387 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69  lements.** atomi
11388 63 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c  c commit and rol
11389 6c 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68  lback through th
1138a 65 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e  e use of a journ
1138b 61 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20  al file that.** 
1138c 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d  is separate from
1138d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1138e 6c 65 2e 20 20 54 68 65 20 70 61 67 65 72 20 61  le.  The pager a
1138f 6c 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66  lso implements f
11390 69 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74  ile.** locking t
11391 6f 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72  o prevent two pr
11392 6f 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69  ocesses from wri
11393 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61  ting the same da
11394 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73  tabase.** file s
11395 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f  imultaneously, o
11396 72 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72  r one process fr
11397 6f 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20 64  om reading the d
11398 61 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a  atabase while.**
11399 20 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74   another is writ
1139a 69 6e 67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ing..*/.#ifndef 
1139b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
1139c 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  IO../*.** Macros
1139d 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f   for troubleshoo
1139e 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20  ting.  Normally 
1139f 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2f 0a 23 69  turned off.*/.#i
113a0 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 50  f 0.int sqlite3P
113a1 61 67 65 72 54 72 61 63 65 3d 31 3b 20 20 2f 2a  agerTrace=1;  /*
113a2 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20   True to enable 
113a3 74 72 61 63 69 6e 67 20 2a 2f 0a 23 64 65 66 69  tracing */.#defi
113a4 6e 65 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  ne sqlite3DebugP
113a5 72 69 6e 74 66 20 70 72 69 6e 74 66 0a 23 64 65  rintf printf.#de
113a6 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 28  fine PAGERTRACE(
113a7 58 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  X)     if( sqlit
113a8 65 33 50 61 67 65 72 54 72 61 63 65 20 29 7b 20  e3PagerTrace ){ 
113a9 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
113aa 74 66 20 58 3b 20 7d 0a 23 65 6c 73 65 0a 23 64  tf X; }.#else.#d
113ab 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45  efine PAGERTRACE
113ac 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (X).#endif../*.*
113ad 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
113ae 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 20 75  two macros are u
113af 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 50  sed within the P
113b0 41 47 45 52 54 52 41 43 45 28 29 20 6d 61 63 72  AGERTRACE() macr
113b1 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 70  os above.** to p
113b2 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d 64 65  rint out file-de
113b3 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a 2a  scriptors. .**.*
113b4 2a 20 50 41 47 45 52 49 44 28 29 20 74 61 6b 65  * PAGERID() take
113b5 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
113b6 20 50 61 67 65 72 20 73 74 72 75 63 74 20 61 73   Pager struct as
113b7 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 20 54   its argument. T
113b8 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  he.** associated
113b9 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72   file-descriptor
113ba 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 46 49   is returned. FI
113bb 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74 61 6b  LEHANDLEID() tak
113bc 65 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 66 69  es an sqlite3_fi
113bd 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 61 73 20  le.** struct as 
113be 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  its argument..*/
113bf 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 49 44  .#define PAGERID
113c0 28 70 29 20 28 28 69 6e 74 29 28 70 2d 3e 66 64  (p) ((int)(p->fd
113c1 29 29 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 48  )).#define FILEH
113c2 41 4e 44 4c 45 49 44 28 66 64 29 20 28 28 69 6e  ANDLEID(fd) ((in
113c3 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  t)fd)../*.** The
113c4 20 70 61 67 65 20 63 61 63 68 65 20 61 73 20 61   page cache as a
113c5 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 61 79 73   whole is always
113c6 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   in one of the f
113c7 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 61 74  ollowing.** stat
113c8 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  es:.**.**   PAGE
113c9 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20  R_UNLOCK        
113ca 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69  The page cache i
113cb 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
113cc 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a 2a 20 20  reading or .**  
113cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113ce 20 20 20 20 20 77 72 69 74 69 6e 67 20 74 68 65       writing the
113cf 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
113d0 20 54 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20   There is no.** 
113d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113d2 20 20 20 20 20 20 64 61 74 61 20 68 65 6c 64 20        data held 
113d3 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73  in memory.  This
113d4 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c 0a   is the initial.
113d5 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
113d6 20 20 20 20 20 20 20 20 20 73 74 61 74 65 2e 0a           state..
113d7 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 48  **.**   PAGER_SH
113d8 41 52 45 44 20 20 20 20 20 20 20 20 54 68 65 20  ARED        The 
113d9 70 61 67 65 20 63 61 63 68 65 20 69 73 20 72 65  page cache is re
113da 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
113db 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  se..**          
113dc 20 20 20 20 20 20 20 20 20 20 20 20 20 57 72 69               Wri
113dd 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 65 72 6d  ting is not perm
113de 69 74 74 65 64 2e 20 20 54 68 65 72 65 20 63 61  itted.  There ca
113df 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  n be.**         
113e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 75                mu
113e1 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 20 61  ltiple readers a
113e2 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d  ccessing the sam
113e3 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20  e database.**   
113e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113e5 20 20 20 20 66 69 6c 65 20 61 74 20 74 68 65 20      file at the 
113e6 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  same time..**.**
113e7 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 45     PAGER_RESERVE
113e8 44 20 20 20 20 20 20 54 68 69 73 20 70 72 6f 63  D      This proc
113e9 65 73 73 20 68 61 73 20 72 65 73 65 72 76 65 64  ess has reserved
113ea 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f   the database fo
113eb 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 20 20 20  r writing.**    
113ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113ed 20 20 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79     but has not y
113ee 65 74 20 6d 61 64 65 20 61 6e 79 20 63 68 61 6e  et made any chan
113ef 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e 65 20 70  ges.  Only one p
113f0 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 20 20 20  rocess.**       
113f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113f2 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 72 65  at a time can re
113f3 73 65 72 76 65 20 74 68 65 20 64 61 74 61 62 61  serve the databa
113f4 73 65 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61  se.  The origina
113f5 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  l.**            
113f6 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62             datab
113f7 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e 6f 74  ase file has not
113f8 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 73   been modified s
113f9 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20  o other.**      
113fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113fb 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 73   processes may s
113fc 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 20  till be reading 
113fd 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 20  the on-disk.**  
113fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113ff 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69       database fi
11400 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  le..**.**   PAGE
11401 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  R_EXCLUSIVE     
11402 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69  The page cache i
11403 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61  s writing the da
11404 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20  tabase..**      
11405 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11406 20 41 63 63 65 73 73 20 69 73 20 65 78 63 6c 75   Access is exclu
11407 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 65 72 20  sive.  No other 
11408 70 72 6f 63 65 73 73 65 73 20 6f 72 0a 2a 2a 20  processes or.** 
11409 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1140a 20 20 20 20 20 20 74 68 72 65 61 64 73 20 63 61        threads ca
1140b 6e 20 62 65 20 72 65 61 64 69 6e 67 20 6f 72 20  n be reading or 
1140c 77 72 69 74 69 6e 67 20 77 68 69 6c 65 20 6f 6e  writing while on
1140d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
1140e 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65             proce
1140f 73 73 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a  ss is writing..*
11410 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 59 4e  *.**   PAGER_SYN
11411 43 45 44 20 20 20 20 20 20 20 20 54 68 65 20 70  CED        The p
11412 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 74 68  ager moves to th
11413 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20 50 41  is state from PA
11414 47 45 52 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a  GER_EXCLUSIVE.**
11415 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11416 20 20 20 20 20 20 20 61 66 74 65 72 20 61 6c 6c         after all
11417 20 64 69 72 74 79 20 70 61 67 65 73 20 68 61 76   dirty pages hav
11418 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  e been written t
11419 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  o the.**        
1141a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
1141b 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1141c 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 62 65   the file has be
1141d 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a 2a 2a 20  en synced to.** 
1141e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1141f 20 20 20 20 20 20 64 69 73 6b 2e 20 41 6c 6c 20        disk. All 
11420 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 6f 20  that remains to 
11421 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f 76 65 20  do is to remove 
11422 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  or.**           
11423 20 20 20 20 20 20 20 20 20 20 20 20 74 72 75 6e              trun
11424 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  cate the journal
11425 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 74 72   file and the tr
11426 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 20 20  ansaction .**   
11427 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11428 20 20 20 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d      will be comm
11429 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  itted..**.** The
1142a 20 70 61 67 65 20 63 61 63 68 65 20 63 6f 6d 65   page cache come
1142b 73 20 75 70 20 69 6e 20 50 41 47 45 52 5f 55 4e  s up in PAGER_UN
1142c 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74  LOCK.  The first
1142d 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 6c 69 74   time a.** sqlit
1142e 65 33 50 61 67 65 72 47 65 74 28 29 20 6f 63 63  e3PagerGet() occ
1142f 75 72 73 2c 20 74 68 65 20 73 74 61 74 65 20 74  urs, the state t
11430 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41  ransitions to PA
11431 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a 2a 20 41  GER_SHARED..** A
11432 66 74 65 72 20 61 6c 6c 20 70 61 67 65 73 20 68  fter all pages h
11433 61 76 65 20 62 65 65 6e 20 72 65 6c 65 61 73 65  ave been release
11434 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 5f 70  d using sqlite_p
11435 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a 2a 2a 20  age_unref(),.** 
11436 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69  the state transi
11437 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f 20 50 41  tions back to PA
11438 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65  GER_UNLOCK.  The
11439 20 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74   first time.** t
1143a 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  hat sqlite3Pager
1143b 57 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65  Write() is calle
1143c 64 2c 20 74 68 65 20 73 74 61 74 65 20 74 72 61  d, the state tra
1143d 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 50  nsitions to.** P
1143e 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 20  AGER_RESERVED.  
1143f 28 4e 6f 74 65 20 74 68 61 74 20 73 71 6c 69 74  (Note that sqlit
11440 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 63  e3PagerWrite() c
11441 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 63 61  an only be.** ca
11442 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 74 73 74  lled on an outst
11443 61 6e 64 69 6e 67 20 70 61 67 65 20 77 68 69 63  anding page whic
11444 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  h means that the
11445 20 70 61 67 65 72 20 6d 75 73 74 0a 2a 2a 20 62   pager must.** b
11446 65 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45  e in PAGER_SHARE
11447 44 20 62 65 66 6f 72 65 20 69 74 20 74 72 61 6e  D before it tran
11448 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52  sitions to PAGER
11449 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a 2a 20 50  _RESERVED.).** P
1144a 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 6d 65  AGER_RESERVED me
1144b 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 69  ans that there i
1144c 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c 6c 62 61  s an open rollba
1144d 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54  ck journal..** T
1144e 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f  he transition to
1144f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
11450 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20 61   occurs before a
11451 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72  ny changes.** ar
11452 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 64 61  e made to the da
11453 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 6f  tabase file, tho
11454 75 67 68 20 77 72 69 74 65 73 20 74 6f 20 74 68  ugh writes to th
11455 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f  e rollback.** jo
11456 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 77 69 74  urnal occurs wit
11457 68 20 6a 75 73 74 20 50 41 47 45 52 5f 52 45 53  h just PAGER_RES
11458 45 52 56 45 44 2e 20 20 41 66 74 65 72 20 61 6e  ERVED.  After an
11459 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
1145a 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 20 73 71  lback().** or sq
1145b 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
1145c 50 68 61 73 65 54 77 6f 28 29 2c 20 74 68 65 20  PhaseTwo(), the 
1145d 73 74 61 74 65 20 63 61 6e 20 67 6f 20 62 61 63  state can go bac
1145e 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45  k to PAGER_SHARE
1145f 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 61 6e 20  D,.** or it can 
11460 73 74 61 79 20 61 74 20 50 41 47 45 52 5f 45 58  stay at PAGER_EX
11461 43 4c 55 53 49 56 45 20 69 66 20 77 65 20 61 72  CLUSIVE if we ar
11462 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 61  e in exclusive a
11463 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a 2f 0a 23  ccess mode..*/.#
11464 64 65 66 69 6e 65 20 50 41 47 45 52 5f 55 4e 4c  define PAGER_UNL
11465 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 65 66 69  OCK      0.#defi
11466 6e 65 20 50 41 47 45 52 5f 53 48 41 52 45 44 20  ne PAGER_SHARED 
11467 20 20 20 20 20 31 20 20 20 2f 2a 20 73 61 6d 65       1   /* same
11468 20 61 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20   as SHARED_LOCK 
11469 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
1146a 5f 52 45 53 45 52 56 45 44 20 20 20 20 32 20 20  _RESERVED    2  
1146b 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 45 53 45   /* same as RESE
1146c 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65  RVED_LOCK */.#de
1146d 66 69 6e 65 20 50 41 47 45 52 5f 45 58 43 4c 55  fine PAGER_EXCLU
1146e 53 49 56 45 20 20 20 34 20 20 20 2f 2a 20 73 61  SIVE   4   /* sa
1146f 6d 65 20 61 73 20 45 58 43 4c 55 53 49 56 45 5f  me as EXCLUSIVE_
11470 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20  LOCK */.#define 
11471 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20  PAGER_SYNCED    
11472 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63    5../*.** A mac
11473 72 6f 20 75 73 65 64 20 66 6f 72 20 69 6e 76 6f  ro used for invo
11474 6b 69 6e 67 20 74 68 65 20 63 6f 64 65 63 20 69  king the codec i
11475 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a  f there is one.*
11476 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
11477 48 41 53 5f 43 4f 44 45 43 0a 23 20 64 65 66 69  HAS_CODEC.# defi
11478 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c  ne CODEC1(P,D,N,
11479 58 2c 45 29 20 5c 0a 20 20 20 20 69 66 28 20 50  X,E) \.    if( P
1147a 2d 3e 78 43 6f 64 65 63 20 26 26 20 50 2d 3e 78  ->xCodec && P->x
1147b 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c  Codec(P->pCodec,
1147c 44 2c 4e 2c 58 29 3d 3d 30 20 29 7b 20 45 3b 20  D,N,X)==0 ){ E; 
1147d 7d 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43  }.# define CODEC
1147e 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 5c  2(P,D,N,X,E,O) \
1147f 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64  .    if( P->xCod
11480 65 63 3d 3d 30 20 29 7b 20 4f 3d 28 63 68 61 72  ec==0 ){ O=(char
11481 2a 29 44 3b 20 7d 65 6c 73 65 20 5c 0a 20 20 20  *)D; }else \.   
11482 20 69 66 28 20 28 4f 3d 28 63 68 61 72 2a 29 28   if( (O=(char*)(
11483 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f  P->xCodec(P->pCo
11484 64 65 63 2c 44 2c 4e 2c 58 29 29 29 3d 3d 30 20  dec,D,N,X)))==0 
11485 29 7b 20 45 3b 20 7d 0a 23 65 6c 73 65 0a 23 20  ){ E; }.#else.# 
11486 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c  define CODEC1(P,
11487 44 2c 4e 2c 58 2c 45 29 20 20 20 2f 2a 20 4e 4f  D,N,X,E)   /* NO
11488 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20  -OP */.# define 
11489 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45  CODEC2(P,D,N,X,E
1148a 2c 4f 29 20 4f 3d 28 63 68 61 72 2a 29 44 0a 23  ,O) O=(char*)D.#
1148b 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
1148c 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   maximum allowed
1148d 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 36 34   sector size. 64
1148e 4b 69 42 2e 20 49 66 20 74 68 65 20 78 53 65 63  KiB. If the xSec
1148f 74 6f 72 73 69 7a 65 28 29 20 6d 65 74 68 6f 64  torsize() method
11490 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76   .** returns a v
11491 61 6c 75 65 20 6c 61 72 67 65 72 20 74 68 61 6e  alue larger than
11492 20 74 68 69 73 2c 20 74 68 65 6e 20 4d 41 58 5f   this, then MAX_
11493 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 73 20 75  SECTOR_SIZE is u
11494 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20  sed instead..** 
11495 54 68 69 73 20 63 6f 75 6c 64 20 63 6f 6e 63 65  This could conce
11496 69 76 61 62 6c 79 20 63 61 75 73 65 20 63 6f 72  ivably cause cor
11497 72 75 70 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e  ruption followin
11498 67 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  g a power failur
11499 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 20 61 20 73  e on.** such a s
1149a 79 73 74 65 6d 2e 20 54 68 69 73 20 69 73 20 63  ystem. This is c
1149b 75 72 72 65 6e 74 6c 79 20 61 6e 20 75 6e 64 6f  urrently an undo
1149c 63 75 6d 65 6e 74 65 64 20 6c 69 6d 69 74 2e 0a  cumented limit..
1149d 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 53  */.#define MAX_S
1149e 45 43 54 4f 52 5f 53 49 5a 45 20 30 78 31 30 30  ECTOR_SIZE 0x100
1149f 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  00../*.** An ins
114a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
114a1 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
114a2 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f   is allocated fo
114a3 72 20 65 61 63 68 20 61 63 74 69 76 65 0a 2a 2a  r each active.**
114a4 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 73   savepoint and s
114a5 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
114a6 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 79 73 74  tion in the syst
114a7 65 6d 2e 20 41 6c 6c 20 73 75 63 68 20 73 74 72  em. All such str
114a8 75 63 74 75 72 65 73 0a 2a 2a 20 61 72 65 20 73  uctures.** are s
114a9 74 6f 72 65 64 20 69 6e 20 74 68 65 20 50 61 67  tored in the Pag
114aa 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20  er.aSavepoint[] 
114ab 61 72 72 61 79 2c 20 77 68 69 63 68 20 69 73 20  array, which is 
114ac 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a  allocated and.**
114ad 20 72 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73   resized using s
114ae 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e  qlite3Realloc().
114af 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61  .**.** When a sa
114b0 76 65 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74  vepoint is creat
114b1 65 64 2c 20 74 68 65 20 50 61 67 65 72 53 61 76  ed, the PagerSav
114b2 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65  epoint.iHdrOffse
114b3 74 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65  t field is.** se
114b4 74 20 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75  t to 0. If a jou
114b5 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77  rnal-header is w
114b6 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
114b7 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69  main journal whi
114b8 6c 65 0a 2a 2a 20 74 68 65 20 73 61 76 65 70 6f  le.** the savepo
114b9 69 6e 74 20 69 73 20 61 63 74 69 76 65 2c 20 74  int is active, t
114ba 68 65 6e 20 69 48 64 72 4f 66 66 73 65 74 20 69  hen iHdrOffset i
114bb 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 79 74  s set to the byt
114bc 65 20 6f 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d  e offset .** imm
114bd 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
114be 6e 67 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 72  ng the last jour
114bf 6e 61 6c 20 72 65 63 6f 72 64 20 77 72 69 74 74  nal record writt
114c0 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e  en into the main
114c1 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f  .** journal befo
114c2 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68  re the journal-h
114c3 65 61 64 65 72 2e 20 54 68 69 73 20 69 73 20 72  eader. This is r
114c4 65 71 75 69 72 65 64 20 64 75 72 69 6e 67 20 73  equired during s
114c5 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c  avepoint.** roll
114c6 62 61 63 6b 20 28 73 65 65 20 70 61 67 65 72 50  back (see pagerP
114c7 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74  laybackSavepoint
114c8 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ())..*/.typedef 
114c9 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65  struct PagerSave
114ca 70 6f 69 6e 74 20 50 61 67 65 72 53 61 76 65 70  point PagerSavep
114cb 6f 69 6e 74 3b 0a 73 74 72 75 63 74 20 50 61 67  oint;.struct Pag
114cc 65 72 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20  erSavepoint {.  
114cd 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20  i64 iOffset;    
114ce 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
114cf 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20  Starting offset 
114d0 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  in main journal 
114d1 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66  */.  i64 iHdrOff
114d2 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  set;            
114d3 20 20 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a    /* See above *
114d4 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53  /.  Bitvec *pInS
114d5 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20  avepoint;       
114d6 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73   /* Set of pages
114d7 20 69 6e 20 74 68 69 73 20 73 61 76 65 70 6f 69   in this savepoi
114d8 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72  nt */.  Pgno nOr
114d9 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ig;             
114da 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c       /* Original
114db 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
114dc 20 69 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67   in file */.  Pg
114dd 6e 6f 20 69 53 75 62 52 65 63 3b 20 20 20 20 20  no iSubRec;     
114de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
114df 64 65 78 20 6f 66 20 66 69 72 73 74 20 72 65 63  dex of first rec
114e0 6f 72 64 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e  ord in sub-journ
114e1 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  al */.};../*.** 
114e2 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68  A open page cach
114e3 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
114e4 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
114e5 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
114e6 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a  ** errCode.**.**
114e7 20 20 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65     Pager.errCode
114e8 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 53   may be set to S
114e9 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c  QLITE_IOERR, SQL
114ea 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a  ITE_CORRUPT, or.
114eb 2a 2a 20 20 20 6f 72 20 53 51 4c 49 54 45 5f 46  **   or SQLITE_F
114ec 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66  ULL. Once one of
114ed 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65   the first three
114ee 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73 2c 20   errors occurs, 
114ef 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a 20 20  it persists.**  
114f0 20 61 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64   and is returned
114f1 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f   as the result o
114f2 66 20 65 76 65 72 79 20 6d 61 6a 6f 72 20 70 61  f every major pa
114f3 67 65 72 20 41 50 49 20 63 61 6c 6c 2e 20 20 54  ger API call.  T
114f4 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46  he.**   SQLITE_F
114f5 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64 65 20  ULL return code 
114f6 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66  is slightly diff
114f7 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73 69 73  erent. It persis
114f8 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68  ts only until th
114f9 65 0a 2a 2a 20 20 20 6e 65 78 74 20 73 75 63 63  e.**   next succ
114fa 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20  essful rollback 
114fb 69 73 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20  is performed on 
114fc 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e  the pager cache.
114fd 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49   Also,.**   SQLI
114fe 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74  TE_FULL does not
114ff 20 61 66 66 65 63 74 20 74 68 65 20 73 71 6c 69   affect the sqli
11500 74 65 33 50 61 67 65 72 47 65 74 28 29 20 61 6e  te3PagerGet() an
11501 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f  d sqlite3PagerLo
11502 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 41 50 49 73  okup().**   APIs
11503 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69 6c 6c  , they may still
11504 20 62 65 20 75 73 65 64 20 73 75 63 63 65 73 73   be used success
11505 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53  fully..**.** dbS
11506 69 7a 65 56 61 6c 69 64 2c 20 64 62 53 69 7a 65  izeValid, dbSize
11507 2c 20 64 62 4f 72 69 67 53 69 7a 65 2c 20 64 62  , dbOrigSize, db
11508 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20  FileSize.**.**  
11509 20 4d 61 6e 61 67 69 6e 67 20 74 68 65 20 73 69   Managing the si
1150a 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
1150b 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73  se file in pages
1150c 20 69 73 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d   is a little com
1150d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54  plicated..**   T
1150e 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 67 65  he variable Page
1150f 72 2e 64 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e  r.dbSize contain
11510 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
11511 70 61 67 65 73 20 74 68 61 74 20 74 68 65 20 64  pages that the d
11512 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61  atabase.**   ima
11513 67 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  ge currently con
11514 74 61 69 6e 73 2e 20 41 73 20 74 68 65 20 64 61  tains. As the da
11515 74 61 62 61 73 65 20 69 6d 61 67 65 20 67 72 6f  tabase image gro
11516 77 73 20 6f 72 20 73 68 72 69 6e 6b 73 20 74 68  ws or shrinks th
11517 69 73 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65  is.**   variable
11518 20 69 73 20 75 70 64 61 74 65 64 2e 20 54 68 65   is updated. The
11519 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e   variable Pager.
1151a 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61  dbFileSize conta
1151b 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a  ins the number.*
1151c 2a 20 20 20 6f 66 20 70 61 67 65 73 20 69 6e 20  *   of pages in 
1151d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1151e 65 2e 20 54 68 69 73 20 6d 61 79 20 62 65 20 64  e. This may be d
1151f 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 50 61  ifferent from Pa
11520 67 65 72 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20  ger.dbSize.**   
11521 69 66 20 73 6f 6d 65 20 70 61 67 65 73 20 68 61  if some pages ha
11522 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64 65 64  ve been appended
11523 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
11524 20 69 6d 61 67 65 20 62 75 74 20 6e 6f 74 20 79   image but not y
11525 65 74 20 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20  et written.**   
11526 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63 61 63  out from the cac
11527 68 65 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c  he to the actual
11528 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f   file on disk. O
11529 72 20 69 66 20 74 68 65 20 69 6d 61 67 65 20 68  r if the image h
1152a 61 73 20 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75  as been.**   tru
1152b 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69 6e 63  ncated by an inc
1152c 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20  remental-vacuum 
1152d 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 50  operation. The P
1152e 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a 65 20  ager.dbOrigSize 
1152f 76 61 72 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f  variable.**   co
11530 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65  ntains the numbe
11531 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
11532 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  e database image
11533 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e   when the curren
11534 74 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69  t.**   transacti
11535 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 54  on was opened. T
11536 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61  he contents of a
11537 6c 6c 20 74 68 72 65 65 20 6f 66 20 74 68 65 73  ll three of thes
11538 65 20 76 61 72 69 61 62 6c 65 73 20 69 73 0a 2a  e variables is.*
11539 2a 20 20 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74  *   only guarant
1153a 65 65 64 20 74 6f 20 62 65 20 63 6f 72 72 65 63  eed to be correc
1153b 74 20 69 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e  t if the boolean
1153c 20 50 61 67 65 72 2e 64 62 53 69 7a 65 56 61 6c   Pager.dbSizeVal
1153d 69 64 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a  id is true..**.*
1153e 2a 20 20 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20  *   TODO: Under 
1153f 77 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20  what conditions 
11540 69 73 20 64 62 53 69 7a 65 56 61 6c 69 64 20 73  is dbSizeValid s
11541 65 74 3f 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a  et? Cleared?.**.
11542 2a 2a 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  ** changeCountDo
11543 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20  ne.**.**   This 
11544 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65  boolean variable
11545 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65   is used to make
11546 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 63   sure that the c
11547 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a  hange-counter .*
11548 2a 20 20 20 28 74 68 65 20 34 2d 62 79 74 65 20  *   (the 4-byte 
11549 68 65 61 64 65 72 20 66 69 65 6c 64 20 61 74 20  header field at 
1154a 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 6f  byte offset 24 o
1154b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
1154c 69 6c 65 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f  ile) is .**   no
1154d 74 20 75 70 64 61 74 65 64 20 6d 6f 72 65 20 6f  t updated more o
1154e 66 74 65 6e 20 74 68 61 6e 20 6e 65 63 65 73 73  ften than necess
1154f 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74  ary. .**.**   It
11550 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20   is set to true 
11551 77 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d  when the change-
11552 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 69 73  counter field is
11553 20 75 70 64 61 74 65 64 2c 20 77 68 69 63 68 20   updated, which 
11554 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68  .**   can only h
11555 61 70 70 65 6e 20 69 66 20 61 6e 20 65 78 63 6c  appen if an excl
11556 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 68 65  usive lock is he
11557 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ld on the databa
11558 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74  se file..**   It
11559 20 69 73 20 63 6c 65 61 72 65 64 20 28 73 65 74   is cleared (set
1155a 20 74 6f 20 66 61 6c 73 65 29 20 77 68 65 6e 65   to false) whene
1155b 76 65 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ver an exclusive
1155c 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72   lock is .**   r
1155d 65 6c 69 6e 71 75 69 73 68 65 64 20 6f 6e 20 74  elinquished on t
1155e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1155f 2e 20 45 61 63 68 20 74 69 6d 65 20 61 20 74 72  . Each time a tr
11560 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
11561 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65  mitted,.**   The
11562 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65   changeCountDone
11563 20 66 6c 61 67 20 69 73 20 69 6e 73 70 65 63 74   flag is inspect
11564 65 64 2e 20 49 66 20 69 74 20 69 73 20 74 72 75  ed. If it is tru
11565 65 2c 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a  e, the work of.*
11566 2a 20 20 20 75 70 64 61 74 69 6e 67 20 74 68 65  *   updating the
11567 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
11568 69 73 20 6f 6d 69 74 74 65 64 20 66 6f 72 20 74  is omitted for t
11569 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
1156a 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  action..**.**   
1156b 54 68 69 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d  This mechanism m
1156c 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20 72  eans that when r
1156d 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73  unning in exclus
1156e 69 76 65 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e  ive mode, a conn
1156f 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65  ection .**   nee
11570 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 20 74 68  d only update th
11571 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
11572 20 6f 6e 63 65 2c 20 66 6f 72 20 74 68 65 20 66   once, for the f
11573 69 72 73 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  irst transaction
11574 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e  .**   committed.
11575 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65  .**.** dbModifie
11576 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62  d.**.**   The db
11577 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69 73  Modified flag is
11578 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 61 20   set whenever a 
11579 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
1157a 20 64 69 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49   dirtied..**   I
1157b 74 20 69 73 20 63 6c 65 61 72 65 64 20 61 74 20  t is cleared at 
1157c 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20  the end of each 
1157d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
1157e 2a 2a 20 20 20 49 74 20 69 73 20 75 73 65 64 20  **   It is used 
1157f 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20  when committing 
11580 6f 72 20 6f 74 68 65 72 77 69 73 65 20 65 6e 64  or otherwise end
11581 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ing a transactio
11582 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 68 65 20 64  n. If.**   the d
11583 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69  bModified flag i
11584 73 20 63 6c 65 61 72 20 74 68 65 6e 20 6c 65 73  s clear then les
11585 73 20 77 6f 72 6b 20 68 61 73 20 74 6f 20 62 65  s work has to be
11586 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75   done..**.** jou
11587 72 6e 61 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a  rnalStarted.**.*
11588 2a 20 20 20 54 68 69 73 20 66 6c 61 67 20 69 73  *   This flag is
11589 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 74 68   set whenever th
1158a 65 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  e the main journ
1158b 61 6c 20 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a  al is synced. .*
1158c 2a 0a 2a 2a 20 20 20 54 68 65 20 70 6f 69 6e 74  *.**   The point
1158d 20 6f 66 20 74 68 69 73 20 66 6c 61 67 20 69 73   of this flag is
1158e 20 74 68 61 74 20 69 74 20 6d 75 73 74 20 62 65   that it must be
1158f 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20 0a   set after the .
11590 2a 2a 20 20 20 66 69 72 73 74 20 6a 6f 75 72 6e  **   first journ
11591 61 6c 20 68 65 61 64 65 72 20 69 6e 20 61 20 6a  al header in a j
11592 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20  ournal file has 
11593 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64  been synced to d
11594 69 73 6b 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20  isk..**   After 
11595 74 68 69 73 20 68 61 73 20 68 61 70 70 65 6e 65  this has happene
11596 64 2c 20 6e 65 77 20 70 61 67 65 73 20 61 70 70  d, new pages app
11597 65 6e 64 65 64 20 74 6f 20 74 68 65 20 64 61 74  ended to the dat
11598 61 62 61 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e  abase .**   do n
11599 6f 74 20 6e 65 65 64 20 74 68 65 20 50 47 48 44  ot need the PGHD
1159a 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
1159b 20 73 65 74 2c 20 61 73 20 74 68 65 79 20 64 6f   set, as they do
1159c 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74   not need.**   t
1159d 6f 20 77 61 69 74 20 66 6f 72 20 61 20 6a 6f 75  o wait for a jou
1159e 72 6e 61 6c 20 73 79 6e 63 20 62 65 66 6f 72 65  rnal sync before
1159f 20 74 68 65 79 20 63 61 6e 20 62 65 20 77 72 69   they can be wri
115a0 74 74 65 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20  tten out to.**  
115a1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
115a2 6c 65 20 28 73 65 65 20 66 75 6e 63 74 69 6f 6e  le (see function
115a3 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 29 2e   pager_write()).
115a4 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73  .**   .** setMas
115a5 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73  ter.**.**   This
115a6 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
115a7 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  d to ensure that
115a8 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
115a9 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a  nal file name.**
115aa 20 20 20 28 69 66 20 61 6e 79 29 20 69 73 20 6f     (if any) is o
115ab 6e 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  nly written into
115ac 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
115ad 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  e once..**.**   
115ae 57 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20  When committing 
115af 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  a transaction, t
115b0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
115b1 6c 20 66 69 6c 65 20 6e 61 6d 65 20 28 69 66 20  l file name (if 
115b2 61 6e 79 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65  any).**   may be
115b3 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
115b4 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77  e journal file w
115b5 68 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 69  hile the pager i
115b6 73 20 73 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20  s still in.**   
115b7 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73  PAGER_RESERVED s
115b8 74 61 74 65 20 28 73 65 65 20 43 6f 6d 6d 69 74  tate (see Commit
115b9 50 68 61 73 65 4f 6e 65 28 29 20 66 6f 72 20 74  PhaseOne() for t
115ba 68 65 20 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a  he action). It.*
115bb 2a 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74  *   then attempt
115bc 73 20 74 6f 20 75 70 67 72 61 64 65 20 74 6f 20  s to upgrade to 
115bd 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
115be 6b 2e 20 49 66 20 74 68 69 73 20 61 74 74 65 6d  k. If this attem
115bf 70 74 0a 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74  pt.**   fails, t
115c0 68 65 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20  hen SQLITE_BUSY 
115c1 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 64 20  may be returned 
115c2 74 6f 20 74 68 65 20 75 73 65 72 20 61 6e 64 20  to the user and 
115c3 74 68 65 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61  the user.**   ma
115c4 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d  y attempt to com
115c5 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74  mit the transact
115c6 69 6f 6e 20 61 67 61 69 6e 20 6c 61 74 65 72 20  ion again later 
115c7 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f  (calling.**   Co
115c8 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 61  mmitPhaseOne() a
115c9 67 61 69 6e 29 2e 20 54 68 69 73 20 66 6c 61 67  gain). This flag
115ca 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75   is used to ensu
115cb 72 65 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20  re that the .** 
115cc 20 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c    master journal
115cd 20 6e 61 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72   name is only wr
115ce 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
115cf 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 66 69  rnal file the fi
115d0 72 73 74 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f  rst.**   time Co
115d1 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 69  mmitPhaseOne() i
115d2 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  s called..**.** 
115d3 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20  doNotSync.**.** 
115d4 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20    This variable 
115d5 69 73 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72  is set and clear
115d6 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 61 67  ed by sqlite3Pag
115d7 65 72 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a  erWrite()..**.**
115d8 20 6e 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20   needSync.**.** 
115d9 20 20 54 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74    TODO: It might
115da 20 62 65 20 65 61 73 69 65 72 20 74 6f 20 73 65   be easier to se
115db 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  t this variable 
115dc 69 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48  in writeJournalH
115dd 64 72 28 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72  dr().**   and wr
115de 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  iteMasterJournal
115df 28 29 20 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20  () only. Change 
115e0 69 74 73 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22  its meaning to "
115e1 75 6e 73 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a  unsynced data.**
115e2 20 20 20 68 61 73 20 62 65 65 6e 20 77 72 69 74     has been writ
115e3 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
115e4 61 6c 22 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49  al"..**.** subjI
115e5 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20  nMemory.**.**   
115e6 54 68 69 73 20 69 73 20 61 20 62 6f 6f 6c 65 61  This is a boolea
115e7 6e 20 76 61 72 69 61 62 6c 65 2e 20 49 66 20 74  n variable. If t
115e8 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 72 65  rue, then any re
115e9 71 75 69 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e  quired sub-journ
115ea 61 6c 0a 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65  al.**   is opene
115eb 64 20 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  d as an in-memor
115ec 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  y journal file. 
115ed 49 66 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 69  If false, then i
115ee 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75  n-memory.**   su
115ef 62 2d 6a 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f  b-journals are o
115f0 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 69 6e 2d  nly used for in-
115f1 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 66 69 6c  memory pager fil
115f2 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61  es..*/.struct Pa
115f3 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ger {.  sqlite3_
115f4 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20  vfs *pVfs;      
115f5 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69      /* OS functi
115f6 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72 20 49  ons to use for I
115f7 4f 20 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75 73  O */.  u8 exclus
115f8 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20  iveMode;        
115f9 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54     /* Boolean. T
115fa 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d  rue if locking_m
115fb 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a  ode==EXCLUSIVE *
115fc 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f  /.  u8 journalMo
115fd 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  de;             
115fe 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20 50 41 47  /* On of the PAG
115ff 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a  ER_JOURNALMODE_*
11600 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20   values */.  u8 
11601 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20  useJournal;     
11602 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20           /* Use 
11603 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
11604 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  al on this file 
11605 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f  */.  u8 noReadlo
11606 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ck;             
11607 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65   /* Do not bothe
11608 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64  r to obtain read
11609 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f  locks */.  u8 no
1160a 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Sync;           
1160b 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74         /* Do not
1160c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61   sync the journa
1160d 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75  l if true */.  u
1160e 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20  8 fullSync;     
1160f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
11610 20 65 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20   extra syncs of 
11611 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20  the journal for 
11612 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20  robustness */.  
11613 75 38 20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20  u8 sync_flags;  
11614 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
11615 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41  ne of SYNC_NORMA
11616 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a  L or SYNC_FULL *
11617 2f 0a 20 20 75 38 20 74 65 6d 70 46 69 6c 65 3b  /.  u8 tempFile;
11618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11619 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  /* zFilename is 
1161a 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
1161b 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c   */.  u8 readOnl
1161c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
1161d 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
1161e 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61  read-only databa
1161f 73 65 20 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62  se */.  u8 memDb
11620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11621 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 69      /* True to i
11622 6e 68 69 62 69 74 20 61 6c 6c 20 66 69 6c 65 20  nhibit all file 
11623 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  I/O */..  /* The
11624 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
11625 20 63 6f 6e 74 61 69 6e 73 20 74 68 6f 73 65 20   contains those 
11626 63 6c 61 73 73 20 6d 65 6d 62 65 72 73 20 74 68  class members th
11627 61 74 20 61 72 65 20 64 79 6e 61 6d 69 63 61 6c  at are dynamical
11628 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64  ly.  ** modified
11629 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f   during normal o
1162a 70 65 72 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f  perations. The o
1162b 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 20 69  ther variables i
1162c 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
1162d 0a 20 20 2a 2a 20 61 72 65 20 65 69 74 68 65 72  .  ** are either
1162e 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67   constant throug
1162f 68 6f 75 74 20 74 68 65 20 6c 69 66 65 74 69 6d  hout the lifetim
11630 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 2c 20  e of the pager, 
11631 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65  or else.  ** use
11632 64 20 74 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69  d to store confi
11633 67 75 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74  guration paramet
11634 65 72 73 20 74 68 61 74 20 61 66 66 65 63 74 20  ers that affect 
11635 74 68 65 20 77 61 79 20 74 68 65 20 70 61 67 65  the way the page
11636 72 20 0a 20 20 2a 2a 20 6f 70 65 72 61 74 65 73  r .  ** operates
11637 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
11638 27 73 74 61 74 65 27 20 76 61 72 69 61 62 6c 65  'state' variable
11639 20 69 73 20 64 65 73 63 72 69 62 65 64 20 69 6e   is described in
1163a 20 6d 6f 72 65 20 64 65 74 61 69 6c 20 61 6c 6f   more detail alo
1163b 6e 67 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a  ng with the.  **
1163c 20 64 65 73 63 72 69 70 74 69 6f 6e 73 20 6f 66   descriptions of
1163d 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 6d   the values it m
1163e 61 79 20 74 61 6b 65 20 2d 20 50 41 47 45 52 5f  ay take - PAGER_
1163f 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79  UNLOCK etc. Many
11640 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68   of the.  ** oth
11641 65 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20  er variables in 
11642 74 68 69 73 20 62 6c 6f 63 6b 20 61 72 65 20 64  this block are d
11643 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20  escribed in the 
11644 63 6f 6d 6d 65 6e 74 20 64 69 72 65 63 74 6c 79  comment directly
11645 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20 74 68 69   .  ** above thi
11646 73 20 63 6c 61 73 73 20 64 65 66 69 6e 69 74 69  s class definiti
11647 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74  on..  */.  u8 st
11648 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ate;            
11649 20 20 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f         /* PAGER_
1164a 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c  UNLOCK, _SHARED,
1164b 20 5f 52 45 53 45 52 56 45 44 2c 20 65 74 63 2e   _RESERVED, etc.
1164c 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f 64 69 66   */.  u8 dbModif
1164d 69 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ied;            
1164e 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
1164f 72 65 20 61 72 65 20 61 6e 79 20 63 68 61 6e 67  re are any chang
11650 65 73 20 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a  es to the Db */.
11651 20 20 75 38 20 6e 65 65 64 53 79 6e 63 3b 20 20    u8 needSync;  
11652 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11653 20 54 72 75 65 20 69 66 20 61 6e 20 66 73 79 6e   True if an fsyn
11654 63 28 29 20 69 73 20 6e 65 65 64 65 64 20 6f 6e  c() is needed on
11655 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   the journal */.
11656 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72    u8 journalStar
11657 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ted;          /*
11658 20 54 72 75 65 20 69 66 20 68 65 61 64 65 72 20   True if header 
11659 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79  of journal is sy
1165a 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 63 68 61  nced */.  u8 cha
1165b 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20  ngeCountDone;   
1165c 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74        /* Set aft
1165d 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  er incrementing 
1165e 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
1165f 65 72 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61  er */.  u8 setMa
11660 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  ster;           
11661 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
11662 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65   m-j name has be
11663 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72  en written to jr
11664 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74  nl */.  u8 doNot
11665 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Sync;           
11666 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20      /* Boolean. 
11667 57 68 69 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e  While true, do n
11668 6f 74 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63  ot spill the cac
11669 68 65 20 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a  he */.  u8 dbSiz
1166a 65 56 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20  eValid;         
1166b 20 20 20 20 2f 2a 20 53 65 74 20 77 68 65 6e 20      /* Set when 
1166c 64 62 53 69 7a 65 20 69 73 20 63 6f 72 72 65 63  dbSize is correc
1166d 74 20 2a 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e  t */.  u8 subjIn
1166e 4d 65 6d 6f 72 79 3b 20 20 20 20 20 20 20 20 20  Memory;         
1166f 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 73     /* True to us
11670 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d  e in-memory sub-
11671 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67  journals */.  Pg
11672 6e 6f 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20  no dbSize;      
11673 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
11674 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
11675 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
11676 20 20 50 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a    Pgno dbOrigSiz
11677 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
11678 20 64 62 53 69 7a 65 20 62 65 66 6f 72 65 20 74   dbSize before t
11679 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
1167a 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f  action */.  Pgno
1167b 20 64 62 46 69 6c 65 53 69 7a 65 3b 20 20 20 20   dbFileSize;    
1167c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1167d 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
1167e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1167f 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65  */.  int errCode
11680 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11681 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72   /* One of sever
11682 61 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f  al kinds of erro
11683 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63  rs */.  int nRec
11684 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11685 20 20 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75      /* Pages jou
11686 72 6e 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61  rnalled since la
11687 73 74 20 6a 2d 68 65 61 64 65 72 20 77 72 69 74  st j-header writ
11688 74 65 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73  ten */.  u32 cks
11689 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  umInit;         
1168a 20 20 20 20 20 2f 2a 20 51 75 61 73 69 2d 72 61       /* Quasi-ra
1168b 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 64 65 64  ndom value added
1168c 20 74 6f 20 65 76 65 72 79 20 63 68 65 63 6b 73   to every checks
1168d 75 6d 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62  um */.  u32 nSub
1168e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
1168f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
11690 20 72 65 63 6f 72 64 73 20 77 72 69 74 74 65 6e   records written
11691 20 74 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20   to sub-journal 
11692 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e  */.  Bitvec *pIn
11693 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20  Journal;        
11694 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20   /* One bit for 
11695 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65  each page in the
11696 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
11697 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
11698 20 2a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20   *fd;           
11699 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74  /* File descript
1169a 6f 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  or for database 
1169b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
1169c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20  e *jfd;         
1169d 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70   /* File descrip
1169e 74 6f 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75  tor for main jou
1169f 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rnal */.  sqlite
116a0 33 5f 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20  3_file *sjfd;   
116a1 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65        /* File de
116a2 73 63 72 69 70 74 6f 72 20 66 6f 72 20 73 75 62  scriptor for sub
116a3 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36  -journal */.  i6
116a4 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20  4 journalOff;   
116a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
116a6 72 65 6e 74 20 77 72 69 74 65 20 6f 66 66 73 65  rent write offse
116a7 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t in the journal
116a8 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a   file */.  i64 j
116a9 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 20 20  ournalHdr;      
116aa 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f         /* Byte o
116ab 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 6f 75  ffset to previou
116ac 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  s journal header
116ad 20 2a 2f 0a 20 20 50 61 67 65 72 53 61 76 65 70   */.  PagerSavep
116ae 6f 69 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e 74  oint *aSavepoint
116af 3b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 63  ; /* Array of ac
116b0 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20  tive savepoints 
116b1 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f  */.  int nSavepo
116b2 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
116b3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
116b4 65 6d 65 6e 74 73 20 69 6e 20 61 53 61 76 65 70  ements in aSavep
116b5 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72  oint[] */.  char
116b6 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b   dbFileVers[16];
116b7 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67          /* Chang
116b8 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61  es whenever data
116b9 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65  base file change
116ba 73 20 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f  s */.  u32 secto
116bb 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  rSize;          
116bc 20 20 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65     /* Assumed se
116bd 63 74 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67  ctor size during
116be 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20   rollback */..  
116bf 75 31 36 20 6e 45 78 74 72 61 3b 20 20 20 20 20  u16 nExtra;     
116c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
116c1 64 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74  dd this many byt
116c2 65 73 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65  es to each in-me
116c3 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69  mory page */.  i
116c4 31 36 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20  16 nReserve;    
116c5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
116c6 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62  mber of unused b
116c7 79 74 65 73 20 61 74 20 65 6e 64 20 6f 66 20 65  ytes at end of e
116c8 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 33  ach page */.  u3
116c9 32 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20  2 vfsFlags;     
116ca 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
116cb 67 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76  gs for sqlite3_v
116cc 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20  fs.xOpen() */.  
116cd 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20  int pageSize;   
116ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
116cf 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
116d0 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67  n a page */.  Pg
116d1 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20  no mxPgno;      
116d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
116d3 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a  imum allowed siz
116d4 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
116d5 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69  e */.  char *zFi
116d6 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  lename;         
116d7 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
116d8 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
116d9 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72  */.  char *zJour
116da 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  nal;            
116db 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
116dc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
116dd 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e    int (*xBusyHan
116de 64 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a  dler)(void*); /*
116df 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c   Function to cal
116e0 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20  l when busy */. 
116e1 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64   void *pBusyHand
116e2 6c 65 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20  lerArg;      /* 
116e3 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74  Context argument
116e4 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65   for xBusyHandle
116e5 72 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  r */.#ifdef SQLI
116e6 54 45 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48  TE_TEST.  int nH
116e7 69 74 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20  it, nMiss;      
116e8 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68        /* Cache h
116e9 69 74 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20  its and missing 
116ea 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20  */.  int nRead, 
116eb 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20  nWrite;         
116ec 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67   /* Database pag
116ed 65 73 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20  es read/written 
116ee 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64  */.#endif.  void
116ef 20 28 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62   (*xReiniter)(Db
116f0 50 61 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20  Page*); /* Call 
116f1 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
116f2 6e 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65  n reloading page
116f3 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  s */.#ifdef SQLI
116f4 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76  TE_HAS_CODEC.  v
116f5 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76  oid *(*xCodec)(v
116f6 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c  oid*,void*,Pgno,
116f7 69 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65  int); /* Routine
116f8 20 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67   for en/decoding
116f9 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20   data */.  void 
116fa 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67  (*xCodecSizeChng
116fb 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29  )(void*,int,int)
116fc 3b 20 2f 2a 20 4e 6f 74 69 66 79 20 6f 66 20 70  ; /* Notify of p
116fd 61 67 65 20 73 69 7a 65 20 63 68 61 6e 67 65 73  age size changes
116fe 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f   */.  void (*xCo
116ff 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 3b  decFree)(void*);
11700 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11701 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74  Destructor for t
11702 68 65 20 63 6f 64 65 63 20 2a 2f 0a 20 20 76 6f  he codec */.  vo
11703 69 64 20 2a 70 43 6f 64 65 63 3b 20 20 20 20 20  id *pCodec;     
11704 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
11705 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78  st argument to x
11706 43 6f 64 65 63 2e 2e 2e 20 6d 65 74 68 6f 64 73  Codec... methods
11707 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61   */.#endif.  cha
11708 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20  r *pTmpSpace;   
11709 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
1170a 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  r.pageSize bytes
1170b 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d   of space for tm
1170c 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 34 20 6a  p use */.  i64 j
1170d 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b  ournalSizeLimit;
1170e 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6c         /* Size l
1170f 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69 73 74  imit for persist
11710 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ent journal file
11711 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70  s */.  PCache *p
11712 50 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20  PCache;         
11713 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
11714 20 70 61 67 65 20 63 61 63 68 65 20 6f 62 6a 65   page cache obje
11715 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ct */.  sqlite3_
11716 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 3b  backup *pBackup;
11717 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
11718 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f 69 6e  o list of ongoin
11719 67 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73  g backup process
1171a 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  es */.};../*.** 
1171b 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c  The following gl
1171c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 68  obal variables h
1171d 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73 65  old counters use
1171e 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67  d for.** testing
1171f 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20   purposes only. 
11720 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65 73   These variables
11721 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 6e   do not exist in
11722 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e  .** a non-testin
11723 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65 20  g build.  These 
11724 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e 6f  variables are no
11725 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a  t thread-safe..*
11726 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
11727 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
11728 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65  int sqlite3_page
11729 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d  r_readdb_count =
1172a 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72   0;    /* Number
1172b 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 72   of full pages r
1172c 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a 53  ead from DB */.S
1172d 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1172e 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74  lite3_pager_writ
1172f 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20  edb_count = 0;  
11730 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75   /* Number of fu
11731 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65 6e  ll pages written
11732 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45   to DB */.SQLITE
11733 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
11734 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f  _pager_writej_co
11735 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e  unt = 0;    /* N
11736 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 77  umber of pages w
11737 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61  ritten to journa
11738 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 41  l */.# define PA
11739 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b  GER_INCR(v)  v++
1173a 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
1173b 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 65  PAGER_INCR(v).#e
1173c 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f  ndif..../*.** Jo
1173d 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 69  urnal files begi
1173e 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f  n with the follo
1173f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69 6e  wing magic strin
11740 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a 20  g.  The data.** 
11741 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  was obtained fro
11742 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20  m /dev/random.  
11743 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  It is used only 
11744 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65 63  as a sanity chec
11745 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76  k..**.** Since v
11746 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68  ersion 2.8.0, th
11747 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74  e journal format
11748 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 69   contains additi
11749 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63  onal sanity.** c
1174a 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74  hecking informat
1174b 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f 77  ion.  If the pow
1174c 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20 74  er fails while t
1174d 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65  he journal is be
1174e 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20  ing.** written, 
1174f 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62  semi-random garb
11750 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20 61  age data might a
11751 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f 75  ppear in the jou
11752 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74  rnal.** file aft
11753 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73 74  er power is rest
11754 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74 74  ored.  If an att
11755 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61 64  empt is then mad
11756 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65  e.** to roll the
11757 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74   journal back, t
11758 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75 6c  he database coul
11759 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20  d be corrupted. 
1175a 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a   The additional.
1175b 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  ** sanity checki
1175c 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61 74  ng data is an at
1175d 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 65  tempt to discove
1175e 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69 6e  r the garbage in
1175f 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
11760 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a  and ignore it..*
11761 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 20  *.** The sanity 
11762 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61  checking informa
11763 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 77  tion for the new
11764 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20   journal format 
11765 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61  consists.** of a
11766 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75 6d   32-bit checksum
11767 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66   on each page of
11768 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65 63   data.  The chec
11769 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 68  ksum covers both
1176a 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d  .** the page num
1176b 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61 67  ber and the pPag
1176c 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74  er->pageSize byt
1176d 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20 74  es of data for t
1176e 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73  he page..** This
1176f 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 61   cksum is initia
11770 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 69  lized to a 32-bi
11771 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74  t random value t
11772 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20 74  hat appears in t
11773 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  he.** journal fi
11774 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20 74  le right after t
11775 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 20  he header.  The 
11776 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a  random initializ
11777 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c  er is important,
11778 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72 62  .** because garb
11779 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61 70  age data that ap
1177a 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e 64  pears at the end
1177b 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73   of a journal is
1177c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20   likely.** data 
1177d 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69 6e  that was once in
1177e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68 61   other files tha
1177f 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e 20  t have now been 
11780 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68 65  deleted.  If the
11781 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74 61  .** garbage data
11782 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62   came from an ob
11783 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66  solete journal f
11784 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73 75  ile, the checksu
11785 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63  ms might.** be c
11786 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79 20  orrect.  But by 
11787 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
11788 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e   checksum to ran
11789 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68 0a  dom value which.
1178a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74 20  ** is different 
1178b 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e 61  for every journa
1178c 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74  l, we minimize t
1178d 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61  hat risk..*/.sta
1178e 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
1178f 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c  ed char aJournal
11790 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78  Magic[] = {.  0x
11791 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c 20  d9, 0xd5, 0x05, 
11792 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61 31  0xf9, 0x20, 0xa1
11793 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b  , 0x63, 0xd7,.};
11794 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65  ../*.** The size
11795 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 68 20   of the of each 
11796 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e 20 74  page record in t
11797 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67 69  he journal is gi
11798 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 6f  ven by.** the fo
11799 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a 2a  llowing macro..*
1179a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41  /.#define JOURNA
1179b 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 20  L_PG_SZ(pPager) 
1179c 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53   ((pPager->pageS
1179d 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a  ize) + 8)../*.**
1179e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   The journal hea
1179f 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69  der size for thi
117a0 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 69 73  s pager. This is
117a1 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 61 6d   usually the sam
117a2 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61 20  e .** size as a 
117a3 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74  single disk sect
117a4 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 65 74  or. See also set
117a5 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a 2f  SectorSize()..*/
117a6 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c  .#define JOURNAL
117a7 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
117a8 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53  (pPager->sectorS
117a9 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ize)../*.** The 
117aa 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 74  macro MEMDB is t
117ab 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64 65  rue if we are de
117ac 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69 6e  aling with an in
117ad 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
117ae 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 20  ..** We do this 
117af 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68  as a macro so th
117b0 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54 45  at if the SQLITE
117b1 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d  _OMIT_MEMORYDB m
117b2 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20  acro is set,.** 
117b3 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d  the value of MEM
117b4 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e  DB will be a con
117b5 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63 6f  stant and the co
117b6 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69  mpiler will opti
117b7 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65  mize.** out code
117b8 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 65   that would neve
117b9 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69  r execute..*/.#i
117ba 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
117bb 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 69  _MEMORYDB.# defi
117bc 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65  ne MEMDB 0.#else
117bd 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20  .# define MEMDB 
117be 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65  pPager->memDb.#e
117bf 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
117c0 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61  maximum legal pa
117c1 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e  ge number is (2^
117c2 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66  31 - 1)..*/.#def
117c3 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47  ine PAGER_MAX_PG
117c4 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a 23  NO 2147483647..#
117c5 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 0a 2f  ifndef NDEBUG ./
117c6 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a  *.** Usage:.**.*
117c7 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 73 65  *   assert( asse
117c8 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70  rt_pager_state(p
117c9 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 74 61  Pager) );.*/.sta
117ca 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 5f 70  tic int assert_p
117cb 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65 72  ager_state(Pager
117cc 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 2f 2a   *pPager){..  /*
117cd 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 73 20   A temp-file is 
117ce 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 52 5f  always in PAGER_
117cf 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 41 47  EXCLUSIVE or PAG
117d0 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 2e  ER_SYNCED state.
117d1 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
117d2 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d  ager->tempFile==
117d3 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61  0 || pPager->sta
117d4 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  te>=PAGER_EXCLUS
117d5 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  IVE );..  /* The
117d6 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65   changeCountDone
117d7 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20   flag is always 
117d8 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c  set for temp-fil
117d9 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  es */.  assert( 
117da 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
117db 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 63  ==0 || pPager->c
117dc 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 29  hangeCountDone )
117dd 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  ;..  return 1;.}
117de 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
117df 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74  eturn true if it
117e0 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f   is necessary to
117e1 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 50 67   write page *pPg
117e2 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f   into the sub-jo
117e3 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 65  urnal..** A page
117e4 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 69   needs to be wri
117e5 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 73 75  tten into the su
117e6 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68 65  b-journal if the
117e7 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a 2a 2a  re exists one.**
117e8 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 73 61   or more open sa
117e9 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 68 69  vepoints for whi
117ea 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68  ch:.**.**   * Th
117eb 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73  e page-number is
117ec 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
117ed 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 76 65  ual to PagerSave
117ee 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e 64  point.nOrig, and
117ef 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 74 20  .**   * The bit 
117f0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
117f1 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   the page-number
117f2 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e 0a 2a   is not set in.*
117f3 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70  *     PagerSavep
117f4 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e  oint.pInSavepoin
117f5 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
117f6 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
117f7 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  e(PgHdr *pPg){. 
117f8 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67   Pgno pgno = pPg
117f9 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20  ->pgno;.  Pager 
117fa 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
117fb 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 3b 0a  Pager;.  int i;.
117fc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
117fd 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
117fe 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72   i++){.    Pager
117ff 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26  Savepoint *p = &
11800 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69  pPager->aSavepoi
11801 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  nt[i];.    if( p
11802 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 26  ->nOrig>=pgno &&
11803 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 76 65   0==sqlite3Bitve
11804 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 76 65  cTest(p->pInSave
11805 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 7b 0a  point, pgno) ){.
11806 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
11807 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
11808 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn 0;.}../*.** R
11809 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
1180a 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64  e page is alread
1180b 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  y in the journal
1180c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1180d 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72 6e   int pageInJourn
1180e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  al(PgHdr *pPg){.
1180f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
11810 42 69 74 76 65 63 54 65 73 74 28 70 50 67 2d 3e  BitvecTest(pPg->
11811 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
11812 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  al, pPg->pgno);.
11813 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20  }../*.** Read a 
11814 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 66  32-bit integer f
11815 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 66 69  rom the given fi
11816 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  le descriptor.  
11817 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 65  Store the intege
11818 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 61  r.** that is rea
11819 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 65 74  d in *pRes.  Ret
1181a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
1181b 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b   everything work
1181c 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 72  ed, or an.** err
1181d 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 74  or code is somet
1181e 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
1181f 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 65  .**.** All value
11820 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20  s are stored on 
11821 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e 64 69  disk as big-endi
11822 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  an..*/.static in
11823 74 20 72 65 61 64 33 32 62 69 74 73 28 73 71 6c  t read32bits(sql
11824 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69  ite3_file *fd, i
11825 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 2a  64 offset, u32 *
11826 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65  pRes){.  unsigne
11827 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20  d char ac[4];.  
11828 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
11829 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20 73  OsRead(fd, ac, s
1182a 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 73 65  izeof(ac), offse
1182b 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
1182c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a  LITE_OK ){.    *
1182d 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 65  pRes = sqlite3Ge
1182e 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 7d 0a  t4byte(ac);.  }.
1182f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11830 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32  /*.** Write a 32
11831 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74  -bit integer int
11832 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65  o a string buffe
11833 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 6e 20  r in big-endian 
11834 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23  byte order..*/.#
11835 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 74 73  define put32bits
11836 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 50 75  (A,B)  sqlite3Pu
11837 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c 42 29  t4byte((u8*)A,B)
11838 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20  ../*.** Write a 
11839 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69  32-bit integer i
1183a 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 66 69  nto the given fi
1183b 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  le descriptor.  
1183c 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
1183d 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 6f  .** on success o
1183e 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  r an error code 
1183f 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  is something goe
11840 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74  s wrong..*/.stat
11841 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62 69  ic int write32bi
11842 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ts(sqlite3_file 
11843 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c  *fd, i64 offset,
11844 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 68 61   u32 val){.  cha
11845 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 33 32  r ac[4];.  put32
11846 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 0a 20  bits(ac, val);. 
11847 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f   return sqlite3O
11848 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 20 34  sWrite(fd, ac, 4
11849 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a  , offset);.}../*
1184a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74  .** The argument
1184b 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69   to this macro i
1184c 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  s a file descrip
1184d 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74 65  tor (type sqlite
1184e 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 74  3_file*)..** Ret
1184f 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 20 6e  urn 0 if it is n
11850 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e 2d  ot open, or non-
11851 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31 29  zero (but not 1)
11852 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a   if it is..**.**
11853 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74   This is so that
11854 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e   expressions can
11855 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a 0a   be written as:.
11856 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f 70  **.**   if( isOp
11857 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
11858 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73  ){ ....**.** ins
11859 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20  tead of.**.**   
1185a 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d  if( pPager->jfd-
1185b 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e  >pMethods ){ ...
1185c 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f 70  .*/.#define isOp
1185d 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d 3e  en(pFd) ((pFd)->
1185e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a  pMethods)../*.**
1185f 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 73 20   If file pFd is 
11860 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74  open, call sqlit
11861 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20  e3OsUnlock() on 
11862 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  it..*/.static in
11863 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  t osUnlock(sqlit
11864 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e  e3_file *pFd, in
11865 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20  t eLock){.  if( 
11866 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 7b 0a  !isOpen(pFd) ){.
11867 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
11868 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  E_OK;.  }.  retu
11869 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  rn sqlite3OsUnlo
1186a 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b 0a  ck(pFd, eLock);.
1186b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
1186c 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65  nction determine
1186d 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
1186e 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74   the atomic-writ
1186f 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a  e optimization.*
11870 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69  * can be used wi
11871 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 54  th this pager. T
11872 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
11873 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a 0a  can be used if:.
11874 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 76  **.**  (a) the v
11875 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
11876 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74   OsDeviceCharact
11877 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 63  eristics() indic
11878 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 20  ates that.**    
11879 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67    a database pag
1187a 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e  e may be written
1187b 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e 64   atomically, and
1187c 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 6c  .**  (b) the val
1187d 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f  ue returned by O
1187e 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 73  sSectorSize() is
1187f 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
11880 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74  ual.**      to t
11881 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a  he page size..**
11882 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 61  .** The optimiza
11883 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 6c 77  tion is also alw
11884 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f 72 20  ays enabled for 
11885 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e  temporary files.
11886 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 72 72   It is.** an err
11887 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  or to call this 
11888 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 61 67  function if pPag
11889 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20  er is opened on 
1188a 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20  an in-memory.** 
1188b 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
1188c 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74  If the optimizat
1188d 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 73  ion cannot be us
1188e 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65  ed, 0 is returne
1188f 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 20  d. If it can be 
11890 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  used,.** then th
11891 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
11892 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   is the size of 
11893 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11894 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74   when it.** cont
11895 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64 61  ains rollback da
11896 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20 6f  ta for exactly o
11897 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 64  ne page..*/.#ifd
11898 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
11899 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73 74  _ATOMIC_WRITE.st
1189a 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75 66  atic int jrnlBuf
1189b 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70  ferSize(Pager *p
1189c 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74  Pager){.  assert
1189d 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66  ( !MEMDB );.  if
1189e 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  ( !pPager->tempF
1189f 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 64  ile ){.    int d
118a0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
118a1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
118a2 44 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72  Device character
118a3 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 69 6e  istics */.    in
118a4 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20 20  t nSector;      
118a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118a6 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 2a  /* Sector size *
118a7 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 65  /.    int szPage
118a8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
118a9 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
118aa 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 73 73  size */..    ass
118ab 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
118ac 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 20 64  er->fd) );.    d
118ad 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76  c = sqlite3OsDev
118ae 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
118af 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a  cs(pPager->fd);.
118b0 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 70 50      nSector = pP
118b1 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
118b2 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d 20 70  ;.    szPage = p
118b3 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b  Pager->pageSize;
118b4 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c  ..    assert(SQL
118b5 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
118b6 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a  512==(512>>8));.
118b7 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54      assert(SQLIT
118b8 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34  E_IOCAP_ATOMIC64
118b9 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a  K==(65536>>8));.
118ba 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 26 28      if( 0==(dc&(
118bb 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
118bc 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 29 29  MIC|(szPage>>8))
118bd 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a 50 61   || nSector>szPa
118be 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ge) ){.      ret
118bf 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
118c0 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 52 4e  ..  return JOURN
118c1 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
118c2 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53  ) + JOURNAL_PG_S
118c3 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 65 6e  Z(pPager);.}.#en
118c4 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51  dif../*.** If SQ
118c5 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
118c6 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65 6e   is defined then
118c7 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 6e 69   we do some sani
118c8 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 6f  ty checking.** o
118c9 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e  n the cache usin
118ca 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 69 6f  g a hash functio
118cb 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  n.  This is used
118cc 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20   for testing.** 
118cd 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e  and debugging on
118ce 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ly..*/.#ifdef SQ
118cf 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
118d0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
118d1 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 20 74  32-bit hash of t
118d2 68 65 20 70 61 67 65 20 64 61 74 61 20 66 6f 72  he page data for
118d3 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69   pPage..*/.stati
118d4 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 74 61  c u32 pager_data
118d5 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 2c 20  hash(int nByte, 
118d6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
118d7 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 61 73  Data){.  u32 has
118d8 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a  h = 0;.  int i;.
118d9 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 79    for(i=0; i<nBy
118da 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 68 61  te; i++){.    ha
118db 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 39 29  sh = (hash*1039)
118dc 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 20 7d   + pData[i];.  }
118dd 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 3b 0a  .  return hash;.
118de 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67  }.static u32 pag
118df 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 48 64  er_pagehash(PgHd
118e0 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74  r *pPage){.  ret
118e1 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 68 61  urn pager_dataha
118e2 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 65 72  sh(pPage->pPager
118e3 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 6e 73  ->pageSize, (uns
118e4 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 50 61  igned char *)pPa
118e5 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a 73 74  ge->pData);.}.st
118e6 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f  atic void pager_
118e7 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 67 48  set_pagehash(PgH
118e8 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 70 50  dr *pPage){.  pP
118e9 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 3d 20  age->pageHash = 
118ea 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
118eb 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Page);.}../*.** 
118ec 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 20 6d  The CHECK_PAGE m
118ed 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 67 48  acro takes a PgH
118ee 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 6d 65  dr* as an argume
118ef 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f 43 48  nt. If SQLITE_CH
118f0 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 73 20  ECK_PAGES.** is 
118f1 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e 44 45  defined, and NDE
118f2 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  BUG is not defin
118f3 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 20  ed, an assert() 
118f4 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 6b 73  statement checks
118f5 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 61 67  .** that the pag
118f6 65 20 69 73 20 65 69 74 68 65 72 20 64 69 72 74  e is either dirt
118f7 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 63 68  y or still match
118f8 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 74 65  es the calculate
118f9 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a 2f 0a  d page-hash..*/.
118fa 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41  #define CHECK_PA
118fb 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 65 28  GE(x) checkPage(
118fc 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  x).static void c
118fd 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 20 2a  heckPage(PgHdr *
118fe 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  pPg){.  Pager *p
118ff 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
11900 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ger;.  assert( !
11901 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 7c 7c  pPg->pageHash ||
11902 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11903 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 2d 3e  .      || (pPg->
11904 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
11905 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65 48  Y) || pPg->pageH
11906 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65 68  ash==pager_pageh
11907 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a 23  ash(pPg) );.}..#
11908 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61 67  else.#define pag
11909 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59 29  er_datahash(X,Y)
1190a 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67 65    0.#define page
1190b 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20 30  r_pagehash(X)  0
1190c 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50  .#define CHECK_P
1190d 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20 2f  AGE(x).#endif  /
1190e 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  * SQLITE_CHECK_P
1190f 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57  AGES */../*.** W
11910 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c  hen this is call
11911 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ed the journal f
11912 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50  ile for pager pP
11913 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70 65  ager must be ope
11914 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  n..** This funct
11915 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
11916 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a 6f  read a master jo
11917 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
11918 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e 64  from the .** end
11919 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64   of the file and
1191a 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c  , if successful,
1191b 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f 20   copies it into 
1191c 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 20  memory supplied 
1191d 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 65  .** by the calle
1191e 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  r. See comments 
1191f 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74 65  above writeMaste
11920 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 74  rJournal() for t
11921 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 65  he format.** use
11922 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61 73  d to store a mas
11923 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
11924 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e 64   name at the end
11925 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   of a journal fi
11926 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65  le..**.** zMaste
11927 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20  r must point to 
11928 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c  a buffer of at l
11929 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74  east nMaster byt
1192a 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a  es allocated by.
1192b 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54  ** the caller. T
1192c 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71  his should be sq
1192d 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68  lite3_vfs.mxPath
1192e 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72  name+1 (to ensur
1192f 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e  e there is.** en
11930 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72  ough space to wr
11931 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ite the master j
11932 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66  ournal name). If
11933 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11934 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74  nal.** name in t
11935 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f  he journal is lo
11936 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65  nger than nMaste
11937 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69  r bytes (includi
11938 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d  ng a.** nul-term
11939 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68  inator), then th
1193a 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73  is is handled as
1193b 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f   if no master jo
1193c 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65  urnal name.** we
1193d 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68  re present in th
1193e 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  e journal..**.**
1193f 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   If a master jou
11940 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69  rnal file name i
11941 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 65  s present at the
11942 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72   end of the jour
11943 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65  nal.** file, the
11944 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69  n it is copied i
11945 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70  nto the buffer p
11946 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61  ointed to by zMa
11947 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d 74  ster. A.** nul-t
11948 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 69  erminator byte i
11949 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  s appended to th
1194a 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77 69  e buffer followi
1194b 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a  ng the master.**
1194c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
1194d 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20  me..**.** If it 
1194e 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68  is determined th
1194f 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  at no master jou
11950 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69  rnal file name i
11951 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a 4d  s present .** zM
11952 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74 20  aster[0] is set 
11953 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f  to 0 and SQLITE_
11954 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  OK returned..**.
11955 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
11956 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 61 64  ccurs while read
11957 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  ing from the jou
11958 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53 51  rnal file, an SQ
11959 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f  Lite.** error co
1195a 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
1195b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
1195c 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  adMasterJournal(
1195d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1195e 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74  rnl, char *zMast
1195f 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72 29  er, u32 nMaster)
11960 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
11961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11962 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
11963 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20 20  .  u32 len;     
11964 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11965 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73   Length in bytes
11966 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   of master journ
11967 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 34  al name */.  i64
11968 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20   szJ;           
11969 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
1196a 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
1196b 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70  f journal file p
1196c 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b  Jrnl */.  u32 ck
1196d 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  sum;            
1196e 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b       /* MJ check
1196f 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 66  sum value read f
11970 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20  rom journal */. 
11971 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20   u32 u;         
11972 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
11973 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75  nsigned loop cou
11974 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  nter */.  unsign
11975 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38  ed char aMagic[8
11976 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72  ];   /* A buffer
11977 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67   to hold the mag
11978 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 7a  ic header */.  z
11979 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27  Master[0] = '\0'
1197a 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  ;..  if( SQLITE_
1197b 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
1197c 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e  3OsFileSize(pJrn
1197d 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c 20  l, &szJ)).   || 
1197e 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51 4c  szJ<16.   || SQL
1197f 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
11980 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20  ad32bits(pJrnl, 
11981 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a 20  szJ-16, &len)). 
11982 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65    || len>=nMaste
11983 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  r .   || SQLITE_
11984 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
11985 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d  bits(pJrnl, szJ-
11986 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20 20  12, &cksum)).   
11987 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  || SQLITE_OK!=(r
11988 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
11989 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 2c  d(pJrnl, aMagic,
1198a 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20 7c   8, szJ-8)).   |
1198b 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c  | memcmp(aMagic,
1198c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
1198d 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  8).   || SQLITE_
1198e 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
1198f 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 7a  3OsRead(pJrnl, z
11990 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a 4a  Master, len, szJ
11991 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a 20  -16-len)).  ){. 
11992 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
11993 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74  }..  /* See if t
11994 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63  he checksum matc
11995 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a  hes the master j
11996 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20  ournal name */. 
11997 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b   for(u=0; u<len;
11998 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d   u++){.    cksum
11999 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a   -= zMaster[u];.
1199a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20    }.  if( cksum 
1199b 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ){.    /* If the
1199c 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27   checksum doesn'
1199d 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f  t add up, then o
1199e 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
1199f 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20  e disk sectors. 
119a0 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67     ** containing
119a1 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
119a2 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  nal filename is 
119a3 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20  corrupted. This 
119a4 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66  means.    ** def
119a5 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63  initely roll bac
119a6 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72  k, so just retur
119a7 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20  n SQLITE_OK and 
119a8 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20  report a (nul). 
119a9 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75     ** master-jou
119aa 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20  rnal filename.. 
119ab 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20     */.    len = 
119ac 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72  0;.  }.  zMaster
119ad 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  [len] = '\0';.  
119ae 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54   .  return SQLIT
119af 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
119b0 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 74  eturn the offset
119b1 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 62   of the sector b
119b2 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69 6d  oundary at or im
119b3 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66 6f  mediately .** fo
119b4 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c 75  llowing the valu
119b5 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75  e in pPager->jou
119b6 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69 6e  rnalOff, assumin
119b7 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20 73  g a sector .** s
119b8 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 73  ize of pPager->s
119b9 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73 2e  ectorSize bytes.
119ba 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61  .**.** i.e for a
119bb 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20   sector size of 
119bc 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67  512:.**.**   Pag
119bd 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 20  er.journalOff   
119be 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 76 61         Return va
119bf 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  lue.**   -------
119c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
119c1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
119c2 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20  .**   0         
119c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119c4 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20 20  0.**   512      
119c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119c6 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20 20   512.**   100   
119c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119c8 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30 30      512.**   200
119c9 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
119ca 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a         2048.** .
119cb 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a 6f  */.static i64 jo
119cc 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 50  urnalHdrOffset(P
119cd 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
119ce 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 3b   i64 offset = 0;
119cf 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 65  .  i64 c = pPage
119d0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20  r->journalOff;. 
119d1 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f 66   if( c ){.    of
119d2 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f  fset = ((c-1)/JO
119d3 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
119d4 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52  ger) + 1) * JOUR
119d5 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
119d6 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  r);.  }.  assert
119d7 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c  ( offset%JOURNAL
119d8 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d  _HDR_SZ(pPager)=
119d9 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
119da 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61  offset>=c );.  a
119db 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d 63  ssert( (offset-c
119dc 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  )<JOURNAL_HDR_SZ
119dd 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65  (pPager) );.  re
119de 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a  turn offset;.}..
119df 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  /*.** The journa
119e0 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f  l file must be o
119e1 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75  pen when this fu
119e2 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
119e3 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
119e4 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
119e5 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   if the journal 
119e6 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65  file has not bee
119e7 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20  n written to.** 
119e8 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 65  within the curre
119e9 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28  nt transaction (
119ea 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f  i.e. if Pager.jo
119eb 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a 2a  urnalOff==0)..**
119ec 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61 74  .** If doTruncat
119ed 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72  e is non-zero or
119ee 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e   the Pager.journ
119ef 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72 69  alSizeLimit vari
119f0 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20 74  able is.** set t
119f1 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63 61  o 0, then trunca
119f2 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  te the journal f
119f3 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74 65  ile to zero byte
119f4 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65 72  s in size. Other
119f5 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68  wise,.** zero th
119f6 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 65 72  e 28-byte header
119f7 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
119f8 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
119f9 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73  e. In either cas
119fa 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 61  e, .** if the pa
119fb 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e 6f  ger is not in no
119fc 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e 63  -sync mode, sync
119fd 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
119fe 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a  e immediately .*
119ff 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e 67 20  * after writing 
11a00 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69 74  or truncating it
11a01 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65 72  ..**.** If Pager
11a02 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  .journalSizeLimi
11a03 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 6f  t is set to a po
11a04 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 6f  sitive, non-zero
11a05 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 66   value, and.** f
11a06 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72 75  ollowing the tru
11a07 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f 69  ncation or zeroi
11a08 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 62 6f  ng described abo
11a09 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ve the size of t
11a0a 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  he .** journal f
11a0b 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73 20  ile in bytes is 
11a0c 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73  larger than this
11a0d 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72 75   value, then tru
11a0e 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75  ncate the.** jou
11a0f 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61 67  rnal file to Pag
11a10 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  er.journalSizeLi
11a11 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 20 6a  mit bytes. The j
11a12 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73  ournal file does
11a13 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  .** not need to 
11a14 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77  be synced follow
11a15 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 69  ing this operati
11a16 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  on..**.** If an 
11a17 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  IO error occurs,
11a18 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73   abandon process
11a19 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ing and return t
11a1a 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  he IO error code
11a1b 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  ..** Otherwise, 
11a1c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
11a1d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11a1e 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50  zeroJournalHdr(P
11a1f 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
11a20 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20  t doTruncate){. 
11a21 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
11a22 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
11a23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a24 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
11a25 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69  e */.  assert( i
11a26 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
11a27 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  d) );.  if( pPag
11a28 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29  er->journalOff )
11a29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20  {.    const i64 
11a2a 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 2d  iLimit = pPager-
11a2b 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  >journalSizeLimi
11a2c 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63  t;    /* Local c
11a2d 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a 0a  ache of jsl */..
11a2e 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 5a      IOTRACE(("JZ
11a2f 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 50  EROHDR %p\n", pP
11a30 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 64  ager)).    if( d
11a31 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c 69  oTruncate || iLi
11a32 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  mit==0 ){.      
11a33 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72  rc = sqlite3OsTr
11a34 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a  uncate(pPager->j
11a35 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73  fd, 0);.    }els
11a36 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  e{.      static 
11a37 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48  const char zeroH
11a38 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 20  dr[28] = {0};.  
11a39 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
11a3a 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
11a3b 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 69  jfd, zeroHdr, si
11a3c 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 30  zeof(zeroHdr), 0
11a3d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
11a3e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
11a3f 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  & !pPager->noSyn
11a40 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  c ){.      rc = 
11a41 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50  sqlite3OsSync(pP
11a42 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54  ager->jfd, SQLIT
11a43 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c  E_SYNC_DATAONLY|
11a44 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61  pPager->sync_fla
11a45 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  gs);.    }..    
11a46 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
11a47 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
11a48 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 75   is committed bu
11a49 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b  t the write lock
11a4a 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c   .    ** is stil
11a4b 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69  l held on the fi
11a4c 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  le. If there is 
11a4d 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f 6e  a size limit con
11a4e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20 20  figured for .   
11a4f 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65   ** the persiste
11a50 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74  nt journal and t
11a51 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11a52 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 6d  currently consum
11a53 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 73  es more.    ** s
11a54 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20 6c  pace than that l
11a55 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 2c  imit allows for,
11a56 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f 77   truncate it now
11a57 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  . There is no ne
11a58 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 6e  ed.    ** to syn
11a59 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c 6f  c the file follo
11a5a 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74  wing this operat
11a5b 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
11a5c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
11a5d 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 7b  K && iLimit>0 ){
11a5e 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a 20  .      i64 sz;. 
11a5f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
11a60 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67  3OsFileSize(pPag
11a61 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a 20  er->jfd, &sz);. 
11a62 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
11a63 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c 69  ITE_OK && sz>iLi
11a64 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  mit ){.        r
11a65 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
11a66 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  ncate(pPager->jf
11a67 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  d, iLimit);.    
11a68 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
11a69 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
11a6a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
11a6b 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65  file must be ope
11a6c 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  n when this rout
11a6d 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 41  ine is called. A
11a6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 64   journal.** head
11a6f 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  er (JOURNAL_HDR_
11a70 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72 69  SZ bytes) is wri
11a71 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f  tten into the jo
11a72 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 68  urnal file at th
11a73 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63  e.** current loc
11a74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
11a75 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 20   format for the 
11a76 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69  journal header i
11a77 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
11a78 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67 69   - 8 bytes: Magi
11a79 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a 6f  c identifying jo
11a7a 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a 2a  urnal format..**
11a7b 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d 62   - 4 bytes: Numb
11a7c 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e  er of records in
11a7d 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 20   journal, or -1 
11a7e 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 20  no-sync mode is 
11a7f 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73  on..** - 4 bytes
11a80 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  : Random number 
11a81 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68 61  used for page ha
11a82 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73  sh..** - 4 bytes
11a83 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62 61  : Initial databa
11a84 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a  se page count..*
11a85 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65 63  * - 4 bytes: Sec
11a86 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62 79  tor size used by
11a87 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61   the process tha
11a88 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f 75  t wrote this jou
11a89 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 74  rnal..** - 4 byt
11a8a 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61 67  es: Database pag
11a8b 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 46  e size..** .** F
11a8c 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 52  ollowed by (JOUR
11a8d 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29  NAL_HDR_SZ - 28)
11a8e 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64   bytes of unused
11a8f 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69   space..*/.stati
11a90 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e  c int writeJourn
11a91 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 61  alHdr(Pager *pPa
11a92 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
11a93 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
11a94 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11a95 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
11a96 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20  char *zHeader = 
11a97 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
11a98 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79  e;  /* Temporary
11a99 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 62   space used to b
11a9a 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a 20  uild header */. 
11a9b 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d 20 70   u32 nHeader = p
11a9c 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b  Pager->pageSize;
11a9d 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
11a9e 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
11a9f 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a 2f 0a  o by zHeader */.
11aa0 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 20 20    u32 nWrite;   
11aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11aa2 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
11aa3 66 20 68 65 61 64 65 72 20 73 65 63 74 6f 72 20  f header sector 
11aa4 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74  written */.  int
11aa5 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
11aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11aa7 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
11aa8 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69   */..  assert( i
11aa9 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
11aaa 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f  d) );      /* Jo
11aab 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20  urnal file must 
11aac 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 69  be open. */..  i
11aad 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e  f( nHeader>JOURN
11aae 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
11aaf 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72  ) ){.    nHeader
11ab0 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53   = JOURNAL_HDR_S
11ab1 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a  Z(pPager);.  }..
11ab2 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
11ab3 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69  e active savepoi
11ab4 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 74  nts and any of t
11ab5 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 65 64  hem were created
11ab6 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 65   .  ** since the
11ab7 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f 75   most recent jou
11ab8 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 73 20  rnal header was 
11ab9 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20  written, update 
11aba 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 53  the .  ** PagerS
11abb 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
11abc 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0a  set fields now..
11abd 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b    */.  for(ii=0;
11abe 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76   ii<pPager->nSav
11abf 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  epoint; ii++){. 
11ac0 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61     if( pPager->a
11ac1 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48  Savepoint[ii].iH
11ac2 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a 20  drOffset==0 ){. 
11ac3 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61       pPager->aSa
11ac4 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72  vepoint[ii].iHdr
11ac5 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d  Offset = pPager-
11ac6 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20  >journalOff;.   
11ac7 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72   }.  }..  pPager
11ac8 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70  ->journalHdr = p
11ac9 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11aca 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66  f = journalHdrOf
11acb 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 0a 20  fset(pPager);.. 
11acc 20 2f 2a 20 0a 20 20 2a 2a 20 57 72 69 74 65 20   /* .  ** Write 
11acd 74 68 65 20 6e 52 65 63 20 46 69 65 6c 64 20 2d  the nRec Field -
11ace 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
11acf 61 67 65 20 72 65 63 6f 72 64 73 20 74 68 61 74  age records that
11ad0 20 66 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a   follow this.  *
11ad1 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  * journal header
11ad2 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f  . Normally, zero
11ad3 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74   is written to t
11ad4 68 69 73 20 76 61 6c 75 65 20 61 74 20 74 68 69  his value at thi
11ad5 73 20 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74  s time..  ** Aft
11ad6 65 72 20 74 68 65 20 72 65 63 6f 72 64 73 20 61  er the records a
11ad7 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  re added to the 
11ad8 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 74 68 65  journal (and the
11ad9 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c   journal synced,
11ada 20 0a 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c   .  ** if in ful
11adb 6c 2d 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68  l-sync mode), th
11adc 65 20 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 72  e zero is overwr
11add 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 74  itten with the t
11ade 72 75 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20  rue number.  ** 
11adf 6f 66 20 72 65 63 6f 72 64 73 20 28 73 65 65 20  of records (see 
11ae0 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a  syncJournal())..
11ae1 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74    **.  ** A fast
11ae2 65 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69  er alternative i
11ae3 73 20 74 6f 20 77 72 69 74 65 20 30 78 46 46 46  s to write 0xFFF
11ae4 46 46 46 46 46 20 74 6f 20 74 68 65 20 6e 52 65  FFFFF to the nRe
11ae5 63 20 66 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20  c field. When.  
11ae6 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 6a  ** reading the j
11ae7 6f 75 72 6e 61 6c 20 74 68 69 73 20 76 61 6c 75  ournal this valu
11ae8 65 20 74 65 6c 6c 73 20 53 51 4c 69 74 65 20 74  e tells SQLite t
11ae9 6f 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68  o assume that th
11aea 65 0a 20 20 2a 2a 20 72 65 73 74 20 6f 66 20 74  e.  ** rest of t
11aeb 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11aec 63 6f 6e 74 61 69 6e 73 20 76 61 6c 69 64 20 70  contains valid p
11aed 61 67 65 20 72 65 63 6f 72 64 73 2e 20 54 68 69  age records. Thi
11aee 73 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a  s assumption.  *
11aef 2a 20 69 73 20 64 61 6e 67 65 72 6f 75 73 2c 20  * is dangerous, 
11af0 61 73 20 69 66 20 61 20 66 61 69 6c 75 72 65 20  as if a failure 
11af1 6f 63 63 75 72 72 65 64 20 77 68 69 6c 73 74 20  occurred whilst 
11af2 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a  writing to the j
11af3 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65  ournal.  ** file
11af4 20 69 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20   it may contain 
11af5 73 6f 6d 65 20 67 61 72 62 61 67 65 20 64 61 74  some garbage dat
11af6 61 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f  a. There are two
11af7 20 73 63 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20   scenarios.  ** 
11af8 77 68 65 72 65 20 74 68 69 73 20 72 69 73 6b 20  where this risk 
11af9 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a  can be ignored:.
11afa 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68    **.  **   * Wh
11afb 65 6e 20 74 68 65 20 70 61 67 65 72 20 69 73 20  en the pager is 
11afc 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e  in no-sync mode.
11afd 20 43 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e 20   Corruption can 
11afe 66 6f 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20  follow a.  **   
11aff 20 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20    power failure 
11b00 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 79  in this case any
11b01 77 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  way..  **.  **  
11b02 20 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49   * When the SQLI
11b03 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50  TE_IOCAP_SAFE_AP
11b04 50 45 4e 44 20 66 6c 61 67 20 69 73 20 73 65 74  PEND flag is set
11b05 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 65  . This guarantee
11b06 73 0a 20 20 2a 2a 20 20 20 20 20 74 68 61 74 20  s.  **     that 
11b07 67 61 72 62 61 67 65 20 64 61 74 61 20 69 73 20  garbage data is 
11b08 6e 65 76 65 72 20 61 70 70 65 6e 64 65 64 20 74  never appended t
11b09 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
11b0a 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  le..  */.  asser
11b0b 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
11b0c 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  ->fd) || pPager-
11b0d 3e 6e 6f 53 79 6e 63 20 29 3b 0a 20 20 69 66 28  >noSync );.  if(
11b0e 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63   (pPager->noSync
11b0f 29 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 6a 6f  ) || (pPager->jo
11b10 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
11b11 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
11b12 4f 52 59 29 0a 20 20 20 7c 7c 20 28 73 71 6c 69  ORY).   || (sqli
11b13 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61  te3OsDeviceChara
11b14 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65  cteristics(pPage
11b15 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f  r->fd)&SQLITE_IO
11b16 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29  CAP_SAFE_APPEND)
11b17 20 0a 20 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70   .  ){.    memcp
11b18 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72  y(zHeader, aJour
11b19 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66  nalMagic, sizeof
11b1a 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29  (aJournalMagic))
11b1b 3b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28  ;.    put32bits(
11b1c 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
11b1d 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c  aJournalMagic)],
11b1e 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20   0xffffffff);.  
11b1f 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65  }else{.    memse
11b20 74 28 7a 48 65 61 64 65 72 2c 20 30 2c 20 73 69  t(zHeader, 0, si
11b21 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11b22 69 63 29 2b 34 29 3b 0a 20 20 7d 0a 0a 20 20 2f  ic)+4);.  }..  /
11b23 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68 65  * The random che
11b24 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c 69  ck-hash initiali
11b25 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65  ser */ .  sqlite
11b26 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
11b27 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73 75  eof(pPager->cksu
11b28 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72 2d  mInit), &pPager-
11b29 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 70  >cksumInit);.  p
11b2a 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65  ut32bits(&zHeade
11b2b 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
11b2c 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61 67  lMagic)+4], pPag
11b2d 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a  er->cksumInit);.
11b2e 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 6c    /* The initial
11b2f 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 2a   database size *
11b30 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a  /.  put32bits(&z
11b31 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
11b32 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d 2c  ournalMagic)+8],
11b33 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53   pPager->dbOrigS
11b34 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61  ize);.  /* The a
11b35 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69  ssumed sector si
11b36 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63  ze for this proc
11b37 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62 69  ess */.  put32bi
11b38 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65  ts(&zHeader[size
11b39 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
11b3a 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73  )+12], pPager->s
11b3b 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 2f  ectorSize);..  /
11b3c 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20  * The page size 
11b3d 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  */.  put32bits(&
11b3e 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
11b3f 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36  JournalMagic)+16
11b40 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  ], pPager->pageS
11b41 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74  ize);..  /* Init
11b42 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 61 69  ializing the tai
11b43 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  l of the buffer 
11b44 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79  is not necessary
11b45 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a 20 20  .  Everything.  
11b46 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20 69 66  ** works find if
11b47 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
11b48 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69 74 74  emset() is omitt
11b49 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69 61 6c  ed.  But initial
11b4a 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d  izing.  ** the m
11b4b 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73 20 76  emory prevents v
11b4c 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63 6f 6d  algrind from com
11b4d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77 65 20  plaining, so we 
11b4e 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f 0a 20  are willing to. 
11b4f 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70 65 72   ** take the per
11b50 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a 20 20  formance hit..  
11b51 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a 48 65  */.  memset(&zHe
11b52 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75  ader[sizeof(aJou
11b53 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d 2c 20  rnalMagic)+20], 
11b54 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48 65 61  0,.         nHea
11b55 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a 6f 75  der-(sizeof(aJou
11b56 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29 29 3b  rnalMagic)+20));
11b57 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79  ..  /* In theory
11b58 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e 65 63  , it is only nec
11b59 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20  essary to write 
11b5a 74 68 65 20 32 38 20 62 79 74 65 73 20 74 68 61  the 28 bytes tha
11b5b 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f 75 72  t the .  ** jour
11b5c 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e 73 75  nal header consu
11b5d 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  mes to the journ
11b5e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20 54 68  al file here. Th
11b5f 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  en increment the
11b60 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a 6f 75   .  ** Pager.jou
11b61 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62 6c 65  rnalOff variable
11b62 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f   by JOURNAL_HDR_
11b63 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e  SZ so that the n
11b64 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f 72 64  ext .  ** record
11b65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74   is written to t
11b66 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 63  he following sec
11b67 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61 20 67  tor (leaving a g
11b68 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 20  ap in the file. 
11b69 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20 62 65   ** that will be
11b6a 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69 6c 6c   implicitly fill
11b6b 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f 53 29  ed in by the OS)
11b6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65  ..  **.  ** Howe
11b6d 76 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20  ver it has been 
11b6e 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20  discovered that 
11b6f 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20  on some systems 
11b70 74 68 69 73 20 70 61 74 74 65 72 6e 20 63 61 6e  this pattern can
11b71 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e 69 66   .  ** be signif
11b72 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 20 74  icantly slower t
11b73 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79  han contiguously
11b74 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f   writing data to
11b75 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a 2a 20   the file,.  ** 
11b76 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61  even if that mea
11b77 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 77 72  ns explicitly wr
11b78 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68  iting data to th
11b79 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a 2a  e block of .  **
11b7a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a   (JOURNAL_HDR_SZ
11b7b 20 2d 20 32 38 29 20 62 79 74 65 73 20 74 68 61   - 28) bytes tha
11b7c 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73  t will not be us
11b7d 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73 20 77  ed. So that is w
11b7e 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f 6e 65  hat.  ** is done
11b7f 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  . .  **.  ** The
11b80 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69 72 65   loop is require
11b81 64 20 68 65 72 65 20 69 6e 20 63 61 73 65 20 74  d here in case t
11b82 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 69  he sector-size i
11b83 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
11b84 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  e .  ** database
11b85 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69 6e 63   page size. Sinc
11b86 65 20 74 68 65 20 7a 48 65 61 64 65 72 20 62 75  e the zHeader bu
11b87 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50 61 67  ffer is only Pag
11b88 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 2a 2a  er.pageSize.  **
11b89 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20   bytes in size, 
11b8a 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 63 61  more than one ca
11b8b 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 57  ll to sqlite3OsW
11b8c 72 69 74 65 28 29 20 6d 61 79 20 62 65 20 72 65  rite() may be re
11b8d 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f 20 70  quired.  ** to p
11b8e 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e 74 69  opulate the enti
11b8f 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  re journal heade
11b90 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f 20 0a  r sector..  */ .
11b91 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30 3b 20    for(nWrite=0; 
11b92 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26 6e  rc==SQLITE_OK&&n
11b93 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48 44  Write<JOURNAL_HD
11b94 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20 6e 57  R_SZ(pPager); nW
11b95 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29 7b 0a  rite+=nHeader){.
11b96 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48      IOTRACE(("JH
11b97 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22  DR %p %lld %d\n"
11b98 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  , pPager, pPager
11b99 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 6e 48  ->journalHdr, nH
11b9a 65 61 64 65 72 29 29 0a 20 20 20 20 72 63 20 3d  eader)).    rc =
11b9b 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
11b9c 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65  pPager->jfd, zHe
11b9d 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c 20 70  ader, nHeader, p
11b9e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11b9f 66 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  f);.    pPager->
11ba0 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 6e 48  journalOff += nH
11ba1 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 65  eader;.  }..  re
11ba2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
11ba3 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
11ba4 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  le must be open 
11ba5 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c  when this is cal
11ba6 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 68  led. A journal h
11ba7 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20 28 4a  eader file.** (J
11ba8 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79  OURNAL_HDR_SZ by
11ba9 74 65 73 29 20 69 73 20 72 65 61 64 20 66 72 6f  tes) is read fro
11baa 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  m the current lo
11bab 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f  cation in the jo
11bac 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 54  urnal.** file. T
11bad 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  he current locat
11bae 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ion in the journ
11baf 61 6c 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e  al file is given
11bb0 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 6a   by.** pPager->j
11bb1 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65 20 63  ournalOff. See c
11bb2 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75  omments above fu
11bb3 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72  nction writeJour
11bb4 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a 2a 2a 20  nalHdr() for.** 
11bb5 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  a description of
11bb6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
11bb7 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a  der format..**.*
11bb8 2a 20 49 66 20 74 68 65 20 68 65 61 64 65 72 20  * If the header 
11bb9 69 73 20 72 65 61 64 20 73 75 63 63 65 73 73 66  is read successf
11bba 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 69 73 20  ully, *pNRec is 
11bbb 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
11bbc 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72 65 63  r of.** page rec
11bbd 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  ords following t
11bbe 68 69 73 20 68 65 61 64 65 72 20 61 6e 64 20 2a  his header and *
11bbf 70 44 62 53 69 7a 65 20 69 73 20 73 65 74 20 74  pDbSize is set t
11bc0 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  o the size of th
11bc1 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 62 65  e.** database be
11bc2 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63  fore the transac
11bc3 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e 20 70  tion began, in p
11bc4 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50 61 67  ages. Also, pPag
11bc5 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a  er->cksumInit.**
11bc6 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76   is set to the v
11bc7 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74  alue read from t
11bc8 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
11bc9 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  r. SQLITE_OK is 
11bca 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e 20 74  returned.** in t
11bcb 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
11bcc 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  If the journal h
11bcd 65 61 64 65 72 20 66 69 6c 65 20 61 70 70 65 61  eader file appea
11bce 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70 74  rs to be corrupt
11bcf 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  ed, SQLITE_DONE 
11bd0 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 61  is.** returned a
11bd1 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 20 2a 50  nd *pNRec and *P
11bd2 44 62 53 69 7a 65 20 61 72 65 20 75 6e 64 65 66  DbSize are undef
11bd3 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 52 4e 41  ined.  If JOURNA
11bd4 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 0a 2a  L_HDR_SZ bytes.*
11bd5 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 61 64  * cannot be read
11bd6 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
11bd7 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 6f 72 20  l file an error 
11bd8 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
11bd9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11bda 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 0a  readJournalHdr(.
11bdb 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c    Pager *pPager,
11bdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11bdd 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a  * Pager object *
11bde 2f 0a 20 20 69 6e 74 20 69 73 48 6f 74 2c 0a 20  /.  int isHot,. 
11bdf 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65   i64 journalSize
11be0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
11be1 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 70 65   Size of the ope
11be2 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  n journal file i
11be3 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32  n bytes */.  u32
11be4 20 2a 70 4e 52 65 63 2c 20 20 20 20 20 20 20 20   *pNRec,        
11be5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
11be6 3a 20 56 61 6c 75 65 20 72 65 61 64 20 66 72 6f  : Value read fro
11be7 6d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64  m the nRec field
11be8 20 2a 2f 0a 20 20 75 33 32 20 2a 70 44 62 53 69   */.  u32 *pDbSi
11be9 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ze              
11bea 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65     /* OUT: Value
11beb 20 6f 66 20 6f 72 69 67 69 6e 61 6c 20 64 61 74   of original dat
11bec 61 62 61 73 65 20 73 69 7a 65 20 66 69 65 6c 64  abase size field
11bed 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
11bee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11bef 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
11bf0 63 6f 64 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  code */.  unsign
11bf1 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38  ed char aMagic[8
11bf2 5d 3b 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66  ];     /* A buff
11bf3 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d  er to hold the m
11bf4 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20  agic header */. 
11bf5 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20   i64 iHdrOff;   
11bf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11bf7 20 4f 66 66 73 65 74 20 6f 66 20 6a 6f 75 72 6e   Offset of journ
11bf8 61 6c 20 68 65 61 64 65 72 20 62 65 69 6e 67 20  al header being 
11bf9 72 65 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  read */..  asser
11bfa 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
11bfb 2d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f  ->jfd) );      /
11bfc 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d  * Journal file m
11bfd 75 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a  ust be open. */.
11bfe 0a 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 50 61  .  /* Advance Pa
11bff 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 74  ger.journalOff t
11c00 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
11c01 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 2e 20  he next sector. 
11c02 49 66 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72  If the.  ** jour
11c03 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 6f 6f 20  nal file is too 
11c04 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 72 65 20  small for there 
11c05 74 6f 20 62 65 20 61 20 68 65 61 64 65 72 20 73  to be a header s
11c06 74 6f 72 65 64 20 61 74 20 74 68 69 73 0a 20 20  tored at this.  
11c07 2a 2a 20 70 6f 69 6e 74 2c 20 72 65 74 75 72 6e  ** point, return
11c08 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 20 20   SQLITE_DONE..  
11c09 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  */.  pPager->jou
11c0a 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61  rnalOff = journa
11c0b 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65  lHdrOffset(pPage
11c0c 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  r);.  if( pPager
11c0d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55  ->journalOff+JOU
11c0e 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
11c0f 65 72 29 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a  er) > journalSiz
11c10 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
11c11 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
11c12 0a 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61  .  iHdrOff = pPa
11c13 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
11c14 0a 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e 20 74  ..  /* Read in t
11c15 68 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73  he first 8 bytes
11c16 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
11c17 68 65 61 64 65 72 2e 20 49 66 20 74 68 65 79 20  header. If they 
11c18 64 6f 20 6e 6f 74 20 6d 61 74 63 68 0a 20 20 2a  do not match.  *
11c19 2a 20 74 68 65 20 20 6d 61 67 69 63 20 73 74 72  * the  magic str
11c1a 69 6e 67 20 66 6f 75 6e 64 20 61 74 20 74 68 65  ing found at the
11c1b 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20 6a   start of each j
11c1c 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 72  ournal header, r
11c1d 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54  eturn.  ** SQLIT
11c1e 45 5f 44 4f 4e 45 2e 20 49 66 20 61 6e 20 49 4f  E_DONE. If an IO
11c1f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72   error occurs, r
11c20 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
11c21 6f 64 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  ode. Otherwise,.
11c22 20 20 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 20 20    ** proceed..  
11c23 2a 2f 0a 20 20 69 66 28 20 69 73 48 6f 74 20 7c  */.  if( isHot |
11c24 7c 20 69 48 64 72 4f 66 66 21 3d 70 50 61 67 65  | iHdrOff!=pPage
11c25 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 29 7b  r->journalHdr ){
11c26 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11c27 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e  3OsRead(pPager->
11c28 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 73 69 7a  jfd, aMagic, siz
11c29 65 6f 66 28 61 4d 61 67 69 63 29 2c 20 69 48 64  eof(aMagic), iHd
11c2a 72 4f 66 66 29 3b 0a 20 20 20 20 69 66 28 20 72  rOff);.    if( r
11c2b 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  c ){.      retur
11c2c 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
11c2d 69 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69  if( memcmp(aMagi
11c2e 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  c, aJournalMagic
11c2f 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29  , sizeof(aMagic)
11c30 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )!=0 ){.      re
11c31 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
11c32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
11c33 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74  * Read the first
11c34 20 74 68 72 65 65 20 33 32 2d 62 69 74 20 66 69   three 32-bit fi
11c35 65 6c 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  elds of the jour
11c36 6e 61 6c 20 68 65 61 64 65 72 3a 20 54 68 65 20  nal header: The 
11c37 6e 52 65 63 0a 20 20 2a 2a 20 66 69 65 6c 64 2c  nRec.  ** field,
11c38 20 74 68 65 20 63 68 65 63 6b 73 75 6d 2d 69 6e   the checksum-in
11c39 69 74 69 61 6c 69 7a 65 72 20 61 6e 64 20 74 68  itializer and th
11c3a 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  e database size 
11c3b 61 74 20 74 68 65 20 73 74 61 72 74 0a 20 20 2a  at the start.  *
11c3c 2a 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  * of the transac
11c3d 74 69 6f 6e 2e 20 52 65 74 75 72 6e 20 61 6e 20  tion. Return an 
11c3e 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e  error code if an
11c3f 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ything goes wron
11c40 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51  g..  */.  if( SQ
11c41 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
11c42 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72  ead32bits(pPager
11c43 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 38  ->jfd, iHdrOff+8
11c44 2c 20 70 4e 52 65 63 29 29 0a 20 20 20 7c 7c 20  , pNRec)).   || 
11c45 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
11c46 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67   read32bits(pPag
11c47 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
11c48 2b 31 32 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b  +12, &pPager->ck
11c49 73 75 6d 49 6e 69 74 29 29 0a 20 20 20 7c 7c 20  sumInit)).   || 
11c4a 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
11c4b 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67   read32bits(pPag
11c4c 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
11c4d 2b 31 36 2c 20 70 44 62 53 69 7a 65 29 29 0a 20  +16, pDbSize)). 
11c4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
11c4f 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  c;.  }..  if( pP
11c50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11c51 3d 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 69  ==0 ){.    u32 i
11c52 50 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20  PageSize;       
11c53 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 2d          /* Page-
11c54 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f  size field of jo
11c55 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a  urnal header */.
11c56 20 20 20 20 75 33 32 20 69 53 65 63 74 6f 72 53      u32 iSectorS
11c57 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
11c58 20 2f 2a 20 53 65 63 74 6f 72 2d 73 69 7a 65 20   /* Sector-size 
11c59 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c  field of journal
11c5a 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75   header */.    u
11c5b 31 36 20 69 50 61 67 65 53 69 7a 65 31 36 3b 20  16 iPageSize16; 
11c5c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
11c5d 6f 70 79 20 6f 66 20 69 50 61 67 65 53 69 7a 65  opy of iPageSize
11c5e 20 69 6e 20 31 36 2d 62 69 74 20 76 61 72 69 61   in 16-bit varia
11c5f 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52  ble */..    /* R
11c60 65 61 64 20 74 68 65 20 70 61 67 65 2d 73 69 7a  ead the page-siz
11c61 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a  e and sector-siz
11c62 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
11c63 20 66 69 65 6c 64 73 2e 20 2a 2f 0a 20 20 20 20   fields. */.    
11c64 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
11c65 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
11c66 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
11c67 72 4f 66 66 2b 32 30 2c 20 26 69 53 65 63 74 6f  rOff+20, &iSecto
11c68 72 53 69 7a 65 29 29 0a 20 20 20 20 20 7c 7c 20  rSize)).     || 
11c69 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
11c6a 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67   read32bits(pPag
11c6b 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
11c6c 2b 32 34 2c 20 26 69 50 61 67 65 53 69 7a 65 29  +24, &iPageSize)
11c6d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
11c6e 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
11c6f 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
11c70 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 72 65  at the values re
11c71 61 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ad from the page
11c72 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72  -size and sector
11c73 2d 73 69 7a 65 20 66 69 65 6c 64 73 0a 20 20 20  -size fields.   
11c74 20 2a 2a 20 61 72 65 20 77 69 74 68 69 6e 20 72   ** are within r
11c75 61 6e 67 65 2e 20 54 6f 20 62 65 20 27 69 6e 20  ange. To be 'in 
11c76 72 61 6e 67 65 27 2c 20 62 6f 74 68 20 76 61 6c  range', both val
11c77 75 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 61  ues need to be a
11c78 20 70 6f 77 65 72 0a 20 20 20 20 2a 2a 20 6f 66   power.    ** of
11c79 20 74 77 6f 20 67 72 65 61 74 65 72 20 74 68 61   two greater tha
11c7a 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 35 31  n or equal to 51
11c7b 32 20 6f 72 20 33 32 2c 20 61 6e 64 20 6e 6f 74  2 or 32, and not
11c7c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
11c7d 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70  eir .    ** resp
11c7e 65 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74  ective compile t
11c7f 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69  ime maximum limi
11c80 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ts..    */.    i
11c81 66 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32  f( iPageSize<512
11c82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c83 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65    || iSectorSize
11c84 3c 33 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67  <32.     || iPag
11c85 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58  eSize>SQLITE_MAX
11c86 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53  _PAGE_SIZE || iS
11c87 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45  ectorSize>MAX_SE
11c88 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c  CTOR_SIZE.     |
11c89 7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29  | ((iPageSize-1)
11c8a 26 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20  &iPageSize)!=0  
11c8b 20 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a   || ((iSectorSiz
11c8c 65 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65  e-1)&iSectorSize
11c8d 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20  )!=0 .    ){.   
11c8e 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74     /* If the eit
11c8f 68 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a  her the page-siz
11c90 65 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65  e or sector-size
11c91 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d   in the journal-
11c92 68 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20  header is .     
11c93 20 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65   ** invalid, the
11c94 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68  n the process th
11c95 61 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75  at wrote the jou
11c96 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74  rnal-header must
11c97 20 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20   have .      ** 
11c98 63 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74  crashed before t
11c99 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79  he header was sy
11c9a 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  nced. In this ca
11c9b 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20  se stop reading 
11c9c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f  .      ** the jo
11c9d 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e  urnal file here.
11c9e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
11c9f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
11ca0 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  NE;.    }..    /
11ca1 2a 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67  * Update the pag
11ca2 65 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20  e-size to match 
11ca3 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66  the value read f
11ca4 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  rom the journal.
11ca5 20 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74   .    ** Use a t
11ca6 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20  estcase() macro 
11ca7 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
11ca8 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  t malloc failure
11ca9 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20   within .    ** 
11caa 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
11cab 28 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20  () is tested..  
11cac 20 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69    */.    iPageSi
11cad 7a 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67  ze16 = (u16)iPag
11cae 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20  eSize;.    rc = 
11caf 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
11cb0 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20  agesize(pPager, 
11cb1 26 69 50 61 67 65 53 69 7a 65 31 36 2c 20 2d 31  &iPageSize16, -1
11cb2 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
11cb3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11cb4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
11cb5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69  !=SQLITE_OK || i
11cb6 50 61 67 65 53 69 7a 65 31 36 3d 3d 28 75 31 36  PageSize16==(u16
11cb7 29 69 50 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20  )iPageSize );.. 
11cb8 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
11cb9 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d   assumed sector-
11cba 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68  size to match th
11cbb 65 20 76 61 6c 75 65 20 75 73 65 64 20 62 79 20  e value used by 
11cbc 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63  .    ** the proc
11cbd 65 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64  ess that created
11cbe 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49   this journal. I
11cbf 66 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77  f this journal w
11cc0 61 73 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65  as.    ** create
11cc1 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f  d by a process o
11cc2 74 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f  ther than this o
11cc3 6e 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  ne, then this ro
11cc4 75 74 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20  utine.    ** is 
11cc5 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f  being called fro
11cc6 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70  m within pager_p
11cc7 6c 61 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c  layback(). The l
11cc8 6f 63 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a  ocal value.    *
11cc9 2a 20 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f  * of Pager.secto
11cca 72 53 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65  rSize is restore
11ccb 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  d at the end of 
11ccc 74 68 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20  that routine..  
11ccd 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d    */.    pPager-
11cce 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53  >sectorSize = iS
11ccf 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a  ectorSize;.  }..
11cd0 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11cd1 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f  lOff += JOURNAL_
11cd2 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a  HDR_SZ(pPager);.
11cd3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11cd4 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  ./*.** Write the
11cd5 20 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72   supplied master
11cd6 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e   journal name in
11cd7 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
11cd8 69 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a  ile for pager.**
11cd9 20 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63   pPager at the c
11cda 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e  urrent location.
11cdb 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   The master jour
11cdc 6e 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65  nal name must be
11cdd 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69   the last.** thi
11cde 6e 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20  ng written to a 
11cdf 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66  journal file. If
11ce0 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
11ce1 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c   full-sync mode,
11ce2 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
11ce3 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
11ce4 69 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74  is advanced to t
11ce5 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62  he next sector b
11ce6 6f 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a  oundary before.*
11ce7 2a 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72  * anything is wr
11ce8 69 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61  itten. The forma
11ce9 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20  t is:.**.**   + 
11cea 34 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d  4 bytes: PAGER_M
11ceb 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e  J_PGNO..**   + N
11cec 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a   bytes: Master j
11ced 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20  ournal filename 
11cee 69 6e 20 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b  in utf-8..**   +
11cef 20 34 20 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e   4 bytes: N (len
11cf0 67 74 68 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f  gth of master jo
11cf1 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79  urnal name in by
11cf2 74 65 73 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d  tes, no nul-term
11cf3 69 6e 61 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20  inator)..**   + 
11cf4 34 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20  4 bytes: Master 
11cf5 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65  journal name che
11cf6 63 6b 73 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20  cksum..**   + 8 
11cf7 62 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d  bytes: aJournalM
11cf8 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  agic[]..**.** Th
11cf9 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11cfa 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69   page checksum i
11cfb 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65  s the sum of the
11cfc 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61   bytes in the ma
11cfd 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  ster.** journal 
11cfe 6e 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68  name, where each
11cff 20 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72   byte is interpr
11d00 65 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64  eted as a signed
11d01 20 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a   8-bit integer..
11d02 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72  **.** If zMaster
11d03 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
11d04 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61  er (occurs for a
11d05 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
11d06 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a   transaction), .
11d07 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20  ** this call is 
11d08 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
11d09 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74  ic int writeMast
11d0a 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20  erJournal(Pager 
11d0b 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63  *pPager, const c
11d0c 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20  har *zMaster){. 
11d0d 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
11d0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d0f 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
11d10 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65   */.  int nMaste
11d11 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
11d12 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
11d13 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74   of string zMast
11d14 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72  er */.  i64 iHdr
11d15 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Off;            
11d16 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
11d17 65 74 20 6f 66 20 68 65 61 64 65 72 20 69 6e 20  et of header in 
11d18 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
11d19 20 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20    i64 jrnlSize; 
11d1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d1b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f     /* Size of jo
11d1c 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69  urnal file on di
11d1d 73 6b 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75  sk */.  u32 cksu
11d1e 6d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  m = 0;          
11d1f 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63           /* Chec
11d20 6b 73 75 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a  ksum of string z
11d21 4d 61 73 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28  Master */..  if(
11d22 20 21 7a 4d 61 73 74 65 72 20 7c 7c 20 70 50 61   !zMaster || pPa
11d23 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 0a 20  ger->setMaster. 
11d24 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75    || pPager->jou
11d25 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
11d26 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
11d27 52 59 20 0a 20 20 20 7c 7c 20 70 50 61 67 65 72  RY .   || pPager
11d28 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
11d29 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
11d2a 5f 4f 46 46 20 0a 20 20 29 7b 0a 20 20 20 20 72  _OFF .  ){.    r
11d2b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
11d2c 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73  .  }.  pPager->s
11d2d 65 74 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20  etMaster = 1;.  
11d2e 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
11d2f 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a  Pager->jfd) );..
11d30 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74    /* Calculate t
11d31 68 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74  he length in byt
11d32 65 73 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b  es and the check
11d33 73 75 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a  sum of zMaster *
11d34 2f 0a 20 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d  /.  for(nMaster=
11d35 30 3b 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74  0; zMaster[nMast
11d36 65 72 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b  er]; nMaster++){
11d37 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d  .    cksum += zM
11d38 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a  aster[nMaster];.
11d39 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20    }..  /* If in 
11d3a 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20  full-sync mode, 
11d3b 61 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e  advance to the n
11d3c 65 78 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20  ext disk sector 
11d3d 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20  before writing. 
11d3e 20 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a   ** the master j
11d3f 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69  ournal name. Thi
11d40 73 20 69 73 20 69 6e 20 63 61 73 65 20 74 68 65  s is in case the
11d41 20 70 72 65 76 69 6f 75 73 20 70 61 67 65 20 77   previous page w
11d42 72 69 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74  ritten to.  ** t
11d43 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61  he journal has a
11d44 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63  lready been sync
11d45 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ed..  */.  if( p
11d46 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20  Pager->fullSync 
11d47 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  ){.    pPager->j
11d48 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72  ournalOff = jour
11d49 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61  nalHdrOffset(pPa
11d4a 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72  ger);.  }.  iHdr
11d4b 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  Off = pPager->jo
11d4c 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20  urnalOff;..  /* 
11d4d 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72  Write the master
11d4e 20 6a 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f   journal data to
11d4f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
11d50 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66  journal file. If
11d51 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f  .  ** an error o
11d52 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68  ccurs, return th
11d53 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20  e error code to 
11d54 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f  the caller..  */
11d55 0a 20 20 69 66 28 20 28 30 20 21 3d 20 28 72 63  .  if( (0 != (rc
11d56 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
11d57 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72  Pager->jfd, iHdr
11d58 4f 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47  Off, PAGER_MJ_PG
11d59 4e 4f 28 70 50 61 67 65 72 29 29 29 29 0a 20 20  NO(pPager)))).  
11d5a 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20   || (0 != (rc = 
11d5b 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
11d5c 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73  Pager->jfd, zMas
11d5d 74 65 72 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48  ter, nMaster, iH
11d5e 64 72 4f 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c  drOff+4))).   ||
11d5f 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69   (0 != (rc = wri
11d60 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  te32bits(pPager-
11d61 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b  >jfd, iHdrOff+4+
11d62 6e 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72  nMaster, nMaster
11d63 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20  ))).   || (0 != 
11d64 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74  (rc = write32bit
11d65 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69  s(pPager->jfd, i
11d66 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72  HdrOff+4+nMaster
11d67 2b 34 2c 20 63 6b 73 75 6d 29 29 29 0a 20 20 20  +4, cksum))).   
11d68 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73  || (0 != (rc = s
11d69 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
11d6a 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72  ager->jfd, aJour
11d6b 6e 61 6c 4d 61 67 69 63 2c 20 38 2c 20 69 48 64  nalMagic, 8, iHd
11d6c 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38  rOff+4+nMaster+8
11d6d 29 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  ))).  ){.    ret
11d6e 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50  urn rc;.  }.  pP
11d6f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11d70 20 2b 3d 20 28 6e 4d 61 73 74 65 72 2b 32 30 29   += (nMaster+20)
11d71 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64  ;.  pPager->need
11d72 53 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e  Sync = !pPager->
11d73 6e 6f 53 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66  noSync;..  /* If
11d74 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
11d75 20 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e   peristent-journ
11d76 61 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68  al mode, then th
11d77 65 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a  e physical .  **
11d78 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61   journal-file ma
11d79 79 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68  y extend past th
11d7a 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73  e end of the mas
11d7b 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter-journal name
11d7c 0a 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65  .  ** and 8 byte
11d7d 73 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20  s of magic data 
11d7e 6a 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20  just written to 
11d7f 74 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69  the file. This i
11d80 73 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75  s .  ** dangerou
11d81 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f  s because the co
11d82 64 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61  de to rollback a
11d83 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   hot-journal fil
11d84 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  e.  ** will not 
11d85 62 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20  be able to find 
11d86 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e  the master-journ
11d87 61 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72  al name to deter
11d88 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68  mine .  ** wheth
11d89 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f  er or not the jo
11d8a 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20  urnal is hot. . 
11d8b 20 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74   **.  ** Easiest
11d8c 20 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20   thing to do in 
11d8d 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73  this scenario is
11d8e 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65   to truncate the
11d8f 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66   journal .  ** f
11d90 69 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69  ile to the requi
11d91 72 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a  red size..  */ .
11d92 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
11d93 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  =(rc = sqlite3Os
11d94 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d  FileSize(pPager-
11d95 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29  >jfd, &jrnlSize)
11d96 29 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65  ).   && jrnlSize
11d97 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  >pPager->journal
11d98 4f 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20  Off.  ){.    rc 
11d99 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
11d9a 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ate(pPager->jfd,
11d9b 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11d9c 4f 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Off);.  }.  retu
11d9d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11d9e 46 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74  Find a page in t
11d9f 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69  he hash table gi
11da0 76 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d  ven its page num
11da1 62 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61  ber. Return.** a
11da2 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
11da3 70 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20  page or NULL if 
11da4 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
11da5 67 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c  ge is not .** al
11da6 72 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e  ready in memory.
11da7 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72  .*/.static PgHdr
11da8 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50   *pager_lookup(P
11da9 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67  ager *pPager, Pg
11daa 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64  no pgno){.  PgHd
11dab 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  r *p;           
11dac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11dad 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f   Return value */
11dae 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74  ..  /* It is not
11daf 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20   possible for a 
11db0 63 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65  call to PcacheFe
11db1 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74  tch() with creat
11db2 65 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a  eFlag==0 to.  **
11db3 20 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20   fail, since no 
11db4 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63  attempt to alloc
11db5 61 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f  ate dynamic memo
11db6 72 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e  ry will be made.
11db7 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71  .  */.  (void)sq
11db8 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
11db9 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
11dba 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a  , pgno, 0, &p);.
11dbb 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
11dbc 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20  *.** Unless the 
11dbd 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f  pager is in erro
11dbe 72 2d 73 74 61 74 65 2c 20 64 69 73 63 61 72 64  r-state, discard
11dbf 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70   all in-memory p
11dc0 61 67 65 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20  ages. If.** the 
11dc1 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f  pager is in erro
11dc2 72 2d 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68  r-state, then th
11dc3 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
11dc4 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20  op..**.** TODO: 
11dc5 57 68 79 20 63 61 6e 20 77 65 20 6e 6f 74 20 72  Why can we not r
11dc6 65 73 65 74 20 74 68 65 20 70 61 67 65 72 20 77  eset the pager w
11dc7 68 69 6c 65 20 69 6e 20 65 72 72 6f 72 20 73 74  hile in error st
11dc8 61 74 65 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ate?.*/.static v
11dc9 6f 69 64 20 70 61 67 65 72 5f 72 65 73 65 74 28  oid pager_reset(
11dca 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11dcb 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
11dcc 3d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65  =pPager->errCode
11dcd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
11dce 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61  ackupRestart(pPa
11dcf 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20  ger->pBackup);. 
11dd0 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
11dd1 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 50  Clear(pPager->pP
11dd2 43 61 63 68 65 29 3b 0a 20 20 20 20 70 50 61 67  Cache);.    pPag
11dd3 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
11dd4 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
11dd5 2a 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63  * Free all struc
11dd6 74 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67  tures in the Pag
11dd7 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20  er.aSavepoint[] 
11dd8 61 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f  array and set bo
11dd9 74 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76  th.** Pager.aSav
11dda 65 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72  epoint and Pager
11ddb 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a  .nSavepoint to z
11ddc 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73  ero. Close the s
11ddd 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66  ub-journal.** if
11dde 20 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20   it is open and 
11ddf 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74  the pager is not
11de0 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
11de1 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  de..*/.static vo
11de2 69 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76  id releaseAllSav
11de3 65 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70  epoints(Pager *p
11de4 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69  Pager){.  int ii
11de5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11de6 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20  /* Iterator for 
11de7 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20  looping through 
11de8 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74  Pager.aSavepoint
11de9 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   */.  for(ii=0; 
11dea 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65  ii<pPager->nSave
11deb 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20  point; ii++){.  
11dec 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
11ded 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61  estroy(pPager->a
11dee 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49  Savepoint[ii].pI
11def 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d  nSavepoint);.  }
11df0 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
11df1 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c  exclusiveMode ||
11df2 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75   sqlite3IsMemJou
11df3 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66  rnal(pPager->sjf
11df4 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  d) ){.    sqlite
11df5 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
11df6 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71  >sjfd);.  }.  sq
11df7 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65  lite3_free(pPage
11df8 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a  r->aSavepoint);.
11df9 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70    pPager->aSavep
11dfa 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67  oint = 0;.  pPag
11dfb 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d  er->nSavepoint =
11dfc 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53   0;.  pPager->nS
11dfd 75 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  ubRec = 0;.}../*
11dfe 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20  .** Set the bit 
11dff 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74  number pgno in t
11e00 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  he PagerSavepoin
11e01 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a  t.pInSavepoint .
11e02 2a 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c  ** bitvecs of al
11e03 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  l open savepoint
11e04 73 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  s. Return SQLITE
11e05 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75  _OK if successfu
11e06 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e  l.** or SQLITE_N
11e07 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63  OMEM if a malloc
11e08 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e   failure occurs.
11e09 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
11e0a 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74  ddToSavepointBit
11e0b 76 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67  vecs(Pager *pPag
11e0c 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  er, Pgno pgno){.
11e0d 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20    int ii;       
11e0e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
11e0f 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
11e10 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
11e11 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  _OK;       /* Re
11e12 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  sult code */..  
11e13 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61  for(ii=0; ii<pPa
11e14 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
11e15 20 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65   ii++){.    Page
11e16 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20  rSavepoint *p = 
11e17 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  &pPager->aSavepo
11e18 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28  int[ii];.    if(
11e19 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20   pgno<=p->nOrig 
11e1a 29 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73  ){.      rc |= s
11e1b 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
11e1c 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c  p->pInSavepoint,
11e1d 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65   pgno);.      te
11e1e 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49  stcase( rc==SQLI
11e1f 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
11e20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
11e21 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53  LITE_OK || rc==S
11e22 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
11e23 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
11e24 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  n rc;.}../*.** U
11e25 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61  nlock the databa
11e26 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 66 75  se file. This fu
11e27 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
11e28 70 20 69 66 20 74 68 65 20 70 61 67 65 72 0a 2a  p if the pager.*
11e29 2a 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76  * is in exclusiv
11e2a 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e mode..**.** If
11e2b 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 75   the pager is cu
11e2c 72 72 65 6e 74 6c 79 20 69 6e 20 65 72 72 6f 72  rrently in error
11e2d 20 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20   state, discard 
11e2e 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
11e2f 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 61 6e  .** the cache an
11e30 64 20 72 65 73 65 74 20 74 68 65 20 50 61 67 65  d reset the Page
11e31 72 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 65  r structure inte
11e32 72 6e 61 6c 20 73 74 61 74 65 2e 20 49 66 20 74  rnal state. If t
11e33 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 6f 70  here is.** an op
11e34 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c  en journal-file,
11e35 20 74 68 65 6e 20 74 68 65 20 6e 65 78 74 20 74   then the next t
11e36 69 6d 65 20 61 20 73 68 61 72 65 64 2d 6c 6f 63  ime a shared-loc
11e37 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  k is obtained.**
11e38 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 66 69   on the pager fi
11e39 6c 65 20 28 62 79 20 74 68 69 73 20 6f 72 20 61  le (by this or a
11e3a 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
11e3b 29 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a 2a 2a  ), it will be.**
11e3c 20 74 72 65 61 74 65 64 20 61 73 20 61 20 68 6f   treated as a ho
11e3d 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f  t-journal and ro
11e3e 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74  lled back..*/.st
11e3f 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f  atic void pager_
11e40 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50  unlock(Pager *pP
11e41 61 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70 50  ager){.  if( !pP
11e42 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
11e43 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ode ){.    int r
11e44 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
11e45 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
11e46 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f  n code */..    /
11e47 2a 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74  * Always close t
11e48 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11e49 77 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68  when dropping th
11e4a 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e  e database lock.
11e4b 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73  .    ** Otherwis
11e4c 65 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65  e, another conne
11e4d 63 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e  ction with journ
11e4e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d  al_mode=delete m
11e4f 69 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65  ight.    ** dele
11e50 74 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20  te the file out 
11e51 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20  from under us.. 
11e52 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
11e53 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
11e54 3e 6a 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  >jfd);.    sqlit
11e55 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
11e56 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
11e57 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  al);.    pPager-
11e58 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  >pInJournal = 0;
11e59 0a 20 20 20 20 72 65 6c 65 61 73 65 41 6c 6c 53  .    releaseAllS
11e5a 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72  avepoints(pPager
11e5b 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
11e5c 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b  e file is unlock
11e5d 65 64 2c 20 73 6f 6d 65 62 6f 64 79 20 65 6c 73  ed, somebody els
11e5e 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  e might change i
11e5f 74 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 76 61  t. The.    ** va
11e60 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 50  lues stored in P
11e61 61 67 65 72 2e 64 62 53 69 7a 65 20 65 74 63 2e  ager.dbSize etc.
11e62 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 69 6e   might become in
11e63 76 61 6c 69 64 20 69 66 0a 20 20 20 20 2a 2a 20  valid if.    ** 
11e64 74 68 69 73 20 68 61 70 70 65 6e 73 2e 20 54 4f  this happens. TO
11e65 44 4f 3a 20 52 65 61 6c 6c 79 2c 20 74 68 69 73  DO: Really, this
11e66 20 64 6f 65 73 6e 27 74 20 6e 65 65 64 20 74 6f   doesn't need to
11e67 20 62 65 20 63 6c 65 61 72 65 64 0a 20 20 20 20   be cleared.    
11e68 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 68 61  ** until the cha
11e69 6e 67 65 2d 63 6f 75 6e 74 65 72 20 63 68 65 63  nge-counter chec
11e6a 6b 20 66 61 69 6c 73 20 69 6e 20 50 61 67 65 72  k fails in Pager
11e6b 53 68 61 72 65 64 4c 6f 63 6b 28 29 2e 0a 20 20  SharedLock()..  
11e6c 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d    */.    pPager-
11e6d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30  >dbSizeValid = 0
11e6e 3b 0a 0a 20 20 20 20 72 63 20 3d 20 6f 73 55 6e  ;..    rc = osUn
11e6f 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  lock(pPager->fd,
11e70 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69   NO_LOCK);.    i
11e71 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 70  f( rc ){.      p
11e72 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d  Pager->errCode =
11e73 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49   rc;.    }.    I
11e74 4f 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20  OTRACE(("UNLOCK 
11e75 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a  %p\n", pPager)).
11e76 0a 20 20 20 20 2f 2a 20 49 66 20 50 61 67 65 72  .    /* If Pager
11e77 2e 65 72 72 43 6f 64 65 20 69 73 20 73 65 74 2c  .errCode is set,
11e78 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
11e79 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
11e7a 20 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a   cannot be.    *
11e7b 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f 77 20 74  * trusted. Now t
11e7c 68 61 74 20 74 68 65 20 70 61 67 65 72 20 66 69  hat the pager fi
11e7d 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20  le is unlocked, 
11e7e 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
11e7f 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 63 68 65  the.    ** cache
11e80 20 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65   can be discarde
11e81 64 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20  d and the error 
11e82 63 6f 64 65 20 73 61 66 65 6c 79 20 63 6c 65 61  code safely clea
11e83 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  red..    */.    
11e84 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
11e85 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ode ){.      if(
11e86 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
11e87 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  {.        pPager
11e88 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49  ->errCode = SQLI
11e89 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20  TE_OK;.      }. 
11e8a 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74       pager_reset
11e8b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a  (pPager);.    }.
11e8c 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61  .    pPager->cha
11e8d 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30  ngeCountDone = 0
11e8e 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ;.    pPager->st
11e8f 61 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f  ate = PAGER_UNLO
11e90 43 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  CK;.  }.}../*.**
11e91 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
11e92 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20  hould be called 
11e93 77 68 65 6e 20 61 6e 20 49 4f 45 52 52 2c 20 43  when an IOERR, C
11e94 4f 52 52 55 50 54 20 6f 72 20 46 55 4c 4c 20 65  ORRUPT or FULL e
11e95 72 72 6f 72 0a 2a 2a 20 6d 61 79 20 68 61 76 65  rror.** may have
11e96 20 6f 63 63 75 72 72 65 64 2e 20 54 68 65 20 66   occurred. The f
11e97 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
11e98 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
11e99 65 20 70 61 67 65 72 20 0a 2a 2a 20 73 74 72 75  e pager .** stru
11e9a 63 74 75 72 65 2c 20 74 68 65 20 73 65 63 6f 6e  cture, the secon
11e9b 64 20 74 68 65 20 65 72 72 6f 72 2d 63 6f 64 65  d the error-code
11e9c 20 61 62 6f 75 74 20 74 6f 20 62 65 20 72 65 74   about to be ret
11e9d 75 72 6e 65 64 20 62 79 20 61 20 70 61 67 65 72  urned by a pager
11e9e 20 0a 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f   .** API functio
11e9f 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  n. The value ret
11ea0 75 72 6e 65 64 20 69 73 20 61 20 63 6f 70 79 20  urned is a copy 
11ea1 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  of the second ar
11ea2 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 74 68  gument .** to th
11ea3 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a  is function. .**
11ea4 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e  .** If the secon
11ea5 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51  d argument is SQ
11ea6 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49  LITE_IOERR, SQLI
11ea7 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 20 53  TE_CORRUPT, or S
11ea8 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68  QLITE_FULL.** th
11ea9 65 20 65 72 72 6f 72 20 62 65 63 6f 6d 65 73 20  e error becomes 
11eaa 70 65 72 73 69 73 74 65 6e 74 2e 20 55 6e 74 69  persistent. Unti
11eab 6c 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 20  l the persisten 
11eac 65 72 72 6f 72 20 69 73 20 63 6c 65 61 72 65 64  error is cleared
11ead 2c 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20  ,.** subsequent 
11eae 41 50 49 20 63 61 6c 6c 73 20 6f 6e 20 74 68 69  API calls on thi
11eaf 73 20 50 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d  s Pager will imm
11eb0 65 64 69 61 74 65 6c 79 20 72 65 74 75 72 6e 20  ediately return 
11eb1 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20 65 72 72  the same .** err
11eb2 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41  or code..**.** A
11eb3 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f   persistent erro
11eb4 72 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  r indicates that
11eb5 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
11eb6 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65   the pager-cache
11eb7 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 74   .** cannot be t
11eb8 72 75 73 74 65 64 2e 20 54 68 69 73 20 73 74 61  rusted. This sta
11eb9 74 65 20 63 61 6e 20 62 65 20 63 6c 65 61 72 65  te can be cleare
11eba 64 20 62 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20  d by completely 
11ebb 64 69 73 63 61 72 64 69 6e 67 20 0a 2a 2a 20 74  discarding .** t
11ebc 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
11ebd 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 2e 20  he pager-cache. 
11ebe 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  If a transaction
11ebf 20 77 61 73 20 61 63 74 69 76 65 20 77 68 65 6e   was active when
11ec0 0a 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65  .** the persiste
11ec1 6e 74 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65  nt error occurre
11ec2 64 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c  d, then the roll
11ec3 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6d 61 79  back journal may
11ec4 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 72   need.** to be r
11ec5 65 70 6c 61 79 65 64 20 74 6f 20 72 65 73 74 6f  eplayed to resto
11ec6 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  re the contents 
11ec7 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
11ec8 66 69 6c 65 20 28 61 73 20 69 66 0a 2a 2a 20 69  file (as if.** i
11ec9 74 20 77 65 72 65 20 61 20 68 6f 74 2d 6a 6f 75  t were a hot-jou
11eca 72 6e 61 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  rnal)..*/.static
11ecb 20 69 6e 74 20 70 61 67 65 72 5f 65 72 72 6f 72   int pager_error
11ecc 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
11ecd 69 6e 74 20 72 63 29 7b 0a 20 20 69 6e 74 20 72  int rc){.  int r
11ece 63 32 20 3d 20 72 63 20 26 20 30 78 66 66 3b 0a  c2 = rc & 0xff;.
11ecf 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
11ed0 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 4d 45 4d 44  LITE_OK || !MEMD
11ed1 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 0a 20  B );.  assert(. 
11ed2 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72        pPager->er
11ed3 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55  rCode==SQLITE_FU
11ed4 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 50 61  LL ||.       pPa
11ed5 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51  ger->errCode==SQ
11ed6 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20  LITE_OK ||.     
11ed7 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f    (pPager->errCo
11ed8 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c 49  de & 0xff)==SQLI
11ed9 54 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20 20  TE_IOERR.  );.  
11eda 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f  if( rc2==SQLITE_
11edb 46 55 4c 4c 20 7c 7c 20 72 63 32 3d 3d 53 51 4c  FULL || rc2==SQL
11edc 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20  ITE_IOERR ){.   
11edd 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11ede 20 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74   = rc;.  }.  ret
11edf 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
11ee0 20 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62   Execute a rollb
11ee1 61 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63  ack if a transac
11ee2 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61  tion is active a
11ee3 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a  nd unlock the .*
11ee4 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  * database file.
11ee5 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70   .**.** If the p
11ee6 61 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 79  ager has already
11ee7 20 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72   entered the err
11ee8 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74  or state, do not
11ee9 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20 74 68 65   attempt .** the
11eea 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 74 68 69   rollback at thi
11eeb 73 20 74 69 6d 65 2e 20 49 6e 73 74 65 61 64 2c  s time. Instead,
11eec 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20   pager_unlock() 
11eed 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 0a 2a  is called. The.*
11eee 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f  * call to pager_
11eef 75 6e 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 64 69  unlock() will di
11ef0 73 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d  scard all in-mem
11ef1 6f 72 79 20 70 61 67 65 73 2c 20 75 6e 6c 6f 63  ory pages, unloc
11ef2 6b 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  k.** the databas
11ef3 65 20 66 69 6c 65 20 61 6e 64 20 63 6c 65 61 72  e file and clear
11ef4 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
11ef5 2e 20 49 66 20 74 68 69 73 20 6d 65 61 6e 73 20  . If this means 
11ef6 74 68 61 74 0a 2a 2a 20 74 68 65 72 65 20 69 73  that.** there is
11ef7 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6c   a hot-journal l
11ef8 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d  eft in the file-
11ef9 73 79 73 74 65 6d 2c 20 74 68 65 20 6e 65 78 74  system, the next
11efa 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 74   connection.** t
11efb 6f 20 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65  o obtain a share
11efc 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61  d lock on the pa
11efd 67 65 72 20 28 77 68 69 63 68 20 6d 61 79 20 62  ger (which may b
11efe 65 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c  e this one) will
11eff 0a 2a 2a 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b  .** roll it back
11f00 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
11f01 61 67 65 72 20 68 61 73 20 6e 6f 74 20 61 6c 72  ager has not alr
11f02 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65  eady entered the
11f03 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 62 75   error state, bu
11f04 74 20 61 6e 20 49 4f 20 6f 72 0a 2a 2a 20 6d 61  t an IO or.** ma
11f05 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72  lloc error occur
11f06 73 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62  s during a rollb
11f07 61 63 6b 2c 20 74 68 65 6e 20 74 68 69 73 20 77  ack, then this w
11f08 69 6c 6c 20 69 74 73 65 6c 66 20 63 61 75 73 65  ill itself cause
11f09 20 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 74   .** the pager t
11f0a 6f 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f  o enter the erro
11f0b 72 20 73 74 61 74 65 2e 20 57 68 69 63 68 20 77  r state. Which w
11f0c 69 6c 6c 20 62 65 20 63 6c 65 61 72 65 64 20 62  ill be cleared b
11f0d 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f  y the.** call to
11f0e 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 2c   pager_unlock(),
11f0f 20 61 73 20 64 65 73 63 72 69 62 65 64 20 61 62   as described ab
11f10 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ove..*/.static v
11f11 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41  oid pagerUnlockA
11f12 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72  ndRollback(Pager
11f13 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28   *pPager){.  if(
11f14 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11f15 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
11f16 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
11f17 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a  GER_RESERVED ){.
11f18 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
11f19 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
11f1a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
11f1b 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29  Rollback(pPager)
11f1c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64  ;.    sqlite3End
11f1d 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
11f1e 20 20 7d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f    }.  pager_unlo
11f1f 63 6b 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f  ck(pPager);.}../
11f20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
11f21 65 20 65 6e 64 73 20 61 20 74 72 61 6e 73 61 63  e ends a transac
11f22 74 69 6f 6e 2e 20 41 20 74 72 61 6e 73 61 63 74  tion. A transact
11f23 69 6f 6e 20 69 73 20 75 73 75 61 6c 6c 79 20 65  ion is usually e
11f24 6e 64 65 64 20 62 79 20 0a 2a 2a 20 65 69 74 68  nded by .** eith
11f25 65 72 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61  er a COMMIT or a
11f26 20 52 4f 4c 4c 42 41 43 4b 20 6f 70 65 72 61 74   ROLLBACK operat
11f27 69 6f 6e 2e 20 54 68 69 73 20 72 6f 75 74 69 6e  ion. This routin
11f28 65 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20  e may be called 
11f29 0a 2a 2a 20 61 66 74 65 72 20 72 6f 6c 6c 62 61  .** after rollba
11f2a 63 6b 20 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72  ck of a hot-jour
11f2b 6e 61 6c 2c 20 6f 72 20 69 66 20 61 6e 20 65 72  nal, or if an er
11f2c 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ror occurs while
11f2d 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 74 68 65 20   opening.** the 
11f2e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20  journal file or 
11f2f 77 72 69 74 69 6e 67 20 74 68 65 20 76 65 72 79  writing the very
11f30 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d 68   first journal-h
11f31 65 61 64 65 72 20 6f 66 20 61 0a 2a 2a 20 64 61  eader of a.** da
11f32 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69  tabase transacti
11f33 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68  on..** .** If th
11f34 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41  e pager is in PA
11f35 47 45 52 5f 53 48 41 52 45 44 20 6f 72 20 50 41  GER_SHARED or PA
11f36 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65  GER_UNLOCK state
11f37 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f   when this.** ro
11f38 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
11f39 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 20 28   it is a no-op (
11f3a 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
11f3b 4b 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  K)..**.** Otherw
11f3c 69 73 65 2c 20 61 6e 79 20 61 63 74 69 76 65 20  ise, any active 
11f3d 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 72  savepoints are r
11f3e 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eleased..**.** I
11f3f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
11f40 6c 65 20 69 73 20 6f 70 65 6e 2c 20 74 68 65 6e  le is open, then
11f41 20 69 74 20 69 73 20 22 66 69 6e 61 6c 69 7a 65   it is "finalize
11f42 64 22 2e 20 4f 6e 63 65 20 61 20 6a 6f 75 72 6e  d". Once a journ
11f43 61 6c 20 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20  al .** file has 
11f44 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 20 69  been finalized i
11f45 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
11f46 65 20 74 6f 20 75 73 65 20 69 74 20 74 6f 20 72  e to use it to r
11f47 6f 6c 6c 20 62 61 63 6b 20 61 20 0a 2a 2a 20 74  oll back a .** t
11f48 72 61 6e 73 61 63 74 69 6f 6e 2e 20 4e 6f 72 20  ransaction. Nor 
11f49 77 69 6c 6c 20 69 74 20 62 65 20 63 6f 6e 73 69  will it be consi
11f4a 64 65 72 65 64 20 74 6f 20 62 65 20 61 20 68 6f  dered to be a ho
11f4b 74 2d 6a 6f 75 72 6e 61 6c 20 62 79 20 74 68 69  t-journal by thi
11f4c 73 0a 2a 2a 20 6f 72 20 61 6e 79 20 6f 74 68 65  s.** or any othe
11f4d 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
11f4e 63 74 69 6f 6e 2e 20 45 78 61 63 74 6c 79 20 68  ction. Exactly h
11f4f 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20  ow a journal is 
11f50 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a 20 64 65 70  finalized.** dep
11f51 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72 20  ends on whether 
11f52 6f 72 20 6e 6f 74 20 74 68 65 20 70 61 67 65 72  or not the pager
11f53 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65   is running in e
11f54 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e  xclusive mode an
11f55 64 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74  d.** the current
11f56 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 28 50   journal-mode (P
11f57 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ager.journalMode
11f58 20 76 61 6c 75 65 29 2c 20 61 73 20 66 6f 6c 6c   value), as foll
11f59 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75  ows:.**.**   jou
11f5a 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59  rnalMode==MEMORY
11f5b 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20  .**     Journal 
11f5c 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
11f5d 69 73 20 73 69 6d 70 6c 79 20 63 6c 6f 73 65 64  is simply closed
11f5e 2e 20 54 68 69 73 20 64 65 73 74 72 6f 79 73 20  . This destroys 
11f5f 61 6e 20 0a 2a 2a 20 20 20 20 20 69 6e 2d 6d 65  an .**     in-me
11f60 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a  mory journal..**
11f61 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64  .**   journalMod
11f62 65 3d 3d 54 52 55 4e 43 41 54 45 0a 2a 2a 20 20  e==TRUNCATE.**  
11f63 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20     Journal file 
11f64 69 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20  is truncated to 
11f65 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69  zero bytes in si
11f66 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72  ze..**.**   jour
11f67 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54  nalMode==PERSIST
11f68 0a 2a 2a 20 20 20 20 20 54 68 65 20 66 69 72 73  .**     The firs
11f69 74 20 32 38 20 62 79 74 65 73 20 6f 66 20 74 68  t 28 bytes of th
11f6a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
11f6b 72 65 20 7a 65 72 6f 65 64 2e 20 54 68 69 73 20  re zeroed. This 
11f6c 69 6e 76 61 6c 69 64 61 74 65 73 0a 2a 2a 20 20  invalidates.**  
11f6d 20 20 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75     the first jou
11f6e 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 74  rnal header in t
11f6f 68 65 20 66 69 6c 65 2c 20 61 6e 64 20 68 65 6e  he file, and hen
11f70 63 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f  ce the entire jo
11f71 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c  urnal.**     fil
11f72 65 2e 20 41 6e 20 69 6e 76 61 6c 69 64 20 6a 6f  e. An invalid jo
11f73 75 72 6e 61 6c 20 66 69 6c 65 20 63 61 6e 6e 6f  urnal file canno
11f74 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  t be rolled back
11f75 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61  ..**.**   journa
11f76 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54 45 0a 2a 2a  lMode==DELETE.**
11f77 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c       The journal
11f78 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 20   file is closed 
11f79 61 6e 64 20 64 65 6c 65 74 65 64 20 75 73 69 6e  and deleted usin
11f7a 67 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74  g sqlite3OsDelet
11f7b 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49  e()..**.**     I
11f7c 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72  f the pager is r
11f7d 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73  unning in exclus
11f7e 69 76 65 20 6d 6f 64 65 2c 20 74 68 69 73 20 6d  ive mode, this m
11f7f 65 74 68 6f 64 20 6f 66 20 66 69 6e 61 6c 69 7a  ethod of finaliz
11f80 69 6e 67 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a  ing.**     the j
11f81 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
11f82 65 76 65 72 20 75 73 65 64 2e 20 49 6e 73 74 65  ever used. Inste
11f83 61 64 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e  ad, if the journ
11f84 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a 20 20 20 20  alMode is.**    
11f85 20 44 45 4c 45 54 45 20 61 6e 64 20 74 68 65 20   DELETE and the 
11f86 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c  pager is in excl
11f87 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 20  usive mode, the 
11f88 6d 65 74 68 6f 64 20 64 65 73 63 72 69 62 65 64  method described
11f89 20 75 6e 64 65 72 0a 2a 2a 20 20 20 20 20 6a 6f   under.**     jo
11f8a 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49  urnalMode==PERSI
11f8b 53 54 20 69 73 20 75 73 65 64 20 69 6e 73 74 65  ST is used inste
11f8c 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  ad..**.** After 
11f8d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66  the journal is f
11f8e 69 6e 61 6c 69 7a 65 64 2c 20 69 66 20 72 75 6e  inalized, if run
11f8f 6e 69 6e 67 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c  ning in non-excl
11f90 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 0a  usive mode, the.
11f91 2a 2a 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74  ** pager moves t
11f92 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73  o PAGER_SHARED s
11f93 74 61 74 65 20 28 61 6e 64 20 64 6f 77 6e 67 72  tate (and downgr
11f94 61 64 65 73 20 74 68 65 20 6c 6f 63 6b 20 6f 6e  ades the lock on
11f95 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
11f96 20 66 69 6c 65 20 61 63 63 6f 72 64 69 6e 67 6c   file accordingl
11f97 79 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  y)..**.** If the
11f98 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e   pager is runnin
11f99 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  g in exclusive m
11f9a 6f 64 65 20 61 6e 64 20 69 73 20 69 6e 20 50 41  ode and is in PA
11f9b 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65  GER_SYNCED state
11f9c 2c 0a 2a 2a 20 69 74 20 6d 6f 76 65 73 20 74 6f  ,.** it moves to
11f9d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
11f9e 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 64  . No locks are d
11f9f 6f 77 6e 67 72 61 64 65 64 20 77 68 65 6e 20 72  owngraded when r
11fa0 75 6e 6e 69 6e 67 20 69 6e 0a 2a 2a 20 65 78 63  unning in.** exc
11fa1 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a  lusive mode..**.
11fa2 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
11fa3 72 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 65  returned if no e
11fa4 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 49 66 20  rror occurs. If 
11fa5 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
11fa6 64 75 72 69 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66  during.** any of
11fa7 20 74 68 65 20 49 4f 20 6f 70 65 72 61 74 69 6f   the IO operatio
11fa8 6e 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74  ns to finalize t
11fa9 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11faa 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a  or unlock the.**
11fab 20 64 61 74 61 62 61 73 65 20 74 68 65 6e 20 74   database then t
11fac 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  he IO error code
11fad 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
11fae 74 68 65 20 75 73 65 72 2e 20 49 66 20 74 68 65  the user. If the
11faf 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74   .** operation t
11fb0 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a  o finalize the j
11fb1 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c  ournal file fail
11fb2 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 64 65  s, then the code
11fb3 20 73 74 69 6c 6c 0a 2a 2a 20 74 72 69 65 73 20   still.** tries 
11fb4 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61  to unlock the da
11fb5 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 6e  tabase file if n
11fb6 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  ot in exclusive 
11fb7 6d 6f 64 65 2e 20 49 66 20 74 68 65 0a 2a 2a 20  mode. If the.** 
11fb8 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e  unlock operation
11fb9 20 66 61 69 6c 73 20 61 73 20 77 65 6c 6c 2c 20   fails as well, 
11fba 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 65  then the first e
11fbb 72 72 6f 72 20 63 6f 64 65 20 72 65 6c 61 74 65  rror code relate
11fbc 64 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73  d.** to the firs
11fbd 74 20 65 72 72 6f 72 20 65 6e 63 6f 75 6e 74 65  t error encounte
11fbe 72 65 64 20 28 74 68 65 20 6a 6f 75 72 6e 61 6c  red (the journal
11fbf 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e   finalization on
11fc0 65 29 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65  e) is.** returne
11fc1 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
11fc2 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73   pager_end_trans
11fc3 61 63 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50  action(Pager *pP
11fc4 61 67 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73  ager, int hasMas
11fc5 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ter){.  int rc =
11fc6 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
11fc7 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66   /* Error code f
11fc8 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61  rom journal fina
11fc9 6c 69 7a 61 74 69 6f 6e 20 6f 70 65 72 61 74 69  lization operati
11fca 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 32 20  on */.  int rc2 
11fcb 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
11fcc 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66   /* Error code f
11fcd 72 6f 6d 20 64 62 20 66 69 6c 65 20 75 6e 6c 6f  rom db file unlo
11fce 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ck operation */.
11fcf 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
11fd0 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52  tate<PAGER_RESER
11fd1 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72  VED ){.    retur
11fd2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
11fd3 0a 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76  .  releaseAllSav
11fd4 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b  epoints(pPager);
11fd5 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ..  assert( isOp
11fd6 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
11fd7 7c 7c 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  || pPager->pInJo
11fd8 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 69 66  urnal==0 );.  if
11fd9 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
11fda 3e 6a 66 64 29 20 29 7b 0a 0a 20 20 20 20 2f 2a  >jfd) ){..    /*
11fdb 20 46 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f   Finalize the jo
11fdc 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20  urnal file. */. 
11fdd 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
11fde 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  MemJournal(pPage
11fdf 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20  r->jfd) ){.     
11fe0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
11fe1 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
11fe2 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
11fe3 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 20 20 20 20  MEMORY );.      
11fe4 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
11fe5 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20  Pager->jfd);.   
11fe6 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65   }else if( pPage
11fe7 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
11fe8 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
11fe9 45 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20 20  E_TRUNCATE ){.  
11fea 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
11feb 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b  journalOff==0 ){
11fec 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
11fed 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
11fee 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
11fef 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e   = sqlite3OsTrun
11ff0 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  cate(pPager->jfd
11ff1 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
11ff2 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
11ff3 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20  nalOff = 0;.    
11ff4 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11ff5 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20  lStarted = 0;.  
11ff6 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67    }else if( pPag
11ff7 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
11ff8 65 20 0a 20 20 20 20 20 7c 7c 20 70 50 61 67 65  e .     || pPage
11ff9 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
11ffa 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
11ffb 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 29 7b  E_PERSIST.    ){
11ffc 0a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72 6f  .      rc = zero
11ffd 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
11ffe 72 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a 20  r, hasMaster);. 
11fff 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72       pager_error
12000 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20  (pPager, rc);.  
12001 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
12002 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20  nalOff = 0;.    
12003 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
12004 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20  lStarted = 0;.  
12005 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
12006 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 6d 61  * This branch ma
12007 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77 69  y be executed wi
12008 74 68 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  th Pager.journal
12009 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 20 69 66 0a  Mode==MEMORY if.
1200a 20 20 20 20 20 20 2a 2a 20 61 20 68 6f 74 2d 6a        ** a hot-j
1200b 6f 75 72 6e 61 6c 20 77 61 73 20 6a 75 73 74 20  ournal was just 
1200c 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 6e 20  rolled back. In 
1200d 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f  this case the jo
1200e 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 66  urnal.      ** f
1200f 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c  ile should be cl
12010 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64  osed and deleted
12011 2e 20 49 66 20 74 68 69 73 20 63 6f 6e 6e 65 63  . If this connec
12012 74 69 6f 6e 20 77 72 69 74 65 73 20 74 6f 0a 20  tion writes to. 
12013 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61       ** the data
12014 62 61 73 65 20 66 69 6c 65 2c 20 69 74 20 77 69  base file, it wi
12015 6c 6c 20 64 6f 20 73 6f 20 75 73 69 6e 67 20 61  ll do so using a
12016 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
12017 6e 61 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61  nal.  */.      a
12018 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a  ssert( pPager->j
12019 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
1201a 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45  R_JOURNALMODE_DE
1201b 4c 45 54 45 20 0a 20 20 20 20 20 20 20 20 20 20  LETE .          
1201c 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
1201d 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
1201e 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
1201f 59 20 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  Y .      );.    
12020 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
12021 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
12022 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72       if( !pPager
12023 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20  ->tempFile ){.  
12024 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12025 65 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65  e3OsDelete(pPage
12026 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d  r->pVfs, pPager-
12027 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20  >zJournal, 0);. 
12028 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69       }.    }..#i
12029 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
1202a 4b 5f 50 41 47 45 53 0a 20 20 20 20 73 71 6c 69  K_PAGES.    sqli
1202b 74 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65  te3PcacheIterate
1202c 44 69 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50  Dirty(pPager->pP
1202d 43 61 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74  Cache, pager_set
1202e 5f 70 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64  _pagehash);.#end
1202f 69 66 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50  if..    sqlite3P
12030 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50  cacheCleanAll(pP
12031 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
12032 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
12033 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d  cDestroy(pPager-
12034 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20  >pInJournal);.  
12035 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75    pPager->pInJou
12036 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 70 50  rnal = 0;.    pP
12037 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a  ager->nRec = 0;.
12038 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67    }..  if( !pPag
12039 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
1203a 65 20 29 7b 0a 20 20 20 20 72 63 32 20 3d 20 6f  e ){.    rc2 = o
1203b 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e  sUnlock(pPager->
1203c 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29  fd, SHARED_LOCK)
1203d 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ;.    pPager->st
1203e 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52  ate = PAGER_SHAR
1203f 45 44 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ED;.    pPager->
12040 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20  changeCountDone 
12041 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
12042 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
12043 50 41 47 45 52 5f 53 59 4e 43 45 44 20 29 7b 0a  PAGER_SYNCED ){.
12044 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
12045 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53  e = PAGER_EXCLUS
12046 49 56 45 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  IVE;.  }.  pPage
12047 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30  r->setMaster = 0
12048 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64  ;.  pPager->need
12049 53 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67  Sync = 0;.  pPag
1204a 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d  er->dbModified =
1204b 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20   0;..  /* TODO: 
1204c 49 73 20 74 68 69 73 20 6f 70 74 69 6d 61 6c 3f  Is this optimal?
1204d 20 57 68 79 20 69 73 20 74 68 65 20 64 62 20 73   Why is the db s
1204e 69 7a 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20  ize invalidated 
1204f 68 65 72 65 20 0a 20 20 2a 2a 20 77 68 65 6e 20  here .  ** when 
12050 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12051 65 20 69 73 20 6e 6f 74 20 75 6e 6c 6f 63 6b 65  e is not unlocke
12052 64 3f 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e  d? */.  pPager->
12053 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 30 3b 0a  dbOrigSize = 0;.
12054 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54    sqlite3PcacheT
12055 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
12056 70 50 43 61 63 68 65 2c 20 70 50 61 67 65 72 2d  pPCache, pPager-
12057 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  >dbSize);.  if( 
12058 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50  !MEMDB ){.    pP
12059 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
1205a 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65  d = 0;.  }..  re
1205b 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45  turn (rc==SQLITE
1205c 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a  _OK?rc2:rc);.}..
1205d 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20  /*.** Parameter 
1205e 61 44 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74  aData must point
1205f 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20   to a buffer of 
12060 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12061 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74   bytes.** of dat
12062 61 2e 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 72  a. Compute and r
12063 65 74 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d  eturn a checksum
12064 20 62 61 73 65 64 20 6f 6e 74 20 74 68 65 20 63   based ont the c
12065 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 0a  ontents of the .
12066 2a 2a 20 70 61 67 65 20 6f 66 20 64 61 74 61 20  ** page of data 
12067 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  and the current 
12068 76 61 6c 75 65 20 6f 66 20 70 50 61 67 65 72 2d  value of pPager-
12069 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a  >cksumInit..**.*
1206a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20  * This is not a 
1206b 72 65 61 6c 20 63 68 65 63 6b 73 75 6d 2e 20 49  real checksum. I
1206c 74 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74  t is really just
1206d 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20   the sum of the 
1206e 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69  .** random initi
1206f 61 6c 20 76 61 6c 75 65 20 28 70 50 61 67 65 72  al value (pPager
12070 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 20 61 6e 64  ->cksumInit) and
12071 20 65 76 65 72 79 20 32 30 30 74 68 20 62 79 74   every 200th byt
12072 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65  e.** of the page
12073 20 64 61 74 61 2c 20 73 74 61 72 74 69 6e 67 20   data, starting 
12074 77 69 74 68 20 62 79 74 65 20 6f 66 66 73 65 74  with byte offset
12075 20 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69   (pPager->pageSi
12076 7a 65 25 32 30 30 29 2e 0a 2a 2a 20 45 61 63 68  ze%200)..** Each
12077 20 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72   byte is interpr
12078 65 74 65 64 20 61 73 20 61 6e 20 38 2d 62 69 74  eted as an 8-bit
12079 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
1207a 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e  r..**.** Changin
1207b 67 20 74 68 65 20 66 6f 72 6d 75 6c 61 20 75 73  g the formula us
1207c 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68  ed to compute th
1207d 69 73 20 63 68 65 63 6b 73 75 6d 20 72 65 73 75  is checksum resu
1207e 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63  lts in an.** inc
1207f 6f 6d 70 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61  ompatible journa
12080 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a  l file format..*
12081 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20  *.** If journal 
12082 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72  corruption occur
12083 73 20 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72  s due to a power
12084 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f   failure, the mo
12085 73 74 20 6c 69 6b 65 6c 79 20 0a 2a 2a 20 73 63  st likely .** sc
12086 65 6e 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f  enario is that o
12087 6e 65 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74  ne end or the ot
12088 68 65 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72  her of the recor
12089 64 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65  d will be change
1208a 64 2e 20 0a 2a 2a 20 49 74 20 69 73 20 6d 75 63  d. .** It is muc
1208b 68 20 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68  h less likely th
1208c 61 74 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20  at the two ends 
1208d 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72  of the journal r
1208e 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a  ecord will be.**
1208f 20 63 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65   correct and the
12090 20 6d 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75   middle be corru
12091 70 74 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20  pt.  Thus, this 
12092 22 63 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d  "checksum" schem
12093 65 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73  e,.** though fas
12094 74 20 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61  t and simple, ca
12095 74 63 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79  tches the mostly
12096 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20   likely kind of 
12097 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73  corruption..*/.s
12098 74 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f  tatic u32 pager_
12099 63 6b 73 75 6d 28 50 61 67 65 72 20 2a 70 50 61  cksum(Pager *pPa
1209a 67 65 72 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61  ger, const u8 *a
1209b 44 61 74 61 29 7b 0a 20 20 75 33 32 20 63 6b 73  Data){.  u32 cks
1209c 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73  um = pPager->cks
1209d 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  umInit;         
1209e 2f 2a 20 43 68 65 63 6b 73 75 6d 20 76 61 6c 75  /* Checksum valu
1209f 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20  e to return */. 
120a0 20 69 6e 74 20 69 20 3d 20 70 50 61 67 65 72 2d   int i = pPager-
120a1 3e 70 61 67 65 53 69 7a 65 2d 32 30 30 3b 20 20  >pageSize-200;  
120a2 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
120a3 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 77 68 69  counter */.  whi
120a4 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63  le( i>0 ){.    c
120a5 6b 73 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d  ksum += aData[i]
120a6 3b 0a 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a  ;.    i -= 200;.
120a7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73    }.  return cks
120a8 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  um;.}../*.** Rea
120a9 64 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  d a single page 
120aa 66 72 6f 6d 20 65 69 74 68 65 72 20 74 68 65 20  from either the 
120ab 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 69 66  journal file (if
120ac 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 31 29 20   isMainJrnl==1) 
120ad 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73  or.** from the s
120ae 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 69 66 20 69  ub-journal (if i
120af 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 29 20 61 6e  sMainJrnl==0) an
120b0 64 20 70 6c 61 79 62 61 63 6b 20 74 68 61 74 20  d playback that 
120b1 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 70 61 67  page..** The pag
120b2 65 20 62 65 67 69 6e 73 20 61 74 20 6f 66 66 73  e begins at offs
120b3 65 74 20 2a 70 4f 66 66 73 65 74 20 69 6e 74 6f  et *pOffset into
120b4 20 74 68 65 20 66 69 6c 65 2e 20 54 68 65 20 2a   the file. The *
120b5 70 4f 66 66 73 65 74 0a 2a 2a 20 76 61 6c 75 65  pOffset.** value
120b6 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 74 6f   is increased to
120b7 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
120b8 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74  e next page in t
120b9 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a  he journal..**.*
120ba 2a 20 54 68 65 20 69 73 4d 61 69 6e 4a 72 6e 6c  * The isMainJrnl
120bb 20 66 6c 61 67 20 69 73 20 74 72 75 65 20 69 66   flag is true if
120bc 20 74 68 69 73 20 69 73 20 74 68 65 20 6d 61 69   this is the mai
120bd 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  n rollback journ
120be 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20  al and.** false 
120bf 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e  for the statemen
120c0 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20  t journal.  The 
120c1 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  main rollback jo
120c2 75 72 6e 61 6c 20 75 73 65 73 0a 2a 2a 20 63 68  urnal uses.** ch
120c3 65 63 6b 73 75 6d 73 20 2d 20 74 68 65 20 73 74  ecksums - the st
120c4 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
120c5 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20  does not..**.** 
120c6 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  If the page numb
120c7 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 72  er of the page r
120c8 65 63 6f 72 64 20 72 65 61 64 20 66 72 6f 6d 20  ecord read from 
120c9 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61  the (sub-)journa
120ca 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 67 72 65  l file.** is gre
120cb 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75  ater than the cu
120cc 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50  rrent value of P
120cd 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 65  ager.dbSize, the
120ce 6e 20 70 6c 61 79 62 61 63 6b 20 69 73 0a 2a 2a  n playback is.**
120cf 20 73 6b 69 70 70 65 64 20 61 6e 64 20 53 51 4c   skipped and SQL
120d0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
120d1 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f  ed..**.** If pDo
120d2 6e 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  ne is not NULL, 
120d3 74 68 65 6e 20 69 74 20 69 73 20 61 20 72 65 63  then it is a rec
120d4 6f 72 64 20 6f 66 20 70 61 67 65 73 20 74 68 61  ord of pages tha
120d5 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a  t have already.*
120d6 2a 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61  * been played ba
120d7 63 6b 2e 20 20 49 66 20 74 68 65 20 70 61 67 65  ck.  If the page
120d8 20 61 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73   at *pOffset has
120d9 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c   already been pl
120da 61 79 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66  ayed back.** (if
120db 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
120dc 6e 67 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20  ng pDone bit is 
120dd 73 65 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74  set) then skip t
120de 68 65 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20  he playback..** 
120df 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44  Make sure the pD
120e0 6f 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f  one bit correspo
120e1 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f  nding to the *pO
120e2 66 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65  ffset page is se
120e3 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65  t.** prior to re
120e4 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49  turning..**.** I
120e5 66 20 74 68 65 20 70 61 67 65 20 72 65 63 6f 72  f the page recor
120e6 64 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c  d is successfull
120e7 79 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  y read from the 
120e8 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69  (sub-)journal fi
120e9 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c 61 79 65 64  le.** and played
120ea 20 62 61 63 6b 2c 20 74 68 65 6e 20 53 51 4c 49   back, then SQLI
120eb 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
120ec 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  d. If an IO erro
120ed 72 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68 69 6c  r occurs.** whil
120ee 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 72 65  e reading the re
120ef 63 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 28 73  cord from the (s
120f0 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ub-)journal file
120f1 20 6f 72 20 77 68 69 6c 65 20 77 72 69 74 69 6e   or while writin
120f2 67 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61  g.** to the data
120f3 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20  base file, then 
120f4 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64  the IO error cod
120f5 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  e is returned. I
120f6 66 20 64 61 74 61 0a 2a 2a 20 69 73 20 73 75 63  f data.** is suc
120f7 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66  cessfully read f
120f8 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f  rom the (sub-)jo
120f9 75 72 6e 61 6c 20 66 69 6c 65 20 62 75 74 20 61  urnal file but a
120fa 70 70 65 61 72 73 20 74 6f 20 62 65 0a 2a 2a 20  ppears to be.** 
120fb 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54  corrupted, SQLIT
120fc 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e  E_DONE is return
120fd 65 64 2e 20 44 61 74 61 20 69 73 20 63 6f 6e 73  ed. Data is cons
120fe 69 64 65 72 65 64 20 63 6f 72 72 75 70 74 65 64  idered corrupted
120ff 20 69 6e 0a 2a 2a 20 74 77 6f 20 63 69 72 63 75   in.** two circu
12100 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a  mstances:.** .**
12101 20 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f     * If the reco
12102 72 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69  rd page-number i
12103 73 20 69 6c 6c 65 67 61 6c 20 28 30 20 6f 72 20  s illegal (0 or 
12104 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20  PAGER_MJ_PGNO), 
12105 6f 72 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65  or.**   * If the
12106 20 72 65 63 6f 72 64 20 69 73 20 62 65 69 6e 67   record is being
12107 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 66 72 6f   rolled back fro
12108 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  m the main journ
12109 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61  al file.**     a
1210a 6e 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20  nd the checksum 
1210b 66 69 65 6c 64 20 64 6f 65 73 20 6e 6f 74 20 6d  field does not m
1210c 61 74 63 68 20 74 68 65 20 72 65 63 6f 72 64 20  atch the record 
1210d 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e  content..**.** N
1210e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20  either of these 
1210f 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72  two scenarios ar
12110 65 20 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e  e possible durin
12111 67 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f  g a savepoint ro
12112 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  llback..**.** If
12113 20 74 68 69 73 20 69 73 20 61 20 73 61 76 65 70   this is a savep
12114 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74  oint rollback, t
12115 68 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68  hen memory may h
12116 61 76 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69  ave to be dynami
12117 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  cally.** allocat
12118 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
12119 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20  ion. If this is 
1211a 74 68 65 20 63 61 73 65 20 61 6e 64 20 61 6e 20  the case and an 
1211b 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
1211c 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ,.** SQLITE_NOME
1211d 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  M is returned..*
1211e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
1211f 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f  er_playback_one_
12120 70 61 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70  page(.  Pager *p
12121 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20  Pager,          
12122 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
12123 65 72 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20  er being played 
12124 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73  back */.  int is
12125 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20 20 20 20 20  MainJrnl,       
12126 20 20 20 20 20 20 20 20 2f 2a 20 31 20 2d 3e 20          /* 1 -> 
12127 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2e 20 30 20  main journal. 0 
12128 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20  -> sub-journal. 
12129 2a 2f 0a 20 20 69 6e 74 20 69 73 55 6e 73 79 6e  */.  int isUnsyn
1212a 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
1212b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65     /* True if re
1212c 61 64 69 6e 67 20 66 72 6f 6d 20 75 6e 73 79 6e  ading from unsyn
1212d 63 65 64 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  ced main journal
1212e 20 2a 2f 0a 20 20 69 36 34 20 2a 70 4f 66 66 73   */.  i64 *pOffs
1212f 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  et,             
12130 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66      /* Offset of
12131 20 72 65 63 6f 72 64 20 74 6f 20 70 6c 61 79 62   record to playb
12132 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53  ack */.  int isS
12133 61 76 65 70 6e 74 2c 20 20 20 20 20 20 20 20 20  avepnt,         
12134 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
12135 6f 72 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72  or a savepoint r
12136 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 42 69 74  ollback */.  Bit
12137 76 65 63 20 2a 70 44 6f 6e 65 20 20 20 20 20 20  vec *pDone      
12138 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69             /* Bi
12139 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c  tvec of pages al
1213a 72 65 61 64 79 20 70 6c 61 79 65 64 20 62 61 63  ready played bac
1213b 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  k */.){.  int rc
1213c 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20  ;.  PgHdr *pPg; 
1213d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1213e 20 20 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67    /* An existing
1213f 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63   page in the cac
12140 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  he */.  Pgno pgn
12141 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o;              
12142 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
12143 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61  e number of a pa
12144 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f  ge in journal */
12145 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20  .  u32 cksum;   
12146 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12147 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65   /* Checksum use
12148 64 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65  d for sanity che
12149 63 6b 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20  cking */.  char 
1214a 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *aData;         
1214b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
1214c 6f 72 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f  orary storage fo
1214d 72 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20  r the page */.  
1214e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66  sqlite3_file *jf
1214f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  d;            /*
12150 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   The file descri
12151 70 74 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75  ptor for the jou
12152 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20  rnal file */..  
12153 61 73 73 65 72 74 28 20 28 69 73 4d 61 69 6e 4a  assert( (isMainJ
12154 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20  rnl&~1)==0 );   
12155 20 20 20 2f 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c     /* isMainJrnl
12156 20 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20   is 0 or 1 */.  
12157 61 73 73 65 72 74 28 20 28 69 73 53 61 76 65 70  assert( (isSavep
12158 6e 74 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20  nt&~1)==0 );    
12159 20 20 20 2f 2a 20 69 73 53 61 76 65 70 6e 74 20     /* isSavepnt 
1215a 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61  is 0 or 1 */.  a
1215b 73 73 65 72 74 28 20 69 73 4d 61 69 6e 4a 72 6e  ssert( isMainJrn
1215c 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 3b 20 20 20  l || pDone );   
1215d 20 20 2f 2a 20 70 44 6f 6e 65 20 61 6c 77 61 79    /* pDone alway
1215e 73 20 75 73 65 64 20 6f 6e 20 73 75 62 2d 6a 6f  s used on sub-jo
1215f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 61 73 73 65  urnals */.  asse
12160 72 74 28 20 69 73 53 61 76 65 70 6e 74 20 7c 7c  rt( isSavepnt ||
12161 20 70 44 6f 6e 65 3d 3d 30 20 29 3b 20 20 20 2f   pDone==0 );   /
12162 2a 20 70 44 6f 6e 65 20 6e 65 76 65 72 20 75 73  * pDone never us
12163 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f  ed on non-savepo
12164 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 61 74 61 20  int */..  aData 
12165 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70  = pPager->pTmpSp
12166 61 63 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61  ace;.  assert( a
12167 44 61 74 61 20 29 3b 20 20 20 20 20 20 20 20 20  Data );         
12168 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20  /* Temp storage 
12169 6d 75 73 74 20 68 61 76 65 20 61 6c 72 65 61 64  must have alread
1216a 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  y been allocated
1216b 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74   */..  /* Read t
1216c 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61  he page number a
1216d 6e 64 20 70 61 67 65 20 64 61 74 61 20 66 72 6f  nd page data fro
1216e 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72  m the journal or
1216f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 20 20 2a   sub-journal.  *
12170 2a 20 66 69 6c 65 2e 20 52 65 74 75 72 6e 20 61  * file. Return a
12171 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20  n error code to 
12172 74 68 65 20 63 61 6c 6c 65 72 20 69 66 20 61 6e  the caller if an
12173 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
12174 2e 0a 20 20 2a 2f 0a 20 20 6a 66 64 20 3d 20 69  ..  */.  jfd = i
12175 73 4d 61 69 6e 4a 72 6e 6c 20 3f 20 70 50 61 67  sMainJrnl ? pPag
12176 65 72 2d 3e 6a 66 64 20 3a 20 70 50 61 67 65 72  er->jfd : pPager
12177 2d 3e 73 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72  ->sjfd;.  rc = r
12178 65 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 2a  ead32bits(jfd, *
12179 70 4f 66 66 73 65 74 2c 20 26 70 67 6e 6f 29 3b  pOffset, &pgno);
1217a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1217b 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
1217c 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1217d 4f 73 52 65 61 64 28 6a 66 64 2c 20 28 75 38 2a  OsRead(jfd, (u8*
1217e 29 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e  )aData, pPager->
1217f 70 61 67 65 53 69 7a 65 2c 20 28 2a 70 4f 66 66  pageSize, (*pOff
12180 73 65 74 29 2b 34 29 3b 0a 20 20 69 66 28 20 72  set)+4);.  if( r
12181 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
12182 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 4f 66  eturn rc;.  *pOf
12183 66 73 65 74 20 2b 3d 20 70 50 61 67 65 72 2d 3e  fset += pPager->
12184 70 61 67 65 53 69 7a 65 20 2b 20 34 20 2b 20 69  pageSize + 4 + i
12185 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b 0a 0a 20 20  sMainJrnl*4;..  
12186 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69  /* Sanity checki
12187 6e 67 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20  ng on the page. 
12188 20 54 68 69 73 20 69 73 20 6d 6f 72 65 20 69 6d   This is more im
12189 70 6f 72 74 61 6e 74 20 74 68 61 74 20 49 20 6f  portant that I o
1218a 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74  riginally.  ** t
1218b 68 6f 75 67 68 74 2e 20 20 49 66 20 61 20 70 6f  hought.  If a po
1218c 77 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75  wer failure occu
1218d 72 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75  rs while the jou
1218e 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 20 77 72  rnal is being wr
1218f 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 69 74 20 63  itten,.  ** it c
12190 6f 75 6c 64 20 63 61 75 73 65 20 69 6e 76 61 6c  ould cause inval
12191 69 64 20 64 61 74 61 20 74 6f 20 62 65 20 77 72  id data to be wr
12192 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
12193 6f 75 72 6e 61 6c 2e 20 20 57 65 20 6e 65 65 64  ournal.  We need
12194 20 74 6f 0a 20 20 2a 2a 20 64 65 74 65 63 74 20   to.  ** detect 
12195 74 68 69 73 20 69 6e 76 61 6c 69 64 20 64 61 74  this invalid dat
12196 61 20 28 77 69 74 68 20 68 69 67 68 20 70 72 6f  a (with high pro
12197 62 61 62 69 6c 69 74 79 29 20 61 6e 64 20 69 67  bability) and ig
12198 6e 6f 72 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20  nore it..  */.  
12199 69 66 28 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70  if( pgno==0 || p
1219a 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47  gno==PAGER_MJ_PG
1219b 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  NO(pPager) ){.  
1219c 20 20 61 73 73 65 72 74 28 20 21 69 73 53 61 76    assert( !isSav
1219d 65 70 6e 74 20 29 3b 0a 20 20 20 20 72 65 74 75  epnt );.    retu
1219e 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
1219f 20 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28    }.  if( pgno>(
121a0 50 67 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53  Pgno)pPager->dbS
121a1 69 7a 65 20 7c 7c 20 73 71 6c 69 74 65 33 42 69  ize || sqlite3Bi
121a2 74 76 65 63 54 65 73 74 28 70 44 6f 6e 65 2c 20  tvecTest(pDone, 
121a3 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 72 65 74  pgno) ){.    ret
121a4 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
121a5 20 7d 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a   }.  if( isMainJ
121a6 72 6e 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  rnl ){.    rc = 
121a7 72 65 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20  read32bits(jfd, 
121a8 28 2a 70 4f 66 66 73 65 74 29 2d 34 2c 20 26 63  (*pOffset)-4, &c
121a9 6b 73 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72  ksum);.    if( r
121aa 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
121ab 20 20 20 69 66 28 20 21 69 73 53 61 76 65 70 6e     if( !isSavepn
121ac 74 20 26 26 20 70 61 67 65 72 5f 63 6b 73 75 6d  t && pager_cksum
121ad 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 61 44  (pPager, (u8*)aD
121ae 61 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20  ata)!=cksum ){. 
121af 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
121b0 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20  TE_DONE;.    }. 
121b1 20 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20   }..  if( pDone 
121b2 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  && (rc = sqlite3
121b3 42 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c  BitvecSet(pDone,
121b4 20 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f   pgno))!=SQLITE_
121b5 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
121b6 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65   rc;.  }..  asse
121b7 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
121b8 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45  e==PAGER_RESERVE
121b9 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61  D || pPager->sta
121ba 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  te>=PAGER_EXCLUS
121bb 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  IVE );..  /* If 
121bc 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
121bd 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20  RESERVED state, 
121be 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20  then there must 
121bf 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69  be a copy of thi
121c0 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74  s.  ** page in t
121c1 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20  he pager cache. 
121c2 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73  In this case jus
121c3 74 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67  t update the pag
121c4 65 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e  er cache,.  ** n
121c5 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ot the database 
121c6 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69  file. The page i
121c7 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69  s left marked di
121c8 72 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65  rty in this case
121c9 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65  ..  **.  ** An e
121ca 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20  xception to the 
121cb 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74  above rule: If t
121cc 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69  he database is i
121cd 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20  n no-sync mode. 
121ce 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69   ** and a page i
121cf 73 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61  s moved during a
121d0 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61  n incremental va
121d1 63 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61  cuum then the pa
121d2 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20  ge may.  ** not 
121d3 62 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20  be in the pager 
121d4 63 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66  cache. Later: if
121d5 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49   a malloc() or I
121d6 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20  O error occurs. 
121d7 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76   ** during a Mov
121d8 65 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68  epage() call, th
121d9 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20  en the page may 
121da 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61  not be in the ca
121db 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e  che.  ** either.
121dc 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f   So the conditio
121dd 6e 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74  n described in t
121de 68 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61  he above paragra
121df 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61  ph is not.  ** a
121e0 73 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a  ssert()able..  *
121e1 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43  *.  ** If in EXC
121e2 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68  LUSIVE state, th
121e3 65 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65  en we update the
121e4 20 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20   pager cache if 
121e5 69 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61  it exists.  ** a
121e6 6e 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65  nd the main file
121e7 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68  . The page is th
121e8 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69  en marked not di
121e9 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  rty..  **.  ** T
121ea 69 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68  icket #1171:  Th
121eb 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  e statement jour
121ec 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69  nal might contai
121ed 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74  n page content t
121ee 68 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66  hat is.  ** diff
121ef 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70  erent from the p
121f0 61 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74  age content at t
121f1 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
121f2 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a  transaction..  *
121f3 2a 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68  * This occurs wh
121f4 65 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61  en a page is cha
121f5 6e 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68  nged prior to th
121f6 65 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61  e start of a sta
121f7 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e  tement.  ** then
121f8 20 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77   changed again w
121f9 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d  ithin the statem
121fa 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69  ent.  When rolli
121fb 6e 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20  ng back such a. 
121fc 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65   ** statement we
121fd 20 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20   must not write 
121fe 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  to the original 
121ff 64 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20  database unless 
12200 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72  we know.  ** for
12201 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72   certain that or
12202 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74  iginal page cont
12203 65 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20  ents are synced 
12204 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f  into the main ro
12205 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72  llback.  ** jour
12206 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c  nal.  Otherwise,
12207 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69   a power loss mi
12208 67 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69  ght leave modifi
12209 65 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20  ed data in the. 
1220a 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
1220b 65 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74  e without an ent
1220c 72 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61  ry in the rollba
1220d 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20  ck journal that 
1220e 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65  can.  ** restore
1220f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f   the database to
12210 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f   its original fo
12211 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69  rm.  Two conditi
12212 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a  ons must be.  **
12213 20 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74   met before writ
12214 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62  ing to the datab
12215 61 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74  ase files. (1) t
12216 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74  he database must
12217 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e   be.  ** locked.
12218 20 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68    (2) we know th
12219 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  at the original 
1221a 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20  page content is 
1221b 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a  fully synced.  *
1221c 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f  * in the main jo
1221d 75 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63  urnal either bec
1221e 61 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73  ause the page is
1221f 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72   not in cache or
12220 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70   else.  ** the p
12221 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73  age is marked as
12222 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20   needSync==0..  
12223 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d  **.  ** 2008-04-
12224 31 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70  14:  When attemp
12225 74 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61  ting to vacuum a
12226 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73   corrupt databas
12227 65 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20  e file, it.  ** 
12228 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66  is possible to f
12229 61 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20  ail a statement 
1222a 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68  on a database th
1222b 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20  at does not yet 
1222c 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e  exist..  ** Do n
1222d 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72  ot attempt to wr
1222e 69 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20  ite if database 
1222f 66 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62  file has never b
12230 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f  een opened..  */
12231 0a 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c  .  pPg = pager_l
12232 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67  ookup(pPager, pg
12233 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  no);.  assert( p
12234 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a  Pg || !MEMDB );.
12235 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50    PAGERTRACE(("P
12236 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20  LAYBACK %d page 
12237 25 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73  %d hash(%08x) %s
12238 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
12239 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
1223a 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 64 61 74   pgno, pager_dat
1223b 61 68 61 73 68 28 70 50 61 67 65 72 2d 3e 70 61  ahash(pPager->pa
1223c 67 65 53 69 7a 65 2c 20 28 75 38 2a 29 61 44 61  geSize, (u8*)aDa
1223d 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ta),.           
1223e 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d 61 69  (isMainJrnl?"mai
1223f 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75 62 2d  n-journal":"sub-
12240 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29 3b 0a  journal").  ));.
12241 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 73    if( (pPager->s
12242 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c  tate>=PAGER_EXCL
12243 55 53 49 56 45 29 0a 20 20 20 26 26 20 28 70 50  USIVE).   && (pP
12244 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50 67 2d  g==0 || 0==(pPg-
12245 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
12246 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26 20 69  D_SYNC)).   && i
12247 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
12248 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73 79 6e  ).   && !isUnsyn
12249 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34 20 6f  c.  ){.    i64 o
1224a 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28  fst = (pgno-1)*(
1224b 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65  i64)pPager->page
1224c 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73  Size;.    rc = s
1224d 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
1224e 61 67 65 72 2d 3e 66 64 2c 20 28 75 38 2a 29 61  ager->fd, (u8*)a
1224f 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61  Data, pPager->pa
12250 67 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20  geSize, ofst);. 
12251 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67     if( pgno>pPag
12252 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29  er->dbFileSize )
12253 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
12254 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e  dbFileSize = pgn
12255 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  o;.    }.    if(
12256 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70   pPager->pBackup
12257 20 29 7b 0a 20 20 20 20 20 20 43 4f 44 45 43 31   ){.      CODEC1
12258 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20  (pPager, aData, 
12259 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49  pgno, 3, rc=SQLI
1225a 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20  TE_NOMEM);.     
1225b 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70   sqlite3BackupUp
1225c 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61  date(pPager->pBa
1225d 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a  ckup, pgno, (u8*
1225e 29 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 43  )aData);.      C
1225f 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 61 44  ODEC2(pPager, aD
12260 61 74 61 2c 20 70 67 6e 6f 2c 20 37 2c 20 72 63  ata, pgno, 7, rc
12261 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 61  =SQLITE_NOMEM, a
12262 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Data);.    }.  }
12263 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e  else if( !isMain
12264 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29  Jrnl && pPg==0 )
12265 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
12266 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f   is a rollback o
12267 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e  f a savepoint an
12268 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77  d data was not w
12269 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a  ritten to.    **
1226a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
1226b 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  d the page is no
1226c 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65  t in-memory, the
1226d 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61  re is a potentia
1226e 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d  l.    ** problem
1226f 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20  . When the page 
12270 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20  is next fetched 
12271 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  by the b-tree la
12272 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20  yer, it .    ** 
12273 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f  will be read fro
12274 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
12275 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f  ile, which may o
12276 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20  r may not be .  
12277 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20    ** current. . 
12278 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
12279 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20  re are a couple 
1227a 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 79  of different way
1227b 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65  s this can happe
1227c 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65  n. All are quite
1227d 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e  .    ** obscure.
1227e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e   When running in
1227f 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64   synchronous mod
12280 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79  e, this can only
12281 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20   happen .    ** 
12282 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f  if the page is o
12283 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
12284 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
12285 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  the transaction,
12286 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70   then.    ** pop
12287 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76  ulated, then mov
12288 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
12289 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e  PagerMovepage().
1228a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1228b 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74  he solution is t
1228c 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f  o add an in-memo
1228d 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 63  ry page to the c
1228e 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a  ache containing.
1228f 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20      ** the data 
12290 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74  just read from t
12291 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20  he sub-journal. 
12292 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73  Mark the page as
12293 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61   dirty .    ** a
12294 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20  nd if the pager 
12295 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e  requires a journ
12296 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61  al-sync, then ma
12297 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 0a  rk the page as .
12298 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67      ** requiring
12299 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20   a journal-sync 
1229a 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 69  before it is wri
1229b 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tten..    */.   
1229c 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70   assert( isSavep
1229d 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72  nt );.    if( (r
1229e 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1229f 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20  Acquire(pPager, 
122a0 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 21  pgno, &pPg, 1))!
122a1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
122a2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
122a3 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 6c     }.    pPg->fl
122a4 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45  ags &= ~PGHDR_NE
122a5 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 6c  ED_READ;.    sql
122a6 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69  ite3PcacheMakeDi
122a7 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20  rty(pPg);.  }.  
122a8 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 2f  if( pPg ){.    /
122a9 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64  * No page should
122aa 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 69   ever be explici
122ab 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  tly rolled back 
122ac 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20  that is in use, 
122ad 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f  except.    ** fo
122ae 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20 69  r page 1 which i
122af 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69 6e  s held in use in
122b0 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74   order to keep t
122b1 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20  he lock on the. 
122b2 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 61     ** database a
122b3 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20 73  ctive. However s
122b4 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 20 62  uch a page may b
122b5 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73  e rolled back as
122b6 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a   a result.    **
122b7 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   of an internal 
122b8 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 20  error resulting 
122b9 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20  in an automatic 
122ba 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73  call to.    ** s
122bb 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62  qlite3PagerRollb
122bc 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ack()..    */.  
122bd 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20    void *pData;. 
122be 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d 3e     pData = pPg->
122bf 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70  pData;.    memcp
122c0 79 28 70 44 61 74 61 2c 20 28 75 38 2a 29 61 44  y(pData, (u8*)aD
122c1 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  ata, pPager->pag
122c2 65 53 69 7a 65 29 3b 0a 20 20 20 20 70 50 61 67  eSize);.    pPag
122c3 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50  er->xReiniter(pP
122c4 67 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4d 61  g);.    if( isMa
122c5 69 6e 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61  inJrnl && (!isSa
122c6 76 65 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65  vepnt || *pOffse
122c7 74 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t<=pPager->journ
122c8 61 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20  alHdr) ){.      
122c9 2f 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e  /* If the conten
122ca 74 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  ts of this page 
122cb 77 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72  were just restor
122cc 65 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ed from the main
122cd 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e   .      ** journ
122ce 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74  al file, then it
122cf 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62  s content must b
122d0 65 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77  e as they were w
122d1 68 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a  hen the .      *
122d2 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61  * transaction wa
122d3 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20  s first opened. 
122d4 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20  In this case we 
122d5 63 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67  can mark the pag
122d6 65 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c  e.      ** as cl
122d7 65 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65  ean, since there
122d8 20 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64   will be no need
122d9 20 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74   to write it out
122da 20 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a   to the..      *
122db 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65  *.      ** There
122dc 20 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f   is one exceptio
122dd 6e 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20  n to this rule. 
122de 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62  If the page is b
122df 65 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20  eing rolled.    
122e0 20 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72    ** back as par
122e1 74 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74  t of a savepoint
122e2 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20   (or statement) 
122e3 72 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e  rollback from an
122e4 20 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e   .      ** unsyn
122e5 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74  ced portion of t
122e6 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
122e7 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73  file, then it is
122e8 20 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20   not safe.      
122e9 2a 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70  ** to mark the p
122ea 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68  age as clean. Th
122eb 69 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61  is is because ma
122ec 72 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61  rking the page a
122ed 73 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e  s.      ** clean
122ee 20 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20   will clear the 
122ef 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
122f0 66 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20  flag. Since the 
122f1 70 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a  page is.      **
122f2 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
122f3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65  journal file (re
122f4 63 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e  corded in Pager.
122f5 70 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a  pInJournal) and.
122f6 20 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48        ** the PGH
122f7 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61  DR_NEED_SYNC fla
122f8 67 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66  g is cleared, if
122f9 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69   the page is wri
122fa 74 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a  tten to.      **
122fb 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68   again within th
122fc 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  is transaction, 
122fd 69 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65  it will be marke
122fe 64 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20  d as dirty but. 
122ff 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44       ** the PGHD
12300 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
12301 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74   will not be set
12302 2e 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20  . It could then 
12303 70 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20  potentially.    
12304 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20    ** be written 
12305 6f 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74  out into the dat
12306 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72  abase file befor
12307 65 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69  e its journal fi
12308 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d  le.      ** segm
12309 65 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49  ent is synced. I
1230a 66 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73  f a crash occurs
1230b 20 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f   during or follo
1230c 77 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20  wing this,.     
1230d 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72   ** database cor
1230e 72 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75  ruption may ensu
1230f 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
12310 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
12311 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20  akeClean(pPg);. 
12312 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
12313 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20  TE_CHECK_PAGES. 
12314 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68     pPg->pageHash
12315 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
12316 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20  h(pPg);.#endif. 
12317 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61     /* If this wa
12318 73 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72  s page 1, then r
12319 65 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65  estore the value
1231a 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65   of Pager.dbFile
1231b 56 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20  Vers..    ** Do 
1231c 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20  this before any 
1231d 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20  decoding. */.   
1231e 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a   if( pgno==1 ){.
1231f 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50        memcpy(&pP
12320 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
12321 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b  , &((u8*)pData)[
12322 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65  24],sizeof(pPage
12323 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b  r->dbFileVers));
12324 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
12325 65 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a  ecode the page j
12326 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69  ust read from di
12327 73 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31  sk */.    CODEC1
12328 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20  (pPager, pData, 
12329 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 2c 20 72 63  pPg->pgno, 3, rc
1232a 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a  =SQLITE_NOMEM);.
1232b 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
1232c 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20  eRelease(pPg);. 
1232d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1232e 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  }../*.** Paramet
1232f 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74 68  er zMaster is th
12330 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74  e name of a mast
12331 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
12332 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61   A single journa
12333 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72  l.** file that r
12334 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d  eferred to the m
12335 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
12336 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  le has just been
12337 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a   rolled back..**
12338 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
12339 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f  ecks if it is po
1233a 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65  ssible to delete
1233b 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1233c 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64  nal file,.** and
1233d 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69   does so if it i
1233e 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  s..**.** Argumen
1233f 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 6f  t zMaster may po
12340 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54 6d  int to Pager.pTm
12341 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74 20  pSpace. So that 
12342 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a  buffer is not .*
12343 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20  * available for 
12344 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73 20  use within this 
12345 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
12346 57 68 65 6e 20 61 20 6d 61 73 74 65 72 20 6a 6f  When a master jo
12347 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 72  urnal file is cr
12348 65 61 74 65 64 2c 20 69 74 20 69 73 20 70 6f 70  eated, it is pop
12349 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ulated with the 
1234a 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c  names .** of all
1234b 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 6a 6f   of its child jo
1234c 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 74 65  urnals, one afte
1234d 72 20 61 6e 6f 74 68 65 72 2c 20 66 6f 72 6d 61  r another, forma
1234e 74 74 65 64 20 61 73 20 75 74 66 2d 38 20 0a 2a  tted as utf-8 .*
1234f 2a 20 65 6e 63 6f 64 65 64 20 74 65 78 74 2e 20  * encoded text. 
12350 54 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20  The end of each 
12351 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 66 69  child journal fi
12352 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74  le is marked wit
12353 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d  h a .** nul-term
12354 69 6e 61 74 6f 72 20 62 79 74 65 20 28 30 78 30  inator byte (0x0
12355 30 29 2e 20 69 2e 65 2e 20 74 68 65 20 65 6e 74  0). i.e. the ent
12356 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  ire contents of 
12357 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
12358 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 61 20 74  .** file for a t
12359 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 76 6f 6c  ransaction invol
1235a 76 69 6e 67 20 74 77 6f 20 64 61 74 61 62 61 73  ving two databas
1235b 65 73 20 6d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a  es might be:.**.
1235c 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c  **   "/home/bill
1235d 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30  /a.db-journal\x0
1235e 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62  0/home/bill/b.db
1235f 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a  -journal\x00".**
12360 0a 2a 2a 20 41 20 6d 61 73 74 65 72 20 6a 6f 75  .** A master jou
12361 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 6e  rnal file may on
12362 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 6f 6e  ly be deleted on
12363 63 65 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68  ce all of its ch
12364 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73  ild .** journals
12365 20 68 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c 65   have been rolle
12366 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  d back..**.** Th
12367 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 61 64  is function read
12368 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  s the contents o
12369 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75  f the master-jou
1236a 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 0a  rnal file into .
1236b 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f  ** memory and lo
1236c 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 68  ops through each
1236d 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f   of the child jo
1236e 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f 72  urnal names. For
1236f 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 6a  .** each child j
12370 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65 63 6b  ournal, it check
12371 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  s if:.**.**   * 
12372 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75  if the child jou
12373 72 6e 61 6c 20 65 78 69 73 74 73 2c 20 61 6e 64  rnal exists, and
12374 20 69 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 69 66   if so.**   * if
12375 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e   the child journ
12376 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65  al contains a re
12377 66 65 72 65 6e 63 65 20 74 6f 20 6d 61 73 74 65  ference to maste
12378 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20  r journal .**   
12379 20 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 0a 2a    file zMaster.*
1237a 2a 0a 2a 2a 20 49 66 20 61 20 63 68 69 6c 64 20  *.** If a child 
1237b 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66  journal can be f
1237c 6f 75 6e 64 20 74 68 61 74 20 6d 61 74 63 68 65  ound that matche
1237d 73 20 62 6f 74 68 20 6f 66 20 74 68 65 20 63 72  s both of the cr
1237e 69 74 65 72 69 61 0a 2a 2a 20 61 62 6f 76 65 2c  iteria.** above,
1237f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
12380 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 64  eturns without d
12381 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 4f  oing anything. O
12382 74 68 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a 20  therwise, if.** 
12383 6e 6f 20 73 75 63 68 20 63 68 69 6c 64 20 6a 6f  no such child jo
12384 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75  urnal can be fou
12385 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 73 74 65 72  nd, file zMaster
12386 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d   is deleted from
12387 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73  .** the file-sys
12388 74 65 6d 20 75 73 69 6e 67 20 73 71 6c 69 74 65  tem using sqlite
12389 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a  3OsDelete()..**.
1238a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  ** If an IO erro
1238b 72 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 75  r within this fu
1238c 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 72 72 6f 72  nction, an error
1238d 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
1238e 64 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74  d. This.** funct
1238f 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d 65  ion allocates me
12390 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20  mory by calling 
12391 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e  sqlite3Malloc().
12392 20 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f   If an allocatio
12393 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49  n.** fails, SQLI
12394 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75  TE_NOMEM is retu
12395 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
12396 20 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c   if no IO or mal
12397 6c 6f 63 20 65 72 72 6f 72 73 20 0a 2a 2a 20 6f  loc errors .** o
12398 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ccur, SQLITE_OK 
12399 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
1239a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 66 75  ** TODO: This fu
1239b 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73  nction allocates
1239c 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63 6b 20   a single block 
1239d 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f 61  of memory to loa
1239e 64 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20  d.** the entire 
1239f 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
123a0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
123a1 69 6c 65 2e 20 54 68 69 73 20 63 6f 75 6c 64 20  ile. This could 
123a2 62 65 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 20 6f  be.** a couple o
123a3 66 20 6b 69 6c 6f 62 79 74 65 73 20 6f 72 20 73  f kilobytes or s
123a4 6f 20 2d 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  o - potentially 
123a5 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
123a6 70 61 67 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a  page .** size..*
123a7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
123a8 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67  er_delmaster(Pag
123a9 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73  er *pPager, cons
123aa 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29  t char *zMaster)
123ab 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
123ac 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e  *pVfs = pPager->
123ad 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20  pVfs;.  int rc; 
123ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
123af 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
123b0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
123b1 6c 65 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20  le *pMaster;    
123b2 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 73 74  /* Malloc'd mast
123b3 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er-journal file 
123b4 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20  descriptor */.  
123b5 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
123b6 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c  ournal;   /* Mal
123b7 6c 6f 63 27 64 20 63 68 69 6c 64 2d 6a 6f 75 72  loc'd child-jour
123b8 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
123b9 74 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  tor */.  char *z
123ba 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20  MasterJournal = 
123bb 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 6f  0; /* Contents o
123bc 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  f master journal
123bd 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6e   file */.  i64 n
123be 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20  MasterJournal;  
123bf 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
123c0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
123c1 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c  ile */..  /* All
123c2 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
123c3 62 6f 74 68 20 74 68 65 20 70 4a 6f 75 72 6e 61  both the pJourna
123c4 6c 20 61 6e 64 20 70 4d 61 73 74 65 72 20 66 69  l and pMaster fi
123c5 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2e 0a  le descriptors..
123c6 20 20 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66    ** If successf
123c7 75 6c 2c 20 6f 70 65 6e 20 74 68 65 20 6d 61 73  ul, open the mas
123c8 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
123c9 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 20 20   for reading..  
123ca 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28  */.  pMaster = (
123cb 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73  sqlite3_file *)s
123cc 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
123cd 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20  (pVfs->szOsFile 
123ce 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c  * 2);.  pJournal
123cf 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65   = (sqlite3_file
123d0 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61 73 74   *)(((u8 *)pMast
123d1 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73  er) + pVfs->szOs
123d2 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 4d  File);.  if( !pM
123d3 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72 63 20  aster ){.    rc 
123d4 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
123d5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e    }else{.    con
123d6 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28  st int flags = (
123d7 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
123d8 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  ONLY|SQLITE_OPEN
123d9 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29  _MASTER_JOURNAL)
123da 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
123db 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a  e3OsOpen(pVfs, z
123dc 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65 72 2c  Master, pMaster,
123dd 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a   flags, 0);.  }.
123de 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
123df 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61  _OK ) goto delma
123e0 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 63 20  ster_out;..  rc 
123e1 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
123e2 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d  ize(pMaster, &nM
123e3 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20  asterJournal);. 
123e4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
123e5 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73  OK ) goto delmas
123e6 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20  ter_out;..  if( 
123e7 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30  nMasterJournal>0
123e8 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a   ){.    char *zJ
123e9 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72  ournal;.    char
123ea 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30   *zMasterPtr = 0
123eb 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65  ;.    int nMaste
123ec 72 50 74 72 20 3d 20 70 56 66 73 2d 3e 6d 78 50  rPtr = pVfs->mxP
123ed 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 20  athname+1;..    
123ee 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 69  /* Load the enti
123ef 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  re master journa
123f0 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 63  l file into spac
123f1 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a  e obtained from.
123f2 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d      ** sqlite3_m
123f3 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e  alloc() and poin
123f4 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65  ted to by zMaste
123f5 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a  rJournal. .    *
123f6 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75  /.    zMasterJou
123f7 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61  rnal = sqlite3Ma
123f8 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65  lloc((int)nMaste
123f9 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74  rJournal + nMast
123fa 65 72 50 74 72 20 2b 20 31 29 3b 0a 20 20 20 20  erPtr + 1);.    
123fb 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72  if( !zMasterJour
123fc 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  nal ){.      rc 
123fd 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
123fe 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
123ff 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  ster_out;.    }.
12400 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d      zMasterPtr =
12401 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c   &zMasterJournal
12402 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2b  [nMasterJournal+
12403 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  1];.    rc = sql
12404 69 74 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74  ite3OsRead(pMast
12405 65 72 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e  er, zMasterJourn
12406 61 6c 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72  al, (int)nMaster
12407 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20  Journal, 0);.   
12408 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12409 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73  OK ) goto delmas
1240a 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7a 4d 61  ter_out;.    zMa
1240b 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73  sterJournal[nMas
1240c 74 65 72 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b  terJournal] = 0;
1240d 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d  ..    zJournal =
1240e 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b   zMasterJournal;
1240f 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f  .    while( (zJo
12410 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75  urnal-zMasterJou
12411 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75  rnal)<nMasterJou
12412 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e  rnal ){.      in
12413 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 20 20  t exists;.      
12414 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63  rc = sqlite3OsAc
12415 63 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75 72  cess(pVfs, zJour
12416 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  nal, SQLITE_ACCE
12417 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73  SS_EXISTS, &exis
12418 74 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ts);.      if( r
12419 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1241a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c          goto del
1241b 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20  master_out;.    
1241c 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 78    }.      if( ex
1241d 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 20 20  ists ){.        
1241e 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f  /* One of the jo
1241f 75 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74  urnals pointed t
12420 6f 20 62 79 20 74 68 65 20 6d 61 73 74 65 72 20  o by the master 
12421 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a  journal exists..
12422 20 20 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20          ** Open 
12423 69 74 20 61 6e 64 20 63 68 65 63 6b 20 69 66 20  it and check if 
12424 69 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65  it points at the
12425 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e   master journal.
12426 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 73   If.        ** s
12427 6f 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75  o, return withou
12428 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6d  t deleting the m
12429 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1242a 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  le..        */. 
1242b 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20         int c;.  
1242c 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20        int flags 
1242d 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  = (SQLITE_OPEN_R
1242e 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f  EADONLY|SQLITE_O
1242f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
12430 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
12431 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56  sqlite3OsOpen(pV
12432 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a  fs, zJournal, pJ
12433 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 30  ournal, flags, 0
12434 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
12435 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
12436 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64            goto d
12437 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20  elmaster_out;.  
12438 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
12439 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72   rc = readMaster
1243a 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c  Journal(pJournal
1243b 2c 20 7a 4d 61 73 74 65 72 50 74 72 2c 20 6e 4d  , zMasterPtr, nM
1243c 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20 20  asterPtr);.     
1243d 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
1243e 65 28 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20  e(pJournal);.   
1243f 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
12440 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12441 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74      goto delmast
12442 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  er_out;.        
12443 7d 0a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 7a  }..        c = z
12444 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d 30 20  MasterPtr[0]!=0 
12445 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 74 65  && strcmp(zMaste
12446 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d 3d  rPtr, zMaster)==
12447 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  0;.        if( c
12448 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
12449 20 57 65 20 68 61 76 65 20 61 20 6d 61 74 63 68   We have a match
1244a 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20  . Do not delete 
1244b 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1244c 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20  al file. */.    
1244d 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
1244e 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20  ster_out;.      
1244f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
12450 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 73    zJournal += (s
12451 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
12452 4a 6f 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20  Journal)+1);.   
12453 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d   }.  }.  .  rc =
12454 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
12455 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20  (pVfs, zMaster, 
12456 30 29 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f  0);..delmaster_o
12457 75 74 3a 0a 20 20 69 66 28 20 7a 4d 61 73 74 65  ut:.  if( zMaste
12458 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  rJournal ){.    
12459 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61  sqlite3_free(zMa
1245a 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20  sterJournal);.  
1245b 7d 20 20 0a 20 20 69 66 28 20 70 4d 61 73 74 65  }  .  if( pMaste
1245c 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
1245d 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 29  OsClose(pMaster)
1245e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69  ;.    assert( !i
1245f 73 4f 70 65 6e 28 70 4a 6f 75 72 6e 61 6c 29 20  sOpen(pJournal) 
12460 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
12461 5f 66 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a  _free(pMaster);.
12462 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
12463 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
12464 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
12465 63 68 61 6e 67 65 20 74 68 65 20 61 63 74 75 61  change the actua
12466 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  l size of the da
12467 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20  tabase .** file 
12468 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  in the file-syst
12469 65 6d 2e 20 54 68 69 73 20 6f 6e 6c 79 20 68 61  em. This only ha
1246a 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 6d 6d 69  ppens when commi
1246b 74 74 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74  tting a transact
1246c 69 6f 6e 2c 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69  ion,.** or rolli
1246d 6e 67 20 62 61 63 6b 20 61 20 74 72 61 6e 73 61  ng back a transa
1246e 63 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67  ction (including
1246f 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20   rolling back a 
12470 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a  hot-journal)..**
12471 0a 2a 2a 20 49 66 20 74 68 65 20 6d 61 69 6e 20  .** If the main 
12472 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
12473 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 61 6e   not open, or an
12474 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
12475 69 73 20 6e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20  is not.** held, 
12476 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
12477 20 61 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77   a no-op. Otherw
12478 69 73 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66  ise, the size of
12479 20 74 68 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20   the file is.** 
1247a 63 68 61 6e 67 65 64 20 74 6f 20 6e 50 61 67 65  changed to nPage
1247b 20 70 61 67 65 73 20 28 6e 50 61 67 65 2a 70 50   pages (nPage*pP
1247c 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62  ager->pageSize b
1247d 79 74 65 73 29 2e 20 49 66 20 74 68 65 20 66 69  ytes). If the fi
1247e 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73  le.** on disk is
1247f 20 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65   currently large
12480 72 20 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67  r than nPage pag
12481 65 73 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65  es, then use the
12482 20 56 46 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74   VFS.** xTruncat
12483 65 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72  e() method to tr
12484 75 6e 63 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a  uncate it..**.**
12485 20 4f 72 2c 20 69 74 20 6d 69 67 68 74 20 6d 69   Or, it might mi
12486 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65 20  ght be the case 
12487 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 6f 6e  that the file on
12488 20 64 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72   disk is smaller
12489 20 74 68 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20   than .** nPage 
1248a 70 61 67 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72  pages. Some oper
1248b 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70  ating system imp
1248c 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e  lementations can
1248d 20 67 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66   get confused if
1248e 20 0a 2a 2a 20 79 6f 75 20 74 72 79 20 74 6f 20   .** you try to 
1248f 74 72 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20  truncate a file 
12490 74 6f 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61  to some size tha
12491 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  t is larger than
12492 20 69 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c   it .** currentl
12493 79 20 69 73 2c 20 73 6f 20 64 65 74 65 63 74 20  y is, so detect 
12494 74 68 69 73 20 63 61 73 65 20 61 6e 64 20 77 72  this case and wr
12495 69 74 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72  ite a single zer
12496 6f 20 62 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68  o byte to .** th
12497 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77  e end of the new
12498 20 66 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a   file instead..*
12499 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
1249a 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ul, return SQLIT
1249b 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65  E_OK. If an IO e
1249c 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
1249d 65 20 6d 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74  e modifying.** t
1249e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1249f 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72  , return the err
124a0 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63  or code to the c
124a1 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  aller..*/.static
124a2 20 69 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63   int pager_trunc
124a3 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ate(Pager *pPage
124a4 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a  r, Pgno nPage){.
124a5 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
124a6 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67  E_OK;.  if( pPag
124a7 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
124a8 5f 45 58 43 4c 55 53 49 56 45 20 26 26 20 69 73  _EXCLUSIVE && is
124a9 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
124aa 20 29 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72   ){.    i64 curr
124ab 65 6e 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65  entSize, newSize
124ac 3b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49  ;.    /* TODO: I
124ad 73 20 69 74 20 73 61 66 65 20 74 6f 20 75 73 65  s it safe to use
124ae 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a   Pager.dbFileSiz
124af 65 20 68 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72  e here? */.    r
124b0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
124b1 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64  eSize(pPager->fd
124b2 2c 20 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b  , &currentSize);
124b3 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 70  .    newSize = p
124b4 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a  Pager->pageSize*
124b5 28 69 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20  (i64)nPage;.    
124b6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
124b7 4b 20 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65  K && currentSize
124b8 21 3d 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20  !=newSize ){.   
124b9 20 20 20 69 66 28 20 63 75 72 72 65 6e 74 53 69     if( currentSi
124ba 7a 65 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20  ze>newSize ){.  
124bb 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
124bc 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61  e3OsTruncate(pPa
124bd 67 65 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65  ger->fd, newSize
124be 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
124bf 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
124c0 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
124c1 65 72 2d 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e  er->fd, "", 1, n
124c2 65 77 53 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20  ewSize-1);.     
124c3 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d   }.      if( rc=
124c4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
124c5 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62        pPager->db
124c6 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65  FileSize = nPage
124c7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
124c8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
124c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
124ca 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
124cb 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20  ager.sectorSize 
124cc 76 61 72 69 61 62 6c 65 20 66 6f 72 20 74 68 65  variable for the
124cd 20 67 69 76 65 6e 0a 2a 2a 20 70 61 67 65 72 20   given.** pager 
124ce 62 61 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c  based on the val
124cf 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ue returned by t
124d0 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 20 6d  he xSectorSize m
124d1 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20  ethod.** of the 
124d2 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69  open database fi
124d3 6c 65 2e 20 54 68 65 20 73 65 63 74 6f 72 20 73  le. The sector s
124d4 69 7a 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64  ize will be used
124d5 20 75 73 65 64 20 0a 2a 2a 20 74 6f 20 64 65 74   used .** to det
124d6 65 72 6d 69 6e 65 20 74 68 65 20 73 69 7a 65 20  ermine the size 
124d7 61 6e 64 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66  and alignment of
124d8 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
124d9 61 6e 64 20 0a 2a 2a 20 6d 61 73 74 65 72 20 6a  and .** master j
124da 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20  ournal pointers 
124db 77 69 74 68 69 6e 20 63 72 65 61 74 65 64 20 6a  within created j
124dc 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a  ournal files..**
124dd 0a 2a 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72  .** For temporar
124de 79 20 66 69 6c 65 73 20 74 68 65 20 65 66 66 65  y files the effe
124df 63 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a  ctive sector siz
124e0 65 20 69 73 20 61 6c 77 61 79 73 20 35 31 32 20  e is always 512 
124e1 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  bytes..**.** Oth
124e2 65 72 77 69 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d  erwise, for non-
124e3 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c  temporary files,
124e4 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 73   the effective s
124e5 65 63 74 6f 72 20 73 69 7a 65 20 69 73 0a 2a 2a  ector size is.**
124e6 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
124e7 6e 65 64 20 62 79 20 74 68 65 20 78 53 65 63 74  ned by the xSect
124e8 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 20  orSize() method 
124e9 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 33 32  rounded up to 32
124ea 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73   if.** it is les
124eb 73 20 74 68 61 6e 20 33 32 2c 20 6f 72 20 72 6f  s than 32, or ro
124ec 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41  unded down to MA
124ed 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66  X_SECTOR_SIZE if
124ee 20 69 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65   it.** is greate
124ef 72 20 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f  r than MAX_SECTO
124f0 52 5f 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69  R_SIZE..*/.stati
124f1 63 20 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72  c void setSector
124f2 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67  Size(Pager *pPag
124f3 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  er){.  assert( i
124f4 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
124f5 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d  ) || pPager->tem
124f6 70 46 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20  pFile );..  if( 
124f7 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
124f8 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74  e ){.    /* Sect
124f9 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20  or size doesn't 
124fa 6d 61 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f  matter for tempo
124fb 72 61 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f  rary files. Also
124fc 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a  , the file.    *
124fd 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62  * may not have b
124fe 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20  een opened yet, 
124ff 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
12500 65 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29  e OsSectorSize()
12501 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c  .    ** call wil
12502 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20  l segfault..    
12503 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  */.    pPager->s
12504 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69  ectorSize = sqli
12505 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  te3OsSectorSize(
12506 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d  pPager->fd);.  }
12507 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
12508 65 63 74 6f 72 53 69 7a 65 3c 33 32 20 29 7b 0a  ectorSize<32 ){.
12509 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74      pPager->sect
1250a 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20  orSize = 512;.  
1250b 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  }.  if( pPager->
1250c 73 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53  sectorSize>MAX_S
1250d 45 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20  ECTOR_SIZE ){.  
1250e 20 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45    assert( MAX_SE
1250f 43 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29  CTOR_SIZE>=512 )
12510 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65  ;.    pPager->se
12511 63 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53  ctorSize = MAX_S
12512 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a  ECTOR_SIZE;.  }.
12513 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63  }../*.** Playbac
12514 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e  k the journal an
12515 64 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74  d thus restore t
12516 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12517 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65   to.** the state
12518 20 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72   it was in befor
12519 65 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b  e we started mak
1251a 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a  ing changes.  .*
1251b 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c  *.** The journal
1251c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20   file format is 
1251d 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a  as follows: .**.
1251e 2a 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20  **  (1)  8 byte 
1251f 70 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20  prefix.  A copy 
12520 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of aJournalMagic
12521 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62  []..**  (2)  4 b
12522 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
12523 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20  nteger which is 
12524 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61  the number of va
12525 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73  lid page records
12526 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65  .**       in the
12527 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68   journal.  If th
12528 69 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66  is value is 0xff
12529 66 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d  ffffff, then com
1252a 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20  pute the.**     
1252b 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65    number of page
1252c 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68   records from th
1252d 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a  e journal size..
1252e 2a 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20  **  (3)  4 byte 
1252f 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67  big-endian integ
12530 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20  er which is the 
12531 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f  initial value fo
12532 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20  r the .**       
12533 73 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e  sanity checksum.
12534 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65  .**  (4)  4 byte
12535 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69   integer which i
12536 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
12537 70 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74  pages to truncat
12538 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64  e the.**       d
12539 61 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e  atabase to durin
1253a 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a  g a rollback..**
1253b 20 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69    (5)  4 byte bi
1253c 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
1253d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65   which is the se
1253e 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20  ctor size.  The 
1253f 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20  header.**       
12540 69 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74  is this many byt
12541 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20  es in size..**  
12542 28 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d  (6)  4 byte big-
12543 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77  endian integer w
12544 68 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65  hich is the page
12545 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 37 29 20 20   size..**  (7)  
12546 7a 65 72 6f 20 70 61 64 64 69 6e 67 20 6f 75 74  zero padding out
12547 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63   to the next sec
12548 74 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 38  tor size..**  (8
12549 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  )  Zero or more 
1254a 70 61 67 65 73 20 69 6e 73 74 61 6e 63 65 73 2c  pages instances,
1254b 20 65 61 63 68 20 61 73 20 66 6f 6c 6c 6f 77 73   each as follows
1254c 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34  :.**        +  4
1254d 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65   byte page numbe
1254e 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20  r..**        +  
1254f 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12550 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 0a   bytes of data..
12551 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34 20 62  **        +  4 b
12552 79 74 65 20 63 68 65 63 6b 73 75 6d 0a 2a 2a 0a  yte checksum.**.
12553 2a 2a 20 57 68 65 6e 20 77 65 20 73 70 65 61 6b  ** When we speak
12554 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
12555 68 65 61 64 65 72 2c 20 77 65 20 6d 65 61 6e 20  header, we mean 
12556 74 68 65 20 66 69 72 73 74 20 37 20 69 74 65 6d  the first 7 item
12557 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45 61 63 68  s above..** Each
12558 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 6a 6f   entry in the jo
12559 75 72 6e 61 6c 20 69 73 20 61 6e 20 69 6e 73 74  urnal is an inst
1255a 61 6e 63 65 20 6f 66 20 74 68 65 20 38 74 68 20  ance of the 8th 
1255b 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c  item..**.** Call
1255c 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d 20   the value from 
1255d 74 68 65 20 73 65 63 6f 6e 64 20 62 75 6c 6c 65  the second bulle
1255e 74 20 22 6e 52 65 63 22 2e 20 20 6e 52 65 63 20  t "nRec".  nRec 
1255f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
12560 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67 65 20 65  .** valid page e
12561 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 6a 6f  ntries in the jo
12562 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f 73 74 20  urnal.  In most 
12563 63 61 73 65 73 2c 20 79 6f 75 20 63 61 6e 20 63  cases, you can c
12564 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 76 61  ompute the.** va
12565 6c 75 65 20 6f 66 20 6e 52 65 63 20 66 72 6f 6d  lue of nRec from
12566 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
12567 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20   journal file.  
12568 42 75 74 20 69 66 20 61 20 70 6f 77 65 72 0a 2a  But if a power.*
12569 2a 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72  * failure occurr
1256a 65 64 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75  ed while the jou
1256b 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 77  rnal was being w
1256c 72 69 74 74 65 6e 2c 20 69 74 20 63 6f 75 6c 64  ritten, it could
1256d 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20   be the.** case 
1256e 74 68 61 74 20 74 68 65 20 73 69 7a 65 20 6f 66  that the size of
1256f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12570 65 20 68 61 64 20 61 6c 72 65 61 64 79 20 62 65  e had already be
12571 65 6e 20 69 6e 63 72 65 61 73 65 64 20 62 75 74  en increased but
12572 0a 2a 2a 20 74 68 65 20 65 78 74 72 61 20 65 6e  .** the extra en
12573 74 72 69 65 73 20 68 61 64 20 6e 6f 74 20 79 65  tries had not ye
12574 74 20 6d 61 64 65 20 69 74 20 73 61 66 65 6c 79  t made it safely
12575 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e 20 73 75   to disk.  In su
12576 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a 20 74 68  ch a case,.** th
12577 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20  e value of nRec 
12578 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d 20 74 68  computed from th
12579 65 20 66 69 6c 65 20 73 69 7a 65 20 77 6f 75 6c  e file size woul
1257a 64 20 62 65 20 74 6f 6f 20 6c 61 72 67 65 2e 20  d be too large. 
1257b 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20 72 65 61   For.** that rea
1257c 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79 73 20 75  son, we always u
1257d 73 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75  se the nRec valu
1257e 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e  e in the header.
1257f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 52  .**.** If the nR
12580 65 63 20 76 61 6c 75 65 20 69 73 20 30 78 66 66  ec value is 0xff
12581 66 66 66 66 66 66 20 69 74 20 6d 65 61 6e 73 20  ffffff it means 
12582 74 68 61 74 20 6e 52 65 63 20 73 68 6f 75 6c 64  that nRec should
12583 20 62 65 20 63 6f 6d 70 75 74 65 64 0a 2a 2a 20   be computed.** 
12584 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69  from the file si
12585 7a 65 2e 20 20 54 68 69 73 20 76 61 6c 75 65 20  ze.  This value 
12586 69 73 20 75 73 65 64 20 77 68 65 6e 20 74 68 65  is used when the
12587 20 75 73 65 72 20 73 65 6c 65 63 74 73 20 74 68   user selects th
12588 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 6f 70 74  e.** no-sync opt
12589 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a 6f 75 72  ion for the jour
1258a 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 20 66 61  nal.  A power fa
1258b 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 61 64  ilure could lead
1258c 20 74 6f 20 63 6f 72 72 75 70 74 69 6f 6e 0a 2a   to corruption.*
1258d 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20  * in this case. 
1258e 20 42 75 74 20 66 6f 72 20 74 68 69 6e 67 73 20   But for things 
1258f 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72 79 20 74  like temporary t
12590 61 62 6c 65 20 28 77 68 69 63 68 20 77 69 6c 6c  able (which will
12591 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77   be.** deleted w
12592 68 65 6e 20 74 68 65 20 70 6f 77 65 72 20 69 73  hen the power is
12593 20 72 65 73 74 6f 72 65 64 29 20 77 65 20 64 6f   restored) we do
12594 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a 2a 0a 2a  n't care.  .**.*
12595 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70  * If the file op
12596 65 6e 65 64 20 61 73 20 74 68 65 20 6a 6f 75 72  ened as the jour
12597 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20  nal file is not 
12598 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a  a well-formed.**
12599 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
1259a 65 6e 20 61 6c 6c 20 70 61 67 65 73 20 75 70 20  en all pages up 
1259b 74 6f 20 74 68 65 20 66 69 72 73 74 20 63 6f 72  to the first cor
1259c 72 75 70 74 65 64 20 70 61 67 65 20 61 72 65 20  rupted page are 
1259d 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 28  rolled.** back (
1259e 6f 72 20 6e 6f 20 70 61 67 65 73 20 69 66 20 74  or no pages if t
1259f 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
125a0 72 20 69 73 20 63 6f 72 72 75 70 74 65 64 29 2e  r is corrupted).
125a1 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
125a2 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20 64 65 6c  e.** is then del
125a3 65 74 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f  eted and SQLITE_
125a4 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20 6a 75 73  OK returned, jus
125a5 74 20 61 73 20 69 66 20 6e 6f 20 63 6f 72 72 75  t as if no corru
125a6 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20 62 65 65  ption had.** bee
125a7 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a  n encountered..*
125a8 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f 4f 20 6f  *.** If an I/O o
125a9 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72  r malloc() error
125aa 20 6f 63 63 75 72 73 2c 20 74 68 65 20 6a 6f 75   occurs, the jou
125ab 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74  rnal-file is not
125ac 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 6e 64 20   deleted.** and 
125ad 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
125ae 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
125af 20 54 68 65 20 69 73 48 6f 74 20 70 61 72 61 6d   The isHot param
125b0 65 74 65 72 20 69 6e 64 69 63 61 74 65 73 20 74  eter indicates t
125b1 68 61 74 20 77 65 20 61 72 65 20 74 72 79 69 6e  hat we are tryin
125b2 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20  g to rollback a 
125b3 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68 61 74 20  journal.** that 
125b4 6d 69 67 68 74 20 62 65 20 61 20 68 6f 74 20 6a  might be a hot j
125b5 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 69 74 20  ournal.  Or, it 
125b6 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74 68  could be that th
125b7 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 0a 2a 2a  e journal is .**
125b8 20 70 72 65 73 65 72 76 65 64 20 62 65 63 61 75   preserved becau
125b9 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c 4d 4f 44  se of JOURNALMOD
125ba 45 5f 50 45 52 53 49 53 54 20 6f 72 20 4a 4f 55  E_PERSIST or JOU
125bb 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
125bc 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75  E..** If the jou
125bd 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 73 20 68  rnal really is h
125be 6f 74 2c 20 72 65 73 65 74 20 74 68 65 20 70 61  ot, reset the pa
125bf 67 65 72 20 63 61 63 68 65 20 70 72 69 6f 72 20  ger cache prior 
125c0 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61 63 6b 20  rolling.** back 
125c1 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20 20 49 66  any content.  If
125c2 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
125c3 6d 65 72 65 6c 79 20 70 65 72 73 69 73 74 65 6e  merely persisten
125c4 74 2c 20 6e 6f 20 72 65 73 65 74 20 69 73 0a 2a  t, no reset is.*
125c5 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61  * needed..*/.sta
125c6 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c  tic int pager_pl
125c7 61 79 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50  ayback(Pager *pP
125c8 61 67 65 72 2c 20 69 6e 74 20 69 73 48 6f 74 29  ager, int isHot)
125c9 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
125ca 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e  *pVfs = pPager->
125cb 70 56 66 73 3b 0a 20 20 69 36 34 20 73 7a 4a 3b  pVfs;.  i64 szJ;
125cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
125cd 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
125ce 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20  journal file in 
125cf 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32 20 6e  bytes */.  u32 n
125d0 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
125d1 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
125d2 20 52 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20   Records in the 
125d3 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32  journal */.  u32
125d4 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   u;             
125d5 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65        /* Unsigne
125d6 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  d loop counter *
125d7 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 20 3d 20  /.  Pgno mxPg = 
125d8 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0;           /* 
125d9 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 72 69 67  Size of the orig
125da 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20 70 61 67  inal file in pag
125db 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  es */.  int rc; 
125dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
125dd 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
125de 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  of a subroutine 
125df 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 31  */.  int res = 1
125e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
125e1 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   Value returned 
125e2 62 79 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65  by sqlite3OsAcce
125e3 73 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ss() */.  char *
125e4 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 20 20 20  zMaster = 0;    
125e5 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6d 61     /* Name of ma
125e6 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
125e7 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 69 6e  e if any */.  in
125e8 74 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74  t needPagerReset
125e9 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74  ;      /* True t
125ea 6f 20 72 65 73 65 74 20 70 61 67 65 20 70 72 69  o reset page pri
125eb 6f 72 20 74 6f 20 66 69 72 73 74 20 70 61 67 65  or to first page
125ec 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20   rollback */..  
125ed 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
125ee 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 73 20 61  w many records a
125ef 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  re in the journa
125f0 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 6c 79 20  l.  Abort early 
125f1 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72  if.  ** the jour
125f2 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e 0a 20 20  nal is empty..  
125f3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  */.  assert( isO
125f4 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
125f5 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
125f6 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
125f7 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 4a 29 3b  ger->jfd, &szJ);
125f8 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
125f9 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d 30 20 29  E_OK || szJ==0 )
125fa 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70  {.    goto end_p
125fb 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20  layback;.  }..  
125fc 2f 2a 20 52 65 61 64 20 74 68 65 20 6d 61 73 74  /* Read the mast
125fd 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20  er journal name 
125fe 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c  from the journal
125ff 2c 20 69 66 20 69 74 20 69 73 20 70 72 65 73 65  , if it is prese
12600 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61 20 6d 61  nt..  ** If a ma
12601 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
12602 65 20 6e 61 6d 65 20 69 73 20 73 70 65 63 69 66  e name is specif
12603 69 65 64 2c 20 62 75 74 20 74 68 65 20 66 69 6c  ied, but the fil
12604 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 70 72  e is not.  ** pr
12605 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b 2c 20 74  esent on disk, t
12606 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hen the journal 
12607 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e 64 20 64  is not hot and d
12608 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
12609 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65 64 20 62  be.  ** played b
1260a 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ack..  **.  ** T
1260b 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61 6c 6c 79  ODO: Technically
1260c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
1260d 73 20 61 6e 20 65 72 72 6f 72 20 62 65 63 61 75  s an error becau
1260e 73 65 20 69 74 20 61 73 73 75 6d 65 73 20 74 68  se it assumes th
1260f 61 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 50  at.  ** buffer P
12610 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 69  ager.pTmpSpace i
12611 73 20 28 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29  s (mxPathname+1)
12612 20 62 79 74 65 73 20 6f 72 20 6c 61 72 67 65 72   bytes or larger
12613 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20 20 2a 2a  . i.e. that.  **
12614 20 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69   (pPager->pageSi
12615 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d 3e 70 56  ze >= pPager->pV
12616 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
12617 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75 6e 69 78  ). Using os_unix
12618 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50 61 74 68  .c,.  **  mxPath
12619 6e 61 6d 65 20 69 73 20 35 31 32 2c 20 77 68 69  name is 512, whi
1261a 63 68 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ch is the same a
1261b 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c  s the minimum al
1261c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65 0a 20 20  lowable value.  
1261d 2a 2a 20 66 6f 72 20 70 61 67 65 53 69 7a 65 2e  ** for pageSize.
1261e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 20  .  */.  zMaster 
1261f 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70  = pPager->pTmpSp
12620 61 63 65 3b 0a 20 20 72 63 20 3d 20 72 65 61 64  ace;.  rc = read
12621 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50  MasterJournal(pP
12622 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74  ager->jfd, zMast
12623 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73  er, pPager->pVfs
12624 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b  ->mxPathname+1);
12625 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
12626 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b  E_OK && zMaster[
12627 30 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  0] ){.    rc = s
12628 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70  qlite3OsAccess(p
12629 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51  Vfs, zMaster, SQ
1262a 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
1262b 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 7d 0a 20  TS, &res);.  }. 
1262c 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20   zMaster = 0;.  
1262d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1262e 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a 20 20 20  K || !res ){.   
1262f 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61   goto end_playba
12630 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72  ck;.  }.  pPager
12631 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
12632 3b 0a 20 20 6e 65 65 64 50 61 67 65 72 52 65 73  ;.  needPagerRes
12633 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a 20 20 2f  et = isHot;..  /
12634 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 65 72 6d  * This loop term
12635 69 6e 61 74 65 73 20 65 69 74 68 65 72 20 77 68  inates either wh
12636 65 6e 20 61 20 72 65 61 64 4a 6f 75 72 6e 61 6c  en a readJournal
12637 48 64 72 28 29 20 6f 72 20 0a 20 20 2a 2a 20 70  Hdr() or .  ** p
12638 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e  ager_playback_on
12639 65 5f 70 61 67 65 28 29 20 63 61 6c 6c 20 72 65  e_page() call re
1263a 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e  turns SQLITE_DON
1263b 45 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72  E or an IO error
1263c 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73 2e 20 0a   .  ** occurs. .
1263d 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 31 20    */.  while( 1 
1263e 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 55 6e 73  ){.    int isUns
1263f 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  ync = 0;..    /*
12640 20 52 65 61 64 20 74 68 65 20 6e 65 78 74 20 6a   Read the next j
12641 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 72  ournal header fr
12642 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  om the journal f
12643 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 20 61  ile.  If there a
12644 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 65 6e  re.    ** not en
12645 6f 75 67 68 20 62 79 74 65 73 20 6c 65 66 74 20  ough bytes left 
12646 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  in the journal f
12647 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65  ile for a comple
12648 74 65 20 68 65 61 64 65 72 2c 20 6f 72 0a 20 20  te header, or.  
12649 20 20 2a 2a 20 69 74 20 69 73 20 63 6f 72 72 75    ** it is corru
1264a 70 74 65 64 2c 20 74 68 65 6e 20 61 20 70 72 6f  pted, then a pro
1264b 63 65 73 73 20 6d 75 73 74 20 6f 66 20 66 61 69  cess must of fai
1264c 6c 65 64 20 77 68 69 6c 65 20 77 72 69 74 69 6e  led while writin
1264d 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20 54 68 69  g it..    ** Thi
1264e 73 20 69 6e 64 69 63 61 74 65 73 20 6e 6f 74 68  s indicates noth
1264f 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64 73 20 74  ing more needs t
12650 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  o be rolled back
12651 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
12652 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72  = readJournalHdr
12653 28 70 50 61 67 65 72 2c 20 69 73 48 6f 74 2c 20  (pPager, isHot, 
12654 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d 78 50  szJ, &nRec, &mxP
12655 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  g);.    if( rc!=
12656 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a 20 20  SQLITE_OK ){ .  
12657 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12658 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  TE_DONE ){.     
12659 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1265a 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  K;.      }.     
1265b 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61   goto end_playba
1265c 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ck;.    }..    /
1265d 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 78 66  * If nRec is 0xf
1265e 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 74 68  fffffff, then th
1265f 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 63  is journal was c
12660 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63  reated by a proc
12661 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 69  ess.    ** worki
12662 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f  ng in no-sync mo
12663 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74  de. This means t
12664 68 61 74 20 74 68 65 20 72 65 73 74 20 6f 66 20  hat the rest of 
12665 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  the journal.    
12666 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73  ** file consists
12667 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 72 65   of pages, there
12668 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75   are no more jou
12669 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 43 6f  rnal headers. Co
1266a 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 68 65  mpute.    ** the
1266b 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 62   value of nRec b
1266c 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 73 73  ased on this ass
1266d 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  umption..    */.
1266e 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 78      if( nRec==0x
1266f 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20  ffffffff ){.    
12670 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12671 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f  ->journalOff==JO
12672 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
12673 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 6e 52  ger) );.      nR
12674 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20  ec = (int)((szJ 
12675 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  - JOURNAL_HDR_SZ
12676 28 70 50 61 67 65 72 29 29 2f 4a 4f 55 52 4e 41  (pPager))/JOURNA
12677 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29  L_PG_SZ(pPager))
12678 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
12679 49 66 20 6e 52 65 63 20 69 73 20 30 20 61 6e 64  If nRec is 0 and
1267a 20 74 68 69 73 20 72 6f 6c 6c 62 61 63 6b 20 69   this rollback i
1267b 73 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69  s of a transacti
1267c 6f 6e 20 63 72 65 61 74 65 64 20 62 79 20 74 68  on created by th
1267d 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73  is.    ** proces
1267e 73 20 61 6e 64 20 69 66 20 74 68 69 73 20 69 73  s and if this is
1267f 20 74 68 65 20 66 69 6e 61 6c 20 68 65 61 64 65   the final heade
12680 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  r in the journal
12681 2c 20 74 68 65 6e 20 69 74 20 6d 65 61 6e 73 0a  , then it means.
12682 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 69 73      ** that this
12683 20 70 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75   part of the jou
12684 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 66  rnal was being f
12685 69 6c 6c 65 64 20 62 75 74 20 68 61 73 20 6e 6f  illed but has no
12686 74 20 79 65 74 20 62 65 65 6e 0a 20 20 20 20 2a  t yet been.    *
12687 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b  * synced to disk
12688 2e 20 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6e  .  Compute the n
12689 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 62  umber of pages b
1268a 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6d 61  ased on the rema
1268b 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 73 69 7a  ining.    ** siz
1268c 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 20  e of the file.. 
1268d 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
1268e 20 74 68 69 72 64 20 74 65 72 6d 20 6f 66 20 74   third term of t
1268f 68 65 20 74 65 73 74 20 77 61 73 20 61 64 64 65  he test was adde
12690 64 20 74 6f 20 66 69 78 20 74 69 63 6b 65 74 20  d to fix ticket 
12691 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a 20 57 68  #2565..    ** Wh
12692 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  en rolling back 
12693 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e  a hot journal, n
12694 52 65 63 3d 3d 30 20 61 6c 77 61 79 73 20 6d 65  Rec==0 always me
12695 61 6e 73 20 74 68 61 74 20 74 68 65 20 6e 65 78  ans that the nex
12696 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e 6b 20 6f  t.    ** chunk o
12697 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 63 6f  f the journal co
12698 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70 61 67 65  ntains zero page
12699 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  s to be rolled b
1269a 61 63 6b 2e 20 20 42 75 74 0a 20 20 20 20 2a 2a  ack.  But.    **
1269b 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 52 4f   when doing a RO
1269c 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68 65 20 6e  LLBACK and the n
1269d 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20 69 73 20  Rec==0 chunk is 
1269e 74 68 65 20 6c 61 73 74 20 63 68 75 6e 6b 20 69  the last chunk i
1269f 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75  n.    ** the jou
126a0 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74  rnal, it means t
126a1 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
126a2 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 64  might contain ad
126a3 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20  ditional.    ** 
126a4 70 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20  pages that need 
126a5 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  to be rolled bac
126a6 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 6e  k and that the n
126a7 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 0a  umber of pages .
126a8 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65      ** should be
126a9 20 63 6f 6d 70 75 74 65 64 20 62 61 73 65 64 20   computed based 
126aa 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  on the journal f
126ab 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f  ile size..    */
126ac 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30  .    if( nRec==0
126ad 20 26 26 20 21 69 73 48 6f 74 20 26 26 0a 20 20   && !isHot &&.  
126ae 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
126af 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c  urnalHdr+JOURNAL
126b0 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d  _HDR_SZ(pPager)=
126b1 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  =pPager->journal
126b2 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 6e 52 65  Off ){.      nRe
126b3 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d  c = (int)((szJ -
126b4 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
126b5 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50  Off) / JOURNAL_P
126b6 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20  G_SZ(pPager));. 
126b7 20 20 20 20 20 69 73 55 6e 73 79 6e 63 20 3d 20       isUnsync = 
126b8 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  1;.    }..    /*
126b9 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
126ba 66 69 72 73 74 20 68 65 61 64 65 72 20 72 65 61  first header rea
126bb 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  d from the journ
126bc 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65  al, truncate the
126bd 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
126be 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74   file back to it
126bf 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e  s original size.
126c0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
126c1 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
126c2 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ff==JOURNAL_HDR_
126c3 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  SZ(pPager) ){.  
126c4 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74      rc = pager_t
126c5 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20  runcate(pPager, 
126c6 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28  mxPg);.      if(
126c7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
126c8 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  {.        goto e
126c9 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20  nd_playback;.   
126ca 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65     }.      pPage
126cb 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67  r->dbSize = mxPg
126cc 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
126cd 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61  Copy original pa
126ce 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a  ges out of the j
126cf 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20  ournal and back 
126d0 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a  into the .    **
126d1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
126d2 6e 64 2f 6f 72 20 70 61 67 65 20 63 61 63 68 65  nd/or page cache
126d3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
126d4 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75 2b  (u=0; u<nRec; u+
126d5 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65  +){.      if( ne
126d6 65 64 50 61 67 65 72 52 65 73 65 74 20 29 7b 0a  edPagerReset ){.
126d7 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65          pager_re
126d8 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20  set(pPager);.   
126d9 20 20 20 20 20 6e 65 65 64 50 61 67 65 72 52 65       needPagerRe
126da 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  set = 0;.      }
126db 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
126dc 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
126dd 61 67 65 28 70 50 61 67 65 72 2c 31 2c 69 73 55  age(pPager,1,isU
126de 6e 73 79 6e 63 2c 26 70 50 61 67 65 72 2d 3e 6a  nsync,&pPager->j
126df 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 30 29 3b 0a  ournalOff,0,0);.
126e0 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
126e1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
126e2 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
126e3 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  E_DONE ){.      
126e4 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
126e5 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50  OK;.          pP
126e6 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
126e7 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 20 20   = szJ;.        
126e8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
126e9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
126ea 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 75    /* If we are u
126eb 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63  nable to rollbac
126ec 6b 2c 20 71 75 69 74 20 61 6e 64 20 72 65 74 75  k, quit and retu
126ed 72 6e 20 74 68 65 20 65 72 72 6f 72 0a 20 20 20  rn the error.   
126ee 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e 20         ** code. 
126ef 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65   This will cause
126f0 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e   the pager to en
126f1 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74  ter the error st
126f2 61 74 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ate.          **
126f3 20 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72 74   so that no furt
126f4 68 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62 65  her harm will be
126f5 20 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73 20   done.  Perhaps 
126f6 74 68 65 20 6e 65 78 74 0a 20 20 20 20 20 20 20  the next.       
126f7 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74 6f     ** process to
126f8 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c 6c   come along will
126f9 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c   be able to roll
126fa 62 61 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  back the databas
126fb 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
126fc 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
126fd 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20  nd_playback;.   
126fe 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
126ff 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54     }.  }.  /*NOT
12700 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73 65  REACHED*/.  asse
12701 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c  rt( 0 );..end_pl
12702 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c  ayback:.  /* Fol
12703 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63  lowing a rollbac
12704 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  k, the database 
12705 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 62  file should be b
12706 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 69 67 69  ack in its origi
12707 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 65 20 70  nal.  ** state p
12708 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72  rior to the star
12709 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  t of the transac
1270a 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65 20  tion, so invoke 
1270b 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  the.  ** SQLITE_
1270c 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47  FCNTL_DB_UNCHANG
1270d 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20  ED file-control 
1270e 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 61 62 6c  method to disabl
1270f 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 73 65 72  e the.  ** asser
12710 74 69 6f 6e 20 74 68 61 74 20 74 68 65 20 74 72  tion that the tr
12711 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65  ansaction counte
12712 72 20 77 61 73 20 6d 6f 64 69 66 69 65 64 2e 0a  r was modified..
12713 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 0a 20    */.  assert(. 
12714 20 20 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70     pPager->fd->p
12715 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c 0a 20 20  Methods==0 ||.  
12716 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43    sqlite3OsFileC
12717 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 2d 3e 66  ontrol(pPager->f
12718 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44  d,SQLITE_FCNTL_D
12719 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30 29 3e 3d  B_UNCHANGED,0)>=
1271a 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 3b 0a 0a  SQLITE_OK.  );..
1271b 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 6c 61    /* If this pla
1271c 79 62 61 63 6b 20 69 73 20 68 61 70 70 65 6e 69  yback is happeni
1271d 6e 67 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ng automatically
1271e 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
1271f 61 6e 20 49 4f 20 6f 72 20 0a 20 20 2a 2a 20 6d  an IO or .  ** m
12720 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74 68 61 74  alloc error that
12721 20 6f 63 63 75 72 72 65 64 20 61 66 74 65 72 20   occurred after 
12722 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
12723 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20 62  er was updated b
12724 75 74 20 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20  ut .  ** before 
12725 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
12726 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74  was committed, t
12727 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63  hen the change-c
12728 6f 75 6e 74 65 72 20 0a 20 20 2a 2a 20 6d 6f 64  ounter .  ** mod
12729 69 66 69 63 61 74 69 6f 6e 20 6d 61 79 20 6a 75  ification may ju
1272a 73 74 20 68 61 76 65 20 62 65 65 6e 20 72 65 76  st have been rev
1272b 65 72 74 65 64 2e 20 49 66 20 74 68 69 73 20 68  erted. If this h
1272c 61 70 70 65 6e 73 20 69 6e 20 65 78 63 6c 75 73  appens in exclus
1272d 69 76 65 20 0a 20 20 2a 2a 20 6d 6f 64 65 2c 20  ive .  ** mode, 
1272e 74 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20  then subsequent 
1272f 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 70 65 72  transactions per
12730 66 6f 72 6d 65 64 20 62 79 20 74 68 65 20 63 6f  formed by the co
12731 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f  nnection will no
12732 74 0a 20 20 2a 2a 20 75 70 64 61 74 65 20 74 68  t.  ** update th
12733 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
12734 20 61 74 20 61 6c 6c 2e 20 54 68 69 73 20 6d 61   at all. This ma
12735 79 20 6c 65 61 64 20 74 6f 20 63 61 63 68 65 20  y lead to cache 
12736 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 20  inconsistency.  
12737 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  ** problems for 
12738 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20  other processes 
12739 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e  at some point in
1273a 20 74 68 65 20 66 75 74 75 72 65 2e 20 53 6f 2c   the future. So,
1273b 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e 20 63 61   just.  ** in ca
1273c 73 65 20 74 68 69 73 20 68 61 73 20 68 61 70 70  se this has happ
1273d 65 6e 65 64 2c 20 63 6c 65 61 72 20 74 68 65 20  ened, clear the 
1273e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20  changeCountDone 
1273f 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20  flag now..  */. 
12740 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43   pPager->changeC
12741 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65  ountDone = pPage
12742 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 0a 20 20  r->tempFile;..  
12743 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12744 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72  K ){.    zMaster
12745 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53   = pPager->pTmpS
12746 70 61 63 65 3b 0a 20 20 20 20 72 63 20 3d 20 72  pace;.    rc = r
12747 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  eadMasterJournal
12748 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d  (pPager->jfd, zM
12749 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70  aster, pPager->p
1274a 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
1274b 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  1);.    testcase
1274c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1274d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
1274e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1274f 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64    rc = pager_end
12750 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  _transaction(pPa
12751 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21  ger, zMaster[0]!
12752 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74 65 73 74  ='\0');.    test
12753 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45  case( rc!=SQLITE
12754 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28  _OK );.  }.  if(
12755 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
12756 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26 20  & zMaster[0] && 
12757 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  res ){.    /* If
12758 20 74 68 65 72 65 20 77 61 73 20 61 20 6d 61 73   there was a mas
12759 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  ter journal and 
1275a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
1275b 6c 20 72 65 74 75 72 6e 20 73 75 63 63 65 73 73  l return success
1275c 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20  ,.    ** see if 
1275d 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
1275e 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  o delete the mas
1275f 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20  ter journal..   
12760 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67   */.    rc = pag
12761 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 50 61  er_delmaster(pPa
12762 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  ger, zMaster);. 
12763 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21     testcase( rc!
12764 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
12765 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 67 65  }..  /* The Page
12766 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72  r.sectorSize var
12767 69 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20 62  iable may have b
12768 65 65 6e 20 75 70 64 61 74 65 64 20 77 68 69 6c  een updated whil
12769 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a 20 62  e rolling.  ** b
1276a 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 63 72  ack a journal cr
1276b 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65  eated by a proce
1276c 73 73 20 77 69 74 68 20 61 20 64 69 66 66 65 72  ss with a differ
1276d 65 6e 74 20 73 65 63 74 6f 72 20 73 69 7a 65 0a  ent sector size.
1276e 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 73 65    ** value. Rese
1276f 74 20 69 74 20 74 6f 20 74 68 65 20 63 6f 72 72  t it to the corr
12770 65 63 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ect value for th
12771 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a 2f  is process..  */
12772 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65  .  setSectorSize
12773 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75  (pPager);.  retu
12774 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12775 50 6c 61 79 62 61 63 6b 20 73 61 76 65 70 6f 69  Playback savepoi
12776 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 2e 20 4f  nt pSavepoint. O
12777 72 2c 20 69 66 20 70 53 61 76 65 70 6f 69 6e 74  r, if pSavepoint
12778 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 6c 61  ==NULL, then pla
12779 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 6e 74  yback.** the ent
1277a 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  ire master journ
1277b 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 61 73  al file. The cas
1277c 65 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55  e pSavepoint==NU
1277d 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 0a  LL occurs when .
1277e 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f  ** a ROLLBACK TO
1277f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e 76 6f   command is invo
12780 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 50 4f 49  ked on a SAVEPOI
12781 4e 54 20 74 68 61 74 20 69 73 20 61 20 74 72 61  NT that is a tra
12782 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 61 76  nsaction .** sav
12783 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68  epoint..**.** Wh
12784 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73  en pSavepoint is
12785 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 6e 69   not NULL (meani
12786 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 63  ng a non-transac
12787 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  tion savepoint i
12788 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c 6c  s .** being roll
12789 65 64 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 74  ed back), then t
1278a 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73  he rollback cons
1278b 69 73 74 73 20 6f 66 20 75 70 20 74 6f 20 74 68  ists of up to th
1278c 72 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a 20 70  ree stages,.** p
1278d 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 20  erformed in the 
1278e 6f 72 64 65 72 20 73 70 65 63 69 66 69 65 64 3a  order specified:
1278f 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73  .**.**   * Pages
12790 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b   are played back
12791 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a   from the main j
12792 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20  ournal starting 
12793 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 6f  at byte.**     o
12794 66 66 73 65 74 20 50 61 67 65 72 53 61 76 65 70  ffset PagerSavep
12795 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64  oint.iOffset and
12796 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 0a   continuing to .
12797 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65  **     PagerSave
12798 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74  point.iHdrOffset
12799 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 6e 64 20  , or to the end 
1279a 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  of the main jour
1279b 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20  nal.**     file 
1279c 69 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  if PagerSavepoin
1279d 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20  t.iHdrOffset is 
1279e 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  zero..**.**   * 
1279f 49 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  If PagerSavepoin
127a0 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20  t.iHdrOffset is 
127a1 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 70  not zero, then p
127a2 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 0a  ages are played.
127a3 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 74 61 72  **     back star
127a4 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f  ting from the jo
127a5 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6d 6d  urnal header imm
127a6 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
127a7 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72  ng .**     Pager
127a8 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66  Savepoint.iHdrOf
127a9 66 73 65 74 20 74 6f 20 74 68 65 20 65 6e 64 20  fset to the end 
127aa 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  of the main jour
127ab 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  nal file..**.** 
127ac 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 74 68    * Pages are th
127ad 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66  en played back f
127ae 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  rom the sub-jour
127af 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 74 69  nal file, starti
127b0 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 20 74  ng.**     with t
127b1 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  he PagerSavepoin
127b2 74 2e 69 53 75 62 52 65 63 20 61 6e 64 20 63 6f  t.iSubRec and co
127b3 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20  ntinuing to the 
127b4 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 74 68  end of.**     th
127b5 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
127b6 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 74  **.** Throughout
127b7 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 72   the rollback pr
127b8 6f 63 65 73 73 2c 20 65 61 63 68 20 74 69 6d 65  ocess, each time
127b9 20 61 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65   a page is rolle
127ba 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 63  d back, the.** c
127bb 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74  orresponding bit
127bc 20 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 74   is set in a bit
127bd 76 65 63 20 73 74 72 75 63 74 75 72 65 20 28 76  vec structure (v
127be 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 6e  ariable pDone in
127bf 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e   the.** implemen
127c0 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 54  tation below). T
127c1 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 65  his is used to e
127c2 6e 73 75 72 65 20 74 68 61 74 20 61 20 70 61 67  nsure that a pag
127c3 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c  e is only.** rol
127c4 6c 65 64 20 62 61 63 6b 20 74 68 65 20 66 69 72  led back the fir
127c5 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e  st time it is en
127c6 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69 74  countered in eit
127c7 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a  her journal..**.
127c8 2a 2a 20 49 66 20 70 53 61 76 65 70 6f 69 6e 74  ** If pSavepoint
127c9 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70   is NULL, then p
127ca 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 70 6c  ages are only pl
127cb 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74  ayed back from t
127cc 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e  he main.** journ
127cd 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 20 69  al file. There i
127ce 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20  s no need for a 
127cf 62 69 74 76 65 63 20 69 6e 20 74 68 69 73 20 63  bitvec in this c
127d0 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69  ase..**.** In ei
127d1 74 68 65 72 20 63 61 73 65 2c 20 62 65 66 6f 72  ther case, befor
127d2 65 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d 6d 65  e playback comme
127d3 6e 63 65 73 20 74 68 65 20 50 61 67 65 72 2e 64  nces the Pager.d
127d4 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a  bSize variable.*
127d5 2a 20 69 73 20 72 65 73 65 74 20 74 6f 20 74 68  * is reset to th
127d6 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 74 20  e value that it 
127d7 68 65 6c 64 20 61 74 20 74 68 65 20 73 74 61 72  held at the star
127d8 74 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69  t of the savepoi
127d9 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 6e 73  nt .** (or trans
127da 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 67 65  action). No page
127db 20 77 69 74 68 20 61 20 70 61 67 65 2d 6e 75 6d   with a page-num
127dc 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e  ber greater than
127dd 20 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a 20 69   this value.** i
127de 73 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 49  s played back. I
127df 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 6e 74  f one is encount
127e0 65 72 65 64 20 69 74 20 69 73 20 73 69 6d 70 6c  ered it is simpl
127e1 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a 73 74  y skipped..*/.st
127e2 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 50 6c  atic int pagerPl
127e3 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28  aybackSavepoint(
127e4 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
127e5 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70  agerSavepoint *p
127e6 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 36  Savepoint){.  i6
127e7 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20  4 szJ;          
127e8 20 20 20 20 20 20 20 2f 2a 20 45 66 66 65 63 74         /* Effect
127e9 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  ive size of the 
127ea 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  main journal */.
127eb 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20    i64 iHdrOff;  
127ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
127ed 64 20 6f 66 20 66 69 72 73 74 20 73 65 67 6d 65  d of first segme
127ee 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e  nt of main-journ
127ef 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a 20 20  al records */.  
127f0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
127f1 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75  OK;      /* Retu
127f2 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 69 74  rn code */.  Bit
127f3 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b 20  vec *pDone = 0; 
127f4 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20        /* Bitvec 
127f5 74 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 20  to ensure pages 
127f6 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c 79  played back only
127f7 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65   once */..  asse
127f8 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
127f9 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20  e>=PAGER_SHARED 
127fa 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  );..  /* Allocat
127fb 65 20 61 20 62 69 74 76 65 63 20 74 6f 20 75 73  e a bitvec to us
127fc 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73  e to store the s
127fd 65 74 20 6f 66 20 70 61 67 65 73 20 72 6f 6c 6c  et of pages roll
127fe 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28  ed back */.  if(
127ff 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20   pSavepoint ){. 
12800 20 20 20 70 44 6f 6e 65 20 3d 20 73 71 6c 69 74     pDone = sqlit
12801 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70  e3BitvecCreate(p
12802 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67  Savepoint->nOrig
12803 29 3b 0a 20 20 20 20 69 66 28 20 21 70 44 6f 6e  );.    if( !pDon
12804 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  e ){.      retur
12805 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
12806 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
12807 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  Set the database
12808 20 73 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 68   size back to th
12809 65 20 76 61 6c 75 65 20 69 74 20 77 61 73 20 62  e value it was b
1280a 65 66 6f 72 65 20 74 68 65 20 73 61 76 65 70 6f  efore the savepo
1280b 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69 6e 67 20  int .  ** being 
1280c 72 65 76 65 72 74 65 64 20 77 61 73 20 6f 70 65  reverted was ope
1280d 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67  ned..  */.  pPag
1280e 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 53 61  er->dbSize = pSa
1280f 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 65 70  vepoint ? pSavep
12810 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 70 50  oint->nOrig : pP
12811 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
12812 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70 50 61 67  ;..  /* Use pPag
12813 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 61  er->journalOff a
12814 73 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20  s the effective 
12815 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e  size of the main
12816 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a   rollback.  ** j
12817 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 61 63 74  ournal.  The act
12818 75 61 6c 20 66 69 6c 65 20 6d 69 67 68 74 20 62  ual file might b
12819 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  e larger than th
1281a 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41 47 45 52  is in.  ** PAGER
1281b 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55  _JOURNALMODE_TRU
1281c 4e 43 41 54 45 20 6f 72 20 50 41 47 45 52 5f 4a  NCATE or PAGER_J
1281d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49  OURNALMODE_PERSI
1281e 53 54 2e 20 20 42 75 74 20 61 6e 79 74 68 69 6e  ST.  But anythin
1281f 67 0a 20 20 2a 2a 20 70 61 73 74 20 70 50 61 67  g.  ** past pPag
12820 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69  er->journalOff i
12821 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20 74 6f 20  s off-limits to 
12822 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a 4a 20 3d  us..  */.  szJ =
12823 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12824 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  Off;..  /* Begin
12825 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b   by rolling back
12826 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68   records from th
12827 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73  e main journal s
12828 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20  tarting at.  ** 
12829 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69  PagerSavepoint.i
1282a 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69  Offset and conti
1282b 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 78  nuing to the nex
1282c 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  t journal header
1282d 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 6d 69 67  ..  ** There mig
1282e 68 74 20 62 65 20 72 65 63 6f 72 64 73 20 69 6e  ht be records in
1282f 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
12830 6c 20 74 68 61 74 20 68 61 76 65 20 61 20 70 61  l that have a pa
12831 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 67  ge number.  ** g
12832 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
12833 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65  current database
12834 20 73 69 7a 65 20 28 70 50 61 67 65 72 2d 3e 64   size (pPager->d
12835 62 53 69 7a 65 29 20 62 75 74 20 74 68 6f 73 65  bSize) but those
12836 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 6b  .  ** will be sk
12837 69 70 70 65 64 20 61 75 74 6f 6d 61 74 69 63 61  ipped automatica
12838 6c 6c 79 2e 20 20 50 61 67 65 73 20 61 72 65 20  lly.  Pages are 
12839 61 64 64 65 64 20 74 6f 20 70 44 6f 6e 65 20 61  added to pDone a
1283a 73 20 74 68 65 79 0a 20 20 2a 2a 20 61 72 65 20  s they.  ** are 
1283b 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a  played back..  *
1283c 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69  /.  if( pSavepoi
1283d 6e 74 20 29 7b 0a 20 20 20 20 69 48 64 72 4f 66  nt ){.    iHdrOf
1283e 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e  f = pSavepoint->
1283f 69 48 64 72 4f 66 66 73 65 74 20 3f 20 70 53 61  iHdrOffset ? pSa
12840 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66  vepoint->iHdrOff
12841 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 20 20 70  set : szJ;.    p
12842 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
12843 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e  f = pSavepoint->
12844 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69  iOffset;.    whi
12845 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
12846 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75  K && pPager->jou
12847 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f 66 66 20  rnalOff<iHdrOff 
12848 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  ){.      rc = pa
12849 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65  ger_playback_one
1284a 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c  _page(pPager, 1,
1284b 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75   0, &pPager->jou
1284c 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e  rnalOff, 1, pDon
1284d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  e);.    }.    as
1284e 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
1284f 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 65 6c 73 65  _DONE );.  }else
12850 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
12851 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20  urnalOff = 0;.  
12852 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65  }..  /* Continue
12853 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65   rolling back re
12854 63 6f 72 64 73 20 6f 75 74 20 6f 66 20 74 68 65  cords out of the
12855 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74   main journal st
12856 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 74  arting at.  ** t
12857 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c  he first journal
12858 20 68 65 61 64 65 72 20 73 65 65 6e 20 61 6e 64   header seen and
12859 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75 6e 74 69   continuing unti
1285a 6c 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20  l the effective 
1285b 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  end.  ** of the 
1285c 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  main journal fil
1285d 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74 6f 20  e.  Continue to 
1285e 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 61 6e 67  skip out-of-rang
1285f 65 20 70 61 67 65 73 20 61 6e 64 0a 20 20 2a 2a  e pages and.  **
12860 20 63 6f 6e 74 69 6e 75 65 20 61 64 64 69 6e 67   continue adding
12861 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61   pages rolled ba
12862 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a 20 20 2a  ck to pDone..  *
12863 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  /.  while( rc==S
12864 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67  QLITE_OK && pPag
12865 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73  er->journalOff<s
12866 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32 20 69 69  zJ ){.    u32 ii
12867 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
12868 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
12869 20 20 20 20 75 33 32 20 6e 4a 52 65 63 20 3d 20      u32 nJRec = 
1286a 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  0;     /* Number
1286b 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f   of Journal Reco
1286c 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64  rds */.    u32 d
1286d 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72  ummy;.    rc = r
1286e 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  eadJournalHdr(pP
1286f 61 67 65 72 2c 20 30 2c 20 73 7a 4a 2c 20 26 6e  ager, 0, szJ, &n
12870 4a 52 65 63 2c 20 26 64 75 6d 6d 79 29 3b 0a 20  JRec, &dummy);. 
12871 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
12872 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 0a 20  QLITE_DONE );.. 
12873 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65     /*.    ** The
12874 20 22 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61   "pPager->journa
12875 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52  lHdr+JOURNAL_HDR
12876 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61  _SZ(pPager)==pPa
12877 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 22  ger->journalOff"
12878 0a 20 20 20 20 2a 2a 20 74 65 73 74 20 69 73 20  .    ** test is 
12879 72 65 6c 61 74 65 64 20 74 6f 20 74 69 63 6b 65  related to ticke
1287a 74 20 23 32 35 36 35 2e 20 20 53 65 65 20 74 68  t #2565.  See th
1287b 65 20 64 69 73 63 75 73 73 69 6f 6e 20 69 6e 20  e discussion in 
1287c 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72  the.    ** pager
1287d 5f 70 6c 61 79 62 61 63 6b 28 29 20 66 75 6e 63  _playback() func
1287e 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f  tion for additio
1287f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
12880 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
12881 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20 20 20 26  nJRec==0 .     &
12882 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
12883 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52  lHdr+JOURNAL_HDR
12884 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61  _SZ(pPager)==pPa
12885 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a  ger->journalOff.
12886 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 4a 52      ){.      nJR
12887 65 63 20 3d 20 28 75 33 32 29 28 28 73 7a 4a 20  ec = (u32)((szJ 
12888 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  - pPager->journa
12889 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47  lOff)/JOURNAL_PG
1288a 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20  _SZ(pPager));.  
1288b 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30    }.    for(ii=0
1288c 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
1288d 26 26 20 69 69 3c 6e 4a 52 65 63 20 26 26 20 70  && ii<nJRec && p
1288e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
1288f 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 7b 0a 20 20  f<szJ; ii++){.  
12890 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70      rc = pager_p
12891 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65  layback_one_page
12892 28 70 50 61 67 65 72 2c 20 31 2c 20 30 2c 20 26  (pPager, 1, 0, &
12893 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12894 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20  ff, 1, pDone);. 
12895 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
12896 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
12897 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   );.  }.  assert
12898 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
12899 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
1289a 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 3b 0a 0a 20  alOff==szJ );.. 
1289b 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 20 72 6f   /* Finally,  ro
1289c 6c 6c 62 61 63 6b 20 70 61 67 65 73 20 66 72 6f  llback pages fro
1289d 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  m the sub-journa
1289e 6c 2e 20 20 50 61 67 65 20 74 68 61 74 20 77 65  l.  Page that we
1289f 72 65 0a 20 20 2a 2a 20 70 72 65 76 69 6f 75 73  re.  ** previous
128a0 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 6f  ly rolled back o
128a1 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  ut of the main j
128a2 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61 72 65 20  ournal (and are 
128a3 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e 65 29 0a  hence in pDone).
128a4 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 6b 69    ** will be ski
128a5 70 70 65 64 2e 20 20 4f 75 74 2d 6f 66 2d 72 61  pped.  Out-of-ra
128a6 6e 67 65 20 70 61 67 65 73 20 61 72 65 20 61 6c  nge pages are al
128a7 73 6f 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f  so skipped..  */
128a8 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e  .  if( pSavepoin
128a9 74 20 29 7b 0a 20 20 20 20 75 33 32 20 69 69 3b  t ){.    u32 ii;
128aa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
128ab 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
128ac 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20     i64 offset = 
128ad 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62  pSavepoint->iSub
128ae 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70  Rec*(4+pPager->p
128af 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 66 6f  ageSize);.    fo
128b0 72 28 69 69 3d 70 53 61 76 65 70 6f 69 6e 74 2d  r(ii=pSavepoint-
128b1 3e 69 53 75 62 52 65 63 3b 20 72 63 3d 3d 53 51  >iSubRec; rc==SQ
128b2 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 70 50  LITE_OK && ii<pP
128b3 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 20 69  ager->nSubRec; i
128b4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65  i++){.      asse
128b5 72 74 28 20 6f 66 66 73 65 74 3d 3d 69 69 2a 28  rt( offset==ii*(
128b6 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  4+pPager->pageSi
128b7 7a 65 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20  ze) );.      rc 
128b8 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
128b9 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72  _one_page(pPager
128ba 2c 20 30 2c 20 30 2c 20 26 6f 66 66 73 65 74 2c  , 0, 0, &offset,
128bb 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20   1, pDone);.    
128bc 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  }.    assert( rc
128bd 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  !=SQLITE_DONE );
128be 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42  .  }..  sqlite3B
128bf 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 44 6f  itvecDestroy(pDo
128c0 6e 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ne);.  if( rc==S
128c1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
128c2 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
128c3 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a 20 20  ff = szJ;.  }.  
128c4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
128c5 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6d  .** Change the m
128c6 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
128c7 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
128c8 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f 77 65   that are allowe
128c9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
128ca 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
128cb 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 69  3PagerSetCachesi
128cc 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
128cd 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
128ce 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65   sqlite3PcacheSe
128cf 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65  tCachesize(pPage
128d0 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d 78 50 61  r->pPCache, mxPa
128d1 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  ge);.}../*.** Ad
128d2 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73 74 6e  just the robustn
128d3 65 73 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  ess of the datab
128d4 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20 64 75  ase to damage du
128d5 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 0a  e to OS crashes.
128d6 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c  ** or power fail
128d7 75 72 65 73 20 62 79 20 63 68 61 6e 67 69 6e 67  ures by changing
128d8 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
128d9 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77 72 69  yncs()s when wri
128da 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c  ting.** the roll
128db 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 54  back journal.  T
128dc 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 6c  here are three l
128dd 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  evels:.**.**    
128de 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69 74 65  OFF       sqlite
128df 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e 65 76  3OsSync() is nev
128e0 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73  er called.  This
128e1 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 0a   is the default.
128e2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
128e3 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 61 6e  for temporary an
128e4 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65  d transient file
128e5 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f 52 4d  s..**.**    NORM
128e6 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61  AL    The journa
128e7 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e 63 65  l is synced once
128e8 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62   before writes b
128e9 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20  egin on the.**  
128ea 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
128eb 62 61 73 65 2e 20 20 54 68 69 73 20 69 73 20 6e  base.  This is n
128ec 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61 74 65  ormally adequate
128ed 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62 75 74   protection, but
128ee 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
128ef 20 69 74 20 69 73 20 74 68 65 6f 72 65 74 69 63   it is theoretic
128f0 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c 20 74  ally possible, t
128f1 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c 69 6b  hough very unlik
128f2 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ely,.**         
128f3 20 20 20 20 20 74 68 61 74 20 61 6e 20 69 6e 6f       that an ino
128f4 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20 66 61  pertune power fa
128f5 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 61 76  ilure could leav
128f6 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  e the journal.**
128f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
128f8 20 61 20 73 74 61 74 65 20 77 68 69 63 68 20 77   a state which w
128f9 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d 61 67  ould cause damag
128fa 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
128fb 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
128fc 20 20 77 68 65 6e 20 69 74 20 69 73 20 72 6f 6c    when it is rol
128fd 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  led back..**.** 
128fe 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54 68 65     FULL      The
128ff 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63   journal is sync
12900 65 64 20 74 77 69 63 65 20 62 65 66 6f 72 65 20  ed twice before 
12901 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20  writes begin on 
12902 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
12903 20 20 20 20 64 61 74 61 62 61 73 65 20 28 77 69      database (wi
12904 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e  th some addition
12905 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d  al information -
12906 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a   the nRec field.
12907 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
12908 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  of the journal h
12909 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20 77 72  eader - being wr
1290a 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65 65 6e  itten in between
1290b 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20 20 20   the two.**     
1290c 20 20 20 20 20 20 20 20 20 73 79 6e 63 73 29 2e           syncs).
1290d 20 20 49 66 20 77 65 20 61 73 73 75 6d 65 20 74    If we assume t
1290e 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a 2a 2a  hat writing a.**
1290f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
12910 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72  ngle disk sector
12911 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68 65 6e   is atomic, then
12912 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f 76 69   this mode provi
12913 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  des.**          
12914 20 20 20 20 61 73 73 75 72 61 6e 63 65 20 74 68      assurance th
12915 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  at the journal w
12916 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72 72 75  ill not be corru
12917 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 20  pted to the.**  
12918 20 20 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e              poin
12919 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64 61 6d  t of causing dam
1291a 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  age to the datab
1291b 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62  ase during rollb
1291c 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 72  ack..**.** Numer
1291d 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f 63 69  ic values associ
1291e 61 74 65 64 20 77 69 74 68 20 74 68 65 73 65 20  ated with these 
1291f 73 74 61 74 65 73 20 61 72 65 20 4f 46 46 3d 3d  states are OFF==
12920 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a 20  1, NORMAL=2,.** 
12921 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a 23  and FULL=3..*/.#
12922 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
12923 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
12924 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12925 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
12926 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28  rSetSafetyLevel(
12927 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
12928 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 62 46  nt level, int bF
12929 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 70 50 61  ullFsync){.  pPa
1292a 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 20 28  ger->noSync =  (
1292b 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 50 61 67  level==1 || pPag
1292c 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31  er->tempFile) ?1
1292d 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 75  :0;.  pPager->fu
1292e 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76 65 6c 3d  llSync = (level=
1292f 3d 33 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74  =3 && !pPager->t
12930 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0a 20  empFile) ?1:0;. 
12931 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
12932 61 67 73 20 3d 20 28 62 46 75 6c 6c 46 73 79 6e  ags = (bFullFsyn
12933 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  c?SQLITE_SYNC_FU
12934 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  LL:SQLITE_SYNC_N
12935 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 20 70 50  ORMAL);.  if( pP
12936 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 20 70  ager->noSync ) p
12937 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
12938 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  = 0;.}.#endif../
12939 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1293a 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  ng global variab
1293b 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  le is incremente
1293c 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 6c  d whenever the l
1293d 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 65 6d 70  ibrary.** attemp
1293e 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d  ts to open a tem
1293f 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 54 68  porary file.  Th
12940 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  is information i
12941 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65  s used for.** te
12942 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73  sting and analys
12943 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a 23 69  is only.  .*/.#i
12944 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
12945 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
12946 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70  sqlite3_opentemp
12947 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64  _count = 0;.#end
12948 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61  if../*.** Open a
12949 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e   temporary file.
1294a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
1294b 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1294c 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e 20 52 65   into *pFile. Re
1294d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
1294e 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a 20 6f 72  n success .** or
1294f 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f   some other erro
12950 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 61 69  r code if we fai
12951 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c 20 61  l. The OS will a
12952 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a  utomatically .**
12953 20 64 65 6c 65 74 65 20 74 68 65 20 74 65 6d 70   delete the temp
12954 6f 72 61 72 79 20 66 69 6c 65 20 77 68 65 6e 20  orary file when 
12955 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a  it is closed..**
12956 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61  .** The flags pa
12957 73 73 65 64 20 74 6f 20 74 68 65 20 56 46 53 20  ssed to the VFS 
12958 6c 61 79 65 72 20 78 4f 70 65 6e 28 29 20 63 61  layer xOpen() ca
12959 6c 6c 20 61 72 65 20 74 68 6f 73 65 20 73 70 65  ll are those spe
1295a 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 70 61 72  cified.** by par
1295b 61 6d 65 74 65 72 20 76 66 73 46 6c 61 67 73 20  ameter vfsFlags 
1295c 4f 52 65 64 20 77 69 74 68 20 74 68 65 20 66 6f  ORed with the fo
1295d 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
1295e 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52     SQLITE_OPEN_R
1295f 45 41 44 57 52 49 54 45 0a 2a 2a 20 20 20 20 20  EADWRITE.**     
12960 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
12961 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  TE.**     SQLITE
12962 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 0a  _OPEN_EXCLUSIVE.
12963 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  **     SQLITE_OP
12964 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
12965 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
12966 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 0a 20 20  agerOpentemp(.  
12967 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20  Pager *pPager,  
12968 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
12969 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73  er object */.  s
1296a 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
1296b 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 20 74 68  le,  /* Write th
1296c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
1296d 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  r here */.  int 
1296e 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20  vfsFlags        
1296f 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65    /* Flags passe
12970 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  d through to the
12971 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74   VFS */.){.  int
12972 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
12973 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
12974 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  e */..#ifdef SQL
12975 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74  ITE_TEST.  sqlit
12976 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e  e3_opentemp_coun
12977 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 20 66 6f  t++;  /* Used fo
12978 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e  r testing and an
12979 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a 2f 0a 23  alysis only */.#
1297a 65 6e 64 69 66 0a 0a 20 20 76 66 73 46 6c 61 67  endif..  vfsFlag
1297b 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f 50 45  s |=  SQLITE_OPE
1297c 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51  N_READWRITE | SQ
1297d 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
1297e 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 53   |.            S
1297f 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
12980 53 49 56 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50  SIVE | SQLITE_OP
12981 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
12982 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
12983 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 70  OsOpen(pPager->p
12984 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c 20 76  Vfs, 0, pFile, v
12985 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20 61  fsFlags, 0);.  a
12986 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
12987 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70  E_OK || isOpen(p
12988 46 69 6c 65 29 20 29 3b 0a 20 20 72 65 74 75 72  File) );.  retur
12989 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
1298a 65 74 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  et the busy hand
1298b 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  ler function..**
1298c 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 6e  .** The pager in
1298d 76 6f 6b 65 73 20 74 68 65 20 62 75 73 79 2d 68  vokes the busy-h
1298e 61 6e 64 6c 65 72 20 69 66 20 73 71 6c 69 74 65  andler if sqlite
1298f 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74 75 72 6e  3OsLock() return
12990 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53  s .** SQLITE_BUS
12991 59 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74 6f  Y when trying to
12992 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 6e 6f   upgrade from no
12993 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48 41 52 45  -lock to a SHARE
12994 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72 20 77 68  D lock,.** or wh
12995 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 70 67  en trying to upg
12996 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45 53 45  rade from a RESE
12997 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 6e 20  RVED lock to an 
12998 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a 20 6c 6f  EXCLUSIVE .** lo
12999 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a 6e 6f 74  ck. It does *not
1299a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  * invoke the bus
1299b 79 20 68 61 6e 64 6c 65 72 20 77 68 65 6e 20 75  y handler when u
1299c 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 0a 2a 2a  pgrading from.**
1299d 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52   SHARED to RESER
1299e 56 45 44 2c 20 6f 72 20 77 68 65 6e 20 75 70 67  VED, or when upg
1299f 72 61 64 69 6e 67 20 66 72 6f 6d 20 53 48 41 52  rading from SHAR
129a0 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45 0a  ED to EXCLUSIVE.
129a1 2a 2a 20 28 77 68 69 63 68 20 6f 63 63 75 72 73  ** (which occurs
129a2 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72   during hot-jour
129a3 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 20 53  nal rollback). S
129a4 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20  ummary:.**.**   
129a5 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 20 20 20  Transition      
129a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129a7 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 42 75 73    | Invokes xBus
129a8 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20 20 2d 2d  yHandler.**   --
129a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129aa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129ab 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129ac 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e 4f 5f 4c  ------.**   NO_L
129ad 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20 53 48 41  OCK       -> SHA
129ae 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 20 7c 20  RED_LOCK      | 
129af 59 65 73 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f  Yes.**   SHARED_
129b0 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53 45 52 56  LOCK   -> RESERV
129b1 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20 4e 6f 0a  ED_LOCK    | No.
129b2 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b  **   SHARED_LOCK
129b3 20 20 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f     -> EXCLUSIVE_
129b4 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20  LOCK   | No.**  
129b5 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2d   RESERVED_LOCK -
129b6 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  > EXCLUSIVE_LOCK
129b7 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a 2a 20 49     | Yes.**.** I
129b8 66 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c  f the busy-handl
129b9 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  er callback retu
129ba 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
129bb 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 72 65  e lock is .** re
129bc 74 72 69 65 64 2e 20 49 66 20 69 74 20 72 65 74  tried. If it ret
129bd 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  urns zero, then 
129be 74 68 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20  the SQLITE_BUSY 
129bf 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72 65 74 75  error is.** retu
129c0 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c  rned to the call
129c1 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 72 20  er of the pager 
129c2 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  API function..*/
129c3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
129c4 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
129c5 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28  rSetBusyhandler(
129c6 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
129c7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
129c8 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72          /* Pager
129c9 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
129ca 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29   (*xBusyHandler)
129cb 28 76 6f 69 64 20 2a 29 2c 20 20 20 20 20 20 20  (void *),       
129cc 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
129cd 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66 75 6e  busy-handler fun
129ce 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
129cf 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67  *pBusyHandlerArg
129d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129d1 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70  /* Argument to p
129d2 61 73 73 20 74 6f 20 78 42 75 73 79 48 61 6e 64  ass to xBusyHand
129d3 6c 65 72 20 2a 2f 0a 29 7b 20 20 0a 20 20 70 50  ler */.){  .  pP
129d4 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c  ager->xBusyHandl
129d5 65 72 20 3d 20 78 42 75 73 79 48 61 6e 64 6c 65  er = xBusyHandle
129d6 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75  r;.  pPager->pBu
129d7 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d 20 70  syHandlerArg = p
129d8 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 3b 0a  BusyHandlerArg;.
129d9 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 20  }../*.** Report 
129da 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65  the current page
129db 20 73 69 7a 65 20 61 6e 64 20 6e 75 6d 62 65 72   size and number
129dc 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 79 74   of reserved byt
129dd 65 73 20 62 61 63 6b 0a 2a 2a 20 74 6f 20 74 68  es back.** to th
129de 65 20 63 6f 64 65 63 2e 0a 2a 2f 0a 23 69 66 64  e codec..*/.#ifd
129df 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  ef SQLITE_HAS_CO
129e0 44 45 43 0a 73 74 61 74 69 63 20 76 6f 69 64 20  DEC.static void 
129e1 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28  pagerReportSize(
129e2 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
129e3 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78 43    if( pPager->xC
129e4 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 29 7b 0a  odecSizeChng ){.
129e5 20 20 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64      pPager->xCod
129e6 65 63 53 69 7a 65 43 68 6e 67 28 70 50 61 67 65  ecSizeChng(pPage
129e7 72 2d 3e 70 43 6f 64 65 63 2c 20 70 50 61 67 65  r->pCodec, pPage
129e8 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20  r->pageSize,.   
129e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129ea 20 20 20 20 20 20 20 20 28 69 6e 74 29 70 50 61          (int)pPa
129eb 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 29 3b 0a  ger->nReserve);.
129ec 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65    }.}.#else.# de
129ed 66 69 6e 65 20 70 61 67 65 72 52 65 70 6f 72 74  fine pagerReport
129ee 53 69 7a 65 28 58 29 20 20 20 20 20 2f 2a 20 4e  Size(X)     /* N
129ef 6f 2d 6f 70 20 69 66 20 77 65 20 64 6f 20 6e 6f  o-op if we do no
129f0 74 20 73 75 70 70 6f 72 74 20 61 20 63 6f 64 65  t support a code
129f1 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  c */.#endif../*.
129f2 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61  ** Change the pa
129f3 67 65 20 73 69 7a 65 20 75 73 65 64 20 62 79 20  ge size used by 
129f4 74 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74  the Pager object
129f5 2e 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 73  . The new page s
129f6 69 7a 65 20 0a 2a 2a 20 69 73 20 70 61 73 73 65  ize .** is passe
129f7 64 20 69 6e 20 2a 70 50 61 67 65 53 69 7a 65 2e  d in *pPageSize.
129f8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
129f9 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 65 72  ger is in the er
129fa 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74  ror state when t
129fb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
129fc 63 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a 20 69 73  called, it.** is
129fd 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 76 61   a no-op. The va
129fe 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
129ff 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20  the error state 
12a00 65 72 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e  error code (i.e.
12a01 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49   .** one of SQLI
12a02 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45  TE_IOERR, SQLITE
12a03 5f 43 4f 52 52 55 50 54 20 6f 72 20 53 51 4c 49  _CORRUPT or SQLI
12a04 54 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20  TE_FULL)..**.** 
12a05 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6c  Otherwise, if al
12a06 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  l of the followi
12a07 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a  ng are true:.**.
12a08 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65 77 20 70  **   * the new p
12a09 61 67 65 20 73 69 7a 65 20 28 76 61 6c 75 65 20  age size (value 
12a0a 6f 66 20 2a 70 50 61 67 65 53 69 7a 65 29 20 69  of *pPageSize) i
12a0b 73 20 76 61 6c 69 64 20 28 61 20 70 6f 77 65 72  s valid (a power
12a0c 20 0a 2a 2a 20 20 20 20 20 6f 66 20 74 77 6f 20   .**     of two 
12a0d 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20  between 512 and 
12a0e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
12a0f 53 49 5a 45 2c 20 69 6e 63 6c 75 73 69 76 65 29  SIZE, inclusive)
12a10 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  , and.**.**   * 
12a11 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74  there are no out
12a12 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65  standing page re
12a13 66 65 72 65 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a  ferences, and.**
12a14 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61  .**   * the data
12a15 62 61 73 65 20 69 73 20 65 69 74 68 65 72 20 6e  base is either n
12a16 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  ot an in-memory 
12a17 64 61 74 61 62 61 73 65 20 6f 72 20 69 74 20 69  database or it i
12a18 73 0a 2a 2a 20 20 20 20 20 61 6e 20 69 6e 2d 6d  s.**     an in-m
12a19 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 74  emory database t
12a1a 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  hat currently co
12a1b 6e 73 69 73 74 73 20 6f 66 20 7a 65 72 6f 20 70  nsists of zero p
12a1c 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e  ages..**.** then
12a1d 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63   the pager objec
12a1e 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73  t page size is s
12a1f 65 74 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65  et to *pPageSize
12a20 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
12a21 61 67 65 20 73 69 7a 65 20 69 73 20 63 68 61 6e  age size is chan
12a22 67 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 66  ged, then this f
12a23 75 6e 63 74 69 6f 6e 20 75 73 65 73 20 73 71 6c  unction uses sql
12a24 69 74 65 33 50 61 67 65 72 4d 61 6c 6c 6f 63 28  ite3PagerMalloc(
12a25 29 20 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20  ) .** to obtain 
12a26 61 20 6e 65 77 20 50 61 67 65 72 2e 70 54 6d 70  a new Pager.pTmp
12a27 53 70 61 63 65 20 62 75 66 66 65 72 2e 20 49 66  Space buffer. If
12a28 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e   this allocation
12a29 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20 66 61 69   attempt .** fai
12a2a 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  ls, SQLITE_NOMEM
12a2b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
12a2c 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 72   the page size r
12a2d 65 6d 61 69 6e 73 20 75 6e 63 68 61 6e 67 65 64  emains unchanged
12a2e 2e 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74 68  . .** In all oth
12a2f 65 72 20 63 61 73 65 73 2c 20 53 51 4c 49 54 45  er cases, SQLITE
12a30 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
12a31 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
12a32 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 20 63  ge size is not c
12a33 68 61 6e 67 65 64 2c 20 65 69 74 68 65 72 20 62  hanged, either b
12a34 65 63 61 75 73 65 20 6f 6e 65 20 6f 66 20 74 68  ecause one of th
12a35 65 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20  e enumerated.** 
12a36 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65  conditions above
12a37 20 69 73 20 6e 6f 74 20 74 72 75 65 2c 20 74 68   is not true, th
12a38 65 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 65  e pager was in e
12a39 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20  rror state when 
12a3a 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  this.** function
12a3b 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 6f 72 20   was called, or 
12a3c 62 65 63 61 75 73 65 20 74 68 65 20 6d 65 6d 6f  because the memo
12a3d 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74  ry allocation at
12a3e 74 65 6d 70 74 20 66 61 69 6c 65 64 2c 20 0a 2a  tempt failed, .*
12a3f 2a 20 74 68 65 6e 20 2a 70 50 61 67 65 53 69 7a  * then *pPageSiz
12a40 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
12a41 6f 6c 64 2c 20 72 65 74 61 69 6e 65 64 20 70 61  old, retained pa
12a42 67 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 72  ge size before r
12a43 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  eturning..*/.SQL
12a44 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
12a45 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
12a46 61 67 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70  agesize(Pager *p
12a47 50 61 67 65 72 2c 20 75 31 36 20 2a 70 50 61 67  Pager, u16 *pPag
12a48 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65  eSize, int nRese
12a49 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  rve){.  int rc =
12a4a 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
12a4b 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
12a4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 31  ITE_OK ){.    u1
12a4d 36 20 70 61 67 65 53 69 7a 65 20 3d 20 2a 70 50  6 pageSize = *pP
12a4e 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 61 73 73  ageSize;.    ass
12a4f 65 72 74 28 20 70 61 67 65 53 69 7a 65 3d 3d 30  ert( pageSize==0
12a50 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e 3d 35   || (pageSize>=5
12a51 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d  12 && pageSize<=
12a52 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
12a53 53 49 5a 45 29 20 29 3b 0a 20 20 20 20 69 66 28  SIZE) );.    if(
12a54 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d   (pPager->memDb=
12a55 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62  =0 || pPager->db
12a56 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20 20 26 26  Size==0).     &&
12a57 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
12a58 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
12a59 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20  PCache)==0 .    
12a5a 20 26 26 20 70 61 67 65 53 69 7a 65 20 26 26 20   && pageSize && 
12a5b 70 61 67 65 53 69 7a 65 21 3d 70 50 61 67 65 72  pageSize!=pPager
12a5c 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20 20 20 20  ->pageSize .    
12a5d 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70  ){.      char *p
12a5e 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73 71  New = (char *)sq
12a5f 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
12a60 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20  pageSize);.     
12a61 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20   if( !pNew ){.  
12a62 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
12a63 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
12a64 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 61  else{.        pa
12a65 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
12a66 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  );.        pPage
12a67 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61  r->pageSize = pa
12a68 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20  geSize;.        
12a69 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
12a6a 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
12a6b 65 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67  e);.        pPag
12a6c 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20  er->pTmpSpace = 
12a6d 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71  pNew;.        sq
12a6e 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61  lite3PcacheSetPa
12a6f 67 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70  geSize(pPager->p
12a70 50 43 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65  PCache, pageSize
12a71 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
12a72 0a 20 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20  .    *pPageSize 
12a73 3d 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 70  = (u16)pPager->p
12a74 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 69 66 28  ageSize;.    if(
12a75 20 6e 52 65 73 65 72 76 65 3c 30 20 29 20 6e 52   nReserve<0 ) nR
12a76 65 73 65 72 76 65 20 3d 20 70 50 61 67 65 72 2d  eserve = pPager-
12a77 3e 6e 52 65 73 65 72 76 65 3b 0a 20 20 20 20 61  >nReserve;.    a
12a78 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e  ssert( nReserve>
12a79 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c 31  =0 && nReserve<1
12a7a 30 30 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65  000 );.    pPage
12a7b 72 2d 3e 6e 52 65 73 65 72 76 65 20 3d 20 28 69  r->nReserve = (i
12a7c 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 20  16)nReserve;.   
12a7d 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65   pagerReportSize
12a7e 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20  (pPager);.  }.  
12a7f 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
12a80 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
12a81 6e 74 65 72 20 74 6f 20 74 68 65 20 22 74 65 6d  nter to the "tem
12a82 70 6f 72 61 72 79 20 70 61 67 65 22 20 62 75 66  porary page" buf
12a83 66 65 72 20 68 65 6c 64 20 69 6e 74 65 72 6e 61  fer held interna
12a84 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 70 61  lly.** by the pa
12a85 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 61 20  ger.  This is a 
12a86 62 75 66 66 65 72 20 74 68 61 74 20 69 73 20 62  buffer that is b
12a87 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c  ig enough to hol
12a88 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20  d the.** entire 
12a89 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 64 61 74  content of a dat
12a8a 61 62 61 73 65 20 70 61 67 65 2e 20 20 54 68 69  abase page.  Thi
12a8b 73 20 62 75 66 66 65 72 20 69 73 20 75 73 65 64  s buffer is used
12a8c 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64   internally.** d
12a8d 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61  uring rollback a
12a8e 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77  nd will be overw
12a8f 72 69 74 74 65 6e 20 77 68 65 6e 65 76 65 72 20  ritten whenever 
12a90 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63  a rollback.** oc
12a91 63 75 72 73 2e 20 20 42 75 74 20 6f 74 68 65 72  curs.  But other
12a92 20 6d 6f 64 75 6c 65 73 20 61 72 65 20 66 72 65   modules are fre
12a93 65 20 74 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c  e to use it too,
12a94 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e   as long as.** n
12a95 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20  o rollbacks are 
12a96 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51  happening..*/.SQ
12a97 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
12a98 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54  d *sqlite3PagerT
12a99 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 20 2a  empSpace(Pager *
12a9a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
12a9b 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70  n pPager->pTmpSp
12a9c 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74  ace;.}../*.** At
12a9d 74 65 6d 70 74 20 74 6f 20 73 65 74 20 74 68 65  tempt to set the
12a9e 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73   maximum databas
12a9f 65 20 70 61 67 65 20 63 6f 75 6e 74 20 69 66 20  e page count if 
12aa0 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69  mxPage is positi
12aa1 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20  ve. .** Make no 
12aa2 63 68 61 6e 67 65 73 20 69 66 20 6d 78 50 61 67  changes if mxPag
12aa3 65 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67  e is zero or neg
12aa4 61 74 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 65  ative.  And neve
12aa5 72 20 72 65 64 75 63 65 20 74 68 65 0a 2a 2a 20  r reduce the.** 
12aa6 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75  maximum page cou
12aa7 6e 74 20 62 65 6c 6f 77 20 74 68 65 20 63 75 72  nt below the cur
12aa8 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65  rent size of the
12aa9 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
12aaa 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6d   Regardless of m
12aab 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68  xPage, return th
12aac 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75  e current maximu
12aad 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f  m page count..*/
12aae 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12aaf 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
12ab0 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67  MaxPageCount(Pag
12ab1 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
12ab2 6d 78 50 61 67 65 29 7b 0a 20 20 69 66 28 20 6d  mxPage){.  if( m
12ab3 78 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 70  xPage>0 ){.    p
12ab4 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20  Pager->mxPgno = 
12ab5 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71  mxPage;.  }.  sq
12ab6 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
12ab7 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  unt(pPager, 0);.
12ab8 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
12ab9 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a  >mxPgno;.}../*.*
12aba 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
12abb 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20  set of routines 
12abc 61 72 65 20 75 73 65 64 20 74 6f 20 64 69 73 61  are used to disa
12abd 62 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65  ble the simulate
12abe 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d  d.** I/O error m
12abf 65 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65  echanism.  These
12ac0 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73   routines are us
12ac1 65 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75  ed to avoid simu
12ac2 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20  lated.** errors 
12ac3 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20  in places where 
12ac4 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61  we do not care a
12ac5 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a  bout errors..**.
12ac6 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49  ** Unless -DSQLI
12ac7 54 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65  TE_TEST=1 is use
12ac8 64 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  d, these routine
12ac9 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73  s are all no-ops
12aca 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65  .** and generate
12acb 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66   no code..*/.#if
12acc 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
12acd 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72  SQLITE_API exter
12ace 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  n int sqlite3_io
12acf 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a  _error_pending;.
12ad0 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72  SQLITE_API exter
12ad1 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  n int sqlite3_io
12ad2 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74  _error_hit;.stat
12ad3 69 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e 74  ic int saved_cnt
12ad4 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73  ;.void disable_s
12ad5 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
12ad6 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 65  rs(void){.  save
12ad7 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f  d_cnt = sqlite3_
12ad8 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67  io_error_pending
12ad9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  ;.  sqlite3_io_e
12ada 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d  rror_pending = -
12adb 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65  1;.}.void enable
12adc 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
12add 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 71  rors(void){.  sq
12ade 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
12adf 65 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f 63  ending = saved_c
12ae0 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65  nt;.}.#else.# de
12ae1 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d  fine disable_sim
12ae2 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
12ae3 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62  ().# define enab
12ae4 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f  le_simulated_io_
12ae5 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a  errors().#endif.
12ae6 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20  ./*.** Read the 
12ae7 66 69 72 73 74 20 4e 20 62 79 74 65 73 20 66 72  first N bytes fr
12ae8 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  om the beginning
12ae9 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 74   of the file int
12aea 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74  o memory.** that
12aeb 20 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74 6f   pDest points to
12aec 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
12aed 70 61 67 65 72 20 77 61 73 20 6f 70 65 6e 65 64  pager was opened
12aee 20 6f 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20   on a transient 
12aef 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65 3d  file (zFilename=
12af0 3d 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f 70 65 6e  =""), or.** open
12af1 65 64 20 6f 6e 20 61 20 66 69 6c 65 20 6c 65 73  ed on a file les
12af2 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 69  s than N bytes i
12af3 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f 75 74 70  n size, the outp
12af4 75 74 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20  ut buffer is.** 
12af5 7a 65 72 6f 65 64 20 61 6e 64 20 53 51 4c 49 54  zeroed and SQLIT
12af6 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 54  E_OK returned. T
12af7 68 65 20 72 61 74 69 6f 6e 61 6c 65 20 66 6f 72  he rationale for
12af8 20 74 68 69 73 20 69 73 20 74 68 61 74 20 74 68   this is that th
12af9 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  is .** function 
12afa 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20  is used to read 
12afb 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 73  database headers
12afc 2c 20 61 6e 64 20 61 20 6e 65 77 20 74 72 61 6e  , and a new tran
12afd 73 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a 65 72 6f  sient or.** zero
12afe 20 73 69 7a 65 64 20 64 61 74 61 62 61 73 65 20   sized database 
12aff 68 61 73 20 61 20 68 65 61 64 65 72 20 74 68 61  has a header tha
12b00 6e 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72  n consists entir
12b01 65 6c 79 20 6f 66 20 7a 65 72 6f 65 73 2e 0a 2a  ely of zeroes..*
12b02 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 49 4f 20 65  *.** If any IO e
12b03 72 72 6f 72 20 61 70 61 72 74 20 66 72 6f 6d 20  rror apart from 
12b04 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
12b05 52 54 5f 52 45 41 44 20 69 73 20 65 6e 63 6f 75  RT_READ is encou
12b06 6e 74 65 72 65 64 2c 0a 2a 2a 20 74 68 65 20 65  ntered,.** the e
12b07 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
12b08 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
12b09 6c 65 72 20 61 6e 64 20 74 68 65 20 63 6f 6e 74  ler and the cont
12b0a 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 6f  ents of the.** o
12b0b 75 74 70 75 74 20 62 75 66 66 65 72 20 75 6e 64  utput buffer und
12b0c 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
12b0d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12b0e 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69  lite3PagerReadFi
12b0f 6c 65 68 65 61 64 65 72 28 50 61 67 65 72 20 2a  leheader(Pager *
12b10 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 75  pPager, int N, u
12b11 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44  nsigned char *pD
12b12 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  est){.  int rc =
12b13 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65   SQLITE_OK;.  me
12b14 6d 73 65 74 28 70 44 65 73 74 2c 20 30 2c 20 4e  mset(pDest, 0, N
12b15 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  );.  assert( isO
12b16 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
12b17 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  || pPager->tempF
12b18 69 6c 65 20 29 3b 0a 20 20 69 66 28 20 69 73 4f  ile );.  if( isO
12b19 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
12b1a 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28  ){.    IOTRACE((
12b1b 22 44 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e  "DBHDR %p 0 %d\n
12b1c 22 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20  ", pPager, N)). 
12b1d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
12b1e 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64  sRead(pPager->fd
12b1f 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a  , pDest, N, 0);.
12b20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12b21 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52  TE_IOERR_SHORT_R
12b22 45 41 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20  EAD ){.      rc 
12b23 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
12b24 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
12b25 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
12b26 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  urn the total nu
12b27 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
12b28 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
12b29 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a  le associated .*
12b2a 2a 20 77 69 74 68 20 70 50 61 67 65 72 2e 20 4e  * with pPager. N
12b2b 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 69 73  ormally, this is
12b2c 20 63 61 6c 63 75 6c 61 74 65 64 20 61 73 20 28   calculated as (
12b2d 3c 64 62 20 66 69 6c 65 20 73 69 7a 65 3e 2f 3c  <db file size>/<
12b2e 70 61 67 65 2d 73 69 7a 65 3e 29 2e 0a 2a 2a 20  page-size>)..** 
12b2f 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 20  However, if the 
12b30 66 69 6c 65 20 69 73 20 62 65 74 77 65 65 6e 20  file is between 
12b31 31 20 61 6e 64 20 3c 70 61 67 65 2d 73 69 7a 65  1 and <page-size
12b32 3e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c  > bytes in size,
12b33 20 74 68 65 6e 20 0a 2a 2a 20 74 68 69 73 20 69   then .** this i
12b34 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 31  s considered a 1
12b35 20 70 61 67 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a   page file..**.*
12b36 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
12b37 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65  s in error state
12b38 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
12b39 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  ion is called, t
12b3a 68 65 6e 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72  hen the.** error
12b3b 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64   state error cod
12b3c 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  e is returned an
12b3d 64 20 2a 70 6e 50 61 67 65 20 6c 65 66 74 20 75  d *pnPage left u
12b3e 6e 63 68 61 6e 67 65 64 2e 20 4f 72 2c 0a 2a 2a  nchanged. Or,.**
12b3f 20 69 66 20 74 68 65 20 66 69 6c 65 20 73 79 73   if the file sys
12b40 74 65 6d 20 68 61 73 20 74 6f 20 62 65 20 71 75  tem has to be qu
12b41 65 72 69 65 64 20 66 6f 72 20 74 68 65 20 73 69  eried for the si
12b42 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61  ze of the file a
12b43 6e 64 0a 2a 2a 20 74 68 65 20 71 75 65 72 79 20  nd.** the query 
12b44 61 74 74 65 6d 70 74 20 72 65 74 75 72 6e 73 20  attempt returns 
12b45 61 6e 20 49 4f 20 65 72 72 6f 72 2c 20 74 68 65  an IO error, the
12b46 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
12b47 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e  s returned.** an
12b48 64 20 2a 70 6e 50 61 67 65 20 69 73 20 6c 65 66  d *pnPage is lef
12b49 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a  t unchanged..**.
12b4a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  ** Otherwise, if
12b4b 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73   everything is s
12b4c 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20  uccessful, then 
12b4d 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
12b4e 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e  urned.** and *pn
12b4f 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 74  Page is set to t
12b50 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
12b51 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
12b52 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
12b53 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
12b54 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
12b55 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
12b56 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 50  nt *pnPage){.  P
12b57 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20  gno nPage;      
12b58 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
12b59 65 20 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20  e to return via 
12b5a 2a 70 6e 50 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a  *pnPage */..  /*
12b5b 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73   If the pager is
12b5c 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
12b5d 65 72 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74  error state, ret
12b5e 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f  urn the error co
12b5f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  de. */.  if( pPa
12b60 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a  ger->errCode ){.
12b61 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65      return pPage
12b62 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a  r->errCode;.  }.
12b63 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20  .  /* Determine 
12b64 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
12b65 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  ges in the file.
12b66 20 53 74 6f 72 65 20 74 68 69 73 20 69 6e 20 6e   Store this in n
12b67 50 61 67 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70  Page. */.  if( p
12b68 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c  Pager->dbSizeVal
12b69 69 64 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20  id ){.    nPage 
12b6a 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  = pPager->dbSize
12b6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
12b6c 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
12b6d 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20         /* Error 
12b6e 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 46 69  returned by OsFi
12b6f 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 20 20 20 20  leSize() */.    
12b70 69 36 34 20 6e 20 3d 20 30 3b 20 20 20 20 20 20  i64 n = 0;      
12b71 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
12b72 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 72 65  size in bytes re
12b73 74 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65  turned by OsFile
12b74 53 69 7a 65 28 29 20 2a 2f 0a 0a 20 20 20 20 61  Size() */..    a
12b75 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
12b76 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61  ager->fd) || pPa
12b77 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
12b78 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28  .    if( isOpen(
12b79 70 50 61 67 65 72 2d 3e 66 64 29 20 26 26 20 28  pPager->fd) && (
12b7a 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74  0 != (rc = sqlit
12b7b 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
12b7c 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29 29 20 29  ger->fd, &n))) )
12b7d 7b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 72  {.      pager_er
12b7e 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b  ror(pPager, rc);
12b7f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
12b80 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
12b81 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d  n>0 && n<pPager-
12b82 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20  >pageSize ){.   
12b83 20 20 20 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20     nPage = 1;.  
12b84 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
12b85 50 61 67 65 20 3d 20 28 50 67 6e 6f 29 28 6e 20  Page = (Pgno)(n 
12b86 2f 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  / pPager->pageSi
12b87 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ze);.    }.    i
12b88 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
12b89 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29  !=PAGER_UNLOCK )
12b8a 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
12b8b 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a  dbSize = nPage;.
12b8c 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62        pPager->db
12b8d 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65  FileSize = nPage
12b8e 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ;.      pPager->
12b8f 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 31 3b  dbSizeValid = 1;
12b90 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
12b91 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   If the current 
12b92 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
12b93 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 67  in the file is g
12b94 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
12b95 0a 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64  .  ** configured
12b96 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 72 20 6e   maximum pager n
12b97 75 6d 62 65 72 2c 20 69 6e 63 72 65 61 73 65 20  umber, increase 
12b98 74 68 65 20 61 6c 6c 6f 77 65 64 20 6c 69 6d 69  the allowed limi
12b99 74 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 74  t so.  ** that t
12b9a 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 72  he file can be r
12b9b 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ead..  */.  if( 
12b9c 6e 50 61 67 65 3e 70 50 61 67 65 72 2d 3e 6d 78  nPage>pPager->mx
12b9d 50 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67  Pgno ){.    pPag
12b9e 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 28 50 67  er->mxPgno = (Pg
12b9f 6e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d 0a 0a 20  no)nPage;.  }.. 
12ba0 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70   /* Set the outp
12ba1 75 74 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20  ut variable and 
12ba2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12ba3 20 2a 2f 0a 20 20 69 66 28 20 70 6e 50 61 67 65   */.  if( pnPage
12ba4 20 29 7b 0a 20 20 20 20 2a 70 6e 50 61 67 65 20   ){.    *pnPage 
12ba5 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 72  = nPage;.  }.  r
12ba6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
12ba7 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  .}.../*.** Try t
12ba8 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20  o obtain a lock 
12ba9 6f 66 20 74 79 70 65 20 6c 6f 63 6b 74 79 70 65  of type locktype
12baa 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
12bab 20 66 69 6c 65 2e 20 49 66 0a 2a 2a 20 61 20 73   file. If.** a s
12bac 69 6d 69 6c 61 72 20 6f 72 20 67 72 65 61 74 65  imilar or greate
12bad 72 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64  r lock is alread
12bae 79 20 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e  y held, this fun
12baf 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
12bb0 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 53  .** (returning S
12bb1 51 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65 64 69 61  QLITE_OK immedia
12bb2 74 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  tely)..**.** Oth
12bb3 65 72 77 69 73 65 2c 20 61 74 74 65 6d 70 74 20  erwise, attempt 
12bb4 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f  to obtain the lo
12bb5 63 6b 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ck using sqlite3
12bb6 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 76 6f 6b 65  OsLock(). Invoke
12bb7 20 0a 2a 2a 20 74 68 65 20 62 75 73 79 20 63 61   .** the busy ca
12bb8 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f  llback if the lo
12bb9 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ck is currently 
12bba 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 52  not available. R
12bbb 65 70 65 61 74 20 0a 2a 2a 20 75 6e 74 69 6c 20  epeat .** until 
12bbc 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
12bbd 6b 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20  k returns false 
12bbe 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 61 74 74  or until the att
12bbf 65 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f 62 74 61  empt to .** obta
12bc0 69 6e 20 74 68 65 20 6c 6f 63 6b 20 73 75 63 63  in the lock succ
12bc1 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  eeds..**.** Retu
12bc2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
12bc3 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65  success and an e
12bc4 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20  rror code if we 
12bc5 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a  cannot obtain.**
12bc6 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68   the lock. If th
12bc7 65 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e  e lock is obtain
12bc8 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ed successfully,
12bc9 20 73 65 74 20 74 68 65 20 50 61 67 65 72 2e 73   set the Pager.s
12bca 74 61 74 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c  tate .** variabl
12bcb 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 20 62 65  e to locktype be
12bcc 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
12bcd 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
12bce 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b  ger_wait_on_lock
12bcf 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
12bd0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20  int locktype){. 
12bd1 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
12bd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12bd3 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
12bd4 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  code */..  /* Th
12bd5 65 20 4f 53 20 6c 6f 63 6b 20 76 61 6c 75 65 73  e OS lock values
12bd6 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d   must be the sam
12bd7 65 20 61 73 20 74 68 65 20 50 61 67 65 72 20 6c  e as the Pager l
12bd8 6f 63 6b 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  ock values */.  
12bd9 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 53 48  assert( PAGER_SH
12bda 41 52 45 44 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  ARED==SHARED_LOC
12bdb 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50  K );.  assert( P
12bdc 41 47 45 52 5f 52 45 53 45 52 56 45 44 3d 3d 52  AGER_RESERVED==R
12bdd 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a  ESERVED_LOCK );.
12bde 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
12bdf 45 58 43 4c 55 53 49 56 45 3d 3d 45 58 43 4c 55  EXCLUSIVE==EXCLU
12be0 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20  SIVE_LOCK );..  
12be1 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 69  /* If the file i
12be2 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f  s currently unlo
12be3 63 6b 65 64 20 74 68 65 6e 20 74 68 65 20 73 69  cked then the si
12be4 7a 65 20 6d 75 73 74 20 62 65 20 75 6e 6b 6e 6f  ze must be unkno
12be5 77 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  wn */.  assert( 
12be6 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
12be7 41 47 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70  AGER_SHARED || p
12be8 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c  Pager->dbSizeVal
12be9 69 64 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43  id==0 );..  /* C
12bea 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69  heck that this i
12beb 73 20 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70  s either a no-op
12bec 20 28 62 65 63 61 75 73 65 20 74 68 65 20 72 65   (because the re
12bed 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20  quested lock is 
12bee 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65  .  ** already he
12bef 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68  ld, or one of th
12bf0 65 20 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74  e transistions t
12bf1 68 61 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e  hat the busy-han
12bf2 64 6c 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65  dler.  ** may be
12bf3 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c   invoked during,
12bf4 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
12bf5 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a  e comment above.
12bf6 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65    ** sqlite3Page
12bf7 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28  rSetBusyhandler(
12bf8 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  )..  */.  assert
12bf9 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  ( (pPager->state
12bfa 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20  >=locktype).    
12bfb 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73     || (pPager->s
12bfc 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
12bfd 43 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d  CK && locktype==
12bfe 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20  PAGER_SHARED).  
12bff 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d       || (pPager-
12c00 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45  >state==PAGER_RE
12c01 53 45 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79  SERVED && lockty
12c02 70 65 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  pe==PAGER_EXCLUS
12c03 49 56 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28  IVE).  );..  if(
12c04 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
12c05 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
12c06 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
12c07 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20    }else{.    do 
12c08 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
12c09 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65  ite3OsLock(pPage
12c0a 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29  r->fd, locktype)
12c0b 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63  ;.    }while( rc
12c0c 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26  ==SQLITE_BUSY &&
12c0d 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61   pPager->xBusyHa
12c0e 6e 64 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42  ndler(pPager->pB
12c0f 75 73 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29  usyHandlerArg) )
12c10 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
12c11 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12c12 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d   pPager->state =
12c13 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20   (u8)locktype;. 
12c14 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c       IOTRACE(("L
12c15 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50  OCK %p %d\n", pP
12c16 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29  ager, locktype))
12c17 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
12c18 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
12c19 20 46 75 6e 63 74 69 6f 6e 20 61 73 73 65 72 74   Function assert
12c1a 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69  TruncateConstrai
12c1b 6e 74 28 70 50 61 67 65 72 29 20 63 68 65 63 6b  nt(pPager) check
12c1c 73 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68  s that one of th
12c1d 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  e .** following 
12c1e 69 73 20 74 72 75 65 20 66 6f 72 20 61 6c 6c 20  is true for all 
12c1f 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72  dirty pages curr
12c20 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67  ently in the pag
12c21 65 2d 63 61 63 68 65 3a 0a 2a 2a 0a 2a 2a 20 20  e-cache:.**.**  
12c22 20 61 29 20 54 68 65 20 70 61 67 65 20 6e 75 6d   a) The page num
12c23 62 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e  ber is less than
12c24 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
12c25 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a   size of the .**
12c26 20 20 20 20 20 20 63 75 72 72 65 6e 74 20 64 61        current da
12c27 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69 6e  tabase image, in
12c28 20 70 61 67 65 73 2c 20 4f 52 0a 2a 2a 0a 2a 2a   pages, OR.**.**
12c29 20 20 20 62 29 20 69 66 20 74 68 65 20 70 61 67     b) if the pag
12c2a 65 20 63 6f 6e 74 65 6e 74 20 77 65 72 65 20 77  e content were w
12c2b 72 69 74 74 65 6e 20 61 74 20 74 68 69 73 20 74  ritten at this t
12c2c 69 6d 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f  ime, it would no
12c2d 74 0a 2a 2a 20 20 20 20 20 20 62 65 20 6e 65 63  t.**      be nec
12c2e 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20  essary to write 
12c2f 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74  the current cont
12c30 65 6e 74 20 6f 75 74 20 74 6f 20 74 68 65 20 73  ent out to the s
12c31 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20  ub-journal.**   
12c32 20 20 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65     (as determine
12c33 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 20 73 75  d by function su
12c34 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 29  bjRequiresPage()
12c35 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  )..**.** If the 
12c36 63 6f 6e 64 69 74 69 6f 6e 20 61 73 73 65 72 74  condition assert
12c37 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
12c38 69 6f 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75  ion were not tru
12c39 65 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 69  e, and the.** di
12c3a 72 74 79 20 70 61 67 65 20 77 65 72 65 20 74 6f  rty page were to
12c3b 20 62 65 20 64 69 73 63 61 72 64 65 64 20 66 72   be discarded fr
12c3c 6f 6d 20 74 68 65 20 63 61 63 68 65 20 76 69 61  om the cache via
12c3d 20 74 68 65 20 70 61 67 65 72 53 74 72 65 73 73   the pagerStress
12c3e 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 70  ().** routine, p
12c3f 61 67 65 72 53 74 72 65 73 73 28 29 20 77 6f 75  agerStress() wou
12c40 6c 64 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65  ld not write the
12c41 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f   current page co
12c42 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20  ntent to.** the 
12c43 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
12c44 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 74 72  f a savepoint tr
12c45 61 6e 73 61 63 74 69 6f 6e 20 77 65 72 65 20 72  ansaction were r
12c46 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72  olled back after
12c47 0a 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 65  .** this happene
12c48 64 2c 20 74 68 65 20 63 6f 72 72 65 63 74 20 62  d, the correct b
12c49 65 68 61 76 69 6f 75 72 20 77 6f 75 6c 64 20 62  ehaviour would b
12c4a 65 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65  e to restore the
12c4b 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 6f 6e 74   current.** cont
12c4c 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e  ent of the page.
12c4d 20 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20   However, since 
12c4e 74 68 69 73 20 63 6f 6e 74 65 6e 74 20 69 73 20  this content is 
12c4f 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 65  not present in e
12c50 69 74 68 65 72 0a 2a 2a 20 74 68 65 20 64 61 74  ither.** the dat
12c51 61 62 61 73 65 20 66 69 6c 65 20 6f 72 20 74 68  abase file or th
12c52 65 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65  e portion of the
12c53 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
12c54 6c 20 61 6e 64 20 0a 2a 2a 20 73 75 62 2d 6a 6f  l and .** sub-jo
12c55 75 72 6e 61 6c 20 72 6f 6c 6c 65 64 20 62 61 63  urnal rolled bac
12c56 6b 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 6f  k the content co
12c57 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 73 74 6f  uld not be resto
12c58 72 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64  red and the.** d
12c59 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 6f  atabase image wo
12c5a 75 6c 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75  uld become corru
12c5b 70 74 2e 20 49 74 20 69 73 20 74 68 65 72 65 66  pt. It is theref
12c5c 6f 72 65 20 66 6f 72 74 75 6e 61 74 65 20 74 68  ore fortunate th
12c5d 61 74 20 0a 2a 2a 20 74 68 69 73 20 63 69 72 63  at .** this circ
12c5e 75 6d 73 74 61 6e 63 65 20 63 61 6e 6e 6f 74 20  umstance cannot 
12c5f 61 72 69 73 65 2e 0a 2a 2f 0a 23 69 66 20 64 65  arise..*/.#if de
12c60 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
12c61 55 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20  UG).static void 
12c62 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f  assertTruncateCo
12c63 6e 73 74 72 61 69 6e 74 43 62 28 50 67 48 64 72  nstraintCb(PgHdr
12c64 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74   *pPg){.  assert
12c65 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48  ( pPg->flags&PGH
12c66 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 61 73  DR_DIRTY );.  as
12c67 73 65 72 74 28 20 21 73 75 62 6a 52 65 71 75 69  sert( !subjRequi
12c68 72 65 73 50 61 67 65 28 70 50 67 29 20 7c 7c 20  resPage(pPg) || 
12c69 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 67 2d 3e  pPg->pgno<=pPg->
12c6a 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29  pPager->dbSize )
12c6b 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
12c6c 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f  assertTruncateCo
12c6d 6e 73 74 72 61 69 6e 74 28 50 61 67 65 72 20 2a  nstraint(Pager *
12c6e 70 50 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74  pPager){.  sqlit
12c6f 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44  e3PcacheIterateD
12c70 69 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43  irty(pPager->pPC
12c71 61 63 68 65 2c 20 61 73 73 65 72 74 54 72 75 6e  ache, assertTrun
12c72 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62  cateConstraintCb
12c73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66  );.}.#else.# def
12c74 69 6e 65 20 61 73 73 65 72 74 54 72 75 6e 63 61  ine assertTrunca
12c75 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61  teConstraint(pPa
12c76 67 65 72 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ger).#endif../*.
12c77 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20  ** Truncate the 
12c78 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
12c79 73 65 20 66 69 6c 65 20 69 6d 61 67 65 20 74 6f  se file image to
12c7a 20 6e 50 61 67 65 20 70 61 67 65 73 2e 20 54 68   nPage pages. Th
12c7b 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  is .** function 
12c7c 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c  does not actuall
12c7d 79 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74  y modify the dat
12c7e 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 69  abase file on di
12c7f 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20  sk. It .** just 
12c80 73 65 74 73 20 74 68 65 20 69 6e 74 65 72 6e 61  sets the interna
12c81 6c 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 70  l state of the p
12c82 61 67 65 72 20 6f 62 6a 65 63 74 20 73 6f 20 74  ager object so t
12c83 68 61 74 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e  hat the .** trun
12c84 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64  cation will be d
12c85 6f 6e 65 20 77 68 65 6e 20 74 68 65 20 63 75 72  one when the cur
12c86 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
12c87 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a   is committed..*
12c88 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
12c89 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
12c8a 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28  erTruncateImage(
12c8b 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
12c8c 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61 73  gno nPage){.  as
12c8d 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62  sert( pPager->db
12c8e 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 61  SizeValid );.  a
12c8f 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
12c90 62 53 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a  bSize>=nPage );.
12c91 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12c92 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52  ->state>=PAGER_R
12c93 45 53 45 52 56 45 44 20 29 3b 0a 20 20 70 50 61  ESERVED );.  pPa
12c94 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50  ger->dbSize = nP
12c95 61 67 65 3b 0a 20 20 61 73 73 65 72 74 54 72 75  age;.  assertTru
12c96 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28  ncateConstraint(
12c97 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pPager);.}../*.*
12c98 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70  * Shutdown the p
12c99 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 65  age cache.  Free
12c9a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20   all memory and 
12c9b 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e  close all files.
12c9c 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e  .**.** If a tran
12c9d 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70  saction was in p
12c9e 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 69  rogress when thi
12c9f 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
12ca0 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61  led, that.** tra
12ca1 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
12ca2 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75  ed back.  All ou
12ca3 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20  tstanding pages 
12ca4 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a  are invalidated.
12ca5 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d  ** and their mem
12ca6 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 41  ory is freed.  A
12ca7 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ny attempt to us
12ca8 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 61  e a page associa
12ca9 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73  ted.** with this
12caa 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74 65   page cache afte
12cab 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  r this function 
12cac 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b  returns will lik
12cad 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e  ely.** result in
12cae 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a   a coredump..**.
12caf 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
12cb0 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73   always succeeds
12cb1 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  . If a transacti
12cb2 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e 20  on is active an 
12cb3 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61  attempt.** is ma
12cb4 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61  de to roll it ba
12cb5 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ck. If an error 
12cb6 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68  occurs during th
12cb7 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61  e rollback .** a
12cb8 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79   hot journal may
12cb9 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20   be left in the 
12cba 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e  filesystem but n
12cbb 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  o error is retur
12cbc 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61  ned.** to the ca
12cbd 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ller..*/.SQLITE_
12cbe 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
12cbf 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61  te3PagerClose(Pa
12cc0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
12cc1 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65  disable_simulate
12cc2 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20  d_io_errors();. 
12cc3 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
12cc4 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 70  ignMalloc();.  p
12cc5 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d  Pager->errCode =
12cc6 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78   0;.  pPager->ex
12cc7 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 30 3b  clusiveMode = 0;
12cc8 0a 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70  .  pager_reset(p
12cc9 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 4d 45  Pager);.  if( ME
12cca 4d 44 42 20 29 7b 0a 20 20 20 20 70 61 67 65 72  MDB ){.    pager
12ccb 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b  _unlock(pPager);
12ccc 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
12ccd 20 53 65 74 20 50 61 67 65 72 2e 6a 6f 75 72 6e   Set Pager.journ
12cce 61 6c 48 64 72 20 74 6f 20 2d 31 20 66 6f 72 20  alHdr to -1 for 
12ccf 74 68 65 20 62 65 6e 65 66 69 74 20 6f 66 20 74  the benefit of t
12cd0 68 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63  he pager_playbac
12cd1 6b 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c  k() .    ** call
12cd2 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 6d 61   which may be ma
12cd3 64 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70  de from within p
12cd4 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c  agerUnlockAndRol
12cd5 6c 62 61 63 6b 28 29 2e 20 49 66 20 69 74 0a 20  lback(). If it. 
12cd6 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 2d 31 2c     ** is not -1,
12cd7 20 74 68 65 6e 20 74 68 65 20 75 6e 73 79 6e 63   then the unsync
12cd8 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 61 6e  ed portion of an
12cd9 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   open journal fi
12cda 6c 65 20 6d 61 79 0a 20 20 20 20 2a 2a 20 62 65  le may.    ** be
12cdb 20 70 6c 61 79 65 64 20 62 61 63 6b 20 69 6e 74   played back int
12cdc 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  o the database. 
12cdd 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75  If a power failu
12cde 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 0a  re occurs while.
12cdf 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68      ** this is h
12ce0 61 70 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61  appening, the da
12ce1 74 61 62 61 73 65 20 6d 61 79 20 62 65 63 6f 6d  tabase may becom
12ce2 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a  e corrupt..    *
12ce3 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  /.    pPager->jo
12ce4 75 72 6e 61 6c 48 64 72 20 3d 20 2d 31 3b 0a 20  urnalHdr = -1;. 
12ce5 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e     pagerUnlockAn
12ce6 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72  dRollback(pPager
12ce7 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
12ce8 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
12ce9 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75  );.  enable_simu
12cea 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
12ceb 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28  );.  PAGERTRACE(
12cec 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50  ("CLOSE %d\n", P
12ced 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29  AGERID(pPager)))
12cee 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c  ;.  IOTRACE(("CL
12cef 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65  OSE %p\n", pPage
12cf0 72 29 29 0a 20 20 73 71 6c 69 74 65 33 4f 73 43  r)).  sqlite3OsC
12cf1 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29  lose(pPager->fd)
12cf2 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46  ;.  sqlite3PageF
12cf3 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70  ree(pPager->pTmp
12cf4 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65  Space);.  sqlite
12cf5 33 50 63 61 63 68 65 43 6c 6f 73 65 28 70 50 61  3PcacheClose(pPa
12cf6 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a  ger->pPCache);..
12cf7 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41  #ifdef SQLITE_HA
12cf8 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 20 70 50  S_CODEC.  if( pP
12cf9 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65  ager->xCodecFree
12cfa 20 29 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65   ) pPager->xCode
12cfb 63 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 43  cFree(pPager->pC
12cfc 6f 64 65 63 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  odec);.#endif.. 
12cfd 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
12cfe 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 26 26 20  ->aSavepoint && 
12cff 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  !pPager->pInJour
12d00 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nal );.  assert(
12d01 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d   !isOpen(pPager-
12d02 3e 6a 66 64 29 20 26 26 20 21 69 73 4f 70 65 6e  >jfd) && !isOpen
12d03 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29  (pPager->sjfd) )
12d04 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
12d05 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74  e(pPager);.  ret
12d06 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
12d07 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  ..#if !defined(N
12d08 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65  DEBUG) || define
12d09 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f  d(SQLITE_TEST)./
12d0a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
12d0b 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  page number for 
12d0c 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c  page pPg..*/.SQL
12d0d 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 6e 6f  ITE_PRIVATE Pgno
12d0e 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12d0f 65 6e 75 6d 62 65 72 28 44 62 50 61 67 65 20 2a  enumber(DbPage *
12d10 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  pPg){.  return p
12d11 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64  Pg->pgno;.}.#end
12d12 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d  if../*.** Increm
12d13 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63  ent the referenc
12d14 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 61 67 65  e count for page
12d15 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   pPg..*/.SQLITE_
12d16 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
12d17 69 74 65 33 50 61 67 65 72 52 65 66 28 44 62 50  ite3PagerRef(DbP
12d18 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c  age *pPg){.  sql
12d19 69 74 65 33 50 63 61 63 68 65 52 65 66 28 70 50  ite3PcacheRef(pP
12d1a 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e  g);.}../*.** Syn
12d1b 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 49  c the journal. I
12d1c 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d  n other words, m
12d1d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65  ake sure all the
12d1e 20 70 61 67 65 73 20 74 68 61 74 20 68 61 76 65   pages that have
12d1f 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e  .** been written
12d20 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
12d21 68 61 76 65 20 61 63 74 75 61 6c 6c 79 20 72 65  have actually re
12d22 61 63 68 65 64 20 74 68 65 20 73 75 72 66 61 63  ached the surfac
12d23 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b  e of the.** disk
12d24 20 61 6e 64 20 63 61 6e 20 62 65 20 72 65 73 74   and can be rest
12d25 6f 72 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e  ored in the even
12d26 74 20 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  t of a hot-journ
12d27 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  al rollback..**.
12d28 2a 2a 20 49 66 20 74 68 65 20 50 61 67 65 72 2e  ** If the Pager.
12d29 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73  needSync flag is
12d2a 20 6e 6f 74 20 73 65 74 2c 20 74 68 65 6e 20 74   not set, then t
12d2b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
12d2c 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65  a.** no-op. Othe
12d2d 72 77 69 73 65 2c 20 74 68 65 20 61 63 74 69 6f  rwise, the actio
12d2e 6e 73 20 72 65 71 75 69 72 65 64 20 64 65 70 65  ns required depe
12d2f 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61  nd on the journa
12d30 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64 20 74 68  l-mode.** and th
12d31 65 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74  e device charact
12d32 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20  eristics of the 
12d33 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c  the file-system,
12d34 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
12d35 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 6a 6f  **   * If the jo
12d36 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6e  urnal file is an
12d37 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
12d38 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61 63 74 69  al file, no acti
12d39 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 62  on need.**     b
12d3a 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20  e taken..**.**  
12d3b 20 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66   * Otherwise, if
12d3c 20 74 68 65 20 64 65 76 69 63 65 20 64 6f 65 73   the device does
12d3d 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68 65   not support the
12d3e 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f   SAFE_APPEND pro
12d3f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20 20 74 68  perty,.**     th
12d40 65 6e 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  en the nRec fiel
12d41 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  d of the most re
12d42 63 65 6e 74 6c 79 20 77 72 69 74 74 65 6e 20 6a  cently written j
12d43 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a  ournal header.**
12d44 20 20 20 20 20 69 73 20 75 70 64 61 74 65 64 20       is updated 
12d45 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e  to contain the n
12d46 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c  umber of journal
12d47 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 68 61   records that ha
12d48 76 65 0a 2a 2a 20 20 20 20 20 62 65 65 6e 20 77  ve.**     been w
12d49 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77 69 6e 67  ritten following
12d4a 20 69 74 2e 20 49 66 20 74 68 65 20 70 61 67 65   it. If the page
12d4b 72 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 69  r is operating i
12d4c 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a 2a 20 20  n full-sync.**  
12d4d 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68     mode, then th
12d4e 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
12d4f 73 20 73 79 6e 63 65 64 20 62 65 66 6f 72 65 20  s synced before 
12d50 74 68 69 73 20 66 69 65 6c 64 20 69 73 20 75 70  this field is up
12d51 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a  dated..**.**   *
12d52 20 49 66 20 74 68 65 20 64 65 76 69 63 65 20 64   If the device d
12d53 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
12d54 74 68 65 20 53 45 51 55 45 4e 54 49 41 4c 20 70  the SEQUENTIAL p
12d55 72 6f 70 65 72 74 79 2c 20 74 68 65 6e 20 0a 2a  roperty, then .*
12d56 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66 69  *     journal fi
12d57 6c 65 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a  le is synced..**
12d58 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73 65 75 64  .** Or, in pseud
12d59 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  o-code:.**.**   
12d5a 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d 65 6d 6f  if( NOT <in-memo
12d5b 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29 7b 0a 2a  ry journal> ){.*
12d5c 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 41  *     if( NOT SA
12d5d 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a 2a 2a 20  FE_APPEND ){.** 
12d5e 20 20 20 20 20 20 69 66 28 20 3c 66 75 6c 6c 2d        if( <full-
12d5f 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20 78 53 79  sync mode> ) xSy
12d60 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  nc(<journal file
12d61 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20 3c 75 70  >);.**       <up
12d62 64 61 74 65 20 6e 52 65 63 20 66 69 65 6c 64 3e  date nRec field>
12d63 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a 20 20 20  .**     } .**   
12d64 20 20 69 66 28 20 4e 4f 54 20 53 45 51 55 45 4e    if( NOT SEQUEN
12d65 54 49 41 4c 20 29 20 78 53 79 6e 63 28 3c 6a 6f  TIAL ) xSync(<jo
12d66 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a  urnal file>);.**
12d67 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50     }.**.** The P
12d68 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c  ager.needSync fl
12d69 61 67 20 69 73 20 6e 65 76 65 72 20 62 65 20 73  ag is never be s
12d6a 65 74 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  et for temporary
12d6b 20 66 69 6c 65 73 2c 20 6f 72 20 61 6e 79 0a 2a   files, or any.*
12d6c 2a 20 66 69 6c 65 20 6f 70 65 72 61 74 69 6e 67  * file operating
12d6d 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65   in no-sync mode
12d6e 20 28 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 73   (Pager.noSync s
12d6f 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 29 2e  et to non-zero).
12d70 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
12d71 73 66 75 6c 2c 20 74 68 69 73 20 72 6f 75 74 69  sful, this routi
12d72 6e 65 20 63 6c 65 61 72 73 20 74 68 65 20 50 47  ne clears the PG
12d73 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c  HDR_NEED_SYNC fl
12d74 61 67 20 6f 66 20 65 76 65 72 79 20 0a 2a 2a 20  ag of every .** 
12d75 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 68  page currently h
12d76 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 65  eld in memory be
12d77 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 53  fore returning S
12d78 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20  QLITE_OK. If an 
12d79 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 65  IO.** error is e
12d7a 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68 65 6e  ncountered, then
12d7b 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f   the IO error co
12d7c 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  de is returned t
12d7d 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f  o the caller..*/
12d7e 0a 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63  .static int sync
12d7f 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70  Journal(Pager *p
12d80 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50  Pager){.  if( pP
12d81 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29  ager->needSync )
12d82 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  {.    assert( !p
12d83 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
12d84 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
12d85 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d  r->journalMode!=
12d86 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
12d87 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20  E_MEMORY ){.    
12d88 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
12d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12d8a 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
12d8b 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 63   code */.      c
12d8c 6f 6e 73 74 20 69 6e 74 20 69 44 63 20 3d 20 73  onst int iDc = s
12d8d 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68  qlite3OsDeviceCh
12d8e 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50  aracteristics(pP
12d8f 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20  ager->fd);.     
12d90 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
12d91 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a  pPager->jfd) );.
12d92 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69  .      if( 0==(i
12d93 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  Dc&SQLITE_IOCAP_
12d94 53 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a  SAFE_APPEND) ){.
12d95 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
12d96 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77 69 74 68  block deals with
12d97 20 61 6e 20 6f 62 73 63 75 72 65 20 70 72 6f 62   an obscure prob
12d98 6c 65 6d 2e 20 49 66 20 74 68 65 20 6c 61 73 74  lem. If the last
12d99 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20   connection.    
12d9a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72 6f 74      ** that wrot
12d9b 65 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  e to this databa
12d9c 73 65 20 77 61 73 20 6f 70 65 72 61 74 69 6e 67  se was operating
12d9d 20 69 6e 20 70 65 72 73 69 73 74 65 6e 74 2d 6a   in persistent-j
12d9e 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a  ournal.        *
12d9f 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65  * mode, then the
12da0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61   journal file ma
12da1 79 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20  y at this point 
12da2 61 63 74 75 61 6c 6c 79 20 62 65 20 6c 61 72 67  actually be larg
12da3 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  er.        ** th
12da4 61 6e 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  an Pager.journal
12da5 4f 66 66 20 62 79 74 65 73 2e 20 49 66 20 74 68  Off bytes. If th
12da6 65 20 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 20  e next thing in 
12da7 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  the journal.    
12da8 20 20 20 20 2a 2a 20 66 69 6c 65 20 68 61 70 70      ** file happ
12da9 65 6e 73 20 74 6f 20 62 65 20 61 20 6a 6f 75 72  ens to be a jour
12daa 6e 61 6c 2d 68 65 61 64 65 72 20 28 77 72 69 74  nal-header (writ
12dab 74 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 74  ten as part of t
12dac 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72  he.        ** pr
12dad 65 76 69 6f 75 73 20 63 6f 6e 6e 65 63 74 69 6f  evious connectio
12dae 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c  ns transaction),
12daf 20 61 6e 64 20 61 20 63 72 61 73 68 20 6f 72 20   and a crash or 
12db0 70 6f 77 65 72 2d 66 61 69 6c 75 72 65 20 0a 20  power-failure . 
12db1 20 20 20 20 20 20 20 2a 2a 20 6f 63 63 75 72 73         ** occurs
12db2 20 61 66 74 65 72 20 6e 52 65 63 20 69 73 20 75   after nRec is u
12db3 70 64 61 74 65 64 20 62 75 74 20 62 65 66 6f 72  pdated but befor
12db4 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f  e this connectio
12db5 6e 20 77 72 69 74 65 73 20 0a 20 20 20 20 20 20  n writes .      
12db6 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c    ** anything el
12db7 73 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  se to the journa
12db8 6c 20 66 69 6c 65 20 28 6f 72 20 63 6f 6d 6d 69  l file (or commi
12db9 74 73 2f 72 6f 6c 6c 73 20 62 61 63 6b 20 69 74  ts/rolls back it
12dba 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72  s .        ** tr
12dbb 61 6e 73 61 63 74 69 6f 6e 29 2c 20 74 68 65 6e  ansaction), then
12dbc 20 53 51 4c 69 74 65 20 6d 61 79 20 62 65 63 6f   SQLite may beco
12dbd 6d 65 20 63 6f 6e 66 75 73 65 64 20 77 68 65 6e  me confused when
12dbe 20 64 6f 69 6e 67 20 74 68 65 20 0a 20 20 20 20   doing the .    
12dbf 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e      ** hot-journ
12dc0 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c  al rollback foll
12dc1 6f 77 69 6e 67 20 72 65 63 6f 76 65 72 79 2e 20  owing recovery. 
12dc2 49 74 20 6d 61 79 20 72 6f 6c 6c 20 62 61 63 6b  It may roll back
12dc3 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20   all.        ** 
12dc4 6f 66 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  of this connecti
12dc5 6f 6e 73 20 64 61 74 61 2c 20 74 68 65 6e 20 70  ons data, then p
12dc6 72 6f 63 65 65 64 20 74 6f 20 72 6f 6c 6c 69 6e  roceed to rollin
12dc7 67 20 62 61 63 6b 20 74 68 65 20 6f 6c 64 2c 0a  g back the old,.
12dc8 20 20 20 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f          ** out-o
12dc9 66 2d 64 61 74 65 20 64 61 74 61 20 74 68 61 74  f-date data that
12dca 20 66 6f 6c 6c 6f 77 73 20 69 74 2e 20 44 61 74   follows it. Dat
12dcb 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
12dcc 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
12dcd 20 20 20 20 20 2a 2a 20 54 6f 20 77 6f 72 6b 20       ** To work 
12dce 61 72 6f 75 6e 64 20 74 68 69 73 2c 20 69 66 20  around this, if 
12dcf 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12dd0 20 64 6f 65 73 20 61 70 70 65 61 72 20 74 6f 20   does appear to 
12dd1 63 6f 6e 74 61 69 6e 0a 20 20 20 20 20 20 20 20  contain.        
12dd2 2a 2a 20 61 20 76 61 6c 69 64 20 68 65 61 64 65  ** a valid heade
12dd3 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 50 61 67 65  r following Page
12dd4 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 74 68  r.journalOff, th
12dd5 65 6e 20 77 72 69 74 65 20 61 20 30 78 30 30 0a  en write a 0x00.
12dd6 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20          ** byte 
12dd7 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
12dd8 69 74 20 74 6f 20 70 72 65 76 65 6e 74 20 69 74  it to prevent it
12dd9 20 66 72 6f 6d 20 62 65 69 6e 67 20 72 65 63 6f   from being reco
12dda 67 6e 69 7a 65 64 2e 0a 20 20 20 20 20 20 20 20  gnized..        
12ddb 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 56 61  **.        ** Va
12ddc 72 69 61 62 6c 65 20 69 4e 65 78 74 48 64 72 4f  riable iNextHdrO
12ddd 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f 20  ffset is set to 
12dde 74 68 65 20 6f 66 66 73 65 74 20 61 74 20 77 68  the offset at wh
12ddf 69 63 68 20 74 68 69 73 0a 20 20 20 20 20 20 20  ich this.       
12de0 20 2a 2a 20 70 72 6f 62 6c 65 6d 61 74 69 63 20   ** problematic 
12de1 68 65 61 64 65 72 20 77 69 6c 6c 20 6f 63 63 75  header will occu
12de2 72 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2e  r, if it exists.
12de3 20 61 4d 61 67 69 63 20 69 73 20 75 73 65 64 20   aMagic is used 
12de4 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73 20 61  .        ** as a
12de5 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65   temporary buffe
12de6 72 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65  r to inspect the
12de7 20 66 69 72 73 74 20 63 6f 75 70 6c 65 20 6f 66   first couple of
12de8 20 62 79 74 65 73 20 6f 66 0a 20 20 20 20 20 20   bytes of.      
12de9 20 20 2a 2a 20 74 68 65 20 70 6f 74 65 6e 74 69    ** the potenti
12dea 61 6c 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  al journal heade
12deb 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  r..        */.  
12dec 20 20 20 20 20 20 69 36 34 20 69 4e 65 78 74 48        i64 iNextH
12ded 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  drOffset;.      
12dee 20 20 75 38 20 61 4d 61 67 69 63 5b 38 5d 3b 0a    u8 aMagic[8];.
12def 09 75 38 20 7a 48 65 61 64 65 72 5b 73 69 7a 65  .u8 zHeader[size
12df0 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
12df1 29 2b 34 5d 3b 0a 0a 09 6d 65 6d 63 70 79 28 7a  )+4];...memcpy(z
12df2 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c  Header, aJournal
12df3 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a  Magic, sizeof(aJ
12df4 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 09  ournalMagic));..
12df5 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
12df6 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
12df7 61 6c 4d 61 67 69 63 29 5d 2c 20 70 50 61 67 65  alMagic)], pPage
12df8 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20  r->nRec);..     
12df9 20 20 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65     iNextHdrOffse
12dfa 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66  t = journalHdrOf
12dfb 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20  fset(pPager);.  
12dfc 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12dfd 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d  e3OsRead(pPager-
12dfe 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 2c  >jfd, aMagic, 8,
12dff 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29   iNextHdrOffset)
12e00 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
12e01 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30  ==SQLITE_OK && 0
12e02 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c  ==memcmp(aMagic,
12e03 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
12e04 38 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  8) ){.          
12e05 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
12e06 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b 0a 20 20  zerobyte = 0;.  
12e07 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
12e08 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
12e09 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f 62 79  er->jfd, &zeroby
12e0a 74 65 2c 20 31 2c 20 69 4e 65 78 74 48 64 72 4f  te, 1, iNextHdrO
12e0b 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20  ffset);.        
12e0c 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  }.        if( rc
12e0d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  !=SQLITE_OK && r
12e0e 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c!=SQLITE_IOERR_
12e0f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20  SHORT_READ ){.  
12e10 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
12e11 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  c;.        }..  
12e12 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
12e13 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e  he nRec value in
12e14 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
12e15 69 6c 65 20 68 65 61 64 65 72 2e 20 49 66 20 69  ile header. If i
12e16 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6c  n.        ** ful
12e17 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f  l-synchronous mo
12e18 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75  de, sync the jou
12e19 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 68 69 73  rnal first. This
12e1a 20 65 6e 73 75 72 65 73 20 74 68 61 74 0a 20 20   ensures that.  
12e1b 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74        ** all dat
12e1c 61 20 68 61 73 20 72 65 61 6c 6c 79 20 68 69 74  a has really hit
12e1d 20 74 68 65 20 64 69 73 6b 20 62 65 66 6f 72 65   the disk before
12e1e 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64   nRec is updated
12e1f 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 20   to mark.       
12e20 20 2a 2a 20 69 74 20 61 73 20 61 20 63 61 6e 64   ** it as a cand
12e21 69 64 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61  idate for rollba
12e22 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ck..        **. 
12e23 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69         ** This i
12e24 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 69  s not required i
12e25 66 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  f the persistent
12e26 20 6d 65 64 69 61 20 73 75 70 70 6f 72 74 73 20   media supports 
12e27 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53  the.        ** S
12e28 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65  AFE_APPEND prope
12e29 72 74 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20  rty. Because in 
12e2a 74 68 69 73 20 63 61 73 65 20 69 74 20 69 73 20  this case it is 
12e2b 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 20  not possible .  
12e2c 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72        ** for gar
12e2d 62 61 67 65 20 64 61 74 61 20 74 6f 20 62 65 20  bage data to be 
12e2e 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20  appended to the 
12e2f 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66  file, the nRec f
12e30 69 65 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  ield.        ** 
12e31 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  is populated wit
12e32 68 20 30 78 46 46 46 46 46 46 46 46 20 77 68 65  h 0xFFFFFFFF whe
12e33 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  n the journal he
12e34 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a  ader is written.
12e35 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e          ** and n
12e36 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65  ever needs to be
12e37 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20   updated..      
12e38 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28    */.        if(
12e39 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
12e3a 63 20 26 26 20 30 3d 3d 28 69 44 63 26 53 51 4c  c && 0==(iDc&SQL
12e3b 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e  ITE_IOCAP_SEQUEN
12e3c 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20  TIAL) ){.       
12e3d 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22     PAGERTRACE(("
12e3e 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20  SYNC journal of 
12e3f 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
12e40 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20  Pager)));.      
12e41 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53      IOTRACE(("JS
12e42 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65  YNC %p\n", pPage
12e43 72 29 29 0a 20 20 20 20 20 20 20 20 20 20 72 63  r)).          rc
12e44 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
12e45 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
12e46 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73  ager->sync_flags
12e47 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
12e48 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12e49 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
12e4a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 49 4f      }.        IO
12e4b 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20  TRACE(("JHDR %p 
12e4c 25 6c 6c 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  %lld\n", pPager,
12e4d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12e4e 48 64 72 29 29 3b 0a 20 20 20 20 20 20 20 20 72  Hdr));.        r
12e4f 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
12e50 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  te(.            
12e51 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65  pPager->jfd, zHe
12e52 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 48 65  ader, sizeof(zHe
12e53 61 64 65 72 29 2c 20 70 50 61 67 65 72 2d 3e 6a  ader), pPager->j
12e54 6f 75 72 6e 61 6c 48 64 72 0a 09 29 3b 0a 20 20  ournalHdr..);.  
12e55 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
12e56 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
12e57 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
12e58 20 20 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53     if( 0==(iDc&S
12e59 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55  QLITE_IOCAP_SEQU
12e5a 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20  ENTIAL) ){.     
12e5b 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22     PAGERTRACE(("
12e5c 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20  SYNC journal of 
12e5d 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
12e5e 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20  Pager)));.      
12e5f 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e    IOTRACE(("JSYN
12e60 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29  C %p\n", pPager)
12e61 29 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ).        rc = s
12e62 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61  qlite3OsSync(pPa
12e63 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72  ger->jfd, pPager
12e64 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20  ->sync_flags| . 
12e65 20 20 20 20 20 20 20 20 20 28 70 50 61 67 65 72           (pPager
12e66 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51  ->sync_flags==SQ
12e67 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53  LITE_SYNC_FULL?S
12e68 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f  QLITE_SYNC_DATAO
12e69 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 20 20 29  NLY:0).        )
12e6a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
12e6b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
12e6c 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
12e6d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
12e6e 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
12e6f 77 61 73 20 6a 75 73 74 20 73 75 63 63 65 73 73  was just success
12e70 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e 20 53 65  fully synced. Se
12e71 74 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63  t Pager.needSync
12e72 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f   .    ** to zero
12e73 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65 20 50   and clear the P
12e74 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66  GHDR_NEED_SYNC f
12e75 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73  lag on all pages
12e76 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50  s..    */.    pP
12e77 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d  ager->needSync =
12e78 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
12e79 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
12e7a 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   1;.    sqlite3P
12e7b 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c  cacheClearSyncFl
12e7c 61 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ags(pPager->pPCa
12e7d 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  che);.  }..  ret
12e7e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
12e7f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75  ../*.** The argu
12e80 6d 65 6e 74 20 69 73 20 74 68 65 20 66 69 72 73  ment is the firs
12e81 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  t in a linked li
12e82 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65  st of dirty page
12e83 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 62  s connected.** b
12e84 79 20 74 68 65 20 50 67 48 64 72 2e 70 44 69 72  y the PgHdr.pDir
12e85 74 79 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73  ty pointer. This
12e86 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 73   function writes
12e87 20 65 61 63 68 20 6f 6e 65 20 6f 66 20 74 68 65   each one of the
12e88 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61  .** in-memory pa
12e89 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ges in the list 
12e8a 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
12e8b 66 69 6c 65 2e 20 54 68 65 20 61 72 67 75 6d 65  file. The argume
12e8c 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c  nt may.** be NUL
12e8d 4c 2c 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  L, representing 
12e8e 61 6e 20 65 6d 70 74 79 20 6c 69 73 74 2e 20 49  an empty list. I
12e8f 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73  n this case this
12e90 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20   function is.** 
12e91 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54  a no-op..**.** T
12e92 68 65 20 70 61 67 65 72 20 6d 75 73 74 20 68 6f  he pager must ho
12e93 6c 64 20 61 74 20 6c 65 61 73 74 20 61 20 52 45  ld at least a RE
12e94 53 45 52 56 45 44 20 6c 6f 63 6b 20 77 68 65 6e  SERVED lock when
12e95 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a   this function.*
12e96 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 42 65 66  * is called. Bef
12e97 6f 72 65 20 77 72 69 74 69 6e 67 20 61 6e 79 74  ore writing anyt
12e98 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61  hing to the data
12e99 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20  base file, this 
12e9a 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70 67 72 61  lock.** is upgra
12e9b 64 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53  ded to an EXCLUS
12e9c 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65  IVE lock. If the
12e9d 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20   lock cannot be 
12e9e 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20 53 51 4c  obtained,.** SQL
12e9f 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75  ITE_BUSY is retu
12ea0 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61  rned and no data
12ea1 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74   is written to t
12ea2 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12ea3 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20  ..** .** If the 
12ea4 70 61 67 65 72 20 69 73 20 61 20 74 65 6d 70 2d  pager is a temp-
12ea5 66 69 6c 65 20 70 61 67 65 72 20 61 6e 64 20 74  file pager and t
12ea6 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 2d 73  he actual file-s
12ea7 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a 20 69 73  ystem file.** is
12ea8 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 2c 20 69   not yet open, i
12ea9 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64  t is created and
12eaa 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 61   opened before a
12eab 6e 79 20 64 61 74 61 20 69 73 20 0a 2a 2a 20 77  ny data is .** w
12eac 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a 2a 0a 2a  ritten out..**.*
12ead 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f 63 6b 20  * Once the lock 
12eae 68 61 73 20 62 65 65 6e 20 75 70 67 72 61 64 65  has been upgrade
12eaf 64 20 61 6e 64 2c 20 69 66 20 6e 65 63 65 73 73  d and, if necess
12eb0 61 72 79 2c 20 74 68 65 20 66 69 6c 65 20 6f 70  ary, the file op
12eb1 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20 70 61 67  ened,.** the pag
12eb2 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f  es are written o
12eb3 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
12eb4 73 65 20 66 69 6c 65 20 69 6e 20 6c 69 73 74 20  se file in list 
12eb5 6f 72 64 65 72 2e 20 57 72 69 74 69 6e 67 0a 2a  order. Writing.*
12eb6 2a 20 61 20 70 61 67 65 20 69 73 20 73 6b 69 70  * a page is skip
12eb7 70 65 64 20 69 66 20 69 74 20 6d 65 65 74 73 20  ped if it meets 
12eb8 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f  either of the fo
12eb9 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61  llowing criteria
12eba 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20  :.**.**   * The 
12ebb 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67  page number is g
12ebc 72 65 61 74 65 72 20 74 68 61 6e 20 50 61 67 65  reater than Page
12ebd 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a 2a 2a 20  r.dbSize, or.** 
12ebe 20 20 2a 20 54 68 65 20 50 47 48 44 52 5f 44 4f    * The PGHDR_DO
12ebf 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 20 69 73  NT_WRITE flag is
12ec0 20 73 65 74 20 6f 6e 20 74 68 65 20 70 61 67 65   set on the page
12ec1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 69 74 69  ..**.** If writi
12ec2 6e 67 20 6f 75 74 20 61 20 70 61 67 65 20 63 61  ng out a page ca
12ec3 75 73 65 73 20 74 68 65 20 64 61 74 61 62 61 73  uses the databas
12ec4 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20  e file to grow, 
12ec5 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65  Pager.dbFileSize
12ec6 0a 2a 2a 20 69 73 20 75 70 64 61 74 65 64 20 61  .** is updated a
12ec7 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49 66 20 70  ccordingly. If p
12ec8 61 67 65 20 31 20 69 73 20 77 72 69 74 74 65 6e  age 1 is written
12ec9 20 6f 75 74 2c 20 74 68 65 6e 20 74 68 65 20 76   out, then the v
12eca 61 6c 75 65 20 63 61 63 68 65 64 0a 2a 2a 20 69  alue cached.** i
12ecb 6e 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65  n Pager.dbFileVe
12ecc 72 73 5b 5d 20 69 73 20 75 70 64 61 74 65 64 20  rs[] is updated 
12ecd 74 6f 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77  to match the new
12ece 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
12ecf 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
12ed0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
12ed1 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75  everything is su
12ed2 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45  ccessful, SQLITE
12ed3 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
12ed4 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
12ed5 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61 6e 20 49  .** occurs, an I
12ed6 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
12ed7 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c 20 69 66  returned. Or, if
12ed8 20 74 68 65 20 45 58 43 4c 55 53 49 56 45 20 6c   the EXCLUSIVE l
12ed9 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65  ock cannot.** be
12eda 20 6f 62 74 61 69 6e 65 64 2c 20 53 51 4c 49 54   obtained, SQLIT
12edb 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e  E_BUSY is return
12edc 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
12edd 74 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61  t pager_write_pa
12ede 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70 4c  gelist(PgHdr *pL
12edf 69 73 74 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  ist){.  Pager *p
12ee0 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20  Pager;          
12ee1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12ee2 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  Pager object */.
12ee3 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
12ee4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ee5 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
12ee6 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20   code */..  if( 
12ee7 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 20  NEVER(pList==0) 
12ee8 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
12ee9 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20 3d 20 70  OK;.  pPager = p
12eea 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20  List->pPager;.. 
12eeb 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
12eec 74 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 65  t there may be e
12eed 69 74 68 65 72 20 61 20 52 45 53 45 52 56 45 44  ither a RESERVED
12eee 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   or EXCLUSIVE lo
12eef 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64  ck on the.  ** d
12ef0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66  atabase file. If
12ef1 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64   there is alread
12ef2 79 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  y an EXCLUSIVE l
12ef3 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ock, the followi
12ef4 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 20 69 73 20  ng.  ** call is 
12ef5 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20  a no-op..  **.  
12ef6 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f  ** Moving the lo
12ef7 63 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44  ck from RESERVED
12ef8 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63   to EXCLUSIVE ac
12ef9 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20  tually involves 
12efa 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75  going.  ** throu
12efb 67 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61  gh an intermedia
12efc 74 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47  te state PENDING
12efd 2e 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f  .   A PENDING lo
12efe 63 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a  ck prevents new.
12eff 20 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f    ** readers fro
12f00 6d 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74  m attaching to t
12f01 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20  he database but 
12f02 69 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20  is unsufficient 
12f03 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77  for us to.  ** w
12f04 72 69 74 65 2e 20 20 54 68 65 20 69 64 65 61 20  rite.  The idea 
12f05 6f 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  of a PENDING loc
12f06 6b 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20  k is to prevent 
12f07 6e 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d  new readers from
12f08 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20  .  ** coming in 
12f09 77 68 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f  while we wait fo
12f0a 72 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65  r existing reade
12f0b 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a  rs to clear..  *
12f0c 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65  *.  ** While the
12f0d 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65   pager is in the
12f0e 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c   RESERVED state,
12f0f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
12f10 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a  tabase file.  **
12f11 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e   is unchanged an
12f12 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63  d we can rollbac
12f13 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67  k without having
12f14 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65   to playback the
12f15 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e  .  ** journal in
12f16 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  to the original 
12f17 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
12f18 4f 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74 69  Once we transiti
12f19 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55  on to.  ** EXCLU
12f1a 53 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74  SIVE, it means t
12f1b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12f1c 20 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65   has been change
12f1d 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61  d and any rollba
12f1e 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71  ck.  ** will req
12f1f 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70  uire a journal p
12f20 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20  layback..  */.  
12f21 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
12f22 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
12f23 45 52 56 45 44 20 29 3b 0a 20 20 72 63 20 3d 20  ERVED );.  rc = 
12f24 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f  pager_wait_on_lo
12f25 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55  ck(pPager, EXCLU
12f26 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f  SIVE_LOCK);..  /
12f27 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73  * If the file is
12f28 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 68 61 73   a temp-file has
12f29 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70   not yet been op
12f2a 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f  ened, open it no
12f2b 77 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 6e 6f  w. It.  ** is no
12f2c 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 72  t possible for r
12f2d 63 20 74 6f 20 62 65 20 6f 74 68 65 72 20 74 68  c to be other th
12f2e 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  an SQLITE_OK if 
12f2f 74 68 69 73 20 62 72 61 6e 63 68 0a 20 20 2a 2a  this branch.  **
12f30 20 69 73 20 74 61 6b 65 6e 2c 20 61 73 20 70 61   is taken, as pa
12f31 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b  ger_wait_on_lock
12f32 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f  () is a no-op fo
12f33 72 20 74 65 6d 70 2d 66 69 6c 65 73 2e 0a 20 20  r temp-files..  
12f34 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e  */.  if( !isOpen
12f35 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a  (pPager->fd) ){.
12f36 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
12f37 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20  er->tempFile && 
12f38 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
12f39 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f  .    rc = pagerO
12f3a 70 65 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20  pentemp(pPager, 
12f3b 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67  pPager->fd, pPag
12f3c 65 72 2d 3e 76 66 73 46 6c 61 67 73 29 3b 0a 20  er->vfsFlags);. 
12f3d 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 72 63 3d   }..  while( rc=
12f3e 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4c  =SQLITE_OK && pL
12f3f 69 73 74 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20  ist ){.    Pgno 
12f40 70 67 6e 6f 20 3d 20 70 4c 69 73 74 2d 3e 70 67  pgno = pList->pg
12f41 6e 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  no;..    /* If t
12f42 68 65 72 65 20 61 72 65 20 64 69 72 74 79 20 70  here are dirty p
12f43 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65  ages in the page
12f44 20 63 61 63 68 65 20 77 69 74 68 20 70 61 67 65   cache with page
12f45 20 6e 75 6d 62 65 72 73 20 67 72 65 61 74 65 72   numbers greater
12f46 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67  .    ** than Pag
12f47 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20  er.dbSize, this 
12f48 6d 65 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67  means sqlite3Pag
12f49 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28  erTruncateImage(
12f4a 29 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a  ) was called to.
12f4b 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20      ** make the 
12f4c 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72  file smaller (pr
12f4d 65 73 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f  esumably by auto
12f4e 2d 76 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44  -vacuum code). D
12f4f 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20  o not write.    
12f50 2a 2a 20 61 6e 79 20 73 75 63 68 20 70 61 67 65  ** any such page
12f51 73 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20  s to the file.. 
12f52 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73     **.    ** Als
12f53 6f 2c 20 64 6f 20 6e 6f 74 20 77 72 69 74 65 20  o, do not write 
12f54 6f 75 74 20 61 6e 79 20 70 61 67 65 20 74 68 61  out any page tha
12f55 74 20 68 61 73 20 74 68 65 20 50 47 48 44 52 5f  t has the PGHDR_
12f56 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 0a  DONT_WRITE flag.
12f57 20 20 20 20 2a 2a 20 73 65 74 20 28 73 65 74 20      ** set (set 
12f58 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 44  by sqlite3PagerD
12f59 6f 6e 74 57 72 69 74 65 28 29 29 2e 20 20 4e 6f  ontWrite()).  No
12f5a 74 65 20 74 68 61 74 20 69 66 20 63 6f 6d 70 69  te that if compi
12f5b 6c 65 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20  led with.    ** 
12f5c 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45  SQLITE_SECURE_DE
12f5d 4c 45 54 45 20 74 68 65 20 50 47 48 44 52 5f 44  LETE the PGHDR_D
12f5e 4f 4e 54 5f 57 52 49 54 45 20 62 69 74 20 69 73  ONT_WRITE bit is
12f5f 20 6e 65 76 65 72 20 73 65 74 20 61 6e 64 20 73   never set and s
12f60 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 65 63  o.    ** the sec
12f61 6f 6e 64 20 74 65 73 74 20 69 73 20 61 6c 77 61  ond test is alwa
12f62 79 73 20 74 72 75 65 2e 0a 20 20 20 20 2a 2f 0a  ys true..    */.
12f63 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50      if( pgno<=pP
12f64 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20  ager->dbSize && 
12f65 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73  0==(pList->flags
12f66 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54  &PGHDR_DONT_WRIT
12f67 45 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20  E) ){.      i64 
12f68 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31  offset = (pgno-1
12f69 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70  )*(i64)pPager->p
12f6a 61 67 65 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66  ageSize;   /* Of
12f6b 66 73 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f  fset to write */
12f6c 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 44 61  .      char *pDa
12f6d 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
12f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f6f 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 6f        /* Data to
12f70 20 77 72 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20   write */    .. 
12f71 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74       /* Encode t
12f72 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
12f73 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67       CODEC2(pPag
12f74 65 72 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61  er, pList->pData
12f75 2c 20 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72  , pgno, 6, retur
12f76 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20  n SQLITE_NOMEM, 
12f77 70 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f  pData);..      /
12f78 2a 20 57 72 69 74 65 20 6f 75 74 20 74 68 65 20  * Write out the 
12f79 70 61 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20  page data. */.  
12f7a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12f7b 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
12f7c 66 64 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65  fd, pData, pPage
12f7d 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66  r->pageSize, off
12f7e 73 65 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  set);..      /* 
12f7f 49 66 20 70 61 67 65 20 31 20 77 61 73 20 6a 75  If page 1 was ju
12f80 73 74 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61  st written, upda
12f81 74 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56  te Pager.dbFileV
12f82 65 72 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20  ers to match.   
12f83 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20     ** the value 
12f84 6e 6f 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68  now stored in th
12f85 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
12f86 20 49 66 20 77 72 69 74 69 6e 67 20 74 68 69 73   If writing this
12f87 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20   .      ** page 
12f88 63 61 75 73 65 64 20 74 68 65 20 64 61 74 61 62  caused the datab
12f89 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77  ase file to grow
12f8a 2c 20 75 70 64 61 74 65 20 64 62 46 69 6c 65 53  , update dbFileS
12f8b 69 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20  ize. .      */. 
12f8c 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31       if( pgno==1
12f8d 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63   ){.        memc
12f8e 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69  py(&pPager->dbFi
12f8f 6c 65 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32  leVers, &pData[2
12f90 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65  4], sizeof(pPage
12f91 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b  r->dbFileVers));
12f92 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
12f93 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e  f( pgno>pPager->
12f94 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20  dbFileSize ){.  
12f95 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62        pPager->db
12f96 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b  FileSize = pgno;
12f97 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
12f98 2f 2a 20 55 70 64 61 74 65 20 61 6e 79 20 62 61  /* Update any ba
12f99 63 6b 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70  ckup objects cop
12f9a 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74  ying the content
12f9b 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e  s of this pager.
12f9c 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
12f9d 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50  3BackupUpdate(pP
12f9e 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70  ager->pBackup, p
12f9f 67 6e 6f 2c 20 28 75 38 2a 29 70 4c 69 73 74 2d  gno, (u8*)pList-
12fa0 3e 70 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20  >pData);..      
12fa1 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 4f  PAGERTRACE(("STO
12fa2 52 45 20 25 64 20 70 61 67 65 20 25 64 20 68 61  RE %d page %d ha
12fa3 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20  sh(%08x)\n",.   
12fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12fa5 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
12fa6 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67   pgno, pager_pag
12fa7 65 68 61 73 68 28 70 4c 69 73 74 29 29 29 3b 0a  ehash(pList)));.
12fa8 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
12fa9 50 47 4f 55 54 20 25 70 20 25 64 5c 6e 22 2c 20  PGOUT %p %d\n", 
12faa 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a  pPager, pgno));.
12fab 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52        PAGER_INCR
12fac 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77  (sqlite3_pager_w
12fad 72 69 74 65 64 62 5f 63 6f 75 6e 74 29 3b 0a 20  ritedb_count);. 
12fae 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28       PAGER_INCR(
12faf 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 29 3b  pPager->nWrite);
12fb0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
12fb1 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4e    PAGERTRACE(("N
12fb2 4f 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25  OSTORE %d page %
12fb3 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
12fb4 61 67 65 72 29 2c 20 70 67 6e 6f 29 29 3b 0a 20  ager), pgno));. 
12fb5 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
12fb6 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20  TE_CHECK_PAGES. 
12fb7 20 20 20 70 4c 69 73 74 2d 3e 70 61 67 65 48 61     pList->pageHa
12fb8 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68  sh = pager_pageh
12fb9 61 73 68 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64  ash(pList);.#end
12fba 69 66 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70  if.    pList = p
12fbb 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20  List->pDirty;.  
12fbc 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
12fbd 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20  }../*.** Append 
12fbe 61 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20  a record of the 
12fbf 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
12fc0 20 70 61 67 65 20 70 50 67 20 74 6f 20 74 68 65   page pPg to the
12fc1 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 0a 2a   sub-journal. .*
12fc2 2a 20 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c  * It is the call
12fc3 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  ers responsibili
12fc4 74 79 20 74 6f 20 75 73 65 20 73 75 62 6a 52 65  ty to use subjRe
12fc5 71 75 69 72 65 73 50 61 67 65 28 29 20 74 6f 20  quiresPage() to 
12fc6 63 68 65 63 6b 20 0a 2a 2a 20 74 68 61 74 20 69  check .** that i
12fc7 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75  t is really requ
12fc8 69 72 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c  ired before call
12fc9 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
12fca 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63  n..**.** If succ
12fcb 65 73 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20  essful, set the 
12fcc 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  bit correspondin
12fcd 67 20 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69  g to pPg->pgno i
12fce 6e 20 74 68 65 20 62 69 74 76 65 63 73 0a 2a 2a  n the bitvecs.**
12fcf 20 66 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73 61   for all open sa
12fd0 76 65 70 6f 69 6e 74 73 20 62 65 66 6f 72 65 20  vepoints before 
12fd1 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a  returning..**.**
12fd2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
12fd3 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
12fd4 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69   if everything i
12fd5 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 6e  s successful, an
12fd6 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64   IO.** error cod
12fd7 65 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 74  e if the attempt
12fd8 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65   to write to the
12fd9 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 61 69   sub-journal fai
12fda 6c 73 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c 49 54  ls, or .** SQLIT
12fdb 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c  E_NOMEM if a mal
12fdc 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20  loc fails while 
12fdd 73 65 74 74 69 6e 67 20 61 20 62 69 74 20 69 6e  setting a bit in
12fde 20 61 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20   a savepoint.** 
12fdf 62 69 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 74 69  bitvec..*/.stati
12fe0 63 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c  c int subjournal
12fe1 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29  Page(PgHdr *pPg)
12fe2 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
12fe3 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20  ITE_OK;.  Pager 
12fe4 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
12fe5 50 61 67 65 72 3b 0a 20 20 69 66 28 20 69 73 4f  Pager;.  if( isO
12fe6 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  pen(pPager->sjfd
12fe7 29 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70  ) ){.    void *p
12fe8 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74  Data = pPg->pDat
12fe9 61 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65  a;.    i64 offse
12fea 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62  t = pPager->nSub
12feb 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70  Rec*(4+pPager->p
12fec 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 63 68  ageSize);.    ch
12fed 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20  ar *pData2;..   
12fee 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20   CODEC2(pPager, 
12fef 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f  pData, pPg->pgno
12ff0 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  , 7, return SQLI
12ff1 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32  TE_NOMEM, pData2
12ff2 29 3b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43  );.    PAGERTRAC
12ff3 45 28 28 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c  E(("STMT-JOURNAL
12ff4 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20   %d page %d\n", 
12ff5 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
12ff6 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20   pPg->pgno));.  
12ff7 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67  .    assert( pag
12ff8 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20  eInJournal(pPg) 
12ff9 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61  || pPg->pgno>pPa
12ffa 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
12ffb 29 3b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74  );.    rc = writ
12ffc 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
12ffd 73 6a 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50  sjfd, offset, pP
12ffe 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66  g->pgno);.    if
12fff 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
13000 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
13001 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
13002 67 65 72 2d 3e 73 6a 66 64 2c 20 70 44 61 74 61  ger->sjfd, pData
13003 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  2, pPager->pageS
13004 69 7a 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b 0a  ize, offset+4);.
13005 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
13006 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
13007 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75  .    pPager->nSu
13008 62 52 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 65  bRec++;.    asse
13009 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76  rt( pPager->nSav
1300a 65 70 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 20  epoint>0 );.    
1300b 72 63 20 3d 20 61 64 64 54 6f 53 61 76 65 70 6f  rc = addToSavepo
1300c 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65  intBitvecs(pPage
1300d 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20  r, pPg->pgno);. 
1300e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1300f 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
13010 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
13011 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20  d by the pcache 
13012 6c 61 79 65 72 20 77 68 65 6e 20 69 74 20 68 61  layer when it ha
13013 73 20 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a  s reached some.*
13014 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69  * soft memory li
13015 6d 69 74 2e 20 54 68 65 20 66 69 72 73 74 20 61  mit. The first a
13016 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69  rgument is a poi
13017 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20  nter to a Pager 
13018 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61 73 74 20  object.** (cast 
13019 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 54 68 65  as a void*). The
1301a 20 70 61 67 65 72 20 69 73 20 61 6c 77 61 79 73   pager is always
1301b 20 27 70 75 72 67 65 61 62 6c 65 27 20 28 6e 6f   'purgeable' (no
1301c 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a  t an in-memory.*
1301d 2a 20 64 61 74 61 62 61 73 65 29 2e 20 54 68 65  * database). The
1301e 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
1301f 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20   is a reference 
13020 74 6f 20 61 20 70 61 67 65 20 74 68 61 74 20 69  to a page that i
13021 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  s .** currently 
13022 64 69 72 74 79 20 62 75 74 20 68 61 73 20 6e 6f  dirty but has no
13023 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66   outstanding ref
13024 65 72 65 6e 63 65 73 2e 20 54 68 65 20 70 61 67  erences. The pag
13025 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61  e.** is always a
13026 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
13027 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  he Pager object 
13028 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
13029 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  rst .** argument
1302a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20  ..**.** The job 
1302b 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
1302c 20 69 73 20 74 6f 20 6d 61 6b 65 20 70 50 67 20   is to make pPg 
1302d 63 6c 65 61 6e 20 62 79 20 77 72 69 74 69 6e 67  clean by writing
1302e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a   its contents.**
1302f 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
13030 62 61 73 65 20 66 69 6c 65 2c 20 69 66 20 70 6f  base file, if po
13031 73 73 69 62 6c 65 2e 20 54 68 69 73 20 6d 61 79  ssible. This may
13032 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63 69 6e 67   involve syncing
13033 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
13034 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  file. .**.** If 
13035 73 75 63 63 65 73 73 66 75 6c 2c 20 73 71 6c 69  successful, sqli
13036 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
13037 61 6e 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f  an() is called o
13038 6e 20 74 68 65 20 70 61 67 65 20 61 6e 64 0a 2a  n the page and.*
13039 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  * SQLITE_OK retu
1303a 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65  rned. If an IO e
1303b 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
1303c 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65  e trying to make
1303d 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63 6c 65   the.** page cle
1303e 61 6e 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72  an, the IO error
1303f 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
13040 64 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 63  d. If the page c
13041 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 64 65  annot be.** made
13042 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20   clean for some 
13043 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75  other reason, bu
13044 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  t no error occur
13045 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f  s, then SQLITE_O
13046 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  K.** is returned
13047 20 62 79 20 73 71 6c 69 74 65 33 50 63 61 63 68   by sqlite3Pcach
13048 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20  eMakeClean() is 
13049 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73  not called..*/.s
1304a 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53  tatic int pagerS
1304b 74 72 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 50  tress(void *p, P
1304c 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61  gHdr *pPg){.  Pa
1304d 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50  ger *pPager = (P
1304e 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 20  ager *)p;.  int 
1304f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
13050 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  .  assert( pPg->
13051 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29  pPager==pPager )
13052 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d  ;.  assert( pPg-
13053 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
13054 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  TY );..  /* The 
13055 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 69  doNotSync flag i
13056 73 20 73 65 74 20 62 79 20 74 68 65 20 73 71 6c  s set by the sql
13057 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
13058 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 6c 65 20   function while 
13059 69 74 0a 20 20 2a 2a 20 69 73 20 6a 6f 75 72 6e  it.  ** is journ
1305a 61 6c 6c 69 6e 67 20 61 20 73 65 74 20 6f 66 20  alling a set of 
1305b 74 77 6f 20 6f 72 20 6d 6f 72 65 20 64 61 74 61  two or more data
1305c 62 61 73 65 20 70 61 67 65 73 20 74 68 61 74 20  base pages that 
1305d 61 72 65 20 73 74 6f 72 65 64 0a 20 20 2a 2a 20  are stored.  ** 
1305e 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 69 73 6b  on the same disk
1305f 20 73 65 63 74 6f 72 2e 20 53 79 6e 63 69 6e 67   sector. Syncing
13060 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
13061 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 77 68 69 6c  not allowed whil
13062 65 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68  e.  ** this is h
13063 61 70 70 65 6e 69 6e 67 20 61 73 20 69 74 20 69  appening as it i
13064 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74  s important that
13065 20 61 6c 6c 20 6d 65 6d 62 65 72 73 20 6f 66 20   all members of 
13066 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 65 74 20  such a.  ** set 
13067 6f 66 20 70 61 67 65 73 20 61 72 65 20 73 79 6e  of pages are syn
13068 63 65 64 20 74 6f 20 64 69 73 6b 20 74 6f 67 65  ced to disk toge
13069 74 68 65 72 2e 20 53 6f 2c 20 69 66 20 74 68 65  ther. So, if the
1306a 20 70 61 67 65 20 74 68 69 73 20 66 75 6e 63 74   page this funct
1306b 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 72 79 69  ion.  ** is tryi
1306c 6e 67 20 74 6f 20 6d 61 6b 65 20 63 6c 65 61 6e  ng to make clean
1306d 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20   will require a 
1306e 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 61 6e 64  journal sync and
1306f 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 0a 20   the doNotSync. 
13070 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c   ** flag is set,
13071 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20   return without 
13072 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20  doing anything. 
13073 54 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 72  The pcache layer
13074 20 77 69 6c 6c 0a 20 20 2a 2a 20 6a 75 73 74 20   will.  ** just 
13075 68 61 76 65 20 74 6f 20 67 6f 20 61 68 65 61 64  have to go ahead
13076 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 61 20   and allocate a 
13077 6e 65 77 20 70 61 67 65 20 62 75 66 66 65 72 20  new page buffer 
13078 69 6e 73 74 65 61 64 20 6f 66 0a 20 20 2a 2a 20  instead of.  ** 
13079 72 65 75 73 69 6e 67 20 70 50 67 2e 0a 20 20 2a  reusing pPg..  *
1307a 2a 0a 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c 79  *.  ** Similarly
1307b 2c 20 69 66 20 74 68 65 20 70 61 67 65 72 20 68  , if the pager h
1307c 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72  as already enter
1307d 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  ed the error sta
1307e 74 65 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20  te, do not.  ** 
1307f 74 72 79 20 74 6f 20 77 72 69 74 65 20 74 68 65  try to write the
13080 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 67   contents of pPg
13081 20 74 6f 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20   to disk..  */. 
13082 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65   if( NEVER(pPage
13083 72 2d 3e 65 72 72 43 6f 64 65 29 0a 20 20 20 7c  r->errCode).   |
13084 7c 20 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74  | (pPager->doNot
13085 53 79 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61  Sync && pPg->fla
13086 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
13087 4e 43 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  NC).  ){.    ret
13088 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
13089 20 7d 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68   }..  /* Sync th
1308a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
1308b 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20  f required. */. 
1308c 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26   if( pPg->flags&
1308d 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
1308e 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63  ){.    rc = sync
1308f 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b  Journal(pPager);
13090 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
13091 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72  ITE_OK && pPager
13092 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20  ->fullSync && . 
13093 20 20 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a       !(pPager->j
13094 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
13095 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
13096 4d 4f 52 59 29 20 26 26 0a 20 20 20 20 20 20 21  MORY) &&.      !
13097 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65  (sqlite3OsDevice
13098 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
13099 70 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49  pPager->fd)&SQLI
1309a 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50  TE_IOCAP_SAFE_AP
1309b 50 45 4e 44 29 0a 20 20 20 20 29 7b 0a 20 20 20  PEND).    ){.   
1309c 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20     pPager->nRec 
1309d 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
1309e 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
1309f 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20  pPager);.    }. 
130a0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
130a1 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
130a2 68 69 73 20 70 61 67 65 20 69 73 20 6c 61 72 67  his page is larg
130a3 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72  er than the curr
130a4 65 6e 74 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a  ent size of.  **
130a5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d   the database im
130a6 61 67 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64  age, it may need
130a7 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74   to be written t
130a8 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  o the sub-journa
130a9 6c 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20  l..  ** This is 
130aa 62 65 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c  because the call
130ab 20 74 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f   to pager_write_
130ac 70 61 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77  pagelist() below
130ad 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61   will not.  ** a
130ae 63 74 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61  ctually write da
130af 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69  ta to the file i
130b0 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a  n this case..  *
130b1 2a 0a 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20  *.  ** Consider 
130b2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65  the following se
130b3 71 75 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 73  quence of events
130b4 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45  :.  **.  **   BE
130b5 47 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a  GIN;.  **     <j
130b6 6f 75 72 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20  ournal page X>. 
130b7 20 2a 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20   **     <modify 
130b8 70 61 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20  page X>.  **    
130b9 20 53 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20   SAVEPOINT sp;. 
130ba 20 2a 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e   **       <shrin
130bb 6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  k database file 
130bc 74 6f 20 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a  to Y pages>.  **
130bd 20 20 20 20 20 20 20 70 61 67 65 72 53 74 72 65         pagerStre
130be 73 73 28 70 61 67 65 20 58 29 0a 20 20 2a 2a 20  ss(page X).  ** 
130bf 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20      ROLLBACK TO 
130c0 73 70 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  sp;.  **.  ** If
130c1 20 28 58 3e 59 29 2c 20 74 68 65 6e 20 77 68 65   (X>Y), then whe
130c2 6e 20 70 61 67 65 72 53 74 72 65 73 73 20 69 73  n pagerStress is
130c3 20 63 61 6c 6c 65 64 20 70 61 67 65 20 58 20 77   called page X w
130c4 69 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74  ill not be writt
130c5 65 6e 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74  en.  ** out to t
130c6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
130c7 2c 20 62 75 74 20 77 69 6c 6c 20 62 65 20 64 72  , but will be dr
130c8 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63  opped from the c
130c9 61 63 68 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a  ache. Then,.  **
130ca 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22   following the "
130cb 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20  ROLLBACK TO sp" 
130cc 73 74 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69  statement, readi
130cd 6e 67 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72  ng page X will r
130ce 65 61 64 0a 20 20 2a 2a 20 64 61 74 61 20 66 72  ead.  ** data fr
130cf 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
130d0 66 69 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20  file. This will 
130d1 62 65 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70  be the copy of p
130d2 61 67 65 20 58 20 61 73 20 69 74 0a 20 20 2a 2a  age X as it.  **
130d3 20 77 61 73 20 77 68 65 6e 20 74 68 65 20 74 72   was when the tr
130d4 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65  ansaction starte
130d5 64 2c 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73  d, not as it was
130d6 20 77 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54   when "SAVEPOINT
130d7 20 73 70 22 0a 20 20 2a 2a 20 77 61 73 20 65 78   sp".  ** was ex
130d8 65 63 75 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  ecuted..  **.  *
130d9 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69  * The solution i
130da 73 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63  s to write the c
130db 75 72 72 65 6e 74 20 64 61 74 61 20 66 6f 72 20  urrent data for 
130dc 70 61 67 65 20 58 20 69 6e 74 6f 20 74 68 65 20  page X into the 
130dd 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61  .  ** sub-journa
130de 6c 20 66 69 6c 65 20 6e 6f 77 20 28 69 66 20 69  l file now (if i
130df 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  t is not already
130e0 20 74 68 65 72 65 29 2c 20 73 6f 20 74 68 61 74   there), so that
130e1 20 69 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65   it will.  ** be
130e2 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73   restored to its
130e3 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77   current value w
130e4 68 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43  hen the "ROLLBAC
130e5 4b 20 54 4f 20 73 70 22 20 69 73 20 0a 20 20 2a  K TO sp" is .  *
130e6 2a 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f  * executed..  */
130e7 0a 20 20 69 66 28 20 4e 45 56 45 52 28 0a 20 20  .  if( NEVER(.  
130e8 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f      rc==SQLITE_O
130e9 4b 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70  K && pPg->pgno>p
130ea 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26  Pager->dbSize &&
130eb 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
130ec 65 28 70 50 67 29 0a 20 20 29 20 29 7b 0a 20 20  e(pPg).  ) ){.  
130ed 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61    rc = subjourna
130ee 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 20 7d 0a  lPage(pPg);.  }.
130ef 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  .  /* Write the 
130f0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
130f1 70 61 67 65 20 6f 75 74 20 74 6f 20 74 68 65 20  page out to the 
130f2 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a  database file. *
130f3 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
130f4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 67  TE_OK ){.    pPg
130f5 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  ->pDirty = 0;.  
130f6 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69    rc = pager_wri
130f7 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29  te_pagelist(pPg)
130f8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b  ;.  }..  /* Mark
130f9 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65   the page as cle
130fa 61 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  an. */.  if( rc=
130fb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
130fc 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53    PAGERTRACE(("S
130fd 54 52 45 53 53 20 25 64 20 70 61 67 65 20 25 64  TRESS %d page %d
130fe 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
130ff 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29  ger), pPg->pgno)
13100 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  );.    sqlite3Pc
13101 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50  acheMakeClean(pP
13102 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  g);.  }..  retur
13103 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50  n pager_error(pP
13104 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f  ager, rc);.}.../
13105 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e  *.** Allocate an
13106 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e  d initialize a n
13107 65 77 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  ew Pager object 
13108 61 6e 64 20 70 75 74 20 61 20 70 6f 69 6e 74 65  and put a pointe
13109 72 20 74 6f 20 69 74 0a 2a 2a 20 69 6e 20 2a 70  r to it.** in *p
1310a 70 50 61 67 65 72 2e 20 54 68 65 20 70 61 67 65  pPager. The page
1310b 72 20 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61  r should eventua
1310c 6c 6c 79 20 62 65 20 66 72 65 65 64 20 62 79 20  lly be freed by 
1310d 70 61 73 73 69 6e 67 20 69 74 0a 2a 2a 20 74 6f  passing it.** to
1310e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
1310f 73 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  se()..**.** The 
13110 7a 46 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65  zFilename argume
13111 6e 74 20 69 73 20 74 68 65 20 70 61 74 68 20 74  nt is the path t
13112 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
13113 69 6c 65 20 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20  ile to open..** 
13114 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  If zFilename is 
13115 4e 55 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64  NULL then a rand
13116 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f  omly-named tempo
13117 72 61 72 79 20 66 69 6c 65 20 69 73 20 63 72 65  rary file is cre
13118 61 74 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64  ated.** and used
13119 20 61 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20   as the file to 
1311a 62 65 20 63 61 63 68 65 64 2e 20 54 65 6d 70 6f  be cached. Tempo
1311b 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 62  rary files are b
1311c 65 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 75 74  e deleted.** aut
1311d 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20  omatically when 
1311e 74 68 65 79 20 61 72 65 20 63 6c 6f 73 65 64 2e  they are closed.
1311f 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   If zFilename is
13120 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e   ":memory:" then
13121 20 0a 2a 2a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61   .** all informa
13122 74 69 6f 6e 20 69 73 20 68 65 6c 64 20 69 6e 20  tion is held in 
13123 63 61 63 68 65 2e 20 49 74 20 69 73 20 6e 65 76  cache. It is nev
13124 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69  er written to di
13125 73 6b 2e 20 0a 2a 2a 20 54 68 69 73 20 63 61 6e  sk. .** This can
13126 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c   be used to impl
13127 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f  ement an in-memo
13128 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  ry database..**.
13129 2a 2a 20 54 68 65 20 6e 45 78 74 72 61 20 70 61  ** The nExtra pa
1312a 72 61 6d 65 74 65 72 20 73 70 65 63 69 66 69 65  rameter specifie
1312b 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
1312c 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61  bytes of space a
1312d 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e  llocated.** alon
1312e 67 20 77 69 74 68 20 65 61 63 68 20 70 61 67 65  g with each page
1312f 20 72 65 66 65 72 65 6e 63 65 2e 20 54 68 69 73   reference. This
13130 20 73 70 61 63 65 20 69 73 20 61 76 61 69 6c 61   space is availa
13131 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 72 0a  ble to the user.
13132 2a 2a 20 76 69 61 20 74 68 65 20 73 71 6c 69 74  ** via the sqlit
13133 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
13134 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ) API..**.** The
13135 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20   flags argument 
13136 69 73 20 75 73 65 64 20 74 6f 20 73 70 65 63 69  is used to speci
13137 66 79 20 70 72 6f 70 65 72 74 69 65 73 20 74 68  fy properties th
13138 61 74 20 61 66 66 65 63 74 20 74 68 65 0a 2a 2a  at affect the.**
13139 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   operation of th
1313a 65 20 70 61 67 65 72 2e 20 49 74 20 73 68 6f 75  e pager. It shou
1313b 6c 64 20 62 65 20 70 61 73 73 65 64 20 73 6f 6d  ld be passed som
1313c 65 20 62 69 74 77 69 73 65 20 63 6f 6d 62 69 6e  e bitwise combin
1313d 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  ation.** of the 
1313e 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e  PAGER_OMIT_JOURN
1313f 41 4c 20 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f  AL and PAGER_NO_
13140 52 45 41 44 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a  READLOCK flags..
13141 2a 2a 0a 2a 2a 20 54 68 65 20 76 66 73 46 6c 61  **.** The vfsFla
13142 67 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  gs parameter is 
13143 61 20 62 69 74 6d 61 73 6b 20 74 6f 20 70 61 73  a bitmask to pas
13144 73 20 74 6f 20 74 68 65 20 66 6c 61 67 73 20 70  s to the flags p
13145 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 66 20 74  arameter.** of t
13146 68 65 20 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f  he xOpen() metho
13147 64 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65  d of the supplie
13148 64 20 56 46 53 20 77 68 65 6e 20 6f 70 65 6e 69  d VFS when openi
13149 6e 67 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a  ng files. .**.**
1314a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 6f 62   If the pager ob
1314b 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65  ject is allocate
1314c 64 20 61 6e 64 20 74 68 65 20 73 70 65 63 69 66  d and the specif
1314d 69 65 64 20 66 69 6c 65 20 6f 70 65 6e 65 64 20  ied file opened 
1314e 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79  .** successfully
1314f 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
13150 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50  eturned and *ppP
13151 61 67 65 72 20 73 65 74 20 74 6f 20 70 6f 69 6e  ager set to poin
13152 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20  t to.** the new 
13153 70 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66  pager object. If
13154 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
13155 2c 20 2a 70 70 50 61 67 65 72 20 69 73 20 73 65  , *ppPager is se
13156 74 20 74 6f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64  t to NULL.** and
13157 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75   error code retu
13158 72 6e 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74  rned. This funct
13159 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53  ion may return S
1315a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28  QLITE_NOMEM.** (
1315b 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20  sqlite3Malloc() 
1315c 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63  is used to alloc
1315d 61 74 65 20 6d 65 6d 6f 72 79 29 2c 20 53 51 4c  ate memory), SQL
1315e 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 6f 72 20  ITE_CANTOPEN or 
1315f 0a 2a 2a 20 76 61 72 69 6f 75 73 20 53 51 4c 49  .** various SQLI
13160 54 45 5f 49 4f 5f 58 58 58 20 65 72 72 6f 72 73  TE_IO_XXX errors
13161 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
13162 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
13163 61 67 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69  agerOpen(.  sqli
13164 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20  te3_vfs *pVfs,  
13165 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74       /* The virt
13166 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 20  ual file system 
13167 74 6f 20 75 73 65 20 2a 2f 0a 20 20 50 61 67 65  to use */.  Page
13168 72 20 2a 2a 70 70 50 61 67 65 72 2c 20 20 20 20  r **ppPager,    
13169 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74       /* OUT: Ret
1316a 75 72 6e 20 74 68 65 20 50 61 67 65 72 20 73 74  urn the Pager st
1316b 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a  ructure here */.
1316c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
1316d 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61  ilename,   /* Na
1316e 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1316f 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20  se file to open 
13170 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c  */.  int nExtra,
13171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13172 20 45 78 74 72 61 20 62 79 74 65 73 20 61 70 70   Extra bytes app
13173 65 6e 64 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d  end to each in-m
13174 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20  emory page */.  
13175 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
13176 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67           /* flag
13177 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68  s controlling th
13178 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  is file */.  int
13179 20 76 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20   vfsFlags,      
1317a 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70        /* flags p
1317b 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f  assed through to
1317c 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70   sqlite3_vfs.xOp
1317d 65 6e 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28  en() */.  void (
1317e 2a 78 52 65 69 6e 69 74 29 28 44 62 50 61 67 65  *xReinit)(DbPage
1317f 2a 29 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74  *) /* Function t
13180 6f 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 20 70  o reinitialize p
13181 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 75 38 20  ages */.){.  u8 
13182 2a 70 50 74 72 3b 0a 20 20 50 61 67 65 72 20 2a  *pPtr;.  Pager *
13183 70 50 61 67 65 72 20 3d 20 30 3b 20 20 20 20 20  pPager = 0;     
13184 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63    /* Pager objec
13185 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e  t to allocate an
13186 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e  d return */.  in
13187 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
13188 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  ;      /* Return
13189 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74   code */.  int t
1318a 65 6d 70 46 69 6c 65 20 3d 20 30 3b 20 20 20 20  empFile = 0;    
1318b 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
1318c 74 65 6d 70 20 66 69 6c 65 73 20 28 69 6e 63 6c  temp files (incl
1318d 2e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65  . in-memory file
1318e 73 29 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 44  s) */.  int memD
1318f 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  b = 0;          
13190 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
13191 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79   is an in-memory
13192 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72   file */.  int r
13193 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 20 20 20 20  eadOnly = 0;    
13194 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
13195 68 69 73 20 69 73 20 61 20 72 65 61 64 2d 6f 6e  his is a read-on
13196 6c 79 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  ly file */.  int
13197 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65   journalFileSize
13198 3b 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74  ;     /* Bytes t
13199 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 65  o allocate for e
1319a 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 64 20 2a  ach journal fd *
1319b 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 6e  /.  char *zPathn
1319c 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ame = 0;     /* 
1319d 46 75 6c 6c 20 70 61 74 68 20 74 6f 20 64 61 74  Full path to dat
1319e 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
1319f 69 6e 74 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20  int nPathname = 
131a0 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  0;       /* Numb
131a1 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a  er of bytes in z
131a2 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e  Pathname */.  in
131a3 74 20 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28  t useJournal = (
131a4 66 6c 61 67 73 20 26 20 50 41 47 45 52 5f 4f 4d  flags & PAGER_OM
131a5 49 54 5f 4a 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20  IT_JOURNAL)==0; 
131a6 2f 2a 20 46 61 6c 73 65 20 74 6f 20 6f 6d 69 74  /* False to omit
131a7 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e   journal */.  in
131a8 74 20 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28  t noReadlock = (
131a9 66 6c 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f  flags & PAGER_NO
131aa 5f 52 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 20 20  _READLOCK)!=0;  
131ab 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20  /* True to omit 
131ac 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69  read-lock */.  i
131ad 6e 74 20 70 63 61 63 68 65 53 69 7a 65 20 3d 20  nt pcacheSize = 
131ae 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a  sqlite3PcacheSiz
131af 65 28 29 3b 20 20 20 20 20 20 20 2f 2a 20 42 79  e();       /* By
131b0 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  tes to allocate 
131b1 66 6f 72 20 50 43 61 63 68 65 20 2a 2f 0a 20 20  for PCache */.  
131b2 75 31 36 20 73 7a 50 61 67 65 44 66 6c 74 20 3d  u16 szPageDflt =
131b3 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
131b4 50 41 47 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44  PAGE_SIZE;  /* D
131b5 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65  efault page size
131b6 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65   */..  /* Figure
131b7 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70   out how much sp
131b8 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64 20  ace is required 
131b9 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c  for each journal
131ba 20 66 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a   file-handle.  *
131bb 2a 20 28 74 68 65 72 65 20 61 72 65 20 74 77 6f  * (there are two
131bc 20 6f 66 20 74 68 65 6d 2c 20 74 68 65 20 6d 61   of them, the ma
131bd 69 6e 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74  in journal and t
131be 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e  he sub-journal).
131bf 20 54 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68   This.  ** is th
131c0 65 20 6d 61 78 69 6d 75 6d 20 73 70 61 63 65 20  e maximum space 
131c1 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20  required for an 
131c2 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61  in-memory journa
131c3 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20  l file handle . 
131c4 20 2a 2a 20 61 6e 64 20 61 20 72 65 67 75 6c 61   ** and a regula
131c5 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68  r journal file-h
131c6 61 6e 64 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74  andle. Note that
131c7 20 61 20 22 72 65 67 75 6c 61 72 20 6a 6f 75 72   a "regular jour
131c8 6e 61 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a  nal-handle".  **
131c9 20 6d 61 79 20 62 65 20 61 20 77 72 61 70 70 65   may be a wrappe
131ca 72 20 63 61 70 61 62 6c 65 20 6f 66 20 63 61 63  r capable of cac
131cb 68 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70  hing the first p
131cc 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f  ortion of the jo
131cd 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20  urnal.  ** file 
131ce 69 6e 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70  in memory to imp
131cf 6c 65 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69  lement the atomi
131d0 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
131d1 74 69 6f 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20  tion (see .  ** 
131d2 73 6f 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72  source file jour
131d3 6e 61 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69  nal.c)..  */.  i
131d4 66 28 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  f( sqlite3Journa
131d5 6c 53 69 7a 65 28 70 56 66 73 29 3e 73 71 6c 69  lSize(pVfs)>sqli
131d6 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a  te3MemJournalSiz
131d7 65 28 29 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e  e() ){.    journ
131d8 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55  alFileSize = ROU
131d9 4e 44 38 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e  ND8(sqlite3Journ
131da 61 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20  alSize(pVfs));. 
131db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72   }else{.    jour
131dc 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f  nalFileSize = RO
131dd 55 4e 44 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a  UND8(sqlite3MemJ
131de 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20  ournalSize());. 
131df 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
131e0 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
131e1 20 74 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65   to NULL in case
131e2 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
131e3 2e 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20  . */.  *ppPager 
131e4 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75  = 0;..  /* Compu
131e5 74 65 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  te and store the
131e6 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69   full pathname i
131e7 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  n an allocated b
131e8 75 66 66 65 72 20 70 6f 69 6e 74 65 64 0a 20 20  uffer pointed.  
131e9 2a 2a 20 74 6f 20 62 79 20 7a 50 61 74 68 6e 61  ** to by zPathna
131ea 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 50 61 74 68  me, length nPath
131eb 6e 61 6d 65 2e 20 4f 72 2c 20 69 66 20 74 68 69  name. Or, if thi
131ec 73 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79  s is a temporary
131ed 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76   file,.  ** leav
131ee 65 20 62 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65  e both nPathname
131ef 20 61 6e 64 20 7a 50 61 74 68 6e 61 6d 65 20 73   and zPathname s
131f0 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20  et to 0..  */.  
131f1 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26  if( zFilename &&
131f2 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b   zFilename[0] ){
131f3 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d  .    nPathname =
131f4 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
131f5 65 2b 31 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61  e+1;.    zPathna
131f6 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  me = sqlite3Mall
131f7 6f 63 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b  oc(nPathname*2);
131f8 0a 20 20 20 20 69 66 28 20 7a 50 61 74 68 6e 61  .    if( zPathna
131f9 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  me==0 ){.      r
131fa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
131fb 45 4d 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  EM;.    }.#ifnde
131fc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45  f SQLITE_OMIT_ME
131fd 4d 4f 52 59 44 42 0a 20 20 20 20 69 66 28 20 73  MORYDB.    if( s
131fe 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c  trcmp(zFilename,
131ff 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29  ":memory:")==0 )
13200 7b 0a 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20  {.      memDb = 
13201 31 3b 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61  1;.      zPathna
13202 6d 65 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d  me[0] = 0;.    }
13203 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
13204 7b 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d  {.      zPathnam
13205 65 5b 30 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b  e[0] = 0; /* Mak
13206 65 20 73 75 72 65 20 69 6e 69 74 69 61 6c 69 7a  e sure initializ
13207 65 64 20 65 76 65 6e 20 69 66 20 46 75 6c 6c 50  ed even if FullP
13208 61 74 68 6e 61 6d 65 28 29 20 66 61 69 6c 73 20  athname() fails 
13209 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  */.      rc = sq
1320a 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e  lite3OsFullPathn
1320b 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e  ame(pVfs, zFilen
1320c 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20  ame, nPathname, 
1320d 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  zPathname);.    
1320e 7d 0a 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65  }..    nPathname
1320f 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
13210 33 30 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20  30(zPathname);. 
13211 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13212 45 5f 4f 4b 20 26 26 20 6e 50 61 74 68 6e 61 6d  E_OK && nPathnam
13213 65 2b 38 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68  e+8>pVfs->mxPath
13214 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  name ){.      /*
13215 20 54 68 69 73 20 62 72 61 6e 63 68 20 69 73 20   This branch is 
13216 74 61 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 6a  taken when the j
13217 6f 75 72 6e 61 6c 20 70 61 74 68 20 72 65 71 75  ournal path requ
13218 69 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a  ired by.      **
13219 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65   the database be
1321a 69 6e 67 20 6f 70 65 6e 65 64 20 77 69 6c 6c 20  ing opened will 
1321b 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 70 56 66  be more than pVf
1321c 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 20 20  s->mxPathname.  
1321d 20 20 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20      ** bytes in 
1321e 6c 65 6e 67 74 68 2e 20 54 68 69 73 20 6d 65 61  length. This mea
1321f 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ns the database 
13220 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64  cannot be opened
13221 2c 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 69 74  ,.      ** as it
13222 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 70 6f 73   will not be pos
13223 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68  sible to open th
13224 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  e journal file o
13225 72 20 65 76 65 6e 0a 20 20 20 20 20 20 2a 2a 20  r even.      ** 
13226 63 68 65 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d  check for a hot-
13227 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 72  journal before r
13228 65 61 64 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f  eading..      */
13229 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1322a 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20  TE_CANTOPEN;.   
1322b 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
1322c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1322d 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
1322e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Pathname);.     
1322f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
13230 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  }.  }..  /* Allo
13231 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20  cate memory for 
13232 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74  the Pager struct
13233 75 72 65 2c 20 50 43 61 63 68 65 20 6f 62 6a 65  ure, PCache obje
13234 63 74 2c 20 74 68 65 0a 20 20 2a 2a 20 74 68 72  ct, the.  ** thr
13235 65 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ee file descript
13236 6f 72 73 2c 20 74 68 65 20 64 61 74 61 62 61 73  ors, the databas
13237 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20  e file name and 
13238 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a  the journal .  *
13239 2a 20 66 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65  * file name. The
1323a 20 6c 61 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72   layout in memor
1323b 79 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  y is as follows:
1323c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50  .  **.  **     P
1323d 61 67 65 72 20 6f 62 6a 65 63 74 20 20 20 20 20  ager object     
1323e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1323f 73 69 7a 65 6f 66 28 50 61 67 65 72 29 20 62 79  sizeof(Pager) by
13240 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 50 43  tes).  **     PC
13241 61 63 68 65 20 6f 62 6a 65 63 74 20 20 20 20 20  ache object     
13242 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 73                (s
13243 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65  qlite3PcacheSize
13244 28 29 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20  () bytes).  **  
13245 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65     Database file
13246 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20   handle         
13247 20 20 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69     (pVfs->szOsFi
13248 6c 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20  le bytes).  **  
13249 20 20 20 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66     Sub-journal f
1324a 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20  ile handle      
1324b 20 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53     (journalFileS
1324c 69 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20  ize bytes).  ** 
1324d 20 20 20 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c      Main journal
1324e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20   file handle    
1324f 20 20 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65      (journalFile
13250 53 69 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a  Size bytes).  **
13251 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69       Database fi
13252 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20  le name         
13253 20 20 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b       (nPathname+
13254 31 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20  1 bytes).  **   
13255 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e    Journal file n
13256 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ame             
13257 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31    (nPathname+8+1
13258 20 62 79 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70   bytes).  */.  p
13259 50 74 72 20 3d 20 28 75 38 20 2a 29 73 71 6c 69  Ptr = (u8 *)sqli
1325a 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20  te3MallocZero(. 
1325b 20 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66     ROUND8(sizeof
1325c 28 2a 70 50 61 67 65 72 29 29 20 2b 20 20 20 20  (*pPager)) +    
1325d 20 20 2f 2a 20 50 61 67 65 72 20 73 74 72 75 63    /* Pager struc
1325e 74 75 72 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e  ture */.    ROUN
1325f 44 38 28 70 63 61 63 68 65 53 69 7a 65 29 20 2b  D8(pcacheSize) +
13260 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43             /* PC
13261 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ache object */. 
13262 20 20 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e     ROUND8(pVfs->
13263 73 7a 4f 73 46 69 6c 65 29 20 2b 20 20 20 20 20  szOsFile) +     
13264 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62    /* The main db
13265 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75   file */.    jou
13266 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32  rnalFileSize * 2
13267 20 2b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54   +          /* T
13268 68 65 20 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66  he two journal f
13269 69 6c 65 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61  iles */ .    nPa
1326a 74 68 6e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20  thname + 1 +    
1326b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a              /* z
1326c 46 69 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  Filename */.    
1326d 6e 50 61 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20  nPathname + 8 + 
1326e 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  1              /
1326f 2a 20 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  * zJournal */.  
13270 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  );.  assert( EIG
13271 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
13272 54 28 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  T(SQLITE_INT_TO_
13273 50 54 52 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53  PTR(journalFileS
13274 69 7a 65 29 29 20 29 3b 0a 20 20 69 66 28 20 21  ize)) );.  if( !
13275 70 50 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  pPtr ){.    sqli
13276 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61  te3_free(zPathna
13277 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
13278 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
13279 7d 0a 20 20 70 50 61 67 65 72 20 3d 20 20 20 20  }.  pPager =    
1327a 20 20 20 20 20 20 20 20 20 20 28 50 61 67 65 72            (Pager
1327b 2a 29 28 70 50 74 72 29 3b 0a 20 20 70 50 61 67  *)(pPtr);.  pPag
1327c 65 72 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 20  er->pPCache =   
1327d 20 28 50 43 61 63 68 65 2a 29 28 70 50 74 72 20   (PCache*)(pPtr 
1327e 2b 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66  += ROUND8(sizeof
1327f 28 2a 70 50 61 67 65 72 29 29 29 3b 0a 20 20 70  (*pPager)));.  p
13280 50 61 67 65 72 2d 3e 66 64 20 3d 20 20 20 28 73  Pager->fd =   (s
13281 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50  qlite3_file*)(pP
13282 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61  tr += ROUND8(pca
13283 63 68 65 53 69 7a 65 29 29 3b 0a 20 20 70 50 61  cheSize));.  pPa
13284 67 65 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c  ger->sjfd = (sql
13285 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72  ite3_file*)(pPtr
13286 20 2b 3d 20 52 4f 55 4e 44 38 28 70 56 66 73 2d   += ROUND8(pVfs-
13287 3e 73 7a 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70  >szOsFile));.  p
13288 50 61 67 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73  Pager->jfd =  (s
13289 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50  qlite3_file*)(pP
1328a 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c  tr += journalFil
1328b 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 72  eSize);.  pPager
1328c 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20  ->zFilename =   
1328d 20 28 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d   (char*)(pPtr +=
1328e 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65   journalFileSize
1328f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  );.  assert( EIG
13290 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
13291 54 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29  T(pPager->jfd) )
13292 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20  ;..  /* Fill in 
13293 74 68 65 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e  the Pager.zFilen
13294 61 6d 65 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a  ame and Pager.zJ
13295 6f 75 72 6e 61 6c 20 62 75 66 66 65 72 73 2c 20  ournal buffers, 
13296 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a  if required. */.
13297 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20    if( zPathname 
13298 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 7a  ){.    pPager->z
13299 4a 6f 75 72 6e 61 6c 20 3d 20 20 20 28 63 68 61  Journal =   (cha
1329a 72 2a 29 28 70 50 74 72 20 2b 3d 20 6e 50 61 74  r*)(pPtr += nPat
1329b 68 6e 61 6d 65 20 2b 20 31 29 3b 0a 20 20 20 20  hname + 1);.    
1329c 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a  memcpy(pPager->z
1329d 46 69 6c 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e  Filename, zPathn
1329e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b  ame, nPathname);
1329f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67  .    memcpy(pPag
132a0 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50  er->zJournal, zP
132a1 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61  athname, nPathna
132a2 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  me);.    memcpy(
132a3 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61  &pPager->zJourna
132a4 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d  l[nPathname], "-
132a5 6a 6f 75 72 6e 61 6c 22 2c 20 38 29 3b 0a 20 20  journal", 8);.  
132a6 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 7a 46    if( pPager->zF
132a7 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 20  ilename[0]==0 ) 
132a8 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
132a9 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  [0] = 0;.    sql
132aa 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e  ite3_free(zPathn
132ab 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67  ame);.  }.  pPag
132ac 65 72 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b  er->pVfs = pVfs;
132ad 0a 20 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c  .  pPager->vfsFl
132ae 61 67 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a  ags = vfsFlags;.
132af 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70  .  /* Open the p
132b0 61 67 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a  ager file..  */.
132b1 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20    if( zFilename 
132b2 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20  && zFilename[0] 
132b3 26 26 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20  && !memDb ){.   
132b4 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20 20   int fout = 0;  
132b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132b6 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 72    /* VFS flags r
132b7 65 74 75 72 6e 65 64 20 62 79 20 78 4f 70 65 6e  eturned by xOpen
132b8 28 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  () */.    rc = s
132b9 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
132ba 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  s, pPager->zFile
132bb 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64  name, pPager->fd
132bc 2c 20 76 66 73 46 6c 61 67 73 2c 20 26 66 6f 75  , vfsFlags, &fou
132bd 74 29 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79  t);.    readOnly
132be 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f   = (fout&SQLITE_
132bf 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a  OPEN_READONLY);.
132c0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66  .    /* If the f
132c1 69 6c 65 20 77 61 73 20 73 75 63 63 65 73 73 66  ile was successf
132c2 75 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20  ully opened for 
132c3 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73  read/write acces
132c4 73 2c 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65  s,.    ** choose
132c5 20 61 20 64 65 66 61 75 6c 74 20 70 61 67 65 20   a default page 
132c6 73 69 7a 65 20 69 6e 20 63 61 73 65 20 77 65 20  size in case we 
132c7 68 61 76 65 20 74 6f 20 63 72 65 61 74 65 20 74  have to create t
132c8 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61  he.    ** databa
132c9 73 65 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66  se file. The def
132ca 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69  ault page size i
132cb 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66  s the maximum of
132cc 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
132cd 20 20 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41     + SQLITE_DEFA
132ce 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20  ULT_PAGE_SIZE,. 
132cf 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76     **    + The v
132d0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
132d1 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72   sqlite3OsSector
132d2 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 20  Size().    **   
132d3 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 70   + The largest p
132d4 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 61  age size that ca
132d5 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f  n be written ato
132d6 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a  mically..    */.
132d7 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
132d8 54 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e  TE_OK && !readOn
132d9 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 65 74 53  ly ){.      setS
132da 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72  ectorSize(pPager
132db 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
132dc 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
132dd 41 47 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 45  AGE_SIZE<=SQLITE
132de 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
132df 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 69  E_SIZE);.      i
132e0 66 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 50  f( szPageDflt<pP
132e1 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
132e2 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
132e3 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
132e4 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45  ze>SQLITE_MAX_DE
132e5 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20  FAULT_PAGE_SIZE 
132e6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50  ){.          szP
132e7 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45  ageDflt = SQLITE
132e8 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
132e9 45 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20  E_SIZE;.        
132ea 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
132eb 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 75   szPageDflt = (u
132ec 31 36 29 70 50 61 67 65 72 2d 3e 73 65 63 74 6f  16)pPager->secto
132ed 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 7d  rSize;.        }
132ee 0a 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20  .      }.#ifdef 
132ef 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
132f0 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20  OMIC_WRITE.     
132f1 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69   {.        int i
132f2 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65  Dc = sqlite3OsDe
132f3 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
132f4 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ics(pPager->fd);
132f5 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b  .        int ii;
132f6 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
132f7 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
132f8 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29  MIC512==(512>>8)
132f9 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
132fa 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  t(SQLITE_IOCAP_A
132fb 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36  TOMIC64K==(65536
132fc 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61  >>8));.        a
132fd 73 73 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58  ssert(SQLITE_MAX
132fe 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
132ff 5a 45 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20  ZE<=65536);.    
13300 20 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67      for(ii=szPag
13301 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54  eDflt; ii<=SQLIT
13302 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
13303 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32  GE_SIZE; ii=ii*2
13304 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
13305 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f 43   iDc&(SQLITE_IOC
13306 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e 38  AP_ATOMIC|(ii>>8
13307 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  )) ){.          
13308 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 69    szPageDflt = i
13309 69 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  i;.          }. 
1330a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1330b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20  .#endif.    }.  
1330c 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66  }else{.    /* If
1330d 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
1330e 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  e is requested, 
1330f 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64  it is not opened
13310 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20   immediately..  
13311 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73    ** In this cas
13312 65 20 77 65 20 61 63 63 65 70 74 20 74 68 65 20  e we accept the 
13313 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a  default page siz
13314 65 20 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75  e and delay actu
13315 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e  ally.    ** open
13316 69 6e 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74  ing the file unt
13317 69 6c 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  il the first cal
13318 6c 20 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a  l to OsWrite()..
13319 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
1331a 69 73 20 62 72 61 6e 63 68 20 69 73 20 61 6c 73  is branch is als
1331b 6f 20 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e 2d  o run for an in-
1331c 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e  memory database.
1331d 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20   An in-memory.  
1331e 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73    ** database is
1331f 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 74   the same as a t
13320 65 6d 70 2d 66 69 6c 65 20 74 68 61 74 20 69 73  emp-file that is
13321 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 6f   never written o
13322 75 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73  ut to.    ** dis
13323 6b 20 61 6e 64 20 75 73 65 73 20 61 6e 20 69 6e  k and uses an in
13324 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b  -memory rollback
13325 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f   journal..    */
13326 20 0a 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d   .    tempFile =
13327 20 31 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   1;.    pPager->
13328 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58  state = PAGER_EX
13329 43 4c 55 53 49 56 45 3b 0a 20 20 20 20 72 65 61  CLUSIVE;.    rea
1332a 64 4f 6e 6c 79 20 3d 20 28 76 66 73 46 6c 61 67  dOnly = (vfsFlag
1332b 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  s&SQLITE_OPEN_RE
1332c 41 44 4f 4e 4c 59 29 3b 0a 20 20 7d 0a 0a 20 20  ADONLY);.  }..  
1332d 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
1332e 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 65   call to PagerSe
1332f 74 50 61 67 65 73 69 7a 65 28 29 20 73 65 72 76  tPagesize() serv
13330 65 73 20 74 6f 20 73 65 74 20 74 68 65 20 76 61  es to set the va
13331 6c 75 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61 67  lue of .  ** Pag
13332 65 72 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 20  er.pageSize and 
13333 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  to allocate the 
13334 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20  Pager.pTmpSpace 
13335 62 75 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  buffer..  */.  i
13336 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13337 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
13338 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30  pPager->memDb==0
13339 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
1333a 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65  ite3PagerSetPage
1333b 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 73 7a  size(pPager, &sz
1333c 50 61 67 65 44 66 6c 74 2c 20 2d 31 29 3b 0a 20  PageDflt, -1);. 
1333d 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21     testcase( rc!
1333e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
1333f 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72  }..  /* If an er
13340 72 6f 72 20 6f 63 63 75 72 72 65 64 20 69 6e 20  ror occurred in 
13341 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 62 6c  either of the bl
13342 6f 63 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 65  ocks above, free
13343 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72   the .  ** Pager
13344 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 63   structure and c
13345 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20  lose the file.. 
13346 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51   */.  if( rc!=SQ
13347 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
13348 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e  ssert( !pPager->
13349 70 54 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 20  pTmpSpace );.   
1334a 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
1334b 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20  pPager->fd);.   
1334c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
1334d 61 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72  ager);.    retur
1334e 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
1334f 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 50  Initialize the P
13350 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f  Cache object. */
13351 0a 20 20 61 73 73 65 72 74 28 20 6e 45 78 74 72  .  assert( nExtr
13352 61 3c 31 30 30 30 20 29 3b 0a 20 20 6e 45 78 74  a<1000 );.  nExt
13353 72 61 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 74  ra = ROUND8(nExt
13354 72 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63  ra);.  sqlite3Pc
13355 61 63 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 44  acheOpen(szPageD
13356 66 6c 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d 65  flt, nExtra, !me
13357 6d 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  mDb,.           
13358 20 20 20 20 20 20 20 20 20 21 6d 65 6d 44 62 3f           !memDb?
13359 70 61 67 65 72 53 74 72 65 73 73 3a 30 2c 20 28  pagerStress:0, (
1335a 76 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70  void *)pPager, p
1335b 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b  Pager->pPCache);
1335c 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ..  PAGERTRACE((
1335d 22 4f 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20  "OPEN %d %s\n", 
1335e 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 61  FILEHANDLEID(pPa
1335f 67 65 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 72  ger->fd), pPager
13360 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20  ->zFilename));. 
13361 20 49 4f 54 52 41 43 45 28 28 22 4f 50 45 4e 20   IOTRACE(("OPEN 
13362 25 70 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 72  %p %s\n", pPager
13363 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e  , pPager->zFilen
13364 61 6d 65 29 29 0a 0a 20 20 70 50 61 67 65 72 2d  ame))..  pPager-
13365 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 75  >useJournal = (u
13366 38 29 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20  8)useJournal;.  
13367 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f  pPager->noReadlo
13368 63 6b 20 3d 20 28 6e 6f 52 65 61 64 6c 6f 63 6b  ck = (noReadlock
13369 20 26 26 20 72 65 61 64 4f 6e 6c 79 29 20 3f 31   && readOnly) ?1
1336a 3a 30 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  :0;.  /* pPager-
1336b 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a  >stmtOpen = 0; *
1336c 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73  /.  /* pPager->s
1336d 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f  tmtInUse = 0; */
1336e 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52  .  /* pPager->nR
1336f 65 66 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  ef = 0; */.  pPa
13370 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
13371 20 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20   = (u8)memDb;.  
13372 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53  /* pPager->stmtS
13373 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a  ize = 0; */.  /*
13374 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69   pPager->stmtJSi
13375 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20  ze = 0; */.  /* 
13376 70 50 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20  pPager->nPage = 
13377 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e  0; */.  pPager->
13378 6d 78 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f  mxPgno = SQLITE_
13379 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a  MAX_PAGE_COUNT;.
1337a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61    /* pPager->sta
1337b 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43  te = PAGER_UNLOC
1337c 4b 3b 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  K; */.  assert( 
1337d 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d  pPager->state ==
1337e 20 28 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47   (tempFile ? PAG
1337f 45 52 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50  ER_EXCLUSIVE : P
13380 41 47 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a  AGER_UNLOCK) );.
13381 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72    /* pPager->err
13382 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70  Mask = 0; */.  p
13383 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
13384 3d 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a  = (u8)tempFile;.
13385 20 20 61 73 73 65 72 74 28 20 74 65 6d 70 46 69    assert( tempFi
13386 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  le==PAGER_LOCKIN
13387 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20  GMODE_NORMAL .  
13388 20 20 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46          || tempF
13389 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ile==PAGER_LOCKI
1338a 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
1338b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41   );.  assert( PA
1338c 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
1338d 45 58 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a  EXCLUSIVE==1 );.
1338e 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73    pPager->exclus
1338f 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65  iveMode = (u8)te
13390 6d 70 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65  mpFile; .  pPage
13391 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
13392 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d  ne = pPager->tem
13393 70 46 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d  pFile;.  pPager-
13394 3e 6d 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d  >memDb = (u8)mem
13395 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65  Db;.  pPager->re
13396 61 64 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61  adOnly = (u8)rea
13397 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67  dOnly;.  /* pPag
13398 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  er->needSync = 0
13399 3b 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75  ; */.  assert( u
1339a 73 65 4a 6f 75 72 6e 61 6c 20 7c 7c 20 70 50 61  seJournal || pPa
1339b 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
1339c 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  .  pPager->noSyn
1339d 63 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  c = pPager->temp
1339e 46 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e  File;.  pPager->
1339f 66 75 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65  fullSync = pPage
133a0 72 2d 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a  r->noSync ?0:1;.
133a1 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66    pPager->sync_f
133a2 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59  lags = SQLITE_SY
133a3 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20  NC_NORMAL;.  /* 
133a4 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d  pPager->pFirst =
133a5 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
133a6 65 72 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64  er->pFirstSynced
133a7 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
133a8 61 67 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b  ager->pLast = 0;
133a9 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45   */.  pPager->nE
133aa 78 74 72 61 20 3d 20 28 75 31 36 29 6e 45 78 74  xtra = (u16)nExt
133ab 72 61 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f  ra;.  pPager->jo
133ac 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d  urnalSizeLimit =
133ad 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
133ae 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d  JOURNAL_SIZE_LIM
133af 49 54 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73  IT;.  assert( is
133b0 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
133b1 20 7c 7c 20 74 65 6d 70 46 69 6c 65 20 29 3b 0a   || tempFile );.
133b2 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28    setSectorSize(
133b3 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21  pPager);.  if( !
133b4 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  useJournal ){.  
133b5 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
133b6 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f  lMode = PAGER_JO
133b7 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 3b 0a 20  URNALMODE_OFF;. 
133b8 20 7d 65 6c 73 65 20 69 66 28 20 6d 65 6d 44 62   }else if( memDb
133b9 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
133ba 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41  journalMode = PA
133bb 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
133bc 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a  MEMORY;.  }.  /*
133bd 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61   pPager->xBusyHa
133be 6e 64 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20  ndler = 0; */.  
133bf 2f 2a 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79  /* pPager->pBusy
133c0 48 61 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20  HandlerArg = 0; 
133c1 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65  */.  pPager->xRe
133c2 69 6e 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74  initer = xReinit
133c3 3b 0a 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50  ;.  /* memset(pP
133c4 61 67 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20  ager->aHash, 0, 
133c5 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61  sizeof(pPager->a
133c6 48 61 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70  Hash)); */.  *pp
133c7 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a  Pager = pPager;.
133c8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
133c9 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54  OK;.}..../*.** T
133ca 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
133cb 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61  called after tra
133cc 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20  nsitioning from 
133cd 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a  PAGER_UNLOCK to.
133ce 2a 2a 20 50 41 47 45 52 5f 53 48 41 52 45 44 20  ** PAGER_SHARED 
133cf 73 74 61 74 65 2e 20 49 74 20 74 65 73 74 73 20  state. It tests 
133d0 69 66 20 74 68 65 72 65 20 69 73 20 61 20 68 6f  if there is a ho
133d1 74 20 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e  t journal presen
133d2 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65  t in.** the file
133d3 2d 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20  -system for the 
133d4 67 69 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68  given pager. A h
133d5 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e  ot journal is on
133d6 65 20 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73  e that .** needs
133d7 20 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61   to be played ba
133d8 63 6b 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f  ck. According to
133d9 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
133da 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a  a hot-journal.**
133db 20 66 69 6c 65 20 65 78 69 73 74 73 20 69 66 20   file exists if 
133dc 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72  the following cr
133dd 69 74 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a  iteria are met:.
133de 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f  **.**   * The jo
133df 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74  urnal file exist
133e0 73 20 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79  s in the file sy
133e1 73 74 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a  stem, and.**   *
133e2 20 4e 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64   No process hold
133e3 73 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  s a RESERVED or 
133e4 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20  greater lock on 
133e5 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
133e6 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68  e, and.**   * Th
133e7 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
133e8 69 74 73 65 6c 66 20 69 73 20 67 72 65 61 74 65  itself is greate
133e9 72 20 74 68 61 6e 20 30 20 62 79 74 65 73 20 69  r than 0 bytes i
133ea 6e 20 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20  n size, and.**  
133eb 20 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74   * The first byt
133ec 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
133ed 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64   file exists and
133ee 20 69 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a   is not 0x00..**
133ef 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65  .** If the curre
133f0 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  nt size of the d
133f1 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
133f2 30 20 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20  0 but a journal 
133f3 66 69 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20  file.** exists, 
133f4 74 68 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79  that is probably
133f5 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20   an old journal 
133f6 6c 65 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61  left over from a
133f7 20 70 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61   prior.** databa
133f8 73 65 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  se with the same
133f9 20 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63   name. In this c
133fa 61 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ase the journal 
133fb 66 69 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20  file is.** just 
133fc 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73  deleted using Os
133fd 44 65 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73  Delete, *pExists
133fe 20 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64   is set to 0 and
133ff 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73   SQLITE_OK.** is
13400 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
13401 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
13402 65 73 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20  es not check if 
13403 74 68 65 72 65 20 69 73 20 61 20 6d 61 73 74 65  there is a maste
13404 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61  r journal filena
13405 6d 65 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64  me.** at the end
13406 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66   of the file. If
13407 20 74 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74   there is, and t
13408 68 61 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  hat master journ
13409 61 6c 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20  al file.** does 
1340a 6e 6f 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20  not exist, then 
1340b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1340c 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68   is not really h
1340d 6f 74 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63  ot. In this.** c
1340e 61 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ase this routine
1340f 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66   will return a f
13410 61 6c 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54  alse-positive. T
13411 68 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63  he pager_playbac
13412 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77  k().** routine w
13413 69 6c 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61  ill discover tha
13414 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  t the journal fi
13415 6c 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79  le is not really
13416 20 68 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c   hot and .** wil
13417 6c 20 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61  l not roll it ba
13418 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ck. .**.** If a 
13419 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  hot-journal file
1341a 20 69 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69   is found to exi
1341b 73 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20  st, *pExists is 
1341c 73 65 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a  set to 1 and .**
1341d 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
1341e 6e 65 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a  ned. If no hot-j
1341f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70  ournal file is p
13420 72 65 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73  resent, *pExists
13421 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20   is.** set to 0 
13422 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65  and SQLITE_OK re
13423 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f  turned. If an IO
13424 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68   error occurs wh
13425 69 6c 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f  ile trying.** to
13426 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   determine wheth
13427 65 72 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d  er or not a hot-
13428 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69  journal file exi
13429 73 74 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f  sts, the IO erro
1342a 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74  r.** code is ret
1342b 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61  urned and the va
1342c 6c 75 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20  lue of *pExists 
1342d 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
1342e 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 48  .static int hasH
1342f 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20  otJournal(Pager 
13430 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45  *pPager, int *pE
13431 78 69 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65  xists){.  sqlite
13432 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56  3_vfs * const pV
13433 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66  fs = pPager->pVf
13434 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  s;.  int rc;    
13435 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13436 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
13437 65 20 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74  e */.  int exist
13438 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
13439 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1343a 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  a journal file i
1343b 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20  s present */..  
1343c 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d  assert( pPager!=
1343d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1343e 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61  Pager->useJourna
1343f 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  l );.  assert( i
13440 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
13441 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ) );.  assert( !
13442 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
13443 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  fd) );.  assert(
13444 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3c   pPager->state <
13445 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 29  = PAGER_SHARED )
13446 3b 0a 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20  ;..  *pExists = 
13447 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  0;.  rc = sqlite
13448 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20  3OsAccess(pVfs, 
13449 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
1344a 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  , SQLITE_ACCESS_
1344b 45 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29  EXISTS, &exists)
1344c 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1344d 54 45 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20  TE_OK && exists 
1344e 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65  ){.    int locke
1344f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
13450 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f     /* True if so
13451 6d 65 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  me process holds
13452 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
13453 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 61 63 65   */..    /* Race
13454 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 3a   condition here:
13455 20 20 41 6e 6f 74 68 65 72 20 70 72 6f 63 65 73    Another proces
13456 73 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 65  s might have bee
13457 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20 20  n holding the.  
13458 20 20 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45    ** the RESERVE
13459 44 20 6c 6f 63 6b 20 61 6e 64 20 68 61 76 65 20  D lock and have 
1345a 61 20 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 20 61  a journal open a
1345b 74 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 41  t the sqlite3OsA
1345c 63 63 65 73 73 28 29 20 0a 20 20 20 20 2a 2a 20  ccess() .    ** 
1345d 63 61 6c 6c 20 61 62 6f 76 65 2c 20 62 75 74 20  call above, but 
1345e 74 68 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20  then delete the 
1345f 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 64 72 6f 70  journal and drop
13460 20 74 68 65 20 6c 6f 63 6b 20 62 65 66 6f 72 65   the lock before
13461 0a 20 20 20 20 2a 2a 20 77 65 20 67 65 74 20 74  .    ** we get t
13462 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
13463 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65  sqlite3OsCheckRe
13464 73 65 72 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c  servedLock() cal
13465 6c 2e 20 20 49 66 20 74 68 61 74 0a 20 20 20 20  l.  If that.    
13466 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  ** is the case, 
13467 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67  this routine mig
13468 68 74 20 74 68 69 6e 6b 20 74 68 65 72 65 20 69  ht think there i
13469 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  s a hot journal 
1346a 77 68 65 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 66  when.    ** in f
1346b 61 63 74 20 74 68 65 72 65 20 69 73 20 6e 6f 6e  act there is non
1346c 65 2e 20 20 54 68 69 73 20 72 65 73 75 6c 74 73  e.  This results
1346d 20 69 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69   in a false-posi
1346e 74 69 76 65 20 77 68 69 63 68 20 77 69 6c 6c 0a  tive which will.
1346f 20 20 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20      ** be dealt 
13470 77 69 74 68 20 62 79 20 74 68 65 20 70 6c 61 79  with by the play
13471 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 20 20 54  back routine.  T
13472 69 63 6b 65 74 20 23 33 38 38 33 2e 0a 20 20 20  icket #3883..   
13473 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
13474 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72  ite3OsCheckReser
13475 76 65 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e  vedLock(pPager->
13476 66 64 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20  fd, &locked);.  
13477 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13478 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29  _OK && !locked )
13479 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67  {.      int nPag
1347a 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65  e;..      /* Che
1347b 63 6b 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ck the size of t
1347c 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1347d 2e 20 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73  . If it consists
1347e 20 6f 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20   of 0 pages,.   
1347f 20 20 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74     ** then delet
13480 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
13481 6c 65 2e 20 53 65 65 20 74 68 65 20 68 65 61 64  le. See the head
13482 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65  er comment above
13483 20 66 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74   for .      ** t
13484 68 65 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72  he reasoning her
13485 65 2e 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f  e.  Delete the o
13486 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20  bsolete journal 
13487 66 69 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20  file under.     
13488 20 2a 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c   ** a RESERVED l
13489 6f 63 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63  ock to avoid rac
1348a 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64  e conditions and
1348b 20 74 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74   to avoid violat
1348c 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33  ing.      ** [H3
1348d 33 30 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a  3020]..      */.
1348e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1348f 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
13490 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  (pPager, &nPage)
13491 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
13492 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13493 20 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d       if( nPage==
13494 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
13495 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
13496 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
13497 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
13498 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66  OsLock(pPager->f
13499 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  d, RESERVED_LOCK
1349a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  )==SQLITE_OK ){.
1349b 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1349c 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
1349d 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  , pPager->zJourn
1349e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  al, 0);.        
1349f 20 20 20 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c      sqlite3OsUnl
134a0 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ock(pPager->fd, 
134a1 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SHARED_LOCK);.  
134a2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
134a3 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65      sqlite3EndBe
134a4 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
134a5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
134a6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f         /* The jo
134a7 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74  urnal file exist
134a8 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 63  s and no other c
134a9 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 20  onnection has a 
134aa 72 65 73 65 72 76 65 64 0a 20 20 20 20 20 20 20  reserved.       
134ab 20 20 20 2a 2a 20 6f 72 20 67 72 65 61 74 65 72     ** or greater
134ac 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
134ad 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77 20  abase file. Now 
134ae 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 65  check that there
134af 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   is.          **
134b0 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e 6f   at least one no
134b1 6e 2d 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20  n-zero bytes at 
134b2 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
134b3 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20   journal file.. 
134b4 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74           ** If t
134b5 68 65 72 65 20 69 73 2c 20 74 68 65 6e 20 77 65  here is, then we
134b6 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 20 6a   consider this j
134b7 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f 74  ournal to be hot
134b8 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20 20  . If not, .     
134b9 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 62       ** it can b
134ba 65 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20 20  e ignored..     
134bb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
134bc 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45    int f = SQLITE
134bd 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53  _OPEN_READONLY|S
134be 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
134bf 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20  JOURNAL;.       
134c0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
134c1 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67  sOpen(pVfs, pPag
134c2 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50  er->zJournal, pP
134c3 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66  ager->jfd, f, &f
134c4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
134c5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
134c6 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 38  {.            u8
134c7 20 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20   first = 0;.    
134c8 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
134c9 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65  ite3OsRead(pPage
134ca 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 64 20 2a 29  r->jfd, (void *)
134cb 26 66 69 72 73 74 2c 20 31 2c 20 30 29 3b 0a 20  &first, 1, 0);. 
134cc 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72             if( r
134cd 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
134ce 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20  SHORT_READ ){.  
134cf 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
134d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
134d1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
134d2 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
134d3 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  lose(pPager->jfd
134d4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  );.            *
134d5 70 45 78 69 73 74 73 20 3d 20 28 66 69 72 73 74  pExists = (first
134d6 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  !=0);.          
134d7 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
134d8 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 7b  LITE_CANTOPEN ){
134d9 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
134da 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70 65  If we cannot ope
134db 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  n the rollback j
134dc 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 6f  ournal file in o
134dd 72 64 65 72 20 74 6f 20 73 65 65 20 69 66 0a 20  rder to see if. 
134de 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74             ** it
134df 73 20 68 61 73 20 61 20 7a 65 72 6f 20 68 65 61  s has a zero hea
134e0 64 65 72 2c 20 74 68 61 74 20 6d 69 67 68 74 20  der, that might 
134e1 62 65 20 64 75 65 20 74 6f 20 61 6e 20 49 2f 4f  be due to an I/O
134e2 20 65 72 72 6f 72 2c 20 6f 72 0a 20 20 20 20 20   error, or.     
134e3 20 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67         ** it mig
134e4 68 74 20 62 65 20 64 75 65 20 74 6f 20 74 68 65  ht be due to the
134e5 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20   race condition 
134e6 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 20  described above 
134e7 61 6e 64 20 69 6e 0a 20 20 20 20 20 20 20 20 20  and in.         
134e8 20 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 33 38     ** ticket #38
134e9 38 33 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c  83.  Either way,
134ea 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65   assume that the
134eb 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e   journal is hot.
134ec 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
134ed 54 68 69 73 20 6d 69 67 68 74 20 62 65 20 61 20  This might be a 
134ee 66 61 6c 73 65 20 70 6f 73 69 74 69 76 65 2e 20  false positive. 
134ef 20 42 75 74 20 69 66 20 69 74 20 69 73 2c 20 74   But if it is, t
134f0 68 65 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20  hen the.        
134f1 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63      ** automatic
134f2 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 63   journal playbac
134f3 6b 20 61 6e 64 20 72 65 63 6f 76 65 72 79 20 6d  k and recovery m
134f4 65 63 68 61 6e 69 73 6d 20 77 69 6c 6c 20 64 65  echanism will de
134f5 61 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  al.            *
134f6 2a 20 77 69 74 68 20 69 74 20 75 6e 64 65 72 20  * with it under 
134f7 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
134f8 6b 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f  k where we do no
134f9 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20  t need to.      
134fa 20 20 20 20 20 20 2a 2a 20 77 6f 72 72 79 20 73        ** worry s
134fb 6f 20 6d 75 63 68 20 77 69 74 68 20 72 61 63 65  o much with race
134fc 20 63 6f 6e 64 69 74 69 6f 6e 73 2e 0a 20 20 20   conditions..   
134fd 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
134fe 20 20 20 20 20 20 20 20 2a 70 45 78 69 73 74 73          *pExists
134ff 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
13500 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
13501 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
13502 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
13503 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
13504 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
13505 20 52 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e   Read the conten
13506 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 20 6f  t for page pPg o
13507 75 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ut of the databa
13508 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 74 6f  se file and into
13509 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 61 74 61 2e   .** pPg->pData.
1350a 20 41 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f   A shared lock o
1350b 72 20 67 72 65 61 74 65 72 20 6d 75 73 74 20 62  r greater must b
1350c 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61  e held on the da
1350d 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 62  tabase.** file b
1350e 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
1350f 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ion is called..*
13510 2a 0a 2a 2a 20 49 66 20 70 61 67 65 20 31 20 69  *.** If page 1 i
13511 73 20 72 65 61 64 2c 20 74 68 65 6e 20 74 68 65  s read, then the
13512 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e   value of Pager.
13513 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20  dbFileVers[] is 
13514 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61  set to.** the va
13515 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68  lue read from th
13516 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
13517 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20  .**.** If an IO 
13518 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
13519 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20  en the IO error 
1351a 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
1351b 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 4f 74  he caller..** Ot
1351c 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f  herwise, SQLITE_
1351d 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
1351e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
1351f 61 64 44 62 50 61 67 65 28 50 67 48 64 72 20 2a  adDbPage(PgHdr *
13520 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  pPg){.  Pager *p
13521 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
13522 67 65 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62  ger; /* Pager ob
13523 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 20  ject associated 
13524 77 69 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f  with page pPg */
13525 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70  .  Pgno pgno = p
13526 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20  Pg->pgno;       
13527 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74  /* Page number t
13528 6f 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20  o read */.  int 
13529 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
1352a 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
1352b 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 36 34  rn code */.  i64
1352c 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
1352d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
1352e 65 20 6f 66 66 73 65 74 20 6f 66 20 66 69 6c 65  e offset of file
1352f 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f   to read from */
13530 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
13531 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
13532 5f 53 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44  _SHARED && !MEMD
13533 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  B );.  assert( i
13534 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
13535 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45  ) );..  if( NEVE
13536 52 28 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  R(!isOpen(pPager
13537 2d 3e 66 64 29 29 20 29 7b 0a 20 20 20 20 61 73  ->fd)) ){.    as
13538 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65  sert( pPager->te
13539 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65  mpFile );.    me
1353a 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c  mset(pPg->pData,
1353b 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65   0, pPager->page
1353c 53 69 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Size);.    retur
1353d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1353e 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67  .  iOffset = (pg
1353f 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65  no-1)*(i64)pPage
13540 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72  r->pageSize;.  r
13541 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
13542 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50  d(pPager->fd, pP
13543 67 2d 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72  g->pData, pPager
13544 2d 3e 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66  ->pageSize, iOff
13545 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  set);.  if( rc==
13546 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
13547 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72  RT_READ ){.    r
13548 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
13549 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31   }.  if( pgno==1
1354a 20 29 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69   ){.    u8 *dbFi
1354b 6c 65 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29  leVers = &((u8*)
1354c 70 50 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b  pPg->pData)[24];
1354d 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61  .    memcpy(&pPa
1354e 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c  ger->dbFileVers,
1354f 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a   dbFileVers, siz
13550 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69  eof(pPager->dbFi
13551 6c 65 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20  leVers));.  }.  
13552 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70  CODEC1(pPager, p
13553 50 67 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c  Pg->pData, pgno,
13554 20 33 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f   3, rc = SQLITE_
13555 4e 4f 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52  NOMEM);..  PAGER
13556 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61  _INCR(sqlite3_pa
13557 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74  ger_readdb_count
13558 29 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28  );.  PAGER_INCR(
13559 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a  pPager->nRead);.
1355a 20 20 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e    IOTRACE(("PGIN
1355b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   %p %d\n", pPage
1355c 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47  r, pgno));.  PAG
1355d 45 52 54 52 41 43 45 28 28 22 46 45 54 43 48 20  ERTRACE(("FETCH 
1355e 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28  %d page %d hash(
1355f 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20  %08x)\n",.      
13560 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44           PAGERID
13561 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20  (pPager), pgno, 
13562 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
13563 50 67 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e  Pg)));..  return
13564 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
13565 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
13566 61 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20  alled to obtain 
13567 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e  a shared lock on
13568 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13569 6c 65 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c  le..** It is ill
1356a 65 67 61 6c 20 74 6f 20 63 61 6c 6c 20 73 71 6c  egal to call sql
1356b 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
1356c 28 29 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74  () until after t
1356d 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
1356e 68 61 73 20 62 65 65 6e 20 73 75 63 63 65 73 73  has been success
1356f 66 75 6c 6c 79 20 63 61 6c 6c 65 64 2e 20 49 66  fully called. If
13570 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69   a shared-lock i
13571 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 77  s already held w
13572 68 65 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  hen.** this func
13573 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20  tion is called, 
13574 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  it is a no-op..*
13575 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
13576 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72  ng operations ar
13577 65 20 61 6c 73 6f 20 70 65 72 66 6f 72 6d 65 64  e also performed
13578 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
13579 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66  n..**.**   1) If
1357a 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 75   the pager is cu
1357b 72 72 65 6e 74 6c 79 20 69 6e 20 50 41 47 45 52  rrently in PAGER
1357c 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20 28 6e  _UNLOCK state (n
1357d 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a 20 20  o lock held.**  
1357e 20 20 20 20 6f 6e 20 74 68 65 20 64 61 74 61 62      on the datab
1357f 61 73 65 20 66 69 6c 65 29 2c 20 74 68 65 6e 20  ase file), then 
13580 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  an attempt is ma
13581 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a  de to obtain a.*
13582 2a 20 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f  *      SHARED lo
13583 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
13584 73 65 20 66 69 6c 65 2e 20 49 6d 6d 65 64 69 61  se file. Immedia
13585 74 65 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69  tely after obtai
13586 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65  ning.**      the
13587 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
13588 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 73  e file-system is
13589 20 63 68 65 63 6b 65 64 20 66 6f 72 20 61 20 68   checked for a h
1358a 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a 20 20  ot-journal,.**  
1358b 20 20 20 20 77 68 69 63 68 20 69 73 20 70 6c 61      which is pla
1358c 79 65 64 20 62 61 63 6b 20 69 66 20 70 72 65 73  yed back if pres
1358d 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 20 61  ent. Following a
1358e 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 0a  ny hot-journal .
1358f 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b  **      rollback
13590 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  , the contents o
13591 66 20 74 68 65 20 63 61 63 68 65 20 61 72 65 20  f the cache are 
13592 76 61 6c 69 64 61 74 65 64 20 62 79 20 63 68 65  validated by che
13593 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68  cking.**      th
13594 65 20 27 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  e 'change-counte
13595 72 27 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  r' field of the 
13596 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 65  database file he
13597 61 64 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20  ader and.**     
13598 20 64 69 73 63 61 72 64 65 64 20 69 66 20 74 68   discarded if th
13599 65 79 20 61 72 65 20 66 6f 75 6e 64 20 74 6f 20  ey are found to 
1359a 62 65 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 0a 2a  be invalid..**.*
1359b 2a 20 20 20 32 29 20 49 66 20 74 68 65 20 70 61  *   2) If the pa
1359c 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69  ger is running i
1359d 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65  n exclusive-mode
1359e 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20  , and there are 
1359f 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20  currently.**    
135a0 20 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67    no outstanding
135a1 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61   references to a
135a2 6e 79 20 70 61 67 65 73 2c 20 61 6e 64 20 69 73  ny pages, and is
135a3 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74   in the error st
135a4 61 74 65 2c 0a 2a 2a 20 20 20 20 20 20 74 68 65  ate,.**      the
135a5 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  n an attempt is 
135a6 6d 61 64 65 20 74 6f 20 63 6c 65 61 72 20 74 68  made to clear th
135a7 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 62 79  e error state by
135a8 20 64 69 73 63 61 72 64 69 6e 67 0a 2a 2a 20 20   discarding.**  
135a9 20 20 20 20 74 68 65 20 63 6f 6e 74 65 6e 74 73      the contents
135aa 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63   of the page cac
135ab 68 65 20 61 6e 64 20 72 6f 6c 6c 69 6e 67 20 62  he and rolling b
135ac 61 63 6b 20 61 6e 79 20 6f 70 65 6e 20 6a 6f 75  ack any open jou
135ad 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 66 69 6c  rnal.**      fil
135ae 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
135af 6f 70 65 72 61 74 69 6f 6e 20 64 65 73 63 72 69  operation descri
135b0 62 65 64 20 62 79 20 28 32 29 20 61 62 6f 76 65  bed by (2) above
135b1 20 69 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65   is not attempte
135b2 64 2c 20 61 6e 64 20 69 66 20 74 68 65 0a 2a 2a  d, and if the.**
135b3 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20   pager is in an 
135b4 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65  error state othe
135b5 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55  r than SQLITE_FU
135b6 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 69 73 20  LL when this is 
135b7 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 65  called,.** the e
135b8 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72  rror state error
135b9 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
135ba 64 2e 20 49 74 20 69 73 20 70 65 72 6d 69 74 74  d. It is permitt
135bb 65 64 20 74 6f 20 72 65 61 64 20 74 68 65 0a 2a  ed to read the.*
135bc 2a 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 20  * database when 
135bd 69 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65  in SQLITE_FULL e
135be 72 72 6f 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a  rror state..**.*
135bf 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  * Otherwise, if 
135c0 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75  everything is su
135c1 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45  ccessful, SQLITE
135c2 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
135c3 20 49 66 20 61 6e 0a 2a 2a 20 49 4f 20 65 72 72   If an.** IO err
135c4 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
135c5 6c 6f 63 6b 69 6e 67 20 74 68 65 20 64 61 74 61  locking the data
135c6 62 61 73 65 2c 20 63 68 65 63 6b 69 6e 67 20 66  base, checking f
135c7 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  or a hot-journal
135c8 0a 2a 2a 20 66 69 6c 65 20 6f 72 20 72 6f 6c 6c  .** file or roll
135c9 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e  ing back a journ
135ca 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 49 4f 20  al file, the IO 
135cb 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
135cc 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
135cd 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
135ce 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64  lite3PagerShared
135cf 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67  Lock(Pager *pPag
135d0 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
135d1 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
135d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
135d3 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn code */.  in
135d4 74 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d  t isErrorReset =
135d5 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
135d6 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 63 6f   /* True if reco
135d7 76 65 72 69 6e 67 20 66 72 6f 6d 20 65 72 72 6f  vering from erro
135d8 72 20 73 74 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a  r state */..  /*
135d9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
135da 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f   only called fro
135db 6d 20 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e 6c  m b-tree and onl
135dc 79 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65  y when there are
135dd 20 6e 6f 0a 20 20 2a 2a 20 6f 75 74 73 74 61 6e   no.  ** outstan
135de 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 20 20  ding pages */.  
135df 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
135e0 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
135e1 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d  ager->pPCache)==
135e2 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  0 );.  if( NEVER
135e3 28 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72  (MEMDB && pPager
135e4 2d 3e 65 72 72 43 6f 64 65 29 20 29 7b 20 72 65  ->errCode) ){ re
135e5 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
135e6 43 6f 64 65 3b 20 7d 0a 0a 20 20 2f 2a 20 49 66  Code; }..  /* If
135e7 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 69   this database i
135e8 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 2d 73 74  s in an error-st
135e9 61 74 65 2c 20 6e 6f 77 20 69 73 20 61 20 63 68  ate, now is a ch
135ea 61 6e 63 65 20 74 6f 20 63 6c 65 61 72 0a 20 20  ance to clear.  
135eb 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 20 44 69  ** the error. Di
135ec 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e  scard the conten
135ed 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d  ts of the pager-
135ee 63 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 62 61  cache and rollba
135ef 63 6b 0a 20 20 2a 2a 20 61 6e 79 20 68 6f 74 20  ck.  ** any hot 
135f0 6a 6f 75 72 6e 61 6c 20 69 6e 20 74 68 65 20 66  journal in the f
135f1 69 6c 65 2d 73 79 73 74 65 6d 2e 0a 20 20 2a 2f  ile-system..  */
135f2 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  .  if( pPager->e
135f3 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 69 66  rrCode ){.    if
135f4 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
135f5 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  >jfd) || pPager-
135f6 3e 7a 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  >zJournal ){.   
135f7 20 20 20 69 73 45 72 72 6f 72 52 65 73 65 74 20     isErrorReset 
135f8 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = 1;.    }.    p
135f9 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d  Pager->errCode =
135fa 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
135fb 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67  pager_reset(pPag
135fc 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  er);.  }..  if( 
135fd 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
135fe 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 69  AGER_UNLOCK || i
135ff 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20  sErrorReset ){. 
13600 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a     sqlite3_vfs *
13601 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50   const pVfs = pP
13602 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 20 20  ager->pVfs;.    
13603 69 6e 74 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c  int isHotJournal
13604 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
13605 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20  ( !MEMDB );.    
13606 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
13607 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
13608 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d  ager->pPCache)==
13609 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  0 );.    if( pPa
1360a 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20  ger->noReadlock 
1360b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1360c 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c   pPager->readOnl
1360d 79 20 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65  y );.      pPage
1360e 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
1360f 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 7d 65 6c  _SHARED;.    }el
13610 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70  se{.      rc = p
13611 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63  ager_wait_on_loc
13612 6b 28 70 50 61 67 65 72 2c 20 53 48 41 52 45 44  k(pPager, SHARED
13613 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66  _LOCK);.      if
13614 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13615 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
13616 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
13617 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29  ==PAGER_UNLOCK )
13618 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
13619 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
1361a 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20  ger, rc);.      
1361b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
1361c 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
1361d 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e>=SHARED_LOCK )
1361e 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6a  ;..    /* If a j
1361f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73  ournal file exis
13620 74 73 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73  ts, and there is
13621 20 6e 6f 20 52 45 53 45 52 56 45 44 20 6c 6f 63   no RESERVED loc
13622 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  k on the.    ** 
13623 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
13624 68 65 6e 20 69 74 20 65 69 74 68 65 72 20 6e 65  hen it either ne
13625 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64  eds to be played
13626 20 62 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 64   back or deleted
13627 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
13628 20 21 69 73 45 72 72 6f 72 52 65 73 65 74 20 29   !isErrorReset )
13629 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1362a 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d  pPager->state <=
1362b 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b   PAGER_SHARED );
1362c 0a 20 20 20 20 20 20 72 63 20 3d 20 68 61 73 48  .      rc = hasH
1362d 6f 74 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  otJournal(pPager
1362e 2c 20 26 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 29  , &isHotJournal)
1362f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
13630 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13631 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
13632 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
13633 20 20 20 20 69 66 28 20 69 73 45 72 72 6f 72 52      if( isErrorR
13634 65 73 65 74 20 7c 7c 20 69 73 48 6f 74 4a 6f 75  eset || isHotJou
13635 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a  rnal ){.      /*
13636 20 47 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56   Get an EXCLUSIV
13637 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  E lock on the da
13638 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20  tabase file. At 
13639 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73  this point it is
1363a 0a 20 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74  .      ** import
1363b 61 6e 74 20 74 68 61 74 20 61 20 52 45 53 45 52  ant that a RESER
1363c 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20  VED lock is not 
1363d 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20  obtained on the 
1363e 77 61 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20  way to the.     
1363f 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   ** EXCLUSIVE lo
13640 63 6b 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20  ck. If it were, 
13641 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  another process 
13642 6d 69 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20  might open the. 
13643 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65       ** database
13644 20 66 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68   file, detect th
13645 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c  e RESERVED lock,
13646 20 61 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68   and conclude th
13647 61 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  at the.      ** 
13648 64 61 74 61 62 61 73 65 20 69 73 20 73 61 66 65  database is safe
13649 20 74 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74   to read while t
1364a 68 69 73 20 70 72 6f 63 65 73 73 20 69 73 20 73  his process is s
1364b 74 69 6c 6c 20 72 6f 6c 6c 69 6e 67 20 74 68 65  till rolling the
1364c 20 0a 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a   .      ** hot-j
1364d 6f 75 72 6e 61 6c 20 62 61 63 6b 2e 0a 20 20 20  ournal back..   
1364e 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20     ** .      ** 
1364f 42 65 63 61 75 73 65 20 74 68 65 20 69 6e 74 65  Because the inte
13650 72 6d 65 64 69 61 74 65 20 52 45 53 45 52 56 45  rmediate RESERVE
13651 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65  D lock is not re
13652 71 75 65 73 74 65 64 2c 20 61 6e 79 0a 20 20 20  quested, any.   
13653 20 20 20 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63     ** other proc
13654 65 73 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74  ess attempting t
13655 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74  o access the dat
13656 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20  abase file will 
13657 67 65 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a  get to .      **
13658 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 74   this point in t
13659 68 65 20 63 6f 64 65 20 61 6e 64 20 66 61 69 6c  he code and fail
1365a 20 74 6f 20 6f 62 74 61 69 6e 20 69 74 73 20 6f   to obtain its o
1365b 77 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  wn EXCLUSIVE loc
1365c 6b 20 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74  k .      ** on t
1365d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1365e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1365f 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
13660 74 65 3c 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  te<EXCLUSIVE_LOC
13661 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
13662 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  = sqlite3OsLock(
13663 70 50 61 67 65 72 2d 3e 66 64 2c 20 45 58 43 4c  pPager->fd, EXCL
13664 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20  USIVE_LOCK);.   
13665 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13666 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13667 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65      rc = pager_e
13668 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
13669 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
1366a 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20   failed;.       
1366b 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 67 65   }.        pPage
1366c 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
1366d 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 20  _EXCLUSIVE;.    
1366e 20 20 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20 4f    }. .      /* O
1366f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  pen the journal 
13670 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 61  for read/write a
13671 63 63 65 73 73 2e 20 54 68 69 73 20 69 73 20 62  ccess. This is b
13672 65 63 61 75 73 65 20 69 6e 20 0a 20 20 20 20 20  ecause in .     
13673 20 2a 2a 20 65 78 63 6c 75 73 69 76 65 2d 61 63   ** exclusive-ac
13674 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 66 69  cess mode the fi
13675 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 69  le descriptor wi
13676 6c 6c 20 62 65 20 6b 65 70 74 20 6f 70 65 6e 20  ll be kept open 
13677 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 70 6f 73  and.      ** pos
13678 73 69 62 6c 79 20 75 73 65 64 20 66 6f 72 20 61  sibly used for a
13679 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61 74   transaction lat
1367a 65 72 20 6f 6e 2e 20 4f 6e 20 73 6f 6d 65 20 73  er on. On some s
1367b 79 73 74 65 6d 73 2c 20 74 68 65 0a 20 20 20 20  ystems, the.    
1367c 20 20 2a 2a 20 4f 73 54 72 75 6e 63 61 74 65 28    ** OsTruncate(
1367d 29 20 63 61 6c 6c 20 75 73 65 64 20 69 6e 20 65  ) call used in e
1367e 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20  xclusive-access 
1367f 6d 6f 64 65 20 61 6c 73 6f 20 72 65 71 75 69 72  mode also requir
13680 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 72 65  es.      ** a re
13681 61 64 2f 77 72 69 74 65 20 66 69 6c 65 20 68 61  ad/write file ha
13682 6e 64 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ndle..      */. 
13683 20 20 20 20 20 69 66 28 20 21 69 73 4f 70 65 6e       if( !isOpen
13684 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
13685 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 73  .        int res
13686 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
13687 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70  qlite3OsAccess(p
13688 56 66 73 2c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  Vfs,pPager->zJou
13689 72 6e 61 6c 2c 53 51 4c 49 54 45 5f 41 43 43 45  rnal,SQLITE_ACCE
1368a 53 53 5f 45 58 49 53 54 53 2c 26 72 65 73 29 3b  SS_EXISTS,&res);
1368b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
1368c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1368d 20 20 20 20 20 20 20 20 69 66 28 20 72 65 73 20          if( res 
1368e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
1368f 6e 74 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20  nt fout = 0;.   
13690 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d           int f =
13691 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
13692 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
13693 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b  EN_MAIN_JOURNAL;
13694 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
13695 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65  ert( !pPager->te
13696 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20  mpFile );.      
13697 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
13698 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70  e3OsOpen(pVfs, p
13699 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
1369a 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c   pPager->jfd, f,
1369b 20 26 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20   &fout);.       
1369c 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21       assert( rc!
1369d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73  =SQLITE_OK || is
1369e 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
1369f 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ) );.           
136a0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
136a1 4f 4b 20 26 26 20 66 6f 75 74 26 53 51 4c 49 54  OK && fout&SQLIT
136a2 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20  E_OPEN_READONLY 
136a3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
136a4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e   rc = SQLITE_CAN
136a5 54 4f 50 45 4e 3b 0a 20 20 20 20 20 20 20 20 20  TOPEN;.         
136a6 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c       sqlite3OsCl
136a7 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  ose(pPager->jfd)
136a8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
136a9 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
136aa 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
136ab 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 64  If the journal d
136ac 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 69  oes not exist, i
136ad 74 20 75 73 75 61 6c 6c 79 20 6d 65 61 6e 73 20  t usually means 
136ae 74 68 61 74 20 73 6f 6d 65 20 0a 20 20 20 20 20  that some .     
136af 20 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20         ** other 
136b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67  connection manag
136b1 65 64 20 74 6f 20 67 65 74 20 69 6e 20 61 6e 64  ed to get in and
136b2 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 20 62 65   roll it back be
136b3 66 6f 72 65 20 0a 20 20 20 20 20 20 20 20 20 20  fore .          
136b4 20 20 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63    ** this connec
136b5 74 69 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68  tion obtained th
136b6 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
136b7 20 61 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a   above. Or, it .
136b8 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d              ** m
136b9 61 79 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65  ay mean that the
136ba 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 74 68   pager was in th
136bb 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68  e error-state wh
136bc 65 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  en this.        
136bd 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20      ** function 
136be 77 61 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 74  was called and t
136bf 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
136c0 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20  does not exist. 
136c1 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
136c2 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74  rc = pager_end_t
136c3 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65  ransaction(pPage
136c4 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  r, 0);.         
136c5 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
136c6 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
136c7 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
136c8 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69          goto fai
136c9 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  led;.      }..  
136ca 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 79      /* TODO: Why
136cb 20 61 72 65 20 74 68 65 73 65 20 63 6c 65 61 72   are these clear
136cc 65 64 20 68 65 72 65 3f 20 49 73 20 69 74 20 6e  ed here? Is it n
136cd 65 63 65 73 73 61 72 79 3f 20 2a 2f 0a 20 20 20  ecessary? */.   
136ce 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
136cf 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20  alStarted = 0;. 
136d0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
136d1 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20  rnalOff = 0;.   
136d2 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61     pPager->setMa
136d3 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  ster = 0;.      
136d4 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
136d5 64 72 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20 20  dr = 0;. .      
136d6 2f 2a 20 50 6c 61 79 62 61 63 6b 20 61 6e 64 20  /* Playback and 
136d7 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e  delete the journ
136d8 61 6c 2e 20 20 44 72 6f 70 20 74 68 65 20 64 61  al.  Drop the da
136d9 74 61 62 61 73 65 20 77 72 69 74 65 0a 20 20 20  tabase write.   
136da 20 20 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 72     ** lock and r
136db 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65 61  eacquire the rea
136dc 64 20 6c 6f 63 6b 2e 20 50 75 72 67 65 20 74 68  d lock. Purge th
136dd 65 20 63 61 63 68 65 20 62 65 66 6f 72 65 0a 20  e cache before. 
136de 20 20 20 20 20 2a 2a 20 70 6c 61 79 69 6e 67 20       ** playing 
136df 62 61 63 6b 20 74 68 65 20 68 6f 74 2d 6a 6f 75  back the hot-jou
136e0 72 6e 61 6c 20 73 6f 20 74 68 61 74 20 77 65 20  rnal so that we 
136e1 64 6f 6e 27 74 20 65 6e 64 20 75 70 20 77 69 74  don't end up wit
136e2 68 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e  h.      ** an in
136e3 63 6f 6e 73 69 73 74 65 6e 74 20 63 61 63 68 65  consistent cache
136e4 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
136e5 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
136e6 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20  er->jfd) ){.    
136e7 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70      rc = pager_p
136e8 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20  layback(pPager, 
136e9 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  1);.        if( 
136ea 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
136eb 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
136ec 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
136ed 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20  er, rc);.       
136ee 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
136ef 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
136f0 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
136f1 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d  (pPager->state==
136f2 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20  PAGER_SHARED).  
136f3 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61           || (pPa
136f4 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
136f5 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74  de && pPager->st
136f6 61 74 65 3e 50 41 47 45 52 5f 53 48 41 52 45 44  ate>PAGER_SHARED
136f7 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  ).      );.    }
136f8 0a 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  ..    if( pPager
136f9 2d 3e 70 42 61 63 6b 75 70 20 7c 7c 20 73 71 6c  ->pBackup || sql
136fa 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f  ite3PcachePageco
136fb 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
136fc 63 68 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20  che)>0 ){.      
136fd 2f 2a 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f  /* The shared-lo
136fe 63 6b 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ck has just been
136ff 20 61 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65   acquired on the
13700 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20   database file. 
13701 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72       ** and ther
13702 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 70 61  e are already pa
13703 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65  ges in the cache
13704 20 28 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75   (from a previou
13705 73 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20  s.      ** read 
13706 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  or write transac
13707 74 69 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f  tion).  Check to
13708 20 73 65 65 20 69 66 20 74 68 65 20 64 61 74 61   see if the data
13709 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61  base.      ** ha
1370a 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e  s been modified.
1370b 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73    If the databas
1370c 65 20 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66  e has changed, f
1370d 6c 75 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a  lush the.      *
1370e 2a 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a  * cache..      *
1370f 2a 0a 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62  *.      ** Datab
13710 61 73 65 20 63 68 61 6e 67 65 73 20 69 73 20 64  ase changes is d
13711 65 74 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69  etected by looki
13712 6e 67 20 61 74 20 31 35 20 62 79 74 65 73 20 62  ng at 15 bytes b
13713 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a  eginning.      *
13714 2a 20 61 74 20 6f 66 66 73 65 74 20 32 34 20 69  * at offset 24 i
13715 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54  nto the file.  T
13716 68 65 20 66 69 72 73 74 20 34 20 6f 66 20 74 68  he first 4 of th
13717 65 73 65 20 31 36 20 62 79 74 65 73 20 61 72 65  ese 16 bytes are
13718 0a 20 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62  .      ** a 32-b
13719 69 74 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20  it counter that 
1371a 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
1371b 69 74 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e  ith each change.
1371c 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f    The.      ** o
1371d 74 68 65 72 20 62 79 74 65 73 20 63 68 61 6e 67  ther bytes chang
1371e 65 20 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20  e randomly with 
1371f 65 61 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65  each file change
13720 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61   when.      ** a
13721 20 63 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65   codec is in use
13722 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20  ..      ** .    
13723 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20    ** There is a 
13724 76 61 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c  vanishingly smal
13725 6c 20 63 68 61 6e 63 65 20 74 68 61 74 20 61 20  l chance that a 
13726 63 68 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20  change will not 
13727 62 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74  be .      ** det
13728 65 63 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e  ected.  The chan
13729 63 65 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63  ce of an undetec
1372a 74 65 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f  ted change is so
1372b 20 73 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20   small that.    
1372c 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e    ** it can be n
1372d 65 67 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20  eglected..      
1372e 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 64 62  */.      char db
1372f 46 69 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28  FileVers[sizeof(
13730 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65  pPager->dbFileVe
13731 72 73 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69  rs)];.      sqli
13732 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
13733 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20  t(pPager, 0);.. 
13734 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d       if( pPager-
13735 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
13736 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d      rc = pPager-
13737 3e 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20  >errCode;.      
13738 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20    goto failed;. 
13739 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73       }..      as
1373a 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62  sert( pPager->db
1373b 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20  SizeValid );.   
1373c 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64     if( pPager->d
1373d 62 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20  bSize>0 ){.     
1373e 20 20 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56     IOTRACE(("CKV
1373f 45 52 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50  ERS %p %d\n", pP
13740 61 67 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46  ager, sizeof(dbF
13741 69 6c 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20  ileVers)));.    
13742 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13743 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66  OsRead(pPager->f
13744 64 2c 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20  d, &dbFileVers, 
13745 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72  sizeof(dbFileVer
13746 73 29 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20  s), 24);.       
13747 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13748 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
13749 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
1374a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1374b 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  se{.        mems
1374c 65 74 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30  et(dbFileVers, 0
1374d 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56  , sizeof(dbFileV
1374e 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  ers));.      }..
1374f 20 20 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70        if( memcmp
13750 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56  (pPager->dbFileV
13751 65 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c  ers, dbFileVers,
13752 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65   sizeof(dbFileVe
13753 72 73 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  rs))!=0 ){.     
13754 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70     pager_reset(p
13755 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  Pager);.      }.
13756 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
13757 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73  ( pPager->exclus
13758 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65  iveMode || pPage
13759 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  r->state==PAGER_
1375a 53 48 41 52 45 44 20 29 3b 0a 20 20 7d 0a 0a 20  SHARED );.  }.. 
1375b 66 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63  failed:.  if( rc
1375c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1375d 20 20 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f     /* pager_unlo
1375e 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20  ck() is a no-op 
1375f 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f  for exclusive mo
13760 64 65 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79  de and in-memory
13761 20 64 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20   databases. */. 
13762 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28     pager_unlock(
13763 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72  pPager);.  }.  r
13764 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
13765 2a 2a 20 49 66 20 74 68 65 20 72 65 66 65 72 65  ** If the refere
13766 6e 63 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65  nce count has re
13767 61 63 68 65 64 20 7a 65 72 6f 2c 20 72 6f 6c 6c  ached zero, roll
13768 62 61 63 6b 20 61 6e 79 20 61 63 74 69 76 65 0a  back any active.
13769 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  ** transaction a
1376a 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61  nd unlock the pa
1376b 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70  ger..**.** Excep
1376c 74 2c 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f  t, in locking_mo
1376d 64 65 3d 45 58 43 4c 55 53 49 56 45 20 77 68 65  de=EXCLUSIVE whe
1376e 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69  n there is nothi
1376f 6e 67 20 74 6f 20 69 6e 0a 2a 2a 20 74 68 65 20  ng to in.** the 
13770 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
13771 2c 20 74 68 65 20 75 6e 6c 6f 63 6b 20 69 73 20  , the unlock is 
13772 6e 6f 74 20 70 65 72 66 6f 72 6d 65 64 20 61 6e  not performed an
13773 64 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f  d there is.** no
13774 74 68 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63  thing to rollbac
13775 6b 2c 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69  k, so this routi
13776 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
13777 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  / .static void p
13778 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73  agerUnlockIfUnus
13779 65 64 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ed(Pager *pPager
1377a 29 7b 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65  ){.  if( (sqlite
1377b 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
1377c 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
1377d 3d 3d 30 29 0a 20 20 20 26 26 20 28 21 70 50 61  ==0).   && (!pPa
1377e 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
1377f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f  de || pPager->jo
13780 75 72 6e 61 6c 4f 66 66 3e 30 29 20 0a 20 20 29  urnalOff>0) .  )
13781 7b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63  {.    pagerUnloc
13782 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61  kAndRollback(pPa
13783 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ger);.  }.}../*.
13784 2a 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 66  ** Acquire a ref
13785 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 6e  erence to page n
13786 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 70 61  umber pgno in pa
13787 67 65 72 20 70 50 61 67 65 72 20 28 61 20 70 61  ger pPager (a pa
13788 67 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20  ge.** reference 
13789 68 61 73 20 74 79 70 65 20 44 62 50 61 67 65 2a  has type DbPage*
1378a 29 2e 20 49 66 20 74 68 65 20 72 65 71 75 65 73  ). If the reques
1378b 74 65 64 20 72 65 66 65 72 65 6e 63 65 20 69 73  ted reference is
1378c 20 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c   .** successfull
1378d 79 20 6f 62 74 61 69 6e 65 64 2c 20 69 74 20 69  y obtained, it i
1378e 73 20 63 6f 70 69 65 64 20 74 6f 20 2a 70 70 50  s copied to *ppP
1378f 61 67 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f  age and SQLITE_O
13790 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  K returned..**.*
13791 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74  * If the request
13792 65 64 20 70 61 67 65 20 69 73 20 61 6c 72 65 61  ed page is alrea
13793 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  dy in the cache,
13794 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e   it is returned.
13795 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20   .** Otherwise, 
13796 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63  a new page objec
13797 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  t is allocated a
13798 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  nd populated wit
13799 68 20 64 61 74 61 0a 2a 2a 20 72 65 61 64 20 66  h data.** read f
1379a 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
1379b 20 66 69 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63   file. In some c
1379c 61 73 65 73 2c 20 74 68 65 20 70 63 61 63 68 65  ases, the pcache
1379d 20 6d 6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63   module may.** c
1379e 68 6f 6f 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c  hoose not to all
1379f 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  ocate a new page
137a0 20 6f 62 6a 65 63 74 20 61 6e 64 20 6d 61 79 20   object and may 
137a1 72 65 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e  reuse an existin
137a2 67 0a 2a 2a 20 6f 62 6a 65 63 74 20 77 69 74 68  g.** object with
137a3 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
137a4 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a  references..**.*
137a5 2a 20 54 68 65 20 65 78 74 72 61 20 64 61 74 61  * The extra data
137a6 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70   appended to a p
137a7 61 67 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e  age is always in
137a8 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72  itialized to zer
137a9 6f 73 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74  os the .** first
137aa 20 74 69 6d 65 20 61 20 70 61 67 65 20 69 73 20   time a page is 
137ab 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f  loaded into memo
137ac 72 79 2e 20 49 66 20 74 68 65 20 70 61 67 65 20  ry. If the page 
137ad 72 65 71 75 65 73 74 65 64 20 69 73 20 0a 2a 2a  requested is .**
137ae 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
137af 63 61 63 68 65 20 77 68 65 6e 20 74 68 69 73 20  cache when this 
137b0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
137b1 65 64 2c 20 74 68 65 6e 20 74 68 65 20 65 78 74  ed, then the ext
137b2 72 61 0a 2a 2a 20 64 61 74 61 20 69 73 20 6c 65  ra.** data is le
137b3 66 74 20 61 73 20 69 74 20 77 61 73 20 77 68 65  ft as it was whe
137b4 6e 20 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63  n the page objec
137b5 74 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e  t was last used.
137b6 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61  .**.** If the da
137b7 74 61 62 61 73 65 20 69 6d 61 67 65 20 69 73 20  tabase image is 
137b8 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65  smaller than the
137b9 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20   requested page 
137ba 6f 72 20 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d  or if a .** non-
137bb 7a 65 72 6f 20 76 61 6c 75 65 20 69 73 20 70 61  zero value is pa
137bc 73 73 65 64 20 61 73 20 74 68 65 20 6e 6f 43 6f  ssed as the noCo
137bd 6e 74 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20  ntent parameter 
137be 61 6e 64 20 74 68 65 20 0a 2a 2a 20 72 65 71 75  and the .** requ
137bf 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f  ested page is no
137c0 74 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64  t already stored
137c1 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74   in the cache, t
137c2 68 65 6e 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61  hen no .** actua
137c3 6c 20 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75  l disk read occu
137c4 72 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  rs. In this case
137c5 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67   the memory imag
137c6 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67  e of the .** pag
137c7 65 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  e is initialized
137c8 20 74 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a   to all zeros. .
137c9 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65  **.** If noConte
137ca 6e 74 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d  nt is true, it m
137cb 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20  eans that we do 
137cc 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 74  not care about t
137cd 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f  he contents.** o
137ce 66 20 74 68 65 20 70 61 67 65 2e 20 54 68 69 73  f the page. This
137cf 20 6f 63 63 75 72 73 20 69 6e 20 74 77 6f 20 73   occurs in two s
137d0 65 70 65 72 61 74 65 20 73 63 65 6e 61 72 69 6f  eperate scenario
137d1 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68  s:.**.**   a) Wh
137d2 65 6e 20 72 65 61 64 69 6e 67 20 61 20 66 72 65  en reading a fre
137d3 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65  e-list leaf page
137d4 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
137d5 73 65 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20  se, and.**.**   
137d6 62 29 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f  b) When a savepo
137d7 69 6e 74 20 69 73 20 62 65 69 6e 67 20 72 6f 6c  int is being rol
137d8 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 77 65 20  led back and we 
137d9 6e 65 65 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20  need to load.** 
137da 20 20 20 20 20 61 20 6e 65 77 20 70 61 67 65 20       a new page 
137db 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 20 74  into the cache t
137dc 6f 20 70 6f 70 75 6c 61 74 65 20 77 69 74 68 20  o populate with 
137dd 74 68 65 20 64 61 74 61 20 72 65 61 64 0a 2a 2a  the data read.**
137de 20 20 20 20 20 20 66 72 6f 6d 20 74 68 65 20 73        from the s
137df 61 76 65 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c  avepoint journal
137e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e  ..**.** If noCon
137e1 74 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68  tent is true, th
137e2 65 6e 20 74 68 65 20 64 61 74 61 20 72 65 74 75  en the data retu
137e3 72 6e 65 64 20 69 73 20 7a 65 72 6f 65 64 20 69  rned is zeroed i
137e4 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69  nstead of.** bei
137e5 6e 67 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  ng read from the
137e6 20 64 61 74 61 62 61 73 65 2e 20 41 64 64 69 74   database. Addit
137e7 69 6f 6e 61 6c 6c 79 2c 20 74 68 65 20 62 69 74  ionally, the bit
137e8 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a  s corresponding.
137e9 2a 2a 20 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61  ** to pgno in Pa
137ea 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28  ger.pInJournal (
137eb 62 69 74 76 65 63 20 6f 66 20 70 61 67 65 73 20  bitvec of pages 
137ec 61 6c 72 65 61 64 79 20 77 72 69 74 74 65 6e 20  already written 
137ed 74 6f 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61  to the.** journa
137ee 6c 20 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20  l file) and the 
137ef 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70  PagerSavepoint.p
137f0 49 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76  InSavepoint bitv
137f1 65 63 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a  ecs of any open.
137f2 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72  ** savepoints ar
137f3 65 20 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e  e set. This mean
137f4 73 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73  s if the page is
137f5 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 61   made writable a
137f6 74 20 61 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69  t any.** point i
137f7 6e 20 74 68 65 20 66 75 74 75 72 65 2c 20 75 73  n the future, us
137f8 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
137f9 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
137fa 29 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a  ), its contents.
137fb 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a  ** will not be j
137fc 6f 75 72 6e 61 6c 65 64 2e 20 54 68 69 73 20 73  ournaled. This s
137fd 61 76 65 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54  aves IO..**.** T
137fe 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d  he acquisition m
137ff 69 67 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65  ight fail for se
13800 76 65 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20  veral reasons.  
13801 49 6e 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a  In all cases,.**
13802 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
13803 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
13804 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61  turned and *ppPa
13805 67 65 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c  ge is set to NUL
13806 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  L..**.** See als
13807 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f  o sqlite3PagerLo
13808 6f 6b 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68  okup().  Both th
13809 69 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c  is routine and L
1380a 6f 6f 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a  ookup() attempt.
1380b 2a 2a 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67  ** to find a pag
1380c 65 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  e in the in-memo
1380d 72 79 20 63 61 63 68 65 20 66 69 72 73 74 2e 20  ry cache first. 
1380e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20   If the page is 
1380f 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69  not already.** i
13810 6e 20 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72  n memory, this r
13811 6f 75 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64  outine goes to d
13812 69 73 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69  isk to read it i
13813 6e 20 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70  n whereas Lookup
13814 28 29 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72  ().** just retur
13815 6e 73 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74  ns 0.  This rout
13816 69 6e 65 20 61 63 71 75 69 72 65 73 20 61 20 72  ine acquires a r
13817 65 61 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72  ead-lock the fir
13818 73 74 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61  st time it.** ha
13819 73 20 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c  s to go to disk,
1381a 20 61 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20   and could also 
1381b 70 6c 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20  playback an old 
1381c 6a 6f 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73  journal if neces
1381d 73 61 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c  sary..** Since L
1381e 6f 6f 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f  ookup() never go
1381f 65 73 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e  es to disk, it n
13820 65 76 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c  ever has to deal
13821 20 77 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f   with locks.** o
13822 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e  r journal files.
13823 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13824 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
13825 67 65 72 41 63 71 75 69 72 65 28 0a 20 20 50 61  gerAcquire(.  Pa
13826 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20  ger *pPager,    
13827 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f    /* The pager o
13828 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62  pen on the datab
13829 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67  ase file */.  Pg
1382a 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20  no pgno,        
1382b 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
1382c 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44   to fetch */.  D
1382d 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20  bPage **ppPage, 
1382e 20 20 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f     /* Write a po
1382f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67  inter to the pag
13830 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  e here */.  int 
13831 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20  noContent       
13832 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72  /* Do not bother
13833 20 72 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74   reading content
13834 20 66 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72   from disk if tr
13835 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ue */.){.  int r
13836 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b  c;.  PgHdr *pPg;
13837 0a 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 65  ..  assert( asse
13838 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70  rt_pager_state(p
13839 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65  Pager) );.  asse
1383a 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
1383b 65 3e 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29  e>PAGER_UNLOCK )
1383c 3b 0a 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30  ;..  if( pgno==0
1383d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1383e 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1383f 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  PT;.  }..  /* If
13840 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
13841 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
13842 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
13843 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a  r immediately. .
13844 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20    ** Otherwise, 
13845 72 65 71 75 65 73 74 20 74 68 65 20 70 61 67 65  request the page
13846 20 66 72 6f 6d 20 74 68 65 20 50 43 61 63 68 65   from the PCache
13847 20 6c 61 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28   layer. */.  if(
13848 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
13849 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  !=SQLITE_OK && p
1384a 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d  Pager->errCode!=
1384b 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20  SQLITE_FULL ){. 
1384c 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e     rc = pPager->
1384d 65 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65  errCode;.  }else
1384e 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1384f 65 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50  e3PcacheFetch(pP
13850 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
13851 67 6e 6f 2c 20 31 2c 20 70 70 50 61 67 65 29 3b  gno, 1, ppPage);
13852 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d  .  }..  if( rc!=
13853 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13854 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 63   /* Either the c
13855 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63  all to sqlite3Pc
13856 61 63 68 65 46 65 74 63 68 28 29 20 72 65 74 75  acheFetch() retu
13857 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 20 6f 72  rned an error or
13858 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65   the.    ** page
13859 72 20 77 61 73 20 61 6c 72 65 61 64 79 20 69 6e  r was already in
1385a 20 74 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65   the error-state
1385b 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
1385c 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a  ion was called..
1385d 20 20 20 20 2a 2a 20 53 65 74 20 70 50 67 20 74      ** Set pPg t
1385e 6f 20 30 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20  o 0 and jump to 
1385f 74 68 65 20 65 78 63 65 70 74 69 6f 6e 20 68 61  the exception ha
13860 6e 64 6c 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70  ndler.  */.    p
13861 50 67 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f  Pg = 0;.    goto
13862 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65   pager_acquire_e
13863 72 72 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rr;.  }.  assert
13864 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e  ( (*ppPage)->pgn
13865 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73  o==pgno );.  ass
13866 65 72 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e  ert( (*ppPage)->
13867 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c  pPager==pPager |
13868 7c 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61  | (*ppPage)->pPa
13869 67 65 72 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28  ger==0 );..  if(
1386a 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67   (*ppPage)->pPag
1386b 65 72 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20  er ){.    /* In 
1386c 74 68 69 73 20 63 61 73 65 20 74 68 65 20 70 63  this case the pc
1386d 61 63 68 65 20 61 6c 72 65 61 64 79 20 63 6f 6e  ache already con
1386e 74 61 69 6e 73 20 61 6e 20 69 6e 69 74 69 61 6c  tains an initial
1386f 69 7a 65 64 20 63 6f 70 79 20 6f 66 0a 20 20 20  ized copy of.   
13870 20 2a 2a 20 74 68 65 20 70 61 67 65 2e 20 52 65   ** the page. Re
13871 74 75 72 6e 20 77 69 74 68 6f 75 74 20 66 75 72  turn without fur
13872 74 68 65 72 20 61 64 6f 2e 20 20 2a 2f 0a 20 20  ther ado.  */.  
13873 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d    assert( pgno<=
13874 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 26  PAGER_MAX_PGNO &
13875 26 20 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a  & pgno!=PAGER_MJ
13876 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b  _PGNO(pPager) );
13877 0a 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28  .    PAGER_INCR(
13878 70 50 61 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20  pPager->nHit);. 
13879 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1387a 5f 4f 4b 3b 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20  _OK;..  }else{. 
1387b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20     /* The pager 
1387c 63 61 63 68 65 20 68 61 73 20 63 72 65 61 74 65  cache has create
1387d 64 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74  d a new page. It
1387e 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20  s content needs 
1387f 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e  to .    ** be in
13880 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20  itialized.  */. 
13881 20 20 20 69 6e 74 20 6e 4d 61 78 3b 0a 0a 20 20     int nMax;..  
13882 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61    PAGER_INCR(pPa
13883 67 65 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20  ger->nMiss);.   
13884 20 70 50 67 20 3d 20 2a 70 70 50 61 67 65 3b 0a   pPg = *ppPage;.
13885 20 20 20 20 70 50 67 2d 3e 70 50 61 67 65 72 20      pPg->pPager 
13886 3d 20 70 50 61 67 65 72 3b 0a 0a 20 20 20 20 2f  = pPager;..    /
13887 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61  * The maximum pa
13888 67 65 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33  ge number is 2^3
13889 31 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  1. Return SQLITE
1388a 5f 43 4f 52 52 55 50 54 20 69 66 20 61 20 70 61  _CORRUPT if a pa
1388b 67 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72  ge.    ** number
1388c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
1388d 69 73 2c 20 6f 72 20 74 68 65 20 75 6e 75 73 65  is, or the unuse
1388e 64 20 6c 6f 63 6b 69 6e 67 2d 70 61 67 65 2c 20  d locking-page, 
1388f 69 73 20 72 65 71 75 65 73 74 65 64 2e 20 2a 2f  is requested. */
13890 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 50 41  .    if( pgno>PA
13891 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20  GER_MAX_PGNO || 
13892 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50  pgno==PAGER_MJ_P
13893 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20  GNO(pPager) ){. 
13894 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
13895 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
13896 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f       goto pager_
13897 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20  acquire_err;.   
13898 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   }..    rc = sql
13899 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
1389a 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 4d 61 78  nt(pPager, &nMax
1389b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
1389c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1389d 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71    goto pager_acq
1389e 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a  uire_err;.    }.
1389f 0a 20 20 20 20 69 66 28 20 6e 4d 61 78 3c 28 69  .    if( nMax<(i
138a0 6e 74 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42  nt)pgno || MEMDB
138a1 20 7c 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b   || noContent ){
138a2 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e  .      if( pgno>
138a3 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29  pPager->mxPgno )
138a4 7b 0a 09 72 63 20 3d 20 53 51 4c 49 54 45 5f 46  {..rc = SQLITE_F
138a5 55 4c 4c 3b 0a 09 67 6f 74 6f 20 70 61 67 65 72  ULL;..goto pager
138a6 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20  _acquire_err;.  
138a7 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
138a8 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20  noContent ){.   
138a9 20 20 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20       /* Failure 
138aa 74 6f 20 73 65 74 20 74 68 65 20 62 69 74 73 20  to set the bits 
138ab 69 6e 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c  in the InJournal
138ac 20 62 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20   bit-vectors is 
138ad 62 65 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20  benign..        
138ae 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61  ** It merely mea
138af 6e 73 20 74 68 61 74 20 77 65 20 6d 69 67 68 74  ns that we might
138b0 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77   do some extra w
138b1 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61  ork to journal a
138b2 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67   .        ** pag
138b3 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  e that does not 
138b4 6e 65 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e  need to be journ
138b5 61 6c 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c  aled.  Neverthel
138b6 65 73 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20  ess, be sure .  
138b7 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74        ** to test
138b8 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20   the case where 
138b9 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f  a malloc error o
138ba 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69  ccurs while tryi
138bb 6e 67 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20  ng to set .     
138bc 20 20 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61     ** a bit in a
138bd 20 62 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20   bit vector..   
138be 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
138bf 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
138c0 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
138c1 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50      if( pgno<=pP
138c2 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
138c3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45   ){.          TE
138c4 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73  STONLY( rc = ) s
138c5 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
138c6 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
138c7 61 6c 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20  al, pgno);.     
138c8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
138c9 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
138ca 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
138cb 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72       TESTONLY( r
138cc 63 20 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70  c = ) addToSavep
138cd 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67  ointBitvecs(pPag
138ce 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20  er, pgno);.     
138cf 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d     testcase( rc=
138d0 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
138d1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
138d2 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
138d3 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
138d4 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
138d5 50 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50  Pg->pData, 0, pP
138d6 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
138d7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49  .      }.      I
138d8 4f 54 52 41 43 45 28 28 22 5a 45 52 4f 20 25 70  OTRACE(("ZERO %p
138d9 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
138da 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73  pgno));.    }els
138db 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
138dc 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50   pPg->pPager==pP
138dd 61 67 65 72 20 29 3b 0a 20 20 20 20 20 20 72 63  ager );.      rc
138de 20 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50   = readDbPage(pP
138df 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  g);.      if( rc
138e0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
138e1 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65         goto page
138e2 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20  r_acquire_err;. 
138e3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66       }.    }.#if
138e4 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
138e5 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e  _PAGES.    pPg->
138e6 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72  pageHash = pager
138e7 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a  _pagehash(pPg);.
138e8 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65  #endif.  }..  re
138e9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
138ea 0a 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65  .pager_acquire_e
138eb 72 72 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63  rr:.  assert( rc
138ec 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
138ed 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20   if( pPg ){.    
138ee 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f  sqlite3PcacheDro
138ef 70 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61  p(pPg);.  }.  pa
138f0 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65  gerUnlockIfUnuse
138f1 64 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70  d(pPager);..  *p
138f2 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 74  pPage = 0;.  ret
138f3 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
138f4 20 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20   Acquire a page 
138f5 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79  if it is already
138f6 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   in the in-memor
138f7 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20  y cache.  Do.** 
138f8 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67  not read the pag
138f9 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65  e from disk.  Re
138fa 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
138fb 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f  o the page,.** o
138fc 72 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20  r 0 if the page 
138fd 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e  is not in cache.
138fe 20 41 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20   Also, return 0 
138ff 69 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72  if the .** pager
13900 20 69 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c   is in PAGER_UNL
13901 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74  OCK state when t
13902 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
13903 63 61 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66  called,.** or if
13904 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
13905 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20   an error state 
13906 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54  other than SQLIT
13907 45 5f 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65  E_FULL..**.** Se
13908 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61  e also sqlite3Pa
13909 67 65 72 47 65 74 28 29 2e 20 20 54 68 65 20 64  gerGet().  The d
1390a 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
1390b 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
1390c 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67  * and sqlite3Pag
1390d 65 72 47 65 74 28 29 20 69 73 20 74 68 61 74 20  erGet() is that 
1390e 5f 67 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74  _get() will go t
1390f 6f 20 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72  o the disk and r
13910 65 61 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61  ead.** in the pa
13911 67 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69  ge if the page i
13912 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e  s not already in
13913 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f   cache.  This ro
13914 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73  utine.** returns
13915 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67   NULL if the pag
13916 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68  e is not in cach
13917 65 20 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49  e or if a disk I
13918 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73  /O error .** has
13919 20 65 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a   ever happened..
1391a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1391b 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65  E DbPage *sqlite
1391c 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67  3PagerLookup(Pag
1391d 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
1391e 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20   pgno){.  PgHdr 
1391f 2a 70 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65  *pPg = 0;.  asse
13920 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b  rt( pPager!=0 );
13921 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21  .  assert( pgno!
13922 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
13923 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 21  pPager->pPCache!
13924 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
13925 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3e 20  pPager->state > 
13926 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a  PAGER_UNLOCK );.
13927 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46    sqlite3PcacheF
13928 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43  etch(pPager->pPC
13929 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26  ache, pgno, 0, &
1392a 70 50 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  pPg);.  return p
1392b 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c  Pg;.}../*.** Rel
1392c 65 61 73 65 20 61 20 70 61 67 65 20 72 65 66 65  ease a page refe
1392d 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rence..**.** If 
1392e 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
1392f 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
13930 70 61 67 65 20 64 72 6f 70 20 74 6f 20 7a 65 72  page drop to zer
13931 6f 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 70  o, then the.** p
13932 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 20  age is added to 
13933 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 20 57  the LRU list.  W
13934 68 65 6e 20 61 6c 6c 20 72 65 66 65 72 65 6e 63  hen all referenc
13935 65 73 20 74 6f 20 61 6c 6c 20 70 61 67 65 73 0a  es to all pages.
13936 2a 2a 20 61 72 65 20 72 65 6c 65 61 73 65 64 2c  ** are released,
13937 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75   a rollback occu
13938 72 73 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  rs and the lock 
13939 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
1393a 69 73 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e 0a 2a  is.** removed..*
1393b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1393c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
1393d 65 72 55 6e 72 65 66 28 44 62 50 61 67 65 20 2a  erUnref(DbPage *
1393e 70 50 67 29 7b 0a 20 20 69 66 28 20 70 50 67 20  pPg){.  if( pPg 
1393f 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50  ){.    Pager *pP
13940 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
13941 65 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  er;.    sqlite3P
13942 63 61 63 68 65 52 65 6c 65 61 73 65 28 70 50 67  cacheRelease(pPg
13943 29 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f  );.    pagerUnlo
13944 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65  ckIfUnused(pPage
13945 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r);.  }.}../*.**
13946 20 49 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   If the main jou
13947 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 61 6c  rnal file has al
13948 72 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65  ready been opene
13949 64 2c 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  d, ensure that t
1394a 68 65 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61  he.** sub-journa
1394b 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 74  l file is open t
1394c 6f 6f 2e 20 49 66 20 74 68 65 20 6d 61 69 6e 20  oo. If the main 
1394d 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 6f  journal is not o
1394e 70 65 6e 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e  pen,.** this fun
1394f 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
13950 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
13951 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  K is returned if
13952 20 65 76 65 72 79 74 68 69 6e 67 20 67 6f 65 73   everything goes
13953 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c   according to pl
13954 61 6e 2e 20 0a 2a 2a 20 41 6e 20 53 51 4c 49 54  an. .** An SQLIT
13955 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f  E_IOERR_XXX erro
13956 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
13957 65 64 20 69 66 20 61 20 63 61 6c 6c 20 74 6f 20  ed if a call to 
13958 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  .** sqlite3OsOpe
13959 6e 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74  n() fails..*/.st
1395a 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62  atic int openSub
1395b 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70  Journal(Pager *p
1395c 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
1395d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1395e 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  if( isOpen(pPage
1395f 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 4f 70  r->jfd) && !isOp
13960 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29  en(pPager->sjfd)
13961 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67   ){.    if( pPag
13962 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
13963 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
13964 44 45 5f 4d 45 4d 4f 52 59 20 7c 7c 20 70 50 61  DE_MEMORY || pPa
13965 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72  ger->subjInMemor
13966 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  y ){.      sqlit
13967 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e  e3MemJournalOpen
13968 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a  (pPager->sjfd);.
13969 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1396a 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74   rc = pagerOpent
1396b 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61 67  emp(pPager, pPag
1396c 65 72 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 54 45  er->sjfd, SQLITE
1396d 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c  _OPEN_SUBJOURNAL
1396e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
1396f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
13970 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
13971 20 69 73 20 63 61 6c 6c 65 64 20 61 74 20 74 68   is called at th
13972 65 20 73 74 61 72 74 20 6f 66 20 65 76 65 72 79  e start of every
13973 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
13974 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  on..** There mus
13975 74 20 61 6c 72 65 61 64 79 20 62 65 20 61 20 52  t already be a R
13976 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55  ESERVED or EXCLU
13977 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
13978 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 69   database .** fi
13979 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  le when this rou
1397a 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
1397b 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 6a  **.** Open the j
1397c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20  ournal file for 
1397d 70 61 67 65 72 20 70 50 61 67 65 72 20 61 6e 64  pager pPager and
1397e 20 77 72 69 74 65 20 61 20 6a 6f 75 72 6e 61 6c   write a journal
1397f 20 68 65 61 64 65 72 0a 2a 2a 20 74 6f 20 74 68   header.** to th
13980 65 20 73 74 61 72 74 20 6f 66 20 69 74 2e 20 49  e start of it. I
13981 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69  f there are acti
13982 76 65 20 73 61 76 65 70 6f 69 6e 74 73 2c 20 6f  ve savepoints, o
13983 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  pen the sub-jour
13984 6e 61 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c 2e 20  nal.** as well. 
13985 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
13986 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20   only used when 
13987 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13988 20 69 73 20 62 65 69 6e 67 20 0a 2a 2a 20 6f 70   is being .** op
13989 65 6e 65 64 20 74 6f 20 77 72 69 74 65 20 61 20  ened to write a 
1398a 72 6f 6c 6c 62 61 63 6b 20 6c 6f 67 20 66 6f 72  rollback log for
1398b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20   a transaction. 
1398c 49 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 0a  It is not used .
1398d 2a 2a 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20  ** when opening 
1398e 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 66 69  a hot journal fi
1398f 6c 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61  le to roll it ba
13990 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ck..**.** If the
13991 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
13992 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 28 61   already open (a
13993 73 20 69 74 20 6d 61 79 20 62 65 20 69 6e 20 65  s it may be in e
13994 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 2c 0a  xclusive mode),.
13995 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  ** then this fun
13996 63 74 69 6f 6e 20 6a 75 73 74 20 77 72 69 74 65  ction just write
13997 73 20 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  s a journal head
13998 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  er to the start 
13999 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 72 65 61 64  of the.** alread
1399a 79 20 6f 70 65 6e 20 66 69 6c 65 2e 20 0a 2a 2a  y open file. .**
1399b 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e  .** Whether or n
1399c 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ot the journal f
1399d 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 62 79  ile is opened by
1399e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
1399f 74 68 65 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e  the.** Pager.pIn
139a0 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 73  Journal bitvec s
139a1 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f  tructure is allo
139a2 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  cated..**.** Ret
139a3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
139a4 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73   everything is s
139a5 75 63 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 72  uccessful. Other
139a6 77 69 73 65 2c 20 72 65 74 75 72 6e 20 0a 2a 2a  wise, return .**
139a7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
139a8 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20   the attempt to 
139a9 61 6c 6c 6f 63 61 74 65 20 50 61 67 65 72 2e 70  allocate Pager.p
139aa 49 6e 4a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 2c  InJournal fails,
139ab 20 6f 72 20 0a 2a 2a 20 61 6e 20 49 4f 20 65 72   or .** an IO er
139ac 72 6f 72 20 63 6f 64 65 20 69 66 20 6f 70 65 6e  ror code if open
139ad 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74  ing or writing t
139ae 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
139af 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  fails..*/.static
139b0 20 69 6e 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f   int pager_open_
139b1 6a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70  journal(Pager *p
139b2 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
139b3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
139b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139b5 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
139b6 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ode */.  sqlite3
139b7 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66  _vfs * const pVf
139b8 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73  s = pPager->pVfs
139b9 3b 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63  ;   /* Local cac
139ba 68 65 20 6f 66 20 76 66 73 20 70 6f 69 6e 74 65  he of vfs pointe
139bb 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  r */..  assert( 
139bc 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
139bd 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b  AGER_RESERVED );
139be 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
139bf 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b  r->useJournal );
139c0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
139c1 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d  r->journalMode!=
139c2 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
139c3 45 5f 4f 46 46 20 29 3b 0a 20 20 61 73 73 65 72  E_OFF );.  asser
139c4 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  t( pPager->pInJo
139c5 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20  urnal==0 );.  . 
139c6 20 2f 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69   /* If already i
139c7 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  n the error stat
139c8 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  e, this function
139c9 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75   is a no-op.  Bu
139ca 74 20 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 6f 74  t on.  ** the ot
139cb 68 65 72 20 68 61 6e 64 2c 20 74 68 69 73 20 72  her hand, this r
139cc 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20  outine is never 
139cd 63 61 6c 6c 65 64 20 69 66 20 77 65 20 61 72 65  called if we are
139ce 20 61 6c 72 65 61 64 79 20 69 6e 0a 20 20 2a 2a   already in.  **
139cf 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2e   an error state.
139d0 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28   */.  if( NEVER(
139d1 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29  pPager->errCode)
139d2 20 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 72   ) return pPager
139d3 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a  ->errCode;..  /*
139d4 20 54 4f 44 4f 3a 20 49 73 20 69 74 20 72 65 61   TODO: Is it rea
139d5 6c 6c 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  lly possible to 
139d6 67 65 74 20 68 65 72 65 20 77 69 74 68 20 64 62  get here with db
139d7 53 69 7a 65 56 61 6c 69 64 3d 3d 30 3f 20 49 66  SizeValid==0? If
139d8 20 6e 6f 74 2c 0a 20 20 2a 2a 20 74 68 65 20 63   not,.  ** the c
139d9 61 6c 6c 20 74 6f 20 50 61 67 65 72 50 61 67 65  all to PagerPage
139da 63 6f 75 6e 74 28 29 20 63 61 6e 20 62 65 20 72  count() can be r
139db 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 74  emoved..  */.  t
139dc 65 73 74 63 61 73 65 28 20 70 50 61 67 65 72 2d  estcase( pPager-
139dd 3e 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20  >dbSizeValid==0 
139de 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  );.  sqlite3Page
139df 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  rPagecount(pPage
139e0 72 2c 20 30 29 3b 0a 0a 20 20 70 50 61 67 65 72  r, 0);..  pPager
139e1 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73  ->pInJournal = s
139e2 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
139e3 74 65 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  te(pPager->dbSiz
139e4 65 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  e);.  if( pPager
139e5 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
139e6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
139e7 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
139e8 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a  .  /* Open the j
139e9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69  ournal file if i
139ea 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  t is not already
139eb 20 6f 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20   open. */.  if( 
139ec 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
139ed 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20  jfd) ){.    if( 
139ee 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
139ef 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
139f0 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b  ALMODE_MEMORY ){
139f1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  .      sqlite3Me
139f2 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61  mJournalOpen(pPa
139f3 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d  ger->jfd);.    }
139f4 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73  else{.      cons
139f5 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20  t int flags =   
139f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139f7 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20  /* VFS flags to 
139f8 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  open journal fil
139f9 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c  e */.        SQL
139fa 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
139fb 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  TE|SQLITE_OPEN_C
139fc 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28  REATE|.        (
139fd 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
139fe 20 3f 20 0a 20 20 20 20 20 20 20 20 20 20 28 53   ? .          (S
139ff 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
13a00 45 4f 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f  EONCLOSE|SQLITE_
13a01 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41  OPEN_TEMP_JOURNA
13a02 4c 29 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53  L):.          (S
13a03 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
13a04 4a 4f 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20  JOURNAL).       
13a05 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   );.#ifdef SQLIT
13a06 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
13a07 57 52 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d  WRITE.      rc =
13a08 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f   sqlite3JournalO
13a09 70 65 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70  pen(.          p
13a0a 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  Vfs, pPager->zJo
13a0b 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a  urnal, pPager->j
13a0c 66 64 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42  fd, flags, jrnlB
13a0d 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72  ufferSize(pPager
13a0e 29 0a 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65  ).      );.#else
13a0f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
13a10 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
13a11 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
13a12 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66  , pPager->jfd, f
13a13 6c 61 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66  lags, 0);.#endif
13a14 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
13a15 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
13a16 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65   || isOpen(pPage
13a17 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a  r->jfd) );.  }..
13a18 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  .  /* Write the 
13a19 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65  first journal he
13a1a 61 64 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72  ader to the jour
13a1b 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65  nal file and ope
13a1c 6e 20 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d  n .  ** the sub-
13a1d 6a 6f 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73  journal if neces
13a1e 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  sary..  */.  if(
13a1f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13a20 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43  {.    /* TODO: C
13a21 68 65 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74  heck if all of t
13a22 68 65 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20  hese are really 
13a23 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20  required. */.   
13a24 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53   pPager->dbOrigS
13a25 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62  ize = pPager->db
13a26 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72  Size;.    pPager
13a27 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
13a28 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
13a29 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  ->needSync = 0;.
13a2a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63      pPager->nRec
13a2b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
13a2c 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
13a2d 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65  ;.    pPager->se
13a2e 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20  tMaster = 0;.   
13a2f 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13a30 48 64 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20  Hdr = 0;.    rc 
13a31 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64  = writeJournalHd
13a32 72 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20  r(pPager);.  }. 
13a33 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13a34 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53  OK && pPager->nS
13a35 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20  avepoint ){.    
13a36 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72  rc = openSubJour
13a37 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  nal(pPager);.  }
13a38 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
13a39 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
13a3a 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
13a3b 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  y(pPager->pInJou
13a3c 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65  rnal);.    pPage
13a3d 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20  r->pInJournal = 
13a3e 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
13a3f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67  rc;.}../*.** Beg
13a40 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73  in a write-trans
13a41 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70  action on the sp
13a42 65 63 69 66 69 65 64 20 70 61 67 65 72 20 6f 62  ecified pager ob
13a43 6a 65 63 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77  ject. If a .** w
13a44 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
13a45 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
13a46 6e 20 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 66  n opened, this f
13a47 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d  unction is a no-
13a48 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  op..**.** If the
13a49 20 65 78 46 6c 61 67 20 61 72 67 75 6d 65 6e 74   exFlag argument
13a4a 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20   is false, then 
13a4b 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74  acquire at least
13a4c 20 61 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c   a RESERVED.** l
13a4d 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
13a4e 61 73 65 20 66 69 6c 65 2e 20 49 66 20 65 78 46  ase file. If exF
13a4f 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 65  lag is true, the
13a50 6e 20 61 63 71 75 69 72 65 20 61 74 20 6c 65 61  n acquire at lea
13a51 73 74 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49  st.** an EXCLUSI
13a52 56 45 20 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68  VE lock. If such
13a53 20 61 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61   a lock is alrea
13a54 64 79 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b  dy held, no lock
13a55 69 6e 67 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ing .** function
13a56 73 20 6e 65 65 64 20 62 65 20 63 61 6c 6c 65 64  s need be called
13a57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20  ..**.** If this 
13a58 69 73 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61  is not a tempora
13a59 72 79 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20  ry or in-memory 
13a5a 66 69 6c 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f  file and, the jo
13a5b 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a  urnal file is .*
13a5c 2a 20 6f 70 65 6e 65 64 20 69 66 20 69 74 20 68  * opened if it h
13a5d 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65  as not been alre
13a5e 61 64 79 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f  ady. For a tempo
13a5f 72 61 72 79 20 66 69 6c 65 2c 20 74 68 65 20 6f  rary file, the o
13a60 70 65 6e 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68  pening .** of th
13a61 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
13a62 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
13a63 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 63 74   there is an act
13a64 75 61 6c 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20  ual need to .** 
13a65 77 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75  write to the jou
13a66 72 6e 61 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20  rnal. TODO: Why 
13a67 68 61 6e 64 6c 65 20 74 65 6d 70 6f 72 61 72 79  handle temporary
13a68 20 66 69 6c 65 73 20 64 69 66 66 65 72 65 6e 74   files different
13a69 6c 79 3f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ly?.**.** If the
13a6a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
13a6b 20 6f 70 65 6e 65 64 20 28 6f 72 20 69 66 20 69   opened (or if i
13a6c 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65  t is already ope
13a6d 6e 29 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f  n), then a.** jo
13a6e 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20  urnal-header is 
13a6f 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73  written to the s
13a70 74 61 72 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a  tart of it..**.*
13a71 2a 20 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d  * If the subjInM
13a72 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69  emory argument i
13a73 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
13a74 20 61 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   any sub-journal
13a75 20 6f 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69   opened.** withi
13a76 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  n this transacti
13a77 6f 6e 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65  on will be opene
13a78 64 20 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  d as an in-memor
13a79 79 20 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20  y file. This.** 
13a7a 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66  has no effect if
13a7b 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
13a7c 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e   is already open
13a7d 65 64 20 28 61 73 20 69 74 20 6d 61 79 20 62 65  ed (as it may be
13a7e 20 77 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67   when.** running
13a7f 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
13a80 64 65 29 20 6f 72 20 69 66 20 74 68 65 20 74 72  de) or if the tr
13a81 61 6e 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e  ansaction does n
13a82 6f 74 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20  ot require a.** 
13a83 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20  sub-journal. If 
13a84 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79  the subjInMemory
13a85 20 61 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72   argument is zer
13a86 6f 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75  o, then any requ
13a87 69 72 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72  ired.** sub-jour
13a88 6e 61 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  nal is implement
13a89 65 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20  ed in-memory if 
13a8a 70 50 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d  pPager is an in-
13a8b 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c  memory database,
13a8c 20 0a 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20   .** or using a 
13a8d 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f  temporary file o
13a8e 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c  therwise..*/.SQL
13a8f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
13a90 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69  sqlite3PagerBegi
13a91 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  n(Pager *pPager,
13a92 20 69 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74   int exFlag, int
13a93 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a   subjInMemory){.
13a94 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
13a95 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
13a96 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50  pPager->state!=P
13a97 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20  AGER_UNLOCK );. 
13a98 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d   pPager->subjInM
13a99 65 6d 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a  emory = (u8)subj
13a9a 49 6e 4d 65 6d 6f 72 79 3b 0a 20 20 69 66 28 20  InMemory;.  if( 
13a9b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
13a9c 41 47 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20  AGER_SHARED ){. 
13a9d 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13a9e 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30  r->pInJournal==0
13a9f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
13aa0 21 4d 45 4d 44 42 20 26 26 20 21 70 50 61 67 65  !MEMDB && !pPage
13aa1 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a  r->tempFile );..
13aa2 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20      /* Obtain a 
13aa3 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e  RESERVED lock on
13aa4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13aa5 6c 65 2e 20 49 66 20 74 68 65 20 65 78 46 6c 61  le. If the exFla
13aa6 67 20 70 61 72 61 6d 65 74 65 72 0a 20 20 20 20  g parameter.    
13aa7 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  ** is true, then
13aa8 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 67   immediately upg
13aa9 72 61 64 65 20 74 68 69 73 20 74 6f 20 61 6e 20  rade this to an 
13aaa 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
13aab 54 68 65 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d  The.    ** busy-
13aac 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b  handler callback
13aad 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 68 65   can be used whe
13aae 6e 20 75 70 67 72 61 64 69 6e 67 20 74 6f 20 74  n upgrading to t
13aaf 68 65 20 45 58 43 4c 55 53 49 56 45 0a 20 20 20  he EXCLUSIVE.   
13ab0 20 2a 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f   ** lock, but no
13ab1 74 20 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67  t when obtaining
13ab2 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f   the RESERVED lo
13ab3 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  ck..    */.    r
13ab4 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  c = sqlite3OsLoc
13ab5 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45  k(pPager->fd, RE
13ab6 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SERVED_LOCK);.  
13ab7 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13ab8 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61  _OK ){.      pPa
13ab9 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47  ger->state = PAG
13aba 45 52 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20  ER_RESERVED;.   
13abb 20 20 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b     if( exFlag ){
13abc 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61  .        rc = pa
13abd 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b  ger_wait_on_lock
13abe 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49  (pPager, EXCLUSI
13abf 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  VE_LOCK);.      
13ac0 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
13ac1 49 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20  If the required 
13ac2 6c 6f 63 6b 73 20 77 65 72 65 20 73 75 63 63 65  locks were succe
13ac3 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64  ssfully obtained
13ac4 2c 20 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , open the journ
13ac5 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 61  al.    ** file a
13ac6 6e 64 20 77 72 69 74 65 20 74 68 65 20 66 69 72  nd write the fir
13ac7 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65  st journal-heade
13ac8 72 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a  r to it..    */.
13ac9 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
13aca 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d  TE_OK && pPager-
13acb 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41  >journalMode!=PA
13acc 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
13acd 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 72 63 20  OFF ){.      rc 
13ace 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75  = pager_open_jou
13acf 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20  rnal(pPager);.  
13ad0 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
13ad1 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
13ad2 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a  fd) && pPager->j
13ad3 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a  ournalOff==0 ){.
13ad4 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70      /* This happ
13ad5 65 6e 73 20 77 68 65 6e 20 74 68 65 20 70 61 67  ens when the pag
13ad6 65 72 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73  er was in exclus
13ad7 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20  ive-access mode 
13ad8 74 68 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20  the last.    ** 
13ad9 74 69 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20  time a (read or 
13ada 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69  write) transacti
13adb 6f 6e 20 77 61 73 20 73 75 63 63 65 73 73 66 75  on was successfu
13adc 6c 6c 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20  lly concluded.  
13add 20 20 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e    ** by this con
13ade 6e 65 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64  nection. Instead
13adf 20 6f 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65   of deleting the
13ae0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74   journal file it
13ae1 20 77 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70   was .    ** kep
13ae2 74 20 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65  t open and eithe
13ae3 72 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 20  r was truncated 
13ae4 74 6f 20 30 20 62 79 74 65 73 20 6f 72 20 69 74  to 0 bytes or it
13ae5 73 20 68 65 61 64 65 72 20 77 61 73 0a 20 20 20  s header was.   
13ae6 20 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20   ** overwritten 
13ae7 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20  with zeros..    
13ae8 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
13ae9 50 61 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29  Pager->nRec==0 )
13aea 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
13aeb 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
13aec 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
13aed 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  t( pPager->pInJo
13aee 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  urnal==0 );.    
13aef 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f  rc = pager_open_
13af0 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b  journal(pPager);
13af1 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41  .  }..  PAGERTRA
13af2 43 45 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e  CE(("TRANSACTION
13af3 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
13af4 70 50 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73  pPager)));.  ass
13af5 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61  ert( !isOpen(pPa
13af6 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61  ger->jfd) || pPa
13af7 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e  ger->journalOff>
13af8 30 20 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f  0 || rc!=SQLITE_
13af9 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  OK );.  if( rc!=
13afa 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13afb 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
13afc 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a  ->dbModified );.
13afd 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 61 6e      /* Ignore an
13afe 79 20 49 4f 20 65 72 72 6f 72 20 74 68 61 74 20  y IO error that 
13aff 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 70 61  occurs within pa
13b00 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
13b01 69 6f 6e 28 29 2e 20 54 68 65 0a 20 20 20 20 2a  ion(). The.    *
13b02 2a 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69  * purpose of thi
13b03 73 20 63 61 6c 6c 20 69 73 20 74 6f 20 72 65 73  s call is to res
13b04 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  et the internal 
13b05 73 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 67  state of the pag
13b06 65 72 0a 20 20 20 20 2a 2a 20 73 75 62 2d 73 79  er.    ** sub-sy
13b07 73 74 65 6d 2e 20 49 74 20 64 6f 65 73 6e 27 74  stem. It doesn't
13b08 20 6d 61 74 74 65 72 20 69 66 20 74 68 65 20 6a   matter if the j
13b09 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e  ournal-file is n
13b0a 6f 74 20 70 72 6f 70 65 72 6c 79 0a 20 20 20 20  ot properly.    
13b0b 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 20 61 74 20  ** finalized at 
13b0c 74 68 69 73 20 70 6f 69 6e 74 20 28 73 69 6e 63  this point (sinc
13b0d 65 20 69 74 20 69 73 20 6e 6f 74 20 61 20 76 61  e it is not a va
13b0e 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  lid journal file
13b0f 20 61 6e 79 77 61 79 29 2e 0a 20 20 20 20 2a 2f   anyway)..    */
13b10 0a 20 20 20 20 70 61 67 65 72 5f 65 6e 64 5f 74  .    pager_end_t
13b11 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65  ransaction(pPage
13b12 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  r, 0);.  }.  ret
13b13 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
13b14 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 64   Mark a single d
13b15 61 74 61 20 70 61 67 65 20 61 73 20 77 72 69 74  ata page as writ
13b16 65 61 62 6c 65 2e 20 54 68 65 20 70 61 67 65 20  eable. The page 
13b17 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  is written into 
13b18 74 68 65 20 0a 2a 2a 20 6d 61 69 6e 20 6a 6f 75  the .** main jou
13b19 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72  rnal or sub-jour
13b1a 6e 61 6c 20 61 73 20 72 65 71 75 69 72 65 64 2e  nal as required.
13b1b 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20   If the page is 
13b1c 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20  written into.** 
13b1d 6f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  one of the journ
13b1e 61 6c 73 2c 20 74 68 65 20 63 6f 72 72 65 73 70  als, the corresp
13b1f 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 20 73 65  onding bit is se
13b20 74 20 69 6e 20 74 68 65 20 0a 2a 2a 20 50 61 67  t in the .** Pag
13b21 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69  er.pInJournal bi
13b22 74 76 65 63 20 61 6e 64 20 74 68 65 20 50 61 67  tvec and the Pag
13b23 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53  erSavepoint.pInS
13b24 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73  avepoint bitvecs
13b25 0a 2a 2a 20 6f 66 20 61 6e 79 20 6f 70 65 6e 20  .** of any open 
13b26 73 61 76 65 70 6f 69 6e 74 73 20 61 73 20 61 70  savepoints as ap
13b27 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74  propriate..*/.st
13b28 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77  atic int pager_w
13b29 72 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 29  rite(PgHdr *pPg)
13b2a 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20  {.  void *pData 
13b2b 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20  = pPg->pData;.  
13b2c 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
13b2d 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69  pPg->pPager;.  i
13b2e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
13b2f 4b 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f  K;..  /* This ro
13b30 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 63 61 6c  utine is not cal
13b31 6c 65 64 20 75 6e 6c 65 73 73 20 61 20 74 72 61  led unless a tra
13b32 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72  nsaction has alr
13b33 65 61 64 79 20 62 65 65 6e 0a 20 20 2a 2a 20 73  eady been.  ** s
13b34 74 61 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61  tarted..  */.  a
13b35 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
13b36 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45  tate>=PAGER_RESE
13b37 52 56 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  RVED );..  /* If
13b38 20 61 6e 20 65 72 72 6f 72 20 68 61 73 20 62 65   an error has be
13b39 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 64 65  en previously de
13b3a 74 65 63 74 65 64 2c 20 77 65 20 73 68 6f 75 6c  tected, we shoul
13b3b 64 20 6e 6f 74 20 62 65 0a 20 20 2a 2a 20 63 61  d not be.  ** ca
13b3c 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
13b3d 6e 65 2e 20 20 52 65 70 65 61 74 20 74 68 65 20  ne.  Repeat the 
13b3e 65 72 72 6f 72 20 66 6f 72 20 72 6f 62 75 73 74  error for robust
13b3f 6e 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ness..  */.  if(
13b40 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65   NEVER(pPager->e
13b41 72 72 43 6f 64 65 29 20 29 20 20 72 65 74 75 72  rrCode) )  retur
13b42 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  n pPager->errCod
13b43 65 3b 0a 0a 20 20 2f 2a 20 48 69 67 68 65 72 2d  e;..  /* Higher-
13b44 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 20 6e  level routines n
13b45 65 76 65 72 20 63 61 6c 6c 20 74 68 69 73 20 66  ever call this f
13b46 75 6e 63 74 69 6f 6e 20 69 66 20 64 61 74 61 62  unction if datab
13b47 61 73 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20  ase is not.  ** 
13b48 77 72 69 74 61 62 6c 65 2e 20 20 42 75 74 20 63  writable.  But c
13b49 68 65 63 6b 20 61 6e 79 77 61 79 2c 20 6a 75 73  heck anyway, jus
13b4a 74 20 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73  t for robustness
13b4b 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52  . */.  if( NEVER
13b4c 28 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c  (pPager->readOnl
13b4d 79 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  y) ) return SQLI
13b4e 54 45 5f 50 45 52 4d 3b 0a 0a 20 20 61 73 73 65  TE_PERM;..  asse
13b4f 72 74 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74  rt( !pPager->set
13b50 4d 61 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45  Master );..  CHE
13b51 43 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20  CK_PAGE(pPg);.. 
13b52 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67   /* Mark the pag
13b53 65 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20  e as dirty.  If 
13b54 74 68 65 20 70 61 67 65 20 68 61 73 20 61 6c 72  the page has alr
13b55 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65  eady been writte
13b56 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f  n.  ** to the jo
13b57 75 72 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61  urnal then we ca
13b58 6e 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 61  n return right a
13b59 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  way..  */.  sqli
13b5a 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72  te3PcacheMakeDir
13b5b 74 79 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70  ty(pPg);.  if( p
13b5c 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67  ageInJournal(pPg
13b5d 29 20 26 26 20 21 73 75 62 6a 52 65 71 75 69 72  ) && !subjRequir
13b5e 65 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20  esPage(pPg) ){. 
13b5f 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64     pPager->dbMod
13b60 69 66 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c  ified = 1;.  }el
13b61 73 65 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77  se{..    /* If w
13b62 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20  e get this far, 
13b63 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
13b64 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20  e page needs to 
13b65 62 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65  be.    ** writte
13b66 6e 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63  n to the transac
13b67 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20  tion journal or 
13b68 74 68 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a  the ckeckpoint j
13b69 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72  ournal.    ** or
13b6a 20 62 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20   both..    **.  
13b6b 20 20 2a 2a 20 48 69 67 68 65 72 20 6c 65 76 65    ** Higher leve
13b6c 6c 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c  l routines shoul
13b6d 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 73  d have already s
13b6e 74 61 72 74 65 64 20 61 20 74 72 61 6e 73 61 63  tarted a transac
13b6f 74 69 6f 6e 2c 0a 20 20 20 20 2a 2a 20 77 68 69  tion,.    ** whi
13b70 63 68 20 6d 65 61 6e 73 20 74 68 65 79 20 68 61  ch means they ha
13b71 76 65 20 61 63 71 75 69 72 65 64 20 74 68 65 20  ve acquired the 
13b72 6e 65 63 65 73 73 61 72 79 20 6c 6f 63 6b 73 20  necessary locks 
13b73 61 6e 64 20 6f 70 65 6e 65 64 0a 20 20 20 20 2a  and opened.    *
13b74 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  * a rollback jou
13b75 72 6e 61 6c 2e 20 20 44 6f 75 62 6c 65 2d 63 68  rnal.  Double-ch
13b76 65 63 6b 20 74 6f 20 6d 61 6b 65 73 20 73 75 72  eck to makes sur
13b77 65 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  e this is the ca
13b78 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  se..    */.    r
13b79 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
13b7a 42 65 67 69 6e 28 70 50 61 67 65 72 2c 20 30 2c  Begin(pPager, 0,
13b7b 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d   pPager->subjInM
13b7c 65 6d 6f 72 79 29 3b 0a 20 20 20 20 69 66 28 20  emory);.    if( 
13b7d 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45  NEVER(rc!=SQLITE
13b7e 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65  _OK) ){.      re
13b7f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
13b80 20 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70     if( !isOpen(p
13b81 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70  Pager->jfd) && p
13b82 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
13b83 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de!=PAGER_JOURNA
13b84 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20  LMODE_OFF ){.   
13b85 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13b86 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b  r->useJournal );
13b87 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
13b88 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70  r_open_journal(p
13b89 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66  Pager);.      if
13b8a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13b8b 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
13b8c 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64   }.    pPager->d
13b8d 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 20  bModified = 1;. 
13b8e 20 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 72 61   .    /* The tra
13b8f 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c  nsaction journal
13b90 20 6e 6f 77 20 65 78 69 73 74 73 20 61 6e 64 20   now exists and 
13b91 77 65 20 68 61 76 65 20 61 20 52 45 53 45 52 56  we have a RESERV
13b92 45 44 20 6f 72 20 61 6e 0a 20 20 20 20 2a 2a 20  ED or an.    ** 
13b93 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
13b94 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
13b95 61 73 65 20 66 69 6c 65 2e 20 20 57 72 69 74 65  ase file.  Write
13b96 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67   the current pag
13b97 65 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  e to.    ** the 
13b98 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72  transaction jour
13b99 6e 61 6c 20 69 66 20 69 74 20 69 73 20 6e 6f 74  nal if it is not
13b9a 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a   there already..
13b9b 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
13b9c 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50  pageInJournal(pP
13b9d 67 29 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61  g) && isOpen(pPa
13b9e 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20  ger->jfd) ){.   
13b9f 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f     if( pPg->pgno
13ba0 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  <=pPager->dbOrig
13ba1 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Size ){.        
13ba2 75 33 32 20 63 6b 73 75 6d 3b 0a 20 20 20 20 20  u32 cksum;.     
13ba3 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b     char *pData2;
13ba4 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20  ..        /* We 
13ba5 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 77 72 69  should never wri
13ba6 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  te to the journa
13ba7 6c 20 66 69 6c 65 20 74 68 65 20 70 61 67 65 20  l file the page 
13ba8 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  that.        ** 
13ba9 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74  contains the dat
13baa 61 62 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68  abase locks.  Th
13bab 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
13bac 72 74 20 76 65 72 69 66 69 65 73 0a 20 20 20 20  rt verifies.    
13bad 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64      ** that we d
13bae 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20  o not. */.      
13baf 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70    assert( pPg->p
13bb0 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47  gno!=PAGER_MJ_PG
13bb1 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  NO(pPager) );.  
13bb2 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61        CODEC2(pPa
13bb3 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d  ger, pData, pPg-
13bb4 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e  >pgno, 7, return
13bb5 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70   SQLITE_NOMEM, p
13bb6 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20  Data2);.        
13bb7 63 6b 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b  cksum = pager_ck
13bb8 73 75 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a  sum(pPager, (u8*
13bb9 29 70 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20  )pData2);.      
13bba 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69    rc = write32bi
13bbb 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
13bbc 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
13bbd 66 66 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  ff, pPg->pgno);.
13bbe 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
13bbf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13bc0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
13bc1 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
13bc2 72 2d 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20  r->jfd, pData2, 
13bc3 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
13bc4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
13bc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13bc6 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
13bc7 66 66 20 2b 20 34 29 3b 0a 20 20 20 20 20 20 20  ff + 4);.       
13bc8 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
13bc9 61 6c 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 2d  alOff += pPager-
13bca 3e 70 61 67 65 53 69 7a 65 2b 34 3b 0a 20 20 20  >pageSize+4;.   
13bcb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
13bcc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13bcd 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
13bce 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
13bcf 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67  Pager->jfd, pPag
13bd0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20  er->journalOff, 
13bd1 63 6b 73 75 6d 29 3b 0a 20 20 20 20 20 20 20 20  cksum);.        
13bd2 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
13bd3 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 20  lOff += 4;.     
13bd4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54     }.        IOT
13bd5 52 41 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25  RACE(("JOUT %p %
13bd6 64 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50  d %lld %d\n", pP
13bd7 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c  ager, pPg->pgno,
13bd8 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
13bd9 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
13bda 61 6c 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70  alOff, pPager->p
13bdb 61 67 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20  ageSize));.     
13bdc 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71     PAGER_INCR(sq
13bdd 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74  lite3_pager_writ
13bde 65 6a 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20  ej_count);.     
13bdf 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22     PAGERTRACE(("
13be0 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20  JOURNAL %d page 
13be1 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68  %d needSync=%d h
13be2 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20  ash(%08x)\n",.  
13be3 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52             PAGER
13be4 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
13be5 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20  >pgno, .        
13be6 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67       ((pPg->flag
13be7 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
13be8 43 29 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70  C)?1:0), pager_p
13be9 61 67 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a  agehash(pPg)));.
13bea 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 76 65 6e  .        /* Even
13beb 20 69 66 20 61 6e 20 49 4f 20 6f 72 20 64 69 73   if an IO or dis
13bec 6b 66 75 6c 6c 20 65 72 72 6f 72 20 6f 63 63 75  kfull error occu
13bed 72 72 65 64 20 77 68 69 6c 65 20 6a 6f 75 72 6e  rred while journ
13bee 61 6c 6c 69 6e 67 20 74 68 65 0a 20 20 20 20 20  alling the.     
13bef 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68     ** page in th
13bf0 65 20 62 6c 6f 63 6b 20 61 62 6f 76 65 2c 20 73  e block above, s
13bf1 65 74 20 74 68 65 20 6e 65 65 64 2d 73 79 6e 63  et the need-sync
13bf2 20 66 6c 61 67 20 66 6f 72 20 74 68 65 20 70 61   flag for the pa
13bf3 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f  ge..        ** O
13bf4 74 68 65 72 77 69 73 65 2c 20 77 68 65 6e 20 74  therwise, when t
13bf5 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
13bf6 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74  s rolled back, t
13bf7 68 65 20 6c 6f 67 69 63 20 69 6e 0a 20 20 20 20  he logic in.    
13bf8 20 20 20 20 2a 2a 20 70 6c 61 79 62 61 63 6b 5f      ** playback_
13bf9 6f 6e 65 5f 70 61 67 65 28 29 20 77 69 6c 6c 20  one_page() will 
13bfa 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 70  think that the p
13bfb 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20  age needs to be 
13bfc 72 65 73 74 6f 72 65 64 0a 20 20 20 20 20 20 20  restored.       
13bfd 20 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62   ** in the datab
13bfe 61 73 65 20 66 69 6c 65 2e 20 41 6e 64 20 69 66  ase file. And if
13bff 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
13c00 75 72 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20  urs while doing 
13c01 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  so,.        ** t
13c02 68 65 6e 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d  hen corruption m
13c03 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20 20 20 20 20  ay follow..     
13c04 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66     */.        if
13c05 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  ( !pPager->noSyn
13c06 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  c ){.          p
13c07 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48  Pg->flags |= PGH
13c08 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
13c09 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
13c0a 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20  needSync = 1;.  
13c0b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
13c0c 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73   /* An error has
13c0d 20 6f 63 63 75 72 72 65 64 20 77 72 69 74 69 6e   occurred writin
13c0e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  g to the journal
13c0f 20 66 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20   file. The .    
13c10 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69      ** transacti
13c11 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65  on will be rolle
13c12 64 20 62 61 63 6b 20 62 79 20 74 68 65 20 6c 61  d back by the la
13c13 79 65 72 20 61 62 6f 76 65 2e 0a 20 20 20 20 20  yer above..     
13c14 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66     */.        if
13c15 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13c16 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
13c17 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
13c18 7d 0a 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  }..        pPage
13c19 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20  r->nRec++;.     
13c1a 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13c1b 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30  r->pInJournal!=0
13c1c 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   );.        rc =
13c1d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65   sqlite3BitvecSe
13c1e 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  t(pPager->pInJou
13c1f 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29  rnal, pPg->pgno)
13c20 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
13c21 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  se( rc==SQLITE_N
13c22 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20  OMEM );.        
13c23 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
13c24 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c  TE_OK || rc==SQL
13c25 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
13c26 20 20 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f       rc |= addTo
13c27 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73  SavepointBitvecs
13c28 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67  (pPager, pPg->pg
13c29 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  no);.        if(
13c2a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13c2b 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
13c2c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  rt( rc==SQLITE_N
13c2d 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20  OMEM );.        
13c2e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
13c2f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
13c30 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  se{.        if( 
13c31 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  !pPager->journal
13c32 53 74 61 72 74 65 64 20 26 26 20 21 70 50 61 67  Started && !pPag
13c33 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20  er->noSync ){.  
13c34 20 20 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61          pPg->fla
13c35 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44  gs |= PGHDR_NEED
13c36 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20  _SYNC;.         
13c37 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
13c38 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  c = 1;.        }
13c39 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52  .        PAGERTR
13c3a 41 43 45 28 28 22 41 50 50 45 4e 44 20 25 64 20  ACE(("APPEND %d 
13c3b 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63  page %d needSync
13c3c 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  =%d\n",.        
13c3d 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28          PAGERID(
13c3e 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67  pPager), pPg->pg
13c3f 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  no,.            
13c40 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26     ((pPg->flags&
13c41 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
13c42 3f 31 3a 30 29 29 29 3b 0a 20 20 20 20 20 20 7d  ?1:0)));.      }
13c43 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
13c44 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   If the statemen
13c45 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65  t journal is ope
13c46 6e 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69  n and the page i
13c47 73 20 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20  s not in it,.   
13c48 20 2a 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74   ** then write t
13c49 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20  he current page 
13c4a 74 6f 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  to the statement
13c4b 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20   journal.  Note 
13c4c 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20  that.    ** the 
13c4d 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
13c4e 6c 20 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73  l format differs
13c4f 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61   from the standa
13c50 72 64 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61  rd journal forma
13c51 74 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74  t.    ** in that
13c52 20 69 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68   it omits the ch
13c53 65 63 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20  ecksums and the 
13c54 68 65 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20  header..    */. 
13c55 20 20 20 69 66 28 20 73 75 62 6a 52 65 71 75 69     if( subjRequi
13c56 72 65 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a  resPage(pPg) ){.
13c57 20 20 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f        rc = subjo
13c58 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a  urnalPage(pPg);.
13c59 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
13c5a 55 70 64 61 74 65 20 74 68 65 20 64 61 74 61 62  Update the datab
13c5b 61 73 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74  ase size and ret
13c5c 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  urn..  */.  asse
13c5d 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
13c5e 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20  e>=PAGER_SHARED 
13c5f 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
13c60 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e  >dbSize<pPg->pgn
13c61 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  o ){.    pPager-
13c62 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70  >dbSize = pPg->p
13c63 67 6e 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  gno;.  }.  retur
13c64 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
13c65 61 72 6b 20 61 20 64 61 74 61 20 70 61 67 65 20  ark a data page 
13c66 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68  as writeable. Th
13c67 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20  is routine must 
13c68 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
13c69 20 0a 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e   .** making chan
13c6a 67 65 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54  ges to a page. T
13c6b 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63  he caller must c
13c6c 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
13c6d 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69  value .** of thi
13c6e 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62  s function and b
13c6f 65 20 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f  e careful not to
13c70 20 63 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65   change any page
13c71 20 64 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a   data unless .**
13c72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
13c73 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e  turns SQLITE_OK.
13c74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65  .**.** The diffe
13c75 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68  rence between th
13c76 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20  is function and 
13c77 70 61 67 65 72 5f 77 72 69 74 65 28 29 20 69 73  pager_write() is
13c78 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75   that this.** fu
13c79 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c  nction also deal
13c7a 73 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69  s with the speci
13c7b 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 32 20  al case where 2 
13c7c 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a  or more pages.**
13c7d 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65   fit on a single
13c7e 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e   disk sector. In
13c7f 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63   this case all c
13c80 6f 2d 72 65 73 69 64 65 6e 74 20 70 61 67 65 73  o-resident pages
13c81 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65  .** must have be
13c82 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  en written to th
13c83 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62  e journal file b
13c84 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
13c85 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
13c86 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54  or occurs, SQLIT
13c87 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f  E_NOMEM or an IO
13c88 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
13c89 65 74 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70  eturned.** as ap
13c8a 70 72 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72  propriate. Other
13c8b 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e  wise, SQLITE_OK.
13c8c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13c8d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
13c8e 67 65 72 57 72 69 74 65 28 44 62 50 61 67 65 20  gerWrite(DbPage 
13c8f 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74  *pDbPage){.  int
13c90 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13c91 0a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d  ..  PgHdr *pPg =
13c92 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65   pDbPage;.  Page
13c93 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
13c94 3e 70 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20  >pPager;.  Pgno 
13c95 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d  nPagePerSector =
13c96 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72   (pPager->sector
13c97 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67  Size/pPager->pag
13c98 65 53 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 6e  eSize);..  if( n
13c99 50 61 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20  PagePerSector>1 
13c9a 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67  ){.    Pgno nPag
13c9b 65 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20  eCount;         
13c9c 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
13c9d 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61 74   of pages in dat
13c9e 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
13c9f 20 20 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20    Pgno pg1;     
13ca0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
13ca1 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
13ca2 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c   sector pPg is l
13ca3 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20  ocated on. */.  
13ca4 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20    int nPage;    
13ca5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
13ca6 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73  umber of pages s
13ca7 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 20 74  tarting at pg1 t
13ca8 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20  o journal */.   
13ca9 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20   int ii;        
13caa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
13cab 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
13cac 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d    int needSync =
13cad 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54   0;         /* T
13cae 72 75 65 20 69 66 20 61 6e 79 20 70 61 67 65 20  rue if any page 
13caf 68 61 73 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  has PGHDR_NEED_S
13cb0 59 4e 43 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53  YNC */..    /* S
13cb1 65 74 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63  et the doNotSync
13cb2 20 66 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73   flag to 1. This
13cb3 20 69 73 20 62 65 63 61 75 73 65 20 77 65 20 63   is because we c
13cb4 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f  annot allow a jo
13cb5 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61  urnal.    ** hea
13cb6 64 65 72 20 74 6f 20 62 65 20 77 72 69 74 74 65  der to be writte
13cb7 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 70 61  n between the pa
13cb8 67 65 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79  ges journaled by
13cb9 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
13cba 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
13cbb 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20  t( !MEMDB );.   
13cbc 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13cbd 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b  >doNotSync==0 );
13cbe 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e  .    pPager->doN
13cbf 6f 74 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20  otSync = 1;..   
13cc0 20 2f 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61   /* This trick a
13cc1 73 73 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68  ssumes that both
13cc2 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61   the page-size a
13cc3 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61  nd sector-size a
13cc4 72 65 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74  re.    ** an int
13cc5 65 67 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e  eger power of 2.
13cc6 20 49 74 20 73 65 74 73 20 76 61 72 69 61 62 6c   It sets variabl
13cc7 65 20 70 67 31 20 74 6f 20 74 68 65 20 69 64 65  e pg1 to the ide
13cc8 6e 74 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f  ntifier.    ** o
13cc9 66 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  f the first page
13cca 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70   of the sector p
13ccb 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e  Pg is located on
13ccc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31  ..    */.    pg1
13ccd 20 3d 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31   = ((pPg->pgno-1
13cce 29 20 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65  ) & ~(nPagePerSe
13ccf 63 74 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20  ctor-1)) + 1;.. 
13cd0 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50     sqlite3PagerP
13cd1 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
13cd2 20 28 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f   (int *)&nPageCo
13cd3 75 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50  unt);.    if( pP
13cd4 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75  g->pgno>nPageCou
13cd5 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67  nt ){.      nPag
13cd6 65 20 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d  e = (pPg->pgno -
13cd7 20 70 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c   pg1)+1;.    }el
13cd8 73 65 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67  se if( (pg1+nPag
13cd9 65 50 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50  ePerSector-1)>nP
13cda 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20  ageCount ){.    
13cdb 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43    nPage = nPageC
13cdc 6f 75 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20  ount+1-pg1;.    
13cdd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61  }else{.      nPa
13cde 67 65 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63  ge = nPagePerSec
13cdf 74 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  tor;.    }.    a
13ce0 73 73 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a  ssert(nPage>0);.
13ce1 20 20 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d      assert(pg1<=
13ce2 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  pPg->pgno);.    
13ce3 61 73 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67  assert((pg1+nPag
13ce4 65 29 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a  e)>pPg->pgno);..
13ce5 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
13ce6 3c 6e 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51  <nPage && rc==SQ
13ce7 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a  LITE_OK; ii++){.
13ce8 20 20 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20        Pgno pg = 
13ce9 70 67 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67  pg1+ii;.      Pg
13cea 48 64 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20  Hdr *pPage;.    
13ceb 20 20 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70    if( pg==pPg->p
13cec 67 6e 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42  gno || !sqlite3B
13ced 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72  itvecTest(pPager
13cee 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67  ->pInJournal, pg
13cef 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ) ){.        if(
13cf0 20 70 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47   pg!=PAGER_MJ_PG
13cf1 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  NO(pPager) ){.  
13cf2 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
13cf3 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61  ite3PagerGet(pPa
13cf4 67 65 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29  ger, pg, &pPage)
13cf5 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
13cf6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
13cf7 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
13cf8 3d 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 50  = pager_write(pP
13cf9 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
13cfa 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61    if( pPage->fla
13cfb 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
13cfc 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  NC ){.          
13cfd 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31      needSync = 1
13cfe 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
13cff 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e  assert(pPager->n
13d00 65 65 64 53 79 6e 63 29 3b 0a 20 20 20 20 20 20  eedSync);.      
13d01 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13d02 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
13d03 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20  Unref(pPage);.  
13d04 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13d05 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20    }.      }else 
13d06 69 66 28 20 28 70 50 61 67 65 20 3d 20 70 61 67  if( (pPage = pag
13d07 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72  er_lookup(pPager
13d08 2c 20 70 67 29 29 21 3d 30 20 29 7b 0a 20 20 20  , pg))!=0 ){.   
13d09 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
13d0a 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
13d0b 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20  _SYNC ){.       
13d0c 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b     needSync = 1;
13d0d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
13d0e 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
13d0f 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20  nref(pPage);.   
13d10 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
13d11 2f 2a 20 49 66 20 74 68 65 20 50 47 48 44 52 5f  /* If the PGHDR_
13d12 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69  NEED_SYNC flag i
13d13 73 20 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66  s set for any of
13d14 20 74 68 65 20 6e 50 61 67 65 20 70 61 67 65 73   the nPage pages
13d15 20 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e   .    ** startin
13d16 67 20 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69  g at pg1, then i
13d17 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65  t needs to be se
13d18 74 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  t for all of the
13d19 6d 2e 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a  m. Because.    *
13d1a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79  * writing to any
13d1b 20 6f 66 20 74 68 65 73 65 20 6e 50 61 67 65 20   of these nPage 
13d1c 70 61 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65  pages may damage
13d1d 20 74 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65   the others, the
13d1e 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20  .    ** journal 
13d1f 66 69 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69  file must contai
13d20 6e 20 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65  n sync()ed copie
13d21 73 20 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d  s of all of them
13d22 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61  .    ** before a
13d23 6e 79 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62  ny of them can b
13d24 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f  e written out to
13d25 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13d26 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  le..    */.    i
13d27 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13d28 20 26 26 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a   && needSync ){.
13d29 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 4d        assert( !M
13d2a 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e  EMDB && pPager->
13d2b 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20  noSync==0 );.   
13d2c 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
13d2d 6e 50 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 20  nPage; ii++){.  
13d2e 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61        PgHdr *pPa
13d2f 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75  ge = pager_looku
13d30 70 28 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69  p(pPager, pg1+ii
13d31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
13d32 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Page ){.        
13d33 20 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c    pPage->flags |
13d34 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  = PGHDR_NEED_SYN
13d35 43 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  C;.          sql
13d36 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
13d37 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  Page);.        }
13d38 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
13d39 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65  ssert(pPager->ne
13d3a 65 64 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a  edSync);.    }..
13d3b 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
13d3c 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31  er->doNotSync==1
13d3d 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   );.    pPager->
13d3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20  doNotSync = 0;. 
13d3f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
13d40 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 44 62   pager_write(pDb
13d41 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Page);.  }.  ret
13d42 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
13d43 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
13d44 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 20 69  the page given i
13d45 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77  n the argument w
13d46 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61  as previously pa
13d47 73 73 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  ssed.** to sqlit
13d48 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2e 20  e3PagerWrite(). 
13d49 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
13d4a 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   return TRUE if 
13d4b 69 74 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63  it is ok.** to c
13d4c 68 61 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e  hange the conten
13d4d 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a  t of the page..*
13d4e 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
13d4f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13d50 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
13d51 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 50 61  Iswriteable(DbPa
13d52 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75  ge *pPg){.  retu
13d53 72 6e 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  rn pPg->flags&PG
13d54 48 44 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e  HDR_DIRTY;.}.#en
13d55 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
13d56 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54  ITE_SECURE_DELET
13d57 45 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  E./*.** A call t
13d58 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  o this routine t
13d59 65 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 74  ells the pager t
13d5a 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65  hat it is not ne
13d5b 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72  cessary to.** wr
13d5c 69 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74  ite the informat
13d5d 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20  ion on page pPg 
13d5e 62 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b  back to the disk
13d5f 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a  , even though.**
13d60 20 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 74   that page might
13d61 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69   be marked as di
13d62 72 74 79 2e 20 20 54 68 69 73 20 68 61 70 70 65  rty.  This happe
13d63 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ns, for example,
13d64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67   when.** the pag
13d65 65 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64  e has been added
13d66 20 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68   as a leaf of th
13d67 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73  e freelist and s
13d68 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74  o its.** content
13d69 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65   no longer matte
13d6a 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76  rs..**.** The ov
13d6b 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65  erlying software
13d6c 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69   layer calls thi
13d6d 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61  s routine when a
13d6e 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a  ll of the data.*
13d6f 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70  * on the given p
13d70 61 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54  age is unused. T
13d71 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74  he pager marks t
13d72 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e  he page as clean
13d73 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64   so.** that it d
13d74 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74  oes not get writ
13d75 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a  ten to disk..**.
13d76 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74 68  ** Tests show th
13d77 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61  at this optimiza
13d78 74 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70  tion can quadrup
13d79 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20  le the speed of 
13d7a 6c 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45  large .** DELETE
13d7b 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a   operations..*/.
13d7c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
13d7d 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
13d7e 44 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72 20  DontWrite(PgHdr 
13d7f 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  *pPg){.  Pager *
13d80 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
13d81 61 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67  ager;.  if( (pPg
13d82 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
13d83 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e  RTY) && pPager->
13d84 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b  nSavepoint==0 ){
13d85 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28  .    PAGERTRACE(
13d86 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67  ("DONT_WRITE pag
13d87 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70  e %d of %d\n", p
13d88 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49  Pg->pgno, PAGERI
13d89 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20  D(pPager)));.   
13d8a 20 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e   IOTRACE(("CLEAN
13d8b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   %p %d\n", pPage
13d8c 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20  r, pPg->pgno)). 
13d8d 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
13d8e 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54   PGHDR_DONT_WRIT
13d8f 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  E;.#ifdef SQLITE
13d90 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20  _CHECK_PAGES.   
13d91 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d   pPg->pageHash =
13d92 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28   pager_pagehash(
13d93 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  pPg);.#endif.  }
13d94 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  .}.#endif /* !de
13d95 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 45 43  fined(SQLITE_SEC
13d96 55 52 45 5f 44 45 4c 45 54 45 29 20 2a 2f 0a 0a  URE_DELETE) */..
13d97 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
13d98 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
13d99 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61  increment the va
13d9a 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  lue of the datab
13d9b 61 73 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61  ase file .** cha
13d9c 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f  nge-counter, sto
13d9d 72 65 64 20 61 73 20 61 20 34 2d 62 79 74 65 20  red as a 4-byte 
13d9e 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67  big-endian integ
13d9f 65 72 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a  er starting at .
13da0 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74 20 32  ** byte offset 2
13da1 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 66  4 of the pager f
13da2 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ile..**.** If th
13da3 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66  e isDirectMode f
13da4 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65  lag is zero, the
13da5 6e 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62  n this is done b
13da6 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71  y calling .** sq
13da7 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
13da8 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65  ) on page 1, the
13da9 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20  n modifying the 
13daa 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a  contents of the.
13dab 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e  ** page data. In
13dac 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 66   this case the f
13dad 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61  ile will be upda
13dae 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 75 72  ted when the cur
13daf 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74  rent.** transact
13db0 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
13db1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69  ..**.** The isDi
13db2 72 65 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61  rectMode flag ma
13db3 79 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65  y only be non-ze
13db4 72 6f 20 69 66 20 74 68 65 20 6c 69 62 72 61 72  ro if the librar
13db5 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a  y was compiled.*
13db6 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  * with the SQLIT
13db7 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
13db8 57 52 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69  WRITE macro defi
13db9 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ned. In this cas
13dba 65 2c 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63  e,.** if isDirec
13dbb 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  t is non-zero, t
13dbc 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
13dbd 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64   file is updated
13dbe 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20   directly.** by 
13dbf 77 72 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74  writing an updat
13dc0 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61  ed version of pa
13dc1 67 65 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c  ge 1 using a cal
13dc2 6c 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c  l to the .** sql
13dc3 69 74 65 33 4f 73 57 72 69 74 65 28 29 20 66 75  ite3OsWrite() fu
13dc4 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
13dc5 63 20 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72  c int pager_incr
13dc6 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50  _changecounter(P
13dc7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
13dc8 74 20 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b  t isDirectMode){
13dc9 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
13dca 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63  TE_OK;..  /* Dec
13dcb 6c 61 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c  lare and initial
13dcc 69 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74  ize constant int
13dcd 65 67 65 72 20 27 69 73 44 69 72 65 63 74 27 2e  eger 'isDirect'.
13dce 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f   If the.  ** ato
13dcf 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69  mic-write optimi
13dd0 7a 61 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65  zation is enable
13dd1 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c  d in this build,
13dd2 20 74 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20   then isDirect. 
13dd3 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a   ** is initializ
13dd4 65 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  ed to the value 
13dd5 70 61 73 73 65 64 20 61 73 20 74 68 65 20 69 73  passed as the is
13dd6 44 69 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d  DirectMode param
13dd7 65 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69  eter.  ** to thi
13dd8 73 20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65  s function. Othe
13dd9 72 77 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77  rwise, it is alw
13dda 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e  ays set to zero.
13ddb 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69  .  **.  ** The i
13ddc 64 65 61 20 69 73 20 74 68 61 74 20 69 66 20 74  dea is that if t
13ddd 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20  he atomic-write 
13dde 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20  optimization is 
13ddf 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64  not.  ** enabled
13de0 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65   at compile time
13de1 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63  , the compiler c
13de2 61 6e 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74  an omit the test
13de3 73 20 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72  s of.  ** 'isDir
13de4 65 63 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77  ect' below, as w
13de5 65 6c 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b  ell as the block
13de6 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65   enclosed in the
13de7 0a 20 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72  .  ** "if( isDir
13de8 65 63 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e  ect )" condition
13de9 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
13dea 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
13deb 4d 49 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69  MIC_WRITE.# defi
13dec 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30  ne DIRECT_MODE 0
13ded 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 69 72  .  assert( isDir
13dee 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20  ectMode==0 );.  
13def 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
13df0 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a  (isDirectMode);.
13df1 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44  #else.# define D
13df2 49 52 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72  IRECT_MODE isDir
13df3 65 63 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a  ectMode.#endif..
13df4 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13df5 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52  ->state>=PAGER_R
13df6 45 53 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28  ESERVED );.  if(
13df7 20 21 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65   !pPager->change
13df8 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20 70 50 61  CountDone && pPa
13df9 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b  ger->dbSize>0 ){
13dfa 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 48  .    PgHdr *pPgH
13dfb 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
13dfc 20 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20     /* Reference 
13dfd 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 20  to page 1 */.   
13dfe 20 75 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e   u32 change_coun
13dff 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ter;           /
13e00 2a 20 49 6e 69 74 69 61 6c 20 76 61 6c 75 65 20  * Initial value 
13e01 6f 66 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  of change-counte
13e02 72 20 66 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20  r field */..    
13e03 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d  assert( !pPager-
13e04 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 69 73 4f  >tempFile && isO
13e05 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
13e06 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20  );..    /* Open 
13e07 70 61 67 65 20 31 20 6f 66 20 74 68 65 20 66 69  page 1 of the fi
13e08 6c 65 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20  le for writing. 
13e09 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  */.    rc = sqli
13e0a 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67  te3PagerGet(pPag
13e0b 65 72 2c 20 31 2c 20 26 70 50 67 48 64 72 29 3b  er, 1, &pPgHdr);
13e0c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67  .    assert( pPg
13e0d 48 64 72 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51  Hdr==0 || rc==SQ
13e0e 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20  LITE_OK );..    
13e0f 2f 2a 20 49 66 20 70 61 67 65 20 6f 6e 65 20 77  /* If page one w
13e10 61 73 20 66 65 74 63 68 65 64 20 73 75 63 63 65  as fetched succe
13e11 73 73 66 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69  ssfully, and thi
13e12 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f  s function is no
13e13 74 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69  t.    ** operati
13e14 6e 67 20 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64  ng in direct-mod
13e15 65 2c 20 6d 61 6b 65 20 70 61 67 65 20 31 20 77  e, make page 1 w
13e16 72 69 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6e  ritable.  When n
13e17 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 64 69  ot in .    ** di
13e18 72 65 63 74 20 6d 6f 64 65 2c 20 70 61 67 65 20  rect mode, page 
13e19 31 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64  1 is always held
13e1a 20 69 6e 20 63 61 63 68 65 20 61 6e 64 20 68 65   in cache and he
13e1b 6e 63 65 20 74 68 65 20 50 61 67 65 72 47 65 74  nce the PagerGet
13e1c 28 29 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 20  ().    ** above 
13e1d 69 73 20 61 6c 77 61 79 73 20 73 75 63 63 65 73  is always succes
13e1e 73 66 75 6c 20 2d 20 68 65 6e 63 65 20 74 68 65  sful - hence the
13e1f 20 41 4c 57 41 59 53 20 6f 6e 20 72 63 3d 3d 53   ALWAYS on rc==S
13e20 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f  QLITE_OK..    */
13e21 0a 20 20 20 20 69 66 28 20 21 44 49 52 45 43 54  .    if( !DIRECT
13e22 5f 4d 4f 44 45 20 26 26 20 41 4c 57 41 59 53 28  _MODE && ALWAYS(
13e23 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29  rc==SQLITE_OK) )
13e24 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
13e25 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
13e26 50 67 48 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20  PgHdr);.    }.. 
13e27 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13e28 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a  E_OK ){.      /*
13e29 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76   Increment the v
13e2a 61 6c 75 65 20 6a 75 73 74 20 72 65 61 64 20 61  alue just read a
13e2b 6e 64 20 77 72 69 74 65 20 69 74 20 62 61 63 6b  nd write it back
13e2c 20 74 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a   to byte 24. */.
13e2d 20 20 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75        change_cou
13e2e 6e 74 65 72 20 3d 20 73 71 6c 69 74 65 33 47 65  nter = sqlite3Ge
13e2f 74 34 62 79 74 65 28 28 75 38 2a 29 70 50 61 67  t4byte((u8*)pPag
13e30 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 3b  er->dbFileVers);
13e31 0a 20 20 20 20 20 20 63 68 61 6e 67 65 5f 63 6f  .      change_co
13e32 75 6e 74 65 72 2b 2b 3b 0a 20 20 20 20 20 20 70  unter++;.      p
13e33 75 74 33 32 62 69 74 73 28 28 28 63 68 61 72 2a  ut32bits(((char*
13e34 29 70 50 67 48 64 72 2d 3e 70 44 61 74 61 29 2b  )pPgHdr->pData)+
13e35 32 34 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74  24, change_count
13e36 65 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49  er);..      /* I
13e37 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 64 69 72  f running in dir
13e38 65 63 74 20 6d 6f 64 65 2c 20 77 72 69 74 65 20  ect mode, write 
13e39 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
13e3a 70 61 67 65 20 31 20 74 6f 20 74 68 65 20 66 69  page 1 to the fi
13e3b 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  le. */.      if(
13e3c 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 29 7b 0a   DIRECT_MODE ){.
13e3d 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f          const vo
13e3e 69 64 20 2a 7a 42 75 66 20 3d 20 70 50 67 48 64  id *zBuf = pPgHd
13e3f 72 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 20 20  r->pData;.      
13e40 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13e41 2d 3e 64 62 46 69 6c 65 53 69 7a 65 3e 30 20 29  ->dbFileSize>0 )
13e42 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
13e43 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
13e44 61 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20  ager->fd, zBuf, 
13e45 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
13e46 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
13e47 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
13e48 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  ){.          pPa
13e49 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74  ger->changeCount
13e4a 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  Done = 1;.      
13e4b 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
13e4c 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
13e4d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
13e4e 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
13e4f 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65    }..    /* Rele
13e50 61 73 65 20 74 68 65 20 70 61 67 65 20 72 65 66  ase the page ref
13e51 65 72 65 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73  erence. */.    s
13e52 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
13e53 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20  (pPgHdr);.  }.  
13e54 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
13e55 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 70 61 67  .** Sync the pag
13e56 65 72 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e  er file to disk.
13e57 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   This is a no-op
13e58 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66   for in-memory f
13e59 69 6c 65 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73  iles.** or pages
13e5a 20 77 69 74 68 20 74 68 65 20 50 61 67 65 72 2e   with the Pager.
13e5b 6e 6f 53 79 6e 63 20 66 6c 61 67 20 73 65 74 2e  noSync flag set.
13e5c 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
13e5d 73 66 75 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20  sful, or called 
13e5e 6f 6e 20 61 20 70 61 67 65 72 20 66 6f 72 20 77  on a pager for w
13e5f 68 69 63 68 20 69 74 20 69 73 20 61 20 6e 6f 2d  hich it is a no-
13e60 6f 70 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63  op, this.** func
13e61 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c  tion returns SQL
13e62 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73  ITE_OK. Otherwis
13e63 65 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  e, an IO error c
13e64 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
13e65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13e66 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
13e67 67 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70  gerSync(Pager *p
13e68 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
13e69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13e6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13e6b 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
13e6c 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44  .  assert( !MEMD
13e6d 42 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  B );.  if( pPage
13e6e 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20  r->noSync ){.   
13e6f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13e70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
13e71 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
13e72 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61  (pPager->fd, pPa
13e73 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29  ger->sync_flags)
13e74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
13e75 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63  c;.}../*.** Sync
13e76 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13e77 6c 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72  le for the pager
13e78 20 70 50 61 67 65 72 2e 20 7a 4d 61 73 74 65 72   pPager. zMaster
13e79 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6e   points to the n
13e7a 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74  ame.** of a mast
13e7b 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
13e7c 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77  that should be w
13e7d 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
13e7e 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f  individual.** jo
13e7f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73  urnal file. zMas
13e80 74 65 72 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c  ter may be NULL,
13e81 20 77 68 69 63 68 20 69 73 20 69 6e 74 65 72 70   which is interp
13e82 72 65 74 65 64 20 61 73 20 6e 6f 20 6d 61 73 74  reted as no mast
13e83 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61  er.** journal (a
13e84 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
13e85 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a   transaction)..*
13e86 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
13e87 65 20 65 6e 73 75 72 65 73 20 74 68 61 74 3a 0a  e ensures that:.
13e88 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61  **.**   * The da
13e89 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e  tabase file chan
13e8a 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 75 70  ge-counter is up
13e8b 64 61 74 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68  dated,.**   * th
13e8c 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e  e journal is syn
13e8d 63 65 64 20 28 75 6e 6c 65 73 73 20 74 68 65 20  ced (unless the 
13e8e 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74  atomic-write opt
13e8f 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 75 73 65  imization is use
13e90 64 29 2c 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64  d),.**   * all d
13e91 69 72 74 79 20 70 61 67 65 73 20 61 72 65 20 77  irty pages are w
13e92 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
13e93 74 61 62 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a  tabase file, .**
13e94 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73     * the databas
13e95 65 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61  e file is trunca
13e96 74 65 64 20 28 69 66 20 72 65 71 75 69 72 65 64  ted (if required
13e97 29 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68  ), and.**   * th
13e98 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13e99 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54  synced. .**.** T
13e9a 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68  he only thing th
13e9b 61 74 20 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f  at remains to co
13e9c 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63  mmit the transac
13e9d 74 69 6f 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c  tion is to final
13e9e 69 7a 65 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c  ize .** (delete,
13e9f 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72   truncate or zer
13ea0 6f 20 74 68 65 20 66 69 72 73 74 20 70 61 72 74  o the first part
13ea1 20 6f 66 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c   of) the journal
13ea2 20 66 69 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65   file (or .** de
13ea3 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  lete the master 
13ea4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20  journal file if 
13ea5 73 70 65 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a  specified)..**.*
13ea6 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 7a  * Note that if z
13ea7 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68  Master==NULL, th
13ea8 69 73 20 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72  is does not over
13ea9 77 72 69 74 65 20 61 20 70 72 65 76 69 6f 75 73  write a previous
13eaa 20 76 61 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64   value.** passed
13eab 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 50 61   to an sqlite3Pa
13eac 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  gerCommitPhaseOn
13ead 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  e() call..**.** 
13eae 49 66 20 74 68 65 20 66 69 6e 61 6c 20 70 61 72  If the final par
13eaf 61 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20  ameter - noSync 
13eb0 2d 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  - is true, then 
13eb1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13eb2 65 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e  e itself.** is n
13eb3 6f 74 20 73 79 6e 63 65 64 2e 20 54 68 65 20 63  ot synced. The c
13eb4 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20  aller must call 
13eb5 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63  sqlite3PagerSync
13eb6 28 29 20 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a  () directly to.*
13eb7 2a 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62  * sync the datab
13eb8 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20  ase file before 
13eb9 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68  calling CommitPh
13eba 61 73 65 54 77 6f 28 29 20 74 6f 20 64 65 6c 65  aseTwo() to dele
13ebb 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61  te the.** journa
13ebc 6c 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63  l file in this c
13ebd 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ase..*/.SQLITE_P
13ebe 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13ebf 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
13ec0 73 65 4f 6e 65 28 0a 20 20 50 61 67 65 72 20 2a  seOne(.  Pager *
13ec1 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20  pPager,         
13ec2 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
13ec3 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f  r object */.  co
13ec4 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
13ec5 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r,            /*
13ec6 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   If not NULL, th
13ec7 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
13ec8 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   name */.  int n
13ec9 6f 53 79 6e 63 20 20 20 20 20 20 20 20 20 20 20  oSync           
13eca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
13ecb 75 65 20 74 6f 20 6f 6d 69 74 20 74 68 65 20 78  ue to omit the x
13ecc 53 79 6e 63 20 6f 6e 20 74 68 65 20 64 62 20 66  Sync on the db f
13ecd 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ile */.){.  int 
13ece 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
13ecf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
13ed0 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
13ed1 20 2f 2a 20 54 68 65 20 64 62 4f 72 69 67 53 69   /* The dbOrigSi
13ed2 7a 65 20 69 73 20 6e 65 76 65 72 20 73 65 74 20  ze is never set 
13ed3 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d  if journal_mode=
13ed4 4f 46 46 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  OFF */.  assert(
13ed5 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13ed6 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode!=PAGER_JOUR
13ed7 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70  NALMODE_OFF || p
13ed8 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a  Pager->dbOrigSiz
13ed9 65 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  e==0 );..  /* If
13eda 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 6f   a prior error o
13edb 63 63 75 72 72 65 64 2c 20 74 68 69 73 20 72 6f  ccurred, this ro
13edc 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74  utine should not
13edd 20 62 65 20 63 61 6c 6c 65 64 2e 20 20 52 4f 4c   be called.  ROL
13ede 4c 42 41 43 4b 0a 20 20 2a 2a 20 69 73 20 74 68  LBACK.  ** is th
13edf 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65  e appropriate re
13ee0 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72  sponse to an err
13ee1 6f 72 2c 20 6e 6f 74 20 43 4f 4d 4d 49 54 2e 20  or, not COMMIT. 
13ee2 20 47 75 61 72 64 20 61 67 61 69 6e 73 74 0a 20   Guard against. 
13ee3 20 2a 2a 20 63 6f 64 69 6e 67 20 65 72 72 6f 72   ** coding error
13ee4 73 20 62 79 20 72 65 70 65 61 74 69 6e 67 20 74  s by repeating t
13ee5 68 65 20 70 72 69 6f 72 20 65 72 72 6f 72 2e 20  he prior error. 
13ee6 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
13ee7 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20  Pager->errCode) 
13ee8 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d  ) return pPager-
13ee9 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47  >errCode;..  PAG
13eea 45 52 54 52 41 43 45 28 28 22 44 41 54 41 42 41  ERTRACE(("DATABA
13eeb 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73  SE SYNC: File=%s
13eec 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a   zMaster=%s nSiz
13eed 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20  e=%d\n", .      
13eee 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
13eef 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67  e, zMaster, pPag
13ef0 65 72 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20  er->dbSize));.. 
13ef1 20 69 66 28 20 4d 45 4d 44 42 20 26 26 20 70 50   if( MEMDB && pP
13ef2 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
13ef3 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
13ef4 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f  is is an in-memo
13ef5 72 79 20 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67  ry db, or no pag
13ef6 65 73 20 68 61 76 65 20 62 65 65 6e 20 77 72 69  es have been wri
13ef7 74 74 65 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73  tten to, or this
13ef8 0a 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e  .    ** function
13ef9 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
13efa 6e 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20  n called, it is 
13efb 6d 6f 73 74 6c 79 20 61 20 6e 6f 2d 6f 70 2e 20  mostly a no-op. 
13efc 20 48 6f 77 65 76 65 72 2c 20 61 6e 79 0a 20 20   However, any.  
13efd 20 20 2a 2a 20 62 61 63 6b 75 70 20 69 6e 20 70    ** backup in p
13efe 72 6f 67 72 65 73 73 20 6e 65 65 64 73 20 74 6f  rogress needs to
13eff 20 62 65 20 72 65 73 74 61 72 74 65 64 2e 0a 20   be restarted.. 
13f00 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
13f01 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 70  3BackupRestart(p
13f02 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b  Pager->pBackup);
13f03 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61  .  }else if( pPa
13f04 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45  ger->state!=PAGE
13f05 52 5f 53 59 4e 43 45 44 20 26 26 20 70 50 61 67  R_SYNCED && pPag
13f06 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29  er->dbModified )
13f07 7b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f  {..    /* The fo
13f08 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70  llowing block up
13f09 64 61 74 65 73 20 74 68 65 20 63 68 61 6e 67 65  dates the change
13f0a 2d 63 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c  -counter. Exactl
13f0b 79 20 68 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20  y how it.    ** 
13f0c 64 6f 65 73 20 74 68 69 73 20 64 65 70 65 6e 64  does this depend
13f0d 73 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20  s on whether or 
13f0e 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 75  not the atomic-u
13f0f 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  pdate optimizati
13f10 6f 6e 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 6e  on.    ** was en
13f11 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  abled at compile
13f12 20 74 69 6d 65 2c 20 61 6e 64 20 69 66 20 74 68   time, and if th
13f13 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  is transaction m
13f14 65 65 74 73 20 74 68 65 20 0a 20 20 20 20 2a 2a  eets the .    **
13f15 20 72 75 6e 74 69 6d 65 20 63 72 69 74 65 72 69   runtime criteri
13f16 61 20 74 6f 20 75 73 65 20 74 68 65 20 6f 70 65  a to use the ope
13f17 72 61 74 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a  ration: .    **.
13f18 20 20 20 20 2a 2a 20 20 20 20 2a 20 54 68 65 20      **    * The 
13f19 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70  file-system supp
13f1a 6f 72 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d  orts the atomic-
13f1b 77 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 66  write property f
13f1c 6f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62  or.    **      b
13f1d 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61  locks of size pa
13f1e 67 65 2d 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20  ge-size, and .  
13f1f 20 20 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63    **    * This c
13f20 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72  ommit is not par
13f21 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c  t of a multi-fil
13f22 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61  e transaction, a
13f23 6e 64 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 45  nd.    **    * E
13f24 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20  xactly one page 
13f25 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  has been modifie
13f26 64 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74  d and store in t
13f27 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
13f28 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
13f29 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  f the optimizati
13f2a 6f 6e 20 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c  on was not enabl
13f2b 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  ed at compile ti
13f2c 6d 65 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20  me, then the.   
13f2d 20 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63   ** pager_incr_c
13f2e 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66  hangecounter() f
13f2f 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
13f30 64 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20  d to update the 
13f31 63 68 61 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f  change.    ** co
13f32 75 6e 74 65 72 20 69 6e 20 27 69 6e 64 69 72 65  unter in 'indire
13f33 63 74 2d 6d 6f 64 65 27 2e 20 49 66 20 74 68 65  ct-mode'. If the
13f34 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
13f35 20 63 6f 6d 70 69 6c 65 64 20 69 6e 20 62 75 74   compiled in but
13f36 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61  .    ** is not a
13f37 70 70 6c 69 63 61 62 6c 65 20 74 6f 20 74 68 69  pplicable to thi
13f38 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63  s transaction, c
13f39 61 6c 6c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  all sqlite3Journ
13f3a 61 6c 43 72 65 61 74 65 28 29 0a 20 20 20 20 2a  alCreate().    *
13f3b 2a 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  * to make sure t
13f3c 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13f3d 68 61 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65  has actually bee
13f3e 6e 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20  n created, then 
13f3f 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65  call.    ** page
13f40 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75  r_incr_changecou
13f41 6e 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65  nter() to update
13f42 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
13f43 74 65 72 20 69 6e 20 69 6e 64 69 72 65 63 74 0a  ter in indirect.
13f44 20 20 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20      ** mode. .  
13f45 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65    **.    ** Othe
13f46 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 6f 70  rwise, if the op
13f47 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f  timization is bo
13f48 74 68 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 61  th enabled and a
13f49 70 70 6c 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a  pplicable,.    *
13f4a 2a 20 74 68 65 6e 20 63 61 6c 6c 20 70 61 67 65  * then call page
13f4b 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75  r_incr_changecou
13f4c 6e 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65  nter() to update
13f4d 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
13f4e 74 65 72 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64  ter.    ** in 'd
13f4f 69 72 65 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20  irect' mode. In 
13f50 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f  this case the jo
13f51 75 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20  urnal file will 
13f52 6e 65 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20  never be.    ** 
13f53 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 69 73  created for this
13f54 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
13f55 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
13f56 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43  TE_ENABLE_ATOMIC
13f57 5f 57 52 49 54 45 0a 20 20 20 20 50 67 48 64 72  _WRITE.    PgHdr
13f58 20 2a 70 50 67 3b 0a 20 20 20 20 61 73 73 65 72   *pPg;.    asser
13f59 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
13f5a 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72  ->jfd) || pPager
13f5b 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
13f5c 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
13f5d 5f 4f 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20  _OFF );.    if( 
13f5e 21 7a 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70  !zMaster && isOp
13f5f 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
13f60 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d  .     && pPager-
13f61 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e  >journalOff==jrn
13f62 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 61 67  lBufferSize(pPag
13f63 65 72 29 20 0a 20 20 20 20 20 26 26 20 70 50 61  er) .     && pPa
13f64 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61  ger->dbSize>=pPa
13f65 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a  ger->dbFileSize.
13f66 20 20 20 20 20 26 26 20 28 30 3d 3d 28 70 50 67       && (0==(pPg
13f67 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65   = sqlite3Pcache
13f68 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 72  DirtyList(pPager
13f69 2d 3e 70 50 43 61 63 68 65 29 29 20 7c 7c 20 30  ->pPCache)) || 0
13f6a 3d 3d 70 50 67 2d 3e 70 44 69 72 74 79 29 0a 20  ==pPg->pDirty). 
13f6b 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55     ){.      /* U
13f6c 70 64 61 74 65 20 74 68 65 20 64 62 20 66 69 6c  pdate the db fil
13f6d 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  e change counter
13f6e 20 76 69 61 20 74 68 65 20 64 69 72 65 63 74 2d   via the direct-
13f6f 77 72 69 74 65 20 6d 65 74 68 6f 64 2e 20 54 68  write method. Th
13f70 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c  e .      ** foll
13f71 6f 77 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20  owing call will 
13f72 6d 6f 64 69 66 79 20 74 68 65 20 69 6e 2d 6d 65  modify the in-me
13f73 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74  mory representat
13f74 69 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 0a 20  ion of page 1 . 
13f75 20 20 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75       ** to inclu
13f76 64 65 20 74 68 65 20 75 70 64 61 74 65 64 20 63  de the updated c
13f77 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e  hange counter an
13f78 64 20 74 68 65 6e 20 77 72 69 74 65 20 70 61 67  d then write pag
13f79 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 64 69  e 1 .      ** di
13f7a 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61  rectly to the da
13f7b 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63  tabase file. Bec
13f7c 61 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d  ause of the atom
13f7d 69 63 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20  ic-write .      
13f7e 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74  ** property of t
13f7f 68 65 20 68 6f 73 74 20 66 69 6c 65 2d 73 79 73  he host file-sys
13f80 74 65 6d 2c 20 74 68 69 73 20 69 73 20 73 61 66  tem, this is saf
13f81 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
13f82 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63    rc = pager_inc
13f83 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28  r_changecounter(
13f84 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20  pPager, 1);.    
13f85 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
13f86 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  = sqlite3Journal
13f87 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a  Create(pPager->j
13f88 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  fd);.      if( r
13f89 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13f8a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67          rc = pag
13f8b 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f  er_incr_changeco
13f8c 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29  unter(pPager, 0)
13f8d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
13f8e 23 65 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 70  #else.    rc = p
13f8f 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65  ager_incr_change
13f90 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20  counter(pPager, 
13f91 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69  0);.#endif.    i
13f92 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13f93 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70   ) goto commit_p
13f94 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a  hase_one_exit;..
13f95 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74      /* If this t
13f96 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d  ransaction has m
13f97 61 64 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ade the database
13f98 20 73 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61   smaller, then a
13f99 6c 6c 20 70 61 67 65 73 0a 20 20 20 20 2a 2a 20  ll pages.    ** 
13f9a 62 65 69 6e 67 20 64 69 73 63 61 72 64 65 64 20  being discarded 
13f9b 62 79 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f  by the truncatio
13f9c 6e 20 6d 75 73 74 20 62 65 20 77 72 69 74 74 65  n must be writte
13f9d 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
13f9e 0a 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68  .    ** file. Th
13f9f 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70  is can only happ
13fa0 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75  en in auto-vacuu
13fa1 6d 20 6d 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20  m mode..    **. 
13fa2 20 20 20 2a 2a 20 42 65 66 6f 72 65 20 72 65 61     ** Before rea
13fa3 64 69 6e 67 20 74 68 65 20 70 61 67 65 73 20 77  ding the pages w
13fa4 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73  ith page numbers
13fa5 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
13fa6 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74   .    ** current
13fa7 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e   value of Pager.
13fa8 64 62 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69  dbSize, set dbSi
13fa9 7a 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76  ze back to the v
13faa 61 6c 75 65 0a 20 20 20 20 2a 2a 20 74 68 61 74  alue.    ** that
13fab 20 69 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20   it took at the 
13fac 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61  start of the tra
13fad 6e 73 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77  nsaction. Otherw
13fae 69 73 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20  ise, the.    ** 
13faf 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
13fb0 50 61 67 65 72 47 65 74 28 29 20 72 65 74 75 72  PagerGet() retur
13fb1 6e 20 7a 65 72 6f 65 64 20 70 61 67 65 73 20 69  n zeroed pages i
13fb2 6e 73 74 65 61 64 20 6f 66 20 0a 20 20 20 20 2a  nstead of .    *
13fb3 2a 20 72 65 61 64 69 6e 67 20 64 61 74 61 20 66  * reading data f
13fb4 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
13fb5 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20   file..    **.  
13fb6 20 20 2a 2a 20 57 68 65 6e 20 6a 6f 75 72 6e 61    ** When journa
13fb7 6c 5f 6d 6f 64 65 3d 3d 4f 46 46 20 74 68 65 20  l_mode==OFF the 
13fb8 64 62 4f 72 69 67 53 69 7a 65 20 69 73 20 61 6c  dbOrigSize is al
13fb9 77 61 79 73 20 7a 65 72 6f 2c 20 73 6f 20 74 68  ways zero, so th
13fba 69 73 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20  is.    ** block 
13fbb 6e 65 76 65 72 20 72 75 6e 73 20 69 66 20 6a 6f  never runs if jo
13fbc 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 2e 0a  urnal_mode=OFF..
13fbd 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
13fbe 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
13fbf 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 50  ACUUM.    if( pP
13fc0 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61  ager->dbSize<pPa
13fc1 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
13fc2 0a 20 20 20 20 20 26 26 20 41 4c 57 41 59 53 28  .     && ALWAYS(
13fc3 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
13fc4 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode!=PAGER_JOURN
13fc5 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 20  ALMODE_OFF).    
13fc6 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 3b  ){.      Pgno i;
13fc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fc9 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
13fca 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20  ariable */.     
13fcb 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69   const Pgno iSki
13fcc 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  p = PAGER_MJ_PGN
13fcd 4f 28 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65  O(pPager); /* Pe
13fce 6e 64 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20  nding lock page 
13fcf 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50  */.      const P
13fd0 67 6e 6f 20 64 62 53 69 7a 65 20 3d 20 70 50 61  gno dbSize = pPa
13fd1 67 65 72 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20  ger->dbSize;    
13fd2 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69     /* Database i
13fd3 6d 61 67 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20  mage size */ .  
13fd4 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
13fd5 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f  ze = pPager->dbO
13fd6 72 69 67 53 69 7a 65 3b 0a 20 20 20 20 20 20 66  rigSize;.      f
13fd7 6f 72 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20  or( i=dbSize+1; 
13fd8 69 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  i<=pPager->dbOri
13fd9 67 53 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20  gSize; i++ ){.  
13fda 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
13fdb 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61  e3BitvecTest(pPa
13fdc 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c  ger->pInJournal,
13fdd 20 69 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20   i) && i!=iSkip 
13fde 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 67 48  ){.          PgH
13fdf 64 72 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20  dr *pPage;      
13fe0 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74         /* Page t
13fe1 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20  o journal */.   
13fe2 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
13fe3 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67  te3PagerGet(pPag
13fe4 65 72 2c 20 69 2c 20 26 70 50 61 67 65 29 3b 0a  er, i, &pPage);.
13fe5 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
13fe6 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
13fe7 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f  to commit_phase_
13fe8 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20  one_exit;.      
13fe9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13fea 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
13feb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
13fec 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
13fed 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
13fee 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13fef 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74  OK ) goto commit
13ff0 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b  _phase_one_exit;
13ff1 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
13ff2 20 7d 20 0a 20 20 20 20 20 20 70 50 61 67 65 72   } .      pPager
13ff3 2d 3e 64 62 53 69 7a 65 20 3d 20 64 62 53 69 7a  ->dbSize = dbSiz
13ff4 65 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  e;.    }.#endif.
13ff5 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68  .    /* Write th
13ff6 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
13ff7 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a   name into the j
13ff8 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
13ff9 61 20 6d 61 73 74 65 72 20 0a 20 20 20 20 2a 2a  a master .    **
13ffa 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
13ffb 6d 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  me has already b
13ffc 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74  een written to t
13ffd 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  he journal file,
13ffe 20 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a   .    ** or if z
13fff 4d 61 73 74 65 72 20 69 73 20 4e 55 4c 4c 20 28  Master is NULL (
14000 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  no master journa
14001 6c 29 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61  l), then this ca
14002 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20  ll is a no-op.. 
14003 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77     */.    rc = w
14004 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61  riteMasterJourna
14005 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65  l(pPager, zMaste
14006 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
14007 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
14008 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e   commit_phase_on
14009 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20  e_exit;..    /* 
1400a 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Sync the journal
1400b 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 61 74   file. If the at
1400c 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69  omic-update opti
1400d 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 65 69 6e  mization is bein
1400e 67 0a 20 20 20 20 2a 2a 20 75 73 65 64 2c 20 74  g.    ** used, t
1400f 68 69 73 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f  his call will no
14010 74 20 63 72 65 61 74 65 20 74 68 65 20 6a 6f 75  t create the jou
14011 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 70 65 72  rnal file or per
14012 66 6f 72 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20  form any.    ** 
14013 72 65 61 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a  real IO..    */.
14014 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75      rc = syncJou
14015 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20  rnal(pPager);.  
14016 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
14017 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69  _OK ) goto commi
14018 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74  t_phase_one_exit
14019 3b 0a 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20  ;..    /* Write 
1401a 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20  all dirty pages 
1401b 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
1401c 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20  file. */.    rc 
1401d 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61  = pager_write_pa
1401e 67 65 6c 69 73 74 28 73 71 6c 69 74 65 33 50 63  gelist(sqlite3Pc
1401f 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50  acheDirtyList(pP
14020 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 29 3b  ager->pPCache));
14021 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
14022 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
14023 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
14024 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44  TE_IOERR_BLOCKED
14025 20 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63   );.      goto c
14026 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f  ommit_phase_one_
14027 65 78 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  exit;.    }.    
14028 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
14029 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50  anAll(pPager->pP
1402a 43 61 63 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20  Cache);..    /* 
1402b 49 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64  If the file on d
1402c 69 73 6b 20 69 73 20 6e 6f 74 20 74 68 65 20 73  isk is not the s
1402d 61 6d 65 20 73 69 7a 65 20 61 73 20 74 68 65 20  ame size as the 
1402e 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c 0a  database image,.
1402f 20 20 20 20 2a 2a 20 74 68 65 6e 20 75 73 65 20      ** then use 
14030 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 20 74  pager_truncate t
14031 6f 20 67 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b  o grow or shrink
14032 20 74 68 65 20 66 69 6c 65 20 68 65 72 65 2e 0a   the file here..
14033 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
14034 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70  Pager->dbSize!=p
14035 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a  Pager->dbFileSiz
14036 65 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20  e ){.      Pgno 
14037 6e 4e 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64  nNew = pPager->d
14038 62 53 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d  bSize - (pPager-
14039 3e 64 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d  >dbSize==PAGER_M
1403a 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b  J_PGNO(pPager));
1403b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1403c 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
1403d 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b  GER_EXCLUSIVE );
1403e 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
1403f 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65  r_truncate(pPage
14040 72 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20  r, nNew);.      
14041 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
14042 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f  K ) goto commit_
14043 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a  phase_one_exit;.
14044 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69      }..    /* Fi
14045 6e 61 6c 6c 79 2c 20 73 79 6e 63 20 74 68 65 20  nally, sync the 
14046 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a  database file. *
14047 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  /.    if( !pPage
14048 72 2d 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f  r->noSync && !no
14049 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63  Sync ){.      rc
1404a 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
1404b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61  (pPager->fd, pPa
1404c 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29  ger->sync_flags)
1404d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52  ;.    }.    IOTR
1404e 41 43 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c  ACE(("DBSYNC %p\
1404f 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20  n", pPager))..  
14050 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
14051 3d 20 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a  = PAGER_SYNCED;.
14052 20 20 7d 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73    }..commit_phas
14053 65 5f 6f 6e 65 5f 65 78 69 74 3a 0a 20 20 72 65  e_one_exit:.  re
14054 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
14055 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e  ** When this fun
14056 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
14057 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
14058 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70  le has been comp
14059 6c 65 74 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65  letely.** update
1405a 64 20 74 6f 20 72 65 66 6c 65 63 74 20 74 68 65  d to reflect the
1405b 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79   changes made by
1405c 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
1405d 6e 73 61 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20  nsaction and.** 
1405e 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20  synced to disk. 
1405f 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  The journal file
14060 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 69 6e   still exists in
14061 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
14062 20 0a 2a 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64   .** though, and
14063 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63   if a failure oc
14064 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69  curs at this poi
14065 6e 74 20 69 74 20 77 69 6c 6c 20 65 76 65 6e 74  nt it will event
14066 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64  ually.** be used
14067 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61   as a hot-journa
14068 6c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  l and the curren
14069 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f  t transaction ro
1406a 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  lled back..**.**
1406b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   This function f
1406c 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75  inalizes the jou
1406d 72 6e 61 6c 20 66 69 6c 65 2c 20 65 69 74 68 65  rnal file, eithe
1406e 72 20 62 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a  r by deleting, .
1406f 2a 2a 20 74 72 75 6e 63 61 74 69 6e 67 20 6f 72  ** truncating or
14070 20 70 61 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69   partially zeroi
14071 6e 67 20 69 74 2c 20 73 6f 20 74 68 61 74 20 69  ng it, so that i
14072 74 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64  t cannot be used
14073 20 0a 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75   .** for hot-jou
14074 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f  rnal rollback. O
14075 6e 63 65 20 74 68 69 73 20 69 73 20 64 6f 6e 65  nce this is done
14076 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
14077 20 69 73 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62   is.** irrevocab
14078 6c 79 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a  ly committed..**
14079 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
1407a 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72  occurs, an IO er
1407b 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
1407c 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 67  rned and the pag
1407d 65 72 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f  er.** moves into
1407e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
1407f 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
14080 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
14081 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
14082 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
14083 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
14084 65 54 77 6f 28 50 61 67 65 72 20 2a 70 50 61 67  eTwo(Pager *pPag
14085 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
14086 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
14087 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
14088 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
14089 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65   /* This routine
1408a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63   should not be c
1408b 61 6c 6c 65 64 20 69 66 20 61 20 70 72 69 6f 72  alled if a prior
1408c 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
1408d 72 65 64 2e 0a 20 20 2a 2a 20 42 75 74 20 69 66  red..  ** But if
1408e 20 28 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e   (due to a codin
1408f 67 20 65 72 72 6f 72 20 65 6c 73 65 77 68 65 72  g error elsewher
14090 65 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d 29  e in the system)
14091 20 69 74 20 64 6f 65 73 20 67 65 74 0a 20 20 2a   it does get.  *
14092 2a 20 63 61 6c 6c 65 64 2c 20 6a 75 73 74 20 72  * called, just r
14093 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 65  eturn the same e
14094 72 72 6f 72 20 63 6f 64 65 20 77 69 74 68 6f 75  rror code withou
14095 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67  t doing anything
14096 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52  . */.  if( NEVER
14097 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65  (pPager->errCode
14098 29 20 29 20 72 65 74 75 72 6e 20 70 50 61 67 65  ) ) return pPage
14099 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f  r->errCode;..  /
1409a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1409b 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61  should not be ca
1409c 6c 6c 65 64 20 69 66 20 74 68 65 20 70 61 67 65  lled if the page
1409d 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c  r is not in at l
1409e 65 61 73 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f  east.  ** PAGER_
1409f 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2e 20  RESERVED state. 
140a0 41 6e 64 20 69 6e 64 65 65 64 20 53 51 4c 69 74  And indeed SQLit
140a1 65 20 6e 65 76 65 72 20 64 6f 65 73 20 74 68 69  e never does thi
140a2 73 2e 20 42 75 74 20 69 74 20 69 73 0a 20 20 2a  s. But it is.  *
140a3 2a 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20 74  * nice to have t
140a4 68 69 73 20 64 65 66 65 6e 73 69 76 65 20 74 65  his defensive te
140a5 73 74 20 68 65 72 65 20 61 6e 79 77 61 79 2e 0a  st here anyway..
140a6 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52    */.  if( NEVER
140a7 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50  (pPager->state<P
140a8 41 47 45 52 5f 52 45 53 45 52 56 45 44 29 20 29  AGER_RESERVED) )
140a9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
140aa 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 41 6e 20 6f  RROR;..  /* An o
140ab 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 49 66 20  ptimization. If 
140ac 74 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73  the database was
140ad 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f   not actually mo
140ae 64 69 66 69 65 64 20 64 75 72 69 6e 67 0a 20 20  dified during.  
140af 2a 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  ** this transact
140b0 69 6f 6e 2c 20 74 68 65 20 70 61 67 65 72 20 69  ion, the pager i
140b1 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63  s running in exc
140b2 6c 75 73 69 76 65 2d 6d 6f 64 65 20 61 6e 64 20  lusive-mode and 
140b3 69 73 0a 20 20 2a 2a 20 75 73 69 6e 67 20 70 65  is.  ** using pe
140b4 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c  rsistent journal
140b5 73 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  s, then this fun
140b6 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
140b7 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
140b8 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75  start of the jou
140b9 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e  rnal file curren
140ba 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 61 20 73  tly contains a s
140bb 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20  ingle journal . 
140bc 20 2a 2a 20 68 65 61 64 65 72 20 77 69 74 68 20   ** header with 
140bd 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 73  the nRec field s
140be 65 74 20 74 6f 20 30 2e 20 49 66 20 73 75 63 68  et to 0. If such
140bf 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73   a journal is us
140c0 65 64 20 61 73 0a 20 20 2a 2a 20 61 20 68 6f 74  ed as.  ** a hot
140c1 2d 6a 6f 75 72 6e 61 6c 20 64 75 72 69 6e 67 20  -journal during 
140c2 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c  hot-journal roll
140c3 62 61 63 6b 2c 20 30 20 63 68 61 6e 67 65 73 20  back, 0 changes 
140c4 77 69 6c 6c 20 62 65 20 6d 61 64 65 0a 20 20 2a  will be made.  *
140c5 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  * to the databas
140c6 65 20 66 69 6c 65 2e 20 53 6f 20 74 68 65 72 65  e file. So there
140c7 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 7a   is no need to z
140c8 65 72 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ero the journal 
140c9 0a 20 20 2a 2a 20 68 65 61 64 65 72 2e 20 53 69  .  ** header. Si
140ca 6e 63 65 20 74 68 65 20 70 61 67 65 72 20 69 73  nce the pager is
140cb 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
140cc 64 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20  de, there is no 
140cd 6e 65 65 64 0a 20 20 2a 2a 20 74 6f 20 64 72 6f  need.  ** to dro
140ce 70 20 61 6e 79 20 6c 6f 63 6b 73 20 65 69 74 68  p any locks eith
140cf 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  er..  */.  if( p
140d0 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65  Pager->dbModifie
140d1 64 3d 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e  d==0 && pPager->
140d2 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20  exclusiveMode . 
140d3 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75    && pPager->jou
140d4 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
140d5 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53  JOURNALMODE_PERS
140d6 49 53 54 0a 20 20 29 7b 0a 20 20 20 20 61 73 73  IST.  ){.    ass
140d7 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
140d8 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c  rnalOff==JOURNAL
140d9 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
140da 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
140db 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
140dc 50 41 47 45 52 54 52 41 43 45 28 28 22 43 4f 4d  PAGERTRACE(("COM
140dd 4d 49 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  MIT %d\n", PAGER
140de 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
140df 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
140e0 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e  state==PAGER_SYN
140e1 43 45 44 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20  CED || MEMDB || 
140e2 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66  !pPager->dbModif
140e3 69 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 70 61  ied );.  rc = pa
140e4 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
140e5 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67  ion(pPager, pPag
140e6 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a  er->setMaster);.
140e7 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65    return pager_e
140e8 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
140e9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62  ;.}../*.** Rollb
140ea 61 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 2e  ack all changes.
140eb 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 61   The database fa
140ec 6c 6c 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45  lls back to PAGE
140ed 52 5f 53 48 41 52 45 44 20 6d 6f 64 65 2e 0a 2a  R_SHARED mode..*
140ee 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
140ef 6f 6e 20 70 65 72 66 6f 72 6d 73 20 74 77 6f 20  on performs two 
140f0 74 61 73 6b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  tasks:.**.**   1
140f1 29 20 49 74 20 72 6f 6c 6c 73 20 62 61 63 6b 20  ) It rolls back 
140f2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
140f3 2c 20 72 65 73 74 6f 72 69 6e 67 20 61 6c 6c 20  , restoring all 
140f4 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
140f5 64 20 0a 2a 2a 20 20 20 20 20 20 69 6e 2d 6d 65  d .**      in-me
140f6 6d 6f 72 79 20 63 61 63 68 65 20 70 61 67 65 73  mory cache pages
140f7 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 74 68   to the state th
140f8 65 79 20 77 65 72 65 20 69 6e 20 77 68 65 6e 20  ey were in when 
140f9 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  the transaction.
140fa 2a 2a 20 20 20 20 20 20 77 61 73 20 6f 70 65 6e  **      was open
140fb 65 64 2c 20 61 6e 64 0a 2a 2a 20 20 20 32 29 20  ed, and.**   2) 
140fc 49 74 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65  It finalizes the
140fd 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73   journal file, s
140fe 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74  o that it is not
140ff 20 75 73 65 64 20 66 6f 72 20 68 6f 74 0a 2a 2a   used for hot.**
14100 20 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b 20 61        rollback a
14101 74 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74  t any point in t
14102 68 65 20 66 75 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  he future..**.**
14103 20 73 75 62 6a 65 63 74 20 74 6f 20 74 68 65 20   subject to the 
14104 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 61 6c 69 66  following qualif
14105 69 63 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20  ications:.**.** 
14106 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * If the journal
14107 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 79 65 74   file is not yet
14108 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20   open when this 
14109 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1410a 65 64 2c 0a 2a 2a 20 20 20 74 68 65 6e 20 6f 6e  ed,.**   then on
1410b 6c 79 20 28 32 29 20 69 73 20 70 65 72 66 6f 72  ly (2) is perfor
1410c 6d 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  med. In this cas
1410d 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6a 6f  e there is no jo
1410e 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20  urnal file.**   
1410f 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 0a 2a 2a  to roll back..**
14110 0a 2a 2a 20 2a 20 49 66 20 69 6e 20 61 6e 20 65  .** * If in an e
14111 72 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72  rror state other
14112 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c   than SQLITE_FUL
14113 4c 2c 20 74 68 65 6e 20 74 61 73 6b 20 28 31 29  L, then task (1)
14114 20 69 73 20 0a 2a 2a 20 20 20 70 65 72 66 6f 72   is .**   perfor
14115 6d 65 64 2e 20 49 66 20 73 75 63 63 65 73 73 66  med. If successf
14116 75 6c 2c 20 74 61 73 6b 20 28 32 29 2e 20 52 65  ul, task (2). Re
14117 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20  gardless of the 
14118 6f 75 74 63 6f 6d 65 0a 2a 2a 20 20 20 6f 66 20  outcome.**   of 
14119 65 69 74 68 65 72 2c 20 74 68 65 20 65 72 72 6f  either, the erro
1411a 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f  r state error co
1411b 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  de is returned t
1411c 6f 20 74 68 65 20 63 61 6c 6c 65 72 0a 2a 2a 20  o the caller.** 
1411d 20 20 28 69 2e 65 2e 20 65 69 74 68 65 72 20 53    (i.e. either S
1411e 51 4c 49 54 45 5f 49 4f 45 52 52 20 6f 72 20 53  QLITE_IOERR or S
1411f 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a  QLITE_CORRUPT)..
14120 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 70  **.** * If the p
14121 61 67 65 72 20 69 73 20 69 6e 20 50 41 47 45 52  ager is in PAGER
14122 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c  _RESERVED state,
14123 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31   then attempt (1
14124 29 2e 20 57 68 65 74 68 65 72 0a 2a 2a 20 20 20  ). Whether.**   
14125 6f 72 20 6e 6f 74 20 28 31 29 20 69 73 20 73 75  or not (1) is su
14126 63 63 75 73 73 66 75 6c 2c 20 61 6c 73 6f 20 61  ccussful, also a
14127 74 74 65 6d 70 74 20 28 32 29 2e 20 49 66 20 73  ttempt (2). If s
14128 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72  uccessful, retur
14129 6e 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4f 4b  n.**   SQLITE_OK
1412a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 65 6e 74  . Otherwise, ent
1412b 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  er the error sta
1412c 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  te and return th
1412d 65 20 66 69 72 73 74 20 0a 2a 2a 20 20 20 65 72  e first .**   er
1412e 72 6f 72 20 63 6f 64 65 20 65 6e 63 6f 75 6e 74  ror code encount
1412f 65 72 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49  ered. .**.**   I
14130 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72  n this case ther
14131 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74  e is no chance t
14132 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
14133 20 77 61 73 20 77 72 69 74 74 65 6e 20 74 6f 2e   was written to.
14134 20 0a 2a 2a 20 20 20 53 6f 20 69 73 20 73 61 66   .**   So is saf
14135 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68  e to finalize th
14136 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  e journal file e
14137 76 65 6e 20 69 66 20 74 68 65 20 70 6c 61 79 62  ven if the playb
14138 61 63 6b 20 0a 2a 2a 20 20 20 28 6f 70 65 72 61  ack .**   (opera
14139 74 69 6f 6e 20 31 29 20 66 61 69 6c 65 64 2e 20  tion 1) failed. 
1413a 48 6f 77 65 76 65 72 20 74 68 65 20 70 61 67 65  However the page
1413b 72 20 6d 75 73 74 20 65 6e 74 65 72 20 74 68 65  r must enter the
1413c 20 65 72 72 6f 72 20 73 74 61 74 65 0a 2a 2a 20   error state.** 
1413d 20 20 61 73 20 74 68 65 20 63 6f 6e 74 65 6e 74    as the content
1413e 73 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  s of the in-memo
1413f 72 79 20 63 61 63 68 65 20 61 72 65 20 6e 6f 77  ry cache are now
14140 20 73 75 73 70 65 63 74 2e 0a 2a 2a 0a 2a 2a 20   suspect..**.** 
14141 2a 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 69 6e  * Finally, if in
14142 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
14143 20 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74   state, then att
14144 65 6d 70 74 20 28 31 29 2e 20 4f 6e 6c 79 0a 2a  empt (1). Only.*
14145 2a 20 20 20 61 74 74 65 6d 70 74 20 28 32 29 20  *   attempt (2) 
14146 69 66 20 28 31 29 20 69 73 20 73 75 63 63 65 73  if (1) is succes
14147 73 66 75 6c 2e 20 52 65 74 75 72 6e 20 53 51 4c  sful. Return SQL
14148 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73  ITE_OK if succes
14149 73 66 75 6c 2c 0a 2a 2a 20 20 20 6f 74 68 65 72  sful,.**   other
1414a 77 69 73 65 20 65 6e 74 65 72 20 74 68 65 20 65  wise enter the e
1414b 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72  rror state and r
1414c 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20  eturn the error 
1414d 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a  code from the .*
1414e 2a 20 20 20 66 61 69 6c 69 6e 67 20 6f 70 65 72  *   failing oper
1414f 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49  ation..**.**   I
14150 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
14151 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61  database file ma
14152 79 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74  y have been writ
14153 74 65 6e 20 74 6f 2e 20 53 6f 20 69 66 20 74 68  ten to. So if th
14154 65 0a 2a 2a 20 20 20 70 6c 61 79 62 61 63 6b 20  e.**   playback 
14155 6f 70 65 72 61 74 69 6f 6e 20 64 69 64 20 6e 6f  operation did no
14156 74 20 73 75 63 63 65 65 64 20 69 74 20 77 6f 75  t succeed it wou
14157 6c 64 20 6e 6f 74 20 62 65 20 73 61 66 65 20 74  ld not be safe t
14158 6f 20 66 69 6e 61 6c 69 7a 65 0a 2a 2a 20 20 20  o finalize.**   
14159 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1415a 2e 20 49 74 20 6e 65 65 64 73 20 74 6f 20 62 65  . It needs to be
1415b 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c   left in the fil
1415c 65 2d 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74  e-system so that
1415d 0a 2a 2a 20 20 20 73 6f 6d 65 20 6f 74 68 65 72  .**   some other
1415e 20 70 72 6f 63 65 73 73 20 63 61 6e 20 75 73 65   process can use
1415f 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 20 74   it to restore t
14160 68 65 20 64 61 74 61 62 61 73 65 20 73 74 61 74  he database stat
14161 65 20 28 62 79 0a 2a 2a 20 20 20 68 6f 74 2d 6a  e (by.**   hot-j
14162 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29  ournal rollback)
14163 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14164 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
14165 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67  agerRollback(Pag
14166 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
14167 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
14168 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K;              
14169 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
1416a 64 65 20 2a 2f 0a 20 20 50 41 47 45 52 54 52 41  de */.  PAGERTRA
1416b 43 45 28 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64  CE(("ROLLBACK %d
1416c 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
1416d 67 65 72 29 29 29 3b 0a 20 20 69 66 28 20 21 70  ger)));.  if( !p
1416e 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65  Pager->dbModifie
1416f 64 20 7c 7c 20 21 69 73 4f 70 65 6e 28 70 50 61  d || !isOpen(pPa
14170 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20  ger->jfd) ){.   
14171 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f   rc = pager_end_
14172 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67  transaction(pPag
14173 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d  er, pPager->setM
14174 61 73 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20  aster);.  }else 
14175 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
14176 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65  ode && pPager->e
14177 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46  rrCode!=SQLITE_F
14178 55 4c 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ULL ){.    if( p
14179 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
1417a 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b  GER_EXCLUSIVE ){
1417b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61  .      pager_pla
1417c 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29  yback(pPager, 0)
1417d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
1417e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
1417f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
14180 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
14181 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  ==PAGER_RESERVED
14182 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63   ){.      int rc
14183 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  2;.      rc = pa
14184 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61  ger_playback(pPa
14185 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72  ger, 0);.      r
14186 63 32 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74  c2 = pager_end_t
14187 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65  ransaction(pPage
14188 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  r, pPager->setMa
14189 73 74 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28  ster);.      if(
1418a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1418b 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
1418c 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c2;.      }.    
1418d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
1418e 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
1418f 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20  (pPager, 0);.   
14190 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d   }..    if( !MEM
14191 44 42 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  DB ){.      pPag
14192 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
14193 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 0;.    }..    
14194 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  /* If an error o
14195 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 20 52  ccurs during a R
14196 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20  OLLBACK, we can 
14197 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20  no longer trust 
14198 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a  the pager.    **
14199 20 63 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20   cache. So call 
1419a 70 61 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e  pager_error() on
1419b 20 74 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20   the way out to 
1419c 6d 61 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a  make any error .
1419d 20 20 20 20 2a 2a 20 70 65 72 73 69 73 74 65 6e      ** persisten
1419e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  t..    */.    rc
1419f 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70   = pager_error(p
141a0 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a  Pager, rc);.  }.
141a1 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
141a2 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
141a3 45 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  E if the databas
141a4 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64  e file is opened
141a5 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74   read-only.  Ret
141a6 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20  urn FALSE.** if 
141a7 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
141a8 28 69 6e 20 74 68 65 6f 72 79 29 20 77 72 69 74  (in theory) writ
141a9 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
141aa 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74  PRIVATE u8 sqlit
141ab 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c  e3PagerIsreadonl
141ac 79 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  y(Pager *pPager)
141ad 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
141ae 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a  r->readOnly;.}..
141af 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
141b0 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   number of refer
141b1 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67  ences to the pag
141b2 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
141b3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
141b4 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50  3PagerRefcount(P
141b5 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
141b6 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50   return sqlite3P
141b7 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
141b8 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
141b9 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
141ba 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
141bb 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
141bc 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a  specified page..
141bd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
141be 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
141bf 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44  erPageRefcount(D
141c0 62 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20  bPage *pPage){. 
141c1 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50   return sqlite3P
141c2 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e  cachePageRefcoun
141c3 74 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66  t(pPage);.}..#if
141c4 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
141c5 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
141c6 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  ne is used for t
141c7 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79  esting and analy
141c8 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  sis only..*/.SQL
141c9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
141ca 2a 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61  *sqlite3PagerSta
141cb 74 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ts(Pager *pPager
141cc 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  ){.  static int 
141cd 61 5b 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20  a[11];.  a[0] = 
141ce 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
141cf 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50  Count(pPager->pP
141d0 43 61 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d  Cache);.  a[1] =
141d1 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61   sqlite3PcachePa
141d2 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e  gecount(pPager->
141d3 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d  pPCache);.  a[2]
141d4 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65   = sqlite3Pcache
141d5 47 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61  GetCachesize(pPa
141d6 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20  ger->pPCache);. 
141d7 20 61 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[3] = pPager->
141d8 64 62 53 69 7a 65 56 61 6c 69 64 20 3f 20 28 69  dbSizeValid ? (i
141d9 6e 74 29 20 70 50 61 67 65 72 2d 3e 64 62 53 69  nt) pPager->dbSi
141da 7a 65 20 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20  ze : -1;.  a[4] 
141db 3d 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3b  = pPager->state;
141dc 0a 20 20 61 5b 35 5d 20 3d 20 70 50 61 67 65 72  .  a[5] = pPager
141dd 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36  ->errCode;.  a[6
141de 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 48 69 74  ] = pPager->nHit
141df 3b 0a 20 20 61 5b 37 5d 20 3d 20 70 50 61 67 65  ;.  a[7] = pPage
141e0 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d  r->nMiss;.  a[8]
141e1 20 3d 20 30 3b 20 20 2f 2a 20 55 73 65 64 20 74   = 0;  /* Used t
141e2 6f 20 62 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76  o be pPager->nOv
141e3 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70  fl */.  a[9] = p
141e4 50 61 67 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20  Pager->nRead;.  
141e5 61 5b 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e  a[10] = pPager->
141e6 6e 57 72 69 74 65 3b 0a 20 20 72 65 74 75 72 6e  nWrite;.  return
141e7 20 61 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a   a;.}.#endif../*
141e8 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
141e9 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e  if this is an in
141ea 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 2e 0a 2a  -memory pager..*
141eb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
141ec 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
141ed 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 20 2a  rIsMemdb(Pager *
141ee 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
141ef 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a  n MEMDB;.}../*.*
141f0 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
141f1 72 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20  re are at least 
141f2 6e 53 61 76 65 70 6f 69 6e 74 20 73 61 76 65 70  nSavepoint savep
141f3 6f 69 6e 74 73 20 6f 70 65 6e 2e 20 49 66 20 74  oints open. If t
141f4 68 65 72 65 20 61 72 65 0a 2a 2a 20 63 75 72 72  here are.** curr
141f5 65 6e 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20  ently less than 
141f6 6e 53 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e  nSavepoints open
141f7 2c 20 74 68 65 6e 20 6f 70 65 6e 20 6f 6e 65 20  , then open one 
141f8 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e  or more savepoin
141f9 74 73 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 75 70  ts.** to make up
141fa 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 2e   the difference.
141fb 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   If the number o
141fc 66 20 73 61 76 65 70 6f 69 6e 74 73 20 69 73 20  f savepoints is 
141fd 61 6c 72 65 61 64 79 0a 2a 2a 20 65 71 75 61 6c  already.** equal
141fe 20 74 6f 20 6e 53 61 76 65 70 6f 69 6e 74 2c 20   to nSavepoint, 
141ff 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
14200 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
14201 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79  *.** If a memory
14202 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
14203 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  s, SQLITE_NOMEM 
14204 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
14205 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63  an error .** occ
14206 75 72 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e  urs while openin
14207 67 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  g the sub-journa
14208 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 61 6e 20  l file, then an 
14209 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  IO error code is
1420a 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74  .** returned. Ot
1420b 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f  herwise, SQLITE_
1420c 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  OK..*/.SQLITE_PR
1420d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1420e 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f  3PagerOpenSavepo
1420f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  int(Pager *pPage
14210 72 2c 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e  r, int nSavepoin
14211 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
14212 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
14213 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14214 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
14215 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74  /.  int nCurrent
14216 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65   = pPager->nSave
14217 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a  point;        /*
14218 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20   Current number 
14219 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f  of savepoints */
1421a 0a 0a 20 20 69 66 28 20 6e 53 61 76 65 70 6f 69  ..  if( nSavepoi
1421b 6e 74 3e 6e 43 75 72 72 65 6e 74 20 26 26 20 70  nt>nCurrent && p
1421c 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61  Pager->useJourna
1421d 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b  l ){.    int ii;
1421e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1421f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14220 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
14221 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67  iable */.    Pag
14222 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65  erSavepoint *aNe
14223 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
14224 20 20 20 20 20 2f 2a 20 4e 65 77 20 50 61 67 65       /* New Page
14225 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 72 72  r.aSavepoint arr
14226 61 79 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 45 69  ay */..    /* Ei
14227 74 68 65 72 20 74 68 65 72 65 20 69 73 20 6e 6f  ther there is no
14228 20 61 63 74 69 76 65 20 6a 6f 75 72 6e 61 6c 20   active journal 
14229 6f 72 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  or the sub-journ
1422a 61 6c 20 69 73 20 6f 70 65 6e 20 6f 72 20 0a 20  al is open or . 
1422b 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61     ** the journa
1422c 6c 20 69 73 20 61 6c 77 61 79 73 20 73 74 6f 72  l is always stor
1422d 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 2a 2f 0a  ed in memory */.
1422e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
1422f 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d  er->nSavepoint==
14230 30 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67  0 || isOpen(pPag
14231 65 72 2d 3e 73 6a 66 64 29 20 7c 7c 0a 20 20 20  er->sjfd) ||.   
14232 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d           pPager-
14233 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
14234 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
14235 4d 45 4d 4f 52 59 20 29 3b 0a 0a 20 20 20 20 2f  MEMORY );..    /
14236 2a 20 47 72 6f 77 20 74 68 65 20 50 61 67 65 72  * Grow the Pager
14237 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61  .aSavepoint arra
14238 79 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28  y using realloc(
14239 29 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ). Return SQLITE
1423a 5f 4e 4f 4d 45 4d 0a 20 20 20 20 2a 2a 20 69 66  _NOMEM.    ** if
1423b 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
1423c 66 61 69 6c 73 2e 20 4f 74 68 65 72 77 69 73 65  fails. Otherwise
1423d 2c 20 7a 65 72 6f 20 74 68 65 20 6e 65 77 20 70  , zero the new p
1423e 6f 72 74 69 6f 6e 20 69 6e 20 63 61 73 65 20 61  ortion in case a
1423f 20 0a 20 20 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20   .    ** malloc 
14240 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77  failure occurs w
14241 68 69 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67 20  hile populating 
14242 69 74 20 69 6e 20 74 68 65 20 66 6f 72 28 2e 2e  it in the for(..
14243 2e 29 20 6c 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20  .) loop below.. 
14244 20 20 20 2a 2f 0a 20 20 20 20 61 4e 65 77 20 3d     */.    aNew =
14245 20 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74   (PagerSavepoint
14246 20 2a 29 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f   *)sqlite3Reallo
14247 63 28 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  c(.        pPage
14248 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 2c 20 73  r->aSavepoint, s
14249 69 7a 65 6f 66 28 50 61 67 65 72 53 61 76 65 70  izeof(PagerSavep
1424a 6f 69 6e 74 29 2a 6e 53 61 76 65 70 6f 69 6e 74  oint)*nSavepoint
1424b 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20  .    );.    if( 
1424c 21 61 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 72  !aNew ){.      r
1424d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1424e 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65  EM;.    }.    me
1424f 6d 73 65 74 28 26 61 4e 65 77 5b 6e 43 75 72 72  mset(&aNew[nCurr
14250 65 6e 74 5d 2c 20 30 2c 20 28 6e 53 61 76 65 70  ent], 0, (nSavep
14251 6f 69 6e 74 2d 6e 43 75 72 72 65 6e 74 29 20 2a  oint-nCurrent) *
14252 20 73 69 7a 65 6f 66 28 50 61 67 65 72 53 61 76   sizeof(PagerSav
14253 65 70 6f 69 6e 74 29 29 3b 0a 20 20 20 20 70 50  epoint));.    pP
14254 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
14255 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 70 50 61   = aNew;.    pPa
14256 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20  ger->nSavepoint 
14257 3d 20 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 0a 20  = nSavepoint;.. 
14258 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74     /* Populate t
14259 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  he PagerSavepoin
1425a 74 20 73 74 72 75 63 74 75 72 65 73 20 6a 75 73  t structures jus
1425b 74 20 61 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a  t allocated. */.
1425c 20 20 20 20 66 6f 72 28 69 69 3d 6e 43 75 72 72      for(ii=nCurr
1425d 65 6e 74 3b 20 69 69 3c 6e 53 61 76 65 70 6f 69  ent; ii<nSavepoi
1425e 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  nt; ii++){.     
1425f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
14260 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a  >dbSizeValid );.
14261 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 6e        aNew[ii].n
14262 4f 72 69 67 20 3d 20 70 50 61 67 65 72 2d 3e 64  Orig = pPager->d
14263 62 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28  bSize;.      if(
14264 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
14265 6a 66 64 29 20 26 26 20 41 4c 57 41 59 53 28 70  jfd) && ALWAYS(p
14266 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
14267 66 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  f>0) ){.        
14268 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74  aNew[ii].iOffset
14269 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
1426a 61 6c 4f 66 66 3b 0a 20 20 20 20 20 20 7d 65 6c  alOff;.      }el
1426b 73 65 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 77  se{.        aNew
1426c 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 4a  [ii].iOffset = J
1426d 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
1426e 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ager);.      }. 
1426f 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 53       aNew[ii].iS
14270 75 62 52 65 63 20 3d 20 70 50 61 67 65 72 2d 3e  ubRec = pPager->
14271 6e 53 75 62 52 65 63 3b 0a 20 20 20 20 20 20 61  nSubRec;.      a
14272 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70  New[ii].pInSavep
14273 6f 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 69  oint = sqlite3Bi
14274 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65  tvecCreate(pPage
14275 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 20 20  r->dbSize);.    
14276 20 20 69 66 28 20 21 61 4e 65 77 5b 69 69 5d 2e    if( !aNew[ii].
14277 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a  pInSavepoint ){.
14278 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
14279 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1427a 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
1427b 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 75 62 2d  /* Open the sub-
1427c 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69  journal, if it i
1427d 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70  s not already op
1427e 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20 72 63 20  ened. */.    rc 
1427f 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c  = openSubJournal
14280 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 61 73  (pPager);.    as
14281 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73  sertTruncateCons
14282 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a  traint(pPager);.
14283 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
14284 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
14285 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
14286 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f  ed to rollback o
14287 72 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69  r release (commi
14288 74 29 20 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a  t) a savepoint..
14289 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74  ** The savepoint
1428a 20 74 6f 20 72 65 6c 65 61 73 65 20 6f 72 20 72   to release or r
1428b 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74  ollback need not
1428c 20 62 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63   be the most rec
1428d 65 6e 74 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65  ently .** create
1428e 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a  d savepoint..**.
1428f 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 6f 70 20  ** Parameter op 
14290 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72  is always either
14291 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42   SAVEPOINT_ROLLB
14292 41 43 4b 20 6f 72 20 53 41 56 45 50 4f 49 4e 54  ACK or SAVEPOINT
14293 5f 52 45 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20  _RELEASE..** If 
14294 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f  it is SAVEPOINT_
14295 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 72 65  RELEASE, then re
14296 6c 65 61 73 65 20 61 6e 64 20 64 65 73 74 72 6f  lease and destro
14297 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  y the savepoint 
14298 77 69 74 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53  with.** index iS
14299 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 69 74 20  avepoint. If it 
1429a 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  is SAVEPOINT_ROL
1429b 4c 42 41 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c  LBACK, then roll
1429c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73  back all changes
1429d 0a 2a 2a 20 74 68 61 74 20 68 61 76 65 20 6f 63  .** that have oc
1429e 63 75 72 72 65 64 20 73 69 6e 63 65 20 74 68 65  curred since the
1429f 20 73 70 65 63 69 66 69 65 64 20 73 61 76 65 70   specified savep
142a0 6f 69 6e 74 20 77 61 73 20 63 72 65 61 74 65 64  oint was created
142a1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65  ..**.** The save
142a2 70 6f 69 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63  point to rollbac
142a3 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 69 73 20  k or release is 
142a4 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 70 61  identified by pa
142a5 72 61 6d 65 74 65 72 20 0a 2a 2a 20 69 53 61 76  rameter .** iSav
142a6 65 70 6f 69 6e 74 2e 20 41 20 76 61 6c 75 65 20  epoint. A value 
142a7 6f 66 20 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70  of 0 means to op
142a8 65 72 61 74 65 20 6f 6e 20 74 68 65 20 6f 75 74  erate on the out
142a9 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74  ermost savepoint
142aa 0a 2a 2a 20 28 74 68 65 20 66 69 72 73 74 20 63  .** (the first c
142ab 72 65 61 74 65 64 29 2e 20 41 20 76 61 6c 75 65  reated). A value
142ac 20 6f 66 20 28 50 61 67 65 72 2e 6e 53 61 76 65   of (Pager.nSave
142ad 70 6f 69 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f  point-1) means o
142ae 70 65 72 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65  perate.** on the
142af 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
142b0 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74  reated savepoint
142b1 2e 20 49 66 20 69 53 61 76 65 70 6f 69 6e 74 20  . If iSavepoint 
142b2 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a  is greater than.
142b3 2a 2a 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70  ** (Pager.nSavep
142b4 6f 69 6e 74 2d 31 29 2c 20 74 68 65 6e 20 74 68  oint-1), then th
142b5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
142b6 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66   no-op..**.** If
142b7 20 61 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75   a negative valu
142b8 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74  e is passed to t
142b9 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  his function, th
142ba 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  en the current.*
142bb 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  * transaction is
142bc 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68   rolled back. Th
142bd 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 20  is is different 
142be 74 6f 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73  to calling .** s
142bf 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62  qlite3PagerRollb
142c0 61 63 6b 28 29 20 62 65 63 61 75 73 65 20 74 68  ack() because th
142c1 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
142c2 20 6e 6f 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a   not terminate.*
142c3 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  * the transactio
142c4 6e 20 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20  n or unlock the 
142c5 64 61 74 61 62 61 73 65 2c 20 69 74 20 6a 75 73  database, it jus
142c6 74 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 0a  t restores the .
142c7 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  ** contents of t
142c8 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69  he database to i
142c9 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  ts original stat
142ca 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79  e. .**.** In any
142cb 20 63 61 73 65 2c 20 61 6c 6c 20 73 61 76 65 70   case, all savep
142cc 6f 69 6e 74 73 20 77 69 74 68 20 61 6e 20 69 6e  oints with an in
142cd 64 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e  dex greater than
142ce 20 69 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20   iSavepoint .** 
142cf 61 72 65 20 64 65 73 74 72 6f 79 65 64 2e 20 49  are destroyed. I
142d0 66 20 74 68 69 73 20 69 73 20 61 20 72 65 6c 65  f this is a rele
142d1 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f  ase operation (o
142d2 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  p==SAVEPOINT_REL
142d3 45 41 53 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73  EASE),.** then s
142d4 61 76 65 70 6f 69 6e 74 20 69 53 61 76 65 70 6f  avepoint iSavepo
142d5 69 6e 74 20 69 73 20 61 6c 73 6f 20 64 65 73 74  int is also dest
142d6 72 6f 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  royed..**.** Thi
142d7 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72  s function may r
142d8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
142d9 45 4d 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61  EM if a memory a
142da 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
142db 0a 2a 2a 20 6f 72 20 61 6e 20 49 4f 20 65 72 72  .** or an IO err
142dc 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 49 4f  or code if an IO
142dd 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68   error occurs wh
142de 69 6c 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b  ile rolling back
142df 20 61 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74   a .** savepoint
142e0 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f  . If no errors o
142e1 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ccur, SQLITE_OK 
142e2 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20  is returned..*/ 
142e3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
142e4 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
142e5 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20  Savepoint(Pager 
142e6 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c  *pPager, int op,
142e7 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29   int iSavepoint)
142e8 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
142e9 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72  ITE_OK;..  asser
142ea 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  t( op==SAVEPOINT
142eb 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d  _RELEASE || op==
142ec 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
142ed 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
142ee 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c  iSavepoint>=0 ||
142ef 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52   op==SAVEPOINT_R
142f0 4f 4c 4c 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66  OLLBACK );..  if
142f1 28 20 69 53 61 76 65 70 6f 69 6e 74 3c 70 50 61  ( iSavepoint<pPa
142f2 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20  ger->nSavepoint 
142f3 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20  ){.    int ii;  
142f4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
142f5 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
142f6 2f 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20  /.    int nNew; 
142f7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
142f8 65 72 20 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20  er of remaining 
142f9 73 61 76 65 70 6f 69 6e 74 73 20 61 66 74 65 72  savepoints after
142fa 20 74 68 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20   this op. */..  
142fb 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
142fc 68 6f 77 20 6d 61 6e 79 20 73 61 76 65 70 6f 69  how many savepoi
142fd 6e 74 73 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62  nts will still b
142fe 65 20 61 63 74 69 76 65 20 61 66 74 65 72 20 74  e active after t
142ff 68 69 73 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61  his.    ** opera
14300 74 69 6f 6e 2e 20 53 74 6f 72 65 20 74 68 69 73  tion. Store this
14301 20 76 61 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20   value in nNew. 
14302 54 68 65 6e 20 66 72 65 65 20 72 65 73 6f 75 72  Then free resour
14303 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 0a  ces associated .
14304 20 20 20 20 2a 2a 20 77 69 74 68 20 61 6e 79 20      ** with any 
14305 73 61 76 65 70 6f 69 6e 74 73 20 74 68 61 74 20  savepoints that 
14306 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 62 79  are destroyed by
14307 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e   this operation.
14308 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77  .    */.    nNew
14309 20 3d 20 69 53 61 76 65 70 6f 69 6e 74 20 2b 20   = iSavepoint + 
1430a 28 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  (op==SAVEPOINT_R
1430b 4f 4c 4c 42 41 43 4b 29 3b 0a 20 20 20 20 66 6f  OLLBACK);.    fo
1430c 72 28 69 69 3d 6e 4e 65 77 3b 20 69 69 3c 70 50  r(ii=nNew; ii<pP
1430d 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
1430e 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  ; ii++){.      s
1430f 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
14310 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 61 76  roy(pPager->aSav
14311 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61  epoint[ii].pInSa
14312 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a  vepoint);.    }.
14313 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76      pPager->nSav
14314 65 70 6f 69 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a  epoint = nNew;..
14315 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
14316 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65  s a rollback ope
14317 72 61 74 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b  ration, playback
14318 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 73   the specified s
14319 61 76 65 70 6f 69 6e 74 2e 0a 20 20 20 20 2a 2a  avepoint..    **
1431a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 74 65   If this is a te
1431b 6d 70 2d 66 69 6c 65 2c 20 69 74 20 69 73 20 70  mp-file, it is p
1431c 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65  ossible that the
1431d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
1431e 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 79 65 74  s.    ** not yet
1431f 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 20 49 6e   been opened. In
14320 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65   this case there
14321 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20 63 68   have been no ch
14322 61 6e 67 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20  anges to.    ** 
14323 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
14324 65 2c 20 73 6f 20 74 68 65 20 70 6c 61 79 62 61  e, so the playba
14325 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 63 61 6e  ck operation can
14326 20 62 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 20   be skipped..   
14327 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d   */.    if( op==
14328 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
14329 43 4b 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61  CK && isOpen(pPa
1432a 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20  ger->jfd) ){.   
1432b 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e     PagerSavepoin
1432c 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 20 3d 20  t *pSavepoint = 
1432d 28 6e 4e 65 77 3d 3d 30 29 3f 30 3a 26 70 50 61  (nNew==0)?0:&pPa
1432e 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b  ger->aSavepoint[
1432f 6e 4e 65 77 2d 31 5d 3b 0a 20 20 20 20 20 20 72  nNew-1];.      r
14330 63 20 3d 20 70 61 67 65 72 50 6c 61 79 62 61 63  c = pagerPlaybac
14331 6b 53 61 76 65 70 6f 69 6e 74 28 70 50 61 67 65  kSavepoint(pPage
14332 72 2c 20 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a  r, pSavepoint);.
14333 20 20 20 20 20 20 61 73 73 65 72 74 28 72 63 21        assert(rc!
14334 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 3b 0a 20  =SQLITE_DONE);. 
14335 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
14336 66 20 74 68 69 73 20 69 73 20 61 20 72 65 6c 65  f this is a rele
14337 61 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72  ase of the outer
14338 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 2c 20  most savepoint, 
14339 74 72 75 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a  truncate .    **
1433a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
1433b 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69   to zero bytes i
1433c 6e 20 73 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69  n size. */.    i
1433d 66 28 20 6e 4e 65 77 3d 3d 30 20 26 26 20 6f 70  f( nNew==0 && op
1433e 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  ==SAVEPOINT_RELE
1433f 41 53 45 20 26 26 20 69 73 4f 70 65 6e 28 70 50  ASE && isOpen(pP
14340 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20  ager->sjfd) ){. 
14341 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d       assert( rc=
14342 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
14343 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14344 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  OsTruncate(pPage
14345 72 2d 3e 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20  r->sjfd, 0);.   
14346 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52     pPager->nSubR
14347 65 63 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ec = 0;.    }.  
14348 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
14349 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1434a 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65  he full pathname
1434b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1434c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   file..*/.SQLITE
1434d 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
1434e 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65  har *sqlite3Page
1434f 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 20  rFilename(Pager 
14350 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
14351 72 6e 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  rn pPager->zFile
14352 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  name;.}../*.** R
14353 65 74 75 72 6e 20 74 68 65 20 56 46 53 20 73 74  eturn the VFS st
14354 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20  ructure for the 
14355 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pager..*/.SQLITE
14356 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
14357 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
14358 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65  te3PagerVfs(Page
14359 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
1435a 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 56 66  turn pPager->pVf
1435b 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  s;.}../*.** Retu
1435c 72 6e 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64  rn the file hand
1435d 6c 65 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  le for the datab
1435e 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61  ase file associa
1435f 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ted.** with the 
14360 70 61 67 65 72 2e 20 20 54 68 69 73 20 6d 69 67  pager.  This mig
14361 68 74 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69  ht return NULL i
14362 66 20 74 68 65 20 66 69 6c 65 20 68 61 73 0a 2a  f the file has.*
14363 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f  * not yet been o
14364 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pened..*/.SQLITE
14365 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
14366 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 50 61  _file *sqlite3Pa
14367 67 65 72 46 69 6c 65 28 50 61 67 65 72 20 2a 70  gerFile(Pager *p
14368 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
14369 20 70 50 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a   pPager->fd;.}..
1436a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1436b 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f   full pathname o
1436c 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
1436d 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
1436e 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
1436f 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f   *sqlite3PagerJo
14370 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 20  urnalname(Pager 
14371 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
14372 72 6e 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  rn pPager->zJour
14373 6e 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  nal;.}../*.** Re
14374 74 75 72 6e 20 74 72 75 65 20 69 66 20 66 73 79  turn true if fsy
14375 6e 63 28 29 20 63 61 6c 6c 73 20 61 72 65 20 64  nc() calls are d
14376 69 73 61 62 6c 65 64 20 66 6f 72 20 74 68 69 73  isabled for this
14377 20 70 61 67 65 72 2e 20 20 52 65 74 75 72 6e 20   pager.  Return 
14378 46 41 4c 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e  FALSE.** if fsyn
14379 63 28 29 73 20 61 72 65 20 65 78 65 63 75 74 65  c()s are execute
1437a 64 20 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53  d normally..*/.S
1437b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1437c 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f  t sqlite3PagerNo
1437d 73 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67  sync(Pager *pPag
1437e 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
1437f 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a  ager->noSync;.}.
14380 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
14381 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53  AS_CODEC./*.** S
14382 65 74 20 6f 72 20 72 65 74 72 69 65 76 65 20 74  et or retrieve t
14383 68 65 20 63 6f 64 65 63 20 66 6f 72 20 74 68 69  he codec for thi
14384 73 20 70 61 67 65 72 0a 2a 2f 0a 73 74 61 74 69  s pager.*/.stati
14385 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  c void sqlite3Pa
14386 67 65 72 53 65 74 43 6f 64 65 63 28 0a 20 20 50  gerSetCodec(.  P
14387 61 67 65 72 20 2a 70 50 61 67 65 72 2c 0a 20 20  ager *pPager,.  
14388 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28  void *(*xCodec)(
14389 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f  void*,void*,Pgno
1438a 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a  ,int),.  void (*
1438b 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 29 28  xCodecSizeChng)(
1438c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 2c 0a  void*,int,int),.
1438d 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46    void (*xCodecF
1438e 72 65 65 29 28 76 6f 69 64 2a 29 2c 0a 20 20 76  ree)(void*),.  v
1438f 6f 69 64 20 2a 70 43 6f 64 65 63 0a 29 7b 0a 20  oid *pCodec.){. 
14390 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f   if( pPager->xCo
14391 64 65 63 46 72 65 65 20 29 20 70 50 61 67 65 72  decFree ) pPager
14392 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 70 50 61  ->xCodecFree(pPa
14393 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b 0a 20 20  ger->pCodec);.  
14394 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d  pPager->xCodec =
14395 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3f   pPager->memDb ?
14396 20 30 20 3a 20 78 43 6f 64 65 63 3b 0a 20 20 70   0 : xCodec;.  p
14397 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a  Pager->xCodecSiz
14398 65 43 68 6e 67 20 3d 20 78 43 6f 64 65 63 53 69  eChng = xCodecSi
14399 7a 65 43 68 6e 67 3b 0a 20 20 70 50 61 67 65 72  zeChng;.  pPager
1439a 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 3d 20 78  ->xCodecFree = x
1439b 43 6f 64 65 63 46 72 65 65 3b 0a 20 20 70 50 61  CodecFree;.  pPa
1439c 67 65 72 2d 3e 70 43 6f 64 65 63 20 3d 20 70 43  ger->pCodec = pC
1439d 6f 64 65 63 3b 0a 20 20 70 61 67 65 72 52 65 70  odec;.  pagerRep
1439e 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b  ortSize(pPager);
1439f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  .}.static void *
143a0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 43  sqlite3PagerGetC
143a1 6f 64 65 63 28 50 61 67 65 72 20 2a 70 50 61 67  odec(Pager *pPag
143a2 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
143a3 61 67 65 72 2d 3e 70 43 6f 64 65 63 3b 0a 7d 0a  ager->pCodec;.}.
143a4 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
143a5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
143a6 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76  VACUUM./*.** Mov
143a7 65 20 74 68 65 20 70 61 67 65 20 70 50 67 20 74  e the page pPg t
143a8 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f 20  o location pgno 
143a9 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a  in the file..**.
143aa 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65  ** There must be
143ab 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73 20 74   no references t
143ac 6f 20 74 68 65 20 70 61 67 65 20 70 72 65 76 69  o the page previ
143ad 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74  ously located at
143ae 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63 68 20  .** pgno (which 
143af 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29 20  we call pPgOld) 
143b0 74 68 6f 75 67 68 20 74 68 61 74 20 70 61 67 65  though that page
143b1 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62   is allowed to b
143b2 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20 20  e.** in cache.  
143b3 49 66 20 74 68 65 20 70 61 67 65 20 70 72 65 76  If the page prev
143b4 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61  iously located a
143b5 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61 6c  t pgno is not al
143b6 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65 20  ready.** in the 
143b7 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
143b8 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75 74 20  , it is not put 
143b9 74 68 65 72 65 20 62 79 20 62 79 20 74 68 69 73  there by by this
143ba 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
143bb 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  References to th
143bc 65 20 70 61 67 65 20 70 50 67 20 72 65 6d 61 69  e page pPg remai
143bd 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74 69 6e  n valid. Updatin
143be 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64 61  g any.** meta-da
143bf 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
143c0 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64 61 74  th pPg (i.e. dat
143c1 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  a stored in the 
143c2 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a 2a 20  nExtra bytes.** 
143c3 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20  allocated along 
143c4 77 69 74 68 20 74 68 65 20 70 61 67 65 29 20 69  with the page) i
143c5 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69  s the responsibi
143c6 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c  lity of the call
143c7 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e  er..**.** A tran
143c8 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  saction must be 
143c9 61 63 74 69 76 65 20 77 68 65 6e 20 74 68 69 73  active when this
143ca 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
143cb 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62  ed. It used to b
143cc 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 74 68  e.** required th
143cd 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  at a statement t
143ce 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6e  ransaction was n
143cf 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74 20 74  ot active, but t
143d0 68 69 73 20 72 65 73 74 72 69 63 74 69 6f 6e 0a  his restriction.
143d1 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65 6d 6f  ** has been remo
143d2 76 65 64 20 28 43 52 45 41 54 45 20 49 4e 44 45  ved (CREATE INDE
143d3 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65 20  X needs to move 
143d4 61 20 70 61 67 65 20 77 68 65 6e 20 61 20 73 74  a page when a st
143d5 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73  atement.** trans
143d6 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
143d7 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  )..**.** If the 
143d8 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c  fourth argument,
143d9 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 20 6e 6f   isCommit, is no
143da 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 69  n-zero, then thi
143db 73 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 0a  s page is being.
143dc 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 61 72 74  ** moved as part
143dd 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 72   of a database r
143de 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e 20 6a 75  eorganization ju
143df 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 74 72  st before the tr
143e0 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 69 73  ansaction .** is
143e1 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64   being committed
143e2 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20  . In this case, 
143e3 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  it is guaranteed
143e4 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
143e5 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 50 67 20  se page .** pPg 
143e6 72 65 66 65 72 73 20 74 6f 20 77 69 6c 6c 20 6e  refers to will n
143e7 6f 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  ot be written to
143e8 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68   again within th
143e9 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  is transaction..
143ea 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
143eb 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53  ion may return S
143ec 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61  QLITE_NOMEM or a
143ed 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  n IO error code 
143ee 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f  if an error.** o
143ef 63 63 75 72 73 2e 20 4f 74 68 65 72 77 69 73 65  ccurs. Otherwise
143f0 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c  , it returns SQL
143f1 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54  ITE_OK..*/.SQLIT
143f2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
143f3 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61  lite3PagerMovepa
143f4 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ge(Pager *pPager
143f5 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c 20 50  , DbPage *pPg, P
143f6 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69 73  gno pgno, int is
143f7 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48 64 72  Commit){.  PgHdr
143f8 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20 20 20   *pPgOld;       
143f9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
143fa 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72 77 72  age being overwr
143fb 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f  itten. */.  Pgno
143fc 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20   needSyncPgno = 
143fd 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c 64 20  0;       /* Old 
143fe 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e 70 67  value of pPg->pg
143ff 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73 20 72  no, if sync is r
14400 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74  equired */.  int
14401 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
14402 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
14403 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 67  urn code */.  Pg
14404 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 20 20 20  no origPgno;    
14405 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
14406 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20  e original page 
14407 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 61 73 73  number */..  ass
14408 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30  ert( pPg->nRef>0
14409 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6f 72 64   );..  /* In ord
1440a 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f  er to be able to
1440b 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 6e 20 69 6e   rollback, an in
1440c 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
1440d 20 6d 75 73 74 20 6a 6f 75 72 6e 61 6c 0a 20 20   must journal.  
1440e 2a 2a 20 74 68 65 20 70 61 67 65 20 77 65 20 61  ** the page we a
1440f 72 65 20 6d 6f 76 69 6e 67 20 66 72 6f 6d 2e 0a  re moving from..
14410 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42    */.  if( MEMDB
14411 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
14412 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
14413 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  Pg);.    if( rc 
14414 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
14415 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
14416 67 65 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69  ge being moved i
14417 73 20 64 69 72 74 79 20 61 6e 64 20 68 61 73 20  s dirty and has 
14418 6e 6f 74 20 62 65 65 6e 20 73 61 76 65 64 20 62  not been saved b
14419 79 20 74 68 65 20 6c 61 74 65 73 74 0a 20 20 2a  y the latest.  *
1441a 2a 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65  * savepoint, the
1441b 6e 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65  n save the curre
1441c 6e 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  nt contents of t
1441d 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65  he page into the
1441e 20 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e   .  ** sub-journ
1441f 61 6c 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20  al now. This is 
14420 72 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64  required to hand
14421 6c 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  le the following
14422 20 73 63 65 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a   scenario:.  **.
14423 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20    **   BEGIN;.  
14424 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20  **     <journal 
14425 70 61 67 65 20 58 2c 20 74 68 65 6e 20 6d 6f 64  page X, then mod
14426 69 66 79 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79  ify it in memory
14427 3e 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50  >.  **     SAVEP
14428 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20  OINT one;.  **  
14429 20 20 20 20 20 3c 4d 6f 76 65 20 70 61 67 65 20       <Move page 
1442a 58 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 59 3e  X to location Y>
1442b 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41  .  **     ROLLBA
1442c 43 4b 20 54 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a  CK TO one;.  **.
1442d 20 20 2a 2a 20 49 66 20 70 61 67 65 20 58 20 77    ** If page X w
1442e 65 72 65 20 6e 6f 74 20 77 72 69 74 74 65 6e 20  ere not written 
1442f 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  to the sub-journ
14430 61 6c 20 68 65 72 65 2c 20 69 74 20 77 6f 75 6c  al here, it woul
14431 64 20 6e 6f 74 0a 20 20 2a 2a 20 62 65 20 70 6f  d not.  ** be po
14432 73 73 69 62 6c 65 20 74 6f 20 72 65 73 74 6f 72  ssible to restor
14433 65 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 77  e its contents w
14434 68 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43  hen the "ROLLBAC
14435 4b 20 54 4f 20 6f 6e 65 22 0a 20 20 2a 2a 20 73  K TO one".  ** s
14436 74 61 74 65 6d 65 6e 74 20 77 65 72 65 20 69 73  tatement were is
14437 20 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2a   processed..  **
14438 0a 20 20 2a 2a 20 73 75 62 6a 6f 75 72 6e 61 6c  .  ** subjournal
14439 50 61 67 65 28 29 20 6d 61 79 20 6e 65 65 64 20  Page() may need 
1443a 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63  to allocate spac
1443b 65 20 74 6f 20 73 74 6f 72 65 20 70 50 67 2d 3e  e to store pPg->
1443c 70 67 6e 6f 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f  pgno into.  ** o
1443d 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 70  ne or more savep
1443e 6f 69 6e 74 20 62 69 74 76 65 63 73 2e 20 54 68  oint bitvecs. Th
1443f 69 73 20 69 73 20 74 68 65 20 72 65 61 73 6f 6e  is is the reason
14440 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20   this function. 
14441 20 2a 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53   ** may return S
14442 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a  QLITE_NOMEM..  *
14443 2f 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61  /.  if( pPg->fla
14444 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 0a 20  gs&PGHDR_DIRTY. 
14445 20 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65    && subjRequire
14446 73 50 61 67 65 28 70 50 67 29 0a 20 20 20 26 26  sPage(pPg).   &&
14447 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
14448 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65  = subjournalPage
14449 28 70 50 67 29 29 0a 20 20 29 7b 0a 20 20 20 20  (pPg)).  ){.    
1444a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
1444b 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4d    PAGERTRACE(("M
1444c 4f 56 45 20 25 64 20 70 61 67 65 20 25 64 20 28  OVE %d page %d (
1444d 6e 65 65 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76  needSync=%d) mov
1444e 65 73 20 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20  es to %d\n", .  
1444f 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67      PAGERID(pPag
14450 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  er), pPg->pgno, 
14451 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
14452 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30  R_NEED_SYNC)?1:0
14453 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52  , pgno));.  IOTR
14454 41 43 45 28 28 22 4d 4f 56 45 20 25 70 20 25 64  ACE(("MOVE %p %d
14455 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
14456 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29  pPg->pgno, pgno)
14457 29 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a  )..  /* If the j
14458 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20  ournal needs to 
14459 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f  be sync()ed befo
1445a 72 65 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e  re page pPg->pgn
1445b 6f 20 63 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72  o can.  ** be wr
1445c 69 74 74 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20  itten to, store 
1445d 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63  pPg->pgno in loc
1445e 61 6c 20 76 61 72 69 61 62 6c 65 20 6e 65 65 64  al variable need
1445f 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20  SyncPgno..  **. 
14460 20 2a 2a 20 49 66 20 74 68 65 20 69 73 43 6f 6d   ** If the isCom
14461 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c  mit flag is set,
14462 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
14463 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68  d to remember th
14464 61 74 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72  at.  ** the jour
14465 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20  nal needs to be 
14466 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20  sync()ed before 
14467 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 50  database page pP
14468 67 2d 3e 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61  g->pgno .  ** ca
14469 6e 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e  n be written to.
1446a 20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20   The caller has 
1446b 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64  already promised
1446c 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f   not to write to
1446d 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
1446e 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
1446f 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 26 26 20  R_NEED_SYNC) && 
14470 21 69 73 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  !isCommit ){.   
14471 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20   needSyncPgno = 
14472 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61  pPg->pgno;.    a
14473 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75  ssert( pageInJou
14474 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67  rnal(pPg) || pPg
14475 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64  ->pgno>pPager->d
14476 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20  bOrigSize );.   
14477 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c   assert( pPg->fl
14478 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20  ags&PGHDR_DIRTY 
14479 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1447a 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
1447b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
1447c 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69  the cache contai
1447d 6e 73 20 61 20 70 61 67 65 20 77 69 74 68 20 70  ns a page with p
1447e 61 67 65 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c  age-number pgno,
1447f 20 72 65 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20   remove it.  ** 
14480 66 72 6f 6d 20 69 74 73 20 68 61 73 68 20 63 68  from its hash ch
14481 61 69 6e 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68  ain. Also, if th
14482 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63  e PgHdr.needSync
14483 20 77 61 73 20 73 65 74 20 66 6f 72 20 0a 20 20   was set for .  
14484 2a 2a 20 70 61 67 65 20 70 67 6e 6f 20 62 65 66  ** page pgno bef
14485 6f 72 65 20 74 68 65 20 27 6d 6f 76 65 27 20 6f  ore the 'move' o
14486 70 65 72 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65  peration, it nee
14487 64 73 20 74 6f 20 62 65 20 72 65 74 61 69 6e 65  ds to be retaine
14488 64 20 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  d .  ** for the 
14489 70 61 67 65 20 6d 6f 76 65 64 20 74 68 65 72 65  page moved there
1448a 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c  ..  */.  pPg->fl
1448b 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45  ags &= ~PGHDR_NE
1448c 45 44 5f 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c  ED_SYNC;.  pPgOl
1448d 64 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70  d = pager_lookup
1448e 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a  (pPager, pgno);.
1448f 20 20 61 73 73 65 72 74 28 20 21 70 50 67 4f 6c    assert( !pPgOl
14490 64 20 7c 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65  d || pPgOld->nRe
14491 66 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 50  f==1 );.  if( pP
14492 67 4f 6c 64 20 29 7b 0a 20 20 20 20 70 50 67 2d  gOld ){.    pPg-
14493 3e 66 6c 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c  >flags |= (pPgOl
14494 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  d->flags&PGHDR_N
14495 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69  EED_SYNC);.    i
14496 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20  f( MEMDB ){.    
14497 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 69 73 63    /* Do not disc
14498 61 72 64 20 70 61 67 65 73 20 66 72 6f 6d 20 61  ard pages from a
14499 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
1449a 62 61 73 65 20 73 69 6e 63 65 20 77 65 20 6d 69  base since we mi
1449b 67 68 74 0a 20 20 20 20 20 20 2a 2a 20 6e 65 65  ght.      ** nee
1449c 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6c 61  d to rollback la
1449d 74 65 72 2e 20 20 4a 75 73 74 20 6d 6f 76 65 20  ter.  Just move 
1449e 74 68 65 20 70 61 67 65 20 6f 75 74 20 6f 66 20  the page out of 
1449f 74 68 65 20 77 61 79 2e 20 2a 2f 0a 20 20 20 20  the way. */.    
144a0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
144a1 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b  ->dbSizeValid );
144a2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63  .      sqlite3Pc
144a3 61 63 68 65 4d 6f 76 65 28 70 50 67 4f 6c 64 2c  acheMove(pPgOld,
144a4 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 2b   pPager->dbSize+
144a5 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  1);.    }else{. 
144a6 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63       sqlite3Pcac
144a7 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a  heDrop(pPgOld);.
144a8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6f 72 69      }.  }..  ori
144a9 67 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e  gPgno = pPg->pgn
144aa 6f 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  o;.  sqlite3Pcac
144ab 68 65 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f  heMove(pPg, pgno
144ac 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  );.  sqlite3Pcac
144ad 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29  heMakeDirty(pPg)
144ae 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f  ;.  pPager->dbMo
144af 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 69  dified = 1;..  i
144b0 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20  f( needSyncPgno 
144b1 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65  ){.    /* If nee
144b2 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e  dSyncPgno is non
144b3 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20  -zero, then the 
144b4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65  journal file nee
144b5 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a  ds to be .    **
144b6 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65   sync()ed before
144b7 20 61 6e 79 20 64 61 74 61 20 69 73 20 77 72 69   any data is wri
144b8 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65  tten to database
144b9 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53   file page needS
144ba 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20  yncPgno..    ** 
144bb 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75  Currently, no su
144bc 63 68 20 70 61 67 65 20 65 78 69 73 74 73 20 69  ch page exists i
144bd 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65  n the page-cache
144be 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a   and the .    **
144bf 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20   "is journaled" 
144c0 62 69 74 76 65 63 20 66 6c 61 67 20 68 61 73 20  bitvec flag has 
144c1 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e  been set. This n
144c2 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64  eeds to be remed
144c3 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f  ied by.    ** lo
144c4 61 64 69 6e 67 20 74 68 65 20 70 61 67 65 20 69  ading the page i
144c5 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61  nto the pager-ca
144c6 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20  che and setting 
144c7 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79  the PgHdr.needSy
144c8 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e  nc .    ** flag.
144c9 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
144ca 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f  f the attempt to
144cb 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20 69   load the page i
144cc 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63  nto the page-cac
144cd 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20  he fails, (due. 
144ce 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f     ** to a mallo
144cf 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72  c() or IO failur
144d0 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69  e), clear the bi
144d1 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72  t in the pInJour
144d2 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  nal[].    ** arr
144d3 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ay. Otherwise, i
144d4 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f  f the page is lo
144d5 61 64 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e  aded and written
144d6 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a   again in.    **
144d7 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
144d8 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72 69  n, it may be wri
144d9 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
144da 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65  base file before
144db 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79  .    ** it is sy
144dc 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f  nced into the jo
144dd 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73  urnal file. This
144de 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64   way, it may end
144df 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68   up in.    ** th
144e0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  e journal file t
144e1 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 69  wice, but that i
144e2 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e  s not a problem.
144e3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
144e4 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  he sqlite3PagerG
144e5 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61  et() call may ca
144e6 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  use the journal 
144e7 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65  to sync. So make
144e8 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65  .    ** sure the
144e9 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20   Pager.needSync 
144ea 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e  flag is set too.
144eb 0a 20 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 64  .    */.    PgHd
144ec 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 61  r *pPgHdr;.    a
144ed 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e  ssert( pPager->n
144ee 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 20 20 72  eedSync );.    r
144ef 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
144f0 47 65 74 28 70 50 61 67 65 72 2c 20 6e 65 65 64  Get(pPager, need
144f1 53 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 64  SyncPgno, &pPgHd
144f2 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
144f3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
144f4 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50     if( needSyncP
144f5 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f  gno<=pPager->dbO
144f6 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  rigSize ){.     
144f7 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
144f8 72 2d 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 20  r->pTmpSpace!=0 
144f9 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
144fa 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 50  e3BitvecClear(pP
144fb 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
144fc 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20  , needSyncPgno, 
144fd 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
144fe 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
144ff 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
14500 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e   }.    pPager->n
14501 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20  eedSync = 1;.   
14502 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
14503 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21 4d  >noSync==0 && !M
14504 45 4d 44 42 20 29 3b 0a 20 20 20 20 70 50 67 48  EMDB );.    pPgH
14505 64 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48  dr->flags |= PGH
14506 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
14507 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
14508 61 6b 65 44 69 72 74 79 28 70 50 67 48 64 72 29  akeDirty(pPgHdr)
14509 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
1450a 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b  erUnref(pPgHdr);
1450b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
1450c 46 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  For an in-memory
1450d 20 64 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 20   database, make 
1450e 73 75 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61  sure the origina
1450f 6c 20 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 73  l page continues
14510 0a 20 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c 20  .  ** to exist, 
14511 69 6e 20 63 61 73 65 20 74 68 65 20 74 72 61 6e  in case the tran
14512 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f  saction needs to
14513 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 20 55 73 65   roll back.  Use
14514 20 70 50 67 4f 6c 64 0a 20 20 2a 2a 20 61 73 20   pPgOld.  ** as 
14515 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  the original pag
14516 65 20 73 69 6e 63 65 20 69 74 20 68 61 73 20 61  e since it has a
14517 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f  lready been allo
14518 63 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  cated..  */.  if
14519 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 73  ( MEMDB ){.    s
1451a 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65  qlite3PcacheMove
1451b 28 70 50 67 4f 6c 64 2c 20 6f 72 69 67 50 67 6e  (pPgOld, origPgn
1451c 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  o);.    sqlite3P
1451d 61 67 65 72 55 6e 72 65 66 28 70 50 67 4f 6c 64  agerUnref(pPgOld
1451e 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
1451f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
14520 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
14521 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
14522 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  the data for the
14523 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e   specified page.
14524 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14525 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
14526 50 61 67 65 72 47 65 74 44 61 74 61 28 44 62 50  PagerGetData(DbP
14527 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73  age *pPg){.  ass
14528 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30  ert( pPg->nRef>0
14529 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d   || pPg->pPager-
1452a 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75  >memDb );.  retu
1452b 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d  rn pPg->pData;.}
1452c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
1452d 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1452e 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74  Pager.nExtra byt
1452f 65 73 20 6f 66 20 22 65 78 74 72 61 22 20 73 70  es of "extra" sp
14530 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  ace .** allocate
14531 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65  d along with the
14532 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e   specified page.
14533 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14534 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
14535 50 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62  PagerGetExtra(Db
14536 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65  Page *pPg){.  re
14537 74 75 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61  turn pPg->pExtra
14538 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73  ;.}../*.** Get/s
14539 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  et the locking-m
1453a 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67  ode for this pag
1453b 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d  er. Parameter eM
1453c 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a  ode must be one.
1453d 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b  ** of PAGER_LOCK
1453e 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50  INGMODE_QUERY, P
1453f 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
14540 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50  _NORMAL or .** P
14541 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
14542 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74  _EXCLUSIVE. If t
14543 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  he parameter is 
14544 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e  not _QUERY, then
14545 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d  .** the locking-
14546 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74  mode is set to t
14547 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69  he value specifi
14548 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ed..**.** The re
14549 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20  turned value is 
1454a 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43  either PAGER_LOC
1454b 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20  KINGMODE_NORMAL 
1454c 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b  or.** PAGER_LOCK
1454d 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
1454e 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  E, indicating th
1454f 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69  e current (possi
14550 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20  bly updated).** 
14551 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f  locking-mode..*/
14552 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14553 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
14554 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65  LockingMode(Page
14555 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65  r *pPager, int e
14556 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Mode){.  assert(
14557 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
14558 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a  CKINGMODE_QUERY.
14559 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
1455a 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
1455b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20  INGMODE_NORMAL. 
1455c 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
1455d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
1455e 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
1455f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41   );.  assert( PA
14560 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
14561 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73  QUERY<0 );.  ass
14562 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49  ert( PAGER_LOCKI
14563 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30  NGMODE_NORMAL>=0
14564 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e   && PAGER_LOCKIN
14565 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e  GMODE_EXCLUSIVE>
14566 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64  =0 );.  if( eMod
14567 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d  e>=0 && !pPager-
14568 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20  >tempFile ){.   
14569 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
1456a 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f  veMode = (u8)eMo
1456b 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  de;.  }.  return
1456c 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78   (int)pPager->ex
1456d 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a  clusiveMode;.}..
1456e 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68  /*.** Get/set th
1456f 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66  e journal-mode f
14570 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50  or this pager. P
14571 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d  arameter eMode m
14572 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a  ust be one of:.*
14573 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f  *.**    PAGER_JO
14574 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a  URNALMODE_QUERY.
14575 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
14576 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a  NALMODE_DELETE.*
14577 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
14578 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a  ALMODE_TRUNCATE.
14579 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
1457a 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a  NALMODE_PERSIST.
1457b 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
1457c 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20  NALMODE_OFF.**  
1457d 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d    PAGER_JOURNALM
1457e 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a  ODE_MEMORY.**.**
1457f 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   If the paramete
14580 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c  r is not _QUERY,
14581 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   then the journa
14582 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f  l_mode is set to
14583 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70   the.** value sp
14584 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63  ecified if the c
14585 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64  hange is allowed
14586 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 73  .  The change is
14587 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66   disallowed.** f
14588 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  or the following
14589 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20   reasons:.**.** 
1458a 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72    *  An in-memor
1458b 79 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f  y database can o
1458c 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75  nly have its jou
1458d 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f  rnal_mode set to
1458e 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72   _OFF.**      or
1458f 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20   _MEMORY..**.** 
14590 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c    *  The journal
14591 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65   mode may not be
14592 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61   changed while a
14593 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
14594 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  active..**.** Th
14595 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63  e returned indic
14596 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
14597 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65  (possibly update
14598 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e  d) journal-mode.
14599 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1459a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
1459b 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  gerJournalMode(P
1459c 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
1459d 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65  t eMode){.  asse
1459e 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52  rt( eMode==PAGER
1459f 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
145a0 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  RY.            |
145a1 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
145a2 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54  OURNALMODE_DELET
145a3 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  E.            ||
145a4 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
145a5 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
145a6 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  TE.            |
145a7 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
145a8 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49  OURNALMODE_PERSI
145a9 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  ST.            |
145aa 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
145ab 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a  OURNALMODE_OFF .
145ac 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
145ad 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
145ae 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29  NALMODE_MEMORY )
145af 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
145b0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
145b1 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65  ERY<0 );.  if( e
145b2 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21  Mode>=0.   && (!
145b3 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d  MEMDB || eMode==
145b4 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
145b5 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20  E_MEMORY .      
145b6 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
145b7 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
145b8 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21  ODE_OFF).   && !
145b9 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
145ba 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65  ed.   && (!isOpe
145bb 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c  n(pPager->jfd) |
145bc 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75  | 0==pPager->jou
145bd 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20  rnalOff).  ){.  
145be 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
145bf 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20  ger->jfd) ){.   
145c0 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
145c1 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  e(pPager->jfd);.
145c2 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72      }.    pPager
145c3 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20  ->journalMode = 
145c4 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20  (u8)eMode;.  }. 
145c5 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61   return (int)pPa
145c6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
145c7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73  ;.}../*.** Get/s
145c8 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69  et the size-limi
145c9 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 69  t used for persi
145ca 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69  stent journal fi
145cb 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69  les..**.** Setti
145cc 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69  ng the size limi
145cd 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f  t to -1 means no
145ce 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63   limit is enforc
145cf 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70  ed..** An attemp
145d0 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74  t to set a limit
145d1 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31   smaller than -1
145d2 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
145d3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
145d4 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  64 sqlite3PagerJ
145d5 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
145d6 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
145d7 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66  64 iLimit){.  if
145d8 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a  ( iLimit>=-1 ){.
145d9 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
145da 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69  nalSizeLimit = i
145db 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74  Limit;.  }.  ret
145dc 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  urn pPager->jour
145dd 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a  nalSizeLimit;.}.
145de 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
145df 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
145e0 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76  Pager->pBackup v
145e1 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63  ariable. The bac
145e2 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e  kup module.** in
145e3 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61   backup.c mainta
145e4 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ins the content 
145e5 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  of this variable
145e6 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a  . This module.**
145e7 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c   uses it opaquel
145e8 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  y as an argument
145e9 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75   to sqlite3Backu
145ea 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a  pRestart() and.*
145eb 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55  * sqlite3BackupU
145ec 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f  pdate() only..*/
145ed 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
145ee 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
145ef 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63  *sqlite3PagerBac
145f0 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50  kupPtr(Pager *pP
145f1 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
145f2 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70  &pPager->pBackup
145f3 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
145f4 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
145f5 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  O */../*********
145f6 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67  ***** End of pag
145f7 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
145f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145fa 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
145fb 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
145fc 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
145fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145ff 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
14600 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a   August 27.**.**
14601 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
14602 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
14603 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
14604 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
14605 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
14606 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
14607 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
14608 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
14609 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1460a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1460b 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1460c 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1460d 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1460e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1460f 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
14610 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
14611 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
14612 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14613 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14614 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14615 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14616 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
14617 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
14618 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
14619 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65  implement mutexe
1461a 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 63  s on Btree objec
1461b 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65  ts..** This code
1461c 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20   really belongs 
1461d 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 42 75 74  in btree.c.  But
1461e 20 62 74 72 65 65 2e 63 20 69 73 20 67 65 74 74   btree.c is gett
1461f 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61  ing too.** big a
14620 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 20 62 72  nd we want to br
14621 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65  eak it down some
14622 2e 20 20 54 68 69 73 20 70 61 63 6b 61 67 65 64  .  This packaged
14623 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20   seemed like.** 
14624 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e  a good breakout.
14625 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
14626 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65  *** Include btre
14627 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69  eInt.h in the mi
14628 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 2e  ddle of btmutex.
14629 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1462a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1462b 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
1462c 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  treeInt.h ******
1462d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1462e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1462f 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41  **/./*.** 2004 A
14630 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
14631 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
14632 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
14633 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
14634 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
14635 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
14636 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
14637 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
14638 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
14639 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1463a 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1463b 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1463c 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1463d 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1463e 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1463f 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
14640 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
14641 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
14642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14643 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14646 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
14647 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  le implements a 
14648 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62  external (disk-b
14649 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75  ased) database u
1464a 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20  sing BTrees..** 
1464b 46 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64  For a detailed d
1464c 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72  iscussion of BTr
1464d 65 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a  ees, refer to.**
1464e 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45  .**     Donald E
1464f 2e 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54  . Knuth, THE ART
14650 20 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f   OF COMPUTER PRO
14651 47 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65  GRAMMING, Volume
14652 20 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74   3:.**     "Sort
14653 69 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e  ing And Searchin
14654 67 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38  g", pages 473-48
14655 30 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65  0. Addison-Wesle
14656 79 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68  y.**     Publish
14657 69 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61  ing Company, Rea
14658 64 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65  ding, Massachuse
14659 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62  tts..**.** The b
1465a 61 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61  asic idea is tha
1465b 74 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74  t each page of t
1465c 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  he file contains
1465d 20 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65   N database.** e
1465e 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70  ntries and N+1 p
1465f 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61  ointers to subpa
14660 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d  ges..**.**   ---
14661 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14662 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14663 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14664 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
14665 20 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b     |  Ptr(0) | K
14666 65 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c  ey(0) | Ptr(1) |
14667 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20   Key(1) | ... | 
14668 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e  Key(N-1) | Ptr(N
14669 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ) |.**   -------
1466a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1466b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1466c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1466d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
1466e 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20  All of the keys 
1466f 6f 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74  on the page that
14670 20 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74   Ptr(0) points t
14671 6f 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65  o have values le
14672 73 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30  ss.** than Key(0
14673 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b  ).  All of the k
14674 65 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28  eys on page Ptr(
14675 31 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61  1) and its subpa
14676 67 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75  ges have.** valu
14677 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  es greater than 
14678 4b 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20  Key(0) and less 
14679 74 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c  than Key(1).  Al
1467a 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a  l of the keys.**
1467b 20 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69   on Ptr(N) and i
1467c 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65  ts subpages have
1467d 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20   values greater 
1467e 74 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20  than Key(N-1).  
1467f 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e  And.** so forth.
14680 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61  .**.** Finding a
14681 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20   particular key 
14682 72 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67  requires reading
14683 20 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73   O(log(M)) pages
14684 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69   from the .** di
14685 73 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68  sk where M is th
14686 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
14687 69 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e  ies in the tree.
14688 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69  .**.** In this i
14689 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61  mplementation, a
1468a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e   single file can
1468b 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72   hold one or mor
1468c 65 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42  e separate .** B
1468d 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72  Trees.  Each BTr
1468e 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  ee is identified
1468f 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66   by the index of
14690 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20   its root page. 
14691 20 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20   The.** key and 
14692 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74  data for any ent
14693 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20  ry are combined 
14694 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79  to form the "pay
14695 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78  load".  A.** fix
14696 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79  ed amount of pay
14697 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72  load can be carr
14698 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20  ied directly on 
14699 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
1469a 70 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61  page.  If the pa
1469b 79 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20  yload is larger 
1469c 74 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20  than the preset 
1469d 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70  amount then surp
1469e 6c 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65  lus.** bytes are
1469f 20 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66   stored on overf
146a0 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20  low pages.  The 
146a1 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65  payload for an e
146a2 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ntry.** and the 
146a3 70 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65  preceding pointe
146a4 72 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74  r are combined t
146a5 6f 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e  o form a "Cell".
146a6 20 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20    Each .** page 
146a7 68 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64  has a small head
146a8 65 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e  er which contain
146a9 73 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69  s the Ptr(N) poi
146aa 6e 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a  nter and other.*
146ab 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75  * information su
146ac 63 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f  ch as the size o
146ad 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a  f key and data..
146ae 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54  **.** FORMAT DET
146af 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  AILS.**.** The f
146b0 69 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69  ile is divided i
146b1 6e 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20  nto pages.  The 
146b2 66 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61  first page is ca
146b3 6c 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20  lled page 1,.** 
146b4 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61  the second is pa
146b5 67 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72  ge 2, and so for
146b6 74 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62  th.  A page numb
146b7 65 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63  er of zero indic
146b8 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68  ates.** "no such
146b9 20 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67   page".  The pag
146ba 65 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e  e size can be an
146bb 79 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74  y power of 2 bet
146bc 77 65 65 6e 20 35 31 32 20 61 6e 64 20 33 32 37  ween 512 and 327
146bd 36 38 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65  68..** Each page
146be 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
146bf 20 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66   btree page, a f
146c0 72 65 65 6c 69 73 74 20 70 61 67 65 2c 20 61 6e  reelist page, an
146c1 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67   overflow.** pag
146c2 65 2c 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 2d  e, or a pointer-
146c3 6d 61 70 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  map page..**.** 
146c4 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  The first page i
146c5 73 20 61 6c 77 61 79 73 20 61 20 62 74 72 65 65  s always a btree
146c6 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 72 73   page.  The firs
146c7 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20 74  t 100 bytes of t
146c8 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65  he first.** page
146c9 20 63 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69   contain a speci
146ca 61 6c 20 68 65 61 64 65 72 20 28 74 68 65 20 22  al header (the "
146cb 66 69 6c 65 20 68 65 61 64 65 72 22 29 20 74 68  file header") th
146cc 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
146cd 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f   file..** The fo
146ce 72 6d 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65  rmat of the file
146cf 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f   header is as fo
146d0 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f  llows:.**.**   O
146d1 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 20  FFSET   SIZE    
146d2 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20  DESCRIPTION.**  
146d3 20 20 20 20 30 20 20 20 20 20 20 31 36 20 20 20      0      16   
146d4 20 20 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a    Header string:
146d5 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20   "SQLite format 
146d6 33 5c 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36  3\000".**     16
146d7 20 20 20 20 20 20 20 32 20 20 20 20 20 50 61 67         2     Pag
146d8 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e  e size in bytes.
146d9 20 20 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20    .**     18    
146da 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f     1     File fo
146db 72 6d 61 74 20 77 72 69 74 65 20 76 65 72 73 69  rmat write versi
146dc 6f 6e 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20  on.**     19    
146dd 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f     1     File fo
146de 72 6d 61 74 20 72 65 61 64 20 76 65 72 73 69 6f  rmat read versio
146df 6e 0a 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20  n.**     20     
146e0 20 20 31 20 20 20 20 20 42 79 74 65 73 20 6f 66    1     Bytes of
146e1 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 61 74   unused space at
146e2 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68   the end of each
146e3 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20   page.**     21 
146e4 20 20 20 20 20 20 31 20 20 20 20 20 4d 61 78 20        1     Max 
146e5 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
146e6 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20   fraction.**    
146e7 20 32 32 20 20 20 20 20 20 20 31 20 20 20 20 20   22       1     
146e8 4d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79  Min embedded pay
146e9 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
146ea 20 20 20 20 20 32 33 20 20 20 20 20 20 20 31 20       23       1 
146eb 20 20 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79      Min leaf pay
146ec 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
146ed 20 20 20 20 20 32 34 20 20 20 20 20 20 20 34 20       24       4 
146ee 20 20 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20      File change 
146ef 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32  counter.**     2
146f0 38 20 20 20 20 20 20 20 34 20 20 20 20 20 52 65  8       4     Re
146f1 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72  served for futur
146f2 65 20 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20  e use.**     32 
146f3 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73        4     Firs
146f4 74 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a  t freelist page.
146f5 2a 2a 20 20 20 20 20 33 36 20 20 20 20 20 20 20  **     36       
146f6 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  4     Number of 
146f7 66 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 69  freelist pages i
146f8 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20  n the file.**   
146f9 20 20 34 30 20 20 20 20 20 20 36 30 20 20 20 20    40      60    
146fa 20 31 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20   15 4-byte meta 
146fb 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f  values passed to
146fc 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a   higher layers.*
146fd 2a 0a 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20  *.**     40     
146fe 20 20 34 20 20 20 20 20 53 63 68 65 6d 61 20 63    4     Schema c
146ff 6f 6f 6b 69 65 0a 2a 2a 20 20 20 20 20 34 34 20  ookie.**     44 
14700 20 20 20 20 20 20 34 20 20 20 20 20 46 69 6c 65        4     File
14701 20 66 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d   format of schem
14702 61 20 6c 61 79 65 72 0a 2a 2a 20 20 20 20 20 34  a layer.**     4
14703 38 20 20 20 20 20 20 20 34 20 20 20 20 20 53 69  8       4     Si
14704 7a 65 20 6f 66 20 70 61 67 65 20 63 61 63 68 65  ze of page cache
14705 0a 2a 2a 20 20 20 20 20 35 32 20 20 20 20 20 20  .**     52      
14706 20 34 20 20 20 20 20 4c 61 72 67 65 73 74 20 72   4     Largest r
14707 6f 6f 74 2d 70 61 67 65 20 28 61 75 74 6f 2f 69  oot-page (auto/i
14708 6e 63 72 5f 76 61 63 75 75 6d 29 0a 2a 2a 20 20  ncr_vacuum).**  
14709 20 20 20 35 36 20 20 20 20 20 20 20 34 20 20 20     56       4   
1470a 20 20 31 3d 55 54 46 2d 38 20 32 3d 55 54 46 31    1=UTF-8 2=UTF1
1470b 36 6c 65 20 33 3d 55 54 46 31 36 62 65 0a 2a 2a  6le 3=UTF16be.**
1470c 20 20 20 20 20 36 30 20 20 20 20 20 20 20 34 20       60       4 
1470d 20 20 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e      User version
1470e 0a 2a 2a 20 20 20 20 20 36 34 20 20 20 20 20 20  .**     64      
1470f 20 34 20 20 20 20 20 49 6e 63 72 65 6d 65 6e 74   4     Increment
14710 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 2a  al vacuum mode.*
14711 2a 20 20 20 20 20 36 38 20 20 20 20 20 20 20 34  *     68       4
14712 20 20 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20       unused.**  
14713 20 20 20 37 32 20 20 20 20 20 20 20 34 20 20 20     72       4   
14714 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20    unused.**     
14715 37 36 20 20 20 20 20 20 20 34 20 20 20 20 20 75  76       4     u
14716 6e 75 73 65 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  nused.**.** All 
14717 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  of the integer v
14718 61 6c 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e  alues are big-en
14719 64 69 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69  dian (most signi
1471a 66 69 63 61 6e 74 20 62 79 74 65 20 66 69 72 73  ficant byte firs
1471b 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  t)..**.** The fi
1471c 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  le change counte
1471d 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  r is incremented
1471e 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
1471f 73 65 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a  se is changed.**
14720 20 54 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c   This counter al
14721 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65  lows other proce
14722 73 73 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65  sses to know whe
14723 6e 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63  n the file has c
14724 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68  hanged.** and th
14725 75 73 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65  us when they nee
14726 64 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72  d to flush their
14727 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   cache..**.** Th
14728 65 20 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70  e max embedded p
14729 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20  ayload fraction 
1472a 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  is the amount of
1472b 20 74 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c   the total usabl
1472c 65 0a 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20  e.** space in a 
1472d 70 61 67 65 20 74 68 61 74 20 63 61 6e 20 62 65  page that can be
1472e 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73   consumed by a s
1472f 69 6e 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73  ingle cell for s
14730 74 61 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65  tandard.** B-tre
14731 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29  e (non-LEAFDATA)
14732 20 74 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75   tables.  A valu
14733 65 20 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31  e of 255 means 1
14734 30 30 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c  00%.  The defaul
14735 74 0a 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74  t.** is to limit
14736 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c   the maximum cel
14737 6c 20 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61  l size so that a
14738 74 20 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20  t least 4 cells 
14739 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f  will fit.** on o
1473a 6e 65 20 70 61 67 65 2e 20 20 54 68 75 73 20 74  ne page.  Thus t
1473b 68 65 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65  he default max e
1473c 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
1473d 66 72 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a  fraction is 64..
1473e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79  **.** If the pay
1473f 6c 6f 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20  load for a cell 
14740 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
14741 68 65 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20  he max payload, 
14742 74 68 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61  then extra.** pa
14743 79 6c 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64  yload is spilled
14744 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   to overflow pag
14745 65 73 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65  es.  Once an ove
14746 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c  rflow page is al
14747 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d  located,.** as m
14748 61 6e 79 20 62 79 74 65 73 20 61 73 20 70 6f 73  any bytes as pos
14749 73 69 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20  sible are moved 
1474a 69 6e 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f  into the overflo
1474b 77 20 70 61 67 65 73 20 77 69 74 68 6f 75 74 20  w pages without 
1474c 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63  letting.** the c
1474d 65 6c 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65  ell size drop be
1474e 6c 6f 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65  low the min embe
1474f 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61  dded payload fra
14750 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ction..**.** The
14751 20 6d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61   min leaf payloa
14752 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69  d fraction is li
14753 6b 65 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64  ke the min embed
14754 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
14755 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74  tion.** except t
14756 68 61 74 20 69 74 20 61 70 70 6c 69 65 73 20 74  hat it applies t
14757 6f 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20  o leaf nodes in 
14758 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e  a LEAFDATA tree.
14759 20 20 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a    The maximum.**
1475a 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
1475b 6e 20 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41  n for a LEAFDATA
1475c 20 74 72 65 65 20 69 73 20 61 6c 77 61 79 73 20   tree is always 
1475d 31 30 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e  100% (or 255) an
1475e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63  d it.** not spec
1475f 69 66 69 65 64 20 69 6e 20 74 68 65 20 68 65 61  ified in the hea
14760 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  der..**.** Each 
14761 62 74 72 65 65 20 70 61 67 65 73 20 69 73 20 64  btree pages is d
14762 69 76 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65  ivided into thre
14763 65 20 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65  e sections:  The
14764 20 68 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20   header, the.** 
14765 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72  cell pointer arr
14766 61 79 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c  ay, and the cell
14767 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20   content area.  
14768 50 61 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20  Page 1 also has 
14769 61 20 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69  a 100-byte.** fi
1476a 6c 65 20 68 65 61 64 65 72 20 74 68 61 74 20 6f  le header that o
1476b 63 63 75 72 73 20 62 65 66 6f 72 65 20 74 68 65  ccurs before the
1476c 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a   page header..**
1476d 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d  .**      |------
1476e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20  ----------|.**  
1476f 20 20 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65      | file heade
14770 72 20 20 20 20 7c 20 20 20 31 30 30 20 62 79 74  r    |   100 byt
14771 65 73 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79  es.  Page 1 only
14772 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d  ..**      |-----
14773 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20  -----------|.** 
14774 20 20 20 20 20 7c 20 70 61 67 65 20 68 65 61 64       | page head
14775 65 72 20 20 20 20 7c 20 20 20 38 20 62 79 74 65  er    |   8 byte
14776 73 20 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31  s for leaves.  1
14777 32 20 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65  2 bytes for inte
14778 72 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20  rior nodes.**   
14779 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     |------------
1477a 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20  ----|.**      | 
1477b 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c  cell pointer   |
1477c 20 20 20 7c 20 20 32 20 62 79 74 65 73 20 70 65     |  2 bytes pe
1477d 72 20 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20  r cell.  Sorted 
1477e 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c  order..**      |
1477f 20 61 72 72 61 79 20 20 20 20 20 20 20 20 20 20   array          
14780 7c 20 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77  |   |  Grows dow
14781 6e 77 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20  nward.**      | 
14782 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
14783 20 20 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d     v.**      |--
14784 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
14785 2a 2a 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f  **      | unallo
14786 63 61 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20  cated    |.**   
14787 20 20 20 7c 20 73 70 61 63 65 20 20 20 20 20 20     | space      
14788 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d      |.**      |-
14789 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c  ---------------|
1478a 20 20 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61     ^  Grows upwa
1478b 72 64 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65  rds.**      | ce
1478c 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20  ll content   |  
1478d 20 7c 20 20 41 72 62 69 74 72 61 72 79 20 6f 72   |  Arbitrary or
1478e 64 65 72 20 69 6e 74 65 72 73 70 65 72 73 65 64  der interspersed
1478f 20 77 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73   with freeblocks
14790 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61  ..**      | area
14791 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c             |   |
14792 20 20 61 6e 64 20 66 72 65 65 20 73 70 61 63 65    and free space
14793 20 66 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20   fragments..**  
14794 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      |-----------
14795 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65  -----|.**.** The
14796 20 70 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f   page headers lo
14797 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  oks like this:.*
14798 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20  *.**   OFFSET   
14799 53 49 5a 45 20 20 20 20 20 44 45 53 43 52 49 50  SIZE     DESCRIP
1479a 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20  TION.**      0  
1479b 20 20 20 20 20 31 20 20 20 20 20 20 46 6c 61 67       1      Flag
1479c 73 2e 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a  s. 1: intkey, 2:
1479d 20 7a 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65   zerodata, 4: le
1479e 61 66 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a  afdata, 8: leaf.
1479f 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20  **      1       
147a0 32 20 20 20 20 20 20 62 79 74 65 20 6f 66 66 73  2      byte offs
147a1 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  et to the first 
147a2 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20  freeblock.**    
147a3 20 20 33 20 20 20 20 20 20 20 32 20 20 20 20 20    3       2     
147a4 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   number of cells
147a5 20 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a   on this page.**
147a6 20 20 20 20 20 20 35 20 20 20 20 20 20 20 32 20        5       2 
147a7 20 20 20 20 20 66 69 72 73 74 20 62 79 74 65 20       first byte 
147a8 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
147a9 65 6e 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20  ent area.**     
147aa 20 37 20 20 20 20 20 20 20 31 20 20 20 20 20 20   7       1      
147ab 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65  number of fragme
147ac 6e 74 65 64 20 66 72 65 65 20 62 79 74 65 73 0a  nted free bytes.
147ad 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 20  **      8       
147ae 34 20 20 20 20 20 20 52 69 67 68 74 20 63 68 69  4      Right chi
147af 6c 64 20 28 74 68 65 20 50 74 72 28 4e 29 20 76  ld (the Ptr(N) v
147b0 61 6c 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20  alue).  Omitted 
147b1 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a  on leaves..**.**
147b2 20 54 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e   The flags defin
147b3 65 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20  e the format of 
147b4 74 68 69 73 20 62 74 72 65 65 20 70 61 67 65 2e  this btree page.
147b5 20 20 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20    The leaf flag 
147b6 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68  means that.** th
147b7 69 73 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63  is page has no c
147b8 68 69 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65  hildren.  The ze
147b9 72 6f 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e  rodata flag mean
147ba 73 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65  s that this page
147bb 20 63 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79   carries.** only
147bc 20 6b 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74   keys and no dat
147bd 61 2e 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66  a.  The intkey f
147be 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74  lag means that t
147bf 68 65 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65  he key is a inte
147c0 67 65 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20  ger.** which is 
147c1 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65  stored in the ke
147c2 79 20 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20  y size entry of 
147c3 74 68 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20  the cell header 
147c4 72 61 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a  rather than in.*
147c5 2a 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72  * the payload ar
147c6 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65  ea..**.** The ce
147c7 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
147c8 20 62 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66   begins on the f
147c9 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20  irst byte after 
147ca 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e  the page header.
147cb 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69  .** The cell poi
147cc 6e 74 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61  nter array conta
147cd 69 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65  ins zero or more
147ce 20 32 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20   2-byte numbers 
147cf 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66  which are.** off
147d0 73 65 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65  sets from the be
147d1 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70  ginning of the p
147d2 61 67 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20  age to the cell 
147d3 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63  content in the c
147d4 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61  ell.** content a
147d5 72 65 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70  rea.  The cell p
147d6 6f 69 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e  ointers occur in
147d7 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20   sorted order.  
147d8 54 68 65 20 73 79 73 74 65 6d 20 73 74 72 69 76  The system striv
147d9 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72  es.** to keep fr
147da 65 65 20 73 70 61 63 65 20 61 66 74 65 72 20 74  ee space after t
147db 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69  he last cell poi
147dc 6e 74 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77  nter so that new
147dd 20 63 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65   cells can.** be
147de 20 65 61 73 69 6c 79 20 61 64 64 65 64 20 77 69   easily added wi
147df 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
147e0 64 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70  defragment the p
147e1 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20  age..**.** Cell 
147e2 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65  content is store
147e3 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e  d at the very en
147e4 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e  d of the page an
147e5 64 20 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74  d grows toward t
147e6 68 65 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20  he.** beginning 
147e7 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a  of the page..**.
147e8 2a 2a 20 55 6e 75 73 65 64 20 73 70 61 63 65 20  ** Unused space 
147e9 77 69 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20  within the cell 
147ea 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20  content area is 
147eb 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61  collected into a
147ec 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a   linked list of.
147ed 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20  ** freeblocks.  
147ee 45 61 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69  Each freeblock i
147ef 73 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74  s at least 4 byt
147f0 65 73 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65  es in size.  The
147f1 20 62 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20   byte offset.** 
147f2 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65  to the first fre
147f3 65 62 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20  eblock is given 
147f4 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 20  in the header.  
147f5 46 72 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72  Freeblocks occur
147f6 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e   in.** increasin
147f7 67 20 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73  g order.  Becaus
147f8 65 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75  e a freeblock mu
147f9 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 34  st be at least 4
147fa 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a   bytes in size,.
147fb 2a 2a 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20  ** any group of 
147fc 33 20 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65  3 or fewer unuse
147fd 64 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63  d bytes in the c
147fe 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
147ff 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74   cannot.** exist
14800 20 6f 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63   on the freebloc
14801 6b 20 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75  k chain.  A grou
14802 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20  p of 3 or fewer 
14803 66 72 65 65 20 62 79 74 65 73 20 69 73 20 63 61  free bytes is ca
14804 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65  lled.** a fragme
14805 6e 74 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e  nt.  The total n
14806 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
14807 6e 20 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20  n all fragments 
14808 69 73 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20  is recorded..** 
14809 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64  in the page head
1480a 65 72 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a  er at offset 7..
1480b 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20  **.**    SIZE   
1480c 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20   DESCRIPTION.** 
1480d 20 20 20 20 20 32 20 20 20 20 20 42 79 74 65 20       2     Byte 
1480e 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65  offset of the ne
1480f 78 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20  xt freeblock.** 
14810 20 20 20 20 20 32 20 20 20 20 20 42 79 74 65 73       2     Bytes
14811 20 69 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f   in this freeblo
14812 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61  ck.**.** Cells a
14813 72 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c  re of variable l
14814 65 6e 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72  ength.  Cells ar
14815 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
14816 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
14817 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20  a at.** the end 
14818 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f  of the page.  Po
14819 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63 65  inters to the ce
1481a 6c 6c 73 20 61 72 65 20 69 6e 20 74 68 65 20 63  lls are in the c
1481b 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
1481c 79 0a 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69  y.** that immedi
1481d 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68  ately follows th
1481e 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20  e page header.  
1481f 43 65 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63  Cells is not nec
14820 65 73 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74  essarily.** cont
14821 69 67 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64  iguous or in ord
14822 65 72 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69  er, but cell poi
14823 6e 74 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67  nters are contig
14824 75 6f 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65  uous and in orde
14825 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f  r..**.** Cell co
14826 6e 74 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20  ntent makes use 
14827 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67  of variable leng
14828 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20  th integers.  A 
14829 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67  variable.** leng
1482a 74 68 20 69 6e 74 65 67 65 72 20 69 73 20 31 20  th integer is 1 
1482b 74 6f 20 39 20 62 79 74 65 73 20 77 68 65 72 65  to 9 bytes where
1482c 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74   the lower 7 bit
1482d 73 20 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79  s of each .** by
1482e 74 65 20 61 72 65 20 75 73 65 64 2e 20 20 54 68  te are used.  Th
1482f 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73  e integer consis
14830 74 73 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20  ts of all bytes 
14831 74 68 61 74 20 68 61 76 65 20 62 69 74 20 38 20  that have bit 8 
14832 73 65 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66  set and.** the f
14833 69 72 73 74 20 62 79 74 65 20 77 69 74 68 20 62  irst byte with b
14834 69 74 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65  it 8 clear.  The
14835 20 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e   most significan
14836 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e  t byte of the in
14837 74 65 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73  teger.** appears
14838 20 66 69 72 73 74 2e 20 20 41 20 76 61 72 69 61   first.  A varia
14839 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
1483a 65 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f  er may not be mo
1483b 72 65 20 74 68 61 6e 20 39 20 62 79 74 65 73 20  re than 9 bytes 
1483c 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70  long..** As a sp
1483d 65 63 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20  ecial case, all 
1483e 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 39  8 bytes of the 9
1483f 74 68 20 62 79 74 65 20 61 72 65 20 75 73 65 64  th byte are used
14840 20 61 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a   as data.  This.
14841 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62  ** allows a 64-b
14842 69 74 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65  it integer to be
14843 20 65 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79   encoded in 9 by
14844 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78  tes..**.**    0x
14845 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
14846 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20          becomes 
14847 20 30 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20   0x00000000.**  
14848 20 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20    0x7f          
14849 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
1484a 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a  mes  0x0000007f.
1484b 2a 2a 20 20 20 20 30 78 38 31 20 30 78 30 30 20  **    0x81 0x00 
1484c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1484d 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30  becomes  0x00000
1484e 30 38 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30  080.**    0x82 0
1484f 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  x00             
14850 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30      becomes  0x0
14851 30 30 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78  0000100.**    0x
14852 38 30 20 30 78 37 66 20 20 20 20 20 20 20 20 20  80 0x7f         
14853 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20          becomes 
14854 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20   0x0000007f.**  
14855 20 20 30 78 38 61 20 30 78 39 31 20 30 78 64 31    0x8a 0x91 0xd1
14856 20 30 78 61 63 20 30 78 37 38 20 20 62 65 63 6f   0xac 0x78  beco
14857 6d 65 73 20 20 30 78 31 32 33 34 35 36 37 38 0a  mes  0x12345678.
14858 2a 2a 20 20 20 20 30 78 38 31 20 30 78 38 31 20  **    0x81 0x81 
14859 30 78 38 31 20 30 78 38 31 20 30 78 30 31 20 20  0x81 0x81 0x01  
1485a 62 65 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34  becomes  0x10204
1485b 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62  081.**.** Variab
1485c 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le length intege
1485d 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20  rs are used for 
1485e 72 6f 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f  rowids and to ho
1485f 6c 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ld the number of
14860 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79  .** bytes of key
14861 20 61 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62   and data in a b
14862 74 72 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a  tree cell..**.**
14863 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   The content of 
14864 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b  a cell looks lik
14865 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
14866 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50   SIZE    DESCRIP
14867 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20  TION.**      4  
14868 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f     Page number o
14869 66 20 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64  f the left child
1486a 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61  . Omitted if lea
1486b 66 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a  f flag is set..*
1486c 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d  *     var    Num
1486d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
1486e 64 61 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66  data. Omitted if
1486f 20 74 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c   the zerodata fl
14870 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20  ag is set..**   
14871 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20    var    Number 
14872 6f 66 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e  of bytes of key.
14873 20 4f 72 20 74 68 65 20 6b 65 79 20 69 74 73 65   Or the key itse
14874 6c 66 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61  lf if intkey fla
14875 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20  g is set..**    
14876 20 20 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a    *     Payload.
14877 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 46 69  **      4     Fi
14878 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  rst page of the 
14879 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20  overflow chain. 
1487a 20 4f 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f   Omitted if no o
1487b 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76  verflow.**.** Ov
1487c 65 72 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72  erflow pages for
1487d 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  m a linked list.
1487e 20 20 45 61 63 68 20 70 61 67 65 20 65 78 63 65    Each page exce
1487f 70 74 20 74 68 65 20 6c 61 73 74 20 69 73 20 63  pt the last is c
14880 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c  ompletely.** fil
14881 6c 65 64 20 77 69 74 68 20 64 61 74 61 20 28 70  led with data (p
14882 61 67 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65  agesize - 4 byte
14883 73 29 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61  s).  The last pa
14884 67 65 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c  ge can have as l
14885 69 74 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79  ittle.** as 1 by
14886 74 65 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a  te of data..**.*
14887 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
14888 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
14889 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62   4     Page numb
1488a 65 72 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66  er of next overf
1488b 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20  low page.**     
1488c 20 2a 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a   *     Data.**.*
1488d 2a 20 46 72 65 65 6c 69 73 74 20 70 61 67 65 73  * Freelist pages
1488e 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62   come in two sub
1488f 74 79 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67  types: trunk pag
14890 65 73 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65  es and leaf page
14891 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20  s.  The.** file 
14892 68 65 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f  header points to
14893 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20   the first in a 
14894 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74  linked list of t
14895 72 75 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68  runk page.  Each
14896 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70   trunk.** page p
14897 6f 69 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c  oints to multipl
14898 65 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54  e leaf pages.  T
14899 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
1489a 6c 65 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20  leaf page is.** 
1489b 75 6e 73 70 65 63 69 66 69 65 64 2e 20 20 41 20  unspecified.  A 
1489c 74 72 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73  trunk page looks
1489d 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
1489e 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
1489f 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
148a0 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62   4     Page numb
148a1 65 72 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b  er of next trunk
148a2 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20   page.**      4 
148a3 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65      Number of le
148a4 61 66 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74  af pointers on t
148a5 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20  his page.**     
148a6 20 2a 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d   *     zero or m
148a7 6f 72 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72  ore pages number
148a8 73 20 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a  s of leaves.*/..
148a9 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ./* The followin
148aa 67 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d  g value is the m
148ab 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  aximum cell size
148ac 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61 78 69   assuming a maxi
148ad 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65  mum page.** size
148ae 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a   give above..*/.
148af 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f  #define MX_CELL_
148b0 53 49 5a 45 28 70 42 74 29 20 20 28 70 42 74 2d  SIZE(pBt)  (pBt-
148b1 3e 70 61 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a  >pageSize-8)../*
148b2 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
148b3 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20  ber of cells on 
148b4 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66  a single page of
148b5 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
148b6 54 68 69 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20  This.** assumes 
148b7 61 20 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73  a minimum cell s
148b8 69 7a 65 20 6f 66 20 36 20 62 79 74 65 73 20 20  ize of 6 bytes  
148b9 28 34 20 62 79 74 65 73 20 66 6f 72 20 74 68 65  (4 bytes for the
148ba 20 63 65 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a 20   cell itself.** 
148bb 70 6c 75 73 20 32 20 62 79 74 65 73 20 66 6f 72  plus 2 bytes for
148bc 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 74 68   the index to th
148bd 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61  e cell in the pa
148be 67 65 20 68 65 61 64 65 72 29 2e 20 20 53 75 63  ge header).  Suc
148bf 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c 73  h.** small cells
148c0 20 77 69 6c 6c 20 62 65 20 72 61 72 65 2c 20 62   will be rare, b
148c1 75 74 20 74 68 65 79 20 61 72 65 20 70 6f 73 73  ut they are poss
148c2 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ible..*/.#define
148c3 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 28 28   MX_CELL(pBt) ((
148c4 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29  pBt->pageSize-8)
148c5 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  /6)../* Forward 
148c6 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a  declarations */.
148c7 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d  typedef struct M
148c8 65 6d 50 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a  emPage MemPage;.
148c9 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
148ca 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f  tLock BtLock;../
148cb 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6d  *.** This is a m
148cc 61 67 69 63 20 73 74 72 69 6e 67 20 74 68 61 74  agic string that
148cd 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20   appears at the 
148ce 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65  beginning of eve
148cf 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74  ry.** SQLite dat
148d0 61 62 61 73 65 20 69 6e 20 6f 72 64 65 72 20 74  abase in order t
148d1 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 66  o identify the f
148d2 69 6c 65 20 61 73 20 61 20 72 65 61 6c 20 64 61  ile as a real da
148d3 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f  tabase..**.** Yo
148d4 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74 68 69  u can change thi
148d5 73 20 76 61 6c 75 65 20 61 74 20 63 6f 6d 70 69  s value at compi
148d6 6c 65 2d 74 69 6d 65 20 62 79 20 73 70 65 63 69  le-time by speci
148d7 66 79 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c  fying a.** -DSQL
148d8 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d  ITE_FILE_HEADER=
148d9 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d  "..." on the com
148da 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69  piler command-li
148db 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 68 65 61 64  ne.  The.** head
148dc 65 72 20 6d 75 73 74 20 62 65 20 65 78 61 63 74  er must be exact
148dd 6c 79 20 31 36 20 62 79 74 65 73 20 69 6e 63 6c  ly 16 bytes incl
148de 75 64 69 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74  uding the zero-t
148df 65 72 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20  erminator so.** 
148e0 74 68 65 20 73 74 72 69 6e 67 20 69 74 73 65 6c  the string itsel
148e1 66 20 73 68 6f 75 6c 64 20 62 65 20 31 35 20 63  f should be 15 c
148e2 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20  haracters long. 
148e3 20 49 66 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a   If you change.*
148e4 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20 74 68  * the header, th
148e5 65 6e 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c  en your custom l
148e6 69 62 72 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20  ibrary will not 
148e7 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20  be able to read 
148e8 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 67 65  .** databases ge
148e9 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 73  nerated by the s
148ea 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e  tandard tools an
148eb 64 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74  d the standard t
148ec 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  ools.** will not
148ed 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64   be able to read
148ee 20 64 61 74 61 62 61 73 65 73 20 63 72 65 61 74   databases creat
148ef 65 64 20 62 79 20 79 6f 75 72 20 63 75 73 74 6f  ed by your custo
148f0 6d 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69  m library..*/.#i
148f1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 46 49 4c  fndef SQLITE_FIL
148f2 45 5f 48 45 41 44 45 52 20 2f 2a 20 31 32 33 34  E_HEADER /* 1234
148f3 35 36 37 38 39 20 31 32 33 34 35 36 20 2a 2f 0a  56789 123456 */.
148f4 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  #  define SQLITE
148f5 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 22 53 51  _FILE_HEADER "SQ
148f6 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 22 0a 23  Lite format 3".#
148f7 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67  endif../*.** Pag
148f8 65 20 74 79 70 65 20 66 6c 61 67 73 2e 20 20 41  e type flags.  A
148f9 6e 20 4f 52 65 64 20 63 6f 6d 62 69 6e 61 74 69  n ORed combinati
148fa 6f 6e 20 6f 66 20 74 68 65 73 65 20 66 6c 61 67  on of these flag
148fb 73 20 61 70 70 65 61 72 20 61 73 20 74 68 65 0a  s appear as the.
148fc 2a 2a 20 66 69 72 73 74 20 62 79 74 65 20 6f 66  ** first byte of
148fd 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f   on-disk image o
148fe 66 20 65 76 65 72 79 20 42 54 72 65 65 20 70 61  f every BTree pa
148ff 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ge..*/.#define P
14900 54 46 5f 49 4e 54 4b 45 59 20 20 20 20 30 78 30  TF_INTKEY    0x0
14901 31 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 5a 45  1.#define PTF_ZE
14902 52 4f 44 41 54 41 20 20 30 78 30 32 0a 23 64 65  RODATA  0x02.#de
14903 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 44 41 54  fine PTF_LEAFDAT
14904 41 20 20 30 78 30 34 0a 23 64 65 66 69 6e 65 20  A  0x04.#define 
14905 50 54 46 5f 4c 45 41 46 20 20 20 20 20 20 30 78  PTF_LEAF      0x
14906 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63  08../*.** As eac
14907 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69  h page of the fi
14908 6c 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74  le is loaded int
14909 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73  o memory, an ins
1490a 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
1490b 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74  lowing.** struct
1490c 75 72 65 20 69 73 20 61 70 70 65 6e 64 65 64 20  ure is appended 
1490d 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  and initialized 
1490e 74 6f 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 73  to zero.  This s
1490f 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 0a  tructure stores.
14910 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ** information a
14911 62 6f 75 74 20 74 68 65 20 70 61 67 65 20 74 68  bout the page th
14912 61 74 20 69 73 20 64 65 63 6f 64 65 64 20 66 72  at is decoded fr
14913 6f 6d 20 74 68 65 20 72 61 77 20 66 69 6c 65 20  om the raw file 
14914 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  page..**.** The 
14915 70 50 61 72 65 6e 74 20 66 69 65 6c 64 20 70 6f  pParent field po
14916 69 6e 74 73 20 62 61 63 6b 20 74 6f 20 74 68 65  ints back to the
14917 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20 54   parent page.  T
14918 68 69 73 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f  his allows us to
14919 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 74 68 65 20  .** walk up the 
1491a 42 54 72 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c  BTree from any l
1491b 65 61 66 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e  eaf to the root.
1491c 20 20 43 61 72 65 20 6d 75 73 74 20 62 65 20 74    Care must be t
1491d 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66  aken to.** unref
1491e 28 29 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  () the parent pa
1491f 67 65 20 70 6f 69 6e 74 65 72 20 77 68 65 6e 20  ge pointer when 
14920 74 68 69 73 20 70 61 67 65 20 69 73 20 6e 6f 20  this page is no 
14921 6c 6f 6e 67 65 72 20 72 65 66 65 72 65 6e 63 65  longer reference
14922 64 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 44 65  d..** The pageDe
14923 73 74 72 75 63 74 6f 72 28 29 20 72 6f 75 74 69  structor() routi
14924 6e 65 20 68 61 6e 64 6c 65 73 20 74 68 61 74 20  ne handles that 
14925 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63  chore..**.** Acc
14926 65 73 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64  ess to all field
14927 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
14928 75 72 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65  ure is controlle
14929 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 0a 2a  d by the mutex.*
1492a 2a 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50  * stored in MemP
1492b 61 67 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a  age.pBt->mutex..
1492c 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 50 61 67  */.struct MemPag
1492d 65 20 7b 0a 20 20 75 38 20 69 73 49 6e 69 74 3b  e {.  u8 isInit;
1492e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1492f 75 65 20 69 66 20 70 72 65 76 69 6f 75 73 6c 79  ue if previously
14930 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55   initialized. MU
14931 53 54 20 42 45 20 46 49 52 53 54 21 20 2a 2f 0a  ST BE FIRST! */.
14932 20 20 75 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20    u8 nOverflow; 
14933 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
14934 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c   of overflow cel
14935 6c 20 62 6f 64 69 65 73 20 69 6e 20 61 43 65 6c  l bodies in aCel
14936 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b  l[] */.  u8 intK
14937 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey;           /*
14938 20 54 72 75 65 20 69 66 20 69 6e 74 6b 65 79 20   True if intkey 
14939 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20  flag is set */. 
1493a 20 75 38 20 6c 65 61 66 3b 20 20 20 20 20 20 20   u8 leaf;       
1493b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1493c 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65   leaf flag is se
1493d 74 20 2a 2f 0a 20 20 75 38 20 68 61 73 44 61 74  t */.  u8 hasDat
1493e 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  a;          /* T
1493f 72 75 65 20 69 66 20 74 68 69 73 20 70 61 67 65  rue if this page
14940 20 73 74 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a   stores data */.
14941 20 20 75 38 20 68 64 72 4f 66 66 73 65 74 3b 20    u8 hdrOffset; 
14942 20 20 20 20 20 20 20 2f 2a 20 31 30 30 20 66 6f         /* 100 fo
14943 72 20 70 61 67 65 20 31 2e 20 20 30 20 6f 74 68  r page 1.  0 oth
14944 65 72 77 69 73 65 20 2a 2f 0a 20 20 75 38 20 63  erwise */.  u8 c
14945 68 69 6c 64 50 74 72 53 69 7a 65 3b 20 20 20 20  hildPtrSize;    
14946 20 2f 2a 20 30 20 69 66 20 6c 65 61 66 3d 3d 31   /* 0 if leaf==1
14947 2e 20 20 34 20 69 66 20 6c 65 61 66 3d 3d 30 20  .  4 if leaf==0 
14948 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61  */.  u16 maxLoca
14949 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70  l;        /* Cop
1494a 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 61  y of BtShared.ma
1494b 78 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72  xLocal or BtShar
1494c 65 64 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20  ed.maxLeaf */.  
1494d 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20  u16 minLocal;   
1494e 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
1494f 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61  BtShared.minLoca
14950 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 69  l or BtShared.mi
14951 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 63  nLeaf */.  u16 c
14952 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20  ellOffset;      
14953 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 44 61 74  /* Index in aDat
14954 61 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20  a of first cell 
14955 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36  pointer */.  u16
14956 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20   nFree;         
14957 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
14958 72 65 65 20 62 79 74 65 73 20 6f 6e 20 74 68 65  ree bytes on the
14959 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e   page */.  u16 n
1495a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
1495b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c  /* Number of cel
1495c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c  ls on this page,
1495d 20 6c 6f 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20   local and ovfl 
1495e 2a 2f 0a 20 20 75 31 36 20 6d 61 73 6b 50 61 67  */.  u16 maskPag
1495f 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73  e;        /* Mas
14960 6b 20 66 6f 72 20 70 61 67 65 20 6f 66 66 73 65  k for page offse
14961 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f  t */.  struct _O
14962 76 66 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43  vflCell {   /* C
14963 65 6c 6c 73 20 74 68 61 74 20 77 69 6c 6c 20 6e  ells that will n
14964 6f 74 20 66 69 74 20 6f 6e 20 61 44 61 74 61 5b  ot fit on aData[
14965 5d 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70 43 65  ] */.    u8 *pCe
14966 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ll;          /* 
14967 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  Pointers to the 
14968 62 6f 64 79 20 6f 66 20 74 68 65 20 6f 76 65 72  body of the over
14969 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20  flow cell */.   
1496a 20 75 31 36 20 69 64 78 3b 20 20 20 20 20 20 20   u16 idx;       
1496b 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74       /* Insert t
1496c 68 69 73 20 63 65 6c 6c 20 62 65 66 6f 72 65 20  his cell before 
1496d 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66  idx-th non-overf
1496e 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20  low cell */.  } 
1496f 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68  aOvfl[5];.  BtSh
14970 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
14971 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 42   /* Pointer to B
14972 74 53 68 61 72 65 64 20 74 68 61 74 20 74 68 69  tShared that thi
14973 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20 6f  s page is part o
14974 66 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 61  f */.  u8 *aData
14975 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ;           /* P
14976 6f 69 6e 74 65 72 20 74 6f 20 64 69 73 6b 20 69  ointer to disk i
14977 6d 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 65  mage of the page
14978 20 64 61 74 61 20 2a 2f 0a 20 20 44 62 50 61 67   data */.  DbPag
14979 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 20 20  e *pDbPage;     
1497a 2f 2a 20 50 61 67 65 72 20 70 61 67 65 20 68 61  /* Pager page ha
1497b 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  ndle */.  Pgno p
1497c 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 2f  gno;           /
1497d 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  * Page number fo
1497e 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 7d  r this page */.}
1497f 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d  ;../*.** The in-
14980 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20  memory image of 
14981 61 20 64 69 73 6b 20 70 61 67 65 20 68 61 73 20  a disk page has 
14982 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e  the auxiliary in
14983 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64  formation append
14984 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64  ed.** to the end
14985 2e 20 20 45 58 54 52 41 5f 53 49 5a 45 20 69 73  .  EXTRA_SIZE is
14986 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
14987 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65  ytes of space ne
14988 65 64 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20  eded to hold.** 
14989 74 68 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72  that extra infor
1498a 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  mation..*/.#defi
1498b 6e 65 20 45 58 54 52 41 5f 53 49 5a 45 20 73 69  ne EXTRA_SIZE si
1498c 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f  zeof(MemPage)../
1498d 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69  *.** A linked li
1498e 73 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  st of the follow
1498f 69 6e 67 20 73 74 72 75 63 74 75 72 65 73 20 69  ing structures i
14990 73 20 73 74 6f 72 65 64 20 61 74 20 42 74 53 68  s stored at BtSh
14991 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c  ared.pLock..** L
14992 6f 63 6b 73 20 61 72 65 20 61 64 64 65 64 20 28  ocks are added (
14993 6f 72 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d  or upgraded from
14994 20 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52   READ_LOCK to WR
14995 49 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61  ITE_LOCK) when a
14996 20 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f   cursor .** is o
14997 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62  pened on the tab
14998 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67  le with root pag
14999 65 20 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c  e BtShared.iTabl
1499a 65 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d  e. Locks are rem
1499b 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69  oved.** from thi
1499c 73 20 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72  s list when a tr
1499d 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
1499e 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64  mitted or rolled
1499f 20 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a   back, or when.*
149a0 2a 20 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65  * a btree handle
149a1 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73   is closed..*/.s
149a2 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20  truct BtLock {. 
149a3 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20   Btree *pBtree; 
149a4 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
149a5 68 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74  handle holding t
149a6 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67  his lock */.  Pg
149a7 6e 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20  no iTable;      
149a8 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65      /* Root page
149a9 20 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75   of table */.  u
149aa 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20  8 eLock;        
149ab 20 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43       /* READ_LOC
149ac 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20  K or WRITE_LOCK 
149ad 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65  */.  BtLock *pNe
149ae 78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  xt;        /* Ne
149af 78 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70  xt in BtShared.p
149b0 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a  Lock list */.};.
149b1 0a 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61  ./* Candidate va
149b2 6c 75 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e  lues for BtLock.
149b3 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65  eLock */.#define
149b4 20 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31   READ_LOCK     1
149b5 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c  .#define WRITE_L
149b6 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a 20 41 20 42  OCK    2../* A B
149b7 74 72 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a  tree handle.**.*
149b8 2a 20 41 20 64 61 74 61 62 61 73 65 20 63 6f 6e  * A database con
149b9 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  nection contains
149ba 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
149bb 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20   instance of.** 
149bc 74 68 69 73 20 6f 62 6a 65 63 74 20 66 6f 72 20  this object for 
149bd 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20 66  every database f
149be 69 6c 65 20 74 68 61 74 20 69 74 20 68 61 73 20  ile that it has 
149bf 6f 70 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75  open.  This stru
149c0 63 74 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61 71  cture.** is opaq
149c1 75 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ue to the databa
149c2 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20  se connection.  
149c3 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
149c4 6e 65 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a  nection cannot.*
149c5 2a 20 73 65 65 20 74 68 65 20 69 6e 74 65 72 6e  * see the intern
149c6 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72 75  als of this stru
149c7 63 74 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20 64  cture and only d
149c8 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65  eals with pointe
149c9 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73 74  rs to.** this st
149ca 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46  ructure..**.** F
149cb 6f 72 20 73 6f 6d 65 20 64 61 74 61 62 61 73 65  or some database
149cc 20 66 69 6c 65 73 2c 20 74 68 65 20 73 61 6d 65   files, the same
149cd 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61   underlying data
149ce 62 61 73 65 20 63 61 63 68 65 20 6d 69 67 68 74  base cache might
149cf 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62   be .** shared b
149d0 65 74 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65 20  etween multiple 
149d1 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49 6e  connections.  In
149d2 20 74 68 61 74 20 63 61 73 65 2c 20 65 61 63 68   that case, each
149d3 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 68   connection.** h
149d4 61 73 20 69 74 20 6f 77 6e 20 69 6e 73 74 61 6e  as it own instan
149d5 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
149d6 74 2e 20 20 42 75 74 20 65 61 63 68 20 69 6e 73  t.  But each ins
149d7 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
149d8 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  ject.** points t
149d9 6f 20 74 68 65 20 73 61 6d 65 20 42 74 53 68 61  o the same BtSha
149da 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 65  red object.  The
149db 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20   database cache 
149dc 61 6e 64 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d  and the.** schem
149dd 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
149de 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  h the database f
149df 69 6c 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74  ile are all cont
149e0 61 69 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20  ained within.** 
149e1 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
149e2 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66  ect..**.** All f
149e3 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74  ields in this st
149e4 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65  ructure are acce
149e5 73 73 65 64 20 75 6e 64 65 72 20 73 71 6c 69 74  ssed under sqlit
149e6 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65  e3.mutex..** The
149e7 20 70 42 74 20 70 6f 69 6e 74 65 72 20 69 74 73   pBt pointer its
149e8 65 6c 66 20 6d 61 79 20 6e 6f 74 20 62 65 20 63  elf may not be c
149e9 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74 68 65  hanged while the
149ea 72 65 20 65 78 69 73 74 73 20 63 75 72 73 6f 72  re exists cursor
149eb 73 20 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 66  s .** in the ref
149ec 65 72 65 6e 63 65 64 20 42 74 53 68 61 72 65 64  erenced BtShared
149ed 20 74 68 61 74 20 70 6f 69 6e 74 20 62 61 63 6b   that point back
149ee 20 74 6f 20 74 68 69 73 20 42 74 72 65 65 20 73   to this Btree s
149ef 69 6e 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75  ince those.** cu
149f0 72 73 6f 72 73 20 68 61 76 65 20 74 6f 20 64 6f  rsors have to do
149f1 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69 73   go through this
149f2 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 74   Btree to find t
149f3 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61 6e  heir BtShared an
149f4 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e 20  d.** they often 
149f5 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68 6f  do so without ho
149f6 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75  lding sqlite3.mu
149f7 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42  tex..*/.struct B
149f8 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  tree {.  sqlite3
149f9 20 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54   *db;       /* T
149fa 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
149fb 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74  ection holding t
149fc 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42  his btree */.  B
149fd 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20  tShared *pBt;   
149fe 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 6f    /* Sharable co
149ff 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62 74  ntent of this bt
14a00 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72  ree */.  u8 inTr
14a01 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ans;        /* T
14a02 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53  RANS_NONE, TRANS
14a03 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f 57  _READ or TRANS_W
14a04 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 61  RITE */.  u8 sha
14a05 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20  rable;       /* 
14a06 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20 73  True if we can s
14a07 68 61 72 65 20 70 42 74 20 77 69 74 68 20 61 6e  hare pBt with an
14a08 6f 74 68 65 72 20 64 62 20 2a 2f 0a 20 20 75 38  other db */.  u8
14a09 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20   locked;        
14a0a 20 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20 63   /* True if db c
14a0b 75 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42 74  urrently has pBt
14a0c 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74   locked */.  int
14a0d 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20   wantToLock;    
14a0e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73  /* Number of nes
14a0f 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  ted calls to sql
14a10 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 29  ite3BtreeEnter()
14a11 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 75   */.  int nBacku
14a12 70 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  p;       /* Numb
14a13 65 72 20 6f 66 20 62 61 63 6b 75 70 20 6f 70 65  er of backup ope
14a14 72 61 74 69 6f 6e 73 20 72 65 61 64 69 6e 67 20  rations reading 
14a15 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20  this btree */.  
14a16 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 20  Btree *pNext;   
14a17 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 74     /* List of ot
14a18 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74 72  her sharable Btr
14a19 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d  ees from the sam
14a1a 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65 20  e db */.  Btree 
14a1b 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a 20  *pPrev;      /* 
14a1c 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66 20  Back pointer of 
14a1d 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a 2f  the same list */
14a1e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14a1f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
14a20 45 0a 20 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b 3b  E.  BtLock lock;
14a21 20 20 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74         /* Object
14a22 20 75 73 65 64 20 74 6f 20 6c 6f 63 6b 20 70 61   used to lock pa
14a23 67 65 20 31 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  ge 1 */.#endif.}
14a24 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e 69  ;../*.** Btree.i
14a25 6e 54 72 61 6e 73 20 6d 61 79 20 74 61 6b 65 20  nTrans may take 
14a26 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
14a27 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a  wing values..**.
14a28 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72 65 64  ** If the shared
14a29 2d 64 61 74 61 20 65 78 74 65 6e 73 69 6f 6e 20  -data extension 
14a2a 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 72  is enabled, ther
14a2b 65 20 6d 61 79 20 62 65 20 6d 75 6c 74 69 70 6c  e may be multipl
14a2c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74 68  e users.** of th
14a2d 65 20 42 74 72 65 65 20 73 74 72 75 63 74 75 72  e Btree structur
14a2e 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f  e. At most one o
14a2f 66 20 74 68 65 73 65 20 6d 61 79 20 6f 70 65 6e  f these may open
14a30 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63   a write transac
14a31 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e 79  tion,.** but any
14a32 20 6e 75 6d 62 65 72 20 6d 61 79 20 68 61 76 65   number may have
14a33 20 61 63 74 69 76 65 20 72 65 61 64 20 74 72 61   active read tra
14a34 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64  nsactions..*/.#d
14a35 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45  efine TRANS_NONE
14a36 20 20 30 0a 23 64 65 66 69 6e 65 20 54 52 41 4e    0.#define TRAN
14a37 53 5f 52 45 41 44 20 20 31 0a 23 64 65 66 69 6e  S_READ  1.#defin
14a38 65 20 54 52 41 4e 53 5f 57 52 49 54 45 20 32 0a  e TRANS_WRITE 2.
14a39 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
14a3a 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
14a3b 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73  t represents a s
14a3c 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66  ingle database f
14a3d 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69  ile..** .** A si
14a3e 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69  ngle database fi
14a3f 6c 65 20 63 61 6e 20 62 65 20 69 6e 20 75 73 65  le can be in use
14a40 20 61 73 20 74 68 65 20 73 61 6d 65 20 74 69 6d   as the same tim
14a41 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d  e by two.** or m
14a42 6f 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ore database con
14a43 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e 20  nections.  When 
14a44 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e  two or more conn
14a45 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73  ections are.** s
14a46 68 61 72 69 6e 67 20 74 68 65 20 73 61 6d 65 20  haring the same 
14a47 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 65  database file, e
14a48 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  ach connection h
14a49 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 69  as it own.** pri
14a4a 76 61 74 65 20 42 74 72 65 65 20 6f 62 6a 65 63  vate Btree objec
14a4b 74 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 61  t for the file a
14a4c 6e 64 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65  nd each of those
14a4d 20 42 74 72 65 65 73 20 70 6f 69 6e 74 73 0a 2a   Btrees points.*
14a4e 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 42 74  * to this one Bt
14a4f 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20  Shared object.  
14a50 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 69 73  BtShared.nRef is
14a51 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
14a52 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75  * connections cu
14a53 72 72 65 6e 74 6c 79 20 73 68 61 72 69 6e 67 20  rrently sharing 
14a54 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69  this database fi
14a55 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73  le..**.** Fields
14a56 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75   in this structu
14a57 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64 20  re are accessed 
14a58 75 6e 64 65 72 20 74 68 65 20 42 74 53 68 61 72  under the BtShar
14a59 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 65  ed.mutex.** mute
14a5a 78 2c 20 65 78 63 65 70 74 20 66 6f 72 20 6e 52  x, except for nR
14a5b 65 66 20 61 6e 64 20 70 4e 65 78 74 20 77 68 69  ef and pNext whi
14a5c 63 68 20 61 72 65 20 61 63 63 65 73 73 65 64 20  ch are accessed 
14a5d 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c 6f  under the.** glo
14a5e 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  bal SQLITE_MUTEX
14a5f 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d  _STATIC_MASTER m
14a60 75 74 65 78 2e 20 20 54 68 65 20 70 50 61 67 65  utex.  The pPage
14a61 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e  r field.** may n
14a62 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f  ot be modified o
14a63 6e 63 65 20 69 74 20 69 73 20 69 6e 69 74 69 61  nce it is initia
14a64 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f 6e 67 20  lly set as long 
14a65 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 68  as nRef>0..** Th
14a66 65 20 70 53 63 68 65 6d 61 20 66 69 65 6c 64 20  e pSchema field 
14a67 6d 61 79 20 62 65 20 73 65 74 20 6f 6e 63 65 20  may be set once 
14a68 75 6e 64 65 72 20 42 74 53 68 61 72 65 64 2e 6d  under BtShared.m
14a69 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65 72  utex and.** ther
14a6a 65 61 66 74 65 72 20 69 73 20 75 6e 63 68 61 6e  eafter is unchan
14a6b 67 65 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e  ged as long as n
14a6c 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 50  Ref>0..**.** isP
14a6d 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  ending:.**.**   
14a6e 49 66 20 61 20 42 74 53 68 61 72 65 64 20 63 6c  If a BtShared cl
14a6f 69 65 6e 74 20 66 61 69 6c 73 20 74 6f 20 6f 62  ient fails to ob
14a70 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63  tain a write-loc
14a71 6b 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 0a  k on a database.
14a72 2a 2a 20 20 20 74 61 62 6c 65 20 28 62 65 63 61  **   table (beca
14a73 75 73 65 20 74 68 65 72 65 20 65 78 69 73 74 73  use there exists
14a74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61   one or more rea
14a75 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 74  d-locks on the t
14a76 61 62 6c 65 29 2c 0a 2a 2a 20 20 20 74 68 65 20  able),.**   the 
14a77 73 68 61 72 65 64 2d 63 61 63 68 65 20 65 6e 74  shared-cache ent
14a78 65 72 73 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63  ers 'pending-loc
14a79 6b 27 20 73 74 61 74 65 20 61 6e 64 20 69 73 50  k' state and isP
14a7a 65 6e 64 69 6e 67 20 69 73 0a 2a 2a 20 20 20 73  ending is.**   s
14a7b 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a 2a  et to true..**.*
14a7c 2a 20 20 20 54 68 65 20 73 68 61 72 65 64 2d 63  *   The shared-c
14a7d 61 63 68 65 20 6c 65 61 76 65 73 20 74 68 65 20  ache leaves the 
14a7e 27 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 27 20 73  'pending lock' s
14a7f 74 61 74 65 20 77 68 65 6e 20 65 69 74 68 65 72  tate when either
14a80 20 6f 66 0a 2a 2a 20 20 20 74 68 65 20 66 6f 6c   of.**   the fol
14a81 6c 6f 77 69 6e 67 20 6f 63 63 75 72 3a 0a 2a 2a  lowing occur:.**
14a82 0a 2a 2a 20 20 20 20 20 31 29 20 54 68 65 20 63  .**     1) The c
14a83 75 72 72 65 6e 74 20 77 72 69 74 65 72 20 28 42  urrent writer (B
14a84 74 53 68 61 72 65 64 2e 70 57 72 69 74 65 72 29  tShared.pWriter)
14a85 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 74   concludes its t
14a86 72 61 6e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a 2a  ransaction, OR.*
14a87 2a 20 20 20 20 20 32 29 20 54 68 65 20 6e 75 6d  *     2) The num
14a88 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c  ber of locks hel
14a89 64 20 62 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65  d by other conne
14a8a 63 74 69 6f 6e 73 20 64 72 6f 70 73 20 74 6f 20  ctions drops to 
14a8b 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 68  zero..**.**   wh
14a8c 69 6c 65 20 69 6e 20 74 68 65 20 27 70 65 6e 64  ile in the 'pend
14a8d 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 2c  ing-lock' state,
14a8e 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d   no connection m
14a8f 61 79 20 73 74 61 72 74 20 61 20 6e 65 77 0a 2a  ay start a new.*
14a90 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  *   transaction.
14a91 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 65  .**.**   This fe
14a92 61 74 75 72 65 20 69 73 20 69 6e 63 6c 75 64 65  ature is include
14a93 64 20 74 6f 20 68 65 6c 70 20 70 72 65 76 65 6e  d to help preven
14a94 74 20 77 72 69 74 65 72 2d 73 74 61 72 76 61 74  t writer-starvat
14a95 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42  ion..*/.struct B
14a96 74 53 68 61 72 65 64 20 7b 0a 20 20 50 61 67 65  tShared {.  Page
14a97 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20  r *pPager;      
14a98 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 63 61    /* The page ca
14a99 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  che */.  sqlite3
14a9a 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f   *db;          /
14a9b 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
14a9c 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ction currently 
14a9d 75 73 69 6e 67 20 74 68 69 73 20 42 74 72 65 65  using this Btree
14a9e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
14a9f 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41  pCursor;    /* A
14aa0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65   list of all ope
14aa1 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d  n cursors */.  M
14aa2 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 20  emPage *pPage1; 
14aa3 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61       /* First pa
14aa4 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
14aa5 73 65 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f  se */.  u8 readO
14aa6 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  nly;          /*
14aa7 20 54 72 75 65 20 69 66 20 74 68 65 20 75 6e 64   True if the und
14aa8 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 69 73 20  erlying file is 
14aa9 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38  readonly */.  u8
14aaa 20 70 61 67 65 53 69 7a 65 46 69 78 65 64 3b 20   pageSizeFixed; 
14aab 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
14aac 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e  he page size can
14aad 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68   no longer be ch
14aae 61 6e 67 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66  anged */.#ifndef
14aaf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
14ab0 4f 56 41 43 55 55 4d 0a 20 20 75 38 20 61 75 74  OVACUUM.  u8 aut
14ab1 6f 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20  oVacuum;        
14ab2 2f 2a 20 54 72 75 65 20 69 66 20 61 75 74 6f 2d  /* True if auto-
14ab3 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65  vacuum is enable
14ab4 64 20 2a 2f 0a 20 20 75 38 20 69 6e 63 72 56 61  d */.  u8 incrVa
14ab5 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20  cuum;        /* 
14ab6 54 72 75 65 20 69 66 20 69 6e 63 72 2d 76 61 63  True if incr-vac
14ab7 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a  uum is enabled *
14ab8 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20 70  /.#endif.  u16 p
14ab9 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  ageSize;        
14aba 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
14abb 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 61 20 70   of bytes on a p
14abc 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 75 73 61  age */.  u16 usa
14abd 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 2f  bleSize;       /
14abe 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62  * Number of usab
14abf 6c 65 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68  le bytes on each
14ac0 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6d   page */.  u16 m
14ac1 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20  axLocal;        
14ac2 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61   /* Maximum loca
14ac3 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e  l payload in non
14ac4 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73  -LEAFDATA tables
14ac5 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63   */.  u16 minLoc
14ac6 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  al;         /* M
14ac7 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79  inimum local pay
14ac8 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46  load in non-LEAF
14ac9 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20  DATA tables */. 
14aca 20 75 31 36 20 6d 61 78 4c 65 61 66 3b 20 20 20   u16 maxLeaf;   
14acb 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
14acc 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20  m local payload 
14acd 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61  in a LEAFDATA ta
14ace 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e  ble */.  u16 min
14acf 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f  Leaf;          /
14ad0 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20  * Minimum local 
14ad1 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41  payload in a LEA
14ad2 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20  FDATA table */. 
14ad3 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f   u8 inTransactio
14ad4 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 61  n;     /* Transa
14ad5 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20  ction state */. 
14ad6 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f   int nTransactio
14ad7 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n;     /* Number
14ad8 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e 73 61 63   of open transac
14ad9 74 69 6f 6e 73 20 28 72 65 61 64 20 2b 20 77 72  tions (read + wr
14ada 69 74 65 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ite) */.  void *
14adb 70 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20  pSchema;        
14adc 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 70  /* Pointer to sp
14add 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  ace allocated by
14ade 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
14adf 65 6d 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20  ema() */.  void 
14ae0 28 2a 78 46 72 65 65 53 63 68 65 6d 61 29 28 76  (*xFreeSchema)(v
14ae1 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72  oid*);  /* Destr
14ae2 75 63 74 6f 72 20 66 6f 72 20 42 74 53 68 61 72  uctor for BtShar
14ae3 65 64 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20  ed.pSchema */.  
14ae4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
14ae5 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63  utex; /* Non-rec
14ae6 75 72 73 69 76 65 20 6d 75 74 65 78 20 72 65 71  ursive mutex req
14ae7 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73 20  uired to access 
14ae8 74 68 69 73 20 73 74 72 75 63 74 20 2a 2f 0a 20  this struct */. 
14ae9 20 42 69 74 76 65 63 20 2a 70 48 61 73 43 6f 6e   Bitvec *pHasCon
14aea 74 65 6e 74 3b 20 20 2f 2a 20 53 65 74 20 6f 66  tent;  /* Set of
14aeb 20 70 61 67 65 73 20 6d 6f 76 65 64 20 74 6f 20   pages moved to 
14aec 66 72 65 65 2d 6c 69 73 74 20 74 68 69 73 20 74  free-list this t
14aed 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 69  ransaction */.#i
14aee 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
14aef 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
14af0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
14af1 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
14af2 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
14af3 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
14af4 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
14af5 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e  pNext;      /* N
14af6 65 78 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66  ext on a list of
14af7 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61 72   sharable BtShar
14af8 65 64 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 20  ed structs */.  
14af9 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20  BtLock *pLock;  
14afa 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
14afb 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74   locks held on t
14afc 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 65  his shared-btree
14afd 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 72   struct */.  Btr
14afe 65 65 20 2a 70 57 72 69 74 65 72 3b 20 20 20 20  ee *pWriter;    
14aff 20 20 20 2f 2a 20 42 74 72 65 65 20 77 69 74 68     /* Btree with
14b00 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20   currently open 
14b01 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
14b02 6e 20 2a 2f 0a 20 20 75 38 20 69 73 45 78 63 6c  n */.  u8 isExcl
14b03 75 73 69 76 65 3b 20 20 20 20 20 20 20 2f 2a 20  usive;       /* 
14b04 54 72 75 65 20 69 66 20 70 57 72 69 74 65 72 20  True if pWriter 
14b05 68 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45  has an EXCLUSIVE
14b06 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 62 20   lock on the db 
14b07 2a 2f 0a 20 20 75 38 20 69 73 50 65 6e 64 69 6e  */.  u8 isPendin
14b08 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  g;         /* If
14b09 20 77 61 69 74 69 6e 67 20 66 6f 72 20 72 65 61   waiting for rea
14b0a 64 2d 6c 6f 63 6b 73 20 74 6f 20 63 6c 65 61 72  d-locks to clear
14b0b 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 38 20   */.#endif.  u8 
14b0c 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20  *pTmpSpace;     
14b0d 20 20 20 2f 2a 20 42 74 53 68 61 72 65 64 2e 70     /* BtShared.p
14b0e 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66  ageSize bytes of
14b0f 20 73 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75   space for tmp u
14b10 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  se */.};../*.** 
14b11 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
14b12 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
14b13 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74  ucture is used t
14b14 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69  o hold informati
14b15 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61 20 63 65  on.** about a ce
14b16 6c 6c 2e 20 20 54 68 65 20 70 61 72 73 65 43 65  ll.  The parseCe
14b17 6c 6c 50 74 72 28 29 20 66 75 6e 63 74 69 6f 6e  llPtr() function
14b18 20 66 69 6c 6c 73 20 69 6e 20 74 68 69 73 20 73   fills in this s
14b19 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 61 73 65  tructure.** base
14b1a 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  d on information
14b1b 20 65 78 74 72 61 63 74 20 66 72 6f 6d 20 74 68   extract from th
14b1c 65 20 72 61 77 20 64 69 73 6b 20 70 61 67 65 2e  e raw disk page.
14b1d 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
14b1e 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c  ct CellInfo Cell
14b1f 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 43 65 6c  Info;.struct Cel
14b20 6c 49 6e 66 6f 20 7b 0a 20 20 75 38 20 2a 70 43  lInfo {.  u8 *pC
14b21 65 6c 6c 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ell;     /* Poin
14b22 74 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74  ter to the start
14b23 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74   of cell content
14b24 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20   */.  i64 nKey; 
14b25 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20       /* The key 
14b26 66 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65  for INTKEY table
14b27 73 2c 20 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  s, or number of 
14b28 62 79 74 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a  bytes in key */.
14b29 20 20 75 33 32 20 6e 44 61 74 61 3b 20 20 20 20    u32 nData;    
14b2a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
14b2b 74 65 73 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20  tes of data */. 
14b2c 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20   u32 nPayload;  
14b2d 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20  /* Total amount 
14b2e 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20  of payload */.  
14b2f 75 31 36 20 6e 48 65 61 64 65 72 3b 20 20 20 2f  u16 nHeader;   /
14b30 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65  * Size of the ce
14b31 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 65  ll content heade
14b32 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  r in bytes */.  
14b33 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f  u16 nLocal;    /
14b34 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c  * Amount of payl
14b35 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79  oad held locally
14b36 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76 65 72 66   */.  u16 iOverf
14b37 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65 74 20 74  low; /* Offset t
14b38 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  o overflow page 
14b39 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f 20 69 66  number.  Zero if
14b3a 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a   no overflow */.
14b3b 20 20 75 31 36 20 6e 53 69 7a 65 3b 20 20 20 20    u16 nSize;    
14b3c 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
14b3d 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 6e 20  cell content on 
14b3e 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20  the main b-tree 
14b3f 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  page */.};../*.*
14b40 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  * Maximum depth 
14b41 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 42 2d 54  of an SQLite B-T
14b42 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 20 41  ree structure. A
14b43 6e 79 20 42 2d 54 72 65 65 20 64 65 65 70 65 72  ny B-Tree deeper
14b44 20 74 68 61 6e 0a 2a 2a 20 74 68 69 73 20 77 69   than.** this wi
14b45 6c 6c 20 62 65 20 64 65 63 6c 61 72 65 64 20 63  ll be declared c
14b46 6f 72 72 75 70 74 2e 20 54 68 69 73 20 76 61 6c  orrupt. This val
14b47 75 65 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64  ue is calculated
14b48 20 62 61 73 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d   based on a.** m
14b49 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20  aximum database 
14b4a 73 69 7a 65 20 6f 66 20 32 5e 33 31 20 70 61 67  size of 2^31 pag
14b4b 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 66 61 6e  es a minimum fan
14b4c 6f 75 74 20 6f 66 20 32 20 66 6f 72 20 61 0a 2a  out of 2 for a.*
14b4d 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 61 6e 64 20  * root-node and 
14b4e 33 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20  3 for all other 
14b4f 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a  internal nodes..
14b50 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 65 65 20  **.** If a tree 
14b51 74 68 61 74 20 61 70 70 65 61 72 73 20 74 6f 20  that appears to 
14b52 62 65 20 74 61 6c 6c 65 72 20 74 68 61 6e 20 74  be taller than t
14b53 68 69 73 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  his is encounter
14b54 65 64 2c 20 69 74 20 69 73 0a 2a 2a 20 61 73 73  ed, it is.** ass
14b55 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 64 61  umed that the da
14b56 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70  tabase is corrup
14b57 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  t..*/.#define BT
14b58 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48  CURSOR_MAX_DEPTH
14b59 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72   20../*.** A cur
14b5a 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  sor is a pointer
14b5b 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72   to a particular
14b5c 20 65 6e 74 72 79 20 77 69 74 68 69 6e 20 61 20   entry within a 
14b5d 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 62 2d  particular.** b-
14b5e 74 72 65 65 20 77 69 74 68 69 6e 20 61 20 64 61  tree within a da
14b5f 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
14b60 2a 2a 20 54 68 65 20 65 6e 74 72 79 20 69 73 20  ** The entry is 
14b61 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 69 74  identified by it
14b62 73 20 4d 65 6d 50 61 67 65 20 61 6e 64 20 74 68  s MemPage and th
14b63 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d 65  e index in.** Me
14b64 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66  mPage.aCell[] of
14b65 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a   the entry..**.*
14b66 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62  * A single datab
14b67 61 73 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61  ase file can sha
14b68 72 65 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20  red by two more 
14b69 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14b6a 69 6f 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72  ions,.** but cur
14b6b 73 6f 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73  sors cannot be s
14b6c 68 61 72 65 64 2e 20 20 45 61 63 68 20 63 75 72  hared.  Each cur
14b6d 73 6f 72 20 69 73 20 61 73 73 6f 63 69 61 74 65  sor is associate
14b6e 64 20 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74  d with a.** part
14b6f 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20  icular database 
14b70 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74  connection ident
14b71 69 66 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70  ified BtCursor.p
14b72 42 74 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20  Btree.db..**.** 
14b73 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73  Fields in this s
14b74 74 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63  tructure are acc
14b75 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 20  essed under the 
14b76 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a  BtShared.mutex.*
14b77 2a 20 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d  * found at self-
14b78 3e 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f  >pBt->mutex. .*/
14b79 0a 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72  .struct BtCursor
14b7a 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72   {.  Btree *pBtr
14b7b 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ee;            /
14b7c 2a 20 54 68 65 20 42 74 72 65 65 20 74 6f 20 77  * The Btree to w
14b7d 68 69 63 68 20 74 68 69 73 20 63 75 72 73 6f 72  hich this cursor
14b7e 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74   belongs */.  Bt
14b7f 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20  Shared *pBt;    
14b80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42          /* The B
14b81 74 53 68 61 72 65 64 20 74 68 69 73 20 63 75 72  tShared this cur
14b82 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f  sor points to */
14b83 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65  .  BtCursor *pNe
14b84 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20  xt, *pPrev;  /* 
14b85 46 6f 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c  Forms a linked l
14b86 69 73 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f  ist of all curso
14b87 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b  rs */.  struct K
14b88 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
14b89 3b 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61  ; /* Argument pa
14b8a 73 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73  ssed to comparis
14b8b 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  on function */. 
14b8c 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20   Pgno pgnoRoot; 
14b8d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
14b8e 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  e root page of t
14b8f 68 69 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71  his tree */.  sq
14b90 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68  lite3_int64 cach
14b91 65 64 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74  edRowid; /* Next
14b92 20 72 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30   rowid cache.  0
14b93 20 6d 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64   means not valid
14b94 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69   */.  CellInfo i
14b95 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  nfo;            
14b96 2f 2a 20 41 20 70 61 72 73 65 20 6f 66 20 74 68  /* A parse of th
14b97 65 20 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f  e cell we are po
14b98 69 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75  inting at */.  u
14b99 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20  8 wrFlag;       
14b9a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
14b9b 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a   if writable */.
14b9c 20 20 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20    u8 atLast;    
14b9d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
14b9e 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
14b9f 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
14ba0 20 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b   */.  u8 validNK
14ba1 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
14ba2 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e  /* True if info.
14ba3 6e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f  nKey is valid */
14ba4 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 20  .  u8 eState;   
14ba5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14ba6 4f 6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f  One of the CURSO
14ba7 52 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20  R_XXX constants 
14ba8 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20  (see below) */. 
14ba9 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20   void *pKey;    
14baa 20 20 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74    /* Saved key t
14bab 68 61 74 20 77 61 73 20 63 75 72 73 6f 72 27 73  hat was cursor's
14bac 20 6c 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69   last known posi
14bad 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b  tion */.  i64 nK
14bae 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ey;        /* Si
14baf 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c  ze of pKey, or l
14bb0 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20  ast integer key 
14bb1 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78  */.  int skipNex
14bb2 74 3b 20 20 20 20 2f 2a 20 50 72 65 76 28 29 20  t;    /* Prev() 
14bb3 69 73 20 6e 6f 6f 70 20 69 66 20 6e 65 67 61 74  is noop if negat
14bb4 69 76 65 2e 20 4e 65 78 74 28 29 20 69 73 20 6e  ive. Next() is n
14bb5 6f 6f 70 20 69 66 20 70 6f 73 69 74 69 76 65 20  oop if positive 
14bb6 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
14bb7 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
14bb8 20 20 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48    u8 isIncrblobH
14bb9 61 6e 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54  andle;      /* T
14bba 72 75 65 20 69 66 20 74 68 69 73 20 63 75 72 73  rue if this curs
14bbb 6f 72 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69  or is an incr. i
14bbc 6f 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67  o handle */.  Pg
14bbd 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20  no *aOverflow;  
14bbe 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65          /* Cache
14bbf 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   of overflow pag
14bc0 65 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23  e locations */.#
14bc1 65 6e 64 69 66 0a 20 20 69 31 36 20 69 50 61 67  endif.  i16 iPag
14bc2 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
14bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14bc4 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e   Index of curren
14bc5 74 20 70 61 67 65 20 69 6e 20 61 70 50 61 67 65  t page in apPage
14bc6 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61   */.  MemPage *a
14bc7 70 50 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d  pPage[BTCURSOR_M
14bc8 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50  AX_DEPTH];  /* P
14bc9 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74  ages from root t
14bca 6f 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a  o current page *
14bcb 2f 0a 20 20 75 31 36 20 61 69 49 64 78 5b 42 54  /.  u16 aiIdx[BT
14bcc 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48  CURSOR_MAX_DEPTH
14bcd 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ];        /* Cur
14bce 72 65 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70  rent index in ap
14bcf 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f  Page[i] */.};../
14bd0 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76  *.** Potential v
14bd1 61 6c 75 65 73 20 66 6f 72 20 42 74 43 75 72 73  alues for BtCurs
14bd2 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  or.eState..**.**
14bd3 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a   CURSOR_VALID:.*
14bd4 2a 20 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74  *   Cursor point
14bd5 73 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74  s to a valid ent
14bd6 72 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29  ry. getPayload()
14bd7 20 65 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c   etc. may be cal
14bd8 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f  led..**.** CURSO
14bd9 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20  R_INVALID:.**   
14bda 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20  Cursor does not 
14bdb 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64  point to a valid
14bdc 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e   entry. This can
14bdd 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61   happen (for exa
14bde 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61  mple) .**   beca
14bdf 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73  use the table is
14be0 20 65 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73   empty or becaus
14be1 65 20 42 74 72 65 65 43 75 72 73 6f 72 46 69 72  e BtreeCursorFir
14be2 73 74 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65  st() has not bee
14be3 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a  n.**   called..*
14be4 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55  *.** CURSOR_REQU
14be5 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68  IRESEEK:.**   Th
14be6 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 69  e table that thi
14be7 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65  s cursor was ope
14be8 6e 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69  ned on still exi
14be9 73 74 73 2c 20 62 75 74 20 68 61 73 20 62 65 65  sts, but has bee
14bea 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64  n .**   modified
14beb 20 73 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f   since the curso
14bec 72 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e  r was last used.
14bed 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69   The cursor posi
14bee 74 69 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a  tion is saved.**
14bef 20 20 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20     in variables 
14bf0 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e  BtCursor.pKey an
14bf1 64 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e  d BtCursor.nKey.
14bf2 20 57 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69   When a cursor i
14bf3 73 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20  s in .**   this 
14bf4 73 74 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75  state, restoreCu
14bf5 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63  rsorPosition() c
14bf6 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20  an be called to 
14bf7 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20  attempt to.**   
14bf8 73 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20  seek the cursor 
14bf9 74 6f 20 74 68 65 20 73 61 76 65 64 20 70 6f 73  to the saved pos
14bfa 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52  ition..**.** CUR
14bfb 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20  SOR_FAULT:.**   
14bfc 41 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20  A unrecoverable 
14bfd 65 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72  error (an I/O er
14bfe 72 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20  ror or a malloc 
14bff 66 61 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63  failure) has occ
14c00 75 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20  urred.**   on a 
14c01 64 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63  different connec
14c02 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73  tion that shares
14c03 20 74 68 65 20 42 74 53 68 61 72 65 64 20 63 61   the BtShared ca
14c04 63 68 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a  che with this.**
14c05 20 20 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20     cursor.  The 
14c06 65 72 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74  error has left t
14c07 68 65 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69  he cache in an i
14c08 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74  nconsistent stat
14c09 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69  e..**   Do nothi
14c0a 6e 67 20 65 6c 73 65 20 77 69 74 68 20 74 68 69  ng else with thi
14c0b 73 20 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61  s cursor.  Any a
14c0c 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68  ttempt to use th
14c0d 65 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68  e cursor.**   sh
14c0e 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20  ould return the 
14c0f 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65  error code store
14c10 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b  d in BtCursor.sk
14c11 69 70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55  ip.*/.#define CU
14c12 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20  RSOR_INVALID    
14c13 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65         0.#define
14c14 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20   CURSOR_VALID   
14c15 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
14c16 69 6e 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49  ine CURSOR_REQUI
14c17 52 45 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23  RESEEK       2.#
14c18 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41  define CURSOR_FA
14c19 55 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ULT             
14c1a 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61  3../* .** The da
14c1b 74 61 62 61 73 65 20 70 61 67 65 20 74 68 65 20  tabase page the 
14c1c 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 63  PENDING_BYTE occ
14c1d 75 70 69 65 73 2e 20 54 68 69 73 20 70 61 67 65  upies. This page
14c1e 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 0a   is never used..
14c1f 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44  */.# define PEND
14c20 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
14c21 74 29 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  t) PAGER_MJ_PGNO
14c22 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  (pBt)../*.** The
14c23 73 65 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65  se macros define
14c24 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66   the location of
14c25 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
14c26 20 65 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a   entry for a .**
14c27 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20   database page. 
14c28 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
14c29 6e 74 20 74 6f 20 65 61 63 68 20 69 73 20 74 68  nt to each is th
14c2a 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62  e number of usab
14c2b 6c 65 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65  le.** bytes on e
14c2c 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20  ach page of the 
14c2d 64 61 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20  database (often 
14c2e 31 30 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e  1024). The secon
14c2f 64 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65  d is the.** page
14c30 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20   number to look 
14c31 75 70 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65  up in the pointe
14c32 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52  r map..**.** PTR
14c33 4d 41 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72  MAP_PAGENO retur
14c34 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ns the database 
14c35 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
14c36 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a  he pointer-map.*
14c37 2a 20 70 61 67 65 20 74 68 61 74 20 73 74 6f 72  * page that stor
14c38 65 73 20 74 68 65 20 72 65 71 75 69 72 65 64 20  es the required 
14c39 70 6f 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f  pointer. PTRMAP_
14c3a 50 54 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e  PTROFFSET return
14c3b 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20  s.** the offset 
14c3c 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  of the requested
14c3d 20 6d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a   map entry..**.*
14c3e 2a 20 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72  * If the pgno ar
14c3f 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f  gument passed to
14c40 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69   PTRMAP_PAGENO i
14c41 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  s a pointer-map 
14c42 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67  page,.** then pg
14c43 6e 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  no is returned. 
14c44 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50  So (pgno==PTRMAP
14c45 5f 50 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67  _PAGENO(pgsz, pg
14c46 6e 6f 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75  no)) can be.** u
14c47 73 65 64 20 74 6f 20 74 65 73 74 20 69 66 20 70  sed to test if p
14c48 67 6e 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72  gno is a pointer
14c49 2d 6d 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41  -map page. PTRMA
14c4a 50 5f 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65  P_ISPAGE impleme
14c4b 6e 74 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74  nts.** this test
14c4c 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52  ..*/.#define PTR
14c4d 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20  MAP_PAGENO(pBt, 
14c4e 70 67 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65  pgno) ptrmapPage
14c4f 6e 6f 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64  no(pBt, pgno).#d
14c50 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52  efine PTRMAP_PTR
14c51 4f 46 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c  OFFSET(pgptrmap,
14c52 20 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d   pgno) (5*(pgno-
14c53 70 67 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65  pgptrmap-1)).#de
14c54 66 69 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41  fine PTRMAP_ISPA
14c55 47 45 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50  GE(pBt, pgno) (P
14c56 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42  TRMAP_PAGENO((pB
14c57 74 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e  t),(pgno))==(pgn
14c58 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  o))../*.** The p
14c59 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20  ointer map is a 
14c5a 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61  lookup table tha
14c5b 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  t identifies the
14c5c 20 70 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72   parent page for
14c5d 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70  .** each child p
14c5e 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
14c5f 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70  ase file.  The p
14c60 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 74 68  arent page is th
14c61 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63  e page that.** c
14c62 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
14c63 72 20 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20  r to the child. 
14c64 20 45 76 65 72 79 20 70 61 67 65 20 69 6e 20 74   Every page in t
14c65 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
14c66 61 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70  ains.** 0 or 1 p
14c67 61 72 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49  arent pages.  (I
14c68 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27  n this context '
14c69 64 61 74 61 62 61 73 65 20 70 61 67 65 27 20 72  database page' r
14c6a 65 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20  efers.** to any 
14c6b 70 61 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74  page that is not
14c6c 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69   part of the poi
14c6d 6e 74 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e  nter map itself.
14c6e 29 20 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20  )  Each pointer 
14c6f 6d 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e  map.** entry con
14c70 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c  sists of a singl
14c71 65 20 62 79 74 65 20 27 74 79 70 65 27 20 61 6e  e byte 'type' an
14c72 64 20 61 20 34 20 62 79 74 65 20 70 61 72 65 6e  d a 4 byte paren
14c73 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a  t page number..*
14c74 2a 20 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58  * The PTRMAP_XXX
14c75 20 69 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c   identifiers bel
14c76 6f 77 20 61 72 65 20 74 68 65 20 76 61 6c 69 64  ow are the valid
14c77 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   types..**.** Th
14c78 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65  e purpose of the
14c79 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20   pointer map is 
14c7a 74 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69  to facility movi
14c7b 6e 67 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e  ng pages from on
14c7c 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e  e.** position in
14c7d 20 74 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f   the file to ano
14c7e 74 68 65 72 20 61 73 20 70 61 72 74 20 6f 66 20  ther as part of 
14c7f 61 75 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65  autovacuum.  Whe
14c80 6e 20 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d  n a page.** is m
14c81 6f 76 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65  oved, the pointe
14c82 72 20 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20  r in its parent 
14c83 6d 75 73 74 20 62 65 20 75 70 64 61 74 65 64 20  must be updated 
14c84 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a  to point to the.
14c85 2a 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e  ** new location.
14c86 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61    The pointer ma
14c87 70 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63  p is used to loc
14c88 61 74 65 20 74 68 65 20 70 61 72 65 6e 74 20 70  ate the parent p
14c89 61 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a  age quickly..**.
14c8a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  ** PTRMAP_ROOTPA
14c8b 47 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65  GE: The database
14c8c 20 70 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d   page is a root-
14c8d 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e  page. The page-n
14c8e 75 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20  umber is not.** 
14c8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14c90 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61   used in this ca
14c91 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  se..**.** PTRMAP
14c92 5f 46 52 45 45 50 41 47 45 3a 20 54 68 65 20 64  _FREEPAGE: The d
14c93 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20  atabase page is 
14c94 61 6e 20 75 6e 75 73 65 64 20 28 66 72 65 65 29  an unused (free)
14c95 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d   page. The page-
14c96 6e 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20  number .**      
14c97 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 6e              is n
14c98 6f 74 20 75 73 65 64 20 69 6e 20 74 68 69 73 20  ot used in this 
14c99 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d  case..**.** PTRM
14c9a 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68  AP_OVERFLOW1: Th
14c9b 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
14c9c 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67  is the first pag
14c9d 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a  e in a list of .
14c9e 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
14c9f 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61       overflow pa
14ca0 67 65 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75  ges. The page nu
14ca1 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20  mber identifies 
14ca2 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a  the page that.**
14ca3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14ca4 20 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20     contains the 
14ca5 63 65 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e  cell with a poin
14ca6 74 65 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72  ter to this over
14ca7 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  flow page..**.**
14ca8 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
14ca9 32 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  2: The database 
14caa 70 61 67 65 20 69 73 20 74 68 65 20 73 65 63 6f  page is the seco
14cab 6e 64 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65  nd or later page
14cac 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a   in a list of.**
14cad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14cae 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65     overflow page
14caf 73 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62  s. The page-numb
14cb0 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68  er identifies th
14cb1 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20  e previous.**   
14cb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14cb3 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72  page in the over
14cb4 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a  flow page list..
14cb5 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52  **.** PTRMAP_BTR
14cb6 45 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65  EE: The database
14cb7 20 70 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72   page is a non-r
14cb8 6f 6f 74 20 62 74 72 65 65 20 70 61 67 65 2e 20  oot btree page. 
14cb9 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a  The page number.
14cba 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
14cbb 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
14cbc 70 61 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74  parent page in t
14cbd 68 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65  he btree..*/.#de
14cbe 66 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54  fine PTRMAP_ROOT
14cbf 50 41 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50  PAGE 1.#define P
14cc0 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32  TRMAP_FREEPAGE 2
14cc1 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
14cc2 4f 56 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66  OVERFLOW1 3.#def
14cc3 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46  ine PTRMAP_OVERF
14cc4 4c 4f 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50  LOW2 4.#define P
14cc5 54 52 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f  TRMAP_BTREE 5../
14cc6 2a 20 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73  * A bunch of ass
14cc7 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
14cc8 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72   to check the tr
14cc9 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20  ansaction state 
14cca 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20  variables.** of 
14ccb 68 61 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42  handle p (type B
14ccc 74 72 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72  tree*) are inter
14ccd 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74  nally consistent
14cce 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72  ..*/.#define btr
14ccf 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c  eeIntegrity(p) \
14cd0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42  .  assert( p->pB
14cd1 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
14cd2 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20  !=TRANS_NONE || 
14cd3 70 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63  p->pBt->nTransac
14cd4 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61  tion==0 ); \.  a
14cd5 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69  ssert( p->pBt->i
14cd6 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d  nTransaction>=p-
14cd7 3e 69 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f  >inTrans ); .../
14cd8 2a 0a 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56  *.** The ISAUTOV
14cd9 41 43 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75  ACUUM macro is u
14cda 73 65 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e  sed within balan
14cdb 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20  ce_nonroot() to 
14cdc 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20  determine.** if 
14cdd 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70  the database sup
14cde 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75  ports auto-vacuu
14cdf 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73  m or not. Becaus
14ce0 65 20 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20  e it is used.** 
14ce1 77 69 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73  within an expres
14ce2 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20  sion that is an 
14ce3 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74  argument to anot
14ce4 68 65 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73  her macro .** (s
14ce5 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c  qliteMallocRaw),
14ce6 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   it is not possi
14ce7 62 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69  ble to use condi
14ce8 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69  tional compilati
14ce9 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20  on..** So, this 
14cea 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64  macro is defined
14ceb 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66   instead..*/.#if
14cec 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14ced 5f 41 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66  _AUTOVACUUM.#def
14cee 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d  ine ISAUTOVACUUM
14cef 20 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75   (pBt->autoVacuu
14cf0 6d 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  m).#else.#define
14cf1 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a   ISAUTOVACUUM 0.
14cf2 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54  #endif.../*.** T
14cf3 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
14cf4 20 70 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74   passed around t
14cf5 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73  hrough all the s
14cf6 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72  anity checking r
14cf7 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72  outines.** in or
14cf8 64 65 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63  der to keep trac
14cf9 6b 20 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c  k of some global
14cfa 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69   state informati
14cfb 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  on..*/.typedef s
14cfc 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43  truct IntegrityC
14cfd 6b 20 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73  k IntegrityCk;.s
14cfe 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43  truct IntegrityC
14cff 6b 20 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  k {.  BtShared *
14d00 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74  pBt;    /* The t
14d01 72 65 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65  ree being checke
14d02 64 20 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72  d out */.  Pager
14d03 20 2a 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20   *pPager;    /* 
14d04 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 70  The associated p
14d05 61 67 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65  ager.  Also acce
14d06 73 73 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70  ssible by pBt->p
14d07 50 61 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20  Pager */.  Pgno 
14d08 6e 50 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20  nPage;       /* 
14d09 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
14d0a 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
14d0b 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b  */.  int *anRef;
14d0c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
14d0d 20 6f 66 20 74 69 6d 65 73 20 65 61 63 68 20 70   of times each p
14d0e 61 67 65 20 69 73 20 72 65 66 65 72 65 6e 63 65  age is reference
14d0f 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72  d */.  int mxErr
14d10 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70  ;        /* Stop
14d11 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72   accumulating er
14d12 72 6f 72 73 20 77 68 65 6e 20 74 68 69 73 20 72  rors when this r
14d13 65 61 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20  eaches zero */. 
14d14 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20   int nErr;      
14d15 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
14d16 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e  messages written
14d17 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66   to zErrMsg so f
14d18 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c  ar */.  int mall
14d19 6f 63 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d  ocFailed; /* A m
14d1a 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
14d1b 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
14d1c 72 65 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75  red */.  StrAccu
14d1d 6d 20 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63  m errMsg;  /* Ac
14d1e 63 75 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72  cumulate the err
14d1f 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20  or message text 
14d20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  here */.};../*.*
14d21 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20  * Read or write 
14d22 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d  a two- and four-
14d23 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
14d24 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a  integer values..
14d25 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62  */.#define get2b
14d26 79 74 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d  yte(x)   ((x)[0]
14d27 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64  <<8 | (x)[1]).#d
14d28 65 66 69 6e 65 20 70 75 74 32 62 79 74 65 28 70  efine put2byte(p
14d29 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75  ,v) ((p)[0] = (u
14d2a 38 29 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b  8)((v)>>8), (p)[
14d2b 31 5d 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64  1] = (u8)(v)).#d
14d2c 65 66 69 6e 65 20 67 65 74 34 62 79 74 65 20 73  efine get4byte s
14d2d 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 0a 23  qlite3Get4byte.#
14d2e 64 65 66 69 6e 65 20 70 75 74 34 62 79 74 65 20  define put4byte 
14d2f 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a  sqlite3Put4byte.
14d30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
14d31 20 45 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74   End of btreeInt
14d32 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
14d33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
14d35 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
14d36 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
14d37 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
14d38 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
14d39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
14d3a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14d3b 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
14d3c 45 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  E.#if SQLITE_THR
14d3d 45 41 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f  EADSAFE../*.** O
14d3e 62 74 61 69 6e 20 74 68 65 20 42 74 53 68 61 72  btain the BtShar
14d3f 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61  ed mutex associa
14d40 74 65 64 20 77 69 74 68 20 42 2d 54 72 65 65 20  ted with B-Tree 
14d41 68 61 6e 64 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a  handle p. Also,.
14d42 2a 2a 20 73 65 74 20 42 74 53 68 61 72 65 64 2e  ** set BtShared.
14d43 64 62 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  db to the databa
14d44 73 65 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69  se handle associ
14d45 61 74 65 64 20 77 69 74 68 20 70 20 61 6e 64 20  ated with p and 
14d46 74 68 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64  the.** p->locked
14d47 20 62 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 75 65   boolean to true
14d48 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14d49 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28   lockBtreeMutex(
14d4a 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
14d4b 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d  ert( p->locked==
14d4c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
14d4d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
14d4e 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74  held(p->pBt->mut
14d4f 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
14d50 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14d51 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
14d52 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  ) );..  sqlite3_
14d53 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70  mutex_enter(p->p
14d54 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d  Bt->mutex);.  p-
14d55 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62  >pBt->db = p->db
14d56 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20  ;.  p->locked = 
14d57 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  1;.}../*.** Rele
14d58 61 73 65 20 74 68 65 20 42 74 53 68 61 72 65 64  ase the BtShared
14d59 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65   mutex associate
14d5a 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 68 61  d with B-Tree ha
14d5b 6e 64 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 63 6c  ndle p and.** cl
14d5c 65 61 72 20 74 68 65 20 70 2d 3e 6c 6f 63 6b 65  ear the p->locke
14d5d 64 20 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74  d boolean..*/.st
14d5e 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b  atic void unlock
14d5f 42 74 72 65 65 4d 75 74 65 78 28 42 74 72 65 65  BtreeMutex(Btree
14d60 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
14d61 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a  p->locked==1 );.
14d62 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14d63 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
14d64 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
14d65 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
14d66 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
14d67 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
14d68 73 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d 70 2d  ssert( p->db==p-
14d69 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 20 73  >pBt->db );..  s
14d6a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
14d6b 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ve(p->pBt->mutex
14d6c 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d  );.  p->locked =
14d6d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74   0;.}../*.** Ent
14d6e 65 72 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68  er a mutex on th
14d6f 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62  e given BTree ob
14d70 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ject..**.** If t
14d71 68 65 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74  he object is not
14d72 20 73 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20   sharable, then 
14d73 6e 6f 20 6d 75 74 65 78 20 69 73 20 65 76 65 72  no mutex is ever
14d74 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64   required.** and
14d75 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
14d76 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75   a no-op.  The u
14d77 6e 64 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20  nderlying mutex 
14d78 69 73 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65  is non-recursive
14d79 2e 0a 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70  ..** But we keep
14d7a 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75   a reference cou
14d7b 6e 74 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74  nt in Btree.want
14d7c 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65  ToLock so the be
14d7d 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69  havior.** of thi
14d7e 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72  s interface is r
14d7f 65 63 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  ecursive..**.** 
14d80 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63  To avoid deadloc
14d81 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72  ks, multiple Btr
14d82 65 65 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69  ees are locked i
14d83 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72  n the same order
14d84 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62  .** by all datab
14d85 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
14d86 20 20 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69    The p->pNext i
14d87 73 20 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65  s a list of othe
14d88 72 0a 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f  r.** Btrees belo
14d89 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d  nging to the sam
14d8a 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14d8b 63 74 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42  ction as the p B
14d8c 74 72 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65  tree.** which ne
14d8d 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20  ed to be locked 
14d8e 61 66 74 65 72 20 70 2e 20 20 49 66 20 77 65 20  after p.  If we 
14d8f 63 61 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63  cannot get a loc
14d90 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20  k on.** p, then 
14d91 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c  first unlock all
14d92 20 6f 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f   of the others o
14d93 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e  n p->pNext, then
14d94 20 77 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65   wait.** for the
14d95 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20   lock to become 
14d96 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20  available on p, 
14d97 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20  then relock all 
14d98 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71  of the.** subseq
14d99 75 65 6e 74 20 42 74 72 65 65 73 20 74 68 61 74  uent Btrees that
14d9a 20 64 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a   desire a lock..
14d9b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14d9c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14d9d 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a  reeEnter(Btree *
14d9e 70 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61  p){.  Btree *pLa
14d9f 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20  ter;..  /* Some 
14da0 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 65  basic sanity che
14da1 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72  cking on the Btr
14da2 65 65 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66  ee.  The list of
14da3 20 42 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e   Btrees.  ** con
14da4 6e 65 63 74 65 64 20 62 79 20 70 4e 65 78 74 20  nected by pNext 
14da5 61 6e 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64  and pPrev should
14da6 20 62 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72   be in sorted or
14da7 64 65 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65  der by.  ** Btre
14da8 65 2e 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c  e.pBt value. All
14da9 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
14daa 20 6c 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c   list should bel
14dab 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20  ong to.  ** the 
14dac 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  same connection.
14dad 20 4f 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72   Only shared Btr
14dae 65 65 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c  ees are on the l
14daf 69 73 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ist. */.  assert
14db0 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c  ( p->pNext==0 ||
14db1 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70   p->pNext->pBt>p
14db2 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72  ->pBt );.  asser
14db3 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c  t( p->pPrev==0 |
14db4 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c  | p->pPrev->pBt<
14db5 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65  p->pBt );.  asse
14db6 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20  rt( p->pNext==0 
14db7 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d  || p->pNext->db=
14db8 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65  =p->db );.  asse
14db9 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20  rt( p->pPrev==0 
14dba 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d  || p->pPrev->db=
14dbb 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65  =p->db );.  asse
14dbc 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  rt( p->sharable 
14dbd 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20  || (p->pNext==0 
14dbe 26 26 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20  && p->pPrev==0) 
14dbf 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66  );..  /* Check f
14dc0 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69  or locking consi
14dc1 73 74 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65  stency */.  asse
14dc2 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c  rt( !p->locked |
14dc3 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e  | p->wantToLock>
14dc4 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
14dc5 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d  ->sharable || p-
14dc6 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29  >wantToLock==0 )
14dc7 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c  ;..  /* We shoul
14dc8 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61  d already hold a
14dc9 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
14dca 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
14dcb 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
14dcc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14dcd 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
14dce 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74  ;..  /* Unless t
14dcf 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 73  he database is s
14dd0 68 61 72 61 62 6c 65 20 61 6e 64 20 75 6e 6c 6f  harable and unlo
14dd1 63 6b 65 64 2c 20 74 68 65 6e 20 42 74 53 68 61  cked, then BtSha
14dd2 72 65 64 2e 64 62 0a 20 20 2a 2a 20 73 68 6f 75  red.db.  ** shou
14dd3 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20 73 65  ld already be se
14dd4 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a  t correctly. */.
14dd5 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 6c 6f    assert( (p->lo
14dd6 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68  cked==0 && p->sh
14dd7 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42  arable) || p->pB
14dd8 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a  t->db==p->db );.
14dd9 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61  .  if( !p->shara
14dda 62 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ble ) return;.  
14ddb 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b  p->wantToLock++;
14ddc 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64  .  if( p->locked
14ddd 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a   ) return;..  /*
14dde 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20   In most cases, 
14ddf 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c  we should be abl
14de0 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65  e to acquire the
14de1 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61   lock we.  ** wa
14de2 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e  nt without havin
14de3 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74  g to go throught
14de4 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c   the ascending l
14de5 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75  ock.  ** procedu
14de6 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e  re that follows.
14de7 20 20 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e    Just be sure n
14de8 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a  ot to block..  *
14de9 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  /.  if( sqlite3_
14dea 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74  mutex_try(p->pBt
14deb 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45  ->mutex)==SQLITE
14dec 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42  _OK ){.    p->pB
14ded 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  t->db = p->db;. 
14dee 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31     p->locked = 1
14def 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
14df0 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64  }..  /* To avoid
14df1 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74   deadlock, first
14df2 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63   release all loc
14df3 6b 73 20 77 69 74 68 20 61 20 6c 61 72 67 65 72  ks with a larger
14df4 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61  .  ** BtShared a
14df5 64 64 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63  ddress.  Then ac
14df6 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20  quire our lock. 
14df7 20 54 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a   Then reacquire.
14df8 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42    ** the other B
14df9 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68  tShared locks th
14dfa 61 74 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f  at we used to ho
14dfb 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a  ld in ascending.
14dfc 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f    ** order..  */
14dfd 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d  .  for(pLater=p-
14dfe 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20  >pNext; pLater; 
14dff 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70  pLater=pLater->p
14e00 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72  Next){.    asser
14e01 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61  t( pLater->shara
14e02 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ble );.    asser
14e03 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74  t( pLater->pNext
14e04 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70  ==0 || pLater->p
14e05 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72  Next->pBt>pLater
14e06 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73  ->pBt );.    ass
14e07 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f  ert( !pLater->lo
14e08 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e  cked || pLater->
14e09 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a  wantToLock>0 );.
14e0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e      if( pLater->
14e0b 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20  locked ){.      
14e0c 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78  unlockBtreeMutex
14e0d 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a  (pLater);.    }.
14e0e 20 20 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 65 4d    }.  lockBtreeM
14e0f 75 74 65 78 28 70 29 3b 0a 20 20 66 6f 72 28 70  utex(p);.  for(p
14e10 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20  Later=p->pNext; 
14e11 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70  pLater; pLater=p
14e12 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  Later->pNext){. 
14e13 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 77     if( pLater->w
14e14 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20  antToLock ){.   
14e15 20 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65     lockBtreeMute
14e16 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d  x(pLater);.    }
14e17 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78  .  }.}../*.** Ex
14e18 69 74 20 74 68 65 20 72 65 63 75 72 73 69 76 65  it the recursive
14e19 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65   mutex on a Btre
14e1a 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
14e1b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14e1c 33 42 74 72 65 65 4c 65 61 76 65 28 42 74 72 65  3BtreeLeave(Btre
14e1d 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  e *p){.  if( p->
14e1e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
14e1f 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54  assert( p->wantT
14e20 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70  oLock>0 );.    p
14e21 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a  ->wantToLock--;.
14e22 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54      if( p->wantT
14e23 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20  oLock==0 ){.    
14e24 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74    unlockBtreeMut
14e25 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ex(p);.    }.  }
14e26 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  .}..#ifndef NDEB
14e27 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  UG./*.** Return 
14e28 74 72 75 65 20 69 66 20 74 68 65 20 42 74 53 68  true if the BtSh
14e29 61 72 65 64 20 6d 75 74 65 78 20 69 73 20 68 65  ared mutex is he
14e2a 6c 64 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c  ld on the btree,
14e2b 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 42 2d   or if the.** B-
14e2c 54 72 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b  Tree is not mark
14e2d 65 64 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0a  ed as sharable..
14e2e 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
14e2f 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  ne is used only 
14e30 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 73 73 65  from within asse
14e31 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
14e32 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14e33 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
14e34 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74  reeHoldsMutex(Bt
14e35 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
14e36 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d  t( p->sharable==
14e37 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d  0 || p->locked==
14e38 30 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  0 || p->wantToLo
14e39 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ck>0 );.  assert
14e3a 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ( p->sharable==0
14e3b 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30   || p->locked==0
14e3c 20 7c 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42   || p->db==p->pB
14e3d 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72  t->db );.  asser
14e3e 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d  t( p->sharable==
14e3f 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d  0 || p->locked==
14e40 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
14e41 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e  ex_held(p->pBt->
14e42 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
14e43 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d  rt( p->sharable=
14e44 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d  =0 || p->locked=
14e45 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
14e46 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
14e47 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72 65 74  mutex) );..  ret
14e48 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65  urn (p->sharable
14e49 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64  ==0 || p->locked
14e4a 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  );.}.#endif...#i
14e4b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
14e4c 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a  T_INCRBLOB./*.**
14e4d 20 45 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65   Enter and leave
14e4e 20 61 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74   a mutex on a Bt
14e4f 72 65 65 20 67 69 76 65 6e 20 61 20 63 75 72 73  ree given a curs
14e50 6f 72 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74  or owned by that
14e51 0a 2a 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73  .** Btree.  Thes
14e52 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61  e entry points a
14e53 72 65 20 75 73 65 64 20 62 79 20 69 6e 63 72 65  re used by incre
14e54 6d 65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63  mental I/O and c
14e55 61 6e 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64  an be.** omitted
14e56 20 69 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20   if that module 
14e57 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a  is not used..*/.
14e58 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
14e59 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
14e5a 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75  EnterCursor(BtCu
14e5b 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73  rsor *pCur){.  s
14e5c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
14e5d 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a  (pCur->pBtree);.
14e5e 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
14e5f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
14e60 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74  eeLeaveCursor(Bt
14e61 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
14e62 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
14e63 76 65 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29  ve(pCur->pBtree)
14e64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
14e65 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
14e66 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e  OB */.../*.** En
14e67 74 65 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e  ter the mutex on
14e68 20 65 76 65 72 79 20 42 74 72 65 65 20 61 73 73   every Btree ass
14e69 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64  ociated with a d
14e6a 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65  atabase.** conne
14e6b 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20  ction.  This is 
14e6c 6e 65 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d  needed (for exam
14e6d 70 6c 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61  ple) prior to pa
14e6e 72 73 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65  rsing.** a state
14e6f 6d 65 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69  ment since we wi
14e70 6c 6c 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20  ll be comparing 
14e71 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
14e72 20 6e 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73   names.** agains
14e73 74 20 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e  t all schemas an
14e74 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74  d we do not want
14e75 20 74 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62   those schemas b
14e76 65 69 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75  eing.** reset ou
14e77 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e  t from under us.
14e78 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
14e79 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  a corresponding 
14e7a 6c 65 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64  leave-all proced
14e7b 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65  ures..**.** Ente
14e7c 72 20 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e  r the mutexes in
14e7d 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   accending order
14e7e 20 62 79 20 42 74 53 68 61 72 65 64 20 70 6f 69   by BtShared poi
14e7f 6e 74 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20  nter address.** 
14e80 74 6f 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73  to avoid the pos
14e81 73 69 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64  sibility of dead
14e82 6c 6f 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68  lock when two th
14e83 72 65 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77  reads with.** tw
14e84 6f 20 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73  o or more btrees
14e85 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20   in common both 
14e86 74 72 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20  try to lock all 
14e87 74 68 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20  their btrees.** 
14e88 61 74 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74  at the same inst
14e89 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ant..*/.SQLITE_P
14e8a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14e8b 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
14e8c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
14e8d 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20   int i;.  Btree 
14e8e 2a 70 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61  *p, *pLater;.  a
14e8f 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
14e90 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
14e91 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d  tex) );.  for(i=
14e92 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
14e93 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e  +){.    p = db->
14e94 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
14e95 61 73 73 65 72 74 28 20 21 70 20 7c 7c 20 28 70  assert( !p || (p
14e96 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70  ->locked==0 && p
14e97 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70  ->sharable) || p
14e98 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62  ->pBt->db==p->db
14e99 20 29 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26   );.    if( p &&
14e9a 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a   p->sharable ){.
14e9b 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c        p->wantToL
14e9c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28  ock++;.      if(
14e9d 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20   !p->locked ){. 
14e9e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
14e9f 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20  ->wantToLock==1 
14ea0 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  );.        while
14ea1 28 20 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d  ( p->pPrev ) p =
14ea2 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20   p->pPrev;.     
14ea3 20 20 20 2f 2a 20 52 65 61 73 6f 6e 20 66 6f 72     /* Reason for
14ea4 20 41 4c 57 41 59 53 3a 20 20 54 68 65 72 65 20   ALWAYS:  There 
14ea5 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
14ea6 20 6f 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 74 72   on unlocked Btr
14ea7 65 65 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a  ee in.        **
14ea8 20 74 68 65 20 63 68 61 69 6e 2e 20 20 4f 74 68   the chain.  Oth
14ea9 65 72 77 69 73 65 20 74 68 65 20 21 70 2d 3e 6c  erwise the !p->l
14eaa 6f 63 6b 65 64 20 74 65 73 74 20 61 62 6f 76 65  ocked test above
14eab 20 77 6f 75 6c 64 20 68 61 76 65 20 66 61 69 6c   would have fail
14eac 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68  ed */.        wh
14ead 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 26  ile( p->locked &
14eae 26 20 41 4c 57 41 59 53 28 70 2d 3e 70 4e 65 78  & ALWAYS(p->pNex
14eaf 74 29 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78  t) ) p = p->pNex
14eb0 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70  t;.        for(p
14eb1 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74  Later = p->pNext
14eb2 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72  ; pLater; pLater
14eb3 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b  =pLater->pNext){
14eb4 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
14eb5 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b  Later->locked ){
14eb6 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c  .            unl
14eb7 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c  ockBtreeMutex(pL
14eb8 61 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  ater);.         
14eb9 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
14eba 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b       while( p ){
14ebb 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42  .          lockB
14ebc 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
14ebd 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70          p = p->p
14ebe 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Next;.        }.
14ebf 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
14ec0 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  }.}.SQLITE_PRIVA
14ec1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
14ec2 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c  treeLeaveAll(sql
14ec3 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
14ec4 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a   i;.  Btree *p;.
14ec5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14ec6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
14ec7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
14ec8 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
14ec9 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64   i++){.    p = d
14eca 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
14ecb 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73     if( p && p->s
14ecc 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  harable ){.     
14ecd 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74   assert( p->want
14ece 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20  ToLock>0 );.    
14ecf 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d    p->wantToLock-
14ed0 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  -;.      if( p->
14ed1 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b  wantToLock==0 ){
14ed2 0a 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42  .        unlockB
14ed3 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
14ed4 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
14ed5 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
14ed6 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  G./*.** Return t
14ed7 72 75 65 20 69 66 20 74 68 65 20 63 75 72 72 65  rue if the curre
14ed8 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20  nt thread holds 
14ed9 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
14eda 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78  nection.** mutex
14edb 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65   and all require
14edc 64 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  d BtShared mutex
14edd 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  es..**.** This r
14ede 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69  outine is used i
14edf 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
14ee0 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a  tatements only..
14ee1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14ee2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
14ee3 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
14ee4 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
14ee5 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21    int i;.  if( !
14ee6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14ee7 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b  ld(db->mutex) ){
14ee8 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
14ee9 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
14eea 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
14eeb 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20     Btree *p;.   
14eec 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   p = db->aDb[i].
14eed 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26  pBt;.    if( p &
14eee 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26  & p->sharable &&
14eef 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61  .         (p->wa
14ef0 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21  ntToLock==0 || !
14ef1 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14ef2 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ld(p->pBt->mutex
14ef3 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  )) ){.      retu
14ef4 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 0;.    }.  }.
14ef5 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
14ef6 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
14ef7 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e  /../*.** Add a n
14ef8 65 77 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72  ew Btree pointer
14ef9 20 74 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78   to a BtreeMutex
14efa 41 72 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68  Array. .** if th
14efb 65 20 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f  e pointer can po
14efc 73 73 69 62 6c 79 20 62 65 20 73 68 61 72 65 64  ssibly be shared
14efd 20 77 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72   with.** another
14efe 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14eff 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
14f00 70 6f 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70  pointers are kep
14f01 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  t in sorted orde
14f02 72 20 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74  r by pBtree->pBt
14f03 2e 20 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77  .  That.** way w
14f04 68 65 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74  hen we go to ent
14f05 65 72 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78  er all the mutex
14f06 65 73 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72  es, we can enter
14f07 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65   them.** in orde
14f08 72 20 77 69 74 68 6f 75 74 20 65 76 65 72 79 20  r without every 
14f09 68 61 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70  having to backup
14f0a 20 61 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77   and retry and w
14f0b 69 74 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69  ithout.** worryi
14f0c 6e 67 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63  ng about deadloc
14f0d 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d  k..**.** The num
14f0e 62 65 72 20 6f 66 20 73 68 61 72 65 64 20 62 74  ber of shared bt
14f0f 72 65 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73  rees will always
14f10 20 62 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c   be small (usual
14f11 6c 79 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f  ly 0 or 1).** so
14f12 20 61 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f   an insertion so
14f13 72 74 20 69 73 20 61 6e 20 61 64 65 71 75 61 74  rt is an adequat
14f14 65 20 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65  e algorithm here
14f15 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14f16 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14f17 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49  BtreeMutexArrayI
14f18 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78  nsert(BtreeMutex
14f19 41 72 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42  Array *pArray, B
14f1a 74 72 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20  tree *pBtree){. 
14f1b 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53   int i, j;.  BtS
14f1c 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66  hared *pBt;.  if
14f1d 28 20 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70  ( pBtree==0 || p
14f1e 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d  Btree->sharable=
14f1f 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66  =0 ) return;.#if
14f20 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a  ndef NDEBUG.  {.
14f21 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
14f22 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69  Array->nMutex; i
14f23 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
14f24 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  t( pArray->aBtre
14f25 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a  e[i]!=pBtree );.
14f26 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
14f27 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61  .  assert( pArra
14f28 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a  y->nMutex>=0 );.
14f29 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79    assert( pArray
14f2a 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69  ->nMutex<ArraySi
14f2b 7a 65 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65  ze(pArray->aBtre
14f2c 65 29 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20  e)-1 );.  pBt = 
14f2d 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66  pBtree->pBt;.  f
14f2e 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79  or(i=0; i<pArray
14f2f 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a  ->nMutex; i++){.
14f30 20 20 20 20 61 73 73 65 72 74 28 20 70 41 72 72      assert( pArr
14f31 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70  ay->aBtree[i]!=p
14f32 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28  Btree );.    if(
14f33 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14f34 69 5d 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20  i]->pBt>pBt ){. 
14f35 20 20 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61       for(j=pArra
14f36 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20  y->nMutex; j>i; 
14f37 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41  j--){.        pA
14f38 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20  rray->aBtree[j] 
14f39 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65  = pArray->aBtree
14f3a 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  [j-1];.      }. 
14f3b 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74       pArray->aBt
14f3c 72 65 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b  ree[i] = pBtree;
14f3d 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e  .      pArray->n
14f3e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72  Mutex++;.      r
14f3f 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
14f40 0a 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  .  pArray->aBtre
14f41 65 5b 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78  e[pArray->nMutex
14f42 2b 2b 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a  ++] = pBtree;.}.
14f43 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65  ./*.** Enter the
14f44 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72 79 20   mutex of every 
14f45 62 74 72 65 65 20 69 6e 20 74 68 65 20 61 72 72  btree in the arr
14f46 61 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ay.  This routin
14f47 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61  e is.** called a
14f48 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
14f49 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  of sqlite3VdbeEx
14f4a 65 63 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78  ec().  The mutex
14f4b 65 73 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64  es are.** exited
14f4c 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
14f4d 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  he same function
14f4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14f4f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14f50 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45  BtreeMutexArrayE
14f51 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 41  nter(BtreeMutexA
14f52 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20  rray *pArray){. 
14f53 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
14f54 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75  0; i<pArray->nMu
14f55 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42  tex; i++){.    B
14f56 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79  tree *p = pArray
14f57 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20  ->aBtree[i];.   
14f58 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73   /* Some basic s
14f59 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
14f5a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d  /.    assert( i=
14f5b 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42  =0 || pArray->aB
14f5c 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70  tree[i-1]->pBt<p
14f5d 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73  ->pBt );.    ass
14f5e 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20  ert( !p->locked 
14f5f 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  || p->wantToLock
14f60 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65  >0 );..    /* We
14f61 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20   should already 
14f62 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  hold a lock on t
14f63 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
14f64 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73  ection */.    as
14f65 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14f66 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
14f67 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f  mutex) );..    /
14f68 2a 20 54 68 65 20 42 74 72 65 65 20 69 73 20 73  * The Btree is s
14f69 68 61 72 61 62 6c 65 20 62 65 63 61 75 73 65 20  harable because 
14f6a 6f 6e 6c 79 20 73 68 61 72 61 62 6c 65 20 42 74  only sharable Bt
14f6b 72 65 65 73 20 61 72 65 20 65 6e 74 65 72 65 64  rees are entered
14f6c 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65  .    ** into the
14f6d 20 61 72 72 61 79 20 69 6e 20 74 68 65 20 66 69   array in the fi
14f6e 72 73 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20  rst place. */.  
14f6f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61    assert( p->sha
14f70 72 61 62 6c 65 20 29 3b 0a 0a 20 20 20 20 70 2d  rable );..    p-
14f71 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20  >wantToLock++;. 
14f72 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65     if( !p->locke
14f73 64 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42  d ){.      lockB
14f74 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
14f75 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
14f76 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74 65 78   Leave the mutex
14f77 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 65 20   of every btree 
14f78 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f  in the group..*/
14f79 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14f7a 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14f7b 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
14f7c 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  (BtreeMutexArray
14f7d 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74   *pArray){.  int
14f7e 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
14f7f 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b  <pArray->nMutex;
14f80 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65   i++){.    Btree
14f81 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42   *p = pArray->aB
14f82 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20  tree[i];.    /* 
14f83 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74  Some basic sanit
14f84 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20  y checking */.  
14f85 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c    assert( i==0 |
14f86 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65  | pArray->aBtree
14f87 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42  [i-1]->pBt<p->pB
14f88 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
14f89 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20   p->locked );.  
14f8a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e    assert( p->wan
14f8b 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20  tToLock>0 );..  
14f8c 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61    /* We should a
14f8d 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f  lready hold a lo
14f8e 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
14f8f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
14f90 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
14f91 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14f92 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
14f93 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c  ..    p->wantToL
14f94 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  ock--;.    if( p
14f95 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
14f96 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42  ){.      unlockB
14f97 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
14f98 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65    }.  }.}..#else
14f99 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14f9a 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14f9b 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29  eEnter(Btree *p)
14f9c 7b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d  {.  p->pBt->db =
14f9d 20 70 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45   p->db;.}.SQLITE
14f9e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14f9f 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
14fa0 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ll(sqlite3 *db){
14fa1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
14fa2 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
14fa3 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20  i++){.    Btree 
14fa4 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  *p = db->aDb[i].
14fa5 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29  pBt;.    if( p )
14fa6 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e  {.      p->pBt->
14fa7 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20  db = p->db;.    
14fa8 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  }.  }.}.#endif /
14fa9 2a 20 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  * if SQLITE_THRE
14faa 41 44 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66  ADSAFE */.#endif
14fab 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54   /* ifndef SQLIT
14fac 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
14fad 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  CHE */../*******
14fae 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62  ******* End of b
14faf 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
14fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fb2 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
14fb3 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
14fb4 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a  le btree.c *****
14fb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fb7 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
14fb8 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a  04 April 6.**.**
14fb9 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
14fba 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
14fbb 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
14fbc 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
14fbd 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
14fbe 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
14fbf 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
14fc0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
14fc1 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
14fc2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
14fc3 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
14fc4 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
14fc5 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
14fc6 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
14fc7 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
14fc8 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
14fc9 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
14fca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fce 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
14fcf 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
14fd0 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69  s a external (di
14fd1 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61  sk-based) databa
14fd2 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e  se using BTrees.
14fd3 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61 64  .** See the head
14fd4 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62  er comment on "b
14fd5 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61  treeInt.h" for a
14fd6 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
14fd7 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64  ation..** Includ
14fd8 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f  ing a descriptio
14fd9 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74  n of file format
14fda 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77   and an overview
14fdb 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a   of operation..*
14fdc 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61  /../*.** The hea
14fdd 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20  der string that 
14fde 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62  appears at the b
14fdf 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72  eginning of ever
14fe0 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61  y.** SQLite data
14fe1 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
14fe2 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69  const char zMagi
14fe3 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49  cHeader[] = SQLI
14fe4 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a  TE_FILE_HEADER;.
14fe5 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20  ./*.** Set this 
14fe6 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
14fe7 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74  to 1 to enable t
14fe8 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65  racing using the
14fe9 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e   TRACE.** macro.
14fea 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71  .*/.#if 0.int sq
14feb 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d  lite3BtreeTrace=
14fec 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65  1;  /* True to e
14fed 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f  nable tracing */
14fee 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28  .# define TRACE(
14fef 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74  X)  if(sqlite3Bt
14ff0 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66  reeTrace){printf
14ff1 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74   X;fflush(stdout
14ff2 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  );}.#else.# defi
14ff3 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64  ne TRACE(X).#end
14ff4 69 66 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  if....#ifndef SQ
14ff5 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
14ff6 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c  _CACHE./*.** A l
14ff7 69 73 74 20 6f 66 20 42 74 53 68 61 72 65 64 20  ist of BtShared 
14ff8 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65  objects that are
14ff9 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61   eligible for pa
14ffa 72 74 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69  rticipation.** i
14ffb 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 2e 20  n shared cache. 
14ffc 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 68   This variable h
14ffd 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75  as file scope du
14ffe 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c  ring normal buil
14fff 64 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74  ds,.** but the t
15000 65 73 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64  est harness need
15001 73 20 74 6f 20 61 63 63 65 73 73 20 69 74 20 73  s to access it s
15002 6f 20 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f  o we make it glo
15003 62 61 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74  bal for .** test
15004 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41   builds..**.** A
15005 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 76 61  ccess to this va
15006 72 69 61 62 6c 65 20 69 73 20 70 72 6f 74 65 63  riable is protec
15007 74 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55  ted by SQLITE_MU
15008 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
15009 52 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  R..*/.#ifdef SQL
1500a 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
1500b 50 52 49 56 41 54 45 20 42 74 53 68 61 72 65 64  PRIVATE BtShared
1500c 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c   *SQLITE_WSD sql
1500d 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
1500e 69 73 74 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73  ist = 0;.#else.s
1500f 74 61 74 69 63 20 42 74 53 68 61 72 65 64 20 2a  tatic BtShared *
15010 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74  SQLITE_WSD sqlit
15011 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73  e3SharedCacheLis
15012 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65  t = 0;.#endif.#e
15013 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
15014 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
15015 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
15016 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
15017 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62  CACHE./*.** Enab
15018 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68  le or disable th
15019 65 20 73 68 61 72 65 64 20 70 61 67 65 72 20 61  e shared pager a
1501a 6e 64 20 73 63 68 65 6d 61 20 66 65 61 74 75 72  nd schema featur
1501b 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  es..**.** This r
1501c 6f 75 74 69 6e 65 20 68 61 73 20 6e 6f 20 65 66  outine has no ef
1501d 66 65 63 74 20 6f 6e 20 65 78 69 73 74 69 6e 67  fect on existing
1501e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1501f 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68  tions..** The sh
15020 61 72 65 64 20 63 61 63 68 65 20 73 65 74 74 69  ared cache setti
15021 6e 67 20 65 66 66 65 63 74 73 20 6f 6e 6c 79 20  ng effects only 
15022 66 75 74 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a  future calls to.
15023 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ** sqlite3_open(
15024 29 2c 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  ), sqlite3_open1
15025 36 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f  6(), or sqlite3_
15026 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 53 51  open_v2()..*/.SQ
15027 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
15028 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
15029 65 64 5f 63 61 63 68 65 28 69 6e 74 20 65 6e 61  ed_cache(int ena
1502a 62 6c 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 47  ble){.  sqlite3G
1502b 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72  lobalConfig.shar
1502c 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 3d  edCacheEnabled =
1502d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 65 74 75 72   enable;.  retur
1502e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
1502f 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 64 65 66 20  endif....#ifdef 
15030 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15031 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20  ED_CACHE.  /*.  
15032 2a 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 73  ** The functions
15033 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68   querySharedCach
15034 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 73 65  eTableLock(), se
15035 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  tSharedCacheTabl
15036 65 4c 6f 63 6b 28 29 2c 0a 20 20 2a 2a 20 61 6e  eLock(),.  ** an
15037 64 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64  d clearAllShared
15038 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28  CacheTableLocks(
15039 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74  ).  ** manipulat
1503a 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  e entries in the
1503b 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20   BtShared.pLock 
1503c 6c 69 6e 6b 65 64 20 6c 69 73 74 20 75 73 65 64  linked list used
1503d 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73   to store.  ** s
1503e 68 61 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c  hared-cache tabl
1503f 65 20 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49  e level locks. I
15040 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73  f the library is
15041 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
15042 68 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63  he.  ** shared-c
15043 61 63 68 65 20 66 65 61 74 75 72 65 20 64 69 73  ache feature dis
15044 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72  abled, then ther
15045 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f  e is only ever o
15046 6e 65 20 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20  ne user.  ** of 
15047 65 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74  each BtShared st
15048 72 75 63 74 75 72 65 20 61 6e 64 20 73 6f 20 74  ructure and so t
15049 68 69 73 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e  his locking is n
1504a 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 20  ot necessary. . 
1504b 20 2a 2a 20 53 6f 20 64 65 66 69 6e 65 20 74 68   ** So define th
1504c 65 20 6c 6f 63 6b 20 72 65 6c 61 74 65 64 20 66  e lock related f
1504d 75 6e 63 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f  unctions as no-o
1504e 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69  ps..  */.  #defi
1504f 6e 65 20 71 75 65 72 79 53 68 61 72 65 64 43 61  ne querySharedCa
15050 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62  cheTableLock(a,b
15051 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20  ,c) SQLITE_OK.  
15052 23 64 65 66 69 6e 65 20 73 65 74 53 68 61 72 65  #define setShare
15053 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
15054 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b  a,b,c) SQLITE_OK
15055 0a 20 20 23 64 65 66 69 6e 65 20 63 6c 65 61 72  .  #define clear
15056 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61  AllSharedCacheTa
15057 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64  bleLocks(a).  #d
15058 65 66 69 6e 65 20 64 6f 77 6e 67 72 61 64 65 41  efine downgradeA
15059 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62  llSharedCacheTab
1505a 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 65  leLocks(a).  #de
1505b 66 69 6e 65 20 68 61 73 53 68 61 72 65 64 43 61  fine hasSharedCa
1505c 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62  cheTableLock(a,b
1505d 2c 63 2c 64 29 20 31 0a 20 20 23 64 65 66 69 6e  ,c,d) 1.  #defin
1505e 65 20 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63  e hasReadConflic
1505f 74 73 28 61 2c 20 62 29 20 30 0a 23 65 6e 64 69  ts(a, b) 0.#endi
15060 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
15061 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
15062 43 48 45 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  CHE..#ifdef SQLI
15063 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a  TE_DEBUG./*.****
15064 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
15065 73 20 6f 6e 6c 79 20 75 73 65 64 20 61 73 20 70  s only used as p
15066 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74  art of an assert
15067 28 29 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2a  () statement. **
15068 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  *.**.** Check to
15069 20 73 65 65 20 69 66 20 70 42 74 72 65 65 20 68   see if pBtree h
1506a 6f 6c 64 73 20 74 68 65 20 72 65 71 75 69 72 65  olds the require
1506b 64 20 6c 6f 63 6b 73 20 74 6f 20 72 65 61 64 20  d locks to read 
1506c 6f 72 20 77 72 69 74 65 20 74 6f 20 74 68 65 20  or write to the 
1506d 0a 2a 2a 20 74 61 62 6c 65 20 77 69 74 68 20 72  .** table with r
1506e 6f 6f 74 20 70 61 67 65 20 69 52 6f 6f 74 2e 20  oot page iRoot. 
1506f 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 69 74    Return 1 if it
15070 20 64 6f 65 73 20 61 6e 64 20 30 20 69 66 20 6e   does and 0 if n
15071 6f 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  ot..**.** For ex
15072 61 6d 70 6c 65 2c 20 77 68 65 6e 20 77 72 69 74  ample, when writ
15073 69 6e 67 20 74 6f 20 61 20 74 61 62 6c 65 20 77  ing to a table w
15074 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 52  ith root-page iR
15075 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 42 74 72 65  oot via .** Btre
15076 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 74  e connection pBt
15077 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61 73  ree:.**.**    as
15078 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43  sert( hasSharedC
15079 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 42  acheTableLock(pB
1507a 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30 2c 20  tree, iRoot, 0, 
1507b 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b 0a 2a  WRITE_LOCK) );.*
1507c 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74 69 6e  *.** When writin
1507d 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 74 68  g to an index th
1507e 61 74 20 72 65 73 69 64 65 73 20 69 6e 20 61 20  at resides in a 
1507f 73 68 61 72 61 62 6c 65 20 64 61 74 61 62 61 73  sharable databas
15080 65 2c 20 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65  e, the .** calle
15081 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20 66 69  r should have fi
15082 72 73 74 20 6f 62 74 61 69 6e 65 64 20 61 20 6c  rst obtained a l
15083 6f 63 6b 20 73 70 65 63 69 66 79 69 6e 67 20 74  ock specifying t
15084 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a  he root page of.
15085 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ** the correspon
15086 64 69 6e 67 20 74 61 62 6c 65 2e 20 54 68 69 73  ding table. This
15087 20 6d 61 6b 65 73 20 74 68 69 6e 67 73 20 61 20   makes things a 
15088 62 69 74 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63  bit more complic
15089 61 74 65 64 2c 0a 2a 2a 20 61 73 20 74 68 69 73  ated,.** as this
1508a 20 6d 6f 64 75 6c 65 20 74 72 65 61 74 73 20 65   module treats e
1508b 61 63 68 20 74 61 62 6c 65 20 61 73 20 61 20 73  ach table as a s
1508c 65 70 61 72 61 74 65 20 73 74 72 75 63 74 75 72  eparate structur
1508d 65 2e 20 54 6f 20 64 65 74 65 72 6d 69 6e 65 0a  e. To determine.
1508e 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 63 6f 72  ** the table cor
1508f 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
15090 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 77 72  e index being wr
15091 69 74 74 65 6e 2c 20 74 68 69 73 0a 2a 2a 20 66  itten, this.** f
15092 75 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73  unction has to s
15093 65 61 72 63 68 20 74 68 72 6f 75 67 68 20 74 68  earch through th
15094 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
15095 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64  a..**.** Instead
15096 20 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68   of a lock on th
15097 65 20 74 61 62 6c 65 2f 69 6e 64 65 78 20 72 6f  e table/index ro
15098 6f 74 65 64 20 61 74 20 70 61 67 65 20 69 52 6f  oted at page iRo
15099 6f 74 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d  ot, the caller m
1509a 61 79 0a 2a 2a 20 68 6f 6c 64 20 61 20 77 72 69  ay.** hold a wri
1509b 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73  te-lock on the s
1509c 63 68 65 6d 61 20 74 61 62 6c 65 20 28 72 6f 6f  chema table (roo
1509d 74 20 70 61 67 65 20 31 29 2e 20 54 68 69 73 20  t page 1). This 
1509e 69 73 20 61 6c 73 6f 0a 2a 2a 20 61 63 63 65 70  is also.** accep
1509f 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  table..*/.static
150a0 20 69 6e 74 20 68 61 73 53 68 61 72 65 64 43 61   int hasSharedCa
150a1 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20  cheTableLock(.  
150a2 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20  Btree *pBtree,  
150a3 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65         /* Handle
150a4 20 74 68 61 74 20 6d 75 73 74 20 68 6f 6c 64 20   that must hold 
150a5 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  lock */.  Pgno i
150a6 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20  Root,           
150a7 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66   /* Root page of
150a8 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74   b-tree */.  int
150a9 20 69 73 49 6e 64 65 78 2c 20 20 20 20 20 20 20   isIndex,       
150aa 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
150ab 52 6f 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74  Root is the root
150ac 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 62 2d 74   of an index b-t
150ad 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4c 6f  ree */.  int eLo
150ae 63 6b 54 79 70 65 20 20 20 20 20 20 20 20 20 20  ckType          
150af 2f 2a 20 52 65 71 75 69 72 65 64 20 6c 6f 63 6b  /* Required lock
150b0 20 74 79 70 65 20 28 52 45 41 44 5f 4c 4f 43 4b   type (READ_LOCK
150b1 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20   or WRITE_LOCK) 
150b2 2a 2f 0a 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a  */.){.  Schema *
150b3 70 53 63 68 65 6d 61 20 3d 20 28 53 63 68 65 6d  pSchema = (Schem
150b4 61 20 2a 29 70 42 74 72 65 65 2d 3e 70 42 74 2d  a *)pBtree->pBt-
150b5 3e 70 53 63 68 65 6d 61 3b 0a 20 20 50 67 6e 6f  >pSchema;.  Pgno
150b6 20 69 54 61 62 20 3d 20 30 3b 0a 20 20 42 74 4c   iTab = 0;.  BtL
150b7 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 0a 20 20 2f  ock *pLock;..  /
150b8 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61  * If this databa
150b9 73 65 20 69 73 20 6e 6f 74 20 73 68 61 72 65 61  se is not sharea
150ba 62 6c 65 2c 20 6f 72 20 69 66 20 74 68 65 20 63  ble, or if the c
150bb 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67  lient is reading
150bc 0a 20 20 2a 2a 20 61 6e 64 20 68 61 73 20 74 68  .  ** and has th
150bd 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74  e read-uncommitt
150be 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65  ed flag set, the
150bf 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 65 71  n no lock is req
150c0 75 69 72 65 64 2e 20 0a 20 20 2a 2a 20 52 65 74  uired. .  ** Ret
150c1 75 72 6e 20 74 72 75 65 20 69 6d 6d 65 64 69 61  urn true immedia
150c2 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tely..  */.  if(
150c3 20 28 70 42 74 72 65 65 2d 3e 73 68 61 72 61 62   (pBtree->sharab
150c4 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c 20 28 65 4c  le==0).   || (eL
150c5 6f 63 6b 54 79 70 65 3d 3d 52 45 41 44 5f 4c 4f  ockType==READ_LO
150c6 43 4b 20 26 26 20 28 70 42 74 72 65 65 2d 3e 64  CK && (pBtree->d
150c7 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
150c8 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  E_ReadUncommitte
150c9 64 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  d)).  ){.    ret
150ca 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 1;.  }..  /*
150cb 20 49 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69   If the client i
150cc 73 20 72 65 61 64 69 6e 67 20 20 6f 72 20 77 72  s reading  or wr
150cd 69 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 61  iting an index a
150ce 6e 64 20 74 68 65 20 73 63 68 65 6d 61 20 69 73  nd the schema is
150cf 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f 61 64 65 64  .  ** not loaded
150d0 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 6f 6f  , then it is too
150d1 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20 61 63   difficult to ac
150d2 74 75 61 6c 6c 79 20 63 68 65 63 6b 20 74 6f 20  tually check to 
150d3 73 65 65 20 69 66 0a 20 20 2a 2a 20 74 68 65 20  see if.  ** the 
150d4 63 6f 72 72 65 63 74 20 6c 6f 63 6b 73 20 61 72  correct locks ar
150d5 65 20 68 65 6c 64 2e 20 20 53 6f 20 64 6f 20 6e  e held.  So do n
150d6 6f 74 20 62 6f 74 68 65 72 20 2d 20 6a 75 73 74  ot bother - just
150d7 20 72 65 74 75 72 6e 20 74 72 75 65 2e 0a 20 20   return true..  
150d8 2a 2a 20 54 68 69 73 20 63 61 73 65 20 64 6f 65  ** This case doe
150d9 73 20 6e 6f 74 20 63 6f 6d 65 20 75 70 20 76 65  s not come up ve
150da 72 79 20 6f 66 74 65 6e 20 61 6e 79 68 6f 77 2e  ry often anyhow.
150db 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 6e  .  */.  if( isIn
150dc 64 65 78 20 26 26 20 28 21 70 53 63 68 65 6d 61  dex && (!pSchema
150dd 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d 3e 66 6c   || (pSchema->fl
150de 61 67 73 26 44 42 5f 53 63 68 65 6d 61 4c 6f 61  ags&DB_SchemaLoa
150df 64 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20  ded)==0) ){.    
150e0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20  return 1;.  }.. 
150e1 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   /* Figure out t
150e2 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 74 68 61  he root-page tha
150e3 74 20 74 68 65 20 6c 6f 63 6b 20 73 68 6f 75 6c  t the lock shoul
150e4 64 20 62 65 20 68 65 6c 64 20 6f 6e 2e 20 46 6f  d be held on. Fo
150e5 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 62 2d 74  r table.  ** b-t
150e6 72 65 65 73 2c 20 74 68 69 73 20 69 73 20 6a 75  rees, this is ju
150e7 73 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  st the root page
150e8 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 62   of the b-tree b
150e9 65 69 6e 67 20 72 65 61 64 20 6f 72 0a 20 20 2a  eing read or.  *
150ea 2a 20 77 72 69 74 74 65 6e 2e 20 46 6f 72 20 69  * written. For i
150eb 6e 64 65 78 20 62 2d 74 72 65 65 73 2c 20 69 74  ndex b-trees, it
150ec 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67   is the root pag
150ed 65 20 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61  e of the associa
150ee 74 65 64 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20  ted.  ** table. 
150ef 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 6e 64 65   */.  if( isInde
150f0 78 20 29 7b 0a 20 20 20 20 48 61 73 68 45 6c 65  x ){.    HashEle
150f1 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d  m *p;.    for(p=
150f2 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
150f3 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  &pSchema->idxHas
150f4 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48  h); p; p=sqliteH
150f5 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20  ashNext(p)){.   
150f6 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d     Index *pIdx =
150f7 20 28 49 6e 64 65 78 20 2a 29 73 71 6c 69 74 65   (Index *)sqlite
150f8 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20  HashData(p);.   
150f9 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75     if( pIdx->tnu
150fa 6d 3d 3d 28 69 6e 74 29 69 52 6f 6f 74 20 29 7b  m==(int)iRoot ){
150fb 0a 20 20 20 20 20 20 20 20 69 54 61 62 20 3d 20  .        iTab = 
150fc 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 74 6e  pIdx->pTable->tn
150fd 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  um;.      }.    
150fe 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  }.  }else{.    i
150ff 54 61 62 20 3d 20 69 52 6f 6f 74 3b 0a 20 20 7d  Tab = iRoot;.  }
15100 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f  ..  /* Search fo
15101 72 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c  r the required l
15102 6f 63 6b 2e 20 45 69 74 68 65 72 20 61 20 77 72  ock. Either a wr
15103 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 72 6f 6f 74  ite-lock on root
15104 2d 70 61 67 65 20 69 54 61 62 2c 20 61 20 0a 20  -page iTab, a . 
15105 20 2a 2a 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f   ** write-lock o
15106 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62  n the schema tab
15107 6c 65 2c 20 6f 72 20 28 69 66 20 74 68 65 20 63  le, or (if the c
15108 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67  lient is reading
15109 29 20 61 0a 20 20 2a 2a 20 72 65 61 64 2d 6c 6f  ) a.  ** read-lo
1510a 63 6b 20 6f 6e 20 69 54 61 62 20 77 69 6c 6c 20  ck on iTab will 
1510b 73 75 66 66 69 63 65 2e 20 52 65 74 75 72 6e 20  suffice. Return 
1510c 31 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 73  1 if any of thes
1510d 65 20 61 72 65 20 66 6f 75 6e 64 2e 20 20 2a 2f  e are found.  */
1510e 0a 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74  .  for(pLock=pBt
1510f 72 65 65 2d 3e 70 42 74 2d 3e 70 4c 6f 63 6b 3b  ree->pBt->pLock;
15110 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c   pLock; pLock=pL
15111 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ock->pNext){.   
15112 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72   if( pLock->pBtr
15113 65 65 3d 3d 70 42 74 72 65 65 20 0a 20 20 20 20  ee==pBtree .    
15114 20 26 26 20 28 70 4c 6f 63 6b 2d 3e 69 54 61 62   && (pLock->iTab
15115 6c 65 3d 3d 69 54 61 62 20 7c 7c 20 28 70 4c 6f  le==iTab || (pLo
15116 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45  ck->eLock==WRITE
15117 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e  _LOCK && pLock->
15118 69 54 61 62 6c 65 3d 3d 31 29 29 0a 20 20 20 20  iTable==1)).    
15119 20 26 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b   && pLock->eLock
1511a 3e 3d 65 4c 6f 63 6b 54 79 70 65 20 0a 20 20 20  >=eLockType .   
1511b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1511c 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   1;.    }.  }.. 
1511d 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 66 69   /* Failed to fi
1511e 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20  nd the required 
1511f 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 72 65 74 75 72  lock. */.  retur
15120 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  n 0;.}.#endif /*
15121 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f   SQLITE_DEBUG */
15122 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
15123 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68  DEBUG./*.**** Th
15124 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20  is function may 
15125 62 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20  be used as part 
15126 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 74  of assert() stat
15127 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2a 2a  ements only. ***
15128 2a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  *.**.** Return t
15129 72 75 65 20 69 66 20 69 74 20 77 6f 75 6c 64 20  rue if it would 
1512a 62 65 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70  be illegal for p
1512b 42 74 72 65 65 20 74 6f 20 77 72 69 74 65 20 69  Btree to write i
1512c 6e 74 6f 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65  nto the.** table
1512d 20 6f 72 20 69 6e 64 65 78 20 72 6f 6f 74 65 64   or index rooted
1512e 20 61 74 20 69 52 6f 6f 74 20 62 65 63 61 75 73   at iRoot becaus
1512f 65 20 6f 74 68 65 72 20 73 68 61 72 65 64 20 63  e other shared c
15130 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a  onnections are.*
15131 2a 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79  * simultaneously
15132 20 72 65 61 64 69 6e 67 20 74 68 61 74 20 73 61   reading that sa
15133 6d 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  me table or inde
15134 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69  x..**.** It is i
15135 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42 74 72 65  llegal for pBtre
15136 65 20 74 6f 20 77 72 69 74 65 20 69 66 20 73 6f  e to write if so
15137 6d 65 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f  me other Btree o
15138 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20 73 68  bject that.** sh
15139 61 72 65 73 20 74 68 65 20 73 61 6d 65 20 42 74  ares the same Bt
1513a 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 69 73  Shared object is
1513b 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69   currently readi
1513c 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a  ng or writing.**
1513d 20 74 68 65 20 69 52 6f 6f 74 20 74 61 62 6c 65   the iRoot table
1513e 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 74 68  .  Except, if th
1513f 65 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62  e other Btree ob
15140 6a 65 63 74 20 68 61 73 20 74 68 65 0a 2a 2a 20  ject has the.** 
15141 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64  read-uncommitted
15142 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65 6e 20   flag set, then 
15143 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65  it is OK for the
15144 20 6f 74 68 65 72 20 6f 62 6a 65 63 74 20 74 6f   other object to
15145 0a 2a 2a 20 68 61 76 65 20 61 20 72 65 61 64 20  .** have a read 
15146 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 46 6f  cursor..**.** Fo
15147 72 20 65 78 61 6d 70 6c 65 2c 20 62 65 66 6f 72  r example, befor
15148 65 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79  e writing to any
15149 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 61 62   part of the tab
1514a 6c 65 20 6f 72 20 69 6e 64 65 78 0a 2a 2a 20 72  le or index.** r
1514b 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 69 52  ooted at page iR
1514c 6f 6f 74 2c 20 6f 6e 65 20 73 68 6f 75 6c 64 20  oot, one should 
1514d 63 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61  call:.**.**    a
1514e 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43  ssert( !hasReadC
1514f 6f 6e 66 6c 69 63 74 73 28 70 42 74 72 65 65 2c  onflicts(pBtree,
15150 20 69 52 6f 6f 74 29 20 29 3b 0a 2a 2f 0a 73 74   iRoot) );.*/.st
15151 61 74 69 63 20 69 6e 74 20 68 61 73 52 65 61 64  atic int hasRead
15152 43 6f 6e 66 6c 69 63 74 73 28 42 74 72 65 65 20  Conflicts(Btree 
15153 2a 70 42 74 72 65 65 2c 20 50 67 6e 6f 20 69 52  *pBtree, Pgno iR
15154 6f 6f 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72  oot){.  BtCursor
15155 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74   *p;.  for(p=pBt
15156 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f  ree->pBt->pCurso
15157 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  r; p; p=p->pNext
15158 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67  ){.    if( p->pg
15159 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20 0a 20  noRoot==iRoot . 
1515a 20 20 20 20 26 26 20 70 2d 3e 70 42 74 72 65 65      && p->pBtree
1515b 21 3d 70 42 74 72 65 65 0a 20 20 20 20 20 26 26  !=pBtree.     &&
1515c 20 30 3d 3d 28 70 2d 3e 70 42 74 72 65 65 2d 3e   0==(p->pBtree->
1515d 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
1515e 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
1515f 65 64 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ed).    ){.     
15160 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
15161 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
15162 0a 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20  .}.#endif    /* 
15163 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
15164 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75  BUG */../*.** Qu
15165 65 72 79 20 74 6f 20 73 65 65 20 69 66 20 42 74  ery to see if Bt
15166 72 65 65 20 68 61 6e 64 6c 65 20 70 20 6d 61 79  ree handle p may
15167 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f   obtain a lock o
15168 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 0a 2a 2a  f type eLock .**
15169 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57   (READ_LOCK or W
1516a 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 74 68  RITE_LOCK) on th
1516b 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f  e table with roo
1516c 74 2d 70 61 67 65 20 69 54 61 62 2e 20 52 65 74  t-page iTab. Ret
1516d 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  urn.** SQLITE_OK
1516e 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 6d 61 79   if the lock may
1516f 20 62 65 20 6f 62 74 61 69 6e 65 64 20 28 62 79   be obtained (by
15170 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 65 74 53   calling.** setS
15171 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
15172 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51 4c 49 54  ock()), or SQLIT
15173 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e 6f 74 2e  E_LOCKED if not.
15174 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 71  .*/.static int q
15175 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54  uerySharedCacheT
15176 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a  ableLock(Btree *
15177 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c 20 75 38  p, Pgno iTab, u8
15178 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61   eLock){.  BtSha
15179 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1517a 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74  t;.  BtLock *pIt
1517b 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  er;..  assert( s
1517c 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1517d 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73  Mutex(p) );.  as
1517e 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41  sert( eLock==REA
1517f 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d  D_LOCK || eLock=
15180 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20  =WRITE_LOCK );. 
15181 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d   assert( p->db!=
15182 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  0 );.  assert( !
15183 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51  (p->db->flags&SQ
15184 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
15185 74 74 65 64 29 7c 7c 65 4c 6f 63 6b 3d 3d 57 52  tted)||eLock==WR
15186 49 54 45 5f 4c 4f 43 4b 7c 7c 69 54 61 62 3d 3d  ITE_LOCK||iTab==
15187 31 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20  1 );.  .  /* If 
15188 72 65 71 75 65 73 74 69 6e 67 20 61 20 77 72 69  requesting a wri
15189 74 65 2d 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68  te-lock, then th
1518a 65 20 42 74 72 65 65 20 6d 75 73 74 20 68 61 76  e Btree must hav
1518b 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 0a  e an open write.
1518c 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
1518d 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 41   on this file. A
1518e 6e 64 2c 20 6f 62 76 69 6f 75 73 6c 79 2c 20 66  nd, obviously, f
1518f 6f 72 20 74 68 69 73 20 74 6f 20 62 65 20 73 6f  or this to be so
15190 20 74 68 65 72 65 20 0a 20 20 2a 2a 20 6d 75 73   there .  ** mus
15191 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 77 72 69  t be an open wri
15192 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te transaction o
15193 6e 20 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c  n the file itsel
15194 66 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  f..  */.  assert
15195 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f  ( eLock==READ_LO
15196 43 4b 20 7c 7c 20 28 70 3d 3d 70 42 74 2d 3e 70  CK || (p==pBt->p
15197 57 72 69 74 65 72 20 26 26 20 70 2d 3e 69 6e 54  Writer && p->inT
15198 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
15199 45 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  E) );.  assert( 
1519a 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b  eLock==READ_LOCK
1519b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73   || pBt->inTrans
1519c 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
1519d 49 54 45 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54  ITE );.  .  /* T
1519e 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
1519f 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68   no-op if the sh
151a0 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f  ared-cache is no
151a1 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69  t enabled */.  i
151a2 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20  f( !p->sharable 
151a3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
151a4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
151a5 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72  /* If some other
151a6 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68   connection is h
151a7 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73  olding an exclus
151a8 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20  ive lock, the.  
151a9 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  ** requested loc
151aa 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74  k may not be obt
151ab 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  ained..  */.  if
151ac 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d  ( pBt->pWriter!=
151ad 70 20 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c  p && pBt->isExcl
151ae 75 73 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c  usive ){.    sql
151af 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c  ite3ConnectionBl
151b0 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74  ocked(p->db, pBt
151b1 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a  ->pWriter->db);.
151b2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
151b3 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
151b4 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72  ACHE;.  }..  for
151b5 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63  (pIter=pBt->pLoc
151b6 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d  k; pIter; pIter=
151b7 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  pIter->pNext){. 
151b8 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74     /* The condit
151b9 69 6f 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63  ion (pIter->eLoc
151ba 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65  k!=eLock) in the
151bb 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e   following if(..
151bc 2e 29 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65  .) .    ** state
151bd 6d 65 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69  ment is a simpli
151be 66 69 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20  fication of:.   
151bf 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c   **.    **   (eL
151c0 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20  ock==WRITE_LOCK 
151c1 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d  || pIter->eLock=
151c2 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20  =WRITE_LOCK).   
151c3 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65   **.    ** since
151c4 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66   we know that if
151c5 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f   eLock==WRITE_LO
151c6 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65  CK, then no othe
151c7 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20  r connection.   
151c8 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57   ** may hold a W
151c9 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79  RITE_LOCK on any
151ca 20 74 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66   table in this f
151cb 69 6c 65 20 28 73 69 6e 63 65 20 74 68 65 72 65  ile (since there
151cc 20 63 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79   can.    ** only
151cd 20 62 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69   be a single wri
151ce 74 65 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ter)..    */.   
151cf 20 61 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e   assert( pIter->
151d0 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b  eLock==READ_LOCK
151d1 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b   || pIter->eLock
151d2 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a  ==WRITE_LOCK );.
151d3 20 20 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63      assert( eLoc
151d4 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20  k==READ_LOCK || 
151d5 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70  pIter->pBtree==p
151d6 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b   || pIter->eLock
151d7 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20  ==READ_LOCK);.  
151d8 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74    if( pIter->pBt
151d9 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d  ree!=p && pIter-
151da 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26  >iTable==iTab &&
151db 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65   pIter->eLock!=e
151dc 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71  Lock ){.      sq
151dd 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
151de 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49  locked(p->db, pI
151df 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29  ter->pBtree->db)
151e0 3b 0a 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63  ;.      if( eLoc
151e1 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b  k==WRITE_LOCK ){
151e2 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
151e3 20 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72   p==pBt->pWriter
151e4 20 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d   );.        pBt-
151e5 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 31 3b 0a  >isPending = 1;.
151e6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
151e7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
151e8 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a  ED_SHAREDCACHE;.
151e9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
151ea 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
151eb 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54  #endif /* !SQLIT
151ec 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
151ed 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  CHE */..#ifndef 
151ee 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
151ef 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41  ED_CACHE./*.** A
151f0 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  dd a lock on the
151f1 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74   table with root
151f2 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20  -page iTable to 
151f3 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  the shared-btree
151f4 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65   used.** by Btre
151f5 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61  e handle p. Para
151f6 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74  meter eLock must
151f7 20 62 65 20 65 69 74 68 65 72 20 52 45 41 44 5f   be either READ_
151f8 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54  LOCK or .** WRIT
151f9 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68  E_LOCK..**.** Th
151fa 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
151fb 6d 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  mes the followin
151fc 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54  g:.**.**   (a) T
151fd 68 65 20 73 70 65 63 69 66 69 65 64 20 42 74 72  he specified Btr
151fe 65 65 20 6f 62 6a 65 63 74 20 70 20 69 73 20 63  ee object p is c
151ff 6f 6e 6e 65 63 74 65 64 20 74 6f 20 61 20 73 68  onnected to a sh
15200 61 72 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20  arable.**       
15201 64 61 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69  database (one wi
15202 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  th the BtShared.
15203 73 68 61 72 61 62 6c 65 20 66 6c 61 67 20 73 65  sharable flag se
15204 74 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20  t), and.**.**   
15205 28 62 29 20 4e 6f 20 6f 74 68 65 72 20 42 74 72  (b) No other Btr
15206 65 65 20 6f 62 6a 65 63 74 73 20 68 6f 6c 64 20  ee objects hold 
15207 61 20 6c 6f 63 6b 20 74 68 61 74 20 63 6f 6e 66  a lock that conf
15208 6c 69 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 77  licts.**       w
15209 69 74 68 20 74 68 65 20 72 65 71 75 65 73 74 65  ith the requeste
1520a 64 20 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 75 65  d lock (i.e. que
1520b 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
1520c 6c 65 4c 6f 63 6b 28 29 20 68 61 73 0a 2a 2a 20  leLock() has.** 
1520d 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 62 65        already be
1520e 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 72 65  en called and re
1520f 74 75 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b  turned SQLITE_OK
15210 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  )..**.** SQLITE_
15211 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  OK is returned i
15212 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 64  f the lock is ad
15213 64 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ded successfully
15214 2e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 0a  . SQLITE_NOMEM .
15215 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ** is returned i
15216 66 20 61 20 6d 61 6c 6c 6f 63 20 61 74 74 65 6d  f a malloc attem
15217 70 74 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  pt fails..*/.sta
15218 74 69 63 20 69 6e 74 20 73 65 74 53 68 61 72 65  tic int setShare
15219 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
1521a 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69  Btree *p, Pgno i
1521b 54 61 62 6c 65 2c 20 75 38 20 65 4c 6f 63 6b 29  Table, u8 eLock)
1521c 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
1521d 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74  t = p->pBt;.  Bt
1521e 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b  Lock *pLock = 0;
1521f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72  .  BtLock *pIter
15220 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
15221 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
15222 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
15223 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f  rt( eLock==READ_
15224 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57  LOCK || eLock==W
15225 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  RITE_LOCK );.  a
15226 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20  ssert( p->db!=0 
15227 29 3b 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e 6e 65  );..  /* A conne
15228 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72  ction with the r
15229 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20  ead-uncommitted 
1522a 66 6c 61 67 20 73 65 74 20 77 69 6c 6c 20 6e 65  flag set will ne
1522b 76 65 72 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20  ver try to.  ** 
1522c 6f 62 74 61 69 6e 20 61 20 72 65 61 64 2d 6c 6f  obtain a read-lo
1522d 63 6b 20 75 73 69 6e 67 20 74 68 69 73 20 66 75  ck using this fu
1522e 6e 63 74 69 6f 6e 2e 20 54 68 65 20 6f 6e 6c 79  nction. The only
1522f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 61 69   read-lock obtai
15230 6e 65 64 0a 20 20 2a 2a 20 62 79 20 61 20 63 6f  ned.  ** by a co
15231 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 72 65 61 64  nnection in read
15232 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 6f 64  -uncommitted mod
15233 65 20 69 73 20 6f 6e 20 74 68 65 20 73 71 6c 69  e is on the sqli
15234 74 65 5f 6d 61 73 74 65 72 20 0a 20 20 2a 2a 20  te_master .  ** 
15235 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 61 74 20  table, and that 
15236 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64  lock is obtained
15237 20 69 6e 20 42 74 72 65 65 42 65 67 69 6e 54 72   in BtreeBeginTr
15238 61 6e 73 28 29 2e 20 20 2a 2f 0a 20 20 61 73 73  ans().  */.  ass
15239 65 72 74 28 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e  ert( 0==(p->db->
1523a 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61  flags&SQLITE_Rea
1523b 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c  dUncommitted) ||
1523c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f   eLock==WRITE_LO
1523d 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  CK );..  /* This
1523e 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
1523f 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20   only be called 
15240 6f 6e 20 61 20 73 68 61 72 61 62 6c 65 20 62 2d  on a sharable b-
15241 74 72 65 65 20 61 66 74 65 72 20 69 74 20 0a 20  tree after it . 
15242 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 64 65 74   ** has been det
15243 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20  ermined that no 
15244 6f 74 68 65 72 20 62 2d 74 72 65 65 20 68 6f 6c  other b-tree hol
15245 64 73 20 61 20 63 6f 6e 66 6c 69 63 74 69 6e 67  ds a conflicting
15246 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73   lock.  */.  ass
15247 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65  ert( p->sharable
15248 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51   );.  assert( SQ
15249 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68  LITE_OK==querySh
1524a 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
1524b 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 4c  ck(p, iTable, eL
1524c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69  ock) );..  /* Fi
1524d 72 73 74 20 73 65 61 72 63 68 20 74 68 65 20 6c  rst search the l
1524e 69 73 74 20 66 6f 72 20 61 6e 20 65 78 69 73 74  ist for an exist
1524f 69 6e 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 69 73  ing lock on this
15250 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72   table. */.  for
15251 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63  (pIter=pBt->pLoc
15252 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d  k; pIter; pIter=
15253 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  pIter->pNext){. 
15254 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 69 54     if( pIter->iT
15255 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20  able==iTable && 
15256 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70  pIter->pBtree==p
15257 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20   ){.      pLock 
15258 3d 20 70 49 74 65 72 3b 0a 20 20 20 20 20 20 62  = pIter;.      b
15259 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1525a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 62 6f  .  /* If the abo
1525b 76 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f  ve search did no
1525c 74 20 66 69 6e 64 20 61 20 42 74 4c 6f 63 6b 20  t find a BtLock 
1525d 73 74 72 75 63 74 20 61 73 73 6f 63 69 61 74 69  struct associati
1525e 6e 67 20 42 74 72 65 65 20 70 0a 20 20 2a 2a 20  ng Btree p.  ** 
1525f 77 69 74 68 20 74 61 62 6c 65 20 69 54 61 62 6c  with table iTabl
15260 65 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20  e, allocate one 
15261 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f  and link it into
15262 20 74 68 65 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a   the list..  */.
15263 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a    if( !pLock ){.
15264 20 20 20 20 70 4c 6f 63 6b 20 3d 20 28 42 74 4c      pLock = (BtL
15265 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  ock *)sqlite3Mal
15266 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42  locZero(sizeof(B
15267 74 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69 66 28  tLock));.    if(
15268 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20   !pLock ){.     
15269 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1526a 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
1526b 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 20 3d 20  pLock->iTable = 
1526c 69 54 61 62 6c 65 3b 0a 20 20 20 20 70 4c 6f 63  iTable;.    pLoc
1526d 6b 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20  k->pBtree = p;. 
1526e 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20     pLock->pNext 
1526f 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20  = pBt->pLock;.  
15270 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70    pBt->pLock = p
15271 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Lock;.  }..  /* 
15272 53 65 74 20 74 68 65 20 42 74 4c 6f 63 6b 2e 65  Set the BtLock.e
15273 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 74 6f  Lock variable to
15274 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20   the maximum of 
15275 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b  the current lock
15276 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65  .  ** and the re
15277 71 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20 54 68  quested lock. Th
15278 69 73 20 6d 65 61 6e 73 20 69 66 20 61 20 77 72  is means if a wr
15279 69 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61 6c 72  ite-lock was alr
1527a 65 61 64 79 20 68 65 6c 64 0a 20 20 2a 2a 20 61  eady held.  ** a
1527b 6e 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 72  nd a read-lock r
1527c 65 71 75 65 73 74 65 64 2c 20 77 65 20 64 6f 6e  equested, we don
1527d 27 74 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 64  't incorrectly d
1527e 6f 77 6e 67 72 61 64 65 20 74 68 65 20 6c 6f 63  owngrade the loc
1527f 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  k..  */.  assert
15280 28 20 57 52 49 54 45 5f 4c 4f 43 4b 3e 52 45 41  ( WRITE_LOCK>REA
15281 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20  D_LOCK );.  if( 
15282 65 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f  eLock>pLock->eLo
15283 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d  ck ){.    pLock-
15284 3e 65 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b 3b 0a  >eLock = eLock;.
15285 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
15286 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
15287 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49  f /* !SQLITE_OMI
15288 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a  T_SHARED_CACHE *
15289 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1528a 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1528b 43 48 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  CHE./*.** Releas
1528c 65 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 20  e all the table 
1528d 6c 6f 63 6b 73 20 28 6c 6f 63 6b 73 20 6f 62 74  locks (locks obt
1528e 61 69 6e 65 64 20 76 69 61 20 63 61 6c 6c 73 20  ained via calls 
1528f 74 6f 0a 2a 2a 20 74 68 65 20 73 65 74 53 68 61  to.** the setSha
15290 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
15291 6b 28 29 20 70 72 6f 63 65 64 75 72 65 29 20 68  k() procedure) h
15292 65 6c 64 20 62 79 20 42 74 72 65 65 20 6f 62 6a  eld by Btree obj
15293 65 63 74 20 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ect p..**.** Thi
15294 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
15295 65 73 20 74 68 61 74 20 42 74 72 65 65 20 70 20  es that Btree p 
15296 68 61 73 20 61 6e 20 6f 70 65 6e 20 72 65 61 64  has an open read
15297 20 6f 72 20 77 72 69 74 65 20 0a 2a 2a 20 74 72   or write .** tr
15298 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74  ansaction. If it
15299 20 64 6f 65 73 20 6e 6f 74 2c 20 74 68 65 6e 20   does not, then 
1529a 74 68 65 20 42 74 53 68 61 72 65 64 2e 69 73 50  the BtShared.isP
1529b 65 6e 64 69 6e 67 20 76 61 72 69 61 62 6c 65 0a  ending variable.
1529c 2a 2a 20 6d 61 79 20 62 65 20 69 6e 63 6f 72 72  ** may be incorr
1529d 65 63 74 6c 79 20 63 6c 65 61 72 65 64 2e 0a 2a  ectly cleared..*
1529e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c  /.static void cl
1529f 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68  earAllSharedCach
152a0 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65  eTableLocks(Btre
152a1 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65  e *p){.  BtShare
152a2 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
152a3 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 74  .  BtLock **ppIt
152a4 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 6b  er = &pBt->pLock
152a5 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
152a6 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
152a7 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
152a8 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  rt( p->sharable 
152a9 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 3b  || 0==*ppIter );
152aa 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
152ab 54 72 61 6e 73 3e 30 20 29 3b 0a 0a 20 20 77 68  Trans>0 );..  wh
152ac 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29 7b 0a  ile( *ppIter ){.
152ad 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63      BtLock *pLoc
152ae 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20 20  k = *ppIter;.   
152af 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73   assert( pBt->is
152b0 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20  Exclusive==0 || 
152b1 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 4c  pBt->pWriter==pL
152b2 6f 63 6b 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20  ock->pBtree );. 
152b3 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
152b4 2d 3e 70 42 74 72 65 65 2d 3e 69 6e 54 72 61 6e  ->pBtree->inTran
152b5 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20  s>=pLock->eLock 
152b6 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b  );.    if( pLock
152b7 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20  ->pBtree==p ){. 
152b8 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d 20 70       *ppIter = p
152b9 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Lock->pNext;.   
152ba 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
152bb 2d 3e 69 54 61 62 6c 65 21 3d 31 20 7c 7c 20 70  ->iTable!=1 || p
152bc 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29  Lock==&p->lock )
152bd 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63  ;.      if( pLoc
152be 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 29 7b 0a  k->iTable!=1 ){.
152bf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
152c0 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20  free(pLock);.   
152c1 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
152c2 20 20 20 20 20 20 70 70 49 74 65 72 20 3d 20 26        ppIter = &
152c3 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20  pLock->pNext;.  
152c4 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72    }.  }..  asser
152c5 74 28 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e  t( pBt->isPendin
152c6 67 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72  g==0 || pBt->pWr
152c7 69 74 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42  iter );.  if( pB
152c8 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b  t->pWriter==p ){
152c9 0a 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65  .    pBt->pWrite
152ca 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e  r = 0;.    pBt->
152cb 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b  isExclusive = 0;
152cc 0a 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64  .    pBt->isPend
152cd 69 6e 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  ing = 0;.  }else
152ce 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73   if( pBt->nTrans
152cf 61 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20  action==2 ){.   
152d0 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f   /* This functio
152d1 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
152d2 20 42 74 72 65 65 20 70 20 69 73 20 63 6f 6e 63   Btree p is conc
152d3 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 20 20  luding its .    
152d4 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ** transaction. 
152d5 49 66 20 74 68 65 72 65 20 63 75 72 72 65 6e 74  If there current
152d6 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 69 74  ly exists a writ
152d7 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f 74  er, and p is not
152d8 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72 69  .    ** that wri
152d9 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 6e 75  ter, then the nu
152da 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65  mber of locks he
152db 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e  ld by connection
152dc 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74  s other.    ** t
152dd 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 20 6d  han the writer m
152de 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 6f 20  ust be about to 
152df 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49 6e  drop to zero. In
152e0 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20 2a   this case.    *
152e1 2a 20 73 65 74 20 74 68 65 20 69 73 50 65 6e 64  * set the isPend
152e2 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e 0a 20  ing flag to 0.. 
152e3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
152e4 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 75 72  there is not cur
152e5 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 72 2c  rently a writer,
152e6 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 2e 69   then BtShared.i
152e7 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a 20 20  sPending must.  
152e8 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61 6c 72    ** be zero alr
152e9 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20 6e 65  eady. So this ne
152ea 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72 6d 6c  xt line is harml
152eb 65 73 73 20 69 6e 20 74 68 61 74 20 63 61 73 65  ess in that case
152ec 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 74  ..    */.    pBt
152ed 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b  ->isPending = 0;
152ee 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
152ef 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e  is function chan
152f0 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c 6f  ges all write-lo
152f1 63 6b 73 20 68 65 6c 64 20 62 79 20 42 74 72 65  cks held by Btre
152f2 65 20 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c 6f  e p into read-lo
152f3 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  cks..*/.static v
152f4 6f 69 64 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c  oid downgradeAll
152f5 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
152f6 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b  Locks(Btree *p){
152f7 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
152f8 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 28   = p->pBt;.  if(
152f9 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70   pBt->pWriter==p
152fa 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a   ){.    BtLock *
152fb 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e  pLock;.    pBt->
152fc 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20  pWriter = 0;.   
152fd 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76   pBt->isExclusiv
152fe 65 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e  e = 0;.    pBt->
152ff 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20  isPending = 0;. 
15300 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74     for(pLock=pBt
15301 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20  ->pLock; pLock; 
15302 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65  pLock=pLock->pNe
15303 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  xt){.      asser
15304 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d  t( pLock->eLock=
15305 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c  =READ_LOCK || pL
15306 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29  ock->pBtree==p )
15307 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 65  ;.      pLock->e
15308 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b  Lock = READ_LOCK
15309 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23  ;.    }.  }.}..#
1530a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1530b 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1530c 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69  E */..static voi
1530d 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65  d releasePage(Me
1530e 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20  mPage *pPage);  
1530f 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  /* Forward refer
15310 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a 2a  ence */../*.****
15311 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
15312 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66  s used inside of
15313 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a   assert() only *
15314 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 79  ***.**.** Verify
15315 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
15316 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78   holds the mutex
15317 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 65 64   on its BtShared
15318 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
15319 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69  E_DEBUG.static i
1531a 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  nt cursorHoldsMu
1531b 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29  tex(BtCursor *p)
1531c 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1531d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
1531e 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a  >pBt->mutex);.}.
1531f 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66  #endif...#ifndef
15320 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
15321 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61  RBLOB./*.** Inva
15322 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66  lidate the overf
15323 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
15324 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70  che for cursor p
15325 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a  Cur, if any..*/.
15326 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61  static void inva
15327 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61  lidateOverflowCa
15328 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43  che(BtCursor *pC
15329 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  ur){.  assert( c
1532a 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1532b 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74  pCur) );.  sqlit
1532c 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f  e3_free(pCur->aO
1532d 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72  verflow);.  pCur
1532e 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b  ->aOverflow = 0;
1532f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69  .}../*.** Invali
15330 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f  date the overflo
15331 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  w page-list cach
15332 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72  e for all cursor
15333 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74  s opened.** on t
15334 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 20  he shared btree 
15335 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a  structure pBt..*
15336 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
15337 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66  validateAllOverf
15338 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 72 65  lowCache(BtShare
15339 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72  d *pBt){.  BtCur
1533a 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  sor *p;.  assert
1533b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1533c 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
1533d 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d   );.  for(p=pBt-
1533e 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70  >pCursor; p; p=p
1533f 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e  ->pNext){.    in
15340 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77  validateOverflow
15341 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  Cache(p);.  }.}.
15342 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
15343 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
15344 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20  efore modifying 
15345 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
15346 61 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 69 6e  a table.** to in
15347 76 61 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63  validate any inc
15348 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 74 68  rblob cursors th
15349 61 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74  at are open on t
1534a 68 65 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65  he.** row or one
1534b 20 6f 66 20 74 68 65 20 72 6f 77 73 20 62 65 69   of the rows bei
1534c 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a  ng modified..**.
1534d 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 69  ** If argument i
1534e 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73 20 74  sClearTable is t
1534f 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 65 6e  rue, then the en
15350 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66  tire contents of
15351 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73   the.** table is
15352 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c   about to be del
15353 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  eted. In this ca
15354 73 65 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c  se invalidate al
15355 6c 20 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75  l incrblob.** cu
15356 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e  rsors open on an
15357 79 20 72 6f 77 20 77 69 74 68 69 6e 20 74 68 65  y row within the
15358 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74   table with root
15359 2d 70 61 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a  -page pgnoRoot..
1535a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
1535b 20 69 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43   if argument isC
1535c 6c 65 61 72 54 61 62 6c 65 20 69 73 20 66 61 6c  learTable is fal
1535d 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  se, then the row
1535e 20 77 69 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69   with.** rowid i
1535f 52 6f 77 20 69 73 20 62 65 69 6e 67 20 72 65 70  Row is being rep
15360 6c 61 63 65 64 20 6f 72 20 64 65 6c 65 74 65 64  laced or deleted
15361 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 69  . In this case i
15362 6e 76 61 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c  nvalidate.** onl
15363 79 20 74 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62  y those incrblob
15364 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e   cursors open on
15365 20 74 68 61 74 20 73 70 65 63 69 66 69 63 20 72   that specific r
15366 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ow..*/.static vo
15367 69 64 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63  id invalidateInc
15368 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20  rblobCursors(.  
15369 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20  Btree *pBtree,  
1536a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
1536b 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
1536c 63 68 65 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69  check */.  i64 i
1536d 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 20  Row,            
1536e 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20     /* The rowid 
1536f 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 63 68  that might be ch
15370 61 6e 67 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  anging */.  int 
15371 69 73 43 6c 65 61 72 54 61 62 6c 65 20 20 20 20  isClearTable    
15372 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
15373 6c 6c 20 72 6f 77 73 20 61 72 65 20 62 65 69 6e  ll rows are bein
15374 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a  g deleted */.){.
15375 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20    BtCursor *p;. 
15376 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15377 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20   pBtree->pBt;.  
15378 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
15379 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
1537a 42 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28  Btree) );.  for(
1537b 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20  p=pBt->pCursor; 
1537c 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
1537d 20 20 20 20 69 66 28 20 70 2d 3e 69 73 49 6e 63      if( p->isInc
1537e 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 28  rblobHandle && (
1537f 69 73 43 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20  isClearTable || 
15380 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52  p->info.nKey==iR
15381 6f 77 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ow) ){.      p->
15382 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
15383 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 20  INVALID;.    }. 
15384 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a   }.}..#else.  /*
15385 20 53 74 75 62 20 66 75 6e 63 74 69 6f 6e 73 20   Stub functions 
15386 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 20 69 73  when INCRBLOB is
15387 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 20 23 64   omitted */.  #d
15388 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65  efine invalidate
15389 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29  OverflowCache(x)
1538a 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c  .  #define inval
1538b 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77  idateAllOverflow
1538c 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 69  Cache(x).  #defi
1538d 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63  ne invalidateInc
1538e 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 78 2c 79  rblobCursors(x,y
1538f 2c 7a 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ,z).#endif /* SQ
15390 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
15391 4f 42 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  OB */../*.** Set
15392 20 62 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65   bit pgno of the
15393 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f   BtShared.pHasCo
15394 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68  ntent bitvec. Th
15395 69 73 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a  is is called .**
15396 20 77 68 65 6e 20 61 20 70 61 67 65 20 74 68 61   when a page tha
15397 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e  t previously con
15398 74 61 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f  tained data beco
15399 6d 65 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20  mes a free-list 
1539a 6c 65 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a  leaf .** page..*
1539b 2a 0a 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65  *.** The BtShare
1539c 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69  d.pHasContent bi
1539d 74 76 65 63 20 65 78 69 73 74 73 20 74 6f 20 77  tvec exists to w
1539e 6f 72 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62  ork around an ob
1539f 73 63 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75  scure.** bug cau
153a0 73 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 72  sed by the inter
153a1 61 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73  action of two us
153a2 65 66 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61  eful IO optimiza
153a3 74 69 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e  tions surroundin
153a4 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c  g.** free-list l
153a5 65 61 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a  eaf pages:.**.**
153a6 20 20 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64     1) When all d
153a7 61 74 61 20 69 73 20 64 65 6c 65 74 65 64 20 66  ata is deleted f
153a8 72 6f 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74  rom a page and t
153a9 68 65 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a  he page becomes.
153aa 2a 2a 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c  **      a free-l
153ab 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74  ist leaf page, t
153ac 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77  he page is not w
153ad 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
153ae 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28  tabase.**      (
153af 61 73 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61  as free-list lea
153b0 66 20 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20  f pages contain 
153b1 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61  no meaningful da
153b2 74 61 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a  ta). Sometimes.*
153b3 2a 20 20 20 20 20 20 73 75 63 68 20 61 20 70 61  *      such a pa
153b4 67 65 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a  ge is not even j
153b5 6f 75 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74  ournalled (as it
153b6 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64   will not be mod
153b7 69 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77  ified,.**      w
153b8 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61  hy bother journa
153b9 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a  lling it?)..**.*
153ba 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20 66 72  *   2) When a fr
153bb 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67  ee-list leaf pag
153bc 65 20 69 73 20 72 65 75 73 65 64 2c 20 69 74 73  e is reused, its
153bd 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20   content is not 
153be 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f  read.**      fro
153bf 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  m the database o
153c0 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  r written to the
153c1 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77   journal file (w
153c2 68 79 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20  hy should it.** 
153c3 20 20 20 20 20 62 65 2c 20 69 66 20 69 74 20 69       be, if it i
153c4 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61  s not at all mea
153c5 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a  ningful?)..**.**
153c6 20 42 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20   By themselves, 
153c7 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69  these optimizati
153c8 6f 6e 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e  ons work fine an
153c9 64 20 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64  d provide a hand
153ca 79 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  y.** performance
153cb 20 62 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64   boost to bulk d
153cc 65 6c 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20  elete or insert 
153cd 6f 70 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65  operations. Howe
153ce 76 65 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67  ver, if.** a pag
153cf 65 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68  e is moved to th
153d0 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20  e free-list and 
153d1 74 68 65 6e 20 72 65 75 73 65 64 20 77 69 74 68  then reused with
153d2 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74  in the same.** t
153d3 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72  ransaction, a pr
153d4 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20  oblem comes up. 
153d5 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  If the page is n
153d6 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68  ot journalled wh
153d7 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65  en.** it is move
153d8 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69  d to the free-li
153d9 73 74 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73  st and it is als
153da 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64  o not journalled
153db 20 77 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65   when it.** is e
153dc 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68  xtracted from th
153dd 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20  e free-list and 
153de 72 65 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65  reused, then the
153df 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a   original data.*
153e0 2a 20 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49  * may be lost. I
153e1 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
153e2 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61   rollback, it ma
153e3 79 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c  y not be possibl
153e4 65 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20  e.** to restore 
153e5 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
153e6 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e  its original con
153e7 66 69 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  figuration..**.*
153e8 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69  * The solution i
153e9 73 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70  s the BtShared.p
153ea 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65  HasContent bitve
153eb 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 70 61  c. Whenever a pa
153ec 67 65 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20  ge is .** moved 
153ed 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65  to become a free
153ee 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c  -list leaf page,
153ef 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
153f0 6e 67 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74  ng bit is.** set
153f1 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20   in the bitvec. 
153f2 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20  Whenever a leaf 
153f3 70 61 67 65 20 69 73 20 65 78 74 72 61 63 74 65  page is extracte
153f4 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d  d from the free-
153f5 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a  list,.** optimiz
153f6 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73  ation 2 above is
153f7 20 6f 6d 69 74 74 65 64 20 69 66 20 74 68 65 20   omitted if the 
153f8 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69  corresponding bi
153f9 74 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20  t is already.** 
153fa 73 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e  set in BtShared.
153fb 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65  pHasContent. The
153fc 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
153fd 20 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61   bitvec are clea
153fe 72 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e  red.** at the en
153ff 64 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73  d of every trans
15400 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  action..*/.stati
15401 63 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61  c int btreeSetHa
15402 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65  sContent(BtShare
15403 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e  d *pBt, Pgno pgn
15404 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  o){.  int rc = S
15405 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
15406 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e  !pBt->pHasConten
15407 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61  t ){.    int nPa
15408 67 65 20 3d 20 31 30 30 3b 0a 20 20 20 20 73 71  ge = 100;.    sq
15409 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
1540a 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  unt(pBt->pPager,
1540b 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 2f 2a   &nPage);.    /*
1540c 20 49 66 20 73 71 6c 69 74 65 33 50 61 67 65 72   If sqlite3Pager
1540d 50 61 67 65 63 6f 75 6e 74 28 29 20 66 61 69 6c  Pagecount() fail
1540e 73 20 74 68 65 72 65 20 69 73 20 6e 6f 20 68 61  s there is no ha
1540f 72 6d 20 62 65 63 61 75 73 65 20 74 68 65 0a 20  rm because the. 
15410 20 20 20 2a 2a 20 6e 50 61 67 65 20 76 61 72 69     ** nPage vari
15411 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65  able is unchange
15412 64 20 66 72 6f 6d 20 69 74 73 20 64 65 66 61 75  d from its defau
15413 6c 74 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20  lt value of 100 
15414 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73  */.    pBt->pHas
15415 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65  Content = sqlite
15416 33 42 69 74 76 65 63 43 72 65 61 74 65 28 28 75  3BitvecCreate((u
15417 33 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 69  32)nPage);.    i
15418 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e  f( !pBt->pHasCon
15419 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63  tent ){.      rc
1541a 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1541b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
1541c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1541d 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42  & pgno<=sqlite3B
1541e 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70  itvecSize(pBt->p
1541f 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20  HasContent) ){. 
15420 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
15421 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48  itvecSet(pBt->pH
15422 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29  asContent, pgno)
15423 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
15424 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72  c;.}../*.** Quer
15425 79 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70  y the BtShared.p
15426 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f  HasContent vecto
15427 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  r..**.** This fu
15428 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
15429 20 77 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73   when a free-lis
1542a 74 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72  t leaf page is r
1542b 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a  emoved from the.
1542c 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72  ** free-list for
1542d 20 72 65 75 73 65 2e 20 49 74 20 72 65 74 75 72   reuse. It retur
1542e 6e 73 20 66 61 6c 73 65 20 69 66 20 69 74 20 69  ns false if it i
1542f 73 20 73 61 66 65 20 74 6f 20 72 65 74 72 69 65  s safe to retrie
15430 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66  ve the.** page f
15431 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61  rom the pager la
15432 79 65 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f  yer with the 'no
15433 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73  -content' flag s
15434 65 74 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69  et. True otherwi
15435 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
15436 74 20 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e  t btreeGetHasCon
15437 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70  tent(BtShared *p
15438 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  Bt, Pgno pgno){.
15439 20 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42    Bitvec *p = pB
1543a 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a  t->pHasContent;.
1543b 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28    return (p && (
1543c 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76  pgno>sqlite3Bitv
1543d 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c  ecSize(p) || sql
1543e 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
1543f 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a  , pgno)));.}../*
15440 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72  .** Clear (destr
15441 6f 79 29 20 74 68 65 20 42 74 53 68 61 72 65 64  oy) the BtShared
15442 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74  .pHasContent bit
15443 76 65 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64  vec. This should
15444 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61   be.** invoked a
15445 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e  t the conclusion
15446 20 6f 66 20 65 61 63 68 20 77 72 69 74 65 2d 74   of each write-t
15447 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  ransaction..*/.s
15448 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65  tatic void btree
15449 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28  ClearHasContent(
1544a 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
1544b 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
1544c 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73  estroy(pBt->pHas
1544d 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d  Content);.  pBt-
1544e 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30  >pHasContent = 0
1544f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20  ;.}../*.** Save 
15450 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73  the current curs
15451 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74  or position in t
15452 68 65 20 76 61 72 69 61 62 6c 65 73 20 42 74 43  he variables BtC
15453 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61  ursor.nKey .** a
15454 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79  nd BtCursor.pKey
15455 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73 20 73  . The cursor's s
15456 74 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 43  tate is set to C
15457 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
15458 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  K..**.** The cal
15459 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20  ler must ensure 
1545a 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20  that the cursor 
1545b 69 73 20 76 61 6c 69 64 20 28 68 61 73 20 65 53  is valid (has eS
1545c 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
1545d 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20  ID).** prior to 
1545e 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
1545f 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69  tine.  .*/.stati
15460 63 20 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72  c int saveCursor
15461 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f  Position(BtCurso
15462 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20  r *pCur){.  int 
15463 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43  rc;..  assert( C
15464 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75  URSOR_VALID==pCu
15465 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61  r->eState );.  a
15466 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e  ssert( 0==pCur->
15467 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74  pKey );.  assert
15468 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
15469 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72  ex(pCur) );..  r
1546a 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1546b 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70  KeySize(pCur, &p
1546c 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 61 73  Cur->nKey);.  as
1546d 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1546e 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 53 69  _OK );  /* KeySi
1546f 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c  ze() cannot fail
15470 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69   */..  /* If thi
15471 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74  s is an intKey t
15472 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61  able, then the a
15473 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72  bove call to Btr
15474 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a  eeKeySize().  **
15475 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65   stores the inte
15476 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d  ger key in pCur-
15477 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63  >nKey. In this c
15478 61 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69  ase this value i
15479 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20  s.  ** all that 
1547a 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68  is required. Oth
1547b 65 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20  erwise, if pCur 
1547c 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61  is not open on a
1547d 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61  n intKey.  ** ta
1547e 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63  ble, then malloc
1547f 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73   space for and s
15480 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e  tore the pCur->n
15481 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79  Key bytes of key
15482 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a   .  ** data..  *
15483 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 72 2d  /.  if( 0==pCur-
15484 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b  >apPage[0]->intK
15485 65 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a  ey ){.    void *
15486 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61  pKey = sqlite3Ma
15487 6c 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d  lloc( (int)pCur-
15488 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28  >nKey );.    if(
15489 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72   pKey ){.      r
1548a 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1548b 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e  Key(pCur, 0, (in
1548c 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b  t)pCur->nKey, pK
1548d 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ey);.      if( r
1548e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1548f 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b          pCur->pK
15490 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20  ey = pKey;.     
15491 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
15492 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65  sqlite3_free(pKe
15493 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  y);.      }.    
15494 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
15495 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
15496 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
15497 72 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67  rt( !pCur->apPag
15498 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20  e[0]->intKey || 
15499 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a  !pCur->pKey );..
1549a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1549b 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  _OK ){.    int i
1549c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1549d 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69  <=pCur->iPage; i
1549e 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  ++){.      relea
1549f 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50  sePage(pCur->apP
154a0 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70  age[i]);.      p
154a1 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d  Cur->apPage[i] =
154a2 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43   0;.    }.    pC
154a3 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a  ur->iPage = -1;.
154a4 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
154a5 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52   = CURSOR_REQUIR
154a6 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e  ESEEK;.  }..  in
154a7 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77  validateOverflow
154a8 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72  Cache(pCur);.  r
154a9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
154aa 2a 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69  ** Save the posi
154ab 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72  tions of all cur
154ac 73 6f 72 73 20 28 65 78 63 65 70 74 20 70 45 78  sors (except pEx
154ad 63 65 70 74 29 20 74 68 61 74 20 61 72 65 20 6f  cept) that are o
154ae 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 74 61  pen on.** the ta
154af 62 6c 65 20 20 77 69 74 68 20 72 6f 6f 74 2d 70  ble  with root-p
154b0 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c  age iRoot. Usual
154b1 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c  ly, this is call
154b2 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63  ed just before c
154b3 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74  ursor.** pExcept
154b4 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f 64 69   is used to modi
154b5 66 79 20 74 68 65 20 74 61 62 6c 65 20 28 42 74  fy the table (Bt
154b6 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42  reeDelete() or B
154b7 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a  treeInsert())..*
154b8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76  /.static int sav
154b9 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68  eAllCursors(BtSh
154ba 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
154bb 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20  iRoot, BtCursor 
154bc 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43  *pExcept){.  BtC
154bd 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65  ursor *p;.  asse
154be 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
154bf 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
154c0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
154c1 70 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45  pExcept==0 || pE
154c2 78 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20  xcept->pBt==pBt 
154c3 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e  );.  for(p=pBt->
154c4 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d  pCursor; p; p=p-
154c5 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
154c6 20 70 21 3d 70 45 78 63 65 70 74 20 26 26 20 28   p!=pExcept && (
154c7 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70  0==iRoot || p->p
154c8 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20  gnoRoot==iRoot) 
154c9 26 26 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 65  && .        p->e
154ca 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
154cb 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  LID ){.      int
154cc 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72   rc = saveCursor
154cd 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20  Position(p);.   
154ce 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
154cf 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  !=rc ){.        
154d0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
154d1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
154d2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
154d3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
154d4 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73  the current curs
154d5 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a  or position..*/.
154d6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
154d7 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
154d8 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75  ClearCursor(BtCu
154d9 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61  rsor *pCur){.  a
154da 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
154db 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
154dc 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
154dd 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70  pCur->pKey);.  p
154de 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20  Cur->pKey = 0;. 
154df 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
154e0 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
154e1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73  }../*.** In this
154e2 20 76 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65   version of Btre
154e3 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73  eMoveto, pKey is
154e4 20 61 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20   a packed index 
154e5 72 65 63 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61  record.** such a
154e6 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62  s is generated b
154e7 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63  y the OP_MakeRec
154e8 6f 72 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70  ord opcode.  Unp
154e9 61 63 6b 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72  ack the.** recor
154ea 64 20 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20  d and then call 
154eb 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
154ec 6b 65 64 28 29 20 74 6f 20 64 6f 20 74 68 65 20  ked() to do the 
154ed 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  work..*/.static 
154ee 69 6e 74 20 62 74 72 65 65 4d 6f 76 65 74 6f 28  int btreeMoveto(
154ef 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
154f0 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  r,     /* Cursor
154f1 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 62 74 72   open on the btr
154f2 65 65 20 74 6f 20 62 65 20 73 65 61 72 63 68 65  ee to be searche
154f3 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  d */.  const voi
154f4 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61  d *pKey,   /* Pa
154f5 63 6b 65 64 20 6b 65 79 20 69 66 20 74 68 65 20  cked key if the 
154f6 62 74 72 65 65 20 69 73 20 61 6e 20 69 6e 64 65  btree is an inde
154f7 78 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c  x */.  i64 nKey,
154f8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
154f9 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 74 61  teger key for ta
154fa 62 6c 65 73 2e 20 20 53 69 7a 65 20 6f 66 20 70  bles.  Size of p
154fb 4b 65 79 20 66 6f 72 20 69 6e 64 69 63 65 73 20  Key for indices 
154fc 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 2c 20 20  */.  int bias,  
154fd 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 61 73           /* Bias
154fe 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68   search to the h
154ff 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74  igh end */.  int
15500 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20   *pRes          
15501 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 63 68   /* Write search
15502 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
15503 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
15504 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15505 20 2f 2a 20 53 74 61 74 75 73 20 63 6f 64 65 20   /* Status code 
15506 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63  */.  UnpackedRec
15507 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 20 20 20  ord *pIdxKey;   
15508 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 65  /* Unpacked inde
15509 78 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72 20  x key */.  char 
1550a 61 53 70 61 63 65 5b 31 35 30 5d 3b 20 20 20 20  aSpace[150];    
1550b 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 70        /* Temp sp
1550c 61 63 65 20 66 6f 72 20 70 49 64 78 4b 65 79 20  ace for pIdxKey 
1550d 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c  - to avoid a mal
1550e 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4b  loc */..  if( pK
1550f 65 79 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ey ){.    assert
15510 28 20 6e 4b 65 79 3d 3d 28 69 36 34 29 28 69 6e  ( nKey==(i64)(in
15511 74 29 6e 4b 65 79 20 29 3b 0a 20 20 20 20 70 49  t)nKey );.    pI
15512 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56  dxKey = sqlite3V
15513 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28  dbeRecordUnpack(
15514 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20  pCur->pKeyInfo, 
15515 28 69 6e 74 29 6e 4b 65 79 2c 20 70 4b 65 79 2c  (int)nKey, pKey,
15516 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
15517 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15518 20 20 20 20 20 20 20 61 53 70 61 63 65 2c 20 73         aSpace, s
15519 69 7a 65 6f 66 28 61 53 70 61 63 65 29 29 3b 0a  izeof(aSpace));.
1551a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d      if( pIdxKey=
1551b 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
1551c 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73  TE_NOMEM;.  }els
1551d 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d  e{.    pIdxKey =
1551e 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73   0;.  }.  rc = s
1551f 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
15520 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72 2c 20  oUnpacked(pCur, 
15521 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62  pIdxKey, nKey, b
15522 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20 69 66  ias, pRes);.  if
15523 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 73 71  ( pKey ){.    sq
15524 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55  lite3VdbeDeleteU
15525 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49  npackedRecord(pI
15526 64 78 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65  dxKey);.  }.  re
15527 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
15528 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 63 75  * Restore the cu
15529 72 73 6f 72 20 74 6f 20 74 68 65 20 70 6f 73 69  rsor to the posi
1552a 74 69 6f 6e 20 69 74 20 77 61 73 20 69 6e 20 28  tion it was in (
1552b 6f 72 20 61 73 20 63 6c 6f 73 65 20 74 6f 20 61  or as close to a
1552c 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a 2a 20 77  s possible).** w
1552d 68 65 6e 20 73 61 76 65 43 75 72 73 6f 72 50 6f  hen saveCursorPo
1552e 73 69 74 69 6f 6e 28 29 20 77 61 73 20 63 61 6c  sition() was cal
1552f 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74  led. Note that t
15530 68 69 73 20 63 61 6c 6c 20 64 65 6c 65 74 65 73  his call deletes
15531 20 74 68 65 20 0a 2a 2a 20 73 61 76 65 64 20 70   the .** saved p
15532 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20 73 74 6f  osition info sto
15533 72 65 64 20 62 79 20 73 61 76 65 43 75 72 73 6f  red by saveCurso
15534 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20 73 6f 20  rPosition(), so 
15535 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20  there can be.** 
15536 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 66 66 65  at most one effe
15537 63 74 69 76 65 20 72 65 73 74 6f 72 65 43 75 72  ctive restoreCur
15538 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61  sorPosition() ca
15539 6c 6c 20 61 66 74 65 72 20 65 61 63 68 20 0a 2a  ll after each .*
1553a 2a 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69  * saveCursorPosi
1553b 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  tion()..*/.stati
1553c 63 20 69 6e 74 20 62 74 72 65 65 52 65 73 74 6f  c int btreeResto
1553d 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
1553e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
1553f 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
15540 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
15541 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
15542 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
15543 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52  eState>=CURSOR_R
15544 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20  EQUIRESEEK );.  
15545 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
15546 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29  ==CURSOR_FAULT )
15547 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43 75  {.    return pCu
15548 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 7d  r->skipNext;.  }
15549 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20  .  pCur->eState 
1554a 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1554b 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 4d 6f  ;.  rc = btreeMo
1554c 76 65 74 6f 28 70 43 75 72 2c 20 70 43 75 72 2d  veto(pCur, pCur-
1554d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e 6e 4b 65  >pKey, pCur->nKe
1554e 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e 73 6b 69  y, 0, &pCur->ski
1554f 70 4e 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63  pNext);.  if( rc
15550 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
15551 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
15552 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20  pCur->pKey);.   
15553 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b   pCur->pKey = 0;
15554 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
15555 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
15556 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d  R_VALID || pCur-
15557 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15558 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20  INVALID );.  }. 
15559 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
1555a 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65 43 75  define restoreCu
1555b 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20  rsorPosition(p) 
1555c 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 65 3e 3d  \.  (p->eState>=
1555d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
1555e 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 20 20 20  EK ? \.         
1555f 62 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73  btreeRestoreCurs
15560 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 3a 20  orPosition(p) : 
15561 5c 0a 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  \.         SQLIT
15562 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 65 74  E_OK)../*.** Det
15563 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
15564 72 20 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68  r not a cursor h
15565 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68  as moved from th
15566 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 0a 2a 2a  e position it.**
15567 20 77 61 73 20 6c 61 73 74 20 70 6c 61 63 65 64   was last placed
15568 20 61 74 2e 20 20 43 75 72 73 6f 72 73 20 63 61   at.  Cursors ca
15569 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74 68 65 20  n move when the 
1556a 72 6f 77 20 74 68 65 79 20 61 72 65 20 70 6f 69  row they are poi
1556b 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 69 73 20 64  nting.** at is d
1556c 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20  eleted out from 
1556d 75 6e 64 65 72 20 74 68 65 6d 2e 0a 2a 2a 0a 2a  under them..**.*
1556e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
1556f 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20  eturns an error 
15570 63 6f 64 65 20 69 66 20 73 6f 6d 65 74 68 69 6e  code if somethin
15571 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 54  g goes wrong.  T
15572 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 2a 70  he.** integer *p
15573 48 61 73 4d 6f 76 65 64 20 69 73 20 73 65 74 20  HasMoved is set 
15574 74 6f 20 6f 6e 65 20 69 66 20 74 68 65 20 63 75  to one if the cu
15575 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 61  rsor has moved a
15576 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a  nd 0 if not..*/.
15577 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15578 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
15579 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74  ursorHasMoved(Bt
1557a 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
1557b 74 20 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a 20  t *pHasMoved){. 
1557c 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d   int rc;..  rc =
1557d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
1557e 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20  sition(pCur);.  
1557f 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70  if( rc ){.    *p
15580 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20  HasMoved = 1;.  
15581 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
15582 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
15583 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate!=CURSOR_VALI
15584 44 20 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 4e  D || pCur->skipN
15585 65 78 74 21 3d 30 20 29 7b 0a 20 20 20 20 2a 70  ext!=0 ){.    *p
15586 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20  HasMoved = 1;.  
15587 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 48 61 73  }else{.    *pHas
15588 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20  Moved = 0;.  }. 
15589 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1558a 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  K;.}..#ifndef SQ
1558b 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1558c 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  CUUM./*.** Given
1558d 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f   a page number o
1558e 66 20 61 20 72 65 67 75 6c 61 72 20 64 61 74 61  f a regular data
1558f 62 61 73 65 20 70 61 67 65 2c 20 72 65 74 75 72  base page, retur
15590 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75  n the page.** nu
15591 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70 6f 69  mber for the poi
15592 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 74 68  nter-map page th
15593 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
15594 65 6e 74 72 79 20 66 6f 72 20 74 68 65 0a 2a 2a  entry for the.**
15595 20 69 6e 70 75 74 20 70 61 67 65 20 6e 75 6d 62   input page numb
15596 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67  er..*/.static Pg
15597 6e 6f 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28  no ptrmapPageno(
15598 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
15599 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74  gno pgno){.  int
1559a 20 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67   nPagesPerMapPag
1559b 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 4d 61  e;.  Pgno iPtrMa
1559c 70 2c 20 72 65 74 3b 0a 20 20 61 73 73 65 72 74  p, ret;.  assert
1559d 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1559e 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
1559f 20 29 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d   );.  nPagesPerM
155a0 61 70 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75  apPage = (pBt->u
155a1 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a  sableSize/5)+1;.
155a2 20 20 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e    iPtrMap = (pgn
155a3 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61  o-2)/nPagesPerMa
155a4 70 50 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28  pPage;.  ret = (
155a5 69 50 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65  iPtrMap*nPagesPe
155a6 72 4d 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a  rMapPage) + 2; .
155a7 20 20 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49    if( ret==PENDI
155a8 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
155a9 29 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a  ) ){.    ret++;.
155aa 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74    }.  return ret
155ab 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
155ac 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74   an entry into t
155ad 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a  he pointer map..
155ae 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
155af 6e 65 20 75 70 64 61 74 65 73 20 74 68 65 20 70  ne updates the p
155b0 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79  ointer map entry
155b1 20 66 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72   for page number
155b2 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61   'key'.** so tha
155b3 74 20 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70  t it maps to typ
155b4 65 20 27 65 54 79 70 65 27 20 61 6e 64 20 70 61  e 'eType' and pa
155b5 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72  rent page number
155b6 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a 20 49   'pgno'..**.** I
155b7 66 20 2a 70 52 43 20 69 73 20 69 6e 69 74 69 61  f *pRC is initia
155b8 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28 6e 6f  lly non-zero (no
155b9 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 20 74 68 65  n-SQLITE_OK) the
155ba 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
155bb 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 20 49  s.** a no-op.  I
155bc 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
155bd 73 2c 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  s, the appropria
155be 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  te error code is
155bf 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f   written.** into
155c0 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 61 74 69 63   *pRC..*/.static
155c1 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75 74 28   void ptrmapPut(
155c2 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
155c3 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 70  gno key, u8 eTyp
155c4 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 2c 20  e, Pgno parent, 
155c5 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 44 62 50  int *pRC){.  DbP
155c6 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f  age *pDbPage;  /
155c7 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  * The pointer ma
155c8 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  p page */.  u8 *
155c9 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a  pPtrmap;      /*
155ca 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   The pointer map
155cb 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20   data */.  Pgno 
155cc 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20  iPtrmap;     /* 
155cd 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
155ce 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  page number */. 
155cf 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20   int offset;    
155d0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20     /* Offset in 
155d1 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
155d2 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
155d3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
155d4 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 66  n code from subf
155d5 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 69  unctions */..  i
155d6 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e  f( *pRC ) return
155d7 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
155d8 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
155d9 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
155da 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 72 2d 6a   /* The master-j
155db 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62  ournal page numb
155dc 65 72 20 6d 75 73 74 20 6e 65 76 65 72 20 62 65  er must never be
155dd 20 75 73 65 64 20 61 73 20 61 20 70 6f 69 6e 74   used as a point
155de 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20  er map page */. 
155df 20 61 73 73 65 72 74 28 20 30 3d 3d 50 54 52 4d   assert( 0==PTRM
155e0 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 50  AP_ISPAGE(pBt, P
155e1 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
155e2 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 61 73 73  (pBt)) );..  ass
155e3 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61  ert( pBt->autoVa
155e4 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 20 6b 65  cuum );.  if( ke
155e5 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 43  y==0 ){.    *pRC
155e6 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
155e7 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65 74 75  T_BKPT;.    retu
155e8 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61  rn;.  }.  iPtrma
155e9 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e  p = PTRMAP_PAGEN
155ea 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72  O(pBt, key);.  r
155eb 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
155ec 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  Get(pBt->pPager,
155ed 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61   iPtrmap, &pDbPa
155ee 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ge);.  if( rc!=S
155ef 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
155f0 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72  *pRC = rc;.    r
155f1 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6f 66 66  eturn;.  }.  off
155f2 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52  set = PTRMAP_PTR
155f3 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20  OFFSET(iPtrmap, 
155f4 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f 66 66 73  key);.  if( offs
155f5 65 74 3c 30 20 29 7b 0a 20 20 20 20 2a 70 52 43  et<0 ){.    *pRC
155f6 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
155f7 54 5f 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f  T_BKPT;.    goto
155f8 20 70 74 72 6d 61 70 5f 65 78 69 74 3b 0a 20 20   ptrmap_exit;.  
155f9 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75  }.  pPtrmap = (u
155fa 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72  8 *)sqlite3Pager
155fb 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29  GetData(pDbPage)
155fc 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d  ;..  if( eType!=
155fd 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20  pPtrmap[offset] 
155fe 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70 50 74  || get4byte(&pPt
155ff 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21  rmap[offset+1])!
15600 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 54  =parent ){.    T
15601 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f 55 50  RACE(("PTRMAP_UP
15602 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64  DATE: %d->(%d,%d
15603 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 65  )\n", key, eType
15604 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 20  , parent));.    
15605 2a 70 52 43 3d 20 72 63 20 3d 20 73 71 6c 69 74  *pRC= rc = sqlit
15606 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62  e3PagerWrite(pDb
15607 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
15608 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
15609 20 20 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66        pPtrmap[of
1560a 66 73 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20  fset] = eType;. 
1560b 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
1560c 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d  Ptrmap[offset+1]
1560d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d  , parent);.    }
1560e 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 5f 65 78 69  .  }..ptrmap_exi
1560f 74 3a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  t:.  sqlite3Page
15610 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b  rUnref(pDbPage);
15611 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61  .}../*.** Read a
15612 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  n entry from the
15613 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a   pointer map..**
15614 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
15615 20 72 65 74 72 69 65 76 65 73 20 74 68 65 20 70   retrieves the p
15616 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79  ointer map entry
15617 20 66 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c   for page 'key',
15618 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20   writing.** the 
15619 74 79 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20  type and parent 
1561a 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a  page number to *
1561b 70 45 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e  pEType and *pPgn
1561c 6f 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a  o respectively..
1561d 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65  ** An error code
1561e 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
1561f 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
15620 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20  rong, otherwise 
15621 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74  SQLITE_OK..*/.st
15622 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47  atic int ptrmapG
15623 65 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  et(BtShared *pBt
15624 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a  , Pgno key, u8 *
15625 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50  pEType, Pgno *pP
15626 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a  gno){.  DbPage *
15627 70 44 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68  pDbPage;   /* Th
15628 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  e pointer map pa
15629 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72  ge */.  int iPtr
1562a 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f  map;       /* Po
1562b 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 69  inter map page i
1562c 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50  ndex */.  u8 *pP
1562d 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20  trmap;       /* 
1562e 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  Pointer map page
1562f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f   data */.  int o
15630 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ffset;        /*
15631 20 4f 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79   Offset of entry
15632 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20   in pointer map 
15633 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  */.  int rc;..  
15634 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15635 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
15636 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74  mutex) );..  iPt
15637 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41  rmap = PTRMAP_PA
15638 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a  GENO(pBt, key);.
15639 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1563a 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67  gerGet(pBt->pPag
1563b 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44  er, iPtrmap, &pD
1563c 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  bPage);.  if( rc
1563d 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
1563e 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72  n rc;.  }.  pPtr
1563f 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69  map = (u8 *)sqli
15640 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
15641 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66  pDbPage);..  off
15642 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52  set = PTRMAP_PTR
15643 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20  OFFSET(iPtrmap, 
15644 6b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  key);.  assert( 
15645 70 45 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a  pEType!=0 );.  *
15646 70 45 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70  pEType = pPtrmap
15647 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20  [offset];.  if( 
15648 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d  pPgno ) *pPgno =
15649 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d   get4byte(&pPtrm
1564a 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a  ap[offset+1]);..
1564b 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
1564c 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ref(pDbPage);.  
1564d 69 66 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c  if( *pEType<1 ||
1564e 20 2a 70 45 54 79 70 65 3e 35 20 29 20 72 65 74   *pEType>5 ) ret
1564f 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15650 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72  PT_BKPT;.  retur
15651 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
15652 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69  #else /* if defi
15653 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned SQLITE_OMIT_
15654 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20  AUTOVACUUM */.  
15655 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75  #define ptrmapPu
15656 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 29 0a 20 20  t(w,x,y,z,rc).  
15657 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65  #define ptrmapGe
15658 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54  t(w,x,y,z) SQLIT
15659 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70  E_OK.  #define p
1565a 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28  trmapPutOvflPtr(
1565b 78 2c 20 79 2c 20 72 63 29 0a 23 65 6e 64 69 66  x, y, rc).#endif
1565c 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20  ../*.** Given a 
1565d 62 74 72 65 65 20 70 61 67 65 20 61 6e 64 20 61  btree page and a
1565e 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d   cell index (0 m
1565f 65 61 6e 73 20 74 68 65 20 66 69 72 73 74 20 63  eans the first c
15660 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61  ell on.** the pa
15661 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20  ge, 1 means the 
15662 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64  second cell, and
15663 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72   so forth) retur
15664 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  n a pointer.** t
15665 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  o the cell conte
15666 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  nt..**.** This r
15667 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c  outine works onl
15668 79 20 66 6f 72 20 70 61 67 65 73 20 74 68 61 74  y for pages that
15669 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20   do not contain 
1566a 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a  overflow cells..
1566b 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43  */.#define findC
1566c 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28 28 50  ell(P,I) \.  ((P
1566d 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d  )->aData + ((P)-
1566e 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65 74 32  >maskPage & get2
1566f 62 79 74 65 28 26 28 50 29 2d 3e 61 44 61 74 61  byte(&(P)->aData
15670 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 66 73 65 74  [(P)->cellOffset
15671 2b 32 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a 0a 2a  +2*(I)])))../*.*
15672 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 63 6f  * This a more co
15673 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f 66  mplex version of
15674 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68 61 74   findCell() that
15675 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 70 61   works for.** pa
15676 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f 6e 74  ges that do cont
15677 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  ain overflow cel
15678 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38  ls..*/.static u8
15679 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65   *findOverflowCe
1567a 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ll(MemPage *pPag
1567b 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20  e, int iCell){. 
1567c 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
1567d 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1567e 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
1567f 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
15680 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  (i=pPage->nOverf
15681 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  low-1; i>=0; i--
15682 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20  ){.    int k;.  
15683 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65    struct _OvflCe
15684 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70  ll *pOvfl;.    p
15685 4f 76 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61  Ovfl = &pPage->a
15686 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d  Ovfl[i];.    k =
15687 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20   pOvfl->idx;.   
15688 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b   if( k<=iCell ){
15689 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43  .      if( k==iC
1568a 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ell ){.        r
1568b 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65  eturn pOvfl->pCe
1568c 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
1568d 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d    iCell--;.    }
1568e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69  .  }.  return fi
1568f 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  ndCell(pPage, iC
15690 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ell);.}../*.** P
15691 61 72 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74  arse a cell cont
15692 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69  ent block and fi
15693 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e  ll in the CellIn
15694 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  fo structure.  T
15695 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20  here.** are two 
15696 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73  versions of this
15697 20 66 75 6e 63 74 69 6f 6e 2e 20 20 62 74 72 65   function.  btre
15698 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 61 6b  eParseCell() tak
15699 65 73 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e  es a .** cell in
1569a 64 65 78 20 61 73 20 74 68 65 20 73 65 63 6f 6e  dex as the secon
1569b 64 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 62  d argument and b
1569c 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
1569d 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 70  () .** takes a p
1569e 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62 6f  ointer to the bo
1569f 64 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 61  dy of the cell a
156a0 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67  s its second arg
156a1 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74  ument..**.** Wit
156a2 68 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74  hin this file, t
156a3 68 65 20 70 61 72 73 65 43 65 6c 6c 28 29 20 6d  he parseCell() m
156a4 61 63 72 6f 20 63 61 6e 20 62 65 20 63 61 6c 6c  acro can be call
156a5 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a  ed instead of.**
156a6 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50   btreeParseCellP
156a7 74 72 28 29 2e 20 55 73 69 6e 67 20 73 6f 6d 65  tr(). Using some
156a8 20 63 6f 6d 70 69 6c 65 72 73 2c 20 74 68 69 73   compilers, this
156a9 20 77 69 6c 6c 20 62 65 20 66 61 73 74 65 72 2e   will be faster.
156aa 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
156ab 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
156ac 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  r(.  MemPage *pP
156ad 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  age,         /* 
156ae 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  Page containing 
156af 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38  the cell */.  u8
156b0 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20   *pCell,        
156b1 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
156b2 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74 65 78   to the cell tex
156b3 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f  t. */.  CellInfo
156b4 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20   *pInfo         
156b5 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20  /* Fill in this 
156b6 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a  structure */.){.
156b7 20 20 75 31 36 20 6e 3b 20 20 20 20 20 20 20 20    u16 n;        
156b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
156b9 62 65 72 20 62 79 74 65 73 20 69 6e 20 63 65 6c  ber bytes in cel
156ba 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72  l content header
156bb 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f   */.  u32 nPaylo
156bc 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ad;           /*
156bd 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
156be 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f 61 64   of cell payload
156bf 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
156c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
156c1 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
156c2 74 65 78 29 20 29 3b 0a 0a 20 20 70 49 6e 66 6f  tex) );..  pInfo
156c3 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b  ->pCell = pCell;
156c4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
156c5 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 50 61  ->leaf==0 || pPa
156c6 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b 0a 20  ge->leaf==1 );. 
156c7 20 6e 20 3d 20 70 50 61 67 65 2d 3e 63 68 69 6c   n = pPage->chil
156c8 64 50 74 72 53 69 7a 65 3b 0a 20 20 61 73 73 65  dPtrSize;.  asse
156c9 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 50 61 67 65  rt( n==4-4*pPage
156ca 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 69 66 28 20  ->leaf );.  if( 
156cb 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
156cc 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
156cd 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20  hasData ){.     
156ce 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33   n += getVarint3
156cf 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61  2(&pCell[n], nPa
156d0 79 6c 6f 61 64 29 3b 0a 20 20 20 20 7d 65 6c 73  yload);.    }els
156d1 65 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c 6f 61  e{.      nPayloa
156d2 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  d = 0;.    }.   
156d3 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 28   n += getVarint(
156d4 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a  &pCell[n], (u64*
156d5 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a  )&pInfo->nKey);.
156d6 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61      pInfo->nData
156d7 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d   = nPayload;.  }
156d8 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 2d  else{.    pInfo-
156d9 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20  >nData = 0;.    
156da 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32  n += getVarint32
156db 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79  (&pCell[n], nPay
156dc 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 6e 66 6f  load);.    pInfo
156dd 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 79 6c 6f 61  ->nKey = nPayloa
156de 64 3b 0a 20 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e  d;.  }.  pInfo->
156df 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61 79 6c  nPayload = nPayl
156e0 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48  oad;.  pInfo->nH
156e1 65 61 64 65 72 20 3d 20 6e 3b 0a 20 20 74 65 73  eader = n;.  tes
156e2 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d  tcase( nPayload=
156e3 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c  =pPage->maxLocal
156e4 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
156e5 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d  nPayload==pPage-
156e6 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20  >maxLocal+1 );. 
156e7 20 69 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79   if( likely(nPay
156e8 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78  load<=pPage->max
156e9 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 20 20 2f 2a  Local) ){.    /*
156ea 20 54 68 69 73 20 69 73 20 74 68 65 20 28 65 61   This is the (ea
156eb 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20  sy) common case 
156ec 77 68 65 72 65 20 74 68 65 20 65 6e 74 69 72 65  where the entire
156ed 20 70 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20   payload fits.  
156ee 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61    ** on the loca
156ef 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72  l page.  No over
156f0 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72 65 64  flow is required
156f1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
156f2 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20   nSize;         
156f3 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f   /* Total size o
156f4 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  f cell content i
156f5 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 6e  n bytes */.    n
156f6 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20  Size = nPayload 
156f7 2b 20 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e  + n;.    pInfo->
156f8 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6e 50  nLocal = (u16)nP
156f9 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 49 6e 66  ayload;.    pInf
156fa 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 30  o->iOverflow = 0
156fb 3b 0a 20 20 20 20 69 66 28 20 28 6e 53 69 7a 65  ;.    if( (nSize
156fc 20 26 20 7e 33 29 3d 3d 30 20 29 7b 0a 20 20 20   & ~3)==0 ){.   
156fd 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 20 20 20     nSize = 4;   
156fe 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20       /* Minimum 
156ff 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a  cell size is 4 *
15700 2f 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66  /.    }.    pInf
15701 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 75 31 36 29  o->nSize = (u16)
15702 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nSize;.  }else{.
15703 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61      /* If the pa
15704 79 6c 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66  yload will not f
15705 69 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e  it completely on
15706 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c   the local page,
15707 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20   we have.    ** 
15708 74 6f 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75  to decide how mu
15709 63 68 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61  ch to store loca
1570a 6c 6c 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68  lly and how much
1570b 20 74 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20   to spill onto. 
1570c 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70     ** overflow p
1570d 61 67 65 73 2e 20 20 54 68 65 20 73 74 72 61 74  ages.  The strat
1570e 65 67 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69  egy is to minimi
1570f 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ze the amount of
15710 20 75 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73   unused.    ** s
15711 70 61 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77  pace on overflow
15712 20 70 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65   pages while kee
15713 70 69 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20  ping the amount 
15714 6f 66 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65  of local storage
15715 0a 20 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65  .    ** in betwe
15716 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20  en minLocal and 
15717 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a  maxLocal..    **
15718 0a 20 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a  .    ** Warning:
15719 20 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77    changing the w
1571a 61 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c  ay overflow payl
1571b 6f 61 64 20 69 73 20 64 69 73 74 72 69 62 75 74  oad is distribut
1571c 65 64 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a  ed in any.    **
1571d 20 77 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74   way will result
1571e 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69   in an incompati
1571f 62 6c 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e  ble file format.
15720 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
15721 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69  minLocal;  /* Mi
15722 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20  nimum amount of 
15723 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63  payload held loc
15724 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ally */.    int 
15725 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61  maxLocal;  /* Ma
15726 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20  ximum amount of 
15727 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63  payload held loc
15728 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ally */.    int 
15729 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76  surplus;   /* Ov
1572a 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61  erflow payload a
1572b 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63  vailable for loc
1572c 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20  al storage */.. 
1572d 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50     minLocal = pP
1572e 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20  age->minLocal;. 
1572f 20 20 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50     maxLocal = pP
15730 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20  age->maxLocal;. 
15731 20 20 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e     surplus = min
15732 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61  Local + (nPayloa
15733 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70  d - minLocal)%(p
15734 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
15735 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20  eSize - 4);.    
15736 74 65 73 74 63 61 73 65 28 20 73 75 72 70 6c 75  testcase( surplu
15737 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20  s==maxLocal );. 
15738 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75 72     testcase( sur
15739 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 2b 31  plus==maxLocal+1
1573a 20 29 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70   );.    if( surp
1573b 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20  lus <= maxLocal 
1573c 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  ){.      pInfo->
1573d 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 73 75  nLocal = (u16)su
1573e 72 70 6c 75 73 3b 0a 20 20 20 20 7d 65 6c 73 65  rplus;.    }else
1573f 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  {.      pInfo->n
15740 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6d 69 6e  Local = (u16)min
15741 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20  Local;.    }.   
15742 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f   pInfo->iOverflo
15743 77 20 3d 20 28 75 31 36 29 28 70 49 6e 66 6f 2d  w = (u16)(pInfo-
15744 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 3b 0a 20 20  >nLocal + n);.  
15745 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d    pInfo->nSize =
15746 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f   pInfo->iOverflo
15747 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65  w + 4;.  }.}.#de
15748 66 69 6e 65 20 70 61 72 73 65 43 65 6c 6c 28 70  fine parseCell(p
15749 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e  Page, iCell, pIn
1574a 66 6f 29 20 5c 0a 20 20 62 74 72 65 65 50 61 72  fo) \.  btreePar
1574b 73 65 43 65 6c 6c 50 74 72 28 28 70 50 61 67 65  seCellPtr((pPage
1574c 29 2c 20 66 69 6e 64 43 65 6c 6c 28 28 70 50 61  ), findCell((pPa
1574d 67 65 29 2c 20 28 69 43 65 6c 6c 29 29 2c 20 28  ge), (iCell)), (
1574e 70 49 6e 66 6f 29 29 0a 73 74 61 74 69 63 20 76  pInfo)).static v
1574f 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65  oid btreeParseCe
15750 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  ll(.  MemPage *p
15751 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  Page,         /*
15752 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67   Page containing
15753 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 69   the cell */.  i
15754 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 20 20 20  nt iCell,       
15755 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65         /* The ce
15756 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 72 73 74  ll index.  First
15757 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f 0a 20 20   cell is 0 */.  
15758 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20  CellInfo *pInfo 
15759 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20          /* Fill 
1575a 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72  in this structur
1575b 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72 73 65 43  e */.){.  parseC
1575c 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c  ell(pPage, iCell
1575d 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a  , pInfo);.}../*.
1575e 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74  ** Compute the t
1575f 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62  otal number of b
15760 79 74 65 73 20 74 68 61 74 20 61 20 43 65 6c 6c  ytes that a Cell
15761 20 6e 65 65 64 73 20 69 6e 20 74 68 65 20 63 65   needs in the ce
15762 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 65 61 20  ll.** data area 
15763 6f 66 20 74 68 65 20 62 74 72 65 65 2d 70 61 67  of the btree-pag
15764 65 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 6e  e.  The return n
15765 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 73 20 74  umber includes t
15766 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20  he cell.** data 
15767 68 65 61 64 65 72 20 61 6e 64 20 74 68 65 20 6c  header and the l
15768 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c 20 62 75  ocal payload, bu
15769 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 72 66 6c  t not any overfl
1576a 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74 68  ow page or.** th
1576b 65 20 73 70 61 63 65 20 75 73 65 64 20 62 79 20  e space used by 
1576c 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  the cell pointer
1576d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20  ..*/.static u16 
1576e 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d 50  cellSizePtr(MemP
1576f 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a  age *pPage, u8 *
15770 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 49  pCell){.  u8 *pI
15771 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b 70 50 61  ter = &pCell[pPa
15772 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65  ge->childPtrSize
15773 5d 3b 0a 20 20 75 33 32 20 6e 53 69 7a 65 3b 0a  ];.  u32 nSize;.
15774 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
15775 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 76 61  EBUG.  /* The va
15776 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
15777 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68  this function sh
15778 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 74  ould always be t
15779 68 65 20 73 61 6d 65 20 61 73 0a 20 20 2a 2a 20  he same as.  ** 
1577a 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f 2e 6e 53  the (CellInfo.nS
1577b 69 7a 65 29 20 76 61 6c 75 65 20 66 6f 75 6e 64  ize) value found
1577c 20 62 79 20 64 6f 69 6e 67 20 61 20 66 75 6c 6c   by doing a full
1577d 20 70 61 72 73 65 20 6f 66 20 74 68 65 0a 20 20   parse of the.  
1577e 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 51 4c 49  ** cell. If SQLI
1577f 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69  TE_DEBUG is defi
15780 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29  ned, an assert()
15781 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f   at the bottom o
15782 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63  f.  ** this func
15783 74 69 6f 6e 20 76 65 72 69 66 69 65 73 20 74 68  tion verifies th
15784 61 74 20 74 68 69 73 20 69 6e 76 61 72 69 61 6e  at this invarian
15785 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c 61 74 65  t is not violate
15786 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f  d. */.  CellInfo
15787 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 20 62 74   debuginfo;.  bt
15788 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
15789 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 64  pPage, pCell, &d
1578a 65 62 75 67 69 6e 66 6f 29 3b 0a 23 65 6e 64 69  ebuginfo);.#endi
1578b 66 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  f..  if( pPage->
1578c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 75 38  intKey ){.    u8
1578d 20 2a 70 45 6e 64 3b 0a 20 20 20 20 69 66 28 20   *pEnd;.    if( 
1578e 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29  pPage->hasData )
1578f 7b 0a 20 20 20 20 20 20 70 49 74 65 72 20 2b 3d  {.      pIter +=
15790 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 49 74   getVarint32(pIt
15791 65 72 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20  er, nSize);.    
15792 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 69  }else{.      nSi
15793 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  ze = 0;.    }.. 
15794 20 20 20 2f 2a 20 70 49 74 65 72 20 6e 6f 77 20     /* pIter now 
15795 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 36 34  points at the 64
15796 2d 62 69 74 20 69 6e 74 65 67 65 72 20 6b 65 79  -bit integer key
15797 20 76 61 6c 75 65 2c 20 61 20 76 61 72 69 61 62   value, a variab
15798 6c 65 20 6c 65 6e 67 74 68 20 0a 20 20 20 20 2a  le length .    *
15799 2a 20 69 6e 74 65 67 65 72 2e 20 54 68 65 20 66  * integer. The f
1579a 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6d  ollowing block m
1579b 6f 76 65 73 20 70 49 74 65 72 20 74 6f 20 70 6f  oves pIter to po
1579c 69 6e 74 20 61 74 20 74 68 65 20 66 69 72 73 74  int at the first
1579d 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 70 61 73   byte.    ** pas
1579e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
1579f 20 6b 65 79 20 76 61 6c 75 65 2e 20 2a 2f 0a 20   key value. */. 
157a0 20 20 20 70 45 6e 64 20 3d 20 26 70 49 74 65 72     pEnd = &pIter
157a1 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  [9];.    while( 
157a2 28 2a 70 49 74 65 72 2b 2b 29 26 30 78 38 30 20  (*pIter++)&0x80 
157a3 26 26 20 70 49 74 65 72 3c 70 45 6e 64 20 29 3b  && pIter<pEnd );
157a4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
157a5 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74  ter += getVarint
157a6 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29  32(pIter, nSize)
157a7 3b 0a 20 20 7d 0a 0a 20 20 74 65 73 74 63 61 73  ;.  }..  testcas
157a8 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d  e( nSize==pPage-
157a9 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74  >maxLocal );.  t
157aa 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d  estcase( nSize==
157ab 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b  pPage->maxLocal+
157ac 31 20 29 3b 0a 20 20 69 66 28 20 6e 53 69 7a 65  1 );.  if( nSize
157ad 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c  >pPage->maxLocal
157ae 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 4c   ){.    int minL
157af 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69  ocal = pPage->mi
157b0 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 6e 53 69 7a  nLocal;.    nSiz
157b1 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28  e = minLocal + (
157b2 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c 6f 63 61 6c  nSize - minLocal
157b3 29 20 25 20 28 70 50 61 67 65 2d 3e 70 42 74 2d  ) % (pPage->pBt-
157b4 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29  >usableSize - 4)
157b5 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
157b6 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61  nSize==pPage->ma
157b7 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 20 20 74 65  xLocal );.    te
157b8 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70  stcase( nSize==p
157b9 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31  Page->maxLocal+1
157ba 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 7a   );.    if( nSiz
157bb 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  e>pPage->maxLoca
157bc 6c 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65  l ){.      nSize
157bd 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20   = minLocal;.   
157be 20 7d 0a 20 20 20 20 6e 53 69 7a 65 20 2b 3d 20   }.    nSize += 
157bf 34 3b 0a 20 20 7d 0a 20 20 6e 53 69 7a 65 20 2b  4;.  }.  nSize +
157c0 3d 20 28 75 33 32 29 28 70 49 74 65 72 20 2d 20  = (u32)(pIter - 
157c1 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68  pCell);..  /* Th
157c2 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f  e minimum size o
157c3 66 20 61 6e 79 20 63 65 6c 6c 20 69 73 20 34 20  f any cell is 4 
157c4 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 66 28 20  bytes. */.  if( 
157c5 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 20 20 20 6e  nSize<4 ){.    n
157c6 53 69 7a 65 20 3d 20 34 3b 0a 20 20 7d 0a 0a 20  Size = 4;.  }.. 
157c7 20 61 73 73 65 72 74 28 20 6e 53 69 7a 65 3d 3d   assert( nSize==
157c8 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 7a 65 20  debuginfo.nSize 
157c9 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 75 31 36  );.  return (u16
157ca 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 64 65  )nSize;.}..#ifde
157cb 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
157cc 2a 20 54 68 69 73 20 76 61 72 69 61 74 69 6f 6e  * This variation
157cd 20 6f 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28   on cellSizePtr(
157ce 29 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65  ) is used inside
157cf 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61   of assert() sta
157d0 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 6c 79 2e  tements.** only.
157d1 20 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 63   */.static u16 c
157d2 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 61 67 65 20  ellSize(MemPage 
157d3 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c  *pPage, int iCel
157d4 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 65 6c  l){.  return cel
157d5 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20  lSizePtr(pPage, 
157d6 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
157d7 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 65 6e 64 69  iCell));.}.#endi
157d8 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
157d9 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
157da 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  M./*.** If the c
157db 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20  ell pCell, part 
157dc 6f 66 20 70 61 67 65 20 70 50 61 67 65 20 63 6f  of page pPage co
157dd 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
157de 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c  .** to an overfl
157df 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20  ow page, insert 
157e0 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68  an entry into th
157e1 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a  e pointer-map.**
157e2 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f   for the overflo
157e3 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69  w page..*/.stati
157e4 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75 74  c void ptrmapPut
157e5 4f 76 66 6c 50 74 72 28 4d 65 6d 50 61 67 65 20  OvflPtr(MemPage 
157e6 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c  *pPage, u8 *pCel
157e7 6c 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20  l, int *pRC){.  
157e8 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
157e9 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74 75   if( *pRC ) retu
157ea 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  rn;.  assert( pC
157eb 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 62 74 72 65  ell!=0 );.  btre
157ec 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
157ed 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
157ee 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69  o);.  assert( (i
157ef 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65  nfo.nData+(pPage
157f0 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e  ->intKey?0:info.
157f1 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61  nKey))==info.nPa
157f2 79 6c 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 69  yload );.  if( i
157f3 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b  nfo.iOverflow ){
157f4 0a 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d  .    Pgno ovfl =
157f5 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c   get4byte(&pCell
157f6 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
157f7 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74  );.    ptrmapPut
157f8 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66  (pPage->pBt, ovf
157f9 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  l, PTRMAP_OVERFL
157fa 4f 57 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  OW1, pPage->pgno
157fb 2c 20 70 52 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65  , pRC);.  }.}.#e
157fc 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  ndif.../*.** Def
157fd 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65  ragment the page
157fe 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c   given.  All Cel
157ff 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20  ls are moved to 
15800 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68  the.** end of th
15801 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 66  e page and all f
15802 72 65 65 20 73 70 61 63 65 20 69 73 20 63 6f 6c  ree space is col
15803 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a  lected into one.
15804 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b 20 74  ** big FreeBlk t
15805 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 62 65  hat occurs in be
15806 74 77 65 65 6e 20 74 68 65 20 68 65 61 64 65 72  tween the header
15807 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69   and cell.** poi
15808 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64 20 74  nter array and t
15809 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
1580a 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  area..*/.static 
1580b 69 6e 74 20 64 65 66 72 61 67 6d 65 6e 74 50 61  int defragmentPa
1580c 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
1580d 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  e){.  int i;    
1580e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1580f 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
15810 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20   */.  int pc;   
15811 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15812 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61   /* Address of a
15813 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20   i-th cell */.  
15814 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20  int hdr;        
15815 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
15816 66 73 65 74 20 74 6f 20 74 68 65 20 70 61 67 65  fset to the page
15817 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74   header */.  int
15818 20 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20   size;          
15819 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
1581a 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 69  of a cell */.  i
1581b 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20  nt usableSize;  
1581c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1581d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79  ber of usable by
1581e 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f  tes on a page */
1581f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65  .  int cellOffse
15820 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
15821 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 63   Offset to the c
15822 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
15823 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b  y */.  int cbrk;
15824 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15825 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74    /* Offset to t
15826 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
15827 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  area */.  int nC
15828 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
15829 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1582a 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70  f cells on the p
1582b 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  age */.  unsigne
1582c 64 20 63 68 61 72 20 2a 64 61 74 61 3b 20 20 20  d char *data;   
1582d 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20      /* The page 
1582e 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  data */.  unsign
1582f 65 64 20 63 68 61 72 20 2a 74 65 6d 70 3b 20 20  ed char *temp;  
15830 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 61 72 65       /* Temp are
15831 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 65  a for cell conte
15832 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c  nt */.  int iCel
15833 6c 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20  lFirst;         
15834 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f     /* First allo
15835 77 61 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78  wable cell index
15836 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 4c   */.  int iCellL
15837 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ast;            
15838 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 6c   /* Last possibl
15839 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a  e cell index */.
1583a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1583b 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
1583c 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
1583d 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
1583e 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29   pPage->pBt!=0 )
1583f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
15840 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
15841 7a 65 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58  ze <= SQLITE_MAX
15842 5f 50 41 47 45 5f 53 49 5a 45 20 29 3b 0a 20 20  _PAGE_SIZE );.  
15843 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
15844 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20  Overflow==0 );. 
15845 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15846 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
15847 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
15848 3b 0a 20 20 74 65 6d 70 20 3d 20 73 71 6c 69 74  ;.  temp = sqlit
15849 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65  e3PagerTempSpace
1584a 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 50 61  (pPage->pBt->pPa
1584b 67 65 72 29 3b 0a 20 20 64 61 74 61 20 3d 20 70  ger);.  data = p
1584c 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68  Page->aData;.  h
1584d 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
1584e 66 66 73 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66  ffset;.  cellOff
1584f 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c  set = pPage->cel
15850 6c 4f 66 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c  lOffset;.  nCell
15851 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   = pPage->nCell;
15852 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c  .  assert( nCell
15853 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61  ==get2byte(&data
15854 5b 68 64 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73  [hdr+3]) );.  us
15855 61 62 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65  ableSize = pPage
15856 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
15857 65 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32  e;.  cbrk = get2
15858 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
15859 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 74 65  ]);.  memcpy(&te
1585a 6d 70 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b  mp[cbrk], &data[
1585b 63 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a  cbrk], usableSiz
1585c 65 20 2d 20 63 62 72 6b 29 3b 0a 20 20 63 62 72  e - cbrk);.  cbr
1585d 6b 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a  k = usableSize;.
1585e 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63    iCellFirst = c
1585f 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43  ellOffset + 2*nC
15860 65 6c 6c 3b 0a 20 20 69 43 65 6c 6c 4c 61 73 74  ell;.  iCellLast
15861 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20   = usableSize - 
15862 34 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  4;.  for(i=0; i<
15863 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
15864 20 75 38 20 2a 70 41 64 64 72 3b 20 20 20 20 20   u8 *pAddr;     
15865 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 65 6c 6c  /* The i-th cell
15866 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20   pointer */.    
15867 70 41 64 64 72 20 3d 20 26 64 61 74 61 5b 63 65  pAddr = &data[ce
15868 6c 6c 4f 66 66 73 65 74 20 2b 20 69 2a 32 5d 3b  llOffset + i*2];
15869 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79  .    pc = get2by
1586a 74 65 28 70 41 64 64 72 29 3b 0a 20 20 20 20 74  te(pAddr);.    t
1586b 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65  estcase( pc==iCe
1586c 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74  llFirst );.    t
1586d 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65  estcase( pc==iCe
1586e 6c 6c 4c 61 73 74 20 29 3b 0a 23 69 66 20 21 64  llLast );.#if !d
1586f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
15870 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45  ABLE_OVERSIZE_CE
15871 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 2f 2a  LL_CHECK).    /*
15872 20 54 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e   These condition
15873 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  s have already b
15874 65 65 6e 20 76 65 72 69 66 69 65 64 20 69 6e 20  een verified in 
15875 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 0a  btreeInitPage().
15876 20 20 20 20 2a 2a 20 69 66 20 53 51 4c 49 54 45      ** if SQLITE
15877 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45  _ENABLE_OVERSIZE
15878 5f 43 45 4c 4c 5f 43 48 45 43 4b 20 69 73 20 64  _CELL_CHECK is d
15879 65 66 69 6e 65 64 20 0a 20 20 20 20 2a 2f 0a 20  efined .    */. 
1587a 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46     if( pc<iCellF
1587b 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c  irst || pc>iCell
1587c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 72 65  Last ){.      re
1587d 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1587e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
1587f 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72  #endif.    asser
15880 74 28 20 70 63 3e 3d 69 43 65 6c 6c 46 69 72 73  t( pc>=iCellFirs
15881 74 20 26 26 20 70 63 3c 3d 69 43 65 6c 6c 4c 61  t && pc<=iCellLa
15882 73 74 20 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d  st );.    size =
15883 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
15884 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a  ge, &temp[pc]);.
15885 20 20 20 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65      cbrk -= size
15886 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ;.#if defined(SQ
15887 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52  LITE_ENABLE_OVER
15888 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29  SIZE_CELL_CHECK)
15889 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 69 43  .    if( cbrk<iC
1588a 65 6c 6c 46 69 72 73 74 20 29 7b 0a 20 20 20 20  ellFirst ){.    
1588b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1588c 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1588d 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 69 66    }.#else.    if
1588e 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73  ( cbrk<iCellFirs
1588f 74 20 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61  t || pc+size>usa
15890 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  bleSize ){.     
15891 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15892 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
15893 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73   }.#endif.    as
15894 73 65 72 74 28 20 63 62 72 6b 2b 73 69 7a 65 3c  sert( cbrk+size<
15895 3d 75 73 61 62 6c 65 53 69 7a 65 20 26 26 20 63  =usableSize && c
15896 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20  brk>=iCellFirst 
15897 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
15898 20 63 62 72 6b 2b 73 69 7a 65 3d 3d 75 73 61 62   cbrk+size==usab
15899 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 74 65  leSize );.    te
1589a 73 74 63 61 73 65 28 20 70 63 2b 73 69 7a 65 3d  stcase( pc+size=
1589b 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20  =usableSize );. 
1589c 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b     memcpy(&data[
1589d 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d  cbrk], &temp[pc]
1589e 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74  , size);.    put
1589f 32 62 79 74 65 28 70 41 64 64 72 2c 20 63 62 72  2byte(pAddr, cbr
158a0 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  k);.  }.  assert
158a1 28 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72  ( cbrk>=iCellFir
158a2 73 74 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65  st );.  put2byte
158a3 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63  (&data[hdr+5], c
158a4 62 72 6b 29 3b 0a 20 20 64 61 74 61 5b 68 64 72  brk);.  data[hdr
158a5 2b 31 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b  +1] = 0;.  data[
158a6 68 64 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61  hdr+2] = 0;.  da
158a7 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20  ta[hdr+7] = 0;. 
158a8 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 69 43   memset(&data[iC
158a9 65 6c 6c 46 69 72 73 74 5d 2c 20 30 2c 20 63 62  ellFirst], 0, cb
158aa 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a  rk-iCellFirst);.
158ab 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
158ac 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
158ad 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
158ae 29 20 29 3b 0a 20 20 69 66 28 20 63 62 72 6b 2d  ) );.  if( cbrk-
158af 69 43 65 6c 6c 46 69 72 73 74 21 3d 70 50 61 67  iCellFirst!=pPag
158b0 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 20  e->nFree ){.    
158b1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
158b2 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a  RRUPT_BKPT;.  }.
158b3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
158b4 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  OK;.}../*.** All
158b5 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65  ocate nByte byte
158b6 73 20 6f 66 20 73 70 61 63 65 20 66 72 6f 6d 20  s of space from 
158b7 77 69 74 68 69 6e 20 74 68 65 20 42 2d 54 72 65  within the B-Tre
158b8 65 20 70 61 67 65 20 70 61 73 73 65 64 0a 2a 2a  e page passed.**
158b9 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
158ba 67 75 6d 65 6e 74 2e 20 57 72 69 74 65 20 69 6e  gument. Write in
158bb 74 6f 20 2a 70 49 64 78 20 74 68 65 20 69 6e 64  to *pIdx the ind
158bc 65 78 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61  ex into pPage->a
158bd 44 61 74 61 5b 5d 0a 2a 2a 20 6f 66 20 74 68 65  Data[].** of the
158be 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 61   first byte of a
158bf 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20  llocated space. 
158c0 52 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51  Return either SQ
158c1 4c 49 54 45 5f 4f 4b 20 6f 72 0a 2a 2a 20 61 6e  LITE_OK or.** an
158c2 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75   error code (usu
158c3 61 6c 6c 79 20 53 51 4c 49 54 45 5f 43 4f 52 52  ally SQLITE_CORR
158c4 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  UPT)..**.** The 
158c5 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65  caller guarantee
158c6 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20  s that there is 
158c7 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65  sufficient space
158c8 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20   to make the.** 
158c9 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69  allocation.  Thi
158ca 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20  s routine might 
158cb 6e 65 65 64 20 74 6f 20 64 65 66 72 61 67 6d 65  need to defragme
158cc 6e 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62  nt in order to b
158cd 72 69 6e 67 0a 2a 2a 20 61 6c 6c 20 74 68 65 20  ring.** all the 
158ce 73 70 61 63 65 20 74 6f 67 65 74 68 65 72 2c 20  space together, 
158cf 68 6f 77 65 76 65 72 2e 20 20 54 68 69 73 20 72  however.  This r
158d0 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 76 6f 69  outine will avoi
158d1 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66  d using.** the f
158d2 69 72 73 74 20 74 77 6f 20 62 79 74 65 73 20 70  irst two bytes p
158d3 61 73 74 20 74 68 65 20 63 65 6c 6c 20 70 6f 69  ast the cell poi
158d4 6e 74 65 72 20 61 72 65 61 20 73 69 6e 63 65 20  nter area since 
158d5 70 72 65 73 75 6d 61 62 6c 79 20 74 68 69 73 0a  presumably this.
158d6 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  ** allocation is
158d7 20 62 65 69 6e 67 20 6d 61 64 65 20 69 6e 20 6f   being made in o
158d8 72 64 65 72 20 74 6f 20 69 6e 73 65 72 74 20 61  rder to insert a
158d9 20 6e 65 77 20 63 65 6c 6c 2c 20 73 6f 20 77 65   new cell, so we
158da 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 65 6e   will.** also en
158db 64 20 75 70 20 6e 65 65 64 69 6e 67 20 61 20 6e  d up needing a n
158dc 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e  ew cell pointer.
158dd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
158de 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 4d 65 6d  llocateSpace(Mem
158df 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74  Page *pPage, int
158e0 20 6e 42 79 74 65 2c 20 69 6e 74 20 2a 70 49 64   nByte, int *pId
158e1 78 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  x){.  const int 
158e2 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
158e3 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4c 6f  Offset;    /* Lo
158e4 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50 61  cal cache of pPa
158e5 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 2a 2f  ge->hdrOffset */
158e6 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 64 61  .  u8 * const da
158e7 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
158e8 61 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c  a;      /* Local
158e9 20 63 61 63 68 65 20 6f 66 20 70 50 61 67 65 2d   cache of pPage-
158ea 3e 61 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  >aData */.  int 
158eb 6e 46 72 61 67 3b 20 20 20 20 20 20 20 20 20 20  nFrag;          
158ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
158ed 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72   /* Number of fr
158ee 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 20 6f  agmented bytes o
158ef 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74  n pPage */.  int
158f0 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20   top;           
158f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
158f2 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
158f3 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  of cell content 
158f4 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20 67 61  area */.  int ga
158f5 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72  p;        /* Fir
158f6 73 74 20 62 79 74 65 20 6f 66 20 67 61 70 20 62  st byte of gap b
158f7 65 74 77 65 65 6e 20 63 65 6c 6c 20 70 6f 69 6e  etween cell poin
158f8 74 65 72 73 20 61 6e 64 20 63 65 6c 6c 20 63 6f  ters and cell co
158f9 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72  ntent */.  int r
158fa 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  c;         /* In
158fb 74 65 67 65 72 20 72 65 74 75 72 6e 20 63 6f 64  teger return cod
158fc 65 20 2a 2f 0a 20 20 0a 20 20 61 73 73 65 72 74  e */.  .  assert
158fd 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
158fe 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
158ff 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61  >pDbPage) );.  a
15900 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42  ssert( pPage->pB
15901 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  t );.  assert( s
15902 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15903 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
15904 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
15905 28 20 6e 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f  ( nByte>=0 );  /
15906 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73  * Minimum cell s
15907 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 61 73  ize is 4 */.  as
15908 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72  sert( pPage->nFr
15909 65 65 3e 3d 6e 42 79 74 65 20 29 3b 0a 20 20 61  ee>=nByte );.  a
1590a 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f  ssert( pPage->nO
1590b 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20  verflow==0 );.  
1590c 61 73 73 65 72 74 28 20 6e 42 79 74 65 3c 70 50  assert( nByte<pP
1590d 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
1590e 53 69 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e 46 72  Size-8 );..  nFr
1590f 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d  ag = data[hdr+7]
15910 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
15911 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 3d  e->cellOffset ==
15912 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50   hdr + 12 - 4*pP
15913 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 67  age->leaf );.  g
15914 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c  ap = pPage->cell
15915 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65  Offset + 2*pPage
15916 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 20 3d  ->nCell;.  top =
15917 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15918 68 64 72 2b 35 5d 29 3b 0a 20 20 69 66 28 20 67  hdr+5]);.  if( g
15919 61 70 3e 74 6f 70 20 29 20 72 65 74 75 72 6e 20  ap>top ) return 
1591a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1591b 4b 50 54 3b 0a 20 20 74 65 73 74 63 61 73 65 28  KPT;.  testcase(
1591c 20 67 61 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20   gap+2==top );. 
1591d 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b 31   testcase( gap+1
1591e 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63  ==top );.  testc
1591f 61 73 65 28 20 67 61 70 3d 3d 74 6f 70 20 29 3b  ase( gap==top );
15920 0a 0a 20 20 69 66 28 20 6e 46 72 61 67 3e 3d 36  ..  if( nFrag>=6
15921 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 61  0 ){.    /* Alwa
15922 79 73 20 64 65 66 72 61 67 6d 65 6e 74 20 68 69  ys defragment hi
15923 67 68 6c 79 20 66 72 61 67 6d 65 6e 74 65 64 20  ghly fragmented 
15924 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 72 63 20  pages */.    rc 
15925 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65  = defragmentPage
15926 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  (pPage);.    if(
15927 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
15928 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62  .    top = get2b
15929 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
1592a 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67  );.  }else if( g
1592b 61 70 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20  ap+2<=top ){.   
1592c 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 66   /* Search the f
1592d 72 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20  reelist looking 
1592e 66 6f 72 20 61 20 66 72 65 65 20 73 6c 6f 74 20  for a free slot 
1592f 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61  big enough to sa
15930 74 69 73 66 79 20 0a 20 20 20 20 2a 2a 20 74 68  tisfy .    ** th
15931 65 20 72 65 71 75 65 73 74 2e 20 54 68 65 20 61  e request. The a
15932 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61 64  llocation is mad
15933 65 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74  e from the first
15934 20 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 0a 20   free slot in . 
15935 20 20 20 2a 2a 20 74 68 65 20 6c 69 73 74 20 74     ** the list t
15936 68 61 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f  hat is large eno
15937 75 67 68 20 74 6f 20 61 63 63 6f 6d 61 64 61 74  ugh to accomadat
15938 65 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  e it..    */.   
15939 20 69 6e 74 20 70 63 2c 20 61 64 64 72 3b 0a 20   int pc, addr;. 
1593a 20 20 20 66 6f 72 28 61 64 64 72 3d 68 64 72 2b     for(addr=hdr+
1593b 31 3b 20 28 70 63 20 3d 20 67 65 74 32 62 79 74  1; (pc = get2byt
1593c 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e  e(&data[addr]))>
1593d 30 3b 20 61 64 64 72 3d 70 63 29 7b 0a 20 20 20  0; addr=pc){.   
1593e 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 67 65     int size = ge
1593f 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b  t2byte(&data[pc+
15940 32 5d 29 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65  2]);     /* Size
15941 20 6f 66 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f   of free slot */
15942 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 3e  .      if( size>
15943 3d 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20  =nByte ){.      
15944 20 20 69 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d    int x = size -
15945 20 6e 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20   nByte;.        
15946 74 65 73 74 63 61 73 65 28 20 78 3d 3d 34 20 29  testcase( x==4 )
15947 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
15948 73 65 28 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20  se( x==3 );.    
15949 20 20 20 20 69 66 28 20 78 3c 34 20 29 7b 0a 20      if( x<4 ){. 
1594a 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f           /* Remo
1594b 76 65 20 74 68 65 20 73 6c 6f 74 20 66 72 6f 6d  ve the slot from
1594c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20   the free-list. 
1594d 55 70 64 61 74 65 20 74 68 65 20 6e 75 6d 62 65  Update the numbe
1594e 72 20 6f 66 0a 20 20 20 20 20 20 20 20 20 20 2a  r of.          *
1594f 2a 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74  * fragmented byt
15950 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 70 61  es within the pa
15951 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ge. */.         
15952 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 61 64   memcpy(&data[ad
15953 64 72 5d 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20  dr], &data[pc], 
15954 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 61  2);.          da
15955 74 61 5b 68 64 72 2b 37 5d 20 3d 20 28 75 38 29  ta[hdr+7] = (u8)
15956 28 6e 46 72 61 67 20 2b 20 78 29 3b 0a 20 20 20  (nFrag + x);.   
15957 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
15958 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 6c 6f        /* The slo
15959 74 20 72 65 6d 61 69 6e 73 20 6f 6e 20 74 68 65  t remains on the
1595a 20 66 72 65 65 2d 6c 69 73 74 2e 20 52 65 64 75   free-list. Redu
1595b 63 65 20 69 74 73 20 73 69 7a 65 20 74 6f 20 61  ce its size to a
1595c 63 63 6f 75 6e 74 0a 20 20 20 20 20 20 20 20 20  ccount.         
1595d 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 6f 72 74   ** for the port
1595e 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 20  ion used by the 
1595f 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  new allocation. 
15960 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 75 74  */.          put
15961 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32  2byte(&data[pc+2
15962 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d  ], x);.        }
15963 0a 20 20 20 20 20 20 20 20 2a 70 49 64 78 20 3d  .        *pIdx =
15964 20 70 63 20 2b 20 78 3b 0a 20 20 20 20 20 20 20   pc + x;.       
15965 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
15966 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  K;.      }.    }
15967 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
15968 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
15969 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70  ere is enough sp
1596a 61 63 65 20 69 6e 20 74 68 65 20 67 61 70 20 74  ace in the gap t
1596b 6f 20 73 61 74 69 73 66 79 0a 20 20 2a 2a 20 74  o satisfy.  ** t
1596c 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20  he allocation.  
1596d 49 66 20 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65  If not, defragme
1596e 6e 74 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63  nt..  */.  testc
1596f 61 73 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 65  ase( gap+2+nByte
15970 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 67  ==top );.  if( g
15971 61 70 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29  ap+2+nByte>top )
15972 7b 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61  {.    rc = defra
15973 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29  gmentPage(pPage)
15974 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
15975 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f  eturn rc;.    to
15976 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  p = get2byte(&da
15977 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20  ta[hdr+5]);.    
15978 61 73 73 65 72 74 28 20 67 61 70 2b 6e 42 79 74  assert( gap+nByt
15979 65 3c 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a  e<=top );.  }...
1597a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65    /* Allocate me
1597b 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61  mory from the ga
1597c 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65  p in between the
1597d 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
1597e 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  ray.  ** and the
1597f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
15980 65 61 2e 20 20 54 68 65 20 62 74 72 65 65 49 6e  ea.  The btreeIn
15981 69 74 50 61 67 65 28 29 20 63 61 6c 6c 20 68 61  itPage() call ha
15982 73 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76  s already.  ** v
15983 61 6c 69 64 61 74 65 64 20 74 68 65 20 66 72 65  alidated the fre
15984 65 6c 69 73 74 2e 20 20 47 69 76 65 6e 20 74 68  elist.  Given th
15985 61 74 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  at the freelist 
15986 69 73 20 76 61 6c 69 64 2c 20 74 68 65 72 65 0a  is valid, there.
15987 20 20 2a 2a 20 69 73 20 6e 6f 20 77 61 79 20 74    ** is no way t
15988 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  hat the allocati
15989 6f 6e 20 63 61 6e 20 65 78 74 65 6e 64 20 6f 66  on can extend of
1598a 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
1598b 20 70 61 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20   page..  ** The 
1598c 61 73 73 65 72 74 28 29 20 62 65 6c 6f 77 20 76  assert() below v
1598d 65 72 69 66 69 65 73 20 74 68 65 20 70 72 65 76  erifies the prev
1598e 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20  ious sentence.. 
1598f 20 2a 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79   */.  top -= nBy
15990 74 65 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26  te;.  put2byte(&
15991 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70  data[hdr+5], top
15992 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 6f 70  );.  assert( top
15993 2b 6e 42 79 74 65 20 3c 3d 20 70 50 61 67 65 2d  +nByte <= pPage-
15994 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
15995 20 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f   );.  *pIdx = to
15996 70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  p;.  return SQLI
15997 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
15998 52 65 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e  Return a section
15999 20 6f 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61   of the pPage->a
1599a 44 61 74 61 20 74 6f 20 74 68 65 20 66 72 65 65  Data to the free
1599b 6c 69 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72  list..** The fir
1599c 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e  st byte of the n
1599d 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73  ew free block is
1599e 20 70 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74   pPage->aDisk[st
1599f 61 72 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20  art].** and the 
159a0 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63  size of the bloc
159a1 6b 20 69 73 20 22 73 69 7a 65 22 20 62 79 74 65  k is "size" byte
159a2 73 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66  s..**.** Most of
159a3 20 74 68 65 20 65 66 66 6f 72 74 20 68 65 72 65   the effort here
159a4 20 69 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20   is involved in 
159a5 63 6f 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 65  coalesing adjace
159a6 6e 74 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b  nt.** free block
159a7 73 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  s into a single 
159a8 62 69 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a  big free block..
159a9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72  */.static int fr
159aa 65 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20  eeSpace(MemPage 
159ab 2a 70 50 61 67 65 2c 20 69 6e 74 20 73 74 61 72  *pPage, int star
159ac 74 2c 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20  t, int size){.  
159ad 69 6e 74 20 61 64 64 72 2c 20 70 62 65 67 69 6e  int addr, pbegin
159ae 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 69 4c 61  , hdr;.  int iLa
159af 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
159b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
159b1 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 66  rgest possible f
159b2 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20  reeblock offset 
159b3 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
159b4 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65  ar *data = pPage
159b5 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 65  ->aData;..  asse
159b6 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d  rt( pPage->pBt!=
159b7 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
159b8 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
159b9 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
159ba 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
159bb 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65  rt( start>=pPage
159bc 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50  ->hdrOffset+6+pP
159bd 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a  age->childPtrSiz
159be 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  e );.  assert( (
159bf 73 74 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70  start + size)<=p
159c0 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
159c1 65 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72  eSize );.  asser
159c2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
159c3 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
159c4 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
159c5 73 65 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b  sert( size>=0 );
159c6 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65     /* Minimum ce
159c7 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a  ll size is 4 */.
159c8 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
159c9 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f  ECURE_DELETE.  /
159ca 2a 20 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65  * Overwrite dele
159cb 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ted information 
159cc 77 69 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20  with zeros when 
159cd 74 68 65 20 53 45 43 55 52 45 5f 44 45 4c 45 54  the SECURE_DELET
159ce 45 20 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69  E .  ** option i
159cf 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d  s enabled at com
159d0 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d  pile-time */.  m
159d1 65 6d 73 65 74 28 26 64 61 74 61 5b 73 74 61 72  emset(&data[star
159d2 74 5d 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65  t], 0, size);.#e
159d3 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74  ndif..  /* Add t
159d4 68 65 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e  he space back in
159d5 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69  to the linked li
159d6 73 74 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73  st of freeblocks
159d7 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a  .  Note that.  *
159d8 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68  * even though th
159d9 65 20 66 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74  e freeblock list
159da 20 77 61 73 20 63 68 65 63 6b 65 64 20 62 79 20   was checked by 
159db 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2c  btreeInitPage(),
159dc 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50  .  ** btreeInitP
159dd 61 67 65 28 29 20 64 69 64 20 6e 6f 74 20 64 65  age() did not de
159de 74 65 63 74 20 6f 76 65 72 6c 61 70 70 69 6e 67  tect overlapping
159df 20 63 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66   cells or.  ** f
159e0 72 65 65 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f  reeblocks that o
159e1 76 65 72 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e  verlapped cells.
159e2 20 20 20 4e 6f 72 20 64 6f 65 73 20 69 74 20 64     Nor does it d
159e3 65 74 65 63 74 20 77 68 65 6e 20 74 68 65 0a 20  etect when the. 
159e4 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74   ** cell content
159e5 20 61 72 65 61 20 65 78 63 65 65 64 73 20 74 68   area exceeds th
159e6 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 70  e value in the p
159e7 61 67 65 20 68 65 61 64 65 72 2e 20 20 49 66 20  age header.  If 
159e8 74 68 65 73 65 0a 20 20 2a 2a 20 73 69 74 75 61  these.  ** situa
159e9 74 69 6f 6e 73 20 61 72 69 73 65 2c 20 74 68 65  tions arise, the
159ea 6e 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73  n subsequent ins
159eb 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d  ert operations m
159ec 69 67 68 74 20 63 6f 72 72 75 70 74 0a 20 20 2a  ight corrupt.  *
159ed 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20  * the freelist. 
159ee 20 53 6f 20 77 65 20 64 6f 20 6e 65 65 64 20 74   So we do need t
159ef 6f 20 63 68 65 63 6b 20 66 6f 72 20 63 6f 72 72  o check for corr
159f0 75 70 74 69 6f 6e 20 77 68 69 6c 65 20 73 63 61  uption while sca
159f1 6e 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66  nning.  ** the f
159f2 72 65 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20  reelist..  */.  
159f3 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
159f4 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d  Offset;.  addr =
159f5 20 68 64 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73   hdr + 1;.  iLas
159f6 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e  t = pPage->pBt->
159f7 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a  usableSize - 4;.
159f8 20 20 61 73 73 65 72 74 28 20 73 74 61 72 74 3c    assert( start<
159f9 3d 69 4c 61 73 74 20 29 3b 0a 20 20 77 68 69 6c  =iLast );.  whil
159fa 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74  e( (pbegin = get
159fb 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72  2byte(&data[addr
159fc 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 62 65  ]))<start && pbe
159fd 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28  gin>0 ){.    if(
159fe 20 70 62 65 67 69 6e 3c 61 64 64 72 2b 34 20 29   pbegin<addr+4 )
159ff 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
15a00 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15a01 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64  PT;.    }.    ad
15a02 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d  dr = pbegin;.  }
15a03 0a 20 20 69 66 28 20 70 62 65 67 69 6e 3e 69 4c  .  if( pbegin>iL
15a04 61 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ast ){.    retur
15a05 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
15a06 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73  _BKPT;.  }.  ass
15a07 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72  ert( pbegin>addr
15a08 20 7c 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b   || pbegin==0 );
15a09 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
15a0a 61 5b 61 64 64 72 5d 2c 20 73 74 61 72 74 29 3b  a[addr], start);
15a0b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
15a0c 61 5b 73 74 61 72 74 5d 2c 20 70 62 65 67 69 6e  a[start], pbegin
15a0d 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64  );.  put2byte(&d
15a0e 61 74 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69  ata[start+2], si
15a0f 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46  ze);.  pPage->nF
15a10 72 65 65 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72  ree = pPage->nFr
15a11 65 65 20 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a  ee + (u16)size;.
15a12 0a 20 20 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61  .  /* Coalesce a
15a13 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62 6c 6f  djacent free blo
15a14 63 6b 73 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20  cks */.  addr = 
15a15 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65  hdr + 1;.  while
15a16 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32  ( (pbegin = get2
15a17 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d  byte(&data[addr]
15a18 29 29 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  ))>0 ){.    int 
15a19 70 6e 65 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b  pnext, psize, x;
15a1a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65  .    assert( pbe
15a1b 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 20 20 20  gin>addr );.    
15a1c 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d  assert( pbegin<=
15a1d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
15a1e 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20  leSize-4 );.    
15a1f 70 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65  pnext = get2byte
15a20 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b  (&data[pbegin]);
15a21 0a 20 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74  .    psize = get
15a22 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67  2byte(&data[pbeg
15a23 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20  in+2]);.    if( 
15a24 70 62 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b  pbegin + psize +
15a25 20 33 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70   3 >= pnext && p
15a26 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20  next>0 ){.      
15a27 69 6e 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74  int frag = pnext
15a28 20 2d 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65   - (pbegin+psize
15a29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 66 72  );.      if( (fr
15a2a 61 67 3c 30 29 20 7c 7c 20 28 66 72 61 67 3e 28  ag<0) || (frag>(
15a2b 69 6e 74 29 64 61 74 61 5b 68 64 72 2b 37 5d 29  int)data[hdr+7])
15a2c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
15a2d 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
15a2e 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a  T_BKPT;.      }.
15a2f 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37        data[hdr+7
15a30 5d 20 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20  ] -= (u8)frag;. 
15a31 20 20 20 20 20 78 20 3d 20 67 65 74 32 62 79 74       x = get2byt
15a32 65 28 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b  e(&data[pnext]);
15a33 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28  .      put2byte(
15a34 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78  &data[pbegin], x
15a35 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65  );.      x = pne
15a36 78 74 20 2b 20 67 65 74 32 62 79 74 65 28 26 64  xt + get2byte(&d
15a37 61 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20  ata[pnext+2]) - 
15a38 70 62 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75  pbegin;.      pu
15a39 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65  t2byte(&data[pbe
15a3a 67 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20  gin+2], x);.    
15a3b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64  }else{.      add
15a3c 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20  r = pbegin;.    
15a3d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
15a3e 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
15a3f 61 72 65 61 20 62 65 67 69 6e 73 20 77 69 74 68  area begins with
15a40 20 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65   a freeblock, re
15a41 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66  move it. */.  if
15a42 28 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64  ( data[hdr+1]==d
15a43 61 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61  ata[hdr+5] && da
15a44 74 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b  ta[hdr+2]==data[
15a45 68 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e  hdr+6] ){.    in
15a46 74 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69  t top;.    pbegi
15a47 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  n = get2byte(&da
15a48 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20  ta[hdr+1]);.    
15a49 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72  memcpy(&data[hdr
15a4a 2b 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69  +1], &data[pbegi
15a4b 6e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20  n], 2);.    top 
15a4c 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
15a4d 5b 68 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62  [hdr+5]) + get2b
15a4e 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
15a4f 2b 32 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79  +2]);.    put2by
15a50 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c  te(&data[hdr+5],
15a51 20 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73   top);.  }.  ass
15a52 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
15a53 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
15a54 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
15a55 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15a56 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63  OK;.}../*.** Dec
15a57 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20 62 79  ode the flags by
15a58 74 65 20 28 74 68 65 20 66 69 72 73 74 20 62 79  te (the first by
15a59 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  te of the header
15a5a 29 20 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20  ) for a page.** 
15a5b 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66  and initialize f
15a5c 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d  ields of the Mem
15a5d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 20 61  Page structure a
15a5e 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a  ccordingly..**.*
15a5f 2a 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f  * Only the follo
15a60 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  wing combination
15a61 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e  s are supported.
15a62 20 20 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65    Anything diffe
15a63 72 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65  rent.** indicate
15a64 73 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61  s a corrupt data
15a65 62 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a  base files:.**.*
15a66 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45  *         PTF_ZE
15a67 52 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20  RODATA.**       
15a68 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c    PTF_ZERODATA |
15a69 20 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20   PTF_LEAF.**    
15a6a 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54       PTF_LEAFDAT
15a6b 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a  A | PTF_INTKEY.*
15a6c 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45  *         PTF_LE
15a6d 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54  AFDATA | PTF_INT
15a6e 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a  KEY | PTF_LEAF.*
15a6f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 63  /.static int dec
15a70 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65  odeFlags(MemPage
15a71 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61   *pPage, int fla
15a72 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72  gByte){.  BtShar
15a73 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20  ed *pBt;     /* 
15a74 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d  A copy of pPage-
15a75 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72  >pBt */..  asser
15a76 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  t( pPage->hdrOff
15a77 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e  set==(pPage->pgn
15a78 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20  o==1 ? 100 : 0) 
15a79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
15a7a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15a7b 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
15a7c 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c  x) );.  pPage->l
15a7d 65 61 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42  eaf = (u8)(flagB
15a7e 79 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74  yte>>3);  assert
15a7f 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c  ( PTF_LEAF == 1<
15a80 3c 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65  <3 );.  flagByte
15a81 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20   &= ~PTF_LEAF;. 
15a82 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72   pPage->childPtr
15a83 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65  Size = 4-4*pPage
15a84 2d 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20  ->leaf;.  pBt = 
15a85 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66  pPage->pBt;.  if
15a86 28 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46  ( flagByte==(PTF
15a87 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f  _LEAFDATA | PTF_
15a88 49 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70  INTKEY) ){.    p
15a89 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31  Page->intKey = 1
15a8a 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73  ;.    pPage->has
15a8b 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65  Data = pPage->le
15a8c 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d  af;.    pPage->m
15a8d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d  axLocal = pBt->m
15a8e 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67  axLeaf;.    pPag
15a8f 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42  e->minLocal = pB
15a90 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65  t->minLeaf;.  }e
15a91 6c 73 65 20 69 66 28 20 66 6c 61 67 42 79 74 65  lse if( flagByte
15a92 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29  ==PTF_ZERODATA )
15a93 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74  {.    pPage->int
15a94 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  Key = 0;.    pPa
15a95 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b  ge->hasData = 0;
15a96 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c  .    pPage->maxL
15a97 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c  ocal = pBt->maxL
15a98 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d  ocal;.    pPage-
15a99 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d  >minLocal = pBt-
15a9a 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c  >minLocal;.  }el
15a9b 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  se{.    return S
15a9c 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15a9d 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  PT;.  }.  return
15a9e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
15a9f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
15aa0 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e  the auxiliary in
15aa1 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20  formation for a 
15aa2 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  disk block..**.*
15aa3 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
15aa4 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  OK on success.  
15aa5 49 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74  If we see that t
15aa6 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20  he page does.** 
15aa7 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65  not contain a we
15aa8 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61  ll-formed databa
15aa9 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65  se page, then re
15aaa 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  turn .** SQLITE_
15aab 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74  CORRUPT.  Note t
15aac 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20  hat a return of 
15aad 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e  SQLITE_OK does n
15aae 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20  ot.** guarantee 
15aaf 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73  that the page is
15ab0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49   well-formed.  I
15ab1 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61  t only shows tha
15ab2 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74  t.** we failed t
15ab3 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72  o detect any cor
15ab4 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  ruption..*/.stat
15ab5 69 63 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74  ic int btreeInit
15ab6 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
15ab7 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28  age){..  assert(
15ab8 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29   pPage->pBt!=0 )
15ab9 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
15aba 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
15abb 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
15abc 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
15abd 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69  Page->pgno==sqli
15abe 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
15abf 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  er(pPage->pDbPag
15ac0 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
15ac1 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33  pPage == sqlite3
15ac2 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50  PagerGetExtra(pP
15ac3 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
15ac4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
15ac5 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74  ->aData == sqlit
15ac6 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
15ac7 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
15ac8 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d  ;..  if( !pPage-
15ac9 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75  >isInit ){.    u
15aca 31 36 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  16 pc;          
15acb 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
15acc 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68  a freeblock with
15acd 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  in pPage->aData[
15ace 5d 20 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b  ] */.    u8 hdr;
15acf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
15ad0 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69  ffset to beginni
15ad1 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61 64 65  ng of page heade
15ad2 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74  r */.    u8 *dat
15ad3 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  a;          /* E
15ad4 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61  qual to pPage->a
15ad5 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68  Data */.    BtSh
15ad6 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
15ad7 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74    /* The main bt
15ad8 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ree structure */
15ad9 0a 20 20 20 20 75 31 36 20 75 73 61 62 6c 65 53  .    u16 usableS
15ada 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e  ize;    /* Amoun
15adb 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63  t of usable spac
15adc 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a  e on each page *
15add 2f 0a 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66  /.    u16 cellOf
15ade 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73  fset;    /* Offs
15adf 65 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66  et from start of
15ae0 20 70 61 67 65 20 74 6f 20 66 69 72 73 74 20 63   page to first c
15ae1 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ell pointer */. 
15ae2 20 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20     u16 nFree;   
15ae3 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
15ae4 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20  of unused bytes 
15ae5 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20  on the page */. 
15ae6 20 20 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20     u16 top;     
15ae7 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62        /* First b
15ae8 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  yte of the cell 
15ae9 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a  content area */.
15aea 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72      int iCellFir
15aeb 73 74 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20  st;    /* First 
15aec 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f  allowable cell o
15aed 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73  r freeblock offs
15aee 65 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  et */.    int iC
15aef 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20  ellLast;     /* 
15af0 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65  Last possible ce
15af1 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20  ll or freeblock 
15af2 6f 66 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70  offset */..    p
15af3 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b  Bt = pPage->pBt;
15af4 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 50 61 67  ..    hdr = pPag
15af5 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
15af6 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e    data = pPage->
15af7 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64  aData;.    if( d
15af8 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65  ecodeFlags(pPage
15af9 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72  , data[hdr]) ) r
15afa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15afb 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61  RUPT_BKPT;.    a
15afc 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65  ssert( pBt->page
15afd 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74  Size>=512 && pBt
15afe 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36  ->pageSize<=3276
15aff 38 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  8 );.    pPage->
15b00 6d 61 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e  maskPage = pBt->
15b01 70 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20  pageSize - 1;.  
15b02 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
15b03 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62  ow = 0;.    usab
15b04 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  leSize = pBt->us
15b05 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50  ableSize;.    pP
15b06 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
15b07 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68  = cellOffset = h
15b08 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67  dr + 12 - 4*pPag
15b09 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70  e->leaf;.    top
15b0a 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
15b0b 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70  a[hdr+5]);.    p
15b0c 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65  Page->nCell = ge
15b0d 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
15b0e 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50  +3]);.    if( pP
15b0f 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45  age->nCell>MX_CE
15b10 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  LL(pBt) ){.     
15b11 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c   /* To many cell
15b12 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70  s for a single p
15b13 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d  age.  The page m
15b14 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a  ust be corrupt *
15b15 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  /.      return S
15b16 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15b17 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65  PT;.    }.    te
15b18 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e  stcase( pPage->n
15b19 43 65 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42  Cell==MX_CELL(pB
15b1a 74 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20  t) );..    /* A 
15b1b 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61  malformed databa
15b1c 73 65 20 70 61 67 65 20 6d 69 67 68 74 20 63 61  se page might ca
15b1d 75 73 65 20 75 73 20 74 6f 20 72 65 61 64 20 70  use us to read p
15b1e 61 73 74 20 74 68 65 20 65 6e 64 0a 20 20 20 20  ast the end.    
15b1f 2a 2a 20 6f 66 20 70 61 67 65 20 77 68 65 6e 20  ** of page when 
15b20 70 61 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20  parsing a cell. 
15b21 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20   .    **.    ** 
15b22 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  The following bl
15b23 6f 63 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63  ock of code chec
15b24 6b 73 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20  ks early to see 
15b25 69 66 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64  if a cell extend
15b26 73 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68  s.    ** past th
15b27 65 20 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20  e end of a page 
15b28 62 6f 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75  boundary and cau
15b29 73 65 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  ses SQLITE_CORRU
15b2a 50 54 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a  PT to be .    **
15b2b 20 72 65 74 75 72 6e 65 64 20 69 66 20 69 74 20   returned if it 
15b2c 64 6f 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  does..    */.   
15b2d 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65   iCellFirst = ce
15b2e 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61  llOffset + 2*pPa
15b2f 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69  ge->nCell;.    i
15b30 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c  CellLast = usabl
15b31 65 53 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64  eSize - 4;.#if d
15b32 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
15b33 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45  ABLE_OVERSIZE_CE
15b34 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a  LL_CHECK).    {.
15b35 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20        int i;    
15b36 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
15b37 20 69 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 70   into the cell p
15b38 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a  ointer array */.
15b39 20 20 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 20        int sz;   
15b3a 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
15b3b 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20  of a cell */..  
15b3c 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e      if( !pPage->
15b3d 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74  leaf ) iCellLast
15b3e 2d 2d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  --;.      for(i=
15b3f 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  0; i<pPage->nCel
15b40 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; i++){.       
15b41 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26   pc = get2byte(&
15b42 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b  data[cellOffset+
15b43 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 74  i*2]);.        t
15b44 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65  estcase( pc==iCe
15b45 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20  llFirst );.     
15b46 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d     testcase( pc=
15b47 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20  =iCellLast );.  
15b48 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 65        if( pc<iCe
15b49 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43  llFirst || pc>iC
15b4a 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20  ellLast ){.     
15b4b 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
15b4c 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
15b4d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
15b4e 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65     sz = cellSize
15b4f 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 61  Ptr(pPage, &data
15b50 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 74  [pc]);.        t
15b51 65 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d  estcase( pc+sz==
15b52 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20  usableSize );.  
15b53 20 20 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e        if( pc+sz>
15b54 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20  usableSize ){.  
15b55 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
15b56 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15b57 50 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  PT;.        }.  
15b58 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
15b59 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69  !pPage->leaf ) i
15b5a 43 65 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20  CellLast++;.    
15b5b 7d 20 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  }  .#endif..    
15b5c 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74  /* Compute the t
15b5d 6f 74 61 6c 20 66 72 65 65 20 73 70 61 63 65 20  otal free space 
15b5e 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20  on the page */. 
15b5f 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65     pc = get2byte
15b60 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a  (&data[hdr+1]);.
15b61 20 20 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61      nFree = data
15b62 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20  [hdr+7] + top;. 
15b63 20 20 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29     while( pc>0 )
15b64 7b 0a 20 20 20 20 20 20 75 31 36 20 6e 65 78 74  {.      u16 next
15b65 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 66  , size;.      if
15b66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20  ( pc<iCellFirst 
15b67 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20  || pc>iCellLast 
15b68 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74  ){.        /* St
15b69 61 72 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63  art of free bloc
15b6a 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70 61 67  k is off the pag
15b6b 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74  e */.        ret
15b6c 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15b6d 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20  PT_BKPT; .      
15b6e 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67  }.      next = g
15b6f 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  et2byte(&data[pc
15b70 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d  ]);.      size =
15b71 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15b72 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66  pc+2]);.      if
15b73 28 20 28 6e 65 78 74 3e 30 20 26 26 20 6e 65 78  ( (next>0 && nex
15b74 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c  t<=pc+size+3) ||
15b75 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53   pc+size>usableS
15b76 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ize ){.        /
15b77 2a 20 46 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75  * Free blocks mu
15b78 73 74 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69  st be in ascendi
15b79 6e 67 20 6f 72 64 65 72 2e 20 41 6e 64 20 74 68  ng order. And th
15b7a 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 09  e last byte of..
15b7b 2a 2a 20 74 68 65 20 66 72 65 65 2d 62 6c 6f 63  ** the free-bloc
15b7c 6b 20 6d 75 73 74 20 6c 69 65 20 6f 6e 20 74 68  k must lie on th
15b7d 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e  e database page.
15b7e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74    */.        ret
15b7f 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15b80 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20  PT_BKPT; .      
15b81 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20  }.      nFree = 
15b82 6e 46 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20  nFree + size;.  
15b83 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20      pc = next;. 
15b84 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20     }..    /* At 
15b85 74 68 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65  this point, nFre
15b86 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73  e contains the s
15b87 75 6d 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74  um of the offset
15b88 20 74 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20   to the start.  
15b89 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c    ** of the cell
15b8a 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 70 6c  -content area pl
15b8b 75 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  us the number of
15b8c 20 66 72 65 65 20 62 79 74 65 73 20 77 69 74 68   free bytes with
15b8d 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65  in.    ** the ce
15b8e 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e  ll-content area.
15b8f 20 49 66 20 74 68 69 73 20 69 73 20 67 72 65 61   If this is grea
15b90 74 65 72 20 74 68 61 6e 20 74 68 65 20 75 73 61  ter than the usa
15b91 62 6c 65 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20  ble-size.    ** 
15b92 6f 66 20 74 68 65 20 70 61 67 65 2c 20 74 68 65  of the page, the
15b93 6e 20 74 68 65 20 70 61 67 65 20 6d 75 73 74 20  n the page must 
15b94 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68  be corrupted. Th
15b95 69 73 20 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20  is check also.  
15b96 20 20 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76    ** serves to v
15b97 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6f  erify that the o
15b98 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 74 61  ffset to the sta
15b99 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63  rt of the cell-c
15b9a 6f 6e 74 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72  ontent.    ** ar
15b9b 65 61 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ea, according to
15b9c 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
15b9d 2c 20 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68  , lies within th
15b9e 65 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20  e page..    */. 
15b9f 20 20 20 69 66 28 20 6e 46 72 65 65 3e 75 73 61     if( nFree>usa
15ba0 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  bleSize ){.     
15ba1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15ba2 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20  ORRUPT_BKPT; .  
15ba3 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e    }.    pPage->n
15ba4 46 72 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72  Free = (u16)(nFr
15ba5 65 65 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29  ee - iCellFirst)
15ba6 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49  ;.    pPage->isI
15ba7 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  nit = 1;.  }.  r
15ba8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15ba9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70  .}../*.** Set up
15baa 20 61 20 72 61 77 20 70 61 67 65 20 73 6f 20 74   a raw page so t
15bab 68 61 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b  hat it looks lik
15bac 65 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67  e a database pag
15bad 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20  e holding.** no 
15bae 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74  entries..*/.stat
15baf 69 63 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65  ic void zeroPage
15bb0 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
15bb1 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75   int flags){.  u
15bb2 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61  nsigned char *da
15bb3 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
15bb4 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  a;.  BtShared *p
15bb5 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b  Bt = pPage->pBt;
15bb6 0a 20 20 75 38 20 68 64 72 20 3d 20 70 50 61 67  .  u8 hdr = pPag
15bb7 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
15bb8 75 31 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73  u16 first;..  as
15bb9 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
15bba 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61  erPagenumber(pPa
15bbb 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50  ge->pDbPage)==pP
15bbc 61 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61  age->pgno );.  a
15bbd 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
15bbe 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67  gerGetExtra(pPag
15bbf 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28  e->pDbPage) == (
15bc0 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20  void*)pPage );. 
15bc1 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15bc2 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61  PagerGetData(pPa
15bc3 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20  ge->pDbPage) == 
15bc4 64 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74  data );.  assert
15bc5 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
15bc6 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
15bc7 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61  >pDbPage) );.  a
15bc8 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15bc9 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
15bca 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d  utex) );.  /*mem
15bcb 73 65 74 28 26 64 61 74 61 5b 68 64 72 5d 2c 20  set(&data[hdr], 
15bcc 30 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  0, pBt->usableSi
15bcd 7a 65 20 2d 20 68 64 72 29 3b 2a 2f 0a 20 20 64  ze - hdr);*/.  d
15bce 61 74 61 5b 68 64 72 5d 20 3d 20 28 63 68 61 72  ata[hdr] = (char
15bcf 29 66 6c 61 67 73 3b 0a 20 20 66 69 72 73 74 20  )flags;.  first 
15bd0 3d 20 68 64 72 20 2b 20 38 20 2b 20 34 2a 28 28  = hdr + 8 + 4*((
15bd1 66 6c 61 67 73 26 50 54 46 5f 4c 45 41 46 29 3d  flags&PTF_LEAF)=
15bd2 3d 30 20 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73  =0 ?1:0);.  mems
15bd3 65 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c  et(&data[hdr+1],
15bd4 20 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68   0, 4);.  data[h
15bd5 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74  dr+7] = 0;.  put
15bd6 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
15bd7 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  5], pBt->usableS
15bd8 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e  ize);.  pPage->n
15bd9 46 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 61 62  Free = pBt->usab
15bda 6c 65 53 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a  leSize - first;.
15bdb 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50    decodeFlags(pP
15bdc 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  age, flags);.  p
15bdd 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20  Page->hdrOffset 
15bde 3d 20 68 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e  = hdr;.  pPage->
15bdf 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72  cellOffset = fir
15be0 73 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76  st;.  pPage->nOv
15be1 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73  erflow = 0;.  as
15be2 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53  sert( pBt->pageS
15be3 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d  ize>=512 && pBt-
15be4 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38  >pageSize<=32768
15be5 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73   );.  pPage->mas
15be6 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 67  kPage = pBt->pag
15be7 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61  eSize - 1;.  pPa
15be8 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20  ge->nCell = 0;. 
15be9 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
15bea 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f   1;.}.../*.** Co
15beb 6e 76 65 72 74 20 61 20 44 62 50 61 67 65 20 6f  nvert a DbPage o
15bec 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 68 65  btained from the
15bed 20 70 61 67 65 72 20 69 6e 74 6f 20 61 20 4d 65   pager into a Me
15bee 6d 50 61 67 65 20 75 73 65 64 20 62 79 0a 2a 2a  mPage used by.**
15bef 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   the btree layer
15bf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50  ..*/.static MemP
15bf1 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 46 72  age *btreePageFr
15bf2 6f 6d 44 62 50 61 67 65 28 44 62 50 61 67 65 20  omDbPage(DbPage 
15bf3 2a 70 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70  *pDbPage, Pgno p
15bf4 67 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70  gno, BtShared *p
15bf5 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  Bt){.  MemPage *
15bf6 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65  pPage = (MemPage
15bf7 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
15bf8 74 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b  tExtra(pDbPage);
15bf9 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20  .  pPage->aData 
15bfa 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
15bfb 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a  tData(pDbPage);.
15bfc 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65    pPage->pDbPage
15bfd 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 70 50   = pDbPage;.  pP
15bfe 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a  age->pBt = pBt;.
15bff 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20    pPage->pgno = 
15c00 70 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68  pgno;.  pPage->h
15c01 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65  drOffset = pPage
15c02 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20  ->pgno==1 ? 100 
15c03 3a 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 50  : 0;.  return pP
15c04 61 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  age; .}../*.** G
15c05 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74  et a page from t
15c06 68 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69  he pager.  Initi
15c07 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67  alize the MemPag
15c08 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d  e.pBt and.** Mem
15c09 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65  Page.aData eleme
15c0a 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a  nts if needed..*
15c0b 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f  *.** If the noCo
15c0c 6e 74 65 6e 74 20 66 6c 61 67 20 69 73 20 73 65  ntent flag is se
15c0d 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  t, it means that
15c0e 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20   we do not care 
15c0f 61 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e  about.** the con
15c10 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65  tent of the page
15c11 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 20   at this time.  
15c12 53 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20  So do not go to 
15c13 74 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66  the disk.** to f
15c14 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74  etch the content
15c15 2e 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20  .  Just fill in 
15c16 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68  the content with
15c17 20 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a   zeros for now..
15c18 2a 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74  ** If in the fut
15c19 75 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69  ure we call sqli
15c1a 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
15c1b 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68  on this page, th
15c1c 61 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68  at.** means we h
15c1d 61 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62  ave started to b
15c1e 65 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75  e concerned abou
15c1f 74 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68  t content and th
15c20 65 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73  e disk.** read s
15c21 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20 74  hould occur at t
15c22 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74  hat point..*/.st
15c23 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 47 65  atic int btreeGe
15c24 74 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65  tPage(.  BtShare
15c25 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 2f 2a  d *pBt,       /*
15c26 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20   The btree */.  
15c27 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20  Pgno pgno,      
15c28 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
15c29 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 66 65  f the page to fe
15c2a 74 63 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  tch */.  MemPage
15c2b 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a   **ppPage,    /*
15c2c 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65   Return the page
15c2d 20 69 6e 20 74 68 69 73 20 70 61 72 61 6d 65 74   in this paramet
15c2e 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f  er */.  int noCo
15c2f 6e 74 65 6e 74 20 20 20 20 20 20 20 20 2f 2a 20  ntent        /* 
15c30 44 6f 20 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65  Do not load page
15c31 20 63 6f 6e 74 65 6e 74 20 69 66 20 74 72 75 65   content if true
15c32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
15c33 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61  .  DbPage *pDbPa
15c34 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ge;..  assert( s
15c35 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15c36 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
15c37 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
15c38 61 67 65 72 41 63 71 75 69 72 65 28 70 42 74 2d  agerAcquire(pBt-
15c39 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28  >pPager, pgno, (
15c3a 44 62 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67  DbPage**)&pDbPag
15c3b 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20  e, noContent);. 
15c3c 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
15c3d 20 72 63 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d   rc;.  *ppPage =
15c3e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62   btreePageFromDb
15c3f 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67  Page(pDbPage, pg
15c40 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65 74 75  no, pBt);.  retu
15c41 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
15c42 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76 65 20  ./*.** Retrieve 
15c43 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
15c44 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 66 20  pager cache. If 
15c45 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
15c46 67 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72  ge is not.** alr
15c47 65 61 64 79 20 69 6e 20 74 68 65 20 70 61 67 65  eady in the page
15c48 72 20 63 61 63 68 65 20 72 65 74 75 72 6e 20 4e  r cache return N
15c49 55 4c 4c 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20  ULL. Initialize 
15c4a 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20  the MemPage.pBt 
15c4b 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61  and.** MemPage.a
15c4c 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66  Data elements if
15c4d 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74   needed..*/.stat
15c4e 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 72 65  ic MemPage *btre
15c4f 65 50 61 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68  ePageLookup(BtSh
15c50 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
15c51 70 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20  pgno){.  DbPage 
15c52 2a 70 44 62 50 61 67 65 3b 0a 20 20 61 73 73 65  *pDbPage;.  asse
15c53 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15c54 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
15c55 78 29 20 29 3b 0a 20 20 70 44 62 50 61 67 65 20  x) );.  pDbPage 
15c56 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f  = sqlite3PagerLo
15c57 6f 6b 75 70 28 70 42 74 2d 3e 70 50 61 67 65 72  okup(pBt->pPager
15c58 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70  , pgno);.  if( p
15c59 44 62 50 61 67 65 20 29 7b 0a 20 20 20 20 72 65  DbPage ){.    re
15c5a 74 75 72 6e 20 62 74 72 65 65 50 61 67 65 46 72  turn btreePageFr
15c5b 6f 6d 44 62 50 61 67 65 28 70 44 62 50 61 67 65  omDbPage(pDbPage
15c5c 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20  , pgno, pBt);.  
15c5d 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
15c5e 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
15c5f 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e size of the da
15c60 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70  tabase file in p
15c61 61 67 65 73 2e 20 49 66 20 74 68 65 72 65 20 69  ages. If there i
15c62 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a  s any kind of.**
15c63 20 65 72 72 6f 72 2c 20 72 65 74 75 72 6e 20 28   error, return (
15c64 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 2d 31  (unsigned int)-1
15c65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e  )..*/.static Pgn
15c66 6f 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  o pagerPagecount
15c67 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b  (BtShared *pBt){
15c68 0a 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d  .  int nPage = -
15c69 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  1;.  int rc;.  a
15c6a 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67  ssert( pBt->pPag
15c6b 65 31 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  e1 );.  rc = sql
15c6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
15c6d 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  nt(pBt->pPager, 
15c6e 26 6e 50 61 67 65 29 3b 0a 20 20 61 73 73 65 72  &nPage);.  asser
15c6f 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
15c70 20 7c 7c 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b   || nPage==-1 );
15c71 0a 20 20 72 65 74 75 72 6e 20 28 50 67 6e 6f 29  .  return (Pgno)
15c72 6e 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  nPage;.}../*.** 
15c73 47 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20  Get a page from 
15c74 74 68 65 20 70 61 67 65 72 20 61 6e 64 20 69 6e  the pager and in
15c75 69 74 69 61 6c 69 7a 65 20 69 74 2e 20 20 54 68  itialize it.  Th
15c76 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6a 75  is routine is ju
15c77 73 74 20 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65  st a.** convenie
15c78 6e 63 65 20 77 72 61 70 70 65 72 20 61 72 6f 75  nce wrapper arou
15c79 6e 64 20 73 65 70 61 72 61 74 65 20 63 61 6c 6c  nd separate call
15c7a 73 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67  s to btreeGetPag
15c7b 65 28 29 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65  e() and .** btre
15c7c 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a  eInitPage()..**.
15c7d 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
15c7e 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20  ccurs, then the 
15c7f 76 61 6c 75 65 20 2a 70 70 50 61 67 65 20 69 73  value *ppPage is
15c80 20 73 65 74 20 74 6f 20 69 73 20 75 6e 64 65 66   set to is undef
15c81 69 6e 65 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20  ined. It.** may 
15c82 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64  remain unchanged
15c83 2c 20 6f 72 20 69 74 20 6d 61 79 20 62 65 20 73  , or it may be s
15c84 65 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64  et to an invalid
15c85 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
15c86 63 20 69 6e 74 20 67 65 74 41 6e 64 49 6e 69 74  c int getAndInit
15c87 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
15c88 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20   *pBt,          
15c89 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
15c8a 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  file */.  Pgno p
15c8b 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f  gno,           /
15c8c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  * Number of the 
15c8d 70 61 67 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20  page to get */. 
15c8e 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67   MemPage **ppPag
15c8f 65 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74  e     /* Write t
15c90 68 65 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20  he page pointer 
15c91 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
15c92 20 72 63 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28   rc;.  TESTONLY(
15c93 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 20 3d 20   Pgno iLastPg = 
15c94 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
15c95 42 74 29 3b 20 29 0a 20 20 61 73 73 65 72 74 28  Bt); ).  assert(
15c96 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15c97 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
15c98 29 3b 0a 0a 20 20 72 63 20 3d 20 62 74 72 65 65  );..  rc = btree
15c99 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e  GetPage(pBt, pgn
15c9a 6f 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20  o, ppPage, 0);. 
15c9b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
15c9c 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62  OK ){.    rc = b
15c9d 74 72 65 65 49 6e 69 74 50 61 67 65 28 2a 70 70  treeInitPage(*pp
15c9e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
15c9f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
15ca0 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
15ca1 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20  e(*ppPage);.    
15ca2 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
15ca3 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67  he requested pag
15ca4 65 20 6e 75 6d 62 65 72 20 77 61 73 20 65 69 74  e number was eit
15ca5 68 65 72 20 30 20 6f 72 20 67 72 65 61 74 65 72  her 0 or greater
15ca6 20 74 68 61 6e 20 74 68 65 20 70 61 67 65 0a 20   than the page. 
15ca7 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 74 68   ** number of th
15ca8 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 74  e last page in t
15ca9 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 69  he database, thi
15caa 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  s function shoul
15cab 64 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51  d return.  ** SQ
15cac 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 6f 72 20  LITE_CORRUPT or 
15cad 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72  some other error
15cae 20 28 69 2e 65 2e 20 53 51 4c 49 54 45 5f 46 55   (i.e. SQLITE_FU
15caf 4c 4c 29 2e 20 43 68 65 63 6b 20 74 68 61 74 20  LL). Check that 
15cb0 74 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65  this.  ** is the
15cb1 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73   case.  */.  ass
15cb2 65 72 74 28 20 28 70 67 6e 6f 3e 30 20 26 26 20  ert( (pgno>0 && 
15cb3 70 67 6e 6f 3c 3d 69 4c 61 73 74 50 67 29 20 7c  pgno<=iLastPg) |
15cb4 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  | rc!=SQLITE_OK 
15cb5 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
15cb6 67 6e 6f 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74  gno==0 );.  test
15cb7 63 61 73 65 28 20 70 67 6e 6f 3d 3d 69 4c 61 73  case( pgno==iLas
15cb8 74 50 67 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e  tPg );..  return
15cb9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
15cba 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 67 65 2e  lease a MemPage.
15cbb 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65    This should be
15cbc 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72   called once for
15cbd 20 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63   each prior.** c
15cbe 61 6c 6c 20 74 6f 20 62 74 72 65 65 47 65 74 50  all to btreeGetP
15cbf 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
15cc0 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28  oid releasePage(
15cc1 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b  MemPage *pPage){
15cc2 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a  .  if( pPage ){.
15cc3 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
15cc4 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  e->nOverflow==0 
15cc5 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  || sqlite3PagerP
15cc6 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61 67  ageRefcount(pPag
15cc7 65 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 3b  e->pDbPage)>1 );
15cc8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
15cc9 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20  ge->aData );.   
15cca 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15ccb 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
15ccc 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  t( sqlite3PagerG
15ccd 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70  etExtra(pPage->p
15cce 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64  DbPage) == (void
15ccf 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 20 20 61  *)pPage );.    a
15cd0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
15cd1 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65  gerGetData(pPage
15cd2 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67  ->pDbPage)==pPag
15cd3 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20  e->aData );.    
15cd4 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15cd5 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
15cd6 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
15cd7 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
15cd8 72 55 6e 72 65 66 28 70 50 61 67 65 2d 3e 70 44  rUnref(pPage->pD
15cd9 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  bPage);.  }.}../
15cda 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20 72 6f  *.** During a ro
15cdb 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74 68 65  llback, when the
15cdc 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73 20 69   pager reloads i
15cdd 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20  nformation into 
15cde 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20  the cache.** so 
15cdf 74 68 61 74 20 74 68 65 20 63 61 63 68 65 20 69  that the cache i
15ce0 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74  s restored to it
15ce1 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65  s original state
15ce2 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
15ce3 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74  .** the transact
15ce4 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20 70 61  ion, for each pa
15ce5 67 65 20 72 65 73 74 6f 72 65 64 20 74 68 69 73  ge restored this
15ce6 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
15ce7 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
15ce8 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20  outine needs to 
15ce9 72 65 73 65 74 20 74 68 65 20 65 78 74 72 61 20  reset the extra 
15cea 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61 74 20  data section at 
15ceb 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a  the end of the.*
15cec 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65 65 20  * page to agree 
15ced 77 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65  with the restore
15cee 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69  d data..*/.stati
15cef 63 20 76 6f 69 64 20 70 61 67 65 52 65 69 6e 69  c void pageReini
15cf0 74 28 44 62 50 61 67 65 20 2a 70 44 61 74 61 29  t(DbPage *pData)
15cf1 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
15cf2 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d  ge;.  pPage = (M
15cf3 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33  emPage *)sqlite3
15cf4 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44  PagerGetExtra(pD
15cf5 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ata);.  assert( 
15cf6 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
15cf7 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e  Refcount(pData)>
15cf8 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  0 );.  if( pPage
15cf9 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20  ->isInit ){.    
15cfa 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15cfb 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
15cfc 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
15cfd 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e  .    pPage->isIn
15cfe 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  it = 0;.    if( 
15cff 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
15d00 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e  Refcount(pData)>
15d01 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 70 50  1 ){.      /* pP
15d02 61 67 65 20 6d 69 67 68 74 20 6e 6f 74 20 62 65  age might not be
15d03 20 61 20 62 74 72 65 65 20 70 61 67 65 3b 20 20   a btree page;  
15d04 69 74 20 6d 69 67 68 74 20 62 65 20 61 6e 20 6f  it might be an o
15d05 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20  verflow page.   
15d06 20 20 20 2a 2a 20 6f 72 20 70 74 72 6d 61 70 20     ** or ptrmap 
15d07 70 61 67 65 20 6f 72 20 61 20 66 72 65 65 20 70  page or a free p
15d08 61 67 65 2e 20 20 49 6e 20 74 68 6f 73 65 20 63  age.  In those c
15d09 61 73 65 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  ases, the follow
15d0a 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c  ing.      ** cal
15d0b 6c 20 74 6f 20 62 74 72 65 65 49 6e 69 74 50 61  l to btreeInitPa
15d0c 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79  ge() will likely
15d0d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15d0e 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a  ORRUPT..      **
15d0f 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 73 20   But no harm is 
15d10 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 20 41  done by this.  A
15d11 6e 64 20 69 74 20 69 73 20 76 65 72 79 20 69 6d  nd it is very im
15d12 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 20 20  portant that.   
15d13 20 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50     ** btreeInitP
15d14 61 67 65 28 29 20 62 65 20 63 61 6c 6c 65 64 20  age() be called 
15d15 6f 6e 20 65 76 65 72 79 20 62 74 72 65 65 20 70  on every btree p
15d16 61 67 65 20 73 6f 20 77 65 20 6d 61 6b 65 0a 20  age so we make. 
15d17 20 20 20 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c       ** the call
15d18 20 66 6f 72 20 65 76 65 72 79 20 70 61 67 65 20   for every page 
15d19 74 68 61 74 20 63 6f 6d 65 73 20 69 6e 20 66 6f  that comes in fo
15d1a 72 20 72 65 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f  r re-initing. */
15d1b 0a 20 20 20 20 20 20 62 74 72 65 65 49 6e 69 74  .      btreeInit
15d1c 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
15d1d 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
15d1e 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  Invoke the busy 
15d1f 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74  handler for a bt
15d20 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ree..*/.static i
15d21 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75  nt btreeInvokeBu
15d22 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a  syHandler(void *
15d23 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65  pArg){.  BtShare
15d24 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72  d *pBt = (BtShar
15d25 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65  ed*)pArg;.  asse
15d26 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20  rt( pBt->db );. 
15d27 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15d28 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
15d29 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
15d2a 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49   return sqlite3I
15d2b 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72  nvokeBusyHandler
15d2c 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48  (&pBt->db->busyH
15d2d 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  andler);.}../*.*
15d2e 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73  * Open a databas
15d2f 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a  e file..** .** z
15d30 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20  Filename is the 
15d31 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
15d32 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a  base file.  If z
15d33 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c  Filename is NULL
15d34 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61  .** a new databa
15d35 73 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d  se with a random
15d36 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64   name is created
15d37 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79  .  This randomly
15d38 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61   named.** databa
15d39 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20  se file will be 
15d3a 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c  deleted when sql
15d3b 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29  ite3BtreeClose()
15d3c 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49   is called..** I
15d3d 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22  f zFilename is "
15d3e 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61  :memory:" then a
15d3f 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
15d40 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 0a  base is created.
15d41 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d  ** that is autom
15d42 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79  atically destroy
15d43 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c  ed when it is cl
15d44 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  osed..**.** If t
15d45 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61  he database is a
15d46 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 69 6e  lready opened in
15d47 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
15d48 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
15d49 20 61 6e 64 20 77 65 20 61 72 65 20 69 6e 20 73   and we are in s
15d4a 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65  hared cache mode
15d4b 2c 20 74 68 65 6e 20 74 68 65 20 6f 70 65 6e 20  , then the open 
15d4c 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61  will fail with a
15d4d 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53  n.** SQLITE_CONS
15d4e 54 52 41 49 4e 54 20 65 72 72 6f 72 2e 20 20 57  TRAINT error.  W
15d4f 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74  e cannot allow t
15d50 77 6f 20 6f 72 20 6d 6f 72 65 20 42 74 53 68 61  wo or more BtSha
15d51 72 65 64 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69  red.** objects i
15d52 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  n the same datab
15d53 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73  ase connection s
15d54 69 6e 63 65 20 64 6f 69 6e 67 20 73 6f 20 77 69  ince doing so wi
15d55 6c 6c 20 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72  ll lead.** to pr
15d56 6f 62 6c 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b  oblems with lock
15d57 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
15d58 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15d59 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 63  e3BtreeOpen(.  c
15d5a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
15d5b 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f  name,  /* Name o
15d5c 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61  f the file conta
15d5d 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 20  ining the BTree 
15d5e 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 71  database */.  sq
15d5f 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
15d60 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61        /* Associa
15d61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 61 6e  ted database han
15d62 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  dle */.  Btree *
15d63 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 20 20  *ppBtree,       
15d64 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e   /* Pointer to n
15d65 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20  ew Btree object 
15d66 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
15d67 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
15d68 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74            /* Opt
15d69 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66  ions */.  int vf
15d6a 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20  sFlags          
15d6b 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65    /* Flags passe
15d6c 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c  d through to sql
15d6d 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29  ite3_vfs.xOpen()
15d6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
15d6f 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20  _vfs *pVfs;     
15d70 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
15d71 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68  FS to use for th
15d72 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74  is btree */.  Bt
15d73 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30 3b  Shared *pBt = 0;
15d74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15d75 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 20 62  Shared part of b
15d76 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
15d77 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20 20  /.  Btree *p;   
15d78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d79 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20     /* Handle to 
15d7a 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69  return */.  sqli
15d7b 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
15d7c 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72  Open = 0;  /* Pr
15d7d 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 63 6f  events a race co
15d7e 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20  ndition. Ticket 
15d7f 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72  #3537 */.  int r
15d80 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
15d81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
15d82 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68  ult code from th
15d83 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  is function */. 
15d84 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20 20   u8 nReserve;   
15d85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d86 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73 65  /* Byte of unuse
15d87 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 20  d space on each 
15d88 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  page */.  unsign
15d89 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 65  ed char zDbHeade
15d8a 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61  r[100];  /* Data
15d8b 62 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e 74  base header cont
15d8c 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74  ent */..  /* Set
15d8d 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 69 73   the variable is
15d8e 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f  Memdb to true fo
15d8f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  r an in-memory d
15d90 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a  atabase, or .  *
15d91 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69  * false for a fi
15d92 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62 61 73  le-based databas
15d93 65 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69  e. This symbol i
15d94 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20  s only required 
15d95 69 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f  if.  ** either o
15d96 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74  f the shared-dat
15d97 61 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20  a or autovacuum 
15d98 66 65 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d  features are com
15d99 70 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f  piled .  ** into
15d9a 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20   the library..  
15d9b 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
15d9c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15d9d 45 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65  ED_CACHE) || !de
15d9e 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
15d9f 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20  T_AUTOVACUUM).  
15da0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
15da1 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20  IT_MEMORYDB.    
15da2 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64  const int isMemd
15da3 62 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20  b = 0;.  #else. 
15da4 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d     const int isM
15da5 65 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65  emdb = zFilename
15da6 20 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c   && !strcmp(zFil
15da7 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a  ename, ":memory:
15da8 22 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e  ");.  #endif.#en
15da9 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64  dif..  assert( d
15daa 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  b!=0 );.  assert
15dab 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
15dac 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
15dad 29 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 2d  );..  pVfs = db-
15dae 3e 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c  >pVfs;.  p = sql
15daf 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  ite3MallocZero(s
15db0 69 7a 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20  izeof(Btree));. 
15db1 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72   if( !p ){.    r
15db2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
15db3 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54  EM;.  }.  p->inT
15db4 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e  rans = TRANS_NON
15db5 45 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  E;.  p->db = db;
15db6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15db7 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
15db8 45 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72  E.  p->lock.pBtr
15db9 65 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63  ee = p;.  p->loc
15dba 6b 2e 69 54 61 62 6c 65 20 3d 20 31 3b 0a 23 65  k.iTable = 1;.#e
15dbb 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e  ndif..#if !defin
15dbc 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
15dbd 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20  HARED_CACHE) && 
15dbe 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
15dbf 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f  OMIT_DISKIO).  /
15dc0 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 42  *.  ** If this B
15dc1 74 72 65 65 20 69 73 20 61 20 63 61 6e 64 69 64  tree is a candid
15dc2 61 74 65 20 66 6f 72 20 73 68 61 72 65 64 20 63  ate for shared c
15dc3 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66 69 6e  ache, try to fin
15dc4 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69  d an.  ** existi
15dc5 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  ng BtShared obje
15dc6 63 74 20 74 68 61 74 20 77 65 20 63 61 6e 20 73  ct that we can s
15dc7 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20  hare with.  */. 
15dc8 20 69 66 28 20 69 73 4d 65 6d 64 62 3d 3d 30 20   if( isMemdb==0 
15dc9 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20  && zFilename && 
15dca 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a  zFilename[0] ){.
15dcb 20 20 20 20 69 66 28 20 76 66 73 46 6c 61 67 73      if( vfsFlags
15dcc 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53   & SQLITE_OPEN_S
15dcd 48 41 52 45 44 43 41 43 48 45 20 29 7b 0a 20 20  HAREDCACHE ){.  
15dce 20 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74      int nFullPat
15dcf 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78  hname = pVfs->mx
15dd0 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20  Pathname+1;.    
15dd1 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74    char *zFullPat
15dd2 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  hname = sqlite3M
15dd3 61 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e  alloc(nFullPathn
15dd4 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ame);.      sqli
15dd5 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
15dd6 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 70 2d  Shared;.      p-
15dd7 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20  >sharable = 1;. 
15dd8 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c 6c 50       if( !zFullP
15dd9 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20  athname ){.     
15dda 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
15ddb 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  p);.        retu
15ddc 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
15ddd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
15dde 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68  qlite3OsFullPath
15ddf 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65  name(pVfs, zFile
15de0 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e  name, nFullPathn
15de1 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61  ame, zFullPathna
15de2 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78  me);.      mutex
15de3 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75  Open = sqlite3Mu
15de4 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
15de5 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45  MUTEX_STATIC_OPE
15de6 4e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  N);.      sqlite
15de7 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
15de8 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20  texOpen);.      
15de9 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73 71  mutexShared = sq
15dea 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
15deb 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
15dec 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20  TIC_MASTER);.   
15ded 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15dee 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72  _enter(mutexShar
15def 65 64 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  ed);.      for(p
15df0 42 74 3d 47 4c 4f 42 41 4c 28 42 74 53 68 61 72  Bt=GLOBAL(BtShar
15df1 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65  ed*,sqlite3Share
15df2 64 43 61 63 68 65 4c 69 73 74 29 3b 20 70 42 74  dCacheList); pBt
15df3 3b 20 70 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74  ; pBt=pBt->pNext
15df4 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
15df5 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29  t( pBt->nRef>0 )
15df6 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d  ;.        if( 0=
15df7 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74  =strcmp(zFullPat
15df8 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61  hname, sqlite3Pa
15df9 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d  gerFilename(pBt-
15dfa 3e 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20  >pPager)).      
15dfb 20 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71             && sq
15dfc 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 70 42  lite3PagerVfs(pB
15dfd 74 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73  t->pPager)==pVfs
15dfe 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
15dff 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 20  t iDb;.         
15e00 20 66 6f 72 28 69 44 62 3d 64 62 2d 3e 6e 44 62   for(iDb=db->nDb
15e01 2d 31 3b 20 69 44 62 3e 3d 30 3b 20 69 44 62 2d  -1; iDb>=0; iDb-
15e02 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -){.            
15e03 42 74 72 65 65 20 2a 70 45 78 69 73 74 69 6e 67  Btree *pExisting
15e04 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
15e05 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20  pBt;.           
15e06 20 69 66 28 20 70 45 78 69 73 74 69 6e 67 20 26   if( pExisting &
15e07 26 20 70 45 78 69 73 74 69 6e 67 2d 3e 70 42 74  & pExisting->pBt
15e08 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ==pBt ){.       
15e09 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d         sqlite3_m
15e0a 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
15e0b 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 20  Shared);.       
15e0c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d         sqlite3_m
15e0d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
15e0e 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  Open);.         
15e0f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
15e10 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  e(zFullPathname)
15e11 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
15e12 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
15e13 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
15e14 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e  eturn SQLITE_CON
15e15 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20  STRAINT;.       
15e16 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
15e17 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e   }.          p->
15e18 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20  pBt = pBt;.     
15e19 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b       pBt->nRef++
15e1a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
15e1b 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
15e1c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
15e1d 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
15e1e 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20  utexShared);.   
15e1f 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
15e20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a  zFullPathname);.
15e21 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c      }.#ifdef SQL
15e22 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 6c  ITE_DEBUG.    el
15e23 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20  se{.      /* In 
15e24 64 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d  debug mode, we m
15e25 61 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 65  ark all persiste
15e26 6e 74 20 64 61 74 61 62 61 73 65 73 20 61 73 20  nt databases as 
15e27 73 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 2a  sharable.      *
15e28 2a 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 79  * even when they
15e29 20 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 20   are not.  This 
15e2a 65 78 65 72 63 69 73 65 73 20 74 68 65 20 6c 6f  exercises the lo
15e2b 63 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20  cking code and. 
15e2c 20 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f       ** gives mo
15e2d 72 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 66  re opportunity f
15e2e 6f 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 74  or asserts(sqlit
15e2f 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 29  e3_mutex_held())
15e30 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d  .      ** statem
15e31 65 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63  ents to find loc
15e32 6b 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20  king problems.. 
15e33 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d       */.      p-
15e34 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20  >sharable = 1;. 
15e35 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
15e36 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 74  #endif.  if( pBt
15e37 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20  ==0 ){.    /*.  
15e38 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    ** The followi
15e39 6e 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 20  ng asserts make 
15e3a 73 75 72 65 20 74 68 61 74 20 73 74 72 75 63 74  sure that struct
15e3b 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65  ures used by the
15e3c 20 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 2a   btree are.    *
15e3d 2a 20 74 68 65 20 72 69 67 68 74 20 73 69 7a 65  * the right size
15e3e 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 75  .  This is to gu
15e3f 61 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a 65  ard against size
15e40 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 65   changes that re
15e41 73 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e  sult.    ** when
15e42 20 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20   compiling on a 
15e43 64 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 74  different archit
15e44 65 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20  ecture..    */. 
15e45 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
15e46 66 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a  f(i64)==8 || siz
15e47 65 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20  eof(i64)==4 );. 
15e48 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
15e49 66 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a  f(u64)==8 || siz
15e4a 65 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20  eof(u64)==4 );. 
15e4b 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
15e4c 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20  f(u32)==4 );.   
15e4d 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
15e4e 75 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61  u16)==2 );.    a
15e4f 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50 67  ssert( sizeof(Pg
15e50 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20  no)==4 );.  .   
15e51 20 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d 61   pBt = sqlite3Ma
15e52 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66  llocZero( sizeof
15e53 28 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66  (*pBt) );.    if
15e54 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ( pBt==0 ){.    
15e55 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
15e56 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  MEM;.      goto 
15e57 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a  btree_open_out;.
15e58 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
15e59 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28  qlite3PagerOpen(
15e5a 70 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67  pVfs, &pBt->pPag
15e5b 65 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20  er, zFilename,. 
15e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15e5d 20 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53           EXTRA_S
15e5e 49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46  IZE, flags, vfsF
15e5f 6c 61 67 73 2c 20 70 61 67 65 52 65 69 6e 69 74  lags, pageReinit
15e60 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
15e61 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
15e62 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
15e63 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65  gerReadFileheade
15e64 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 73 69  r(pBt->pPager,si
15e65 7a 65 6f 66 28 7a 44 62 48 65 61 64 65 72 29 2c  zeof(zDbHeader),
15e66 7a 44 62 48 65 61 64 65 72 29 3b 0a 20 20 20 20  zDbHeader);.    
15e67 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  }.    if( rc!=SQ
15e68 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15e69 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e   goto btree_open
15e6a 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
15e6b 70 42 74 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20  pBt->db = db;.  
15e6c 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65    sqlite3PagerSe
15e6d 74 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42 74  tBusyhandler(pBt
15e6e 2d 3e 70 50 61 67 65 72 2c 20 62 74 72 65 65 49  ->pPager, btreeI
15e6f 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72  nvokeBusyHandler
15e70 2c 20 70 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70  , pBt);.    p->p
15e71 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20  Bt = pBt;.  .   
15e72 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20   pBt->pCursor = 
15e73 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67  0;.    pBt->pPag
15e74 65 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d  e1 = 0;.    pBt-
15e75 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69  >readOnly = sqli
15e76 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e  te3PagerIsreadon
15e77 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  ly(pBt->pPager);
15e78 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69  .    pBt->pageSi
15e79 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 7a  ze = get2byte(&z
15e7a 44 62 48 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20  DbHeader[16]);. 
15e7b 20 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65     if( pBt->page
15e7c 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d  Size<512 || pBt-
15e7d 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45  >pageSize>SQLITE
15e7e 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20  _MAX_PAGE_SIZE. 
15e7f 20 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74          || ((pBt
15e80 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42  ->pageSize-1)&pB
15e81 74 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20  t->pageSize)!=0 
15e82 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61  ){.      pBt->pa
15e83 67 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e  geSize = 0;.#ifn
15e84 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15e85 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
15e86 20 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63   /* If the magic
15e87 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22   name ":memory:"
15e88 20 77 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20   will create an 
15e89 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
15e8a 73 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a  se, then.      *
15e8b 2a 20 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f  * leave the auto
15e8c 56 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30  Vacuum mode at 0
15e8d 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61   (do not auto-va
15e8e 63 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20  cuum), even if. 
15e8f 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44       ** SQLITE_D
15e90 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
15e91 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68  M is true. On th
15e92 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66  e other hand, if
15e93 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45  .      ** SQLITE
15e94 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68  _OMIT_MEMORYDB h
15e95 61 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c  as been defined,
15e96 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22   then ":memory:"
15e97 20 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20   is just a.     
15e98 20 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65   ** regular file
15e99 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63  -name. In this c
15e9a 61 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63  ase the auto-vac
15e9b 75 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70  uum applies as p
15e9c 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20  er normal..     
15e9d 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46   */.      if( zF
15e9e 69 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65  ilename && !isMe
15e9f 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70  mdb ){.        p
15ea0 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d  Bt->autoVacuum =
15ea1 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54   (SQLITE_DEFAULT
15ea2 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20  _AUTOVACUUM ? 1 
15ea3 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42  : 0);.        pB
15ea4 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
15ea5 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  (SQLITE_DEFAULT_
15ea6 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20  AUTOVACUUM==2 ? 
15ea7 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  1 : 0);.      }.
15ea8 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65  #endif.      nRe
15ea9 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d  serve = 0;.    }
15eaa 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73  else{.      nRes
15eab 65 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72  erve = zDbHeader
15eac 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d  [20];.      pBt-
15ead 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d  >pageSizeFixed =
15eae 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   1;.#ifndef SQLI
15eaf 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
15eb0 55 4d 0a 20 20 20 20 20 20 70 42 74 2d 3e 61 75  UM.      pBt->au
15eb1 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34  toVacuum = (get4
15eb2 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b  byte(&zDbHeader[
15eb3 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b  36 + 4*4])?1:0);
15eb4 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72  .      pBt->incr
15eb5 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79  Vacuum = (get4by
15eb6 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36  te(&zDbHeader[36
15eb7 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23   + 7*4])?1:0);.#
15eb8 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20  endif.    }.    
15eb9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15eba 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74  rSetPagesize(pBt
15ebb 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e  ->pPager, &pBt->
15ebc 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73 65 72  pageSize, nReser
15ebd 76 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ve);.    if( rc 
15ebe 29 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65  ) goto btree_ope
15ebf 6e 5f 6f 75 74 3b 0a 20 20 20 20 70 42 74 2d 3e  n_out;.    pBt->
15ec0 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74  usableSize = pBt
15ec1 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e 52 65  ->pageSize - nRe
15ec2 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72  serve;.    asser
15ec3 74 28 20 28 70 42 74 2d 3e 70 61 67 65 53 69 7a  t( (pBt->pageSiz
15ec4 65 20 26 20 37 29 3d 3d 30 20 29 3b 20 20 2f 2a  e & 7)==0 );  /*
15ec5 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e   8-byte alignmen
15ec6 74 20 6f 66 20 70 61 67 65 53 69 7a 65 20 2a 2f  t of pageSize */
15ec7 0a 20 20 20 0a 23 69 66 20 21 64 65 66 69 6e 65  .   .#if !define
15ec8 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  d(SQLITE_OMIT_SH
15ec9 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 21  ARED_CACHE) && !
15eca 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
15ecb 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 20 20  MIT_DISKIO).    
15ecc 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20 42  /* Add the new B
15ecd 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74  tShared object t
15ece 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  o the linked lis
15ecf 74 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61  t sharable BtSha
15ed0 72 65 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  reds..    */.   
15ed1 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65   if( p->sharable
15ed2 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
15ed3 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68  3_mutex *mutexSh
15ed4 61 72 65 64 3b 0a 20 20 20 20 20 20 70 42 74 2d  ared;.      pBt-
15ed5 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20  >nRef = 1;.     
15ed6 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73   mutexShared = s
15ed7 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
15ed8 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
15ed9 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20  ATIC_MASTER);.  
15eda 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54      if( SQLITE_T
15edb 48 52 45 41 44 53 41 46 45 20 26 26 20 73 71 6c  HREADSAFE && sql
15edc 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
15edd 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20  .bCoreMutex ){. 
15ede 20 20 20 20 20 20 20 70 42 74 2d 3e 6d 75 74 65         pBt->mute
15edf 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  x = sqlite3Mutex
15ee0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
15ee1 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20 20 20  EX_FAST);.      
15ee2 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 74 65 78    if( pBt->mutex
15ee3 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
15ee4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
15ee5 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62  EM;.          db
15ee6 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
15ee7 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   0;.          go
15ee8 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75  to btree_open_ou
15ee9 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
15eea 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
15eeb 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
15eec 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20  utexShared);.   
15eed 20 20 20 70 42 74 2d 3e 70 4e 65 78 74 20 3d 20     pBt->pNext = 
15eee 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
15eef 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
15ef0 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 20 20  cheList);.      
15ef1 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
15ef2 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
15ef3 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 3b 0a  cheList) = pBt;.
15ef4 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
15ef5 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53  tex_leave(mutexS
15ef6 68 61 72 65 64 29 3b 0a 20 20 20 20 7d 0a 23 65  hared);.    }.#e
15ef7 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 66 20 21 64  ndif.  }..#if !d
15ef8 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
15ef9 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29  IT_SHARED_CACHE)
15efa 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
15efb 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29  ITE_OMIT_DISKIO)
15efc 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 65 77  .  /* If the new
15efd 20 42 74 72 65 65 20 75 73 65 73 20 61 20 73 68   Btree uses a sh
15efe 61 72 61 62 6c 65 20 70 42 74 53 68 61 72 65 64  arable pBtShared
15eff 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 74 68 65 20  , then link the 
15f00 6e 65 77 0a 20 20 2a 2a 20 42 74 72 65 65 20 69  new.  ** Btree i
15f01 6e 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20  nto the list of 
15f02 61 6c 6c 20 73 68 61 72 61 62 6c 65 20 42 74 72  all sharable Btr
15f03 65 65 73 20 66 6f 72 20 74 68 65 20 73 61 6d 65  ees for the same
15f04 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a   connection..  *
15f05 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 6b 65  * The list is ke
15f06 70 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  pt in ascending 
15f07 6f 72 64 65 72 20 62 79 20 70 42 74 20 61 64 64  order by pBt add
15f08 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ress..  */.  if(
15f09 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a   p->sharable ){.
15f0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 42      int i;.    B
15f0b 74 72 65 65 20 2a 70 53 69 62 3b 0a 20 20 20 20  tree *pSib;.    
15f0c 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
15f0d 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
15f0e 69 66 28 20 28 70 53 69 62 20 3d 20 64 62 2d 3e  if( (pSib = db->
15f0f 61 44 62 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26  aDb[i].pBt)!=0 &
15f10 26 20 70 53 69 62 2d 3e 73 68 61 72 61 62 6c 65  & pSib->sharable
15f11 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c   ){.        whil
15f12 65 28 20 70 53 69 62 2d 3e 70 50 72 65 76 20 29  e( pSib->pPrev )
15f13 7b 20 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70  { pSib = pSib->p
15f14 50 72 65 76 3b 20 7d 0a 20 20 20 20 20 20 20 20  Prev; }.        
15f15 69 66 28 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d  if( p->pBt<pSib-
15f16 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20  >pBt ){.        
15f17 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69    p->pNext = pSi
15f18 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  b;.          p->
15f19 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20  pPrev = 0;.     
15f1a 20 20 20 20 20 70 53 69 62 2d 3e 70 50 72 65 76       pSib->pPrev
15f1b 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 65   = p;.        }e
15f1c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 77  lse{.          w
15f1d 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 4e 65 78  hile( pSib->pNex
15f1e 74 20 26 26 20 70 53 69 62 2d 3e 70 4e 65 78 74  t && pSib->pNext
15f1f 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 7b 0a  ->pBt<p->pBt ){.
15f20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 69 62              pSib
15f21 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a   = pSib->pNext;.
15f22 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
15f23 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
15f24 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20   pSib->pNext;.  
15f25 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76          p->pPrev
15f26 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20   = pSib;.       
15f27 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20     if( p->pNext 
15f28 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
15f29 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d  ->pNext->pPrev =
15f2a 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   p;.          }.
15f2b 20 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e            pSib->
15f2c 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20 20 20  pNext = p;.     
15f2d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65     }.        bre
15f2e 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
15f2f 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a  }.  }.#endif.  *
15f30 70 70 42 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74  ppBtree = p;..bt
15f31 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20  ree_open_out:.  
15f32 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15f33 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 74  K ){.    if( pBt
15f34 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20   && pBt->pPager 
15f35 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
15f36 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e  PagerClose(pBt->
15f37 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20  pPager);.    }. 
15f38 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
15f39 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pBt);.    sqlite
15f3a 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 2a  3_free(p);.    *
15f3b 70 70 42 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d  ppBtree = 0;.  }
15f3c 0a 20 20 69 66 28 20 6d 75 74 65 78 4f 70 65 6e  .  if( mutexOpen
15f3d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
15f3e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
15f3f 6c 64 28 6d 75 74 65 78 4f 70 65 6e 29 20 29 3b  ld(mutexOpen) );
15f40 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
15f41 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70  ex_leave(mutexOp
15f42 65 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  en);.  }.  retur
15f43 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  n rc;.}../*.** D
15f44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 42 74 53  ecrement the BtS
15f45 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74  hared.nRef count
15f46 65 72 2e 20 20 57 68 65 6e 20 69 74 20 72 65 61  er.  When it rea
15f47 63 68 65 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65  ches zero,.** re
15f48 6d 6f 76 65 20 74 68 65 20 42 74 53 68 61 72 65  move the BtShare
15f49 64 20 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d  d structure from
15f4a 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73   the sharing lis
15f4b 74 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72  t.  Return.** tr
15f4c 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72  ue if the BtShar
15f4d 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 20  ed.nRef counter 
15f4e 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 6e 64  reaches zero and
15f4f 20 72 65 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65   return.** false
15f50 20 69 66 20 69 74 20 69 73 20 73 74 69 6c 6c 20   if it is still 
15f51 70 6f 73 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61  positive..*/.sta
15f52 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 46 72  tic int removeFr
15f53 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 42 74  omSharingList(Bt
15f54 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 23 69  Shared *pBt){.#i
15f55 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15f56 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
15f57 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
15f58 70 4d 61 73 74 65 72 3b 0a 20 20 42 74 53 68 61  pMaster;.  BtSha
15f59 72 65 64 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e  red *pList;.  in
15f5a 74 20 72 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a  t removed = 0;..
15f5b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15f5c 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
15f5d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
15f5e 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74   pMaster = sqlit
15f5f 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
15f60 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
15f61 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c 69  _MASTER);.  sqli
15f62 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
15f63 70 4d 61 73 74 65 72 29 3b 0a 20 20 70 42 74 2d  pMaster);.  pBt-
15f64 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70  >nRef--;.  if( p
15f65 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 29 7b 0a 20  Bt->nRef<=0 ){. 
15f66 20 20 20 69 66 28 20 47 4c 4f 42 41 4c 28 42 74     if( GLOBAL(Bt
15f67 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
15f68 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3d  haredCacheList)=
15f69 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 47 4c  =pBt ){.      GL
15f6a 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73  OBAL(BtShared*,s
15f6b 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68  qlite3SharedCach
15f6c 65 4c 69 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e  eList) = pBt->pN
15f6d 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ext;.    }else{.
15f6e 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 47 4c        pList = GL
15f6f 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73  OBAL(BtShared*,s
15f70 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68  qlite3SharedCach
15f71 65 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 77 68  eList);.      wh
15f72 69 6c 65 28 20 41 4c 57 41 59 53 28 70 4c 69 73  ile( ALWAYS(pLis
15f73 74 29 20 26 26 20 70 4c 69 73 74 2d 3e 70 4e 65  t) && pList->pNe
15f74 78 74 21 3d 70 42 74 20 29 7b 0a 20 20 20 20 20  xt!=pBt ){.     
15f75 20 20 20 70 4c 69 73 74 3d 70 4c 69 73 74 2d 3e     pList=pList->
15f76 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
15f77 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
15f78 70 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20  pList) ){.      
15f79 20 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d    pList->pNext =
15f7a 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   pBt->pNext;.   
15f7b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
15f7c 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  f( SQLITE_THREAD
15f7d 53 41 46 45 20 29 7b 0a 20 20 20 20 20 20 73 71  SAFE ){.      sq
15f7e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
15f7f 28 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  (pBt->mutex);.  
15f80 20 20 7d 0a 20 20 20 20 72 65 6d 6f 76 65 64 20    }.    removed 
15f81 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 1;.  }.  sqlit
15f82 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
15f83 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72  Master);.  retur
15f84 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 65 6c 73 65  n removed;.#else
15f85 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e  .  return 1;.#en
15f86 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  dif.}../*.** Mak
15f87 65 20 73 75 72 65 20 70 42 74 2d 3e 70 54 6d 70  e sure pBt->pTmp
15f88 53 70 61 63 65 20 70 6f 69 6e 74 73 20 74 6f 20  Space points to 
15f89 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66  an allocation of
15f8a 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a   .** MX_CELL_SIZ
15f8b 45 28 70 42 74 29 20 62 79 74 65 73 2e 0a 2a 2f  E(pBt) bytes..*/
15f8c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 6c 6c  .static void all
15f8d 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 42  ocateTempSpace(B
15f8e 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
15f8f 20 69 66 28 20 21 70 42 74 2d 3e 70 54 6d 70 53   if( !pBt->pTmpS
15f90 70 61 63 65 20 29 7b 0a 20 20 20 20 70 42 74 2d  pace ){.    pBt-
15f91 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 73 71 6c  >pTmpSpace = sql
15f92 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20  ite3PageMalloc( 
15f93 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b  pBt->pageSize );
15f94 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  .  }.}../*.** Fr
15f95 65 65 20 74 68 65 20 70 42 74 2d 3e 70 54 6d 70  ee the pBt->pTmp
15f96 53 70 61 63 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  Space allocation
15f97 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
15f98 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 42 74  freeTempSpace(Bt
15f99 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
15f9a 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
15f9b 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 29   pBt->pTmpSpace)
15f9c 3b 0a 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61  ;.  pBt->pTmpSpa
15f9d 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ce = 0;.}../*.**
15f9e 20 43 6c 6f 73 65 20 61 6e 20 6f 70 65 6e 20 64   Close an open d
15f9f 61 74 61 62 61 73 65 20 61 6e 64 20 69 6e 76 61  atabase and inva
15fa0 6c 69 64 61 74 65 20 61 6c 6c 20 63 75 72 73 6f  lidate all curso
15fa1 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rs..*/.SQLITE_PR
15fa2 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15fa3 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65  3BtreeClose(Btre
15fa4 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65  e *p){.  BtShare
15fa5 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
15fa6 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
15fa7 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61  r;..  /* Close a
15fa8 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65  ll cursors opene
15fa9 64 20 76 69 61 20 74 68 69 73 20 68 61 6e 64 6c  d via this handl
15faa 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  e.  */.  assert(
15fab 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15fac 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
15fad 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ) );.  sqlite3Bt
15fae 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
15faf 43 75 72 20 3d 20 70 42 74 2d 3e 70 43 75 72 73  Cur = pBt->pCurs
15fb0 6f 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 75  or;.  while( pCu
15fb1 72 20 29 7b 0a 20 20 20 20 42 74 43 75 72 73 6f  r ){.    BtCurso
15fb2 72 20 2a 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a  r *pTmp = pCur;.
15fb3 20 20 20 20 70 43 75 72 20 3d 20 70 43 75 72 2d      pCur = pCur-
15fb4 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20  >pNext;.    if( 
15fb5 70 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20  pTmp->pBtree==p 
15fb6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
15fb7 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72  BtreeCloseCursor
15fb8 28 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20  (pTmp);.    }.  
15fb9 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b  }..  /* Rollback
15fba 20 61 6e 79 20 61 63 74 69 76 65 20 74 72 61 6e   any active tran
15fbb 73 61 63 74 69 6f 6e 20 61 6e 64 20 66 72 65 65  saction and free
15fbc 20 74 68 65 20 68 61 6e 64 6c 65 20 73 74 72 75   the handle stru
15fbd 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20  cture..  ** The 
15fbe 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42  call to sqlite3B
15fbf 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64  treeRollback() d
15fc0 72 6f 70 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c  rops any table-l
15fc1 6f 63 6b 73 20 68 65 6c 64 20 62 79 0a 20 20 2a  ocks held by.  *
15fc2 2a 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20  * this handle.. 
15fc3 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72   */.  sqlite3Btr
15fc4 65 65 52 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20  eeRollback(p);. 
15fc5 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
15fc6 76 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  ve(p);..  /* If 
15fc7 74 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20  there are still 
15fc8 6f 74 68 65 72 20 6f 75 74 73 74 61 6e 64 69 6e  other outstandin
15fc9 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  g references to 
15fca 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  the shared-btree
15fcb 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 2c  .  ** structure,
15fcc 20 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65   return now. The
15fcd 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68   remainder of th
15fce 69 73 20 70 72 6f 63 65 64 75 72 65 20 63 6c 65  is procedure cle
15fcf 61 6e 73 20 0a 20 20 2a 2a 20 75 70 20 74 68 65  ans .  ** up the
15fd0 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e 0a 20   shared-btree.. 
15fd1 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
15fd2 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26  >wantToLock==0 &
15fd3 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29  & p->locked==0 )
15fd4 3b 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72  ;.  if( !p->shar
15fd5 61 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72  able || removeFr
15fd6 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 70 42  omSharingList(pB
15fd7 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  t) ){.    /* The
15fd8 20 70 42 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65   pBt is no longe
15fd9 72 20 6f 6e 20 74 68 65 20 73 68 61 72 69 6e 67  r on the sharing
15fda 20 6c 69 73 74 2c 20 73 6f 20 77 65 20 63 61 6e   list, so we can
15fdb 20 61 63 63 65 73 73 0a 20 20 20 20 2a 2a 20 69   access.    ** i
15fdc 74 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67  t without having
15fdd 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74   to hold the mut
15fde 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ex..    **.    *
15fdf 2a 20 43 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20  * Clean out and 
15fe0 64 65 6c 65 74 65 20 74 68 65 20 42 74 53 68 61  delete the BtSha
15fe1 72 65 64 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20  red object..    
15fe2 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  */.    assert( !
15fe3 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a  pBt->pCursor );.
15fe4 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
15fe5 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65  Close(pBt->pPage
15fe6 72 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 2d  r);.    if( pBt-
15fe7 3e 78 46 72 65 65 53 63 68 65 6d 61 20 26 26 20  >xFreeSchema && 
15fe8 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a  pBt->pSchema ){.
15fe9 20 20 20 20 20 20 70 42 74 2d 3e 78 46 72 65 65        pBt->xFree
15fea 53 63 68 65 6d 61 28 70 42 74 2d 3e 70 53 63 68  Schema(pBt->pSch
15feb 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ema);.    }.    
15fec 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74  sqlite3_free(pBt
15fed 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ->pSchema);.    
15fee 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42  freeTempSpace(pB
15fef 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
15ff0 66 72 65 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a  free(pBt);.  }..
15ff1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15ff2 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
15ff3 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61  .  assert( p->wa
15ff4 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20  ntToLock==0 );. 
15ff5 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b   assert( p->lock
15ff6 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  ed==0 );.  if( p
15ff7 2d 3e 70 50 72 65 76 20 29 20 70 2d 3e 70 50 72  ->pPrev ) p->pPr
15ff8 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70  ev->pNext = p->p
15ff9 4e 65 78 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70  Next;.  if( p->p
15ffa 4e 65 78 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d  Next ) p->pNext-
15ffb 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65  >pPrev = p->pPre
15ffc 76 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c  v;.#endif..  sql
15ffd 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
15ffe 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15fff 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
16000 65 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74  e the limit on t
16001 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
16002 65 73 20 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68  es allowed in th
16003 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54  e cache..**.** T
16004 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
16005 72 20 6f 66 20 63 61 63 68 65 20 70 61 67 65 73  r of cache pages
16006 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 61   is set to the a
16007 62 73 6f 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65  bsolute.** value
16008 20 6f 66 20 6d 78 50 61 67 65 2e 20 20 49 66 20   of mxPage.  If 
16009 6d 78 50 61 67 65 20 69 73 20 6e 65 67 61 74 69  mxPage is negati
1600a 76 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 69  ve, the pager wi
1600b 6c 6c 0a 2a 2a 20 6f 70 65 72 61 74 65 20 61 73  ll.** operate as
1600c 79 6e 63 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69  ynchronously - i
1600d 74 20 77 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20  t will not stop 
1600e 74 6f 20 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a  to do fsync()s.*
1600f 2a 20 74 6f 20 69 6e 73 75 72 65 20 64 61 74 61  * to insure data
16010 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74   is written to t
16011 68 65 20 64 69 73 6b 20 73 75 72 66 61 63 65 20  he disk surface 
16012 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e  before.** contin
16013 75 69 6e 67 2e 20 20 54 72 61 6e 73 61 63 74 69  uing.  Transacti
16014 6f 6e 73 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69  ons still work i
16015 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  f synchronous is
16016 20 6f 66 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65   off,.** and the
16017 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74   database cannot
16018 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 69 66   be corrupted if
16019 20 74 68 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a   this program.**
1601a 20 63 72 61 73 68 65 73 2e 20 20 42 75 74 20 69   crashes.  But i
1601b 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  f the operating 
1601c 73 79 73 74 65 6d 20 63 72 61 73 68 65 73 20 6f  system crashes o
1601d 72 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e  r there is.** an
1601e 20 61 62 72 75 70 74 20 70 6f 77 65 72 20 66 61   abrupt power fa
1601f 69 6c 75 72 65 20 77 68 65 6e 20 73 79 6e 63 68  ilure when synch
16020 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 20 74  ronous is off, t
16021 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  he database.** c
16022 6f 75 6c 64 20 62 65 20 6c 65 66 74 20 69 6e 20  ould be left in 
16023 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20  an inconsistent 
16024 61 6e 64 20 75 6e 72 65 63 6f 76 65 72 61 62 6c  and unrecoverabl
16025 65 20 73 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63  e state..** Sync
16026 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79  hronous is on by
16027 20 64 65 66 61 75 6c 74 20 73 6f 20 64 61 74 61   default so data
16028 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
16029 69 73 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c  is not.** normal
1602a 6c 79 20 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53  ly a worry..*/.S
1602b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1602c 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
1602d 74 43 61 63 68 65 53 69 7a 65 28 42 74 72 65 65  tCacheSize(Btree
1602e 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29   *p, int mxPage)
1602f 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
16030 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73  t = p->pBt;.  as
16031 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
16032 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
16033 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
16034 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
16035 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ;.  sqlite3Pager
16036 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 42 74  SetCachesize(pBt
16037 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65  ->pPager, mxPage
16038 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
16039 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
1603a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1603b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
1603c 68 65 20 77 61 79 20 64 61 74 61 20 69 73 20 73  he way data is s
1603d 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 69 6e  ynced to disk in
1603e 20 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 65 61   order to increa
1603f 73 65 20 6f 72 20 64 65 63 72 65 61 73 65 0a 2a  se or decrease.*
16040 2a 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20 64  * how well the d
16041 61 74 61 62 61 73 65 20 72 65 73 69 73 74 73 20  atabase resists 
16042 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53  damage due to OS
16043 20 63 72 61 73 68 65 73 20 61 6e 64 20 70 6f 77   crashes and pow
16044 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20  er.** failures. 
16045 20 4c 65 76 65 6c 20 31 20 69 73 20 74 68 65 20   Level 1 is the 
16046 73 61 6d 65 20 61 73 20 61 73 79 6e 63 68 72 6f  same as asynchro
16047 6e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 28 29  nous (no syncs()
16048 20 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68   occur and.** th
16049 65 72 65 20 69 73 20 61 20 68 69 67 68 20 70 72  ere is a high pr
1604a 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d  obability of dam
1604b 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20 69 73  age)  Level 2 is
1604c 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54   the default.  T
1604d 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 65 72  here.** is a ver
1604e 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65  y low but non-ze
1604f 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f  ro probability o
16050 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c  f damage.  Level
16051 20 33 20 72 65 64 75 63 65 73 20 74 68 65 0a 2a   3 reduces the.*
16052 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66  * probability of
16053 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 72 20   damage to near 
16054 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20 61 20  zero but with a 
16055 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61 6e 63  write performanc
16056 65 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  e reduction..*/.
16057 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16058 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
16059 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  S.SQLITE_PRIVATE
1605a 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1605b 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28  eSetSafetyLevel(
1605c 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65  Btree *p, int le
1605d 76 65 6c 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e  vel, int fullSyn
1605e 63 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  c){.  BtShared *
1605f 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
16060 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16061 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
16062 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
16063 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
16064 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67  p);.  sqlite3Pag
16065 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c  erSetSafetyLevel
16066 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65  (pBt->pPager, le
16067 76 65 6c 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a  vel, fullSync);.
16068 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
16069 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1606a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
1606b 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
1606c 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67  rn TRUE if the g
1606d 69 76 65 6e 20 62 74 72 65 65 20 69 73 20 73 65  iven btree is se
1606e 74 20 74 6f 20 73 61 66 65 74 79 20 6c 65 76 65  t to safety leve
1606f 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a  l 1.  In other.*
16070 2a 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20  * words, return 
16071 54 52 55 45 20 69 66 20 6e 6f 20 73 79 6e 63 28  TRUE if no sync(
16072 29 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20  ) occurs on the 
16073 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53  disk files..*/.S
16074 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16075 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79  t sqlite3BtreeSy
16076 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65  ncDisabled(Btree
16077 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64   *p){.  BtShared
16078 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
16079 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
1607a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1607b 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
1607c 74 65 78 29 20 29 3b 20 20 0a 20 20 73 71 6c 69  tex) );  .  sqli
1607d 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
1607e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 20  ;.  assert( pBt 
1607f 26 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29  && pBt->pPager )
16080 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
16081 50 61 67 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d  PagerNosync(pBt-
16082 3e 70 50 61 67 65 72 29 3b 0a 20 20 73 71 6c 69  >pPager);.  sqli
16083 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
16084 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
16085 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
16086 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
16087 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65  _PRAGMAS) || !de
16088 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
16089 54 5f 56 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20  T_VACUUM)./*.** 
1608a 43 68 61 6e 67 65 20 74 68 65 20 64 65 66 61 75  Change the defau
1608b 6c 74 20 70 61 67 65 73 20 73 69 7a 65 20 61 6e  lt pages size an
1608c 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  d the number of 
1608d 72 65 73 65 72 76 65 64 20 62 79 74 65 73 20 70  reserved bytes p
1608e 65 72 20 70 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20  er page..** Or, 
1608f 69 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  if the page size
16090 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
16091 6e 20 66 69 78 65 64 2c 20 72 65 74 75 72 6e 20  n fixed, return 
16092 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20  SQLITE_READONLY 
16093 0a 2a 2a 20 77 69 74 68 6f 75 74 20 63 68 61 6e  .** without chan
16094 67 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 0a 2a  ging anything..*
16095 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 73 69  *.** The page si
16096 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77  ze must be a pow
16097 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e 20  er of 2 between 
16098 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e 20 20  512 and 65536.  
16099 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 73  If the page.** s
1609a 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64 6f 65  ize supplied doe
1609b 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 73 20  s not meet this 
1609c 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 6e 20  constraint then 
1609d 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73  the page size is
1609e 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e   not.** changed.
1609f 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65  .**.** Page size
160a0 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e 65  s are constraine
160a1 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 20  d to be a power 
160a2 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20 74  of two so that t
160a3 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20  he region.** of 
160a4 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
160a5 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69  e used for locki
160a6 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61 74  ng (beginning at
160a7 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a   PENDING_BYTE,.*
160a8 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  * the first byte
160a9 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62 6f   past the 1GB bo
160aa 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30 30 30  undary, 0x400000
160ab 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 63 63  00) needs to occ
160ac 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 65 67  ur.** at the beg
160ad 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 67 65  inning of a page
160ae 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d  ..**.** If param
160af 65 74 65 72 20 6e 52 65 73 65 72 76 65 20 69 73  eter nReserve is
160b0 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
160b1 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72   then the number
160b2 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a 2a 20   of reserved.** 
160b3 62 79 74 65 73 20 70 65 72 20 70 61 67 65 20 69  bytes per page i
160b4 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64  s left unchanged
160b5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ..**.** If the i
160b6 46 69 78 21 3d 30 20 74 68 65 6e 20 74 68 65 20  Fix!=0 then the 
160b7 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 66 6c  pageSizeFixed fl
160b8 61 67 20 69 73 20 73 65 74 20 73 6f 20 74 68 61  ag is set so tha
160b9 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 0a  t the page size.
160ba 2a 2a 20 61 6e 64 20 61 75 74 6f 76 61 63 75 75  ** and autovacuu
160bb 6d 20 6d 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f  m mode can no lo
160bc 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 2e  nger be changed.
160bd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
160be 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
160bf 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42  reeSetPageSize(B
160c0 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 61 67  tree *p, int pag
160c1 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65  eSize, int nRese
160c2 72 76 65 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a  rve, int iFix){.
160c3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
160c4 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64  E_OK;.  BtShared
160c5 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
160c6 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72    assert( nReser
160c7 76 65 3e 3d 2d 31 20 26 26 20 6e 52 65 73 65 72  ve>=-1 && nReser
160c8 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c  ve<=255 );.  sql
160c9 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
160ca 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 61  );.  if( pBt->pa
160cb 67 65 53 69 7a 65 46 69 78 65 64 20 29 7b 0a 20  geSizeFixed ){. 
160cc 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
160cd 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74  eave(p);.    ret
160ce 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
160cf 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  NLY;.  }.  if( n
160d0 52 65 73 65 72 76 65 3c 30 20 29 7b 0a 20 20 20  Reserve<0 ){.   
160d1 20 6e 52 65 73 65 72 76 65 20 3d 20 70 42 74 2d   nReserve = pBt-
160d2 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d  >pageSize - pBt-
160d3 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d  >usableSize;.  }
160d4 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65  .  assert( nRese
160d5 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72  rve>=0 && nReser
160d6 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 69 66 28  ve<=255 );.  if(
160d7 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26   pageSize>=512 &
160d8 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49  & pageSize<=SQLI
160d9 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
160da 20 26 26 0a 20 20 20 20 20 20 20 20 28 28 70 61   &&.        ((pa
160db 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69  geSize-1)&pageSi
160dc 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  ze)==0 ){.    as
160dd 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20  sert( (pageSize 
160de 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61  & 7)==0 );.    a
160df 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 50 61  ssert( !pBt->pPa
160e0 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70 43 75  ge1 && !pBt->pCu
160e1 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42 74 2d  rsor );.    pBt-
160e2 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36  >pageSize = (u16
160e3 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66  )pageSize;.    f
160e4 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 74  reeTempSpace(pBt
160e5 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  );.  }.  rc = sq
160e6 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
160e7 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65  esize(pBt->pPage
160e8 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a  r, &pBt->pageSiz
160e9 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20  e, nReserve);.  
160ea 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
160eb 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  = pBt->pageSize 
160ec 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b  - (u16)nReserve;
160ed 0a 20 20 69 66 28 20 69 46 69 78 20 29 20 70 42  .  if( iFix ) pB
160ee 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64  t->pageSizeFixed
160ef 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 42   = 1;.  sqlite3B
160f0 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
160f1 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
160f2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63  .** Return the c
160f3 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64  urrently defined
160f4 20 70 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51   page size.*/.SQ
160f5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
160f6 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
160f7 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a  PageSize(Btree *
160f8 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p){.  return p->
160f9 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d  pBt->pageSize;.}
160fa 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
160fb 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
160fc 65 73 20 6f 66 20 73 70 61 63 65 20 61 74 20 74  es of space at t
160fd 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20  he end of every 
160fe 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 61 72 65  page that.** are
160ff 20 69 6e 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66   intentually lef
16100 74 20 75 6e 75 73 65 64 2e 20 20 54 68 69 73 20  t unused.  This 
16101 69 73 20 74 68 65 20 22 72 65 73 65 72 76 65 64  is the "reserved
16102 22 20 73 70 61 63 65 20 74 68 61 74 20 69 73 0a  " space that is.
16103 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 75 73 65  ** sometimes use
16104 64 20 62 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e  d by extensions.
16105 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
16106 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
16107 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74  reeGetReserve(Bt
16108 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  ree *p){.  int n
16109 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1610a 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20  Enter(p);.  n = 
1610b 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  p->pBt->pageSize
1610c 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c   - p->pBt->usabl
1610d 65 53 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33  eSize;.  sqlite3
1610e 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1610f 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a   return n;.}../*
16110 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69  .** Set the maxi
16111 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66  mum page count f
16112 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 69 66  or a database if
16113 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 74   mxPage is posit
16114 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67  ive..** No chang
16115 65 73 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d  es are made if m
16116 78 50 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65  xPage is 0 or ne
16117 67 61 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72  gative..** Regar
16118 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c  dless of the val
16119 75 65 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65  ue of mxPage, re
1611a 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  turn the maximum
1611b 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a   page count..*/.
1611c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1611d 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
1611e 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65  axPageCount(Btre
1611f 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65  e *p, int mxPage
16120 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71  ){.  int n;.  sq
16121 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
16122 70 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65  p);.  n = sqlite
16123 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75  3PagerMaxPageCou
16124 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  nt(p->pBt->pPage
16125 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71  r, mxPage);.  sq
16126 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
16127 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a  p);.  return n;.
16128 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
16129 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1612a 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20  _PAGER_PRAGMAS) 
1612b 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
1612c 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20  TE_OMIT_VACUUM) 
1612d 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  */../*.** Change
1612e 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75   the 'auto-vacuu
1612f 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74  m' property of t
16130 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 20  he database. If 
16131 74 68 65 20 27 61 75 74 6f 56 61 63 75 75 6d 27  the 'autoVacuum'
16132 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73  .** parameter is
16133 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
16134 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
16135 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 20   is enabled. If 
16136 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64  zero, it.** is d
16137 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 65 66  isabled. The def
16138 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
16139 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 70  he auto-vacuum p
1613a 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 64  roperty is .** d
1613b 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65  etermined by the
1613c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
1613d 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f  AUTOVACUUM macro
1613e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1613f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
16140 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75  treeSetAutoVacuu
16141 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  m(Btree *p, int 
16142 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66  autoVacuum){.#if
16143 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
16144 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74  AUTOVACUUM.  ret
16145 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
16146 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53  NLY;.#else.  BtS
16147 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
16148 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  pBt;.  int rc = 
16149 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20  SQLITE_OK;.  u8 
1614a 61 76 20 3d 20 28 75 38 29 61 75 74 6f 56 61 63  av = (u8)autoVac
1614b 75 75 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  uum;..  sqlite3B
1614c 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
1614d 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  if( pBt->pageSiz
1614e 65 46 69 78 65 64 20 26 26 20 28 61 76 20 3f 31  eFixed && (av ?1
1614f 3a 30 29 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61  :0)!=pBt->autoVa
16150 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d  cuum ){.    rc =
16151 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
16152 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
16153 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d  Bt->autoVacuum =
16154 20 61 76 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42   av ?1:0;.    pB
16155 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
16156 61 76 3d 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a  av==2 ?1:0;.  }.
16157 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
16158 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
16159 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f   rc;.#endif.}../
1615a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1615b 76 61 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75  value of the 'au
1615c 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65  to-vacuum' prope
1615d 72 74 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63  rty. If auto-vac
1615e 75 75 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c  uum is .** enabl
1615f 65 64 20 31 20 69 73 20 72 65 74 75 72 6e 65 64  ed 1 is returned
16160 2e 20 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a  . Otherwise 0..*
16161 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16162 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16163 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42  eGetAutoVacuum(B
16164 74 72 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66  tree *p){.#ifdef
16165 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
16166 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e  OVACUUM.  return
16167 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
16168 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20  M_NONE;.#else.  
16169 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
1616a 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
1616b 20 20 72 63 20 3d 20 28 0a 20 20 20 20 28 21 70    rc = (.    (!p
1616c 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ->pBt->autoVacuu
1616d 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43  m)?BTREE_AUTOVAC
1616e 55 55 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21  UUM_NONE:.    (!
1616f 70 2d 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  p->pBt->incrVacu
16170 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41  um)?BTREE_AUTOVA
16171 43 55 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42  CUUM_FULL:.    B
16172 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
16173 49 4e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69  INCR.  );.  sqli
16174 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
16175 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23  ;.  return rc;.#
16176 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  endif.}.../*.** 
16177 47 65 74 20 61 20 72 65 66 65 72 65 6e 63 65 20  Get a reference 
16178 74 6f 20 70 50 61 67 65 31 20 6f 66 20 74 68 65  to pPage1 of the
16179 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1617a 20 54 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c   This will.** al
1617b 73 6f 20 61 63 71 75 69 72 65 20 61 20 72 65 61  so acquire a rea
1617c 64 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 69  dlock on that fi
1617d 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  le..**.** SQLITE
1617e 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
1617f 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20  on success.  If 
16180 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20  the file is not 
16181 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  a.** well-formed
16182 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
16183 74 68 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  then SQLITE_CORR
16184 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e  UPT is returned.
16185 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20  .** SQLITE_BUSY 
16186 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
16187 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c  he database is l
16188 6f 63 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e  ocked.  SQLITE_N
16189 4f 4d 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72  OMEM.** is retur
1618a 6e 65 64 20 69 66 20 77 65 20 72 75 6e 20 6f 75  ned if we run ou
1618b 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f  t of memory. .*/
1618c 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b  .static int lock
1618d 42 74 72 65 65 28 42 74 53 68 61 72 65 64 20 2a  Btree(BtShared *
1618e 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  pBt){.  int rc;.
1618f 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
16190 31 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a  1;.  int nPage;.
16191 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16192 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
16193 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
16194 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67  ssert( pBt->pPag
16195 65 31 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20  e1==0 );.  rc = 
16196 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 61 72  sqlite3PagerShar
16197 65 64 4c 6f 63 6b 28 70 42 74 2d 3e 70 50 61 67  edLock(pBt->pPag
16198 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  er);.  if( rc!=S
16199 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
1619a 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 62 74 72  n rc;.  rc = btr
1619b 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31  eeGetPage(pBt, 1
1619c 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3b 0a 20  , &pPage1, 0);. 
1619d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1619e 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
1619f 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68  .  /* Do some ch
161a0 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70 20 69  ecking to help i
161a1 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65 20 77  nsure the file w
161a2 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c 79 20  e opened really 
161a3 69 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69 64 20  is.  ** a valid 
161a4 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a  database file. .
161a5 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
161a6 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
161a7 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26  t(pBt->pPager, &
161a8 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  nPage);.  if( rc
161a9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
161aa 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e     goto page1_in
161ab 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 65 6c  it_failed;.  }el
161ac 73 65 20 69 66 28 20 6e 50 61 67 65 3e 30 20 29  se if( nPage>0 )
161ad 7b 0a 20 20 20 20 69 6e 74 20 70 61 67 65 53 69  {.    int pageSi
161ae 7a 65 3b 0a 20 20 20 20 69 6e 74 20 75 73 61 62  ze;.    int usab
161af 6c 65 53 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a  leSize;.    u8 *
161b0 70 61 67 65 31 20 3d 20 70 50 61 67 65 31 2d 3e  page1 = pPage1->
161b1 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20 3d 20  aData;.    rc = 
161b2 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20  SQLITE_NOTADB;. 
161b3 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 61     if( memcmp(pa
161b4 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 61 64 65  ge1, zMagicHeade
161b5 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20 20 20  r, 16)!=0 ){.   
161b6 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e     goto page1_in
161b7 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d  it_failed;.    }
161b8 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31  .    if( page1[1
161b9 38 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 70 42  8]>1 ){.      pB
161ba 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b  t->readOnly = 1;
161bb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
161bc 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a 20 20  age1[19]>1 ){.  
161bd 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69      goto page1_i
161be 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20  nit_failed;.    
161bf 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61  }..    /* The ma
161c0 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 20 66  ximum embedded f
161c1 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  raction must be 
161c2 65 78 61 63 74 6c 79 20 32 35 25 2e 20 20 41 6e  exactly 25%.  An
161c3 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20  d the minimum.  
161c4 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 20 66 72    ** embedded fr
161c5 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 31  action must be 1
161c6 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 20 6c 65  2.5% for both le
161c7 61 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d  af-data and non-
161c8 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a  leaf-data..    *
161c9 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  * The original d
161ca 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68  esign allowed th
161cb 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76  ese amounts to v
161cc 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66 0a 20  ary, but as of. 
161cd 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e     ** version 3.
161ce 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72 65 20  6.0, we require 
161cf 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78 65 64  them to be fixed
161d0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
161d1 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 5b 32   memcmp(&page1[2
161d2 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c 30 34  1], "\100\040\04
161d3 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20  0",3)!=0 ){.    
161d4 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69    goto page1_ini
161d5 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a  t_failed;.    }.
161d6 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d 20 67      pageSize = g
161d7 65 74 32 62 79 74 65 28 26 70 61 67 65 31 5b 31  et2byte(&page1[1
161d8 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 28 28 70  6]);.    if( ((p
161d9 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53  ageSize-1)&pageS
161da 69 7a 65 29 21 3d 30 20 7c 7c 20 70 61 67 65 53  ize)!=0 || pageS
161db 69 7a 65 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20  ize<512 ||.     
161dc 20 20 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50     (SQLITE_MAX_P
161dd 41 47 45 5f 53 49 5a 45 3c 33 32 37 36 38 20 26  AGE_SIZE<32768 &
161de 26 20 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54  & pageSize>SQLIT
161df 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29  E_MAX_PAGE_SIZE)
161e0 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 67 6f  .    ){.      go
161e1 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61  to page1_init_fa
161e2 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  iled;.    }.    
161e3 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a  assert( (pageSiz
161e4 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20  e & 7)==0 );.   
161e5 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 61   usableSize = pa
161e6 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31 5b 32  geSize - page1[2
161e7 30 5d 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65  0];.    if( page
161e8 53 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67 65 53  Size!=pBt->pageS
161e9 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ize ){.      /* 
161ea 41 66 74 65 72 20 72 65 61 64 69 6e 67 20 74 68  After reading th
161eb 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20  e first page of 
161ec 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73  the database ass
161ed 75 6d 69 6e 67 20 61 20 70 61 67 65 20 73 69 7a  uming a page siz
161ee 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 42 74  e.      ** of Bt
161ef 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 2c  Shared.pageSize,
161f0 20 77 65 20 68 61 76 65 20 64 69 73 63 6f 76 65   we have discove
161f1 72 65 64 20 74 68 61 74 20 74 68 65 20 70 61 67  red that the pag
161f2 65 2d 73 69 7a 65 20 69 73 0a 20 20 20 20 20 20  e-size is.      
161f3 2a 2a 20 61 63 74 75 61 6c 6c 79 20 70 61 67 65  ** actually page
161f4 53 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65  Size. Unlock the
161f5 20 64 61 74 61 62 61 73 65 2c 20 6c 65 61 76 65   database, leave
161f6 20 70 42 74 2d 3e 70 50 61 67 65 31 20 61 74 0a   pBt->pPage1 at.
161f7 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e        ** zero an
161f8 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  d return SQLITE_
161f9 4f 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77  OK. The caller w
161fa 69 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20 66 75  ill call this fu
161fb 6e 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20  nction.      ** 
161fc 61 67 61 69 6e 20 77 69 74 68 20 74 68 65 20 63  again with the c
161fd 6f 72 72 65 63 74 20 70 61 67 65 2d 73 69 7a 65  orrect page-size
161fe 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
161ff 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
16200 67 65 31 29 3b 0a 20 20 20 20 20 20 70 42 74 2d  ge1);.      pBt-
16201 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75  >usableSize = (u
16202 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20  16)usableSize;. 
16203 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69       pBt->pageSi
16204 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69  ze = (u16)pageSi
16205 7a 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 65  ze;.      freeTe
16206 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20  mpSpace(pBt);.  
16207 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16208 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
16209 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70  (pBt->pPager, &p
1620a 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20  Bt->pageSize,.  
1620b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1620c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1620d 20 70 61 67 65 53 69 7a 65 2d 75 73 61 62 6c 65   pageSize-usable
1620e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 72 65 74  Size);.      ret
1620f 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
16210 20 20 69 66 28 20 75 73 61 62 6c 65 53 69 7a 65    if( usableSize
16211 3c 34 38 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  <480 ){.      go
16212 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61  to page1_init_fa
16213 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  iled;.    }.    
16214 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20  pBt->pageSize = 
16215 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20  (u16)pageSize;. 
16216 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69     pBt->usableSi
16217 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65  ze = (u16)usable
16218 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51  Size;.#ifndef SQ
16219 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1621a 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75  CUUM.    pBt->au
1621b 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34  toVacuum = (get4
1621c 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b  byte(&page1[36 +
1621d 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20   4*4])?1:0);.   
1621e 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d   pBt->incrVacuum
1621f 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61   = (get4byte(&pa
16220 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31  ge1[36 + 7*4])?1
16221 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  :0);.#endif.  }.
16222 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69  .  /* maxLocal i
16223 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d  s the maximum am
16224 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
16225 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79  to store locally
16226 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c   for.  ** a cell
16227 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20  .  Make sure it 
16228 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20  is small enough 
16229 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74  so that at least
1622a 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20   minFanout.  ** 
1622b 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66  cells can will f
1622c 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20  it on one page. 
1622d 20 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d   We assume a 10-
1622e 62 79 74 65 20 70 61 67 65 20 68 65 61 64 65 72  byte page header
1622f 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74  ..  ** Besides t
16230 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20  he payload, the 
16231 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a  cell must store:
16232 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65  .  **     2-byte
16233 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
16234 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d  cell.  **     4-
16235 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74  byte child point
16236 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79  er.  **     9-by
16237 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20  te nKey value.  
16238 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44  **     4-byte nD
16239 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20  ata value.  **  
1623a 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c     4-byte overfl
1623b 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a  ow page pointer.
1623c 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63    ** So a cell c
1623d 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62  onsists of a 2-b
1623e 79 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65  yte poiner, a he
1623f 61 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73  ader which is as
16240 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37   much as.  ** 17
16241 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74   bytes long, 0 t
16242 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79  o N bytes of pay
16243 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74  load, and an opt
16244 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65  ional 4 byte ove
16245 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20  rflow.  ** page 
16246 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20  pointer..  */.  
16247 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20  pBt->maxLocal = 
16248 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
16249 2d 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33  -12)*64/255 - 23
1624a 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61  ;.  pBt->minLoca
1624b 6c 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65  l = (pBt->usable
1624c 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20  Size-12)*32/255 
1624d 2d 20 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78  - 23;.  pBt->max
1624e 4c 65 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62  Leaf = pBt->usab
1624f 6c 65 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70  leSize - 35;.  p
16250 42 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70  Bt->minLeaf = (p
16251 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31  Bt->usableSize-1
16252 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a  2)*32/255 - 23;.
16253 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d    assert( pBt->m
16254 61 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d  axLeaf + 23 <= M
16255 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
16256 20 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65   );.  pBt->pPage
16257 31 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65  1 = pPage1;.  re
16258 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
16259 0a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c  .page1_init_fail
1625a 65 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67  ed:.  releasePag
1625b 65 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74  e(pPage1);.  pBt
1625c 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20  ->pPage1 = 0;.  
1625d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1625e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65  .** If there are
1625f 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
16260 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65 20 61  cursors and we a
16261 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69  re not in the mi
16262 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61  ddle.** of a tra
16263 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74 68 65  nsaction but the
16264 72 65 20 69 73 20 61 20 72 65 61 64 20 6c 6f 63  re is a read loc
16265 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
16266 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20  e, then.** this 
16267 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73 20 74  routine unrefs t
16268 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66  he first page of
16269 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1626a 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73  le which .** has
1626b 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 72   the effect of r
1626c 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72 65 61  eleasing the rea
1626d 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  d lock..**.** If
1626e 20 74 68 65 72 65 20 69 73 20 61 20 74 72 61 6e   there is a tran
1626f 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72  saction in progr
16270 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ess, this routin
16271 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
16272 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c  .static void unl
16273 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
16274 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b  (BtShared *pBt){
16275 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16276 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
16277 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
16278 73 73 65 72 74 28 20 70 42 74 2d 3e 70 43 75 72  ssert( pBt->pCur
16279 73 6f 72 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69  sor==0 || pBt->i
1627a 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41  nTransaction>TRA
1627b 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28  NS_NONE );.  if(
1627c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
1627d 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20  ion==TRANS_NONE 
1627e 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 21 3d  && pBt->pPage1!=
1627f 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
16280 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44   pBt->pPage1->aD
16281 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ata );.    asser
16282 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  t( sqlite3PagerR
16283 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  efcount(pBt->pPa
16284 67 65 72 29 3d 3d 31 20 29 3b 0a 20 20 20 20 61  ger)==1 );.    a
16285 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67  ssert( pBt->pPag
16286 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20  e1->aData );.   
16287 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 42 74   releasePage(pBt
16288 2d 3e 70 50 61 67 65 31 29 3b 0a 20 20 20 20 70  ->pPage1);.    p
16289 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a  Bt->pPage1 = 0;.
1628a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20    }.}../*.** If 
1628b 70 42 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  pBt points to an
1628c 20 65 6d 70 74 79 20 66 69 6c 65 20 74 68 65 6e   empty file then
1628d 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 65 6d   convert that em
1628e 70 74 79 20 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f  pty file.** into
1628f 20 61 20 6e 65 77 20 65 6d 70 74 79 20 64 61 74   a new empty dat
16290 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 61 6c  abase by initial
16291 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 74 20  izing the first 
16292 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 64  page of.** the d
16293 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74  atabase..*/.stat
16294 69 63 20 69 6e 74 20 6e 65 77 44 61 74 61 62 61  ic int newDataba
16295 73 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  se(BtShared *pBt
16296 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  ){.  MemPage *pP
16297 31 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  1;.  unsigned ch
16298 61 72 20 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20  ar *data;.  int 
16299 72 63 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b  rc;.  int nPage;
1629a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1629b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1629c 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1629d 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1629e 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61 64 79  size has already
1629f 20 62 65 65 6e 20 6d 65 61 73 75 72 65 64 20 61   been measured a
162a0 6e 64 20 63 61 63 68 65 64 2c 20 73 6f 20 66 61  nd cached, so fa
162a1 69 6c 75 72 65 0a 20 20 2a 2a 20 69 73 20 69 6d  ilure.  ** is im
162a2 70 6f 73 73 69 62 6c 65 20 68 65 72 65 2e 20 20  possible here.  
162a3 49 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  If the original 
162a4 73 69 7a 65 20 6d 65 61 73 75 72 65 6d 65 6e 74  size measurement
162a5 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 0a 20 20   failed, then.  
162a6 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 61 62  ** processing ab
162a7 6f 72 74 73 20 62 65 66 6f 72 65 20 65 6e 74 65  orts before ente
162a8 72 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  ring this routin
162a9 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  e. */.  rc = sql
162aa 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
162ab 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  nt(pBt->pPager, 
162ac 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 4e  &nPage);.  if( N
162ad 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f  EVER(rc!=SQLITE_
162ae 4f 4b 29 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29  OK) || nPage>0 )
162af 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
162b0 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20 70 42 74  .  }.  pP1 = pBt
162b1 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61 73 73 65  ->pPage1;.  asse
162b2 72 74 28 20 70 50 31 21 3d 30 20 29 3b 0a 20 20  rt( pP1!=0 );.  
162b3 64 61 74 61 20 3d 20 70 50 31 2d 3e 61 44 61 74  data = pP1->aDat
162b4 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  a;.  rc = sqlite
162b5 33 50 61 67 65 72 57 72 69 74 65 28 70 50 31 2d  3PagerWrite(pP1-
162b6 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28  >pDbPage);.  if(
162b7 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
162b8 0a 20 20 6d 65 6d 63 70 79 28 64 61 74 61 2c 20  .  memcpy(data, 
162b9 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 73 69  zMagicHeader, si
162ba 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65  zeof(zMagicHeade
162bb 72 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  r));.  assert( s
162bc 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64  izeof(zMagicHead
162bd 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74  er)==16 );.  put
162be 32 62 79 74 65 28 26 64 61 74 61 5b 31 36 5d 2c  2byte(&data[16],
162bf 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b   pBt->pageSize);
162c0 0a 20 20 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b  .  data[18] = 1;
162c1 0a 20 20 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b  .  data[19] = 1;
162c2 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
162c3 75 73 61 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d  usableSize<=pBt-
162c4 3e 70 61 67 65 53 69 7a 65 20 26 26 20 70 42 74  >pageSize && pBt
162c5 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2b 32 35 35  ->usableSize+255
162c6 3e 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29  >=pBt->pageSize)
162c7 3b 0a 20 20 64 61 74 61 5b 32 30 5d 20 3d 20 28  ;.  data[20] = (
162c8 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a  u8)(pBt->pageSiz
162c9 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e - pBt->usableS
162ca 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d  ize);.  data[21]
162cb 20 3d 20 36 34 3b 0a 20 20 64 61 74 61 5b 32 32   = 64;.  data[22
162cc 5d 20 3d 20 33 32 3b 0a 20 20 64 61 74 61 5b 32  ] = 32;.  data[2
162cd 33 5d 20 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65  3] = 32;.  memse
162ce 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20  t(&data[24], 0, 
162cf 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50  100-24);.  zeroP
162d0 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49 4e 54  age(pP1, PTF_INT
162d1 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46  KEY|PTF_LEAF|PTF
162d2 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 20 70  _LEAFDATA );.  p
162d3 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
162d4 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53  d = 1;.#ifndef S
162d5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
162d6 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74 28 20  ACUUM.  assert( 
162d7 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d  pBt->autoVacuum=
162d8 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56  =1 || pBt->autoV
162d9 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73  acuum==0 );.  as
162da 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 72 56  sert( pBt->incrV
162db 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d  acuum==1 || pBt-
162dc 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20 29  >incrVacuum==0 )
162dd 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61  ;.  put4byte(&da
162de 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42  ta[36 + 4*4], pB
162df 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a  t->autoVacuum);.
162e0 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61    put4byte(&data
162e1 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d  [36 + 7*4], pBt-
162e2 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65  >incrVacuum);.#e
162e3 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51  ndif.  return SQ
162e4 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
162e5 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61  * Attempt to sta
162e6 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63  rt a new transac
162e7 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72  tion. A write-tr
162e8 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20  ansaction.** is 
162e9 73 74 61 72 74 65 64 20 69 66 20 74 68 65 20 73  started if the s
162ea 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
162eb 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72  s nonzero, other
162ec 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20  wise a read-.** 
162ed 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66  transaction.  If
162ee 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
162ef 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72  ment is 2 or mor
162f0 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a  e and exclusive.
162f1 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
162f2 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69  s started, meani
162f3 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  ng that no other
162f4 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f   process is allo
162f5 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73  wed.** to access
162f6 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
162f7 41 20 70 72 65 65 78 69 73 74 69 6e 67 20 74 72  A preexisting tr
162f8 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f  ansaction may no
162f9 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64  t be.** upgraded
162fa 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79   to exclusive by
162fb 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f   calling this ro
162fc 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74  utine a second t
162fd 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63  ime - the.** exc
162fe 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e  lusivity flag on
162ff 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e  ly works for a n
16300 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  ew transaction..
16301 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72  **.** A write-tr
16302 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
16303 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65  e started before
16304 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20   attempting any 
16305 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74  .** changes to t
16306 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f  he database.  No
16307 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
16308 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a  ing routines .**
16309 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73   will work unles
1630a 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  s a transaction 
1630b 69 73 20 73 74 61 72 74 65 64 20 66 69 72 73 74  is started first
1630c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c  :.**.**      sql
1630d 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
1630e 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
1630f 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
16310 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20  eIndex().**     
16311 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
16312 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20  arTable().**    
16313 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72    sqlite3BtreeDr
16314 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20  opTable().**    
16315 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e    sqlite3BtreeIn
16316 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73  sert().**      s
16317 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
16318 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
16319 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
1631a 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ta().**.** If an
1631b 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74   initial attempt
1631c 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20   to acquire the 
1631d 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75  lock fails becau
1631e 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65  se of lock conte
1631f 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65  ntion.** and the
16320 20 64 61 74 61 62 61 73 65 20 77 61 73 20 70 72   database was pr
16321 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65  eviously unlocke
16322 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74  d, then invoke t
16323 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a  he busy handler.
16324 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ** if there is o
16325 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68 65 72  ne.  But if ther
16326 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  e was previously
16327 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f   a read-lock, do
16328 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74   not.** invoke t
16329 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  he busy handler 
1632a 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51  - just return SQ
1632b 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49  LITE_BUSY.  SQLI
1632c 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72  TE_BUSY is .** r
1632d 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65  eturned when the
1632e 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
1632f 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64  read-lock in ord
16330 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65  er to avoid a de
16331 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75  adlock..**.** Su
16332 70 70 6f 73 65 20 74 68 65 72 65 20 61 72 65 20  ppose there are 
16333 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20  two processes A 
16334 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20 61 20  and B.  A has a 
16335 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20  read lock and B 
16336 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65  has.** a reserve
16337 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73  d lock.  B tries
16338 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65   to promote to e
16339 78 63 6c 75 73 69 76 65 20 62 75 74 20 69 73 20  xclusive but is 
1633a 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a  blocked because.
1633b 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c  ** of A's read l
1633c 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f  ock.  A tries to
1633d 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65   promote to rese
1633e 72 76 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63  rved but is bloc
1633f 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65  ked by B..** One
16340 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66   or the other of
16341 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73   the two process
16342 65 73 20 6d 75 73 74 20 67 69 76 65 20 77 61 79  es must give way
16343 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20 62 65   or there can be
16344 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e  .** no progress.
16345 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53    By returning S
16346 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e  QLITE_BUSY and n
16347 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  ot invoking the 
16348 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  busy callback.**
16349 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20   when A already 
1634a 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c  has a read lock,
1634b 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20   we encourage A 
1634c 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c  to give up and l
1634d 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e  et B.** proceed.
1634e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1634f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
16350 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74  reeBeginTrans(Bt
16351 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c  ree *p, int wrfl
16352 61 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ag){.  sqlite3 *
16353 70 42 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74  pBlock = 0;.  Bt
16354 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
16355 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  >pBt;.  int rc =
16356 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73   SQLITE_OK;..  s
16357 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
16358 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e 74 65  (p);.  btreeInte
16359 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20  grity(p);..  /* 
1635a 49 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20  If the btree is 
1635b 61 6c 72 65 61 64 79 20 69 6e 20 61 20 77 72 69  already in a wri
1635c 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  te-transaction, 
1635d 6f 72 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c  or it.  ** is al
1635e 72 65 61 64 79 20 69 6e 20 61 20 72 65 61 64 2d  ready in a read-
1635f 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20  transaction and 
16360 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69  a read-transacti
16361 6f 6e 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 65  on.  ** is reque
16362 73 74 65 64 2c 20 74 68 69 73 20 69 73 20 61 20  sted, this is a 
16363 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66  no-op..  */.  if
16364 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
16365 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d  ANS_WRITE || (p-
16366 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
16367 52 45 41 44 20 26 26 20 21 77 72 66 6c 61 67 29  READ && !wrflag)
16368 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61   ){.    goto tra
16369 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20  ns_begun;.  }.. 
1636a 20 2f 2a 20 57 72 69 74 65 20 74 72 61 6e 73 61   /* Write transa
1636b 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70  ctions are not p
1636c 6f 73 73 69 62 6c 65 20 6f 6e 20 61 20 72 65 61  ossible on a rea
1636d 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20  d-only database 
1636e 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 72 65  */.  if( pBt->re
1636f 61 64 4f 6e 6c 79 20 26 26 20 77 72 66 6c 61 67  adOnly && wrflag
16370 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
16371 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
16372 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67    goto trans_beg
16373 75 6e 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  un;.  }..#ifndef
16374 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
16375 52 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49  RED_CACHE.  /* I
16376 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61  f another databa
16377 73 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6c  se handle has al
16378 72 65 61 64 79 20 6f 70 65 6e 65 64 20 61 20 77  ready opened a w
16379 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
1637a 20 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73   .  ** on this s
1637b 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 75  hared-btree stru
1637c 63 74 75 72 65 20 61 6e 64 20 61 20 73 65 63 6f  cture and a seco
1637d 6e 64 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  nd write transac
1637e 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71  tion is.  ** req
1637f 75 65 73 74 65 64 2c 20 72 65 74 75 72 6e 20 53  uested, return S
16380 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20  QLITE_LOCKED..  
16381 2a 2f 0a 20 20 69 66 28 20 28 77 72 66 6c 61 67  */.  if( (wrflag
16382 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73   && pBt->inTrans
16383 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
16384 49 54 45 29 20 7c 7c 20 70 42 74 2d 3e 69 73 50  ITE) || pBt->isP
16385 65 6e 64 69 6e 67 20 29 7b 0a 20 20 20 20 70 42  ending ){.    pB
16386 6c 6f 63 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69  lock = pBt->pWri
16387 74 65 72 2d 3e 64 62 3b 0a 20 20 7d 65 6c 73 65  ter->db;.  }else
16388 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20 29 7b   if( wrflag>1 ){
16389 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74  .    BtLock *pIt
1638a 65 72 3b 0a 20 20 20 20 66 6f 72 28 70 49 74 65  er;.    for(pIte
1638b 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49  r=pBt->pLock; pI
1638c 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72  ter; pIter=pIter
1638d 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
1638e 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65  if( pIter->pBtre
1638f 65 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 20 20  e!=p ){.        
16390 70 42 6c 6f 63 6b 20 3d 20 70 49 74 65 72 2d 3e  pBlock = pIter->
16391 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20  pBtree->db;.    
16392 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
16393 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69   }.    }.  }.  i
16394 66 28 20 70 42 6c 6f 63 6b 20 29 7b 0a 20 20 20  f( pBlock ){.   
16395 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
16396 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c  onBlocked(p->db,
16397 20 70 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63   pBlock);.    rc
16398 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44   = SQLITE_LOCKED
16399 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20  _SHAREDCACHE;.  
1639a 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67    goto trans_beg
1639b 75 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  un;.  }.#endif..
1639c 20 20 2f 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e    /* Any read-on
1639d 6c 79 20 6f 72 20 72 65 61 64 2d 77 72 69 74 65  ly or read-write
1639e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70   transaction imp
1639f 6c 69 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  lies a read-lock
163a0 20 6f 6e 20 0a 20 20 2a 2a 20 70 61 67 65 20 31   on .  ** page 1
163a1 2e 20 53 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68  . So if some oth
163a2 65 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  er shared-cache 
163a3 63 6c 69 65 6e 74 20 61 6c 72 65 61 64 79 20 68  client already h
163a4 61 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  as a write-lock 
163a5 0a 20 20 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c  .  ** on page 1,
163a6 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
163a7 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65   cannot be opene
163a8 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65  d. */.  rc = que
163a9 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
163aa 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52  leLock(p, MASTER
163ab 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b  _ROOT, READ_LOCK
163ac 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
163ad 4f 4b 21 3d 72 63 20 29 20 67 6f 74 6f 20 74 72  OK!=rc ) goto tr
163ae 61 6e 73 5f 62 65 67 75 6e 3b 0a 0a 20 20 64 6f  ans_begun;..  do
163af 20 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c   {.    /* Call l
163b0 6f 63 6b 42 74 72 65 65 28 29 20 75 6e 74 69 6c  ockBtree() until
163b1 20 65 69 74 68 65 72 20 70 42 74 2d 3e 70 50 61   either pBt->pPa
163b2 67 65 31 20 69 73 20 70 6f 70 75 6c 61 74 65 64  ge1 is populated
163b3 20 6f 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42   or.    ** lockB
163b4 74 72 65 65 28 29 20 72 65 74 75 72 6e 73 20 73  tree() returns s
163b5 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74  omething other t
163b6 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c  han SQLITE_OK. l
163b7 6f 63 6b 42 74 72 65 65 28 29 0a 20 20 20 20 2a  ockBtree().    *
163b8 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  * may return SQL
163b9 49 54 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65  ITE_OK but leave
163ba 20 70 42 74 2d 3e 70 50 61 67 65 31 20 73 65 74   pBt->pPage1 set
163bb 20 74 6f 20 30 20 69 66 20 61 66 74 65 72 0a 20   to 0 if after. 
163bc 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 61     ** reading pa
163bd 67 65 20 31 20 69 74 20 64 69 73 63 6f 76 65 72  ge 1 it discover
163be 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d  s that the page-
163bf 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
163c0 62 61 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c  base .    ** fil
163c1 65 20 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61  e is not pBt->pa
163c2 67 65 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 20  geSize. In this 
163c3 63 61 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 29  case lockBtree()
163c4 20 77 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 20   will update.   
163c5 20 2a 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 7a   ** pBt->pageSiz
163c6 65 20 74 6f 20 74 68 65 20 70 61 67 65 2d 73 69  e to the page-si
163c7 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f  ze of the file o
163c8 6e 20 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20  n disk..    */. 
163c9 20 20 20 77 68 69 6c 65 28 20 70 42 74 2d 3e 70     while( pBt->p
163ca 50 61 67 65 31 3d 3d 30 20 26 26 20 53 51 4c 49  Page1==0 && SQLI
163cb 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63  TE_OK==(rc = loc
163cc 6b 42 74 72 65 65 28 70 42 74 29 29 20 29 3b 0a  kBtree(pBt)) );.
163cd 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
163ce 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67  ITE_OK && wrflag
163cf 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42   ){.      if( pB
163d0 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20  t->readOnly ){. 
163d1 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
163d2 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20  TE_READONLY;.   
163d3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
163d4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
163d5 67 65 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50  gerBegin(pBt->pP
163d6 61 67 65 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71  ager,wrflag>1,sq
163d7 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72  lite3TempInMemor
163d8 79 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20  y(p->db));.     
163d9 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
163da 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
163db 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61    rc = newDataba
163dc 73 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20  se(pBt);.       
163dd 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
163de 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d  .  .    if( rc!=
163df 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
163e0 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66     unlockBtreeIf
163e1 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20  Unused(pBt);.   
163e2 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d   }.  }while( rc=
163e3 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20  =SQLITE_BUSY && 
163e4 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
163e5 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26  on==TRANS_NONE &
163e6 26 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 65  &.          btre
163e7 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c  eInvokeBusyHandl
163e8 65 72 28 70 42 74 29 20 29 3b 0a 0a 20 20 69 66  er(pBt) );..  if
163e9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
163ea 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e  ){.    if( p->in
163eb 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e  Trans==TRANS_NON
163ec 45 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  E ){.      pBt->
163ed 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a  nTransaction++;.
163ee 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
163ef 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
163f0 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 73 68  .      if( p->sh
163f1 61 72 61 62 6c 65 20 29 7b 0a 09 61 73 73 65 72  arable ){..asser
163f2 74 28 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65  t( p->lock.pBtre
163f3 65 3d 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e  e==p && p->lock.
163f4 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20  iTable==1 );.   
163f5 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f       p->lock.eLo
163f6 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a  ck = READ_LOCK;.
163f7 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e          p->lock.
163f8 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f  pNext = pBt->pLo
163f9 63 6b 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d  ck;.        pBt-
163fa 3e 70 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63  >pLock = &p->loc
163fb 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  k;.      }.#endi
163fc 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69  f.    }.    p->i
163fd 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67  nTrans = (wrflag
163fe 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41  ?TRANS_WRITE:TRA
163ff 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66  NS_READ);.    if
16400 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74  ( p->inTrans>pBt
16401 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20  ->inTransaction 
16402 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e  ){.      pBt->in
16403 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d  Transaction = p-
16404 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a  >inTrans;.    }.
16405 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16406 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
16407 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61 67 20  .    if( wrflag 
16408 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
16409 20 21 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29   !pBt->pWriter )
1640a 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 57 72  ;.      pBt->pWr
1640b 69 74 65 72 20 3d 20 70 3b 0a 20 20 20 20 20 20  iter = p;.      
1640c 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65  pBt->isExclusive
1640d 20 3d 20 28 75 38 29 28 77 72 66 6c 61 67 3e 31   = (u8)(wrflag>1
1640e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
1640f 20 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75    }...trans_begu
16410 6e 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  n:.  if( rc==SQL
16411 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67  ITE_OK && wrflag
16412 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
16413 63 61 6c 6c 20 6d 61 6b 65 73 20 73 75 72 65 20  call makes sure 
16414 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 68  that the pager h
16415 61 73 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e  as the correct n
16416 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20  umber of.    ** 
16417 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e  open savepoints.
16418 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70   If the second p
16419 61 72 61 6d 65 74 65 72 20 69 73 20 67 72 65 61  arameter is grea
1641a 74 65 72 20 74 68 61 6e 20 30 20 61 6e 64 0a 20  ter than 0 and. 
1641b 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f     ** the sub-jo
1641c 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72  urnal is not alr
1641d 65 61 64 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20  eady open, then 
1641e 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65  it will be opene
1641f 64 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20  d here..    */. 
16420 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
16421 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e  agerOpenSavepoin
16422 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70  t(pBt->pPager, p
16423 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ->db->nSavepoint
16424 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49  );.  }..  btreeI
16425 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73  ntegrity(p);.  s
16426 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
16427 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
16428 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
16429 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
1642a 55 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  UUM../*.** Set t
1642b 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
1642c 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63  ntries for all c
1642d 68 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20  hildren of page 
1642e 70 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a  pPage. Also, if.
1642f 2a 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e  ** pPage contain
16430 73 20 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69  s cells that poi
16431 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70  nt to overflow p
16432 61 67 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f  ages, set the po
16433 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74  inter.** map ent
16434 72 69 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65  ries for the ove
16435 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77  rflow pages as w
16436 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ell..*/.static i
16437 6e 74 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61  nt setChildPtrma
16438 70 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ps(MemPage *pPag
16439 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  e){.  int i;    
1643a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1643b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e           /* Coun
1643c 74 65 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  ter variable */.
1643d 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20    int nCell;    
1643e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1643f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
16440 66 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20  f cells in page 
16441 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72  pPage */.  int r
16442 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
16443 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16444 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
16445 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
16446 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
16447 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20  u8 isInitOrig = 
16448 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20  pPage->isInit;. 
16449 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61   Pgno pgno = pPa
1644a 67 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73  ge->pgno;..  ass
1644b 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1644c 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
1644d 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1644e 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61  rc = btreeInitPa
1644f 67 65 28 70 50 61 67 65 29 3b 0a 20 20 69 66 28  ge(pPage);.  if(
16450 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
16451 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63  {.    goto set_c
16452 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74  hild_ptrmaps_out
16453 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20  ;.  }.  nCell = 
16454 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20  pPage->nCell;.. 
16455 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
16456 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20  l; i++){.    u8 
16457 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c  *pCell = findCel
16458 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20  l(pPage, i);..  
16459 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50    ptrmapPutOvflP
1645a 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
1645b 20 26 72 63 29 3b 0a 0a 20 20 20 20 69 66 28 20   &rc);..    if( 
1645c 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
1645d 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64        Pgno child
1645e 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  Pgno = get4byte(
1645f 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74  pCell);.      pt
16460 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63 68 69  rmapPut(pBt, chi
16461 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42  ldPgno, PTRMAP_B
16462 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29  TREE, pgno, &rc)
16463 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  ;.    }.  }..  i
16464 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
16465 29 7b 0a 20 20 20 20 50 67 6e 6f 20 63 68 69 6c  ){.    Pgno chil
16466 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65  dPgno = get4byte
16467 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
16468 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
16469 38 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 50  8]);.    ptrmapP
1646a 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e  ut(pBt, childPgn
1646b 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  o, PTRMAP_BTREE,
1646c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d   pgno, &rc);.  }
1646d 0a 0a 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d  ..set_child_ptrm
1646e 61 70 73 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65  aps_out:.  pPage
1646f 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69  ->isInit = isIni
16470 74 4f 72 69 67 3b 0a 20 20 72 65 74 75 72 6e 20  tOrig;.  return 
16471 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d  rc;.}../*.** Som
16472 65 77 68 65 72 65 20 6f 6e 20 70 50 61 67 65 20  ewhere on pPage 
16473 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
16474 70 61 67 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64  page iFrom.  Mod
16475 69 66 79 20 74 68 69 73 20 70 6f 69 6e 74 65 72  ify this pointer
16476 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 70   so.** that it p
16477 6f 69 6e 74 73 20 74 6f 20 69 54 6f 2e 20 50 61  oints to iTo. Pa
16478 72 61 6d 65 74 65 72 20 65 54 79 70 65 20 64 65  rameter eType de
16479 73 63 72 69 62 65 73 20 74 68 65 20 74 79 70 65  scribes the type
1647a 20 6f 66 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a   of pointer to.*
1647b 2a 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 61  * be modified, a
1647c 73 20 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  s  follows:.**.*
1647d 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20  * PTRMAP_BTREE: 
1647e 20 20 20 20 70 50 61 67 65 20 69 73 20 61 20 62      pPage is a b
1647f 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70  tree-page. The p
16480 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74  ointer points at
16481 20 61 20 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20   a child .**    
16482 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
16483 61 67 65 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a  age of pPage..**
16484 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46  .** PTRMAP_OVERF
16485 4c 4f 57 31 3a 20 70 50 61 67 65 20 69 73 20 61  LOW1: pPage is a
16486 20 62 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65   btree-page. The
16487 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20   pointer points 
16488 61 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a  at an overflow.*
16489 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
1648a 20 20 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64      page pointed
1648b 20 74 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68   to by one of th
1648c 65 20 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65  e cells on pPage
1648d 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f  ..**.** PTRMAP_O
1648e 56 45 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20  VERFLOW2: pPage 
1648f 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70  is an overflow-p
16490 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72  age. The pointer
16491 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e   points at the n
16492 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ext.**          
16493 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f           overflo
16494 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69  w page in the li
16495 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  st..*/.static in
16496 74 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e  t modifyPagePoin
16497 74 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ter(MemPage *pPa
16498 67 65 2c 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20  ge, Pgno iFrom, 
16499 50 67 6e 6f 20 69 54 6f 2c 20 75 38 20 65 54 79  Pgno iTo, u8 eTy
1649a 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  pe){.  assert( s
1649b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1649c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
1649d 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
1649e 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
1649f 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
164a0 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69  >pDbPage) );.  i
164a1 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  f( eType==PTRMAP
164a2 5f 4f 56 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20  _OVERFLOW2 ){.  
164a3 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72    /* The pointer
164a4 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 66   is always the f
164a5 69 72 73 74 20 34 20 62 79 74 65 73 20 6f 66 20  irst 4 bytes of 
164a6 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 73  the page in this
164a7 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69   case.  */.    i
164a8 66 28 20 67 65 74 34 62 79 74 65 28 70 50 61 67  f( get4byte(pPag
164a9 65 2d 3e 61 44 61 74 61 29 21 3d 69 46 72 6f 6d  e->aData)!=iFrom
164aa 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
164ab 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
164ac 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BKPT;.    }.    
164ad 70 75 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e  put4byte(pPage->
164ae 61 44 61 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d  aData, iTo);.  }
164af 65 6c 73 65 7b 0a 20 20 20 20 75 38 20 69 73 49  else{.    u8 isI
164b0 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d  nitOrig = pPage-
164b1 3e 69 73 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74  >isInit;.    int
164b2 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c   i;.    int nCel
164b3 6c 3b 0a 0a 20 20 20 20 62 74 72 65 65 49 6e 69  l;..    btreeIni
164b4 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  tPage(pPage);.  
164b5 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d    nCell = pPage-
164b6 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72  >nCell;..    for
164b7 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69  (i=0; i<nCell; i
164b8 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70  ++){.      u8 *p
164b9 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
164ba 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20  pPage, i);.     
164bb 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d   if( eType==PTRM
164bc 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a  AP_OVERFLOW1 ){.
164bd 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f          CellInfo
164be 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 62   info;.        b
164bf 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
164c0 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26  (pPage, pCell, &
164c1 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 69  info);.        i
164c2 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  f( info.iOverflo
164c3 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  w ){.          i
164c4 66 28 20 69 46 72 6f 6d 3d 3d 67 65 74 34 62 79  f( iFrom==get4by
164c5 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  te(&pCell[info.i
164c6 4f 76 65 72 66 6c 6f 77 5d 29 20 29 7b 0a 20 20  Overflow]) ){.  
164c7 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79            put4by
164c8 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  te(&pCell[info.i
164c9 4f 76 65 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b  Overflow], iTo);
164ca 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
164cb 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
164cc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
164cd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
164ce 66 28 20 67 65 74 34 62 79 74 65 28 70 43 65 6c  f( get4byte(pCel
164cf 6c 29 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  l)==iFrom ){.   
164d0 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28         put4byte(
164d1 70 43 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20  pCell, iTo);.   
164d2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
164d3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
164d4 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28      }.  .    if(
164d5 20 69 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20   i==nCell ){.   
164d6 20 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 54     if( eType!=PT
164d7 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20  RMAP_BTREE || . 
164d8 20 20 20 20 20 20 20 20 20 67 65 74 34 62 79 74           get4byt
164d9 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e(&pPage->aData[
164da 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
164db 2b 38 5d 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20  +8])!=iFrom ){. 
164dc 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
164dd 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
164de 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  T;.      }.     
164df 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
164e0 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
164e1 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f  drOffset+8], iTo
164e2 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50  );.    }..    pP
164e3 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73  age->isInit = is
164e4 49 6e 69 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20  InitOrig;.  }.  
164e5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
164e6 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  ;.}.../*.** Move
164e7 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62 61   the open databa
164e8 73 65 20 70 61 67 65 20 70 44 62 50 61 67 65 20  se page pDbPage 
164e9 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65  to location iFre
164ea 65 50 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a  ePage in the .**
164eb 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 70   database. The p
164ec 44 62 50 61 67 65 20 72 65 66 65 72 65 6e 63 65  DbPage reference
164ed 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a   remains valid..
164ee 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d  **.** The isComm
164ef 69 74 20 66 6c 61 67 20 69 6e 64 69 63 61 74 65  it flag indicate
164f0 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20  s that there is 
164f1 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d  no need to remem
164f2 62 65 72 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ber that.** the 
164f3 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f  journal needs to
164f4 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66   be sync()ed bef
164f5 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67  ore database pag
164f6 65 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20  e pDbPage->pgno 
164f7 0a 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74  .** can be writt
164f8 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65  en to. The calle
164f9 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72  r has already pr
164fa 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72  omised not to wr
164fb 69 74 65 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70  ite to that.** p
164fc 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  age..*/.static i
164fd 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28  nt relocatePage(
164fe 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
164ff 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ,           /* B
16500 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  tree */.  MemPag
16501 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20  e *pDbPage,     
16502 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20     /* Open page 
16503 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20  to move */.  u8 
16504 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 20  eType,          
16505 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
16506 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74 72   map 'type' entr
16507 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f  y for pDbPage */
16508 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65  .  Pgno iPtrPage
16509 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ,           /* P
1650a 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65  ointer map 'page
1650b 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70  -no' entry for p
1650c 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f  DbPage */.  Pgno
1650d 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 20 20   iFreePage,     
1650e 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61       /* The loca
1650f 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62  tion to move pDb
16510 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74  Page to */.  int
16511 20 69 73 43 6f 6d 6d 69 74 20 20 20 20 20 20 20   isCommit       
16512 20 20 20 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69        /* isCommi
16513 74 20 66 6c 61 67 20 70 61 73 73 65 64 20 74 6f  t flag passed to
16514 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76   sqlite3PagerMov
16515 65 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65  epage */.){.  Me
16516 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67 65 3b  mPage *pPtrPage;
16517 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74     /* The page t
16518 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  hat contains a p
16519 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50 61 67  ointer to pDbPag
1651a 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50  e */.  Pgno iDbP
1651b 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d 3e 70  age = pDbPage->p
1651c 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  gno;.  Pager *pP
1651d 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67  ager = pBt->pPag
1651e 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  er;.  int rc;.. 
1651f 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d   assert( eType==
16520 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
16521 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41   || eType==PTRMA
16522 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a  P_OVERFLOW1 || .
16523 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 54 52        eType==PTR
16524 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79  MAP_BTREE || eTy
16525 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe==PTRMAP_ROOTP
16526 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  AGE );.  assert(
16527 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16528 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
16529 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 62  );.  assert( pDb
1652a 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29  Page->pBt==pBt )
1652b 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67  ;..  /* Move pag
1652c 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d 20 69  e iDbPage from i
1652d 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  ts current locat
1652e 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d 62  ion to page numb
1652f 65 72 20 69 46 72 65 65 50 61 67 65 20 2a 2f 0a  er iFreePage */.
16530 20 20 54 52 41 43 45 28 28 22 41 55 54 4f 56 41    TRACE(("AUTOVA
16531 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20  CUUM: Moving %d 
16532 74 6f 20 66 72 65 65 20 70 61 67 65 20 25 64 20  to free page %d 
16533 28 70 74 72 20 70 61 67 65 20 25 64 20 74 79 70  (ptr page %d typ
16534 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20  e %d)\n", .     
16535 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 50   iDbPage, iFreeP
16536 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c 20 65  age, iPtrPage, e
16537 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d 20 73  Type));.  rc = s
16538 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70  qlite3PagerMovep
16539 61 67 65 28 70 50 61 67 65 72 2c 20 70 44 62 50  age(pPager, pDbP
1653a 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 69 46  age->pDbPage, iF
1653b 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69  reePage, isCommi
1653c 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  t);.  if( rc!=SQ
1653d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
1653e 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
1653f 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20  pDbPage->pgno = 
16540 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a  iFreePage;..  /*
16541 20 49 66 20 70 44 62 50 61 67 65 20 77 61 73 20   If pDbPage was 
16542 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20 74 68  a btree-page, th
16543 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65 20 63  en it may have c
16544 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64 2f 6f  hild pages and/o
16545 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61  r cells.  ** tha
16546 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66  t point to overf
16547 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70  low pages. The p
16548 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69  ointer map entri
16549 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65  es for all these
1654a 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 64  .  ** pages need
1654b 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 2e 0a   to be changed..
1654c 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62    **.  ** If pDb
1654d 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72 66  Page is an overf
1654e 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 74  low page, then t
1654f 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73  he first 4 bytes
16550 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 20 2a   may store a.  *
16551 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  * pointer to a s
16552 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c  ubsequent overfl
16553 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68 69 73  ow page. If this
16554 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
16555 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e  en.  ** the poin
16556 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20 74 6f  ter map needs to
16557 20 62 65 20 75 70 64 61 74 65 64 20 66 6f 72 20   be updated for 
16558 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 6f  the subsequent o
16559 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20  verflow page..  
1655a 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d  */.  if( eType==
1655b 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20  PTRMAP_BTREE || 
1655c 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f  eType==PTRMAP_RO
1655d 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63  OTPAGE ){.    rc
1655e 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61   = setChildPtrma
1655f 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20  ps(pDbPage);.   
16560 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16561 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
16562 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
16563 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e  else{.    Pgno n
16564 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 79  extOvfl = get4by
16565 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44 61 74  te(pDbPage->aDat
16566 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74  a);.    if( next
16567 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20  Ovfl!=0 ){.     
16568 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
16569 6e 65 78 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50  nextOvfl, PTRMAP
1656a 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 46 72 65  _OVERFLOW2, iFre
1656b 65 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20  ePage, &rc);.   
1656c 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1656d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1656e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1656f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
16570 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74 61 62  /* Fix the datab
16571 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70  ase pointer on p
16572 61 67 65 20 69 50 74 72 50 61 67 65 20 74 68 61  age iPtrPage tha
16573 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69 44 62  t pointed at iDb
16574 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61  Page so.  ** tha
16575 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 69  t it points at i
16576 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 66  FreePage. Also f
16577 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  ix the pointer m
16578 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a  ap entry for.  *
16579 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f  * iPtrPage..  */
1657a 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 54  .  if( eType!=PT
1657b 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b  RMAP_ROOTPAGE ){
1657c 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47  .    rc = btreeG
1657d 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 74 72  etPage(pBt, iPtr
1657e 50 61 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c  Page, &pPtrPage,
1657f 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   0);.    if( rc!
16580 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16581 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
16582 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
16583 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
16584 70 50 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67  pPtrPage->pDbPag
16585 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
16586 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16587 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
16588 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20  PtrPage);.      
16589 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
1658a 0a 20 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79  .    rc = modify
1658b 50 61 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72  PagePointer(pPtr
1658c 50 61 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69  Page, iDbPage, i
1658d 46 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 29  FreePage, eType)
1658e 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  ;.    releasePag
1658f 65 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20  e(pPtrPage);.   
16590 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16591 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d  OK ){.      ptrm
16592 61 70 50 75 74 28 70 42 74 2c 20 69 46 72 65 65  apPut(pBt, iFree
16593 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74  Page, eType, iPt
16594 72 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20  rPage, &rc);.   
16595 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
16596 72 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72  rc;.}../* Forwar
16597 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65  d declaration re
16598 71 75 69 72 65 64 20 62 79 20 69 6e 63 72 56 61  quired by incrVa
16599 63 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73  cuumStep(). */.s
1659a 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61  tatic int alloca
1659b 74 65 42 74 72 65 65 50 61 67 65 28 42 74 53 68  teBtreePage(BtSh
1659c 61 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20  ared *, MemPage 
1659d 2a 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f  **, Pgno *, Pgno
1659e 2c 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65  , u8);../*.** Pe
1659f 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73  rform a single s
165a0 74 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d  tep of an increm
165a1 65 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66  ental-vacuum. If
165a2 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
165a3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
165a4 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  . If there is no
165a5 20 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64   work to do (and
165a6 20 74 68 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a   therefore no.**
165a7 20 70 6f 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e   point in callin
165a8 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  g this function 
165a9 61 67 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53  again), return S
165aa 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a  QLITE_DONE..**.*
165ab 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 6c  * More specificl
165ac 79 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  y, this function
165ad 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d   attempts to re-
165ae 6f 72 67 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a  organize the .**
165af 20 64 61 74 61 62 61 73 65 20 73 6f 20 74 68 61   database so tha
165b0 74 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20  t the last page 
165b1 6f 66 20 74 68 65 20 66 69 6c 65 20 63 75 72 72  of the file curr
165b2 65 6e 74 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20  ently in use.** 
165b3 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20  is no longer in 
165b4 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  use..**.** If th
165b5 65 20 6e 46 69 6e 20 70 61 72 61 6d 65 74 65 72  e nFin parameter
165b6 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
165b7 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
165b8 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  mes.** that the 
165b9 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70  caller will keep
165ba 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63   calling incrVac
165bb 75 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 0a  uumStep() until.
165bc 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20 53 51  ** it returns SQ
165bd 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20  LITE_DONE or an 
165be 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 20  error, and that 
165bf 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e  nFin is the.** n
165c0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74  umber of pages t
165c1 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
165c2 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66   will contain af
165c3 74 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f  ter this .** pro
165c4 63 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 65  cess is complete
165c5 2e 20 20 49 66 20 6e 46 69 6e 20 69 73 20 7a 65  .  If nFin is ze
165c6 72 6f 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65  ro, it is assume
165c7 64 20 74 68 61 74 0a 2a 2a 20 69 6e 63 72 56 61  d that.** incrVa
165c8 63 75 75 6d 53 74 65 70 28 29 20 77 69 6c 6c 20  cuumStep() will 
165c9 62 65 20 63 61 6c 6c 65 64 20 61 20 66 69 6e 69  be called a fini
165ca 74 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  te amount of tim
165cb 65 73 0a 2a 2a 20 77 68 69 63 68 20 6d 61 79 20  es.** which may 
165cc 6f 72 20 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79  or may not empty
165cd 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20   the freelist.  
165ce 41 20 66 75 6c 6c 20 61 75 74 6f 76 61 63 75 75  A full autovacuu
165cf 6d 0a 2a 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e  m.** has nFin>0.
165d0 20 20 41 20 22 50 52 41 47 4d 41 20 69 6e 63 72    A "PRAGMA incr
165d1 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20  emental_vacuum" 
165d2 68 61 73 20 6e 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a  has nFin==0..*/.
165d3 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 72 56  static int incrV
165d4 61 63 75 75 6d 53 74 65 70 28 42 74 53 68 61 72  acuumStep(BtShar
165d5 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46  ed *pBt, Pgno nF
165d6 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74 50 67  in, Pgno iLastPg
165d7 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c  ){.  Pgno nFreeL
165d8 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ist;           /
165d9 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
165da 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66  s still on the f
165db 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61  ree-list */..  a
165dc 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
165dd 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
165de 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
165df 74 28 20 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20  t( iLastPg>nFin 
165e0 29 3b 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41  );..  if( !PTRMA
165e1 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c  P_ISPAGE(pBt, iL
165e2 61 73 74 50 67 29 20 26 26 20 69 4c 61 73 74 50  astPg) && iLastP
165e3 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  g!=PENDING_BYTE_
165e4 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
165e5 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 38 20   int rc;.    u8 
165e6 65 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20  eType;.    Pgno 
165e7 69 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e  iPtrPage;..    n
165e8 46 72 65 65 4c 69 73 74 20 3d 20 67 65 74 34 62  FreeList = get4b
165e9 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31  yte(&pBt->pPage1
165ea 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20  ->aData[36]);.  
165eb 20 20 69 66 28 20 6e 46 72 65 65 4c 69 73 74 3d    if( nFreeList=
165ec 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
165ed 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
165ee 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
165ef 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69  ptrmapGet(pBt, i
165f0 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c 20  LastPg, &eType, 
165f1 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20  &iPtrPage);.    
165f2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
165f3 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
165f4 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
165f5 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
165f6 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20  P_ROOTPAGE ){.  
165f7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
165f8 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
165f9 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 65      }..    if( e
165fa 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45  Type==PTRMAP_FRE
165fb 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 69  EPAGE ){.      i
165fc 66 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20  f( nFin==0 ){.  
165fd 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20        /* Remove 
165fe 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68  the page from th
165ff 65 20 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73  e files free-lis
16600 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72  t. This is not r
16601 65 71 75 69 72 65 64 0a 20 20 20 20 20 20 20 20  equired.        
16602 2a 2a 20 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f  ** if nFin is no
16603 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74 20  n-zero. In that 
16604 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 2d 6c  case, the free-l
16605 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  ist will be.    
16606 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 64      ** truncated
16607 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 20 74   to zero after t
16608 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
16609 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65 73  urns, so it does
1660a 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  n't .        ** 
1660b 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 74 69  matter if it sti
1660c 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65  ll contains some
1660d 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 65 73   garbage entries
1660e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
1660f 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50       Pgno iFreeP
16610 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61  g;.        MemPa
16611 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20  ge *pFreePg;.   
16612 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
16613 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
16614 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65   &pFreePg, &iFre
16615 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 31 29  ePg, iLastPg, 1)
16616 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
16617 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
16618 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
16619 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
1661a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46        assert( iF
1661b 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 67 20 29  reePg==iLastPg )
1661c 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
1661d 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a  ePage(pFreePg);.
1661e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c        }.    } el
1661f 73 65 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20  se {.      Pgno 
16620 69 46 72 65 65 50 67 3b 20 20 20 20 20 20 20 20  iFreePg;        
16621 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
16622 20 66 72 65 65 20 70 61 67 65 20 74 6f 20 6d 6f   free page to mo
16623 76 65 20 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f  ve pLastPg to */
16624 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  .      MemPage *
16625 70 4c 61 73 74 50 67 3b 0a 0a 20 20 20 20 20 20  pLastPg;..      
16626 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
16627 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20  e(pBt, iLastPg, 
16628 26 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20  &pLastPg, 0);.  
16629 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1662a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1662b 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1662c 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66    }..      /* If
1662d 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74   nFin is zero, t
1662e 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78  his loop runs ex
1662f 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70  actly once and p
16630 61 67 65 20 70 4c 61 73 74 50 67 0a 20 20 20 20  age pLastPg.    
16631 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65 64 20    ** is swapped 
16632 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20 66  with the first f
16633 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65 64 20  ree page pulled 
16634 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c 69 73  off the free lis
16635 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  t..      **.    
16636 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65    ** On the othe
16637 72 20 68 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20  r hand, if nFin 
16638 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
16639 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a  zero, then keep.
1663a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67        ** looping
1663b 20 75 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61   until a free-pa
1663c 67 65 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69  ge located withi
1663d 6e 20 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e  n the first nFin
1663e 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20   pages.      ** 
1663f 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 66  of the file is f
16640 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ound..      */. 
16641 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20       do {.      
16642 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65    MemPage *pFree
16643 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  Pg;.        rc =
16644 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
16645 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67  ge(pBt, &pFreePg
16646 2c 20 26 69 46 72 65 65 50 67 2c 20 30 2c 20 30  , &iFreePg, 0, 0
16647 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
16648 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
16649 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73            releas
1664a 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a  ePage(pLastPg);.
1664b 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
1664c 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
1664d 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
1664e 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20  ge(pFreePg);.   
1664f 20 20 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21     }while( nFin!
16650 3d 30 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46  =0 && iFreePg>nF
16651 69 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  in );.      asse
16652 72 74 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73  rt( iFreePg<iLas
16653 74 50 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20  tPg );.      .  
16654 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16655 50 61 67 65 72 57 72 69 74 65 28 70 4c 61 73 74  PagerWrite(pLast
16656 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  Pg->pDbPage);.  
16657 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
16658 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
16659 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61   rc = relocatePa
1665a 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c  ge(pBt, pLastPg,
1665b 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65   eType, iPtrPage
1665c 2c 20 69 46 72 65 65 50 67 2c 20 6e 46 69 6e 21  , iFreePg, nFin!
1665d 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  =0);.      }.   
1665e 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1665f 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69  LastPg);.      i
16660 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16661 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
16662 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
16663 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
16664 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69  nFin==0 ){.    i
16665 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68  LastPg--;.    wh
16666 69 6c 65 28 20 69 4c 61 73 74 50 67 3d 3d 50 45  ile( iLastPg==PE
16667 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
16668 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50  pBt)||PTRMAP_ISP
16669 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67  AGE(pBt, iLastPg
1666a 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 50  ) ){.      if( P
1666b 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
1666c 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20  , iLastPg) ){.  
1666d 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
1666e 50 67 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  Pg;.        int 
1666f 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
16670 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20  e(pBt, iLastPg, 
16671 26 70 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20  &pPg, 0);.      
16672 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16673 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
16674 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
16675 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
16676 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
16677 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 61 67  rite(pPg->pDbPag
16678 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65  e);.        rele
16679 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a 20 20  asePage(pPg);.  
1667a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1667b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1667c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1667d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1667e 7d 0a 20 20 20 20 20 20 69 4c 61 73 74 50 67 2d  }.      iLastPg-
1667f 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  -;.    }.    sql
16680 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
16681 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67  eImage(pBt->pPag
16682 65 72 2c 20 69 4c 61 73 74 50 67 29 3b 0a 20 20  er, iLastPg);.  
16683 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
16684 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
16685 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
16686 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65  on must be opene
16687 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  d before calling
16688 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
16689 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61  ** It performs a
1668a 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20   single unit of 
1668b 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20  work towards an 
1668c 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  incremental vacu
1668d 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  um..**.** If the
1668e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   incremental vac
1668f 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20  uum is finished 
16690 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74  after this funct
16691 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20  ion has run,.** 
16692 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72  SQLITE_DONE is r
16693 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69  eturned. If it i
16694 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20  s not finished, 
16695 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63  but no error occ
16696 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45  urred,.** SQLITE
16697 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
16698 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51   Otherwise an SQ
16699 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
1669a 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
1669b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1669c 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42  treeIncrVacuum(B
1669d 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  tree *p){.  int 
1669e 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  rc;.  BtShared *
1669f 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20  pBt = p->pBt;.. 
166a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
166a1 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28  er(p);.  assert(
166a2 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
166a3 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
166a4 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d   && p->inTrans==
166a5 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
166a6 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74 6f 56   if( !pBt->autoV
166a7 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20  acuum ){.    rc 
166a8 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
166a9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61   }else{.    inva
166aa 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f  lidateAllOverflo
166ab 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20  wCache(pBt);.   
166ac 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d   rc = incrVacuum
166ad 53 74 65 70 28 70 42 74 2c 20 30 2c 20 70 61 67  Step(pBt, 0, pag
166ae 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
166af 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
166b0 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
166b1 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
166b2 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
166b3 65 20 69 73 20 63 61 6c 6c 65 64 20 70 72 69 6f  e is called prio
166b4 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  r to sqlite3Page
166b5 72 43 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20 74  rCommit when a t
166b6 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73  ransaction.** is
166b7 20 63 6f 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e   commited for an
166b8 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
166b9 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  abase..**.** If 
166ba 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
166bb 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54  urned, then *pnT
166bc 72 75 6e 63 20 69 73 20 73 65 74 20 74 6f 20 74  runc is set to t
166bd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
166be 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  es.** the databa
166bf 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  se file should b
166c0 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 64  e truncated to d
166c1 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74  uring the commit
166c2 20 70 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e   process. .** i.
166c3 65 2e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  e. the database 
166c4 68 61 73 20 62 65 65 6e 20 72 65 6f 72 67 61 6e  has been reorgan
166c5 69 7a 65 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c  ized so that onl
166c6 79 20 74 68 65 20 66 69 72 73 74 20 2a 70 6e 54  y the first *pnT
166c7 72 75 6e 63 0a 2a 2a 20 70 61 67 65 73 20 61 72  runc.** pages ar
166c8 65 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61  e in use..*/.sta
166c9 74 69 63 20 69 6e 74 20 61 75 74 6f 56 61 63 75  tic int autoVacu
166ca 75 6d 43 6f 6d 6d 69 74 28 42 74 53 68 61 72 65  umCommit(BtShare
166cb 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72  d *pBt){.  int r
166cc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
166cd 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
166ce 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pBt->pPager;.  
166cf 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52  VVA_ONLY( int nR
166d0 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ef = sqlite3Page
166d1 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72  rRefcount(pPager
166d2 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ) );..  assert( 
166d3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
166d4 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
166d5 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c  ;.  invalidateAl
166d6 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70  lOverflowCache(p
166d7 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28 70 42  Bt);.  assert(pB
166d8 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a  t->autoVacuum);.
166d9 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e 63 72    if( !pBt->incr
166da 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67  Vacuum ){.    Pg
166db 6e 6f 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 20  no nFin;        
166dc 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
166dd 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 20  ges in database 
166de 61 66 74 65 72 20 61 75 74 6f 76 61 63 75 75 6d  after autovacuum
166df 69 6e 67 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  ing */.    Pgno 
166e0 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a  nFree;        /*
166e1 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
166e2 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74   on the freelist
166e3 20 69 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20   initially */.  
166e4 20 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 20    Pgno nPtrmap; 
166e5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
166e6 66 20 50 74 72 4d 61 70 20 70 61 67 65 73 20 74  f PtrMap pages t
166e7 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20  o be freed */.  
166e8 20 20 50 67 6e 6f 20 69 46 72 65 65 3b 20 20 20    Pgno iFree;   
166e9 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74       /* The next
166ea 20 70 61 67 65 20 74 6f 20 62 65 20 66 72 65 65   page to be free
166eb 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e  d */.    int nEn
166ec 74 72 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  try;        /* N
166ed 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
166ee 20 6f 6e 20 6f 6e 65 20 70 74 72 6d 61 70 20 70   on one ptrmap p
166ef 61 67 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  age */.    Pgno 
166f0 6e 4f 72 69 67 3b 20 20 20 20 20 20 20 20 2f 2a  nOrig;        /*
166f1 20 44 61 74 61 62 61 73 65 20 73 69 7a 65 20 62   Database size b
166f2 65 66 6f 72 65 20 66 72 65 65 69 6e 67 20 2a 2f  efore freeing */
166f3 0a 0a 20 20 20 20 6e 4f 72 69 67 20 3d 20 70 61  ..    nOrig = pa
166f4 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
166f5 29 3b 0a 20 20 20 20 69 66 28 20 50 54 52 4d 41  );.    if( PTRMA
166f6 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 4f  P_ISPAGE(pBt, nO
166f7 72 69 67 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50  rig) || nOrig==P
166f8 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
166f9 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f  (pBt) ){.      /
166fa 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
166fb 69 62 6c 65 20 74 6f 20 63 72 65 61 74 65 20 61  ible to create a
166fc 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 77 68   database for wh
166fd 69 63 68 20 74 68 65 20 66 69 6e 61 6c 20 70 61  ich the final pa
166fe 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65  ge.      ** is e
166ff 69 74 68 65 72 20 61 20 70 6f 69 6e 74 65 72 2d  ither a pointer-
16700 6d 61 70 20 70 61 67 65 20 6f 72 20 74 68 65 20  map page or the 
16701 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67  pending-byte pag
16702 65 2e 20 49 66 20 6f 6e 65 0a 20 20 20 20 20 20  e. If one.      
16703 2a 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  ** is encountere
16704 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65  d, this indicate
16705 73 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20  s corruption..  
16706 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74      */.      ret
16707 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
16708 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a  PT_BKPT;.    }..
16709 20 20 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34      nFree = get4
1670a 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65  byte(&pBt->pPage
1670b 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20  1->aData[36]);. 
1670c 20 20 20 6e 45 6e 74 72 79 20 3d 20 70 42 74 2d     nEntry = pBt-
1670d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 3b 0a 20  >usableSize/5;. 
1670e 20 20 20 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46     nPtrmap = (nF
1670f 72 65 65 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50  ree-nOrig+PTRMAP
16710 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72  _PAGENO(pBt, nOr
16711 69 67 29 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74  ig)+nEntry)/nEnt
16712 72 79 3b 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e  ry;.    nFin = n
16713 4f 72 69 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e  Orig - nFree - n
16714 50 74 72 6d 61 70 3b 0a 20 20 20 20 69 66 28 20  Ptrmap;.    if( 
16715 6e 4f 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59  nOrig>PENDING_BY
16716 54 45 5f 50 41 47 45 28 70 42 74 29 20 26 26 20  TE_PAGE(pBt) && 
16717 6e 46 69 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54  nFin<PENDING_BYT
16718 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
16719 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20       nFin--;.   
1671a 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54   }.    while( PT
1671b 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c  RMAP_ISPAGE(pBt,
1671c 20 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d   nFin) || nFin==
1671d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1671e 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  E(pBt) ){.      
1671f 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20  nFin--;.    }.  
16720 20 20 69 66 28 20 6e 46 69 6e 3e 6e 4f 72 69 67    if( nFin>nOrig
16721 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
16722 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a  _CORRUPT_BKPT;..
16723 20 20 20 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f      for(iFree=nO
16724 72 69 67 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20  rig; iFree>nFin 
16725 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
16726 3b 20 69 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20  ; iFree--){.    
16727 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75    rc = incrVacuu
16728 6d 53 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c  mStep(pBt, nFin,
16729 20 69 46 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20   iFree);.    }. 
1672a 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49     if( (rc==SQLI
1672b 54 45 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53  TE_DONE || rc==S
1672c 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72  QLITE_OK) && nFr
1672d 65 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63  ee>0 ){.      rc
1672e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1672f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16730 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e  PagerWrite(pBt->
16731 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
16732 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  ;.      put4byte
16733 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61  (&pBt->pPage1->a
16734 44 61 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20  Data[32], 0);.  
16735 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 42      put4byte(&pB
16736 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
16737 5b 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20  [36], 0);.      
16738 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e  sqlite3PagerTrun
16739 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70  cateImage(pBt->p
1673a 50 61 67 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20  Pager, nFin);.  
1673b 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
1673c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1673d 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52     sqlite3PagerR
1673e 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b  ollback(pPager);
1673f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73  .    }.  }..  as
16740 73 65 72 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69  sert( nRef==sqli
16741 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
16742 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65  (pPager) );.  re
16743 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73  turn rc;.}..#els
16744 65 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49  e /* ifndef SQLI
16745 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
16746 55 4d 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73  UM */.# define s
16747 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 78  etChildPtrmaps(x
16748 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64  ) SQLITE_OK.#end
16749 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  if../*.** This r
1674a 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20  outine does the 
1674b 66 69 72 73 74 20 70 68 61 73 65 20 6f 66 20 61  first phase of a
1674c 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69   two-phase commi
1674d 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  t.  This routine
1674e 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72 6f 6c  .** causes a rol
1674f 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f  lback journal to
16750 20 62 65 20 63 72 65 61 74 65 64 20 28 69 66 20   be created (if 
16751 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65  it does not alre
16752 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e  ady exist).** an
16753 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  d populated with
16754 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74   enough informat
16755 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66 20 61  ion so that if a
16756 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63 63 75   power loss occu
16757 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  rs.** the databa
16758 73 65 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72  se can be restor
16759 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e  ed to its origin
1675a 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c 61 79  al state by play
1675b 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20  ing back.** the 
1675c 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74  journal.  Then t
1675d 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
1675e 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66  he journal are f
1675f 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a  lushed out to.**
16760 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66 74 65   the disk.  Afte
16761 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  r the journal is
16762 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65   safely on oxide
16763 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20 74 6f  , the changes to
16764 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
16765 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74   are written int
16766 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
16767 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65 64 20  ile and flushed 
16768 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20  to oxide..** At 
16769 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20  the end of this 
1676a 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61  call, the rollba
1676b 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c  ck journal still
1676c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a   exists on the.*
1676d 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20 61 72  * disk and we ar
1676e 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20  e still holding 
1676f 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68  all locks, so th
16770 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61  e transaction ha
16771 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74  s not.** committ
16772 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33  ed.  See sqlite3
16773 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
16774 54 77 6f 28 29 20 66 6f 72 20 74 68 65 20 73 65  Two() for the se
16775 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 74 68  cond phase of th
16776 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63  e.** commit proc
16777 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ess..**.** This 
16778 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20  call is a no-op 
16779 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e  if no write-tran
1677a 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65  saction is curre
1677b 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e 20 70  ntly active on p
1677c 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  Bt..**.** Otherw
1677d 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20 64 61  ise, sync the da
1677e 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20  tabase file for 
1677f 74 68 65 20 62 74 72 65 65 20 70 42 74 2e 20 7a  the btree pBt. z
16780 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f  Master points to
16781 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  .** the name of 
16782 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
16783 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c   file that shoul
16784 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  d be written int
16785 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64  o the.** individ
16786 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ual journal file
16787 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e  , or is NULL, in
16788 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74  dicating no mast
16789 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
1678a 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61 74 61  .** (single data
1678b 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  base transaction
1678c 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  )..**.** When th
1678d 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  is is called, th
1678e 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
1678f 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20   should already 
16790 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65  have been.** cre
16791 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65 64 20  ated, populated 
16792 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72 6e 61  with this journa
16793 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 73 79  l pointer and sy
16794 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a  nced to disk..**
16795 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20 69 73  .** Once this is
16796 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72 65 74   routine has ret
16797 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20  urned, the only 
16798 74 68 69 6e 67 20 72 65 71 75 69 72 65 64 20 74  thing required t
16799 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20  o commit.** the 
1679a 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1679b 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62  n for this datab
1679c 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f 20 64  ase file is to d
1679d 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  elete the journa
1679e 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  l..*/.SQLITE_PRI
1679f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
167a0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
167a1 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20 63 6f  One(Btree *p, co
167a2 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
167a3 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
167a4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
167a5 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
167a6 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42  S_WRITE ){.    B
167a7 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
167a8 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74  ->pBt;.    sqlit
167a9 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
167aa 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
167ab 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
167ac 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
167ad 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
167ae 20 72 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d   rc = autoVacuum
167af 43 6f 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20  Commit(pBt);.   
167b0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
167b1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
167b2 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
167b3 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65  e(p);.        re
167b4 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
167b5 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
167b6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
167b7 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  gerCommitPhaseOn
167b8 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a  e(pBt->pPager, z
167b9 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20  Master, 0);.    
167ba 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
167bb 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  e(p);.  }.  retu
167bc 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
167bd 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
167be 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74   called from bot
167bf 68 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  h BtreeCommitPha
167c0 73 65 54 77 6f 28 29 20 61 6e 64 20 42 74 72 65  seTwo() and Btre
167c1 65 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61  eRollback().** a
167c2 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e  t the conclusion
167c3 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   of a transactio
167c4 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
167c5 64 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61  d btreeEndTransa
167c6 63 74 69 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b  ction(Btree *p){
167c7 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
167c8 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73   = p->pBt;.  ass
167c9 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
167ca 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29  eHoldsMutex(p) )
167cb 3b 0a 0a 20 20 62 74 72 65 65 43 6c 65 61 72 48  ;..  btreeClearH
167cc 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a  asContent(pBt);.
167cd 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
167ce 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70  >TRANS_NONE && p
167cf 2d 3e 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ->db->activeVdbe
167d0 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20  Cnt>1 ){.    /* 
167d1 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 74 68  If there are oth
167d2 65 72 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  er active statem
167d3 65 6e 74 73 20 74 68 61 74 20 62 65 6c 6f 6e 67  ents that belong
167d4 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73   to this databas
167d5 65 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2c  e.    ** handle,
167d6 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 61 20   downgrade to a 
167d7 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61  read-only transa
167d8 63 74 69 6f 6e 2e 20 54 68 65 20 6f 74 68 65 72  ction. The other
167d9 20 73 74 61 74 65 6d 65 6e 74 73 0a 20 20 20 20   statements.    
167da 2a 2a 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20  ** may still be 
167db 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65  reading from the
167dc 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20   database.  */. 
167dd 20 20 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53     downgradeAllS
167de 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
167df 6f 63 6b 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e  ocks(p);.    p->
167e0 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f  inTrans = TRANS_
167e1 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  READ;.  }else{. 
167e2 20 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e     /* If the han
167e3 64 6c 65 20 68 61 64 20 61 6e 79 20 6b 69 6e 64  dle had any kind
167e4 20 6f 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   of transaction 
167e5 6f 70 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20  open, decrement 
167e6 74 68 65 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e  the .    ** tran
167e7 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66  saction count of
167e8 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65   the shared btre
167e9 65 2e 20 49 66 20 74 68 65 20 74 72 61 6e 73 61  e. If the transa
167ea 63 74 69 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20  ction count .   
167eb 20 2a 2a 20 72 65 61 63 68 65 73 20 30 2c 20 73   ** reaches 0, s
167ec 65 74 20 74 68 65 20 73 68 61 72 65 64 20 73 74  et the shared st
167ed 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e  ate to TRANS_NON
167ee 45 2e 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72  E. The unlockBtr
167ef 65 65 49 66 55 6e 75 73 65 64 28 29 0a 20 20 20  eeIfUnused().   
167f0 20 2a 2a 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77   ** call below w
167f1 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70  ill unlock the p
167f2 61 67 65 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66  ager.  */.    if
167f3 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52  ( p->inTrans!=TR
167f4 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20  ANS_NONE ){.    
167f5 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64    clearAllShared
167f6 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28  CacheTableLocks(
167f7 70 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e  p);.      pBt->n
167f8 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20  Transaction--;. 
167f9 20 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d       if( 0==pBt-
167fa 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b  >nTransaction ){
167fb 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e  .        pBt->in
167fc 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52  Transaction = TR
167fd 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20  ANS_NONE;.      
167fe 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
167ff 53 65 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  Set the current 
16800 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74  transaction stat
16801 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20  e to TRANS_NONE 
16802 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a  and unlock the .
16803 20 20 20 20 2a 2a 20 70 61 67 65 72 20 69 66 20      ** pager if 
16804 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64  this call closed
16805 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f   the only read o
16806 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  r write transact
16807 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e  ion.  */.    p->
16808 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f  inTrans = TRANS_
16809 4e 4f 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b  NONE;.    unlock
1680a 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42  BtreeIfUnused(pB
1680b 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65  t);.  }..  btree
1680c 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a  Integrity(p);.}.
1680d 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68  ./*.** Commit th
1680e 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75  e transaction cu
1680f 72 72 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72  rrently in progr
16810 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ess..**.** This 
16811 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e  routine implemen
16812 74 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68  ts the second ph
16813 61 73 65 20 6f 66 20 61 20 32 2d 70 68 61 73 65  ase of a 2-phase
16814 20 63 6f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a   commit.  The.**
16815 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
16816 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 72 6f  mitPhaseOne() ro
16817 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66  utine does the f
16818 69 72 73 74 20 70 68 61 73 65 20 61 6e 64 20 73  irst phase and s
16819 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f  hould.** be invo
1681a 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  ked prior to cal
1681b 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  ling this routin
1681c 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 42  e.  The sqlite3B
1681d 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
1681e 6e 65 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  ne().** routine 
1681f 64 69 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b  did all the work
16820 20 6f 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f   of writing info
16821 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64  rmation out to d
16822 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67  isk and flushing
16823 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73   the.** contents
16824 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 61 72   so that they ar
16825 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74  e written onto t
16826 68 65 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e  he disk platter.
16827 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f    All this.** ro
16828 75 74 69 6e 65 20 68 61 73 20 74 6f 20 64 6f 20  utine has to do 
16829 69 73 20 64 65 6c 65 74 65 20 6f 72 20 74 72 75  is delete or tru
1682a 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68  ncate or zero th
1682b 65 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 0a  e header in the.
1682c 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ** the rollback 
1682d 6a 6f 75 72 6e 61 6c 20 28 77 68 69 63 68 20 63  journal (which c
1682e 61 75 73 65 73 20 74 68 65 20 74 72 61 6e 73 61  auses the transa
1682f 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29  ction to commit)
16830 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63   and.** drop loc
16831 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77  ks..**.** This w
16832 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20  ill release the 
16833 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  write lock on th
16834 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
16835 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72    If there.** ar
16836 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73  e no active curs
16837 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c  ors, it also rel
16838 65 61 73 65 73 20 74 68 65 20 72 65 61 64 20 6c  eases the read l
16839 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ock..*/.SQLITE_P
1683a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1683b 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
1683c 73 65 54 77 6f 28 42 74 72 65 65 20 2a 70 29 7b  seTwo(Btree *p){
1683d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1683e 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71   = p->pBt;..  sq
1683f 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
16840 70 29 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67  p);.  btreeInteg
16841 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49  rity(p);..  /* I
16842 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61 73  f the handle has
16843 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   a write-transac
16844 74 69 6f 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69  tion open, commi
16845 74 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  t the shared-btr
16846 65 65 73 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61  ees .  ** transa
16847 63 74 69 6f 6e 20 61 6e 64 20 73 65 74 20 74 68  ction and set th
16848 65 20 73 68 61 72 65 64 20 73 74 61 74 65 20 74  e shared state t
16849 6f 20 54 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20  o TRANS_READ..  
1684a 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  */.  if( p->inTr
1684b 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
1684c 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a   ){.    int rc;.
1684d 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d      assert( pBt-
1684e 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
1684f 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
16850 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
16851 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29  nTransaction>0 )
16852 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
16853 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
16854 73 65 54 77 6f 28 70 42 74 2d 3e 70 50 61 67 65  seTwo(pBt->pPage
16855 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
16856 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16857 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
16858 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72  eave(p);.      r
16859 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1685a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73      pBt->inTrans
1685b 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52  action = TRANS_R
1685c 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65  EAD;.  }..  btre
1685d 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28  eEndTransaction(
1685e 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  p);.  sqlite3Btr
1685f 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
16860 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
16861 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68  }../*.** Do both
16862 20 70 68 61 73 65 73 20 6f 66 20 61 20 63 6f 6d   phases of a com
16863 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  mit..*/.SQLITE_P
16864 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16865 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74  e3BtreeCommit(Bt
16866 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ree *p){.  int r
16867 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  c;.  sqlite3Btre
16868 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20  eEnter(p);.  rc 
16869 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
1686a 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20  mmitPhaseOne(p, 
1686b 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  0);.  if( rc==SQ
1686c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
1686d 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1686e 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
1686f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
16870 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
16871 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
16872 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
16873 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
16874 75 6d 62 65 72 20 6f 66 20 77 72 69 74 65 2d 63  umber of write-c
16875 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74  ursors open on t
16876 68 69 73 20 68 61 6e 64 6c 65 2e 20 54 68 69 73  his handle. This
16877 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69   is for use.** i
16878 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 72 65  n assert() expre
16879 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73  ssions, so it is
1687a 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69   only compiled i
1687b 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a  f NDEBUG is not.
1687c 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a  ** defined..**.*
1687d 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73  * For the purpos
1687e 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  es of this routi
1687f 6e 65 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73  ne, a write-curs
16880 6f 72 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72  or is any cursor
16881 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 61 70 61   that.** is capa
16882 62 6c 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74  ble of writing t
16883 6f 20 74 68 65 20 64 61 74 61 62 73 65 2e 20 20  o the databse.  
16884 54 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63  That means the c
16885 75 72 73 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69  ursor was.** ori
16886 67 69 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66  ginally opened f
16887 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74  or writing and t
16888 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f  he cursor has no
16889 74 20 62 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a  t be disabled.**
1688a 20 62 79 20 68 61 76 69 6e 67 20 69 74 73 20 73   by having its s
1688b 74 61 74 65 20 63 68 61 6e 67 65 64 20 74 6f 20  tate changed to 
1688c 43 55 52 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f  CURSOR_FAULT..*/
1688d 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e  .static int coun
1688e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28 42 74  tWriteCursors(Bt
1688f 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
16890 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  BtCursor *pCur;.
16891 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66    int r = 0;.  f
16892 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75  or(pCur=pBt->pCu
16893 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72  rsor; pCur; pCur
16894 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pCur->pNext){. 
16895 20 20 20 69 66 28 20 70 43 75 72 2d 3e 77 72 46     if( pCur->wrF
16896 6c 61 67 20 26 26 20 70 43 75 72 2d 3e 65 53 74  lag && pCur->eSt
16897 61 74 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c  ate!=CURSOR_FAUL
16898 54 20 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20  T ) r++; .  }.  
16899 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64  return r;.}.#end
1689a 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  if../*.** This r
1689b 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
1689c 73 74 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f  state to CURSOR_
1689d 46 41 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72  FAULT and the er
1689e 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65  ror.** code to e
1689f 72 72 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79  rrCode for every
168a0 20 63 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61   cursor on BtSha
168a1 72 65 64 20 74 68 61 74 20 70 42 74 72 65 65 0a  red that pBtree.
168a2 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a  ** references..*
168a3 2a 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f  *.** Every curso
168a4 72 20 69 73 20 74 72 69 70 70 65 64 2c 20 69 6e  r is tripped, in
168a5 63 6c 75 64 69 6e 67 20 63 75 72 73 6f 72 73 20  cluding cursors 
168a6 74 68 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74  that belong.** t
168a7 6f 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  o other database
168a8 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61   connections tha
168a9 74 20 68 61 70 70 65 6e 20 74 6f 20 62 65 20 73  t happen to be s
168aa 68 61 72 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61  haring.** the ca
168ab 63 68 65 20 77 69 74 68 20 70 42 74 72 65 65 2e  che with pBtree.
168ac 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
168ad 69 6e 65 20 67 65 74 73 20 63 61 6c 6c 65 64 20  ine gets called 
168ae 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20  when a rollback 
168af 6f 63 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63  occurs..** All c
168b0 75 72 73 6f 72 73 20 75 73 69 6e 67 20 74 68 65  ursors using the
168b1 20 73 61 6d 65 20 63 61 63 68 65 20 6d 75 73 74   same cache must
168b2 20 62 65 20 74 72 69 70 70 65 64 0a 2a 2a 20 74   be tripped.** t
168b3 6f 20 70 72 65 76 65 6e 74 20 74 68 65 6d 20 66  o prevent them f
168b4 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20 75 73  rom trying to us
168b5 65 20 74 68 65 20 62 74 72 65 65 20 61 66 74 65  e the btree afte
168b6 72 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63  r.** the rollbac
168b7 6b 2e 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b  k.  The rollback
168b8 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65 74 65   may have delete
168b9 64 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d  d tables.** or m
168ba 6f 76 65 64 20 72 6f 6f 74 20 70 61 67 65 73 2c  oved root pages,
168bb 20 73 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75   so it is not su
168bc 66 66 69 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73  fficient to.** s
168bd 61 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  ave the state of
168be 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68   the cursor.  Th
168bf 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65  e cursor must be
168c0 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  .** invalidated.
168c1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
168c2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
168c3 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f  treeTripAllCurso
168c4 72 73 28 42 74 72 65 65 20 2a 70 42 74 72 65 65  rs(Btree *pBtree
168c5 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a  , int errCode){.
168c6 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20    BtCursor *p;. 
168c7 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
168c8 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f  er(pBtree);.  fo
168c9 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d  r(p=pBtree->pBt-
168ca 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70  >pCursor; p; p=p
168cb 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e  ->pNext){.    in
168cc 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  t i;.    sqlite3
168cd 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72  BtreeClearCursor
168ce 28 70 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61  (p);.    p->eSta
168cf 74 65 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c  te = CURSOR_FAUL
168d0 54 3b 0a 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65  T;.    p->skipNe
168d1 78 74 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20  xt = errCode;.  
168d2 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d    for(i=0; i<=p-
168d3 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20  >iPage; i++){.  
168d4 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
168d5 70 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20  p->apPage[i]);. 
168d6 20 20 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69       p->apPage[i
168d7 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  ] = 0;.    }.  }
168d8 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
168d9 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a  eave(pBtree);.}.
168da 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20  ./*.** Rollback 
168db 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
168dc 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c  in progress.  Al
168dd 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62  l cursors will b
168de 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62  e.** invalided b
168df 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  y this operation
168e0 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74  .  Any attempt t
168e1 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a  o use a cursor.*
168e2 2a 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20  * that was open 
168e3 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
168e4 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 69   of this operati
168e5 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a  on will result.*
168e6 2a 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  * in an error..*
168e7 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72  *.** This will r
168e8 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65  elease the write
168e9 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
168ea 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20  abase file.  If 
168eb 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20  there.** are no 
168ec 61 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20  active cursors, 
168ed 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73  it also releases
168ee 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a   the read lock..
168ef 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
168f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
168f1 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65  eeRollback(Btree
168f2 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a   *p){.  int rc;.
168f3 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
168f4 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50  = p->pBt;.  MemP
168f5 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20  age *pPage1;..  
168f6 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
168f7 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76  r(p);.  rc = sav
168f8 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c  eAllCursors(pBt,
168f9 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20   0, 0);.#ifndef 
168fa 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
168fb 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72  ED_CACHE.  if( r
168fc 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
168fd 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61      /* This is a
168fe 20 68 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74   horrible situat
168ff 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61  ion. An IO or ma
16900 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63  lloc() error occ
16901 75 72 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20  urred whilst.   
16902 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61   ** trying to sa
16903 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  ve cursor positi
16904 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73 20  ons. If this is 
16905 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c  an automatic rol
16906 6c 62 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a  lback (as.    **
16907 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61   the result of a
16908 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c   constraint, mal
16909 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72  loc() failure or
1690a 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20   IO error) then 
1690b 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68  .    ** the cach
1690c 65 20 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61  e may be interna
1690d 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74  lly inconsistent
1690e 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61   (not contain va
1690f 6c 69 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20  lid trees) so.  
16910 20 20 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73    ** we cannot s
16911 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65  imply return the
16912 20 65 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61   error to the ca
16913 6c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61  ller. Instead, a
16914 62 6f 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c  bort .    ** all
16915 20 71 75 65 72 69 65 73 20 74 68 61 74 20 6d 61   queries that ma
16916 79 20 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f  y be using any o
16917 66 20 74 68 65 20 63 75 72 73 6f 72 73 20 74 68  f the cursors th
16918 61 74 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76  at failed to sav
16919 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  e..    */.    sq
1691a 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c  lite3BtreeTripAl
1691b 6c 43 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b  lCursors(p, rc);
1691c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74  .  }.#endif.  bt
1691d 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b  reeIntegrity(p);
1691e 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61  ..  if( p->inTra
1691f 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns==TRANS_WRITE 
16920 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a  ){.    int rc2;.
16921 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 52 41  .    assert( TRA
16922 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69  NS_WRITE==pBt->i
16923 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a  nTransaction );.
16924 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65      rc2 = sqlite
16925 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70  3PagerRollback(p
16926 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
16927 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45   if( rc2!=SQLITE
16928 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
16929 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20  = rc2;.    }..  
1692a 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63    /* The rollbac
1692b 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 74 72  k may have destr
1692c 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 31 2d  oyed the pPage1-
1692d 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53  >aData value.  S
1692e 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74  o.    ** call bt
1692f 72 65 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20  reeGetPage() on 
16930 70 61 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20  page 1 again to 
16931 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65  make.    ** sure
16932 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69   pPage1->aData i
16933 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e  s set correctly.
16934 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 74 72 65   */.    if( btre
16935 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c  eGetPage(pBt, 1,
16936 20 26 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51   &pPage1, 0)==SQ
16937 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
16938 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
16939 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ge1);.    }.    
1693a 61 73 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69  assert( countWri
1693b 74 65 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d  teCursors(pBt)==
1693c 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e  0 );.    pBt->in
1693d 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52  Transaction = TR
1693e 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20  ANS_READ;.  }.. 
1693f 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63   btreeEndTransac
16940 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74  tion(p);.  sqlit
16941 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
16942 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
16943 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73  ./*.** Start a s
16944 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e  tatement subtran
16945 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62  saction. The sub
16946 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20  transaction can 
16947 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a  can be rolled.**
16948 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e   back independen
16949 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  tly of the main 
1694a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75  transaction. You
1694b 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 74 72   must start a tr
1694c 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65  ansaction .** be
1694d 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 20  fore starting a 
1694e 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  subtransaction. 
1694f 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69  The subtransacti
16950 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f  on is ended auto
16951 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66  matically .** if
16952 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61   the main transa
16953 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72  ction commits or
16954 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a   rolls back..**.
16955 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62  ** Statement sub
16956 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65  transactions are
16957 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64   used around ind
16958 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74  ividual SQL stat
16959 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61  ements.** that a
1695a 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74  re contained wit
1695b 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f  hin a BEGIN...CO
1695c 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20  MMIT block.  If 
1695d 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  a constraint.** 
1695e 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74  error occurs wit
1695f 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e  hin the statemen
16960 74 2c 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  t, the effect of
16961 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d   that one statem
16962 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f  ent.** can be ro
16963 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75  lled back withou
16964 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c  t having to roll
16965 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20  back the entire 
16966 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
16967 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73  ** A statement s
16968 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ub-transaction i
16969 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73  s implemented as
1696a 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61   an anonymous sa
1696b 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20  vepoint. The.** 
1696c 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
1696d 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
1696e 65 74 65 72 20 69 73 20 74 68 65 20 74 6f 74 61  eter is the tota
1696f 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65  l number of save
16970 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75  points,.** inclu
16971 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f  ding the new ano
16972 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74  nymous savepoint
16973 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d  , open on the B-
16974 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68  Tree. i.e. if th
16975 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63  ere.** are no ac
16976 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20  tive savepoints 
16977 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61  and no other sta
16978 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69  tement-transacti
16979 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74  ons open,.** iSt
1697a 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68  atement is 1. Th
1697b 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76  is anonymous sav
1697c 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65  epoint can be re
1697d 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64  leased or rolled
1697e 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74   back.** using t
1697f 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  he sqlite3BtreeS
16980 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74  avepoint() funct
16981 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
16982 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16983 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74  e3BtreeBeginStmt
16984 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69  (Btree *p, int i
16985 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e  Statement){.  in
16986 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64  t rc;.  BtShared
16987 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
16988 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
16989 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74  ter(p);.  assert
1698a 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
1698b 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61  ANS_WRITE );.  a
1698c 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64  ssert( pBt->read
1698d 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Only==0 );.  ass
1698e 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e  ert( iStatement>
1698f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
16990 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d  Statement>p->db-
16991 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20  >nSavepoint );. 
16992 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e   if( NEVER(p->in
16993 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49  Trans!=TRANS_WRI
16994 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f  TE || pBt->readO
16995 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  nly) ){.    rc =
16996 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c   SQLITE_INTERNAL
16997 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
16998 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72  ssert( pBt->inTr
16999 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
1699a 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a  _WRITE );.    /*
1699b 20 41 74 20 74 68 65 20 70 61 67 65 72 20 6c 65   At the pager le
1699c 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74  vel, a statement
1699d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1699e 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68  a savepoint with
1699f 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78  .    ** an index
169a0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c   greater than al
169a1 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65  l savepoints cre
169a2 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20  ated explicitly 
169a3 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c  using.    ** SQL
169a4 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20   statements. It 
169a5 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70  is illegal to op
169a6 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72  en, release or r
169a7 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20  ollback any.    
169a8 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e  ** such savepoin
169a9 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 74 61  ts while the sta
169aa 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
169ab 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20  on savepoint is 
169ac 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20  active..    */. 
169ad 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
169ae 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e  agerOpenSavepoin
169af 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
169b0 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a  Statement);.  }.
169b1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
169b2 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
169b3 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
169b4 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
169b5 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
169b6 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79  on, op, is alway
169b7 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  s SAVEPOINT_ROLL
169b8 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50  BACK.** or SAVEP
169b9 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68  OINT_RELEASE. Th
169ba 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68  is function eith
169bb 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72  er releases or r
169bc 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a  olls back the.**
169bd 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74   savepoint ident
169be 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
169bf 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64  er iSavepoint, d
169c0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
169c1 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e  value .** of op.
169c2 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c  .**.** Normally,
169c3 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67   iSavepoint is g
169c4 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
169c5 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f  qual to zero. Ho
169c6 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a  wever, if op is.
169c7 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  ** SAVEPOINT_ROL
169c8 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76  LBACK, then iSav
169c9 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20  epoint may also 
169ca 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63  be -1. In this c
169cb 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74  ase the .** cont
169cc 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69  ents of the enti
169cd 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  re transaction a
169ce 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  re rolled back. 
169cf 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e  This is differen
169d0 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d  t.** from a norm
169d1 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72  al transaction r
169d2 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c  ollback, as no l
169d3 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65  ocks are release
169d4 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61  d and the.** tra
169d5 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73  nsaction remains
169d6 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45   open..*/.SQLITE
169d7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
169d8 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
169d9 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  nt(Btree *p, int
169da 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f   op, int iSavepo
169db 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  int){.  int rc =
169dc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
169dd 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e  ( p && p->inTran
169de 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
169df 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a  {.    BtShared *
169e0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
169e1 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41    assert( op==SA
169e2 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20  VEPOINT_RELEASE 
169e3 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  || op==SAVEPOINT
169e4 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20  _ROLLBACK );.   
169e5 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f   assert( iSavepo
169e6 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65  int>=0 || (iSave
169e7 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d  point==-1 && op=
169e8 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
169e9 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  ACK) );.    sqli
169ea 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
169eb 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
169ec 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  e3PagerSavepoint
169ed 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70  (pBt->pPager, op
169ee 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20  , iSavepoint);. 
169ef 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
169f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
169f1 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70   = newDatabase(p
169f2 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  Bt);.    }.    s
169f3 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
169f4 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  (p);.  }.  retur
169f5 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
169f6 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 72 73  reate a new curs
169f7 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 65 65  or for the BTree
169f8 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f   whose root is o
169f9 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54  n the page.** iT
169fa 61 62 6c 65 2e 20 49 66 20 61 20 72 65 61 64 2d  able. If a read-
169fb 6f 6e 6c 79 20 63 75 72 73 6f 72 20 69 73 20 72  only cursor is r
169fc 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20  equested, it is 
169fd 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20  assumed that.** 
169fe 74 68 65 20 63 61 6c 6c 65 72 20 61 6c 72 65 61  the caller alrea
169ff 64 79 20 68 61 73 20 61 74 20 6c 65 61 73 74 20  dy has at least 
16a00 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e  a read-only tran
16a01 73 61 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20  saction open.** 
16a02 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
16a03 61 6c 72 65 61 64 79 2e 20 49 66 20 61 20 77 72  already. If a wr
16a04 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 72 65  ite-cursor is re
16a05 71 75 65 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a  quested, then.**
16a06 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61   the caller is a
16a07 73 73 75 6d 65 64 20 74 6f 20 68 61 76 65 20 61  ssumed to have a
16a08 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61  n open write tra
16a09 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
16a0a 49 66 20 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68  If wrFlag==0, th
16a0b 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63 61  en the cursor ca
16a0c 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 66  n only be used f
16a0d 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49  or reading..** I
16a0e 66 20 77 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65  f wrFlag==1, the
16a0f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e  n the cursor can
16a10 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61   be used for rea
16a11 64 69 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77  ding or for.** w
16a12 72 69 74 69 6e 67 20 69 66 20 6f 74 68 65 72 20  riting if other 
16a13 63 6f 6e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77  conditions for w
16a14 72 69 74 69 6e 67 20 61 72 65 20 61 6c 73 6f 20  riting are also 
16a15 6d 65 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61  met.  These.** a
16a16 72 65 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  re the condition
16a17 73 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 6d  s that must be m
16a18 65 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20  et in order for 
16a19 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65  writing to.** be
16a1a 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20   allowed:.**.** 
16a1b 31 3a 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d  1:  The cursor m
16a1c 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70  ust have been op
16a1d 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 67  ened with wrFlag
16a1e 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74  ==1.**.** 2:  Ot
16a1f 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  her database con
16a20 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68  nections that sh
16a21 61 72 65 20 74 68 65 20 73 61 6d 65 20 70 61 67  are the same pag
16a22 65 72 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20  er cache.**     
16a23 62 75 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f  but which are no
16a24 74 20 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e  t in the READ_UN
16a25 43 4f 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20  COMMITTED state 
16a26 6d 61 79 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20  may not have.** 
16a27 20 20 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e      cursors open
16a28 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20   with wrFlag==0 
16a29 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c  on the same tabl
16a2a 65 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a  e.  Otherwise.**
16a2b 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65 73       the changes
16a2c 20 6d 61 64 65 20 62 79 20 74 68 69 73 20 77 72   made by this wr
16a2d 69 74 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64  ite cursor would
16a2e 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a   be visible to.*
16a2f 2a 20 20 20 20 20 74 68 65 20 72 65 61 64 20 63  *     the read c
16a30 75 72 73 6f 72 73 20 69 6e 20 74 68 65 20 6f 74  ursors in the ot
16a31 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  her database con
16a32 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33  nection..**.** 3
16a33 3a 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20  :  The database 
16a34 6d 75 73 74 20 62 65 20 77 72 69 74 61 62 6c 65  must be writable
16a35 20 28 6e 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e   (not on read-on
16a36 6c 79 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20  ly media).**.** 
16a37 34 3a 20 20 54 68 65 72 65 20 6d 75 73 74 20 62  4:  There must b
16a38 65 20 61 6e 20 61 63 74 69 76 65 20 74 72 61 6e  e an active tran
16a39 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e  saction..**.** N
16a3a 6f 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f  o checking is do
16a3b 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ne to make sure 
16a3c 74 68 61 74 20 70 61 67 65 20 69 54 61 62 6c 65  that page iTable
16a3d 20 72 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a   really is the.*
16a3e 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61  * root page of a
16a3f 20 62 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20   b-tree.  If it 
16a40 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65  is not, then the
16a41 20 63 75 72 73 6f 72 20 61 63 71 75 69 72 65 64   cursor acquired
16a42 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72  .** will not wor
16a43 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a  k correctly..**.
16a44 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  ** It is assumed
16a45 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65   that the sqlite
16a46 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f  3BtreeCursorZero
16a47 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  () has been call
16a48 65 64 0a 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f  ed.** on pCur to
16a49 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
16a4a 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 70 72 69  memory space pri
16a4b 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74  or to invoking t
16a4c 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  his routine..*/.
16a4d 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65  static int btree
16a4e 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20  Cursor(.  Btree 
16a4f 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
16a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a51 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f   /* The btree */
16a52 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20  .  int iTable,  
16a53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a54 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f            /* Roo
16a55 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20  t page of table 
16a56 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74  to open */.  int
16a57 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20   wrFlag,        
16a58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a59 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74      /* 1 to writ
16a5a 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a  e. 0 read-only *
16a5b 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e  /.  struct KeyIn
16a5c 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20  fo *pKeyInfo,   
16a5d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
16a5e 72 73 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61  rst arg to compa
16a5f 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a  rison function *
16a60 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
16a61 75 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ur              
16a62 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
16a63 61 63 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73  ace for new curs
16a64 6f 72 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61  or */.){.  BtSha
16a65 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
16a66 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
16a67 20 20 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72    /* Shared b-tr
16a68 65 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20  ee handle */..  
16a69 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
16a6a 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
16a6b 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77  ) );.  assert( w
16a6c 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c  rFlag==0 || wrFl
16a6d 61 67 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54  ag==1 );..  /* T
16a6e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73  he following ass
16a6f 65 72 74 20 73 74 61 74 65 6d 65 6e 74 73 20 76  ert statements v
16a70 65 72 69 66 79 20 74 68 61 74 20 69 66 20 74 68  erify that if th
16a71 69 73 20 69 73 20 61 20 73 68 61 72 61 62 6c 65  is is a sharable
16a72 20 0a 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 61   .  ** b-tree da
16a73 74 61 62 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e  tabase, the conn
16a74 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e  ection is holdin
16a75 67 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74  g the required t
16a76 61 62 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a  able locks, .  *
16a77 2a 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74  * and that no ot
16a78 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  her connection h
16a79 61 73 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 73  as any open curs
16a7a 6f 72 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74  or that conflict
16a7b 73 20 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 69  s with .  ** thi
16a7c 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73  s lock.  */.  as
16a7d 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43  sert( hasSharedC
16a7e 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  acheTableLock(p,
16a7f 20 69 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66   iTable, pKeyInf
16a80 6f 21 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20  o!=0, wrFlag+1) 
16a81 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46  );.  assert( wrF
16a82 6c 61 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65  lag==0 || !hasRe
16a83 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69  adConflicts(p, i
16a84 54 61 62 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20  Table) );..  /* 
16a85 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20  Assert that the 
16a86 63 61 6c 6c 65 72 20 68 61 73 20 6f 70 65 6e 65  caller has opene
16a87 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74  d the required t
16a88 72 61 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20  ransaction. */. 
16a89 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
16a8a 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ans>TRANS_NONE )
16a8b 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c  ;.  assert( wrFl
16a8c 61 67 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72  ag==0 || p->inTr
16a8d 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
16a8e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
16a8f 74 2d 3e 70 50 61 67 65 31 20 26 26 20 70 42 74  t->pPage1 && pBt
16a90 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20  ->pPage1->aData 
16a91 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28  );..  if( NEVER(
16a92 77 72 46 6c 61 67 20 26 26 20 70 42 74 2d 3e 72  wrFlag && pBt->r
16a93 65 61 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20  eadOnly) ){.    
16a94 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45  return SQLITE_RE
16a95 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66  ADONLY;.  }.  if
16a96 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 70  ( iTable==1 && p
16a97 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
16a98 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  t)==0 ){.    ret
16a99 75 72 6e 20 53 51 4c 49 54 45 5f 45 4d 50 54 59  urn SQLITE_EMPTY
16a9a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20  ;.  }..  /* Now 
16a9b 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72  that no other er
16a9c 72 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20  rors can occur, 
16a9d 66 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69  finish filling i
16a9e 6e 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20  n the BtCursor. 
16a9f 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e   ** variables an
16aa0 64 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 73 6f  d link the curso
16aa1 72 20 69 6e 74 6f 20 74 68 65 20 42 74 53 68 61  r into the BtSha
16aa2 72 65 64 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20  red list.  */.  
16aa3 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d  pCur->pgnoRoot =
16aa4 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20   (Pgno)iTable;. 
16aa5 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d   pCur->iPage = -
16aa6 31 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 49  1;.  pCur->pKeyI
16aa7 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a  nfo = pKeyInfo;.
16aa8 20 20 70 43 75 72 2d 3e 70 42 74 72 65 65 20 3d    pCur->pBtree =
16aa9 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 20   p;.  pCur->pBt 
16aaa 3d 20 70 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77  = pBt;.  pCur->w
16aab 72 46 6c 61 67 20 3d 20 28 75 38 29 77 72 46 6c  rFlag = (u8)wrFl
16aac 61 67 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78  ag;.  pCur->pNex
16aad 74 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72  t = pBt->pCursor
16aae 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e  ;.  if( pCur->pN
16aaf 65 78 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  ext ){.    pCur-
16ab0 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20  >pNext->pPrev = 
16ab1 70 43 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d  pCur;.  }.  pBt-
16ab2 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b  >pCursor = pCur;
16ab3 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20  .  pCur->eState 
16ab4 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
16ab5 3b 0a 20 20 70 43 75 72 2d 3e 63 61 63 68 65 64  ;.  pCur->cached
16ab6 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20 72 65 74  Rowid = 0;.  ret
16ab7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
16ab8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16ab9 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16aba 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20  Cursor(.  Btree 
16abb 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
16abc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16abd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72        /* The btr
16abe 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  ee */.  int iTab
16abf 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  le,             
16ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ac1 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65      /* Root page
16ac2 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65   of table to ope
16ac3 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61  n */.  int wrFla
16ac4 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g,              
16ac5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ac6 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65     /* 1 to write
16ac7 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f  . 0 read-only */
16ac8 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66  .  struct KeyInf
16ac9 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20  o *pKeyInfo,    
16aca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16acb 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 78  * First arg to x
16acc 43 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42  Compare() */.  B
16acd 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20  tCursor *pCur   
16ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16acf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
16ad0 69 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68  ite new cursor h
16ad1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
16ad2 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  rc;.  sqlite3Btr
16ad3 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63  eeEnter(p);.  rc
16ad4 20 3d 20 62 74 72 65 65 43 75 72 73 6f 72 28 70   = btreeCursor(p
16ad5 2c 20 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67  , iTable, wrFlag
16ad6 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72  , pKeyInfo, pCur
16ad7 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
16ad8 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
16ad9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
16ada 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
16adb 20 6f 66 20 61 20 42 74 43 75 72 73 6f 72 20 6f   of a BtCursor o
16adc 62 6a 65 63 74 20 69 6e 20 62 79 74 65 73 2e 0a  bject in bytes..
16add 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
16ade 66 61 63 65 73 20 69 73 20 6e 65 65 64 65 64 20  faces is needed 
16adf 73 6f 20 74 68 61 74 20 75 73 65 72 73 20 6f 66  so that users of
16ae0 20 63 75 72 73 6f 72 73 20 63 61 6e 20 70 72 65   cursors can pre
16ae1 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66  allocate.** suff
16ae2 69 63 69 65 6e 74 20 73 74 6f 72 61 67 65 20 74  icient storage t
16ae3 6f 20 68 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e  o hold a cursor.
16ae4 20 20 54 68 65 20 42 74 43 75 72 73 6f 72 20 6f    The BtCursor o
16ae5 62 6a 65 63 74 20 69 73 20 6f 70 61 71 75 65 0a  bject is opaque.
16ae6 2a 2a 20 74 6f 20 75 73 65 72 73 20 73 6f 20 74  ** to users so t
16ae7 68 65 79 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68  hey cannot do th
16ae8 65 20 73 69 7a 65 6f 66 28 29 20 74 68 65 6d 73  e sizeof() thems
16ae9 65 6c 76 65 73 20 2d 20 74 68 65 79 20 6d 75 73  elves - they mus
16aea 74 20 63 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72  t call.** this r
16aeb 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54  outine..*/.SQLIT
16aec 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
16aed 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
16aee 53 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65  Size(void){.  re
16aef 74 75 72 6e 20 52 4f 55 4e 44 38 28 73 69 7a 65  turn ROUND8(size
16af0 6f 66 28 42 74 43 75 72 73 6f 72 29 29 3b 0a 7d  of(BtCursor));.}
16af1 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
16af2 7a 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 77  ze memory that w
16af3 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74 65 64  ill be converted
16af4 20 69 6e 74 6f 20 61 20 42 74 43 75 72 73 6f 72   into a BtCursor
16af5 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54   object..**.** T
16af6 68 65 20 73 69 6d 70 6c 65 20 61 70 70 72 6f 61  he simple approa
16af7 63 68 20 68 65 72 65 20 77 6f 75 6c 64 20 62 65  ch here would be
16af8 20 74 6f 20 6d 65 6d 73 65 74 28 29 20 74 68 65   to memset() the
16af9 20 65 6e 74 69 72 65 20 6f 62 6a 65 63 74 0a 2a   entire object.*
16afa 2a 20 74 6f 20 7a 65 72 6f 2e 20 20 42 75 74 20  * to zero.  But 
16afb 69 74 20 74 75 72 6e 73 20 6f 75 74 20 74 68 61  it turns out tha
16afc 74 20 74 68 65 20 61 70 50 61 67 65 5b 5d 20 61  t the apPage[] a
16afd 6e 64 20 61 69 49 64 78 5b 5d 20 61 72 72 61 79  nd aiIdx[] array
16afe 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 65 64  s.** do not need
16aff 20 74 6f 20 62 65 20 7a 65 72 6f 65 64 20 61 6e   to be zeroed an
16b00 64 20 74 68 65 79 20 61 72 65 20 6c 61 72 67 65  d they are large
16b01 2c 20 73 6f 20 77 65 20 63 61 6e 20 73 61 76 65  , so we can save
16b02 20 61 20 6c 6f 74 0a 2a 2a 20 6f 66 20 72 75 6e   a lot.** of run
16b03 2d 74 69 6d 65 20 62 79 20 73 6b 69 70 70 69 6e  -time by skippin
16b04 67 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61  g the initializa
16b05 74 69 6f 6e 20 6f 66 20 74 68 6f 73 65 20 65 6c  tion of those el
16b06 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ements..*/.SQLIT
16b07 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
16b08 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
16b09 72 5a 65 72 6f 28 42 74 43 75 72 73 6f 72 20 2a  rZero(BtCursor *
16b0a 70 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20  p){.  memset(p, 
16b0b 30 2c 20 6f 66 66 73 65 74 6f 66 28 42 74 43 75  0, offsetof(BtCu
16b0c 72 73 6f 72 2c 20 69 50 61 67 65 29 29 3b 0a 7d  rsor, iPage));.}
16b0d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
16b0e 63 61 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c  cached rowid val
16b0f 75 65 20 6f 66 20 65 76 65 72 79 20 63 75 72 73  ue of every curs
16b10 6f 72 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64  or in the same d
16b11 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20  atabase file.** 
16b12 61 73 20 70 43 75 72 20 61 6e 64 20 68 61 76 69  as pCur and havi
16b13 6e 67 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74  ng the same root
16b14 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20   page number as 
16b15 70 43 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65  pCur.  The value
16b16 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52   is.** set to iR
16b17 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79  owid..**.** Only
16b18 20 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20   positive rowid 
16b19 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69  values are consi
16b1a 64 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20  dered valid for 
16b1b 74 68 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54  this cache..** T
16b1c 68 65 20 63 61 63 68 65 20 69 73 20 69 6e 69 74  he cache is init
16b1d 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c  ialized to zero,
16b1e 20 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69   indicating an i
16b1f 6e 76 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a  nvalid cache..**
16b20 20 41 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f   A btree will wo
16b21 72 6b 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72  rk fine with zer
16b22 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f  o or negative ro
16b23 77 69 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63  wids.  We just c
16b24 61 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a  annot.** cache z
16b25 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20  ero or negative 
16b26 72 6f 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65  rowids, which me
16b27 61 6e 73 20 74 61 62 6c 65 73 20 74 68 61 74 20  ans tables that 
16b28 75 73 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e  use zero or.** n
16b29 65 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d  egative rowids m
16b2a 69 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c  ight run a littl
16b2b 65 20 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69  e slower.  But i
16b2c 6e 20 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f  n practice, zero
16b2d 0a 2a 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20  .** or negative 
16b2e 72 6f 77 69 64 73 20 61 72 65 20 76 65 72 79 20  rowids are very 
16b2f 75 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73  uncommon so this
16b30 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61   should not be a
16b31 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c   problem..*/.SQL
16b32 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
16b33 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
16b34 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75  CachedRowid(BtCu
16b35 72 73 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69  rsor *pCur, sqli
16b36 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64  te3_int64 iRowid
16b37 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ){.  BtCursor *p
16b38 3b 0a 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e  ;.  for(p=pCur->
16b39 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b  pBt->pCursor; p;
16b3a 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
16b3b 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f    if( p->pgnoRoo
16b3c 74 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  t==pCur->pgnoRoo
16b3d 74 20 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77  t ) p->cachedRow
16b3e 69 64 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d  id = iRowid;.  }
16b3f 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
16b40 3e 63 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52  >cachedRowid==iR
16b41 6f 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  owid );.}../*.**
16b42 20 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68   Return the cach
16b43 65 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65  ed rowid for the
16b44 20 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20   given cursor.  
16b45 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65  A negative or ze
16b46 72 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c  ro.** return val
16b47 75 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  ue indicates tha
16b48 74 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68  t the rowid cach
16b49 65 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64  e is invalid and
16b4a 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67   should be.** ig
16b4b 6e 6f 72 65 64 2e 20 20 49 66 20 74 68 65 20 72  nored.  If the r
16b4c 6f 77 69 64 20 63 61 63 68 65 20 68 61 73 20 6e  owid cache has n
16b4d 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e  ever before been
16b4e 20 73 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20   set, then a.** 
16b4f 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
16b50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16b51 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ATE sqlite3_int6
16b52 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  4 sqlite3BtreeGe
16b53 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43  tCachedRowid(BtC
16b54 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
16b55 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63  return pCur->cac
16b56 68 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a  hedRowid;.}../*.
16b57 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f  ** Close a curso
16b58 72 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63  r.  The read loc
16b59 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
16b5a 65 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73  e file is releas
16b5b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c  ed.** when the l
16b5c 61 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c  ast cursor is cl
16b5d 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  osed..*/.SQLITE_
16b5e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
16b5f 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72  te3BtreeCloseCur
16b60 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  sor(BtCursor *pC
16b61 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42  ur){.  Btree *pB
16b62 74 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74  tree = pCur->pBt
16b63 72 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65  ree;.  if( pBtre
16b64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  e ){.    int i;.
16b65 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42      BtShared *pB
16b66 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20  t = pCur->pBt;. 
16b67 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45     sqlite3BtreeE
16b68 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20  nter(pBtree);.  
16b69 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
16b6a 65 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b  earCursor(pCur);
16b6b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70  .    if( pCur->p
16b6c 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 43  Prev ){.      pC
16b6d 75 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74  ur->pPrev->pNext
16b6e 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a   = pCur->pNext;.
16b6f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
16b70 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20   pBt->pCursor = 
16b71 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pCur->pNext;.   
16b72 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d   }.    if( pCur-
16b73 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20  >pNext ){.      
16b74 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72  pCur->pNext->pPr
16b75 65 76 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 76  ev = pCur->pPrev
16b76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
16b77 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50  i=0; i<=pCur->iP
16b78 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  age; i++){.     
16b79 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75   releasePage(pCu
16b7a 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20  r->apPage[i]);. 
16b7b 20 20 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42     }.    unlockB
16b7c 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74  treeIfUnused(pBt
16b7d 29 3b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74  );.    invalidat
16b7e 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70  eOverflowCache(p
16b7f 43 75 72 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c  Cur);.    /* sql
16b80 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b  ite3_free(pCur);
16b81 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42   */.    sqlite3B
16b82 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65  treeLeave(pBtree
16b83 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
16b84 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
16b85 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
16b86 65 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65  e BtCursor* give
16b87 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e  n in the argumen
16b88 74 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a  t has a valid.**
16b89 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73   BtCursor.info s
16b8a 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 74  tructure.  If it
16b8b 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
16b8c 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62  valid, call.** b
16b8d 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20  treeParseCell() 
16b8e 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a  to fill it in..*
16b8f 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e  *.** BtCursor.in
16b90 66 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f 66  fo is a cache of
16b91 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   the information
16b92 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
16b93 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74  cell..** Using t
16b94 68 69 73 20 63 61 63 68 65 20 72 65 64 75 63 65  his cache reduce
16b95 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
16b96 63 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61  calls to btreePa
16b97 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a  rseCell()..**.**
16b98 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68   2007-06-25:  Th
16b99 65 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20  ere is a bug in 
16b9a 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  some versions of
16b9b 20 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65   MSVC that cause
16b9c 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72   the.** compiler
16b9d 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67   to crash when g
16b9e 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20  etCellInfo() is 
16b9f 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61  implemented as a
16ba0 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74   macro..** But t
16ba1 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 72  here is a measur
16ba2 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 61  eable speed adva
16ba3 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74  ntage to using t
16ba4 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a  he macro on gcc.
16ba5 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f  ** (when less co
16ba6 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74  mpiler optimizat
16ba7 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72  ions like -Os or
16ba8 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e   -O0 are used an
16ba9 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65  d the.** compile
16baa 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61  r is not doing a
16bab 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e  gressive inlinin
16bac 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61  g.)  So we use a
16bad 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a   real function.*
16bae 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61  * for MSVC and a
16baf 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79   macro for every
16bb0 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63  thing else.  Tic
16bb1 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69  ket #2457..*/.#i
16bb2 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73  fndef NDEBUG.  s
16bb3 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72  tatic void asser
16bb4 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73  tCellInfo(BtCurs
16bb5 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43  or *pCur){.    C
16bb6 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
16bb7 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43    int iPage = pC
16bb8 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d  ur->iPage;.    m
16bb9 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20  emset(&info, 0, 
16bba 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20  sizeof(info));. 
16bbb 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c     btreeParseCel
16bbc 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  l(pCur->apPage[i
16bbd 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49  Page], pCur->aiI
16bbe 64 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f  dx[iPage], &info
16bbf 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  );.    assert( m
16bc0 65 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43  emcmp(&info, &pC
16bc1 75 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66  ur->info, sizeof
16bc2 28 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20  (info))==0 );.  
16bc3 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  }.#else.  #defin
16bc4 65 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f  e assertCellInfo
16bc5 28 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  (x).#endif.#ifde
16bc6 66 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20  f _MSC_VER.  /* 
16bc7 55 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74  Use a real funct
16bc8 69 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77  ion in MSVC to w
16bc9 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20  ork around bugs 
16bca 69 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72  in that compiler
16bcb 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f  . */.  static vo
16bcc 69 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42  id getCellInfo(B
16bcd 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
16bce 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e      if( pCur->in
16bcf 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20  fo.nSize==0 ){. 
16bd0 20 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d       int iPage =
16bd1 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20   pCur->iPage;.  
16bd2 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65      btreeParseCe
16bd3 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  ll(pCur->apPage[
16bd4 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49  iPage],pCur->aiI
16bd5 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d  dx[iPage],&pCur-
16bd6 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43  >info);.      pC
16bd7 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
16bd8 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
16bd9 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e      assertCellIn
16bda 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a  fo(pCur);.    }.
16bdb 20 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20    }.#else /* if 
16bdc 6e 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a  not _MSC_VER */.
16bdd 20 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f    /* Use a macro
16bde 20 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f   in all other co
16bdf 6d 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20  mpilers so that 
16be0 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
16be1 69 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69  inlined */.#defi
16be2 6e 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70  ne getCellInfo(p
16be3 43 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20  Cur)            
16be4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16be5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16be6 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
16be7 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69  ( pCur->info.nSi
16be8 7a 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20  ze==0 ){        
16be9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16beb 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
16bec 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75   int iPage = pCu
16bed 72 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20  r->iPage;       
16bee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bf0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
16bf1 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
16bf2 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50  (pCur->apPage[iP
16bf3 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78  age],pCur->aiIdx
16bf4 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69  [iPage],&pCur->i
16bf5 6e 66 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72  nfo); \.    pCur
16bf6 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b  ->validNKey = 1;
16bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bfa 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65         \.  }else
16bfb 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
16bfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bff 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73          \.    as
16c00 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75  sertCellInfo(pCu
16c01 72 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  r);             
16c02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c04 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23           \.  }.#
16c05 65 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45  endif /* _MSC_VE
16c06 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44  R */..#ifndef ND
16c07 45 42 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78  EBUG  /* The nex
16c08 74 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f  t routine used o
16c09 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 72  nly within asser
16c0a 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a  t() statements *
16c0b 2f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  /./*.** Return t
16c0c 72 75 65 20 69 66 20 74 68 65 20 67 69 76 65 6e  rue if the given
16c0d 20 42 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c   BtCursor is val
16c0e 69 64 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72  id.  A valid cur
16c0f 73 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68  sor is one.** th
16c10 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  at is currently 
16c11 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f  pointing to a ro
16c12 77 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74  w in a (non-empt
16c13 79 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69  y) table..** Thi
16c14 73 20 69 73 20 61 20 76 65 72 69 66 69 63 61 74  s is a verificat
16c15 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75  ion routine is u
16c16 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20  sed only within 
16c17 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
16c18 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts..*/.SQLITE_P
16c19 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16c1a 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56  e3BtreeCursorIsV
16c1b 61 6c 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70  alid(BtCursor *p
16c1c 43 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  Cur){.  return p
16c1d 43 75 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74  Cur && pCur->eSt
16c1e 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
16c1f 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  D;.}.#endif /* N
16c20 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  DEBUG */../*.** 
16c21 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68  Set *pSize to th
16c22 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75  e size of the bu
16c23 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68  ffer needed to h
16c24 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  old the value of
16c25 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20  .** the key for 
16c26 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72  the current entr
16c27 79 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f  y.  If the curso
16c28 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e  r is not pointin
16c29 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20  g.** to a valid 
16c2a 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73  entry, *pSize is
16c2b 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a   set to 0. .**.*
16c2c 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69  * For a table wi
16c2d 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c  th the INTKEY fl
16c2e 61 67 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75  ag set, this rou
16c2f 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
16c30 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20   key.** itself, 
16c31 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  not the number o
16c32 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b  f bytes in the k
16c33 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  ey..**.** The ca
16c34 6c 6c 65 72 20 6d 75 73 74 20 70 6f 73 69 74 69  ller must positi
16c35 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 70 72  on the cursor pr
16c36 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20  ior to invoking 
16c37 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  this routine..**
16c38 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e   .** This routin
16c39 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20  e cannot fail.  
16c3a 49 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  It always return
16c3b 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a  s SQLITE_OK.  .*
16c3c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16c3d 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16c3e 65 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f  eKeySize(BtCurso
16c3f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53  r *pCur, i64 *pS
16c40 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ize){.  assert( 
16c41 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
16c42 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
16c43 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
16c44 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
16c45 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65   || pCur->eState
16c46 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
16c47 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53  ;.  if( pCur->eS
16c48 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c  tate!=CURSOR_VAL
16c49 49 44 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65  ID ){.    *pSize
16c4a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
16c4b 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70     getCellInfo(p
16c4c 43 75 72 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65  Cur);.    *pSize
16c4d 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b   = pCur->info.nK
16c4e 65 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ey;.  }.  return
16c4f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
16c50 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20  *.** Set *pSize 
16c51 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
16c52 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 69   bytes of data i
16c53 6e 20 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a  n the entry the.
16c54 2a 2a 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e  ** cursor curren
16c55 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a  tly points to..*
16c56 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20  *.** The caller 
16c57 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74  must guarantee t
16c58 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69  hat the cursor i
16c59 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20  s pointing to a 
16c5a 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69  non-NULL.** vali
16c5b 64 20 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68  d entry.  In oth
16c5c 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61  er words, the ca
16c5d 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20  lling procedure 
16c5e 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 0a 2a  must guarantee.*
16c5f 2a 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  * that the curso
16c60 72 20 68 61 73 20 43 75 72 73 6f 72 2e 65 53 74  r has Cursor.eSt
16c61 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
16c62 44 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65  D..**.** Failure
16c63 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
16c64 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  .  This function
16c65 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
16c66 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74  SQLITE_OK..** It
16c67 20 6d 69 67 68 74 20 6a 75 73 74 20 61 73 20 77   might just as w
16c68 65 6c 6c 20 62 65 20 61 20 70 72 6f 63 65 64 75  ell be a procedu
16c69 72 65 20 28 72 65 74 75 72 6e 69 6e 67 20 76 6f  re (returning vo
16c6a 69 64 29 20 62 75 74 20 77 65 20 63 6f 6e 74 69  id) but we conti
16c6b 6e 75 65 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e  nue.** to return
16c6c 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75   an integer resu
16c6d 6c 74 20 63 6f 64 65 20 66 6f 72 20 68 69 73 74  lt code for hist
16c6e 6f 72 69 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a  orical reasons..
16c6f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16c70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
16c71 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72  eeDataSize(BtCur
16c72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a  sor *pCur, u32 *
16c73 70 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74  pSize){.  assert
16c74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16c75 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
16c76 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
16c77 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
16c78 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66   );.  getCellInf
16c79 6f 28 70 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a  o(pCur);.  *pSiz
16c7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  e = pCur->info.n
16c7b 44 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 53  Data;.  return S
16c7c 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
16c7d 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67  ** Given the pag
16c7e 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f  e number of an o
16c7f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20  verflow page in 
16c80 74 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61  the database (pa
16c81 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29  rameter.** ovfl)
16c82 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
16c83 66 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e  finds the page n
16c84 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78  umber of the nex
16c85 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a  t page in the .*
16c86 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  * linked list of
16c87 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
16c88 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74   If possible, it
16c89 20 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76   uses the auto-v
16c8a 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72  acuum.** pointer
16c8b 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61  -map data instea
16c8c 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65  d of reading the
16c8d 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65   content of page
16c8e 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20   ovfl to do so. 
16c8f 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
16c90 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c  or occurs an SQL
16c91 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ite error code i
16c92 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  s returned. Othe
16c93 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65  rwise:.**.** The
16c94 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
16c95 74 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f  the next overflo
16c96 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69  w page in the li
16c97 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a  nked list is .**
16c98 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67   written to *pPg
16c99 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20  noNext. If page 
16c9a 6f 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74  ovfl is the last
16c9b 20 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e   page in its lin
16c9c 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70  ked .** list, *p
16c9d 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20  PgnoNext is set 
16c9e 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20  to zero. .**.** 
16c9f 49 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74  If ppPage is not
16ca0 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66   NULL, and a ref
16ca1 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65  erence to the Me
16ca2 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72  mPage object cor
16ca3 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f  responding.** to
16ca4 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76   page number pOv
16ca5 66 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c  fl was obtained,
16ca6 20 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73   then *ppPage is
16ca7 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
16ca8 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e   that.** referen
16ca9 63 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65  ce. It is the re
16caa 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
16cab 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61  the caller to ca
16cac 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29  ll releasePage()
16cad 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74  .** on *ppPage t
16cae 6f 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72  o free the refer
16caf 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65  ence. In no refe
16cb0 72 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e  rence was obtain
16cb1 65 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74  ed (because.** t
16cb2 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77  he pointer-map w
16cb3 61 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69  as used to obtai
16cb4 6e 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20  n the value for 
16cb5 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65  *pPgnoNext), the
16cb6 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20  n.** *ppPage is 
16cb7 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a  set to zero..*/.
16cb8 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76  static int getOv
16cb9 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74  erflowPage(.  Bt
16cba 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20  Shared *pBt,    
16cbb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
16cbc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
16cbd 2a 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20  */.  Pgno ovfl, 
16cbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16cbf 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65    /* Current ove
16cc0 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65  rflow page numbe
16cc1 72 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  r */.  MemPage *
16cc2 2a 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20  *ppPage,        
16cc3 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50      /* OUT: MemP
16cc4 61 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20  age handle (may 
16cc5 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67  be NULL) */.  Pg
16cc6 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20  no *pPgnoNext   
16cc7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
16cc8 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77  T: Next overflow
16cc9 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
16cca 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d  ){.  Pgno next =
16ccb 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70   0;.  MemPage *p
16ccc 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Page = 0;.  int 
16ccd 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
16cce 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16ccf 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
16cd0 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
16cd1 73 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29  ssert(pPgnoNext)
16cd2 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
16cd3 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
16cd4 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69  M.  /* Try to fi
16cd5 6e 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65  nd the next page
16cd6 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   in the overflow
16cd7 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a   list using the.
16cd8 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20    ** autovacuum 
16cd9 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
16cda 73 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 68  s. Guess that th
16cdb 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a  e next page in .
16cdc 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f    ** the overflo
16cdd 77 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e  w list is page n
16cde 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20  umber (ovfl+1). 
16cdf 49 66 20 74 68 61 74 20 67 75 65 73 73 20 74 75  If that guess tu
16ce0 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f  rns .  ** out to
16ce1 20 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20   be wrong, fall 
16ce2 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20  back to loading 
16ce3 74 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 65  the data of page
16ce4 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76   .  ** number ov
16ce5 66 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  fl to determine 
16ce6 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75  the next page nu
16ce7 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  mber..  */.  if(
16ce8 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
16ce9 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e   ){.    Pgno pgn
16cea 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65  o;.    Pgno iGue
16ceb 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20  ss = ovfl+1;.   
16cec 20 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20   u8 eType;..    
16ced 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53  while( PTRMAP_IS
16cee 50 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 73  PAGE(pBt, iGuess
16cef 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e  ) || iGuess==PEN
16cf0 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
16cf1 42 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75  Bt) ){.      iGu
16cf2 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  ess++;.    }..  
16cf3 20 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61    if( iGuess<=pa
16cf4 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
16cf5 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
16cf6 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69  ptrmapGet(pBt, i
16cf7 47 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26  Guess, &eType, &
16cf8 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28  pgno);.      if(
16cf9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
16cfa 26 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  & eType==PTRMAP_
16cfb 4f 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e  OVERFLOW2 && pgn
16cfc 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20  o==ovfl ){.     
16cfd 20 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73     next = iGuess
16cfe 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ;.        rc = S
16cff 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
16d00 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65    }.    }.  }.#e
16d01 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20  ndif..  assert( 
16d02 6e 65 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53  next==0 || rc==S
16d03 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20  QLITE_DONE );.  
16d04 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16d05 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74  K ){.    rc = bt
16d06 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
16d07 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29  ovfl, &pPage, 0)
16d08 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
16d09 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
16d0a 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69  Page==0 );.    i
16d0b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16d0c 20 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d   ){.      next =
16d0d 20 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d   get4byte(pPage-
16d0e 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20  >aData);.    }. 
16d0f 20 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74   }..  *pPgnoNext
16d10 20 3d 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70   = next;.  if( p
16d11 70 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70  pPage ){.    *pp
16d12 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20  Page = pPage;.  
16d13 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61  }else{.    relea
16d14 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
16d15 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d   }.  return (rc=
16d16 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53  =SQLITE_DONE ? S
16d17 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a  QLITE_OK : rc);.
16d18 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61  }../*.** Copy da
16d19 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72  ta from a buffer
16d1a 20 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66   to a page, or f
16d1b 72 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20  rom a page to a 
16d1c 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50  buffer..**.** pP
16d1d 61 79 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e  ayload is a poin
16d1e 74 65 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72  ter to data stor
16d1f 65 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70  ed on database p
16d20 61 67 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20  age pDbPage..** 
16d21 49 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20  If argument eOp 
16d22 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e  is false, then n
16d23 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61  Byte bytes of da
16d24 74 61 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a  ta are copied.**
16d25 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74   from pPayload t
16d26 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69  o the buffer poi
16d27 6e 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e  nted at by pBuf.
16d28 20 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c   If eOp is true,
16d29 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33  .** then sqlite3
16d2a 50 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20  PagerWrite() is 
16d2b 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67  called on pDbPag
16d2c 65 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65  e and nByte byte
16d2d 73 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65  s.** of data are
16d2e 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65   copied from the
16d2f 20 62 75 66 66 65 72 20 70 42 75 66 20 74 6f 20   buffer pBuf to 
16d30 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20  pPayload..**.** 
16d31 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
16d32 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73  urned on success
16d33 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65  , otherwise an e
16d34 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
16d35 61 74 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79  atic int copyPay
16d36 6c 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50  load(.  void *pP
16d37 61 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20  ayload,         
16d38 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
16d39 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76  page data */.  v
16d3a 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20  oid *pBuf,      
16d3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
16d3c 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f  ter to buffer */
16d3d 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20  .  int nByte,   
16d3e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16d3f 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
16d40 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74  to copy */.  int
16d41 20 65 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20   eOp,           
16d42 20 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63         /* 0 -> c
16d43 6f 70 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31  opy from page, 1
16d44 20 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65   -> copy to page
16d45 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44   */.  DbPage *pD
16d46 62 50 61 67 65 20 20 20 20 20 20 20 20 20 20 20  bPage           
16d47 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69  /* Page containi
16d48 6e 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29  ng pPayload */.)
16d49 7b 0a 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20  {.  if( eOp ){. 
16d4a 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20     /* Copy data 
16d4b 66 72 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70  from buffer to p
16d4c 61 67 65 20 28 61 20 77 72 69 74 65 20 6f 70 65  age (a write ope
16d4d 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69  ration) */.    i
16d4e 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  nt rc = sqlite3P
16d4f 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67  agerWrite(pDbPag
16d50 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
16d51 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16d52 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
16d53 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70    }.    memcpy(p
16d54 50 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e  Payload, pBuf, n
16d55 42 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Byte);.  }else{.
16d56 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61      /* Copy data
16d57 20 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75   from page to bu
16d58 66 66 65 72 20 28 61 20 72 65 61 64 20 6f 70 65  ffer (a read ope
16d59 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d  ration) */.    m
16d5a 65 6d 63 70 79 28 70 42 75 66 2c 20 70 50 61 79  emcpy(pBuf, pPay
16d5b 6c 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20  load, nByte);.  
16d5c 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
16d5d 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
16d5e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
16d5f 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20  used to read or 
16d60 6f 76 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61  overwrite payloa
16d61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  d information.**
16d62 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74   for the entry t
16d63 68 61 74 20 74 68 65 20 70 43 75 72 20 63 75 72  hat the pCur cur
16d64 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  sor is pointing 
16d65 74 6f 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a  to. If the eOp.*
16d66 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30  * parameter is 0
16d67 2c 20 74 68 69 73 20 69 73 20 61 20 72 65 61 64  , this is a read
16d68 20 6f 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61   operation (data
16d69 20 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20   copied into.** 
16d6a 62 75 66 66 65 72 20 70 42 75 66 29 2e 20 49 66  buffer pBuf). If
16d6b 20 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c   it is non-zero,
16d6c 20 61 20 77 72 69 74 65 20 28 64 61 74 61 20 63   a write (data c
16d6d 6f 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75  opied from.** bu
16d6e 66 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a  ffer pBuf)..**.*
16d6f 2a 20 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d  * A total of "am
16d70 74 22 20 62 79 74 65 73 20 61 72 65 20 72 65 61  t" bytes are rea
16d71 64 20 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67  d or written beg
16d72 69 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65  inning at "offse
16d73 74 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72  t"..** Data is r
16d74 65 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74  ead to or from t
16d75 68 65 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a  he buffer pBuf..
16d76 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e  **.** The conten
16d77 74 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 20  t being read or 
16d78 77 72 69 74 74 65 6e 20 6d 69 67 68 74 20 61 70  written might ap
16d79 70 65 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e  pear on the main
16d7a 20 70 61 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73   page.** or be s
16d7b 63 61 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20  cattered out on 
16d7c 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f  multiple overflo
16d7d 77 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  w pages..**.** I
16d7e 66 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69  f the BtCursor.i
16d7f 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
16d80 66 6c 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64  flag is set, and
16d81 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
16d82 63 75 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65  cursor entry use
16d83 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76  s one or more ov
16d84 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68  erflow pages, th
16d85 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61  is function.** a
16d86 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66  llocates space f
16d87 6f 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f  or and lazily po
16d88 70 6c 75 61 74 65 73 20 74 68 65 20 6f 76 65 72  pluates the over
16d89 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a  flow page-list .
16d8a 2a 2a 20 63 61 63 68 65 20 61 72 72 61 79 20 28  ** cache array (
16d8b 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c  BtCursor.aOverfl
16d8c 6f 77 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20  ow). Subsequent 
16d8d 63 61 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a  calls use this.*
16d8e 2a 20 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20  * cache to make 
16d8f 73 65 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73  seeking to the s
16d90 75 70 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d  upplied offset m
16d91 6f 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a  ore efficient..*
16d92 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65  *.** Once an ove
16d93 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20  rflow page-list 
16d94 63 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61  cache has been a
16d95 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79  llocated, it may
16d96 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74   be.** invalidat
16d97 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  ed if some other
16d98 20 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74   cursor writes t
16d99 6f 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  o the same table
16d9a 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63  , or if.** the c
16d9b 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74  ursor is moved t
16d9c 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f  o a different ro
16d9d 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  w. Additionally,
16d9e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a   in auto-vacuum.
16d9f 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c  ** mode, the fol
16da0 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61  lowing events ma
16da1 79 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20  y invalidate an 
16da2 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69  overflow page-li
16da3 73 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20  st cache..**.** 
16da4 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74    * An increment
16da5 61 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20  al vacuum,.**   
16da6 2a 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75  * A commit in au
16da7 74 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22  to_vacuum="full"
16da8 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72   mode,.**   * Cr
16da9 65 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28  eating a table (
16daa 6d 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69  may require movi
16dab 6e 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  ng an overflow p
16dac 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  age)..*/.static 
16dad 69 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61  int accessPayloa
16dae 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  d(.  BtCursor *p
16daf 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72  Cur,      /* Cur
16db0 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  sor pointing to 
16db1 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72  entry to read fr
16db2 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73  om */.  u32 offs
16db3 65 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  et,          /* 
16db4 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68  Begin reading th
16db5 69 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c  is far into payl
16db6 6f 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74  oad */.  u32 amt
16db7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
16db8 20 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20   Read this many 
16db9 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67  bytes */.  unsig
16dba 6e 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20  ned char *pBuf, 
16dbb 2f 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 74  /* Write the byt
16dbc 65 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66  es into this buf
16dbd 66 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f  fer */ .  int eO
16dbe 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p              /
16dbf 2a 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20  * zero to read. 
16dc0 6e 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74  non-zero to writ
16dc1 65 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67  e. */.){.  unsig
16dc2 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f  ned char *aPaylo
16dc3 61 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ad;.  int rc = S
16dc4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20  QLITE_OK;.  u32 
16dc5 6e 4b 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78  nKey;.  int iIdx
16dc6 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20   = 0;.  MemPage 
16dc7 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  *pPage = pCur->a
16dc8 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
16dc9 65 5d 3b 20 2f 2a 20 42 74 72 65 65 20 70 61 67  e]; /* Btree pag
16dca 65 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74  e of current ent
16dcb 72 79 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64  ry */.  BtShared
16dcc 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42   *pBt = pCur->pB
16dcd 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
16dce 20 20 20 20 2f 2a 20 42 74 72 65 65 20 74 68 69      /* Btree thi
16dcf 73 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73  s cursor belongs
16dd0 20 74 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74   to */..  assert
16dd1 28 20 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73  ( pPage );.  ass
16dd2 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
16dd3 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
16dd4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
16dd5 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
16dd6 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65  Page]<pPage->nCe
16dd7 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ll );.  assert( 
16dd8 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
16dd9 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74  (pCur) );..  get
16dda 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a  CellInfo(pCur);.
16ddb 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75    aPayload = pCu
16ddc 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20  r->info.pCell + 
16ddd 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64  pCur->info.nHead
16dde 65 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50  er;.  nKey = (pP
16ddf 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20  age->intKey ? 0 
16de0 3a 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66  : (int)pCur->inf
16de1 6f 2e 6e 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20  o.nKey);..  if( 
16de2 4e 45 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74  NEVER(offset+amt
16de3 20 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e   > nKey+pCur->in
16de4 66 6f 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c  fo.nData) .   ||
16de5 20 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d   &aPayload[pCur-
16de6 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20  >info.nLocal] > 
16de7 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42  &pPage->aData[pB
16de8 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20  t->usableSize]. 
16de9 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e   ){.    /* Tryin
16dea 67 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  g to read or wri
16deb 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  te past the end 
16dec 6f 66 20 74 68 65 20 64 61 74 61 20 69 73 20 61  of the data is a
16ded 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72  n error */.    r
16dee 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
16def 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a  RUPT_BKPT;.  }..
16df0 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61    /* Check if da
16df1 74 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f  ta must be read/
16df2 77 72 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20  written to/from 
16df3 74 68 65 20 62 74 72 65 65 20 70 61 67 65 20 69  the btree page i
16df4 74 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20  tself. */.  if( 
16df5 6f 66 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66  offset<pCur->inf
16df6 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20  o.nLocal ){.    
16df7 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20  int a = amt;.   
16df8 20 69 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43   if( a+offset>pC
16df9 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20  ur->info.nLocal 
16dfa 29 7b 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75  ){.      a = pCu
16dfb 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d  r->info.nLocal -
16dfc 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20   offset;.    }. 
16dfd 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c     rc = copyPayl
16dfe 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66  oad(&aPayload[of
16dff 66 73 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20  fset], pBuf, a, 
16e00 65 4f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50  eOp, pPage->pDbP
16e01 61 67 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74  age);.    offset
16e02 20 3d 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b   = 0;.    pBuf +
16e03 3d 20 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20  = a;.    amt -= 
16e04 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  a;.  }else{.    
16e05 6f 66 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e  offset -= pCur->
16e06 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d  info.nLocal;.  }
16e07 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
16e08 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29  TE_OK && amt>0 )
16e09 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20  {.    const u32 
16e0a 6f 76 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e  ovflSize = pBt->
16e0b 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20  usableSize - 4; 
16e0c 20 2f 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e   /* Bytes conten
16e0d 74 20 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20  t per ovfl page 
16e0e 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74  */.    Pgno next
16e0f 50 61 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50  Page;..    nextP
16e10 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26  age = get4byte(&
16e11 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69  aPayload[pCur->i
16e12 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23  nfo.nLocal]);..#
16e13 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16e14 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20  IT_INCRBLOB.    
16e15 2f 2a 20 49 66 20 74 68 65 20 69 73 49 6e 63 72  /* If the isIncr
16e16 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20  blobHandle flag 
16e17 69 73 20 73 65 74 20 61 6e 64 20 74 68 65 20 42  is set and the B
16e18 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f  tCursor.aOverflo
16e19 77 5b 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e  w[].    ** has n
16e1a 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  ot been allocate
16e1b 64 2c 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e  d, allocate it n
16e1c 6f 77 2e 20 54 68 65 20 61 72 72 61 79 20 69 73  ow. The array is
16e1d 20 73 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a   sized at.    **
16e1e 20 6f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   one entry for e
16e1f 61 63 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ach overflow pag
16e20 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f  e in the overflo
16e21 77 20 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20  w chain. The.   
16e22 20 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20   ** page number 
16e23 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f 76 65  of the first ove
16e24 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74  rflow page is st
16e25 6f 72 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f  ored in aOverflo
16e26 77 5b 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63  w[0],.    ** etc
16e27 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69  . A value of 0 i
16e28 6e 20 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b  n the aOverflow[
16e29 5d 20 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e  ] array means "n
16e2a 6f 74 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20  ot yet known".  
16e2b 20 20 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20    ** (the cache 
16e2c 69 73 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61  is lazily popula
16e2d 74 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ted)..    */.   
16e2e 20 69 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63   if( pCur->isInc
16e2f 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21  rblobHandle && !
16e30 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
16e31 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76  ){.      int nOv
16e32 66 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f  fl = (pCur->info
16e33 2e 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e  .nPayload-pCur->
16e34 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c  info.nLocal+ovfl
16e35 53 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65  Size-1)/ovflSize
16e36 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f  ;.      pCur->aO
16e37 76 65 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20  verflow = (Pgno 
16e38 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  *)sqlite3MallocZ
16e39 65 72 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29  ero(sizeof(Pgno)
16e3a 2a 6e 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f  *nOvfl);.      /
16e3b 2a 20 6e 4f 76 66 6c 20 69 73 20 61 6c 77 61 79  * nOvfl is alway
16e3c 73 20 70 6f 73 69 74 69 76 65 2e 20 20 49 66 20  s positive.  If 
16e3d 69 74 20 77 65 72 65 20 7a 65 72 6f 2c 20 66 65  it were zero, fe
16e3e 74 63 68 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64  tchPayload would
16e3f 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 62   have.      ** b
16e40 65 65 6e 20 75 73 65 64 20 69 6e 73 74 65 61 64  een used instead
16e41 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
16e42 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41  . */.      if( A
16e43 4c 57 41 59 53 28 6e 4f 76 66 6c 29 20 26 26 20  LWAYS(nOvfl) && 
16e44 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77  !pCur->aOverflow
16e45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
16e46 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
16e47 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
16e48 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72    /* If the over
16e49 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63  flow page-list c
16e4a 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c  ache has been al
16e4b 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a  located and the.
16e4c 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72      ** entry for
16e4d 20 74 68 65 20 66 69 72 73 74 20 72 65 71 75 69   the first requi
16e4e 72 65 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  red overflow pag
16e4f 65 20 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70  e is valid, skip
16e50 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79  .    ** directly
16e51 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20   to it..    */. 
16e52 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76     if( pCur->aOv
16e53 65 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e  erflow && pCur->
16e54 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74  aOverflow[offset
16e55 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20  /ovflSize] ){.  
16e56 20 20 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73      iIdx = (offs
16e57 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20  et/ovflSize);.  
16e58 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70      nextPage = p
16e59 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
16e5a 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73  Idx];.      offs
16e5b 65 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66  et = (offset%ovf
16e5c 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65  lSize);.    }.#e
16e5d 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b  ndif..    for( ;
16e5e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
16e5f 26 20 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50  & amt>0 && nextP
16e60 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23  age; iIdx++){..#
16e61 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16e62 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20  IT_INCRBLOB.    
16e63 20 20 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64    /* If required
16e64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f  , populate the o
16e65 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
16e66 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20  t cache. */.    
16e67 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65    if( pCur->aOve
16e68 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20  rflow ){.       
16e69 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61   assert(!pCur->a
16e6a 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c  Overflow[iIdx] |
16e6b 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  | pCur->aOverflo
16e6c 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67  w[iIdx]==nextPag
16e6d 65 29 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72  e);.        pCur
16e6e 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
16e6f 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20  ] = nextPage;.  
16e70 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
16e71 20 20 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d      if( offset>=
16e72 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20  ovflSize ){.    
16e73 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20      /* The only 
16e74 72 65 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74  reason to read t
16e75 68 69 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f  his page is to o
16e76 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20  btain the page. 
16e77 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72         ** number
16e78 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61   for the next pa
16e79 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c  ge in the overfl
16e7a 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61  ow chain. The pa
16e7b 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61  ge.        ** da
16e7c 74 61 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  ta is not requir
16e7d 65 64 2e 20 53 6f 20 66 69 72 73 74 20 74 72 79  ed. So first try
16e7e 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f   to lookup the o
16e7f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 20  verflow.        
16e80 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63  ** page-list cac
16e81 68 65 2c 20 69 66 20 61 6e 79 2c 20 74 68 65 6e  he, if any, then
16e82 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68   fall back to th
16e83 65 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67  e getOverflowPag
16e84 65 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 66  e().        ** f
16e85 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20  unction..       
16e86 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
16e87 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
16e88 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75  .        if( pCu
16e89 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20  r->aOverflow && 
16e8a 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b  pCur->aOverflow[
16e8b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20  iIdx+1] ){.     
16e8c 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20       nextPage = 
16e8d 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b  pCur->aOverflow[
16e8e 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20  iIdx+1];.       
16e8f 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a   } else .#endif.
16e90 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 67            rc = g
16e91 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70  etOverflowPage(p
16e92 42 74 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c  Bt, nextPage, 0,
16e93 20 26 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20   &nextPage);.   
16e94 20 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f       offset -= o
16e95 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d  vflSize;.      }
16e96 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
16e97 20 4e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68   Need to read th
16e98 69 73 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79  is page properly
16e99 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f  . It contains so
16e9a 6d 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20  me of the.      
16e9b 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61    ** range of da
16e9c 74 61 20 74 68 61 74 20 69 73 20 62 65 69 6e 67  ta that is being
16e9d 20 72 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f   read (eOp==0) o
16e9e 72 20 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d  r written (eOp!=
16e9f 30 29 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  0)..        */. 
16ea0 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70         DbPage *p
16ea1 44 62 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20  DbPage;.        
16ea2 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20  int a = amt;.   
16ea3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
16ea4 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70  3PagerGet(pBt->p
16ea5 50 61 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c  Pager, nextPage,
16ea6 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20   &pDbPage);.    
16ea7 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
16ea8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
16ea9 20 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71     aPayload = sq
16eaa 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
16eab 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  a(pDbPage);.    
16eac 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d        nextPage =
16ead 20 67 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f   get4byte(aPaylo
16eae 61 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ad);.          i
16eaf 66 28 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20  f( a + offset > 
16eb0 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20  ovflSize ){.    
16eb1 20 20 20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c          a = ovfl
16eb2 53 69 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20  Size - offset;. 
16eb3 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
16eb4 20 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61       rc = copyPa
16eb5 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b  yload(&aPayload[
16eb6 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c  offset+4], pBuf,
16eb7 20 61 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65   a, eOp, pDbPage
16eb8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
16eb9 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
16eba 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
16ebb 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20     offset = 0;. 
16ebc 20 20 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20           amt -= 
16ebd 61 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 75  a;.          pBu
16ebe 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 20  f += a;.        
16ebf 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
16ec0 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53    }..  if( rc==S
16ec1 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e  QLITE_OK && amt>
16ec2 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
16ec3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
16ec4 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  KPT;.  }.  retur
16ec5 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
16ec6 65 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20  ead part of the 
16ec7 6b 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  key associated w
16ec8 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e  ith cursor pCur.
16ec9 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d    Exactly.** "am
16eca 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65  t" bytes will be
16ecb 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f   transfered into
16ecc 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72   pBuf[].  The tr
16ecd 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73  ansfer.** begins
16ece 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a   at "offset"..**
16ecf 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  .** The caller m
16ed0 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20  ust ensure that 
16ed1 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67  pCur is pointing
16ed2 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a   to a valid row.
16ed3 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ** in the table.
16ed4 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
16ed5 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
16ed6 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63  ss or an error c
16ed7 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ode if anything 
16ed8 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20  goes.** wrong.  
16ed9 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  An error is retu
16eda 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b  rned if "offset+
16edb 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74  amt" is larger t
16edc 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c  han.** the avail
16edd 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f  able payload..*/
16ede 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16edf 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16ee0 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43  Key(BtCursor *pC
16ee1 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20  ur, u32 offset, 
16ee2 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70  u32 amt, void *p
16ee3 42 75 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Buf){.  assert( 
16ee4 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
16ee5 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
16ee6 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
16ee7 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
16ee8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16ee9 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43  ->iPage>=0 && pC
16eea 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
16eeb 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73  >iPage] );.  ass
16eec 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
16eed 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43  [pCur->iPage]<pC
16eee 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
16eef 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29  >iPage]->nCell )
16ef0 3b 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73  ;.  return acces
16ef1 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f  sPayload(pCur, o
16ef2 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73  ffset, amt, (uns
16ef3 69 67 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66  igned char*)pBuf
16ef4 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  , 0);.}../*.** R
16ef5 65 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20  ead part of the 
16ef6 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  data associated 
16ef7 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72  with cursor pCur
16ef8 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61  .  Exactly.** "a
16ef9 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62  mt" bytes will b
16efa 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74  e transfered int
16efb 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74  o pBuf[].  The t
16efc 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e  ransfer.** begin
16efd 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a  s at "offset"..*
16efe 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
16eff 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
16f00 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64   or an error cod
16f01 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f  e if anything go
16f02 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e  es.** wrong.  An
16f03 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
16f04 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d  ed if "offset+am
16f05 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61  t" is larger tha
16f06 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62  n.** the availab
16f07 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53  le payload..*/.S
16f08 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16f09 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
16f0a 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ta(BtCursor *pCu
16f0b 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  r, u32 offset, u
16f0c 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42  32 amt, void *pB
16f0d 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  uf){.  int rc;..
16f0e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16f0f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69  MIT_INCRBLOB.  i
16f10 66 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  f ( pCur->eState
16f11 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
16f12 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16f13 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d  QLITE_ABORT;.  }
16f14 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
16f15 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
16f16 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72  tex(pCur) );.  r
16f17 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f  c = restoreCurso
16f18 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b  rPosition(pCur);
16f19 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
16f1a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  E_OK ){.    asse
16f1b 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
16f1c 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
16f1d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ;.    assert( pC
16f1e 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20  ur->iPage>=0 && 
16f1f 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
16f20 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20  r->iPage] );.   
16f21 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
16f22 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
16f23 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  ]<pCur->apPage[p
16f24 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65  Cur->iPage]->nCe
16f25 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61  ll );.    rc = a
16f26 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75  ccessPayload(pCu
16f27 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20  r, offset, amt, 
16f28 70 42 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  pBuf, 0);.  }.  
16f29 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
16f2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
16f2b 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20  nter to payload 
16f2c 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
16f2d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20   the entry that 
16f2e 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72  the .** pCur cur
16f2f 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  sor is pointing 
16f30 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72  to.  The pointer
16f31 20 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e   is to the begin
16f32 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b  ning of.** the k
16f33 65 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30  ey if skipKey==0
16f34 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74   and it points t
16f35 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
16f36 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b  of data if.** sk
16f37 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e  ipKey==1.  The n
16f38 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
16f39 66 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f  f available key/
16f3a 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a  data is written.
16f3b 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20  ** into *pAmt.  
16f3c 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65  If *pAmt==0, the
16f3d 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
16f3e 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65  rned will not be
16f3f 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e  .** a valid poin
16f40 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ter..**.** This 
16f41 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70  routine is an op
16f42 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20  timization.  It 
16f43 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68  is common for th
16f44 65 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20  e entire key.** 
16f45 61 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20  and data to fit 
16f46 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67  on the local pag
16f47 65 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20  e and for there 
16f48 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f  to be no overflo
16f49 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65  w.** pages.  Whe
16f4a 6e 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68  n that is so, th
16f4b 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62  is routine can b
16f4c 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  e used to access
16f4d 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20   the.** key and 
16f4e 64 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b  data without mak
16f4f 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20  ing a copy.  If 
16f50 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64  the key and/or d
16f51 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e  ata spills.** on
16f52 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
16f53 73 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61  s, then accessPa
16f54 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20  yload() must be 
16f55 75 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62  used to reassemb
16f56 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61  le.** the key/da
16f57 74 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69  ta and copy it i
16f58 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74  nto a preallocat
16f59 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  ed buffer..**.**
16f5a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74   The pointer ret
16f5b 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f  urned by this ro
16f5c 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65  utine looks dire
16f5d 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61  ctly into the ca
16f5e 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20  ched.** page of 
16f5f 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
16f60 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68  he data might ch
16f61 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65  ange or move the
16f62 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e   next time.** an
16f63 79 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20  y btree routine 
16f64 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74  is called..*/.st
16f65 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
16f66 6e 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50  ned char *fetchP
16f67 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73  ayload(.  BtCurs
16f68 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f  or *pCur,      /
16f69 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  * Cursor pointin
16f6a 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65  g to entry to re
16f6b 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74  ad from */.  int
16f6c 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20   *pAmt,         
16f6d 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e    /* Write the n
16f6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62  umber of availab
16f6f 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f  le bytes here */
16f70 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20  .  int skipKey  
16f71 20 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20          /* read 
16f72 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74  beginning at dat
16f73 61 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75  a if this is tru
16f74 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e  e */.){.  unsign
16f75 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61  ed char *aPayloa
16f76 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  d;.  MemPage *pP
16f77 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b  age;.  u32 nKey;
16f78 0a 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a  .  u32 nLocal;..
16f79 20 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d    assert( pCur!=
16f7a 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65  0 && pCur->iPage
16f7b 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50  >=0 && pCur->apP
16f7c 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
16f7d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
16f7e 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
16f7f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73  R_VALID );.  ass
16f80 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
16f81 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
16f82 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
16f83 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
16f84 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  e];.  assert( pC
16f85 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
16f86 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43  iPage]<pPage->nC
16f87 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 4e 45 56  ell );.  if( NEV
16f88 45 52 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53  ER(pCur->info.nS
16f89 69 7a 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62  ize==0) ){.    b
16f8a 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43  treeParseCell(pC
16f8b 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
16f8c 3e 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61  >iPage], pCur->a
16f8d 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
16f8e 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
16f8f 20 20 20 20 20 20 26 70 43 75 72 2d 3e 69 6e 66        &pCur->inf
16f90 6f 29 3b 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f  o);.  }.  aPaylo
16f91 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  ad = pCur->info.
16f92 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61  pCell;.  aPayloa
16f93 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  d += pCur->info.
16f94 6e 48 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70  nHeader;.  if( p
16f95 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a  Page->intKey ){.
16f96 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20      nKey = 0;.  
16f97 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20  }else{.    nKey 
16f98 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66  = (int)pCur->inf
16f99 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66  o.nKey;.  }.  if
16f9a 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20  ( skipKey ){.   
16f9b 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65   aPayload += nKe
16f9c 79 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20  y;.    nLocal = 
16f9d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
16f9e 6c 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73  l - nKey;.  }els
16f9f 65 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20  e{.    nLocal = 
16fa0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
16fa1 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  l;.    assert( n
16fa2 4c 6f 63 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20  Local<=nKey );. 
16fa3 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f   }.  *pAmt = nLo
16fa4 63 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50  cal;.  return aP
16fa5 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ayload;.}.../*.*
16fa6 2a 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20  * For the entry 
16fa7 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72  that cursor pCur
16fa8 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65   is point to, re
16fa9 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20  turn as.** many 
16faa 62 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79  bytes of the key
16fab 20 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20   or data as are 
16fac 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65  available on the
16fad 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65   local.** b-tree
16fae 20 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68   page.  Write th
16faf 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69  e number of avai
16fb0 6c 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f  lable bytes into
16fb1 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68   *pAmt..**.** Th
16fb2 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e  e pointer return
16fb3 65 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e  ed is ephemeral.
16fb4 20 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d    The key/data m
16fb5 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65  ay move.** or be
16fb6 20 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68   destroyed on th
16fb7 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61  e next call to a
16fb8 6e 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65  ny Btree routine
16fb9 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63  ,.** including c
16fba 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20  alls from other 
16fbb 74 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20  threads against 
16fbc 74 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a  the same cache..
16fbd 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65  ** Hence, a mute
16fbe 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65  x on the BtShare
16fbf 64 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64  d should be held
16fc0 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
16fc1 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  g.** this routin
16fc2 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  e..**.** These r
16fc3 6f 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20  outines is used 
16fc4 74 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63  to get quick acc
16fc5 65 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64  ess to key and d
16fc6 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f  ata.** in the co
16fc7 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20  mmon case where 
16fc8 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  no overflow page
16fc9 73 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53  s are used..*/.S
16fca 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
16fcb 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
16fcc 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42  3BtreeKeyFetch(B
16fcd 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69  tCursor *pCur, i
16fce 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e  nt *pAmt){.  con
16fcf 73 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a  st void *p = 0;.
16fd0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16fd1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75  3_mutex_held(pCu
16fd2 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d  r->pBtree->db->m
16fd3 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
16fd4 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
16fd5 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69  tex(pCur) );.  i
16fd6 66 28 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e  f( ALWAYS(pCur->
16fd7 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
16fd8 41 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d  ALID) ){.    p =
16fd9 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65   (const void*)fe
16fda 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c  tchPayload(pCur,
16fdb 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20   pAmt, 0);.  }. 
16fdc 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c   return p;.}.SQL
16fdd 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
16fde 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42  t void *sqlite3B
16fdf 74 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74  treeDataFetch(Bt
16fe0 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
16fe1 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73  t *pAmt){.  cons
16fe2 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20  t void *p = 0;. 
16fe3 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16fe4 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
16fe5 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
16fe6 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
16fe7 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16fe8 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66  ex(pCur) );.  if
16fe9 28 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65  ( ALWAYS(pCur->e
16fea 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
16feb 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20  LID) ){.    p = 
16fec 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74  (const void*)fet
16fed 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  chPayload(pCur, 
16fee 70 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20  pAmt, 1);.  }.  
16fef 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a  return p;.}.../*
16ff0 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72  .** Move the cur
16ff1 73 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65  sor down to a ne
16ff2 77 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54  w child page.  T
16ff3 68 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d  he newPgno argum
16ff4 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61  ent is the.** pa
16ff5 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
16ff6 20 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d   child page to m
16ff7 6f 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68  ove to..**.** Th
16ff8 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
16ff9 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  rns SQLITE_CORRU
16ffa 50 54 20 69 66 20 74 68 65 20 70 61 67 65 2d 68  PT if the page-h
16ffb 65 61 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c  eader flags fiel
16ffc 64 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20  d of.** the new 
16ffd 63 68 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20  child page does 
16ffe 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c  not match the fl
16fff 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ags field of the
17000 20 70 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a   parent (i.e..**
17001 20 69 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61   if an intkey pa
17002 67 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65  ge appears to be
17003 20 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61   the parent of a
17004 20 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65   non-intkey page
17005 2c 20 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72  , or.** vice-ver
17006 73 61 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  sa)..*/.static i
17007 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42  nt moveToChild(B
17008 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75  tCursor *pCur, u
17009 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69  32 newPgno){.  i
1700a 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d  nt rc;.  int i =
1700b 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20   pCur->iPage;.  
1700c 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67  MemPage *pNewPag
1700d 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e;.  BtShared *p
1700e 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a  Bt = pCur->pBt;.
1700f 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
17010 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
17011 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
17012 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
17013 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61  SOR_VALID );.  a
17014 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61  ssert( pCur->iPa
17015 67 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f  ge<BTCURSOR_MAX_
17016 44 45 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70  DEPTH );.  if( p
17017 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43  Cur->iPage>=(BTC
17018 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d  URSOR_MAX_DEPTH-
17019 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  1) ){.    return
1701a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1701b 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  BKPT;.  }.  rc =
1701c 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
1701d 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70  pBt, newPgno, &p
1701e 4e 65 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20  NewPage);.  if( 
1701f 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
17020 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69    pCur->apPage[i
17021 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a  +1] = pNewPage;.
17022 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b    pCur->aiIdx[i+
17023 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e  1] = 0;.  pCur->
17024 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72  iPage++;..  pCur
17025 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
17026 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e  ;.  pCur->validN
17027 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  Key = 0;.  if( p
17028 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31  NewPage->nCell<1
17029 20 7c 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e   || pNewPage->in
1702a 74 4b 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61  tKey!=pCur->apPa
1702b 67 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b  ge[i]->intKey ){
1702c 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1702d 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1702e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
1702f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e  LITE_OK;.}..#ifn
17030 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a  def NDEBUG./*.**
17031 20 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 73   Page pParent is
17032 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f   an internal (no
17033 6e 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 67  n-leaf) tree pag
17034 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  e. This function
17035 20 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61   .** asserts tha
17036 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43  t page number iC
17037 68 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74  hild is the left
17038 2d 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 49  -child if the iI
17039 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e  dx'th.** cell in
1703a 20 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f   page pParent. O
1703b 72 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 71  r, if iIdx is eq
1703c 75 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c  ual to the total
1703d 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65   number of.** ce
1703e 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20  lls in pParent, 
1703f 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72  that page number
17040 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 72   iChild is the r
17041 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a  ight-child of.**
17042 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74   the page..*/.st
17043 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74  atic void assert
17044 50 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50  ParentIndex(MemP
17045 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e  age *pParent, in
17046 74 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68  t iIdx, Pgno iCh
17047 69 6c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ild){.  assert( 
17048 69 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e  iIdx<=pParent->n
17049 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49  Cell );.  if( iI
1704a 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65  dx==pParent->nCe
1704b 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ll ){.    assert
1704c 28 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72  ( get4byte(&pPar
1704d 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
1704e 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
1704f 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d  )==iChild );.  }
17050 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
17051 28 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43  ( get4byte(findC
17052 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64  ell(pParent, iId
17053 78 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20  x))==iChild );. 
17054 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65   }.}.#else.#  de
17055 66 69 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e  fine assertParen
17056 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23  tIndex(x,y,z) .#
17057 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76  endif../*.** Mov
17058 65 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20  e the cursor up 
17059 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  to the parent pa
1705a 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e  ge..**.** pCur->
1705b 69 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68  idx is set to th
1705c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61  e cell index tha
1705d 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70  t contains the p
1705e 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ointer.** to the
1705f 20 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d   page we are com
17060 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65  ing from.  If we
17061 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d   are coming from
17062 20 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f   the.** right-mo
17063 73 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68  st child page th
17064 65 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20  en pCur->idx is 
17065 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20  set to one more 
17066 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67  than.** the larg
17067 65 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a  est cell index..
17068 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
17069 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75  oveToParent(BtCu
1706a 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61  rsor *pCur){.  a
1706b 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
1706c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
1706d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
1706e 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1706f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
17070 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30  t( pCur->iPage>0
17071 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
17072 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
17073 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73  >iPage] );.  ass
17074 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a  ertParentIndex(.
17075 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65      pCur->apPage
17076 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c  [pCur->iPage-1],
17077 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64   .    pCur->aiId
17078 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d  x[pCur->iPage-1]
17079 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50  , .    pCur->apP
1707a 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
1707b 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65  ->pgno.  );.  re
1707c 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e  leasePage(pCur->
1707d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1707e 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50  ge]);.  pCur->iP
1707f 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69  age--;.  pCur->i
17080 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20  nfo.nSize = 0;. 
17081 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
17082 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d   = 0;.}../*.** M
17083 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74  ove the cursor t
17084 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72  o point to the r
17085 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73 20  oot page of its 
17086 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65  b-tree structure
17087 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74  ..**.** If the t
17088 61 62 6c 65 20 68 61 73 20 61 20 76 69 72 74 75  able has a virtu
17089 61 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68  al root page, th
1708a 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  en the cursor is
1708b 20 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a   moved to point.
1708c 2a 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 61  ** to the virtua
1708d 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74  l root page inst
1708e 65 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 61  ead of the actua
1708f 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74  l root page. A t
17090 61 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 69  able has a.** vi
17091 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20  rtual root page 
17092 77 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20  when the actual 
17093 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69  root page contai
17094 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20  ns no cells and 
17095 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69  a .** single chi
17096 6c 64 20 70 61 67 65 2e 20 54 68 69 73 20 63 61  ld page. This ca
17097 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 69  n only happen wi
17098 74 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f  th the table roo
17099 74 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a 2a  ted at page 1..*
1709a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 72  *.** If the b-tr
1709b 65 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ee structure is 
1709c 65 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73 6f  empty, the curso
1709d 72 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74  r state is set t
1709e 6f 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56  o .** CURSOR_INV
1709f 41 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65 2c  ALID. Otherwise,
170a0 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 73   the cursor is s
170a1 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  et to point to t
170a2 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c  he first.** cell
170a3 20 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20   located on the 
170a4 72 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61 6c  root (or virtual
170a5 20 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20   root) page and 
170a6 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65  the cursor state
170a7 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43 55  .** is set to CU
170a8 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a  RSOR_VALID..**.*
170a9 2a 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69  * If this functi
170aa 6f 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63 65  on returns succe
170ab 73 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20  ssfully, it may 
170ac 62 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  be assumed that 
170ad 74 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64  the.** page-head
170ae 65 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61 74  er flags indicat
170af 65 20 74 68 61 74 20 74 68 65 20 5b 76 69 72 74  e that the [virt
170b0 75 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69  ual] root-page i
170b1 73 20 74 68 65 20 65 78 70 65 63 74 65 64 20 0a  s the expected .
170b2 2a 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65  ** kind of b-tre
170b3 65 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66 20  e page (i.e. if 
170b4 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65  when opening the
170b5 20 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c 6c   cursor the call
170b6 65 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70  er did not.** sp
170b7 65 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20  ecify a KeyInfo 
170b8 73 74 72 75 63 74 75 72 65 20 74 68 65 20 66 6c  structure the fl
170b9 61 67 73 20 62 79 74 65 20 69 73 20 73 65 74 20  ags byte is set 
170ba 74 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44 2c  to 0x05 or 0x0D,
170bb 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 61  .** indicating a
170bc 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f   table b-tree, o
170bd 72 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20  r if the caller 
170be 64 69 64 20 73 70 65 63 69 66 79 20 61 20 4b 65  did specify a Ke
170bf 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74  yInfo .** struct
170c0 75 72 65 20 74 68 65 20 66 6c 61 67 73 20 62 79  ure the flags by
170c1 74 65 20 69 73 20 73 65 74 20 74 6f 20 30 78 30  te is set to 0x0
170c2 32 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 63  2 or 0x0A, indic
170c3 61 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a  ating an index.*
170c4 2a 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74  * b-tree)..*/.st
170c5 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52  atic int moveToR
170c6 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  oot(BtCursor *pC
170c7 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ur){.  MemPage *
170c8 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20  pRoot;.  int rc 
170c9 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42  = SQLITE_OK;.  B
170ca 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e  tree *p = pCur->
170cb 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72  pBtree;.  BtShar
170cc 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
170cd 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
170ce 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
170cf 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
170d0 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20   CURSOR_INVALID 
170d1 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  < CURSOR_REQUIRE
170d2 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  SEEK );.  assert
170d3 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20  ( CURSOR_VALID  
170d4 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52   < CURSOR_REQUIR
170d5 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72  ESEEK );.  asser
170d6 74 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  t( CURSOR_FAULT 
170d7 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49    > CURSOR_REQUI
170d8 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20  RESEEK );.  if( 
170d9 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55  pCur->eState>=CU
170da 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
170db 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72   ){.    if( pCur
170dc 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
170dd 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20  _FAULT ){.      
170de 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b  assert( pCur->sk
170df 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f  ipNext!=SQLITE_O
170e0 4b 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  K );.      retur
170e1 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74  n pCur->skipNext
170e2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
170e3 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
170e4 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a  sor(pCur);.  }..
170e5 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67    if( pCur->iPag
170e6 65 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  e>=0 ){.    int 
170e7 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  i;.    for(i=1; 
170e8 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20  i<=pCur->iPage; 
170e9 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  i++){.      rele
170ea 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70  asePage(pCur->ap
170eb 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  Page[i]);.    }.
170ec 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20      pCur->iPage 
170ed 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
170ee 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69    rc = getAndIni
170ef 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d  tPage(pBt, pCur-
170f0 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72  >pgnoRoot, &pCur
170f1 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20  ->apPage[0]);.  
170f2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
170f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75  _OK ){.      pCu
170f4 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
170f5 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20  OR_INVALID;.    
170f6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
170f7 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61   }.    pCur->iPa
170f8 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  ge = 0;..    /* 
170f9 49 66 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  If pCur->pKeyInf
170fa 6f 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  o is not NULL, t
170fb 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74  hen the caller t
170fc 68 61 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20  hat opened this 
170fd 63 75 72 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78  cursor.    ** ex
170fe 70 65 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69  pected to open i
170ff 74 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d  t on an index b-
17100 74 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  tree. Otherwise,
17101 20 69 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a   if pKeyInfo is.
17102 20 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65      ** NULL, the
17103 20 63 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20   caller expects 
17104 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20  a table b-tree. 
17105 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74  If this is not t
17106 68 65 20 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20  he case,.    ** 
17107 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45  return an SQLITE
17108 5f 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20  _CORRUPT error. 
17109 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
1710a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d  pCur->apPage[0]-
1710b 3e 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43  >intKey==1 || pC
1710c 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
1710d 6e 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20  ntKey==0 );.    
1710e 69 66 28 20 28 70 43 75 72 2d 3e 70 4b 65 79 49  if( (pCur->pKeyI
1710f 6e 66 6f 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61  nfo==0)!=pCur->a
17110 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79  pPage[0]->intKey
17111 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
17112 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
17113 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  BKPT;.    }.  }.
17114 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61  .  /* Assert tha
17115 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  t the root page 
17116 69 73 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63  is of the correc
17117 74 20 74 79 70 65 2e 20 54 68 69 73 20 6d 75 73  t type. This mus
17118 74 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 63 61  t be the.  ** ca
17119 73 65 20 61 73 20 74 68 65 20 63 61 6c 6c 20 74  se as the call t
1711a 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  o this function 
1711b 74 68 61 74 20 6c 6f 61 64 65 64 20 74 68 65 20  that loaded the 
1711c 72 6f 6f 74 2d 70 61 67 65 20 28 65 69 74 68 65  root-page (eithe
1711d 72 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c  r.  ** this call
1711e 20 6f 72 20 61 20 70 72 65 76 69 6f 75 73 20 69   or a previous i
1711f 6e 76 6f 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64  nvocation) would
17120 20 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63   have detected c
17121 6f 72 72 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20  orruption .  ** 
17122 69 66 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f  if the assumptio
17123 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c  n were not true,
17124 20 61 6e 64 20 69 74 20 69 73 20 6e 6f 74 20 70   and it is not p
17125 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20  ossible for the 
17126 66 6c 61 67 73 20 0a 20 20 2a 2a 20 62 79 74 65  flags .  ** byte
17127 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 6d 6f   to have been mo
17128 64 69 66 69 65 64 20 77 68 69 6c 65 20 74 68 69  dified while thi
17129 73 20 63 75 72 73 6f 72 20 69 73 20 68 6f 6c 64  s cursor is hold
1712a 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 0a  ing a reference.
1712b 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65    ** to the page
1712c 2e 20 20 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20  .  */.  pRoot = 
1712d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b  pCur->apPage[0];
1712e 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74  .  assert( pRoot
1712f 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67  ->pgno==pCur->pg
17130 6e 6f 52 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65  noRoot );.  asse
17131 72 74 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69  rt( pRoot->isIni
17132 74 20 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79  t && (pCur->pKey
17133 49 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d  Info==0)==pRoot-
17134 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43  >intKey );..  pC
17135 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30  ur->aiIdx[0] = 0
17136 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  ;.  pCur->info.n
17137 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72  Size = 0;.  pCur
17138 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20  ->atLast = 0;.  
17139 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1713a 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f  = 0;..  if( pRoo
1713b 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21  t->nCell==0 && !
1713c 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20  pRoot->leaf ){. 
1713d 20 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b     Pgno subpage;
1713e 0a 20 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e  .    if( pRoot->
1713f 70 67 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e  pgno!=1 ) return
17140 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
17141 42 4b 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67  BKPT;.    subpag
17142 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52  e = get4byte(&pR
17143 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74  oot->aData[pRoot
17144 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
17145 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74  .    pCur->eStat
17146 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44  e = CURSOR_VALID
17147 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54  ;.    rc = moveT
17148 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62  oChild(pCur, sub
17149 70 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  page);.  }else{.
1714a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
1714b 20 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c   = ((pRoot->nCel
1714c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49  l>0)?CURSOR_VALI
1714d 44 3a 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  D:CURSOR_INVALID
1714e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1714f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76  rc;.}../*.** Mov
17150 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77  e the cursor dow
17151 6e 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f  n to the left-mo
17152 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65  st leaf entry be
17153 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74  neath the.** ent
17154 72 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69  ry to which it i
17155 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
17156 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ting..**.** The 
17157 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69  left-most leaf i
17158 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74  s the one with t
17159 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20  he smallest key 
1715a 2d 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69  - the first.** i
1715b 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
1715c 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
1715d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28   moveToLeftmost(
1715e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
1715f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20  .  Pgno pgno;.  
17160 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
17161 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  OK;.  MemPage *p
17162 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
17163 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
17164 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
17165 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
17166 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
17167 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d  );.  while( rc==
17168 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70  SQLITE_OK && !(p
17169 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
1716a 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
1716b 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61  )->leaf ){.    a
1716c 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49  ssert( pCur->aiI
1716d 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c  dx[pCur->iPage]<
1716e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pPage->nCell );.
1716f 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62      pgno = get4b
17170 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61  yte(findCell(pPa
17171 67 65 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  ge, pCur->aiIdx[
17172 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a  pCur->iPage]));.
17173 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
17174 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29  hild(pCur, pgno)
17175 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
17176 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  c;.}../*.** Move
17177 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e   the cursor down
17178 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f   to the right-mo
17179 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65  st leaf entry be
1717a 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67  neath the.** pag
1717b 65 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73  e to which it is
1717c 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1717d 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65  ing.  Notice the
1717e 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62   difference.** b
1717f 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66  etween moveToLef
17180 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65  tmost() and move
17181 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20  ToRightmost().  
17182 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29  moveToLeftmost()
17183 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65  .** finds the le
17184 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65  ft-most entry be
17185 6e 65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79  neath the *entry
17186 2a 20 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f  * whereas moveTo
17187 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66  Rightmost().** f
17188 69 6e 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d  inds the right-m
17189 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74  ost entry beneat
1718a 68 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a  h the *page*..**
1718b 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f  .** The right-mo
1718c 73 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 20  st entry is the 
1718d 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 72  one with the lar
1718e 67 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c  gest key - the l
1718f 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73  ast.** key in as
17190 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a  cending order..*
17191 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76  /.static int mov
17192 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43  eToRightmost(BtC
17193 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
17194 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74  Pgno pgno;.  int
17195 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
17196 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
17197 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  e = 0;..  assert
17198 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
17199 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
1719a 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
1719b 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
1719c 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d   );.  while( rc=
1719d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28  =SQLITE_OK && !(
1719e 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
1719f 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
171a0 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ])->leaf ){.    
171a1 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
171a2 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
171a3 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
171a4 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69  ]);.    pCur->ai
171a5 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
171a6 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   = pPage->nCell;
171a7 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  .    rc = moveTo
171a8 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f  Child(pCur, pgno
171a9 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
171aa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
171ab 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43    pCur->aiIdx[pC
171ac 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61  ur->iPage] = pPa
171ad 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20  ge->nCell-1;.   
171ae 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
171af 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d  e = 0;.    pCur-
171b0 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
171b1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
171b2 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20  .}../* Move the 
171b3 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69  cursor to the fi
171b4 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  rst entry in the
171b5 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20   table.  Return 
171b6 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20  SQLITE_OK.** on 
171b7 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70  success.  Set *p
171b8 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20  Res to 0 if the 
171b9 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20  cursor actually 
171ba 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68  points to someth
171bb 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70  ing.** or set *p
171bc 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20  Res to 1 if the 
171bd 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a  table is empty..
171be 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
171bf 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
171c0 65 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72  eeFirst(BtCursor
171c1 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65   *pCur, int *pRe
171c2 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  s){.  int rc;.. 
171c3 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
171c4 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
171c5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
171c6 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
171c7 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
171c8 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63  ->mutex) );.  rc
171c9 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43   = moveToRoot(pC
171ca 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ur);.  if( rc==S
171cb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
171cc 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
171cd 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
171ce 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
171cf 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  ( pCur->apPage[p
171d0 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65  Cur->iPage]->nCe
171d1 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a  ll==0 );.      *
171d2 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20  pRes = 1;.      
171d3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
171d4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
171d5 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
171d6 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
171d7 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20  e]->nCell>0 );. 
171d8 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a       *pRes = 0;.
171d9 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54        rc = moveT
171da 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b  oLeftmost(pCur);
171db 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
171dc 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f  urn rc;.}../* Mo
171dd 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
171de 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
171df 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52  in the table.  R
171e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a  eturn SQLITE_OK.
171e1 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  ** on success.  
171e2 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69  Set *pRes to 0 i
171e3 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74  f the cursor act
171e4 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20  ually points to 
171e5 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20  something.** or 
171e6 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69  set *pRes to 1 i
171e7 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  f the table is e
171e8 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mpty..*/.SQLITE_
171e9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
171ea 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43  te3BtreeLast(BtC
171eb 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74  ursor *pCur, int
171ec 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72   *pRes){.  int r
171ed 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63  c;. .  assert( c
171ee 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
171ef 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
171f0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
171f1 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72  _held(pCur->pBtr
171f2 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ee->db->mutex) )
171f3 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  ;..  /* If the c
171f4 75 72 73 6f 72 20 61 6c 72 65 61 64 79 20 70 6f  ursor already po
171f5 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74  ints to the last
171f6 20 65 6e 74 72 79 2c 20 74 68 69 73 20 69 73 20   entry, this is 
171f7 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66  a no-op. */.  if
171f8 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d  ( CURSOR_VALID==
171f9 70 43 75 72 2d 3e 65 53 74 61 74 65 20 26 26 20  pCur->eState && 
171fa 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a  pCur->atLast ){.
171fb 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
171fc 42 55 47 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  BUG.    /* This 
171fd 62 6c 6f 63 6b 20 73 65 72 76 65 73 20 74 6f 20  block serves to 
171fe 61 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68  assert() that th
171ff 65 20 63 75 72 73 6f 72 20 72 65 61 6c 6c 79 20  e cursor really 
17200 64 6f 65 73 20 70 6f 69 6e 74 20 0a 20 20 20 20  does point .    
17201 2a 2a 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65  ** to the last e
17202 6e 74 72 79 20 69 6e 20 74 68 65 20 62 2d 74 72  ntry in the b-tr
17203 65 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ee. */.    int i
17204 69 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  i;.    for(ii=0;
17205 20 69 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b   ii<pCur->iPage;
17206 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73   ii++){.      as
17207 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
17208 78 5b 69 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50  x[ii]==pCur->apP
17209 61 67 65 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29  age[ii]->nCell )
1720a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
1720b 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
1720c 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43  pCur->iPage]==pC
1720d 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1720e 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31  >iPage]->nCell-1
1720f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
17210 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
17211 72 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20  r->iPage]->leaf 
17212 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65  );.#endif.    re
17213 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
17214 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65    }..  rc = move
17215 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20  ToRoot(pCur);.  
17216 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17217 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52  K ){.    if( CUR
17218 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75  SOR_INVALID==pCu
17219 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20  r->eState ){.   
1721a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
1721b 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1721c 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  age]->nCell==0 )
1721d 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  ;.      *pRes = 
1721e 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
1721f 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
17220 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
17221 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20  _VALID );.      
17222 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20  *pRes = 0;.     
17223 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68   rc = moveToRigh
17224 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20  tmost(pCur);.   
17225 20 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20     pCur->atLast 
17226 3d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  = rc==SQLITE_OK 
17227 3f 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ?1:0;.    }.  }.
17228 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
17229 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  /* Move the curs
1722a 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f  or so that it po
1722b 69 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79  ints to an entry
1722c 20 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a   near the key .*
1722d 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70  * specified by p
1722e 49 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79  IdxKey or intKey
1722f 2e 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63  .   Return a suc
17230 63 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  cess code..**.**
17231 20 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c   For INTKEY tabl
17232 65 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70  es, the intKey p
17233 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64  arameter is used
17234 2e 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d  .  pIdxKey .** m
17235 75 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f  ust be NULL.  Fo
17236 72 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20  r index tables, 
17237 70 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20  pIdxKey is used 
17238 61 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73  and intKey.** is
17239 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   ignored..**.** 
1723a 49 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63  If an exact matc
1723b 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20  h is not found, 
1723c 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20  then the cursor 
1723d 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66  is always.** lef
1723e 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20  t pointing at a 
1723f 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20  leaf page which 
17240 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65  would hold the e
17241 6e 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65  ntry if it.** we
17242 72 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65  re present.  The
17243 20 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f   cursor might po
17244 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20  int to an entry 
17245 74 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65  that comes.** be
17246 66 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68  fore or after th
17247 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  e key..**.** An 
17248 69 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74  integer is writt
17249 65 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68  en into *pRes wh
1724a 69 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c  ich is the resul
1724b 74 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e  t of.** comparin
1724c 67 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74  g the key with t
1724d 68 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63  he entry to whic
1724e 68 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  h the cursor is 
1724f 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54  .** pointing.  T
17250 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68  he meaning of th
17251 65 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65  e integer writte
17252 6e 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20  n into.** *pRes 
17253 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
17254 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30  *.**     *pRes<0
17255 20 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72        The cursor
17256 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
17257 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68  g at an entry th
17258 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  at.**           
17259 20 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65         is smalle
1725a 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49  r than intKey/pI
1725b 64 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20  dxKey or if the 
1725c 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a  table is empty.*
1725d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
1725e 20 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f     and the curso
1725f 72 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c  r is therefore l
17260 65 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74  eft point to not
17261 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  hing..**.**     
17262 2a 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65  *pRes==0     The
17263 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20   cursor is left 
17264 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65  pointing at an e
17265 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20  ntry that.**    
17266 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78                ex
17267 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e  actly matches in
17268 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a  tKey/pIdxKey..**
17269 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20  .**     *pRes>0 
1726a 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20       The cursor 
1726b 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
1726c 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61   at an entry tha
1726d 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
1726e 20 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20        is larger 
1726f 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78  than intKey/pIdx
17270 4b 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  Key..**.*/.SQLIT
17271 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
17272 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
17273 55 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75  Unpacked(.  BtCu
17274 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
17275 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
17276 6f 72 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a  or to be moved *
17277 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  /.  UnpackedReco
17278 72 64 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20  rd *pIdxKey, /* 
17279 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b  Unpacked index k
1727a 65 79 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b  ey */.  i64 intK
1727b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ey,             
1727c 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65   /* The table ke
1727d 79 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52  y */.  int biasR
1727e 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
1727f 2f 2a 20 49 66 20 74 72 75 65 2c 20 62 69 61 73  /* If true, bias
17280 20 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 74   the search to t
17281 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20  he high end */. 
17282 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20   int *pRes      
17283 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
17284 74 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74  te search result
17285 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
17286 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
17287 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
17288 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
17289 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1728a 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70  tex_held(pCur->p
1728b 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78  Btree->db->mutex
1728c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
1728d 52 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Res );.  assert(
1728e 20 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28   (pIdxKey==0)==(
1728f 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d  pCur->pKeyInfo==
17290 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  0) );..  /* If t
17291 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72  he cursor is alr
17292 65 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20  eady positioned 
17293 61 74 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20  at the point we 
17294 61 72 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20  are trying.  ** 
17295 74 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e  to move to, then
17296 20 6a 75 73 74 20 72 65 74 75 72 6e 20 77 69 74   just return wit
17297 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77  hout doing any w
17298 6f 72 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75  ork */.  if( pCu
17299 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
1729a 52 5f 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d  R_VALID && pCur-
1729b 3e 76 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26  >validNKey .   &
1729c 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  & pCur->apPage[0
1729d 5d 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a  ]->intKey .  ){.
1729e 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e      if( pCur->in
1729f 66 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20  fo.nKey==intKey 
172a0 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  ){.      *pRes =
172a1 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   0;.      return
172a2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
172a3 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  }.    if( pCur->
172a4 61 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e  atLast && pCur->
172a5 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79  info.nKey<intKey
172a6 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20   ){.      *pRes 
172a7 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75  = -1;.      retu
172a8 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
172a9 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20    }.  }..  rc = 
172aa 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
172ab 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
172ac 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
172ad 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
172ae 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
172af 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74  age] );.  assert
172b0 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  ( pCur->apPage[p
172b1 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49  Cur->iPage]->isI
172b2 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nit );.  assert(
172b3 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
172b4 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
172b5 6c 3e 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74  l>0 || pCur->eSt
172b6 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
172b7 4c 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75  LID );.  if( pCu
172b8 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
172b9 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20  R_INVALID ){.   
172ba 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20   *pRes = -1;.   
172bb 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
172bc 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
172bd 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a  e]->nCell==0 );.
172be 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
172bf 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65  E_OK;.  }.  asse
172c0 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65  rt( pCur->apPage
172c1 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70  [0]->intKey || p
172c2 49 64 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28  IdxKey );.  for(
172c3 3b 3b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72  ;;){.    int lwr
172c4 2c 20 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20  , upr;.    Pgno 
172c5 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50  chldPg;.    MemP
172c6 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75  age *pPage = pCu
172c7 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
172c8 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20  iPage];.    int 
172c9 63 3b 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65  c;..    /* pPage
172ca 2d 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 20  ->nCell must be 
172cb 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
172cc 6f 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68  o. If this is th
172cd 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20  e root-page.    
172ce 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 77 6f  ** the cursor wo
172cf 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 49 4e  uld have been IN
172d0 56 41 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 20  VALID above and 
172d1 74 68 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f  this for(;;) loo
172d2 70 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e  p.    ** not run
172d3 2e 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74  . If this is not
172d4 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20   the root-page, 
172d5 74 68 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43  then the moveToC
172d6 68 69 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a 20  hild() routine. 
172d7 20 20 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65     ** would have
172d8 20 61 6c 72 65 61 64 79 20 64 65 74 65 63 74 65   already detecte
172d9 64 20 64 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e  d db corruption.
172da 20 53 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67   Similarly, pPag
172db 65 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65  e must.    ** be
172dc 20 74 68 65 20 72 69 67 68 74 20 6b 69 6e 64 20   the right kind 
172dd 28 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 29  (index or table)
172de 20 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 2e   of b-tree page.
172df 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a   Otherwise.    *
172e0 2a 20 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  * a moveToChild(
172e1 29 20 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28  ) or moveToRoot(
172e2 29 20 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76  ) call would hav
172e3 65 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75  e detected corru
172e4 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61  ption.  */.    a
172e5 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43  ssert( pPage->nC
172e6 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ell>0 );.    ass
172e7 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b  ert( pPage->intK
172e8 65 79 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29  ey==(pIdxKey==0)
172e9 20 29 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b   );.    lwr = 0;
172ea 0a 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 65  .    upr = pPage
172eb 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69  ->nCell-1;.    i
172ec 66 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a  f( biasRight ){.
172ed 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64        pCur->aiId
172ee 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d  x[pCur->iPage] =
172ef 20 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d   (u16)upr;.    }
172f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72  else{.      pCur
172f1 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
172f2 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70  age] = (u16)((up
172f3 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d  r+lwr)/2);.    }
172f4 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20  .    for(;;){.  
172f5 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43      int idx = pC
172f6 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
172f7 69 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78  iPage]; /* Index
172f8 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c   of current cell
172f9 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20   in pPage */.   
172fa 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20     u8 *pCell;   
172fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
172fc 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
172fd 72 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c  r to current cel
172fe 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20  l in pPage */.. 
172ff 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e       pCur->info.
17300 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20  nSize = 0;.     
17301 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c   pCell = findCel
17302 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20  l(pPage, idx) + 
17303 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53  pPage->childPtrS
17304 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ize;.      if( p
17305 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a  Page->intKey ){.
17306 20 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c          i64 nCel
17307 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66  lKey;.        if
17308 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61  ( pPage->hasData
17309 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33   ){.          u3
1730a 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20  2 dummy;.       
1730b 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56     pCell += getV
1730c 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64  arint32(pCell, d
1730d 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d  ummy);.        }
1730e 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69  .        getVari
1730f 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29  nt(pCell, (u64*)
17310 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20  &nCellKey);.    
17311 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79      if( nCellKey
17312 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  ==intKey ){.    
17313 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20        c = 0;.   
17314 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
17315 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29  CellKey<intKey )
17316 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20  {.          c = 
17317 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  -1;.        }els
17318 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  e{.          ass
17319 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e  ert( nCellKey>in
1731a 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20  tKey );.        
1731b 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20    c = +1;.      
1731c 20 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72    }.        pCur
1731d 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b  ->validNKey = 1;
1731e 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69  .        pCur->i
1731f 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c  nfo.nKey = nCell
17320 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  Key;.      }else
17321 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
17322 20 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74   maximum support
17323 65 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20  ed page-size is 
17324 33 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 69  32768 bytes. Thi
17325 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20  s means that.   
17326 20 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69       ** the maxi
17327 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  mum number of re
17328 63 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65  cord bytes store
17329 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d  d on an index B-
1732a 54 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  Tree.        ** 
1732b 70 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20  page is at most 
1732c 38 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 63  8198 bytes, whic
1732d 68 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20  h may be stored 
1732e 61 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20  as a 2-byte.    
1732f 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54      ** varint. T
17330 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
17331 69 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d  is used to attem
17332 70 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73  pt to avoid pars
17333 69 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ing .        ** 
17334 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20  the entire cell 
17335 62 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20  by checking for 
17336 74 68 65 20 63 61 73 65 73 20 77 68 65 72 65 20  the cases where 
17337 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20  the record is . 
17338 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64         ** stored
17339 20 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e   entirely within
1733a 20 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65   the b-tree page
1733b 20 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74   by inspecting t
1733c 68 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 20  he first .      
1733d 20 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20    ** 2 bytes of 
1733e 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20  the cell..      
1733f 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
17340 20 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30   nCell = pCell[0
17341 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ];.        if( !
17342 28 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26  (nCell & 0x80) &
17343 26 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e  & nCell<=pPage->
17344 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20  maxLocal ){.    
17345 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72        /* This br
17346 61 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 65  anch runs if the
17347 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65   record-size fie
17348 6c 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69  ld of the cell i
17349 73 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  s a.          **
1734a 20 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72   single byte var
1734b 69 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f  int and the reco
1734c 72 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79  rd fits entirely
1734d 20 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20   on the main.   
1734e 20 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65         ** b-tree
1734f 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20   page.  */.     
17350 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33       c = sqlite3
17351 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
17352 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29  e(nCell, (void*)
17353 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b  &pCell[1], pIdxK
17354 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ey);.        }el
17355 73 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31  se if( !(pCell[1
17356 5d 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20  ] & 0x80) .     
17357 20 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d       && (nCell =
17358 20 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c   ((nCell&0x7f)<<
17359 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d  7) + pCell[1])<=
1735a 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a  pPage->maxLocal.
1735b 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
1735c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f       /* The reco
1735d 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73  rd-size field is
1735e 20 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74   a 2 byte varint
1735f 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20   and the record 
17360 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69  .          ** fi
17361 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74  ts entirely on t
17362 68 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70  he main b-tree p
17363 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  age.  */.       
17364 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64     c = sqlite3Vd
17365 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
17366 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70  nCell, (void*)&p
17367 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79  Cell[2], pIdxKey
17368 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
17369 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
1736a 68 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20  he record flows 
1736b 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72  over onto one or
1736c 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70   more overflow p
1736d 61 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20  ages. In.       
1736e 20 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20     ** this case 
1736f 74 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e  the whole cell n
17370 65 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 65  eeds to be parse
17371 64 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f  d, a buffer allo
17372 63 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 20  cated.          
17373 2a 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 79  ** and accessPay
17374 6c 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 72  load() used to r
17375 65 74 72 69 65 76 65 20 74 68 65 20 72 65 63 6f  etrieve the reco
17376 72 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20  rd into the.    
17377 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20        ** buffer 
17378 62 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72  before VdbeRecor
17379 64 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62  dCompare() can b
1737a 65 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20  e called. */.   
1737b 20 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65         void *pCe
1737c 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20  llKey;.         
1737d 20 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c   u8 * const pCel
1737e 6c 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20  lBody = pCell - 
1737f 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53  pPage->childPtrS
17380 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 62  ize;.          b
17381 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
17382 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64  (pPage, pCellBod
17383 79 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b  y, &pCur->info);
17384 0a 20 20 20 20 20 20 20 20 20 20 6e 43 65 6c 6c  .          nCell
17385 20 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e   = (int)pCur->in
17386 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20  fo.nKey;.       
17387 20 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71     pCellKey = sq
17388 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65  lite3Malloc( nCe
17389 6c 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ll );.          
1738a 69 66 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20  if( pCellKey==0 
1738b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
1738c 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1738d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f  ;.            go
1738e 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68  to moveto_finish
1738f 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
17390 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 63 63          rc = acc
17391 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c  essPayload(pCur,
17392 20 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69   0, nCell, (unsi
17393 67 6e 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c  gned char*)pCell
17394 4b 65 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Key, 0);.       
17395 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
17396 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
17397 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b  _free(pCellKey);
17398 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
17399 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b  o moveto_finish;
1739a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1739b 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74         c = sqlit
1739c 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70  e3VdbeRecordComp
1739d 61 72 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c  are(nCell, pCell
1739e 4b 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20  Key, pIdxKey);. 
1739f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
173a0 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b  _free(pCellKey);
173a1 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
173a2 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d   }.      if( c==
173a3 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  0 ){.        if(
173a4 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26   pPage->intKey &
173a5 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  & !pPage->leaf )
173a6 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20  {.          lwr 
173a7 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20  = idx;.         
173a8 20 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a   upr = lwr - 1;.
173a9 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
173aa 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
173ab 20 20 20 20 20 20 20 20 20 20 2a 70 52 65 73 20            *pRes 
173ac 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72  = 0;.          r
173ad 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
173ae 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f           goto mo
173af 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20  veto_finish;.   
173b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
173b1 20 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a       if( c<0 ){.
173b2 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64          lwr = id
173b3 78 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  x+1;.      }else
173b4 7b 0a 20 20 20 20 20 20 20 20 75 70 72 20 3d 20  {.        upr = 
173b5 69 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20  idx-1;.      }. 
173b6 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72       if( lwr>upr
173b7 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
173b8 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
173b9 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
173ba 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36  r->iPage] = (u16
173bb 29 28 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a  )((lwr+upr)/2);.
173bc 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
173bd 28 20 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a  ( lwr==upr+1 );.
173be 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
173bf 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20  e->isInit );.   
173c0 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66   if( pPage->leaf
173c1 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67   ){.      chldPg
173c2 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
173c3 69 66 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e  if( lwr>=pPage->
173c4 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63  nCell ){.      c
173c5 68 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65  hldPg = get4byte
173c6 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
173c7 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
173c8 38 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  8]);.    }else{.
173c9 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67        chldPg = g
173ca 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c  et4byte(findCell
173cb 28 70 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20  (pPage, lwr));. 
173cc 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c     }.    if( chl
173cd 64 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  dPg==0 ){.      
173ce 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69  assert( pCur->ai
173cf 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
173d0 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  <pCur->apPage[pC
173d1 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
173d2 6c 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73  l );.      *pRes
173d3 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d   = c;.      rc =
173d4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
173d5 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69    goto moveto_fi
173d6 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nish;.    }.    
173d7 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
173d8 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29  ->iPage] = (u16)
173d9 6c 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69  lwr;.    pCur->i
173da 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20  nfo.nSize = 0;. 
173db 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b     pCur->validNK
173dc 65 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  ey = 0;.    rc =
173dd 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
173de 72 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20  r, chldPg);.    
173df 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f  if( rc ) goto mo
173e0 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d  veto_finish;.  }
173e1 0a 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a  .moveto_finish:.
173e2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
173e3 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
173e4 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72  UE if the cursor
173e5 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67   is not pointing
173e6 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20   at an entry of 
173e7 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  the table..**.**
173e8 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65   TRUE will be re
173e9 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63  turned after a c
173ea 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74  all to sqlite3Bt
173eb 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a  reeNext() moves.
173ec 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74  ** past the last
173ed 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61   entry in the ta
173ee 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74  ble or sqlite3Bt
173ef 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20  reePrev() moves 
173f0 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73  past.** the firs
173f1 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69  t entry.  TRUE i
173f2 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20  s also returned 
173f3 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
173f4 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  empty..*/.SQLITE
173f5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
173f6 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43  ite3BtreeEof(BtC
173f7 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
173f8 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66  /* TODO: What if
173f9 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69   the cursor is i
173fa 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  n CURSOR_REQUIRE
173fb 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62  SEEK but all tab
173fc 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20  le entries.  ** 
173fd 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65  have been delete
173fe 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c  d? This API will
173ff 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20   need to change 
17400 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  to return an err
17401 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20  or code.  ** as 
17402 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c  well as the bool
17403 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65  ean result value
17404 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ..  */.  return 
17405 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70  (CURSOR_VALID!=p
17406 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a  Cur->eState);.}.
17407 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74  ./*.** Advance t
17408 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
17409 20 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 74   next entry in t
1740a 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66  he database.  If
1740b 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74  .** successful t
1740c 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e  hen set *pRes=0.
1740d 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a    If the cursor.
1740e 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70  ** was already p
1740f 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c  ointing to the l
17410 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ast entry in the
17411 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65   database before
17412 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
17413 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65   was called, the
17414 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a  n set *pRes=1..*
17415 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17416 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
17417 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a  eNext(BtCursor *
17418 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29  pCur, int *pRes)
17419 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  {.  int rc;.  in
1741a 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65  t idx;.  MemPage
1741b 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65   *pPage;..  asse
1741c 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
1741d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
1741e 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73  rc = restoreCurs
1741f 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
17420 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
17421 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
17422 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73  urn rc;.  }.  as
17423 73 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b  sert( pRes!=0 );
17424 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e  .  if( CURSOR_IN
17425 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74  VALID==pCur->eSt
17426 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73  ate ){.    *pRes
17427 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
17428 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
17429 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70    if( pCur->skip
1742a 4e 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 70 43  Next>0 ){.    pC
1742b 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30  ur->skipNext = 0
1742c 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b  ;.    *pRes = 0;
1742d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1742e 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75  TE_OK;.  }.  pCu
1742f 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b  r->skipNext = 0;
17430 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72  ..  pPage = pCur
17431 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
17432 50 61 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b  Page];.  idx = +
17433 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75  +pCur->aiIdx[pCu
17434 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73  r->iPage];.  ass
17435 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e  ert( pPage->isIn
17436 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  it );.  assert( 
17437 69 64 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  idx<=pPage->nCel
17438 6c 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e  l );..  pCur->in
17439 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
1743a 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1743b 3d 20 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d  = 0;.  if( idx>=
1743c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  pPage->nCell ){.
1743d 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e      if( !pPage->
1743e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63  leaf ){.      rc
1743f 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70   = moveToChild(p
17440 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70  Cur, get4byte(&p
17441 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
17442 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
17443 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
17444 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
17445 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65     rc = moveToLe
17446 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20  ftmost(pCur);.  
17447 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
17448 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
17449 20 20 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20      }.    do{.  
1744a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50      if( pCur->iP
1744b 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  age==0 ){.      
1744c 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20    *pRes = 1;.   
1744d 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74       pCur->eStat
1744e 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  e = CURSOR_INVAL
1744f 49 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  ID;.        retu
17450 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
17451 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65      }.      move
17452 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a  ToParent(pCur);.
17453 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43        pPage = pC
17454 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
17455 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68  >iPage];.    }wh
17456 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ile( pCur->aiIdx
17457 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70  [pCur->iPage]>=p
17458 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
17459 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
1745a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74    if( pPage->int
1745b 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Key ){.      rc 
1745c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  = sqlite3BtreeNe
1745d 78 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a  xt(pCur, pRes);.
1745e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1745f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
17460 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
17461 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65  n rc;.  }.  *pRe
17462 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61  s = 0;.  if( pPa
17463 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
17464 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
17465 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76  ;.  }.  rc = mov
17466 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72  eToLeftmost(pCur
17467 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
17468 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74  }.../*.** Step t
17469 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
1746a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65   back to the pre
1746b 76 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74  vious entry in t
1746c 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66  he database.  If
1746d 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74  .** successful t
1746e 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e  hen set *pRes=0.
1746f 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a    If the cursor.
17470 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70  ** was already p
17471 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66  ointing to the f
17472 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  irst entry in th
17473 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72  e database befor
17474 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  e.** this routin
17475 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68  e was called, th
17476 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a  en set *pRes=1..
17477 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17478 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
17479 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72  eePrevious(BtCur
1747a 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
1747b 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pRes){.  int rc;
1747c 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
1747d 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  e;..  assert( cu
1747e 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
1747f 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72  Cur) );.  rc = r
17480 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
17481 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66  tion(pCur);.  if
17482 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17483 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
17484 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74  ;.  }.  pCur->at
17485 4c 61 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20  Last = 0;.  if( 
17486 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d  CURSOR_INVALID==
17487 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a  pCur->eState ){.
17488 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20      *pRes = 1;. 
17489 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1748a 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  _OK;.  }.  if( p
1748b 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20  Cur->skipNext<0 
1748c 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69  ){.    pCur->ski
1748d 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a  pNext = 0;.    *
1748e 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65  pRes = 0;.    re
1748f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
17490 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70    }.  pCur->skip
17491 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61  Next = 0;..  pPa
17492 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
17493 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
17494 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
17495 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28  >isInit );.  if(
17496 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
17497 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70  .    int idx = p
17498 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
17499 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20  >iPage];.    rc 
1749a 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
1749b 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e  ur, get4byte(fin
1749c 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78  dCell(pPage, idx
1749d 29 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  )));.    if( rc 
1749e 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1749f 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  rc;.    }.    rc
174a0 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f   = moveToRightmo
174a1 73 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73  st(pCur);.  }els
174a2 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43  e{.    while( pC
174a3 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
174a4 69 50 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20  iPage]==0 ){.   
174a5 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61     if( pCur->iPa
174a6 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ge==0 ){.       
174a7 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
174a8 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
174a9 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20          *pRes = 
174aa 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  1;.        retur
174ab 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
174ac 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54     }.      moveT
174ad 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20  oParent(pCur);. 
174ae 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69     }.    pCur->i
174af 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20  nfo.nSize = 0;. 
174b0 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b     pCur->validNK
174b1 65 79 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75  ey = 0;..    pCu
174b2 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
174b3 50 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61  Page]--;.    pPa
174b4 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
174b5 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
174b6 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69      if( pPage->i
174b7 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d  ntKey && !pPage-
174b8 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72  >leaf ){.      r
174b9 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
174ba 50 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70  Previous(pCur, p
174bb 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Res);.    }else{
174bc 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
174bd 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
174be 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20  .  *pRes = 0;.  
174bf 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
174c0 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
174c1 65 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65  ew page from the
174c2 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
174c3 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61  **.** The new pa
174c4 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  ge is marked as 
174c5 64 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65  dirty.  (In othe
174c6 72 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33  r words, sqlite3
174c7 50 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20  PagerWrite().** 
174c8 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
174c9 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e   called on the n
174ca 65 77 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e  ew page.)  The n
174cb 65 77 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f  ew page has also
174cc 0a 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e  .** been referen
174cd 63 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c  ced and the call
174ce 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72  ing routine is r
174cf 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63  esponsible for c
174d0 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65  alling.** sqlite
174d1 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e  3PagerUnref() on
174d2 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 77 68   the new page wh
174d3 65 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a  en it is done..*
174d4 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  *.** SQLITE_OK i
174d5 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75  s returned on su
174d6 63 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65  ccess.  Any othe
174d7 72 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  r return value i
174d8 6e 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65  ndicates.** an e
174d9 72 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61  rror.  *ppPage a
174da 6e 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e  nd *pPgno are un
174db 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65  defined in the e
174dc 76 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72  vent of an error
174dd 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f  ..** Do not invo
174de 6b 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 55  ke sqlite3PagerU
174df 6e 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67  nref() on *ppPag
174e0 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73  e if an error is
174e1 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
174e2 20 49 66 20 74 68 65 20 22 6e 65 61 72 62 79 22   If the "nearby"
174e3 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
174e4 74 20 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65  t 0, then a (fee
174e5 62 6c 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d  ble) effort is m
174e6 61 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74  ade to .** locat
174e7 65 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74  e a page close t
174e8 6f 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  o the page numbe
174e9 72 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69  r "nearby".  Thi
174ea 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e  s can be used in
174eb 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74   an.** attempt t
174ec 6f 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70  o keep related p
174ed 61 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61  ages close to ea
174ee 63 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20  ch other in the 
174ef 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a  database file,.*
174f0 2a 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20  * which in turn 
174f1 63 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73  can make databas
174f2 65 20 61 63 63 65 73 73 20 66 61 73 74 65 72 2e  e access faster.
174f3 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65  .**.** If the "e
174f4 78 61 63 74 22 20 70 61 72 61 6d 65 74 65 72 20  xact" parameter 
174f5 69 73 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68  is not 0, and th
174f6 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65  e page-number ne
174f7 61 72 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20  arby exists .** 
174f8 61 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20  anywhere on the 
174f9 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20  free-list, then 
174fa 69 74 20 69 73 20 67 75 61 72 65 6e 74 65 65 64  it is guarenteed
174fb 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e   to be returned.
174fc 20 54 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79   This.** is only
174fd 20 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61   used by auto-va
174fe 63 75 75 6d 20 64 61 74 61 62 61 73 65 73 20 77  cuum databases w
174ff 68 65 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61  hen allocating a
17500 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73   new table..*/.s
17501 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61  tatic int alloca
17502 74 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 42  teBtreePage(.  B
17503 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20  tShared *pBt, . 
17504 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67   MemPage **ppPag
17505 65 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e  e, .  Pgno *pPgn
17506 6f 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62  o, .  Pgno nearb
17507 79 2c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b  y,.  u8 exact.){
17508 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
17509 65 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e1;.  int rc;.  
1750a 75 33 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75  u32 n;     /* Nu
1750b 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e  mber of pages on
1750c 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f   the freelist */
1750d 0a 20 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a  .  u32 k;     /*
1750e 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65   Number of leave
1750f 73 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f  s on the trunk o
17510 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a  f the freelist *
17511 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72  /.  MemPage *pTr
17512 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61  unk = 0;.  MemPa
17513 67 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d  ge *pPrevTrunk =
17514 20 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67   0;.  Pgno mxPag
17515 65 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20  e;     /* Total 
17516 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
17517 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20  base file */..  
17518 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
17519 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
1751a 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67  mutex) );.  pPag
1751b 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31  e1 = pBt->pPage1
1751c 3b 0a 20 20 6d 78 50 61 67 65 20 3d 20 70 61 67  ;.  mxPage = pag
1751d 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
1751e 3b 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65  ;.  n = get4byte
1751f 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
17520 33 36 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65  36]);.  testcase
17521 28 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b  ( n==mxPage-1 );
17522 0a 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65  .  if( n>=mxPage
17523 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
17524 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
17525 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e  PT;.  }.  if( n>
17526 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72  0 ){.    /* Ther
17527 65 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74  e are pages on t
17528 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65  he freelist.  Re
17529 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65  use one of those
1752a 20 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50   pages. */.    P
1752b 67 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20  gno iTrunk;.    
1752c 75 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20  u8 searchList = 
1752d 30 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65  0; /* If the fre
1752e 65 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73  e-list must be s
1752f 65 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61  earched for 'nea
17530 72 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20  rby' */.    .   
17531 20 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63   /* If the 'exac
17532 74 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73  t' parameter was
17533 20 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72   true and a quer
17534 79 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72  y of the pointer
17535 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77  -map.    ** show
17536 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  s that the page 
17537 27 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65  'nearby' is some
17538 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65  where on the fre
17539 65 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20  e-list, then.   
1753a 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c   ** the entire-l
1753b 69 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72  ist will be sear
1753c 63 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61  ched for that pa
1753d 67 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64  ge..    */.#ifnd
1753e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1753f 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
17540 28 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62  ( exact && nearb
17541 79 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20  y<=mxPage ){.   
17542 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20     u8 eType;.   
17543 20 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62     assert( nearb
17544 79 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  y>0 );.      ass
17545 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61  ert( pBt->autoVa
17546 63 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63  cuum );.      rc
17547 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74   = ptrmapGet(pBt
17548 2c 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65  , nearby, &eType
17549 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
1754a 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1754b 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d        if( eType=
1754c 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45  =PTRMAP_FREEPAGE
1754d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72   ){.        sear
1754e 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20  chList = 1;.    
1754f 20 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f    }.      *pPgno
17550 20 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d   = nearby;.    }
17551 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
17552 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72  Decrement the fr
17553 65 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79  ee-list count by
17554 20 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74   1. Set iTrunk t
17555 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  o the index of t
17556 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20  he.    ** first 
17557 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20  free-list trunk 
17558 70 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b  page. iPrevTrunk
17559 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e   is initially 1.
1755a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
1755b 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1755c 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61  te(pPage1->pDbPa
1755d 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ge);.    if( rc 
1755e 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
1755f 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
17560 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d  1->aData[36], n-
17561 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20  1);..    /* The 
17562 63 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73  code within this
17563 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c   loop is run onl
17564 79 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73  y once if the 's
17565 65 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61  earchList' varia
17566 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f  ble.    ** is no
17567 74 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73  t true. Otherwis
17568 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20  e, it runs once 
17569 66 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70  for each trunk-p
1756a 61 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a  age on the.    *
1756b 2a 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69  * free-list unti
1756c 6c 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72  l the page 'near
1756d 62 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a  by' is located..
1756e 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a      */.    do {.
1756f 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b        pPrevTrunk
17570 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20   = pTrunk;.     
17571 20 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20   if( pPrevTrunk 
17572 29 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e  ){.        iTrun
17573 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  k = get4byte(&pP
17574 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  revTrunk->aData[
17575 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  0]);.      }else
17576 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b  {.        iTrunk
17577 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61   = get4byte(&pPa
17578 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b  ge1->aData[32]);
17579 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74  .      }.      t
1757a 65 73 74 63 61 73 65 28 20 69 54 72 75 6e 6b 3d  estcase( iTrunk=
1757b 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20  =mxPage );.     
1757c 20 69 66 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61   if( iTrunk>mxPa
1757d 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  ge ){.        rc
1757e 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
1757f 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65  T_BKPT;.      }e
17580 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20  lse{.        rc 
17581 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
17582 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72  Bt, iTrunk, &pTr
17583 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  unk, 0);.      }
17584 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
17585 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20  .        pTrunk 
17586 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  = 0;.        got
17587 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
17588 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  age;.      }..  
17589 20 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65      k = get4byte
1758a 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
1758b 34 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b  4]);.      if( k
1758c 3d 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c 69  ==0 && !searchLi
1758d 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  st ){.        /*
1758e 20 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e   The trunk has n
1758f 6f 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65  o leaves and the
17590 20 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69   list is not bei
17591 6e 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20  ng searched. .  
17592 20 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72        ** So extr
17593 61 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61  act the trunk pa
17594 67 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73  ge itself and us
17595 65 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c  e it as the newl
17596 79 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c  y .        ** al
17597 6c 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a  located page */.
17598 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
17599 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b  pPrevTrunk==0 );
1759a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1759b 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1759c 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29  pTrunk->pDbPage)
1759d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
1759e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
1759f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
175a0 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  page;.        }.
175a1 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d          *pPgno =
175a2 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20   iTrunk;.       
175a3 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d   memcpy(&pPage1-
175a4 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72  >aData[32], &pTr
175a5 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34  unk->aData[0], 4
175a6 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61  );.        *ppPa
175a7 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20  ge = pTrunk;.   
175a8 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b       pTrunk = 0;
175a9 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28  .        TRACE((
175aa 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72  "ALLOCATE: %d tr
175ab 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61  unk - %d free pa
175ac 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50  ges left\n", *pP
175ad 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20  gno, n-1));.    
175ae 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75    }else if( k>(u
175af 33 32 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53  32)(pBt->usableS
175b0 69 7a 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20  ize/4 - 2) ){.  
175b1 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
175b2 66 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61  f k is out of ra
175b3 6e 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63  nge.  Database c
175b4 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20  orruption */.   
175b5 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
175b6 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
175b7 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
175b8 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23  allocate_page;.#
175b9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
175ba 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
175bb 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65      }else if( se
175bc 61 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72  archList && near
175bd 62 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20  by==iTrunk ){.  
175be 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73        /* The lis
175bf 74 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63  t is being searc
175c0 68 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75  hed and this tru
175c1 6e 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70  nk page is the p
175c2 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  age.        ** t
175c3 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61  o allocate, rega
175c4 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
175c5 72 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e  r it has leaves.
175c6 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
175c7 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67      assert( *pPg
175c8 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20  no==iTrunk );.  
175c9 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20        *ppPage = 
175ca 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20  pTrunk;.        
175cb 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a  searchList = 0;.
175cc 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
175cd 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
175ce 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
175cf 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20  .        if( rc 
175d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
175d1 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
175d2 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  age;.        }. 
175d3 20 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20         if( k==0 
175d4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
175d5 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a   !pPrevTrunk ){.
175d6 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63              memc
175d7 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  py(&pPage1->aDat
175d8 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  a[32], &pTrunk->
175d9 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20  aData[0], 4);.  
175da 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
175db 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
175dc 79 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61  y(&pPrevTrunk->a
175dd 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b  Data[0], &pTrunk
175de 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a  ->aData[0], 4);.
175df 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
175e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
175e1 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e       /* The trun
175e2 6b 20 70 61 67 65 20 69 73 20 72 65 71 75 69 72  k page is requir
175e3 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
175e4 20 62 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73   but it contains
175e5 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70   .          ** p
175e6 6f 69 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d  ointers to free-
175e7 6c 69 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65  list leaves. The
175e8 20 66 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f   first leaf beco
175e9 6d 65 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20  mes a trunk.    
175ea 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e        ** page in
175eb 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20   this case..    
175ec 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
175ed 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77     MemPage *pNew
175ee 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20  Trunk;.         
175ef 20 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20   Pgno iNewTrunk 
175f0 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75  = get4byte(&pTru
175f1 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20  nk->aData[8]);. 
175f2 20 20 20 20 20 20 20 20 20 69 66 28 20 69 4e 65           if( iNe
175f3 77 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b  wTrunk>mxPage ){
175f4 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63   .            rc
175f5 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
175f6 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20  T_BKPT;.        
175f7 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
175f8 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
175f9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
175fa 20 20 74 65 73 74 63 61 73 65 28 20 69 4e 65 77    testcase( iNew
175fb 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b  Trunk==mxPage );
175fc 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
175fd 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  btreeGetPage(pBt
175fe 2c 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e  , iNewTrunk, &pN
175ff 65 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20  ewTrunk, 0);.   
17600 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
17601 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17602 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
17603 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
17604 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
17605 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17606 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65  e3PagerWrite(pNe
17607 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29  wTrunk->pDbPage)
17608 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
17609 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1760a 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c  .            rel
1760b 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75  easePage(pNewTru
1760c 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nk);.           
1760d 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
1760e 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20  te_page;.       
1760f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d     }.          m
17610 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b  emcpy(&pNewTrunk
17611 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72  ->aData[0], &pTr
17612 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34  unk->aData[0], 4
17613 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74  );.          put
17614 34 62 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b  4byte(&pNewTrunk
17615 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29  ->aData[4], k-1)
17616 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63  ;.          memc
17617 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61  py(&pNewTrunk->a
17618 44 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b  Data[8], &pTrunk
17619 2d 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d  ->aData[12], (k-
1761a 31 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20  1)*4);.         
1761b 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65   releasePage(pNe
1761c 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20  wTrunk);.       
1761d 20 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75     if( !pPrevTru
1761e 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nk ){.          
1761f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17620 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
17621 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  e(pPage1->pDbPag
17622 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  e) );.          
17623 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67    put4byte(&pPag
17624 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69  e1->aData[32], i
17625 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20  NewTrunk);.     
17626 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17627 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
17628 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
17629 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61  PrevTrunk->pDbPa
1762a 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ge);.           
1762b 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
1762c 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
1762d 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
1762e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
1762f 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62             put4b
17630 79 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d  yte(&pPrevTrunk-
17631 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54  >aData[0], iNewT
17632 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20  runk);.         
17633 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
17634 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b       pTrunk = 0;
17635 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28  .        TRACE((
17636 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72  "ALLOCATE: %d tr
17637 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61  unk - %d free pa
17638 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50  ges left\n", *pP
17639 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64  gno, n-1));.#end
1763a 69 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  if.      }else i
1763b 66 28 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20  f( k>0 ){.      
1763c 20 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c    /* Extract a l
1763d 65 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75  eaf from the tru
1763e 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33  nk */.        u3
1763f 32 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20  2 closest;.     
17640 20 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20     Pgno iPage;. 
17641 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20         unsigned 
17642 63 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54  char *aData = pT
17643 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20  runk->aData;.   
17644 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
17645 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75  3PagerWrite(pTru
17646 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  nk->pDbPage);.  
17647 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
17648 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
17649 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
1764a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1764b 20 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30      if( nearby>0
1764c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33   ){.          u3
1764d 32 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69  2 i;.          i
1764e 6e 74 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20  nt dist;.       
1764f 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a     closest = 0;.
17650 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d            dist =
17651 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61   get4byte(&aData
17652 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20  [8]) - nearby;. 
17653 20 20 20 20 20 20 20 20 20 69 66 28 20 64 69 73           if( dis
17654 74 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69  t<0 ) dist = -di
17655 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  st;.          fo
17656 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29  r(i=1; i<k; i++)
17657 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
17658 74 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 28  t d2 = get4byte(
17659 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d  &aData[8+i*4]) -
1765a 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20   nearby;.       
1765b 20 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 20       if( d2<0 ) 
1765c 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20  d2 = -d2;.      
1765d 20 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 73        if( d2<dis
1765e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
1765f 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a     closest = i;.
17660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69                di
17661 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20  st = d2;.       
17662 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
17663 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
17664 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73  {.          clos
17665 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  est = 0;.       
17666 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67   }..        iPag
17667 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44  e = get4byte(&aD
17668 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d  ata[8+closest*4]
17669 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
1766a 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61  ase( iPage==mxPa
1766b 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ge );.        if
1766c 28 20 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29  ( iPage>mxPage )
1766d 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
1766e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1766f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20  BKPT;.          
17670 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
17671 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20  e_page;.        
17672 7d 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  }.        testca
17673 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67  se( iPage==mxPag
17674 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e );.        if(
17675 20 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20   !searchList || 
17676 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b  iPage==nearby ){
17677 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e  .          int n
17678 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20  oContent;.      
17679 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61      *pPgno = iPa
1767a 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 54 52  ge;.          TR
1767b 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20  ACE(("ALLOCATE: 
1767c 25 64 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f  %d was leaf %d o
1767d 66 20 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64  f %d on trunk %d
1767e 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
1767f 20 20 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72     ": %d more fr
17680 65 65 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20  ee pages\n",.   
17681 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70                *p
17682 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c  Pgno, closest+1,
17683 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f   k, pTrunk->pgno
17684 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20  , n-1));.       
17685 20 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b     if( closest<k
17686 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  -1 ){.          
17687 20 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b    memcpy(&aData[
17688 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61  8+closest*4], &a
17689 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b  Data[4+k*4], 4);
1768a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1768b 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28         put4byte(
1768c 26 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b  &aData[4], k-1);
1768d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1768e 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
1768f 73 77 72 69 74 65 61 62 6c 65 28 70 54 72 75 6e  swriteable(pTrun
17690 6b 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  k->pDbPage) );. 
17691 20 20 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65           noConte
17692 6e 74 20 3d 20 21 62 74 72 65 65 47 65 74 48 61  nt = !btreeGetHa
17693 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70  sContent(pBt, *p
17694 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20  Pgno);.         
17695 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
17696 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20  ge(pBt, *pPgno, 
17697 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e  ppPage, noConten
17698 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  t);.          if
17699 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1769a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
1769b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1769c 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d  Write((*ppPage)-
1769d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
1769e 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
1769f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
176a0 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73            releas
176a1 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a  ePage(*ppPage);.
176a2 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
176a3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
176a4 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d      searchList =
176a5 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
176a6 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65      }.      rele
176a7 61 73 65 50 61 67 65 28 70 50 72 65 76 54 72 75  asePage(pPrevTru
176a8 6e 6b 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76  nk);.      pPrev
176a9 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d  Trunk = 0;.    }
176aa 77 68 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73  while( searchLis
176ab 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  t );.  }else{.  
176ac 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e    /* There are n
176ad 6f 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66  o pages on the f
176ae 72 65 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61  reelist, so crea
176af 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 61 74  te a new page at
176b0 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20   the.    ** end 
176b1 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20  of the file */. 
176b2 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 70     int nPage = p
176b3 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
176b4 74 29 3b 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d  t);.    *pPgno =
176b5 20 6e 50 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20   nPage + 1;..   
176b6 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e   if( *pPgno==PEN
176b7 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
176b8 42 74 29 20 29 7b 0a 20 20 20 20 20 20 28 2a 70  Bt) ){.      (*p
176b9 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  Pgno)++;.    }..
176ba 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
176bb 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
176bc 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
176bd 56 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50  Vacuum && PTRMAP
176be 5f 49 53 50 41 47 45 28 70 42 74 2c 20 2a 70 50  _ISPAGE(pBt, *pP
176bf 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  gno) ){.      /*
176c0 20 49 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72   If *pPgno refer
176c1 73 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d  s to a pointer-m
176c2 61 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74  ap page, allocat
176c3 65 20 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a  e two new pages.
176c4 20 20 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20        ** at the 
176c5 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20  end of the file 
176c6 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20  instead of one. 
176c7 54 68 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61  The first alloca
176c8 74 65 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a  ted page.      *
176c9 2a 20 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20  * becomes a new 
176ca 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
176cb 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20  , the second is 
176cc 75 73 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  used by the call
176cd 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  er..      */.   
176ce 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20     MemPage *pPg 
176cf 3d 20 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45  = 0;.      TRACE
176d0 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20  (("ALLOCATE: %d 
176d1 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65  from end of file
176d2 20 28 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61   (pointer-map pa
176d3 67 65 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29  ge)\n", *pPgno))
176d4 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
176d5 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f  *pPgno!=PENDING_
176d6 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
176d7 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72  ;.      rc = btr
176d8 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a  eeGetPage(pBt, *
176d9 70 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b  pPgno, &pPg, 0);
176da 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
176db 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
176dc 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
176dd 50 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e  PagerWrite(pPg->
176de 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
176df 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
176e0 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
176e1 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
176e2 6e 20 72 63 3b 0a 20 20 20 20 20 20 28 2a 70 50  n rc;.      (*pP
176e3 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66  gno)++;.      if
176e4 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e  ( *pPgno==PENDIN
176e5 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
176e6 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20   ){ (*pPgno)++; 
176e7 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
176e8 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67      assert( *pPg
176e9 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  no!=PENDING_BYTE
176ea 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20  _PAGE(pBt) );.  
176eb 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50    rc = btreeGetP
176ec 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c  age(pBt, *pPgno,
176ed 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20   ppPage, 0);.   
176ee 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
176ef 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71   rc;.    rc = sq
176f0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
176f1 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61  (*ppPage)->pDbPa
176f2 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
176f3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
176f4 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
176f5 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  *ppPage);.    }.
176f6 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f      TRACE(("ALLO
176f7 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e  CATE: %d from en
176f8 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70  d of file\n", *p
176f9 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61  Pgno));.  }..  a
176fa 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50  ssert( *pPgno!=P
176fb 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
176fc 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c  (pBt) );..end_al
176fd 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 72  locate_page:.  r
176fe 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e  eleasePage(pTrun
176ff 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67  k);.  releasePag
17700 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20  e(pPrevTrunk);. 
17701 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
17702 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  OK ){.    if( sq
17703 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65  lite3PagerPageRe
17704 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29  fcount((*ppPage)
17705 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a  ->pDbPage)>1 ){.
17706 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
17707 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20  e(*ppPage);.    
17708 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17709 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1770a 20 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65    }.    (*ppPage
1770b 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20  )->isInit = 0;. 
1770c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50   }else{.    *ppP
1770d 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  age = 0;.  }.  r
1770e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1770f 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
17710 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
17711 70 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68  page iPage to th
17712 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
17713 66 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49  free-list. .** I
17714 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
17715 74 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  t the page is no
17716 74 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74  t already a part
17717 20 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73   of the free-lis
17718 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c  t..**.** The val
17719 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ue passed as the
1771a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
1771b 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
1771c 6e 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a  n is optional..*
1771d 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
1771e 68 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20  happens to have 
1771f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
17720 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20   MemPage object 
17721 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  .** correspondin
17722 67 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20  g to page iPage 
17723 68 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61  handy, it may pa
17724 73 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63  ss it as the sec
17725 6f 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f  ond value. .** O
17726 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79  therwise, it may
17727 20 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a   pass NULL..**.*
17728 2a 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74  * If a pointer t
17729 6f 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65  o a MemPage obje
1772a 63 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20  ct is passed as 
1772b 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
1772c 65 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65  ent,.** its refe
1772d 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e  rence count is n
1772e 6f 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68  ot altered by th
1772f 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  is function..*/.
17730 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50  static int freeP
17731 61 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70  age2(BtShared *p
17732 42 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65  Bt, MemPage *pMe
17733 6d 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67  mPage, Pgno iPag
17734 65 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  e){.  MemPage *p
17735 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20  Trunk = 0;      
17736 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65            /* Fre
17737 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67  e-list trunk pag
17738 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75  e */.  Pgno iTru
17739 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nk = 0;         
1773a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
1773b 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65  ge number of fre
1773c 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67  e-list trunk pag
1773d 65 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20  e */ .  MemPage 
1773e 2a 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70  *pPage1 = pBt->p
1773f 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c  Page1;      /* L
17740 6f 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74  ocal reference t
17741 6f 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65  o page 1 */.  Me
17742 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20  mPage *pPage;   
17743 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17744 20 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20    /* Page being 
17745 66 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55  freed. May be NU
17746 4c 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  LL. */.  int rc;
17747 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17748 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17749 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20  Return Code */. 
1774a 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20   int nFree;     
1774b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1774c 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20       /* Initial 
1774d 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
1774e 6f 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a  on free-list */.
1774f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17750 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
17751 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
17752 73 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29  ssert( iPage>1 )
17753 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65  ;.  assert( !pMe
17754 6d 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67  mPage || pMemPag
17755 65 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29  e->pgno==iPage )
17756 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67  ;..  if( pMemPag
17757 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d  e ){.    pPage =
17758 20 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73   pMemPage;.    s
17759 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70  qlite3PagerRef(p
1775a 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
1775b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61    }else{.    pPa
1775c 67 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f  ge = btreePageLo
1775d 6f 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29  okup(pBt, iPage)
1775e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72  ;.  }..  /* Incr
1775f 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70  ement the free p
17760 61 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61  age count on pPa
17761 67 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ge1 */.  rc = sq
17762 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
17763 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
17764 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74  ;.  if( rc ) got
17765 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
17766 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79    nFree = get4by
17767 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  te(&pPage1->aDat
17768 61 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79  a[36]);.  put4by
17769 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  te(&pPage1->aDat
1776a 61 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b  a[36], nFree+1);
1776b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1776c 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20  SECURE_DELETE.  
1776d 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45  /* If the SQLITE
1776e 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63  _SECURE_DELETE c
1776f 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
17770 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74  on is enabled, t
17771 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20  hen.  ** always 
17772 66 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20  fully overwrite 
17773 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74  deleted informat
17774 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a  ion with zeros..
17775 20 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61    */.  if( (!pPa
17776 67 65 20 26 26 20 28 72 63 20 3d 20 62 74 72 65  ge && (rc = btre
17777 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50  eGetPage(pBt, iP
17778 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29  age, &pPage, 0))
17779 29 0a 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20  ).   ||         
1777a 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33     (rc = sqlite3
1777b 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
1777c 2d 3e 70 44 62 50 61 67 65 29 29 0a 20 20 29 7b  ->pDbPage)).  ){
1777d 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61  .    goto freepa
1777e 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65  ge_out;.  }.  me
1777f 6d 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74  mset(pPage->aDat
17780 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 74  a, 0, pPage->pBt
17781 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e  ->pageSize);.#en
17782 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  dif..  /* If the
17783 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
17784 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20  ts auto-vacuum, 
17785 77 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69  write an entry i
17786 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  n the pointer-ma
17787 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61  p.  ** to indica
17788 74 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65  te that the page
17789 20 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20   is free..  */. 
1778a 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
1778b 4d 20 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50  M ){.    ptrmapP
1778c 75 74 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50  ut(pBt, iPage, P
1778d 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20  TRMAP_FREEPAGE, 
1778e 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28  0, &rc);.    if(
1778f 20 72 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70   rc ) goto freep
17790 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  age_out;.  }..  
17791 2f 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74  /* Now manipulat
17792 65 20 74 68 65 20 61 63 74 75 61 6c 20 64 61 74  e the actual dat
17793 61 62 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20  abase free-list 
17794 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 72 65  structure. There
17795 20 61 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f   are two.  ** po
17796 73 73 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20  ssibilities. If 
17797 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73  the free-list is
17798 20 63 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79   currently empty
17799 2c 20 6f 72 20 69 66 20 74 68 65 20 66 69 72 73  , or if the firs
1779a 74 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67  t.  ** trunk pag
1779b 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69  e in the free-li
1779c 73 74 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e  st is full, then
1779d 20 74 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20   this page will 
1779e 62 65 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65  become a.  ** ne
1779f 77 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e  w free-list trun
177a0 6b 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73  k page. Otherwis
177a1 65 2c 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d  e, it will becom
177a2 65 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a  e a leaf of the.
177a3 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b    ** first trunk
177a4 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 75 72   page in the cur
177a5 72 65 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20  rent free-list. 
177a6 54 68 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73  This block tests
177a7 20 69 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70   if it.  ** is p
177a8 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74  ossible to add t
177a9 68 65 20 70 61 67 65 20 61 73 20 61 20 6e 65 77  he page as a new
177aa 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e   free-list leaf.
177ab 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65  .  */.  if( nFre
177ac 65 21 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20  e!=0 ){.    u32 
177ad 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20  nLeaf;          
177ae 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c        /* Initial
177af 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20   number of leaf 
177b0 63 65 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70  cells on trunk p
177b1 61 67 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75  age */..    iTru
177b2 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  nk = get4byte(&p
177b3 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
177b4 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65  );.    rc = btre
177b5 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54  eGetPage(pBt, iT
177b6 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30  runk, &pTrunk, 0
177b7 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
177b8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
177b9 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f    goto freepage_
177ba 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  out;.    }..    
177bb 6e 4c 65 61 66 20 3d 20 67 65 74 34 62 79 74 65  nLeaf = get4byte
177bc 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
177bd 34 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  4]);.    assert(
177be 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
177bf 3e 33 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e  >32 );.    if( n
177c0 4c 65 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d  Leaf > (u32)pBt-
177c1 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20  >usableSize/4 - 
177c2 32 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  2 ){.      rc = 
177c3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
177c4 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
177c5 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
177c6 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61    }.    if( nLea
177c7 66 20 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73  f < (u32)pBt->us
177c8 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29  ableSize/4 - 8 )
177c9 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68  {.      /* In th
177ca 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73  is case there is
177cb 20 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75   room on the tru
177cc 6e 6b 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72  nk page to inser
177cd 74 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20  t the page.     
177ce 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20   ** being freed 
177cf 61 73 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20  as a new leaf.. 
177d0 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
177d1 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 74   Note that the t
177d2 72 75 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74  runk page is not
177d3 20 72 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74   really full unt
177d4 69 6c 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20  il it contains. 
177d5 20 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69       ** usableSi
177d6 7a 65 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73  ze/4 - 2 entries
177d7 2c 20 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65  , not usableSize
177d8 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61  /4 - 8 entries a
177d9 73 20 77 65 20 68 61 76 65 0a 20 20 20 20 20 20  s we have.      
177da 2a 2a 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64  ** coded.  But d
177db 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65  ue to a coding e
177dc 72 72 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73  rror in versions
177dd 20 6f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72   of SQLite prior
177de 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36   to.      ** 3.6
177df 2e 30 2c 20 64 61 74 61 62 61 73 65 73 20 77 69  .0, databases wi
177e0 74 68 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e  th freelist trun
177e1 6b 20 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20  k pages holding 
177e2 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20  more than.      
177e3 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20  ** usableSize/4 
177e4 2d 20 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c  - 8 entries will
177e5 20 62 65 20 72 65 70 6f 72 74 65 64 20 61 73 20   be reported as 
177e6 63 6f 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64  corrupt.  In ord
177e7 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d  er.      ** to m
177e8 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64  aintain backward
177e9 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  s compatibility 
177ea 77 69 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69  with older versi
177eb 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20  ons of SQLite,. 
177ec 20 20 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20       ** we will 
177ed 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74  continue to rest
177ee 72 69 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20  rict the number 
177ef 6f 66 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73  of entries to us
177f0 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20  ableSize/4 - 8. 
177f1 20 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e       ** for now.
177f2 20 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20    At some point 
177f3 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 28 6f  in the future (o
177f4 6e 63 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73  nce everyone has
177f5 20 75 70 67 72 61 64 65 64 0a 20 20 20 20 20 20   upgraded.      
177f6 2a 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c  ** to 3.6.0 or l
177f7 61 74 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20  ater) we should 
177f8 63 6f 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20  consider fixing 
177f9 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20  the conditional 
177fa 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74  above.      ** t
177fb 6f 20 72 65 61 64 20 22 75 73 61 62 6c 65 53 69  o read "usableSi
177fc 7a 65 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20  ze/4-2" instead 
177fd 6f 66 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34  of "usableSize/4
177fe 2d 38 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  -8"..      */.  
177ff 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17800 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
17801 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
17802 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
17803 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
17804 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b  put4byte(&pTrunk
17805 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61  ->aData[4], nLea
17806 66 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75  f+1);.        pu
17807 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e  t4byte(&pTrunk->
17808 61 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d  aData[8+nLeaf*4]
17809 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65  , iPage);.#ifnde
1780a 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f  f SQLITE_SECURE_
1780b 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 69  DELETE.        i
1780c 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20  f( pPage ){.    
1780d 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
1780e 65 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67  erDontWrite(pPag
1780f 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
17810 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
17811 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65        rc = btree
17812 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42  SetHasContent(pB
17813 74 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20  t, iPage);.     
17814 20 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28 28   }.      TRACE((
17815 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6c  "FREE-PAGE: %d l
17816 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67  eaf on trunk pag
17817 65 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70  e %d\n",pPage->p
17818 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f  gno,pTrunk->pgno
17819 29 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66  ));.      goto f
1781a 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20  reepage_out;.   
1781b 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
1781c 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f  control flows to
1781d 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
1781e 6e 20 69 74 20 77 61 73 20 6e 6f 74 20 70 6f 73  n it was not pos
1781f 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65  sible to add the
17820 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 62  .  ** the page b
17821 65 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 20  eing freed as a 
17822 6c 65 61 66 20 70 61 67 65 20 6f 66 20 74 68 65  leaf page of the
17823 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20   first trunk in 
17824 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20  the free-list.. 
17825 20 2a 2a 20 50 6f 73 73 69 62 6c 79 20 62 65 63   ** Possibly bec
17826 61 75 73 65 20 74 68 65 20 66 72 65 65 2d 6c 69  ause the free-li
17827 73 74 20 69 73 20 65 6d 70 74 79 2c 20 6f 72 20  st is empty, or 
17828 70 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65  possibly because
17829 20 74 68 65 20 0a 20 20 2a 2a 20 66 69 72 73 74   the .  ** first
1782a 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72   trunk in the fr
1782b 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e  ee-list is full.
1782c 20 45 69 74 68 65 72 20 77 61 79 2c 20 74 68 65   Either way, the
1782d 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65   page being free
1782e 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f  d.  ** will beco
1782f 6d 65 20 74 68 65 20 6e 65 77 20 66 69 72 73 74  me the new first
17830 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74   trunk page in t
17831 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20  he free-list..  
17832 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d  */.  if( pPage==
17833 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d  0 && SQLITE_OK!=
17834 28 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61  (rc = btreeGetPa
17835 67 65 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26  ge(pBt, iPage, &
17836 70 50 61 67 65 2c 20 30 29 29 20 29 7b 0a 20 20  pPage, 0)) ){.  
17837 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f    goto freepage_
17838 6f 75 74 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  out;.  }.  rc = 
17839 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1783a 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
1783b 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1783c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f  ITE_OK ){.    go
1783d 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b  to freepage_out;
1783e 0a 20 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28  .  }.  put4byte(
1783f 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54  pPage->aData, iT
17840 72 75 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74  runk);.  put4byt
17841 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e(&pPage->aData[
17842 34 5d 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79  4], 0);.  put4by
17843 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  te(&pPage1->aDat
17844 61 5b 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20  a[32], iPage);. 
17845 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41   TRACE(("FREE-PA
17846 47 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b  GE: %d new trunk
17847 20 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20   page replacing 
17848 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67  %d\n", pPage->pg
17849 6e 6f 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66  no, iTrunk));..f
1784a 72 65 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69  reepage_out:.  i
1784b 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20  f( pPage ){.    
1784c 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
1784d 30 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65  0;.  }.  release
1784e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72  Page(pPage);.  r
1784f 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e  eleasePage(pTrun
17850 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  k);.  return rc;
17851 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  .}.static void f
17852 72 65 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20  reePage(MemPage 
17853 2a 70 50 61 67 65 2c 20 69 6e 74 20 2a 70 52 43  *pPage, int *pRC
17854 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d  ){.  if( (*pRC)=
17855 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17856 20 20 2a 70 52 43 20 3d 20 66 72 65 65 50 61 67    *pRC = freePag
17857 65 32 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70  e2(pPage->pBt, p
17858 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e  Page, pPage->pgn
17859 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  o);.  }.}../*.**
1785a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c   Free any overfl
1785b 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 61  ow pages associa
1785c 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76  ted with the giv
1785d 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74  en Cell..*/.stat
1785e 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c  ic int clearCell
1785f 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
17860 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
17861 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72  pCell){.  BtShar
17862 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d  ed *pBt = pPage-
17863 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f  >pBt;.  CellInfo
17864 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76   info;.  Pgno ov
17865 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63  flPgno;.  int rc
17866 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20  ;.  int nOvfl;. 
17867 20 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69 7a   u16 ovflPageSiz
17868 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
17869 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1786a 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
1786b 65 78 29 20 29 3b 0a 20 20 62 74 72 65 65 50 61  ex) );.  btreePa
1786c 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65  rseCellPtr(pPage
1786d 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b  , pCell, &info);
1786e 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65  .  if( info.iOve
1786f 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20  rflow==0 ){.    
17870 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
17871 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f  ;  /* No overflo
17872 77 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20  w pages. Return 
17873 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e  without doing an
17874 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20  ything */.  }.  
17875 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62  ovflPgno = get4b
17876 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e  yte(&pCell[info.
17877 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61  iOverflow]);.  a
17878 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62  ssert( pBt->usab
17879 6c 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20  leSize > 4 );.  
1787a 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70  ovflPageSize = p
1787b 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
1787c 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69   4;.  nOvfl = (i
1787d 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69  nfo.nPayload - i
1787e 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66  nfo.nLocal + ovf
1787f 6c 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f  lPageSize - 1)/o
17880 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61  vflPageSize;.  a
17881 73 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d  ssert( ovflPgno=
17882 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b  =0 || nOvfl>0 );
17883 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d  .  while( nOvfl-
17884 2d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e  - ){.    Pgno iN
17885 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d  ext = 0;.    Mem
17886 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b  Page *pOvfl = 0;
17887 0a 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e  .    if( ovflPgn
17888 6f 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e  o<2 || ovflPgno>
17889 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
1788a 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  Bt) ){.      /* 
1788b 30 20 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c  0 is not a legal
1788c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64   page number and
1788d 20 70 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62   page 1 cannot b
1788e 65 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f  e an .      ** o
1788f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68  verflow page. Th
17890 65 72 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50  erefore if ovflP
17891 67 6e 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68  gno<2 or past th
17892 65 20 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20  e end of the .  
17893 20 20 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20      ** file the 
17894 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65  database must be
17895 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20   corrupt. */.   
17896 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17897 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
17898 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76     }.    if( nOv
17899 66 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  fl ){.      rc =
1789a 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65   getOverflowPage
1789b 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20  (pBt, ovflPgno, 
1789c 26 70 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b  &pOvfl, &iNext);
1789d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
1789e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
1789f 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61  .    rc = freePa
178a0 67 65 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20  ge2(pBt, pOvfl, 
178a1 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69  ovflPgno);.    i
178a2 66 28 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20  f( pOvfl ){.    
178a3 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
178a4 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61  ref(pOvfl->pDbPa
178a5 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ge);.    }.    i
178a6 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
178a7 63 3b 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20  c;.    ovflPgno 
178a8 3d 20 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72  = iNext;.  }.  r
178a9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
178aa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
178ab 20 74 68 65 20 62 79 74 65 20 73 65 71 75 65 6e   the byte sequen
178ac 63 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65  ce used to repre
178ad 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70  sent a cell on p
178ae 61 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64  age pPage.** and
178af 20 77 72 69 74 65 20 74 68 61 74 20 62 79 74 65   write that byte
178b0 20 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70   sequence into p
178b1 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f  Cell[].  Overflo
178b2 77 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61  w pages are.** a
178b3 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c  llocated and fil
178b4 6c 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73  led in as necess
178b5 61 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  ary.  The callin
178b6 67 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69  g procedure.** i
178b7 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
178b8 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75  r making sure su
178b9 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 68  fficient space h
178ba 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  as been allocate
178bb 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d  d.** for pCell[]
178bc 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
178bd 74 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74  t pCell does not
178be 20 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 20   necessary need 
178bf 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
178c0 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20  pPage->aData.** 
178c1 61 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67  area.  pCell mig
178c2 68 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65  ht point to some
178c3 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61   temporary stora
178c4 67 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69  ge.  The cell wi
178c5 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75  ll.** be constru
178c6 63 74 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d  cted in this tem
178c7 70 6f 72 61 72 79 20 61 72 65 61 20 74 68 65 6e  porary area then
178c8 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61   copied into pPa
178c9 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74  ge->aData.** lat
178ca 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
178cb 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20  t fillInCell(.  
178cc 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
178cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
178ce 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20  * The page that 
178cf 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c  contains the cel
178d0 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  l */.  unsigned 
178d1 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20  char *pCell,    
178d2 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74        /* Complet
178d3 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 65  e text of the ce
178d4 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ll */.  const vo
178d5 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b  id *pKey, i64 nK
178d6 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65  ey,    /* The ke
178d7 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  y */.  const voi
178d8 64 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61  d *pData,int nDa
178d9 74 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74  ta,   /* The dat
178da 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f  a */.  int nZero
178db 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
178dc 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a        /* Extra z
178dd 65 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70  ero bytes to app
178de 65 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a  end to pData */.
178df 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20    int *pnSize   
178e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
178e1 20 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73   /* Write cell s
178e2 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ize here */.){. 
178e3 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20   int nPayload;. 
178e4 20 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b   const u8 *pSrc;
178e5 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20  .  int nSrc, n, 
178e6 72 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c  rc;.  int spaceL
178e7 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  eft;.  MemPage *
178e8 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d  pOvfl = 0;.  Mem
178e9 50 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65  Page *pToRelease
178ea 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64   = 0;.  unsigned
178eb 20 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20   char *pPrior;. 
178ec 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
178ed 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68  pPayload;.  BtSh
178ee 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67  ared *pBt = pPag
178ef 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70  e->pBt;.  Pgno p
178f0 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69  gnoOvfl = 0;.  i
178f1 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65  nt nHeader;.  Ce
178f2 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20  llInfo info;..  
178f3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
178f4 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
178f5 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
178f6 0a 0a 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20  ..  /* pPage is 
178f7 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20  not necessarily 
178f8 77 72 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20  writeable since 
178f9 70 43 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61  pCell might be a
178fa 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75  uxiliary.  ** bu
178fb 66 66 65 72 20 73 70 61 63 65 20 74 68 61 74 20  ffer space that 
178fc 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d  is separate from
178fd 20 74 68 65 20 70 50 61 67 65 20 62 75 66 66 65   the pPage buffe
178fe 72 20 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65  r area */.  asse
178ff 72 74 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d  rt( pCell<pPage-
17900 3e 61 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e  >aData || pCell>
17901 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  =&pPage->aData[p
17902 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20  Bt->pageSize].  
17903 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c            || sql
17904 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
17905 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
17906 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69  age) );..  /* Fi
17907 6c 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  ll in the header
17908 2e 20 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d  . */.  nHeader =
17909 20 30 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65   0;.  if( !pPage
1790a 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48  ->leaf ){.    nH
1790b 65 61 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a  eader += 4;.  }.
1790c 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73    if( pPage->has
1790d 44 61 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61  Data ){.    nHea
1790e 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74  der += putVarint
1790f 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d  (&pCell[nHeader]
17910 2c 20 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a  , nData+nZero);.
17911 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61    }else{.    nDa
17912 74 61 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a  ta = nZero = 0;.
17913 20 20 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d    }.  nHeader +=
17914 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c   putVarint(&pCel
17915 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36  l[nHeader], *(u6
17916 34 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72  4*)&nKey);.  btr
17917 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
17918 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e  Page, pCell, &in
17919 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  fo);.  assert( i
1791a 6e 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65  nfo.nHeader==nHe
1791b 61 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74  ader );.  assert
1791c 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65  ( info.nKey==nKe
1791d 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  y );.  assert( i
1791e 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29  nfo.nData==(u32)
1791f 28 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b  (nData+nZero) );
17920 0a 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e  .  .  /* Fill in
17921 20 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a   the payload */.
17922 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61    nPayload = nDa
17923 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66  ta + nZero;.  if
17924 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
17925 29 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44  ){.    pSrc = pD
17926 61 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20  ata;.    nSrc = 
17927 6e 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61  nData;.    nData
17928 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a   = 0;.  }else{ .
17929 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b      if( NEVER(nK
1792a 65 79 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c  ey>0x7fffffff ||
1792b 20 70 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20   pKey==0) ){.   
1792c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1792d 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1792e 20 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61     }.    nPayloa
1792f 64 20 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a  d += (int)nKey;.
17930 20 20 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b      pSrc = pKey;
17931 0a 20 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74  .    nSrc = (int
17932 29 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e  )nKey;.  }.  *pn
17933 53 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a  Size = info.nSiz
17934 65 3b 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d  e;.  spaceLeft =
17935 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20   info.nLocal;.  
17936 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c  pPayload = &pCel
17937 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50  l[nHeader];.  pP
17938 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e  rior = &pCell[in
17939 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a  fo.iOverflow];..
1793a 20 20 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61    while( nPayloa
1793b 64 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73  d>0 ){.    if( s
1793c 70 61 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23  paceLeft==0 ){.#
1793d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1793e 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1793f 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72      Pgno pgnoPtr
17940 6d 61 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20  map = pgnoOvfl; 
17941 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65  /* Overflow page
17942 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
17943 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20  ry page */.     
17944 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
17945 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  cuum ){.        
17946 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67  do{.          pg
17947 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20  noOvfl++;.      
17948 20 20 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20    } while( .    
17949 20 20 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50        PTRMAP_ISP
1794a 41 47 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66  AGE(pBt, pgnoOvf
1794b 6c 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d  l) || pgnoOvfl==
1794c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1794d 45 28 70 42 74 29 20 0a 20 20 20 20 20 20 20 20  E(pBt) .        
1794e 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
1794f 66 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c  f.      rc = all
17950 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
17951 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e  Bt, &pOvfl, &pgn
17952 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c  oOvfl, pgnoOvfl,
17953 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   0);.#ifndef SQL
17954 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
17955 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  UUM.      /* If 
17956 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70  the database sup
17957 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75  ports auto-vacuu
17958 6d 2c 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  m, and the secon
17959 64 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a  d or subsequent.
1795a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f        ** overflo
1795b 77 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20  w page is being 
1795c 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61  allocated, add a
1795d 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70  n entry to the p
1795e 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20  ointer-map.     
1795f 20 2a 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67   ** for that pag
17960 65 20 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a  e now. .      **
17961 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69  .      ** If thi
17962 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f  s is the first o
17963 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68  verflow page, th
17964 65 6e 20 77 72 69 74 65 20 61 20 70 61 72 74 69  en write a parti
17965 61 6c 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20  al entry .      
17966 2a 2a 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65  ** to the pointe
17967 72 2d 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69  r-map. If we wri
17968 74 65 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68  te nothing to th
17969 69 73 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73  is pointer-map s
1796a 6c 6f 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68  lot,.      ** th
1796b 65 6e 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69  en the optimisti
1796c 63 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e  c overflow chain
1796d 20 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63   processing in c
1796e 6c 65 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20  learCell().     
1796f 20 2a 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72   ** may misinter
17970 70 72 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69  pret the uniniti
17971 61 6c 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e  alised values an
17972 64 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20  d delete the.   
17973 20 20 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65     ** wrong page
17974 73 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  s from the datab
17975 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ase..      */.  
17976 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
17977 6f 56 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53  oVacuum && rc==S
17978 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17979 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 28      u8 eType = (
1797a 70 67 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41  pgnoPtrmap?PTRMA
1797b 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d  P_OVERFLOW2:PTRM
1797c 41 50 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20  AP_OVERFLOW1);. 
1797d 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74         ptrmapPut
1797e 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20  (pBt, pgnoOvfl, 
1797f 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61  eType, pgnoPtrma
17980 70 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20  p, &rc);.       
17981 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
17982 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
17983 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20  (pOvfl);.       
17984 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69   }.      }.#endi
17985 66 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  f.      if( rc )
17986 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  {.        releas
17987 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65  ePage(pToRelease
17988 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
17989 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  n rc;.      }.. 
1798a 20 20 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65       /* If pToRe
1798b 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72  lease is not zer
1798c 6f 20 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f  o than pPrior po
1798d 69 6e 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61  ints into the da
1798e 74 61 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a  ta area.      **
1798f 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20   of pToRelease. 
17990 20 4d 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65   Make sure pToRe
17991 6c 65 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77  lease is still w
17992 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20  riteable. */.   
17993 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65     assert( pToRe
17994 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69  lease==0 || sqli
17995 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
17996 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e  ble(pToRelease->
17997 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20  pDbPage) );..   
17998 20 20 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20     /* If pPrior 
17999 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  is part of the d
1799a 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67  ata area of pPag
1799b 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72  e, then make sur
1799c 65 20 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a  e pPage.      **
1799d 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61   is still writea
1799e 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  ble */.      ass
1799f 65 72 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67  ert( pPrior<pPag
179a0 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69  e->aData || pPri
179a1 6f 72 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74  or>=&pPage->aDat
179a2 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d  a[pBt->pageSize]
179a3 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
179a4 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
179a5 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
179a6 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20  DbPage) );..    
179a7 20 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f    put4byte(pPrio
179a8 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20  r, pgnoOvfl);.  
179a9 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
179aa 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20  pToRelease);.   
179ab 20 20 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20     pToRelease = 
179ac 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72  pOvfl;.      pPr
179ad 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61  ior = pOvfl->aDa
179ae 74 61 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79  ta;.      put4by
179af 74 65 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20  te(pPrior, 0);. 
179b0 20 20 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20       pPayload = 
179b1 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d  &pOvfl->aData[4]
179b2 3b 0a 20 20 20 20 20 20 73 70 61 63 65 4c 65 66  ;.      spaceLef
179b3 74 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  t = pBt->usableS
179b4 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20  ize - 4;.    }. 
179b5 20 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b     n = nPayload;
179b6 0a 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65  .    if( n>space
179b7 4c 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65  Left ) n = space
179b8 4c 65 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  Left;..    /* If
179b9 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e   pToRelease is n
179ba 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61  ot zero than pPa
179bb 79 6c 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74  yload points int
179bc 6f 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a  o the data area.
179bd 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c      ** of pToRel
179be 65 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65  ease.  Make sure
179bf 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73   pToRelease is s
179c0 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20  till writeable. 
179c1 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
179c2 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20  ToRelease==0 || 
179c3 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
179c4 69 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61  iteable(pToRelea
179c5 73 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  se->pDbPage) );.
179c6 0a 20 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c  .    /* If pPayl
179c7 6f 61 64 20 69 73 20 70 61 72 74 20 6f 66 20 74  oad is part of t
179c8 68 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20  he data area of 
179c9 70 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65  pPage, then make
179ca 20 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20   sure pPage.    
179cb 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74  ** is still writ
179cc 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73  eable */.    ass
179cd 65 72 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50  ert( pPayload<pP
179ce 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50  age->aData || pP
179cf 61 79 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e  ayload>=&pPage->
179d0 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53  aData[pBt->pageS
179d1 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20  ize].           
179d2 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72   || sqlite3Pager
179d3 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
179d4 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  e->pDbPage) );..
179d5 20 20 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29      if( nSrc>0 )
179d6 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53  {.      if( n>nS
179d7 72 63 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20  rc ) n = nSrc;. 
179d8 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72       assert( pSr
179d9 63 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  c );.      memcp
179da 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63  y(pPayload, pSrc
179db 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , n);.    }else{
179dc 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50  .      memset(pP
179dd 61 79 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20  ayload, 0, n);. 
179de 20 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61     }.    nPayloa
179df 64 20 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79  d -= n;.    pPay
179e0 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70  load += n;.    p
179e1 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53  Src += n;.    nS
179e2 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61  rc -= n;.    spa
179e3 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20  ceLeft -= n;.   
179e4 20 69 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a   if( nSrc==0 ){.
179e5 20 20 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61        nSrc = nDa
179e6 74 61 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d  ta;.      pSrc =
179e7 20 70 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20   pData;.    }.  
179e8 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  }.  releasePage(
179e9 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72  pToRelease);.  r
179ea 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
179eb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
179ec 20 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66   the i-th cell f
179ed 72 6f 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73  rom pPage.  This
179ee 20 72 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73   routine effects
179ef 20 70 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20   pPage only..** 
179f0 54 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  The cell content
179f1 20 69 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72   is not freed or
179f2 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49   deallocated.  I
179f3 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
179f4 74 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f  t.** the cell co
179f5 6e 74 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63  ntent has been c
179f6 6f 70 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20  opied someplace 
179f7 65 6c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74  else.  This rout
179f8 69 6e 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f  ine just.** remo
179f9 76 65 73 20 74 68 65 20 72 65 66 65 72 65 6e 63  ves the referenc
179fa 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72  e to the cell fr
179fb 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  om pPage..**.** 
179fc 22 73 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65  "sz" must be the
179fd 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
179fe 20 69 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f   in the cell..*/
179ff 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f  .static void dro
17a00 70 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70  pCell(MemPage *p
17a01 50 61 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69  Page, int idx, i
17a02 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29  nt sz, int *pRC)
17a03 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
17a04 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
17a05 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b  ter */.  int pc;
17a06 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
17a07 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65  et to cell conte
17a08 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67  nt of cell being
17a09 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38   deleted */.  u8
17a0a 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a   *data;       /*
17a0b 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f   pPage->aData */
17a0c 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20  .  u8 *ptr;     
17a0d 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f     /* Used to mo
17a0e 76 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20  ve bytes around 
17a0f 77 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f  within data[] */
17a10 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
17a11 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e     /* The return
17a12 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68   code */.  int h
17a13 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65  dr;        /* Be
17a14 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68  ginning of the h
17a15 65 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70  eader.  0 most p
17a16 61 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 20  ages.  100 page 
17a17 31 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43  1 */..  if( *pRC
17a18 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73   ) return;..  as
17a19 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20  sert( idx>=0 && 
17a1a 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  idx<pPage->nCell
17a1b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a   );.  assert( sz
17a1c 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65  ==cellSize(pPage
17a1d 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65  , idx) );.  asse
17a1e 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
17a1f 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
17a20 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
17a21 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17a22 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
17a23 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
17a24 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65  ;.  data = pPage
17a25 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d  ->aData;.  ptr =
17a26 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65   &data[pPage->ce
17a27 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78  llOffset + 2*idx
17a28 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79  ];.  pc = get2by
17a29 74 65 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d  te(ptr);.  hdr =
17a2a 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
17a2b 74 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  t;.  testcase( p
17a2c 63 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74  c==get2byte(&dat
17a2d 61 5b 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74  a[hdr+5]) );.  t
17a2e 65 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d  estcase( pc+sz==
17a2f 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
17a30 6c 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20  leSize );.  if( 
17a31 70 63 20 3c 20 67 65 74 32 62 79 74 65 28 26 64  pc < get2byte(&d
17a32 61 74 61 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70  ata[hdr+5]) || p
17a33 63 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42  c+sz > pPage->pB
17a34 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b  t->usableSize ){
17a35 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49  .    *pRC = SQLI
17a36 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
17a37 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
17a38 0a 20 20 72 63 20 3d 20 66 72 65 65 53 70 61 63  .  rc = freeSpac
17a39 65 28 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29  e(pPage, pc, sz)
17a3a 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
17a3b 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20    *pRC = rc;.   
17a3c 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66   return;.  }.  f
17a3d 6f 72 28 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50  or(i=idx+1; i<pP
17a3e 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c  age->nCell; i++,
17a3f 20 70 74 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74   ptr+=2){.    pt
17a40 72 5b 30 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20  r[0] = ptr[2];. 
17a41 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b     ptr[1] = ptr[
17a42 33 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d  3];.  }.  pPage-
17a43 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32  >nCell--;.  put2
17a44 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33  byte(&data[hdr+3
17a45 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29  ], pPage->nCell)
17a46 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  ;.  pPage->nFree
17a47 20 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   += 2;.}../*.** 
17a48 49 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c  Insert a new cel
17a49 6c 20 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65  l on pPage at ce
17a4a 6c 6c 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70  ll index "i".  p
17a4b 43 65 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74  Cell points to t
17a4c 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66  he.** content of
17a4d 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a   the cell..**.**
17a4e 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   If the cell con
17a4f 74 65 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e  tent will fit on
17a50 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20   the page, then 
17a51 70 75 74 20 69 74 20 74 68 65 72 65 2e 20 20 49  put it there.  I
17a52 66 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  f it.** will not
17a53 20 66 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20   fit, then make 
17a54 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65  a copy of the ce
17a55 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20  ll content into 
17a56 70 54 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d  pTemp if.** pTem
17a57 70 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20  p is not null.  
17a58 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54  Regardless of pT
17a59 65 6d 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20  emp, allocate a 
17a5a 6e 65 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20  new entry.** in 
17a5b 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61  pPage->aOvfl[] a
17a5c 6e 64 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74  nd make it point
17a5d 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   to the cell con
17a5e 74 65 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20  tent (either.** 
17a5f 69 6e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20  in pTemp or the 
17a60 6f 72 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20  original pCell) 
17a61 61 6e 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20  and also record 
17a62 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41  its index. .** A
17a63 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20  llocating a new 
17a64 65 6e 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e  entry in pPage->
17a65 61 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20  aCell[] implies 
17a66 74 68 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e  that .** pPage->
17a67 6e 4f 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63  nOverflow is inc
17a68 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  remented..**.** 
17a69 49 66 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d  If nSkip is non-
17a6a 7a 65 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  zero, then do no
17a6b 74 20 63 6f 70 79 20 74 68 65 20 66 69 72 73 74  t copy the first
17a6c 20 6e 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20   nSkip bytes of 
17a6d 74 68 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65  the.** cell. The
17a6e 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65   caller will ove
17a6f 72 77 72 69 74 65 20 74 68 65 6d 20 61 66 74 65  rwrite them afte
17a70 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  r this function 
17a71 72 65 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e  returns. If.** n
17a72 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  Skip is non-zero
17a73 2c 20 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79  , then pCell may
17a74 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e   not point to an
17a75 20 69 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20   invalid memory 
17a76 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75  location .** (bu
17a77 74 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73  t pCell+nSkip is
17a78 20 61 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a   always valid)..
17a79 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
17a7a 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d  nsertCell(.  Mem
17a7b 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f  Page *pPage,   /
17a7c 2a 20 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63  * Page into whic
17a7d 68 20 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67  h we are copying
17a7e 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20   */.  int i,    
17a7f 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63          /* New c
17a80 65 6c 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20  ell becomes the 
17a81 69 2d 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65  i-th cell of the
17a82 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70   page */.  u8 *p
17a83 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20  Cell,        /* 
17a84 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e  Content of the n
17a85 65 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74  ew cell */.  int
17a86 20 73 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f   sz,           /
17a87 2a 20 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65  * Bytes of conte
17a88 6e 74 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20  nt in pCell */. 
17a89 20 75 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20   u8 *pTemp,     
17a8a 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61     /* Temp stora
17a8b 67 65 20 73 70 61 63 65 20 66 6f 72 20 70 43 65  ge space for pCe
17a8c 6c 6c 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f  ll, if needed */
17a8d 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20  .  Pgno iChild, 
17a8e 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a       /* If non-z
17a8f 65 72 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72  ero, replace fir
17a90 73 74 20 34 20 62 79 74 65 73 20 77 69 74 68 20  st 4 bytes with 
17a91 74 68 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20  this value */.  
17a92 69 6e 74 20 2a 70 52 43 20 20 20 20 20 20 20 20  int *pRC        
17a93 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
17a94 69 74 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  ite return code 
17a95 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  from here */.){.
17a96 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20    int idx;      
17a97 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20      /* Where to 
17a98 77 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63  write new cell c
17a99 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d  ontent in data[]
17a9a 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20   */.  int j;    
17a9b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
17a9c 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
17a9d 20 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f   end;          /
17a9e 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73  * First byte pas
17a9f 74 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20  t the last cell 
17aa0 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b  pointer in data[
17aa1 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20  ] */.  int ins; 
17aa2 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
17aa3 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72  x in data[] wher
17aa4 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74  e new cell point
17aa5 65 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a  er is inserted *
17aa6 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73  /.  int cellOffs
17aa7 65 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73  et;   /* Address
17aa8 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70   of first cell p
17aa9 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d  ointer in data[]
17aaa 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20   */.  u8 *data; 
17aab 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
17aac 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68  ontent of the wh
17aad 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38  ole page */.  u8
17aae 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20   *ptr;          
17aaf 2f 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69  /* Used for movi
17ab0 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ng information a
17ab1 72 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20  round in data[] 
17ab2 2a 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20  */..  int nSkip 
17ab3 3d 20 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20  = (iChild ? 4 : 
17ab4 30 29 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20  0);..  if( *pRC 
17ab5 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73  ) return;..  ass
17ab6 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d  ert( i>=0 && i<=
17ab7 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61  pPage->nCell+pPa
17ab8 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b  ge->nOverflow );
17ab9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
17aba 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c  ->nCell<=MX_CELL
17abb 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20  (pPage->pBt) && 
17abc 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70  MX_CELL(pPage->p
17abd 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61  Bt)<=5460 );.  a
17abe 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f  ssert( pPage->nO
17abf 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69  verflow<=ArraySi
17ac0 7a 65 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29  ze(pPage->aOvfl)
17ac1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a   );.  assert( sz
17ac2 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  ==cellSizePtr(pP
17ac3 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20  age, pCell) );. 
17ac4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17ac5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
17ac6 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
17ac7 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e  ;.  if( pPage->n
17ac8 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32  Overflow || sz+2
17ac9 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b  >pPage->nFree ){
17aca 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 29  .    if( pTemp )
17acb 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  {.      memcpy(p
17acc 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c  Temp+nSkip, pCel
17acd 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69  l+nSkip, sz-nSki
17ace 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20  p);.      pCell 
17acf 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20  = pTemp;.    }. 
17ad0 20 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 7b     if( iChild ){
17ad1 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
17ad2 70 43 65 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a  pCell, iChild);.
17ad3 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50      }.    j = pP
17ad4 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b  age->nOverflow++
17ad5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c  ;.    assert( j<
17ad6 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61  (int)(sizeof(pPa
17ad7 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f  ge->aOvfl)/sizeo
17ad8 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30  f(pPage->aOvfl[0
17ad9 5d 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65  ])) );.    pPage
17ada 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c  ->aOvfl[j].pCell
17adb 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50   = pCell;.    pP
17adc 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64  age->aOvfl[j].id
17add 78 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 7d 65  x = (u16)i;.  }e
17ade 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  lse{.    int rc 
17adf 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
17ae0 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
17ae1 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
17ae2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17ae3 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20      *pRC = rc;. 
17ae4 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
17ae5 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73   }.    assert( s
17ae6 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
17ae7 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
17ae8 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 64 61  bPage) );.    da
17ae9 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
17aea 61 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65  a;.    cellOffse
17aeb 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f  t = pPage->cellO
17aec 66 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d  ffset;.    end =
17aed 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a   cellOffset + 2*
17aee 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20  pPage->nCell;.  
17aef 20 20 69 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73    ins = cellOffs
17af0 65 74 20 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63  et + 2*i;.    rc
17af1 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65   = allocateSpace
17af2 28 70 50 61 67 65 2c 20 73 7a 2c 20 26 69 64 78  (pPage, sz, &idx
17af3 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
17af4 20 2a 70 52 43 20 3d 20 72 63 3b 20 72 65 74 75   *pRC = rc; retu
17af5 72 6e 3b 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65  rn; }.    /* The
17af6 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 29   allocateSpace()
17af7 20 72 6f 75 74 69 6e 65 20 67 75 61 72 61 6e 74   routine guarant
17af8 65 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ees the followin
17af9 67 20 74 77 6f 20 70 72 6f 70 65 72 74 69 65 73  g two properties
17afa 0a 20 20 20 20 2a 2a 20 69 66 20 69 74 20 72 65  .    ** if it re
17afb 74 75 72 6e 73 20 73 75 63 63 65 73 73 20 2a 2f  turns success */
17afc 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78  .    assert( idx
17afd 20 3e 3d 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20   >= end+2 );.   
17afe 20 61 73 73 65 72 74 28 20 69 64 78 2b 73 7a 20   assert( idx+sz 
17aff 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  <= pPage->pBt->u
17b00 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20  sableSize );.   
17b01 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b   pPage->nCell++;
17b02 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  .    pPage->nFre
17b03 65 20 2d 3d 20 28 75 31 36 29 28 32 20 2b 20 73  e -= (u16)(2 + s
17b04 7a 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  z);.    memcpy(&
17b05 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c  data[idx+nSkip],
17b06 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a   pCell+nSkip, sz
17b07 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 69 66 28  -nSkip);.    if(
17b08 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20   iChild ){.     
17b09 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b   put4byte(&data[
17b0a 69 64 78 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20  idx], iChild);. 
17b0b 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 65     }.    for(j=e
17b0c 6e 64 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d  nd, ptr=&data[j]
17b0d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70  ; j>ins; j-=2, p
17b0e 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 74  tr-=2){.      pt
17b0f 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a  r[0] = ptr[-2];.
17b10 20 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70        ptr[1] = p
17b11 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20  tr[-1];.    }.  
17b12 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
17b13 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20  [ins], idx);.   
17b14 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
17b15 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
17b16 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c  +3], pPage->nCel
17b17 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  l);.#ifndef SQLI
17b18 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
17b19 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  UM.    if( pPage
17b1a 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ->pBt->autoVacuu
17b1b 6d 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  m ){.      /* Th
17b1c 65 20 63 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61  e cell may conta
17b1d 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  in a pointer to 
17b1e 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
17b1f 2e 20 49 66 20 73 6f 2c 20 77 72 69 74 65 0a 20  . If so, write. 
17b20 20 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72       ** the entr
17b21 79 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c  y for the overfl
17b22 6f 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65  ow page into the
17b23 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20   pointer map..  
17b24 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 74 72      */.      ptr
17b25 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50  mapPutOvflPtr(pP
17b26 61 67 65 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29  age, pCell, pRC)
17b27 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
17b28 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
17b29 61 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73 20  a list of cells 
17b2a 74 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65 20  to a page.  The 
17b2b 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 69  page should be i
17b2c 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a  nitially empty..
17b2d 2a 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72 65  ** The cells are
17b2e 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66   guaranteed to f
17b2f 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a  it on the page..
17b30 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
17b31 73 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d  ssemblePage(.  M
17b32 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20  emPage *pPage,  
17b33 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20   /* The page to 
17b34 62 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f  be assemblied */
17b35 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20  .  int nCell,   
17b36 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62       /* The numb
17b37 65 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61  er of cells to a
17b38 64 64 20 74 6f 20 74 68 69 73 20 70 61 67 65 20  dd to this page 
17b39 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c  */.  u8 **apCell
17b3a 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  ,      /* Pointe
17b3b 72 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65  rs to cell bodie
17b3c 73 20 2a 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a  s */.  u16 *aSiz
17b3d 65 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  e        /* Size
17b3e 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a  s of the cells *
17b3f 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
17b40 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
17b41 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38   counter */.  u8
17b42 20 2a 70 43 65 6c 6c 70 74 72 3b 20 20 20 20 20   *pCellptr;     
17b43 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65  /* Address of ne
17b44 78 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  xt cell pointer 
17b45 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64  */.  int cellbod
17b46 79 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  y;     /* Addres
17b47 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 62  s of next cell b
17b48 6f 64 79 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f  ody */.  u8 * co
17b49 6e 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65  nst data = pPage
17b4a 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 20 20 20  ->aData;        
17b4b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
17b4c 74 6f 20 64 61 74 61 20 66 6f 72 20 70 50 61 67  to data for pPag
17b4d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74  e */.  const int
17b4e 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64   hdr = pPage->hd
17b4f 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  rOffset;        
17b50 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20     /* Offset of 
17b51 68 65 61 64 65 72 20 6f 6e 20 70 50 61 67 65 20  header on pPage 
17b52 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e  */.  const int n
17b53 55 73 61 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e  Usable = pPage->
17b54 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
17b55 20 2f 2a 20 55 73 61 62 6c 65 20 73 69 7a 65 20   /* Usable size 
17b56 6f 66 20 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73  of page */..  as
17b57 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
17b58 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61  erflow==0 );.  a
17b59 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
17b5a 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
17b5b 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
17b5c 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e    assert( nCell>
17b5d 3d 30 20 26 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f  =0 && nCell<=MX_
17b5e 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29  CELL(pPage->pBt)
17b5f 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67   && MX_CELL(pPag
17b60 65 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b  e->pBt)<=5460 );
17b61 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17b62 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
17b63 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
17b64 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  e) );..  /* Chec
17b65 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  k that the page 
17b66 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 7a 65  has just been ze
17b67 72 6f 65 64 20 62 79 20 7a 65 72 6f 50 61 67 65  roed by zeroPage
17b68 28 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  () */.  assert( 
17b69 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20  pPage->nCell==0 
17b6a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 67 65 74  );.  assert( get
17b6b 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
17b6c 35 5d 29 3d 3d 6e 55 73 61 62 6c 65 20 29 3b 0a  5])==nUsable );.
17b6d 0a 20 20 70 43 65 6c 6c 70 74 72 20 3d 20 26 64  .  pCellptr = &d
17b6e 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f  ata[pPage->cellO
17b6f 66 66 73 65 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d  ffset + nCell*2]
17b70 3b 0a 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 6e  ;.  cellbody = n
17b71 55 73 61 62 6c 65 3b 0a 20 20 66 6f 72 28 69 3d  Usable;.  for(i=
17b72 6e 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69  nCell-1; i>=0; i
17b73 2d 2d 29 7b 0a 20 20 20 20 70 43 65 6c 6c 70 74  --){.    pCellpt
17b74 72 20 2d 3d 20 32 3b 0a 20 20 20 20 63 65 6c 6c  r -= 2;.    cell
17b75 62 6f 64 79 20 2d 3d 20 61 53 69 7a 65 5b 69 5d  body -= aSize[i]
17b76 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70  ;.    put2byte(p
17b77 43 65 6c 6c 70 74 72 2c 20 63 65 6c 6c 62 6f 64  Cellptr, cellbod
17b78 79 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  y);.    memcpy(&
17b79 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20  data[cellbody], 
17b7a 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65  apCell[i], aSize
17b7b 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 75 74 32  [i]);.  }.  put2
17b7c 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33  byte(&data[hdr+3
17b7d 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74  ], nCell);.  put
17b7e 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
17b7f 35 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20  5], cellbody);. 
17b80 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d   pPage->nFree -=
17b81 20 28 6e 43 65 6c 6c 2a 32 20 2b 20 6e 55 73 61   (nCell*2 + nUsa
17b82 62 6c 65 20 2d 20 63 65 6c 6c 62 6f 64 79 29 3b  ble - cellbody);
17b83 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  .  pPage->nCell 
17b84 3d 20 28 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a  = (u16)nCell;.}.
17b85 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
17b86 77 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20  wing parameters 
17b87 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61  determine how ma
17b88 6e 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65  ny adjacent page
17b89 73 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a  s get involved.*
17b8a 2a 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67  * in a balancing
17b8b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20   operation.  NN 
17b8c 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
17b8d 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69   neighbors on ei
17b8e 74 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20  ther side.** of 
17b8f 74 68 65 20 70 61 67 65 20 74 68 61 74 20 70 61  the page that pa
17b90 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65  rticipate in the
17b91 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61   balancing opera
17b92 74 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 68 65  tion.  NB is the
17b93 0a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  .** total number
17b94 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 70   of pages that p
17b95 61 72 74 69 63 69 70 61 74 65 2c 20 69 6e 63 6c  articipate, incl
17b96 75 64 69 6e 67 20 74 68 65 20 74 61 72 67 65 74  uding the target
17b97 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20   page and.** NN 
17b98 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74  neighbors on eit
17b99 68 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20  her side..**.** 
17b9a 54 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75  The minimum valu
17b9b 65 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 6f 66  e of NN is 1 (of
17b9c 20 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 72 65   course).  Incre
17b9d 61 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31  asing NN above 1
17b9e 0a 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 29 20  .** (to 2 or 3) 
17b9f 67 69 76 65 73 20 61 20 6d 6f 64 65 73 74 20 69  gives a modest i
17ba0 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45  mprovement in SE
17ba1 4c 45 43 54 20 61 6e 64 20 44 45 4c 45 54 45 20  LECT and DELETE 
17ba2 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69  performance.** i
17ba3 6e 20 65 78 63 68 61 6e 67 65 20 66 6f 72 20 61  n exchange for a
17ba4 20 6c 61 72 67 65 72 20 64 65 67 72 61 64 61 74   larger degradat
17ba5 69 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 61 6e  ion in INSERT an
17ba6 64 20 55 50 44 41 54 45 20 70 65 72 66 6f 72 6d  d UPDATE perform
17ba7 61 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c  ance..** The val
17ba8 75 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 72 73  ue of NN appears
17ba9 20 74 6f 20 67 69 76 65 20 74 68 65 20 62 65 73   to give the bes
17baa 74 20 72 65 73 75 6c 74 73 20 6f 76 65 72 61 6c  t results overal
17bab 6c 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e  l..*/.#define NN
17bac 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   1             /
17bad 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67  * Number of neig
17bae 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20  hbors on either 
17baf 73 69 64 65 20 6f 66 20 70 50 61 67 65 20 2a 2f  side of pPage */
17bb0 0a 23 64 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a  .#define NB (NN*
17bb1 32 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 6f 74  2+1)      /* Tot
17bb2 61 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65  al pages involve
17bb3 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 65  d in the balance
17bb4 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51   */...#ifndef SQ
17bb5 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42  LITE_OMIT_QUICKB
17bb6 41 4c 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69  ALANCE./*.** Thi
17bb7 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c  s version of bal
17bb8 61 6e 63 65 28 29 20 68 61 6e 64 6c 65 73 20 74  ance() handles t
17bb9 68 65 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61  he common specia
17bba 6c 20 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20  l case where.** 
17bbb 61 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 62  a new entry is b
17bbc 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6f 6e  eing inserted on
17bbd 20 74 68 65 20 65 78 74 72 65 6d 65 20 72 69 67   the extreme rig
17bbe 68 74 2d 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a  ht-end of the.**
17bbf 20 74 72 65 65 2c 20 69 6e 20 6f 74 68 65 72 20   tree, in other 
17bc0 77 6f 72 64 73 2c 20 77 68 65 6e 20 74 68 65 20  words, when the 
17bc1 6e 65 77 20 65 6e 74 72 79 20 77 69 6c 6c 20 62  new entry will b
17bc2 65 63 6f 6d 65 20 74 68 65 20 6c 61 72 67 65 73  ecome the larges
17bc3 74 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68  t.** entry in th
17bc4 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e  e tree..**.** In
17bc5 73 74 65 61 64 20 6f 66 20 74 72 79 69 6e 67 20  stead of trying 
17bc6 74 6f 20 62 61 6c 61 6e 63 65 20 74 68 65 20 33  to balance the 3
17bc7 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66   right-most leaf
17bc8 20 70 61 67 65 73 2c 20 6a 75 73 74 20 61 64 64   pages, just add
17bc9 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67 65 20 74  .** a new page t
17bca 6f 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  o the right-hand
17bcb 20 73 69 64 65 20 61 6e 64 20 70 75 74 20 74 68   side and put th
17bcc 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72 79 20  e one new entry 
17bcd 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 2e  in.** that page.
17bce 20 20 54 68 69 73 20 6c 65 61 76 65 73 20 74 68    This leaves th
17bcf 65 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 20  e right side of 
17bd0 74 68 65 20 74 72 65 65 20 73 6f 6d 65 77 68 61  the tree somewha
17bd1 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65 64 2e  t.** unbalanced.
17bd2 20 20 42 75 74 20 6f 64 64 73 20 61 72 65 20 74    But odds are t
17bd3 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 69  hat we will be i
17bd4 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 65 6e 74  nserting new ent
17bd5 72 69 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 65  ries.** at the e
17bd6 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72 77 61 72  nd soon afterwar
17bd7 64 73 20 73 6f 20 74 68 65 20 6e 65 61 72 6c 79  ds so the nearly
17bd8 20 65 6d 70 74 79 20 70 61 67 65 20 77 69 6c 6c   empty page will
17bd9 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c   quickly.** fill
17bda 20 75 70 2e 20 20 4f 6e 20 61 76 65 72 61 67 65   up.  On average
17bdb 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20 69 73  ..**.** pPage is
17bdc 20 74 68 65 20 6c 65 61 66 20 70 61 67 65 20 77   the leaf page w
17bdd 68 69 63 68 20 69 73 20 74 68 65 20 72 69 67 68  hich is the righ
17bde 74 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e 20 74  t-most page in t
17bdf 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50 61 72  he tree..** pPar
17be0 65 6e 74 20 69 73 20 69 74 73 20 70 61 72 65 6e  ent is its paren
17be1 74 2e 20 20 70 50 61 67 65 20 6d 75 73 74 20 68  t.  pPage must h
17be2 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f 76 65  ave a single ove
17be3 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a 20 77  rflow entry.** w
17be4 68 69 63 68 20 69 73 20 61 6c 73 6f 20 74 68 65  hich is also the
17be5 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72   right-most entr
17be6 79 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a  y on the page..*
17be7 2a 0a 2a 2a 20 54 68 65 20 70 53 70 61 63 65 20  *.** The pSpace 
17be8 62 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74  buffer is used t
17be9 6f 20 73 74 6f 72 65 20 61 20 74 65 6d 70 6f 72  o store a tempor
17bea 61 72 79 20 63 6f 70 79 20 6f 66 20 74 68 65 20  ary copy of the 
17beb 64 69 76 69 64 65 72 0a 2a 2a 20 63 65 6c 6c 20  divider.** cell 
17bec 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 73  that will be ins
17bed 65 72 74 65 64 20 69 6e 74 6f 20 70 50 61 72 65  erted into pPare
17bee 6e 74 2e 20 53 75 63 68 20 61 20 63 65 6c 6c 20  nt. Such a cell 
17bef 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 0a  consists of a 4.
17bf0 2a 2a 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d  ** byte page num
17bf1 62 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  ber followed by 
17bf2 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74  a variable lengt
17bf3 68 20 69 6e 74 65 67 65 72 2e 20 49 6e 20 6f 74  h integer. In ot
17bf4 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 74  her.** words, at
17bf5 20 6d 6f 73 74 20 31 33 20 62 79 74 65 73 2e 20   most 13 bytes. 
17bf6 48 65 6e 63 65 20 74 68 65 20 70 53 70 61 63 65  Hence the pSpace
17bf7 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20   buffer must be 
17bf8 61 74 0a 2a 2a 20 6c 65 61 73 74 20 31 33 20 62  at.** least 13 b
17bf9 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f  ytes in size..*/
17bfa 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61  .static int bala
17bfb 6e 63 65 5f 71 75 69 63 6b 28 4d 65 6d 50 61 67  nce_quick(MemPag
17bfc 65 20 2a 70 50 61 72 65 6e 74 2c 20 4d 65 6d 50  e *pParent, MemP
17bfd 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a  age *pPage, u8 *
17bfe 70 53 70 61 63 65 29 7b 0a 20 20 42 74 53 68 61  pSpace){.  BtSha
17bff 72 65 64 20 2a 63 6f 6e 73 74 20 70 42 74 20 3d  red *const pBt =
17c00 20 70 50 61 67 65 2d 3e 70 42 74 3b 20 20 20 20   pPage->pBt;    
17c01 2f 2a 20 42 2d 54 72 65 65 20 44 61 74 61 62 61  /* B-Tree Databa
17c02 73 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  se */.  MemPage 
17c03 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20  *pNew;          
17c04 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17c05 4e 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  Newly allocated 
17c06 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  page */.  int rc
17c07 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
17c08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17c09 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f  * Return Code */
17c0a 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b  .  Pgno pgnoNew;
17c0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c0c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
17c0d 6e 75 6d 62 65 72 20 6f 66 20 70 4e 65 77 20 2a  number of pNew *
17c0e 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  /..  assert( sql
17c0f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17c10 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
17c11 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
17c12 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
17c13 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d  iteable(pParent-
17c14 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61  >pDbPage) );.  a
17c15 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f  ssert( pPage->nO
17c16 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20  verflow==1 );.. 
17c17 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c   if( pPage->nCel
17c18 6c 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  l<=0 ) return SQ
17c19 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
17c1a 54 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  T;..  /* Allocat
17c1b 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 54 68  e a new page. Th
17c1c 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63  is page will bec
17c1d 6f 6d 65 20 74 68 65 20 72 69 67 68 74 2d 73 69  ome the right-si
17c1e 62 6c 69 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70  bling of .  ** p
17c1f 50 61 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 70  Page. Make the p
17c20 61 72 65 6e 74 20 70 61 67 65 20 77 72 69 74 61  arent page writa
17c21 62 6c 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65  ble, so that the
17c22 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c   new divider cel
17c23 6c 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e  l.  ** may be in
17c24 73 65 72 74 65 64 2e 20 49 66 20 62 6f 74 68 20  serted. If both 
17c25 74 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73  these operations
17c26 20 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 2c   are successful,
17c27 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20   proceed..  */. 
17c28 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74   rc = allocateBt
17c29 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e  reePage(pBt, &pN
17c2a 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c  ew, &pgnoNew, 0,
17c2b 20 30 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d   0);..  if( rc==
17c2c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20  SQLITE_OK ){..  
17c2d 20 20 75 38 20 2a 70 4f 75 74 20 3d 20 26 70 53    u8 *pOut = &pS
17c2e 70 61 63 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20  pace[4];.    u8 
17c2f 2a 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e  *pCell = pPage->
17c30 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a  aOvfl[0].pCell;.
17c31 20 20 20 20 75 31 36 20 73 7a 43 65 6c 6c 20 3d      u16 szCell =
17c32 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
17c33 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  ge, pCell);.    
17c34 75 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 20  u8 *pStop;..    
17c35 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
17c36 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
17c37 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 20 29  pNew->pDbPage) )
17c38 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
17c39 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28  age->aData[0]==(
17c3a 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c  PTF_INTKEY|PTF_L
17c3b 45 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 46  EAFDATA|PTF_LEAF
17c3c 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67  ) );.    zeroPag
17c3d 65 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b  e(pNew, PTF_INTK
17c3e 45 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c  EY|PTF_LEAFDATA|
17c3f 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 61  PTF_LEAF);.    a
17c40 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77  ssemblePage(pNew
17c41 2c 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a  , 1, &pCell, &sz
17c42 43 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  Cell);..    /* I
17c43 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
17c44 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
17c45 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f  e, update the po
17c46 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a  inter map.    **
17c47 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f   with entries fo
17c48 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20  r the new page, 
17c49 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20  and any pointer 
17c4a 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a  from the .    **
17c4b 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67   cell on the pag
17c4c 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77  e to an overflow
17c4d 20 70 61 67 65 2e 20 49 66 20 65 69 74 68 65 72   page. If either
17c4e 20 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a   of these.    **
17c4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 61 69 6c   operations fail
17c50 73 2c 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f  s, the return co
17c51 64 65 20 69 73 20 73 65 74 2c 20 62 75 74 20 74  de is set, but t
17c52 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20  he contents.    
17c53 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74  ** of the parent
17c54 20 70 61 67 65 20 61 72 65 20 73 74 69 6c 6c 20   page are still 
17c55 6d 61 6e 69 70 75 6c 61 74 65 64 20 62 79 20 74  manipulated by t
17c56 68 68 20 63 6f 64 65 20 62 65 6c 6f 77 2e 0a 20  hh code below.. 
17c57 20 20 20 2a 2a 20 54 68 61 74 20 69 73 20 4f 6b     ** That is Ok
17c58 2c 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20  , at this point 
17c59 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
17c5a 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
17c5b 0a 20 20 20 20 2a 2a 20 62 65 20 6d 61 72 6b 65  .    ** be marke
17c5c 64 20 61 73 20 64 69 72 74 79 2e 20 52 65 74 75  d as dirty. Retu
17c5d 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63  rning an error c
17c5e 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 61  ode will cause a
17c5f 0a 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b  .    ** rollback
17c60 2c 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 63 68  , undoing any ch
17c61 61 6e 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68  anges made to th
17c62 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20  e parent page.. 
17c63 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53     */.    if( IS
17c64 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20  AUTOVACUUM ){.  
17c65 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42      ptrmapPut(pB
17c66 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d  t, pgnoNew, PTRM
17c67 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e  AP_BTREE, pParen
17c68 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20  t->pgno, &rc);. 
17c69 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 3e       if( szCell>
17c6a 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29  pNew->minLocal )
17c6b 7b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70  {.        ptrmap
17c6c 50 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c  PutOvflPtr(pNew,
17c6d 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20   pCell, &rc);.  
17c6e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
17c6f 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 64     /* Create a d
17c70 69 76 69 64 65 72 20 63 65 6c 6c 20 74 6f 20 69  ivider cell to i
17c71 6e 73 65 72 74 20 69 6e 74 6f 20 70 50 61 72 65  nsert into pPare
17c72 6e 74 2e 20 54 68 65 20 64 69 76 69 64 65 72 20  nt. The divider 
17c73 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f 6e 73  cell.    ** cons
17c74 69 73 74 73 20 6f 66 20 61 20 34 2d 62 79 74 65  ists of a 4-byte
17c75 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 74 68   page number (th
17c76 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  e page number of
17c77 20 70 50 61 67 65 29 20 61 6e 64 0a 20 20 20 20   pPage) and.    
17c78 2a 2a 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65  ** a variable le
17c79 6e 67 74 68 20 6b 65 79 20 76 61 6c 75 65 20 28  ngth key value (
17c7a 77 68 69 63 68 20 6d 75 73 74 20 62 65 20 74 68  which must be th
17c7b 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20  e same value as 
17c7c 74 68 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65  the.    ** large
17c7d 73 74 20 6b 65 79 20 6f 6e 20 70 50 61 67 65 29  st key on pPage)
17c7e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
17c7f 54 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67  To find the larg
17c80 65 73 74 20 6b 65 79 20 76 61 6c 75 65 20 6f 6e  est key value on
17c81 20 70 50 61 67 65 2c 20 66 69 72 73 74 20 66 69   pPage, first fi
17c82 6e 64 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  nd the right-mos
17c83 74 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f  t .    ** cell o
17c84 6e 20 70 50 61 67 65 2e 20 54 68 65 20 66 69 72  n pPage. The fir
17c85 73 74 20 74 77 6f 20 66 69 65 6c 64 73 20 6f 66  st two fields of
17c86 20 74 68 69 73 20 63 65 6c 6c 20 61 72 65 20 74   this cell are t
17c87 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72  he .    ** recor
17c88 64 2d 6c 65 6e 67 74 68 20 28 61 20 76 61 72 69  d-length (a vari
17c89 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65  able length inte
17c8a 67 65 72 20 61 74 20 6d 6f 73 74 20 33 32 2d 62  ger at most 32-b
17c8b 69 74 73 20 69 6e 20 73 69 7a 65 29 0a 20 20 20  its in size).   
17c8c 20 2a 2a 20 61 6e 64 20 74 68 65 20 6b 65 79 20   ** and the key 
17c8d 76 61 6c 75 65 20 28 61 20 76 61 72 69 61 62 6c  value (a variabl
17c8e 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e length integer
17c8f 2c 20 6d 61 79 20 68 61 76 65 20 61 6e 79 20 76  , may have any v
17c90 61 6c 75 65 29 2e 0a 20 20 20 20 2a 2a 20 54 68  alue)..    ** Th
17c91 65 20 66 69 72 73 74 20 6f 66 20 74 68 65 20 77  e first of the w
17c92 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20  hile(...) loops 
17c93 62 65 6c 6f 77 20 73 6b 69 70 73 20 6f 76 65 72  below skips over
17c94 20 74 68 65 20 72 65 63 6f 72 64 2d 6c 65 6e 67   the record-leng
17c95 74 68 0a 20 20 20 20 2a 2a 20 66 69 65 6c 64 2e  th.    ** field.
17c96 20 54 68 65 20 73 65 63 6f 6e 64 20 77 68 69 6c   The second whil
17c97 65 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 6f 70 69  e(...) loop copi
17c98 65 73 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  es the key value
17c99 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 2a 2a   from the.    **
17c9a 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 69   cell on pPage i
17c9b 6e 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62  nto the pSpace b
17c9c 75 66 66 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  uffer..    */.  
17c9d 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
17c9e 6c 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d  ll(pPage, pPage-
17c9f 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 70  >nCell-1);.    p
17ca0 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d  Stop = &pCell[9]
17ca1 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28  ;.    while( (*(
17ca2 70 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26  pCell++)&0x80) &
17ca3 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b  & pCell<pStop );
17ca4 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70 43  .    pStop = &pC
17ca5 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c  ell[9];.    whil
17ca6 65 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29 20 3d  e( ((*(pOut++) =
17ca7 20 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30 78 38   *(pCell++))&0x8
17ca8 30 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f  0) && pCell<pSto
17ca9 70 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73  p );..    /* Ins
17caa 65 72 74 20 74 68 65 20 6e 65 77 20 64 69 76 69  ert the new divi
17cab 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 70 50  der cell into pP
17cac 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 6e  arent. */.    in
17cad 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74  sertCell(pParent
17cae 2c 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  , pParent->nCell
17caf 2c 20 70 53 70 61 63 65 2c 20 28 69 6e 74 29 28  , pSpace, (int)(
17cb0 70 4f 75 74 2d 70 53 70 61 63 65 29 2c 0a 20 20  pOut-pSpace),.  
17cb1 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
17cb2 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 26 72 63  pPage->pgno, &rc
17cb3 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74  );..    /* Set t
17cb4 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70  he right-child p
17cb5 6f 69 6e 74 65 72 20 6f 66 20 70 50 61 72 65 6e  ointer of pParen
17cb6 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
17cb7 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20  e new page. */. 
17cb8 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61     put4byte(&pPa
17cb9 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72  rent->aData[pPar
17cba 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  ent->hdrOffset+8
17cbb 5d 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a  ], pgnoNew);.  .
17cbc 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74      /* Release t
17cbd 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  he reference to 
17cbe 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f  the new page. */
17cbf 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
17cc0 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72  (pNew);.  }..  r
17cc1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
17cc2 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
17cc3 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a  T_QUICKBALANCE *
17cc4 2f 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54  /..#if 0./*.** T
17cc5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  his function doe
17cc6 73 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75 74 65  s not contribute
17cc7 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65   anything to the
17cc8 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 53 51   operation of SQ
17cc9 4c 69 74 65 2e 0a 2a 2a 20 69 74 20 69 73 20 73  Lite..** it is s
17cca 6f 6d 65 74 69 6d 65 73 20 61 63 74 69 76 61 74  ometimes activat
17ccb 65 64 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 77  ed temporarily w
17ccc 68 69 6c 65 20 64 65 62 75 67 67 69 6e 67 20 63  hile debugging c
17ccd 6f 64 65 20 72 65 73 70 6f 6e 73 69 62 6c 65 20  ode responsible 
17cce 0a 2a 2a 20 66 6f 72 20 73 65 74 74 69 6e 67 20  .** for setting 
17ccf 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
17cd0 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ies..*/.static i
17cd1 6e 74 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61  nt ptrmapCheckPa
17cd2 67 65 73 28 4d 65 6d 50 61 67 65 20 2a 2a 61 70  ges(MemPage **ap
17cd3 50 61 67 65 2c 20 69 6e 74 20 6e 50 61 67 65 29  Page, int nPage)
17cd4 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  {.  int i, j;.  
17cd5 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 61 67 65  for(i=0; i<nPage
17cd6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 6e 6f  ; i++){.    Pgno
17cd7 20 6e 3b 0a 20 20 20 20 75 38 20 65 3b 0a 20 20   n;.    u8 e;.  
17cd8 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
17cd9 20 3d 20 61 70 50 61 67 65 5b 69 5d 3b 0a 20 20   = apPage[i];.  
17cda 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
17cdb 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
17cdc 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
17cdd 3e 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20 20 20  >isInit );..    
17cde 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61 67 65  for(j=0; j<pPage
17cdf 2d 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20  ->nCell; j++){. 
17ce0 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e       CellInfo in
17ce1 66 6f 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 3b  fo;.      u8 *z;
17ce2 0a 20 20 20 20 20 0a 20 20 20 20 20 20 7a 20 3d  .     .      z =
17ce3 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
17ce4 20 6a 29 3b 0a 20 20 20 20 20 20 62 74 72 65 65   j);.      btree
17ce5 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61  ParseCellPtr(pPa
17ce6 67 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20  ge, z, &info);. 
17ce7 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f       if( info.iO
17ce8 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20  verflow ){.     
17ce9 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67     Pgno ovfl = g
17cea 65 74 34 62 79 74 65 28 26 7a 5b 69 6e 66 6f 2e  et4byte(&z[info.
17ceb 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20  iOverflow]);.   
17cec 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70       ptrmapGet(p
17ced 42 74 2c 20 6f 76 66 6c 2c 20 26 65 2c 20 26 6e  Bt, ovfl, &e, &n
17cee 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
17cef 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e  t( n==pPage->pgn
17cf0 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 4f  o && e==PTRMAP_O
17cf1 56 45 52 46 4c 4f 57 31 20 29 3b 0a 20 20 20 20  VERFLOW1 );.    
17cf2 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70    }.      if( !p
17cf3 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
17cf4 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64        Pgno child
17cf5 20 3d 20 67 65 74 34 62 79 74 65 28 7a 29 3b 0a   = get4byte(z);.
17cf6 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65          ptrmapGe
17cf7 74 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65  t(pBt, child, &e
17cf8 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61  , &n);.        a
17cf9 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d  ssert( n==pPage-
17cfa 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d  >pgno && e==PTRM
17cfb 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20  AP_BTREE );.    
17cfc 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
17cfd 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
17cfe 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69  {.      Pgno chi
17cff 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  ld = get4byte(&p
17d00 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
17d01 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
17d02 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 47 65  ;.      ptrmapGe
17d03 74 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65  t(pBt, child, &e
17d04 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 61 73 73  , &n);.      ass
17d05 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70  ert( n==pPage->p
17d06 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50  gno && e==PTRMAP
17d07 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20 7d 0a  _BTREE );.    }.
17d08 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
17d09 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
17d0a 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
17d0b 20 75 73 65 64 20 74 6f 20 63 6f 70 79 20 74 68   used to copy th
17d0c 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
17d0d 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 73 74  e b-tree node st
17d0e 6f 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 61 67 65  ored .** on page
17d0f 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70   pFrom to page p
17d10 54 6f 2e 20 49 66 20 70 61 67 65 20 70 46 72 6f  To. If page pFro
17d11 6d 20 77 61 73 20 6e 6f 74 20 61 20 6c 65 61 66  m was not a leaf
17d12 20 70 61 67 65 2c 20 74 68 65 6e 0a 2a 2a 20 74   page, then.** t
17d13 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
17d14 6e 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20  ntries for each 
17d15 63 68 69 6c 64 20 70 61 67 65 20 61 72 65 20 75  child page are u
17d16 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74  pdated so that t
17d17 68 65 0a 2a 2a 20 70 61 72 65 6e 74 20 70 61 67  he.** parent pag
17d18 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
17d19 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 70  pointer map is p
17d1a 61 67 65 20 70 54 6f 2e 20 49 66 20 70 46 72 6f  age pTo. If pFro
17d1b 6d 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 61  m contained.** a
17d1c 6e 79 20 63 65 6c 6c 73 20 77 69 74 68 20 6f 76  ny cells with ov
17d1d 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e  erflow page poin
17d1e 74 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 63  ters, then the c
17d1f 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 69  orresponding poi
17d20 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72  nter.** map entr
17d21 69 65 73 20 61 72 65 20 61 6c 73 6f 20 75 70 64  ies are also upd
17d22 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  ated so that the
17d23 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20   parent page is 
17d24 70 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20  page pTo..**.** 
17d25 49 66 20 70 46 72 6f 6d 20 69 73 20 63 75 72 72  If pFrom is curr
17d26 65 6e 74 6c 79 20 63 61 72 72 79 69 6e 67 20 61  ently carrying a
17d27 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  ny overflow cell
17d28 73 20 28 65 6e 74 72 69 65 73 20 69 6e 20 74 68  s (entries in th
17d29 65 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 4f 76  e.** MemPage.aOv
17d2a 66 6c 5b 5d 20 61 72 72 61 79 29 2c 20 74 68 65  fl[] array), the
17d2b 79 20 61 72 65 20 6e 6f 74 20 63 6f 70 69 65 64  y are not copied
17d2c 20 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20   to pTo. .**.** 
17d2d 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  Before returning
17d2e 2c 20 70 61 67 65 20 70 54 6f 20 69 73 20 72 65  , page pTo is re
17d2f 69 6e 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e  initialized usin
17d30 67 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  g btreeInitPage(
17d31 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72  )..**.** The per
17d32 66 6f 72 6d 61 6e 63 65 20 6f 66 20 74 68 69 73  formance of this
17d33 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74   function is not
17d34 20 63 72 69 74 69 63 61 6c 2e 20 49 74 20 69 73   critical. It is
17d35 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 0a 2a   only used by .*
17d36 2a 20 74 68 65 20 62 61 6c 61 6e 63 65 5f 73 68  * the balance_sh
17d37 61 6c 6c 6f 77 65 72 28 29 20 61 6e 64 20 62 61  allower() and ba
17d38 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 70  lance_deeper() p
17d39 72 6f 63 65 64 75 72 65 73 2c 20 6e 65 69 74 68  rocedures, neith
17d3a 65 72 20 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61  er of.** which a
17d3b 72 65 20 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20  re called often 
17d3c 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72  under normal cir
17d3d 63 75 6d 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73  cumstances..*/.s
17d3e 74 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 4e  tatic void copyN
17d3f 6f 64 65 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61  odeContent(MemPa
17d40 67 65 20 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61  ge *pFrom, MemPa
17d41 67 65 20 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52  ge *pTo, int *pR
17d42 43 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29  C){.  if( (*pRC)
17d43 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
17d44 20 20 20 42 74 53 68 61 72 65 64 20 2a 20 63 6f     BtShared * co
17d45 6e 73 74 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d  nst pBt = pFrom-
17d46 3e 70 42 74 3b 0a 20 20 20 20 75 38 20 2a 20 63  >pBt;.    u8 * c
17d47 6f 6e 73 74 20 61 46 72 6f 6d 20 3d 20 70 46 72  onst aFrom = pFr
17d48 6f 6d 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 75  om->aData;.    u
17d49 38 20 2a 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20  8 * const aTo = 
17d4a 70 54 6f 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  pTo->aData;.    
17d4b 69 6e 74 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48  int const iFromH
17d4c 64 72 20 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f  dr = pFrom->hdrO
17d4d 66 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 63  ffset;.    int c
17d4e 6f 6e 73 74 20 69 54 6f 48 64 72 20 3d 20 28 28  onst iToHdr = ((
17d4f 70 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20  pTo->pgno==1) ? 
17d50 31 30 30 20 3a 20 30 29 3b 0a 20 20 20 20 54 45  100 : 0);.    TE
17d51 53 54 4f 4e 4c 59 28 69 6e 74 20 72 63 3b 29 0a  STONLY(int rc;).
17d52 20 20 20 20 69 6e 74 20 69 44 61 74 61 3b 0a 20      int iData;. 
17d53 20 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28   .  .    assert(
17d54 20 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29   pFrom->isInit )
17d55 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  ;.    assert( pF
17d56 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48  rom->nFree>=iToH
17d57 64 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  dr );.    assert
17d58 28 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f  ( get2byte(&aFro
17d59 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d  m[iFromHdr+5])<=
17d5a 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
17d5b 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70  );.  .    /* Cop
17d5c 79 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64  y the b-tree nod
17d5d 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70  e content from p
17d5e 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67  age pFrom to pag
17d5f 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44  e pTo. */.    iD
17d60 61 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 26  ata = get2byte(&
17d61 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35  aFrom[iFromHdr+5
17d62 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  ]);.    memcpy(&
17d63 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 72  aTo[iData], &aFr
17d64 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e  om[iData], pBt->
17d65 75 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 61  usableSize-iData
17d66 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61  );.    memcpy(&a
17d67 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 72  To[iToHdr], &aFr
17d68 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46  om[iFromHdr], pF
17d69 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  rom->cellOffset 
17d6a 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c  + 2*pFrom->nCell
17d6b 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 69  );.  .    /* Rei
17d6c 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70  nitialize page p
17d6d 54 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  To so that the c
17d6e 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d  ontents of the M
17d6f 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65  emPage structure
17d70 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68  .    ** match th
17d71 65 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20  e new data. The 
17d72 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f  initialization o
17d73 66 20 70 54 6f 20 22 63 61 6e 6e 6f 74 22 20 66  f pTo "cannot" f
17d74 61 69 6c 2c 20 61 73 20 74 68 65 0a 20 20 20 20  ail, as the.    
17d75 2a 2a 20 64 61 74 61 20 63 6f 70 69 65 64 20 66  ** data copied f
17d76 72 6f 6d 20 70 46 72 6f 6d 20 69 73 20 6b 6e 6f  rom pFrom is kno
17d77 77 6e 20 74 6f 20 62 65 20 76 61 6c 69 64 2e 20  wn to be valid. 
17d78 20 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e 69 73 49   */.    pTo->isI
17d79 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 54 45 53  nit = 0;.    TES
17d7a 54 4f 4e 4c 59 28 72 63 20 3d 20 29 20 62 74 72  TONLY(rc = ) btr
17d7b 65 65 49 6e 69 74 50 61 67 65 28 70 54 6f 29 3b  eeInitPage(pTo);
17d7c 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
17d7d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
17d7e 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
17d7f 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  is an auto-vacuu
17d80 6d 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61  m database, upda
17d81 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  te the pointer-m
17d82 61 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a  ap entries.    *
17d83 2a 20 66 6f 72 20 61 6e 79 20 62 2d 74 72 65 65  * for any b-tree
17d84 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   or overflow pag
17d85 65 73 20 74 68 61 74 20 70 54 6f 20 6e 6f 77 20  es that pTo now 
17d86 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69  contains the poi
17d87 6e 74 65 72 73 20 74 6f 2e 0a 20 20 20 20 2a 2f  nters to..    */
17d88 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56  .    if( ISAUTOV
17d89 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 2a  ACUUM ){.      *
17d8a 70 52 43 20 3d 20 73 65 74 43 68 69 6c 64 50 74  pRC = setChildPt
17d8b 72 6d 61 70 73 28 70 54 6f 29 3b 0a 20 20 20 20  rmaps(pTo);.    
17d8c 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  }.  }.}../*.** T
17d8d 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 64 69  his routine redi
17d8e 73 74 72 69 62 75 74 65 73 20 63 65 6c 6c 73 20  stributes cells 
17d8f 6f 6e 20 74 68 65 20 69 50 61 72 65 6e 74 49 64  on the iParentId
17d90 78 27 74 68 20 63 68 69 6c 64 20 6f 66 20 70 50  x'th child of pP
17d91 61 72 65 6e 74 0a 2a 2a 20 28 68 65 72 65 61 66  arent.** (hereaf
17d92 74 65 72 20 22 74 68 65 20 70 61 67 65 22 29 20  ter "the page") 
17d93 61 6e 64 20 75 70 20 74 6f 20 32 20 73 69 62 6c  and up to 2 sibl
17d94 69 6e 67 73 20 73 6f 20 74 68 61 74 20 61 6c 6c  ings so that all
17d95 20 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75   pages have abou
17d96 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 6d  t the.** same am
17d97 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61  ount of free spa
17d98 63 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 73 69  ce. Usually a si
17d99 6e 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20  ngle sibling on 
17d9a 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74  either side of t
17d9b 68 65 0a 2a 2a 20 70 61 67 65 20 61 72 65 20 75  he.** page are u
17d9c 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e  sed in the balan
17d9d 63 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 6f 74  cing, though bot
17d9e 68 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 68 74  h siblings might
17d9f 20 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a   come from one.*
17da0 2a 20 73 69 64 65 20 69 66 20 74 68 65 20 70 61  * side if the pa
17da1 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20  ge is the first 
17da2 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f 66  or last child of
17da3 20 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20   its parent. If 
17da4 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 68 61 73  the page .** has
17da5 20 66 65 77 65 72 20 74 68 61 6e 20 32 20 73 69   fewer than 2 si
17da6 62 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68 69 6e  blings (somethin
17da7 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79  g which can only
17da8 20 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 70   happen if the p
17da9 61 67 65 0a 2a 2a 20 69 73 20 61 20 72 6f 6f 74  age.** is a root
17daa 20 70 61 67 65 20 6f 72 20 61 20 63 68 69 6c 64   page or a child
17dab 20 6f 66 20 61 20 72 6f 6f 74 20 70 61 67 65 29   of a root page)
17dac 20 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61   then all availa
17dad 62 6c 65 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20  ble siblings.** 
17dae 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74  participate in t
17daf 68 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a  he balancing..**
17db0 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  .** The number o
17db1 66 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 74 68  f siblings of th
17db2 65 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20  e page might be 
17db3 69 6e 63 72 65 61 73 65 64 20 6f 72 20 64 65 63  increased or dec
17db4 72 65 61 73 65 64 20 62 79 20 0a 2a 2a 20 6f 6e  reased by .** on
17db5 65 20 6f 72 20 74 77 6f 20 69 6e 20 61 6e 20 65  e or two in an e
17db6 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 70 61  ffort to keep pa
17db7 67 65 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20  ges nearly full 
17db8 62 75 74 20 6e 6f 74 20 6f 76 65 72 20 66 75 6c  but not over ful
17db9 6c 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  l. .**.** Note t
17dba 68 61 74 20 77 68 65 6e 20 74 68 69 73 20 72 6f  hat when this ro
17dbb 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
17dbc 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 65 6c   some of the cel
17dbd 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a  ls on the page.*
17dbe 2a 20 6d 69 67 68 74 20 6e 6f 74 20 61 63 74 75  * might not actu
17dbf 61 6c 6c 79 20 62 65 20 73 74 6f 72 65 64 20 69  ally be stored i
17dc0 6e 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b  n MemPage.aData[
17dc1 5d 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70  ]. This can happ
17dc2 65 6e 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67  en.** if the pag
17dc3 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 54  e is overfull. T
17dc4 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75  his routine ensu
17dc5 72 65 73 20 74 68 61 74 20 61 6c 6c 20 63 65 6c  res that all cel
17dc6 6c 73 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20  ls allocated.** 
17dc7 74 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64 20  to the page and 
17dc8 69 74 73 20 73 69 62 6c 69 6e 67 73 20 66 69 74  its siblings fit
17dc9 20 69 6e 74 6f 20 4d 65 6d 50 61 67 65 2e 61 44   into MemPage.aD
17dca 61 74 61 5b 5d 20 62 65 66 6f 72 65 20 72 65 74  ata[] before ret
17dcb 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e  urning..**.** In
17dcc 20 74 68 65 20 63 6f 75 72 73 65 20 6f 66 20 62   the course of b
17dcd 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 70 61 67  alancing the pag
17dce 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e  e and its siblin
17dcf 67 73 2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65  gs, cells may be
17dd0 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74  .** inserted int
17dd1 6f 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f  o or removed fro
17dd2 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  m the parent pag
17dd3 65 20 28 70 50 61 72 65 6e 74 29 2e 20 44 6f 69  e (pParent). Doi
17dd4 6e 67 20 73 6f 0a 2a 2a 20 6d 61 79 20 63 61 75  ng so.** may cau
17dd5 73 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  se the parent pa
17dd6 67 65 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65  ge to become ove
17dd7 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75  rfull or underfu
17dd8 6c 6c 2e 20 49 66 20 74 68 69 73 0a 2a 2a 20 68  ll. If this.** h
17dd9 61 70 70 65 6e 73 2c 20 69 74 20 69 73 20 74 68  appens, it is th
17dda 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  e responsibility
17ddb 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74   of the caller t
17ddc 6f 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72  o invoke the cor
17ddd 72 65 63 74 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e  rect.** balancin
17dde 67 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 69 78  g routine to fix
17ddf 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 73   this problem (s
17de0 65 65 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29  ee the balance()
17de1 20 72 6f 75 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a   routine). .**.*
17de2 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * If this routin
17de3 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20  e fails for any 
17de4 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74  reason, it might
17de5 20 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62   leave the datab
17de6 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72  ase.** in a corr
17de7 75 70 74 65 64 20 73 74 61 74 65 2e 20 53 6f 20  upted state. So 
17de8 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  if this routine 
17de9 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62  fails, the datab
17dea 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  ase should.** be
17deb 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a   rolled back..**
17dec 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72  .** The third ar
17ded 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
17dee 75 6e 63 74 69 6f 6e 2c 20 61 4f 76 66 6c 53 70  unction, aOvflSp
17def 61 63 65 2c 20 69 73 20 61 20 70 6f 69 6e 74 65  ace, is a pointe
17df0 72 20 74 6f 20 61 0a 2a 2a 20 62 75 66 66 65 72  r to a.** buffer
17df1 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68   big enough to h
17df2 6f 6c 64 20 6f 6e 65 20 70 61 67 65 2e 20 49 66  old one page. If
17df3 20 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67   while inserting
17df4 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20   cells into the 
17df5 70 61 72 65 6e 74 0a 2a 2a 20 70 61 67 65 20 28  parent.** page (
17df6 70 50 61 72 65 6e 74 29 20 74 68 65 20 70 61 72  pParent) the par
17df7 65 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73  ent page becomes
17df8 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 69 73 20   overfull, this 
17df9 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 75 73 65  buffer is.** use
17dfa 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 70  d to store the p
17dfb 61 72 65 6e 74 27 73 20 6f 76 65 72 66 6c 6f 77  arent's overflow
17dfc 20 63 65 6c 6c 73 2e 20 42 65 63 61 75 73 65 20   cells. Because 
17dfd 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e  this function in
17dfe 73 65 72 74 73 0a 2a 2a 20 61 20 6d 61 78 69 6d  serts.** a maxim
17dff 75 6d 20 6f 66 20 66 6f 75 72 20 64 69 76 69 64  um of four divid
17e00 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68  er cells into th
17e01 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 61  e parent page, a
17e02 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a  nd the maximum.*
17e03 2a 20 73 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c  * size of a cell
17e04 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 61   stored within a
17e05 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20  n internal node 
17e06 69 73 20 61 6c 77 61 79 73 20 6c 65 73 73 20 74  is always less t
17e07 68 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66 20 74 68  han 1/4.** of th
17e08 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65  e page-size, the
17e09 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 62 75   aOvflSpace[] bu
17e0a 66 66 65 72 20 69 73 20 67 75 61 72 61 6e 74 65  ffer is guarante
17e0b 65 64 20 74 6f 20 62 65 20 6c 61 72 67 65 0a 2a  ed to be large.*
17e0c 2a 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c  * enough for all
17e0d 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e   overflow cells.
17e0e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76 66 6c 53  .**.** If aOvflS
17e0f 70 61 63 65 20 69 73 20 73 65 74 20 74 6f 20 61  pace is set to a
17e10 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2c 20 74   null pointer, t
17e11 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
17e12 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  urns .** SQLITE_
17e13 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63  NOMEM..*/.static
17e14 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e   int balance_non
17e15 72 6f 6f 74 28 0a 20 20 4d 65 6d 50 61 67 65 20  root(.  MemPage 
17e16 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20  *pParent,       
17e17 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e          /* Paren
17e18 74 20 70 61 67 65 20 6f 66 20 73 69 62 6c 69 6e  t page of siblin
17e19 67 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65  gs being balance
17e1a 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65  d */.  int iPare
17e1b 6e 74 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  ntIdx,          
17e1c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
17e1d 6f 66 20 22 74 68 65 20 70 61 67 65 22 20 69 6e  of "the page" in
17e1e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38   pParent */.  u8
17e1f 20 2a 61 4f 76 66 6c 53 70 61 63 65 2c 20 20 20   *aOvflSpace,   
17e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17e21 20 70 61 67 65 2d 73 69 7a 65 20 62 79 74 65 73   page-size bytes
17e22 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 70 61   of space for pa
17e23 72 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69  rent ovfl */.  i
17e24 6e 74 20 69 73 52 6f 6f 74 20 20 20 20 20 20 20  nt isRoot       
17e25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17e26 2a 20 54 72 75 65 20 69 66 20 70 50 61 72 65 6e  * True if pParen
17e27 74 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65  t is a root-page
17e28 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65   */.){.  BtShare
17e29 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20  d *pBt;         
17e2a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f        /* The who
17e2b 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  le database */. 
17e2c 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20   int nCell = 0; 
17e2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17e2e 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   Number of cells
17e2f 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a   in apCell[] */.
17e30 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20    int nMaxCells 
17e31 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 0;           /
17e32 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  * Allocated size
17e33 20 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65   of apCell, szCe
17e34 6c 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20  ll, aFrom. */.  
17e35 69 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20  int nNew = 0;   
17e36 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17e37 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
17e38 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20  in apNew[] */.  
17e39 69 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20  int nOld;       
17e3a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17e3b 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
17e3c 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20  in apOld[] */.  
17e3d 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20  int i, j, k;    
17e3e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17e3f 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
17e40 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 20 20  .  int nxDiv;   
17e41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e42 2f 2a 20 4e 65 78 74 20 64 69 76 69 64 65 72 20  /* Next divider 
17e43 73 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e 74 2d  slot in pParent-
17e44 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e  >aCell[] */.  in
17e45 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
17e46 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
17e47 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  e return code */
17e48 0a 20 20 75 31 36 20 6c 65 61 66 43 6f 72 72 65  .  u16 leafCorre
17e49 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20  ction;          
17e4a 2f 2a 20 34 20 69 66 20 70 50 61 67 65 20 69 73  /* 4 if pPage is
17e4b 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 6e   a leaf.  0 if n
17e4c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66  ot */.  int leaf
17e4d 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
17e4e 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
17e4f 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 20  pPage is a leaf 
17e50 6f 66 20 61 20 4c 45 41 46 44 41 54 41 20 74 72  of a LEAFDATA tr
17e51 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62  ee */.  int usab
17e52 6c 65 53 70 61 63 65 3b 20 20 20 20 20 20 20 20  leSpace;        
17e53 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e       /* Bytes in
17e54 20 70 50 61 67 65 20 62 65 79 6f 6e 64 20 74 68   pPage beyond th
17e55 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e  e header */.  in
17e56 74 20 70 61 67 65 46 6c 61 67 73 3b 20 20 20 20  t pageFlags;    
17e57 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
17e58 6c 75 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44  lue of pPage->aD
17e59 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20  ata[0] */.  int 
17e5a 73 75 62 74 6f 74 61 6c 3b 20 20 20 20 20 20 20  subtotal;       
17e5b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 74           /* Subt
17e5c 6f 74 61 6c 20 6f 66 20 62 79 74 65 73 20 69 6e  otal of bytes in
17e5d 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61   cells on one pa
17e5e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61  ge */.  int iSpa
17e5f 63 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ce1 = 0;        
17e60 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e       /* First un
17e61 75 73 65 64 20 62 79 74 65 20 6f 66 20 61 53 70  used byte of aSp
17e62 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  ace1[] */.  int 
17e63 69 4f 76 66 6c 53 70 61 63 65 20 3d 20 30 3b 20  iOvflSpace = 0; 
17e64 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
17e65 74 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66  t unused byte of
17e66 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 2a 2f   aOvflSpace[] */
17e67 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68  .  int szScratch
17e68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
17e69 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74  /* Size of scrat
17e6a 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73  ch memory reques
17e6b 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  ted */.  MemPage
17e6c 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20   *apOld[NB];    
17e6d 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61        /* pPage a
17e6e 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62  nd up to two sib
17e6f 6c 69 6e 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 61  lings */.  MemPa
17e70 67 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20  ge *apCopy[NB]; 
17e71 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 61          /* Priva
17e72 74 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 4f  te copies of apO
17e73 6c 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20  ld[] pages */.  
17e74 4d 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e  MemPage *apNew[N
17e75 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20  B+2];        /* 
17e76 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20  pPage and up to 
17e77 4e 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 65  NB siblings afte
17e78 72 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20  r balancing */. 
17e79 20 75 38 20 2a 70 52 69 67 68 74 3b 20 20 20 20   u8 *pRight;    
17e7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17e7b 20 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 70 61 72   Location in par
17e7c 65 6e 74 20 6f 66 20 72 69 67 68 74 2d 73 69 62  ent of right-sib
17e7d 6c 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  ling pointer */.
17e7e 20 20 75 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31    u8 *apDiv[NB-1
17e7f 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ];             /
17e80 2a 20 44 69 76 69 64 65 72 20 63 65 6c 6c 73 20  * Divider cells 
17e81 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20  in pParent */.  
17e82 69 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d  int cntNew[NB+2]
17e83 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
17e84 49 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d  Index in aCell[]
17e85 20 6f 66 20 63 65 6c 6c 20 61 66 74 65 72 20 69   of cell after i
17e86 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  -th page */.  in
17e87 74 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20  t szNew[NB+2];  
17e88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
17e89 6d 62 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 63  mbined size of c
17e8a 65 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d  ells place on i-
17e8b 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20  th page */.  u8 
17e8c 2a 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20  **apCell = 0;   
17e8d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
17e8e 20 63 65 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c   cells begin bal
17e8f 61 6e 63 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a  anced */.  u16 *
17e90 73 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20  szCell;         
17e91 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c          /* Local
17e92 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c   size of all cel
17e93 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a  ls in apCell[] *
17e94 2f 0a 20 20 75 38 20 2a 61 53 70 61 63 65 31 3b  /.  u8 *aSpace1;
17e95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e96 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 6f   /* Space for co
17e97 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73  pies of dividers
17e98 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f   cells */.  Pgno
17e99 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
17e9a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
17e9b 20 76 61 72 20 74 6f 20 73 74 6f 72 65 20 61 20   var to store a 
17e9c 70 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a  page number in *
17e9d 2f 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 72 65  /..  pBt = pPare
17e9e 6e 74 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72  nt->pBt;.  asser
17e9f 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
17ea0 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
17ea1 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
17ea2 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
17ea3 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e  teable(pParent->
17ea4 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 23 69 66  pDbPage) );..#if
17ea5 20 30 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c   0.  TRACE(("BAL
17ea6 41 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 65  ANCE: begin page
17ea7 20 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 5c   %d child of %d\
17ea8 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  n", pPage->pgno,
17ea9 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29   pParent->pgno))
17eaa 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41  ;.#endif..  /* A
17eab 74 20 74 68 69 73 20 70 6f 69 6e 74 20 70 50 61  t this point pPa
17eac 72 65 6e 74 20 6d 61 79 20 68 61 76 65 20 61 74  rent may have at
17ead 20 6d 6f 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c   most one overfl
17eae 6f 77 20 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0a  ow cell. And if.
17eaf 20 20 2a 2a 20 74 68 69 73 20 6f 76 65 72 66 6c    ** this overfl
17eb0 6f 77 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65  ow cell is prese
17eb1 6e 74 2c 20 69 74 20 6d 75 73 74 20 62 65 20 74  nt, it must be t
17eb2 68 65 20 63 65 6c 6c 20 77 69 74 68 20 0a 20 20  he cell with .  
17eb3 2a 2a 20 69 6e 64 65 78 20 69 50 61 72 65 6e 74  ** index iParent
17eb4 49 64 78 2e 20 54 68 69 73 20 73 63 65 6e 61 72  Idx. This scenar
17eb5 69 6f 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 77  io comes about w
17eb6 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
17eb7 6e 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64  n.  ** is called
17eb8 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 66 72   (indirectly) fr
17eb9 6f 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  om sqlite3BtreeD
17eba 65 6c 65 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 20  elete()..  */.  
17ebb 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d  assert( pParent-
17ebc 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c  >nOverflow==0 ||
17ebd 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66   pParent->nOverf
17ebe 6c 6f 77 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65  low==1 );.  asse
17ebf 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76  rt( pParent->nOv
17ec0 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61  erflow==0 || pPa
17ec1 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69  rent->aOvfl[0].i
17ec2 64 78 3d 3d 69 50 61 72 65 6e 74 49 64 78 20 29  dx==iParentIdx )
17ec3 3b 0a 0a 20 20 69 66 28 20 21 61 4f 76 66 6c 53  ;..  if( !aOvflS
17ec4 70 61 63 65 20 29 7b 0a 20 20 20 20 72 65 74 75  pace ){.    retu
17ec5 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
17ec6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20  .  }..  /* Find 
17ec7 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65  the sibling page
17ec8 73 20 74 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c  s to balance. Al
17ec9 73 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 63 65  so locate the ce
17eca 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 0a  lls in pParent .
17ecb 20 20 2a 2a 20 74 68 61 74 20 64 69 76 69 64 65    ** that divide
17ecc 20 74 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 41   the siblings. A
17ecd 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64  n attempt is mad
17ece 65 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62  e to find NN sib
17ecf 6c 69 6e 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65  lings on .  ** e
17ed0 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 50  ither side of pP
17ed1 61 67 65 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e  age. More siblin
17ed2 67 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f  gs are taken fro
17ed3 6d 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65  m one side, howe
17ed4 76 65 72 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68  ver, .  ** if th
17ed5 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68  ere are fewer th
17ed6 61 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f  an NN siblings o
17ed7 6e 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65  n the other side
17ed8 2e 20 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a  . If pParent.  *
17ed9 2a 20 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65  * has NB or fewe
17eda 72 20 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20  r children then 
17edb 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20  all children of 
17edc 70 50 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65  pParent are take
17edd 6e 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  n.  .  **.  ** T
17ede 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72  his loop also dr
17edf 6f 70 73 20 74 68 65 20 64 69 76 69 64 65 72 20  ops the divider 
17ee0 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 70  cells from the p
17ee1 61 72 65 6e 74 20 70 61 67 65 2e 20 54 68 69 73  arent page. This
17ee2 0a 20 20 2a 2a 20 77 61 79 2c 20 74 68 65 20 72  .  ** way, the r
17ee3 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20  emainder of the 
17ee4 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  function does no
17ee5 74 20 68 61 76 65 20 74 6f 20 64 65 61 6c 20 77  t have to deal w
17ee6 69 74 68 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65  ith any.  ** ove
17ee7 72 66 6c 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74  rflow cells in t
17ee8 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20  he parent page, 
17ee9 73 69 6e 63 65 20 69 66 20 61 6e 79 20 65 78 69  since if any exi
17eea 73 74 65 64 20 74 68 65 79 20 77 69 6c 6c 0a 20  sted they will. 
17eeb 20 2a 2a 20 68 61 76 65 20 61 6c 72 65 61 64 79   ** have already
17eec 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20   been removed.. 
17eed 20 2a 2f 0a 20 20 69 20 3d 20 70 50 61 72 65 6e   */.  i = pParen
17eee 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70  t->nOverflow + p
17eef 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20  Parent->nCell;. 
17ef0 20 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20   if( i<2 ){.    
17ef1 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 6e  nxDiv = 0;.    n
17ef2 4f 6c 64 20 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c  Old = i+1;.  }el
17ef3 73 65 7b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 33  se{.    nOld = 3
17ef4 3b 0a 20 20 20 20 69 66 28 20 69 50 61 72 65 6e  ;.    if( iParen
17ef5 74 49 64 78 3d 3d 30 20 29 7b 20 20 20 20 20 20  tIdx==0 ){      
17ef6 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20             .    
17ef7 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20    nxDiv = 0;.   
17ef8 20 7d 65 6c 73 65 20 69 66 28 20 69 50 61 72 65   }else if( iPare
17ef9 6e 74 49 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20  ntIdx==i ){.    
17efa 20 20 6e 78 44 69 76 20 3d 20 69 2d 32 3b 0a 20    nxDiv = i-2;. 
17efb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17efc 6e 78 44 69 76 20 3d 20 69 50 61 72 65 6e 74 49  nxDiv = iParentI
17efd 64 78 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  dx-1;.    }.    
17efe 69 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28  i = 2;.  }.  if(
17eff 20 28 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e   (i+nxDiv-pParen
17f00 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70  t->nOverflow)==p
17f01 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b  Parent->nCell ){
17f02 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 26 70  .    pRight = &p
17f03 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50  Parent->aData[pP
17f04 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74  arent->hdrOffset
17f05 2b 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  +8];.  }else{.  
17f06 20 20 70 52 69 67 68 74 20 3d 20 66 69 6e 64 43    pRight = findC
17f07 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e  ell(pParent, i+n
17f08 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f  xDiv-pParent->nO
17f09 76 65 72 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20  verflow);.  }.  
17f0a 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
17f0b 70 52 69 67 68 74 29 3b 0a 20 20 77 68 69 6c 65  pRight);.  while
17f0c 28 20 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ( 1 ){.    rc = 
17f0d 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70  getAndInitPage(p
17f0e 42 74 2c 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64  Bt, pgno, &apOld
17f0f 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63  [i]);.    if( rc
17f10 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74   ){.      memset
17f11 28 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29  (apOld, 0, (i+1)
17f12 2a 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a  *sizeof(MemPage*
17f13 29 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  ));.      goto b
17f14 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
17f15 20 20 20 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65      }.    nMaxCe
17f16 6c 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69  lls += 1+apOld[i
17f17 5d 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69  ]->nCell+apOld[i
17f18 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20  ]->nOverflow;.  
17f19 20 20 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29    if( (i--)==0 )
17f1a 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28   break;..    if(
17f1b 20 69 2b 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e   i+nxDiv==pParen
17f1c 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20  t->aOvfl[0].idx 
17f1d 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65  && pParent->nOve
17f1e 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 61  rflow ){.      a
17f1f 70 44 69 76 5b 69 5d 20 3d 20 70 50 61 72 65 6e  pDiv[i] = pParen
17f20 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c  t->aOvfl[0].pCel
17f21 6c 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20  l;.      pgno = 
17f22 67 65 74 34 62 79 74 65 28 61 70 44 69 76 5b 69  get4byte(apDiv[i
17f23 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b  ]);.      szNew[
17f24 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  i] = cellSizePtr
17f25 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b  (pParent, apDiv[
17f26 69 5d 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65  i]);.      pPare
17f27 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20  nt->nOverflow = 
17f28 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
17f29 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66      apDiv[i] = f
17f2a 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c  indCell(pParent,
17f2b 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74   i+nxDiv-pParent
17f2c 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20  ->nOverflow);.  
17f2d 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62      pgno = get4b
17f2e 79 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20  yte(apDiv[i]);. 
17f2f 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20       szNew[i] = 
17f30 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72  cellSizePtr(pPar
17f31 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a  ent, apDiv[i]);.
17f32 0a 20 20 20 20 20 20 2f 2a 20 44 72 6f 70 20 74  .      /* Drop t
17f33 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65  he cell from the
17f34 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 61 70   parent page. ap
17f35 44 69 76 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69  Div[i] still poi
17f36 6e 74 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  nts to.      ** 
17f37 74 68 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20  the cell within 
17f38 74 68 65 20 70 61 72 65 6e 74 2c 20 65 76 65 6e  the parent, even
17f39 20 74 68 6f 75 67 68 20 69 74 20 68 61 73 20 62   though it has b
17f3a 65 65 6e 20 64 72 6f 70 70 65 64 2e 0a 20 20 20  een dropped..   
17f3b 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 73 61     ** This is sa
17f3c 66 65 20 62 65 63 61 75 73 65 20 64 72 6f 70 70  fe because dropp
17f3d 69 6e 67 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20  ing a cell only 
17f3e 6f 76 65 72 77 72 69 74 65 73 20 74 68 65 20 66  overwrites the f
17f3f 69 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f  irst.      ** fo
17f40 75 72 20 62 79 74 65 73 20 6f 66 20 69 74 2c 20  ur bytes of it, 
17f41 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f  and this functio
17f42 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
17f43 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 20  the first.      
17f44 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66  ** four bytes of
17f45 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
17f46 6c 2e 20 53 6f 20 74 68 65 20 70 6f 69 6e 74 65  l. So the pointe
17f47 72 20 69 73 20 73 61 66 65 20 74 6f 20 75 73 65  r is safe to use
17f48 0a 20 20 20 20 20 20 2a 2a 20 6c 61 74 65 72 20  .      ** later 
17f49 6f 6e 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20  on.  .      **. 
17f4a 20 20 20 20 20 2a 2a 20 55 6e 6c 65 73 73 20 53       ** Unless S
17f4b 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
17f4c 64 20 69 6e 20 73 65 63 75 72 65 2d 64 65 6c 65  d in secure-dele
17f4d 74 65 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73  te mode. In this
17f4e 20 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20   case,.      ** 
17f4f 74 68 65 20 64 72 6f 70 43 65 6c 6c 28 29 20 72  the dropCell() r
17f50 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72  outine will over
17f51 77 72 69 74 65 20 74 68 65 20 65 6e 74 69 72 65  write the entire
17f52 20 63 65 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65   cell with zeroe
17f53 73 2e 0a 20 20 20 20 20 20 2a 2a 20 49 6e 20 74  s..      ** In t
17f54 68 69 73 20 63 61 73 65 2c 20 74 65 6d 70 6f 72  his case, tempor
17f55 61 72 69 6c 79 20 63 6f 70 79 20 74 68 65 20 63  arily copy the c
17f56 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 61 4f 76  ell into the aOv
17f57 66 6c 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20  flSpace[].      
17f58 2a 2a 20 62 75 66 66 65 72 2e 20 49 74 20 77 69  ** buffer. It wi
17f59 6c 6c 20 62 65 20 63 6f 70 69 65 64 20 6f 75 74  ll be copied out
17f5a 20 61 67 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61   again as soon a
17f5b 73 20 74 68 65 20 61 53 70 61 63 65 5b 5d 20 62  s the aSpace[] b
17f5c 75 66 66 65 72 0a 20 20 20 20 20 20 2a 2a 20 69  uffer.      ** i
17f5d 73 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f  s allocated.  */
17f5e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
17f5f 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 20  ECURE_DELETE.   
17f60 20 20 20 6d 65 6d 63 70 79 28 26 61 4f 76 66 6c     memcpy(&aOvfl
17f61 53 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70  Space[apDiv[i]-p
17f62 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 2c 20  Parent->aData], 
17f63 61 70 44 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b  apDiv[i], szNew[
17f64 69 5d 29 3b 0a 20 20 20 20 20 20 61 70 44 69 76  i]);.      apDiv
17f65 5b 69 5d 20 3d 20 26 61 4f 76 66 6c 53 70 61 63  [i] = &aOvflSpac
17f66 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65  e[apDiv[i]-pPare
17f67 6e 74 2d 3e 61 44 61 74 61 5d 3b 0a 23 65 6e 64  nt->aData];.#end
17f68 69 66 0a 20 20 20 20 20 20 64 72 6f 70 43 65 6c  if.      dropCel
17f69 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44  l(pParent, i+nxD
17f6a 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65  iv-pParent->nOve
17f6b 72 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c  rflow, szNew[i],
17f6c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   &rc);.    }.  }
17f6d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78  ..  /* Make nMax
17f6e 43 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65  Cells a multiple
17f6f 20 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74   of 4 in order t
17f70 6f 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74  o preserve 8-byt
17f71 65 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74  e.  ** alignment
17f72 20 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20   */.  nMaxCells 
17f73 3d 20 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33  = (nMaxCells + 3
17f74 29 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  )&~3;..  /*.  **
17f75 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   Allocate space 
17f76 66 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75 63  for memory struc
17f77 74 75 72 65 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d  tures.  */.  k =
17f78 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b   pBt->pageSize +
17f79 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d   ROUND8(sizeof(M
17f7a 65 6d 50 61 67 65 29 29 3b 0a 20 20 73 7a 53 63  emPage));.  szSc
17f7b 72 61 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e  ratch =.       n
17f7c 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28  MaxCells*sizeof(
17f7d 75 38 2a 29 20 20 20 20 20 20 20 20 20 20 20 20  u8*)            
17f7e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70             /* ap
17f7f 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e  Cell */.     + n
17f80 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28  MaxCells*sizeof(
17f81 75 31 36 29 20 20 20 20 20 20 20 20 20 20 20 20  u16)            
17f82 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a             /* sz
17f83 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 70  Cell */.     + p
17f84 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 20 20 20  Bt->pageSize    
17f85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f86 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53             /* aS
17f87 70 61 63 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20  pace1 */.     + 
17f88 6b 2a 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20  k*nOld;         
17f89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f8a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
17f8b 61 67 65 20 63 6f 70 69 65 73 20 28 61 70 43 6f  age copies (apCo
17f8c 70 79 29 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20  py) */.  apCell 
17f8d 3d 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68  = sqlite3Scratch
17f8e 4d 61 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63  Malloc( szScratc
17f8f 68 20 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65  h ); .  if( apCe
17f90 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  ll==0 ){.    rc 
17f91 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
17f92 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65      goto balance
17f93 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
17f94 73 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26  szCell = (u16*)&
17f95 61 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73  apCell[nMaxCells
17f96 5d 3b 0a 20 20 61 53 70 61 63 65 31 20 3d 20 28  ];.  aSpace1 = (
17f97 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78  u8*)&szCell[nMax
17f98 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74  Cells];.  assert
17f99 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
17f9a 47 4e 4d 45 4e 54 28 61 53 70 61 63 65 31 29 20  GNMENT(aSpace1) 
17f9b 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f  );..  /*.  ** Lo
17f9c 61 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61  ad pointers to a
17f9d 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 69 62 6c  ll cells on sibl
17f9e 69 6e 67 20 70 61 67 65 73 20 61 6e 64 20 74 68  ing pages and th
17f9f 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a  e divider cells.
17fa0 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 6f    ** into the lo
17fa1 63 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 61 72 72  cal apCell[] arr
17fa2 61 79 2e 20 20 4d 61 6b 65 20 63 6f 70 69 65 73  ay.  Make copies
17fa3 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20   of the divider 
17fa4 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20  cells.  ** into 
17fa5 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66  space obtained f
17fa6 72 6f 6d 20 61 53 70 61 63 65 31 5b 5d 20 61 6e  rom aSpace1[] an
17fa7 64 20 72 65 6d 6f 76 65 20 74 68 65 20 74 68 65  d remove the the
17fa8 20 64 69 76 69 64 65 72 20 43 65 6c 6c 73 0a 20   divider Cells. 
17fa9 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65 6e 74   ** from pParent
17faa 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
17fab 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20  he siblings are 
17fac 6f 6e 20 6c 65 61 66 20 70 61 67 65 73 2c 20 74  on leaf pages, t
17fad 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70 6f  hen the child po
17fae 69 6e 74 65 72 73 20 6f 66 20 74 68 65 0a 20 20  inters of the.  
17faf 2a 2a 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73  ** divider cells
17fb0 20 61 72 65 20 73 74 72 69 70 70 65 64 20 66 72   are stripped fr
17fb1 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20 62 65 66  om the cells bef
17fb2 6f 72 65 20 74 68 65 79 20 61 72 65 20 63 6f 70  ore they are cop
17fb3 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 53  ied.  ** into aS
17fb4 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74 68 69  pace1[].  In thi
17fb5 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73  s way, all cells
17fb6 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 72 65   in apCell[] are
17fb7 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 63 68   without.  ** ch
17fb8 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 49  ild pointers.  I
17fb9 66 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e  f siblings are n
17fba 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20  ot leaves, then 
17fbb 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a  all cell in.  **
17fbc 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64   apCell[] includ
17fbd 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73  e child pointers
17fbe 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61  .  Either way, a
17fbf 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65  ll cells in apCe
17fc0 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20 61 6c  ll[].  ** are al
17fc1 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c  ike..  **.  ** l
17fc2 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20  eafCorrection:  
17fc3 34 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20  4 if pPage is a 
17fc4 6c 65 61 66 2e 20 20 30 20 69 66 20 70 50 61 67  leaf.  0 if pPag
17fc5 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e  e is not a leaf.
17fc6 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 61 66  .  **       leaf
17fc7 44 61 74 61 3a 20 20 31 20 69 66 20 70 50 61 67  Data:  1 if pPag
17fc8 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61  e holds key+data
17fc9 20 61 6e 64 20 70 50 61 72 65 6e 74 20 68 6f 6c   and pParent hol
17fca 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 20  ds only keys..  
17fcb 2a 2f 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74  */.  leafCorrect
17fcc 69 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e  ion = apOld[0]->
17fcd 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61  leaf*4;.  leafDa
17fce 74 61 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68  ta = apOld[0]->h
17fcf 61 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d  asData;.  for(i=
17fd0 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b  0; i<nOld; i++){
17fd1 0a 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 3b 0a  .    int limit;.
17fd2 20 20 20 20 0a 20 20 20 20 2f 2a 20 42 65 66 6f      .    /* Befo
17fd3 72 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e  re doing anythin
17fd4 67 20 65 6c 73 65 2c 20 74 61 6b 65 20 61 20 63  g else, take a c
17fd5 6f 70 79 20 6f 66 20 74 68 65 20 69 27 74 68 20  opy of the i'th 
17fd6 6f 72 69 67 69 6e 61 6c 20 73 69 62 6c 69 6e 67  original sibling
17fd7 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 74  .    ** The rest
17fd8 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
17fd9 6e 20 77 69 6c 6c 20 75 73 65 20 64 61 74 61 20  n will use data 
17fda 66 72 6f 6d 20 74 68 65 20 63 6f 70 69 65 73 20  from the copies 
17fdb 72 61 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 68  rather.    ** th
17fdc 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  at the original 
17fdd 70 61 67 65 73 20 73 69 6e 63 65 20 74 68 65 20  pages since the 
17fde 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 77  original pages w
17fdf 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 0a 20 20  ill be in the.  
17fe0 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 20    ** process of 
17fe1 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65  being overwritte
17fe2 6e 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61  n.  */.    MemPa
17fe3 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70  ge *pOld = apCop
17fe4 79 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a  y[i] = (MemPage*
17fe5 29 26 61 53 70 61 63 65 31 5b 70 42 74 2d 3e 70  )&aSpace1[pBt->p
17fe6 61 67 65 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a  ageSize + k*i];.
17fe7 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2c      memcpy(pOld,
17fe8 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f   apOld[i], sizeo
17fe9 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 20  f(MemPage));.   
17fea 20 70 4f 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28   pOld->aData = (
17feb 76 6f 69 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a  void*)&pOld[1];.
17fec 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d      memcpy(pOld-
17fed 3e 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d  >aData, apOld[i]
17fee 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61  ->aData, pBt->pa
17fef 67 65 53 69 7a 65 29 3b 0a 0a 20 20 20 20 6c 69  geSize);..    li
17ff0 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c  mit = pOld->nCel
17ff1 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  l+pOld->nOverflo
17ff2 77 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  w;.    for(j=0; 
17ff3 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20  j<limit; j++){. 
17ff4 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65       assert( nCe
17ff5 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a  ll<nMaxCells );.
17ff6 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65        apCell[nCe
17ff7 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c  ll] = findOverfl
17ff8 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b  owCell(pOld, j);
17ff9 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43  .      szCell[nC
17ffa 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50  ell] = cellSizeP
17ffb 74 72 28 70 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b  tr(pOld, apCell[
17ffc 6e 43 65 6c 6c 5d 29 3b 0a 20 20 20 20 20 20 6e  nCell]);.      n
17ffd 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  Cell++;.    }.  
17ffe 20 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 26    if( i<nOld-1 &
17fff 26 20 21 6c 65 61 66 44 61 74 61 29 7b 0a 20 20  & !leafData){.  
18000 20 20 20 20 75 31 36 20 73 7a 20 3d 20 28 75 31      u16 sz = (u1
18001 36 29 73 7a 4e 65 77 5b 69 5d 3b 0a 20 20 20 20  6)szNew[i];.    
18002 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20    u8 *pTemp;.   
18003 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c     assert( nCell
18004 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20  <nMaxCells );.  
18005 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c      szCell[nCell
18006 5d 20 3d 20 73 7a 3b 0a 20 20 20 20 20 20 70 54  ] = sz;.      pT
18007 65 6d 70 20 3d 20 26 61 53 70 61 63 65 31 5b 69  emp = &aSpace1[i
18008 53 70 61 63 65 31 5d 3b 0a 20 20 20 20 20 20 69  Space1];.      i
18009 53 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20  Space1 += sz;.  
1800a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d      assert( sz<=
1800b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20  pBt->pageSize/4 
1800c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1800d 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70   iSpace1<=pBt->p
1800e 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20  ageSize );.     
1800f 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c 20 61   memcpy(pTemp, a
18010 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20  pDiv[i], sz);.  
18011 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c      apCell[nCell
18012 5d 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f  ] = pTemp+leafCo
18013 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20  rrection;.      
18014 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72  assert( leafCorr
18015 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61  ection==0 || lea
18016 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29  fCorrection==4 )
18017 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e  ;.      szCell[n
18018 43 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e  Cell] = szCell[n
18019 43 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72  Cell] - leafCorr
1801a 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66  ection;.      if
1801b 28 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b  ( !pOld->leaf ){
1801c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1801d 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d   leafCorrection=
1801e 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =0 );.        as
1801f 73 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f  sert( pOld->hdrO
18020 66 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20  ffset==0 );.    
18021 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
18022 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20   pointer of the 
18023 63 68 69 6c 64 20 70 61 67 65 20 70 4f 6c 64 20  child page pOld 
18024 62 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 66 74  becomes the left
18025 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e  .        ** poin
18026 74 65 72 20 6f 66 20 74 68 65 20 64 69 76 69 64  ter of the divid
18027 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20  er cell */.     
18028 20 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c     memcpy(apCell
18029 5b 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e  [nCell], &pOld->
1802a 61 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20  aData[8], 4);.  
1802b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1802c 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43     assert( leafC
1802d 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a  orrection==4 );.
1802e 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65          if( szCe
1802f 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20  ll[nCell]<4 ){. 
18030 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e           /* Do n
18031 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c  ot allow any cel
18032 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  ls smaller than 
18033 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20  4 bytes. */.    
18034 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65        szCell[nCe
18035 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20  ll] = 4;.       
18036 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
18037 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a   nCell++;.    }.
18038 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46    }..  /*.  ** F
18039 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75  igure out the nu
1803a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65  mber of pages ne
1803b 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c  eded to hold all
1803c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20   nCell cells..  
1803d 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75  ** Store this nu
1803e 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c  mber in "k".  Al
1803f 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77  so compute szNew
18040 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20  [] which is the 
18041 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20  total.  ** size 
18042 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20  of all cells on 
18043 74 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e  the i-th page an
18044 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68  d cntNew[] which
18045 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20   is the index.  
18046 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f  ** in apCell[] o
18047 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
18048 64 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66  divides page i f
18049 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a  rom page i+1.  .
1804a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73    ** cntNew[k] s
1804b 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c  hould equal nCel
1804c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c  l..  **.  ** Val
1804d 75 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20  ues computed by 
1804e 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a  this block:.  **
1804f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .  **           
18050 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d  k: The total num
18051 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70  ber of sibling p
18052 61 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e  ages.  **    szN
18053 65 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73  ew[i]: Spaced us
18054 65 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73  ed on the i-th s
18055 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a  ibling page..  *
18056 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49  *   cntNew[i]: I
18057 6e 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ndex in apCell[]
18058 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f   and szCell[] fo
18059 72 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c  r the first cell
1805a 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   to.  **        
1805b 20 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20        the right 
1805c 6f 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c  of the i-th sibl
1805d 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75  ing page..  ** u
1805e 73 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62  sableSpace: Numb
1805f 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
18060 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f  pace available o
18061 6e 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a  n each sibling..
18062 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61    ** .  */.  usa
18063 62 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e  bleSpace = pBt->
18064 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20  usableSize - 12 
18065 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  + leafCorrection
18066 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c  ;.  for(subtotal
18067 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  =k=i=0; i<nCell;
18068 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72   i++){.    asser
18069 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( i<nMaxCells )
1806a 3b 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b  ;.    subtotal +
1806b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b  = szCell[i] + 2;
1806c 0a 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61  .    if( subtota
1806d 6c 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20  l > usableSpace 
1806e 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b  ){.      szNew[k
1806f 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73  ] = subtotal - s
18070 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20  zCell[i];.      
18071 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20  cntNew[k] = i;. 
18072 20 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74       if( leafDat
18073 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20  a ){ i--; }.    
18074 20 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a    subtotal = 0;.
18075 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20        k++;.     
18076 20 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72   if( k>NB+1 ){ r
18077 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
18078 50 54 3b 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65  PT; goto balance
18079 5f 63 6c 65 61 6e 75 70 3b 20 7d 0a 20 20 20 20  _cleanup; }.    
1807a 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d  }.  }.  szNew[k]
1807b 20 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63   = subtotal;.  c
1807c 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c  ntNew[k] = nCell
1807d 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20  ;.  k++;..  /*. 
1807e 20 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67 20   ** The packing 
1807f 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20  computed by the 
18080 70 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69  previous block i
18081 73 20 62 69 61 73 65 64 20 74 6f 77 61 72 64 20  s biased toward 
18082 74 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a  the siblings.  *
18083 2a 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73 69  * on the left si
18084 64 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73 69  de.  The left si
18085 62 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61 79  blings are alway
18086 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77  s nearly full, w
18087 68 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72 69  hile the.  ** ri
18088 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67  ght-most sibling
18089 20 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c 79   might be nearly
1808a 20 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62 6c   empty.  This bl
1808b 6f 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74 65  ock of code atte
1808c 6d 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a  mpts.  ** to adj
1808d 75 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67 20  ust the packing 
1808e 6f 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 67  of siblings to g
1808f 65 74 20 61 20 62 65 74 74 65 72 20 62 61 6c 61  et a better bala
18090 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  nce..  **.  ** T
18091 68 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69  his adjustment i
18092 73 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f  s more than an o
18093 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68  ptimization.  Th
18094 65 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65 20  e packing above 
18095 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f  might.  ** be so
18096 20 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65 20   out of balance 
18097 61 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c  as to be illegal
18098 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
18099 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20  the right-most. 
1809a 20 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68   ** sibling migh
1809b 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20  t be completely 
1809c 65 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64 6a  empty.  This adj
1809d 75 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f  ustment is not o
1809e 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20  ptional..  */.  
1809f 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20  for(i=k-1; i>0; 
180a0 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a  i--){.    int sz
180a1 52 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d  Right = szNew[i]
180a2 3b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69  ;  /* Size of si
180a3 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67  bling on the rig
180a4 68 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a  ht */.    int sz
180a5 4c 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31  Left = szNew[i-1
180a6 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69  ]; /* Size of si
180a7 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66  bling on the lef
180a8 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20  t */.    int r; 
180a9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
180aa 49 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d 6d  Index of right-m
180ab 6f 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74  ost cell in left
180ac 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20   sibling */.    
180ad 69 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20 20  int d;          
180ae 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
180af 66 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74 68  first cell to th
180b0 65 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74 20  e left of right 
180b1 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20  sibling */..    
180b2 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20  r = cntNew[i-1] 
180b3 2d 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b  - 1;.    d = r +
180b4 20 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20   1 - leafData;. 
180b5 20 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d 61     assert( d<nMa
180b6 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73  xCells );.    as
180b7 73 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c  sert( r<nMaxCell
180b8 73 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  s );.    while( 
180b9 73 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a  szRight==0 || sz
180ba 52 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b  Right+szCell[d]+
180bb 32 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c  2<=szLeft-(szCel
180bc 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20  l[r]+2) ){.     
180bd 20 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65   szRight += szCe
180be 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20  ll[d] + 2;.     
180bf 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c   szLeft -= szCel
180c0 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20  l[r] + 2;.      
180c1 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20  cntNew[i-1]--;. 
180c2 20 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b       r = cntNew[
180c3 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20  i-1] - 1;.      
180c4 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66  d = r + 1 - leaf
180c5 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Data;.    }.    
180c6 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67  szNew[i] = szRig
180c7 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d  ht;.    szNew[i-
180c8 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d  1] = szLeft;.  }
180c9 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65  ..  /* Either we
180ca 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f   found one or mo
180cb 72 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77  re cells (cntnew
180cc 5b 30 5d 29 3e 30 29 20 6f 72 20 70 50 61 67 65  [0])>0) or pPage
180cd 20 69 73 0a 20 20 2a 2a 20 61 20 76 69 72 74 75   is.  ** a virtu
180ce 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41  al root page.  A
180cf 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61   virtual root pa
180d0 67 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72  ge is when the r
180d1 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61  eal root.  ** pa
180d2 67 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64  ge is page 1 and
180d3 20 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79   we are the only
180d4 20 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70   child of that p
180d5 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  age..  */.  asse
180d6 72 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20  rt( cntNew[0]>0 
180d7 7c 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e  || (pParent->pgn
180d8 6f 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74 2d  o==1 && pParent-
180d9 3e 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20  >nCell==0) );.. 
180da 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45   TRACE(("BALANCE
180db 3a 20 6f 6c 64 3a 20 25 64 20 25 64 20 25 64 20  : old: %d %d %d 
180dc 20 22 2c 0a 20 20 20 20 61 70 4f 6c 64 5b 30 5d   ",.    apOld[0]
180dd 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c  ->pgno, .    nOl
180de 64 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d  d>=2 ? apOld[1]-
180df 3e 70 67 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 6e  >pgno : 0,.    n
180e0 4f 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b 32  Old>=3 ? apOld[2
180e1 5d 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 29  ]->pgno : 0.  ))
180e2 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c  ;..  /*.  ** All
180e3 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 65  ocate k new page
180e4 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 61  s.  Reuse old pa
180e5 67 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 62  ges where possib
180e6 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61  le..  */.  if( a
180e7 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31  pOld[0]->pgno<=1
180e8 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
180e9 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20  ITE_CORRUPT;.   
180ea 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
180eb 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 61 67  eanup;.  }.  pag
180ec 65 46 6c 61 67 73 20 3d 20 61 70 4f 6c 64 5b 30  eFlags = apOld[0
180ed 5d 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a 20 20 66  ]->aData[0];.  f
180ee 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b  or(i=0; i<k; i++
180ef 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  ){.    MemPage *
180f0 70 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 69 3c  pNew;.    if( i<
180f1 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 4e  nOld ){.      pN
180f2 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 20 3d 20  ew = apNew[i] = 
180f3 61 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20  apOld[i];.      
180f4 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20  apOld[i] = 0;.  
180f5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
180f6 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 2d  PagerWrite(pNew-
180f7 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
180f8 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69   nNew++;.      i
180f9 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c  f( rc ) goto bal
180fa 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
180fb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
180fc 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20  ssert( i>0 );.  
180fd 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74      rc = allocat
180fe 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
180ff 26 70 4e 65 77 2c 20 26 70 67 6e 6f 2c 20 70 67  &pNew, &pgno, pg
18100 6e 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  no, 0);.      if
18101 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61  ( rc ) goto bala
18102 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
18103 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e     apNew[i] = pN
18104 65 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b  ew;.      nNew++
18105 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20  ;..      /* Set 
18106 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
18107 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6e 65  entry for the ne
18108 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20  w sibling page. 
18109 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 49 53 41  */.      if( ISA
1810a 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
1810b 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
1810c 42 74 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20  Bt, pNew->pgno, 
1810d 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50  PTRMAP_BTREE, pP
1810e 61 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63  arent->pgno, &rc
1810f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
18110 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
18111 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62            goto b
18112 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
18113 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
18114 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
18115 2a 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70  * Free any old p
18116 61 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e  ages that were n
18117 6f 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77  ot reused as new
18118 20 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77   pages..  */.  w
18119 68 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a  hile( i<nOld ){.
1811a 20 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4f      freePage(apO
1811b 6c 64 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20  ld[i], &rc);.   
1811c 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62   if( rc ) goto b
1811d 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
1811e 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1811f 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61  apOld[i]);.    a
18120 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20  pOld[i] = 0;.   
18121 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a   i++;.  }..  /*.
18122 20 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77    ** Put the new
18123 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64   pages in accend
18124 69 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73  ing order.  This
18125 20 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b   helps to.  ** k
18126 65 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74  eep entries in t
18127 68 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20  he disk file in 
18128 6f 72 64 65 72 20 73 6f 20 74 68 61 74 20 61 20  order so that a 
18129 73 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65  scan.  ** of the
1812a 20 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65   table is a line
1812b 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20  ar scan through 
1812c 74 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a  the file.  That.
1812d 20 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c    ** in turn hel
1812e 70 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  ps the operating
1812f 20 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76   system to deliv
18130 65 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72  er pages.  ** fr
18131 6f 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65  om the disk more
18132 20 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20   rapidly..  **. 
18133 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e   ** An O(n^2) in
18134 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67  sertion sort alg
18135 6f 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20  orithm is used, 
18136 62 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e  but since.  ** n
18137 20 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74   is never more t
18138 68 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20  han NB (a small 
18139 63 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20  constant), that 
1813a 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20  should.  ** not 
1813b 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20  be a problem..  
1813c 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d  **.  ** When NB=
1813d 3d 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74  =3, this one opt
1813e 69 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20  imization makes 
1813f 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a  the database.  *
18140 2a 20 61 62 6f 75 74 20 32 35 25 20 66 61 73 74  * about 25% fast
18141 65 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73  er for large ins
18142 65 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65  ertions and dele
18143 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f  tions..  */.  fo
18144 72 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b  r(i=0; i<k-1; i+
18145 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56  +){.    int minV
18146 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e   = apNew[i]->pgn
18147 6f 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 20  o;.    int minI 
18148 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69  = i;.    for(j=i
18149 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20  +1; j<k; j++){. 
1814a 20 20 20 20 20 69 66 28 20 61 70 4e 65 77 5b 6a       if( apNew[j
1814b 5d 2d 3e 70 67 6e 6f 3c 28 75 6e 73 69 67 6e 65  ]->pgno<(unsigne
1814c 64 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20  d)minV ){.      
1814d 20 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20    minI = j;.    
1814e 20 20 20 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77      minV = apNew
1814f 5b 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20  [j]->pgno;.     
18150 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
18151 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 20 20 20   minI>i ){.     
18152 20 69 6e 74 20 74 3b 0a 20 20 20 20 20 20 4d 65   int t;.      Me
18153 6d 50 61 67 65 20 2a 70 54 3b 0a 20 20 20 20 20  mPage *pT;.     
18154 20 74 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70   t = apNew[i]->p
18155 67 6e 6f 3b 0a 20 20 20 20 20 20 70 54 20 3d 20  gno;.      pT = 
18156 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20  apNew[i];.      
18157 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77  apNew[i] = apNew
18158 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70  [minI];.      ap
18159 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a  New[minI] = pT;.
1815a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43      }.  }.  TRAC
1815b 45 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29 20  E(("new: %d(%d) 
1815c 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64  %d(%d) %d(%d) %d
1815d 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a  (%d) %d(%d)\n",.
1815e 20 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67      apNew[0]->pg
1815f 6e 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20  no, szNew[0],.  
18160 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65    nNew>=2 ? apNe
18161 77 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20  w[1]->pgno : 0, 
18162 6e 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b  nNew>=2 ? szNew[
18163 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77  1] : 0,.    nNew
18164 3e 3d 33 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e  >=3 ? apNew[2]->
18165 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d  pgno : 0, nNew>=
18166 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30  3 ? szNew[2] : 0
18167 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20  ,.    nNew>=4 ? 
18168 61 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a  apNew[3]->pgno :
18169 20 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a   0, nNew>=4 ? sz
1816a 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20  New[3] : 0,.    
1816b 6e 4e 65 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b  nNew>=5 ? apNew[
1816c 34 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e  4]->pgno : 0, nN
1816d 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d  ew>=5 ? szNew[4]
1816e 20 3a 20 30 29 29 3b 0a 0a 20 20 61 73 73 65 72   : 0));..  asser
1816f 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
18170 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65  swriteable(pPare
18171 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  nt->pDbPage) );.
18172 20 20 70 75 74 34 62 79 74 65 28 70 52 69 67 68    put4byte(pRigh
18173 74 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d  t, apNew[nNew-1]
18174 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20  ->pgno);..  /*. 
18175 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72   ** Evenly distr
18176 69 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69  ibute the data i
18177 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73  n apCell[] acros
18178 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e  s the new pages.
18179 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76  .  ** Insert div
1817a 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20  ider cells into 
1817b 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73  pParent as neces
1817c 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d  sary..  */.  j =
1817d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
1817e 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nNew; i++){.   
1817f 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65   /* Assemble the
18180 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67   new sibling pag
18181 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67  e. */.    MemPag
18182 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b  e *pNew = apNew[
18183 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  i];.    assert( 
18184 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  j<nMaxCells );. 
18185 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77     zeroPage(pNew
18186 2c 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20  , pageFlags);.  
18187 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70    assemblePage(p
18188 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a  New, cntNew[i]-j
18189 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73  , &apCell[j], &s
1818a 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61  zCell[j]);.    a
1818b 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65  ssert( pNew->nCe
1818c 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31  ll>0 || (nNew==1
1818d 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30   && cntNew[0]==0
1818e 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
1818f 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77   pNew->nOverflow
18190 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20  ==0 );..    j = 
18191 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20  cntNew[i];..    
18192 2f 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e  /* If the siblin
18193 67 20 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64  g page assembled
18194 20 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 20 74   above was not t
18195 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69  he right-most si
18196 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e  bling,.    ** in
18197 73 65 72 74 20 61 20 64 69 76 69 64 65 72 20 63  sert a divider c
18198 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 61 72  ell into the par
18199 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  ent page..    */
1819a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e  .    assert( i<n
1819b 4e 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c  New-1 || j==nCel
1819c 6c 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e  l );.    if( j<n
1819d 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38  Cell ){.      u8
1819e 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75   *pCell;.      u
1819f 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20  8 *pTemp;.      
181a0 69 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61  int sz;..      a
181a1 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c  ssert( j<nMaxCel
181a2 6c 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c  ls );.      pCel
181a3 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20  l = apCell[j];. 
181a4 20 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c       sz = szCell
181a5 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63  [j] + leafCorrec
181a6 74 69 6f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d  tion;.      pTem
181a7 70 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b  p = &aOvflSpace[
181a8 69 4f 76 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20  iOvflSpace];.   
181a9 20 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65     if( !pNew->le
181aa 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  af ){.        me
181ab 6d 63 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74  mcpy(&pNew->aDat
181ac 61 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b  a[8], pCell, 4);
181ad 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
181ae 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20   leafData ){.   
181af 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74       /* If the t
181b0 72 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61  ree is a leaf-da
181b1 74 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65  ta tree, and the
181b2 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65   siblings are le
181b3 61 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a  aves, .        *
181b4 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  * then there is 
181b5 6e 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  no divider cell 
181b6 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73  in apCell[]. Ins
181b7 74 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65  tead, the divide
181b8 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65  r .        ** ce
181b9 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74  ll consists of t
181ba 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66  he integer key f
181bb 6f 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  or the right-mos
181bc 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20  t cell of .     
181bd 20 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e     ** the siblin
181be 67 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64  g-page assembled
181bf 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20   above only..   
181c0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
181c1 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
181c2 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20         j--;.    
181c3 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65      btreeParseCe
181c4 6c 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65  llPtr(pNew, apCe
181c5 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20  ll[j], &info);. 
181c6 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70         pCell = p
181c7 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 73 7a  Temp;.        sz
181c8 20 3d 20 34 20 2b 20 70 75 74 56 61 72 69 6e 74   = 4 + putVarint
181c9 28 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f  (&pCell[4], info
181ca 2e 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  .nKey);.        
181cb 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20  pTemp = 0;.     
181cc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
181cd 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20  pCell -= 4;.    
181ce 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63      /* Obscure c
181cf 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66  ase for non-leaf
181d0 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20  -data trees: If 
181d1 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c  the cell at pCel
181d2 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a  l was.        **
181d3 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72   previously stor
181d4 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64  ed on a leaf nod
181d5 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72  e, and its repor
181d6 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20  ted size was 4. 
181d7 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c         ** bytes,
181d8 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74   then it may act
181d9 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72  ually be smaller
181da 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20   than this .    
181db 20 20 20 20 2a 2a 20 28 73 65 65 20 62 74 72 65      ** (see btre
181dc 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c  eParseCellPtr(),
181dd 20 34 20 62 79 74 65 73 20 69 73 20 74 68 65 20   4 bytes is the 
181de 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a  minimum size of.
181df 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63          ** any c
181e0 65 6c 6c 29 2e 20 42 75 74 20 69 74 20 69 73 20  ell). But it is 
181e1 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 70 61 73  important to pas
181e2 73 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69  s the correct si
181e3 7a 65 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a  ze to .        *
181e4 2a 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20  * insertCell(), 
181e5 73 6f 20 72 65 70 61 72 73 65 20 74 68 65 20 63  so reparse the c
181e6 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20  ell now..       
181e7 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e   **.        ** N
181e8 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61  ote that this ca
181e9 6e 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69  n never happen i
181ea 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61  n an SQLite data
181eb 20 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20   file, as all.  
181ec 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61        ** cells a
181ed 72 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79  re at least 4 by
181ee 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70  tes. It only hap
181ef 70 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20  pens in b-trees 
181f0 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  used.        ** 
181f1 74 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e 20  to evaluate "IN 
181f2 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e  (SELECT ...)" an
181f3 64 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73 65  d similar clause
181f4 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
181f5 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c        if( szCell
181f6 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20  [j]==4 ){.      
181f7 20 20 20 20 61 73 73 65 72 74 28 6c 65 61 66 43      assert(leafC
181f8 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20  orrection==4);. 
181f9 20 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 65           sz = ce
181fa 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e  llSizePtr(pParen
181fb 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20  t, pCell);.     
181fc 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
181fd 20 20 20 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d     iOvflSpace +=
181fe 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72   sz;.      asser
181ff 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65  t( sz<=pBt->page
18200 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20  Size/4 );.      
18201 61 73 73 65 72 74 28 20 69 4f 76 66 6c 53 70 61  assert( iOvflSpa
18202 63 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a  ce<=pBt->pageSiz
18203 65 20 29 3b 0a 20 20 20 20 20 20 69 6e 73 65 72  e );.      inser
18204 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e  tCell(pParent, n
18205 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c  xDiv, pCell, sz,
18206 20 70 54 65 6d 70 2c 20 70 4e 65 77 2d 3e 70 67   pTemp, pNew->pg
18207 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  no, &rc);.      
18208 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
18209 4b 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65  K ) goto balance
1820a 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20  _cleanup;.      
1820b 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
1820c 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
1820d 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  pParent->pDbPage
1820e 29 20 29 3b 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b  ) );..      j++;
1820f 0a 20 20 20 20 20 20 6e 78 44 69 76 2b 2b 3b 0a  .      nxDiv++;.
18210 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
18211 72 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a  rt( j==nCell );.
18212 20 20 61 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30    assert( nOld>0
18213 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4e   );.  assert( nN
18214 65 77 3e 30 20 29 3b 0a 20 20 69 66 28 20 28 70  ew>0 );.  if( (p
18215 61 67 65 46 6c 61 67 73 20 26 20 50 54 46 5f 4c  ageFlags & PTF_L
18216 45 41 46 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75  EAF)==0 ){.    u
18217 38 20 2a 7a 43 68 69 6c 64 20 3d 20 26 61 70 43  8 *zChild = &apC
18218 6f 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61  opy[nOld-1]->aDa
18219 74 61 5b 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70  ta[8];.    memcp
1821a 79 28 26 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d  y(&apNew[nNew-1]
1821b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69  ->aData[8], zChi
1821c 6c 64 2c 20 34 29 3b 0a 20 20 7d 0a 0a 20 20 69  ld, 4);.  }..  i
1821d 66 28 20 69 73 52 6f 6f 74 20 26 26 20 70 50 61  f( isRoot && pPa
1821e 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26  rent->nCell==0 &
1821f 26 20 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66  & pParent->hdrOf
18220 66 73 65 74 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e  fset<=apNew[0]->
18221 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 2f 2a 20  nFree ){.    /* 
18222 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  The root page of
18223 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 77 20   the b-tree now 
18224 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c  contains no cell
18225 73 2e 20 54 68 65 20 6f 6e 6c 79 20 73 69 62 6c  s. The only sibl
18226 69 6e 67 0a 20 20 20 20 2a 2a 20 70 61 67 65 20  ing.    ** page 
18227 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69  is the right-chi
18228 6c 64 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74  ld of the parent
18229 2e 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65  . Copy the conte
1822a 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  nts of the.    *
1822b 2a 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 74  * child page int
1822c 6f 20 74 68 65 20 70 61 72 65 6e 74 2c 20 64 65  o the parent, de
1822d 63 72 65 61 73 69 6e 67 20 74 68 65 20 6f 76 65  creasing the ove
1822e 72 61 6c 6c 20 68 65 69 67 68 74 20 6f 66 20 74  rall height of t
1822f 68 65 0a 20 20 20 20 2a 2a 20 62 2d 74 72 65 65  he.    ** b-tree
18230 20 73 74 72 75 63 74 75 72 65 20 62 79 20 6f 6e   structure by on
18231 65 2e 20 54 68 69 73 20 69 73 20 64 65 73 63 72  e. This is descr
18232 69 62 65 64 20 61 73 20 74 68 65 20 22 62 61 6c  ibed as the "bal
18233 61 6e 63 65 2d 73 68 61 6c 6c 6f 77 65 72 22 0a  ance-shallower".
18234 20 20 20 20 2a 2a 20 73 75 62 2d 61 6c 67 6f 72      ** sub-algor
18235 69 74 68 6d 20 69 6e 20 73 6f 6d 65 20 64 6f 63  ithm in some doc
18236 75 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20  umentation..    
18237 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69  **.    ** If thi
18238 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63  s is an auto-vac
18239 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 74 68  uum database, th
1823a 65 20 63 61 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f  e call to copyNo
1823b 64 65 43 6f 6e 74 65 6e 74 28 29 20 0a 20 20 20  deContent() .   
1823c 20 2a 2a 20 73 65 74 73 20 61 6c 6c 20 70 6f 69   ** sets all poi
1823d 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
1823e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
1823f 6f 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  o database image
18240 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 66   pages .    ** f
18241 6f 72 20 77 68 69 63 68 20 74 68 65 20 70 6f 69  or which the poi
18242 6e 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 77  nter is stored w
18243 69 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e  ithin the conten
18244 74 20 62 65 69 6e 67 20 63 6f 70 69 65 64 2e 0a  t being copied..
18245 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
18246 65 20 73 65 63 6f 6e 64 20 61 73 73 65 72 74 20  e second assert 
18247 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74  below verifies t
18248 68 61 74 20 74 68 65 20 63 68 69 6c 64 20 70 61  hat the child pa
18249 67 65 20 69 73 20 64 65 66 72 61 67 6d 65 6e 74  ge is defragment
1824a 65 64 0a 20 20 20 20 2a 2a 20 28 69 74 20 6d 75  ed.    ** (it mu
1824b 73 74 20 62 65 2c 20 61 73 20 69 74 20 77 61 73  st be, as it was
1824c 20 6a 75 73 74 20 72 65 63 6f 6e 73 74 72 75 63   just reconstruc
1824d 74 65 64 20 75 73 69 6e 67 20 61 73 73 65 6d 62  ted using assemb
1824e 6c 65 50 61 67 65 28 29 29 2e 20 54 68 69 73 0a  lePage()). This.
1824f 20 20 20 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74      ** is import
18250 61 6e 74 20 69 66 20 74 68 65 20 70 61 72 65 6e  ant if the paren
18251 74 20 70 61 67 65 20 68 61 70 70 65 6e 73 20 74  t page happens t
18252 6f 20 62 65 20 70 61 67 65 20 31 20 6f 66 20 74  o be page 1 of t
18253 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
18254 2a 2a 20 69 6d 61 67 65 2e 20 20 2a 2f 0a 20 20  ** image.  */.  
18255 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3d 3d    assert( nNew==
18256 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1 );.    assert(
18257 20 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65   apNew[0]->nFree
18258 20 3d 3d 20 0a 20 20 20 20 20 20 20 20 28 67 65   == .        (ge
18259 74 32 62 79 74 65 28 26 61 70 4e 65 77 5b 30 5d  t2byte(&apNew[0]
1825a 2d 3e 61 44 61 74 61 5b 35 5d 29 2d 61 70 4e 65  ->aData[5])-apNe
1825b 77 5b 30 5d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74  w[0]->cellOffset
1825c 2d 61 70 4e 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c  -apNew[0]->nCell
1825d 2a 32 29 20 0a 20 20 20 20 29 3b 0a 20 20 20 20  *2) .    );.    
1825e 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28  copyNodeContent(
1825f 61 70 4e 65 77 5b 30 5d 2c 20 70 50 61 72 65 6e  apNew[0], pParen
18260 74 2c 20 26 72 63 29 3b 0a 20 20 20 20 66 72 65  t, &rc);.    fre
18261 65 50 61 67 65 28 61 70 4e 65 77 5b 30 5d 2c 20  ePage(apNew[0], 
18262 26 72 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  &rc);.  }else if
18263 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29  ( ISAUTOVACUUM )
18264 7b 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65  {.    /* Fix the
18265 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
18266 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65  ries for all the
18267 20 63 65 6c 6c 73 20 74 68 61 74 20 77 65 72 65   cells that were
18268 20 73 68 69 66 74 65 64 20 61 72 6f 75 6e 64 2e   shifted around.
18269 20 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61   .    ** There a
1826a 72 65 20 73 65 76 65 72 61 6c 20 64 69 66 66 65  re several diffe
1826b 72 65 6e 74 20 74 79 70 65 73 20 6f 66 20 70 6f  rent types of po
1826c 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65  inter-map entrie
1826d 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20  s that need to. 
1826e 20 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77     ** be dealt w
1826f 69 74 68 20 62 79 20 74 68 69 73 20 72 6f 75 74  ith by this rout
18270 69 6e 65 2e 20 53 6f 6d 65 20 6f 66 20 74 68 65  ine. Some of the
18271 73 65 20 68 61 76 65 20 62 65 65 6e 20 73 65 74  se have been set
18272 20 61 6c 72 65 61 64 79 2c 20 62 75 74 0a 20 20   already, but.  
18273 20 20 2a 2a 20 6d 61 6e 79 20 68 61 76 65 20 6e    ** many have n
18274 6f 74 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ot. The followin
18275 67 20 69 73 20 61 20 73 75 6d 6d 61 72 79 3a 0a  g is a summary:.
18276 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
18277 31 29 20 54 68 65 20 65 6e 74 72 69 65 73 20 61  1) The entries a
18278 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e  ssociated with n
18279 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73  ew sibling pages
1827a 20 74 68 61 74 20 77 65 72 65 20 6e 6f 74 0a 20   that were not. 
1827b 20 20 20 2a 2a 20 20 20 20 20 20 73 69 62 6c 69     **      sibli
1827c 6e 67 73 20 77 68 65 6e 20 74 68 69 73 20 66 75  ngs when this fu
1827d 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65  nction was calle
1827e 64 2e 20 54 68 65 73 65 20 68 61 76 65 20 61 6c  d. These have al
1827f 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 20 20 20  ready.    **    
18280 20 20 62 65 65 6e 20 73 65 74 2e 20 57 65 20 64    been set. We d
18281 6f 6e 27 74 20 6e 65 65 64 20 74 6f 20 77 6f 72  on't need to wor
18282 72 79 20 61 62 6f 75 74 20 6f 6c 64 20 73 69 62  ry about old sib
18283 6c 69 6e 67 73 20 74 68 61 74 20 77 65 72 65 0a  lings that were.
18284 20 20 20 20 2a 2a 20 20 20 20 20 20 6d 6f 76 65      **      move
18285 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69  d to the free-li
18286 73 74 20 2d 20 74 68 65 20 66 72 65 65 50 61 67  st - the freePag
18287 65 28 29 20 63 6f 64 65 20 68 61 73 20 74 61 6b  e() code has tak
18288 65 6e 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 20  en care.    **  
18289 20 20 20 20 6f 66 20 74 68 6f 73 65 2e 0a 20 20      of those..  
1828a 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 32 29    **.    **   2)
1828b 20 54 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   The pointer-map
1828c 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61   entries associa
1828d 74 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72  ted with the fir
1828e 73 74 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20  st overflow.    
1828f 2a 2a 20 20 20 20 20 20 70 61 67 65 20 69 6e 20  **      page in 
18290 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 68 61  any overflow cha
18291 69 6e 73 20 75 73 65 64 20 62 79 20 6e 65 77 20  ins used by new 
18292 64 69 76 69 64 65 72 20 63 65 6c 6c 73 2e 20 54  divider cells. T
18293 68 65 73 65 20 0a 20 20 20 20 2a 2a 20 20 20 20  hese .    **    
18294 20 20 68 61 76 65 20 61 6c 73 6f 20 61 6c 72 65    have also alre
18295 61 64 79 20 62 65 65 6e 20 74 61 6b 65 6e 20 63  ady been taken c
18296 61 72 65 20 6f 66 20 62 79 20 74 68 65 20 69 6e  are of by the in
18297 73 65 72 74 43 65 6c 6c 28 29 20 63 6f 64 65 2e  sertCell() code.
18298 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
18299 20 33 29 20 49 66 20 74 68 65 20 73 69 62 6c 69   3) If the sibli
1829a 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74  ng pages are not
1829b 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 74 68   leaves, then th
1829c 65 20 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66  e child pages of
1829d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 63 65 6c  .    **      cel
1829e 6c 73 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65  ls stored on the
1829f 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 6d   sibling pages m
182a0 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70  ay need to be up
182a1 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  dated..    **.  
182a2 20 20 2a 2a 20 20 20 34 29 20 49 66 20 74 68 65    **   4) If the
182a3 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61   sibling pages a
182a4 72 65 20 6e 6f 74 20 69 6e 74 65 72 6e 61 6c 20  re not internal 
182a5 69 6e 74 6b 65 79 20 6e 6f 64 65 73 2c 20 74 68  intkey nodes, th
182a6 65 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 20 20  en any.    **   
182a7 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65     overflow page
182a8 73 20 75 73 65 64 20 62 79 20 74 68 65 73 65 20  s used by these 
182a9 63 65 6c 6c 73 20 6d 61 79 20 6e 65 65 64 20 74  cells may need t
182aa 6f 20 62 65 20 75 70 64 61 74 65 64 0a 20 20 20  o be updated.   
182ab 20 2a 2a 20 20 20 20 20 20 28 69 6e 74 65 72 6e   **      (intern
182ac 61 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20  al intkey nodes 
182ad 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f  never contain po
182ae 69 6e 74 65 72 73 20 74 6f 20 6f 76 65 72 66 6c  inters to overfl
182af 6f 77 20 70 61 67 65 73 29 2e 0a 20 20 20 20 2a  ow pages)..    *
182b0 2a 0a 20 20 20 20 2a 2a 20 20 20 35 29 20 49 66  *.    **   5) If
182b1 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67   the sibling pag
182b2 65 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65  es are not leave
182b3 73 2c 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e  s, then the poin
182b4 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 20  ter-map.    **  
182b5 20 20 20 20 65 6e 74 72 69 65 73 20 66 6f 72 20      entries for 
182b6 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
182b7 70 61 67 65 73 20 6f 66 20 65 61 63 68 20 73 69  pages of each si
182b8 62 6c 69 6e 67 20 6d 61 79 20 6e 65 65 64 0a 20  bling may need. 
182b9 20 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 62 65     **      to be
182ba 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a   updated..    **
182bb 0a 20 20 20 20 2a 2a 20 43 61 73 65 73 20 31 20  .    ** Cases 1 
182bc 61 6e 64 20 32 20 61 72 65 20 64 65 61 6c 74 20  and 2 are dealt 
182bd 77 69 74 68 20 61 62 6f 76 65 20 62 79 20 6f 74  with above by ot
182be 68 65 72 20 63 6f 64 65 2e 20 54 68 65 20 6e 65  her code. The ne
182bf 78 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20  xt.    ** block 
182c0 64 65 61 6c 73 20 77 69 74 68 20 63 61 73 65 73  deals with cases
182c1 20 33 20 61 6e 64 20 34 20 61 6e 64 20 74 68 65   3 and 4 and the
182c2 20 6f 6e 65 20 61 66 74 65 72 20 74 68 61 74 2c   one after that,
182c3 20 63 61 73 65 20 35 2e 20 53 69 6e 63 65 0a 20   case 5. Since. 
182c4 20 20 20 2a 2a 20 73 65 74 74 69 6e 67 20 61 20     ** setting a 
182c5 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72  pointer map entr
182c6 79 20 69 73 20 61 20 72 65 6c 61 74 69 76 65 6c  y is a relativel
182c7 79 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72  y expensive oper
182c8 61 74 69 6f 6e 2c 20 74 68 69 73 0a 20 20 20 20  ation, this.    
182c9 2a 2a 20 63 6f 64 65 20 6f 6e 6c 79 20 73 65 74  ** code only set
182ca 73 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e  s pointer map en
182cb 74 72 69 65 73 20 66 6f 72 20 63 68 69 6c 64 20  tries for child 
182cc 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  or overflow page
182cd 73 20 74 68 61 74 20 68 61 76 65 0a 20 20 20 20  s that have.    
182ce 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6d 6f 76 65  ** actually move
182cf 64 20 62 65 74 77 65 65 6e 20 70 61 67 65 73 2e  d between pages.
182d0 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65    */.    MemPage
182d1 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 30   *pNew = apNew[0
182d2 5d 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  ];.    MemPage *
182d3 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 30 5d  pOld = apCopy[0]
182d4 3b 0a 20 20 20 20 69 6e 74 20 6e 4f 76 65 72 66  ;.    int nOverf
182d5 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65  low = pOld->nOve
182d6 72 66 6c 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69  rflow;.    int i
182d7 4e 65 78 74 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e  NextOld = pOld->
182d8 6e 43 65 6c 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f  nCell + nOverflo
182d9 77 3b 0a 20 20 20 20 69 6e 74 20 69 4f 76 65 72  w;.    int iOver
182da 66 6c 6f 77 20 3d 20 28 6e 4f 76 65 72 66 6c 6f  flow = (nOverflo
182db 77 20 3f 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b  w ? pOld->aOvfl[
182dc 30 5d 2e 69 64 78 20 3a 20 2d 31 29 3b 0a 20 20  0].idx : -1);.  
182dd 20 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20    j = 0;        
182de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
182df 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
182e0 27 6f 6c 64 27 20 73 69 62 6c 69 6e 67 20 70 61  'old' sibling pa
182e1 67 65 20 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b  ge */.    k = 0;
182e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
182e3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
182e4 43 75 72 72 65 6e 74 20 27 6e 65 77 27 20 73 69  Current 'new' si
182e5 62 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20  bling page */.  
182e6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65    for(i=0; i<nCe
182e7 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ll; i++){.      
182e8 69 6e 74 20 69 73 44 69 76 69 64 65 72 20 3d 20  int isDivider = 
182e9 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  0;.      while( 
182ea 69 3d 3d 69 4e 65 78 74 4f 6c 64 20 29 7b 0a 20  i==iNextOld ){. 
182eb 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69         /* Cell i
182ec 20 69 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d   is the cell imm
182ed 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
182ee 6e 67 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c  ng the last cell
182ef 20 6f 6e 20 6f 6c 64 0a 20 20 20 20 20 20 20 20   on old.        
182f0 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20  ** sibling page 
182f1 6a 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e  j. If the siblin
182f2 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20  gs are not leaf 
182f3 70 61 67 65 73 20 6f 66 20 61 6e 0a 20 20 20 20  pages of an.    
182f4 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d      ** intkey b-
182f5 74 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20  tree, then cell 
182f6 69 20 77 61 73 20 61 20 64 69 76 69 64 65 72 20  i was a divider 
182f7 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20  cell. */.       
182f8 20 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b   pOld = apCopy[+
182f9 2b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 4e 65  +j];.        iNe
182fa 78 74 4f 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61  xtOld = i + !lea
182fb 66 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43  fData + pOld->nC
182fc 65 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65  ell + pOld->nOve
182fd 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 69  rflow;.        i
182fe 66 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c  f( pOld->nOverfl
182ff 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ow ){.          
18300 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64  nOverflow = pOld
18301 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20  ->nOverflow;.   
18302 20 20 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77         iOverflow
18303 20 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61   = i + !leafData
18304 20 2b 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30   + pOld->aOvfl[0
18305 5d 2e 69 64 78 3b 0a 20 20 20 20 20 20 20 20 7d  ].idx;.        }
18306 0a 20 20 20 20 20 20 20 20 69 73 44 69 76 69 64  .        isDivid
18307 65 72 20 3d 20 21 6c 65 61 66 44 61 74 61 3b 20  er = !leafData; 
18308 20 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20   .      }..     
18309 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f   assert(nOverflo
1830a 77 3e 30 20 7c 7c 20 69 4f 76 65 72 66 6c 6f 77  w>0 || iOverflow
1830b 3c 69 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  <i );.      asse
1830c 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c 32 20 7c  rt(nOverflow<2 |
1830d 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d  | pOld->aOvfl[0]
1830e 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66  .idx==pOld->aOvf
1830f 6c 5b 31 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20  l[1].idx-1);.   
18310 20 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66     assert(nOverf
18311 6c 6f 77 3c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61  low<3 || pOld->a
18312 4f 76 66 6c 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c  Ovfl[1].idx==pOl
18313 64 2d 3e 61 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d  d->aOvfl[2].idx-
18314 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d  1);.      if( i=
18315 3d 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  =iOverflow ){.  
18316 20 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20        isDivider 
18317 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28  = 1;.        if(
18318 20 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30   (--nOverflow)>0
18319 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 4f   ){.          iO
1831a 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20  verflow++;.     
1831b 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
1831c 20 20 20 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65      if( i==cntNe
1831d 77 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  w[k] ){.        
1831e 2f 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65  /* Cell i is the
1831f 20 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c   cell immediatel
18320 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  y following the 
18321 6c 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77  last cell on new
18322 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c  .        ** sibl
18323 69 6e 67 20 70 61 67 65 20 6b 2e 20 49 66 20 74  ing page k. If t
18324 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20  he siblings are 
18325 6e 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f  not leaf pages o
18326 66 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  f an.        ** 
18327 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74  intkey b-tree, t
18328 68 65 6e 20 63 65 6c 6c 20 69 20 69 73 20 61 20  hen cell i is a 
18329 64 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a  divider cell.  *
1832a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  /.        pNew =
1832b 20 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20   apNew[++k];.   
1832c 20 20 20 20 20 69 66 28 20 21 6c 65 61 66 44 61       if( !leafDa
1832d 74 61 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ta ) continue;. 
1832e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
1832f 65 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20  ert( j<nOld );. 
18330 20 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e       assert( k<n
18331 4e 65 77 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  New );..      /*
18332 20 49 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73   If the cell was
18333 20 6f 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69   originally divi
18334 64 65 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73  der cell (and is
18335 20 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20   not now) or.   
18336 20 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f     ** an overflo
18337 77 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68  w cell, or if th
18338 65 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74  e cell was locat
18339 65 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e  ed on a differen
1833a 74 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 20 20  t sibling.      
1833b 2a 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74  ** page before t
1833c 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68  he balancing, th
1833d 65 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  en the pointer m
1833e 61 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63  ap entries assoc
1833f 69 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 77  iated.      ** w
18340 69 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f 72  ith any child or
18341 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
18342 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74  need to be updat
18343 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66  ed.  */.      if
18344 28 20 69 73 44 69 76 69 64 65 72 20 7c 7c 20 70  ( isDivider || p
18345 4f 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d  Old->pgno!=pNew-
18346 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20  >pgno ){.       
18347 20 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63   if( !leafCorrec
18348 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  tion ){.        
18349 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c    ptrmapPut(pBt,
1834a 20 67 65 74 34 62 79 74 65 28 61 70 43 65 6c 6c   get4byte(apCell
1834b 5b 69 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 52  [i]), PTRMAP_BTR
1834c 45 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20  EE, pNew->pgno, 
1834d 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  &rc);.        }.
1834e 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65          if( szCe
1834f 6c 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c  ll[i]>pNew->minL
18350 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  ocal ){.        
18351 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50    ptrmapPutOvflP
18352 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b  tr(pNew, apCell[
18353 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  i], &rc);.      
18354 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
18355 7d 0a 0a 20 20 20 20 69 66 28 20 21 6c 65 61 66  }..    if( !leaf
18356 43 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20  Correction ){.  
18357 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
18358 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  New; i++){.     
18359 20 20 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74     u32 key = get
1835a 34 62 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d  4byte(&apNew[i]-
1835b 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20  >aData[8]);.    
1835c 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42      ptrmapPut(pB
1835d 74 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42  t, key, PTRMAP_B
1835e 54 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e  TREE, apNew[i]->
1835f 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20  pgno, &rc);.    
18360 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30    }.    }..#if 0
18361 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 74 72 6d  .    /* The ptrm
18362 61 70 43 68 65 63 6b 50 61 67 65 73 28 29 20 63  apCheckPages() c
18363 6f 6e 74 61 69 6e 73 20 61 73 73 65 72 74 28 29  ontains assert()
18364 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74   statements that
18365 20 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20   verify that.   
18366 20 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20   ** all pointer 
18367 6d 61 70 20 70 61 67 65 73 20 61 72 65 20 73 65  map pages are se
18368 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 69  t correctly. Thi
18369 73 20 69 73 20 68 65 6c 70 66 75 6c 20 77 68 69  s is helpful whi
1836a 6c 65 20 0a 20 20 20 20 2a 2a 20 64 65 62 75 67  le .    ** debug
1836b 67 69 6e 67 2e 20 54 68 69 73 20 69 73 20 75 73  ging. This is us
1836c 75 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 62  ually disabled b
1836d 65 63 61 75 73 65 20 61 20 63 6f 72 72 75 70 74  ecause a corrupt
1836e 20 64 61 74 61 62 61 73 65 20 6d 61 79 0a 20 20   database may.  
1836f 20 20 2a 2a 20 63 61 75 73 65 20 61 6e 20 61 73    ** cause an as
18370 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
18371 20 74 6f 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20   to fail.  */.  
18372 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67    ptrmapCheckPag
18373 65 73 28 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b  es(apNew, nNew);
18374 0a 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b  .    ptrmapCheck
18375 50 61 67 65 73 28 26 70 50 61 72 65 6e 74 2c 20  Pages(&pParent, 
18376 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a  1);.#endif.  }..
18377 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
18378 74 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 54  t->isInit );.  T
18379 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20  RACE(("BALANCE: 
1837a 66 69 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64  finished: old=%d
1837b 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64   new=%d cells=%d
1837c 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 6e  \n",.          n
1837d 4f 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c  Old, nNew, nCell
1837e 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43  ));..  /*.  ** C
1837f 6c 65 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65  leanup before re
18380 74 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61  turning..  */.ba
18381 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20  lance_cleanup:. 
18382 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46   sqlite3ScratchF
18383 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66  ree(apCell);.  f
18384 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20  or(i=0; i<nOld; 
18385 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73  i++){.    releas
18386 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b  ePage(apOld[i]);
18387 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
18388 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20  i<nNew; i++){.  
18389 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70    releasePage(ap
1838a 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20  New[i]);.  }..  
1838b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
1838c 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1838d 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  on is called whe
1838e 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  n the root page 
1838f 6f 66 20 61 20 62 2d 74 72 65 65 20 73 74 72 75  of a b-tree stru
18390 63 74 75 72 65 20 69 73 0a 2a 2a 20 6f 76 65 72  cture is.** over
18391 66 75 6c 6c 20 28 68 61 73 20 6f 6e 65 20 6f 72  full (has one or
18392 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70   more overflow p
18393 61 67 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e  ages)..**.** A n
18394 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 69 73  ew child page is
18395 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74   allocated and t
18396 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
18397 68 65 20 63 75 72 72 65 6e 74 20 72 6f 6f 74 0a  he current root.
18398 2a 2a 20 70 61 67 65 2c 20 69 6e 63 6c 75 64 69  ** page, includi
18399 6e 67 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  ng overflow cell
1839a 73 2c 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e  s, are copied in
1839b 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 54 68  to the child. Th
1839c 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69  e root.** page i
1839d 73 20 74 68 65 6e 20 6f 76 65 72 77 72 69 74 74  s then overwritt
1839e 65 6e 20 74 6f 20 6d 61 6b 65 20 69 74 20 61 6e  en to make it an
1839f 20 65 6d 70 74 79 20 70 61 67 65 20 77 69 74 68   empty page with
183a0 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64   the right-child
183a1 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 70 6f 69   .** pointer poi
183a2 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 77  nting to the new
183a3 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66   page..**.** Bef
183a4 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 61  ore returning, a
183a5 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  ll pointer-map e
183a6 6e 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f 6e  ntries correspon
183a7 64 69 6e 67 20 74 6f 20 70 61 67 65 73 20 0a 2a  ding to pages .*
183a8 2a 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 63  * that the new c
183a9 68 69 6c 64 2d 70 61 67 65 20 6e 6f 77 20 63 6f  hild-page now co
183aa 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20  ntains pointers 
183ab 74 6f 20 61 72 65 20 75 70 64 61 74 65 64 2e 20  to are updated. 
183ac 54 68 65 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 72  The.** entry cor
183ad 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
183ae 65 20 6e 65 77 20 72 69 67 68 74 2d 63 68 69 6c  e new right-chil
183af 64 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65  d pointer of the
183b0 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73   root.** page is
183b1 20 61 6c 73 6f 20 75 70 64 61 74 65 64 2e 0a 2a   also updated..*
183b2 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
183b3 75 6c 2c 20 2a 70 70 43 68 69 6c 64 20 69 73 20  ul, *ppChild is 
183b4 73 65 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61  set to contain a
183b5 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
183b6 65 20 63 68 69 6c 64 20 0a 2a 2a 20 70 61 67 65  e child .** page
183b7 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69   and SQLITE_OK i
183b8 73 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74  s returned. In t
183b9 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c  his case the cal
183ba 6c 65 72 20 69 73 20 72 65 71 75 69 72 65 64 0a  ler is required.
183bb 2a 2a 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61  ** to call relea
183bc 73 65 50 61 67 65 28 29 20 6f 6e 20 2a 70 70 43  sePage() on *ppC
183bd 68 69 6c 64 20 65 78 61 63 74 6c 79 20 6f 6e 63  hild exactly onc
183be 65 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  e. If an error o
183bf 63 63 75 72 73 2c 0a 2a 2a 20 61 6e 20 65 72 72  ccurs,.** an err
183c0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
183c1 6e 65 64 20 61 6e 64 20 2a 70 70 43 68 69 6c 64  ned and *ppChild
183c2 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f   is set to 0..*/
183c3 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61  .static int bala
183c4 6e 63 65 5f 64 65 65 70 65 72 28 4d 65 6d 50 61  nce_deeper(MemPa
183c5 67 65 20 2a 70 52 6f 6f 74 2c 20 4d 65 6d 50 61  ge *pRoot, MemPa
183c6 67 65 20 2a 2a 70 70 43 68 69 6c 64 29 7b 0a 20  ge **ppChild){. 
183c7 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
183c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
183c9 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20  /* Return value 
183ca 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72  from subprocedur
183cb 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  es */.  MemPage 
183cc 2a 70 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20  *pChild = 0;    
183cd 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
183ce 72 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64  r to a new child
183cf 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20   page */.  Pgno 
183d0 70 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 20 20  pgnoChild = 0;  
183d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
183d2 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
183d3 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a  new child page *
183d4 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
183d5 74 20 3d 20 70 52 6f 6f 74 2d 3e 70 42 74 3b 20  t = pRoot->pBt; 
183d6 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20     /* The BTree 
183d7 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 52  */..  assert( pR
183d8 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30  oot->nOverflow>0
183d9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
183da 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
183db 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
183dc 0a 20 20 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74  .  /* Make pRoot
183dd 2c 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  , the root page 
183de 6f 66 20 74 68 65 20 62 2d 74 72 65 65 2c 20 77  of the b-tree, w
183df 72 69 74 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74  ritable. Allocat
183e0 65 20 61 20 6e 65 77 20 0a 20 20 2a 2a 20 70 61  e a new .  ** pa
183e1 67 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63  ge that will bec
183e2 6f 6d 65 20 74 68 65 20 6e 65 77 20 72 69 67 68  ome the new righ
183e3 74 2d 63 68 69 6c 64 20 6f 66 20 70 50 61 67 65  t-child of pPage
183e4 2e 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65  . Copy the conte
183e5 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  nts.  ** of the 
183e6 6e 6f 64 65 20 73 74 6f 72 65 64 20 6f 6e 20 70  node stored on p
183e7 52 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 6e 65  Root into the ne
183e8 77 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20  w child page..  
183e9 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
183ea 33 50 61 67 65 72 57 72 69 74 65 28 70 52 6f 6f  3PagerWrite(pRoo
183eb 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69  t->pDbPage);.  i
183ec 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
183ed 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c   ){.    rc = all
183ee 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
183ef 42 74 2c 26 70 43 68 69 6c 64 2c 26 70 67 6e 6f  Bt,&pChild,&pgno
183f0 43 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e  Child,pRoot->pgn
183f1 6f 2c 30 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f  o,0);.    copyNo
183f2 64 65 43 6f 6e 74 65 6e 74 28 70 52 6f 6f 74 2c  deContent(pRoot,
183f3 20 70 43 68 69 6c 64 2c 20 26 72 63 29 3b 0a 20   pChild, &rc);. 
183f4 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
183f5 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 70 74 72  UUM ){.      ptr
183f6 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f  mapPut(pBt, pgno
183f7 43 68 69 6c 64 2c 20 50 54 52 4d 41 50 5f 42 54  Child, PTRMAP_BT
183f8 52 45 45 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f  REE, pRoot->pgno
183f9 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20  , &rc);.    }.  
183fa 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  }.  if( rc ){.  
183fb 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 30 3b 0a    *ppChild = 0;.
183fc 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
183fd 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74  pChild);.    ret
183fe 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73  urn rc;.  }.  as
183ff 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
18400 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43  erIswriteable(pC
18401 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29  hild->pDbPage) )
18402 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
18403 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
18404 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61  ble(pRoot->pDbPa
18405 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
18406 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d   pChild->nCell==
18407 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pRoot->nCell );.
18408 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e  .  TRACE(("BALAN
18409 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25 64  CE: copy root %d
1840a 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 52 6f   into %d\n", pRo
1840b 6f 74 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64  ot->pgno, pChild
1840c 2d 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20  ->pgno));..  /* 
1840d 43 6f 70 79 20 74 68 65 20 6f 76 65 72 66 6c 6f  Copy the overflo
1840e 77 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 52 6f  w cells from pRo
1840f 6f 74 20 74 6f 20 70 43 68 69 6c 64 20 2a 2f 0a  ot to pChild */.
18410 20 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d    memcpy(pChild-
18411 3e 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61  >aOvfl, pRoot->a
18412 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76  Ovfl, pRoot->nOv
18413 65 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 52  erflow*sizeof(pR
18414 6f 6f 74 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b  oot->aOvfl[0]));
18415 0a 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72  .  pChild->nOver
18416 66 6c 6f 77 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f  flow = pRoot->nO
18417 76 65 72 66 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a  verflow;..  /* Z
18418 65 72 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ero the contents
18419 20 6f 66 20 70 52 6f 6f 74 2e 20 54 68 65 6e 20   of pRoot. Then 
1841a 69 6e 73 74 61 6c 6c 20 70 43 68 69 6c 64 20 61  install pChild a
1841b 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c  s the right-chil
1841c 64 2e 20 2a 2f 0a 20 20 7a 65 72 6f 50 61 67 65  d. */.  zeroPage
1841d 28 70 52 6f 6f 74 2c 20 70 43 68 69 6c 64 2d 3e  (pRoot, pChild->
1841e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f  aData[0] & ~PTF_
1841f 4c 45 41 46 29 3b 0a 20 20 70 75 74 34 62 79 74  LEAF);.  put4byt
18420 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b  e(&pRoot->aData[
18421 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74  pRoot->hdrOffset
18422 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b  +8], pgnoChild);
18423 0a 0a 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 70  ..  *ppChild = p
18424 43 68 69 6c 64 3b 0a 20 20 72 65 74 75 72 6e 20  Child;.  return 
18425 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
18426 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 74 68 61  .** The page tha
18427 74 20 70 43 75 72 20 63 75 72 72 65 6e 74 6c 79  t pCur currently
18428 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 6a   points to has j
18429 75 73 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  ust been modifie
1842a 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79  d in.** some way
1842b 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
1842c 66 69 67 75 72 65 73 20 6f 75 74 20 69 66 20 74  figures out if t
1842d 68 69 73 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e  his modification
1842e 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72   means the.** tr
1842f 65 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 62  ee needs to be b
18430 61 6c 61 6e 63 65 64 2c 20 61 6e 64 20 69 66 20  alanced, and if 
18431 73 6f 20 63 61 6c 6c 73 20 74 68 65 20 61 70 70  so calls the app
18432 72 6f 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69  ropriate balanci
18433 6e 67 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20  ng .** routine. 
18434 42 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e  Balancing routin
18435 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  es are:.**.**   
18436 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 0a  balance_quick().
18437 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 64 65 65  **   balance_dee
18438 70 65 72 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e  per().**   balan
18439 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a  ce_nonroot().*/.
1843a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e  static int balan
1843b 63 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ce(BtCursor *pCu
1843c 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
1843d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73  QLITE_OK;.  cons
1843e 74 20 69 6e 74 20 6e 4d 69 6e 20 3d 20 70 43 75  t int nMin = pCu
1843f 72 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  r->pBt->usableSi
18440 7a 65 20 2a 20 32 20 2f 20 33 3b 0a 20 20 75 38  ze * 2 / 3;.  u8
18441 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70   aBalanceQuickSp
18442 61 63 65 5b 31 33 5d 3b 0a 20 20 75 38 20 2a 70  ace[13];.  u8 *p
18443 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20 54 45 53  Free = 0;..  TES
18444 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e  TONLY( int balan
18445 63 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20  ce_quick_called 
18446 3d 20 30 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c  = 0 );.  TESTONL
18447 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64  Y( int balance_d
18448 65 65 70 65 72 5f 63 61 6c 6c 65 64 20 3d 20 30  eeper_called = 0
18449 20 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20   );..  do {.    
1844a 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72  int iPage = pCur
1844b 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 4d 65 6d  ->iPage;.    Mem
1844c 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43  Page *pPage = pC
1844d 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65  ur->apPage[iPage
1844e 5d 3b 0a 0a 20 20 20 20 69 66 28 20 69 50 61 67  ];..    if( iPag
1844f 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  e==0 ){.      if
18450 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
18451 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ow ){.        /*
18452 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   The root page o
18453 66 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 20  f the b-tree is 
18454 6f 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69  overfull. In thi
18455 73 20 63 61 73 65 20 63 61 6c 6c 20 74 68 65 0a  s case call the.
18456 20 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e          ** balan
18457 63 65 5f 64 65 65 70 65 72 28 29 20 66 75 6e 63  ce_deeper() func
18458 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61  tion to create a
18459 20 6e 65 77 20 63 68 69 6c 64 20 66 6f 72 20 74   new child for t
1845a 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20  he root-page.   
1845b 20 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 70 79       ** and copy
1845c 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e   the current con
1845d 74 65 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 6f  tents of the roo
1845e 74 2d 70 61 67 65 20 74 6f 20 69 74 2e 20 54 68  t-page to it. Th
1845f 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 78  e.        ** nex
18460 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74  t iteration of t
18461 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20  he do-loop will 
18462 62 61 6c 61 6e 63 65 20 74 68 65 20 63 68 69 6c  balance the chil
18463 64 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20  d page..        
18464 2a 2f 20 0a 20 20 20 20 20 20 20 20 61 73 73 65  */ .        asse
18465 72 74 28 20 28 62 61 6c 61 6e 63 65 5f 64 65 65  rt( (balance_dee
18466 70 65 72 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30  per_called++)==0
18467 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   );.        rc =
18468 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28   balance_deeper(
18469 70 50 61 67 65 2c 20 26 70 43 75 72 2d 3e 61 70  pPage, &pCur->ap
1846a 50 61 67 65 5b 31 5d 29 3b 0a 20 20 20 20 20 20  Page[1]);.      
1846b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1846c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1846d 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 31   pCur->iPage = 1
1846e 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72  ;.          pCur
1846f 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a  ->aiIdx[0] = 0;.
18470 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e            pCur->
18471 61 69 49 64 78 5b 31 5d 20 3d 20 30 3b 0a 20 20  aiIdx[1] = 0;.  
18472 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
18473 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 5d 2d  pCur->apPage[1]-
18474 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20  >nOverflow );.  
18475 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
18476 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65  lse{.        bre
18477 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
18478 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d  }else if( pPage-
18479 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26  >nOverflow==0 &&
1847a 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e   pPage->nFree<=n
1847b 4d 69 6e 20 29 7b 0a 20 20 20 20 20 20 62 72 65  Min ){.      bre
1847c 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ak;.    }else{. 
1847d 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 20 63       MemPage * c
1847e 6f 6e 73 74 20 70 50 61 72 65 6e 74 20 3d 20 70  onst pParent = p
1847f 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67  Cur->apPage[iPag
18480 65 2d 31 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20  e-1];.      int 
18481 63 6f 6e 73 74 20 69 49 64 78 20 3d 20 70 43 75  const iIdx = pCu
18482 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 2d 31  r->aiIdx[iPage-1
18483 5d 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ];..      rc = s
18484 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
18485 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67  (pParent->pDbPag
18486 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  e);.      if( rc
18487 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23  ==SQLITE_OK ){.#
18488 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18489 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a  IT_QUICKBALANCE.
1848a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67          if( pPag
1848b 65 2d 3e 68 61 73 44 61 74 61 0a 20 20 20 20 20  e->hasData.     
1848c 20 20 20 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f      && pPage->nO
1848d 76 65 72 66 6c 6f 77 3d 3d 31 0a 20 20 20 20 20  verflow==1.     
1848e 20 20 20 20 26 26 20 70 50 61 67 65 2d 3e 61 4f      && pPage->aO
1848f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67  vfl[0].idx==pPag
18490 65 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20  e->nCell.       
18491 20 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 67    && pParent->pg
18492 6e 6f 21 3d 31 0a 20 20 20 20 20 20 20 20 20 26  no!=1.         &
18493 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  & pParent->nCell
18494 3d 3d 69 49 64 78 0a 20 20 20 20 20 20 20 20 29  ==iIdx.        )
18495 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  {.          /* C
18496 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63  all balance_quic
18497 6b 28 29 20 74 6f 20 63 72 65 61 74 65 20 61 20  k() to create a 
18498 6e 65 77 20 73 69 62 6c 69 6e 67 20 6f 66 20 70  new sibling of p
18499 50 61 67 65 20 6f 6e 20 77 68 69 63 68 0a 20 20  Page on which.  
1849a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 74          ** to st
1849b 6f 72 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  ore the overflow
1849c 20 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 65 5f 71   cell. balance_q
1849d 75 69 63 6b 28 29 20 69 6e 73 65 72 74 73 20 61  uick() inserts a
1849e 20 6e 65 77 20 63 65 6c 6c 0a 20 20 20 20 20 20   new cell.      
1849f 20 20 20 20 2a 2a 20 69 6e 74 6f 20 70 50 61 72      ** into pPar
184a0 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63  ent, which may c
184a1 61 75 73 65 20 70 50 61 72 65 6e 74 20 6f 76 65  ause pParent ove
184a2 72 66 6c 6f 77 2e 20 49 66 20 74 68 69 73 0a 20  rflow. If this. 
184a3 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 70 70           ** happ
184a4 65 6e 73 2c 20 74 68 65 20 6e 65 78 74 20 69 6e  ens, the next in
184a5 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
184a6 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c  do-loop will bal
184a7 61 6e 63 65 20 70 50 61 72 65 6e 74 20 0a 20 20  ance pParent .  
184a8 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 65          ** use e
184a9 69 74 68 65 72 20 62 61 6c 61 6e 63 65 5f 6e 6f  ither balance_no
184aa 6e 72 6f 6f 74 28 29 20 6f 72 20 62 61 6c 61 6e  nroot() or balan
184ab 63 65 5f 64 65 65 70 65 72 28 29 2e 20 55 6e 74  ce_deeper(). Unt
184ac 69 6c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  il this.        
184ad 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68    ** happens, th
184ae 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20  e overflow cell 
184af 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  is stored in the
184b0 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70   aBalanceQuickSp
184b1 61 63 65 5b 5d 0a 20 20 20 20 20 20 20 20 20 20  ace[].          
184b2 2a 2a 20 62 75 66 66 65 72 2e 20 0a 20 20 20 20  ** buffer. .    
184b3 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
184b4 20 20 20 2a 2a 20 54 68 65 20 70 75 72 70 6f 73     ** The purpos
184b5 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
184b6 6e 67 20 61 73 73 65 72 74 28 29 20 69 73 20 74  ng assert() is t
184b7 6f 20 63 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c  o check that onl
184b8 79 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  y a.          **
184b9 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 20   single call to 
184ba 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20  balance_quick() 
184bb 69 73 20 6d 61 64 65 20 66 6f 72 20 65 61 63 68  is made for each
184bc 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 0a 20 20   call to this.  
184bd 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74          ** funct
184be 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 77 65 72  ion. If this wer
184bf 65 20 6e 6f 74 20 76 65 72 69 66 69 65 64 2c 20  e not verified, 
184c0 61 20 73 75 62 74 6c 65 20 62 75 67 20 69 6e 76  a subtle bug inv
184c1 6f 6c 76 69 6e 67 20 72 65 75 73 65 0a 20 20 20  olving reuse.   
184c2 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65         ** of the
184c3 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70   aBalanceQuickSp
184c4 61 63 65 5b 5d 20 6d 69 67 68 74 20 73 6e 65 61  ace[] might snea
184c5 6b 20 69 6e 2e 0a 20 20 20 20 20 20 20 20 20 20  k in..          
184c6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  */.          ass
184c7 65 72 74 28 20 28 62 61 6c 61 6e 63 65 5f 71 75  ert( (balance_qu
184c8 69 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30  ick_called++)==0
184c9 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   );.          rc
184ca 20 3d 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b   = balance_quick
184cb 28 70 50 61 72 65 6e 74 2c 20 70 50 61 67 65 2c  (pParent, pPage,
184cc 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70   aBalanceQuickSp
184cd 61 63 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  ace);.        }e
184ce 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  lse.#endif.     
184cf 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 2f     {.          /
184d0 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20  * In this case, 
184d1 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e  call balance_non
184d2 72 6f 6f 74 28 29 20 74 6f 20 72 65 64 69 73 74  root() to redist
184d3 72 69 62 75 74 65 20 63 65 6c 6c 73 0a 20 20 20  ribute cells.   
184d4 20 20 20 20 20 20 20 2a 2a 20 62 65 74 77 65 65         ** betwee
184d5 6e 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74  n pPage and up t
184d6 6f 20 32 20 6f 66 20 69 74 73 20 73 69 62 6c 69  o 2 of its sibli
184d7 6e 67 20 70 61 67 65 73 2e 20 54 68 69 73 20 69  ng pages. This i
184d8 6e 76 6f 6c 76 65 73 0a 20 20 20 20 20 20 20 20  nvolves.        
184d9 20 20 2a 2a 20 6d 6f 64 69 66 79 69 6e 67 20 74    ** modifying t
184da 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70  he contents of p
184db 50 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61  Parent, which ma
184dc 79 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20  y cause pParent 
184dd 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  to.          ** 
184de 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20  become overfull 
184df 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68  or underfull. Th
184e0 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  e next iteration
184e1 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a   of the do-loop.
184e2 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c            ** wil
184e3 6c 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70 61  l balance the pa
184e4 72 65 6e 74 20 70 61 67 65 20 74 6f 20 63 6f 72  rent page to cor
184e5 72 65 63 74 20 74 68 69 73 2e 0a 20 20 20 20 20  rect this..     
184e6 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 20       ** .       
184e7 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72     ** If the par
184e8 65 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73  ent page becomes
184e9 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f   overfull, the o
184ea 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20  verflow cell or 
184eb 63 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 20  cells.          
184ec 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e  ** are stored in
184ed 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66   the pSpace buff
184ee 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d  er allocated imm
184ef 65 64 69 61 74 65 6c 79 20 62 65 6c 6f 77 2e 20  ediately below. 
184f0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 20  .          ** A 
184f1 73 75 62 73 65 71 75 65 6e 74 20 69 74 65 72 61  subsequent itera
184f2 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c  tion of the do-l
184f3 6f 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20 77 69  oop will deal wi
184f4 74 68 20 74 68 69 73 20 62 79 0a 20 20 20 20 20  th this by.     
184f5 20 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20       ** calling 
184f6 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28  balance_nonroot(
184f7 29 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65  ) (balance_deepe
184f8 72 28 29 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  r() may be calle
184f9 64 20 66 69 72 73 74 2c 0a 20 20 20 20 20 20 20  d first,.       
184fa 20 20 20 2a 2a 20 62 75 74 20 69 74 20 64 6f 65     ** but it doe
184fb 73 6e 27 74 20 64 65 61 6c 20 77 69 74 68 20 6f  sn't deal with o
184fc 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 2d 20  verflow cells - 
184fd 6a 75 73 74 20 6d 6f 76 65 73 20 74 68 65 6d 20  just moves them 
184fe 74 6f 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a  to a.          *
184ff 2a 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65  * different page
18500 29 2e 20 4f 6e 63 65 20 74 68 69 73 20 73 75 62  ). Once this sub
18501 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20  sequent call to 
18502 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28  balance_nonroot(
18503 29 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ) .          ** 
18504 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 2c 20 69  has completed, i
18505 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 6c  t is safe to rel
18506 65 61 73 65 20 74 68 65 20 70 53 70 61 63 65 20  ease the pSpace 
18507 62 75 66 66 65 72 20 75 73 65 64 20 62 79 0a 20  buffer used by. 
18508 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20           ** the 
18509 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2c 20 61  previous call, a
1850a 73 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  s the overflow c
1850b 65 6c 6c 20 64 61 74 61 20 77 69 6c 6c 20 68 61  ell data will ha
1850c 76 65 20 62 65 65 6e 20 0a 20 20 20 20 20 20 20  ve been .       
1850d 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 65 69 74     ** copied eit
1850e 68 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f 64  her into the bod
1850f 79 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  y of a database 
18510 70 61 67 65 20 6f 72 20 69 6e 74 6f 20 74 68 65  page or into the
18511 20 6e 65 77 0a 20 20 20 20 20 20 20 20 20 20 2a   new.          *
18512 2a 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20  * pSpace buffer 
18513 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 6c 61  passed to the la
18514 74 74 65 72 20 63 61 6c 6c 20 74 6f 20 62 61 6c  tter call to bal
18515 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a  ance_nonroot()..
18516 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
18517 20 20 20 20 20 20 20 75 38 20 2a 70 53 70 61 63         u8 *pSpac
18518 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d  e = sqlite3PageM
18519 61 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 42 74 2d  alloc(pCur->pBt-
1851a 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20  >pageSize);.    
1851b 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e        rc = balan
1851c 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 61 72 65  ce_nonroot(pPare
1851d 6e 74 2c 20 69 49 64 78 2c 20 70 53 70 61 63 65  nt, iIdx, pSpace
1851e 2c 20 69 50 61 67 65 3d 3d 31 29 3b 0a 20 20 20  , iPage==1);.   
1851f 20 20 20 20 20 20 20 69 66 28 20 70 46 72 65 65         if( pFree
18520 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
18521 2f 2a 20 49 66 20 70 46 72 65 65 20 69 73 20 6e  /* If pFree is n
18522 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e  ot NULL, it poin
18523 74 73 20 74 6f 20 74 68 65 20 70 53 70 61 63 65  ts to the pSpace
18524 20 62 75 66 66 65 72 20 75 73 65 64 20 0a 20 20   buffer used .  
18525 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20            ** by 
18526 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20  a previous call 
18527 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f  to balance_nonro
18528 6f 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e  ot(). Its conten
18529 74 73 20 61 72 65 0a 20 20 20 20 20 20 20 20 20  ts are.         
1852a 20 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64     ** now stored
1852b 20 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20   either on real 
1852c 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f  database pages o
1852d 72 20 77 69 74 68 69 6e 20 74 68 65 20 0a 20 20  r within the .  
1852e 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 77            ** new
1852f 20 70 53 70 61 63 65 20 62 75 66 66 65 72 2c 20   pSpace buffer, 
18530 73 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61 66  so it may be saf
18531 65 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e 20  ely freed here. 
18532 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  */.            s
18533 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70  qlite3PageFree(p
18534 46 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Free);.         
18535 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   }..          /*
18536 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66   The pSpace buff
18537 65 72 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64  er will be freed
18538 20 61 66 74 65 72 20 74 68 65 20 6e 65 78 74 20   after the next 
18539 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20  call to.        
1853a 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e    ** balance_non
1853b 72 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 73 74 20  root(), or just 
1853c 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63  before this func
1853d 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 77 68  tion returns, wh
1853e 69 63 68 65 76 65 72 0a 20 20 20 20 20 20 20 20  ichever.        
1853f 20 20 2a 2a 20 63 6f 6d 65 73 20 66 69 72 73 74    ** comes first
18540 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70  . */.          p
18541 46 72 65 65 20 3d 20 70 53 70 61 63 65 3b 0a 20  Free = pSpace;. 
18542 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
18543 0a 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e  ..      pPage->n
18544 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20  Overflow = 0;.. 
18545 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74       /* The next
18546 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
18547 65 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63  e do-loop balanc
18548 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  es the parent pa
18549 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c  ge. */.      rel
1854a 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
1854b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61  .      pCur->iPa
1854c 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77  ge--;.    }.  }w
1854d 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
1854e 5f 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 46  _OK );..  if( pF
1854f 72 65 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ree ){.    sqlit
18550 65 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65  e3PageFree(pFree
18551 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
18552 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e  rc;.}.../*.** In
18553 73 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72  sert a new recor
18554 64 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65  d into the BTree
18555 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69  .  The key is gi
18556 76 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65  ven by (pKey,nKe
18557 79 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  y).** and the da
18558 74 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28  ta is given by (
18559 70 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54  pData,nData).  T
1855a 68 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65  he cursor is use
1855b 64 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66  d only to.** def
1855c 69 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74  ine what table t
1855d 68 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64  he record should
1855e 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74   be inserted int
1855f 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a  o.  The cursor.*
18560 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  * is left pointi
18561 6e 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c  ng at a random l
18562 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46  ocation..**.** F
18563 6f 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62  or an INTKEY tab
18564 6c 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65  le, only the nKe
18565 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b  y value of the k
18566 65 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65  ey is used.  pKe
18567 79 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e  y is.** ignored.
18568 20 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41    For a ZERODATA
18569 20 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74   table, the pDat
1856a 61 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20  a and nData are 
1856b 62 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a  both ignored..**
1856c 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52  .** If the seekR
1856d 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20  esult parameter 
1856e 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1856f 6e 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63  n a successful c
18570 61 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f  all to.** Moveto
18571 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 73 65  Unpacked() to se
18572 65 6b 20 63 75 72 73 6f 72 20 70 43 75 72 20 74  ek cursor pCur t
18573 6f 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68  o (pKey, nKey) h
18574 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65  as already.** be
18575 65 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 73 65  en performed. se
18576 65 6b 52 65 73 75 6c 74 20 69 73 20 74 68 65 20  ekResult is the 
18577 73 65 61 72 63 68 20 72 65 73 75 6c 74 20 72 65  search result re
18578 74 75 72 6e 65 64 20 28 61 20 6e 65 67 61 74 69  turned (a negati
18579 76 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20  ve.** number if 
1857a 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61  pCur points at a
1857b 6e 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20  n entry that is 
1857c 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b  smaller than (pK
1857d 65 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a  ey, nKey), or.**
1857e 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75   a positive valu
1857f 65 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73  e if pCur points
18580 20 61 74 20 61 6e 20 65 74 72 79 20 74 68 61 74   at an etry that
18581 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
18582 0a 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29  .** (pKey, nKey)
18583 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ). .**.** If the
18584 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61   seekResult para
18585 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72  meter is non-zer
18586 6f 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c  o, then the call
18587 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68  er guarantees th
18588 61 74 0a 2a 2a 20 63 75 72 73 6f 72 20 70 43 75  at.** cursor pCu
18589 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 61 74  r is pointing at
1858a 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f   the existing co
1858b 70 79 20 6f 66 20 61 20 72 6f 77 20 74 68 61 74  py of a row that
1858c 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65   is to be.** ove
1858d 72 77 72 69 74 74 65 6e 2e 20 20 49 66 20 74 68  rwritten.  If th
1858e 65 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72  e seekResult par
1858f 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65  ameter is 0, the
18590 6e 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d 61  n cursor pCur ma
18591 79 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e  y.** point to an
18592 79 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f  y entry or to no
18593 20 65 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e   entry at all an
18594 64 20 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69  d so this functi
18595 6f 6e 20 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a  on has to seek.*
18596 2a 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66  * the cursor bef
18597 6f 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20  ore the new key 
18598 63 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e  can be inserted.
18599 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1859a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1859b 72 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43  reeInsert(.  BtC
1859c 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20  ursor *pCur,    
1859d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1859e 6e 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20  nsert data into 
1859f 74 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69  the table of thi
185a0 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f  s cursor */.  co
185a1 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
185a2 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20  i64 nKey,    /* 
185a3 54 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e  The key of the n
185a4 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63  ew record */.  c
185a5 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61  onst void *pData
185a6 2c 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a  , int nData,  /*
185a7 20 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65   The data of the
185a8 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20   new record */. 
185a9 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20   int nZero,     
185aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185ab 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74  /* Number of ext
185ac 72 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70  ra 0 bytes to ap
185ad 70 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a  pend to data */.
185ae 20 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73    int appendBias
185af 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
185b0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
185b1 20 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70   is likely an ap
185b2 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65  pend */.  int se
185b3 65 6b 52 65 73 75 6c 74 20 20 20 20 20 20 20 20  ekResult        
185b4 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
185b5 6c 74 20 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65  lt of prior Move
185b6 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 63 61 6c  toUnpacked() cal
185b7 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  l */.){.  int rc
185b8 3b 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65  ;.  int loc = se
185b9 65 6b 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20  ekResult;       
185ba 20 20 20 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65     /* -1: before
185bb 20 64 65 73 69 72 65 64 20 6c 6f 63 61 74 69 6f   desired locatio
185bc 6e 20 20 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a  n  +1: after */.
185bd 20 20 69 6e 74 20 73 7a 4e 65 77 20 3d 20 30 3b    int szNew = 0;
185be 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65  .  int idx;.  Me
185bf 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20  mPage *pPage;.  
185c0 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d  Btree *p = pCur-
185c1 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61  >pBtree;.  BtSha
185c2 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
185c3 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  t;.  unsigned ch
185c4 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75  ar *oldCell;.  u
185c5 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65  nsigned char *ne
185c6 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 69 66  wCell = 0;..  if
185c7 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
185c8 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a  CURSOR_FAULT ){.
185c9 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
185ca 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49  ->skipNext!=SQLI
185cb 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 65 74  TE_OK );.    ret
185cc 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65  urn pCur->skipNe
185cd 78 74 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  xt;.  }..  asser
185ce 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
185cf 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
185d0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46  ssert( pCur->wrF
185d1 6c 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72  lag && pBt->inTr
185d2 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
185d3 5f 57 52 49 54 45 20 26 26 20 21 70 42 74 2d 3e  _WRITE && !pBt->
185d4 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73  readOnly );.  as
185d5 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43  sert( hasSharedC
185d6 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  acheTableLock(p,
185d7 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
185d8 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21   pCur->pKeyInfo!
185d9 3d 30 2c 20 32 29 20 29 3b 0a 0a 20 20 2f 2a 20  =0, 2) );..  /* 
185da 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20  Assert that the 
185db 63 61 6c 6c 65 72 20 68 61 73 20 62 65 65 6e 20  caller has been 
185dc 63 6f 6e 73 69 73 74 65 6e 74 2e 20 49 66 20 74  consistent. If t
185dd 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f  his cursor was o
185de 70 65 6e 65 64 0a 20 20 2a 2a 20 65 78 70 65 63  pened.  ** expec
185df 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62 2d  ting an index b-
185e0 74 72 65 65 2c 20 74 68 65 6e 20 74 68 65 20 63  tree, then the c
185e1 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20  aller should be 
185e2 69 6e 73 65 72 74 69 6e 67 20 62 6c 6f 62 0a 20  inserting blob. 
185e3 20 2a 2a 20 6b 65 79 73 20 77 69 74 68 20 6e 6f   ** keys with no
185e4 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61   associated data
185e5 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20  . If the cursor 
185e6 77 61 73 20 6f 70 65 6e 65 64 20 65 78 70 65 63  was opened expec
185e7 74 69 6e 67 20 61 6e 0a 20 20 2a 2a 20 69 6e 74  ting an.  ** int
185e8 6b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 20 63  key table, the c
185e9 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20  aller should be 
185ea 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 65 67 65  inserting intege
185eb 72 20 6b 65 79 73 20 77 69 74 68 20 61 0a 20 20  r keys with a.  
185ec 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 73 73 6f 63  ** blob of assoc
185ed 69 61 74 65 64 20 64 61 74 61 2e 20 20 2a 2f 0a  iated data.  */.
185ee 20 20 61 73 73 65 72 74 28 20 28 70 4b 65 79 3d    assert( (pKey=
185ef 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79  =0)==(pCur->pKey
185f0 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f  Info==0) );..  /
185f1 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
185f2 69 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 74 61  insert into a ta
185f3 62 6c 65 20 62 2d 74 72 65 65 2c 20 69 6e 76 61  ble b-tree, inva
185f4 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62  lidate any incrb
185f5 6c 6f 62 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72  lob .  ** cursor
185f6 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 72 6f  s open on the ro
185f7 77 20 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64  w being replaced
185f8 20 28 61 73 73 75 6d 69 6e 67 20 74 68 69 73 20   (assuming this 
185f9 69 73 20 61 20 72 65 70 6c 61 63 65 0a 20 20 2a  is a replace.  *
185fa 2a 20 6f 70 65 72 61 74 69 6f 6e 20 2d 20 69 66  * operation - if
185fb 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 20   it is not, the 
185fc 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20 6e  following is a n
185fd 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 66 28  o-op).  */.  if(
185fe 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d   pCur->pKeyInfo=
185ff 3d 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69  =0 ){.    invali
18600 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73  dateIncrblobCurs
18601 6f 72 73 28 70 2c 20 6e 4b 65 79 2c 20 30 29 3b  ors(p, nKey, 0);
18602 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20  .  }..  /* Save 
18603 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66  the positions of
18604 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73 6f   any other curso
18605 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20  rs open on this 
18606 74 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  table..  **.  **
18607 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c 20   In some cases, 
18608 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 74 72 65  the call to btre
18609 65 4d 6f 76 65 74 6f 28 29 20 62 65 6c 6f 77 20  eMoveto() below 
1860a 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a  is a no-op. For.
1860b 20 20 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68    ** example, wh
1860c 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 64 61 74  en inserting dat
1860d 61 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 77  a into a table w
1860e 69 74 68 20 61 75 74 6f 2d 67 65 6e 65 72 61 74  ith auto-generat
1860f 65 64 20 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20  ed integer.  ** 
18610 6b 65 79 73 2c 20 74 68 65 20 56 44 42 45 20 6c  keys, the VDBE l
18611 61 79 65 72 20 69 6e 76 6f 6b 65 73 20 73 71 6c  ayer invokes sql
18612 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20  ite3BtreeLast() 
18613 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68  to figure out th
18614 65 20 0a 20 20 2a 2a 20 69 6e 74 65 67 65 72 20  e .  ** integer 
18615 6b 65 79 20 74 6f 20 75 73 65 2e 20 49 74 20 74  key to use. It t
18616 68 65 6e 20 63 61 6c 6c 73 20 74 68 69 73 20 66  hen calls this f
18617 75 6e 63 74 69 6f 6e 20 74 6f 20 61 63 74 75 61  unction to actua
18618 6c 6c 79 20 69 6e 73 65 72 74 20 74 68 65 20 0a  lly insert the .
18619 20 20 2a 2a 20 64 61 74 61 20 69 6e 74 6f 20 74    ** data into t
1861a 68 65 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65  he intkey B-Tree
1861b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 62  . In this case b
1861c 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 72 65 63  treeMoveto() rec
1861d 6f 67 6e 69 7a 65 73 0a 20 20 2a 2a 20 74 68 61  ognizes.  ** tha
1861e 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  t the cursor is 
1861f 61 6c 72 65 61 64 79 20 77 68 65 72 65 20 69 74  already where it
18620 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6e 64   needs to be and
18621 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74   returns without
18622 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 6e 79 20  .  ** doing any 
18623 77 6f 72 6b 2e 20 54 6f 20 61 76 6f 69 64 20 74  work. To avoid t
18624 68 77 61 72 74 69 6e 67 20 74 68 65 73 65 20 6f  hwarting these o
18625 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2c 20 69 74  ptimizations, it
18626 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 0a 20 20   is important.  
18627 2a 2a 20 6e 6f 74 20 74 6f 20 63 6c 65 61 72 20  ** not to clear 
18628 74 68 65 20 63 75 72 73 6f 72 20 68 65 72 65 2e  the cursor here.
18629 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76  .  */.  rc = sav
1862a 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c  eAllCursors(pBt,
1862b 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
1862c 20 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63   pCur);.  if( rc
1862d 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
1862e 69 66 28 20 21 6c 6f 63 20 29 7b 0a 20 20 20 20  if( !loc ){.    
1862f 72 63 20 3d 20 62 74 72 65 65 4d 6f 76 65 74 6f  rc = btreeMoveto
18630 28 70 43 75 72 2c 20 70 4b 65 79 2c 20 6e 4b 65  (pCur, pKey, nKe
18631 79 2c 20 61 70 70 65 6e 64 42 69 61 73 2c 20 26  y, appendBias, &
18632 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63  loc);.    if( rc
18633 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
18634 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  }.  assert( pCur
18635 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
18636 5f 56 41 4c 49 44 20 7c 7c 20 28 70 43 75 72 2d  _VALID || (pCur-
18637 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
18638 49 4e 56 41 4c 49 44 20 26 26 20 6c 6f 63 29 20  INVALID && loc) 
18639 29 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43  );..  pPage = pC
1863a 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1863b 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72  >iPage];.  asser
1863c 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  t( pPage->intKey
1863d 20 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20   || nKey>=0 );. 
1863e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1863f 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e  leaf || !pPage->
18640 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 54 52 41  intKey );..  TRA
18641 43 45 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62  CE(("INSERT: tab
18642 6c 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20  le=%d nkey=%lld 
18643 6e 64 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64  ndata=%d page=%d
18644 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
18645 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74    pCur->pgnoRoot
18646 2c 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70  , nKey, nData, p
18647 50 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20  Page->pgno,.    
18648 20 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22        loc==0 ? "
18649 6f 76 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65  overwrite" : "ne
1864a 77 20 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73  w entry"));.  as
1864b 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
1864c 6e 69 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74  nit );.  allocat
1864d 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
1864e 0a 20 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74  .  newCell = pBt
1864f 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69  ->pTmpSpace;.  i
18650 66 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20  f( newCell==0 ) 
18651 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
18652 4d 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c  MEM;.  rc = fill
18653 49 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65  InCell(pPage, ne
18654 77 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65  wCell, pKey, nKe
18655 79 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  y, pData, nData,
18656 20 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b   nZero, &szNew);
18657 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f  .  if( rc ) goto
18658 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61   end_insert;.  a
18659 73 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65  ssert( szNew==ce
1865a 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
1865b 20 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61   newCell) );.  a
1865c 73 73 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58  ssert( szNew<=MX
1865d 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20  _CELL_SIZE(pBt) 
1865e 29 3b 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d  );.  idx = pCur-
1865f 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
18660 67 65 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d  ge];.  if( loc==
18661 30 20 29 7b 0a 20 20 20 20 75 31 36 20 73 7a 4f  0 ){.    u16 szO
18662 6c 64 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ld;.    assert( 
18663 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  idx<pPage->nCell
18664 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
18665 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
18666 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
18667 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
18668 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73      goto end_ins
18669 65 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f  ert;.    }.    o
1866a 6c 64 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c  ldCell = findCel
1866b 6c 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20  l(pPage, idx);. 
1866c 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
1866d 65 61 66 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  eaf ){.      mem
1866e 63 70 79 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64  cpy(newCell, old
1866f 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a  Cell, 4);.    }.
18670 20 20 20 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c      szOld = cell
18671 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6f  SizePtr(pPage, o
18672 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20  ldCell);.    rc 
18673 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67  = clearCell(pPag
18674 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20  e, oldCell);.   
18675 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c   dropCell(pPage,
18676 20 69 64 78 2c 20 73 7a 4f 6c 64 2c 20 26 72 63   idx, szOld, &rc
18677 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
18678 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b  goto end_insert;
18679 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63  .  }else if( loc
1867a 3c 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65  <0 && pPage->nCe
1867b 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65  ll>0 ){.    asse
1867c 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  rt( pPage->leaf 
1867d 29 3b 0a 20 20 20 20 69 64 78 20 3d 20 2b 2b 70  );.    idx = ++p
1867e 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
1867f 3e 69 50 61 67 65 5d 3b 0a 20 20 7d 65 6c 73 65  >iPage];.  }else
18680 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
18681 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d  age->leaf );.  }
18682 0a 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50  .  insertCell(pP
18683 61 67 65 2c 20 69 64 78 2c 20 6e 65 77 43 65 6c  age, idx, newCel
18684 6c 2c 20 73 7a 4e 65 77 2c 20 30 2c 20 30 2c 20  l, szNew, 0, 0, 
18685 26 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20  &rc);.  assert( 
18686 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
18687 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20   pPage->nCell>0 
18688 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  || pPage->nOverf
18689 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49  low>0 );..  /* I
1868a 66 20 6e 6f 20 65 72 72 6f 72 20 68 61 73 20 6f  f no error has o
1868b 63 63 75 72 65 64 20 61 6e 64 20 70 50 61 67 65  ccured and pPage
1868c 20 68 61 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77   has an overflow
1868d 20 63 65 6c 6c 2c 20 63 61 6c 6c 20 62 61 6c 61   cell, call bala
1868e 6e 63 65 28 29 20 0a 20 20 2a 2a 20 74 6f 20 72  nce() .  ** to r
1868f 65 64 69 73 74 72 69 62 75 74 65 20 74 68 65 20  edistribute the 
18690 63 65 6c 6c 73 20 77 69 74 68 69 6e 20 74 68 65  cells within the
18691 20 74 72 65 65 2e 20 53 69 6e 63 65 20 62 61 6c   tree. Since bal
18692 61 6e 63 65 28 29 20 6d 61 79 20 6d 6f 76 65 0a  ance() may move.
18693 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2c    ** the cursor,
18694 20 7a 65 72 6f 20 74 68 65 20 42 74 43 75 72 73   zero the BtCurs
18695 6f 72 2e 69 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e  or.info.nSize an
18696 64 20 42 74 43 75 72 73 6f 72 2e 76 61 6c 69 64  d BtCursor.valid
18697 4e 4b 65 79 0a 20 20 2a 2a 20 76 61 72 69 61 62  NKey.  ** variab
18698 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50  les..  **.  ** P
18699 72 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73  revious versions
1869a 20 6f 66 20 53 51 4c 69 74 65 20 63 61 6c 6c 65   of SQLite calle
1869b 64 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 74  d moveToRoot() t
1869c 6f 20 6d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  o move the curso
1869d 72 0a 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 74  r.  ** back to t
1869e 68 65 20 72 6f 6f 74 20 70 61 67 65 20 61 73 20  he root page as 
1869f 62 61 6c 61 6e 63 65 28 29 20 75 73 65 64 20 74  balance() used t
186a0 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65  o invalidate the
186a1 20 63 6f 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 6f   contents.  ** o
186a2 66 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61 67  f BtCursor.apPag
186a3 65 5b 5d 20 61 6e 64 20 42 74 43 75 72 73 6f 72  e[] and BtCursor
186a4 2e 61 69 49 64 78 5b 5d 2e 20 49 6e 73 74 65 61  .aiIdx[]. Instea
186a5 64 20 6f 66 20 64 6f 69 6e 67 20 74 68 61 74 2c  d of doing that,
186a6 0a 20 20 2a 2a 20 73 65 74 20 74 68 65 20 63 75  .  ** set the cu
186a7 72 73 6f 72 20 73 74 61 74 65 20 74 6f 20 22 69  rsor state to "i
186a8 6e 76 61 6c 69 64 22 2e 20 54 68 69 73 20 6d 61  nvalid". This ma
186a9 6b 65 73 20 63 6f 6d 6d 6f 6e 20 69 6e 73 65 72  kes common inser
186aa 74 20 6f 70 65 72 61 74 69 6f 6e 73 0a 20 20 2a  t operations.  *
186ab 2a 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65  * slightly faste
186ac 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  r..  **.  ** The
186ad 72 65 20 69 73 20 61 20 73 75 62 74 6c 65 20 62  re is a subtle b
186ae 75 74 20 69 6d 70 6f 72 74 61 6e 74 20 6f 70 74  ut important opt
186af 69 6d 69 7a 61 74 69 6f 6e 20 68 65 72 65 20 74  imization here t
186b0 6f 6f 2e 20 57 68 65 6e 20 69 6e 73 65 72 74 69  oo. When inserti
186b1 6e 67 0a 20 20 2a 2a 20 6d 75 6c 74 69 70 6c 65  ng.  ** multiple
186b2 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 6e   records into an
186b3 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 20 75   intkey b-tree u
186b4 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 63 75  sing a single cu
186b5 72 73 6f 72 20 28 61 73 20 63 61 6e 0a 20 20 2a  rsor (as can.  *
186b6 2a 20 68 61 70 70 65 6e 20 77 68 69 6c 65 20 70  * happen while p
186b7 72 6f 63 65 73 73 69 6e 67 20 61 6e 20 22 49 4e  rocessing an "IN
186b8 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45  SERT INTO ... SE
186b9 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e 74 29  LECT" statement)
186ba 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 64 76  , it.  ** is adv
186bb 61 6e 74 61 67 65 6f 75 73 20 74 6f 20 6c 65 61  antageous to lea
186bc 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f  ve the cursor po
186bd 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61  inting to the la
186be 73 74 20 65 6e 74 72 79 20 69 6e 0a 20 20 2a 2a  st entry in.  **
186bf 20 74 68 65 20 62 2d 74 72 65 65 20 69 66 20 70   the b-tree if p
186c0 6f 73 73 69 62 6c 65 2e 20 49 66 20 74 68 65 20  ossible. If the 
186c1 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70  cursor is left p
186c2 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c  ointing to the l
186c3 61 73 74 0a 20 20 2a 2a 20 65 6e 74 72 79 20 69  ast.  ** entry i
186c4 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64  n the table, and
186c5 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e   the next row in
186c6 73 65 72 74 65 64 20 68 61 73 20 61 6e 20 69 6e  serted has an in
186c7 74 65 67 65 72 20 6b 65 79 0a 20 20 2a 2a 20 6c  teger key.  ** l
186c8 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6c  arger than the l
186c9 61 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20  argest existing 
186ca 6b 65 79 2c 20 69 74 20 69 73 20 70 6f 73 73 69  key, it is possi
186cb 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 74 68  ble to insert th
186cc 65 0a 20 20 2a 2a 20 72 6f 77 20 77 69 74 68 6f  e.  ** row witho
186cd 75 74 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63  ut seeking the c
186ce 75 72 73 6f 72 2e 20 54 68 69 73 20 63 61 6e 20  ursor. This can 
186cf 62 65 20 61 20 62 69 67 20 70 65 72 66 6f 72 6d  be a big perform
186d0 61 6e 63 65 20 62 6f 6f 73 74 2e 0a 20 20 2a 2f  ance boost..  */
186d1 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53  .  pCur->info.nS
186d2 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d  ize = 0;.  pCur-
186d3 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
186d4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
186d5 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f  _OK && pPage->nO
186d6 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 72  verflow ){.    r
186d7 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72  c = balance(pCur
186d8 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20  );..    /* Must 
186d9 6d 61 6b 65 20 73 75 72 65 20 6e 4f 76 65 72 66  make sure nOverf
186da 6c 6f 77 20 69 73 20 72 65 73 65 74 20 74 6f 20  low is reset to 
186db 7a 65 72 6f 20 65 76 65 6e 20 69 66 20 74 68 65  zero even if the
186dc 20 62 61 6c 61 6e 63 65 28 29 0a 20 20 20 20 2a   balance().    *
186dd 2a 20 66 61 69 6c 73 2e 20 49 6e 74 65 72 6e 61  * fails. Interna
186de 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  l data structure
186df 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c   corruption will
186e0 20 72 65 73 75 6c 74 20 6f 74 68 65 72 77 69 73   result otherwis
186e1 65 2e 20 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c  e. .    ** Also,
186e2 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20   set the cursor 
186e3 73 74 61 74 65 20 74 6f 20 69 6e 76 61 6c 69 64  state to invalid
186e4 2e 20 54 68 69 73 20 73 74 6f 70 73 20 73 61 76  . This stops sav
186e5 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
186e6 29 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 72  ).    ** from tr
186e7 79 69 6e 67 20 74 6f 20 73 61 76 65 20 74 68 65  ying to save the
186e8 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f   current positio
186e9 6e 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e  n of the cursor.
186ea 20 20 2a 2f 0a 20 20 20 20 70 43 75 72 2d 3e 61    */.    pCur->a
186eb 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
186ec 65 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20  e]->nOverflow = 
186ed 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74  0;.    pCur->eSt
186ee 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
186ef 41 4c 49 44 3b 0a 20 20 7d 0a 20 20 61 73 73 65  ALID;.  }.  asse
186f0 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65  rt( pCur->apPage
186f1 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
186f2 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a  Overflow==0 );..
186f3 65 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65  end_insert:.  re
186f4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
186f5 2a 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74  * Delete the ent
186f6 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73  ry that the curs
186f7 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
186f8 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a  o.  The cursor.*
186f9 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  * is left pointi
186fa 6e 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72  ng at a arbitrar
186fb 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  y location..*/.S
186fc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
186fd 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  t sqlite3BtreeDe
186fe 6c 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70  lete(BtCursor *p
186ff 43 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70  Cur){.  Btree *p
18700 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b   = pCur->pBtree;
18701 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
18702 20 3d 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20   = p->pBt;      
18703 20 20 20 20 20 20 20 20 0a 20 20 69 6e 74 20 72          .  int r
18704 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
18705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18706 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
18707 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  /.  MemPage *pPa
18708 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
18709 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
1870a 20 74 6f 20 64 65 6c 65 74 65 20 63 65 6c 6c 20   to delete cell 
1870b 66 72 6f 6d 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  from */.  unsign
1870c 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 20  ed char *pCell; 
1870d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1870e 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 65 6c  * Pointer to cel
1870f 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20  l to delete */. 
18710 20 69 6e 74 20 69 43 65 6c 6c 49 64 78 3b 20 20   int iCellIdx;  
18711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18712 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
18713 66 20 63 65 6c 6c 20 74 6f 20 64 65 6c 65 74 65  f cell to delete
18714 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 44   */.  int iCellD
18715 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  epth;           
18716 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
18717 70 74 68 20 6f 66 20 6e 6f 64 65 20 63 6f 6e 74  pth of node cont
18718 61 69 6e 69 6e 67 20 70 43 65 6c 6c 20 2a 2f 20  aining pCell */ 
18719 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
1871a 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
1871b 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
1871c 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
1871d 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  on==TRANS_WRITE 
1871e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 42  );.  assert( !pB
1871f 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20  t->readOnly );. 
18720 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77   assert( pCur->w
18721 72 46 6c 61 67 20 29 3b 0a 20 20 61 73 73 65 72  rFlag );.  asser
18722 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68  t( hasSharedCach
18723 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43  eTableLock(p, pC
18724 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43  ur->pgnoRoot, pC
18725 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c  ur->pKeyInfo!=0,
18726 20 32 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28   2) );.  assert(
18727 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63   !hasReadConflic
18728 74 73 28 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f  ts(p, pCur->pgno
18729 52 6f 6f 74 29 20 29 3b 0a 0a 20 20 69 66 28 20  Root) );..  if( 
1872a 4e 45 56 45 52 28 70 43 75 72 2d 3e 61 69 49 64  NEVER(pCur->aiId
1872b 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d  x[pCur->iPage]>=
1872c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1872d 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
1872e 29 20 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28 70  ) .   || NEVER(p
1872f 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52  Cur->eState!=CUR
18730 53 4f 52 5f 56 41 4c 49 44 29 0a 20 20 29 7b 0a  SOR_VALID).  ){.
18731 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
18732 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 53 6f 6d  E_ERROR;  /* Som
18733 65 74 68 69 6e 67 20 68 61 73 20 67 6f 6e 65 20  ething has gone 
18734 61 77 72 79 2e 20 2a 2f 0a 20 20 7d 0a 0a 20 20  awry. */.  }..  
18735 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
18736 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e  delete operation
18737 20 74 6f 20 72 65 6d 6f 76 65 20 61 20 72 6f 77   to remove a row
18738 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 62 2d   from a table b-
18739 74 72 65 65 2c 0a 20 20 2a 2a 20 69 6e 76 61 6c  tree,.  ** inval
1873a 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c  idate any incrbl
1873b 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20  ob cursors open 
1873c 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  on the row being
1873d 20 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a 20 20   deleted.  */.  
1873e 69 66 28 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e  if( pCur->pKeyIn
1873f 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 76  fo==0 ){.    inv
18740 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43  alidateIncrblobC
18741 75 72 73 6f 72 73 28 70 2c 20 70 43 75 72 2d 3e  ursors(p, pCur->
18742 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 29 3b 0a 20  info.nKey, 0);. 
18743 20 7d 0a 0a 20 20 69 43 65 6c 6c 44 65 70 74 68   }..  iCellDepth
18744 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a   = pCur->iPage;.
18745 20 20 69 43 65 6c 6c 49 64 78 20 3d 20 70 43 75    iCellIdx = pCu
18746 72 2d 3e 61 69 49 64 78 5b 69 43 65 6c 6c 44 65  r->aiIdx[iCellDe
18747 70 74 68 5d 3b 0a 20 20 70 50 61 67 65 20 3d 20  pth];.  pPage = 
18748 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 65  pCur->apPage[iCe
18749 6c 6c 44 65 70 74 68 5d 3b 0a 20 20 70 43 65 6c  llDepth];.  pCel
1874a 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  l = findCell(pPa
1874b 67 65 2c 20 69 43 65 6c 6c 49 64 78 29 3b 0a 0a  ge, iCellIdx);..
1874c 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65    /* If the page
1874d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1874e 65 6e 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20  entry to delete 
1874f 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61  is not a leaf pa
18750 67 65 2c 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 68  ge, move.  ** th
18751 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
18752 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 69 6e  largest entry in
18753 20 74 68 65 20 74 72 65 65 20 74 68 61 74 20 69   the tree that i
18754 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 0a 20  s smaller than. 
18755 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 62 65   ** the entry be
18756 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69  ing deleted. Thi
18757 73 20 63 65 6c 6c 20 77 69 6c 6c 20 72 65 70 6c  s cell will repl
18758 61 63 65 20 74 68 65 20 63 65 6c 6c 20 62 65 69  ace the cell bei
18759 6e 67 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20  ng deleted.  ** 
1875a 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61  from the interna
1875b 6c 20 6e 6f 64 65 2e 20 54 68 65 20 27 70 72 65  l node. The 'pre
1875c 76 69 6f 75 73 27 20 65 6e 74 72 79 20 69 73 20  vious' entry is 
1875d 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 69 6e  used for this in
1875e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 74 68  stead.  ** of th
1875f 65 20 27 6e 65 78 74 27 20 65 6e 74 72 79 2c 20  e 'next' entry, 
18760 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  as the previous 
18761 65 6e 74 72 79 20 69 73 20 61 6c 77 61 79 73 20  entry is always 
18762 61 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 20  a part of the.  
18763 2a 2a 20 73 75 62 2d 74 72 65 65 20 68 65 61 64  ** sub-tree head
18764 65 64 20 62 79 20 74 68 65 20 63 68 69 6c 64 20  ed by the child 
18765 70 61 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  page of the cell
18766 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20   being deleted. 
18767 54 68 69 73 20 6d 61 6b 65 73 0a 20 20 2a 2a 20  This makes.  ** 
18768 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 74 72  balancing the tr
18769 65 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ee following the
1876a 20 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f   delete operatio
1876b 6e 20 65 61 73 69 65 72 2e 20 20 2a 2f 0a 20 20  n easier.  */.  
1876c 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
1876d 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55   ){.    int notU
1876e 73 65 64 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  sed;.    rc = sq
1876f 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f  lite3BtreePrevio
18770 75 73 28 70 43 75 72 2c 20 26 6e 6f 74 55 73 65  us(pCur, &notUse
18771 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  d);.    if( rc )
18772 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
18773 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70  .  /* Save the p
18774 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20  ositions of any 
18775 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70  other cursors op
18776 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65  en on this table
18777 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 6d 61 6b   before.  ** mak
18778 69 6e 67 20 61 6e 79 20 6d 6f 64 69 66 69 63 61  ing any modifica
18779 74 69 6f 6e 73 2e 20 4d 61 6b 65 20 74 68 65 20  tions. Make the 
1877a 70 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  page containing 
1877b 74 68 65 20 65 6e 74 72 79 20 74 6f 20 62 65 20  the entry to be 
1877c 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 77 72  .  ** deleted wr
1877d 69 74 61 62 6c 65 2e 20 54 68 65 6e 20 66 72 65  itable. Then fre
1877e 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70  e any overflow p
1877f 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ages associated 
18780 77 69 74 68 20 74 68 65 20 0a 20 20 2a 2a 20 65  with the .  ** e
18781 6e 74 72 79 20 61 6e 64 20 66 69 6e 61 6c 6c 79  ntry and finally
18782 20 72 65 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c   remove the cell
18783 20 69 74 73 65 6c 66 20 66 72 6f 6d 20 77 69 74   itself from wit
18784 68 69 6e 20 74 68 65 20 70 61 67 65 2e 20 20 0a  hin the page.  .
18785 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 65    */.  rc = save
18786 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20  AllCursors(pBt, 
18787 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20  pCur->pgnoRoot, 
18788 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20  pCur);.  if( rc 
18789 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
1878a 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1878b 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
1878c 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20  Page);.  if( rc 
1878d 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
1878e 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50  c = clearCell(pP
1878f 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 64  age, pCell);.  d
18790 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  ropCell(pPage, i
18791 43 65 6c 6c 49 64 78 2c 20 63 65 6c 6c 53 69 7a  CellIdx, cellSiz
18792 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  ePtr(pPage, pCel
18793 6c 29 2c 20 26 72 63 29 3b 0a 20 20 69 66 28 20  l), &rc);.  if( 
18794 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
18795 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c  .  /* If the cel
18796 6c 20 64 65 6c 65 74 65 64 20 77 61 73 20 6e 6f  l deleted was no
18797 74 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 6c  t located on a l
18798 65 61 66 20 70 61 67 65 2c 20 74 68 65 6e 20 74  eaf page, then t
18799 68 65 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 69  he cursor.  ** i
1879a 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
1879b 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 72 67  ting to the larg
1879c 65 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  est entry in the
1879d 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64   sub-tree headed
1879e 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 63 68 69  .  ** by the chi
1879f 6c 64 2d 70 61 67 65 20 6f 66 20 74 68 65 20 63  ld-page of the c
187a0 65 6c 6c 20 74 68 61 74 20 77 61 73 20 6a 75 73  ell that was jus
187a1 74 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 61  t deleted from a
187a2 6e 20 69 6e 74 65 72 6e 61 6c 0a 20 20 2a 2a 20  n internal.  ** 
187a3 6e 6f 64 65 2e 20 54 68 65 20 63 65 6c 6c 20 66  node. The cell f
187a4 72 6f 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f 64  rom the leaf nod
187a5 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6d 6f  e needs to be mo
187a6 76 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72  ved to the inter
187a7 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 20 74 6f  nal.  ** node to
187a8 20 72 65 70 6c 61 63 65 20 74 68 65 20 64 65 6c   replace the del
187a9 65 74 65 64 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20  eted cell.  */. 
187aa 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
187ab 66 20 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65  f ){.    MemPage
187ac 20 2a 70 4c 65 61 66 20 3d 20 70 43 75 72 2d 3e   *pLeaf = pCur->
187ad 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
187ae 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65  ge];.    int nCe
187af 6c 6c 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 20 3d  ll;.    Pgno n =
187b0 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 43   pCur->apPage[iC
187b1 65 6c 6c 44 65 70 74 68 2b 31 5d 2d 3e 70 67 6e  ellDepth+1]->pgn
187b2 6f 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  o;.    unsigned 
187b3 63 68 61 72 20 2a 70 54 6d 70 3b 0a 0a 20 20 20  char *pTmp;..   
187b4 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c   pCell = findCel
187b5 6c 28 70 4c 65 61 66 2c 20 70 4c 65 61 66 2d 3e  l(pLeaf, pLeaf->
187b6 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 6e 43  nCell-1);.    nC
187b7 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  ell = cellSizePt
187b8 72 28 70 4c 65 61 66 2c 20 70 43 65 6c 6c 29 3b  r(pLeaf, pCell);
187b9 0a 20 20 20 20 61 73 73 65 72 74 28 20 4d 58 5f  .    assert( MX_
187ba 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 3e 3d  CELL_SIZE(pBt)>=
187bb 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 61 6c  nCell );..    al
187bc 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28  locateTempSpace(
187bd 70 42 74 29 3b 0a 20 20 20 20 70 54 6d 70 20 3d  pBt);.    pTmp =
187be 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b   pBt->pTmpSpace;
187bf 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
187c0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4c 65  e3PagerWrite(pLe
187c1 61 66 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  af->pDbPage);.  
187c2 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61    insertCell(pPa
187c3 67 65 2c 20 69 43 65 6c 6c 49 64 78 2c 20 70 43  ge, iCellIdx, pC
187c4 65 6c 6c 2d 34 2c 20 6e 43 65 6c 6c 2b 34 2c 20  ell-4, nCell+4, 
187c5 70 54 6d 70 2c 20 6e 2c 20 26 72 63 29 3b 0a 20  pTmp, n, &rc);. 
187c6 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 4c 65 61     dropCell(pLea
187c7 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d  f, pLeaf->nCell-
187c8 31 2c 20 6e 43 65 6c 6c 2c 20 26 72 63 29 3b 0a  1, nCell, &rc);.
187c9 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
187ca 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
187cb 2a 20 42 61 6c 61 6e 63 65 20 74 68 65 20 74 72  * Balance the tr
187cc 65 65 2e 20 49 66 20 74 68 65 20 65 6e 74 72 79  ee. If the entry
187cd 20 64 65 6c 65 74 65 64 20 77 61 73 20 6c 6f 63   deleted was loc
187ce 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70  ated on a leaf p
187cf 61 67 65 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74  age,.  ** then t
187d0 68 65 20 63 75 72 73 6f 72 20 73 74 69 6c 6c 20  he cursor still 
187d1 70 6f 69 6e 74 73 20 74 6f 20 74 68 61 74 20 70  points to that p
187d2 61 67 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  age. In this cas
187d3 65 20 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a  e the first.  **
187d4 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65   call to balance
187d5 28 29 20 72 65 70 61 69 72 73 20 74 68 65 20 74  () repairs the t
187d6 72 65 65 2c 20 61 6e 64 20 74 68 65 20 69 66 28  ree, and the if(
187d7 2e 2e 2e 29 20 63 6f 6e 64 69 74 69 6f 6e 20 69  ...) condition i
187d8 73 0a 20 20 2a 2a 20 6e 65 76 65 72 20 74 72 75  s.  ** never tru
187d9 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68  e..  **.  ** Oth
187da 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 65  erwise, if the e
187db 6e 74 72 79 20 64 65 6c 65 74 65 64 20 77 61 73  ntry deleted was
187dc 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   on an internal 
187dd 6e 6f 64 65 20 70 61 67 65 2c 20 74 68 65 6e 0a  node page, then.
187de 20 20 2a 2a 20 70 43 75 72 20 69 73 20 70 6f 69    ** pCur is poi
187df 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 65 61  nting to the lea
187e0 66 20 70 61 67 65 20 66 72 6f 6d 20 77 68 69 63  f page from whic
187e1 68 20 61 20 63 65 6c 6c 20 77 61 73 20 72 65 6d  h a cell was rem
187e2 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 72 65 70  oved to.  ** rep
187e3 6c 61 63 65 20 74 68 65 20 63 65 6c 6c 20 64 65  lace the cell de
187e4 6c 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20 69  leted from the i
187e5 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 20 54 68  nternal node. Th
187e6 69 73 20 69 73 20 73 6c 69 67 68 74 6c 79 0a 20  is is slightly. 
187e7 20 2a 2a 20 74 72 69 63 6b 79 20 61 73 20 74 68   ** tricky as th
187e8 65 20 6c 65 61 66 20 6e 6f 64 65 20 6d 61 79 20  e leaf node may 
187e9 62 65 20 75 6e 64 65 72 66 75 6c 6c 2c 20 61 6e  be underfull, an
187ea 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e  d the internal n
187eb 6f 64 65 20 6d 61 79 0a 20 20 2a 2a 20 62 65 20  ode may.  ** be 
187ec 65 69 74 68 65 72 20 75 6e 64 65 72 20 6f 72 20  either under or 
187ed 6f 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69  overfull. In thi
187ee 73 20 63 61 73 65 20 72 75 6e 20 74 68 65 20 62  s case run the b
187ef 61 6c 61 6e 63 69 6e 67 20 61 6c 67 6f 72 69 74  alancing algorit
187f0 68 6d 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c  hm.  ** on the l
187f1 65 61 66 20 6e 6f 64 65 20 66 69 72 73 74 2e 20  eaf node first. 
187f2 49 66 20 74 68 65 20 62 61 6c 61 6e 63 65 20 70  If the balance p
187f3 72 6f 63 65 65 64 73 20 66 61 72 20 65 6e 6f 75  roceeds far enou
187f4 67 68 20 75 70 20 74 68 65 0a 20 20 2a 2a 20 74  gh up the.  ** t
187f5 72 65 65 20 74 68 61 74 20 77 65 20 63 61 6e 20  ree that we can 
187f6 62 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 79  be sure that any
187f7 20 70 72 6f 62 6c 65 6d 20 69 6e 20 74 68 65 20   problem in the 
187f8 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 68 61  internal node ha
187f9 73 0a 20 20 2a 2a 20 62 65 65 6e 20 63 6f 72 72  s.  ** been corr
187fa 65 63 74 65 64 2c 20 73 6f 20 62 65 20 69 74 2e  ected, so be it.
187fb 20 4f 74 68 65 72 77 69 73 65 2c 20 61 66 74 65   Otherwise, afte
187fc 72 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20  r balancing the 
187fd 6c 65 61 66 20 6e 6f 64 65 2c 0a 20 20 2a 2a 20  leaf node,.  ** 
187fe 77 61 6c 6b 20 74 68 65 20 63 75 72 73 6f 72 20  walk the cursor 
187ff 75 70 20 74 68 65 20 74 72 65 65 20 74 6f 20 74  up the tree to t
18800 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  he internal node
18801 20 61 6e 64 20 62 61 6c 61 6e 63 65 20 69 74 20   and balance it 
18802 61 73 20 0a 20 20 2a 2a 20 77 65 6c 6c 2e 20 20  as .  ** well.  
18803 2a 2f 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63  */.  rc = balanc
18804 65 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  e(pCur);.  if( r
18805 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
18806 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c  pCur->iPage>iCel
18807 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 77 68  lDepth ){.    wh
18808 69 6c 65 28 20 70 43 75 72 2d 3e 69 50 61 67 65  ile( pCur->iPage
18809 3e 69 43 65 6c 6c 44 65 70 74 68 20 29 7b 0a 20  >iCellDepth ){. 
1880a 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1880b 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  (pCur->apPage[pC
1880c 75 72 2d 3e 69 50 61 67 65 2d 2d 5d 29 3b 0a 20  ur->iPage--]);. 
1880d 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 62 61     }.    rc = ba
1880e 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 20 20 7d  lance(pCur);.  }
1880f 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
18810 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76  TE_OK ){.    mov
18811 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20  eToRoot(pCur);. 
18812 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
18813 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
18814 61 20 6e 65 77 20 42 54 72 65 65 20 74 61 62 6c  a new BTree tabl
18815 65 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a  e.  Write into *
18816 70 69 54 61 62 6c 65 20 74 68 65 20 70 61 67 65  piTable the page
18817 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  .** number for t
18818 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
18819 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a  the new table..*
1881a 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 6f 66  *.** The type of
1881b 20 74 79 70 65 20 69 73 20 64 65 74 65 72 6d 69   type is determi
1881c 6e 65 64 20 62 79 20 74 68 65 20 66 6c 61 67 73  ned by the flags
1881d 20 70 61 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c   parameter.  Onl
1881e 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  y the.** followi
1881f 6e 67 20 76 61 6c 75 65 73 20 6f 66 20 66 6c 61  ng values of fla
18820 67 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  gs are currently
18821 20 69 6e 20 75 73 65 2e 20 20 4f 74 68 65 72 20   in use.  Other 
18822 76 61 6c 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c  values for.** fl
18823 61 67 73 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f  ags might not wo
18824 72 6b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54  rk:.**.**     BT
18825 52 45 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45  REE_INTKEY|BTREE
18826 5f 4c 45 41 46 44 41 54 41 20 20 20 20 20 55 73  _LEAFDATA     Us
18827 65 64 20 66 6f 72 20 53 51 4c 20 74 61 62 6c 65  ed for SQL table
18828 73 20 77 69 74 68 20 72 6f 77 69 64 20 6b 65 79  s with rowid key
18829 73 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 5a  s.**     BTREE_Z
1882a 45 52 4f 44 41 54 41 20 20 20 20 20 20 20 20 20  ERODATA         
1882b 20 20 20 20 20 20 20 20 20 55 73 65 64 20 66 6f           Used fo
1882c 72 20 53 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f  r SQL indices.*/
1882d 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
1882e 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72  eCreateTable(Btr
1882f 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61  ee *p, int *piTa
18830 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  ble, int flags){
18831 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
18832 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d   = p->pBt;.  Mem
18833 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50  Page *pRoot;.  P
18834 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20  gno pgnoRoot;.  
18835 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
18836 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
18837 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a  oldsMutex(p) );.
18838 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69    assert( pBt->i
18839 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
1883a 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61  ANS_WRITE );.  a
1883b 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61  ssert( !pBt->rea
1883c 64 4f 6e 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66  dOnly );..#ifdef
1883d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1883e 4f 56 41 43 55 55 4d 0a 20 20 72 63 20 3d 20 61  OVACUUM.  rc = a
1883f 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65  llocateBtreePage
18840 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70  (pBt, &pRoot, &p
18841 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a  gnoRoot, 1, 0);.
18842 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
18843 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23  return rc;.  }.#
18844 65 6c 73 65 0a 20 20 69 66 28 20 70 42 74 2d 3e  else.  if( pBt->
18845 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
18846 20 20 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b    Pgno pgnoMove;
18847 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20        /* Move a 
18848 70 61 67 65 20 68 65 72 65 20 74 6f 20 6d 61 6b  page here to mak
18849 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72  e room for the r
1884a 6f 6f 74 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20  oot-page */.    
1884b 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 4d 6f  MemPage *pPageMo
1884c 76 65 3b 20 2f 2a 20 54 68 65 20 70 61 67 65 20  ve; /* The page 
1884d 74 6f 20 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a  to move to. */..
1884e 20 20 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20      /* Creating 
1884f 61 20 6e 65 77 20 74 61 62 6c 65 20 6d 61 79 20  a new table may 
18850 70 72 6f 62 61 62 6c 79 20 72 65 71 75 69 72 65  probably require
18851 20 6d 6f 76 69 6e 67 20 61 6e 20 65 78 69 73 74   moving an exist
18852 69 6e 67 20 64 61 74 61 62 61 73 65 0a 20 20 20  ing database.   
18853 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d   ** to make room
18854 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62   for the new tab
18855 6c 65 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 49  les root page. I
18856 6e 20 63 61 73 65 20 74 68 69 73 20 70 61 67 65  n case this page
18857 20 74 75 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75   turns.    ** ou
18858 74 20 74 6f 20 62 65 20 61 6e 20 6f 76 65 72 66  t to be an overf
18859 6c 6f 77 20 70 61 67 65 2c 20 64 65 6c 65 74 65  low page, delete
1885a 20 61 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61   all overflow pa
1885b 67 65 2d 6d 61 70 20 63 61 63 68 65 73 0a 20 20  ge-map caches.  
1885c 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65    ** held by ope
1885d 6e 20 63 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a  n cursors..    *
1885e 2f 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65  /.    invalidate
1885f 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
18860 28 70 42 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52  (pBt);..    /* R
18861 65 61 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ead the value of
18862 20 6d 65 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68   meta[3] from th
18863 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 65  e database to de
18864 74 65 72 6d 69 6e 65 20 77 68 65 72 65 20 74 68  termine where th
18865 65 0a 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61  e.    ** root pa
18866 67 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61  ge of the new ta
18867 62 6c 65 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d  ble should go. m
18868 65 74 61 5b 33 5d 20 69 73 20 74 68 65 20 6c 61  eta[3] is the la
18869 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a  rgest root-page.
1886a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73      ** created s
1886b 6f 20 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65  o far, so the ne
1886c 77 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28  w root-page is (
1886d 6d 65 74 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20  meta[3]+1)..    
1886e 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  */.    sqlite3Bt
1886f 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54  reeGetMeta(p, BT
18870 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54  REE_LARGEST_ROOT
18871 5f 50 41 47 45 2c 20 26 70 67 6e 6f 52 6f 6f 74  _PAGE, &pgnoRoot
18872 29 3b 0a 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b  );.    pgnoRoot+
18873 2b 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e  +;..    /* The n
18874 65 77 20 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79  ew root-page may
18875 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65   not be allocate
18876 64 20 6f 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d  d on a pointer-m
18877 61 70 20 70 61 67 65 2c 20 6f 72 20 74 68 65 0a  ap page, or the.
18878 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42      ** PENDING_B
18879 59 54 45 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  YTE page..    */
1887a 0a 20 20 20 20 77 68 69 6c 65 28 20 70 67 6e 6f  .    while( pgno
1887b 52 6f 6f 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47  Root==PTRMAP_PAG
1887c 45 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f  ENO(pBt, pgnoRoo
1887d 74 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 70 67  t) ||.        pg
1887e 6e 6f 52 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f  noRoot==PENDING_
1887f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
18880 7b 0a 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74  {.      pgnoRoot
18881 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  ++;.    }.    as
18882 73 65 72 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d  sert( pgnoRoot>=
18883 33 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c  3 );..    /* All
18884 6f 63 61 74 65 20 61 20 70 61 67 65 2e 20 54 68  ocate a page. Th
18885 65 20 70 61 67 65 20 74 68 61 74 20 63 75 72 72  e page that curr
18886 65 6e 74 6c 79 20 72 65 73 69 64 65 73 20 61 74  ently resides at
18887 20 70 67 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20   pgnoRoot will. 
18888 20 20 20 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74     ** be moved t
18889 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  o the allocated 
1888a 70 61 67 65 20 28 75 6e 6c 65 73 73 20 74 68 65  page (unless the
1888b 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20   allocated page 
1888c 68 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74  happens.    ** t
1888d 6f 20 72 65 73 69 64 65 20 61 74 20 70 67 6e 6f  o reside at pgno
1888e 52 6f 6f 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  Root)..    */.  
1888f 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
18890 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
18891 50 61 67 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d  PageMove, &pgnoM
18892 6f 76 65 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31  ove, pgnoRoot, 1
18893 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
18894 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
18895 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
18896 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 67 6e 6f   }..    if( pgno
18897 4d 6f 76 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29  Move!=pgnoRoot )
18898 7b 0a 20 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52  {.      /* pgnoR
18899 6f 6f 74 20 69 73 20 74 68 65 20 70 61 67 65 20  oot is the page 
1889a 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65  that will be use
1889b 64 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70  d for the root-p
1889c 61 67 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20  age of.      ** 
1889d 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 61  the new table (a
1889e 73 73 75 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72  ssuming an error
1889f 20 64 69 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e   did not occur).
188a0 20 42 75 74 20 77 65 20 77 65 72 65 0a 20 20 20   But we were.   
188a1 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20     ** allocated 
188a2 70 67 6e 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71  pgnoMove. If req
188a3 75 69 72 65 64 20 28 69 2e 65 2e 20 69 66 20 69  uired (i.e. if i
188a4 74 20 77 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61  t was not alloca
188a5 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20  ted.      ** by 
188a6 65 78 74 65 6e 64 69 6e 67 20 74 68 65 20 66 69  extending the fi
188a7 6c 65 29 2c 20 74 68 65 20 63 75 72 72 65 6e 74  le), the current
188a8 20 70 61 67 65 20 61 74 20 70 6f 73 69 74 69 6f   page at positio
188a9 6e 20 70 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20  n pgnoMove.     
188aa 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 6a   ** is already j
188ab 6f 75 72 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20  ournaled..      
188ac 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 54 79 70  */.      u8 eTyp
188ad 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 50 67 6e  e = 0;.      Pgn
188ae 6f 20 69 50 74 72 50 61 67 65 20 3d 20 30 3b 0a  o iPtrPage = 0;.
188af 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
188b0 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a 0a  ge(pPageMove);..
188b1 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68        /* Move th
188b2 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79  e page currently
188b3 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f 20   at pgnoRoot to 
188b4 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20  pgnoMove. */.   
188b5 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74     rc = btreeGet
188b6 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f  Page(pBt, pgnoRo
188b7 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a  ot, &pRoot, 0);.
188b8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
188b9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
188ba 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
188bb 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d      }.      rc =
188bc 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20   ptrmapGet(pBt, 
188bd 70 67 6e 6f 52 6f 6f 74 2c 20 26 65 54 79 70 65  pgnoRoot, &eType
188be 2c 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20  , &iPtrPage);.  
188bf 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50      if( eType==P
188c0 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c  TRMAP_ROOTPAGE |
188c1 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  | eType==PTRMAP_
188c2 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20  FREEPAGE ){.    
188c3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
188c4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
188c5 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
188c6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
188c7 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
188c8 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20  Page(pRoot);.   
188c9 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
188ca 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
188cb 73 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52  sert( eType!=PTR
188cc 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a  MAP_ROOTPAGE );.
188cd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54        assert( eT
188ce 79 70 65 21 3d 50 54 52 4d 41 50 5f 46 52 45 45  ype!=PTRMAP_FREE
188cf 50 41 47 45 20 29 3b 0a 20 20 20 20 20 20 72 63  PAGE );.      rc
188d0 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28   = relocatePage(
188d1 70 42 74 2c 20 70 52 6f 6f 74 2c 20 65 54 79 70  pBt, pRoot, eTyp
188d2 65 2c 20 69 50 74 72 50 61 67 65 2c 20 70 67 6e  e, iPtrPage, pgn
188d3 6f 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20  oMove, 0);.     
188d4 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
188d5 6f 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f  ot);..      /* O
188d6 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 61  btain the page a
188d7 74 20 70 67 6e 6f 52 6f 6f 74 20 2a 2f 0a 20 20  t pgnoRoot */.  
188d8 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
188d9 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
188da 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
188db 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 62    }.      rc = b
188dc 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
188dd 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f   pgnoRoot, &pRoo
188de 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  t, 0);.      if(
188df 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
188e0 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
188e1 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
188e2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
188e3 61 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d  agerWrite(pRoot-
188e4 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
188e5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
188e6 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
188e7 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
188e8 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
188e9 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
188ea 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52   }else{.      pR
188eb 6f 6f 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b  oot = pPageMove;
188ec 0a 20 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20  .    } ..    /* 
188ed 55 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74  Update the point
188ee 65 72 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d  er-map and meta-
188ef 64 61 74 61 20 77 69 74 68 20 74 68 65 20 6e 65  data with the ne
188f0 77 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62  w root-page numb
188f1 65 72 2e 20 2a 2f 0a 20 20 20 20 70 74 72 6d 61  er. */.    ptrma
188f2 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f  pPut(pBt, pgnoRo
188f3 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50  ot, PTRMAP_ROOTP
188f4 41 47 45 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20  AGE, 0, &rc);.  
188f5 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
188f6 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52    releasePage(pR
188f7 6f 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  oot);.      retu
188f8 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
188f9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
188fa 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20  eeUpdateMeta(p, 
188fb 34 2c 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20  4, pgnoRoot);.  
188fc 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
188fd 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52    releasePage(pR
188fe 6f 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  oot);.      retu
188ff 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20  rn rc;.    }..  
18900 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
18901 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
18902 65 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26  e(pBt, &pRoot, &
18903 70 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b  pgnoRoot, 1, 0);
18904 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
18905 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e  turn rc;.  }.#en
18906 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 73 71  dif.  assert( sq
18907 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
18908 65 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62  eable(pRoot->pDb
18909 50 61 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50  Page) );.  zeroP
1890a 61 67 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73  age(pRoot, flags
1890b 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20   | PTF_LEAF);.  
1890c 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
1890d 66 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65  f(pRoot->pDbPage
1890e 29 3b 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20  );.  *piTable = 
1890f 28 69 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20  (int)pgnoRoot;. 
18910 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
18911 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  K;.}.SQLITE_PRIV
18912 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
18913 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28  treeCreateTable(
18914 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70  Btree *p, int *p
18915 69 54 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67  iTable, int flag
18916 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  s){.  int rc;.  
18917 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
18918 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72  r(p);.  rc = btr
18919 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c  eeCreateTable(p,
1891a 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29   piTable, flags)
1891b 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1891c 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
1891d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1891e 45 72 61 73 65 20 74 68 65 20 67 69 76 65 6e 20  Erase the given 
1891f 64 61 74 61 62 61 73 65 20 70 61 67 65 20 61 6e  database page an
18920 64 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72  d all its childr
18921 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74  en.  Return.** t
18922 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 66  he page to the f
18923 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74  reelist..*/.stat
18924 69 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61  ic int clearData
18925 62 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 68  basePage(.  BtSh
18926 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20  ared *pBt,      
18927 20 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65       /* The BTre
18928 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
18929 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50  the table */.  P
1892a 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20  gno pgno,       
1892b 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
1892c 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a  ber to clear */.
1892d 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c    int freePageFl
1892e 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c  ag,     /* Deall
1892f 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74 72  ocate page if tr
18930 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  ue */.  int *pnC
18931 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 61  hange.){.  MemPa
18932 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74  ge *pPage;.  int
18933 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20   rc;.  unsigned 
18934 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69  char *pCell;.  i
18935 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28  nt i;..  assert(
18936 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
18937 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
18938 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e 70 61  );.  if( pgno>pa
18939 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
1893a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
1893b 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1893c 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d  KPT;.  }..  rc =
1893d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
1893e 70 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67  pBt, pgno, &pPag
1893f 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  e);.  if( rc ) r
18940 65 74 75 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28  eturn rc;.  for(
18941 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43  i=0; i<pPage->nC
18942 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  ell; i++){.    p
18943 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
18944 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69  pPage, i);.    i
18945 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
18946 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c  ){.      rc = cl
18947 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28  earDatabasePage(
18948 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43  pBt, get4byte(pC
18949 65 6c 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67  ell), 1, pnChang
1894a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  e);.      if( rc
1894b 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74   ) goto cleardat
1894c 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20  abasepage_out;. 
1894d 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c     }.    rc = cl
1894e 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70  earCell(pPage, p
1894f 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  Cell);.    if( r
18950 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61  c ) goto clearda
18951 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a  tabasepage_out;.
18952 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65    }.  if( !pPage
18953 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 63  ->leaf ){.    rc
18954 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65   = clearDatabase
18955 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79  Page(pBt, get4by
18956 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
18957 5b 38 5d 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67  [8]), 1, pnChang
18958 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
18959 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62   goto cleardatab
1895a 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d  asepage_out;.  }
1895b 65 6c 73 65 20 69 66 28 20 70 6e 43 68 61 6e 67  else if( pnChang
1895c 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  e ){.    assert(
1895d 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
1895e 3b 0a 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 20  ;.    *pnChange 
1895f 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b  += pPage->nCell;
18960 0a 20 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50  .  }.  if( freeP
18961 61 67 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 66  ageFlag ){.    f
18962 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26  reePage(pPage, &
18963 72 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  rc);.  }else if(
18964 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61   (rc = sqlite3Pa
18965 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e  gerWrite(pPage->
18966 70 44 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a  pDbPage))==0 ){.
18967 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61      zeroPage(pPa
18968 67 65 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61  ge, pPage->aData
18969 5b 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b  [0] | PTF_LEAF);
1896a 0a 20 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62  .  }..cleardatab
1896b 61 73 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72  asepage_out:.  r
1896c 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
1896d 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1896e 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
1896f 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  all information 
18970 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61  from a single ta
18971 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  ble in the datab
18972 61 73 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a  ase.  iTable is.
18973 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  ** the page numb
18974 65 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f  er of the root o
18975 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66  f the table.  Af
18976 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
18977 20 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65   returns,.** the
18978 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d   root page is em
18979 70 74 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65  pty, but still e
1897a 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  xists..**.** Thi
1897b 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66  s routine will f
1897c 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ail with SQLITE_
1897d 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20  LOCKED if there 
1897e 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20  are any open.** 
1897f 72 65 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20  read cursors on 
18980 74 68 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e  the table.  Open
18981 20 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 61   write cursors a
18982 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a  re moved to the.
18983 2a 2a 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74  ** root of the t
18984 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  able..**.** If p
18985 6e 43 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e  nChange is not N
18986 55 4c 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20  ULL, then table 
18987 69 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61  iTable must be a
18988 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20  n intkey table. 
18989 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76  The.** integer v
1898a 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  alue pointed to 
1898b 62 79 20 70 6e 43 68 61 6e 67 65 20 69 73 20 69  by pnChange is i
1898c 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68  ncremented by th
1898d 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65  e number of.** e
1898e 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 74 61  ntries in the ta
1898f 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
18990 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18991 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c  e3BtreeClearTabl
18992 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  e(Btree *p, int 
18993 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43  iTable, int *pnC
18994 68 61 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 63  hange){.  int rc
18995 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
18996 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71  t = p->pBt;.  sq
18997 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
18998 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p);.  assert( p-
18999 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
1899a 57 52 49 54 45 20 29 3b 0a 0a 20 20 2f 2a 20 49  WRITE );..  /* I
1899b 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e  nvalidate all in
1899c 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f  crblob cursors o
1899d 70 65 6e 20 6f 6e 20 74 61 62 6c 65 20 69 54 61  pen on table iTa
1899e 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 69 54  ble (assuming iT
1899f 61 62 6c 65 0a 20 20 2a 2a 20 69 73 20 74 68 65  able.  ** is the
189a0 20 72 6f 6f 74 20 6f 66 20 61 20 74 61 62 6c 65   root of a table
189a1 20 62 2d 74 72 65 65 20 2d 20 69 66 20 69 74 20   b-tree - if it 
189a2 69 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c  is not, the foll
189a3 6f 77 69 6e 67 20 63 61 6c 6c 20 69 73 0a 20 20  owing call is.  
189a4 2a 2a 20 61 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f  ** a no-op).  */
189a5 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63  .  invalidateInc
189a6 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c 20  rblobCursors(p, 
189a7 30 2c 20 31 29 3b 0a 0a 20 20 72 63 20 3d 20 73  0, 1);..  rc = s
189a8 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42  aveAllCursors(pB
189a9 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c  t, (Pgno)iTable,
189aa 20 30 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54   0);.  if( SQLIT
189ab 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK==rc ){.    
189ac 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61  rc = clearDataba
189ad 73 65 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e  sePage(pBt, (Pgn
189ae 6f 29 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e 43  o)iTable, 0, pnC
189af 68 61 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71  hange);.  }.  sq
189b0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
189b1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
189b2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20  .}../*.** Erase 
189b3 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  all information 
189b4 69 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61  in a table and a
189b5 64 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  dd the root of t
189b6 68 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74  he table to.** t
189b7 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78  he freelist.  Ex
189b8 63 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f  cept, the root o
189b9 66 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20  f the principle 
189ba 74 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f  table (the one o
189bb 6e 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20  n.** page 1) is 
189bc 6e 65 76 65 72 20 61 64 64 65 64 20 74 6f 20 74  never added to t
189bd 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a  he freelist..**.
189be 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
189bf 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53  will fail with S
189c0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20  QLITE_LOCKED if 
189c1 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70  there are any op
189c2 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e  en.** cursors on
189c3 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
189c4 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20  * If AUTOVACUUM 
189c5 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74  is enabled and t
189c6 68 65 20 70 61 67 65 20 61 74 20 69 54 61 62 6c  he page at iTabl
189c7 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73  e is not the las
189c8 74 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69  t.** root page i
189c9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
189ca 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61  ile, then the la
189cb 73 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a  st root page .**
189cc 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
189cd 20 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69   file is moved i
189ce 6e 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72  nto the slot for
189cf 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62  merly occupied b
189d0 79 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20  y.** iTable and 
189d1 74 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66  that last slot f
189d2 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64  ormerly occupied
189d3 20 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f   by the last roo
189d4 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64  t page.** is add
189d5 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ed to the freeli
189d6 73 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54  st instead of iT
189d7 61 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73  able.  In this s
189d8 61 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20  ay, all.** root 
189d9 70 61 67 65 73 20 61 72 65 20 6b 65 70 74 20 61  pages are kept a
189da 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
189db 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
189dc 66 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69  file, which.** i
189dd 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20  s necessary for 
189de 41 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f  AUTOVACUUM to wo
189df 72 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f  rk right.  *piMo
189e0 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 74 68  ved is set to th
189e1 65 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65  e .** page numbe
189e2 72 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 62  r that used to b
189e3 65 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20  e the last root 
189e4 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65  page in the file
189e5 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d   before.** the m
189e6 6f 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65  ove.  If no page
189e7 20 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69   gets moved, *pi
189e8 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20  Moved is set to 
189e9 30 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72  0..** The last r
189ea 6f 6f 74 20 70 61 67 65 20 69 73 20 72 65 63 6f  oot page is reco
189eb 72 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20  rded in meta[3] 
189ec 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  and the value of
189ed 0a 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75  .** meta[3] is u
189ee 70 64 61 74 65 64 20 62 79 20 74 68 69 73 20 70  pdated by this p
189ef 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
189f0 74 69 63 20 69 6e 74 20 62 74 72 65 65 44 72 6f  tic int btreeDro
189f1 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  pTable(Btree *p,
189f2 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 69 6e   Pgno iTable, in
189f3 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69  t *piMoved){.  i
189f4 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65  nt rc;.  MemPage
189f5 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42   *pPage = 0;.  B
189f6 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
189f7 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74  ->pBt;..  assert
189f8 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
189f9 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
189fa 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
189fb 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
189fc 20 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20   );..  /* It is 
189fd 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20  illegal to drop 
189fe 61 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63  a table if any c
189ff 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20  ursors are open 
18a00 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  on the.  ** data
18a01 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 62 65  base. This is be
18a02 63 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61  cause in auto-va
18a03 63 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61  cuum mode the ba
18a04 63 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e  ckend may.  ** n
18a05 65 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74  eed to move anot
18a06 68 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f  her root-page to
18a07 20 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74   fill a gap left
18a08 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a   by the deleted.
18a09 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20    ** root page. 
18a0a 49 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f  If an open curso
18a0b 72 20 77 61 73 20 75 73 69 6e 67 20 74 68 69 73  r was using this
18a0c 20 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20   page a problem 
18a0d 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75  would .  ** occu
18a0e 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  r..  **.  ** Thi
18a0f 73 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 68  s error is caugh
18a10 74 20 6c 6f 6e 67 20 62 65 66 6f 72 65 20 63 6f  t long before co
18a11 6e 74 72 6f 6c 20 72 65 61 63 68 65 73 20 74 68  ntrol reaches th
18a12 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 20  is point..  */. 
18a13 20 69 66 28 20 4e 45 56 45 52 28 70 42 74 2d 3e   if( NEVER(pBt->
18a14 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20  pCursor) ){.    
18a15 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
18a16 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20  nBlocked(p->db, 
18a17 70 42 74 2d 3e 70 43 75 72 73 6f 72 2d 3e 70 42  pBt->pCursor->pB
18a18 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 72  tree->db);.    r
18a19 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
18a1a 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b  KED_SHAREDCACHE;
18a1b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 62 74 72  .  }..  rc = btr
18a1c 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28  eeGetPage(pBt, (
18a1d 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50  Pgno)iTable, &pP
18a1e 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  age, 0);.  if( r
18a1f 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
18a20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
18a21 65 65 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20  eeClearTable(p, 
18a22 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66  iTable, 0);.  if
18a23 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 6c 65  ( rc ){.    rele
18a24 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
18a25 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18a26 20 7d 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d   }..  *piMoved =
18a27 20 30 3b 0a 0a 20 20 69 66 28 20 69 54 61 62 6c   0;..  if( iTabl
18a28 65 3e 31 20 29 7b 0a 23 69 66 64 65 66 20 53 51  e>1 ){.#ifdef SQ
18a29 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
18a2a 43 55 55 4d 0a 20 20 20 20 66 72 65 65 50 61 67  CUUM.    freePag
18a2b 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20  e(pPage, &rc);. 
18a2c 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
18a2d 50 61 67 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20  Page);.#else.   
18a2e 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
18a2f 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 50 67  cuum ){.      Pg
18a30 6e 6f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a  no maxRootPgno;.
18a31 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
18a32 65 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52  eeGetMeta(p, BTR
18a33 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f  EE_LARGEST_ROOT_
18a34 50 41 47 45 2c 20 26 6d 61 78 52 6f 6f 74 50 67  PAGE, &maxRootPg
18a35 6e 6f 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  no);..      if( 
18a36 69 54 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50  iTable==maxRootP
18a37 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  gno ){.        /
18a38 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 62  * If the table b
18a39 65 69 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20  eing dropped is 
18a3a 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 74  the table with t
18a3b 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d  he largest root-
18a3c 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  page.        ** 
18a3d 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61  number in the da
18a3e 74 61 62 61 73 65 2c 20 70 75 74 20 74 68 65 20  tabase, put the 
18a3f 72 6f 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65  root page on the
18a40 20 66 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20   free list. .   
18a41 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
18a42 66 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20  freePage(pPage, 
18a43 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 72 65  &rc);.        re
18a44 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
18a45 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
18a46 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
18a47 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
18a48 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
18a49 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18a4a 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
18a4b 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f  being dropped do
18a4c 65 73 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20  es not have the 
18a4d 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67  largest root-pag
18a4e 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d  e.        ** num
18a4f 62 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62  ber in the datab
18a50 61 73 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65  ase. So move the
18a51 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20   page that does 
18a52 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20  into the .      
18a53 20 20 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79    ** gap left by
18a54 20 74 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f   the deleted roo
18a55 74 2d 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20  t-page..        
18a56 2a 2f 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61  */.        MemPa
18a57 67 65 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20  ge *pMove;.     
18a58 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
18a59 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72  Page);.        r
18a5a 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
18a5b 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e  (pBt, maxRootPgn
18a5c 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20  o, &pMove, 0);. 
18a5d 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
18a5e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
18a5f 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
18a60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
18a61 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65     rc = relocate
18a62 50 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c  Page(pBt, pMove,
18a63 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
18a64 2c 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b  , 0, iTable, 0);
18a65 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
18a66 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20  Page(pMove);.   
18a67 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
18a68 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18a69 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18a6a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
18a6b 20 70 4d 6f 76 65 20 3d 20 30 3b 0a 20 20 20 20   pMove = 0;.    
18a6c 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
18a6d 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f  tPage(pBt, maxRo
18a6e 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20  otPgno, &pMove, 
18a6f 30 29 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65  0);.        free
18a70 50 61 67 65 28 70 4d 6f 76 65 2c 20 26 72 63 29  Page(pMove, &rc)
18a71 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
18a72 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20  ePage(pMove);.  
18a73 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
18a74 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
18a75 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
18a76 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
18a77 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78    *piMoved = max
18a78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20  RootPgno;.      
18a79 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20  }..      /* Set 
18a7a 74 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f  the new 'max-roo
18a7b 74 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e  t-page' value in
18a7c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65   the database he
18a7d 61 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20  ader. This.     
18a7e 20 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76   ** is the old v
18a7f 61 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c  alue less one, l
18a80 65 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20  ess one more if 
18a81 74 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a  that happens to.
18a82 20 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f        ** be a ro
18a83 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20  ot-page number, 
18a84 6c 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69  less one again i
18a85 66 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20  f that is the.  
18a86 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42      ** PENDING_B
18a87 59 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20  YTE_PAGE..      
18a88 2a 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74  */.      maxRoot
18a89 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 77 68  Pgno--;.      wh
18a8a 69 6c 65 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  ile( maxRootPgno
18a8b 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
18a8c 41 47 45 28 70 42 74 29 0a 20 20 20 20 20 20 20  AGE(pBt).       
18a8d 20 20 20 20 20 20 7c 7c 20 50 54 52 4d 41 50 5f        || PTRMAP_
18a8e 49 53 50 41 47 45 28 70 42 74 2c 20 6d 61 78 52  ISPAGE(pBt, maxR
18a8f 6f 6f 74 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20  ootPgno) ){.    
18a90 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d      maxRootPgno-
18a91 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  -;.      }.     
18a92 20 61 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74   assert( maxRoot
18a93 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno!=PENDING_BY
18a94 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a  TE_PAGE(pBt) );.
18a95 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
18a96 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
18a97 74 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74  ta(p, 4, maxRoot
18a98 50 67 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Pgno);.    }else
18a99 7b 0a 20 20 20 20 20 20 66 72 65 65 50 61 67 65  {.      freePage
18a9a 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20  (pPage, &rc);.  
18a9b 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
18a9c 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65  pPage);.    }.#e
18a9d 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ndif.  }else{.  
18a9e 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 42    /* If sqlite3B
18a9f 74 72 65 65 44 72 6f 70 54 61 62 6c 65 20 77 61  treeDropTable wa
18aa0 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65  s called on page
18aa1 20 31 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20   1..    ** This 
18aa2 72 65 61 6c 6c 79 20 6e 65 76 65 72 20 73 68 6f  really never sho
18aa3 75 6c 64 20 68 61 70 70 65 6e 20 65 78 63 65 70  uld happen excep
18aa4 74 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 20  t in a corrupt. 
18aa5 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20     ** database. 
18aa6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 65 72 6f  .    */.    zero
18aa7 50 61 67 65 28 70 50 61 67 65 2c 20 50 54 46 5f  Page(pPage, PTF_
18aa8 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20  INTKEY|PTF_LEAF 
18aa9 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  );.    releasePa
18aaa 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20  ge(pPage);.  }. 
18aab 20 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a   return rc;  .}.
18aac 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18aad 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
18aae 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a  ropTable(Btree *
18aaf 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69  p, int iTable, i
18ab0 6e 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20  nt *piMoved){.  
18ab1 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
18ab2 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
18ab3 20 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f 70    rc = btreeDrop
18ab4 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c  Table(p, iTable,
18ab5 20 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c   piMoved);.  sql
18ab6 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
18ab7 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
18ab8 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
18ab9 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79  unction may only
18aba 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68   be called if th
18abb 65 20 62 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74  e b-tree connect
18abc 69 6f 6e 20 61 6c 72 65 61 64 79 0a 2a 2a 20 68  ion already.** h
18abd 61 73 20 61 20 72 65 61 64 20 6f 72 20 77 72 69  as a read or wri
18abe 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te transaction o
18abf 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62  pen on the datab
18ac0 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20  ase..**.** Read 
18ac1 74 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61  the meta-informa
18ac2 74 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61  tion out of a da
18ac3 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65  tabase file.  Me
18ac4 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20  ta[0].** is the 
18ac5 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70  number of free p
18ac6 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69  ages currently i
18ac7 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
18ac8 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f   Meta[1].** thro
18ac9 75 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65  ugh meta[15] are
18aca 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75   available for u
18acb 73 65 20 62 79 20 68 69 67 68 65 72 20 6c 61 79  se by higher lay
18acc 65 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a  ers.  Meta[0].**
18acd 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74   is read-only, t
18ace 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 72 65  he others are re
18acf 61 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a  ad/write..** .**
18ad0 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65   The schema laye
18ad1 72 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76  r numbers meta v
18ad2 61 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c  alues differentl
18ad3 79 2e 20 20 41 74 20 74 68 65 20 73 63 68 65 6d  y.  At the schem
18ad4 61 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20  a.** layer (and 
18ad5 74 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e  the SetCookie an
18ad6 64 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63  d ReadCookie opc
18ad7 6f 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72  odes) the number
18ad8 20 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65   of.** free page
18ad9 73 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65  s is not visible
18ada 2e 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20  .  So Cookie[0] 
18adb 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d  is the same as M
18adc 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  eta[1]..*/.SQLIT
18add 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
18ade 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
18adf 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  ta(Btree *p, int
18ae0 20 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61   idx, u32 *pMeta
18ae1 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
18ae2 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20  Bt = p->pBt;..  
18ae3 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
18ae4 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r(p);.  assert( 
18ae5 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53  p->inTrans>TRANS
18ae6 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72  _NONE );.  asser
18ae7 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75  t( SQLITE_OK==qu
18ae8 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61  erySharedCacheTa
18ae9 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45  bleLock(p, MASTE
18aea 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43  R_ROOT, READ_LOC
18aeb 4b 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  K) );.  assert( 
18aec 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20  pBt->pPage1 );. 
18aed 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20   assert( idx>=0 
18aee 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 0a 20  && idx<=15 );.. 
18aef 20 2a 70 4d 65 74 61 20 3d 20 67 65 74 34 62 79   *pMeta = get4by
18af0 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
18af1 3e 61 44 61 74 61 5b 33 36 20 2b 20 69 64 78 2a  >aData[36 + idx*
18af2 34 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 75  4]);..  /* If au
18af3 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 64 69 73  to-vacuum is dis
18af4 61 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75  abled in this bu
18af5 69 6c 64 20 61 6e 64 20 74 68 69 73 20 69 73 20  ild and this is 
18af6 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 20  an auto-vacuum. 
18af7 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 6d 61   ** database, ma
18af8 72 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20  rk the database 
18af9 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 2a  as read-only.  *
18afa 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
18afb 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
18afc 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45 45    if( idx==BTREE
18afd 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41  _LARGEST_ROOT_PA
18afe 47 45 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29  GE && *pMeta>0 )
18aff 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d   pBt->readOnly =
18b00 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71   1;.#endif..  sq
18b01 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
18b02 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  p);.}../*.** Wri
18b03 74 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74  te meta-informat
18b04 69 6f 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  ion back into th
18b05 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74  e database.  Met
18b06 61 5b 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d  a[0] is.** read-
18b07 6f 6e 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74  only and may not
18b08 20 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a   be written..*/.
18b09 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18b0a 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 55  nt sqlite3BtreeU
18b0b 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 20  pdateMeta(Btree 
18b0c 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32  *p, int idx, u32
18b0d 20 69 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61   iMeta){.  BtSha
18b0e 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
18b0f 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  t;.  unsigned ch
18b10 61 72 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72  ar *pP1;.  int r
18b11 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78  c;.  assert( idx
18b12 3e 3d 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29  >=1 && idx<=15 )
18b13 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
18b14 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65  Enter(p);.  asse
18b15 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  rt( p->inTrans==
18b16 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
18b17 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50   assert( pBt->pP
18b18 61 67 65 31 21 3d 30 20 29 3b 0a 20 20 70 50 31  age1!=0 );.  pP1
18b19 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e   = pBt->pPage1->
18b1a 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71  aData;.  rc = sq
18b1b 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
18b1c 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62  pBt->pPage1->pDb
18b1d 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  Page);.  if( rc=
18b1e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
18b1f 20 20 70 75 74 34 62 79 74 65 28 26 70 50 31 5b    put4byte(&pP1[
18b20 33 36 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65  36 + idx*4], iMe
18b21 74 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ta);.#ifndef SQL
18b22 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18b23 55 55 4d 0a 20 20 20 20 69 66 28 20 69 64 78 3d  UUM.    if( idx=
18b24 3d 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55  =BTREE_INCR_VACU
18b25 55 4d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  UM ){.      asse
18b26 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  rt( pBt->autoVac
18b27 75 75 6d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20  uum || iMeta==0 
18b28 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
18b29 20 69 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65   iMeta==0 || iMe
18b2a 74 61 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70  ta==1 );.      p
18b2b 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d  Bt->incrVacuum =
18b2c 20 28 75 38 29 69 4d 65 74 61 3b 0a 20 20 20 20   (u8)iMeta;.    
18b2d 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73  }.#endif.  }.  s
18b2e 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
18b2f 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
18b30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
18b31 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f  ITE_OMIT_BTREECO
18b32 55 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69  UNT./*.** The fi
18b33 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43  rst argument, pC
18b34 75 72 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20  ur, is a cursor 
18b35 6f 70 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62  opened on some b
18b36 2d 74 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65  -tree. Count the
18b37 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  .** number of en
18b38 74 72 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74  tries in the b-t
18b39 72 65 65 20 61 6e 64 20 77 72 69 74 65 20 74 68  ree and write th
18b3a 65 20 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45  e result to *pnE
18b3b 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  ntry..**.** SQLI
18b3c 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
18b3d 64 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69  d if the operati
18b3e 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  on is successful
18b3f 6c 79 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a  ly executed. .**
18b40 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
18b41 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  n error is encou
18b42 6e 74 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20  ntered (i.e. an 
18b43 49 4f 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61  IO error or data
18b44 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69  base.** corrupti
18b45 6f 6e 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72  on) an SQLite er
18b46 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
18b47 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
18b48 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18b49 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74  te3BtreeCount(Bt
18b4a 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36  Cursor *pCur, i6
18b4b 34 20 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69  4 *pnEntry){.  i
18b4c 36 34 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20  64 nEntry = 0;  
18b4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b4e 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
18b4f 72 65 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74  return in *pnEnt
18b50 72 79 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  ry */.  int rc; 
18b51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b52 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18b53 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
18b54 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74   rc = moveToRoot
18b55 28 70 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e  (pCur);..  /* Un
18b56 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63  less an error oc
18b57 63 75 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  curs, the follow
18b58 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e  ing loop runs on
18b59 65 20 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20  e iteration for 
18b5a 65 61 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69  each.  ** page i
18b5b 6e 20 74 68 65 20 42 2d 54 72 65 65 20 73 74 72  n the B-Tree str
18b5c 75 63 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c  ucture (not incl
18b5d 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70  uding overflow p
18b5e 61 67 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77  ages). .  */.  w
18b5f 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
18b60 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  _OK ){.    int i
18b61 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
18b62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18b63 20 49 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20   Index of child 
18b64 6e 6f 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a  node in parent *
18b65 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  /.    MemPage *p
18b66 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
18b67 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
18b68 65 6e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  ent page of the 
18b69 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f  b-tree */..    /
18b6a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6c  * If this is a l
18b6b 65 61 66 20 70 61 67 65 20 6f 72 20 74 68 65 20  eaf page or the 
18b6c 74 72 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69  tree is not an i
18b6d 6e 74 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65  nt-key tree, the
18b6e 6e 20 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70  n .    ** this p
18b6f 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75  age contains cou
18b70 6e 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20  ntable entries. 
18b71 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e  Increment the en
18b72 74 72 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20  try counter.    
18b73 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a  ** accordingly..
18b74 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65      */.    pPage
18b75 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
18b76 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
18b77 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61    if( pPage->lea
18b78 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74  f || !pPage->int
18b79 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e  Key ){.      nEn
18b7a 74 72 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43  try += pPage->nC
18b7b 65 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ell;.    }..    
18b7c 2f 2a 20 70 50 61 67 65 20 69 73 20 61 20 6c 65  /* pPage is a le
18b7d 61 66 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f  af node. This lo
18b7e 6f 70 20 6e 61 76 69 67 61 74 65 73 20 74 68 65  op navigates the
18b7f 20 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20   cursor so that 
18b80 69 74 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74  it .    ** point
18b81 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69  s to the first i
18b82 6e 74 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61  nterior cell tha
18b83 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  t it points to t
18b84 68 65 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20  he parent of.   
18b85 20 2a 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67   ** the next pag
18b86 65 20 69 6e 20 74 68 65 20 74 72 65 65 20 74 68  e in the tree th
18b87 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  at has not yet b
18b88 65 65 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65  een visited. The
18b89 0a 20 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69  .    ** pCur->ai
18b8a 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
18b8b 20 76 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f   value is set to
18b8c 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
18b8d 65 20 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20  e parent cell.  
18b8e 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65    ** of the page
18b8f 2c 20 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62  , or to the numb
18b90 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74  er of cells in t
18b91 68 65 20 70 61 67 65 20 69 66 20 74 68 65 20 6e  he page if the n
18b92 65 78 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20  ext page.    ** 
18b93 74 6f 20 76 69 73 69 74 20 69 73 20 74 68 65 20  to visit is the 
18b94 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69  right-child of i
18b95 74 73 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a  ts parent..    *
18b96 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20  *.    ** If all 
18b97 70 61 67 65 73 20 69 6e 20 74 68 65 20 74 72 65  pages in the tre
18b98 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 69  e have been visi
18b99 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ted, return SQLI
18b9a 54 45 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20  TE_OK to the.   
18b9b 20 2a 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20   ** caller..    
18b9c 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  */.    if( pPage
18b9d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
18b9e 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28  do {.        if(
18b9f 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20   pCur->iPage==0 
18ba0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
18ba1 41 6c 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65  All pages of the
18ba2 20 62 2d 74 72 65 65 20 68 61 76 65 20 62 65 65   b-tree have bee
18ba3 6e 20 76 69 73 69 74 65 64 2e 20 52 65 74 75 72  n visited. Retur
18ba4 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20  n successfully. 
18ba5 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e  */.          *pn
18ba6 45 6e 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a  Entry = nEntry;.
18ba7 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
18ba8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
18ba9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 6f      }.        mo
18baa 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29  veToParent(pCur)
18bab 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 20 28  ;.      }while (
18bac 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
18bad 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d  r->iPage]>=pCur-
18bae 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
18baf 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a  age]->nCell );..
18bb0 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64        pCur->aiId
18bb1 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2b 2b  x[pCur->iPage]++
18bb2 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20  ;.      pPage = 
18bb3 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
18bb4 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d  r->iPage];.    }
18bb5 0a 0a 20 20 20 20 2f 2a 20 44 65 73 63 65 6e 64  ..    /* Descend
18bb6 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20 6e 6f   to the child no
18bb7 64 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74  de of the cell t
18bb8 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 63  hat the cursor c
18bb9 75 72 72 65 6e 74 6c 79 20 0a 20 20 20 20 2a 2a  urrently .    **
18bba 20 70 6f 69 6e 74 73 20 61 74 2e 20 54 68 69 73   points at. This
18bbb 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68   is the right-ch
18bbc 69 6c 64 20 69 66 20 28 69 49 64 78 3d 3d 70 50  ild if (iIdx==pP
18bbd 61 67 65 2d 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20  age->nCell)..   
18bbe 20 2a 2f 0a 20 20 20 20 69 49 64 78 20 3d 20 70   */.    iIdx = p
18bbf 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
18bc0 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28  >iPage];.    if(
18bc1 20 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43   iIdx==pPage->nC
18bc2 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ell ){.      rc 
18bc3 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
18bc4 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50  ur, get4byte(&pP
18bc5 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
18bc6 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29  ->hdrOffset+8]))
18bc7 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
18bc8 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
18bc9 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79  ild(pCur, get4by
18bca 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67  te(findCell(pPag
18bcb 65 2c 20 69 49 64 78 29 29 29 3b 0a 20 20 20 20  e, iIdx)));.    
18bcc 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65  }.  }..  /* An e
18bcd 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
18bce 64 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  d. Return an err
18bcf 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 72 65  or code. */.  re
18bd0 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
18bd1 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
18bd2 74 68 65 20 70 61 67 65 72 20 61 73 73 6f 63 69  the pager associ
18bd3 61 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65  ated with a BTre
18bd4 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
18bd5 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20   is used for.** 
18bd6 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
18bd7 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53  gging only..*/.S
18bd8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 61  QLITE_PRIVATE Pa
18bd9 67 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65  ger *sqlite3Btre
18bda 65 50 61 67 65 72 28 42 74 72 65 65 20 2a 70 29  ePager(Btree *p)
18bdb 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42  {.  return p->pB
18bdc 74 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a 23 69  t->pPager;.}..#i
18bdd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18bde 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
18bdf 4b 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  K./*.** Append a
18be0 20 6d 65 73 73 61 67 65 20 74 6f 20 74 68 65 20   message to the 
18be1 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74  error message st
18be2 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ring..*/.static 
18be3 76 6f 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64  void checkAppend
18be4 4d 73 67 28 0a 20 20 49 6e 74 65 67 72 69 74 79  Msg(.  Integrity
18be5 43 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 20 63 68  Ck *pCheck,.  ch
18be6 61 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e  ar *zMsg1,.  con
18be7 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
18be8 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f  ,.  ....){.  va_
18be9 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21  list ap;.  if( !
18bea 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 20  pCheck->mxErr ) 
18beb 72 65 74 75 72 6e 3b 0a 20 20 70 43 68 65 63 6b  return;.  pCheck
18bec 2d 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 43 68  ->mxErr--;.  pCh
18bed 65 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76  eck->nErr++;.  v
18bee 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
18bef 6d 61 74 29 3b 0a 20 20 69 66 28 20 70 43 68 65  mat);.  if( pChe
18bf0 63 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 61 72  ck->errMsg.nChar
18bf1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
18bf2 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70  trAccumAppend(&p
18bf3 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 22  Check->errMsg, "
18bf4 5c 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69  \n", 1);.  }.  i
18bf5 66 28 20 7a 4d 73 67 31 20 29 7b 0a 20 20 20 20  f( zMsg1 ){.    
18bf6 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
18bf7 70 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65  ppend(&pCheck->e
18bf8 72 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31  rrMsg, zMsg1, -1
18bf9 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
18bfa 56 58 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b  VXPrintf(&pCheck
18bfb 2d 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f  ->errMsg, 1, zFo
18bfc 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
18bfd 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70  end(ap);.  if( p
18bfe 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61  Check->errMsg.ma
18bff 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
18c00 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63    pCheck->malloc
18c01 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
18c02 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
18c03 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
18c04 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e  Y_CHECK */..#ifn
18c05 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18c06 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a  INTEGRITY_CHECK.
18c07 2f 2a 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74  /*.** Add 1 to t
18c08 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
18c09 6e 74 20 66 6f 72 20 70 61 67 65 20 69 50 61 67  nt for page iPag
18c0a 65 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 74  e.  If this is t
18c0b 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66  he second.** ref
18c0c 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61  erence to the pa
18c0d 67 65 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72  ge, add an error
18c0e 20 6d 65 73 73 61 67 65 20 74 6f 20 70 43 68 65   message to pChe
18c0f 63 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20  ck->zErrMsg..** 
18c10 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72  Return 1 if ther
18c11 65 20 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65  e are 2 ore more
18c12 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
18c13 68 65 20 70 61 67 65 20 61 6e 64 20 30 20 69 66  he page and 0 if
18c14 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74  .** if this is t
18c15 68 65 20 66 69 72 73 74 20 72 65 66 65 72 65 6e  he first referen
18c16 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a  ce to the page..
18c17 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b  **.** Also check
18c18 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e   that the page n
18c19 75 6d 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e  umber is in boun
18c1a 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ds..*/.static in
18c1b 74 20 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67  t checkRef(Integ
18c1c 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20  rityCk *pCheck, 
18c1d 50 67 6e 6f 20 69 50 61 67 65 2c 20 63 68 61 72  Pgno iPage, char
18c1e 20 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69   *zContext){.  i
18c1f 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65  f( iPage==0 ) re
18c20 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 50  turn 1;.  if( iP
18c21 61 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67  age>pCheck->nPag
18c22 65 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70  e ){.    checkAp
18c23 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
18c24 7a 43 6f 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c  zContext, "inval
18c25 69 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 25  id page number %
18c26 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20  d", iPage);.    
18c27 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
18c28 69 66 28 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65  if( pCheck->anRe
18c29 66 5b 69 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20  f[iPage]==1 ){. 
18c2a 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
18c2b 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65  g(pCheck, zConte
18c2c 78 74 2c 20 22 32 6e 64 20 72 65 66 65 72 65 6e  xt, "2nd referen
18c2d 63 65 20 74 6f 20 70 61 67 65 20 25 64 22 2c 20  ce to page %d", 
18c2e 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75  iPage);.    retu
18c2f 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
18c30 72 6e 20 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52  rn  (pCheck->anR
18c31 65 66 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a  ef[iPage]++)>1;.
18c32 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
18c33 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18c34 4d 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  M./*.** Check th
18c35 61 74 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20  at the entry in 
18c36 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
18c37 66 6f 72 20 70 61 67 65 20 69 43 68 69 6c 64 20  for page iChild 
18c38 6d 61 70 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65  maps to .** page
18c39 20 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65   iParent, pointe
18c3a 72 20 74 79 70 65 20 70 74 72 54 79 70 65 2e 20  r type ptrType. 
18c3b 49 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61  If not, append a
18c3c 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a  n error message.
18c3d 2a 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f  ** to pCheck..*/
18c3e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65  .static void che
18c3f 63 6b 50 74 72 6d 61 70 28 0a 20 20 49 6e 74 65  ckPtrmap(.  Inte
18c40 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c  grityCk *pCheck,
18c41 20 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20     /* Integrity 
18c42 63 68 65 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f  check context */
18c43 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20  .  Pgno iChild, 
18c44 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 69            /* Chi
18c45 6c 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a  ld page number *
18c46 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20  /.  u8 eType,   
18c47 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
18c48 70 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d  pected pointer m
18c49 61 70 20 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e  ap type */.  Pgn
18c4a 6f 20 69 50 61 72 65 6e 74 2c 20 20 20 20 20 20  o iParent,      
18c4b 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20      /* Expected 
18c4c 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 72 65  pointer map pare
18c4d 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a  nt page number *
18c4e 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  /.  char *zConte
18c4f 78 74 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  xt         /* Co
18c50 6e 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f  ntext descriptio
18c51 6e 20 28 75 73 65 64 20 66 6f 72 20 65 72 72 6f  n (used for erro
18c52 72 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69  r msg) */.){.  i
18c53 6e 74 20 72 63 3b 0a 20 20 75 38 20 65 50 74 72  nt rc;.  u8 ePtr
18c54 6d 61 70 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20  mapType;.  Pgno 
18c55 69 50 74 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a  iPtrmapParent;..
18c56 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74    rc = ptrmapGet
18c57 28 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43  (pCheck->pBt, iC
18c58 68 69 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79  hild, &ePtrmapTy
18c59 70 65 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65  pe, &iPtrmapPare
18c5a 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  nt);.  if( rc!=S
18c5b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
18c5c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
18c5d 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  OMEM || rc==SQLI
18c5e 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29  TE_IOERR_NOMEM )
18c5f 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46   pCheck->mallocF
18c60 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63  ailed = 1;.    c
18c61 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18c62 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
18c63 22 46 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20  "Failed to read 
18c64 70 74 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20  ptrmap key=%d", 
18c65 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74  iChild);.    ret
18c66 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  urn;.  }..  if( 
18c67 65 50 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79  ePtrmapType!=eTy
18c68 70 65 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72  pe || iPtrmapPar
18c69 65 6e 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a  ent!=iParent ){.
18c6a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18c6b 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
18c6c 65 78 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64  ext, .      "Bad
18c6d 20 70 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b   ptr map entry k
18c6e 65 79 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28  ey=%d expected=(
18c6f 25 64 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25  %d,%d) got=(%d,%
18c70 64 29 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69  d)", .      iChi
18c71 6c 64 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65  ld, eType, iPare
18c72 6e 74 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c  nt, ePtrmapType,
18c73 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b   iPtrmapParent);
18c74 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
18c75 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69  *.** Check the i
18c76 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20  ntegrity of the 
18c77 66 72 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61  freelist or of a
18c78 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  n overflow page 
18c79 6c 69 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20  list..** Verify 
18c7a 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20  that the number 
18c7b 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20  of pages on the 
18c7c 6c 69 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74  list is N..*/.st
18c7d 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c  atic void checkL
18c7e 69 73 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79  ist(.  Integrity
18c7f 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20  Ck *pCheck,  /* 
18c80 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 69  Integrity checki
18c81 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
18c82 69 6e 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20  int isFreeList, 
18c83 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
18c84 72 20 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46  r a freelist.  F
18c85 61 6c 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f  alse for overflo
18c86 77 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20  w page list */. 
18c87 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20   int iPage,     
18c88 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
18c89 75 6d 62 65 72 20 66 6f 72 20 66 69 72 73 74 20  umber for first 
18c8a 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74  page in the list
18c8b 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20   */.  int N,    
18c8c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
18c8d 78 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f  xpected number o
18c8e 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c  f pages in the l
18c8f 69 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ist */.  char *z
18c90 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f  Context        /
18c91 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72  * Context for er
18c92 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
18c93 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
18c94 74 20 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a  t expected = N;.
18c95 20 20 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69    int iFirst = i
18c96 50 61 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e  Page;.  while( N
18c97 2d 2d 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b  -- > 0 && pCheck
18c98 2d 3e 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44  ->mxErr ){.    D
18c99 62 50 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65  bPage *pOvflPage
18c9a 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
18c9b 68 61 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a  har *pOvflData;.
18c9c 20 20 20 20 69 66 28 20 69 50 61 67 65 3c 31 20      if( iPage<1 
18c9d 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
18c9e 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
18c9f 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20  zContext,.      
18ca0 20 20 20 22 25 64 20 6f 66 20 25 64 20 70 61 67     "%d of %d pag
18ca1 65 73 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20  es missing from 
18ca2 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74  overflow list st
18ca3 61 72 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20  arting at %d",. 
18ca4 20 20 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78           N+1, ex
18ca5 70 65 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b  pected, iFirst);
18ca6 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
18ca7 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63    }.    if( chec
18ca8 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61  kRef(pCheck, iPa
18ca9 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20  ge, zContext) ) 
18caa 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73  break;.    if( s
18cab 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
18cac 43 68 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28  Check->pPager, (
18cad 50 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76  Pgno)iPage, &pOv
18cae 66 6c 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20  flPage) ){.     
18caf 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18cb0 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
18cb1 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74  , "failed to get
18cb2 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65   page %d", iPage
18cb3 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
18cb4 20 20 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44      }.    pOvflD
18cb5 61 74 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  ata = (unsigned 
18cb6 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61  char *)sqlite3Pa
18cb7 67 65 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c  gerGetData(pOvfl
18cb8 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69  Page);.    if( i
18cb9 73 46 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20  sFreeList ){.   
18cba 20 20 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62     int n = get4b
18cbb 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34  yte(&pOvflData[4
18cbc 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ]);.#ifndef SQLI
18cbd 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18cbe 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 43 68  UM.      if( pCh
18cbf 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61  eck->pBt->autoVa
18cc0 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  cuum ){.        
18cc1 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65  checkPtrmap(pChe
18cc2 63 6b 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41  ck, iPage, PTRMA
18cc3 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a  P_FREEPAGE, 0, z
18cc4 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
18cc5 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
18cc6 66 28 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74  f( n>pCheck->pBt
18cc7 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32  ->usableSize/4-2
18cc8 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
18cc9 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
18cca 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20  k, zContext,.   
18ccb 20 20 20 20 20 20 20 20 22 66 72 65 65 6c 69 73          "freelis
18ccc 74 20 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f  t leaf count too
18ccd 20 62 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22   big on page %d"
18cce 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
18ccf 20 20 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c    N--;.      }el
18cd0 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  se{.        for(
18cd1 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a  i=0; i<n; i++){.
18cd2 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69            Pgno i
18cd3 46 72 65 65 50 61 67 65 20 3d 20 67 65 74 34 62  FreePage = get4b
18cd4 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38  yte(&pOvflData[8
18cd5 2b 69 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20  +i*4]);.#ifndef 
18cd6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
18cd7 56 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20  VACUUM.         
18cd8 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74   if( pCheck->pBt
18cd9 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
18cda 20 20 20 20 20 20 20 20 20 20 20 20 63 68 65 63              chec
18cdb 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
18cdc 69 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41  iFreePage, PTRMA
18cdd 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a  P_FREEPAGE, 0, z
18cde 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
18cdf 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
18ce0 20 20 20 20 20 20 20 63 68 65 63 6b 52 65 66 28         checkRef(
18ce1 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67  pCheck, iFreePag
18ce2 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  e, zContext);.  
18ce3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
18ce4 4e 20 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a  N -= n;.      }.
18ce5 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
18ce6 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
18ce7 43 55 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20  CUUM.    else{. 
18ce8 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20       /* If this 
18ce9 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
18cea 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e  s auto-vacuum an
18ceb 64 20 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74  d iPage is not t
18cec 68 65 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a  he last.      **
18ced 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76   page in this ov
18cee 65 72 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65  erflow list, che
18cef 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e  ck that the poin
18cf0 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f  ter-map entry fo
18cf1 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66  r.      ** the f
18cf2 6f 6c 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61  ollowing page ma
18cf3 74 63 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20  tches iPage..   
18cf4 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
18cf5 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74  pCheck->pBt->aut
18cf6 6f 56 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29  oVacuum && N>0 )
18cf7 7b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67 65  {.        i = ge
18cf8 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61  t4byte(pOvflData
18cf9 29 3b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  );.        check
18cfa 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69  Ptrmap(pCheck, i
18cfb 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  , PTRMAP_OVERFLO
18cfc 57 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  W2, iPage, zCont
18cfd 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ext);.      }.  
18cfe 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69    }.#endif.    i
18cff 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
18d00 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20  pOvflData);.    
18d01 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
18d02 66 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20  f(pOvflPage);.  
18d03 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  }.}.#endif /* SQ
18d04 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
18d05 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69  ITY_CHECK */..#i
18d06 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18d07 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
18d08 4b 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f  K./*.** Do vario
18d09 75 73 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73  us sanity checks
18d0a 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67   on a single pag
18d0b 65 20 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65  e of a tree.  Re
18d0c 74 75 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65  turn.** the tree
18d0d 20 64 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61   depth.  Root pa
18d0e 67 65 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50  ges return 0.  P
18d0f 61 72 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70  arents of root p
18d10 61 67 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31  ages.** return 1
18d11 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a  , and so forth..
18d12 2a 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65  ** .** These che
18d13 63 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a  cks are done:.**
18d14 0a 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b  .**      1.  Mak
18d15 65 20 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c  e sure that cell
18d16 73 20 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73  s and freeblocks
18d17 20 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a   do not overlap.
18d18 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 20  **          but 
18d19 63 6f 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c  combine to compl
18d1a 65 74 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20  etely cover the 
18d1b 70 61 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e  page..**  NO  2.
18d1c 20 20 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c    Make sure cell
18d1d 20 6b 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64   keys are in ord
18d1e 65 72 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20  er..**  NO  3.  
18d1f 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79  Make sure no key
18d20 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
18d21 20 65 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72   equal to zLower
18d22 42 6f 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34  Bound..**  NO  4
18d23 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20  .  Make sure no 
18d24 6b 65 79 20 69 73 20 67 72 65 61 74 65 72 20 74  key is greater t
18d25 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
18d26 7a 55 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20  zUpperBound..** 
18d27 20 20 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74       5.  Check t
18d28 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20  he integrity of 
18d29 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a  overflow pages..
18d2a 2a 2a 20 20 20 20 20 20 36 2e 20 20 52 65 63 75  **      6.  Recu
18d2b 72 73 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65  rsively call che
18d2c 63 6b 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c  ckTreePage on al
18d2d 6c 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20  l children..**  
18d2e 20 20 20 20 37 2e 20 20 56 65 72 69 66 79 20 74      7.  Verify t
18d2f 68 61 74 20 74 68 65 20 64 65 70 74 68 20 6f 66  hat the depth of
18d30 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73   all children is
18d31 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20   the same..**   
18d32 20 20 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65     8.  Make sure
18d33 20 74 68 69 73 20 70 61 67 65 20 69 73 20 61 74   this page is at
18d34 20 6c 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20   least 33% full 
18d35 6f 72 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a  or else it is.**
18d36 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72 6f            the ro
18d37 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a  ot of the tree..
18d38 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68  */.static int ch
18d39 65 63 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49  eckTreePage(.  I
18d3a 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65  ntegrityCk *pChe
18d3b 63 6b 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20  ck,  /* Context 
18d3c 66 6f 72 20 74 68 65 20 73 61 6e 69 74 79 20 63  for the sanity c
18d3d 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50  heck */.  int iP
18d3e 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  age,            
18d3f 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f  /* Page number o
18d40 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 63 68  f the page to ch
18d41 65 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  eck */.  char *z
18d42 50 61 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f  ParentContext  /
18d43 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74  * Parent context
18d44 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65   */.){.  MemPage
18d45 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69   *pPage;.  int i
18d46 2c 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c  , rc, depth, d2,
18d47 20 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e   pgno, cnt;.  in
18d48 74 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74  t hdr, cellStart
18d49 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20  ;.  int nCell;. 
18d4a 20 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53   u8 *data;.  BtS
18d4b 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e  hared *pBt;.  in
18d4c 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  t usableSize;.  
18d4d 63 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30  char zContext[10
18d4e 30 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20  0];.  char *hit 
18d4f 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  = 0;..  sqlite3_
18d50 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
18d51 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74  zContext), zCont
18d52 65 78 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22  ext, "Page %d: "
18d53 2c 20 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20  , iPage);..  /* 
18d54 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70  Check that the p
18d55 61 67 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a  age exists.  */.
18d56 20 20 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e    pBt = pCheck->
18d57 70 42 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a  pBt;.  usableSiz
18d58 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e = pBt->usableS
18d59 69 7a 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65  ize;.  if( iPage
18d5a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
18d5b 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70    if( checkRef(p
18d5c 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50  Check, iPage, zP
18d5d 61 72 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20  arentContext) ) 
18d5e 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
18d5f 28 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61  (rc = btreeGetPa
18d60 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 50  ge(pBt, (Pgno)iP
18d61 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29  age, &pPage, 0))
18d62 21 3d 30 20 29 7b 0a 20 20 20 20 63 68 65 63 6b  !=0 ){.    check
18d63 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
18d64 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20  , zContext,.    
18d65 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65     "unable to ge
18d66 74 20 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f  t the page. erro
18d67 72 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b  r code=%d", rc);
18d68 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
18d69 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 4d   }..  /* Clear M
18d6a 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 74 6f  emPage.isInit to
18d6b 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63   make sure the c
18d6c 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74  orruption detect
18d6d 69 6f 6e 20 63 6f 64 65 20 69 6e 0a 20 20 2a 2a  ion code in.  **
18d6e 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29   btreeInitPage()
18d6f 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 20 2a   is executed.  *
18d70 2f 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  /.  pPage->isIni
18d71 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 72 63  t = 0;.  if( (rc
18d72 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65   = btreeInitPage
18d73 28 70 50 61 67 65 29 29 21 3d 30 20 29 7b 0a 20  (pPage))!=0 ){. 
18d74 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
18d75 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 29 3b  QLITE_CORRUPT );
18d76 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 70 6f    /* The only po
18d77 73 73 69 62 6c 65 20 65 72 72 6f 72 20 66 72 6f  ssible error fro
18d78 6d 20 49 6e 69 74 50 61 67 65 20 2a 2f 0a 20 20  m InitPage */.  
18d79 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
18d7a 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
18d7b 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  t, .            
18d7c 20 20 20 20 20 20 20 22 62 74 72 65 65 49 6e 69         "btreeIni
18d7d 74 50 61 67 65 28 29 20 72 65 74 75 72 6e 73 20  tPage() returns 
18d7e 65 72 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20  error code %d", 
18d7f 72 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65  rc);.    release
18d80 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
18d81 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
18d82 20 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61    /* Check out a
18d83 6c 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20  ll the cells..  
18d84 2a 2f 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a  */.  depth = 0;.
18d85 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
18d86 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68  ge->nCell && pCh
18d87 65 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29  eck->mxErr; i++)
18d88 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b  {.    u8 *pCell;
18d89 0a 20 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 20  .    u32 sz;.   
18d8a 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
18d8b 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61  .    /* Check pa
18d8c 79 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70  yload overflow p
18d8d 61 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ages.    */.    
18d8e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
18d8f 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74  (sizeof(zContext
18d90 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20  ), zContext,.   
18d91 20 20 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72            "On tr
18d92 65 65 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20  ee page %d cell 
18d93 25 64 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29  %d: ", iPage, i)
18d94 3b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69  ;.    pCell = fi
18d95 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b  ndCell(pPage,i);
18d96 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43  .    btreeParseC
18d97 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
18d98 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20  ell, &info);.   
18d99 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74 61   sz = info.nData
18d9a 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  ;.    if( !pPage
18d9b 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b 3d  ->intKey ) sz +=
18d9c 20 28 69 6e 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b   (int)info.nKey;
18d9d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3d  .    assert( sz=
18d9e 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29  =info.nPayload )
18d9f 3b 0a 20 20 20 20 69 66 28 20 28 73 7a 3e 69 6e  ;.    if( (sz>in
18da0 66 6f 2e 6e 4c 6f 63 61 6c 29 20 0a 20 20 20 20  fo.nLocal) .    
18da1 20 26 26 20 28 26 70 43 65 6c 6c 5b 69 6e 66 6f   && (&pCell[info
18da2 2e 69 4f 76 65 72 66 6c 6f 77 5d 3c 3d 26 70 50  .iOverflow]<=&pP
18da3 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e  age->aData[pBt->
18da4 75 73 61 62 6c 65 53 69 7a 65 5d 29 0a 20 20 20  usableSize]).   
18da5 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50   ){.      int nP
18da6 61 67 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f  age = (sz - info
18da7 2e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65  .nLocal + usable
18da8 53 69 7a 65 20 2d 20 35 29 2f 28 75 73 61 62 6c  Size - 5)/(usabl
18da9 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20  eSize - 4);.    
18daa 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20    Pgno pgnoOvfl 
18dab 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c  = get4byte(&pCel
18dac 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  l[info.iOverflow
18dad 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ]);.#ifndef SQLI
18dae 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18daf 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  UM.      if( pBt
18db0 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
18db1 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72          checkPtr
18db2 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f  map(pCheck, pgno
18db3 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45  Ovfl, PTRMAP_OVE
18db4 52 46 4c 4f 57 31 2c 20 69 50 61 67 65 2c 20 7a  RFLOW1, iPage, z
18db5 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
18db6 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 63  }.#endif.      c
18db7 68 65 63 6b 4c 69 73 74 28 70 43 68 65 63 6b 2c  heckList(pCheck,
18db8 20 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50   0, pgnoOvfl, nP
18db9 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a  age, zContext);.
18dba 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68      }..    /* Ch
18dbb 65 63 6b 20 73 61 6e 69 74 79 20 6f 66 20 6c 65  eck sanity of le
18dbc 66 74 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20  ft child page.. 
18dbd 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70     */.    if( !p
18dbe 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
18dbf 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62      pgno = get4b
18dc0 79 74 65 28 70 43 65 6c 6c 29 3b 0a 23 69 66 6e  yte(pCell);.#ifn
18dc1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18dc2 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
18dc3 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
18dc4 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  cuum ){.        
18dc5 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65  checkPtrmap(pChe
18dc6 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50  ck, pgno, PTRMAP
18dc7 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 7a  _BTREE, iPage, z
18dc8 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
18dc9 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 64  }.#endif.      d
18dca 32 20 3d 20 63 68 65 63 6b 54 72 65 65 50 61 67  2 = checkTreePag
18dcb 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20  e(pCheck, pgno, 
18dcc 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20  zContext);.     
18dcd 20 69 66 28 20 69 3e 30 20 26 26 20 64 32 21 3d   if( i>0 && d2!=
18dce 64 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 20  depth ){.       
18dcf 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18dd0 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
18dd1 2c 20 22 43 68 69 6c 64 20 70 61 67 65 20 64 65  , "Child page de
18dd2 70 74 68 20 64 69 66 66 65 72 73 22 29 3b 0a 20  pth differs");. 
18dd3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 70       }.      dep
18dd4 74 68 20 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20  th = d2;.    }. 
18dd5 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d   }.  if( !pPage-
18dd6 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e  >leaf ){.    pgn
18dd7 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  o = get4byte(&pP
18dd8 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
18dd9 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
18dda 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
18ddb 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f  rintf(sizeof(zCo
18ddc 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74  ntext), zContext
18ddd 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
18dde 20 20 20 20 20 20 20 20 22 4f 6e 20 70 61 67 65          "On page
18ddf 20 25 64 20 61 74 20 72 69 67 68 74 20 63 68 69   %d at right chi
18de0 6c 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 23  ld: ", iPage);.#
18de1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18de2 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18de3 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
18de4 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 63  acuum ){.      c
18de5 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63  heckPtrmap(pChec
18de6 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f  k, pgno, PTRMAP_
18de7 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 30 29  BTREE, iPage, 0)
18de8 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
18de9 20 20 20 63 68 65 63 6b 54 72 65 65 50 61 67 65     checkTreePage
18dea 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a  (pCheck, pgno, z
18deb 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a  Context);.  }. .
18dec 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63    /* Check for c
18ded 6f 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67 65  omplete coverage
18dee 20 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20 2a   of the page.  *
18def 2f 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65  /.  data = pPage
18df0 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d  ->aData;.  hdr =
18df1 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
18df2 74 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 74  t;.  hit = sqlit
18df3 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42  e3PageMalloc( pB
18df4 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20  t->pageSize );. 
18df5 20 69 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a 20   if( hit==0 ){. 
18df6 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f     pCheck->mallo
18df7 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
18df8 65 6c 73 65 7b 0a 20 20 20 20 75 31 36 20 63 6f  else{.    u16 co
18df9 6e 74 65 6e 74 4f 66 66 73 65 74 20 3d 20 67 65  ntentOffset = ge
18dfa 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
18dfb 2b 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +5]);.    assert
18dfc 28 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 3c  ( contentOffset<
18dfd 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20  =usableSize );  
18dfe 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62  /* Enforced by b
18dff 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a  treeInitPage() *
18e00 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74  /.    memset(hit
18e01 2b 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20  +contentOffset, 
18e02 30 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f  0, usableSize-co
18e03 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20  ntentOffset);.  
18e04 20 20 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c    memset(hit, 1,
18e05 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b   contentOffset);
18e06 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74  .    nCell = get
18e07 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
18e08 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61  3]);.    cellSta
18e09 72 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20  rt = hdr + 12 - 
18e0a 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20  4*pPage->leaf;. 
18e0b 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
18e0c 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ell; i++){.     
18e0d 20 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79   int pc = get2by
18e0e 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61  te(&data[cellSta
18e0f 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20  rt+i*2]);.      
18e10 75 31 36 20 73 69 7a 65 20 3d 20 31 30 32 34 3b  u16 size = 1024;
18e11 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  .      int j;.  
18e12 20 20 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62      if( pc<=usab
18e13 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20  leSize-4 ){.    
18e14 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53      size = cellS
18e15 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64  izePtr(pPage, &d
18e16 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20  ata[pc]);.      
18e17 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70 63 2b  }.      if( (pc+
18e18 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53  size-1)>=usableS
18e19 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ize ){.        c
18e1a 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18e1b 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20  heck, 0, .      
18e1c 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f        "Corruptio
18e1d 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65  n detected in ce
18e1e 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64  ll %d on page %d
18e1f 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20  ",i,iPage,0);.  
18e20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18e21 20 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65     for(j=pc+size
18e22 2d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20  -1; j>=pc; j--) 
18e23 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20  hit[j]++;.      
18e24 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20  }.    }.    i = 
18e25 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
18e26 64 72 2b 31 5d 29 3b 0a 20 20 20 20 77 68 69 6c  dr+1]);.    whil
18e27 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20  e( i>0 ){.      
18e28 69 6e 74 20 73 69 7a 65 2c 20 6a 3b 0a 20 20 20  int size, j;.   
18e29 20 20 20 61 73 73 65 72 74 28 20 69 3c 3d 75 73     assert( i<=us
18e2a 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20  ableSize-4 );   
18e2b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79    /* Enforced by
18e2c 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29   btreeInitPage()
18e2d 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 20 3d   */.      size =
18e2e 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
18e2f 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 61 73 73  i+2]);.      ass
18e30 65 72 74 28 20 69 2b 73 69 7a 65 3c 3d 75 73 61  ert( i+size<=usa
18e31 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45  bleSize );  /* E
18e32 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65  nforced by btree
18e33 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20  InitPage() */.  
18e34 20 20 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65      for(j=i+size
18e35 2d 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68  -1; j>=i; j--) h
18e36 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 6a  it[j]++;.      j
18e37 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
18e38 61 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73  a[i]);.      ass
18e39 65 72 74 28 20 6a 3d 3d 30 20 7c 7c 20 6a 3e 69  ert( j==0 || j>i
18e3a 2b 73 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66  +size );  /* Enf
18e3b 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e  orced by btreeIn
18e3c 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20  itPage() */.    
18e3d 20 20 61 73 73 65 72 74 28 20 6a 3c 3d 75 73 61    assert( j<=usa
18e3e 62 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 2f  bleSize-4 );   /
18e3f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74  * Enforced by bt
18e40 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f  reeInitPage() */
18e41 0a 20 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20  .      i = j;.  
18e42 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e    }.    for(i=cn
18e43 74 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a  t=0; i<usableSiz
18e44 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  e; i++){.      i
18e45 66 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a  f( hit[i]==0 ){.
18e46 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20          cnt++;. 
18e47 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68       }else if( h
18e48 69 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20  it[i]>1 ){.     
18e49 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
18e4a 67 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20  g(pCheck, 0,.   
18e4b 20 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65         "Multiple
18e4c 20 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 25   uses for byte %
18e4d 64 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69  d of page %d", i
18e4e 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
18e4f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
18e50 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63  .    }.    if( c
18e51 6e 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20  nt!=data[hdr+7] 
18e52 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
18e53 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
18e54 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 46  0, .          "F
18e55 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  ragmentation of 
18e56 25 64 20 62 79 74 65 73 20 72 65 70 6f 72 74 65  %d bytes reporte
18e57 64 20 61 73 20 25 64 20 6f 6e 20 70 61 67 65 20  d as %d on page 
18e58 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 63  %d",.          c
18e59 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c  nt, data[hdr+7],
18e5a 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20   iPage);.    }. 
18e5b 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65   }.  sqlite3Page
18e5c 46 72 65 65 28 68 69 74 29 3b 0a 20 20 72 65 6c  Free(hit);.  rel
18e5d 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
18e5e 0a 20 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b  .  return depth+
18e5f 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  1;.}.#endif /* S
18e60 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
18e61 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23  RITY_CHECK */..#
18e62 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18e63 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
18e64 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  CK./*.** This ro
18e65 75 74 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d  utine does a com
18e66 70 6c 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74  plete check of t
18e67 68 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 66  he given BTree f
18e68 69 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73  ile.  aRoot[] is
18e69 0a 2a 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20  .** an array of 
18e6a 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65  pages numbers we
18e6b 72 65 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d  re each page num
18e6c 62 65 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20  ber is the root 
18e6d 70 61 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62  page of.** a tab
18e6e 6c 65 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68  le.  nRoot is th
18e6f 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
18e70 69 65 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a  ies in aRoot..**
18e71 0a 2a 2a 20 41 20 72 65 61 64 2d 6f 6e 6c 79 20  .** A read-only 
18e72 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72  or read-write tr
18e73 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
18e74 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20  e opened before 
18e75 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20  calling.** this 
18e76 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
18e77 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72  Write the number
18e78 20 6f 66 20 65 72 72 6f 72 20 73 65 65 6e 20 69   of error seen i
18e79 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 70  n *pnErr.  Excep
18e7a 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72  t for some memor
18e7b 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
18e7c 65 72 72 6f 72 73 2c 20 20 61 6e 20 65 72 72 6f  errors,  an erro
18e7d 72 20 6d 65 73 73 61 67 65 20 68 65 6c 64 20 69  r message held i
18e7e 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
18e7f 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63  d from.** malloc
18e80 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
18e81 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65  *pnErr is non-ze
18e82 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d  ro.  If *pnErr==
18e83 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a  0 then NULL is.*
18e84 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20  * returned.  If 
18e85 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
18e86 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ion error occurs
18e87 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  , NULL is return
18e88 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
18e89 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
18e8a 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74  te3BtreeIntegrit
18e8b 79 43 68 65 63 6b 28 0a 20 20 42 74 72 65 65 20  yCheck(.  Btree 
18e8c 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 62  *p,     /* The b
18e8d 74 72 65 65 20 74 6f 20 62 65 20 63 68 65 63 6b  tree to be check
18e8e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f  ed */.  int *aRo
18e8f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72 61  ot,   /* An arra
18e90 79 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 20  y of root pages 
18e91 6e 75 6d 62 65 72 73 20 66 6f 72 20 69 6e 64 69  numbers for indi
18e92 76 69 64 75 61 6c 20 74 72 65 65 73 20 2a 2f 0a  vidual trees */.
18e93 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 20    int nRoot,    
18e94 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
18e95 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 5b 5d 20  ries in aRoot[] 
18e96 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 2c 20  */.  int mxErr, 
18e97 20 20 20 2f 2a 20 53 74 6f 70 20 72 65 70 6f 72     /* Stop repor
18e98 74 69 6e 67 20 65 72 72 6f 72 73 20 61 66 74 65  ting errors afte
18e99 72 20 74 68 69 73 20 6d 61 6e 79 20 2a 2f 0a 20  r this many */. 
18e9a 20 69 6e 74 20 2a 70 6e 45 72 72 20 20 20 20 2f   int *pnErr    /
18e9b 2a 20 57 72 69 74 65 20 6e 75 6d 62 65 72 20 6f  * Write number o
18e9c 66 20 65 72 72 6f 72 73 20 73 65 65 6e 20 74 6f  f errors seen to
18e9d 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 2a   this variable *
18e9e 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a 20  /.){.  Pgno i;. 
18e9f 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20 49 6e 74   int nRef;.  Int
18ea0 65 67 72 69 74 79 43 6b 20 73 43 68 65 63 6b 3b  egrityCk sCheck;
18ea1 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
18ea2 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 63 68 61   = p->pBt;.  cha
18ea3 72 20 7a 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 20  r zErr[100];..  
18ea4 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
18ea5 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r(p);.  assert( 
18ea6 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53  p->inTrans>TRANS
18ea7 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 69 6e  _NONE && pBt->in
18ea8 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e  Transaction>TRAN
18ea9 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 6e 52 65 66  S_NONE );.  nRef
18eaa 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52   = sqlite3PagerR
18eab 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  efcount(pBt->pPa
18eac 67 65 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e 70  ger);.  sCheck.p
18ead 42 74 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 65  Bt = pBt;.  sChe
18eae 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d  ck.pPager = pBt-
18eaf 3e 70 50 61 67 65 72 3b 0a 20 20 73 43 68 65 63  >pPager;.  sChec
18eb0 6b 2e 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50  k.nPage = pagerP
18eb1 61 67 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e  agecount(sCheck.
18eb2 70 42 74 29 3b 0a 20 20 73 43 68 65 63 6b 2e 6d  pBt);.  sCheck.m
18eb3 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 20  xErr = mxErr;.  
18eb4 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 3b  sCheck.nErr = 0;
18eb5 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63  .  sCheck.malloc
18eb6 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a 70  Failed = 0;.  *p
18eb7 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  nErr = 0;.  if( 
18eb8 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 20  sCheck.nPage==0 
18eb9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
18eba 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20  reeLeave(p);.   
18ebb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
18ebc 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20   sCheck.anRef = 
18ebd 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28  sqlite3Malloc( (
18ebe 73 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a  sCheck.nPage+1)*
18ebf 73 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e  sizeof(sCheck.an
18ec0 52 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  Ref[0]) );.  if(
18ec1 20 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29   !sCheck.anRef )
18ec2 7b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31  {.    *pnErr = 1
18ec3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
18ec4 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20  eeLeave(p);.    
18ec5 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
18ec6 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65  for(i=0; i<=sChe
18ec7 63 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20  ck.nPage; i++){ 
18ec8 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20  sCheck.anRef[i] 
18ec9 3d 20 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e  = 0; }.  i = PEN
18eca 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
18ecb 42 74 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43  Bt);.  if( i<=sC
18ecc 68 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20  heck.nPage ){.  
18ecd 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69    sCheck.anRef[i
18ece 5d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c  ] = 1;.  }.  sql
18ecf 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
18ed0 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c  (&sCheck.errMsg,
18ed1 20 7a 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45   zErr, sizeof(zE
18ed2 72 72 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20  rr), 20000);..  
18ed3 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74  /* Check the int
18ed4 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72  egrity of the fr
18ed5 65 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68  eelist.  */.  ch
18ed6 65 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c  eckList(&sCheck,
18ed7 20 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42   1, get4byte(&pB
18ed8 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
18ed9 5b 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20  [32]),.         
18eda 20 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74     get4byte(&pBt
18edb 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
18edc 33 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65  36]), "Main free
18edd 6c 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20  list: ");..  /* 
18ede 43 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61  Check all the ta
18edf 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  bles..  */.  for
18ee0 28 69 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f  (i=0; (int)i<nRo
18ee1 6f 74 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45  ot && sCheck.mxE
18ee2 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  rr; i++){.    if
18ee3 28 20 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20  ( aRoot[i]==0 ) 
18ee4 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65  continue;.#ifnde
18ee5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
18ee6 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
18ee7 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
18ee8 20 26 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29   && aRoot[i]>1 )
18ee9 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72  {.      checkPtr
18eea 6d 61 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f  map(&sCheck, aRo
18eeb 6f 74 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f  ot[i], PTRMAP_RO
18eec 4f 54 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20  OTPAGE, 0, 0);. 
18eed 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
18eee 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 26 73  checkTreePage(&s
18eef 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c  Check, aRoot[i],
18ef0 20 22 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72   "List of tree r
18ef1 6f 6f 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20  oots: ");.  }.. 
18ef2 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76   /* Make sure ev
18ef3 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
18ef4 66 69 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63  file is referenc
18ef5 65 64 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  ed.  */.  for(i=
18ef6 31 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61  1; i<=sCheck.nPa
18ef7 67 65 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45  ge && sCheck.mxE
18ef8 72 72 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66  rr; i++){.#ifdef
18ef9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
18efa 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
18efb 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d  sCheck.anRef[i]=
18efc 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  =0 ){.      chec
18efd 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65  kAppendMsg(&sChe
18efe 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20  ck, 0, "Page %d 
18eff 69 73 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20  is never used", 
18f00 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a  i);.    }.#else.
18f01 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61      /* If the da
18f02 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20  tabase supports 
18f03 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b  auto-vacuum, mak
18f04 65 20 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73  e sure no tables
18f05 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20   contain.    ** 
18f06 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f  references to po
18f07 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e  inter-map pages.
18f08 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
18f09 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d  sCheck.anRef[i]=
18f0a 3d 30 20 26 26 20 0a 20 20 20 20 20 20 20 28 50  =0 && .       (P
18f0b 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
18f0c 2c 20 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d  , i)!=i || !pBt-
18f0d 3e 61 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a  >autoVacuum) ){.
18f0e 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e        checkAppen
18f0f 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c  dMsg(&sCheck, 0,
18f10 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76   "Page %d is nev
18f11 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20  er used", i);.  
18f12 20 20 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65    }.    if( sChe
18f13 63 6b 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26  ck.anRef[i]!=0 &
18f14 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41  & .       (PTRMA
18f15 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29  P_PAGENO(pBt, i)
18f16 3d 3d 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f  ==i && pBt->auto
18f17 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20  Vacuum) ){.     
18f18 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18f19 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69  &sCheck, 0, "Poi
18f1a 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 25 64  nter map page %d
18f1b 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c   is referenced",
18f1c 20 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69   i);.    }.#endi
18f1d 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  f.  }..  /* Make
18f1e 20 73 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79   sure this analy
18f1f 73 69 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76  sis did not leav
18f20 65 20 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61  e any unref() pa
18f21 67 65 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69  ges..  ** This i
18f22 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f  s an internal co
18f23 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b  nsistency check;
18f24 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68   an integrity ch
18f25 65 63 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  eck.  ** of the 
18f26 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e  integrity check.
18f27 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45  .  */.  if( NEVE
18f28 52 28 6e 52 65 66 20 21 3d 20 73 71 6c 69 74 65  R(nRef != sqlite
18f29 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
18f2a 42 74 2d 3e 70 50 61 67 65 72 29 29 20 29 7b 0a  Bt->pPager)) ){.
18f2b 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18f2c 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 0a  sg(&sCheck, 0, .
18f2d 20 20 20 20 20 20 22 4f 75 74 73 74 61 6e 64 69        "Outstandi
18f2e 6e 67 20 70 61 67 65 20 63 6f 75 6e 74 20 67 6f  ng page count go
18f2f 65 73 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64  es from %d to %d
18f30 20 64 75 72 69 6e 67 20 74 68 69 73 20 61 6e 61   during this ana
18f31 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 20 6e 52  lysis",.      nR
18f32 65 66 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72  ef, sqlite3Pager
18f33 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  Refcount(pBt->pP
18f34 61 67 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 7d  ager).    );.  }
18f35 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 20 75 70  ..  /* Clean  up
18f36 20 61 6e 64 20 72 65 70 6f 72 74 20 65 72 72 6f   and report erro
18f37 72 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  rs..  */.  sqlit
18f38 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
18f39 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
18f3a 73 43 68 65 63 6b 2e 61 6e 52 65 66 29 3b 0a 20  sCheck.anRef);. 
18f3b 20 69 66 28 20 73 43 68 65 63 6b 2e 6d 61 6c 6c   if( sCheck.mall
18f3c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
18f3d 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
18f3e 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72  eset(&sCheck.err
18f3f 4d 73 67 29 3b 0a 20 20 20 20 2a 70 6e 45 72 72  Msg);.    *pnErr
18f40 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 2b 31   = sCheck.nErr+1
18f41 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
18f42 20 20 7d 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73    }.  *pnErr = s
18f43 43 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 69 66  Check.nErr;.  if
18f44 28 20 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d 30  ( sCheck.nErr==0
18f45 20 29 20 73 71 6c 69 74 65 33 53 74 72 41 63 63   ) sqlite3StrAcc
18f46 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e  umReset(&sCheck.
18f47 65 72 72 4d 73 67 29 3b 0a 20 20 72 65 74 75 72  errMsg);.  retur
18f48 6e 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  n sqlite3StrAccu
18f49 6d 46 69 6e 69 73 68 28 26 73 43 68 65 63 6b 2e  mFinish(&sCheck.
18f4a 65 72 72 4d 73 67 29 3b 0a 7d 0a 23 65 6e 64 69  errMsg);.}.#endi
18f4b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
18f4c 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
18f4d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   */../*.** Retur
18f4e 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e  n the full pathn
18f4f 61 6d 65 20 6f 66 20 74 68 65 20 75 6e 64 65 72  ame of the under
18f50 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 66  lying database f
18f51 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ile..**.** The p
18f52 61 67 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 73  ager filename is
18f53 20 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f   invariant as lo
18f54 6e 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20  ng as the pager 
18f55 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74  is.** open so it
18f56 20 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65   is safe to acce
18f57 73 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42  ss without the B
18f58 74 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a  tShared mutex..*
18f59 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18f5a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
18f5b 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65  ite3BtreeGetFile
18f5c 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  name(Btree *p){.
18f5d 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74    assert( p->pBt
18f5e 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20  ->pPager!=0 );. 
18f5f 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50   return sqlite3P
18f60 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d 3e  agerFilename(p->
18f61 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a  pBt->pPager);.}.
18f62 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
18f63 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68  e pathname of th
18f64 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
18f65 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65  or this database
18f66 2e 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20  . The return.** 
18f67 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f  value of this ro
18f68 75 74 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d  utine is the sam
18f69 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  e regardless of 
18f6a 77 68 65 74 68 65 72 20 74 68 65 20 6a 6f 75 72  whether the jour
18f6b 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20  nal file.** has 
18f6c 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f 72 20  been created or 
18f6d 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  not..**.** The p
18f6e 61 67 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ager journal fil
18f6f 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61  ename is invaria
18f70 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68  nt as long as th
18f71 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70  e pager is.** op
18f72 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65  en so it is safe
18f73 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f   to access witho
18f74 75 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20  ut the BtShared 
18f75 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45  mutex..*/.SQLITE
18f76 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
18f77 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65  har *sqlite3Btre
18f78 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  eGetJournalname(
18f79 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
18f7a 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61  ert( p->pBt->pPa
18f7b 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75  ger!=0 );.  retu
18f7c 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  rn sqlite3PagerJ
18f7d 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42  ournalname(p->pB
18f7e 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f  t->pPager);.}../
18f7f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d  *.** Return non-
18f80 7a 65 72 6f 20 69 66 20 61 20 74 72 61 6e 73 61  zero if a transa
18f81 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e  ction is active.
18f82 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18f83 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
18f84 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72  reeIsInTrans(Btr
18f85 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ee *p){.  assert
18f86 28 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ( p==0 || sqlite
18f87 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
18f88 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
18f89 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d  return (p && (p-
18f8a 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
18f8b 57 52 49 54 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  WRITE));.}../*.*
18f8c 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * Return non-zer
18f8d 6f 20 69 66 20 61 20 72 65 61 64 20 28 6f 72 20  o if a read (or 
18f8e 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69  write) transacti
18f8f 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f  on is active..*/
18f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18f91 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
18f92 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74  IsInReadTrans(Bt
18f93 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
18f94 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74  t( p );.  assert
18f95 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
18f96 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
18f97 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  x) );.  return p
18f98 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53  ->inTrans!=TRANS
18f99 5f 4e 4f 4e 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45  _NONE;.}..SQLITE
18f9a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
18f9b 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63  ite3BtreeIsInBac
18f9c 6b 75 70 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  kup(Btree *p){. 
18f9d 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20   assert( p );.  
18f9e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
18f9f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
18fa0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65  ->mutex) );.  re
18fa1 74 75 72 6e 20 70 2d 3e 6e 42 61 63 6b 75 70 21  turn p->nBackup!
18fa2 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  =0;.}../*.** Thi
18fa3 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
18fa4 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
18fa5 61 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79  a blob of memory
18fa6 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
18fa7 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61  .** a single sha
18fa8 72 65 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d  red-btree. The m
18fa9 65 6d 6f 72 79 20 69 73 20 75 73 65 64 20 62 79  emory is used by
18faa 20 63 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72   client code for
18fab 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70   its own.** purp
18fac 6f 73 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c  oses (for exampl
18fad 65 2c 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69  e, to store a hi
18fae 67 68 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20  gh-level schema 
18faf 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
18fb0 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62  .** the shared-b
18fb1 74 72 65 65 29 2e 20 54 68 65 20 62 74 72 65 65  tree). The btree
18fb2 20 6c 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72   layer manages r
18fb3 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e  eference countin
18fb4 67 20 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  g issues..**.** 
18fb5 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
18fb6 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  his is called on
18fb7 20 61 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c   a shared-btree,
18fb8 20 6e 42 79 74 65 73 20 62 79 74 65 73 20 6f 66   nBytes bytes of
18fb9 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61   memory.** are a
18fba 6c 6c 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64  llocated, zeroed
18fbb 2c 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74  , and returned t
18fbc 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f  o the caller. Fo
18fbd 72 20 65 61 63 68 20 73 75 62 73 65 71 75 65 6e  r each subsequen
18fbe 74 20 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e  t .** call the n
18fbf 42 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20  Bytes parameter 
18fc0 69 73 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61  is ignored and a
18fc1 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
18fc2 73 61 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20  same blob.** of 
18fc3 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e  memory returned.
18fc4 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e   .**.** If the n
18fc5 42 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20  Bytes parameter 
18fc6 69 73 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f  is 0 and the blo
18fc7 62 20 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20  b of memory has 
18fc8 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20  not yet been.** 
18fc9 61 6c 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c  allocated, a nul
18fca 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74  l pointer is ret
18fcb 75 72 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c  urned. If the bl
18fcc 6f 62 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ob has already b
18fcd 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  een.** allocated
18fce 2c 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64  , it is returned
18fcf 20 61 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a   as normal..**.*
18fd0 2a 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68  * Just before th
18fd1 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 69  e shared-btree i
18fd2 73 20 63 6c 6f 73 65 64 2c 20 74 68 65 20 66 75  s closed, the fu
18fd3 6e 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73  nction passed as
18fd4 20 74 68 65 20 0a 2a 2a 20 78 46 72 65 65 20 61   the .** xFree a
18fd5 72 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65  rgument when the
18fd6 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
18fd7 6f 6e 20 77 61 73 20 6d 61 64 65 20 69 73 20 69  on was made is i
18fd8 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a  nvoked on the .*
18fd9 2a 20 62 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61  * blob of alloca
18fda 74 65 64 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73  ted memory. This
18fdb 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
18fdc 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65   not call sqlite
18fdd 33 5f 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74  3_free().** on t
18fde 68 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62  he memory, the b
18fdf 74 72 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20  tree layer does 
18fe0 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  that..*/.SQLITE_
18fe1 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
18fe2 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
18fe3 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e  (Btree *p, int n
18fe4 42 79 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72  Bytes, void(*xFr
18fe5 65 65 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20  ee)(void *)){.  
18fe6 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
18fe7 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65  p->pBt;.  sqlite
18fe8 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
18fe9 20 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68    if( !pBt->pSch
18fea 65 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b  ema && nBytes ){
18feb 0a 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d  .    pBt->pSchem
18fec 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  a = sqlite3Mallo
18fed 63 5a 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20  cZero(nBytes);. 
18fee 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68     pBt->xFreeSch
18fef 65 6d 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d  ema = xFree;.  }
18ff0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
18ff1 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
18ff2 6e 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a  n pBt->pSchema;.
18ff3 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
18ff4 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48  SQLITE_LOCKED_SH
18ff5 41 52 45 44 43 41 43 48 45 20 69 66 20 61 6e 6f  AREDCACHE if ano
18ff6 74 68 65 72 20 75 73 65 72 20 6f 66 20 74 68 65  ther user of the
18ff7 20 73 61 6d 65 20 73 68 61 72 65 64 20 0a 2a 2a   same shared .**
18ff8 20 62 74 72 65 65 20 61 73 20 74 68 65 20 61 72   btree as the ar
18ff9 67 75 6d 65 6e 74 20 68 61 6e 64 6c 65 20 68 6f  gument handle ho
18ffa 6c 64 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65  lds an exclusive
18ffb 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a   lock on the .**
18ffc 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
18ffd 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 20  able. Otherwise 
18ffe 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51  SQLITE_OK..*/.SQ
18fff 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19000 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
19001 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20  emaLocked(Btree 
19002 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *p){.  int rc;. 
19003 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
19004 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
19005 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
19006 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
19007 28 70 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72  (p);.  rc = quer
19008 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
19009 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f  eLock(p, MASTER_
1900a 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29  ROOT, READ_LOCK)
1900b 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
1900c 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d  SQLITE_OK || rc=
1900d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53  =SQLITE_LOCKED_S
1900e 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20  HAREDCACHE );.  
1900f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
19010 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
19011 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  c;.}...#ifndef S
19012 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
19013 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62  D_CACHE./*.** Ob
19014 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  tain a lock on t
19015 68 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72  he table whose r
19016 6f 6f 74 20 70 61 67 65 20 69 73 20 69 54 61 62  oot page is iTab
19017 2e 20 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69  .  The.** lock i
19018 73 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69  s a write lock i
19019 66 20 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73  f isWritelock is
1901a 20 74 72 75 65 20 6f 72 20 61 20 72 65 61 64 20   true or a read 
1901b 6c 6f 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73  lock.** if it is
1901c 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54   false..*/.SQLIT
1901d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1901e 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61  lite3BtreeLockTa
1901f 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ble(Btree *p, in
19020 74 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69  t iTab, u8 isWri
19021 74 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72  teLock){.  int r
19022 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
19023 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
19024 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20  ans!=TRANS_NONE 
19025 29 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72  );.  if( p->shar
19026 61 62 6c 65 20 29 7b 0a 20 20 20 20 75 38 20 6c  able ){.    u8 l
19027 6f 63 6b 54 79 70 65 20 3d 20 52 45 41 44 5f 4c  ockType = READ_L
19028 4f 43 4b 20 2b 20 69 73 57 72 69 74 65 4c 6f 63  OCK + isWriteLoc
19029 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 52  k;.    assert( R
1902a 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54  EAD_LOCK+1==WRIT
1902b 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73  E_LOCK );.    as
1902c 73 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63  sert( isWriteLoc
1902d 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c  k==0 || isWriteL
1902e 6f 63 6b 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 73  ock==1 );..    s
1902f 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
19030 28 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 75  (p);.    rc = qu
19031 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61  erySharedCacheTa
19032 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c  bleLock(p, iTab,
19033 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20   lockType);.    
19034 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
19035 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
19036 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61  setSharedCacheTa
19037 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c  bleLock(p, iTab,
19038 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20   lockType);.    
19039 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  }.    sqlite3Btr
1903a 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a  eeLeave(p);.  }.
1903b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
1903c 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
1903d 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
1903e 4c 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65  LOB./*.** Argume
1903f 6e 74 20 70 43 73 72 20 6d 75 73 74 20 62 65 20  nt pCsr must be 
19040 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20  a cursor opened 
19041 66 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61  for writing on a
19042 6e 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62  n .** INTKEY tab
19043 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  le currently poi
19044 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64  nting at a valid
19045 20 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a   table entry. .*
19046 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
19047 6d 6f 64 69 66 69 65 73 20 74 68 65 20 64 61 74  modifies the dat
19048 61 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74  a stored as part
19049 20 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a   of that entry..
1904a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 64  **.** Only the d
1904b 61 74 61 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20  ata content may 
1904c 6f 6e 6c 79 20 62 65 20 6d 6f 64 69 66 69 65 64  only be modified
1904d 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  , it is not poss
1904e 69 62 6c 65 20 74 6f 20 0a 2a 2a 20 63 68 61 6e  ible to .** chan
1904f 67 65 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  ge the length of
19050 20 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64   the data stored
19051 2e 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69  . If this functi
19052 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74  on is called wit
19053 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20  h.** parameters 
19054 74 68 61 74 20 61 74 74 65 6d 70 74 20 74 6f 20  that attempt to 
19055 77 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65  write past the e
19056 6e 64 20 6f 66 20 74 68 65 20 65 78 69 73 74 69  nd of the existi
19057 6e 67 20 64 61 74 61 2c 0a 2a 2a 20 6e 6f 20 6d  ng data,.** no m
19058 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65  odifications are
19059 20 6d 61 64 65 20 61 6e 64 20 53 51 4c 49 54 45   made and SQLITE
1905a 5f 43 4f 52 52 55 50 54 20 69 73 20 72 65 74 75  _CORRUPT is retu
1905b 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
1905c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1905d 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28  te3BtreePutData(
1905e 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20  BtCursor *pCsr, 
1905f 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
19060 61 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20  amt, void *z){. 
19061 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
19062 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
19063 74 65 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61  tex(pCsr) );.  a
19064 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
19065 75 74 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e  utex_held(pCsr->
19066 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65  pBtree->db->mute
19067 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
19068 70 43 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62  pCsr->isIncrblob
19069 48 61 6e 64 6c 65 20 29 3b 0a 0a 20 20 72 63 20  Handle );..  rc 
1906a 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  = restoreCursorP
1906b 6f 73 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a 20  osition(pCsr);. 
1906c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1906d 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
1906e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72   rc;.  }.  asser
1906f 74 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21  t( pCsr->eState!
19070 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53  =CURSOR_REQUIRES
19071 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73  EEK );.  if( pCs
19072 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f  r->eState!=CURSO
19073 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72  R_VALID ){.    r
19074 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f  eturn SQLITE_ABO
19075 52 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  RT;.  }..  /* Ch
19076 65 63 6b 20 73 6f 6d 65 20 61 73 73 75 6d 70 74  eck some assumpt
19077 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61  ions: .  **   (a
19078 29 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  ) the cursor is 
19079 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67  open for writing
1907a 2c 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65  ,.  **   (b) the
1907b 72 65 20 69 73 20 61 20 72 65 61 64 2f 77 72 69  re is a read/wri
1907c 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te transaction o
1907d 70 65 6e 2c 0a 20 20 2a 2a 20 20 20 28 63 29 20  pen,.  **   (c) 
1907e 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  the connection h
1907f 6f 6c 64 73 20 61 20 77 72 69 74 65 2d 6c 6f 63  olds a write-loc
19080 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 28  k on the table (
19081 69 66 20 72 65 71 75 69 72 65 64 29 2c 0a 20 20  if required),.  
19082 2a 2a 20 20 20 28 64 29 20 74 68 65 72 65 20 61  **   (d) there a
19083 72 65 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e  re no conflictin
19084 67 20 72 65 61 64 2d 6c 6f 63 6b 73 2c 20 61 6e  g read-locks, an
19085 64 0a 20 20 2a 2a 20 20 20 28 65 29 20 74 68 65  d.  **   (e) the
19086 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 61   cursor points a
19087 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20 6f 66  t a valid row of
19088 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65   an intKey table
19089 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43  ..  */.  if( !pC
1908a 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20  sr->wrFlag ){.  
1908b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1908c 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20  READONLY;.  }.  
1908d 61 73 73 65 72 74 28 20 21 70 43 73 72 2d 3e 70  assert( !pCsr->p
1908e 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20  Bt->readOnly && 
1908f 70 43 73 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61  pCsr->pBt->inTra
19090 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
19091 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72  WRITE );.  asser
19092 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68  t( hasSharedCach
19093 65 54 61 62 6c 65 4c 6f 63 6b 28 70 43 73 72 2d  eTableLock(pCsr-
19094 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70  >pBtree, pCsr->p
19095 67 6e 6f 52 6f 6f 74 2c 20 30 2c 20 32 29 20 29  gnoRoot, 0, 2) )
19096 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73  ;.  assert( !has
19097 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 43  ReadConflicts(pC
19098 73 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 73 72  sr->pBtree, pCsr
19099 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0a 20  ->pgnoRoot) );. 
1909a 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 61   assert( pCsr->a
1909b 70 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67  pPage[pCsr->iPag
1909c 65 5d 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20  e]->intKey );.. 
1909d 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61   return accessPa
1909e 79 6c 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73  yload(pCsr, offs
1909f 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e  et, amt, (unsign
190a0 65 64 20 63 68 61 72 20 2a 29 7a 2c 20 31 29 3b  ed char *)z, 1);
190a1 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61  .}../* .** Set a
190a2 20 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75   flag on this cu
190a3 72 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68  rsor to cache th
190a4 65 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70  e locations of p
190a5 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a  ages from the .*
190a6 2a 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20  * overflow list 
190a7 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
190a8 72 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73 65  row. This is use
190a9 64 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65  d by cursors ope
190aa 6e 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65  ned.** for incre
190ab 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f  mental blob IO o
190ac 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  nly..**.** This 
190ad 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20  function sets a 
190ae 66 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61  flag only. The a
190af 63 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74  ctual page locat
190b0 69 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74  ion cache.** (st
190b1 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72  ored in BtCursor
190b2 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73  .aOverflow[]) is
190b3 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75   allocated and u
190b4 73 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a  sed by function.
190b5 2a 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64  ** accessPayload
190b6 28 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66  () (the worker f
190b7 75 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69  unction for sqli
190b8 74 65 33 42 74 72 65 65 44 61 74 61 28 29 20 61  te3BtreeData() a
190b9 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72  nd.** sqlite3Btr
190ba 65 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f  eePutData())..*/
190bb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
190bc 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
190bd 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42  eCacheOverflow(B
190be 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
190bf 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
190c0 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
190c1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
190c2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
190c3 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64  (pCur->pBtree->d
190c4 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
190c5 73 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49  ssert(!pCur->isI
190c6 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a  ncrblobHandle);.
190c7 20 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e    assert(!pCur->
190c8 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43  aOverflow);.  pC
190c9 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61  ur->isIncrblobHa
190ca 6e 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64  ndle = 1;.}.#end
190cb 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
190cc 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65  *** End of btree
190cd 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
190ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
190d1 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
190d2 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ackup.c ********
190d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190d5 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4a  **/./*.** 2009 J
190d6 61 6e 75 61 72 79 20 32 38 0a 2a 2a 0a 2a 2a 20  anuary 28.**.** 
190d7 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
190d8 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
190d9 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
190da 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
190db 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
190dc 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
190dd 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
190de 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
190df 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
190e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
190e1 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
190e2 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
190e3 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
190e4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
190e5 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
190e6 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
190e7 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
190e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190ec 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
190ed 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
190ee 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
190ef 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
190f0 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 0a 2a  _backup_XXX() .*
190f1 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20  * API functions 
190f2 61 6e 64 20 74 68 65 20 72 65 6c 61 74 65 64 20  and the related 
190f3 66 65 61 74 75 72 65 73 2e 0a 2a 2f 0a 0a 2f 2a  features..*/../*
190f4 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74   Macro to find t
190f5 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77  he minimum of tw
190f6 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73  o numeric values
190f7 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e  ..*/.#ifndef MIN
190f8 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c  .# define MIN(x,
190f9 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a  y) ((x)<(y)?(x):
190fa 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  (y)).#endif../*.
190fb 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c  ** Structure all
190fc 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
190fd 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
190fe 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
190ff 74 65 33 5f 62 61 63 6b 75 70 20 7b 0a 20 20 73  te3_backup {.  s
19100 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 3b  qlite3* pDestDb;
19101 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69          /* Desti
19102 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
19103 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65  handle */.  Btre
19104 65 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20  e *pDest;       
19105 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74       /* Destinat
19106 69 6f 6e 20 62 2d 74 72 65 65 20 66 69 6c 65 20  ion b-tree file 
19107 2a 2f 0a 20 20 75 33 32 20 69 44 65 73 74 53 63  */.  u32 iDestSc
19108 68 65 6d 61 3b 20 20 20 20 20 20 20 20 20 2f 2a  hema;         /*
19109 20 4f 72 69 67 69 6e 61 6c 20 73 63 68 65 6d 61   Original schema
1910a 20 63 6f 6f 6b 69 65 20 69 6e 20 64 65 73 74 69   cookie in desti
1910b 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nation */.  int 
1910c 62 44 65 73 74 4c 6f 63 6b 65 64 3b 20 20 20 20  bDestLocked;    
1910d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63       /* True onc
1910e 65 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61  e a write-transa
1910f 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e  ction is open on
19110 20 70 44 65 73 74 20 2a 2f 0a 0a 20 20 50 67 6e   pDest */..  Pgn
19111 6f 20 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20  o iNext;        
19112 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
19113 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74  mber of the next
19114 20 73 6f 75 72 63 65 20 70 61 67 65 20 74 6f 20   source page to 
19115 63 6f 70 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65  copy */.  sqlite
19116 33 2a 20 70 53 72 63 44 62 3b 20 20 20 20 20 20  3* pSrcDb;      
19117 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74     /* Source dat
19118 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
19119 20 20 42 74 72 65 65 20 2a 70 53 72 63 3b 20 20    Btree *pSrc;  
1911a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
1911b 75 72 63 65 20 62 2d 74 72 65 65 20 66 69 6c 65  urce b-tree file
1911c 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 3b 20 20   */..  int rc;  
1911d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1911e 2f 2a 20 42 61 63 6b 75 70 20 70 72 6f 63 65 73  /* Backup proces
1911f 73 20 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a  s error code */.
19120 0a 20 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20  .  /* These two 
19121 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 73 65  variables are se
19122 74 20 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20  t by every call 
19123 74 6f 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29  to backup_step()
19124 2e 20 54 68 65 79 20 61 72 65 0a 20 20 2a 2a 20  . They are.  ** 
19125 72 65 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f  read by calls to
19126 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e   backup_remainin
19127 67 28 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70  g() and backup_p
19128 61 67 65 63 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f  agecount()..  */
19129 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e 69  .  Pgno nRemaini
1912a 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ng;         /* N
1912b 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6c  umber of pages l
1912c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20  eft to copy */. 
1912d 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75 6e 74   Pgno nPagecount
1912e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74  ;         /* Tot
1912f 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
19130 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20  es to copy */.. 
19131 20 69 6e 74 20 69 73 41 74 74 61 63 68 65 64 3b   int isAttached;
19132 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
19133 65 20 6f 6e 63 65 20 62 61 63 6b 75 70 20 68 61  e once backup ha
19134 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  s been registere
19135 64 20 77 69 74 68 20 70 61 67 65 72 20 2a 2f 0a  d with pager */.
19136 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
19137 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65   *pNext;   /* Ne
19138 78 74 20 62 61 63 6b 75 70 20 61 73 73 6f 63 69  xt backup associ
19139 61 74 65 64 20 77 69 74 68 20 73 6f 75 72 63 65  ated with source
1913a 20 70 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pager */.};../*
1913b 0a 2a 2a 20 54 48 52 45 41 44 20 53 41 46 45 54  .** THREAD SAFET
1913c 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 20  Y NOTES:.**.**  
1913d 20 4f 6e 63 65 20 69 74 20 68 61 73 20 62 65 65   Once it has bee
1913e 6e 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  n created using 
1913f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 61  backup_init(), a
19140 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 5f   single sqlite3_
19141 62 61 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 75  backup.**   stru
19142 63 74 75 72 65 20 6d 61 79 20 62 65 20 61 63 63  cture may be acc
19143 65 73 73 65 64 20 76 69 61 20 74 77 6f 20 67 72  essed via two gr
19144 6f 75 70 73 20 6f 66 20 74 68 72 65 61 64 2d 73  oups of thread-s
19145 61 66 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  afe entry points
19146 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69  :.**.**     * Vi
19147 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61  a the sqlite3_ba
19148 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 66  ckup_XXX() API f
19149 75 6e 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73  unction backup_s
1914a 74 65 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 20  tep() and .**   
1914b 20 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73      backup_finis
1914c 68 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20  h(). Both these 
1914d 66 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e  functions obtain
1914e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
1914f 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 61  base.**       ha
19150 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74  ndle mutex and t
19151 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61  he mutex associa
19152 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75  ted with the sou
19153 72 63 65 20 42 74 53 68 61 72 65 64 20 0a 2a 2a  rce BtShared .**
19154 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72 65         structure
19155 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72 2e  , in that order.
19156 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61  .**.**     * Via
19157 20 74 68 65 20 42 61 63 6b 75 70 55 70 64 61 74   the BackupUpdat
19158 65 28 29 20 61 6e 64 20 42 61 63 6b 75 70 52 65  e() and BackupRe
19159 73 74 61 72 74 28 29 20 66 75 6e 63 74 69 6f 6e  start() function
1915a 73 2c 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20  s, which are.** 
1915b 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 79        invoked by
1915c 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72   the pager layer
1915d 20 74 6f 20 72 65 70 6f 72 74 20 76 61 72 69 6f   to report vario
1915e 75 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 73  us state changes
1915f 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 65   in.**       the
19160 20 70 61 67 65 20 63 61 63 68 65 20 61 73 73 6f   page cache asso
19161 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
19162 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e  source database.
19163 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20   The mutex.**   
19164 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 77      associated w
19165 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 64  ith the source d
19166 61 74 61 62 61 73 65 20 42 74 53 68 61 72 65 64  atabase BtShared
19167 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20   structure will 
19168 61 6c 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 20  always .**      
19169 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 65 69   be held when ei
1916a 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 66 75  ther of these fu
1916b 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f  nctions are invo
1916c 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65  ked..**.**   The
1916d 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 62   other sqlite3_b
1916e 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20  ackup_XXX() API 
1916f 66 75 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b 75  functions, backu
19170 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e  p_remaining() an
19171 64 0a 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 61  d.**   backup_pa
19172 67 65 63 6f 75 6e 74 28 29 20 61 72 65 20 6e 6f  gecount() are no
19173 74 20 74 68 72 65 61 64 2d 73 61 66 65 20 66 75  t thread-safe fu
19174 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 79  nctions. If they
19175 20 61 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 20   are called.**  
19176 20 77 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 65   while some othe
19177 72 20 74 68 72 65 61 64 20 69 73 20 63 61 6c 6c  r thread is call
19178 69 6e 67 20 62 61 63 6b 75 70 5f 73 74 65 70 28  ing backup_step(
19179 29 20 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e 69  ) or backup_fini
1917a 73 68 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 76  sh(),.**   the v
1917b 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 6d  alues returned m
1917c 61 79 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 54  ay be invalid. T
1917d 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66  here is no way f
1917e 6f 72 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  or a call to.** 
1917f 20 20 42 61 63 6b 75 70 55 70 64 61 74 65 28 29    BackupUpdate()
19180 20 6f 72 20 42 61 63 6b 75 70 52 65 73 74 61 72   or BackupRestar
19181 74 28 29 20 74 6f 20 69 6e 74 65 72 66 65 72 65  t() to interfere
19182 20 77 69 74 68 20 62 61 63 6b 75 70 5f 72 65 6d   with backup_rem
19183 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f 72  aining().**   or
19184 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e   backup_pagecoun
19185 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 70  t()..**.**   Dep
19186 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51  ending on the SQ
19187 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69  Lite configurati
19188 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  on, the database
19189 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0a   handles and/or.
1918a 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20 6f  **   the Btree o
1918b 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65 20  bjects may have 
1918c 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 65  their own mutexe
1918d 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20 6c  s that require l
1918e 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f 6e  ocking..**   Non
1918f 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73  -sharable Btrees
19190 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61   (in-memory data
19191 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d 70 6c  bases for exampl
19192 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a  e), do not have.
19193 2a 2a 20 20 20 61 73 73 6f 63 69 61 74 65 64 20  **   associated 
19194 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a  mutexes..*/../*.
19195 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
19196 74 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ter correspondin
19197 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 7a 44  g to database zD
19198 62 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20  b (i.e. "main", 
19199 22 74 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 6f  "temp").** in co
1919a 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
1919b 70 44 62 2e 20 49 66 20 73 75 63 68 20 61 20 64  pDb. If such a d
1919c 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62  atabase cannot b
1919d 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 0a  e found, return.
1919e 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ** a NULL pointe
1919f 72 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 65  r and write an e
191a0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20  rror message to 
191a1 70 45 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a 20  pErrorDb..**.** 
191a2 49 66 20 74 68 65 20 22 74 65 6d 70 22 20 64 61  If the "temp" da
191a3 74 61 62 61 73 65 20 69 73 20 72 65 71 75 65 73  tabase is reques
191a4 74 65 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 64  ted, it may need
191a5 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 79   to be opened by
191a6 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   this .** functi
191a7 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  on. If an error 
191a8 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69  occurs while doi
191a9 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 20  ng so, return 0 
191aa 61 6e 64 20 77 72 69 74 65 20 61 6e 20 0a 2a 2a  and write an .**
191ab 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
191ac 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a 73  o pErrorDb..*/.s
191ad 74 61 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e  tatic Btree *fin
191ae 64 42 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a  dBtree(sqlite3 *
191af 70 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65  pErrorDb, sqlite
191b0 33 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68  3 *pDb, const ch
191b1 61 72 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 20  ar *zDb){.  int 
191b2 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44  i = sqlite3FindD
191b3 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b  bName(pDb, zDb);
191b4 0a 0a 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a  ..  if( i==1 ){.
191b5 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73      Parse *pPars
191b6 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  e;.    int rc = 
191b7 30 3b 0a 20 20 20 20 70 50 61 72 73 65 20 3d 20  0;.    pParse = 
191b8 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
191b9 63 5a 65 72 6f 28 70 45 72 72 6f 72 44 62 2c 20  cZero(pErrorDb, 
191ba 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29  sizeof(*pParse))
191bb 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  ;.    if( pParse
191bc 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
191bd 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72  ite3Error(pError
191be 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  Db, SQLITE_NOMEM
191bf 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  , "out of memory
191c0 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ");.      rc = S
191c1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
191c2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50   }else{.      pP
191c3 61 72 73 65 2d 3e 64 62 20 3d 20 70 44 62 3b 0a  arse->db = pDb;.
191c4 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
191c5 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
191c6 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  e(pParse) ){.   
191c7 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
191c8 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a  rClear(pParse);.
191c9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
191ca 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 70  rror(pErrorDb, p
191cb 50 61 72 73 65 2d 3e 72 63 2c 20 22 25 73 22 2c  Parse->rc, "%s",
191cc 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
191cd 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
191ce 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
191cf 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
191d0 74 65 33 53 74 61 63 6b 46 72 65 65 28 70 45 72  te3StackFree(pEr
191d1 72 6f 72 44 62 2c 20 70 50 61 72 73 65 29 3b 0a  rorDb, pParse);.
191d2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
191d3 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
191d4 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
191d5 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20   if( i<0 ){.    
191d6 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72  sqlite3Error(pEr
191d7 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f 45 52  rorDb, SQLITE_ER
191d8 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61  ROR, "unknown da
191d9 74 61 62 61 73 65 20 25 73 22 2c 20 7a 44 62 29  tabase %s", zDb)
191da 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
191db 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 44    }..  return pD
191dc 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 7d  b->aDb[i].pBt;.}
191dd 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
191de 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  n sqlite3_backup
191df 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79   process to copy
191e0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
191e1 20 7a 53 72 63 44 62 20 66 72 6f 6d 0a 2a 2a 20   zSrcDb from.** 
191e2 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
191e3 65 20 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73  e pSrcDb to zDes
191e4 74 44 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20  tDb in pDestDb. 
191e5 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  If successful, r
191e6 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
191e7 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71  er to the new sq
191e8 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a  lite3_backup obj
191e9 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ect..**.** If an
191ea 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
191eb 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
191ec 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
191ed 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73  e and error mess
191ee 61 67 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e  age.** stored in
191ef 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
191f0 20 70 44 65 73 74 44 62 2e 0a 2a 2f 0a 53 51 4c   pDestDb..*/.SQL
191f1 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
191f2 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f  backup *sqlite3_
191f3 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73  backup_init(.  s
191f4 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 2c  qlite3* pDestDb,
191f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
191f6 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
191f7 20 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a   to write to */.
191f8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
191f9 65 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20  estDb,          
191fa 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
191fb 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74 68  of database with
191fc 69 6e 20 70 44 65 73 74 44 62 20 2a 2f 0a 20 20  in pDestDb */.  
191fd 73 71 6c 69 74 65 33 2a 20 70 53 72 63 44 62 2c  sqlite3* pSrcDb,
191fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
191ff 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
19200 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  e connection to 
19201 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 63  read from */.  c
19202 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 72 63 44  onst char *zSrcD
19203 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b               
19204 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
19205 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20  database within 
19206 70 53 72 63 44 62 20 2a 2f 0a 29 7b 0a 20 20 73  pSrcDb */.){.  s
19207 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
19208 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19209 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
1920a 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a   return */..  /*
1920b 20 4c 6f 63 6b 20 74 68 65 20 73 6f 75 72 63 65   Lock the source
1920c 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1920d 2e 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f  . The destinatio
1920e 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  n database.  ** 
1920f 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f  handle is not lo
19210 63 6b 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75  cked in this rou
19211 74 69 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20  tine, but it is 
19212 6c 6f 63 6b 65 64 20 69 6e 0a 20 20 2a 2a 20 73  locked in.  ** s
19213 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
19214 65 70 28 29 2e 20 54 68 65 20 75 73 65 72 20 69  ep(). The user i
19215 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e  s required to en
19216 73 75 72 65 20 74 68 61 74 20 6e 6f 0a 20 20 2a  sure that no.  *
19217 2a 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61  * other thread a
19218 63 63 65 73 73 65 73 20 74 68 65 20 64 65 73 74  ccesses the dest
19219 69 6e 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66  ination handle f
1921a 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0a  or the duration.
1921b 20 20 2a 2a 20 6f 66 20 74 68 65 20 62 61 63 6b    ** of the back
1921c 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41  up operation.  A
1921d 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ny attempt to us
1921e 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  e the destinatio
1921f 6e 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  n.  ** database 
19220 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65  connection while
19221 20 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20   a backup is in 
19222 70 72 6f 67 72 65 73 73 20 6d 61 79 20 63 61 75  progress may cau
19223 73 65 0a 20 20 2a 2a 20 61 20 6d 61 6c 66 75 6e  se.  ** a malfun
19224 63 74 69 6f 6e 20 6f 72 20 61 20 64 65 61 64 6c  ction or a deadl
19225 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ock..  */.  sqli
19226 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
19227 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  pSrcDb->mutex);.
19228 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
19229 65 6e 74 65 72 28 70 44 65 73 74 44 62 2d 3e 6d  enter(pDestDb->m
1922a 75 74 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 53  utex);..  if( pS
1922b 72 63 44 62 3d 3d 70 44 65 73 74 44 62 20 29 7b  rcDb==pDestDb ){
1922c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1922d 72 28 0a 20 20 20 20 20 20 20 20 70 44 65 73 74  r(.        pDest
1922e 44 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  Db, SQLITE_ERROR
1922f 2c 20 22 73 6f 75 72 63 65 20 61 6e 64 20 64 65  , "source and de
19230 73 74 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62  stination must b
19231 65 20 64 69 73 74 69 6e 63 74 22 0a 20 20 20 20  e distinct".    
19232 29 3b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20  );.    p = 0;.  
19233 7d 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41  }else {.    /* A
19234 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
19235 72 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f  r a new sqlite3_
19236 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 2a 2f  backup object */
19237 0a 20 20 20 20 70 20 3d 20 28 73 71 6c 69 74 65  .    p = (sqlite
19238 33 5f 62 61 63 6b 75 70 20 2a 29 73 71 6c 69 74  3_backup *)sqlit
19239 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
1923a 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29  (sqlite3_backup)
1923b 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b  );.    if( !p ){
1923c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1923d 72 6f 72 28 70 44 65 73 74 44 62 2c 20 53 51 4c  ror(pDestDb, SQL
1923e 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20  ITE_NOMEM, 0);. 
1923f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
19240 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
19241 20 73 75 63 63 65 65 64 65 64 2c 20 70 6f 70 75   succeeded, popu
19242 6c 61 74 65 20 74 68 65 20 6e 65 77 20 6f 62 6a  late the new obj
19243 65 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 20  ect. */.  if( p 
19244 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c  ){.    memset(p,
19245 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74   0, sizeof(sqlit
19246 65 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20  e3_backup));.   
19247 20 70 2d 3e 70 53 72 63 20 3d 20 66 69 6e 64 42   p->pSrc = findB
19248 74 72 65 65 28 70 44 65 73 74 44 62 2c 20 70 53  tree(pDestDb, pS
19249 72 63 44 62 2c 20 7a 53 72 63 44 62 29 3b 0a 20  rcDb, zSrcDb);. 
1924a 20 20 20 70 2d 3e 70 44 65 73 74 20 3d 20 66 69     p->pDest = fi
1924b 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62 2c  ndBtree(pDestDb,
1924c 20 70 44 65 73 74 44 62 2c 20 7a 44 65 73 74 44   pDestDb, zDestD
1924d 62 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74  b);.    p->pDest
1924e 44 62 20 3d 20 70 44 65 73 74 44 62 3b 0a 20 20  Db = pDestDb;.  
1924f 20 20 70 2d 3e 70 53 72 63 44 62 20 3d 20 70 53    p->pSrcDb = pS
19250 72 63 44 62 3b 0a 20 20 20 20 70 2d 3e 69 4e 65  rcDb;.    p->iNe
19251 78 74 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 69  xt = 1;.    p->i
19252 73 41 74 74 61 63 68 65 64 20 3d 20 30 3b 0a 0a  sAttached = 0;..
19253 20 20 20 20 69 66 28 20 30 3d 3d 70 2d 3e 70 53      if( 0==p->pS
19254 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e 70 44 65 73  rc || 0==p->pDes
19255 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e  t ){.      /* On
19256 65 20 28 6f 72 20 62 6f 74 68 29 20 6f 66 20 74  e (or both) of t
19257 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73  he named databas
19258 65 73 20 64 69 64 20 6e 6f 74 20 65 78 69 73 74  es did not exist
19259 2e 20 41 6e 20 65 72 72 6f 72 20 68 61 73 0a 20  . An error has. 
1925a 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20       ** already 
1925b 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74  been written int
1925c 6f 20 74 68 65 20 70 44 65 73 74 44 62 20 68 61  o the pDestDb ha
1925d 6e 64 6c 65 2e 20 41 6c 6c 20 74 68 61 74 20 69  ndle. All that i
1925e 73 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2a 20  s left.      ** 
1925f 74 6f 20 64 6f 20 68 65 72 65 20 69 73 20 66 72  to do here is fr
19260 65 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  ee the sqlite3_b
19261 61 63 6b 75 70 20 73 74 72 75 63 74 75 72 65 2e  ackup structure.
19262 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
19263 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
19264 0a 20 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20  .      p = 0;.  
19265 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20    }.  }.  if( p 
19266 29 7b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e  ){.    p->pSrc->
19267 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a  nBackup++;.  }..
19268 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
19269 6c 65 61 76 65 28 70 44 65 73 74 44 62 2d 3e 6d  leave(pDestDb->m
1926a 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  utex);.  sqlite3
1926b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 72  _mutex_leave(pSr
1926c 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  cDb->mutex);.  r
1926d 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
1926e 2a 20 41 72 67 75 6d 65 6e 74 20 72 63 20 69 73  * Argument rc is
1926f 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
19270 20 63 6f 64 65 2e 20 52 65 74 75 72 6e 20 74 72   code. Return tr
19271 75 65 20 69 66 20 74 68 69 73 20 65 72 72 6f 72  ue if this error
19272 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72   is .** consider
19273 65 64 20 66 61 74 61 6c 20 69 66 20 65 6e 63 6f  ed fatal if enco
19274 75 6e 74 65 72 65 64 20 64 75 72 69 6e 67 20 61  untered during a
19275 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
19276 6e 2e 20 41 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a  n. All errors.**
19277 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20   are considered 
19278 66 61 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72  fatal except for
19279 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64   SQLITE_BUSY and
1927a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a   SQLITE_LOCKED..
1927b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
1927c 46 61 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72  FatalError(int r
1927d 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 72 63  c){.  return (rc
1927e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  !=SQLITE_OK && r
1927f 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26  c!=SQLITE_BUSY &
19280 26 20 41 4c 57 41 59 53 28 72 63 21 3d 53 51 4c  & ALWAYS(rc!=SQL
19281 49 54 45 5f 4c 4f 43 4b 45 44 29 29 3b 0a 7d 0a  ITE_LOCKED));.}.
19282 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  ./*.** Parameter
19283 20 7a 53 72 63 44 61 74 61 20 70 6f 69 6e 74 73   zSrcData points
19284 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e   to a buffer con
19285 74 61 69 6e 69 6e 67 20 74 68 65 20 64 61 74 61  taining the data
19286 20 66 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 53   for .** page iS
19287 72 63 50 67 20 66 72 6f 6d 20 74 68 65 20 73 6f  rcPg from the so
19288 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 43  urce database. C
19289 6f 70 79 20 74 68 69 73 20 64 61 74 61 20 69 6e  opy this data in
1928a 74 6f 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 69  to the .** desti
1928b 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e  nation database.
1928c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
1928d 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 6c  ackupOnePage(sql
1928e 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20  ite3_backup *p, 
1928f 50 67 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f 6e  Pgno iSrcPg, con
19290 73 74 20 75 38 20 2a 7a 53 72 63 44 61 74 61 29  st u8 *zSrcData)
19291 7b 0a 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73  {.  Pager * cons
19292 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20 73  t pDestPager = s
19293 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
19294 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f  (p->pDest);.  co
19295 6e 73 74 20 69 6e 74 20 6e 53 72 63 50 67 73 7a  nst int nSrcPgsz
19296 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
19297 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53  etPageSize(p->pS
19298 72 63 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 74  rc);.  int nDest
19299 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74  Pgsz = sqlite3Bt
1929a 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
1929b 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73  ->pDest);.  cons
1929c 74 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49  t int nCopy = MI
1929d 4e 28 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 73  N(nSrcPgsz, nDes
1929e 74 50 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 20  tPgsz);.  const 
1929f 69 36 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29  i64 iEnd = (i64)
192a0 69 53 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63  iSrcPg*(i64)nSrc
192a1 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 20 72 63 20  Pgsz;..  int rc 
192a2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
192a3 36 34 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 65  64 iOff;..  asse
192a4 72 74 28 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b  rt( p->bDestLock
192a5 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ed );.  assert( 
192a6 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 70 2d  !isFatalError(p-
192a7 3e 72 63 29 20 29 3b 0a 20 20 61 73 73 65 72 74  >rc) );.  assert
192a8 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e  ( iSrcPg!=PENDIN
192a9 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70  G_BYTE_PAGE(p->p
192aa 53 72 63 2d 3e 70 42 74 29 20 29 3b 0a 20 20 61  Src->pBt) );.  a
192ab 73 73 65 72 74 28 20 7a 53 72 63 44 61 74 61 20  ssert( zSrcData 
192ac 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 63 68 20 74  );..  /* Catch t
192ad 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68  he case where th
192ae 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73  e destination is
192af 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
192b0 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 0a 20  tabase and the. 
192b1 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 73 20 6f   ** page sizes o
192b2 66 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64  f the source and
192b3 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 69 66   destination dif
192b4 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  fer. .  */.  if(
192b5 20 6e 53 72 63 50 67 73 7a 21 3d 6e 44 65 73 74   nSrcPgsz!=nDest
192b6 50 67 73 7a 20 26 26 20 73 71 6c 69 74 65 33 50  Pgsz && sqlite3P
192b7 61 67 65 72 49 73 4d 65 6d 64 62 28 73 71 6c 69  agerIsMemdb(sqli
192b8 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d  te3BtreePager(p-
192b9 3e 70 44 65 73 74 29 29 20 29 7b 0a 20 20 20 20  >pDest)) ){.    
192ba 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44  rc = SQLITE_READ
192bb 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ONLY;.  }..  /* 
192bc 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f  This loop runs o
192bd 6e 63 65 20 66 6f 72 20 65 61 63 68 20 64 65 73  nce for each des
192be 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 70  tination page sp
192bf 61 6e 6e 65 64 20 62 79 20 74 68 65 20 73 6f 75  anned by the sou
192c0 72 63 65 20 0a 20 20 2a 2a 20 70 61 67 65 2e 20  rce .  ** page. 
192c1 46 6f 72 20 65 61 63 68 20 69 74 65 72 61 74 69  For each iterati
192c2 6f 6e 2c 20 76 61 72 69 61 62 6c 65 20 69 4f 66  on, variable iOf
192c3 66 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  f is set to the 
192c4 62 79 74 65 20 6f 66 66 73 65 74 0a 20 20 2a 2a  byte offset.  **
192c5 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   of the destinat
192c6 69 6f 6e 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20  ion page..  */. 
192c7 20 66 6f 72 28 69 4f 66 66 3d 69 45 6e 64 2d 28   for(iOff=iEnd-(
192c8 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 20 72 63  i64)nSrcPgsz; rc
192c9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
192ca 4f 66 66 3c 69 45 6e 64 3b 20 69 4f 66 66 2b 3d  Off<iEnd; iOff+=
192cb 6e 44 65 73 74 50 67 73 7a 29 7b 0a 20 20 20 20  nDestPgsz){.    
192cc 44 62 50 61 67 65 20 2a 70 44 65 73 74 50 67 20  DbPage *pDestPg 
192cd 3d 20 30 3b 0a 20 20 20 20 50 67 6e 6f 20 69 44  = 0;.    Pgno iD
192ce 65 73 74 20 3d 20 28 50 67 6e 6f 29 28 69 4f 66  est = (Pgno)(iOf
192cf 66 2f 6e 44 65 73 74 50 67 73 7a 29 2b 31 3b 0a  f/nDestPgsz)+1;.
192d0 20 20 20 20 69 66 28 20 69 44 65 73 74 3d 3d 50      if( iDest==P
192d1 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
192d2 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20  (p->pDest->pBt) 
192d3 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
192d4 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  if( SQLITE_OK==(
192d5 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
192d6 72 47 65 74 28 70 44 65 73 74 50 61 67 65 72 2c  rGet(pDestPager,
192d7 20 69 44 65 73 74 2c 20 26 70 44 65 73 74 50 67   iDest, &pDestPg
192d8 29 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54  )).     && SQLIT
192d9 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
192da 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44  te3PagerWrite(pD
192db 65 73 74 50 67 29 29 0a 20 20 20 20 29 7b 0a 20  estPg)).    ){. 
192dc 20 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a       const u8 *z
192dd 49 6e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b 69  In = &zSrcData[i
192de 4f 66 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0a 20  Off%nSrcPgsz];. 
192df 20 20 20 20 20 75 38 20 2a 7a 44 65 73 74 44 61       u8 *zDestDa
192e0 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ta = sqlite3Page
192e1 72 47 65 74 44 61 74 61 28 70 44 65 73 74 50 67  rGetData(pDestPg
192e2 29 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 75  );.      u8 *zOu
192e3 74 20 3d 20 26 7a 44 65 73 74 44 61 74 61 5b 69  t = &zDestData[i
192e4 4f 66 66 25 6e 44 65 73 74 50 67 73 7a 5d 3b 0a  Off%nDestPgsz];.
192e5 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74  .      /* Copy t
192e6 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  he data from the
192e7 20 73 6f 75 72 63 65 20 70 61 67 65 20 69 6e 74   source page int
192e8 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
192e9 6e 20 70 61 67 65 2e 0a 20 20 20 20 20 20 2a 2a  n page..      **
192ea 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20   Then clear the 
192eb 42 74 72 65 65 20 6c 61 79 65 72 20 4d 65 6d 50  Btree layer MemP
192ec 61 67 65 2e 69 73 49 6e 69 74 20 66 6c 61 67 2e  age.isInit flag.
192ed 20 42 6f 74 68 20 74 68 69 73 20 6d 6f 64 75 6c   Both this modul
192ee 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74  e.      ** and t
192ef 68 65 20 70 61 67 65 72 20 63 6f 64 65 20 75 73  he pager code us
192f0 65 20 74 68 69 73 20 74 72 69 63 6b 20 28 63 6c  e this trick (cl
192f1 65 61 72 69 6e 67 20 74 68 65 20 66 69 72 73 74  earing the first
192f2 20 62 79 74 65 0a 20 20 20 20 20 20 2a 2a 20 6f   byte.      ** o
192f3 66 20 74 68 65 20 70 61 67 65 20 27 65 78 74 72  f the page 'extr
192f4 61 27 20 73 70 61 63 65 20 74 6f 20 69 6e 76 61  a' space to inva
192f5 6c 69 64 61 74 65 20 74 68 65 20 42 74 72 65 65  lidate the Btree
192f6 20 6c 61 79 65 72 73 0a 20 20 20 20 20 20 2a 2a   layers.      **
192f7 20 63 61 63 68 65 64 20 70 61 72 73 65 20 6f 66   cached parse of
192f8 20 74 68 65 20 70 61 67 65 29 2e 20 4d 65 6d 50   the page). MemP
192f9 61 67 65 2e 69 73 49 6e 69 74 20 69 73 20 6d 61  age.isInit is ma
192fa 72 6b 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 22  rked .      ** "
192fb 4d 55 53 54 20 42 45 20 46 49 52 53 54 22 20 66  MUST BE FIRST" f
192fc 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 65 2e  or this purpose.
192fd 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
192fe 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 49 6e  memcpy(zOut, zIn
192ff 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 20  , nCopy);.      
19300 28 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61  ((u8 *)sqlite3Pa
19301 67 65 72 47 65 74 45 78 74 72 61 28 70 44 65 73  gerGetExtra(pDes
19302 74 50 67 29 29 5b 30 5d 20 3d 20 30 3b 0a 20 20  tPg))[0] = 0;.  
19303 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50    }.    sqlite3P
19304 61 67 65 72 55 6e 72 65 66 28 70 44 65 73 74 50  agerUnref(pDestP
19305 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  g);.  }..  retur
19306 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
19307 66 20 70 46 69 6c 65 20 69 73 20 63 75 72 72 65  f pFile is curre
19308 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e  ntly larger than
19309 20 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68   iSize bytes, th
1930a 65 6e 20 74 72 75 6e 63 61 74 65 20 69 74 20 74  en truncate it t
1930b 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 20 69 53 69  o.** exactly iSi
1930c 7a 65 20 62 79 74 65 73 2e 20 49 66 20 70 46 69  ze bytes. If pFi
1930d 6c 65 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 72  le is not larger
1930e 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65   than iSize byte
1930f 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20  s, then.** this 
19310 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
19311 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  -op..**.** Retur
19312 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65  n SQLITE_OK if e
19313 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63  verything is suc
19314 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53  cessful, or an S
19315 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20  QLite error .** 
19316 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72  code if an error
19317 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74   occurs..*/.stat
19318 69 63 20 69 6e 74 20 62 61 63 6b 75 70 54 72 75  ic int backupTru
19319 6e 63 61 74 65 46 69 6c 65 28 73 71 6c 69 74 65  ncateFile(sqlite
1931a 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69  3_file *pFile, i
1931b 36 34 20 69 53 69 7a 65 29 7b 0a 20 20 69 36 34  64 iSize){.  i64
1931c 20 69 43 75 72 72 65 6e 74 3b 0a 20 20 69 6e 74   iCurrent;.  int
1931d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46   rc = sqlite3OsF
1931e 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2c 20 26  ileSize(pFile, &
1931f 69 43 75 72 72 65 6e 74 29 3b 0a 20 20 69 66 28  iCurrent);.  if(
19320 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
19321 26 20 69 43 75 72 72 65 6e 74 3e 69 53 69 7a 65  & iCurrent>iSize
19322 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
19323 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
19324 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20  File, iSize);.  
19325 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
19326 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
19327 20 74 68 69 73 20 62 61 63 6b 75 70 20 6f 62 6a   this backup obj
19328 65 63 74 20 77 69 74 68 20 74 68 65 20 61 73 73  ect with the ass
19329 6f 63 69 61 74 65 64 20 73 6f 75 72 63 65 20 70  ociated source p
1932a 61 67 65 72 20 66 6f 72 0a 2a 2a 20 63 61 6c 6c  ager for.** call
1932b 62 61 63 6b 73 20 77 68 65 6e 20 70 61 67 65 73  backs when pages
1932c 20 61 72 65 20 63 68 61 6e 67 65 64 20 6f 72 20   are changed or 
1932d 74 68 65 20 63 61 63 68 65 20 69 6e 76 61 6c 69  the cache invali
1932e 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  dated..*/.static
1932f 20 76 6f 69 64 20 61 74 74 61 63 68 42 61 63 6b   void attachBack
19330 75 70 4f 62 6a 65 63 74 28 73 71 6c 69 74 65 33  upObject(sqlite3
19331 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73  _backup *p){.  s
19332 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a  qlite3_backup **
19333 70 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  pp;.  assert( sq
19334 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
19335 75 74 65 78 28 70 2d 3e 70 53 72 63 29 20 29 3b  utex(p->pSrc) );
19336 0a 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50  .  pp = sqlite3P
19337 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 71  agerBackupPtr(sq
19338 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
19339 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 70 2d 3e  p->pSrc));.  p->
1933a 70 4e 65 78 74 20 3d 20 2a 70 70 3b 0a 20 20 2a  pNext = *pp;.  *
1933b 70 70 20 3d 20 70 3b 0a 20 20 70 2d 3e 69 73 41  pp = p;.  p->isA
1933c 74 74 61 63 68 65 64 20 3d 20 31 3b 0a 7d 0a 0a  ttached = 1;.}..
1933d 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e 50 61 67 65  /*.** Copy nPage
1933e 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20   pages from the 
1933f 73 6f 75 72 63 65 20 62 2d 74 72 65 65 20 74 6f  source b-tree to
19340 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
19341 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
19342 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  int sqlite3_back
19343 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f  up_step(sqlite3_
19344 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e  backup *p, int n
19345 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Page){.  int rc;
19346 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
19347 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44  x_enter(p->pSrcD
19348 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
19349 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
1934a 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70  ->pSrc);.  if( p
1934b 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20  ->pDestDb ){.   
1934c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1934d 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d  nter(p->pDestDb-
1934e 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20  >mutex);.  }..  
1934f 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 66  rc = p->rc;.  if
19350 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28  ( !isFatalError(
19351 72 63 29 20 29 7b 0a 20 20 20 20 50 61 67 65 72  rc) ){.    Pager
19352 20 2a 20 63 6f 6e 73 74 20 70 53 72 63 50 61 67   * const pSrcPag
19353 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
19354 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 3b  ePager(p->pSrc);
19355 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70       /* Source p
19356 61 67 65 72 20 2a 2f 0a 20 20 20 20 50 61 67 65  ager */.    Page
19357 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 50  r * const pDestP
19358 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
19359 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73  reePager(p->pDes
1935a 74 29 3b 20 20 20 2f 2a 20 44 65 73 74 20 70 61  t);   /* Dest pa
1935b 67 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ger */.    int i
1935c 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
1935d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1935e 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
1935f 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53  le */.    int nS
19360 72 63 50 61 67 65 20 3d 20 2d 31 3b 20 20 20 20  rcPage = -1;    
19361 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19362 53 69 7a 65 20 6f 66 20 73 6f 75 72 63 65 20 64  Size of source d
19363 62 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20  b in pages */.  
19364 20 20 69 6e 74 20 62 43 6c 6f 73 65 54 72 61 6e    int bCloseTran
19365 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  s = 0;          
19366 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
19367 73 72 63 20 64 62 20 72 65 71 75 69 72 65 73 20  src db requires 
19368 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20  unlocking */..  
19369 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72    /* If the sour
1936a 63 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72  ce pager is curr
1936b 65 6e 74 6c 79 20 69 6e 20 61 20 77 72 69 74 65  ently in a write
1936c 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65  -transaction, re
1936d 74 75 72 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49  turn.    ** SQLI
1936e 54 45 5f 42 55 53 59 20 69 6d 6d 65 64 69 61 74  TE_BUSY immediat
1936f 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ely..    */.    
19370 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 26  if( p->pDestDb &
19371 26 20 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e  & p->pSrc->pBt->
19372 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
19373 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
19374 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
19375 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  BUSY;.    }else{
19376 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
19377 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20  TE_OK;.    }..  
19378 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 64 65    /* Lock the de
19379 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
1937a 73 65 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74  se, if it is not
1937b 20 6c 6f 63 6b 65 64 20 61 6c 72 65 61 64 79 2e   locked already.
1937c 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49   */.    if( SQLI
1937d 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d 3e  TE_OK==rc && p->
1937e 62 44 65 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20  bDestLocked==0. 
1937f 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b      && SQLITE_OK
19380 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42  ==(rc = sqlite3B
19381 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70  treeBeginTrans(p
19382 2d 3e 70 44 65 73 74 2c 20 32 29 29 20 0a 20 20  ->pDest, 2)) .  
19383 20 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 62 44    ){.      p->bD
19384 65 73 74 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20  estLocked = 1;. 
19385 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
19386 65 47 65 74 4d 65 74 61 28 70 2d 3e 70 44 65 73  eGetMeta(p->pDes
19387 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f  t, BTREE_SCHEMA_
19388 56 45 52 53 49 4f 4e 2c 20 26 70 2d 3e 69 44 65  VERSION, &p->iDe
19389 73 74 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d  stSchema);.    }
1938a 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  ..    /* If ther
1938b 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61  e is no open rea
1938c 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  d-transaction on
1938d 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
1938e 62 61 73 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a  base, open.    *
1938f 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20  * one now. If a 
19390 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
19391 70 65 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e  pened here, then
19392 20 69 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73   it will be clos
19393 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65  ed.    ** before
19394 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65   this function e
19395 78 69 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  xits..    */.   
19396 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
19397 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33  OK && 0==sqlite3
19398 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61  BtreeIsInReadTra
19399 6e 73 28 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20  ns(p->pSrc) ){. 
1939a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1939b 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73  3BtreeBeginTrans
1939c 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20  (p->pSrc, 0);.  
1939d 20 20 20 20 62 43 6c 6f 73 65 54 72 61 6e 73 20      bCloseTrans 
1939e 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  = 1;.    }.  .  
1939f 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68    /* Now that th
193a0 65 72 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f  ere is a read-lo
193a1 63 6b 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65  ck on the source
193a2 20 64 61 74 61 62 61 73 65 2c 20 71 75 65 72 79   database, query
193a3 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 6f 75 72   the.    ** sour
193a4 63 65 20 70 61 67 65 72 20 66 6f 72 20 74 68 65  ce pager for the
193a5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
193a6 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
193a7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
193a8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
193a9 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
193aa 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
193ab 6e 74 28 70 53 72 63 50 61 67 65 72 2c 20 26 6e  nt(pSrcPager, &n
193ac 53 72 63 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  SrcPage);.    }.
193ad 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 28 6e      for(ii=0; (n
193ae 50 61 67 65 3c 30 20 7c 7c 20 69 69 3c 6e 50 61  Page<0 || ii<nPa
193af 67 65 29 20 26 26 20 70 2d 3e 69 4e 65 78 74 3c  ge) && p->iNext<
193b0 3d 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20  =(Pgno)nSrcPage 
193b1 26 26 20 21 72 63 3b 20 69 69 2b 2b 29 7b 0a 20  && !rc; ii++){. 
193b2 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20       const Pgno 
193b3 69 53 72 63 50 67 20 3d 20 70 2d 3e 69 4e 65 78  iSrcPg = p->iNex
193b4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
193b5 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67     /* Source pag
193b6 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
193b7 20 20 69 66 28 20 69 53 72 63 50 67 21 3d 50 45    if( iSrcPg!=PE
193b8 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
193b9 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 7b  p->pSrc->pBt) ){
193ba 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20  .        DbPage 
193bb 2a 70 53 72 63 50 67 3b 20 20 20 20 20 20 20 20  *pSrcPg;        
193bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193bd 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70       /* Source p
193be 61 67 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  age object */.  
193bf 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
193c0 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50  e3PagerGet(pSrcP
193c1 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70  ager, iSrcPg, &p
193c2 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20  SrcPg);.        
193c3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
193c4 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
193c5 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67  c = backupOnePag
193c6 65 28 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c  e(p, iSrcPg, sql
193c7 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
193c8 28 70 53 72 63 50 67 29 29 3b 0a 20 20 20 20 20  (pSrcPg));.     
193c9 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
193ca 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a  rUnref(pSrcPg);.
193cb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
193cc 7d 0a 20 20 20 20 20 20 70 2d 3e 69 4e 65 78 74  }.      p->iNext
193cd 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ++;.    }.    if
193ce 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
193cf 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 50 61 67  ){.      p->nPag
193d0 65 63 6f 75 6e 74 20 3d 20 6e 53 72 63 50 61 67  ecount = nSrcPag
193d1 65 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 6d  e;.      p->nRem
193d2 61 69 6e 69 6e 67 20 3d 20 6e 53 72 63 50 61 67  aining = nSrcPag
193d3 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a 20 20  e+1-p->iNext;.  
193d4 20 20 20 20 69 66 28 20 70 2d 3e 69 4e 65 78 74      if( p->iNext
193d5 3e 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20  >(Pgno)nSrcPage 
193d6 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
193d7 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
193d8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 2d     }else if( !p-
193d9 3e 69 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20  >isAttached ){. 
193da 20 20 20 20 20 20 20 61 74 74 61 63 68 42 61 63         attachBac
193db 6b 75 70 4f 62 6a 65 63 74 28 70 29 3b 0a 20 20  kupObject(p);.  
193dc 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
193dd 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
193de 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20   schema version 
193df 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73  field in the des
193e0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
193e1 65 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69  e. This.    ** i
193e2 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  s to make sure t
193e3 68 61 74 20 74 68 65 20 73 63 68 65 6d 61 2d 76  hat the schema-v
193e4 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20 64 6f  ersion really do
193e5 65 73 20 63 68 61 6e 67 65 20 69 6e 0a 20 20 20  es change in.   
193e6 20 2a 2a 20 74 68 65 20 63 61 73 65 20 77 68 65   ** the case whe
193e7 72 65 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e  re the source an
193e8 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  d destination da
193e9 74 61 62 61 73 65 73 20 68 61 76 65 20 74 68 65  tabases have the
193ea 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 73 63 68  .    ** same sch
193eb 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20  ema version..   
193ec 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
193ed 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 20  SQLITE_DONE .   
193ee 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74    && (rc = sqlit
193ef 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74  e3BtreeUpdateMet
193f0 61 28 70 2d 3e 70 44 65 73 74 2c 31 2c 70 2d 3e  a(p->pDest,1,p->
193f1 69 44 65 73 74 53 63 68 65 6d 61 2b 31 29 29 3d  iDestSchema+1))=
193f2 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 29  =SQLITE_OK.    )
193f3 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e  {.      const in
193f4 74 20 6e 53 72 63 50 61 67 65 73 69 7a 65 20 3d  t nSrcPagesize =
193f5 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
193f6 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63  PageSize(p->pSrc
193f7 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69  );.      const i
193f8 6e 74 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65  nt nDestPagesize
193f9 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
193fa 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44  etPageSize(p->pD
193fb 65 73 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  est);.      int 
193fc 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20  nDestTruncate;. 
193fd 20 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70   .      if( p->p
193fe 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 20 20  DestDb ){.      
193ff 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
19400 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 70 2d 3e  ternalSchema(p->
19401 70 44 65 73 74 44 62 2c 20 30 29 3b 0a 20 20 20  pDestDb, 0);.   
19402 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53     }..      /* S
19403 65 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65  et nDestTruncate
19404 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 6e 75   to the final nu
19405 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
19406 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
19407 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61  .      ** databa
19408 73 65 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63 61  se. The complica
19409 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61  tion here is tha
1940a 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  t the destinatio
1940b 6e 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20  n page.      ** 
1940c 73 69 7a 65 20 6d 61 79 20 62 65 20 64 69 66 66  size may be diff
1940d 65 72 65 6e 74 20 74 6f 20 74 68 65 20 73 6f 75  erent to the sou
1940e 72 63 65 20 70 61 67 65 20 73 69 7a 65 2e 20 0a  rce page size. .
1940f 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
19410 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20  * If the source 
19411 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 6d 61  page size is sma
19412 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64 65  ller than the de
19413 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73  stination page s
19414 69 7a 65 2c 20 0a 20 20 20 20 20 20 2a 2a 20 72  ize, .      ** r
19415 6f 75 6e 64 20 75 70 2e 20 49 6e 20 74 68 69 73  ound up. In this
19416 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74   case the call t
19417 6f 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  o sqlite3OsTrunc
19418 61 74 65 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c  ate() below will
19419 0a 20 20 20 20 20 20 2a 2a 20 66 69 78 20 74 68  .      ** fix th
1941a 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69  e size of the fi
1941b 6c 65 2e 20 48 6f 77 65 76 65 72 20 69 74 20 69  le. However it i
1941c 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63  s important to c
1941d 61 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c  all.      ** sql
1941e 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
1941f 65 49 6d 61 67 65 28 29 20 68 65 72 65 20 73 6f  eImage() here so
19420 20 74 68 61 74 20 61 6e 79 20 70 61 67 65 73 20   that any pages 
19421 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  in the .      **
19422 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c   destination fil
19423 65 20 74 68 61 74 20 6c 69 65 20 62 65 79 6f 6e  e that lie beyon
19424 64 20 74 68 65 20 6e 44 65 73 74 54 72 75 6e 63  d the nDestTrunc
19425 61 74 65 20 70 61 67 65 20 6d 61 72 6b 20 61 72  ate page mark ar
19426 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e  e.      ** journ
19427 61 6c 6c 65 64 20 62 79 20 50 61 67 65 72 43 6f  alled by PagerCo
19428 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 62  mmitPhaseOne() b
19429 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 64  efore they are d
1942a 65 73 74 72 6f 79 65 64 0a 20 20 20 20 20 20 2a  estroyed.      *
1942b 2a 20 62 79 20 74 68 65 20 66 69 6c 65 20 74 72  * by the file tr
1942c 75 6e 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20  uncation..      
1942d 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72  */.      if( nSr
1942e 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 50  cPagesize<nDestP
1942f 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 20  agesize ){.     
19430 20 20 20 69 6e 74 20 72 61 74 69 6f 20 3d 20 6e     int ratio = n
19431 44 65 73 74 50 61 67 65 73 69 7a 65 2f 6e 53 72  DestPagesize/nSr
19432 63 50 61 67 65 73 69 7a 65 3b 0a 20 20 20 20 20  cPagesize;.     
19433 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65     nDestTruncate
19434 20 3d 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74   = (nSrcPage+rat
19435 69 6f 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 20  io-1)/ratio;.   
19436 20 20 20 20 20 69 66 28 20 6e 44 65 73 74 54 72       if( nDestTr
19437 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 4e  uncate==(int)PEN
19438 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
19439 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 7b  ->pDest->pBt) ){
1943a 0a 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 74  .          nDest
1943b 54 72 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 20 20  Truncate--;.    
1943c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
1943d 65 7b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74  e{.        nDest
1943e 54 72 75 6e 63 61 74 65 20 3d 20 6e 53 72 63 50  Truncate = nSrcP
1943f 61 67 65 20 2a 20 28 6e 53 72 63 50 61 67 65 73  age * (nSrcPages
19440 69 7a 65 2f 6e 44 65 73 74 50 61 67 65 73 69 7a  ize/nDestPagesiz
19441 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
19442 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72    sqlite3PagerTr
19443 75 6e 63 61 74 65 49 6d 61 67 65 28 70 44 65 73  uncateImage(pDes
19444 74 50 61 67 65 72 2c 20 6e 44 65 73 74 54 72 75  tPager, nDestTru
19445 6e 63 61 74 65 29 3b 0a 0a 20 20 20 20 20 20 69  ncate);..      i
19446 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c  f( nSrcPagesize<
19447 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b  nDestPagesize ){
19448 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74  .        /* If t
19449 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 2d 73  he source page-s
1944a 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  ize is smaller t
1944b 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74  han the destinat
1944c 69 6f 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20  ion page-size,. 
1944d 20 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 65 78         ** two ex
1944e 74 72 61 20 74 68 69 6e 67 73 20 6d 61 79 20 6e  tra things may n
1944f 65 65 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a 20  eed to happen:. 
19450 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
19451 20 20 2a 2a 20 20 20 2a 20 54 68 65 20 64 65 73    **   * The des
19452 74 69 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65 65  tination may nee
19453 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 65  d to be truncate
19454 64 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  d, and.        *
19455 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a  *.        **   *
19456 20 44 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20   Data stored on 
19457 74 68 65 20 70 61 67 65 73 20 69 6d 6d 65 64 69  the pages immedi
19458 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20  ately following 
19459 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
1945a 20 20 20 20 70 65 6e 64 69 6e 67 2d 62 79 74 65      pending-byte
1945b 20 70 61 67 65 20 69 6e 20 74 68 65 20 73 6f 75   page in the sou
1945c 72 63 65 20 64 61 74 61 62 61 73 65 20 6d 61 79  rce database may
1945d 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 20 20   need to be.    
1945e 20 20 20 20 2a 2a 20 20 20 20 20 63 6f 70 69 65      **     copie
1945f 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69  d into the desti
19460 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e  nation database.
19461 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
19462 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 53      const i64 iS
19463 69 7a 65 20 3d 20 28 69 36 34 29 6e 53 72 63 50  ize = (i64)nSrcP
19464 61 67 65 73 69 7a 65 20 2a 20 28 69 36 34 29 6e  agesize * (i64)n
19465 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20 20 20  SrcPage;.       
19466 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 20   sqlite3_file * 
19467 63 6f 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 71  const pFile = sq
19468 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70  lite3PagerFile(p
19469 44 65 73 74 50 61 67 65 72 29 3b 0a 0a 20 20 20  DestPager);..   
1946a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69       assert( pFi
1946b 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  le );.        as
1946c 73 65 72 74 28 20 28 69 36 34 29 6e 44 65 73 74  sert( (i64)nDest
1946d 54 72 75 6e 63 61 74 65 2a 28 69 36 34 29 6e 44  Truncate*(i64)nD
1946e 65 73 74 50 61 67 65 73 69 7a 65 20 3e 3d 20 69  estPagesize >= i
1946f 53 69 7a 65 20 7c 7c 20 28 0a 20 20 20 20 20 20  Size || (.      
19470 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75          nDestTru
19471 6e 63 61 74 65 3d 3d 28 69 6e 74 29 28 50 45 4e  ncate==(int)(PEN
19472 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
19473 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 2d 31 29  ->pDest->pBt)-1)
19474 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 69  .           && i
19475 53 69 7a 65 3e 3d 50 45 4e 44 49 4e 47 5f 42 59  Size>=PENDING_BY
19476 54 45 20 26 26 20 69 53 69 7a 65 3c 3d 50 45 4e  TE && iSize<=PEN
19477 44 49 4e 47 5f 42 59 54 45 2b 6e 44 65 73 74 50  DING_BYTE+nDestP
19478 61 67 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20  agesize.        
19479 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ));.        if( 
1947a 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
1947b 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
1947c 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73  mitPhaseOne(pDes
1947d 74 50 61 67 65 72 2c 20 30 2c 20 31 29 29 0a 20  tPager, 0, 1)). 
1947e 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54          && SQLIT
1947f 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 62 61 63 6b  E_OK==(rc = back
19480 75 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 70  upTruncateFile(p
19481 46 69 6c 65 2c 20 69 53 69 7a 65 29 29 0a 20 20  File, iSize)).  
19482 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45         && SQLITE
19483 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
19484 65 33 50 61 67 65 72 53 79 6e 63 28 70 44 65 73  e3PagerSync(pDes
19485 74 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20  tPager)).       
19486 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36   ){.          i6
19487 34 20 69 4f 66 66 3b 0a 20 20 20 20 20 20 20 20  4 iOff;.        
19488 20 20 69 36 34 20 69 45 6e 64 20 3d 20 4d 49 4e    i64 iEnd = MIN
19489 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b 20  (PENDING_BYTE + 
1948a 6e 44 65 73 74 50 61 67 65 73 69 7a 65 2c 20 69  nDestPagesize, i
1948b 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Size);.         
1948c 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 20 20   for(.          
1948d 20 20 69 4f 66 66 3d 50 45 4e 44 49 4e 47 5f 42    iOff=PENDING_B
1948e 59 54 45 2b 6e 53 72 63 50 61 67 65 73 69 7a 65  YTE+nSrcPagesize
1948f 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ; .            r
19490 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
19491 69 4f 66 66 3c 69 45 6e 64 3b 20 0a 20 20 20 20  iOff<iEnd; .    
19492 20 20 20 20 20 20 20 20 69 4f 66 66 2b 3d 6e 53          iOff+=nS
19493 72 63 50 61 67 65 73 69 7a 65 0a 20 20 20 20 20  rcPagesize.     
19494 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
19495 20 20 20 20 50 67 48 64 72 20 2a 70 53 72 63 50      PgHdr *pSrcP
19496 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  g = 0;.         
19497 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53     const Pgno iS
19498 72 63 50 67 20 3d 20 28 50 67 6e 6f 29 28 28 69  rcPg = (Pgno)((i
19499 4f 66 66 2f 6e 53 72 63 50 61 67 65 73 69 7a 65  Off/nSrcPagesize
1949a 29 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )+1);.          
1949b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1949c 67 65 72 47 65 74 28 70 53 72 63 50 61 67 65 72  gerGet(pSrcPager
1949d 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72 63 50  , iSrcPg, &pSrcP
1949e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  g);.            
1949f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
194a0 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
194a1 20 20 20 75 38 20 2a 7a 44 61 74 61 20 3d 20 73     u8 *zData = s
194a2 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
194a3 74 61 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20  ta(pSrcPg);.    
194a4 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
194a5 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 46  qlite3OsWrite(pF
194a6 69 6c 65 2c 20 7a 44 61 74 61 2c 20 6e 53 72 63  ile, zData, nSrc
194a7 50 61 67 65 73 69 7a 65 2c 20 69 4f 66 66 29 3b  Pagesize, iOff);
194a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
194a9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
194aa 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 53 72  e3PagerUnref(pSr
194ab 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20  cPg);.          
194ac 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
194ad 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
194ae 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
194af 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  erCommitPhaseOne
194b0 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20  (pDestPager, 0, 
194b1 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20  0);.      }.  . 
194b2 20 20 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 63       /* Finish c
194b3 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 20 74 72  ommitting the tr
194b4 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 74 68 65  ansaction to the
194b5 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
194b6 61 62 61 73 65 2e 20 2a 2f 0a 20 20 20 20 20 20  abase. */.      
194b7 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72  if( SQLITE_OK==r
194b8 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c 49  c.       && SQLI
194b9 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
194ba 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
194bb 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74  haseTwo(p->pDest
194bc 29 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  )).      ){.    
194bd 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
194be 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  DONE;.      }.  
194bf 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66    }.  .    /* If
194c0 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 69 73 20   bCloseTrans is 
194c1 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 73 20  true, then this 
194c2 66 75 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20  function opened 
194c3 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  a read transacti
194c4 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65  on.    ** on the
194c5 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
194c6 2e 20 43 6c 6f 73 65 20 74 68 65 20 72 65 61 64  . Close the read
194c7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 65 72   transaction her
194c8 65 2e 20 54 68 65 72 65 20 69 73 0a 20 20 20 20  e. There is.    
194c9 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63 68  ** no need to ch
194ca 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76  eck the return v
194cb 61 6c 75 65 73 20 6f 66 20 74 68 65 20 62 74 72  alues of the btr
194cc 65 65 20 6d 65 74 68 6f 64 73 20 68 65 72 65 2c  ee methods here,
194cd 20 61 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d 6d   as.    ** "comm
194ce 69 74 74 69 6e 67 22 20 61 20 72 65 61 64 2d 6f  itting" a read-o
194cf 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  nly transaction 
194d0 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20  cannot fail..   
194d1 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 43 6c 6f   */.    if( bClo
194d2 73 65 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 20  seTrans ){.     
194d3 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 72   TESTONLY( int r
194d4 63 32 20 29 3b 0a 20 20 20 20 20 20 54 45 53 54  c2 );.      TEST
194d5 4f 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20 73  ONLY( rc2  = ) s
194d6 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
194d7 74 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 72  tPhaseOne(p->pSr
194d8 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 54 45 53  c, 0);.      TES
194d9 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 20  TONLY( rc2 |= ) 
194da 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
194db 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 53  itPhaseTwo(p->pS
194dc 72 63 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  rc);.      asser
194dd 74 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f  t( rc2==SQLITE_O
194de 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  K );.    }.  .  
194df 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
194e0 7d 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74  }.  if( p->pDest
194e1 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Db ){.    sqlite
194e2 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
194e3 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29  >pDestDb->mutex)
194e4 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42  ;.  }.  sqlite3B
194e5 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72  treeLeave(p->pSr
194e6 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
194e7 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53 72  tex_leave(p->pSr
194e8 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  cDb->mutex);.  r
194e9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
194ea 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 72  ** Release all r
194eb 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61  esources associa
194ec 74 65 64 20 77 69 74 68 20 61 6e 20 73 71 6c 69  ted with an sqli
194ed 74 65 33 5f 62 61 63 6b 75 70 2a 20 68 61 6e 64  te3_backup* hand
194ee 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
194ef 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61  I int sqlite3_ba
194f0 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c 69  ckup_finish(sqli
194f1 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a  te3_backup *p){.
194f2 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
194f3 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20   **pp;          
194f4 20 20 20 20 20 20 20 2f 2a 20 50 74 72 20 74 6f         /* Ptr to
194f5 20 68 65 61 64 20 6f 66 20 70 61 67 65 72 73 20   head of pagers 
194f6 62 61 63 6b 75 70 20 6c 69 73 74 20 2a 2f 0a 20  backup list */. 
194f7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
194f8 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20  mutex;          
194f9 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74        /* Mutex t
194fa 6f 20 70 72 6f 74 65 63 74 20 73 6f 75 72 63 65  o protect source
194fb 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
194fc 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
194fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
194fe 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
194ff 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20  return */..  /* 
19500 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65  Enter the mutexe
19501 73 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20  s */.  if( p==0 
19502 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
19503 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  OK;.  sqlite3_mu
19504 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72  tex_enter(p->pSr
19505 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73  cDb->mutex);.  s
19506 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
19507 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 6d 75 74  (p->pSrc);.  mut
19508 65 78 20 3d 20 70 2d 3e 70 53 72 63 44 62 2d 3e  ex = p->pSrcDb->
19509 6d 75 74 65 78 3b 0a 20 20 69 66 28 20 70 2d 3e  mutex;.  if( p->
1950a 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73  pDestDb ){.    s
1950b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1950c 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d  er(p->pDestDb->m
1950d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  utex);.  }..  /*
1950e 20 44 65 74 61 63 68 20 74 68 69 73 20 62 61 63   Detach this bac
1950f 6b 75 70 20 66 72 6f 6d 20 74 68 65 20 73 6f 75  kup from the sou
19510 72 63 65 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20  rce pager. */.  
19511 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29  if( p->pDestDb )
19512 7b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e  {.    p->pSrc->n
19513 42 61 63 6b 75 70 2d 2d 3b 0a 20 20 7d 0a 20 20  Backup--;.  }.  
19514 69 66 28 20 70 2d 3e 69 73 41 74 74 61 63 68 65  if( p->isAttache
19515 64 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 73 71  d ){.    pp = sq
19516 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70  lite3PagerBackup
19517 50 74 72 28 73 71 6c 69 74 65 33 42 74 72 65 65  Ptr(sqlite3Btree
19518 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b  Pager(p->pSrc));
19519 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 70 70 21  .    while( *pp!
1951a 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 70 20 3d  =p ){.      pp =
1951b 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a   &(*pp)->pNext;.
1951c 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20      }.    *pp = 
1951d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20  p->pNext;.  }.. 
1951e 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63   /* If a transac
1951f 74 69 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f 70  tion is still op
19520 65 6e 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2c  en on the Btree,
19521 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 2a   roll it back. *
19522 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  /.  sqlite3Btree
19523 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e 70 44 65 73  Rollback(p->pDes
19524 74 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68  t);..  /* Set th
19525 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20  e error code of 
19526 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
19527 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  database handle.
19528 20 2a 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e 72   */.  rc = (p->r
19529 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20  c==SQLITE_DONE) 
1952a 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70 2d  ? SQLITE_OK : p-
1952b 3e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  >rc;.  sqlite3Er
1952c 72 6f 72 28 70 2d 3e 70 44 65 73 74 44 62 2c 20  ror(p->pDestDb, 
1952d 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 78  rc, 0);..  /* Ex
1952e 69 74 20 74 68 65 20 6d 75 74 65 78 65 73 20 61  it the mutexes a
1952f 6e 64 20 66 72 65 65 20 74 68 65 20 62 61 63 6b  nd free the back
19530 75 70 20 63 6f 6e 74 65 78 74 20 73 74 72 75 63  up context struc
19531 74 75 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ture. */.  if( p
19532 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20  ->pDestDb ){.   
19533 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
19534 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d  eave(p->pDestDb-
19535 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73  >mutex);.  }.  s
19536 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
19537 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28  (p->pSrc);.  if(
19538 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20   p->pDestDb ){. 
19539 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1953a 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  p);.  }.  sqlite
1953b 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
1953c 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
1953d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
1953e 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
1953f 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f 20   pages still to 
19540 62 65 20 62 61 63 6b 65 64 20 75 70 20 61 73 20  be backed up as 
19541 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
19542 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71  nt.** call to sq
19543 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
19544 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  p()..*/.SQLITE_A
19545 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
19546 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28  ackup_remaining(
19547 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
19548 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p){.  return p->
19549 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a 7d 0a 0a 2f  nRemaining;.}../
1954a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1954b 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
1954c 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f 75  pages in the sou
1954d 72 63 65 20 64 61 74 61 62 61 73 65 20 61 73 20  rce database as 
1954e 6f 66 20 74 68 65 20 6d 6f 73 74 20 0a 2a 2a 20  of the most .** 
1954f 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73  recent call to s
19550 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
19551 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ep()..*/.SQLITE_
19552 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
19553 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
19554 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
19555 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
19556 3e 6e 50 61 67 65 63 6f 75 6e 74 3b 0a 7d 0a 0a  >nPagecount;.}..
19557 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
19558 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66  ion is called af
19559 74 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ter the contents
1955a 20 6f 66 20 70 61 67 65 20 69 50 61 67 65 20 6f   of page iPage o
1955b 66 20 74 68 65 0a 2a 2a 20 73 6f 75 72 63 65 20  f the.** source 
1955c 64 61 74 61 62 61 73 65 20 68 61 76 65 20 62 65  database have be
1955d 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 49 66 20  en modified. If 
1955e 70 61 67 65 20 69 50 61 67 65 20 68 61 73 20 61  page iPage has a
1955f 6c 72 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a 20  lready been .** 
19560 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20  copied into the 
19561 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
19562 62 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64  base, then the d
19563 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f 20 74  ata written to t
19564 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f  he.** destinatio
19565 6e 20 69 73 20 6e 6f 77 20 69 6e 76 61 6c 69 64  n is now invalid
19566 61 74 65 64 2e 20 54 68 65 20 64 65 73 74 69 6e  ated. The destin
19567 61 74 69 6f 6e 20 63 6f 70 79 20 6f 66 20 69 50  ation copy of iP
19568 61 67 65 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20  age needs.** to 
19569 62 65 20 75 70 64 61 74 65 64 20 77 69 74 68 20  be updated with 
1956a 74 68 65 20 6e 65 77 20 64 61 74 61 20 62 65 66  the new data bef
1956b 6f 72 65 20 74 68 65 20 62 61 63 6b 75 70 20 6f  ore the backup o
1956c 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 63  peration is.** c
1956d 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 49  omplete..**.** I
1956e 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
1956f 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f  t the mutex asso
19570 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
19571 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a  BtShared object.
19572 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
19573 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64   to the source d
19574 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 20  atabase is held 
19575 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
19576 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e  on is.** called.
19577 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19578 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
19579 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69  ackupUpdate(sqli
1957a 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63  te3_backup *pBac
1957b 6b 75 70 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c  kup, Pgno iPage,
1957c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61   const u8 *aData
1957d 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  ){.  sqlite3_bac
1957e 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20  kup *p;         
1957f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
19580 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
19581 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75  /.  for(p=pBacku
19582 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  p; p; p=p->pNext
19583 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ){.    assert( s
19584 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
19585 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e  d(p->pSrc->pBt->
19586 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 69 66  mutex) );.    if
19587 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28  ( !isFatalError(
19588 70 2d 3e 72 63 29 20 26 26 20 69 50 61 67 65 3c  p->rc) && iPage<
19589 70 2d 3e 69 4e 65 78 74 20 29 7b 0a 20 20 20 20  p->iNext ){.    
1958a 20 20 2f 2a 20 54 68 65 20 62 61 63 6b 75 70 20    /* The backup 
1958b 70 72 6f 63 65 73 73 20 70 20 68 61 73 20 61 6c  process p has al
1958c 72 65 61 64 79 20 63 6f 70 69 65 64 20 70 61 67  ready copied pag
1958d 65 20 69 50 61 67 65 2e 20 42 75 74 20 6e 6f 77  e iPage. But now
1958e 20 69 74 0a 20 20 20 20 20 20 2a 2a 20 68 61 73   it.      ** has
1958f 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62   been modified b
19590 79 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  y a transaction 
19591 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 70 61  on the source pa
19592 67 65 72 2e 20 43 6f 70 79 0a 20 20 20 20 20 20  ger. Copy.      
19593 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20  ** the new data 
19594 69 6e 74 6f 20 74 68 65 20 62 61 63 6b 75 70 2e  into the backup.
19595 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
19596 69 6e 74 20 72 63 20 3d 20 62 61 63 6b 75 70 4f  int rc = backupO
19597 6e 65 50 61 67 65 28 70 2c 20 69 50 61 67 65 2c  nePage(p, iPage,
19598 20 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 61   aData);.      a
19599 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
1959a 45 5f 42 55 53 59 20 26 26 20 72 63 21 3d 53 51  E_BUSY && rc!=SQ
1959b 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20  LITE_LOCKED );. 
1959c 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1959d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1959e 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
1959f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
195a0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72 74  }../*.** Restart
195a1 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63   the backup proc
195a2 65 73 73 2e 20 54 68 69 73 20 69 73 20 63 61 6c  ess. This is cal
195a3 6c 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67  led when the pag
195a4 65 72 20 6c 61 79 65 72 0a 2a 2a 20 64 65 74 65  er layer.** dete
195a5 63 74 73 20 74 68 61 74 20 74 68 65 20 64 61 74  cts that the dat
195a6 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d  abase has been m
195a7 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 65 78  odified by an ex
195a8 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 0a  ternal database.
195a9 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49  ** connection. I
195aa 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72  n this case ther
195ab 65 20 69 73 20 6e 6f 20 77 61 79 20 6f 66 20 6b  e is no way of k
195ac 6e 6f 77 69 6e 67 20 77 68 69 63 68 20 6f 66 20  nowing which of 
195ad 74 68 65 0a 2a 2a 20 70 61 67 65 73 20 74 68 61  the.** pages tha
195ae 74 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70 69  t have been copi
195af 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74  ed into the dest
195b0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
195b1 20 61 72 65 20 73 74 69 6c 6c 20 0a 2a 2a 20 76   are still .** v
195b2 61 6c 69 64 20 61 6e 64 20 77 68 69 63 68 20 61  alid and which a
195b3 72 65 20 6e 6f 74 2c 20 73 6f 20 74 68 65 20 65  re not, so the e
195b4 6e 74 69 72 65 20 70 72 6f 63 65 73 73 20 6e 65  ntire process ne
195b5 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72  eds to be restar
195b6 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  ted..**.** It is
195b7 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
195b8 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74  e mutex associat
195b9 65 64 20 77 69 74 68 20 74 68 65 20 42 74 53 68  ed with the BtSh
195ba 61 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 63  ared object.** c
195bb 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
195bc 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
195bd 61 73 65 20 69 73 20 68 65 6c 64 20 77 68 65 6e  ase is held when
195be 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
195bf 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  s.** called..*/.
195c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
195c1 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75  oid sqlite3Backu
195c2 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33  pRestart(sqlite3
195c3 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70  _backup *pBackup
195c4 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  ){.  sqlite3_bac
195c5 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20  kup *p;         
195c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
195c7 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
195c8 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75  /.  for(p=pBacku
195c9 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  p; p; p=p->pNext
195ca 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ){.    assert( s
195cb 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
195cc 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e  d(p->pSrc->pBt->
195cd 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 2d  mutex) );.    p-
195ce 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 7d 0a  >iNext = 1;.  }.
195cf 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
195d0 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a  E_OMIT_VACUUM./*
195d1 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 6f 6d  .** Copy the com
195d2 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66  plete content of
195d3 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70 42   pBtFrom into pB
195d4 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61 63 74  tTo.  A transact
195d5 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61  ion.** must be a
195d6 63 74 69 76 65 20 66 6f 72 20 62 6f 74 68 20 66  ctive for both f
195d7 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  iles..**.** The 
195d8 73 69 7a 65 20 6f 66 20 66 69 6c 65 20 70 54 6f  size of file pTo
195d9 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64 20   may be reduced 
195da 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  by this operatio
195db 6e 2e 20 49 66 20 61 6e 79 74 68 69 6e 67 20 0a  n. If anything .
195dc 2a 2a 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74  ** goes wrong, t
195dd 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
195de 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 20  n pTo is rolled 
195df 62 61 63 6b 2e 20 49 66 20 73 75 63 63 65 73 73  back. If success
195e0 66 75 6c 2c 20 74 68 65 20 0a 2a 2a 20 74 72 61  ful, the .** tra
195e1 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d  nsaction is comm
195e2 69 74 74 65 64 20 62 65 66 6f 72 65 20 72 65 74  itted before ret
195e3 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  urning..*/.SQLIT
195e4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
195e5 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69  lite3BtreeCopyFi
195e6 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42  le(Btree *pTo, B
195e7 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20  tree *pFrom){.  
195e8 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
195e9 33 5f 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 71  3_backup b;.  sq
195ea 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
195eb 70 54 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  pTo);.  sqlite3B
195ec 74 72 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29  treeEnter(pFrom)
195ed 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 61  ;..  /* Set up a
195ee 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  n sqlite3_backup
195ef 20 6f 62 6a 65 63 74 2e 20 73 71 6c 69 74 65 33   object. sqlite3
195f0 5f 62 61 63 6b 75 70 2e 70 44 65 73 74 44 62 20  _backup.pDestDb 
195f1 6d 75 73 74 20 62 65 20 73 65 74 0a 20 20 2a 2a  must be set.  **
195f2 20 74 6f 20 30 2e 20 54 68 69 73 20 69 73 20 75   to 0. This is u
195f3 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65  sed by the imple
195f4 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71  mentations of sq
195f5 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
195f6 70 28 29 0a 20 20 2a 2a 20 61 6e 64 20 73 71 6c  p().  ** and sql
195f7 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
195f8 73 68 28 29 20 74 6f 20 64 65 74 65 63 74 20 74  sh() to detect t
195f9 68 61 74 20 74 68 65 79 20 61 72 65 20 62 65 69  hat they are bei
195fa 6e 67 20 63 61 6c 6c 65 64 0a 20 20 2a 2a 20 66  ng called.  ** f
195fb 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f  rom this functio
195fc 6e 2c 20 6e 6f 74 20 64 69 72 65 63 74 6c 79 20  n, not directly 
195fd 62 79 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a  by the user..  *
195fe 2f 0a 20 20 6d 65 6d 73 65 74 28 26 62 2c 20 30  /.  memset(&b, 0
195ff 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 0a 20 20  , sizeof(b));.  
19600 62 2e 70 53 72 63 44 62 20 3d 20 70 46 72 6f 6d  b.pSrcDb = pFrom
19601 2d 3e 64 62 3b 0a 20 20 62 2e 70 53 72 63 20 3d  ->db;.  b.pSrc =
19602 20 70 46 72 6f 6d 3b 0a 20 20 62 2e 70 44 65 73   pFrom;.  b.pDes
19603 74 20 3d 20 70 54 6f 3b 0a 20 20 62 2e 69 4e 65  t = pTo;.  b.iNe
19604 78 74 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 30 78  xt = 1;..  /* 0x
19605 37 46 46 46 46 46 46 46 20 69 73 20 74 68 65 20  7FFFFFFF is the 
19606 68 61 72 64 20 6c 69 6d 69 74 20 66 6f 72 20 74  hard limit for t
19607 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
19608 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65  es in a database
19609 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 42 79 20 70  .  ** file. By p
1960a 61 73 73 69 6e 67 20 74 68 69 73 20 61 73 20 74  assing this as t
1960b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
1960c 65 73 20 74 6f 20 63 6f 70 79 20 74 6f 0a 20 20  es to copy to.  
1960d 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
1960e 70 5f 73 74 65 70 28 29 2c 20 77 65 20 63 61 6e  p_step(), we can
1960f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
19610 74 68 65 20 63 6f 70 79 20 66 69 6e 69 73 68 65  the copy finishe
19611 73 20 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 61  s .  ** within a
19612 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 28 75 6e   single call (un
19613 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63  less an error oc
19614 63 75 72 73 29 2e 20 54 68 65 20 61 73 73 65 72  curs). The asser
19615 74 28 29 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  t() statement.  
19616 2a 2a 20 63 68 65 63 6b 73 20 74 68 69 73 20 61  ** checks this a
19617 73 73 75 6d 70 74 69 6f 6e 20 2d 20 28 70 2d 3e  ssumption - (p->
19618 72 63 29 20 73 68 6f 75 6c 64 20 62 65 20 73 65  rc) should be se
19619 74 20 74 6f 20 65 69 74 68 65 72 20 53 51 4c 49  t to either SQLI
1961a 54 45 5f 44 4f 4e 45 20 0a 20 20 2a 2a 20 6f 72  TE_DONE .  ** or
1961b 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a   an error code..
1961c 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62    */.  sqlite3_b
1961d 61 63 6b 75 70 5f 73 74 65 70 28 26 62 2c 20 30  ackup_step(&b, 0
1961e 78 37 46 46 46 46 46 46 46 29 3b 0a 20 20 61 73  x7FFFFFFF);.  as
1961f 73 65 72 74 28 20 62 2e 72 63 21 3d 53 51 4c 49  sert( b.rc!=SQLI
19620 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20  TE_OK );.  rc = 
19621 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
19622 69 6e 69 73 68 28 26 62 29 3b 0a 20 20 69 66 28  inish(&b);.  if(
19623 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
19624 7b 0a 20 20 20 20 70 54 6f 2d 3e 70 42 74 2d 3e  {.    pTo->pBt->
19625 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20  pageSizeFixed = 
19626 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  0;.  }..  sqlite
19627 33 42 74 72 65 65 4c 65 61 76 65 28 70 46 72 6f  3BtreeLeave(pFro
19628 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  m);.  sqlite3Btr
19629 65 65 4c 65 61 76 65 28 70 54 6f 29 3b 0a 20 20  eeLeave(pTo);.  
1962a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
1962b 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1962c 49 54 5f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a  IT_VACUUM */../*
1962d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1962e 64 20 6f 66 20 62 61 63 6b 75 70 2e 63 20 2a 2a  d of backup.c **
1962f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19631 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
19632 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
19633 67 69 6e 20 66 69 6c 65 20 76 64 62 65 6d 65 6d  gin file vdbemem
19634 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
19635 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19636 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
19637 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a  .** 2004 May 26.
19638 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
19639 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1963a 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1963b 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1963c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1963d 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1963e 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1963f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
19640 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
19641 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
19642 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
19643 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
19644 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
19645 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
19646 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
19647 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
19648 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
19649 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1964a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1964b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1964c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1964d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1964e 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
1964f 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
19650 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20 22   to manipulate "
19651 4d 65 6d 22 20 73 74 72 75 63 74 75 72 65 2e 20  Mem" structure. 
19652 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72   A "Mem".** stor
19653 65 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c 75  es a single valu
19654 65 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20  e in the VDBE.  
19655 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 71 75 65  Mem is an opaque
19656 20 73 74 72 75 63 74 75 72 65 20 76 69 73 69 62   structure visib
19657 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 69  le.** only withi
19658 6e 20 74 68 65 20 56 44 42 45 2e 20 20 49 6e 74  n the VDBE.  Int
19659 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20  erface routines 
1965a 72 65 66 65 72 20 74 6f 20 61 20 4d 65 6d 20 75  refer to a Mem u
1965b 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d 65  sing the.** name
1965c 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a 2f   sqlite_value.*/
1965d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c  ../*.** Call sql
1965e 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e  ite3VdbeMemExpan
1965f 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73  dBlob() on the s
19660 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74  upplied value (t
19661 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69  ype Mem*).** P i
19662 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23  f required..*/.#
19663 64 65 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f  define expandBlo
19664 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67  b(P) (((P)->flag
19665 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69  s&MEM_Zero)?sqli
19666 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
19667 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a  Blob(P):0)../*.*
19668 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20  * If pMem is an 
19669 6f 62 6a 65 63 74 20 77 69 74 68 20 61 20 76 61  object with a va
1966a 6c 69 64 20 73 74 72 69 6e 67 20 72 65 70 72 65  lid string repre
1966b 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20  sentation, this 
1966c 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72  routine.** ensur
1966d 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  es the internal 
1966e 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65  encoding for the
1966f 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
19670 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65  tation is.** 'de
19671 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f  siredEnc', one o
19672 66 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  f SQLITE_UTF8, S
19673 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
19674 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e   SQLITE_UTF16BE.
19675 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69  .**.** If pMem i
19676 73 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f  s not a string o
19677 62 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e  bject, or the en
19678 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74  coding of the st
19679 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e  ring.** represen
1967a 74 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64  tation is alread
1967b 79 20 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74  y stored using t
1967c 68 65 20 72 65 71 75 65 73 74 65 64 20 65 6e 63  he requested enc
1967d 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73  oding, then this
1967e 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61  .** routine is a
1967f 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51   no-op..**.** SQ
19680 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
19681 6e 65 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65  ned if the conve
19682 72 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73  rsion is success
19683 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75  ful (or not requ
19684 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45  ired)..** SQLITE
19685 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65  _NOMEM may be re
19686 74 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c  turned if a mall
19687 6f 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e  oc() fails durin
19688 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20  g conversion.** 
19689 62 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e  between formats.
1968a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1968b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1968c 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
1968d 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20  (Mem *pMem, int 
1968e 64 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69  desiredEnc){.  i
1968f 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
19690 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45   (pMem->flags&ME
19691 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a  M_RowSet)==0 );.
19692 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65    assert( desire
19693 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
19694 38 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d  8 || desiredEnc=
19695 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a  =SQLITE_UTF16LE.
19696 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65             || de
19697 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
19698 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66  _UTF16BE );.  if
19699 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  ( !(pMem->flags&
1969a 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d  MEM_Str) || pMem
1969b 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e  ->enc==desiredEn
1969c 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
1969d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1969e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
1969f 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
196a0 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
196a1 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23  >db->mutex) );.#
196a2 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
196a3 54 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e  T_UTF16.  return
196a4 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23   SQLITE_ERROR;.#
196a5 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72  else..  /* MemTr
196a6 61 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65  anslate() may re
196a7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
196a8 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20  r SQLITE_NOMEM. 
196a9 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75  If NOMEM is retu
196aa 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20  rned,.  ** then 
196ab 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
196ac 74 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f  the value may no
196ad 74 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a  t have changed..
196ae 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
196af 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c  te3VdbeMemTransl
196b0 61 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65  ate(pMem, (u8)de
196b1 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73  siredEnc);.  ass
196b2 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ert(rc==SQLITE_O
196b3 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  K    || rc==SQLI
196b4 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73  TE_NOMEM);.  ass
196b5 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ert(rc==SQLITE_O
196b6 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e  K    || pMem->en
196b7 63 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a  c!=desiredEnc);.
196b8 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c    assert(rc==SQL
196b9 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65  ITE_NOMEM || pMe
196ba 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45  m->enc==desiredE
196bb 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  nc);.  return rc
196bc 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
196bd 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d  * Make sure pMem
196be 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  ->z points to a 
196bf 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74  writable allocat
196c0 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20  ion of at least 
196c1 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a  .** n bytes..**.
196c2 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79  ** If the memory
196c3 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20   cell currently 
196c4 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20  contains string 
196c5 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20  or blob data.** 
196c6 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72  and the third ar
196c7 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f  gument passed to
196c8 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
196c9 73 20 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20  s true, the .** 
196ca 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20  current content 
196cb 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70  of the cell is p
196cc 72 65 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77  reserved. Otherw
196cd 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62  ise, it may.** b
196ce 65 20 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a  e discarded.  .*
196cf 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
196d0 6f 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f  on sets the MEM_
196d1 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65  Dyn flag and cle
196d2 61 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c  ars any xDel cal
196d3 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73  lback..** It als
196d4 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68  o clears MEM_Eph
196d5 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69  em and MEM_Stati
196d6 63 2e 20 49 66 20 74 68 65 20 70 72 65 73 65 72  c. If the preser
196d7 76 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e  ve flag is .** n
196d8 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73  ot set, Mem.n is
196d9 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49   zeroed..*/.SQLI
196da 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
196db 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
196dc 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74  w(Mem *pMem, int
196dd 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65   n, int preserve
196de 29 7b 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e  ){.  assert( 1 >
196df 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d  =.    ((pMem->zM
196e0 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a  alloc && pMem->z
196e1 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29  Malloc==pMem->z)
196e2 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20   ? 1 : 0) +.    
196e3 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  (((pMem->flags&M
196e4 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78  EM_Dyn)&&pMem->x
196e5 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20  Del) ? 1 : 0) + 
196e6 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61  .    ((pMem->fla
196e7 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20  gs&MEM_Ephem) ? 
196e8 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28  1 : 0) + .    ((
196e9 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
196ea 53 74 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29  Static) ? 1 : 0)
196eb 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  .  );.  assert( 
196ec 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
196ed 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a  _RowSet)==0 );..
196ee 20 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d    if( n<32 ) n =
196ef 20 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74   32;.  if( sqlit
196f0 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  e3DbMallocSize(p
196f1 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
196f2 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20  Malloc)<n ){.   
196f3 20 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26   if( preserve &&
196f4 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e   pMem->z==pMem->
196f5 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  zMalloc ){.     
196f6 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d   pMem->z = pMem-
196f7 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  >zMalloc = sqlit
196f8 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
196f9 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
196fa 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70  ->z, n);.      p
196fb 72 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20  reserve = 0;.   
196fc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
196fd 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d  lite3DbFree(pMem
196fe 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c  ->db, pMem->zMal
196ff 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d  loc);.      pMem
19700 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69  ->zMalloc = sqli
19701 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
19702 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20  Mem->db, n);.   
19703 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d   }.  }..  if( pM
19704 65 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76  em->z && preserv
19705 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  e && pMem->zMall
19706 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70  oc && pMem->z!=p
19707 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a  Mem->zMalloc ){.
19708 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d      memcpy(pMem-
19709 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e  >zMalloc, pMem->
1970a 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d  z, pMem->n);.  }
1970b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  .  if( pMem->fla
1970c 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d  gs&MEM_Dyn && pM
1970d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20  em->xDel ){.    
1970e 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64  pMem->xDel((void
1970f 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20   *)(pMem->z));. 
19710 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20   }..  pMem->z = 
19711 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20  pMem->zMalloc;. 
19712 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20   if( pMem->z==0 
19713 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
19714 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
19715 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d   }else{.    pMem
19716 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
19717 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69  _Ephem|MEM_Stati
19718 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e  c);.  }.  pMem->
19719 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75  xDel = 0;.  retu
1971a 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51  rn (pMem->z ? SQ
1971b 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
1971c 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  _NOMEM);.}../*.*
1971d 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e  * Make the given
1971e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f   Mem object MEM_
1971f 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  Dyn.  In other w
19720 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f  ords, make it so
19721 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58  .** that any TEX
19722 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e  T or BLOB conten
19723 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d  t is stored in m
19724 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
19725 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e  rom.** malloc().
19726 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77    In this way, w
19727 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20  e know that the 
19728 6d 65 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74  memory is safe t
19729 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74  o be.** overwrit
1972a 74 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a  ten or altered..
1972b 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
1972c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
1972d 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  s or SQLITE_NOME
1972e 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  M if malloc fail
1972f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
19730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
19731 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
19732 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  able(Mem *pMem){
19733 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65  .  int f;.  asse
19734 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
19735 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
19736 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
19737 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
19738 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
19739 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20  &MEM_RowSet)==0 
1973a 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28  );.  expandBlob(
1973b 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65  pMem);.  f = pMe
1973c 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20  m->flags;.  if( 
1973d 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  (f&(MEM_Str|MEM_
1973e 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e  Blob)) && pMem->
1973f 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63  z!=pMem->zMalloc
19740 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
19741 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
19742 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32  Mem, pMem->n + 2
19743 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65  , 1) ){.      re
19744 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
19745 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65  M;.    }.    pMe
19746 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20  m->z[pMem->n] = 
19747 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70  0;.    pMem->z[p
19748 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20  Mem->n+1] = 0;. 
19749 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
1974a 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a  = MEM_Term;.  }.
1974b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1974c 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  _OK;.}../*.** If
1974d 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20   the given Mem* 
1974e 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65  has a zero-fille
1974f 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20  d tail, turn it 
19750 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79  into an ordinary
19751 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20  .** blob stored 
19752 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  in dynamically a
19753 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a  llocated space..
19754 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
19755 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
19756 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19757 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
19758 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20  mExpandBlob(Mem 
19759 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d  *pMem){.  if( pM
1975a 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1975b 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20  Zero ){.    int 
1975c 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72  nByte;.    asser
1975d 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  t( pMem->flags&M
1975e 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61  EM_Blob );.    a
1975f 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
19760 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  ags&MEM_RowSet)=
19761 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
19762 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
19763 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19764 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
19765 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  tex) );..    /* 
19766 53 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65  Set nByte to the
19767 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
19768 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
19769 72 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20  re the expanded 
1976a 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79  blob. */.    nBy
1976b 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70  te = pMem->n + p
1976c 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20  Mem->u.nZero;.  
1976d 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29    if( nByte<=0 )
1976e 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20  {.      nByte = 
1976f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
19770 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
19771 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c  row(pMem, nByte,
19772 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74   1) ){.      ret
19773 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
19774 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d  ;.    }..    mem
19775 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65  set(&pMem->z[pMe
19776 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e  m->n], 0, pMem->
19777 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d  u.nZero);.    pM
19778 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75  em->n += pMem->u
19779 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d  .nZero;.    pMem
1977a 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
1977b 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b  _Zero|MEM_Term);
1977c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
1977d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
1977e 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  f.../*.** Make s
1977f 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65  ure the given Me
19780 6d 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d  m is \u0000 term
19781 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  inated..*/.SQLIT
19782 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19783 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54  lite3VdbeMemNulT
19784 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d  erminate(Mem *pM
19785 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  em){.  assert( p
19786 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
19787 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
19788 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
19789 29 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d  ) );.  if( (pMem
1978a 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65  ->flags & MEM_Te
1978b 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d  rm)!=0 || (pMem-
1978c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72  >flags & MEM_Str
1978d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
1978e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20  rn SQLITE_OK;   
1978f 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f  /* Nothing to do
19790 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71   */.  }.  if( sq
19791 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
19792 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32  (pMem, pMem->n+2
19793 2c 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 1) ){.    retu
19794 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
19795 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70  .  }.  pMem->z[p
19796 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70  Mem->n] = 0;.  p
19797 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31  Mem->z[pMem->n+1
19798 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66  ] = 0;.  pMem->f
19799 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d  lags |= MEM_Term
1979a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1979b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
1979c 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68  dd MEM_Str to th
1979d 65 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65  e set of represe
1979e 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65  ntations for the
1979f 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d   given Mem.  Num
197a0 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76  bers.** are conv
197a1 65 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69  erted using sqli
197a2 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20  te3_snprintf(). 
197a3 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c   Converting a BL
197a4 4f 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a  OB to a string.*
197a5 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  * is a no-op..**
197a6 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70  .** Existing rep
197a7 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d  resentations MEM
197a8 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61  _Int and MEM_Rea
197a9 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61  l are *not* inva
197aa 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41  lidated..**.** A
197ab 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20   MEM_Null value 
197ac 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61  will never be pa
197ad 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e  ssed to this fun
197ae 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63  ction. This func
197af 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20  tion is.** used 
197b0 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76  for converting v
197b1 61 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f  alues to text fo
197b2 72 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74  r returning to t
197b3 68 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69  he user (i.e. vi
197b4 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  a.** sqlite3_val
197b5 75 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66  ue_text()), or f
197b6 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74  or ensuring that
197b7 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73   values to be us
197b8 65 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b  ed as btree.** k
197b9 65 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e  eys are strings.
197ba 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63   In the former c
197bb 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ase a NULL point
197bc 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  er is returned t
197bd 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74  he.** user and t
197be 68 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69  he later is an i
197bf 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d  nternal programm
197c0 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51  ing error..*/.SQ
197c1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
197c2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
197c3 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d  tringify(Mem *pM
197c4 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20  em, int enc){.  
197c5 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
197c6 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70  OK;.  int fg = p
197c7 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f  Mem->flags;.  co
197c8 6e 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  nst int nByte = 
197c9 33 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  32;..  assert( p
197ca 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
197cb 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
197cc 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
197cd 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ) );.  assert( !
197ce 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b  (fg&MEM_Zero) );
197cf 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26  .  assert( !(fg&
197d0 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
197d1 62 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  b)) );.  assert(
197d2 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d   fg&(MEM_Int|MEM
197d3 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65  _Real) );.  asse
197d4 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
197d5 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20  &MEM_RowSet)==0 
197d6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  );.  assert( EIG
197d7 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
197d8 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69  T(pMem) );...  i
197d9 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
197da 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74  mGrow(pMem, nByt
197db 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74  e, 0) ){.    ret
197dc 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
197dd 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20  ;.  }..  /* For 
197de 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65  a Real or Intege
197df 72 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d  r, use sqlite3_m
197e0 70 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64  printf() to prod
197e1 75 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20  uce the UTF-8.  
197e2 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  ** string repres
197e3 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
197e4 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20  value. Then, if 
197e5 74 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63  the required enc
197e6 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54  oding.  ** is UT
197e7 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36  F-16le or UTF-16
197e8 62 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74  be do a translat
197e9 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20  ion..  ** .  ** 
197ea 46 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64  FIX ME: It would
197eb 20 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71   be better if sq
197ec 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
197ed 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36   could do UTF-16
197ee 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20  ..  */.  if( fg 
197ef 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
197f0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
197f1 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a  f(nByte, pMem->z
197f2 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e  , "%lld", pMem->
197f3 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  u.i);.  }else{. 
197f4 20 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20     assert( fg & 
197f5 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20  MEM_Real );.    
197f6 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
197f7 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c  (nByte, pMem->z,
197f8 20 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d   "%!.15g", pMem-
197f9 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d  >r);.  }.  pMem-
197fa 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  >n = sqlite3Strl
197fb 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20  en30(pMem->z);. 
197fc 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
197fd 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d  ITE_UTF8;.  pMem
197fe 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53  ->flags |= MEM_S
197ff 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73  tr|MEM_Term;.  s
19800 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
19801 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65  Encoding(pMem, e
19802 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  nc);.  return rc
19803 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72  ;.}../*.** Memor
19804 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74  y cell pMem cont
19805 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74  ains the context
19806 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65   of an aggregate
19807 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68   function..** Th
19808 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73  is routine calls
19809 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65   the finalize me
1980a 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75  thod for that fu
1980b 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20  nction.  The.** 
1980c 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67  result of the ag
1980d 67 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65  gregate is store
1980e 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d  d back into pMem
1980f 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
19810 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74  QLITE_ERROR if t
19811 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70  he finalizer rep
19812 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20  orts an error.  
19813 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68  SQLITE_OK.** oth
19814 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
19815 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19816 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61  lite3VdbeMemFina
19817 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  lize(Mem *pMem, 
19818 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b  FuncDef *pFunc){
19819 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1981a 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57  TE_OK;.  if( ALW
1981b 41 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75  AYS(pFunc && pFu
1981c 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29  nc->xFinalize) )
1981d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  {.    sqlite3_co
1981e 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61  ntext ctx;.    a
1981f 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
19820 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21  ags & MEM_Null)!
19821 3d 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65  =0 || pFunc==pMe
19822 6d 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20  m->u.pDef );.   
19823 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
19824 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
19825 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
19826 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
19827 20 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20     memset(&ctx, 
19828 30 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b  0, sizeof(ctx));
19829 0a 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73  .    ctx.s.flags
1982a 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1982b 20 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d   ctx.s.db = pMem
1982c 2d 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d  ->db;.    ctx.pM
1982d 65 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63  em = pMem;.    c
1982e 74 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63  tx.pFunc = pFunc
1982f 3b 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69  ;.    pFunc->xFi
19830 6e 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20  nalize(&ctx);.  
19831 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d    assert( 0==(pM
19832 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  em->flags&MEM_Dy
19833 6e 29 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65  n) && !pMem->xDe
19834 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  l );.    sqlite3
19835 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c  DbFree(pMem->db,
19836 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b   pMem->zMalloc);
19837 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d  .    memcpy(pMem
19838 2c 20 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66  , &ctx.s, sizeof
19839 28 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63  (ctx.s));.    rc
1983a 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a   = ctx.isError;.
1983b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1983c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
1983d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e   memory cell con
1983e 74 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76  tains a string v
1983f 61 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62  alue that must b
19840 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e  e freed by.** in
19841 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e  voking an extern
19842 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65  al callback, fre
19843 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e  e it now. Callin
19844 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  g this function.
19845 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65  ** does not free
19846 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63   any Mem.zMalloc
19847 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49   buffer..*/.SQLI
19848 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19849 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1984a 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65  leaseExternal(Me
1984b 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  m *p){.  assert(
1984c 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c   p->db==0 || sql
1984d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1984e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
1984f 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e  .  testcase( p->
19850 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20  flags & MEM_Agg 
19851 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
19852 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79  ->flags & MEM_Dy
19853 6e 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  n );.  testcase(
19854 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   p->flags & MEM_
19855 52 6f 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74  RowSet );.  test
19856 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26  case( p->flags &
19857 20 4d 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20   MEM_Frame );.  
19858 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45  if( p->flags&(ME
19859 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45  M_Agg|MEM_Dyn|ME
1985a 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61  M_RowSet|MEM_Fra
1985b 6d 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70  me) ){.    if( p
1985c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20  ->flags&MEM_Agg 
1985d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1985e 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28  VdbeMemFinalize(
1985f 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20  p, p->u.pDef);. 
19860 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d       assert( (p-
19861 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67  >flags & MEM_Agg
19862 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  )==0 );.      sq
19863 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19864 61 73 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73  ase(p);.    }els
19865 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d  e if( p->flags&M
19866 45 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65  EM_Dyn && p->xDe
19867 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  l ){.      asser
19868 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d  t( (p->flags&MEM
19869 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
1986a 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f       p->xDel((vo
1986b 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20  id *)p->z);.    
1986c 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20    p->xDel = 0;. 
1986d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
1986e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74  flags&MEM_RowSet
1986f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
19870 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e  3RowSetClear(p->
19871 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20  u.pRowSet);.    
19872 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61  }else if( p->fla
19873 67 73 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a  gs&MEM_Frame ){.
19874 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19875 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a  eMemSetNull(p);.
19876 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
19877 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d  ** Release any m
19878 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68  emory held by th
19879 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20  e Mem. This may 
1987a 6c 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e  leave the Mem in
1987b 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74   an.** inconsist
1987c 65 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65  ent state, for e
1987d 78 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d  xample with (Mem
1987e 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d  .z==0) and.** (M
1987f 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f  em.type==SQLITE_
19880 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TEXT)..*/.SQLITE
19881 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19882 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19883 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73  ase(Mem *p){.  s
19884 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
19885 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b  easeExternal(p);
19886 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
19887 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c  (p->db, p->zMall
19888 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b  oc);.  p->z = 0;
19889 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  .  p->zMalloc = 
1988a 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30  0;.  p->xDel = 0
1988b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
1988c 72 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45  rt a 64-bit IEEE
1988d 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36   double into a 6
1988e 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
1988f 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20  eger..** If the 
19890 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61  double is too la
19891 72 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30  rge, return 0x80
19892 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a  00000000000000..
19893 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65  **.** Most syste
19894 6d 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20  ms appear to do 
19895 74 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61  this simply by a
19896 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69  ssigning.** vari
19897 61 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75  ables and withou
19898 74 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67  t the extra rang
19899 65 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a  e tests.  But.**
1989a 20 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72   there are repor
1989b 74 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20  ts that windows 
1989c 74 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74  throws an expect
1989d 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c  ion.** if the fl
1989e 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
1989f 75 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e  ue is out of ran
198a0 67 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20  ge. (See ticket 
198a1 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75  #2880.).** Becau
198a2 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d  se we do not com
198a3 70 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61  pletely understa
198a4 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20  nd the problem, 
198a5 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20  we will.** take 
198a6 74 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65  the conservative
198a7 20 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c   approach and al
198a8 77 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65  ways do range te
198a9 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74  sts.** before at
198aa 74 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e  tempting the con
198ab 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  version..*/.stat
198ac 69 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49  ic i64 doubleToI
198ad 6e 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a  nt64(double r){.
198ae 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63    /*.  ** Many c
198af 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f  ompilers we enco
198b0 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66  unter do not def
198b1 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f  ine constants fo
198b2 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d  r the.  ** minim
198b3 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36  um and maximum 6
198b4 34 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20  4-bit integers, 
198b5 6f 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74  or they define t
198b6 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69  hem.  ** inconsi
198b7 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61  stently.  And ma
198b8 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73  ny do not unders
198b9 74 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f  tand the "LL" no
198ba 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20  tation..  ** So 
198bb 77 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77  we define our ow
198bc 6e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e  n static constan
198bd 74 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f  ts here using no
198be 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65  thing.  ** large
198bf 72 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20  r than a 32-bit 
198c0 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
198c1 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  ..  */.  static 
198c2 63 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74  const i64 maxInt
198c3 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34   = LARGEST_INT64
198c4 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
198c5 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d   i64 minInt = SM
198c6 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20  ALLEST_INT64;.. 
198c7 20 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d   if( r<(double)m
198c8 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  inInt ){.    ret
198c9 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65  urn minInt;.  }e
198ca 6c 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c  lse if( r>(doubl
198cb 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20  e)maxInt ){.    
198cc 2f 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72  /* minInt is cor
198cd 72 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20  rect here - not 
198ce 6d 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e  maxInt.  It turn
198cf 73 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 67  s out that assig
198d0 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65  ning.    ** a ve
198d1 72 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76  ry large positiv
198d2 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69  e number to an i
198d3 6e 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69  nteger results i
198d4 6e 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20  n a very large. 
198d5 20 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69     ** negative i
198d6 6e 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61  nteger.  This ma
198d7 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75  kes no sense, bu
198d8 74 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36  t it is what x86
198d9 20 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a   hardware.    **
198da 20 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d   does so for com
198db 70 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 69  patibility we wi
198dc 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69  ll do the same i
198dd 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20  n software. */. 
198de 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74     return minInt
198df 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
198e0 65 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20  eturn (i64)r;.  
198e1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
198e2 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69  n some kind of i
198e3 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 69  nteger value whi
198e4 63 68 20 69 73 20 74 68 65 20 62 65 73 74 20 77  ch is the best w
198e5 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72  e can do.** at r
198e6 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
198e7 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d  value that *pMem
198e8 20 64 65 73 63 72 69 62 65 73 20 61 73 20 61 6e   describes as an
198e9 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20   integer..** If 
198ea 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67  pMem is an integ
198eb 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  er, then the val
198ec 75 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 66  ue is exact.  If
198ed 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c   pMem is.** a fl
198ee 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65  oating-point the
198ef 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
198f0 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 65  rned is the inte
198f1 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20  ger part..** If 
198f2 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67  pMem is a string
198f3 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77   or blob, then w
198f4 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70  e make an attemp
198f5 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20  t to convert.** 
198f6 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65  it into a intege
198f7 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61  r and return tha
198f8 74 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72  t.  If pMem repr
198f9 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20  esents an.** an 
198fa 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20  SQL-NULL value, 
198fb 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20  return 0..**.** 
198fc 49 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e  If pMem represen
198fd 74 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75  ts a string valu
198fe 65 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20  e, its encoding 
198ff 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64  might be changed
19900 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19901 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56  ATE i64 sqlite3V
19902 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20  dbeIntValue(Mem 
19903 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c  *pMem){.  int fl
19904 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ags;.  assert( p
19905 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
19906 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
19907 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
19908 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45  ) );.  assert( E
19909 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
1990a 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66  ENT(pMem) );.  f
1990b 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  lags = pMem->fla
1990c 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20  gs;.  if( flags 
1990d 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
1990e 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e   return pMem->u.
1990f 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  i;.  }else if( f
19910 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
19911 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f  ){.    return do
19912 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d  ubleToInt64(pMem
19913 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ->r);.  }else if
19914 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53  ( flags & (MEM_S
19915 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a  tr|MEM_Blob) ){.
19916 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20      i64 value;. 
19917 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
19918 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69  = MEM_Str;.    i
19919 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  f( sqlite3VdbeCh
1991a 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65  angeEncoding(pMe
1991b 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a  m, SQLITE_UTF8).
1991c 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
1991d 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
1991e 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20  nate(pMem) ){.  
1991f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
19920 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
19921 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73  pMem->z );.    s
19922 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65  qlite3Atoi64(pMe
19923 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20  m->z, &value);. 
19924 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b     return value;
19925 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
19926 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
19927 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
19928 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74  best representat
19929 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74  ion of pMem that
1992a 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f   we can get into
1992b 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49   a.** double.  I
1992c 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64  f pMem is alread
1992d 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e  y a double or an
1992e 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
1992f 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20   its.** value.  
19930 49 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e  If it is a strin
19931 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74  g or blob, try t
19932 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20  o convert it to 
19933 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20  a double..** If 
19934 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65  it is a NULL, re
19935 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c  turn 0.0..*/.SQL
19936 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62  ITE_PRIVATE doub
19937 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  le sqlite3VdbeRe
19938 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65  alValue(Mem *pMe
19939 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
1993a 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
1993b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1993c 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
1993d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49   );.  assert( EI
1993e 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
1993f 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66  NT(pMem) );.  if
19940 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
19941 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
19942 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a  return pMem->r;.
19943 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d    }else if( pMem
19944 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
19945 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
19946 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e  (double)pMem->u.
19947 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  i;.  }else if( p
19948 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
19949 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
1994a 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c  ){.    /* (doubl
1994b 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53  e)0 In case of S
1994c 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
1994d 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a  ING_POINT... */.
1994e 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d      double val =
1994f 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20   (double)0;.    
19950 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
19951 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20  EM_Str;.    if( 
19952 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
19953 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20  eEncoding(pMem, 
19954 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20  SQLITE_UTF8).   
19955 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64      || sqlite3Vd
19956 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
19957 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20  e(pMem) ){.     
19958 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e   /* (double)0 In
19959 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f   case of SQLITE_
1995a 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
1995b 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20  INT... */.      
1995c 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30  return (double)0
1995d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
1995e 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20  rt( pMem->z );. 
1995f 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70     sqlite3AtoF(p
19960 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20  Mem->z, &val);. 
19961 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20     return val;. 
19962 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28   }else{.    /* (
19963 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65  double)0 In case
19964 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   of SQLITE_OMIT_
19965 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e  FLOATING_POINT..
19966 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
19967 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d  (double)0;.  }.}
19968 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20  ../*.** The MEM 
19969 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72  structure is alr
1996a 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e  eady a MEM_Real.
1996b 20 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61    Try to also ma
1996c 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49  ke it a.** MEM_I
1996d 6e 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f  nt if we can..*/
1996e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1996f 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
19970 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28  IntegerAffinity(
19971 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
19972 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
19973 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a  s & MEM_Real );.
19974 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
19975 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
19976 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Set)==0 );.  ass
19977 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30  ert( pMem->db==0
19978 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
19979 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d  x_held(pMem->db-
1997a 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
1997b 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
1997c 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20  ALIGNMENT(pMem) 
1997d 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20  );..  pMem->u.i 
1997e 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28  = doubleToInt64(
1997f 70 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20  pMem->r);..  /* 
19980 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61  Only mark the va
19981 6c 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65  lue as an intege
19982 72 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  r if.  **.  **  
19983 20 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d    (1) the round-
19984 74 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20  trip conversion 
19985 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20  real->int->real 
19986 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a  is a no-op, and.
19987 20 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20    **    (2) The 
19988 69 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 68  integer is neith
19989 65 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e  er the largest n
1998a 6f 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a  or the smallest.
1998b 20 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73    **        poss
1998c 69 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69  ible integer (ti
1998d 63 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a  cket #3922).  **
1998e 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  .  ** The second
1998f 20 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73   and third terms
19990 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   in the followin
19991 67 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e  g conditional en
19992 66 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20  forces.  ** the 
19993 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e  second condition
19994 20 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d   under the assum
19995 70 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74  ption that addit
19996 69 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75  ion overflow cau
19997 73 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20  ses.  ** values 
19998 74 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20  to wrap around. 
19999 20 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 65   On x86 hardware
1999a 2c 20 74 68 65 20 74 68 69 72 64 20 74 65 72 6d  , the third term
1999b 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   is always.  ** 
1999c 74 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62  true and could b
1999d 65 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20  e omitted.  But 
1999e 77 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62  we leave it in b
1999f 65 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a  ecause other.  *
199a0 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20  * architectures 
199a1 6d 69 67 68 74 20 62 65 68 61 76 65 20 64 69 66  might behave dif
199a2 66 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20  ferently..  */. 
199a3 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64   if( pMem->r==(d
199a4 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20  ouble)pMem->u.i 
199a5 26 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41  && pMem->u.i>SMA
199a6 4c 4c 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20  LLEST_INT64.    
199a7 20 20 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d    && ALWAYS(pMem
199a8 2d 3e 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e  ->u.i<LARGEST_IN
199a9 54 36 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d  T64) ){.    pMem
199aa 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49  ->flags |= MEM_I
199ab 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  nt;.  }.}../*.**
199ac 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f   Convert pMem to
199ad 20 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20   type integer.  
199ae 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70  Invalidate any p
199af 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74  rior representat
199b0 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
199b1 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
199b2 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
199b3 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  rify(Mem *pMem){
199b4 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
199b5 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
199b6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65  3_mutex_held(pMe
199b7 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
199b8 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
199b9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
199ba 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73  wSet)==0 );.  as
199bb 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
199bc 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29  _ALIGNMENT(pMem)
199bd 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69   );..  pMem->u.i
199be 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
199bf 74 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20  tValue(pMem);.  
199c0 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
199c1 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Mem, MEM_Int);. 
199c2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
199c3 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  K;.}../*.** Conv
199c4 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74  ert pMem so that
199c5 20 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d   it is of type M
199c6 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61  EM_Real..** Inva
199c7 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72  lidate any prior
199c8 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73   representations
199c9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
199ca 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
199cb 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65  dbeMemRealify(Me
199cc 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65  m *pMem){.  asse
199cd 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
199ce 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
199cf 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
199d0 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
199d1 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
199d2 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29  LIGNMENT(pMem) )
199d3 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73  ;..  pMem->r = s
199d4 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
199d5 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d  lue(pMem);.  Mem
199d6 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d  SetTypeFlag(pMem
199d7 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72  , MEM_Real);.  r
199d8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
199d9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
199da 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69  t pMem so that i
199db 74 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f  t has types MEM_
199dc 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20  Real or MEM_Int 
199dd 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61  or both..** Inva
199de 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72  lidate any prior
199df 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73   representations
199e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
199e1 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
199e2 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d  dbeMemNumerify(M
199e3 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75  em *pMem){.  dou
199e4 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36  ble r1, r2;.  i6
199e5 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28  4 i;.  assert( (
199e6 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pMem->flags & (M
199e7 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c  EM_Int|MEM_Real|
199e8 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b  MEM_Null))==0 );
199e9 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
199ea 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42  ->flags & (MEM_B
199eb 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30  lob|MEM_Str))!=0
199ec 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
199ed 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
199ee 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
199ef 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
199f0 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74   );.  r1 = sqlit
199f1 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
199f2 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75  pMem);.  i = dou
199f3 62 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a  bleToInt64(r1);.
199f4 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69    r2 = (double)i
199f5 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29  ;.  if( r1==r2 )
199f6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
199f7 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
199f8 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Mem);.  }else{. 
199f9 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b     pMem->r = r1;
199fa 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
199fb 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65  lag(pMem, MEM_Re
199fc 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  al);.  }.  retur
199fd 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
199fe 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79  /*.** Delete any
199ff 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
19a00 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
19a01 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65  e stored in *pMe
19a02 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51  m to NULL..*/.SQ
19a03 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19a04 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
19a05 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65  SetNull(Mem *pMe
19a06 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e  m){.  if( pMem->
19a07 66 6c 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d  flags & MEM_Fram
19a08 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
19a09 56 64 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28  VdbeFrameDelete(
19a0a 70 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b  pMem->u.pFrame);
19a0b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d  .  }.  if( pMem-
19a0c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
19a0d 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Set ){.    sqlit
19a0e 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d  e3RowSetClear(pM
19a0f 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a  em->u.pRowSet);.
19a10 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65    }.  MemSetType
19a11 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e  Flag(pMem, MEM_N
19a12 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79  ull);.  pMem->ty
19a13 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  pe = SQLITE_NULL
19a14 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
19a15 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76  e any previous v
19a16 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65  alue and set the
19a17 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42   value to be a B
19a18 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a  LOB of length.**
19a19 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c   n containing al
19a1a 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49  l zeros..*/.SQLI
19a1b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19a1c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19a1d 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70  tZeroBlob(Mem *p
19a1e 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73  Mem, int n){.  s
19a1f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
19a20 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d  ease(pMem);.  pM
19a21 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
19a22 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20  Blob|MEM_Zero;. 
19a23 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
19a24 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65  LITE_BLOB;.  pMe
19a25 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20  m->n = 0;.  if( 
19a26 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70  n<0 ) n = 0;.  p
19a27 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e  Mem->u.nZero = n
19a28 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
19a29 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69  SQLITE_UTF8;..#i
19a2a 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
19a2b 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69  _INCRBLOB.  sqli
19a2c 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
19a2d 4d 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66  Mem, n, 0);.  if
19a2e 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20  ( pMem->z ){.   
19a2f 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20   pMem->n = n;.  
19a30 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a    memset(pMem->z
19a31 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e  , 0, n);.  }.#en
19a32 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  dif.}../*.** Del
19a33 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ete any previous
19a34 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74   value and set t
19a35 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  he value stored 
19a36 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c  in *pMem to val,
19a37 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70  .** manifest typ
19a38 65 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51  e INTEGER..*/.SQ
19a39 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19a3a 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
19a3b 53 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d  SetInt64(Mem *pM
19a3c 65 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20  em, i64 val){.  
19a3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
19a3e 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70  lease(pMem);.  p
19a3f 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a  Mem->u.i = val;.
19a40 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19a41 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d  MEM_Int;.  pMem-
19a42 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
19a43 4e 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NTEGER;.}../*.**
19a44 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76   Delete any prev
19a45 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73  ious value and s
19a46 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  et the value sto
19a47 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20  red in *pMem to 
19a48 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74  val,.** manifest
19a49 20 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53   type REAL..*/.S
19a4a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
19a4b 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
19a4c 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a  mSetDouble(Mem *
19a4d 70 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c  pMem, double val
19a4e 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
19a4f 49 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20  IsNaN(val) ){.  
19a50 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19a51 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20  SetNull(pMem);. 
19a52 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
19a53 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
19a54 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65  e(pMem);.    pMe
19a55 6d 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20  m->r = val;.    
19a56 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
19a57 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d  M_Real;.    pMem
19a58 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
19a59 46 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  FLOAT;.  }.}../*
19a5a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70  .** Delete any p
19a5b 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e  revious value an
19a5c 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  d set the value 
19a5d 6f 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e  of pMem to be an
19a5e 0a 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61  .** empty boolea
19a5f 6e 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49  n index..*/.SQLI
19a60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19a61 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19a62 74 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65  tRowSet(Mem *pMe
19a63 6d 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  m){.  sqlite3 *d
19a64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20  b = pMem->db;.  
19a65 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
19a66 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
19a67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
19a68 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71  wSet)==0 );.  sq
19a69 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19a6a 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65  ase(pMem);.  pMe
19a6b 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c  m->zMalloc = sql
19a6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
19a6d 64 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64  db, 64);.  if( d
19a6e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
19a6f 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
19a70 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
19a71 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
19a72 72 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  rt( pMem->zMallo
19a73 63 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75  c );.    pMem->u
19a74 2e 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74  .pRowSet = sqlit
19a75 65 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c  e3RowSetInit(db,
19a76 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20   pMem->zMalloc, 
19a77 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
19a78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a79 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
19a7a 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
19a7b 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b  pMem->zMalloc));
19a7c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
19a7d 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20  m->u.pRowSet!=0 
19a7e 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  );.    pMem->fla
19a7f 67 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b  gs = MEM_RowSet;
19a80 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
19a81 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
19a82 20 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74   Mem object cont
19a83 61 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42  ains a TEXT or B
19a84 4c 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74  LOB that is.** t
19a85 6f 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65  oo large - whose
19a86 20 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51   size exceeds SQ
19a87 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e  LITE_MAX_LENGTH.
19a88 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19a89 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19a8a 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20  beMemTooBig(Mem 
19a8b 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
19a8c 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28  ->db!=0 );.  if(
19a8d 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d   p->flags & (MEM
19a8e 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29  _Str|MEM_Blob) )
19a8f 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d  {.    int n = p-
19a90 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66  >n;.    if( p->f
19a91 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
19a92 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d  ){.      n += p-
19a93 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a  >u.nZero;.    }.
19a94 20 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e      return n>p->
19a95 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
19a96 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b  E_LIMIT_LENGTH];
19a97 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
19a98 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20   .}../*.** Size 
19a99 6f 66 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f  of struct Mem no
19a9a 74 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20  t including the 
19a9b 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62  Mem.zMalloc memb
19a9c 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  er..*/.#define M
19a9d 45 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65  EMCELLSIZE (size
19a9e 5f 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29  _t)(&(((Mem *)0)
19a9f 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a  ->zMalloc))../*.
19aa0 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c  ** Make an shall
19aa1 6f 77 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d  ow copy of pFrom
19aa2 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f   into pTo.  Prio
19aa3 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a  r contents of.**
19aa4 20 70 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20   pTo are freed. 
19aa5 20 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69   The pFrom->z fi
19aa6 65 6c 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69  eld is not dupli
19aa7 63 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46  cated.  If.** pF
19aa8 72 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20  rom->z is used, 
19aa9 74 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e  then pTo->z poin
19aaa 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74  ts to the same t
19aab 68 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a  hing as pFrom->z
19aac 0a 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65  .** and flags ge
19aad 74 73 20 73 72 63 54 79 70 65 20 28 65 69 74 68  ts srcType (eith
19aae 65 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20  er MEM_Ephem or 
19aaf 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a  MEM_Static)..*/.
19ab0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19ab1 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
19ab2 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65  emShallowCopy(Me
19ab3 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65  m *pTo, const Me
19ab4 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72  m *pFrom, int sr
19ab5 63 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74  cType){.  assert
19ab6 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20  ( (pFrom->flags 
19ab7 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
19ab8 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
19ab9 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
19aba 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63  nal(pTo);.  memc
19abb 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d  py(pTo, pFrom, M
19abc 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70  EMCELLSIZE);.  p
19abd 54 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20  To->xDel = 0;.  
19abe 69 66 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67  if( (pFrom->flag
19abf 73 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c  s&MEM_Dyn)!=0 ||
19ac0 20 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d   pFrom->z==pFrom
19ac1 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20  ->zMalloc ){.   
19ac2 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e   pTo->flags &= ~
19ac3 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61  (MEM_Dyn|MEM_Sta
19ac4 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  tic|MEM_Ephem);.
19ac5 20 20 20 20 61 73 73 65 72 74 28 20 73 72 63 54      assert( srcT
19ac6 79 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c  ype==MEM_Ephem |
19ac7 7c 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53  | srcType==MEM_S
19ac8 74 61 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f  tatic );.    pTo
19ac9 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79  ->flags |= srcTy
19aca 70 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  pe;.  }.}../*.**
19acb 20 4d 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70   Make a full cop
19acc 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20  y of pFrom into 
19acd 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74  pTo.  Prior cont
19ace 65 6e 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a  ents of pTo are.
19acf 2a 2a 20 66 72 65 65 64 20 62 65 66 6f 72 65 20  ** freed before 
19ad0 74 68 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65  the copy is made
19ad1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19ad2 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
19ad3 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a  dbeMemCopy(Mem *
19ad4 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a  pTo, const Mem *
19ad5 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63  pFrom){.  int rc
19ad6 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
19ad7 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d   assert( (pFrom-
19ad8 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
19ad9 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  Set)==0 );.  sql
19ada 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19adb 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b  seExternal(pTo);
19adc 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70  .  memcpy(pTo, p
19add 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a  From, MEMCELLSIZ
19ade 45 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73  E);.  pTo->flags
19adf 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20   &= ~MEM_Dyn;.. 
19ae0 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26   if( pTo->flags&
19ae1 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
19ae2 62 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d  b) ){.    if( 0=
19ae3 3d 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d  =(pFrom->flags&M
19ae4 45 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20  EM_Static) ){.  
19ae5 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c      pTo->flags |
19ae6 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20  = MEM_Ephem;.   
19ae7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
19ae8 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
19ae9 62 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a  ble(pTo);.    }.
19aea 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
19aeb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  ;.}../*.** Trans
19aec 66 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  fer the contents
19aed 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f   of pFrom to pTo
19aee 2e 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76  . Any existing v
19aef 61 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a  alue in pTo is.*
19af0 2a 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f  * freed. If pFro
19af1 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d  m contains ephem
19af2 65 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70  eral data, a cop
19af3 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a  y is made..**.**
19af4 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20   pFrom contains 
19af5 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e  an SQL NULL when
19af6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
19af7 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  turns..*/.SQLITE
19af8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19af9 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
19afa 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a  (Mem *pTo, Mem *
19afb 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74  pFrom){.  assert
19afc 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c  ( pFrom->db==0 |
19afd 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
19afe 68 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e  held(pFrom->db->
19aff 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
19b00 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c  rt( pTo->db==0 |
19b01 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
19b02 68 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75  held(pTo->db->mu
19b03 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19b04 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c  ( pFrom->db==0 |
19b05 7c 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20  | pTo->db==0 || 
19b06 70 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e  pFrom->db==pTo->
19b07 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  db );..  sqlite3
19b08 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
19b09 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54  To);.  memcpy(pT
19b0a 6f 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66  o, pFrom, sizeof
19b0b 28 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d  (Mem));.  pFrom-
19b0c 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
19b0d 6c 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c  l;.  pFrom->xDel
19b0e 20 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a   = 0;.  pFrom->z
19b0f 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f  Malloc = 0;.}../
19b10 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
19b11 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74  value of a Mem t
19b12 6f 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72  o be a string or
19b13 20 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54   a BLOB..**.** T
19b14 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65  he memory manage
19b15 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65  ment strategy de
19b16 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c  pends on the val
19b17 75 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a  ue of the xDel.*
19b18 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20  * parameter. If 
19b19 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64  the value passed
19b1a 20 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53   is SQLITE_TRANS
19b1b 49 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a  IENT, then the .
19b1c 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70  ** string is cop
19b1d 69 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73  ied into a (poss
19b1e 69 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62  ibly existing) b
19b1f 75 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79  uffer managed by
19b20 20 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72   the .** Mem str
19b21 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73  ucture. Otherwis
19b22 65 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20  e, any existing 
19b23 62 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20  buffer is freed 
19b24 61 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74  and the.** point
19b25 65 72 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a  er copied..**.**
19b26 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69   If the string i
19b27 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20  s too large (if 
19b28 69 74 20 65 78 63 65 65 64 73 20 74 68 65 20 53  it exceeds the S
19b29 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
19b2a 54 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74  TH.** size limit
19b2b 29 20 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79  ) then no memory
19b2c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75   allocation occu
19b2d 72 73 2e 20 20 49 66 20 74 68 65 20 73 74 72 69  rs.  If the stri
19b2e 6e 67 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f  ng can be.** sto
19b2f 72 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f  red without allo
19b30 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74  cating memory, t
19b31 68 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61  hen it is.  If a
19b32 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
19b33 6f 6e 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65  on.** is require
19b34 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73  d to store the s
19b35 74 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75  tring, then valu
19b36 65 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63  e of pMem is unc
19b37 68 61 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65  hanged.  In.** e
19b38 69 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49  ither case, SQLI
19b39 54 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74  TE_TOOBIG is ret
19b3a 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
19b3b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19b3c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
19b3d 72 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20  r(.  Mem *pMem, 
19b3e 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
19b3f 72 79 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74  ry cell to set t
19b40 6f 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a  o string value *
19b41 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
19b42 7a 2c 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e  z,      /* Strin
19b43 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69  g pointer */.  i
19b44 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20  nt n,           
19b45 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73     /* Bytes in s
19b46 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69  tring, or negati
19b47 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20  ve */.  u8 enc, 
19b48 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
19b49 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30  ncoding of z.  0
19b4a 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20   for BLOBs */.  
19b4b 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
19b4c 64 2a 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f  d*) /* Destructo
19b4d 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  r function */.){
19b4e 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e  .  int nByte = n
19b4f 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61  ;      /* New va
19b50 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20  lue for pMem->n 
19b51 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b  */.  int iLimit;
19b52 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
19b53 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69  mum allowed stri
19b54 6e 67 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20  ng or blob size 
19b55 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d  */.  u16 flags =
19b56 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20   0;      /* New 
19b57 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e  value for pMem->
19b58 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65  flags */..  asse
19b59 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
19b5a 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
19b5b 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
19b5c 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
19b5d 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
19b5e 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
19b5f 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20  0 );..  /* If z 
19b60 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
19b61 72 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63  r, set pMem to c
19b62 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55  ontain an SQL NU
19b63 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20  LL. */.  if( !z 
19b64 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
19b65 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65  beMemSetNull(pMe
19b66 6d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  m);.    return S
19b67 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
19b68 20 69 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b   if( pMem->db ){
19b69 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d  .    iLimit = pM
19b6a 65 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  em->db->aLimit[S
19b6b 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
19b6c 54 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  TH];.  }else{.  
19b6d 20 20 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54    iLimit = SQLIT
19b6e 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20  E_MAX_LENGTH;.  
19b6f 7d 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63  }.  flags = (enc
19b70 3d 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d  ==0?MEM_Blob:MEM
19b71 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79  _Str);.  if( nBy
19b72 74 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65  te<0 ){.    asse
19b73 72 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20  rt( enc!=0 );.  
19b74 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54    if( enc==SQLIT
19b75 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20  E_UTF8 ){.      
19b76 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79  for(nByte=0; nBy
19b77 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b  te<=iLimit && z[
19b78 6e 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29  nByte]; nByte++)
19b79 7b 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  {}.    }else{.  
19b7a 20 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b      for(nByte=0;
19b7b 20 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26   nByte<=iLimit &
19b7c 26 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b  & (z[nByte] | z[
19b7d 6e 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65  nByte+1]); nByte
19b7e 2b 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20  +=2){}.    }.   
19b7f 20 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65   flags |= MEM_Te
19b80 72 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  rm;.  }..  /* Th
19b81 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
19b82 6b 20 73 65 74 73 20 74 68 65 20 6e 65 77 20 76  k sets the new v
19b83 61 6c 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61  alues of Mem.z a
19b84 6e 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a  nd Mem.xDel. It.
19b85 20 20 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61    ** also sets a
19b86 20 66 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76   flag in local v
19b87 61 72 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20  ariable "flags" 
19b88 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20  to indicate the 
19b89 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61  memory.  ** mana
19b8a 67 65 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d  gement (one of M
19b8b 45 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74  EM_Dyn or MEM_St
19b8c 61 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66  atic)..  */.  if
19b8d 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54  ( xDel==SQLITE_T
19b8e 52 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20  RANSIENT ){.    
19b8f 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79  int nAlloc = nBy
19b90 74 65 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67  te;.    if( flag
19b91 73 26 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20  s&MEM_Term ){.  
19b92 20 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65      nAlloc += (e
19b93 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f  nc==SQLITE_UTF8?
19b94 31 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  1:2);.    }.    
19b95 69 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74  if( nByte>iLimit
19b96 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19b97 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a   SQLITE_TOOBIG;.
19b98 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
19b99 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
19b9a 28 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30  (pMem, nAlloc, 0
19b9b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
19b9c 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
19b9d 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79      }.    memcpy
19b9e 28 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c  (pMem->z, z, nAl
19b9f 6c 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  loc);.  }else if
19ba0 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44  ( xDel==SQLITE_D
19ba1 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71  YNAMIC ){.    sq
19ba2 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19ba3 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70  ase(pMem);.    p
19ba4 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70  Mem->zMalloc = p
19ba5 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a  Mem->z = (char *
19ba6 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44  )z;.    pMem->xD
19ba7 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  el = 0;.  }else{
19ba8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19ba9 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
19baa 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20  ;.    pMem->z = 
19bab 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70  (char *)z;.    p
19bac 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c  Mem->xDel = xDel
19bad 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28  ;.    flags |= (
19bae 28 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54  (xDel==SQLITE_ST
19baf 41 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63  ATIC)?MEM_Static
19bb0 3a 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a  :MEM_Dyn);.  }..
19bb1 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74    pMem->n = nByt
19bb2 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  e;.  pMem->flags
19bb3 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d   = flags;.  pMem
19bb4 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20  ->enc = (enc==0 
19bb5 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20  ? SQLITE_UTF8 : 
19bb6 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79  enc);.  pMem->ty
19bb7 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53  pe = (enc==0 ? S
19bb8 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c  QLITE_BLOB : SQL
19bb9 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e  ITE_TEXT);..#ifn
19bba 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19bbb 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d  UTF16.  if( pMem
19bbc 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54  ->enc!=SQLITE_UT
19bbd 46 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  F8 && sqlite3Vdb
19bbe 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d  eMemHandleBom(pM
19bbf 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  em) ){.    retur
19bc0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
19bc1 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
19bc2 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29  ( nByte>iLimit )
19bc3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
19bc4 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a  ITE_TOOBIG;.  }.
19bc5 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
19bc6 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  _OK;.}../*.** Co
19bc7 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73  mpare the values
19bc8 20 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68   contained by th
19bc9 65 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c  e two memory cel
19bca 6c 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a  ls, returning.**
19bcb 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20   negative, zero 
19bcc 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70  or positive if p
19bcd 4d 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61  Mem1 is less tha
19bce 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20  n, equal to, or 
19bcf 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20  greater.** than 
19bd0 70 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f  pMem2. Sorting o
19bd1 72 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66  rder is NULL's f
19bd2 69 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62  irst, followed b
19bd3 79 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67  y numbers (integ
19bd4 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73  ers.** and reals
19bd5 29 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63  ) sorted numeric
19bd6 61 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62  ally, followed b
19bd7 79 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62  y text ordered b
19bd8 79 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a  y the collating.
19bd9 2a 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c  ** sequence pCol
19bda 6c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c  l and finally bl
19bdb 6f 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20  ob's ordered by 
19bdc 6d 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20  memcmp()..**.** 
19bdd 54 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20  Two NULL values 
19bde 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65  are considered e
19bdf 71 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e  qual by this fun
19be0 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
19be1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19be2 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63  ite3MemCompare(c
19be3 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c  onst Mem *pMem1,
19be4 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d   const Mem *pMem
19be5 32 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71  2, const CollSeq
19be6 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20   *pColl){.  int 
19be7 72 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32  rc;.  int f1, f2
19be8 3b 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64  ;.  int combined
19be9 5f 66 6c 61 67 73 3b 0a 0a 20 20 66 31 20 3d 20  _flags;..  f1 = 
19bea 70 4d 65 6d 31 2d 3e 66 6c 61 67 73 3b 0a 20 20  pMem1->flags;.  
19beb 66 32 20 3d 20 70 4d 65 6d 32 2d 3e 66 6c 61 67  f2 = pMem2->flag
19bec 73 3b 0a 20 20 63 6f 6d 62 69 6e 65 64 5f 66 6c  s;.  combined_fl
19bed 61 67 73 20 3d 20 66 31 7c 66 32 3b 0a 20 20 61  ags = f1|f2;.  a
19bee 73 73 65 72 74 28 20 28 63 6f 6d 62 69 6e 65 64  ssert( (combined
19bef 5f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  _flags & MEM_Row
19bf0 53 65 74 29 3d 3d 30 20 29 3b 0a 20 0a 20 20 2f  Set)==0 );. .  /
19bf1 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69  * If one value i
19bf2 73 20 4e 55 4c 4c 2c 20 69 74 20 69 73 20 6c 65  s NULL, it is le
19bf3 73 73 20 74 68 61 6e 20 74 68 65 20 6f 74 68 65  ss than the othe
19bf4 72 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  r. If both value
19bf5 73 0a 20 20 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c  s.  ** are NULL,
19bf6 20 72 65 74 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a   return 0..  */.
19bf7 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66    if( combined_f
19bf8 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b  lags&MEM_Null ){
19bf9 0a 20 20 20 20 72 65 74 75 72 6e 20 28 66 32 26  .    return (f2&
19bfa 4d 45 4d 5f 4e 75 6c 6c 29 20 2d 20 28 66 31 26  MEM_Null) - (f1&
19bfb 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a  MEM_Null);.  }..
19bfc 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75    /* If one valu
19bfd 65 20 69 73 20 61 20 6e 75 6d 62 65 72 20 61 6e  e is a number an
19bfe 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e  d the other is n
19bff 6f 74 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 69  ot, the number i
19c00 73 20 6c 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20  s less..  ** If 
19c01 62 6f 74 68 20 61 72 65 20 6e 75 6d 62 65 72 73  both are numbers
19c02 2c 20 63 6f 6d 70 61 72 65 20 61 73 20 72 65 61  , compare as rea
19c03 6c 73 20 69 66 20 6f 6e 65 20 69 73 20 61 20 72  ls if one is a r
19c04 65 61 6c 2c 20 6f 72 20 61 73 20 69 6e 74 65 67  eal, or as integ
19c05 65 72 73 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68  ers.  ** if both
19c06 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65   values are inte
19c07 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  gers..  */.  if(
19c08 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26   combined_flags&
19c09 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
19c0a 6c 29 20 29 7b 0a 20 20 20 20 69 66 28 20 21 28  l) ){.    if( !(
19c0b 66 31 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  f1&(MEM_Int|MEM_
19c0c 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20  Real)) ){.      
19c0d 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
19c0e 20 20 20 20 69 66 28 20 21 28 66 32 26 28 4d 45      if( !(f2&(ME
19c0f 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29  M_Int|MEM_Real))
19c10 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19c11 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   -1;.    }.    i
19c12 66 28 20 28 66 31 20 26 20 66 32 20 26 20 4d 45  f( (f1 & f2 & ME
19c13 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20  M_Int)==0 ){.   
19c14 20 20 20 64 6f 75 62 6c 65 20 72 31 2c 20 72 32     double r1, r2
19c15 3b 0a 20 20 20 20 20 20 69 66 28 20 28 66 31 26  ;.      if( (f1&
19c16 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a  MEM_Real)==0 ){.
19c17 20 20 20 20 20 20 20 20 72 31 20 3d 20 28 64 6f          r1 = (do
19c18 75 62 6c 65 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b  uble)pMem1->u.i;
19c19 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19c1a 20 20 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31        r1 = pMem1
19c1b 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ->r;.      }.   
19c1c 20 20 20 69 66 28 20 28 66 32 26 4d 45 4d 5f 52     if( (f2&MEM_R
19c1d 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  eal)==0 ){.     
19c1e 20 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29     r2 = (double)
19c1f 70 4d 65 6d 32 2d 3e 75 2e 69 3b 0a 20 20 20 20  pMem2->u.i;.    
19c20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19c21 20 72 32 20 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a   r2 = pMem2->r;.
19c22 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
19c23 28 20 72 31 3c 72 32 20 29 20 72 65 74 75 72 6e  ( r1<r2 ) return
19c24 20 2d 31 3b 0a 20 20 20 20 20 20 69 66 28 20 72   -1;.      if( r
19c25 31 3e 72 32 20 29 20 72 65 74 75 72 6e 20 31 3b  1>r2 ) return 1;
19c26 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
19c27 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
19c28 20 20 61 73 73 65 72 74 28 20 66 31 26 4d 45 4d    assert( f1&MEM
19c29 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 61 73  _Int );.      as
19c2a 73 65 72 74 28 20 66 32 26 4d 45 4d 5f 49 6e 74  sert( f2&MEM_Int
19c2b 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d   );.      if( pM
19c2c 65 6d 31 2d 3e 75 2e 69 20 3c 20 70 4d 65 6d 32  em1->u.i < pMem2
19c2d 2d 3e 75 2e 69 20 29 20 72 65 74 75 72 6e 20 2d  ->u.i ) return -
19c2e 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65  1;.      if( pMe
19c2f 6d 31 2d 3e 75 2e 69 20 3e 20 70 4d 65 6d 32 2d  m1->u.i > pMem2-
19c30 3e 75 2e 69 20 29 20 72 65 74 75 72 6e 20 31 3b  >u.i ) return 1;
19c31 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
19c32 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
19c33 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73   If one value is
19c34 20 61 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68   a string and th
19c35 65 20 6f 74 68 65 72 20 69 73 20 61 20 62 6c 6f  e other is a blo
19c36 62 2c 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  b, the string is
19c37 20 6c 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62   less..  ** If b
19c38 6f 74 68 20 61 72 65 20 73 74 72 69 6e 67 73 2c  oth are strings,
19c39 20 63 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 74   compare using t
19c3a 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  he collating fun
19c3b 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  ctions..  */.  i
19c3c 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  f( combined_flag
19c3d 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20  s&MEM_Str ){.   
19c3e 20 69 66 28 20 28 66 31 20 26 20 4d 45 4d 5f 53   if( (f1 & MEM_S
19c3f 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  tr)==0 ){.      
19c40 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
19c41 20 20 20 20 69 66 28 20 28 66 32 20 26 20 4d 45      if( (f2 & ME
19c42 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20  M_Str)==0 ){.   
19c43 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20     return -1;.  
19c44 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28    }..    assert(
19c45 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65   pMem1->enc==pMe
19c46 6d 32 2d 3e 65 6e 63 20 29 3b 0a 20 20 20 20 61  m2->enc );.    a
19c47 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e  ssert( pMem1->en
19c48 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c  c==SQLITE_UTF8 |
19c49 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  | .            p
19c4a 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  Mem1->enc==SQLIT
19c4b 45 5f 55 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65  E_UTF16LE || pMe
19c4c 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  m1->enc==SQLITE_
19c4d 55 54 46 31 36 42 45 20 29 3b 0a 0a 20 20 20 20  UTF16BE );..    
19c4e 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  /* The collation
19c4f 20 73 65 71 75 65 6e 63 65 20 6d 75 73 74 20 62   sequence must b
19c50 65 20 64 65 66 69 6e 65 64 20 61 74 20 74 68 69  e defined at thi
19c51 73 20 70 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66  s point, even if
19c52 0a 20 20 20 20 2a 2a 20 74 68 65 20 75 73 65 72  .    ** the user
19c53 20 64 65 6c 65 74 65 73 20 74 68 65 20 63 6f 6c   deletes the col
19c54 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
19c55 61 66 74 65 72 20 74 68 65 20 76 64 62 65 20 70  after the vdbe p
19c56 72 6f 67 72 61 6d 20 69 73 0a 20 20 20 20 2a 2a  rogram is.    **
19c57 20 63 6f 6d 70 69 6c 65 64 20 28 74 68 69 73 20   compiled (this 
19c58 77 61 73 20 6e 6f 74 20 61 6c 77 61 79 73 20 74  was not always t
19c59 68 65 20 63 61 73 65 29 2e 0a 20 20 20 20 2a 2f  he case)..    */
19c5a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 43  .    assert( !pC
19c5b 6f 6c 6c 20 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43  oll || pColl->xC
19c5c 6d 70 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70  mp );..    if( p
19c5d 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  Coll ){.      if
19c5e 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43  ( pMem1->enc==pC
19c5f 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20  oll->enc ){.    
19c60 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e      /* The strin
19c61 67 73 20 61 72 65 20 61 6c 72 65 61 64 79 20 69  gs are already i
19c62 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 65 6e  n the correct en
19c63 63 6f 64 69 6e 67 2e 20 20 43 61 6c 6c 20 74 68  coding.  Call th
19c64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d  e.        ** com
19c65 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
19c66 20 64 69 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20   directly */.   
19c67 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 6f 6c       return pCol
19c68 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70  l->xCmp(pColl->p
19c69 55 73 65 72 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d  User,pMem1->n,pM
19c6a 65 6d 31 2d 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c  em1->z,pMem2->n,
19c6b 70 4d 65 6d 32 2d 3e 7a 29 3b 0a 20 20 20 20 20  pMem2->z);.     
19c6c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
19c6d 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 31 2c 20  const void *v1, 
19c6e 2a 76 32 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  *v2;.        int
19c6f 20 6e 31 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20   n1, n2;.       
19c70 20 4d 65 6d 20 63 31 3b 0a 20 20 20 20 20 20 20   Mem c1;.       
19c71 20 4d 65 6d 20 63 32 3b 0a 20 20 20 20 20 20 20   Mem c2;.       
19c72 20 6d 65 6d 73 65 74 28 26 63 31 2c 20 30 2c 20   memset(&c1, 0, 
19c73 73 69 7a 65 6f 66 28 63 31 29 29 3b 0a 20 20 20  sizeof(c1));.   
19c74 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 32 2c       memset(&c2,
19c75 20 30 2c 20 73 69 7a 65 6f 66 28 63 32 29 29 3b   0, sizeof(c2));
19c76 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
19c77 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
19c78 70 79 28 26 63 31 2c 20 70 4d 65 6d 31 2c 20 4d  py(&c1, pMem1, M
19c79 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20  EM_Ephem);.     
19c7a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19c7b 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 32  mShallowCopy(&c2
19c7c 2c 20 70 4d 65 6d 32 2c 20 4d 45 4d 5f 45 70 68  , pMem2, MEM_Eph
19c7d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 76 31 20  em);.        v1 
19c7e 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  = sqlite3ValueTe
19c7f 78 74 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  xt((sqlite3_valu
19c80 65 2a 29 26 63 31 2c 20 70 43 6f 6c 6c 2d 3e 65  e*)&c1, pColl->e
19c81 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 6e 31 20  nc);.        n1 
19c82 3d 20 76 31 3d 3d 30 20 3f 20 30 20 3a 20 63 31  = v1==0 ? 0 : c1
19c83 2e 6e 3b 0a 20 20 20 20 20 20 20 20 76 32 20 3d  .n;.        v2 =
19c84 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78   sqlite3ValueTex
19c85 74 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t((sqlite3_value
19c86 2a 29 26 63 32 2c 20 70 43 6f 6c 6c 2d 3e 65 6e  *)&c2, pColl->en
19c87 63 29 3b 0a 20 20 20 20 20 20 20 20 6e 32 20 3d  c);.        n2 =
19c88 20 76 32 3d 3d 30 20 3f 20 30 20 3a 20 63 32 2e   v2==0 ? 0 : c2.
19c89 6e 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  n;.        rc = 
19c8a 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c  pColl->xCmp(pCol
19c8b 6c 2d 3e 70 55 73 65 72 2c 20 6e 31 2c 20 76 31  l->pUser, n1, v1
19c8c 2c 20 6e 32 2c 20 76 32 29 3b 0a 20 20 20 20 20  , n2, v2);.     
19c8d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19c8e 6d 52 65 6c 65 61 73 65 28 26 63 31 29 3b 0a 20  mRelease(&c1);. 
19c8f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19c90 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 32  beMemRelease(&c2
19c91 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
19c92 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
19c93 20 20 7d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20    }.    /* If a 
19c94 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 77 61 73  NULL pointer was
19c95 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 63   passed as the c
19c96 6f 6c 6c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c  ollate function,
19c97 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 20 20   fall through.  
19c98 20 20 2a 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62    ** to the blob
19c99 20 63 61 73 65 20 61 6e 64 20 75 73 65 20 6d 65   case and use me
19c9a 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 7d 0a  mcmp().  */.  }.
19c9b 20 0a 20 20 2f 2a 20 42 6f 74 68 20 76 61 6c 75   .  /* Both valu
19c9c 65 73 20 6d 75 73 74 20 62 65 20 62 6c 6f 62 73  es must be blobs
19c9d 2e 20 20 43 6f 6d 70 61 72 65 20 75 73 69 6e 67  .  Compare using
19c9e 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20   memcmp().  */. 
19c9f 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4d 65   rc = memcmp(pMe
19ca0 6d 31 2d 3e 7a 2c 20 70 4d 65 6d 32 2d 3e 7a 2c  m1->z, pMem2->z,
19ca1 20 28 70 4d 65 6d 31 2d 3e 6e 3e 70 4d 65 6d 32   (pMem1->n>pMem2
19ca2 2d 3e 6e 29 3f 70 4d 65 6d 32 2d 3e 6e 3a 70 4d  ->n)?pMem2->n:pM
19ca3 65 6d 31 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 72  em1->n);.  if( r
19ca4 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  c==0 ){.    rc =
19ca5 20 70 4d 65 6d 31 2d 3e 6e 20 2d 20 70 4d 65 6d   pMem1->n - pMem
19ca6 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75  2->n;.  }.  retu
19ca7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
19ca8 4d 6f 76 65 20 64 61 74 61 20 6f 75 74 20 6f 66  Move data out of
19ca9 20 61 20 62 74 72 65 65 20 6b 65 79 20 6f 72 20   a btree key or 
19caa 64 61 74 61 20 66 69 65 6c 64 20 61 6e 64 20 69  data field and i
19cab 6e 74 6f 20 61 20 4d 65 6d 20 73 74 72 75 63 74  nto a Mem struct
19cac 75 72 65 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61  ure..** The data
19cad 20 6f 72 20 6b 65 79 20 69 73 20 74 61 6b 65 6e   or key is taken
19cae 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20   from the entry 
19caf 74 68 61 74 20 70 43 75 72 20 69 73 20 63 75 72  that pCur is cur
19cb0 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 0a  rently pointing.
19cb1 2a 2a 20 74 6f 2e 20 20 6f 66 66 73 65 74 20 61  ** to.  offset a
19cb2 6e 64 20 61 6d 74 20 64 65 74 65 72 6d 69 6e 65  nd amt determine
19cb3 20 77 68 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66   what portion of
19cb4 20 74 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79   the data or key
19cb5 20 74 6f 20 72 65 74 72 69 65 76 65 2e 0a 2a 2a   to retrieve..**
19cb6 20 6b 65 79 20 69 73 20 74 72 75 65 20 74 6f 20   key is true to 
19cb7 67 65 74 20 74 68 65 20 6b 65 79 20 6f 72 20 66  get the key or f
19cb8 61 6c 73 65 20 74 6f 20 67 65 74 20 64 61 74 61  alse to get data
19cb9 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73  .  The result is
19cba 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f   written.** into
19cbb 20 74 68 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e   the pMem elemen
19cbc 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4d 65  t..**.** The pMe
19cbd 6d 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  m structure is a
19cbe 73 73 75 6d 65 64 20 74 6f 20 62 65 20 75 6e 69  ssumed to be uni
19cbf 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 41 6e 79  nitialized.  Any
19cc0 20 70 72 69 6f 72 20 63 6f 6e 74 65 6e 74 0a 2a   prior content.*
19cc1 2a 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e  * is overwritten
19cc2 20 77 69 74 68 6f 75 74 20 62 65 69 6e 67 20 66   without being f
19cc3 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  reed..**.** If t
19cc4 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c  his routine fail
19cc5 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e  s for any reason
19cc6 20 28 6d 61 6c 6c 6f 63 20 72 65 74 75 72 6e 73   (malloc returns
19cc7 20 4e 55 4c 4c 20 6f 72 20 75 6e 61 62 6c 65 0a   NULL or unable.
19cc8 2a 2a 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  ** to read from 
19cc9 74 68 65 20 64 69 73 6b 29 20 74 68 65 6e 20 74  the disk) then t
19cca 68 65 20 70 4d 65 6d 20 69 73 20 6c 65 66 74 20  he pMem is left 
19ccb 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
19ccc 6e 74 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c  nt state..*/.SQL
19ccd 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19cce 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
19ccf 6f 6d 42 74 72 65 65 28 0a 20 20 42 74 43 75 72  omBtree(.  BtCur
19cd0 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 2f 2a 20  sor *pCur,   /* 
19cd1 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  Cursor pointing 
19cd2 61 74 20 72 65 63 6f 72 64 20 74 6f 20 72 65 74  at record to ret
19cd3 72 69 65 76 65 2e 20 2a 2f 0a 20 20 69 6e 74 20  rieve. */.  int 
19cd4 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20 2f 2a  offset,       /*
19cd5 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65   Offset from the
19cd6 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 74   start of data t
19cd7 6f 20 72 65 74 75 72 6e 20 62 79 74 65 73 20 66  o return bytes f
19cd8 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d  rom. */.  int am
19cd9 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  t,          /* N
19cda 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
19cdb 6f 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 69  o return. */.  i
19cdc 6e 74 20 6b 65 79 2c 20 20 20 20 20 20 20 20 20  nt key,         
19cdd 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 72 65 74   /* If true, ret
19cde 72 69 65 76 65 20 66 72 6f 6d 20 74 68 65 20 62  rieve from the b
19cdf 74 72 65 65 20 6b 65 79 2c 20 6e 6f 74 20 64 61  tree key, not da
19ce0 74 61 2e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ta. */.  Mem *pM
19ce1 65 6d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  em         /* OU
19ce2 54 3a 20 52 65 74 75 72 6e 20 64 61 74 61 20 69  T: Return data i
19ce3 6e 20 74 68 69 73 20 4d 65 6d 20 73 74 72 75 63  n this Mem struc
19ce4 74 75 72 65 2e 20 2a 2f 0a 29 7b 0a 20 20 63 68  ture. */.){.  ch
19ce5 61 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20  ar *zData;      
19ce6 20 20 2f 2a 20 44 61 74 61 20 66 72 6f 6d 20 74    /* Data from t
19ce7 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 2a  he btree layer *
19ce8 2f 0a 20 20 69 6e 74 20 61 76 61 69 6c 61 62 6c  /.  int availabl
19ce9 65 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65  e = 0;  /* Numbe
19cea 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c  r of bytes avail
19ceb 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61  able on the loca
19cec 6c 20 62 74 72 65 65 20 70 61 67 65 20 2a 2f 0a  l btree page */.
19ced 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
19cee 45 5f 4f 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20  E_OK; /* Return 
19cef 63 6f 64 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  code */..  asser
19cf0 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  t( sqlite3BtreeC
19cf1 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75  ursorIsValid(pCu
19cf2 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65  r) );..  /* Note
19cf3 3a 20 74 68 65 20 63 61 6c 6c 73 20 74 6f 20 42  : the calls to B
19cf4 74 72 65 65 4b 65 79 46 65 74 63 68 28 29 20 61  treeKeyFetch() a
19cf5 6e 64 20 44 61 74 61 46 65 74 63 68 28 29 20 62  nd DataFetch() b
19cf6 65 6c 6f 77 20 61 73 73 65 72 74 28 29 20 0a 20  elow assert() . 
19cf7 20 2a 2a 20 74 68 61 74 20 62 6f 74 68 20 74 68   ** that both th
19cf8 65 20 42 74 53 68 61 72 65 64 20 61 6e 64 20 64  e BtShared and d
19cf9 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d  atabase handle m
19cfa 75 74 65 78 65 73 20 61 72 65 20 68 65 6c 64 2e  utexes are held.
19cfb 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70   */.  assert( (p
19cfc 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
19cfd 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
19cfe 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20   if( key ){.    
19cff 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29  zData = (char *)
19d00 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46  sqlite3BtreeKeyF
19d01 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 69  etch(pCur, &avai
19d02 6c 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  lable);.  }else{
19d03 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68  .    zData = (ch
19d04 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65  ar *)sqlite3Btre
19d05 65 44 61 74 61 46 65 74 63 68 28 70 43 75 72 2c  eDataFetch(pCur,
19d06 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20   &available);.  
19d07 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 44 61 74  }.  assert( zDat
19d08 61 21 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 6f  a!=0 );..  if( o
19d09 66 66 73 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c  ffset+amt<=avail
19d0a 61 62 6c 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66  able && (pMem->f
19d0b 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30  lags&MEM_Dyn)==0
19d0c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
19d0d 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d  dbeMemRelease(pM
19d0e 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  em);.    pMem->z
19d0f 20 3d 20 26 7a 44 61 74 61 5b 6f 66 66 73 65 74   = &zData[offset
19d10 5d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ];.    pMem->fla
19d11 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45  gs = MEM_Blob|ME
19d12 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65  M_Ephem;.  }else
19d13 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
19d14 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62  (rc = sqlite3Vdb
19d15 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 61  eMemGrow(pMem, a
19d16 6d 74 2b 32 2c 20 30 29 29 20 29 7b 0a 20 20 20  mt+2, 0)) ){.   
19d17 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
19d18 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c  EM_Blob|MEM_Dyn|
19d19 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 4d  MEM_Term;.    pM
19d1a 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20  em->enc = 0;.   
19d1b 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
19d1c 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 20 20 69  LITE_BLOB;.    i
19d1d 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 20 20  f( key ){.      
19d1e 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
19d1f 65 4b 65 79 28 70 43 75 72 2c 20 6f 66 66 73 65  eKey(pCur, offse
19d20 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29  t, amt, pMem->z)
19d21 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
19d22 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
19d23 74 72 65 65 44 61 74 61 28 70 43 75 72 2c 20 6f  treeData(pCur, o
19d24 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d  ffset, amt, pMem
19d25 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ->z);.    }.    
19d26 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d 20 30  pMem->z[amt] = 0
19d27 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d  ;.    pMem->z[am
19d28 74 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66  t+1] = 0;.    if
19d29 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
19d2a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
19d2b 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
19d2c 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Mem);.    }.  }.
19d2d 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b    pMem->n = amt;
19d2e 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
19d2f 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ../* This functi
19d30 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  on is only avail
19d31 61 62 6c 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c  able internally,
19d32 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20   it is not part 
19d33 6f 66 20 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e  of the.** extern
19d34 61 6c 20 41 50 49 2e 20 49 74 20 77 6f 72 6b 73  al API. It works
19d35 20 69 6e 20 61 20 73 69 6d 69 6c 61 72 20 77 61   in a similar wa
19d36 79 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c  y to sqlite3_val
19d37 75 65 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78  ue_text(),.** ex
19d38 63 65 70 74 20 74 68 65 20 64 61 74 61 20 72 65  cept the data re
19d39 74 75 72 6e 65 64 20 69 73 20 69 6e 20 74 68 65  turned is in the
19d3a 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66   encoding specif
19d3b 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e  ied by the secon
19d3c 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20  d.** parameter, 
19d3d 77 68 69 63 68 20 6d 75 73 74 20 62 65 20 6f 6e  which must be on
19d3e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31  e of SQLITE_UTF1
19d3f 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  6BE, SQLITE_UTF1
19d40 36 4c 45 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  6LE or.** SQLITE
19d41 5f 55 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30  _UTF8..**.** (20
19d42 30 36 2d 30 32 2d 31 36 3a 29 20 20 54 68 65 20  06-02-16:)  The 
19d43 65 6e 63 20 76 61 6c 75 65 20 63 61 6e 20 62 65  enc value can be
19d44 20 6f 72 2d 65 64 20 77 69 74 68 20 53 51 4c 49   or-ed with SQLI
19d45 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
19d46 2e 0a 2a 2a 20 49 66 20 74 68 61 74 20 69 73 20  ..** If that is 
19d47 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74  the case, then t
19d48 68 65 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62  he result must b
19d49 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20  e aligned on an 
19d4a 65 76 65 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75  even byte.** bou
19d4b 6e 64 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ndary..*/.SQLITE
19d4c 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
19d4d 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75  oid *sqlite3Valu
19d4e 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61  eText(sqlite3_va
19d4f 6c 75 65 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e  lue* pVal, u8 en
19d50 63 29 7b 0a 20 20 69 66 28 20 21 70 56 61 6c 20  c){.  if( !pVal 
19d51 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 61  ) return 0;..  a
19d52 73 73 65 72 74 28 20 70 56 61 6c 2d 3e 64 62 3d  ssert( pVal->db=
19d53 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
19d54 74 65 78 5f 68 65 6c 64 28 70 56 61 6c 2d 3e 64  tex_held(pVal->d
19d55 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
19d56 73 73 65 72 74 28 20 28 65 6e 63 26 33 29 3d 3d  ssert( (enc&3)==
19d57 28 65 6e 63 26 7e 53 51 4c 49 54 45 5f 55 54 46  (enc&~SQLITE_UTF
19d58 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 3b 0a 20  16_ALIGNED) );. 
19d59 20 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e   assert( (pVal->
19d5a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
19d5b 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28  et)==0 );..  if(
19d5c 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d   pVal->flags&MEM
19d5d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  _Null ){.    ret
19d5e 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
19d5f 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e  ert( (MEM_Blob>>
19d60 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b  3) == MEM_Str );
19d61 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c  .  pVal->flags |
19d62 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26  = (pVal->flags &
19d63 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20   MEM_Blob)>>3;. 
19d64 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c   expandBlob(pVal
19d65 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66  );.  if( pVal->f
19d66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a  lags&MEM_Str ){.
19d67 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
19d68 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56  hangeEncoding(pV
19d69 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54  al, enc & ~SQLIT
19d6a 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
19d6b 3b 0a 20 20 20 20 69 66 28 20 28 65 6e 63 20 26  ;.    if( (enc &
19d6c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c   SQLITE_UTF16_AL
19d6d 49 47 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d  IGNED)!=0 && 1==
19d6e 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f  (1&SQLITE_PTR_TO
19d6f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29  _INT(pVal->z)) )
19d70 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
19d71 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28  (pVal->flags & (
19d72 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
19d73 61 74 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20  atic))!=0 );.   
19d74 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
19d75 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
19d76 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45  le(pVal)!=SQLITE
19d77 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
19d78 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
19d79 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
19d7a 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d  e3VdbeMemNulTerm
19d7b 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d  inate(pVal);.  }
19d7c 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
19d7d 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d  ( (pVal->flags&M
19d7e 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20  EM_Blob)==0 );. 
19d7f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19d80 6d 53 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c  mStringify(pVal,
19d81 20 65 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72   enc);.    asser
19d82 74 28 20 30 3d 3d 28 31 26 53 51 4c 49 54 45 5f  t( 0==(1&SQLITE_
19d83 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d  PTR_TO_INT(pVal-
19d84 3e 7a 29 29 20 29 3b 0a 20 20 7d 0a 20 20 61 73  >z)) );.  }.  as
19d85 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d 3d  sert(pVal->enc==
19d86 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55  (enc & ~SQLITE_U
19d87 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c 7c  TF16_ALIGNED) ||
19d88 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20 20   pVal->db==0.   
19d89 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 56             || pV
19d8a 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  al->db->mallocFa
19d8b 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 56  iled );.  if( pV
19d8c 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20  al->enc==(enc & 
19d8d 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
19d8e 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20 72 65  IGNED) ){.    re
19d8f 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20 20  turn pVal->z;.  
19d90 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
19d91 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  n 0;.  }.}../*.*
19d92 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73  * Create a new s
19d93 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
19d94 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ect..*/.SQLITE_P
19d95 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76  RIVATE sqlite3_v
19d96 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c  alue *sqlite3Val
19d97 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 64  ueNew(sqlite3 *d
19d98 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 73  b){.  Mem *p = s
19d99 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
19d9a 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  ro(db, sizeof(*p
19d9b 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  ));.  if( p ){. 
19d9c 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45     p->flags = ME
19d9d 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e 74  M_Null;.    p->t
19d9e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c  ype = SQLITE_NUL
19d9f 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20 64  L;.    p->db = d
19da0 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  b;.  }.  return 
19da1 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  p;.}../*.** Crea
19da2 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33  te a new sqlite3
19da3 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20 63  _value object, c
19da4 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61  ontaining the va
19da5 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a 2a  lue of pExpr..**
19da6 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f  .** This only wo
19da7 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73 69 6d  rks for very sim
19da8 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ple expressions 
19da9 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f 66 20  that consist of 
19daa 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20  one constant.** 
19dab 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 2c  token (i.e. "5",
19dac 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74 72 69   "5.1", "'a stri
19dad 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20 65 78  ng'"). If the ex
19dae 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a 20  pression can.** 
19daf 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64 69 72  be converted dir
19db0 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 6c  ectly into a val
19db1 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ue, then the val
19db2 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ue is allocated 
19db3 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  and.** a pointer
19db4 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 56   written to *ppV
19db5 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 69  al. The caller i
19db6 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
19db7 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a 2a  r deallocating.*
19db8 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79 20 70  * the value by p
19db9 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c  assing it to sql
19dba 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 29 20  ite3ValueFree() 
19dbb 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74 68 65  later on. If the
19dbc 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 63   expression.** c
19dbd 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74  annot be convert
19dbe 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c 20 74  ed to a value, t
19dbf 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20 73 65  hen *ppVal is se
19dc0 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51  t to NULL..*/.SQ
19dc1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19dc2 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
19dc3 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33  mExpr(.  sqlite3
19dc4 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
19dc5 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
19dc6 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
19dc7 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
19dc8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19dc9 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  The expression t
19dca 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a 20 20  o evaluate */.  
19dcb 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20  u8 enc,         
19dcc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63            /* Enc
19dcd 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a  oding to use */.
19dce 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 20    u8 affinity,  
19dcf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
19dd0 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 2a  ffinity to use *
19dd1 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  /.  sqlite3_valu
19dd2 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20 2f 2a  e **ppVal     /*
19dd3 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20 76   Write the new v
19dd4 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  alue here */.){.
19dd5 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68 61 72    int op;.  char
19dd6 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 73 71   *zVal = 0;.  sq
19dd7 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
19dd8 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 21 70  l = 0;..  if( !p
19dd9 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70 70 56  Expr ){.    *ppV
19dda 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  al = 0;.    retu
19ddb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
19ddc 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e  }.  op = pExpr->
19ddd 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b  op;.  if( op==TK
19dde 5f 52 45 47 49 53 54 45 52 20 29 7b 0a 20 20 20  _REGISTER ){.   
19ddf 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 32   op = pExpr->op2
19de0 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20  ;  /* This only 
19de1 68 61 70 70 65 6e 73 20 77 69 74 68 20 53 51 4c  happens with SQL
19de2 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
19de3 20 2a 2f 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6f   */.  }..  if( o
19de4 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20  p==TK_STRING || 
19de5 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20  op==TK_FLOAT || 
19de6 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29  op==TK_INTEGER )
19de7 7b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c  {.    pVal = sql
19de8 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29  ite3ValueNew(db)
19de9 3b 0a 20 20 20 20 69 66 28 20 70 56 61 6c 3d 3d  ;.    if( pVal==
19dea 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  0 ) goto no_mem;
19deb 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73  .    if( ExprHas
19dec 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
19ded 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 7b 0a  EP_IntValue) ){.
19dee 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19def 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 56 61  eMemSetInt64(pVa
19df0 6c 2c 20 28 69 36 34 29 70 45 78 70 72 2d 3e 75  l, (i64)pExpr->u
19df1 2e 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 7d 65  .iValue);.    }e
19df2 6c 73 65 7b 0a 20 20 20 20 20 20 7a 56 61 6c 20  lse{.      zVal 
19df3 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
19df4 70 28 64 62 2c 20 70 45 78 70 72 2d 3e 75 2e 7a  p(db, pExpr->u.z
19df5 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 69 66  Token);.      if
19df6 28 20 7a 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f  ( zVal==0 ) goto
19df7 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 73   no_mem;.      s
19df8 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
19df9 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c  r(pVal, -1, zVal
19dfa 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
19dfb 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
19dfc 20 20 20 20 20 20 69 66 28 20 6f 70 3d 3d 54 4b        if( op==TK
19dfd 5f 46 4c 4f 41 54 20 29 20 70 56 61 6c 2d 3e 74  _FLOAT ) pVal->t
19dfe 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f  ype = SQLITE_FLO
19dff 41 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  AT;.    }.    if
19e00 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45  ( (op==TK_INTEGE
19e01 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41  R || op==TK_FLOA
19e02 54 20 29 20 26 26 20 61 66 66 69 6e 69 74 79 3d  T ) && affinity=
19e03 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
19e04 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
19e05 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e  3ValueApplyAffin
19e06 69 74 79 28 70 56 61 6c 2c 20 53 51 4c 49 54 45  ity(pVal, SQLITE
19e07 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 53 51  _AFF_NUMERIC, SQ
19e08 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20  LITE_UTF8);.    
19e09 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
19e0a 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66  ite3ValueApplyAf
19e0b 66 69 6e 69 74 79 28 70 56 61 6c 2c 20 61 66 66  finity(pVal, aff
19e0c 69 6e 69 74 79 2c 20 53 51 4c 49 54 45 5f 55 54  inity, SQLITE_UT
19e0d 46 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  F8);.    }.    i
19e0e 66 28 20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55  f( enc!=SQLITE_U
19e0f 54 46 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  TF8 ){.      sql
19e10 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
19e11 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e 63  coding(pVal, enc
19e12 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
19e13 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e   if( op==TK_UMIN
19e14 55 53 20 29 20 7b 0a 20 20 20 20 69 66 28 20 53  US ) {.    if( S
19e15 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65  QLITE_OK==sqlite
19e16 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64  3ValueFromExpr(d
19e17 62 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 65  b,pExpr->pLeft,e
19e18 6e 63 2c 61 66 66 69 6e 69 74 79 2c 26 70 56 61  nc,affinity,&pVa
19e19 6c 29 20 29 7b 0a 20 20 20 20 20 20 70 56 61 6c  l) ){.      pVal
19e1a 2d 3e 75 2e 69 20 3d 20 2d 31 20 2a 20 70 56 61  ->u.i = -1 * pVa
19e1b 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 2f 2a  l->u.i;.      /*
19e1c 20 28 64 6f 75 62 6c 65 29 2d 31 20 49 6e 20 63   (double)-1 In c
19e1d 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
19e1e 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
19e1f 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 56  T... */.      pV
19e20 61 6c 2d 3e 72 20 3d 20 28 64 6f 75 62 6c 65 29  al->r = (double)
19e21 2d 31 20 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20  -1 * pVal->r;.  
19e22 20 20 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20    }.  }.#ifndef 
19e23 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
19e24 5f 4c 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20  _LITERAL.  else 
19e25 69 66 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20  if( op==TK_BLOB 
19e26 29 7b 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b  ){.    int nVal;
19e27 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
19e28 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d  pr->u.zToken[0]=
19e29 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75  ='x' || pExpr->u
19e2a 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20  .zToken[0]=='X' 
19e2b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
19e2c 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31  Expr->u.zToken[1
19e2d 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70  ]=='\'' );.    p
19e2e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Val = sqlite3Val
19e2f 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69  ueNew(db);.    i
19e30 66 28 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20  f( !pVal ) goto 
19e31 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7a 56 61 6c  no_mem;.    zVal
19e32 20 3d 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f   = &pExpr->u.zTo
19e33 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20 6e 56 61 6c  ken[2];.    nVal
19e34 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
19e35 33 30 28 7a 56 61 6c 29 2d 31 3b 0a 20 20 20 20  30(zVal)-1;.    
19e36 61 73 73 65 72 74 28 20 7a 56 61 6c 5b 6e 56 61  assert( zVal[nVa
19e37 6c 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20  l]=='\'' );.    
19e38 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19e39 74 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74  tStr(pVal, sqlit
19e3a 65 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20  e3HexToBlob(db, 
19e3b 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61  zVal, nVal), nVa
19e3c 6c 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20  l/2,.           
19e3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
19e3e 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
19e3f 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
19e40 69 66 28 20 70 56 61 6c 20 29 7b 0a 20 20 20 20  if( pVal ){.    
19e41 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
19e42 6f 72 65 54 79 70 65 28 70 56 61 6c 29 3b 0a 20  oreType(pVal);. 
19e43 20 7d 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56   }.  *ppVal = pV
19e44 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  al;.  return SQL
19e45 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a  ITE_OK;..no_mem:
19e46 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  .  db->mallocFai
19e47 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74  led = 1;.  sqlit
19e48 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61  e3DbFree(db, zVa
19e49 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c  l);.  sqlite3Val
19e4a 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20  ueFree(pVal);.  
19e4b 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65  *ppVal = 0;.  re
19e4c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
19e4d 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  M;.}../*.** Chan
19e4e 67 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 61  ge the string va
19e4f 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  lue of an sqlite
19e50 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a  3_value object.*
19e51 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19e52 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c   void sqlite3Val
19e53 75 65 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69  ueSetStr(.  sqli
19e54 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20  te3_value *v,   
19e55 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65    /* Value to be
19e56 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c   set */.  int n,
19e57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e58 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72  /* Length of str
19e59 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74  ing z */.  const
19e5a 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20   void *z,       
19e5b 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
19e5c 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  new string */.  
19e5d 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20  u8 enc,         
19e5e 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e        /* Encodin
19e5f 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f  g to use */.  vo
19e60 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
19e61 29 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f  )   /* Destructo
19e62 72 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67  r for the string
19e63 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29   */.){.  if( v )
19e64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19e65 65 74 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20  etStr((Mem *)v, 
19e66 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29  z, n, enc, xDel)
19e67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
19e68 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  an sqlite3_value
19e69 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54   object.*/.SQLIT
19e6a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19e6b 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
19e6c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76  sqlite3_value *v
19e6d 29 7b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65  ){.  if( !v ) re
19e6e 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56  turn;.  sqlite3V
19e6f 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d  dbeMemRelease((M
19e70 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74  em *)v);.  sqlit
19e71 65 33 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29  e3DbFree(((Mem*)
19e72 76 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f  v)->db, v);.}../
19e73 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
19e74 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
19e75 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  in the sqlite3_v
19e76 61 6c 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75  alue object assu
19e77 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20  ming.** that it 
19e78 75 73 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e  uses the encodin
19e79 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54  g "enc".*/.SQLIT
19e7a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19e7b 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
19e7c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
19e7d 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20  Val, u8 enc){.  
19e7e 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70  Mem *p = (Mem*)p
19e7f 56 61 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66  Val;.  if( (p->f
19e80 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29  lags & MEM_Blob)
19e81 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61  !=0 || sqlite3Va
19e82 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e  lueText(pVal, en
19e83 63 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  c) ){.    if( p-
19e84 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72  >flags & MEM_Zer
19e85 6f 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  o ){.      retur
19e86 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e 75 2e 6e 5a  n p->n + p->u.nZ
19e87 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ero;.    }else{.
19e88 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e        return p->
19e89 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  n;.    }.  }.  r
19e8a 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a  eturn 0;.}../***
19e8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
19e8c 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a  of vdbemem.c ***
19e8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
19e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
19e91 6e 20 66 69 6c 65 20 76 64 62 65 61 75 78 2e 63  n file vdbeaux.c
19e92 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
19e93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
19e95 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62 65 72  * 2003 September
19e96 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   6.**.** The aut
19e97 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
19e98 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
19e99 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
19e9a 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
19e9b 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
19e9c 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
19e9d 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
19e9e 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
19e9f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
19ea0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
19ea1 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
19ea2 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
19ea3 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
19ea4 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
19ea5 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
19ea6 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
19ea7 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
19ea8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ea9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eac 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
19ead 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
19eae 64 20 66 6f 72 20 63 72 65 61 74 69 6e 67 2c 20  d for creating, 
19eaf 64 65 73 74 72 6f 79 69 6e 67 2c 20 61 6e 64 20  destroying, and 
19eb0 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a 20 61 20  populating.** a 
19eb1 56 44 42 45 20 28 6f 72 20 61 6e 20 22 73 71 6c  VDBE (or an "sql
19eb2 69 74 65 33 5f 73 74 6d 74 22 20 61 73 20 69 74  ite3_stmt" as it
19eb3 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 74 68 65   is known to the
19eb4 20 6f 75 74 73 69 64 65 20 77 6f 72 6c 64 2e 29   outside world.)
19eb5 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f 20 76 65    Prior.** to ve
19eb6 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c  rsion 2.8.7, all
19eb7 20 74 68 69 73 20 63 6f 64 65 20 77 61 73 20 63   this code was c
19eb8 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65  ombined into the
19eb9 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66   vdbe.c source f
19eba 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74  ile..** But that
19ebb 20 66 69 6c 65 20 77 61 73 20 67 65 74 74 69 6e   file was gettin
19ebc 67 20 74 6f 6f 20 62 69 67 20 73 6f 20 74 68 69  g too big so thi
19ebd 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65  s subroutines we
19ebe 72 65 20 73 70 6c 69 74 20 6f 75 74 2e 0a 2a 2f  re split out..*/
19ebf 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64  ..../*.** When d
19ec0 65 62 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64  ebugging the cod
19ec1 65 20 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61  e generator in a
19ec2 20 73 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67   symbolic debugg
19ec3 65 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73  er, one can.** s
19ec4 65 74 20 74 68 65 20 73 71 6c 69 74 65 33 56 64  et the sqlite3Vd
19ec5 62 65 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20  beAddopTrace to 
19ec6 31 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65  1 and all opcode
19ec7 73 20 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65  s will be printe
19ec8 64 0a 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65  d.** as they are
19ec9 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e   added to the in
19eca 73 74 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d  struction stream
19ecb 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
19ecc 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
19ecd 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19ece 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63  te3VdbeAddopTrac
19ecf 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a  e = 0;.#endif...
19ed0 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
19ed1 65 77 20 76 69 72 74 75 61 6c 20 64 61 74 61 62  ew virtual datab
19ed2 61 73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53  ase engine..*/.S
19ed3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
19ed4 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43  be *sqlite3VdbeC
19ed5 72 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64  reate(sqlite3 *d
19ed6 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20  b){.  Vdbe *p;. 
19ed7 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61   p = sqlite3DbMa
19ed8 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
19ed9 65 6f 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69  eof(Vdbe) );.  i
19eda 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
19edb 20 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62   0;.  p->db = db
19edc 3b 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62  ;.  if( db->pVdb
19edd 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64  e ){.    db->pVd
19ede 62 65 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20  be->pPrev = p;. 
19edf 20 7d 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20   }.  p->pNext = 
19ee0 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e  db->pVdbe;.  p->
19ee1 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d  pPrev = 0;.  db-
19ee2 3e 70 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d  >pVdbe = p;.  p-
19ee3 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
19ee4 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75  GIC_INIT;.  retu
19ee5 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
19ee6 65 6d 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20  emember the SQL 
19ee7 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65  string for a pre
19ee8 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e  pared statement.
19ee9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19eea 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
19eeb 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a  dbeSetSql(Vdbe *
19eec 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
19eed 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50  , int n, int isP
19eee 72 65 70 61 72 65 56 32 29 7b 0a 20 20 61 73 73  repareV2){.  ass
19eef 65 72 74 28 20 69 73 50 72 65 70 61 72 65 56 32  ert( isPrepareV2
19ef0 3d 3d 31 20 7c 7c 20 69 73 50 72 65 70 61 72 65  ==1 || isPrepare
19ef1 56 32 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  V2==0 );.  if( p
19ef2 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69  ==0 ) return;.#i
19ef3 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
19ef4 5f 54 52 41 43 45 0a 20 20 69 66 28 20 21 69 73  _TRACE.  if( !is
19ef5 50 72 65 70 61 72 65 56 32 20 29 20 72 65 74 75  PrepareV2 ) retu
19ef6 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73  rn;.#endif.  ass
19ef7 65 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20  ert( p->zSql==0 
19ef8 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73  );.  p->zSql = s
19ef9 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
19efa 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20  p->db, z, n);.  
19efb 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d  p->isPrepareV2 =
19efc 20 28 75 38 29 69 73 50 72 65 70 61 72 65 56 32   (u8)isPrepareV2
19efd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
19efe 6e 20 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69  n the SQL associ
19eff 61 74 65 64 20 77 69 74 68 20 61 20 70 72 65 70  ated with a prep
19f00 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
19f01 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
19f02 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
19f03 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d  _sql(sqlite3_stm
19f04 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62  t *pStmt){.  Vdb
19f05 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
19f06 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 28  Stmt;.  return (
19f07 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3f  p->isPrepareV2 ?
19f08 20 70 2d 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d   p->zSql : 0);.}
19f09 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c  ../*.** Swap all
19f0a 20 63 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e   content between
19f0b 20 74 77 6f 20 56 44 42 45 20 73 74 72 75 63 74   two VDBE struct
19f0c 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ures..*/.SQLITE_
19f0d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19f0e 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62  ite3VdbeSwap(Vdb
19f0f 65 20 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29  e *pA, Vdbe *pB)
19f10 7b 0a 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70  {.  Vdbe tmp, *p
19f11 54 6d 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d  Tmp;.  char *zTm
19f12 70 3b 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a  p;.  tmp = *pA;.
19f13 20 20 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a    *pA = *pB;.  *
19f14 70 42 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70  pB = tmp;.  pTmp
19f15 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20   = pA->pNext;.  
19f16 70 41 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e  pA->pNext = pB->
19f17 70 4e 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65  pNext;.  pB->pNe
19f18 78 74 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d  xt = pTmp;.  pTm
19f19 70 20 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20  p = pA->pPrev;. 
19f1a 20 70 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d   pA->pPrev = pB-
19f1b 3e 70 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50  >pPrev;.  pB->pP
19f1c 72 65 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54  rev = pTmp;.  zT
19f1d 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20  mp = pA->zSql;. 
19f1e 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e   pA->zSql = pB->
19f1f 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c  zSql;.  pB->zSql
19f20 20 3d 20 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69   = zTmp;.  pB->i
19f21 73 50 72 65 70 61 72 65 56 32 20 3d 20 70 41 2d  sPrepareV2 = pA-
19f22 3e 69 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a  >isPrepareV2;.}.
19f23 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
19f24 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  EBUG./*.** Turn 
19f25 74 72 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66  tracing on or of
19f26 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  f.*/.SQLITE_PRIV
19f27 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
19f28 56 64 62 65 54 72 61 63 65 28 56 64 62 65 20 2a  VdbeTrace(Vdbe *
19f29 70 2c 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b  p, FILE *trace){
19f2a 0a 20 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72  .  p->trace = tr
19f2b 61 63 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ace;.}.#endif../
19f2c 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20  *.** Resize the 
19f2d 56 64 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73  Vdbe.aOp array s
19f2e 6f 20 74 68 61 74 20 69 74 20 69 73 20 61 74 20  o that it is at 
19f2f 6c 65 61 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72  least one op lar
19f30 67 65 72 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20  ger than .** it 
19f31 77 61 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  was..**.** If an
19f32 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65   out-of-memory e
19f33 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
19f34 65 20 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61  e resizing the a
19f35 72 72 61 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20  rray, return.** 
19f36 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e  SQLITE_NOMEM. In
19f37 20 74 68 69 73 20 63 61 73 65 20 56 64 62 65 2e   this case Vdbe.
19f38 61 4f 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70  aOp and Vdbe.nOp
19f39 41 6c 6c 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a  Alloc remain .**
19f3a 20 75 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73   unchanged (this
19f3b 20 69 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20   is so that any 
19f3c 6f 70 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20  opcodes already 
19f3d 61 6c 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65  allocated can be
19f3e 20 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64   .** correctly d
19f3f 65 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67  eallocated along
19f40 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 6f   with the rest o
19f41 66 20 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a  f the Vdbe)..*/.
19f42 73 74 61 74 69 63 20 69 6e 74 20 67 72 6f 77 4f  static int growO
19f43 70 41 72 72 61 79 28 56 64 62 65 20 2a 70 29 7b  pArray(Vdbe *p){
19f44 0a 20 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b  .  VdbeOp *pNew;
19f45 0a 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70  .  int nNew = (p
19f46 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e  ->nOpAlloc ? p->
19f47 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e  nOpAlloc*2 : (in
19f48 74 29 28 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f  t)(1024/sizeof(O
19f49 70 29 29 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73  p)));.  pNew = s
19f4a 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
19f4b 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e  p->db, p->aOp, n
19f4c 4e 65 77 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b  New*sizeof(Op));
19f4d 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20  .  if( pNew ){. 
19f4e 20 20 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d     p->nOpAlloc =
19f4f 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
19f50 53 69 7a 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77  Size(p->db, pNew
19f51 29 2f 73 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20  )/sizeof(Op);.  
19f52 20 20 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b    p->aOp = pNew;
19f53 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70  .  }.  return (p
19f54 4e 65 77 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  New ? SQLITE_OK 
19f55 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b  : SQLITE_NOMEM);
19f56 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
19f57 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  new instruction 
19f58 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69  to the list of i
19f59 6e 73 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72  nstructions curr
19f5a 65 6e 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44  ent in the.** VD
19f5b 42 45 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  BE.  Return the 
19f5c 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e  address of the n
19f5d 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ew instruction..
19f5e 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73  **.** Parameters
19f5f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20  :.**.**    p    
19f60 20 20 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74             Point
19f61 65 72 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a  er to the VDBE.*
19f62 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20  *.**    op      
19f63 20 20 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f          The opco
19f64 64 65 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74  de for this inst
19f65 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20  ruction.**.**   
19f66 20 70 31 2c 20 70 32 2c 20 70 33 20 20 20 20 20   p1, p2, p3     
19f67 20 4f 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20   Operands.**.** 
19f68 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 56  Use the sqlite3V
19f69 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
19f6a 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69  ) function to fi
19f6b 78 20 61 6e 20 61 64 64 72 65 73 73 20 61 6e 64  x an address and
19f6c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56  .** the sqlite3V
19f6d 64 62 65 43 68 61 6e 67 65 50 34 28 29 20 66 75  dbeChangeP4() fu
19f6e 6e 63 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65  nction to change
19f6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
19f70 65 20 50 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e  e P4.** operand.
19f71 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19f72 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19f73 62 65 41 64 64 4f 70 33 28 56 64 62 65 20 2a 70  beAddOp3(Vdbe *p
19f74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31  , int op, int p1
19f75 2c 20 69 6e 74 20 70 32 2c 20 69 6e 74 20 70 33  , int p2, int p3
19f76 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64  ){.  int i;.  Vd
19f77 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20  beOp *pOp;..  i 
19f78 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65  = p->nOp;.  asse
19f79 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44  rt( p->magic==VD
19f7a 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b  BE_MAGIC_INIT );
19f7b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 30 20  .  assert( op>0 
19f7c 26 26 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20  && op<0xff );.  
19f7d 69 66 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c  if( p->nOpAlloc<
19f7e 3d 69 20 29 7b 0a 20 20 20 20 69 66 28 20 67 72  =i ){.    if( gr
19f7f 6f 77 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a  owOpArray(p) ){.
19f80 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
19f81 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e      }.  }.  p->n
19f82 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70  Op++;.  pOp = &p
19f83 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d  ->aOp[i];.  pOp-
19f84 3e 6f 70 63 6f 64 65 20 3d 20 28 75 38 29 6f 70  >opcode = (u8)op
19f85 3b 0a 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b  ;.  pOp->p5 = 0;
19f86 0a 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b  .  pOp->p1 = p1;
19f87 0a 20 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b  .  pOp->p2 = p2;
19f88 0a 20 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b  .  pOp->p3 = p3;
19f89 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30  .  pOp->p4.p = 0
19f8a 3b 0a 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20  ;.  pOp->p4type 
19f8b 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20  = P4_NOTUSED;.  
19f8c 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a  p->expired = 0;.
19f8d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
19f8e 42 55 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d  BUG.  pOp->zComm
19f8f 65 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  ent = 0;.  if( s
19f90 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54  qlite3VdbeAddopT
19f91 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 56 64  race ) sqlite3Vd
19f92 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20  bePrintOp(0, i, 
19f93 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e  &p->aOp[i]);.#en
19f94 64 69 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f  dif.#ifdef VDBE_
19f95 50 52 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63  PROFILE.  pOp->c
19f96 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70  ycles = 0;.  pOp
19f97 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69  ->cnt = 0;.#endi
19f98 66 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a  f.  return i;.}.
19f99 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19f9a 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
19f9b 64 4f 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e  dOp0(Vdbe *p, in
19f9c 74 20 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  t op){.  return 
19f9d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19f9e 33 28 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30  3(p, op, 0, 0, 0
19f9f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
19fa0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
19fa1 64 62 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a  dbeAddOp1(Vdbe *
19fa2 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70  p, int op, int p
19fa3 31 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  1){.  return sql
19fa4 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70  ite3VdbeAddOp3(p
19fa5 2c 20 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b  , op, p1, 0, 0);
19fa6 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
19fa7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19fa8 65 41 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c  eAddOp2(Vdbe *p,
19fa9 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c   int op, int p1,
19faa 20 69 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75   int p2){.  retu
19fab 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  rn sqlite3VdbeAd
19fac 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20  dOp3(p, op, p1, 
19fad 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  p2, 0);.}.../*.*
19fae 2a 20 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20  * Add an opcode 
19faf 74 68 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68  that includes th
19fb0 65 20 70 34 20 76 61 6c 75 65 20 61 73 20 61 20  e p4 value as a 
19fb1 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
19fb2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19fb3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
19fb4 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20  (.  Vdbe *p,    
19fb5 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74          /* Add t
19fb6 68 65 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69  he opcode to thi
19fb7 73 20 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  s VM */.  int op
19fb8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
19fb9 20 54 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20   The new opcode 
19fba 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20  */.  int p1,    
19fbb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
19fbc 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  P1 operand */.  
19fbd 69 6e 74 20 70 32 2c 20 20 20 20 20 20 20 20 20  int p2,         
19fbe 20 20 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70      /* The P2 op
19fbf 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70  erand */.  int p
19fc0 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  3,             /
19fc1 2a 20 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64  * The P3 operand
19fc2 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
19fc3 20 2a 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65   *zP4,    /* The
19fc4 20 50 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20   P4 operand */. 
19fc5 20 69 6e 74 20 70 34 74 79 70 65 20 20 20 20 20   int p4type     
19fc6 20 20 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61       /* P4 opera
19fc7 6e 64 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20  nd type */.){.  
19fc8 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74  int addr = sqlit
19fc9 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
19fca 6f 70 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b  op, p1, p2, p3);
19fcb 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
19fcc 61 6e 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20  angeP4(p, addr, 
19fcd 7a 50 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20  zP4, p4type);.  
19fce 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a  return addr;.}..
19fcf 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63  /*.** Add an opc
19fd0 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65  ode that include
19fd1 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61  s the p4 value a
19fd2 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f  s an integer..*/
19fd3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19fd4 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
19fd5 64 64 4f 70 34 49 6e 74 28 0a 20 20 56 64 62 65  ddOp4Int(.  Vdbe
19fd6 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
19fd7 2f 2a 20 41 64 64 20 74 68 65 20 6f 70 63 6f 64  /* Add the opcod
19fd8 65 20 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a  e to this VM */.
19fd9 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
19fda 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77        /* The new
19fdb 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74   opcode */.  int
19fdc 20 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20   p1,            
19fdd 20 2f 2a 20 54 68 65 20 50 31 20 6f 70 65 72 61   /* The P1 opera
19fde 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32 2c 20  nd */.  int p2, 
19fdf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
19fe0 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 2a 2f  he P2 operand */
19fe1 0a 20 20 69 6e 74 20 70 33 2c 20 20 20 20 20 20  .  int p3,      
19fe2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33         /* The P3
19fe3 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
19fe4 74 20 70 34 20 20 20 20 20 20 20 20 20 20 20 20  t p4            
19fe5 20 20 2f 2a 20 54 68 65 20 50 34 20 6f 70 65 72    /* The P4 oper
19fe6 61 6e 64 20 61 73 20 61 6e 20 69 6e 74 65 67 65  and as an intege
19fe7 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64  r */.){.  int ad
19fe8 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
19fe9 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31  AddOp3(p, op, p1
19fea 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 6c  , p2, p3);.  sql
19feb 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
19fec 28 70 2c 20 61 64 64 72 2c 20 53 51 4c 49 54 45  (p, addr, SQLITE
19fed 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 70 34 29 2c  _INT_TO_PTR(p4),
19fee 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 72 65   P4_INT32);.  re
19fef 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a  turn addr;.}../*
19ff0 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
19ff1 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20   symbolic label 
19ff2 66 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69  for an instructi
19ff3 6f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20  on that has yet 
19ff4 74 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20  to be.** coded. 
19ff5 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61   The symbolic la
19ff6 62 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75  bel is really ju
19ff7 73 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75  st a negative nu
19ff8 6d 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61  mber.  The.** la
19ff9 62 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20  bel can be used 
19ffa 61 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20  as the P2 value 
19ffb 6f 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e  of an operation.
19ffc 20 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a    Later, when.**
19ffd 20 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65   the label is re
19ffe 73 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63  solved to a spec
19fff 69 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68  ific address, th
1a000 65 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e  e VDBE will scan
1a001 0a 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20  .** through its 
1a002 6f 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61  operation list a
1a003 6e 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61  nd change all va
1a004 6c 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68  lues of P2 which
1a005 20 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61   match.** the la
1a006 62 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73  bel into the res
1a007 6f 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a  olved address..*
1a008 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e  *.** The VDBE kn
1a009 6f 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61  ows that a P2 va
1a00a 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62  lue is a label b
1a00b 65 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72  ecause labels ar
1a00c 65 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61  e.** always nega
1a00d 74 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75  tive and P2 valu
1a00e 65 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74  es are suppose t
1a00f 6f 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  o be non-negativ
1a010 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e  e..** Hence, a n
1a011 65 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65  egative P2 value
1a012 20 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74   is a label that
1a013 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72   has yet to be r
1a014 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a  esolved..**.** Z
1a015 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20  ero is returned 
1a016 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
1a017 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
1a018 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1a019 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
1a01a 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
1a01b 69 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62  i;.  i = p->nLab
1a01c 65 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20  el++;.  assert( 
1a01d 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
1a01e 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
1a01f 66 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41  f( i>=p->nLabelA
1a020 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20  lloc ){.    int 
1a021 6e 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c  n = p->nLabelAll
1a022 6f 63 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d  oc*2 + 5;.    p-
1a023 3e 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65  >aLabel = sqlite
1a024 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
1a025 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65  (p->db, p->aLabe
1a026 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
1a027 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a028 20 20 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65            n*size
1a029 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29  of(p->aLabel[0])
1a02a 29 3b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c  );.    p->nLabel
1a02b 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  Alloc = sqlite3D
1a02c 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64  bMallocSize(p->d
1a02d 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69  b, p->aLabel)/si
1a02e 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30  zeof(p->aLabel[0
1a02f 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ]);.  }.  if( p-
1a030 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70  >aLabel ){.    p
1a031 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31  ->aLabel[i] = -1
1a032 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d  ;.  }.  return -
1a033 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  1-i;.}../*.** Re
1a034 73 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20  solve label "x" 
1a035 74 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73  to be the addres
1a036 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e  s of the next in
1a037 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20  struction to.** 
1a038 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68  be inserted.  Th
1a039 65 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20  e parameter "x" 
1a03a 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f  must have been o
1a03b 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
1a03c 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
1a03d 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
1a03e 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  abel()..*/.SQLIT
1a03f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a040 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
1a041 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20  eLabel(Vdbe *p, 
1a042 69 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20  int x){.  int j 
1a043 3d 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74  = -1-x;.  assert
1a044 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
1a045 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
1a046 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26   assert( j>=0 &&
1a047 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a   j<p->nLabel );.
1a048 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20    if( p->aLabel 
1a049 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c  ){.    p->aLabel
1a04a 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20  [j] = p->nOp;.  
1a04b 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  }.}..#ifdef SQLI
1a04c 54 45 5f 44 45 42 55 47 20 2f 2a 20 73 71 6c 69  TE_DEBUG /* sqli
1a04d 74 65 33 41 73 73 65 72 74 4d 61 79 41 62 6f 72  te3AssertMayAbor
1a04e 74 28 29 20 6c 6f 67 69 63 20 2a 2f 0a 0a 2f 2a  t() logic */../*
1a04f 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1a050 67 20 74 79 70 65 20 61 6e 64 20 66 75 6e 63 74  g type and funct
1a051 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20  ion are used to 
1a052 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
1a053 61 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 69  all opcodes.** i
1a054 6e 20 61 20 56 64 62 65 20 6d 61 69 6e 20 70 72  n a Vdbe main pr
1a055 6f 67 72 61 6d 20 61 6e 64 20 65 61 63 68 20 6f  ogram and each o
1a056 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61  f the sub-progra
1a057 6d 73 20 28 74 72 69 67 67 65 72 73 29 20 69 74  ms (triggers) it
1a058 20 6d 61 79 20 0a 2a 2a 20 69 6e 76 6f 6b 65 20   may .** invoke 
1a059 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69  directly or indi
1a05a 72 65 63 74 6c 79 2e 20 49 74 20 73 68 6f 75 6c  rectly. It shoul
1a05b 64 20 62 65 20 75 73 65 64 20 61 73 20 66 6f 6c  d be used as fol
1a05c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70  lows:.**.**   Op
1a05d 20 2a 70 4f 70 3b 0a 2a 2a 20 20 20 56 64 62 65   *pOp;.**   Vdbe
1a05e 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a 2a 2a  OpIter sIter;.**
1a05f 0a 2a 2a 20 20 20 6d 65 6d 73 65 74 28 26 73 49  .**   memset(&sI
1a060 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ter, 0, sizeof(s
1a061 49 74 65 72 29 29 3b 0a 2a 2a 20 20 20 73 49 74  Iter));.**   sIt
1a062 65 72 2e 76 20 3d 20 76 3b 20 20 20 20 20 20 20  er.v = v;       
1a063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a064 20 20 20 20 20 2f 2f 20 76 20 69 73 20 6f 66 20       // v is of 
1a065 74 79 70 65 20 56 64 62 65 2a 20 0a 2a 2a 20 20  type Vdbe* .**  
1a066 20 77 68 69 6c 65 28 20 28 70 4f 70 20 3d 20 6f   while( (pOp = o
1a067 70 49 74 65 72 4e 65 78 74 28 26 73 49 74 65 72  pIterNext(&sIter
1a068 29 29 20 29 7b 0a 2a 2a 20 20 20 20 20 2f 2f 20  )) ){.**     // 
1a069 44 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74  Do something wit
1a06a 68 20 70 4f 70 0a 2a 2a 20 20 20 7d 0a 2a 2a 20  h pOp.**   }.** 
1a06b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1a06c 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53  v->db, sIter.apS
1a06d 75 62 29 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79 70 65  ub);.** .*/.type
1a06e 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 4f  def struct VdbeO
1a06f 70 49 74 65 72 20 56 64 62 65 4f 70 49 74 65 72  pIter VdbeOpIter
1a070 3b 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70 49  ;.struct VdbeOpI
1a071 74 65 72 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b  ter {.  Vdbe *v;
1a072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a073 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20 69 74     /* Vdbe to it
1a074 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 68  erate through th
1a075 65 20 6f 70 63 6f 64 65 73 20 6f 66 20 2a 2f 0a  e opcodes of */.
1a076 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 61    SubProgram **a
1a077 70 53 75 62 3b 20 20 20 20 20 20 20 20 2f 2a 20  pSub;        /* 
1a078 41 72 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67  Array of subprog
1a079 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  rams */.  int nS
1a07a 75 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ub;             
1a07b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1a07c 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 70 53  f entries in apS
1a07d 75 62 20 2a 2f 0a 20 20 69 6e 74 20 69 41 64 64  ub */.  int iAdd
1a07e 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1a07f 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
1a080 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1a081 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20  n to return */. 
1a082 20 69 6e 74 20 69 53 75 62 3b 20 20 20 20 20 20   int iSub;      
1a083 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30              /* 0
1a084 20 3d 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2c   = main program,
1a085 20 31 20 3d 20 66 69 72 73 74 20 73 75 62 2d 70   1 = first sub-p
1a086 72 6f 67 72 61 6d 20 65 74 63 2e 20 2a 2f 0a 7d  rogram etc. */.}
1a087 3b 0a 73 74 61 74 69 63 20 4f 70 20 2a 6f 70 49  ;.static Op *opI
1a088 74 65 72 4e 65 78 74 28 56 64 62 65 4f 70 49 74  terNext(VdbeOpIt
1a089 65 72 20 2a 70 29 7b 0a 20 20 56 64 62 65 20 2a  er *p){.  Vdbe *
1a08a 76 20 3d 20 70 2d 3e 76 3b 0a 20 20 4f 70 20 2a  v = p->v;.  Op *
1a08b 70 52 65 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a  pRet = 0;.  Op *
1a08c 61 4f 70 3b 0a 20 20 69 6e 74 20 6e 4f 70 3b 0a  aOp;.  int nOp;.
1a08d 0a 20 20 69 66 28 20 70 2d 3e 69 53 75 62 3c 3d  .  if( p->iSub<=
1a08e 70 2d 3e 6e 53 75 62 20 29 7b 0a 0a 20 20 20 20  p->nSub ){..    
1a08f 69 66 28 20 70 2d 3e 69 53 75 62 3d 3d 30 20 29  if( p->iSub==0 )
1a090 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 76 2d  {.      aOp = v-
1a091 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20  >aOp;.      nOp 
1a092 3d 20 76 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 65  = v->nOp;.    }e
1a093 6c 73 65 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d  lse{.      aOp =
1a094 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 53 75   p->apSub[p->iSu
1a095 62 2d 31 5d 2d 3e 61 4f 70 3b 0a 20 20 20 20 20  b-1]->aOp;.     
1a096 20 6e 4f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b   nOp = p->apSub[
1a097 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 6e 4f 70 3b  p->iSub-1]->nOp;
1a098 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
1a099 74 28 20 70 2d 3e 69 41 64 64 72 3c 6e 4f 70 20  t( p->iAddr<nOp 
1a09a 29 3b 0a 0a 20 20 20 20 70 52 65 74 20 3d 20 26  );..    pRet = &
1a09b 61 4f 70 5b 70 2d 3e 69 41 64 64 72 5d 3b 0a 20  aOp[p->iAddr];. 
1a09c 20 20 20 70 2d 3e 69 41 64 64 72 2b 2b 3b 0a 20     p->iAddr++;. 
1a09d 20 20 20 69 66 28 20 70 2d 3e 69 41 64 64 72 3d     if( p->iAddr=
1a09e 3d 6e 4f 70 20 29 7b 0a 20 20 20 20 20 20 70 2d  =nOp ){.      p-
1a09f 3e 69 53 75 62 2b 2b 3b 0a 20 20 20 20 20 20 70  >iSub++;.      p
1a0a0 2d 3e 69 41 64 64 72 20 3d 20 30 3b 0a 20 20 20  ->iAddr = 0;.   
1a0a1 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 70 52   }.  .    if( pR
1a0a2 65 74 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53  et->p4type==P4_S
1a0a3 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20  UBPROGRAM ){.   
1a0a4 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28     int nByte = (
1a0a5 70 2d 3e 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f  p->nSub+1)*sizeo
1a0a6 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a  f(SubProgram*);.
1a0a7 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
1a0a8 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d     for(j=0; j<p-
1a0a9 3e 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20  >nSub; j++){.   
1a0aa 20 20 20 20 20 69 66 28 20 70 2d 3e 61 70 53 75       if( p->apSu
1a0ab 62 5b 6a 5d 3d 3d 70 52 65 74 2d 3e 70 34 2e 70  b[j]==pRet->p4.p
1a0ac 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b  Program ) break;
1a0ad 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1a0ae 66 28 20 6a 3d 3d 70 2d 3e 6e 53 75 62 20 29 7b  f( j==p->nSub ){
1a0af 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70 53 75  .        p->apSu
1a0b0 62 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  b = sqlite3DbRea
1a0b1 6c 6c 6f 63 4f 72 46 72 65 65 28 76 2d 3e 64 62  llocOrFree(v->db
1a0b2 2c 20 70 2d 3e 61 70 53 75 62 2c 20 6e 42 79 74  , p->apSub, nByt
1a0b3 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
1a0b4 21 70 2d 3e 61 70 53 75 62 20 29 7b 0a 20 20 20  !p->apSub ){.   
1a0b5 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 30 3b         pRet = 0;
1a0b6 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1a0b7 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 70 53            p->apS
1a0b8 75 62 5b 70 2d 3e 6e 53 75 62 2b 2b 5d 20 3d 20  ub[p->nSub++] = 
1a0b9 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67 72 61  pRet->p4.pProgra
1a0ba 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  m;.        }.   
1a0bb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
1a0bc 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d    return pRet;.}
1a0bd 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 69 66  ../*.** Check if
1a0be 20 74 68 65 20 70 72 6f 67 72 61 6d 20 73 74 6f   the program sto
1a0bf 72 65 64 20 69 6e 20 74 68 65 20 56 4d 20 61 73  red in the VM as
1a0c0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 50  sociated with pP
1a0c1 61 72 73 65 20 6d 61 79 0a 2a 2a 20 74 68 72 6f  arse may.** thro
1a0c2 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70  w an ABORT excep
1a0c3 74 69 6f 6e 20 28 63 61 75 73 69 6e 67 20 74 68  tion (causing th
1a0c4 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 75 74  e statement, but
1a0c5 20 6e 6f 74 20 65 6e 74 69 72 65 20 74 72 61 6e   not entire tran
1a0c6 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65  saction.** to be
1a0c7 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2e 20 54   rolled back). T
1a0c8 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73  his condition is
1a0c9 20 74 72 75 65 20 69 66 20 74 68 65 20 6d 61 69   true if the mai
1a0ca 6e 20 70 72 6f 67 72 61 6d 20 6f 72 20 61 6e 79  n program or any
1a0cb 0a 2a 2a 20 73 75 62 2d 70 72 6f 67 72 61 6d 73  .** sub-programs
1a0cc 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 6f 66   contains any of
1a0cd 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
1a0ce 2a 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61  **.**   *  OP_Ha
1a0cf 6c 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54  lt with P1=SQLIT
1a0d0 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64  E_CONSTRAINT and
1a0d1 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a   P2=OE_Abort..**
1a0d2 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 49 66 4e     *  OP_HaltIfN
1a0d3 75 6c 6c 20 77 69 74 68 20 50 31 3d 53 51 4c 49  ull with P1=SQLI
1a0d4 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e  TE_CONSTRAINT an
1a0d5 64 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a  d P2=OE_Abort..*
1a0d6 2a 20 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f  *   *  OP_Destro
1a0d7 79 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70  y.**   *  OP_VUp
1a0d8 64 61 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f  date.**   *  OP_
1a0d9 56 52 65 6e 61 6d 65 0a 2a 2a 20 20 20 2a 20 20  VRename.**   *  
1a0da 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 77 69 74  OP_FkCounter wit
1a0db 68 20 50 32 3d 3d 30 20 28 69 6d 6d 65 64 69 61  h P2==0 (immedia
1a0dc 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  te foreign key c
1a0dd 6f 6e 73 74 72 61 69 6e 74 29 0a 2a 2a 0a 2a 2a  onstraint).**.**
1a0de 20 54 68 65 6e 20 63 68 65 63 6b 20 74 68 61 74   Then check that
1a0df 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61   the value of Pa
1a0e0 72 73 65 2e 6d 61 79 41 62 6f 72 74 20 69 73 20  rse.mayAbort is 
1a0e1 74 72 75 65 20 69 66 20 61 6e 0a 2a 2a 20 41 42  true if an.** AB
1a0e2 4f 52 54 20 6d 61 79 20 62 65 20 74 68 72 6f 77  ORT may be throw
1a0e3 6e 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65  n, or false othe
1a0e4 72 77 69 73 65 2e 20 52 65 74 75 72 6e 20 74 72  rwise. Return tr
1a0e5 75 65 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ue if it does.**
1a0e6 20 6d 61 74 63 68 2c 20 6f 72 20 66 61 6c 73 65   match, or false
1a0e7 20 6f 74 68 65 72 77 69 73 65 2e 20 54 68 69 73   otherwise. This
1a0e8 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 74   function is int
1a0e9 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64  ended to be used
1a0ea 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61   as.** part of a
1a0eb 6e 20 61 73 73 65 72 74 20 73 74 61 74 65 6d 65  n assert stateme
1a0ec 6e 74 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c  nt in the compil
1a0ed 65 72 2e 20 53 69 6d 69 6c 61 72 20 74 6f 3a 0a  er. Similar to:.
1a0ee 2a 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20  **.**   assert( 
1a0ef 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 72  sqlite3VdbeAsser
1a0f0 74 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65  tMayAbort(pParse
1a0f1 2d 3e 70 56 64 62 65 2c 20 70 50 61 72 73 65 2d  ->pVdbe, pParse-
1a0f2 3e 6d 61 79 41 62 6f 72 74 29 20 29 3b 0a 2a 2f  >mayAbort) );.*/
1a0f3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a0f4 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
1a0f5 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 56 64  ssertMayAbort(Vd
1a0f6 62 65 20 2a 76 2c 20 69 6e 74 20 6d 61 79 41 62  be *v, int mayAb
1a0f7 6f 72 74 29 7b 0a 20 20 69 6e 74 20 68 61 73 41  ort){.  int hasA
1a0f8 62 6f 72 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a  bort = 0;.  Op *
1a0f9 70 4f 70 3b 0a 20 20 56 64 62 65 4f 70 49 74 65  pOp;.  VdbeOpIte
1a0fa 72 20 73 49 74 65 72 3b 0a 20 20 6d 65 6d 73 65  r sIter;.  memse
1a0fb 74 28 26 73 49 74 65 72 2c 20 30 2c 20 73 69 7a  t(&sIter, 0, siz
1a0fc 65 6f 66 28 73 49 74 65 72 29 29 3b 0a 20 20 73  eof(sIter));.  s
1a0fd 49 74 65 72 2e 76 20 3d 20 76 3b 0a 0a 20 20 77  Iter.v = v;..  w
1a0fe 68 69 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49  hile( (pOp = opI
1a0ff 74 65 72 4e 65 78 74 28 26 73 49 74 65 72 29 29  terNext(&sIter))
1a100 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f  !=0 ){.    int o
1a101 70 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63  pcode = pOp->opc
1a102 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 6f 70 63  ode;.    if( opc
1a103 6f 64 65 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20  ode==OP_Destroy 
1a104 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55  || opcode==OP_VU
1a105 70 64 61 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d  pdate || opcode=
1a106 3d 4f 50 5f 56 52 65 6e 61 6d 65 20 0a 23 69 66  =OP_VRename .#if
1a107 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1a108 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20  _FOREIGN_KEY.   
1a109 20 20 7c 7c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50    || (opcode==OP
1a10a 5f 46 6b 43 6f 75 6e 74 65 72 20 26 26 20 70 4f  _FkCounter && pO
1a10b 70 2d 3e 70 31 3d 3d 30 20 26 26 20 70 4f 70 2d  p->p1==0 && pOp-
1a10c 3e 70 32 3d 3d 31 29 20 0a 23 65 6e 64 69 66 0a  >p2==1) .#endif.
1a10d 20 20 20 20 20 7c 7c 20 28 28 6f 70 63 6f 64 65       || ((opcode
1a10e 3d 3d 4f 50 5f 48 61 6c 74 20 7c 7c 20 6f 70 63  ==OP_Halt || opc
1a10f 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 49 66 4e 75  ode==OP_HaltIfNu
1a110 6c 6c 29 20 0a 20 20 20 20 20 20 26 26 20 28 70  ll) .      && (p
1a111 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43  Op->p1==SQLITE_C
1a112 4f 4e 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70  ONSTRAINT && pOp
1a113 2d 3e 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 29 29  ->p2==OE_Abort))
1a114 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 68 61  .    ){.      ha
1a115 73 41 62 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20  sAbort = 1;.    
1a116 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1a117 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72   }.  sqlite3DbFr
1a118 65 65 28 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e  ee(v->db, sIter.
1a119 61 70 53 75 62 29 3b 0a 0a 20 20 2f 2a 20 52 65  apSub);..  /* Re
1a11a 74 75 72 6e 20 74 72 75 65 20 69 66 20 68 61 73  turn true if has
1a11b 41 62 6f 72 74 3d 3d 6d 61 79 41 62 6f 72 74 2e  Abort==mayAbort.
1a11c 20 4f 72 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20   Or if a malloc 
1a11d 66 61 69 6c 75 72 65 20 6f 63 63 75 72 65 64 2e  failure occured.
1a11e 0a 20 20 2a 2a 20 49 66 20 6d 61 6c 6c 6f 63 20  .  ** If malloc 
1a11f 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 74 68 65  failed, then the
1a120 20 77 68 69 6c 65 28 29 20 6c 6f 6f 70 20 61 62   while() loop ab
1a121 6f 76 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65  ove may not have
1a122 20 69 74 65 72 61 74 65 64 0a 20 20 2a 2a 20 74   iterated.  ** t
1a123 68 72 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64  hrough all opcod
1a124 65 73 20 61 6e 64 20 68 61 73 41 62 6f 72 74 20  es and hasAbort 
1a125 6d 61 79 20 62 65 20 73 65 74 20 69 6e 63 6f 72  may be set incor
1a126 72 65 63 74 6c 79 2e 20 52 65 74 75 72 6e 0a 20  rectly. Return. 
1a127 20 2a 2a 20 74 72 75 65 20 66 6f 72 20 74 68 69   ** true for thi
1a128 73 20 63 61 73 65 20 74 6f 20 70 72 65 76 65 6e  s case to preven
1a129 74 20 74 68 65 20 61 73 73 65 72 74 28 29 20 69  t the assert() i
1a12a 6e 20 74 68 65 20 63 61 6c 6c 65 72 73 20 66 72  n the callers fr
1a12b 61 6d 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 66 61  ame.  ** from fa
1a12c 69 6c 69 6e 67 2e 20 20 2a 2f 0a 20 20 72 65 74  iling.  */.  ret
1a12d 75 72 6e 20 28 20 76 2d 3e 64 62 2d 3e 6d 61 6c  urn ( v->db->mal
1a12e 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 68 61 73  locFailed || has
1a12f 41 62 6f 72 74 3d 3d 6d 61 79 41 62 6f 72 74 20  Abort==mayAbort 
1a130 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
1a131 51 4c 49 54 45 5f 44 45 42 55 47 20 2d 20 74 68  QLITE_DEBUG - th
1a132 65 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d  e sqlite3AssertM
1a133 61 79 41 62 6f 72 74 28 29 20 66 75 6e 63 74 69  ayAbort() functi
1a134 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f  on */../*.** Loo
1a135 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 70 72  p through the pr
1a136 6f 67 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f  ogram looking fo
1a137 72 20 50 32 20 76 61 6c 75 65 73 20 74 68 61 74  r P2 values that
1a138 20 61 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a   are negative.**
1a139 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63   on jump instruc
1a13a 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63  tions.  Each suc
1a13b 68 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62  h value is a lab
1a13c 65 6c 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65  el.  Resolve the
1a13d 0a 2a 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74  .** label by set
1a13e 74 69 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75  ting the P2 valu
1a13f 65 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74  e to its correct
1a140 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e   non-zero value.
1a141 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1a142 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  ine is called on
1a143 63 65 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63  ce after all opc
1a144 6f 64 65 73 20 68 61 76 65 20 62 65 65 6e 20 69  odes have been i
1a145 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56  nserted..**.** V
1a146 61 72 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e  ariable *pMaxFun
1a147 63 41 72 67 73 20 69 73 20 73 65 74 20 74 6f 20  cArgs is set to 
1a148 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75  the maximum valu
1a149 65 20 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75  e of any P2 argu
1a14a 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f  ment .** to an O
1a14b 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41  P_Function, OP_A
1a14c 67 67 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69  ggStep or OP_VFi
1a14d 6c 74 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69  lter opcode. Thi
1a14e 73 20 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a  s is used by .**
1a14f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1a150 52 65 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20  Ready() to size 
1a151 74 68 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d  the Vdbe.apArg[]
1a152 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68   array..**.** Th
1a153 65 20 4f 70 2e 6f 70 66 6c 61 67 73 20 66 69 65  e Op.opflags fie
1a154 6c 64 20 69 73 20 73 65 74 20 6f 6e 20 61 6c 6c  ld is set on all
1a155 20 6f 70 63 6f 64 65 73 2e 0a 2a 2f 0a 73 74 61   opcodes..*/.sta
1a156 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65  tic void resolve
1a157 50 32 56 61 6c 75 65 73 28 56 64 62 65 20 2a 70  P2Values(Vdbe *p
1a158 2c 20 69 6e 74 20 2a 70 4d 61 78 46 75 6e 63 41  , int *pMaxFuncA
1a159 72 67 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  rgs){.  int i;. 
1a15a 20 69 6e 74 20 6e 4d 61 78 41 72 67 73 20 3d 20   int nMaxArgs = 
1a15b 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 3b 0a 20  *pMaxFuncArgs;. 
1a15c 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20   Op *pOp;.  int 
1a15d 2a 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61  *aLabel = p->aLa
1a15e 62 65 6c 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e  bel;.  p->readOn
1a15f 6c 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28 70 4f  ly = 1;.  for(pO
1a160 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e  p=p->aOp, i=p->n
1a161 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c  Op-1; i>=0; i--,
1a162 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20   pOp++){.    u8 
1a163 6f 70 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70  opcode = pOp->op
1a164 63 6f 64 65 3b 0a 0a 20 20 20 20 70 4f 70 2d 3e  code;..    pOp->
1a165 6f 70 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65  opflags = sqlite
1a166 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b  3OpcodeProperty[
1a167 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28  opcode];.    if(
1a168 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63   opcode==OP_Func
1a169 74 69 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d  tion || opcode==
1a16a 4f 50 5f 41 67 67 53 74 65 70 20 29 7b 0a 20 20  OP_AggStep ){.  
1a16b 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 3e      if( pOp->p5>
1a16c 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41  nMaxArgs ) nMaxA
1a16d 72 67 73 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20  rgs = pOp->p5;. 
1a16e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63     }else if( opc
1a16f 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74  ode==OP_Transact
1a170 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d  ion && pOp->p2!=
1a171 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65  0 ){.      p->re
1a172 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e  adOnly = 0;.#ifn
1a173 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a174 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
1a175 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64   }else if( opcod
1a176 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 29 7b  e==OP_VUpdate ){
1a177 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  .      if( pOp->
1a178 70 32 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d  p2>nMaxArgs ) nM
1a179 61 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 32  axArgs = pOp->p2
1a17a 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1a17b 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74  opcode==OP_VFilt
1a17c 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  er ){.      int 
1a17d 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  n;.      assert(
1a17e 20 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33   p->nOp - i >= 3
1a17f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1a180 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65  ( pOp[-1].opcode
1a181 3d 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a  ==OP_Integer );.
1a182 20 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31        n = pOp[-1
1a183 5d 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20  ].p1;.      if( 
1a184 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61  n>nMaxArgs ) nMa
1a185 78 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69  xArgs = n;.#endi
1a186 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  f.    }..    if(
1a187 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26   (pOp->opflags &
1a188 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 21 3d 30 20   OPFLG_JUMP)!=0 
1a189 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a  && pOp->p2<0 ){.
1a18a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d 31        assert( -1
1a18b 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62  -pOp->p2<p->nLab
1a18c 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d  el );.      pOp-
1a18d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d  >p2 = aLabel[-1-
1a18e 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a  pOp->p2];.    }.
1a18f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
1a190 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c  ree(p->db, p->aL
1a191 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62  abel);.  p->aLab
1a192 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78  el = 0;..  *pMax
1a193 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41  FuncArgs = nMaxA
1a194 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rgs;.}../*.** Re
1a195 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 73  turn the address
1a196 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73   of the next ins
1a197 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69  truction to be i
1a198 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  nserted..*/.SQLI
1a199 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1a19a 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
1a19b 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a  tAddr(Vdbe *p){.
1a19c 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
1a19d 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
1a19e 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  NIT );.  return 
1a19f 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  p->nOp;.}../*.**
1a1a0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
1a1a1 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
1a1a2 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66   to the array of
1a1a3 20 6f 70 63 6f 64 65 73 20 61 73 73 6f 63 69 61   opcodes associa
1a1a4 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20  ted with.** the 
1a1a5 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 74  Vdbe passed as t
1a1a6 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
1a1a7 74 2e 20 49 74 20 69 73 20 74 68 65 20 63 61 6c  t. It is the cal
1a1a8 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c  lers responsibil
1a1a9 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 61 6e 67  ity.** to arrang
1a1aa 65 20 66 6f 72 20 74 68 65 20 72 65 74 75 72 6e  e for the return
1a1ab 65 64 20 61 72 72 61 79 20 74 6f 20 62 65 20 65  ed array to be e
1a1ac 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 20  ventually freed 
1a1ad 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 76 64  using the .** vd
1a1ae 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 29 20  beFreeOpArray() 
1a1af 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
1a1b0 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  Before returning
1a1b1 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 74 20 74  , *pnOp is set t
1a1b2 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
1a1b3 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 72  entries in the r
1a1b4 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 72 61 79  eturned.** array
1a1b5 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 41 72  . Also, *pnMaxAr
1a1b6 67 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  g is set to the 
1a1b7 6c 61 72 67 65 72 20 6f 66 20 69 74 73 20 63 75  larger of its cu
1a1b8 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20  rrent value and 
1a1b9 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
1a1ba 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  f entries in the
1a1bb 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72   Vdbe.apArg[] ar
1a1bc 72 61 79 20 72 65 71 75 69 72 65 64 20 74 6f 20  ray required to 
1a1bd 65 78 65 63 75 74 65 20 74 68 65 20 0a 2a 2a 20  execute the .** 
1a1be 72 65 74 75 72 6e 65 64 20 70 72 6f 67 72 61 6d  returned program
1a1bf 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a1c0 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69  ATE VdbeOp *sqli
1a1c1 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72  te3VdbeTakeOpArr
1a1c2 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  ay(Vdbe *p, int 
1a1c3 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d 61  *pnOp, int *pnMa
1a1c4 78 41 72 67 29 7b 0a 20 20 56 64 62 65 4f 70 20  xArg){.  VdbeOp 
1a1c5 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20  *aOp = p->aOp;. 
1a1c6 20 61 73 73 65 72 74 28 20 61 4f 70 20 26 26 20   assert( aOp && 
1a1c7 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  !p->db->mallocFa
1a1c8 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43 68  iled );..  /* Ch
1a1c9 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74 65 33  eck that sqlite3
1a1ca 56 64 62 65 55 73 65 73 42 74 72 65 65 28 29 20  VdbeUsesBtree() 
1a1cb 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 6f  was not called o
1a1cc 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 61  n this VM */.  a
1a1cd 73 73 65 72 74 28 20 70 2d 3e 61 4d 75 74 65 78  ssert( p->aMutex
1a1ce 2e 6e 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 0a 20  .nMutex==0 );.. 
1a1cf 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73   resolveP2Values
1a1d0 28 70 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a 20  (p, pnMaxArg);. 
1a1d1 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b   *pnOp = p->nOp;
1a1d2 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20  .  p->aOp = 0;. 
1a1d3 20 72 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a   return aOp;.}..
1a1d4 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f 6c  /*.** Add a whol
1a1d5 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 74  e list of operat
1a1d6 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 72  ions to the oper
1a1d7 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 65  ation stack.  Re
1a1d8 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 72  turn the.** addr
1a1d9 65 73 73 20 6f 66 20 74 68 65 20 66 69 72 73 74  ess of the first
1a1da 20 6f 70 65 72 61 74 69 6f 6e 20 61 64 64 65 64   operation added
1a1db 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a1dc 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1a1dd 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62  dbeAddOpList(Vdb
1a1de 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 56  e *p, int nOp, V
1a1df 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20  dbeOpList const 
1a1e0 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 64  *aOp){.  int add
1a1e1 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  r;.  assert( p->
1a1e2 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
1a1e3 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20  C_INIT );.  if( 
1a1e4 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70  p->nOp + nOp > p
1a1e5 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72  ->nOpAlloc && gr
1a1e6 6f 77 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a  owOpArray(p) ){.
1a1e7 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1a1e8 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f  }.  addr = p->nO
1a1e9 70 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  p;.  if( ALWAYS(
1a1ea 6e 4f 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 6e  nOp>0) ){.    in
1a1eb 74 20 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c  t i;.    VdbeOpL
1a1ec 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d  ist const *pIn =
1a1ed 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d   aOp;.    for(i=
1a1ee 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70  0; i<nOp; i++, p
1a1ef 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74  In++){.      int
1a1f0 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20   p2 = pIn->p2;. 
1a1f1 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75       VdbeOp *pOu
1a1f2 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64  t = &p->aOp[i+ad
1a1f3 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  dr];.      pOut-
1a1f4 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f  >opcode = pIn->o
1a1f5 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75  pcode;.      pOu
1a1f6 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b  t->p1 = pIn->p1;
1a1f7 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 30 20  .      if( p2<0 
1a1f8 26 26 20 28 73 71 6c 69 74 65 33 4f 70 63 6f 64  && (sqlite3Opcod
1a1f9 65 50 72 6f 70 65 72 74 79 5b 70 4f 75 74 2d 3e  eProperty[pOut->
1a1fa 6f 70 63 6f 64 65 5d 20 26 20 4f 50 46 4c 47 5f  opcode] & OPFLG_
1a1fb 4a 55 4d 50 29 21 3d 30 20 29 7b 0a 20 20 20 20  JUMP)!=0 ){.    
1a1fc 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61      pOut->p2 = a
1a1fd 64 64 72 20 2b 20 41 44 44 52 28 70 32 29 3b 0a  ddr + ADDR(p2);.
1a1fe 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1a1ff 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20       pOut->p2 = 
1a200 70 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p2;.      }.    
1a201 20 20 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e    pOut->p3 = pIn
1a202 2d 3e 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 74  ->p3;.      pOut
1a203 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f  ->p4type = P4_NO
1a204 54 55 53 45 44 3b 0a 20 20 20 20 20 20 70 4f 75  TUSED;.      pOu
1a205 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20  t->p4.p = 0;.   
1a206 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b     pOut->p5 = 0;
1a207 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1a208 45 42 55 47 0a 20 20 20 20 20 20 70 4f 75 74 2d  EBUG.      pOut-
1a209 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20  >zComment = 0;. 
1a20a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1a20b 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 29  VdbeAddopTrace )
1a20c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1a20d 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20  3VdbePrintOp(0, 
1a20e 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b  i+addr, &p->aOp[
1a20f 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 20  i+addr]);.      
1a210 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20  }.#endif.    }. 
1a211 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70     p->nOp += nOp
1a212 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61  ;.  }.  return a
1a213 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  ddr;.}../*.** Ch
1a214 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
1a215 66 20 74 68 65 20 50 31 20 6f 70 65 72 61 6e 64  f the P1 operand
1a216 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20   for a specific 
1a217 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
1a218 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1a219 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61  useful when a la
1a21a 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c  rge program is l
1a21b 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20  oaded from a.** 
1a21c 73 74 61 74 69 63 20 61 72 72 61 79 20 75 73 69  static array usi
1a21d 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  ng sqlite3VdbeAd
1a21e 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77  dOpList but we w
1a21f 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a  ant to make a.**
1a220 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67   few minor chang
1a221 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61  es to the progra
1a222 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  m..*/.SQLITE_PRI
1a223 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a224 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 64  3VdbeChangeP1(Vd
1a225 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c  be *p, int addr,
1a226 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73   int val){.  ass
1a227 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61  ert( p!=0 );.  a
1a228 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29  ssert( addr>=0 )
1a229 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61  ;.  if( p->nOp>a
1a22a 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f  ddr ){.    p->aO
1a22b 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c  p[addr].p1 = val
1a22c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
1a22d 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
1a22e 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e  of the P2 operan
1a22f 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63  d for a specific
1a230 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
1a231 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1a232 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74   useful for sett
1a233 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69  ing a jump desti
1a234 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  nation..*/.SQLIT
1a235 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a236 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1a237 50 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  P2(Vdbe *p, int 
1a238 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a  addr, int val){.
1a239 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
1a23a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72  ;.  assert( addr
1a23b 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  >=0 );.  if( p->
1a23c 6e 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 20  nOp>addr ){.    
1a23d 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20  p->aOp[addr].p2 
1a23e 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  = val;.  }.}../*
1a23f 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76  .** Change the v
1a240 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20 6f  alue of the P3 o
1a241 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65  perand for a spe
1a242 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f  cific instructio
1a243 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1a244 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a245 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56 64  3VdbeChangeP3(Vd
1a246 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c  be *p, int addr,
1a247 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73   int val){.  ass
1a248 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61  ert( p!=0 );.  a
1a249 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29  ssert( addr>=0 )
1a24a 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61  ;.  if( p->nOp>a
1a24b 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f  ddr ){.    p->aO
1a24c 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 76 61 6c  p[addr].p3 = val
1a24d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
1a24e 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
1a24f 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72 61 6e  of the P5 operan
1a250 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72  d for the most r
1a251 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 64  ecently.** added
1a252 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 53   operation..*/.S
1a253 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1a254 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
1a255 61 6e 67 65 50 35 28 56 64 62 65 20 2a 70 2c 20  angeP5(Vdbe *p, 
1a256 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72  u8 val){.  asser
1a257 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28  t( p!=0 );.  if(
1a258 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61   p->aOp ){.    a
1a259 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20  ssert( p->nOp>0 
1a25a 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d  );.    p->aOp[p-
1a25b 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c  >nOp-1].p5 = val
1a25c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
1a25d 68 61 6e 67 65 20 74 68 65 20 50 32 20 6f 70 65  hange the P2 ope
1a25e 72 61 6e 64 20 6f 66 20 69 6e 73 74 72 75 63 74  rand of instruct
1a25f 69 6f 6e 20 61 64 64 72 20 73 6f 20 74 68 61 74  ion addr so that
1a260 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a   it points to.**
1a261 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
1a262 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
1a263 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 64 65 64  tion to be coded
1a264 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a265 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a266 56 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62  VdbeJumpHere(Vdb
1a267 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b  e *p, int addr){
1a268 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
1a269 61 6e 67 65 50 32 28 70 2c 20 61 64 64 72 2c 20  angeP2(p, addr, 
1a26a 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  p->nOp);.}.../*.
1a26b 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  ** If the input 
1a26c 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
1a26d 65 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2c 20  e is ephemeral, 
1a26e 74 68 65 6e 20 66 72 65 65 20 69 74 2e 20 20 49  then free it.  I
1a26f 66 0a 2a 2a 20 74 68 65 20 46 75 6e 63 44 65 66  f.** the FuncDef
1a270 20 69 73 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c   is not ephermal
1a271 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e  , then do nothin
1a272 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  g..*/.static voi
1a273 64 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46  d freeEphemeralF
1a274 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20  unction(sqlite3 
1a275 2a 64 62 2c 20 46 75 6e 63 44 65 66 20 2a 70 44  *db, FuncDef *pD
1a276 65 66 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59  ef){.  if( ALWAY
1a277 53 28 70 44 65 66 29 20 26 26 20 28 70 44 65 66  S(pDef) && (pDef
1a278 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1a279 5f 46 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20  _FUNC_EPHEM)!=0 
1a27a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
1a27b 46 72 65 65 28 64 62 2c 20 70 44 65 66 29 3b 0a  Free(db, pDef);.
1a27c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c    }.}../*.** Del
1a27d 65 74 65 20 61 20 50 34 20 76 61 6c 75 65 20 69  ete a P4 value i
1a27e 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a  f necessary..*/.
1a27f 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
1a280 50 34 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  P4(sqlite3 *db, 
1a281 69 6e 74 20 70 34 74 79 70 65 2c 20 76 6f 69 64  int p4type, void
1a282 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 70 34 20   *p4){.  if( p4 
1a283 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  ){.    switch( p
1a284 34 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63  4type ){.      c
1a285 61 73 65 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20  ase P4_REAL:.   
1a286 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34     case P4_INT64
1a287 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  :.      case P4_
1a288 4d 50 52 49 4e 54 46 3a 0a 20 20 20 20 20 20 63  MPRINTF:.      c
1a289 61 73 65 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a  ase P4_DYNAMIC:.
1a28a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45        case P4_KE
1a28b 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20 63 61 73  YINFO:.      cas
1a28c 65 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20  e P4_INTARRAY:. 
1a28d 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59       case P4_KEY
1a28e 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a  INFO_HANDOFF: {.
1a28f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1a290 62 46 72 65 65 28 64 62 2c 20 70 34 29 3b 0a 20  bFree(db, p4);. 
1a291 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1a292 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
1a293 20 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a   P4_VDBEFUNC: {.
1a294 20 20 20 20 20 20 20 20 56 64 62 65 46 75 6e 63          VdbeFunc
1a295 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56   *pVdbeFunc = (V
1a296 64 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20  dbeFunc *)p4;.  
1a297 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65        freeEpheme
1a298 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ralFunction(db, 
1a299 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63  pVdbeFunc->pFunc
1a29a 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1a29b 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44  e3VdbeDeleteAuxD
1a29c 61 74 61 28 70 56 64 62 65 46 75 6e 63 2c 20 30  ata(pVdbeFunc, 0
1a29d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1a29e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 64  e3DbFree(db, pVd
1a29f 62 65 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20  beFunc);.       
1a2a0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1a2a1 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 46 55        case P4_FU
1a2a2 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 20  NCDEF: {.       
1a2a3 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75   freeEphemeralFu
1a2a4 6e 63 74 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63  nction(db, (Func
1a2a5 44 65 66 2a 29 70 34 29 3b 0a 20 20 20 20 20 20  Def*)p4);.      
1a2a6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1a2a7 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d  .      case P4_M
1a2a8 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  EM: {.        sq
1a2a9 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 28  lite3ValueFree((
1a2aa 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70  sqlite3_value*)p
1a2ab 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  4);.        brea
1a2ac 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
1a2ad 20 63 61 73 65 20 50 34 5f 56 54 41 42 20 3a 20   case P4_VTAB : 
1a2ae 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1a2af 33 56 74 61 62 55 6e 6c 6f 63 6b 28 28 56 54 61  3VtabUnlock((VTa
1a2b0 62 6c 65 20 2a 29 70 34 29 3b 0a 20 20 20 20 20  ble *)p4);.     
1a2b1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1a2b2 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  }.      case P4_
1a2b3 53 55 42 50 52 4f 47 52 41 4d 20 3a 20 7b 0a 20  SUBPROGRAM : {. 
1a2b4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1a2b5 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28  beProgramDelete(
1a2b6 64 62 2c 20 28 53 75 62 50 72 6f 67 72 61 6d 20  db, (SubProgram 
1a2b7 2a 29 70 34 2c 20 31 29 3b 0a 20 20 20 20 20 20  *)p4, 1);.      
1a2b8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1a2b9 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1a2ba 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 73 70 61  .** Free the spa
1a2bb 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  ce allocated for
1a2bc 20 61 4f 70 20 61 6e 64 20 61 6e 79 20 70 34 20   aOp and any p4 
1a2bd 76 61 6c 75 65 73 20 61 6c 6c 6f 63 61 74 65 64  values allocated
1a2be 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 70 63 6f   for the.** opco
1a2bf 64 65 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 69  des contained wi
1a2c0 74 68 69 6e 2e 20 49 66 20 61 4f 70 20 69 73 20  thin. If aOp is 
1a2c1 6e 6f 74 20 4e 55 4c 4c 20 69 74 20 69 73 20 61  not NULL it is a
1a2c2 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69  ssumed to contai
1a2c3 6e 20 0a 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 65  n .** nOp entrie
1a2c4 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  s. .*/.static vo
1a2c5 69 64 20 76 64 62 65 46 72 65 65 4f 70 41 72 72  id vdbeFreeOpArr
1a2c6 61 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ay(sqlite3 *db, 
1a2c7 4f 70 20 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70  Op *aOp, int nOp
1a2c8 29 7b 0a 20 20 69 66 28 20 61 4f 70 20 29 7b 0a  ){.  if( aOp ){.
1a2c9 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20      Op *pOp;.   
1a2ca 20 66 6f 72 28 70 4f 70 3d 61 4f 70 3b 20 70 4f   for(pOp=aOp; pO
1a2cb 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f 70  p<&aOp[nOp]; pOp
1a2cc 2b 2b 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50  ++){.      freeP
1a2cd 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70  4(db, pOp->p4typ
1a2ce 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23  e, pOp->p4.p);.#
1a2cf 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1a2d0 55 47 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  UG.      sqlite3
1a2d1 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e  DbFree(db, pOp->
1a2d2 7a 43 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69  zComment);.#endi
1a2d3 66 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d  f     .    }.  }
1a2d4 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1a2d5 28 64 62 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a  (db, aOp);.}../*
1a2d6 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68  .** Decrement th
1a2d7 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 74  e ref-count on t
1a2d8 68 65 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74  he SubProgram st
1a2d9 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61  ructure passed a
1a2da 73 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20  s the.** second 
1a2db 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 65  argument. If the
1a2dc 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 68   ref-count reach
1a2dd 65 73 20 7a 65 72 6f 2c 20 66 72 65 65 20 74 68  es zero, free th
1a2de 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  e structure..**.
1a2df 2a 2a 20 54 68 65 20 61 72 72 61 79 20 6f 66 20  ** The array of 
1a2e0 56 44 42 45 20 6f 70 63 6f 64 65 73 20 73 74 6f  VDBE opcodes sto
1a2e1 72 65 64 20 61 73 20 53 75 62 50 72 6f 67 72 61  red as SubProgra
1a2e2 6d 2e 61 4f 70 20 69 73 20 66 72 65 65 64 20 69  m.aOp is freed i
1a2e3 66 0a 2a 2a 20 65 69 74 68 65 72 20 74 68 65 20  f.** either the 
1a2e4 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65  ref-count reache
1a2e5 73 20 7a 65 72 6f 20 6f 72 20 70 61 72 61 6d 65  s zero or parame
1a2e6 74 65 72 20 66 72 65 65 6f 70 20 69 73 20 6e 6f  ter freeop is no
1a2e7 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69  n-zero..**.** Si
1a2e8 6e 63 65 20 74 68 65 20 61 72 72 61 79 20 6f 66  nce the array of
1a2e9 20 6f 70 63 6f 64 65 73 20 70 6f 69 6e 74 65 64   opcodes pointed
1a2ea 20 74 6f 20 62 79 20 53 75 62 50 72 6f 67 72 61   to by SubProgra
1a2eb 6d 2e 61 4f 70 20 6d 61 79 20 64 69 72 65 63 74  m.aOp may direct
1a2ec 6c 79 0a 2a 2a 20 6f 72 20 69 6e 64 69 72 65 63  ly.** or indirec
1a2ed 74 6c 79 20 63 6f 6e 74 61 69 6e 20 61 20 72 65  tly contain a re
1a2ee 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 53  ference to the S
1a2ef 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74  ubProgram struct
1a2f0 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 42  ure itself..** B
1a2f1 79 20 70 61 73 73 69 6e 67 20 61 20 6e 6f 6e 2d  y passing a non-
1a2f2 7a 65 72 6f 20 66 72 65 65 6f 70 20 70 61 72 61  zero freeop para
1a2f3 6d 65 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 65  meter, the calle
1a2f4 72 20 6d 61 79 20 65 6e 73 75 72 65 20 74 68 61  r may ensure tha
1a2f5 74 20 61 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f 67  t all.** SubProg
1a2f6 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61  ram structures a
1a2f7 6e 64 20 74 68 65 69 72 20 61 4f 70 20 61 72 72  nd their aOp arr
1a2f8 61 79 73 20 61 72 65 20 66 72 65 65 64 2c 20 65  ays are freed, e
1a2f9 76 65 6e 20 77 68 65 6e 20 74 68 65 72 65 0a 2a  ven when there.*
1a2fa 2a 20 61 72 65 20 73 75 63 68 20 63 69 72 63 75  * are such circu
1a2fb 6c 61 72 20 72 65 66 65 72 65 6e 63 65 73 2e 0a  lar references..
1a2fc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a2fd 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a2fe 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28  beProgramDelete(
1a2ff 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 75 62  sqlite3 *db, Sub
1a300 50 72 6f 67 72 61 6d 20 2a 70 2c 20 69 6e 74 20  Program *p, int 
1a301 66 72 65 65 6f 70 29 7b 0a 20 20 69 66 28 20 70  freeop){.  if( p
1a302 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1a303 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20  p->nRef>0 );.   
1a304 20 69 66 28 20 66 72 65 65 6f 70 20 7c 7c 20 70   if( freeop || p
1a305 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20  ->nRef==1 ){.   
1a306 20 20 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e     Op *aOp = p->
1a307 61 4f 70 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f  aOp;.      p->aO
1a308 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 76 64 62  p = 0;.      vdb
1a309 65 46 72 65 65 4f 70 41 72 72 61 79 28 64 62 2c  eFreeOpArray(db,
1a30a 20 61 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20   aOp, p->nOp);. 
1a30b 20 20 20 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 3b       p->nOp = 0;
1a30c 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52  .    }.    p->nR
1a30d 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d  ef--;.    if( p-
1a30e 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
1a30f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1a310 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20  db, p);.    }.  
1a311 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  }.}.../*.** Chan
1a312 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61  ge N opcodes sta
1a313 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f  rting at addr to
1a314 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49   No-ops..*/.SQLI
1a315 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a316 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1a317 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c  eToNoop(Vdbe *p,
1a318 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e   int addr, int N
1a319 29 7b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20  ){.  if( p->aOp 
1a31a 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a 70  ){.    VdbeOp *p
1a31b 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64  Op = &p->aOp[add
1a31c 72 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  r];.    sqlite3 
1a31d 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20  *db = p->db;.   
1a31e 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20   while( N-- ){. 
1a31f 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20       freeP4(db, 
1a320 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70  pOp->p4type, pOp
1a321 2d 3e 70 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d  ->p4.p);.      m
1a322 65 6d 73 65 74 28 70 4f 70 2c 20 30 2c 20 73 69  emset(pOp, 0, si
1a323 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20  zeof(pOp[0]));. 
1a324 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65       pOp->opcode
1a325 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20   = OP_Noop;.    
1a326 20 20 70 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20    pOp++;.    }. 
1a327 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
1a328 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
1a329 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 66  the P4 operand f
1a32a 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e  or a specific in
1a32b 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  struction..** Th
1a32c 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
1a32d 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67  eful when a larg
1a32e 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61  e program is loa
1a32f 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74  ded from a.** st
1a330 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67  atic array using
1a331 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1a332 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e  pList but we wan
1a333 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66  t to make a.** f
1a334 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73  ew minor changes
1a335 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e   to the program.
1a336 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74  .**.** If n>=0 t
1a337 68 65 6e 20 74 68 65 20 50 34 20 6f 70 65 72 61  hen the P4 opera
1a338 6e 64 20 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d  nd is dynamic, m
1a339 65 61 6e 69 6e 67 20 74 68 61 74 20 61 20 63 6f  eaning that a co
1a33a 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72  py of.** the str
1a33b 69 6e 67 20 69 73 20 6d 61 64 65 20 69 6e 74 6f  ing is made into
1a33c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
1a33d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
1a33e 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c  lloc()..** A val
1a33f 75 65 20 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73  ue of n==0 means
1a340 20 63 6f 70 79 20 62 79 74 65 73 20 6f 66 20 7a   copy bytes of z
1a341 50 34 20 75 70 20 74 6f 20 61 6e 64 20 69 6e 63  P4 up to and inc
1a342 6c 75 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  luding the.** fi
1a343 72 73 74 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20  rst null byte.  
1a344 49 66 20 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79  If n>0 then copy
1a345 20 6e 2b 31 20 62 79 74 65 73 20 6f 66 20 7a 50   n+1 bytes of zP
1a346 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50  4..**.** If n==P
1a347 34 5f 4b 45 59 49 4e 46 4f 20 69 74 20 6d 65 61  4_KEYINFO it mea
1a348 6e 73 20 74 68 61 74 20 7a 50 34 20 69 73 20 61  ns that zP4 is a
1a349 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65   pointer to a Ke
1a34a 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  yInfo structure.
1a34b 0a 2a 2a 20 41 20 63 6f 70 79 20 69 73 20 6d 61  .** A copy is ma
1a34c 64 65 20 6f 66 20 74 68 65 20 4b 65 79 49 6e 66  de of the KeyInf
1a34d 6f 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f  o structure into
1a34e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
1a34f 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33   from.** sqlite3
1a350 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66  _malloc, to be f
1a351 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64  reed when the Vd
1a352 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e  be is finalized.
1a353 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46  .** n==P4_KEYINF
1a354 4f 5f 48 41 4e 44 4f 46 46 20 69 6e 64 69 63 61  O_HANDOFF indica
1a355 74 65 73 20 74 68 61 74 20 7a 50 34 20 70 6f 69  tes that zP4 poi
1a356 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  nts to a KeyInfo
1a357 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 73 74   structure.** st
1a358 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74  ored in memory t
1a359 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68  hat the caller h
1a35a 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  as obtained from
1a35b 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e   sqlite3_malloc.
1a35c 20 54 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20   The .** caller 
1a35d 73 68 6f 75 6c 64 20 6e 6f 74 20 66 72 65 65 20  should not free 
1a35e 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  the allocation, 
1a35f 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64  it will be freed
1a360 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69   when the Vdbe i
1a361 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a  s.** finalized..
1a362 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c  ** .** Other val
1a363 75 65 73 20 6f 66 20 6e 20 28 50 34 5f 53 54 41  ues of n (P4_STA
1a364 54 49 43 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20  TIC, P4_COLLSEQ 
1a365 65 74 63 2e 29 20 69 6e 64 69 63 61 74 65 20 74  etc.) indicate t
1a366 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a  hat zP4 points.*
1a367 2a 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f 72  * to a string or
1a368 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
1a369 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
1a36a 20 65 78 69 73 74 20 66 6f 72 20 74 68 65 20 6c   exist for the l
1a36b 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68  ifetime of.** th
1a36c 65 20 56 64 62 65 2e 20 49 6e 20 74 68 65 73 65  e Vdbe. In these
1a36d 20 63 61 73 65 73 20 77 65 20 63 61 6e 20 6a 75   cases we can ju
1a36e 73 74 20 63 6f 70 79 20 74 68 65 20 70 6f 69 6e  st copy the poin
1a36f 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64  ter..**.** If ad
1a370 64 72 3c 30 20 74 68 65 6e 20 63 68 61 6e 67 65  dr<0 then change
1a371 20 50 34 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20   P4 on the most 
1a372 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65  recently inserte
1a373 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  d instruction..*
1a374 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a375 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1a376 65 43 68 61 6e 67 65 50 34 28 56 64 62 65 20 2a  eChangeP4(Vdbe *
1a377 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e  p, int addr, con
1a378 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e  st char *zP4, in
1a379 74 20 6e 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b  t n){.  Op *pOp;
1a37a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1a37b 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
1a37c 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  ;.  db = p->db;.
1a37d 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
1a37e 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
1a37f 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  NIT );.  if( p->
1a380 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61  aOp==0 || db->ma
1a381 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1a382 20 20 69 66 20 28 20 6e 21 3d 50 34 5f 4b 45 59    if ( n!=P4_KEY
1a383 49 4e 46 4f 20 26 26 20 6e 21 3d 50 34 5f 56 54  INFO && n!=P4_VT
1a384 41 42 20 29 20 7b 0a 20 20 20 20 20 20 66 72 65  AB ) {.      fre
1a385 65 50 34 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64  eP4(db, n, (void
1a386 2a 29 2a 28 63 68 61 72 2a 2a 29 26 7a 50 34 29  *)*(char**)&zP4)
1a387 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
1a388 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rn;.  }.  assert
1a389 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20  ( p->nOp>0 );.  
1a38a 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d 3e  assert( addr<p->
1a38b 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64 64  nOp );.  if( add
1a38c 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72 20  r<0 ){.    addr 
1a38d 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20  = p->nOp - 1;.  
1a38e 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f  }.  pOp = &p->aO
1a38f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65 65 50  p[addr];.  freeP
1a390 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70  4(db, pOp->p4typ
1a391 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20  e, pOp->p4.p);. 
1a392 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a   pOp->p4.p = 0;.
1a393 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33    if( n==P4_INT3
1a394 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65  2 ){.    /* Note
1a395 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 20 73  : this cast is s
1a396 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 68 65  afe, because the
1a397 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 6f 69   origin data poi
1a398 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a 20 20  nt was an int.  
1a399 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 63 61    ** that was ca
1a39a 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 20 63  st to a (const c
1a39b 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70  har *). */.    p
1a39c 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54  Op->p4.i = SQLIT
1a39d 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34  E_PTR_TO_INT(zP4
1a39e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  );.    pOp->p4ty
1a39f 70 65 20 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20  pe = P4_INT32;. 
1a3a0 20 7d 65 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d   }else if( zP4==
1a3a1 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  0 ){.    pOp->p4
1a3a2 2e 70 20 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d  .p = 0;.    pOp-
1a3a3 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54  >p4type = P4_NOT
1a3a4 55 53 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66  USED;.  }else if
1a3a5 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20  ( n==P4_KEYINFO 
1a3a6 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  ){.    KeyInfo *
1a3a7 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e  pKeyInfo;.    in
1a3a8 74 20 6e 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b  t nField, nByte;
1a3a9 0a 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 28  ..    nField = (
1a3aa 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e  (KeyInfo*)zP4)->
1a3ab 6e 46 69 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74  nField;.    nByt
1a3ac 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79  e = sizeof(*pKey
1a3ad 49 6e 66 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d  Info) + (nField-
1a3ae 31 29 2a 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e  1)*sizeof(pKeyIn
1a3af 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20  fo->aColl[0]) + 
1a3b0 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79  nField;.    pKey
1a3b1 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61  Info = sqlite3Ma
1a3b2 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20  lloc( nByte );. 
1a3b3 20 20 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49     pOp->p4.pKeyI
1a3b4 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a  nfo = pKeyInfo;.
1a3b5 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f      if( pKeyInfo
1a3b6 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 61 53   ){.      u8 *aS
1a3b7 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20  ortOrder;.      
1a3b8 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c  memcpy(pKeyInfo,
1a3b9 20 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20   zP4, nByte);.  
1a3ba 20 20 20 20 61 53 6f 72 74 4f 72 64 65 72 20 3d      aSortOrder =
1a3bb 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
1a3bc 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 69 66 28  Order;.      if(
1a3bd 20 61 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20   aSortOrder ){. 
1a3be 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d         pKeyInfo-
1a3bf 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  >aSortOrder = (u
1a3c0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70  nsigned char*)&p
1a3c1 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e  KeyInfo->aColl[n
1a3c2 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20  Field];.        
1a3c3 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d  memcpy(pKeyInfo-
1a3c4 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f  >aSortOrder, aSo
1a3c5 72 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64 29  rtOrder, nField)
1a3c6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1a3c7 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
1a3c8 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65  _KEYINFO;.    }e
1a3c9 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62  lse{.      p->db
1a3ca 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1a3cb 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70   1;.      pOp->p
1a3cc 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53  4type = P4_NOTUS
1a3cd 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ED;.    }.  }els
1a3ce 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49  e if( n==P4_KEYI
1a3cf 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20  NFO_HANDOFF ){. 
1a3d0 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28     pOp->p4.p = (
1a3d1 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70  void*)zP4;.    p
1a3d2 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  Op->p4type = P4_
1a3d3 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65  KEYINFO;.  }else
1a3d4 20 69 66 28 20 6e 3d 3d 50 34 5f 56 54 41 42 20   if( n==P4_VTAB 
1a3d5 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70  ){.    pOp->p4.p
1a3d6 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20   = (void*)zP4;. 
1a3d7 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d     pOp->p4type =
1a3d8 20 50 34 5f 56 54 41 42 3b 0a 20 20 20 20 73 71   P4_VTAB;.    sq
1a3d9 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 28 56  lite3VtabLock((V
1a3da 54 61 62 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 20  Table *)zP4);.  
1a3db 20 20 61 73 73 65 72 74 28 20 28 28 56 54 61 62    assert( ((VTab
1a3dc 6c 65 20 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d 70  le *)zP4)->db==p
1a3dd 2d 3e 64 62 20 29 3b 0a 20 20 7d 65 6c 73 65 20  ->db );.  }else 
1a3de 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70  if( n<0 ){.    p
1a3df 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64  Op->p4.p = (void
1a3e0 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e  *)zP4;.    pOp->
1a3e1 70 34 74 79 70 65 20 3d 20 28 73 69 67 6e 65 64  p4type = (signed
1a3e2 20 63 68 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 65   char)n;.  }else
1a3e3 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29  {.    if( n==0 )
1a3e4 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
1a3e5 65 6e 33 30 28 7a 50 34 29 3b 0a 20 20 20 20 70  en30(zP4);.    p
1a3e6 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74  Op->p4.z = sqlit
1a3e7 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64  e3DbStrNDup(p->d
1a3e8 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20  b, zP4, n);.    
1a3e9 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
1a3ea 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a  _DYNAMIC;.  }.}.
1a3eb 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
1a3ec 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
1a3ed 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20   comment on the 
1a3ee 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
1a3ef 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74  y coded instruct
1a3f0 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65  ion.  Or.** inse
1a3f1 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61  rt a No-op and a
1a3f2 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74  dd the comment t
1a3f3 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74 72  o that new instr
1a3f4 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a  uction.  This.**
1a3f5 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20   makes the code 
1a3f6 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20 64  easier to read d
1a3f7 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67 2e  uring debugging.
1a3f8 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 68    None of this h
1a3f9 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70  appens.** in a p
1a3fa 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e  roduction build.
1a3fb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a3fc 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a3fd 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20  dbeComment(Vdbe 
1a3fe 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *p, const char *
1a3ff 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
1a400 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69   va_list ap;.  i
1a401 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a  f( !p ) return;.
1a402 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70    assert( p->nOp
1a403 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20  >0 || p->aOp==0 
1a404 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
1a405 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70  aOp==0 || p->aOp
1a406 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d  [p->nOp-1].zComm
1a407 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d  ent==0 || p->db-
1a408 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
1a409 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b  .  if( p->nOp ){
1a40a 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d  .    char **pz =
1a40b 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d   &p->aOp[p->nOp-
1a40c 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20  1].zComment;.   
1a40d 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
1a40e 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69  ormat);.    sqli
1a40f 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c  te3DbFree(p->db,
1a410 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d   *pz);.    *pz =
1a411 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66   sqlite3VMPrintf
1a412 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  (p->db, zFormat,
1a413 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64   ap);.    va_end
1a414 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49  (ap);.  }.}.SQLI
1a415 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a416 73 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43  sqlite3VdbeNoopC
1a417 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20  omment(Vdbe *p, 
1a418 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
1a419 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
1a41a 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21  list ap;.  if( !
1a41b 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  p ) return;.  sq
1a41c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
1a41d 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61  p, OP_Noop);.  a
1a41e 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20  ssert( p->nOp>0 
1a41f 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a  || p->aOp==0 );.
1a420 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70    assert( p->aOp
1a421 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d  ==0 || p->aOp[p-
1a422 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74  >nOp-1].zComment
1a423 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61  ==0 || p->db->ma
1a424 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
1a425 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20  if( p->nOp ){.  
1a426 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70    char **pz = &p
1a427 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e  ->aOp[p->nOp-1].
1a428 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61  zComment;.    va
1a429 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
1a42a 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  at);.    sqlite3
1a42b 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70  DbFree(p->db, *p
1a42c 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71  z);.    *pz = sq
1a42d 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d  lite3VMPrintf(p-
1a42e 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  >db, zFormat, ap
1a42f 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70  );.    va_end(ap
1a430 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  );.  }.}.#endif 
1a431 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f   /* NDEBUG */../
1a432 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1a433 6f 70 63 6f 64 65 20 66 6f 72 20 61 20 67 69 76  opcode for a giv
1a434 65 6e 20 61 64 64 72 65 73 73 2e 20 20 49 66 20  en address.  If 
1a435 74 68 65 20 61 64 64 72 65 73 73 20 69 73 20 2d  the address is -
1a436 31 2c 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72  1, then.** retur
1a437 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  n the most recen
1a438 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70 63  tly inserted opc
1a439 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ode..**.** If a 
1a43a 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1a43b 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  n error has occu
1a43c 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68  rred prior to th
1a43d 65 20 63 61 6c 6c 69 6e 67 20 6f 66 20 74 68 69  e calling of thi
1a43e 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 74 68  s.** routine, th
1a43f 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  en a pointer to 
1a440 61 20 64 75 6d 6d 79 20 56 64 62 65 4f 70 20 77  a dummy VdbeOp w
1a441 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e  ill be returned.
1a442 20 20 54 68 61 74 20 6f 70 63 6f 64 65 0a 2a 2a    That opcode.**
1a443 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64   is readable and
1a444 20 77 72 69 74 61 62 6c 65 2c 20 62 75 74 20 69   writable, but i
1a445 74 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 2e  t has no effect.
1a446 20 20 54 68 65 20 72 65 74 75 72 6e 20 6f 66 20    The return of 
1a447 61 20 64 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f 64  a dummy.** opcod
1a448 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c  e allows the cal
1a449 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75  l to continue fu
1a44a 6e 63 74 69 6f 6e 69 6e 67 20 61 66 74 65 72 20  nctioning after 
1a44b 61 20 4f 4f 4d 20 66 61 75 6c 74 20 77 69 74 68  a OOM fault with
1a44c 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f  out.** having to
1a44d 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
1a44e 20 74 68 65 20 72 65 74 75 72 6e 20 66 72 6f 6d   the return from
1a44f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1a450 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72   a valid pointer
1a451 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 68  ..**.** About th
1a452 65 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  e #ifdef SQLITE_
1a453 4f 4d 49 54 5f 54 52 41 43 45 3a 20 20 4e 6f 72  OMIT_TRACE:  Nor
1a454 6d 61 6c 6c 79 2c 20 74 68 69 73 20 72 6f 75 74  mally, this rout
1a455 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c  ine is never cal
1a456 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 2d  led.** unless p-
1a457 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 73 20 69 73  >nOp>0.  This is
1a458 20 62 65 63 61 75 73 65 20 69 6e 20 74 68 65 20   because in the 
1a459 61 62 73 65 6e 73 65 20 6f 66 20 53 51 4c 49 54  absense of SQLIT
1a45a 45 5f 4f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a 2a  E_OMIT_TRACE,.**
1a45b 20 61 6e 20 4f 50 5f 54 72 61 63 65 20 69 6e 73   an OP_Trace ins
1a45c 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 77 61  truction is alwa
1a45d 79 73 20 69 6e 73 65 72 74 65 64 20 62 79 20 73  ys inserted by s
1a45e 71 6c 69 74 65 33 56 64 62 65 47 65 74 28 29 20  qlite3VdbeGet() 
1a45f 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 20  as soon as.** a 
1a460 6e 65 77 20 56 44 42 45 20 69 73 20 63 72 65 61  new VDBE is crea
1a461 74 65 64 2e 20 20 53 6f 20 77 65 20 61 72 65 20  ted.  So we are 
1a462 66 72 65 65 20 74 6f 20 73 65 74 20 61 64 64 72  free to set addr
1a463 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 74   to p->nOp-1 wit
1a464 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74  hout.** having t
1a465 6f 20 64 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74  o double-check t
1a466 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
1a467 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e   the result is n
1a468 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 42 75 74  on-negative. But
1a469 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d  .** if SQLITE_OM
1a46a 49 54 5f 54 52 41 43 45 20 69 73 20 64 65 66 69  IT_TRACE is defi
1a46b 6e 65 64 2c 20 74 68 65 20 4f 50 5f 54 72 61 63  ned, the OP_Trac
1a46c 65 20 69 73 20 6f 6d 69 74 74 65 64 20 61 6e 64  e is omitted and
1a46d 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 0a 2a   we do need to.*
1a46e 2a 20 63 68 65 63 6b 20 74 68 65 20 76 61 6c 75  * check the valu
1a46f 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65  e of p->nOp-1 be
1a470 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e  fore continuing.
1a471 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a472 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74  TE VdbeOp *sqlit
1a473 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65  e3VdbeGetOp(Vdbe
1a474 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a   *p, int addr){.
1a475 20 20 73 74 61 74 69 63 20 56 64 62 65 4f 70 20    static VdbeOp 
1a476 64 75 6d 6d 79 3b 0a 20 20 61 73 73 65 72 74 28  dummy;.  assert(
1a477 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
1a478 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
1a479 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 23 69  if( addr<0 ){.#i
1a47a 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1a47b 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 70  _TRACE.    if( p
1a47c 2d 3e 6e 4f 70 3d 3d 30 20 29 20 72 65 74 75 72  ->nOp==0 ) retur
1a47d 6e 20 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66  n &dummy;.#endif
1a47e 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e  .    addr = p->n
1a47f 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73  Op - 1;.  }.  as
1a480 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26  sert( (addr>=0 &
1a481 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c  & addr<p->nOp) |
1a482 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  | p->db->mallocF
1a483 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70  ailed );.  if( p
1a484 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1a485 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
1a486 20 26 64 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 65   &dummy;.  }else
1a487 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 2d  {.    return &p-
1a488 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 7d 0a  >aOp[addr];.  }.
1a489 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
1a48a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
1a48b 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  AIN) || !defined
1a48c 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 20 20  (NDEBUG) \.     
1a48d 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f  || defined(VDBE_
1a48e 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69  PROFILE) || defi
1a48f 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
1a490 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  )./*.** Compute 
1a491 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65  a string that de
1a492 73 63 72 69 62 65 73 20 74 68 65 20 50 34 20 70  scribes the P4 p
1a493 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20  arameter for an 
1a494 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a  opcode..** Use z
1a495 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71  Temp for any req
1a496 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20  uired temporary 
1a497 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f  buffer space..*/
1a498 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 69  .static char *di
1a499 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c  splayP4(Op *pOp,
1a49a 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e   char *zTemp, in
1a49b 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 72  t nTemp){.  char
1a49c 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20   *zP4 = zTemp;. 
1a49d 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d   assert( nTemp>=
1a49e 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20  20 );.  switch( 
1a49f 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20  pOp->p4type ){. 
1a4a0 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
1a4a1 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 20 63  FO_STATIC:.    c
1a4a2 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20  ase P4_KEYINFO: 
1a4a3 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a  {.      int i, j
1a4a4 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20  ;.      KeyInfo 
1a4a5 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d  *pKeyInfo = pOp-
1a4a6 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  >p4.pKeyInfo;.  
1a4a7 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a4a8 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
1a4a9 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c  p, "keyinfo(%d",
1a4aa 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c   pKeyInfo->nFiel
1a4ab 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 71  d);.      i = sq
1a4ac 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54  lite3Strlen30(zT
1a4ad 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  emp);.      for(
1a4ae 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d  j=0; j<pKeyInfo-
1a4af 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20  >nField; j++){. 
1a4b0 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a         CollSeq *
1a4b1 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f  pColl = pKeyInfo
1a4b2 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20  ->aColl[j];.    
1a4b3 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b      if( pColl ){
1a4b4 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e  .          int n
1a4b5 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
1a4b6 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29  30(pColl->zName)
1a4b7 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1a4b8 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20  i+n>nTemp-6 ){. 
1a4b9 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
1a4ba 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e  y(&zTemp[i],",..
1a4bb 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20  .",4);.         
1a4bc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1a4bd 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1a4be 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27  zTemp[i++] = ','
1a4bf 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1a4c0 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f  pKeyInfo->aSortO
1a4c1 72 64 65 72 20 26 26 20 70 4b 65 79 49 6e 66 6f  rder && pKeyInfo
1a4c2 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20  ->aSortOrder[j] 
1a4c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a  ){.            z
1a4c4 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b  Temp[i++] = '-';
1a4c5 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1a4c6 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a         memcpy(&z
1a4c7 54 65 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e  Temp[i], pColl->
1a4c8 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20  zName,n+1);.    
1a4c9 20 20 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20        i += n;.  
1a4ca 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1a4cb 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20  i+4<nTemp-6 ){. 
1a4cc 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
1a4cd 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22  &zTemp[i],",nil"
1a4ce 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ,4);.          i
1a4cf 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d   += 4;.        }
1a4d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
1a4d1 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b  Temp[i++] = ')';
1a4d2 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20  .      zTemp[i] 
1a4d3 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72  = 0;.      asser
1a4d4 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20  t( i<nTemp );.  
1a4d5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1a4d6 0a 20 20 20 20 63 61 73 65 20 50 34 5f 43 4f 4c  .    case P4_COL
1a4d7 4c 53 45 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f  LSEQ: {.      Co
1a4d8 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70  llSeq *pColl = p
1a4d9 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20  Op->p4.pColl;.  
1a4da 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a4db 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
1a4dc 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30  p, "collseq(%.20
1a4dd 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  s)", pColl->zNam
1a4de 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  e);.      break;
1a4df 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1a4e0 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20  P4_FUNCDEF: {.  
1a4e1 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65      FuncDef *pDe
1a4e2 66 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e  f = pOp->p4.pFun
1a4e3 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
1a4e4 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c  _snprintf(nTemp,
1a4e5 20 7a 54 65 6d 70 2c 20 22 25 73 28 25 64 29 22   zTemp, "%s(%d)"
1a4e6 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70  , pDef->zName, p
1a4e7 44 65 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20  Def->nArg);.    
1a4e8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1a4e9 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34     case P4_INT64
1a4ea 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
1a4eb 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
1a4ec 2c 20 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c  , zTemp, "%lld",
1a4ed 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b   *pOp->p4.pI64);
1a4ee 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1a4ef 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f    }.    case P4_
1a4f0 49 4e 54 33 32 3a 20 7b 0a 20 20 20 20 20 20 73  INT32: {.      s
1a4f1 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a4f2 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25  nTemp, zTemp, "%
1a4f3 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a  d", pOp->p4.i);.
1a4f4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a4f5 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 52   }.    case P4_R
1a4f6 45 41 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  EAL: {.      sql
1a4f7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
1a4f8 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31  emp, zTemp, "%.1
1a4f9 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52  6g", *pOp->p4.pR
1a4fa 65 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  eal);.      brea
1a4fb 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1a4fc 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20  e P4_MEM: {.    
1a4fd 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f    Mem *pMem = pO
1a4fe 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20  p->p4.pMem;.    
1a4ff 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
1a500 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1a501 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  l)==0 );.      i
1a502 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
1a503 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20   MEM_Str ){.    
1a504 20 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e      zP4 = pMem->
1a505 7a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  z;.      }else i
1a506 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
1a507 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
1a508 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a509 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
1a50a 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d  p, "%lld", pMem-
1a50b 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c  >u.i);.      }el
1a50c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  se if( pMem->fla
1a50d 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b  gs & MEM_Real ){
1a50e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a50f 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c  _snprintf(nTemp,
1a510 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c   zTemp, "%.16g",
1a511 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20   pMem->r);.     
1a512 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1a513 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c  assert( pMem->fl
1a514 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29  ags & MEM_Blob )
1a515 3b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20  ;.        zP4 = 
1a516 22 28 62 6c 6f 62 29 22 3b 0a 20 20 20 20 20 20  "(blob)";.      
1a517 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1a518 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
1a519 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1a51a 54 41 42 4c 45 0a 20 20 20 20 63 61 73 65 20 50  TABLE.    case P
1a51b 34 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20 20 20  4_VTAB: {.      
1a51c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1a51d 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56  tab = pOp->p4.pV
1a51e 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20  tab->pVtab;.    
1a51f 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1a520 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
1a521 20 22 76 74 61 62 3a 25 70 3a 25 70 22 2c 20 70   "vtab:%p:%p", p
1a522 56 74 61 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f  Vtab, pVtab->pMo
1a523 64 75 6c 65 29 3b 0a 20 20 20 20 20 20 62 72 65  dule);.      bre
1a524 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
1a525 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54  .    case P4_INT
1a526 41 52 52 41 59 3a 20 7b 0a 20 20 20 20 20 20 73  ARRAY: {.      s
1a527 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a528 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69  nTemp, zTemp, "i
1a529 6e 74 61 72 72 61 79 22 29 3b 0a 20 20 20 20 20  ntarray");.     
1a52a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1a52b 20 20 63 61 73 65 20 50 34 5f 53 55 42 50 52 4f    case P4_SUBPRO
1a52c 47 52 41 4d 3a 20 7b 0a 20 20 20 20 20 20 73 71  GRAM: {.      sq
1a52d 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
1a52e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 70 72  Temp, zTemp, "pr
1a52f 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 20 62  ogram");.      b
1a530 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1a531 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
1a532 20 7a 50 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a   zP4 = pOp->p4.z
1a533 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 34 3d  ;.      if( zP4=
1a534 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 50  =0 ){.        zP
1a535 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20  4 = zTemp;.     
1a536 20 20 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b     zTemp[0] = 0;
1a537 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1a538 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 50 34   }.  assert( zP4
1a539 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  !=0 );.  return 
1a53a 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  zP4;.}.#endif../
1a53b 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 6f 20  *.** Declare to 
1a53c 74 68 65 20 56 64 62 65 20 74 68 61 74 20 74 68  the Vdbe that th
1a53d 65 20 42 54 72 65 65 20 6f 62 6a 65 63 74 20 61  e BTree object a
1a53e 74 20 64 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20  t db->aDb[i] is 
1a53f 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  used..*/.SQLITE_
1a540 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a541 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
1a542 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69  e(Vdbe *p, int i
1a543 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20  ){.  int mask;. 
1a544 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
1a545 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26   i<p->db->nDb &&
1a546 20 69 3c 73 69 7a 65 6f 66 28 75 33 32 29 2a 38   i<sizeof(u32)*8
1a547 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c   );.  assert( i<
1a548 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62  (int)sizeof(p->b
1a549 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20  treeMask)*8 );. 
1a54a 20 6d 61 73 6b 20 3d 20 28 28 75 33 32 29 31 29   mask = ((u32)1)
1a54b 3c 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62  <<i;.  if( (p->b
1a54c 74 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29  treeMask & mask)
1a54d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74  ==0 ){.    p->bt
1a54e 72 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b  reeMask |= mask;
1a54f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1a550 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72  eMutexArrayInser
1a551 74 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d  t(&p->aMutex, p-
1a552 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29  >db->aDb[i].pBt)
1a553 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65  ;.  }.}...#if de
1a554 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49  fined(VDBE_PROFI
1a555 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  LE) || defined(S
1a556 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a  QLITE_DEBUG)./*.
1a557 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c  ** Print a singl
1a558 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20  e opcode.  This 
1a559 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
1a55a 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e  for debugging on
1a55b 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
1a55c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a55d 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49  e3VdbePrintOp(FI
1a55e 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63  LE *pOut, int pc
1a55f 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68  , Op *pOp){.  ch
1a560 61 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20  ar *zP4;.  char 
1a561 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74  zPtr[50];.  stat
1a562 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ic const char *z
1a563 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25  Format1 = "%4d %
1a564 2d 31 33 73 20 25 34 64 20 25 34 64 20 25 34 64  -13s %4d %4d %4d
1a565 20 25 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22   %-4s %.2X %s\n"
1a566 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20  ;.  if( pOut==0 
1a567 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b  ) pOut = stdout;
1a568 0a 20 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79  .  zP4 = display
1a569 50 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69  P4(pOp, zPtr, si
1a56a 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66  zeof(zPtr));.  f
1a56b 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f  printf(pOut, zFo
1a56c 72 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20  rmat1, pc, .    
1a56d 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e    sqlite3OpcodeN
1a56e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29  ame(pOp->opcode)
1a56f 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
1a570 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34  p2, pOp->p3, zP4
1a571 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65  , pOp->p5,.#ifde
1a572 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
1a573 20 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65       pOp->zComme
1a574 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65  nt ? pOp->zComme
1a575 6e 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20  nt : "".#else.  
1a576 20 20 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20      "".#endif.  
1a577 29 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74  );.  fflush(pOut
1a578 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
1a579 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72  ** Release an ar
1a57a 72 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65  ray of N Mem ele
1a57b 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20  ments.*/.static 
1a57c 76 6f 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41  void releaseMemA
1a57d 72 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74  rray(Mem *p, int
1a57e 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20   N){.  if( p && 
1a57f 4e 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45  N ){.    Mem *pE
1a580 6e 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  nd;.    sqlite3 
1a581 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20  *db = p->db;.   
1a582 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65   u8 malloc_faile
1a583 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  d = db->mallocFa
1a584 69 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 45  iled;.    for(pE
1a585 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64  nd=&p[N]; p<pEnd
1a586 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73  ; p++){.      as
1a587 73 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70  sert( (&p[1])==p
1a588 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d  End || p[0].db==
1a589 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20  p[1].db );..    
1a58a 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20    /* This block 
1a58b 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c  is really an inl
1a58c 69 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ined version of 
1a58d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1a58e 6c 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a  lease().      **
1a58f 20 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61   that takes adva
1a590 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63  ntage of the fac
1a591 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72  t that the memor
1a592 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20  y cell value is 
1a593 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20  .      ** being 
1a594 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65  set to NULL afte
1a595 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20  r releasing any 
1a596 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65  dynamic resource
1a597 73 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  s..      **.    
1a598 20 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69    ** The justifi
1a599 63 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69  cation for dupli
1a59a 63 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74  cating code is t
1a59b 68 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  hat according to
1a59c 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67   .      ** callg
1a59d 72 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65  rind, this cause
1a59e 73 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 74  s a certain test
1a59f 20 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 65   case to hit the
1a5a0 20 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20   CPU 4.7 .      
1a5a1 2a 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20  ** percent less 
1a5a2 28 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20  (x86 linux, gcc 
1a5a3 76 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d  version 4.1.2, -
1a5a4 4f 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20  O6) than if .   
1a5a5 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d     ** sqlite3Mem
1a5a6 52 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 63  Release() were c
1a5a7 61 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e  alled from here.
1a5a8 20 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20   With -O2, this 
1a5a9 6a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74  jumps.      ** t
1a5aa 6f 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54  o 6.6 percent. T
1a5ab 68 65 20 74 65 73 74 20 63 61 73 65 20 69 73 20  he test case is 
1a5ac 69 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 72  inserting 1000 r
1a5ad 6f 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65  ows into a table
1a5ae 20 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20   .      ** with 
1a5af 6e 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67  no indexes using
1a5b0 20 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72   a single prepar
1a5b1 65 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  ed INSERT statem
1a5b2 65 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20  ent, bind() .   
1a5b3 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28     ** and reset(
1a5b4 29 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 67  ). Inserts are g
1a5b5 72 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72  rouped into a tr
1a5b6 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20  ansaction..     
1a5b7 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d   */.      if( p-
1a5b8 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c  >flags&(MEM_Agg|
1a5b9 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d  MEM_Dyn|MEM_Fram
1a5ba 65 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b  e|MEM_RowSet) ){
1a5bb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a5bc 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
1a5bd 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
1a5be 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b  f( p->zMalloc ){
1a5bf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a5c0 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d  DbFree(db, p->zM
1a5c1 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20  alloc);.        
1a5c2 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  p->zMalloc = 0;.
1a5c3 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70        }..      p
1a5c4 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  ->flags = MEM_Nu
1a5c5 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  ll;.    }.    db
1a5c6 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1a5c7 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a   malloc_failed;.
1a5c8 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c    }.}../*.** Del
1a5c9 65 74 65 20 61 20 56 64 62 65 46 72 61 6d 65 20  ete a VdbeFrame 
1a5ca 6f 62 6a 65 63 74 20 61 6e 64 20 69 74 73 20 63  object and its c
1a5cb 6f 6e 74 65 6e 74 73 2e 20 56 64 62 65 46 72 61  ontents. VdbeFra
1a5cc 6d 65 20 6f 62 6a 65 63 74 73 20 61 72 65 0a 2a  me objects are.*
1a5cd 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74  * allocated by t
1a5ce 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70  he OP_Program op
1a5cf 63 6f 64 65 20 69 6e 20 73 71 6c 69 74 65 33 56  code in sqlite3V
1a5d0 64 62 65 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 51  dbeExec()..*/.SQ
1a5d1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a5d2 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61  d sqlite3VdbeFra
1a5d3 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 72 61  meDelete(VdbeFra
1a5d4 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  me *p){.  int i;
1a5d5 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 56  .  Mem *aMem = V
1a5d6 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 3b 0a  dbeFrameMem(p);.
1a5d7 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61    VdbeCursor **a
1a5d8 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 72 73  pCsr = (VdbeCurs
1a5d9 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e 6e  or **)&aMem[p->n
1a5da 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f 72  ChildMem];.  for
1a5db 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 6c  (i=0; i<p->nChil
1a5dc 64 43 73 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  dCsr; i++){.    
1a5dd 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
1a5de 75 72 73 6f 72 28 70 2d 3e 76 2c 20 61 70 43 73  ursor(p->v, apCs
1a5df 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c  r[i]);.  }.  rel
1a5e0 65 61 73 65 4d 65 6d 41 72 72 61 79 28 61 4d 65  easeMemArray(aMe
1a5e1 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 29  m, p->nChildMem)
1a5e2 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1a5e3 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b 0a  e(p->v->db, p);.
1a5e4 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1a5e5 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f  E_OMIT_EXPLAIN./
1a5e6 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69 73 74  *.** Give a list
1a5e7 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f 67 72  ing of the progr
1a5e8 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74 75 61  am in the virtua
1a5e9 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a  l machine..**.**
1a5ea 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 69   The interface i
1a5eb 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 73 71  s the same as sq
1a5ec 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e  lite3VdbeExec().
1a5ed 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66    But instead of
1a5ee 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 20  .** running the 
1a5ef 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73  code, it invokes
1a5f0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
1a5f1 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e 73 74  ce for each inst
1a5f2 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73  ruction..** This
1a5f3 20 66 65 61 74 75 72 65 20 69 73 20 75 73 65 64   feature is used
1a5f4 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45   to implement "E
1a5f5 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57  XPLAIN"..**.** W
1a5f6 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d  hen p->explain==
1a5f7 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75 63 74  1, each instruct
1a5f8 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e 20 20  ion is listed.  
1a5f9 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61  When.** p->expla
1a5fa 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45  in==2, only OP_E
1a5fb 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 63 74 69  xplain instructi
1a5fc 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 64 20 61  ons are listed a
1a5fd 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 72 65 20  nd these.** are 
1a5fe 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 66 66 65  shown in a diffe
1a5ff 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d  rent format.  p-
1a600 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75  >explain==2 is u
1a601 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
1a602 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 55 45 52  .** EXPLAIN QUER
1a603 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54  Y PLAN..*/.SQLIT
1a604 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1a605 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 0a 20  lite3VdbeList(. 
1a606 20 56 64 62 65 20 2a 70 20 20 20 20 20 20 20 20   Vdbe *p        
1a607 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1a608 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69  e VDBE */.){.  i
1a609 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20  nt nRow;        
1a60a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a60b 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
1a60c 62 65 72 20 6f 66 20 72 6f 77 73 20 74 6f 20 72  ber of rows to r
1a60d 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  eturn */.  int n
1a60e 53 75 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Sub = 0;        
1a60f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a610 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 75 62  /* Number of sub
1a611 2d 76 64 62 65 73 20 73 65 65 6e 20 73 6f 20 66  -vdbes seen so f
1a612 61 72 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72  ar */.  SubProgr
1a613 61 6d 20 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20  am **apSub = 0; 
1a614 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a615 41 72 72 61 79 20 6f 66 20 73 75 62 2d 76 64 62  Array of sub-vdb
1a616 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75  es */.  Mem *pSu
1a617 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  b = 0;.  sqlite3
1a618 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20   *db = p->db;.  
1a619 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
1a61a 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d  = SQLITE_OK;.  M
1a61b 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52  em *pMem = p->pR
1a61c 65 73 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61  esultSet = &p->a
1a61d 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72  Mem[1];..  asser
1a61e 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b  t( p->explain );
1a61f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
1a620 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
1a621 52 55 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 28  RUN );.  assert(
1a622 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49   db->magic==SQLI
1a623 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b  TE_MAGIC_BUSY );
1a624 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63  .  assert( p->rc
1a625 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
1a626 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ->rc==SQLITE_BUS
1a627 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49  Y || p->rc==SQLI
1a628 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f  TE_NOMEM );..  /
1a629 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68  * Even though th
1a62a 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e  is opcode does n
1a62b 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73  ot use dynamic s
1a62c 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20  trings for.  ** 
1a62d 74 68 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75  the result, resu
1a62e 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62  lt columns may b
1a62f 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66  ecome dynamic if
1a630 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a   the user calls.
1a631 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c    ** sqlite3_col
1a632 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61  umn_text16(), ca
1a633 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74  using a translat
1a634 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e  ion to UTF-16 en
1a635 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72  coding..  */.  r
1a636 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70  eleaseMemArray(p
1a637 4d 65 6d 2c 20 38 29 3b 0a 0a 20 20 69 66 28 20  Mem, 8);..  if( 
1a638 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  p->rc==SQLITE_NO
1a639 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  MEM ){.    /* Th
1a63a 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 20  is happens if a 
1a63b 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20  malloc() inside 
1a63c 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
1a63d 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20  3_column_text() 
1a63e 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  or.    ** sqlite
1a63f 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
1a640 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20  ) failed.  */.  
1a641 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1a642 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  ed = 1;.    retu
1a643 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
1a644 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72  .  }..  /* Figur
1a645 65 20 6f 75 74 20 74 6f 74 61 6c 20 6e 75 6d 62  e out total numb
1a646 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
1a647 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64  will be returned
1a648 20 62 79 20 74 68 69 73 20 0a 20 20 2a 2a 20 45   by this .  ** E
1a649 58 50 4c 41 49 4e 20 70 72 6f 67 72 61 6d 2e 20  XPLAIN program. 
1a64a 20 2a 2f 0a 20 20 6e 52 6f 77 20 3d 20 70 2d 3e   */.  nRow = p->
1a64b 6e 4f 70 3b 0a 20 20 69 66 28 20 70 2d 3e 65 78  nOp;.  if( p->ex
1a64c 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20  plain==1 ){.    
1a64d 70 53 75 62 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pSub = &p->aMem[
1a64e 39 5d 3b 0a 20 20 20 20 69 66 28 20 70 53 75 62  9];.    if( pSub
1a64f 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62  ->flags&MEM_Blob
1a650 20 29 7b 0a 20 20 20 20 20 20 6e 53 75 62 20 3d   ){.      nSub =
1a651 20 70 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28   pSub->n/sizeof(
1a652 56 64 62 65 2a 29 3b 0a 20 20 20 20 20 20 61 70  Vdbe*);.      ap
1a653 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61  Sub = (SubProgra
1a654 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20  m **)pSub->z;.  
1a655 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
1a656 20 69 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20   i<nSub; i++){. 
1a657 20 20 20 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53       nRow += apS
1a658 75 62 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20  ub[i]->nOp;.    
1a659 7d 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20  }.  }..  do{.   
1a65a 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20   i = p->pc++;.  
1a65b 7d 77 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26  }while( i<nRow &
1a65c 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20  & p->explain==2 
1a65d 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63  && p->aOp[i].opc
1a65e 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20  ode!=OP_Explain 
1a65f 29 3b 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77  );.  if( i>=nRow
1a660 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1a661 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72  SQLITE_OK;.    r
1a662 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
1a663 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d  .  }else if( db-
1a664 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
1a665 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  d ){.    p->rc =
1a666 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
1a667 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  T;.    rc = SQLI
1a668 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71  TE_ERROR;.    sq
1a669 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1a66a 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1a66b 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72  "%s", sqlite3Err
1a66c 53 74 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d  Str(p->rc));.  }
1a66d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
1a66e 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a  z;.    Op *pOp;.
1a66f 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70      if( i<p->nOp
1a670 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 20 3d 20   ){.      pOp = 
1a671 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20  &p->aOp[i];.    
1a672 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
1a673 20 6a 3b 0a 20 20 20 20 20 20 69 20 2d 3d 20 70   j;.      i -= p
1a674 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72  ->nOp;.      for
1a675 28 6a 3d 30 3b 20 69 3e 3d 61 70 53 75 62 5b 6a  (j=0; i>=apSub[j
1a676 5d 2d 3e 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 20  ]->nOp; j++){.  
1a677 20 20 20 20 20 20 69 20 2d 3d 20 61 70 53 75 62        i -= apSub
1a678 5b 6a 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20  [j]->nOp;.      
1a679 7d 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 61  }.      pOp = &a
1a67a 70 53 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b  pSub[j]->aOp[i];
1a67b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1a67c 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a  ->explain==1 ){.
1a67d 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1a67e 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1a67f 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1a680 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a  SQLITE_INTEGER;.
1a681 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
1a682 3d 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  = i;            
1a683 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a684 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63      /* Program c
1a685 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  ounter */.      
1a686 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20  pMem++;.  .     
1a687 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1a688 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74  EM_Static|MEM_St
1a689 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  r|MEM_Term;.    
1a68a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61    pMem->z = (cha
1a68b 72 2a 29 73 71 6c 69 74 65 33 4f 70 63 6f 64 65  r*)sqlite3Opcode
1a68c 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65  Name(pOp->opcode
1a68d 29 3b 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f  );  /* Opcode */
1a68e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1a68f 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20  Mem->z!=0 );.   
1a690 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c     pMem->n = sql
1a691 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65  ite3Strlen30(pMe
1a692 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65  m->z);.      pMe
1a693 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1a694 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65  _TEXT;.      pMe
1a695 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  m->enc = SQLITE_
1a696 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d  UTF8;.      pMem
1a697 2b 2b 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70  ++;..      if( p
1a698 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53  Op->p4type==P4_S
1a699 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20  UBPROGRAM ){.   
1a69a 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d       int nByte =
1a69b 20 28 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66   (nSub+1)*sizeof
1a69c 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20  (SubProgram*);. 
1a69d 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20         int j;.  
1a69e 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
1a69f 3c 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20  <nSub; j++){.   
1a6a0 20 20 20 20 20 20 20 69 66 28 20 61 70 53 75 62         if( apSub
1a6a1 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72  [j]==pOp->p4.pPr
1a6a2 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20  ogram ) break;. 
1a6a3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1a6a4 20 69 66 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20   if( j==nSub && 
1a6a5 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74  SQLITE_OK==sqlit
1a6a6 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 53  e3VdbeMemGrow(pS
1a6a7 75 62 2c 20 6e 42 79 74 65 2c 20 31 29 20 29 7b  ub, nByte, 1) ){
1a6a8 0a 20 20 20 20 20 20 20 20 20 20 61 70 53 75 62  .          apSub
1a6a9 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a   = (SubProgram *
1a6aa 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 20  *)pSub->z;.     
1a6ab 20 20 20 20 20 61 70 53 75 62 5b 6e 53 75 62 2b       apSub[nSub+
1a6ac 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72  +] = pOp->p4.pPr
1a6ad 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 20  ogram;.         
1a6ae 20 70 53 75 62 2d 3e 66 6c 61 67 73 20 7c 3d 20   pSub->flags |= 
1a6af 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20  MEM_Blob;.      
1a6b0 20 20 20 20 70 53 75 62 2d 3e 6e 20 3d 20 6e 53      pSub->n = nS
1a6b1 75 62 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 6f  ub*sizeof(SubPro
1a6b2 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20 20 20  gram*);.        
1a6b3 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
1a6b4 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
1a6b5 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1a6b6 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d  pMem->u.i = pOp-
1a6b7 3e 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  >p1;            
1a6b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a6b9 20 50 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d   P1 */.    pMem-
1a6ba 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
1a6bb 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d  NTEGER;.    pMem
1a6bc 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66  ++;..    pMem->f
1a6bd 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1a6be 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
1a6bf 70 4f 70 2d 3e 70 32 3b 20 20 20 20 20 20 20 20  pOp->p2;        
1a6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6c1 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70    /* P2 */.    p
1a6c2 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a6c3 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  TE_INTEGER;.    
1a6c4 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28  pMem++;..    if(
1a6c5 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29   p->explain==1 )
1a6c6 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  {.      pMem->fl
1a6c7 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1a6c8 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d       pMem->u.i =
1a6c9 20 70 4f 70 2d 3e 70 33 3b 20 20 20 20 20 20 20   pOp->p3;       
1a6ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6cb 20 20 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20     /* P3 */.    
1a6cc 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
1a6cd 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20  QLITE_INTEGER;. 
1a6ce 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20       pMem++;.   
1a6cf 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69   }..    if( sqli
1a6d0 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
1a6d1 4d 65 6d 2c 20 33 32 2c 20 30 29 20 29 7b 20 20  Mem, 32, 0) ){  
1a6d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20            /* P4 
1a6d3 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
1a6d4 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
1a6d5 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65  iled );.      re
1a6d6 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1a6d7 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65  R;.    }.    pMe
1a6d8 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44  m->flags = MEM_D
1a6d9 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54  yn|MEM_Str|MEM_T
1a6da 65 72 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73  erm;.    z = dis
1a6db 70 6c 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d  playP4(pOp, pMem
1a6dc 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 66  ->z, 32);.    if
1a6dd 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a  ( z!=pMem->z ){.
1a6de 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1a6df 65 4d 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c  eMemSetStr(pMem,
1a6e0 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55   z, -1, SQLITE_U
1a6e1 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c  TF8, 0);.    }el
1a6e2 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
1a6e3 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a  ( pMem->z!=0 );.
1a6e4 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20        pMem->n = 
1a6e5 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1a6e6 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20  pMem->z);.      
1a6e7 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
1a6e8 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20  TE_UTF8;.    }. 
1a6e9 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1a6ea 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20  SQLITE_TEXT;.   
1a6eb 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66   pMem++;..    if
1a6ec 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20  ( p->explain==1 
1a6ed 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
1a6ee 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1a6ef 70 4d 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20  pMem, 4, 0) ){. 
1a6f0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1a6f1 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1a6f2 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65  ed );.        re
1a6f3 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1a6f4 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  R;.      }.     
1a6f5 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1a6f6 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d  EM_Dyn|MEM_Str|M
1a6f7 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  EM_Term;.      p
1a6f8 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20  Mem->n = 2;.    
1a6f9 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1a6fa 74 66 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22  tf(3, pMem->z, "
1a6fb 25 2e 32 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b  %.2x", pOp->p5);
1a6fc 20 20 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20     /* P5 */.    
1a6fd 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
1a6fe 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20  QLITE_TEXT;.    
1a6ff 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51    pMem->enc = SQ
1a700 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20  LITE_UTF8;.     
1a701 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64   pMem++;.  .#ifd
1a702 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1a703 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a        if( pOp->z
1a704 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20  Comment ){.     
1a705 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a706 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72   MEM_Str|MEM_Ter
1a707 6d 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  m;.        pMem-
1a708 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65  >z = pOp->zComme
1a709 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d  nt;.        pMem
1a70a 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ->n = sqlite3Str
1a70b 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a  len30(pMem->z);.
1a70c 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e          pMem->en
1a70d 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
1a70e 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74  .        pMem->t
1a70f 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58  ype = SQLITE_TEX
1a710 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23  T;.      }else.#
1a711 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20  endif.      {.  
1a712 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1a713 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20  s = MEM_Null;   
1a714 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a715 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a      /* Comment *
1a716 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  /.        pMem->
1a717 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55  type = SQLITE_NU
1a718 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  LL;.      }.    
1a719 7d 0a 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f  }..    p->nResCo
1a71a 6c 75 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d  lumn = 8 - 5*(p-
1a71b 3e 65 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20  >explain-1);.   
1a71c 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
1a71d 4f 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  OK;.    rc = SQL
1a71e 49 54 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72  ITE_ROW;.  }.  r
1a71f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
1a720 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1a721 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69  T_EXPLAIN */..#i
1a722 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1a723 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68  G./*.** Print th
1a724 65 20 53 51 4c 20 74 68 61 74 20 77 61 73 20 75  e SQL that was u
1a725 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
1a726 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a  a VDBE program..
1a727 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a728 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a729 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20  bePrintSql(Vdbe 
1a72a 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d  *p){.  int nOp =
1a72b 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f   p->nOp;.  VdbeO
1a72c 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f  p *pOp;.  if( nO
1a72d 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  p<1 ) return;.  
1a72e 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d  pOp = &p->aOp[0]
1a72f 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63  ;.  if( pOp->opc
1a730 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26  ode==OP_Trace &&
1a731 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b   pOp->p4.z!=0 ){
1a732 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1a733 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  *z = pOp->p4.z;.
1a734 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
1a735 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20  e3Isspace(*z) ) 
1a736 7a 2b 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28  z++;.    printf(
1a737 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a  "SQL: [%s]\n", z
1a738 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  );.  }.}.#endif.
1a739 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1a73a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29  LITE_OMIT_TRACE)
1a73b 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
1a73c 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43  TE_ENABLE_IOTRAC
1a73d 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61  E)./*.** Print a
1a73e 6e 20 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67  n IOTRACE messag
1a73f 65 20 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f  e showing SQL co
1a740 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ntent..*/.SQLITE
1a741 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a742 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65  lite3VdbeIOTrace
1a743 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  Sql(Vdbe *p){.  
1a744 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70  int nOp = p->nOp
1a745 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b  ;.  VdbeOp *pOp;
1a746 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f  .  if( sqlite3Io
1a747 54 72 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72  Trace==0 ) retur
1a748 6e 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29  n;.  if( nOp<1 )
1a749 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d   return;.  pOp =
1a74a 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69   &p->aOp[0];.  i
1a74b 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1a74c 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d  OP_Trace && pOp-
1a74d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20  >p4.z!=0 ){.    
1a74e 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68  int i, j;.    ch
1a74f 61 72 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20  ar z[1000];.    
1a750 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1a751 28 73 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22  (sizeof(z), z, "
1a752 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b  %s", pOp->p4.z);
1a753 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71  .    for(i=0; sq
1a754 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69  lite3Isspace(z[i
1a755 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66  ]); i++){}.    f
1a756 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b  or(j=0; z[i]; i+
1a757 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  +){.      if( sq
1a758 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69  lite3Isspace(z[i
1a759 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ]) ){.        if
1a75a 28 20 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b  ( z[i-1]!=' ' ){
1a75b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b  .          z[j++
1a75c 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20  ] = ' ';.       
1a75d 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
1a75e 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
1a75f 20 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20   z[i];.      }. 
1a760 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20     }.    z[j] = 
1a761 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6f  0;.    sqlite3Io
1a762 54 72 61 63 65 28 22 53 51 4c 20 25 73 5c 6e 22  Trace("SQL %s\n"
1a763 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  , z);.  }.}.#end
1a764 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d  if /* !SQLITE_OM
1a765 49 54 5f 54 52 41 43 45 20 26 26 20 53 51 4c 49  IT_TRACE && SQLI
1a766 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43  TE_ENABLE_IOTRAC
1a767 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  E */../*.** Allo
1a768 63 61 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20  cate space from 
1a769 61 20 66 69 78 65 64 20 73 69 7a 65 20 62 75 66  a fixed size buf
1a76a 66 65 72 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70  fer.  Make *pp p
1a76b 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61  oint to the.** a
1a76c 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20  llocated space. 
1a76d 20 28 4e 6f 74 65 3a 20 20 70 70 20 69 73 20 61   (Note:  pp is a
1a76e 20 63 68 61 72 2a 20 72 61 74 68 65 72 20 74 68   char* rather th
1a76f 61 6e 20 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a  an a void** to.*
1a770 2a 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  * work around th
1a771 65 20 70 6f 69 6e 74 65 72 20 61 6c 69 61 73 69  e pointer aliasi
1a772 6e 67 20 72 75 6c 65 73 20 6f 66 20 43 2e 29 20  ng rules of C.) 
1a773 20 2a 70 70 20 73 68 6f 75 6c 64 20 69 6e 69 74   *pp should init
1a774 69 61 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f  ially.** be zero
1a775 2e 20 20 49 66 20 2a 70 70 20 69 73 20 6e 6f 74  .  If *pp is not
1a776 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e   zero, that mean
1a777 73 20 74 68 61 74 20 74 68 65 20 73 70 61 63 65  s that the space
1a778 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20   has already.** 
1a779 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61  been allocated a
1a77a 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
1a77b 69 73 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a  is a noop..**.**
1a77c 20 6e 42 79 74 65 20 69 73 20 74 68 65 20 6e 75   nByte is the nu
1a77d 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1a77e 20 73 70 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a   space needed..*
1a77f 2a 0a 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69  *.** *ppFrom poi
1a780 6e 74 20 74 6f 20 61 76 61 69 6c 61 62 6c 65 20  nt to available 
1a781 73 70 61 63 65 20 61 6e 64 20 70 45 6e 64 20 70  space and pEnd p
1a782 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 65 6e 64  oints to the end
1a783 20 6f 66 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c   of the.** avail
1a784 61 62 6c 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a  able space..**.*
1a785 2a 20 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63  * *pnByte is a c
1a786 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75  ounter of the nu
1a787 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1a788 20 73 70 61 63 65 20 74 68 61 74 20 68 61 76 65   space that have
1a789 20 66 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c   failed.** to al
1a78a 6c 6f 63 61 74 65 2e 20 20 49 66 20 74 68 65 72  locate.  If ther
1a78b 65 20 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e  e is insufficien
1a78c 74 20 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72  t space in *ppFr
1a78d 6f 6d 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  om to satisfy th
1a78e 65 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68  e.** request, th
1a78f 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e  en increment *pn
1a790 42 79 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75  Byte by the amou
1a791 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73  nt of the reques
1a792 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
1a793 64 20 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20  d allocSpace(.  
1a794 63 68 61 72 20 2a 70 70 2c 20 20 20 20 20 20 20  char *pp,       
1a795 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
1a796 53 65 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74  Set *pp to point
1a797 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20 62 75   to allocated bu
1a798 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ffer */.  int nB
1a799 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  yte,           /
1a79a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1a79b 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f  s to allocate */
1a79c 0a 20 20 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20  .  u8 **ppFrom, 
1a79d 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55          /* IN/OU
1a79e 54 3a 20 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d  T: Allocate from
1a79f 20 2a 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38   *ppFrom */.  u8
1a7a0 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20   *pEnd,         
1a7a1 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1a7a2 20 31 20 62 79 74 65 20 70 61 73 74 20 74 68 65   1 byte past the
1a7a3 20 65 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20   end of *ppFrom 
1a7a4 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20  buffer */.  int 
1a7a5 2a 70 6e 42 79 74 65 20 20 20 20 20 20 20 20 20  *pnByte         
1a7a6 20 2f 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f   /* If allocatio
1a7a7 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65  n cannot be made
1a7a8 2c 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42  , increment *pnB
1a7a9 79 74 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65  yte */.){.  asse
1a7aa 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
1a7ab 4c 49 47 4e 4d 45 4e 54 28 2a 70 70 46 72 6f 6d  LIGNMENT(*ppFrom
1a7ac 29 20 29 3b 0a 20 20 69 66 28 20 28 2a 28 76 6f  ) );.  if( (*(vo
1a7ad 69 64 2a 2a 29 70 70 29 3d 3d 30 20 29 7b 0a 20  id**)pp)==0 ){. 
1a7ae 20 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44     nByte = ROUND
1a7af 38 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66  8(nByte);.    if
1a7b0 28 20 26 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79  ( &(*ppFrom)[nBy
1a7b1 74 65 5d 20 3c 3d 20 70 45 6e 64 20 29 7b 0a 20  te] <= pEnd ){. 
1a7b2 20 20 20 20 20 2a 28 76 6f 69 64 2a 2a 29 70 70       *(void**)pp
1a7b3 20 3d 20 28 76 6f 69 64 20 2a 29 2a 70 70 46 72   = (void *)*ppFr
1a7b4 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 70 46 72 6f  om;.      *ppFro
1a7b5 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20  m += nByte;.    
1a7b6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 6e  }else{.      *pn
1a7b7 42 79 74 65 20 2b 3d 20 6e 42 79 74 65 3b 0a 20  Byte += nByte;. 
1a7b8 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
1a7b9 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72 74  * Prepare a virt
1a7ba 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72 20  ual machine for 
1a7bb 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69 73  execution.  This
1a7bc 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67 73   involves things
1a7bd 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f   such.** as allo
1a7be 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70 61  cating stack spa
1a7bf 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  ce and initializ
1a7c0 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d 20  ing the program 
1a7c1 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65  counter..** Afte
1a7c2 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20 62  r the VDBE has b
1a7c3 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63 61  e prepped, it ca
1a7c4 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62 79  n be executed by
1a7c5 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20   one or more.** 
1a7c6 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
1a7c7 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a  VdbeExec().  .**
1a7c8 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
1a7c9 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65  only way to move
1a7ca 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44 42   a VDBE from VDB
1a7cb 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a  E_MAGIC_INIT to.
1a7cc 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  ** VDBE_MAGIC_RU
1a7cd 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  N..**.** This fu
1a7ce 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61  nction may be ca
1a7cf 6c 6c 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f  lled more than o
1a7d0 6e 63 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  nce on a single 
1a7d1 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
1a7d2 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 63 61  .** The first ca
1a7d3 6c 6c 20 69 73 20 6d 61 64 65 20 77 68 69 6c 65  ll is made while
1a7d4 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 53   compiling the S
1a7d5 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 53 75  QL statement. Su
1a7d6 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c  bsequent.** call
1a7d7 73 20 61 72 65 20 6d 61 64 65 20 61 73 20 70 61  s are made as pa
1a7d8 72 74 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73  rt of the proces
1a7d9 73 20 6f 66 20 72 65 73 65 74 74 69 6e 67 20 61  s of resetting a
1a7da 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65   statement to be
1a7db 0a 2a 2a 20 72 65 2d 65 78 65 63 75 74 65 64 20  .** re-executed 
1a7dc 28 66 72 6f 6d 20 61 20 63 61 6c 6c 20 74 6f 20  (from a call to 
1a7dd 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 29  sqlite3_reset())
1a7de 2e 20 54 68 65 20 6e 56 61 72 2c 20 6e 4d 65 6d  . The nVar, nMem
1a7df 2c 20 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 61 6e  , nCursor .** an
1a7e0 64 20 69 73 45 78 70 6c 61 69 6e 20 70 61 72 61  d isExplain para
1a7e1 6d 65 74 65 72 73 20 61 72 65 20 6f 6e 6c 79 20  meters are only 
1a7e2 70 61 73 73 65 64 20 63 6f 72 72 65 63 74 20 76  passed correct v
1a7e3 61 6c 75 65 73 20 74 68 65 20 66 69 72 73 74 20  alues the first 
1a7e4 74 69 6d 65 0a 2a 2a 20 74 68 65 20 66 75 6e 63  time.** the func
1a7e5 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20  tion is called. 
1a7e6 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  On subsequent ca
1a7e7 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 6c 69 74 65  lls, from sqlite
1a7e8 33 5f 72 65 73 65 74 28 29 2c 20 6e 56 61 72 0a  3_reset(), nVar.
1a7e9 2a 2a 20 69 73 20 70 61 73 73 65 64 20 2d 31 20  ** is passed -1 
1a7ea 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f  and nMem, nCurso
1a7eb 72 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e 20  r and isExplain 
1a7ec 61 72 65 20 61 6c 6c 20 70 61 73 73 65 64 20 7a  are all passed z
1a7ed 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ero..*/.SQLITE_P
1a7ee 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a7ef 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
1a7f0 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20  (.  Vdbe *p,    
1a7f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a7f2 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a     /* The VDBE *
1a7f3 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20  /.  int nVar,   
1a7f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a7f5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1a7f6 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 53  '?' see in the S
1a7f7 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  QL statement */.
1a7f8 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20    int nMem,     
1a7f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a7fa 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
1a7fb 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c  mory cells to al
1a7fc 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20  locate */.  int 
1a7fd 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20  nCursor,        
1a7fe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1a7ff 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20  mber of cursors 
1a800 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20  to allocate */. 
1a801 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20   int nArg,      
1a802 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a803 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  /* Maximum numbe
1a804 72 20 6f 66 20 61 72 67 73 20 69 6e 20 53 75 62  r of args in Sub
1a805 50 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e  Programs */.  in
1a806 74 20 69 73 45 78 70 6c 61 69 6e 2c 20 20 20 20  t isExplain,    
1a807 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a808 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50 4c  True if the EXPL
1a809 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69 73 20  AIN keywords is 
1a80a 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  present */.  int
1a80b 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c   usesStmtJournal
1a80c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1a80d 72 75 65 20 74 6f 20 73 65 74 20 56 64 62 65 2e  rue to set Vdbe.
1a80e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20  usesStmtJournal 
1a80f 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20  */.){.  int n;. 
1a810 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1a811 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  ->db;..  assert(
1a812 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   p!=0 );.  asser
1a813 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
1a814 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
1a815 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75  .  /* There shou
1a816 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f  ld be at least o
1a817 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a  ne opcode..  */.
1a818 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70    assert( p->nOp
1a819 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  >0 );..  /* Set 
1a81a 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42  the magic to VDB
1a81b 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e  E_MAGIC_RUN soon
1a81c 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c  er rather than l
1a81d 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61  ater. */.  p->ma
1a81e 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43  gic = VDBE_MAGIC
1a81f 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20  _RUN;..  /* For 
1a820 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 71 75  each cursor requ
1a821 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63  ired, also alloc
1a822 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c  ate a memory cel
1a823 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63  l. Memory.  ** c
1a824 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75  ells (nMem+1-nCu
1a825 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63  rsor)..nMem, inc
1a826 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76  lusive, will nev
1a827 65 72 20 62 65 20 75 73 65 64 20 62 79 0a 20 20  er be used by.  
1a828 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  ** the vdbe prog
1a829 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 65  ram. Instead the
1a82a 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 6c  y are used to al
1a82b 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
1a82c 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72  .  ** VdbeCursor
1a82d 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 63 74  /BtCursor struct
1a82e 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f  ures. The blob o
1a82f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  f memory associa
1a830 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 63  ted with .  ** c
1a831 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f 72 65  ursor 0 is store
1a832 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  d in memory cell
1a833 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65   nMem. Memory ce
1a834 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a  ll (nMem-1).  **
1a835 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c 6f 62   stores the blob
1a836 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63   of memory assoc
1a837 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f  iated with curso
1a838 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20  r 1, etc..  **. 
1a839 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c   ** See also: al
1a83a 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0a  locateCursor()..
1a83b 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e    */.  nMem += n
1a83c 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c  Cursor;..  /* Al
1a83d 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
1a83e 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72   memory register
1a83f 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73  s, SQL variables
1a840 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 61  , VDBE cursors a
1a841 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61  nd .  ** an arra
1a842 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c  y to marshal SQL
1a843 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
1a844 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 73 20  nts in. This is 
1a845 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 20  only done the.  
1a846 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  ** first time th
1a847 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1a848 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 76 65  alled for a give
1a849 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e  n VDBE, not when
1a84a 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e   it is.  ** bein
1a84b 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71  g called from sq
1a84c 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 74 6f  lite3_reset() to
1a84d 20 72 65 73 65 74 20 74 68 65 20 76 69 72 74 75   reset the virtu
1a84e 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f  al machine..  */
1a84f 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 26  .  if( nVar>=0 &
1a850 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6d 61 6c  & ALWAYS(db->mal
1a851 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 29 20 29 7b  locFailed==0) ){
1a852 0a 20 20 20 20 75 38 20 2a 7a 43 73 72 20 3d 20  .    u8 *zCsr = 
1a853 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d  (u8 *)&p->aOp[p-
1a854 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 38 20 2a 7a  >nOp];.    u8 *z
1a855 45 6e 64 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e  End = (u8 *)&p->
1a856 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d  aOp[p->nOpAlloc]
1a857 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b  ;.    int nByte;
1a858 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 56 61  .    resolveP2Va
1a859 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a  lues(p, &nArg);.
1a85a 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a      p->usesStmtJ
1a85b 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65  ournal = (u8)use
1a85c 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 0a 20 20  sStmtJournal;.  
1a85d 20 20 69 66 28 20 69 73 45 78 70 6c 61 69 6e 20    if( isExplain 
1a85e 26 26 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 20  && nMem<10 ){.  
1a85f 20 20 20 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20      nMem = 10;. 
1a860 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28     }.    memset(
1a861 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64 2d 7a 43  zCsr, 0, zEnd-zC
1a862 73 72 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d  sr);.    zCsr +=
1a863 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29 30 29   (zCsr - (u8*)0)
1a864 26 37 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  &7;.    assert( 
1a865 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e  EIGHT_BYTE_ALIGN
1a866 4d 45 4e 54 28 7a 43 73 72 29 20 29 3b 0a 0a 20  MENT(zCsr) );.. 
1a867 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 6e 42     do {.      nB
1a868 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 61  yte = 0;.      a
1a869 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a  llocSpace((char*
1a86a 29 26 70 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 6d 2a  )&p->aMem, nMem*
1a86b 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a 43  sizeof(Mem), &zC
1a86c 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65  sr, zEnd, &nByte
1a86d 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70  );.      allocSp
1a86e 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61  ace((char*)&p->a
1a86f 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66  Var, nVar*sizeof
1a870 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45  (Mem), &zCsr, zE
1a871 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20  nd, &nByte);.   
1a872 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63     allocSpace((c
1a873 68 61 72 2a 29 26 70 2d 3e 61 70 41 72 67 2c 20  har*)&p->apArg, 
1a874 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d 2a  nArg*sizeof(Mem*
1a875 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20  ), &zCsr, zEnd, 
1a876 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61  &nByte);.      a
1a877 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a  llocSpace((char*
1a878 29 26 70 2d 3e 61 7a 56 61 72 2c 20 6e 56 61 72  )&p->azVar, nVar
1a879 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2c 20  *sizeof(char*), 
1a87a 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42  &zCsr, zEnd, &nB
1a87b 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f  yte);.      allo
1a87c 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70  cSpace((char*)&p
1a87d 2d 3e 61 70 43 73 72 2c 20 0a 20 20 20 20 20 20  ->apCsr, .      
1a87e 20 20 20 20 20 20 20 20 20 20 20 6e 43 75 72 73             nCurs
1a87f 6f 72 2a 73 69 7a 65 6f 66 28 56 64 62 65 43 75  or*sizeof(VdbeCu
1a880 72 73 6f 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a  rsor*), &zCsr, z
1a881 45 6e 64 2c 20 26 6e 42 79 74 65 0a 20 20 20 20  End, &nByte.    
1a882 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e    );.      if( n
1a883 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Byte ){.        
1a884 70 2d 3e 70 46 72 65 65 20 3d 20 73 71 6c 69 74  p->pFree = sqlit
1a885 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
1a886 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  b, nByte);.     
1a887 20 7d 0a 20 20 20 20 20 20 7a 43 73 72 20 3d 20   }.      zCsr = 
1a888 70 2d 3e 70 46 72 65 65 3b 0a 20 20 20 20 20 20  p->pFree;.      
1a889 7a 45 6e 64 20 3d 20 26 7a 43 73 72 5b 6e 42 79  zEnd = &zCsr[nBy
1a88a 74 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  te];.    }while(
1a88b 20 6e 42 79 74 65 20 26 26 20 21 64 62 2d 3e 6d   nByte && !db->m
1a88c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a  allocFailed );..
1a88d 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d      p->nCursor =
1a88e 20 28 75 31 36 29 6e 43 75 72 73 6f 72 3b 0a 20   (u16)nCursor;. 
1a88f 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20 29     if( p->aVar )
1a890 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20  {.      p->nVar 
1a891 3d 20 28 79 6e 56 61 72 29 6e 56 61 72 3b 0a 20  = (ynVar)nVar;. 
1a892 20 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c       for(n=0; n<
1a893 6e 56 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20  nVar; n++){.    
1a894 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66      p->aVar[n].f
1a895 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1a896 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 56 61 72  .        p->aVar
1a897 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20  [n].db = db;.   
1a898 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1a899 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20  f( p->aMem ){.  
1a89a 20 20 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20      p->aMem--;  
1a89b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a89c 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f      /* aMem[] go
1a89d 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20  es from 1..nMem 
1a89e 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d  */.      p->nMem
1a89f 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20   = nMem;        
1a8a0 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20 20           /*     
1a8a1 20 20 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d    not from 0..nM
1a8a2 65 6d 2d 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f  em-1 */.      fo
1a8a3 72 28 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20  r(n=1; n<=nMem; 
1a8a4 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  n++){.        p-
1a8a5 3e 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d  >aMem[n].flags =
1a8a6 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20   MEM_Null;.     
1a8a7 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62     p->aMem[n].db
1a8a8 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20   = db;.      }. 
1a8a9 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20     }.  }.#ifdef 
1a8aa 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66  SQLITE_DEBUG.  f
1a8ab 6f 72 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65  or(n=1; n<p->nMe
1a8ac 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73  m; n++){.    ass
1a8ad 65 72 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e  ert( p->aMem[n].
1a8ae 64 62 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65  db==db );.  }.#e
1a8af 6e 64 69 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20  ndif..  p->pc = 
1a8b0 2d 31 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51  -1;.  p->rc = SQ
1a8b1 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72  LITE_OK;.  p->er
1a8b2 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41  rorAction = OE_A
1a8b3 62 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61  bort;.  p->expla
1a8b4 69 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b  in |= isExplain;
1a8b5 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
1a8b6 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20  BE_MAGIC_RUN;.  
1a8b7 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a  p->nChange = 0;.
1a8b8 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20    p->cacheCtr = 
1a8b9 31 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65  1;.  p->minWrite
1a8ba 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35  FileFormat = 255
1a8bb 3b 0a 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  ;.  p->iStatemen
1a8bc 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44  t = 0;.#ifdef VD
1a8bd 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20  BE_PROFILE.  {. 
1a8be 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
1a8bf 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
1a8c0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e   i++){.      p->
1a8c1 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a  aOp[i].cnt = 0;.
1a8c2 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e        p->aOp[i].
1a8c3 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20  cycles = 0;.    
1a8c4 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  }.  }.#endif.}..
1a8c5 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44  /*.** Close a VD
1a8c6 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65  BE cursor and re
1a8c7 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65  lease all the re
1a8c8 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72  sources that cur
1a8c9 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20  sor .** happens 
1a8ca 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49  to hold..*/.SQLI
1a8cb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a8cc 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
1a8cd 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56  ursor(Vdbe *p, V
1a8ce 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b  dbeCursor *pCx){
1a8cf 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b  .  if( pCx==0 ){
1a8d0 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1a8d1 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20  .  if( pCx->pBt 
1a8d2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
1a8d3 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42  reeClose(pCx->pB
1a8d4 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70  t);.    /* The p
1a8d5 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c  Cx->pCursor will
1a8d6 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61   be close automa
1a8d7 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65  tically, if it e
1a8d8 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a  xists, by.    **
1a8d9 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e   the call above.
1a8da 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
1a8db 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a  pCx->pCursor ){.
1a8dc 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1a8dd 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d  CloseCursor(pCx-
1a8de 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23  >pCursor);.  }.#
1a8df 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a8e0 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1a8e1 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62    if( pCx->pVtab
1a8e2 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71  Cursor ){.    sq
1a8e3 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1a8e4 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d  r *pVtabCursor =
1a8e5 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f   pCx->pVtabCurso
1a8e6 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  r;.    const sql
1a8e7 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1a8e8 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64  dule = pCx->pMod
1a8e9 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74  ule;.    p->inVt
1a8ea 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20  abMethod = 1;.  
1a8eb 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1a8ec 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b  afetyOff(p->db);
1a8ed 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  .    pModule->xC
1a8ee 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72  lose(pVtabCursor
1a8ef 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  );.    (void)sql
1a8f0 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e  ite3SafetyOn(p->
1a8f1 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74  db);.    p->inVt
1a8f2 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1a8f3 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
1a8f4 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65  * Copy the value
1a8f5 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
1a8f6 56 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74  VdbeFrame struct
1a8f7 75 72 65 20 74 6f 20 69 74 73 20 56 64 62 65 2e  ure to its Vdbe.
1a8f8 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64   This.** is used
1a8f9 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77  , for example, w
1a8fa 68 65 6e 20 61 20 74 72 69 67 67 65 72 20 73 75  hen a trigger su
1a8fb 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 68 61 6c  b-program is hal
1a8fc 74 65 64 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a  ted to restore.*
1a8fd 2a 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65  * control to the
1a8fe 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a   main program..*
1a8ff 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a900 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1a901 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62  FrameRestore(Vdb
1a902 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b  eFrame *pFrame){
1a903 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72  .  Vdbe *v = pFr
1a904 61 6d 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70  ame->v;.  v->aOp
1a905 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a   = pFrame->aOp;.
1a906 20 20 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d    v->nOp = pFram
1a907 65 2d 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65  e->nOp;.  v->aMe
1a908 6d 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d  m = pFrame->aMem
1a909 3b 0a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46  ;.  v->nMem = pF
1a90a 72 61 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d  rame->nMem;.  v-
1a90b 3e 61 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d  >apCsr = pFrame-
1a90c 3e 61 70 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75  >apCsr;.  v->nCu
1a90d 72 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e  rsor = pFrame->n
1a90e 43 75 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d  Cursor;.  v->db-
1a90f 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72  >lastRowid = pFr
1a910 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a  ame->lastRowid;.
1a911 20 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70    v->nChange = p
1a912 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a  Frame->nChange;.
1a913 20 20 72 65 74 75 72 6e 20 70 46 72 61 6d 65 2d    return pFrame-
1a914 3e 70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  >pc;.}../*.** Cl
1a915 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e  ose all cursors.
1a916 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65  .**.** Also rele
1a917 61 73 65 20 61 6e 79 20 64 79 6e 61 6d 69 63 20  ase any dynamic 
1a918 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74  memory held by t
1a919 68 65 20 56 4d 20 69 6e 20 74 68 65 20 56 64 62  he VM in the Vdb
1a91a 65 2e 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a  e.aMem memory .*
1a91b 2a 20 63 65 6c 6c 20 61 72 72 61 79 2e 20 54 68  * cell array. Th
1a91c 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  is is necessary 
1a91d 61 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65  as the memory ce
1a91e 6c 6c 20 61 72 72 61 79 20 6d 61 79 20 63 6f 6e  ll array may con
1a91f 74 61 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73  tain.** pointers
1a920 20 74 6f 20 56 64 62 65 46 72 61 6d 65 20 6f 62   to VdbeFrame ob
1a921 6a 65 63 74 73 2c 20 77 68 69 63 68 20 6d 61 79  jects, which may
1a922 20 69 6e 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e   in turn contain
1a923 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
1a924 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f  open cursors..*/
1a925 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f  .static void clo
1a926 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62  seAllCursors(Vdb
1a927 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  e *p){.  if( p->
1a928 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64  pFrame ){.    Vd
1a929 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20  beFrame *pFrame 
1a92a 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20  = p->pFrame;.   
1a92b 20 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70   for(pFrame=p->p
1a92c 46 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70  Frame; pFrame->p
1a92d 50 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70  Parent; pFrame=p
1a92e 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b  Frame->pParent);
1a92f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1a930 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72  FrameRestore(pFr
1a931 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  ame);.  }.  p->p
1a932 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  Frame = 0;.  p->
1a933 6e 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69  nFrame = 0;..  i
1a934 66 28 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20  f( p->apCsr ){. 
1a935 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
1a936 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72  r(i=0; i<p->nCur
1a937 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  sor; i++){.     
1a938 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20   VdbeCursor *pC 
1a939 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20  = p->apCsr[i];. 
1a93a 20 20 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20       if( pC ){. 
1a93b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1a93c 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20  beFreeCursor(p, 
1a93d 70 43 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  pC);.        p->
1a93e 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20  apCsr[i] = 0;.  
1a93f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1a940 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b    if( p->aMem ){
1a941 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41  .    releaseMemA
1a942 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d  rray(&p->aMem[1]
1a943 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a  , p->nMem);.  }.
1a944 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75  }../*.** Clean u
1a945 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 65  p the VM after e
1a946 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  xecution..**.** 
1a947 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
1a948 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
1a949 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72  close any cursor
1a94a 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72  s, lists, and/or
1a94b 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74  .** sorters that
1a94c 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e   were left open.
1a94d 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65    It also delete
1a94e 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a  s the values of.
1a94f 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20  ** variables in 
1a950 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79  the aVar[] array
1a951 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1a952 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70   Cleanup(Vdbe *p
1a953 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1a954 20 3d 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65   = p->db;..#ifde
1a955 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
1a956 20 2f 2a 20 45 78 65 63 75 74 65 20 61 73 73 65   /* Execute asse
1a957 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
1a958 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  to ensure that t
1a959 68 65 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20  he Vdbe.apCsr[] 
1a95a 61 6e 64 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61  and .  ** Vdbe.a
1a95b 4d 65 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76  Mem[] arrays hav
1a95c 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  e already been c
1a95d 6c 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20  leaned up.  */. 
1a95e 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
1a95f 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b  0; i<p->nCursor;
1a960 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d   i++) assert( p-
1a961 3e 61 70 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e  >apCsr==0 || p->
1a962 61 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20  apCsr[i]==0 );. 
1a963 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e   for(i=1; i<=p->
1a964 6e 4d 65 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72  nMem; i++) asser
1a965 74 28 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c  t( p->aMem==0 ||
1a966 20 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67   p->aMem[i].flag
1a967 73 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23  s==MEM_Null );.#
1a968 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
1a969 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1a96a 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72  rrMsg);.  p->zEr
1a96b 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70  rMsg = 0;.  p->p
1a96c 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d  ResultSet = 0;.}
1a96d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
1a96e 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74  number of result
1a96f 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69   columns that wi
1a970 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  ll be returned b
1a971 79 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74  y this SQL.** st
1a972 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  atement. This is
1a973 20 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70   now set at comp
1a974 69 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72  ile time, rather
1a975 20 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20   than during.** 
1a976 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65  execution of the
1a977 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f   vdbe program so
1a978 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f   that sqlite3_co
1a979 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e  lumn_count() can
1a97a 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e  .** be called on
1a97b 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
1a97c 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33  t before sqlite3
1a97d 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49  _step()..*/.SQLI
1a97e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a97f 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
1a980 6d 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69  mCols(Vdbe *p, i
1a981 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a  nt nResColumn){.
1a982 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b    Mem *pColName;
1a983 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69  .  int n;.  sqli
1a984 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
1a985 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72  ..  releaseMemAr
1a986 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c  ray(p->aColName,
1a987 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43   p->nResColumn*C
1a988 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c  OLNAME_N);.  sql
1a989 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1a98a 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e  ->aColName);.  n
1a98b 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f   = nResColumn*CO
1a98c 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52  LNAME_N;.  p->nR
1a98d 65 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29  esColumn = (u16)
1a98e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d  nResColumn;.  p-
1a98f 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c  >aColName = pCol
1a990 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c  Name = (Mem*)sql
1a991 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
1a992 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29  (db, sizeof(Mem)
1a993 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  *n );.  if( p->a
1a994 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74  ColName==0 ) ret
1a995 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d  urn;.  while( n-
1a996 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f  - > 0 ){.    pCo
1a997 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d  lName->flags = M
1a998 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f  EM_Null;.    pCo
1a999 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64  lName->db = p->d
1a99a 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b  b;.    pColName+
1a99b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  +;.  }.}../*.** 
1a99c 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  Set the name of 
1a99d 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d  the idx'th colum
1a99e 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  n to be returned
1a99f 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74   by the SQL stat
1a9a0 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20  ement..** zName 
1a9a1 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65  must be a pointe
1a9a2 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69  r to a nul termi
1a9a3 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a  nated string..**
1a9a4 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75  .** This call mu
1a9a5 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72  st be made after
1a9a6 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
1a9a7 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1a9a8 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ()..**.** The fi
1a9a9 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78  nal parameter, x
1a9aa 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65  Del, must be one
1a9ab 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d   of SQLITE_DYNAM
1a9ac 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  IC, SQLITE_STATI
1a9ad 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54  C.** or SQLITE_T
1a9ae 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20  RANSIENT. If it 
1a9af 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  is SQLITE_DYNAMI
1a9b0 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66  C, then the buff
1a9b1 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  er pointed.** to
1a9b2 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62   by zName will b
1a9b3 65 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74  e freed by sqlit
1a9b4 65 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20  e3DbFree() when 
1a9b5 74 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74  the vdbe is dest
1a9b6 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  royed..*/.SQLITE
1a9b7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1a9b8 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
1a9b9 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20  me(.  Vdbe *p,  
1a9ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9bb 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62         /* Vdbe b
1a9bc 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20  eing configured 
1a9bd 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20  */.  int idx,   
1a9be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9bf 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1a9c0 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61  f column zName a
1a9c1 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69  pplies to */.  i
1a9c2 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20  nt var,         
1a9c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9c4 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f  /* One of the CO
1a9c5 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74  LNAME_* constant
1a9c6 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
1a9c7 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
1a9c8 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
1a9c9 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e  er to buffer con
1a9ca 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a  taining name */.
1a9cb 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
1a9cc 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20  oid*)           
1a9cd 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e     /* Memory man
1a9ce 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79  agement strategy
1a9cf 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b   for zName */.){
1a9d0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d  .  int rc;.  Mem
1a9d1 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73   *pColName;.  as
1a9d2 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65  sert( idx<p->nRe
1a9d3 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73  sColumn );.  ass
1a9d4 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45  ert( var<COLNAME
1a9d5 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64  _N );.  if( p->d
1a9d6 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a9d7 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
1a9d8 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53  zName || xDel!=S
1a9d9 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b  QLITE_DYNAMIC );
1a9da 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1a9db 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
1a9dc 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e  assert( p->aColN
1a9dd 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c  ame!=0 );.  pCol
1a9de 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c  Name = &(p->aCol
1a9df 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e  Name[idx+var*p->
1a9e0 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20  nResColumn]);.  
1a9e1 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1a9e2 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61  MemSetStr(pColNa
1a9e3 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53  me, zName, -1, S
1a9e4 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c  QLITE_UTF8, xDel
1a9e5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
1a9e6 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20  =0 || !zName || 
1a9e7 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73  (pColName->flags
1a9e8 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b  &MEM_Term)!=0 );
1a9e9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1a9ea 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72  ./*.** A read or
1a9eb 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1a9ec 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f  on may or may no
1a9ed 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64  t be active on d
1a9ee 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a  atabase handle.*
1a9ef 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73  * db. If a trans
1a9f0 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
1a9f1 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20  , commit it. If 
1a9f2 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72  there is a.** wr
1a9f3 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
1a9f4 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68  spanning more th
1a9f5 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  an one database 
1a9f6 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69  file, this routi
1a9f7 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65  ne.** takes care
1a9f8 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a   of the master j
1a9f9 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e  ournal trickery.
1a9fa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
1a9fb 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65  dbeCommit(sqlite
1a9fc 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b  3 *db, Vdbe *p){
1a9fd 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1a9fe 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20  nTrans = 0;  /* 
1a9ff 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61  Number of databa
1aa00 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69  ses with an acti
1aa01 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  ve write-transac
1aa02 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  tion */.  int rc
1aa03 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1aa04 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20  int needXcommit 
1aa05 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  = 0;..#ifdef SQL
1aa06 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1aa07 54 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20  TABLE.  /* With 
1aa08 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c  this option, sql
1aa09 69 74 65 33 56 74 61 62 53 79 6e 63 28 29 20 69  ite3VtabSync() i
1aa0a 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20  s defined to be 
1aa0b 73 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c  simply .  ** SQL
1aa0c 49 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e  ITE_OK so p is n
1aa0d 6f 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20  ot used. .  */. 
1aa0e 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1aa0f 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  R(p);.#endif..  
1aa10 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  /* Before doing 
1aa11 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63  anything else, c
1aa12 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20  all the xSync() 
1aa13 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79  callback for any
1aa14 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f  .  ** virtual mo
1aa15 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74  dule tables writ
1aa16 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e  ten in this tran
1aa17 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61  saction. This ha
1aa18 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e  s to.  ** be don
1aa19 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69  e before determi
1aa1a 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d  ning whether a m
1aa1b 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1aa1c 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75  le is .  ** requ
1aa1d 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e  ired, as an xSyn
1aa1e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79  c() callback may
1aa1f 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 64   add an attached
1aa20 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74   database.  ** t
1aa21 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  o the transactio
1aa22 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  n..  */.  rc = s
1aa23 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 64  qlite3VtabSync(d
1aa24 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  b, &p->zErrMsg);
1aa25 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1aa26 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
1aa27 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
1aa28 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72   This loop deter
1aa29 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74 68 65  mines (a) if the
1aa2a 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f   commit hook sho
1aa2b 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61  uld be invoked a
1aa2c 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20  nd.  ** (b) how 
1aa2d 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20 66 69  many database fi
1aa2e 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72  les have open wr
1aa2f 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ite transactions
1aa30 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c  , not .  ** incl
1aa31 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64  uding the temp d
1aa32 61 74 61 62 61 73 65 2e 20 28 62 29 20 69 73 20  atabase. (b) is 
1aa33 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73  important becaus
1aa34 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a  e if more than .
1aa35 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73    ** one databas
1aa36 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70  e file has an op
1aa37 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  en write transac
1aa38 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a  tion, a master j
1aa39 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65  ournal.  ** file
1aa3a 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
1aa3b 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69   an atomic commi
1aa3c 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69  t..  */ .  for(i
1aa3d 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1aa3e 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20  ++){ .    Btree 
1aa3f 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *pBt = db->aDb[i
1aa40 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73  ].pBt;.    if( s
1aa41 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
1aa42 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20  rans(pBt) ){.   
1aa43 20 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d     needXcommit =
1aa44 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21   1;.      if( i!
1aa45 3d 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20  =1 ) nTrans++;. 
1aa46 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
1aa47 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
1aa48 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1aa49 6e 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b  ns at all, invok
1aa4a 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f  e the commit hoo
1aa4b 6b 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58  k */.  if( needX
1aa4c 63 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43  commit && db->xC
1aa4d 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b  ommitCallback ){
1aa4e 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
1aa4f 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1aa50 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43  .    rc = db->xC
1aa51 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62  ommitCallback(db
1aa52 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20  ->pCommitArg);. 
1aa53 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
1aa54 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
1aa55 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1aa56 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1aa57 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20  CONSTRAINT;.    
1aa58 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
1aa59 73 69 6d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f  simple case - no
1aa5a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64   more than one d
1aa5b 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f  atabase file (no
1aa5c 74 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20  t counting the. 
1aa5d 20 2a 2a 20 54 45 4d 50 20 64 61 74 61 62 61 73   ** TEMP databas
1aa5e 65 29 20 68 61 73 20 61 20 74 72 61 6e 73 61 63  e) has a transac
1aa5f 74 69 6f 6e 20 61 63 74 69 76 65 2e 20 20 20 54  tion active.   T
1aa60 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
1aa61 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73  for the.  ** mas
1aa62 74 65 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a  ter-journal..  *
1aa63 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65  *.  ** If the re
1aa64 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71  turn value of sq
1aa65 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c  lite3BtreeGetFil
1aa66 65 6e 61 6d 65 28 29 20 69 73 20 61 20 7a 65 72  ename() is a zer
1aa67 6f 20 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74  o length.  ** st
1aa68 72 69 6e 67 2c 20 69 74 20 6d 65 61 6e 73 20 74  ring, it means t
1aa69 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1aa6a 20 69 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20   is :memory: or 
1aa6b 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e  a temp file.  In
1aa6c 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65   .  ** that case
1aa6d 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f   we do not suppo
1aa6e 72 74 20 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d  rt atomic multi-
1aa6f 66 69 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f  file commits, so
1aa70 20 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 73   use the .  ** s
1aa71 69 6d 70 6c 65 20 63 61 73 65 20 74 68 65 6e 20  imple case then 
1aa72 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  too..  */.  if( 
1aa73 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  0==sqlite3Strlen
1aa74 33 30 28 73 71 6c 69 74 65 33 42 74 72 65 65 47  30(sqlite3BtreeG
1aa75 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61  etFilename(db->a
1aa76 44 62 5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c  Db[0].pBt)).   |
1aa77 7c 20 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b  | nTrans<=1.  ){
1aa78 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63  .    for(i=0; rc
1aa79 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
1aa7a 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
1aa7b 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
1aa7c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
1aa7d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  t;.      if( pBt
1aa7e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1aa7f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1aa80 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c  mitPhaseOne(pBt,
1aa81 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1aa82 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68   }..    /* Do th
1aa83 65 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66  e commit only if
1aa84 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73   all databases s
1aa85 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70  uccessfully comp
1aa86 6c 65 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20  lete phase 1. . 
1aa87 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20     ** If one of 
1aa88 74 68 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50  the BtreeCommitP
1aa89 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20  haseOne() calls 
1aa8a 66 61 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69  fails, this indi
1aa8b 63 61 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20  cates an.    ** 
1aa8c 49 4f 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64  IO error while d
1aa8d 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63  eleting or trunc
1aa8e 61 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20  ating a journal 
1aa8f 66 69 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69  file. It is unli
1aa90 6b 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74  kely,.    ** but
1aa91 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49   could happen. I
1aa92 6e 20 74 68 69 73 20 63 61 73 65 20 61 62 61 6e  n this case aban
1aa93 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61  don processing a
1aa94 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  nd return the er
1aa95 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ror..    */.    
1aa96 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
1aa97 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e  ITE_OK && i<db->
1aa98 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
1aa99 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
1aa9a 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
1aa9b 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
1aa9c 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1aa9d 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
1aa9e 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20  aseTwo(pBt);.   
1aa9f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1aaa0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1aaa1 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1aaa2 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b  3VtabCommit(db);
1aaa3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1aaa4 20 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73   The complex cas
1aaa5 65 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d  e - There is a m
1aaa6 75 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d  ulti-file write-
1aaa7 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69  transaction acti
1aaa8 76 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65  ve..  ** This re
1aaa9 71 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20  quires a master 
1aaaa 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20  journal file to 
1aaab 65 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73  ensure the trans
1aaac 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63  action is.  ** c
1aaad 6f 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c  ommitted atomicl
1aaae 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  y..  */.#ifndef 
1aaaf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
1aab0 49 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73  IO.  else{.    s
1aab1 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1aab2 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20   = db->pVfs;.   
1aab3 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20   int needSync = 
1aab4 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61  0;.    char *zMa
1aab5 73 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46  ster = 0;   /* F
1aab6 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65  ile-name for the
1aab7 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1aab8 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73  */.    char cons
1aab9 74 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73  t *zMainFile = s
1aaba 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69  qlite3BtreeGetFi
1aabb 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30  lename(db->aDb[0
1aabc 5d 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69  ].pBt);.    sqli
1aabd 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65  te3_file *pMaste
1aabe 72 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f  r = 0;.    i64 o
1aabf 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69  ffset = 0;.    i
1aac0 6e 74 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20  nt res;..    /* 
1aac1 53 65 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20  Select a master 
1aac2 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
1aac3 65 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20  e */.    do {.  
1aac4 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b      u32 iRandom;
1aac5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1aac6 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  Free(db, zMaster
1aac7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1aac8 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65  _randomness(size
1aac9 6f 66 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52  of(iRandom), &iR
1aaca 61 6e 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d  andom);.      zM
1aacb 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d  aster = sqlite3M
1aacc 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d  Printf(db, "%s-m
1aacd 6a 25 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c  j%08X", zMainFil
1aace 65 2c 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66  e, iRandom&0x7ff
1aacf 66 66 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66  fffff);.      if
1aad0 28 20 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20  ( !zMaster ){.  
1aad1 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1aad2 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
1aad3 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   }.      rc = sq
1aad4 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56  lite3OsAccess(pV
1aad5 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c  fs, zMaster, SQL
1aad6 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
1aad7 53 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77  S, &res);.    }w
1aad8 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
1aad9 5f 4f 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20  _OK && res );.  
1aada 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1aadb 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  _OK ){.      /* 
1aadc 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20  Open the master 
1aadd 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20  journal. */.    
1aade 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1aadf 4f 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c  OpenMalloc(pVfs,
1aae0 20 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74   zMaster, &pMast
1aae1 65 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53  er, .          S
1aae2 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
1aae3 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
1aae4 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20  _CREATE|.       
1aae5 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
1aae6 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f  XCLUSIVE|SQLITE_
1aae7 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
1aae8 4e 41 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a  NAL, 0.      );.
1aae9 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
1aaea 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1aaeb 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1aaec 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
1aaed 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1aaee 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a  ;.    }. .    /*
1aaef 20 57 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20   Write the name 
1aaf0 6f 66 20 65 61 63 68 20 64 61 74 61 62 61 73 65  of each database
1aaf1 20 66 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61   file in the tra
1aaf2 6e 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68  nsaction into th
1aaf3 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73  e new.    ** mas
1aaf4 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1aaf5 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
1aaf6 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69  curs at this poi
1aaf7 6e 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20  nt close.    ** 
1aaf8 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d  and delete the m
1aaf9 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1aafa 6c 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69  le. All the indi
1aafb 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66  vidual journal f
1aafc 69 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c  iles.    ** stil
1aafd 6c 20 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73  l have 'null' as
1aafe 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1aaff 6e 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20  nal pointer, so 
1ab00 74 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20  they will roll. 
1ab01 20 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70     ** back indep
1ab02 65 6e 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61  endently if a fa
1ab03 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20  ilure occurs..  
1ab04 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
1ab05 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1ab06 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a  ){.      Btree *
1ab07 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
1ab08 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20  .pBt;.      if( 
1ab09 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b  i==1 ) continue;
1ab0a 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65     /* Ignore the
1ab0b 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a   TEMP database *
1ab0c 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  /.      if( sqli
1ab0d 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
1ab0e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  s(pBt) ){.      
1ab0f 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46    char const *zF
1ab10 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ile = sqlite3Btr
1ab11 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65  eeGetJournalname
1ab12 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69  (pBt);.        i
1ab13 66 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29  f( zFile[0]==0 )
1ab14 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49   continue;  /* I
1ab15 67 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64  gnore :memory: d
1ab16 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20  atabases */.    
1ab17 20 20 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e      if( !needSyn
1ab18 63 20 26 26 20 21 73 71 6c 69 74 65 33 42 74 72  c && !sqlite3Btr
1ab19 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70  eeSyncDisabled(p
1ab1a 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Bt) ){.         
1ab1b 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20   needSync = 1;. 
1ab1c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1ab1d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
1ab1e 72 69 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46  rite(pMaster, zF
1ab1f 69 6c 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ile, sqlite3Strl
1ab20 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f  en30(zFile)+1, o
1ab21 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20  ffset);.        
1ab22 6f 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65  offset += sqlite
1ab23 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29  3Strlen30(zFile)
1ab24 2b 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +1;.        if( 
1ab25 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1ab26 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1ab27 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d  e3OsCloseFree(pM
1ab28 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20  aster);.        
1ab29 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74    sqlite3OsDelet
1ab2a 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c  e(pVfs, zMaster,
1ab2b 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   0);.          s
1ab2c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ab2d 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20   zMaster);.     
1ab2e 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1ab2f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1ab30 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
1ab31 53 79 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20  Sync the master 
1ab32 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66  journal file. If
1ab33 20 74 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45   the IOCAP_SEQUE
1ab34 4e 54 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20  NTIAL device.   
1ab35 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20   ** flag is set 
1ab36 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75  this is not requ
1ab37 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
1ab38 20 69 66 28 20 6e 65 65 64 53 79 6e 63 20 0a 20   if( needSync . 
1ab39 20 20 20 20 26 26 20 30 3d 3d 28 73 71 6c 69 74      && 0==(sqlit
1ab3a 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  e3OsDeviceCharac
1ab3b 74 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65  teristics(pMaste
1ab3c 72 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  r)&SQLITE_IOCAP_
1ab3d 53 45 51 55 45 4e 54 49 41 4c 29 0a 20 20 20 20  SEQUENTIAL).    
1ab3e 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   && SQLITE_OK!=(
1ab3f 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79  rc = sqlite3OsSy
1ab40 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 51 4c 49  nc(pMaster, SQLI
1ab41 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29  TE_SYNC_NORMAL))
1ab42 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71  .    ){.      sq
1ab43 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
1ab44 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  (pMaster);.     
1ab45 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1ab46 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20  (pVfs, zMaster, 
1ab47 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
1ab48 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73  3DbFree(db, zMas
1ab49 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ter);.      retu
1ab4a 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20  rn rc;.    }..  
1ab4b 20 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68    /* Sync all th
1ab4c 65 20 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c  e db files invol
1ab4d 76 65 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73  ved in the trans
1ab4e 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65  action. The same
1ab4f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74   call.    ** set
1ab50 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  s the master jou
1ab51 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20  rnal pointer in 
1ab52 65 61 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20  each individual 
1ab53 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20  journal. If.    
1ab54 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  ** an error occu
1ab55 72 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20  rs here, do not 
1ab56 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65  delete the maste
1ab57 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  r journal file..
1ab58 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66      **.    ** If
1ab59 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72   the error occur
1ab5a 73 20 64 75 72 69 6e 67 20 74 68 65 20 66 69 72  s during the fir
1ab5b 73 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a  st call to.    *
1ab5c 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  * sqlite3BtreeCo
1ab5d 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 2c 20  mmitPhaseOne(), 
1ab5e 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20  then there is a 
1ab5f 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 0a  chance that the.
1ab60 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f      ** master jo
1ab61 75 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20  urnal file will 
1ab62 62 65 20 6f 72 70 68 61 6e 65 64 2e 20 42 75 74  be orphaned. But
1ab63 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74   we cannot delet
1ab64 65 20 69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20  e it,.    ** in 
1ab65 63 61 73 65 20 74 68 65 20 6d 61 73 74 65 72 20  case the master 
1ab66 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
1ab67 65 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e  e was written in
1ab68 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  to the journal. 
1ab69 20 20 20 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72     ** file befor
1ab6a 65 20 74 68 65 20 66 61 69 6c 75 72 65 20 6f 63  e the failure oc
1ab6b 63 75 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  curred..    */. 
1ab6c 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d     for(i=0; rc==
1ab6d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64  SQLITE_OK && i<d
1ab6e 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20  b->nDb; i++){ . 
1ab6f 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
1ab70 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1ab71 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20  ;.      if( pBt 
1ab72 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1ab73 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
1ab74 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20  itPhaseOne(pBt, 
1ab75 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  zMaster);.      
1ab76 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1ab77 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70  te3OsCloseFree(p
1ab78 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28  Master);.    if(
1ab79 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1ab7a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
1ab7b 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65  bFree(db, zMaste
1ab7c 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  r);.      return
1ab7d 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   rc;.    }..    
1ab7e 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6d 61  /* Delete the ma
1ab7f 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
1ab80 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74 73 20  e. This commits 
1ab81 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
1ab82 20 41 66 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f   After.    ** do
1ab83 69 6e 67 20 74 68 69 73 20 74 68 65 20 64 69 72  ing this the dir
1ab84 65 63 74 6f 72 79 20 69 73 20 73 79 6e 63 65 64  ectory is synced
1ab85 20 61 67 61 69 6e 20 62 65 66 6f 72 65 20 61 6e   again before an
1ab86 79 20 69 6e 64 69 76 69 64 75 61 6c 0a 20 20 20  y individual.   
1ab87 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
1ab88 66 69 6c 65 73 20 61 72 65 20 64 65 6c 65 74 65  files are delete
1ab89 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  d..    */.    rc
1ab8a 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65   = sqlite3OsDele
1ab8b 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  te(pVfs, zMaster
1ab8c 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
1ab8d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73  3DbFree(db, zMas
1ab8e 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65  ter);.    zMaste
1ab8f 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72  r = 0;.    if( r
1ab90 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  c ){.      retur
1ab91 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n rc;.    }..   
1ab92 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e   /* All files an
1ab93 64 20 64 69 72 65 63 74 6f 72 69 65 73 20 68 61  d directories ha
1ab94 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
1ab95 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66  synced, so the f
1ab96 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20  ollowing.    ** 
1ab97 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
1ab98 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
1ab99 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20 63  Two() are only c
1ab9a 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 61 6e 64  losing files and
1ab9b 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67  .    ** deleting
1ab9c 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 6a   or truncating j
1ab9d 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65  ournals. If some
1ab9e 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
1ab9f 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68   while.    ** th
1aba0 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20  is is happening 
1aba1 77 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20  we don't really 
1aba2 63 61 72 65 2e 20 54 68 65 20 69 6e 74 65 67 72  care. The integr
1aba3 69 74 79 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  ity of the.    *
1aba4 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  * transaction is
1aba5 20 61 6c 72 65 61 64 79 20 67 75 61 72 61 6e 74   already guarant
1aba6 65 65 64 2c 20 62 75 74 20 73 6f 6d 65 20 73 74  eed, but some st
1aba7 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e  ray 'cold' journ
1aba8 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62  als.    ** may b
1aba9 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20  e lying around. 
1abaa 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72  Returning an err
1abab 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74 20 68 65  or code won't he
1abac 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20 20 20 20  lp matters..    
1abad 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 5f 73  */.    disable_s
1abae 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
1abaf 72 73 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rs();.    sqlite
1abb0 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
1abb1 6f 63 28 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  oc();.    for(i=
1abb2 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1abb3 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65  +){ .      Btree
1abb4 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
1abb5 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
1abb6 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ( pBt ){.       
1abb7 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1abb8 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 29  mitPhaseTwo(pBt)
1abb9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1abba 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65      sqlite3EndBe
1abbb 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1abbc 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74    enable_simulat
1abbd 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a  ed_io_errors();.
1abbe 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62  .    sqlite3Vtab
1abbf 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a  Commit(db);.  }.
1abc0 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
1abc1 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54   rc;.}../* .** T
1abc2 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
1abc3 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ks that the sqli
1abc4 74 65 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e  te3.activeVdbeCn
1abc5 74 20 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65  t count variable
1abc6 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20  .** matches the 
1abc7 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73  number of vdbe's
1abc8 20 69 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c   in the list sql
1abc9 69 74 65 33 2e 70 56 64 62 65 20 74 68 61 74 20  ite3.pVdbe that 
1abca 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  are.** currently
1abcb 20 61 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65   active. An asse
1abcc 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74  rtion fails if t
1abcd 68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f  he two counts do
1abce 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54   not match..** T
1abcf 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  his is an intern
1abd0 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e  al self-check on
1abd1 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61  ly - it is not a
1abd2 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63  n essential proc
1abd3 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a  essing.** step..
1abd4 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20  **.** This is a 
1abd5 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20  no-op if NDEBUG 
1abd6 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  is defined..*/.#
1abd7 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
1abd8 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41  atic void checkA
1abd9 63 74 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c  ctiveVdbeCnt(sql
1abda 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62  ite3 *db){.  Vdb
1abdb 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20  e *p;.  int cnt 
1abdc 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74  = 0;.  int nWrit
1abdd 65 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d  e = 0;.  p = db-
1abde 3e 70 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28  >pVdbe;.  while(
1abdf 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d   p ){.    if( p-
1abe0 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1abe1 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e  IC_RUN && p->pc>
1abe2 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b  =0 ){.      cnt+
1abe3 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  +;.      if( p->
1abe4 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57  readOnly==0 ) nW
1abe5 72 69 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  rite++;.    }.  
1abe6 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a    p = p->pNext;.
1abe7 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 6e    }.  assert( cn
1abe8 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56 64 62  t==db->activeVdb
1abe9 65 43 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74  eCnt );.  assert
1abea 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72  ( nWrite==db->wr
1abeb 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a  iteVdbeCnt );.}.
1abec 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 68  #else.#define ch
1abed 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74  eckActiveVdbeCnt
1abee 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (x).#endif../*.*
1abef 2a 20 46 6f 72 20 65 76 65 72 79 20 42 74 72 65  * For every Btre
1abf0 65 20 74 68 61 74 20 69 6e 20 64 61 74 61 62 61  e that in databa
1abf1 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  se connection db
1abf2 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 62   which .** has b
1abf3 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c 20 22 74  een modified, "t
1abf4 72 69 70 22 20 6f 72 20 69 6e 76 61 6c 69 64 61  rip" or invalida
1abf5 74 65 20 65 61 63 68 20 63 75 72 73 6f 72 20 69  te each cursor i
1abf6 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72 65 65 20  n.** that Btree 
1abf7 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20  might have been 
1abf8 6d 6f 64 69 66 69 65 64 20 73 6f 20 74 68 61 74  modified so that
1abf9 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63   the cursor.** c
1abfa 61 6e 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  an never be used
1abfb 20 61 67 61 69 6e 2e 20 20 54 68 69 73 20 68 61   again.  This ha
1abfc 70 70 65 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c  ppens when a rol
1abfd 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73  lback.*** occurs
1abfe 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 74 72  .  We have to tr
1abff 69 70 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72  ip all the other
1ac00 20 63 75 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a   cursors, even.*
1ac01 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74  * cursor from ot
1ac02 68 65 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65  her VMs in diffe
1ac03 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f  rent database co
1ac04 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f  nnections,.** so
1ac05 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68   that none of th
1ac06 65 6d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68  em try to use th
1ac07 65 20 64 61 74 61 20 61 74 20 77 68 69 63 68 20  e data at which 
1ac08 74 68 65 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69  they.** were poi
1ac09 6e 74 69 6e 67 20 61 6e 64 20 77 68 69 63 68 20  nting and which 
1ac0a 6e 6f 77 20 6d 61 79 20 68 61 76 65 20 62 65 65  now may have bee
1ac0b 6e 20 63 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a  n changed due.**
1ac0c 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   to the rollback
1ac0d 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72  ..**.** Remember
1ac0e 20 74 68 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b   that a rollback
1ac0f 20 63 61 6e 20 64 65 6c 65 74 65 20 74 61 62 6c   can delete tabl
1ac10 65 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a  es complete and.
1ac11 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f 6f 74 70  ** reorder rootp
1ac12 61 67 65 73 2e 20 20 53 6f 20 69 74 20 69 73 20  ages.  So it is 
1ac13 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 6a  not sufficient j
1ac14 75 73 74 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74  ust to save.** t
1ac15 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
1ac16 63 75 72 73 6f 72 2e 20 20 57 65 20 68 61 76 65  cursor.  We have
1ac17 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74   to invalidate t
1ac18 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20  he cursor.** so 
1ac19 74 68 61 74 20 69 74 20 69 73 20 6e 65 76 65 72  that it is never
1ac1a 20 75 73 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a   used again..*/.
1ac1b 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61  static void inva
1ac1c 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d  lidateCursorsOnM
1ac1d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 73 71  odifiedBtrees(sq
1ac1e 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
1ac1f 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
1ac20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1ac21 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20  .    Btree *p = 
1ac22 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1ac23 20 20 20 20 69 66 28 20 70 20 26 26 20 73 71 6c      if( p && sql
1ac24 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
1ac25 6e 73 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73  ns(p) ){.      s
1ac26 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41  qlite3BtreeTripA
1ac27 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c  llCursors(p, SQL
1ac28 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20  ITE_ABORT);.    
1ac29 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  }.  }.}../*.** I
1ac2a 66 20 74 68 65 20 56 64 62 65 20 70 61 73 73 65  f the Vdbe passe
1ac2b 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
1ac2c 72 67 75 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61  rgument opened a
1ac2d 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73   statement-trans
1ac2e 61 63 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65  action,.** close
1ac2f 20 69 74 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e   it now. Argumen
1ac30 74 20 65 4f 70 20 6d 75 73 74 20 62 65 20 65 69  t eOp must be ei
1ac31 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52  ther SAVEPOINT_R
1ac32 4f 4c 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41  OLLBACK or.** SA
1ac33 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e  VEPOINT_RELEASE.
1ac34 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f   If it is SAVEPO
1ac35 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68  INT_ROLLBACK, th
1ac36 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  en the statement
1ac37 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
1ac38 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  is rolled back. 
1ac39 49 66 20 65 4f 70 20 69 73 20 53 41 56 45 50 4f  If eOp is SAVEPO
1ac3a 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65  INT_RELEASE, the
1ac3b 6e 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d  n the .** statem
1ac3c 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
1ac3d 69 73 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a  is commtted..**.
1ac3e 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  ** If an IO erro
1ac3f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c  r occurs, an SQL
1ac40 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72  ITE_IOERR_XXX er
1ac41 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
1ac42 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77  rned. .** Otherw
1ac43 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  ise SQLITE_OK..*
1ac44 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ac45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1ac46 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56  CloseStatement(V
1ac47 64 62 65 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29  dbe *p, int eOp)
1ac48 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e  {.  sqlite3 *con
1ac49 73 74 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  st db = p->db;. 
1ac4a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1ac4b 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d  _OK;..  /* If p-
1ac4c 3e 69 53 74 61 74 65 6d 65 6e 74 20 69 73 20 67  >iStatement is g
1ac4d 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f  reater than zero
1ac4e 2c 20 74 68 65 6e 20 74 68 69 73 20 56 64 62 65  , then this Vdbe
1ac4f 20 6f 70 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20   opened a .  ** 
1ac50 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1ac51 63 74 69 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c  ction that shoul
1ac52 64 20 62 65 20 63 6c 6f 73 65 64 20 68 65 72 65  d be closed here
1ac53 2e 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70  . The only excep
1ac54 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61  tion.  ** is tha
1ac55 74 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61  t an IO error ma
1ac56 79 20 68 61 76 65 20 6f 63 63 75 72 65 64 2c 20  y have occured, 
1ac57 63 61 75 73 69 6e 67 20 61 6e 20 65 6d 65 72 67  causing an emerg
1ac58 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20  ency rollback.. 
1ac59 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   ** In this case
1ac5a 20 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74   (db->nStatement
1ac5b 3d 3d 30 29 2c 20 61 6e 64 20 74 68 65 72 65 20  ==0), and there 
1ac5c 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f  is nothing to do
1ac5d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
1ac5e 3e 6e 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70  >nStatement && p
1ac5f 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a  ->iStatement ){.
1ac60 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63      int i;.    c
1ac61 6f 6e 73 74 20 69 6e 74 20 69 53 61 76 65 70 6f  onst int iSavepo
1ac62 69 6e 74 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d  int = p->iStatem
1ac63 65 6e 74 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65  ent-1;..    asse
1ac64 72 74 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49  rt( eOp==SAVEPOI
1ac65 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65  NT_ROLLBACK || e
1ac66 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  Op==SAVEPOINT_RE
1ac67 4c 45 41 53 45 29 3b 0a 20 20 20 20 61 73 73 65  LEASE);.    asse
1ac68 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65  rt( db->nStateme
1ac69 6e 74 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  nt>0 );.    asse
1ac6a 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  rt( p->iStatemen
1ac6b 74 3d 3d 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65  t==(db->nStateme
1ac6c 6e 74 2b 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e  nt+db->nSavepoin
1ac6d 74 29 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69  t) );..    for(i
1ac6e 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1ac6f 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20  ++){ .      int 
1ac70 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  rc2 = SQLITE_OK;
1ac71 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
1ac72 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
1ac73 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42  Bt;.      if( pB
1ac74 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  t ){.        if(
1ac75 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f   eOp==SAVEPOINT_
1ac76 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20  ROLLBACK ){.    
1ac77 20 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69        rc2 = sqli
1ac78 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e  te3BtreeSavepoin
1ac79 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54  t(pBt, SAVEPOINT
1ac7a 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65  _ROLLBACK, iSave
1ac7b 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  point);.        
1ac7c 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  }.        if( rc
1ac7d 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  2==SQLITE_OK ){.
1ac7e 20 20 20 20 20 20 20 20 20 20 72 63 32 20 3d 20            rc2 = 
1ac7f 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65  sqlite3BtreeSave
1ac80 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50  point(pBt, SAVEP
1ac81 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 53  OINT_RELEASE, iS
1ac82 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20  avepoint);.     
1ac83 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1ac84 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1ac85 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
1ac86 20 72 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a   rc2;.        }.
1ac87 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1ac88 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74    db->nStatement
1ac89 2d 2d 3b 0a 20 20 20 20 70 2d 3e 69 53 74 61 74  --;.    p->iStat
1ac8a 65 6d 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20  ement = 0;..    
1ac8b 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 6d  /* If the statem
1ac8c 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
1ac8d 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20  is being rolled 
1ac8e 62 61 63 6b 2c 20 61 6c 73 6f 20 72 65 73 74 6f  back, also resto
1ac8f 72 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64  re the .    ** d
1ac90 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20  atabase handles 
1ac91 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61  deferred constra
1ac92 69 6e 74 20 63 6f 75 6e 74 65 72 20 74 6f 20 74  int counter to t
1ac93 68 65 20 76 61 6c 75 65 20 69 74 20 68 61 64 20  he value it had 
1ac94 77 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65  when .    ** the
1ac95 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1ac96 61 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65  action was opene
1ac97 64 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65  d.  */.    if( e
1ac98 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  Op==SAVEPOINT_RO
1ac99 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20  LLBACK ){.      
1ac9a 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e  db->nDeferredCon
1ac9b 73 20 3d 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43  s = p->nStmtDefC
1ac9c 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ons;.    }.  }. 
1ac9d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1ac9e 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69  *.** If SQLite i
1ac9f 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75  s compiled to su
1aca0 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63  pport shared-cac
1aca1 68 65 20 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62  he mode and to b
1aca2 65 20 74 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a  e threadsafe,.**
1aca3 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 62   this routine ob
1aca4 74 61 69 6e 73 20 74 68 65 20 6d 75 74 65 78 20  tains the mutex 
1aca5 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1aca6 65 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74  each BtShared st
1aca7 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20  ructure.** that 
1aca8 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20  may be accessed 
1aca9 62 79 20 74 68 65 20 56 4d 20 70 61 73 73 65 64  by the VM passed
1acaa 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
1acab 20 49 6e 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a   In doing so it.
1acac 2a 2a 20 73 65 74 73 20 74 68 65 20 42 74 53 68  ** sets the BtSh
1acad 61 72 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f  ared.db member o
1acae 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 42 74  f each of the Bt
1acaf 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
1acb0 73 2c 20 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74  s, ensuring.** t
1acb1 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20  hat the correct 
1acb2 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c  busy-handler cal
1acb3 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
1acb4 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a   if required..**
1acb5 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73  .** If SQLite is
1acb6 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20   not threadsafe 
1acb7 62 75 74 20 64 6f 65 73 20 73 75 70 70 6f 72 74  but does support
1acb8 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
1acb9 64 65 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69  de, then.** sqli
1acba 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
1acbb 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f  () is invoked to
1acbc 20 73 65 74 20 74 68 65 20 42 74 53 68 61 72 65   set the BtShare
1acbd 64 2e 64 62 20 76 61 72 69 61 62 6c 65 73 0a 2a  d.db variables.*
1acbe 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53 68  * of all of BtSh
1acbf 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73 20  ared structures 
1acc0 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 74  accessible via t
1acc1 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
1acc2 6c 65 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  le .** associate
1acc3 64 20 77 69 74 68 20 74 68 65 20 56 4d 2e 20 4f  d with the VM. O
1acc4 66 20 63 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20  f course only a 
1acc5 73 75 62 73 65 74 20 6f 66 20 74 68 65 73 65 20  subset of these 
1acc6 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 77 69  structures.** wi
1acc7 6c 6c 20 62 65 20 61 63 63 65 73 73 65 64 20 62  ll be accessed b
1acc8 79 20 74 68 65 20 56 4d 2c 20 61 6e 64 20 77 65  y the VM, and we
1acc9 20 63 6f 75 6c 64 20 75 73 65 20 56 64 62 65 2e   could use Vdbe.
1acca 62 74 72 65 65 4d 61 73 6b 20 74 6f 20 66 69 67  btreeMask to fig
1accb 75 72 65 0a 2a 2a 20 74 68 61 74 20 73 75 62 73  ure.** that subs
1accc 65 74 20 6f 75 74 2c 20 62 75 74 20 74 68 65 72  et out, but ther
1accd 65 20 69 73 20 6e 6f 20 61 64 76 61 6e 74 61 67  e is no advantag
1acce 65 20 74 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a  e to doing so..*
1accf 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69  *.** If SQLite i
1acd0 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65  s not threadsafe
1acd1 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 73 75   and does not su
1acd2 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63  pport shared-cac
1acd3 68 65 20 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a  he mode, this.**
1acd4 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
1acd5 6f 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  o-op..*/.#ifndef
1acd6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1acd7 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45  RED_CACHE.SQLITE
1acd8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1acd9 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72  lite3VdbeMutexAr
1acda 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70  rayEnter(Vdbe *p
1acdb 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  ){.#if SQLITE_TH
1acdc 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74  READSAFE.  sqlit
1acdd 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
1acde 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65  yEnter(&p->aMute
1acdf 78 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69  x);.#else.  sqli
1ace0 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
1ace1 28 70 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a  (p->db);.#endif.
1ace2 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1ace3 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
1ace4 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 74   called when a t
1ace5 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65  ransaction opene
1ace6 64 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73  d by the databas
1ace7 65 20 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73  e .** handle ass
1ace8 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
1ace9 20 56 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e   VM passed as an
1acea 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 62 6f   argument is abo
1aceb 75 74 20 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d  ut to be .** com
1acec 6d 69 74 74 65 64 2e 20 49 66 20 74 68 65 72 65  mitted. If there
1aced 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67   are outstanding
1acee 20 64 65 66 65 72 72 65 64 20 66 6f 72 65 69 67   deferred foreig
1acef 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
1acf0 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20  .** violations, 
1acf1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1acf2 52 4f 52 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ROR. Otherwise, 
1acf3 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a  SQLITE_OK..**.**
1acf4 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75   If there are ou
1acf5 74 73 74 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f  tstanding FK vio
1acf6 6c 61 74 69 6f 6e 73 20 61 6e 64 20 74 68 69 73  lations and this
1acf7 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
1acf8 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52  s .** SQLITE_ERR
1acf9 4f 52 2c 20 73 65 74 20 74 68 65 20 72 65 73 75  OR, set the resu
1acfa 6c 74 20 6f 66 20 74 68 65 20 56 4d 20 74 6f 20  lt of the VM to 
1acfb 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
1acfc 54 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61  T and write.** a
1acfd 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
1acfe 74 6f 20 69 74 2e 20 54 68 65 6e 20 72 65 74 75  to it. Then retu
1acff 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e  rn SQLITE_ERROR.
1ad00 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1ad01 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
1ad02 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  KEY.SQLITE_PRIVA
1ad03 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1ad04 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a  beCheckFk(Vdbe *
1ad05 70 2c 20 69 6e 74 20 64 65 66 65 72 72 65 64 29  p, int deferred)
1ad06 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
1ad07 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28  = p->db;.  if( (
1ad08 64 65 66 65 72 72 65 64 20 26 26 20 64 62 2d 3e  deferred && db->
1ad09 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 29  nDeferredCons>0)
1ad0a 20 7c 7c 20 28 21 64 65 66 65 72 72 65 64 20 26   || (!deferred &
1ad0b 26 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69  & p->nFkConstrai
1ad0c 6e 74 3e 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e  nt>0) ){.    p->
1ad0d 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53  rc = SQLITE_CONS
1ad0e 54 52 41 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65  TRAINT;.    p->e
1ad0f 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f  rrorAction = OE_
1ad10 41 62 6f 72 74 3b 0a 20 20 20 20 73 71 6c 69 74  Abort;.    sqlit
1ad11 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1ad12 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f  zErrMsg, db, "fo
1ad13 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
1ad14 61 69 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a 20  aint failed");. 
1ad15 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1ad16 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65  _ERROR;.  }.  re
1ad17 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1ad18 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1ad19 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1ad1a 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20  called the when 
1ad1b 61 20 56 44 42 45 20 74 72 69 65 73 20 74 6f 20  a VDBE tries to 
1ad1c 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44  halt.  If the VD
1ad1d 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63  BE.** has made c
1ad1e 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e  hanges and is in
1ad1f 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   autocommit mode
1ad20 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68  , then commit th
1ad21 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20  ose.** changes. 
1ad22 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69   If a rollback i
1ad23 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64  s needed, then d
1ad24 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a  o the rollback..
1ad25 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1ad26 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77  ne is the only w
1ad27 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73  ay to move the s
1ad28 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f  tate of a VM fro
1ad29 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49  m.** SQLITE_MAGI
1ad2a 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f  C_RUN to SQLITE_
1ad2b 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20  MAGIC_HALT.  It 
1ad2c 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a  is harmless to.*
1ad2d 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61  * call this on a
1ad2e 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74   VM that is in t
1ad2f 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  he SQLITE_MAGIC_
1ad30 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a  HALT state..**.*
1ad31 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  * Return an erro
1ad32 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20  r code.  If the 
1ad33 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74  commit could not
1ad34 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73   complete becaus
1ad35 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e  e of.** lock con
1ad36 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20  tention, return 
1ad37 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66  SQLITE_BUSY.  If
1ad38 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
1ad39 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20  returned, it.** 
1ad3a 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20  means the close 
1ad3b 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61  did not happen a
1ad3c 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  nd needs to be r
1ad3d 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  epeated..*/.SQLI
1ad3e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1ad3f 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56  qlite3VdbeHalt(V
1ad40 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  dbe *p){.  int r
1ad41 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
1ad42 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
1ad43 65 64 20 74 6f 20 73 74 6f 72 65 20 74 72 61 6e  ed to store tran
1ad44 73 69 65 6e 74 20 72 65 74 75 72 6e 20 63 6f 64  sient return cod
1ad45 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  es */.  sqlite3 
1ad46 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20  *db = p->db;..  
1ad47 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
1ad48 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f   contains the lo
1ad49 67 69 63 20 74 68 61 74 20 64 65 74 65 72 6d 69  gic that determi
1ad4a 6e 65 73 20 69 66 20 61 20 73 74 61 74 65 6d 65  nes if a stateme
1ad4b 6e 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73  nt or.  ** trans
1ad4c 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63  action will be c
1ad4d 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c  ommitted or roll
1ad4e 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73  ed back as a res
1ad4f 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  ult of the.  ** 
1ad50 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69  execution of thi
1ad51 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  s virtual machin
1ad52 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  e. .  **.  ** If
1ad53 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c   any of the foll
1ad54 6f 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63  owing errors occ
1ad55 75 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ur:.  **.  **   
1ad56 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20    SQLITE_NOMEM. 
1ad57 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49   **     SQLITE_I
1ad58 4f 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51  OERR.  **     SQ
1ad59 4c 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20  LITE_FULL.  **  
1ad5a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52     SQLITE_INTERR
1ad5b 55 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  UPT.  **.  ** Th
1ad5c 65 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  en the internal 
1ad5d 63 61 63 68 65 20 6d 69 67 68 74 20 68 61 76 65  cache might have
1ad5e 20 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e   been left in an
1ad5f 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20   inconsistent.  
1ad60 2a 2a 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65  ** state.  We ne
1ad61 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  ed to rollback t
1ad62 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  he statement tra
1ad63 6e 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65  nsaction, if the
1ad64 72 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20  re is.  ** one, 
1ad65 6f 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  or the complete 
1ad66 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74  transaction if t
1ad67 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65  here is no state
1ad68 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1ad69 2e 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d  ..  */..  if( p-
1ad6a 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1ad6b 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  d ){.    p->rc =
1ad6c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1ad6d 20 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72   }.  closeAllCur
1ad6e 73 6f 72 73 28 70 29 3b 0a 20 20 69 66 28 20 70  sors(p);.  if( p
1ad6f 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41  ->magic!=VDBE_MA
1ad70 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72  GIC_RUN ){.    r
1ad71 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1ad72 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69  .  }.  checkActi
1ad73 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a  veVdbeCnt(db);..
1ad74 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f    /* No commit o
1ad75 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65  r rollback neede
1ad76 64 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d  d if the program
1ad77 20 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a   never started *
1ad78 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30  /.  if( p->pc>=0
1ad79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b   ){.    int mrc;
1ad7a 20 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72     /* Primary er
1ad7b 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d  ror code from p-
1ad7c 3e 72 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65  >rc */.    int e
1ad7d 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b  StatementOp = 0;
1ad7e 0a 20 20 20 20 69 6e 74 20 69 73 53 70 65 63 69  .    int isSpeci
1ad7f 61 6c 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20  alError;        
1ad80 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72      /* Set to tr
1ad81 75 65 20 69 66 20 61 20 27 73 70 65 63 69 61 6c  ue if a 'special
1ad82 27 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20  ' error */..    
1ad83 2f 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65  /* Lock all btre
1ad84 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73  es used by the s
1ad85 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
1ad86 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78  sqlite3VdbeMutex
1ad87 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a  ArrayEnter(p);..
1ad88 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72      /* Check for
1ad89 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63   one of the spec
1ad8a 69 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  ial errors */.  
1ad8b 20 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20    mrc = p->rc & 
1ad8c 30 78 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74  0xff;.    assert
1ad8d 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  ( p->rc!=SQLITE_
1ad8e 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b  IOERR_BLOCKED );
1ad8f 20 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20    /* This error 
1ad90 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73  no longer exists
1ad91 20 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61   */.    isSpecia
1ad92 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51  lError = mrc==SQ
1ad93 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72  LITE_NOMEM || mr
1ad94 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a  c==SQLITE_IOERR.
1ad95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad96 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c       || mrc==SQL
1ad97 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c  ITE_INTERRUPT ||
1ad98 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c   mrc==SQLITE_FUL
1ad99 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 65  L;.    if( isSpe
1ad9a 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20  cialError ){.   
1ad9b 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65     /* If the que
1ad9c 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79  ry was read-only
1ad9d 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20  , we need do no 
1ad9e 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e  rollback at all.
1ad9f 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20   Otherwise,.    
1ada0 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74    ** proceed wit
1ada1 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 68 61  h the special ha
1ada2 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f  ndling..      */
1ada3 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72  .      if( !p->r
1ada4 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d  eadOnly || mrc!=
1ada5 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
1ada6 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1ada7 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  (mrc==SQLITE_NOM
1ada8 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54  EM || mrc==SQLIT
1ada9 45 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73  E_FULL) && p->us
1adaa 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b  esStmtJournal ){
1adab 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74  .          eStat
1adac 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f  ementOp = SAVEPO
1adad 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20  INT_ROLLBACK;.  
1adae 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1adaf 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65         /* We are
1adb0 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20   forced to roll 
1adb1 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20  back the active 
1adb2 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66  transaction. Bef
1adb3 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20  ore doing.      
1adb4 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74      ** so, abort
1adb5 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65   any other state
1adb6 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c  ments this handl
1adb7 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20  e currently has 
1adb8 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20  active..        
1adb9 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69    */.          i
1adba 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73  nvalidateCursors
1adbb 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73  OnModifiedBtrees
1adbc 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
1adbd 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41  sqlite3RollbackA
1adbe 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ll(db);.        
1adbf 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61    sqlite3CloseSa
1adc0 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20  vepoints(db);.  
1adc1 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f          db->auto
1adc2 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  Commit = 1;.    
1adc3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1adc4 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63    }..    /* Chec
1adc5 6b 20 66 6f 72 20 69 6d 6d 65 64 69 61 74 65 20  k for immediate 
1adc6 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c  foreign key viol
1adc7 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 69  ations. */.    i
1adc8 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  f( p->rc==SQLITE
1adc9 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
1adca 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28  ite3VdbeCheckFk(
1adcb 70 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0a  p, 0);.    }.  .
1adcc 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 61 75      /* If the au
1adcd 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69  to-commit flag i
1adce 73 20 73 65 74 20 61 6e 64 20 74 68 69 73 20 69  s set and this i
1adcf 73 20 74 68 65 20 6f 6e 6c 79 20 61 63 74 69 76  s the only activ
1add0 65 20 77 72 69 74 65 72 20 0a 20 20 20 20 2a 2a  e writer .    **
1add1 20 56 4d 2c 20 74 68 65 6e 20 77 65 20 64 6f 20   VM, then we do 
1add2 65 69 74 68 65 72 20 61 20 63 6f 6d 6d 69 74 20  either a commit 
1add3 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74  or rollback of t
1add4 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
1add5 61 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a  action. .    **.
1add6 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69      ** Note: Thi
1add7 73 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e  s block also run
1add8 73 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  s if one of the 
1add9 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73 20 68  special errors h
1adda 61 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61  andled .    ** a
1addb 62 6f 76 65 20 68 61 73 20 6f 63 63 75 72 72 65  bove has occurre
1addc 64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  d. .    */.    i
1addd 66 28 20 21 73 71 6c 69 74 65 33 56 74 61 62 49  f( !sqlite3VtabI
1adde 6e 53 79 6e 63 28 64 62 29 20 0a 20 20 20 20 20  nSync(db) .     
1addf 26 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  && db->autoCommi
1ade0 74 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 77  t .     && db->w
1ade1 72 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d  riteVdbeCnt==(p-
1ade2 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20  >readOnly==0) . 
1ade3 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20     ){.      if( 
1ade4 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->rc==SQLITE_OK
1ade5 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 74   || (p->errorAct
1ade6 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20  ion==OE_Fail && 
1ade7 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 29  !isSpecialError)
1ade8 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1ade9 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b  sqlite3VdbeCheck
1adea 46 6b 28 70 2c 20 31 29 20 29 7b 0a 20 20 20 20  Fk(p, 1) ){.    
1adeb 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1adec 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76  eeMutexArrayLeav
1aded 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20  e(&p->aMutex);. 
1adee 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
1adef 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1adf0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1adf1 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d  /* The auto-comm
1adf2 69 74 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c  it flag is true,
1adf3 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61   the vdbe progra
1adf4 6d 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  m was successful
1adf5 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20   .        ** or 
1adf6 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27  hit an 'OR FAIL'
1adf7 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20   constraint and 
1adf8 74 68 65 72 65 20 61 72 65 20 6e 6f 20 64 65 66  there are no def
1adf9 65 72 72 65 64 20 66 6f 72 65 69 67 6e 0a 20 20  erred foreign.  
1adfa 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e        ** key con
1adfb 73 74 72 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64  straints to hold
1adfc 20 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74   up the transact
1adfd 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  ion. This means 
1adfe 61 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20  a commit .      
1adff 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64    ** is required
1ae00 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63  .  */.        rc
1ae01 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62   = vdbeCommit(db
1ae02 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , p);.        if
1ae03 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
1ae04 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  Y ){.          s
1ae05 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
1ae06 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61  ArrayLeave(&p->a
1ae07 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20  Mutex);.        
1ae08 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1ae09 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65  BUSY;.        }e
1ae0a 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49  lse if( rc!=SQLI
1ae0b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1ae0c 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20     p->rc = rc;. 
1ae0d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1ae0e 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b  RollbackAll(db);
1ae0f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1ae10 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 44            db->nD
1ae11 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b  eferredCons = 0;
1ae12 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1ae13 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c  e3CommitInternal
1ae14 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20  Changes(db);.   
1ae15 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1ae16 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
1ae17 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64  te3RollbackAll(d
1ae18 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  b);.      }.    
1ae19 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74    db->nStatement
1ae1a 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
1ae1b 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70  if( eStatementOp
1ae1c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
1ae1d 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
1ae1e 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74  K || p->errorAct
1ae1f 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a  ion==OE_Fail ){.
1ae20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65          eStateme
1ae21 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54  ntOp = SAVEPOINT
1ae22 5f 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20  _RELEASE;.      
1ae23 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72  }else if( p->err
1ae24 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f  orAction==OE_Abo
1ae25 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53  rt ){.        eS
1ae26 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56  tatementOp = SAV
1ae27 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b  EPOINT_ROLLBACK;
1ae28 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1ae29 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65        invalidate
1ae2a 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65  CursorsOnModifie
1ae2b 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20  dBtrees(db);.   
1ae2c 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c       sqlite3Roll
1ae2d 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20  backAll(db);.   
1ae2e 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73       sqlite3Clos
1ae2f 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b  eSavepoints(db);
1ae30 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74  .        db->aut
1ae31 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  oCommit = 1;.   
1ae32 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
1ae33 20 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65    /* If eStateme
1ae34 6e 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  ntOp is non-zero
1ae35 2c 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65  , then a stateme
1ae36 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e  nt transaction n
1ae37 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62  eeds to.    ** b
1ae38 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72  e committed or r
1ae39 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c  olled back. Call
1ae3a 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73   sqlite3VdbeClos
1ae3b 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a  eStatement() to.
1ae3c 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66      ** do so. If
1ae3d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
1ae3e 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
1ae3f 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  , and the curren
1ae40 74 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20  t statement.    
1ae41 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ** error code is
1ae42 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51   SQLITE_OK or SQ
1ae43 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c  LITE_CONSTRAINT,
1ae44 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72   then set the er
1ae45 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20  ror.    ** code 
1ae46 74 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65  to the new value
1ae47 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1ae48 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b   eStatementOp ){
1ae49 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1ae4a 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74  te3VdbeCloseStat
1ae4b 65 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d  ement(p, eStatem
1ae4c 65 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66  entOp);.      if
1ae4d 28 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d  ( rc && (p->rc==
1ae4e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
1ae4f 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54  rc==SQLITE_CONST
1ae50 52 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20  RAINT) ){.      
1ae51 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
1ae52 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1ae53 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1ae54 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  sg);.        p->
1ae55 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
1ae56 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
1ae57 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73    /* If this was
1ae58 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41   an INSERT, UPDA
1ae59 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64  TE or DELETE and
1ae5a 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72   no statement tr
1ae5b 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a  ansaction.    **
1ae5c 20 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64   has been rolled
1ae5d 20 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68   back, update th
1ae5e 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1ae5f 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75  ction change-cou
1ae60 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  nter. .    */.  
1ae61 20 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43    if( p->changeC
1ae62 6e 74 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66  ntOn ){.      if
1ae63 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d  ( eStatementOp!=
1ae64 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
1ae65 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  CK ){.        sq
1ae66 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
1ae67 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e  ges(db, p->nChan
1ae68 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ge);.      }else
1ae69 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1ae6a 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
1ae6b 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  db, 0);.      }.
1ae6c 20 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65        p->nChange
1ae6d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20   = 0;.    }.  . 
1ae6e 20 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f     /* Rollback o
1ae6f 72 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68  r commit any sch
1ae70 65 6d 61 20 63 68 61 6e 67 65 73 20 74 68 61 74  ema changes that
1ae71 20 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20   occurred. */.  
1ae72 20 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c    if( p->rc!=SQL
1ae73 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c  ITE_OK && db->fl
1ae74 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72  ags&SQLITE_Inter
1ae75 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20  nChanges ){.    
1ae76 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
1ae77 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
1ae78 20 30 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66   0);.      db->f
1ae79 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67  lags = (db->flag
1ae7a 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72  s | SQLITE_Inter
1ae7b 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d  nChanges);.    }
1ae7c 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65  ..    /* Release
1ae7d 20 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20   the locks */.  
1ae7e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75    sqlite3BtreeMu
1ae7f 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70  texArrayLeave(&p
1ae80 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a  ->aMutex);.  }..
1ae81 20 20 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63    /* We have suc
1ae82 63 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64  cessfully halted
1ae83 20 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20   and closed the 
1ae84 56 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73  VM.  Record this
1ae85 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20   fact. */.  if( 
1ae86 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20  p->pc>=0 ){.    
1ae87 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
1ae88 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d  t--;.    if( !p-
1ae89 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  >readOnly ){.   
1ae8a 20 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65     db->writeVdbe
1ae8b 43 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20  Cnt--;.    }.   
1ae8c 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74   assert( db->act
1ae8d 69 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e  iveVdbeCnt>=db->
1ae8e 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a  writeVdbeCnt );.
1ae8f 20 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d    }.  p->magic =
1ae90 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54   VDBE_MAGIC_HALT
1ae91 3b 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56  ;.  checkActiveV
1ae92 64 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66  dbeCnt(db);.  if
1ae93 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ( p->db->mallocF
1ae94 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e  ailed ){.    p->
1ae95 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1ae96 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  M;.  }..  /* If 
1ae97 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  the auto-commit 
1ae98 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20 74  flag is set to t
1ae99 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f  rue, then any lo
1ae9a 63 6b 73 20 74 68 61 74 20 77 65 72 65 20 68 65  cks that were he
1ae9b 6c 64 0a 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65  ld.  ** by conne
1ae9c 63 74 69 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f  ction db have no
1ae9d 77 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e  w been released.
1ae9e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e   Call sqlite3Con
1ae9f 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
1aea0 29 20 0a 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b  ) .  ** to invok
1aea1 65 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 75  e any required u
1aea2 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
1aea3 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69  lbacks..  */.  i
1aea4 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  f( db->autoCommi
1aea5 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
1aea6 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b  ConnectionUnlock
1aea7 65 64 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61  ed(db);.  }..  a
1aea8 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76  ssert( db->activ
1aea9 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62  eVdbeCnt>0 || db
1aeaa 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20  ->autoCommit==0 
1aeab 7c 7c 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e  || db->nStatemen
1aeac 74 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  t==0 );.  return
1aead 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
1aeae 2f 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20  /*.** Each VDBE 
1aeaf 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74  holds the result
1aeb0 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
1aeb1 65 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ent sqlite3_step
1aeb2 28 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d  () call.** in p-
1aeb3 3e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69  >rc.  This routi
1aeb4 6e 65 20 73 65 74 73 20 74 68 61 74 20 72 65 73  ne sets that res
1aeb5 75 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49  ult back to SQLI
1aeb6 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TE_OK..*/.SQLITE
1aeb7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1aeb8 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74  lite3VdbeResetSt
1aeb9 65 70 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70  epResult(Vdbe *p
1aeba 29 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  ){.  p->rc = SQL
1aebb 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
1aebc 20 43 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45   Clean up a VDBE
1aebd 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e   after execution
1aebe 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65   but do not dele
1aebf 74 65 20 74 68 65 20 56 44 42 45 20 6a 75 73 74  te the VDBE just
1aec0 20 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61   yet..** Write a
1aec1 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ny error message
1aec2 73 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67  s into *pzErrMsg
1aec3 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65  .  Return the re
1aec4 73 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  sult code..**.**
1aec5 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74   After this rout
1aec6 69 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20  ine is run, the 
1aec7 56 44 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72  VDBE should be r
1aec8 65 61 64 79 20 74 6f 20 62 65 20 65 78 65 63 75  eady to be execu
1aec9 74 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a  ted.** again..**
1aeca 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69  .** To look at i
1aecb 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74  t another way, t
1aecc 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65  his routine rese
1aecd 74 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  ts the state of 
1aece 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d  the.** virtual m
1aecf 61 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45  achine from VDBE
1aed0 5f 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44  _MAGIC_RUN or VD
1aed1 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61  BE_MAGIC_HALT ba
1aed2 63 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41  ck to.** VDBE_MA
1aed3 47 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c  GIC_INIT..*/.SQL
1aed4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1aed5 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74  sqlite3VdbeReset
1aed6 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c  (Vdbe *p){.  sql
1aed7 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d  ite3 *db;.  db =
1aed8 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66   p->db;..  /* If
1aed9 20 74 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20   the VM did not 
1aeda 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f  run to completio
1aedb 6e 20 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75  n or if it encou
1aedc 6e 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65  ntered an.  ** e
1aedd 72 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69  rror, then it mi
1aede 67 68 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65  ght not have bee
1aedf 6e 20 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c  n halted properl
1aee0 79 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a  y.  So halt.  **
1aee1 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20   it now..  */.  
1aee2 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1aee3 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c  etyOn(db);.  sql
1aee4 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b  ite3VdbeHalt(p);
1aee5 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
1aee6 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a  SafetyOff(db);..
1aee7 20 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45    /* If the VDBE
1aee8 20 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e   has be run even
1aee9 20 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e   partially, then
1aeea 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72   transfer the er
1aeeb 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e  ror code.  ** an
1aeec 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  d error message 
1aeed 66 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e  from the VDBE in
1aeee 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  to the main data
1aeef 62 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20  base structure. 
1aef0 20 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65   But.  ** if the
1aef1 20 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62   VDBE has just b
1aef2 65 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62  een set to run b
1aef3 75 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61  ut has not actua
1aef4 6c 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79  lly executed any
1aef5 0a 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f  .  ** instructio
1aef6 6e 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68  ns yet, leave th
1aef7 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1aef8 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f  error informatio
1aef9 6e 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a  n unchanged..  *
1aefa 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30  /.  if( p->pc>=0
1aefb 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a   ){.    if( p->z
1aefc 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
1aefd 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
1aefe 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
1aeff 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
1af00 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31  tStr(db->pErr,-1
1af01 2c 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49  ,p->zErrMsg,SQLI
1af02 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54  TE_UTF8,SQLITE_T
1af03 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20  RANSIENT);.     
1af04 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
1af05 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
1af06 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70   db->errCode = p
1af07 2d 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69  ->rc;.      sqli
1af08 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1af09 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  >zErrMsg);.     
1af0a 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b   p->zErrMsg = 0;
1af0b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
1af0c 2d 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71  ->rc ){.      sq
1af0d 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70  lite3Error(db, p
1af0e 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65  ->rc, 0);.    }e
1af0f 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
1af10 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
1af11 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d  TE_OK, 0);.    }
1af12 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
1af13 72 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64  rc && p->expired
1af14 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65   ){.    /* The e
1af15 78 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20  xpired flag was 
1af16 73 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20  set on the VDBE 
1af17 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74  before the first
1af18 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20   call.    ** to 
1af19 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20  sqlite3_step(). 
1af1a 46 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20  For consistency 
1af1b 28 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73  (since sqlite3_s
1af1c 74 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a  tep() was.    **
1af1d 20 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68   called), set th
1af1e 65 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72  e database error
1af1f 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73   in this case as
1af20 20 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20   well..    */.  
1af21 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
1af22 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20  b, p->rc, 0);.  
1af23 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
1af24 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d  tStr(db->pErr, -
1af25 31 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53  1, p->zErrMsg, S
1af26 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
1af27 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
1af28 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1af29 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1af2a 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
1af2b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
1af2c 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f  Reclaim all memo
1af2d 72 79 20 75 73 65 64 20 62 79 20 74 68 65 20 56  ry used by the V
1af2e 44 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e  DBE.  */.  Clean
1af2f 75 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76  up(p);..  /* Sav
1af30 65 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f  e profiling info
1af31 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69  rmation from thi
1af32 73 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f  s VDBE run..  */
1af33 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
1af34 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c  FILE.  {.    FIL
1af35 45 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22  E *out = fopen("
1af36 76 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74  vdbe_profile.out
1af37 22 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28  ", "a");.    if(
1af38 20 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e   out ){.      in
1af39 74 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e  t i;.      fprin
1af3a 74 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29  tf(out, "---- ")
1af3b 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
1af3c 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
1af3d 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66  .        fprintf
1af3e 28 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d  (out, "%02x", p-
1af3f 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b  >aOp[i].opcode);
1af40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
1af41 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
1af42 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
1af43 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29  ; i<p->nOp; i++)
1af44 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74  {.        fprint
1af45 66 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c  f(out, "%6d %10l
1af46 6c 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20  ld %8lld ",.    
1af47 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d         p->aOp[i]
1af48 2e 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20  .cnt,.          
1af49 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65   p->aOp[i].cycle
1af4a 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d  s,.           p-
1af4b 3e 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20  >aOp[i].cnt>0 ? 
1af4c 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73  p->aOp[i].cycles
1af4d 2f 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a  /p->aOp[i].cnt :
1af4e 20 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20   0.        );.  
1af4f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1af50 65 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c  ePrintOp(out, i,
1af51 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20   &p->aOp[i]);.  
1af52 20 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f      }.      fclo
1af53 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20  se(out);.    }. 
1af54 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d   }.#endif.  p->m
1af55 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
1af56 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e  C_INIT;.  return
1af57 20 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72   p->rc & db->err
1af58 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20  Mask;.}. ./*.** 
1af59 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c  Clean up and del
1af5a 65 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72  ete a VDBE after
1af5b 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74   execution.  Ret
1af5c 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77  urn an integer w
1af5d 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72  hich is.** the r
1af5e 65 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69  esult code.  Wri
1af5f 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73  te any error mes
1af60 73 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a  sage text into *
1af61 70 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c  pzErrMsg..*/.SQL
1af62 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1af63 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
1af64 69 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ize(Vdbe *p){.  
1af65 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1af66 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67  OK;.  if( p->mag
1af67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic==VDBE_MAGIC_R
1af68 55 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d  UN || p->magic==
1af69 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20  VDBE_MAGIC_HALT 
1af6a 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1af6b 74 65 33 56 64 62 65 52 65 73 65 74 28 70 29 3b  te3VdbeReset(p);
1af6c 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63  .    assert( (rc
1af6d 20 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73   & p->db->errMas
1af6e 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20  k)==rc );.  }.  
1af6f 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
1af70 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
1af71 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  c;.}../*.** Call
1af72 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
1af73 66 6f 72 20 65 61 63 68 20 61 75 78 64 61 74 61  for each auxdata
1af74 20 65 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46   entry in pVdbeF
1af75 75 6e 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a  unc for which.**
1af76 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1af77 6e 67 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69  ng bit in mask i
1af78 73 20 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74  s clear.  Auxdat
1af79 61 20 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64  a entries beyond
1af7a 20 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79   31.** are alway
1af7b 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f  s destroyed.  To
1af7c 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78   destroy all aux
1af7d 64 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61  data entries, ca
1af7e 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  ll this.** routi
1af7f 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e  ne with mask==0.
1af80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1af81 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1af82 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61  dbeDeleteAuxData
1af83 28 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65  (VdbeFunc *pVdbe
1af84 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b  Func, int mask){
1af85 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
1af86 69 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63  i=0; i<pVdbeFunc
1af87 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20  ->nAux; i++){.  
1af88 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61    struct AuxData
1af89 20 2a 70 41 75 78 20 3d 20 26 70 56 64 62 65 46   *pAux = &pVdbeF
1af8a 75 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20  unc->apAux[i];. 
1af8b 20 20 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20     if( (i>31 || 
1af8c 21 28 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29  !(mask&(((u32)1)
1af8d 3c 3c 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e  <<i))) && pAux->
1af8e 70 41 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66  pAux ){.      if
1af8f 28 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20  ( pAux->xDelete 
1af90 29 7b 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d  ){.        pAux-
1af91 3e 78 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70  >xDelete(pAux->p
1af92 41 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Aux);.      }.  
1af93 20 20 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d      pAux->pAux =
1af94 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   0;.    }.  }.}.
1af95 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
1af96 20 65 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f   entire VDBE..*/
1af97 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1af98 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1af99 44 65 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b  Delete(Vdbe *p){
1af9a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1af9b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d  .  if( NEVER(p==
1af9c 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64  0) ) return;.  d
1af9d 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28  b = p->db;.  if(
1af9e 20 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20   p->pPrev ){.   
1af9f 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74   p->pPrev->pNext
1afa0 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d   = p->pNext;.  }
1afa1 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
1afa2 28 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29  ( db->pVdbe==p )
1afa3 3b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20  ;.    db->pVdbe 
1afa4 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  = p->pNext;.  }.
1afa5 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29    if( p->pNext )
1afa6 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e  {.    p->pNext->
1afa7 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76  pPrev = p->pPrev
1afa8 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d  ;.  }.  releaseM
1afa9 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c  emArray(p->aVar,
1afaa 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c   p->nVar);.  rel
1afab 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e  easeMemArray(p->
1afac 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65  aColName, p->nRe
1afad 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f  sColumn*COLNAME_
1afae 4e 29 3b 0a 20 20 76 64 62 65 46 72 65 65 4f 70  N);.  vdbeFreeOp
1afaf 41 72 72 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70  Array(db, p->aOp
1afb0 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c  , p->nOp);.  sql
1afb1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1afb2 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c  ->aLabel);.  sql
1afb3 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1afb4 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73  ->aColName);.  s
1afb5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1afb6 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e   p->zSql);.  p->
1afb7 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47  magic = VDBE_MAG
1afb8 49 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74  IC_DEAD;.  sqlit
1afb9 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1afba 70 46 72 65 65 29 3b 0a 20 20 73 71 6c 69 74 65  pFree);.  sqlite
1afbb 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
1afbc 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
1afbd 72 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 20  re the cursor p 
1afbe 69 73 20 72 65 61 64 79 20 74 6f 20 72 65 61 64  is ready to read
1afbf 20 6f 72 20 77 72 69 74 65 20 74 68 65 20 72 6f   or write the ro
1afc0 77 20 74 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a  w to which it.**
1afc1 20 77 61 73 20 6c 61 73 74 20 70 6f 73 69 74 69   was last positi
1afc2 6f 6e 65 64 2e 20 20 52 65 74 75 72 6e 20 61 6e  oned.  Return an
1afc3 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61   error code if a
1afc4 6e 20 4f 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49  n OOM fault or I
1afc5 2f 4f 20 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76  /O error.** prev
1afc6 65 6e 74 73 20 75 73 20 66 72 6f 6d 20 70 6f 73  ents us from pos
1afc7 69 74 69 6f 6e 69 6e 67 20 74 68 65 20 63 75 72  itioning the cur
1afc8 73 6f 72 20 74 6f 20 69 74 73 20 63 6f 72 72 65  sor to its corre
1afc9 63 74 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a  ct position..**.
1afca 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f  ** If a MoveTo o
1afcb 70 65 72 61 74 69 6f 6e 20 69 73 20 70 65 6e 64  peration is pend
1afcc 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 76 65 6e  ing on the given
1afcd 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f   cursor, then do
1afce 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20   that.** MoveTo 
1afcf 6e 6f 77 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65  now.  If no move
1afd0 20 69 73 20 70 65 6e 64 69 6e 67 2c 20 63 68 65   is pending, che
1afd1 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
1afd2 20 72 6f 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a   row has been.**
1afd3 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f   deleted out fro
1afd4 6d 20 75 6e 64 65 72 20 74 68 65 20 63 75 72 73  m under the curs
1afd5 6f 72 20 61 6e 64 20 69 66 20 69 74 20 68 61 73  or and if it has
1afd6 2c 20 6d 61 72 6b 20 74 68 65 20 72 6f 77 20 61  , mark the row a
1afd7 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e  s.** a NULL row.
1afd8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75  .**.** If the cu
1afd9 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  rsor is already 
1afda 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
1afdb 63 6f 72 72 65 63 74 20 72 6f 77 20 61 6e 64 20  correct row and 
1afdc 74 68 61 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20  that row has.** 
1afdd 6e 6f 74 20 62 65 65 6e 20 64 65 6c 65 74 65 64  not been deleted
1afde 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20   out from under 
1afdf 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 6e  the cursor, then
1afe0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1afe1 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c   a no-op..*/.SQL
1afe2 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1afe3 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1afe4 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73  rMoveto(VdbeCurs
1afe5 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  or *p){.  if( p-
1afe6 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1afe7 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20  ){.    int res, 
1afe8 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  rc;.#ifdef SQLIT
1afe9 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74 65 72  E_TEST.    exter
1afea 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65  n int sqlite3_se
1afeb 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64  arch_count;.#end
1afec 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  if.    assert( p
1afed 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20  ->isTable );.   
1afee 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1afef 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
1aff0 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  (p->pCursor, 0, 
1aff1 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c  p->movetoTarget,
1aff2 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69   0, &res);.    i
1aff3 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1aff4 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f  c;.    p->lastRo
1aff5 77 69 64 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54  wid = p->movetoT
1aff6 61 72 67 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f  arget;.    p->ro
1aff7 77 69 64 49 73 56 61 6c 69 64 20 3d 20 41 4c 57  widIsValid = ALW
1aff8 41 59 53 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30  AYS(res==0) ?1:0
1aff9 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  ;.    if( NEVER(
1affa 72 65 73 3c 30 29 20 29 7b 0a 20 20 20 20 20 20  res<0) ){.      
1affb 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1affc 65 4e 65 78 74 28 70 2d 3e 70 43 75 72 73 6f 72  eNext(p->pCursor
1affd 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69  , &res);.      i
1affe 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1afff 63 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20  c;.    }.#ifdef 
1b000 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20  SQLITE_TEST.    
1b001 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63  sqlite3_search_c
1b002 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
1b003 20 20 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f     p->deferredMo
1b004 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d  veto = 0;.    p-
1b005 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
1b006 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65  ACHE_STALE;.  }e
1b007 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 70  lse if( ALWAYS(p
1b008 2d 3e 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20  ->pCursor) ){.  
1b009 20 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a    int hasMoved;.
1b00a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c      int rc = sql
1b00b 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48  ite3BtreeCursorH
1b00c 61 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73  asMoved(p->pCurs
1b00d 6f 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a  or, &hasMoved);.
1b00e 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1b00f 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20  urn rc;.    if( 
1b010 68 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20  hasMoved ){.    
1b011 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73    p->cacheStatus
1b012 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1b013 20 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77        p->nullRow
1b014 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
1b015 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1b016 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OK;.}../*.** The
1b017 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74   following funct
1b018 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  ions:.**.** sqli
1b019 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1b01a 65 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  e().** sqlite3Vd
1b01b 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1b01c 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  ).** sqlite3Vdbe
1b01d 53 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73  SerialLen().** s
1b01e 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1b01f 50 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33  Put().** sqlite3
1b020 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a  VdbeSerialGet().
1b021 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74  **.** encapsulat
1b022 65 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20  e the code that 
1b023 73 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65  serializes value
1b024 73 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e  s for storage in
1b025 20 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20   SQLite.** data 
1b026 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64  and index record
1b027 73 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a  s. Each serializ
1b028 65 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74  ed value consist
1b029 73 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61  s of a.** 'seria
1b02a 6c 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c  l-type' and a bl
1b02b 6f 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20  ob of data. The 
1b02c 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61  serial type is a
1b02d 6e 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  n 8-byte unsigne
1b02e 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74  d.** integer, st
1b02f 6f 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74  ored as a varint
1b030 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51  ..**.** In an SQ
1b031 4c 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72  Lite index recor
1b032 64 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74 79  d, the serial ty
1b033 70 65 20 69 73 20 73 74 6f 72 65 64 20 64 69 72  pe is stored dir
1b034 65 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20  ectly before.** 
1b035 74 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61  the blob of data
1b036 20 74 68 61 74 20 69 74 20 63 6f 72 72 65 73 70   that it corresp
1b037 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61  onds to. In a ta
1b038 62 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20  ble record, all 
1b039 73 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20  serial.** types 
1b03a 61 72 65 20 73 74 6f 72 65 64 20 61 74 20 74 68  are stored at th
1b03b 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72  e start of the r
1b03c 65 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62  ecord, and the b
1b03d 6c 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a  lobs of data at.
1b03e 2a 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63  ** the end. Henc
1b03f 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  e these function
1b040 73 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c  s allow the call
1b041 65 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65  er to handle the
1b042 0a 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20  .** serial-type 
1b043 61 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65  and data blob se
1b044 70 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20  perately..**.** 
1b045 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  The following ta
1b046 62 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68  ble describes th
1b047 65 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67  e various storag
1b048 65 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61  e classes for da
1b049 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69  ta:.**.**   seri
1b04a 61 6c 20 74 79 70 65 20 20 20 20 20 20 20 20 62  al type        b
1b04b 79 74 65 73 20 6f 66 20 64 61 74 61 20 20 20 20  ytes of data    
1b04c 20 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d    type.**   ----
1b04d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d  ----------     -
1b04e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
1b04f 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
1b050 2d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20  -.**      0     
1b051 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b052 30 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c  0            NUL
1b053 4c 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20  L.**      1     
1b054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b055 31 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67  1            sig
1b056 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  ned integer.**  
1b057 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20      2           
1b058 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20            2     
1b059 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e         signed in
1b05a 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20  teger.**      3 
1b05b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b05c 20 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20      3           
1b05d 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a   signed integer.
1b05e 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20  **      4       
1b05f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20                4 
1b060 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
1b061 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  d integer.**    
1b062 20 20 35 20 20 20 20 20 20 20 20 20 20 20 20 20    5             
1b063 20 20 20 20 20 20 20 20 36 20 20 20 20 20 20 20          6       
1b064 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
1b065 67 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20  ger.**      6   
1b066 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b067 20 20 38 20 20 20 20 20 20 20 20 20 20 20 20 73    8            s
1b068 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
1b069 20 20 20 20 20 20 37 20 20 20 20 20 20 20 20 20        7         
1b06a 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20              8   
1b06b 20 20 20 20 20 20 20 20 20 49 45 45 45 20 66 6c           IEEE fl
1b06c 6f 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20  oat.**      8   
1b06d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b06e 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49    0            I
1b06f 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20  nteger constant 
1b070 30 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20  0.**      9     
1b071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b072 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74  0            Int
1b073 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a  eger constant 1.
1b074 2a 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20 20  **     10,11    
1b075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b076 20 20 20 20 20 20 20 20 20 20 20 72 65 73 65 72             reser
1b077 76 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f  ved for expansio
1b078 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e  n.**    N>=12 an
1b079 64 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d  d even       (N-
1b07a 31 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f  12)/2        BLO
1b07b 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e  B.**    N>=13 an
1b07c 64 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d  d odd        (N-
1b07d 31 33 29 2f 32 20 20 20 20 20 20 20 20 74 65 78  13)/2        tex
1b07e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e  t.**.** The 8 an
1b07f 64 20 39 20 74 79 70 65 73 20 77 65 72 65 20 61  d 9 types were a
1b080 64 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66  dded in 3.3.0, f
1b081 69 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50  ile format 4.  P
1b082 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a  rior versions.**
1b083 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20   of SQLite will 
1b084 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74  not understand t
1b085 68 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70 65  hose serial type
1b086 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  s..*/../*.** Ret
1b087 75 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74  urn the serial-t
1b088 79 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75  ype for the valu
1b089 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d  e stored in pMem
1b08a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b08b 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56  ATE u32 sqlite3V
1b08c 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65  dbeSerialType(Me
1b08d 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c  m *pMem, int fil
1b08e 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74  e_format){.  int
1b08f 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66   flags = pMem->f
1b090 6c 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a  lags;.  int n;..
1b091 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f    if( flags&MEM_
1b092 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75  Null ){.    retu
1b093 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
1b094 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b  flags&MEM_Int ){
1b095 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  .    /* Figure o
1b096 75 74 20 77 68 65 74 68 65 72 20 74 6f 20 75 73  ut whether to us
1b097 65 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20  e 1, 2, 4, 6 or 
1b098 38 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20  8 bytes. */.#   
1b099 64 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45  define MAX_6BYTE
1b09a 20 28 28 28 28 69 36 34 29 30 78 30 30 30 30 38   ((((i64)0x00008
1b09b 30 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20  000)<<32)-1).   
1b09c 20 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75   i64 i = pMem->u
1b09d 2e 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20  .i;.    u64 u;. 
1b09e 20 20 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d     if( file_form
1b09f 61 74 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d  at>=4 && (i&1)==
1b0a0 69 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  i ){.      retur
1b0a1 6e 20 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20  n 8+(u32)i;.    
1b0a2 7d 0a 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20  }.    u = i<0 ? 
1b0a3 2d 69 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20  -i : i;.    if( 
1b0a4 75 3c 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20  u<=127 ) return 
1b0a5 31 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32  1;.    if( u<=32
1b0a6 37 36 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a  767 ) return 2;.
1b0a7 20 20 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36      if( u<=83886
1b0a8 30 37 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20  07 ) return 3;. 
1b0a9 20 20 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38     if( u<=214748
1b0aa 33 36 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b  3647 ) return 4;
1b0ab 0a 20 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f  .    if( u<=MAX_
1b0ac 36 42 59 54 45 20 29 20 72 65 74 75 72 6e 20 35  6BYTE ) return 5
1b0ad 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a  ;.    return 6;.
1b0ae 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26    }.  if( flags&
1b0af 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
1b0b0 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20  return 7;.  }.  
1b0b1 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
1b0b2 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
1b0b3 7c 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72  | flags&(MEM_Str
1b0b4 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20  |MEM_Blob) );.  
1b0b5 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69  n = pMem->n;.  i
1b0b6 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a  f( flags & MEM_Z
1b0b7 65 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20  ero ){.    n += 
1b0b8 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20  pMem->u.nZero;. 
1b0b9 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d   }.  assert( n>=
1b0ba 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28  0 );.  return ((
1b0bb 6e 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c  n*2) + 12 + ((fl
1b0bc 61 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29  ags&MEM_Str)!=0)
1b0bd 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
1b0be 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  rn the length of
1b0bf 20 74 68 65 20 64 61 74 61 20 63 6f 72 72 65 73   the data corres
1b0c0 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73  ponding to the s
1b0c1 75 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74  upplied serial-t
1b0c2 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ype..*/.SQLITE_P
1b0c3 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
1b0c4 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1b0c5 4c 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74  Len(u32 serial_t
1b0c6 79 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69  ype){.  if( seri
1b0c7 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20  al_type>=12 ){. 
1b0c8 20 20 20 72 65 74 75 72 6e 20 28 73 65 72 69 61     return (seria
1b0c9 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20  l_type-12)/2;.  
1b0ca 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69  }else{.    stati
1b0cb 63 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65  c const u8 aSize
1b0cc 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20  [] = { 0, 1, 2, 
1b0cd 33 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30  3, 4, 6, 8, 8, 0
1b0ce 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20  , 0, 0, 0 };.   
1b0cf 20 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65   return aSize[se
1b0d0 72 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a  rial_type];.  }.
1b0d1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61  }../*.** If we a
1b0d2 72 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65  re on an archite
1b0d3 63 74 75 72 65 20 77 69 74 68 20 6d 69 78 65 64  cture with mixed
1b0d4 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67  -endian floating
1b0d5 20 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a   .** points (ex:
1b0d6 20 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70   ARM7) then swap
1b0d7 20 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74   the lower 4 byt
1b0d8 65 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20  es with the .** 
1b0d9 75 70 70 65 72 20 34 20 62 79 74 65 73 2e 20 20  upper 4 bytes.  
1b0da 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c  Return the resul
1b0db 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73  t..**.** For mos
1b0dc 74 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c  t architectures,
1b0dd 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   this is a no-op
1b0de 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a  ..**.** (later):
1b0df 20 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64    It is reported
1b0e0 20 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20   to me that the 
1b0e1 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f  mixed-endian pro
1b0e2 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20  blem.** on ARM7 
1b0e3 69 73 20 61 6e 20 69 73 73 75 65 20 77 69 74 68  is an issue with
1b0e4 20 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74   GCC, not with t
1b0e5 68 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49  he ARM7 chip.  I
1b0e6 74 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20  t seems.** that 
1b0e7 65 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f  early versions o
1b0e8 66 20 47 43 43 20 73 74 6f 72 65 64 20 74 68 65  f GCC stored the
1b0e9 20 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20   two words of a 
1b0ea 36 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20  64-bit.** float 
1b0eb 69 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64  in the wrong ord
1b0ec 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72  er.  And that er
1b0ed 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f  ror has been pro
1b0ee 70 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20  pagated.** ever 
1b0ef 73 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d  since.  The blam
1b0f0 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  e is not necessa
1b0f1 72 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74  rily with GCC, t
1b0f2 68 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69  hough..** GCC mi
1b0f3 67 68 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f  ght have just co
1b0f4 70 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65  pying the proble
1b0f5 6d 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63  m from a prior c
1b0f6 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d  ompiler..** I am
1b0f7 20 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20   also told that 
1b0f8 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  newer versions o
1b0f9 66 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f  f GCC that follo
1b0fa 77 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  w a different.**
1b0fb 20 41 42 49 20 67 65 74 20 74 68 65 20 62 79 74   ABI get the byt
1b0fc 65 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a  e order right..*
1b0fd 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20  *.** Developers 
1b0fe 75 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20  using SQLite on 
1b0ff 61 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63  an ARM7 should c
1b100 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74  ompile and run t
1b101 68 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74  heir.** applicat
1b102 69 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49  ion using -DSQLI
1b103 54 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65  TE_DEBUG=1 at le
1b104 61 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20  ast once.  With 
1b105 44 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64  DEBUG.** enabled
1b106 2c 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62  , some asserts b
1b107 65 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65  elow will ensure
1b108 20 74 68 61 74 20 74 68 65 20 62 79 74 65 20 6f   that the byte o
1b109 72 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74  rder of.** float
1b10a 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73  ing point values
1b10b 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a   is correct..**.
1b10c 2a 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20  ** (2007-08-30) 
1b10d 20 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20   Frank van Vugt 
1b10e 68 61 73 20 73 74 75 64 69 65 64 20 74 68 69 73  has studied this
1b10f 20 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79   problem closely
1b110 0a 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64  .** and has send
1b111 20 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f   his findings to
1b112 20 74 68 65 20 53 51 4c 69 74 65 20 64 65 76 65   the SQLite deve
1b113 6c 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a  lopers.  Frank.*
1b114 2a 20 77 72 69 74 65 73 20 74 68 61 74 20 73 6f  * writes that so
1b115 6d 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73  me Linux kernels
1b116 20 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20   offer floating 
1b117 70 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a  point hardware.*
1b118 2a 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74  * emulation that
1b119 20 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69   uses only 32-bi
1b11a 74 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74  t mantissas inst
1b11b 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a  ead of a full .*
1b11c 2a 20 34 38 2d 62 69 74 73 20 61 73 20 72 65 71  * 48-bits as req
1b11d 75 69 72 65 64 20 62 79 20 74 68 65 20 49 45 45  uired by the IEE
1b11e 45 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68  E standard.  (Th
1b11f 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e  is is the.** CON
1b120 46 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20  FIG_FPE_FASTFPE 
1b121 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63  option.)  On suc
1b122 68 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74  h systems, float
1b123 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74  ing point.** byt
1b124 65 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d  e swapping becom
1b125 65 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61  es very complica
1b126 74 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70  ted.  To avoid p
1b127 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20  roblems,.** the 
1b128 6e 65 63 65 73 73 61 72 79 20 62 79 74 65 20 73  necessary byte s
1b129 77 61 70 70 69 6e 67 20 69 73 20 63 61 72 72 69  wapping is carri
1b12a 65 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36  ed out using a 6
1b12b 34 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a  4-bit integer.**
1b12c 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 36   rather than a 6
1b12d 34 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72  4-bit float.  Fr
1b12e 61 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20 74  ank assures us t
1b12f 68 61 74 20 74 68 65 20 63 6f 64 65 20 68 65 72  hat the code her
1b130 65 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68  e.** works for h
1b131 69 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76  im.  We, the dev
1b132 65 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f  elopers, have no
1b133 20 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64   way to independ
1b134 65 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20  ently.** verify 
1b135 74 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20  this, but Frank 
1b136 73 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68  seems to know wh
1b137 61 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67  at he is talking
1b138 20 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20   about.** so we 
1b139 74 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69  trust him..*/.#i
1b13a 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45  fdef SQLITE_MIXE
1b13b 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46  D_ENDIAN_64BIT_F
1b13c 4c 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34 20  LOAT.static u64 
1b13d 66 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e  floatSwap(u64 in
1b13e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20  ){.  union {.   
1b13f 20 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20   u64 r;.    u32 
1b140 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75  i[2];.  } u;.  u
1b141 33 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69  32 t;..  u.r = i
1b142 6e 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b  n;.  t = u.i[0];
1b143 0a 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b  .  u.i[0] = u.i[
1b144 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74  1];.  u.i[1] = t
1b145 3b 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a  ;.  return u.r;.
1b146 7d 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d  }.# define swapM
1b147 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
1b148 58 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61  X)  X = floatSwa
1b149 70 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  p(X).#else.# def
1b14a 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64  ine swapMixedEnd
1b14b 69 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64  ianFloat(X).#end
1b14c 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  if../*.** Write 
1b14d 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64  the serialized d
1b14e 61 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65  ata blob for the
1b14f 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
1b150 20 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62   pMem into .** b
1b151 75 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65  uf. It is assume
1b152 64 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65  d that the calle
1b153 72 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20  r has allocated 
1b154 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65  sufficient space
1b155 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
1b156 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1b157 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e  written..**.** n
1b158 42 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e  Buf is the amoun
1b159 74 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20  t of space left 
1b15a 69 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20  in buf[].  nBuf 
1b15b 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a  must always be.*
1b15c 2a 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74  * large enough t
1b15d 6f 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72  o hold the entir
1b15e 65 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74  e field.  Except
1b15f 2c 20 69 66 20 74 68 65 20 66 69 65 6c 64 20 69  , if the field i
1b160 73 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68  s.** a blob with
1b161 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74   a zero-filled t
1b162 61 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20  ail, then buf[] 
1b163 6d 69 67 68 74 20 62 65 20 6a 75 73 74 20 74 68  might be just th
1b164 65 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20  e right.** size 
1b165 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69  to hold everythi
1b166 6e 67 20 65 78 63 65 70 74 20 66 6f 72 20 74 68  ng except for th
1b167 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  e zero-filled ta
1b168 69 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a  il.  If buf[].**
1b169 20 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f   is only big eno
1b16a 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ugh to hold the 
1b16b 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c  non-zero prefix,
1b16c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65   then only write
1b16d 20 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20   that.** prefix 
1b16e 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74  into buf[].  But
1b16f 20 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72   if buf[] is lar
1b170 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c  ge enough to hol
1b171 64 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72  d both the.** pr
1b172 65 66 69 78 20 61 6e 64 20 74 68 65 20 74 61 69  efix and the tai
1b173 6c 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65  l then write the
1b174 20 70 72 65 66 69 78 20 61 6e 64 20 73 65 74 20   prefix and set 
1b175 74 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a  the tail to all.
1b176 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20  ** zeros..**.** 
1b177 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1b178 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61  r of bytes actua
1b179 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  lly written into
1b17a 20 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d   buf[].  The num
1b17b 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20  ber.** of bytes 
1b17c 69 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c  in the zero-fill
1b17d 65 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75  ed tail is inclu
1b17e 64 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 72  ded in the retur
1b17f 6e 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20  n value only.** 
1b180 69 66 20 74 68 6f 73 65 20 62 79 74 65 73 20 77  if those bytes w
1b181 65 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75  ere zeroed in bu
1b182 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f  f[]..*/ .SQLITE_
1b183 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69  PRIVATE u32 sqli
1b184 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74  te3VdbeSerialPut
1b185 28 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42  (u8 *buf, int nB
1b186 75 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  uf, Mem *pMem, i
1b187 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b  nt file_format){
1b188 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79  .  u32 serial_ty
1b189 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  pe = sqlite3Vdbe
1b18a 53 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c  SerialType(pMem,
1b18b 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20   file_format);. 
1b18c 20 75 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20   u32 len;..  /* 
1b18d 49 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c  Integer and Real
1b18e 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c   */.  if( serial
1b18f 5f 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69  _type<=7 && seri
1b190 61 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20  al_type>0 ){.   
1b191 20 75 36 34 20 76 3b 0a 20 20 20 20 75 33 32 20   u64 v;.    u32 
1b192 69 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69 61  i;.    if( seria
1b193 6c 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20  l_type==7 ){.   
1b194 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
1b195 66 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65  f(v)==sizeof(pMe
1b196 6d 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d  m->r) );.      m
1b197 65 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d  emcpy(&v, &pMem-
1b198 3e 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a  >r, sizeof(v));.
1b199 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45        swapMixedE
1b19a 6e 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20  ndianFloat(v);. 
1b19b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1b19c 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20  v = pMem->u.i;. 
1b19d 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69     }.    len = i
1b19e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1b19f 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69  rialTypeLen(seri
1b1a0 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73  al_type);.    as
1b1a1 73 65 72 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29  sert( len<=(u32)
1b1a2 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c  nBuf );.    whil
1b1a3 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20  e( i-- ){.      
1b1a4 62 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26  buf[i] = (u8)(v&
1b1a5 30 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e  0xFF);.      v >
1b1a6 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  >= 8;.    }.    
1b1a7 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a  return len;.  }.
1b1a8 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20  .  /* String or 
1b1a9 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65  blob */.  if( se
1b1aa 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b  rial_type>=12 ){
1b1ab 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
1b1ac 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66  m->n + ((pMem->f
1b1ad 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29  lags & MEM_Zero)
1b1ae 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30  ?pMem->u.nZero:0
1b1af 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d  ).             =
1b1b0 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64  = (int)sqlite3Vd
1b1b1 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1b1b2 73 65 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a  serial_type) );.
1b1b3 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
1b1b4 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20  ->n<=nBuf );.   
1b1b5 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a   len = pMem->n;.
1b1b6 20 20 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20      memcpy(buf, 
1b1b7 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20  pMem->z, len);. 
1b1b8 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61     if( pMem->fla
1b1b9 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b  gs & MEM_Zero ){
1b1ba 0a 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d  .      len += pM
1b1bb 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20  em->u.nZero;.   
1b1bc 20 20 20 61 73 73 65 72 74 28 20 6e 42 75 66 3e     assert( nBuf>
1b1bd 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
1b1be 6c 65 6e 20 3e 20 28 75 33 32 29 6e 42 75 66 20  len > (u32)nBuf 
1b1bf 29 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d  ){.        len =
1b1c0 20 28 75 33 32 29 6e 42 75 66 3b 0a 20 20 20 20   (u32)nBuf;.    
1b1c1 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74    }.      memset
1b1c2 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20  (&buf[pMem->n], 
1b1c3 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b  0, len-pMem->n);
1b1c4 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
1b1c5 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  n len;.  }..  /*
1b1c6 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e   NULL or constan
1b1c7 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72  ts 0 or 1 */.  r
1b1c8 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1b1c9 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68  * Deserialize th
1b1ca 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e  e data blob poin
1b1cb 74 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73  ted to by buf as
1b1cc 20 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72   serial type ser
1b1cd 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20  ial_type.** and 
1b1ce 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1b1cf 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72   in pMem.  Retur
1b1d0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1b1d1 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a  bytes read..*/ .
1b1d2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
1b1d3 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  32 sqlite3VdbeSe
1b1d4 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74  rialGet(.  const
1b1d5 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
1b1d6 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66  buf,     /* Buff
1b1d7 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a  er to deserializ
1b1d8 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20  e from */.  u32 
1b1d9 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20  serial_type,    
1b1da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72            /* Ser
1b1db 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65  ial type to dese
1b1dc 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d  rialize */.  Mem
1b1dd 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20   *pMem          
1b1de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
1b1df 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69  mory cell to wri
1b1e0 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f  te value into */
1b1e1 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65  .){.  switch( se
1b1e2 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20  rial_type ){.   
1b1e3 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52   case 10:   /* R
1b1e4 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75  eserved for futu
1b1e5 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61  re use */.    ca
1b1e6 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65  se 11:   /* Rese
1b1e7 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20  rved for future 
1b1e8 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  use */.    case 
1b1e9 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f  0: {  /* NULL */
1b1ea 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
1b1eb 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1b1ec 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1b1ed 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20  }.    case 1: { 
1b1ee 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 1-byte signed
1b1ef 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
1b1f0 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73    pMem->u.i = (s
1b1f1 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30  igned char)buf[0
1b1f2 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ];.      pMem->f
1b1f3 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1b1f4 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1b1f5 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32      }.    case 2
1b1f6 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69  : { /* 2-byte si
1b1f7 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
1b1f8 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
1b1f9 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72  = (((signed char
1b1fa 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62  )buf[0])<<8) | b
1b1fb 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65  uf[1];.      pMe
1b1fc 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
1b1fd 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  nt;.      return
1b1fe 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   2;.    }.    ca
1b1ff 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74  se 3: { /* 3-byt
1b200 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
1b201 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
1b202 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20  u.i = (((signed 
1b203 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36  char)buf[0])<<16
1b204 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20  ) | (buf[1]<<8) 
1b205 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20  | buf[2];.      
1b206 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1b207 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74  M_Int;.      ret
1b208 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 3;.    }.   
1b209 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d   case 4: { /* 4-
1b20a 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
1b20b 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ger */.      pMe
1b20c 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d  m->u.i = (buf[0]
1b20d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c  <<24) | (buf[1]<
1b20e 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c  <16) | (buf[2]<<
1b20f 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20  8) | buf[3];.   
1b210 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1b211 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1b212 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a  return 4;.    }.
1b213 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a      case 5: { /*
1b214 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   6-byte signed i
1b215 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
1b216 75 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65  u64 x = (((signe
1b217 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c  d char)buf[0])<<
1b218 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20  8) | buf[1];.   
1b219 20 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b     u32 y = (buf[
1b21a 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33  2]<<24) | (buf[3
1b21b 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d  ]<<16) | (buf[4]
1b21c 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20  <<8) | buf[5];. 
1b21d 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29       x = (x<<32)
1b21e 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d   | y;.      pMem
1b21f 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26  ->u.i = *(i64*)&
1b220 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  x;.      pMem->f
1b221 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1b222 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a        return 6;.
1b223 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36      }.    case 6
1b224 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69  :   /* 8-byte si
1b225 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
1b226 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a      case 7: { /*
1b227 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70   IEEE floating p
1b228 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36  oint */.      u6
1b229 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79  4 x;.      u32 y
1b22a 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  ;.#if !defined(N
1b22b 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e  DEBUG) && !defin
1b22c 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
1b22d 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20  LOATING_POINT). 
1b22e 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74       /* Verify t
1b22f 68 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64  hat integers and
1b230 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
1b231 76 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73  values use the s
1b232 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74  ame.      ** byt
1b233 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68  e order.  Or, th
1b234 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58  at if SQLITE_MIX
1b235 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f  ED_ENDIAN_64BIT_
1b236 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a  FLOAT is.      *
1b237 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36  * defined that 6
1b238 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70  4-bit floating p
1b239 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c  oint values real
1b23a 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20  ly are mixed.   
1b23b 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20     ** endian..  
1b23c 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61      */.      sta
1b23d 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31  tic const u64 t1
1b23e 20 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30   = ((u64)0x3ff00
1b23f 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20  000)<<32;.      
1b240 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75  static const dou
1b241 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20  ble r1 = 1.0;.  
1b242 20 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b      u64 t2 = t1;
1b243 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64  .      swapMixed
1b244 45 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b  EndianFloat(t2);
1b245 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
1b246 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f  izeof(r1)==sizeo
1b247 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28  f(t2) && memcmp(
1b248 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66  &r1, &t2, sizeof
1b249 28 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64  (r1))==0 );.#end
1b24a 69 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62  if..      x = (b
1b24b 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75  uf[0]<<24) | (bu
1b24c 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66  f[1]<<16) | (buf
1b24d 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d  [2]<<8) | buf[3]
1b24e 3b 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66  ;.      y = (buf
1b24f 5b 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b  [4]<<24) | (buf[
1b250 35 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36  5]<<16) | (buf[6
1b251 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a  ]<<8) | buf[7];.
1b252 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32        x = (x<<32
1b253 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28  ) | y;.      if(
1b254 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20   serial_type==6 
1b255 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  ){.        pMem-
1b256 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78  >u.i = *(i64*)&x
1b257 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
1b258 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1b259 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1b25a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69        assert( si
1b25b 7a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69  zeof(x)==8 && si
1b25c 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38  zeof(pMem->r)==8
1b25d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70   );.        swap
1b25e 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74  MixedEndianFloat
1b25f 28 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  (x);.        mem
1b260 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78  cpy(&pMem->r, &x
1b261 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20  , sizeof(x));.  
1b262 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1b263 73 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  s = sqlite3IsNaN
1b264 28 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f  (pMem->r) ? MEM_
1b265 4e 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b  Null : MEM_Real;
1b266 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1b267 65 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20  eturn 8;.    }. 
1b268 20 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a     case 8:    /*
1b269 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20   Integer 0 */.  
1b26a 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20    case 9: {  /* 
1b26b 49 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20  Integer 1 */.   
1b26c 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73     pMem->u.i = s
1b26d 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20  erial_type-8;.  
1b26e 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1b26f 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
1b270 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
1b271 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
1b272 20 20 20 20 20 20 75 33 32 20 6c 65 6e 20 3d 20        u32 len = 
1b273 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29  (serial_type-12)
1b274 2f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  /2;.      pMem->
1b275 7a 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b  z = (char *)buf;
1b276 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
1b277 20 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d   len;.      pMem
1b278 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20  ->xDel = 0;.    
1b279 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
1b27a 65 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20  e&0x01 ){.      
1b27b 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1b27c 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70  MEM_Str | MEM_Ep
1b27d 68 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  hem;.      }else
1b27e 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
1b27f 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62  flags = MEM_Blob
1b280 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20   | MEM_Ephem;.  
1b281 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
1b282 72 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20  rn len;.    }.  
1b283 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1b284 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68  ../*.** Given th
1b285 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f  e nKey-byte enco
1b286 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64  ding of a record
1b287 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73   in pKey[], pars
1b288 65 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20  e the.** record 
1b289 69 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52  into a UnpackedR
1b28a 65 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e  ecord structure.
1b28b 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
1b28c 65 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74  er to.** that st
1b28d 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
1b28e 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
1b28f 69 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64  ion might provid
1b290 65 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20  e szSpace bytes 
1b291 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61  of memory.** spa
1b292 63 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54  ce at pSpace.  T
1b293 68 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65  his space can be
1b294 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68   used to hold th
1b295 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44  e returned.** VD
1b296 62 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73  beParsedRecord s
1b297 74 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69  tructure if it i
1b298 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20  s large enough. 
1b299 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74   If it is.** not
1b29a 20 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61   big enough, spa
1b29b 63 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ce is obtained f
1b29c 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
1b29d 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oc()..**.** The 
1b29e 72 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75  returned structu
1b29f 72 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f  re should be clo
1b2a0 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  sed by a call to
1b2a1 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44  .** sqlite3VdbeD
1b2a2 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
1b2a3 6f 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54  ord()..*/ .SQLIT
1b2a4 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b  E_PRIVATE Unpack
1b2a5 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65  edRecord *sqlite
1b2a6 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63  3VdbeRecordUnpac
1b2a7 6b 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  k(.  KeyInfo *pK
1b2a8 65 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49  eyInfo,     /* I
1b2a9 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
1b2aa 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d   the record form
1b2ab 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79  at */.  int nKey
1b2ac 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1b2ad 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69  * Size of the bi
1b2ae 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20  nary record */. 
1b2af 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
1b2b0 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62  y,      /* The b
1b2b1 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a  inary record */.
1b2b2 20 20 63 68 61 72 20 2a 70 53 70 61 63 65 2c 20    char *pSpace, 
1b2b3 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c           /* Unal
1b2b4 69 67 6e 65 64 20 73 70 61 63 65 20 61 76 61 69  igned space avai
1b2b5 6c 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68  lable to hold th
1b2b6 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  e object */.  in
1b2b7 74 20 73 7a 53 70 61 63 65 20 20 20 20 20 20 20  t szSpace       
1b2b8 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1b2b9 70 53 70 61 63 65 5b 5d 20 69 6e 20 62 79 74 65  pSpace[] in byte
1b2ba 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  s */.){.  const 
1b2bb 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61  unsigned char *a
1b2bc 4b 65 79 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73  Key = (const uns
1b2bd 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65  igned char *)pKe
1b2be 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63  y;.  UnpackedRec
1b2bf 6f 72 64 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20  ord *p;  /* The 
1b2c0 75 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20  unpacked record 
1b2c1 74 68 61 74 20 77 65 20 77 69 6c 6c 20 72 65 74  that we will ret
1b2c2 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  urn */.  int nBy
1b2c3 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  te;          /* 
1b2c4 4d 65 6d 6f 72 79 20 73 70 61 63 65 20 6e 65 65  Memory space nee
1b2c5 64 65 64 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69  ded to hold p, i
1b2c6 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74  n bytes */.  int
1b2c7 20 64 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20   d;.  u32 idx;. 
1b2c8 20 75 31 36 20 75 3b 20 20 20 20 20 20 20 20 20   u16 u;         
1b2c9 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64       /* Unsigned
1b2ca 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   loop counter */
1b2cb 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 0a 20 20  .  u32 szHdr;.  
1b2cc 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74  Mem *pMem;.  int
1b2cd 20 6e 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20   nOff;          
1b2ce 20 2f 2a 20 49 6e 63 72 65 61 73 65 20 70 53 70   /* Increase pSp
1b2cf 61 63 65 20 62 79 20 74 68 69 73 20 6d 75 63 68  ace by this much
1b2d0 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   to 8-byte align
1b2d1 20 69 74 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20   it */.  .  /*. 
1b2d2 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 73   ** We want to s
1b2d3 68 69 66 74 20 74 68 65 20 70 6f 69 6e 74 65 72  hift the pointer
1b2d4 20 70 53 70 61 63 65 20 75 70 20 73 75 63 68 20   pSpace up such 
1b2d5 74 68 61 74 20 69 74 20 69 73 20 38 2d 62 79 74  that it is 8-byt
1b2d6 65 20 61 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20  e aligned..  ** 
1b2d7 54 68 75 73 2c 20 77 65 20 6e 65 65 64 20 74 6f  Thus, we need to
1b2d8 20 63 61 6c 63 75 6c 61 74 65 20 61 20 76 61 6c   calculate a val
1b2d9 75 65 2c 20 6e 4f 66 66 2c 20 62 65 74 77 65 65  ue, nOff, betwee
1b2da 6e 20 30 20 61 6e 64 20 37 2c 20 74 6f 20 73 68  n 0 and 7, to sh
1b2db 69 66 74 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e  ift .  ** it by.
1b2dc 20 20 49 66 20 70 53 70 61 63 65 20 69 73 20 61    If pSpace is a
1b2dd 6c 72 65 61 64 79 20 38 2d 62 79 74 65 20 61 6c  lready 8-byte al
1b2de 69 67 6e 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75  igned, nOff shou
1b2df 6c 64 20 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f  ld be zero..  */
1b2e0 0a 20 20 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28  .  nOff = (8 - (
1b2e1 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e  SQLITE_PTR_TO_IN
1b2e2 54 28 70 53 70 61 63 65 29 20 26 20 37 29 29 20  T(pSpace) & 7)) 
1b2e3 26 20 37 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d  & 7;.  pSpace +=
1b2e4 20 6e 4f 66 66 3b 0a 20 20 73 7a 53 70 61 63 65   nOff;.  szSpace
1b2e5 20 2d 3d 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74   -= nOff;.  nByt
1b2e6 65 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f  e = ROUND8(sizeo
1b2e7 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  f(UnpackedRecord
1b2e8 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29  )) + sizeof(Mem)
1b2e9 2a 28 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  *(pKeyInfo->nFie
1b2ea 6c 64 2b 31 29 3b 0a 20 20 69 66 28 20 6e 42 79  ld+1);.  if( nBy
1b2eb 74 65 3e 73 7a 53 70 61 63 65 20 29 7b 0a 20 20  te>szSpace ){.  
1b2ec 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d    p = sqlite3DbM
1b2ed 61 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49 6e 66  allocRaw(pKeyInf
1b2ee 6f 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20  o->db, nByte);. 
1b2ef 20 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65     if( p==0 ) re
1b2f0 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66  turn 0;.    p->f
1b2f1 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f  lags = UNPACKED_
1b2f2 4e 45 45 44 5f 46 52 45 45 20 7c 20 55 4e 50 41  NEED_FREE | UNPA
1b2f3 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f  CKED_NEED_DESTRO
1b2f4 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  Y;.  }else{.    
1b2f5 70 20 3d 20 28 55 6e 70 61 63 6b 65 64 52 65 63  p = (UnpackedRec
1b2f6 6f 72 64 2a 29 70 53 70 61 63 65 3b 0a 20 20 20  ord*)pSpace;.   
1b2f7 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41   p->flags = UNPA
1b2f8 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f  CKED_NEED_DESTRO
1b2f9 59 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79  Y;.  }.  p->pKey
1b2fa 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b  Info = pKeyInfo;
1b2fb 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70  .  p->nField = p
1b2fc 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20  KeyInfo->nField 
1b2fd 2b 20 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d  + 1;.  p->aMem =
1b2fe 20 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28   pMem = (Mem*)&(
1b2ff 28 63 68 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38  (char*)p)[ROUND8
1b300 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64  (sizeof(Unpacked
1b301 52 65 63 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73  Record))];.  ass
1b302 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
1b303 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20  ALIGNMENT(pMem) 
1b304 29 3b 0a 20 20 69 64 78 20 3d 20 67 65 74 56 61  );.  idx = getVa
1b305 72 69 6e 74 33 32 28 61 4b 65 79 2c 20 73 7a 48  rint32(aKey, szH
1b306 64 72 29 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72  dr);.  d = szHdr
1b307 3b 0a 20 20 75 20 3d 20 30 3b 0a 20 20 77 68 69  ;.  u = 0;.  whi
1b308 6c 65 28 20 69 64 78 3c 73 7a 48 64 72 20 26 26  le( idx<szHdr &&
1b309 20 75 3c 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20   u<p->nField && 
1b30a 64 3c 3d 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75  d<=nKey ){.    u
1b30b 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a  32 serial_type;.
1b30c 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56  .    idx += getV
1b30d 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b 69 64  arint32(&aKey[id
1b30e 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 29  x], serial_type)
1b30f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20  ;.    pMem->enc 
1b310 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b  = pKeyInfo->enc;
1b311 0a 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20  .    pMem->db = 
1b312 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20  pKeyInfo->db;.  
1b313 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1b314 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61  0;.    pMem->zMa
1b315 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20  lloc = 0;.    d 
1b316 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  += sqlite3VdbeSe
1b317 72 69 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d  rialGet(&aKey[d]
1b318 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70  , serial_type, p
1b319 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b  Mem);.    pMem++
1b31a 3b 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20  ;.    u++;.  }. 
1b31b 20 61 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79   assert( u<=pKey
1b31c 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31  Info->nField + 1
1b31d 20 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20   );.  p->nField 
1b31e 3d 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76  = u;.  return (v
1b31f 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  oid*)p;.}../*.**
1b320 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65   This routine de
1b321 73 74 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65  stroys a Unpacke
1b322 64 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a  dRecord object..
1b323 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b324 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1b325 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64  beDeleteUnpacked
1b326 52 65 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52  Record(UnpackedR
1b327 65 63 6f 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74  ecord *p){.  int
1b328 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b   i;.  Mem *pMem;
1b329 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  ..  assert( p!=0
1b32a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1b32b 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45  >flags & UNPACKE
1b32c 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29  D_NEED_DESTROY )
1b32d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65  ;.  for(i=0, pMe
1b32e 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e  m=p->aMem; i<p->
1b32f 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65  nField; i++, pMe
1b330 6d 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  m++){.    /* The
1b331 20 75 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64   unpacked record
1b332 20 69 73 20 61 6c 77 61 79 73 20 63 6f 6e 73 74   is always const
1b333 72 75 63 74 65 64 20 62 79 20 74 68 65 0a 20 20  ructed by the.  
1b334 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65    ** sqlite3Vdbe
1b335 55 6e 70 61 63 6b 52 65 63 6f 72 64 28 29 20 66  UnpackRecord() f
1b336 75 6e 63 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77  unction above, w
1b337 68 69 63 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20  hich makes all. 
1b338 20 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e     ** strings an
1b339 64 20 62 6c 6f 62 73 20 73 74 61 74 69 63 2e 20  d blobs static. 
1b33a 20 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65   And none of the
1b33b 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 0a 20 20   elements are.  
1b33c 20 20 2a 2a 20 65 76 65 72 20 74 72 61 6e 73 66    ** ever transf
1b33d 6f 72 6d 65 64 2c 20 73 6f 20 74 68 65 72 65 20  ormed, so there 
1b33e 69 73 20 6e 65 76 65 72 20 61 6e 79 74 68 69 6e  is never anythin
1b33f 67 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20  g to delete..   
1b340 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45   */.    if( NEVE
1b341 52 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29  R(pMem->zMalloc)
1b342 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   ) sqlite3VdbeMe
1b343 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
1b344 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61    }.  if( p->fla
1b345 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45  gs & UNPACKED_NE
1b346 45 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 73  ED_FREE ){.    s
1b347 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e  qlite3DbFree(p->
1b348 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29  pKeyInfo->db, p)
1b349 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
1b34a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d  his function com
1b34b 70 61 72 65 73 20 74 68 65 20 74 77 6f 20 74 61  pares the two ta
1b34c 62 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65  ble rows or inde
1b34d 78 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65  x records.** spe
1b34e 63 69 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31  cified by {nKey1
1b34f 2c 20 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b  , pKey1} and pPK
1b350 65 79 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73  ey2.  It returns
1b351 20 61 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72   a negative, zer
1b352 6f 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65  o.** or positive
1b353 20 69 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31   integer if key1
1b354 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65   is less than, e
1b355 71 75 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67  qual to or .** g
1b356 72 65 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32  reater than key2
1b357 2e 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70  .  The {nKey1, p
1b358 4b 65 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62  Key1} key must b
1b359 65 20 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61  e a blob.** crea
1b35a 74 65 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b  ted by th OP_Mak
1b35b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f  eRecord opcode o
1b35c 66 20 74 68 65 20 56 44 42 45 2e 20 20 54 68 65  f the VDBE.  The
1b35d 20 70 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d   pPKey2.** key m
1b35e 75 73 74 20 62 65 20 61 20 70 61 72 73 65 64 20  ust be a parsed 
1b35f 6b 65 79 20 73 75 63 68 20 61 73 20 6f 62 74 61  key such as obta
1b360 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c  ined from.** sql
1b361 69 74 65 33 56 64 62 65 50 61 72 73 65 52 65 63  ite3VdbeParseRec
1b362 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20  ord..**.** Key1 
1b363 61 6e 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20  and Key2 do not 
1b364 68 61 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20  have to contain 
1b365 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20  the same number 
1b366 6f 66 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68  of fields..** Th
1b367 65 20 6b 65 79 20 77 69 74 68 20 66 65 77 65 72  e key with fewer
1b368 20 66 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c   fields is usual
1b369 6c 79 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73  ly compares less
1b36a 20 74 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f   than the .** lo
1b36b 6e 67 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76  nger key.  Howev
1b36c 65 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b  er if the UNPACK
1b36d 45 44 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73  ED_INCRKEY flags
1b36e 20 69 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65   in pPKey2 is se
1b36f 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d  t.** and the com
1b370 6d 6f 6e 20 70 72 65 66 69 78 65 73 20 61 72 65  mon prefixes are
1b371 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79   equal, then key
1b372 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b  1 is less than k
1b373 65 79 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68  ey2..** Or if th
1b374 65 20 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48  e UNPACKED_MATCH
1b375 5f 50 52 45 46 49 58 20 66 6c 61 67 20 69 73 20  _PREFIX flag is 
1b376 73 65 74 20 61 6e 64 20 74 68 65 20 70 72 65 66  set and the pref
1b377 69 78 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61  ixes are.** equa
1b378 6c 2c 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73  l, then the keys
1b379 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20   are considered 
1b37a 74 6f 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a  to be equal and.
1b37b 2a 2a 20 74 68 65 20 70 61 72 74 73 20 62 65 79  ** the parts bey
1b37c 6f 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70  ond the common p
1b37d 72 65 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65  refix are ignore
1b37e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1b37f 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f  UNPACKED_IGNORE_
1b380 52 4f 57 49 44 20 66 6c 61 67 20 69 73 20 73 65  ROWID flag is se
1b381 74 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74  t, then the last
1b382 20 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20   byte of.** the 
1b383 68 65 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20  header of pKey1 
1b384 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20  is ignored.  It 
1b385 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
1b386 70 4b 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69  pKey1 is.** an i
1b387 6e 64 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68  ndex key, and th
1b388 75 73 20 65 6e 64 73 20 77 69 74 68 20 61 20 72  us ends with a r
1b389 6f 77 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65  owid value.  The
1b38a 20 6c 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66   last byte.** of
1b38b 20 74 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c   the header will
1b38c 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 74 68   therefore be th
1b38d 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66  e serial type of
1b38e 20 74 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f   the rowid:.** o
1b38f 6e 65 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34  ne of 1, 2, 3, 4
1b390 2c 20 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20  , 5, 6, 8, or 9 
1b391 2d 20 74 68 65 20 69 6e 74 65 67 65 72 20 73 65  - the integer se
1b392 72 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54  rial types..** T
1b393 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f  he serial type o
1b394 66 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69  f the final rowi
1b395 64 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65  d will always be
1b396 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a   a single byte..
1b397 2a 2a 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74  ** By ignoring t
1b398 68 69 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66  his last byte of
1b399 20 74 68 65 20 68 65 61 64 65 72 2c 20 77 65 20   the header, we 
1b39a 66 6f 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72  force the compar
1b39b 69 73 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72  ison.** to ignor
1b39c 65 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74  e the rowid at t
1b39d 68 65 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a  he end of key1..
1b39e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b39f 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1b3a0 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a  eRecordCompare(.
1b3a1 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e    int nKey1, con
1b3a2 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20  st void *pKey1, 
1b3a3 2f 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20  /* Left key */. 
1b3a4 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1b3a5 2a 70 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f  *pPKey2        /
1b3a6 2a 20 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29  * Right key */.)
1b3a7 7b 0a 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20  {.  int d1;     
1b3a8 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1b3a9 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20   into aKey[] of 
1b3aa 6e 65 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e  next data elemen
1b3ab 74 20 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b  t */.  u32 idx1;
1b3ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
1b3ad 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20  set into aKey[] 
1b3ae 6f 66 20 6e 65 78 74 20 68 65 61 64 65 72 20 65  of next header e
1b3af 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20  lement */.  u32 
1b3b0 73 7a 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f  szHdr1;        /
1b3b1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1b3b2 73 20 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20  s in header */. 
1b3b3 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e   int i = 0;.  in
1b3b4 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20  t nField;.  int 
1b3b5 72 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  rc = 0;.  const 
1b3b6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61  unsigned char *a
1b3b7 4b 65 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e  Key1 = (const un
1b3b8 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b  signed char *)pK
1b3b9 65 79 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  ey1;.  KeyInfo *
1b3ba 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20  pKeyInfo;.  Mem 
1b3bb 6d 65 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66  mem1;..  pKeyInf
1b3bc 6f 20 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79  o = pPKey2->pKey
1b3bd 49 6e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63  Info;.  mem1.enc
1b3be 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63   = pKeyInfo->enc
1b3bf 3b 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b  ;.  mem1.db = pK
1b3c0 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 2f 2a  eyInfo->db;.  /*
1b3c1 20 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20 30 3b   mem1.flags = 0;
1b3c2 20 20 2f 2f 20 57 69 6c 6c 20 62 65 20 69 6e 69    // Will be ini
1b3c3 74 69 61 6c 69 7a 65 64 20 62 79 20 73 71 6c 69  tialized by sqli
1b3c4 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74  te3VdbeSerialGet
1b3c5 28 29 20 2a 2f 0a 20 20 56 56 41 5f 4f 4e 4c 59  () */.  VVA_ONLY
1b3c6 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d  ( mem1.zMalloc =
1b3c7 20 30 3b 20 29 20 2f 2a 20 4f 6e 6c 79 20 6e 65   0; ) /* Only ne
1b3c8 65 64 65 64 20 62 79 20 61 73 73 65 72 74 28 29  eded by assert()
1b3c9 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 0a   statements */..
1b3ca 20 20 2f 2a 20 43 6f 6d 70 69 6c 65 72 73 20 6d    /* Compilers m
1b3cb 61 79 20 63 6f 6d 70 6c 61 69 6e 20 74 68 61 74  ay complain that
1b3cc 20 6d 65 6d 31 2e 75 2e 69 20 69 73 20 70 6f 74   mem1.u.i is pot
1b3cd 65 6e 74 69 61 6c 6c 79 20 75 6e 69 6e 69 74 69  entially uniniti
1b3ce 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a 20 57 65 20  alized..  ** We 
1b3cf 63 6f 75 6c 64 20 69 6e 69 74 69 61 6c 69 7a 65  could initialize
1b3d0 20 69 74 2c 20 61 73 20 73 68 6f 77 6e 20 68 65   it, as shown he
1b3d1 72 65 2c 20 74 6f 20 73 69 6c 65 6e 63 65 20 74  re, to silence t
1b3d2 68 6f 73 65 20 63 6f 6d 70 6c 61 69 6e 74 73 2e  hose complaints.
1b3d3 0a 20 20 2a 2a 20 42 75 74 20 69 6e 20 66 61 63  .  ** But in fac
1b3d4 74 2c 20 6d 65 6d 31 2e 75 2e 69 20 77 69 6c 6c  t, mem1.u.i will
1b3d5 20 6e 65 76 65 72 20 61 63 74 75 61 6c 6c 79 20   never actually 
1b3d6 62 65 20 75 73 65 64 20 69 6e 69 74 69 61 6c 69  be used initiali
1b3d7 7a 65 64 2c 20 61 6e 64 20 64 6f 69 6e 67 20 0a  zed, and doing .
1b3d8 20 20 2a 2a 20 74 68 65 20 75 6e 6e 65 63 65 73    ** the unneces
1b3d9 73 61 72 79 20 69 6e 69 74 69 61 6c 69 7a 61 74  sary initializat
1b3da 69 6f 6e 20 68 61 73 20 61 20 6d 65 61 73 75 72  ion has a measur
1b3db 61 62 6c 65 20 6e 65 67 61 74 69 76 65 20 70 65  able negative pe
1b3dc 72 66 6f 72 6d 61 6e 63 65 0a 20 20 2a 2a 20 69  rformance.  ** i
1b3dd 6d 70 61 63 74 2c 20 73 69 6e 63 65 20 74 68 69  mpact, since thi
1b3de 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 76  s routine is a v
1b3df 65 72 79 20 68 69 67 68 20 72 75 6e 6e 65 72 2e  ery high runner.
1b3e0 20 20 41 6e 64 20 73 6f 2c 20 77 65 20 63 68 6f    And so, we cho
1b3e1 6f 73 65 0a 20 20 2a 2a 20 74 6f 20 69 67 6e 6f  ose.  ** to igno
1b3e2 72 65 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  re the compiler 
1b3e3 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 6c 65 61  warnings and lea
1b3e4 76 65 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  ve this variable
1b3e5 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a   uninitialized..
1b3e6 20 20 2a 2f 0a 20 20 2f 2a 20 20 6d 65 6d 31 2e    */.  /*  mem1.
1b3e7 75 2e 69 20 3d 20 30 3b 20 20 2f 2f 20 6e 6f 74  u.i = 0;  // not
1b3e8 20 6e 65 65 64 65 64 2c 20 68 65 72 65 20 74 6f   needed, here to
1b3e9 20 73 69 6c 65 6e 63 65 20 63 6f 6d 70 69 6c 65   silence compile
1b3ea 72 20 77 61 72 6e 69 6e 67 20 2a 2f 0a 20 20 0a  r warning */.  .
1b3eb 20 20 69 64 78 31 20 3d 20 67 65 74 56 61 72 69    idx1 = getVari
1b3ec 6e 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64  nt32(aKey1, szHd
1b3ed 72 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64  r1);.  d1 = szHd
1b3ee 72 31 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32  r1;.  if( pPKey2
1b3ef 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b  ->flags & UNPACK
1b3f0 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20  ED_IGNORE_ROWID 
1b3f1 29 7b 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b  ){.    szHdr1--;
1b3f2 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20  .  }.  nField = 
1b3f3 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1b3f4 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c  ;.  while( idx1<
1b3f5 73 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65  szHdr1 && i<pPKe
1b3f6 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20  y2->nField ){.  
1b3f7 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
1b3f8 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64  e1;..    /* Read
1b3f9 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   the serial type
1b3fa 73 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65  s for the next e
1b3fb 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b  lement in each k
1b3fc 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20  ey. */.    idx1 
1b3fd 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 20  += getVarint32( 
1b3fe 61 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69  aKey1+idx1, seri
1b3ff 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20  al_type1 );.    
1b400 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26  if( d1>=nKey1 &&
1b401 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1b402 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c  alTypeLen(serial
1b403 5f 74 79 70 65 31 29 3e 30 20 29 20 62 72 65 61  _type1)>0 ) brea
1b404 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61  k;..    /* Extra
1b405 63 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f  ct the values to
1b406 20 62 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20   be compared..  
1b407 20 20 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73    */.    d1 += s
1b408 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1b409 47 65 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20  Get(&aKey1[d1], 
1b40a 73 65 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d  serial_type1, &m
1b40b 65 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f  em1);..    /* Do
1b40c 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a   the comparison.
1b40d 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
1b40e 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72  sqlite3MemCompar
1b40f 65 28 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32  e(&mem1, &pPKey2
1b410 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20  ->aMem[i],.     
1b411 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b412 20 20 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f        i<nField ?
1b413 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c   pKeyInfo->aColl
1b414 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66  [i] : 0);.    if
1b415 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20  ( rc!=0 ){.     
1b416 20 61 73 73 65 72 74 28 20 6d 65 6d 31 2e 7a 4d   assert( mem1.zM
1b417 61 6c 6c 6f 63 3d 3d 30 20 29 3b 20 20 2f 2a 20  alloc==0 );  /* 
1b418 53 65 65 20 63 6f 6d 6d 65 6e 74 20 62 65 6c 6f  See comment belo
1b419 77 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49  w */..      /* I
1b41a 6e 76 65 72 74 20 74 68 65 20 72 65 73 75 6c 74  nvert the result
1b41b 20 69 66 20 77 65 20 61 72 65 20 75 73 69 6e 67   if we are using
1b41c 20 44 45 53 43 20 73 6f 72 74 20 6f 72 64 65 72   DESC sort order
1b41d 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70  . */.      if( p
1b41e 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72  KeyInfo->aSortOr
1b41f 64 65 72 20 26 26 20 69 3c 6e 46 69 65 6c 64 20  der && i<nField 
1b420 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f  && pKeyInfo->aSo
1b421 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20  rtOrder[i] ){.  
1b422 20 20 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a        rc = -rc;.
1b423 20 20 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20        }.    .   
1b424 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45     /* If the PRE
1b425 46 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20  FIX_SEARCH flag 
1b426 69 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66  is set and all f
1b427 69 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 65  ields except the
1b428 20 66 69 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20   final.      ** 
1b429 72 6f 77 69 64 20 66 69 65 6c 64 20 77 65 72 65  rowid field were
1b42a 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 63 6c 65   equal, then cle
1b42b 61 72 20 74 68 65 20 50 52 45 46 49 58 5f 53 45  ar the PREFIX_SE
1b42c 41 52 43 48 20 66 6c 61 67 20 61 6e 64 20 73 65  ARCH flag and se
1b42d 74 20 0a 20 20 20 20 20 20 2a 2a 20 70 50 4b 65  t .      ** pPKe
1b42e 79 32 2d 3e 72 6f 77 69 64 20 74 6f 20 74 68 65  y2->rowid to the
1b42f 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f   value of the ro
1b430 77 69 64 20 66 69 65 6c 64 20 69 6e 20 28 70 4b  wid field in (pK
1b431 65 79 31 2c 20 6e 4b 65 79 31 29 2e 0a 20 20 20  ey1, nKey1)..   
1b432 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 75 73     ** This is us
1b433 65 64 20 62 79 20 74 68 65 20 4f 50 5f 49 73 55  ed by the OP_IsU
1b434 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e 0a 20 20  nique opcode..  
1b435 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
1b436 20 28 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20   (pPKey2->flags 
1b437 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  & UNPACKED_PREFI
1b438 58 5f 53 45 41 52 43 48 29 20 26 26 20 69 3d 3d  X_SEARCH) && i==
1b439 28 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 2d  (pPKey2->nField-
1b43a 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  1) ){.        as
1b43b 73 65 72 74 28 20 69 64 78 31 3d 3d 73 7a 48 64  sert( idx1==szHd
1b43c 72 31 20 26 26 20 72 63 20 29 3b 0a 20 20 20 20  r1 && rc );.    
1b43d 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 31      assert( mem1
1b43e 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  .flags & MEM_Int
1b43f 20 29 3b 0a 20 20 20 20 20 20 20 20 70 50 4b 65   );.        pPKe
1b440 79 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e  y2->flags &= ~UN
1b441 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45  PACKED_PREFIX_SE
1b442 41 52 43 48 3b 0a 20 20 20 20 20 20 20 20 70 50  ARCH;.        pP
1b443 4b 65 79 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65  Key2->rowid = me
1b444 6d 31 2e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 0a  m1.u.i;.      }.
1b445 20 20 20 20 0a 20 20 20 20 20 20 72 65 74 75 72      .      retur
1b446 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
1b447 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e  i++;.  }..  /* N
1b448 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  o memory allocat
1b449 69 6f 6e 20 69 73 20 65 76 65 72 20 75 73 65 64  ion is ever used
1b44a 20 6f 6e 20 6d 65 6d 31 2e 20 20 50 72 6f 76 65   on mem1.  Prove
1b44b 20 74 68 69 73 20 75 73 69 6e 67 0a 20 20 2a 2a   this using.  **
1b44c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
1b44d 73 73 65 72 74 28 29 2e 20 20 49 66 20 74 68 65  ssert().  If the
1b44e 20 61 73 73 65 72 74 28 29 20 66 61 69 6c 73 2c   assert() fails,
1b44f 20 69 74 20 69 6e 64 69 63 61 74 65 73 20 61 0a   it indicates a.
1b450 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 6c 65 61 6b    ** memory leak
1b451 20 61 6e 64 20 61 20 6e 65 65 64 20 74 6f 20 63   and a need to c
1b452 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 4d  all sqlite3VdbeM
1b453 65 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29  emRelease(&mem1)
1b454 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
1b455 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 3d 3d 30   mem1.zMalloc==0
1b456 20 29 3b 0a 0a 20 20 2f 2a 20 72 63 3d 3d 30 20   );..  /* rc==0 
1b457 68 65 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20  here means that 
1b458 6f 6e 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20  one of the keys 
1b459 72 61 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64  ran out of field
1b45a 73 20 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 20 74  s and.  ** all t
1b45b 68 65 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20  he fields up to 
1b45c 74 68 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20  that point were 
1b45d 65 71 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e  equal. If the UN
1b45e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a 20  PACKED_INCRKEY. 
1b45f 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c   ** flag is set,
1b460 20 74 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20   then break the 
1b461 74 69 65 20 62 79 20 74 72 65 61 74 69 6e 67 20  tie by treating 
1b462 6b 65 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0a  key2 as larger..
1b463 20 20 2a 2a 20 49 66 20 74 68 65 20 55 50 41 43    ** If the UPAC
1b464 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48  KED_PREFIX_MATCH
1b465 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
1b466 65 6e 20 6b 65 79 73 20 77 69 74 68 20 63 6f 6d  en keys with com
1b467 6d 6f 6e 20 70 72 65 66 69 78 65 73 0a 20 20 2a  mon prefixes.  *
1b468 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64  * are considered
1b469 20 74 6f 20 62 65 20 65 71 75 61 6c 2e 20 20 4f   to be equal.  O
1b46a 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 6c 6f  therwise, the lo
1b46b 6e 67 65 72 20 6b 65 79 20 69 73 20 74 68 65 20  nger key is the 
1b46c 0a 20 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41  .  ** larger.  A
1b46d 73 20 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68  s it happens, th
1b46e 65 20 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c  e pPKey2 will al
1b46f 77 61 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67  ways be the long
1b470 65 72 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65  er.  ** if there
1b471 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65   is a difference
1b472 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
1b473 20 72 63 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20   rc==0 );.  if( 
1b474 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20  pPKey2->flags & 
1b475 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
1b476 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 2d 31 3b   ){.    rc = -1;
1b477 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 4b  .  }else if( pPK
1b478 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50  ey2->flags & UNP
1b479 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
1b47a 43 48 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 61  CH ){.    /* Lea
1b47b 76 65 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 7d 65  ve rc==0 */.  }e
1b47c 6c 73 65 20 69 66 28 20 69 64 78 31 3c 73 7a 48  lse if( idx1<szH
1b47d 64 72 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  dr1 ){.    rc = 
1b47e 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
1b47f 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70  rc;.}. ../*.** p
1b480 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e  Cur points at an
1b481 20 69 6e 64 65 78 20 65 6e 74 72 79 20 63 72 65   index entry cre
1b482 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f  ated using the O
1b483 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63  P_MakeRecord opc
1b484 6f 64 65 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65  ode..** Read the
1b485 20 72 6f 77 69 64 20 28 74 68 65 20 6c 61 73 74   rowid (the last
1b486 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 72 65   field in the re
1b487 63 6f 72 64 29 20 61 6e 64 20 73 74 6f 72 65 20  cord) and store 
1b488 69 74 20 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a  it in *rowid..**
1b489 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
1b48a 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20  K if everything 
1b48b 77 6f 72 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72  works, or an err
1b48c 6f 72 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73  or code otherwis
1b48d 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 20 6d 69  e..**.** pCur mi
1b48e 67 68 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20  ght be pointing 
1b48f 74 6f 20 74 65 78 74 20 6f 62 74 61 69 6e 65 64  to text obtained
1b490 20 66 72 6f 6d 20 61 20 63 6f 72 72 75 70 74 20   from a corrupt 
1b491 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1b492 2a 20 53 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74  * So the content
1b493 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74   cannot be trust
1b494 65 64 2e 20 20 44 6f 20 61 70 70 72 6f 70 72 69  ed.  Do appropri
1b495 61 74 65 20 63 68 65 63 6b 73 20 6f 6e 20 74 68  ate checks on th
1b496 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51  e content..*/.SQ
1b497 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b498 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52   sqlite3VdbeIdxR
1b499 6f 77 69 64 28 73 71 6c 69 74 65 33 20 2a 64 62  owid(sqlite3 *db
1b49a 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  , BtCursor *pCur
1b49b 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b 0a 20  , i64 *rowid){. 
1b49c 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20   i64 nCellKey = 
1b49d 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75  0;.  int rc;.  u
1b49e 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20  32 szHdr;       
1b49f 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
1b4a0 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20  header */.  u32 
1b4a1 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20 2f 2a  typeRowid;    /*
1b4a2 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20   Serial type of 
1b4a3 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 75  the rowid */.  u
1b4a4 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20 20  32 lenRowid;    
1b4a5 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
1b4a6 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d 20 6d  rowid */.  Mem m
1b4a7 2c 20 76 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50  , v;..  UNUSED_P
1b4a8 41 52 41 4d 45 54 45 52 28 64 62 29 3b 0a 0a 20  ARAMETER(db);.. 
1b4a9 20 2f 2a 20 47 65 74 20 74 68 65 20 73 69 7a 65   /* Get the size
1b4aa 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e   of the index en
1b4ab 74 72 79 2e 20 20 4f 6e 6c 79 20 69 6e 64 69 63  try.  Only indic
1b4ac 65 73 20 65 6e 74 72 69 65 73 20 6f 66 20 6c 65  es entries of le
1b4ad 73 73 0a 20 20 2a 2a 20 74 68 61 6e 20 32 47 69  ss.  ** than 2Gi
1b4ae 42 20 61 72 65 20 73 75 70 70 6f 72 74 20 2d 20  B are support - 
1b4af 61 6e 79 74 68 69 6e 67 20 6c 61 72 67 65 20 6d  anything large m
1b4b0 75 73 74 20 62 65 20 64 61 74 61 62 61 73 65 20  ust be database 
1b4b1 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 2a 2a  corruption..  **
1b4b2 20 41 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 20   Any corruption 
1b4b3 69 73 20 64 65 74 65 63 74 65 64 20 69 6e 20 73  is detected in s
1b4b4 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
1b4b5 43 65 6c 6c 50 74 72 28 29 2c 20 74 68 6f 75 67  CellPtr(), thoug
1b4b6 68 2c 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20  h, so.  ** this 
1b4b7 63 6f 64 65 20 63 61 6e 20 73 61 66 65 6c 79 20  code can safely 
1b4b8 61 73 73 75 6d 65 20 74 68 61 74 20 6e 43 65 6c  assume that nCel
1b4b9 6c 4b 65 79 20 69 73 20 33 32 2d 62 69 74 73 20  lKey is 32-bits 
1b4ba 20 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28   .  */.  assert(
1b4bb 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
1b4bc 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 29  sorIsValid(pCur)
1b4bd 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
1b4be 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70  e3BtreeKeySize(p
1b4bf 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b  Cur, &nCellKey);
1b4c0 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
1b4c1 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 20  QLITE_OK );     
1b4c2 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77 61 79  /* pCur is alway
1b4c3 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79 53 69  s valid so KeySi
1b4c4 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a  ze cannot fail *
1b4c5 2f 0a 20 20 61 73 73 65 72 74 28 20 28 6e 43 65  /.  assert( (nCe
1b4c6 6c 6c 4b 65 79 20 26 20 53 51 4c 49 54 45 5f 4d  llKey & SQLITE_M
1b4c7 41 58 5f 55 33 32 29 3d 3d 28 75 36 34 29 6e 43  AX_U32)==(u64)nC
1b4c8 65 6c 6c 4b 65 79 20 29 3b 0a 0a 20 20 2f 2a 20  ellKey );..  /* 
1b4c9 52 65 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70  Read in the comp
1b4ca 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20  lete content of 
1b4cb 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20  the index entry 
1b4cc 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20  */.  memset(&m, 
1b4cd 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20  0, sizeof(m));. 
1b4ce 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1b4cf 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43  eMemFromBtree(pC
1b4d0 75 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c  ur, 0, (int)nCel
1b4d1 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20  lKey, 1, &m);.  
1b4d2 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
1b4d3 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
1b4d4 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74  /* The index ent
1b4d5 72 79 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69  ry must begin wi
1b4d6 74 68 20 61 20 68 65 61 64 65 72 20 73 69 7a 65  th a header size
1b4d7 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56   */.  (void)getV
1b4d8 61 72 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a  arint32((u8*)m.z
1b4d9 2c 20 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74  , szHdr);.  test
1b4da 63 61 73 65 28 20 73 7a 48 64 72 3d 3d 33 20 29  case( szHdr==3 )
1b4db 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 7a  ;.  testcase( sz
1b4dc 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66  Hdr==m.n );.  if
1b4dd 28 20 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72  ( unlikely(szHdr
1b4de 3c 33 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72  <3 || (int)szHdr
1b4df 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74  >m.n) ){.    got
1b4e0 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72  o idx_rowid_corr
1b4e1 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  uption;.  }..  /
1b4e2 2a 20 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64  * The last field
1b4e3 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 73 68   of the index sh
1b4e4 6f 75 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67  ould be an integ
1b4e5 65 72 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a  er - the ROWID..
1b4e6 20 20 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74    ** Verify that
1b4e7 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
1b4e8 72 65 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74  really is an int
1b4e9 65 67 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64  eger. */.  (void
1b4ea 29 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38  )getVarint32((u8
1b4eb 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c  *)&m.z[szHdr-1],
1b4ec 20 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74   typeRowid);.  t
1b4ed 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1b4ee 69 64 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63  id==1 );.  testc
1b4ef 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1b4f0 32 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  2 );.  testcase(
1b4f1 20 74 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b   typeRowid==3 );
1b4f2 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1b4f3 65 52 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74  eRowid==4 );.  t
1b4f4 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1b4f5 69 64 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63  id==5 );.  testc
1b4f6 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1b4f7 36 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  6 );.  testcase(
1b4f8 20 74 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b   typeRowid==8 );
1b4f9 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1b4fa 65 52 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69  eRowid==9 );.  i
1b4fb 66 28 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65  f( unlikely(type
1b4fc 52 6f 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52  Rowid<1 || typeR
1b4fd 6f 77 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f  owid>9 || typeRo
1b4fe 77 69 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67  wid==7) ){.    g
1b4ff 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f  oto idx_rowid_co
1b500 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20  rruption;.  }.  
1b501 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74  lenRowid = sqlit
1b502 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1b503 4c 65 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a  Len(typeRowid);.
1b504 20 20 74 65 73 74 63 61 73 65 28 20 28 75 33 32    testcase( (u32
1b505 29 6d 2e 6e 3d 3d 73 7a 48 64 72 2b 6c 65 6e 52  )m.n==szHdr+lenR
1b506 6f 77 69 64 20 29 3b 0a 20 20 69 66 28 20 75 6e  owid );.  if( un
1b507 6c 69 6b 65 6c 79 28 28 75 33 32 29 6d 2e 6e 3c  likely((u32)m.n<
1b508 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64 29 20  szHdr+lenRowid) 
1b509 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f  ){.    goto idx_
1b50a 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e  rowid_corruption
1b50b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 65 74 63  ;.  }..  /* Fetc
1b50c 68 20 74 68 65 20 69 6e 74 65 67 65 72 20 6f 66  h the integer of
1b50d 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
1b50e 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f   index record */
1b50f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  .  sqlite3VdbeSe
1b510 72 69 61 6c 47 65 74 28 28 75 38 2a 29 26 6d 2e  rialGet((u8*)&m.
1b511 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c  z[m.n-lenRowid],
1b512 20 74 79 70 65 52 6f 77 69 64 2c 20 26 76 29 3b   typeRowid, &v);
1b513 0a 20 20 2a 72 6f 77 69 64 20 3d 20 76 2e 75 2e  .  *rowid = v.u.
1b514 69 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  i;.  sqlite3Vdbe
1b515 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a  MemRelease(&m);.
1b516 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1b517 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68  OK;..  /* Jump h
1b518 65 72 65 20 69 66 20 64 61 74 61 62 61 73 65 20  ere if database 
1b519 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65  corruption is de
1b51a 74 65 63 74 65 64 20 61 66 74 65 72 20 6d 20 68  tected after m h
1b51b 61 73 20 62 65 65 6e 0a 20 20 2a 2a 20 61 6c 6c  as been.  ** all
1b51c 6f 63 61 74 65 64 2e 20 20 46 72 65 65 20 74 68  ocated.  Free th
1b51d 65 20 6d 20 6f 62 6a 65 63 74 20 61 6e 64 20 72  e m object and r
1b51e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
1b51f 52 55 50 54 2e 20 2a 2f 0a 69 64 78 5f 72 6f 77  RUPT. */.idx_row
1b520 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3a 0a 20  id_corruption:. 
1b521 20 74 65 73 74 63 61 73 65 28 20 6d 2e 7a 4d 61   testcase( m.zMa
1b522 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20 20 73 71 6c  lloc!=0 );.  sql
1b523 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1b524 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e  se(&m);.  return
1b525 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1b526 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  BKPT;.}../*.** C
1b527 6f 6d 70 61 72 65 20 74 68 65 20 6b 65 79 20 6f  ompare the key o
1b528 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  f the index entr
1b529 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 43  y that cursor pC
1b52a 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   is pointing to 
1b52b 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 6b  against.** the k
1b52c 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70 55 6e  ey string in pUn
1b52d 70 61 63 6b 65 64 2e 20 20 57 72 69 74 65 20 69  packed.  Write i
1b52e 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62  nto *pRes a numb
1b52f 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65  er.** that is ne
1b530 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72  gative, zero, or
1b531 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 43 20   positive if pC 
1b532 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71  is less than, eq
1b533 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72  ual to,.** or gr
1b534 65 61 74 65 72 20 74 68 61 6e 20 70 55 6e 70 61  eater than pUnpa
1b535 63 6b 65 64 2e 20 20 52 65 74 75 72 6e 20 53 51  cked.  Return SQ
1b536 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
1b537 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63  ss..**.** pUnpac
1b538 6b 65 64 20 69 73 20 65 69 74 68 65 72 20 63 72  ked is either cr
1b539 65 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 20  eated without a 
1b53a 72 6f 77 69 64 20 6f 72 20 69 73 20 74 72 75 6e  rowid or is trun
1b53b 63 61 74 65 64 20 73 6f 20 74 68 61 74 20 69 74  cated so that it
1b53c 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 65 20 72 6f  .** omits the ro
1b53d 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 2e 20  wid at the end. 
1b53e 20 54 68 65 20 72 6f 77 69 64 20 61 74 20 74 68   The rowid at th
1b53f 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64  e end of the ind
1b540 65 78 20 65 6e 74 72 79 0a 2a 2a 20 69 73 20 69  ex entry.** is i
1b541 67 6e 6f 72 65 64 20 61 73 20 77 65 6c 6c 2e 20  gnored as well. 
1b542 20 48 65 6e 63 65 2c 20 74 68 69 73 20 72 6f 75   Hence, this rou
1b543 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6d 70 61 72  tine only compar
1b544 65 73 20 74 68 65 20 70 72 65 66 69 78 65 73 20  es the prefixes 
1b545 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65 79 73 20  .** of the keys 
1b546 70 72 69 6f 72 20 74 6f 20 74 68 65 20 66 69 6e  prior to the fin
1b547 61 6c 20 72 6f 77 69 64 2c 20 6e 6f 74 20 74 68  al rowid, not th
1b548 65 20 65 6e 74 69 72 65 20 6b 65 79 2e 0a 2a 2f  e entire key..*/
1b549 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b54a 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
1b54b 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20  dxKeyCompare(.  
1b54c 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c 20  VdbeCursor *pC, 
1b54d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1b54e 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d  he cursor to com
1b54f 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a  pare against */.
1b550 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1b551 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f 2a   *pUnpacked,  /*
1b552 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f   Unpacked versio
1b553 6e 20 6f 66 20 6b 65 79 20 74 6f 20 63 6f 6d 70  n of key to comp
1b554 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20  are against */. 
1b555 20 69 6e 74 20 2a 72 65 73 20 20 20 20 20 20 20   int *res       
1b556 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b557 57 72 69 74 65 20 74 68 65 20 63 6f 6d 70 61 72  Write the compar
1b558 69 73 6f 6e 20 72 65 73 75 6c 74 20 68 65 72 65  ison result here
1b559 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20 6e 43 65   */.){.  i64 nCe
1b55a 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74  llKey = 0;.  int
1b55b 20 72 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20   rc;.  BtCursor 
1b55c 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70 43 75 72  *pCur = pC->pCur
1b55d 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b 0a 0a 20  sor;.  Mem m;.. 
1b55e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b55f 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c  BtreeCursorIsVal
1b560 69 64 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  id(pCur) );.  rc
1b561 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
1b562 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e 43  eySize(pCur, &nC
1b563 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 73 73 65 72  ellKey);.  asser
1b564 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
1b565 20 29 3b 20 20 20 20 2f 2a 20 70 43 75 72 20 69   );    /* pCur i
1b566 73 20 61 6c 77 61 79 73 20 76 61 6c 69 64 20 73  s always valid s
1b567 6f 20 4b 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74  o KeySize cannot
1b568 20 66 61 69 6c 20 2a 2f 0a 20 20 2f 2a 20 6e 43   fail */.  /* nC
1b569 65 6c 6c 4b 65 79 20 77 69 6c 6c 20 61 6c 77 61  ellKey will alwa
1b56a 79 73 20 62 65 20 62 65 74 77 65 65 6e 20 30 20  ys be between 0 
1b56b 61 6e 64 20 30 78 66 66 66 66 66 66 66 66 20 62  and 0xffffffff b
1b56c 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 73 61  ecause of the sa
1b56d 79 0a 20 20 2a 2a 20 74 68 61 74 20 62 74 72 65  y.  ** that btre
1b56e 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20  eParseCellPtr() 
1b56f 61 6e 64 20 73 71 6c 69 74 65 33 47 65 74 56 61  and sqlite3GetVa
1b570 72 69 6e 74 33 32 28 29 20 61 72 65 20 69 6d 70  rint32() are imp
1b571 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a 20 20 69 66  lemented */.  if
1b572 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c  ( nCellKey<=0 ||
1b573 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66   nCellKey>0x7fff
1b574 66 66 66 66 20 29 7b 0a 20 20 20 20 2a 72 65 73  ffff ){.    *res
1b575 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
1b576 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b   SQLITE_CORRUPT;
1b577 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d  .  }.  memset(&m
1b578 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b  , 0, sizeof(m));
1b579 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
1b57a 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1b57b 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  pC->pCursor, 0, 
1b57c 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31  (int)nCellKey, 1
1b57d 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20  , &m);.  if( rc 
1b57e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
1b57f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1b580 70 55 6e 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73  pUnpacked->flags
1b581 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f   & UNPACKED_IGNO
1b582 52 45 5f 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72  RE_ROWID );.  *r
1b583 65 73 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  es = sqlite3Vdbe
1b584 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e  RecordCompare(m.
1b585 6e 2c 20 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65  n, m.z, pUnpacke
1b586 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  d);.  sqlite3Vdb
1b587 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b  eMemRelease(&m);
1b588 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1b589 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
1b58a 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
1b58b 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20  the value to be 
1b58c 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 73  returned by subs
1b58d 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a  equent calls to.
1b58e 2a 2a 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  ** sqlite3_chang
1b58f 65 73 28 29 20 6f 6e 20 74 68 65 20 64 61 74 61  es() on the data
1b590 62 61 73 65 20 68 61 6e 64 6c 65 20 27 64 62 27  base handle 'db'
1b591 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
1b592 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1b593 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
1b594 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
1b595 20 6e 43 68 61 6e 67 65 29 7b 0a 20 20 61 73 73   nChange){.  ass
1b596 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b597 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
1b598 78 29 20 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61  x) );.  db->nCha
1b599 6e 67 65 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20  nge = nChange;. 
1b59a 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67   db->nTotalChang
1b59b 65 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a  e += nChange;.}.
1b59c 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61  ./*.** Set a fla
1b59d 67 20 69 6e 20 74 68 65 20 76 64 62 65 20 74 6f  g in the vdbe to
1b59e 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e   update the chan
1b59f 67 65 20 63 6f 75 6e 74 65 72 20 77 68 65 6e 20  ge counter when 
1b5a0 69 74 20 69 73 20 66 69 6e 61 6c 69 73 65 64 0a  it is finalised.
1b5a1 2a 2a 20 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a  ** or reset..*/.
1b5a2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1b5a3 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
1b5a4 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65  ountChanges(Vdbe
1b5a5 20 2a 76 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67   *v){.  v->chang
1b5a6 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f  eCntOn = 1;.}../
1b5a7 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76 65 72 79 20  *.** Mark every 
1b5a8 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1b5a9 6e 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  nt associated wi
1b5aa 74 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  th a database co
1b5ab 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65  nnection.** as e
1b5ac 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e  xpired..**.** An
1b5ad 20 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65   expired stateme
1b5ae 6e 74 20 6d 65 61 6e 73 20 74 68 61 74 20 72 65  nt means that re
1b5af 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74  compilation of t
1b5b0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 0a  he statement is.
1b5b1 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53  ** recommend.  S
1b5b2 74 61 74 65 6d 65 6e 74 73 20 65 78 70 69 72 65  tatements expire
1b5b3 20 77 68 65 6e 20 74 68 69 6e 67 73 20 68 61 70   when things hap
1b5b4 70 65 6e 20 74 68 61 74 20 6d 61 6b 65 20 74 68  pen that make th
1b5b5 65 69 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20  eir.** programs 
1b5b6 6f 62 73 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76  obsolete.  Remov
1b5b7 69 6e 67 20 75 73 65 72 2d 64 65 66 69 6e 65 64  ing user-defined
1b5b8 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f   functions or co
1b5b9 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65  llating.** seque
1b5ba 6e 63 65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e  nces, or changin
1b5bb 67 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69  g an authorizati
1b5bc 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20  on function are 
1b5bd 74 68 65 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20  the types of.** 
1b5be 74 68 69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65  things that make
1b5bf 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1b5c0 65 6e 74 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a  ents obsolete..*
1b5c1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b5c2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
1b5c3 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
1b5c4 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64  ments(sqlite3 *d
1b5c5 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20  b){.  Vdbe *p;. 
1b5c6 20 66 6f 72 28 70 20 3d 20 64 62 2d 3e 70 56 64   for(p = db->pVd
1b5c7 62 65 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  be; p; p=p->pNex
1b5c8 74 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72  t){.    p->expir
1b5c9 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ed = 1;.  }.}../
1b5ca 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1b5cb 64 61 74 61 62 61 73 65 20 61 73 73 6f 63 69 61  database associa
1b5cc 74 65 64 20 77 69 74 68 20 74 68 65 20 56 64 62  ted with the Vdb
1b5cd 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1b5ce 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71  VATE sqlite3 *sq
1b5cf 6c 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65  lite3VdbeDb(Vdbe
1b5d0 20 2a 76 29 7b 0a 20 20 72 65 74 75 72 6e 20 76   *v){.  return v
1b5d1 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ->db;.}../*.** R
1b5d2 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1b5d3 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61  to an sqlite3_va
1b5d4 6c 75 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  lue structure co
1b5d5 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c  ntaining the val
1b5d6 75 65 20 62 6f 75 6e 64 0a 2a 2a 20 70 61 72 61  ue bound.** para
1b5d7 6d 65 74 65 72 20 69 56 61 72 20 6f 66 20 56 4d  meter iVar of VM
1b5d8 20 76 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74   v. Except, if t
1b5d9 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53  he value is an S
1b5da 51 4c 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20  QL NULL, return 
1b5db 0a 2a 2a 20 30 20 69 6e 73 74 65 61 64 2e 20 55  .** 0 instead. U
1b5dc 6e 6c 65 73 73 20 69 74 20 69 73 20 4e 55 4c 4c  nless it is NULL
1b5dd 2c 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79  , apply affinity
1b5de 20 61 66 66 20 28 6f 6e 65 20 6f 66 20 74 68 65   aff (one of the
1b5df 20 53 51 4c 49 54 45 5f 41 46 46 5f 2a 0a 2a 2a   SQLITE_AFF_*.**
1b5e0 20 63 6f 6e 73 74 61 6e 74 73 29 20 74 6f 20 74   constants) to t
1b5e1 68 65 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20  he value before 
1b5e2 72 65 74 75 72 6e 69 6e 67 20 69 74 2e 0a 2a 2a  returning it..**
1b5e3 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
1b5e4 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 66   value must be f
1b5e5 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  reed by the call
1b5e6 65 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  er using sqlite3
1b5e7 56 61 6c 75 65 46 72 65 65 28 29 2e 0a 2a 2f 0a  ValueFree()..*/.
1b5e8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
1b5e9 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71  qlite3_value *sq
1b5ea 6c 69 74 65 33 56 64 62 65 47 65 74 56 61 6c 75  lite3VdbeGetValu
1b5eb 65 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69  e(Vdbe *v, int i
1b5ec 56 61 72 2c 20 75 38 20 61 66 66 29 7b 0a 20 20  Var, u8 aff){.  
1b5ed 61 73 73 65 72 74 28 20 69 56 61 72 3e 30 20 29  assert( iVar>0 )
1b5ee 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
1b5ef 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 76 2d   Mem *pMem = &v-
1b5f0 3e 61 56 61 72 5b 69 56 61 72 2d 31 5d 3b 0a 20  >aVar[iVar-1];. 
1b5f1 20 20 20 69 66 28 20 30 3d 3d 28 70 4d 65 6d 2d     if( 0==(pMem-
1b5f2 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1b5f3 6c 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  l) ){.      sqli
1b5f4 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 74 20  te3_value *pRet 
1b5f5 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65  = sqlite3ValueNe
1b5f6 77 28 76 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20  w(v->db);.      
1b5f7 69 66 28 20 70 52 65 74 20 29 7b 0a 20 20 20 20  if( pRet ){.    
1b5f8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1b5f9 65 6d 43 6f 70 79 28 28 4d 65 6d 20 2a 29 70 52  emCopy((Mem *)pR
1b5fa 65 74 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20  et, pMem);.     
1b5fb 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41     sqlite3ValueA
1b5fc 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65  pplyAffinity(pRe
1b5fd 74 2c 20 61 66 66 2c 20 53 51 4c 49 54 45 5f 55  t, aff, SQLITE_U
1b5fe 54 46 38 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TF8);.        sq
1b5ff 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72  lite3VdbeMemStor
1b600 65 54 79 70 65 28 28 4d 65 6d 20 2a 29 70 52 65  eType((Mem *)pRe
1b601 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
1b602 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 20    return pRet;. 
1b603 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1b604 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n 0;.}../*.** Co
1b605 6e 66 69 67 75 72 65 20 53 51 4c 20 76 61 72 69  nfigure SQL vari
1b606 61 62 6c 65 20 69 56 61 72 20 73 6f 20 74 68 61  able iVar so tha
1b607 74 20 62 69 6e 64 69 6e 67 20 61 20 6e 65 77 20  t binding a new 
1b608 76 61 6c 75 65 20 74 6f 20 69 74 20 73 69 67 6e  value to it sign
1b609 61 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  als.** to sqlite
1b60a 33 5f 72 65 6f 70 74 69 6d 69 7a 65 28 29 20 74  3_reoptimize() t
1b60b 68 61 74 20 72 65 2d 70 72 65 70 61 72 69 6e 67  hat re-preparing
1b60c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6d   the statement m
1b60d 61 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20  ay result.** in 
1b60e 61 20 62 65 74 74 65 72 20 71 75 65 72 79 20 70  a better query p
1b60f 6c 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  lan..*/.SQLITE_P
1b610 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1b611 74 65 33 56 64 62 65 53 65 74 56 61 72 6d 61 73  te3VdbeSetVarmas
1b612 6b 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69  k(Vdbe *v, int i
1b613 56 61 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Var){.  assert( 
1b614 69 56 61 72 3e 30 20 29 3b 0a 20 20 69 66 28 20  iVar>0 );.  if( 
1b615 69 56 61 72 3e 33 32 20 29 7b 0a 20 20 20 20 76  iVar>32 ){.    v
1b616 2d 3e 65 78 70 6d 61 73 6b 20 3d 20 30 78 66 66  ->expmask = 0xff
1b617 66 66 66 66 66 66 3b 0a 20 20 7d 65 6c 73 65 7b  ffffff;.  }else{
1b618 0a 20 20 20 20 76 2d 3e 65 78 70 6d 61 73 6b 20  .    v->expmask 
1b619 7c 3d 20 28 28 75 33 32 29 31 20 3c 3c 20 28 69  |= ((u32)1 << (i
1b61a 56 61 72 2d 31 29 29 3b 0a 20 20 7d 0a 7d 0a 0a  Var-1));.  }.}..
1b61b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1b61c 45 6e 64 20 6f 66 20 76 64 62 65 61 75 78 2e 63  End of vdbeaux.c
1b61d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1b61e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b61f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1b620 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1b621 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61  Begin file vdbea
1b622 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pi.c ***********
1b623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b624 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1b625 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
1b626 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
1b627 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1b628 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1b629 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1b62a 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1b62b 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1b62c 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1b62d 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1b62e 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1b62f 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1b630 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1b631 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1b632 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1b633 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1b634 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1b635 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1b636 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1b637 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1b638 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b639 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b63a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b63b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b63c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
1b63d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75   contains code u
1b63e 73 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  se to implement 
1b63f 41 50 49 73 20 74 68 61 74 20 61 72 65 20 70 61  APIs that are pa
1b640 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42  rt of the.** VDB
1b641 45 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  E..*/..#ifndef S
1b642 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
1b643 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75  CATED./*.** Retu
1b644 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72  rn TRUE (non-zer
1b645 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  o) of the statem
1b646 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20  ent supplied as 
1b647 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64  an argument need
1b648 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d  s.** to be recom
1b649 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d  piled.  A statem
1b64a 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ent needs to be 
1b64b 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65  recompiled whene
1b64c 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75  ver the.** execu
1b64d 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74  tion environment
1b64e 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61   changes in a wa
1b64f 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74  y that would alt
1b650 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a  er the program.*
1b651 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70  * that sqlite3_p
1b652 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74  repare() generat
1b653 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  es.  For example
1b654 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f  , if new functio
1b655 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69  ns or.** collati
1b656 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65  ng sequences are
1b657 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69   registered or i
1b658 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20  f an authorizer 
1b659 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61  function is.** a
1b65a 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e  dded or changed.
1b65b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1b65c 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  nt sqlite3_expir
1b65d 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ed(sqlite3_stmt 
1b65e 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
1b65f 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *p = (Vdbe*)pStm
1b660 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  t;.  return p==0
1b661 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a   || p->expired;.
1b662 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1b663 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
1b664 75 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61  utine destroys a
1b665 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1b666 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64   that is created
1b667 20 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74   by.** the sqlit
1b668 65 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75  e3_compile() rou
1b669 74 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65  tine. The intege
1b66a 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e  r returned is an
1b66b 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63   SQLITE_.** succ
1b66c 65 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65  ess/failure code
1b66d 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
1b66e 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78  the result of ex
1b66f 65 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 74  ecuting the virt
1b670 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a  ual.** machine..
1b671 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1b672 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f  ne sets the erro
1b673 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e  r code and strin
1b674 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  g returned by.**
1b675 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
1b676 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  (), sqlite3_errm
1b677 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  sg() and sqlite3
1b678 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a  _errmsg16()..*/.
1b679 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b67a 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1b67b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1b67c 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  tmt){.  int rc;.
1b67d 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29    if( pStmt==0 )
1b67e 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1b67f 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
1b680 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64     Vdbe *v = (Vd
1b681 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73  be*)pStmt;.    s
1b682 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e  qlite3 *db = v->
1b683 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  db;.#if SQLITE_T
1b684 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71  HREADSAFE.    sq
1b685 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
1b686 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65  ex = v->db->mute
1b687 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71  x;.#endif.    sq
1b688 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1b689 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63  r(mutex);.    rc
1b68a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69   = sqlite3VdbeFi
1b68b 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72  nalize(v);.    r
1b68c 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
1b68d 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20  it(db, rc);.    
1b68e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1b68f 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  ave(mutex);.  }.
1b690 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1b691 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 20  /*.** Terminate 
1b692 74 68 65 20 63 75 72 72 65 6e 74 20 65 78 65 63  the current exec
1b693 75 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20  ution of an SQL 
1b694 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
1b695 73 65 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 74  set it.** back t
1b696 6f 20 69 74 73 20 73 74 61 72 74 69 6e 67 20 73  o its starting s
1b697 74 61 74 65 20 73 6f 20 74 68 61 74 20 69 74 20  tate so that it 
1b698 63 61 6e 20 62 65 20 72 65 75 73 65 64 2e 20 41  can be reused. A
1b699 20 73 75 63 63 65 73 73 20 63 6f 64 65 20 66 72   success code fr
1b69a 6f 6d 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 20  om.** the prior 
1b69b 65 78 65 63 75 74 69 6f 6e 20 69 73 20 72 65 74  execution is ret
1b69c 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  urned..**.** Thi
1b69d 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
1b69e 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  he error code an
1b69f 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65  d string returne
1b6a0 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  d by.** sqlite3_
1b6a1 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74  errcode(), sqlit
1b6a2 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20  e3_errmsg() and 
1b6a3 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
1b6a4 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
1b6a5 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65  I int sqlite3_re
1b6a6 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  set(sqlite3_stmt
1b6a7 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20   *pStmt){.  int 
1b6a8 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d  rc;.  if( pStmt=
1b6a9 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
1b6aa 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
1b6ab 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  e{.    Vdbe *v =
1b6ac 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
1b6ad 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1b6ae 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75  _enter(v->db->mu
1b6af 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  tex);.    rc = s
1b6b0 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28  qlite3VdbeReset(
1b6b1 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  v);.    sqlite3V
1b6b2 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20  dbeMakeReady(v, 
1b6b3 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  -1, 0, 0, 0, 0, 
1b6b4 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
1b6b5 28 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72  (rc & (v->db->er
1b6b6 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20  rMask))==rc );. 
1b6b7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
1b6b8 70 69 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63  piExit(v->db, rc
1b6b9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1b6ba 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62  utex_leave(v->db
1b6bb 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
1b6bc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1b6bd 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20  .** Set all the 
1b6be 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68  parameters in th
1b6bf 65 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  e compiled SQL s
1b6c0 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c  tatement to NULL
1b6c1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1b6c2 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61  int sqlite3_clea
1b6c3 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
1b6c4 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1b6c5 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1b6c6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1b6c7 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
1b6c8 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51  e*)pStmt;.#if SQ
1b6c9 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
1b6ca 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
1b6cb 2a 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a  *mutex = ((Vdbe*
1b6cc 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74  )pStmt)->db->mut
1b6cd 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c  ex;.#endif.  sql
1b6ce 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1b6cf 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69  (mutex);.  for(i
1b6d0 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69  =0; i<p->nVar; i
1b6d1 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
1b6d2 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1b6d3 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20  p->aVar[i]);.   
1b6d4 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67   p->aVar[i].flag
1b6d5 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1b6d6 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65  }.  if( p->isPre
1b6d7 70 61 72 65 56 32 20 26 26 20 70 2d 3e 65 78 70  pareV2 && p->exp
1b6d8 6d 61 73 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 65  mask ){.    p->e
1b6d9 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  xpired = 1;.  }.
1b6da 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1b6db 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20  leave(mutex);.  
1b6dc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
1b6dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c  ************ sql
1b6df 69 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a  ite3_value_  ***
1b6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1b6e2 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
1b6e3 75 74 69 6e 65 73 20 65 78 74 72 61 63 74 20 69  utines extract i
1b6e4 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
1b6e5 61 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65 33  a Mem or sqlite3
1b6e6 5f 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63 74  _value.** struct
1b6e7 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ure..*/.SQLITE_A
1b6e8 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1b6e9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
1b6ea 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  b(sqlite3_value 
1b6eb 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70  *pVal){.  Mem *p
1b6ec 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20   = (Mem*)pVal;. 
1b6ed 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20   if( p->flags & 
1b6ee 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74  (MEM_Blob|MEM_St
1b6ef 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  r) ){.    sqlite
1b6f0 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
1b6f1 6f 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c  ob(p);.    p->fl
1b6f2 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b  ags &= ~MEM_Str;
1b6f3 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d  .    p->flags |=
1b6f4 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72   MEM_Blob;.    r
1b6f5 65 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65  eturn p->z;.  }e
1b6f6 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
1b6f7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1b6f8 78 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a  xt(pVal);.  }.}.
1b6f9 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b6fa 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
1b6fb 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  es(sqlite3_value
1b6fc 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72   *pVal){.  retur
1b6fd 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79  n sqlite3ValueBy
1b6fe 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45  tes(pVal, SQLITE
1b6ff 5f 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 54 45  _UTF8);.}.SQLITE
1b700 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1b701 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73  _value_bytes16(s
1b702 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1b703 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
1b704 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
1b705 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
1b706 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c  16NATIVE);.}.SQL
1b707 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73  ITE_API double s
1b708 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
1b709 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ble(sqlite3_valu
1b70a 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
1b70b 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  rn sqlite3VdbeRe
1b70c 61 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56  alValue((Mem*)pV
1b70d 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  al);.}.SQLITE_AP
1b70e 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
1b70f 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f  lue_int(sqlite3_
1b710 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
1b711 72 65 74 75 72 6e 20 28 69 6e 74 29 73 71 6c 69  return (int)sqli
1b712 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
1b713 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53  (Mem*)pVal);.}.S
1b714 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
1b715 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 76  _int64 sqlite3_v
1b716 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 74  alue_int64(sqlit
1b717 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
1b718 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1b719 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d  3VdbeIntValue((M
1b71a 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c  em*)pVal);.}.SQL
1b71b 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e  ITE_API const un
1b71c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c  signed char *sql
1b71d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1b71e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1b71f 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  Val){.  return (
1b720 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1b721 68 61 72 20 2a 29 73 71 6c 69 74 65 33 56 61 6c  har *)sqlite3Val
1b722 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
1b723 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66  ITE_UTF8);.}.#if
1b724 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b725 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
1b726 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1b727 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1b728 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  16(sqlite3_value
1b729 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72  * pVal){.  retur
1b72a 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  n sqlite3ValueTe
1b72b 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  xt(pVal, SQLITE_
1b72c 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a  UTF16NATIVE);.}.
1b72d 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1b72e 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
1b72f 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71  alue_text16be(sq
1b730 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1b731 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
1b732 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56  ite3ValueText(pV
1b733 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  al, SQLITE_UTF16
1b734 42 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  BE);.}.SQLITE_AP
1b735 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1b736 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1b737 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  16le(sqlite3_val
1b738 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue *pVal){.  ret
1b739 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
1b73a 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
1b73b 45 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a 23 65  E_UTF16LE);.}.#e
1b73c 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1b73d 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c  MIT_UTF16 */.SQL
1b73e 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1b73f 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73  te3_value_type(s
1b740 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56  qlite3_value* pV
1b741 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  al){.  return pV
1b742 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a  al->type;.}../**
1b743 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b744 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74  ********** sqlit
1b745 65 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a  e3_result_  ****
1b746 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b747 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1b748 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
1b749 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62  tines are used b
1b74a 79 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66  y user-defined f
1b74b 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70 65 63  unctions to spec
1b74c 69 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74  ify.** the funct
1b74d 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a  ion result..**.*
1b74e 2a 20 54 68 65 20 73 65 74 53 74 72 4f 72 45 72  * The setStrOrEr
1b74f 72 6f 72 28 29 20 66 75 6e 74 69 6f 6e 20 63 61  ror() funtion ca
1b750 6c 6c 73 20 73 71 6c 69 74 65 33 56 64 62 65 4d  lls sqlite3VdbeM
1b751 65 6d 53 65 74 53 74 72 28 29 20 74 6f 20 73 74  emSetStr() to st
1b752 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c  ore the.** resul
1b753 74 20 61 73 20 61 20 73 74 72 69 6e 67 20 6f 72  t as a string or
1b754 20 62 6c 6f 62 20 62 75 74 20 69 66 20 74 68 65   blob but if the
1b755 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
1b756 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 69 74  is too large, it
1b757 0a 2a 2a 20 74 68 65 6e 20 73 65 74 73 20 74 68  .** then sets th
1b758 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20  e error code to 
1b759 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f  SQLITE_TOOBIG.*/
1b75a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
1b75b 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72  ResultStrOrError
1b75c 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
1b75d 65 78 74 20 2a 70 43 74 78 2c 20 20 2f 2a 20 46  ext *pCtx,  /* F
1b75e 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  unction context 
1b75f 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1b760 2a 7a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  *z,          /* 
1b761 53 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a  String pointer *
1b762 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20  /.  int n,      
1b763 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1b764 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67 2c 20  ytes in string, 
1b765 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20  or negative */. 
1b766 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20   u8 enc,        
1b767 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f           /* Enco
1b768 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f  ding of z.  0 fo
1b769 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69  r BLOBs */.  voi
1b76a 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
1b76b 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74       /* Destruct
1b76c 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  or function */.)
1b76d 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  {.  if( sqlite3V
1b76e 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43  dbeMemSetStr(&pC
1b76f 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63  tx->s, z, n, enc
1b770 2c 20 78 44 65 6c 29 3d 3d 53 51 4c 49 54 45 5f  , xDel)==SQLITE_
1b771 54 4f 4f 42 49 47 20 29 7b 0a 20 20 20 20 73 71  TOOBIG ){.    sq
1b772 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1b773 6f 72 5f 74 6f 6f 62 69 67 28 70 43 74 78 29 3b  or_toobig(pCtx);
1b774 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  .  }.}.SQLITE_AP
1b775 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1b776 65 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71  esult_blob(.  sq
1b777 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b778 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f  Ctx, .  const vo
1b779 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  id *z, .  int n,
1b77a 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29   .  void (*xDel)
1b77b 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73  (void *).){.  as
1b77c 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20  sert( n>=0 );.  
1b77d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1b77e 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1b77f 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1b780 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f  .  setResultStrO
1b781 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20  rError(pCtx, z, 
1b782 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53  n, 0, xDel);.}.S
1b783 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b784 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f  qlite3_result_do
1b785 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  uble(sqlite3_con
1b786 74 65 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62  text *pCtx, doub
1b787 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65  le rVal){.  asse
1b788 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b789 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1b78a 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
1b78b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1b78c 44 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c  Double(&pCtx->s,
1b78d 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45   rVal);.}.SQLITE
1b78e 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1b78f 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73  3_result_error(s
1b790 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b791 70 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72  pCtx, const char
1b792 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61   *z, int n){.  a
1b793 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1b794 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1b795 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1b796 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20    pCtx->isError 
1b797 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1b798 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b799 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
1b79a 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
1b79b 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  F8, SQLITE_TRANS
1b79c 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  IENT);.}.#ifndef
1b79d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1b79e 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  16.SQLITE_API vo
1b79f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b7a0 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65  t_error16(sqlite
1b7a1 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
1b7a2 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20   const void *z, 
1b7a3 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74  int n){.  assert
1b7a4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b7a5 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b7a6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74  >mutex) );.  pCt
1b7a7 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c  x->isError = SQL
1b7a8 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c  ITE_ERROR;.  sql
1b7a9 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
1b7aa 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e  r(&pCtx->s, z, n
1b7ab 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
1b7ac 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41  TIVE, SQLITE_TRA
1b7ad 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69  NSIENT);.}.#endi
1b7ae 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  f.SQLITE_API voi
1b7af 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b7b0 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  _int(sqlite3_con
1b7b1 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20  text *pCtx, int 
1b7b2 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28  iVal){.  assert(
1b7b3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b7b4 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b7b5 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
1b7b6 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
1b7b7 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 28 69 36  64(&pCtx->s, (i6
1b7b8 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54  4)iVal);.}.SQLIT
1b7b9 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1b7ba 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
1b7bb 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b7bc 2a 70 43 74 78 2c 20 69 36 34 20 69 56 61 6c 29  *pCtx, i64 iVal)
1b7bd 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1b7be 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b7bf 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b7c0 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
1b7c1 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70  beMemSetInt64(&p
1b7c2 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d  Ctx->s, iVal);.}
1b7c3 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b7c4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b7c5 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e  null(sqlite3_con
1b7c6 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61  text *pCtx){.  a
1b7c7 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1b7c8 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1b7c9 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1b7ca 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b7cb 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73  SetNull(&pCtx->s
1b7cc 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b7cd 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b7ce 75 6c 74 5f 74 65 78 74 28 0a 20 20 73 71 6c 69  ult_text(.  sqli
1b7cf 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1b7d0 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  x, .  const char
1b7d1 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 0a 20   *z, .  int n,. 
1b7d2 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1b7d3 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72  id *).){.  asser
1b7d4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b7d5 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b7d6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65  ->mutex) );.  se
1b7d7 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f  tResultStrOrErro
1b7d8 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51  r(pCtx, z, n, SQ
1b7d9 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29  LITE_UTF8, xDel)
1b7da 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1b7db 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1b7dc 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1b7dd 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1b7de 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  t16(.  sqlite3_c
1b7df 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20  ontext *pCtx, . 
1b7e0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20   const void *z, 
1b7e1 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69  .  int n, .  voi
1b7e2 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a  d (*xDel)(void *
1b7e3 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ).){.  assert( s
1b7e4 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1b7e5 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1b7e6 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73  tex) );.  setRes
1b7e7 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43  ultStrOrError(pC
1b7e8 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  tx, z, n, SQLITE
1b7e9 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44  _UTF16NATIVE, xD
1b7ea 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  el);.}.SQLITE_AP
1b7eb 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1b7ec 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a  esult_text16be(.
1b7ed 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1b7ee 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73  t *pCtx, .  cons
1b7ef 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e  t void *z, .  in
1b7f0 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  t n, .  void (*x
1b7f1 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a  Del)(void *).){.
1b7f2 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b7f3 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1b7f4 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1b7f5 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74  );.  setResultSt
1b7f6 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a  rOrError(pCtx, z
1b7f7 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  , n, SQLITE_UTF1
1b7f8 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51  6BE, xDel);.}.SQ
1b7f9 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1b7fa 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1b7fb 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  t16le(.  sqlite3
1b7fc 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b7fd 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1b7fe 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76  , .  int n, .  v
1b7ff 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1b800 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   *).){.  assert(
1b801 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b802 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b803 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52  mutex) );.  setR
1b804 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28  esultStrOrError(
1b805 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49  pCtx, z, n, SQLI
1b806 54 45 5f 55 54 46 31 36 4c 45 2c 20 78 44 65 6c  TE_UTF16LE, xDel
1b807 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
1b808 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1b809 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76   */.SQLITE_API v
1b80a 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1b80b 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33  lt_value(sqlite3
1b80c 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b80d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1b80e 56 61 6c 75 65 29 7b 0a 20 20 61 73 73 65 72 74  Value){.  assert
1b80f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b810 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b811 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
1b812 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28  ite3VdbeMemCopy(
1b813 26 70 43 74 78 2d 3e 73 2c 20 70 56 61 6c 75 65  &pCtx->s, pValue
1b814 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b815 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b816 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c  ult_zeroblob(sql
1b817 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1b818 74 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73  tx, int n){.  as
1b819 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b81a 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b81b 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b81c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1b81d 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 43 74 78  etZeroBlob(&pCtx
1b81e 2d 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54  ->s, n);.}.SQLIT
1b81f 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1b820 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
1b821 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  code(sqlite3_con
1b822 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20  text *pCtx, int 
1b823 65 72 72 43 6f 64 65 29 7b 0a 20 20 70 43 74 78  errCode){.  pCtx
1b824 2d 3e 69 73 45 72 72 6f 72 20 3d 20 65 72 72 43  ->isError = errC
1b825 6f 64 65 3b 0a 20 20 69 66 28 20 70 43 74 78 2d  ode;.  if( pCtx-
1b826 3e 73 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  >s.flags & MEM_N
1b827 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ull ){.    sqlit
1b828 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
1b829 26 70 43 74 78 2d 3e 73 2c 20 73 71 6c 69 74 65  &pCtx->s, sqlite
1b82a 33 45 72 72 53 74 72 28 65 72 72 43 6f 64 65 29  3ErrStr(errCode)
1b82b 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20  , -1, .         
1b82c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b82d 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
1b82e 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d  ITE_STATIC);.  }
1b82f 0a 7d 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20  .}../* Force an 
1b830 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72  SQLITE_TOOBIG er
1b831 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  ror. */.SQLITE_A
1b832 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1b833 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
1b834 62 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  big(sqlite3_cont
1b835 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73  ext *pCtx){.  as
1b836 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b837 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b838 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b839 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
1b83a 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a   SQLITE_TOOBIG;.
1b83b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b83c 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
1b83d 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62   "string or blob
1b83e 20 74 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a   too big", -1, .
1b83f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b840 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54         SQLITE_UT
1b841 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  F8, SQLITE_STATI
1b842 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c  C);.}../* An SQL
1b843 49 54 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e  ITE_NOMEM error.
1b844 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76   */.SQLITE_API v
1b845 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1b846 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73  lt_error_nomem(s
1b847 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b848 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28  pCtx){.  assert(
1b849 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b84a 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b84b 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
1b84c 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1b84d 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70  l(&pCtx->s);.  p
1b84e 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53  Ctx->isError = S
1b84f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70  QLITE_NOMEM;.  p
1b850 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f  Ctx->s.db->mallo
1b851 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a  cFailed = 1;.}..
1b852 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68  /*.** Execute th
1b853 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d  e statement pStm
1b854 74 2c 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20  t, either until 
1b855 61 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73  a row of data is
1b856 20 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73   ready, the.** s
1b857 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70  tatement is comp
1b858 6c 65 74 65 6c 79 20 65 78 65 63 75 74 65 64 20  letely executed 
1b859 6f 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  or an error occu
1b85a 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  rs..**.** This r
1b85b 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74  outine implement
1b85c 73 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68  s the bulk of th
1b85d 65 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74  e logic behind t
1b85e 68 65 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29  he sqlite_step()
1b85f 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e  .** API.  The on
1b860 6c 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64  ly thing omitted
1b861 20 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69   is the automati
1b862 63 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61  c recompile if a
1b863 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e   .** schema chan
1b864 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e  ge has occurred.
1b865 20 20 54 68 61 74 20 64 65 74 61 69 6c 20 69 73    That detail is
1b866 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a   handled by the.
1b867 2a 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33  ** outer sqlite3
1b868 5f 73 74 65 70 28 29 20 77 72 61 70 70 65 72 20  _step() wrapper 
1b869 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74  procedure..*/.st
1b86a 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
1b86b 53 74 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20  Step(Vdbe *p){. 
1b86c 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
1b86d 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
1b86e 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d  t(p);.  if( p->m
1b86f 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43  agic!=VDBE_MAGIC
1b870 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75  _RUN ){.    retu
1b871 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
1b872 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65  ;.  }..  /* Asse
1b873 72 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29  rt that malloc()
1b874 20 68 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20   has not failed 
1b875 2a 2f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b  */.  db = p->db;
1b876 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
1b877 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
1b878 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1b879 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  EM;.  }..  if( p
1b87a 2d 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78  ->pc<=0 && p->ex
1b87b 70 69 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28  pired ){.    if(
1b87c 20 41 4c 57 41 59 53 28 70 2d 3e 72 63 3d 3d 53   ALWAYS(p->rc==S
1b87d 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72  QLITE_OK || p->r
1b87e 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c==SQLITE_SCHEMA
1b87f 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63  ) ){.      p->rc
1b880 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41   = SQLITE_SCHEMA
1b881 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
1b882 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1b883 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73     goto end_of_s
1b884 74 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  tep;.  }.  if( s
1b885 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1b886 62 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  b) ){.    p->rc 
1b887 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
1b888 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1b889 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
1b88a 20 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a   if( p->pc<0 ){.
1b88b 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
1b88c 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61  are no other sta
1b88d 74 65 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c  tements currentl
1b88e 79 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a  y running, then.
1b88f 20 20 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65      ** reset the
1b890 20 69 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e   interrupt flag.
1b891 20 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20    This prevents 
1b892 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
1b893 33 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20  3_interrupt.    
1b894 2a 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70  ** from interrup
1b895 74 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74  ting a statement
1b896 20 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65   that has not ye
1b897 74 20 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a  t started..    *
1b898 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63  /.    if( db->ac
1b899 74 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29  tiveVdbeCnt==0 )
1b89a 7b 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69  {.      db->u1.i
1b89b 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30  sInterrupted = 0
1b89c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73  ;.    }..    ass
1b89d 65 72 74 28 20 64 62 2d 3e 77 72 69 74 65 56 64  ert( db->writeVd
1b89e 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61  beCnt>0 || db->a
1b89f 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20  utoCommit==0 || 
1b8a0 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e  db->nDeferredCon
1b8a1 73 3d 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66  s==0 );..#ifndef
1b8a2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
1b8a3 43 45 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78  CE.    if( db->x
1b8a4 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e  Profile && !db->
1b8a5 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
1b8a6 20 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a     double rNow;.
1b8a7 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
1b8a8 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70  urrentTime(db->p
1b8a9 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20  Vfs, &rNow);.   
1b8aa 20 20 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20     p->startTime 
1b8ab 3d 20 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20  = (u64)((rNow - 
1b8ac 28 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e  (int)rNow)*3600.
1b8ad 30 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30  0*24.0*100000000
1b8ae 30 2e 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  0.0);.    }.#end
1b8af 69 66 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69  if..    db->acti
1b8b0 76 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20  veVdbeCnt++;.   
1b8b1 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79   if( p->readOnly
1b8b2 3d 3d 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56  ==0 ) db->writeV
1b8b3 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d  dbeCnt++;.    p-
1b8b4 3e 70 63 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66  >pc = 0;.  }.#if
1b8b5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b8b6 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 70  _EXPLAIN.  if( p
1b8b7 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20  ->explain ){.   
1b8b8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1b8b9 65 4c 69 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73  eList(p);.  }els
1b8ba 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
1b8bb 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20  TE_OMIT_EXPLAIN 
1b8bc 2a 2f 0a 20 20 7b 0a 20 20 20 20 72 63 20 3d 20  */.  {.    rc = 
1b8bd 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
1b8be 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73  p);.  }..  if( s
1b8bf 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1b8c0 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  db) ){.    rc = 
1b8c1 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1b8c2 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
1b8c3 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20  TE_OMIT_TRACE.  
1b8c4 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72  /* Invoke the pr
1b8c5 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69  ofile callback i
1b8c6 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 20  f there is one. 
1b8c7 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51   */.  if( rc!=SQ
1b8c8 4c 49 54 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e  LITE_ROW && db->
1b8c9 78 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d  xProfile && !db-
1b8ca 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70 2d  >init.busy && p-
1b8cb 3e 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 75  >zSql ){.    dou
1b8cc 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 75 36  ble rNow;.    u6
1b8cd 34 20 65 6c 61 70 73 65 54 69 6d 65 3b 0a 0a 20  4 elapseTime;.. 
1b8ce 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72     sqlite3OsCurr
1b8cf 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73  entTime(db->pVfs
1b8d0 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 65 6c  , &rNow);.    el
1b8d1 61 70 73 65 54 69 6d 65 20 3d 20 28 75 36 34 29  apseTime = (u64)
1b8d2 28 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e  ((rNow - (int)rN
1b8d3 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a  ow)*3600.0*24.0*
1b8d4 31 30 30 30 30 30 30 30 30 30 2e 30 29 3b 0a 20  1000000000.0);. 
1b8d5 20 20 20 65 6c 61 70 73 65 54 69 6d 65 20 2d 3d     elapseTime -=
1b8d6 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 3b 0a 20   p->startTime;. 
1b8d7 20 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 28     db->xProfile(
1b8d8 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 2c  db->pProfileArg,
1b8d9 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c 61 70 73 65   p->zSql, elapse
1b8da 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  Time);.  }.#endi
1b8db 66 0a 0a 20 20 64 62 2d 3e 65 72 72 43 6f 64 65  f..  db->errCode
1b8dc 20 3d 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c   = rc;.  if( SQL
1b8dd 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74  ITE_NOMEM==sqlit
1b8de 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c  e3ApiExit(p->db,
1b8df 20 70 2d 3e 72 63 29 20 29 7b 0a 20 20 20 20 70   p->rc) ){.    p
1b8e0 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
1b8e1 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64 5f 6f 66 5f  MEM;.  }.end_of_
1b8e2 73 74 65 70 3a 0a 20 20 2f 2a 20 41 74 20 74 68  step:.  /* At th
1b8e3 69 73 20 70 6f 69 6e 74 20 6c 6f 63 61 6c 20 76  is point local v
1b8e4 61 72 69 61 62 6c 65 20 72 63 20 68 6f 6c 64 73  ariable rc holds
1b8e5 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20   the value that 
1b8e6 73 68 6f 75 6c 64 20 62 65 20 0a 20 20 2a 2a 20  should be .  ** 
1b8e7 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 69 73  returned if this
1b8e8 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 63   statement was c
1b8e9 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 74 68  ompiled using th
1b8ea 65 20 6c 65 67 61 63 79 20 0a 20 20 2a 2a 20 73  e legacy .  ** s
1b8eb 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
1b8ec 20 69 6e 74 65 72 66 61 63 65 2e 20 41 63 63 6f   interface. Acco
1b8ed 72 64 69 6e 67 20 74 6f 20 74 68 65 20 64 6f 63  rding to the doc
1b8ee 73 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79  s, this can only
1b8ef 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20  .  ** be one of 
1b8f0 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 74 68  the values in th
1b8f1 65 20 66 69 72 73 74 20 61 73 73 65 72 74 28 29  e first assert()
1b8f2 20 62 65 6c 6f 77 2e 20 56 61 72 69 61 62 6c 65   below. Variable
1b8f3 20 70 2d 3e 72 63 20 0a 20 20 2a 2a 20 63 6f 6e   p->rc .  ** con
1b8f4 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 20  tains the value 
1b8f5 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 72 65  that would be re
1b8f6 74 75 72 6e 65 64 20 69 66 20 73 71 6c 69 74 65  turned if sqlite
1b8f7 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 0a 20 20  3_finalize() .  
1b8f8 2a 2a 20 77 65 72 65 20 63 61 6c 6c 65 64 20 6f  ** were called o
1b8f9 6e 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 20  n statement p.. 
1b8fa 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63   */.  assert( rc
1b8fb 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 20 7c 7c  ==SQLITE_ROW  ||
1b8fc 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
1b8fd 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45     || rc==SQLITE
1b8fe 5f 45 52 52 4f 52 20 0a 20 20 20 20 20 20 20 7c  _ERROR .       |
1b8ff 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  | rc==SQLITE_BUS
1b900 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  Y || rc==SQLITE_
1b901 4d 49 53 55 53 45 0a 20 20 29 3b 0a 20 20 61 73  MISUSE.  );.  as
1b902 73 65 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c  sert( p->rc!=SQL
1b903 49 54 45 5f 52 4f 57 20 26 26 20 70 2d 3e 72 63  ITE_ROW && p->rc
1b904 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  !=SQLITE_DONE );
1b905 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70  .  if( p->isPrep
1b906 61 72 65 56 32 20 26 26 20 72 63 21 3d 53 51 4c  areV2 && rc!=SQL
1b907 49 54 45 5f 52 4f 57 20 26 26 20 72 63 21 3d 53  ITE_ROW && rc!=S
1b908 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
1b909 20 20 2f 2a 20 49 66 20 74 68 69 73 20 73 74 61    /* If this sta
1b90a 74 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61  tement was prepa
1b90b 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  red using sqlite
1b90c 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 2c 20  3_prepare_v2(), 
1b90d 61 6e 64 20 61 6e 0a 20 20 20 20 2a 2a 20 65 72  and an.    ** er
1b90e 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 2c  ror has occured,
1b90f 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65   then return the
1b910 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 70   error code in p
1b911 2d 3e 72 63 20 74 6f 20 74 68 65 0a 20 20 20 20  ->rc to the.    
1b912 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 65 74 20 74  ** caller. Set t
1b913 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e  he error code in
1b914 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
1b915 6e 64 6c 65 20 74 6f 20 74 68 65 20 73 61 6d 65  ndle to the same
1b916 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 20 0a   value..    */ .
1b917 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 65 72 72      rc = db->err
1b918 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20  Code = p->rc;.  
1b919 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 26 64  }.  return (rc&d
1b91a 62 2d 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d 0a 0a  b->errMask);.}..
1b91b 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
1b91c 65 20 74 6f 70 2d 6c 65 76 65 6c 20 69 6d 70 6c  e top-level impl
1b91d 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
1b91e 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 43  lite3_step().  C
1b91f 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 53 74  all.** sqlite3St
1b920 65 70 28 29 20 74 6f 20 64 6f 20 6d 6f 73 74 20  ep() to do most 
1b921 6f 66 20 74 68 65 20 77 6f 72 6b 2e 20 20 49 66  of the work.  If
1b922 20 61 20 73 63 68 65 6d 61 20 65 72 72 6f 72 20   a schema error 
1b923 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20  occurs,.** call 
1b924 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65  sqlite3Reprepare
1b925 28 29 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e  () and try again
1b926 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1b927 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70  int sqlite3_step
1b928 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1b929 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20  Stmt){.  int rc 
1b92a 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
1b92b 0a 20 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a  .  if( pStmt ){.
1b92c 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b      int cnt = 0;
1b92d 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28  .    Vdbe *v = (
1b92e 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20  Vdbe*)pStmt;.   
1b92f 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76   sqlite3 *db = v
1b930 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65  ->db;.    sqlite
1b931 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
1b932 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 77 68  ->mutex);.    wh
1b933 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74  ile( (rc = sqlit
1b934 65 33 53 74 65 70 28 76 29 29 3d 3d 53 51 4c 49  e3Step(v))==SQLI
1b935 54 45 5f 53 43 48 45 4d 41 0a 20 20 20 20 20 20  TE_SCHEMA.      
1b936 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20       && cnt++ < 
1b937 35 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  5.           && 
1b938 28 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 70  (rc = sqlite3Rep
1b939 72 65 70 61 72 65 28 76 29 29 3d 3d 53 51 4c 49  repare(v))==SQLI
1b93a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
1b93b 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
1b93c 6d 74 29 3b 0a 20 20 20 20 20 20 76 2d 3e 65 78  mt);.      v->ex
1b93d 70 69 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d  pired = 0;.    }
1b93e 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1b93f 49 54 45 5f 53 43 48 45 4d 41 20 26 26 20 41 4c  ITE_SCHEMA && AL
1b940 57 41 59 53 28 76 2d 3e 69 73 50 72 65 70 61 72  WAYS(v->isPrepar
1b941 65 56 32 29 20 26 26 20 41 4c 57 41 59 53 28 64  eV2) && ALWAYS(d
1b942 62 2d 3e 70 45 72 72 29 20 29 7b 0a 20 20 20 20  b->pErr) ){.    
1b943 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f    /* This case o
1b944 63 63 75 72 73 20 61 66 74 65 72 20 66 61 69 6c  ccurs after fail
1b945 69 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65  ing to recompile
1b946 20 61 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e   an sql statemen
1b947 74 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65  t. .      ** The
1b948 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66   error message f
1b949 72 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70  rom the SQL comp
1b94a 69 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79  iler has already
1b94b 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20   been loaded .  
1b94c 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20      ** into the 
1b94d 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  database handle.
1b94e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69   This block copi
1b94f 65 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  es the error mes
1b950 73 61 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66  sage .      ** f
1b951 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
1b952 20 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65   handle into the
1b953 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73   statement and s
1b954 65 74 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ets the statemen
1b955 74 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72  t.      ** progr
1b956 61 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20  am counter to 0 
1b957 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 77  to ensure that w
1b958 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e  hen the statemen
1b959 74 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66  t is .      ** f
1b95a 69 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65  inalized or rese
1b95b 74 20 74 68 65 20 70 61 72 73 65 72 20 65 72 72  t the parser err
1b95c 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 61 76  or message is av
1b95d 61 69 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20  ailable via.    
1b95e 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72    ** sqlite3_err
1b95f 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  msg() and sqlite
1b960 33 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20  3_errcode()..   
1b961 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73     */.      cons
1b962 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28  t char *zErr = (
1b963 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
1b964 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1b965 64 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20  db->pErr); .    
1b966 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1b967 64 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, v->zErrMsg);
1b968 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e  .      if( !db->
1b969 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1b96a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d          v->zErrM
1b96b 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  sg = sqlite3DbSt
1b96c 72 44 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a  rDup(db, zErr);.
1b96d 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20        } else {. 
1b96e 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73         v->zErrMs
1b96f 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 76  g = 0;.        v
1b970 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
1b971 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
1b972 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
1b973 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
1b974 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  c);.    sqlite3_
1b975 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
1b976 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65  mutex);.  }.  re
1b977 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1b978 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73  * Extract the us
1b979 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73  er data from a s
1b97a 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73  qlite3_context s
1b97b 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74  tructure and ret
1b97c 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72  urn a.** pointer
1b97d 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54   to it..*/.SQLIT
1b97e 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
1b97f 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71  te3_user_data(sq
1b980 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b981 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  ){.  assert( p &
1b982 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20  & p->pFunc );.  
1b983 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d  return p->pFunc-
1b984 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f  >pUserData;.}../
1b985 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
1b986 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20   user data from 
1b987 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a sqlite3_contex
1b988 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  t structure and 
1b989 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e  return a.** poin
1b98a 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51  ter to it..*/.SQ
1b98b 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
1b98c 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   *sqlite3_contex
1b98d 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69  t_db_handle(sqli
1b98e 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b  te3_context *p){
1b98f 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20  .  assert( p && 
1b990 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65  p->pFunc );.  re
1b991 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a  turn p->s.db;.}.
1b992 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
1b993 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c  wing is the impl
1b994 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e  ementation of an
1b995 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68   SQL function th
1b996 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69  at always.** fai
1b997 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ls with an error
1b998 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67   message stating
1b999 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
1b99a 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68  on is used in th
1b99b 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65  e.** wrong conte
1b99c 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  xt.  The sqlite3
1b99d 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
1b99e 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63  on() API might c
1b99f 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20  onstruct.** SQL 
1b9a0 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73  function that us
1b9a1 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  e this routine s
1b9a2 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74  o that the funct
1b9a3 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a  ions will exist.
1b9a4 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f  ** for name reso
1b9a5 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61  lution but are a
1b9a6 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64  ctually overload
1b9a7 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46  ed by the xFindF
1b9a8 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f  unction.** metho
1b9a9 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62  d of virtual tab
1b9aa 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  les..*/.SQLITE_P
1b9ab 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1b9ac 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69  te3InvalidFuncti
1b9ad 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  on(.  sqlite3_co
1b9ae 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
1b9af 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e   /* The function
1b9b0 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74   calling context
1b9b1 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65   */.  int NotUse
1b9b2 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
1b9b3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
1b9b4 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
1b9b5 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  unction */.  sql
1b9b6 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
1b9b7 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65  Used2   /* Value
1b9b8 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e   of each argumen
1b9b9 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  t */.){.  const 
1b9ba 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f  char *zName = co
1b9bb 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e  ntext->pFunc->zN
1b9bc 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72  ame;.  char *zEr
1b9bd 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  r;.  UNUSED_PARA
1b9be 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
1b9bf 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72  NotUsed2);.  zEr
1b9c0 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  r = sqlite3_mpri
1b9c1 6e 74 66 28 0a 20 20 20 20 20 20 22 75 6e 61 62  ntf(.      "unab
1b9c2 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69  le to use functi
1b9c3 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71  on %s in the req
1b9c4 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c  uested context",
1b9c5 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74   zName);.  sqlit
1b9c6 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
1b9c7 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
1b9c8 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  1);.  sqlite3_fr
1b9c9 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a  ee(zErr);.}../*.
1b9ca 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72  ** Allocate or r
1b9cb 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67  eturn the aggreg
1b9cc 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ate context for 
1b9cd 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e  a user function.
1b9ce 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65    A new.** conte
1b9cf 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  xt is allocated 
1b9d0 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  on the first cal
1b9d1 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63  l.  Subsequent c
1b9d2 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a  alls return the.
1b9d3 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20  ** same context 
1b9d4 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65  that was returne
1b9d5 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73  d on prior calls
1b9d6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1b9d7 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67  void *sqlite3_ag
1b9d8 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
1b9d9 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b9da 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a  *p, int nByte){.
1b9db 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61    Mem *pMem;.  a
1b9dc 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70  ssert( p && p->p
1b9dd 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63  Func && p->pFunc
1b9de 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73  ->xStep );.  ass
1b9df 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b9e0 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d  ex_held(p->s.db-
1b9e1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65  >mutex) );.  pMe
1b9e2 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69  m = p->pMem;.  i
1b9e3 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  f( (pMem->flags 
1b9e4 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b  & MEM_Agg)==0 ){
1b9e5 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d  .    if( nByte==
1b9e6 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1b9e7 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1b9e8 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a  External(pMem);.
1b9e9 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1b9ea 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1b9eb 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b      pMem->z = 0;
1b9ec 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1b9ed 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b9ee 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65  Grow(pMem, nByte
1b9ef 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d  , 0);.      pMem
1b9f0 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67  ->flags = MEM_Ag
1b9f1 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  g;.      pMem->u
1b9f2 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63  .pDef = p->pFunc
1b9f3 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d  ;.      if( pMem
1b9f4 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  ->z ){.        m
1b9f5 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30  emset(pMem->z, 0
1b9f6 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
1b9f7 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
1b9f8 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d  turn (void*)pMem
1b9f9 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ->z;.}../*.** Re
1b9fa 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72  turn the auxilar
1b9fb 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20  y data pointer, 
1b9fc 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20  if any, for the 
1b9fd 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74  iArg'th argument
1b9fe 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d   to.** the user-
1b9ff 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64  function defined
1ba00 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c   by pCtx..*/.SQL
1ba01 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
1ba02 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
1ba03 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a(sqlite3_contex
1ba04 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72  t *pCtx, int iAr
1ba05 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a  g){.  VdbeFunc *
1ba06 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73  pVdbeFunc;..  as
1ba07 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1ba08 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1ba09 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1ba0a 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74   pVdbeFunc = pCt
1ba0b 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20  x->pVdbeFunc;.  
1ba0c 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c  if( !pVdbeFunc |
1ba0d 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e  | iArg>=pVdbeFun
1ba0e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c  c->nAux || iArg<
1ba0f 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1ba10 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
1ba11 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78  pVdbeFunc->apAux
1ba12 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a  [iArg].pAux;.}..
1ba13 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75  /*.** Set the au
1ba14 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e  xilary data poin
1ba15 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66  ter and delete f
1ba16 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65  unction, for the
1ba17 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75   iArg'th.** argu
1ba18 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72  ment to the user
1ba19 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65  -function define
1ba1a 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70  d by pCtx. Any p
1ba1b 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73  revious value is
1ba1c 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63  .** deleted by c
1ba1d 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74  alling the delet
1ba1e 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69  e function speci
1ba1f 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 73  fied when it was
1ba20 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   set..*/.SQLITE_
1ba21 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1ba22 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20  _set_auxdata(.  
1ba23 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1ba24 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41  *pCtx, .  int iA
1ba25 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75  rg, .  void *pAu
1ba26 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  x, .  void (*xDe
1ba27 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  lete)(void*).){.
1ba28 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61    struct AuxData
1ba29 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64   *pAuxData;.  Vd
1ba2a 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e  beFunc *pVdbeFun
1ba2b 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20  c;.  if( iArg<0 
1ba2c 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a  ) goto failed;..
1ba2d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1ba2e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1ba2f 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1ba30 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d  );.  pVdbeFunc =
1ba31 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63   pCtx->pVdbeFunc
1ba32 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75  ;.  if( !pVdbeFu
1ba33 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d  nc || pVdbeFunc-
1ba34 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20  >nAux<=iArg ){. 
1ba35 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70     int nAux = (p
1ba36 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65  VdbeFunc ? pVdbe
1ba37 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b  Func->nAux : 0);
1ba38 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63  .    int nMalloc
1ba39 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75   = sizeof(VdbeFu
1ba3a 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72  nc) + sizeof(str
1ba3b 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 72  uct AuxData)*iAr
1ba3c 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63  g;.    pVdbeFunc
1ba3d 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
1ba3e 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20  loc(pCtx->s.db, 
1ba3f 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c  pVdbeFunc, nMall
1ba40 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56  oc);.    if( !pV
1ba41 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20  dbeFunc ){.     
1ba42 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20   goto failed;.  
1ba43 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56    }.    pCtx->pV
1ba44 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46  dbeFunc = pVdbeF
1ba45 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  unc;.    memset(
1ba46 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75  &pVdbeFunc->apAu
1ba47 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65  x[nAux], 0, size
1ba48 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74  of(struct AuxDat
1ba49 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29  a)*(iArg+1-nAux)
1ba4a 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63  );.    pVdbeFunc
1ba4b 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b  ->nAux = iArg+1;
1ba4c 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e  .    pVdbeFunc->
1ba4d 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46  pFunc = pCtx->pF
1ba4e 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78  unc;.  }..  pAux
1ba4f 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e  Data = &pVdbeFun
1ba50 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a  c->apAux[iArg];.
1ba51 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e    if( pAuxData->
1ba52 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74 61  pAux && pAuxData
1ba53 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20  ->xDelete ){.   
1ba54 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65   pAuxData->xDele
1ba55 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75  te(pAuxData->pAu
1ba56 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61  x);.  }.  pAuxDa
1ba57 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b  ta->pAux = pAux;
1ba58 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65  .  pAuxData->xDe
1ba59 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a  lete = xDelete;.
1ba5a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65    return;..faile
1ba5b 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65  d:.  if( xDelete
1ba5c 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28   ){.    xDelete(
1ba5d 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  pAux);.  }.}..#i
1ba5e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1ba5f 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a  T_DEPRECATED./*.
1ba60 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1ba61 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
1ba62 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20  e Step function 
1ba63 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68  of a aggregate h
1ba64 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c  as been .** call
1ba65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ed..**.** This f
1ba66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65  unction is depre
1ba67 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75  cated.  Do not u
1ba68 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f  se it for new co
1ba69 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72  de.  It is.** pr
1ba6a 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76  ovide only to av
1ba6b 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67  oid breaking leg
1ba6c 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61  acy code.  New a
1ba6d 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
1ba6e 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  n.** implementat
1ba6f 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70  ions should keep
1ba70 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74   their own count
1ba71 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 61  s within their a
1ba72 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74  ggregate.** cont
1ba73 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ext..*/.SQLITE_A
1ba74 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61  PI int sqlite3_a
1ba75 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73  ggregate_count(s
1ba76 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1ba77 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
1ba78 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d  && p->pMem && p-
1ba79 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75  >pFunc && p->pFu
1ba7a 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72  nc->xStep );.  r
1ba7b 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e  eturn p->pMem->n
1ba7c 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1ba7d 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1ba7e 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
1ba7f 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
1ba80 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65   for the stateme
1ba81 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c  nt pStmt..*/.SQL
1ba82 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1ba83 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
1ba84 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1ba85 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  Stmt){.  Vdbe *p
1ba86 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  Vm = (Vdbe *)pSt
1ba87 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d  mt;.  return pVm
1ba88 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75   ? pVm->nResColu
1ba89 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  mn : 0;.}../*.**
1ba8a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1ba8b 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61  er of values ava
1ba8c 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20  ilable from the 
1ba8d 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74  current row of t
1ba8e 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  he.** currently 
1ba8f 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d  executing statem
1ba90 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51  ent pStmt..*/.SQ
1ba91 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1ba92 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28  ite3_data_count(
1ba93 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1ba94 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56  tmt){.  Vdbe *pV
1ba95 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  m = (Vdbe *)pStm
1ba96 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20  t;.  if( pVm==0 
1ba97 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53  || pVm->pResultS
1ba98 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  et==0 ) return 0
1ba99 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e  ;.  return pVm->
1ba9a 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a  nResColumn;.}...
1ba9b 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
1ba9c 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f  ee if column iCo
1ba9d 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73  l of the given s
1ba9e 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69  tatement is vali
1ba9f 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c  d.  If.** it is,
1baa0 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
1baa1 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72  r to the Mem for
1baa2 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1baa3 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66  at column..** If
1baa4 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c   iCol is not val
1baa5 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  id, return a poi
1baa6 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68  nter to a Mem wh
1baa7 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a  ich has a value.
1baa8 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73  ** of NULL..*/.s
1baa9 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d  tatic Mem *colum
1baaa 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d  nMem(sqlite3_stm
1baab 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1baac 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20  {.  Vdbe *pVm;. 
1baad 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d   int vals;.  Mem
1baae 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d   *pOut;..  pVm =
1baaf 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a   (Vdbe *)pStmt;.
1bab0 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d    if( pVm && pVm
1bab1 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20  ->pResultSet!=0 
1bab2 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f  && i<pVm->nResCo
1bab3 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a  lumn && i>=0 ){.
1bab4 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1bab5 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d  x_enter(pVm->db-
1bab6 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c  >mutex);.    val
1bab7 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61  s = sqlite3_data
1bab8 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20  _count(pStmt);. 
1bab9 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e     pOut = &pVm->
1baba 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20  pResultSet[i];. 
1babb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49   }else{.    /* I
1babc 66 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73  f the value pass
1babd 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
1babe 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 75 74   argument is out
1babf 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72   of range, retur
1bac0 6e 0a 20 20 20 20 2a 2a 20 61 20 70 6f 69 6e 74  n.    ** a point
1bac1 65 72 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  er to the follow
1bac2 69 6e 67 20 73 74 61 74 69 63 20 4d 65 6d 20 6f  ing static Mem o
1bac3 62 6a 65 63 74 20 77 68 69 63 68 20 63 6f 6e 74  bject which cont
1bac4 61 69 6e 73 20 74 68 65 0a 20 20 20 20 2a 2a 20  ains the.    ** 
1bac5 76 61 6c 75 65 20 53 51 4c 20 4e 55 4c 4c 2e 20  value SQL NULL. 
1bac6 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20  Even though the 
1bac7 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 63 6f  Mem structure co
1bac8 6e 74 61 69 6e 73 20 61 6e 20 65 6c 65 6d 65 6e  ntains an elemen
1bac9 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 79 70 65  t.    ** of type
1baca 20 69 36 34 2c 20 6f 6e 20 63 65 72 74 61 69 6e   i64, on certain
1bacb 20 61 72 63 68 69 74 65 63 74 75 72 65 20 28 78   architecture (x
1bacc 38 36 29 20 77 69 74 68 20 63 65 72 74 61 69 6e  86) with certain
1bacd 20 63 6f 6d 70 69 6c 65 72 0a 20 20 20 20 2a 2a   compiler.    **
1bace 20 73 77 69 74 63 68 65 73 20 28 2d 4f 73 29 2c   switches (-Os),
1bacf 20 67 63 63 20 6d 61 79 20 61 6c 69 67 6e 20 74   gcc may align t
1bad0 68 69 73 20 4d 65 6d 20 6f 62 6a 65 63 74 20 6f  his Mem object o
1bad1 6e 20 61 20 34 2d 62 79 74 65 20 62 6f 75 6e 64  n a 4-byte bound
1bad2 61 72 79 0a 20 20 20 20 2a 2a 20 69 6e 73 74 65  ary.    ** inste
1bad3 61 64 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20  ad of an 8-byte 
1bad4 6f 6e 65 2e 20 54 68 69 73 20 61 6c 6c 20 77 6f  one. This all wo
1bad5 72 6b 73 20 66 69 6e 65 2c 20 65 78 63 65 70 74  rks fine, except
1bad6 20 74 68 61 74 20 77 68 65 6e 0a 20 20 20 20 2a   that when.    *
1bad7 2a 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 53  * running with S
1bad8 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65 66 69  QLITE_DEBUG defi
1bad9 6e 65 64 20 74 68 65 20 53 51 4c 69 74 65 20 63  ned the SQLite c
1bada 6f 64 65 20 73 6f 6d 65 74 69 6d 65 73 20 61 73  ode sometimes as
1badb 73 65 72 74 28 29 73 0a 20 20 20 20 2a 2a 20 74  sert()s.    ** t
1badc 68 61 74 20 61 20 4d 65 6d 20 73 74 72 75 63 74  hat a Mem struct
1badd 75 72 65 20 69 73 20 6c 6f 63 61 74 65 64 20 6f  ure is located o
1bade 6e 20 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e  n an 8-byte boun
1badf 64 61 72 79 2e 20 54 6f 20 70 72 65 76 65 6e 74  dary. To prevent
1bae0 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 61 73 73  .    ** this ass
1bae1 65 72 74 28 29 20 66 72 6f 6d 20 66 61 69 6c 69  ert() from faili
1bae2 6e 67 2c 20 77 68 65 6e 20 62 75 69 6c 64 69 6e  ng, when buildin
1bae3 67 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45  g with SQLITE_DE
1bae4 42 55 47 20 64 65 66 69 6e 65 64 0a 20 20 20 20  BUG defined.    
1bae5 2a 2a 20 75 73 69 6e 67 20 67 63 63 2c 20 66 6f  ** using gcc, fo
1bae6 72 63 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f 20 62  rce nullMem to b
1bae7 65 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64  e 8-byte aligned
1bae8 20 75 73 69 6e 67 20 74 68 65 20 6d 61 67 69 63   using the magic
1bae9 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 74 72  al.    ** __attr
1baea 69 62 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64  ibute__((aligned
1baeb 28 38 29 29 29 20 6d 61 63 72 6f 2e 20 20 2a 2f  (8))) macro.  */
1baec 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
1baed 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 0a 23  t Mem nullMem .#
1baee 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
1baef 45 5f 44 45 42 55 47 29 20 26 26 20 64 65 66 69  E_DEBUG) && defi
1baf0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 20 20  ned(__GNUC__).  
1baf1 20 20 20 20 5f 5f 61 74 74 72 69 62 75 74 65 5f      __attribute_
1baf2 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29 20  _((aligned(8))) 
1baf3 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3d 20  .#endif.      = 
1baf4 7b 7b 30 7d 2c 20 28 64 6f 75 62 6c 65 29 30 2c  {{0}, (double)0,
1baf5 20 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 5f 4e   0, "", 0, MEM_N
1baf6 75 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  ull, SQLITE_NULL
1baf7 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 20 20  , 0, 0, 0 };..  
1baf8 20 20 69 66 28 20 70 56 6d 20 26 26 20 41 4c 57    if( pVm && ALW
1baf9 41 59 53 28 70 56 6d 2d 3e 64 62 29 20 29 7b 0a  AYS(pVm->db) ){.
1bafa 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
1bafb 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64  tex_enter(pVm->d
1bafc 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  b->mutex);.     
1bafd 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56   sqlite3Error(pV
1bafe 6d 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41  m->db, SQLITE_RA
1baff 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  NGE, 0);.    }. 
1bb00 20 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29     pOut = (Mem*)
1bb01 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20  &nullMem;.  }.  
1bb02 72 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a  return pOut;.}..
1bb03 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1bb04 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66  ion is called af
1bb05 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20  ter invoking an 
1bb06 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58  sqlite3_value_XX
1bb07 58 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20  X function on a 
1bb08 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  .** column value
1bb09 20 28 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72   (i.e. a value r
1bb0a 65 74 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75  eturned by evalu
1bb0b 61 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70  ating an SQL exp
1bb0c 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a  ression in the.*
1bb0d 2a 20 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66  * select list of
1bb0e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
1bb0f 65 6e 74 29 20 74 68 61 74 20 6d 61 79 20 63 61  ent) that may ca
1bb10 75 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66  use a malloc() f
1bb11 61 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d  ailure. If .** m
1bb12 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c  alloc() has fail
1bb13 65 64 2c 20 74 68 65 20 74 68 72 65 61 64 73 20  ed, the threads 
1bb14 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
1bb15 67 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64  g is cleared and
1bb16 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63   the result.** c
1bb17 6f 64 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74  ode of statement
1bb18 20 70 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51   pStmt set to SQ
1bb19 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a  LITE_NOMEM..**.*
1bb1a 2a 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20  * Specifically, 
1bb1b 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66  this is called f
1bb1c 72 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a  rom within:.**.*
1bb1d 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
1bb1e 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20  lumn_int().**   
1bb1f 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1bb20 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20  _int64().**     
1bb21 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
1bb22 65 78 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  ext().**     sql
1bb23 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
1bb24 31 36 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  16().**     sqli
1bb25 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28  te3_column_real(
1bb26 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
1bb27 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a  _column_bytes().
1bb28 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
1bb29 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a  olumn_bytes16().
1bb2a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f  **.** But not fo
1bb2b 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  r sqlite3_column
1bb2c 5f 62 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e  _blob(), which n
1bb2d 65 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f  ever calls mallo
1bb2e 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  c()..*/.static v
1bb2f 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  oid columnMalloc
1bb30 46 61 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f  Failure(sqlite3_
1bb31 73 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20  stmt *pStmt).{. 
1bb32 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20   /* If malloc() 
1bb33 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e  failed during an
1bb34 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72   encoding conver
1bb35 73 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20  sion within an. 
1bb36 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   ** sqlite3_colu
1bb37 6d 6e 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e  mn_XXX API, then
1bb38 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20   set the return 
1bb39 63 6f 64 65 20 6f 66 20 74 68 65 20 73 74 61 74  code of the stat
1bb3a 65 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51  ement to.  ** SQ
1bb3b 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20  LITE_NOMEM. The 
1bb3c 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74  next call to _st
1bb3d 65 70 28 29 20 28 69 66 20 61 6e 79 29 20 77 69  ep() (if any) wi
1bb3e 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ll return SQLITE
1bb3f 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20  _ERROR.  ** and 
1bb40 5f 66 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c  _finalize() will
1bb41 20 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20   return NOMEM.. 
1bb42 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20   */.  Vdbe *p = 
1bb43 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
1bb44 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d   if( p ){.    p-
1bb45 3e 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69  >rc = sqlite3Api
1bb46 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72  Exit(p->db, p->r
1bb47 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  c);.    sqlite3_
1bb48 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
1bb49 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d  b->mutex);.  }.}
1bb4a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1bb4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
1bb4c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20  sqlite3_column_ 
1bb4d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1bb4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bb4f 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1bb50 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  g routines are u
1bb51 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c  sed to access el
1bb52 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75  ements of the cu
1bb53 72 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20  rrent row.** in 
1bb54 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a  the result set..
1bb55 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1bb56 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1bb57 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71  3_column_blob(sq
1bb58 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bb59 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e  t, int i){.  con
1bb5a 73 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20  st void *val;.  
1bb5b 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
1bb5c 6c 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e  lue_blob( column
1bb5d 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a  Mem(pStmt,i) );.
1bb5e 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68    /* Even though
1bb5f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63   there is no enc
1bb60 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e  oding conversion
1bb61 2c 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d  , value_blob() m
1bb62 69 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74  ight.  ** need t
1bb63 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20  o call malloc() 
1bb64 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 72 65  to expand the re
1bb65 73 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c  sult of a zerobl
1bb66 6f 62 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65  ob() .  ** expre
1bb67 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63  ssion. .  */.  c
1bb68 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1bb69 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1bb6a 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
1bb6b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bb6c 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73  3_column_bytes(s
1bb6d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bb6e 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e  mt, int i){.  in
1bb6f 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f  t val = sqlite3_
1bb70 76 61 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c  value_bytes( col
1bb71 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
1bb72 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
1bb73 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
1bb74 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
1bb75 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1bb76 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
1bb77 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73  ytes16(sqlite3_s
1bb78 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bb79 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20  i){.  int val = 
1bb7a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
1bb7b 74 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d  tes16( columnMem
1bb7c 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
1bb7d 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1bb7e 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1bb7f 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
1bb80 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c  E_API double sql
1bb81 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62  ite3_column_doub
1bb82 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  le(sqlite3_stmt 
1bb83 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1bb84 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73    double val = s
1bb85 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
1bb86 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  ble( columnMem(p
1bb87 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c  Stmt,i) );.  col
1bb88 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1bb89 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1bb8a 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1bb8b 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bb8c 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74  column_int(sqlit
1bb8d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bb8e 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61  int i){.  int va
1bb8f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
1bb90 65 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d  e_int( columnMem
1bb91 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
1bb92 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1bb93 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1bb94 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
1bb95 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74  E_API sqlite_int
1bb96 36 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  64 sqlite3_colum
1bb97 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f  n_int64(sqlite3_
1bb98 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bb99 20 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e   i){.  sqlite_in
1bb9a 74 36 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  t64 val = sqlite
1bb9b 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63  3_value_int64( c
1bb9c 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
1bb9d 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
1bb9e 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
1bb9f 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
1bba0 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  .}.SQLITE_API co
1bba1 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1bba2 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r *sqlite3_colum
1bba3 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73  n_text(sqlite3_s
1bba4 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bba5 69 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  i){.  const unsi
1bba6 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d  gned char *val =
1bba7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1bba8 65 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  ext( columnMem(p
1bba9 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c  Stmt,i) );.  col
1bbaa 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1bbab 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1bbac 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1bbad 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  API sqlite3_valu
1bbae 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e *sqlite3_colum
1bbaf 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  n_value(sqlite3_
1bbb0 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bbb1 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74   i){.  Mem *pOut
1bbb2 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74   = columnMem(pSt
1bbb3 6d 74 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f  mt, i);.  if( pO
1bbb4 75 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74  ut->flags&MEM_St
1bbb5 61 74 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74  atic ){.    pOut
1bbb6 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f  ->flags &= ~MEM_
1bbb7 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74  Static;.    pOut
1bbb8 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45  ->flags |= MEM_E
1bbb9 70 68 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75  phem;.  }.  colu
1bbba 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
1bbbb 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
1bbbc 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20   (sqlite3_value 
1bbbd 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65  *)pOut;.}.#ifnde
1bbbe 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
1bbbf 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  F16.SQLITE_API c
1bbc0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1bbc1 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
1bbc2 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bbc3 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
1bbc4 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20  const void *val 
1bbc5 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1bbc6 74 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65  text16( columnMe
1bbc7 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20  m(pStmt,i) );.  
1bbc8 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c  columnMallocFail
1bbc9 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65  ure(pStmt);.  re
1bbca 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64  turn val;.}.#end
1bbcb 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1bbcc 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54  T_UTF16 */.SQLIT
1bbcd 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bbce 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71  3_column_type(sq
1bbcf 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bbd0 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74  t, int i){.  int
1bbd1 20 69 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33   iType = sqlite3
1bbd2 5f 76 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c  _value_type( col
1bbd3 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
1bbd4 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
1bbd5 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
1bbd6 0a 20 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b  .  return iType;
1bbd7 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  .}../* The follo
1bbd8 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73  wing function is
1bbd9 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e   experimental an
1bbda 64 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  d subject to cha
1bbdb 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61  nge or.** remova
1bbdc 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74  l */./*int sqlit
1bbdd 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69  e3_column_numeri
1bbde 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73  c_type(sqlite3_s
1bbdf 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bbe0 69 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73  i){.**  return s
1bbe1 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d  qlite3_value_num
1bbe2 65 72 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d  eric_type( colum
1bbe3 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1bbe4 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  .**}.*/../*.** C
1bbe5 6f 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20  onvert the N-th 
1bbe6 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74  element of pStmt
1bbe7 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74  ->pColName[] int
1bbe8 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67  o a string using
1bbe9 0a 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e  .** xFunc() then
1bbea 20 72 65 74 75 72 6e 20 74 68 61 74 20 73 74 72   return that str
1bbeb 69 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75  ing.  If N is ou
1bbec 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75  t of range, retu
1bbed 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  rn 0..**.** Ther
1bbee 65 20 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61  e are up to 5 na
1bbef 6d 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  mes for each col
1bbf0 75 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65  umn.  useType de
1bbf1 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a  termines which.*
1bbf2 2a 20 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e  * name is return
1bbf3 65 64 2e 20 20 48 65 72 65 20 61 72 65 20 74 68  ed.  Here are th
1bbf4 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  e names:.**.**  
1bbf5 20 20 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c    0      The col
1bbf6 75 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73  umn name as it s
1bbf7 68 6f 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79  hould be display
1bbf8 65 64 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a  ed for output.**
1bbf9 20 20 20 20 31 20 20 20 20 20 20 54 68 65 20 64      1      The d
1bbfa 61 74 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72  atatype name for
1bbfb 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20   the column.**  
1bbfc 20 20 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d    2      The nam
1bbfd 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1bbfe 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d  e that the colum
1bbff 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a  n derives from.*
1bc00 2a 20 20 20 20 33 20 20 20 20 20 20 54 68 65 20  *    3      The 
1bc01 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
1bc02 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d  e that the colum
1bc03 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a  n derives from.*
1bc04 2a 20 20 20 20 34 20 20 20 20 20 20 54 68 65 20  *    4      The 
1bc05 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
1bc06 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68  e column that th
1bc07 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20  e result column 
1bc08 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a  derives from.**.
1bc09 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74  ** If the result
1bc0a 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65   is not a simple
1bc0b 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63   column referenc
1bc0c 65 20 28 69 66 20 69 74 20 69 73 20 61 6e 20 65  e (if it is an e
1bc0d 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20  xpression.** or 
1bc0e 61 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e  a constant) then
1bc0f 20 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20   useTypes 2, 3, 
1bc10 61 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c  and 4 return NUL
1bc11 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  L..*/.static con
1bc12 73 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e  st void *columnN
1bc13 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73  ame(.  sqlite3_s
1bc14 74 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e  tmt *pStmt,.  in
1bc15 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69  t N,.  const voi
1bc16 64 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a  d *(*xFunc)(Mem*
1bc17 29 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65  ),.  int useType
1bc18 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  .){.  const void
1bc19 20 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62   *ret = 0;.  Vdb
1bc1a 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
1bc1b 53 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20  Stmt;.  int n;. 
1bc1c 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1bc1d 2d 3e 64 62 3b 0a 20 20 0a 20 20 61 73 73 65 72  ->db;.  .  asser
1bc1e 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 6e 20  t( db!=0 );.  n 
1bc1f 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
1bc20 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20  _count(pStmt);. 
1bc21 20 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30   if( N<n && N>=0
1bc22 20 29 7b 0a 20 20 20 20 4e 20 2b 3d 20 75 73 65   ){.    N += use
1bc23 54 79 70 65 2a 6e 3b 0a 20 20 20 20 73 71 6c 69  Type*n;.    sqli
1bc24 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1bc25 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
1bc26 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c  assert( db->mall
1bc27 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20  ocFailed==0 );. 
1bc28 20 20 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26     ret = xFunc(&
1bc29 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b  p->aColName[N]);
1bc2a 0a 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f  .     /* A mallo
1bc2b 63 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65  c may have faile
1bc2c 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20  d inside of the 
1bc2d 78 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66  xFunc() call. If
1bc2e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20   this.    ** is 
1bc2f 74 68 65 20 63 61 73 65 2c 20 63 6c 65 61 72 20  the case, clear 
1bc30 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  the mallocFailed
1bc31 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e   flag and return
1bc32 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20   NULL..    */.  
1bc33 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
1bc34 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
1bc35 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1bc36 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 20   = 0;.      ret 
1bc37 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 0;.    }.    s
1bc38 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1bc39 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
1bc3a 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b   }.  return ret;
1bc3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1bc3c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1bc3d 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   Nth column of t
1bc3e 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 72 65  he result set re
1bc3f 74 75 72 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a  turned by SQL.**
1bc40 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74   statement pStmt
1bc41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1bc42 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
1bc43 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28  te3_column_name(
1bc44 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1bc45 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
1bc46 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
1bc47 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
1bc48 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
1bc49 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
1bc4a 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e  value_text, COLN
1bc4b 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66  AME_NAME);.}.#if
1bc4c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1bc4d 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
1bc4e 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1bc4f 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
1bc50 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
1bc51 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
1bc52 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
1bc53 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
1bc54 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
1bc55 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
1bc56 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1bc57 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b  , COLNAME_NAME);
1bc58 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1bc59 20 43 6f 6e 73 74 72 61 69 6e 74 3a 20 20 49 66   Constraint:  If
1bc5a 20 79 6f 75 20 68 61 76 65 20 45 4e 41 42 4c 45   you have ENABLE
1bc5b 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
1bc5c 20 74 68 65 6e 20 79 6f 75 20 6d 75 73 74 0a 2a   then you must.*
1bc5d 2a 20 6e 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49  * not define OMI
1bc5e 54 5f 44 45 43 4c 54 59 50 45 2e 0a 2a 2f 0a 23  T_DECLTYPE..*/.#
1bc5f 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
1bc60 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 29  E_OMIT_DECLTYPE)
1bc61 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
1bc62 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
1bc63 5f 4d 45 54 41 44 41 54 41 29 0a 23 20 65 72 72  _METADATA).# err
1bc64 6f 72 20 22 4d 75 73 74 20 6e 6f 74 20 64 65 66  or "Must not def
1bc65 69 6e 65 20 62 6f 74 68 20 53 51 4c 49 54 45 5f  ine both SQLITE_
1bc66 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 5c 0a  OMIT_DECLTYPE \.
1bc67 20 20 20 20 20 20 20 20 20 61 6e 64 20 53 51 4c           and SQL
1bc68 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
1bc69 4e 5f 4d 45 54 41 44 41 54 41 22 0a 23 65 6e 64  N_METADATA".#end
1bc6a 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
1bc6b 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45  TE_OMIT_DECLTYPE
1bc6c 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1bc6d 65 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61  e column declara
1bc6e 74 69 6f 6e 20 74 79 70 65 20 28 69 66 20 61 70  tion type (if ap
1bc6f 70 6c 69 63 61 62 6c 65 29 20 6f 66 20 74 68 65  plicable) of the
1bc70 20 27 69 27 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a   'i'th column.**
1bc71 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
1bc72 65 74 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d  et of SQL statem
1bc73 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51  ent pStmt..*/.SQ
1bc74 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
1bc75 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
1bc76 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c  umn_decltype(sql
1bc77 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bc78 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1bc79 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1bc7a 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1bc7b 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1bc7c 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1bc7d 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
1bc7e 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 69  _DECLTYPE);.}.#i
1bc7f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bc80 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41  T_UTF16.SQLITE_A
1bc81 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1bc82 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
1bc83 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 33  cltype16(sqlite3
1bc84 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1bc85 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  t N){.  return c
1bc86 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20  olumnName(.     
1bc87 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73   pStmt, N, (cons
1bc88 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29  t void*(*)(Mem*)
1bc89 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1bc8a 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44  ext16, COLNAME_D
1bc8b 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 65 6e 64  ECLTYPE);.}.#end
1bc8c 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1bc8d 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69  T_UTF16 */.#endi
1bc8e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1bc8f 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a 0a 23 69  _DECLTYPE */..#i
1bc90 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
1bc91 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
1bc92 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  TA./*.** Return 
1bc93 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1bc94 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20 77 68  database from wh
1bc95 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c  ich a result col
1bc96 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20  umn derives..** 
1bc97 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
1bc98 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63   if the result c
1bc99 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72  olumn is an expr
1bc9a 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61  ession or consta
1bc9b 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e  nt or.** anythin
1bc9c 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20  g else which is 
1bc9d 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75  not an unabiguou
1bc9e 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61  s reference to a
1bc9f 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e   database column
1bca0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1bca1 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
1bca2 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
1bca3 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  ase_name(sqlite3
1bca4 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1bca5 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  t N){.  return c
1bca6 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20  olumnName(.     
1bca7 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73   pStmt, N, (cons
1bca8 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29  t void*(*)(Mem*)
1bca9 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1bcaa 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54  ext, COLNAME_DAT
1bcab 41 42 41 53 45 29 3b 0a 7d 0a 23 69 66 6e 64 65  ABASE);.}.#ifnde
1bcac 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
1bcad 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  F16.SQLITE_API c
1bcae 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1bcaf 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
1bcb0 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  se_name16(sqlite
1bcb1 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1bcb2 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
1bcb3 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
1bcb4 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
1bcb5 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
1bcb6 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
1bcb7 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f  text16, COLNAME_
1bcb8 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e  DATABASE);.}.#en
1bcb9 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1bcba 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a  IT_UTF16 */../*.
1bcbb 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61  ** Return the na
1bcbc 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1bcbd 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73  from which a res
1bcbe 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76  ult column deriv
1bcbf 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72  es..** NULL is r
1bcc0 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72  eturned if the r
1bcc1 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20  esult column is 
1bcc2 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  an expression or
1bcc3 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20   constant or.** 
1bcc4 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68  anything else wh
1bcc5 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e  ich is not an un
1bcc6 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e  abiguous referen
1bcc7 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65  ce to a database
1bcc8 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49   column..*/.SQLI
1bcc9 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
1bcca 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r *sqlite3_colum
1bccb 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c  n_table_name(sql
1bccc 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bccd 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1bcce 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1bccf 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1bcd0 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1bcd1 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1bcd2 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
1bcd3 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 69 66 6e 64  _TABLE);.}.#ifnd
1bcd4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
1bcd5 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
1bcd6 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
1bcd7 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
1bcd8 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
1bcd9 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bcda 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bcdb 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bcdc 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bcdd 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bcde 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bcdf 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41  xt16, COLNAME_TA
1bce0 42 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  BLE);.}.#endif /
1bce1 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
1bce2 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  F16 */../*.** Re
1bce3 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
1bce4 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d   the table colum
1bce5 6e 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72  n from which a r
1bce6 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72  esult column der
1bce7 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73  ives..** NULL is
1bce8 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
1bce9 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69   result column i
1bcea 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s an expression 
1bceb 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a  or constant or.*
1bcec 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20  * anything else 
1bced 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20  which is not an 
1bcee 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 72  unabiguous refer
1bcef 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 61  ence to a databa
1bcf0 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51  se column..*/.SQ
1bcf1 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
1bcf2 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
1bcf3 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28  umn_origin_name(
1bcf4 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1bcf5 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
1bcf6 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
1bcf7 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
1bcf8 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
1bcf9 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
1bcfa 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e  value_text, COLN
1bcfb 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23  AME_COLUMN);.}.#
1bcfc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1bcfd 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1bcfe 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1bcff 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
1bd00 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c  rigin_name16(sql
1bd01 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bd02 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1bd03 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1bd04 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1bd05 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1bd06 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1bd07 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41  ue_text16, COLNA
1bd08 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 65  ME_COLUMN);.}.#e
1bd09 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1bd0a 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e  MIT_UTF16 */.#en
1bd0b 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
1bd0c 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
1bd0d 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  DATA */.../*****
1bd0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74  ********** sqlit
1bd10 65 33 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a  e3_bind_  ******
1bd11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd12 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 6f 75  *****.** .** Rou
1bd13 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 61 74  tines used to at
1bd14 74 61 63 68 20 76 61 6c 75 65 73 20 74 6f 20 77  tach values to w
1bd15 69 6c 64 63 61 72 64 73 20 69 6e 20 61 20 63 6f  ildcards in a co
1bd16 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65  mpiled SQL state
1bd17 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55  ment..*/./*.** U
1bd18 6e 62 69 6e 64 20 74 68 65 20 76 61 6c 75 65 20  nbind the value 
1bd19 62 6f 75 6e 64 20 74 6f 20 76 61 72 69 61 62 6c  bound to variabl
1bd1a 65 20 69 20 69 6e 20 76 69 72 74 75 61 6c 20 6d  e i in virtual m
1bd1b 61 63 68 69 6e 65 20 70 2e 20 54 68 69 73 20 69  achine p. This i
1bd1c 73 20 74 68 65 20 0a 2a 2a 20 74 68 65 20 73 61  s the .** the sa
1bd1d 6d 65 20 61 73 20 62 69 6e 64 69 6e 67 20 61 20  me as binding a 
1bd1e 4e 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20 74 68  NULL value to th
1bd1f 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65  e column. If the
1bd20 20 22 69 22 20 70 61 72 61 6d 65 74 65 72 20 69   "i" parameter i
1bd21 73 0a 2a 2a 20 6f 75 74 20 6f 66 20 72 61 6e 67  s.** out of rang
1bd22 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 52  e, then SQLITE_R
1bd23 41 4e 47 45 20 69 73 20 72 65 74 75 72 6e 65 64  ANGE is returned
1bd24 2e 20 4f 74 68 65 77 69 73 65 20 53 51 4c 49 54  . Othewise SQLIT
1bd25 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 75  E_OK..**.** A su
1bd26 63 63 65 73 73 66 75 6c 20 65 76 61 6c 75 61 74  ccessful evaluat
1bd27 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74  ion of this rout
1bd28 69 6e 65 20 61 63 71 75 69 72 65 73 20 74 68 65  ine acquires the
1bd29 20 6d 75 74 65 78 20 6f 6e 20 70 2e 0a 2a 2a 20   mutex on p..** 
1bd2a 74 68 65 20 6d 75 74 65 78 20 69 73 20 72 65 6c  the mutex is rel
1bd2b 65 61 73 65 64 20 69 66 20 61 6e 79 20 6b 69 6e  eased if any kin
1bd2c 64 20 6f 66 20 65 72 72 6f 72 20 6f 63 63 75 72  d of error occur
1bd2d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72  s..**.** The err
1bd2e 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69  or code stored i
1bd2f 6e 20 64 61 74 61 62 61 73 65 20 70 2d 3e 64 62  n database p->db
1bd30 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20   is overwritten 
1bd31 77 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 0a  with the return.
1bd32 2a 2a 20 76 61 6c 75 65 20 69 6e 20 61 6e 79 20  ** value in any 
1bd33 63 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  case..*/.static 
1bd34 69 6e 74 20 76 64 62 65 55 6e 62 69 6e 64 28 56  int vdbeUnbind(V
1bd35 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a  dbe *p, int i){.
1bd36 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69    Mem *pVar;.  i
1bd37 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
1bd38 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
1bd39 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1bd3a 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74  enter(p->db->mut
1bd3b 65 78 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61  ex);.  if( p->ma
1bd3c 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic!=VDBE_MAGIC_
1bd3d 52 55 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20  RUN || p->pc>=0 
1bd3e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1bd3f 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54  ror(p->db, SQLIT
1bd40 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20  E_MISUSE, 0);.  
1bd41 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1bd42 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
1bd43 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ex);.    return 
1bd44 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1bd45 20 7d 0a 20 20 69 66 28 20 69 3c 31 20 7c 7c 20   }.  if( i<1 || 
1bd46 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20  i>p->nVar ){.   
1bd47 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d   sqlite3Error(p-
1bd48 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47  >db, SQLITE_RANG
1bd49 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  E, 0);.    sqlit
1bd4a 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1bd4b 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1bd4c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1bd4d 52 41 4e 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d  RANGE;.  }.  i--
1bd4e 3b 0a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61  ;.  pVar = &p->a
1bd4f 56 61 72 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65  Var[i];.  sqlite
1bd50 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1bd51 70 56 61 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66  pVar);.  pVar->f
1bd52 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1bd53 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  .  sqlite3Error(
1bd54 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  p->db, SQLITE_OK
1bd55 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  , 0);..  /* If t
1bd56 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e  he bit correspon
1bd57 64 69 6e 67 20 74 6f 20 74 68 69 73 20 76 61 72  ding to this var
1bd58 69 61 62 6c 65 20 69 6e 20 56 64 62 65 2e 65 78  iable in Vdbe.ex
1bd59 70 6d 61 73 6b 20 69 73 20 73 65 74 2c 20 74 68  pmask is set, th
1bd5a 65 6e 20 0a 20 20 2a 2a 20 62 69 6e 64 69 6e 67  en .  ** binding
1bd5b 20 61 20 6e 65 77 20 76 61 6c 75 65 20 74 6f 20   a new value to 
1bd5c 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e  this variable in
1bd5d 76 61 6c 69 64 61 74 65 73 20 74 68 65 20 63 75  validates the cu
1bd5e 72 72 65 6e 74 20 71 75 65 72 79 20 70 6c 61 6e  rrent query plan
1bd5f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
1bd60 69 73 50 72 65 70 61 72 65 56 32 20 26 26 0a 20  isPrepareV2 &&. 
1bd61 20 20 20 20 28 28 69 3c 33 32 20 26 26 20 70 2d      ((i<32 && p-
1bd62 3e 65 78 70 6d 61 73 6b 20 26 20 28 28 75 33 32  >expmask & ((u32
1bd63 29 31 20 3c 3c 20 69 29 29 20 7c 7c 20 70 2d 3e  )1 << i)) || p->
1bd64 65 78 70 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66  expmask==0xfffff
1bd65 66 66 66 29 0a 20 20 29 7b 0a 20 20 20 20 70 2d  fff).  ){.    p-
1bd66 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20  >expired = 1;.  
1bd67 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
1bd68 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42  E_OK;.}../*.** B
1bd69 69 6e 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c  ind a text or BL
1bd6a 4f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61  OB value..*/.sta
1bd6b 74 69 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74  tic int bindText
1bd6c 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  (.  sqlite3_stmt
1bd6d 20 2a 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68   *pStmt,   /* Th
1bd6e 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  e statement to b
1bd6f 69 6e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20  ind against */. 
1bd70 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20   int i,         
1bd71 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
1bd72 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   of the paramete
1bd73 72 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63  r to bind */.  c
1bd74 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61  onst void *zData
1bd75 2c 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ,     /* Pointer
1bd76 20 74 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20   to the data to 
1bd77 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e  be bound */.  in
1bd78 74 20 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20  t nData,        
1bd79 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1bd7a 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  f bytes of data 
1bd7b 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20  to be bound */. 
1bd7c 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1bd7d 69 64 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72  id*),   /* Destr
1bd7e 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61  uctor for the da
1bd7f 74 61 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64  ta */.  u8 encod
1bd80 69 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 2f  ing            /
1bd81 2a 20 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74  * Encoding for t
1bd82 68 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20  he data */.){.  
1bd83 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
1bd84 2a 29 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a  *)pStmt;.  Mem *
1bd85 70 56 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  pVar;.  int rc;.
1bd86 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69  .  rc = vdbeUnbi
1bd87 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20  nd(p, i);.  if( 
1bd88 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1bd89 0a 20 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d  .    if( zData!=
1bd8a 30 20 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20  0 ){.      pVar 
1bd8b 3d 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b  = &p->aVar[i-1];
1bd8c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1bd8d 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
1bd8e 28 70 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44  (pVar, zData, nD
1bd8f 61 74 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78  ata, encoding, x
1bd90 44 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Del);.      if( 
1bd91 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1bd92 20 65 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a   encoding!=0 ){.
1bd93 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1bd94 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
1bd95 63 6f 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43  coding(pVar, ENC
1bd96 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20  (p->db));.      
1bd97 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  }.      sqlite3E
1bd98 72 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20  rror(p->db, rc, 
1bd99 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  0);.      rc = s
1bd9a 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d  qlite3ApiExit(p-
1bd9b 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a  >db, rc);.    }.
1bd9c 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1bd9d 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
1bd9e 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
1bd9f 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
1bda0 2a 20 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61  * Bind a blob va
1bda1 6c 75 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74  lue to an SQL st
1bda2 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65  atement variable
1bda3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1bda4 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1bda5 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33  _blob(.  sqlite3
1bda6 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20  _stmt *pStmt, . 
1bda7 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74   int i, .  const
1bda8 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20   void *zData, . 
1bda9 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76   int nData, .  v
1bdaa 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1bdab 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62  *).){.  return b
1bdac 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69  indText(pStmt, i
1bdad 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , zData, nData, 
1bdae 78 44 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49  xDel, 0);.}.SQLI
1bdaf 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1bdb0 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73  e3_bind_double(s
1bdb1 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bdb2 6d 74 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c  mt, int i, doubl
1bdb3 65 20 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74  e rValue){.  int
1bdb4 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d   rc;.  Vdbe *p =
1bdb5 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a   (Vdbe *)pStmt;.
1bdb6 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e    rc = vdbeUnbin
1bdb7 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72  d(p, i);.  if( r
1bdb8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1bdb9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1bdba 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e  emSetDouble(&p->
1bdbb 61 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75  aVar[i-1], rValu
1bdbc 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  e);.    sqlite3_
1bdbd 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
1bdbe 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20  b->mutex);.  }. 
1bdbf 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51   return rc;.}.SQ
1bdc0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1bdc1 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71  ite3_bind_int(sq
1bdc2 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69  lite3_stmt *p, i
1bdc3 6e 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65  nt i, int iValue
1bdc4 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1bdc5 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
1bdc6 2c 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65  , i, (i64)iValue
1bdc7 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1bdc8 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1bdc9 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73  _int64(sqlite3_s
1bdca 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bdcb 69 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  i, sqlite_int64 
1bdcc 69 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72  iValue){.  int r
1bdcd 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  c;.  Vdbe *p = (
1bdce 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
1bdcf 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28  rc = vdbeUnbind(
1bdd0 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d  p, i);.  if( rc=
1bdd1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1bdd2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1bdd3 53 65 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61  SetInt64(&p->aVa
1bdd4 72 5b 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b  r[i-1], iValue);
1bdd5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1bdd6 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e  ex_leave(p->db->
1bdd7 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65  mutex);.  }.  re
1bdd8 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
1bdd9 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bdda 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69  3_bind_null(sqli
1bddb 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bddc 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72   int i){.  int r
1bddd 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  c;.  Vdbe *p = (
1bdde 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72  Vdbe*)pStmt;.  r
1bddf 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70  c = vdbeUnbind(p
1bde0 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , i);.  if( rc==
1bde1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1bde2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1bde3 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
1bde4 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  x);.  }.  return
1bde5 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   rc;.}.SQLITE_AP
1bde6 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1bde7 6e 64 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69  nd_text( .  sqli
1bde8 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bde9 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f   .  int i, .  co
1bdea 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c  nst char *zData,
1bdeb 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a   .  int nData, .
1bdec 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
1bded 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72  oid*).){.  retur
1bdee 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74  n bindText(pStmt
1bdef 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74  , i, zData, nDat
1bdf0 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f  a, xDel, SQLITE_
1bdf1 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  UTF8);.}.#ifndef
1bdf2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1bdf3 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  16.SQLITE_API in
1bdf4 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  t sqlite3_bind_t
1bdf5 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ext16(.  sqlite3
1bdf6 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20  _stmt *pStmt, . 
1bdf7 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74   int i, .  const
1bdf8 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20   void *zData, . 
1bdf9 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76   int nData, .  v
1bdfa 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1bdfb 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62  *).){.  return b
1bdfc 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69  indText(pStmt, i
1bdfd 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , zData, nData, 
1bdfe 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  xDel, SQLITE_UTF
1bdff 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e  16NATIVE);.}.#en
1be00 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1be01 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49  IT_UTF16 */.SQLI
1be02 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1be03 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71  e3_bind_value(sq
1be04 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1be05 74 2c 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20  t, int i, const 
1be06 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1be07 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63  Value){.  int rc
1be08 3b 0a 20 20 73 77 69 74 63 68 28 20 70 56 61 6c  ;.  switch( pVal
1be09 75 65 2d 3e 74 79 70 65 20 29 7b 0a 20 20 20 20  ue->type ){.    
1be0a 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
1be0b 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  GER: {.      rc 
1be0c 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
1be0d 6e 74 36 34 28 70 53 74 6d 74 2c 20 69 2c 20 70  nt64(pStmt, i, p
1be0e 56 61 6c 75 65 2d 3e 75 2e 69 29 3b 0a 20 20 20  Value->u.i);.   
1be0f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1be10 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
1be11 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72  FLOAT: {.      r
1be12 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
1be13 5f 64 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 69  _double(pStmt, i
1be14 2c 20 70 56 61 6c 75 65 2d 3e 72 29 3b 0a 20 20  , pValue->r);.  
1be15 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1be16 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
1be17 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69  _BLOB: {.      i
1be18 66 28 20 70 56 61 6c 75 65 2d 3e 66 6c 61 67 73  f( pValue->flags
1be19 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20   & MEM_Zero ){. 
1be1a 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1be1b 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  te3_bind_zeroblo
1be1c 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c  b(pStmt, i, pVal
1be1d 75 65 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20  ue->u.nZero);.  
1be1e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1be1f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
1be20 62 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c  bind_blob(pStmt,
1be21 20 69 2c 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70   i, pValue->z, p
1be22 56 61 6c 75 65 2d 3e 6e 2c 53 51 4c 49 54 45 5f  Value->n,SQLITE_
1be23 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
1be24 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1be25 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1be26 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20  SQLITE_TEXT: {. 
1be27 20 20 20 20 20 72 63 20 3d 20 62 69 6e 64 54 65       rc = bindTe
1be28 78 74 28 70 53 74 6d 74 2c 69 2c 20 20 70 56 61  xt(pStmt,i,  pVa
1be29 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e  lue->z, pValue->
1be2a 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  n, SQLITE_TRANSI
1be2b 45 4e 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ENT,.           
1be2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1be2d 20 20 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b     pValue->enc);
1be2e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1be2f 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
1be30 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   {.      rc = sq
1be31 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28  lite3_bind_null(
1be32 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20  pStmt, i);.     
1be33 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1be34 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1be35 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1be36 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72  sqlite3_bind_zer
1be37 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74  oblob(sqlite3_st
1be38 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1be39 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20  , int n){.  int 
1be3a 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  rc;.  Vdbe *p = 
1be3b 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
1be3c 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64   rc = vdbeUnbind
1be3d 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63  (p, i);.  if( rc
1be3e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1be3f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1be40 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d  mSetZeroBlob(&p-
1be41 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a  >aVar[i-1], n);.
1be42 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1be43 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
1be44 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
1be45 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1be46 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1be47 65 72 20 6f 66 20 77 69 6c 64 63 61 72 64 73 20  er of wildcards 
1be48 74 68 61 74 20 63 61 6e 20 62 65 20 70 6f 74 65  that can be pote
1be49 6e 74 69 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f  ntially bound to
1be4a 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1be4b 65 20 69 73 20 61 64 64 65 64 20 74 6f 20 73 75  e is added to su
1be4c 70 70 6f 72 74 20 44 42 44 3a 3a 53 51 4c 69 74  pport DBD::SQLit
1be4d 65 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  e.  .*/.SQLITE_A
1be4e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1be4f 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
1be50 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  unt(sqlite3_stmt
1be51 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65   *pStmt){.  Vdbe
1be52 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74   *p = (Vdbe*)pSt
1be53 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f  mt;.  return p ?
1be54 20 70 2d 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a   p->nVar : 0;.}.
1be55 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
1be56 6d 61 70 70 69 6e 67 20 66 72 6f 6d 20 76 61 72  mapping from var
1be57 69 61 62 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f  iable numbers to
1be58 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a   variable names.
1be59 2a 2a 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61  ** in the Vdbe.a
1be5a 7a 56 61 72 5b 5d 20 61 72 72 61 79 2c 20 69 66  zVar[] array, if
1be5b 20 73 75 63 68 20 61 20 6d 61 70 70 69 6e 67 20   such a mapping 
1be5c 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
1be5d 0a 2a 2a 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74  .** exist..*/.st
1be5e 61 74 69 63 20 76 6f 69 64 20 63 72 65 61 74 65  atic void create
1be5f 56 61 72 4d 61 70 28 56 64 62 65 20 2a 70 29 7b  VarMap(Vdbe *p){
1be60 0a 20 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72  .  if( !p->okVar
1be61 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20   ){.    int j;. 
1be62 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20     Op *pOp;.    
1be63 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1be64 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ter(p->db->mutex
1be65 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 61  );.    /* The ra
1be66 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72  ce condition her
1be67 65 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20  e is harmless.  
1be68 49 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 63  If two threads c
1be69 61 6c 6c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20  all this.    ** 
1be6a 72 6f 75 74 69 6e 65 20 6f 6e 20 74 68 65 20 73  routine on the s
1be6b 61 6d 65 20 56 64 62 65 20 61 74 20 74 68 65 20  ame Vdbe at the 
1be6c 73 61 6d 65 20 74 69 6d 65 2c 20 74 68 65 79 20  same time, they 
1be6d 62 6f 74 68 20 6d 69 67 68 74 20 65 6e 64 0a 20  both might end. 
1be6e 20 20 20 2a 2a 20 75 70 20 69 6e 69 74 69 61 6c     ** up initial
1be6f 69 7a 69 6e 67 20 74 68 65 20 56 64 62 65 2e 61  izing the Vdbe.a
1be70 7a 56 61 72 5b 5d 20 61 72 72 61 79 2e 20 20 54  zVar[] array.  T
1be71 68 61 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20  hat is a little 
1be72 65 78 74 72 61 0a 20 20 20 20 2a 2a 20 77 6f 72  extra.    ** wor
1be73 6b 20 62 75 74 20 69 74 20 72 65 73 75 6c 74 73  k but it results
1be74 20 69 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73   in the same ans
1be75 77 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  wer..    */.    
1be76 66 6f 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e  for(j=0, pOp=p->
1be77 61 4f 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a  aOp; j<p->nOp; j
1be78 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20  ++, pOp++){.    
1be79 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
1be7a 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29  e==OP_Variable )
1be7b 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1be7c 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70  ( pOp->p1>0 && p
1be7d 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20  Op->p1<=p->nVar 
1be7e 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 7a  );.        p->az
1be7f 56 61 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d  Var[pOp->p1-1] =
1be80 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20   pOp->p4.z;.    
1be81 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d    }.    }.    p-
1be82 3e 6f 6b 56 61 72 20 3d 20 31 3b 0a 20 20 20 20  >okVar = 1;.    
1be83 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1be84 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
1be85 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1be86 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  Return the name 
1be87 6f 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61  of a wildcard pa
1be88 72 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e  rameter.  Return
1be89 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64   NULL if the ind
1be8a 65 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20  ex.** is out of 
1be8b 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20  range or if the 
1be8c 77 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61  wildcard is unna
1be8d 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  med..**.** The r
1be8e 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20  esult is always 
1be8f 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45  UTF-8..*/.SQLITE
1be90 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
1be91 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  *sqlite3_bind_pa
1be92 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c  rameter_name(sql
1be93 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1be94 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65  , int i){.  Vdbe
1be95 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74   *p = (Vdbe*)pSt
1be96 6d 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c  mt;.  if( p==0 |
1be97 7c 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56  | i<1 || i>p->nV
1be98 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ar ){.    return
1be99 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65   0;.  }.  create
1be9a 56 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74  VarMap(p);.  ret
1be9b 75 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31  urn p->azVar[i-1
1be9c 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  ];.}../*.** Give
1be9d 6e 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72  n a wildcard par
1be9e 61 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74  ameter name, ret
1be9f 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66  urn the index of
1bea0 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a   the variable.**
1bea1 20 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e   with that name.
1bea2 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
1bea3 20 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 74   variable with t
1bea4 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a  he given name,.*
1bea5 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53  * return 0..*/.S
1bea6 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bea7 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
1bea8 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74  eter_index(sqlit
1bea9 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1beaa 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1beab 65 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  e){.  Vdbe *p = 
1beac 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
1bead 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d  int i;.  if( p==
1beae 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1beaf 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56  0;.  }.  createV
1beb0 61 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28  arMap(p); .  if(
1beb1 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f   zName ){.    fo
1beb2 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72  r(i=0; i<p->nVar
1beb3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f  ; i++){.      co
1beb4 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d  nst char *z = p-
1beb5 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20  >azVar[i];.     
1beb6 20 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70   if( z && strcmp
1beb7 28 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  (z,zName)==0 ){.
1beb8 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69          return i
1beb9 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  +1;.      }.    
1beba 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
1bebb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  ;.}../*.** Trans
1bebc 66 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73  fer all bindings
1bebd 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20   from the first 
1bebe 73 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74  statement over t
1bebf 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f  o the second..*/
1bec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1bec1 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73  int sqlite3Trans
1bec2 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69  ferBindings(sqli
1bec3 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53  te3_stmt *pFromS
1bec4 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  tmt, sqlite3_stm
1bec5 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56  t *pToStmt){.  V
1bec6 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64  dbe *pFrom = (Vd
1bec7 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20  be*)pFromStmt;. 
1bec8 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64   Vdbe *pTo = (Vd
1bec9 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69  be*)pToStmt;.  i
1beca 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
1becb 70 54 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e  pTo->db==pFrom->
1becc 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  db );.  assert( 
1becd 70 54 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d  pTo->nVar==pFrom
1bece 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69  ->nVar );.  sqli
1becf 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1bed0 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  pTo->db->mutex);
1bed1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46  .  for(i=0; i<pF
1bed2 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b  rom->nVar; i++){
1bed3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1bed4 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56  MemMove(&pTo->aV
1bed5 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61  ar[i], &pFrom->a
1bed6 56 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73  Var[i]);.  }.  s
1bed7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1bed8 76 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65  ve(pTo->db->mute
1bed9 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
1beda 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
1bedb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
1bedc 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20  EPRECATED./*.** 
1bedd 44 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72  Deprecated exter
1bede 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20  nal interface.  
1bedf 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51  Internal/core SQ
1bee0 4c 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f  Lite code.** sho
1bee1 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  uld call sqlite3
1bee2 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73  TransferBindings
1bee3 2e 0a 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69  ..**.** Is is mi
1bee4 73 75 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69  suse to call thi
1bee5 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73  s routine with s
1bee6 74 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64  tatements from d
1bee7 69 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61  ifferent.** data
1bee8 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
1bee9 2e 20 20 42 75 74 20 61 73 20 74 68 69 73 20 69  .  But as this i
1beea 73 20 61 20 64 65 70 72 65 63 61 74 65 64 20 69  s a deprecated i
1beeb 6e 74 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20  nterface, we.** 
1beec 77 69 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20  will not bother 
1beed 74 6f 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61  to check for tha
1beee 74 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a  t condition..**.
1beef 2a 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73 74  ** If the two st
1bef0 61 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e  atements contain
1bef1 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d   a different num
1bef2 62 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c  ber of bindings,
1bef3 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49   then.** an SQLI
1bef4 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75  TE_ERROR is retu
1bef5 72 6e 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65  rned.  Nothing e
1bef6 6c 73 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67  lse can go wrong
1bef7 2c 20 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a  , so otherwise.*
1bef8 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
1bef9 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
1befa 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1befb 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64  e3_transfer_bind
1befc 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
1befd 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71  t *pFromStmt, sq
1befe 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53  lite3_stmt *pToS
1beff 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46  tmt){.  Vdbe *pF
1bf00 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72  rom = (Vdbe*)pFr
1bf01 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a  omStmt;.  Vdbe *
1bf02 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f  pTo = (Vdbe*)pTo
1bf03 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f  Stmt;.  if( pFro
1bf04 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56  m->nVar!=pTo->nV
1bf05 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ar ){.    return
1bf06 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1bf07 20 7d 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 73   }.  if( pTo->is
1bf08 50 72 65 70 61 72 65 56 32 20 26 26 20 70 54 6f  PrepareV2 && pTo
1bf09 2d 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20  ->expmask ){.   
1bf0a 20 70 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d 20   pTo->expired = 
1bf0b 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 72  1;.  }.  if( pFr
1bf0c 6f 6d 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20  om->isPrepareV2 
1bf0d 26 26 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 73  && pFrom->expmas
1bf0e 6b 20 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d 3e  k ){.    pFrom->
1bf0f 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d  expired = 1;.  }
1bf10 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1bf11 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
1bf12 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f  s(pFromStmt, pTo
1bf13 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  Stmt);.}.#endif.
1bf14 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1bf15 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62  e sqlite3* datab
1bf16 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68  ase handle to wh
1bf17 69 63 68 20 74 68 65 20 70 72 65 70 61 72 65 64  ich the prepared
1bf18 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e   statement given
1bf19 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d  .** in the argum
1bf1a 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68  ent belongs.  Th
1bf1b 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 64  is is the same d
1bf1c 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74  atabase handle t
1bf1d 68 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66  hat was.** the f
1bf1e 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
1bf1f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   the sqlite3_pre
1bf20 70 61 72 65 28 29 20 74 68 61 74 20 77 61 73 20  pare() that was 
1bf21 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a  used to create.*
1bf22 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
1bf23 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61  in the first pla
1bf24 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ce..*/.SQLITE_AP
1bf25 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  I sqlite3 *sqlit
1bf26 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c  e3_db_handle(sql
1bf27 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bf28 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d  ){.  return pStm
1bf29 74 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d  t ? ((Vdbe*)pStm
1bf2a 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f  t)->db : 0;.}../
1bf2b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
1bf2c 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78  inter to the nex
1bf2d 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  t prepared state
1bf2e 6d 65 6e 74 20 61 66 74 65 72 20 70 53 74 6d 74  ment after pStmt
1bf2f 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
1bf30 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ith database con
1bf31 6e 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66  nection pDb.  If
1bf32 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20   pStmt is NULL, 
1bf33 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74  return the first
1bf34 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61  .** prepared sta
1bf35 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64  tement for the d
1bf36 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1bf37 6f 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c  on.  Return NULL
1bf38 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65   if there.** are
1bf39 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c   no more..*/.SQL
1bf3a 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
1bf3b 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65  stmt *sqlite3_ne
1bf3c 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20  xt_stmt(sqlite3 
1bf3d 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74  *pDb, sqlite3_st
1bf3e 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71  mt *pStmt){.  sq
1bf3f 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78  lite3_stmt *pNex
1bf40 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  t;.  sqlite3_mut
1bf41 65 78 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75  ex_enter(pDb->mu
1bf42 74 65 78 29 3b 0a 20 20 69 66 28 20 70 53 74 6d  tex);.  if( pStm
1bf43 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 78  t==0 ){.    pNex
1bf44 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d  t = (sqlite3_stm
1bf45 74 2a 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20  t*)pDb->pVdbe;. 
1bf46 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78   }else{.    pNex
1bf47 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d  t = (sqlite3_stm
1bf48 74 2a 29 28 28 56 64 62 65 2a 29 70 53 74 6d 74  t*)((Vdbe*)pStmt
1bf49 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  )->pNext;.  }.  
1bf4a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1bf4b 61 76 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b  ave(pDb->mutex);
1bf4c 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 78 74 3b  .  return pNext;
1bf4d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1bf4e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
1bf4f 73 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66  status counter f
1bf50 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74  or a prepared st
1bf51 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54  atement.*/.SQLIT
1bf52 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bf53 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71  3_stmt_status(sq
1bf54 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bf55 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72  t, int op, int r
1bf56 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 62  esetFlag){.  Vdb
1bf57 65 20 2a 70 56 64 62 65 20 3d 20 28 56 64 62 65  e *pVdbe = (Vdbe
1bf58 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76  *)pStmt;.  int v
1bf59 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74   = pVdbe->aCount
1bf5a 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20  er[op-1];.  if( 
1bf5b 72 65 73 65 74 46 6c 61 67 20 29 20 70 56 64 62  resetFlag ) pVdb
1bf5c 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31  e->aCounter[op-1
1bf5d 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  ] = 0;.  return 
1bf5e 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  v;.}../*********
1bf5f 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
1bf60 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eapi.c *********
1bf61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf63 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1bf64 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1bf65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.c ********
1bf66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf68 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
1bf69 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
1bf6a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1bf6b 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1bf6c 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1bf6d 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1bf6e 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1bf6f 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1bf70 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1bf71 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1bf72 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1bf73 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1bf74 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1bf75 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1bf76 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1bf77 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1bf78 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1bf79 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1bf7a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1bf7b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1bf7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1bf80 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
1bf81 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73   file implements
1bf82 20 65 78 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f   execution metho
1bf83 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20 56 69 72  d of the .** Vir
1bf84 74 75 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e  tual Database En
1bf85 67 69 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20  gine (VDBE).  A 
1bf86 73 65 70 61 72 61 74 65 20 66 69 6c 65 20 28 22  separate file ("
1bf87 76 64 62 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68  vdbeaux.c").** h
1bf88 61 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70  andles housekeep
1bf89 69 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68  ing details such
1bf8a 20 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64   as creating and
1bf8b 20 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42   deleting.** VDB
1bf8c 45 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68  E instances.  Th
1bf8d 69 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c  is file is solel
1bf8e 79 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20  y interested in 
1bf8f 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65  executing.** the
1bf90 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a   VDBE program..*
1bf91 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 74 65  *.** In the exte
1bf92 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2c 20  rnal interface, 
1bf93 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74  an "sqlite3_stmt
1bf94 2a 22 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20  *" is an opaque 
1bf95 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20  pointer.** to a 
1bf96 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  VDBE..**.** The 
1bf97 53 51 4c 20 70 61 72 73 65 72 20 67 65 6e 65 72  SQL parser gener
1bf98 61 74 65 73 20 61 20 70 72 6f 67 72 61 6d 20 77  ates a program w
1bf99 68 69 63 68 20 69 73 20 74 68 65 6e 20 65 78 65  hich is then exe
1bf9a 63 75 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  cuted by.** the 
1bf9b 56 44 42 45 20 74 6f 20 64 6f 20 74 68 65 20 77  VDBE to do the w
1bf9c 6f 72 6b 20 6f 66 20 74 68 65 20 53 51 4c 20 73  ork of the SQL s
1bf9d 74 61 74 65 6d 65 6e 74 2e 20 20 56 44 42 45 20  tatement.  VDBE 
1bf9e 70 72 6f 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a  programs are .**
1bf9f 20 73 69 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d   similar in form
1bfa0 20 74 6f 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e   to assembly lan
1bfa1 67 75 61 67 65 2e 20 20 54 68 65 20 70 72 6f 67  guage.  The prog
1bfa2 72 61 6d 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a  ram consists of.
1bfa3 2a 2a 20 61 20 6c 69 6e 65 61 72 20 73 65 71 75  ** a linear sequ
1bfa4 65 6e 63 65 20 6f 66 20 6f 70 65 72 61 74 69 6f  ence of operatio
1bfa5 6e 73 2e 20 20 45 61 63 68 20 6f 70 65 72 61 74  ns.  Each operat
1bfa6 69 6f 6e 20 68 61 73 20 61 6e 20 6f 70 63 6f 64  ion has an opcod
1bfa7 65 20 0a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72  e .** and 5 oper
1bfa8 61 6e 64 73 2e 20 20 4f 70 65 72 61 6e 64 73 20  ands.  Operands 
1bfa9 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 33 20 61  P1, P2, and P3 a
1bfaa 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70  re integers.  Op
1bfab 65 72 61 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20  erand P4 .** is 
1bfac 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
1bfad 64 20 73 74 72 69 6e 67 2e 20 20 4f 70 65 72 61  d string.  Opera
1bfae 6e 64 20 50 35 20 69 73 20 61 6e 20 75 6e 73 69  nd P5 is an unsi
1bfaf 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 2e 0a  gned character..
1bfb0 2a 2a 20 46 65 77 20 6f 70 63 6f 64 65 73 20 75  ** Few opcodes u
1bfb1 73 65 20 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64  se all 5 operand
1bfb2 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61  s..**.** Computa
1bfb3 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65  tion results are
1bfb4 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74   stored on a set
1bfb5 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6e 75   of registers nu
1bfb6 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67  mbered beginning
1bfb7 0a 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67  .** with 1 and g
1bfb8 6f 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e  oing up to Vdbe.
1bfb9 6e 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69  nMem.  Each regi
1bfba 73 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a  ster can store.*
1bfbb 2a 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65  * either an inte
1bfbc 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d  ger, a null-term
1bfbd 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61  inated string, a
1bfbe 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a   floating point.
1bfbf 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68  ** number, or th
1bfc0 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c  e SQL "NULL" val
1bfc1 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69 74  ue.  An implicit
1bfc2 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d   conversion from
1bfc3 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20   one.** type to 
1bfc4 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75 72 73  the other occurs
1bfc5 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
1bfc6 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68  * .** Most of th
1bfc7 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
1bfc8 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 75 70 20  ile is taken up 
1bfc9 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 56 64  by the sqlite3Vd
1bfca 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63  beExec().** func
1bfcb 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20  tion which does 
1bfcc 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65  the work of inte
1bfcd 72 70 72 65 74 69 6e 67 20 61 20 56 44 42 45 20  rpreting a VDBE 
1bfce 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20  program..** But 
1bfcf 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 73 20 61  other routines a
1bfd0 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 64  re also provided
1bfd1 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 75 69 6c   to help in buil
1bfd2 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f  ding up.** a pro
1bfd3 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  gram instruction
1bfd4 20 62 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e   by instruction.
1bfd5 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73  .**.** Various s
1bfd6 63 72 69 70 74 73 20 73 63 61 6e 20 74 68 69 73  cripts scan this
1bfd7 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 6e 20   source file in 
1bfd8 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74  order to generat
1bfd9 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65  e HTML.** docume
1bfda 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 65 72 73  ntation, headers
1bfdb 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72   files, or other
1bfdc 20 64 65 72 69 76 65 64 20 66 69 6c 65 73 2e 20   derived files. 
1bfdd 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a   The formatting.
1bfde 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69  ** of the code i
1bfdf 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 2c 20  n this file is, 
1bfe0 74 68 65 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72  therefore, impor
1bfe1 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 68 65 72  tant.  See other
1bfe2 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20   comments.** in 
1bfe3 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65  this file for de
1bfe4 74 61 69 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f  tails.  If in do
1bfe5 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69  ubt, do not devi
1bfe6 61 74 65 20 66 72 6f 6d 20 65 78 69 73 74 69 6e  ate from existin
1bfe7 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20  g.** commenting 
1bfe8 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20  and indentation 
1bfe9 70 72 61 63 74 69 63 65 73 20 77 68 65 6e 20 63  practices when c
1bfea 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 64 69 6e  hanging or addin
1bfeb 67 20 63 6f 64 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  g code..*/../*.*
1bfec 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1bfed 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1bfee 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65  is incremented e
1bfef 76 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 73  very time a curs
1bff0 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74  or.** moves, eit
1bff1 68 65 72 20 62 79 20 74 68 65 20 4f 50 5f 53 65  her by the OP_Se
1bff2 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f  ekXX, OP_Next, o
1bff3 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 65  r OP_Prev opcode
1bff4 73 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a 20  s.  The test.** 
1bff5 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74  procedures use t
1bff6 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
1bff7 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
1bff8 74 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a  t indices are.**
1bff9 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74   working correct
1bffa 6c 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 62  ly.  This variab
1bffb 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69  le has no functi
1bffc 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f  on other than to
1bffd 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20  .** help verify 
1bffe 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72  the correct oper
1bfff 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62  ation of the lib
1c000 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rary..*/.#ifdef 
1c001 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
1c002 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1c003 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20  e3_search_count 
1c004 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
1c005 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f  ** When this glo
1c006 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
1c007 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74  positive, it get
1c008 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e  s decremented on
1c009 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63  ce before.** eac
1c00a 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e  h instruction in
1c00b 20 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e   the VDBE.  When
1c00c 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74   reaches zero, t
1c00d 68 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70  he u1.isInterrup
1c00e 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20  ted.** field of 
1c00f 74 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75  the sqlite3 stru
1c010 63 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20  cture is set in 
1c011 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74  order to simulat
1c012 65 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e  e and interrupt.
1c013 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69  .**.** This faci
1c014 6c 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72  lity is used for
1c015 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65   testing purpose
1c016 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73  s only.  It does
1c017 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a   not function.**
1c018 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20   in an ordinary 
1c019 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  build..*/.#ifdef
1c01a 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
1c01b 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1c01c 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f  te3_interrupt_co
1c01d 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
1c01e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  ./*.** The next 
1c01f 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1c020 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65  is incremented e
1c021 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f  ach type the OP_
1c022 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69  Sort opcode.** i
1c023 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65  s executed.  The
1c024 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73   test procedures
1c025 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d   use this inform
1c026 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75  ation to make su
1c027 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69  re that.** sorti
1c028 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 20  ng is occurring 
1c029 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67  or not occurring
1c02a 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20   at appropriate 
1c02b 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 61  times.   This va
1c02c 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f  riable.** has no
1c02d 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20   function other 
1c02e 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72  than to help ver
1c02f 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
1c030 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
1c031 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  .** library..*/.
1c032 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1c033 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
1c034 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63  t sqlite3_sort_c
1c035 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
1c036 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ../*.** The next
1c037 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
1c038 20 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 7a   records the siz
1c039 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  e of the largest
1c03a 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20   MEM_Blob.** or 
1c03b 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 73  MEM_Str that has
1c03c 20 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 20   been used by a 
1c03d 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 68  VDBE opcode.  Th
1c03e 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65  e test procedure
1c03f 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 6e  s.** use this in
1c040 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
1c041 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
1c042 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69  zero-blob functi
1c043 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f  onality.** is wo
1c044 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e  rking correctly.
1c045 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65     This variable
1c046 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e   has no function
1c047 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a   other than to.*
1c048 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68  * help verify th
1c049 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74  e correct operat
1c04a 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ion of the libra
1c04b 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ry..*/.#ifdef SQ
1c04c 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
1c04d 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1c04e 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20  _max_blobsize = 
1c04f 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  0;.static void u
1c050 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65  pdateMaxBlobsize
1c051 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20  (Mem *p){.  if( 
1c052 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  (p->flags & (MEM
1c053 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21  _Str|MEM_Blob))!
1c054 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74  =0 && p->n>sqlit
1c055 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20  e3_max_blobsize 
1c056 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
1c057 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d  ax_blobsize = p-
1c058 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  >n;.  }.}.#endif
1c059 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ../*.** The next
1c05a 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
1c05b 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
1c05c 65 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50  each type the OP
1c05d 5f 46 6f 75 6e 64 20 6f 70 63 6f 64 65 0a 2a 2a  _Found opcode.**
1c05e 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68   is executed. Th
1c05f 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65  is is used to te
1c060 73 74 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  st whether or no
1c061 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  t the foreign ke
1c062 79 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69  y.** operation i
1c063 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67  mplemented using
1c064 20 4f 50 5f 46 6b 49 73 5a 65 72 6f 20 69 73 20   OP_FkIsZero is 
1c065 77 6f 72 6b 69 6e 67 2e 20 54 68 69 73 20 76 61  working. This va
1c066 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f  riable.** has no
1c067 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20   function other 
1c068 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72  than to help ver
1c069 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
1c06a 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
1c06b 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  .** library..*/.
1c06c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1c06d 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
1c06e 74 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f  t sqlite3_found_
1c06f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69  count = 0;.#endi
1c070 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20  f../*.** Test a 
1c071 72 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20  register to see 
1c072 69 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68  if it exceeds th
1c073 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75  e current maximu
1c074 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20  m blob size..** 
1c075 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f  If it does, reco
1c076 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d  rd the new maxim
1c077 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f  um blob size..*/
1c078 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
1c079 49 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65  ITE_TEST) && !de
1c07a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1c07b 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a  T_BUILTIN_TEST).
1c07c 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f  # define UPDATE_
1c07d 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20  MAX_BLOBSIZE(P) 
1c07e 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69   updateMaxBlobsi
1c07f 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65  ze(P).#else.# de
1c080 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f  fine UPDATE_MAX_
1c081 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64  BLOBSIZE(P).#end
1c082 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  if../*.** Conver
1c083 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69  t the given regi
1c084 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69  ster into a stri
1c085 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f  ng if it isn't o
1c086 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52  ne.** already. R
1c087 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
1c088 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
1c089 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ls..*/.#define S
1c08a 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29  tringify(P, enc)
1c08b 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66   \.   if(((P)->f
1c08c 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45  lags&(MEM_Str|ME
1c08d 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73  M_Blob))==0 && s
1c08e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
1c08f 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c  ingify(P,enc)) \
1c090 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f  .     { goto no_
1c091 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e  mem; }../*.** An
1c092 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e   ephemeral strin
1c093 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69  g value (signifi
1c094 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70  ed by the MEM_Ep
1c095 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69  hem flag) contai
1c096 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  ns.** a pointer 
1c097 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  to a dynamically
1c098 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e   allocated strin
1c099 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68  g where some oth
1c09a 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20  er entity.** is 
1c09b 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
1c09c 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61  deallocating tha
1c09d 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75  t string.  Becau
1c09e 73 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a  se the register.
1c09f 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74  ** does not cont
1c0a0 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20  rol the string, 
1c0a1 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65  it might be dele
1c0a2 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20  ted without the 
1c0a3 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77  register.** know
1c0a4 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ing it..**.** Th
1c0a5 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65  is routine conve
1c0a6 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c  rts an ephemeral
1c0a7 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64   string into a d
1c0a8 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
1c0a9 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74  ated.** string t
1c0aa 68 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72  hat the register
1c0ab 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73   itself controls
1c0ac 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
1c0ad 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74  s, it.** convert
1c0ae 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73  s an MEM_Ephem s
1c0af 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45  tring into an ME
1c0b0 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f  M_Dyn string..*/
1c0b1 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d  .#define Deephem
1c0b2 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20  eralize(P) \.   
1c0b3 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26  if( ((P)->flags&
1c0b4 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a  MEM_Ephem)!=0 \.
1c0b5 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
1c0b6 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
1c0b7 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f  eable(P) ){ goto
1c0b8 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a   no_mem;}../*.**
1c0b9 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62   Call sqlite3Vdb
1c0ba 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29  eMemExpandBlob()
1c0bb 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64   on the supplied
1c0bc 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d   value (type Mem
1c0bd 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69  *).** P if requi
1c0be 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  red..*/.#define 
1c0bf 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28  ExpandBlob(P) ((
1c0c0 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a  (P)->flags&MEM_Z
1c0c1 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65  ero)?sqlite3Vdbe
1c0c2 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29  MemExpandBlob(P)
1c0c3 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  :0)../*.** Argum
1c0c4 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20  ent pMem points 
1c0c5 61 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68  at a register th
1c0c6 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65  at will be passe
1c0c7 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64  d to a.** user-d
1c0c8 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
1c0c9 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  or returned to t
1c0ca 68 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72  he user as the r
1c0cb 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79  esult of a query
1c0cc 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1c0cd 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d  e sets the pMem-
1c0ce 3e 74 79 70 65 20 76 61 72 69 61 62 6c 65 20 75  >type variable u
1c0cf 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74  sed by the sqlit
1c0d0 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 0a 2a 2a  e3_value_*() .**
1c0d1 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51   routines..*/.SQ
1c0d2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1c0d3 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
1c0d4 53 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70  StoreType(Mem *p
1c0d5 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67  Mem){.  int flag
1c0d6 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b  s = pMem->flags;
1c0d7 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  .  if( flags & M
1c0d8 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70  EM_Null ){.    p
1c0d9 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1c0da 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65  TE_NULL;.  }.  e
1c0db 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
1c0dc 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70  MEM_Int ){.    p
1c0dd 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1c0de 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a  TE_INTEGER;.  }.
1c0df 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73    else if( flags
1c0e0 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
1c0e1 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1c0e2 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20  SQLITE_FLOAT;.  
1c0e3 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61  }.  else if( fla
1c0e4 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a  gs & MEM_Str ){.
1c0e5 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1c0e6 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20   SQLITE_TEXT;.  
1c0e7 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d  }else{.    pMem-
1c0e8 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42  >type = SQLITE_B
1c0e9 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  LOB;.  }.}../*.*
1c0ea 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43  * Allocate VdbeC
1c0eb 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75  ursor number iCu
1c0ec 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  r.  Return a poi
1c0ed 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74  nter to it.  Ret
1c0ee 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77  urn NULL.** if w
1c0ef 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d  e run out of mem
1c0f0 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56  ory..*/.static V
1c0f1 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63  dbeCursor *alloc
1c0f2 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62  ateCursor(.  Vdb
1c0f3 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
1c0f4 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
1c0f5 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69  l machine */.  i
1c0f6 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20  nt iCur,        
1c0f7 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1c0f8 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 72   the new VdbeCur
1c0f9 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69  sor */.  int nFi
1c0fa 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f  eld,           /
1c0fb 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * Number of fiel
1c0fc 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ds in the table 
1c0fd 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  or index */.  in
1c0fe 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20  t iDb,          
1c0ff 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 61      /* When data
1c100 62 61 73 65 20 74 68 65 20 63 75 72 73 6f 72 20  base the cursor 
1c101 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d  belongs to, or -
1c102 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 72  1 */.  int isBtr
1c103 65 65 43 75 72 73 6f 72 20 20 20 20 20 2f 2a 20  eeCursor     /* 
1c104 54 72 75 65 20 66 6f 72 20 42 2d 54 72 65 65 2e  True for B-Tree.
1c105 20 20 46 61 6c 73 65 20 66 6f 72 20 70 73 65 75    False for pseu
1c106 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76 74 61 62  do-table or vtab
1c107 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64   */.){.  /* Find
1c108 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
1c109 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73   that will be us
1c10a 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
1c10b 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20  blob of memory. 
1c10c 20 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f 72   ** required for
1c10d 20 74 68 69 73 20 56 64 62 65 43 75 72 73 6f 72   this VdbeCursor
1c10e 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 69   structure. It i
1c10f 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20  s convenient to 
1c110 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62 65  use a .  ** vdbe
1c111 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20   memory cell to 
1c112 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f 72  manage the memor
1c113 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
1c114 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a  uired for a.  **
1c115 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 75   VdbeCursor stru
1c116 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 66 6f  cture for the fo
1c117 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a  llowing reasons:
1c118 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53  .  **.  **   * S
1c119 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 6f 72 20  ometimes cursor 
1c11a 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64  numbers are used
1c11b 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 6f 66   for a couple of
1c11c 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20   different.  ** 
1c11d 20 20 20 20 70 75 72 70 6f 73 65 73 20 69 6e 20      purposes in 
1c11e 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20  a vdbe program. 
1c11f 54 68 65 20 64 69 66 66 65 72 65 6e 74 20 75 73  The different us
1c120 65 73 20 6d 69 67 68 74 20 72 65 71 75 69 72 65  es might require
1c121 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 66 65 72  .  **     differ
1c122 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61  ent sized alloca
1c123 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65  tions. Memory ce
1c124 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 72 6f 77  lls provide grow
1c125 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c  able.  **     al
1c126 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a  locations..  **.
1c127 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75 73    **   * When us
1c128 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ing ENABLE_MEMOR
1c129 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65  Y_MANAGEMENT, me
1c12a 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65 72  mory cell buffer
1c12b 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62  s can.  **     b
1c12c 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20 76  e freed lazily v
1c12d 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  ia the sqlite3_r
1c12e 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
1c12f 41 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20  API. This.  **  
1c130 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65     minimizes the
1c131 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f   number of mallo
1c132 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 20  c calls made by 
1c133 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a  the system..  **
1c134 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c  .  ** Memory cel
1c135 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 73 20 61  ls for cursors a
1c136 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20  re allocated at 
1c137 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 61  the top of the a
1c138 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 61 63  ddress.  ** spac
1c139 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28  e. Memory cell (
1c13a 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70  p->nMem) corresp
1c13b 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 20 30  onds to cursor 0
1c13c 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a  . Space for.  **
1c13d 20 63 75 72 73 6f 72 20 31 20 69 73 20 6d 61 6e   cursor 1 is man
1c13e 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 20 63  aged by memory c
1c13f 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c  ell (p->nMem-1),
1c140 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d   etc..  */.  Mem
1c141 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65   *pMem = &p->aMe
1c142 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b  m[p->nMem-iCur];
1c143 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  ..  int nByte;. 
1c144 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1c145 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20   = 0;.  nByte = 
1c146 0a 20 20 20 20 20 20 52 4f 55 4e 44 38 28 73 69  .      ROUND8(si
1c147 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29  zeof(VdbeCursor)
1c148 29 20 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74  ) + .      (isBt
1c149 72 65 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65  reeCursor?sqlite
1c14a 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
1c14b 28 29 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32  ():0) + .      2
1c14c 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75  *nField*sizeof(u
1c14d 33 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  32);..  assert( 
1c14e 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  iCur<p->nCursor 
1c14f 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73  );.  if( p->apCs
1c150 72 5b 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73  r[iCur] ){.    s
1c151 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
1c152 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72  rsor(p, p->apCsr
1c153 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e  [iCur]);.    p->
1c154 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b  apCsr[iCur] = 0;
1c155 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54  .  }.  if( SQLIT
1c156 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62  E_OK==sqlite3Vdb
1c157 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e  eMemGrow(pMem, n
1c158 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Byte, 0) ){.    
1c159 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d  p->apCsr[iCur] =
1c15a 20 70 43 78 20 3d 20 28 56 64 62 65 43 75 72 73   pCx = (VdbeCurs
1c15b 6f 72 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20  or*)pMem->z;.   
1c15c 20 6d 65 6d 73 65 74 28 70 43 78 2c 20 30 2c 20   memset(pCx, 0, 
1c15d 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f  sizeof(VdbeCurso
1c15e 72 29 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44  r));.    pCx->iD
1c15f 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78  b = iDb;.    pCx
1c160 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c  ->nField = nFiel
1c161 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c  d;.    if( nFiel
1c162 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e  d ){.      pCx->
1c163 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26  aType = (u32 *)&
1c164 70 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73  pMem->z[ROUND8(s
1c165 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
1c166 29 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ))];.    }.    i
1c167 66 28 20 69 73 42 74 72 65 65 43 75 72 73 6f 72  f( isBtreeCursor
1c168 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 70   ){.      pCx->p
1c169 43 75 72 73 6f 72 20 3d 20 28 42 74 43 75 72 73  Cursor = (BtCurs
1c16a 6f 72 2a 29 0a 20 20 20 20 20 20 20 20 20 20 26  or*).          &
1c16b 70 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73  pMem->z[ROUND8(s
1c16c 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
1c16d 29 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65  ))+2*nField*size
1c16e 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 20 20  of(u32)];.      
1c16f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1c170 6f 72 5a 65 72 6f 28 70 43 78 2d 3e 70 43 75 72  orZero(pCx->pCur
1c171 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  sor);.    }.  }.
1c172 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a    return pCx;.}.
1c173 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f  ./*.** Try to co
1c174 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20 69 6e  nvert a value in
1c175 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70  to a numeric rep
1c176 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 77  resentation if w
1c177 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77  e can.** do so w
1c178 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69  ithout loss of i
1c179 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20  nformation.  In 
1c17a 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
1c17b 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f  the string.** lo
1c17c 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65  oks like a numbe
1c17d 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e  r, convert it in
1c17e 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66  to a number.  If
1c17f 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20   it does not.** 
1c180 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62  look like a numb
1c181 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61 6c 6f  er, leave it alo
1c182 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ne..*/.static vo
1c183 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41  id applyNumericA
1c184 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65  ffinity(Mem *pRe
1c185 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65 63 2d  c){.  if( (pRec-
1c186 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65  >flags & (MEM_Re
1c187 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20  al|MEM_Int))==0 
1c188 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61 6c 6e  ){.    int realn
1c189 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  um;.    sqlite3V
1c18a 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
1c18b 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66  te(pRec);.    if
1c18c 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d  ( (pRec->flags&M
1c18d 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20 20 20  EM_Str).        
1c18e 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e 75 6d   && sqlite3IsNum
1c18f 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65  ber(pRec->z, &re
1c190 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63  alnum, pRec->enc
1c191 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76  ) ){.      i64 v
1c192 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  alue;.      sqli
1c193 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1c194 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51 4c 49  oding(pRec, SQLI
1c195 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20  TE_UTF8);.      
1c196 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26 26 20  if( !realnum && 
1c197 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 52  sqlite3Atoi64(pR
1c198 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29  ec->z, &value) )
1c199 7b 0a 20 20 20 20 20 20 20 20 70 52 65 63 2d 3e  {.        pRec->
1c19a 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20  u.i = value;.   
1c19b 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46       MemSetTypeF
1c19c 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e  lag(pRec, MEM_In
1c19d 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
1c19e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c19f 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70  VdbeMemRealify(p
1c1a0 52 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Rec);.      }.  
1c1a1 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1c1a2 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64   Processing is d
1c1a3 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20  etermine by the 
1c1a4 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74  affinity paramet
1c1a5 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  er:.**.** SQLITE
1c1a6 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a  _AFF_INTEGER:.**
1c1a7 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
1c1a8 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  :.** SQLITE_AFF_
1c1a9 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54  NUMERIC:.**    T
1c1aa 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52  ry to convert pR
1c1ab 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  ec to an integer
1c1ac 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1c1ad 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61  or a .**    floa
1c1ae 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65  ting-point repre
1c1af 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20  sentation if an 
1c1b0 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e  integer represen
1c1b1 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20  tation.**    is 
1c1b2 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e  not possible.  N
1c1b3 6f 74 65 20 74 68 61 74 20 74 68 65 20 69 6e 74  ote that the int
1c1b4 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74  eger representat
1c1b5 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77  ion is.**    alw
1c1b6 61 79 73 20 70 72 65 66 65 72 72 65 64 2c 20 65  ays preferred, e
1c1b7 76 65 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e  ven if the affin
1c1b8 69 74 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63  ity is REAL, bec
1c1b9 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e  ause.**    an in
1c1ba 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61  teger representa
1c1bb 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61  tion is more spa
1c1bc 63 65 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20  ce efficient on 
1c1bd 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  disk..**.** SQLI
1c1be 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20  TE_AFF_TEXT:.** 
1c1bf 20 20 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20     Convert pRec 
1c1c0 74 6f 20 61 20 74 65 78 74 20 72 65 70 72 65 73  to a text repres
1c1c1 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
1c1c2 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a  SQLITE_AFF_NONE:
1c1c3 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70  .**    No-op.  p
1c1c4 52 65 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64  Rec is unchanged
1c1c5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1c1c6 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a   applyAffinity(.
1c1c7 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20    Mem *pRec,    
1c1c8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c        /* The val
1c1c9 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69  ue to apply affi
1c1ca 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61  nity to */.  cha
1c1cb 72 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20  r affinity,     
1c1cc 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
1c1cd 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a   to be applied *
1c1ce 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 20 20 20  /.  u8 enc      
1c1cf 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74          /* Use t
1c1d0 68 69 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  his text encodin
1c1d1 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66  g */.){.  if( af
1c1d2 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
1c1d3 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f  FF_TEXT ){.    /
1c1d4 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74  * Only attempt t
1c1d5 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f  he conversion to
1c1d6 20 54 45 58 54 20 69 66 20 74 68 65 72 65 20 69   TEXT if there i
1c1d7 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20  s an integer or 
1c1d8 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72  real.    ** repr
1c1d9 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62  esentation (blob
1c1da 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74   and NULL do not
1c1db 20 67 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20   get converted) 
1c1dc 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20  but no string.  
1c1dd 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74    ** representat
1c1de 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
1c1df 69 66 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c  if( 0==(pRec->fl
1c1e0 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20  ags&MEM_Str) && 
1c1e1 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45  (pRec->flags&(ME
1c1e2 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29  M_Real|MEM_Int))
1c1e3 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1c1e4 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66  3VdbeMemStringif
1c1e5 79 28 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20  y(pRec, enc);.  
1c1e6 20 20 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c    }.    pRec->fl
1c1e7 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61  ags &= ~(MEM_Rea
1c1e8 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65  l|MEM_Int);.  }e
1c1e9 6c 73 65 20 69 66 28 20 61 66 66 69 6e 69 74 79  lse if( affinity
1c1ea 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  !=SQLITE_AFF_NON
1c1eb 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  E ){.    assert(
1c1ec 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
1c1ed 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c  E_AFF_INTEGER ||
1c1ee 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
1c1ef 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20  E_AFF_REAL.     
1c1f0 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e          || affin
1c1f1 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
1c1f2 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61  NUMERIC );.    a
1c1f3 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
1c1f4 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69  ity(pRec);.    i
1c1f5 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26  f( pRec->flags &
1c1f6 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20   MEM_Real ){.   
1c1f7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e     sqlite3VdbeIn
1c1f8 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 52  tegerAffinity(pR
1c1f9 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ec);.    }.  }.}
1c1fa 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63  ../*.** Try to c
1c1fb 6f 6e 76 65 72 74 20 74 68 65 20 74 79 70 65 20  onvert the type 
1c1fc 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72  of a function ar
1c1fd 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75  gument or a resu
1c1fe 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74  lt column.** int
1c1ff 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72  o a numeric repr
1c200 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65  esentation.  Use
1c201 20 65 69 74 68 65 72 20 49 4e 54 45 47 45 52 20   either INTEGER 
1c202 6f 72 20 52 45 41 4c 20 77 68 69 63 68 65 76 65  or REAL whicheve
1c203 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69  r.** is appropri
1c204 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64  ate.  But only d
1c205 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  o the conversion
1c206 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62   if it is possib
1c207 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f  le without.** lo
1c208 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f  ss of informatio
1c209 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  n and return the
1c20a 20 72 65 76 69 73 65 64 20 74 79 70 65 20 6f 66   revised type of
1c20b 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   the argument..*
1c20c 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20  *.** This is an 
1c20d 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 61 70 69  EXPERIMENTAL api
1c20e 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20   and is subject 
1c20f 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 72 65 6d  to change or rem
1c210 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oval..*/.SQLITE_
1c211 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1c212 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
1c213 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  pe(sqlite3_value
1c214 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a   *pVal){.  Mem *
1c215 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61  pMem = (Mem*)pVa
1c216 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69  l;.  applyNumeri
1c217 63 41 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b  cAffinity(pMem);
1c218 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1c219 6d 53 74 6f 72 65 54 79 70 65 28 70 4d 65 6d 29  mStoreType(pMem)
1c21a 3b 0a 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d  ;.  return pMem-
1c21b 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  >type;.}../*.** 
1c21c 45 78 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e  Exported version
1c21d 20 6f 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74   of applyAffinit
1c21e 79 28 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f  y(). This one wo
1c21f 72 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76  rks on sqlite3_v
1c220 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74  alue*, .** not t
1c221 68 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a  he internal Mem*
1c222 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   type..*/.SQLITE
1c223 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1c224 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41  lite3ValueApplyA
1c225 66 66 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74  ffinity(.  sqlit
1c226 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20  e3_value *pVal, 
1c227 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20  .  u8 affinity, 
1c228 0a 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61  .  u8 enc.){.  a
1c229 70 70 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65  pplyAffinity((Me
1c22a 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69  m *)pVal, affini
1c22b 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66  ty, enc);.}..#if
1c22c 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1c22d 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e  ./*.** Write a n
1c22e 69 63 65 20 73 74 72 69 6e 67 20 72 65 70 72 65  ice string repre
1c22f 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
1c230 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c   contents of cel
1c231 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62  l pMem.** into b
1c232 75 66 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67  uffer zBuf, leng
1c233 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49  th nBuf..*/.SQLI
1c234 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1c235 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72  sqlite3VdbeMemPr
1c236 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70  ettyPrint(Mem *p
1c237 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Mem, char *zBuf)
1c238 7b 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d  {.  char *zCsr =
1c239 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d   zBuf;.  int f =
1c23a 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20   pMem->flags;.. 
1c23b 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
1c23c 61 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d  ar *const encnam
1c23d 65 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22  es[] = {"(X)", "
1c23e 28 38 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20  (8)", "(16LE)", 
1c23f 22 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66  "(16BE)"};..  if
1c240 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a  ( f&MEM_Blob ){.
1c241 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63      int i;.    c
1c242 68 61 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66  har c;.    if( f
1c243 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20   & MEM_Dyn ){.  
1c244 20 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20      c = 'z';.   
1c245 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1c246 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
1c247 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20  Ephem))==0 );.  
1c248 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20    }else if( f & 
1c249 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20  MEM_Static ){.  
1c24a 20 20 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20      c = 't';.   
1c24b 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1c24c 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68  (MEM_Dyn|MEM_Eph
1c24d 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  em))==0 );.    }
1c24e 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
1c24f 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20  _Ephem ){.      
1c250 63 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61  c = 'e';.      a
1c251 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
1c252 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29  _Static|MEM_Dyn)
1c253 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73  )==0 );.    }els
1c254 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27  e{.      c = 's'
1c255 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
1c256 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
1c257 30 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63  0, zCsr, "%c", c
1c258 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73  );.    zCsr += s
1c259 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
1c25a 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Csr);.    sqlite
1c25b 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
1c25c 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65  zCsr, "%d[", pMe
1c25d 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20  m->n);.    zCsr 
1c25e 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c25f 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f  30(zCsr);.    fo
1c260 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69  r(i=0; i<16 && i
1c261 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a  <pMem->n; i++){.
1c262 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1c263 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
1c264 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29  , "%02X", ((int)
1c265 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46  pMem->z[i] & 0xF
1c266 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20  F));.      zCsr 
1c267 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c268 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a  30(zCsr);.    }.
1c269 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31      for(i=0; i<1
1c26a 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20  6 && i<pMem->n; 
1c26b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72  i++){.      char
1c26c 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b   z = pMem->z[i];
1c26d 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20  .      if( z<32 
1c26e 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72  || z>126 ) *zCsr
1c26f 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20  ++ = '.';.      
1c270 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a  else *zCsr++ = z
1c271 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
1c272 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
1c273 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20  0, zCsr, "]%s", 
1c274 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65  encnames[pMem->e
1c275 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b  nc]);.    zCsr +
1c276 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1c277 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28  0(zCsr);.    if(
1c278 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b   f & MEM_Zero ){
1c279 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1c27a 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73  nprintf(100, zCs
1c27b 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75  r,"+%dz",pMem->u
1c27c 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a  .nZero);.      z
1c27d 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  Csr += sqlite3St
1c27e 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20  rlen30(zCsr);.  
1c27f 20 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20    }.    *zCsr = 
1c280 27 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66  '\0';.  }else if
1c281 28 20 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b  ( f & MEM_Str ){
1c282 0a 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20  .    int j, k;. 
1c283 20 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27     zBuf[0] = ' '
1c284 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45  ;.    if( f & ME
1c285 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a  M_Dyn ){.      z
1c286 42 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20  Buf[1] = 'z';.  
1c287 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26      assert( (f &
1c288 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d   (MEM_Static|MEM
1c289 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20  _Ephem))==0 );. 
1c28a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26     }else if( f &
1c28b 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20   MEM_Static ){. 
1c28c 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27       zBuf[1] = '
1c28d 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t';.      assert
1c28e 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c  ( (f & (MEM_Dyn|
1c28f 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29  MEM_Ephem))==0 )
1c290 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1c291 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b  f & MEM_Ephem ){
1c292 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d  .      zBuf[1] =
1c293 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65   'e';.      asse
1c294 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74  rt( (f & (MEM_St
1c295 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d  atic|MEM_Dyn))==
1c296 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  0 );.    }else{.
1c297 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20        zBuf[1] = 
1c298 27 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b  's';.    }.    k
1c299 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 2;.    sqlite
1c29a 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
1c29b 26 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20  &zBuf[k], "%d", 
1c29c 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20  pMem->n);.    k 
1c29d 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c29e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20  30(&zBuf[k]);.  
1c29f 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b    zBuf[k++] = '[
1c2a0 27 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  ';.    for(j=0; 
1c2a1 6a 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e  j<15 && j<pMem->
1c2a2 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75  n; j++){.      u
1c2a3 38 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d  8 c = pMem->z[j]
1c2a4 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30  ;.      if( c>=0
1c2a5 78 32 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b  x20 && c<0x7f ){
1c2a6 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b  .        zBuf[k+
1c2a7 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65  +] = c;.      }e
1c2a8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  lse{.        zBu
1c2a9 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20  f[k++] = '.';.  
1c2aa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1c2ab 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b  zBuf[k++] = ']';
1c2ac 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1c2ad 72 69 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b  rintf(100,&zBuf[
1c2ae 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65  k], encnames[pMe
1c2af 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20  m->enc]);.    k 
1c2b0 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c2b1 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20  30(&zBuf[k]);.  
1c2b2 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b    zBuf[k++] = 0;
1c2b3 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  .  }.}.#endif..#
1c2b4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1c2b5 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74  UG./*.** Print t
1c2b6 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65  he value of a re
1c2b7 67 69 73 74 65 72 20 66 6f 72 20 74 72 61 63 69  gister for traci
1c2b8 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a  ng purposes:.*/.
1c2b9 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54  static void memT
1c2ba 72 61 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a  racePrint(FILE *
1c2bb 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20  out, Mem *p){.  
1c2bc 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  if( p->flags & M
1c2bd 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66  EM_Null ){.    f
1c2be 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55  printf(out, " NU
1c2bf 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  LL");.  }else if
1c2c0 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d  ( (p->flags & (M
1c2c1 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29  EM_Int|MEM_Str))
1c2c2 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53  ==(MEM_Int|MEM_S
1c2c3 74 72 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  tr) ){.    fprin
1c2c4 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c  tf(out, " si:%ll
1c2c5 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d  d", p->u.i);.  }
1c2c6 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67  else if( p->flag
1c2c7 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
1c2c8 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
1c2c9 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e  " i:%lld", p->u.
1c2ca 69 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  i);.#ifndef SQLI
1c2cb 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
1c2cc 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69  _POINT.  }else i
1c2cd 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45  f( p->flags & ME
1c2ce 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70  M_Real ){.    fp
1c2cf 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25  rintf(out, " r:%
1c2d0 67 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69  g", p->r);.#endi
1c2d1 66 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  f.  }else if( p-
1c2d2 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
1c2d3 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  Set ){.    fprin
1c2d4 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73 65  tf(out, " (rowse
1c2d5 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  t)");.  }else{. 
1c2d6 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30     char zBuf[200
1c2d7 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
1c2d8 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
1c2d9 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66  (p, zBuf);.    f
1c2da 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29  printf(out, " ")
1c2db 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
1c2dc 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a  t, "%s", zBuf);.
1c2dd 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
1c2de 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28  d registerTrace(
1c2df 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69  FILE *out, int i
1c2e0 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20  Reg, Mem *p){.  
1c2e1 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45  fprintf(out, "RE
1c2e2 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29  G[%d] = ", iReg)
1c2e3 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e  ;.  memTracePrin
1c2e4 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72  t(out, p);.  fpr
1c2e5 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
1c2e6 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  .}.#endif..#ifde
1c2e7 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23  f SQLITE_DEBUG.#
1c2e8 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45    define REGISTE
1c2e9 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28  R_TRACE(R,M) if(
1c2ea 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65  p->trace)registe
1c2eb 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c  rTrace(p->trace,
1c2ec 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65  R,M).#else.#  de
1c2ed 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52  fine REGISTER_TR
1c2ee 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a  ACE(R,M).#endif.
1c2ef 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ..#ifdef VDBE_PR
1c2f0 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77  OFILE../* .** hw
1c2f1 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20  time.h contains 
1c2f2 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72  inline assembler
1c2f3 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d   code for implem
1c2f4 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d  enting .** high-
1c2f5 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69  performance timi
1c2f6 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  ng routines..*/.
1c2f7 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1c2f8 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68  Include hwtime.h
1c2f9 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
1c2fa 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.c *******
1c2fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1c2fc 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1c2fd 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d  Begin file hwtim
1c2fe 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
1c2ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1c301 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
1c302 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
1c303 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1c304 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1c305 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1c306 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1c307 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1c308 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1c309 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1c30a 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1c30b 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1c30c 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1c30d 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1c30e 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1c30f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1c310 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1c311 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1c312 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1c313 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1c314 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c315 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c316 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c317 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c318 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1c319 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69   file contains i
1c31a 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66  nline asm code f
1c31b 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68  or retrieving "h
1c31c 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22  igh-performance"
1c31d 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72  .** counters for
1c31e 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e   x86 class CPUs.
1c31f 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54  .*/.#ifndef _HWT
1c320 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  IME_H_.#define _
1c321 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  HWTIME_H_../*.**
1c322 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
1c323 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
1c324 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61  s on pentium-cla
1c325 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72  ss (or newer) pr
1c326 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20  ocessors..** It 
1c327 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f  uses the RDTSC o
1c328 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68  pcode to read th
1c329 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61  e cycle count va
1c32a 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a  lue out of the.*
1c32b 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20  * processor and 
1c32c 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c  returns that val
1c32d 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ue.  This can be
1c32e 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72   used for high-r
1c32f 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e  es.** profiling.
1c330 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64  .*/.#if (defined
1c331 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65  (__GNUC__) || de
1c332 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29  fined(_MSC_VER))
1c333 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66   && \.      (def
1c334 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65  ined(i386) || de
1c335 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20  fined(__i386__) 
1c336 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58  || defined(_M_IX
1c337 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69  86))..  #if defi
1c338 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20  ned(__GNUC__).. 
1c339 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
1c33a 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
1c33b 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
1c33c 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
1c33d 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f   lo, hi;.     __
1c33e 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
1c33f 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
1c340 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68  a" (lo), "=d" (h
1c341 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e  i));.     return
1c342 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29   (sqlite_uint64)
1c343 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20  hi << 32 | lo;. 
1c344 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69   }..  #elif defi
1c345 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20  ned(_MSC_VER).. 
1c346 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65   __declspec(nake
1c347 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69  d) __inline sqli
1c348 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63  te_uint64 __cdec
1c349 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  l sqlite3Hwtime(
1c34a 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73  void){.     __as
1c34b 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73  m {.        rdts
1c34c 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20  c.        ret   
1c34d 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c      ; return val
1c34e 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20  ue at EDX:EAX.  
1c34f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64     }.  }..  #end
1c350 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  if..#elif (defin
1c351 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
1c352 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34  defined(__x86_64
1c353 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  __))..  __inline
1c354 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
1c355 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
1c356 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  oid){.      unsi
1c357 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20  gned long val;. 
1c358 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
1c359 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
1c35a 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29  c" : "=A" (val))
1c35b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76  ;.      return v
1c35c 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20  al;.  }. .#elif 
1c35d 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
1c35e 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  _) && defined(__
1c35f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  ppc__))..  __inl
1c360 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
1c361 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
1c362 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
1c363 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
1c364 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20  g retval;.      
1c365 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75  unsigned long ju
1c366 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  nk;.      __asm_
1c367 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
1c368 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31  "\n\.          1
1c369 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25  :      mftbu   %
1c36a 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
1c36b 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25         mftb    %
1c36c 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  L0\n\.          
1c36d 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20          mftbu   
1c36e 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %0\n\.          
1c36f 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20          cmpw    
1c370 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20  %0,%1\n\.       
1c371 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20             bne  
1c372 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20     1b".         
1c373 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20           : "=r" 
1c374 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28  (retval), "=r" (
1c375 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65  junk));.      re
1c376 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d  turn retval;.  }
1c377 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f  ..#else..  #erro
1c378 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74  r Need implement
1c379 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
1c37a 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75  Hwtime() for you
1c37b 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f  r platform...  /
1c37c 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c  *.  ** To compil
1c37d 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d  e without implem
1c37e 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77  enting sqlite3Hw
1c37f 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
1c380 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79  platform,.  ** y
1c381 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68  ou can remove th
1c382 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61  e above #error a
1c383 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  nd use the follo
1c384 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66  wing.  ** stub f
1c385 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69  unction.  You wi
1c386 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73  ll lose timing s
1c387 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a  upport for many.
1c388 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75    ** of the debu
1c389 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e  gging and testin
1c38a 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74  g utilities, but
1c38b 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20   it should at.  
1c38c 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65  ** least compile
1c38d 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53   and run..  */.S
1c38e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
1c38f 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
1c390 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
1c391 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69  ){ return ((sqli
1c392 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a  te_uint64)0); }.
1c393 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
1c394 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54  /* !defined(_HWT
1c395 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  IME_H_) */../***
1c396 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1c397 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a  of hwtime.h ****
1c398 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c399 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c39a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1c39b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
1c39c 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
1c39d 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65  left off in vdbe
1c39e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1c39f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e  **********/..#en
1c3a0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43  dif../*.** The C
1c3a1 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
1c3a2 50 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  PT macro defined
1c3a3 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73   here looks to s
1c3a4 65 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c  ee if the.** sql
1c3a5 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
1c3a6 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65   routine has bee
1c3a7 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74  n called.  If it
1c3a8 20 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a   has been, then.
1c3a9 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66  ** processing of
1c3aa 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
1c3ab 6d 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  m is interrupted
1c3ac 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63  ..**.** This mac
1c3ad 72 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65 72  ro added to ever
1c3ae 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  y instruction th
1c3af 61 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69  at does a jump i
1c3b0 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d  n order to.** im
1c3b1 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20  plement a loop. 
1c3b2 20 54 68 69 73 20 74 65 73 74 20 75 73 65 64 20   This test used 
1c3b3 74 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73  to be on every s
1c3b4 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f  ingle instructio
1c3b5 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d  n,.** but that m
1c3b6 65 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73  eant we more tes
1c3b7 74 69 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65  ting that we nee
1c3b8 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65  ded.  By only te
1c3b9 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61  sting the.** fla
1c3ba 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75  g on jump instru
1c3bb 63 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61  ctions, we get a
1c3bc 20 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69   (small) speed i
1c3bd 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23  mprovement..*/.#
1c3be 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52  define CHECK_FOR
1c3bf 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20  _INTERRUPT \.   
1c3c0 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  if( db->u1.isInt
1c3c1 65 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20  errupted ) goto 
1c3c2 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74  abort_due_to_int
1c3c3 65 72 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20  errupt;..#ifdef 
1c3c4 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61  SQLITE_DEBUG.sta
1c3c5 74 69 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73  tic int fileExis
1c3c6 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ts(sqlite3 *db, 
1c3c7 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
1c3c8 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20  e){.  int res = 
1c3c9 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  0;.  int rc = SQ
1c3ca 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20  LITE_OK;.#ifdef 
1c3cb 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a  SQLITE_TEST.  /*
1c3cc 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72 65   If we are curre
1c3cd 6e 74 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20  ntly testing IO 
1c3ce 65 72 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20  errors, then do 
1c3cf 6e 6f 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73  not call OsAcces
1c3d0 73 28 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74  s() to.  ** test
1c3d1 20 66 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63   for the presenc
1c3d2 65 20 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73  e of zFile. This
1c3d3 20 69 73 20 62 65 63 61 75 73 65 20 61 6e 79 20   is because any 
1c3d4 49 4f 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20  IO error that.  
1c3d5 2a 2a 20 6f 63 63 75 72 73 20 68 65 72 65 20 77  ** occurs here w
1c3d6 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72  ill not be repor
1c3d7 74 65 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65  ted, causing the
1c3d8 20 74 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20   test to fail.. 
1c3d9 20 2a 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74   */.  extern int
1c3da 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
1c3db 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28  r_pending;.  if(
1c3dc 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
1c3dd 72 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23  r_pending<=0 ).#
1c3de 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73  endif.    rc = s
1c3df 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 64  qlite3OsAccess(d
1c3e0 62 2d 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20  b->pVfs, zFile, 
1c3e1 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
1c3e2 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72  ISTS, &res);.  r
1c3e3 65 74 75 72 6e 20 28 72 65 73 20 26 26 20 72 63  eturn (res && rc
1c3e4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a  ==SQLITE_OK);.}.
1c3e5 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
1c3e6 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69  NDEBUG./*.** Thi
1c3e7 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e  s function is on
1c3e8 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  ly called from w
1c3e9 69 74 68 69 6e 20 61 6e 20 61 73 73 65 72 74 28  ithin an assert(
1c3ea 29 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 74  ) expression. It
1c3eb 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68 61 74 20  .** checks that 
1c3ec 74 68 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 61  the sqlite3.nTra
1c3ed 6e 73 61 63 74 69 6f 6e 20 76 61 72 69 61 62 6c  nsaction variabl
1c3ee 65 20 69 73 20 63 6f 72 72 65 63 74 6c 79 20 73  e is correctly s
1c3ef 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d  et to.** the num
1c3f0 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73  ber of non-trans
1c3f1 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74  action savepoint
1c3f2 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74  s currently in t
1c3f3 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69  he .** linked li
1c3f4 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 20 73  st starting at s
1c3f5 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e  qlite3.pSavepoin
1c3f6 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a  t..** .** Usage:
1c3f7 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 72  .**.**     asser
1c3f8 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e  t( checkSavepoin
1c3f9 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f  tCount(db) );.*/
1c3fa 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63  .static int chec
1c3fb 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28  kSavepointCount(
1c3fc 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
1c3fd 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76  int n = 0;.  Sav
1c3fe 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72  epoint *p;.  for
1c3ff 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e  (p=db->pSavepoin
1c400 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  t; p; p=p->pNext
1c401 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28  ) n++;.  assert(
1c402 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f   n==(db->nSavepo
1c403 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e  int + db->isTran
1c404 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74  sactionSavepoint
1c405 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ) );.  return 1;
1c406 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1c407 20 45 78 65 63 75 74 65 20 61 73 20 6d 75 63 68   Execute as much
1c408 20 6f 66 20 61 20 56 44 42 45 20 70 72 6f 67 72   of a VDBE progr
1c409 61 6d 20 61 73 20 77 65 20 63 61 6e 20 74 68 65  am as we can the
1c40a 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20  n return..**.** 
1c40b 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52  sqlite3VdbeMakeR
1c40c 65 61 64 79 28 29 20 6d 75 73 74 20 62 65 20 63  eady() must be c
1c40d 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68 69  alled before thi
1c40e 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64  s routine in ord
1c40f 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74  er to.** close t
1c410 68 65 20 70 72 6f 67 72 61 6d 20 77 69 74 68 20  he program with 
1c411 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20  a final OP_Halt 
1c412 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20 74 68  and to set up th
1c413 65 20 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61  e callbacks.** a
1c414 6e 64 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  nd the error mes
1c415 73 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  sage pointer..**
1c416 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 61 20 72  .** Whenever a r
1c417 6f 77 20 6f 72 20 72 65 73 75 6c 74 20 64 61 74  ow or result dat
1c418 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  a is available, 
1c419 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
1c41a 6c 20 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f  l either.** invo
1c41b 6b 65 20 74 68 65 20 72 65 73 75 6c 74 20 63 61  ke the result ca
1c41c 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 65  llback (if there
1c41d 20 69 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 75   is one) or retu
1c41e 72 6e 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54  rn with.** SQLIT
1c41f 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  E_ROW..**.** If 
1c420 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  an attempt is ma
1c421 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63  de to open a loc
1c422 6b 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 68  ked database, th
1c423 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
1c424 2a 2a 20 77 69 6c 6c 20 65 69 74 68 65 72 20 69  ** will either i
1c425 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63  nvoke the busy c
1c426 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72  allback (if ther
1c427 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 20  e is one) or it 
1c428 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53  will.** return S
1c429 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a  QLITE_BUSY..**.*
1c42a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
1c42b 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d  curs, an error m
1c42c 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65  essage is writte
1c42d 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  n to memory obta
1c42e 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c  ined.** from sql
1c42f 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e  ite3_malloc() an
1c430 64 20 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20  d p->zErrMsg is 
1c431 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f  made to point to
1c432 20 74 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a   that memory..**
1c433 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   The error code 
1c434 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e  is stored in p->
1c435 72 63 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  rc and this rout
1c436 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ine returns SQLI
1c437 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20  TE_ERROR..**.** 
1c438 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  If the callback 
1c439 65 76 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e  ever returns non
1c43a 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20  -zero, then the 
1c43b 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a  program exits.**
1c43c 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 54   immediately.  T
1c43d 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20  here will be no 
1c43e 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75  error message bu
1c43f 74 20 74 68 65 20 70 2d 3e 72 63 20 66 69 65 6c  t the p->rc fiel
1c440 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53  d is.** set to S
1c441 51 4c 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 20  QLITE_ABORT and 
1c442 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
1c443 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  l return SQLITE_
1c444 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d  ERROR..**.** A m
1c445 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
1c446 20 65 72 72 6f 72 20 63 61 75 73 65 73 20 70 2d   error causes p-
1c447 3e 72 63 20 74 6f 20 62 65 20 73 65 74 20 74 6f  >rc to be set to
1c448 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e   SQLITE_NOMEM an
1c449 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  d this.** routin
1c44a 65 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49  e to return SQLI
1c44b 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20  TE_ERROR..**.** 
1c44c 4f 74 68 65 72 20 66 61 74 61 6c 20 65 72 72 6f  Other fatal erro
1c44d 72 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  rs return SQLITE
1c44e 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66  _ERROR..**.** Af
1c44f 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
1c450 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 73   has finished, s
1c451 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
1c452 7a 65 28 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a  ze() should be.*
1c453 2a 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20  * used to clean 
1c454 75 70 20 74 68 65 20 6d 65 73 73 20 74 68 61 74  up the mess that
1c455 20 77 61 73 20 6c 65 66 74 20 62 65 68 69 6e 64   was left behind
1c456 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1c457 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1c458 64 62 65 45 78 65 63 28 0a 20 20 56 64 62 65 20  dbeExec(.  Vdbe 
1c459 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *p              
1c45a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
1c45b 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63  E */.){.  int pc
1c45c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1c45d 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67       /* The prog
1c45e 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  ram counter */. 
1c45f 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f   Op *aOp = p->aO
1c460 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  p;          /* C
1c461 6f 70 79 20 6f 66 20 70 2d 3e 61 4f 70 20 2a 2f  opy of p->aOp */
1c462 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 20 20  .  Op *pOp;     
1c463 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c464 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69   Current operati
1c465 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  on */.  int rc =
1c466 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
1c467 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72     /* Value to r
1c468 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  eturn */.  sqlit
1c469 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20  e3 *db = p->db; 
1c46a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
1c46b 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 72 65  abase */.  u8 re
1c46c 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74  setSchemaOnFault
1c46d 20 3d 20 30 3b 20 2f 2a 20 52 65 73 65 74 20 73   = 0; /* Reset s
1c46e 63 68 65 6d 61 20 61 66 74 65 72 20 61 6e 20 65  chema after an e
1c46f 72 72 6f 72 20 69 66 20 74 72 75 65 20 2a 2f 0a  rror if true */.
1c470 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20    u8 encoding = 
1c471 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a 20  ENC(db);     /* 
1c472 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63  The database enc
1c473 6f 64 69 6e 67 20 2a 2f 0a 23 69 66 6e 64 65 66  oding */.#ifndef
1c474 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f   SQLITE_OMIT_PRO
1c475 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20  GRESS_CALLBACK. 
1c476 20 75 38 20 63 68 65 63 6b 50 72 6f 67 72 65 73   u8 checkProgres
1c477 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  s;          /* T
1c478 72 75 65 20 69 66 20 70 72 6f 67 72 65 73 73 20  rue if progress 
1c479 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 65 6e  callbacks are en
1c47a 61 62 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  abled */.  int n
1c47b 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b  ProgressOps = 0;
1c47c 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73        /* Opcodes
1c47d 20 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20   executed since 
1c47e 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
1c47f 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 4d  k. */.#endif.  M
1c480 65 6d 20 2a 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d  em *aMem = p->aM
1c481 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70  em;       /* Cop
1c482 79 20 6f 66 20 70 2d 3e 61 4d 65 6d 20 2a 2f 0a  y of p->aMem */.
1c483 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b    Mem *pIn1 = 0;
1c484 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c485 31 73 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e  1st input operan
1c486 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32  d */.  Mem *pIn2
1c487 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1c488 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f    /* 2nd input o
1c489 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20  perand */.  Mem 
1c48a 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20  *pIn3 = 0;      
1c48b 20 20 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e         /* 3rd in
1c48c 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  put operand */. 
1c48d 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20   Mem *pOut = 0; 
1c48e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1c48f 75 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f  utput operand */
1c490 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20  .  int iCompare 
1c491 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
1c492 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20   Result of last 
1c493 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61  OP_Compare opera
1c494 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  tion */.  int *a
1c495 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20  Permute = 0;    
1c496 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74       /* Permutat
1c497 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66  ion of columns f
1c498 6f 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f  or OP_Compare */
1c499 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
1c49a 46 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74  FILE.  u64 start
1c49b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1c49c 20 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63    /* CPU clock c
1c49d 6f 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66  ount at start of
1c49e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74   opcode */.  int
1c49f 20 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20   origPc;        
1c4a0 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72          /* Progr
1c4a1 61 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74  am counter at st
1c4a2 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f  art of opcode */
1c4a3 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a  .#endif.  /*****
1c4a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1c4a8 20 20 2a 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c    ** Automatical
1c4a9 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  ly generated cod
1c4aa 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  e.  **.  ** The 
1c4ab 66 6f 6c 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20  following union 
1c4ac 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
1c4ad 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68   generated by th
1c4ae 65 0a 20 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70  e.  ** vdbe-comp
1c4af 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70 74 2e  ress.tcl script.
1c4b0 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66    The purpose of
1c4b1 20 74 68 69 73 20 75 6e 69 6f 6e 20 69 73 20 74   this union is t
1c4b2 6f 0a 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68  o.  ** reduce th
1c4b3 65 20 61 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63  e amount of stac
1c4b4 6b 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64  k space required
1c4b5 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
1c4b6 6e 2e 0a 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d  n..  ** See comm
1c4b7 65 6e 74 73 20 69 6e 20 74 68 65 20 76 64 62 65  ents in the vdbe
1c4b8 2d 63 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63  -compress.tcl sc
1c4b9 72 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73  ript for details
1c4ba 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76  ..  */.  union v
1c4bb 64 62 65 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20  dbeExecUnion {. 
1c4bc 20 20 20 73 74 72 75 63 74 20 4f 50 5f 59 69 65     struct OP_Yie
1c4bd 6c 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ld_stack_vars {.
1c4be 20 20 20 20 20 20 69 6e 74 20 70 63 44 65 73 74        int pcDest
1c4bf 3b 0a 20 20 20 20 7d 20 61 61 3b 0a 20 20 20 20  ;.    } aa;.    
1c4c0 73 74 72 75 63 74 20 4f 50 5f 56 61 72 69 61 62  struct OP_Variab
1c4c1 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  le_stack_vars {.
1c4c2 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20        int p1;   
1c4c3 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62         /* Variab
1c4c4 6c 65 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20  le to copy from 
1c4c5 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b  */.      int p2;
1c4c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1c4c7 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f  ister to copy to
1c4c8 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b   */.      int n;
1c4c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1c4ca 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c  mber of values l
1c4cb 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20  eft to copy */. 
1c4cc 20 20 20 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20       Mem *pVar; 
1c4cd 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62        /* Value b
1c4ce 65 69 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64  eing transferred
1c4cf 20 2a 2f 0a 20 20 20 20 7d 20 61 62 3b 0a 20 20   */.    } ab;.  
1c4d0 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 6f 76 65    struct OP_Move
1c4d1 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c4d2 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f      char *zMallo
1c4d3 63 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20  c;   /* Holding 
1c4d4 76 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c  variable for all
1c4d5 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f  ocated memory */
1c4d6 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20  .      int n;   
1c4d7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1c4d8 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6c  r of registers l
1c4d9 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20  eft to copy */. 
1c4da 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20       int p1;    
1c4db 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
1c4dc 72 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a  r to copy from *
1c4dd 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20  /.      int p2; 
1c4de 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
1c4df 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20  ster to copy to 
1c4e0 2a 2f 0a 20 20 20 20 7d 20 61 63 3b 0a 20 20 20  */.    } ac;.   
1c4e1 20 73 74 72 75 63 74 20 4f 50 5f 52 65 73 75 6c   struct OP_Resul
1c4e2 74 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 20  tRow_stack_vars 
1c4e3 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65  {.      Mem *pMe
1c4e4 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  m;.      int i;.
1c4e5 20 20 20 20 7d 20 61 64 3b 0a 20 20 20 20 73 74      } ad;.    st
1c4e6 72 75 63 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73  ruct OP_Concat_s
1c4e7 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c4e8 20 20 69 36 34 20 6e 42 79 74 65 3b 0a 20 20 20    i64 nByte;.   
1c4e9 20 7d 20 61 65 3b 0a 20 20 20 20 73 74 72 75 63   } ae;.    struc
1c4ea 74 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 5f 73  t OP_Remainder_s
1c4eb 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c4ec 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20    int flags;    
1c4ed 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45    /* Combined ME
1c4ee 4d 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62  M_* flags from b
1c4ef 6f 74 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20  oth inputs */.  
1c4f0 20 20 20 20 69 36 34 20 69 41 3b 20 20 20 20 20      i64 iA;     
1c4f1 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76      /* Integer v
1c4f2 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65  alue of left ope
1c4f3 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 36  rand */.      i6
1c4f4 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  4 iB;         /*
1c4f5 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f   Integer value o
1c4f6 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  f right operand 
1c4f7 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  */.      double 
1c4f8 72 41 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c  rA;      /* Real
1c4f9 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f   value of left o
1c4fa 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20  perand */.      
1c4fb 64 6f 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20  double rB;      
1c4fc 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66  /* Real value of
1c4fd 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   right operand *
1c4fe 2f 0a 20 20 20 20 7d 20 61 66 3b 0a 20 20 20 20  /.    } af;.    
1c4ff 73 74 72 75 63 74 20 4f 50 5f 46 75 6e 63 74 69  struct OP_Functi
1c500 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  on_stack_vars {.
1c501 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
1c502 20 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20     Mem *pArg;.  
1c503 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74      sqlite3_cont
1c504 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20 20 73  ext ctx;.      s
1c505 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
1c506 70 56 61 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20  pVal;.      int 
1c507 6e 3b 0a 20 20 20 20 7d 20 61 67 3b 0a 20 20 20  n;.    } ag;.   
1c508 20 73 74 72 75 63 74 20 4f 50 5f 53 68 69 66 74   struct OP_Shift
1c509 52 69 67 68 74 5f 73 74 61 63 6b 5f 76 61 72 73  Right_stack_vars
1c50a 20 7b 0a 20 20 20 20 20 20 69 36 34 20 61 3b 0a   {.      i64 a;.
1c50b 20 20 20 20 20 20 69 36 34 20 62 3b 0a 20 20 20        i64 b;.   
1c50c 20 7d 20 61 68 3b 0a 20 20 20 20 73 74 72 75 63   } ah;.    struc
1c50d 74 20 4f 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61  t OP_Ge_stack_va
1c50e 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  rs {.      int r
1c50f 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  es;            /
1c510 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20  * Result of the 
1c511 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49  comparison of pI
1c512 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20  n1 against pIn3 
1c513 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 61 66  */.      char af
1c514 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20  finity;      /* 
1c515 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20  Affinity to use 
1c516 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a  for comparison *
1c517 2f 0a 20 20 20 20 7d 20 61 69 3b 0a 20 20 20 20  /.    } ai;.    
1c518 73 74 72 75 63 74 20 4f 50 5f 43 6f 6d 70 61 72  struct OP_Compar
1c519 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  e_stack_vars {. 
1c51a 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
1c51b 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69    int i;.      i
1c51c 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74  nt p1;.      int
1c51d 20 70 32 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74   p2;.      const
1c51e 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
1c51f 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64  fo;.      int id
1c520 78 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  x;.      CollSeq
1c521 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43   *pColl;    /* C
1c522 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1c523 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73  e to use on this
1c524 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 69   term */.      i
1c525 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20  nt bRev;        
1c526 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45    /* True for DE
1c527 53 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72  SCENDING sort or
1c528 64 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b  der */.    } aj;
1c529 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f  .    struct OP_O
1c52a 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  r_stack_vars {. 
1c52b 20 20 20 20 20 69 6e 74 20 76 31 3b 20 20 20 20       int v1;    
1c52c 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a  /* Left operand:
1c52d 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54    0==FALSE, 1==T
1c52e 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20  RUE, 2==UNKNOWN 
1c52f 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20  or NULL */.     
1c530 20 69 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52   int v2;    /* R
1c531 69 67 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d  ight operand: 0=
1c532 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c  =FALSE, 1==TRUE,
1c533 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e   2==UNKNOWN or N
1c534 55 4c 4c 20 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b  ULL */.    } ak;
1c535 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c536 66 4e 6f 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  fNot_stack_vars 
1c537 7b 0a 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20  {.      int c;. 
1c538 20 20 20 7d 20 61 6c 3b 0a 20 20 20 20 73 74 72     } al;.    str
1c539 75 63 74 20 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74  uct OP_Column_st
1c53a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c53b 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65   u32 payloadSize
1c53c 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ;   /* Number of
1c53d 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65   bytes in the re
1c53e 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 36  cord */.      i6
1c53f 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b  4 payloadSize64;
1c540 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1c541 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
1c542 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70  d */.      int p
1c543 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  1;            /*
1c544 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65   P1 value of the
1c545 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20   opcode */.     
1c546 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20   int p2;        
1c547 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75      /* column nu
1c548 6d 62 65 72 20 74 6f 20 72 65 74 72 69 65 76 65  mber to retrieve
1c549 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75   */.      VdbeCu
1c54a 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20  rsor *pC;    /* 
1c54b 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
1c54c 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  */.      char *z
1c54d 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  Rec;        /* P
1c54e 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65  ointer to comple
1c54f 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a  te record-data *
1c550 2f 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  /.      BtCursor
1c551 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68   *pCrsr;   /* Th
1c552 65 20 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a  e BTree cursor *
1c553 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61 54 79  /.      u32 *aTy
1c554 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54  pe;        /* aT
1c555 79 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65  ype[i] holds the
1c556 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66   numeric type of
1c557 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e   the i-th column
1c558 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61   */.      u32 *a
1c559 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  Offset;      /* 
1c55a 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66  aOffset[i] is of
1c55b 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66  fset to start of
1c55c 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63   data for i-th c
1c55d 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69  olumn */.      i
1c55e 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
1c55f 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66    /* number of f
1c560 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63  ields in the rec
1c561 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ord */.      int
1c562 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20   len;           
1c563 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66  /* The length of
1c564 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20   the serialized 
1c565 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c  data for the col
1c566 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  umn */.      int
1c567 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1c568 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1c569 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  */.      char *z
1c56a 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50  Data;       /* P
1c56b 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72  art of the recor
1c56c 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20  d being decoded 
1c56d 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44  */.      Mem *pD
1c56e 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57  est;        /* W
1c56f 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68  here to write th
1c570 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75  e extracted valu
1c571 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 73  e */.      Mem s
1c572 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Mem;          /*
1c573 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65   For storing the
1c574 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65   record being de
1c575 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 75  coded */.      u
1c576 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20  8 *zIdx;        
1c577 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20    /* Index into 
1c578 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20  header */.      
1c579 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20  u8 *zEndHdr;    
1c57a 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1c57b 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65   first byte afte
1c57c 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  r the header */.
1c57d 20 20 20 20 20 20 75 33 32 20 6f 66 66 73 65 74        u32 offset
1c57e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73  ;        /* Offs
1c57f 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  et into the data
1c580 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6f 66   */.      u64 of
1c581 66 73 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20  fset64;      /* 
1c582 36 34 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20  64-bit offset.  
1c583 36 34 20 62 69 74 73 20 6e 65 65 64 65 64 20 74  64 bits needed t
1c584 6f 20 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77  o catch overflow
1c585 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a   */.      int sz
1c586 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Hdr;         /* 
1c587 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64  Size of the head
1c588 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74  er size field at
1c589 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64   start of record
1c58a 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 76   */.      int av
1c58b 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ail;         /* 
1c58c 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1c58d 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74  of available dat
1c58e 61 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a  a */.      Mem *
1c58f 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  pReg;         /*
1c590 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70   PseudoTable inp
1c591 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  ut register */. 
1c592 20 20 20 7d 20 61 6d 3b 0a 20 20 20 20 73 74 72     } am;.    str
1c593 75 63 74 20 4f 50 5f 41 66 66 69 6e 69 74 79 5f  uct OP_Affinity_
1c594 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c595 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1c596 41 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54  Affinity;   /* T
1c597 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62  he affinity to b
1c598 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 20  e applied */.   
1c599 20 20 20 63 68 61 72 20 63 41 66 66 3b 20 20 20     char cAff;   
1c59a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
1c59b 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65   single characte
1c59c 72 20 6f 66 20 61 66 66 69 6e 69 74 79 20 2a 2f  r of affinity */
1c59d 0a 20 20 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73  .    } an;.    s
1c59e 74 72 75 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63  truct OP_MakeRec
1c59f 6f 72 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ord_stack_vars {
1c5a0 0a 20 20 20 20 20 20 75 38 20 2a 7a 4e 65 77 52  .      u8 *zNewR
1c5a1 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a  ecord;        /*
1c5a2 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c   A buffer to hol
1c5a3 64 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  d the data for t
1c5a4 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f  he new record */
1c5a5 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63  .      Mem *pRec
1c5a6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c5a7 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   The new record 
1c5a8 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61  */.      u64 nDa
1c5a9 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
1c5aa 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1c5ab 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65  es of data space
1c5ac 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 48   */.      int nH
1c5ad 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
1c5ae 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1c5af 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70  tes of header sp
1c5b0 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 36 34  ace */.      i64
1c5b1 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
1c5b2 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63      /* Data spac
1c5b3 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74  e required for t
1c5b4 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  his record */.  
1c5b5 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20      int nZero;  
1c5b6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1c5b7 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74  mber of zero byt
1c5b8 65 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  es at the end of
1c5b9 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1c5ba 20 20 20 20 20 69 6e 74 20 6e 56 61 72 69 6e 74       int nVarint
1c5bb 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1c5bc 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1c5bd 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20  n a varint */.  
1c5be 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74      u32 serial_t
1c5bf 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79  ype;       /* Ty
1c5c0 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20  pe field */.    
1c5c1 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20    Mem *pData0;  
1c5c2 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
1c5c3 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f  t field to be co
1c5c4 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20  mbined into the 
1c5c5 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20  record */.      
1c5c6 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20  Mem *pLast;     
1c5c7 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66         /* Last f
1c5c8 69 65 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f  ield of the reco
1c5c9 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  rd */.      int 
1c5ca 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20  nField;         
1c5cb 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c5cc 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65  fields in the re
1c5cd 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 63 68  cord */.      ch
1c5ce 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20  ar *zAffinity;  
1c5cf 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69       /* The affi
1c5d0 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20  nity string for 
1c5d1 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1c5d2 20 20 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72      int file_for
1c5d3 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  mat;       /* Fi
1c5d4 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65  le format to use
1c5d5 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f   for encoding */
1c5d6 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20  .      int i;   
1c5d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c5d8 20 53 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a   Space used in z
1c5d9 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20  NewRecord[] */. 
1c5da 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20       int len;   
1c5db 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1c5dc 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64  ength of a field
1c5dd 20 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20   */.    } ao;.  
1c5de 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e    struct OP_Coun
1c5df 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c5e0 20 20 20 20 20 69 36 34 20 6e 45 6e 74 72 79 3b       i64 nEntry;
1c5e1 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20  .      BtCursor 
1c5e2 2a 70 43 72 73 72 3b 0a 20 20 20 20 7d 20 61 70  *pCrsr;.    } ap
1c5e3 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c5e4 53 61 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f  Savepoint_stack_
1c5e5 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c5e6 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   p1;            
1c5e7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c5e8 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72  Value of P1 oper
1c5e9 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61  and */.      cha
1c5ea 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
1c5eb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c5ec 4e 61 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e  Name of savepoin
1c5ed 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  t */.      int n
1c5ee 4e 61 6d 65 3b 0a 20 20 20 20 20 20 53 61 76 65  Name;.      Save
1c5ef 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20  point *pNew;.   
1c5f0 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53     Savepoint *pS
1c5f1 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20  avepoint;.      
1c5f2 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b  Savepoint *pTmp;
1c5f3 0a 20 20 20 20 20 20 69 6e 74 20 69 53 61 76 65  .      int iSave
1c5f4 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74  point;.      int
1c5f5 20 69 69 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20   ii;.    } aq;. 
1c5f6 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 75 74     struct OP_Aut
1c5f7 6f 43 6f 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61  oCommit_stack_va
1c5f8 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  rs {.      int d
1c5f9 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1c5fa 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6c  ;.      int iRol
1c5fb 6c 62 61 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74  lback;.      int
1c5fc 20 74 75 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d   turnOnAC;.    }
1c5fd 20 61 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ar;.    struct 
1c5fe 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73  OP_Transaction_s
1c5ff 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c600 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20    Btree *pBt;.  
1c601 20 20 7d 20 61 73 3b 0a 20 20 20 20 73 74 72 75    } as;.    stru
1c602 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  ct OP_ReadCookie
1c603 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c604 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20      int iMeta;. 
1c605 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20       int iDb;.  
1c606 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b      int iCookie;
1c607 0a 20 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73  .    } at;.    s
1c608 74 72 75 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b  truct OP_SetCook
1c609 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ie_stack_vars {.
1c60a 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20        Db *pDb;. 
1c60b 20 20 20 7d 20 61 75 3b 0a 20 20 20 20 73 74 72     } au;.    str
1c60c 75 63 74 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f  uct OP_VerifyCoo
1c60d 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  kie_stack_vars {
1c60e 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61  .      int iMeta
1c60f 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  ;.      Btree *p
1c610 42 74 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20  Bt;.    } av;.  
1c611 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e    struct OP_Open
1c612 57 72 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73  Write_stack_vars
1c613 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69   {.      int nFi
1c614 65 6c 64 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e  eld;.      KeyIn
1c615 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
1c616 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20      int p2;.    
1c617 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
1c618 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 20   int wrFlag;.   
1c619 20 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20     Btree *pX;.  
1c61a 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c61b 70 43 75 72 3b 0a 20 20 20 20 20 20 44 62 20 2a  pCur;.      Db *
1c61c 70 44 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20  pDb;.    } aw;. 
1c61d 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65     struct OP_Ope
1c61e 6e 45 70 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b  nEphemeral_stack
1c61f 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c620 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20  beCursor *pCx;. 
1c621 20 20 20 7d 20 61 78 3b 0a 20 20 20 20 73 74 72     } ax;.    str
1c622 75 63 74 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64  uct OP_OpenPseud
1c623 6f 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  o_stack_vars {. 
1c624 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c625 2a 70 43 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a  *pCx;.    } ay;.
1c626 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65      struct OP_Se
1c627 65 6b 47 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  ekGt_stack_vars 
1c628 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  {.      int res;
1c629 0a 20 20 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20  .      int oc;. 
1c62a 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c62b 2a 70 43 3b 0a 20 20 20 20 20 20 55 6e 70 61 63  *pC;.      Unpac
1c62c 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20  kedRecord r;.   
1c62d 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20     int nField;. 
1c62e 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20       i64 iKey;  
1c62f 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64      /* The rowid
1c630 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20   we are to seek 
1c631 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a  to */.    } az;.
1c632 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65      struct OP_Se
1c633 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ek_stack_vars {.
1c634 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c635 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a   *pC;.    } ba;.
1c636 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 46 6f      struct OP_Fo
1c637 75 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  und_stack_vars {
1c638 0a 20 20 20 20 20 20 69 6e 74 20 61 6c 72 65 61  .      int alrea
1c639 64 79 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20  dyExists;.      
1c63a 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c63b 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c63c 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63       UnpackedRec
1c63d 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20  ord *pIdxKey;.  
1c63e 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f      UnpackedReco
1c63f 72 64 20 72 3b 0a 20 20 20 20 20 20 63 68 61 72  rd r;.      char
1c640 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38   aTempRec[ROUND8
1c641 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64  (sizeof(Unpacked
1c642 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f  Record)) + sizeo
1c643 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20  f(Mem)*3 + 7];. 
1c644 20 20 20 7d 20 62 62 3b 0a 20 20 20 20 73 74 72     } bb;.    str
1c645 75 63 74 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f  uct OP_IsUnique_
1c646 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c647 20 20 20 75 31 36 20 69 69 3b 0a 20 20 20 20 20     u16 ii;.     
1c648 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1c649 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  ;.      BtCursor
1c64a 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 75   *pCrsr;.      u
1c64b 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20  16 nField;.     
1c64c 20 4d 65 6d 20 2a 61 4d 78 3b 0a 20 20 20 20 20   Mem *aMx;.     
1c64d 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1c64e 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1c64f 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e      /* B-Tree in
1c650 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a  dex search key *
1c651 2f 0a 20 20 20 20 20 20 69 36 34 20 52 3b 20 20  /.      i64 R;  
1c652 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c653 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
1c654 77 69 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65  wid stored in re
1c655 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 20  gister P3 */.   
1c656 20 7d 20 62 63 3b 0a 20 20 20 20 73 74 72 75 63   } bc;.    struc
1c657 74 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73  t OP_NotExists_s
1c658 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c659 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c65a 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  ;.      BtCursor
1c65b 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69   *pCrsr;.      i
1c65c 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 75 36  nt res;.      u6
1c65d 34 20 69 4b 65 79 3b 0a 20 20 20 20 7d 20 62 64  4 iKey;.    } bd
1c65e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c65f 4e 65 77 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76  NewRowid_stack_v
1c660 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20  ars {.      i64 
1c661 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
1c662 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f     /* The new ro
1c663 77 69 64 20 2a 2f 0a 20 20 20 20 20 20 56 64 62  wid */.      Vdb
1c664 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20  eCursor *pC;    
1c665 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66      /* Cursor of
1c666 20 74 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68   table to get th
1c667 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20  e new rowid */. 
1c668 20 20 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20       int res;   
1c669 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1c66a 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69  esult of an sqli
1c66b 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a  te3BtreeLast() *
1c66c 2f 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 3b  /.      int cnt;
1c66d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c66e 2a 20 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d  * Counter to lim
1c66f 69 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  it the number of
1c670 20 73 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 20   searches */.   
1c671 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20     Mem *pMem;   
1c672 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1c673 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61  ister holding la
1c674 72 67 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20  rgest rowid for 
1c675 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f  AUTOINCREMENT */
1c676 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 6d 65  .      VdbeFrame
1c677 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a   *pFrame;     /*
1c678 20 52 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 56   Root frame of V
1c679 44 42 45 20 2a 2f 0a 20 20 20 20 7d 20 62 65 3b  DBE */.    } be;
1c67a 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c67b 6e 73 65 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76  nsertInt_stack_v
1c67c 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20  ars {.      Mem 
1c67d 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a  *pData;       /*
1c67e 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e   MEM cell holdin
1c67f 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72  g data for the r
1c680 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65  ecord to be inse
1c681 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65  rted */.      Me
1c682 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20  m *pKey;        
1c683 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64  /* MEM cell hold
1c684 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74 68 65  ing key  for the
1c685 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
1c686 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20   i64 iKey;      
1c687 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65     /* The intege
1c688 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66  r ROWID or key f
1c689 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  or the record to
1c68a 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
1c68b 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c68c 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f   *pC;   /* Curso
1c68d 72 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20  r to table into 
1c68e 77 68 69 63 68 20 69 6e 73 65 72 74 20 69 73 20  which insert is 
1c68f 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20  written */.     
1c690 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20   int nZero;     
1c691 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c692 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70  zero-bytes to ap
1c693 70 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e  pend */.      in
1c694 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20  t seekResult;   
1c695 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69  /* Result of pri
1c696 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69 66 20  or seek or 0 if 
1c697 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54  no USESEEKRESULT
1c698 20 66 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20 63   flag */.      c
1c699 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20  onst char *zDb; 
1c69a 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d   /* database nam
1c69b 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20  e - used by the 
1c69c 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20  update hook */. 
1c69d 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1c69e 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20  *zTbl; /* Table 
1c69f 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74  name - used by t
1c6a0 68 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a  he opdate hook *
1c6a1 2f 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20  /.      int op; 
1c6a2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63            /* Opc
1c6a3 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68  ode for update h
1c6a4 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41  ook: SQLITE_UPDA
1c6a5 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53  TE or SQLITE_INS
1c6a6 45 52 54 20 2a 2f 0a 20 20 20 20 7d 20 62 66 3b  ERT */.    } bf;
1c6a7 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 44  .    struct OP_D
1c6a8 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 73  elete_stack_vars
1c6a9 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65   {.      i64 iKe
1c6aa 79 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72  y;.      VdbeCur
1c6ab 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62  sor *pC;.    } b
1c6ac 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  g;.    struct OP
1c6ad 5f 52 6f 77 44 61 74 61 5f 73 74 61 63 6b 5f 76  _RowData_stack_v
1c6ae 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
1c6af 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20  Cursor *pC;.    
1c6b0 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1c6b1 72 3b 0a 20 20 20 20 20 20 75 33 32 20 6e 3b 0a  r;.      u32 n;.
1c6b2 20 20 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20        i64 n64;. 
1c6b3 20 20 20 7d 20 62 68 3b 0a 20 20 20 20 73 74 72     } bh;.    str
1c6b4 75 63 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74 61  uct OP_Rowid_sta
1c6b5 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c6b6 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c6b7 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20        i64 v;.   
1c6b8 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
1c6b9 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f  *pVtab;.      co
1c6ba 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
1c6bb 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20  le *pModule;.   
1c6bc 20 7d 20 62 69 3b 0a 20 20 20 20 73 74 72 75 63   } bi;.    struc
1c6bd 74 20 4f 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61  t OP_NullRow_sta
1c6be 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c6bf 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c6c0 20 20 20 20 7d 20 62 6a 3b 0a 20 20 20 20 73 74      } bj;.    st
1c6c1 72 75 63 74 20 4f 50 5f 4c 61 73 74 5f 73 74 61  ruct OP_Last_sta
1c6c2 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c6c3 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c6c4 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a        BtCursor *
1c6c5 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74  pCrsr;.      int
1c6c6 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6b 3b 0a   res;.    } bk;.
1c6c7 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65      struct OP_Re
1c6c8 77 69 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20  wind_stack_vars 
1c6c9 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  {.      VdbeCurs
1c6ca 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74  or *pC;.      Bt
1c6cb 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1c6cc 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20       int res;.  
1c6cd 20 20 7d 20 62 6c 3b 0a 20 20 20 20 73 74 72 75    } bl;.    stru
1c6ce 63 74 20 4f 50 5f 4e 65 78 74 5f 73 74 61 63 6b  ct OP_Next_stack
1c6cf 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c6d0 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c6d1 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
1c6d2 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  rsr;.      int r
1c6d3 65 73 3b 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 20  es;.    } bm;.  
1c6d4 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 49    struct OP_IdxI
1c6d5 6e 73 65 72 74 5f 73 74 61 63 6b 5f 76 61 72 73  nsert_stack_vars
1c6d6 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c6d7 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42  sor *pC;.      B
1c6d8 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c6d9 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a        int nKey;.
1c6da 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1c6db 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 7d 20 62 6e   *zKey;.    } bn
1c6dc 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c6dd 49 64 78 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f  IdxDelete_stack_
1c6de 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62  vars {.      Vdb
1c6df 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c6e0 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72     BtCursor *pCr
1c6e1 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65  sr;.      int re
1c6e2 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65  s;.      Unpacke
1c6e3 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d  dRecord r;.    }
1c6e4 20 62 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bo;.    struct 
1c6e5 4f 50 5f 49 64 78 52 6f 77 69 64 5f 73 74 61 63  OP_IdxRowid_stac
1c6e6 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42  k_vars {.      B
1c6e7 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c6e8 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c6e9 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 36 34 20   *pC;.      i64 
1c6ea 72 6f 77 69 64 3b 0a 20 20 20 20 7d 20 62 70 3b  rowid;.    } bp;
1c6eb 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c6ec 64 78 47 45 5f 73 74 61 63 6b 5f 76 61 72 73 20  dxGE_stack_vars 
1c6ed 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  {.      VdbeCurs
1c6ee 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e  or *pC;.      in
1c6ef 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e 70  t res;.      Unp
1c6f0 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20  ackedRecord r;. 
1c6f1 20 20 20 7d 20 62 71 3b 0a 20 20 20 20 73 74 72     } bq;.    str
1c6f2 75 63 74 20 4f 50 5f 44 65 73 74 72 6f 79 5f 73  uct OP_Destroy_s
1c6f3 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c6f4 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20    int iMoved;.  
1c6f5 20 20 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20      int iCnt;.  
1c6f6 20 20 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b      Vdbe *pVdbe;
1c6f7 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a  .      int iDb;.
1c6f8 20 20 20 20 7d 20 62 72 3b 0a 20 20 20 20 73 74      } br;.    st
1c6f9 72 75 63 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74  ruct OP_Clear_st
1c6fa 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c6fb 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 20 20   int nChange;.  
1c6fc 20 20 7d 20 62 73 3b 0a 20 20 20 20 73 74 72 75    } bs;.    stru
1c6fd 63 74 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c  ct OP_CreateTabl
1c6fe 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  e_stack_vars {. 
1c6ff 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20       int pgno;. 
1c700 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a       int flags;.
1c701 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20        Db *pDb;. 
1c702 20 20 20 7d 20 62 74 3b 0a 20 20 20 20 73 74 72     } bt;.    str
1c703 75 63 74 20 4f 50 5f 50 61 72 73 65 53 63 68 65  uct OP_ParseSche
1c704 6d 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ma_stack_vars {.
1c705 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20        int iDb;. 
1c706 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1c707 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 20 20  *zMaster;.      
1c708 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20  char *zSql;.    
1c709 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44    InitData initD
1c70a 61 74 61 3b 0a 20 20 20 20 7d 20 62 75 3b 0a 20  ata;.    } bu;. 
1c70b 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 74     struct OP_Int
1c70c 65 67 72 69 74 79 43 6b 5f 73 74 61 63 6b 5f 76  egrityCk_stack_v
1c70d 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ars {.      int 
1c70e 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e  nRoot;      /* N
1c70f 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20  umber of tables 
1c710 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62  to check.  (Numb
1c711 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73  er of root pages
1c712 2e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  .) */.      int 
1c713 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41  *aRoot;     /* A
1c714 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65  rray of rootpage
1c715 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62   numbers for tab
1c716 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65  les to be checke
1c717 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a  d */.      int j
1c718 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ;          /* Lo
1c719 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1c71a 20 20 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20      int nErr;   
1c71b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1c71c 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64   errors reported
1c71d 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a   */.      char *
1c71e 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78  z;        /* Tex
1c71f 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72  t of the error r
1c720 65 70 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20 4d  eport */.      M
1c721 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f  em *pnErr;     /
1c722 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70 69  * Register keepi
1c723 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f  ng track of erro
1c724 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a  rs remaining */.
1c725 20 20 20 20 7d 20 62 76 3b 0a 20 20 20 20 73 74      } bv;.    st
1c726 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 52 65  ruct OP_RowSetRe
1c727 61 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ad_stack_vars {.
1c728 20 20 20 20 20 20 69 36 34 20 76 61 6c 3b 0a 20        i64 val;. 
1c729 20 20 20 7d 20 62 77 3b 0a 20 20 20 20 73 74 72     } bw;.    str
1c72a 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 54 65 73  uct OP_RowSetTes
1c72b 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c72c 20 20 20 20 20 69 6e 74 20 69 53 65 74 3b 0a 20       int iSet;. 
1c72d 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b       int exists;
1c72e 0a 20 20 20 20 7d 20 62 78 3b 0a 20 20 20 20 73  .    } bx;.    s
1c72f 74 72 75 63 74 20 4f 50 5f 50 72 6f 67 72 61 6d  truct OP_Program
1c730 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c731 20 20 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20      int nMem;   
1c732 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1c733 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
1c734 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 75  registers for su
1c735 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20  b-program */.   
1c736 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20     int nByte;   
1c737 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
1c738 74 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73  tes of runtime s
1c739 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f  pace required fo
1c73a 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f  r sub-program */
1c73b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b  .      Mem *pRt;
1c73c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c73d 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c  * Register to al
1c73e 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73  locate runtime s
1c73f 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65  pace */.      Me
1c740 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *pMem;        
1c741 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
1c742 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
1c743 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f   memory cells */
1c744 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64  .      Mem *pEnd
1c745 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1c746 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65  * Last memory ce
1c747 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20  ll in new array 
1c748 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 46 72 61  */.      VdbeFra
1c749 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20  me *pFrame;     
1c74a 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61   /* New vdbe fra
1c74b 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e  me to execute in
1c74c 20 2a 2f 0a 20 20 20 20 20 20 53 75 62 50 72 6f   */.      SubPro
1c74d 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20  gram *pProgram; 
1c74e 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d    /* Sub-program
1c74f 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20   to execute */. 
1c750 20 20 20 20 20 76 6f 69 64 20 2a 74 3b 20 20 20       void *t;   
1c751 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c752 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e  Token identifyin
1c753 67 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20  g trigger */.   
1c754 20 7d 20 62 79 3b 0a 20 20 20 20 73 74 72 75 63   } by;.    struc
1c755 74 20 4f 50 5f 50 61 72 61 6d 5f 73 74 61 63 6b  t OP_Param_stack
1c756 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c757 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
1c758 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e 3b  .      Mem *pIn;
1c759 0a 20 20 20 20 7d 20 62 7a 3b 0a 20 20 20 20 73  .    } bz;.    s
1c75a 74 72 75 63 74 20 4f 50 5f 4d 65 6d 4d 61 78 5f  truct OP_MemMax_
1c75b 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c75c 20 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20     Mem *pIn1;.  
1c75d 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70      VdbeFrame *p
1c75e 46 72 61 6d 65 3b 0a 20 20 20 20 7d 20 63 61 3b  Frame;.    } ca;
1c75f 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41  .    struct OP_A
1c760 67 67 53 74 65 70 5f 73 74 61 63 6b 5f 76 61 72  ggStep_stack_var
1c761 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b  s {.      int n;
1c762 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
1c763 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20      Mem *pMem;. 
1c764 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a       Mem *pRec;.
1c765 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
1c766 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20  ntext ctx;.     
1c767 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1c768 2a 61 70 56 61 6c 3b 0a 20 20 20 20 7d 20 63 62  *apVal;.    } cb
1c769 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c76a 41 67 67 46 69 6e 61 6c 5f 73 74 61 63 6b 5f 76  AggFinal_stack_v
1c76b 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20  ars {.      Mem 
1c76c 2a 70 4d 65 6d 3b 0a 20 20 20 20 7d 20 63 63 3b  *pMem;.    } cc;
1c76d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c76e 6e 63 72 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f  ncrVacuum_stack_
1c76f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72  vars {.      Btr
1c770 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63  ee *pBt;.    } c
1c771 64 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  d;.    struct OP
1c772 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61  _VBegin_stack_va
1c773 72 73 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c  rs {.      VTabl
1c774 65 20 2a 70 56 54 61 62 3b 0a 20 20 20 20 7d 20  e *pVTab;.    } 
1c775 63 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ce;.    struct O
1c776 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61  P_VOpen_stack_va
1c777 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43  rs {.      VdbeC
1c778 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20  ursor *pCur;.   
1c779 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f     sqlite3_vtab_
1c77a 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
1c77b 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  sor;.      sqlit
1c77c 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1c77d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f        sqlite3_mo
1c77e 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c77f 20 20 20 7d 20 63 66 3b 0a 20 20 20 20 73 74 72     } cf;.    str
1c780 75 63 74 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73  uct OP_VFilter_s
1c781 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c782 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20    int nArg;.    
1c783 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20    int iQuery;.  
1c784 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65      const sqlite
1c785 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
1c786 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 51  e;.      Mem *pQ
1c787 75 65 72 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20  uery;.      Mem 
1c788 2a 70 41 72 67 63 3b 0a 20 20 20 20 20 20 73 71  *pArgc;.      sq
1c789 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1c78a 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  r *pVtabCursor;.
1c78b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c78c 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c78d 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1c78e 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73  r;.      int res
1c78f 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  ;.      int i;. 
1c790 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67       Mem **apArg
1c791 3b 0a 20 20 20 20 7d 20 63 67 3b 0a 20 20 20 20  ;.    } cg;.    
1c792 73 74 72 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d  struct OP_VColum
1c793 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  n_stack_vars {. 
1c794 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
1c795 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20  b *pVtab;.      
1c796 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
1c797 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c798 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b       Mem *pDest;
1c799 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  .      sqlite3_c
1c79a 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b  ontext sContext;
1c79b 0a 20 20 20 20 7d 20 63 68 3b 0a 20 20 20 20 73  .    } ch;.    s
1c79c 74 72 75 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73  truct OP_VNext_s
1c79d 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c79e 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1c79f 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e  pVtab;.      con
1c7a0 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
1c7a1 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20  e *pModule;.    
1c7a2 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
1c7a3 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1c7a4 72 3b 0a 20 20 20 20 7d 20 63 69 3b 0a 20 20 20  r;.    } ci;.   
1c7a5 20 73 74 72 75 63 74 20 4f 50 5f 56 52 65 6e 61   struct OP_VRena
1c7a6 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  me_stack_vars {.
1c7a7 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c7a8 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c7a9 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20   Mem *pName;.   
1c7aa 20 7d 20 63 6a 3b 0a 20 20 20 20 73 74 72 75 63   } cj;.    struc
1c7ab 74 20 4f 50 5f 56 55 70 64 61 74 65 5f 73 74 61  t OP_VUpdate_sta
1c7ac 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c7ad 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1c7ae 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  tab;.      sqlit
1c7af 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1c7b0 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41  le;.      int nA
1c7b1 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  rg;.      int i;
1c7b2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e  .      sqlite_in
1c7b3 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 20  t64 rowid;.     
1c7b4 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20   Mem **apArg;.  
1c7b5 20 20 20 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20      Mem *pX;.   
1c7b6 20 7d 20 63 6b 3b 0a 20 20 20 20 73 74 72 75 63   } ck;.    struc
1c7b7 74 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 5f 73  t OP_Pagecount_s
1c7b8 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c7b9 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 20 20 20    int p1;.      
1c7ba 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20  int nPage;.     
1c7bb 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a   Pager *pPager;.
1c7bc 20 20 20 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74      } cl;.    st
1c7bd 72 75 63 74 20 4f 50 5f 54 72 61 63 65 5f 73 74  ruct OP_Trace_st
1c7be 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c7bf 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20   char *zTrace;. 
1c7c0 20 20 20 7d 20 63 6d 3b 0a 20 20 7d 20 75 3b 0a     } cm;.  } u;.
1c7c1 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74    /* End automat
1c7c2 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
1c7c3 20 63 6f 64 65 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a   code.  ********
1c7c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20  ************/.. 
1c7c8 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
1c7c9 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1c7ca 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33  N );  /* sqlite3
1c7cb 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 73  _step() verifies
1c7cc 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 72   this */.  asser
1c7cd 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  t( db->magic==SQ
1c7ce 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
1c7cf 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1c7d0 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28  MutexArrayEnter(
1c7d1 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d  p);.  if( p->rc=
1c7d2 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
1c7d3 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70  .    /* This hap
1c7d4 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63  pens if a malloc
1c7d5 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c  () inside a call
1c7d6 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
1c7d7 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20  mn_text() or.   
1c7d8 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   ** sqlite3_colu
1c7d9 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c  mn_text16() fail
1c7da 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f  ed.  */.    goto
1c7db 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61   no_mem;.  }.  a
1c7dc 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51  ssert( p->rc==SQ
1c7dd 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63  LITE_OK || p->rc
1c7de 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b  ==SQLITE_BUSY );
1c7df 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54  .  p->rc = SQLIT
1c7e0 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
1c7e1 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b  p->explain==0 );
1c7e2 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74  .  p->pResultSet
1c7e3 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79   = 0;.  db->busy
1c7e4 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20  Handler.nBusy = 
1c7e5 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49  0;.  CHECK_FOR_I
1c7e6 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69  NTERRUPT;.  sqli
1c7e7 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
1c7e8 6c 28 70 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  l(p);.#ifndef SQ
1c7e9 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45  LITE_OMIT_PROGRE
1c7ea 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 63 68  SS_CALLBACK.  ch
1c7eb 65 63 6b 50 72 6f 67 72 65 73 73 20 3d 20 64 62  eckProgress = db
1c7ec 2d 3e 78 50 72 6f 67 72 65 73 73 21 3d 30 3b 0a  ->xProgress!=0;.
1c7ed 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
1c7ee 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 6c  LITE_DEBUG.  sql
1c7ef 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
1c7f0 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 70  alloc();.  if( p
1c7f1 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 28  ->pc==0 .   && (
1c7f2 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  (p->db->flags & 
1c7f3 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69  SQLITE_VdbeListi
1c7f4 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 74  ng) || fileExist
1c7f5 73 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 6c  s(db, "vdbe_expl
1c7f6 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 20  ain")).  ){.    
1c7f7 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74  int i;.    print
1c7f8 66 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20  f("VDBE Program 
1c7f9 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20  Listing:\n");.  
1c7fa 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1c7fb 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f  ntSql(p);.    fo
1c7fc 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
1c7fd 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
1c7fe 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
1c7ff 73 74 64 6f 75 74 2c 20 69 2c 20 26 61 4f 70 5b  stdout, i, &aOp[
1c800 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  i]);.    }.  }. 
1c801 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 28   if( fileExists(
1c802 64 62 2c 20 22 76 64 62 65 5f 74 72 61 63 65 22  db, "vdbe_trace"
1c803 29 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63  ) ){.    p->trac
1c804 65 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a  e = stdout;.  }.
1c805 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
1c806 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64  gnMalloc();.#end
1c807 69 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70  if.  for(pc=p->p
1c808 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  c; rc==SQLITE_OK
1c809 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73  ; pc++){.    ass
1c80a 65 72 74 28 20 70 63 3e 3d 30 20 26 26 20 70 63  ert( pc>=0 && pc
1c80b 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69  <p->nOp );.    i
1c80c 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
1c80d 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  led ) goto no_me
1c80e 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50  m;.#ifdef VDBE_P
1c80f 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 67 50  ROFILE.    origP
1c810 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 61 72  c = pc;.    star
1c811 74 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t = sqlite3Hwtim
1c812 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  e();.#endif.    
1c813 70 4f 70 20 3d 20 26 61 4f 70 5b 70 63 5d 3b 0a  pOp = &aOp[pc];.
1c814 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c  .    /* Only all
1c815 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51  ow tracing if SQ
1c816 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65  LITE_DEBUG is de
1c817 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69  fined..    */.#i
1c818 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1c819 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61  G.    if( p->tra
1c81a 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
1c81b 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  pc==0 ){.       
1c81c 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78   printf("VDBE Ex
1c81d 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e  ecution Trace:\n
1c81e 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ");.        sqli
1c81f 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
1c820 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
1c821 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1c822 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70  ntOp(p->trace, p
1c823 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20  c, pOp);.    }. 
1c824 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d     if( p->trace=
1c825 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20  =0 && pc==0 ){. 
1c826 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
1c827 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
1c828 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45  .      if( fileE
1c829 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f  xists(db, "vdbe_
1c82a 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20  sqltrace") ){.  
1c82b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c82c 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20  ePrintSql(p);.  
1c82d 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1c82e 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
1c82f 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  oc();.    }.#end
1c830 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f  if.      ..    /
1c831 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1c832 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d  f we need to sim
1c833 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75  ulate an interru
1c834 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68  pt.  This only h
1c835 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66  appens.    ** if
1c836 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69   we have a speci
1c837 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20  al test build.. 
1c838 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c     */.#ifdef SQL
1c839 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28  ITE_TEST.    if(
1c83a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1c83b 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20  pt_count>0 ){.  
1c83c 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65      sqlite3_inte
1c83d 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20  rrupt_count--;. 
1c83e 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1c83f 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
1c840 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
1c841 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1c842 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  (db);.      }.  
1c843 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e    }.#endif..#ifn
1c844 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c845 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
1c846 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68  K.    /* Call th
1c847 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
1c848 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e  ack if it is con
1c849 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20  figured and the 
1c84a 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a  required number.
1c84b 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f      ** of VDBE o
1c84c 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65  ps have been exe
1c84d 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69  cuted (either si
1c84e 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74  nce this invocat
1c84f 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71  ion of.    ** sq
1c850 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20  lite3VdbeExec() 
1c851 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69  or since last ti
1c852 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  me the progress 
1c853 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c  callback was cal
1c854 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20  led)..    ** If 
1c855 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
1c856 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
1c857 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65  n-zero, exit the
1c858 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1c859 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72   with.    ** a r
1c85a 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54  eturn code SQLIT
1c85b 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a  E_ABORT..    */.
1c85c 20 20 20 20 69 66 28 20 63 68 65 63 6b 50 72 6f      if( checkPro
1c85d 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69  gress ){.      i
1c85e 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73  f( db->nProgress
1c85f 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70  Ops==nProgressOp
1c860 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  s ){.        int
1c861 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66   prc;.        if
1c862 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1c863 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1c864 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1c865 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d  e;.        prc =
1c866 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62  db->xProgress(db
1c867 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b  ->pProgressArg);
1c868 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1c869 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1c86a 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1c86b 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
1c86c 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20       if( prc!=0 
1c86d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
1c86e 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
1c86f 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  PT;.          go
1c870 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
1c871 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  lt;.        }.  
1c872 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
1c873 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ps = 0;.      }.
1c874 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
1c875 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ps++;.    }.#end
1c876 69 66 0a 0a 20 20 20 20 2f 2a 20 4f 6e 20 61 6e  if..    /* On an
1c877 79 20 6f 70 63 6f 64 65 20 77 69 74 68 20 74 68  y opcode with th
1c878 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c 61 73  e "out2-prerelas
1c879 65 22 20 74 61 67 2c 20 66 72 65 65 20 61 6e 79  e" tag, free any
1c87a 0a 20 20 20 20 2a 2a 20 65 78 74 65 72 6e 61 6c  .    ** external
1c87b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 75 74   allocations out
1c87c 20 6f 66 20 6d 65 6d 5b 70 32 5d 20 61 6e 64 20   of mem[p2] and 
1c87d 73 65 74 20 6d 65 6d 5b 70 32 5d 20 74 6f 20 62  set mem[p2] to b
1c87e 65 0a 20 20 20 20 2a 2a 20 61 6e 20 75 6e 64 65  e.    ** an unde
1c87f 66 69 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20  fined integer.  
1c880 4f 70 63 6f 64 65 73 20 77 69 6c 6c 20 65 69 74  Opcodes will eit
1c881 68 65 72 20 66 69 6c 6c 20 69 6e 20 74 68 65 20  her fill in the 
1c882 69 6e 74 65 67 65 72 0a 20 20 20 20 2a 2a 20 76  integer.    ** v
1c883 61 6c 75 65 20 6f 72 20 63 6f 6e 76 65 72 74 20  alue or convert 
1c884 6d 65 6d 5b 70 32 5d 20 74 6f 20 61 20 64 69 66  mem[p2] to a dif
1c885 66 65 72 65 6e 74 20 74 79 70 65 2e 0a 20 20 20  ferent type..   
1c886 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
1c887 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 3d 3d 73 71  pOp->opflags==sq
1c888 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 65  lite3OpcodePrope
1c889 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d  rty[pOp->opcode]
1c88a 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   );.    if( pOp-
1c88b 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47  >opflags & OPFLG
1c88c 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45  _OUT2_PRERELEASE
1c88d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1c88e 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20  ( pOp->p2>0 );. 
1c88f 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1c890 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p2<=p->nMem );
1c891 0a 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 61  .      pOut = &a
1c892 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1c893 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1c894 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
1c895 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70  l(pOut);.      p
1c896 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
1c897 5f 49 6e 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _Int;.    }..   
1c898 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b   /* Sanity check
1c899 69 6e 67 20 6f 6e 20 6f 74 68 65 72 20 6f 70 65  ing on other ope
1c89a 72 61 6e 64 73 20 2a 2f 0a 23 69 66 64 65 66 20  rands */.#ifdef 
1c89b 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
1c89c 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61   if( (pOp->opfla
1c89d 67 73 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21  gs & OPFLG_IN1)!
1c89e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
1c89f 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b  rt( pOp->p1>0 );
1c8a0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c8a1 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1c8a2 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  );.      REGISTE
1c8a3 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
1c8a4 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 29   &aMem[pOp->p1])
1c8a5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1c8a6 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20  (pOp->opflags & 
1c8a7 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b  OPFLG_IN2)!=0 ){
1c8a8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c8a9 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20  Op->p2>0 );.    
1c8aa 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c8ab 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  2<=p->nMem );.  
1c8ac 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1c8ad 43 45 28 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65  CE(pOp->p2, &aMe
1c8ae 6d 5b 70 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 20  m[pOp->p2]);.   
1c8af 20 7d 0a 20 20 20 20 69 66 28 20 28 70 4f 70 2d   }.    if( (pOp-
1c8b0 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47  >opflags & OPFLG
1c8b1 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20  _IN3)!=0 ){.    
1c8b2 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c8b3 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  3>0 );.      ass
1c8b4 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  ert( pOp->p3<=p-
1c8b5 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 52  >nMem );.      R
1c8b6 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1c8b7 70 2d 3e 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70  p->p3, &aMem[pOp
1c8b8 2d 3e 70 33 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  ->p3]);.    }.  
1c8b9 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c    if( (pOp->opfl
1c8ba 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32  ags & OPFLG_OUT2
1c8bb 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  )!=0 ){.      as
1c8bc 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1c8bd 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1c8be 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65   pOp->p2<=p->nMe
1c8bf 6d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  m );.    }.    i
1c8c0 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73  f( (pOp->opflags
1c8c1 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d   & OPFLG_OUT3)!=
1c8c2 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
1c8c3 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a  t( pOp->p3>0 );.
1c8c4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1c8c5 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1c8c6 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1c8c7 20 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f   .    switch( pO
1c8c8 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a  p->opcode ){../*
1c8c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1c8ce 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20  What follows is 
1c8cf 61 20 6d 61 73 73 69 76 65 20 73 77 69 74 63 68  a massive switch
1c8d0 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 72 65   statement where
1c8d1 20 65 61 63 68 20 63 61 73 65 20 69 6d 70 6c 65   each case imple
1c8d2 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 70 61 72  ments a.** separ
1c8d3 61 74 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ate instruction 
1c8d4 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  in the virtual m
1c8d5 61 63 68 69 6e 65 2e 20 20 49 66 20 77 65 20 66  achine.  If we f
1c8d6 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 61 6c 0a  ollow the usual.
1c8d7 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 63  ** indentation c
1c8d8 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 61 63 68  onventions, each
1c8d9 20 63 61 73 65 20 73 68 6f 75 6c 64 20 62 65 20   case should be 
1c8da 69 6e 64 65 6e 74 65 64 20 62 79 20 36 20 73 70  indented by 6 sp
1c8db 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68  aces.  But.** th
1c8dc 61 74 20 69 73 20 61 20 6c 6f 74 20 6f 66 20 77  at is a lot of w
1c8dd 61 73 74 65 64 20 73 70 61 63 65 20 6f 6e 20 74  asted space on t
1c8de 68 65 20 6c 65 66 74 20 6d 61 72 67 69 6e 2e 20  he left margin. 
1c8df 20 53 6f 20 74 68 65 20 63 6f 64 65 20 77 69 74   So the code wit
1c8e0 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 69 74 63  hin.** the switc
1c8e1 68 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c  h statement will
1c8e2 20 62 72 65 61 6b 20 77 69 74 68 20 63 6f 6e 76   break with conv
1c8e3 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 20 66 6c  ention and be fl
1c8e4 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 65  ush-left. Anothe
1c8e5 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d 65 6e 74  r.** big comment
1c8e6 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69   (similar to thi
1c8e7 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d 61 72 6b  s one) will mark
1c8e8 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 20 74 68   the point in th
1c8e9 65 20 63 6f 64 65 20 77 68 65 72 65 0a 2a 2a 20  e code where.** 
1c8ea 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 62 61  we transition ba
1c8eb 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64  ck to normal ind
1c8ec 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
1c8ed 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f  The formatting o
1c8ee 66 20 65 61 63 68 20 63 61 73 65 20 69 73 20 69  f each case is i
1c8ef 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 65 20 6d  mportant.  The m
1c8f0 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 51 4c 69  akefile for SQLi
1c8f1 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 73 20  te.** generates 
1c8f2 74 77 6f 20 43 20 66 69 6c 65 73 20 22 6f 70 63  two C files "opc
1c8f3 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 6f 70 63  odes.h" and "opc
1c8f4 6f 64 65 73 2e 63 22 20 62 79 20 73 63 61 6e 6e  odes.c" by scann
1c8f5 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 69 6c 65  ing this.** file
1c8f6 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e   looking for lin
1c8f7 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69  es that begin wi
1c8f8 74 68 20 22 63 61 73 65 20 4f 50 5f 22 2e 20 20  th "case OP_".  
1c8f9 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 20 66 69  The opcodes.h fi
1c8fa 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66  les.** will be f
1c8fb 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65 66 69  illed with #defi
1c8fc 6e 65 73 20 74 68 61 74 20 67 69 76 65 20 75 6e  nes that give un
1c8fd 69 71 75 65 20 69 6e 74 65 67 65 72 20 76 61 6c  ique integer val
1c8fe 75 65 73 20 74 6f 20 65 61 63 68 0a 2a 2a 20 6f  ues to each.** o
1c8ff 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20 6f 70  pcode and the op
1c900 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69 73 20  codes.c file is 
1c901 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e 20 61  filled with an a
1c902 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 73 20  rray of strings 
1c903 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 73 74  where.** each st
1c904 72 69 6e 67 20 69 73 20 74 68 65 20 73 79 6d 62  ring is the symb
1c905 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20 74 68  olic name for th
1c906 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
1c907 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68 65 0a  opcode.  If the.
1c908 2a 2a 20 63 61 73 65 20 73 74 61 74 65 6d 65 6e  ** case statemen
1c909 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79  t is followed by
1c90a 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 68   a comment of th
1c90b 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 6d 65 20  e form "/# same 
1c90c 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68  as ... #/".** th
1c90d 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 20 75 73  at comment is us
1c90e 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
1c90f 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76  the particular v
1c910 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f  alue of the opco
1c911 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20  de..**.** Other 
1c912 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 68 65 20  keywords in the 
1c913 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 66 6f 6c  comment that fol
1c914 6c 6f 77 73 20 65 61 63 68 20 63 61 73 65 20 61  lows each case a
1c915 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 6f  re used to.** co
1c916 6e 73 74 72 75 63 74 20 74 68 65 20 4f 50 46 4c  nstruct the OPFL
1c917 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 76 61  G_INITIALIZER va
1c918 6c 75 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c  lue that initial
1c919 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 6f 70 65  izes opcodePrope
1c91a 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72  rty[]..** Keywor
1c91b 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 6e 31 2c  ds include: in1,
1c91c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 74 32 5f   in2, in3, out2_
1c91d 70 72 65 72 65 6c 65 61 73 65 2c 20 6f 75 74 32  prerelease, out2
1c91e 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a 2a 2a 20  , out3.  See.** 
1c91f 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77  the mkopcodeh.aw
1c920 6b 20 73 63 72 69 70 74 20 66 6f 72 20 61 64 64  k script for add
1c921 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
1c922 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d  ion..**.** Docum
1c923 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 20 56  entation about V
1c924 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 20 67  DBE opcodes is g
1c925 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 61 6e  enerated by scan
1c926 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 0a 2a  ning this file.*
1c927 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74  * for lines of t
1c928 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63  hat contain "Opc
1c929 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e  ode:".  That lin
1c92a 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 65 71  e and all subseq
1c92b 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20  uent.** comment 
1c92c 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 69  lines are used i
1c92d 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 6f 6e  n the generation
1c92e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 68   of the opcode.h
1c92f 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  tml documentatio
1c930 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  n.** file..**.**
1c931 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20   SUMMARY:.**.** 
1c932 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 20 69      Formatting i
1c933 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73  s important to s
1c934 63 72 69 70 74 73 20 74 68 61 74 20 73 63 61 6e  cripts that scan
1c935 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20   this file..**  
1c936 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 61 74     Do not deviat
1c937 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61  e from the forma
1c938 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 72 72  tting style curr
1c939 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a  ently in use..**
1c93a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1c93b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c93c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c93d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c93e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1c93f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74  ./* Opcode:  Got
1c940 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a  o * P2 * * *.**.
1c941 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f  ** An unconditio
1c942 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 64 64 72  nal jump to addr
1c943 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e  ess P2..** The n
1c944 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1c945 65 78 65 63 75 74 65 64 20 77 69 6c 6c 20 62 65  executed will be
1c946 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 61 74 20   .** the one at 
1c947 69 6e 64 65 78 20 50 32 20 66 72 6f 6d 20 74 68  index P2 from th
1c948 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a  e beginning of.*
1c949 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a  * the program..*
1c94a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20  /.case OP_Goto: 
1c94b 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {             /*
1c94c 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b   jump */.  CHECK
1c94d 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
1c94e 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1c94f 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   1;.  break;.}..
1c950 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75  /* Opcode:  Gosu
1c951 62 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  b P1 P2 * * *.**
1c952 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75  .** Write the cu
1c953 72 72 65 6e 74 20 61 64 64 72 65 73 73 20 6f 6e  rrent address on
1c954 74 6f 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a  to register P1.*
1c955 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20  * and then jump 
1c956 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a  to address P2..*
1c957 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 75 62 3a  /.case OP_Gosub:
1c958 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
1c959 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20   jump, in1 */.  
1c95a 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1c95b 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1c95c 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1c95d 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20  MEM_Dyn)==0 );. 
1c95e 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d   pIn1->flags = M
1c95f 45 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e  EM_Int;.  pIn1->
1c960 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49  u.i = pc;.  REGI
1c961 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1c962 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20  p1, pIn1);.  pc 
1c963 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1c964 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1c965 63 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31  code:  Return P1
1c966 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a   * * * *.**.** J
1c967 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ump to the next 
1c968 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65  instruction afte
1c969 72 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e  r the address in
1c96a 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f   register P1..*/
1c96b 0a 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a  .case OP_Return:
1c96c 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1c96d 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20  in1 */.  pIn1 = 
1c96e 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
1c96f 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
1c970 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
1c971 29 3b 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70  );.  pc = (int)p
1c972 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61  In1->u.i;.  brea
1c973 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1c974 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a    Yield P1 * * *
1c975 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68   *.**.** Swap th
1c976 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  e program counte
1c977 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65  r with the value
1c978 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
1c979 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c  .*/.case OP_Yiel
1c97a 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  d: {            
1c97b 2f 2a 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20  /* in1 */.#if 0 
1c97c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1c97d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1c97e 2e 61 61 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44  .aa */.  int pcD
1c97f 65 73 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  est;.#endif /* l
1c980 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1c981 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a  oved into u.aa *
1c982 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  /.  pIn1 = &aMem
1c983 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1c984 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67  ert( (pIn1->flag
1c985 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20  s & MEM_Dyn)==0 
1c986 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73  );.  pIn1->flags
1c987 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e   = MEM_Int;.  u.
1c988 61 61 2e 70 63 44 65 73 74 20 3d 20 28 69 6e 74  aa.pcDest = (int
1c989 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49  )pIn1->u.i;.  pI
1c98a 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20  n1->u.i = pc;.  
1c98b 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1c98c 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20  Op->p1, pIn1);. 
1c98d 20 70 63 20 3d 20 75 2e 61 61 2e 70 63 44 65 73   pc = u.aa.pcDes
1c98e 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  t;.  break;.}../
1c98f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 49  * Opcode:  HaltI
1c990 66 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 33 20  fNull  P1 P2 P3 
1c991 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b  P4 *.**.** Check
1c992 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1c993 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 69  gister P3.  If i
1c994 73 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48  s is NULL then H
1c995 61 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72  alt using.** par
1c996 61 6d 65 74 65 72 20 50 31 2c 20 50 32 2c 20 61  ameter P1, P2, a
1c997 6e 64 20 50 34 20 61 73 20 69 66 20 74 68 69 73  nd P4 as if this
1c998 20 77 65 72 65 20 61 20 48 61 6c 74 20 69 6e 73   were a Halt ins
1c999 74 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 74 68  truction.  If th
1c99a 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 72 65  e.** value in re
1c99b 67 69 73 74 65 72 20 50 33 20 69 73 20 6e 6f 74  gister P3 is not
1c99c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73   NULL, then this
1c99d 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
1c99e 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  -op..*/.case OP_
1c99f 48 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20  HaltIfNull: {   
1c9a0 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 70     /* in3 */.  p
1c9a1 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In3 = &aMem[pOp-
1c9a2 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e  >p3];.  if( (pIn
1c9a3 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  3->flags & MEM_N
1c9a4 75 6c 6c 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b  ull)==0 ) break;
1c9a5 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75  .  /* Fall throu
1c9a6 67 68 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20  gh into OP_Halt 
1c9a7 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  */.}../* Opcode:
1c9a8 20 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20 50    Halt P1 P2 * P
1c9a9 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69  4 *.**.** Exit i
1c9aa 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c  mmediately.  All
1c9ab 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2c 20 65   open cursors, e
1c9ac 74 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a  tc are closed.**
1c9ad 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a   automatically..
1c9ae 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
1c9af 72 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 75  result code retu
1c9b0 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
1c9b1 65 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 5f  exec(), sqlite3_
1c9b2 72 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73  reset(),.** or s
1c9b3 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1c9b4 29 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c  ).  For a normal
1c9b5 20 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f 75   halt, this shou
1c9b6 6c 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20  ld be SQLITE_OK 
1c9b7 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f  (0)..** For erro
1c9b8 72 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 6f  rs, it can be so
1c9b9 6d 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e 20  me other value. 
1c9ba 20 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 50   If P1!=0 then P
1c9bb 32 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65  2 will determine
1c9bc 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e  .** whether or n
1c9bd 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  ot to rollback t
1c9be 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
1c9bf 61 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20  action.  Do not 
1c9c0 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50  rollback.** if P
1c9c1 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74  2==OE_Fail. Do t
1c9c2 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50  he rollback if P
1c9c3 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20  2==OE_Rollback. 
1c9c4 20 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74   If P2==OE_Abort
1c9c5 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f  ,.** then back o
1c9c6 75 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 74  ut all changes t
1c9c7 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65  hat have occurre
1c9c8 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 78  d during this ex
1c9c9 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a  ecution of the.*
1c9ca 2a 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 6e  * VDBE, but do n
1c9cb 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  ot rollback the 
1c9cc 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a  transaction. .**
1c9cd 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74  .** If P4 is not
1c9ce 20 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73   null then it is
1c9cf 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1c9d0 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  e string..**.** 
1c9d1 54 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c  There is an impl
1c9d2 69 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22  ied "Halt 0 0 0"
1c9d3 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73   instruction ins
1c9d4 65 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72  erted at the ver
1c9d5 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72  y end of.** ever
1c9d6 79 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61  y program.  So a
1c9d7 20 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 6c   jump past the l
1c9d8 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ast instruction 
1c9d9 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a  of the program.*
1c9da 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  * is the same as
1c9db 20 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 2e   executing Halt.
1c9dc 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74  .*/.case OP_Halt
1c9dd 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70  : {.  if( pOp->p
1c9de 31 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  1==SQLITE_OK && 
1c9df 70 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20  p->pFrame ){.   
1c9e0 20 2f 2a 20 48 61 6c 74 20 74 68 65 20 73 75 62   /* Halt the sub
1c9e1 2d 70 72 6f 67 72 61 6d 2e 20 52 65 74 75 72 6e  -program. Return
1c9e2 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20   control to the 
1c9e3 70 61 72 65 6e 74 20 66 72 61 6d 65 2e 20 2a 2f  parent frame. */
1c9e4 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a  .    VdbeFrame *
1c9e5 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61  pFrame = p->pFra
1c9e6 6d 65 3b 0a 20 20 20 20 70 2d 3e 70 46 72 61 6d  me;.    p->pFram
1c9e7 65 20 3d 20 70 46 72 61 6d 65 2d 3e 70 50 61 72  e = pFrame->pPar
1c9e8 65 6e 74 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 61  ent;.    p->nFra
1c9e9 6d 65 2d 2d 3b 0a 20 20 20 20 73 71 6c 69 74 65  me--;.    sqlite
1c9ea 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
1c9eb 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b  db, p->nChange);
1c9ec 0a 20 20 20 20 70 63 20 3d 20 73 71 6c 69 74 65  .    pc = sqlite
1c9ed 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72  3VdbeFrameRestor
1c9ee 65 28 70 46 72 61 6d 65 29 3b 0a 20 20 20 20 69  e(pFrame);.    i
1c9ef 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49  f( pOp->p2==OE_I
1c9f0 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 2f  gnore ){.      /
1c9f1 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 70 63  * Instruction pc
1c9f2 20 69 73 20 74 68 65 20 4f 50 5f 50 72 6f 67 72   is the OP_Progr
1c9f3 61 6d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20  am that invoked 
1c9f4 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  the sub-program 
1c9f5 0a 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e  .      ** curren
1c9f6 74 6c 79 20 62 65 69 6e 67 20 68 61 6c 74 65 64  tly being halted
1c9f7 2e 20 49 66 20 74 68 65 20 70 32 20 69 6e 73 74  . If the p2 inst
1c9f8 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ruction of this 
1c9f9 4f 50 5f 48 61 6c 74 0a 20 20 20 20 20 20 2a 2a  OP_Halt.      **
1c9fa 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
1c9fb 73 65 74 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65  set to OE_Ignore
1c9fc 2c 20 74 68 65 6e 20 74 68 65 20 73 75 62 2d 70  , then the sub-p
1c9fd 72 6f 67 72 61 6d 20 69 73 20 74 68 72 6f 77 69  rogram is throwi
1c9fe 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 49  ng.      ** an I
1c9ff 47 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e  GNORE exception.
1ca00 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75   In this case ju
1ca01 6d 70 20 74 6f 20 74 68 65 20 61 64 64 72 65 73  mp to the addres
1ca02 73 20 73 70 65 63 69 66 69 65 64 0a 20 20 20 20  s specified.    
1ca03 20 20 2a 2a 20 61 73 20 74 68 65 20 70 32 20 6f    ** as the p2 o
1ca04 66 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 4f 50  f the calling OP
1ca05 5f 50 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20  _Program.  */.  
1ca06 20 20 20 20 70 63 20 3d 20 70 2d 3e 61 4f 70 5b      pc = p->aOp[
1ca07 70 63 5d 2e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a  pc].p2-1;.    }.
1ca08 20 20 20 20 61 4f 70 20 3d 20 70 2d 3e 61 4f 70      aOp = p->aOp
1ca09 3b 0a 20 20 20 20 61 4d 65 6d 20 3d 20 70 2d 3e  ;.    aMem = p->
1ca0a 61 4d 65 6d 3b 0a 20 20 20 20 62 72 65 61 6b 3b  aMem;.    break;
1ca0b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20  .  }..  p->rc = 
1ca0c 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72  pOp->p1;.  p->er
1ca0d 72 6f 72 41 63 74 69 6f 6e 20 3d 20 28 75 38 29  rorAction = (u8)
1ca0e 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63  pOp->p2;.  p->pc
1ca0f 20 3d 20 70 63 3b 0a 20 20 69 66 28 20 70 4f 70   = pc;.  if( pOp
1ca10 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71  ->p4.z ){.    sq
1ca11 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1ca12 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1ca13 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  "%s", pOp->p4.z)
1ca14 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
1ca15 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b  ite3VdbeHalt(p);
1ca16 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
1ca17 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63  QLITE_BUSY || rc
1ca18 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72  ==SQLITE_OK || r
1ca19 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  c==SQLITE_ERROR 
1ca1a 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1ca1b 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
1ca1c 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c  p->rc = rc = SQL
1ca1d 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73  ITE_BUSY;.  }els
1ca1e 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  e{.    assert( r
1ca1f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
1ca20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f  p->rc==SQLITE_CO
1ca21 4e 53 54 52 41 49 4e 54 20 29 3b 0a 20 20 20 20  NSTRAINT );.    
1ca22 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1ca23 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65  TE_OK || db->nDe
1ca24 66 65 72 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a  ferredCons>0 );.
1ca25 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f      rc = p->rc ?
1ca26 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20   SQLITE_ERROR : 
1ca27 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
1ca28 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74  .  goto vdbe_ret
1ca29 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  urn;.}../* Opcod
1ca2a 65 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32  e: Integer P1 P2
1ca2b 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65   * * *.**.** The
1ca2c 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
1ca2d 76 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74  value P1 is writ
1ca2e 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ten into registe
1ca2f 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P2..*/.case OP
1ca30 5f 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20  _Integer: {     
1ca31 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1ca32 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74  elease */.  pOut
1ca33 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  ->u.i = pOp->p1;
1ca34 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1ca35 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20  Opcode: Int64 * 
1ca36 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  P2 * P4 *.**.** 
1ca37 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
1ca38 74 6f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65  to a 64-bit inte
1ca39 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72  ger value..** Wr
1ca3a 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69  ite that value i
1ca3b 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
1ca3c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36  .*/.case OP_Int6
1ca3d 34 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f  4: {           /
1ca3e 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1ca3f 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  e */.  assert( p
1ca40 4f 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29  Op->p4.pI64!=0 )
1ca41 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
1ca42 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20  *pOp->p4.pI64;. 
1ca43 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1ca44 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20  code: Real * P2 
1ca45 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1ca46 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1ca47 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e  a 64-bit floatin
1ca48 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a  g point value..*
1ca49 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c  * Write that val
1ca4a 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  ue into register
1ca4b 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P2..*/.case OP_
1ca4c 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20  Real: {         
1ca4d 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1ca4e 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65  _FLOAT, out2-pre
1ca4f 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75  release */.  pOu
1ca50 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52  t->flags = MEM_R
1ca51 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21  eal;.  assert( !
1ca52 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f  sqlite3IsNaN(*pO
1ca53 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a  p->p4.pReal) );.
1ca54 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70    pOut->r = *pOp
1ca55 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72  ->p4.pReal;.  br
1ca56 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ca57 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20  e: String8 * P2 
1ca58 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1ca59 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20  points to a nul 
1ca5a 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
1ca5b 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70   string. This op
1ca5c 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72  code is transfor
1ca5d 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20  med .** into an 
1ca5e 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65  OP_String before
1ca5f 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20   it is executed 
1ca60 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69  for the first ti
1ca61 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  me..*/.case OP_S
1ca62 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20  tring8: {       
1ca63 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1ca64 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65  STRING, out2-pre
1ca65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73  release */.  ass
1ca66 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d  ert( pOp->p4.z!=
1ca67 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  0 );.  pOp->opco
1ca68 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a  de = OP_String;.
1ca69 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69    pOp->p1 = sqli
1ca6a 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d  te3Strlen30(pOp-
1ca6b 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66  >p4.z);..#ifndef
1ca6c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1ca6d 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e  16.  if( encodin
1ca6e 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  g!=SQLITE_UTF8 )
1ca6f 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1ca70 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
1ca71 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c  pOut, pOp->p4.z,
1ca72 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
1ca73 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
1ca74 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1ca75 4c 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f  LITE_TOOBIG ) go
1ca76 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20  to too_big;.    
1ca77 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
1ca78 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1ca79 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65  Encoding(pOut, e
1ca7a 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20  ncoding) ) goto 
1ca7b 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65  no_mem;.    asse
1ca7c 72 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f  rt( pOut->zMallo
1ca7d 63 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20  c==pOut->z );.  
1ca7e 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e    assert( pOut->
1ca7f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20  flags & MEM_Dyn 
1ca80 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61  );.    pOut->zMa
1ca81 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f  lloc = 0;.    pO
1ca82 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  ut->flags |= MEM
1ca83 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75  _Static;.    pOu
1ca84 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d  t->flags &= ~MEM
1ca85 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f  _Dyn;.    if( pO
1ca86 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59  p->p4type==P4_DY
1ca87 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73  NAMIC ){.      s
1ca88 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ca89 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20   pOp->p4.z);.   
1ca8a 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79   }.    pOp->p4ty
1ca8b 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b  pe = P4_DYNAMIC;
1ca8c 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d  .    pOp->p4.z =
1ca8d 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f   pOut->z;.    pO
1ca8e 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b  p->p1 = pOut->n;
1ca8f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
1ca90 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c  ( pOp->p1>db->aL
1ca91 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1ca92 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1ca93 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
1ca94 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72   }.  /* Fall thr
1ca95 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
1ca96 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67   case, OP_String
1ca97 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f   */.}.  ./* Opco
1ca98 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32  de: String P1 P2
1ca99 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68   * P4 *.**.** Th
1ca9a 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50  e string value P
1ca9b 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28  4 of length P1 (
1ca9c 62 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64  bytes) is stored
1ca9d 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
1ca9e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69  .*/.case OP_Stri
1ca9f 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f  ng: {          /
1caa0 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1caa1 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  e */.  assert( p
1caa2 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20  Op->p4.z!=0 );. 
1caa3 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1caa4 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69  EM_Str|MEM_Stati
1caa5 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f  c|MEM_Term;.  pO
1caa6 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e  ut->z = pOp->p4.
1caa7 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70  z;.  pOut->n = p
1caa8 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e  Op->p1;.  pOut->
1caa9 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  enc = encoding;.
1caaa 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1caab 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62  BSIZE(pOut);.  b
1caac 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1caad 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20  de: Null * P2 * 
1caae 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1caaf 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69  a NULL into regi
1cab0 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  ster P2..*/.case
1cab1 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20   OP_Null: {     
1cab2 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1cab3 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f  erelease */.  pO
1cab4 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
1cab5 4e 75 6c 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Null;.  break;.}
1cab6 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c  .../* Opcode: Bl
1cab7 6f 62 20 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a  ob P1 P2 * P4.**
1cab8 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f  .** P4 points to
1cab9 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20   a blob of data 
1caba 50 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20  P1 bytes long.  
1cabb 53 74 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c  Store this.** bl
1cabc 6f 62 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ob in register P
1cabd 32 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  2. This instruct
1cabe 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64  ion is not coded
1cabf 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20   directly.** by 
1cac0 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e  the compiler. In
1cac1 73 74 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69  stead, the compi
1cac2 6c 65 72 20 6c 61 79 65 72 20 73 70 65 63 69 66  ler layer specif
1cac3 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78  ies.** an OP_Hex
1cac4 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74  Blob opcode, wit
1cac5 68 20 74 68 65 20 68 65 78 20 73 74 72 69 6e 67  h the hex string
1cac6 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1cac7 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61  of.** the blob a
1cac8 73 20 50 34 2e 20 54 68 69 73 20 6f 70 63 6f 64  s P4. This opcod
1cac9 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64  e is transformed
1caca 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a   to an OP_Blob.*
1cacb 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  * the first time
1cacc 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e   it is executed.
1cacd 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62  .*/.case OP_Blob
1cace 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1cacf 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1cad0 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72  lease */.  asser
1cad1 74 28 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51  t( pOp->p1 <= SQ
1cad2 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20  LITE_MAX_LENGTH 
1cad3 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1cad4 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20  MemSetStr(pOut, 
1cad5 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e  pOp->p4.z, pOp->
1cad6 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75  p1, 0, 0);.  pOu
1cad7 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e  t->enc = encodin
1cad8 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  g;.  UPDATE_MAX_
1cad9 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1cada 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1cadb 70 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65 20  pcode: Variable 
1cadc 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
1cadd 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65  .** Transfer the
1cade 20 76 61 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64   values of bound
1cadf 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 2e 2e   parameters P1..
1cae0 50 31 2b 50 33 2d 31 20 69 6e 74 6f 20 72 65 67  P1+P3-1 into reg
1cae1 69 73 74 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 32  isters.** P2..P2
1cae2 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  +P3-1..**.** If 
1cae3 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  the parameter is
1cae4 20 6e 61 6d 65 64 2c 20 74 68 65 6e 20 69 74 73   named, then its
1cae5 20 6e 61 6d 65 20 61 70 70 65 61 72 73 20 69 6e   name appears in
1cae6 20 50 34 20 61 6e 64 20 50 33 3d 3d 31 2e 0a 2a   P4 and P3==1..*
1cae7 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 69  * The P4 value i
1cae8 73 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65  s used by sqlite
1cae9 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
1caea 5f 6e 61 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 65  _name()..*/.case
1caeb 20 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a   OP_Variable: {.
1caec 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1caed 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1caee 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 69  into u.ab */.  i
1caef 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20  nt p1;          
1caf0 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6f 20 63  /* Variable to c
1caf1 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e  opy from */.  in
1caf2 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f  t p2;          /
1caf3 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f  * Register to co
1caf4 70 79 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e  py to */.  int n
1caf5 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1caf6 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20  umber of values 
1caf7 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a  left to copy */.
1caf8 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20    Mem *pVar;    
1caf9 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e     /* Value bein
1cafa 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f  g transferred */
1cafb 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1cafc 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1cafd 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a 20   into u.ab */.. 
1cafe 20 75 2e 61 62 2e 70 31 20 3d 20 70 4f 70 2d 3e   u.ab.p1 = pOp->
1caff 70 31 20 2d 20 31 3b 0a 20 20 75 2e 61 62 2e 70  p1 - 1;.  u.ab.p
1cb00 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75  2 = pOp->p2;.  u
1cb01 2e 61 62 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b  .ab.n = pOp->p3;
1cb02 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e  .  assert( u.ab.
1cb03 70 31 3e 3d 30 20 26 26 20 75 2e 61 62 2e 70 31  p1>=0 && u.ab.p1
1cb04 2b 75 2e 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 72  +u.ab.n<=p->nVar
1cb05 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1cb06 61 62 2e 70 32 3e 3d 31 20 26 26 20 75 2e 61 62  ab.p2>=1 && u.ab
1cb07 2e 70 32 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 2d  .p2+u.ab.n-1<=p-
1cb08 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72  >nMem );.  asser
1cb09 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20  t( pOp->p4.z==0 
1cb0a 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 7c 7c  || pOp->p3==1 ||
1cb0b 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 0a 0a   pOp->p3==0 );..
1cb0c 20 20 77 68 69 6c 65 28 20 75 2e 61 62 2e 6e 2d    while( u.ab.n-
1cb0d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 75 2e 61  - > 0 ){.    u.a
1cb0e 62 2e 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61  b.pVar = &p->aVa
1cb0f 72 5b 75 2e 61 62 2e 70 31 2b 2b 5d 3b 0a 20 20  r[u.ab.p1++];.  
1cb10 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1cb11 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 61 62 2e  eMemTooBig(u.ab.
1cb12 70 56 61 72 29 20 29 7b 0a 20 20 20 20 20 20 67  pVar) ){.      g
1cb13 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20  oto too_big;.   
1cb14 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 61   }.    pOut = &a
1cb15 4d 65 6d 5b 75 2e 61 62 2e 70 32 2b 2b 5d 3b 0a  Mem[u.ab.p2++];.
1cb16 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1cb17 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
1cb18 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 70 4f 75  l(pOut);.    pOu
1cb19 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  t->flags = MEM_N
1cb1a 75 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ull;.    sqlite3
1cb1b 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
1cb1c 70 79 28 70 4f 75 74 2c 20 75 2e 61 62 2e 70 56  py(pOut, u.ab.pV
1cb1d 61 72 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b  ar, MEM_Static);
1cb1e 0a 20 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  .    UPDATE_MAX_
1cb1f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1cb20 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1cb21 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20  /* Opcode: Move 
1cb22 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1cb23 2a 2a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75  ** Move the valu
1cb24 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  es in register P
1cb25 31 2e 2e 50 31 2b 50 33 2d 31 20 6f 76 65 72 20  1..P1+P3-1 over 
1cb26 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72  into.** register
1cb27 73 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20  s P2..P2+P3-1.  
1cb28 52 65 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31  Registers P1..P1
1cb29 2b 50 31 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66  +P1-1 are.** lef
1cb2a 74 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c  t holding a NULL
1cb2b 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f  .  It is an erro
1cb2c 72 20 66 6f 72 20 72 65 67 69 73 74 65 72 20 72  r for register r
1cb2d 61 6e 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b  anges.** P1..P1+
1cb2e 50 33 2d 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b  P3-1 and P2..P2+
1cb2f 50 33 2d 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e  P3-1 to overlap.
1cb30 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65  .*/.case OP_Move
1cb31 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1cb32 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1cb33 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f  ved into u.ac */
1cb34 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63  .  char *zMalloc
1cb35 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76  ;   /* Holding v
1cb36 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f  ariable for allo
1cb37 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a  cated memory */.
1cb38 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
1cb39 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1cb3a 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 20 74  registers left t
1cb3b 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20  o copy */.  int 
1cb3c 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p1;          /* 
1cb3d 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79  Register to copy
1cb3e 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70   from */.  int p
1cb3f 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  2;          /* R
1cb40 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20  egister to copy 
1cb41 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  to */.#endif /* 
1cb42 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1cb43 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20  moved into u.ac 
1cb44 2a 2f 0a 0a 20 20 75 2e 61 63 2e 6e 20 3d 20 70  */..  u.ac.n = p
1cb45 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 63 2e 70  Op->p3;.  u.ac.p
1cb46 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  1 = pOp->p1;.  u
1cb47 2e 61 63 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32  .ac.p2 = pOp->p2
1cb48 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 63  ;.  assert( u.ac
1cb49 2e 6e 3e 30 20 26 26 20 75 2e 61 63 2e 70 31 3e  .n>0 && u.ac.p1>
1cb4a 30 20 26 26 20 75 2e 61 63 2e 70 32 3e 30 20 29  0 && u.ac.p2>0 )
1cb4b 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 63  ;.  assert( u.ac
1cb4c 2e 70 31 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63  .p1+u.ac.n<=u.ac
1cb4d 2e 70 32 20 7c 7c 20 75 2e 61 63 2e 70 32 2b 75  .p2 || u.ac.p2+u
1cb4e 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 31 20 29  .ac.n<=u.ac.p1 )
1cb4f 3b 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  ;..  pIn1 = &aMe
1cb50 6d 5b 75 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 4f  m[u.ac.p1];.  pO
1cb51 75 74 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 63 2e  ut = &aMem[u.ac.
1cb52 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e  p2];.  while( u.
1cb53 61 63 2e 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73  ac.n-- ){.    as
1cb54 73 65 72 74 28 20 70 4f 75 74 3c 3d 26 61 4d 65  sert( pOut<=&aMe
1cb55 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20  m[p->nMem] );.  
1cb56 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d    assert( pIn1<=
1cb57 26 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29  &aMem[p->nMem] )
1cb58 3b 0a 20 20 20 20 75 2e 61 63 2e 7a 4d 61 6c 6c  ;.    u.ac.zMall
1cb59 6f 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c  oc = pOut->zMall
1cb5a 6f 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d  oc;.    pOut->zM
1cb5b 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73  alloc = 0;.    s
1cb5c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1cb5d 65 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20  e(pOut, pIn1);. 
1cb5e 20 20 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63     pIn1->zMalloc
1cb5f 20 3d 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 3b   = u.ac.zMalloc;
1cb60 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52  .    REGISTER_TR
1cb61 41 43 45 28 75 2e 61 63 2e 70 32 2b 2b 2c 20 70  ACE(u.ac.p2++, p
1cb62 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b  Out);.    pIn1++
1cb63 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20  ;.    pOut++;.  
1cb64 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1cb65 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31   Opcode: Copy P1
1cb66 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1cb67 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72  Make a copy of r
1cb68 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20  egister P1 into 
1cb69 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
1cb6a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
1cb6b 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70  ion makes a deep
1cb6c 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
1cb6d 75 65 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65  ue.  A duplicate
1cb6e 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61  .** is made of a
1cb6f 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f  ny string or blo
1cb70 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65  b constant.  See
1cb71 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a   also OP_SCopy..
1cb72 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a  */.case OP_Copy:
1cb73 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
1cb74 2a 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20  * in1, out2 */. 
1cb75 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
1cb76 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d  p->p1];.  pOut =
1cb77 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b   &aMem[pOp->p2];
1cb78 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21  .  assert( pOut!
1cb79 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74  =pIn1 );.  sqlit
1cb7a 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
1cb7b 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c  Copy(pOut, pIn1,
1cb7c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44   MEM_Ephem);.  D
1cb7d 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f  eephemeralize(pO
1cb7e 75 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  ut);.  REGISTER_
1cb7f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
1cb80 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1cb81 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f  ../* Opcode: SCo
1cb82 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  py P1 P2 * * *.*
1cb83 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c  *.** Make a shal
1cb84 6c 6f 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69  low copy of regi
1cb85 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67  ster P1 into reg
1cb86 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ister P2..**.** 
1cb87 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1cb88 20 6d 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77   makes a shallow
1cb89 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
1cb8a 75 65 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75  ue.  If the valu
1cb8b 65 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67  e.** is a string
1cb8c 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74   or blob, then t
1cb8d 68 65 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20  he copy is only 
1cb8e 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1cb8f 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64  .** original and
1cb90 20 68 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72   hence if the or
1cb91 69 67 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73  iginal changes s
1cb92 6f 20 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e  o will the copy.
1cb93 0a 2a 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68  .** Worse, if th
1cb94 65 20 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65  e original is de
1cb95 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63  allocated, the c
1cb96 6f 70 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61  opy becomes inva
1cb97 6c 69 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65  lid..** Thus the
1cb98 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75   program must gu
1cb99 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65  arantee that the
1cb9a 20 6f 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e   original will n
1cb9b 6f 74 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72  ot change.** dur
1cb9c 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65  ing the lifetime
1cb9d 20 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55   of the copy.  U
1cb9e 73 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61  se OP_Copy to ma
1cb9f 6b 65 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a  ke a complete.**
1cba0 20 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f   copy..*/.case O
1cba1 50 5f 53 43 6f 70 79 3a 20 7b 20 20 20 20 20 20  P_SCopy: {      
1cba2 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20 6f 75        /* in1, ou
1cba3 74 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26  t2 */.  pIn1 = &
1cba4 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1cba5 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f   pOut = &aMem[pO
1cba6 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74  p->p2];.  assert
1cba7 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a  ( pOut!=pIn1 );.
1cba8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cba9 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74  ShallowCopy(pOut
1cbaa 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65  , pIn1, MEM_Ephe
1cbab 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  m);.  REGISTER_T
1cbac 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f  RACE(pOp->p2, pO
1cbad 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
1cbae 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 75  ./* Opcode: Resu
1cbaf 6c 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a 20  ltRow P1 P2 * * 
1cbb0 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69  *.**.** The regi
1cbb1 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 68  sters P1 through
1cbb2 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e   P1+P2-1 contain
1cbb3 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
1cbb4 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68 69  .** results. Thi
1cbb5 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20  s opcode causes 
1cbb6 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  the sqlite3_step
1cbb7 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69  () call to termi
1cbb8 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20  nate.** with an 
1cbb9 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75 72  SQLITE_ROW retur
1cbba 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73 65  n code and it se
1cbbb 74 73 20 75 70 20 74 68 65 20 73 71 6c 69 74 65  ts up the sqlite
1cbbc 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74  3_stmt.** struct
1cbbd 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61  ure to provide a
1cbbe 63 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f 70  ccess to the top
1cbbf 20 50 31 20 76 61 6c 75 65 73 20 61 73 20 74 68   P1 values as th
1cbc0 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e  e result.** row.
1cbc1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 75  .*/.case OP_Resu
1cbc2 6c 74 52 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20  ltRow: {.#if 0  
1cbc3 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cbc4 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cbc5 61 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65  ad */.  Mem *pMe
1cbc6 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 23 65 6e 64  m;.  int i;.#end
1cbc7 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1cbc8 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1cbc9 20 75 2e 61 64 20 2a 2f 0a 20 20 61 73 73 65 72   u.ad */.  asser
1cbca 74 28 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e  t( p->nResColumn
1cbcb 3d 3d 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61  ==pOp->p2 );.  a
1cbcc 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30  ssert( pOp->p1>0
1cbcd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1cbce 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70  p->p1+pOp->p2<=p
1cbcf 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f  ->nMem+1 );..  /
1cbd0 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d  * If this statem
1cbd1 65 6e 74 20 68 61 73 20 76 69 6f 6c 61 74 65 64  ent has violated
1cbd2 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69   immediate forei
1cbd3 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
1cbd4 74 73 2c 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20  ts, do.  ** not 
1cbd5 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
1cbd6 72 20 6f 66 20 72 6f 77 73 20 6d 6f 64 69 66 69  r of rows modifi
1cbd7 65 64 2e 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52  ed. And do not R
1cbd8 45 4c 45 41 53 45 20 74 68 65 20 73 74 61 74 65  ELEASE the state
1cbd9 6d 65 6e 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61  ment.  ** transa
1cbda 63 74 69 6f 6e 2e 20 49 74 20 6e 65 65 64 73 20  ction. It needs 
1cbdb 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  to be rolled bac
1cbdc 6b 2e 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c  k.  */.  if( SQL
1cbdd 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
1cbde 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b  lite3VdbeCheckFk
1cbdf 28 70 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 61  (p, 0)) ){.    a
1cbe0 73 73 65 72 74 28 20 64 62 2d 3e 66 6c 61 67 73  ssert( db->flags
1cbe1 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77  &SQLITE_CountRow
1cbe2 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  s );.    assert(
1cbe3 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72   p->usesStmtJour
1cbe4 6e 61 6c 20 29 3b 0a 20 20 20 20 62 72 65 61 6b  nal );.    break
1cbe5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
1cbe6 68 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52  he SQLITE_CountR
1cbe7 6f 77 73 20 66 6c 61 67 20 69 73 20 73 65 74 20  ows flag is set 
1cbe8 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61 67 73  in sqlite3.flags
1cbe9 20 6d 61 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a   mask, then.  **
1cbea 20 44 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   DML statements 
1cbeb 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f  invoke this opco
1cbec 64 65 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65  de to return the
1cbed 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a   number of rows.
1cbee 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f    ** modified to
1cbef 20 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20   the user. This 
1cbf0 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20  is the only way 
1cbf1 74 68 61 74 20 61 20 56 4d 20 74 68 61 74 0a 20  that a VM that. 
1cbf2 20 2a 2a 20 6f 70 65 6e 73 20 61 20 73 74 61 74   ** opens a stat
1cbf3 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1cbf4 6e 20 6d 61 79 20 69 6e 76 6f 6b 65 20 74 68 69  n may invoke thi
1cbf5 73 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20  s opcode..  **. 
1cbf6 20 2a 2a 20 49 6e 20 63 61 73 65 20 74 68 69 73   ** In case this
1cbf7 20 69 73 20 73 75 63 68 20 61 20 73 74 61 74 65   is such a state
1cbf8 6d 65 6e 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20  ment, close any 
1cbf9 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1cbfa 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65  ction.  ** opene
1cbfb 64 20 62 79 20 74 68 69 73 20 56 4d 20 62 65 66  d by this VM bef
1cbfc 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f  ore returning co
1cbfd 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65  ntrol to the use
1cbfe 72 2e 20 54 68 69 73 20 69 73 20 74 6f 0a 20 20  r. This is to.  
1cbff 2a 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73  ** ensure that s
1cc00 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63  tatement-transac
1cc01 74 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73  tions are always
1cc02 20 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65   nested, not ove
1cc03 72 6c 61 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49  rlapping..  ** I
1cc04 66 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65  f the open state
1cc05 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e  ment-transaction
1cc06 20 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68   is not closed h
1cc07 65 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73  ere, then the us
1cc08 65 72 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65 70  er.  ** may step
1cc09 20 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61 74   another VM that
1cc0a 20 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20 73   opens its own s
1cc0b 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1cc0c 74 69 6f 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20  tion. This.  ** 
1cc0d 6d 61 79 20 6c 65 61 64 20 74 6f 20 6f 76 65 72  may lead to over
1cc0e 6c 61 70 70 69 6e 67 20 73 74 61 74 65 6d 65 6e  lapping statemen
1cc0f 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a  t transactions..
1cc10 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74    **.  ** The st
1cc11 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1cc12 69 6f 6e 20 69 73 20 6e 65 76 65 72 20 61 20 74  ion is never a t
1cc13 6f 70 2d 6c 65 76 65 6c 20 74 72 61 6e 73 61 63  op-level transac
1cc14 74 69 6f 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a  tion.  Hence.  *
1cc15 2a 20 74 68 65 20 52 45 4c 45 41 53 45 20 63 61  * the RELEASE ca
1cc16 6c 6c 20 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76  ll below can nev
1cc17 65 72 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20  er fail..  */.  
1cc18 61 73 73 65 72 74 28 20 70 2d 3e 69 53 74 61 74  assert( p->iStat
1cc19 65 6d 65 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e  ement==0 || db->
1cc1a 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75  flags&SQLITE_Cou
1cc1b 6e 74 52 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d  ntRows );.  rc =
1cc1c 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73   sqlite3VdbeClos
1cc1d 65 53 74 61 74 65 6d 65 6e 74 28 70 2c 20 53 41  eStatement(p, SA
1cc1e 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29  VEPOINT_RELEASE)
1cc1f 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63  ;.  if( NEVER(rc
1cc20 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a  !=SQLITE_OK) ){.
1cc21 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
1cc22 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20    /* Invalidate 
1cc23 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75  all ephemeral cu
1cc24 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20  rsor row caches 
1cc25 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72  */.  p->cacheCtr
1cc26 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20   = (p->cacheCtr 
1cc27 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61  + 2)|1;..  /* Ma
1cc28 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75  ke sure the resu
1cc29 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65  lts of the curre
1cc2a 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20  nt row are \000 
1cc2b 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20  terminated.  ** 
1cc2c 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69  and have an assi
1cc2d 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20  gned type.  The 
1cc2e 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65  results are de-e
1cc2f 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a  phemeralized as.
1cc30 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66    ** as side eff
1cc31 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64  ect..  */.  u.ad
1cc32 2e 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75  .pMem = p->pResu
1cc33 6c 74 53 65 74 20 3d 20 26 61 4d 65 6d 5b 70 4f  ltSet = &aMem[pO
1cc34 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e  p->p1];.  for(u.
1cc35 61 64 2e 69 3d 30 3b 20 75 2e 61 64 2e 69 3c 70  ad.i=0; u.ad.i<p
1cc36 4f 70 2d 3e 70 32 3b 20 75 2e 61 64 2e 69 2b 2b  Op->p2; u.ad.i++
1cc37 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1cc38 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
1cc39 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61  e(&u.ad.pMem[u.a
1cc3a 64 2e 69 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74  d.i]);.    sqlit
1cc3b 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79  e3VdbeMemStoreTy
1cc3c 70 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e  pe(&u.ad.pMem[u.
1cc3d 61 64 2e 69 5d 29 3b 0a 20 20 20 20 52 45 47 49  ad.i]);.    REGI
1cc3e 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1cc3f 70 31 2b 75 2e 61 64 2e 69 2c 20 26 75 2e 61 64  p1+u.ad.i, &u.ad
1cc40 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a  .pMem[u.ad.i]);.
1cc41 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
1cc42 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
1cc43 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20  o no_mem;..  /* 
1cc44 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f  Return SQLITE_RO
1cc45 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d  W.  */.  p->pc =
1cc46 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20   pc + 1;.  rc = 
1cc47 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f  SQLITE_ROW;.  go
1cc48 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1cc49 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  }../* Opcode: Co
1cc4a 6e 63 61 74 20 50 31 20 50 32 20 50 33 20 2a 20  ncat P1 P2 P3 * 
1cc4b 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  *.**.** Add the 
1cc4c 74 65 78 74 20 69 6e 20 72 65 67 69 73 74 65 72  text in register
1cc4d 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64   P1 onto the end
1cc4e 20 6f 66 20 74 68 65 20 74 65 78 74 20 69 6e 0a   of the text in.
1cc4f 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 20 61  ** register P2 a
1cc50 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1cc51 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1cc52 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1cc53 20 74 68 65 20 50 31 20 6f 72 20 50 32 20 74 65   the P1 or P2 te
1cc54 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e  xt are NULL then
1cc55 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50   store NULL in P
1cc56 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20  3..**.**   P3 = 
1cc57 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49  P2 || P1.**.** I
1cc58 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72  t is illegal for
1cc59 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20 62 65   P1 and P3 to be
1cc5a 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74   the same regist
1cc5b 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a  er. Sometimes,.*
1cc5c 2a 20 69 66 20 50 33 20 69 73 20 74 68 65 20 73  * if P3 is the s
1cc5d 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 73 20  ame register as 
1cc5e 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  P2, the implemen
1cc5f 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a  tation is able.*
1cc60 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d  * to avoid a mem
1cc61 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f  cpy()..*/.case O
1cc62 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20  P_Concat: {     
1cc63 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1cc64 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c   TK_CONCAT, in1,
1cc65 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69   in2, out3 */.#i
1cc66 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1cc67 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1cc68 74 6f 20 75 2e 61 65 20 2a 2f 0a 20 20 69 36 34  to u.ae */.  i64
1cc69 20 6e 42 79 74 65 3b 0a 23 65 6e 64 69 66 20 2f   nByte;.#endif /
1cc6a 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1cc6b 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1cc6c 65 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26  e */..  pIn1 = &
1cc6d 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1cc6e 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn2 = &aMem[pO
1cc6f 70 2d 3e 70 32 5d 3b 0a 20 20 70 4f 75 74 20 3d  p->p2];.  pOut =
1cc70 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
1cc71 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21  .  assert( pIn1!
1cc72 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28  =pOut );.  if( (
1cc73 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49  pIn1->flags | pI
1cc74 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d  n2->flags) & MEM
1cc75 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c  _Null ){.    sql
1cc76 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1cc77 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72  ll(pOut);.    br
1cc78 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45  eak;.  }.  if( E
1cc79 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20  xpandBlob(pIn1) 
1cc7a 7c 7c 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49  || ExpandBlob(pI
1cc7b 6e 32 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  n2) ) goto no_me
1cc7c 6d 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70  m;.  Stringify(p
1cc7d 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  In1, encoding);.
1cc7e 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32    Stringify(pIn2
1cc7f 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75  , encoding);.  u
1cc80 2e 61 65 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31  .ae.nByte = pIn1
1cc81 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20  ->n + pIn2->n;. 
1cc82 20 69 66 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e   if( u.ae.nByte>
1cc83 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1cc84 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
1cc85 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  ){.    goto too_
1cc86 62 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65  big;.  }.  MemSe
1cc87 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1cc88 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20  MEM_Str);.  if( 
1cc89 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1cc8a 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e  ow(pOut, (int)u.
1cc8b 61 65 2e 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74  ae.nByte+2, pOut
1cc8c 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67  ==pIn2) ){.    g
1cc8d 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1cc8e 20 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32    if( pOut!=pIn2
1cc8f 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70   ){.    memcpy(p
1cc90 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c  Out->z, pIn2->z,
1cc91 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20   pIn2->n);.  }. 
1cc92 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a   memcpy(&pOut->z
1cc93 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d  [pIn2->n], pIn1-
1cc94 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20  >z, pIn1->n);.  
1cc95 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79  pOut->z[u.ae.nBy
1cc96 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d  te] = 0;.  pOut-
1cc97 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d  >z[u.ae.nByte+1]
1cc98 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c   = 0;.  pOut->fl
1cc99 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
1cc9a 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e  .  pOut->n = (in
1cc9b 74 29 75 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20  t)u.ae.nByte;.  
1cc9c 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  pOut->enc = enco
1cc9d 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d  ding;.  UPDATE_M
1cc9e 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74  AX_BLOBSIZE(pOut
1cc9f 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1cca0 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31  * Opcode: Add P1
1cca1 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1cca2 20 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69   Add the value i
1cca3 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f  n register P1 to
1cca4 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1cca5 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64  gister P2.** and
1cca6 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1cca7 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1cca8 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1cca9 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1ccaa 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1ccab 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1ccac 4d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50  Multiply P1 P2 P
1ccad 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d  3 * *.**.**.** M
1ccae 75 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75  ultiply the valu
1ccaf 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1ccb0 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e   by the value in
1ccb1 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20   register P2.** 
1ccb2 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
1ccb3 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72  sult in register
1ccb4 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65   P3..** If eithe
1ccb5 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c  r input is NULL,
1ccb6 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
1ccb7 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ULL..*/./* Opcod
1ccb8 65 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50  e: Subtract P1 P
1ccb9 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  2 P3 * *.**.** S
1ccba 75 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75  ubtract the valu
1ccbb 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1ccbc 20 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20   from the value 
1ccbd 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a  in register P2.*
1ccbe 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  * and store the 
1ccbf 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1ccc0 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1ccc1 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1ccc2 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1ccc3 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1ccc4 6f 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50  ode: Divide P1 P
1ccc5 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44  2 P3 * *.**.** D
1ccc6 69 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20  ivide the value 
1ccc7 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62  in register P1 b
1ccc8 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  y the value in r
1ccc9 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e  egister P2.** an
1ccca 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  d store the resu
1cccb 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  lt in register P
1cccc 33 20 28 50 33 3d 50 32 2f 50 31 29 2e 20 49 66  3 (P3=P2/P1). If
1cccd 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a   the value in .*
1ccce 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  * register P1 is
1cccf 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
1ccd0 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20  result is NULL. 
1ccd1 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
1ccd2 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65  is .** NULL, the
1ccd3 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1ccd4 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .*/./* Opcode: R
1ccd5 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50  emainder P1 P2 P
1ccd6 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70  3 * *.**.** Comp
1ccd7 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 65  ute the remainde
1ccd8 72 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 20  r after integer 
1ccd9 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 20  division of the 
1ccda 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69  value in.** regi
1ccdb 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76  ster P1 by the v
1ccdc 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1ccdd 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68   P2 and store th
1ccde 65 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e 20  e result in P3. 
1ccdf 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1cce0 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20   in register P2 
1cce1 69 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 75  is zero the resu
1cce2 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49  lt is NULL..** I
1cce3 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
1cce4 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
1cce5 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1cce6 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20  .case OP_Add:   
1cce7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cce8 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c  /* same as TK_PL
1cce9 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  US, in1, in2, ou
1ccea 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75  t3 */.case OP_Su
1cceb 62 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 20  btract:         
1ccec 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cced 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69  TK_MINUS, in1, i
1ccee 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1ccef 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20   OP_Multiply:   
1ccf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1ccf1 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 69  me as TK_STAR, i
1ccf2 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1ccf3 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a  .case OP_Divide:
1ccf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ccf5 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c  /* same as TK_SL
1ccf6 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  ASH, in1, in2, o
1ccf7 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  ut3 */.case OP_R
1ccf8 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 20  emainder: {     
1ccf9 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1ccfa 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e   TK_REM, in1, in
1ccfb 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30  2, out3 */.#if 0
1ccfc 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1ccfd 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ccfe 75 2e 61 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  u.af */.  int fl
1ccff 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d  ags;      /* Com
1cd00 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67  bined MEM_* flag
1cd01 73 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75  s from both inpu
1cd02 74 73 20 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20  ts */.  i64 iA; 
1cd03 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
1cd04 65 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74  er value of left
1cd05 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36   operand */.  i6
1cd06 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  4 iB;         /*
1cd07 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f   Integer value o
1cd08 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  f right operand 
1cd09 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20  */.  double rA; 
1cd0a 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
1cd0b 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61  ue of left opera
1cd0c 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72  nd */.  double r
1cd0d 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20  B;      /* Real 
1cd0e 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f  value of right o
1cd0f 70 65 72 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66  perand */.#endif
1cd10 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1cd11 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1cd12 2e 61 66 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d  .af */..  pIn1 =
1cd13 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
1cd14 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41  .  applyNumericA
1cd15 66 66 69 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20  ffinity(pIn1);. 
1cd16 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn2 = &aMem[pO
1cd17 70 2d 3e 70 32 5d 3b 0a 20 20 61 70 70 6c 79 4e  p->p2];.  applyN
1cd18 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70  umericAffinity(p
1cd19 49 6e 32 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26  In2);.  pOut = &
1cd1a 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1cd1b 20 75 2e 61 66 2e 66 6c 61 67 73 20 3d 20 70 49   u.af.flags = pI
1cd1c 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32  n1->flags | pIn2
1cd1d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28  ->flags;.  if( (
1cd1e 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d  u.af.flags & MEM
1cd1f 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 67 6f 74 6f  _Null)!=0 ) goto
1cd20 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75   arithmetic_resu
1cd21 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66  lt_is_null;.  if
1cd22 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1cd23 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn2->flags & M
1cd24 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74  EM_Int)==MEM_Int
1cd25 20 29 7b 0a 20 20 20 20 75 2e 61 66 2e 69 41 20   ){.    u.af.iA 
1cd26 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20  = pIn1->u.i;.   
1cd27 20 75 2e 61 66 2e 69 42 20 3d 20 70 49 6e 32 2d   u.af.iB = pIn2-
1cd28 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 74 63 68  >u.i;.    switch
1cd29 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
1cd2a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41  .      case OP_A
1cd2b 64 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 66  dd:         u.af
1cd2c 2e 69 42 20 2b 3d 20 75 2e 61 66 2e 69 41 3b 20  .iB += u.af.iA; 
1cd2d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1cd2e 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72     case OP_Subtr
1cd2f 61 63 74 3a 20 20 20 20 75 2e 61 66 2e 69 42 20  act:    u.af.iB 
1cd30 2d 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20  -= u.af.iA;     
1cd31 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1cd32 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a  ase OP_Multiply:
1cd33 20 20 20 20 75 2e 61 66 2e 69 42 20 2a 3d 20 75      u.af.iB *= u
1cd34 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 62 72  .af.iA;       br
1cd35 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
1cd36 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20  OP_Divide: {.   
1cd37 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41       if( u.af.iA
1cd38 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  ==0 ) goto arith
1cd39 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
1cd3a 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a  null;.        /*
1cd3b 20 44 69 76 69 64 69 6e 67 20 74 68 65 20 6c 61   Dividing the la
1cd3c 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e  rgest possible n
1cd3d 65 67 61 74 69 76 65 20 36 34 2d 62 69 74 20 69  egative 64-bit i
1cd3e 6e 74 65 67 65 72 20 28 31 3c 3c 36 33 29 20 62  nteger (1<<63) b
1cd3f 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20  y.        ** -1 
1cd40 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67  returns an integ
1cd41 65 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20  er too large to 
1cd42 73 74 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69  store in a 64-bi
1cd43 74 20 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a  t data-type. On.
1cd44 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20          ** some 
1cd45 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74  architectures, t
1cd46 68 65 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f  he value overflo
1cd47 77 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f  ws to (1<<63). O
1cd48 6e 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20  n others,.      
1cd49 20 20 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73    ** a SIGFPE is
1cd4a 20 69 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c   issued. The fol
1cd4b 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
1cd4c 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73   normalizes this
1cd4d 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61  .        ** beha
1cd4e 76 69 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c  vior so that all
1cd4f 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 62   architectures b
1cd50 65 68 61 76 65 20 61 73 20 69 66 20 69 6e 74 65  ehave as if inte
1cd51 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  ger.        ** o
1cd52 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 64  verflow occurred
1cd53 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
1cd54 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41       if( u.af.iA
1cd55 3d 3d 2d 31 20 26 26 20 75 2e 61 66 2e 69 42 3d  ==-1 && u.af.iB=
1cd56 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20  =SMALLEST_INT64 
1cd57 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20  ) u.af.iA = 1;. 
1cd58 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 2f         u.af.iB /
1cd59 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20  = u.af.iA;.     
1cd5a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1cd5b 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
1cd5c 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75   {.        if( u
1cd5d 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f  .af.iA==0 ) goto
1cd5e 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75   arithmetic_resu
1cd5f 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20  lt_is_null;.    
1cd60 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d      if( u.af.iA=
1cd61 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d 20  =-1 ) u.af.iA = 
1cd62 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e  1;.        u.af.
1cd63 69 42 20 25 3d 20 75 2e 61 66 2e 69 41 3b 0a 20  iB %= u.af.iA;. 
1cd64 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1cd65 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1cd66 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 66  pOut->u.i = u.af
1cd67 2e 69 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54  .iB;.    MemSetT
1cd68 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1cd69 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  M_Int);.  }else{
1cd6a 0a 20 20 20 20 75 2e 61 66 2e 72 41 20 3d 20 73  .    u.af.rA = s
1cd6b 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
1cd6c 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 20 75  lue(pIn1);.    u
1cd6d 2e 61 66 2e 72 42 20 3d 20 73 71 6c 69 74 65 33  .af.rB = sqlite3
1cd6e 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49  VdbeRealValue(pI
1cd6f 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28  n2);.    switch(
1cd70 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a   pOp->opcode ){.
1cd71 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64        case OP_Ad
1cd72 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 66 2e  d:         u.af.
1cd73 72 42 20 2b 3d 20 75 2e 61 66 2e 72 41 3b 20 20  rB += u.af.rA;  
1cd74 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1cd75 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61    case OP_Subtra
1cd76 63 74 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 2d  ct:    u.af.rB -
1cd77 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20  = u.af.rA;      
1cd78 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
1cd79 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20  se OP_Multiply: 
1cd7a 20 20 20 75 2e 61 66 2e 72 42 20 2a 3d 20 75 2e     u.af.rB *= u.
1cd7b 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65  af.rA;       bre
1cd7c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1cd7d 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20  P_Divide: {.    
1cd7e 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30      /* (double)0
1cd7f 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
1cd80 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
1cd81 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20  _POINT... */.   
1cd82 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 72 41       if( u.af.rA
1cd83 3d 3d 28 64 6f 75 62 6c 65 29 30 20 29 20 67 6f  ==(double)0 ) go
1cd84 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65  to arithmetic_re
1cd85 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20  sult_is_null;.  
1cd86 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 2f 3d        u.af.rB /=
1cd87 20 75 2e 61 66 2e 72 41 3b 0a 20 20 20 20 20 20   u.af.rA;.      
1cd88 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1cd89 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20  .      default: 
1cd8a 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69  {.        u.af.i
1cd8b 41 20 3d 20 28 69 36 34 29 75 2e 61 66 2e 72 41  A = (i64)u.af.rA
1cd8c 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69  ;.        u.af.i
1cd8d 42 20 3d 20 28 69 36 34 29 75 2e 61 66 2e 72 42  B = (i64)u.af.rB
1cd8e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  ;.        if( u.
1cd8f 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20  af.iA==0 ) goto 
1cd90 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c  arithmetic_resul
1cd91 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20  t_is_null;.     
1cd92 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d     if( u.af.iA==
1cd93 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 31  -1 ) u.af.iA = 1
1cd94 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72  ;.        u.af.r
1cd95 42 20 3d 20 28 64 6f 75 62 6c 65 29 28 75 2e 61  B = (double)(u.a
1cd96 66 2e 69 42 20 25 20 75 2e 61 66 2e 69 41 29 3b  f.iB % u.af.iA);
1cd97 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1cd98 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1cd99 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e    if( sqlite3IsN
1cd9a 61 4e 28 75 2e 61 66 2e 72 42 29 20 29 7b 0a 20  aN(u.af.rB) ){. 
1cd9b 20 20 20 20 20 67 6f 74 6f 20 61 72 69 74 68 6d       goto arithm
1cd9c 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e  etic_result_is_n
1cd9d 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ull;.    }.    p
1cd9e 4f 75 74 2d 3e 72 20 3d 20 75 2e 61 66 2e 72 42  Out->r = u.af.rB
1cd9f 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
1cda0 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52  Flag(pOut, MEM_R
1cda1 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 75  eal);.    if( (u
1cda2 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  .af.flags & MEM_
1cda3 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Real)==0 ){.    
1cda4 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74    sqlite3VdbeInt
1cda5 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75  egerAffinity(pOu
1cda6 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  t);.    }.  }.  
1cda7 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74  break;..arithmet
1cda8 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
1cda9 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  l:.  sqlite3Vdbe
1cdaa 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29  MemSetNull(pOut)
1cdab 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1cdac 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71   Opcode: CollSeq
1cdad 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34   * * P4.**.** P4
1cdae 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1cdaf 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63   a CollSeq struc
1cdb0 74 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 63  t. If the next c
1cdb1 61 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75  all to a user fu
1cdb2 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67  nction.** or agg
1cdb3 72 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c  regate calls sql
1cdb4 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
1cdb5 65 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61  eq(), this colla
1cdb6 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69  tion sequence wi
1cdb7 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65  ll.** be returne
1cdb8 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  d. This is used 
1cdb9 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  by the built-in 
1cdba 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64  min(), max() and
1cdbb 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e   nullif().** fun
1cdbc 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ctions..**.** Th
1cdbd 65 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 64  e interface used
1cdbe 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   by the implemen
1cdbf 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66  tation of the af
1cdc0 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e  orementioned fun
1cdc1 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74  ctions.** to ret
1cdc2 72 69 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74  rieve the collat
1cdc3 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74  ion sequence set
1cdc4 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20   by this opcode 
1cdc5 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
1cdc6 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e  .** publicly, on
1cdc7 6c 79 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74  ly to user funct
1cdc8 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20  ions defined in 
1cdc9 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20  func.c..*/.case 
1cdca 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20  OP_CollSeq: {.  
1cdcb 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1cdcc 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20  ype==P4_COLLSEQ 
1cdcd 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1cdce 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69  * Opcode: Functi
1cdcf 6f 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50  on P1 P2 P3 P4 P
1cdd0 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61  5.**.** Invoke a
1cdd1 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28   user function (
1cdd2 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
1cdd3 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74  to a Function st
1cdd4 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20  ructure that.** 
1cdd5 64 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63  defines the func
1cdd6 74 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72  tion) with P5 ar
1cdd7 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72  guments taken fr
1cdd8 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61  om register P2 a
1cdd9 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73  nd.** successors
1cdda 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66  .  The result of
1cddb 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
1cddc 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1cddd 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73  ter P3..** Regis
1cdde 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20  ter P3 must not 
1cddf 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75  be one of the fu
1cde0 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a  nction inputs..*
1cde1 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d  *.** P1 is a 32-
1cde2 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69  bit bitmask indi
1cde3 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f  cating whether o
1cde4 72 20 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d  r not each argum
1cde5 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66  ent to the .** f
1cde6 75 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65  unction was dete
1cde7 72 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e  rmined to be con
1cde8 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65  stant at compile
1cde9 20 74 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69   time. If the fi
1cdea 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
1cdeb 77 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65  was constant the
1cdec 6e 20 62 69 74 20 30 20 6f 66 20 50 31 20 69 73  n bit 0 of P1 is
1cded 20 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73   set. This is us
1cdee 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a  ed to determine.
1cdef 2a 2a 20 77 68 65 74 68 65 72 20 6d 65 74 61 20  ** whether meta 
1cdf0 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  data associated 
1cdf1 77 69 74 68 20 61 20 75 73 65 72 20 66 75 6e 63  with a user func
1cdf2 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73  tion argument us
1cdf3 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  ing the.** sqlit
1cdf4 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29  e3_set_auxdata()
1cdf5 20 41 50 49 20 6d 61 79 20 62 65 20 73 61 66 65   API may be safe
1cdf6 6c 79 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69  ly retained unti
1cdf7 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e  l the next.** in
1cdf8 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  vocation of this
1cdf9 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53   opcode..**.** S
1cdfa 65 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70  ee also: AggStep
1cdfb 20 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f   and AggFinal.*/
1cdfc 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f  .case OP_Functio
1cdfd 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  n: {.#if 0  /* l
1cdfe 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1cdff 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a  oved into u.ag *
1ce00 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d  /.  int i;.  Mem
1ce01 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65   *pArg;.  sqlite
1ce02 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20  3_context ctx;. 
1ce03 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1ce04 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 3b  *apVal;.  int n;
1ce05 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1ce06 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ce07 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20   into u.ag */.. 
1ce08 20 75 2e 61 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70   u.ag.n = pOp->p
1ce09 35 3b 0a 20 20 75 2e 61 67 2e 61 70 56 61 6c 20  5;.  u.ag.apVal 
1ce0a 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73  = p->apArg;.  as
1ce0b 73 65 72 74 28 20 75 2e 61 67 2e 61 70 56 61 6c  sert( u.ag.apVal
1ce0c 20 7c 7c 20 75 2e 61 67 2e 6e 3d 3d 30 20 29 3b   || u.ag.n==0 );
1ce0d 0a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 67  ..  assert( u.ag
1ce0e 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70  .n==0 || (pOp->p
1ce0f 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75  2>0 && pOp->p2+u
1ce10 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31  .ag.n<=p->nMem+1
1ce11 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
1ce12 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c  Op->p3<pOp->p2 |
1ce13 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e  | pOp->p3>=pOp->
1ce14 70 32 2b 75 2e 61 67 2e 6e 20 29 3b 0a 20 20 75  p2+u.ag.n );.  u
1ce15 2e 61 67 2e 70 41 72 67 20 3d 20 26 61 4d 65 6d  .ag.pArg = &aMem
1ce16 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72  [pOp->p2];.  for
1ce17 28 75 2e 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e  (u.ag.i=0; u.ag.
1ce18 69 3c 75 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69  i<u.ag.n; u.ag.i
1ce19 2b 2b 2c 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29  ++, u.ag.pArg++)
1ce1a 7b 0a 20 20 20 20 75 2e 61 67 2e 61 70 56 61 6c  {.    u.ag.apVal
1ce1b 5b 75 2e 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e  [u.ag.i] = u.ag.
1ce1c 70 41 72 67 3b 0a 20 20 20 20 73 71 6c 69 74 65  pArg;.    sqlite
1ce1d 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70  3VdbeMemStoreTyp
1ce1e 65 28 75 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20  e(u.ag.pArg);.  
1ce1f 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1ce20 28 70 4f 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70  (pOp->p2, u.ag.p
1ce21 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  Arg);.  }..  ass
1ce22 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1ce23 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20  ==P4_FUNCDEF || 
1ce24 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1ce25 56 44 42 45 46 55 4e 43 20 29 3b 0a 20 20 69 66  VDBEFUNC );.  if
1ce26 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1ce27 34 5f 46 55 4e 43 44 45 46 20 29 7b 0a 20 20 20  4_FUNCDEF ){.   
1ce28 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20   u.ag.ctx.pFunc 
1ce29 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b  = pOp->p4.pFunc;
1ce2a 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 56  .    u.ag.ctx.pV
1ce2b 64 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d  dbeFunc = 0;.  }
1ce2c 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63  else{.    u.ag.c
1ce2d 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 28  tx.pVdbeFunc = (
1ce2e 56 64 62 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70  VdbeFunc*)pOp->p
1ce2f 34 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20  4.pVdbeFunc;.   
1ce30 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20   u.ag.ctx.pFunc 
1ce31 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65  = u.ag.ctx.pVdbe
1ce32 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d  Func->pFunc;.  }
1ce33 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1ce34 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33  >p3>0 && pOp->p3
1ce35 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
1ce36 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  Out = &aMem[pOp-
1ce37 3e 70 33 5d 3b 0a 20 20 75 2e 61 67 2e 63 74 78  >p3];.  u.ag.ctx
1ce38 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  .s.flags = MEM_N
1ce39 75 6c 6c 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e  ull;.  u.ag.ctx.
1ce3a 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61  s.db = db;.  u.a
1ce3b 67 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30  g.ctx.s.xDel = 0
1ce3c 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 7a  ;.  u.ag.ctx.s.z
1ce3d 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f  Malloc = 0;..  /
1ce3e 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c  * The output cel
1ce3f 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61  l may already ha
1ce40 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f  ve a buffer allo
1ce41 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a  cated. Move.  **
1ce42 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
1ce43 75 2e 61 67 2e 63 74 78 2e 73 20 73 6f 20 69 6e  u.ag.ctx.s so in
1ce44 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66   case the user-f
1ce45 75 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a  unction can use.
1ce46 20 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79    ** the already
1ce47 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65   allocated buffe
1ce48 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c  r instead of all
1ce49 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e  ocating a new on
1ce4a 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  e..  */.  sqlite
1ce4b 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e  3VdbeMemMove(&u.
1ce4c 61 67 2e 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b  ag.ctx.s, pOut);
1ce4d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1ce4e 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 4d  g(&u.ag.ctx.s, M
1ce4f 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61  EM_Null);..  u.a
1ce50 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20  g.ctx.isError = 
1ce51 30 3b 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74  0;.  if( u.ag.ct
1ce52 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26  x.pFunc->flags &
1ce53 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45   SQLITE_FUNC_NEE
1ce54 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73  DCOLL ){.    ass
1ce55 65 72 74 28 20 70 4f 70 3e 61 4f 70 20 29 3b 0a  ert( pOp>aOp );.
1ce56 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b      assert( pOp[
1ce57 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43  -1].p4type==P4_C
1ce58 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73  OLLSEQ );.    as
1ce59 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70  sert( pOp[-1].op
1ce5a 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71  code==OP_CollSeq
1ce5b 20 29 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78   );.    u.ag.ctx
1ce5c 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d  .pColl = pOp[-1]
1ce5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20  .p4.pColl;.  }. 
1ce5e 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1ce5f 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1ce60 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1ce61 73 75 73 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63  suse;.  (*u.ag.c
1ce62 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29  tx.pFunc->xFunc)
1ce63 28 26 75 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67  (&u.ag.ctx, u.ag
1ce64 2e 6e 2c 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b  .n, u.ag.apVal);
1ce65 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1ce66 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20  fetyOn(db) ){.  
1ce67 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1ce68 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e 63 74  Release(&u.ag.ct
1ce69 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61  x.s);.    goto a
1ce6a 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1ce6b 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  se;.  }.  if( db
1ce6c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1ce6d 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68  {.    /* Even th
1ce6e 6f 75 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20  ough a malloc() 
1ce6f 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20  has failed, the 
1ce70 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1ce71 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65  f the.    ** use
1ce72 72 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68  r function may h
1ce73 61 76 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71  ave called an sq
1ce74 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58  lite3_result_XXX
1ce75 28 29 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20  () function.    
1ce76 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76  ** to return a v
1ce77 61 6c 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  alue. The follow
1ce78 69 6e 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65  ing call release
1ce79 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a  s any resources.
1ce7a 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65      ** associate
1ce7b 64 20 77 69 74 68 20 73 75 63 68 20 61 20 76 61  d with such a va
1ce7c 6c 75 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  lue..    **.    
1ce7d 2a 2a 20 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d  ** Note: Maybe M
1ce7e 65 6d 52 65 6c 65 61 73 65 28 29 20 73 68 6f 75  emRelease() shou
1ce7f 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20  ld be called if 
1ce80 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1ce81 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c 73 20 61  ).    ** fails a
1ce82 6c 73 6f 20 28 74 68 65 20 69 66 28 2e 2e 2e 29  lso (the if(...)
1ce83 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65   statement above
1ce84 29 2e 20 42 75 74 20 69 66 20 70 65 6f 70 6c 65  ). But if people
1ce85 20 61 72 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75   are.    ** misu
1ce86 73 69 6e 67 20 73 71 6c 69 74 65 2c 20 74 68 65  sing sqlite, the
1ce87 79 20 68 61 76 65 20 62 69 67 67 65 72 20 70 72  y have bigger pr
1ce88 6f 62 6c 65 6d 73 20 74 68 61 6e 20 61 20 6c 65  oblems than a le
1ce89 61 6b 65 64 20 76 61 6c 75 65 2e 0a 20 20 20 20  aked value..    
1ce8a 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
1ce8b 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e  beMemRelease(&u.
1ce8c 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67  ag.ctx.s);.    g
1ce8d 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1ce8e 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61 75 78  .  /* If any aux
1ce8f 69 6c 69 61 72 79 20 64 61 74 61 20 66 75 6e 63  iliary data func
1ce90 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20  tions have been 
1ce91 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73 20 75  called by this u
1ce92 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20  ser function,.  
1ce93 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 63  ** immediately c
1ce94 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74  all the destruct
1ce95 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73  or for any non-s
1ce96 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a 20 20  tatic values..  
1ce97 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74  */.  if( u.ag.ct
1ce98 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20  x.pVdbeFunc ){. 
1ce99 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65     sqlite3VdbeDe
1ce9a 6c 65 74 65 41 75 78 44 61 74 61 28 75 2e 61 67  leteAuxData(u.ag
1ce9b 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2c 20  .ctx.pVdbeFunc, 
1ce9c 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f  pOp->p1);.    pO
1ce9d 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 20  p->p4.pVdbeFunc 
1ce9e 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65  = u.ag.ctx.pVdbe
1ce9f 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  Func;.    pOp->p
1cea0 34 74 79 70 65 20 3d 20 50 34 5f 56 44 42 45 46  4type = P4_VDBEF
1cea1 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  UNC;.  }..  /* I
1cea2 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72  f the function r
1cea3 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72  eturned an error
1cea4 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70  , throw an excep
1cea5 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e  tion */.  if( u.
1cea6 61 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 29  ag.ctx.isError )
1cea7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
1cea8 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1cea9 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
1ceaa 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1ceab 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a  (&u.ag.ctx.s));.
1ceac 20 20 20 20 72 63 20 3d 20 75 2e 61 67 2e 63 74      rc = u.ag.ct
1cead 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a  x.isError;.  }..
1ceae 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65    /* Copy the re
1ceaf 73 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63  sult of the func
1ceb0 74 69 6f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74  tion into regist
1ceb1 65 72 20 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74  er P3 */.  sqlit
1ceb2 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
1ceb3 64 69 6e 67 28 26 75 2e 61 67 2e 63 74 78 2e 73  ding(&u.ag.ctx.s
1ceb4 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73  , encoding);.  s
1ceb5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1ceb6 65 28 70 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74  e(pOut, &u.ag.ct
1ceb7 78 2e 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  x.s);.  if( sqli
1ceb8 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1ceb9 28 70 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f  (pOut) ){.    go
1ceba 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1cebb 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1cebc 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b  (pOp->p3, pOut);
1cebd 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
1cebe 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20  OBSIZE(pOut);.  
1cebf 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1cec0 6f 64 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50  ode: BitAnd P1 P
1cec1 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  2 P3 * *.**.** T
1cec2 61 6b 65 20 74 68 65 20 62 69 74 2d 77 69 73 65  ake the bit-wise
1cec3 20 41 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75   AND of the valu
1cec4 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  es in register P
1cec5 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20  1 and P2 and.** 
1cec6 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1cec7 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1cec8 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e  .** If either in
1cec9 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  put is NULL, the
1ceca 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1cecb 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42  .*/./* Opcode: B
1cecc 69 74 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20  itOr P1 P2 P3 * 
1cecd 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65  *.**.** Take the
1cece 20 62 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20   bit-wise OR of 
1cecf 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65  the values in re
1ced0 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 32  gister P1 and P2
1ced1 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68   and.** store th
1ced2 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
1ced3 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
1ced4 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
1ced5 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
1ced6 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
1ced7 70 63 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74  pcode: ShiftLeft
1ced8 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1ced9 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e  .** Shift the in
1ceda 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72  teger value in r
1cedb 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68  egister P2 to th
1cedc 65 20 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a  e left by the.**
1cedd 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   number of bits 
1cede 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
1cedf 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69   integer in regi
1cee0 73 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65  ser P1..** Store
1cee1 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1cee2 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1cee3 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1cee4 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1cee5 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1cee6 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52  * Opcode: ShiftR
1cee7 69 67 68 74 20 50 31 20 50 32 20 50 33 20 2a 20  ight P1 P2 P3 * 
1cee8 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68  *.**.** Shift th
1cee9 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  e integer value 
1ceea 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74  in register P2 t
1ceeb 6f 20 74 68 65 20 72 69 67 68 74 20 62 79 20 74  o the right by t
1ceec 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
1ceed 62 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62  bits specified b
1ceee 79 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e  y the integer in
1ceef 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1cef0 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   Store the resul
1cef1 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1cef2 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1cef3 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1cef4 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1cef5 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74  ..*/.case OP_Bit
1cef6 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20  And:            
1cef7 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cef8 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20  TK_BITAND, in1, 
1cef9 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
1cefa 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20  e OP_BitOr:     
1cefb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cefc 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52  same as TK_BITOR
1cefd 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
1cefe 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66   */.case OP_Shif
1ceff 74 4c 65 66 74 3a 20 20 20 20 20 20 20 20 20 20  tLeft:          
1cf00 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1cf01 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69  K_LSHIFT, in1, i
1cf02 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1cf03 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20   OP_ShiftRight: 
1cf04 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  {           /* s
1cf05 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54  ame as TK_RSHIFT
1cf06 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
1cf07 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1cf08 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1cf09 76 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f  ved into u.ah */
1cf0a 0a 20 20 69 36 34 20 61 3b 0a 20 20 69 36 34 20  .  i64 a;.  i64 
1cf0b 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  b;.#endif /* loc
1cf0c 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1cf0d 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a  ed into u.ah */.
1cf0e 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
1cf0f 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32  pOp->p1];.  pIn2
1cf10 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32   = &aMem[pOp->p2
1cf11 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  ];.  pOut = &aMe
1cf12 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66  m[pOp->p3];.  if
1cf13 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c  ( (pIn1->flags |
1cf14 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20   pIn2->flags) & 
1cf15 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1cf16 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1cf17 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20  tNull(pOut);.   
1cf18 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e   break;.  }.  u.
1cf19 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 33 56 64  ah.a = sqlite3Vd
1cf1a 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29  beIntValue(pIn2)
1cf1b 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 73 71 6c  ;.  u.ah.b = sql
1cf1c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
1cf1d 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 74 63 68  (pIn1);.  switch
1cf1e 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
1cf1f 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74  .    case OP_Bit
1cf20 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 68 2e 61  And:      u.ah.a
1cf21 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20   &= u.ah.b;     
1cf22 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1cf23 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20  OP_BitOr:       
1cf24 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 68 2e 62  u.ah.a |= u.ah.b
1cf25 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
1cf26 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65   case OP_ShiftLe
1cf27 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 3c 3c 3d  ft:   u.ah.a <<=
1cf28 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72 65 61   u.ah.b;    brea
1cf29 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
1cf2a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70   assert( pOp->op
1cf2b 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69  code==OP_ShiftRi
1cf2c 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ght );.         
1cf2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cf2e 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e 61 68 2e  u.ah.a >>= u.ah.
1cf2f 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  b;    break;.  }
1cf30 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75  .  pOut->u.i = u
1cf31 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 65 74 54  .ah.a;.  MemSetT
1cf32 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1cf33 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b  M_Int);.  break;
1cf34 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41  .}../* Opcode: A
1cf35 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a  ddImm  P1 P2 * *
1cf36 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68   *.** .** Add th
1cf37 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f  e constant P2 to
1cf38 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1cf39 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68  gister P1..** Th
1cf3a 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61  e result is alwa
1cf3b 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  ys an integer..*
1cf3c 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e  *.** To force an
1cf3d 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 62 65  y register to be
1cf3e 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a 75 73   an integer, jus
1cf3f 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65  t add 0..*/.case
1cf40 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20   OP_AddImm: {   
1cf41 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20           /* in1 
1cf42 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
1cf43 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 73 71  m[pOp->p1];.  sq
1cf44 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
1cf45 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  gerify(pIn1);.  
1cf46 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70  pIn1->u.i += pOp
1cf47 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  ->p2;.  break;.}
1cf48 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73  ../* Opcode: Mus
1cf49 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a  tBeInt P1 P2 * *
1cf4a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20   *.** .** Force 
1cf4b 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1cf4c 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 61  ister P1 to be a
1cf4d 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74  n integer.  If t
1cf4e 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50  he value.** in P
1cf4f 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  1 is not an inte
1cf50 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62  ger and cannot b
1cf51 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
1cf52 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77   an integer.** w
1cf53 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73  ithout data loss
1cf54 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65  , then jump imme
1cf55 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f  diately to P2, o
1cf56 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61  r if P2==0.** ra
1cf57 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49  ise an SQLITE_MI
1cf58 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e  SMATCH exception
1cf59 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73  ..*/.case OP_Mus
1cf5a 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20  tBeInt: {       
1cf5b 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1cf5c 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61  1 */.  pIn1 = &a
1cf5d 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1cf5e 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49  applyAffinity(pI
1cf5f 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  n1, SQLITE_AFF_N
1cf60 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67  UMERIC, encoding
1cf61 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  );.  if( (pIn1->
1cf62 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29  flags & MEM_Int)
1cf63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
1cf64 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20  Op->p2==0 ){.   
1cf65 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d     rc = SQLITE_M
1cf66 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67  ISMATCH;.      g
1cf67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1cf68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73  _error;.    }els
1cf69 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  e{.      pc = pO
1cf6a 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d  p->p2 - 1;.    }
1cf6b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65  .  }else{.    Me
1cf6c 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e  mSetTypeFlag(pIn
1cf6d 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d  1, MEM_Int);.  }
1cf6e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1cf6f 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69  Opcode: RealAffi
1cf70 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  nity P1 * * * *.
1cf71 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65  **.** If registe
1cf72 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e  r P1 holds an in
1cf73 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74  teger convert it
1cf74 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65   to a real value
1cf75 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
1cf76 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e  ode is used when
1cf77 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f   extracting info
1cf78 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63  rmation from a c
1cf79 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61  olumn that.** ha
1cf7a 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e  s REAL affinity.
1cf7b 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61    Such column va
1cf7c 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62  lues may still b
1cf7d 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69  e stored as.** i
1cf7e 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61  ntegers, for spa
1cf7f 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62  ce efficiency, b
1cf80 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74  ut after extract
1cf81 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d  ion we want them
1cf82 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79  .** to have only
1cf83 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a   a real value..*
1cf84 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66  /.case OP_RealAf
1cf85 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20  finity: {       
1cf86 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
1cf87 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61  1 */.  pIn1 = &a
1cf88 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1cf89 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  if( pIn1->flags 
1cf8a 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
1cf8b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1cf8c 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  ealify(pIn1);.  
1cf8d 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69  }.  break;.}..#i
1cf8e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1cf8f 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65  T_CAST./* Opcode
1cf90 3a 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20  : ToText P1 * * 
1cf91 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20  * *.**.** Force 
1cf92 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1cf93 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 74  ister P1 to be t
1cf94 65 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76  ext..** If the v
1cf95 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c  alue is numeric,
1cf96 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
1cf97 20 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68   string using th
1cf98 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  e.** equivalent 
1cf99 6f 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c  of printf().  Bl
1cf9a 6f 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e  ob values are un
1cf9b 63 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61  changed and.** a
1cf9c 72 65 20 61 66 74 65 72 77 61 72 64 73 20 73 69  re afterwards si
1cf9d 6d 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64  mply interpreted
1cf9e 20 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20   as text..**.** 
1cf9f 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20  A NULL value is 
1cfa0 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74  not changed by t
1cfa1 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74  his routine.  It
1cfa2 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a   remains NULL..*
1cfa3 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74  /.case OP_ToText
1cfa4 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1cfa5 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cfa6 54 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20  TK_TO_TEXT, in1 
1cfa7 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
1cfa8 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66  m[pOp->p1];.  if
1cfa9 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
1cfaa 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b  MEM_Null ) break
1cfab 3b 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f  ;.  assert( MEM_
1cfac 53 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e  Str==(MEM_Blob>>
1cfad 33 29 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c  3) );.  pIn1->fl
1cfae 61 67 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c  ags |= (pIn1->fl
1cfaf 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33  ags&MEM_Blob)>>3
1cfb0 3b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74  ;.  applyAffinit
1cfb1 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41  y(pIn1, SQLITE_A
1cfb2 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e  FF_TEXT, encodin
1cfb3 67 29 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e  g);.  rc = Expan
1cfb4 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61  dBlob(pIn1);.  a
1cfb5 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61  ssert( pIn1->fla
1cfb6 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20  gs & MEM_Str || 
1cfb7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1cfb8 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67   );.  pIn1->flag
1cfb9 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d  s &= ~(MEM_Int|M
1cfba 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62  EM_Real|MEM_Blob
1cfbb 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50  |MEM_Zero);.  UP
1cfbc 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1cfbd 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b  E(pIn1);.  break
1cfbe 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cfbf 54 6f 42 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20  ToBlob P1 * * * 
1cfc0 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
1cfc1 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1cfc2 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 42  ter P1 to be a B
1cfc3 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76  LOB..** If the v
1cfc4 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c  alue is numeric,
1cfc5 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
1cfc6 20 73 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a   string first..*
1cfc7 2a 20 53 74 72 69 6e 67 73 20 61 72 65 20 73 69  * Strings are si
1cfc8 6d 70 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74  mply reinterpret
1cfc9 65 64 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68  ed as blobs with
1cfca 20 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f   no change.** to
1cfcb 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
1cfcc 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  data..**.** A NU
1cfcd 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
1cfce 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1cfcf 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
1cfd0 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
1cfd1 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20  se OP_ToBlob: { 
1cfd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cfd3 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
1cfd4 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20  O_BLOB, in1 */. 
1cfd5 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
1cfd6 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70 49  p->p1];.  if( pI
1cfd7 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1cfd8 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20  Null ) break;.  
1cfd9 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1cfda 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20   & MEM_Blob)==0 
1cfdb 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69  ){.    applyAffi
1cfdc 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54  nity(pIn1, SQLIT
1cfdd 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f  E_AFF_TEXT, enco
1cfde 64 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72  ding);.    asser
1cfdf 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  t( pIn1->flags &
1cfe0 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e   MEM_Str || db->
1cfe1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
1cfe2 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1cfe3 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f  ag(pIn1, MEM_Blo
1cfe4 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  b);.  }else{.   
1cfe5 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20   pIn1->flags &= 
1cfe6 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e  ~(MEM_TypeMask&~
1cfe7 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20  MEM_Blob);.  }. 
1cfe8 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1cfe9 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72  SIZE(pIn1);.  br
1cfea 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cfeb 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20  e: ToNumeric P1 
1cfec 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f  * * * *.**.** Fo
1cfed 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  rce the value in
1cfee 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1cfef 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69 74 68  be numeric (eith
1cff0 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72  er an.** integer
1cff1 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70   or a floating-p
1cff2 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a  oint number.).**
1cff3 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   If the value is
1cff4 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74   text or blob, t
1cff5 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74  ry to convert it
1cff6 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 68 65   to an using the
1cff7 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f  .** equivalent o
1cff8 66 20 61 74 6f 69 28 29 20 6f 72 20 61 74 6f 66  f atoi() or atof
1cff9 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69  () and store 0 i
1cffa 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72  f no such conver
1cffb 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73  sion .** is poss
1cffc 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ible..**.** A NU
1cffd 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
1cffe 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1cfff 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
1d000 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
1d001 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a  se OP_ToNumeric:
1d002 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
1d003 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d004 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e  K_TO_NUMERIC, in
1d005 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61  1 */.  pIn1 = &a
1d006 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1d007 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1d008 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d   & (MEM_Null|MEM
1d009 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d  _Int|MEM_Real))=
1d00a 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1d00b 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79  3VdbeMemNumerify
1d00c 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72  (pIn1);.  }.  br
1d00d 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1d00e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
1d00f 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  T */../* Opcode:
1d010 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20   ToInt P1 * * * 
1d011 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
1d012 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1d013 74 65 72 20 50 31 20 62 65 20 61 6e 20 69 6e 74  ter P1 be an int
1d014 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65  eger.  If.** The
1d015 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e   value is curren
1d016 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d 62 65  tly a real numbe
1d017 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 61 63  r, drop its frac
1d018 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20  tional part..** 
1d019 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
1d01a 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72  text or blob, tr
1d01b 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20  y to convert it 
1d01c 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 75 73  to an integer us
1d01d 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76  ing the.** equiv
1d01e 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20  alent of atoi() 
1d01f 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e  and store 0 if n
1d020 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f  o such conversio
1d021 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a  n is possible..*
1d022 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75  *.** A NULL valu
1d023 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64  e is not changed
1d024 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
1d025 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55  .  It remains NU
1d026 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  LL..*/.case OP_T
1d027 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20  oInt: {         
1d028 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1d029 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69   as TK_TO_INT, i
1d02a 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26  n1 */.  pIn1 = &
1d02b 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1d02c 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
1d02d 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
1d02e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1d02f 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
1d030 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72  (pIn1);.  }.  br
1d031 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  eak;.}..#ifndef 
1d032 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
1d033 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65  ./* Opcode: ToRe
1d034 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  al P1 * * * *.**
1d035 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61  .** Force the va
1d036 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1d037 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74  P1 to be a float
1d038 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72  ing point number
1d039 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75  ..** If The valu
1d03a 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  e is currently a
1d03b 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65  n integer, conve
1d03c 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65  rt it..** If the
1d03d 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f   value is text o
1d03e 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63  r blob, try to c
1d03f 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20  onvert it to an 
1d040 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68  integer using th
1d041 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  e.** equivalent 
1d042 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74  of atoi() and st
1d043 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75  ore 0.0 if no su
1d044 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  ch conversion is
1d045 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a   possible..**.**
1d046 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73   A NULL value is
1d047 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20   not changed by 
1d048 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49  this routine.  I
1d049 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a  t remains NULL..
1d04a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61  */.case OP_ToRea
1d04b 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  l: {            
1d04c 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1d04d 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31   TK_TO_REAL, in1
1d04e 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d   */.  pIn1 = &aM
1d04f 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69  em[pOp->p1];.  i
1d050 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
1d051 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29  & MEM_Null)==0 )
1d052 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1d053 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31  eMemRealify(pIn1
1d054 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1d055 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1d056 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a  TE_OMIT_CAST */.
1d057 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50  ./* Opcode: Lt P
1d058 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1d059 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
1d05a 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74  values in regist
1d05b 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49  er P1 and P3.  I
1d05c 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31  f reg(P3)<reg(P1
1d05d 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74  ) then.** jump t
1d05e 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a  o address P2.  .
1d05f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
1d060 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62  ITE_JUMPIFNULL b
1d061 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20  it of P5 is set 
1d062 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50  and either reg(P
1d063 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29  1) or.** reg(P3)
1d064 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61   is NULL then ta
1d065 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66  ke the jump.  If
1d066 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50   the SQLITE_JUMP
1d067 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69  IFNULL .** bit i
1d068 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c  s clear then fal
1d069 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72  l thru if either
1d06a 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1d06b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
1d06c 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74  TE_AFF_MASK port
1d06d 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62  ion of P5 must b
1d06e 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68  e an affinity ch
1d06f 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c  aracter -.** SQL
1d070 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51  ITE_AFF_TEXT, SQ
1d071 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
1d072 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
1d073 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  An attempt is ma
1d074 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65  de .** to coerce
1d075 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63   both inputs acc
1d076 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61  ording to this a
1d077 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74  ffinity before t
1d078 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e  he.** comparison
1d079 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65   is made. If the
1d07a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
1d07b 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e   is 0x00, then n
1d07c 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69  umeric.** affini
1d07d 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65  ty is used. Note
1d07e 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69   that the affini
1d07f 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61  ty conversions a
1d080 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63  re stored.** bac
1d081 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74  k into the input
1d082 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1d083 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f  d P3.  So this o
1d084 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a  pcode can cause.
1d085 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68  ** persistent ch
1d086 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65  anges to registe
1d087 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a  rs P1 and P3..**
1d088 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e  .** Once any con
1d089 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61  versions have ta
1d08a 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e  ken place, and n
1d08b 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20  either value is 
1d08c 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61  NULL, .** the va
1d08d 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65  lues are compare
1d08e 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  d. If both value
1d08f 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e  s are blobs then
1d090 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20   memcmp() is.** 
1d091 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
1d092 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  e the results of
1d093 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e   the comparison.
1d094 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
1d095 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68  .** are text, th
1d096 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  en the appropria
1d097 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  te collating fun
1d098 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
1d099 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65  in.** P4 is  use
1d09a 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70  d to do the comp
1d09b 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69  arison.  If P4 i
1d09c 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  s not specified 
1d09d 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29  then.** memcmp()
1d09e 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70   is used to comp
1d09f 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e  are text string.
1d0a0 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
1d0a1 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c   are.** numeric,
1d0a2 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20   then a numeric 
1d0a3 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73  comparison is us
1d0a4 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76  ed. If the two v
1d0a5 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20  alues.** are of 
1d0a6 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c  different types,
1d0a7 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72   then numbers ar
1d0a8 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
1d0a9 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67  s than.** string
1d0aa 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72  s and strings ar
1d0ab 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
1d0ac 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a  s than blobs..**
1d0ad 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54  .** If the SQLIT
1d0ae 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66  E_STOREP2 bit of
1d0af 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e   P5 is set, then
1d0b0 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49   do not jump.  I
1d0b1 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65  nstead,.** store
1d0b2 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c   a boolean resul
1d0b3 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20  t (either 0, or 
1d0b4 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72  1, or NULL) in r
1d0b5 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f  egister P2..*/./
1d0b6 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20  * Opcode: Ne P1 
1d0b7 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1d0b8 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73  * This works jus
1d0b9 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70  t like the Lt op
1d0ba 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74  code except that
1d0bb 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b   the jump is tak
1d0bc 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65  en if.** the ope
1d0bd 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65  rands in registe
1d0be 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65  rs P1 and P3 are
1d0bf 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65   not equal.  See
1d0c0 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66   the Lt opcode f
1d0c1 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c  or.** additional
1d0c2 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
1d0c3 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55  .** If SQLITE_NU
1d0c4 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 50  LLEQ is set in P
1d0c5 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  5 then the resul
1d0c6 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20  t of comparison 
1d0c7 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72  is always either
1d0c8 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c 73  .** true or fals
1d0c9 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20 4e  e and is never N
1d0ca 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f 70  ULL.  If both op
1d0cb 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20  erands are NULL 
1d0cc 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a  then the result.
1d0cd 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e  ** of comparison
1d0ce 20 69 73 20 66 61 6c 73 65 2e 20 20 49 66 20 65   is false.  If e
1d0cf 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1d0d0 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72   NULL then the r
1d0d1 65 73 75 6c 74 20 69 73 20 74 72 75 65 2e 0a 2a  esult is true..*
1d0d2 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 70 65  * If neither ope
1d0d3 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65  rand is NULL the
1d0d4 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 74   the result is t
1d0d5 68 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f  he same as it wo
1d0d6 75 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68 65  uld be if.** the
1d0d7 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66   SQLITE_NULLEQ f
1d0d8 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64  lag were omitted
1d0d9 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20   from P5..*/./* 
1d0da 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32  Opcode: Eq P1 P2
1d0db 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1d0dc 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20  This works just 
1d0dd 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f  like the Lt opco
1d0de 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74  de except that t
1d0df 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1d0e0 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61   if.** the opera
1d0e1 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73  nds in registers
1d0e2 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 65   P1 and P3 are e
1d0e3 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65  qual..** See the
1d0e4 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61   Lt opcode for a
1d0e5 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1d0e6 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ation..**.** If 
1d0e7 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73  SQLITE_NULLEQ is
1d0e8 20 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20   set in P5 then 
1d0e9 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f  the result of co
1d0ea 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61  mparison is alwa
1d0eb 79 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75  ys either.** tru
1d0ec 65 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69  e or false and i
1d0ed 73 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49  s never NULL.  I
1d0ee 66 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20  f both operands 
1d0ef 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68  are NULL then th
1d0f0 65 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63  e result.** of c
1d0f1 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 72 75  omparison is tru
1d0f2 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70  e.  If either op
1d0f3 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68  erand is NULL th
1d0f4 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
1d0f5 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65   false..** If ne
1d0f6 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1d0f7 20 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 72 65   NULL the the re
1d0f8 73 75 6c 74 20 69 73 20 74 68 65 20 73 61 6d 65  sult is the same
1d0f9 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20   as it would be 
1d0fa 69 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45  if.** the SQLITE
1d0fb 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 65 72  _NULLEQ flag wer
1d0fc 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 50  e omitted from P
1d0fd 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  5..*/./* Opcode:
1d0fe 20 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20   Le P1 P2 P3 P4 
1d0ff 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
1d100 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
1d101 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
1d102 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
1d103 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20   is taken if.** 
1d104 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1d105 65 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65  egister P3 is le
1d106 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
1d107 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   to the content 
1d108 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  of.** register P
1d109 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f  1.  See the Lt o
1d10a 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
1d10b 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1d10c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1d10d 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Gt P1 P2 P3 P4 P
1d10e 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  5.**.** This wor
1d10f 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  ks just like the
1d110 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70   Lt opcode excep
1d111 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20  t that the jump 
1d112 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74  is taken if.** t
1d113 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1d114 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65  gister P3 is gre
1d115 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f  ater than the co
1d116 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69  ntent of.** regi
1d117 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68  ster P1.  See th
1d118 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20  e Lt opcode for 
1d119 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
1d11a 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70  mation..*/./* Op
1d11b 63 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50  code: Ge P1 P2 P
1d11c 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68  3 P4 P5.**.** Th
1d11d 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69  is works just li
1d11e 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65  ke the Lt opcode
1d11f 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
1d120 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69   jump is taken i
1d121 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  f.** the content
1d122 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20   of register P3 
1d123 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1d124 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
1d125 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65  content of.** re
1d126 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20  gister P1.  See 
1d127 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f  the Lt opcode fo
1d128 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
1d129 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  ormation..*/.cas
1d12a 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20  e OP_Eq:        
1d12b 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1d12c 73 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69  s TK_EQ, jump, i
1d12d 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  n1, in3 */.case 
1d12e 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20  OP_Ne:          
1d12f 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d130 54 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31  TK_NE, jump, in1
1d131 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50  , in3 */.case OP
1d132 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  _Lt:            
1d133 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1d134 5f 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20  _LT, jump, in1, 
1d135 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c  in3 */.case OP_L
1d136 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e:              
1d137 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
1d138 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e  E, jump, in1, in
1d139 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a  3 */.case OP_Gt:
1d13a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d13b 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c  * same as TK_GT,
1d13c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
1d13d 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b  */.case OP_Ge: {
1d13e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d13f 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a  same as TK_GE, j
1d140 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
1d141 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d142 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d143 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 20 20   into u.ai */.  
1d144 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 20 20  int res;        
1d145 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66      /* Result of
1d146 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1d147 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73 74 20  of pIn1 against 
1d148 70 49 6e 33 20 2a 2f 0a 20 20 63 68 61 72 20 61  pIn3 */.  char a
1d149 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a  ffinity;      /*
1d14a 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65   Affinity to use
1d14b 20 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20   for comparison 
1d14c 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1d14d 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d14e 65 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a  ed into u.ai */.
1d14f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
1d150 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33  pOp->p1];.  pIn3
1d151 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
1d152 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  ];.  if( (pIn1->
1d153 66 6c 61 67 73 20 7c 20 70 49 6e 33 2d 3e 66 6c  flags | pIn3->fl
1d154 61 67 73 29 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b  ags)&MEM_Null ){
1d155 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 62  .    /* One or b
1d156 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65  oth operands are
1d157 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 69 66 28   NULL */.    if(
1d158 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54   pOp->p5 & SQLIT
1d159 45 5f 4e 55 4c 4c 45 51 20 29 7b 0a 20 20 20 20  E_NULLEQ ){.    
1d15a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e    /* If SQLITE_N
1d15b 55 4c 4c 45 51 20 69 73 20 73 65 74 20 28 77 68  ULLEQ is set (wh
1d15c 69 63 68 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 61  ich will only ha
1d15d 70 70 65 6e 20 69 66 20 74 68 65 20 6f 70 65 72  ppen if the oper
1d15e 61 74 6f 72 20 69 73 0a 20 20 20 20 20 20 2a 2a  ator is.      **
1d15f 20 4f 50 5f 45 71 20 6f 72 20 4f 50 5f 4e 65 29   OP_Eq or OP_Ne)
1d160 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a   then take the j
1d161 75 6d 70 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e  ump or not depen
1d162 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0a  ding on whether.
1d163 20 20 20 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20        ** or not 
1d164 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72  both operands ar
1d165 65 20 6e 75 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f  e null..      */
1d166 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1d167 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45  Op->opcode==OP_E
1d168 71 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  q || pOp->opcode
1d169 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20  ==OP_Ne );.     
1d16a 20 75 2e 61 69 2e 72 65 73 20 3d 20 28 70 49 6e   u.ai.res = (pIn
1d16b 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e 33 2d  1->flags & pIn3-
1d16c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1d16d 6c 29 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65  l)==0;.    }else
1d16e 7b 0a 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54  {.      /* SQLIT
1d16f 45 5f 4e 55 4c 4c 45 51 20 69 73 20 63 6c 65 61  E_NULLEQ is clea
1d170 72 20 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f  r and at least o
1d171 6e 65 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55  ne operand is NU
1d172 4c 4c 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  LL,.      ** the
1d173 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  n the result is 
1d174 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 20  always NULL..   
1d175 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69     ** The jump i
1d176 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53  s taken if the S
1d177 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1d178 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20   bit is set..   
1d179 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
1d17a 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45  pOp->p5 & SQLITE
1d17b 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20  _STOREP2 ){.    
1d17c 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d      pOut = &aMem
1d17d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20  [pOp->p2];.     
1d17e 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1d17f 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c  g(pOut, MEM_Null
1d180 29 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53  );.        REGIS
1d181 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1d182 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20  2, pOut);.      
1d183 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70  }else if( pOp->p
1d184 35 20 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  5 & SQLITE_JUMPI
1d185 46 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20  FNULL ){.       
1d186 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b   pc = pOp->p2-1;
1d187 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1d188 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  reak;.    }.  }e
1d189 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74  lse{.    /* Neit
1d18a 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e  her operand is N
1d18b 55 4c 4c 2e 20 20 44 6f 20 61 20 63 6f 6d 70 61  ULL.  Do a compa
1d18c 72 69 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 75 2e  rison. */.    u.
1d18d 61 69 2e 61 66 66 69 6e 69 74 79 20 3d 20 70 4f  ai.affinity = pO
1d18e 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 41  p->p5 & SQLITE_A
1d18f 46 46 5f 4d 41 53 4b 3b 0a 20 20 20 20 69 66 28  FF_MASK;.    if(
1d190 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 20 29   u.ai.affinity )
1d191 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 66  {.      applyAff
1d192 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61 69  inity(pIn1, u.ai
1d193 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64  .affinity, encod
1d194 69 6e 67 29 3b 0a 20 20 20 20 20 20 61 70 70 6c  ing);.      appl
1d195 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c 20  yAffinity(pIn3, 
1d196 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20 65  u.ai.affinity, e
1d197 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 20 20  ncoding);.      
1d198 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1d199 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  iled ) goto no_m
1d19a 65 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61  em;.    }..    a
1d19b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1d19c 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c  pe==P4_COLLSEQ |
1d19d 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d  | pOp->p4.pColl=
1d19e 3d 30 20 29 3b 0a 20 20 20 20 45 78 70 61 6e 64  =0 );.    Expand
1d19f 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 20 20  Blob(pIn1);.    
1d1a0 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29  ExpandBlob(pIn3)
1d1a1 3b 0a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d  ;.    u.ai.res =
1d1a2 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61   sqlite3MemCompa
1d1a3 72 65 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70  re(pIn3, pIn1, p
1d1a4 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20  Op->p4.pColl);. 
1d1a5 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70   }.  switch( pOp
1d1a6 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20  ->opcode ){.    
1d1a7 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 75  case OP_Eq:    u
1d1a8 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72  .ai.res = u.ai.r
1d1a9 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 6b  es==0;     break
1d1aa 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e 65  ;.    case OP_Ne
1d1ab 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20  :    u.ai.res = 
1d1ac 75 2e 61 69 2e 72 65 73 21 3d 30 3b 20 20 20 20  u.ai.res!=0;    
1d1ad 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
1d1ae 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 69 2e   OP_Lt:    u.ai.
1d1af 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c 30  res = u.ai.res<0
1d1b0 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;      break;.  
1d1b1 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20    case OP_Le:   
1d1b2 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69   u.ai.res = u.ai
1d1b3 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 65  .res<=0;     bre
1d1b4 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f  ak;.    case OP_
1d1b5 47 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20  Gt:    u.ai.res 
1d1b6 3d 20 75 2e 61 69 2e 72 65 73 3e 30 3b 20 20 20  = u.ai.res>0;   
1d1b7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
1d1b8 66 61 75 6c 74 3a 20 20 20 20 20 20 20 75 2e 61  fault:       u.a
1d1b9 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73  i.res = u.ai.res
1d1ba 3e 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a  >=0;     break;.
1d1bb 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e    }..  if( pOp->
1d1bc 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52  p5 & SQLITE_STOR
1d1bd 45 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 20  EP2 ){.    pOut 
1d1be 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  = &aMem[pOp->p2]
1d1bf 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
1d1c0 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1d1c1 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  nt);.    pOut->u
1d1c2 2e 69 20 3d 20 75 2e 61 69 2e 72 65 73 3b 0a 20  .i = u.ai.res;. 
1d1c3 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
1d1c4 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29  E(pOp->p2, pOut)
1d1c5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e  ;.  }else if( u.
1d1c6 61 69 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63  ai.res ){.    pc
1d1c7 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20   = pOp->p2-1;.  
1d1c8 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1d1c9 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61   Opcode: Permuta
1d1ca 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a  tion * * * P4 *.
1d1cb 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65  **.** Set the pe
1d1cc 72 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20 62  rmutation used b
1d1cd 79 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65  y the OP_Compare
1d1ce 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20   operator to be 
1d1cf 74 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20  the array.** of 
1d1d0 69 6e 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a  integers in P4..
1d1d1 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74  **.** The permut
1d1d2 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61  ation is only va
1d1d3 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65  lid until the ne
1d1d4 78 74 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f  xt OP_Permutatio
1d1d5 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a  n, OP_Compare,.*
1d1d6 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50  * OP_Halt, or OP
1d1d7 5f 52 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70  _ResultRow.  Typ
1d1d8 69 63 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65  ically the OP_Pe
1d1d9 72 6d 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64  rmutation should
1d1da 20 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69   occur.** immedi
1d1db 61 74 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74  ately prior to t
1d1dc 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a  he OP_Compare..*
1d1dd 2f 0a 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74  /.case OP_Permut
1d1de 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72  ation: {.  asser
1d1df 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1d1e0 50 34 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20  P4_INTARRAY );. 
1d1e1 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1d1e2 2e 61 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74  .ai );.  aPermut
1d1e3 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a  e = pOp->p4.ai;.
1d1e4 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d1e5 70 63 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50  pcode: Compare P
1d1e6 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1d1e7 2a 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65  ** Compare to ve
1d1e8 63 74 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65  ctors of registe
1d1e9 72 73 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72  rs in reg(P1)..r
1d1ea 65 67 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c  eg(P1+P3-1) (all
1d1eb 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22   this.** one "A"
1d1ec 29 20 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29  ) and in reg(P2)
1d1ed 2e 2e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28  ..reg(P2+P3-1) (
1d1ee 22 42 22 29 2e 20 20 53 61 76 65 20 74 68 65 20  "B").  Save the 
1d1ef 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65  result of.** the
1d1f0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20   comparison for 
1d1f1 75 73 65 20 62 79 20 74 68 65 20 6e 65 78 74 20  use by the next 
1d1f2 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74  OP_Jump instruct
1d1f3 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  ..**.** P4 is a 
1d1f4 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
1d1f5 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 63  e that defines c
1d1f6 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1d1f7 65 73 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f  es and sort.** o
1d1f8 72 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f  rders for the co
1d1f9 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70  mparison.  The p
1d1fa 65 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69  ermutation appli
1d1fb 65 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a  es to registers.
1d1fc 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65  ** only.  The Ke
1d1fd 79 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61  yInfo elements a
1d1fe 72 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69  re used sequenti
1d1ff 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ally..**.** The 
1d200 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20  comparison is a 
1d201 73 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c  sort comparison,
1d202 20 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72   so NULLs compar
1d203 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c  e equal,.** NULL
1d204 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20  s are less than 
1d205 6e 75 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73  numbers, numbers
1d206 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73   are less than s
1d207 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73  trings,.** and s
1d208 74 72 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20  trings are less 
1d209 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63  than blobs..*/.c
1d20a 61 73 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20  ase OP_Compare: 
1d20b 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1d20c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d20d 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20  d into u.aj */. 
1d20e 20 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b   int n;.  int i;
1d20f 0a 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74  .  int p1;.  int
1d210 20 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79   p2;.  const Key
1d211 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a  Info *pKeyInfo;.
1d212 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 43 6f 6c    int idx;.  Col
1d213 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20  lSeq *pColl;    
1d214 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
1d215 75 65 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20  uence to use on 
1d216 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69  this term */.  i
1d217 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20  nt bRev;        
1d218 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45    /* True for DE
1d219 53 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72  SCENDING sort or
1d21a 64 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  der */.#endif /*
1d21b 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1d21c 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a   moved into u.aj
1d21d 20 2a 2f 0a 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20   */..  u.aj.n = 
1d21e 70 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e  pOp->p3;.  u.aj.
1d21f 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e  pKeyInfo = pOp->
1d220 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61  p4.pKeyInfo;.  a
1d221 73 73 65 72 74 28 20 75 2e 61 6a 2e 6e 3e 30 20  ssert( u.aj.n>0 
1d222 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  );.  assert( u.a
1d223 6a 2e 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b  j.pKeyInfo!=0 );
1d224 0a 20 20 75 2e 61 6a 2e 70 31 20 3d 20 70 4f 70  .  u.aj.p1 = pOp
1d225 2d 3e 70 31 3b 0a 20 20 75 2e 61 6a 2e 70 32 20  ->p1;.  u.aj.p2 
1d226 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 69 66 20 53  = pOp->p2;.#if S
1d227 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66  QLITE_DEBUG.  if
1d228 28 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20  ( aPermute ){.  
1d229 20 20 69 6e 74 20 6b 2c 20 6d 78 20 3d 20 30 3b    int k, mx = 0;
1d22a 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c  .    for(k=0; k<
1d22b 75 2e 61 6a 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28  u.aj.n; k++) if(
1d22c 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20   aPermute[k]>mx 
1d22d 29 20 6d 78 20 3d 20 61 50 65 72 6d 75 74 65 5b  ) mx = aPermute[
1d22e 6b 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  k];.    assert( 
1d22f 75 2e 61 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61  u.aj.p1>0 && u.a
1d230 6a 2e 70 31 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d  j.p1+mx<=p->nMem
1d231 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +1 );.    assert
1d232 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75  ( u.aj.p2>0 && u
1d233 2e 61 6a 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d  .aj.p2+mx<=p->nM
1d234 65 6d 2b 31 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  em+1 );.  }else{
1d235 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61  .    assert( u.a
1d236 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70  j.p1>0 && u.aj.p
1d237 31 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65  1+u.aj.n<=p->nMe
1d238 6d 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  m+1 );.    asser
1d239 74 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20  t( u.aj.p2>0 && 
1d23a 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d  u.aj.p2+u.aj.n<=
1d23b 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d  p->nMem+1 );.  }
1d23c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1d23d 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 66 6f 72  E_DEBUG */.  for
1d23e 28 75 2e 61 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e  (u.aj.i=0; u.aj.
1d23f 69 3c 75 2e 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69  i<u.aj.n; u.aj.i
1d240 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6a 2e 69 64  ++){.    u.aj.id
1d241 78 20 3d 20 61 50 65 72 6d 75 74 65 20 3f 20 61  x = aPermute ? a
1d242 50 65 72 6d 75 74 65 5b 75 2e 61 6a 2e 69 5d 20  Permute[u.aj.i] 
1d243 3a 20 75 2e 61 6a 2e 69 3b 0a 20 20 20 20 52 45  : u.aj.i;.    RE
1d244 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61  GISTER_TRACE(u.a
1d245 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 2c 20 26  j.p1+u.aj.idx, &
1d246 61 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61  aMem[u.aj.p1+u.a
1d247 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47  j.idx]);.    REG
1d248 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a  ISTER_TRACE(u.aj
1d249 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 61  .p2+u.aj.idx, &a
1d24a 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a  Mem[u.aj.p2+u.aj
1d24b 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65  .idx]);.    asse
1d24c 72 74 28 20 75 2e 61 6a 2e 69 3c 75 2e 61 6a 2e  rt( u.aj.i<u.aj.
1d24d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1d24e 20 29 3b 0a 20 20 20 20 75 2e 61 6a 2e 70 43 6f   );.    u.aj.pCo
1d24f 6c 6c 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e  ll = u.aj.pKeyIn
1d250 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6a 2e 69  fo->aColl[u.aj.i
1d251 5d 3b 0a 20 20 20 20 75 2e 61 6a 2e 62 52 65 76  ];.    u.aj.bRev
1d252 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f   = u.aj.pKeyInfo
1d253 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75 2e 61  ->aSortOrder[u.a
1d254 6a 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 61  j.i];.    iCompa
1d255 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43  re = sqlite3MemC
1d256 6f 6d 70 61 72 65 28 26 61 4d 65 6d 5b 75 2e 61  ompare(&aMem[u.a
1d257 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20  j.p1+u.aj.idx], 
1d258 26 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e  &aMem[u.aj.p2+u.
1d259 61 6a 2e 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43  aj.idx], u.aj.pC
1d25a 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43  oll);.    if( iC
1d25b 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20  ompare ){.      
1d25c 69 66 28 20 75 2e 61 6a 2e 62 52 65 76 20 29 20  if( u.aj.bRev ) 
1d25d 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d  iCompare = -iCom
1d25e 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61  pare;.      brea
1d25f 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  k;.    }.  }.  a
1d260 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62  Permute = 0;.  b
1d261 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d262 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50  de: Jump P1 P2 P
1d263 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1d264 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74   to the instruct
1d265 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50  ion at address P
1d266 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70  1, P2, or P3 dep
1d267 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
1d268 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74  r.** in the most
1d269 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61   recent OP_Compa
1d26a 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  re instruction t
1d26b 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73  he P1 vector was
1d26c 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71   less than.** eq
1d26d 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
1d26e 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76  er than the P2 v
1d26f 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76  ector, respectiv
1d270 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ely..*/.case OP_
1d271 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20  Jump: {         
1d272 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
1d273 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20   if( iCompare<0 
1d274 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1d275 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p1 - 1;.  }else
1d276 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30   if( iCompare==0
1d277 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1d278 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73  ->p2 - 1;.  }els
1d279 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  e{.    pc = pOp-
1d27a 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p3 - 1;.  }.  b
1d27b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d27c 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33  de: And P1 P2 P3
1d27d 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20   * *.**.** Take 
1d27e 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20  the logical AND 
1d27f 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
1d280 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1d281 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74  d P2 and.** writ
1d282 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  e the result int
1d283 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  o register P3..*
1d284 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50  *.** If either P
1d285 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61  1 or P2 is 0 (fa
1d286 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65  lse) then the re
1d287 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69  sult is 0 even i
1d288 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69  f.** the other i
1d289 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41  nput is NULL.  A
1d28a 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f   NULL and true o
1d28b 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65  r two NULLs give
1d28c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75  .** a NULL outpu
1d28d 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  t..*/./* Opcode:
1d28e 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a   Or P1 P2 P3 * *
1d28f 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20  .**.** Take the 
1d290 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68  logical OR of th
1d291 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1d292 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
1d293 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
1d294 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74  answer in regist
1d295 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
1d296 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20  either P1 or P2 
1d297 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65  is nonzero (true
1d298 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  ) then the resul
1d299 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a  t is 1 (true).**
1d29a 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68   even if the oth
1d29b 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1d29c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61  .  A NULL and fa
1d29d 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73  lse or two NULLs
1d29e 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20  .** give a NULL 
1d29f 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20  output..*/.case 
1d2a0 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20  OP_And:         
1d2a1 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d2a2 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32  TK_AND, in1, in2
1d2a3 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1d2a4 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Or: {         
1d2a5 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d2a6 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  K_OR, in1, in2, 
1d2a7 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f  out3 */.#if 0  /
1d2a8 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d2a9 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d2aa 6b 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20  k */.  int v1;  
1d2ab 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e    /* Left operan
1d2ac 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d  d:  0==FALSE, 1=
1d2ad 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57  =TRUE, 2==UNKNOW
1d2ae 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69  N or NULL */.  i
1d2af 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67  nt v2;    /* Rig
1d2b0 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46  ht operand: 0==F
1d2b1 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1d2b2 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1d2b3 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  L */.#endif /* l
1d2b4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d2b5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a  oved into u.ak *
1d2b6 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  /..  pIn1 = &aMe
1d2b7 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66  m[pOp->p1];.  if
1d2b8 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
1d2b9 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1d2ba 75 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d  u.ak.v1 = 2;.  }
1d2bb 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76  else{.    u.ak.v
1d2bc 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  1 = sqlite3VdbeI
1d2bd 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30  ntValue(pIn1)!=0
1d2be 3b 0a 20 20 7d 0a 20 20 70 49 6e 32 20 3d 20 26  ;.  }.  pIn2 = &
1d2bf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1d2c0 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73   if( pIn2->flags
1d2c1 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1d2c2 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a     u.ak.v2 = 2;.
1d2c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61    }else{.    u.a
1d2c4 6b 2e 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64  k.v2 = sqlite3Vd
1d2c5 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29  beIntValue(pIn2)
1d2c6 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  !=0;.  }.  if( p
1d2c7 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41  Op->opcode==OP_A
1d2c8 6e 64 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  nd ){.    static
1d2c9 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1d2ca 63 68 61 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d  char and_logic[]
1d2cb 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c   = { 0, 0, 0, 0,
1d2cc 20 31 2c 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d   1, 2, 0, 2, 2 }
1d2cd 3b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20  ;.    u.ak.v1 = 
1d2ce 61 6e 64 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76  and_logic[u.ak.v
1d2cf 31 2a 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20  1*3+u.ak.v2];.  
1d2d0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69  }else{.    stati
1d2d1 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
1d2d2 20 63 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d   char or_logic[]
1d2d3 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c   = { 0, 1, 2, 1,
1d2d4 20 31 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d   1, 1, 2, 1, 2 }
1d2d5 3b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20  ;.    u.ak.v1 = 
1d2d6 6f 72 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31  or_logic[u.ak.v1
1d2d7 2a 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d  *3+u.ak.v2];.  }
1d2d8 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b  .  pOut = &aMem[
1d2d9 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20  pOp->p3];.  if( 
1d2da 75 2e 61 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20  u.ak.v1==2 ){.  
1d2db 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1d2dc 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  (pOut, MEM_Null)
1d2dd 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1d2de 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e  Out->u.i = u.ak.
1d2df 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  v1;.    MemSetTy
1d2e0 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1d2e1 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65  _Int);.  }.  bre
1d2e2 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1d2e3 3a 20 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20  : Not P1 P2 * * 
1d2e4 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  *.**.** Interpre
1d2e5 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  t the value in r
1d2e6 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 20  egister P1 as a 
1d2e7 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20  boolean value.  
1d2e8 53 74 6f 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f  Store the.** boo
1d2e9 6c 65 61 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20  lean complement 
1d2ea 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20  in register P2. 
1d2eb 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   If the value in
1d2ec 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1d2ed 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61  .** NULL, then a
1d2ee 20 4e 55 4c 4c 20 69 73 20 73 74 6f 72 65 64 20   NULL is stored 
1d2ef 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  in P2..*/.case O
1d2f0 50 5f 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20  P_Not: {        
1d2f1 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1d2f2 61 73 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31 2c 20  as TK_NOT, in1, 
1d2f3 6f 75 74 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d  out2 */.  pIn1 =
1d2f4 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
1d2f5 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b  .  pOut = &aMem[
1d2f6 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20  pOp->p2];.  if( 
1d2f7 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1d2f8 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71  M_Null ){.    sq
1d2f9 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1d2fa 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c  ull(pOut);.  }el
1d2fb 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  se{.    sqlite3V
1d2fc 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70  dbeMemSetInt64(p
1d2fd 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62  Out, !sqlite3Vdb
1d2fe 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29  eIntValue(pIn1))
1d2ff 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d300 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74  ../* Opcode: Bit
1d301 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Not P1 P2 * * *.
1d302 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  **.** Interpret 
1d303 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1d304 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e  egister P1 as an
1d305 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 65   integer.  Store
1d306 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d   the.** ones-com
1d307 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 50  plement of the P
1d308 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67  1 value into reg
1d309 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 31  ister P2.  If P1
1d30a 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c   holds.** a NULL
1d30b 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e 55   then store a NU
1d30c 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73  LL in P2..*/.cas
1d30d 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20  e OP_BitNot: {  
1d30e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1d30f 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c  me as TK_BITNOT,
1d310 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 20   in1, out2 */.  
1d311 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1d312 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20  ->p1];.  pOut = 
1d313 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  &aMem[pOp->p2];.
1d314 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67    if( pIn1->flag
1d315 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  s & MEM_Null ){.
1d316 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1d317 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b  emSetNull(pOut);
1d318 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
1d319 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
1d31a 6e 74 36 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69  nt64(pOut, ~sqli
1d31b 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
1d31c 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72  pIn1));.  }.  br
1d31d 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d31e 65 3a 20 49 66 20 50 31 20 50 32 20 50 33 20 2a  e: If P1 P2 P3 *
1d31f 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
1d320 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65   P2 if the value
1d321 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d322 69 73 20 74 72 75 65 2e 20 20 54 68 65 20 76 61  is true.  The va
1d323 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e  lue is.** is con
1d324 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20  sidered true if 
1d325 69 74 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e  it is numeric an
1d326 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20  d non-zero.  If 
1d327 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20  the value.** in 
1d328 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  P1 is NULL then 
1d329 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66  take the jump if
1d32a 20 50 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a   P3 is true..*/.
1d32b 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74  /* Opcode: IfNot
1d32c 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1d32d 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69  .** Jump to P2 i
1d32e 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  f the value in r
1d32f 65 67 69 73 74 65 72 20 50 31 20 69 73 20 46 61  egister P1 is Fa
1d330 6c 73 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20  lse.  The value 
1d331 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  is.** is conside
1d332 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 68  red true if it h
1d333 61 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c  as a numeric val
1d334 75 65 20 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20  ue of zero.  If 
1d335 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20  the value.** in 
1d336 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  P1 is NULL then 
1d337 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66  take the jump if
1d338 20 50 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a   P3 is true..*/.
1d339 63 61 73 65 20 4f 50 5f 49 66 3a 20 20 20 20 20  case OP_If:     
1d33a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
1d33b 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65  ump, in1 */.case
1d33c 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20   OP_IfNot: {    
1d33d 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1d33e 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in1 */.#if 0  /
1d33f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d340 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d341 6c 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65  l */.  int c;.#e
1d342 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1d343 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d344 74 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 70 49 6e  to u.al */.  pIn
1d345 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  1 = &aMem[pOp->p
1d346 31 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e  1];.  if( pIn1->
1d347 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1d348 20 29 7b 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d   ){.    u.al.c =
1d349 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73   pOp->p3;.  }els
1d34a 65 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  e{.#ifdef SQLITE
1d34b 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
1d34c 4f 49 4e 54 0a 20 20 20 20 75 2e 61 6c 2e 63 20  OINT.    u.al.c 
1d34d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
1d34e 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a  Value(pIn1)!=0;.
1d34f 23 65 6c 73 65 0a 20 20 20 20 75 2e 61 6c 2e 63  #else.    u.al.c
1d350 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1d351 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30  alValue(pIn1)!=0
1d352 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69  .0;.#endif.    i
1d353 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1d354 4f 50 5f 49 66 4e 6f 74 20 29 20 75 2e 61 6c 2e  OP_IfNot ) u.al.
1d355 63 20 3d 20 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d  c = !u.al.c;.  }
1d356 0a 20 20 69 66 28 20 75 2e 61 6c 2e 63 20 29 7b  .  if( u.al.c ){
1d357 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1d358 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  2-1;.  }.  break
1d359 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d35a 49 73 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a  IsNull P1 P2 * *
1d35b 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
1d35c 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65   P2 if the value
1d35d 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d35e 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65  is NULL..*/.case
1d35f 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20   OP_IsNull: {   
1d360 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1d361 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a   as TK_ISNULL, j
1d362 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49  ump, in1 */.  pI
1d363 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
1d364 70 31 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31  p1];.  if( (pIn1
1d365 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1d366 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 63  ll)!=0 ){.    pc
1d367 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1d368 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d369 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75  /* Opcode: NotNu
1d36a 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ll P1 P2 * * *.*
1d36b 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20  *.** Jump to P2 
1d36c 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  if the value in 
1d36d 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6e  register P1 is n
1d36e 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61  ot NULL.  .*/.ca
1d36f 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b  se OP_NotNull: {
1d370 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1d371 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c  ame as TK_NOTNUL
1d372 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  L, jump, in1 */.
1d373 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
1d374 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28  Op->p1];.  if( (
1d375 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1d376 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Null)==0 ){.  
1d377 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1d378 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1d379 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
1d37a 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 50  olumn P1 P2 P3 P
1d37b 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72  4 P5.**.** Inter
1d37c 70 72 65 74 20 74 68 65 20 64 61 74 61 20 74 68  pret the data th
1d37d 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69  at cursor P1 poi
1d37e 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74 72 75  nts to as a stru
1d37f 63 74 75 72 65 20 62 75 69 6c 74 20 75 73 69 6e  cture built usin
1d380 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52 65 63  g.** the MakeRec
1d381 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ord instruction.
1d382 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b 65 52    (See the MakeR
1d383 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66 6f 72  ecord opcode for
1d384 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69   additional.** i
1d385 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
1d386 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74   the format of t
1d387 68 65 20 64 61 74 61 2e 29 20 20 45 78 74 72 61  he data.)  Extra
1d388 63 74 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c  ct the P2-th col
1d389 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73  umn.** from this
1d38a 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74 68 65   record.  If the
1d38b 72 65 20 61 72 65 20 6c 65 73 73 20 74 68 61 74  re are less that
1d38c 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61 6c 75   (P2+1) .** valu
1d38d 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  es in the record
1d38e 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c  , extract a NULL
1d38f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ..**.** The valu
1d390 65 20 65 78 74 72 61 63 74 65 64 20 69 73 20 73  e extracted is s
1d391 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1d392 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  r P3..**.** If t
1d393 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69  he column contai
1d394 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20 50 32  ns fewer than P2
1d395 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 65 78   fields, then ex
1d396 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f  tract a NULL.  O
1d397 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50 34 20  r,.** if the P4 
1d398 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 50 34  argument is a P4
1d399 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76 61 6c  _MEM use the val
1d39a 75 65 20 6f 66 20 74 68 65 20 50 34 20 61 72 67  ue of the P4 arg
1d39b 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20  ument as.** the 
1d39c 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  result..**.** If
1d39d 20 74 68 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41   the OPFLAG_CLEA
1d39e 52 43 41 43 48 45 20 62 69 74 20 69 73 20 73 65  RCACHE bit is se
1d39f 74 20 6f 6e 20 50 35 20 61 6e 64 20 50 31 20 69  t on P5 and P1 i
1d3a0 73 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  s a pseudo-table
1d3a1 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 6e   cursor,.** then
1d3a2 20 74 68 65 20 63 61 63 68 65 20 6f 66 20 74 68   the cache of th
1d3a3 65 20 63 75 72 73 6f 72 20 69 73 20 72 65 73 65  e cursor is rese
1d3a4 74 20 70 72 69 6f 72 20 74 6f 20 65 78 74 72 61  t prior to extra
1d3a5 63 74 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e  cting the column
1d3a6 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 4f  ..** The first O
1d3a7 50 5f 43 6f 6c 75 6d 6e 20 61 67 61 69 6e 73 74  P_Column against
1d3a8 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20   a pseudo-table 
1d3a9 61 66 74 65 72 20 74 68 65 20 76 61 6c 75 65 20  after the value 
1d3aa 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a  of the content.*
1d3ab 2a 20 72 65 67 69 73 74 65 72 20 68 61 73 20 63  * register has c
1d3ac 68 61 6e 67 65 64 20 73 68 6f 75 6c 64 20 68 61  hanged should ha
1d3ad 76 65 20 74 68 69 73 20 62 69 74 20 73 65 74 2e  ve this bit set.
1d3ae 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75  .*/.case OP_Colu
1d3af 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  mn: {.#if 0  /* 
1d3b0 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d3b1 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6d 20  moved into u.am 
1d3b2 2a 2f 0a 20 20 75 33 32 20 70 61 79 6c 6f 61 64  */.  u32 payload
1d3b3 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65  Size;   /* Numbe
1d3b4 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
1d3b5 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 36  e record */.  i6
1d3b6 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b  4 payloadSize64;
1d3b7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1d3b8 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
1d3b9 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20  d */.  int p1;  
1d3ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20            /* P1 
1d3bb 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63  value of the opc
1d3bc 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b  ode */.  int p2;
1d3bd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63              /* c
1d3be 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20  olumn number to 
1d3bf 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 56 64  retrieve */.  Vd
1d3c0 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20  beCursor *pC;   
1d3c1 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
1d3c2 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  sor */.  char *z
1d3c3 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  Rec;        /* P
1d3c4 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65  ointer to comple
1d3c5 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a  te record-data *
1d3c6 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
1d3c7 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54  rsr;   /* The BT
1d3c8 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  ree cursor */.  
1d3c9 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20  u32 *aType;     
1d3ca 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68     /* aType[i] h
1d3cb 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 63  olds the numeric
1d3cc 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d 74   type of the i-t
1d3cd 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33  h column */.  u3
1d3ce 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20  2 *aOffset;     
1d3cf 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d 20 69   /* aOffset[i] i
1d3d0 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 61 72  s offset to star
1d3d1 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 69 2d  t of data for i-
1d3d2 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  th column */.  i
1d3d3 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
1d3d4 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66    /* number of f
1d3d5 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63  ields in the rec
1d3d6 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e  ord */.  int len
1d3d7 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
1d3d8 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
1d3d9 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61   serialized data
1d3da 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20   for the column 
1d3db 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
1d3dc 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1d3dd 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61  counter */.  cha
1d3de 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20  r *zData;       
1d3df 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 20 72  /* Part of the r
1d3e0 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f  ecord being deco
1d3e1 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44  ded */.  Mem *pD
1d3e2 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57  est;        /* W
1d3e3 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68  here to write th
1d3e4 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75  e extracted valu
1d3e5 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b  e */.  Mem sMem;
1d3e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
1d3e7 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65 63   storing the rec
1d3e8 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65  ord being decode
1d3e9 64 20 2a 2f 0a 20 20 75 38 20 2a 7a 49 64 78 3b  d */.  u8 *zIdx;
1d3ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
1d3eb 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20 2a  ex into header *
1d3ec 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b  /.  u8 *zEndHdr;
1d3ed 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1d3ee 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20  r to first byte 
1d3ef 61 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72  after the header
1d3f0 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74   */.  u32 offset
1d3f1 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73  ;        /* Offs
1d3f2 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  et into the data
1d3f3 20 2a 2f 0a 20 20 75 36 34 20 6f 66 66 73 65 74   */.  u64 offset
1d3f4 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 2d 62  64;      /* 64-b
1d3f5 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 20 62  it offset.  64 b
1d3f6 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 63 61  its needed to ca
1d3f7 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a  tch overflow */.
1d3f8 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 20 20    int szHdr;    
1d3f9 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1d3fa 74 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20  the header size 
1d3fb 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f  field at start o
1d3fc 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  f record */.  in
1d3fd 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 20  t avail;        
1d3fe 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1d3ff 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65  tes of available
1d400 20 64 61 74 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a   data */.  Mem *
1d401 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  pReg;         /*
1d402 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70   PseudoTable inp
1d403 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23  ut register */.#
1d404 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1d405 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1d406 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 0a 0a 20 20  nto u.am */...  
1d407 75 2e 61 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70  u.am.p1 = pOp->p
1d408 31 3b 0a 20 20 75 2e 61 6d 2e 70 32 20 3d 20 70  1;.  u.am.p2 = p
1d409 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6d 2e 70  Op->p2;.  u.am.p
1d40a 43 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28  C = 0;.  memset(
1d40b 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 30 2c 20 73  &u.am.sMem, 0, s
1d40c 69 7a 65 6f 66 28 75 2e 61 6d 2e 73 4d 65 6d 29  izeof(u.am.sMem)
1d40d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  );.  assert( u.a
1d40e 6d 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  m.p1<p->nCursor 
1d40f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1d410 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
1d411 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1d412 75 2e 61 6d 2e 70 44 65 73 74 20 3d 20 26 61 4d  u.am.pDest = &aM
1d413 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d  em[pOp->p3];.  M
1d414 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 75 2e  emSetTypeFlag(u.
1d415 61 6d 2e 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75  am.pDest, MEM_Nu
1d416 6c 6c 29 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63  ll);.  u.am.zRec
1d417 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73   = 0;..  /* This
1d418 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20   block sets the 
1d419 76 61 72 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61  variable u.am.pa
1d41a 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20  yloadSize to be 
1d41b 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
1d41c 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69   of.  ** bytes i
1d41d 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20  n the record..  
1d41e 2a 2a 0a 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65  **.  ** u.am.zRe
1d41f 63 20 69 73 20 73 65 74 20 74 6f 20 62 65 20 74  c is set to be t
1d420 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74  he complete text
1d421 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 69   of the record i
1d422 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c  f it is availabl
1d423 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70  e..  ** The comp
1d424 6c 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74  lete record text
1d425 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 69 6c   is always avail
1d426 61 62 6c 65 20 66 6f 72 20 70 73 65 75 64 6f 2d  able for pseudo-
1d427 74 61 62 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74  tables.  ** If t
1d428 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 74 6f  he record is sto
1d429 72 65 64 20 69 6e 20 61 20 63 75 72 73 6f 72 2c  red in a cursor,
1d42a 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 65   the complete re
1d42b 63 6f 72 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d  cord text.  ** m
1d42c 69 67 68 74 20 62 65 20 61 76 61 69 6c 61 62 6c  ight be availabl
1d42d 65 20 69 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70  e in the  u.am.p
1d42e 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
1d42f 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20  Or it might not 
1d430 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20  be..  ** If the 
1d431 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61  data is unavaila
1d432 62 6c 65 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20  ble,  u.am.zRec 
1d433 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a  is set to NULL..
1d434 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73    **.  ** We als
1d435 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 75  o compute the nu
1d436 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1d437 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20 20  in the record.  
1d438 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a  For cursors,.  *
1d439 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
1d43a 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 65  columns is store
1d43b 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75 72  d in the VdbeCur
1d43c 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65  sor.nField eleme
1d43d 6e 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e  nt..  */.  u.am.
1d43e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e  pC = p->apCsr[u.
1d43f 61 6d 2e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  am.p1];.  assert
1d440 28 20 75 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a  ( u.am.pC!=0 );.
1d441 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1d442 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1d443 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e  .  assert( u.am.
1d444 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d  pC->pVtabCursor=
1d445 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75  =0 );.#endif.  u
1d446 2e 61 6d 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d  .am.pCrsr = u.am
1d447 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  .pC->pCursor;.  
1d448 69 66 28 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d  if( u.am.pCrsr!=
1d449 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  0 ){.    /* The 
1d44a 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64  record is stored
1d44b 20 69 6e 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a   in a B-Tree */.
1d44c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d44d 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f  VdbeCursorMoveto
1d44e 28 75 2e 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69  (u.am.pC);.    i
1d44f 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f  f( rc ) goto abo
1d450 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1d451 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 43  .    if( u.am.pC
1d452 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20  ->nullRow ){.   
1d453 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53     u.am.payloadS
1d454 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ize = 0;.    }el
1d455 73 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e  se if( u.am.pC->
1d456 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e  cacheStatus==p->
1d457 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20  cacheCtr ){.    
1d458 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69    u.am.payloadSi
1d459 7a 65 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61  ze = u.am.pC->pa
1d45a 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20  yloadSize;.     
1d45b 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68   u.am.zRec = (ch
1d45c 61 72 2a 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f  ar*)u.am.pC->aRo
1d45d 77 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  w;.    }else if(
1d45e 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65   u.am.pC->isInde
1d45f 78 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  x ){.      asser
1d460 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  t( sqlite3BtreeC
1d461 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 61  ursorIsValid(u.a
1d462 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20 20 20  m.pCrsr) );.    
1d463 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1d464 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e  reeKeySize(u.am.
1d465 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79  pCrsr, &u.am.pay
1d466 6c 6f 61 64 53 69 7a 65 36 34 29 3b 0a 20 20 20  loadSize64);.   
1d467 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
1d468 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a  QLITE_OK );   /*
1d469 20 54 72 75 65 20 62 65 63 61 75 73 65 20 6f 66   True because of
1d46a 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20   CursorMoveto() 
1d46b 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20  call above */.  
1d46c 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74      /* sqlite3Bt
1d46d 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
1d46e 29 20 75 73 65 73 20 67 65 74 56 61 72 69 6e 74  ) uses getVarint
1d46f 33 32 28 29 20 74 6f 20 65 78 74 72 61 63 74 20  32() to extract 
1d470 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 79  the.      ** pay
1d471 6c 6f 61 64 20 73 69 7a 65 2c 20 73 6f 20 69 74  load size, so it
1d472 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66   is impossible f
1d473 6f 72 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  or u.am.payloadS
1d474 69 7a 65 36 34 20 74 6f 20 62 65 0a 20 20 20 20  ize64 to be.    
1d475 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e    ** larger than
1d476 20 33 32 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20   32 bits. */.   
1d477 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 61 6d     assert( (u.am
1d478 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 26  .payloadSize64 &
1d479 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29   SQLITE_MAX_U32)
1d47a 3d 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c  ==(u64)u.am.payl
1d47b 6f 61 64 53 69 7a 65 36 34 20 29 3b 0a 20 20 20  oadSize64 );.   
1d47c 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53     u.am.payloadS
1d47d 69 7a 65 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e  ize = (u32)u.am.
1d47e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20  payloadSize64;. 
1d47f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d480 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1d481 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69  treeCursorIsVali
1d482 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b  d(u.am.pCrsr) );
1d483 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1d484 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65  te3BtreeDataSize
1d485 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e  (u.am.pCrsr, &u.
1d486 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b  am.payloadSize);
1d487 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
1d488 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20  c==SQLITE_OK ); 
1d489 20 20 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20    /* DataSize() 
1d48a 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20  cannot fail */. 
1d48b 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
1d48c 20 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f   u.am.pC->pseudo
1d48d 54 61 62 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20  TableReg>0 ){.  
1d48e 20 20 75 2e 61 6d 2e 70 52 65 67 20 3d 20 26 61    u.am.pReg = &a
1d48f 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65  Mem[u.am.pC->pse
1d490 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20  udoTableReg];.  
1d491 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70    assert( u.am.p
1d492 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Reg->flags & MEM
1d493 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61  _Blob );.    u.a
1d494 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20  m.payloadSize = 
1d495 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20  u.am.pReg->n;.  
1d496 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e    u.am.zRec = u.
1d497 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20  am.pReg->z;.    
1d498 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d499 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26  atus = (pOp->p5&
1d49a 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48  OPFLAG_CLEARCACH
1d49b 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c 45  E) ? CACHE_STALE
1d49c 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a   : p->cacheCtr;.
1d49d 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d49e 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20  .payloadSize==0 
1d49f 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20  || u.am.zRec!=0 
1d4a0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1d4a1 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20  /* Consider the 
1d4a2 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a  row to be NULL *
1d4a3 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f  /.    u.am.paylo
1d4a4 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a  adSize = 0;.  }.
1d4a5 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61  .  /* If u.am.pa
1d4a6 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20  yloadSize is 0, 
1d4a7 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20  then just store 
1d4a8 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20  a NULL */.  if( 
1d4a9 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d4aa 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
1d4ab 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66  t( u.am.pDest->f
1d4ac 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b  lags&MEM_Null );
1d4ad 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c  .    goto op_col
1d4ae 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61  umn_out;.  }.  a
1d4af 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69  ssert( db->aLimi
1d4b0 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1d4b1 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69  ENGTH]>=0 );.  i
1d4b2 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  f( u.am.payloadS
1d4b3 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61  ize > (u32)db->a
1d4b4 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1d4b5 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1d4b6 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1d4b7 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65    }..  u.am.nFie
1d4b8 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46  ld = u.am.pC->nF
1d4b9 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20  ield;.  assert( 
1d4ba 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69  u.am.p2<u.am.nFi
1d4bb 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  eld );..  /* Rea
1d4bc 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20  d and parse the 
1d4bd 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53  table header.  S
1d4be 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
1d4bf 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20   of the parse.  
1d4c0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f  ** into the reco
1d4c1 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20  rd header cache 
1d4c2 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75  fields of the cu
1d4c3 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61  rsor..  */.  u.a
1d4c4 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70  m.aType = u.am.p
1d4c5 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20  C->aType;.  if( 
1d4c6 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d4c7 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74  atus==p->cacheCt
1d4c8 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f  r ){.    u.am.aO
1d4c9 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d  ffset = u.am.pC-
1d4ca 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73  >aOffset;.  }els
1d4cb 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e  e{.    assert(u.
1d4cc 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75  am.aType);.    u
1d4cd 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20  .am.avail = 0;. 
1d4ce 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66     u.am.pC->aOff
1d4cf 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73  set = u.am.aOffs
1d4d0 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65  et = &u.am.aType
1d4d1 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20  [u.am.nField];. 
1d4d2 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c     u.am.pC->payl
1d4d3 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70  oadSize = u.am.p
1d4d4 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20  ayloadSize;.    
1d4d5 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d4d6 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43  atus = p->cacheC
1d4d7 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75  tr;..    /* Figu
1d4d8 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
1d4d9 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65  bytes are in the
1d4da 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69   header */.    i
1d4db 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a  f( u.am.zRec ){.
1d4dc 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61        u.am.zData
1d4dd 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20   = u.am.zRec;.  
1d4de 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1d4df 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e  f( u.am.pC->isIn
1d4e0 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75  dex ){.        u
1d4e1 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61  .am.zData = (cha
1d4e2 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b  r*)sqlite3BtreeK
1d4e3 65 79 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 72  eyFetch(u.am.pCr
1d4e4 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29  sr, &u.am.avail)
1d4e5 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1d4e6 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74         u.am.zDat
1d4e7 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
1d4e8 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
1d4e9 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e  (u.am.pCrsr, &u.
1d4ea 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20  am.avail);.     
1d4eb 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b   }.      /* If K
1d4ec 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65  eyFetch()/DataFe
1d4ed 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f  tch() managed to
1d4ee 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20   get the entire 
1d4ef 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a  payload,.      *
1d4f0 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f  * save the paylo
1d4f1 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70  ad in the u.am.p
1d4f2 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
1d4f3 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75  That will save u
1d4f4 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20  s from.      ** 
1d4f5 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61  having to make a
1d4f6 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20  dditional calls 
1d4f7 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e  to fetch the con
1d4f8 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a  tent portion of.
1d4f9 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63        ** the rec
1d4fa 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ord..      */.  
1d4fb 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d4fc 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20  .avail>=0 );.   
1d4fd 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c     if( u.am.payl
1d4fe 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29  oadSize <= (u32)
1d4ff 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20  u.am.avail ){.  
1d500 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20        u.am.zRec 
1d501 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20  = u.am.zData;.  
1d502 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61        u.am.pC->a
1d503 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e  Row = (u8*)u.am.
1d504 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c  zData;.      }el
1d505 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d  se{.        u.am
1d506 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20  .pC->aRow = 0;. 
1d507 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1d508 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
1d509 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65  g assert is true
1d50a 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61 63   in all cases ac
1d50b 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a  cept when.    **
1d50c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1d50d 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72  le has been corr
1d50e 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79  upted externally
1d50f 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65  ..    **    asse
1d510 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30  rt( u.am.zRec!=0
1d511 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d   || u.am.avail>=
1d512 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d513 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d   || u.am.avail>=
1d514 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d  9 ); */.    u.am
1d515 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69  .szHdr = getVari
1d516 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a  nt32((u8*)u.am.z
1d517 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65  Data, u.am.offse
1d518 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  t);..    /* Make
1d519 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20   sure a corrupt 
1d51a 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74  database has not
1d51b 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65   given us an ove
1d51c 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20  rsize header..  
1d51d 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77    ** Do this now
1d51e 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65   to avoid an ove
1d51f 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  rsize memory all
1d520 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a  ocation..    **.
1d521 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72      ** Type entr
1d522 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65  ies can be betwe
1d523 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73  en 1 and 5 bytes
1d524 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e   each.  But 4 an
1d525 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20  d 5 byte.    ** 
1d526 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63  types use so muc
1d527 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61  h data space tha
1d528 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  t there can only
1d529 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20   be 4096 and 32 
1d52a 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20  of.    ** them, 
1d52b 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53  respectively.  S
1d52c 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65  o the maximum he
1d52d 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75  ader length resu
1d52e 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a  lts from a.    *
1d52f 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f  * 3-byte type fo
1d530 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61  r each of the ma
1d531 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63  ximum of 32768 c
1d532 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65  olumns plus thre
1d533 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62  e.    ** extra b
1d534 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61  ytes for the hea
1d535 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c  der length itsel
1d536 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20  f.  32768*3 + 3 
1d537 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a  = 98307..    */.
1d538 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66      if( u.am.off
1d539 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20  set > 98307 ){. 
1d53a 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1d53b 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1d53c 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c       goto op_col
1d53d 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  umn_out;.    }..
1d53e 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69      /* Compute i
1d53f 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e  n u.am.len the n
1d540 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1d541 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74  f data we need t
1d542 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a  o read in order.
1d543 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e      ** to get u.
1d544 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76  am.nField type v
1d545 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66  alues.  u.am.off
1d546 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20  set is an upper 
1d547 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20  bound on this.  
1d548 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e  But.    ** u.am.
1d549 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20  nField might be 
1d54a 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65  significantly le
1d54b 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65  ss than the true
1d54c 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1d54d 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  ns.    ** in the
1d54e 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74   table, and in t
1d54f 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d  hat case, 5*u.am
1d550 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20  .nField+3 might 
1d551 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  be smaller than 
1d552 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20  u.am.offset..   
1d553 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d   ** We want to m
1d554 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e  inimize u.am.len
1d555 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d   in order to lim
1d556 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  it the size of t
1d557 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a  he memory.    **
1d558 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70   allocation, esp
1d559 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72  ecially if a cor
1d55a 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
1d55b 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e  le has caused u.
1d55c 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a  am.offset.    **
1d55d 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64   to be oversized
1d55e 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69  . Offset is limi
1d55f 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f  ted to 98307 abo
1d560 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d  ve.  But 98307 m
1d561 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c  ight.    ** stil
1d562 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20  l exceed Robson 
1d563 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1d564 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65  n limits on some
1d565 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e   configurations.
1d566 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65  .    ** On syste
1d567 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74  ms that cannot t
1d568 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65  olerate large me
1d569 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
1d56a 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b  , u.am.nField*5+
1d56b 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69  3.    ** will li
1d56c 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61  kely be much sma
1d56d 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e  ller since u.am.
1d56e 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65  nField will like
1d56f 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a  ly be less than.
1d570 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e      ** 20 or so.
1d571 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74    This insures t
1d572 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72  hat Robson memor
1d573 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d  y allocation lim
1d574 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e  its are.    ** n
1d575 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e  ot exceeded even
1d576 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74   for corrupt dat
1d577 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20  abase files..   
1d578 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e   */.    u.am.len
1d579 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35   = u.am.nField*5
1d57a 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e   + 3;.    if( u.
1d57b 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e  am.len > (int)u.
1d57c 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6d  am.offset ) u.am
1d57d 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d  .len = (int)u.am
1d57e 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a  .offset;..    /*
1d57f 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20   The KeyFetch() 
1d580 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61  or DataFetch() a
1d581 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e  bove are fast an
1d582 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65  d will get the e
1d583 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63  ntire.    ** rec
1d584 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f  ord header in mo
1d585 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74  st cases.  But t
1d586 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f  hey will fail to
1d587 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74   get the complet
1d588 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20  e.    ** record 
1d589 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65  header if the re
1d58a 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73  cord header does
1d58b 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69   not fit on a si
1d58c 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a  ngle page.    **
1d58d 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20   in the B-Tree. 
1d58e 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65   When that happe
1d58f 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56  ns, use sqlite3V
1d590 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1d591 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75  ) to.    ** acqu
1d592 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ire the complete
1d593 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20   header text..  
1d594 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e    */.    if( !u.
1d595 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e  am.zRec && u.am.
1d596 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29  avail<u.am.len )
1d597 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65  {.      u.am.sMe
1d598 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20  m.flags = 0;.   
1d599 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20     u.am.sMem.db 
1d59a 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
1d59b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
1d59c 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72  omBtree(u.am.pCr
1d59d 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c  sr, 0, u.am.len,
1d59e 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65   u.am.pC->isInde
1d59f 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a  x, &u.am.sMem);.
1d5a0 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1d5a1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1d5a2 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
1d5a3 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  n_out;.      }. 
1d5a4 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20       u.am.zData 
1d5a5 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20  = u.am.sMem.z;. 
1d5a6 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45     }.    u.am.zE
1d5a7 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75  ndHdr = (u8 *)&u
1d5a8 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c  .am.zData[u.am.l
1d5a9 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49  en];.    u.am.zI
1d5aa 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d  dx = (u8 *)&u.am
1d5ab 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64  .zData[u.am.szHd
1d5ac 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e  r];..    /* Scan
1d5ad 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20   the header and 
1d5ae 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69  use it to fill i
1d5af 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 79 70 65  n the u.am.aType
1d5b0 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66  [] and u.am.aOff
1d5b1 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  set[].    ** arr
1d5b2 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65  ays.  u.am.aType
1d5b3 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f  [u.am.i] will co
1d5b4 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69  ntain the type i
1d5b5 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75  nteger for the u
1d5b6 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20  .am.i-th.    ** 
1d5b7 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e  column and u.am.
1d5b8 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20  aOffset[u.am.i] 
1d5b9 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65  will contain the
1d5ba 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f   u.am.offset fro
1d5bb 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
1d5bc 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65      ** of the re
1d5bd 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72  cord to the star
1d5be 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f  t of the data fo
1d5bf 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20  r the u.am.i-th 
1d5c0 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20  column.    */.  
1d5c1 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20    u.am.offset64 
1d5c2 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20  = u.am.offset;. 
1d5c3 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b     for(u.am.i=0;
1d5c4 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69   u.am.i<u.am.nFi
1d5c5 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a  eld; u.am.i++){.
1d5c6 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a        if( u.am.z
1d5c7 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72  Idx<u.am.zEndHdr
1d5c8 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d   ){.        u.am
1d5c9 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d  .aOffset[u.am.i]
1d5ca 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66   = (u32)u.am.off
1d5cb 73 65 74 36 34 3b 0a 20 20 20 20 20 20 20 20 75  set64;.        u
1d5cc 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56  .am.zIdx += getV
1d5cd 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64  arint32(u.am.zId
1d5ce 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e  x, u.am.aType[u.
1d5cf 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 20 20  am.i]);.        
1d5d0 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d  u.am.offset64 +=
1d5d1 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1d5d2 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61  alTypeLen(u.am.a
1d5d3 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20  Type[u.am.i]);. 
1d5d4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d5d5 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69      /* If u.am.i
1d5d6 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e   is less that u.
1d5d7 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20  am.nField, then 
1d5d8 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66  there are less f
1d5d9 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20  ields in this.  
1d5da 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20        ** record 
1d5db 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d  than SetNumColum
1d5dc 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65  ns indicated the
1d5dd 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69  re are columns i
1d5de 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  n the.        **
1d5df 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20   table. Set the 
1d5e0 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20  u.am.offset for 
1d5e1 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e  any extra column
1d5e2 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e  s not present in
1d5e3 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
1d5e4 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69  record to 0. Thi
1d5e5 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c  s tells code bel
1d5e6 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55  ow to store a NU
1d5e7 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  LL.        ** in
1d5e8 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61  stead of deseria
1d5e9 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66  lizing a value f
1d5ea 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a  rom the record..
1d5eb 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1d5ec 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b     u.am.aOffset[
1d5ed 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20  u.am.i] = 0;.   
1d5ee 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
1d5ef 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1d5f0 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29  ease(&u.am.sMem)
1d5f1 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e  ;.    u.am.sMem.
1d5f2 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1d5f3 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  ;..    /* If we 
1d5f4 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68  have read more h
1d5f5 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20  eader data than 
1d5f6 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  was contained in
1d5f7 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20   the header,.   
1d5f8 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e   ** or if the en
1d5f9 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69  d of the last fi
1d5fa 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62  eld appears to b
1d5fb 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
1d5fc 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63  f the.    ** rec
1d5fd 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65  ord, or if the e
1d5fe 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66  nd of the last f
1d5ff 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20  ield appears to 
1d600 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e  be before the en
1d601 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  d.    ** of the 
1d602 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c  record (when all
1d603 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29   fields present)
1d604 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62  , then we must b
1d605 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a  e dealing.    **
1d606 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20   with a corrupt 
1d607 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f  database..    */
1d608 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a  .    if( (u.am.z
1d609 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48  Idx > u.am.zEndH
1d60a 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73  dr)|| (u.am.offs
1d60b 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c  et64 > u.am.payl
1d60c 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c  oadSize).     ||
1d60d 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61   (u.am.zIdx==u.a
1d60e 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 75 2e 61  m.zEndHdr && u.a
1d60f 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 75 36 34  m.offset64!=(u64
1d610 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a  )u.am.payloadSiz
1d611 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  e) ){.      rc =
1d612 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1d613 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BKPT;.      goto
1d614 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a   op_column_out;.
1d615 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1d616 47 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  Get the column i
1d617 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75  nformation. If u
1d618 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d  .am.aOffset[u.am
1d619 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  .p2] is non-zero
1d61a 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65  , then.  ** dese
1d61b 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75  rialize the valu
1d61c 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72  e from the recor
1d61d 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73  d. If u.am.aOffs
1d61e 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a  et[u.am.p2] is z
1d61f 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74  ero,.  ** then t
1d620 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f  here are not eno
1d621 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 68  ugh fields in th
1d622 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 69  e record to sati
1d623 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 71  sfy the.  ** req
1d624 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 63  uest.  In this c
1d625 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 6c  ase, set the val
1d626 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34  ue NULL or to P4
1d627 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 61   if P4 is.  ** a
1d628 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65   pointer to a Me
1d629 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20  m object..  */. 
1d62a 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65   if( u.am.aOffse
1d62b 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20  t[u.am.p2] ){.  
1d62c 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1d62d 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69  LITE_OK );.    i
1d62e 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a  f( u.am.zRec ){.
1d62f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d630 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
1d631 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b  nal(u.am.pDest);
1d632 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d633 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 20  beSerialGet((u8 
1d634 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61  *)&u.am.zRec[u.a
1d635 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70  m.aOffset[u.am.p
1d636 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b  2]], u.am.aType[
1d637 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70  u.am.p2], u.am.p
1d638 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Dest);.    }else
1d639 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e  {.      u.am.len
1d63a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1d63b 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d  rialTypeLen(u.am
1d63c 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29  .aType[u.am.p2])
1d63d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d63e 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d  dbeMemMove(&u.am
1d63f 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73  .sMem, u.am.pDes
1d640 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  t);.      rc = s
1d641 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
1d642 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73  mBtree(u.am.pCrs
1d643 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b  r, u.am.aOffset[
1d644 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c  u.am.p2], u.am.l
1d645 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49  en, u.am.pC->isI
1d646 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d  ndex, &u.am.sMem
1d647 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1d648 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1d649 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
1d64a 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  lumn_out;.      
1d64b 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61  }.      u.am.zDa
1d64c 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a  ta = u.am.sMem.z
1d64d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d64e 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38  dbeSerialGet((u8
1d64f 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e  *)u.am.zData, u.
1d650 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32  am.aType[u.am.p2
1d651 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  ], u.am.pDest);.
1d652 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70      }.    u.am.p
1d653 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  Dest->enc = enco
1d654 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ding;.  }else{. 
1d655 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79     if( pOp->p4ty
1d656 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20  pe==P4_MEM ){.  
1d657 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1d658 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e  emShallowCopy(u.
1d659 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70  am.pDest, pOp->p
1d65a 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74  4.pMem, MEM_Stat
1d65b 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ic);.    }else{.
1d65c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1d65d 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 26  am.pDest->flags&
1d65e 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  MEM_Null );.    
1d65f 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77  }.  }..  /* If w
1d660 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  e dynamically al
1d661 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f  located space to
1d662 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28   hold the data (
1d663 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  in the.  ** sqli
1d664 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1d665 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65  ree() call above
1d666 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  ) then transfer 
1d667 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a  control of that.
1d668 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79    ** dynamically
1d669 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
1d66a 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75 2e 61   over to the u.a
1d66b 6d 2e 70 44 65 73 74 20 73 74 72 75 63 74 75 72  m.pDest structur
1d66c 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 65  e..  ** This pre
1d66d 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79 20 63  vents a memory c
1d66e 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  opy..  */.  if( 
1d66f 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f  u.am.sMem.zMallo
1d670 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
1d671 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e   u.am.sMem.z==u.
1d672 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20  am.sMem.zMalloc 
1d673 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
1d674 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61  (u.am.pDest->fla
1d675 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b  gs & MEM_Dyn) );
1d676 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75  .    assert( !(u
1d677 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73  .am.pDest->flags
1d678 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d   & (MEM_Blob|MEM
1d679 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70  _Str)) || u.am.p
1d67a 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d  Dest->z==u.am.sM
1d67b 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d  em.z );.    u.am
1d67c 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d  .pDest->flags &=
1d67d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d   ~(MEM_Ephem|MEM
1d67e 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 75 2e  _Static);.    u.
1d67f 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20  am.pDest->flags 
1d680 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  |= MEM_Term;.   
1d681 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d   u.am.pDest->z =
1d682 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20   u.am.sMem.z;.  
1d683 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d    u.am.pDest->zM
1d684 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65  alloc = u.am.sMe
1d685 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a  m.zMalloc;.  }..
1d686 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1d687 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
1d688 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  le(u.am.pDest);.
1d689 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a  .op_column_out:.
1d68a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1d68b 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74  BSIZE(u.am.pDest
1d68c 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
1d68d 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61  ACE(pOp->p3, u.a
1d68e 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 72 65 61  m.pDest);.  brea
1d68f 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d690 20 41 66 66 69 6e 69 74 79 20 50 31 20 50 32 20   Affinity P1 P2 
1d691 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70  * P4 *.**.** App
1d692 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20 74 6f  ly affinities to
1d693 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32 20 72   a range of P2 r
1d694 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
1d695 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a  g with P1..**.**
1d696 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20   P4 is a string 
1d697 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61  that is P2 chara
1d698 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20  cters long. The 
1d699 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66  nth character of
1d69a 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69   the.** string i
1d69b 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c  ndicates the col
1d69c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61  umn affinity tha
1d69d 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  t should be used
1d69e 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20   for the nth.** 
1d69f 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74  memory cell in t
1d6a0 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73  he range..*/.cas
1d6a1 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b  e OP_Affinity: {
1d6a2 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d6a3 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d6a4 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20   into u.an */.  
1d6a5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 66 66  const char *zAff
1d6a6 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 65 20  inity;   /* The 
1d6a7 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 61  affinity to be a
1d6a8 70 70 6c 69 65 64 20 2a 2f 0a 20 20 63 68 61 72  pplied */.  char
1d6a9 20 63 41 66 66 3b 20 20 20 20 20 20 20 20 20 20   cAff;          
1d6aa 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
1d6ab 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 61 66   character of af
1d6ac 66 69 6e 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66  finity */.#endif
1d6ad 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d6ae 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d6af 2e 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a  .an */..  u.an.z
1d6b0 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e  Affinity = pOp->
1d6b1 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20  p4.z;.  assert( 
1d6b2 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 21 3d  u.an.zAffinity!=
1d6b3 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  0 );.  assert( u
1d6b4 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 5b 70 4f  .an.zAffinity[pO
1d6b5 70 2d 3e 70 32 5d 3d 3d 30 20 29 3b 0a 20 20 70  p->p2]==0 );.  p
1d6b6 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In1 = &aMem[pOp-
1d6b7 3e 70 31 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28  >p1];.  while( (
1d6b8 75 2e 61 6e 2e 63 41 66 66 20 3d 20 2a 28 75 2e  u.an.cAff = *(u.
1d6b9 61 6e 2e 7a 41 66 66 69 6e 69 74 79 2b 2b 29 29  an.zAffinity++))
1d6ba 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  !=0 ){.    asser
1d6bb 74 28 20 70 49 6e 31 20 3c 3d 20 26 70 2d 3e 61  t( pIn1 <= &p->a
1d6bc 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a  Mem[p->nMem] );.
1d6bd 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70      ExpandBlob(p
1d6be 49 6e 31 29 3b 0a 20 20 20 20 61 70 70 6c 79 41  In1);.    applyA
1d6bf 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e  ffinity(pIn1, u.
1d6c0 61 6e 2e 63 41 66 66 2c 20 65 6e 63 6f 64 69 6e  an.cAff, encodin
1d6c1 67 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a  g);.    pIn1++;.
1d6c2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d6c3 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 61 6b 65 52  /* Opcode: MakeR
1d6c4 65 63 6f 72 64 20 50 31 20 50 32 20 50 33 20 50  ecord P1 P2 P3 P
1d6c5 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72  4 *.**.** Conver
1d6c6 74 20 50 32 20 72 65 67 69 73 74 65 72 73 20 62  t P2 registers b
1d6c7 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 31  eginning with P1
1d6c8 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 65   into a single e
1d6c9 6e 74 72 79 0a 2a 2a 20 73 75 69 74 61 62 6c 65  ntry.** suitable
1d6ca 20 66 6f 72 20 75 73 65 20 61 73 20 61 20 64 61   for use as a da
1d6cb 74 61 20 72 65 63 6f 72 64 20 69 6e 20 61 20 64  ta record in a d
1d6cc 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
1d6cd 20 61 73 20 61 20 6b 65 79 0a 2a 2a 20 69 6e 20   as a key.** in 
1d6ce 61 6e 20 69 6e 64 65 78 2e 20 20 54 68 65 20 64  an index.  The d
1d6cf 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 66 6f  etails of the fo
1d6d0 72 6d 61 74 20 61 72 65 20 69 72 72 65 6c 65 76  rmat are irrelev
1d6d1 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a  ant as long as.*
1d6d2 2a 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  * the OP_Column 
1d6d3 6f 70 63 6f 64 65 20 63 61 6e 20 64 65 63 6f 64  opcode can decod
1d6d4 65 20 74 68 65 20 72 65 63 6f 72 64 20 6c 61 74  e the record lat
1d6d5 65 72 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20  er..** Refer to 
1d6d6 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d  source code comm
1d6d7 65 6e 74 73 20 66 6f 72 20 74 68 65 20 64 65 74  ents for the det
1d6d8 61 69 6c 73 20 6f 66 20 74 68 65 20 72 65 63 6f  ails of the reco
1d6d9 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a  rd.** format..**
1d6da 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20  .** P4 may be a 
1d6db 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50  string that is P
1d6dc 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e  2 characters lon
1d6dd 67 2e 20 20 54 68 65 20 6e 74 68 20 63 68 61 72  g.  The nth char
1d6de 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20  acter of the.** 
1d6df 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73  string indicates
1d6e0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   the column affi
1d6e1 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64  nity that should
1d6e2 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
1d6e3 20 6e 74 68 0a 2a 2a 20 66 69 65 6c 64 20 6f 66   nth.** field of
1d6e4 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 0a   the index key..
1d6e5 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 70 70 69 6e  **.** The mappin
1d6e6 67 20 66 72 6f 6d 20 63 68 61 72 61 63 74 65 72  g from character
1d6e7 20 74 6f 20 61 66 66 69 6e 69 74 79 20 69 73 20   to affinity is 
1d6e8 67 69 76 65 6e 20 62 79 20 74 68 65 20 53 51 4c  given by the SQL
1d6e9 49 54 45 5f 41 46 46 5f 0a 2a 2a 20 6d 61 63 72  ITE_AFF_.** macr
1d6ea 6f 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71  os defined in sq
1d6eb 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a  liteInt.h..**.**
1d6ec 20 49 66 20 50 34 20 69 73 20 4e 55 4c 4c 20 74   If P4 is NULL t
1d6ed 68 65 6e 20 61 6c 6c 20 69 6e 64 65 78 20 66 69  hen all index fi
1d6ee 65 6c 64 73 20 68 61 76 65 20 74 68 65 20 61 66  elds have the af
1d6ef 66 69 6e 69 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a  finity NONE..*/.
1d6f0 63 61 73 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  case OP_MakeReco
1d6f1 72 64 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  rd: {.#if 0  /* 
1d6f2 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d6f3 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20  moved into u.ao 
1d6f4 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63  */.  u8 *zNewRec
1d6f5 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ord;        /* A
1d6f6 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20   buffer to hold 
1d6f7 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  the data for the
1d6f8 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20   new record */. 
1d6f9 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20   Mem *pRec;     
1d6fa 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
1d6fb 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75  ew record */.  u
1d6fc 36 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20  64 nData;       
1d6fd 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d6fe 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
1d6ff 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20   space */.  int 
1d700 6e 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20  nHdr;           
1d701 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1d702 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20  bytes of header 
1d703 73 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e  space */.  i64 n
1d704 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  Byte;           
1d705 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20    /* Data space 
1d706 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69  required for thi
1d707 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  s record */.  in
1d708 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20  t nZero;        
1d709 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d70a 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20  f zero bytes at 
1d70b 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72  the end of the r
1d70c 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
1d70d 56 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20  Varint;         
1d70e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d70f 79 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74  ytes in a varint
1d710 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c   */.  u32 serial
1d711 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20  _type;       /* 
1d712 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20  Type field */.  
1d713 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20  Mem *pData0;    
1d714 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1d715 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62  field to be comb
1d716 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65  ined into the re
1d717 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  cord */.  Mem *p
1d718 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Last;           
1d719 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f   /* Last field o
1d71a 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  f the record */.
1d71b 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20    int nField;   
1d71c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1d71d 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
1d71e 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1d71f 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b  char *zAffinity;
1d720 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66         /* The af
1d721 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f  finity string fo
1d722 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  r the record */.
1d723 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61    int file_forma
1d724 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65  t;       /* File
1d725 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66   format to use f
1d726 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  or encoding */. 
1d727 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1d728 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
1d729 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63   used in zNewRec
1d72a 6f 72 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c  ord[] */.  int l
1d72b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
1d72c 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61    /* Length of a
1d72d 20 66 69 65 6c 64 20 2a 2f 0a 23 65 6e 64 69 66   field */.#endif
1d72e 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d72f 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d730 2e 61 6f 20 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73  .ao */..  /* Ass
1d731 75 6d 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  uming the record
1d732 20 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c   contains N fiel
1d733 64 73 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66  ds, the record f
1d734 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a  ormat looks.  **
1d735 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a   like this:.  **
1d736 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .  ** ----------
1d737 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d738 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d739 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d73a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20  --------------. 
1d73b 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c   ** | hdr-size |
1d73c 20 74 79 70 65 20 30 20 7c 20 74 79 70 65 20 31   type 0 | type 1
1d73d 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d   | ... | type N-
1d73e 31 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20  1 | data0 | ... 
1d73f 7c 20 64 61 74 61 20 4e 2d 31 20 7c 0a 20 20 2a  | data N-1 |.  *
1d740 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
1d741 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d742 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d743 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d744 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a  ----------.  **.
1d745 20 20 2a 2a 20 44 61 74 61 28 30 29 20 69 73 20    ** Data(0) is 
1d746 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73  taken from regis
1d747 74 65 72 20 50 31 2e 20 20 44 61 74 61 28 31 29  ter P1.  Data(1)
1d748 20 63 6f 6d 65 73 20 66 72 6f 6d 20 72 65 67 69   comes from regi
1d749 73 74 65 72 20 50 31 2b 31 0a 20 20 2a 2a 20 61  ster P1+1.  ** a
1d74a 6e 64 20 73 6f 20 66 72 6f 74 68 2e 0a 20 20 2a  nd so froth..  *
1d74b 2a 0a 20 20 2a 2a 20 45 61 63 68 20 74 79 70 65  *.  ** Each type
1d74c 20 66 69 65 6c 64 20 69 73 20 61 20 76 61 72 69   field is a vari
1d74d 6e 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  nt representing 
1d74e 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20  the serial type 
1d74f 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72  of the.  ** corr
1d750 65 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 20 65  esponding data e
1d751 6c 65 6d 65 6e 74 20 28 73 65 65 20 73 71 6c 69  lement (see sqli
1d752 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1d753 65 28 29 29 2e 20 54 68 65 0a 20 20 2a 2a 20 68  e()). The.  ** h
1d754 64 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73  dr-size field is
1d755 20 61 6c 73 6f 20 61 20 76 61 72 69 6e 74 20 77   also a varint w
1d756 68 69 63 68 20 69 73 20 74 68 65 20 6f 66 66 73  hich is the offs
1d757 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  et from the begi
1d758 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f 66 20 74 68  nning.  ** of th
1d759 65 20 72 65 63 6f 72 64 20 74 6f 20 64 61 74 61  e record to data
1d75a 30 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e  0..  */.  u.ao.n
1d75b 44 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20  Data = 0;       
1d75c 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d75d 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61  ytes of data spa
1d75e 63 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64  ce */.  u.ao.nHd
1d75f 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
1d760 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1d761 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61  es of header spa
1d762 63 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79  ce */.  u.ao.nBy
1d763 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  te = 0;         
1d764 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65  /* Data space re
1d765 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20  quired for this 
1d766 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f  record */.  u.ao
1d767 2e 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20  .nZero = 0;     
1d768 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1d769 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74   zero bytes at t
1d76a 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65  he end of the re
1d76b 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e  cord */.  u.ao.n
1d76c 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b  Field = pOp->p1;
1d76d 0a 20 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74  .  u.ao.zAffinit
1d76e 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  y = pOp->p4.z;. 
1d76f 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 6e 46   assert( u.ao.nF
1d770 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ield>0 && pOp->p
1d771 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75  2>0 && pOp->p2+u
1d772 2e 61 6f 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e  .ao.nField<=p->n
1d773 4d 65 6d 2b 31 20 29 3b 0a 20 20 75 2e 61 6f 2e  Mem+1 );.  u.ao.
1d774 70 44 61 74 61 30 20 3d 20 26 61 4d 65 6d 5b 75  pData0 = &aMem[u
1d775 2e 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75  .ao.nField];.  u
1d776 2e 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70  .ao.nField = pOp
1d777 2d 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61  ->p2;.  u.ao.pLa
1d778 73 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61  st = &u.ao.pData
1d779 30 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d  0[u.ao.nField-1]
1d77a 3b 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f  ;.  u.ao.file_fo
1d77b 72 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69  rmat = p->minWri
1d77c 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20  teFileFormat;.. 
1d77d 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
1d77e 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68   the elements th
1d77f 61 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20  at will make up 
1d780 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69  the record to fi
1d781 67 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f  gure.  ** out ho
1d782 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20  w much space is 
1d783 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65  required for the
1d784 20 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a   new record..  *
1d785 2f 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65  /.  for(u.ao.pRe
1d786 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75  c=u.ao.pData0; u
1d787 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70  .ao.pRec<=u.ao.p
1d788 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b  Last; u.ao.pRec+
1d789 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f  +){.    if( u.ao
1d78a 2e 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20  .zAffinity ){.  
1d78b 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74      applyAffinit
1d78c 79 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61  y(u.ao.pRec, u.a
1d78d 6f 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f  o.zAffinity[u.ao
1d78e 2e 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61  .pRec-u.ao.pData
1d78f 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  0], encoding);. 
1d790 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61     }.    if( u.a
1d791 6f 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45  o.pRec->flags&ME
1d792 4d 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70  M_Zero && u.ao.p
1d793 52 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20  Rec->n>0 ){.    
1d794 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d795 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e  ExpandBlob(u.ao.
1d796 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pRec);.    }.   
1d797 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70   u.ao.serial_typ
1d798 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  e = sqlite3VdbeS
1d799 65 72 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70  erialType(u.ao.p
1d79a 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66  Rec, u.ao.file_f
1d79b 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f  ormat);.    u.ao
1d79c 2e 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64  .len = sqlite3Vd
1d79d 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1d79e 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65  u.ao.serial_type
1d79f 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74  );.    u.ao.nDat
1d7a0 61 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20  a += u.ao.len;. 
1d7a1 20 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20     u.ao.nHdr += 
1d7a2 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
1d7a3 28 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70  (u.ao.serial_typ
1d7a4 65 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f  e);.    if( u.ao
1d7a5 2e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d  .pRec->flags & M
1d7a6 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20  EM_Zero ){.     
1d7a7 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65   /* Only pure ze
1d7a8 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20  ro-filled BLOBs 
1d7a9 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20  can be input to 
1d7aa 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20  this Opcode..   
1d7ab 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20     ** We do not 
1d7ac 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68  allow blobs with
1d7ad 20 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20   a prefix and a 
1d7ae 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c  zero-filled tail
1d7af 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e  . */.      u.ao.
1d7b0 6e 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52  nZero += u.ao.pR
1d7b1 65 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20  ec->u.nZero;.   
1d7b2 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e   }else if( u.ao.
1d7b3 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61  len ){.      u.a
1d7b4 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20  o.nZero = 0;.   
1d7b5 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64   }.  }..  /* Add
1d7b6 20 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61   the initial hea
1d7b7 64 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74  der varint and t
1d7b8 6f 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f  otal the size */
1d7b9 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20  .  u.ao.nHdr += 
1d7ba 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73  u.ao.nVarint = s
1d7bb 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
1d7bc 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66  u.ao.nHdr);.  if
1d7bd 28 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73  ( u.ao.nVarint<s
1d7be 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
1d7bf 75 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20  u.ao.nHdr) ){.  
1d7c0 20 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20    u.ao.nHdr++;. 
1d7c1 20 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20   }.  u.ao.nByte 
1d7c2 3d 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f  = u.ao.nHdr+u.ao
1d7c3 2e 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72  .nData-u.ao.nZer
1d7c4 6f 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42  o;.  if( u.ao.nB
1d7c5 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  yte>db->aLimit[S
1d7c6 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
1d7c7 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  TH] ){.    goto 
1d7c8 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20  too_big;.  }..  
1d7c9 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
1d7ca 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72   output register
1d7cb 20 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61   has a buffer la
1d7cc 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74  rge enough to st
1d7cd 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77  ore.  ** the new
1d7ce 20 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74   record. The out
1d7cf 70 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f  put register (pO
1d7d0 70 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c  p->p3) is not al
1d7d1 6c 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65  lowed to.  ** be
1d7d2 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75   one of the inpu
1d7d3 74 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63  t registers (bec
1d7d4 61 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ause the followi
1d7d5 6e 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20  ng call to.  ** 
1d7d6 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1d7d7 6f 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62  ow() could clobb
1d7d8 65 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66  er the value bef
1d7d9 6f 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e  ore it is used).
1d7da 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
1d7db 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20  pOp->p3<pOp->p1 
1d7dc 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d  || pOp->p3>=pOp-
1d7dd 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20  >p1+pOp->p2 );. 
1d7de 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f   pOut = &aMem[pO
1d7df 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 73 71  p->p3];.  if( sq
1d7e0 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1d7e1 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 6f  (pOut, (int)u.ao
1d7e2 2e 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20  .nByte, 0) ){.  
1d7e3 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1d7e4 20 7d 0a 20 20 75 2e 61 6f 2e 7a 4e 65 77 52 65   }.  u.ao.zNewRe
1d7e5 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70 4f 75  cord = (u8 *)pOu
1d7e6 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74  t->z;..  /* Writ
1d7e7 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  e the record */.
1d7e8 20 20 75 2e 61 6f 2e 69 20 3d 20 70 75 74 56 61    u.ao.i = putVa
1d7e9 72 69 6e 74 33 32 28 75 2e 61 6f 2e 7a 4e 65 77  rint32(u.ao.zNew
1d7ea 52 65 63 6f 72 64 2c 20 75 2e 61 6f 2e 6e 48 64  Record, u.ao.nHd
1d7eb 72 29 3b 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70  r);.  for(u.ao.p
1d7ec 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b  Rec=u.ao.pData0;
1d7ed 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f   u.ao.pRec<=u.ao
1d7ee 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65  .pLast; u.ao.pRe
1d7ef 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 73  c++){.    u.ao.s
1d7f0 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c  erial_type = sql
1d7f1 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
1d7f2 70 65 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e  pe(u.ao.pRec, u.
1d7f3 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b  ao.file_format);
1d7f4 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d 20 70  .    u.ao.i += p
1d7f5 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e 61 6f  utVarint32(&u.ao
1d7f6 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f  .zNewRecord[u.ao
1d7f7 2e 69 5d 2c 20 75 2e 61 6f 2e 73 65 72 69 61 6c  .i], u.ao.serial
1d7f8 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f 2a 20  _type);      /* 
1d7f9 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20  serial type */. 
1d7fa 20 7d 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52   }.  for(u.ao.pR
1d7fb 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20  ec=u.ao.pData0; 
1d7fc 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e  u.ao.pRec<=u.ao.
1d7fd 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63  pLast; u.ao.pRec
1d7fe 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20  ++){  /* serial 
1d7ff 64 61 74 61 20 2a 2f 0a 20 20 20 20 75 2e 61 6f  data */.    u.ao
1d800 2e 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62  .i += sqlite3Vdb
1d801 65 53 65 72 69 61 6c 50 75 74 28 26 75 2e 61 6f  eSerialPut(&u.ao
1d802 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f  .zNewRecord[u.ao
1d803 2e 69 5d 2c 20 28 69 6e 74 29 28 75 2e 61 6f 2e  .i], (int)(u.ao.
1d804 6e 42 79 74 65 2d 75 2e 61 6f 2e 69 29 2c 20 75  nByte-u.ao.i), u
1d805 2e 61 6f 2e 70 52 65 63 2c 75 2e 61 6f 2e 66 69  .ao.pRec,u.ao.fi
1d806 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a  le_format);.  }.
1d807 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 69    assert( u.ao.i
1d808 3d 3d 75 2e 61 6f 2e 6e 42 79 74 65 20 29 3b 0a  ==u.ao.nByte );.
1d809 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1d80a 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
1d80b 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f  =p->nMem );.  pO
1d80c 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75 2e 61  ut->n = (int)u.a
1d80d 6f 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d  o.nByte;.  pOut-
1d80e 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f  >flags = MEM_Blo
1d80f 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70  b | MEM_Dyn;.  p
1d810 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20  Out->xDel = 0;. 
1d811 20 69 66 28 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20   if( u.ao.nZero 
1d812 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 6e  ){.    pOut->u.n
1d813 5a 65 72 6f 20 3d 20 75 2e 61 6f 2e 6e 5a 65 72  Zero = u.ao.nZer
1d814 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  o;.    pOut->fla
1d815 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a  gs |= MEM_Zero;.
1d816 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20    }.  pOut->enc 
1d817 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20  = SQLITE_UTF8;  
1d818 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 62  /* In case the b
1d819 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f 6e 76  lob is ever conv
1d81a 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 2a 2f  erted to text */
1d81b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1d81c 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29  E(pOp->p3, pOut)
1d81d 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ;.  UPDATE_MAX_B
1d81e 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20  LOBSIZE(pOut);. 
1d81f 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d820 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31 20 50  code: Count P1 P
1d821 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74  2 * * *.**.** St
1d822 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ore the number o
1d823 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20 69 6e  f entries (an in
1d824 74 65 67 65 72 20 76 61 6c 75 65 29 20 69 6e 20  teger value) in 
1d825 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
1d826 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 62 79  ex .** opened by
1d827 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20 72 65   cursor P1 in re
1d828 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23 69 66  gister P2.*/.#if
1d829 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d82a 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61 73 65  _BTREECOUNT.case
1d82b 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20 20 20   OP_Count: {    
1d82c 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1d82d 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1d82e 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1d82f 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d830 75 2e 61 70 20 2a 2f 0a 20 20 69 36 34 20 6e 45  u.ap */.  i64 nE
1d831 6e 74 72 79 3b 0a 20 20 42 74 43 75 72 73 6f 72  ntry;.  BtCursor
1d832 20 2a 70 43 72 73 72 3b 0a 23 65 6e 64 69 66 20   *pCrsr;.#endif 
1d833 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d834 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d835 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70 2e 70 43  ap */..  u.ap.pC
1d836 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  rsr = p->apCsr[p
1d837 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72  Op->p1]->pCursor
1d838 3b 0a 20 20 69 66 28 20 75 2e 61 70 2e 70 43 72  ;.  if( u.ap.pCr
1d839 73 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  sr ){.    rc = s
1d83a 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74  qlite3BtreeCount
1d83b 28 75 2e 61 70 2e 70 43 72 73 72 2c 20 26 75 2e  (u.ap.pCrsr, &u.
1d83c 61 70 2e 6e 45 6e 74 72 79 29 3b 0a 20 20 7d 65  ap.nEntry);.  }e
1d83d 6c 73 65 7b 0a 20 20 20 20 75 2e 61 70 2e 6e 45  lse{.    u.ap.nE
1d83e 6e 74 72 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ntry = 0;.  }.  
1d83f 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 70  pOut->u.i = u.ap
1d840 2e 6e 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b  .nEntry;.  break
1d841 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f  ;.}.#endif../* O
1d842 70 63 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74  pcode: Savepoint
1d843 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a   P1 * * P4 *.**.
1d844 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65  ** Open, release
1d845 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65   or rollback the
1d846 20 73 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64   savepoint named
1d847 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 50 34   by parameter P4
1d848 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f  , depending.** o
1d849 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50  n the value of P
1d84a 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77  1. To open a new
1d84b 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d   savepoint, P1==
1d84c 30 2e 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63  0. To release (c
1d84d 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69  ommit) an.** exi
1d84e 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c  sting savepoint,
1d84f 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f   P1==1, or to ro
1d850 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69  llback an existi
1d851 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d  ng savepoint P1=
1d852 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  =2..*/.case OP_S
1d853 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20  avepoint: {.#if 
1d854 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1d855 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d856 20 75 2e 61 71 20 2a 2f 0a 20 20 69 6e 74 20 70   u.aq */.  int p
1d857 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
1d858 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
1d859 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e  lue of P1 operan
1d85a 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  d */.  char *zNa
1d85b 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
1d85c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
1d85d 66 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20  f savepoint */. 
1d85e 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 53 61   int nName;.  Sa
1d85f 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20  vepoint *pNew;. 
1d860 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76   Savepoint *pSav
1d861 65 70 6f 69 6e 74 3b 0a 20 20 53 61 76 65 70 6f  epoint;.  Savepo
1d862 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 69 6e 74  int *pTmp;.  int
1d863 20 69 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 69   iSavepoint;.  i
1d864 6e 74 20 69 69 3b 0a 23 65 6e 64 69 66 20 2f 2a  nt ii;.#endif /*
1d865 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1d866 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71   moved into u.aq
1d867 20 2a 2f 0a 0a 20 20 75 2e 61 71 2e 70 31 20 3d   */..  u.aq.p1 =
1d868 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 71   pOp->p1;.  u.aq
1d869 2e 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34  .zName = pOp->p4
1d86a 2e 7a 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74  .z;..  /* Assert
1d86b 20 74 68 61 74 20 74 68 65 20 75 2e 61 71 2e 70   that the u.aq.p
1d86c 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 76  1 parameter is v
1d86d 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 20  alid. Also that 
1d86e 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f  if there is no o
1d86f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  pen.  ** transac
1d870 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 65  tion, then there
1d871 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73   cannot be any s
1d872 61 76 65 70 6f 69 6e 74 73 2e 0a 20 20 2a 2f 0a  avepoints..  */.
1d873 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53    assert( db->pS
1d874 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64  avepoint==0 || d
1d875 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30  b->autoCommit==0
1d876 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1d877 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  aq.p1==SAVEPOINT
1d878 5f 42 45 47 49 4e 7c 7c 75 2e 61 71 2e 70 31 3d  _BEGIN||u.aq.p1=
1d879 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41  =SAVEPOINT_RELEA
1d87a 53 45 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56  SE||u.aq.p1==SAV
1d87b 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1d87c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
1d87d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64  >pSavepoint || d
1d87e 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  b->isTransaction
1d87f 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a  Savepoint==0 );.
1d880 20 20 61 73 73 65 72 74 28 20 63 68 65 63 6b 53    assert( checkS
1d881 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62  avepointCount(db
1d882 29 20 29 3b 0a 0a 20 20 69 66 28 20 75 2e 61 71  ) );..  if( u.aq
1d883 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42  .p1==SAVEPOINT_B
1d884 45 47 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20  EGIN ){.    if( 
1d885 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74  db->writeVdbeCnt
1d886 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41  >0 ){.      /* A
1d887 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63   new savepoint c
1d888 61 6e 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64  annot be created
1d889 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 63   if there are ac
1d88a 74 69 76 65 20 77 72 69 74 65 0a 20 20 20 20 20  tive write.     
1d88b 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28   ** statements (
1d88c 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77  i.e. open read/w
1d88d 72 69 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c  rite incremental
1d88e 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a   blob handles)..
1d88f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1d890 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1d891 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1d892 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61   "cannot open sa
1d893 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20  vepoint - ".    
1d894 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65      "SQL stateme
1d895 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22  nts in progress"
1d896 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
1d897 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
1d898 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 71  else{.      u.aq
1d899 2e 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  .nName = sqlite3
1d89a 53 74 72 6c 65 6e 33 30 28 75 2e 61 71 2e 7a 4e  Strlen30(u.aq.zN
1d89b 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ame);..      /* 
1d89c 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 76  Create a new sav
1d89d 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65  epoint structure
1d89e 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e  . */.      u.aq.
1d89f 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
1d8a0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
1d8a1 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e 74 29 2b  zeof(Savepoint)+
1d8a2 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 20  u.aq.nName+1);. 
1d8a3 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e 70 4e       if( u.aq.pN
1d8a4 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  ew ){.        u.
1d8a5 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d  aq.pNew->zName =
1d8a6 20 28 63 68 61 72 20 2a 29 26 75 2e 61 71 2e 70   (char *)&u.aq.p
1d8a7 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  New[1];.        
1d8a8 6d 65 6d 63 70 79 28 75 2e 61 71 2e 70 4e 65 77  memcpy(u.aq.pNew
1d8a9 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e  ->zName, u.aq.zN
1d8aa 61 6d 65 2c 20 75 2e 61 71 2e 6e 4e 61 6d 65 2b  ame, u.aq.nName+
1d8ab 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  1);..        /* 
1d8ac 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f  If there is no o
1d8ad 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  pen transaction,
1d8ae 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 69 73 20   then mark this 
1d8af 61 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20  as a special.   
1d8b0 20 20 20 20 20 2a 2a 20 22 74 72 61 6e 73 61 63       ** "transac
1d8b1 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e  tion savepoint".
1d8b2 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
1d8b3 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
1d8b4 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e  {.          db->
1d8b5 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a  autoCommit = 0;.
1d8b6 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73            db->is
1d8b7 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
1d8b8 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  oint = 1;.      
1d8b9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1d8ba 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e     db->nSavepoin
1d8bb 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  t++;.        }..
1d8bc 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20          /* Link 
1d8bd 74 68 65 20 6e 65 77 20 73 61 76 65 70 6f 69 6e  the new savepoin
1d8be 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  t into the datab
1d8bf 61 73 65 20 68 61 6e 64 6c 65 27 73 20 6c 69 73  ase handle's lis
1d8c0 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e  t. */.        u.
1d8c1 61 71 2e 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d  aq.pNew->pNext =
1d8c2 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b   db->pSavepoint;
1d8c3 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61  .        db->pSa
1d8c4 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70  vepoint = u.aq.p
1d8c5 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  New;.        u.a
1d8c6 71 2e 70 4e 65 77 2d 3e 6e 44 65 66 65 72 72 65  q.pNew->nDeferre
1d8c7 64 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66  dCons = db->nDef
1d8c8 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20  erredCons;.     
1d8c9 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
1d8ca 7b 0a 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65  {.    u.aq.iSave
1d8cb 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20  point = 0;..    
1d8cc 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65  /* Find the name
1d8cd 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20  d savepoint. If 
1d8ce 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68  there is no such
1d8cf 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e   savepoint, then
1d8d0 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72   an.    ** an er
1d8d1 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
1d8d2 74 6f 20 74 68 65 20 75 73 65 72 2e 20 20 2a 2f  to the user.  */
1d8d3 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20  .    for(.      
1d8d4 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20  u.aq.pSavepoint 
1d8d5 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74  = db->pSavepoint
1d8d6 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 53 61  ;.      u.aq.pSa
1d8d7 76 65 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74  vepoint && sqlit
1d8d8 65 33 53 74 72 49 43 6d 70 28 75 2e 61 71 2e 70  e3StrICmp(u.aq.p
1d8d9 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65  Savepoint->zName
1d8da 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20  , u.aq.zName);. 
1d8db 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70       u.aq.pSavep
1d8dc 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76  oint = u.aq.pSav
1d8dd 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20  epoint->pNext.  
1d8de 20 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e    ){.      u.aq.
1d8df 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20  iSavepoint++;.  
1d8e0 20 20 7d 0a 20 20 20 20 69 66 28 20 21 75 2e 61    }.    if( !u.a
1d8e1 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a  q.pSavepoint ){.
1d8e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74        sqlite3Set
1d8e3 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1d8e4 73 67 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63 68  sg, db, "no such
1d8e5 20 73 61 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c   savepoint: %s",
1d8e6 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20   u.aq.zName);.  
1d8e7 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1d8e8 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65  ERROR;.    }else
1d8e9 20 69 66 28 0a 20 20 20 20 20 20 20 20 64 62 2d   if(.        db-
1d8ea 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20  >writeVdbeCnt>0 
1d8eb 7c 7c 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56  || (u.aq.p1==SAV
1d8ec 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1d8ed 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  && db->activeVdb
1d8ee 65 43 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20  eCnt>1).    ){. 
1d8ef 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f       /* It is no
1d8f0 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65  t possible to re
1d8f1 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61  lease (commit) a
1d8f2 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68   savepoint if th
1d8f3 65 72 65 20 61 72 65 0a 20 20 20 20 20 20 2a 2a  ere are.      **
1d8f4 20 61 63 74 69 76 65 20 77 72 69 74 65 20 73 74   active write st
1d8f5 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20  atements. It is 
1d8f6 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
1d8f7 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 65 70  rollback a savep
1d8f8 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69 66  oint.      ** if
1d8f9 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61   there are any a
1d8fa 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73  ctive statements
1d8fb 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a   at all..      *
1d8fc 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  /.      sqlite3S
1d8fd 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1d8fe 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20  rMsg, db,.      
1d8ff 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73 61 76    "cannot %s sav
1d900 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73 74 61  epoint - SQL sta
1d901 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72  tements in progr
1d902 65 73 73 22 2c 0a 20 20 20 20 20 20 20 20 28 75  ess",.        (u
1d903 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e  .aq.p1==SAVEPOIN
1d904 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f  T_ROLLBACK ? "ro
1d905 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c 65 61 73  llback": "releas
1d906 65 22 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  e").      );.   
1d907 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
1d908 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  USY;.    }else{.
1d909 0a 20 20 20 20 20 20 2f 2a 20 44 65 74 65 72 6d  .      /* Determ
1d90a 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
1d90b 6f 74 20 74 68 69 73 20 69 73 20 61 20 74 72 61  ot this is a tra
1d90c 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69  nsaction savepoi
1d90d 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20  nt. If so,.     
1d90e 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 69 73 20   ** and this is 
1d90f 61 20 52 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e  a RELEASE comman
1d910 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 72  d, then the curr
1d911 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ent transaction.
1d912 20 20 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d        ** is comm
1d913 69 74 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  itted..      */.
1d914 20 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e        int isTran
1d915 73 61 63 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70  saction = u.aq.p
1d916 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74  Savepoint->pNext
1d917 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73 54 72 61  ==0 && db->isTra
1d918 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e  nsactionSavepoin
1d919 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 54  t;.      if( isT
1d91a 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 75 2e  ransaction && u.
1d91b 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  aq.p1==SAVEPOINT
1d91c 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20  _RELEASE ){.    
1d91d 20 20 20 20 69 66 28 20 28 72 63 20 3d 20 73 71      if( (rc = sq
1d91e 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b  lite3VdbeCheckFk
1d91f 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f  (p, 1))!=SQLITE_
1d920 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1d921 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1d922 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1d923 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1d924 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  it = 1;.        
1d925 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48  if( sqlite3VdbeH
1d926 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42  alt(p)==SQLITE_B
1d927 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  USY ){.         
1d928 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20   p->pc = pc;.   
1d929 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1d92a 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  ommit = 0;.     
1d92b 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20       p->rc = rc 
1d92c 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
1d92d 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64           goto vd
1d92e 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  be_return;.     
1d92f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d     }.        db-
1d930 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61  >isTransactionSa
1d931 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20  vepoint = 0;.   
1d932 20 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b       rc = p->rc;
1d933 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1d934 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65        u.aq.iSave
1d935 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76  point = db->nSav
1d936 65 70 6f 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53  epoint - u.aq.iS
1d937 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20  avepoint - 1;.  
1d938 20 20 20 20 20 20 66 6f 72 28 75 2e 61 71 2e 69        for(u.aq.i
1d939 69 3d 30 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d  i=0; u.aq.ii<db-
1d93a 3e 6e 44 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29  >nDb; u.aq.ii++)
1d93b 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
1d93c 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76   sqlite3BtreeSav
1d93d 65 70 6f 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75  epoint(db->aDb[u
1d93e 2e 61 71 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61  .aq.ii].pBt, u.a
1d93f 71 2e 70 31 2c 20 75 2e 61 71 2e 69 53 61 76 65  q.p1, u.aq.iSave
1d940 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  point);.        
1d941 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1d942 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1d943 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1d944 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1d945 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1d946 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  }.        if( u.
1d947 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  aq.p1==SAVEPOINT
1d948 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62  _ROLLBACK && (db
1d949 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49  ->flags&SQLITE_I
1d94a 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30  nternChanges)!=0
1d94b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
1d94c 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
1d94d 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
1d94e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1d94f 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
1d950 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
1d951 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1d952 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 67 61  }..      /* Rega
1d953 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
1d954 72 20 74 68 69 73 20 69 73 20 61 20 52 45 4c 45  r this is a RELE
1d955 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c  ASE or ROLLBACK,
1d956 20 64 65 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20   destroy all.   
1d957 20 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73     ** savepoints
1d958 20 6e 65 73 74 65 64 20 69 6e 73 69 64 65 20 6f   nested inside o
1d959 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  f the savepoint 
1d95a 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f  being operated o
1d95b 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  n. */.      whil
1d95c 65 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e  e( db->pSavepoin
1d95d 74 21 3d 75 2e 61 71 2e 70 53 61 76 65 70 6f 69  t!=u.aq.pSavepoi
1d95e 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  nt ){.        u.
1d95f 61 71 2e 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53  aq.pTmp = db->pS
1d960 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20  avepoint;.      
1d961 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74    db->pSavepoint
1d962 20 3d 20 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e   = u.aq.pTmp->pN
1d963 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ext;.        sql
1d964 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75  ite3DbFree(db, u
1d965 2e 61 71 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20  .aq.pTmp);.     
1d966 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e     db->nSavepoin
1d967 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  t--;.      }..  
1d968 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 73 20      /* If it is 
1d969 61 20 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20  a RELEASE, then 
1d96a 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65  destroy the save
1d96b 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72  point being oper
1d96c 61 74 65 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a  ated on.      **
1d96d 20 74 6f 6f 2e 20 49 66 20 69 74 20 69 73 20 61   too. If it is a
1d96e 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 2c 20 74 68   ROLLBACK TO, th
1d96f 65 6e 20 73 65 74 20 74 68 65 20 6e 75 6d 62 65  en set the numbe
1d970 72 20 6f 66 20 64 65 66 65 72 72 65 64 0a 20 20  r of deferred.  
1d971 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e      ** constrain
1d972 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65  t violations pre
1d973 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61  sent in the data
1d974 62 61 73 65 20 74 6f 20 74 68 65 20 76 61 6c 75  base to the valu
1d975 65 20 73 74 6f 72 65 64 0a 20 20 20 20 20 20 2a  e stored.      *
1d976 2a 20 77 68 65 6e 20 74 68 65 20 73 61 76 65 70  * when the savep
1d977 6f 69 6e 74 20 77 61 73 20 63 72 65 61 74 65 64  oint was created
1d978 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  .  */.      if( 
1d979 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49  u.aq.p1==SAVEPOI
1d97a 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20  NT_RELEASE ){.  
1d97b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1d97c 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64  aq.pSavepoint==d
1d97d 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b  b->pSavepoint );
1d97e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61  .        db->pSa
1d97f 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70  vepoint = u.aq.p
1d980 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74  Savepoint->pNext
1d981 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1d982 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71  3DbFree(db, u.aq
1d983 2e 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20  .pSavepoint);.  
1d984 20 20 20 20 20 20 69 66 28 20 21 69 73 54 72 61        if( !isTra
1d985 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nsaction ){.    
1d986 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70        db->nSavep
1d987 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20  oint--;.        
1d988 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
1d989 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65         db->nDefe
1d98a 72 72 65 64 43 6f 6e 73 20 3d 20 75 2e 61 71 2e  rredCons = u.aq.
1d98b 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 44 65 66  pSavepoint->nDef
1d98c 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20  erredCons;.     
1d98d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
1d98e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1d98f 6f 64 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20  ode: AutoCommit 
1d990 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1d991 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 61  * Set the databa
1d992 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66  se auto-commit f
1d993 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20  lag to P1 (1 or 
1d994 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72 75  0). If P2 is tru
1d995 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20  e, roll.** back 
1d996 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63  any currently ac
1d997 74 69 76 65 20 62 74 72 65 65 20 74 72 61 6e 73  tive btree trans
1d998 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72  actions. If ther
1d999 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65  e are any active
1d99a 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66  .** VMs (apart f
1d99b 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74  rom this one), t
1d99c 68 65 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66  hen a ROLLBACK f
1d99d 61 69 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20  ails.  A COMMIT 
1d99e 66 61 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72  fails if.** ther
1d99f 65 20 61 72 65 20 61 63 74 69 76 65 20 77 72 69  e are active wri
1d9a0 74 69 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69  ting VMs or acti
1d9a1 76 65 20 56 4d 73 20 74 68 61 74 20 75 73 65 20  ve VMs that use 
1d9a2 73 68 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a  shared cache..**
1d9a3 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
1d9a4 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20  tion causes the 
1d9a5 56 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63  VM to halt..*/.c
1d9a6 61 73 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  ase OP_AutoCommi
1d9a7 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  t: {.#if 0  /* l
1d9a8 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d9a9 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a  oved into u.ar *
1d9aa 2f 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 41  /.  int desiredA
1d9ab 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74  utoCommit;.  int
1d9ac 20 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e   iRollback;.  in
1d9ad 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64  t turnOnAC;.#end
1d9ae 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1d9af 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d9b0 20 75 2e 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72   u.ar */..  u.ar
1d9b1 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d  .desiredAutoComm
1d9b2 69 74 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  it = pOp->p1;.  
1d9b3 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d  u.ar.iRollback =
1d9b4 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72   pOp->p2;.  u.ar
1d9b5 2e 74 75 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72  .turnOnAC = u.ar
1d9b6 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d  .desiredAutoComm
1d9b7 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43  it && !db->autoC
1d9b8 6f 6d 6d 69 74 3b 0a 20 20 61 73 73 65 72 74 28  ommit;.  assert(
1d9b9 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74   u.ar.desiredAut
1d9ba 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e  oCommit==1 || u.
1d9bb 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f  ar.desiredAutoCo
1d9bc 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73  mmit==0 );.  ass
1d9bd 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65  ert( u.ar.desire
1d9be 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c  dAutoCommit==1 |
1d9bf 7c 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b  | u.ar.iRollback
1d9c0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1d9c1 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
1d9c2 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c  nt>0 );  /* At l
1d9c3 65 61 73 74 20 74 68 69 73 20 6f 6e 65 20 56 4d  east this one VM
1d9c4 20 69 73 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20   is active */.. 
1d9c5 20 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e   if( u.ar.turnOn
1d9c6 41 43 20 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c  AC && u.ar.iRoll
1d9c7 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 74 69  back && db->acti
1d9c8 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20  veVdbeCnt>1 ){. 
1d9c9 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e     /* If this in
1d9ca 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d  struction implem
1d9cb 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20  ents a ROLLBACK 
1d9cc 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72  and other VMs ar
1d9cd 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72  e.    ** still r
1d9ce 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 74 72  unning, and a tr
1d9cf 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
1d9d0 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65  ive, return an e
1d9d1 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 0a  rror indicating.
1d9d2 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20      ** that the 
1d9d3 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63  other VMs must c
1d9d4 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a 20  omplete first.. 
1d9d5 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
1d9d6 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1d9d7 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e  ErrMsg, db, "can
1d9d8 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61  not rollback tra
1d9d9 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20  nsaction - ".   
1d9da 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d       "SQL statem
1d9db 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73  ents in progress
1d9dc 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ");.    rc = SQL
1d9dd 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73  ITE_BUSY;.  }els
1d9de 65 20 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f  e if( u.ar.turnO
1d9df 6e 41 43 20 26 26 20 21 75 2e 61 72 2e 69 52 6f  nAC && !u.ar.iRo
1d9e0 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72  llback && db->wr
1d9e1 69 74 65 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a  iteVdbeCnt>0 ){.
1d9e2 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
1d9e3 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65  nstruction imple
1d9e4 6d 65 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61  ments a COMMIT a
1d9e5 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65  nd other VMs are
1d9e6 20 77 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20   writing.    ** 
1d9e7 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1d9e8 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
1d9e9 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75  the other VMs mu
1d9ea 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73  st complete firs
1d9eb 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  t..    */.    sq
1d9ec 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1d9ed 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1d9ee 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74  "cannot commit t
1d9ef 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20  ransaction - ". 
1d9f0 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74         "SQL stat
1d9f1 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65  ements in progre
1d9f2 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ss");.    rc = S
1d9f3 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65  QLITE_BUSY;.  }e
1d9f4 6c 73 65 20 69 66 28 20 75 2e 61 72 2e 64 65 73  lse if( u.ar.des
1d9f5 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d  iredAutoCommit!=
1d9f6 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
1d9f7 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 72 2e 69  {.    if( u.ar.i
1d9f8 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Rollback ){.    
1d9f9 20 20 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64    assert( u.ar.d
1d9fa 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1d9fb 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
1d9fc 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28  ite3RollbackAll(
1d9fd 64 62 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61  db);.      db->a
1d9fe 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20  utoCommit = 1;. 
1d9ff 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63     }else if( (rc
1da00 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68   = sqlite3VdbeCh
1da01 65 63 6b 46 6b 28 70 2c 20 31 29 29 21 3d 53 51  eckFk(p, 1))!=SQ
1da02 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1da03 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72   goto vdbe_retur
1da04 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  n;.    }else{.  
1da05 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1da06 69 74 20 3d 20 28 75 38 29 75 2e 61 72 2e 64 65  it = (u8)u.ar.de
1da07 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b  siredAutoCommit;
1da08 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1da09 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53  e3VdbeHalt(p)==S
1da0a 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
1da0b 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
1da0c 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75  ;.        db->au
1da0d 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28  toCommit = (u8)(
1da0e 31 2d 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75  1-u.ar.desiredAu
1da0f 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20  toCommit);.     
1da10 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20     p->rc = rc = 
1da11 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
1da12 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72       goto vdbe_r
1da13 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
1da14 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1da15 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d   db->nStatement=
1da16 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
1da17 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73  3CloseSavepoints
1da18 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  (db);.    if( p-
1da19 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc==SQLITE_OK )
1da1a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1da1b 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65  ITE_DONE;.    }e
1da1c 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1da1d 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1da1e 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62    }.    goto vdb
1da1f 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73  e_return;.  }els
1da20 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  e{.    sqlite3Se
1da21 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1da22 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20  Msg, db,.       
1da23 20 28 21 75 2e 61 72 2e 64 65 73 69 72 65 64 41   (!u.ar.desiredA
1da24 75 74 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e  utoCommit)?"cann
1da25 6f 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73  ot start a trans
1da26 61 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 61 20  action within a 
1da27 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20  transaction":(. 
1da28 20 20 20 20 20 20 20 28 75 2e 61 72 2e 69 52 6f         (u.ar.iRo
1da29 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20  llback)?"cannot 
1da2a 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72  rollback - no tr
1da2b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
1da2c 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20  ive":.          
1da2d 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74           "cannot
1da2e 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61   commit - no tra
1da2f 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
1da30 76 65 22 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d  ve"));..    rc =
1da31 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1da32 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1da33 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61  * Opcode: Transa
1da34 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20  ction P1 P2 * * 
1da35 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20  *.**.** Begin a 
1da36 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68  transaction.  Th
1da37 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e  e transaction en
1da38 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74  ds when a Commit
1da39 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20   or Rollback.** 
1da3a 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e  opcode is encoun
1da3b 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e  tered.  Dependin
1da3c 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46  g on the ON CONF
1da3d 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68  LICT setting, th
1da3e 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  e.** transaction
1da3f 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72   might also be r
1da40 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e  olled back if an
1da41 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
1da42 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  tered..**.** P1 
1da43 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
1da44 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1da45 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74  e on which the t
1da46 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
1da47 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78   started.  Index
1da48 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64   0 is the main d
1da49 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1da4a 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a   index 1 is the.
1da4b 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72  ** file used for
1da4c 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1da4d 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32  s.  Indices of 2
1da4e 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65   or more are use
1da4f 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65  d for.** attache
1da50 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a  d databases..**.
1da51 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d  ** If P2 is non-
1da52 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69  zero, then a wri
1da53 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te-transaction i
1da54 73 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 45  s started.  A RE
1da55 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a  SERVED lock is.*
1da56 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68  * obtained on th
1da57 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1da58 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61  when a write-tra
1da59 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72  nsaction is star
1da5a 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65  ted.  No.** othe
1da5b 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74  r process can st
1da5c 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74  art another writ
1da5d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68  e transaction wh
1da5e 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 63  ile this transac
1da5f 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72  tion is.** under
1da60 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61  way.  Starting a
1da61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1da62 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20  on also creates 
1da63 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
1da64 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74  al. A.** write t
1da65 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
1da66 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72  be started befor
1da67 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61  e any changes ca
1da68 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65  n be made to the
1da69 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49  .** database.  I
1da6a 66 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 65  f P2 is 2 or gre
1da6b 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43  ater then an EXC
1da6c 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61  LUSIVE lock is a
1da6d 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  lso obtained.** 
1da6e 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a  on the file..**.
1da6f 2a 2a 20 49 66 20 61 20 77 72 69 74 65 2d 74 72  ** If a write-tr
1da70 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61  ansaction is sta
1da71 72 74 65 64 20 61 6e 64 20 74 68 65 20 56 64 62  rted and the Vdb
1da72 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61  e.usesStmtJourna
1da73 6c 20 66 6c 61 67 20 69 73 0a 2a 2a 20 74 72 75  l flag is.** tru
1da74 65 20 28 74 68 69 73 20 66 6c 61 67 20 69 73 20  e (this flag is 
1da75 73 65 74 20 69 66 20 74 68 65 20 56 64 62 65 20  set if the Vdbe 
1da76 6d 61 79 20 6d 6f 64 69 66 79 20 6d 6f 72 65 20  may modify more 
1da77 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 61 6e 64  than one row and
1da78 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e   may.** throw an
1da79 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e   ABORT exception
1da7a 29 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  ), a statement t
1da7b 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 61  ransaction may a
1da7c 6c 73 6f 20 62 65 20 6f 70 65 6e 65 64 2e 0a 2a  lso be opened..*
1da7d 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 61  * More specifica
1da7e 6c 6c 79 2c 20 61 20 73 74 61 74 65 6d 65 6e 74  lly, a statement
1da7f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1da80 6f 70 65 6e 65 64 20 69 66 66 20 74 68 65 20 64  opened iff the d
1da81 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65  atabase.** conne
1da82 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74  ction is current
1da83 6c 79 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f  ly not in autoco
1da84 6d 6d 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66  mmit mode, or if
1da85 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72   there are other
1da86 0a 2a 2a 20 61 63 74 69 76 65 20 73 74 61 74 65  .** active state
1da87 6d 65 6e 74 73 2e 20 41 20 73 74 61 74 65 6d 65  ments. A stateme
1da88 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  nt transaction a
1da89 6c 6c 6f 77 73 20 74 68 65 20 61 66 66 65 63 74  llows the affect
1da8a 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 56 44 42  s of this.** VDB
1da8b 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  E to be rolled b
1da8c 61 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 72  ack after an err
1da8d 6f 72 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e  or without havin
1da8e 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74  g to roll back t
1da8f 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72 61  he.** entire tra
1da90 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  nsaction. If no 
1da91 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
1da92 65 72 65 64 2c 20 74 68 65 20 73 74 61 74 65 6d  ered, the statem
1da93 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ent transaction.
1da94 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69  ** will automati
1da95 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20 77 68 65  cally commit whe
1da96 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74 73  n the VDBE halts
1da97 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73  ..**.** If P2 is
1da98 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 72 65   zero, then a re
1da99 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69  ad-lock is obtai
1da9a 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ned on the datab
1da9b 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73  ase file..*/.cas
1da9c 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e  e OP_Transaction
1da9d 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1da9e 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1da9f 76 65 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f  ved into u.as */
1daa0 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23  .  Btree *pBt;.#
1daa1 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1daa2 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1daa3 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 61  nto u.as */..  a
1daa4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1daa5 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d  0 && pOp->p1<db-
1daa6 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1daa7 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1daa8 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21  & (1<<pOp->p1))!
1daa9 3d 30 20 29 3b 0a 20 20 75 2e 61 73 2e 70 42 74  =0 );.  u.as.pBt
1daaa 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e   = db->aDb[pOp->
1daab 70 31 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20  p1].pBt;..  if( 
1daac 75 2e 61 73 2e 70 42 74 20 29 7b 0a 20 20 20 20  u.as.pBt ){.    
1daad 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1daae 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 73  eBeginTrans(u.as
1daaf 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a  .pBt, pOp->p2);.
1dab0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1dab1 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20  TE_BUSY ){.     
1dab2 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20   p->pc = pc;.   
1dab3 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20     p->rc = rc = 
1dab4 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
1dab5 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
1dab6 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  urn;.    }.    i
1dab7 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1dab8 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61   ){.      goto a
1dab9 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1daba 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  r;.    }..    if
1dabb 28 20 70 4f 70 2d 3e 70 32 20 26 26 20 70 2d 3e  ( pOp->p2 && p->
1dabc 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 0a  usesStmtJournal.
1dabd 20 20 20 20 20 26 26 20 28 64 62 2d 3e 61 75 74       && (db->aut
1dabe 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62  oCommit==0 || db
1dabf 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
1dac0 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  1).    ){.      
1dac1 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1dac2 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 75 2e  treeIsInTrans(u.
1dac3 61 73 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 20  as.pBt) );.     
1dac4 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65   if( p->iStateme
1dac5 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  nt==0 ){.       
1dac6 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 74   assert( db->nSt
1dac7 61 74 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62  atement>=0 && db
1dac8 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20  ->nSavepoint>=0 
1dac9 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e  );.        db->n
1daca 53 74 61 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 20  Statement++;.   
1dacb 20 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65       p->iStateme
1dacc 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f  nt = db->nSavepo
1dacd 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65  int + db->nState
1dace 6d 65 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ment;.      }.  
1dacf 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1dad0 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 75  BtreeBeginStmt(u
1dad1 2e 61 73 2e 70 42 74 2c 20 70 2d 3e 69 53 74 61  .as.pBt, p->iSta
1dad2 74 65 6d 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20  tement);..      
1dad3 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 63 75 72  /* Store the cur
1dad4 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
1dad5 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1dad6 65 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  es deferred cons
1dad7 74 72 61 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20  traint.      ** 
1dad8 63 6f 75 6e 74 65 72 2e 20 49 66 20 74 68 65 20  counter. If the 
1dad9 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1dada 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 62  ction needs to b
1dadb 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 0a 20  e rolled back,. 
1dadc 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75       ** the valu
1dadd 65 20 6f 66 20 74 68 69 73 20 63 6f 75 6e 74 65  e of this counte
1dade 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  r needs to be re
1dadf 73 74 6f 72 65 64 20 74 6f 6f 2e 20 20 2a 2f 0a  stored too.  */.
1dae0 20 20 20 20 20 20 70 2d 3e 6e 53 74 6d 74 44 65        p->nStmtDe
1dae1 66 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66  fCons = db->nDef
1dae2 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 7d  erredCons;.    }
1dae3 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1dae4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64  ./* Opcode: Read
1dae5 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20  Cookie P1 P2 P3 
1dae6 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63  * *.**.** Read c
1dae7 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20  ookie number P3 
1dae8 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31  from database P1
1dae9 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e   and write it in
1daea 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1daeb 2a 2a 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20  ** P3==1 is the 
1daec 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20  schema version. 
1daed 20 50 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61   P3==2 is the da
1daee 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a  tabase format..*
1daef 2a 20 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72  * P3==3 is the r
1daf0 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72  ecommended pager
1daf1 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64   cache size, and
1daf2 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d   so forth.  P1==
1daf3 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e  0 is.** the main
1daf4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
1daf5 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20  nd P1==1 is the 
1daf6 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73  database file us
1daf7 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74  ed to store.** t
1daf8 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
1daf9 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
1dafa 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b  t be a read-lock
1dafb 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1dafc 20 28 65 69 74 68 65 72 20 61 20 74 72 61 6e 73   (either a trans
1dafd 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62  action.** must b
1dafe 65 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 65  e started or the
1daff 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70  re must be an op
1db00 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72  en cursor) befor
1db01 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74  e.** executing t
1db02 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  his instruction.
1db03 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64  .*/.case OP_Read
1db04 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20  Cookie: {       
1db05 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1db06 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69  prerelease */.#i
1db07 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1db08 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1db09 74 6f 20 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74  to u.at */.  int
1db0a 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44   iMeta;.  int iD
1db0b 62 3b 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65  b;.  int iCookie
1db0c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1db0d 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1db0e 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a  d into u.at */..
1db0f 20 20 75 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70    u.at.iDb = pOp
1db10 2d 3e 70 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f  ->p1;.  u.at.iCo
1db11 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a  okie = pOp->p3;.
1db12 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1db13 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45  3<SQLITE_N_BTREE
1db14 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72  _META );.  asser
1db15 74 28 20 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26  t( u.at.iDb>=0 &
1db16 26 20 75 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e  & u.at.iDb<db->n
1db17 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
1db18 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62  db->aDb[u.at.iDb
1db19 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73  ].pBt!=0 );.  as
1db1a 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
1db1b 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69  ask & (1<<u.at.i
1db1c 44 62 29 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71  Db))!=0 );..  sq
1db1d 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
1db1e 61 28 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69  a(db->aDb[u.at.i
1db1f 44 62 5d 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43  Db].pBt, u.at.iC
1db20 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75  ookie, (u32 *)&u
1db21 2e 61 74 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f  .at.iMeta);.  pO
1db22 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69  ut->u.i = u.at.i
1db23 4d 65 74 61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Meta;.  break;.}
1db24 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74  ../* Opcode: Set
1db25 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20  Cookie P1 P2 P3 
1db26 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1db27 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1db28 65 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65  egister P3 (inte
1db29 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e  rpreted as an in
1db2a 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63  teger).** into c
1db2b 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20  ookie number P2 
1db2c 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 20  of database P1. 
1db2d 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 73 63   P2==1 is the sc
1db2e 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a  hema version.  .
1db2f 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20  ** P2==2 is the 
1db30 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e  database format.
1db31 20 50 32 3d 3d 33 20 69 73 20 74 68 65 20 72 65   P2==3 is the re
1db32 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20  commended pager 
1db33 63 61 63 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20  cache .** size, 
1db34 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50  and so forth.  P
1db35 31 3d 3d 30 20 69 73 20 74 68 65 20 6d 61 69 6e  1==0 is the main
1db36 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
1db37 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20  nd P1==1 is the 
1db38 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
1db39 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
1db3a 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1db3b 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61  ..**.** A transa
1db3c 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74  ction must be st
1db3d 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65  arted before exe
1db3e 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f  cuting this opco
1db3f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  de..*/.case OP_S
1db40 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20  etCookie: {     
1db41 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20    /* in3 */.#if 
1db42 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1db43 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1db44 20 75 2e 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70   u.au */.  Db *p
1db45 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  Db;.#endif /* lo
1db46 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1db47 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f  ved into u.au */
1db48 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1db49 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45  p2<SQLITE_N_BTRE
1db4a 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65  E_META );.  asse
1db4b 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1db4c 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
1db4d 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1db4e 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1db4f 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20  1<<pOp->p1))!=0 
1db50 29 3b 0a 20 20 75 2e 61 75 2e 70 44 62 20 3d 20  );.  u.au.pDb = 
1db51 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31  &db->aDb[pOp->p1
1db52 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  ];.  assert( u.a
1db53 75 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b  u.pDb->pBt!=0 );
1db54 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b  .  pIn3 = &aMem[
1db55 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 73 71 6c 69  pOp->p3];.  sqli
1db56 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
1db57 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a  rify(pIn3);.  /*
1db58 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20   See note about 
1db59 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f  index shifting o
1db5a 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20  n OP_ReadCookie 
1db5b 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
1db5c 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
1db5d 28 75 2e 61 75 2e 70 44 62 2d 3e 70 42 74 2c 20  (u.au.pDb->pBt, 
1db5e 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49  pOp->p2, (int)pI
1db5f 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20  n3->u.i);.  if( 
1db60 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 53  pOp->p2==BTREE_S
1db61 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 29 7b  CHEMA_VERSION ){
1db62 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65  .    /* When the
1db63 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
1db64 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74  hanges, record t
1db65 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e  he new cookie in
1db66 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20  ternally */.    
1db67 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d  u.au.pDb->pSchem
1db68 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  a->schema_cookie
1db69 20 3d 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e   = (int)pIn3->u.
1db6a 69 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  i;.    db->flags
1db6b 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
1db6c 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73  nChanges;.  }els
1db6d 65 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42  e if( pOp->p2==B
1db6e 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
1db6f 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72   ){.    /* Recor
1db70 64 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65  d changes in the
1db71 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a   file format */.
1db72 20 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53      u.au.pDb->pS
1db73 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
1db74 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75  at = (u8)pIn3->u
1db75 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f  .i;.  }.  if( pO
1db76 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20  p->p1==1 ){.    
1db77 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c  /* Invalidate al
1db78 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  l prepared state
1db79 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74  ments whenever t
1db7a 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
1db7b 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69  .    ** schema i
1db7c 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b  s changed.  Tick
1db7d 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20  et #1644 */.    
1db7e 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
1db7f 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
1db80 64 62 29 3b 0a 20 20 20 20 70 2d 3e 65 78 70 69  db);.    p->expi
1db81 72 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62  red = 0;.  }.  b
1db82 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1db83 64 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b 69 65  de: VerifyCookie
1db84 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43   P1 P2 *.**.** C
1db85 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f  heck the value o
1db86 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 62 61 73  f global databas
1db87 65 20 70 61 72 61 6d 65 74 65 72 20 6e 75 6d 62  e parameter numb
1db88 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 73 63 68  er 0 (the.** sch
1db89 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 61 6e 64  ema version) and
1db8a 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
1db8b 20 65 71 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a   equal to P2.  .
1db8c 2a 2a 20 50 31 20 69 73 20 74 68 65 20 64 61 74  ** P1 is the dat
1db8d 61 62 61 73 65 20 6e 75 6d 62 65 72 20 77 68 69  abase number whi
1db8e 63 68 20 69 73 20 30 20 66 6f 72 20 74 68 65 20  ch is 0 for the 
1db8f 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1db90 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20  le.** and 1 for 
1db91 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67  the file holding
1db92 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1db93 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 67 68 65  s and some highe
1db94 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20  r number.** for 
1db95 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
1db96 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ses..**.** The c
1db97 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 20 69 74  ookie changes it
1db98 73 20 76 61 6c 75 65 20 77 68 65 6e 65 76 65 72  s value whenever
1db99 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1db9a 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a  hema changes..**
1db9b 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   This operation 
1db9c 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 63  is used to detec
1db9d 74 20 77 68 65 6e 20 74 68 61 74 20 74 68 65 20  t when that the 
1db9e 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 61 6e 67  cookie has chang
1db9f 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74  ed.** and that t
1dba0 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65  he current proce
1dba1 73 73 20 6e 65 65 64 73 20 74 6f 20 72 65 72 65  ss needs to rere
1dba2 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a  ad the schema..*
1dba3 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 20 74 72  *.** Either a tr
1dba4 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20  ansaction needs 
1dba5 74 6f 20 68 61 76 65 20 62 65 65 6e 20 73 74 61  to have been sta
1dba6 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70  rted or an OP_Op
1dba7 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62  en needs.** to b
1dba8 65 20 65 78 65 63 75 74 65 64 20 28 74 6f 20 65  e executed (to e
1dba9 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 20  stablish a read 
1dbaa 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 74 68 69  lock) before thi
1dbab 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69  s opcode is.** i
1dbac 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20  nvoked..*/.case 
1dbad 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a  OP_VerifyCookie:
1dbae 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1dbaf 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1dbb0 65 64 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a  ed into u.av */.
1dbb1 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42    int iMeta;.  B
1dbb2 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69  tree *pBt;.#endi
1dbb3 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1dbb4 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1dbb5 75 2e 61 76 20 2a 2f 0a 20 20 61 73 73 65 72 74  u.av */.  assert
1dbb6 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1dbb7 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
1dbb8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1dbb9 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1dbba 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b  <pOp->p1))!=0 );
1dbbb 0a 20 20 75 2e 61 76 2e 70 42 74 20 3d 20 64 62  .  u.av.pBt = db
1dbbc 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
1dbbd 42 74 3b 0a 20 20 69 66 28 20 75 2e 61 76 2e 70  Bt;.  if( u.av.p
1dbbe 42 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Bt ){.    sqlite
1dbbf 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 75 2e  3BtreeGetMeta(u.
1dbc0 61 76 2e 70 42 74 2c 20 42 54 52 45 45 5f 53 43  av.pBt, BTREE_SC
1dbc1 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75  HEMA_VERSION, (u
1dbc2 33 32 20 2a 29 26 75 2e 61 76 2e 69 4d 65 74 61  32 *)&u.av.iMeta
1dbc3 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1dbc4 75 2e 61 76 2e 69 4d 65 74 61 20 3d 20 30 3b 0a  u.av.iMeta = 0;.
1dbc5 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 76 2e 69    }.  if( u.av.i
1dbc6 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b  Meta!=pOp->p2 ){
1dbc7 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1dbc8 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1dbc9 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d  g);.    p->zErrM
1dbca 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  sg = sqlite3DbSt
1dbcb 72 44 75 70 28 64 62 2c 20 22 64 61 74 61 62 61  rDup(db, "databa
1dbcc 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68  se schema has ch
1dbcd 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20  anged");.    /* 
1dbce 49 66 20 74 68 65 20 73 63 68 65 6d 61 2d 63 6f  If the schema-co
1dbcf 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61  okie from the da
1dbd0 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 74 63  tabase file matc
1dbd1 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20  hes the cookie. 
1dbd2 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 77 69 74     ** stored wit
1dbd3 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  h the in-memory 
1dbd4 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
1dbd5 66 20 74 68 65 20 73 63 68 65 6d 61 2c 20 64 6f  f the schema, do
1dbd6 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f  .    ** not relo
1dbd7 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 66 72  ad the schema fr
1dbd8 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
1dbd9 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  file..    **.   
1dbda 20 2a 2a 20 49 66 20 76 69 72 74 75 61 6c 2d 74   ** If virtual-t
1dbdb 61 62 6c 65 73 20 61 72 65 20 69 6e 20 75 73 65  ables are in use
1dbdc 2c 20 74 68 69 73 20 69 73 20 6e 6f 74 20 6a 75  , this is not ju
1dbdd 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  st an optimizati
1dbde 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e  on..    ** Often
1dbdf 2c 20 76 2d 74 61 62 6c 65 73 20 73 74 6f 72 65  , v-tables store
1dbe0 20 74 68 65 69 72 20 64 61 74 61 20 69 6e 20 6f   their data in o
1dbe1 74 68 65 72 20 53 51 4c 69 74 65 20 74 61 62 6c  ther SQLite tabl
1dbe2 65 73 2c 20 77 68 69 63 68 0a 20 20 20 20 2a 2a  es, which.    **
1dbe3 20 61 72 65 20 71 75 65 72 69 65 64 20 66 72 6f   are queried fro
1dbe4 6d 20 77 69 74 68 69 6e 20 78 4e 65 78 74 28 29  m within xNext()
1dbe5 20 61 6e 64 20 6f 74 68 65 72 20 76 2d 74 61 62   and other v-tab
1dbe6 6c 65 20 6d 65 74 68 6f 64 73 20 75 73 69 6e 67  le methods using
1dbe7 0a 20 20 20 20 2a 2a 20 70 72 65 70 61 72 65 64  .    ** prepared
1dbe8 20 71 75 65 72 69 65 73 2e 20 49 66 20 73 75 63   queries. If suc
1dbe9 68 20 61 20 71 75 65 72 79 20 69 73 20 6f 75 74  h a query is out
1dbea 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20 64 6f 20  -of-date, we do 
1dbeb 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20 20 20 20  not want to.    
1dbec 2a 2a 20 64 69 73 63 61 72 64 20 74 68 65 20 64  ** discard the d
1dbed 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2c 20  atabase schema, 
1dbee 61 73 20 74 68 65 20 75 73 65 72 20 63 6f 64 65  as the user code
1dbef 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68   implementing th
1dbf0 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65  e.    ** v-table
1dbf1 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 62   would have to b
1dbf2 65 20 72 65 61 64 79 20 66 6f 72 20 74 68 65 20  e ready for the 
1dbf3 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72  sqlite3_vtab str
1dbf4 75 63 74 75 72 65 20 69 74 73 65 6c 66 0a 20 20  ucture itself.  
1dbf5 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c    ** to be inval
1dbf6 69 64 61 74 65 64 20 77 68 65 6e 65 76 65 72 20  idated whenever 
1dbf7 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69  sqlite3_step() i
1dbf8 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  s called from wi
1dbf9 74 68 69 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d  thin.    ** a v-
1dbfa 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20  table method..  
1dbfb 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d    */.    if( db-
1dbfc 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53  >aDb[pOp->p1].pS
1dbfd 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f  chema->schema_co
1dbfe 6f 6b 69 65 21 3d 75 2e 61 76 2e 69 4d 65 74 61  okie!=u.av.iMeta
1dbff 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1dc00 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1dc01 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31  hema(db, pOp->p1
1dc02 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  );.    }..    sq
1dc03 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
1dc04 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
1dc05 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
1dc06 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20  TE_SCHEMA;.  }. 
1dc07 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1dc08 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50  code: OpenRead P
1dc09 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1dc0a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d  .** Open a read-
1dc0b 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20  only cursor for 
1dc0c 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62  the database tab
1dc0d 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61  le whose root pa
1dc0e 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61  ge is.** P2 in a
1dc0f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1dc10 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
1dc11 6c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  le is determined
1dc12 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d   by P3. .** P3==
1dc13 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e  0 means the main
1dc14 20 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31   database, P3==1
1dc15 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62   means the datab
1dc16 61 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a  ase used for .**
1dc17 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1dc18 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e  s, and P3>1 mean
1dc19 73 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 65  s used the corre
1dc1a 73 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65  sponding attache
1dc1b 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20  d.** database.  
1dc1c 47 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 72  Give the new cur
1dc1d 73 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65  sor an identifie
1dc1e 72 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31  r of P1.  The P1
1dc1f 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20  .** values need 
1dc20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75  not be contiguou
1dc21 73 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c  s but all P1 val
1dc22 75 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d  ues should be sm
1dc23 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a  all integers..**
1dc24 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   It is an error 
1dc25 66 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67  for P1 to be neg
1dc26 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ative..**.** If 
1dc27 50 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74  P5!=0 then use t
1dc28 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1dc29 67 69 73 74 65 72 20 50 32 20 61 73 20 74 68 65  gister P2 as the
1dc2a 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a   root page, not.
1dc2b 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
1dc2c 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a  P2 itself..**.**
1dc2d 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61   There will be a
1dc2e 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68   read lock on th
1dc2f 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 65  e database whene
1dc30 76 65 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a  ver there is an.
1dc31 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20  ** open cursor. 
1dc32 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
1dc33 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72   was unlocked pr
1dc34 69 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74  ior to this inst
1dc35 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20  ruction.** then 
1dc36 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61  a read lock is a
1dc37 63 71 75 69 72 65 64 20 61 73 20 70 61 72 74 20  cquired as part 
1dc38 6f 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  of this instruct
1dc39 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20  ion.  A read.** 
1dc3a 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65  lock allows othe
1dc3b 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72  r processes to r
1dc3c 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ead the database
1dc3d 20 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a   but prohibits.*
1dc3e 2a 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  * any other proc
1dc3f 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69  ess from modifyi
1dc40 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ng the database.
1dc41 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20    The read lock 
1dc42 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77  is.** released w
1dc43 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20  hen all cursors 
1dc44 61 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20  are closed.  If 
1dc45 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1dc46 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20   attempts.** to 
1dc47 67 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  get a read lock 
1dc48 62 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73  but fails, the s
1dc49 63 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73  cript terminates
1dc4a 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49   with an.** SQLI
1dc4b 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f  TE_BUSY error co
1dc4c 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34  de..**.** The P4
1dc4d 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69   value may be ei
1dc4e 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20  ther an integer 
1dc4f 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20  (P4_INT32) or a 
1dc50 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20  pointer to.** a 
1dc51 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
1dc52 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20  e (P4_KEYINFO). 
1dc53 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74  If it is a point
1dc54 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20  er to a KeyInfo 
1dc55 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74  .** structure, t
1dc56 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75  hen said structu
1dc57 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63  re defines the c
1dc58 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61  ontent and colla
1dc59 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63  ting .** sequenc
1dc5a 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62  e of the index b
1dc5b 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68  eing opened. Oth
1dc5c 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73  erwise, if P4 is
1dc5d 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20   an integer .** 
1dc5e 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74  value, it is set
1dc5f 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
1dc60 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
1dc61 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65   table..**.** Se
1dc62 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65  e also OpenWrite
1dc63 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1dc64 4f 70 65 6e 57 72 69 74 65 20 50 31 20 50 32 20  OpenWrite P1 P2 
1dc65 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f  P3 P4 P5.**.** O
1dc66 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65  pen a read/write
1dc67 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31   cursor named P1
1dc68 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72   on the table or
1dc69 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f   index whose roo
1dc6a 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e  t.** page is P2.
1dc6b 20 20 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73    Or if P5!=0 us
1dc6c 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
1dc6d 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20   register P2 to 
1dc6e 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74  find the.** root
1dc6f 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   page..**.** The
1dc70 20 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65   P4 value may be
1dc71 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67   either an integ
1dc72 65 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72  er (P4_INT32) or
1dc73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
1dc74 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
1dc75 74 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f  ture (P4_KEYINFO
1dc76 29 2e 20 49 66 20 69 74 20 69 73 20 61 20 70 6f  ). If it is a po
1dc77 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e  inter to a KeyIn
1dc78 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  fo .** structure
1dc79 2c 20 74 68 65 6e 20 73 61 69 64 20 73 74 72 75  , then said stru
1dc7a 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68  cture defines th
1dc7b 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f  e content and co
1dc7c 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75  llating .** sequ
1dc7d 65 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65  ence of the inde
1dc7e 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20  x being opened. 
1dc7f 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 34  Otherwise, if P4
1dc80 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a   is an integer .
1dc81 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20  ** value, it is 
1dc82 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
1dc83 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1dc84 74 68 65 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f  the table, or to
1dc85 20 74 68 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20   the.** largest 
1dc86 69 6e 64 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c  index of any col
1dc87 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65  umn of the table
1dc88 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c   that is actuall
1dc89 79 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  y used..**.** Th
1dc8a 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77  is instruction w
1dc8b 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f  orks just like O
1dc8c 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 74  penRead except t
1dc8d 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 65  hat it opens the
1dc8e 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65   cursor.** in re
1dc8f 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 20  ad/write mode.  
1dc90 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 6c  For a given tabl
1dc91 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 20  e, there can be 
1dc92 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64  one or more read
1dc93 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73  -only.** cursors
1dc94 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 61   or a single rea
1dc95 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 62  d/write cursor b
1dc96 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a  ut not both..**.
1dc97 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e  ** See also Open
1dc98 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  Read..*/.case OP
1dc99 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 20  _OpenRead:.case 
1dc9a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a  OP_OpenWrite: {.
1dc9b 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1dc9c 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1dc9d 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 20 20 69  into u.aw */.  i
1dc9e 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 4b 65 79  nt nField;.  Key
1dc9f 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a  Info *pKeyInfo;.
1dca0 20 20 69 6e 74 20 70 32 3b 0a 20 20 69 6e 74 20    int p2;.  int 
1dca1 69 44 62 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61  iDb;.  int wrFla
1dca2 67 3b 0a 20 20 42 74 72 65 65 20 2a 70 58 3b 0a  g;.  Btree *pX;.
1dca3 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1dca4 75 72 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23  ur;.  Db *pDb;.#
1dca5 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1dca6 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1dca7 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 0a 20 20 69  nto u.aw */..  i
1dca8 66 28 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b  f( p->expired ){
1dca9 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1dcaa 5f 41 42 4f 52 54 3b 0a 20 20 20 20 62 72 65 61  _ABORT;.    brea
1dcab 6b 3b 0a 20 20 7d 0a 0a 20 20 75 2e 61 77 2e 6e  k;.  }..  u.aw.n
1dcac 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 75 2e 61  Field = 0;.  u.a
1dcad 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a  w.pKeyInfo = 0;.
1dcae 20 20 75 2e 61 77 2e 70 32 20 3d 20 70 4f 70 2d    u.aw.p2 = pOp-
1dcaf 3e 70 32 3b 0a 20 20 75 2e 61 77 2e 69 44 62 20  >p2;.  u.aw.iDb 
1dcb0 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73  = pOp->p3;.  ass
1dcb1 65 72 74 28 20 75 2e 61 77 2e 69 44 62 3e 3d 30  ert( u.aw.iDb>=0
1dcb2 20 26 26 20 75 2e 61 77 2e 69 44 62 3c 64 62 2d   && u.aw.iDb<db-
1dcb3 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1dcb4 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1dcb5 26 20 28 31 3c 3c 75 2e 61 77 2e 69 44 62 29 29  & (1<<u.aw.iDb))
1dcb6 21 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 44  !=0 );.  u.aw.pD
1dcb7 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 75 2e 61  b = &db->aDb[u.a
1dcb8 77 2e 69 44 62 5d 3b 0a 20 20 75 2e 61 77 2e 70  w.iDb];.  u.aw.p
1dcb9 58 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 42  X = u.aw.pDb->pB
1dcba 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  t;.  assert( u.a
1dcbb 77 2e 70 58 21 3d 30 20 29 3b 0a 20 20 69 66 28  w.pX!=0 );.  if(
1dcbc 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1dcbd 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a 20 20  _OpenWrite ){.  
1dcbe 20 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20    u.aw.wrFlag = 
1dcbf 31 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 77 2e  1;.    if( u.aw.
1dcc0 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
1dcc1 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d  le_format < p->m
1dcc2 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61  inWriteFileForma
1dcc3 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69  t ){.      p->mi
1dcc4 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74  nWriteFileFormat
1dcc5 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63   = u.aw.pDb->pSc
1dcc6 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
1dcc7 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  t;.    }.  }else
1dcc8 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 6c 61  {.    u.aw.wrFla
1dcc9 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  g = 0;.  }.  if(
1dcca 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20   pOp->p5 ){.    
1dccb 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 3e  assert( u.aw.p2>
1dccc 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
1dccd 20 75 2e 61 77 2e 70 32 3c 3d 70 2d 3e 6e 4d 65   u.aw.p2<=p->nMe
1dcce 6d 20 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20  m );.    pIn2 = 
1dccf 26 61 4d 65 6d 5b 75 2e 61 77 2e 70 32 5d 3b 0a  &aMem[u.aw.p2];.
1dcd0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1dcd1 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e  emIntegerify(pIn
1dcd2 32 29 3b 0a 20 20 20 20 75 2e 61 77 2e 70 32 20  2);.    u.aw.p2 
1dcd3 3d 20 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69  = (int)pIn2->u.i
1dcd4 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 2e 61  ;.    /* The u.a
1dcd5 77 2e 70 32 20 76 61 6c 75 65 20 61 6c 77 61 79  w.p2 value alway
1dcd6 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70  s comes from a p
1dcd7 72 69 6f 72 20 4f 50 5f 43 72 65 61 74 65 54 61  rior OP_CreateTa
1dcd8 62 6c 65 20 6f 70 63 6f 64 65 20 61 6e 64 0a 20  ble opcode and. 
1dcd9 20 20 20 2a 2a 20 74 68 61 74 20 6f 70 63 6f 64     ** that opcod
1dcda 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 73 65  e will always se
1dcdb 74 20 74 68 65 20 75 2e 61 77 2e 70 32 20 76 61  t the u.aw.p2 va
1dcdc 6c 75 65 20 74 6f 20 32 20 6f 72 20 6d 6f 72 65  lue to 2 or more
1dcdd 20 6f 72 20 65 6c 73 65 20 66 61 69 6c 2e 0a 20   or else fail.. 
1dcde 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 77     ** If there w
1dcdf 65 72 65 20 61 20 66 61 69 6c 75 72 65 2c 20 74  ere a failure, t
1dce0 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
1dce1 65 6d 65 6e 74 20 77 6f 75 6c 64 20 68 61 76 65  ement would have
1dce2 20 68 61 6c 74 65 64 0a 20 20 20 20 2a 2a 20 62   halted.    ** b
1dce3 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74  efore reaching t
1dce4 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  his instruction.
1dce5 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45   */.    if( NEVE
1dce6 52 28 75 2e 61 77 2e 70 32 3c 32 29 20 29 20 7b  R(u.aw.p2<2) ) {
1dce7 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1dce8 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1dce9 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
1dcea 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1dceb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
1dcec 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1dced 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 75  KEYINFO ){.    u
1dcee 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70  .aw.pKeyInfo = p
1dcef 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b  Op->p4.pKeyInfo;
1dcf0 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e  .    u.aw.pKeyIn
1dcf1 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d  fo->enc = ENC(p-
1dcf2 3e 64 62 29 3b 0a 20 20 20 20 75 2e 61 77 2e 6e  >db);.    u.aw.n
1dcf3 46 69 65 6c 64 20 3d 20 75 2e 61 77 2e 70 4b 65  Field = u.aw.pKe
1dcf4 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b  yInfo->nField+1;
1dcf5 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70  .  }else if( pOp
1dcf6 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54  ->p4type==P4_INT
1dcf7 33 32 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 6e  32 ){.    u.aw.n
1dcf8 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e  Field = pOp->p4.
1dcf9 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  i;.  }.  assert(
1dcfa 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20   pOp->p1>=0 );. 
1dcfb 20 75 2e 61 77 2e 70 43 75 72 20 3d 20 61 6c 6c   u.aw.pCur = all
1dcfc 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70  ocateCursor(p, p
1dcfd 4f 70 2d 3e 70 31 2c 20 75 2e 61 77 2e 6e 46 69  Op->p1, u.aw.nFi
1dcfe 65 6c 64 2c 20 75 2e 61 77 2e 69 44 62 2c 20 31  eld, u.aw.iDb, 1
1dcff 29 3b 0a 20 20 69 66 28 20 75 2e 61 77 2e 70 43  );.  if( u.aw.pC
1dd00 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  ur==0 ) goto no_
1dd01 6d 65 6d 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72  mem;.  u.aw.pCur
1dd02 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20  ->nullRow = 1;. 
1dd03 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1dd04 65 65 43 75 72 73 6f 72 28 75 2e 61 77 2e 70 58  eeCursor(u.aw.pX
1dd05 2c 20 75 2e 61 77 2e 70 32 2c 20 75 2e 61 77 2e  , u.aw.p2, u.aw.
1dd06 77 72 46 6c 61 67 2c 20 75 2e 61 77 2e 70 4b 65  wrFlag, u.aw.pKe
1dd07 79 49 6e 66 6f 2c 20 75 2e 61 77 2e 70 43 75 72  yInfo, u.aw.pCur
1dd08 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e  ->pCursor);.  u.
1dd09 61 77 2e 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  aw.pCur->pKeyInf
1dd0a 6f 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66  o = u.aw.pKeyInf
1dd0b 6f 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 69  o;..  /* Since i
1dd0c 74 20 70 65 72 66 6f 72 6d 73 20 6e 6f 20 6d 65  t performs no me
1dd0d 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1dd0e 6f 72 20 49 4f 2c 20 74 68 65 20 6f 6e 6c 79 20  or IO, the only 
1dd0f 76 61 6c 75 65 73 20 74 68 61 74 0a 20 20 2a 2a  values that.  **
1dd10 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
1dd11 73 6f 72 28 29 20 6d 61 79 20 72 65 74 75 72 6e  sor() may return
1dd12 20 61 72 65 20 53 51 4c 49 54 45 5f 45 4d 50 54   are SQLITE_EMPT
1dd13 59 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 2e  Y and SQLITE_OK.
1dd14 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 45 4d 50  .  ** SQLITE_EMP
1dd15 54 59 20 69 73 20 6f 6e 6c 79 20 72 65 74 75 72  TY is only retur
1dd16 6e 65 64 20 77 68 65 6e 20 61 74 74 65 6d 70 74  ned when attempt
1dd17 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  ing to open the 
1dd18 74 61 62 6c 65 0a 20 20 2a 2a 20 72 6f 6f 74 65  table.  ** roote
1dd19 64 20 61 74 20 70 61 67 65 20 31 20 6f 66 20 61  d at page 1 of a
1dd1a 20 7a 65 72 6f 2d 62 79 74 65 20 64 61 74 61 62   zero-byte datab
1dd1b 61 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  ase.  */.  asser
1dd1c 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d  t( rc==SQLITE_EM
1dd1d 50 54 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  PTY || rc==SQLIT
1dd1e 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63  E_OK );.  if( rc
1dd1f 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29  ==SQLITE_EMPTY )
1dd20 7b 0a 20 20 20 20 75 2e 61 77 2e 70 43 75 72 2d  {.    u.aw.pCur-
1dd21 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  >pCursor = 0;.  
1dd22 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
1dd23 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
1dd24 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69  the VdbeCursor.i
1dd25 73 54 61 62 6c 65 20 61 6e 64 20 69 73 49 6e 64  sTable and isInd
1dd26 65 78 20 76 61 72 69 61 62 6c 65 73 2e 20 50 72  ex variables. Pr
1dd27 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20  evious versions 
1dd28 6f 66 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 75  of.  ** SQLite u
1dd29 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20  sed to check if 
1dd2a 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c  the root-page fl
1dd2b 61 67 73 20 77 65 72 65 20 73 61 6e 65 20 61 74  ags were sane at
1dd2c 20 74 68 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a   this point.  **
1dd2d 20 61 6e 64 20 72 65 70 6f 72 74 20 64 61 74 61   and report data
1dd2e 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
1dd2f 69 66 20 74 68 65 79 20 77 65 72 65 20 6e 6f 74  if they were not
1dd30 2c 20 62 75 74 20 74 68 69 73 20 63 68 65 63 6b  , but this check
1dd31 20 68 61 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20   has.  ** since 
1dd32 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 62  moved into the b
1dd33 74 72 65 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a  tree layer.  */.
1dd34 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54    u.aw.pCur->isT
1dd35 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79  able = pOp->p4ty
1dd36 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a  pe!=P4_KEYINFO;.
1dd37 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 49    u.aw.pCur->isI
1dd38 6e 64 65 78 20 3d 20 21 75 2e 61 77 2e 70 43 75  ndex = !u.aw.pCu
1dd39 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72  r->isTable;.  br
1dd3a 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1dd3b 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  e: OpenEphemeral
1dd3c 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a   P1 P2 * P4 *.**
1dd3d 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63  .** Open a new c
1dd3e 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 74 72  ursor P1 to a tr
1dd3f 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a  ansient table..*
1dd40 2a 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20  * The cursor is 
1dd41 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 20 72 65  always opened re
1dd42 61 64 2f 77 72 69 74 65 20 65 76 65 6e 20 69 66  ad/write even if
1dd43 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61   .** the main da
1dd44 74 61 62 61 73 65 20 69 73 20 72 65 61 64 2d 6f  tabase is read-o
1dd45 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 6e 73 69  nly.  The transi
1dd46 65 6e 74 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a  ent or virtual.*
1dd47 2a 20 74 61 62 6c 65 20 69 73 20 64 65 6c 65 74  * table is delet
1dd48 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
1dd49 20 77 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   when the cursor
1dd4a 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a   is closed..**.*
1dd4b 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62  * P2 is the numb
1dd4c 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
1dd4d 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1dd4e 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  le..** The curso
1dd4f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 42 54  r points to a BT
1dd50 72 65 65 20 74 61 62 6c 65 20 69 66 20 50 34 3d  ree table if P4=
1dd51 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 54 72 65  =0 and to a BTre
1dd52 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50 34  e index.** if P4
1dd53 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 66 20 50   is not 0.  If P
1dd54 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69  4 is not NULL, i
1dd55 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65  t points to a Ke
1dd56 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a  yInfo structure.
1dd57 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  ** that defines 
1dd58 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65  the format of ke
1dd59 79 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  ys in the index.
1dd5a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1dd5b 64 65 20 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c  de was once call
1dd5c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75  ed OpenTemp.  Bu
1dd5d 74 20 74 68 61 74 20 63 72 65 61 74 65 64 0a 2a  t that created.*
1dd5e 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61  * confusion beca
1dd5f 75 73 65 20 74 68 65 20 74 65 72 6d 20 22 74 65  use the term "te
1dd60 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 67 68 74  mp table", might
1dd61 20 72 65 66 65 72 20 65 69 74 68 65 72 0a 2a 2a   refer either.**
1dd62 20 74 6f 20 61 20 54 45 4d 50 20 74 61 62 6c 65   to a TEMP table
1dd63 20 61 74 20 74 68 65 20 53 51 4c 20 6c 65 76 65   at the SQL leve
1dd64 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 62 6c 65  l, or to a table
1dd65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68   opened by.** th
1dd66 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e  is opcode.  Then
1dd67 20 74 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73   this opcode was
1dd68 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61   call OpenVirtua
1dd69 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20  l.  But.** that 
1dd6a 63 72 65 61 74 65 64 20 63 6f 6e 66 75 73 69 6f  created confusio
1dd6b 6e 20 77 69 74 68 20 74 68 65 20 77 68 6f 6c 65  n with the whole
1dd6c 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69   virtual-table i
1dd6d 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  dea..*/.case OP_
1dd6e 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b  OpenEphemeral: {
1dd6f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1dd70 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1dd71 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20   into u.ax */.  
1dd72 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b  VdbeCursor *pCx;
1dd73 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1dd74 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1dd75 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20   into u.ax */.  
1dd76 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74  static const int
1dd77 20 6f 70 65 6e 46 6c 61 67 73 20 3d 0a 20 20 20   openFlags =.   
1dd78 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52     SQLITE_OPEN_R
1dd79 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20  EADWRITE |.     
1dd7a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
1dd7b 41 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  ATE |.      SQLI
1dd7c 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
1dd7d 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45  E |.      SQLITE
1dd7e 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
1dd7f 4f 53 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  OSE |.      SQLI
1dd80 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e  TE_OPEN_TRANSIEN
1dd81 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28  T_DB;..  assert(
1dd82 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20   pOp->p1>=0 );. 
1dd83 20 75 2e 61 78 2e 70 43 78 20 3d 20 61 6c 6c 6f   u.ax.pCx = allo
1dd84 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f  cateCursor(p, pO
1dd85 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20  p->p1, pOp->p2, 
1dd86 2d 31 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e  -1, 1);.  if( u.
1dd87 61 78 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f  ax.pCx==0 ) goto
1dd88 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 78 2e   no_mem;.  u.ax.
1dd89 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31  pCx->nullRow = 1
1dd8a 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1dd8b 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c  BtreeFactory(db,
1dd8c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45   0, 1, SQLITE_DE
1dd8d 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45  FAULT_TEMP_CACHE
1dd8e 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73  _SIZE, openFlags
1dd8f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1dd90 20 20 20 20 20 20 20 20 20 20 20 20 20 26 75 2e               &u.
1dd91 61 78 2e 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20  ax.pCx->pBt);.  
1dd92 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1dd93 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  K ){.    rc = sq
1dd94 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
1dd95 72 61 6e 73 28 75 2e 61 78 2e 70 43 78 2d 3e 70  rans(u.ax.pCx->p
1dd96 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66  Bt, 1);.  }.  if
1dd97 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1dd98 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74  ){.    /* If a t
1dd99 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 69  ransient index i
1dd9a 73 20 72 65 71 75 69 72 65 64 2c 20 63 72 65 61  s required, crea
1dd9b 74 65 20 69 74 20 62 79 20 63 61 6c 6c 69 6e 67  te it by calling
1dd9c 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42  .    ** sqlite3B
1dd9d 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28  treeCreateTable(
1dd9e 29 20 77 69 74 68 20 74 68 65 20 42 54 52 45 45  ) with the BTREE
1dd9f 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61 67 20 62  _ZERODATA flag b
1dda0 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65  efore.    ** ope
1dda1 6e 69 6e 67 20 69 74 2e 20 49 66 20 61 20 74 72  ning it. If a tr
1dda2 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 69 73  ansient table is
1dda3 20 72 65 71 75 69 72 65 64 2c 20 6a 75 73 74 20   required, just 
1dda4 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 61  use the.    ** a
1dda5 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65  utomatically cre
1dda6 61 74 65 64 20 74 61 62 6c 65 20 77 69 74 68 20  ated table with 
1dda7 72 6f 6f 74 2d 70 61 67 65 20 31 20 28 61 6e 20  root-page 1 (an 
1dda8 49 4e 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20  INTKEY table).. 
1dda9 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f     */.    if( pO
1ddaa 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29  p->p4.pKeyInfo )
1ddab 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f  {.      int pgno
1ddac 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ddad 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1ddae 4b 45 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20  KEYINFO );.     
1ddaf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1ddb0 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e  eeCreateTable(u.
1ddb1 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 26 70 67  ax.pCx->pBt, &pg
1ddb2 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 41  no, BTREE_ZERODA
1ddb3 54 41 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  TA);.      if( r
1ddb4 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1ddb5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1ddb6 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f  pgno==MASTER_ROO
1ddb7 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 20 72  T+1 );.        r
1ddb8 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1ddb9 43 75 72 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d  Cursor(u.ax.pCx-
1ddba 3e 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 0a 20  >pBt, pgno, 1,. 
1ddbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ddbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1ddbd 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34  KeyInfo*)pOp->p4
1ddbe 2e 7a 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43  .z, u.ax.pCx->pC
1ddbf 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20  ursor);.        
1ddc0 75 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e  u.ax.pCx->pKeyIn
1ddc1 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65  fo = pOp->p4.pKe
1ddc2 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 75  yInfo;.        u
1ddc3 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66  .ax.pCx->pKeyInf
1ddc4 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e  o->enc = ENC(p->
1ddc5 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  db);.      }.   
1ddc6 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54     u.ax.pCx->isT
1ddc7 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65  able = 0;.    }e
1ddc8 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1ddc9 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1ddca 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74  or(u.ax.pCx->pBt
1ddcb 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31  , MASTER_ROOT, 1
1ddcc 2c 20 30 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70  , 0, u.ax.pCx->p
1ddcd 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 75  Cursor);.      u
1ddce 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65  .ax.pCx->isTable
1ddcf 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
1ddd0 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 49 6e    u.ax.pCx->isIn
1ddd1 64 65 78 20 3d 20 21 75 2e 61 78 2e 70 43 78 2d  dex = !u.ax.pCx-
1ddd2 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61  >isTable;.  brea
1ddd3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1ddd4 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50   OpenPseudo P1 P
1ddd5 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f  2 P3 * *.**.** O
1ddd6 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72  pen a new cursor
1ddd7 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20   that points to 
1ddd8 61 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61  a fake table tha
1ddd9 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  t contains a sin
1ddda 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61  gle.** row of da
1dddb 74 61 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74  ta.  The content
1dddc 20 6f 66 20 74 68 61 74 20 6f 6e 65 20 72 6f 77   of that one row
1dddd 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   in the content 
1ddde 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67  of memory.** reg
1dddf 69 73 74 65 72 20 50 32 2e 20 20 49 6e 20 6f 74  ister P2.  In ot
1dde0 68 65 72 20 77 6f 72 64 73 2c 20 63 75 72 73 6f  her words, curso
1dde1 72 20 50 31 20 62 65 63 6f 6d 65 73 20 61 6e 20  r P1 becomes an 
1dde2 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 0a 2a  alias for the .*
1dde3 2a 20 4d 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65  * MEM_Blob conte
1dde4 6e 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  nt contained in 
1dde5 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
1dde6 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c  ** A pseudo-tabl
1dde7 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69  e created by thi
1dde8 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64  s opcode is used
1dde9 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73   to hold the a s
1ddea 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74  ingle.** row out
1ddeb 70 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72  put from the sor
1ddec 74 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20  ter so that the 
1dded 72 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d  row can be decom
1ddee 70 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e  posed into.** in
1ddef 64 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73  dividual columns
1ddf0 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f   using the OP_Co
1ddf1 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68  lumn opcode.  Th
1ddf2 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f  e OP_Column opco
1ddf3 64 65 0a 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c  de.** is the onl
1ddf4 79 20 63 75 72 73 6f 72 20 6f 70 63 6f 64 65 20  y cursor opcode 
1ddf5 74 68 61 74 20 77 6f 72 6b 73 20 77 69 74 68 20  that works with 
1ddf6 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a  a pseudo-table..
1ddf7 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 20  **.** P3 is the 
1ddf8 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73  number of fields
1ddf9 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20   in the records 
1ddfa 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f  that will be sto
1ddfb 72 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 73  red by.** the ps
1ddfc 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63  eudo-table..*/.c
1ddfd 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64  ase OP_OpenPseud
1ddfe 6f 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  o: {.#if 0  /* l
1ddff 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1de00 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a  oved into u.ay *
1de01 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1de02 70 43 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  pCx;.#endif /* l
1de03 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1de04 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a  oved into u.ay *
1de05 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
1de06 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61  ->p1>=0 );.  u.a
1de07 79 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65  y.pCx = allocate
1de08 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70  Cursor(p, pOp->p
1de09 31 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20  1, pOp->p3, -1, 
1de0a 30 29 3b 0a 20 20 69 66 28 20 75 2e 61 79 2e 70  0);.  if( u.ay.p
1de0b 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  Cx==0 ) goto no_
1de0c 6d 65 6d 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d  mem;.  u.ay.pCx-
1de0d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20  >nullRow = 1;.  
1de0e 75 2e 61 79 2e 70 43 78 2d 3e 70 73 65 75 64 6f  u.ay.pCx->pseudo
1de0f 54 61 62 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e  TableReg = pOp->
1de10 70 32 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e  p2;.  u.ay.pCx->
1de11 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75  isTable = 1;.  u
1de12 2e 61 79 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78  .ay.pCx->isIndex
1de13 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1de14 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f  ../* Opcode: Clo
1de15 73 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  se P1 * * * *.**
1de16 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73  .** Close a curs
1de17 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70  or previously op
1de18 65 6e 65 64 20 61 73 20 50 31 2e 20 20 49 66 20  ened as P1.  If 
1de19 50 31 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72  P1 is not.** cur
1de1a 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69  rently open, thi
1de1b 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73  s instruction is
1de1c 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73   a no-op..*/.cas
1de1d 65 20 4f 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20  e OP_Close: {.  
1de1e 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1de1f 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
1de20 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71  >nCursor );.  sq
1de21 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72  lite3VdbeFreeCur
1de22 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b  sor(p, p->apCsr[
1de23 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 20 70 2d 3e  pOp->p1]);.  p->
1de24 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 20 3d  apCsr[pOp->p1] =
1de25 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   0;.  break;.}..
1de26 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47  /* Opcode: SeekG
1de27 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  e P1 P2 P3 P4 *.
1de28 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20  **.** If cursor 
1de29 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20  P1 refers to an 
1de2a 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65  SQL table (B-Tre
1de2b 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65  e that uses inte
1de2c 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75  ger keys), .** u
1de2d 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  se the value in 
1de2e 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 74  register P3 as t
1de2f 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75 72 73  he key.  If curs
1de30 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a  or P1 refers .**
1de31 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78   to an SQL index
1de32 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65  , then P3 is the
1de33 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72   first in an arr
1de34 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65  ay of P4 registe
1de35 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  rs .** that are 
1de36 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63  used as an unpac
1de37 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a  ked index key. .
1de38 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e  **.** Reposition
1de39 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
1de3a 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  at  it points to
1de3b 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e   the smallest en
1de3c 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20  try that .** is 
1de3d 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1de3e 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79  equal to the key
1de3f 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65   value. If there
1de40 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
1de41 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
1de42 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
1de43 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e   key and P2 is n
1de44 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75  ot zero, then ju
1de45 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  mp to P2..**.** 
1de46 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c  See also: Found,
1de47 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69   NotFound, Disti
1de48 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65  nct, SeekLt, See
1de49 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f  kGt, SeekLe.*/./
1de4a 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 74  * Opcode: SeekGt
1de4b 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1de4c 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  *.** If cursor P
1de4d 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
1de4e 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
1de4f 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
1de50 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73  er keys), .** us
1de51 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1de52 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20  egister P3 as a 
1de53 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50  key. If cursor P
1de54 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20  1 refers .** to 
1de55 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68  an SQL index, th
1de56 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72  en P3 is the fir
1de57 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f  st in an array o
1de58 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a  f P4 registers .
1de59 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64  ** that are used
1de5a 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20   as an unpacked 
1de5b 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a  index key. .**.*
1de5c 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72  * Reposition cur
1de5d 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20  sor P1 so that  
1de5e 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
1de5f 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20   smallest entry 
1de60 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61  that .** is grea
1de61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b 65 79  ter than the key
1de62 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65   value. If there
1de63 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
1de64 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a  greater than .**
1de65 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
1de66 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
1de67 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1de68 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1de69 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44  und, NotFound, D
1de6a 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c  istinct, SeekLt,
1de6b 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a   SeekGe, SeekLe.
1de6c 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  */./* Opcode: Se
1de6d 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20 50 34  ekLt P1 P2 P3 P4
1de6e 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72   * .**.** If cur
1de6f 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f  sor P1 refers to
1de70 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42   an SQL table (B
1de71 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20  -Tree that uses 
1de72 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a  integer keys), .
1de73 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65  ** use the value
1de74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20   in register P3 
1de75 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72  as a key. If cur
1de76 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a  sor P1 refers .*
1de77 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65  * to an SQL inde
1de78 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68  x, then P3 is th
1de79 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72  e first in an ar
1de7a 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74  ray of P4 regist
1de7b 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65  ers .** that are
1de7c 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61   used as an unpa
1de7d 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20  cked index key. 
1de7e 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f  .**.** Repositio
1de7f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  n cursor P1 so t
1de80 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74  hat  it points t
1de81 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e  o the largest en
1de82 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20  try that .** is 
1de83 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65  less than the ke
1de84 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72  y value. If ther
1de85 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73  e are no records
1de86 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74   less than .** t
1de87 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73  he key and P2 is
1de88 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20   not zero, then 
1de89 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
1de8a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
1de8b 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73  d, NotFound, Dis
1de8c 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53  tinct, SeekGt, S
1de8d 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f  eekGe, SeekLe.*/
1de8e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b  ./* Opcode: Seek
1de8f 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  Le P1 P2 P3 P4 *
1de90 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  .**.** If cursor
1de91 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e   P1 refers to an
1de92 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72   SQL table (B-Tr
1de93 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74  ee that uses int
1de94 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20  eger keys), .** 
1de95 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  use the value in
1de96 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
1de97 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
1de98 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
1de99 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
1de9a 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
1de9b 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
1de9c 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1de9d 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
1de9e 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
1de9f 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
1dea0 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
1dea1 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1dea2 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
1dea3 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20  e largest entry 
1dea4 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73  that .** is less
1dea5 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1dea6 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e  o the key value.
1dea7 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1dea8 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73   records .** les
1dea9 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
1deaa 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50  to the key and P
1deab 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74  2 is not zero, t
1deac 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a  hen jump to P2..
1dead 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1deae 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
1deaf 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47   Distinct, SeekG
1deb0 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c  t, SeekGe, SeekL
1deb1 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  t.*/.case OP_See
1deb2 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f 2a 20  kLt:         /* 
1deb3 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1deb4 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20  e OP_SeekLe:    
1deb5 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1deb6 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  3 */.case OP_See
1deb7 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20  kGe:         /* 
1deb8 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1deb9 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20  e OP_SeekGt: {  
1deba 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1debb 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  3 */.#if 0  /* l
1debc 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1debd 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a  oved into u.az *
1debe 2f 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69  /.  int res;.  i
1debf 6e 74 20 6f 63 3b 0a 20 20 56 64 62 65 43 75 72  nt oc;.  VdbeCur
1dec0 73 6f 72 20 2a 70 43 3b 0a 20 20 55 6e 70 61 63  sor *pC;.  Unpac
1dec1 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 69  kedRecord r;.  i
1dec2 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69 36 34  nt nField;.  i64
1dec3 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54   iKey;      /* T
1dec4 68 65 20 72 6f 77 69 64 20 77 65 20 61 72 65 20  he rowid we are 
1dec5 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a 23 65  to seek to */.#e
1dec6 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1dec7 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1dec8 74 6f 20 75 2e 61 7a 20 2a 2f 0a 0a 20 20 61 73  to u.az */..  as
1dec9 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1deca 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1decb 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
1decc 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29  rt( pOp->p2!=0 )
1decd 3b 0a 20 20 75 2e 61 7a 2e 70 43 20 3d 20 70 2d  ;.  u.az.pC = p-
1dece 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1decf 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e  .  assert( u.az.
1ded0 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pC!=0 );.  asser
1ded1 74 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 73 65 75  t( u.az.pC->pseu
1ded2 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b  doTableReg==0 );
1ded3 0a 20 20 61 73 73 65 72 74 28 20 4f 50 5f 53 65  .  assert( OP_Se
1ded4 65 6b 4c 65 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c  ekLe == OP_SeekL
1ded5 74 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28  t+1 );.  assert(
1ded6 20 4f 50 5f 53 65 65 6b 47 65 20 3d 3d 20 4f 50   OP_SeekGe == OP
1ded7 5f 53 65 65 6b 4c 74 2b 32 20 29 3b 0a 20 20 61  _SeekLt+2 );.  a
1ded8 73 73 65 72 74 28 20 4f 50 5f 53 65 65 6b 47 74  ssert( OP_SeekGt
1ded9 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c 74 2b 33 20   == OP_SeekLt+3 
1deda 29 3b 0a 20 20 69 66 28 20 75 2e 61 7a 2e 70 43  );.  if( u.az.pC
1dedb 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a  ->pCursor!=0 ){.
1dedc 20 20 20 20 75 2e 61 7a 2e 6f 63 20 3d 20 70 4f      u.az.oc = pO
1dedd 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 75  p->opcode;.    u
1dede 2e 61 7a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .az.pC->nullRow 
1dedf 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e 61  = 0;.    if( u.a
1dee0 7a 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b  z.pC->isTable ){
1dee1 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e  .      /* The in
1dee2 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20  put value in P3 
1dee3 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20  might be of any 
1dee4 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72  type: integer, r
1dee5 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20  eal, string,.   
1dee6 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e     ** blob, or N
1dee7 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65  ULL.  But it nee
1dee8 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65  ds to be an inte
1dee9 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 61  ger before we ca
1deea 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  n do.      ** th
1deeb 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72  e seek, so cover
1deec 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70  t it. */.      p
1deed 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In3 = &aMem[pOp-
1deee 3e 70 33 5d 3b 0a 20 20 20 20 20 20 61 70 70 6c  >p3];.      appl
1deef 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  yNumericAffinity
1def0 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e  (pIn3);.      u.
1def1 61 7a 2e 69 4b 65 79 20 3d 20 73 71 6c 69 74 65  az.iKey = sqlite
1def2 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
1def3 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e  n3);.      u.az.
1def4 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1def5 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   = 0;..      /* 
1def6 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20  If the P3 value 
1def7 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e  could not be con
1def8 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69  verted into an i
1def9 6e 74 65 67 65 72 20 77 69 74 68 6f 75 74 0a 20  nteger without. 
1defa 20 20 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20       ** loss of 
1defb 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65  information, the
1defc 6e 20 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73  n special proces
1defd 73 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64  sing is required
1defe 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ... */.      if(
1deff 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20   (pIn3->flags & 
1df00 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20  MEM_Int)==0 ){. 
1df01 20 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33         if( (pIn3
1df02 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ->flags & MEM_Re
1df03 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  al)==0 ){.      
1df04 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33      /* If the P3
1df05 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65   value cannot be
1df06 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
1df07 61 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75  any kind of a nu
1df08 6d 62 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  mber,.          
1df09 2a 2a 20 74 68 65 6e 20 74 68 65 20 73 65 65 6b  ** then the seek
1df0a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1df0b 2c 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20  , so jump to P2 
1df0c 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 63 20  */.          pc 
1df0d 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1df0e 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
1df0f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1df10 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68    /* If we reach
1df11 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
1df12 6e 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 6d  n the P3 value m
1df13 75 73 74 20 62 65 20 61 20 66 6c 6f 61 74 69 6e  ust be a floatin
1df14 67 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69  g.        ** poi
1df15 6e 74 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20  nt number. */.  
1df16 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
1df17 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In3->flags & MEM
1df18 5f 52 65 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20  _Real)!=0 );..  
1df19 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 69        if( u.az.i
1df1a 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e  Key==SMALLEST_IN
1df1b 54 36 34 20 26 26 20 28 70 49 6e 33 2d 3e 72 3c  T64 && (pIn3->r<
1df1c 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65  (double)u.az.iKe
1df1d 79 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20  y || pIn3->r>0) 
1df1e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
1df1f 54 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20  The P3 value is 
1df20 74 6f 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67  too large in mag
1df21 6e 69 74 75 64 65 20 74 6f 20 62 65 20 65 78 70  nitude to be exp
1df22 72 65 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20  ressed as an.   
1df23 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65         ** intege
1df24 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  r. */.          
1df25 75 2e 61 7a 2e 72 65 73 20 3d 20 31 3b 0a 20 20  u.az.res = 1;.  
1df26 20 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33          if( pIn3
1df27 2d 3e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ->r<0 ){.       
1df28 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63       if( u.az.oc
1df29 3e 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 20 20  >=OP_SeekGe ){  
1df2a 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d  assert( u.az.oc=
1df2b 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e  =OP_SeekGe || u.
1df2c 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74  az.oc==OP_SeekGt
1df2d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
1df2e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1df2f 72 65 65 46 69 72 73 74 28 75 2e 61 7a 2e 70 43  reeFirst(u.az.pC
1df30 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a  ->pCursor, &u.az
1df31 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20  .res);.         
1df32 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1df33 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62  ITE_OK ) goto ab
1df34 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1df35 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
1df36 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
1df37 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
1df38 20 75 2e 61 7a 2e 6f 63 3c 3d 4f 50 5f 53 65 65   u.az.oc<=OP_See
1df39 6b 4c 65 20 29 7b 20 20 61 73 73 65 72 74 28 20  kLe ){  assert( 
1df3a 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1df3b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  Lt || u.az.oc==O
1df3c 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20  P_SeekLe );.    
1df3d 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
1df3e 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28  qlite3BtreeLast(
1df3f 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.az.pC->pCursor
1df40 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20  , &u.az.res);.  
1df41 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1df42 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
1df43 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1df44 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1df45 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1df46 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
1df47 20 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20   u.az.res ){.   
1df48 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f           pc = pO
1df49 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
1df4a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1df4b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1df4c 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 7a 2e 6f  }else if( u.az.o
1df4d 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20  c==OP_SeekLt || 
1df4e 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1df4f 47 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Ge ){.          
1df50 2f 2a 20 55 73 65 20 74 68 65 20 63 65 69 6c 69  /* Use the ceili
1df51 6e 67 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ng() function to
1df52 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69   convert real->i
1df53 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  nt */.          
1df54 69 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64  if( pIn3->r > (d
1df55 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20  ouble)u.az.iKey 
1df56 29 20 75 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20  ) u.az.iKey++;. 
1df57 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1df58 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74          /* Use t
1df59 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74  he floor() funct
1df5a 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72  ion to convert r
1df5b 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20  eal->int */.    
1df5c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1df5d 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65  az.oc==OP_SeekLe
1df5e 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f   || u.az.oc==OP_
1df5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20  SeekGt );.      
1df60 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20      if( pIn3->r 
1df61 3c 20 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69  < (double)u.az.i
1df62 4b 65 79 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2d  Key ) u.az.iKey-
1df63 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  -;.        }.   
1df64 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
1df65 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1df66 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e  toUnpacked(u.az.
1df67 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  pC->pCursor, 0, 
1df68 28 75 36 34 29 75 2e 61 7a 2e 69 4b 65 79 2c 20  (u64)u.az.iKey, 
1df69 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20  0, &u.az.res);. 
1df6a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1df6b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1df6c 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1df6d 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
1df6e 20 7d 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61   }.      if( u.a
1df6f 7a 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20  z.res==0 ){.    
1df70 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77      u.az.pC->row
1df71 69 64 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20  idIsValid = 1;. 
1df72 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e         u.az.pC->
1df73 6c 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 61 7a  lastRowid = u.az
1df74 2e 69 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20  .iKey;.      }. 
1df75 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1df76 75 2e 61 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 4f  u.az.nField = pO
1df77 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20 61  p->p4.i;.      a
1df78 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1df79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a  pe==P4_INT32 );.
1df7a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1df7b 61 7a 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0a 20  az.nField>0 );. 
1df7c 20 20 20 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 79       u.az.r.pKey
1df7d 49 6e 66 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e  Info = u.az.pC->
1df7e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pKeyInfo;.      
1df7f 75 2e 61 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d 20  u.az.r.nField = 
1df80 28 75 31 36 29 75 2e 61 7a 2e 6e 46 69 65 6c 64  (u16)u.az.nField
1df81 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;..      /* The 
1df82 6e 65 78 74 20 6c 69 6e 65 20 6f 66 20 63 6f 64  next line of cod
1df83 65 20 63 6f 6d 70 75 74 65 73 20 61 73 20 66 6f  e computes as fo
1df84 6c 6c 6f 77 73 2c 20 6f 6e 6c 79 20 66 61 73 74  llows, only fast
1df85 65 72 3a 0a 20 20 20 20 20 20 2a 2a 20 20 20 69  er:.      **   i
1df86 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  f( u.az.oc==OP_S
1df87 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63  eekGt || u.az.oc
1df88 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20  ==OP_SeekLe ){. 
1df89 20 20 20 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a       **     u.az
1df8a 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43  .r.flags = UNPAC
1df8b 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20  KED_INCRKEY;.   
1df8c 20 20 20 2a 2a 20 20 20 7d 65 6c 73 65 7b 0a 20     **   }else{. 
1df8d 20 20 20 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a       **     u.az
1df8e 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20  .r.flags = 0;.  
1df8f 20 20 20 20 2a 2a 20 20 20 7d 0a 20 20 20 20 20      **   }.     
1df90 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72   */.      u.az.r
1df91 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45  .flags = UNPACKE
1df92 44 5f 49 4e 43 52 4b 45 59 20 2a 20 28 31 20 26  D_INCRKEY * (1 &
1df93 20 28 75 2e 61 7a 2e 6f 63 20 2d 20 4f 50 5f 53   (u.az.oc - OP_S
1df94 65 65 6b 4c 74 29 29 3b 0a 20 20 20 20 20 20 61  eekLt));.      a
1df95 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 21 3d  ssert( u.az.oc!=
1df96 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e 61  OP_SeekGt || u.a
1df97 7a 2e 72 2e 66 6c 61 67 73 3d 3d 55 4e 50 41 43  z.r.flags==UNPAC
1df98 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 3b 0a 20  KED_INCRKEY );. 
1df99 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61       assert( u.a
1df9a 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65 6b 4c 65 20  z.oc!=OP_SeekLe 
1df9b 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 3d  || u.az.r.flags=
1df9c 3d 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45  =UNPACKED_INCRKE
1df9d 59 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Y );.      asser
1df9e 74 28 20 75 2e 61 7a 2e 6f 63 21 3d 4f 50 5f 53  t( u.az.oc!=OP_S
1df9f 65 65 6b 47 65 20 7c 7c 20 75 2e 61 7a 2e 72 2e  eekGe || u.az.r.
1dfa0 66 6c 61 67 73 3d 3d 30 20 29 3b 0a 20 20 20 20  flags==0 );.    
1dfa1 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f    assert( u.az.o
1dfa2 63 21 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20  c!=OP_SeekLt || 
1dfa3 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 3d 3d 30 20  u.az.r.flags==0 
1dfa4 29 3b 0a 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72  );..      u.az.r
1dfa5 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f  .aMem = &aMem[pO
1dfa6 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 45 78  p->p3];.      Ex
1dfa7 70 61 6e 64 42 6c 6f 62 28 75 2e 61 7a 2e 72 2e  pandBlob(u.az.r.
1dfa8 61 4d 65 6d 29 3b 0a 20 20 20 20 20 20 72 63 20  aMem);.      rc 
1dfa9 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
1dfaa 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61  vetoUnpacked(u.a
1dfab 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26  z.pC->pCursor, &
1dfac 75 2e 61 7a 2e 72 2c 20 30 2c 20 30 2c 20 26 75  u.az.r, 0, 0, &u
1dfad 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20  .az.res);.      
1dfae 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1dfaf 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
1dfb0 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1dfb1 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rror;.      }.  
1dfb2 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77      u.az.pC->row
1dfb3 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20  idIsValid = 0;. 
1dfb4 20 20 20 7d 0a 20 20 20 20 75 2e 61 7a 2e 70 43     }.    u.az.pC
1dfb5 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1dfb6 20 3d 20 30 3b 0a 20 20 20 20 75 2e 61 7a 2e 70   = 0;.    u.az.p
1dfb7 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1dfb8 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 23 69   CACHE_STALE;.#i
1dfb9 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1dfba 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61  .    sqlite3_sea
1dfbb 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e  rch_count++;.#en
1dfbc 64 69 66 0a 20 20 20 20 69 66 28 20 75 2e 61 7a  dif.    if( u.az
1dfbd 2e 6f 63 3e 3d 4f 50 5f 53 65 65 6b 47 65 20 29  .oc>=OP_SeekGe )
1dfbe 7b 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e  {  assert( u.az.
1dfbf 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c  oc==OP_SeekGe ||
1dfc0 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.az.oc==OP_See
1dfc1 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28  kGt );.      if(
1dfc2 20 75 2e 61 7a 2e 72 65 73 3c 30 20 7c 7c 20 28   u.az.res<0 || (
1dfc3 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75  u.az.res==0 && u
1dfc4 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47  .az.oc==OP_SeekG
1dfc5 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  t) ){.        rc
1dfc6 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e   = sqlite3BtreeN
1dfc7 65 78 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75  ext(u.az.pC->pCu
1dfc8 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29  rsor, &u.az.res)
1dfc9 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
1dfca 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
1dfcb 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1dfcc 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 75  error;.        u
1dfcd 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .az.pC->rowidIsV
1dfce 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  alid = 0;.      
1dfcf 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75  }else{.        u
1dfd0 2e 61 7a 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20  .az.res = 0;.   
1dfd1 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
1dfd2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1dfd3 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74  az.oc==OP_SeekLt
1dfd4 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f   || u.az.oc==OP_
1dfd5 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 20 20  SeekLe );.      
1dfd6 69 66 28 20 75 2e 61 7a 2e 72 65 73 3e 30 20 7c  if( u.az.res>0 |
1dfd7 7c 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26  | (u.az.res==0 &
1dfd8 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65  & u.az.oc==OP_Se
1dfd9 65 6b 4c 74 29 20 29 7b 0a 20 20 20 20 20 20 20  ekLt) ){.       
1dfda 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1dfdb 65 65 50 72 65 76 69 6f 75 73 28 75 2e 61 7a 2e  eePrevious(u.az.
1dfdc 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e  pC->pCursor, &u.
1dfdd 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20  az.res);.       
1dfde 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1dfdf 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  OK ) goto abort_
1dfe0 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1dfe1 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72        u.az.pC->r
1dfe2 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
1dfe3 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1dfe4 20 20 20 20 20 20 2f 2a 20 75 2e 61 7a 2e 72 65        /* u.az.re
1dfe5 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67 61 74  s might be negat
1dfe6 69 76 65 20 62 65 63 61 75 73 65 20 74 68 65 20  ive because the 
1dfe7 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20  table is empty. 
1dfe8 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 20 20   Check to.      
1dfe9 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68 69 73    ** see if this
1dfea 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20   is the case..  
1dfeb 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1dfec 20 75 2e 61 7a 2e 72 65 73 20 3d 20 73 71 6c 69   u.az.res = sqli
1dfed 74 65 33 42 74 72 65 65 45 6f 66 28 75 2e 61 7a  te3BtreeEof(u.az
1dfee 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20  .pC->pCursor);. 
1dfef 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1dff0 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1dff1 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 75 2e  >0 );.    if( u.
1dff2 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20  az.res ){.      
1dff3 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1dff4 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1dff5 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70  .    /* This hap
1dff6 70 65 6e 73 20 77 68 65 6e 20 61 74 74 65 6d 70  pens when attemp
1dff7 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65  ting to open the
1dff8 20 73 71 6c 69 74 65 33 5f 6d 61 73 74 65 72 20   sqlite3_master 
1dff9 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72  table.    ** for
1dffa 20 72 65 61 64 20 61 63 63 65 73 73 20 72 65 74   read access ret
1dffb 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54  urns SQLITE_EMPT
1dffc 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  Y. In this case 
1dffd 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 74 61  always.    ** ta
1dffe 6b 65 20 74 68 65 20 6a 75 6d 70 20 28 73 69 6e  ke the jump (sin
1dfff 63 65 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  ce there are no 
1e000 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74  records in the t
1e001 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  able)..    */.  
1e002 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1e003 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1e004 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1e005 65 65 6b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  eek P1 P2 * * *.
1e006 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f  **.** P1 is an o
1e007 70 65 6e 20 74 61 62 6c 65 20 63 75 72 73 6f 72  pen table cursor
1e008 20 61 6e 64 20 50 32 20 69 73 20 61 20 72 6f 77   and P2 is a row
1e009 69 64 20 69 6e 74 65 67 65 72 2e 20 20 41 72 72  id integer.  Arr
1e00a 61 6e 67 65 0a 2a 2a 20 66 6f 72 20 50 31 20 74  ange.** for P1 t
1e00b 6f 20 6d 6f 76 65 20 73 6f 20 74 68 61 74 20 69  o move so that i
1e00c 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
1e00d 72 6f 77 69 64 20 67 69 76 65 6e 20 62 79 20 50  rowid given by P
1e00e 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  2..**.** This is
1e00f 20 61 63 74 75 61 6c 6c 79 20 61 20 64 65 66 65   actually a defe
1e010 72 72 65 64 20 73 65 65 6b 2e 20 20 4e 6f 74 68  rred seek.  Noth
1e011 69 6e 67 20 61 63 74 75 61 6c 6c 79 20 68 61 70  ing actually hap
1e012 70 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a 20 74 68  pens until.** th
1e013 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64  e cursor is used
1e014 20 74 6f 20 72 65 61 64 20 61 20 72 65 63 6f 72   to read a recor
1e015 64 2e 20 20 54 68 61 74 20 77 61 79 2c 20 69 66  d.  That way, if
1e016 20 6e 6f 20 72 65 61 64 73 0a 2a 2a 20 6f 63 63   no reads.** occ
1e017 75 72 2c 20 6e 6f 20 75 6e 6e 65 63 65 73 73 61  ur, no unnecessa
1e018 72 79 20 49 2f 4f 20 68 61 70 70 65 6e 73 2e 0a  ry I/O happens..
1e019 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 3a  */.case OP_Seek:
1e01a 20 7b 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a   {    /* in2 */.
1e01b 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1e01c 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e01d 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 20 20 56  into u.ba */.  V
1e01e 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23  dbeCursor *pC;.#
1e01f 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1e020 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e021 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 0a 20 20 61  nto u.ba */..  a
1e022 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1e023 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
1e024 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62  nCursor );.  u.b
1e025 61 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  a.pC = p->apCsr[
1e026 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1e027 72 74 28 20 75 2e 62 61 2e 70 43 21 3d 30 20 29  rt( u.ba.pC!=0 )
1e028 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75  ;.  if( ALWAYS(u
1e029 2e 62 61 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21  .ba.pC->pCursor!
1e02a 3d 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72  =0) ){.    asser
1e02b 74 28 20 75 2e 62 61 2e 70 43 2d 3e 69 73 54 61  t( u.ba.pC->isTa
1e02c 62 6c 65 20 29 3b 0a 20 20 20 20 75 2e 62 61 2e  ble );.    u.ba.
1e02d 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b  pC->nullRow = 0;
1e02e 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 61 4d 65  .    pIn2 = &aMe
1e02f 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1e030 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54  u.ba.pC->movetoT
1e031 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56  arget = sqlite3V
1e032 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32  dbeIntValue(pIn2
1e033 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e  );.    u.ba.pC->
1e034 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1e035 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 64  ;.    u.ba.pC->d
1e036 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1e037 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1e038 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  }.  ../* Opcode:
1e039 20 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 20   Found P1 P2 P3 
1e03a 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34  P4 *.**.** If P4
1e03b 3d 3d 30 20 74 68 65 6e 20 72 65 67 69 73 74 65  ==0 then registe
1e03c 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f  r P3 holds a blo
1e03d 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79  b constructed by
1e03e 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 49 66   MakeRecord.  If
1e03f 0a 2a 2a 20 50 34 3e 30 20 74 68 65 6e 20 72 65  .** P4>0 then re
1e040 67 69 73 74 65 72 20 50 33 20 69 73 20 74 68 65  gister P3 is the
1e041 20 66 69 72 73 74 20 6f 66 20 50 34 20 72 65 67   first of P4 reg
1e042 69 73 74 65 72 73 20 74 68 61 74 20 66 6f 72 6d  isters that form
1e043 20 61 6e 20 75 6e 70 61 63 6b 65 64 0a 2a 2a 20   an unpacked.** 
1e044 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 43 75  record..**.** Cu
1e045 72 73 6f 72 20 50 31 20 69 73 20 6f 6e 20 61 6e  rsor P1 is on an
1e046 20 69 6e 64 65 78 20 62 74 72 65 65 2e 20 20 49   index btree.  I
1e047 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 64 65  f the record ide
1e048 6e 74 69 66 69 65 64 20 62 79 20 50 33 20 61 6e  ntified by P3 an
1e049 64 20 50 34 0a 2a 2a 20 69 73 20 61 20 70 72 65  d P4.** is a pre
1e04a 66 69 78 20 6f 66 20 61 6e 79 20 65 6e 74 72 79  fix of any entry
1e04b 20 69 6e 20 50 31 20 74 68 65 6e 20 61 20 6a 75   in P1 then a ju
1e04c 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50 32  mp is made to P2
1e04d 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 6c 65   and.** P1 is le
1e04e 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74  ft pointing at t
1e04f 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72  he matching entr
1e050 79 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  y..*/./* Opcode:
1e051 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20   NotFound P1 P2 
1e052 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  P3 P4 *.**.** If
1e053 20 50 34 3d 3d 30 20 74 68 65 6e 20 72 65 67 69   P4==0 then regi
1e054 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20  ster P3 holds a 
1e055 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64  blob constructed
1e056 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20   by MakeRecord. 
1e057 20 49 66 0a 2a 2a 20 50 34 3e 30 20 74 68 65 6e   If.** P4>0 then
1e058 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20   register P3 is 
1e059 74 68 65 20 66 69 72 73 74 20 6f 66 20 50 34 20  the first of P4 
1e05a 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20 66  registers that f
1e05b 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 0a  orm an unpacked.
1e05c 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 0a 2a  ** record..** .*
1e05d 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f  * Cursor P1 is o
1e05e 6e 20 61 6e 20 69 6e 64 65 78 20 62 74 72 65 65  n an index btree
1e05f 2e 20 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  .  If the record
1e060 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 50   identified by P
1e061 33 20 61 6e 64 20 50 34 0a 2a 2a 20 69 73 20 6e  3 and P4.** is n
1e062 6f 74 20 74 68 65 20 70 72 65 66 69 78 20 6f 66  ot the prefix of
1e063 20 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31   any entry in P1
1e064 20 74 68 65 6e 20 61 20 6a 75 6d 70 20 69 73 20   then a jump is 
1e065 6d 61 64 65 20 74 6f 20 50 32 2e 20 20 49 66 20  made to P2.  If 
1e066 50 31 20 0a 2a 2a 20 64 6f 65 73 20 63 6f 6e 74  P1 .** does cont
1e067 61 69 6e 20 61 6e 20 65 6e 74 72 79 20 77 68 6f  ain an entry who
1e068 73 65 20 70 72 65 66 69 78 20 6d 61 74 63 68 65  se prefix matche
1e069 73 20 74 68 65 20 50 33 2f 50 34 20 72 65 63 6f  s the P3/P4 reco
1e06a 72 64 20 74 68 65 6e 20 63 6f 6e 74 72 6f 6c 0a  rd then control.
1e06b 2a 2a 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68  ** falls through
1e06c 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
1e06d 74 72 75 63 74 69 6f 6e 20 61 6e 64 20 50 31 20  truction and P1 
1e06e 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
1e06f 20 61 74 20 74 68 65 0a 2a 2a 20 6d 61 74 63 68   at the.** match
1e070 69 6e 67 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a  ing entry..**.**
1e071 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64   See also: Found
1e072 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73 55  , NotExists, IsU
1e073 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50  nique.*/.case OP
1e074 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20 20  _NotFound:      
1e075 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f   /* jump, in3 */
1e076 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a 20  .case OP_Found: 
1e077 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1e078 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  , in3 */.#if 0  
1e079 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e07a 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e07b 62 62 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 72 65  bb */.  int alre
1e07c 61 64 79 45 78 69 73 74 73 3b 0a 20 20 56 64 62  adyExists;.  Vdb
1e07d 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69  eCursor *pC;.  i
1e07e 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b  nt res;.  Unpack
1e07f 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65  edRecord *pIdxKe
1e080 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63  y;.  UnpackedRec
1e081 6f 72 64 20 72 3b 0a 20 20 63 68 61 72 20 61 54  ord r;.  char aT
1e082 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69  empRec[ROUND8(si
1e083 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63  zeof(UnpackedRec
1e084 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d  ord)) + sizeof(M
1e085 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 23 65 6e 64  em)*3 + 7];.#end
1e086 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e087 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e088 20 75 2e 62 62 20 2a 2f 0a 0a 23 69 66 64 65 66   u.bb */..#ifdef
1e089 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73   SQLITE_TEST.  s
1e08a 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75  qlite3_found_cou
1e08b 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  nt++;.#endif..  
1e08c 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73  u.bb.alreadyExis
1e08d 74 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ts = 0;.  assert
1e08e 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e08f 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e090 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  or );.  assert( 
1e091 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1e092 49 4e 54 33 32 20 29 3b 0a 20 20 75 2e 62 62 2e  INT32 );.  u.bb.
1e093 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1e094 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e095 28 20 75 2e 62 62 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bb.pC!=0 );.
1e096 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70    pIn3 = &aMem[p
1e097 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 41  Op->p3];.  if( A
1e098 4c 57 41 59 53 28 75 2e 62 62 2e 70 43 2d 3e 70  LWAYS(u.bb.pC->p
1e099 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 0a 20  Cursor!=0) ){.. 
1e09a 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e     assert( u.bb.
1e09b 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29  pC->isTable==0 )
1e09c 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  ;.    if( pOp->p
1e09d 34 2e 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 75  4.i>0 ){.      u
1e09e 2e 62 62 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d  .bb.r.pKeyInfo =
1e09f 20 75 2e 62 62 2e 70 43 2d 3e 70 4b 65 79 49 6e   u.bb.pC->pKeyIn
1e0a0 66 6f 3b 0a 20 20 20 20 20 20 75 2e 62 62 2e 72  fo;.      u.bb.r
1e0a1 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70  .nField = pOp->p
1e0a2 34 2e 69 3b 0a 20 20 20 20 20 20 75 2e 62 62 2e  4.i;.      u.bb.
1e0a3 72 2e 61 4d 65 6d 20 3d 20 70 49 6e 33 3b 0a 20  r.aMem = pIn3;. 
1e0a4 20 20 20 20 20 75 2e 62 62 2e 72 2e 66 6c 61 67       u.bb.r.flag
1e0a5 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45  s = UNPACKED_PRE
1e0a6 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20 20 20  FIX_MATCH;.     
1e0a7 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 20 3d 20   u.bb.pIdxKey = 
1e0a8 26 75 2e 62 62 2e 72 3b 0a 20 20 20 20 7d 65 6c  &u.bb.r;.    }el
1e0a9 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
1e0aa 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20  ( pIn3->flags & 
1e0ab 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20  MEM_Blob );.    
1e0ac 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e    ExpandBlob(pIn
1e0ad 33 29 3b 0a 20 20 20 20 20 20 75 2e 62 62 2e 70  3);.      u.bb.p
1e0ae 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33  IdxKey = sqlite3
1e0af 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b  VdbeRecordUnpack
1e0b0 28 75 2e 62 62 2e 70 43 2d 3e 70 4b 65 79 49 6e  (u.bb.pC->pKeyIn
1e0b1 66 6f 2c 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e  fo, pIn3->n, pIn
1e0b2 33 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 20  3->z,.          
1e0b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e0b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 2e                u.
1e0b5 62 62 2e 61 54 65 6d 70 52 65 63 2c 20 73 69 7a  bb.aTempRec, siz
1e0b6 65 6f 66 28 75 2e 62 62 2e 61 54 65 6d 70 52 65  eof(u.bb.aTempRe
1e0b7 63 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75  c));.      if( u
1e0b8 2e 62 62 2e 70 49 64 78 4b 65 79 3d 3d 30 20 29  .bb.pIdxKey==0 )
1e0b9 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6e  {.        goto n
1e0ba 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20  o_mem;.      }. 
1e0bb 20 20 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65       u.bb.pIdxKe
1e0bc 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41  y->flags |= UNPA
1e0bd 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43  CKED_PREFIX_MATC
1e0be 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  H;.    }.    rc 
1e0bf 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
1e0c0 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62  vetoUnpacked(u.b
1e0c1 62 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75  b.pC->pCursor, u
1e0c2 2e 62 62 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20  .bb.pIdxKey, 0, 
1e0c3 30 2c 20 26 75 2e 62 62 2e 72 65 73 29 3b 0a 20  0, &u.bb.res);. 
1e0c4 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 69     if( pOp->p4.i
1e0c5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
1e0c6 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e  ite3VdbeDeleteUn
1e0c7 70 61 63 6b 65 64 52 65 63 6f 72 64 28 75 2e 62  packedRecord(u.b
1e0c8 62 2e 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20  b.pIdxKey);.    
1e0c9 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  }.    if( rc!=SQ
1e0ca 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1e0cb 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1e0cc 20 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78    u.bb.alreadyEx
1e0cd 69 73 74 73 20 3d 20 28 75 2e 62 62 2e 72 65 73  ists = (u.bb.res
1e0ce 3d 3d 30 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70  ==0);.    u.bb.p
1e0cf 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e0d0 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 62 2e  o = 0;.    u.bb.
1e0d1 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1e0d2 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1e0d3 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70   }.  if( pOp->op
1e0d4 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29  code==OP_Found )
1e0d5 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 62 2e 61  {.    if( u.bb.a
1e0d6 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70  lreadyExists ) p
1e0d7 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1e0d8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
1e0d9 28 20 21 75 2e 62 62 2e 61 6c 72 65 61 64 79 45  ( !u.bb.alreadyE
1e0da 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70  xists ) pc = pOp
1e0db 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20  ->p2 - 1;.  }.  
1e0dc 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1e0dd 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31  ode: IsUnique P1
1e0de 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1e0df 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f  * Cursor P1 is o
1e0e0 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20  pen on an index 
1e0e1 62 2d 74 72 65 65 20 2d 20 74 68 61 74 20 69 73  b-tree - that is
1e0e2 20 74 6f 20 73 61 79 2c 20 61 20 62 74 72 65 65   to say, a btree
1e0e3 20 77 68 69 63 68 0a 2a 2a 20 6e 6f 20 64 61 74   which.** no dat
1e0e4 61 20 61 6e 64 20 77 68 65 72 65 20 74 68 65 20  a and where the 
1e0e5 6b 65 79 20 61 72 65 20 72 65 63 6f 72 64 73 20  key are records 
1e0e6 67 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50 5f  generated by OP_
1e0e7 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 0a  MakeRecord with.
1e0e8 2a 2a 20 74 68 65 20 6c 69 73 74 20 66 69 65 6c  ** the list fiel
1e0e9 64 20 62 65 69 6e 67 20 74 68 65 20 69 6e 74 65  d being the inte
1e0ea 67 65 72 20 52 4f 57 49 44 20 6f 66 20 74 68 65  ger ROWID of the
1e0eb 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
1e0ec 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20 72  index.** entry r
1e0ed 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20  efers to..**.** 
1e0ee 54 68 65 20 50 33 20 72 65 67 69 73 74 65 72 20  The P3 register 
1e0ef 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e 74 65  contains an inte
1e0f0 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  ger record numbe
1e0f1 72 2e 20 43 61 6c 6c 20 74 68 69 73 20 72 65 63  r. Call this rec
1e0f2 6f 72 64 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 52  ord .** number R
1e0f3 2e 20 52 65 67 69 73 74 65 72 20 50 34 20 69 73  . Register P4 is
1e0f4 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20   the first in a 
1e0f5 73 65 74 20 6f 66 20 4e 20 63 6f 6e 74 69 67 75  set of N contigu
1e0f6 6f 75 73 20 72 65 67 69 73 74 65 72 73 0a 2a 2a  ous registers.**
1e0f7 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 61 6e   that make up an
1e0f8 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20   unpacked index 
1e0f9 6b 65 79 20 74 68 61 74 20 63 61 6e 20 62 65 20  key that can be 
1e0fa 75 73 65 64 20 77 69 74 68 20 63 75 72 73 6f 72  used with cursor
1e0fb 20 50 31 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75   P1..** The valu
1e0fc 65 20 6f 66 20 4e 20 63 61 6e 20 62 65 20 69 6e  e of N can be in
1e0fd 66 65 72 72 65 64 20 66 72 6f 6d 20 74 68 65 20  ferred from the 
1e0fe 63 75 72 73 6f 72 2e 20 4e 20 69 6e 63 6c 75 64  cursor. N includ
1e0ff 65 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20  es the rowid.** 
1e100 76 61 6c 75 65 20 61 70 70 65 6e 64 65 64 20 74  value appended t
1e101 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
1e102 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2e 20 54   index record. T
1e103 68 69 73 20 72 6f 77 69 64 20 76 61 6c 75 65 20  his rowid value 
1e104 6d 61 79 0a 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f  may.** or may no
1e105 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73  t be the same as
1e106 20 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79   R..**.** If any
1e107 20 6f 66 20 74 68 65 20 4e 20 72 65 67 69 73 74   of the N regist
1e108 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ers beginning wi
1e109 74 68 20 72 65 67 69 73 74 65 72 20 50 34 20 63  th register P4 c
1e10a 6f 6e 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a  ontains a NULL.*
1e10b 2a 20 76 61 6c 75 65 2c 20 6a 75 6d 70 20 69 6d  * value, jump im
1e10c 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e  mediately to P2.
1e10d 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
1e10e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  , this instructi
1e10f 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 63 75 72  on checks if cur
1e110 73 6f 72 20 50 31 20 63 6f 6e 74 61 69 6e 73 20  sor P1 contains 
1e111 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 77 68 65 72  an entry.** wher
1e112 65 20 74 68 65 20 66 69 72 73 74 20 28 4e 2d 31  e the first (N-1
1e113 29 20 66 69 65 6c 64 73 20 6d 61 74 63 68 20 62  ) fields match b
1e114 75 74 20 74 68 65 20 72 6f 77 69 64 20 76 61 6c  ut the rowid val
1e115 75 65 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a  ue at the end.**
1e116 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e   of the index en
1e117 74 72 79 20 69 73 20 6e 6f 74 20 52 2e 20 49 66  try is not R. If
1e118 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63   there is no suc
1e119 68 20 65 6e 74 72 79 2c 20 63 6f 6e 74 72 6f 6c  h entry, control
1e11a 20 6a 75 6d 70 73 0a 2a 2a 20 74 6f 20 69 6e 73   jumps.** to ins
1e11b 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68  truction P2. Oth
1e11c 65 72 77 69 73 65 2c 20 74 68 65 20 72 6f 77 69  erwise, the rowi
1e11d 64 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63  d of the conflic
1e11e 74 69 6e 67 20 69 6e 64 65 78 0a 2a 2a 20 65 6e  ting index.** en
1e11f 74 72 79 20 69 73 20 63 6f 70 69 65 64 20 74 6f  try is copied to
1e120 20 72 65 67 69 73 74 65 72 20 50 33 20 61 6e 64   register P3 and
1e121 20 63 6f 6e 74 72 6f 6c 20 66 61 6c 6c 73 20 74   control falls t
1e122 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
1e123 78 74 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f  xt.** instructio
1e124 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  n..**.** See als
1e125 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74  o: NotFound, Not
1e126 45 78 69 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f  Exists, Found.*/
1e127 0a 63 61 73 65 20 4f 50 5f 49 73 55 6e 69 71 75  .case OP_IsUniqu
1e128 65 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  e: {        /* j
1e129 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20  ump, in3 */.#if 
1e12a 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e12b 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e12c 20 75 2e 62 63 20 2a 2f 0a 20 20 75 31 36 20 69   u.bc */.  u16 i
1e12d 69 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  i;.  VdbeCursor 
1e12e 2a 70 43 78 3b 0a 20 20 42 74 43 75 72 73 6f 72  *pCx;.  BtCursor
1e12f 20 2a 70 43 72 73 72 3b 0a 20 20 75 31 36 20 6e   *pCrsr;.  u16 n
1e130 46 69 65 6c 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d  Field;.  Mem *aM
1e131 78 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63  x;.  UnpackedRec
1e132 6f 72 64 20 72 3b 20 20 20 20 20 20 20 20 20 20  ord r;          
1e133 20 20 20 20 20 20 20 20 2f 2a 20 42 2d 54 72 65          /* B-Tre
1e134 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b  e index search k
1e135 65 79 20 2a 2f 0a 20 20 69 36 34 20 52 3b 20 20  ey */.  i64 R;  
1e136 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e137 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
1e138 77 69 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65  wid stored in re
1e139 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 23 65 6e  gister P3 */.#en
1e13a 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e13b 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e13c 6f 20 75 2e 62 63 20 2a 2f 0a 0a 20 20 70 49 6e  o u.bc */..  pIn
1e13d 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  3 = &aMem[pOp->p
1e13e 33 5d 3b 0a 20 20 75 2e 62 63 2e 61 4d 78 20 3d  3];.  u.bc.aMx =
1e13f 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69   &aMem[pOp->p4.i
1e140 5d 3b 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74  ];.  /* Assert t
1e141 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 6f  hat the values o
1e142 66 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 20  f parameters P1 
1e143 61 6e 64 20 50 34 20 61 72 65 20 69 6e 20 72 61  and P4 are in ra
1e144 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  nge. */.  assert
1e145 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1e146 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 73  4_INT32 );.  ass
1e147 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 69 3e 30  ert( pOp->p4.i>0
1e148 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d 70   && pOp->p4.i<=p
1e149 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65  ->nMem );.  asse
1e14a 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e14b 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e14c 72 73 6f 72 20 29 3b 0a 0a 20 20 2f 2a 20 46 69  rsor );..  /* Fi
1e14d 6e 64 20 74 68 65 20 69 6e 64 65 78 20 63 75 72  nd the index cur
1e14e 73 6f 72 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e 70  sor. */.  u.bc.p
1e14f 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  Cx = p->apCsr[pO
1e150 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e151 28 20 75 2e 62 63 2e 70 43 78 2d 3e 64 65 66 65  ( u.bc.pCx->defe
1e152 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b  rredMoveto==0 );
1e153 0a 20 20 75 2e 62 63 2e 70 43 78 2d 3e 73 65 65  .  u.bc.pCx->see
1e154 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 75  kResult = 0;.  u
1e155 2e 62 63 2e 70 43 78 2d 3e 63 61 63 68 65 53 74  .bc.pCx->cacheSt
1e156 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1e157 4c 45 3b 0a 20 20 75 2e 62 63 2e 70 43 72 73 72  LE;.  u.bc.pCrsr
1e158 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 43 75   = u.bc.pCx->pCu
1e159 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  rsor;..  /* If a
1e15a 6e 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73  ny of the values
1e15b 20 61 72 65 20 4e 55 4c 4c 2c 20 74 61 6b 65 20   are NULL, take 
1e15c 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a 20 20 75  the jump. */.  u
1e15d 2e 62 63 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 62  .bc.nField = u.b
1e15e 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d  c.pCx->pKeyInfo-
1e15f 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f 72 28 75  >nField;.  for(u
1e160 2e 62 63 2e 69 69 3d 30 3b 20 75 2e 62 63 2e 69  .bc.ii=0; u.bc.i
1e161 69 3c 75 2e 62 63 2e 6e 46 69 65 6c 64 3b 20 75  i<u.bc.nField; u
1e162 2e 62 63 2e 69 69 2b 2b 29 7b 0a 20 20 20 20 69  .bc.ii++){.    i
1e163 66 28 20 75 2e 62 63 2e 61 4d 78 5b 75 2e 62 63  f( u.bc.aMx[u.bc
1e164 2e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d  .ii].flags & MEM
1e165 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70  _Null ){.      p
1e166 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1e167 0a 20 20 20 20 20 20 75 2e 62 63 2e 70 43 72 73  .      u.bc.pCrs
1e168 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65  r = 0;.      bre
1e169 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
1e16a 61 73 73 65 72 74 28 20 28 75 2e 62 63 2e 61 4d  assert( (u.bc.aM
1e16b 78 5b 75 2e 62 63 2e 6e 46 69 65 6c 64 5d 2e 66  x[u.bc.nField].f
1e16c 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1e16d 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 75 2e  ==0 );..  if( u.
1e16e 62 63 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a 20  bc.pCrsr!=0 ){. 
1e16f 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74     /* Populate t
1e170 68 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 20  he index search 
1e171 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 75 2e 62 63  key. */.    u.bc
1e172 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e  .r.pKeyInfo = u.
1e173 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f  bc.pCx->pKeyInfo
1e174 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 6e 46 69  ;.    u.bc.r.nFi
1e175 65 6c 64 20 3d 20 75 2e 62 63 2e 6e 46 69 65 6c  eld = u.bc.nFiel
1e176 64 20 2b 20 31 3b 0a 20 20 20 20 75 2e 62 63 2e  d + 1;.    u.bc.
1e177 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b  r.flags = UNPACK
1e178 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48  ED_PREFIX_SEARCH
1e179 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 61 4d 65  ;.    u.bc.r.aMe
1e17a 6d 20 3d 20 75 2e 62 63 2e 61 4d 78 3b 0a 0a 20  m = u.bc.aMx;.. 
1e17b 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68     /* Extract th
1e17c 65 20 76 61 6c 75 65 20 6f 66 20 75 2e 62 63 2e  e value of u.bc.
1e17d 52 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20  R from register 
1e17e 50 33 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  P3. */.    sqlit
1e17f 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1e180 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 75  ify(pIn3);.    u
1e181 2e 62 63 2e 52 20 3d 20 70 49 6e 33 2d 3e 75 2e  .bc.R = pIn3->u.
1e182 69 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 61 72 63  i;..    /* Searc
1e183 68 20 74 68 65 20 42 2d 54 72 65 65 20 69 6e 64  h the B-Tree ind
1e184 65 78 2e 20 49 66 20 6e 6f 20 63 6f 6e 66 6c 69  ex. If no confli
1e185 63 74 69 6e 67 20 72 65 63 6f 72 64 20 69 73 20  cting record is 
1e186 66 6f 75 6e 64 2c 20 6a 75 6d 70 0a 20 20 20 20  found, jump.    
1e187 2a 2a 20 74 6f 20 50 32 2e 20 4f 74 68 65 72 77  ** to P2. Otherw
1e188 69 73 65 2c 20 63 6f 70 79 20 74 68 65 20 72 6f  ise, copy the ro
1e189 77 69 64 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c  wid of the confl
1e18a 69 63 74 69 6e 67 20 72 65 63 6f 72 64 20 74 6f  icting record to
1e18b 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72  .    ** register
1e18c 20 50 33 20 61 6e 64 20 66 61 6c 6c 20 74 68 72   P3 and fall thr
1e18d 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
1e18e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 2a   instruction.  *
1e18f 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
1e190 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1e191 61 63 6b 65 64 28 75 2e 62 63 2e 70 43 72 73 72  acked(u.bc.pCrsr
1e192 2c 20 26 75 2e 62 63 2e 72 2c 20 30 2c 20 30 2c  , &u.bc.r, 0, 0,
1e193 20 26 75 2e 62 63 2e 70 43 78 2d 3e 73 65 65 6b   &u.bc.pCx->seek
1e194 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 69 66 28  Result);.    if(
1e195 20 28 75 2e 62 63 2e 72 2e 66 6c 61 67 73 20 26   (u.bc.r.flags &
1e196 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58   UNPACKED_PREFIX
1e197 5f 53 45 41 52 43 48 29 20 7c 7c 20 75 2e 62 63  _SEARCH) || u.bc
1e198 2e 72 2e 72 6f 77 69 64 3d 3d 75 2e 62 63 2e 52  .r.rowid==u.bc.R
1e199 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70   ){.      pc = p
1e19a 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1e19b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e  }else{.      pIn
1e19c 33 2d 3e 75 2e 69 20 3d 20 75 2e 62 63 2e 72 2e  3->u.i = u.bc.r.
1e19d 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 7d  rowid;.    }.  }
1e19e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1e19f 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 78 69 73 74  Opcode: NotExist
1e1a0 73 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  s P1 P2 P3 * *.*
1e1a1 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 63 6f 6e  *.** Use the con
1e1a2 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72  tent of register
1e1a3 20 50 33 20 61 73 20 61 20 69 6e 74 65 67 65 72   P3 as a integer
1e1a4 20 6b 65 79 2e 20 20 49 66 20 61 20 72 65 63 6f   key.  If a reco
1e1a5 72 64 20 0a 2a 2a 20 77 69 74 68 20 74 68 61 74  rd .** with that
1e1a6 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 65 78   key does not ex
1e1a7 69 73 74 20 69 6e 20 74 61 62 6c 65 20 6f 66 20  ist in table of 
1e1a8 50 31 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  P1, then jump to
1e1a9 20 50 32 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20   P2. .** If the 
1e1aa 72 65 63 6f 72 64 20 64 6f 65 73 20 65 78 69 73  record does exis
1e1ab 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  t, then fall thr
1e1ac 75 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 69  u.  The cursor i
1e1ad 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f 69 6e 74  s left .** point
1e1ae 69 6e 67 20 74 6f 20 74 68 65 20 72 65 63 6f 72  ing to the recor
1e1af 64 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a  d if it exists..
1e1b0 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72  **.** The differ
1e1b1 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69  ence between thi
1e1b2 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20  s operation and 
1e1b3 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 68 61 74  NotFound is that
1e1b4 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 74 69   this.** operati
1e1b5 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 6b  on assumes the k
1e1b6 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  ey is an integer
1e1b7 20 61 6e 64 20 74 68 61 74 20 50 31 20 69 73 20   and that P1 is 
1e1b8 61 20 74 61 62 6c 65 20 77 68 65 72 65 61 73 0a  a table whereas.
1e1b9 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 73 73 75  ** NotFound assu
1e1ba 6d 65 73 20 6b 65 79 20 69 73 20 61 20 62 6c 6f  mes key is a blo
1e1bb 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72  b constructed fr
1e1bc 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 20 61 6e  om MakeRecord an
1e1bd 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 69 6e  d.** P1 is an in
1e1be 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  dex..**.** See a
1e1bf 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46  lso: Found, NotF
1e1c0 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 0a 2a  ound, IsUnique.*
1e1c1 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 45 78 69  /.case OP_NotExi
1e1c2 73 74 73 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  sts: {        /*
1e1c3 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69   jump, in3 */.#i
1e1c4 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e1c5 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e1c6 74 6f 20 75 2e 62 64 20 2a 2f 0a 20 20 56 64 62  to u.bd */.  Vdb
1e1c7 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1e1c8 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1e1c9 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 75 36 34    int res;.  u64
1e1ca 20 69 4b 65 79 3b 0a 23 65 6e 64 69 66 20 2f 2a   iKey;.#endif /*
1e1cb 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e1cc 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 64   moved into u.bd
1e1cd 20 2a 2f 0a 0a 20 20 70 49 6e 33 20 3d 20 26 61   */..  pIn3 = &a
1e1ce 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1e1cf 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c  assert( pIn3->fl
1e1d0 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
1e1d1 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e1d2 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e1d3 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e1d4 20 75 2e 62 64 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bd.pC = p->ap
1e1d5 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e1d6 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 21  assert( u.bd.pC!
1e1d7 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e1d8 75 2e 62 64 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bd.pC->isTable
1e1d9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1e1da 62 64 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  bd.pC->pseudoTab
1e1db 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 75 2e  leReg==0 );.  u.
1e1dc 62 64 2e 70 43 72 73 72 20 3d 20 75 2e 62 64 2e  bd.pCrsr = u.bd.
1e1dd 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69  pC->pCursor;.  i
1e1de 66 28 20 75 2e 62 64 2e 70 43 72 73 72 21 3d 30  f( u.bd.pCrsr!=0
1e1df 20 29 7b 0a 20 20 20 20 75 2e 62 64 2e 72 65 73   ){.    u.bd.res
1e1e0 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 69   = 0;.    u.bd.i
1e1e1 4b 65 79 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b  Key = pIn3->u.i;
1e1e2 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1e1e3 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
1e1e4 63 6b 65 64 28 75 2e 62 64 2e 70 43 72 73 72 2c  cked(u.bd.pCrsr,
1e1e5 20 30 2c 20 75 2e 62 64 2e 69 4b 65 79 2c 20 30   0, u.bd.iKey, 0
1e1e6 2c 20 26 75 2e 62 64 2e 72 65 73 29 3b 0a 20 20  , &u.bd.res);.  
1e1e7 20 20 75 2e 62 64 2e 70 43 2d 3e 6c 61 73 74 52    u.bd.pC->lastR
1e1e8 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e 75 2e 69  owid = pIn3->u.i
1e1e9 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 72  ;.    u.bd.pC->r
1e1ea 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 75 2e  owidIsValid = u.
1e1eb 62 64 2e 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a  bd.res==0 ?1:0;.
1e1ec 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6e 75 6c      u.bd.pC->nul
1e1ed 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 2e  lRow = 0;.    u.
1e1ee 62 64 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bd.pC->cacheStat
1e1ef 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e1f0 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 64  ;.    u.bd.pC->d
1e1f1 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1e1f2 30 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 64 2e  0;.    if( u.bd.
1e1f3 72 65 73 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  res!=0 ){.      
1e1f4 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1e1f5 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1e1f6 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 49 73  u.bd.pC->rowidIs
1e1f7 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20  Valid==0 );.    
1e1f8 7d 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73  }.    u.bd.pC->s
1e1f9 65 65 6b 52 65 73 75 6c 74 20 3d 20 75 2e 62 64  eekResult = u.bd
1e1fa 2e 72 65 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  .res;.  }else{. 
1e1fb 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65     /* This happe
1e1fc 6e 73 20 77 68 65 6e 20 61 6e 20 61 74 74 65 6d  ns when an attem
1e1fd 70 74 20 74 6f 20 6f 70 65 6e 20 61 20 72 65 61  pt to open a rea
1e1fe 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65 0a  d cursor on the.
1e1ff 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61      ** sqlite_ma
1e200 73 74 65 72 20 74 61 62 6c 65 20 72 65 74 75 72  ster table retur
1e201 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e  ns SQLITE_EMPTY.
1e202 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d  .    */.    pc =
1e203 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1e204 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70    assert( u.bd.p
1e205 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d  C->rowidIsValid=
1e206 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 64 2e 70  =0 );.    u.bd.p
1e207 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20  C->seekResult = 
1e208 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  0;.  }.  break;.
1e209 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  }../* Opcode: Se
1e20a 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a 20 2a  quence P1 P2 * *
1e20b 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74 68   *.**.** Find th
1e20c 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65  e next available
1e20d 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72   sequence number
1e20e 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a   for cursor P1..
1e20f 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 65 71  ** Write the seq
1e210 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e 74  uence number int
1e211 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  o register P2..*
1e212 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 20 6e  * The sequence n
1e213 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 75 72  umber on the cur
1e214 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  sor is increment
1e215 65 64 20 61 66 74 65 72 20 74 68 69 73 0a 2a 2a  ed after this.**
1e216 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a   instruction.  .
1e217 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75 65  */.case OP_Seque
1e218 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20  nce: {          
1e219 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1e21a 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ase */.  assert(
1e21b 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1e21c 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
1e21d 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1e21e 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e21f 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 75  !=0 );.  pOut->u
1e220 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  .i = p->apCsr[pO
1e221 70 2d 3e 70 31 5d 2d 3e 73 65 71 43 6f 75 6e 74  p->p1]->seqCount
1e222 2b 2b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  ++;.  break;.}..
1e223 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52  ./* Opcode: NewR
1e224 6f 77 69 64 20 50 31 20 50 32 20 50 33 20 2a 20  owid P1 P2 P3 * 
1e225 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65  *.**.** Get a ne
1e226 77 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64  w integer record
1e227 20 6e 75 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22   number (a.k.a "
1e228 72 6f 77 69 64 22 29 20 75 73 65 64 20 61 73 20  rowid") used as 
1e229 74 68 65 20 6b 65 79 20 74 6f 20 61 20 74 61 62  the key to a tab
1e22a 6c 65 2e 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72  le..** The recor
1e22b 64 20 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20  d number is not 
1e22c 70 72 65 76 69 6f 75 73 6c 79 20 75 73 65 64 20  previously used 
1e22d 61 73 20 61 20 6b 65 79 20 69 6e 20 74 68 65 20  as a key in the 
1e22e 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c  database.** tabl
1e22f 65 20 74 68 61 74 20 63 75 72 73 6f 72 20 50 31  e that cursor P1
1e230 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 54 68 65   points to.  The
1e231 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62   new record numb
1e232 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  er is written.**
1e233 20 77 72 69 74 74 65 6e 20 74 6f 20 72 65 67 69   written to regi
1e234 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49  ster P2..**.** I
1e235 66 20 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69  f P3>0 then P3 i
1e236 73 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20  s a register in 
1e237 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f  the root frame o
1e238 66 20 74 68 69 73 20 56 44 42 45 20 74 68 61 74  f this VDBE that
1e239 20 68 6f 6c 64 73 20 0a 2a 2a 20 74 68 65 20 6c   holds .** the l
1e23a 61 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c  argest previousl
1e23b 79 20 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f  y generated reco
1e23c 72 64 20 6e 75 6d 62 65 72 2e 20 4e 6f 20 6e 65  rd number. No ne
1e23d 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73  w record numbers
1e23e 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20   are.** allowed 
1e23f 74 6f 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20  to be less than 
1e240 74 68 69 73 20 76 61 6c 75 65 2e 20 57 68 65 6e  this value. When
1e241 20 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63   this value reac
1e242 68 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c  hes its maximum,
1e243 20 0a 2a 2a 20 61 20 53 51 4c 49 54 45 5f 46 55   .** a SQLITE_FU
1e244 4c 4c 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65  LL error is gene
1e245 72 61 74 65 64 2e 20 54 68 65 20 50 33 20 72 65  rated. The P3 re
1e246 67 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65  gister is update
1e247 64 20 77 69 74 68 20 74 68 65 20 27 0a 2a 2a 20  d with the '.** 
1e248 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64  generated record
1e249 20 6e 75 6d 62 65 72 2e 20 54 68 69 73 20 50 33   number. This P3
1e24a 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73   mechanism is us
1e24b 65 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65  ed to help imple
1e24c 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f  ment the.** AUTO
1e24d 49 4e 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72  INCREMENT featur
1e24e 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65  e..*/.case OP_Ne
1e24f 77 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20  wRowid: {       
1e250 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1e251 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20  elease */.#if 0 
1e252 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e253 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e254 2e 62 65 20 2a 2f 0a 20 20 69 36 34 20 76 3b 20  .be */.  i64 v; 
1e255 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e256 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64  /* The new rowid
1e257 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1e258 20 2a 70 43 3b 20 20 20 20 20 20 20 20 2f 2a 20   *pC;        /* 
1e259 43 75 72 73 6f 72 20 6f 66 20 74 61 62 6c 65 20  Cursor of table 
1e25a 74 6f 20 67 65 74 20 74 68 65 20 6e 65 77 20 72  to get the new r
1e25b 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65  owid */.  int re
1e25c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
1e25d 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e   /* Result of an
1e25e 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73   sqlite3BtreeLas
1e25f 74 28 29 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  t() */.  int cnt
1e260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1e261 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69  /* Counter to li
1e262 6d 69 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  mit the number o
1e263 66 20 73 65 61 72 63 68 65 73 20 2a 2f 0a 20 20  f searches */.  
1e264 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20  Mem *pMem;      
1e265 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
1e266 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 65  er holding large
1e267 73 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55 54  st rowid for AUT
1e268 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20  OINCREMENT */.  
1e269 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d  VdbeFrame *pFram
1e26a 65 3b 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66  e;     /* Root f
1e26b 72 61 6d 65 20 6f 66 20 56 44 42 45 20 2a 2f 0a  rame of VDBE */.
1e26c 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1e26d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e26e 69 6e 74 6f 20 75 2e 62 65 20 2a 2f 0a 0a 20 20  into u.be */..  
1e26f 75 2e 62 65 2e 76 20 3d 20 30 3b 0a 20 20 75 2e  u.be.v = 0;.  u.
1e270 62 65 2e 72 65 73 20 3d 20 30 3b 0a 20 20 61 73  be.res = 0;.  as
1e271 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e272 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e273 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 65  Cursor );.  u.be
1e274 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e275 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e276 74 28 20 75 2e 62 65 2e 70 43 21 3d 30 20 29 3b  t( u.be.pC!=0 );
1e277 0a 20 20 69 66 28 20 4e 45 56 45 52 28 75 2e 62  .  if( NEVER(u.b
1e278 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3d 3d 30  e.pC->pCursor==0
1e279 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  ) ){.    /* The 
1e27a 7a 65 72 6f 20 69 6e 69 74 69 61 6c 69 7a 61 74  zero initializat
1e27b 69 6f 6e 20 61 62 6f 76 65 20 69 73 20 61 6c 6c  ion above is all
1e27c 20 74 68 61 74 20 69 73 20 6e 65 65 64 65 64 20   that is needed 
1e27d 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  */.  }else{.    
1e27e 2f 2a 20 54 68 65 20 6e 65 78 74 20 72 6f 77 69  /* The next rowi
1e27f 64 20 6f 72 20 72 65 63 6f 72 64 20 6e 75 6d 62  d or record numb
1e280 65 72 20 28 64 69 66 66 65 72 65 6e 74 20 74 65  er (different te
1e281 72 6d 73 20 66 6f 72 20 74 68 65 20 73 61 6d 65  rms for the same
1e282 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67 29 20 69  .    ** thing) i
1e283 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 61 20  s obtained in a 
1e284 74 77 6f 2d 73 74 65 70 20 61 6c 67 6f 72 69 74  two-step algorit
1e285 68 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  hm..    **.    *
1e286 2a 20 46 69 72 73 74 20 77 65 20 61 74 74 65 6d  * First we attem
1e287 70 74 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6c  pt to find the l
1e288 61 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20  argest existing 
1e289 72 6f 77 69 64 20 61 6e 64 20 61 64 64 20 6f 6e  rowid and add on
1e28a 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 61 74  e.    ** to that
1e28b 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6c 61  .  But if the la
1e28c 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 72  rgest existing r
1e28d 6f 77 69 64 20 69 73 20 61 6c 72 65 61 64 79 20  owid is already 
1e28e 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20  the maximum.    
1e28f 2a 2a 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65  ** positive inte
1e290 67 65 72 2c 20 77 65 20 68 61 76 65 20 74 6f 20  ger, we have to 
1e291 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20  fall through to 
1e292 74 68 65 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a  the second.    *
1e293 2a 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20  * probabilistic 
1e294 61 6c 67 6f 72 69 74 68 6d 0a 20 20 20 20 2a 2a  algorithm.    **
1e295 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  .    ** The seco
1e296 6e 64 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20  nd algorithm is 
1e297 74 6f 20 73 65 6c 65 63 74 20 61 20 72 6f 77 69  to select a rowi
1e298 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20  d at random and 
1e299 73 65 65 20 69 66 0a 20 20 20 20 2a 2a 20 69 74  see if.    ** it
1e29a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
1e29b 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49  in the table.  I
1e29c 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78  f it does not ex
1e29d 69 73 74 2c 20 77 65 20 68 61 76 65 0a 20 20 20  ist, we have.   
1e29e 20 2a 2a 20 73 75 63 63 65 65 64 65 64 2e 20 20   ** succeeded.  
1e29f 49 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 72 6f  If the random ro
1e2a0 77 69 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20  wid does exist, 
1e2a1 77 65 20 73 65 6c 65 63 74 20 61 20 6e 65 77 20  we select a new 
1e2a2 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74  one.    ** and t
1e2a3 72 79 20 61 67 61 69 6e 2c 20 75 70 20 74 6f 20  ry again, up to 
1e2a4 31 30 30 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a  100 times..    *
1e2a5 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  /.    assert( u.
1e2a6 62 65 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29  be.pC->isTable )
1e2a7 3b 0a 20 20 20 20 75 2e 62 65 2e 63 6e 74 20 3d  ;.    u.be.cnt =
1e2a8 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   0;..#ifdef SQLI
1e2a9 54 45 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23  TE_32BIT_ROWID.#
1e2aa 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f     define MAX_RO
1e2ab 57 49 44 20 30 78 37 66 66 66 66 66 66 66 0a 23  WID 0x7fffffff.#
1e2ac 65 6c 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65  else.    /* Some
1e2ad 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c   compilers compl
1e2ae 61 69 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61  ain about consta
1e2af 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  nts of the form 
1e2b0 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66  0x7fffffffffffff
1e2b1 66 66 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72  ff..    ** Other
1e2b2 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74  s complain about
1e2b3 20 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66   0x7ffffffffffff
1e2b4 66 66 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c  ffffLL.  The fol
1e2b5 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65  lowing macro see
1e2b6 6d 73 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f  ms.    ** to pro
1e2b7 76 69 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e  vide the constan
1e2b8 74 20 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61  t while making a
1e2b9 6c 6c 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70  ll compilers hap
1e2ba 70 79 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64  py..    */.#   d
1e2bb 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20  efine MAX_ROWID 
1e2bc 20 28 69 36 34 29 28 20 28 28 28 75 36 34 29 30   (i64)( (((u64)0
1e2bd 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 20  x7fffffff)<<32) 
1e2be 7c 20 28 75 36 34 29 30 78 66 66 66 66 66 66 66  | (u64)0xfffffff
1e2bf 66 20 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  f ).#endif..    
1e2c0 69 66 28 20 21 75 2e 62 65 2e 70 43 2d 3e 75 73  if( !u.be.pC->us
1e2c1 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a  eRandomRowid ){.
1e2c2 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 73        u.be.v = s
1e2c3 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61  qlite3BtreeGetCa
1e2c4 63 68 65 64 52 6f 77 69 64 28 75 2e 62 65 2e 70  chedRowid(u.be.p
1e2c5 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20  C->pCursor);.   
1e2c6 20 20 20 69 66 28 20 75 2e 62 65 2e 76 3d 3d 30     if( u.be.v==0
1e2c7 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1e2c8 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73   sqlite3BtreeLas
1e2c9 74 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73  t(u.be.pC->pCurs
1e2ca 6f 72 2c 20 26 75 2e 62 65 2e 72 65 73 29 3b 0a  or, &u.be.res);.
1e2cb 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1e2cc 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1e2cd 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
1e2ce 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1e2cf 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e2d0 20 20 69 66 28 20 75 2e 62 65 2e 72 65 73 20 29    if( u.be.res )
1e2d1 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65  {.          u.be
1e2d2 2e 76 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  .v = 1;.        
1e2d3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1e2d4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1e2d5 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c  BtreeCursorIsVal
1e2d6 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72  id(u.be.pC->pCur
1e2d7 73 6f 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20  sor) );.        
1e2d8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e2d9 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 62 65 2e  reeKeySize(u.be.
1e2da 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e  pC->pCursor, &u.
1e2db 62 65 2e 76 29 3b 0a 20 20 20 20 20 20 20 20 20  be.v);.         
1e2dc 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1e2dd 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 43  ITE_OK );   /* C
1e2de 61 6e 6e 6f 74 20 66 61 69 6c 20 66 6f 6c 6c 6f  annot fail follo
1e2df 77 69 6e 67 20 42 74 72 65 65 4c 61 73 74 28 29  wing BtreeLast()
1e2e0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66   */.          if
1e2e1 28 20 75 2e 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f  ( u.be.v==MAX_RO
1e2e2 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20  WID ){.         
1e2e3 20 20 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52     u.be.pC->useR
1e2e4 61 6e 64 6f 6d 52 6f 77 69 64 20 3d 20 31 3b 0a  andomRowid = 1;.
1e2e5 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
1e2e6 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 2e 62  .            u.b
1e2e7 65 2e 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  e.v++;.         
1e2e8 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1e2e9 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51     }..#ifndef SQ
1e2ea 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
1e2eb 43 52 45 4d 45 4e 54 0a 20 20 20 20 20 20 69 66  CREMENT.      if
1e2ec 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20  ( pOp->p3 ){.   
1e2ed 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74       /* Assert t
1e2ee 68 61 74 20 50 33 20 69 73 20 61 20 76 61 6c 69  hat P3 is a vali
1e2ef 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a  d memory cell. *
1e2f0 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
1e2f1 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20  ( pOp->p3>0 );. 
1e2f2 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 46         if( p->pF
1e2f3 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20  rame ){.        
1e2f4 20 20 66 6f 72 28 75 2e 62 65 2e 70 46 72 61 6d    for(u.be.pFram
1e2f5 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62  e=p->pFrame; u.b
1e2f6 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e  e.pFrame->pParen
1e2f7 74 3b 20 75 2e 62 65 2e 70 46 72 61 6d 65 3d 75  t; u.be.pFrame=u
1e2f8 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72  .be.pFrame->pPar
1e2f9 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ent);.          
1e2fa 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 50  /* Assert that P
1e2fb 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d  3 is a valid mem
1e2fc 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20  ory cell. */.   
1e2fd 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1e2fe 4f 70 2d 3e 70 33 3c 3d 75 2e 62 65 2e 70 46 72  Op->p3<=u.be.pFr
1e2ff 61 6d 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  ame->nMem );.   
1e300 20 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d         u.be.pMem
1e301 20 3d 20 26 75 2e 62 65 2e 70 46 72 61 6d 65 2d   = &u.be.pFrame-
1e302 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1e303 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1e304 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65           /* Asse
1e305 72 74 20 74 68 61 74 20 50 33 20 69 73 20 61 20  rt that P3 is a 
1e306 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c  valid memory cel
1e307 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  l. */.          
1e308 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c  assert( pOp->p3<
1e309 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20  =p->nMem );.    
1e30a 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20        u.be.pMem 
1e30b 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  = &aMem[pOp->p3]
1e30c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20  ;.        }..   
1e30d 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52       REGISTER_TR
1e30e 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62  ACE(pOp->p3, u.b
1e30f 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20  e.pMem);.       
1e310 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1e311 6e 74 65 67 65 72 69 66 79 28 75 2e 62 65 2e 70  ntegerify(u.be.p
1e312 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61 73  Mem);.        as
1e313 73 65 72 74 28 20 28 75 2e 62 65 2e 70 4d 65 6d  sert( (u.be.pMem
1e314 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1e315 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65 6d  t)!=0 );  /* mem
1e316 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69 6e  (P3) holds an in
1e317 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 20  teger */.       
1e318 20 69 66 28 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e   if( u.be.pMem->
1e319 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c  u.i==MAX_ROWID |
1e31a 7c 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61  | u.be.pC->useRa
1e31b 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20  ndomRowid ){.   
1e31c 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1e31d 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 20  TE_FULL;.       
1e31e 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1e31f 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1e320 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1e321 28 20 75 2e 62 65 2e 76 3c 75 2e 62 65 2e 70 4d  ( u.be.v<u.be.pM
1e322 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20  em->u.i+1 ){.   
1e323 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20         u.be.v = 
1e324 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 2b  u.be.pMem->u.i +
1e325 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
1e326 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 2d        u.be.pMem-
1e327 3e 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20  >u.i = u.be.v;. 
1e328 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20       }.#endif.. 
1e329 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1e32a 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28  eSetCachedRowid(
1e32b 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.be.pC->pCursor
1e32c 2c 20 75 2e 62 65 2e 76 3c 4d 41 58 5f 52 4f 57  , u.be.v<MAX_ROW
1e32d 49 44 20 3f 20 75 2e 62 65 2e 76 2b 31 20 3a 20  ID ? u.be.v+1 : 
1e32e 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  0);.    }.    if
1e32f 28 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61  ( u.be.pC->useRa
1e330 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20  ndomRowid ){.   
1e331 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1e332 70 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 57 65 20  p3==0 );  /* We 
1e333 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 72 61 6e  cannot be in ran
1e334 64 6f 6d 20 72 6f 77 69 64 20 6d 6f 64 65 20 69  dom rowid mode i
1e335 66 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20  f this is.      
1e336 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e337 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 41 55 54         ** an AUT
1e338 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c 65  OINCREMENT table
1e339 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62 65 2e  . */.      u.be.
1e33a 76 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69  v = db->lastRowi
1e33b 64 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 63 6e  d;.      u.be.cn
1e33c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b  t = 0;.      do{
1e33d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62  .        if( u.b
1e33e 65 2e 63 6e 74 3d 3d 30 20 26 26 20 28 75 2e 62  e.cnt==0 && (u.b
1e33f 65 2e 76 26 30 78 66 66 66 66 66 66 29 3d 3d 75  e.v&0xffffff)==u
1e340 2e 62 65 2e 76 20 29 7b 0a 20 20 20 20 20 20 20  .be.v ){.       
1e341 20 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20     u.be.v++;.   
1e342 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1e343 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61        sqlite3_ra
1e344 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28  ndomness(sizeof(
1e345 75 2e 62 65 2e 76 29 2c 20 26 75 2e 62 65 2e 76  u.be.v), &u.be.v
1e346 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
1e347 20 75 2e 62 65 2e 63 6e 74 3c 35 20 29 20 75 2e   u.be.cnt<5 ) u.
1e348 62 65 2e 76 20 26 3d 20 30 78 66 66 66 66 66 66  be.v &= 0xffffff
1e349 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1e34a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e34b 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1e34c 6b 65 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75  ked(u.be.pC->pCu
1e34d 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75 2e  rsor, 0, (u64)u.
1e34e 62 65 2e 76 2c 20 30 2c 20 26 75 2e 62 65 2e 72  be.v, 0, &u.be.r
1e34f 65 73 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 62  es);.        u.b
1e350 65 2e 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d  e.cnt++;.      }
1e351 77 68 69 6c 65 28 20 75 2e 62 65 2e 63 6e 74 3c  while( u.be.cnt<
1e352 31 30 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  100 && rc==SQLIT
1e353 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 73  E_OK && u.be.res
1e354 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ==0 );.      if(
1e355 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1e356 26 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29 7b  & u.be.res==0 ){
1e357 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1e358 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20  LITE_FULL;.     
1e359 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1e35a 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1e35b 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e    }.    }.    u.
1e35c 62 65 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  be.pC->rowidIsVa
1e35d 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62  lid = 0;.    u.b
1e35e 65 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  e.pC->deferredMo
1e35f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e  veto = 0;.    u.
1e360 62 65 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  be.pC->cacheStat
1e361 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e362 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e  ;.  }.  pOut->u.
1e363 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 62 72  i = u.be.v;.  br
1e364 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e365 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 32 20  e: Insert P1 P2 
1e366 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57  P3 P4 P5.**.** W
1e367 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  rite an entry in
1e368 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20  to the table of 
1e369 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 6e 65  cursor P1.  A ne
1e36a 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 63 72  w entry is.** cr
1e36b 65 61 74 65 64 20 69 66 20 69 74 20 64 6f 65 73  eated if it does
1e36c 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  n't already exis
1e36d 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 66 6f  t or the data fo
1e36e 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a  r an existing.**
1e36f 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 77 72   entry is overwr
1e370 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 74 61  itten.  The data
1e371 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 4d 45   is the value ME
1e372 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e  M_Blob stored in
1e373 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e 75 6d   register.** num
1e374 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65 79 20  ber P2. The key 
1e375 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
1e376 69 73 74 65 72 20 50 33 2e 20 54 68 65 20 6b 65  ister P3. The ke
1e377 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 20 4d  y must.** be a M
1e378 45 4d 5f 49 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  EM_Int..**.** If
1e379 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41   the OPFLAG_NCHA
1e37a 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35 20 69  NGE flag of P5 i
1e37b 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  s set, then the 
1e37c 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  row change count
1e37d 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74   is.** increment
1e37e 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f  ed (otherwise no
1e37f 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50 46 4c  t).  If the OPFL
1e380 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66 6c 61  AG_LASTROWID fla
1e381 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 0a  g of P5 is set,.
1e382 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20 69 73  ** then rowid is
1e383 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75 62 73   stored for subs
1e384 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20 62 79  equent return by
1e385 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
1e386 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
1e387 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 6f 74  d() function (ot
1e388 68 65 72 77 69 73 65 20 69 74 20 69 73 20 75 6e  herwise it is un
1e389 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a  modified)..**.**
1e38a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 55   If the OPFLAG_U
1e38b 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61  SESEEKRESULT fla
1e38c 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 61  g of P5 is set a
1e38d 6e 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  nd if the result
1e38e 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20   of.** the last 
1e38f 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e 20 28  seek operation (
1e390 4f 50 5f 4e 6f 74 45 78 69 73 74 73 29 20 77 61  OP_NotExists) wa
1e391 73 20 61 20 73 75 63 63 65 73 73 2c 20 74 68 65  s a success, the
1e392 6e 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 74  n this.** operat
1e393 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74  ion will not att
1e394 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 68 65  empt to find the
1e395 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 6f 77   appropriate row
1e396 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 0a 2a 2a   before doing.**
1e397 20 74 68 65 20 69 6e 73 65 72 74 20 62 75 74 20   the insert but 
1e398 77 69 6c 6c 20 69 6e 73 74 65 61 64 20 6f 76 65  will instead ove
1e399 72 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74  rwrite the row t
1e39a 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69  hat the cursor i
1e39b 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 70  s.** currently p
1e39c 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 50 72 65  ointing to.  Pre
1e39d 73 75 6d 61 62 6c 79 2c 20 74 68 65 20 70 72 69  sumably, the pri
1e39e 6f 72 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20  or OP_NotExists 
1e39f 6f 70 63 6f 64 65 0a 2a 2a 20 68 61 73 20 61 6c  opcode.** has al
1e3a0 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64  ready positioned
1e3a1 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f 72 72   the cursor corr
1e3a2 65 63 74 6c 79 2e 20 20 54 68 69 73 20 69 73 20  ectly.  This is 
1e3a3 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a  an optimization.
1e3a4 2a 2a 20 74 68 61 74 20 62 6f 6f 73 74 73 20 70  ** that boosts p
1e3a5 65 72 66 6f 72 6d 61 6e 63 65 20 62 79 20 61 76  erformance by av
1e3a6 6f 69 64 69 6e 67 20 72 65 64 75 6e 64 61 6e 74  oiding redundant
1e3a7 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   seeks..**.** If
1e3a8 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53 55 50   the OPFLAG_ISUP
1e3a9 44 41 54 45 20 66 6c 61 67 20 69 73 20 73 65 74  DATE flag is set
1e3aa 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f  , then this opco
1e3ab 64 65 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e  de is part of an
1e3ac 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 72 61  .** UPDATE opera
1e3ad 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73 65  tion.  Otherwise
1e3ae 20 28 69 66 20 74 68 65 20 66 6c 61 67 20 69 73   (if the flag is
1e3af 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74 68 69   clear) then thi
1e3b0 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 70  s opcode.** is p
1e3b1 61 72 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54  art of an INSERT
1e3b2 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 68 65   operation.  The
1e3b3 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 6f   difference is o
1e3b4 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  nly important to
1e3b5 0a 2a 2a 20 74 68 65 20 75 70 64 61 74 65 20 68  .** the update h
1e3b6 6f 6f 6b 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d  ook..**.** Param
1e3b7 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69 6e  eter P4 may poin
1e3b8 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63 6f  t to a string co
1e3b9 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62  ntaining the tab
1e3ba 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d  le-name, or.** m
1e3bb 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20 69  ay be NULL. If i
1e3bc 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  t is not NULL, t
1e3bd 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d 68  hen the update-h
1e3be 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65 33  ook .** (sqlite3
1e3bf 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b  .xUpdateCallback
1e3c0 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f 6c  ) is invoked fol
1e3c1 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73 73  lowing a success
1e3c2 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a  ful insert..**.*
1e3c3 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a  * (WARNING/TODO:
1e3c4 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65 75   If P1 is a pseu
1e3c5 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50 32  do-cursor and P2
1e3c6 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a   is dynamically.
1e3c7 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68  ** allocated, th
1e3c8 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20  en ownership of 
1e3c9 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72 65  P2 is transferre
1e3ca 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d  d to the pseudo-
1e3cb 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 65  cursor.** and re
1e3cc 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d 65  gister P2 become
1e3cd 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49 66  s ephemeral.  If
1e3ce 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63   the cursor is c
1e3cf 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20 76  hanged, the.** v
1e3d0 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72  alue of register
1e3d1 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63 68   P2 will then ch
1e3d2 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72 65  ange.  Make sure
1e3d3 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a 2a   this does not.*
1e3d4 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f 62  * cause any prob
1e3d5 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69  lems.).**.** Thi
1e3d6 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e  s instruction on
1e3d7 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c  ly works on tabl
1e3d8 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c  es.  The equival
1e3d9 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a  ent instruction.
1e3da 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20 69  ** for indices i
1e3db 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e 0a  s OP_IdxInsert..
1e3dc 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e  */./* Opcode: In
1e3dd 73 65 72 74 49 6e 74 20 50 31 20 50 32 20 50 33  sertInt P1 P2 P3
1e3de 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69   P4 P5.**.** Thi
1e3df 73 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20  s works exactly 
1e3e0 6c 69 6b 65 20 4f 50 5f 49 6e 73 65 72 74 20 65  like OP_Insert e
1e3e1 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6b  xcept that the k
1e3e2 65 79 20 69 73 20 74 68 65 0a 2a 2a 20 69 6e 74  ey is the.** int
1e3e3 65 67 65 72 20 76 61 6c 75 65 20 50 33 2c 20 6e  eger value P3, n
1e3e4 6f 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ot the value of 
1e3e5 74 68 65 20 69 6e 74 65 67 65 72 20 73 74 6f 72  the integer stor
1e3e6 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1e3e7 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  3..*/.case OP_In
1e3e8 73 65 72 74 3a 20 0a 63 61 73 65 20 4f 50 5f 49  sert: .case OP_I
1e3e9 6e 73 65 72 74 49 6e 74 3a 20 7b 0a 23 69 66 20  nsertInt: {.#if 
1e3ea 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e3eb 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e3ec 20 75 2e 62 66 20 2a 2f 0a 20 20 4d 65 6d 20 2a   u.bf */.  Mem *
1e3ed 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20  pData;       /* 
1e3ee 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67  MEM cell holding
1e3ef 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 65   data for the re
1e3f0 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72  cord to be inser
1e3f1 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4b  ted */.  Mem *pK
1e3f2 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 45  ey;        /* ME
1e3f3 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b  M cell holding k
1e3f4 65 79 20 20 66 6f 72 20 74 68 65 20 72 65 63 6f  ey  for the reco
1e3f5 72 64 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79  rd */.  i64 iKey
1e3f6 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
1e3f7 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f   integer ROWID o
1e3f8 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65  r key for the re
1e3f9 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72  cord to be inser
1e3fa 74 65 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  ted */.  VdbeCur
1e3fb 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75  sor *pC;   /* Cu
1e3fc 72 73 6f 72 20 74 6f 20 74 61 62 6c 65 20 69 6e  rsor to table in
1e3fd 74 6f 20 77 68 69 63 68 20 69 6e 73 65 72 74 20  to which insert 
1e3fe 69 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  is written */.  
1e3ff 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20  int nZero;      
1e400 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a    /* Number of z
1e401 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 70  ero-bytes to app
1e402 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65  end */.  int see
1e403 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52 65  kResult;   /* Re
1e404 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 73 65  sult of prior se
1e405 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20 55 53  ek or 0 if no US
1e406 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67  ESEEKRESULT flag
1e407 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1e408 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74 61 62   *zDb;  /* datab
1e409 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 20  ase name - used 
1e40a 62 79 20 74 68 65 20 75 70 64 61 74 65 20 68 6f  by the update ho
1e40b 6f 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ok */.  const ch
1e40c 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 62  ar *zTbl; /* Tab
1e40d 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 62  le name - used b
1e40e 79 20 74 68 65 20 6f 70 64 61 74 65 20 68 6f 6f  y the opdate hoo
1e40f 6b 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20  k */.  int op;  
1e410 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f           /* Opco
1e411 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68 6f  de for update ho
1e412 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 54  ok: SQLITE_UPDAT
1e413 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 45  E or SQLITE_INSE
1e414 52 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  RT */.#endif /* 
1e415 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e416 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 20  moved into u.bf 
1e417 2a 2f 0a 0a 20 20 75 2e 62 66 2e 70 44 61 74 61  */..  u.bf.pData
1e418 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32   = &aMem[pOp->p2
1e419 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  ];.  assert( pOp
1e41a 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e41b 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e41c 0a 20 20 75 2e 62 66 2e 70 43 20 3d 20 70 2d 3e  .  u.bf.pC = p->
1e41d 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1e41e 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70    assert( u.bf.p
1e41f 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  C!=0 );.  assert
1e420 28 20 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73  ( u.bf.pC->pCurs
1e421 6f 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  or!=0 );.  asser
1e422 74 28 20 75 2e 62 66 2e 70 43 2d 3e 70 73 65 75  t( u.bf.pC->pseu
1e423 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b  doTableReg==0 );
1e424 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e  .  assert( u.bf.
1e425 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20  pC->isTable );. 
1e426 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1e427 70 4f 70 2d 3e 70 32 2c 20 75 2e 62 66 2e 70 44  pOp->p2, u.bf.pD
1e428 61 74 61 29 3b 0a 0a 20 20 69 66 28 20 70 4f 70  ata);..  if( pOp
1e429 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 73  ->opcode==OP_Ins
1e42a 65 72 74 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e  ert ){.    u.bf.
1e42b 70 4b 65 79 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pKey = &aMem[pOp
1e42c 2d 3e 70 33 5d 3b 0a 20 20 20 20 61 73 73 65 72  ->p3];.    asser
1e42d 74 28 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 66 6c  t( u.bf.pKey->fl
1e42e 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
1e42f 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52  .    REGISTER_TR
1e430 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62  ACE(pOp->p3, u.b
1e431 66 2e 70 4b 65 79 29 3b 0a 20 20 20 20 75 2e 62  f.pKey);.    u.b
1e432 66 2e 69 4b 65 79 20 3d 20 75 2e 62 66 2e 70 4b  f.iKey = u.bf.pK
1e433 65 79 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65  ey->u.i;.  }else
1e434 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  {.    assert( pO
1e435 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e  p->opcode==OP_In
1e436 73 65 72 74 49 6e 74 20 29 3b 0a 20 20 20 20 75  sertInt );.    u
1e437 2e 62 66 2e 69 4b 65 79 20 3d 20 70 4f 70 2d 3e  .bf.iKey = pOp->
1e438 70 33 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  p3;.  }..  if( p
1e439 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
1e43a 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68  NCHANGE ) p->nCh
1e43b 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 20 70 4f  ange++;.  if( pO
1e43c 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4c  p->p5 & OPFLAG_L
1e43d 41 53 54 52 4f 57 49 44 20 29 20 64 62 2d 3e 6c  ASTROWID ) db->l
1e43e 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 62 66 2e  astRowid = u.bf.
1e43f 69 4b 65 79 3b 0a 20 20 69 66 28 20 75 2e 62 66  iKey;.  if( u.bf
1e440 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20  .pData->flags & 
1e441 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1e442 75 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 20 3d 20  u.bf.pData->z = 
1e443 30 3b 0a 20 20 20 20 75 2e 62 66 2e 70 44 61 74  0;.    u.bf.pDat
1e444 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  a->n = 0;.  }els
1e445 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  e{.    assert( u
1e446 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73  .bf.pData->flags
1e447 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d   & (MEM_Blob|MEM
1e448 5f 53 74 72 29 20 29 3b 0a 20 20 7d 0a 20 20 75  _Str) );.  }.  u
1e449 2e 62 66 2e 73 65 65 6b 52 65 73 75 6c 74 20 3d  .bf.seekResult =
1e44a 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46   ((pOp->p5 & OPF
1e44b 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
1e44c 54 29 20 3f 20 75 2e 62 66 2e 70 43 2d 3e 73 65  T) ? u.bf.pC->se
1e44d 65 6b 52 65 73 75 6c 74 20 3a 20 30 29 3b 0a 20  ekResult : 0);. 
1e44e 20 69 66 28 20 75 2e 62 66 2e 70 44 61 74 61 2d   if( u.bf.pData-
1e44f 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72  >flags & MEM_Zer
1e450 6f 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a  o ){.    u.bf.nZ
1e451 65 72 6f 20 3d 20 75 2e 62 66 2e 70 44 61 74 61  ero = u.bf.pData
1e452 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c  ->u.nZero;.  }el
1e453 73 65 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65  se{.    u.bf.nZe
1e454 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  ro = 0;.  }.  sq
1e455 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
1e456 68 65 64 52 6f 77 69 64 28 75 2e 62 66 2e 70 43  hedRowid(u.bf.pC
1e457 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20  ->pCursor, 0);. 
1e458 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e459 65 65 49 6e 73 65 72 74 28 75 2e 62 66 2e 70 43  eeInsert(u.bf.pC
1e45a 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 75 2e  ->pCursor, 0, u.
1e45b 62 66 2e 69 4b 65 79 2c 0a 20 20 20 20 20 20 20  bf.iKey,.       
1e45c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e45d 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 7a     u.bf.pData->z
1e45e 2c 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 2c  , u.bf.pData->n,
1e45f 20 75 2e 62 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20   u.bf.nZero,.   
1e460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e461 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 35 20 26         pOp->p5 &
1e462 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 2c 20   OPFLAG_APPEND, 
1e463 75 2e 62 66 2e 73 65 65 6b 52 65 73 75 6c 74 0a  u.bf.seekResult.
1e464 20 20 29 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e    );.  u.bf.pC->
1e465 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1e466 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 64 65 66  ;.  u.bf.pC->def
1e467 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b  erredMoveto = 0;
1e468 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 63 61 63 68  .  u.bf.pC->cach
1e469 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1e46a 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76  STALE;..  /* Inv
1e46b 6f 6b 65 20 74 68 65 20 75 70 64 61 74 65 2d 68  oke the update-h
1e46c 6f 6f 6b 20 69 66 20 72 65 71 75 69 72 65 64 2e  ook if required.
1e46d 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
1e46e 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78  LITE_OK && db->x
1e46f 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26  UpdateCallback &
1e470 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20  & pOp->p4.z ){. 
1e471 20 20 20 75 2e 62 66 2e 7a 44 62 20 3d 20 64 62     u.bf.zDb = db
1e472 2d 3e 61 44 62 5b 75 2e 62 66 2e 70 43 2d 3e 69  ->aDb[u.bf.pC->i
1e473 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 75  Db].zName;.    u
1e474 2e 62 66 2e 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e  .bf.zTbl = pOp->
1e475 70 34 2e 7a 3b 0a 20 20 20 20 75 2e 62 66 2e 6f  p4.z;.    u.bf.o
1e476 70 20 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 20  p = ((pOp->p5 & 
1e477 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 29  OPFLAG_ISUPDATE)
1e478 20 3f 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45   ? SQLITE_UPDATE
1e479 20 3a 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54   : SQLITE_INSERT
1e47a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  );.    assert( u
1e47b 2e 62 66 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20  .bf.pC->isTable 
1e47c 29 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61  );.    db->xUpda
1e47d 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70  teCallback(db->p
1e47e 55 70 64 61 74 65 41 72 67 2c 20 75 2e 62 66 2e  UpdateArg, u.bf.
1e47f 6f 70 2c 20 75 2e 62 66 2e 7a 44 62 2c 20 75 2e  op, u.bf.zDb, u.
1e480 62 66 2e 7a 54 62 6c 2c 20 75 2e 62 66 2e 69 4b  bf.zTbl, u.bf.iK
1e481 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ey);.    assert(
1e482 20 75 2e 62 66 2e 70 43 2d 3e 69 44 62 3e 3d 30   u.bf.pC->iDb>=0
1e483 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   );.  }.  break;
1e484 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44  .}../* Opcode: D
1e485 65 6c 65 74 65 20 50 31 20 50 32 20 2a 20 50 34  elete P1 P2 * P4
1e486 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20   *.**.** Delete 
1e487 74 68 65 20 72 65 63 6f 72 64 20 61 74 20 77 68  the record at wh
1e488 69 63 68 20 74 68 65 20 50 31 20 63 75 72 73 6f  ich the P1 curso
1e489 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  r is currently p
1e48a 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  ointing..**.** T
1e48b 68 65 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 62  he cursor will b
1e48c 65 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  e left pointing 
1e48d 61 74 20 65 69 74 68 65 72 20 74 68 65 20 6e 65  at either the ne
1e48e 78 74 20 6f 72 20 74 68 65 20 70 72 65 76 69 6f  xt or the previo
1e48f 75 73 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 20  us.** record in 
1e490 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 69 74  the table. If it
1e491 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
1e492 67 20 61 74 20 74 68 65 20 6e 65 78 74 20 72 65  g at the next re
1e493 63 6f 72 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  cord, then.** th
1e494 65 20 6e 65 78 74 20 4e 65 78 74 20 69 6e 73 74  e next Next inst
1e495 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  ruction will be 
1e496 61 20 6e 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 20  a no-op.  Hence 
1e497 69 74 20 69 73 20 4f 4b 20 74 6f 20 64 65 6c 65  it is OK to dele
1e498 74 65 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20 66  te.** a record f
1e499 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 4e 65  rom within an Ne
1e49a 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49  xt loop..**.** I
1e49b 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48  f the OPFLAG_NCH
1e49c 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 32 20  ANGE flag of P2 
1e49d 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  is set, then the
1e49e 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e   row change coun
1e49f 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e  t is.** incremen
1e4a0 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e  ted (otherwise n
1e4a1 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75  ot)..**.** P1 mu
1e4a2 73 74 20 6e 6f 74 20 62 65 20 70 73 65 75 64 6f  st not be pseudo
1e4a3 2d 74 61 62 6c 65 2e 20 20 49 74 20 68 61 73 20  -table.  It has 
1e4a4 74 6f 20 62 65 20 61 20 72 65 61 6c 20 74 61 62  to be a real tab
1e4a5 6c 65 20 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 69  le with.** multi
1e4a6 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20  ple rows..**.** 
1e4a7 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c  If P4 is not NUL
1e4a8 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68  L, then it is th
1e4a9 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
1e4aa 62 6c 65 20 74 68 61 74 20 50 31 20 69 73 0a 2a  ble that P1 is.*
1e4ab 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20  * pointing to.  
1e4ac 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20  The update hook 
1e4ad 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c  will be invoked,
1e4ae 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a   if it exists..*
1e4af 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e  * If P4 is not N
1e4b0 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 50 31 20  ULL then the P1 
1e4b1 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65  cursor must have
1e4b2 20 62 65 65 6e 20 70 6f 73 69 74 69 6f 6e 65 64   been positioned
1e4b3 0a 2a 2a 20 75 73 69 6e 67 20 4f 50 5f 4e 6f 74  .** using OP_Not
1e4b4 46 6f 75 6e 64 20 70 72 69 6f 72 20 74 6f 20 69  Found prior to i
1e4b5 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 6f 70 63  nvoking this opc
1e4b6 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ode..*/.case OP_
1e4b7 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 20  Delete: {.#if 0 
1e4b8 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e4b9 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e4ba 2e 62 67 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65  .bg */.  i64 iKe
1e4bb 79 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  y;.  VdbeCursor 
1e4bc 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  *pC;.#endif /* l
1e4bd 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e4be 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a  oved into u.bg *
1e4bf 2f 0a 0a 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d  /..  u.bg.iKey =
1e4c0 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   0;.  assert( pO
1e4c1 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1e4c2 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1e4c3 3b 0a 20 20 75 2e 62 67 2e 70 43 20 3d 20 70 2d  ;.  u.bg.pC = p-
1e4c4 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1e4c5 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e  .  assert( u.bg.
1e4c6 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pC!=0 );.  asser
1e4c7 74 28 20 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72  t( u.bg.pC->pCur
1e4c8 73 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e  sor!=0 );  /* On
1e4c9 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61  ly valid for rea
1e4ca 6c 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65  l tables, no pse
1e4cb 75 64 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20  udotables */..  
1e4cc 2f 2a 20 49 66 20 74 68 65 20 75 70 64 61 74 65  /* If the update
1e4cd 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e  -hook will be in
1e4ce 76 6f 6b 65 64 2c 20 73 65 74 20 75 2e 62 67 2e  voked, set u.bg.
1e4cf 69 4b 65 79 20 74 6f 20 74 68 65 20 72 6f 77 69  iKey to the rowi
1e4d0 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f  d of the.  ** ro
1e4d1 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e  w being deleted.
1e4d2 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
1e4d3 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20  xUpdateCallback 
1e4d4 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a  && pOp->p4.z ){.
1e4d5 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 67      assert( u.bg
1e4d6 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a  .pC->isTable );.
1e4d7 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 67      assert( u.bg
1e4d8 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
1e4d9 64 20 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77  d );  /* lastRow
1e4da 69 64 20 73 65 74 20 62 79 20 70 72 65 76 69 6f  id set by previo
1e4db 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a  us OP_NotFound *
1e4dc 2f 0a 20 20 20 20 75 2e 62 67 2e 69 4b 65 79 20  /.    u.bg.iKey 
1e4dd 3d 20 75 2e 62 67 2e 70 43 2d 3e 6c 61 73 74 52  = u.bg.pC->lastR
1e4de 6f 77 69 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  owid;.  }..  /* 
1e4df 54 68 65 20 4f 50 5f 44 65 6c 65 74 65 20 6f 70  The OP_Delete op
1e4e0 63 6f 64 65 20 61 6c 77 61 79 73 20 66 6f 6c 6c  code always foll
1e4e1 6f 77 73 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69  ows an OP_NotExi
1e4e2 73 74 73 20 6f 72 20 4f 50 5f 4c 61 73 74 20 6f  sts or OP_Last o
1e4e3 72 0a 20 20 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e  r.  ** OP_Column
1e4e4 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62   on the same tab
1e4e5 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 79 20 69  le without any i
1e4e6 6e 74 65 72 76 65 6e 69 6e 67 20 6f 70 65 72 61  ntervening opera
1e4e7 74 69 6f 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20  tions that.  ** 
1e4e8 6d 69 67 68 74 20 6d 6f 76 65 20 6f 72 20 69 6e  might move or in
1e4e9 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72  validate the cur
1e4ea 73 6f 72 2e 20 20 48 65 6e 63 65 20 63 75 72 73  sor.  Hence curs
1e4eb 6f 72 20 75 2e 62 67 2e 70 43 20 69 73 20 61 6c  or u.bg.pC is al
1e4ec 77 61 79 73 20 70 6f 69 6e 74 69 6e 67 0a 20 20  ways pointing.  
1e4ed 2a 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 74 6f  ** to the row to
1e4ee 20 62 65 20 64 65 6c 65 74 65 64 20 61 6e 64 20   be deleted and 
1e4ef 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 43  the sqlite3VdbeC
1e4f0 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 6f 70  ursorMoveto() op
1e4f1 65 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c  eration.  ** bel
1e4f2 6f 77 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e  ow is always a n
1e4f3 6f 2d 6f 70 20 61 6e 64 20 63 61 6e 6e 6f 74 20  o-op and cannot 
1e4f4 66 61 69 6c 2e 20 20 57 65 20 77 69 6c 6c 20 72  fail.  We will r
1e4f5 75 6e 20 69 74 20 61 6e 79 68 6f 77 2c 20 74 68  un it anyhow, th
1e4f6 6f 75 67 68 2c 0a 20 20 2a 2a 20 74 6f 20 67 75  ough,.  ** to gu
1e4f7 61 72 64 20 61 67 61 69 6e 73 74 20 66 75 74 75  ard against futu
1e4f8 72 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  re changes to th
1e4f9 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72  e code generator
1e4fa 2e 0a 20 20 2a 2a 2f 0a 20 20 61 73 73 65 72 74  ..  **/.  assert
1e4fb 28 20 75 2e 62 67 2e 70 43 2d 3e 64 65 66 65 72  ( u.bg.pC->defer
1e4fc 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a  redMoveto==0 );.
1e4fd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1e4fe 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75  beCursorMoveto(u
1e4ff 2e 62 67 2e 70 43 29 3b 0a 20 20 69 66 28 20 4e  .bg.pC);.  if( N
1e500 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f  EVER(rc!=SQLITE_
1e501 4f 4b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  OK) ) goto abort
1e502 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a  _due_to_error;..
1e503 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
1e504 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62  tCachedRowid(u.b
1e505 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30  g.pC->pCursor, 0
1e506 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1e507 33 42 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62  3BtreeDelete(u.b
1e508 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a  g.pC->pCursor);.
1e509 20 20 75 2e 62 67 2e 70 43 2d 3e 63 61 63 68 65    u.bg.pC->cache
1e50a 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1e50b 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f  TALE;..  /* Invo
1e50c 6b 65 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f  ke the update-ho
1e50d 6f 6b 20 69 66 20 72 65 71 75 69 72 65 64 2e 20  ok if required. 
1e50e 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
1e50f 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55  ITE_OK && db->xU
1e510 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26  pdateCallback &&
1e511 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20   pOp->p4.z ){.  
1e512 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1e513 62 20 3d 20 64 62 2d 3e 61 44 62 5b 75 2e 62 67  b = db->aDb[u.bg
1e514 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b  .pC->iDb].zName;
1e515 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1e516 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e  *zTbl = pOp->p4.
1e517 7a 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61  z;.    db->xUpda
1e518 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70  teCallback(db->p
1e519 55 70 64 61 74 65 41 72 67 2c 20 53 51 4c 49 54  UpdateArg, SQLIT
1e51a 45 5f 44 45 4c 45 54 45 2c 20 7a 44 62 2c 20 7a  E_DELETE, zDb, z
1e51b 54 62 6c 2c 20 75 2e 62 67 2e 69 4b 65 79 29 3b  Tbl, u.bg.iKey);
1e51c 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1e51d 67 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a  g.pC->iDb>=0 );.
1e51e 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70    }.  if( pOp->p
1e51f 32 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e  2 & OPFLAG_NCHAN
1e520 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b  GE ) p->nChange+
1e521 2b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a  +;.  break;.}./*
1e522 20 4f 70 63 6f 64 65 3a 20 52 65 73 65 74 43 6f   Opcode: ResetCo
1e523 75 6e 74 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  unt * * * * *.**
1e524 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66  .** The value of
1e525 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e   the change coun
1e526 74 65 72 20 69 73 20 63 6f 70 69 65 64 20 74 6f  ter is copied to
1e527 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
1e528 6e 64 6c 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63  ndle.** change c
1e529 6f 75 6e 74 65 72 20 28 72 65 74 75 72 6e 65 64  ounter (returned
1e52a 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63   by subsequent c
1e52b 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  alls to sqlite3_
1e52c 63 68 61 6e 67 65 73 28 29 29 2e 0a 2a 2a 20 54  changes())..** T
1e52d 68 65 6e 20 74 68 65 20 56 4d 73 20 69 6e 74 65  hen the VMs inte
1e52e 72 6e 61 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e  rnal change coun
1e52f 74 65 72 20 72 65 73 65 74 73 20 74 6f 20 30 2e  ter resets to 0.
1e530 0a 2a 2a 20 54 68 69 73 20 69 73 20 75 73 65 64  .** This is used
1e531 20 62 79 20 74 72 69 67 67 65 72 20 70 72 6f 67   by trigger prog
1e532 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  rams..*/.case OP
1e533 5f 52 65 73 65 74 43 6f 75 6e 74 3a 20 7b 0a 20  _ResetCount: {. 
1e534 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
1e535 68 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43  hanges(db, p->nC
1e536 68 61 6e 67 65 29 3b 0a 20 20 70 2d 3e 6e 43 68  hange);.  p->nCh
1e537 61 6e 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 61  ange = 0;.  brea
1e538 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e539 20 52 6f 77 44 61 74 61 20 50 31 20 50 32 20 2a   RowData P1 P2 *
1e53a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65   * *.**.** Write
1e53b 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1e53c 32 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72  2 the complete r
1e53d 6f 77 20 64 61 74 61 20 66 6f 72 20 63 75 72 73  ow data for curs
1e53e 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20  or P1..** There 
1e53f 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61  is no interpreta
1e540 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61  tion of the data
1e541 2e 20 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73  .  .** It is jus
1e542 74 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68  t copied onto th
1e543 65 20 50 32 20 72 65 67 69 73 74 65 72 20 65 78  e P2 register ex
1e544 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20  actly as .** it 
1e545 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20  is found in the 
1e546 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1e547 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63  *.** If the P1 c
1e548 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f  ursor must be po
1e549 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69  inting to a vali
1e54a 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c  d row (not a NUL
1e54b 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72  L row).** of a r
1e54c 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61  eal table, not a
1e54d 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a   pseudo-table..*
1e54e 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77  /./* Opcode: Row
1e54f 4b 65 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Key P1 P2 * * *.
1e550 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f  **.** Write into
1e551 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68 65   register P2 the
1e552 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65   complete row ke
1e553 79 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e  y for cursor P1.
1e554 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20  .** There is no 
1e555 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f  interpretation o
1e556 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a  f the data.  .**
1e557 20 54 68 65 20 6b 65 79 20 69 73 20 63 6f 70 69   The key is copi
1e558 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 72  ed onto the P3 r
1e559 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 20  egister exactly 
1e55a 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75  as .** it is fou
1e55b 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nd in the databa
1e55c 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  se file..**.** I
1e55d 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20  f the P1 cursor 
1e55e 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67  must be pointing
1e55f 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20   to a valid row 
1e560 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29  (not a NULL row)
1e561 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61  .** of a real ta
1e562 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64  ble, not a pseud
1e563 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65  o-table..*/.case
1e564 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65   OP_RowKey:.case
1e565 20 4f 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 23   OP_RowData: {.#
1e566 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1e567 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e568 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a 20 20 56 64  nto u.bh */.  Vd
1e569 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1e56a 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1e56b 0a 20 20 75 33 32 20 6e 3b 0a 20 20 69 36 34 20  .  u32 n;.  i64 
1e56c 6e 36 34 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  n64;.#endif /* l
1e56d 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e56e 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a  oved into u.bh *
1e56f 2f 0a 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  /..  pOut = &aMe
1e570 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f  m[pOp->p2];..  /
1e571 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b  * Note that RowK
1e572 65 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61  ey and RowData a
1e573 72 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c  re really exactl
1e574 79 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72  y the same instr
1e575 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65  uction */.  asse
1e576 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e577 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e578 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 68 2e 70  rsor );.  u.bh.p
1e579 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e57a 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e57b 20 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c   u.bh.pC->isTabl
1e57c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  e || pOp->opcode
1e57d 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20  ==OP_RowKey );. 
1e57e 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43   assert( u.bh.pC
1e57f 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70  ->isIndex || pOp
1e580 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77  ->opcode==OP_Row
1e581 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74  Data );.  assert
1e582 28 20 75 2e 62 68 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bh.pC!=0 );.
1e583 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70    assert( u.bh.p
1e584 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b  C->nullRow==0 );
1e585 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e  .  assert( u.bh.
1e586 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52  pC->pseudoTableR
1e587 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  eg==0 );.  asser
1e588 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72  t( u.bh.pC->pCur
1e589 73 6f 72 21 3d 30 20 29 3b 0a 20 20 75 2e 62 68  sor!=0 );.  u.bh
1e58a 2e 70 43 72 73 72 20 3d 20 75 2e 62 68 2e 70 43  .pCrsr = u.bh.pC
1e58b 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73  ->pCursor;.  ass
1e58c 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1e58d 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75  eCursorIsValid(u
1e58e 2e 62 68 2e 70 43 72 73 72 29 20 29 3b 0a 0a 20  .bh.pCrsr) );.. 
1e58f 20 2f 2a 20 54 68 65 20 4f 50 5f 52 6f 77 4b 65   /* The OP_RowKe
1e590 79 20 61 6e 64 20 4f 50 5f 52 6f 77 44 61 74 61  y and OP_RowData
1e591 20 6f 70 63 6f 64 65 73 20 61 6c 77 61 79 73 20   opcodes always 
1e592 66 6f 6c 6c 6f 77 20 4f 50 5f 4e 6f 74 45 78 69  follow OP_NotExi
1e593 73 74 73 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f 52  sts or.  ** OP_R
1e594 65 77 69 6e 64 2f 4f 70 5f 4e 65 78 74 20 77 69  ewind/Op_Next wi
1e595 74 68 20 6e 6f 20 69 6e 74 65 72 76 65 6e 69 6e  th no intervenin
1e596 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74  g instructions t
1e597 68 61 74 20 6d 69 67 68 74 20 69 6e 76 61 6c 69  hat might invali
1e598 64 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 63 75  date.  ** the cu
1e599 72 73 6f 72 2e 20 20 48 65 6e 63 65 20 74 68 65  rsor.  Hence the
1e59a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74   following sqlit
1e59b 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
1e59c 74 6f 28 29 20 63 61 6c 6c 20 69 73 20 61 6c 77  to() call is alw
1e59d 61 79 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70  ays.  ** a no-op
1e59e 20 61 6e 64 20 63 61 6e 20 6e 65 76 65 72 20 66   and can never f
1e59f 61 69 6c 2e 20 20 42 75 74 20 77 65 20 6c 65 61  ail.  But we lea
1e5a0 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 61  ve it in place a
1e5a1 73 20 61 20 73 61 66 65 74 79 2e 0a 20 20 2a 2f  s a safety..  */
1e5a2 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e  .  assert( u.bh.
1e5a3 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1e5a4 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20  to==0 );.  rc = 
1e5a5 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1e5a6 72 4d 6f 76 65 74 6f 28 75 2e 62 68 2e 70 43 29  rMoveto(u.bh.pC)
1e5a7 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63  ;.  if( NEVER(rc
1e5a8 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67  !=SQLITE_OK) ) g
1e5a9 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1e5aa 5f 65 72 72 6f 72 3b 0a 0a 20 20 69 66 28 20 75  _error;..  if( u
1e5ab 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20  .bh.pC->isIndex 
1e5ac 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
1e5ad 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bh.pC->isTable
1e5ae 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
1e5af 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
1e5b0 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 2e  (u.bh.pCrsr, &u.
1e5b1 62 68 2e 6e 36 34 29 3b 0a 20 20 20 20 61 73 73  bh.n64);.    ass
1e5b2 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1e5b3 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75 65  OK );    /* True
1e5b4 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73   because of Curs
1e5b5 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20  orMoveto() call 
1e5b6 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 69 66 28  above */.    if(
1e5b7 20 75 2e 62 68 2e 6e 36 34 3e 64 62 2d 3e 61 4c   u.bh.n64>db->aL
1e5b8 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1e5b9 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1e5ba 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1e5bb 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 68 2e  .    }.    u.bh.
1e5bc 6e 20 3d 20 28 75 33 32 29 75 2e 62 68 2e 6e 36  n = (u32)u.bh.n6
1e5bd 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  4;.  }else{.    
1e5be 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e5bf 65 44 61 74 61 53 69 7a 65 28 75 2e 62 68 2e 70  eDataSize(u.bh.p
1e5c0 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 29 3b 0a  Crsr, &u.bh.n);.
1e5c1 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
1e5c2 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20  SQLITE_OK );    
1e5c3 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61  /* DataSize() ca
1e5c4 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20  nnot fail */.   
1e5c5 20 69 66 28 20 75 2e 62 68 2e 6e 3e 28 75 33 32   if( u.bh.n>(u32
1e5c6 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  )db->aLimit[SQLI
1e5c7 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
1e5c8 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74   ){.      goto t
1e5c9 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20  oo_big;.    }.  
1e5ca 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  }.  if( sqlite3V
1e5cb 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c  dbeMemGrow(pOut,
1e5cc 20 75 2e 62 68 2e 6e 2c 20 30 29 20 29 7b 0a 20   u.bh.n, 0) ){. 
1e5cd 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
1e5ce 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20    }.  pOut->n = 
1e5cf 75 2e 62 68 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74  u.bh.n;.  MemSet
1e5d0 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1e5d1 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20  EM_Blob);.  if( 
1e5d2 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78  u.bh.pC->isIndex
1e5d3 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1e5d4 69 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e 62  ite3BtreeKey(u.b
1e5d5 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68  h.pCrsr, 0, u.bh
1e5d6 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20  .n, pOut->z);.  
1e5d7 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1e5d8 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
1e5d9 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 30 2c 20  (u.bh.pCrsr, 0, 
1e5da 75 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29  u.bh.n, pOut->z)
1e5db 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e  ;.  }.  pOut->en
1e5dc 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
1e5dd 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65    /* In case the
1e5de 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61   blob is ever ca
1e5df 73 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20  st to text */.  
1e5e0 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1e5e1 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65  IZE(pOut);.  bre
1e5e2 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e5e3 3a 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20  : Rowid P1 P2 * 
1e5e4 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20  * *.**.** Store 
1e5e5 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61  in register P2 a
1e5e6 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
1e5e7 69 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68  is the key of th
1e5e8 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 68  e table entry th
1e5e9 61 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72 72  at.** P1 is curr
1e5ea 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e 0a  ently point to..
1e5eb 2a 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65 20  **.** P1 can be 
1e5ec 65 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e 61  either an ordina
1e5ed 72 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69  ry table or a vi
1e5ee 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
1e5ef 65 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62  ere used to.** b
1e5f0 65 20 61 20 73 65 70 61 72 61 74 65 20 4f 50 5f  e a separate OP_
1e5f1 56 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66 6f  VRowid opcode fo
1e5f2 72 20 75 73 65 20 77 69 74 68 20 76 69 72 74 75  r use with virtu
1e5f3 61 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20 74  al tables, but t
1e5f4 68 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64  his.** one opcod
1e5f5 65 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72 20  e now works for 
1e5f6 62 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65 73  both table types
1e5f7 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77  ..*/.case OP_Row
1e5f8 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  id: {           
1e5f9 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1e5fa 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1e5fb 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e5fc 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e5fd 20 75 2e 62 69 20 2a 2f 0a 20 20 56 64 62 65 43   u.bi */.  VdbeC
1e5fe 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34  ursor *pC;.  i64
1e5ff 20 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   v;.  sqlite3_vt
1e600 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e  ab *pVtab;.  con
1e601 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
1e602 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64  e *pModule;.#end
1e603 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e604 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e605 20 75 2e 62 69 20 2a 2f 0a 0a 20 20 61 73 73 65   u.bi */..  asse
1e606 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e607 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e608 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 69 2e 70  rsor );.  u.bi.p
1e609 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e60a 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e60b 20 75 2e 62 69 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bi.pC!=0 );. 
1e60c 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43   assert( u.bi.pC
1e60d 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67  ->pseudoTableReg
1e60e 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62  ==0 );.  if( u.b
1e60f 69 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b  i.pC->nullRow ){
1e610 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
1e611 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1e612 20 62 72 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 20   break;.  }else 
1e613 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 64 65 66  if( u.bi.pC->def
1e614 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20  erredMoveto ){. 
1e615 20 20 20 75 2e 62 69 2e 76 20 3d 20 75 2e 62 69     u.bi.v = u.bi
1e616 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65  .pC->movetoTarge
1e617 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  t;.#ifndef SQLIT
1e618 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1e619 42 4c 45 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  BLE.  }else if( 
1e61a 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75  u.bi.pC->pVtabCu
1e61b 72 73 6f 72 20 29 7b 0a 20 20 20 20 75 2e 62 69  rsor ){.    u.bi
1e61c 2e 70 56 74 61 62 20 3d 20 75 2e 62 69 2e 70 43  .pVtab = u.bi.pC
1e61d 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  ->pVtabCursor->p
1e61e 56 74 61 62 3b 0a 20 20 20 20 75 2e 62 69 2e 70  Vtab;.    u.bi.p
1e61f 4d 6f 64 75 6c 65 20 3d 20 75 2e 62 69 2e 70 56  Module = u.bi.pV
1e620 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  tab->pModule;.  
1e621 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70    assert( u.bi.p
1e622 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29  Module->xRowid )
1e623 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1e624 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1e625 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1e626 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 72  to_misuse;.    r
1e627 63 20 3d 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65  c = u.bi.pModule
1e628 2d 3e 78 52 6f 77 69 64 28 75 2e 62 69 2e 70 43  ->xRowid(u.bi.pC
1e629 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26  ->pVtabCursor, &
1e62a 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 73 71 6c  u.bi.v);.    sql
1e62b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1e62c 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
1e62d 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 62  p->zErrMsg = u.b
1e62e 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  i.pVtab->zErrMsg
1e62f 3b 0a 20 20 20 20 75 2e 62 69 2e 70 56 74 61 62  ;.    u.bi.pVtab
1e630 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1e631 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1e632 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1e633 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1e634 69 73 75 73 65 3b 0a 23 65 6e 64 69 66 20 2f 2a  isuse;.#endif /*
1e635 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1e636 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 20 20 7d  TUALTABLE */.  }
1e637 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
1e638 28 20 75 2e 62 69 2e 70 43 2d 3e 70 43 75 72 73  ( u.bi.pC->pCurs
1e639 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20  or!=0 );.    rc 
1e63a 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
1e63b 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 69 2e 70  sorMoveto(u.bi.p
1e63c 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  C);.    if( rc )
1e63d 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1e63e 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 69 66  to_error;.    if
1e63f 28 20 75 2e 62 69 2e 70 43 2d 3e 72 6f 77 69 64  ( u.bi.pC->rowid
1e640 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 20  IsValid ){.     
1e641 20 75 2e 62 69 2e 76 20 3d 20 75 2e 62 69 2e 70   u.bi.v = u.bi.p
1e642 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20  C->lastRowid;.  
1e643 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
1e644 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e645 4b 65 79 53 69 7a 65 28 75 2e 62 69 2e 70 43 2d  KeySize(u.bi.pC-
1e646 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 69 2e  >pCursor, &u.bi.
1e647 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  v);.      assert
1e648 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1e649 29 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 73 6f  );  /* Always so
1e64a 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73   because of Curs
1e64b 6f 72 4d 6f 76 65 74 6f 28 29 20 61 62 6f 76 65  orMoveto() above
1e64c 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   */.    }.  }.  
1e64d 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 69  pOut->u.i = u.bi
1e64e 2e 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  .v;.  break;.}..
1e64f 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 52  /* Opcode: NullR
1e650 6f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  ow P1 * * * *.**
1e651 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72  .** Move the cur
1e652 73 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c 6c  sor P1 to a null
1e653 20 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 6f   row.  Any OP_Co
1e654 6c 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 0a  lumn operations.
1e655 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20 77 68  ** that occur wh
1e656 69 6c 65 20 74 68 65 20 63 75 72 73 6f 72 20 69  ile the cursor i
1e657 73 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 6f  s on the null ro
1e658 77 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a  w will always.**
1e659 20 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a   write a NULL..*
1e65a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f  /.case OP_NullRo
1e65b 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  w: {.#if 0  /* l
1e65c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e65d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20 2a  oved into u.bj *
1e65e 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e65f 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  pC;.#endif /* lo
1e660 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e661 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f  ved into u.bj */
1e662 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1e663 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e664 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1e665 20 20 75 2e 62 6a 2e 70 43 20 3d 20 70 2d 3e 61    u.bj.pC = p->a
1e666 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
1e667 20 61 73 73 65 72 74 28 20 75 2e 62 6a 2e 70 43   assert( u.bj.pC
1e668 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 43  !=0 );.  u.bj.pC
1e669 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20  ->nullRow = 1;. 
1e66a 20 75 2e 62 6a 2e 70 43 2d 3e 72 6f 77 69 64 49   u.bj.pC->rowidI
1e66b 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 69 66  sValid = 0;.  if
1e66c 28 20 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73  ( u.bj.pC->pCurs
1e66d 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  or ){.    sqlite
1e66e 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f  3BtreeClearCurso
1e66f 72 28 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73  r(u.bj.pC->pCurs
1e670 6f 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  or);.  }.  break
1e671 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1e672 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Last P1 P2 * * *
1e673 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  .**.** The next 
1e674 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64  use of the Rowid
1e675 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65   or Column or Ne
1e676 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66  xt instruction f
1e677 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72  or P1 .** will r
1e678 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74  efer to the last
1e679 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
1e67a 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
1e67b 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65  index..** If the
1e67c 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1e67d 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e  is empty and P2>
1e67e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  0, then jump imm
1e67f 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
1e680 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72  ** If P2 is 0 or
1e681 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   if the table or
1e682 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d   index is not em
1e683 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  pty, fall throug
1e684 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
1e685 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
1e686 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61  n..*/.case OP_La
1e687 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  st: {        /* 
1e688 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f  jump */.#if 0  /
1e689 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e68a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e68b 6b 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  k */.  VdbeCurso
1e68c 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f  r *pC;.  BtCurso
1e68d 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20  r *pCrsr;.  int 
1e68e 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  res;.#endif /* l
1e68f 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e690 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20 2a  oved into u.bk *
1e691 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
1e692 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e693 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e694 0a 20 20 75 2e 62 6b 2e 70 43 20 3d 20 70 2d 3e  .  u.bk.pC = p->
1e695 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1e696 20 20 61 73 73 65 72 74 28 20 75 2e 62 6b 2e 70    assert( u.bk.p
1e697 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6b 2e 70  C!=0 );.  u.bk.p
1e698 43 72 73 72 20 3d 20 75 2e 62 6b 2e 70 43 2d 3e  Crsr = u.bk.pC->
1e699 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75  pCursor;.  if( u
1e69a 2e 62 6b 2e 70 43 72 73 72 3d 3d 30 20 29 7b 0a  .bk.pCrsr==0 ){.
1e69b 20 20 20 20 75 2e 62 6b 2e 72 65 73 20 3d 20 31      u.bk.res = 1
1e69c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
1e69d 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e69e 4c 61 73 74 28 75 2e 62 6b 2e 70 43 72 73 72 2c  Last(u.bk.pCrsr,
1e69f 20 26 75 2e 62 6b 2e 72 65 73 29 3b 0a 20 20 7d   &u.bk.res);.  }
1e6a0 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 6e 75 6c 6c  .  u.bk.pC->null
1e6a1 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 6b 2e 72  Row = (u8)u.bk.r
1e6a2 65 73 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 64  es;.  u.bk.pC->d
1e6a3 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1e6a4 30 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 72 6f  0;.  u.bk.pC->ro
1e6a5 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1e6a6 20 20 75 2e 62 6b 2e 70 43 2d 3e 63 61 63 68 65    u.bk.pC->cache
1e6a7 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1e6a8 54 41 4c 45 3b 0a 20 20 69 66 28 20 70 4f 70 2d  TALE;.  if( pOp-
1e6a9 3e 70 32 3e 30 20 26 26 20 75 2e 62 6b 2e 72 65  >p2>0 && u.bk.re
1e6aa 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  s ){.    pc = pO
1e6ab 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p2 - 1;.  }. 
1e6ac 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f   break;.}.../* O
1e6ad 70 63 6f 64 65 3a 20 53 6f 72 74 20 50 31 20 50  pcode: Sort P1 P
1e6ae 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  2 * * *.**.** Th
1e6af 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 65  is opcode does e
1e6b0 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20  xactly the same 
1e6b1 74 68 69 6e 67 20 61 73 20 4f 50 5f 52 65 77 69  thing as OP_Rewi
1e6b2 6e 64 20 65 78 63 65 70 74 20 74 68 61 74 0a 2a  nd except that.*
1e6b3 2a 20 69 74 20 69 6e 63 72 65 6d 65 6e 74 73 20  * it increments 
1e6b4 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20  an undocumented 
1e6b5 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1e6b6 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
1e6b7 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 74 69 6e 67 20  ..**.** Sorting 
1e6b8 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 65 64 20  is accomplished 
1e6b9 62 79 20 77 72 69 74 69 6e 67 20 72 65 63 6f 72  by writing recor
1e6ba 64 73 20 69 6e 74 6f 20 61 20 73 6f 72 74 69 6e  ds into a sortin
1e6bb 67 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 6e  g index,.** then
1e6bc 20 72 65 77 69 6e 64 69 6e 67 20 74 68 61 74 20   rewinding that 
1e6bd 69 6e 64 65 78 20 61 6e 64 20 70 6c 61 79 69 6e  index and playin
1e6be 67 20 69 74 20 62 61 63 6b 20 66 72 6f 6d 20 62  g it back from b
1e6bf 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65  eginning to.** e
1e6c0 6e 64 2e 20 20 57 65 20 75 73 65 20 74 68 65 20  nd.  We use the 
1e6c1 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 20 69  OP_Sort opcode i
1e6c2 6e 73 74 65 61 64 20 6f 66 20 4f 50 5f 52 65 77  nstead of OP_Rew
1e6c3 69 6e 64 20 74 6f 20 64 6f 20 74 68 65 0a 2a 2a  ind to do the.**
1e6c4 20 72 65 77 69 6e 64 69 6e 67 20 73 6f 20 74 68   rewinding so th
1e6c5 61 74 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61  at the global va
1e6c6 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 65 20 69  riable will be i
1e6c7 6e 63 72 65 6d 65 6e 74 65 64 20 61 6e 64 0a 2a  ncremented and.*
1e6c8 2a 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73  * regression tes
1e6c9 74 73 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65  ts can determine
1e6ca 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1e6cb 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 69 73  the optimizer is
1e6cc 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 6f 70  .** correctly op
1e6cd 74 69 6d 69 7a 69 6e 67 20 6f 75 74 20 73 6f 72  timizing out sor
1e6ce 74 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  ts..*/.case OP_S
1e6cf 6f 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  ort: {        /*
1e6d0 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 64 65 66 20   jump */.#ifdef 
1e6d1 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71  SQLITE_TEST.  sq
1e6d2 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e 74  lite3_sort_count
1e6d3 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 65  ++;.  sqlite3_se
1e6d4 61 72 63 68 5f 63 6f 75 6e 74 2d 2d 3b 0a 23 65  arch_count--;.#e
1e6d5 6e 64 69 66 0a 20 20 70 2d 3e 61 43 6f 75 6e 74  ndif.  p->aCount
1e6d6 65 72 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54  er[SQLITE_STMTST
1e6d7 41 54 55 53 5f 53 4f 52 54 2d 31 5d 2b 2b 3b 0a  ATUS_SORT-1]++;.
1e6d8 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
1e6d9 68 20 69 6e 74 6f 20 4f 50 5f 52 65 77 69 6e 64  h into OP_Rewind
1e6da 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a   */.}./* Opcode:
1e6db 20 52 65 77 69 6e 64 20 50 31 20 50 32 20 2a 20   Rewind P1 P2 * 
1e6dc 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  * *.**.** The ne
1e6dd 78 74 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f  xt use of the Ro
1e6de 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72  wid or Column or
1e6df 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   Next instructio
1e6e0 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c  n for P1 .** wil
1e6e1 6c 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 66  l refer to the f
1e6e2 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  irst entry in th
1e6e3 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  e database table
1e6e4 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66   or index..** If
1e6e5 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
1e6e6 64 65 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64  dex is empty and
1e6e7 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70   P2>0, then jump
1e6e8 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
1e6e9 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20 69 73 20  P2..** If P2 is 
1e6ea 30 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c  0 or if the tabl
1e6eb 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 6e 6f  e or index is no
1e6ec 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68  t empty, fall th
1e6ed 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20  rough.** to the 
1e6ee 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75  following instru
1e6ef 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ction..*/.case O
1e6f0 50 5f 52 65 77 69 6e 64 3a 20 7b 20 20 20 20 20  P_Rewind: {     
1e6f1 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1e6f2 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e6f3 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e6f4 74 6f 20 75 2e 62 6c 20 2a 2f 0a 20 20 56 64 62  to u.bl */.  Vdb
1e6f5 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1e6f6 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1e6f7 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69    int res;.#endi
1e6f8 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e6f9 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e6fa 75 2e 62 6c 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bl */..  asser
1e6fb 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e6fc 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e6fd 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6c 2e 70 43  sor );.  u.bl.pC
1e6fe 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1e6ff 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1e700 75 2e 62 6c 2e 70 43 21 3d 30 20 29 3b 0a 20 20  u.bl.pC!=0 );.  
1e701 69 66 28 20 28 75 2e 62 6c 2e 70 43 72 73 72 20  if( (u.bl.pCrsr 
1e702 3d 20 75 2e 62 6c 2e 70 43 2d 3e 70 43 75 72 73  = u.bl.pC->pCurs
1e703 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63  or)!=0 ){.    rc
1e704 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46   = sqlite3BtreeF
1e705 69 72 73 74 28 75 2e 62 6c 2e 70 43 72 73 72 2c  irst(u.bl.pCrsr,
1e706 20 26 75 2e 62 6c 2e 72 65 73 29 3b 0a 20 20 20   &u.bl.res);.   
1e707 20 75 2e 62 6c 2e 70 43 2d 3e 61 74 46 69 72 73   u.bl.pC->atFirs
1e708 74 20 3d 20 75 2e 62 6c 2e 72 65 73 3d 3d 30 20  t = u.bl.res==0 
1e709 3f 31 3a 30 3b 0a 20 20 20 20 75 2e 62 6c 2e 70  ?1:0;.    u.bl.p
1e70a 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e70b 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 6c 2e  o = 0;.    u.bl.
1e70c 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1e70d 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1e70e 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 72 6f 77 69     u.bl.pC->rowi
1e70f 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1e710 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 6c 2e  }else{.    u.bl.
1e711 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 75  res = 1;.  }.  u
1e712 2e 62 6c 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .bl.pC->nullRow 
1e713 3d 20 28 75 38 29 75 2e 62 6c 2e 72 65 73 3b 0a  = (u8)u.bl.res;.
1e714 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e715 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 70  2>0 && pOp->p2<p
1e716 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 75  ->nOp );.  if( u
1e717 2e 62 6c 2e 72 65 73 20 29 7b 0a 20 20 20 20 70  .bl.res ){.    p
1e718 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1e719 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e71a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74  ./* Opcode: Next
1e71b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1e71c 2a 2a 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f  ** Advance curso
1e71d 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20  r P1 so that it 
1e71e 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65  points to the ne
1e71f 78 74 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72  xt key/data pair
1e720 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65   in its.** table
1e721 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74   or index.  If t
1e722 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65  here are no more
1e723 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73   key/value pairs
1e724 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75   then fall throu
1e725 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c  gh.** to the fol
1e726 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69  lowing instructi
1e727 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20  on.  But if the 
1e728 63 75 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77  cursor advance w
1e729 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a  as successful,.*
1e72a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  * jump immediate
1e72b 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ly to P2..**.** 
1e72c 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75  The P1 cursor mu
1e72d 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c  st be for a real
1e72e 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73   table, not a ps
1e72f 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  eudo-table..**.*
1e730 2a 20 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76  * See also: Prev
1e731 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50  .*/./* Opcode: P
1e732 72 65 76 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  rev P1 P2 * * *.
1e733 2a 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75  **.** Back up cu
1e734 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20  rsor P1 so that 
1e735 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
1e736 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61   previous key/da
1e737 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a  ta pair in its.*
1e738 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  * table or index
1e739 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  .  If there is n
1e73a 6f 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76  o previous key/v
1e73b 61 6c 75 65 20 70 61 69 72 73 20 74 68 65 6e 20  alue pairs then 
1e73c 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20  fall through.** 
1e73d 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
1e73e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42   instruction.  B
1e73f 75 74 20 69 66 20 74 68 65 20 63 75 72 73 6f 72  ut if the cursor
1e740 20 62 61 63 6b 75 70 20 77 61 73 20 73 75 63 63   backup was succ
1e741 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20  essful,.** jump 
1e742 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
1e743 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20  2..**.** The P1 
1e744 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66  cursor must be f
1e745 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c  or a real table,
1e746 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61   not a pseudo-ta
1e747 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ble..*/.case OP_
1e748 50 72 65 76 3a 20 20 20 20 20 20 20 20 20 20 2f  Prev:          /
1e749 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f  * jump */.case O
1e74a 50 5f 4e 65 78 74 3a 20 7b 20 20 20 20 20 20 20  P_Next: {       
1e74b 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20   /* jump */.#if 
1e74c 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e74d 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e74e 20 75 2e 62 6d 20 2a 2f 0a 20 20 56 64 62 65 43   u.bm */.  VdbeC
1e74f 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43  ursor *pC;.  BtC
1e750 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1e751 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20  int res;.#endif 
1e752 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e753 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e754 62 6d 20 2a 2f 0a 0a 20 20 43 48 45 43 4b 5f 46  bm */..  CHECK_F
1e755 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  OR_INTERRUPT;.  
1e756 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1e757 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
1e758 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e  >nCursor );.  u.
1e759 62 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  bm.pC = p->apCsr
1e75a 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28  [pOp->p1];.  if(
1e75b 20 75 2e 62 6d 2e 70 43 3d 3d 30 20 29 7b 0a 20   u.bm.pC==0 ){. 
1e75c 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 65     break;  /* Se
1e75d 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 20 2a  e ticket #2273 *
1e75e 2f 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 72  /.  }.  u.bm.pCr
1e75f 73 72 20 3d 20 75 2e 62 6d 2e 70 43 2d 3e 70 43  sr = u.bm.pC->pC
1e760 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62  ursor;.  if( u.b
1e761 6d 2e 70 43 72 73 72 3d 3d 30 20 29 7b 0a 20 20  m.pCrsr==0 ){.  
1e762 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52    u.bm.pC->nullR
1e763 6f 77 20 3d 20 31 3b 0a 20 20 20 20 62 72 65 61  ow = 1;.    brea
1e764 6b 3b 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 72 65  k;.  }.  u.bm.re
1e765 73 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28  s = 1;.  assert(
1e766 20 75 2e 62 6d 2e 70 43 2d 3e 64 65 66 65 72 72   u.bm.pC->deferr
1e767 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20  edMoveto==0 );. 
1e768 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64   rc = pOp->opcod
1e769 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c  e==OP_Next ? sql
1e76a 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 75 2e  ite3BtreeNext(u.
1e76b 62 6d 2e 70 43 72 73 72 2c 20 26 75 2e 62 6d 2e  bm.pCrsr, &u.bm.
1e76c 72 65 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20  res) :.         
1e76d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e76e 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1e76f 65 50 72 65 76 69 6f 75 73 28 75 2e 62 6d 2e 70  ePrevious(u.bm.p
1e770 43 72 73 72 2c 20 26 75 2e 62 6d 2e 72 65 73 29  Crsr, &u.bm.res)
1e771 3b 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c  ;.  u.bm.pC->nul
1e772 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 6d 2e  lRow = (u8)u.bm.
1e773 72 65 73 3b 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e  res;.  u.bm.pC->
1e774 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1e775 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28  CHE_STALE;.  if(
1e776 20 75 2e 62 6d 2e 72 65 73 3d 3d 30 20 29 7b 0a   u.bm.res==0 ){.
1e777 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1e778 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20 70 4f   - 1;.    if( pO
1e779 70 2d 3e 70 35 20 29 20 70 2d 3e 61 43 6f 75 6e  p->p5 ) p->aCoun
1e77a 74 65 72 5b 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b  ter[pOp->p5-1]++
1e77b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
1e77c 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33  TEST.    sqlite3
1e77d 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b  _search_count++;
1e77e 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 75 2e  .#endif.  }.  u.
1e77f 62 6d 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  bm.pC->rowidIsVa
1e780 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b  lid = 0;.  break
1e781 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1e782 49 64 78 49 6e 73 65 72 74 20 50 31 20 50 32 20  IdxInsert P1 P2 
1e783 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65  P3 * P5.**.** Re
1e784 67 69 73 74 65 72 20 50 32 20 68 6f 6c 64 73 20  gister P2 holds 
1e785 61 20 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20  a SQL index key 
1e786 6d 61 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a  made using the.*
1e787 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73  * MakeRecord ins
1e788 74 72 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  tructions.  This
1e789 20 6f 70 63 6f 64 65 20 77 72 69 74 65 73 20 74   opcode writes t
1e78a 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20  hat key.** into 
1e78b 74 68 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44  the index P1.  D
1e78c 61 74 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72  ata for the entr
1e78d 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20  y is nil..**.** 
1e78e 50 33 20 69 73 20 61 20 66 6c 61 67 20 74 68 61  P3 is a flag tha
1e78f 74 20 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e  t provides a hin
1e790 74 20 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20  t to the b-tree 
1e791 6c 61 79 65 72 20 74 68 61 74 20 74 68 69 73 0a  layer that this.
1e792 2a 2a 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b  ** insert is lik
1e793 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70  ely to be an app
1e794 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  end..**.** This 
1e795 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79  instruction only
1e796 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63   works for indic
1e797 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c  es.  The equival
1e798 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a  ent instruction.
1e799 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73  ** for tables is
1e79a 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63   OP_Insert..*/.c
1e79b 61 73 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  ase OP_IdxInsert
1e79c 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e  : {        /* in
1e79d 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  2 */.#if 0  /* l
1e79e 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e79f 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6e 20 2a  oved into u.bn *
1e7a0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e7a1 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  pC;.  BtCursor *
1e7a2 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 6e 4b 65  pCrsr;.  int nKe
1e7a3 79 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  y;.  const char 
1e7a4 2a 7a 4b 65 79 3b 0a 23 65 6e 64 69 66 20 2f 2a  *zKey;.#endif /*
1e7a5 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e7a6 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6e   moved into u.bn
1e7a7 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1e7a8 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e7a9 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e7aa 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 20 3d 20 70  );.  u.bn.pC = p
1e7ab 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e7ac 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e  ;.  assert( u.bn
1e7ad 2e 70 43 21 3d 30 20 29 3b 0a 20 20 70 49 6e 32  .pC!=0 );.  pIn2
1e7ae 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32   = &aMem[pOp->p2
1e7af 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  ];.  assert( pIn
1e7b0 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42  2->flags & MEM_B
1e7b1 6c 6f 62 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43  lob );.  u.bn.pC
1e7b2 72 73 72 20 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70  rsr = u.bn.pC->p
1e7b3 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c  Cursor;.  if( AL
1e7b4 57 41 59 53 28 75 2e 62 6e 2e 70 43 72 73 72 21  WAYS(u.bn.pCrsr!
1e7b5 3d 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72  =0) ){.    asser
1e7b6 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 69 73 54 61  t( u.bn.pC->isTa
1e7b7 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63  ble==0 );.    rc
1e7b8 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49   = ExpandBlob(pI
1e7b9 6e 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  n2);.    if( rc=
1e7ba 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1e7bb 20 20 20 20 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20      u.bn.nKey = 
1e7bc 70 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 75  pIn2->n;.      u
1e7bd 2e 62 6e 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d  .bn.zKey = pIn2-
1e7be 3e 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  >z;.      rc = s
1e7bf 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72  qlite3BtreeInser
1e7c0 74 28 75 2e 62 6e 2e 70 43 72 73 72 2c 20 75 2e  t(u.bn.pCrsr, u.
1e7c1 62 6e 2e 7a 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b  bn.zKey, u.bn.nK
1e7c2 65 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f  ey, "", 0, 0, pO
1e7c3 70 2d 3e 70 33 2c 0a 20 20 20 20 20 20 20 20 20  p->p3,.         
1e7c4 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46   ((pOp->p5 & OPF
1e7c5 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
1e7c6 54 29 20 3f 20 75 2e 62 6e 2e 70 43 2d 3e 73 65  T) ? u.bn.pC->se
1e7c7 65 6b 52 65 73 75 6c 74 20 3a 20 30 29 0a 20 20  ekResult : 0).  
1e7c8 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73      );.      ass
1e7c9 65 72 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 64 65  ert( u.bn.pC->de
1e7ca 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20  ferredMoveto==0 
1e7cb 29 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 70 43  );.      u.bn.pC
1e7cc 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1e7cd 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20  CACHE_STALE;.   
1e7ce 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1e7cf 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  }../* Opcode: Id
1e7d0 78 44 65 6c 65 74 65 20 50 31 20 50 32 20 50 33  xDelete P1 P2 P3
1e7d1 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63   * *.**.** The c
1e7d2 6f 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 65 67  ontent of P3 reg
1e7d3 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20  isters starting 
1e7d4 61 74 20 72 65 67 69 73 74 65 72 20 50 32 20 66  at register P2 f
1e7d5 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b  orm.** an unpack
1e7d6 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 68  ed index key. Th
1e7d7 69 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65  is opcode remove
1e7d8 73 20 74 68 61 74 20 65 6e 74 72 79 20 66 72 6f  s that entry fro
1e7d9 6d 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78 20  m the .** index 
1e7da 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72  opened by cursor
1e7db 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P1..*/.case OP_
1e7dc 49 64 78 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66  IdxDelete: {.#if
1e7dd 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e7de 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e7df 6f 20 75 2e 62 6f 20 2a 2f 0a 20 20 56 64 62 65  o u.bo */.  Vdbe
1e7e0 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
1e7e1 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1e7e2 20 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61   int res;.  Unpa
1e7e3 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65  ckedRecord r;.#e
1e7e4 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e7e5 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e7e6 74 6f 20 75 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73  to u.bo */..  as
1e7e7 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20  sert( pOp->p3>0 
1e7e8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1e7e9 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p2>0 && pOp->p
1e7ea 32 2b 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  2+pOp->p3<=p->nM
1e7eb 65 6d 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74  em+1 );.  assert
1e7ec 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e7ed 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e7ee 6f 72 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 20  or );.  u.bo.pC 
1e7ef 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e7f0 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e7f1 2e 62 6f 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75  .bo.pC!=0 );.  u
1e7f2 2e 62 6f 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f  .bo.pCrsr = u.bo
1e7f3 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  .pC->pCursor;.  
1e7f4 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e  if( ALWAYS(u.bo.
1e7f5 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20  pCrsr!=0) ){.   
1e7f6 20 75 2e 62 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f   u.bo.r.pKeyInfo
1e7f7 20 3d 20 75 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79   = u.bo.pC->pKey
1e7f8 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 6f 2e 72  Info;.    u.bo.r
1e7f9 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70  .nField = (u16)p
1e7fa 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 75 2e 62 6f  Op->p3;.    u.bo
1e7fb 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20  .r.flags = 0;.  
1e7fc 20 20 75 2e 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20    u.bo.r.aMem = 
1e7fd 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  &aMem[pOp->p2];.
1e7fe 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e7ff 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1e800 6b 65 64 28 75 2e 62 6f 2e 70 43 72 73 72 2c 20  ked(u.bo.pCrsr, 
1e801 26 75 2e 62 6f 2e 72 2c 20 30 2c 20 30 2c 20 26  &u.bo.r, 0, 0, &
1e802 75 2e 62 6f 2e 72 65 73 29 3b 0a 20 20 20 20 69  u.bo.res);.    i
1e803 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1e804 20 26 26 20 75 2e 62 6f 2e 72 65 73 3d 3d 30 20   && u.bo.res==0 
1e805 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1e806 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65  lite3BtreeDelete
1e807 28 75 2e 62 6f 2e 70 43 72 73 72 29 3b 0a 20 20  (u.bo.pCrsr);.  
1e808 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1e809 75 2e 62 6f 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bo.pC->deferre
1e80a 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
1e80b 20 20 75 2e 62 6f 2e 70 43 2d 3e 63 61 63 68 65    u.bo.pC->cache
1e80c 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1e80d 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61  TALE;.  }.  brea
1e80e 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e80f 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 20   IdxRowid P1 P2 
1e810 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  * * *.**.** Writ
1e811 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
1e812 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68  P2 an integer wh
1e813 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 20  ich is the last 
1e814 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 63  entry in the rec
1e815 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e  ord at.** the en
1e816 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b  d of the index k
1e817 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ey pointed to by
1e818 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 69   cursor P1.  Thi
1e819 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c 64  s integer should
1e81a 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64   be.** the rowid
1e81b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e   of the table en
1e81c 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 69  try to which thi
1e81d 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 6f  s index entry po
1e81e 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ints..**.** See 
1e81f 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b  also: Rowid, Mak
1e820 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65  eRecord..*/.case
1e821 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20   OP_IdxRowid: { 
1e822 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e823 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1e824 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1e825 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e826 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a  ed into u.bp */.
1e827 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1e828 72 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  r;.  VdbeCursor 
1e829 2a 70 43 3b 0a 20 20 69 36 34 20 72 6f 77 69 64  *pC;.  i64 rowid
1e82a 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e82b 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e82c 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 0a  d into u.bp */..
1e82d 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e82e 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e82f 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e830 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bp.pC = p->apC
1e831 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1e832 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 21 3d  ssert( u.bp.pC!=
1e833 30 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 72 73  0 );.  u.bp.pCrs
1e834 72 20 3d 20 75 2e 62 70 2e 70 43 2d 3e 70 43 75  r = u.bp.pC->pCu
1e835 72 73 6f 72 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c  rsor;.  pOut->fl
1e836 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
1e837 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62    if( ALWAYS(u.b
1e838 70 2e 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20  p.pCrsr!=0) ){. 
1e839 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1e83a 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
1e83b 75 2e 62 70 2e 70 43 29 3b 0a 20 20 20 20 69 66  u.bp.pC);.    if
1e83c 28 20 4e 45 56 45 52 28 72 63 29 20 29 20 67 6f  ( NEVER(rc) ) go
1e83d 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1e83e 65 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72  error;.    asser
1e83f 74 28 20 75 2e 62 70 2e 70 43 2d 3e 64 65 66 65  t( u.bp.pC->defe
1e840 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b  rredMoveto==0 );
1e841 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1e842 70 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30  p.pC->isTable==0
1e843 20 29 3b 0a 20 20 20 20 69 66 28 20 21 75 2e 62   );.    if( !u.b
1e844 70 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b  p.pC->nullRow ){
1e845 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1e846 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 28  te3VdbeIdxRowid(
1e847 64 62 2c 20 75 2e 62 70 2e 70 43 72 73 72 2c 20  db, u.bp.pCrsr, 
1e848 26 75 2e 62 70 2e 72 6f 77 69 64 29 3b 0a 20 20  &u.bp.rowid);.  
1e849 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1e84a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1e84b 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1e84c 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1e84d 7d 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e  }.      pOut->u.
1e84e 69 20 3d 20 75 2e 62 70 2e 72 6f 77 69 64 3b 0a  i = u.bp.rowid;.
1e84f 20 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67        pOut->flag
1e850 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1e851 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1e852 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  }../* Opcode: Id
1e853 78 47 45 20 50 31 20 50 32 20 50 33 20 50 34 20  xGE P1 P2 P3 P4 
1e854 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20  P5.**.** The P4 
1e855 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20  register values 
1e856 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50  beginning with P
1e857 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b  3 form an unpack
1e858 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79  ed index .** key
1e859 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20   that omits the 
1e85a 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20  ROWID.  Compare 
1e85b 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61  this key value a
1e85c 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78  gainst the index
1e85d 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20   .** that P1 is 
1e85e 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
1e85f 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20  ng to, ignoring 
1e860 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65  the ROWID on the
1e861 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a   P1 index..**.**
1e862 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78   If the P1 index
1e863 20 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65   entry is greate
1e864 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
1e865 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  to the key value
1e866 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  .** then jump to
1e867 20 50 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20   P2.  Otherwise 
1e868 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20  fall through to 
1e869 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
1e86a 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  tion..**.** If P
1e86b 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68  5 is non-zero th
1e86c 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  en the key value
1e86d 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79   is increased by
1e86e 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20   an epsilon .** 
1e86f 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d  prior to the com
1e870 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d  parison.  This m
1e871 61 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77  ake the opcode w
1e872 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65  ork like IdxGT e
1e873 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66  xcept.** that if
1e874 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65   the key from re
1e875 67 69 73 74 65 72 20 50 33 20 69 73 20 61 20 70  gister P3 is a p
1e876 72 65 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79  refix of the key
1e877 20 69 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a   in the cursor,.
1e878 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  ** the result is
1e879 20 66 61 6c 73 65 20 77 68 65 72 65 61 73 20 69   false whereas i
1e87a 74 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20  t would be true 
1e87b 77 69 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f  with IdxGT..*/./
1e87c 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20  * Opcode: IdxLT 
1e87d 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a  P1 P2 P3 * P5.**
1e87e 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73  .** The P4 regis
1e87f 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e  ter values begin
1e880 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72  ning with P3 for
1e881 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e  m an unpacked in
1e882 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74  dex .** key that
1e883 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44   omits the ROWID
1e884 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20  .  Compare this 
1e885 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73  key value agains
1e886 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20  t the index .** 
1e887 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65  that P1 is curre
1e888 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
1e889 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52  , ignoring the R
1e88a 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69  OWID on the P1 i
1e88b 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ndex..**.** If t
1e88c 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72  he P1 index entr
1e88d 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74  y is less than t
1e88e 68 65 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65  he key value the
1e88f 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1e890 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20   Otherwise fall 
1e891 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
1e892 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
1e893 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20  .**.** If P5 is 
1e894 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68  non-zero then th
1e895 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69  e key value is i
1e896 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65  ncreased by an e
1e897 70 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a  psilon prior .**
1e898 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73   to the comparis
1e899 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20  on.  This makes 
1e89a 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20  the opcode work 
1e89b 6c 69 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63  like IdxLE..*/.c
1e89c 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20  ase OP_IdxLT:   
1e89d 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
1e89e 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45 3a  /.case OP_IdxGE:
1e89f 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1e8a0 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  p */.#if 0  /* l
1e8a1 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e8a2 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a  oved into u.bq *
1e8a3 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e8a4 70 43 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20  pC;.  int res;. 
1e8a5 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1e8a6 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  r;.#endif /* loc
1e8a7 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e8a8 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a  ed into u.bq */.
1e8a9 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e8aa 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e8ab 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e8ac 20 75 2e 62 71 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bq.pC = p->ap
1e8ad 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e8ae 61 73 73 65 72 74 28 20 75 2e 62 71 2e 70 43 21  assert( u.bq.pC!
1e8af 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41  =0 );.  if( ALWA
1e8b0 59 53 28 75 2e 62 71 2e 70 43 2d 3e 70 43 75 72  YS(u.bq.pC->pCur
1e8b1 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61  sor!=0) ){.    a
1e8b2 73 73 65 72 74 28 20 75 2e 62 71 2e 70 43 2d 3e  ssert( u.bq.pC->
1e8b3 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d  deferredMoveto==
1e8b4 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
1e8b5 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70   pOp->p5==0 || p
1e8b6 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20  Op->p5==1 );.   
1e8b7 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1e8b8 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29  type==P4_INT32 )
1e8b9 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 70 4b 65  ;.    u.bq.r.pKe
1e8ba 79 49 6e 66 6f 20 3d 20 75 2e 62 71 2e 70 43 2d  yInfo = u.bq.pC-
1e8bb 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75  >pKeyInfo;.    u
1e8bc 2e 62 71 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28  .bq.r.nField = (
1e8bd 75 31 36 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20  u16)pOp->p4.i;. 
1e8be 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29     if( pOp->p5 )
1e8bf 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 2e 66  {.      u.bq.r.f
1e8c0 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f  lags = UNPACKED_
1e8c1 49 4e 43 52 4b 45 59 20 7c 20 55 4e 50 41 43 4b  INCRKEY | UNPACK
1e8c2 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b  ED_IGNORE_ROWID;
1e8c3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1e8c4 20 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d    u.bq.r.flags =
1e8c5 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45   UNPACKED_IGNORE
1e8c6 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20  _ROWID;.    }.  
1e8c7 20 20 75 2e 62 71 2e 72 2e 61 4d 65 6d 20 3d 20    u.bq.r.aMem = 
1e8c8 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
1e8c9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e8ca 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72  VdbeIdxKeyCompar
1e8cb 65 28 75 2e 62 71 2e 70 43 2c 20 26 75 2e 62 71  e(u.bq.pC, &u.bq
1e8cc 2e 72 2c 20 26 75 2e 62 71 2e 72 65 73 29 3b 0a  .r, &u.bq.res);.
1e8cd 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63      if( pOp->opc
1e8ce 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b  ode==OP_IdxLT ){
1e8cf 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 65 73 20  .      u.bq.res 
1e8d0 3d 20 2d 75 2e 62 71 2e 72 65 73 3b 0a 20 20 20  = -u.bq.res;.   
1e8d1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
1e8d2 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  sert( pOp->opcod
1e8d3 65 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20  e==OP_IdxGE );. 
1e8d4 20 20 20 20 20 75 2e 62 71 2e 72 65 73 2b 2b 3b       u.bq.res++;
1e8d5 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75  .    }.    if( u
1e8d6 2e 62 71 2e 72 65 73 3e 30 20 29 7b 0a 20 20 20  .bq.res>0 ){.   
1e8d7 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1e8d8 2d 20 31 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  - 1 ;.    }.  }.
1e8d9 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e8da 70 63 6f 64 65 3a 20 44 65 73 74 72 6f 79 20 50  pcode: Destroy P
1e8db 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1e8dc 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69  * Delete an enti
1e8dd 72 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  re database tabl
1e8de 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65  e or index whose
1e8df 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68   root page in th
1e8e0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  e database.** fi
1e8e1 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50  le is given by P
1e8e2 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62  1..**.** The tab
1e8e3 6c 65 20 62 65 69 6e 67 20 64 65 73 74 72 6f 79  le being destroy
1e8e4 65 64 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69  ed is in the mai
1e8e5 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
1e8e6 69 66 20 50 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a  if P3==0.  If.**
1e8e7 20 50 33 3d 3d 31 20 74 68 65 6e 20 74 68 65 20   P3==1 then the 
1e8e8 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 61  table to be clea
1e8e9 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78 69  r is in the auxi
1e8ea 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66  liary database f
1e8eb 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 75  ile.** that is u
1e8ec 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61 62  sed to store tab
1e8ed 6c 65 73 20 63 72 65 61 74 65 20 75 73 69 6e 67  les create using
1e8ee 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41 52   CREATE TEMPORAR
1e8ef 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49  Y TABLE..**.** I
1e8f0 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20  f AUTOVACUUM is 
1e8f1 65 6e 61 62 6c 65 64 20 74 68 65 6e 20 69 74 20  enabled then it 
1e8f2 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74  is possible that
1e8f3 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 20 70 61   another root pa
1e8f4 67 65 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 6d  ge.** might be m
1e8f5 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6e 65  oved into the ne
1e8f6 77 6c 79 20 64 65 6c 65 74 65 64 20 72 6f 6f 74  wly deleted root
1e8f7 20 70 61 67 65 20 69 6e 20 6f 72 64 65 72 20 74   page in order t
1e8f8 6f 20 6b 65 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f  o keep all.** ro
1e8f9 6f 74 20 70 61 67 65 73 20 63 6f 6e 74 69 67 75  ot pages contigu
1e8fa 6f 75 73 20 61 74 20 74 68 65 20 62 65 67 69 6e  ous at the begin
1e8fb 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61  ning of the data
1e8fc 62 61 73 65 2e 20 20 54 68 65 20 66 6f 72 6d 65  base.  The forme
1e8fd 72 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68  r.** value of th
1e8fe 65 20 72 6f 6f 74 20 70 61 67 65 20 74 68 61 74  e root page that
1e8ff 20 6d 6f 76 65 64 20 2d 20 69 74 73 20 76 61 6c   moved - its val
1e900 75 65 20 62 65 66 6f 72 65 20 74 68 65 20 6d 6f  ue before the mo
1e901 76 65 20 6f 63 63 75 72 72 65 64 20 2d 0a 2a 2a  ve occurred -.**
1e902 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65   is stored in re
1e903 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 6e  gister P2.  If n
1e904 6f 20 70 61 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d  o page .** movem
1e905 65 6e 74 20 77 61 73 20 72 65 71 75 69 72 65 64  ent was required
1e906 20 28 62 65 63 61 75 73 65 20 74 68 65 20 74 61   (because the ta
1e907 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
1e908 64 20 77 61 73 20 61 6c 72 65 61 64 79 20 0a 2a  d was already .*
1e909 2a 20 74 68 65 20 6c 61 73 74 20 6f 6e 65 20 69  * the last one i
1e90a 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 29 20  n the database) 
1e90b 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73  then a zero is s
1e90c 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1e90d 72 20 50 32 2e 0a 2a 2a 20 49 66 20 41 55 54 4f  r P2..** If AUTO
1e90e 56 41 43 55 55 4d 20 69 73 20 64 69 73 61 62 6c  VACUUM is disabl
1e90f 65 64 20 74 68 65 6e 20 61 20 7a 65 72 6f 20 69  ed then a zero i
1e910 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  s stored in regi
1e911 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53  ster P2..**.** S
1e912 65 65 20 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a  ee also: Clear.*
1e913 2f 0a 63 61 73 65 20 4f 50 5f 44 65 73 74 72 6f  /.case OP_Destro
1e914 79 3a 20 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32  y: {     /* out2
1e915 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23  -prerelease */.#
1e916 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1e917 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e918 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a 20 20 69 6e  nto u.br */.  in
1e919 74 20 69 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20  t iMoved;.  int 
1e91a 69 43 6e 74 3b 0a 20 20 56 64 62 65 20 2a 70 56  iCnt;.  Vdbe *pV
1e91b 64 62 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  dbe;.  int iDb;.
1e91c 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1e91d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e91e 69 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a 23 69 66  into u.br */.#if
1e91f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1e920 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1e921 75 2e 62 72 2e 69 43 6e 74 20 3d 20 30 3b 0a 20  u.br.iCnt = 0;. 
1e922 20 66 6f 72 28 75 2e 62 72 2e 70 56 64 62 65 3d   for(u.br.pVdbe=
1e923 64 62 2d 3e 70 56 64 62 65 3b 20 75 2e 62 72 2e  db->pVdbe; u.br.
1e924 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 62  pVdbe; u.br.pVdb
1e925 65 20 3d 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e  e = u.br.pVdbe->
1e926 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
1e927 75 2e 62 72 2e 70 56 64 62 65 2d 3e 6d 61 67 69  u.br.pVdbe->magi
1e928 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1e929 4e 20 26 26 20 75 2e 62 72 2e 70 56 64 62 65 2d  N && u.br.pVdbe-
1e92a 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 3c 32 20  >inVtabMethod<2 
1e92b 26 26 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70  && u.br.pVdbe->p
1e92c 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 75 2e  c>=0 ){.      u.
1e92d 62 72 2e 69 43 6e 74 2b 2b 3b 0a 20 20 20 20 7d  br.iCnt++;.    }
1e92e 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 75 2e 62  .  }.#else.  u.b
1e92f 72 2e 69 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74  r.iCnt = db->act
1e930 69 76 65 56 64 62 65 43 6e 74 3b 0a 23 65 6e 64  iveVdbeCnt;.#end
1e931 69 66 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  if.  pOut->flags
1e932 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 69   = MEM_Null;.  i
1e933 66 28 20 75 2e 62 72 2e 69 43 6e 74 3e 31 20 29  f( u.br.iCnt>1 )
1e934 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1e935 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 70 2d  E_LOCKED;.    p-
1e936 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f  >errorAction = O
1e937 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 65  E_Abort;.  }else
1e938 7b 0a 20 20 20 20 75 2e 62 72 2e 69 44 62 20 3d  {.    u.br.iDb =
1e939 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 61 73   pOp->p3;.    as
1e93a 73 65 72 74 28 20 75 2e 62 72 2e 69 43 6e 74 3d  sert( u.br.iCnt=
1e93b 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
1e93c 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1e93d 26 20 28 31 3c 3c 75 2e 62 72 2e 69 44 62 29 29  & (1<<u.br.iDb))
1e93e 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  !=0 );.    rc = 
1e93f 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70  sqlite3BtreeDrop
1e940 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 75 2e  Table(db->aDb[u.
1e941 62 72 2e 69 44 62 5d 2e 70 42 74 2c 20 70 4f 70  br.iDb].pBt, pOp
1e942 2d 3e 70 31 2c 20 26 75 2e 62 72 2e 69 4d 6f 76  ->p1, &u.br.iMov
1e943 65 64 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66  ed);.    pOut->f
1e944 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1e945 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20      pOut->u.i = 
1e946 75 2e 62 72 2e 69 4d 6f 76 65 64 3b 0a 23 69 66  u.br.iMoved;.#if
1e947 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1e948 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
1e949 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1e94a 4b 20 26 26 20 75 2e 62 72 2e 69 4d 6f 76 65 64  K && u.br.iMoved
1e94b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=0 ){.      sql
1e94c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65  ite3RootPageMove
1e94d 64 28 26 64 62 2d 3e 61 44 62 5b 75 2e 62 72 2e  d(&db->aDb[u.br.
1e94e 69 44 62 5d 2c 20 75 2e 62 72 2e 69 4d 6f 76 65  iDb], u.br.iMove
1e94f 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20  d, pOp->p1);.   
1e950 20 20 20 72 65 73 65 74 53 63 68 65 6d 61 4f 6e     resetSchemaOn
1e951 46 61 75 6c 74 20 3d 20 31 3b 0a 20 20 20 20 7d  Fault = 1;.    }
1e952 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 62 72  .#endif.  }.  br
1e953 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e954 65 3a 20 43 6c 65 61 72 20 50 31 20 50 32 20 50  e: Clear P1 P2 P
1e955 33 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  3.**.** Delete a
1e956 6c 6c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  ll contents of t
1e957 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
1e958 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65  e or index whose
1e959 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 6e   root page.** in
1e95a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1e95b 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50  le is given by P
1e95c 31 2e 20 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20  1.  But, unlike 
1e95d 44 65 73 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a  Destroy, do not.
1e95e 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61  ** remove the ta
1e95f 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 66 72 6f  ble or index fro
1e960 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
1e961 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ile..**.** The t
1e962 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72  able being clear
1e963 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20   is in the main 
1e964 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66  database file if
1e965 20 50 32 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50   P2==0.  If.** P
1e966 32 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61  2==1 then the ta
1e967 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20  ble to be clear 
1e968 69 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69  is in the auxili
1e969 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  ary database fil
1e96a 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65  e.** that is use
1e96b 64 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65  d to store table
1e96c 73 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 43  s create using C
1e96d 52 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20  REATE TEMPORARY 
1e96e 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  TABLE..**.** If 
1e96f 74 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20  the P3 value is 
1e970 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
1e971 68 65 20 74 61 62 6c 65 20 72 65 66 65 72 72 65  he table referre
1e972 64 20 74 6f 20 6d 75 73 74 20 62 65 20 61 6e 0a  d to must be an.
1e973 2a 2a 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20  ** intkey table 
1e974 28 61 6e 20 53 51 4c 20 74 61 62 6c 65 2c 20 6e  (an SQL table, n
1e975 6f 74 20 61 6e 20 69 6e 64 65 78 29 2e 20 49 6e  ot an index). In
1e976 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 72   this case the r
1e977 6f 77 20 63 68 61 6e 67 65 20 0a 2a 2a 20 63 6f  ow change .** co
1e978 75 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  unt is increment
1e979 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72  ed by the number
1e97a 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20   of rows in the 
1e97b 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61  table being clea
1e97c 72 65 64 2e 20 0a 2a 2a 20 49 66 20 50 33 20 69  red. .** If P3 i
1e97d 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a  s greater than z
1e97e 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 76 61  ero, then the va
1e97f 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 72 65  lue stored in re
1e980 67 69 73 74 65 72 20 50 33 20 69 73 0a 2a 2a 20  gister P3 is.** 
1e981 61 6c 73 6f 20 69 6e 63 72 65 6d 65 6e 74 65 64  also incremented
1e982 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
1e983 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61  f rows in the ta
1e984 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65  ble being cleare
1e985 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  d..**.** See als
1e986 6f 3a 20 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61  o: Destroy.*/.ca
1e987 73 65 20 4f 50 5f 43 6c 65 61 72 3a 20 7b 0a 23  se OP_Clear: {.#
1e988 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1e989 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e98a 6e 74 6f 20 75 2e 62 73 20 2a 2f 0a 20 20 69 6e  nto u.bs */.  in
1e98b 74 20 6e 43 68 61 6e 67 65 3b 0a 23 65 6e 64 69  t nChange;.#endi
1e98c 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e98d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e98e 75 2e 62 73 20 2a 2f 0a 0a 20 20 75 2e 62 73 2e  u.bs */..  u.bs.
1e98f 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 61  nChange = 0;.  a
1e990 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65  ssert( (p->btree
1e991 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e  Mask & (1<<pOp->
1e992 70 32 29 29 21 3d 30 20 29 3b 0a 20 20 72 63 20  p2))!=0 );.  rc 
1e993 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  = sqlite3BtreeCl
1e994 65 61 72 54 61 62 6c 65 28 0a 20 20 20 20 20 20  earTable(.      
1e995 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d  db->aDb[pOp->p2]
1e996 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 28  .pBt, pOp->p1, (
1e997 70 4f 70 2d 3e 70 33 20 3f 20 26 75 2e 62 73 2e  pOp->p3 ? &u.bs.
1e998 6e 43 68 61 6e 67 65 20 3a 20 30 29 0a 20 20 29  nChange : 0).  )
1e999 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20  ;.  if( pOp->p3 
1e99a 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67  ){.    p->nChang
1e99b 65 20 2b 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67  e += u.bs.nChang
1e99c 65 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  e;.    if( pOp->
1e99d 70 33 3e 30 20 29 7b 0a 20 20 20 20 20 20 61 4d  p3>0 ){.      aM
1e99e 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20  em[pOp->p3].u.i 
1e99f 2b 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b  += u.bs.nChange;
1e9a0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
1e9a1 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e9a2 3a 20 43 72 65 61 74 65 54 61 62 6c 65 20 50 31  : CreateTable P1
1e9a3 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1e9a4 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74  Allocate a new t
1e9a5 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e  able in the main
1e9a6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1e9a7 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68  f P1==0 or in th
1e9a8 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64  e.** auxiliary d
1e9a9 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1e9aa 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61  P1==1 or in an a
1e9ab 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1e9ac 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72   if.** P1>1.  Wr
1e9ad 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67  ite the root pag
1e9ae 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
1e9af 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a  new table into.*
1e9b0 2a 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a  * register P2.**
1e9b1 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e  .** The differen
1e9b2 63 65 20 62 65 74 77 65 65 6e 20 61 20 74 61 62  ce between a tab
1e9b3 6c 65 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20  le and an index 
1e9b4 69 73 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c  is this:  A tabl
1e9b5 65 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61  e must.** have a
1e9b6 20 34 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20   4-byte integer 
1e9b7 6b 65 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65  key and can have
1e9b8 20 61 72 62 69 74 72 61 72 79 20 64 61 74 61 2e   arbitrary data.
1e9b9 20 20 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61    An index.** ha
1e9ba 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b  s an arbitrary k
1e9bb 65 79 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a  ey but no data..
1e9bc 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1e9bd 43 72 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f  CreateIndex.*/./
1e9be 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65  * Opcode: Create
1e9bf 49 6e 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20  Index P1 P2 * * 
1e9c0 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  *.**.** Allocate
1e9c1 20 61 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20   a new index in 
1e9c2 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1e9c3 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20  e file if P1==0 
1e9c4 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78  or in the.** aux
1e9c5 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
1e9c6 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72  file if P1==1 or
1e9c7 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20   in an attached 
1e9c8 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50  database if.** P
1e9c9 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20  1>1.  Write the 
1e9ca 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
1e9cb 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c   of the new tabl
1e9cc 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74  e into.** regist
1e9cd 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  er P2..**.** See
1e9ce 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f   documentation o
1e9cf 6e 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65  n OP_CreateTable
1e9d0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
1e9d1 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1e9d2 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e  case OP_CreateIn
1e9d3 64 65 78 3a 20 20 20 20 20 20 20 20 20 20 20 20  dex:            
1e9d4 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1e9d5 73 65 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72  se */.case OP_Cr
1e9d6 65 61 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20  eateTable: {    
1e9d7 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1e9d8 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1e9d9 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e9da 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e9db 20 75 2e 62 74 20 2a 2f 0a 20 20 69 6e 74 20 70   u.bt */.  int p
1e9dc 67 6e 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73  gno;.  int flags
1e9dd 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e  ;.  Db *pDb;.#en
1e9de 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e9df 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e9e0 6f 20 75 2e 62 74 20 2a 2f 0a 0a 20 20 75 2e 62  o u.bt */..  u.b
1e9e1 74 2e 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73  t.pgno = 0;.  as
1e9e2 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e9e3 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e   && pOp->p1<db->
1e9e4 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
1e9e5 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1e9e6 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d   (1<<pOp->p1))!=
1e9e7 30 20 29 3b 0a 20 20 75 2e 62 74 2e 70 44 62 20  0 );.  u.bt.pDb 
1e9e8 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  = &db->aDb[pOp->
1e9e9 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e9ea 2e 62 74 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20  .bt.pDb->pBt!=0 
1e9eb 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  );.  if( pOp->op
1e9ec 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54  code==OP_CreateT
1e9ed 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75  able ){.    /* u
1e9ee 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45  .bt.flags = BTRE
1e9ef 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20  E_INTKEY; */.   
1e9f0 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54   u.bt.flags = BT
1e9f1 52 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52  REE_LEAFDATA|BTR
1e9f2 45 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c  EE_INTKEY;.  }el
1e9f3 73 65 7b 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61  se{.    u.bt.fla
1e9f4 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44  gs = BTREE_ZEROD
1e9f5 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ATA;.  }.  rc = 
1e9f6 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
1e9f7 74 65 54 61 62 6c 65 28 75 2e 62 74 2e 70 44 62  teTable(u.bt.pDb
1e9f8 2d 3e 70 42 74 2c 20 26 75 2e 62 74 2e 70 67 6e  ->pBt, &u.bt.pgn
1e9f9 6f 2c 20 75 2e 62 74 2e 66 6c 61 67 73 29 3b 0a  o, u.bt.flags);.
1e9fa 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e    pOut->u.i = u.
1e9fb 62 74 2e 70 67 6e 6f 3b 0a 20 20 62 72 65 61 6b  bt.pgno;.  break
1e9fc 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1e9fd 50 61 72 73 65 53 63 68 65 6d 61 20 50 31 20 50  ParseSchema P1 P
1e9fe 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52  2 * P4 *.**.** R
1e9ff 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c  ead and parse al
1ea00 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74  l entries from t
1ea01 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  he SQLITE_MASTER
1ea02 20 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61   table of databa
1ea03 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61  se P1.** that ma
1ea04 74 63 68 20 74 68 65 20 57 48 45 52 45 20 63 6c  tch the WHERE cl
1ea05 61 75 73 65 20 50 34 2e 20 20 50 32 20 69 73 20  ause P4.  P2 is 
1ea06 74 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61 67  the "force" flag
1ea07 2e 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a  .   Always do.**
1ea08 20 74 68 65 20 70 61 72 73 69 6e 67 20 69 66 20   the parsing if 
1ea09 50 32 20 69 73 20 74 72 75 65 2e 20 20 49 66 20  P2 is true.  If 
1ea0a 50 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  P2 is false, the
1ea0b 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
1ea0c 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20  s a.** no-op if 
1ea0d 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 6f  the schema is no
1ea0e 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64  t currently load
1ea0f 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ed.  In other wo
1ea10 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73  rds, if P2.** is
1ea11 20 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c 49   false, the SQLI
1ea12 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20  TE_MASTER table 
1ea13 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 69  is only parsed i
1ea14 66 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68  f the rest of th
1ea15 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 61  e.** schema is a
1ea16 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69 6e  lready loaded in
1ea17 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 61  to the symbol ta
1ea18 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ble..**.** This 
1ea19 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74  opcode invokes t
1ea1a 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 72 65  he parser to cre
1ea1b 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61  ate a new virtua
1ea1c 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68  l machine,.** th
1ea1d 65 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77 20  en runs the new 
1ea1e 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
1ea1f 20 20 49 74 20 69 73 20 74 68 75 73 20 61 20 72    It is thus a r
1ea20 65 2d 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65  e-entrant opcode
1ea21 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72  ..*/.case OP_Par
1ea22 73 65 53 63 68 65 6d 61 3a 20 7b 0a 23 69 66 20  seSchema: {.#if 
1ea23 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1ea24 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1ea25 20 75 2e 62 75 20 2a 2f 0a 20 20 69 6e 74 20 69   u.bu */.  int i
1ea26 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  Db;.  const char
1ea27 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 63 68 61   *zMaster;.  cha
1ea28 72 20 2a 7a 53 71 6c 3b 0a 20 20 49 6e 69 74 44  r *zSql;.  InitD
1ea29 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 23 65  ata initData;.#e
1ea2a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1ea2b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ea2c 74 6f 20 75 2e 62 75 20 2a 2f 0a 0a 20 20 75 2e  to u.bu */..  u.
1ea2d 62 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31  bu.iDb = pOp->p1
1ea2e 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 75  ;.  assert( u.bu
1ea2f 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 62 75 2e  .iDb>=0 && u.bu.
1ea30 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a  iDb<db->nDb );..
1ea31 20 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 32 20    /* If pOp->p2 
1ea32 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73 20  is 0, then this 
1ea33 6f 70 63 6f 64 65 20 69 73 20 62 65 69 6e 67 20  opcode is being 
1ea34 65 78 65 63 75 74 65 64 20 74 6f 20 72 65 61 64  executed to read
1ea35 20 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 72   a.  ** single r
1ea36 6f 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20  ow, for example 
1ea37 74 68 65 20 72 6f 77 20 63 6f 72 72 65 73 70 6f  the row correspo
1ea38 6e 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 20 69  nding to a new i
1ea39 6e 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 74 65  ndex.  ** create
1ea3a 64 20 62 79 20 74 68 69 73 20 56 44 42 45 2c 20  d by this VDBE, 
1ea3b 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f  from the sqlite_
1ea3c 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 74  master table. It
1ea3d 20 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 20   only.  ** does 
1ea3e 74 68 69 73 20 69 66 20 74 68 65 20 63 6f 72 72  this if the corr
1ea3f 65 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d  esponding in-mem
1ea40 6f 72 79 20 73 63 68 65 6d 61 20 69 73 20 63 75  ory schema is cu
1ea41 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61  rrently.  ** loa
1ea42 64 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ded. Otherwise, 
1ea43 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 64 65  the new index de
1ea44 66 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 65 20  finition can be 
1ea45 6c 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 20 2a  loaded along.  *
1ea46 2a 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20  * with the rest 
1ea47 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 68  of the schema wh
1ea48 65 6e 20 69 74 20 69 73 20 72 65 71 75 69 72 65  en it is require
1ea49 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74  d..  **.  ** Alt
1ea4a 68 6f 75 67 68 20 74 68 65 20 6d 75 74 65 78 20  hough the mutex 
1ea4b 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20  on the BtShared 
1ea4c 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f 72 72  object that corr
1ea4d 65 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a 20  esponds to.  ** 
1ea4e 64 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69 44  database u.bu.iD
1ea4f 62 20 28 74 68 65 20 64 61 74 61 62 61 73 65 20  b (the database 
1ea50 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73  containing the s
1ea51 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
1ea52 6c 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 20  le.  ** read by 
1ea53 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1ea54 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68  ) is currently h
1ea55 65 6c 64 2c 20 69 74 20 69 73 20 6e 65 63 65 73  eld, it is neces
1ea56 73 61 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74  sary to.  ** obt
1ea57 61 69 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20  ain the mutexes 
1ea58 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20  on all attached 
1ea59 64 61 74 61 62 61 73 65 73 20 62 65 66 6f 72 65  databases before
1ea5a 20 63 68 65 63 6b 69 6e 67 20 69 66 0a 20 20 2a   checking if.  *
1ea5b 2a 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20  * the schema of 
1ea5c 75 2e 62 75 2e 69 44 62 20 69 73 20 6c 6f 61 64  u.bu.iDb is load
1ea5d 65 64 2e 20 54 68 69 73 20 69 73 20 62 65 63 61  ed. This is beca
1ea5e 75 73 65 2c 20 61 74 20 74 68 65 20 73 74 61 72  use, at the star
1ea5f 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71  t of.  ** the sq
1ea60 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c  lite3_exec() cal
1ea61 6c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 20  l below, SQLite 
1ea62 77 69 6c 6c 20 69 6e 76 6f 6b 65 0a 20 20 2a 2a  will invoke.  **
1ea63 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1ea64 65 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c 20  erAll(). If all 
1ea65 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20  mutexes are not 
1ea66 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68  already held, th
1ea67 65 0a 20 20 2a 2a 20 75 2e 62 75 2e 69 44 62 20  e.  ** u.bu.iDb 
1ea68 6d 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 6d  mutex may be tem
1ea69 70 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 65  porarily release
1ea6a 64 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 6c  d to avoid deadl
1ea6b 6f 63 6b 2e 20 49 66 0a 20 20 2a 2a 20 74 68 69  ock. If.  ** thi
1ea6c 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20  s happens, then 
1ea6d 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61  some other threa
1ea6e 64 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 65  d may delete the
1ea6f 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20   in-memory.  ** 
1ea70 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61  schema of databa
1ea71 73 65 20 75 2e 62 75 2e 69 44 62 20 62 65 66 6f  se u.bu.iDb befo
1ea72 72 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  re the SQL state
1ea73 6d 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20 73  ment runs. The s
1ea74 63 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c 20  chema.  ** will 
1ea75 6e 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64 20  not be reloaded 
1ea76 62 65 63 75 61 73 65 20 74 68 65 20 64 62 2d 3e  becuase the db->
1ea77 69 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20 69  init.busy flag i
1ea78 73 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a 2a  s set. This.  **
1ea79 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 61   can result in a
1ea7a 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a   "no such table:
1ea7b 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 20   sqlite_master" 
1ea7c 6f 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20 20  or "malformed.  
1ea7d 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ** database sche
1ea7e 6d 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67 20  ma" error being 
1ea7f 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
1ea80 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  user..  */.  ass
1ea81 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1ea82 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e  eHoldsMutex(db->
1ea83 61 44 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 70 42  aDb[u.bu.iDb].pB
1ea84 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  t) );.  sqlite3B
1ea85 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29  treeEnterAll(db)
1ea86 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20  ;.  if( pOp->p2 
1ea87 7c 7c 20 44 62 48 61 73 50 72 6f 70 65 72 74 79  || DbHasProperty
1ea88 28 64 62 2c 20 75 2e 62 75 2e 69 44 62 2c 20 44  (db, u.bu.iDb, D
1ea89 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20  B_SchemaLoaded) 
1ea8a 29 7b 0a 20 20 20 20 75 2e 62 75 2e 7a 4d 61 73  ){.    u.bu.zMas
1ea8b 74 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  ter = SCHEMA_TAB
1ea8c 4c 45 28 75 2e 62 75 2e 69 44 62 29 3b 0a 20 20  LE(u.bu.iDb);.  
1ea8d 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e    u.bu.initData.
1ea8e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 75 2e 62  db = db;.    u.b
1ea8f 75 2e 69 6e 69 74 44 61 74 61 2e 69 44 62 20 3d  u.initData.iDb =
1ea90 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 75 2e   pOp->p1;.    u.
1ea91 62 75 2e 69 6e 69 74 44 61 74 61 2e 70 7a 45 72  bu.initData.pzEr
1ea92 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d  rMsg = &p->zErrM
1ea93 73 67 3b 0a 20 20 20 20 75 2e 62 75 2e 7a 53 71  sg;.    u.bu.zSq
1ea94 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  l = sqlite3MPrin
1ea95 74 66 28 64 62 2c 0a 20 20 20 20 20 20 20 22 53  tf(db,.       "S
1ea96 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74  ELECT name, root
1ea97 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27  page, sql FROM '
1ea98 25 71 27 2e 25 73 20 57 48 45 52 45 20 25 73 22  %q'.%s WHERE %s"
1ea99 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62  ,.       db->aDb
1ea9a 5b 75 2e 62 75 2e 69 44 62 5d 2e 7a 4e 61 6d 65  [u.bu.iDb].zName
1ea9b 2c 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 2c 20  , u.bu.zMaster, 
1ea9c 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20  pOp->p4.z);.    
1ea9d 69 66 28 20 75 2e 62 75 2e 7a 53 71 6c 3d 3d 30  if( u.bu.zSql==0
1ea9e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
1ea9f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1eaa0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76   }else{.      (v
1eaa1 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1eaa2 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
1eaa3 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74  assert( db->init
1eaa4 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20  .busy==0 );.    
1eaa5 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20    db->init.busy 
1eaa6 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 75 2e  = 1;.      u.bu.
1eaa7 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53 51  initData.rc = SQ
1eaa8 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 61  LITE_OK;.      a
1eaa9 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
1eaaa 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
1eaab 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
1eaac 78 65 63 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71  xec(db, u.bu.zSq
1eaad 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61  l, sqlite3InitCa
1eaae 6c 6c 62 61 63 6b 2c 20 26 75 2e 62 75 2e 69 6e  llback, &u.bu.in
1eaaf 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20  itData, 0);.    
1eab0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1eab1 5f 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 75 2e  _OK ) rc = u.bu.
1eab2 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20  initData.rc;.   
1eab3 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1eab4 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 29 3b  (db, u.bu.zSql);
1eab5 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e  .      db->init.
1eab6 62 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  busy = 0;.      
1eab7 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1eab8 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d  etyOn(db);.    }
1eab9 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
1eaba 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
1eabb 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1eabc 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 67  E_NOMEM ){.    g
1eabd 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1eabe 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20    break;.}..#if 
1eabf 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1eac0 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0a 2f 2a  OMIT_ANALYZE)./*
1eac1 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61   Opcode: LoadAna
1eac2 6c 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a  lysis P1 * * * *
1eac3 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20  .**.** Read the 
1eac4 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
1eac5 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  le for database 
1eac6 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 20  P1 and load the 
1eac7 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68  content.** of th
1eac8 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68  at table into th
1eac9 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78  e internal index
1eaca 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68   hash table.  Th
1eacb 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a  is will cause.**
1eacc 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74 6f   the analysis to
1eacd 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70 72   be used when pr
1eace 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 73  eparing all subs
1eacf 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e 0a  equent queries..
1ead0 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41  */.case OP_LoadA
1ead1 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 20 61 73 73  nalysis: {.  ass
1ead2 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1ead3 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e  && pOp->p1<db->n
1ead4 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Db );.  rc = sql
1ead5 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64  ite3AnalysisLoad
1ead6 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20  (db, pOp->p1);. 
1ead7 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64   break;  .}.#end
1ead8 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
1ead9 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59  QLITE_OMIT_ANALY
1eada 5a 45 29 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  ZE) */../* Opcod
1eadb 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20  e: DropTable P1 
1eadc 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52  * * P4 *.**.** R
1eadd 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e  emove the intern
1eade 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64  al (in-memory) d
1eadf 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74  ata structures t
1eae0 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20  hat describe.** 
1eae1 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20  the table named 
1eae2 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  P4 in database P
1eae3 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c  1.  This is call
1eae4 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65  ed after a table
1eae5 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69  .** is dropped i
1eae6 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
1eae7 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
1eae8 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
1eae9 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e  he.** schema con
1eaea 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61  sistent with wha
1eaeb 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f  t is on disk..*/
1eaec 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62  .case OP_DropTab
1eaed 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55  le: {.  sqlite3U
1eaee 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61  nlinkAndDeleteTa
1eaef 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c  ble(db, pOp->p1,
1eaf0 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62   pOp->p4.z);.  b
1eaf1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1eaf2 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31  de: DropIndex P1
1eaf3 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1eaf4 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72  Remove the inter
1eaf5 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20  nal (in-memory) 
1eaf6 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
1eaf7 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a  that describe.**
1eaf8 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64   the index named
1eaf9 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20   P4 in database 
1eafa 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c  P1.  This is cal
1eafb 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64  led after an ind
1eafc 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64  ex.** is dropped
1eafd 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65   in order to kee
1eafe 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72  p the internal r
1eaff 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
1eb00 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63   the.** schema c
1eb01 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77  onsistent with w
1eb02 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a  hat is on disk..
1eb03 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49  */.case OP_DropI
1eb04 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65  ndex: {.  sqlite
1eb05 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
1eb06 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70  Index(db, pOp->p
1eb07 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  1, pOp->p4.z);. 
1eb08 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1eb09 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65  code: DropTrigge
1eb0a 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  r P1 * * P4 *.**
1eb0b 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69  .** Remove the i
1eb0c 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f  nternal (in-memo
1eb0d 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74 75  ry) data structu
1eb0e 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62  res that describ
1eb0f 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72  e.** the trigger
1eb10 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74   named P4 in dat
1eb11 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20  abase P1.  This 
1eb12 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
1eb13 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20  a trigger.** is 
1eb14 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72  dropped in order
1eb15 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74   to keep the int
1eb16 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
1eb17 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73  tion of the.** s
1eb18 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74  chema consistent
1eb19 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e   with what is on
1eb1a 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f   disk..*/.case O
1eb1b 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b  P_DropTrigger: {
1eb1c 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  .  sqlite3Unlink
1eb1d 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72  AndDeleteTrigger
1eb1e 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  (db, pOp->p1, pO
1eb1f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61  p->p4.z);.  brea
1eb20 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  k;.}...#ifndef S
1eb21 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
1eb22 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70  RITY_CHECK./* Op
1eb23 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43  code: IntegrityC
1eb24 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a  k P1 P2 P3 * P5.
1eb25 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c  **.** Do an anal
1eb26 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 72  ysis of the curr
1eb27 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62  ently open datab
1eb28 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a  ase.  Store in.*
1eb29 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 68  * register P1 th
1eb2a 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72  e text of an err
1eb2b 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 72  or message descr
1eb2c 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65  ibing any proble
1eb2d 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f  ms..** If no pro
1eb2e 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c  blems are found,
1eb2f 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e   store a NULL in
1eb30 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1eb31 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 72  .** The register
1eb32 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65   P3 contains the
1eb33 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1eb34 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72  of allowed error
1eb35 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65  s..** At most re
1eb36 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c  g(P3) errors wil
1eb37 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a  l be reported..*
1eb38 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
1eb39 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73  , the analysis s
1eb3a 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20  tops as soon as 
1eb3b 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 61  reg(P1) errors a
1eb3c 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65  re .** seen.  Re
1eb3d 67 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64  g(P1) is updated
1eb3e 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72   with the number
1eb3f 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69   of errors remai
1eb40 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ning..**.** The 
1eb41 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
1eb42 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20  s of all tables 
1eb43 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1eb44 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73  are integer.** s
1eb45 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 29  tored in reg(P1)
1eb46 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67  , reg(P1+1), reg
1eb47 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68  (P1+2), ....  Th
1eb48 65 72 65 20 61 72 65 20 50 32 20 74 61 62 6c 65  ere are P2 table
1eb49 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a  s.** total..**.*
1eb4a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a  * If P5 is not z
1eb4b 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69  ero, the check i
1eb4c 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75  s done on the au
1eb4d 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
1eb4e 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68  .** file, not th
1eb4f 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1eb50 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  file..**.** This
1eb51 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20   opcode is used 
1eb52 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1eb53 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b   integrity_check
1eb54 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65   pragma..*/.case
1eb55 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a   OP_IntegrityCk:
1eb56 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1eb57 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1eb58 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a  ed into u.bv */.
1eb59 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20    int nRoot;    
1eb5a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
1eb5b 61 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20  ables to check. 
1eb5c 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74   (Number of root
1eb5d 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 6e   pages.) */.  in
1eb5e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a  t *aRoot;     /*
1eb5f 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61   Array of rootpa
1eb60 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74  ge numbers for t
1eb61 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63  ables to be chec
1eb62 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20  ked */.  int j; 
1eb63 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1eb64 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
1eb65 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a  t nErr;       /*
1eb66 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   Number of error
1eb67 73 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20  s reported */.  
1eb68 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
1eb69 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 65  /* Text of the e
1eb6a 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20  rror report */. 
1eb6b 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20   Mem *pnErr;    
1eb6c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65   /* Register kee
1eb6d 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72  ping track of er
1eb6e 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a  rors remaining *
1eb6f 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  /.#endif /* loca
1eb70 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1eb71 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 0a  d into u.bv */..
1eb72 20 20 75 2e 62 76 2e 6e 52 6f 6f 74 20 3d 20 70    u.bv.nRoot = p
1eb73 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74  Op->p2;.  assert
1eb74 28 20 75 2e 62 76 2e 6e 52 6f 6f 74 3e 30 20 29  ( u.bv.nRoot>0 )
1eb75 3b 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 20 3d  ;.  u.bv.aRoot =
1eb76 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1eb77 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
1eb78 6e 74 29 2a 28 75 2e 62 76 2e 6e 52 6f 6f 74 2b  nt)*(u.bv.nRoot+
1eb79 31 29 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 76  1) );.  if( u.bv
1eb7a 2e 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f  .aRoot==0 ) goto
1eb7b 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72   no_mem;.  asser
1eb7c 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
1eb7d 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1eb7e 20 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72   );.  u.bv.pnErr
1eb7f 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
1eb80 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e  ];.  assert( (u.
1eb81 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20  bv.pnErr->flags 
1eb82 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b  & MEM_Int)!=0 );
1eb83 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 76  .  assert( (u.bv
1eb84 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20  .pnErr->flags & 
1eb85 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
1eb86 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31  b))==0 );.  pIn1
1eb87 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
1eb88 5d 3b 0a 20 20 66 6f 72 28 75 2e 62 76 2e 6a 3d  ];.  for(u.bv.j=
1eb89 30 3b 20 75 2e 62 76 2e 6a 3c 75 2e 62 76 2e 6e  0; u.bv.j<u.bv.n
1eb8a 52 6f 6f 74 3b 20 75 2e 62 76 2e 6a 2b 2b 29 7b  Root; u.bv.j++){
1eb8b 0a 20 20 20 20 75 2e 62 76 2e 61 52 6f 6f 74 5b  .    u.bv.aRoot[
1eb8c 75 2e 62 76 2e 6a 5d 20 3d 20 28 69 6e 74 29 73  u.bv.j] = (int)s
1eb8d 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1eb8e 75 65 28 26 70 49 6e 31 5b 75 2e 62 76 2e 6a 5d  ue(&pIn1[u.bv.j]
1eb8f 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 76 2e 61 52  );.  }.  u.bv.aR
1eb90 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d 20 30 3b  oot[u.bv.j] = 0;
1eb91 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1eb92 70 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  p5<db->nDb );.  
1eb93 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
1eb94 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d  eMask & (1<<pOp-
1eb95 3e 70 35 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e  >p5))!=0 );.  u.
1eb96 62 76 2e 7a 20 3d 20 73 71 6c 69 74 65 33 42 74  bv.z = sqlite3Bt
1eb97 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63  reeIntegrityChec
1eb98 6b 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70  k(db->aDb[pOp->p
1eb99 35 5d 2e 70 42 74 2c 20 75 2e 62 76 2e 61 52 6f  5].pBt, u.bv.aRo
1eb9a 6f 74 2c 20 75 2e 62 76 2e 6e 52 6f 6f 74 2c 0a  ot, u.bv.nRoot,.
1eb9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb9d 20 28 69 6e 74 29 75 2e 62 76 2e 70 6e 45 72 72   (int)u.bv.pnErr
1eb9e 2d 3e 75 2e 69 2c 20 26 75 2e 62 76 2e 6e 45 72  ->u.i, &u.bv.nEr
1eb9f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  r);.  sqlite3DbF
1eba0 72 65 65 28 64 62 2c 20 75 2e 62 76 2e 61 52 6f  ree(db, u.bv.aRo
1eba1 6f 74 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72  ot);.  u.bv.pnEr
1eba2 72 2d 3e 75 2e 69 20 2d 3d 20 75 2e 62 76 2e 6e  r->u.i -= u.bv.n
1eba3 45 72 72 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  Err;.  sqlite3Vd
1eba4 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49 6e  beMemSetNull(pIn
1eba5 31 29 3b 0a 20 20 69 66 28 20 75 2e 62 76 2e 6e  1);.  if( u.bv.n
1eba6 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  Err==0 ){.    as
1eba7 73 65 72 74 28 20 75 2e 62 76 2e 7a 3d 3d 30 20  sert( u.bv.z==0 
1eba8 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75  );.  }else if( u
1eba9 2e 62 76 2e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  .bv.z==0 ){.    
1ebaa 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
1ebab 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
1ebac 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
1ebad 49 6e 31 2c 20 75 2e 62 76 2e 7a 2c 20 2d 31 2c  In1, u.bv.z, -1,
1ebae 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73 71   SQLITE_UTF8, sq
1ebaf 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d  lite3_free);.  }
1ebb0 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
1ebb1 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20  OBSIZE(pIn1);.  
1ebb2 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1ebb3 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31 2c 20  eEncoding(pIn1, 
1ebb4 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65  encoding);.  bre
1ebb5 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1ebb6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
1ebb7 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a  GRITY_CHECK */..
1ebb8 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65  /* Opcode: RowSe
1ebb9 74 41 64 64 20 50 31 20 50 32 20 2a 20 2a 20 2a  tAdd P1 P2 * * *
1ebba 0a 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68  .**.** Insert th
1ebbb 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  e integer value 
1ebbc 68 65 6c 64 20 62 79 20 72 65 67 69 73 74 65 72  held by register
1ebbd 20 50 32 20 69 6e 74 6f 20 61 20 62 6f 6f 6c 65   P2 into a boole
1ebbe 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 65 6c 64  an index.** held
1ebbf 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
1ebc0 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73 65 72 74  .**.** An assert
1ebc1 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 50 32 20  ion fails if P2 
1ebc2 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65  is not an intege
1ebc3 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f  r..*/.case OP_Ro
1ebc4 77 53 65 74 41 64 64 3a 20 7b 20 20 20 20 20 20  wSetAdd: {      
1ebc5 20 2f 2a 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a   /* in1, in2 */.
1ebc6 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
1ebc7 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32 20  Op->p1];.  pIn2 
1ebc8 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  = &aMem[pOp->p2]
1ebc9 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 6e  ;.  assert( (pIn
1ebca 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  2->flags & MEM_I
1ebcb 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  nt)!=0 );.  if( 
1ebcc 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
1ebcd 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b  EM_RowSet)==0 ){
1ebce 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ebcf 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e  MemSetRowSet(pIn
1ebd0 31 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e  1);.    if( (pIn
1ebd1 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  1->flags & MEM_R
1ebd2 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f  owSet)==0 ) goto
1ebd3 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73   no_mem;.  }.  s
1ebd4 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65  qlite3RowSetInse
1ebd5 72 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53  rt(pIn1->u.pRowS
1ebd6 65 74 2c 20 70 49 6e 32 2d 3e 75 2e 69 29 3b 0a  et, pIn2->u.i);.
1ebd7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1ebd8 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 52 65 61  pcode: RowSetRea
1ebd9 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  d P1 P2 P3 * *.*
1ebda 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
1ebdb 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20   smallest value 
1ebdc 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69 6e 64  from boolean ind
1ebdd 65 78 20 50 31 20 61 6e 64 20 70 75 74 20 74 68  ex P1 and put th
1ebde 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a  at value into.**
1ebdf 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 20 4f   register P3.  O
1ebe0 72 2c 20 69 66 20 62 6f 6f 6c 65 61 6e 20 69 6e  r, if boolean in
1ebe1 64 65 78 20 50 31 20 69 73 20 69 6e 69 74 69 61  dex P1 is initia
1ebe2 6c 6c 79 20 65 6d 70 74 79 2c 20 6c 65 61 76 65  lly empty, leave
1ebe3 20 50 33 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64   P3.** unchanged
1ebe4 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 69 6e 73   and jump to ins
1ebe5 74 72 75 63 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a  truction P2..*/.
1ebe6 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 52 65  case OP_RowSetRe
1ebe7 61 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a  ad: {       /* j
1ebe8 75 6d 70 2c 20 69 6e 31 2c 20 6f 75 74 33 20 2a  ump, in1, out3 *
1ebe9 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1ebea 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ebeb 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20  d into u.bw */. 
1ebec 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64 69 66   i64 val;.#endif
1ebed 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ebee 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ebef 2e 62 77 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46  .bw */.  CHECK_F
1ebf0 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  OR_INTERRUPT;.  
1ebf1 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1ebf2 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28 70 49  ->p1];.  if( (pI
1ebf3 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1ebf4 52 6f 77 53 65 74 29 3d 3d 30 0a 20 20 20 7c 7c  RowSet)==0.   ||
1ebf5 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65   sqlite3RowSetNe
1ebf6 78 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53  xt(pIn1->u.pRowS
1ebf7 65 74 2c 20 26 75 2e 62 77 2e 76 61 6c 29 3d 3d  et, &u.bw.val)==
1ebf8 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  0.  ){.    /* Th
1ebf9 65 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20  e boolean index 
1ebfa 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20  is empty */.    
1ebfb 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1ebfc 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 20  tNull(pIn1);.   
1ebfd 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1ebfe 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
1ebff 2f 2a 20 41 20 76 61 6c 75 65 20 77 61 73 20 70  /* A value was p
1ec00 75 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 69  ulled from the i
1ec01 6e 64 65 78 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ndex */.    sqli
1ec02 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
1ec03 36 34 28 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  64(&aMem[pOp->p3
1ec04 5d 2c 20 75 2e 62 77 2e 76 61 6c 29 3b 0a 20 20  ], u.bw.val);.  
1ec05 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1ec06 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 54   Opcode: RowSetT
1ec07 65 73 74 20 50 31 20 50 32 20 50 33 20 50 34 0a  est P1 P2 P3 P4.
1ec08 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50  **.** Register P
1ec09 33 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  3 is assumed to 
1ec0a 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20 69 6e  hold a 64-bit in
1ec0b 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49 66 20  teger value. If 
1ec0c 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 63  register P1.** c
1ec0d 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53 65 74  ontains a RowSet
1ec0e 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61 74   object and that
1ec0f 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 63   RowSet object c
1ec10 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 76  ontains.** the v
1ec11 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50 33 2c  alue held in P3,
1ec12 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73 74 65   jump to registe
1ec13 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c  r P2. Otherwise,
1ec14 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a 20 69   insert the.** i
1ec15 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69 6e 74  nteger in P3 int
1ec16 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61 6e 64  o the RowSet and
1ec17 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f 20   continue on to 
1ec18 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70 63 6f  the.** next opco
1ec19 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f  de..**.** The Ro
1ec1a 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73 20 6f  wSet object is o
1ec1b 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74 68 65  ptimized for the
1ec1c 20 63 61 73 65 20 77 68 65 72 65 20 73 75 63 63   case where succ
1ec1d 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a 20 6f  essive sets.** o
1ec1e 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68 65 72  f integers, wher
1ec1f 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e 74 61  e each set conta
1ec20 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61 74 65  ins no duplicate
1ec21 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a 20 6f  s. Each set.** o
1ec22 66 20 76 61 6c 75 65 73 20 69 73 20 69 64 65 6e  f values is iden
1ec23 74 69 66 69 65 64 20 62 79 20 61 20 75 6e 69 71  tified by a uniq
1ec24 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54 68 65  ue P4 value. The
1ec25 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20 6d 75   first set.** mu
1ec26 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c 20 74  st have P4==0, t
1ec27 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50 34 3d  he final set P4=
1ec28 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62 65 20  -1.  P4 must be 
1ec29 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a 2a 20  either -1 or.** 
1ec2a 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 46  non-negative.  F
1ec2b 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  or non-negative 
1ec2c 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f 6e 6c  values of P4 onl
1ec2d 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a 2a 2a  y the lower 4.**
1ec2e 20 62 69 74 73 20 61 72 65 20 73 69 67 6e 69 66   bits are signif
1ec2f 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  icant..**.** Thi
1ec30 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d 69 7a  s allows optimiz
1ec31 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68 65 6e  ations: (a) when
1ec32 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69 73 20   P4==0 there is 
1ec33 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74 0a  no need to test.
1ec34 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20 6f 62  ** the rowset ob
1ec35 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61 73 20  ject for P3, as 
1ec36 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  it is guaranteed
1ec37 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20   not to contain 
1ec38 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65 6e 20  it,.** (b) when 
1ec39 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69 73 20  P4==-1 there is 
1ec3a 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73 65 72  no need to inser
1ec3b 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61 73 20  t the value, as 
1ec3c 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72  it will.** never
1ec3d 20 62 65 20 74 65 73 74 65 64 20 66 6f 72 2c 20   be tested for, 
1ec3e 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61 20 76  and (c) when a v
1ec3f 61 6c 75 65 20 74 68 61 74 20 69 73 20 70 61 72  alue that is par
1ec40 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a 2a 2a  t of set X is.**
1ec41 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65 72 65   inserted, there
1ec42 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 73   is no need to s
1ec43 65 61 72 63 68 20 74 6f 20 73 65 65 20 69 66 20  earch to see if 
1ec44 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 77  the same value w
1ec45 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  as.** previously
1ec46 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61 72   inserted as par
1ec47 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e 6c 79  t of set X (only
1ec48 20 69 66 20 69 74 20 77 61 73 20 70 72 65 76 69   if it was previ
1ec49 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65  ously.** inserte
1ec4a 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 6f 6d  d as part of som
1ec4b 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a 2a 2f  e other set)..*/
1ec4c 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 54  .case OP_RowSetT
1ec4d 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  est: {          
1ec4e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
1ec4f 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
1ec50 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1ec51 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ec52 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a 20 20 69  into u.bx */.  i
1ec53 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e 74 20 65  nt iSet;.  int e
1ec54 78 69 73 74 73 3b 0a 23 65 6e 64 69 66 20 2f 2a  xists;.#endif /*
1ec55 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1ec56 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78   moved into u.bx
1ec57 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61   */..  pIn1 = &a
1ec58 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1ec59 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn3 = &aMem[pOp
1ec5a 2d 3e 70 33 5d 3b 0a 20 20 75 2e 62 78 2e 69 53  ->p3];.  u.bx.iS
1ec5b 65 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a  et = pOp->p4.i;.
1ec5c 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e    assert( pIn3->
1ec5d 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b  flags&MEM_Int );
1ec5e 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
1ec5f 69 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65  is anything othe
1ec60 72 20 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20  r than a rowset 
1ec61 6f 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79  object in memory
1ec62 20 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 64   cell P1,.  ** d
1ec63 65 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64  elete it now and
1ec64 20 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77   initialize P1 w
1ec65 69 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77  ith an empty row
1ec66 73 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  set.  */.  if( (
1ec67 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1ec68 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a  M_RowSet)==0 ){.
1ec69 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1ec6a 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e 31  emSetRowSet(pIn1
1ec6b 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31  );.    if( (pIn1
1ec6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
1ec6d 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20  wSet)==0 ) goto 
1ec6e 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61  no_mem;.  }..  a
1ec6f 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1ec70 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a  pe==P4_INT32 );.
1ec71 20 20 61 73 73 65 72 74 28 20 75 2e 62 78 2e 69    assert( u.bx.i
1ec72 53 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 62 78 2e  Set==-1 || u.bx.
1ec73 69 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28  iSet>=0 );.  if(
1ec74 20 75 2e 62 78 2e 69 53 65 74 20 29 7b 0a 20 20   u.bx.iSet ){.  
1ec75 20 20 75 2e 62 78 2e 65 78 69 73 74 73 20 3d 20    u.bx.exists = 
1ec76 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73  sqlite3RowSetTes
1ec77 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65  t(pIn1->u.pRowSe
1ec78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
1ec79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec7a 20 20 28 75 38 29 28 75 2e 62 78 2e 69 53 65 74    (u8)(u.bx.iSet
1ec7b 3e 3d 30 20 3f 20 75 2e 62 78 2e 69 53 65 74 20  >=0 ? u.bx.iSet 
1ec7c 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a 20  & 0xf : 0xff),. 
1ec7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
1ec7f 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69 66  n3->u.i);.    if
1ec80 28 20 75 2e 62 78 2e 65 78 69 73 74 73 20 29 7b  ( u.bx.exists ){
1ec81 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
1ec82 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62  >p2 - 1;.      b
1ec83 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1ec84 20 20 69 66 28 20 75 2e 62 78 2e 69 53 65 74 3e    if( u.bx.iSet>
1ec85 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1ec86 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49  3RowSetInsert(pI
1ec87 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70  n1->u.pRowSet, p
1ec88 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a 20  In3->u.i);.  }. 
1ec89 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e   break;.}...#ifn
1ec8a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ec8b 54 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70 63 6f  TRIGGER../* Opco
1ec8c 64 65 3a 20 50 72 6f 67 72 61 6d 20 50 31 20 50  de: Program P1 P
1ec8d 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1ec8e 45 78 65 63 75 74 65 20 74 68 65 20 74 72 69 67  Execute the trig
1ec8f 67 65 72 20 70 72 6f 67 72 61 6d 20 70 61 73 73  ger program pass
1ec90 65 64 20 61 73 20 50 34 20 28 74 79 70 65 20 50  ed as P4 (type P
1ec91 34 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e 20 0a  4_SUBPROGRAM). .
1ec92 2a 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69 6e  **.** P1 contain
1ec93 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  s the address of
1ec94 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
1ec95 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
1ec96 68 65 20 66 69 72 73 74 20 6d 65 6d 6f 72 79 20  he first memory 
1ec97 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20 61  .** cell in an a
1ec98 72 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 75  rray of values u
1ec99 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73  sed as arguments
1ec9a 20 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f 67   to the sub-prog
1ec9b 72 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f 6e 74  ram. P2 .** cont
1ec9c 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 73  ains the address
1ec9d 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 74   to jump to if t
1ec9e 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 74  he sub-program t
1ec9f 68 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45 20  hrows an IGNORE 
1eca0 0a 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75 73  .** exception us
1eca1 69 6e 67 20 74 68 65 20 52 41 49 53 45 28 29 20  ing the RAISE() 
1eca2 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73 74  function. Regist
1eca3 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74  er P3 contains t
1eca4 68 65 20 61 64 64 72 65 73 73 20 0a 2a 2a 20 6f  he address .** o
1eca5 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  f a memory cell 
1eca6 69 6e 20 74 68 69 73 20 28 74 68 65 20 70 61 72  in this (the par
1eca7 65 6e 74 29 20 56 4d 20 74 68 61 74 20 69 73 20  ent) VM that is 
1eca8 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  used to allocate
1eca9 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20   the .** memory 
1ecaa 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20  required by the 
1ecab 73 75 62 2d 76 64 62 65 20 61 74 20 72 75 6e 74  sub-vdbe at runt
1ecac 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  ime..**.** P4 is
1ecad 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1ecae 65 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e 67 20  e VM containing 
1ecaf 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  the trigger prog
1ecb0 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ram..*/.case OP_
1ecb1 50 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20 20 20  Program: {      
1ecb2 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66    /* jump */.#if
1ecb3 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1ecb4 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1ecb5 6f 20 75 2e 62 79 20 2a 2f 0a 20 20 69 6e 74 20  o u.by */.  int 
1ecb6 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  nMem;           
1ecb7 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1ecb8 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72   memory register
1ecb9 73 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61  s for sub-progra
1ecba 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  m */.  int nByte
1ecbb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1ecbc 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 69  * Bytes of runti
1ecbd 6d 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65  me space require
1ecbe 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61  d for sub-progra
1ecbf 6d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 74 3b  m */.  Mem *pRt;
1ecc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ecc1 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c  * Register to al
1ecc2 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73  locate runtime s
1ecc3 70 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  pace */.  Mem *p
1ecc4 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
1ecc5 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65    /* Used to ite
1ecc6 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d  rate through mem
1ecc7 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 4d  ory cells */.  M
1ecc8 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20  em *pEnd;       
1ecc9 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d         /* Last m
1ecca 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65  emory cell in ne
1eccb 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 56 64 62  w array */.  Vdb
1eccc 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20  eFrame *pFrame; 
1eccd 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65       /* New vdbe
1ecce 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74   frame to execut
1eccf 65 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50 72 6f  e in */.  SubPro
1ecd0 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20  gram *pProgram; 
1ecd1 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d    /* Sub-program
1ecd2 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20   to execute */. 
1ecd3 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20   void *t;       
1ecd4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65           /* Toke
1ecd5 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72  n identifying tr
1ecd6 69 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20  igger */.#endif 
1ecd7 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ecd8 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ecd9 62 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 70 50  by */..  u.by.pP
1ecda 72 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e 70 34  rogram = pOp->p4
1ecdb 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75 2e 62  .pProgram;.  u.b
1ecdc 79 2e 70 52 74 20 3d 20 26 61 4d 65 6d 5b 70 4f  y.pRt = &aMem[pO
1ecdd 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74  p->p3];.  assert
1ecde 28 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d  ( u.by.pProgram-
1ecdf 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20  >nOp>0 );..  /* 
1ece0 49 66 20 74 68 65 20 70 35 20 66 6c 61 67 20 69  If the p5 flag i
1ece1 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20 72 65  s clear, then re
1ece2 63 75 72 73 69 76 65 20 69 6e 76 6f 63 61 74 69  cursive invocati
1ece3 6f 6e 20 6f 66 20 74 72 69 67 67 65 72 73 20 69  on of triggers i
1ece4 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 20  s.  ** disabled 
1ece5 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f  for backwards co
1ece6 6d 70 61 74 69 62 69 6c 69 74 79 20 28 70 35 20  mpatibility (p5 
1ece7 69 73 20 73 65 74 20 69 66 20 74 68 69 73 20 73  is set if this s
1ece8 75 62 2d 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20  ub-program.  ** 
1ece9 69 73 20 72 65 61 6c 6c 79 20 61 20 74 72 69 67  is really a trig
1ecea 67 65 72 2c 20 6e 6f 74 20 61 20 66 6f 72 65 69  ger, not a forei
1eceb 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 2c 20 61  gn key action, a
1ecec 6e 64 20 74 68 65 20 66 6c 61 67 20 73 65 74 0a  nd the flag set.
1eced 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 65 64    ** and cleared
1ecee 20 62 79 20 74 68 65 20 22 50 52 41 47 4d 41 20   by the "PRAGMA 
1ecef 72 65 63 75 72 73 69 76 65 5f 74 72 69 67 67 65  recursive_trigge
1ecf0 72 73 22 20 63 6f 6d 6d 61 6e 64 20 69 73 20 63  rs" command is c
1ecf1 6c 65 61 72 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  lear)..  **.  **
1ecf2 20 49 74 20 69 73 20 72 65 63 75 72 73 69 76 65   It is recursive
1ecf3 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
1ecf4 72 69 67 67 65 72 73 2c 20 61 74 20 74 68 65 20  riggers, at the 
1ecf5 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68 61 74 20  SQL level, that 
1ecf6 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 64  is.  ** disabled
1ecf7 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 20  . In some cases 
1ecf8 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72  a single trigger
1ecf9 20 6d 61 79 20 67 65 6e 65 72 61 74 65 20 6d 6f   may generate mo
1ecfa 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20 20 2a 2a  re than one.  **
1ecfb 20 53 75 62 50 72 6f 67 72 61 6d 20 28 69 66 20   SubProgram (if 
1ecfc 74 68 65 20 74 72 69 67 67 65 72 20 6d 61 79 20  the trigger may 
1ecfd 62 65 20 65 78 65 63 75 74 65 64 20 77 69 74 68  be executed with
1ecfe 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64   more than one d
1ecff 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 4f 4e  ifferent.  ** ON
1ed00 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69   CONFLICT algori
1ed01 74 68 6d 29 2e 20 53 75 62 50 72 6f 67 72 61 6d  thm). SubProgram
1ed02 20 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f   structures asso
1ed03 63 69 61 74 65 64 20 77 69 74 68 20 61 0a 20 20  ciated with a.  
1ed04 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65  ** single trigge
1ed05 72 20 61 6c 6c 20 68 61 76 65 20 74 68 65 20 73  r all have the s
1ed06 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ame value for th
1ed07 65 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b  e SubProgram.tok
1ed08 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65  en.  ** variable
1ed09 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 70 2d  .  */.  if( pOp-
1ed0a 3e 70 35 20 29 7b 0a 20 20 20 20 75 2e 62 79 2e  >p5 ){.    u.by.
1ed0b 74 20 3d 20 75 2e 62 79 2e 70 50 72 6f 67 72 61  t = u.by.pProgra
1ed0c 6d 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f  m->token;.    fo
1ed0d 72 28 75 2e 62 79 2e 70 46 72 61 6d 65 3d 70 2d  r(u.by.pFrame=p-
1ed0e 3e 70 46 72 61 6d 65 3b 20 75 2e 62 79 2e 70 46  >pFrame; u.by.pF
1ed0f 72 61 6d 65 20 26 26 20 75 2e 62 79 2e 70 46 72  rame && u.by.pFr
1ed10 61 6d 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e 62 79  ame->token!=u.by
1ed11 2e 74 3b 20 75 2e 62 79 2e 70 46 72 61 6d 65 3d  .t; u.by.pFrame=
1ed12 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 70 50 61  u.by.pFrame->pPa
1ed13 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 75  rent);.    if( u
1ed14 2e 62 79 2e 70 46 72 61 6d 65 20 29 20 62 72 65  .by.pFrame ) bre
1ed15 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  ak;.  }..  if( p
1ed16 2d 3e 6e 46 72 61 6d 65 3e 3d 64 62 2d 3e 61 4c  ->nFrame>=db->aL
1ed17 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1ed18 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 5d  T_TRIGGER_DEPTH]
1ed19 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
1ed1a 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73  ITE_ERROR;.    s
1ed1b 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1ed1c 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1ed1d 20 22 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c   "too many level
1ed1e 73 20 6f 66 20 74 72 69 67 67 65 72 20 72 65 63  s of trigger rec
1ed1f 75 72 73 69 6f 6e 22 29 3b 0a 20 20 20 20 62 72  ursion");.    br
1ed20 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  eak;.  }..  /* R
1ed21 65 67 69 73 74 65 72 20 75 2e 62 79 2e 70 52 74  egister u.by.pRt
1ed22 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72   is used to stor
1ed23 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71  e the memory req
1ed24 75 69 72 65 64 20 74 6f 20 73 61 76 65 20 74 68  uired to save th
1ed25 65 20 73 74 61 74 65 0a 20 20 2a 2a 20 6f 66 20  e state.  ** of 
1ed26 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 67  the current prog
1ed27 72 61 6d 2c 20 61 6e 64 20 74 68 65 20 6d 65 6d  ram, and the mem
1ed28 6f 72 79 20 72 65 71 75 69 72 65 64 20 61 74 20  ory required at 
1ed29 72 75 6e 74 69 6d 65 20 74 6f 20 65 78 65 63 75  runtime to execu
1ed2a 74 65 0a 20 20 2a 2a 20 74 68 65 20 74 72 69 67  te.  ** the trig
1ed2b 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20  ger program. If 
1ed2c 74 68 69 73 20 74 72 69 67 67 65 72 20 68 61 73  this trigger has
1ed2d 20 62 65 65 6e 20 66 69 72 65 64 20 62 65 66 6f   been fired befo
1ed2e 72 65 2c 20 74 68 65 6e 20 75 2e 62 79 2e 70 52  re, then u.by.pR
1ed2f 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64  t.  ** is alread
1ed30 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4f 74 68  y allocated. Oth
1ed31 65 72 77 69 73 65 2c 20 69 74 20 6d 75 73 74 20  erwise, it must 
1ed32 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  be initialized. 
1ed33 20 2a 2f 0a 20 20 69 66 28 20 28 75 2e 62 79 2e   */.  if( (u.by.
1ed34 70 52 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 46  pRt->flags&MEM_F
1ed35 72 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  rame)==0 ){.    
1ed36 2f 2a 20 53 75 62 50 72 6f 67 72 61 6d 2e 6e 4d  /* SubProgram.nM
1ed37 65 6d 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  em is set to the
1ed38 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72   number of memor
1ed39 79 20 63 65 6c 6c 73 20 75 73 65 64 20 62 79 20  y cells used by 
1ed3a 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f 67 72  the.    ** progr
1ed3b 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 53 75 62  am stored in Sub
1ed3c 50 72 6f 67 72 61 6d 2e 61 4f 70 2e 20 41 73 20  Program.aOp. As 
1ed3d 77 65 6c 6c 20 61 73 20 74 68 65 73 65 2c 20 6f  well as these, o
1ed3e 6e 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a  ne memory.    **
1ed3f 20 63 65 6c 6c 20 69 73 20 72 65 71 75 69 72 65   cell is require
1ed40 64 20 66 6f 72 20 65 61 63 68 20 63 75 72 73 6f  d for each curso
1ed41 72 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72  r used by the pr
1ed42 6f 67 72 61 6d 2e 20 53 65 74 20 6c 6f 63 61 6c  ogram. Set local
1ed43 0a 20 20 20 20 2a 2a 20 76 61 72 69 61 62 6c 65  .    ** variable
1ed44 20 75 2e 62 79 2e 6e 4d 65 6d 20 28 61 6e 64 20   u.by.nMem (and 
1ed45 6c 61 74 65 72 2c 20 56 64 62 65 46 72 61 6d 65  later, VdbeFrame
1ed46 2e 6e 43 68 69 6c 64 4d 65 6d 29 20 74 6f 20 74  .nChildMem) to t
1ed47 68 69 73 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a  his value..    *
1ed48 2f 0a 20 20 20 20 75 2e 62 79 2e 6e 4d 65 6d 20  /.    u.by.nMem 
1ed49 3d 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d  = u.by.pProgram-
1ed4a 3e 6e 4d 65 6d 20 2b 20 75 2e 62 79 2e 70 50 72  >nMem + u.by.pPr
1ed4b 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20  ogram->nCsr;.   
1ed4c 20 75 2e 62 79 2e 6e 42 79 74 65 20 3d 20 52 4f   u.by.nByte = RO
1ed4d 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64 62 65  UND8(sizeof(Vdbe
1ed4e 46 72 61 6d 65 29 29 0a 20 20 20 20 20 20 20 20  Frame)).        
1ed4f 20 20 20 20 20 20 2b 20 75 2e 62 79 2e 6e 4d 65        + u.by.nMe
1ed50 6d 20 2a 20 73 69 7a 65 6f 66 28 4d 65 6d 29 0a  m * sizeof(Mem).
1ed51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20                + 
1ed52 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e  u.by.pProgram->n
1ed53 43 73 72 20 2a 20 73 69 7a 65 6f 66 28 56 64 62  Csr * sizeof(Vdb
1ed54 65 43 75 72 73 6f 72 20 2a 29 3b 0a 20 20 20 20  eCursor *);.    
1ed55 75 2e 62 79 2e 70 46 72 61 6d 65 20 3d 20 73 71  u.by.pFrame = sq
1ed56 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
1ed57 6f 28 64 62 2c 20 75 2e 62 79 2e 6e 42 79 74 65  o(db, u.by.nByte
1ed58 29 3b 0a 20 20 20 20 69 66 28 20 21 75 2e 62 79  );.    if( !u.by
1ed59 2e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20  .pFrame ){.     
1ed5a 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1ed5b 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
1ed5c 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 75 2e  dbeMemRelease(u.
1ed5d 62 79 2e 70 52 74 29 3b 0a 20 20 20 20 75 2e 62  by.pRt);.    u.b
1ed5e 79 2e 70 52 74 2d 3e 66 6c 61 67 73 20 3d 20 4d  y.pRt->flags = M
1ed5f 45 4d 5f 46 72 61 6d 65 3b 0a 20 20 20 20 75 2e  EM_Frame;.    u.
1ed60 62 79 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65  by.pRt->u.pFrame
1ed61 20 3d 20 75 2e 62 79 2e 70 46 72 61 6d 65 3b 0a   = u.by.pFrame;.
1ed62 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65  .    u.by.pFrame
1ed63 2d 3e 76 20 3d 20 70 3b 0a 20 20 20 20 75 2e 62  ->v = p;.    u.b
1ed64 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64  y.pFrame->nChild
1ed65 4d 65 6d 20 3d 20 75 2e 62 79 2e 6e 4d 65 6d 3b  Mem = u.by.nMem;
1ed66 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65  .    u.by.pFrame
1ed67 2d 3e 6e 43 68 69 6c 64 43 73 72 20 3d 20 75 2e  ->nChildCsr = u.
1ed68 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73  by.pProgram->nCs
1ed69 72 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61  r;.    u.by.pFra
1ed6a 6d 65 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20  me->pc = pc;.   
1ed6b 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 61 4d   u.by.pFrame->aM
1ed6c 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b 0a 20 20  em = p->aMem;.  
1ed6d 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e    u.by.pFrame->n
1ed6e 4d 65 6d 20 3d 20 70 2d 3e 6e 4d 65 6d 3b 0a 20  Mem = p->nMem;. 
1ed6f 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e     u.by.pFrame->
1ed70 61 70 43 73 72 20 3d 20 70 2d 3e 61 70 43 73 72  apCsr = p->apCsr
1ed71 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d  ;.    u.by.pFram
1ed72 65 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 70 2d 3e  e->nCursor = p->
1ed73 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 75 2e 62  nCursor;.    u.b
1ed74 79 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 20 3d 20  y.pFrame->aOp = 
1ed75 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 75 2e 62 79  p->aOp;.    u.by
1ed76 2e 70 46 72 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70  .pFrame->nOp = p
1ed77 2d 3e 6e 4f 70 3b 0a 20 20 20 20 75 2e 62 79 2e  ->nOp;.    u.by.
1ed78 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 20 3d 20  pFrame->token = 
1ed79 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 74  u.by.pProgram->t
1ed7a 6f 6b 65 6e 3b 0a 0a 20 20 20 20 75 2e 62 79 2e  oken;..    u.by.
1ed7b 70 45 6e 64 20 3d 20 26 56 64 62 65 46 72 61 6d  pEnd = &VdbeFram
1ed7c 65 4d 65 6d 28 75 2e 62 79 2e 70 46 72 61 6d 65  eMem(u.by.pFrame
1ed7d 29 5b 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e  )[u.by.pFrame->n
1ed7e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 20 20 66  ChildMem];.    f
1ed7f 6f 72 28 75 2e 62 79 2e 70 4d 65 6d 3d 56 64 62  or(u.by.pMem=Vdb
1ed80 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 79 2e 70  eFrameMem(u.by.p
1ed81 46 72 61 6d 65 29 3b 20 75 2e 62 79 2e 70 4d 65  Frame); u.by.pMe
1ed82 6d 21 3d 75 2e 62 79 2e 70 45 6e 64 3b 20 75 2e  m!=u.by.pEnd; u.
1ed83 62 79 2e 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20  by.pMem++){.    
1ed84 20 20 75 2e 62 79 2e 70 4d 65 6d 2d 3e 66 6c 61    u.by.pMem->fla
1ed85 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1ed86 20 20 20 20 20 75 2e 62 79 2e 70 4d 65 6d 2d 3e       u.by.pMem->
1ed87 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 7d 0a 20  db = db;.    }. 
1ed88 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 79   }else{.    u.by
1ed89 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 79 2e 70  .pFrame = u.by.p
1ed8a 52 74 2d 3e 75 2e 70 46 72 61 6d 65 3b 0a 20 20  Rt->u.pFrame;.  
1ed8b 20 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e 70    assert( u.by.p
1ed8c 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e  Program->nMem+u.
1ed8d 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73  by.pProgram->nCs
1ed8e 72 3d 3d 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e  r==u.by.pFrame->
1ed8f 6e 43 68 69 6c 64 4d 65 6d 20 29 3b 0a 20 20 20  nChildMem );.   
1ed90 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e 70 50   assert( u.by.pP
1ed91 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e  rogram->nCsr==u.
1ed92 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c  by.pFrame->nChil
1ed93 64 43 73 72 20 29 3b 0a 20 20 20 20 61 73 73 65  dCsr );.    asse
1ed94 72 74 28 20 70 63 3d 3d 75 2e 62 79 2e 70 46 72  rt( pc==u.by.pFr
1ed95 61 6d 65 2d 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a  ame->pc );.  }..
1ed96 20 20 70 2d 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20    p->nFrame++;. 
1ed97 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 70 50   u.by.pFrame->pP
1ed98 61 72 65 6e 74 20 3d 20 70 2d 3e 70 46 72 61 6d  arent = p->pFram
1ed99 65 3b 0a 20 20 75 2e 62 79 2e 70 46 72 61 6d 65  e;.  u.by.pFrame
1ed9a 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62  ->lastRowid = db
1ed9b 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 75  ->lastRowid;.  u
1ed9c 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 61  .by.pFrame->nCha
1ed9d 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 61 6e 67 65  nge = p->nChange
1ed9e 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d  ;.  p->nChange =
1ed9f 20 30 3b 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20   0;.  p->pFrame 
1eda0 3d 20 75 2e 62 79 2e 70 46 72 61 6d 65 3b 0a 20  = u.by.pFrame;. 
1eda1 20 70 2d 3e 61 4d 65 6d 20 3d 20 61 4d 65 6d 20   p->aMem = aMem 
1eda2 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65 6d 28  = &VdbeFrameMem(
1eda3 75 2e 62 79 2e 70 46 72 61 6d 65 29 5b 2d 31 5d  u.by.pFrame)[-1]
1eda4 3b 0a 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e  ;.  p->nMem = u.
1eda5 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c  by.pFrame->nChil
1eda6 64 4d 65 6d 3b 0a 20 20 70 2d 3e 6e 43 75 72 73  dMem;.  p->nCurs
1eda7 6f 72 20 3d 20 28 75 31 36 29 75 2e 62 79 2e 70  or = (u16)u.by.p
1eda8 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72  Frame->nChildCsr
1eda9 3b 0a 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28  ;.  p->apCsr = (
1edaa 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 29 26 61  VdbeCursor **)&a
1edab 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0a  Mem[p->nMem+1];.
1edac 20 20 70 2d 3e 61 4f 70 20 3d 20 61 4f 70 20 3d    p->aOp = aOp =
1edad 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e   u.by.pProgram->
1edae 61 4f 70 3b 0a 20 20 70 2d 3e 6e 4f 70 20 3d 20  aOp;.  p->nOp = 
1edaf 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e  u.by.pProgram->n
1edb0 4f 70 3b 0a 20 20 70 63 20 3d 20 2d 31 3b 0a 0a  Op;.  pc = -1;..
1edb1 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1edb2 70 63 6f 64 65 3a 20 50 61 72 61 6d 20 50 31 20  pcode: Param P1 
1edb3 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  P2 * * *.**.** T
1edb4 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6f 6e  his opcode is on
1edb5 6c 79 20 65 76 65 72 20 70 72 65 73 65 6e 74 20  ly ever present 
1edb6 69 6e 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20  in sub-programs 
1edb7 63 61 6c 6c 65 64 20 76 69 61 20 74 68 65 20 0a  called via the .
1edb8 2a 2a 20 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e  ** OP_Program in
1edb9 73 74 72 75 63 74 69 6f 6e 2e 20 43 6f 70 79 20  struction. Copy 
1edba 61 20 76 61 6c 75 65 20 63 75 72 72 65 6e 74 6c  a value currentl
1edbb 79 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6d 65  y stored in a me
1edbc 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c 6c 20 6f 66  mory .** cell of
1edbd 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 28 70 61   the calling (pa
1edbe 72 65 6e 74 29 20 66 72 61 6d 65 20 74 6f 20 63  rent) frame to c
1edbf 65 6c 6c 20 50 32 20 69 6e 20 74 68 65 20 63 75  ell P2 in the cu
1edc0 72 72 65 6e 74 20 66 72 61 6d 65 73 20 0a 2a 2a  rrent frames .**
1edc1 20 61 64 64 72 65 73 73 20 73 70 61 63 65 2e 20   address space. 
1edc2 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
1edc3 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73  trigger programs
1edc4 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 6e   to access the n
1edc5 65 77 2e 2a 20 0a 2a 2a 20 61 6e 64 20 6f 6c 64  ew.* .** and old
1edc6 2e 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a  .* values..**.**
1edc7 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   The address of 
1edc8 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20  the cell in the 
1edc9 70 61 72 65 6e 74 20 66 72 61 6d 65 20 69 73 20  parent frame is 
1edca 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 61 64  determined by ad
1edcb 64 69 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ding.** the valu
1edcc 65 20 6f 66 20 74 68 65 20 50 31 20 61 72 67 75  e of the P1 argu
1edcd 6d 65 6e 74 20 74 6f 20 74 68 65 20 76 61 6c 75  ment to the valu
1edce 65 20 6f 66 20 74 68 65 20 50 31 20 61 72 67 75  e of the P1 argu
1edcf 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 63  ment to the.** c
1edd0 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f 67 72 61  alling OP_Progra
1edd1 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  m instruction..*
1edd2 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 61 6d 3a  /.case OP_Param:
1edd3 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1edd4 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1edd5 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1edd6 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1edd7 65 64 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f 0a  ed into u.bz */.
1edd8 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72    VdbeFrame *pFr
1edd9 61 6d 65 3b 0a 20 20 4d 65 6d 20 2a 70 49 6e 3b  ame;.  Mem *pIn;
1edda 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1eddb 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1eddc 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20   into u.bz */.  
1eddd 75 2e 62 7a 2e 70 46 72 61 6d 65 20 3d 20 70 2d  u.bz.pFrame = p-
1edde 3e 70 46 72 61 6d 65 3b 0a 20 20 75 2e 62 7a 2e  >pFrame;.  u.bz.
1eddf 70 49 6e 20 3d 20 26 75 2e 62 7a 2e 70 46 72 61  pIn = &u.bz.pFra
1ede0 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  me->aMem[pOp->p1
1ede1 20 2b 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e   + u.bz.pFrame->
1ede2 61 4f 70 5b 75 2e 62 7a 2e 70 46 72 61 6d 65 2d  aOp[u.bz.pFrame-
1ede3 3e 70 63 5d 2e 70 31 5d 3b 0a 20 20 73 71 6c 69  >pc].p1];.  sqli
1ede4 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f  te3VdbeMemShallo
1ede5 77 43 6f 70 79 28 70 4f 75 74 2c 20 75 2e 62 7a  wCopy(pOut, u.bz
1ede6 2e 70 49 6e 2c 20 4d 45 4d 5f 45 70 68 65 6d 29  .pIn, MEM_Ephem)
1ede7 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 65  ;.  break;.}..#e
1ede8 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20  ndif /* #ifndef 
1ede9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1edea 47 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  GER */..#ifndef 
1edeb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
1edec 49 47 4e 5f 4b 45 59 0a 2f 2a 20 4f 70 63 6f 64  IGN_KEY./* Opcod
1eded 65 3a 20 46 6b 43 6f 75 6e 74 65 72 20 50 31 20  e: FkCounter P1 
1edee 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49  P2 * * *.**.** I
1edef 6e 63 72 65 6d 65 6e 74 20 61 20 22 63 6f 6e 73  ncrement a "cons
1edf0 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 20  traint counter" 
1edf1 62 79 20 50 32 20 28 50 32 20 6d 61 79 20 62 65  by P2 (P2 may be
1edf2 20 6e 65 67 61 74 69 76 65 20 6f 72 20 70 6f 73   negative or pos
1edf3 69 74 69 76 65 29 2e 0a 2a 2a 20 49 66 20 50 31  itive)..** If P1
1edf4 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
1edf5 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 73 74  e database const
1edf6 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 69 73  raint counter is
1edf7 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 0a 2a 2a   incremented .**
1edf8 20 28 64 65 66 65 72 72 65 64 20 66 6f 72 65 69   (deferred forei
1edf9 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
1edfa 74 73 29 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ts). Otherwise, 
1edfb 69 66 20 50 31 20 69 73 20 7a 65 72 6f 2c 20 74  if P1 is zero, t
1edfc 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  he .** statement
1edfd 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 72   counter is incr
1edfe 65 6d 65 6e 74 65 64 20 28 69 6d 6d 65 64 69 61  emented (immedia
1edff 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  te foreign key c
1ee00 6f 6e 73 74 72 61 69 6e 74 73 29 2e 0a 2a 2f 0a  onstraints)..*/.
1ee01 63 61 73 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 65  case OP_FkCounte
1ee02 72 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  r: {.  if( pOp->
1ee03 70 31 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6e 44  p1 ){.    db->nD
1ee04 65 66 65 72 72 65 64 43 6f 6e 73 20 2b 3d 20 70  eferredCons += p
1ee05 4f 70 2d 3e 70 32 3b 0a 20 20 7d 65 6c 73 65 7b  Op->p2;.  }else{
1ee06 0a 20 20 20 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74  .    p->nFkConst
1ee07 72 61 69 6e 74 20 2b 3d 20 70 4f 70 2d 3e 70 32  raint += pOp->p2
1ee08 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1ee09 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 49  ../* Opcode: FkI
1ee0a 66 5a 65 72 6f 20 50 31 20 50 32 20 2a 20 2a 20  fZero P1 P2 * * 
1ee0b 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  *.**.** This opc
1ee0c 6f 64 65 20 74 65 73 74 73 20 69 66 20 61 20 66  ode tests if a f
1ee0d 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
1ee0e 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73  raint-counter is
1ee0f 20 63 75 72 72 65 6e 74 6c 79 20 7a 65 72 6f 2e   currently zero.
1ee10 0a 2a 2a 20 49 66 20 73 6f 2c 20 6a 75 6d 70 20  .** If so, jump 
1ee11 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50  to instruction P
1ee12 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61  2. Otherwise, fa
1ee13 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1ee14 65 20 6e 65 78 74 20 0a 2a 2a 20 69 6e 73 74 72  e next .** instr
1ee15 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  uction..**.** If
1ee16 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c   P1 is non-zero,
1ee17 20 74 68 65 6e 20 74 68 65 20 6a 75 6d 70 20 69   then the jump i
1ee18 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 64  s taken if the d
1ee19 61 74 61 62 61 73 65 20 63 6f 6e 73 74 72 61 69  atabase constrai
1ee1a 6e 74 2d 63 6f 75 6e 74 65 72 0a 2a 2a 20 69 73  nt-counter.** is
1ee1b 20 7a 65 72 6f 20 28 74 68 65 20 6f 6e 65 20 74   zero (the one t
1ee1c 68 61 74 20 63 6f 75 6e 74 73 20 64 65 66 65 72  hat counts defer
1ee1d 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  red constraint v
1ee1e 69 6f 6c 61 74 69 6f 6e 73 29 2e 20 49 66 20 50  iolations). If P
1ee1f 31 20 69 73 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68  1 is.** zero, th
1ee20 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20  e jump is taken 
1ee21 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  if the statement
1ee22 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e   constraint-coun
1ee23 74 65 72 20 69 73 20 7a 65 72 6f 0a 2a 2a 20 28  ter is zero.** (
1ee24 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67  immediate foreig
1ee25 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
1ee26 20 76 69 6f 6c 61 74 69 6f 6e 73 29 2e 0a 2a 2f   violations)..*/
1ee27 0a 63 61 73 65 20 4f 50 5f 46 6b 49 66 5a 65 72  .case OP_FkIfZer
1ee28 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20  o: {         /* 
1ee29 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 70 4f  jump */.  if( pO
1ee2a 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 69 66 28  p->p1 ){.    if(
1ee2b 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f   db->nDeferredCo
1ee2c 6e 73 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70  ns==0 ) pc = pOp
1ee2d 2d 3e 70 32 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b  ->p2-1;.  }else{
1ee2e 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 46 6b 43  .    if( p->nFkC
1ee2f 6f 6e 73 74 72 61 69 6e 74 3d 3d 30 20 29 20 70  onstraint==0 ) p
1ee30 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20  c = pOp->p2-1;. 
1ee31 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1ee32 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20  ndif /* #ifndef 
1ee33 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
1ee34 49 47 4e 5f 4b 45 59 20 2a 2f 0a 0a 23 69 66 6e  IGN_KEY */..#ifn
1ee35 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ee36 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a  AUTOINCREMENT./*
1ee37 20 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20   Opcode: MemMax 
1ee38 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1ee39 2a 20 50 31 20 69 73 20 61 20 72 65 67 69 73 74  * P1 is a regist
1ee3a 65 72 20 69 6e 20 74 68 65 20 72 6f 6f 74 20 66  er in the root f
1ee3b 72 61 6d 65 20 6f 66 20 74 68 69 73 20 56 4d 20  rame of this VM 
1ee3c 28 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20  (the root frame 
1ee3d 69 73 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20  is.** different 
1ee3e 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74  from the current
1ee3f 20 66 72 61 6d 65 20 69 66 20 74 68 69 73 20 69   frame if this i
1ee40 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 62 65  nstruction is be
1ee41 69 6e 67 20 65 78 65 63 75 74 65 64 0a 2a 2a 20  ing executed.** 
1ee42 77 69 74 68 69 6e 20 61 20 73 75 62 2d 70 72 6f  within a sub-pro
1ee43 67 72 61 6d 29 2e 20 53 65 74 20 74 68 65 20 76  gram). Set the v
1ee44 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72  alue of register
1ee45 20 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d   P1 to the maxim
1ee46 75 6d 20 6f 66 20 0a 2a 2a 20 69 74 73 20 63 75  um of .** its cu
1ee47 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20  rrent value and 
1ee48 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1ee49 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ister P2..**.** 
1ee4a 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1ee4b 20 74 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72   throws an error
1ee4c 20 69 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63   if the memory c
1ee4d 65 6c 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69  ell is not initi
1ee4e 61 6c 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67  ally.** an integ
1ee4f 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d  er..*/.case OP_M
1ee50 65 6d 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20  emMax: {        
1ee51 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20  /* in2 */.#if 0 
1ee52 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ee53 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ee54 2e 63 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49  .ca */.  Mem *pI
1ee55 6e 31 3b 0a 20 20 56 64 62 65 46 72 61 6d 65 20  n1;.  VdbeFrame 
1ee56 2a 70 46 72 61 6d 65 3b 0a 23 65 6e 64 69 66 20  *pFrame;.#endif 
1ee57 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ee58 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ee59 63 61 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  ca */.  if( p->p
1ee5a 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72  Frame ){.    for
1ee5b 28 75 2e 63 61 2e 70 46 72 61 6d 65 3d 70 2d 3e  (u.ca.pFrame=p->
1ee5c 70 46 72 61 6d 65 3b 20 75 2e 63 61 2e 70 46 72  pFrame; u.ca.pFr
1ee5d 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 2e  ame->pParent; u.
1ee5e 63 61 2e 70 46 72 61 6d 65 3d 75 2e 63 61 2e 70  ca.pFrame=u.ca.p
1ee5f 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b  Frame->pParent);
1ee60 0a 20 20 20 20 75 2e 63 61 2e 70 49 6e 31 20 3d  .    u.ca.pIn1 =
1ee61 20 26 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 61   &u.ca.pFrame->a
1ee62 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1ee63 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 63 61 2e  }else{.    u.ca.
1ee64 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1ee65 2d 3e 70 31 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c  ->p1];.  }.  sql
1ee66 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
1ee67 65 72 69 66 79 28 75 2e 63 61 2e 70 49 6e 31 29  erify(u.ca.pIn1)
1ee68 3b 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d  ;.  pIn2 = &aMem
1ee69 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 73 71 6c  [pOp->p2];.  sql
1ee6a 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
1ee6b 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 69  erify(pIn2);.  i
1ee6c 66 28 20 75 2e 63 61 2e 70 49 6e 31 2d 3e 75 2e  f( u.ca.pIn1->u.
1ee6d 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20  i<pIn2->u.i){.  
1ee6e 20 20 75 2e 63 61 2e 70 49 6e 31 2d 3e 75 2e 69    u.ca.pIn1->u.i
1ee6f 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20   = pIn2->u.i;.  
1ee70 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  }.  break;.}.#en
1ee71 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1ee72 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
1ee73 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20   */../* Opcode: 
1ee74 49 66 50 6f 73 20 50 31 20 50 32 20 2a 20 2a 20  IfPos P1 P2 * * 
1ee75 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76  *.**.** If the v
1ee76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72  alue of register
1ee77 20 50 31 20 69 73 20 31 20 6f 72 20 67 72 65 61   P1 is 1 or grea
1ee78 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e  ter, jump to P2.
1ee79 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c  .**.** It is ill
1ee7a 65 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73  egal to use this
1ee7b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20   instruction on 
1ee7c 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20  a register that 
1ee7d 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74  does.** not cont
1ee7e 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20  ain an integer. 
1ee7f 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
1ee80 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  ult will result 
1ee81 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63  if you try..*/.c
1ee82 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a 20 7b 20  ase OP_IfPos: { 
1ee83 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1ee84 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20  in1 */.  pIn1 = 
1ee85 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
1ee86 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
1ee87 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b  flags&MEM_Int );
1ee88 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69  .  if( pIn1->u.i
1ee89 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20  >0 ){.     pc = 
1ee8a 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1ee8b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1ee8c 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20 50 31  Opcode: IfNeg P1
1ee8d 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1ee8e 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  If the value of 
1ee8f 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6c  register P1 is l
1ee90 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 6a  ess than zero, j
1ee91 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a  ump to P2. .**.*
1ee92 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
1ee93 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74  to use this inst
1ee94 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67  ruction on a reg
1ee95 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a  ister that does.
1ee96 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  ** not contain a
1ee97 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61  n integer.  An a
1ee98 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77  ssertion fault w
1ee99 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f  ill result if yo
1ee9a 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  u try..*/.case O
1ee9b 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20 20 20  P_IfNeg: {      
1ee9c 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a    /* jump, in1 *
1ee9d 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  /.  pIn1 = &aMem
1ee9e 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1ee9f 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
1eea0 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66  &MEM_Int );.  if
1eea1 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29 7b  ( pIn1->u.i<0 ){
1eea2 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  .     pc = pOp->
1eea3 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
1eea4 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1eea5 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32 20  e: IfZero P1 P2 
1eea6 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  P3 * *.**.** The
1eea7 20 72 65 67 69 73 74 65 72 20 50 31 20 6d 75 73   register P1 mus
1eea8 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74  t contain an int
1eea9 65 67 65 72 2e 20 20 41 64 64 20 6c 69 74 65 72  eger.  Add liter
1eeaa 61 6c 20 50 33 20 74 6f 20 74 68 65 0a 2a 2a 20  al P3 to the.** 
1eeab 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1eeac 72 20 50 31 2e 20 20 49 66 20 74 68 65 20 72 65  r P1.  If the re
1eead 73 75 6c 74 20 69 73 20 65 78 61 63 74 6c 79 20  sult is exactly 
1eeae 30 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a  0, jump to P2. .
1eeaf 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65  **.** It is ille
1eeb0 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20  gal to use this 
1eeb1 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61  instruction on a
1eeb2 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64   register that d
1eeb3 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61  oes.** not conta
1eeb4 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  in an integer.  
1eeb5 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75  An assertion fau
1eeb6 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  lt will result i
1eeb7 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61  f you try..*/.ca
1eeb8 73 65 20 4f 50 5f 49 66 5a 65 72 6f 3a 20 7b 20  se OP_IfZero: { 
1eeb9 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1eeba 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20  in1 */.  pIn1 = 
1eebb 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
1eebc 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
1eebd 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b  flags&MEM_Int );
1eebe 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20  .  pIn1->u.i += 
1eebf 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 70  pOp->p3;.  if( p
1eec0 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20  In1->u.i==0 ){. 
1eec1 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1eec2 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1eec3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1eec4 20 41 67 67 53 74 65 70 20 2a 20 50 32 20 50 33   AggStep * P2 P3
1eec5 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 45 78 65   P4 P5.**.** Exe
1eec6 63 75 74 65 20 74 68 65 20 73 74 65 70 20 66 75  cute the step fu
1eec7 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67  nction for an ag
1eec8 67 72 65 67 61 74 65 2e 20 20 54 68 65 0a 2a 2a  gregate.  The.**
1eec9 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 50 35   function has P5
1eeca 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 20 50 34   arguments.   P4
1eecb 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1eecc 20 74 68 65 20 46 75 6e 63 44 65 66 0a 2a 2a 20   the FuncDef.** 
1eecd 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 73  structure that s
1eece 70 65 63 69 66 69 65 73 20 74 68 65 20 66 75 6e  pecifies the fun
1eecf 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 65 67 69  ction.  Use regi
1eed0 73 74 65 72 0a 2a 2a 20 50 33 20 61 73 20 74 68  ster.** P3 as th
1eed1 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a  e accumulator..*
1eed2 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61 72 67 75  *.** The P5 argu
1eed3 6d 65 6e 74 73 20 61 72 65 20 74 61 6b 65 6e 20  ments are taken 
1eed4 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32  from register P2
1eed5 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73 75 63 63   and its.** succ
1eed6 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61 73 65 20  essors..*/.case 
1eed7 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b 0a 23 69  OP_AggStep: {.#i
1eed8 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1eed9 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1eeda 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 20 69 6e 74  to u.cb */.  int
1eedb 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d   n;.  int i;.  M
1eedc 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d 65 6d 20  em *pMem;.  Mem 
1eedd 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74 65 33  *pRec;.  sqlite3
1eede 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20  _context ctx;.  
1eedf 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
1eee0 61 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a  apVal;.#endif /*
1eee1 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1eee2 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 62   moved into u.cb
1eee3 20 2a 2f 0a 0a 20 20 75 2e 63 62 2e 6e 20 3d 20   */..  u.cb.n = 
1eee4 70 4f 70 2d 3e 70 35 3b 0a 20 20 61 73 73 65 72  pOp->p5;.  asser
1eee5 74 28 20 75 2e 63 62 2e 6e 3e 3d 30 20 29 3b 0a  t( u.cb.n>=0 );.
1eee6 20 20 75 2e 63 62 2e 70 52 65 63 20 3d 20 26 61    u.cb.pRec = &a
1eee7 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1eee8 75 2e 63 62 2e 61 70 56 61 6c 20 3d 20 70 2d 3e  u.cb.apVal = p->
1eee9 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28  apArg;.  assert(
1eeea 20 75 2e 63 62 2e 61 70 56 61 6c 20 7c 7c 20 75   u.cb.apVal || u
1eeeb 2e 63 62 2e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f  .cb.n==0 );.  fo
1eeec 72 28 75 2e 63 62 2e 69 3d 30 3b 20 75 2e 63 62  r(u.cb.i=0; u.cb
1eeed 2e 69 3c 75 2e 63 62 2e 6e 3b 20 75 2e 63 62 2e  .i<u.cb.n; u.cb.
1eeee 69 2b 2b 2c 20 75 2e 63 62 2e 70 52 65 63 2b 2b  i++, u.cb.pRec++
1eeef 29 7b 0a 20 20 20 20 75 2e 63 62 2e 61 70 56 61  ){.    u.cb.apVa
1eef0 6c 5b 75 2e 63 62 2e 69 5d 20 3d 20 75 2e 63 62  l[u.cb.i] = u.cb
1eef1 2e 70 52 65 63 3b 0a 20 20 20 20 73 71 6c 69 74  .pRec;.    sqlit
1eef2 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79  e3VdbeMemStoreTy
1eef3 70 65 28 75 2e 63 62 2e 70 52 65 63 29 3b 0a 20  pe(u.cb.pRec);. 
1eef4 20 7d 0a 20 20 75 2e 63 62 2e 63 74 78 2e 70 46   }.  u.cb.ctx.pF
1eef5 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46  unc = pOp->p4.pF
1eef6 75 6e 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70  unc;.  assert( p
1eef7 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d  Op->p3>0 && pOp-
1eef8 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1eef9 20 20 75 2e 63 62 2e 63 74 78 2e 70 4d 65 6d 20    u.cb.ctx.pMem 
1eefa 3d 20 75 2e 63 62 2e 70 4d 65 6d 20 3d 20 26 61  = u.cb.pMem = &a
1eefb 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1eefc 75 2e 63 62 2e 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a  u.cb.pMem->n++;.
1eefd 20 20 75 2e 63 62 2e 63 74 78 2e 73 2e 66 6c 61    u.cb.ctx.s.fla
1eefe 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1eeff 20 75 2e 63 62 2e 63 74 78 2e 73 2e 7a 20 3d 20   u.cb.ctx.s.z = 
1ef00 30 3b 0a 20 20 75 2e 63 62 2e 63 74 78 2e 73 2e  0;.  u.cb.ctx.s.
1ef01 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 75  zMalloc = 0;.  u
1ef02 2e 63 62 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d  .cb.ctx.s.xDel =
1ef03 20 30 3b 0a 20 20 75 2e 63 62 2e 63 74 78 2e 73   0;.  u.cb.ctx.s
1ef04 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 63 62  .db = db;.  u.cb
1ef05 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30  .ctx.isError = 0
1ef06 3b 0a 20 20 75 2e 63 62 2e 63 74 78 2e 70 43 6f  ;.  u.cb.ctx.pCo
1ef07 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e  ll = 0;.  if( u.
1ef08 63 62 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c  cb.ctx.pFunc->fl
1ef09 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e  ags & SQLITE_FUN
1ef0a 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20  C_NEEDCOLL ){.  
1ef0b 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d    assert( pOp>p-
1ef0c 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65  >aOp );.    asse
1ef0d 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79  rt( pOp[-1].p4ty
1ef0e 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29  pe==P4_COLLSEQ )
1ef0f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ;.    assert( pO
1ef10 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50  p[-1].opcode==OP
1ef11 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20  _CollSeq );.    
1ef12 75 2e 63 62 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d  u.cb.ctx.pColl =
1ef13 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c   pOp[-1].p4.pCol
1ef14 6c 3b 0a 20 20 7d 0a 20 20 28 75 2e 63 62 2e 63  l;.  }.  (u.cb.c
1ef15 74 78 2e 70 46 75 6e 63 2d 3e 78 53 74 65 70 29  tx.pFunc->xStep)
1ef16 28 26 75 2e 63 62 2e 63 74 78 2c 20 75 2e 63 62  (&u.cb.ctx, u.cb
1ef17 2e 6e 2c 20 75 2e 63 62 2e 61 70 56 61 6c 29 3b  .n, u.cb.apVal);
1ef18 0a 20 20 69 66 28 20 75 2e 63 62 2e 63 74 78 2e  .  if( u.cb.ctx.
1ef19 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73  isError ){.    s
1ef1a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1ef1b 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1ef1c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76   "%s", sqlite3_v
1ef1d 61 6c 75 65 5f 74 65 78 74 28 26 75 2e 63 62 2e  alue_text(&u.cb.
1ef1e 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20  ctx.s));.    rc 
1ef1f 3d 20 75 2e 63 62 2e 63 74 78 2e 69 73 45 72 72  = u.cb.ctx.isErr
1ef20 6f 72 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  or;.  }.  sqlite
1ef21 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1ef22 26 75 2e 63 62 2e 63 74 78 2e 73 29 3b 0a 20 20  &u.cb.ctx.s);.  
1ef23 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1ef24 6f 64 65 3a 20 41 67 67 46 69 6e 61 6c 20 50 31  ode: AggFinal P1
1ef25 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
1ef26 20 45 78 65 63 75 74 65 20 74 68 65 20 66 69 6e   Execute the fin
1ef27 61 6c 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20  alizer function 
1ef28 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65  for an aggregate
1ef29 2e 20 20 50 31 20 69 73 0a 2a 2a 20 74 68 65 20  .  P1 is.** the 
1ef2a 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  memory location 
1ef2b 74 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75  that is the accu
1ef2c 6d 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20  mulator for the 
1ef2d 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a  aggregate..**.**
1ef2e 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65   P2 is the numbe
1ef2f 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
1ef30 68 61 74 20 74 68 65 20 73 74 65 70 20 66 75 6e  hat the step fun
1ef31 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64 0a  ction takes and.
1ef32 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1ef33 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65  er to the FuncDe
1ef34 66 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 74  f for this funct
1ef35 69 6f 6e 2e 20 20 54 68 65 20 50 32 0a 2a 2a 20  ion.  The P2.** 
1ef36 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20  argument is not 
1ef37 75 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 63  used by this opc
1ef38 6f 64 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ode.  It is only
1ef39 20 74 68 65 72 65 20 74 6f 20 64 69 73 61 6d 62   there to disamb
1ef3a 69 67 75 61 74 65 0a 2a 2a 20 66 75 6e 63 74 69  iguate.** functi
1ef3b 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 74 61 6b  ons that can tak
1ef3c 65 20 76 61 72 79 69 6e 67 20 6e 75 6d 62 65 72  e varying number
1ef3d 73 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20  s of arguments. 
1ef3e 20 54 68 65 0a 2a 2a 20 50 34 20 61 72 67 75 6d   The.** P4 argum
1ef3f 65 6e 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64  ent is only need
1ef40 65 64 20 66 6f 72 20 74 68 65 20 64 65 67 65 6e  ed for the degen
1ef41 65 72 61 74 65 20 63 61 73 65 20 77 68 65 72 65  erate case where
1ef42 0a 2a 2a 20 74 68 65 20 73 74 65 70 20 66 75 6e  .** the step fun
1ef43 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 70 72  ction was not pr
1ef44 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2e  eviously called.
1ef45 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 46  .*/.case OP_AggF
1ef46 69 6e 61 6c 3a 20 7b 0a 23 69 66 20 30 20 20 2f  inal: {.#if 0  /
1ef47 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1ef48 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1ef49 63 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d  c */.  Mem *pMem
1ef4a 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1ef4b 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ef4c 64 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20  d into u.cc */. 
1ef4d 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1ef4e 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70  >0 && pOp->p1<=p
1ef4f 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 63  ->nMem );.  u.cc
1ef50 2e 70 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f  .pMem = &aMem[pO
1ef51 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1ef52 28 20 28 75 2e 63 63 2e 70 4d 65 6d 2d 3e 66 6c  ( (u.cc.pMem->fl
1ef53 61 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c  ags & ~(MEM_Null
1ef54 7c 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20 29 3b  |MEM_Agg))==0 );
1ef55 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
1ef56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 75  dbeMemFinalize(u
1ef57 2e 63 63 2e 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70  .cc.pMem, pOp->p
1ef58 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66 28 20  4.pFunc);.  if( 
1ef59 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rc ){.    sqlite
1ef5a 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1ef5b 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22  ErrMsg, db, "%s"
1ef5c 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
1ef5d 74 65 78 74 28 75 2e 63 63 2e 70 4d 65 6d 29 29  text(u.cc.pMem))
1ef5e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
1ef5f 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
1ef60 67 28 75 2e 63 63 2e 70 4d 65 6d 2c 20 65 6e 63  g(u.cc.pMem, enc
1ef61 6f 64 69 6e 67 29 3b 0a 20 20 55 50 44 41 54 45  oding);.  UPDATE
1ef62 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 75 2e  _MAX_BLOBSIZE(u.
1ef63 63 63 2e 70 4d 65 6d 29 3b 0a 20 20 69 66 28 20  cc.pMem);.  if( 
1ef64 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f  sqlite3VdbeMemTo
1ef65 6f 42 69 67 28 75 2e 63 63 2e 70 4d 65 6d 29 20  oBig(u.cc.pMem) 
1ef66 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  ){.    goto too_
1ef67 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  big;.  }.  break
1ef68 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e  ;.}...#if !defin
1ef69 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
1ef6a 41 43 55 55 4d 29 20 26 26 20 21 64 65 66 69 6e  ACUUM) && !defin
1ef6b 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ed(SQLITE_OMIT_A
1ef6c 54 54 41 43 48 29 0a 2f 2a 20 4f 70 63 6f 64 65  TTACH)./* Opcode
1ef6d 3a 20 56 61 63 75 75 6d 20 2a 20 2a 20 2a 20 2a  : Vacuum * * * *
1ef6e 20 2a 0a 2a 2a 0a 2a 2a 20 56 61 63 75 75 6d 20   *.**.** Vacuum 
1ef6f 74 68 65 20 65 6e 74 69 72 65 20 64 61 74 61 62  the entire datab
1ef70 61 73 65 2e 20 20 54 68 69 73 20 6f 70 63 6f 64  ase.  This opcod
1ef71 65 20 77 69 6c 6c 20 63 61 75 73 65 20 6f 74 68  e will cause oth
1ef72 65 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61  er virtual.** ma
1ef73 63 68 69 6e 65 73 20 74 6f 20 62 65 20 63 72 65  chines to be cre
1ef74 61 74 65 64 20 61 6e 64 20 72 75 6e 2e 20 20 49  ated and run.  I
1ef75 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 61 6c  t may not be cal
1ef76 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a  led from within.
1ef77 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ** a transaction
1ef78 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 61 63  ..*/.case OP_Vac
1ef79 75 75 6d 3a 20 7b 0a 20 20 69 66 28 20 73 71 6c  uum: {.  if( sql
1ef7a 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1ef7b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1ef7c 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 20 0a 20  ue_to_misuse; . 
1ef7d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 75 6e   rc = sqlite3Run
1ef7e 56 61 63 75 75 6d 28 26 70 2d 3e 7a 45 72 72 4d  Vacuum(&p->zErrM
1ef7f 73 67 2c 20 64 62 29 3b 0a 20 20 69 66 28 20 73  sg, db);.  if( s
1ef80 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1ef81 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1ef82 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1ef83 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1ef84 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1ef85 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1ef86 41 43 55 55 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65  ACUUM)./* Opcode
1ef87 3a 20 49 6e 63 72 56 61 63 75 75 6d 20 50 31 20  : IncrVacuum P1 
1ef88 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50  P2 * * *.**.** P
1ef89 65 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20  erform a single 
1ef8a 73 74 65 70 20 6f 66 20 74 68 65 20 69 6e 63 72  step of the incr
1ef8b 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 70  emental vacuum p
1ef8c 72 6f 63 65 64 75 72 65 20 6f 6e 0a 2a 2a 20 74  rocedure on.** t
1ef8d 68 65 20 50 31 20 64 61 74 61 62 61 73 65 2e 20  he P1 database. 
1ef8e 49 66 20 74 68 65 20 76 61 63 75 75 6d 20 68 61  If the vacuum ha
1ef8f 73 20 66 69 6e 69 73 68 65 64 2c 20 6a 75 6d 70  s finished, jump
1ef90 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a   to instruction.
1ef91 2a 2a 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65  ** P2. Otherwise
1ef92 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  , fall through t
1ef93 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
1ef94 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20  uction..*/.case 
1ef95 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 3a 20 7b  OP_IncrVacuum: {
1ef96 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20          /* jump 
1ef97 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1ef98 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1ef99 65 64 20 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a  ed into u.cd */.
1ef9a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65    Btree *pBt;.#e
1ef9b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1ef9c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ef9d 74 6f 20 75 2e 63 64 20 2a 2f 0a 0a 20 20 61 73  to u.cd */..  as
1ef9e 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1ef9f 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e   && pOp->p1<db->
1efa0 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
1efa1 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1efa2 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d   (1<<pOp->p1))!=
1efa3 30 20 29 3b 0a 20 20 75 2e 63 64 2e 70 42 74 20  0 );.  u.cd.pBt 
1efa4 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70  = db->aDb[pOp->p
1efa5 31 5d 2e 70 42 74 3b 0a 20 20 72 63 20 3d 20 73  1].pBt;.  rc = s
1efa6 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56  qlite3BtreeIncrV
1efa7 61 63 75 75 6d 28 75 2e 63 64 2e 70 42 74 29 3b  acuum(u.cd.pBt);
1efa8 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1efa9 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 70 63  E_DONE ){.    pc
1efaa 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1efab 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1efac 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  OK;.  }.  break;
1efad 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70  .}.#endif../* Op
1efae 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50 31 20  code: Expire P1 
1efaf 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61  * * * *.**.** Ca
1efb0 75 73 65 20 70 72 65 63 6f 6d 70 69 6c 65 64 20  use precompiled 
1efb1 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 62 65  statements to be
1efb2 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 41 6e  come expired. An
1efb3 20 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65   expired stateme
1efb4 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68  nt.** fails with
1efb5 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f   an error code o
1efb6 66 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  f SQLITE_SCHEMA 
1efb7 69 66 20 69 74 20 69 73 20 65 76 65 72 20 65 78  if it is ever ex
1efb8 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 69 61 20  ecuted .** (via 
1efb9 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 29 2e  sqlite3_step()).
1efba 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 20 69 73  .** .** If P1 is
1efbb 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 53 51 4c   0, then all SQL
1efbc 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65 63 6f   statements beco
1efbd 6d 65 20 65 78 70 69 72 65 64 2e 20 49 66 20 50  me expired. If P
1efbe 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a  1 is non-zero,.*
1efbf 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20  * then only the 
1efc0 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74  currently execut
1efc1 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ing statement is
1efc2 20 61 66 66 65 63 74 65 64 2e 20 0a 2a 2f 0a 63   affected. .*/.c
1efc3 61 73 65 20 4f 50 5f 45 78 70 69 72 65 3a 20 7b  ase OP_Expire: {
1efc4 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e 70 31 20  .  if( !pOp->p1 
1efc5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
1efc6 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
1efc7 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 65  ements(db);.  }e
1efc8 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69  lse{.    p->expi
1efc9 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62  red = 1;.  }.  b
1efca 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  reak;.}..#ifndef
1efcb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1efcc 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 4f 70 63  RED_CACHE./* Opc
1efcd 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 6b 20 50  ode: TableLock P
1efce 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1efcf 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b  ** Obtain a lock
1efd0 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   on a particular
1efd1 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69 6e 73   table. This ins
1efd2 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  truction is only
1efd3 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68   used when.** th
1efd4 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66  e shared-cache f
1efd5 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65  eature is enable
1efd6 64 2e 20 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  d. .**.** P1 is 
1efd7 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
1efd8 20 64 61 74 61 62 61 73 65 20 69 6e 20 73 71 6c   database in sql
1efd9 69 74 65 33 2e 61 44 62 5b 5d 20 6f 66 20 74 68  ite3.aDb[] of th
1efda 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 6f 6e  e database.** on
1efdb 20 77 68 69 63 68 20 74 68 65 20 6c 6f 63 6b 20   which the lock 
1efdc 69 73 20 61 63 71 75 69 72 65 64 2e 20 20 41 20  is acquired.  A 
1efdd 72 65 61 64 6c 6f 63 6b 20 69 73 20 6f 62 74 61  readlock is obta
1efde 69 6e 65 64 20 69 66 20 50 33 3d 3d 30 20 6f 72  ined if P3==0 or
1efdf 0a 2a 2a 20 61 20 77 72 69 74 65 20 6c 6f 63 6b  .** a write lock
1efe0 20 69 66 20 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a   if P3==1..**.**
1efe1 20 50 32 20 63 6f 6e 74 61 69 6e 73 20 74 68 65   P2 contains the
1efe2 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68   root-page of th
1efe3 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e  e table to lock.
1efe4 0a 2a 2a 0a 2a 2a 20 50 34 20 63 6f 6e 74 61 69  .**.** P4 contai
1efe5 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
1efe6 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1efe7 74 61 62 6c 65 20 62 65 69 6e 67 20 6c 6f 63 6b  table being lock
1efe8 65 64 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79  ed. This is only
1efe9 0a 2a 2a 20 75 73 65 64 20 74 6f 20 67 65 6e 65  .** used to gene
1efea 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rate an error me
1efeb 73 73 61 67 65 20 69 66 20 74 68 65 20 6c 6f 63  ssage if the loc
1efec 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61  k cannot be obta
1efed 69 6e 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  ined..*/.case OP
1efee 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20  _TableLock: {.  
1efef 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d  u8 isWriteLock =
1eff0 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b 0a 20 20   (u8)pOp->p3;.  
1eff1 69 66 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 20  if( isWriteLock 
1eff2 7c 7c 20 30 3d 3d 28 64 62 2d 3e 66 6c 61 67 73  || 0==(db->flags
1eff3 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f  &SQLITE_ReadUnco
1eff4 6d 6d 69 74 74 65 64 29 20 29 7b 0a 20 20 20 20  mmitted) ){.    
1eff5 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31  int p1 = pOp->p1
1eff6 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ; .    assert( p
1eff7 31 3e 3d 30 20 26 26 20 70 31 3c 64 62 2d 3e 6e  1>=0 && p1<db->n
1eff8 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Db );.    assert
1eff9 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1effa 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 20 29 3b  & (1<<p1))!=0 );
1effb 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 57  .    assert( isW
1effc 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69  riteLock==0 || i
1effd 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b  sWriteLock==1 );
1effe 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1efff 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28  3BtreeLockTable(
1f000 64 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 2c  db->aDb[p1].pBt,
1f001 20 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 69 74   pOp->p2, isWrit
1f002 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20  eLock);.    if( 
1f003 28 72 63 26 30 78 46 46 29 3d 3d 53 51 4c 49 54  (rc&0xFF)==SQLIT
1f004 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20  E_LOCKED ){.    
1f005 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
1f006 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20  = pOp->p4.z;.   
1f007 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1f008 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1f009 20 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 74   db, "database t
1f00a 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 3a 20  able is locked: 
1f00b 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 7d 0a 20  %s", z);.    }. 
1f00c 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1f00d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1f00e 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1f00f 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1f010 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1f011 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
1f012 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 50 34   VBegin * * * P4
1f013 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20   *.**.** P4 may 
1f014 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
1f015 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  an sqlite3_vtab 
1f016 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 73 6f  structure. If so
1f017 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a 2a 20 78  , call the .** x
1f018 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66 6f 72  Begin method for
1f019 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a   that table..**.
1f01a 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 68 65 72  ** Also, whether
1f01b 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 20 73 65   or not P4 is se
1f01c 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68  t, check that th
1f01d 69 73 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  is is not being 
1f01e 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a 2a 20 77  called from.** w
1f01f 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 61 63 6b  ithin a callback
1f020 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61   to a virtual ta
1f021 62 6c 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68  ble xSync() meth
1f022 6f 64 2e 20 49 66 20 69 74 20 69 73 2c 20 74 68  od. If it is, th
1f023 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20  e error.** code 
1f024 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 53  will be set to S
1f025 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f  QLITE_LOCKED..*/
1f026 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 69 6e 3a  .case OP_VBegin:
1f027 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1f028 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f029 65 64 20 69 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a  ed into u.ce */.
1f02a 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 3b    VTable *pVTab;
1f02b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1f02c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f02d 20 69 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20   into u.ce */.  
1f02e 75 2e 63 65 2e 70 56 54 61 62 20 3d 20 70 4f 70  u.ce.pVTab = pOp
1f02f 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 72 63  ->p4.pVtab;.  rc
1f030 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 42 65   = sqlite3VtabBe
1f031 67 69 6e 28 64 62 2c 20 75 2e 63 65 2e 70 56 54  gin(db, u.ce.pVT
1f032 61 62 29 3b 0a 20 20 69 66 28 20 75 2e 63 65 2e  ab);.  if( u.ce.
1f033 70 56 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c  pVTab ){.    sql
1f034 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1f035 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
1f036 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63  p->zErrMsg = u.c
1f037 65 2e 70 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e  e.pVTab->pVtab->
1f038 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63  zErrMsg;.    u.c
1f039 65 2e 70 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e  e.pVTab->pVtab->
1f03a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d  zErrMsg = 0;.  }
1f03b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1f03c 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1f03d 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1f03e 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1f03f 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1f040 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1f041 43 72 65 61 74 65 20 50 31 20 2a 20 2a 20 50 34  Create P1 * * P4
1f042 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74   *.**.** P4 is t
1f043 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72  he name of a vir
1f044 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61  tual table in da
1f045 74 61 62 61 73 65 20 50 31 2e 20 43 61 6c 6c 20  tabase P1. Call 
1f046 74 68 65 20 78 43 72 65 61 74 65 20 6d 65 74 68  the xCreate meth
1f047 6f 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20 74  od.** for that t
1f048 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  able..*/.case OP
1f049 5f 56 43 72 65 61 74 65 3a 20 7b 0a 20 20 72 63  _VCreate: {.  rc
1f04a 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61   = sqlite3VtabCa
1f04b 6c 6c 43 72 65 61 74 65 28 64 62 2c 20 70 4f 70  llCreate(db, pOp
1f04c 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c  ->p1, pOp->p4.z,
1f04d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20   &p->zErrMsg);. 
1f04e 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f04f 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1f050 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1f051 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f052 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1f053 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 44 65  E./* Opcode: VDe
1f054 73 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 34 20  stroy P1 * * P4 
1f055 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68  *.**.** P4 is th
1f056 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74  e name of a virt
1f057 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74  ual table in dat
1f058 61 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c 20  abase P1.  Call 
1f059 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74  the xDestroy met
1f05a 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74  hod.** of that t
1f05b 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  able..*/.case OP
1f05c 5f 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20 20 70  _VDestroy: {.  p
1f05d 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
1f05e 20 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   2;.  rc = sqlit
1f05f 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f  e3VtabCallDestro
1f060 79 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  y(db, pOp->p1, p
1f061 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e  Op->p4.z);.  p->
1f062 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30  inVtabMethod = 0
1f063 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  ;.  break;.}.#en
1f064 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1f065 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
1f066 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1f067 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1f068 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ABLE./* Opcode: 
1f069 56 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50 34 20  VOpen P1 * * P4 
1f06a 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  *.**.** P4 is a 
1f06b 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72  pointer to a vir
1f06c 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63  tual table objec
1f06d 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  t, an sqlite3_vt
1f06e 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ab structure..**
1f06f 20 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20   P1 is a cursor 
1f070 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f 70  number.  This op
1f071 63 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75 72  code opens a cur
1f072 73 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74 75  sor to the virtu
1f073 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20  al.** table and 
1f074 73 74 6f 72 65 73 20 74 68 61 74 20 63 75 72 73  stores that curs
1f075 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73  or in P1..*/.cas
1f076 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a 23 69  e OP_VOpen: {.#i
1f077 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1f078 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f079 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 56 64 62  to u.cf */.  Vdb
1f07a 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20  eCursor *pCur;. 
1f07b 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
1f07c 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f  rsor *pVtabCurso
1f07d 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  r;.  sqlite3_vta
1f07e 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c 69  b *pVtab;.  sqli
1f07f 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1f080 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  ule;.#endif /* l
1f081 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f082 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 66 20 2a  oved into u.cf *
1f083 2f 0a 0a 20 20 75 2e 63 66 2e 70 43 75 72 20 3d  /..  u.cf.pCur =
1f084 20 30 3b 0a 20 20 75 2e 63 66 2e 70 56 74 61 62   0;.  u.cf.pVtab
1f085 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 75 2e  Cursor = 0;.  u.
1f086 63 66 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e  cf.pVtab = pOp->
1f087 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b  p4.pVtab->pVtab;
1f088 0a 20 20 75 2e 63 66 2e 70 4d 6f 64 75 6c 65 20  .  u.cf.pModule 
1f089 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  = (sqlite3_modul
1f08a 65 20 2a 29 75 2e 63 66 2e 70 56 74 61 62 2d 3e  e *)u.cf.pVtab->
1f08b 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72  pModule;.  asser
1f08c 74 28 75 2e 63 66 2e 70 56 74 61 62 20 26 26 20  t(u.cf.pVtab && 
1f08d 75 2e 63 66 2e 70 4d 6f 64 75 6c 65 29 3b 0a 20  u.cf.pModule);. 
1f08e 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1f08f 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1f090 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1f091 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63  suse;.  rc = u.c
1f092 66 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e  f.pModule->xOpen
1f093 28 75 2e 63 66 2e 70 56 74 61 62 2c 20 26 75 2e  (u.cf.pVtab, &u.
1f094 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b  cf.pVtabCursor);
1f095 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1f096 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1f097 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d  ;.  p->zErrMsg =
1f098 20 75 2e 63 66 2e 70 56 74 61 62 2d 3e 7a 45 72   u.cf.pVtab->zEr
1f099 72 4d 73 67 3b 0a 20 20 75 2e 63 66 2e 70 56 74  rMsg;.  u.cf.pVt
1f09a 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1f09b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1f09c 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1f09d 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1f09e 69 73 75 73 65 3b 0a 20 20 69 66 28 20 53 51 4c  isuse;.  if( SQL
1f09f 49 54 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20  ITE_OK==rc ){.  
1f0a0 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
1f0a1 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
1f0a2 73 6f 72 20 62 61 73 65 20 63 6c 61 73 73 20 2a  sor base class *
1f0a3 2f 0a 20 20 20 20 75 2e 63 66 2e 70 56 74 61 62  /.    u.cf.pVtab
1f0a4 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 20 3d 20  Cursor->pVtab = 
1f0a5 75 2e 63 66 2e 70 56 74 61 62 3b 0a 0a 20 20 20  u.cf.pVtab;..   
1f0a6 20 2f 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 76   /* Initialise v
1f0a7 64 62 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63  dbe cursor objec
1f0a8 74 20 2a 2f 0a 20 20 20 20 75 2e 63 66 2e 70 43  t */.    u.cf.pC
1f0a9 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72  ur = allocateCur
1f0aa 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20  sor(p, pOp->p1, 
1f0ab 30 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 69  0, -1, 0);.    i
1f0ac 66 28 20 75 2e 63 66 2e 70 43 75 72 20 29 7b 0a  f( u.cf.pCur ){.
1f0ad 20 20 20 20 20 20 75 2e 63 66 2e 70 43 75 72 2d        u.cf.pCur-
1f0ae 3e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 75  >pVtabCursor = u
1f0af 2e 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 3b  .cf.pVtabCursor;
1f0b0 0a 20 20 20 20 20 20 75 2e 63 66 2e 70 43 75 72  .      u.cf.pCur
1f0b1 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 66  ->pModule = u.cf
1f0b2 2e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56  .pVtabCursor->pV
1f0b3 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  tab->pModule;.  
1f0b4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
1f0b5 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1f0b6 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 63 66 2e  = 1;.      u.cf.
1f0b7 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28  pModule->xClose(
1f0b8 75 2e 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72  u.cf.pVtabCursor
1f0b9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  );.    }.  }.  b
1f0ba 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1f0bb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1f0bc 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1f0bd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f0be 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1f0bf 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46 69 6c 74  /* Opcode: VFilt
1f0c0 65 72 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  er P1 P2 P3 P4 *
1f0c1 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 63  .**.** P1 is a c
1f0c2 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 75 73 69  ursor opened usi
1f0c3 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32 20 69 73  ng VOpen.  P2 is
1f0c4 20 61 6e 20 61 64 64 72 65 73 73 20 74 6f 20 6a   an address to j
1f0c5 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20 74 68 65  ump to if.** the
1f0c6 20 66 69 6c 74 65 72 65 64 20 72 65 73 75 6c 74   filtered result
1f0c7 20 73 65 74 20 69 73 20 65 6d 70 74 79 2e 0a 2a   set is empty..*
1f0c8 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69 74 68 65  *.** P4 is eithe
1f0c9 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72 69  r NULL or a stri
1f0ca 6e 67 20 74 68 61 74 20 77 61 73 20 67 65 6e 65  ng that was gene
1f0cb 72 61 74 65 64 20 62 79 20 74 68 65 20 78 42 65  rated by the xBe
1f0cc 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 68 6f  stIndex.** metho
1f0cd 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e  d of the module.
1f0ce 20 20 54 68 65 20 69 6e 74 65 72 70 72 65 74 61    The interpreta
1f0cf 74 69 6f 6e 20 6f 66 20 74 68 65 20 50 34 20 73  tion of the P4 s
1f0d0 74 72 69 6e 67 20 69 73 20 6c 65 66 74 0a 2a 2a  tring is left.**
1f0d1 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69   to the module i
1f0d2 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a  mplementation..*
1f0d3 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  *.** This opcode
1f0d4 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 46 69   invokes the xFi
1f0d5 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f 6e 20 74  lter method on t
1f0d6 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
1f0d7 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79   specified.** by
1f0d8 20 50 31 2e 20 20 54 68 65 20 69 6e 74 65 67 65   P1.  The intege
1f0d9 72 20 71 75 65 72 79 20 70 6c 61 6e 20 70 61 72  r query plan par
1f0da 61 6d 65 74 65 72 20 74 6f 20 78 46 69 6c 74 65  ameter to xFilte
1f0db 72 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  r is stored in r
1f0dc 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 2e 20 52  egister.** P3. R
1f0dd 65 67 69 73 74 65 72 20 50 33 2b 31 20 73 74 6f  egister P3+1 sto
1f0de 72 65 73 20 74 68 65 20 61 72 67 63 20 70 61 72  res the argc par
1f0df 61 6d 65 74 65 72 20 74 6f 20 62 65 20 70 61 73  ameter to be pas
1f0e0 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46  sed to the.** xF
1f0e1 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 20 52 65  ilter method. Re
1f0e2 67 69 73 74 65 72 73 20 50 33 2b 32 2e 2e 50 33  gisters P3+2..P3
1f0e3 2b 31 2b 61 72 67 63 20 61 72 65 20 74 68 65 20  +1+argc are the 
1f0e4 61 72 67 63 0a 2a 2a 20 61 64 64 69 74 69 6f 6e  argc.** addition
1f0e5 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 77 68  al parameters wh
1f0e6 69 63 68 20 61 72 65 20 70 61 73 73 65 64 20 74  ich are passed t
1f0e7 6f 0a 2a 2a 20 78 46 69 6c 74 65 72 20 61 73 20  o.** xFilter as 
1f0e8 61 72 67 76 2e 20 52 65 67 69 73 74 65 72 20 50  argv. Register P
1f0e9 33 2b 32 20 62 65 63 6f 6d 65 73 20 61 72 67 76  3+2 becomes argv
1f0ea 5b 30 5d 20 77 68 65 6e 20 70 61 73 73 65 64 20  [0] when passed 
1f0eb 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a  to xFilter..**.*
1f0ec 2a 20 41 20 6a 75 6d 70 20 69 73 20 6d 61 64 65  * A jump is made
1f0ed 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 72 65   to P2 if the re
1f0ee 73 75 6c 74 20 73 65 74 20 61 66 74 65 72 20 66  sult set after f
1f0ef 69 6c 74 65 72 69 6e 67 20 77 6f 75 6c 64 20 62  iltering would b
1f0f0 65 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 73 65  e empty..*/.case
1f0f1 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20 7b 20 20   OP_VFilter: {  
1f0f2 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20   /* jump */.#if 
1f0f3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f0f4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f0f5 20 75 2e 63 67 20 2a 2f 0a 20 20 69 6e 74 20 6e   u.cg */.  int n
1f0f6 41 72 67 3b 0a 20 20 69 6e 74 20 69 51 75 65 72  Arg;.  int iQuer
1f0f7 79 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  y;.  const sqlit
1f0f8 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1f0f9 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75 65 72  le;.  Mem *pQuer
1f0fa 79 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 63 3b  y;.  Mem *pArgc;
1f0fb 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
1f0fc 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
1f0fd 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  sor;.  sqlite3_v
1f0fe 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 56 64  tab *pVtab;.  Vd
1f0ff 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  beCursor *pCur;.
1f100 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 6e 74    int res;.  int
1f101 20 69 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72   i;.  Mem **apAr
1f102 67 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  g;.#endif /* loc
1f103 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f104 65 64 20 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a  ed into u.cg */.
1f105 0a 20 20 75 2e 63 67 2e 70 51 75 65 72 79 20 3d  .  u.cg.pQuery =
1f106 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
1f107 0a 20 20 75 2e 63 67 2e 70 41 72 67 63 20 3d 20  .  u.cg.pArgc = 
1f108 26 75 2e 63 67 2e 70 51 75 65 72 79 5b 31 5d 3b  &u.cg.pQuery[1];
1f109 0a 20 20 75 2e 63 67 2e 70 43 75 72 20 3d 20 70  .  u.cg.pCur = p
1f10a 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1f10b 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  ;.  REGISTER_TRA
1f10c 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 67  CE(pOp->p3, u.cg
1f10d 2e 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65  .pQuery);.  asse
1f10e 72 74 28 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70  rt( u.cg.pCur->p
1f10f 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20  VtabCursor );.  
1f110 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72  u.cg.pVtabCursor
1f111 20 3d 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56   = u.cg.pCur->pV
1f112 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63  tabCursor;.  u.c
1f113 67 2e 70 56 74 61 62 20 3d 20 75 2e 63 67 2e 70  g.pVtab = u.cg.p
1f114 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61  VtabCursor->pVta
1f115 62 3b 0a 20 20 75 2e 63 67 2e 70 4d 6f 64 75 6c  b;.  u.cg.pModul
1f116 65 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e  e = u.cg.pVtab->
1f117 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47  pModule;..  /* G
1f118 72 61 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75  rab the index nu
1f119 6d 62 65 72 20 61 6e 64 20 61 72 67 63 20 70 61  mber and argc pa
1f11a 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73  rameters */.  as
1f11b 73 65 72 74 28 20 28 75 2e 63 67 2e 70 51 75 65  sert( (u.cg.pQue
1f11c 72 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e  ry->flags&MEM_In
1f11d 74 29 21 3d 30 20 26 26 20 75 2e 63 67 2e 70 41  t)!=0 && u.cg.pA
1f11e 72 67 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f  rgc->flags==MEM_
1f11f 49 6e 74 20 29 3b 0a 20 20 75 2e 63 67 2e 6e 41  Int );.  u.cg.nA
1f120 72 67 20 3d 20 28 69 6e 74 29 75 2e 63 67 2e 70  rg = (int)u.cg.p
1f121 41 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63  Argc->u.i;.  u.c
1f122 67 2e 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29  g.iQuery = (int)
1f123 75 2e 63 67 2e 70 51 75 65 72 79 2d 3e 75 2e 69  u.cg.pQuery->u.i
1f124 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  ;..  /* Invoke t
1f125 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f  he xFilter metho
1f126 64 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63  d */.  {.    u.c
1f127 67 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75  g.res = 0;.    u
1f128 2e 63 67 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61  .cg.apArg = p->a
1f129 70 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e  pArg;.    for(u.
1f12a 63 67 2e 69 20 3d 20 30 3b 20 75 2e 63 67 2e 69  cg.i = 0; u.cg.i
1f12b 3c 75 2e 63 67 2e 6e 41 72 67 3b 20 75 2e 63 67  <u.cg.nArg; u.cg
1f12c 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63  .i++){.      u.c
1f12d 67 2e 61 70 41 72 67 5b 75 2e 63 67 2e 69 5d 20  g.apArg[u.cg.i] 
1f12e 3d 20 26 75 2e 63 67 2e 70 41 72 67 63 5b 75 2e  = &u.cg.pArgc[u.
1f12f 63 67 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73  cg.i+1];.      s
1f130 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f  qlite3VdbeMemSto
1f131 72 65 54 79 70 65 28 75 2e 63 67 2e 61 70 41 72  reType(u.cg.apAr
1f132 67 5b 75 2e 63 67 2e 69 5d 29 3b 0a 20 20 20 20  g[u.cg.i]);.    
1f133 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  }..    if( sqlit
1f134 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1f135 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1f136 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20  _to_misuse;.    
1f137 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20  p->inVtabMethod 
1f138 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e  = 1;.    rc = u.
1f139 63 67 2e 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c  cg.pModule->xFil
1f13a 74 65 72 28 75 2e 63 67 2e 70 56 74 61 62 43 75  ter(u.cg.pVtabCu
1f13b 72 73 6f 72 2c 20 75 2e 63 67 2e 69 51 75 65 72  rsor, u.cg.iQuer
1f13c 79 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e  y, pOp->p4.z, u.
1f13d 63 67 2e 6e 41 72 67 2c 20 75 2e 63 67 2e 61 70  cg.nArg, u.cg.ap
1f13e 41 72 67 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56  Arg);.    p->inV
1f13f 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20  tabMethod = 0;. 
1f140 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1f141 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1f142 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
1f143 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e 7a   = u.cg.pVtab->z
1f144 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 67  ErrMsg;.    u.cg
1f145 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20  .pVtab->zErrMsg 
1f146 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  = 0;.    if( rc=
1f147 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1f148 20 20 20 20 75 2e 63 67 2e 72 65 73 20 3d 20 75      u.cg.res = u
1f149 2e 63 67 2e 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f  .cg.pModule->xEo
1f14a 66 28 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73  f(u.cg.pVtabCurs
1f14b 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  or);.    }.    i
1f14c 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1f14d 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
1f14e 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1f14f 65 3b 0a 0a 20 20 20 20 69 66 28 20 75 2e 63 67  e;..    if( u.cg
1f150 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63  .res ){.      pc
1f151 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1f152 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 63 67      }.  }.  u.cg
1f153 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d  .pCur->nullRow =
1f154 20 30 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a   0;..  break;.}.
1f155 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1f156 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1f157 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  LE */..#ifndef S
1f158 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1f159 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
1f15a 65 3a 20 56 43 6f 6c 75 6d 6e 20 50 31 20 50 32  e: VColumn P1 P2
1f15b 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74   P3 * *.**.** St
1f15c 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ore the value of
1f15d 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d   the P2-th colum
1f15e 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20  n of.** the row 
1f15f 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  of the virtual-t
1f160 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 0a 2a  able that the .*
1f161 2a 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 70  * P1 cursor is p
1f162 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20  ointing to into 
1f163 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a  register P3..*/.
1f164 63 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a  case OP_VColumn:
1f165 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1f166 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f167 65 64 20 69 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a  ed into u.ch */.
1f168 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1f169 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73  pVtab;.  const s
1f16a 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
1f16b 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70  Module;.  Mem *p
1f16c 44 65 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Dest;.  sqlite3_
1f16d 63 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74  context sContext
1f16e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1f16f 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1f170 64 20 69 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a  d into u.ch */..
1f171 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1f172 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  ur = p->apCsr[pO
1f173 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1f174 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  ( pCur->pVtabCur
1f175 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  sor );.  assert(
1f176 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f   pOp->p3>0 && pO
1f177 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1f178 3b 0a 20 20 75 2e 63 68 2e 70 44 65 73 74 20 3d  ;.  u.ch.pDest =
1f179 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
1f17a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c  .  if( pCur->nul
1f17b 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  lRow ){.    sqli
1f17c 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1f17d 6c 28 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a 20  l(u.ch.pDest);. 
1f17e 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
1f17f 75 2e 63 68 2e 70 56 74 61 62 20 3d 20 70 43 75  u.ch.pVtab = pCu
1f180 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e  r->pVtabCursor->
1f181 70 56 74 61 62 3b 0a 20 20 75 2e 63 68 2e 70 4d  pVtab;.  u.ch.pM
1f182 6f 64 75 6c 65 20 3d 20 75 2e 63 68 2e 70 56 74  odule = u.ch.pVt
1f183 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61  ab->pModule;.  a
1f184 73 73 65 72 74 28 20 75 2e 63 68 2e 70 4d 6f 64  ssert( u.ch.pMod
1f185 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a  ule->xColumn );.
1f186 20 20 6d 65 6d 73 65 74 28 26 75 2e 63 68 2e 73    memset(&u.ch.s
1f187 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65  Context, 0, size
1f188 6f 66 28 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74  of(u.ch.sContext
1f189 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75  ));..  /* The ou
1f18a 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c  tput cell may al
1f18b 72 65 61 64 79 20 68 61 76 65 20 61 20 62 75 66  ready have a buf
1f18c 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d  fer allocated. M
1f18d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72  ove.  ** the cur
1f18e 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 74 6f  rent contents to
1f18f 20 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e 73   u.ch.sContext.s
1f190 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20   so in case the 
1f191 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 0a 20 20  user-function.  
1f192 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 65 20 61  ** can use the a
1f193 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64  lready allocated
1f194 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20   buffer instead 
1f195 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 0a  of allocating a.
1f196 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20    ** new one..  
1f197 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1f198 4d 65 6d 4d 6f 76 65 28 26 75 2e 63 68 2e 73 43  MemMove(&u.ch.sC
1f199 6f 6e 74 65 78 74 2e 73 2c 20 75 2e 63 68 2e 70  ontext.s, u.ch.p
1f19a 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54  Dest);.  MemSetT
1f19b 79 70 65 46 6c 61 67 28 26 75 2e 63 68 2e 73 43  ypeFlag(&u.ch.sC
1f19c 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75  ontext.s, MEM_Nu
1f19d 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69  ll);..  if( sqli
1f19e 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1f19f 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1f1a0 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72  e_to_misuse;.  r
1f1a1 63 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65  c = u.ch.pModule
1f1a2 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e  ->xColumn(pCur->
1f1a3 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 75 2e  pVtabCursor, &u.
1f1a4 63 68 2e 73 43 6f 6e 74 65 78 74 2c 20 70 4f 70  ch.sContext, pOp
1f1a5 2d 3e 70 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  ->p2);.  sqlite3
1f1a6 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1f1a7 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72  rrMsg);.  p->zEr
1f1a8 72 4d 73 67 20 3d 20 75 2e 63 68 2e 70 56 74 61  rMsg = u.ch.pVta
1f1a9 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e  b->zErrMsg;.  u.
1f1aa 63 68 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  ch.pVtab->zErrMs
1f1ab 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e 63  g = 0;.  if( u.c
1f1ac 68 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72 72  h.sContext.isErr
1f1ad 6f 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 75  or ){.    rc = u
1f1ae 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45  .ch.sContext.isE
1f1af 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rror;.  }..  /* 
1f1b0 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74 20  Copy the result 
1f1b1 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  of the function 
1f1b2 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74  to the P3 regist
1f1b3 65 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74  er. We.  ** do t
1f1b4 68 69 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f  his regardless o
1f1b5 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  f whether or not
1f1b6 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72   an error occurr
1f1b7 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61 6e 79  ed to ensure any
1f1b8 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c  .  ** dynamic al
1f1b9 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 75 2e 63 68  location in u.ch
1f1ba 2e 73 43 6f 6e 74 65 78 74 2e 73 20 28 61 20 4d  .sContext.s (a M
1f1bb 65 6d 20 73 74 72 75 63 74 29 20 69 73 20 20 72  em struct) is  r
1f1bc 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20  eleased..  */.  
1f1bd 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f1be 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e 63 68 2e  eEncoding(&u.ch.
1f1bf 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f  sContext.s, enco
1f1c0 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53 54 45  ding);.  REGISTE
1f1c1 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
1f1c2 20 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a 20 20   u.ch.pDest);.  
1f1c3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
1f1c4 76 65 28 75 2e 63 68 2e 70 44 65 73 74 2c 20 26  ve(u.ch.pDest, &
1f1c5 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e 73 29  u.ch.sContext.s)
1f1c6 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ;.  UPDATE_MAX_B
1f1c7 4c 4f 42 53 49 5a 45 28 75 2e 63 68 2e 70 44 65  LOBSIZE(u.ch.pDe
1f1c8 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69  st);..  if( sqli
1f1c9 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1f1ca 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  ){.    goto abor
1f1cb 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1f1cc 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
1f1cd 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
1f1ce 75 2e 63 68 2e 70 44 65 73 74 29 20 29 7b 0a 20  u.ch.pDest) ){. 
1f1cf 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1f1d0 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1f1d1 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1f1d2 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1f1d3 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  LE */..#ifndef S
1f1d4 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1f1d5 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
1f1d6 65 3a 20 56 4e 65 78 74 20 50 31 20 50 32 20 2a  e: VNext P1 P2 *
1f1d7 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e   * *.**.** Advan
1f1d8 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ce virtual table
1f1d9 20 50 31 20 74 6f 20 74 68 65 20 6e 65 78 74 20   P1 to the next 
1f1da 72 6f 77 20 69 6e 20 69 74 73 20 72 65 73 75 6c  row in its resul
1f1db 74 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d  t set and.** jum
1f1dc 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e  p to instruction
1f1dd 20 50 32 2e 20 20 4f 72 2c 20 69 66 20 74 68 65   P2.  Or, if the
1f1de 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68   virtual table h
1f1df 61 73 20 72 65 61 63 68 65 64 0a 2a 2a 20 74 68  as reached.** th
1f1e0 65 20 65 6e 64 20 6f 66 20 69 74 73 20 72 65 73  e end of its res
1f1e1 75 6c 74 20 73 65 74 2c 20 74 68 65 6e 20 66 61  ult set, then fa
1f1e2 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1f1e3 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1f1e4 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  on..*/.case OP_V
1f1e5 4e 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d  Next: {   /* jum
1f1e6 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  p */.#if 0  /* l
1f1e7 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f1e8 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 69 20 2a  oved into u.ci *
1f1e9 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
1f1ea 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74   *pVtab;.  const
1f1eb 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1f1ec 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20  *pModule;.  int 
1f1ed 72 65 73 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  res;.  VdbeCurso
1f1ee 72 20 2a 70 43 75 72 3b 0a 23 65 6e 64 69 66 20  r *pCur;.#endif 
1f1ef 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1f1f0 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1f1f1 63 69 20 2a 2f 0a 0a 20 20 75 2e 63 69 2e 72 65  ci */..  u.ci.re
1f1f2 73 20 3d 20 30 3b 0a 20 20 75 2e 63 69 2e 70 43  s = 0;.  u.ci.pC
1f1f3 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  ur = p->apCsr[pO
1f1f4 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1f1f5 28 20 75 2e 63 69 2e 70 43 75 72 2d 3e 70 56 74  ( u.ci.pCur->pVt
1f1f6 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66  abCursor );.  if
1f1f7 28 20 75 2e 63 69 2e 70 43 75 72 2d 3e 6e 75 6c  ( u.ci.pCur->nul
1f1f8 6c 52 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 61  lRow ){.    brea
1f1f9 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 69 2e 70 56  k;.  }.  u.ci.pV
1f1fa 74 61 62 20 3d 20 75 2e 63 69 2e 70 43 75 72 2d  tab = u.ci.pCur-
1f1fb 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56  >pVtabCursor->pV
1f1fc 74 61 62 3b 0a 20 20 75 2e 63 69 2e 70 4d 6f 64  tab;.  u.ci.pMod
1f1fd 75 6c 65 20 3d 20 75 2e 63 69 2e 70 56 74 61 62  ule = u.ci.pVtab
1f1fe 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73  ->pModule;.  ass
1f1ff 65 72 74 28 20 75 2e 63 69 2e 70 4d 6f 64 75 6c  ert( u.ci.pModul
1f200 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 20 2f  e->xNext );..  /
1f201 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e 65  * Invoke the xNe
1f202 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74  xt() method of t
1f203 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72 65  he module. There
1f204 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 74   is no way for t
1f205 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69  he.  ** underlyi
1f206 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
1f207 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65  n to return an e
1f208 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63 75  rror if one occu
1f209 72 73 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 78  rs during.  ** x
1f20a 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61 64 2c  Next(). Instead,
1f20b 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
1f20c 75 72 73 2c 20 74 72 75 65 20 69 73 20 72 65 74  urs, true is ret
1f20d 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74 69 6e  urned (indicatin
1f20e 67 20 74 68 61 74 0a 20 20 2a 2a 20 64 61 74 61  g that.  ** data
1f20f 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29 20 61   is available) a
1f210 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  nd the error cod
1f211 65 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20  e returned when 
1f212 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20  xColumn or.  ** 
1f213 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 68 6f  some other metho
1f214 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f 6b 65  d is next invoke
1f215 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20 76 69  d on the save vi
1f216 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73  rtual table curs
1f217 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  or..  */.  if( s
1f218 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1f219 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1f21a 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1f21b 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
1f21c 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 75 2e  d = 1;.  rc = u.
1f21d 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78  ci.pModule->xNex
1f21e 74 28 75 2e 63 69 2e 70 43 75 72 2d 3e 70 56 74  t(u.ci.pCur->pVt
1f21f 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 2d 3e  abCursor);.  p->
1f220 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30  inVtabMethod = 0
1f221 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1f222 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1f223 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  );.  p->zErrMsg 
1f224 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45  = u.ci.pVtab->zE
1f225 72 72 4d 73 67 3b 0a 20 20 75 2e 63 69 2e 70 56  rrMsg;.  u.ci.pV
1f226 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30  tab->zErrMsg = 0
1f227 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1f228 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 2e 63  TE_OK ){.    u.c
1f229 69 2e 72 65 73 20 3d 20 75 2e 63 69 2e 70 4d 6f  i.res = u.ci.pMo
1f22a 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 69 2e  dule->xEof(u.ci.
1f22b 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
1f22c 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  r);.  }.  if( sq
1f22d 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1f22e 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1f22f 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20  ue_to_misuse;.. 
1f230 20 69 66 28 20 21 75 2e 63 69 2e 72 65 73 20 29   if( !u.ci.res )
1f231 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  {.    /* If ther
1f232 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d 70 20  e is data, jump 
1f233 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70 63 20  to P2 */.    pc 
1f234 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1f235 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1f236 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1f237 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f238 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1f239 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1f23a 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
1f23b 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20 2a 20   VRename P1 * * 
1f23c 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  P4 *.**.** P4 is
1f23d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1f23e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62  virtual table ob
1f23f 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33  ject, an sqlite3
1f240 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e  _vtab structure.
1f241 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1f242 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72  invokes the corr
1f243 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d  esponding xRenam
1f244 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 76 61  e method. The va
1f245 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74  lue.** in regist
1f246 65 72 20 50 31 20 69 73 20 70 61 73 73 65 64 20  er P1 is passed 
1f247 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61 72 67  as the zName arg
1f248 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78 52 65  ument to the xRe
1f249 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  name method..*/.
1f24a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65 3a  case OP_VRename:
1f24b 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1f24c 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f24d 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a  ed into u.cj */.
1f24e 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1f24f 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a 70 4e  pVtab;.  Mem *pN
1f250 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  ame;.#endif /* l
1f251 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f252 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a  oved into u.cj *
1f253 2f 0a 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 20  /..  u.cj.pVtab 
1f254 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d  = pOp->p4.pVtab-
1f255 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 6a 2e 70  >pVtab;.  u.cj.p
1f256 4e 61 6d 65 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  Name = &aMem[pOp
1f257 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1f258 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f   u.cj.pVtab->pMo
1f259 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b  dule->xRename );
1f25a 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1f25b 45 28 70 4f 70 2d 3e 70 31 2c 20 75 2e 63 6a 2e  E(pOp->p1, u.cj.
1f25c 70 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74  pName);.  assert
1f25d 28 20 75 2e 63 6a 2e 70 4e 61 6d 65 2d 3e 66 6c  ( u.cj.pName->fl
1f25e 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b  ags & MEM_Str );
1f25f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1f260 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1f261 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1f262 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75  misuse;.  rc = u
1f263 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  .cj.pVtab->pModu
1f264 6c 65 2d 3e 78 52 65 6e 61 6d 65 28 75 2e 63 6a  le->xRename(u.cj
1f265 2e 70 56 74 61 62 2c 20 75 2e 63 6a 2e 70 4e 61  .pVtab, u.cj.pNa
1f266 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65  me->z);.  sqlite
1f267 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1f268 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
1f269 72 72 4d 73 67 20 3d 20 75 2e 63 6a 2e 70 56 74  rrMsg = u.cj.pVt
1f26a 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75  ab->zErrMsg;.  u
1f26b 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .cj.pVtab->zErrM
1f26c 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71  sg = 0;.  if( sq
1f26d 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1f26e 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1f26f 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20  ue_to_misuse;.. 
1f270 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f271 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1f272 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1f273 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55  LE./* Opcode: VU
1f274 70 64 61 74 65 20 50 31 20 50 32 20 50 33 20 50  pdate P1 P2 P3 P
1f275 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20  4 *.**.** P4 is 
1f276 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76  a pointer to a v
1f277 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a  irtual table obj
1f278 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f  ect, an sqlite3_
1f279 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a  vtab structure..
1f27a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
1f27b 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65  nvokes the corre
1f27c 73 70 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 65  sponding xUpdate
1f27d 20 6d 65 74 68 6f 64 2e 20 50 32 20 76 61 6c 75   method. P2 valu
1f27e 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67  es.** are contig
1f27f 75 6f 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  uous memory cell
1f280 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 50 33  s starting at P3
1f281 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20   to pass to the 
1f282 78 55 70 64 61 74 65 20 0a 2a 2a 20 69 6e 76 6f  xUpdate .** invo
1f283 63 61 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75  cation. The valu
1f284 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 28 50  e in register (P
1f285 33 2b 50 32 2d 31 29 20 63 6f 72 72 65 73 70 6f  3+P2-1) correspo
1f286 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70  nds to the .** p
1f287 32 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  2th element of t
1f288 68 65 20 61 72 67 76 20 61 72 72 61 79 20 70 61  he argv array pa
1f289 73 73 65 64 20 74 6f 20 78 55 70 64 61 74 65 2e  ssed to xUpdate.
1f28a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 61  .**.** The xUpda
1f28b 74 65 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64  te method will d
1f28c 6f 20 61 20 44 45 4c 45 54 45 20 6f 72 20 61 6e  o a DELETE or an
1f28d 20 49 4e 53 45 52 54 20 6f 72 20 62 6f 74 68 2e   INSERT or both.
1f28e 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b 30 5d 20  .** The argv[0] 
1f28f 65 6c 65 6d 65 6e 74 20 28 77 68 69 63 68 20 63  element (which c
1f290 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 6d 65  orresponds to me
1f291 6d 6f 72 79 20 63 65 6c 6c 20 50 33 29 0a 2a 2a  mory cell P3).**
1f292 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66   is the rowid of
1f293 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65   a row to delete
1f294 2e 20 20 49 66 20 61 72 67 76 5b 30 5d 20 69 73  .  If argv[0] is
1f295 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a   NULL then no .*
1f296 2a 20 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75 72  * deletion occur
1f297 73 2e 20 20 54 68 65 20 61 72 67 76 5b 31 5d 20  s.  The argv[1] 
1f298 65 6c 65 6d 65 6e 74 20 69 73 20 74 68 65 20 72  element is the r
1f299 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20  owid of the new 
1f29a 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 69 73 20 63  .** row.  This c
1f29b 61 6e 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61  an be NULL to ha
1f29c 76 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ve the virtual t
1f29d 61 62 6c 65 20 73 65 6c 65 63 74 20 74 68 65 20  able select the 
1f29e 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 64 20 66 6f  new .** rowid fo
1f29f 72 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 73  r itself.  The s
1f2a0 75 62 73 65 71 75 65 6e 74 20 65 6c 65 6d 65 6e  ubsequent elemen
1f2a1 74 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 20  ts in the array 
1f2a2 61 72 65 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75  are .** the valu
1f2a3 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  es of columns in
1f2a4 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a   the new row..**
1f2a5 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 20 74 68 65  .** If P2==1 the
1f2a6 6e 20 6e 6f 20 69 6e 73 65 72 74 20 69 73 20 70  n no insert is p
1f2a7 65 72 66 6f 72 6d 65 64 2e 20 20 61 72 67 76 5b  erformed.  argv[
1f2a8 30 5d 20 69 73 20 74 68 65 20 72 6f 77 69 64 20  0] is the rowid 
1f2a9 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74 6f 20 64  of.** a row to d
1f2aa 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  elete..**.** P1 
1f2ab 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61  is a boolean fla
1f2ac 67 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 20  g. If it is set 
1f2ad 74 6f 20 74 72 75 65 20 61 6e 64 20 74 68 65 20  to true and the 
1f2ae 78 55 70 64 61 74 65 20 63 61 6c 6c 0a 2a 2a 20  xUpdate call.** 
1f2af 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74  is successful, t
1f2b0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
1f2b1 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
1f2b2 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
1f2b3 77 69 64 28 29 20 0a 2a 2a 20 69 73 20 73 65 74  wid() .** is set
1f2b4 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
1f2b5 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74   the rowid for t
1f2b6 68 65 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65  he row just inse
1f2b7 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  rted..*/.case OP
1f2b8 5f 56 55 70 64 61 74 65 3a 20 7b 0a 23 69 66 20  _VUpdate: {.#if 
1f2b9 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f2ba 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f2bb 20 75 2e 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74   u.ck */.  sqlit
1f2bc 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1f2bd 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65    sqlite3_module
1f2be 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74   *pModule;.  int
1f2bf 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 3b 0a   nArg;.  int i;.
1f2c0 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72    sqlite_int64 r
1f2c1 6f 77 69 64 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70  owid;.  Mem **ap
1f2c2 41 72 67 3b 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a  Arg;.  Mem *pX;.
1f2c3 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1f2c4 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1f2c5 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20  into u.ck */..  
1f2c6 75 2e 63 6b 2e 70 56 74 61 62 20 3d 20 70 4f 70  u.ck.pVtab = pOp
1f2c7 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61  ->p4.pVtab->pVta
1f2c8 62 3b 0a 20 20 75 2e 63 6b 2e 70 4d 6f 64 75 6c  b;.  u.ck.pModul
1f2c9 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64  e = (sqlite3_mod
1f2ca 75 6c 65 20 2a 29 75 2e 63 6b 2e 70 56 74 61 62  ule *)u.ck.pVtab
1f2cb 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 75 2e 63  ->pModule;.  u.c
1f2cc 6b 2e 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32  k.nArg = pOp->p2
1f2cd 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1f2ce 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42  >p4type==P4_VTAB
1f2cf 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53   );.  if( ALWAYS
1f2d0 28 75 2e 63 6b 2e 70 4d 6f 64 75 6c 65 2d 3e 78  (u.ck.pModule->x
1f2d1 55 70 64 61 74 65 29 20 29 7b 0a 20 20 20 20 75  Update) ){.    u
1f2d2 2e 63 6b 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61  .ck.apArg = p->a
1f2d3 70 41 72 67 3b 0a 20 20 20 20 75 2e 63 6b 2e 70  pArg;.    u.ck.p
1f2d4 58 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  X = &aMem[pOp->p
1f2d5 33 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 6b  3];.    for(u.ck
1f2d6 2e 69 3d 30 3b 20 75 2e 63 6b 2e 69 3c 75 2e 63  .i=0; u.ck.i<u.c
1f2d7 6b 2e 6e 41 72 67 3b 20 75 2e 63 6b 2e 69 2b 2b  k.nArg; u.ck.i++
1f2d8 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1f2d9 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65  VdbeMemStoreType
1f2da 28 75 2e 63 6b 2e 70 58 29 3b 0a 20 20 20 20 20  (u.ck.pX);.     
1f2db 20 75 2e 63 6b 2e 61 70 41 72 67 5b 75 2e 63 6b   u.ck.apArg[u.ck
1f2dc 2e 69 5d 20 3d 20 75 2e 63 6b 2e 70 58 3b 0a 20  .i] = u.ck.pX;. 
1f2dd 20 20 20 20 20 75 2e 63 6b 2e 70 58 2b 2b 3b 0a       u.ck.pX++;.
1f2de 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
1f2df 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1f2e0 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1f2e1 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1f2e2 20 20 20 72 63 20 3d 20 75 2e 63 6b 2e 70 4d 6f     rc = u.ck.pMo
1f2e3 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 28 75 2e  dule->xUpdate(u.
1f2e4 63 6b 2e 70 56 74 61 62 2c 20 75 2e 63 6b 2e 6e  ck.pVtab, u.ck.n
1f2e5 41 72 67 2c 20 75 2e 63 6b 2e 61 70 41 72 67 2c  Arg, u.ck.apArg,
1f2e6 20 26 75 2e 63 6b 2e 72 6f 77 69 64 29 3b 0a 20   &u.ck.rowid);. 
1f2e7 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1f2e8 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1f2e9 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
1f2ea 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 7a   = u.ck.pVtab->z
1f2eb 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 6b  ErrMsg;.    u.ck
1f2ec 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20  .pVtab->zErrMsg 
1f2ed 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  = 0;.    if( sql
1f2ee 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1f2ef 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1f2f0 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
1f2f1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1f2f2 4f 4b 20 26 26 20 70 4f 70 2d 3e 70 31 20 29 7b  OK && pOp->p1 ){
1f2f3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75  .      assert( u
1f2f4 2e 63 6b 2e 6e 41 72 67 3e 31 20 26 26 20 75 2e  .ck.nArg>1 && u.
1f2f5 63 6b 2e 61 70 41 72 67 5b 30 5d 20 26 26 20 28  ck.apArg[0] && (
1f2f6 75 2e 63 6b 2e 61 70 41 72 67 5b 30 5d 2d 3e 66  u.ck.apArg[0]->f
1f2f7 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29  lags&MEM_Null) )
1f2f8 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74  ;.      db->last
1f2f9 52 6f 77 69 64 20 3d 20 75 2e 63 6b 2e 72 6f 77  Rowid = u.ck.row
1f2fa 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  id;.    }.    p-
1f2fb 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a  >nChange++;.  }.
1f2fc 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1f2fd 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1f2fe 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1f2ff 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 4c 49 54  ..#ifndef  SQLIT
1f300 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
1f301 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  GMAS./* Opcode: 
1f302 50 61 67 65 63 6f 75 6e 74 20 50 31 20 50 32 20  Pagecount P1 P2 
1f303 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  * * *.**.** Writ
1f304 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75  e the current nu
1f305 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
1f306 20 64 61 74 61 62 61 73 65 20 50 31 20 74 6f 20   database P1 to 
1f307 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32 2e 0a  memory cell P2..
1f308 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 67 65 63  */.case OP_Pagec
1f309 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20  ount: {         
1f30a 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1f30b 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20  lease */.#if 0  
1f30c 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1f30d 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1f30e 63 6c 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 0a  cl */.  int p1;.
1f30f 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 50    int nPage;.  P
1f310 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 23 65  ager *pPager;.#e
1f311 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1f312 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f313 74 6f 20 75 2e 63 6c 20 2a 2f 0a 0a 20 20 75 2e  to u.cl */..  u.
1f314 63 6c 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b  cl.p1 = pOp->p1;
1f315 0a 20 20 75 2e 63 6c 2e 70 50 61 67 65 72 20 3d  .  u.cl.pPager =
1f316 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
1f317 65 72 28 64 62 2d 3e 61 44 62 5b 75 2e 63 6c 2e  er(db->aDb[u.cl.
1f318 70 31 5d 2e 70 42 74 29 3b 0a 20 20 72 63 20 3d  p1].pBt);.  rc =
1f319 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
1f31a 65 63 6f 75 6e 74 28 75 2e 63 6c 2e 70 50 61 67  ecount(u.cl.pPag
1f31b 65 72 2c 20 26 75 2e 63 6c 2e 6e 50 61 67 65 29  er, &u.cl.nPage)
1f31c 3b 0a 20 20 2f 2a 20 4f 50 5f 50 61 67 65 63 6f  ;.  /* OP_Pageco
1f31d 75 6e 74 20 69 73 20 61 6c 77 61 79 73 20 63 61  unt is always ca
1f31e 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
1f31f 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74   a read transact
1f320 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a 2a 20 70  ion.  The.  ** p
1f321 61 67 65 20 63 6f 75 6e 74 20 68 61 73 20 61 6c  age count has al
1f322 72 65 61 64 79 20 62 65 65 6e 20 73 75 63 63 65  ready been succe
1f323 73 73 66 75 6c 6c 79 20 72 65 61 64 20 61 6e 64  ssfully read and
1f324 20 63 61 63 68 65 64 2e 20 20 53 6f 20 74 68 65   cached.  So the
1f325 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67  .  ** sqlite3Pag
1f326 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 63 61  erPagecount() ca
1f327 6c 6c 20 61 62 6f 76 65 20 63 61 6e 6e 6f 74 20  ll above cannot 
1f328 66 61 69 6c 2e 20 2a 2f 0a 20 20 69 66 28 20 41  fail. */.  if( A
1f329 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45  LWAYS(rc==SQLITE
1f32a 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 70 4f 75 74  _OK) ){.    pOut
1f32b 2d 3e 75 2e 69 20 3d 20 75 2e 63 6c 2e 6e 50 61  ->u.i = u.cl.nPa
1f32c 67 65 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  ge;.  }.  break;
1f32d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
1f32e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1f32f 52 41 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  RACE./* Opcode: 
1f330 54 72 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a  Trace * * * P4 *
1f331 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e  .**.** If tracin
1f332 67 20 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79  g is enabled (by
1f333 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61   the sqlite3_tra
1f334 63 65 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c  ce()) interface,
1f335 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46   then.** the UTF
1f336 2d 38 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69  -8 string contai
1f337 6e 65 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69  ned in P4 is emi
1f338 74 74 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63  tted on the trac
1f339 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63  e callback..*/.c
1f33a 61 73 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a  ase OP_Trace: {.
1f33b 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1f33c 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1f33d 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 20 20 63  into u.cm */.  c
1f33e 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a 23 65 6e  har *zTrace;.#en
1f33f 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1f340 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1f341 6f 20 75 2e 63 6d 20 2a 2f 0a 0a 20 20 75 2e 63  o u.cm */..  u.c
1f342 6d 2e 7a 54 72 61 63 65 20 3d 20 28 70 4f 70 2d  m.zTrace = (pOp-
1f343 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e  >p4.z ? pOp->p4.
1f344 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20  z : p->zSql);.  
1f345 69 66 28 20 75 2e 63 6d 2e 7a 54 72 61 63 65 20  if( u.cm.zTrace 
1f346 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78  ){.    if( db->x
1f347 54 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 64  Trace ){.      d
1f348 62 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54  b->xTrace(db->pT
1f349 72 61 63 65 41 72 67 2c 20 75 2e 63 6d 2e 7a 54  raceArg, u.cm.zT
1f34a 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66  race);.    }.#if
1f34b 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1f34c 0a 20 20 20 20 69 66 28 20 28 64 62 2d 3e 66 6c  .    if( (db->fl
1f34d 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 71 6c  ags & SQLITE_Sql
1f34e 54 72 61 63 65 29 21 3d 30 20 29 7b 0a 20 20 20  Trace)!=0 ){.   
1f34f 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
1f350 72 69 6e 74 66 28 22 53 51 4c 2d 74 72 61 63 65  rintf("SQL-trace
1f351 3a 20 25 73 5c 6e 22 2c 20 75 2e 63 6d 2e 7a 54  : %s\n", u.cm.zT
1f352 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  race);.    }.#en
1f353 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45  dif /* SQLITE_DE
1f354 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 20 62 72 65  BUG */.  }.  bre
1f355 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f  ak;.}.#endif.../
1f356 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a  * Opcode: Noop *
1f357 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44   * * * *.**.** D
1f358 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 69 73  o nothing.  This
1f359 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
1f35a 6f 66 74 65 6e 20 75 73 65 66 75 6c 20 61 73 20  often useful as 
1f35b 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 69 6e  a jump.** destin
1f35c 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20  ation..*/./*.** 
1f35d 54 68 65 20 6d 61 67 69 63 20 45 78 70 6c 61 69  The magic Explai
1f35e 6e 20 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e 6c  n opcode are onl
1f35f 79 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e 20  y inserted when 
1f360 65 78 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69 63  explain==2 (whic
1f361 68 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79 20 77  h.** is to say w
1f362 68 65 6e 20 74 68 65 20 45 58 50 4c 41 49 4e 20  hen the EXPLAIN 
1f363 51 55 45 52 59 20 50 4c 41 4e 20 73 79 6e 74 61  QUERY PLAN synta
1f364 78 20 69 73 20 75 73 65 64 2e 29 0a 2a 2a 20 54  x is used.).** T
1f365 68 69 73 20 6f 70 63 6f 64 65 20 72 65 63 6f 72  his opcode recor
1f366 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ds information f
1f367 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a 65  rom the optimize
1f368 72 2e 20 20 49 74 20 69 73 20 74 68 65 0a 2a 2a  r.  It is the.**
1f369 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 6e   the same as a n
1f36a 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f 70 63 6f  o-op.  This opco
1f36b 64 65 73 6e 65 76 65 72 20 61 70 70 65 61 72 73  desnever appears
1f36c 20 69 6e 20 61 20 72 65 61 6c 20 56 4d 20 70 72   in a real VM pr
1f36d 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 75 6c  ogram..*/.defaul
1f36e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a  t: {          /*
1f36f 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c 79 20   This is really 
1f370 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 5f 45  OP_Noop and OP_E
1f371 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 62 72 65 61  xplain */.  brea
1f372 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  k;.}../*********
1f373 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f374 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f375 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f376 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f377 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73 65  ****.** The case
1f378 73 20 6f 66 20 74 68 65 20 73 77 69 74 63 68 20  s of the switch 
1f379 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 20  statement above 
1f37a 74 68 69 73 20 6c 69 6e 65 20 73 68 6f 75 6c 64  this line should
1f37b 20 61 6c 6c 20 62 65 20 69 6e 64 65 6e 74 65 64   all be indented
1f37c 0a 2a 2a 20 62 79 20 36 20 73 70 61 63 65 73 2e  .** by 6 spaces.
1f37d 20 20 42 75 74 20 74 68 65 20 6c 65 66 74 2d 6d    But the left-m
1f37e 6f 73 74 20 36 20 73 70 61 63 65 73 20 68 61 76  ost 6 spaces hav
1f37f 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 74  e been removed t
1f380 6f 20 69 6d 70 72 6f 76 65 20 74 68 65 0a 2a 2a  o improve the.**
1f381 20 72 65 61 64 61 62 69 6c 69 74 79 2e 20 20 46   readability.  F
1f382 72 6f 6d 20 74 68 69 73 20 70 6f 69 6e 74 20 6f  rom this point o
1f383 6e 20 64 6f 77 6e 2c 20 74 68 65 20 6e 6f 72 6d  n down, the norm
1f384 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 72  al indentation r
1f385 75 6c 65 73 20 61 72 65 0a 2a 2a 20 72 65 73 74  ules are.** rest
1f386 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ored..**********
1f387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f38a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f38b 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a 23 69 66 64  ***/.    }..#ifd
1f38c 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a  ef VDBE_PROFILE.
1f38d 20 20 20 20 7b 0a 20 20 20 20 20 20 75 36 34 20      {.      u64 
1f38e 65 6c 61 70 73 65 64 20 3d 20 73 71 6c 69 74 65  elapsed = sqlite
1f38f 33 48 77 74 69 6d 65 28 29 20 2d 20 73 74 61 72  3Hwtime() - star
1f390 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 79  t;.      pOp->cy
1f391 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 65 64 3b  cles += elapsed;
1f392 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 6e 74 2b  .      pOp->cnt+
1f393 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20  +;.#if 0.       
1f394 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 74 2c   fprintf(stdout,
1f395 20 22 25 31 30 6c 6c 75 20 22 2c 20 65 6c 61 70   "%10llu ", elap
1f396 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71  sed);.        sq
1f397 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70  lite3VdbePrintOp
1f398 28 73 74 64 6f 75 74 2c 20 6f 72 69 67 50 63 2c  (stdout, origPc,
1f399 20 26 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a   &aOp[origPc]);.
1f39a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e  #endif.    }.#en
1f39b 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20  dif..    /* The 
1f39c 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61  following code a
1f39d 64 64 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74  dds nothing to t
1f39e 68 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69  he actual functi
1f39f 6f 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f  onality.    ** o
1f3a0 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20  f the program.  
1f3a1 49 74 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20  It is only here 
1f3a2 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20  for testing and 
1f3a3 64 65 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a  debugging..    *
1f3a4 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  * On the other h
1f3a5 61 6e 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72  and, it does bur
1f3a6 6e 20 43 50 55 20 63 79 63 6c 65 73 20 65 76 65  n CPU cycles eve
1f3a7 72 79 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a  ry time through.
1f3a8 20 20 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75      ** the evalu
1f3a9 61 74 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77  ator loop.  So w
1f3aa 65 20 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f  e can leave it o
1f3ab 75 74 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69  ut when NDEBUG i
1f3ac 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a  s defined..    *
1f3ad 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
1f3ae 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e  .    assert( pc>
1f3af 3d 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70  =-1 && pc<p->nOp
1f3b0 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   );..#ifdef SQLI
1f3b1 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28  TE_DEBUG.    if(
1f3b2 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20   p->trace ){.   
1f3b3 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 66     if( rc!=0 ) f
1f3b4 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c  printf(p->trace,
1f3b5 22 72 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20  "rc=%d\n",rc);. 
1f3b6 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70       if( pOp->op
1f3b7 66 6c 61 67 73 20 26 20 28 4f 50 46 4c 47 5f 4f  flags & (OPFLG_O
1f3b8 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 7c 4f  UT2_PRERELEASE|O
1f3b9 50 46 4c 47 5f 4f 55 54 32 29 20 29 7b 0a 20 20  PFLG_OUT2) ){.  
1f3ba 20 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72        registerTr
1f3bb 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f  ace(p->trace, pO
1f3bc 70 2d 3e 70 32 2c 20 26 61 4d 65 6d 5b 70 4f 70  p->p2, &aMem[pOp
1f3bd 2d 3e 70 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  ->p2]);.      }.
1f3be 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f        if( pOp->o
1f3bf 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f  pflags & OPFLG_O
1f3c0 55 54 33 20 29 7b 0a 20 20 20 20 20 20 20 20 72  UT3 ){.        r
1f3c1 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e  egisterTrace(p->
1f3c2 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 33 2c 20  trace, pOp->p3, 
1f3c3 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 29 3b  &aMem[pOp->p3]);
1f3c4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
1f3c5 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45  endif  /* SQLITE
1f3c6 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69 66  _DEBUG */.#endif
1f3c7 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20    /* NDEBUG */. 
1f3c8 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f   }  /* The end o
1f3c9 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c 6f  f the for(;;) lo
1f3ca 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68 72  op the loops thr
1f3cb 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f 0a  ough opcodes */.
1f3cc 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63  .  /* If we reac
1f3cd 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74  h this point, it
1f3ce 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65 63   means that exec
1f3cf 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 65  ution is finishe
1f3d0 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20 65  d with.  ** an e
1f3d1 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69 6e  rror of some kin
1f3d2 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 72 72  d..  */.vdbe_err
1f3d3 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 65 72  or_halt:.  asser
1f3d4 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e 72 63  t( rc );.  p->rc
1f3d5 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33   = rc;.  sqlite3
1f3d6 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 69  VdbeHalt(p);.  i
1f3d7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  f( rc==SQLITE_IO
1f3d8 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e  ERR_NOMEM ) db->
1f3d9 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1f3da 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  ;.  rc = SQLITE_
1f3db 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 65 73  ERROR;.  if( res
1f3dc 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 20  etSchemaOnFault 
1f3dd 29 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e  ) sqlite3ResetIn
1f3de 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
1f3df 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20   0);..  /* This 
1f3e0 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20  is the only way 
1f3e1 6f 75 74 20 6f 66 20 74 68 69 73 20 70 72 6f 63  out of this proc
1f3e2 65 64 75 72 65 2e 20 20 57 65 20 68 61 76 65 20  edure.  We have 
1f3e3 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20  to.  ** release 
1f3e4 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 62  the mutexes on b
1f3e5 74 72 65 65 73 20 74 68 61 74 20 77 65 72 65 20  trees that were 
1f3e6 61 63 71 75 69 72 65 64 20 61 74 20 74 68 65 0a  acquired at the.
1f3e7 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76 64 62    ** top. */.vdb
1f3e8 65 5f 72 65 74 75 72 6e 3a 0a 20 20 73 71 6c 69  e_return:.  sqli
1f3e9 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
1f3ea 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74  ayLeave(&p->aMut
1f3eb 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
1f3ec 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ;..  /* Jump to 
1f3ed 68 65 72 65 20 69 66 20 61 20 73 74 72 69 6e 67  here if a string
1f3ee 20 6f 72 20 62 6c 6f 62 20 6c 61 72 67 65 72 20   or blob larger 
1f3ef 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58 5f  than SQLITE_MAX_
1f3f0 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 69 73 20 65  LENGTH.  ** is e
1f3f1 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 20 20 2a 2f  ncountered..  */
1f3f2 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 73 71 6c 69  .too_big:.  sqli
1f3f3 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1f3f4 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 73  >zErrMsg, db, "s
1f3f5 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f  tring or blob to
1f3f6 6f 20 62 69 67 22 29 3b 0a 20 20 72 63 20 3d 20  o big");.  rc = 
1f3f7 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20  SQLITE_TOOBIG;. 
1f3f8 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72   goto vdbe_error
1f3f9 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  _halt;..  /* Jum
1f3fa 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 6d  p to here if a m
1f3fb 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 20  alloc() fails.. 
1f3fc 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62   */.no_mem:.  db
1f3fd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1f3fe 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74   1;.  sqlite3Set
1f3ff 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1f400 73 67 2c 20 64 62 2c 20 22 6f 75 74 20 6f 66 20  sg, db, "out of 
1f401 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 72 63 20 3d  memory");.  rc =
1f402 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1f403 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72   goto vdbe_error
1f404 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  _halt;..  /* Jum
1f405 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20 61 6e  p to here for an
1f406 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 65   SQLITE_MISUSE e
1f407 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74  rror..  */.abort
1f408 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3a 0a  _due_to_misuse:.
1f409 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49    rc = SQLITE_MI
1f40a 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20  SUSE;.  /* Fall 
1f40b 74 68 72 75 20 69 6e 74 6f 20 61 62 6f 72 74 5f  thru into abort_
1f40c 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20 2a 2f 0a  due_to_error */.
1f40d 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65  .  /* Jump to he
1f40e 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72  re for any other
1f40f 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 6c 20 65   kind of fatal e
1f410 72 72 6f 72 2e 20 20 54 68 65 20 22 72 63 22 20  rror.  The "rc" 
1f411 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20 73 68  variable.  ** sh
1f412 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 72  ould hold the er
1f413 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f  ror number..  */
1f414 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72  .abort_due_to_er
1f415 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28 20 70  ror:.  assert( p
1f416 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b 0a  ->zErrMsg==0 );.
1f417 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
1f418 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20 53 51  Failed ) rc = SQ
1f419 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66  LITE_NOMEM;.  if
1f41a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45  ( rc!=SQLITE_IOE
1f41b 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  RR_NOMEM ){.    
1f41c 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1f41d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
1f41e 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45  , "%s", sqlite3E
1f41f 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 7d 0a  rrStr(rc));.  }.
1f420 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f    goto vdbe_erro
1f421 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75  r_halt;..  /* Ju
1f422 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 74 68  mp to here if th
1f423 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  e sqlite3_interr
1f424 75 70 74 28 29 20 41 50 49 20 73 65 74 73 20 74  upt() API sets t
1f425 68 65 20 69 6e 74 65 72 72 75 70 74 0a 20 20 2a  he interrupt.  *
1f426 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f  * flag..  */.abo
1f427 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72  rt_due_to_interr
1f428 75 70 74 3a 0a 20 20 61 73 73 65 72 74 28 20 64  upt:.  assert( d
1f429 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
1f42a 74 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 53 51  ted );.  rc = SQ
1f42b 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a  LITE_INTERRUPT;.
1f42c 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
1f42d 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1f42e 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
1f42f 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45  , "%s", sqlite3E
1f430 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 67 6f  rrStr(rc));.  go
1f431 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
1f432 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  lt;.}../********
1f433 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
1f434 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.c ***********
1f435 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f436 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f437 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1f438 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1f439 65 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a  e vdbeblob.c ***
1f43a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f43b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f43c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1f43d 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68  7 May 1.**.** Th
1f43e 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1f43f 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1f440 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1f441 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1f442 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1f443 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1f444 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1f445 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1f446 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1f447 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1f448 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1f449 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1f44a 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1f44b 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1f44c 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1f44d 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1f44e 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1f44f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f451 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f452 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f453 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
1f454 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1f455 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70  code used to imp
1f456 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d 65 6e 74  lement increment
1f457 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a 2f 0a  al BLOB I/O..*/.
1f458 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1f459 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a  _OMIT_INCRBLOB..
1f45a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69  /*.** Valid sqli
1f45b 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65  te3_blob* handle
1f45c 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62  s point to Incrb
1f45d 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e 0a  lob structures..
1f45e 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
1f45f 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62  t Incrblob Incrb
1f460 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 72  lob;.struct Incr
1f461 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61  blob {.  int fla
1f462 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
1f463 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c 61   /* Copy of "fla
1f464 67 73 22 20 70 61 73 73 65 64 20 74 6f 20 73 71  gs" passed to sq
1f465 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
1f466 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  ) */.  int nByte
1f467 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1f468 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62  * Size of open b
1f469 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f  lob, in bytes */
1f46a 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20  .  int iOffset; 
1f46b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
1f46c 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f  te offset of blo
1f46d 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74 61  b in cursor data
1f46e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
1f46f 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f 2a  pCsr;         /*
1f470 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   Cursor pointing
1f471 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a   at blob row */.
1f472 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1f473 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61  pStmt;    /* Sta
1f474 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63  tement holding c
1f475 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20  ursor open */.  
1f476 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
1f477 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
1f478 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61  ssociated databa
1f479 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  se */.};../*.** 
1f47a 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64  Open a blob hand
1f47b 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
1f47c 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1f47d 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74  ob_open(.  sqlit
1f47e 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20  e3* db,         
1f47f 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1f480 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
1f481 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1f482 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  Db,        /* Th
1f483 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  e attached datab
1f484 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ase containing t
1f485 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e  he blob */.  con
1f486 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c  st char *zTable,
1f487 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
1f488 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
1f489 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74   blob */.  const
1f48a 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20   char *zColumn, 
1f48b 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e     /* The column
1f48c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1f48d 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65  blob */.  sqlite
1f48e 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20  _int64 iRow,    
1f48f 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e    /* The row con
1f490 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62  taining the glob
1f491 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c   */.  int flags,
1f492 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f493 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72   True -> read/wr
1f494 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73  ite access, fals
1f495 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a  e -> read-only *
1f496 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  /.  sqlite3_blob
1f497 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48   **ppBlob   /* H
1f498 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73  andle for access
1f499 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74  ing the blob ret
1f49a 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b  urned here */.){
1f49b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20  .  int nAttempt 
1f49c 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b  = 0;.  int iCol;
1f49d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f49e 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75  * Index of zColu
1f49f 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64  mn in row-record
1f4a0 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56   */..  /* This V
1f4a1 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b  DBE program seek
1f4a2 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f 72  s a btree cursor
1f4a3 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69   to the identifi
1f4a4 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c  ed .  ** db/tabl
1f4a5 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65  e/row entry. The
1f4a6 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e   reason for usin
1f4a7 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d  g a vdbe program
1f4a8 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66   instead.  ** of
1f4a9 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f   writing code to
1f4aa 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65 20   use the b-tree 
1f4ab 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20 69  layer directly i
1f4ac 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20  s that the.  ** 
1f4ad 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c  vdbe program wil
1f4ae 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65  l take advantage
1f4af 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20   of the various 
1f4b0 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a  transaction,.  *
1f4b1 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72  * locking and er
1f4b2 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66  ror handling inf
1f4b3 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69 6c  rastructure buil
1f4b4 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e  t into the vdbe.
1f4b5 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72  .  **.  ** After
1f4b6 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72   seeking the cur
1f4b7 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65 78  sor, the vdbe ex
1f4b8 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 73  ecutes an OP_Res
1f4b9 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64  ultRow..  ** Cod
1f4ba 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 68  e external to th
1f4bb 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f 72  e Vdbe then "bor
1f4bc 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65 65  rows" the b-tree
1f4bd 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a   cursor and.  **
1f4be 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70 6c   uses it to impl
1f4bf 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72  ement the blob_r
1f4c0 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74  ead(), blob_writ
1f4c1 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c  e() and .  ** bl
1f4c2 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63 74  ob_bytes() funct
1f4c3 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ions..  **.  ** 
1f4c4 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  The sqlite3_blob
1f4c5 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f  _close() functio
1f4c6 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20  n finalizes the 
1f4c7 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20  vdbe program,.  
1f4c8 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73 20  ** which closes 
1f4c9 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f  the b-tree curso
1f4ca 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 29  r and (possibly)
1f4cb 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 20   commits the .  
1f4cc 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  ** transaction..
1f4cd 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f    */.  static co
1f4ce 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f  nst VdbeOpList o
1f4cf 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20  penBlob[] = {.  
1f4d0 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f    {OP_Transactio
1f4d1 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20  n, 0, 0, 0},    
1f4d2 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74   /* 0: Start a t
1f4d3 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
1f4d4 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b    {OP_VerifyCook
1f4d5 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  ie, 0, 0, 0},   
1f4d6 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65   /* 1: Check the
1f4d7 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a   schema cookie *
1f4d8 2f 0a 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65 4c  /.    {OP_TableL
1f4d9 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  ock, 0, 0, 0},  
1f4da 20 20 20 20 20 2f 2a 20 32 3a 20 41 63 71 75 69       /* 2: Acqui
1f4db 72 65 20 61 20 72 65 61 64 20 6f 72 20 77 72 69  re a read or wri
1f4dc 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20  te lock */..    
1f4dd 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f  /* One of the fo
1f4de 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74  llowing two inst
1f4df 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c  ructions is repl
1f4e0 61 63 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e 6f  aced by an OP_No
1f4e1 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f  op. */.    {OP_O
1f4e2 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30  penRead, 0, 0, 0
1f4e3 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20  },        /* 3: 
1f4e4 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f  Open cursor 0 fo
1f4e5 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20  r reading */.   
1f4e6 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20   {OP_OpenWrite, 
1f4e7 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20  0, 0, 0},       
1f4e8 2f 2a 20 34 3a 20 4f 70 65 6e 20 63 75 72 73 6f  /* 4: Open curso
1f4e9 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69  r 0 for read/wri
1f4ea 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56  te */..    {OP_V
1f4eb 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 31  ariable, 1, 1, 1
1f4ec 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 35 3a 20  },        /* 5: 
1f4ed 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20 74  Push the rowid t
1f4ee 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20  o the stack */. 
1f4ef 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73     {OP_NotExists
1f4f0 2c 20 30 2c 20 39 2c 20 31 7d 2c 20 20 20 20 20  , 0, 9, 1},     
1f4f1 20 20 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68 65    /* 6: Seek the
1f4f2 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b   cursor */.    {
1f4f3 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c  OP_Column, 0, 0,
1f4f4 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a   1},          /*
1f4f5 20 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52   7  */.    {OP_R
1f4f6 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20  esultRow, 1, 0, 
1f4f7 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 38 20 20  0},       /* 8  
1f4f8 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65  */.    {OP_Close
1f4f9 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20  , 0, 0, 0},     
1f4fa 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a 20        /* 9  */. 
1f4fb 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20     {OP_Halt, 0, 
1f4fc 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20  0, 0},          
1f4fd 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 7d 3b 0a    /* 10 */.  };.
1f4fe 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a  .  Vdbe *v = 0;.
1f4ff 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1f500 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 45  E_OK;.  char *zE
1f501 72 72 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20  rr = 0;.  Table 
1f502 2a 70 54 61 62 3b 0a 20 20 50 61 72 73 65 20 2a  *pTab;.  Parse *
1f503 70 50 61 72 73 65 3b 0a 0a 20 20 2a 70 70 42 6c  pParse;..  *ppBl
1f504 6f 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  ob = 0;.  sqlite
1f505 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
1f506 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 50 61 72  ->mutex);.  pPar
1f507 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
1f508 6b 41 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  kAllocRaw(db, si
1f509 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a  zeof(*pParse));.
1f50a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20    if( pParse==0 
1f50b 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
1f50c 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
1f50d 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
1f50e 3b 0a 20 20 7d 0a 20 20 64 6f 20 7b 0a 20 20 20  ;.  }.  do {.   
1f50f 20 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2c 20   memset(pParse, 
1f510 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29  0, sizeof(Parse)
1f511 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64  );.    pParse->d
1f512 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66 28  b = db;..    if(
1f513 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
1f514 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71  (db) ){.      sq
1f515 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1f516 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c  zErr);.      sql
1f517 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64 62  ite3StackFree(db
1f518 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  , pParse);.     
1f519 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1f51a 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
1f51b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1f51c 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20  LITE_MISUSE;.   
1f51d 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42   }..    sqlite3B
1f51e 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29  treeEnterAll(db)
1f51f 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c  ;.    pTab = sql
1f520 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
1f521 70 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c  pParse, 0, zTabl
1f522 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28  e, zDb);.    if(
1f523 20 70 54 61 62 20 26 26 20 49 73 56 69 72 74 75   pTab && IsVirtu
1f524 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
1f525 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20    pTab = 0;.    
1f526 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1f527 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
1f528 74 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 74  t open virtual t
1f529 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 6c  able: %s", zTabl
1f52a 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  e);.    }.#ifnde
1f52b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1f52c 45 57 0a 20 20 20 20 69 66 28 20 70 54 61 62 20  EW.    if( pTab 
1f52d 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  && pTab->pSelect
1f52e 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   ){.      pTab =
1f52f 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
1f530 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1f531 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76  , "cannot open v
1f532 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65  iew: %s", zTable
1f533 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
1f534 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 7b      if( !pTab ){
1f535 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73  .      if( pPars
1f536 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  e->zErrMsg ){.  
1f537 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1f538 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
1f539 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 70 50         zErr = pP
1f53a 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20  arse->zErrMsg;. 
1f53b 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a         pParse->z
1f53c 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20  ErrMsg = 0;.    
1f53d 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 53    }.      rc = S
1f53e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1f53f 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
1f540 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
1f541 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1f542 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
1f543 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1f544 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  pen_out;.    }..
1f545 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63      /* Now searc
1f546 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20 65  h pTab for the e
1f547 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a  xact column. */.
1f548 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
1f549 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f  iCol < pTab->nCo
1f54a 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20  l; iCol++) {.   
1f54b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
1f54c 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c  rICmp(pTab->aCol
1f54d 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  [iCol].zName, zC
1f54e 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  olumn)==0 ){.   
1f54f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1f550 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
1f551 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43  ( iCol==pTab->nC
1f552 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ol ){.      sqli
1f553 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
1f554 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20  rr);.      zErr 
1f555 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
1f556 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f  (db, "no such co
1f557 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 7a  lumn: \"%s\"", z
1f558 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 72  Column);.      r
1f559 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1f55a 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71  ;.      (void)sq
1f55b 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1f55c 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
1f55d 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
1f55e 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  b);.      goto b
1f55f 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  lob_open_out;.  
1f560 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
1f561 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69 6e  he value is bein
1f562 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69  g opened for wri
1f563 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 74  ting, check that
1f564 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75   the.    ** colu
1f565 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 65  mn is not indexe
1f566 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 69  d, and that it i
1f567 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20  s not part of a 
1f568 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 0a 20 20  foreign key. .  
1f569 20 20 2a 2a 20 49 74 20 69 73 20 61 67 61 69 6e    ** It is again
1f56a 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f 20  st the rules to 
1f56b 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 6f  open a column to
1f56c 20 77 68 69 63 68 20 65 69 74 68 65 72 20 6f 66   which either of
1f56d 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 64 65   these.    ** de
1f56e 73 63 72 69 70 74 69 6f 6e 73 20 61 70 70 6c 69  scriptions appli
1f56f 65 73 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20  es for writing. 
1f570 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 67   */.    if( flag
1f571 73 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  s ){.      const
1f572 20 63 68 61 72 20 2a 7a 46 61 75 6c 74 20 3d 20   char *zFault = 
1f573 30 3b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a  0;.      Index *
1f574 70 49 64 78 3b 0a 23 69 66 6e 64 65 66 20 53 51  pIdx;.#ifndef SQ
1f575 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
1f576 4e 5f 4b 45 59 0a 20 20 20 20 20 20 69 66 28 20  N_KEY.      if( 
1f577 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
1f578 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a  _ForeignKeys ){.
1f579 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
1f57a 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e   that the column
1f57b 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20   is not part of 
1f57c 61 6e 20 46 4b 20 63 68 69 6c 64 20 6b 65 79 20  an FK child key 
1f57d 64 65 66 69 6e 69 74 69 6f 6e 2e 20 49 74 0a 20  definition. It. 
1f57e 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74         ** is not
1f57f 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63 68   necessary to ch
1f580 65 63 6b 20 69 66 20 69 74 20 69 73 20 70 61 72  eck if it is par
1f581 74 20 6f 66 20 61 20 70 61 72 65 6e 74 20 6b 65  t of a parent ke
1f582 79 2c 20 61 73 20 70 61 72 65 6e 74 0a 20 20 20  y, as parent.   
1f583 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6c 75       ** key colu
1f584 6d 6e 73 20 6d 75 73 74 20 62 65 20 69 6e 64 65  mns must be inde
1f585 78 65 64 2e 20 54 68 65 20 63 68 65 63 6b 20 62  xed. The check b
1f586 65 6c 6f 77 20 77 69 6c 6c 20 70 69 63 6b 20 75  elow will pick u
1f587 70 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20  p this .        
1f588 2a 2a 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20  ** case.  */.   
1f589 20 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 79       FKey *pFKey
1f58a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 46  ;.        for(pF
1f58b 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b  Key=pTab->pFKey;
1f58c 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46   pFKey; pFKey=pF
1f58d 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b  Key->pNextFrom){
1f58e 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a  .          int j
1f58f 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
1f590 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79 2d 3e 6e 43  j=0; j<pFKey->nC
1f591 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; j++){.      
1f592 20 20 20 20 20 20 69 66 28 20 70 46 4b 65 79 2d        if( pFKey-
1f593 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 3d 3d  >aCol[j].iFrom==
1f594 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  iCol ){.        
1f595 20 20 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 22        zFault = "
1f596 66 6f 72 65 69 67 6e 20 6b 65 79 22 3b 0a 20 20  foreign key";.  
1f597 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1f598 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1f599 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
1f59a 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d  .      for(pIdx=
1f59b 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
1f59c 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
1f59d 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69  Next){.        i
1f59e 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 6f  nt j;.        fo
1f59f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e  r(j=0; j<pIdx->n
1f5a0 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20  Column; j++){.  
1f5a1 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
1f5a2 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69  ->aiColumn[j]==i
1f5a3 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Col ){.         
1f5a4 20 20 20 7a 46 61 75 6c 74 20 3d 20 22 69 6e 64     zFault = "ind
1f5a5 65 78 65 64 22 3b 0a 20 20 20 20 20 20 20 20 20  exed";.         
1f5a6 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1f5a7 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a     }.      if( z
1f5a8 46 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 20  Fault ){.       
1f5a9 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1f5aa 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
1f5ab 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33    zErr = sqlite3
1f5ac 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e  MPrintf(db, "can
1f5ad 6e 6f 74 20 6f 70 65 6e 20 25 73 20 63 6f 6c 75  not open %s colu
1f5ae 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 22 2c  mn for writing",
1f5af 20 7a 46 61 75 6c 74 29 3b 0a 20 20 20 20 20 20   zFault);.      
1f5b0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1f5b1 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 28 76 6f  ROR;.        (vo
1f5b2 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
1f5b3 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 20  Off(db);.       
1f5b4 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1f5b5 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20  veAll(db);.     
1f5b6 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65     goto blob_ope
1f5b7 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  n_out;.      }. 
1f5b8 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 71     }..    v = sq
1f5b9 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28  lite3VdbeCreate(
1f5ba 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 29  db);.    if( v )
1f5bb 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20  {.      int iDb 
1f5bc 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
1f5bd 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
1f5be 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20  >pSchema);.     
1f5bf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f5c0 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 28  pList(v, sizeof(
1f5c1 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f 66  openBlob)/sizeof
1f5c2 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f 70  (VdbeOpList), op
1f5c3 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 66  enBlob);.      f
1f5c4 6c 61 67 73 20 3d 20 21 21 66 6c 61 67 73 3b 20  lags = !!flags; 
1f5c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5c6 2f 2a 20 66 6c 61 67 73 20 3d 20 28 66 6c 61 67  /* flags = (flag
1f5c7 73 20 3f 20 31 20 3a 20 30 29 3b 20 2a 2f 0a 0a  s ? 1 : 0); */..
1f5c8 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
1f5c9 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73 61  re the OP_Transa
1f5ca 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73  ction */.      s
1f5cb 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f5cc 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a 20  P1(v, 0, iDb);. 
1f5cd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5ce 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20 66  ChangeP2(v, 0, f
1f5cf 6c 61 67 73 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  lags);..      /*
1f5d0 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f   Configure the O
1f5d1 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a  P_VerifyCookie *
1f5d2 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
1f5d3 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 31  dbeChangeP1(v, 1
1f5d4 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
1f5d5 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1f5d6 32 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53  2(v, 1, pTab->pS
1f5d7 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f  chema->schema_co
1f5d8 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  okie);..      /*
1f5d9 20 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 74   Make sure a mut
1f5da 65 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68  ex is held on th
1f5db 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63  e table to be ac
1f5dc 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20 20  cessed */.      
1f5dd 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
1f5de 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a 0a  tree(v, iDb); ..
1f5df 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
1f5e0 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c 65 4c  re the OP_TableL
1f5e1 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ock instruction 
1f5e2 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1f5e3 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
1f5e4 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  2, iDb);.      s
1f5e5 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f5e6 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d 3e 74  P2(v, 2, pTab->t
1f5e7 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  num);.      sqli
1f5e8 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
1f5e9 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b 0a 20 20  v, 2, flags);.  
1f5ea 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1f5eb 68 61 6e 67 65 50 34 28 76 2c 20 32 2c 20 70 54  hangeP4(v, 2, pT
1f5ec 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54 52  ab->zName, P4_TR
1f5ed 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 20 20 20 20  ANSIENT);..     
1f5ee 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68 65   /* Remove eithe
1f5ef 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72 69  r the OP_OpenWri
1f5f0 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e 20  te or OpenRead. 
1f5f1 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20 20  Set the P2 .    
1f5f2 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 6f    ** parameter o
1f5f3 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 70  f the other to p
1f5f4 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0a 20  Tab->tnum.  */. 
1f5f5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5f6 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20  ChangeToNoop(v, 
1f5f7 34 20 2d 20 66 6c 61 67 73 2c 20 31 29 3b 0a 20  4 - flags, 1);. 
1f5f8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5f9 43 68 61 6e 67 65 50 32 28 76 2c 20 33 20 2b 20  ChangeP2(v, 3 + 
1f5fa 66 6c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e 75  flags, pTab->tnu
1f5fb 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  m);.      sqlite
1f5fc 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
1f5fd 20 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62 29   3 + flags, iDb)
1f5fe 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66  ;..      /* Conf
1f5ff 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72  igure the number
1f600 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f 6e   of columns. Con
1f601 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 6f  figure the curso
1f602 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  r to.      ** th
1f603 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 62  ink that the tab
1f604 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 20  le has one more 
1f605 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 72  column than it r
1f606 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 64  eally.      ** d
1f607 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 6d  oes. An OP_Colum
1f608 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  n to retrieve th
1f609 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f 6c  is imaginary col
1f60a 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a  umn will.      *
1f60b 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  * always return 
1f60c 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 69  an SQL NULL. Thi
1f60d 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 61  s is useful beca
1f60e 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 20  use it means.   
1f60f 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e 76     ** we can inv
1f610 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f  oke OP_Column to
1f611 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 62   fill in the vdb
1f612 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 0a  e cursors type .
1f613 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 66        ** and off
1f614 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f 75  set cache withou
1f615 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 4f  t causing any IO
1f616 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1f617 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1f618 67 65 50 34 28 76 2c 20 33 2b 66 6c 61 67 73 2c  geP4(v, 3+flags,
1f619 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50   SQLITE_INT_TO_P
1f61a 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29  TR(pTab->nCol+1)
1f61b 2c 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20  ,P4_INT32);.    
1f61c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1f61d 6e 67 65 50 32 28 76 2c 20 37 2c 20 70 54 61 62  ngeP2(v, 7, pTab
1f61e 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 69  ->nCol);.      i
1f61f 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
1f620 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  iled ){.        
1f621 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52  sqlite3VdbeMakeR
1f622 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31 2c  eady(v, 1, 1, 1,
1f623 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20   0, 0, 0);.     
1f624 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 20   }.    }.   .   
1f625 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1f626 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 72  veAll(db);.    r
1f627 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74  c = sqlite3Safet
1f628 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 66  yOff(db);.    if
1f629 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49  ( NEVER(rc!=SQLI
1f62a 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 2d 3e 6d 61  TE_OK) || db->ma
1f62b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1f62c 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1f62d 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20  en_out;.    }.. 
1f62e 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
1f62f 69 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f 73  int64((sqlite3_s
1f630 74 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f 77  tmt *)v, 1, iRow
1f631 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1f632 74 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74 65  te3_step((sqlite
1f633 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20  3_stmt *)v);.   
1f634 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1f635 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 74  ROW ){.      nAt
1f636 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 72  tempt++;.      r
1f637 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  c = sqlite3_fina
1f638 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74  lize((sqlite3_st
1f639 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 73  mt *)v);.      s
1f63a 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f63b 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a 45   zErr);.      zE
1f63c 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  rr = sqlite3MPri
1f63d 6e 74 66 28 64 62 2c 20 73 71 6c 69 74 65 33 5f  ntf(db, sqlite3_
1f63e 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20  errmsg(db));.   
1f63f 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d 0a     v = 0;.    }.
1f640 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74 65    } while( nAtte
1f641 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51 4c  mpt<5 && rc==SQL
1f642 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a 20  ITE_SCHEMA );.. 
1f643 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1f644 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ROW ){.    /* Th
1f645 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61 73  e row-record has
1f646 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75 63   been opened suc
1f647 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63 6b  cessfully. Check
1f648 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a   that the.    **
1f649 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73 74   column in quest
1f64a 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65 78  ion contains tex
1f64b 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66 20  t or a blob. If 
1f64c 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20  it contains.    
1f64d 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20 75  ** text, it is u
1f64e 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20  p to the caller 
1f64f 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f 64  to get the encod
1f650 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20 2a  ing right..    *
1f651 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20 2a  /.    Incrblob *
1f652 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20 74  pBlob;.    u32 t
1f653 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30  ype = v->apCsr[0
1f654 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b 0a  ]->aType[iCol];.
1f655 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31 32  .    if( type<12
1f656 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1f657 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
1f658 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d 20  );.      zErr = 
1f659 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1f65a 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20  b, "cannot open 
1f65b 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25 73  value of type %s
1f65c 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79 70  ",.          typ
1f65d 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79 70  e==0?"null": typ
1f65e 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69 6e  e==7?"real": "in
1f65f 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b 0a  teger".      );.
1f660 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1f661 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 67  E_ERROR;.      g
1f662 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1f663 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c  t;.    }.    pBl
1f664 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a  ob = (Incrblob *
1f665 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  )sqlite3DbMalloc
1f666 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
1f667 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20 20  Incrblob));.    
1f668 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1f669 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71  iled ){.      sq
1f66a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1f66b 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67 6f  pBlob);.      go
1f66c 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
1f66d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c 6f  ;.    }.    pBlo
1f66e 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73  b->flags = flags
1f66f 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43 73  ;.    pBlob->pCs
1f670 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 5d  r =  v->apCsr[0]
1f671 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 73  ->pCursor;.    s
1f672 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1f673 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43  Cursor(pBlob->pC
1f674 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sr);.    sqlite3
1f675 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c  BtreeCacheOverfl
1f676 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b  ow(pBlob->pCsr);
1f677 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1f678 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42 6c  eLeaveCursor(pBl
1f679 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 70  ob->pCsr);.    p
1f67a 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 73  Blob->pStmt = (s
1f67b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 3b  qlite3_stmt *)v;
1f67c 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66 66  .    pBlob->iOff
1f67d 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30  set = v->apCsr[0
1f67e 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c 5d  ]->aOffset[iCol]
1f67f 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42 79  ;.    pBlob->nBy
1f680 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  te = sqlite3Vdbe
1f681 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79  SerialTypeLen(ty
1f682 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e  pe);.    pBlob->
1f683 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70 70  db = db;.    *pp
1f684 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 5f  Blob = (sqlite3_
1f685 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20  blob *)pBlob;.  
1f686 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
1f687 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63  ;.  }else if( rc
1f688 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1f689 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1f68a 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  (db, zErr);.    
1f68b 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50  zErr = sqlite3MP
1f68c 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75  rintf(db, "no su
1f68d 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 2c  ch rowid: %lld",
1f68e 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d   iRow);.    rc =
1f68f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1f690 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75   }..blob_open_ou
1f691 74 3a 0a 20 20 69 66 28 20 76 20 26 26 20 28 72  t:.  if( v && (r
1f692 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
1f693 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1f694 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
1f695 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b  VdbeFinalize(v);
1f696 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72  .  }.  sqlite3Er
1f697 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72 72  ror(db, rc, zErr
1f698 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1f699 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  ee(db, zErr);.  
1f69a 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65  sqlite3StackFree
1f69b 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20  (db, pParse);.  
1f69c 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
1f69d 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
1f69e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1f69f 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
1f6a0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1f6a1 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c 6f  *.** Close a blo
1f6a2 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61  b handle that wa
1f6a3 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 65  s previously cre
1f6a4 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71  ated using.** sq
1f6a5 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
1f6a6 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
1f6a7 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
1f6a8 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  b_close(sqlite3_
1f6a9 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20  blob *pBlob){.  
1f6aa 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49  Incrblob *p = (I
1f6ab 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b  ncrblob *)pBlob;
1f6ac 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
1f6ad 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28  ite3 *db;..  if(
1f6ae 20 70 20 29 7b 0a 20 20 20 20 64 62 20 3d 20 70   p ){.    db = p
1f6af 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65  ->db;.    sqlite
1f6b0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
1f6b1 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63  ->mutex);.    rc
1f6b2 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c   = sqlite3_final
1f6b3 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a 20  ize(p->pStmt);. 
1f6b4 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1f6b5 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c  (db, p);.    sql
1f6b6 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1f6b7 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  (db->mutex);.  }
1f6b8 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53  else{.    rc = S
1f6b9 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
1f6ba 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1f6bb 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65  .** Perform a re
1f6bc 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65 72  ad or write oper
1f6bd 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 0a  ation on a blob.
1f6be 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c  */.static int bl
1f6bf 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20 73  obReadWrite(.  s
1f6c0 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c  qlite3_blob *pBl
1f6c1 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c 20  ob, .  void *z, 
1f6c2 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e 74  .  int n, .  int
1f6c3 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e 74   iOffset, .  int
1f6c4 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 73   (*xCall)(BtCurs
1f6c5 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 76  or*, u32, u32, v
1f6c6 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  oid*).){.  int r
1f6c7 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70  c;.  Incrblob *p
1f6c8 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70   = (Incrblob *)p
1f6c9 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b  Blob;.  Vdbe *v;
1f6ca 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1f6cb 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
1f6cc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
1f6cd 53 45 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62  SE;.  db = p->db
1f6ce 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1f6cf 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
1f6d0 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65 2a  x);.  v = (Vdbe*
1f6d1 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 66  )p->pStmt;..  if
1f6d2 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65 74  ( n<0 || iOffset
1f6d3 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b 6e  <0 || (iOffset+n
1f6d4 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 20  )>p->nByte ){.  
1f6d5 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73 20    /* Request is 
1f6d6 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52 65  out of range. Re
1f6d7 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e 74  turn a transient
1f6d8 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 72   error. */.    r
1f6d9 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1f6da 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ;.    sqlite3Err
1f6db 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 52  or(db, SQLITE_ER
1f6dc 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c 73  ROR, 0);.  } els
1f6dd 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20  e if( v==0 ){.  
1f6de 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
1f6df 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68 61   no statement ha
1f6e0 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 62  ndle, then the b
1f6e1 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a 20  lob-handle has. 
1f6e2 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65     ** already be
1f6e3 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20  en invalidated. 
1f6e4 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42  Return SQLITE_AB
1f6e5 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61 73 65  ORT in this case
1f6e6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
1f6e7 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a  = SQLITE_ABORT;.
1f6e8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1f6e9 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72 65  Call either Btre
1f6ea 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65 65  eData() or Btree
1f6eb 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53 51  PutData(). If SQ
1f6ec 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20 20  LITE_ABORT is.  
1f6ed 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 63    ** returned, c
1f6ee 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61 74  lean-up the stat
1f6ef 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 20  ement handle..  
1f6f0 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
1f6f1 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b 0a   db == v->db );.
1f6f2 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1f6f3 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e 70  EnterCursor(p->p
1f6f4 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 78  Csr);.    rc = x
1f6f5 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f  Call(p->pCsr, iO
1f6f6 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74  ffset+p->iOffset
1f6f7 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c  , n, z);.    sql
1f6f8 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75  ite3BtreeLeaveCu
1f6f9 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20  rsor(p->pCsr);. 
1f6fa 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1f6fb 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 20  E_ABORT ){.     
1f6fc 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
1f6fd 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20 70  lize(v);.      p
1f6fe 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20  ->pStmt = 0;.   
1f6ff 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62   }else{.      db
1f700 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a  ->errCode = rc;.
1f701 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 63        v->rc = rc
1f702 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63  ;.    }.  }.  rc
1f703 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
1f704 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
1f705 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1f706 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
1f707 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1f708 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f  ** Read data fro
1f709 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e  m a blob handle.
1f70a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1f70b 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
1f70c 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f  read(sqlite3_blo
1f70d 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a  b *pBlob, void *
1f70e 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f  z, int n, int iO
1f70f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 6e  ffset){.  return
1f710 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 70   blobReadWrite(p
1f711 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66  Blob, z, n, iOff
1f712 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65  set, sqlite3Btre
1f713 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  eData);.}../*.**
1f714 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 61   Write data to a
1f715 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f   blob handle..*/
1f716 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1f717 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69  sqlite3_blob_wri
1f718 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  te(sqlite3_blob 
1f719 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 6f  *pBlob, const vo
1f71a 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e  id *z, int n, in
1f71b 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65  t iOffset){.  re
1f71c 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69  turn blobReadWri
1f71d 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64 20  te(pBlob, (void 
1f71e 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c  *)z, n, iOffset,
1f71f 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
1f720 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Data);.}../*.** 
1f721 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61 6e  Query a blob han
1f722 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a 65  dle for the size
1f723 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a 2a   of the data..**
1f724 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f 62  .** The Incrblob
1f725 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73 20  .nByte field is 
1f726 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c 69  fixed for the li
1f727 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49 6e  fetime of the In
1f728 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f 20  crblob.** so no 
1f729 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72 65  mutex is require
1f72a 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a 2f  d for access..*/
1f72b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1f72c 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
1f72d 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  es(sqlite3_blob 
1f72e 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62  *pBlob){.  Incrb
1f72f 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c  lob *p = (Incrbl
1f730 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 65  ob *)pBlob;.  re
1f731 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 42 79 74  turn p ? p->nByt
1f732 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66  e : 0;.}..#endif
1f733 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49   /* #ifndef SQLI
1f734 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
1f735 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
1f736 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65  **** End of vdbe
1f737 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  blob.c *********
1f738 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f739 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f73a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1f73b 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1f73c 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a  journal.c ******
1f73d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f73e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f73f 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
1f740 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a 20  August 22.**.** 
1f741 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
1f742 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
1f743 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
1f744 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
1f745 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
1f746 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
1f747 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
1f748 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1f749 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
1f74a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1f74b 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1f74c 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
1f74d 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
1f74e 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
1f74f 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
1f750 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
1f751 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
1f752 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f753 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f754 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f755 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f756 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
1f757 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
1f758 6e 74 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69  nts a special ki
1f759 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69  nd of sqlite3_fi
1f75a 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a  le object used.*
1f75b 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63  * by SQLite to c
1f75c 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69  reate journal fi
1f75d 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69  les if the atomi
1f75e 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
1f75f 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c  tion.** is enabl
1f760 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69  ed..**.** The di
1f761 73 74 69 6e 63 74 69 76 65 20 63 68 61 72 61 63  stinctive charac
1f762 74 65 72 69 73 74 69 63 20 6f 66 20 74 68 69 73  teristic of this
1f763 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73   sqlite3_file is
1f764 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74   that the.** act
1f765 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65  ual on disk file
1f766 20 69 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69   is created lazi
1f767 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c  ly. When the fil
1f768 65 20 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a  e is created,.**
1f769 20 74 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63   the caller spec
1f76a 69 66 69 65 73 20 61 20 62 75 66 66 65 72 20 73  ifies a buffer s
1f76b 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65  ize for an in-me
1f76c 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a  mory buffer to.*
1f76d 2a 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 72  * be used to ser
1f76e 76 69 63 65 20 72 65 61 64 28 29 20 61 6e 64 20  vice read() and 
1f76f 77 72 69 74 65 28 29 20 72 65 71 75 65 73 74 73  write() requests
1f770 2e 20 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c  . The actual fil
1f771 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20  e.** on disk is 
1f772 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72 20 70  not created or p
1f773 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65  opulated until e
1f774 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ither:.**.**   1
1f775 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  ) The in-memory 
1f776 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67  representation g
1f777 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66  rows too large f
1f778 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  or the allocated
1f779 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72   .**      buffer
1f77a 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65  , or.**   2) The
1f77b 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43   sqlite3JournalC
1f77c 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e  reate() function
1f77d 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23   is called..*/.#
1f77e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
1f77f 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
1f780 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e  .../*.** A Journ
1f781 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69 73  alFile object is
1f782 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73   a subclass of s
1f783 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65 64  qlite3_file used
1f784 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65   by.** as an ope
1f785 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f  n file handle fo
1f786 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e  r journal files.
1f787 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e  .*/.struct Journ
1f788 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74  alFile {.  sqlit
1f789 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70  e3_io_methods *p
1f78a 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f  Method;    /* I/
1f78b 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75  O methods on jou
1f78c 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20  rnal files */.  
1f78d 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20 20  int nBuf;       
1f78e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f78f 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b  /* Size of zBuf[
1f790 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  ] in bytes */.  
1f791 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20 20  char *zBuf;     
1f792 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f793 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66 66  /* Space to buff
1f794 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65  er journal write
1f795 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65  s */.  int iSize
1f796 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1f797 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74         /* Amount
1f798 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72 65   of zBuf[] curre
1f799 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 69  ntly used */.  i
1f79a 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nt flags;       
1f79b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f79c 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f  * xOpen flags */
1f79d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
1f79e 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20  pVfs;           
1f79f 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22     /* The "real"
1f7a0 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 20   underlying VFS 
1f7a1 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
1f7a2 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20  e *pReal;       
1f7a3 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61       /* The "rea
1f7a4 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69  l" underlying fi
1f7a5 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f  le descriptor */
1f7a6 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1f7a7 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20  Journal;        
1f7a8 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
1f7a9 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  e journal file *
1f7aa 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72  /.};.typedef str
1f7ab 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20  uct JournalFile 
1f7ac 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a  JournalFile;../*
1f7ad 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20 6e  .** If it does n
1f7ae 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
1f7af 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70 6f  s, create and po
1f7b0 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64 69  pulate the on-di
1f7b1 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20  sk file .** for 
1f7b2 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a  JournalFile p..*
1f7b3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 65  /.static int cre
1f7b4 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46  ateFile(JournalF
1f7b5 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ile *p){.  int r
1f7b6 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1f7b7 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 29   if( !p->pReal )
1f7b8 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  {.    sqlite3_fi
1f7b9 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c  le *pReal = (sql
1f7ba 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31  ite3_file *)&p[1
1f7bb 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ];.    rc = sqli
1f7bc 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66  te3OsOpen(p->pVf
1f7bd 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  s, p->zJournal, 
1f7be 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c  pReal, p->flags,
1f7bf 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d   0);.    if( rc=
1f7c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1f7c1 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70      p->pReal = p
1f7c2 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 20  Real;.      if( 
1f7c3 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20  p->iSize>0 ){.  
1f7c4 20 20 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e        assert(p->
1f7c5 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b  iSize<=p->nBuf);
1f7c6 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1f7c7 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e  lite3OsWrite(p->
1f7c8 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20  pReal, p->zBuf, 
1f7c9 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20  p->iSize, 0);.  
1f7ca 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1f7cb 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1f7cc 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20  /*.** Close the 
1f7cd 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1f7ce 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71  int jrnlClose(sq
1f7cf 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1f7d0 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  ){.  JournalFile
1f7d1 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69   *p = (JournalFi
1f7d2 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28  le *)pJfd;.  if(
1f7d3 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20   p->pReal ){.   
1f7d4 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
1f7d5 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20  p->pReal);.  }. 
1f7d6 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d   sqlite3_free(p-
1f7d7 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e  >zBuf);.  return
1f7d8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1f7d9 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66  *.** Read data f
1f7da 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  rom the file..*/
1f7db 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
1f7dc 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f  Read(.  sqlite3_
1f7dd 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f  file *pJfd,    /
1f7de 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
1f7df 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f  le from which to
1f7e0 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20   read */.  void 
1f7e1 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 20  *zBuf,          
1f7e2 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73    /* Put the res
1f7e3 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69  ults here */.  i
1f7e4 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20  nt iAmt,        
1f7e5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1f7e6 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64  of bytes to read
1f7e7 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74   */.  sqlite_int
1f7e8 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20  64 iOfst     /* 
1f7e9 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74  Begin reading at
1f7ea 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a   this offset */.
1f7eb 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1f7ec 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1f7ed 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1f7ee 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1f7ef 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20  .  if( p->pReal 
1f7f0 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1f7f1 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52 65  te3OsRead(p->pRe
1f7f2 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20  al, zBuf, iAmt, 
1f7f3 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20  iOfst);.  }else 
1f7f4 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 73 74 29  if( (iAmt+iOfst)
1f7f5 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20  >p->iSize ){.   
1f7f6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
1f7f7 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20  RR_SHORT_READ;. 
1f7f8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63   }else{.    memc
1f7f9 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75  py(zBuf, &p->zBu
1f7fa 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b  f[iOfst], iAmt);
1f7fb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f7fc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
1f7fd 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c   data to the fil
1f7fe 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1f7ff 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 73 71   jrnlWrite(.  sq
1f800 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1f801 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72  ,    /* The jour
1f802 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68  nal file into wh
1f803 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  ich to write */.
1f804 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42    const void *zB
1f805 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b 65  uf,      /* Take
1f806 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 74   data to be writ
1f807 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f  ten from here */
1f808 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20  .  int iAmt,    
1f809 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1f80a 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
1f80b 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74  write */.  sqlit
1f80c 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20  e_int64 iOfst   
1f80d 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 74 69    /* Begin writi
1f80e 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65  ng at this offse
1f80f 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20  t into the file 
1f810 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
1f811 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f   SQLITE_OK;.  Jo
1f812 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1f813 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1f814 66 64 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52  fd;.  if( !p->pR
1f815 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b 69 41  eal && (iOfst+iA
1f816 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20  mt)>p->nBuf ){. 
1f817 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 46 69     rc = createFi
1f818 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28  le(p);.  }.  if(
1f819 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1f81a 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 52 65  {.    if( p->pRe
1f81b 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  al ){.      rc =
1f81c 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
1f81d 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20  p->pReal, zBuf, 
1f81e 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20  iAmt, iOfst);.  
1f81f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
1f820 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69  emcpy(&p->zBuf[i
1f821 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d  Ofst], zBuf, iAm
1f822 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  t);.      if( p-
1f823 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41  >iSize<(iOfst+iA
1f824 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  mt) ){.        p
1f825 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f 66 73 74  ->iSize = (iOfst
1f826 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a  +iAmt);.      }.
1f827 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1f828 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1f829 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c  Truncate the fil
1f82a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1f82b 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71   jrnlTruncate(sq
1f82c 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1f82d 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73  , sqlite_int64 s
1f82e 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ize){.  int rc =
1f82f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f   SQLITE_OK;.  Jo
1f830 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1f831 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1f832 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65  fd;.  if( p->pRe
1f833 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  al ){.    rc = s
1f834 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
1f835 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29  (p->pReal, size)
1f836 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 69  ;.  }else if( si
1f837 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20  ze<p->iSize ){. 
1f838 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69     p->iSize = si
1f839 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ze;.  }.  return
1f83a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79   rc;.}../*.** Sy
1f83b 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  nc the file..*/.
1f83c 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53  static int jrnlS
1f83d 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ync(sqlite3_file
1f83e 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67   *pJfd, int flag
1f83f 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  s){.  int rc;.  
1f840 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d  JournalFile *p =
1f841 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29   (JournalFile *)
1f842 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70  pJfd;.  if( p->p
1f843 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  Real ){.    rc =
1f844 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
1f845 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b  ->pReal, flags);
1f846 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
1f847 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1f848 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1f849 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68  ../*.** Query th
1f84a 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69  e size of the fi
1f84b 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a  le in bytes..*/.
1f84c 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46  static int jrnlF
1f84d 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  ileSize(sqlite3_
1f84e 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69  file *pJfd, sqli
1f84f 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29  te_int64 *pSize)
1f850 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
1f851 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61  ITE_OK;.  Journa
1f852 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72  lFile *p = (Jour
1f853 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a  nalFile *)pJfd;.
1f854 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29    if( p->pReal )
1f855 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1f856 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e  e3OsFileSize(p->
1f857 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20  pReal, pSize);. 
1f858 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69   }else{.    *pSi
1f859 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74  ze = (sqlite_int
1f85a 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20  64) p->iSize;.  
1f85b 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1f85c 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66  ../*.** Table of
1f85d 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75   methods for Jou
1f85e 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 74 65 33  rnalFile sqlite3
1f85f 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  _file object..*/
1f860 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73  .static struct s
1f861 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
1f862 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74  s JournalFileMet
1f863 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20  hods = {.  1,   
1f864 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
1f865 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43  rsion */.  jrnlC
1f866 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c  lose,     /* xCl
1f867 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61  ose */.  jrnlRea
1f868 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64  d,      /* xRead
1f869 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c   */.  jrnlWrite,
1f86a 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a       /* xWrite *
1f86b 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65  /.  jrnlTruncate
1f86c 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20  ,  /* xTruncate 
1f86d 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20  */.  jrnlSync,  
1f86e 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a      /* xSync */.
1f86f 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20    jrnlFileSize, 
1f870 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f   /* xFileSize */
1f871 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
1f872 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20    /* xLock */.  
1f873 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1f874 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30  * xUnlock */.  0
1f875 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
1f876 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
1f877 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ock */.  0,     
1f878 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65          /* xFile
1f879 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20  Control */.  0, 
1f87a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1f87b 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20  SectorSize */.  
1f87c 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0              /
1f87d 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74  * xDeviceCharact
1f87e 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a  eristics */.};..
1f87f 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f  /* .** Open a jo
1f880 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53  urnal file..*/.S
1f881 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1f882 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  t sqlite3Journal
1f883 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  Open(.  sqlite3_
1f884 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
1f885 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f     /* The VFS to
1f886 20 75 73 65 20 66 6f 72 20 61 63 74 75 61 6c 20   use for actual 
1f887 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f  file I/O */.  co
1f888 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
1f889 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
1f88a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
1f88b 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  file */.  sqlite
1f88c 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20  3_file *pJfd,   
1f88d 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63       /* Prealloc
1f88e 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65  ated, blank file
1f88f 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74   handle */.  int
1f890 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
1f891 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69          /* Openi
1f892 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e  ng flags */.  in
1f893 74 20 6e 42 75 66 20 20 20 20 20 20 20 20 20 20  t nBuf          
1f894 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
1f895 73 20 62 75 66 66 65 72 65 64 20 62 65 66 6f 72  s buffered befor
1f896 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69  e opening the fi
1f897 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e  le */.){.  Journ
1f898 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1f899 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1f89a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
1f89b 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69  sqlite3JournalSi
1f89c 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69 66 28  ze(pVfs));.  if(
1f89d 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20 20 70   nBuf>0 ){.    p
1f89e 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33  ->zBuf = sqlite3
1f89f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29  MallocZero(nBuf)
1f8a0 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 7a 42  ;.    if( !p->zB
1f8a1 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  uf ){.      retu
1f8a2 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1f8a3 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
1f8a4 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
1f8a5 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a  e3OsOpen(pVfs, z
1f8a6 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67  Name, pJfd, flag
1f8a7 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e  s, 0);.  }.  p->
1f8a8 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e  pMethod = &Journ
1f8a9 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20  alFileMethods;. 
1f8aa 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b   p->nBuf = nBuf;
1f8ab 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c  .  p->flags = fl
1f8ac 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e  ags;.  p->zJourn
1f8ad 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d  al = zName;.  p-
1f8ae 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20  >pVfs = pVfs;.  
1f8af 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1f8b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
1f8b1 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 6f 69  e argument p poi
1f8b2 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c  nts to a Journal
1f8b3 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20  File structure, 
1f8b4 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69  and the underlyi
1f8b5 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e  ng.** file has n
1f8b6 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65 61  ot yet been crea
1f8b7 74 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 6e  ted, create it n
1f8b8 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ow..*/.SQLITE_PR
1f8b9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1f8ba 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73  3JournalCreate(s
1f8bb 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b  qlite3_file *p){
1f8bc 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f  .  if( p->pMetho
1f8bd 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65  ds!=&JournalFile
1f8be 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72  Methods ){.    r
1f8bf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1f8c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 72  .  }.  return cr
1f8c1 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61  eateFile((Journa
1f8c2 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f  lFile *)p);.}../
1f8c3 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  * .** Return the
1f8c4 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1f8c5 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
1f8c6 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  re a JournalFile
1f8c7 20 74 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a   that uses vfs.*
1f8c8 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 65  * pVfs to create
1f8c9 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
1f8ca 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a  on-disk files..*
1f8cb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f8cc 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72   int sqlite3Jour
1f8cd 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  nalSize(sqlite3_
1f8ce 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 72 65  vfs *pVfs){.  re
1f8cf 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73  turn (pVfs->szOs
1f8d0 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72  File+sizeof(Jour
1f8d1 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e  nalFile));.}.#en
1f8d2 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
1f8d3 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72  **** End of jour
1f8d4 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nal.c **********
1f8d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d7 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1f8d8 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1f8d9 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a  memjournal.c ***
1f8da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8dc 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
1f8dd 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20  October 7.**.** 
1f8de 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
1f8df 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
1f8e0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
1f8e1 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
1f8e2 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
1f8e3 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
1f8e4 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
1f8e5 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1f8e6 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
1f8e7 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1f8e8 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1f8e9 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
1f8ea 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
1f8eb 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
1f8ec 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
1f8ed 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
1f8ee 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
1f8ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8f3 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
1f8f4 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
1f8f5 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d  s code use to im
1f8f6 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65  plement an in-me
1f8f7 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  mory rollback jo
1f8f8 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e  urnal..** The in
1f8f9 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b  -memory rollback
1f8fa 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 64   journal is used
1f8fb 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e   to journal tran
1f8fc 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20  sactions for.** 
1f8fd 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 74 61 62  ":memory:" datab
1f8fe 61 73 65 73 20 61 6e 64 20 77 68 65 6e 20 74 68  ases and when th
1f8ff 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d  e journal_mode=M
1f900 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 69 73 20  EMORY pragma is 
1f901 75 73 65 64 2e 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72  used..*/../* For
1f902 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20  ward references 
1f903 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75  to internal stru
1f904 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64 65  ctures */.typede
1f905 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72  f struct MemJour
1f906 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a  nal MemJournal;.
1f907 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
1f908 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69  ilePoint FilePoi
1f909 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  nt;.typedef stru
1f90a 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c  ct FileChunk Fil
1f90b 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63  eChunk;../* Spac
1f90c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f  e to hold the ro
1f90d 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69  llback journal i
1f90e 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69  s allocated in i
1f90f 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20  ncrements of.** 
1f910 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 2e  this many bytes.
1f911 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20  .**.** The size 
1f912 63 68 6f 73 65 6e 20 69 73 20 61 20 6c 69 74 74  chosen is a litt
1f913 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 61 20 70  le less than a p
1f914 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 54 68  ower of two.  Th
1f915 61 74 20 77 61 79 2c 0a 2a 2a 20 74 68 65 20 46  at way,.** the F
1f916 69 6c 65 43 68 75 6e 6b 20 6f 62 6a 65 63 74 20  ileChunk object 
1f917 77 69 6c 6c 20 68 61 76 65 20 61 20 73 69 7a 65  will have a size
1f918 20 74 68 61 74 20 61 6c 6d 6f 73 74 20 65 78 61   that almost exa
1f919 63 74 6c 79 20 66 69 6c 6c 73 0a 2a 2a 20 61 20  ctly fills.** a 
1f91a 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c  power-of-two all
1f91b 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 6d  ocation.  This m
1f91c 69 6d 69 6d 69 7a 65 73 20 77 61 73 74 65 64 20  imimizes wasted 
1f91d 73 70 61 63 65 20 69 6e 20 70 6f 77 65 72 2d 6f  space in power-o
1f91e 66 2d 74 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20  f-two.** memory 
1f91f 61 6c 6c 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23  allocators..*/.#
1f920 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43  define JOURNAL_C
1f921 48 55 4e 4b 53 49 5a 45 20 28 28 69 6e 74 29 28  HUNKSIZE ((int)(
1f922 31 30 32 34 2d 73 69 7a 65 6f 66 28 46 69 6c 65  1024-sizeof(File
1f923 43 68 75 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61  Chunk*)))../* Ma
1f924 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20  cro to find the 
1f925 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e  minimum of two n
1f926 75 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a  umeric values..*
1f927 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20  /.#ifndef MIN.# 
1f928 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20  define MIN(x,y) 
1f929 28 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29  ((x)<(y)?(x):(y)
1f92a 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
1f92b 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  The rollback jou
1f92c 72 6e 61 6c 20 69 73 20 63 6f 6d 70 6f 73 65 64  rnal is composed
1f92d 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
1f92e 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  t of these struc
1f92f 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  tures..*/.struct
1f930 20 46 69 6c 65 43 68 75 6e 6b 20 7b 0a 20 20 46   FileChunk {.  F
1f931 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 78 74 3b  ileChunk *pNext;
1f932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f933 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 69 6e 20  * Next chunk in 
1f934 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20  the journal */. 
1f935 20 75 38 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e   u8 zChunk[JOURN
1f936 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 5d 3b 20 20  AL_CHUNKSIZE];  
1f937 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74   /* Content of t
1f938 68 69 73 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a  his chunk */.};.
1f939 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
1f93a 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
1f93b 74 20 73 65 72 76 65 73 20 61 73 20 61 20 63 75  t serves as a cu
1f93c 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 72 6f  rsor into the ro
1f93d 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a  llback journal..
1f93e 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 61  ** The cursor ca
1f93f 6e 20 62 65 20 65 69 74 68 65 72 20 66 6f 72 20  n be either for 
1f940 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
1f941 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69  ng..*/.struct Fi
1f942 6c 65 50 6f 69 6e 74 20 7b 0a 20 20 73 71 6c 69  lePoint {.  sqli
1f943 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73 65  te3_int64 iOffse
1f944 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  t;          /* O
1f945 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 62  ffset from the b
1f946 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
1f947 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 43 68  file */.  FileCh
1f948 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20  unk *pChunk;    
1f949 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65            /* Spe
1f94a 63 69 66 69 63 20 63 68 75 6e 6b 20 69 6e 74 6f  cific chunk into
1f94b 20 77 68 69 63 68 20 63 75 72 73 6f 72 20 70 6f   which cursor po
1f94c 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ints */.};../*.*
1f94d 2a 20 54 68 69 73 20 73 75 62 63 6c 61 73 73 20  * This subclass 
1f94e 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66  is a subclass of
1f94f 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 20 20   sqlite3_file.  
1f950 45 61 63 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 79  Each open memory
1f951 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 61  -journal.** is a
1f952 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
1f953 69 73 20 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72  is class..*/.str
1f954 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b  uct MemJournal {
1f955 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  .  sqlite3_io_me
1f956 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20  thods *pMethod; 
1f957 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 63 6c 61     /* Parent cla
1f958 73 73 2e 20 4d 55 53 54 20 42 45 20 46 49 52 53  ss. MUST BE FIRS
1f959 54 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b  T */.  FileChunk
1f95a 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20 20   *pFirst;       
1f95b 20 20 20 20 20 20 20 2f 2a 20 48 65 61 64 20 6f         /* Head o
1f95c 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 68 75 6e  f in-memory chun
1f95d 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 46 69 6c 65  k-list */.  File
1f95e 50 6f 69 6e 74 20 65 6e 64 70 6f 69 6e 74 3b 20  Point endpoint; 
1f95f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1f960 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e  ointer to the en
1f961 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f  d of the file */
1f962 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 72 65 61  .  FilePoint rea
1f963 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20  dpoint;         
1f964 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1f965 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1f966 6c 61 73 74 20 78 52 65 61 64 28 29 20 2a 2f 0a  last xRead() */.
1f967 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64  };../*.** Read d
1f968 61 74 61 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d  ata from the in-
1f969 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66  memory journal f
1f96a 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 74 68  ile.  This is th
1f96b 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1f96c 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74  .** of the sqlit
1f96d 65 33 5f 76 66 73 2e 78 52 65 61 64 20 6d 65 74  e3_vfs.xRead met
1f96e 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
1f96f 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 28 0a  nt memjrnlRead(.
1f970 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1f971 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20  pJfd,    /* The 
1f972 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f  journal file fro
1f973 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 20  m which to read 
1f974 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c  */.  void *zBuf,
1f975 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1f976 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 20 68  ut the results h
1f977 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d  ere */.  int iAm
1f978 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
1f979 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1f97a 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20  es to read */.  
1f97b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66  sqlite_int64 iOf
1f97c 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20  st     /* Begin 
1f97d 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20  reading at this 
1f97e 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d  offset */.){.  M
1f97f 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28  emJournal *p = (
1f980 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66  MemJournal *)pJf
1f981 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20  d;.  u8 *zOut = 
1f982 7a 42 75 66 3b 0a 20 20 69 6e 74 20 6e 52 65 61  zBuf;.  int nRea
1f983 64 20 3d 20 69 41 6d 74 3b 0a 20 20 69 6e 74 20  d = iAmt;.  int 
1f984 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20  iChunkOffset;.  
1f985 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e  FileChunk *pChun
1f986 6b 3b 0a 0a 20 20 2f 2a 20 53 51 4c 69 74 65 20  k;..  /* SQLite 
1f987 6e 65 76 65 72 20 74 72 69 65 73 20 74 6f 20 72  never tries to r
1f988 65 61 64 20 70 61 73 74 20 74 68 65 20 65 6e 64  ead past the end
1f989 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a   of a rollback j
1f98a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20  ournal file */. 
1f98b 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 2b 69   assert( iOfst+i
1f98c 41 6d 74 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74  Amt<=p->endpoint
1f98d 2e 69 4f 66 66 73 65 74 20 29 3b 0a 0a 20 20 69  .iOffset );..  i
1f98e 66 28 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e  f( p->readpoint.
1f98f 69 4f 66 66 73 65 74 21 3d 69 4f 66 73 74 20 7c  iOffset!=iOfst |
1f990 7c 20 69 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 20  | iOfst==0 ){.  
1f991 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
1f992 69 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 66 6f  iOff = 0;.    fo
1f993 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72  r(pChunk=p->pFir
1f994 73 74 3b 20 0a 20 20 20 20 20 20 20 20 41 4c 57  st; .        ALW
1f995 41 59 53 28 70 43 68 75 6e 6b 29 20 26 26 20 28  AYS(pChunk) && (
1f996 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55  iOff+JOURNAL_CHU
1f997 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a  NKSIZE)<=iOfst;.
1f998 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d 70          pChunk=p
1f999 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 20  Chunk->pNext.   
1f99a 20 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20 2b   ){.      iOff +
1f99b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  = JOURNAL_CHUNKS
1f99c 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  IZE;.    }.  }el
1f99d 73 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d  se{.    pChunk =
1f99e 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43   p->readpoint.pC
1f99f 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 68  hunk;.  }..  iCh
1f9a0 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e 74  unkOffset = (int
1f9a1 29 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c 5f  )(iOfst%JOURNAL_
1f9a2 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 6f  CHUNKSIZE);.  do
1f9a3 20 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61 63   {.    int iSpac
1f9a4 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  e = JOURNAL_CHUN
1f9a5 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66  KSIZE - iChunkOf
1f9a6 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e 43  fset;.    int nC
1f9a7 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64 2c  opy = MIN(nRead,
1f9a8 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53   (JOURNAL_CHUNKS
1f9a9 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73  IZE - iChunkOffs
1f9aa 65 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  et));.    memcpy
1f9ab 28 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d 3e  (zOut, &pChunk->
1f9ac 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66  zChunk[iChunkOff
1f9ad 73 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20  set], nCopy);.  
1f9ae 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 3b    zOut += nCopy;
1f9af 0a 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69 53  .    nRead -= iS
1f9b0 70 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e 6b  pace;.    iChunk
1f9b1 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d 20  Offset = 0;.  } 
1f9b2 77 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20  while( nRead>=0 
1f9b3 26 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e  && (pChunk=pChun
1f9b4 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26 20  k->pNext)!=0 && 
1f9b5 6e 52 65 61 64 3e 30 20 29 3b 0a 20 20 70 2d 3e  nRead>0 );.  p->
1f9b6 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65  readpoint.iOffse
1f9b7 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b 0a  t = iOfst+iAmt;.
1f9b8 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70    p->readpoint.p
1f9b9 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a  Chunk = pChunk;.
1f9ba 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1f9bb 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  _OK;.}../*.** Wr
1f9bc 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 20  ite data to the 
1f9bd 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1f9be 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65  int memjrnlWrite
1f9bf 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
1f9c0 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68   *pJfd,    /* Th
1f9c1 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
1f9c2 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69  nto which to wri
1f9c3 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  te */.  const vo
1f9c4 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f  id *zBuf,      /
1f9c5 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20 62  * Take data to b
1f9c6 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 68  e written from h
1f9c7 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d  ere */.  int iAm
1f9c8 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
1f9c9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1f9ca 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20  es to write */. 
1f9cb 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f   sqlite_int64 iO
1f9cc 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e  fst     /* Begin
1f9cd 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 73   writing at this
1f9ce 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65   offset into the
1f9cf 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65   file */.){.  Me
1f9d0 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d  mJournal *p = (M
1f9d1 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64  emJournal *)pJfd
1f9d2 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 3d  ;.  int nWrite =
1f9d3 20 69 41 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 72   iAmt;.  u8 *zWr
1f9d4 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75 66  ite = (u8 *)zBuf
1f9d5 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65  ;..  /* An in-me
1f9d6 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  mory journal fil
1f9d7 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 76  e should only ev
1f9d8 65 72 20 62 65 20 61 70 70 65 6e 64 65 64 20 74  er be appended t
1f9d9 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61  o. Random.  ** a
1f9da 63 63 65 73 73 20 77 72 69 74 65 73 20 61 72 65  ccess writes are
1f9db 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 62 79   not required by
1f9dc 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20   sqlite..  */.  
1f9dd 61 73 73 65 72 74 28 20 69 4f 66 73 74 3d 3d 70  assert( iOfst==p
1f9de 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73  ->endpoint.iOffs
1f9df 65 74 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  et );.  UNUSED_P
1f9e0 41 52 41 4d 45 54 45 52 28 69 4f 66 73 74 29 3b  ARAMETER(iOfst);
1f9e1 0a 0a 20 20 77 68 69 6c 65 28 20 6e 57 72 69 74  ..  while( nWrit
1f9e2 65 3e 30 20 29 7b 0a 20 20 20 20 46 69 6c 65 43  e>0 ){.    FileC
1f9e3 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 70  hunk *pChunk = p
1f9e4 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e  ->endpoint.pChun
1f9e5 6b 3b 0a 20 20 20 20 69 6e 74 20 69 43 68 75 6e  k;.    int iChun
1f9e6 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28  kOffset = (int)(
1f9e7 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66  p->endpoint.iOff
1f9e8 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  set%JOURNAL_CHUN
1f9e9 4b 53 49 5a 45 29 3b 0a 20 20 20 20 69 6e 74 20  KSIZE);.    int 
1f9ea 69 53 70 61 63 65 20 3d 20 4d 49 4e 28 6e 57 72  iSpace = MIN(nWr
1f9eb 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55  ite, JOURNAL_CHU
1f9ec 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f  NKSIZE - iChunkO
1f9ed 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 69 66 28  ffset);..    if(
1f9ee 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30   iChunkOffset==0
1f9ef 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77   ){.      /* New
1f9f0 20 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69 72   chunk is requir
1f9f1 65 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68 65  ed to extend the
1f9f2 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20   file. */.      
1f9f3 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20  FileChunk *pNew 
1f9f4 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
1f9f5 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e  (sizeof(FileChun
1f9f6 6b 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  k));.      if( !
1f9f7 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
1f9f8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
1f9f9 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ERR_NOMEM;.     
1f9fa 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70   }.      pNew->p
1f9fb 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Next = 0;.      
1f9fc 69 66 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20  if( pChunk ){.  
1f9fd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
1f9fe 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20  >pFirst );.     
1f9ff 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74     pChunk->pNext
1fa00 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d   = pNew;.      }
1fa01 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
1fa02 73 65 72 74 28 20 21 70 2d 3e 70 46 69 72 73 74  sert( !p->pFirst
1fa03 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70   );.        p->p
1fa04 46 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20  First = pNew;.  
1fa05 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 65      }.      p->e
1fa06 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d  ndpoint.pChunk =
1fa07 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 20   pNew;.    }..  
1fa08 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64    memcpy(&p->end
1fa09 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43  point.pChunk->zC
1fa0a 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65  hunk[iChunkOffse
1fa0b 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70 61  t], zWrite, iSpa
1fa0c 63 65 29 3b 0a 20 20 20 20 7a 57 72 69 74 65 20  ce);.    zWrite 
1fa0d 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 6e  += iSpace;.    n
1fa0e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65 3b  Write -= iSpace;
1fa0f 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74  .    p->endpoint
1fa10 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 53 70 61  .iOffset += iSpa
1fa11 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  ce;.  }..  retur
1fa12 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1fa13 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74  /*.** Truncate t
1fa14 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
1fa15 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72  ic int memjrnlTr
1fa16 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66  uncate(sqlite3_f
1fa17 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74  ile *pJfd, sqlit
1fa18 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20  e_int64 size){. 
1fa19 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d   MemJournal *p =
1fa1a 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70   (MemJournal *)p
1fa1b 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b  Jfd;.  FileChunk
1fa1c 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 73 73 65   *pChunk;.  asse
1fa1d 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 55  rt(size==0);.  U
1fa1e 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1fa1f 73 69 7a 65 29 3b 0a 20 20 70 43 68 75 6e 6b 20  size);.  pChunk 
1fa20 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0a 20 20 77  = p->pFirst;.  w
1fa21 68 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b 0a  hile( pChunk ){.
1fa22 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70      FileChunk *p
1fa23 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 20  Tmp = pChunk;.  
1fa24 20 20 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e    pChunk = pChun
1fa25 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71  k->pNext;.    sq
1fa26 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70 29  lite3_free(pTmp)
1fa27 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d  ;.  }.  sqlite3M
1fa28 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a  emJournalOpen(pJ
1fa29 66 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  fd);.  return SQ
1fa2a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1fa2b 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  * Close the file
1fa2c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1fa2d 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c  memjrnlClose(sql
1fa2e 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29  ite3_file *pJfd)
1fa2f 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63  {.  memjrnlTrunc
1fa30 61 74 65 28 70 4a 66 64 2c 20 30 29 3b 0a 20 20  ate(pJfd, 0);.  
1fa31 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1fa32 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63  ;.}.../*.** Sync
1fa33 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
1fa34 20 53 79 6e 63 69 6e 67 20 61 6e 20 69 6e 2d 6d   Syncing an in-m
1fa35 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 69 73  emory journal is
1fa36 20 61 20 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20   a no-op.  And, 
1fa37 69 6e 20 66 61 63 74 2c 20 74 68 69 73 20 72 6f  in fact, this ro
1fa38 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6e 65 76 65  utine.** is neve
1fa39 72 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 77 6f  r called in a wo
1fa3a 72 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61  rking implementa
1fa3b 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c  tion.  This impl
1fa3c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65 78  ementation.** ex
1fa3d 69 73 74 73 20 70 75 72 65 6c 79 20 61 73 20 61  ists purely as a
1fa3e 20 63 6f 6e 74 69 6e 67 65 6e 63 79 2c 20 69 6e   contingency, in
1fa3f 20 63 61 73 65 20 73 6f 6d 65 20 6d 61 6c 66 75   case some malfu
1fa40 6e 63 74 69 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f  nction in some o
1fa41 74 68 65 72 0a 2a 2a 20 70 61 72 74 20 6f 66 20  ther.** part of 
1fa42 53 51 4c 69 74 65 20 63 61 75 73 65 73 20 53 79  SQLite causes Sy
1fa43 6e 63 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20  nc to be called 
1fa44 62 79 20 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73  by mistake..*/.s
1fa45 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e  tatic int memjrn
1fa46 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69  lSync(sqlite3_fi
1fa47 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74  le *NotUsed, int
1fa48 20 4e 6f 74 55 73 65 64 32 29 7b 20 20 20 2f 2a   NotUsed2){   /*
1fa49 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 55 4e 55 53  NO_TEST*/.  UNUS
1fa4a 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
1fa4b 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
1fa4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1fa4d 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45           /*NO_TE
1fa4e 53 54 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30  ST*/.  assert( 0
1fa4f 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   );             
1fa50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa52 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a      /*NO_TEST*/.
1fa53 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1fa54 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
1fa55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fa57 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 7d 20 20 20 20  *NO_TEST*/.}    
1fa58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa5b 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54            /*NO_T
1fa5c 45 53 54 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65  EST*/../*.** Que
1fa5d 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ry the size of t
1fa5e 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73  he file in bytes
1fa5f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1fa60 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28  memjrnlFileSize(
1fa61 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1fa62 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  fd, sqlite_int64
1fa63 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a   *pSize){.  MemJ
1fa64 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d  ournal *p = (Mem
1fa65 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a  Journal *)pJfd;.
1fa66 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c 69    *pSize = (sqli
1fa67 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64  te_int64) p->end
1fa68 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20  point.iOffset;. 
1fa69 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1fa6a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c  K;.}../*.** Tabl
1fa6b 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72  e of methods for
1fa6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69   MemJournal sqli
1fa6d 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e  te3_file object.
1fa6e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
1fa6f 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
1fa70 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d  hods MemJournalM
1fa71 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20  ethods = {.  1, 
1fa72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fa73 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20  * iVersion */.  
1fa74 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20  memjrnlClose,   
1fa75 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20    /* xClose */. 
1fa76 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 20   memjrnlRead,   
1fa77 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20     /* xRead */. 
1fa78 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 20   memjrnlWrite,  
1fa79 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a     /* xWrite */.
1fa7a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74    memjrnlTruncat
1fa7b 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65  e,  /* xTruncate
1fa7c 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e   */.  memjrnlSyn
1fa7d 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63  c,      /* xSync
1fa7e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c   */.  memjrnlFil
1fa7f 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65  eSize,  /* xFile
1fa80 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Size */.  0,    
1fa81 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1fa82 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Lock */.  0,    
1fa83 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1fa84 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20  Unlock */.  0,  
1fa85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1fa86 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
1fa87 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ock */.  0,     
1fa88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
1fa89 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20  ileControl */.  
1fa8a 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1fa8b 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65    /* xSectorSize
1fa8c 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20   */.  0         
1fa8d 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69          /* xDevi
1fa8e 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
1fa8f 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20  s */.};../* .** 
1fa90 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66  Open a journal f
1fa91 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile..*/.SQLITE_P
1fa92 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1fa93 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65  te3MemJournalOpe
1fa94 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  n(sqlite3_file *
1fa95 70 4a 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72  pJfd){.  MemJour
1fa96 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75  nal *p = (MemJou
1fa97 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 61  rnal *)pJfd;.  a
1fa98 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54  ssert( EIGHT_BYT
1fa99 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29  E_ALIGNMENT(p) )
1fa9a 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ;.  memset(p, 0,
1fa9b 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e   sqlite3MemJourn
1fa9c 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 2d 3e  alSize());.  p->
1fa9d 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f  pMethod = &MemJo
1fa9e 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a  urnalMethods;.}.
1fa9f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
1faa0 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 2d 68  ue if the file-h
1faa1 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73 20  andle passed as 
1faa2 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 0a  an argument is .
1faa3 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  ** an in-memory 
1faa4 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49  journal .*/.SQLI
1faa5 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1faa6 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e  qlite3IsMemJourn
1faa7 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  al(sqlite3_file 
1faa8 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 72 6e  *pJfd){.  return
1faa9 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d   pJfd->pMethods=
1faaa 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68  =&MemJournalMeth
1faab 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  ods;.}../* .** R
1faac 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1faad 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72   of bytes requir
1faae 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4d 65  ed to store a Me
1faaf 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 75 73  mJournal that us
1fab0 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 74  es vfs.** pVfs t
1fab1 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e 64  o create the und
1fab2 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20  erlying on-disk 
1fab3 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  files..*/.SQLITE
1fab4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1fab5 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69  ite3MemJournalSi
1fab6 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75  ze(void){.  retu
1fab7 72 6e 20 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75  rn sizeof(MemJou
1fab8 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  rnal);.}../*****
1fab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1faba 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a   memjournal.c **
1fabb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fabc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fabd 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1fabe 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1fabf 66 69 6c 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a  file walker.c **
1fac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fac2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1fac3 32 30 30 38 20 41 75 67 75 73 74 20 31 36 0a 2a  2008 August 16.*
1fac4 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1fac5 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1fac6 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1fac7 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1fac8 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
1fac9 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1faca 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1facb 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1facc 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1facd 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1face 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1facf 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1fad0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1fad1 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1fad2 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1fad3 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1fad4 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1fad5 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1fad6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fad7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fad8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fad9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1fada 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
1fadb 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65  ins routines use
1fadc 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68  d for walking th
1fadd 65 20 70 61 72 73 65 72 20 74 72 65 65 20 66 6f  e parser tree fo
1fade 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 73 74 61 74  r.** an SQL stat
1fadf 65 6d 65 6e 74 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a  ement..*/.../*.*
1fae0 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73  * Walk an expres
1fae1 73 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f  sion tree.  Invo
1fae2 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ke the callback 
1fae3 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e 6f  once for each no
1fae4 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 70  de.** of the exp
1fae5 72 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64  ression, while d
1fae6 65 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f  ecending.  (In o
1fae7 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
1fae8 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69  callback.** is i
1fae9 6e 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 69  nvoked before vi
1faea 73 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e  siting children.
1faeb 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ).**.** The retu
1faec 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68  rn value from th
1faed 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c  e callback shoul
1faee 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20  d be one of the 
1faef 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e  WRC_*.** constan
1faf0 74 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 6f  ts to specify ho
1faf1 77 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 74  w to proceed wit
1faf2 68 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a  h the walk..**.*
1faf3 2a 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75  *    WRC_Continu
1faf4 65 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 20  e      Continue 
1faf5 64 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20  descending down 
1faf6 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  the tree..**.** 
1faf7 20 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20     WRC_Prune    
1faf8 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 63       Do not desc
1faf9 65 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e  end into child n
1fafa 6f 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77  odes.  But allow
1fafb 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1fafc 20 20 20 20 20 20 20 20 20 74 68 65 20 77 61 6c           the wal
1fafd 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69  k to continue wi
1fafe 74 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73  th sibling nodes
1faff 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41  ..**.**    WRC_A
1fb00 62 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20  bort         Do 
1fb01 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b  no more callback
1fb02 73 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73  s.  Unwind the s
1fb03 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20  tack and.**     
1fb04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fb05 20 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 2d   return the top-
1fb06 6c 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e  level walk call.
1fb07 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
1fb08 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69  n value from thi
1fb09 73 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 43  s routine is WRC
1fb0a 5f 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f  _Abort to abando
1fb0b 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 0a  n the tree walk.
1fb0c 2a 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69  ** and WRC_Conti
1fb0d 6e 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e  nue to continue.
1fb0e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1fb0f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
1fb10 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70  lkExpr(Walker *p
1fb11 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45  Walker, Expr *pE
1fb12 78 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  xpr){.  int rc;.
1fb13 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
1fb14 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
1fb15 69 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 65  inue;.  testcase
1fb16 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
1fb17 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65  y(pExpr, EP_Toke
1fb18 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 74  nOnly) );.  test
1fb19 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f  case( ExprHasPro
1fb1a 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1fb1b 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 72 63  Reduced) );.  rc
1fb1c 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70   = pWalker->xExp
1fb1d 72 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65  rCallback(pWalke
1fb1e 72 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28  r, pExpr);.  if(
1fb1f 20 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75   rc==WRC_Continu
1fb20 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e.              
1fb21 26 26 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  && !ExprHasAnyPr
1fb22 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f  operty(pExpr,EP_
1fb23 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  TokenOnly) ){.  
1fb24 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1fb25 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70  kExpr(pWalker, p
1fb26 45 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72  Expr->pLeft) ) r
1fb27 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1fb28 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1fb29 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72  WalkExpr(pWalker
1fb2a 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
1fb2b 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fb2c 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 70  ort;.    if( Exp
1fb2d 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
1fb2e 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
1fb2f 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ) ){.      if( s
1fb30 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1fb31 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d  (pWalker, pExpr-
1fb32 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65  >x.pSelect) ) re
1fb33 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1fb34 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1fb35 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1fb36 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72  ExprList(pWalker
1fb37 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  , pExpr->x.pList
1fb38 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1fb39 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  bort;.    }.  }.
1fb3a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52    return rc & WR
1fb3b 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  C_Abort;.}../*.*
1fb3c 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61  * Call sqlite3Wa
1fb3d 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65  lkExpr() for eve
1fb3e 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ry expression in
1fb3f 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 6c   list p or until
1fb40 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 71  .** an abort req
1fb41 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a 2f  uest is seen..*/
1fb42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1fb43 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  int sqlite3WalkE
1fb44 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a  xprList(Walker *
1fb45 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73  pWalker, ExprLis
1fb46 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t *p){.  int i;.
1fb47 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1fb48 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
1fb49 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 66 6f   if( p ){.    fo
1fb4a 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49  r(i=p->nExpr, pI
1fb4b 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69  tem=p->a; i>0; i
1fb4c 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
1fb4d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
1fb4e 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  alkExpr(pWalker,
1fb4f 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29   pItem->pExpr) )
1fb50 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1fb51 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  t;.    }.  }.  r
1fb52 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
1fb53 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c  ue;.}../*.** Wal
1fb54 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e  k all expression
1fb55 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
1fb56 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  h SELECT stateme
1fb57 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74  nt p.  Do.** not
1fb58 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 4c 45   invoke the SELE
1fb59 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70  CT callback on p
1fb5a 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 6f 75  , but do (of cou
1fb5b 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a 20 61  rse) invoke.** a
1fb5c 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 63 6b  ny expr callback
1fb5d 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63 61 6c  s and SELECT cal
1fb5e 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f 6d 65  lbacks that come
1fb5f 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69 65 73   from subqueries
1fb60 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f  ..** Return WRC_
1fb61 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e  Abort or WRC_Con
1fb62 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tinue..*/.SQLITE
1fb63 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1fb64 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78  ite3WalkSelectEx
1fb65 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b  pr(Walker *pWalk
1fb66 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a  er, Select *p){.
1fb67 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1fb68 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65  kExprList(pWalke
1fb69 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29 20  r, p->pEList) ) 
1fb6a 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1fb6b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ;.  if( sqlite3W
1fb6c 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  alkExpr(pWalker,
1fb6d 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72 65   p->pWhere) ) re
1fb6e 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1fb6f 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1fb70 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65  kExprList(pWalke
1fb71 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 20  r, p->pGroupBy) 
1fb72 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fb73 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1fb74 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1fb75 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20 29  r, p->pHaving) )
1fb76 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1fb77 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  t;.  if( sqlite3
1fb78 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61  WalkExprList(pWa
1fb79 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 72 42  lker, p->pOrderB
1fb7a 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  y) ) return WRC_
1fb7b 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
1fb7c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1fb7d 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  lker, p->pLimit)
1fb7e 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fb7f 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ort;.  if( sqlit
1fb80 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b  e3WalkExpr(pWalk
1fb81 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20  er, p->pOffset) 
1fb82 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fb83 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43  rt;.  return WRC
1fb84 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a  _Continue;.}../*
1fb85 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 61 72  .** Walk the par
1fb86 73 65 20 74 72 65 65 73 20 61 73 73 6f 63 69 61  se trees associa
1fb87 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73 75 62  ted with all sub
1fb88 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 0a 2a  queries in the.*
1fb89 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  * FROM clause of
1fb8a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1fb8b 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 6e 76  t p.  Do not inv
1fb8c 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74 0a 2a  oke the select.*
1fb8d 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c  * callback on p,
1fb8e 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 20 69   but do invoke i
1fb8f 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d 20 63  t on each FROM c
1fb90 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 0a 2a  lause subquery.*
1fb91 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 75 62  * and on any sub
1fb92 71 75 65 72 69 65 73 20 66 75 72 74 68 65 72 20  queries further 
1fb93 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 65 65  down in the tree
1fb94 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 57 52  .  Return .** WR
1fb95 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43  C_Abort or WRC_C
1fb96 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 4c 49  ontinue;.*/.SQLI
1fb97 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1fb98 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1fb99 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57 61  From(Walker *pWa
1fb9a 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
1fb9b 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  {.  SrcList *pSr
1fb9c 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  c;.  int i;.  st
1fb9d 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
1fb9e 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 53 72  m *pItem;..  pSr
1fb9f 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69  c = p->pSrc;.  i
1fba0 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 20  f( ALWAYS(pSrc) 
1fba1 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72  ){.    for(i=pSr
1fba2 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70  c->nSrc, pItem=p
1fba3 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d  Src->a; i>0; i--
1fba4 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
1fba5 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1fba6 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c  kSelect(pWalker,
1fba7 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29   pItem->pSelect)
1fba8 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
1fba9 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1fbaa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1fbab 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e    return WRC_Con
1fbac 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a 2a  tinue;.} ../*.**
1fbad 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c   Call sqlite3Wal
1fbae 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72  kExpr() for ever
1fbaf 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  y expression in 
1fbb0 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  Select statement
1fbb1 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71   p..** Invoke sq
1fbb2 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
1fbb3 29 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73  ) for subqueries
1fbb4 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
1fbb5 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 68  use and.** on th
1fbb6 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  e compound selec
1fbb7 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 69  t chain, p->pPri
1fbb8 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  or..**.** Return
1fbb9 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 6e   WRC_Continue un
1fbba 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 69  der normal condi
1fbbb 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 57  tions.  Return W
1fbbc 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 74  RC_Abort if.** t
1fbbd 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 74  here is an abort
1fbbe 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a 20   request..**.** 
1fbbf 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 6f  If the Walker do
1fbc0 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 78  es not have an x
1fbc1 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 29  SelectCallback()
1fbc2 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
1fbc3 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70  ne.** is a no-op
1fbc4 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f 43   returning WRC_C
1fbc5 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49  ontinue..*/.SQLI
1fbc6 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1fbc7 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1fbc8 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
1fbc9 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
1fbca 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d  int rc;.  if( p=
1fbcb 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78  =0 || pWalker->x
1fbcc 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d  SelectCallback==
1fbcd 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43  0 ) return WRC_C
1fbce 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20  ontinue;.  rc = 
1fbcf 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  WRC_Continue;.  
1fbd0 77 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 20  while( p  ){.   
1fbd1 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78   rc = pWalker->x
1fbd2 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70  SelectCallback(p
1fbd3 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 20  Walker, p);.    
1fbd4 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a  if( rc ) break;.
1fbd5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
1fbd6 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 57  alkSelectExpr(pW
1fbd7 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75  alker, p) ) retu
1fbd8 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1fbd9 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1fbda 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 6c  kSelectFrom(pWal
1fbdb 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 6e  ker, p) ) return
1fbdc 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
1fbdd 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  p = p->pPrior;. 
1fbde 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26   }.  return rc &
1fbdf 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f   WRC_Abort;.}../
1fbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
1fbe1 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a  nd of walker.c *
1fbe2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbe3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1fbe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1fbe6 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76  egin file resolv
1fbe7 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1fbe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbe9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1fbea 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74  *.** 2008 August
1fbeb 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   18.**.** The au
1fbec 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1fbed 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1fbee 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1fbef 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1fbf0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1fbf1 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1fbf2 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1fbf3 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1fbf4 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
1fbf5 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
1fbf6 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
1fbf7 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
1fbf8 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
1fbf9 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
1fbfa 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
1fbfb 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1fbfc 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1fbfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc01 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
1fc02 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74  le contains rout
1fc03 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61  ines used for wa
1fc04 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72  lking the parser
1fc05 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73   tree and.** res
1fc06 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66  olve all identif
1fc07 69 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74  iers by associat
1fc08 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20  ing them with a 
1fc09 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61  particular.** ta
1fc0a 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a  ble and column..
1fc0b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74  */../*.** Turn t
1fc0c 68 65 20 70 45 78 70 72 20 65 78 70 72 65 73 73  he pExpr express
1fc0d 69 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61  ion into an alia
1fc0e 73 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74  s for the iCol-t
1fc0f 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a  h column of the.
1fc10 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e  ** result set in
1fc11 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49   pEList..**.** I
1fc12 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
1fc13 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d   column is a sim
1fc14 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  ple column refer
1fc15 65 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20  ence, then this 
1fc16 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73  routine.** makes
1fc17 20 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20   an exact copy. 
1fc18 20 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68   But for any oth
1fc19 65 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65  er kind of expre
1fc1a 73 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72  ssion, this.** r
1fc1b 6f 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f  outine make a co
1fc1c 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  py of the result
1fc1d 20 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74   set column as t
1fc1e 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  he argument to t
1fc1f 68 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72  he.** TK_AS oper
1fc20 61 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53  ator.  The TK_AS
1fc21 20 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73   operator causes
1fc22 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1fc23 74 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74  to be.** evaluat
1fc24 65 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64  ed just once and
1fc25 20 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72   then reused for
1fc26 20 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a   each alias..**.
1fc27 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f  ** The reason fo
1fc28 72 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68  r suppressing th
1fc29 65 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65  e TK_AS term whe
1fc2a 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
1fc2b 20 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20   is a simple.** 
1fc2c 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65  column reference
1fc2d 20 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 20   is so that the 
1fc2e 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65  column reference
1fc2f 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69   will be recogni
1fc30 7a 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65  zed as.** usable
1fc31 20 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 68   by indices with
1fc32 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
1fc33 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c  use processing l
1fc34 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63  ogic. .**.** Hac
1fc35 6b 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70  k:  The TK_AS op
1fc36 65 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69  erator is inhibi
1fc37 74 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d  ted if zType[0]=
1fc38 3d 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e  ='G'.  This mean
1fc39 73 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47  s.** that in a G
1fc3a 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20  ROUP BY clause, 
1fc3b 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
1fc3c 73 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 63  s evaluated twic
1fc3d 65 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a  e.  Hence:.**.**
1fc3e 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64       SELECT rand
1fc3f 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75  om()%5 AS x, cou
1fc40 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47  nt(*) FROM tab G
1fc41 52 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20  ROUP BY x.**.** 
1fc42 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  Is equivalent to
1fc43 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
1fc44 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53  CT random()%5 AS
1fc45 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f   x, count(*) FRO
1fc46 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 72  M tab GROUP BY r
1fc47 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20  andom()%5.**.** 
1fc48 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61  The result of ra
1fc49 6e 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20  ndom()%5 in the 
1fc4a 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1fc4b 69 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66  is probably diff
1fc4c 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68  erent.** from th
1fc4d 65 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20  e result in the 
1fc4e 72 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20  result-set.  We 
1fc4f 6d 69 67 68 74 20 66 69 78 20 74 68 69 73 20 73  might fix this s
1fc50 6f 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74  omeday.  Or.** t
1fc51 68 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69  hen again, we mi
1fc52 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74  ght not....*/.st
1fc53 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76  atic void resolv
1fc54 65 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20  eAlias(.  Parse 
1fc55 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
1fc56 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
1fc57 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
1fc58 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20  t *pEList,      
1fc59 2f 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20  /* A result set 
1fc5a 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20  */.  int iCol,  
1fc5b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
1fc5c 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72   column in the r
1fc5d 65 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70  esult set.  0..p
1fc5e 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a  EList->nExpr-1 *
1fc5f 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
1fc60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1fc61 61 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74  ansform this int
1fc62 6f 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68  o an alias to th
1fc63 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  e result set */.
1fc64 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
1fc65 79 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f  ype      /* "GRO
1fc66 55 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f  UP" or "ORDER" o
1fc67 72 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  r "" */.){.  Exp
1fc68 72 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20  r *pOrig;       
1fc69 20 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d      /* The iCol-
1fc6a 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
1fc6b 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
1fc6c 20 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20   Expr *pDup;    
1fc6d 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
1fc6e 6f 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71  of pOrig */.  sq
1fc6f 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
1fc70 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1fc71 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1fc72 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43  */..  assert( iC
1fc73 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45  ol>=0 && iCol<pE
1fc74 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20  List->nExpr );. 
1fc75 20 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d   pOrig = pEList-
1fc76 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a  >a[iCol].pExpr;.
1fc77 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 21    assert( pOrig!
1fc78 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1fc79 70 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45  pOrig->flags & E
1fc7a 50 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20  P_Resolved );.  
1fc7b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1fc7c 0a 20 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70  .  if( pOrig->op
1fc7d 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a  !=TK_COLUMN && z
1fc7e 54 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a  Type[0]!='G' ){.
1fc7f 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74      pDup = sqlit
1fc80 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f  e3ExprDup(db, pO
1fc81 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 75  rig, 0);.    pDu
1fc82 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  p = sqlite3PExpr
1fc83 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20  (pParse, TK_AS, 
1fc84 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20  pDup, 0, 0);.   
1fc85 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72   if( pDup==0 ) r
1fc86 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70  eturn;.    if( p
1fc87 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69  EList->a[iCol].i
1fc88 41 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20  Alias==0 ){.    
1fc89 20 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c    pEList->a[iCol
1fc8a 5d 2e 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29  ].iAlias = (u16)
1fc8b 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61  (++pParse->nAlia
1fc8c 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44  s);.    }.    pD
1fc8d 75 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c  up->iTable = pEL
1fc8e 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c  ist->a[iCol].iAl
1fc8f 69 61 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ias;.  }else if(
1fc90 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1fc91 28 70 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 61  (pOrig, EP_IntVa
1fc92 6c 75 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e 75  lue) || pOrig->u
1fc93 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 20  .zToken==0 ){.  
1fc94 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
1fc95 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69  ExprDup(db, pOri
1fc96 67 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  g, 0);.    if( p
1fc97 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Dup==0 ) return;
1fc98 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68  .  }else{.    ch
1fc99 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f 72  ar *zToken = pOr
1fc9a 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20  ig->u.zToken;.  
1fc9b 20 20 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 6e    assert( zToken
1fc9c 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 67  !=0 );.    pOrig
1fc9d 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b 0a  ->u.zToken = 0;.
1fc9e 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74      pDup = sqlit
1fc9f 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f  e3ExprDup(db, pO
1fca0 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f 72  rig, 0);.    pOr
1fca1 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a  ig->u.zToken = z
1fca2 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70  Token;.    if( p
1fca3 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Dup==0 ) return;
1fca4 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44  .    assert( (pD
1fca5 75 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f  up->flags & (EP_
1fca6 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
1fca7 4f 6e 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 20  Only))==0 );.   
1fca8 20 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d   pDup->flags2 |=
1fca9 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b   EP2_MallocedTok
1fcaa 65 6e 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 2e  en;.    pDup->u.
1fcab 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  zToken = sqlite3
1fcac 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54 6f  DbStrDup(db, zTo
1fcad 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ken);.  }.  if( 
1fcae 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45  pExpr->flags & E
1fcaf 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a  P_ExpCollate ){.
1fcb0 20 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20      pDup->pColl 
1fcb1 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a  = pExpr->pColl;.
1fcb2 20 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 20      pDup->flags 
1fcb3 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  |= EP_ExpCollate
1fcb4 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
1fcb5 78 70 72 43 6c 65 61 72 28 64 62 2c 20 70 45 78  xprClear(db, pEx
1fcb6 70 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45  pr);.  memcpy(pE
1fcb7 78 70 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f  xpr, pDup, sizeo
1fcb8 66 28 2a 70 45 78 70 72 29 29 3b 0a 20 20 73 71  f(*pExpr));.  sq
1fcb9 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1fcba 70 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pDup);.}../*.** 
1fcbb 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  Given the name o
1fcbc 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  f a column of th
1fcbd 65 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20  e form X.Y.Z or 
1fcbe 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c  Y.Z or just Z, l
1fcbf 6f 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e  ook up.** that n
1fcc0 61 6d 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f  ame in the set o
1fcc1 66 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20  f source tables 
1fcc2 69 6e 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20  in pSrcList and 
1fcc3 6d 61 6b 65 20 74 68 65 20 70 45 78 70 72 20 0a  make the pExpr .
1fcc4 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  ** expression no
1fcc5 64 65 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f  de refer back to
1fcc6 20 74 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c   that source col
1fcc7 75 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  umn.  The follow
1fcc8 69 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61  ing changes.** a
1fcc9 72 65 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72  re made to pExpr
1fcca 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72  :.**.**    pExpr
1fccb 2d 3e 69 44 62 20 20 20 20 20 20 20 20 20 20 20  ->iDb           
1fccc 53 65 74 20 74 68 65 20 69 6e 64 65 78 20 69 6e  Set the index in
1fccd 20 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68   db->aDb[] of th
1fcce 65 20 64 61 74 61 62 61 73 65 20 58 0a 2a 2a 20  e database X.** 
1fccf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcd0 20 20 20 20 20 20 20 20 28 65 76 65 6e 20 69 66          (even if
1fcd1 20 58 20 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a   X is implied)..
1fcd2 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61  **    pExpr->iTa
1fcd3 62 6c 65 20 20 20 20 20 20 20 20 53 65 74 20 74  ble        Set t
1fcd4 6f 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  o the cursor num
1fcd5 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ber for the tabl
1fcd6 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20  e obtained.**   
1fcd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcd8 20 20 20 20 20 20 66 72 6f 6d 20 70 53 72 63 4c        from pSrcL
1fcd9 69 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72  ist..**    pExpr
1fcda 2d 3e 70 54 61 62 20 20 20 20 20 20 20 20 20 20  ->pTab          
1fcdb 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61  Points to the Ta
1fcdc 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 6f 66  ble structure of
1fcdd 20 58 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a   X.Y (even if.**
1fcde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcdf 20 20 20 20 20 20 20 20 20 58 20 61 6e 64 2f 6f           X and/o
1fce0 72 20 59 20 61 72 65 20 69 6d 70 6c 69 65 64 2e  r Y are implied.
1fce1 29 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69  ).**    pExpr->i
1fce2 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74  Column       Set
1fce3 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e   to the column n
1fce4 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68 65  umber within the
1fce5 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45   table..**    pE
1fce6 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20  xpr->op         
1fce7 20 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c     Set to TK_COL
1fce8 55 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72  UMN..**    pExpr
1fce9 2d 3e 70 4c 65 66 74 20 20 20 20 20 20 20 20 20  ->pLeft         
1fcea 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74  Any expression t
1fceb 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73  his points to is
1fcec 20 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70   deleted.**    p
1fced 45 78 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20  Expr->pRight    
1fcee 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69      Any expressi
1fcef 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74  on this points t
1fcf0 6f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a  o is deleted..**
1fcf1 0a 2a 2a 20 54 68 65 20 7a 44 62 20 76 61 72 69  .** The zDb vari
1fcf2 61 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65  able is the name
1fcf3 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1fcf4 20 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 69   (the "X").  Thi
1fcf5 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a  s value may be.*
1fcf6 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74  * NULL meaning t
1fcf7 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74  hat name is of t
1fcf8 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a  he form Y.Z or Z
1fcf9 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65  .  Any available
1fcfa 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e   database.** can
1fcfb 20 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a   be used.  The z
1fcfc 54 61 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69  Table variable i
1fcfd 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1fcfe 65 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22  e table (the "Y"
1fcff 29 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75  ).  This.** valu
1fd00 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66  e can be NULL if
1fd01 20 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c   zDb is also NUL
1fd02 4c 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73  L.  If zTable is
1fd03 20 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e   NULL it.** mean
1fd04 73 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20  s that the form 
1fd05 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a  of the name is Z
1fd06 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e   and that column
1fd07 73 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65  s from any table
1fd08 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e  .** can be used.
1fd09 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61  .**.** If the na
1fd0a 6d 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73  me cannot be res
1fd0b 6f 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75  olved unambiguou
1fd0c 73 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72  sly, leave an er
1fd0d 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69  ror message.** i
1fd0e 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74  n pParse and ret
1fd0f 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20 20  urn WRC_Abort.  
1fd10 52 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65  Return WRC_Prune
1fd11 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a   on success..*/.
1fd12 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75  static int looku
1fd13 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a  pName(.  Parse *
1fd14 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
1fd15 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
1fd16 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  text */.  const 
1fd17 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 2f  char *zDb,     /
1fd18 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
1fd19 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
1fd1a 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c  g table, or NULL
1fd1b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1fd1c 20 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e 61   *zTab,    /* Na
1fd1d 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74  me of table cont
1fd1e 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f  aining column, o
1fd1f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73  r NULL */.  cons
1fd20 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20  t char *zCol,   
1fd21 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1fd22 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d  column. */.  Nam
1fd23 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20  eContext *pNC,  
1fd24 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f    /* The name co
1fd25 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65  ntext used to re
1fd26 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a  solve the name *
1fd27 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  /.  Expr *pExpr 
1fd28 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65           /* Make
1fd29 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20   this EXPR node 
1fd2a 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c  point to the sel
1fd2b 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  ected column */.
1fd2c 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  ){.  int i, j;  
1fd2d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1fd2e 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
1fd2f 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20  int cnt = 0;    
1fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fd31 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
1fd32 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e  atching column n
1fd33 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e  ames */.  int cn
1fd34 74 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20  tTab = 0;       
1fd35 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1fd36 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e  umber of matchin
1fd37 67 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f  g table names */
1fd38 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1fd39 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20   pParse->db;    
1fd3a 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1fd3b 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1fd3c 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
1fd3d 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1fd3e 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f         /* Use fo
1fd3f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70  r looping over p
1fd40 53 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f  SrcList items */
1fd41 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
1fd42 74 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d  t_item *pMatch =
1fd43 20 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63   0;  /* The matc
1fd44 68 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74  hing pSrcList it
1fd45 65 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  em */.  NameCont
1fd46 65 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e  ext *pTopNC = pN
1fd47 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72  C;        /* Fir
1fd48 73 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69  st namecontext i
1fd49 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20  n the list */.  
1fd4a 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20  Schema *pSchema 
1fd4b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1fd4c 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 74    /* Schema of t
1fd4d 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  he expression */
1fd4e 0a 20 20 69 6e 74 20 69 73 54 72 69 67 67 65 72  .  int isTrigger
1fd4f 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
1fd50 20 70 4e 43 20 29 3b 20 20 20 20 20 2f 2a 20 74   pNC );     /* t
1fd51 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20  he name context 
1fd52 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20  cannot be NULL. 
1fd53 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f  */.  assert( zCo
1fd54 6c 20 29 3b 20 20 20 20 2f 2a 20 54 68 65 20 5a  l );    /* The Z
1fd55 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74   in X.Y.Z cannot
1fd56 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 61 73   be NULL */.  as
1fd57 73 65 72 74 28 20 7e 45 78 70 72 48 61 73 41 6e  sert( ~ExprHasAn
1fd58 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  yProperty(pExpr,
1fd59 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50   EP_TokenOnly|EP
1fd5a 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 0a 20 20  _Reduced) );..  
1fd5b 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
1fd5c 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74  e node to no-mat
1fd5d 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e 69  ch */.  pExpr->i
1fd5e 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 45  Table = -1;.  pE
1fd5f 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20  xpr->pTab = 0;. 
1fd60 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69   ExprSetIrreduci
1fd61 62 6c 65 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f  ble(pExpr);..  /
1fd62 2a 20 53 74 61 72 74 20 61 74 20 74 68 65 20 69  * Start at the i
1fd63 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78  nner-most contex
1fd64 74 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61  t and move outwa
1fd65 72 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68  rd until a match
1fd66 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77   is found */.  w
1fd67 68 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74  hile( pNC && cnt
1fd68 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c  ==0 ){.    ExprL
1fd69 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20  ist *pEList;.   
1fd6a 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69   SrcList *pSrcLi
1fd6b 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69  st = pNC->pSrcLi
1fd6c 73 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72  st;..    if( pSr
1fd6d 63 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66  cList ){.      f
1fd6e 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53  or(i=0, pItem=pS
1fd6f 72 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72  rcList->a; i<pSr
1fd70 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  cList->nSrc; i++
1fd71 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
1fd72 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
1fd73 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62  .        int iDb
1fd74 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e  ;.        Column
1fd75 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20   *pCol;.  .     
1fd76 20 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d     pTab = pItem-
1fd77 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61  >pTab;.        a
1fd78 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 26  ssert( pTab!=0 &
1fd79 26 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30  & pTab->zName!=0
1fd7a 20 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20   );.        iDb 
1fd7b 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
1fd7c 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
1fd7d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20  >pSchema);.     
1fd7e 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
1fd7f 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20  >nCol>0 );.     
1fd80 20 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20     if( zTab ){. 
1fd81 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74           if( pIt
1fd82 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20  em->zAlias ){.  
1fd83 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
1fd84 7a 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d  zTabName = pItem
1fd85 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20  ->zAlias;.      
1fd86 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1fd87 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d  3StrICmp(zTabNam
1fd88 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f  e, zTab)!=0 ) co
1fd89 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
1fd8a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1fd8b 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e       char *zTabN
1fd8c 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d  ame = pTab->zNam
1fd8d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  e;.            i
1fd8e 66 28 20 4e 45 56 45 52 28 7a 54 61 62 4e 61 6d  f( NEVER(zTabNam
1fd8f 65 3d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 65 33  e==0) || sqlite3
1fd90 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65  StrICmp(zTabName
1fd91 2c 20 7a 54 61 62 29 21 3d 30 20 29 7b 0a 20 20  , zTab)!=0 ){.  
1fd92 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74              cont
1fd93 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  inue;.          
1fd94 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1fd95 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71  if( zDb!=0 && sq
1fd96 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d  lite3StrICmp(db-
1fd97 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
1fd98 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20   zDb)!=0 ){.    
1fd99 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
1fd9a 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
1fd9b 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
1fd9c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fd9d 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b  if( 0==(cntTab++
1fd9e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
1fd9f 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
1fda0 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20  Item->iCursor;. 
1fda1 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1fda2 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20  pTab = pTab;.   
1fda3 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d         pSchema =
1fda4 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
1fda5 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
1fda6 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20   = pItem;.      
1fda7 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28    }.        for(
1fda8 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e  j=0, pCol=pTab->
1fda9 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  aCol; j<pTab->nC
1fdaa 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; j++, pCol++)
1fdab 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1fdac 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
1fdad 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  Col->zName, zCol
1fdae 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1fdaf 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69      IdList *pUsi
1fdb0 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ng;.            
1fdb1 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  cnt++;.         
1fdb2 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1fdb3 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f   = pItem->iCurso
1fdb4 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  r;.            p
1fdb5 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61  Expr->pTab = pTa
1fdb6 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  b;.            p
1fdb7 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20  Match = pItem;. 
1fdb8 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65             pSche
1fdb9 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
1fdba 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ma;.            
1fdbb 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68  /* Substitute th
1fdbc 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20  e rowid (column 
1fdbd 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45  -1) for the INTE
1fdbe 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
1fdbf 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  */.            p
1fdc0 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  Expr->iColumn = 
1fdc1 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f  j==pTab->iPKey ?
1fdc2 20 2d 31 20 3a 20 28 69 31 36 29 6a 3b 0a 20 20   -1 : (i16)j;.  
1fdc3 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3c            if( i<
1fdc4 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31  pSrcList->nSrc-1
1fdc5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1fdc6 20 20 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a    if( pItem[1].j
1fdc7 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54  ointype & JT_NAT
1fdc8 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  URAL ){.        
1fdc9 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
1fdca 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 72 65  is match occurre
1fdcb 64 20 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61  d in the left ta
1fdcc 62 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 61 6c  ble of a natural
1fdcd 20 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 20   join,.         
1fdce 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 73         ** then s
1fdcf 6b 69 70 20 74 68 65 20 72 69 67 68 74 20 74 61  kip the right ta
1fdd0 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64  ble to avoid a d
1fdd1 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 2a  uplicate match *
1fdd2 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
1fdd3 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20    pItem++;.     
1fdd4 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
1fdd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
1fdd6 6c 73 65 20 69 66 28 20 28 70 55 73 69 6e 67 20  lse if( (pUsing 
1fdd7 3d 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e  = pItem[1].pUsin
1fdd8 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  g)!=0 ){.       
1fdd9 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
1fdda 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 73  his match occurs
1fddb 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61   on a column tha
1fddc 74 20 69 73 20 69 6e 20 74 68 65 20 55 53 49 4e  t is in the USIN
1fddd 47 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20  G clause.       
1fdde 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61           ** of a
1fddf 20 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 20   join, skip the 
1fde0 73 65 61 72 63 68 20 6f 66 20 74 68 65 20 72 69  search of the ri
1fde1 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65  ght table of the
1fde2 20 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 20   join.          
1fde3 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69        ** to avoi
1fde4 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61  d a duplicate ma
1fde5 74 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20  tch there. */.  
1fde6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
1fde7 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  t k;.           
1fde8 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c       for(k=0; k<
1fde9 70 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b  pUsing->nId; k++
1fdea 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1fdeb 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1fdec 53 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e  StrICmp(pUsing->
1fded 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  a[k].zName, zCol
1fdee 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1fdef 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65              pIte
1fdf0 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  m++;.           
1fdf1 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20           i++;.  
1fdf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdf3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1fdf4 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1fdf5 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
1fdf6 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
1fdf7 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1fdf8 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
1fdf9 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1fdfa 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1fdfb 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
1fdfc 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1fdfd 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61  .    /* If we ha
1fdfe 76 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 72  ve not already r
1fdff 65 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d 65  esolved the name
1fe00 2c 20 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 20  , then maybe .  
1fe01 20 20 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77    ** it is a new
1fe02 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67  .* or old.* trig
1fe03 67 65 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66  ger argument ref
1fe04 65 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20  erence.    */.  
1fe05 20 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20    if( zDb==0 && 
1fe06 7a 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d  zTab!=0 && cnt==
1fe07 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72  0 && pParse->pTr
1fe08 69 67 67 65 72 54 61 62 21 3d 30 20 29 7b 0a 20  iggerTab!=0 ){. 
1fe09 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 50       int op = pP
1fe0a 61 72 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70  arse->eTriggerOp
1fe0b 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  ;.      Table *p
1fe0c 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 61  Tab = 0;.      a
1fe0d 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 44 45  ssert( op==TK_DE
1fe0e 4c 45 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55  LETE || op==TK_U
1fe0f 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  PDATE || op==TK_
1fe10 49 4e 53 45 52 54 20 29 3b 0a 20 20 20 20 20 20  INSERT );.      
1fe11 69 66 28 20 6f 70 21 3d 54 4b 5f 44 45 4c 45 54  if( op!=TK_DELET
1fe12 45 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  E && sqlite3StrI
1fe13 43 6d 70 28 22 6e 65 77 22 2c 7a 54 61 62 29 20  Cmp("new",zTab) 
1fe14 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20  == 0 ){.        
1fe15 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
1fe16 31 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  1;.        pTab 
1fe17 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67  = pParse->pTrigg
1fe18 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 65 6c  erTab;.      }el
1fe19 73 65 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e  se if( op!=TK_IN
1fe1a 53 45 52 54 20 26 26 20 73 71 6c 69 74 65 33 53  SERT && sqlite3S
1fe1b 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 54 61  trICmp("old",zTa
1fe1c 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  b)==0 ){.       
1fe1d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
1fe1e 20 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62   0;.        pTab
1fe1f 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67   = pParse->pTrig
1fe20 67 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a  gerTab;.      }.
1fe21 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20  .      if( pTab 
1fe22 29 7b 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ){ .        int 
1fe23 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53  iCol;.        pS
1fe24 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53  chema = pTab->pS
1fe25 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63  chema;.        c
1fe26 6e 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20  ntTab++;.       
1fe27 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f   if( sqlite3IsRo
1fe28 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20  wid(zCol) ){.   
1fe29 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31         iCol = -1
1fe2a 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1fe2b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69  .          for(i
1fe2c 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62  Col=0; iCol<pTab
1fe2d 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b  ->nCol; iCol++){
1fe2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c  .            Col
1fe2f 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61  umn *pCol = &pTa
1fe30 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20  b->aCol[iCol];. 
1fe31 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73             if( s
1fe32 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
1fe33 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  ol->zName, zCol)
1fe34 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1fe35 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70       if( iCol==p
1fe36 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
1fe37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43                iC
1fe38 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20  ol = -1;.       
1fe39 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1fe3a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1fe3b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1fe3c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fe3d 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43  }.        if( iC
1fe3e 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  ol<pTab->nCol ){
1fe3f 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b  .          cnt++
1fe40 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1fe41 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20  iCol<0 ){.      
1fe42 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66        pExpr->aff
1fe43 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
1fe44 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  FF_INTEGER;.    
1fe45 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1fe46 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30  pExpr->iTable==0
1fe47 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1fe48 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d  testcase( iCol==
1fe49 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  31 );.          
1fe4a 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c    testcase( iCol
1fe4b 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20  ==32 );.        
1fe4c 20 20 20 20 70 50 61 72 73 65 2d 3e 6f 6c 64 6d      pParse->oldm
1fe4d 61 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d 33 32  ask |= (iCol>=32
1fe4e 20 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20   ? 0xffffffff : 
1fe4f 28 28 28 75 33 32 29 31 29 3c 3c 69 43 6f 6c 29  (((u32)1)<<iCol)
1fe50 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
1fe51 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1fe52 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 69  iColumn = (i16)i
1fe53 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 70  Col;.          p
1fe54 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61  Expr->pTab = pTa
1fe55 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69 73 54  b;.          isT
1fe56 72 69 67 67 65 72 20 3d 20 31 3b 0a 20 20 20 20  rigger = 1;.    
1fe57 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1fe58 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64    }.#endif /* !d
1fe59 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1fe5a 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a  IT_TRIGGER) */..
1fe5b 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65      /*.    ** Pe
1fe5c 72 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69  rhaps the name i
1fe5d 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
1fe5e 20 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a   the ROWID.    *
1fe5f 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30  /.    if( cnt==0
1fe60 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26   && cntTab==1 &&
1fe61 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
1fe62 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63  zCol) ){.      c
1fe63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45  nt = 1;.      pE
1fe64 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d  xpr->iColumn = -
1fe65 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  1;.      pExpr->
1fe66 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54  affinity = SQLIT
1fe67 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20  E_AFF_INTEGER;. 
1fe68 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
1fe69 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74   ** If the input
1fe6a 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   is of the form 
1fe6b 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e  Z (not Y.Z or X.
1fe6c 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61  Y.Z) then the na
1fe6d 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68  me Z.    ** migh
1fe6e 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65  t refer to an re
1fe6f 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20  sult-set alias. 
1fe70 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66   This happens, f
1fe71 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e  or example, when
1fe72 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72  .    ** we are r
1fe73 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69  esolving names i
1fe74 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
1fe75 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  se of the follow
1fe76 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20  ing command:.   
1fe77 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53   **.    **     S
1fe78 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46  ELECT a+b AS x F
1fe79 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20  ROM table WHERE 
1fe7a 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20  x<10;.    **.   
1fe7b 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b   ** In cases lik
1fe7c 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20  e this, replace 
1fe7d 70 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70  pExpr with a cop
1fe7e 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  y of the express
1fe7f 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ion that.    ** 
1fe80 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74  forms the result
1fe81 20 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62   set entry ("a+b
1fe82 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65  " in the example
1fe83 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d  ) and return imm
1fe84 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a  ediately..    **
1fe85 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65   Note that the e
1fe86 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
1fe87 20 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75   result set shou
1fe88 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ld have already 
1fe89 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f  been.    ** reso
1fe8a 6c 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65  lved by the time
1fe8b 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1fe8c 65 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20  e is resolved.. 
1fe8d 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e     */.    if( cn
1fe8e 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20  t==0 && (pEList 
1fe8f 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d  = pNC->pEList)!=
1fe90 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a  0 && zTab==0 ){.
1fe91 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
1fe92 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
1fe93 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68  j++){.        ch
1fe94 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74  ar *zAs = pEList
1fe95 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->a[j].zName;.  
1fe96 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30        if( zAs!=0
1fe97 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1fe98 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30  mp(zAs, zCol)==0
1fe99 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78   ){.          Ex
1fe9a 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20  pr *pOrig;.     
1fe9b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
1fe9c 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20  pr->pLeft==0 && 
1fe9d 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30  pExpr->pRight==0
1fe9e 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   );.          as
1fe9f 73 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70  sert( pExpr->x.p
1fea0 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  List==0 );.     
1fea1 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
1fea2 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30  pr->x.pSelect==0
1fea3 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   );.          pO
1fea4 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  rig = pEList->a[
1fea5 6a 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  j].pExpr;.      
1fea6 20 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c      if( !pNC->al
1fea7 6c 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61  lowAgg && ExprHa
1fea8 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c  sProperty(pOrig,
1fea9 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20   EP_Agg) ){.    
1feaa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1feab 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1feac 22 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73  "misuse of alias
1fead 65 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22  ed aggregate %s"
1feae 2c 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20  , zAs);.        
1feaf 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
1feb0 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 20 20  bort;.          
1feb1 7d 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f  }.          reso
1feb2 6c 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c  lveAlias(pParse,
1feb3 20 70 45 4c 69 73 74 2c 20 6a 2c 20 70 45 78 70   pEList, j, pExp
1feb4 72 2c 20 22 22 29 3b 0a 20 20 20 20 20 20 20 20  r, "");.        
1feb5 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20    cnt = 1;.     
1feb6 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b       pMatch = 0;
1feb7 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1feb8 74 28 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44  t( zTab==0 && zD
1feb9 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  b==0 );.        
1feba 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d    goto lookupnam
1febb 65 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d  e_end;.        }
1febc 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a  .      } .    }.
1febd 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20  .    /* Advance 
1febe 74 6f 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65  to the next name
1febf 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c   context.  The l
1fec0 6f 6f 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68  oop will exit wh
1fec1 65 6e 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a  en either.    **
1fec2 20 77 65 20 68 61 76 65 20 61 20 6d 61 74 63 68   we have a match
1fec3 20 28 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e   (cnt>0) or when
1fec4 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e   we run out of n
1fec5 61 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20  ame contexts..  
1fec6 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74    */.    if( cnt
1fec7 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43  ==0 ){.      pNC
1fec8 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20   = pNC->pNext;. 
1fec9 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20     }.  }..  /*. 
1feca 20 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61   ** If X and Y a
1fecb 72 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65  re NULL (in othe
1fecc 72 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20  r words if only 
1fecd 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  the column name 
1fece 5a 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69  Z is.  ** suppli
1fecf 65 64 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75  ed) and the valu
1fed0 65 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73  e of Z is enclos
1fed1 65 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f  ed in double-quo
1fed2 74 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a  tes, then.  ** Z
1fed3 20 69 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74   is a string lit
1fed4 65 72 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e  eral if it doesn
1fed5 27 74 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c  't match any col
1fed6 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74  umn names.  In t
1fed7 68 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77  hat.  ** case, w
1fed8 65 20 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e  e need to return
1fed9 20 72 69 67 68 74 20 61 77 61 79 20 61 6e 64 20   right away and 
1feda 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61  not make any cha
1fedb 6e 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78  nges to.  ** pEx
1fedc 70 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65  pr..  **.  ** Be
1fedd 63 61 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e  cause no referen
1fede 63 65 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f  ce was made to o
1fedf 75 74 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74  uter contexts, t
1fee0 68 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a  he pNC->nRef.  *
1fee1 2a 20 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74  * fields are not
1fee2 20 63 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20   changed in any 
1fee3 63 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20  context..  */.  
1fee4 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54  if( cnt==0 && zT
1fee5 61 62 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73  ab==0 && ExprHas
1fee6 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45  Property(pExpr,E
1fee7 50 5f 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0a  P_DblQuoted) ){.
1fee8 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
1fee9 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70  TK_STRING;.    p
1feea 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a  Expr->pTab = 0;.
1feeb 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
1feec 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  rune;.  }..  /*.
1feed 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e    ** cnt==0 mean
1feee 73 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20  s there was not 
1feef 6d 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65  match.  cnt>1 me
1fef0 61 6e 73 20 74 68 65 72 65 20 77 65 72 65 20 74  ans there were t
1fef1 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20  wo or.  ** more 
1fef2 6d 61 74 63 68 65 73 2e 20 20 45 69 74 68 65 72  matches.  Either
1fef3 20 77 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e   way, we have an
1fef4 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69   error..  */.  i
1fef5 66 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20  f( cnt!=1 ){.   
1fef6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72   const char *zEr
1fef7 72 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e  r;.    zErr = cn
1fef8 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20  t==0 ? "no such 
1fef9 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67  column" : "ambig
1fefa 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  uous column name
1fefb 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29  ";.    if( zDb )
1fefc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1fefd 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1fefe 22 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20  "%s: %s.%s.%s", 
1feff 7a 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c  zErr, zDb, zTab,
1ff00 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73   zCol);.    }els
1ff01 65 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20  e if( zTab ){.  
1ff02 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1ff03 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a  Msg(pParse, "%s:
1ff04 20 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a   %s.%s", zErr, z
1ff05 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  Tab, zCol);.    
1ff06 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
1ff07 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1ff08 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a  rse, "%s: %s", z
1ff09 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  Err, zCol);.    
1ff0a 7d 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45  }.    pTopNC->nE
1ff0b 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rr++;.  }..  /* 
1ff0c 49 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d  If a column from
1ff0d 20 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63   a table in pSrc
1ff0e 4c 69 73 74 20 69 73 20 72 65 66 65 72 65 6e 63  List is referenc
1ff0f 65 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a  ed, then record.
1ff10 20 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69    ** this fact i
1ff11 6e 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61  n the pSrcList.a
1ff12 5b 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61  [].colUsed bitma
1ff13 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61  sk.  Column 0 ca
1ff14 75 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20  uses.  ** bit 0 
1ff15 74 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75  to be set.  Colu
1ff16 6d 6e 20 31 20 73 65 74 73 20 62 69 74 20 31 2e  mn 1 sets bit 1.
1ff17 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20    And so forth. 
1ff18 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c   If the.  ** col
1ff19 75 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72  umn number is gr
1ff1a 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e  eater than the n
1ff1b 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e  umber of bits in
1ff1c 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a   the bitmask.  *
1ff1d 2a 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 68  * then set the h
1ff1e 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66  igh-order bit of
1ff1f 20 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20   the bitmask..  
1ff20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  */.  if( pExpr->
1ff21 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d  iColumn>=0 && pM
1ff22 61 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69  atch!=0 ){.    i
1ff23 6e 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43  nt n = pExpr->iC
1ff24 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63  olumn;.    testc
1ff25 61 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b  ase( n==BMS-1 );
1ff26 0a 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20  .    if( n>=BMS 
1ff27 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53  ){.      n = BMS
1ff28 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  -1;.    }.    as
1ff29 73 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43  sert( pMatch->iC
1ff2a 75 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54  ursor==pExpr->iT
1ff2b 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74  able );.    pMat
1ff2c 63 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28  ch->colUsed |= (
1ff2d 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a  (Bitmask)1)<<n;.
1ff2e 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20    }..  /* Clean 
1ff2f 75 70 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20  up and return.  
1ff30 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  */.  sqlite3Expr
1ff31 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72  Delete(db, pExpr
1ff32 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70  ->pLeft);.  pExp
1ff33 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20  r->pLeft = 0;.  
1ff34 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1ff35 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69  e(db, pExpr->pRi
1ff36 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70  ght);.  pExpr->p
1ff37 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78  Right = 0;.  pEx
1ff38 70 72 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67  pr->op = (isTrig
1ff39 67 65 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52  ger ? TK_TRIGGER
1ff3a 20 3a 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c   : TK_COLUMN);.l
1ff3b 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20  ookupname_end:. 
1ff3c 20 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20   if( cnt==1 ){. 
1ff3d 20 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d     assert( pNC!=
1ff3e 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 );.    sqlite3
1ff3f 41 75 74 68 52 65 61 64 28 70 50 61 72 73 65 2c  AuthRead(pParse,
1ff40 20 70 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c   pExpr, pSchema,
1ff41 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b   pNC->pSrcList);
1ff42 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e  .    /* Incremen
1ff43 74 20 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65  t the nRef value
1ff44 20 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e   on all name con
1ff45 74 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43  texts from TopNC
1ff46 20 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68   up to.    ** th
1ff47 65 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68  e point where th
1ff48 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20  e name matched. 
1ff49 2a 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a  */.    for(;;){.
1ff4a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
1ff4b 6f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20  opNC!=0 );.     
1ff4c 20 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b   pTopNC->nRef++;
1ff4d 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e  .      if( pTopN
1ff4e 43 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a  C==pNC ) break;.
1ff4f 20 20 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70        pTopNC = p
1ff50 54 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20  TopNC->pNext;.  
1ff51 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57    }.    return W
1ff52 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c  RC_Prune;.  } el
1ff53 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  se {.    return 
1ff54 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d  WRC_Abort;.  }.}
1ff55 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
1ff56 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f   and return a po
1ff57 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72  inter to an expr
1ff58 65 73 73 69 6f 6e 20 74 6f 20 6c 6f 61 64 20 74  ession to load t
1ff59 68 65 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 0a 2a  he column iCol.*
1ff5a 2a 20 66 72 6f 6d 20 64 61 74 61 73 6f 75 72 63  * from datasourc
1ff5b 65 20 69 53 72 63 20 64 61 74 61 73 6f 75 72 63  e iSrc datasourc
1ff5c 65 20 69 6e 20 53 72 63 4c 69 73 74 20 70 53 72  e in SrcList pSr
1ff5d 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  c..*/.SQLITE_PRI
1ff5e 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
1ff5f 65 33 43 72 65 61 74 65 43 6f 6c 75 6d 6e 45 78  e3CreateColumnEx
1ff60 70 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  pr(sqlite3 *db, 
1ff61 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 69  SrcList *pSrc, i
1ff62 6e 74 20 69 53 72 63 2c 20 69 6e 74 20 69 43 6f  nt iSrc, int iCo
1ff63 6c 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  l){.  Expr *p = 
1ff64 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63  sqlite3ExprAlloc
1ff65 28 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20  (db, TK_COLUMN, 
1ff66 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 29  0, 0);.  if( p )
1ff67 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63  {.    struct Src
1ff68 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1ff69 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 53 72 63   = &pSrc->a[iSrc
1ff6a 5d 3b 0a 20 20 20 20 70 2d 3e 70 54 61 62 20 3d  ];.    p->pTab =
1ff6b 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20   pItem->pTab;.  
1ff6c 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49    p->iTable = pI
1ff6d 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
1ff6e 20 20 69 66 28 20 70 2d 3e 70 54 61 62 2d 3e 69    if( p->pTab->i
1ff6f 50 4b 65 79 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20  PKey==iCol ){.  
1ff70 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d      p->iColumn =
1ff71 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   -1;.    }else{.
1ff72 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e        p->iColumn
1ff73 20 3d 20 28 79 6e 56 61 72 29 69 43 6f 6c 3b 0a   = (ynVar)iCol;.
1ff74 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 63 6f 6c        pItem->col
1ff75 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73  Used |= ((Bitmas
1ff76 6b 29 31 29 3c 3c 28 69 43 6f 6c 3e 3d 42 4d 53  k)1)<<(iCol>=BMS
1ff77 20 3f 20 42 4d 53 2d 31 20 3a 20 69 43 6f 6c 29   ? BMS-1 : iCol)
1ff78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 45 78 70 72  ;.    }.    Expr
1ff79 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45  SetProperty(p, E
1ff7a 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 20 20 7d  P_Resolved);.  }
1ff7b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
1ff7c 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1ff7d 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 66  ne is callback f
1ff7e 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  or sqlite3WalkEx
1ff7f 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f  pr()..**.** Reso
1ff80 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d  lve symbolic nam
1ff81 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d  es into TK_COLUM
1ff82 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 20  N operators for 
1ff83 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e  the current.** n
1ff84 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 65  ode in the expre
1ff85 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74  ssion tree.  Ret
1ff86 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75  urn 0 to continu
1ff87 65 20 74 68 65 20 73 65 61 72 63 68 20 64 6f 77  e the search dow
1ff88 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f 72  n.** the tree or
1ff89 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20   2 to abort the 
1ff8a 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a  tree walk..**.**
1ff8b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   This routine al
1ff8c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 68  so does error ch
1ff8d 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20  ecking and name 
1ff8e 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a  resolution for.*
1ff8f 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73  * function names
1ff90 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20  .  The operator 
1ff91 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75  for aggregate fu
1ff92 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e 67  nctions is chang
1ff93 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f  ed.** to TK_AGG_
1ff94 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61  FUNCTION..*/.sta
1ff95 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 45  tic int resolveE
1ff96 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 2a  xprStep(Walker *
1ff97 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70  pWalker, Expr *p
1ff98 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e  Expr){.  NameCon
1ff99 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72  text *pNC;.  Par
1ff9a 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70  se *pParse;..  p
1ff9b 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e  NC = pWalker->u.
1ff9c 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70  pNC;.  assert( p
1ff9d 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 73  NC!=0 );.  pPars
1ff9e 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
1ff9f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
1ffa0 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72  e==pWalker->pPar
1ffa1 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 70  se );..  if( Exp
1ffa2 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
1ffa3 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76  pExpr, EP_Resolv
1ffa4 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  ed) ) return WRC
1ffa5 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 65  _Prune;.  ExprSe
1ffa6 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  tProperty(pExpr,
1ffa7 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23   EP_Resolved);.#
1ffa8 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
1ffa9 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  if( pNC->pSrcLis
1ffaa 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69  t && pNC->pSrcLi
1ffab 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a  st->nAlloc>0 ){.
1ffac 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72      SrcList *pSr
1ffad 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  cList = pNC->pSr
1ffae 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69  cList;.    int i
1ffaf 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1ffb0 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e  <pNC->pSrcList->
1ffb1 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSrc; i++){.    
1ffb2 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69    assert( pSrcLi
1ffb3 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72  st->a[i].iCursor
1ffb4 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d  >=0 && pSrcList-
1ffb5 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50  >a[i].iCursor<pP
1ffb6 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20  arse->nTab);.   
1ffb7 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
1ffb8 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f  switch( pExpr->o
1ffb9 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65  p ){..#if define
1ffba 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
1ffbb 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49  UPDATE_DELETE_LI
1ffbc 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64  MIT) && !defined
1ffbd 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
1ffbe 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 68  QUERY).    /* Th
1ffbf 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74  e special operat
1ffc0 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20  or TK_ROW means 
1ffc1 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f  use the rowid fo
1ffc2 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20  r the first.    
1ffc3 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  ** column in the
1ffc4 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54   FROM clause.  T
1ffc5 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74  his is used by t
1ffc6 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44  he LIMIT and ORD
1ffc7 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61  ER BY.    ** cla
1ffc8 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f  use processing o
1ffc9 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 4c  n UPDATE and DEL
1ffca 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ETE statements..
1ffcb 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
1ffcc 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20  TK_ROW: {.      
1ffcd 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
1ffce 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
1ffcf 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  t;.      struct 
1ffd0 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
1ffd1 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  tem;.      asser
1ffd2 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 70  t( pSrcList && p
1ffd3 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  SrcList->nSrc==1
1ffd4 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 20   );.      pItem 
1ffd5 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a  = pSrcList->a; .
1ffd6 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
1ffd7 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20  = TK_COLUMN;.   
1ffd8 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d     pExpr->pTab =
1ffd9 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20   pItem->pTab;.  
1ffda 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
1ffdb 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73  e = pItem->iCurs
1ffdc 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  or;.      pExpr-
1ffdd 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20  >iColumn = -1;. 
1ffde 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69       pExpr->affi
1ffdf 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46  nity = SQLITE_AF
1ffe0 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  F_INTEGER;.     
1ffe1 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
1ffe2 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28  ndif /* defined(
1ffe3 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50  SQLITE_ENABLE_UP
1ffe4 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49  DATE_DELETE_LIMI
1ffe5 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  T) && !defined(S
1ffe6 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1ffe7 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  ERY) */..    /* 
1ffe8 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 65  A lone identifie
1ffe9 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  r is the name of
1ffea 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a   a column..    *
1ffeb 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44  /.    case TK_ID
1ffec 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  : {.      return
1ffed 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72   lookupName(pPar
1ffee 73 65 2c 20 30 2c 20 30 2c 20 70 45 78 70 72 2d  se, 0, 0, pExpr-
1ffef 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43 2c 20  >u.zToken, pNC, 
1fff0 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20  pExpr);.    }.  
1fff1 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20  .    /* A table 
1fff2 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20  name and column 
1fff3 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0a  name:     ID.ID.
1fff4 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61      ** Or a data
1fff5 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20  base, table and 
1fff6 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49  column:  ID.ID.I
1fff7 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  D.    */.    cas
1fff8 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 20  e TK_DOT: {.    
1fff9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
1fffa 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63 6f 6e  olumn;.      con
1fffb 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 3b  st char *zTable;
1fffc 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
1fffd 72 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20 45 78  r *zDb;.      Ex
1fffe 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20  pr *pRight;..   
1ffff 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c 69     /* if( pSrcLi
20000 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a  st==0 ) break; *
20001 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d  /.      pRight =
20002 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a   pExpr->pRight;.
20003 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74        if( pRight
20004 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20  ->op==TK_ID ){. 
20005 20 20 20 20 20 20 20 7a 44 62 20 3d 20 30 3b 0a         zDb = 0;.
20006 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20 3d          zTable =
20007 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75   pExpr->pLeft->u
20008 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20  .zToken;.       
20009 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 68   zColumn = pRigh
2000a 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  t->u.zToken;.   
2000b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2000c 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74    assert( pRight
2000d 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0a  ->op==TK_DOT );.
2000e 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 70 45          zDb = pE
2000f 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54  xpr->pLeft->u.zT
20010 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a 54  oken;.        zT
20011 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d 3e 70  able = pRight->p
20012 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  Left->u.zToken;.
20013 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 20          zColumn 
20014 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67 68 74  = pRight->pRight
20015 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ->u.zToken;.    
20016 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
20017 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72   lookupName(pPar
20018 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c 65 2c  se, zDb, zTable,
20019 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70   zColumn, pNC, p
2001a 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Expr);.    }..  
2001b 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 6e    /* Resolve fun
2001c 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 20  ction names.    
2001d 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  */.    case TK_C
2001e 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63  ONST_FUNC:.    c
2001f 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a  ase TK_FUNCTION:
20020 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73   {.      ExprLis
20021 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72  t *pList = pExpr
20022 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f 2a  ->x.pList;    /*
20023 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c 69   The argument li
20024 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  st */.      int 
20025 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 73  n = pList ? pLis
20026 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20 20  t->nExpr : 0;   
20027 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
20028 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  guments */.     
20029 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 6e   int no_such_fun
2002a 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  c = 0;       /* 
2002b 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 20  True if no such 
2002c 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 20  function exists 
2002d 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 6f  */.      int wro
2002e 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 3b  ng_num_args = 0;
2002f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
20030 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20  wrong number of 
20031 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20  arguments */.   
20032 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d 20     int is_agg = 
20033 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
20034 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e 20  * True if is an 
20035 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
20036 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  on */.      int 
20037 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  auth;           
20038 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 6f          /* Autho
20039 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 20  rization to use 
2003a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
2003b 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20        int nId;  
2003c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2003d 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2003e 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 6e  haracters in fun
2003f 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
20040 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
20041 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  zId;            
20042 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  /* The function 
20043 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46  name. */.      F
20044 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20  uncDef *pDef;   
20045 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
20046 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
20047 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
20048 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45        u8 enc = E
20049 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 20  NC(pParse->db); 
2004a 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2004b 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a 20  e encoding */.. 
2004c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
2004d 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4e  Expr->op==TK_CON
2004e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 20  ST_FUNC );.     
2004f 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
20050 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
20051 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
20052 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 70 45  ;.      zId = pE
20053 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  xpr->u.zToken;. 
20054 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c 69 74       nId = sqlit
20055 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 29 3b  e3Strlen30(zId);
20056 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71  .      pDef = sq
20057 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
20058 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49  n(pParse->db, zI
20059 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20  d, nId, n, enc, 
2005a 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44  0);.      if( pD
2005b 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ef==0 ){.       
2005c 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46   pDef = sqlite3F
2005d 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  indFunction(pPar
2005e 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64  se->db, zId, nId
2005f 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20  , -1, enc, 0);. 
20060 20 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d         if( pDef=
20061 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
20062 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 31  no_such_func = 1
20063 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
20064 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67  .          wrong
20065 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20  _num_args = 1;. 
20066 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
20067 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 73  else{.        is
20068 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 75  _agg = pDef->xFu
20069 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23  nc==0;.      }.#
2006a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2006b 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
2006c 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 20  .      if( pDef 
2006d 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 20  ){.        auth 
2006e 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  = sqlite3AuthChe
2006f 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
20070 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70  E_FUNCTION, 0, p
20071 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Def->zName, 0);.
20072 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68          if( auth
20073 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
20074 20 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74           if( aut
20075 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29  h==SQLITE_DENY )
20076 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  {.            sq
20077 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
20078 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f  arse, "not autho
20079 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 6e  rized to use fun
2007a 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20  ction: %s",.    
2007b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2007c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2007d 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pDef->zName);.  
2007e 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e            pNC->n
2007f 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  Err++;.         
20080 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78   }.          pEx
20081 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c  pr->op = TK_NULL
20082 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
20083 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
20084 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
20085 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28  #endif.      if(
20086 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43 2d   is_agg && !pNC-
20087 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20 20  >allowAgg ){.   
20088 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
20089 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69  rMsg(pParse, "mi
2008a 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74  suse of aggregat
2008b 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28  e function %.*s(
2008c 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20  )", nId,zId);.  
2008d 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b        pNC->nErr+
2008e 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67  +;.        is_ag
2008f 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  g = 0;.      }el
20090 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66  se if( no_such_f
20091 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73  unc ){.        s
20092 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
20093 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
20094 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c  function: %.*s",
20095 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20   nId, zId);.    
20096 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b      pNC->nErr++;
20097 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
20098 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20   wrong_num_args 
20099 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2009a 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2009b 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20  e,"wrong number 
2009c 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
2009d 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22  function %.*s()"
2009e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  ,.             n
2009f 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20  Id, zId);.      
200a0 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20    pNC->nErr++;. 
200a1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
200a2 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20 20   is_agg ){.     
200a3 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
200a4 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a  K_AGG_FUNCTION;.
200a5 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 73          pNC->has
200a6 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  Agg = 1;.      }
200a7 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67  .      if( is_ag
200a8 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67  g ) pNC->allowAg
200a9 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  g = 0;.      sql
200aa 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74  ite3WalkExprList
200ab 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74 29  (pWalker, pList)
200ac 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61  ;.      if( is_a
200ad 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41  gg ) pNC->allowA
200ae 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a  gg = 1;.      /*
200af 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74   FIX ME:  Comput
200b0 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  e pExpr->affinit
200b1 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 65  y based on the e
200b2 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a 20  xpected return. 
200b3 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 20       ** type of 
200b4 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20  the function .  
200b5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74      */.      ret
200b6 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
200b7 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
200b8 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
200b9 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45  Y.    case TK_SE
200ba 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54  LECT:.    case T
200bb 4b 5f 45 58 49 53 54 53 3a 20 20 74 65 73 74 63  K_EXISTS:  testc
200bc 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
200bd 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 65 6e  TK_EXISTS );.#en
200be 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  dif.    case TK_
200bf 49 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  IN: {.      test
200c0 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
200c1 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20 20 20  =TK_IN );.      
200c2 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
200c3 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
200c4 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
200c5 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 70      int nRef = p
200c6 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 65  NC->nRef;.#ifnde
200c7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
200c8 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ECK.        if( 
200c9 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a  pNC->isCheck ){.
200ca 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
200cb 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
200cc 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 72 6f  ,"subqueries pro
200cd 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b  hibited in CHECK
200ce 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a   constraints");.
200cf 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
200d0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
200d1 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b  WalkSelect(pWalk
200d2 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  er, pExpr->x.pSe
200d3 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 61  lect);.        a
200d4 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 66  ssert( pNC->nRef
200d5 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 20  >=nRef );.      
200d6 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 2d    if( nRef!=pNC-
200d7 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 20  >nRef ){.       
200d8 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
200d9 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72  ty(pExpr, EP_Var
200da 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20  Select);.       
200db 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
200dc 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69   break;.    }.#i
200dd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
200de 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 73 65  T_CHECK.    case
200df 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a   TK_VARIABLE: {.
200e0 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69        if( pNC->i
200e1 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20  sCheck ){.      
200e2 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
200e3 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 6d 65  g(pParse,"parame
200e4 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 64 20  ters prohibited 
200e5 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  in CHECK constra
200e6 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a  ints");.      }.
200e7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
200e8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20   }.#endif.  }.  
200e9 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e  return (pParse->
200ea 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d 3e  nErr || pParse->
200eb 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
200ec 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20  ) ? WRC_Abort : 
200ed 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a  WRC_Continue;.}.
200ee 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73  ./*.** pEList is
200ef 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65   a list of expre
200f0 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72 65  ssions which are
200f1 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 73 75   really the resu
200f2 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a 2a 2a  lt set of the.**
200f3 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
200f4 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 74 65  ent.  pE is a te
200f5 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42  rm in an ORDER B
200f6 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c  Y or GROUP BY cl
200f7 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  ause..** This ro
200f8 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 6f 20  utine checks to 
200f9 73 65 65 20 69 66 20 70 45 20 69 73 20 61 20 73  see if pE is a s
200fa 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72  imple identifier
200fb 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e   which correspon
200fc 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 2d  ds.** to the AS-
200fd 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 74  name of one of t
200fe 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20  he terms of the 
200ff 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e  expression list.
20100 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 74    If it is,.** t
20101 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
20102 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65  rn an integer be
20103 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 68  tween 1 and N wh
20104 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ere N is the num
20105 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 6e  ber of.** elemen
20106 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 6f  ts in pEList, co
20107 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74  rresponding to t
20108 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72  he matching entr
20109 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 0a  y.  If there is.
2010a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 20  ** no match, or 
2010b 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 20 73  if pE is not a s
2010c 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72  imple identifier
2010d 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
2010e 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e  ine.** return 0.
2010f 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 61  .**.** pEList ha
20110 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e  s been resolved.
20111 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a 2f    pE has not..*/
20112 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
20113 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 72  lveAsName(.  Par
20114 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
20115 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
20116 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73  xt for error mes
20117 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c  sages */.  ExprL
20118 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f 2a  ist *pEList,  /*
20119 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   List of express
2011a 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a  ions to scan */.
2011b 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20 20    Expr *pE      
2011c 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
2011d 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 6e 67  on we are trying
2011e 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a   to match */.){.
2011f 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
20120 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
20121 6e 74 65 72 20 2a 2f 0a 0a 20 20 55 4e 55 53 45  nter */..  UNUSE
20122 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61 72  D_PARAMETER(pPar
20123 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 45 2d 3e  se);..  if( pE->
20124 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20  op==TK_ID ){.   
20125 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 45   char *zCol = pE
20126 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ->u.zToken;.    
20127 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73  for(i=0; i<pELis
20128 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
20129 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20        char *zAs 
2012a 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  = pEList->a[i].z
2012b 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Name;.      if( 
2012c 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65  zAs!=0 && sqlite
2012d 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43  3StrICmp(zAs, zC
2012e 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
2012f 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20    return i+1;.  
20130 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
20131 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
20132 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 6f 69  *.** pE is a poi
20133 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65  nter to an expre
20134 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 20 61  ssion which is a
20135 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20   single term in 
20136 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20  the.** ORDER BY 
20137 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  of a compound SE
20138 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 72 65  LECT.  The expre
20139 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65  ssion has not be
2013a 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c  en.** name resol
2013b 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68  ved..**.** At th
2013c 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f 75  e point this rou
2013d 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
2013e 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20  we already know 
2013f 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 45  that the.** ORDE
20140 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e 6f 74  R BY term is not
20141 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 65   an integer inde
20142 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 75 6c  x into the resul
20143 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a 20  t set.  That.** 
20144 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 20  case is handled 
20145 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72  by the calling r
20146 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 74  outine..**.** At
20147 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 70  tempt to match p
20148 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74  E against result
20149 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   set columns in 
2014a 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a 2a  the left-most.**
2014b 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2014c 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69  t.  Return the i
2014d 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 6d 61  ndex i of the ma
2014e 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a 2a  tching column,.*
2014f 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 74 69  * as an indicati
20150 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  on to the caller
20151 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20   that it should 
20152 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74 68  sort by the i-th
20153 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 20   column..** The 
20154 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
20155 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 72   is 1.  In other
20156 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 6c 75   words, the valu
20157 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  e returned is th
20158 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 65  e.** same intege
20159 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f 75  r value that wou
2015a 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 68  ld be used in th
2015b 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
2015c 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74  to indicate.** t
2015d 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a  he column..**.**
2015e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
2015f 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e  match, return 0.
20160 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20 61    Return -1 if a
20161 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  n error occurs..
20162 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
20163 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d  solveOrderByTerm
20164 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61  ToExprList(.  Pa
20165 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
20166 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20167 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65  ext for error me
20168 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c 65  ssages */.  Sele
20169 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f  ct *pSelect,   /
2016a 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
2016b 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65 20  tement with the 
2016c 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2016d 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20  */.  Expr *pE   
2016e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
2016f 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42 59  pecific ORDER BY
20170 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e   term */.){.  in
20171 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
20172 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
20173 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
20174 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 20  pEList;  /* The 
20175 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 72  columns of the r
20176 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 4e  esult set */.  N
20177 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 20  ameContext nc;  
20178 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
20179 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 20  t for resolving 
2017a 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  pE */..  assert(
2017b 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e   sqlite3ExprIsIn
2017c 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d 3d 30  teger(pE, &i)==0
2017d 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70   );.  pEList = p
2017e 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a  Select->pEList;.
2017f 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 6c  .  /* Resolve al
20180 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 4f  l names in the O
20181 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78 70  RDER BY term exp
20182 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d  ression.  */.  m
20183 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 69  emset(&nc, 0, si
20184 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 2e  zeof(nc));.  nc.
20185 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
20186 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 3d  .  nc.pSrcList =
20187 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a   pSelect->pSrc;.
20188 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 45    nc.pEList = pE
20189 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f 77  List;.  nc.allow
2018a 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e 45  Agg = 1;.  nc.nE
2018b 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71  rr = 0;.  if( sq
2018c 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
2018d 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 29  Names(&nc, pE) )
2018e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2018f 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  orClear(pParse);
20190 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
20191 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20   }..  /* Try to 
20192 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52 20  match the ORDER 
20193 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61 67  BY expression ag
20194 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73 73  ainst an express
20195 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ion.  ** in the 
20196 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65 74  result set.  Ret
20197 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 69  urn an 1-based i
20198 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63  ndex of the matc
20199 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c 74  hing.  ** result
2019a 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a 2f  -set entry..  */
2019b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45  .  for(i=0; i<pE
2019c 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
2019d 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
2019e 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45  e3ExprCompare(pE
2019f 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
201a0 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72  , pE) ){.      r
201a1 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 7d  eturn i+1;.    }
201a2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f  .  }..  /* If no
201a3 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30   match, return 0
201a4 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b  . */.  return 0;
201a5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
201a6 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f  te an ORDER BY o
201a7 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 20  r GROUP BY term 
201a8 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 72  out-of-range err
201a9 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  or..*/.static vo
201aa 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52  id resolveOutOfR
201ab 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 61 72  angeError(.  Par
201ac 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
201ad 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 6f 72      /* The error
201ae 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 68   context into wh
201af 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68 65  ich to write the
201b0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73   error */.  cons
201b1 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20  t char *zType,  
201b2 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72     /* "ORDER" or
201b3 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 6e   "GROUP" */.  in
201b4 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20  t i,            
201b5 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
201b6 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 20 74  x (1-based) of t
201b7 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72  he term out of r
201b8 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  ange */.  int mx
201b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
201ba 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 72 6d   /* Largest perm
201bb 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 6f 66  issible value of
201bc 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74   i */.){.  sqlit
201bd 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
201be 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 20 42  e, .    "%r %s B
201bf 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61  Y term out of ra
201c0 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 20  nge - should be 
201c1 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e 20 31  ".    "between 1
201c2 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 79   and %d", i, zTy
201c3 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pe, mx);.}../*.*
201c4 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f 52  * Analyze the OR
201c5 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e  DER BY clause in
201c6 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
201c7 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20  CT statement.   
201c8 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 74  Modify.** each t
201c9 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52  erm of the ORDER
201ca 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 20   BY clause is a 
201cb 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72  constant integer
201cc 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 6e   between 1.** an
201cd 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74  d N where N is t
201ce 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
201cf 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d 70  umns in the comp
201d0 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a  ound SELECT..**.
201d1 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  ** ORDER BY term
201d2 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 65 61  s that are alrea
201d3 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65  dy an integer be
201d4 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 72  tween 1 and N ar
201d5 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 2e  e.** unmodified.
201d6 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73    ORDER BY terms
201d7 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 67 65   that are intege
201d8 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 20 72  rs outside the r
201d9 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 72  ange of.** 1 thr
201da 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 65 20  ough N generate 
201db 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 52  an error.  ORDER
201dc 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61   BY terms that a
201dd 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a  re expressions.*
201de 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 61 67  * are matched ag
201df 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74  ainst result set
201e0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   expressions of 
201e1 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 0a  compound SELECT.
201e2 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  ** beginning wit
201e3 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  h the left-most 
201e4 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b 69  SELECT and worki
201e5 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 72 69  ng toward the ri
201e6 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 66  ght..** At the f
201e7 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 65 20  irst match, the 
201e8 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73 73  ORDER BY express
201e9 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 6d  ion is transform
201ea 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 69  ed into.** the i
201eb 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75  nteger column nu
201ec 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  mber..**.** Retu
201ed 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
201ee 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a 2f   errors seen..*/
201ef 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
201f0 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72  lveCompoundOrder
201f1 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  By(.  Parse *pPa
201f2 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
201f3 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20  arsing context. 
201f4 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73   Leave error mes
201f5 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20  sages here */.  
201f6 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
201f7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
201f8 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  ECT statement co
201f9 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 44  ntaining the ORD
201fa 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ER BY */.){.  in
201fb 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 20  t i;.  ExprList 
201fc 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 70  *pOrderBy;.  Exp
201fd 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20  rList *pEList;. 
201fe 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
201ff 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31  int moreToDo = 1
20200 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20  ;..  pOrderBy = 
20201 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 42  pSelect->pOrderB
20202 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  y;.  if( pOrderB
20203 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  y==0 ) return 0;
20204 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
20205 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  db;.#if SQLITE_M
20206 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20  AX_COLUMN.  if( 
20207 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e  pOrderBy->nExpr>
20208 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
20209 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20  E_LIMIT_COLUMN] 
2020a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2020b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2020c 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69  too many terms i
2020d 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
2020e 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  e");.    return 
2020f 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  1;.  }.#endif.  
20210 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65  for(i=0; i<pOrde
20211 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  rBy->nExpr; i++)
20212 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e  {.    pOrderBy->
20213 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a 20  a[i].done = 0;. 
20214 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 4e   }.  pSelect->pN
20215 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  ext = 0;.  while
20216 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  ( pSelect->pPrio
20217 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 74  r ){.    pSelect
20218 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 20  ->pPrior->pNext 
20219 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70  = pSelect;.    p
2021a 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74  Select = pSelect
2021b 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20  ->pPrior;.  }.  
2021c 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 26  while( pSelect &
2021d 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 20  & moreToDo ){.  
2021e 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2021f 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
20220 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 3b     moreToDo = 0;
20221 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 53  .    pEList = pS
20222 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20  elect->pEList;. 
20223 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73     assert( pELis
20224 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28  t!=0 );.    for(
20225 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65  i=0, pItem=pOrde
20226 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72  rBy->a; i<pOrder
20227 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  By->nExpr; i++, 
20228 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
20229 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20  int iCol = -1;. 
2022a 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20 2a       Expr *pE, *
2022b 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20  pDup;.      if( 
2022c 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 6f  pItem->done ) co
2022d 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45  ntinue;.      pE
2022e 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b   = pItem->pExpr;
2022f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
20230 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
20231 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20  pE, &iCol) ){.  
20232 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 3d        if( iCol<=
20233 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 74  0 || iCol>pEList
20234 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20  ->nExpr ){.     
20235 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f       resolveOutO
20236 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72  fRangeError(pPar
20237 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 2b 31  se, "ORDER", i+1
20238 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
20239 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
2023a 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 1;.        }.
2023b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2023c 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f       iCol = reso
2023d 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65  lveAsName(pParse
2023e 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20  , pEList, pE);. 
2023f 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3d         if( iCol=
20240 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
20241 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
20242 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20 30 29  prDup(db, pE, 0)
20243 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
20244 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
20245 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  d ){.           
20246 20 61 73 73 65 72 74 28 70 44 75 70 29 3b 0a 20   assert(pDup);. 
20247 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c 20             iCol 
20248 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 79  = resolveOrderBy
20249 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 70  TermToExprList(p
2024a 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
2024b 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20  pDup);.         
2024c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c   }.          sql
2024d 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2024e 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20  b, pDup);.      
2024f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
20250 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b 0a    if( iCol>0 ){.
20251 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
20252 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 6f  *pColl = pE->pCo
20253 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ll;.        int 
20254 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 67  flags = pE->flag
20255 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
20256 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e;.        sqlit
20257 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
20258 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70 49   pE);.        pI
20259 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 20  tem->pExpr = pE 
2025a 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  = sqlite3Expr(db
2025b 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 29  , TK_INTEGER, 0)
2025c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45  ;.        if( pE
2025d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==0 ) return 1;.
2025e 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f 6c          pE->pCol
2025f 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  l = pColl;.     
20260 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d 20     pE->flags |= 
20261 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66 6c  EP_IntValue | fl
20262 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 45 2d  ags;.        pE-
20263 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 43 6f 6c  >u.iValue = iCol
20264 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d  ;.        pItem-
20265 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f  >iCol = (u16)iCo
20266 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d  l;.        pItem
20267 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20  ->done = 1;.    
20268 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
20269 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 20   moreToDo = 1;. 
2026a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2026b 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65   pSelect = pSele
2026c 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20  ct->pNext;.  }. 
2026d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64   for(i=0; i<pOrd
2026e 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  erBy->nExpr; i++
2026f 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65  ){.    if( pOrde
20270 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d 3d  rBy->a[i].done==
20271 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
20272 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
20273 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59 20  e, "%r ORDER BY 
20274 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d 61  term does not ma
20275 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20 20  tch any ".      
20276 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69 6e        "column in
20277 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 22   the result set"
20278 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72 65  , i+1);.      re
20279 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2027a 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2027b 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76 65  ./*.** Check eve
2027c 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 4f  ry term in the O
2027d 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
2027e 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64 65   BY clause pOrde
2027f 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53 45  rBy of.** the SE
20280 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70  LECT statement p
20281 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79 20  Select.  If any 
20282 74 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e 63  term is referenc
20283 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c 74  e to a.** result
20284 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20   set expression 
20285 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62  (as determined b
20286 79 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61  y the ExprList.a
20287 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a 20  .iCol field).** 
20288 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 61  then convert tha
20289 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63 6f  t term into a co
2028a 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73  py of the corres
2028b 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20 73  ponding result s
2028c 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a  et.** column..**
2028d 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72  .** If any error
2028e 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20  s are detected, 
2028f 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  add an error mes
20290 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 20 61  sage to pParse a
20291 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e  nd.** return non
20292 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a  -zero.  Return z
20293 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72 73  ero if no errors
20294 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51   are seen..*/.SQ
20295 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
20296 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f   sqlite3ResolveO
20297 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 50  rderGroupBy(.  P
20298 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
20299 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2029a 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20  context.  Leave 
2029b 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68  error messages h
2029c 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ere */.  Select 
2029d 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f  *pSelect,      /
2029e 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
2029f 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e  tement containin
202a0 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a  g the clause */.
202a1 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
202a2 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20 4f  erBy,   /* The O
202a3 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
202a4 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62 65   BY clause to be
202a5 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20   processed */.  
202a6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70  const char *zTyp
202a7 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22  e     /* "ORDER"
202a8 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 29   or "GROUP" */.)
202a9 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c  {.  int i;.  sql
202aa 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
202ab 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69 73  e->db;.  ExprLis
202ac 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72  t *pEList;.  str
202ad 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
202ae 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28  m *pItem;..  if(
202af 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20   pOrderBy==0 || 
202b0 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
202b1 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
202b2 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  n 0;.#if SQLITE_
202b3 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28  MAX_COLUMN.  if(
202b4 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
202b5 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
202b6 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
202b7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
202b8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
202b9 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20  "too many terms 
202ba 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65 22  in %s BY clause"
202bb 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72 65  , zType);.    re
202bc 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64  turn 1;.  }.#end
202bd 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53  if.  pEList = pS
202be 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20  elect->pEList;. 
202bf 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
202c0 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65  =0 );  /* sqlite
202c1 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75 61  3SelectNew() gua
202c2 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f 0a  rantees this */.
202c3 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d    for(i=0, pItem
202c4 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c  =pOrderBy->a; i<
202c5 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
202c6 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
202c7 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
202c8 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Col ){.      if(
202c9 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45 4c   pItem->iCol>pEL
202ca 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20  ist->nExpr ){.  
202cb 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74        resolveOut
202cc 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61  OfRangeError(pPa
202cd 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c  rse, zType, i+1,
202ce 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b   pEList->nExpr);
202cf 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
202d0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
202d1 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50   resolveAlias(pP
202d2 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 49  arse, pEList, pI
202d3 74 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49 74  tem->iCol-1, pIt
202d4 65 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 70 65  em->pExpr, zType
202d5 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
202d6 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
202d7 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e  * pOrderBy is an
202d8 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
202d9 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20  UP BY clause in 
202da 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
202db 20 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 68 65   pSelect..** The
202dc 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66   Name context of
202dd 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
202de 65 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 20 7a  ement is pNC.  z
202df 54 79 70 65 20 69 73 20 65 69 74 68 65 72 0a 2a  Type is either.*
202e0 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52  * "ORDER" or "GR
202e1 4f 55 50 22 20 64 65 70 65 6e 64 69 6e 67 20 6f  OUP" depending o
202e2 6e 20 77 68 69 63 68 20 74 79 70 65 20 6f 66 20  n which type of 
202e3 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 20  clause pOrderBy 
202e4 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  is..**.** This r
202e5 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73 20  outine resolves 
202e6 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65  each term of the
202e7 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e 20   clause into an 
202e8 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 49  expression..** I
202e9 66 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74  f the order-by t
202ea 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65  erm is an intege
202eb 72 20 49 20 62 65 74 77 65 65 6e 20 31 20 61 6e  r I between 1 an
202ec 64 20 4e 20 28 77 68 65 72 65 20 4e 20 69 73 20  d N (where N is 
202ed 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  the.** number of
202ee 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
202ef 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
202f0 65 20 53 45 4c 45 43 54 29 20 74 68 65 6e 20 74  e SELECT) then t
202f1 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
202f2 20 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 74 69   in the resoluti
202f3 6f 6e 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  on is a copy of 
202f4 74 68 65 20 49 2d 74 68 20 72 65 73 75 6c 74 2d  the I-th result-
202f5 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  set expression. 
202f6 20 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 65 72   If.** the order
202f7 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69  -by term is an i
202f8 64 65 6e 74 69 66 79 20 74 68 61 74 20 63 6f 72  dentify that cor
202f9 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20  responds to the 
202fa 41 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61 20  AS-name of.** a 
202fb 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65  result-set expre
202fc 73 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20  ssion, then the 
202fd 74 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 74 6f  term resolves to
202fe 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a   a copy of the.*
202ff 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70  * result-set exp
20300 72 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72 77  ression.  Otherw
20301 69 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73  ise, the express
20302 69 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 64 20  ion is resolved 
20303 69 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c 20  in.** the usual 
20304 77 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c 69  way - using sqli
20305 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
20306 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  mes()..**.** Thi
20307 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
20308 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
20309 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72 6f  errors.  If erro
2030a 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0a 2a  rs occur, then.*
2030b 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  * an appropriate
2030c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6d   error message m
2030d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69 6e 20  ight be left in 
2030e 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 65 72  pParse.  (OOM er
2030f 72 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 65 64  rors.** excepted
20310 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  .).*/.static int
20311 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f   resolveOrderGro
20312 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f 6e 74  upBy(.  NameCont
20313 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 2f 2a  ext *pNC,     /*
20314 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78   The name contex
20315 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  t of the SELECT 
20316 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53  statement */.  S
20317 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
20318 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
20319 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68 6f 6c  CT statement hol
2031a 64 69 6e 67 20 70 4f 72 64 65 72 42 79 20 2a 2f  ding pOrderBy */
2031b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
2031c 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20 4f  derBy,   /* An O
2031d 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
2031e 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 72 65   BY clause to re
2031f 73 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  solve */.  const
20320 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20   char *zType    
20321 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44 45   /* Either "ORDE
20322 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20 61  R" or "GROUP", a
20323 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f  s appropriate */
20324 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
20325 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20326 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
20327 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 43  nter */.  int iC
20328 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
20329 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
2032a 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73  mn number */.  s
2032b 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2032c 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 2f 2a  tem *pItem;   /*
2032d 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f   A term of the O
2032e 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
2032f 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  /.  Parse *pPars
20330 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
20331 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
20332 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  ntext */.  int n
20333 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20  Result;         
20334 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
20335 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
20336 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
20337 2f 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  /..  if( pOrderB
20338 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  y==0 ) return 0;
20339 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 53 65  .  nResult = pSe
2033a 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  lect->pEList->nE
2033b 78 70 72 3b 0a 20 20 70 50 61 72 73 65 20 3d 20  xpr;.  pParse = 
2033c 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 66  pNC->pParse;.  f
2033d 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f  or(i=0, pItem=pO
2033e 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72  rderBy->a; i<pOr
2033f 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b  derBy->nExpr; i+
20340 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
20341 20 45 78 70 72 20 2a 70 45 20 3d 20 70 49 74 65   Expr *pE = pIte
20342 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 43  m->pExpr;.    iC
20343 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61  ol = resolveAsNa
20344 6d 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  me(pParse, pSele
20345 63 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 29 3b  ct->pEList, pE);
20346 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20  .    if( iCol>0 
20347 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61  ){.      /* If a
20348 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 20  n AS-name match 
20349 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 74  is found, mark t
2034a 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 6f 6c  his ORDER BY col
2034b 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 20  umn as being.   
2034c 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20     ** a copy of 
2034d 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75  the iCol-th resu
2034e 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 20  lt-set column.  
2034f 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 63  The subsequent c
20350 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  all to.      ** 
20351 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
20352 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c  derGroupBy() wil
20353 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78  l convert the ex
20354 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 20  pression to a.  
20355 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74      ** copy of t
20356 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c  he iCol-th resul
20357 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
20358 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d  . */.      pItem
20359 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43  ->iCol = (u16)iC
2035a 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e  ol;.      contin
2035b 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ue;.    }.    if
2035c 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
2035d 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c  nteger(pE, &iCol
2035e 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  ) ){.      /* Th
2035f 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20  e ORDER BY term 
20360 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  is an integer co
20361 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c 20  nstant.  Again, 
20362 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20  set the column. 
20363 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 73       ** number s
20364 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 52 65  o that sqlite3Re
20365 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
20366 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74  y() will convert
20367 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72   the.      ** or
20368 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 61  der-by term to a
20369 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73   copy of the res
2036a 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69  ult-set expressi
2036b 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  on */.      if( 
2036c 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 20  iCol<1 ){.      
2036d 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61    resolveOutOfRa
2036e 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c  ngeError(pParse,
2036f 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 65   zType, i+1, nRe
20370 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 72  sult);.        r
20371 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
20372 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43  .      pItem->iC
20373 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a  ol = (u16)iCol;.
20374 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
20375 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 74      }..    /* Ot
20376 68 65 72 77 69 73 65 2c 20 74 72 65 61 74 20 74  herwise, treat t
20377 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  he ORDER BY term
20378 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20   as an ordinary 
20379 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
2037a 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20    pItem->iCol = 
2037b 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  0;.    if( sqlit
2037c 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
2037d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a 20  es(pNC, pE) ){. 
2037e 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2037f 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
20380 6e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  n sqlite3Resolve
20381 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61  OrderGroupBy(pPa
20382 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 4f  rse, pSelect, pO
20383 72 64 65 72 42 79 2c 20 7a 54 79 70 65 29 3b 0a  rderBy, zType);.
20384 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65  }../*.** Resolve
20385 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 53 45   names in the SE
20386 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70  LECT statement p
20387 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20   and all of its 
20388 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a  descendents..*/.
20389 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c  static int resol
2038a 76 65 53 65 6c 65 63 74 53 74 65 70 28 57 61 6c  veSelectStep(Wal
2038b 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65  ker *pWalker, Se
2038c 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d 65  lect *p){.  Name
2038d 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e  Context *pOuterN
2038e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 74  C;  /* Context t
2038f 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69  hat contains thi
20390 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e 61  s SELECT */.  Na
20391 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
20392 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f        /* Name co
20393 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 53 45  ntext of this SE
20394 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 73  LECT */.  int is
20395 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20  Compound;       
20396 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 20 69    /* True if p i
20397 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  s a compound sel
20398 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ect */.  int nCo
20399 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20  mpound;         
2039a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
2039b 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 6f  mpound terms pro
2039c 63 65 73 73 65 64 20 73 6f 20 66 61 72 20 2a 2f  cessed so far */
2039d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2039e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ;          /* Pa
2039f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
203a0 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
203a1 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  ist;       /* Re
203a2 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73  sult set express
203a3 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  ion list */.  in
203a4 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
203a5 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
203a6 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c  unter */.  ExprL
203a7 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20  ist *pGroupBy;  
203a8 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20     /* The GROUP 
203a9 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53  BY clause */.  S
203aa 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 74  elect *pLeftmost
203ab 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d 6d  ;      /* Left-m
203ac 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 20 6f 66  ost of SELECT of
203ad 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20   a compound */. 
203ae 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
203af 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
203b0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
203b1 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 28  */.  ..  assert(
203b2 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70   p!=0 );.  if( p
203b3 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
203b4 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 20  Resolved ){.    
203b5 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65  return WRC_Prune
203b6 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e 43  ;.  }.  pOuterNC
203b7 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e   = pWalker->u.pN
203b8 43 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57  C;.  pParse = pW
203b9 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20  alker->pParse;. 
203ba 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
203bb 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79  ;..  /* Normally
203bc 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
203bd 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 63  pand() will be c
203be 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e 64 20  alled first and 
203bf 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61  will have.  ** a
203c0 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65 64 20  lready expanded 
203c1 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20 48 6f  this SELECT.  Ho
203c2 77 65 76 65 72 2c 20 69 66 20 74 68 69 73 20 69  wever, if this i
203c3 73 20 61 20 73 75 62 71 75 65 72 79 20 77 69 74  s a subquery wit
203c4 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 72  hin.  ** an expr
203c5 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 52  ession, sqlite3R
203c6 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
203c7 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64  ) will be called
203c8 20 77 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a 20   without a.  ** 
203c9 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71  prior call to sq
203ca 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e  lite3SelectExpan
203cb 64 28 29 2e 20 20 57 68 65 6e 20 74 68 61 74 20  d().  When that 
203cc 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 2a  happens, let.  *
203cd 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50  * sqlite3SelectP
203ce 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 20  rep() do all of 
203cf 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66  the processing f
203d0 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 2e 0a  or this SELECT..
203d1 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65    ** sqlite3Sele
203d2 63 74 50 72 65 70 28 29 20 77 69 6c 6c 20 69 6e  ctPrep() will in
203d3 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 65  voke both sqlite
203d4 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20  3SelectExpand() 
203d5 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 6f  and.  ** this ro
203d6 75 74 69 6e 65 20 69 6e 20 74 68 65 20 63 6f 72  utine in the cor
203d7 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a 2f  rect order..  */
203d8 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c  .  if( (p->selFl
203d9 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65  ags & SF_Expande
203da 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  d)==0 ){.    sql
203db 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 70  ite3SelectPrep(p
203dc 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72  Parse, p, pOuter
203dd 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  NC);.    return 
203de 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c  (pParse->nErr ||
203df 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
203e0 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a  d) ? WRC_Abort :
203e1 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a   WRC_Prune;.  }.
203e2 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d 20  .  isCompound = 
203e3 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 20  p->pPrior!=0;.  
203e4 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a 20  nCompound = 0;. 
203e5 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b 0a   pLeftmost = p;.
203e6 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20    while( p ){.  
203e7 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65    assert( (p->se
203e8 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61  lFlags & SF_Expa
203e9 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 20  nded)!=0 );.    
203ea 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46  assert( (p->selF
203eb 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76  lags & SF_Resolv
203ec 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d  ed)==0 );.    p-
203ed 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
203ee 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 2f  Resolved;..    /
203ef 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78  * Resolve the ex
203f0 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
203f1 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
203f2 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65 73 65  T clauses. These
203f3 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20  .    ** are not 
203f4 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72  allowed to refer
203f5 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 73   to any names, s
203f6 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74 79 20  o pass an empty 
203f7 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 20  NameContext..   
203f8 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26   */.    memset(&
203f9 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
203fa 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50  NC));.    sNC.pP
203fb 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
203fc 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
203fd 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
203fe 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20  sNC, p->pLimit) 
203ff 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ||.        sqlit
20400 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
20401 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 66  es(&sNC, p->pOff
20402 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  set) ){.      re
20403 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
20404 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
20405 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c  Set up the local
20406 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f   name-context to
20407 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74 65 33   pass to sqlite3
20408 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
20409 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 73  () to.    ** res
2040a 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c 74 2d  olve the result-
2040b 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  set expression l
2040c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ist..    */.    
2040d 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31  sNC.allowAgg = 1
2040e 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69  ;.    sNC.pSrcLi
2040f 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20  st = p->pSrc;.  
20410 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4f    sNC.pNext = pO
20411 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20 2f  uterNC;.  .    /
20412 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20  * Resolve names 
20413 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
20414 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73 74  t. */.    pEList
20415 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20   = p->pEList;.  
20416 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
20417 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69  !=0 );.    for(i
20418 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; i<pEList->nE
20419 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
2041a 20 45 78 70 72 20 2a 70 58 20 3d 20 70 45 4c 69   Expr *pX = pELi
2041b 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  st->a[i].pExpr;.
2041c 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2041d 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2041e 73 28 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a 20  s(&sNC, pX) ){. 
2041f 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52         return WR
20420 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d  C_Abort;.      }
20421 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
20422 20 52 65 63 75 72 73 69 76 65 6c 79 20 72 65 73   Recursively res
20423 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c  olve names in al
20424 6c 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 20  l subqueries.   
20425 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
20426 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63   i<p->pSrc->nSrc
20427 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74  ; i++){.      st
20428 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
20429 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70  m *pItem = &p->p
2042a 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20  Src->a[i];.     
2042b 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c   if( pItem->pSel
2042c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ect ){.        c
2042d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65  onst char *zSave
2042e 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73  dContext = pPars
2042f 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b  e->zAuthContext;
20430 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74  .        if( pIt
20431 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61 72  em->zName ) pPar
20432 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
20433 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b   = pItem->zName;
20434 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
20435 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d  ResolveSelectNam
20436 65 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d  es(pParse, pItem
20437 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75 74 65  ->pSelect, pOute
20438 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 70 50  rNC);.        pP
20439 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
2043a 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74 65  xt = zSavedConte
2043b 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  xt;.        if( 
2043c 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
2043d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2043e 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
2043f 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ort;.      }.   
20440 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20   }.  .    /* If 
20441 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 67 67  there are no agg
20442 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
20443 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73   in the result-s
20444 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50  et, and no GROUP
20445 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78 70 72   BY .    ** expr
20446 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 61  ession, do not a
20447 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 73 20  llow aggregates 
20448 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74  in any of the ot
20449 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 2e  her expressions.
2044a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
2044b 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  rt( (p->selFlags
2044c 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 29   & SF_Aggregate)
2044d 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72 6f 75  ==0 );.    pGrou
2044e 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42  pBy = p->pGroupB
2044f 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75  y;.    if( pGrou
20450 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 67  pBy || sNC.hasAg
20451 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73 65  g ){.      p->se
20452 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 67  lFlags |= SF_Agg
20453 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c 73  regate;.    }els
20454 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c 6c  e{.      sNC.all
20455 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d  owAgg = 0;.    }
20456 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 20  .  .    /* If a 
20457 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 69 73  HAVING clause is
20458 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74   present, then t
20459 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 47  here must be a G
2045a 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a  ROUP BY clause..
2045b 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
2045c 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 70 47  ->pHaving && !pG
2045d 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20  roupBy ){.      
2045e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2045f 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f 55 50  pParse, "a GROUP
20460 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 72 65   BY clause is re
20461 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 48 41  quired before HA
20462 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 65  VING");.      re
20463 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
20464 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
20465 41 64 64 20 74 68 65 20 65 78 70 72 65 73 73 69  Add the expressi
20466 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 20 6e  on list to the n
20467 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 6f  ame-context befo
20468 72 65 20 70 61 72 73 69 6e 67 20 74 68 65 0a 20  re parsing the. 
20469 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 72     ** other expr
2046a 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53  essions in the S
2046b 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
2046c 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74   This is so that
2046d 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69  .    ** expressi
2046e 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  ons in the WHERE
2046f 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29 20 63   clause (etc.) c
20470 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78 70 72  an refer to expr
20471 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 2a  essions by.    *
20472 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74 68 65  * aliases in the
20473 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 20   result set..   
20474 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f 72   **.    ** Minor
20475 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20   point: If this 
20476 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65  is the case, the
20477 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
20478 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20   will be.    ** 
20479 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72  re-evaluated for
2047a 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 65 20   each reference 
2047b 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  to it..    */.  
2047c 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 70    sNC.pEList = p
2047d 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 66  ->pEList;.    if
2047e 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
2047f 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
20480 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 20  p->pWhere) ||.  
20481 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f       sqlite3Reso
20482 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
20483 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a 20  C, p->pHaving). 
20484 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75     ){.      retu
20485 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
20486 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20    }..    /* The 
20487 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47 52 4f  ORDER BY and GRO
20488 55 50 20 42 59 20 63 6c 61 75 73 65 73 20 6d 61  UP BY clauses ma
20489 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 74  y not refer to t
2048a 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 6f  erms in.    ** o
2048b 75 74 65 72 20 71 75 65 72 69 65 73 20 0a 20 20  uter queries .  
2048c 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e 65    */.    sNC.pNe
2048d 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 2e  xt = 0;.    sNC.
2048e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a 20  allowAgg = 1;.. 
2048f 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68     /* Process th
20490 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
20491 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e 20  e for singleton 
20492 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
20493 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f 52  s..    ** The OR
20494 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f  DER BY clause fo
20495 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c 45  r compounds SELE
20496 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73  CT statements is
20497 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a 20   handled.    ** 
20498 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c 6c  below, after all
20499 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73   of the result-s
2049a 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74  ets for all of t
2049b 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a 20  he elements of. 
2049c 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75     ** the compou
2049d 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65 73  nd have been res
2049e 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  olved..    */.  
2049f 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f 75 6e    if( !isCompoun
204a0 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72 64 65  d && resolveOrde
204a1 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70  rGroupBy(&sNC, p
204a2 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22  , p->pOrderBy, "
204a3 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20 20 20  ORDER") ){.     
204a4 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
204a5 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
204a6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
204a7 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  d ){.      retur
204a8 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
204a9 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73   }.  .    /* Res
204aa 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50 20 42  olve the GROUP B
204ab 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20 74 68  Y clause.  At th
204ac 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61 6b  e same time, mak
204ad 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a 20 74  e sure .    ** t
204ae 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
204af 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74  se does not cont
204b0 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 66 75  ain aggregate fu
204b1 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a  nctions..    */.
204b2 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79      if( pGroupBy
204b3 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74   ){.      struct
204b4 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
204b5 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20 20  pItem;.    .    
204b6 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 64    if( resolveOrd
204b7 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20  erGroupBy(&sNC, 
204b8 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 47 52  p, pGroupBy, "GR
204b9 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c  OUP") || db->mal
204ba 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
204bb 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
204bc 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20  Abort;.      }. 
204bd 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49       for(i=0, pI
204be 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b  tem=pGroupBy->a;
204bf 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78   i<pGroupBy->nEx
204c0 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
204c1 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45  ){.        if( E
204c2 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
204c3 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50 5f  Item->pExpr, EP_
204c4 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Agg) ){.        
204c5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
204c6 67 28 70 50 61 72 73 65 2c 20 22 61 67 67 72 65  g(pParse, "aggre
204c7 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  gate functions a
204c8 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69  re not allowed i
204c9 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  n ".            
204ca 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 59 20    "the GROUP BY 
204cb 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 20 20  clause");.      
204cc 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
204cd 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  bort;.        }.
204ce 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
204cf 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f     /* Advance to
204d0 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20 6f   the next term o
204d1 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a 20  f the compound. 
204d2 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 2d     */.    p = p-
204d3 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43 6f  >pPrior;.    nCo
204d4 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a 20  mpound++;.  }.. 
204d5 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20   /* Resolve the 
204d6 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20 63 6f  ORDER BY on a co
204d7 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 66  mpound SELECT af
204d8 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66  ter all terms of
204d9 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75  .  ** the compou
204da 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65 73  nd have been res
204db 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  olved..  */.  if
204dc 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20  ( isCompound && 
204dd 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f  resolveCompoundO
204de 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20 70  rderBy(pParse, p
204df 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20 20  Leftmost) ){.   
204e0 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
204e1 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  t;.  }..  return
204e2 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a 2f   WRC_Prune;.}../
204e3 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
204e4 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 72 65  e walks an expre
204e5 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 72  ssion tree and r
204e6 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 6e 63  esolves referenc
204e7 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 63  es to.** table c
204e8 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 6c  olumns and resul
204e9 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 20  t-set columns.  
204ea 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  At the same time
204eb 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63 68  , do error.** ch
204ec 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 69  ecking on functi
204ed 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73 65 74  on usage and set
204ee 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79 20 61   a flag if any a
204ef 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
204f0 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e 0a  ns.** are seen..
204f1 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65  **.** To resolve
204f2 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 72   table columns r
204f3 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c 6f 6f  eferences we loo
204f4 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72 20  k for nodes (or 
204f5 73 75 62 74 72 65 65 73 29 20 6f 66 20 74 68 65  subtrees) of the
204f6 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a 20   .** form X.Y.Z 
204f7 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a  or Y.Z or just Z
204f8 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20 20   where.**.**    
204f9 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d 65 20    X:   The name 
204fa 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 20 20  of a database.  
204fb 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20 22  Ex:  "main" or "
204fc 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20 20  temp" or.**     
204fd 20 20 20 20 20 20 74 68 65 20 73 79 6d 62 6f 6c        the symbol
204fe 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64  ic name assigned
204ff 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d 65 64   to an ATTACH-ed
20500 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
20501 20 20 20 20 20 20 59 3a 20 20 20 54 68 65 20 6e        Y:   The n
20502 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20 69  ame of a table i
20503 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  n a FROM clause.
20504 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67 67 65    Or in a trigge
20505 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f  r.**           o
20506 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 61  ne of the specia
20507 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f 72  l names "old" or
20508 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20 20   "new"..**.**   
20509 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d 65     Z:   The name
2050a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   of a column in 
2050b 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20 54  table Y..**.** T
2050c 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65 20 72  he node at the r
2050d 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62 74 72  oot of the subtr
2050e 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 61  ee is modified a
2050f 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
20510 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20 20 20      Expr.op     
20511 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20 54 4b     Changed to TK
20512 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45 78  _COLUMN.**    Ex
20513 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 6f 69  pr.pTab      Poi
20514 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65  nts to the Table
20515 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 0a   object for X.Y.
20516 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f 6c 75  **    Expr.iColu
20517 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20  mn   The column 
20518 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 20 2d  index in X.Y.  -
20519 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e  1 for the rowid.
2051a 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54 61 62  .**    Expr.iTab
2051b 6c 65 20 20 20 20 54 68 65 20 56 44 42 45 20 63  le    The VDBE c
2051c 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
2051d 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 6f   X.Y.**.**.** To
2051e 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c 74 2d   resolve result-
2051f 73 65 74 20 72 65 66 65 72 65 6e 63 65 73 2c 20  set references, 
20520 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65 73 73  look for express
20521 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68 65  ion nodes of the
20522 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74 68  .** form Z (with
20523 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72 65 66   no X and Y pref
20524 69 78 29 20 77 68 65 72 65 20 74 68 65 20 5a 20  ix) where the Z 
20525 6d 61 74 63 68 65 73 20 74 68 65 20 72 69 67 68  matches the righ
20526 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20 6f  t-hand.** size o
20527 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65 20 69  f an AS clause i
20528 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  n the result-set
20529 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20 20 54   of a SELECT.  T
2052a 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f 6e 0a  he Z expression.
2052b 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64 20 62  ** is replaced b
2052c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  y a copy of the 
2052d 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
2052e 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  f the result-set
2052f 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20   expression..** 
20530 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20 66  Table-name and f
20531 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74 69  unction resoluti
20532 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65  on occurs on the
20533 20 73 75 62 73 74 69 74 75 74 65 64 20 65 78 70   substituted exp
20534 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e  ression.** tree.
20535 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69    For example, i
20536 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45  n:.**.**      SE
20537 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63  LECT a+b AS x, c
20538 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20  +d AS y FROM t1 
20539 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a 2a  ORDER BY x;.**.*
2053a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d 20 6f  * The "x" term o
2053b 66 20 74 68 65 20 6f 72 64 65 72 20 62 79 20 69  f the order by i
2053c 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 22 61  s replaced by "a
2053d 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a 2a  +b" to render:.*
2053e 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54  *.**      SELECT
2053f 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41   a+b AS x, c+d A
20540 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45  S y FROM t1 ORDE
20541 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a 20  R BY a+b;.**.** 
20542 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 61  Function calls a
20543 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20 6d 61  re checked to ma
20544 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
20545 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a 2a   function is .**
20546 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74 68 61   defined and tha
20547 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75  t the correct nu
20548 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
20549 73 20 61 72 65 20 73 70 65 63 69 66 69 65 64 2e  s are specified.
2054a 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74  .** If the funct
2054b 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72 65 67  ion is an aggreg
2054c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  ate function, th
2054d 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61 73 41  en the pNC->hasA
2054e 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e 64  gg is.** set and
2054f 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 63   the opcode is c
20550 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f 46  hanged from TK_F
20551 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41 47  UNCTION to TK_AG
20552 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 49  G_FUNCTION..** I
20553 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  f an expression 
20554 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61  contains aggrega
20555 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 65  te functions the
20556 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a 20  n the EP_Agg.** 
20557 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 65 20  property on the 
20558 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65  expression is se
20559 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f  t..**.** An erro
2055a 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65 66  r message is lef
2055b 74 20 69 6e 20 70 50 61 72 73 65 20 69 66 20 61  t in pParse if a
2055c 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 73 73  nything is amiss
2055d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a  .  The number.**
2055e 20 69 66 20 65 72 72 6f 72 73 20 69 73 20 72 65   if errors is re
2055f 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
20560 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20561 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
20562 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 6f  Names( .  NameCo
20563 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20  ntext *pNC,     
20564 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 74    /* Namespace t
20565 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73  o resolve expres
20566 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 45  sions in. */.  E
20567 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20  xpr *pExpr      
20568 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
20569 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61  pression to be a
2056a 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a 20  nalyzed. */.){. 
2056b 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 67 67   int savedHasAgg
2056c 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20  ;.  Walker w;.. 
2056d 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
2056e 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51  return 0;.#if SQ
2056f 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
20570 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50 61  PTH>0.  {.    Pa
20571 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 4e  rse *pParse = pN
20572 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 69  C->pParse;.    i
20573 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 68  f( sqlite3ExprCh
20574 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 65  eckHeight(pParse
20575 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74  , pExpr->nHeight
20576 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48  +pNC->pParse->nH
20577 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  eight) ){.      
20578 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
20579 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69      pParse->nHei
2057a 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e 48  ght += pExpr->nH
2057b 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 69  eight;.  }.#endi
2057c 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 67 20  f.  savedHasAgg 
2057d 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a 20  = pNC->hasAgg;. 
2057e 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 30   pNC->hasAgg = 0
2057f 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
20580 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70  ack = resolveExp
20581 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65  rStep;.  w.xSele
20582 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73  ctCallback = res
20583 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a  olveSelectStep;.
20584 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 4e 43    w.pParse = pNC
20585 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e  ->pParse;.  w.u.
20586 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71 6c  pNC = pNC;.  sql
20587 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c  ite3WalkExpr(&w,
20588 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53 51 4c   pExpr);.#if SQL
20589 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
2058a 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 72  TH>0.  pNC->pPar
2058b 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 70  se->nHeight -= p
2058c 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23  Expr->nHeight;.#
2058d 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e 43 2d  endif.  if( pNC-
2058e 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e 70 50 61  >nErr>0 || w.pPa
2058f 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20  rse->nErr>0 ){. 
20590 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
20591 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72  ty(pExpr, EP_Err
20592 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  or);.  }.  if( p
20593 4e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20 20  NC->hasAgg ){.  
20594 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
20595 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29  y(pExpr, EP_Agg)
20596 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 61  ;.  }else if( sa
20597 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20 20  vedHasAgg ){.   
20598 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31   pNC->hasAgg = 1
20599 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 45  ;.  }.  return E
2059a 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2059b 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b  Expr, EP_Error);
2059c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c  .}.../*.** Resol
2059d 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20  ve all names in 
2059e 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20  all expressions 
2059f 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64 20  of a SELECT and 
205a0 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e 64  in all.** decend
205a1 65 6e 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45  ents of the SELE
205a2 43 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 6f  CT, including co
205a3 6d 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20 70  mpounds off of p
205a4 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75 62  ->pPrior,.** sub
205a5 71 75 65 72 69 65 73 20 69 6e 20 65 78 70 72 65  queries in expre
205a6 73 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62 71  ssions, and subq
205a7 75 65 72 69 65 73 20 75 73 65 64 20 61 73 20 46  ueries used as F
205a8 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74 65  ROM clause.** te
205a9 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 73  rms..**.** See s
205aa 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
205ab 72 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 20 64  rNames() for a d
205ac 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
205ad 65 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74 72  e kinds of.** tr
205ae 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74 68  ansformations th
205af 61 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20  at occur..**.** 
205b0 41 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 74 65  All SELECT state
205b1 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 61 76  ments should hav
205b2 65 20 62 65 65 6e 20 65 78 70 61 6e 64 65 64 20  e been expanded 
205b3 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  using.** sqlite3
205b4 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 70  SelectExpand() p
205b5 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67  rior to invoking
205b6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
205b7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
205b8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73   void sqlite3Res
205b9 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28  olveSelectNames(
205ba 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
205bb 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
205bc 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
205bd 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
205be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
205bf 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
205c0 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e  ent being coded.
205c1 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
205c2 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20  t *pOuterNC  /* 
205c3 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  Name context for
205c4 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54 20 73   parent SELECT s
205c5 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20  tatement */.){. 
205c6 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61 73   Walker w;..  as
205c7 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20  sert( p!=0 );.  
205c8 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20  w.xExprCallback 
205c9 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65  = resolveExprSte
205ca 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  p;.  w.xSelectCa
205cb 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65  llback = resolve
205cc 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 2e  SelectStep;.  w.
205cd 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
205ce 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f 75  .  w.u.pNC = pOu
205cf 74 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33  terNC;.  sqlite3
205d0 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70  WalkSelect(&w, p
205d1 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
205d2 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 73  ***** End of res
205d3 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  olve.c *********
205d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205d6 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
205d7 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
205d8 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   expr.c ********
205d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205db 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
205dc 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
205dd 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
205de 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
205df 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
205e0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
205e1 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
205e2 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
205e3 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
205e4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
205e5 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
205e6 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
205e7 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
205e8 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
205e9 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
205ea 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
205eb 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
205ec 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
205ed 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
205ee 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
205ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
205f3 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
205f4 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64  ns routines used
205f5 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 65   for analyzing e
205f6 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a 2a  xpressions and.*
205f7 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e 67  * for generating
205f8 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20   VDBE code that 
205f9 65 76 61 6c 75 61 74 65 73 20 65 78 70 72 65 73  evaluates expres
205fa 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 2e  sions in SQLite.
205fb 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .*/../*.** Retur
205fc 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 79 27  n the 'affinity'
205fd 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
205fe 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e 79 2e  on pExpr if any.
205ff 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20  .**.** If pExpr 
20600 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20 72  is a column, a r
20601 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 63 6f  eference to a co
20602 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 53 27  lumn via an 'AS'
20603 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61 20   alias,.** or a 
20604 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 68 20  sub-select with 
20605 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20  a column as the 
20606 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 74 68  return value, th
20607 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 69 6e  en the .** affin
20608 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f 6c 75  ity of that colu
20609 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  mn is returned. 
2060a 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 30 20  Otherwise, 0x00 
2060b 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20  is returned,.** 
2060c 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 61 66  indicating no af
2060d 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 20 65  finity for the e
2060e 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
2060f 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 45 20   i.e. the WHERE 
20610 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 73 69  clause expresssi
20611 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ons in the follo
20612 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20  wing statements 
20613 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20 61  all.** have an a
20614 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20 43  ffinity:.**.** C
20615 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
20616 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46  );.** SELECT * F
20617 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3b 0a  ROM t1 WHERE a;.
20618 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 20 62  ** SELECT a AS b
20619 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 62   FROM t1 WHERE b
2061a 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 52  ;.** SELECT * FR
2061b 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 65 6c  OM t1 WHERE (sel
2061c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 3b 0a  ect a from t1);.
2061d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2061e 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 78  E char sqlite3Ex
2061f 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 20  prAffinity(Expr 
20620 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 6f  *pExpr){.  int o
20621 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20  p = pExpr->op;. 
20622 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45   if( op==TK_SELE
20623 43 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  CT ){.    assert
20624 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 26 45  ( pExpr->flags&E
20625 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b 0a 20  P_xIsSelect );. 
20626 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
20627 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45  3ExprAffinity(pE
20628 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e  xpr->x.pSelect->
20629 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2062a 70 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  pr);.  }.#ifndef
2062b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
2062c 54 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 43  T.  if( op==TK_C
2062d 41 53 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72  AST ){.    asser
2062e 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2062f 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e  rty(pExpr, EP_In
20630 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 72  tValue) );.    r
20631 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 66 66  eturn sqlite3Aff
20632 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 72 2d  inityType(pExpr-
20633 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d 0a  >u.zToken);.  }.
20634 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f 70  #endif.  if( (op
20635 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  ==TK_AGG_COLUMN 
20636 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  || op==TK_COLUMN
20637 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53   || op==TK_REGIS
20638 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78 70  TER) .   && pExp
20639 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b 0a  r->pTab!=0.  ){.
2063a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45      /* op==TK_RE
2063b 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 2d  GISTER && pExpr-
2063c 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73  >pTab!=0 happens
2063d 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20   when pExpr was 
2063e 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 2a  originally.    *
2063f 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75  * a TK_COLUMN bu
20640 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
20641 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63   evaluated and c
20642 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73  ached in a regis
20643 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a  ter */.    int j
20644 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
20645 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20 29  n;.    if( j<0 )
20646 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41   return SQLITE_A
20647 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  FF_INTEGER;.    
20648 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
20649 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d 3e  Tab && j<pExpr->
2064a 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
2064b 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d 3e    return pExpr->
2064c 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61 66  pTab->aCol[j].af
2064d 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72 65  finity;.  }.  re
2064e 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66 69  turn pExpr->affi
2064f 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  nity;.}../*.** S
20650 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  et the collating
20651 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 78   sequence for ex
20652 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 74  pression pExpr t
20653 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74 69  o be the collati
20654 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6e  ng.** sequence n
20655 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e 20  amed by pToken. 
20656 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
20657 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73 65  er to the revise
20658 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  d expression..**
20659 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   The collating s
2065a 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b 65  equence is marke
2065b 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22 20  d as "explicit" 
2065c 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78 70  using the EP_Exp
2065d 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67 2e  Collate.** flag.
2065e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63 6f    An explicit co
2065f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
20660 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20 69   will override i
20661 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c 61  mplicit.** colla
20662 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e 0a  ting sequences..
20663 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20664 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
20665 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65  xprSetColl(Parse
20666 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
20667 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 43  pExpr, Token *pC
20668 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72  ollName){.  char
20669 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 20   *zColl = 0;    
2066a 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f          /* Dequo
2066b 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c  ted name of coll
2066c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a  ation sequence *
2066d 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  /.  CollSeq *pCo
2066e 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ll;.  sqlite3 *d
2066f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
20670 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    zColl = sqlite
20671 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
20672 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a 20  b, pCollName);. 
20673 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a 43   if( pExpr && zC
20674 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c  oll ){.    pColl
20675 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
20676 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
20677 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  zColl);.    if( 
20678 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70  pColl ){.      p
20679 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43  Expr->pColl = pC
2067a 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78 70 72  oll;.      pExpr
2067b 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78  ->flags |= EP_Ex
2067c 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 7d 0a  pCollate;.    }.
2067d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
2067e 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a  ree(db, zColl);.
2067f 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 3b 0a    return pExpr;.
20680 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
20681 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c  the default coll
20682 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
20683 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  or the expressio
20684 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20 74  n pExpr. If.** t
20685 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66 61 75  here is no defau
20686 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70  lt collation typ
20687 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  e, return 0..*/.
20688 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
20689 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45  ollSeq *sqlite3E
2068a 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  xprCollSeq(Parse
2068b 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
2068c 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53 65  pExpr){.  CollSe
2068d 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20  q *pColl = 0;.  
2068e 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70 72 3b  Expr *p = pExpr;
2068f 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53  .  while( ALWAYS
20690 28 70 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f  (p) ){.    int o
20691 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70  p;.    pColl = p
20692 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28  ->pColl;.    if(
20693 20 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b 3b 0a   pColl ) break;.
20694 20 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a      op = p->op;.
20695 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62 21      if( p->pTab!
20696 3d 30 20 26 26 20 28 0a 20 20 20 20 20 20 20 20  =0 && (.        
20697 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  op==TK_AGG_COLUM
20698 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  N || op==TK_COLU
20699 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47  MN || op==TK_REG
2069a 49 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  ISTER || op==TK_
2069b 54 52 49 47 47 45 52 0a 20 20 20 20 29 29 7b 0a  TRIGGER.    )){.
2069c 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f        /* op==TK_
2069d 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e 70  REGISTER && p->p
2069e 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77  Tab!=0 happens w
2069f 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72  hen pExpr was or
206a0 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a  iginally.      *
206a1 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75  * a TK_COLUMN bu
206a2 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
206a3 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63   evaluated and c
206a4 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73  ached in a regis
206a5 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e  ter */.      con
206a6 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a  st char *zColl;.
206a7 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d        int j = p-
206a8 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
206a9 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20  if( j>=0 ){.    
206aa 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
206ab 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
206ac 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d        zColl = p-
206ad 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a  >pTab->aCol[j].z
206ae 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 43  Coll;.        pC
206af 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
206b0 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43  dCollSeq(db, ENC
206b1 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29 3b  (db), zColl, 0);
206b2 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
206b3 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20  pColl = pColl;. 
206b4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
206b5 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
206b6 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 26 26  ( op!=TK_CAST &&
206b7 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 29 7b   op!=TK_UPLUS ){
206b8 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
206b9 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70    }.    p = p->p
206ba 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Left;.  }.  if( 
206bb 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c  sqlite3CheckColl
206bc 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f 6c  Seq(pParse, pCol
206bd 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c 6c  l) ){ .    pColl
206be 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
206bf 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  rn pColl;.}../*.
206c0 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e 20 6f  ** pExpr is an o
206c1 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d 70  perand of a comp
206c2 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e  arison operator.
206c3 20 20 61 66 66 32 20 69 73 20 74 68 65 0a 2a 2a    aff2 is the.**
206c4 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 6f   type affinity o
206c5 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 65 72  f the other oper
206c6 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69  and.  This routi
206c7 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a  ne returns the.*
206c8 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20  * type affinity 
206c9 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75  that should be u
206ca 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d 70  sed for the comp
206cb 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e  arison operator.
206cc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
206cd 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 43  TE char sqlite3C
206ce 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 45  ompareAffinity(E
206cf 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72  xpr *pExpr, char
206d0 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 20 61   aff2){.  char a
206d1 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ff1 = sqlite3Exp
206d2 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 29  rAffinity(pExpr)
206d3 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 26 20  ;.  if( aff1 && 
206d4 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 42  aff2 ){.    /* B
206d5 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68 65  oth sides of the
206d6 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65 20   comparison are 
206d7 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65 20  columns. If one 
206d8 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 20 20  has numeric.    
206d9 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73 65  ** affinity, use
206da 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73 65   that. Otherwise
206db 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74 79   use no affinity
206dc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
206dd 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69   sqlite3IsNumeri
206de 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 29 20  cAffinity(aff1) 
206df 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65  || sqlite3IsNume
206e0 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 32  ricAffinity(aff2
206e1 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
206e2 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  n SQLITE_AFF_NUM
206e3 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ERIC;.    }else{
206e4 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
206e5 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20  LITE_AFF_NONE;. 
206e6 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
206e7 20 21 61 66 66 31 20 26 26 20 21 61 66 66 32 20   !aff1 && !aff2 
206e8 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 65  ){.    /* Neithe
206e9 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f  r side of the co
206ea 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 63 6f  mparison is a co
206eb 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20 74  lumn.  Compare t
206ec 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74  he.    ** result
206ed 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20 20  s directly..    
206ee 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
206ef 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20  LITE_AFF_NONE;. 
206f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4f   }else{.    /* O
206f1 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 6f 6c  ne side is a col
206f2 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 20 69  umn, the other i
206f3 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 20 63  s not. Use the c
206f4 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 2e  olumns affinity.
206f5 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
206f6 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32 3d  aff1==0 || aff2=
206f7 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  =0 );.    return
206f8 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b 0a   (aff1 + aff2);.
206f9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78    }.}../*.** pEx
206fa 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69 73  pr is a comparis
206fb 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 52 65  on operator.  Re
206fc 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 61 66  turn the type af
206fd 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75  finity that shou
206fe 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65 64  ld.** be applied
206ff 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e 64   to both operand
20700 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e 67  s prior to doing
20701 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e   the comparison.
20702 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
20703 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69  comparisonAffini
20704 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b  ty(Expr *pExpr){
20705 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 20 61  .  char aff;.  a
20706 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70  ssert( pExpr->op
20707 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70 72  ==TK_EQ || pExpr
20708 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 70  ->op==TK_IN || p
20709 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20  Expr->op==TK_LT 
2070a 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 78  ||.          pEx
2070b 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c 7c  pr->op==TK_GT ||
2070c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47   pExpr->op==TK_G
2070d 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  E || pExpr->op==
2070e 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20 20  TK_LE ||.       
2070f 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b     pExpr->op==TK
20710 5f 4e 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  _NE || pExpr->op
20711 3d 3d 54 4b 5f 49 53 20 7c 7c 20 70 45 78 70 72  ==TK_IS || pExpr
20712 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54 20 29  ->op==TK_ISNOT )
20713 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  ;.  assert( pExp
20714 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20 20 61 66  r->pLeft );.  af
20715 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  f = sqlite3ExprA
20716 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70  ffinity(pExpr->p
20717 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78  Left);.  if( pEx
20718 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20  pr->pRight ){.  
20719 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43    aff = sqlite3C
2071a 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
2071b 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 61 66  Expr->pRight, af
2071c 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  f);.  }else if( 
2071d 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2071e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2071f 65 63 74 29 20 29 7b 0a 20 20 20 20 61 66 66 20  ect) ){.    aff 
20720 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65  = sqlite3Compare
20721 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e  Affinity(pExpr->
20722 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  x.pSelect->pELis
20723 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 61  t->a[0].pExpr, a
20724 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ff);.  }else if(
20725 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61 66 66   !aff ){.    aff
20726 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
20727 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  NE;.  }.  return
20728 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70   aff;.}../*.** p
20729 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72  Expr is a compar
2072a 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c  ison expression,
2072b 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20 49   eg. '=', '<', I
2072c 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20 69  N(...) etc..** i
2072d 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73 20 74  dx_affinity is t
2072e 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 61  he affinity of a
2072f 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  n indexed column
20730 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a 2a 2a  . Return true.**
20731 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 77 69   if the index wi
20732 74 68 20 61 66 66 69 6e 69 74 79 20 69 64 78 5f  th affinity idx_
20733 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62 65 20  affinity may be 
20734 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
20735 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69  t.** the compari
20736 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a 2f  son in pExpr..*/
20737 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20738 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78  int sqlite3Index
20739 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20  AffinityOk(Expr 
2073a 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78  *pExpr, char idx
2073b 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20 63 68  _affinity){.  ch
2073c 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69  ar aff = compari
2073d 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70  sonAffinity(pExp
2073e 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20 61 66  r);.  switch( af
2073f 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  f ){.    case SQ
20740 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 20  LITE_AFF_NONE:. 
20741 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
20742 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
20743 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 72  FF_TEXT:.      r
20744 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69 6e 69  eturn idx_affini
20745 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54  ty==SQLITE_AFF_T
20746 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  EXT;.    default
20747 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73  :.      return s
20748 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41  qlite3IsNumericA
20749 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66 66 69  ffinity(idx_affi
2074a 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  nity);.  }.}../*
2074b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 50  .** Return the P
2074c 35 20 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f  5 value that sho
2074d 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20  uld be used for 
2074e 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69  a binary compari
2074f 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28 4f  son.** opcode (O
20750 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74 63 2e  P_Eq, OP_Ge etc.
20751 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72  ) used to compar
20752 65 20 70 45 78 70 72 31 20 61 6e 64 20 70 45 78  e pExpr1 and pEx
20753 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  pr2..*/.static u
20754 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50  8 binaryCompareP
20755 35 28 45 78 70 72 20 2a 70 45 78 70 72 31 2c 20  5(Expr *pExpr1, 
20756 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20 69 6e  Expr *pExpr2, in
20757 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20  t jumpIfNull){. 
20758 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 72 29   u8 aff = (char)
20759 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
2075a 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20 20 61  ity(pExpr2);.  a
2075b 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74 65 33  ff = (u8)sqlite3
2075c 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
2075d 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c 20 28  pExpr1, aff) | (
2075e 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a 20  u8)jumpIfNull;. 
2075f 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a   return aff;.}..
20760 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
20761 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f  ointer to the co
20762 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
20763 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
20764 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e  used by.** a bin
20765 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  ary comparison o
20766 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e  perator comparin
20767 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  g pLeft and pRig
20768 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ht..**.** If the
20769 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 65   left hand expre
2076a 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c  ssion has a coll
2076b 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74  ating sequence t
2076c 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a  ype, then it is.
2076d 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69  ** used. Otherwi
2076e 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  se the collation
2076f 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
20770 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 70  e right hand exp
20771 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73  ression.** is us
20772 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75  ed, or the defau
20773 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e  lt (BINARY) if n
20774 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f  either expressio
20775 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e  n has a collatin
20776 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a  g.** type..**.**
20777 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74   Argument pRight
20778 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29   (but not pLeft)
20779 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70   may be a null p
2077a 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20  ointer. In this 
2077b 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e  case,.** it is n
2077c 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a  ot considered..*
2077d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2077e 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65   CollSeq *sqlite
2077f 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f  3BinaryCompareCo
20780 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a  llSeq(.  Parse *
20781 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20  pParse, .  Expr 
20782 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20  *pLeft, .  Expr 
20783 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c  *pRight.){.  Col
20784 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61  lSeq *pColl;.  a
20785 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a  ssert( pLeft );.
20786 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61    if( pLeft->fla
20787 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
20788 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  te ){.    assert
20789 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29  ( pLeft->pColl )
2078a 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c  ;.    pColl = pL
2078b 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65  eft->pColl;.  }e
2078c 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 20 26  lse if( pRight &
2078d 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20  & pRight->flags 
2078e 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  & EP_ExpCollate 
2078f 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
20790 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a  Right->pColl );.
20791 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67      pColl = pRig
20792 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c  ht->pColl;.  }el
20793 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  se{.    pColl = 
20794 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
20795 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  eq(pParse, pLeft
20796 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c  );.    if( !pCol
20797 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  l ){.      pColl
20798 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
20799 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 52  llSeq(pParse, pR
2079a 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
2079b 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b  .  return pColl;
2079c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
2079d 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20  te the operands 
2079e 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e  for a comparison
2079f 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 66   operation.  Bef
207a0 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e  ore.** generatin
207a1 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 65  g the code for e
207a2 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 74  ach operand, set
207a3 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a 2a   the EP_AnyAff.*
207a4 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 78  * flag on the ex
207a5 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74  pression so that
207a6 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c 65   it will be able
207a7 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 61   to used a.** ca
207a8 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  ched column valu
207a9 65 20 74 68 61 74 20 68 61 73 20 70 72 65 76 69  e that has previ
207aa 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 20  ously undergone 
207ab 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 63  an.** affinity c
207ac 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  hange..*/.static
207ad 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 72   void codeCompar
207ae 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 72  eOperands(.  Par
207af 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
207b0 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
207b1 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
207b2 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20  ntext */.  Expr 
207b3 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20  *pLeft,      /* 
207b4 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  The left operand
207b5 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 4c   */.  int *pRegL
207b6 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 73  eft,    /* Regis
207b7 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 20 6f  ter where left o
207b8 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65 64  perand is stored
207b9 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65   */.  int *pFree
207ba 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 20  Left,   /* Free 
207bb 74 68 69 73 20 72 65 67 69 73 74 65 72 20 77 68  this register wh
207bc 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 70  en done */.  Exp
207bd 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f  r *pRight,     /
207be 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72  * The right oper
207bf 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52  and */.  int *pR
207c0 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 65  egRight,   /* Re
207c1 67 69 73 74 65 72 20 77 68 65 72 65 20 72 69 67  gister where rig
207c2 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74  ht operand is st
207c3 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ored */.  int *p
207c4 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20 57  FreeRight   /* W
207c5 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 73 74  rite temp regist
207c6 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70 65  er for right ope
207c7 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 7b  rand there */.){
207c8 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d  .  while( pLeft-
207c9 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20  >op==TK_UPLUS ) 
207ca 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70  pLeft = pLeft->p
207cb 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e 66  Left;.  pLeft->f
207cc 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 66  lags |= EP_AnyAf
207cd 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 3d  f;.  *pRegLeft =
207ce 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
207cf 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65  Temp(pParse, pLe
207d0 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b 0a  ft, pFreeLeft);.
207d1 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 74 2d    while( pRight-
207d2 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20  >op==TK_UPLUS ) 
207d3 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 2d  pRight = pRight-
207d4 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 74  >pLeft;.  pRight
207d5 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e  ->flags |= EP_An
207d6 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 67  yAff;.  *pRegRig
207d7 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ht = sqlite3Expr
207d8 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
207d9 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 52 69   pRight, pFreeRi
207da 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ght);.}../*.** G
207db 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
207dc 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70   a comparison op
207dd 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  erator..*/.stati
207de 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72  c int codeCompar
207df 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
207e0 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61  se,    /* The pa
207e1 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20  rsing (and code 
207e2 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74  generating) cont
207e3 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ext */.  Expr *p
207e4 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68  Left,      /* Th
207e5 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  e left operand *
207e6 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  /.  Expr *pRight
207e7 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67  ,     /* The rig
207e8 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
207e9 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20  int opcode,     
207ea 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69    /* The compari
207eb 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  son opcode */.  
207ec 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e 32  int in1, int in2
207ed 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f  , /* Register ho
207ee 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 2a  lding operands *
207ef 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20 20  /.  int dest,   
207f0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
207f1 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f 0a  re if true.  */.
207f2 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c    int jumpIfNull
207f3 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
207f4 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20 6f  jump if either o
207f5 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a  perand is NULL *
207f6 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a 20  /.){.  int p5;. 
207f7 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f 6c   int addr;.  Col
207f8 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 20  lSeq *p4;..  p4 
207f9 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43  = sqlite3BinaryC
207fa 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50  ompareCollSeq(pP
207fb 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69  arse, pLeft, pRi
207fc 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 6e  ght);.  p5 = bin
207fd 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c 65  aryCompareP5(pLe
207fe 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70  ft, pRight, jump
207ff 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 20  IfNull);.  addr 
20800 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
20801 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62  Op4(pParse->pVdb
20802 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c 20  e, opcode, in2, 
20803 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 20  dest, in1,.     
20804 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20805 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34 2c        (void*)p4,
20806 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20   P4_COLLSEQ);.  
20807 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
20808 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64 62  eP5(pParse->pVdb
20809 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20 69 66  e, (u8)p5);.  if
2080a 28 20 28 70 35 20 26 20 53 51 4c 49 54 45 5f 41  ( (p5 & SQLITE_A
2080b 46 46 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 54 45  FF_MASK)!=SQLITE
2080c 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20  _AFF_NONE ){.   
2080d 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2080e 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
2080f 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31 29 3b  pParse, in1, 1);
20810 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
20811 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
20812 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 32 2c  nge(pParse, in2,
20813 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   1);.  }.  retur
20814 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 53  n addr;.}..#if S
20815 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
20816 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 65  EPTH>0./*.** Che
20817 63 6b 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74  ck that argument
20818 20 6e 48 65 69 67 68 74 20 69 73 20 6c 65 73 73   nHeight is less
20819 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
2081a 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a  o the maximum.**
2081b 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74   expression dept
2081c 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74  h allowed. If it
2081d 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61   is not, leave a
2081e 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
2081f 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2f  in.** pParse..*/
20820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20821 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
20822 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65  heckHeight(Parse
20823 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 48   *pParse, int nH
20824 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63  eight){.  int rc
20825 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
20826 69 6e 74 20 6d 78 48 65 69 67 68 74 20 3d 20 70  int mxHeight = p
20827 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69  Parse->db->aLimi
20828 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45  t[SQLITE_LIMIT_E
20829 58 50 52 5f 44 45 50 54 48 5d 3b 0a 20 20 69 66  XPR_DEPTH];.  if
2082a 28 20 6e 48 65 69 67 68 74 3e 6d 78 48 65 69 67  ( nHeight>mxHeig
2082b 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ht ){.    sqlite
2082c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2082d 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70 72 65  , .       "Expre
2082e 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20 74 6f  ssion tree is to
2082f 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 6d  o large (maximum
20830 20 64 65 70 74 68 20 25 64 29 22 2c 20 6d 78 48   depth %d)", mxH
20831 65 69 67 68 74 0a 20 20 20 20 29 3b 0a 20 20 20  eight.    );.   
20832 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
20833 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OR;.  }.  return
20834 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66   rc;.}../* The f
20835 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66  ollowing three f
20836 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67 68 74  unctions, height
20837 4f 66 45 78 70 72 28 29 2c 20 68 65 69 67 68 74  OfExpr(), height
20838 4f 66 45 78 70 72 4c 69 73 74 28 29 0a 2a 2a 20  OfExprList().** 
20839 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65  and heightOfSele
2083a 63 74 28 29 2c 20 61 72 65 20 75 73 65 64 20 74  ct(), are used t
2083b 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
2083c 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 0a 2a  maximum height.*
2083d 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73  * of any express
2083e 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e  ion tree referen
2083f 63 65 64 20 62 79 20 74 68 65 20 73 74 72 75 63  ced by the struc
20840 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74  ture passed as t
20841 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75  he.** first argu
20842 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ment..**.** If t
20843 68 69 73 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  his maximum heig
20844 68 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ht is greater th
20845 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  an the current v
20846 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20  alue pointed.** 
20847 74 6f 20 62 79 20 70 6e 48 65 69 67 68 74 2c 20  to by pnHeight, 
20848 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
20849 65 74 65 72 2c 20 74 68 65 6e 20 73 65 74 20 2a  eter, then set *
2084a 70 6e 48 65 69 67 68 74 20 74 6f 20 74 68 61 74  pnHeight to that
2084b 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74  .** value..*/.st
2084c 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74  atic void height
2084d 4f 66 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20  OfExpr(Expr *p, 
2084e 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a  int *pnHeight){.
2084f 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69    if( p ){.    i
20850 66 28 20 70 2d 3e 6e 48 65 69 67 68 74 3e 2a 70  f( p->nHeight>*p
20851 6e 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 20  nHeight ){.     
20852 20 2a 70 6e 48 65 69 67 68 74 20 3d 20 70 2d 3e   *pnHeight = p->
20853 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20  nHeight;.    }. 
20854 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
20855 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
20856 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69  t(ExprList *p, i
20857 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20  nt *pnHeight){. 
20858 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e   if( p ){.    in
20859 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
2085a 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b  ; i<p->nExpr; i+
2085b 2b 29 7b 0a 20 20 20 20 20 20 68 65 69 67 68 74  +){.      height
2085c 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69 5d 2e 70  OfExpr(p->a[i].p
2085d 45 78 70 72 2c 20 70 6e 48 65 69 67 68 74 29 3b  Expr, pnHeight);
2085e 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61  .    }.  }.}.sta
2085f 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f  tic void heightO
20860 66 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a  fSelect(Select *
20861 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74  p, int *pnHeight
20862 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
20863 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70    heightOfExpr(p
20864 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48 65 69 67  ->pWhere, pnHeig
20865 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
20866 66 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67  fExpr(p->pHaving
20867 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20  , pnHeight);.   
20868 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d   heightOfExpr(p-
20869 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65 69 67 68  >pLimit, pnHeigh
2086a 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66  t);.    heightOf
2086b 45 78 70 72 28 70 2d 3e 70 4f 66 66 73 65 74 2c  Expr(p->pOffset,
2086c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20   pnHeight);.    
2086d 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
2086e 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65  (p->pEList, pnHe
2086f 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68  ight);.    heigh
20870 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70  tOfExprList(p->p
20871 47 72 6f 75 70 42 79 2c 20 70 6e 48 65 69 67 68  GroupBy, pnHeigh
20872 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66  t);.    heightOf
20873 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64  ExprList(p->pOrd
20874 65 72 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b  erBy, pnHeight);
20875 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c  .    heightOfSel
20876 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70  ect(p->pPrior, p
20877 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 7d 0a  nHeight);.  }.}.
20878 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45  ./*.** Set the E
20879 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69  xpr.nHeight vari
2087a 61 62 6c 65 20 69 6e 20 74 68 65 20 73 74 72 75  able in the stru
2087b 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20  cture passed as 
2087c 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e  an .** argument.
2087d 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77   An expression w
2087e 69 74 68 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2c  ith no children,
2087f 20 45 78 70 72 2e 70 4c 69 73 74 20 6f 72 20 0a   Expr.pList or .
20880 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65 63 74 20  ** Expr.pSelect 
20881 6d 65 6d 62 65 72 20 68 61 73 20 61 20 68 65 69  member has a hei
20882 67 68 74 20 6f 66 20 31 2e 20 41 6e 79 20 6f 74  ght of 1. Any ot
20883 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  her expression.*
20884 2a 20 68 61 73 20 61 20 68 65 69 67 68 74 20 65  * has a height e
20885 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69  qual to the maxi
20886 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e  mum height of an
20887 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72 65 66 65  y other .** refe
20888 72 65 6e 63 65 64 20 45 78 70 72 20 70 6c 75 73  renced Expr plus
20889 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   one..*/.static 
2088a 76 6f 69 64 20 65 78 70 72 53 65 74 48 65 69 67  void exprSetHeig
2088b 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69  ht(Expr *p){.  i
2088c 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a  nt nHeight = 0;.
2088d 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70    heightOfExpr(p
2088e 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69 67 68  ->pLeft, &nHeigh
2088f 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78  t);.  heightOfEx
20890 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e  pr(p->pRight, &n
20891 48 65 69 67 68 74 29 3b 0a 20 20 69 66 28 20 45  Height);.  if( E
20892 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20893 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
20894 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53  ){.    heightOfS
20895 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c 65  elect(p->x.pSele
20896 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20  ct, &nHeight);. 
20897 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 65 69 67   }else{.    heig
20898 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e  htOfExprList(p->
20899 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68  x.pList, &nHeigh
2089a 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 48 65  t);.  }.  p->nHe
2089b 69 67 68 74 20 3d 20 6e 48 65 69 67 68 74 20 2b  ight = nHeight +
2089c 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   1;.}../*.** Set
2089d 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68   the Expr.nHeigh
2089e 74 20 76 61 72 69 61 62 6c 65 20 75 73 69 6e 67  t variable using
2089f 20 74 68 65 20 65 78 70 72 53 65 74 48 65 69 67   the exprSetHeig
208a0 68 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 49  ht() function. I
208a1 66 0a 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20  f.** the height 
208a2 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
208a3 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f  the maximum allo
208a4 77 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 64  wed expression d
208a5 65 70 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 61  epth,.** leave a
208a6 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73  n error in pPars
208a7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
208a8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
208a9 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 50  3ExprSetHeight(P
208aa 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
208ab 70 72 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 65  pr *p){.  exprSe
208ac 74 48 65 69 67 68 74 28 70 29 3b 0a 20 20 73 71  tHeight(p);.  sq
208ad 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65  lite3ExprCheckHe
208ae 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e  ight(pParse, p->
208af 6e 48 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a  nHeight);.}../*.
208b0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61  ** Return the ma
208b1 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20  ximum height of 
208b2 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74  any expression t
208b3 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a  ree referenced.*
208b4 2a 20 62 79 20 74 68 65 20 73 65 6c 65 63 74 20  * by the select 
208b5 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64  statement passed
208b6 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
208b7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
208b8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65  TE int sqlite3Se
208b9 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 53  lectExprHeight(S
208ba 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74  elect *p){.  int
208bb 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20   nHeight = 0;.  
208bc 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70  heightOfSelect(p
208bd 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72  , &nHeight);.  r
208be 65 74 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d  eturn nHeight;.}
208bf 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
208c0 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 79   exprSetHeight(y
208c1 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ).#endif /* SQLI
208c2 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
208c3 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  H>0 */../*.** Th
208c4 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
208c5 65 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74 6f 72  e core allocator
208c6 20 66 6f 72 20 45 78 70 72 20 6e 6f 64 65 73 2e   for Expr nodes.
208c7 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74  .**.** Construct
208c8 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f   a new expressio
208c9 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 72  n node and retur
208ca 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  n a pointer to i
208cb 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f  t.  Memory.** fo
208cc 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e 64 20  r this node and 
208cd 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e 20 61  for the pToken a
208ce 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73 69 6e  rgument is a sin
208cf 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  gle allocation.*
208d0 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  * obtained from 
208d1 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28  sqlite3DbMalloc(
208d2 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  ).  The calling 
208d3 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 72  function.** is r
208d4 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d  esponsible for m
208d5 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 6e  aking sure the n
208d6 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 67  ode eventually g
208d7 65 74 73 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a  ets freed..**.**
208d8 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 20 74   If dequote is t
208d9 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 74 6f  rue, then the to
208da 6b 65 6e 20 28 69 66 20 69 74 20 65 78 69 73 74  ken (if it exist
208db 73 29 20 69 73 20 64 65 71 75 6f 74 65 64 2e 0a  s) is dequoted..
208dc 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 73  ** If dequote is
208dd 20 66 61 6c 73 65 2c 20 6e 6f 20 64 65 71 75 6f   false, no dequo
208de 74 69 6e 67 20 69 73 20 70 65 72 66 6f 72 6d 61  ting is performa
208df 6e 63 65 2e 20 20 54 68 65 20 64 65 51 75 6f 74  nce.  The deQuot
208e0 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  e.** parameter i
208e1 73 20 69 67 6e 6f 72 65 64 20 69 66 20 70 54 6f  s ignored if pTo
208e2 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69  ken is NULL or i
208e3 66 20 74 68 65 20 74 6f 6b 65 6e 20 64 6f 65 73  f the token does
208e4 20 6e 6f 74 0a 2a 2a 20 61 70 70 65 61 72 20 74   not.** appear t
208e5 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 49 66  o be quoted.  If
208e6 20 74 68 65 20 71 75 6f 74 65 73 20 77 65 72 65   the quotes were
208e7 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2e 2e   of the form "..
208e8 2e 22 20 28 64 6f 75 62 6c 65 2d 71 75 6f 74 65  ." (double-quote
208e9 73 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 45  s).** then the E
208ea 50 5f 44 62 6c 51 75 6f 74 65 64 20 66 6c 61 67  P_DblQuoted flag
208eb 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 65   is set on the e
208ec 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 2e 0a  xpression node..
208ed 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 63 61  **.** Special ca
208ee 73 65 3a 20 20 49 66 20 6f 70 3d 3d 54 4b 5f 49  se:  If op==TK_I
208ef 4e 54 45 47 45 52 20 61 6e 64 20 70 54 6f 6b 65  NTEGER and pToke
208f0 6e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74  n points to a st
208f1 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63 61 6e  ring that.** can
208f2 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 20 69   be translated i
208f3 6e 74 6f 20 61 20 33 32 2d 62 69 74 20 69 6e 74  nto a 32-bit int
208f4 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 74  eger, then the t
208f5 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 73  oken is not.** s
208f6 74 6f 72 65 64 20 69 6e 20 75 2e 7a 54 6f 6b 65  tored in u.zToke
208f7 6e 2e 20 20 49 6e 73 74 65 61 64 2c 20 74 68 65  n.  Instead, the
208f8 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20   integer values 
208f9 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e  is written.** in
208fa 74 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e 64 20  to u.iValue and 
208fb 74 68 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20  the EP_IntValue 
208fc 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 6f  flag is set.  No
208fd 20 65 78 74 72 61 20 73 74 6f 72 61 67 65 0a 2a   extra storage.*
208fe 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 74  * is allocated t
208ff 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74 65 67  o hold the integ
20900 65 72 20 74 65 78 74 20 61 6e 64 20 74 68 65 20  er text and the 
20901 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69 73 20  dequote flag is 
20902 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ignored..*/.SQLI
20903 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
20904 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f  *sqlite3ExprAllo
20905 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  c(.  sqlite3 *db
20906 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
20907 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74  Handle for sqlit
20908 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29  e3DbMallocZero()
20909 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a   (may be null) *
2090a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
2090b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
2090c 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65  xpression opcode
2090d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65   */.  const Toke
2090e 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20 20 2f 2a  n *pToken,    /*
2090f 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e   Token argument.
20910 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20    Might be NULL 
20911 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 65  */.  int dequote
20912 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20913 54 72 75 65 20 74 6f 20 64 65 71 75 6f 74 65 20  True to dequote 
20914 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e  */.){.  Expr *pN
20915 65 77 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72 61  ew;.  int nExtra
20916 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 56 61 6c   = 0;.  int iVal
20917 75 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70  ue = 0;..  if( p
20918 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66 28  Token ){.    if(
20919 20 6f 70 21 3d 54 4b 5f 49 4e 54 45 47 45 52 20   op!=TK_INTEGER 
2091a 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d 30 0a  || pToken->z==0.
2091b 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c            || sql
2091c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 54 6f  ite3GetInt32(pTo
2091d 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75 65 29  ken->z, &iValue)
2091e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 78  ==0 ){.      nEx
2091f 74 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 2b  tra = pToken->n+
20920 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
20921 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
20922 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
20923 7a 65 6f 66 28 45 78 70 72 29 2b 6e 45 78 74 72  zeof(Expr)+nExtr
20924 61 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29  a);.  if( pNew )
20925 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 20 3d  {.    pNew->op =
20926 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 70 4e 65   (u8)op;.    pNe
20927 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a 20 20  w->iAgg = -1;.  
20928 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a    if( pToken ){.
20929 20 20 20 20 20 20 69 66 28 20 6e 45 78 74 72 61        if( nExtra
2092a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
2092b 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  New->flags |= EP
2092c 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 20  _IntValue;.     
2092d 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c 75     pNew->u.iValu
2092e 65 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 20 20  e = iValue;.    
2092f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
20930 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 20   int c;.        
20931 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d  pNew->u.zToken =
20932 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d   (char*)&pNew[1]
20933 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
20934 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  (pNew->u.zToken,
20935 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b   pToken->z, pTok
20936 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20  en->n);.        
20937 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 70  pNew->u.zToken[p
20938 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b 0a 20  Token->n] = 0;. 
20939 20 20 20 20 20 20 20 69 66 28 20 64 65 71 75 6f         if( dequo
2093a 74 65 20 26 26 20 6e 45 78 74 72 61 3e 3d 33 20  te && nExtra>=3 
2093b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26  .             &&
2093c 20 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e 7a   ((c = pToken->z
2093d 5b 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 3d  [0])=='\'' || c=
2093e 3d 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 7c  ='"' || c=='[' |
2093f 7c 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20 20  | c=='`') ){.   
20940 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
20941 71 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e 7a 54  quote(pNew->u.zT
20942 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  oken);.         
20943 20 69 66 28 20 63 3d 3d 27 22 27 20 29 20 70 4e   if( c=='"' ) pN
20944 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ew->flags |= EP_
20945 44 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20 20  DblQuoted;.     
20946 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
20947 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41   }.#if SQLITE_MA
20948 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20  X_EXPR_DEPTH>0. 
20949 20 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67 68 74     pNew->nHeight
2094a 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20 0a 20   = 1;.#endif  . 
2094b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
2094c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
2094d 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73  ate a new expres
2094e 73 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d 20 61  sion node from a
2094f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
20950 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 61 73 0a   token that has.
20951 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
20952 64 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53 51 4c  dequoted..*/.SQL
20953 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
20954 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a 20   *sqlite3Expr(. 
20955 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
20956 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
20957 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 62  le for sqlite3Db
20958 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61  MallocZero() (ma
20959 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20  y be null) */.  
2095a 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
2095b 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
2095c 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a  ssion opcode */.
2095d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2095e 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 6f 6b  oken      /* Tok
2095f 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 4d 69  en argument.  Mi
20960 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29  ght be NULL */.)
20961 7b 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20 20 78  {.  Token x;.  x
20962 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 78  .z = zToken;.  x
20963 2e 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20 73 71  .n = zToken ? sq
20964 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54  lite3Strlen30(zT
20965 6f 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72 65 74  oken) : 0;.  ret
20966 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41  urn sqlite3ExprA
20967 6c 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26 78 2c  lloc(db, op, &x,
20968 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74   0);.}../*.** At
20969 74 61 63 68 20 73 75 62 74 72 65 65 73 20 70 4c  tach subtrees pL
2096a 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20 74  eft and pRight t
2096b 6f 20 74 68 65 20 45 78 70 72 20 6e 6f 64 65 20  o the Expr node 
2096c 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  pRoot..**.** If 
2096d 70 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68 61 74  pRoot==NULL that
2096e 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 65   means that a me
2096f 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
20970 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
20971 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63  ed..** In that c
20972 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20  ase, delete the 
20973 73 75 62 74 72 65 65 73 20 70 4c 65 66 74 20 61  subtrees pLeft a
20974 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 53 51  nd pRight..*/.SQ
20975 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20976 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74  d sqlite3ExprAtt
20977 61 63 68 53 75 62 74 72 65 65 73 28 0a 20 20 73  achSubtrees(.  s
20978 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 45 78  qlite3 *db,.  Ex
20979 70 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45 78 70  pr *pRoot,.  Exp
2097a 72 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78 70 72  r *pLeft,.  Expr
2097b 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 69 66   *pRight.){.  if
2097c 28 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20 20  ( pRoot==0 ){.  
2097d 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
2097e 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
2097f 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
20980 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29 3b 0a  ete(db, pLeft);.
20981 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
20982 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67 68 74  elete(db, pRight
20983 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
20984 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a 20 20  if( pRight ){.  
20985 20 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69 67 68      pRoot->pRigh
20986 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20  t = pRight;.    
20987 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66 6c    if( pRight->fl
20988 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
20989 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ate ){.        p
2098a 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Root->flags |= E
2098b 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
2098c 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43 6f        pRoot->pCo
2098d 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f  ll = pRight->pCo
2098e 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
2098f 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 20  }.    if( pLeft 
20990 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e  ){.      pRoot->
20991 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20  pLeft = pLeft;. 
20992 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e       if( pLeft->
20993 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
20994 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20  llate ){.       
20995 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d   pRoot->flags |=
20996 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a   EP_ExpCollate;.
20997 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70          pRoot->p
20998 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43  Coll = pLeft->pC
20999 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
2099a 20 7d 0a 20 20 20 20 65 78 70 72 53 65 74 48 65   }.    exprSetHe
2099b 69 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20 20 7d  ight(pRoot);.  }
2099c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
2099d 74 65 20 61 20 45 78 70 72 20 6e 6f 64 65 20 77  te a Expr node w
2099e 68 69 63 68 20 6a 6f 69 6e 73 20 61 73 20 6d 61  hich joins as ma
2099f 6e 79 20 61 73 20 74 77 6f 20 73 75 62 74 72 65  ny as two subtre
209a0 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 6f 72  es..**.** One or
209a1 20 62 6f 74 68 20 6f 66 20 74 68 65 20 73 75 62   both of the sub
209a2 74 72 65 65 73 20 63 61 6e 20 62 65 20 4e 55 4c  trees can be NUL
209a3 4c 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  L.  Return a poi
209a4 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a  nter to the new.
209a5 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 20 4f  ** Expr node.  O
209a6 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72  r, if an OOM err
209a7 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 70  or occurs, set p
209a8 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
209a9 63 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72 65 65  cFailed,.** free
209aa 20 74 68 65 20 73 75 62 74 72 65 65 73 20 61 6e   the subtrees an
209ab 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  d return NULL..*
209ac 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
209ad 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45   Expr *sqlite3PE
209ae 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  xpr(.  Parse *pP
209af 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
209b0 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
209b1 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  t */.  int op,  
209b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
209b3 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63  * Expression opc
209b4 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ode */.  Expr *p
209b5 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20  Left,           
209b6 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64   /* Left operand
209b7 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67   */.  Expr *pRig
209b8 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ht,           /*
209b9 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   Right operand *
209ba 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20  /.  const Token 
209bb 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 41  *pToken     /* A
209bc 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  rgument token */
209bd 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  .){.  Expr *p = 
209be 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63  sqlite3ExprAlloc
209bf 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c  (pParse->db, op,
209c0 20 70 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20 73   pToken, 1);.  s
209c1 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63 68  qlite3ExprAttach
209c2 53 75 62 74 72 65 65 73 28 70 50 61 72 73 65 2d  Subtrees(pParse-
209c3 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c 20 70  >db, p, pLeft, p
209c4 52 69 67 68 74 29 3b 0a 20 20 72 65 74 75 72 6e  Right);.  return
209c5 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69   p;.}../*.** Joi
209c6 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e  n two expression
209c7 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f  s using an AND o
209c8 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74  perator.  If eit
209c9 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69  her expression i
209ca 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  s.** NULL, then 
209cb 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  just return the 
209cc 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  other expression
209cd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
209ce 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
209cf 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65 33  3ExprAnd(sqlite3
209d0 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 66   *db, Expr *pLef
209d1 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 74 29  t, Expr *pRight)
209d2 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d 30  {.  if( pLeft==0
209d3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
209d4 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69  Right;.  }else i
209d5 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a  f( pRight==0 ){.
209d6 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66 74      return pLeft
209d7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 45  ;.  }else{.    E
209d8 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69  xpr *pNew = sqli
209d9 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c  te3ExprAlloc(db,
209da 20 54 4b 5f 41 4e 44 2c 20 30 2c 20 30 29 3b 0a   TK_AND, 0, 0);.
209db 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41      sqlite3ExprA
209dc 74 74 61 63 68 53 75 62 74 72 65 65 73 28 64 62  ttachSubtrees(db
209dd 2c 20 70 4e 65 77 2c 20 70 4c 65 66 74 2c 20 70  , pNew, pLeft, p
209de 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75  Right);.    retu
209df 72 6e 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a  rn pNew;.  }.}..
209e0 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20  /*.** Construct 
209e1 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e  a new expression
209e2 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e 63   node for a func
209e3 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69 70  tion with multip
209e4 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e  le.** arguments.
209e5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
209e6 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
209e7 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72  ExprFunction(Par
209e8 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
209e9 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b  List *pList, Tok
209ea 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45  en *pToken){.  E
209eb 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 71 6c  xpr *pNew;.  sql
209ec 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
209ed 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  e->db;.  assert(
209ee 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e 65   pToken );.  pNe
209ef 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  w = sqlite3ExprA
209f0 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 46 55 4e 43  lloc(db, TK_FUNC
209f1 54 49 4f 4e 2c 20 70 54 6f 6b 65 6e 2c 20 31 29  TION, pToken, 1)
209f2 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
209f3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
209f4 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
209f5 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69   pList); /* Avoi
209f6 64 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77 68  d memory leak wh
209f7 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20  en malloc fails 
209f8 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  */.    return 0;
209f9 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 78 2e 70  .  }.  pNew->x.p
209fa 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20  List = pList;.  
209fb 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
209fc 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
209fd 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
209fe 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
209ff 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70  Height(pParse, p
20a00 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  New);.  return p
20a01 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73  New;.}../*.** As
20a02 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c 65 20  sign a variable 
20a03 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65 78 70  number to an exp
20a04 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 6e 63  ression that enc
20a05 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72 64 0a  odes a wildcard.
20a06 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  ** in the origin
20a07 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  al SQL statement
20a08 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61  .  .**.** Wildca
20a09 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f  rds consisting o
20a0a 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22 20 61  f a single "?" a
20a0b 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20  re assigned the 
20a0c 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61 6c 0a  next sequential.
20a0d 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  ** variable numb
20a0e 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61  er..**.** Wildca
20a0f 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  rds of the form 
20a10 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73 69 67  "?nnn" are assig
20a11 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20 22  ned the number "
20a12 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65 0a 2a  nnn".  We make.*
20a13 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69 73 20  * sure "nnn" is 
20a14 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20 61 76  not too be to av
20a15 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f 66 20  oid a denial of 
20a16 73 65 72 76 69 63 65 20 61 74 74 61 63 6b 20 77  service attack w
20a17 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73  hen.** the SQL s
20a18 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66  tatement comes f
20a19 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61 6c 20  rom an external 
20a1a 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 69  source..**.** Wi
20a1b 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 66  ldcards of the f
20a1c 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40 61 61  orm ":aaa", "@aa
20a1d 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20 61 72  a", or "$aaa" ar
20a1e 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 73  e assigned the s
20a1f 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 73  ame number.** as
20a20 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e   the previous in
20a21 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61  stance of the sa
20a22 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f 72  me wildcard.  Or
20a23 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
20a24 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 6e 63  first.** instanc
20a25 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 61 72  e of the wildcar
20a26 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 71 75  d, the next sequ
20a27 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 20 6e  enial variable n
20a28 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 73 69  umber is.** assi
20a29 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gned..*/.SQLITE_
20a2a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
20a2b 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61  ite3ExprAssignVa
20a2c 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 2a 70  rNumber(Parse *p
20a2d 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
20a2e 70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  pr){.  sqlite3 *
20a2f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
20a30 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
20a31 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  ;..  if( pExpr==
20a32 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  0 ) return;.  as
20a33 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e  sert( !ExprHasAn
20a34 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  yProperty(pExpr,
20a35 20 45 50 5f 49 6e 74 56 61 6c 75 65 7c 45 50 5f   EP_IntValue|EP_
20a36 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
20a37 4f 6e 6c 79 29 20 29 3b 0a 20 20 7a 20 3d 20 70  Only) );.  z = p
20a38 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  Expr->u.zToken;.
20a39 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 29    assert( z!=0 )
20a3a 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 5b 30 5d  ;.  assert( z[0]
20a3b 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a 5b 31  !=0 );.  if( z[1
20a3c 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57  ]==0 ){.    /* W
20a3d 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66  ildcard of the f
20a3e 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 6e  orm "?".  Assign
20a3f 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 62   the next variab
20a40 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20  le number */.   
20a41 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d 3d 27   assert( z[0]=='
20a42 3f 27 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d  ?' );.    pExpr-
20a43 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61  >iColumn = (ynVa
20a44 72 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61  r)(++pParse->nVa
20a45 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  r);.  }else if( 
20a46 7a 5b 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20  z[0]=='?' ){.   
20a47 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20   /* Wildcard of 
20a48 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e  the form "?nnn".
20a49 20 20 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20    Convert "nnn" 
20a4a 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e  to an integer an
20a4b 64 0a 20 20 20 20 2a 2a 20 75 73 65 20 69 74 20  d.    ** use it 
20a4c 61 73 20 74 68 65 20 76 61 72 69 61 62 6c 65 20  as the variable 
20a4d 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e  number */.    in
20a4e 74 20 69 20 3d 20 61 74 6f 69 28 28 63 68 61 72  t i = atoi((char
20a4f 2a 29 26 7a 5b 31 5d 29 3b 0a 20 20 20 20 70 45  *)&z[1]);.    pE
20a50 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28  xpr->iColumn = (
20a51 79 6e 56 61 72 29 69 3b 0a 20 20 20 20 74 65 73  ynVar)i;.    tes
20a52 74 63 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20  tcase( i==0 );. 
20a53 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
20a54 31 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  1 );.    testcas
20a55 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74  e( i==db->aLimit
20a56 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41  [SQLITE_LIMIT_VA
20a57 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31  RIABLE_NUMBER]-1
20a58 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
20a59 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  ( i==db->aLimit[
20a5a 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
20a5b 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b  IABLE_NUMBER] );
20a5c 0a 20 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20  .    if( i<1 || 
20a5d 69 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  i>db->aLimit[SQL
20a5e 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42  ITE_LIMIT_VARIAB
20a5f 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20  LE_NUMBER] ){.  
20a60 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
20a61 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 61 72  Msg(pParse, "var
20a62 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73  iable number mus
20a63 74 20 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20  t be between ?1 
20a64 61 6e 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20  and ?%d",.      
20a65 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53      db->aLimit[S
20a66 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
20a67 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20  ABLE_NUMBER]);. 
20a68 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70     }.    if( i>p
20a69 50 61 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20  Parse->nVar ){. 
20a6a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61       pParse->nVa
20a6b 72 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d  r = i;.    }.  }
20a6c 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c  else{.    /* Wil
20a6d 64 63 61 72 64 73 20 6c 69 6b 65 20 22 3a 61 61  dcards like ":aa
20a6e 61 22 2c 20 22 24 61 61 61 22 20 6f 72 20 22 40  a", "$aaa" or "@
20a6f 61 61 61 22 2e 20 20 52 65 75 73 65 20 74 68 65  aaa".  Reuse the
20a70 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 0a 20   same variable. 
20a71 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20     ** number as 
20a72 74 68 65 20 70 72 69 6f 72 20 61 70 70 65 61 72  the prior appear
20a73 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65  ance of the same
20a74 20 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74 68 65   name, or if the
20a75 20 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73   name.    ** has
20a76 20 6e 65 76 65 72 20 61 70 70 65 61 72 65 64 20   never appeared 
20a77 62 65 66 6f 72 65 2c 20 72 65 75 73 65 20 74 68  before, reuse th
20a78 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 20  e same variable 
20a79 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20  number.    */.  
20a7a 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 75 33 32    int i;.    u32
20a7b 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69   n;.    n = sqli
20a7c 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a  te3Strlen30(z);.
20a7d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
20a7e 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b  Parse->nVarExpr;
20a7f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70   i++){.      Exp
20a80 72 20 2a 70 45 20 3d 20 70 50 61 72 73 65 2d 3e  r *pE = pParse->
20a81 61 70 56 61 72 45 78 70 72 5b 69 5d 3b 0a 20 20  apVarExpr[i];.  
20a82 20 20 20 20 61 73 73 65 72 74 28 20 70 45 21 3d      assert( pE!=
20a83 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d  0 );.      if( m
20a84 65 6d 63 6d 70 28 70 45 2d 3e 75 2e 7a 54 6f 6b  emcmp(pE->u.zTok
20a85 65 6e 2c 20 7a 2c 20 6e 29 3d 3d 30 20 26 26 20  en, z, n)==0 && 
20a86 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e 5d 3d  pE->u.zToken[n]=
20a87 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45  =0 ){.        pE
20a88 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70  xpr->iColumn = p
20a89 45 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  E->iColumn;.    
20a8a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
20a8b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
20a8c 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72   i>=pParse->nVar
20a8d 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 70 45  Expr ){.      pE
20a8e 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28  xpr->iColumn = (
20a8f 79 6e 56 61 72 29 28 2b 2b 70 50 61 72 73 65 2d  ynVar)(++pParse-
20a90 3e 6e 56 61 72 29 3b 0a 20 20 20 20 20 20 69 66  >nVar);.      if
20a91 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78  ( pParse->nVarEx
20a92 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72  pr>=pParse->nVar
20a93 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a 20  ExprAlloc-1 ){. 
20a94 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e         pParse->n
20a95 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d 20  VarExprAlloc += 
20a96 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
20a97 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 20  Alloc + 10;.    
20a98 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61      pParse->apVa
20a99 72 45 78 70 72 20 3d 0a 20 20 20 20 20 20 20 20  rExpr =.        
20a9a 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61      sqlite3DbRea
20a9b 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 20 20  llocOrFree(.    
20a9c 20 20 20 20 20 20 20 20 20 20 64 62 2c 0a 20 20            db,.  
20a9d 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72              pPar
20a9e 73 65 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a 20  se->apVarExpr,. 
20a9f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
20aa0 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c  rse->nVarExprAll
20aa1 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65  oc*sizeof(pParse
20aa2 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 5d 29 0a  ->apVarExpr[0]).
20aa3 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20              );. 
20aa4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
20aa5 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
20aa6 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  ed ){.        as
20aa7 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70  sert( pParse->ap
20aa8 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 20  VarExpr!=0 );.  
20aa9 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70        pParse->ap
20aaa 56 61 72 45 78 70 72 5b 70 50 61 72 73 65 2d 3e  VarExpr[pParse->
20aab 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 45  nVarExpr++] = pE
20aac 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  xpr;.      }.   
20aad 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 70   }.  } .  if( !p
20aae 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 70  Parse->nErr && p
20aaf 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62 2d 3e  Parse->nVar>db->
20ab0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
20ab1 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
20ab2 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69  BER] ){.    sqli
20ab3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
20ab4 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53 51  se, "too many SQ
20ab5 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b 0a 20  L variables");. 
20ab6 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61   }.}../*.** Clea
20ab7 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  r an expression 
20ab8 73 74 72 75 63 74 75 72 65 20 77 69 74 68 6f 75  structure withou
20ab9 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 73  t deleting the s
20aba 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e  tructure itself.
20abb 0a 2a 2a 20 53 75 62 73 74 72 75 63 74 75 72 65  .** Substructure
20abc 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a   is deleted..*/.
20abd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20abe 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
20abf 6c 65 61 72 28 73 71 6c 69 74 65 33 20 2a 64 62  lear(sqlite3 *db
20ac0 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 61 73  , Expr *p){.  as
20ac1 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20  sert( p!=0 );.  
20ac2 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  if( !ExprHasAnyP
20ac3 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f  roperty(p, EP_To
20ac4 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20  kenOnly) ){.    
20ac5 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
20ac6 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b  e(db, p->pLeft);
20ac7 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
20ac8 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52  Delete(db, p->pR
20ac9 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 21  ight);.    if( !
20aca 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
20acb 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 26  p, EP_Reduced) &
20acc 26 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45  & (p->flags2 & E
20acd 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e  P2_MallocedToken
20ace 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
20acf 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
20ad0 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  p->u.zToken);.  
20ad1 20 20 7d 0a 20 20 20 20 69 66 28 20 45 78 70 72    }.    if( Expr
20ad2 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
20ad3 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a  P_xIsSelect) ){.
20ad4 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
20ad5 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  ectDelete(db, p-
20ad6 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  >x.pSelect);.   
20ad7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
20ad8 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
20ad9 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69  ete(db, p->x.pLi
20ada 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  st);.    }.  }.}
20adb 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76  ../*.** Recursiv
20adc 65 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78  ely delete an ex
20add 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a  pression tree..*
20ade 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20adf 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
20ae0 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20  rDelete(sqlite3 
20ae1 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20  *db, Expr *p){. 
20ae2 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
20ae3 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  rn;.  sqlite3Exp
20ae4 72 43 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a 20  rClear(db, p);. 
20ae5 20 69 66 28 20 21 45 78 70 72 48 61 73 50 72 6f   if( !ExprHasPro
20ae6 70 65 72 74 79 28 70 2c 20 45 50 5f 53 74 61 74  perty(p, EP_Stat
20ae7 69 63 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ic) ){.    sqlit
20ae8 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
20ae9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
20aea 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
20aeb 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74  of bytes allocat
20aec 65 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65  ed for the expre
20aed 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20  ssion structure 
20aee 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68  .** passed as th
20aef 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
20af0 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73  . This is always
20af1 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c   one of EXPR_FUL
20af2 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f 52  LSIZE,.** EXPR_R
20af3 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58  EDUCEDSIZE or EX
20af4 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45  PR_TOKENONLYSIZE
20af5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
20af6 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 45  exprStructSize(E
20af7 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45  xpr *p){.  if( E
20af8 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20af9 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20  , EP_TokenOnly) 
20afa 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f 54 4f  ) return EXPR_TO
20afb 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 69  KENONLYSIZE;.  i
20afc 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
20afd 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64  ty(p, EP_Reduced
20afe 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f  ) ) return EXPR_
20aff 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20 20 72  REDUCEDSIZE;.  r
20b00 65 74 75 72 6e 20 45 58 50 52 5f 46 55 4c 4c 53  eturn EXPR_FULLS
20b01 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  IZE;.}../*.** Th
20b02 65 20 64 75 70 65 64 45 78 70 72 2a 53 69 7a 65  e dupedExpr*Size
20b03 28 29 20 72 6f 75 74 69 6e 65 73 20 65 61 63 68  () routines each
20b04 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   return the numb
20b05 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75  er of bytes requ
20b06 69 72 65 64 0a 2a 2a 20 74 6f 20 73 74 6f 72 65  ired.** to store
20b07 20 61 20 63 6f 70 79 20 6f 66 20 61 6e 20 65 78   a copy of an ex
20b08 70 72 65 73 73 69 6f 6e 20 6f 72 20 65 78 70 72  pression or expr
20b09 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 54 68  ession tree.  Th
20b0a 65 79 20 64 69 66 66 65 72 20 69 6e 0a 2a 2a 20  ey differ in.** 
20b0b 68 6f 77 20 6d 75 63 68 20 6f 66 20 74 68 65 20  how much of the 
20b0c 74 72 65 65 20 69 73 20 6d 65 61 73 75 72 65 64  tree is measured
20b0d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64 75 70 65  ..**.**     dupe
20b0e 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28  dExprStructSize(
20b0f 29 20 20 20 20 20 53 69 7a 65 20 6f 66 20 6f 6e  )     Size of on
20b10 6c 79 20 74 68 65 20 45 78 70 72 20 73 74 72 75  ly the Expr stru
20b11 63 74 75 72 65 20 0a 2a 2a 20 20 20 20 20 64 75  cture .**     du
20b12 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28  pedExprNodeSize(
20b13 29 20 20 20 20 20 20 20 53 69 7a 65 20 6f 66 20  )       Size of 
20b14 45 78 70 72 20 2b 20 73 70 61 63 65 20 66 6f 72  Expr + space for
20b15 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 64 75   token.**     du
20b16 70 65 64 45 78 70 72 53 69 7a 65 28 29 20 20 20  pedExprSize()   
20b17 20 20 20 20 20 20 20 20 45 78 70 72 20 2b 20 74          Expr + t
20b18 6f 6b 65 6e 20 2b 20 73 75 62 74 72 65 65 20 63  oken + subtree c
20b19 6f 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a 2a 2a  omponents.**.***
20b1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b1e 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
20b1f 68 65 20 64 75 70 65 64 45 78 70 72 53 74 72 75  he dupedExprStru
20b20 63 74 53 69 7a 65 28 29 20 66 75 6e 63 74 69 6f  ctSize() functio
20b21 6e 20 72 65 74 75 72 6e 73 20 74 77 6f 20 76 61  n returns two va
20b22 6c 75 65 73 20 4f 52 2d 65 64 20 74 6f 67 65 74  lues OR-ed toget
20b23 68 65 72 3a 20 20 0a 2a 2a 20 28 31 29 20 74 68  her:  .** (1) th
20b24 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64  e space required
20b25 20 66 6f 72 20 61 20 63 6f 70 79 20 6f 66 20 74   for a copy of t
20b26 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72  he Expr structur
20b27 65 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a 20 28  e only and .** (
20b28 32 29 20 74 68 65 20 45 50 5f 78 78 78 20 66 6c  2) the EP_xxx fl
20b29 61 67 73 20 74 68 61 74 20 69 6e 64 69 63 61 74  ags that indicat
20b2a 65 20 77 68 61 74 20 74 68 65 20 73 74 72 75 63  e what the struc
20b2b 74 75 72 65 20 73 69 7a 65 20 73 68 6f 75 6c 64  ture size should
20b2c 20 62 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75   be..** The retu
20b2d 72 6e 20 76 61 6c 75 65 73 20 69 73 20 61 6c 77  rn values is alw
20b2e 61 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a  ays one of:.**.*
20b2f 2a 20 20 20 20 20 20 45 58 50 52 5f 46 55 4c 4c  *      EXPR_FULL
20b30 53 49 5a 45 0a 2a 2a 20 20 20 20 20 20 45 58 50  SIZE.**      EXP
20b31 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20  R_REDUCEDSIZE   
20b32 7c 20 45 50 5f 52 65 64 75 63 65 64 0a 2a 2a 20  | EP_Reduced.** 
20b33 20 20 20 20 20 45 58 50 52 5f 54 4f 4b 45 4e 4f       EXPR_TOKENO
20b34 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b  NLYSIZE | EP_Tok
20b35 65 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 65  enOnly.**.** The
20b36 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 74 72   size of the str
20b37 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 66 6f  ucture can be fo
20b38 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74  und by masking t
20b39 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
20b3a 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  ** of this routi
20b3b 6e 65 20 77 69 74 68 20 30 78 66 66 66 2e 20 20  ne with 0xfff.  
20b3c 54 68 65 20 66 6c 61 67 73 20 63 61 6e 20 62 65  The flags can be
20b3d 20 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e   found by maskin
20b3e 67 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20  g the.** return 
20b3f 76 61 6c 75 65 20 77 69 74 68 20 45 50 5f 52 65  value with EP_Re
20b40 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e  duced|EP_TokenOn
20b41 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  ly..**.** Note t
20b42 68 61 74 20 77 69 74 68 20 66 6c 61 67 73 3d 3d  hat with flags==
20b43 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20  EXPRDUP_REDUCE, 
20b44 74 68 69 73 20 72 6f 75 74 69 6e 65 73 20 77 6f  this routines wo
20b45 72 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65  rks on full-size
20b46 0a 2a 2a 20 28 75 6e 72 65 64 75 63 65 64 29 20  .** (unreduced) 
20b47 45 78 70 72 20 6f 62 6a 65 63 74 73 20 61 73 20  Expr objects as 
20b48 74 68 65 79 20 6f 72 20 6f 72 69 67 69 6e 61 6c  they or original
20b49 6c 79 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  ly constructed b
20b4a 79 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a  y the parser..**
20b4b 20 44 75 72 69 6e 67 20 65 78 70 72 65 73 73 69   During expressi
20b4c 6f 6e 20 61 6e 61 6c 79 73 69 73 2c 20 65 78 74  on analysis, ext
20b4d 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ra information i
20b4e 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 6d  s computed and m
20b4f 6f 76 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c 61 74  oved into.** lat
20b50 65 72 20 70 61 72 74 73 20 6f 66 20 74 65 68 20  er parts of teh 
20b51 45 78 70 72 20 6f 62 6a 65 63 74 20 61 6e 64 20  Expr object and 
20b52 74 68 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72  that extra infor
20b53 6d 61 74 69 6f 6e 20 6d 69 67 68 74 20 67 65 74  mation might get
20b54 20 63 68 6f 70 70 65 64 0a 2a 2a 20 6f 66 66 20   chopped.** off 
20b55 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
20b56 6e 20 69 73 20 72 65 64 75 63 65 64 2e 20 20 4e  n is reduced.  N
20b57 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 69 74  ote also that it
20b58 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 74   does not work t
20b59 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 45 58 50 52  o.** make a EXPR
20b5a 44 55 50 5f 52 45 44 55 43 45 20 63 6f 70 79 20  DUP_REDUCE copy 
20b5b 6f 66 20 61 20 72 65 64 75 63 65 64 20 65 78 70  of a reduced exp
20b5c 72 65 73 73 69 6f 6e 2e 20 20 49 74 20 69 73 20  ression.  It is 
20b5d 6f 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20 74 6f  only legal.** to
20b5e 20 72 65 64 75 63 65 20 61 20 70 72 69 73 74 69   reduce a pristi
20b5f 6e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  ne expression tr
20b60 65 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73  ee from the pars
20b61 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65  er.  The impleme
20b62 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 64 75  ntation.** of du
20b63 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a  pedExprStructSiz
20b64 65 28 29 20 63 6f 6e 74 61 69 6e 20 6d 75 6c 74  e() contain mult
20b65 69 70 6c 65 20 61 73 73 65 72 74 28 29 20 73 74  iple assert() st
20b66 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 61 74  atements that at
20b67 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 66 6f  tempt.** to enfo
20b68 72 63 65 20 74 68 69 73 20 63 6f 6e 73 74 72 61  rce this constra
20b69 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  int..*/.static i
20b6a 6e 74 20 64 75 70 65 64 45 78 70 72 53 74 72 75  nt dupedExprStru
20b6b 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20  ctSize(Expr *p, 
20b6c 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e  int flags){.  in
20b6d 74 20 6e 53 69 7a 65 3b 0a 20 20 61 73 73 65 72  t nSize;.  asser
20b6e 74 28 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55  t( flags==EXPRDU
20b6f 50 5f 52 45 44 55 43 45 20 7c 7c 20 66 6c 61 67  P_REDUCE || flag
20b70 73 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c 79 20  s==0 ); /* Only 
20b71 6f 6e 65 20 66 6c 61 67 20 76 61 6c 75 65 20 61  one flag value a
20b72 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 69 66 28 20  llowed */.  if( 
20b73 30 3d 3d 28 66 6c 61 67 73 26 45 58 50 52 44 55  0==(flags&EXPRDU
20b74 50 5f 52 45 44 55 43 45 29 20 29 7b 0a 20 20 20  P_REDUCE) ){.   
20b75 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 46 55   nSize = EXPR_FU
20b76 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 7b  LLSIZE;.  }else{
20b77 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78  .    assert( !Ex
20b78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
20b79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  (p, EP_TokenOnly
20b7a 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a  |EP_Reduced) );.
20b7b 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
20b7c 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
20b7d 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20  EP_FromJoin) ); 
20b7e 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d  .    assert( (p-
20b7f 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61  >flags2 & EP2_Ma
20b80 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20  llocedToken)==0 
20b81 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
20b82 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f  p->flags2 & EP2_
20b83 49 72 72 65 64 75 63 69 62 6c 65 29 3d 3d 30 20  Irreducible)==0 
20b84 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c  );.    if( p->pL
20b85 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74  eft || p->pRight
20b86 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c 7c 20   || p->pColl || 
20b87 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20  p->x.pList ){.  
20b88 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52      nSize = EXPR
20b89 5f 52 45 44 55 43 45 44 53 49 5a 45 20 7c 20 45  _REDUCEDSIZE | E
20b8a 50 5f 52 65 64 75 63 65 64 3b 0a 20 20 20 20 7d  P_Reduced;.    }
20b8b 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a  else{.      nSiz
20b8c 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e  e = EXPR_TOKENON
20b8d 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65  LYSIZE | EP_Toke
20b8e 6e 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20 20 7d  nOnly;.    }.  }
20b8f 0a 20 20 72 65 74 75 72 6e 20 6e 53 69 7a 65 3b  .  return nSize;
20b90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
20b91 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
20b92 74 68 65 20 73 70 61 63 65 20 69 6e 20 62 79 74  the space in byt
20b93 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73  es required to s
20b94 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20 0a 2a  tore the copy .*
20b95 2a 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74  * of the Expr st
20b96 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 63 6f  ructure and a co
20b97 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 2e 75  py of the Expr.u
20b98 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28  .zToken string (
20b99 69 66 20 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e  if that.** strin
20b9a 67 20 69 73 20 64 65 66 69 6e 65 64 2e 29 0a 2a  g is defined.).*
20b9b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70  /.static int dup
20b9c 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 45  edExprNodeSize(E
20b9d 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  xpr *p, int flag
20b9e 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20  s){.  int nByte 
20b9f 3d 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63  = dupedExprStruc
20ba0 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 20  tSize(p, flags) 
20ba1 26 20 30 78 66 66 66 3b 0a 20 20 69 66 28 20 21  & 0xfff;.  if( !
20ba2 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
20ba3 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  p, EP_IntValue) 
20ba4 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29  && p->u.zToken )
20ba5 7b 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73  {.    nByte += s
20ba6 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
20ba7 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b 0a 20  ->u.zToken)+1;. 
20ba8 20 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e   }.  return ROUN
20ba9 44 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a  D8(nByte);.}../*
20baa 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
20bab 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
20bac 65 71 75 69 72 65 64 20 74 6f 20 63 72 65 61 74  equired to creat
20bad 65 20 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66  e a duplicate of
20bae 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73   the .** express
20baf 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68  ion passed as th
20bb0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
20bb1 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  . The second arg
20bb2 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20 6d 61  ument is a.** ma
20bb3 73 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 45 58  sk containing EX
20bb4 50 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e  PRDUP_XXX flags.
20bb5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .**.** The value
20bb6 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64   returned includ
20bb7 65 73 20 73 70 61 63 65 20 74 6f 20 63 72 65 61  es space to crea
20bb8 74 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  te a copy of the
20bb9 20 45 78 70 72 20 73 74 72 75 63 74 0a 2a 2a 20   Expr struct.** 
20bba 69 74 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62  itself and the b
20bbb 75 66 66 65 72 20 72 65 66 65 72 72 65 64 20 74  uffer referred t
20bbc 6f 20 62 79 20 45 78 70 72 2e 75 2e 7a 54 6f 6b  o by Expr.u.zTok
20bbd 65 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a  en, if any..**.*
20bbe 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50  * If the EXPRDUP
20bbf 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20  _REDUCE flag is 
20bc0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65  set, then the re
20bc1 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75  turn value inclu
20bc2 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f  des .** space to
20bc3 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45   duplicate all E
20bc4 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65  xpr nodes in the
20bc5 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20   tree formed by 
20bc6 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61  Expr.pLeft .** a
20bc7 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 76  nd Expr.pRight v
20bc8 61 72 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f  ariables (but no
20bc9 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74  t for any struct
20bca 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20  ures pointed to 
20bcb 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64  or .** descended
20bcc 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78   from the Expr.x
20bcd 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78  .pList or Expr.x
20bce 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c  .pSelect variabl
20bcf 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  es)..*/.static i
20bd0 6e 74 20 64 75 70 65 64 45 78 70 72 53 69 7a 65  nt dupedExprSize
20bd1 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c  (Expr *p, int fl
20bd2 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ags){.  int nByt
20bd3 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29  e = 0;.  if( p )
20bd4 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75  {.    nByte = du
20bd5 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28  pedExprNodeSize(
20bd6 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69  p, flags);.    i
20bd7 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50  f( flags&EXPRDUP
20bd8 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20  _REDUCE ){.     
20bd9 20 6e 42 79 74 65 20 2b 3d 20 64 75 70 65 64 45   nByte += dupedE
20bda 78 70 72 53 69 7a 65 28 70 2d 3e 70 4c 65 66 74  xprSize(p->pLeft
20bdb 2c 20 66 6c 61 67 73 29 20 2b 20 64 75 70 65 64  , flags) + duped
20bdc 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 52 69 67  ExprSize(p->pRig
20bdd 68 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20  ht, flags);.    
20bde 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  }.  }.  return n
20bdf 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  Byte;.}../*.** T
20be0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
20be1 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74  similar to sqlit
20be2 65 33 45 78 70 72 44 75 70 28 29 2c 20 65 78 63  e3ExprDup(), exc
20be3 65 70 74 20 74 68 61 74 20 69 66 20 70 7a 42 75  ept that if pzBu
20be4 66 66 65 72 20 0a 2a 2a 20 69 73 20 6e 6f 74 20  ffer .** is not 
20be5 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 42 75 66  NULL then *pzBuf
20be6 66 65 72 20 69 73 20 61 73 73 75 6d 65 64 20 74  fer is assumed t
20be7 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66  o point to a buf
20be8 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68  fer large enough
20be9 20 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68   .** to store th
20bea 65 20 63 6f 70 79 20 6f 66 20 65 78 70 72 65 73  e copy of expres
20beb 73 69 6f 6e 20 70 2c 20 74 68 65 20 63 6f 70 69  sion p, the copi
20bec 65 73 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f 6b 65  es of p->u.zToke
20bed 6e 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61  n.** (if applica
20bee 62 6c 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f  ble), and the co
20bef 70 69 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70  pies of the p->p
20bf0 4c 65 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67  Left and p->pRig
20bf1 68 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a  ht expressions,.
20bf2 2a 2a 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72  ** if any. Befor
20bf3 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a  e returning, *pz
20bf4 42 75 66 66 65 72 20 69 73 20 73 65 74 20 74 6f  Buffer is set to
20bf5 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
20bf6 70 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f  passed the.** po
20bf7 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66  rtion of the buf
20bf8 66 65 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  fer copied into 
20bf9 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
20bfa 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72  ..*/.static Expr
20bfb 20 2a 65 78 70 72 44 75 70 28 73 71 6c 69 74 65   *exprDup(sqlite
20bfc 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20  3 *db, Expr *p, 
20bfd 69 6e 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a  int flags, u8 **
20bfe 70 7a 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70  pzBuffer){.  Exp
20bff 72 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20  r *pNew = 0;    
20c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20c01 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
20c02 74 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20  turn */.  if( p 
20c03 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  ){.    const int
20c04 20 69 73 52 65 64 75 63 65 64 20 3d 20 28 66 6c   isReduced = (fl
20c05 61 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55  ags&EXPRDUP_REDU
20c06 43 45 29 3b 0a 20 20 20 20 75 38 20 2a 7a 41 6c  CE);.    u8 *zAl
20c07 6c 6f 63 3b 0a 20 20 20 20 75 33 32 20 73 74 61  loc;.    u32 sta
20c08 74 69 63 46 6c 61 67 20 3d 20 30 3b 0a 0a 20 20  ticFlag = 0;..  
20c09 20 20 61 73 73 65 72 74 28 20 70 7a 42 75 66 66    assert( pzBuff
20c0a 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65 64 75 63  er==0 || isReduc
20c0b 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69  ed );..    /* Fi
20c0c 67 75 72 65 20 6f 75 74 20 77 68 65 72 65 20 74  gure out where t
20c0d 6f 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 20  o write the new 
20c0e 45 78 70 72 20 73 74 72 75 63 74 75 72 65 2e 20  Expr structure. 
20c0f 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a 42 75 66  */.    if( pzBuf
20c10 66 65 72 20 29 7b 0a 20 20 20 20 20 20 7a 41 6c  fer ){.      zAl
20c11 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66 65 72 3b  loc = *pzBuffer;
20c12 0a 20 20 20 20 20 20 73 74 61 74 69 63 46 6c 61  .      staticFla
20c13 67 20 3d 20 45 50 5f 53 74 61 74 69 63 3b 0a 20  g = EP_Static;. 
20c14 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
20c15 7a 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33  zAlloc = sqlite3
20c16 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
20c17 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 70 2c  dupedExprSize(p,
20c18 20 66 6c 61 67 73 29 29 3b 0a 20 20 20 20 7d 0a   flags));.    }.
20c19 20 20 20 20 70 4e 65 77 20 3d 20 28 45 78 70 72      pNew = (Expr
20c1a 20 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20   *)zAlloc;..    
20c1b 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20  if( pNew ){.    
20c1c 20 20 2f 2a 20 53 65 74 20 6e 4e 65 77 53 69 7a    /* Set nNewSiz
20c1d 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20 61 6c  e to the size al
20c1e 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20  located for the 
20c1f 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65  structure pointe
20c20 64 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 79  d to.      ** by
20c21 20 70 4e 65 77 2e 20 54 68 69 73 20 69 73 20 65   pNew. This is e
20c22 69 74 68 65 72 20 45 58 50 52 5f 46 55 4c 4c 53  ither EXPR_FULLS
20c23 49 5a 45 2c 20 45 58 50 52 5f 52 45 44 55 43 45  IZE, EXPR_REDUCE
20c24 44 53 49 5a 45 20 6f 72 0a 20 20 20 20 20 20 2a  DSIZE or.      *
20c25 2a 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59  * EXPR_TOKENONLY
20c26 53 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20  SIZE. nToken is 
20c27 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
20c28 72 20 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75  r of bytes consu
20c29 6d 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20  med.      ** by 
20c2a 74 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20  the copy of the 
20c2b 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69  p->u.zToken stri
20c2c 6e 67 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20  ng (if any)..   
20c2d 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73     */.      cons
20c2e 74 20 75 6e 73 69 67 6e 65 64 20 6e 53 74 72 75  t unsigned nStru
20c2f 63 74 53 69 7a 65 20 3d 20 64 75 70 65 64 45 78  ctSize = dupedEx
20c30 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20  prStructSize(p, 
20c31 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 63 6f  flags);.      co
20c32 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 69 7a 65  nst int nNewSize
20c33 20 3d 20 6e 53 74 72 75 63 74 53 69 7a 65 20 26   = nStructSize &
20c34 20 30 78 66 66 66 3b 0a 20 20 20 20 20 20 69 6e   0xfff;.      in
20c35 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  t nToken;.      
20c36 69 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  if( !ExprHasProp
20c37 65 72 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61  erty(p, EP_IntVa
20c38 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f  lue) && p->u.zTo
20c39 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  ken ){.        n
20c3a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 53  Token = sqlite3S
20c3b 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f  trlen30(p->u.zTo
20c3c 6b 65 6e 29 20 2b 20 31 3b 0a 20 20 20 20 20 20  ken) + 1;.      
20c3d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e  }else{.        n
20c3e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  Token = 0;.     
20c3f 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 52   }.      if( isR
20c40 65 64 75 63 65 64 20 29 7b 0a 20 20 20 20 20 20  educed ){.      
20c41 20 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61    assert( ExprHa
20c42 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  sProperty(p, EP_
20c43 52 65 64 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20  Reduced)==0 );. 
20c44 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41         memcpy(zA
20c45 6c 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a  lloc, p, nNewSiz
20c46 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  e);.      }else{
20c47 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 53 69  .        int nSi
20c48 7a 65 20 3d 20 65 78 70 72 53 74 72 75 63 74 53  ze = exprStructS
20c49 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20  ize(p);.        
20c4a 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70  memcpy(zAlloc, p
20c4b 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  , nSize);.      
20c4c 20 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63    memset(&zAlloc
20c4d 5b 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52  [nSize], 0, EXPR
20c4e 5f 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29  _FULLSIZE-nSize)
20c4f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
20c50 20 2f 2a 20 53 65 74 20 74 68 65 20 45 50 5f 52   /* Set the EP_R
20c51 65 64 75 63 65 64 2c 20 45 50 5f 54 6f 6b 65 6e  educed, EP_Token
20c52 4f 6e 6c 79 2c 20 61 6e 64 20 45 50 5f 53 74 61  Only, and EP_Sta
20c53 74 69 63 20 66 6c 61 67 73 20 61 70 70 72 6f 70  tic flags approp
20c54 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20 20  riately. */.    
20c55 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 26 3d    pNew->flags &=
20c56 20 7e 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50   ~(EP_Reduced|EP
20c57 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 74  _TokenOnly|EP_St
20c58 61 74 69 63 29 3b 0a 20 20 20 20 20 20 70 4e 65  atic);.      pNe
20c59 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e 53 74 72  w->flags |= nStr
20c5a 75 63 74 53 69 7a 65 20 26 20 28 45 50 5f 52 65  uctSize & (EP_Re
20c5b 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e  duced|EP_TokenOn
20c5c 6c 79 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  ly);.      pNew-
20c5d 3e 66 6c 61 67 73 20 7c 3d 20 73 74 61 74 69 63  >flags |= static
20c5e 46 6c 61 67 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  Flag;..      /* 
20c5f 43 6f 70 79 20 74 68 65 20 70 2d 3e 75 2e 7a 54  Copy the p->u.zT
20c60 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 66 20  oken string, if 
20c61 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  any. */.      if
20c62 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20  ( nToken ){.    
20c63 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e      char *zToken
20c64 20 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65   = pNew->u.zToke
20c65 6e 20 3d 20 28 63 68 61 72 2a 29 26 7a 41 6c 6c  n = (char*)&zAll
20c66 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a 20 20  oc[nNewSize];.  
20c67 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 54 6f        memcpy(zTo
20c68 6b 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e  ken, p->u.zToken
20c69 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  , nToken);.     
20c6a 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d   }..      if( 0=
20c6b 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77  =((p->flags|pNew
20c6c 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f 54 6f  ->flags) & EP_To
20c6d 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20  kenOnly) ){.    
20c6e 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74      /* Fill in t
20c6f 68 65 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65  he pNew->x.pSele
20c70 63 74 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c  ct or pNew->x.pL
20c71 69 73 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20  ist member. */. 
20c72 20 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48         if( ExprH
20c73 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  asProperty(p, EP
20c74 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
20c75 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78           pNew->x
20c76 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  .pSelect = sqlit
20c77 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
20c78 70 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73  p->x.pSelect, is
20c79 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20  Reduced);.      
20c7a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
20c7b 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74     pNew->x.pList
20c7c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
20c7d 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70  stDup(db, p->x.p
20c7e 4c 69 73 74 2c 20 69 73 52 65 64 75 63 65 64 29  List, isReduced)
20c7f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
20c80 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69    }..      /* Fi
20c81 6c 6c 20 69 6e 20 70 4e 65 77 2d 3e 70 4c 65 66  ll in pNew->pLef
20c82 74 20 61 6e 64 20 70 4e 65 77 2d 3e 70 52 69 67  t and pNew->pRig
20c83 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ht. */.      if(
20c84 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65   ExprHasAnyPrope
20c85 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64  rty(pNew, EP_Red
20c86 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  uced|EP_TokenOnl
20c87 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 41  y) ){.        zA
20c88 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64 45 78 70  lloc += dupedExp
20c89 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61  rNodeSize(p, fla
20c8a 67 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  gs);.        if(
20c8b 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
20c8c 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65  (pNew, EP_Reduce
20c8d 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  d) ){.          
20c8e 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 65 78  pNew->pLeft = ex
20c8f 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65  prDup(db, p->pLe
20c90 66 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  ft, EXPRDUP_REDU
20c91 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20  CE, &zAlloc);.  
20c92 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52          pNew->pR
20c93 69 67 68 74 20 3d 20 65 78 70 72 44 75 70 28 64  ight = exprDup(d
20c94 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 45 58  b, p->pRight, EX
20c95 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a  PRDUP_REDUCE, &z
20c96 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20  Alloc);.        
20c97 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 7a  }.        if( pz
20c98 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20  Buffer ){.      
20c99 20 20 20 20 2a 70 7a 42 75 66 66 65 72 20 3d 20      *pzBuffer = 
20c9a 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20  zAlloc;.        
20c9b 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
20c9c 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61         pNew->fla
20c9d 67 73 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  gs2 = 0;.       
20c9e 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79   if( !ExprHasAny
20c9f 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54  Property(p, EP_T
20ca0 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20  okenOnly) ){.   
20ca1 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65         pNew->pLe
20ca2 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
20ca3 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74  Dup(db, p->pLeft
20ca4 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
20ca5 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 73  pNew->pRight = s
20ca6 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
20ca7 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b  , p->pRight, 0);
20ca8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
20ca9 20 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   }..    }.  }.  
20caa 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a  return pNew;.}..
20cab 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
20cac 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75  ing group of rou
20cad 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20  tines make deep 
20cae 63 6f 70 69 65 73 20 6f 66 20 65 78 70 72 65 73  copies of expres
20caf 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73  sions,.** expres
20cb0 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c  sion lists, ID l
20cb1 69 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74  ists, and select
20cb2 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68   statements.  Th
20cb3 65 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20  e copies can.** 
20cb4 62 65 20 64 65 6c 65 74 65 64 20 28 62 79 20 62  be deleted (by b
20cb5 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74  eing passed to t
20cb6 68 65 69 72 20 72 65 73 70 65 63 74 69 76 65 20  heir respective 
20cb7 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74  ...Delete() rout
20cb8 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74  ines).** without
20cb9 20 65 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f   effecting the o
20cba 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20  riginals..**.** 
20cbb 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  The expression l
20cbc 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75  ist, ID, and sou
20cbd 72 63 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e  rce lists return
20cbe 20 62 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c   by sqlite3ExprL
20cbf 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c  istDup(),.** sql
20cc0 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c  ite3IdListDup(),
20cc1 20 61 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c   and sqlite3SrcL
20cc2 69 73 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74  istDup() can not
20cc3 20 62 65 20 66 75 72 74 68 65 72 20 65 78 70 61   be further expa
20cc4 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73  nded .** by subs
20cc5 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20  equent calls to 
20cc6 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e  sqlite*ListAppen
20cc7 64 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  d() routines..**
20cc8 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74  .** Any tables t
20cc9 68 61 74 20 74 68 65 20 53 72 63 4c 69 73 74 20  hat the SrcList 
20cca 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61  might point to a
20ccb 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65  re not duplicate
20ccc 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61  d..**.** The fla
20ccd 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 6f 6e  gs parameter con
20cce 74 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e 61 74  tains a combinat
20ccf 69 6f 6e 20 6f 66 20 74 68 65 20 45 58 50 52 44  ion of the EXPRD
20cd0 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a 2a  UP_XXX flags..**
20cd1 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f   If the EXPRDUP_
20cd2 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73  REDUCE flag is s
20cd3 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72  et, then the str
20cd4 75 63 74 75 72 65 20 72 65 74 75 72 6e 65 64 20  ucture returned 
20cd5 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61 74 65  is a.** truncate
20cd6 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  d version of the
20cd7 20 75 73 75 61 6c 20 45 78 70 72 20 73 74 72 75   usual Expr stru
20cd8 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20  cture that will 
20cd9 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20  be stored as.** 
20cda 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d  part of the in-m
20cdb 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61  emory representa
20cdc 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61  tion of the data
20cdd 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a  base schema..*/.
20cde 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
20cdf 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
20ce0 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Dup(sqlite3 *db,
20ce1 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c   Expr *p, int fl
20ce2 61 67 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 65  ags){.  return e
20ce3 78 70 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c  xprDup(db, p, fl
20ce4 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54  ags, 0);.}.SQLIT
20ce5 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69  E_PRIVATE ExprLi
20ce6 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c  st *sqlite3ExprL
20ce7 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a  istDup(sqlite3 *
20ce8 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 2c  db, ExprList *p,
20ce9 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 45   int flags){.  E
20cea 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20  xprList *pNew;. 
20ceb 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
20cec 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20 2a 70  _item *pItem, *p
20ced 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69  OldItem;.  int i
20cee 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
20cef 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20  eturn 0;.  pNew 
20cf0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
20cf1 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
20cf2 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20  *pNew) );.  if( 
20cf3 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  pNew==0 ) return
20cf4 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 43 75   0;.  pNew->iECu
20cf5 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e 65 77  rsor = 0;.  pNew
20cf6 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77 2d 3e  ->nExpr = pNew->
20cf7 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 78 70  nAlloc = p->nExp
20cf8 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 70  r;.  pNew->a = p
20cf9 49 74 65 6d 20 3d 20 73 71 6c 69 74 65 33 44 62  Item = sqlite3Db
20cfa 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 20 70  MallocRaw(db,  p
20cfb 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66 28 70  ->nExpr*sizeof(p
20cfc 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  ->a[0]) );.  if(
20cfd 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20 20   pItem==0 ){.   
20cfe 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
20cff 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65  b, pNew);.    re
20d00 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70  turn 0;.  } .  p
20d01 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a  OldItem = p->a;.
20d02 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
20d03 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
20d04 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29  m++, pOldItem++)
20d05 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6c 64  {.    Expr *pOld
20d06 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  Expr = pOldItem-
20d07 3e 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65  >pExpr;.    pIte
20d08 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  m->pExpr = sqlit
20d09 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f  e3ExprDup(db, pO
20d0a 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 29 3b 0a  ldExpr, flags);.
20d0b 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65      pItem->zName
20d0c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
20d0d 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
20d0e 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74  >zName);.    pIt
20d0f 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69  em->zSpan = sqli
20d10 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
20d11 70 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70 61 6e 29  pOldItem->zSpan)
20d12 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f 72  ;.    pItem->sor
20d13 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 65  tOrder = pOldIte
20d14 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  m->sortOrder;.  
20d15 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20    pItem->done = 
20d16 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43  0;.    pItem->iC
20d17 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69  ol = pOldItem->i
20d18 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  Col;.    pItem->
20d19 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65  iAlias = pOldIte
20d1a 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20  m->iAlias;.  }. 
20d1b 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
20d1c 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  ./*.** If cursor
20d1d 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69 65  s, triggers, vie
20d1e 77 73 20 61 6e 64 20 73 75 62 71 75 65 72 69 65  ws and subquerie
20d1f 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65  s are all omitte
20d20 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75  d from.** the bu
20d21 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f  ild, then none o
20d22 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
20d23 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74  routines, except
20d24 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33   for .** sqlite3
20d25 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e  SelectDup(), can
20d26 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69   be called. sqli
20d27 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 20 69  te3SelectDup() i
20d28 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63  s sometimes.** c
20d29 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c  alled with a NUL
20d2a 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23  L argument..*/.#
20d2b 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
20d2c 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c  TE_OMIT_VIEW) ||
20d2d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
20d2e 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c  _OMIT_TRIGGER) \
20d2f 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  . || !defined(SQ
20d30 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
20d31 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  RY).SQLITE_PRIVA
20d32 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69  TE SrcList *sqli
20d33 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 71  te3SrcListDup(sq
20d34 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69  lite3 *db, SrcLi
20d35 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73  st *p, int flags
20d36 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4e  ){.  SrcList *pN
20d37 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  ew;.  int i;.  i
20d38 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 20  nt nByte;.  if( 
20d39 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
20d3a 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f  .  nByte = sizeo
20d3b 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 63  f(*p) + (p->nSrc
20d3c 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e 61  >0 ? sizeof(p->a
20d3d 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 2d  [0]) * (p->nSrc-
20d3e 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 20  1) : 0);.  pNew 
20d3f 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
20d40 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20 29  cRaw(db, nByte )
20d41 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
20d42 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
20d43 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 2d  ew->nSrc = pNew-
20d44 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72  >nAlloc = p->nSr
20d45 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  c;.  for(i=0; i<
20d46 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  p->nSrc; i++){. 
20d47 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
20d48 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d  t_item *pNewItem
20d49 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a   = &pNew->a[i];.
20d4a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
20d4b 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65  st_item *pOldIte
20d4c 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20  m = &p->a[i];.  
20d4d 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
20d4e 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 61     pNewItem->zDa
20d4f 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33  tabase = sqlite3
20d50 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c  DbStrDup(db, pOl
20d51 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  dItem->zDatabase
20d52 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  );.    pNewItem-
20d53 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
20d54 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c  DbStrDup(db, pOl
20d55 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  dItem->zName);. 
20d56 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c     pNewItem->zAl
20d57 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ias = sqlite3DbS
20d58 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74  trDup(db, pOldIt
20d59 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20  em->zAlias);.   
20d5a 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74   pNewItem->joint
20d5b 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  ype = pOldItem->
20d5c 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 70 4e  jointype;.    pN
20d5d 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20  ewItem->iCursor 
20d5e 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75 72  = pOldItem->iCur
20d5f 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  sor;.    pNewIte
20d60 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d  m->isPopulated =
20d61 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f 70   pOldItem->isPop
20d62 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 77  ulated;.    pNew
20d63 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73  Item->zIndex = s
20d64 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
20d65 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e  b, pOldItem->zIn
20d66 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 49 74  dex);.    pNewIt
20d67 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d  em->notIndexed =
20d68 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e   pOldItem->notIn
20d69 64 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 77 49  dexed;.    pNewI
20d6a 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 4f  tem->pIndex = pO
20d6b 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b 0a  ldItem->pIndex;.
20d6c 20 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77 49      pTab = pNewI
20d6d 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c 64  tem->pTab = pOld
20d6e 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Item->pTab;.    
20d6f 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
20d70 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a    pTab->nRef++;.
20d71 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 74      }.    pNewIt
20d72 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71  em->pSelect = sq
20d73 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
20d74 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 65  b, pOldItem->pSe
20d75 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20  lect, flags);.  
20d76 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20    pNewItem->pOn 
20d77 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
20d78 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70  (db, pOldItem->p
20d79 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20  On, flags);.    
20d7a 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e 67  pNewItem->pUsing
20d7b 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74   = sqlite3IdList
20d7c 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
20d7d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 70  ->pUsing);.    p
20d7e 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64  NewItem->colUsed
20d7f 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c   = pOldItem->col
20d80 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Used;.  }.  retu
20d81 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54  rn pNew;.}.SQLIT
20d82 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74  E_PRIVATE IdList
20d83 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44   *sqlite3IdListD
20d84 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
20d85 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49 64  IdList *p){.  Id
20d86 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  List *pNew;.  in
20d87 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  t i;.  if( p==0 
20d88 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
20d89 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
20d8a 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
20d8b 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69  of(*pNew) );.  i
20d8c 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74  f( pNew==0 ) ret
20d8d 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e  urn 0;.  pNew->n
20d8e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f  Id = pNew->nAllo
20d8f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e  c = p->nId;.  pN
20d90 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44  ew->a = sqlite3D
20d91 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 70  bMallocRaw(db, p
20d92 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d 3e  ->nId*sizeof(p->
20d93 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70  a[0]) );.  if( p
20d94 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20  New->a==0 ){.   
20d95 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
20d96 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65  b, pNew);.    re
20d97 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f  turn 0;.  }.  fo
20d98 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64 3b  r(i=0; i<p->nId;
20d99 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63   i++){.    struc
20d9a 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70  t IdList_item *p
20d9b 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d  NewItem = &pNew-
20d9c 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63  >a[i];.    struc
20d9d 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70  t IdList_item *p
20d9e 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b  OldItem = &p->a[
20d9f 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  i];.    pNewItem
20da0 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ->zName = sqlite
20da1 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f  3DbStrDup(db, pO
20da2 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a  ldItem->zName);.
20da3 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 64      pNewItem->id
20da4 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 64  x = pOldItem->id
20da5 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  x;.  }.  return 
20da6 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  pNew;.}.SQLITE_P
20da7 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73  RIVATE Select *s
20da8 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
20da9 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c  sqlite3 *db, Sel
20daa 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  ect *p, int flag
20dab 73 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e  s){.  Select *pN
20dac 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  ew;.  if( p==0 )
20dad 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
20dae 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
20daf 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
20db0 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70  f(*p) );.  if( p
20db1 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
20db2 30 3b 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73  0;.  pNew->pELis
20db3 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
20db4 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45  istDup(db, p->pE
20db5 4c 69 73 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20  List, flags);.  
20db6 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c  pNew->pSrc = sql
20db7 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 64  ite3SrcListDup(d
20db8 62 2c 20 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67  b, p->pSrc, flag
20db9 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65  s);.  pNew->pWhe
20dba 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
20dbb 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72  Dup(db, p->pWher
20dbc 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65  e, flags);.  pNe
20dbd 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71  w->pGroupBy = sq
20dbe 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
20dbf 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79  (db, p->pGroupBy
20dc0 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77  , flags);.  pNew
20dc1 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69  ->pHaving = sqli
20dc2 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
20dc3 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73  ->pHaving, flags
20dc4 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65  );.  pNew->pOrde
20dc5 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  rBy = sqlite3Exp
20dc6 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
20dc7 70 4f 72 64 65 72 42 79 2c 20 66 6c 61 67 73 29  pOrderBy, flags)
20dc8 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70  ;.  pNew->op = p
20dc9 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50  ->op;.  pNew->pP
20dca 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65  rior = sqlite3Se
20dcb 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  lectDup(db, p->p
20dcc 50 72 69 6f 72 2c 20 66 6c 61 67 73 29 3b 0a 20  Prior, flags);. 
20dcd 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20   pNew->pLimit = 
20dce 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
20dcf 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c  b, p->pLimit, fl
20dd0 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f  ags);.  pNew->pO
20dd1 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45  ffset = sqlite3E
20dd2 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f  xprDup(db, p->pO
20dd3 66 66 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20  ffset, flags);. 
20dd4 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20   pNew->iLimit = 
20dd5 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73  0;.  pNew->iOffs
20dd6 65 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  et = 0;.  pNew->
20dd7 73 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65  selFlags = p->se
20dd8 6c 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65  lFlags & ~SF_Use
20dd9 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e  sEphemeral;.  pN
20dda 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d  ew->pRightmost =
20ddb 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72   0;.  pNew->addr
20ddc 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31  OpenEphm[0] = -1
20ddd 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
20dde 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a  enEphm[1] = -1;.
20ddf 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e    pNew->addrOpen
20de0 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[2] = -1;.  
20de1 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23  return pNew;.}.#
20de2 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56  else.SQLITE_PRIV
20de3 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69  ATE Select *sqli
20de4 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c  te3SelectDup(sql
20de5 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74  ite3 *db, Select
20de6 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b   *p, int flags){
20de7 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20  .  assert( p==0 
20de8 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  );.  return 0;.}
20de9 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
20dea 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e  Add a new elemen
20deb 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  t to the end of 
20dec 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  an expression li
20ded 73 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73  st.  If pList is
20dee 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55  .** initially NU
20def 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  LL, then create 
20df0 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e  a new expression
20df1 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   list..**.** If 
20df2 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
20df3 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ion error occurs
20df4 2c 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73  , the entire lis
20df5 74 20 69 73 20 66 72 65 65 64 20 61 6e 64 0a 2a  t is freed and.*
20df6 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  * NULL is return
20df7 65 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c  ed.  If non-NULL
20df8 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
20df9 65 6e 20 69 74 20 69 73 20 67 75 61 72 61 6e 74  en it is guarant
20dfa 65 65 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  eed.** that the 
20dfb 6e 65 77 20 65 6e 74 72 79 20 77 61 73 20 73 75  new entry was su
20dfc 63 63 65 73 73 66 75 6c 6c 79 20 61 70 70 65 6e  ccessfully appen
20dfd 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ded..*/.SQLITE_P
20dfe 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20  RIVATE ExprList 
20dff 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74  *sqlite3ExprList
20e00 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20  Append(.  Parse 
20e01 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
20e02 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
20e03 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
20e04 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20  st *pList,      
20e05 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69    /* List to whi
20e06 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69  ch to append. Mi
20e07 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  ght be NULL */. 
20e08 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20   Expr *pExpr    
20e09 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72           /* Expr
20e0a 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70  ession to be app
20e0b 65 6e 64 65 64 2e 20 4d 69 67 68 74 20 62 65 20  ended. Might be 
20e0c 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  NULL */.){.  sql
20e0d 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
20e0e 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69  e->db;.  if( pLi
20e0f 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69  st==0 ){.    pLi
20e10 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  st = sqlite3DbMa
20e11 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
20e12 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b  eof(ExprList) );
20e13 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  .    if( pList==
20e14 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
20e15 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20  no_mem;.    }.  
20e16 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
20e17 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20  >nAlloc==0 );.  
20e18 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e  }.  if( pList->n
20e19 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45  Alloc<=pList->nE
20e1a 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63  xpr ){.    struc
20e1b 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
20e1c 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  *a;.    int n = 
20e1d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20  pList->nAlloc*2 
20e1e 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c  + 4;.    a = sql
20e1f 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
20e20 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69  , pList->a, n*si
20e21 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d  zeof(pList->a[0]
20e22 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30  ));.    if( a==0
20e23 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e   ){.      goto n
20e24 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  o_mem;.    }.   
20e25 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20   pList->a = a;. 
20e26 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63     pList->nAlloc
20e27 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
20e28 6f 63 53 69 7a 65 28 64 62 2c 20 61 29 2f 73 69  ocSize(db, a)/si
20e29 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a  zeof(a[0]);.  }.
20e2a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
20e2b 3e 61 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 31  >a!=0 );.  if( 1
20e2c 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45   ){.    struct E
20e2d 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
20e2e 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b  tem = &pList->a[
20e2f 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b  pList->nExpr++];
20e30 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 74 65  .    memset(pIte
20e31 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 49  m, 0, sizeof(*pI
20e32 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 65 6d  tem));.    pItem
20e33 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b  ->pExpr = pExpr;
20e34 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c  .  }.  return pL
20e35 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20  ist;..no_mem:   
20e36 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65    .  /* Avoid le
20e37 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20  aking memory if 
20e38 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65  malloc has faile
20e39 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45  d. */.  sqlite3E
20e3a 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45  xprDelete(db, pE
20e3b 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  xpr);.  sqlite3E
20e3c 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
20e3d 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75  , pList);.  retu
20e3e 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  rn 0;.}../*.** S
20e3f 65 74 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e  et the ExprList.
20e40 61 5b 5d 2e 7a 4e 61 6d 65 20 65 6c 65 6d 65 6e  a[].zName elemen
20e41 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  t of the most re
20e42 63 65 6e 74 6c 79 20 61 64 64 65 64 20 69 74 65  cently added ite
20e43 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 72  m.** on the expr
20e44 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a  ession list..**.
20e45 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 20 62  ** pList might b
20e46 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67  e NULL following
20e47 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20   an OOM error.  
20e48 42 75 74 20 70 4e 61 6d 65 20 73 68 6f 75 6c 64  But pName should
20e49 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 4c   never be.** NUL
20e4a 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  L.  If a memory 
20e4b 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
20e4c 2c 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64 62  , the pParse->db
20e4d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  ->mallocFailed f
20e4e 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a 2a  lag.** is set..*
20e4f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
20e51 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 0a 20 20  rListSetName(.  
20e52 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
20e53 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
20e54 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
20e55 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
20e56 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
20e57 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64 64 20  to which to add 
20e58 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 54  the span. */.  T
20e59 6f 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 20 20 20  oken *pName,    
20e5a 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 74         /* Name t
20e5b 6f 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20  o be added */.  
20e5c 69 6e 74 20 64 65 71 75 6f 74 65 20 20 20 20 20  int dequote     
20e5d 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
20e5e 74 6f 20 63 61 75 73 65 20 74 68 65 20 6e 61 6d  to cause the nam
20e5f 65 20 74 6f 20 62 65 20 64 65 71 75 6f 74 65 64  e to be dequoted
20e60 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   */.){.  assert(
20e61 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20 70 50 61   pList!=0 || pPa
20e62 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
20e63 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 66  ailed!=0 );.  if
20e64 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 73  ( pList ){.    s
20e65 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
20e66 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20  tem *pItem;.    
20e67 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e  assert( pList->n
20e68 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 70 49  Expr>0 );.    pI
20e69 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b  tem = &pList->a[
20e6a 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b  pList->nExpr-1];
20e6b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74  .    assert( pIt
20e6c 65 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 3b 0a  em->zName==0 );.
20e6d 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65      pItem->zName
20e6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
20e6f 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  Dup(pParse->db, 
20e70 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d  pName->z, pName-
20e71 3e 6e 29 3b 0a 20 20 20 20 69 66 28 20 64 65 71  >n);.    if( deq
20e72 75 6f 74 65 20 26 26 20 70 49 74 65 6d 2d 3e 7a  uote && pItem->z
20e73 4e 61 6d 65 20 29 20 73 71 6c 69 74 65 33 44 65  Name ) sqlite3De
20e74 71 75 6f 74 65 28 70 49 74 65 6d 2d 3e 7a 4e 61  quote(pItem->zNa
20e75 6d 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  me);.  }.}../*.*
20e76 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 4c 69  * Set the ExprLi
20e77 73 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65 6c 65  st.a[].zSpan ele
20e78 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74  ment of the most
20e79 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20   recently added 
20e7a 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65  item.** on the e
20e7b 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a  xpression list..
20e7c 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68  **.** pList migh
20e7d 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77  t be NULL follow
20e7e 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72  ing an OOM error
20e7f 2e 20 20 42 75 74 20 70 53 70 61 6e 20 73 68 6f  .  But pSpan sho
20e80 75 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20  uld never be.** 
20e81 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f  NULL.  If a memo
20e82 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
20e83 69 6c 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d  ils, the pParse-
20e84 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
20e85 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74  d flag.** is set
20e86 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20e87 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20e88 45 78 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28  ExprListSetSpan(
20e89 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20e8a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
20e8b 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
20e8c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
20e8d 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  st,        /* Li
20e8e 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61  st to which to a
20e8f 64 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a  dd the span. */.
20e90 20 20 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61    ExprSpan *pSpa
20e91 6e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  n         /* The
20e92 20 73 70 61 6e 20 74 6f 20 62 65 20 61 64 64 65   span to be adde
20e93 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  d */.){.  sqlite
20e94 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
20e95 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  db;.  assert( pL
20e96 69 73 74 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61  ist!=0 || db->ma
20e97 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b  llocFailed!=0 );
20e98 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a  .  if( pList ){.
20e99 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
20e9a 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
20e9b 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73  = &pList->a[pLis
20e9c 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20  t->nExpr-1];.   
20e9d 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e   assert( pList->
20e9e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 61  nExpr>0 );.    a
20e9f 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f  ssert( db->mallo
20ea0 63 46 61 69 6c 65 64 20 7c 7c 20 70 49 74 65 6d  cFailed || pItem
20ea1 2d 3e 70 45 78 70 72 3d 3d 70 53 70 61 6e 2d 3e  ->pExpr==pSpan->
20ea2 70 45 78 70 72 20 29 3b 0a 20 20 20 20 73 71 6c  pExpr );.    sql
20ea3 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
20ea4 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20  Item->zSpan);.  
20ea5 20 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d    pItem->zSpan =
20ea6 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75   sqlite3DbStrNDu
20ea7 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 53 70  p(db, (char*)pSp
20ea8 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 20 20  an->zStart,.    
20ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20eaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20eab 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e  (int)(pSpan->zEn
20eac 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72  d - pSpan->zStar
20ead 74 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  t));.  }.}../*.*
20eae 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
20eaf 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20  ion list pEList 
20eb0 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68  contains more th
20eb1 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e  an iLimit elemen
20eb2 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20  ts,.** leave an 
20eb3 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
20eb4 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49   pParse..*/.SQLI
20eb5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
20eb6 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43  sqlite3ExprListC
20eb7 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61  heckLength(.  Pa
20eb8 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45  rse *pParse,.  E
20eb9 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
20eba 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
20ebb 4f 62 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20  Object.){.  int 
20ebc 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  mx = pParse->db-
20ebd 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
20ebe 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20  IMIT_COLUMN];.  
20ebf 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74  testcase( pEList
20ec0 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
20ec1 72 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63  r==mx );.  testc
20ec2 61 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70  ase( pEList && p
20ec3 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78  EList->nExpr==mx
20ec4 2b 31 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69  +1 );.  if( pELi
20ec5 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45  st && pEList->nE
20ec6 78 70 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71  xpr>mx ){.    sq
20ec7 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
20ec8 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
20ec9 63 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20  columns in %s", 
20eca 7a 4f 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a  zObject);.  }.}.
20ecb 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
20ecc 20 65 6e 74 69 72 65 20 65 78 70 72 65 73 73 69   entire expressi
20ecd 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49  on list..*/.SQLI
20ece 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
20ecf 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
20ed0 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
20ed1 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  b, ExprList *pLi
20ed2 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  st){.  int i;.  
20ed3 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
20ed4 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
20ed5 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
20ed6 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
20ed7 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28  pList->a!=0 || (
20ed8 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20  pList->nExpr==0 
20ed9 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63  && pList->nAlloc
20eda 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ==0) );.  assert
20edb 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d  ( pList->nExpr<=
20edc 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b  pList->nAlloc );
20edd 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69  .  for(pItem=pLi
20ede 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c  st->a, i=0; i<pL
20edf 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  ist->nExpr; i++,
20ee0 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73   pItem++){.    s
20ee1 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
20ee2 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  (db, pItem->pExp
20ee3 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  r);.    sqlite3D
20ee4 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d  bFree(db, pItem-
20ee5 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c  >zName);.    sql
20ee6 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
20ee7 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20  Item->zSpan);.  
20ee8 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
20ee9 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b  e(db, pList->a);
20eea 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
20eeb 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a  (db, pList);.}..
20eec 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  /*.** These rout
20eed 69 6e 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20  ines are Walker 
20eee 63 61 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b  callbacks.  Walk
20eef 65 72 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69  er.u.pi is a poi
20ef0 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e  nter.** to an in
20ef1 74 65 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f  teger.  These ro
20ef2 75 74 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b  utines are check
20ef3 69 6e 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ing an expressio
20ef4 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66 20 69  n to see.** if i
20ef5 74 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e  t is a constant.
20ef6 20 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e    Set *Walker.u.
20ef7 70 69 20 74 6f 20 30 20 69 66 20 74 68 65 20 65  pi to 0 if the e
20ef8 78 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20  xpression is.** 
20ef9 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a  not constant..**
20efa 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 62 61  .** These callba
20efb 63 6b 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  ck routines are 
20efc 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
20efd 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  t the following:
20efe 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  .**.**     sqlit
20eff 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
20f00 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
20f01 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
20f02 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 20 20  otJoin().**     
20f03 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
20f04 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28  stantOrFunction(
20f05 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ).**.*/.static i
20f06 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e  nt exprNodeIsCon
20f07 73 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57  stant(Walker *pW
20f08 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
20f09 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 70 57  pr){..  /* If pW
20f0a 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 20 33 20  alker->u.i is 3 
20f0b 74 68 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66  then any term of
20f0c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20f0d 74 68 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a  that comes from.
20f0e 20 20 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55    ** the ON or U
20f0f 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20  SING clauses of 
20f10 61 20 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66  a join disqualif
20f11 69 65 73 20 74 68 65 20 65 78 70 72 65 73 73 69  ies the expressi
20f12 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69  on.  ** from bei
20f13 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f  ng considered co
20f14 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28  nstant. */.  if(
20f15 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33   pWalker->u.i==3
20f16 20 26 26 20 45 78 70 72 48 61 73 41 6e 79 50 72   && ExprHasAnyPr
20f17 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
20f18 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20  _FromJoin) ){.  
20f19 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d    pWalker->u.i =
20f1a 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   0;.    return W
20f1b 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20  RC_Abort;.  }.. 
20f1c 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
20f1d 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e  op ){.    /* Con
20f1e 73 69 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  sider functions 
20f1f 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 69  to be constant i
20f20 66 20 61 6c 6c 20 74 68 65 69 72 20 61 72 67 75  f all their argu
20f21 6d 65 6e 74 73 20 61 72 65 20 63 6f 6e 73 74 61  ments are consta
20f22 6e 74 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 57  nt.    ** and pW
20f23 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f  alker->u.i==2 */
20f24 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e  .    case TK_FUN
20f25 43 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28  CTION:.      if(
20f26 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32   pWalker->u.i==2
20f27 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
20f28 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75     /* Fall throu
20f29 67 68 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54  gh */.    case T
20f2a 4b 5f 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54  K_ID:.    case T
20f2b 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61  K_COLUMN:.    ca
20f2c 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  se TK_AGG_FUNCTI
20f2d 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ON:.    case TK_
20f2e 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20  AGG_COLUMN:.    
20f2f 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
20f30 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a  r->op==TK_ID );.
20f31 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
20f32 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pExpr->op==TK_CO
20f33 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65  LUMN );.      te
20f34 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
20f35 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  p==TK_AGG_FUNCTI
20f36 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ON );.      test
20f37 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
20f38 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29  =TK_AGG_COLUMN )
20f39 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 72 2d  ;.      pWalker-
20f3a 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20  >u.i = 0;.      
20f3b 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
20f3c 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20  ;.    default:. 
20f3d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
20f3e 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c  Expr->op==TK_SEL
20f3f 45 43 54 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74  ECT ); /* select
20f40 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77  NodeIsConstant w
20f41 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a  ill disallow */.
20f42 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
20f43 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58  pExpr->op==TK_EX
20f44 49 53 54 53 20 29 3b 20 2f 2a 20 73 65 6c 65 63  ISTS ); /* selec
20f45 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20  tNodeIsConstant 
20f46 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f  will disallow */
20f47 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52  .      return WR
20f48 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a  C_Continue;.  }.
20f49 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c  }.static int sel
20f4a 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e  ectNodeIsConstan
20f4b 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  t(Walker *pWalke
20f4c 72 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73  r, Select *NotUs
20f4d 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
20f4e 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
20f4f 3b 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69  ;.  pWalker->u.i
20f50 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57   = 0;.  return W
20f51 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74  RC_Abort;.}.stat
20f52 69 63 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e  ic int exprIsCon
20f53 73 74 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  st(Expr *p, int 
20f54 69 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c  initFlag){.  Wal
20f55 6b 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d  ker w;.  w.u.i =
20f56 20 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78   initFlag;.  w.x
20f57 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65  ExprCallback = e
20f58 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e  xprNodeIsConstan
20f59 74 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  t;.  w.xSelectCa
20f5a 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e  llback = selectN
20f5b 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20  odeIsConstant;. 
20f5c 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
20f5d 28 26 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72  (&w, p);.  retur
20f5e 6e 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  n w.u.i;.}../*.*
20f5f 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73  * Walk an expres
20f60 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75  sion tree.  Retu
20f61 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72  rn 1 if the expr
20f62 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61  ession is consta
20f63 6e 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69  nt.** and 0 if i
20f64 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61  t involves varia
20f65 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e  bles or function
20f66 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f   calls..**.** Fo
20f67 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
20f68 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  f this function,
20f69 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64   a double-quoted
20f6a 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62   string (ex: "ab
20f6b 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64  c").** is consid
20f6c 65 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20  ered a variable 
20f6d 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f  but a single-quo
20f6e 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20  ted string (ex: 
20f6f 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63  'abc') is.** a c
20f70 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  onstant..*/.SQLI
20f71 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
20f72 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
20f73 74 61 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20  tant(Expr *p){. 
20f74 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f   return exprIsCo
20f75 6e 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a  nst(p, 1);.}../*
20f76 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72  .** Walk an expr
20f77 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65  ession tree.  Re
20f78 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78  turn 1 if the ex
20f79 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73  pression is cons
20f7a 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65  tant.** that doe
20f7b 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66  s no originate f
20f7c 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53  rom the ON or US
20f7d 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61  ING clauses of a
20f7e 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e   join..** Return
20f7f 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65   0 if it involve
20f80 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66  s variables or f
20f81 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72  unction calls or
20f82 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61   terms from.** a
20f83 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  n ON or USING cl
20f84 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ause..*/.SQLITE_
20f85 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
20f86 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
20f87 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70  tNotJoin(Expr *p
20f88 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72  ){.  return expr
20f89 49 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d  IsConst(p, 3);.}
20f8a 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20  ../*.** Walk an 
20f8b 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
20f8c 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68    Return 1 if th
20f8d 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
20f8e 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61  constant.** or a
20f8f 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77   function call w
20f90 69 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67  ith constant arg
20f91 75 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20  uments.  Return 
20f92 61 6e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a  and 0 if there.*
20f93 2a 20 61 72 65 20 61 6e 79 20 76 61 72 69 61 62  * are any variab
20f94 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  les..**.** For t
20f95 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74  he purposes of t
20f96 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20  his function, a 
20f97 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74  double-quoted st
20f98 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29  ring (ex: "abc")
20f99 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65  .** is considere
20f9a 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74  d a variable but
20f9b 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64   a single-quoted
20f9c 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62   string (ex: 'ab
20f9d 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73  c') is.** a cons
20f9e 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tant..*/.SQLITE_
20f9f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
20fa0 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
20fa1 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72  tOrFunction(Expr
20fa2 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65   *p){.  return e
20fa3 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29  xprIsConst(p, 2)
20fa4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
20fa5 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63  e expression p c
20fa6 6f 64 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20  odes a constant 
20fa7 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20  integer that is 
20fa8 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20  small enough.** 
20fa9 74 6f 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62  to fit in a 32-b
20faa 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75  it integer, retu
20fab 72 6e 20 31 20 61 6e 64 20 70 75 74 20 74 68 65  rn 1 and put the
20fac 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e   value of the in
20fad 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61  teger.** in *pVa
20fae 6c 75 65 2e 20 20 49 66 20 74 68 65 20 65 78 70  lue.  If the exp
20faf 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61  ression is not a
20fb0 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20  n integer or if 
20fb1 69 74 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a  it is too big.**
20fb2 20 74 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67   to fit in a sig
20fb3 6e 65 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67  ned 32-bit integ
20fb4 65 72 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64  er, return 0 and
20fb5 20 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75   leave *pValue u
20fb6 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c  nchanged..*/.SQL
20fb7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
20fb8 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74  sqlite3ExprIsInt
20fb9 65 67 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e  eger(Expr *p, in
20fba 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e  t *pValue){.  in
20fbb 74 20 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20  t rc = 0;.  if( 
20fbc 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e  p->flags & EP_In
20fbd 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70  tValue ){.    *p
20fbe 56 61 6c 75 65 20 3d 20 70 2d 3e 75 2e 69 56 61  Value = p->u.iVa
20fbf 6c 75 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  lue;.    return 
20fc0 31 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28  1;.  }.  switch(
20fc1 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61   p->op ){.    ca
20fc2 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b  se TK_INTEGER: {
20fc3 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
20fc4 74 65 33 47 65 74 49 6e 74 33 32 28 70 2d 3e 75  te3GetInt32(p->u
20fc5 2e 7a 54 6f 6b 65 6e 2c 20 70 56 61 6c 75 65 29  .zToken, pValue)
20fc6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
20fc7 72 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  rc==0 );.      b
20fc8 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
20fc9 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b  case TK_UPLUS: {
20fca 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
20fcb 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
20fcc 28 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75  (p->pLeft, pValu
20fcd 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  e);.      break;
20fce 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
20fcf 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20  TK_UMINUS: {.   
20fd0 20 20 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20     int v;.      
20fd1 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49  if( sqlite3ExprI
20fd2 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66  sInteger(p->pLef
20fd3 74 2c 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20  t, &v) ){.      
20fd4 20 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a    *pValue = -v;.
20fd5 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a          rc = 1;.
20fd6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
20fd7 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
20fd8 65 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20  efault: break;. 
20fd9 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20   }.  if( rc ){. 
20fda 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72 48     assert( ExprH
20fdb 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c  asAnyProperty(p,
20fdc 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54   EP_Reduced|EP_T
20fdd 6f 6b 65 6e 4f 6e 6c 79 29 0a 20 20 20 20 20 20  okenOnly).      
20fde 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 2d 3e           || (p->
20fdf 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c  flags2 & EP2_Mal
20fe0 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29  locedToken)==0 )
20fe1 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b  ;.    p->op = TK
20fe2 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 2d  _INTEGER;.    p-
20fe3 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74  >flags |= EP_Int
20fe4 56 61 6c 75 65 3b 0a 20 20 20 20 70 2d 3e 75 2e  Value;.    p->u.
20fe5 69 56 61 6c 75 65 20 3d 20 2a 70 56 61 6c 75 65  iValue = *pValue
20fe6 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
20fe7 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
20fe8 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 72  rn FALSE if ther
20fe9 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74  e is no chance t
20fea 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69  hat the expressi
20feb 6f 6e 20 63 61 6e 20 62 65 20 4e 55 4c 4c 2e 0a  on can be NULL..
20fec 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
20fed 72 65 73 73 69 6f 6e 20 6d 69 67 68 74 20 62 65  ression might be
20fee 20 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20   NULL or if the 
20fef 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 6f  expression is to
20ff0 6f 20 63 6f 6d 70 6c 65 78 0a 2a 2a 20 74 6f 20  o complex.** to 
20ff1 74 65 6c 6c 20 72 65 74 75 72 6e 20 54 52 55 45  tell return TRUE
20ff2 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  .  .**.** This r
20ff3 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 61  outine is used a
20ff4 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
20ff5 6e 2c 20 74 6f 20 73 6b 69 70 20 4f 50 5f 49 73  n, to skip OP_Is
20ff6 4e 75 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a 20  Null opcodes.** 
20ff7 77 68 65 6e 20 77 65 20 6b 6e 6f 77 20 74 68 61  when we know tha
20ff8 74 20 61 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74  t a value cannot
20ff9 20 62 65 20 4e 55 4c 4c 2e 20 20 48 65 6e 63 65   be NULL.  Hence
20ffa 2c 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 69  , a false positi
20ffb 76 65 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67  ve.** (returning
20ffc 20 54 52 55 45 20 77 68 65 6e 20 69 6e 20 66 61   TRUE when in fa
20ffd 63 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  ct the expressio
20ffe 6e 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 4e  n can never be N
20fff 55 4c 4c 29 20 6d 69 67 68 74 0a 2a 2a 20 62 65  ULL) might.** be
21000 20 61 20 73 6d 61 6c 6c 20 70 65 72 66 6f 72 6d   a small perform
21001 61 6e 63 65 20 68 69 74 20 62 75 74 20 69 73 20  ance hit but is 
21002 6f 74 68 65 72 77 69 73 65 20 68 61 72 6d 6c 65  otherwise harmle
21003 73 73 2e 20 20 4f 6e 20 74 68 65 20 6f 74 68 65  ss.  On the othe
21004 72 0a 2a 2a 20 68 61 6e 64 2c 20 61 20 66 61 6c  r.** hand, a fal
21005 73 65 20 6e 65 67 61 74 69 76 65 20 28 72 65 74  se negative (ret
21006 75 72 6e 69 6e 67 20 46 41 4c 53 45 20 77 68 65  urning FALSE whe
21007 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 75  n the result cou
21008 6c 64 20 62 65 20 4e 55 4c 4c 29 0a 2a 2a 20 77  ld be NULL).** w
21009 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 73 75 6c  ill likely resul
2100a 74 20 69 6e 20 61 6e 20 69 6e 63 6f 72 72 65 63  t in an incorrec
2100b 74 20 61 6e 73 77 65 72 2e 20 20 53 6f 20 77 68  t answer.  So wh
2100c 65 6e 20 69 6e 20 64 6f 75 62 74 2c 20 72 65 74  en in doubt, ret
2100d 75 72 6e 0a 2a 2a 20 54 52 55 45 2e 0a 2a 2f 0a  urn.** TRUE..*/.
2100e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2100f 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  nt sqlite3ExprCa
21010 6e 42 65 4e 75 6c 6c 28 63 6f 6e 73 74 20 45 78  nBeNull(const Ex
21011 70 72 20 2a 70 29 7b 0a 20 20 75 38 20 6f 70 3b  pr *p){.  u8 op;
21012 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d  .  while( p->op=
21013 3d 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 70 2d 3e  =TK_UPLUS || p->
21014 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 7b  op==TK_UMINUS ){
21015 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 20 7d   p = p->pLeft; }
21016 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20  .  op = p->op;. 
21017 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49   if( op==TK_REGI
21018 53 54 45 52 20 29 20 6f 70 20 3d 20 70 2d 3e 6f  STER ) op = p->o
21019 70 32 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  p2;.  switch( op
2101a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
2101b 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73  INTEGER:.    cas
2101c 65 20 54 4b 5f 53 54 52 49 4e 47 3a 0a 20 20 20  e TK_STRING:.   
2101d 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a   case TK_FLOAT:.
2101e 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42      case TK_BLOB
2101f 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  :.      return 0
21020 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20  ;.    default:. 
21021 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
21022 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65   }.}../*.** Gene
21023 72 61 74 65 20 61 6e 20 4f 50 5f 49 73 4e 75 6c  rate an OP_IsNul
21024 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  l instruction th
21025 61 74 20 74 65 73 74 73 20 72 65 67 69 73 74 65  at tests registe
21026 72 20 69 52 65 67 20 61 6e 64 20 6a 75 6d 70 73  r iReg and jumps
21027 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20  .** to location 
21028 69 44 65 73 74 20 69 66 20 74 68 65 20 76 61 6c  iDest if the val
21029 75 65 20 69 6e 20 69 52 65 67 20 69 73 20 4e 55  ue in iReg is NU
2102a 4c 4c 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69  LL.  The value i
2102b 6e 20 69 52 65 67 20 0a 2a 2a 20 77 61 73 20 63  n iReg .** was c
2102c 6f 6d 70 75 74 65 64 20 62 79 20 70 45 78 70 72  omputed by pExpr
2102d 2e 20 20 49 66 20 77 65 20 63 61 6e 20 6c 6f 6f  .  If we can loo
2102e 6b 20 61 74 20 70 45 78 70 72 20 61 74 20 63 6f  k at pExpr at co
2102f 6d 70 69 6c 65 2d 74 69 6d 65 20 61 6e 64 0a 2a  mpile-time and.*
21030 2a 20 64 65 74 65 72 6d 69 6e 65 20 74 68 61 74  * determine that
21031 20 69 74 20 63 61 6e 20 6e 65 76 65 72 20 67 65   it can never ge
21032 6e 65 72 61 74 65 20 61 20 4e 55 4c 4c 2c 20 74  nerate a NULL, t
21033 68 65 6e 20 74 68 65 20 4f 50 5f 49 73 4e 75 6c  hen the OP_IsNul
21034 6c 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 63  l operation.** c
21035 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 0a 2a  an be omitted..*
21036 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21037 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
21038 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28  rCodeIsNullJump(
21039 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20  .  Vdbe *v,     
2103a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
2103b 42 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  BE under constru
2103c 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
2103d 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 2f   Expr *pExpr,  /
2103e 2a 20 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65 20  * Only generate 
2103f 4f 50 5f 49 73 4e 75 6c 6c 20 69 66 20 74 68 69  OP_IsNull if thi
21040 73 20 65 78 70 72 20 63 61 6e 20 62 65 20 4e 55  s expr can be NU
21041 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67  LL */.  int iReg
21042 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
21043 65 73 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e  est the value in
21044 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 66   this register f
21045 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74  or NULL */.  int
21046 20 69 44 65 73 74 20 20 20 20 20 20 20 20 20 20   iDest          
21047 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66   /* Jump here if
21048 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75   the value is nu
21049 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 73  ll */.){.  if( s
2104a 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e  qlite3ExprCanBeN
2104b 75 6c 6c 28 70 45 78 70 72 29 20 29 7b 0a 20 20  ull(pExpr) ){.  
2104c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2104d 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c  Op2(v, OP_IsNull
2104e 2c 20 69 52 65 67 2c 20 69 44 65 73 74 29 3b 0a  , iReg, iDest);.
2104f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
21050 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
21051 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e  given expression
21052 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 77   is a constant w
21053 68 69 63 68 20 77 6f 75 6c 64 20 62 65 0a 2a 2a  hich would be.**
21054 20 75 6e 63 68 61 6e 67 65 64 20 62 79 20 4f 50   unchanged by OP
21055 5f 41 66 66 69 6e 69 74 79 20 77 69 74 68 20 74  _Affinity with t
21056 68 65 20 61 66 66 69 6e 69 74 79 20 67 69 76 65  he affinity give
21057 6e 20 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 0a  n in the second.
21058 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  ** argument..**.
21059 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2105a 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  is used to deter
2105b 6d 69 6e 65 20 69 66 20 74 68 65 20 4f 50 5f 41  mine if the OP_A
2105c 66 66 69 6e 69 74 79 20 6f 70 65 72 61 74 69 6f  ffinity operatio
2105d 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 6d 69 74  n.** can be omit
2105e 74 65 64 2e 20 20 57 68 65 6e 20 69 6e 20 64 6f  ted.  When in do
2105f 75 62 74 20 72 65 74 75 72 6e 20 46 41 4c 53 45  ubt return FALSE
21060 2e 20 20 41 20 66 61 6c 73 65 20 6e 65 67 61 74  .  A false negat
21061 69 76 65 0a 2a 2a 20 69 73 20 68 61 72 6d 6c 65  ive.** is harmle
21062 73 73 2e 20 20 41 20 66 61 6c 73 65 20 70 6f 73  ss.  A false pos
21063 69 74 69 76 65 2c 20 68 6f 77 65 76 65 72 2c 20  itive, however, 
21064 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 74 68  can result in th
21065 65 20 77 72 6f 6e 67 0a 2a 2a 20 61 6e 73 77 65  e wrong.** answe
21066 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
21067 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21068 45 78 70 72 4e 65 65 64 73 4e 6f 41 66 66 69 6e  ExprNeedsNoAffin
21069 69 74 79 43 68 61 6e 67 65 28 63 6f 6e 73 74 20  ityChange(const 
2106a 45 78 70 72 20 2a 70 2c 20 63 68 61 72 20 61 66  Expr *p, char af
2106b 66 29 7b 0a 20 20 75 38 20 6f 70 3b 0a 20 20 69  f){.  u8 op;.  i
2106c 66 28 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41  f( aff==SQLITE_A
2106d 46 46 5f 4e 4f 4e 45 20 29 20 72 65 74 75 72 6e  FF_NONE ) return
2106e 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e   1;.  while( p->
2106f 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 7c 7c 20  op==TK_UPLUS || 
21070 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53  p->op==TK_UMINUS
21071 20 29 7b 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74   ){ p = p->pLeft
21072 3b 20 7d 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70  ; }.  op = p->op
21073 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52  ;.  if( op==TK_R
21074 45 47 49 53 54 45 52 20 29 20 6f 70 20 3d 20 70  EGISTER ) op = p
21075 2d 3e 6f 70 32 3b 0a 20 20 73 77 69 74 63 68 28  ->op2;.  switch(
21076 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
21077 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20  TK_INTEGER: {.  
21078 20 20 20 20 72 65 74 75 72 6e 20 61 66 66 3d 3d      return aff==
21079 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
2107a 45 52 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54  ER || aff==SQLIT
2107b 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20  E_AFF_NUMERIC;. 
2107c 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
2107d 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20  _FLOAT: {.      
2107e 72 65 74 75 72 6e 20 61 66 66 3d 3d 53 51 4c 49  return aff==SQLI
2107f 54 45 5f 41 46 46 5f 52 45 41 4c 20 7c 7c 20 61  TE_AFF_REAL || a
21080 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
21081 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 0a 20 20  UMERIC;.    }.  
21082 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47    case TK_STRING
21083 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  : {.      return
21084 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46   aff==SQLITE_AFF
21085 5f 54 45 58 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _TEXT;.    }.   
21086 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b   case TK_BLOB: {
21087 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
21088 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21089 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20  TK_COLUMN: {.   
2108a 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 54     assert( p->iT
2108b 61 62 6c 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 70  able>=0 );  /* p
2108c 20 63 61 6e 6e 6f 74 20 62 65 20 70 61 72 74 20   cannot be part 
2108d 6f 66 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74  of a CHECK const
2108e 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 72  raint */.      r
2108f 65 74 75 72 6e 20 70 2d 3e 69 43 6f 6c 75 6d 6e  eturn p->iColumn
21090 3c 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  <0.          && 
21091 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46  (aff==SQLITE_AFF
21092 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 3d  _INTEGER || aff=
21093 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
21094 52 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  RIC);.    }.    
21095 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
21096 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
21097 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
21098 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
21099 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73   given string is
2109a 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e   a row-id column
2109b 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   name..*/.SQLITE
2109c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2109d 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73  ite3IsRowid(cons
2109e 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66  t char *z){.  if
2109f 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
210a0 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d  (z, "_ROWID_")==
210a1 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
210a2 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
210a3 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d  mp(z, "ROWID")==
210a4 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
210a5 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
210a6 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20  mp(z, "OID")==0 
210a7 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65  ) return 1;.  re
210a8 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
210a9 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
210aa 77 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 74  we are able to t
210ab 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 6f  he IN operator o
210ac 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61  ptimization on a
210ad 0a 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 65  .** query of the
210ae 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20   form.**.**     
210af 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e    x IN (SELECT .
210b0 2e 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20  ..).**.** Where 
210b1 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 63 6c  the SELECT... cl
210b2 61 75 73 65 20 69 73 20 61 73 20 73 70 65 63 69  ause is as speci
210b3 66 69 65 64 20 62 79 20 74 68 65 20 70 61 72 61  fied by the para
210b4 6d 65 74 65 72 20 74 6f 20 74 68 69 73 0a 2a 2a  meter to this.**
210b5 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
210b6 54 68 65 20 53 65 6c 65 63 74 20 6f 62 6a 65 63  The Select objec
210b7 74 20 70 61 73 73 65 64 20 69 6e 20 68 61 73 20  t passed in has 
210b8 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72 65  already been pre
210b9 70 72 6f 63 65 73 73 65 64 20 61 6e 64 20 6e 6f  processed and no
210ba 0a 2a 2a 20 65 72 72 6f 72 73 20 68 61 76 65 20  .** errors have 
210bb 62 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23  been found..*/.#
210bc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
210bd 49 54 5f 53 55 42 51 55 45 52 59 0a 73 74 61 74  IT_SUBQUERY.stat
210be 69 63 20 69 6e 74 20 69 73 43 61 6e 64 69 64 61  ic int isCandida
210bf 74 65 46 6f 72 49 6e 4f 70 74 28 53 65 6c 65 63  teForInOpt(Selec
210c0 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74  t *p){.  SrcList
210c1 20 2a 70 53 72 63 3b 0a 20 20 45 78 70 72 4c 69   *pSrc;.  ExprLi
210c2 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 54 61  st *pEList;.  Ta
210c3 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 66 28  ble *pTab;.  if(
210c4 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
210c5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
210c6 20 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61 6e      /* right-han
210c7 64 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73 20  d side of IN is 
210c8 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20  SELECT */.  if( 
210c9 70 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74 75  p->pPrior ) retu
210ca 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
210cb 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d 70     /* Not a comp
210cc 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0a 20  ound SELECT */. 
210cd 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73   if( p->selFlags
210ce 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c   & (SF_Distinct|
210cf 53 46 5f 41 67 67 72 65 67 61 74 65 29 20 29 7b  SF_Aggregate) ){
210d0 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28  .    testcase( (
210d1 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53  p->selFlags & (S
210d2 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67  F_Distinct|SF_Ag
210d3 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69  gregate))==SF_Di
210d4 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 74 65  stinct );.    te
210d5 73 74 63 61 73 65 28 20 28 70 2d 3e 73 65 6c 46  stcase( (p->selF
210d6 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69  lags & (SF_Disti
210d7 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65  nct|SF_Aggregate
210d8 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65  ))==SF_Aggregate
210d9 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   );.    return 0
210da 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 43 54  ; /* No DISTINCT
210db 20 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e 6f 20   keyword and no 
210dc 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
210dd 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  ons */.  }.  ass
210de 65 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79  ert( p->pGroupBy
210df 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20  ==0 );          
210e0 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 47 52      /* Has no GR
210e1 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  OUP BY clause */
210e2 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74  .  if( p->pLimit
210e3 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
210e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
210e5 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65   no LIMIT clause
210e6 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
210e7 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20  >pOffset==0 );  
210e8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
210e9 4e 6f 20 4c 49 4d 49 54 20 6d 65 61 6e 73 20 6e  No LIMIT means n
210ea 6f 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 69 66  o OFFSET */.  if
210eb 28 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65  ( p->pWhere ) re
210ec 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
210ed 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57       /* Has no W
210ee 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
210ef 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b   pSrc = p->pSrc;
210f0 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21  .  assert( pSrc!
210f1 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63  =0 );.  if( pSrc
210f2 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75  ->nSrc!=1 ) retu
210f3 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f  rn 0;          /
210f4 2a 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e  * Single term in
210f5 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
210f6 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d    if( pSrc->a[0]
210f7 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72  .pSelect ) retur
210f8 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d  n 0;     /* FROM
210f9 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71 75 65   is not a subque
210fa 72 79 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20  ry or view */.  
210fb 70 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30  pTab = pSrc->a[0
210fc 5d 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 45  ].pTab;.  if( NE
210fd 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72  VER(pTab==0) ) r
210fe 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72  eturn 0;.  asser
210ff 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  t( pTab->pSelect
21100 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20  ==0 );          
21101 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
21102 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a   is not a view *
21103 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  /.  if( IsVirtua
21104 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  l(pTab) ) return
21105 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 52   0;        /* FR
21106 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 20  OM clause not a 
21107 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
21108 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70  .  pEList = p->p
21109 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c  EList;.  if( pEL
2110a 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20  ist->nExpr!=1 ) 
2110b 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
2110c 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e  /* One column in
2110d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2110e 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  */.  if( pEList-
2110f 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21  >a[0].pExpr->op!
21110 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74  =TK_COLUMN ) ret
21111 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c 74  urn 0; /* Result
21112 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   is a column */.
21113 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
21114 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
21115 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a  MIT_SUBQUERY */.
21116 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
21117 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20  tion is used by 
21118 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
21119 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e 2e  on of the IN (..
2111a 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20  .) operator..** 
2111b 49 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 66  It's job is to f
2111c 69 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 20  ind or create a 
2111d 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65  b-tree structure
2111e 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65   that may be use
2111f 64 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 74  d.** either to t
21120 65 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 68  est for membersh
21121 69 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 20  ip of the (...) 
21122 73 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 74  set or to iterat
21123 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 73  e through.** its
21124 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69   members, skippi
21125 6e 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a  ng duplicates..*
21126 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 6f  *.** The index o
21127 66 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70 65  f the cursor ope
21128 6e 65 64 20 6f 6e 20 74 68 65 20 62 2d 74 72 65  ned on the b-tre
21129 65 20 28 64 61 74 61 62 61 73 65 20 74 61 62 6c  e (database tabl
2112a 65 2c 20 64 61 74 61 62 61 73 65 20 69 6e 64 65  e, database inde
2112b 78 20 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61  x .** or epherma
2112c 6c 20 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72  l table) is stor
2112d 65 64 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65  ed in pX->iTable
2112e 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e   before this fun
2112f 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a  ction returns..*
21130 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76  * The returned v
21131 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e  alue of this fun
21132 63 74 69 6f 6e 20 69 6e 64 69 63 61 74 65 73 20  ction indicates 
21133 74 68 65 20 62 2d 74 72 65 65 20 74 79 70 65 2c  the b-tree type,
21134 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
21135 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  **   IN_INDEX_RO
21136 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72  WID - The cursor
21137 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61   was opened on a
21138 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e   database table.
21139 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49  .**   IN_INDEX_I
2113a 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f  NDEX - The curso
2113b 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  r was opened on 
2113c 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  a database index
2113d 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f  ..**   IN_INDEX_
2113e 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73  EPH -   The curs
2113f 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e  or was opened on
21140 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65   a specially cre
21141 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20  ated and.**     
21142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
21143 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d  opulated epherem
21144 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
21145 41 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74 72  An existing b-tr
21146 65 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75  ee may only be u
21147 73 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43  sed if the SELEC
21148 54 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70  T is of the simp
21149 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a  le.** form:.**.*
2114a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f  *     SELECT <co
2114b 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c  lumn> FROM <tabl
2114c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e>.**.** If the 
2114d 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d  prNotFound param
2114e 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20  eter is 0, then 
2114f 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20  the b-tree will 
21150 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61  be used to itera
21151 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68  te.** through th
21152 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 73  e set members, s
21153 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70 6c  kipping any dupl
21154 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73 20  icates. In this 
21155 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72  case an.** epher
21156 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20  emal table must 
21157 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74  be used unless t
21158 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c  he selected <col
21159 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 65  umn> is guarante
2115a 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71  ed.** to be uniq
2115b 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 63 61  ue - either beca
2115c 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e 54  use it is an INT
2115d 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
2115e 20 6f 72 20 69 74 0a 2a 2a 20 68 61 73 20 61 20   or it.** has a 
2115f 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e  UNIQUE constrain
21160 74 20 6f 72 20 55 4e 49 51 55 45 20 69 6e 64 65  t or UNIQUE inde
21161 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  x..**.** If the 
21162 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d  prNotFound param
21163 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74  eter is not 0, t
21164 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 77  hen the b-tree w
21165 69 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20  ill be used .** 
21166 66 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d  for fast set mem
21167 62 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 49  bership tests. I
21168 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65  n this case an e
21169 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d  pheremal table m
2116a 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20  ust .** be used 
2116b 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20  unless <column> 
2116c 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  is an INTEGER PR
2116d 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20  IMARY KEY or an 
2116e 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65  index can .** be
2116f 20 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c   found with <col
21170 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74  umn> as its left
21171 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a  -most column..**
21172 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d 74  .** When the b-t
21173 72 65 65 20 69 73 20 62 65 69 6e 67 20 75 73 65  ree is being use
21174 64 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70  d for membership
21175 20 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c 6c   tests, the call
21176 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ing function.** 
21177 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68  needs to know wh
21178 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
21179 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
2117a 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20  ins an SQL NULL 
2117b 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 64  .** value in ord
2117c 65 72 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 20  er to correctly 
2117d 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73  evaluate express
2117e 69 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e 20  ions like "X IN 
2117f 28 59 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74  (Y, Z)"..** If t
21180 68 65 72 65 20 69 73 20 61 6e 79 20 63 68 61 6e  here is any chan
21181 63 65 20 74 68 61 74 20 74 68 65 20 28 2e 2e 2e  ce that the (...
21182 29 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20  ) might contain 
21183 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a  a NULL value at.
21184 2a 2a 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e  ** runtime, then
21185 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61   a register is a
21186 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65  llocated and the
21187 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
21188 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a   written.** to *
21189 70 72 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74  prNotFound. If t
2118a 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63  here is no chanc
2118b 65 20 74 68 61 74 20 74 68 65 20 28 2e 2e 2e 29  e that the (...)
2118c 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e   contains a.** N
2118d 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e 20  ULL value, then 
2118e 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c  *prNotFound is l
2118f 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a  eft unchanged..*
21190 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74  *.** If a regist
21191 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  er is allocated 
21192 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e  and its location
21193 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f   stored in *prNo
21194 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20  tFound, then.** 
21195 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c 75  its initial valu
21196 65 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20 74  e is NULL.  If t
21197 68 65 20 28 2e 2e 2e 29 20 64 6f 65 73 20 6e 6f  he (...) does no
21198 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e  t remain constan
21199 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72  t.** for the dur
2119a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65  ation of the que
2119b 72 79 20 28 69 2e 65 2e 20 74 68 65 20 53 45 4c  ry (i.e. the SEL
2119c 45 43 54 20 77 69 74 68 69 6e 20 74 68 65 20 28  ECT within the (
2119d 2e 2e 2e 29 0a 2a 2a 20 69 73 20 61 20 63 6f 72  ...).** is a cor
2119e 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79  related subquery
2119f 29 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  ) then the value
211a0 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65   of the allocate
211a1 64 20 72 65 67 69 73 74 65 72 20 69 73 0a 2a 2a  d register is.**
211a2 20 72 65 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65   reset to NULL e
211a3 61 63 68 20 74 69 6d 65 20 74 68 65 20 73 75 62  ach time the sub
211a4 71 75 65 72 79 20 69 73 20 72 65 72 75 6e 2e 20  query is rerun. 
211a5 54 68 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 0a  This allows the.
211a6 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65  ** caller to use
211a7 20 76 64 62 65 20 63 6f 64 65 20 65 71 75 69 76   vdbe code equiv
211a8 61 6c 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c  alent to the fol
211a9 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
211aa 69 66 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55  if( register==NU
211ab 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73  LL ){.**     has
211ac 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66  _null = <test if
211ad 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
211ae 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a  contains null>.*
211af 2a 20 20 20 20 20 72 65 67 69 73 74 65 72 20 3d  *     register =
211b0 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20   1.**   }.**.** 
211b1 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69  in order to avoi
211b2 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74  d running the <t
211b3 65 73 74 20 69 66 20 64 61 74 61 20 73 74 72 75  est if data stru
211b4 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e  cture contains n
211b5 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72  ull>.** test mor
211b6 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 69 73 20  e often than is 
211b7 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69  necessary..*/.#i
211b8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
211b9 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54  T_SUBQUERY.SQLIT
211ba 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
211bb 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78  lite3FindInIndex
211bc 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
211bd 45 78 70 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70  Expr *pX, int *p
211be 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65  rNotFound){.  Se
211bf 6c 65 63 74 20 2a 70 3b 20 20 20 20 20 20 20 20  lect *p;        
211c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
211c1 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 74 6f      /* SELECT to
211c2 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 49 4e   the right of IN
211c3 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69   operator */.  i
211c4 6e 74 20 65 54 79 70 65 20 3d 20 30 3b 20 20 20  nt eType = 0;   
211c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
211c6 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20       /* Type of 
211c7 52 48 53 20 74 61 62 6c 65 2e 20 49 4e 5f 49 4e  RHS table. IN_IN
211c8 44 45 58 5f 2a 20 2a 2f 0a 20 20 69 6e 74 20 69  DEX_* */.  int i
211c9 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
211ca 61 62 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20  ab++;           
211cb 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 68   /* Cursor of th
211cc 65 20 52 48 53 20 74 61 62 6c 65 20 2a 2f 0a 20  e RHS table */. 
211cd 20 69 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 75   int mustBeUniqu
211ce 65 20 3d 20 28 70 72 4e 6f 74 46 6f 75 6e 64 3d  e = (prNotFound=
211cf 3d 30 29 3b 20 20 20 2f 2a 20 54 72 75 65 20 69  =0);   /* True i
211d0 66 20 52 48 53 20 6d 75 73 74 20 62 65 20 75 6e  f RHS must be un
211d1 69 71 75 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  ique */..  asser
211d2 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  t( pX->op==TK_IN
211d3 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20   );..  /* Check 
211d4 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65 78 69  to see if an exi
211d5 73 74 69 6e 67 20 74 61 62 6c 65 20 6f 72 20 69  sting table or i
211d6 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64  ndex can be used
211d7 20 74 6f 0a 20 20 2a 2a 20 73 61 74 69 73 66 79   to.  ** satisfy
211d8 20 74 68 65 20 71 75 65 72 79 2e 20 20 54 68 69   the query.  Thi
211d9 73 20 69 73 20 70 72 65 66 65 72 61 62 6c 65 20  s is preferable 
211da 74 6f 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20  to generating a 
211db 6e 65 77 20 0a 20 20 2a 2a 20 65 70 68 65 6d 65  new .  ** epheme
211dc 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ral table..  */.
211dd 20 20 70 20 3d 20 28 45 78 70 72 48 61 73 50 72    p = (ExprHasPr
211de 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78 49  operty(pX, EP_xI
211df 73 53 65 6c 65 63 74 29 20 3f 20 70 58 2d 3e 78  sSelect) ? pX->x
211e0 2e 70 53 65 6c 65 63 74 20 3a 20 30 29 3b 0a 20  .pSelect : 0);. 
211e1 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 61 72   if( ALWAYS(pPar
211e2 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20  se->nErr==0) && 
211e3 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e  isCandidateForIn
211e4 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20 73 71  Opt(p) ){.    sq
211e5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
211e6 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20  se->db;         
211e7 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
211e8 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
211e9 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d     Expr *pExpr =
211ea 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d   p->pEList->a[0]
211eb 2e 70 45 78 70 72 3b 20 20 20 2f 2a 20 45 78 70  .pExpr;   /* Exp
211ec 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d 6e 3e  ression <column>
211ed 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   */.    int iCol
211ee 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
211ef 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  n;             /
211f0 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
211f1 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20  n <column> */.  
211f2 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
211f3 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
211f4 65 29 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74  e);      /* Virt
211f5 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e  ual machine bein
211f6 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 54  g coded */.    T
211f7 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d 3e  able *pTab = p->
211f8 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b  pSrc->a[0].pTab;
211f9 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 3c        /* Table <
211fa 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69  table>. */.    i
211fb 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
211fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
211fd 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
211fe 65 20 69 64 78 20 66 6f 72 20 70 54 61 62 20 2a  e idx for pTab *
211ff 2f 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 6f 64  /.   .    /* Cod
21200 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 43 6f  e an OP_VerifyCo
21201 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 62 6c  okie and OP_Tabl
21202 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 6c 65  eLock for <table
21203 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20  >. */.    iDb = 
21204 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
21205 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
21206 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 6c  Schema);.    sql
21207 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
21208 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62  hema(pParse, iDb
21209 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 61  );.    sqlite3Ta
2120a 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20  bleLock(pParse, 
2120b 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
2120c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29   0, pTab->zName)
2120d 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66  ;..    /* This f
2120e 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
2120f 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 6f 20  called from two 
21210 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 68 20  places. In both 
21211 63 61 73 65 73 20 74 68 65 20 76 64 62 65 0a 20  cases the vdbe. 
21212 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64     ** has alread
21213 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  y been allocated
21214 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71 6c 69  . So assume sqli
21215 74 65 33 47 65 74 56 64 62 65 28 29 20 69 73 20  te3GetVdbe() is 
21216 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 73 75  always.    ** su
21217 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e 0a 20  ccessful here.. 
21218 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
21219 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f  (v);.    if( iCo
2121a 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  l<0 ){.      int
2121b 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65   iMem = ++pParse
2121c 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e  ->nMem;.      in
2121d 74 20 69 41 64 64 72 3b 0a 0a 20 20 20 20 20 20  t iAddr;..      
2121e 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56  iAddr = sqlite3V
2121f 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
21220 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20  If, iMem);.     
21221 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21222 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
21223 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20  , 1, iMem);..   
21224 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61     sqlite3OpenTa
21225 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62  ble(pParse, iTab
21226 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f  , iDb, pTab, OP_
21227 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 20  OpenRead);.     
21228 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45   eType = IN_INDE
21229 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20 20  X_ROWID;..      
2122a 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2122b 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20  ere(v, iAddr);. 
2122c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2122d 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
2122e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2122f 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
21230 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20   variable */..  
21231 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61      /* The colla
21232 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73  tion sequence us
21233 65 64 20 62 79 20 74 68 65 20 63 6f 6d 70 61 72  ed by the compar
21234 69 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 65  ison. If an inde
21235 78 20 69 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a  x is to.      **
21236 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63   be used in plac
21237 65 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 6c  e of a temp-tabl
21238 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20 6f 72  e, it must be or
21239 64 65 72 65 64 20 61 63 63 6f 72 64 69 6e 67 0a  dered according.
2123a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 73        ** to this
2123b 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2123c 6e 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 43  nce.  */.      C
2123d 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73  ollSeq *pReq = s
2123e 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70  qlite3BinaryComp
2123f 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  areCollSeq(pPars
21240 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45  e, pX->pLeft, pE
21241 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  xpr);..      /* 
21242 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61  Check that the a
21243 66 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c  ffinity that wil
21244 6c 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72  l be used to per
21245 66 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20  form the .      
21246 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  ** comparison is
21247 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
21248 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
21249 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20   column. If.    
2124a 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20    ** it is not, 
2124b 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  it is not possib
2124c 6c 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e  le to use any in
2124d 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  dex..      */.  
2124e 20 20 20 20 63 68 61 72 20 61 66 66 20 3d 20 63      char aff = c
2124f 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74  omparisonAffinit
21250 79 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e 74  y(pX);.      int
21251 20 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20 28   affinity_ok = (
21252 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
21253 2e 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c 7c  .affinity==aff||
21254 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
21255 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66 6f  NONE);..      fo
21256 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
21257 64 65 78 3b 20 70 49 64 78 20 26 26 20 65 54 79  dex; pIdx && eTy
21258 70 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69 74  pe==0 && affinit
21259 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78 2d  y_ok; pIdx=pIdx-
2125a 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20  >pNext){.       
2125b 20 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43 6f   if( (pIdx->aiCo
2125c 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20  lumn[0]==iCol). 
2125d 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74          && sqlit
2125e 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
2125f 2c 20 45 4e 43 28 64 62 29 2c 20 70 49 64 78 2d  , ENC(db), pIdx-
21260 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 30 29 3d 3d  >azColl[0], 0)==
21261 70 52 65 71 0a 20 20 20 20 20 20 20 20 20 26 26  pReq.         &&
21262 20 28 21 6d 75 73 74 42 65 55 6e 69 71 75 65 20   (!mustBeUnique 
21263 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  || (pIdx->nColum
21264 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 6f 6e  n==1 && pIdx->on
21265 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29  Error!=OE_None))
21266 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20  .        ){.    
21267 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d        int iMem =
21268 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
21269 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
2126a 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Addr;.          
2126b 63 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20  char *pKey;.  . 
2126c 20 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d 20           pKey = 
2126d 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 49  (char *)sqlite3I
2126e 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
2126f 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  se, pIdx);.     
21270 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c       iAddr = sql
21271 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
21272 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a  , OP_If, iMem);.
21273 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
21274 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
21275 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d  P_Integer, 1, iM
21276 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20  em);.  .        
21277 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21278 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65  Op4(v, OP_OpenRe
21279 61 64 2c 20 69 54 61 62 2c 20 70 49 64 78 2d 3e  ad, iTab, pIdx->
2127a 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20  tnum, iDb,.     
2127b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2127c 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 2c 50            pKey,P
2127d 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
2127e 46 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64  F);.          Vd
2127f 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
21280 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29  s", pIdx->zName)
21281 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 54 79  );.          eTy
21282 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e  pe = IN_INDEX_IN
21283 44 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  DEX;..          
21284 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
21285 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20  ere(v, iAddr);. 
21286 20 20 20 20 20 20 20 20 20 69 66 28 20 70 72 4e           if( prN
21287 6f 74 46 6f 75 6e 64 20 26 26 20 21 70 54 61 62  otFound && !pTab
21288 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74  ->aCol[iCol].not
21289 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Null ){.        
2128a 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20      *prNotFound 
2128b 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2128c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2128d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2128e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
2128f 20 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20   eType==0 ){.   
21290 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20 66 6f   /* Could not fo
21291 75 6e 64 20 61 6e 20 65 78 69 73 74 69 6e 67 20  und an existing 
21292 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74  table or index t
21293 6f 20 75 73 65 20 61 73 20 74 68 65 20 52 48 53  o use as the RHS
21294 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20   b-tree..    ** 
21295 57 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20  We will have to 
21296 67 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68 65  generate an ephe
21297 6d 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20 64  meral table to d
21298 6f 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20 2a  o the job..    *
21299 2f 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61  /.    int rMayHa
2129a 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  veNull = 0;.    
2129b 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58  eType = IN_INDEX
2129c 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 72  _EPH;.    if( pr
2129d 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20  NotFound ){.    
2129e 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20    *prNotFound = 
2129f 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b  rMayHaveNull = +
212a0 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
212a1 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d     }else if( pX-
212a2 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c  >pLeft->iColumn<
212a3 30 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79  0 && !ExprHasAny
212a4 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f  Property(pX, EP_
212a5 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
212a6 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49      eType = IN_I
212a7 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 20  NDEX_ROWID;.    
212a8 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  }.    sqlite3Cod
212a9 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73  eSubselect(pPars
212aa 65 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 65 4e  e, pX, rMayHaveN
212ab 75 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49  ull, eType==IN_I
212ac 4e 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 7d  NDEX_ROWID);.  }
212ad 65 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54  else{.    pX->iT
212ae 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d  able = iTab;.  }
212af 0a 20 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b  .  return eType;
212b0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
212b1 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
212b2 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 65  or scalar subque
212b3 72 69 65 73 20 75 73 65 64 20 61 73 20 61 6e 20  ries used as an 
212b4 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e  expression.** an
212b5 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20  d IN operators. 
212b6 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a   Examples:.**.**
212b7 20 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 46       (SELECT a F
212b8 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 20  ROM b)          
212b9 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20  -- subquery.**  
212ba 20 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 43     EXISTS (SELEC
212bb 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d  T a FROM b)   --
212bc 20 45 58 49 53 54 53 20 73 75 62 71 75 65 72 79   EXISTS subquery
212bd 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c  .**     x IN (4,
212be 35 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 20  5,11)           
212bf 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f     -- IN operato
212c0 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72  r with list on r
212c1 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a  ight-hand side.*
212c2 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45  *     x IN (SELE
212c3 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20  CT a FROM b)    
212c4 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   -- IN operator 
212c5 77 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e  with subquery on
212c6 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a   the right.**.**
212c7 20 54 68 65 20 70 45 78 70 72 20 70 61 72 61 6d   The pExpr param
212c8 65 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74  eter describes t
212c9 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  he expression th
212ca 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
212cb 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f  IN.** operator o
212cc 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a  r subquery..**.*
212cd 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69  * If parameter i
212ce 73 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a 65  sRowid is non-ze
212cf 72 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 73 73  ro, then express
212d0 69 6f 6e 20 70 45 78 70 72 20 69 73 20 67 75 61  ion pExpr is gua
212d1 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65  ranteed.** to be
212d2 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 72   of the form "<r
212d3 6f 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20  owid> IN (?, ?, 
212d4 3f 29 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 69  ?)", where <rowi
212d5 64 3e 20 69 73 20 61 20 72 65 66 65 72 65 6e 63  d> is a referenc
212d6 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e 74  e.** to some int
212d7 65 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e 20  eger key column 
212d8 6f 66 20 61 20 74 61 62 6c 65 20 42 2d 54 72 65  of a table B-Tre
212d9 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  e. In this case,
212da 20 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65   use an.** intke
212db 79 20 42 2d 54 72 65 65 20 74 6f 20 73 74 6f 72  y B-Tree to stor
212dc 65 20 74 68 65 20 73 65 74 20 6f 66 20 49 4e 28  e the set of IN(
212dd 2e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 74  ...) values inst
212de 65 61 64 20 6f 66 20 74 68 65 20 75 73 75 61 6c  ead of the usual
212df 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 72  .** (slower) var
212e0 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79  iable length key
212e1 73 20 42 2d 54 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  s B-Tree..**.** 
212e2 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20  If rMayHaveNull 
212e3 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 61  is non-zero, tha
212e4 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
212e5 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e   operation is an
212e6 20 49 4e 0a 2a 2a 20 28 6e 6f 74 20 61 20 53 45   IN.** (not a SE
212e7 4c 45 43 54 20 6f 72 20 45 58 49 53 54 53 29 20  LECT or EXISTS) 
212e8 61 6e 64 20 74 68 61 74 20 74 68 65 20 52 48 53  and that the RHS
212e9 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 73 20   might contains 
212ea 4e 55 4c 4c 73 2e 0a 2a 2a 20 46 75 72 74 68 65  NULLs..** Furthe
212eb 72 6d 6f 72 65 2c 20 74 68 65 20 49 4e 20 69 73  rmore, the IN is
212ec 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75   in a WHERE clau
212ed 73 65 20 61 6e 64 20 74 68 61 74 20 77 65 20 72  se and that we r
212ee 65 61 6c 6c 79 20 77 61 6e 74 0a 2a 2a 20 74 6f  eally want.** to
212ef 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 74 68   iterate over th
212f0 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20  e RHS of the IN 
212f1 6f 70 65 72 61 74 6f 72 20 69 6e 20 6f 72 64 65  operator in orde
212f2 72 20 74 6f 20 71 75 69 63 6b 6c 79 20 6c 6f 63  r to quickly loc
212f3 61 74 65 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72 65  ate.** all corre
212f4 73 70 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c 65  sponding LHS ele
212f5 6d 65 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69 73  ments.  All this
212f6 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 73   routine does is
212f7 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74   initialize.** t
212f8 68 65 20 72 65 67 69 73 74 65 72 20 67 69 76 65  he register give
212f9 6e 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75 6c  n by rMayHaveNul
212fa 6c 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c  l to NULL.  Call
212fb 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69 6c  ing routines wil
212fc 6c 20 74 61 6b 65 0a 2a 2a 20 63 61 72 65 20 6f  l take.** care o
212fd 66 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20  f changing this 
212fe 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 20 74  register value t
212ff 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74 68  o non-NULL if th
21300 65 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66 72  e RHS is NULL-fr
21301 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61  ee..**.** If rMa
21302 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a 65 72  yHaveNull is zer
21303 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  o, that means th
21304 61 74 20 74 68 65 20 73 75 62 71 75 65 72 79 20  at the subquery 
21305 69 73 20 62 65 69 6e 67 20 75 73 65 64 0a 2a 2a  is being used.**
21306 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20   for membership 
21307 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2e 20 20 54  testing only.  T
21308 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
21309 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e  to initialize an
2130a 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 74  y.** registers t
2130b 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 70  o indicate the p
2130c 72 65 73 65 6e 73 65 20 6f 72 20 61 62 73 65 6e  resense or absen
2130d 63 65 20 6f 66 20 4e 55 4c 4c 73 20 6f 6e 20 74  ce of NULLs on t
2130e 68 65 20 52 48 53 2e 0a 2a 2a 0a 2a 2a 20 46 6f  he RHS..**.** Fo
2130f 72 20 61 20 53 45 4c 45 43 54 20 6f 72 20 45 58  r a SELECT or EX
21310 49 53 54 53 20 6f 70 65 72 61 74 6f 72 2c 20 72  ISTS operator, r
21311 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74  eturn the regist
21312 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  er that holds th
21313 65 0a 2a 2a 20 72 65 73 75 6c 74 2e 20 20 46 6f  e.** result.  Fo
21314 72 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 6f  r IN operators o
21315 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  r if an error oc
21316 63 75 72 73 2c 20 74 68 65 20 72 65 74 75 72 6e  curs, the return
21317 20 76 61 6c 75 65 20 69 73 20 30 2e 0a 2a 2f 0a   value is 0..*/.
21318 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
21319 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c  MIT_SUBQUERY.SQL
2131a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2131b 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65  sqlite3CodeSubse
2131c 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70  lect(.  Parse *p
2131d 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2131e 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2131f 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  xt */.  Expr *pE
21320 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  xpr,            
21321 2f 2a 20 54 68 65 20 49 4e 2c 20 53 45 4c 45 43  /* The IN, SELEC
21322 54 2c 20 6f 72 20 45 58 49 53 54 53 20 6f 70 65  T, or EXISTS ope
21323 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 72  rator */.  int r
21324 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 20 20 20  MayHaveNull,    
21325 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74     /* Register t
21326 68 61 74 20 72 65 63 6f 72 64 73 20 77 68 65 74  hat records whet
21327 68 65 72 20 4e 55 4c 4c 73 20 65 78 69 73 74 20  her NULLs exist 
21328 69 6e 20 52 48 53 20 2a 2f 0a 20 20 69 6e 74 20  in RHS */.  int 
21329 69 73 52 6f 77 69 64 20 20 20 20 20 20 20 20 20  isRowid         
2132a 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
2132b 4c 48 53 20 6f 66 20 49 4e 20 6f 70 65 72 61 74  LHS of IN operat
2132c 6f 72 20 69 73 20 61 20 72 6f 77 69 64 20 2a 2f  or is a rowid */
2132d 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64  .){.  int testAd
2132e 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  dr = 0;         
2132f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21330 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61   One-time test a
21331 64 64 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20  ddress */.  int 
21332 72 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20 20  rReg = 0;       
21333 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21334 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
21335 73 74 6f 72 69 6e 67 20 72 65 73 75 6c 74 69 6e  storing resultin
21336 67 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d  g */.  Vdbe *v =
21337 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
21338 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 4e  pParse);.  if( N
21339 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74  EVER(v==0) ) ret
2133a 75 72 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  urn 0;.  sqlite3
2133b 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
2133c 61 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69  arse);..  /* Thi
2133d 73 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72  s code must be r
2133e 75 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65  un in its entire
2133f 74 79 20 65 76 65 72 79 20 74 69 6d 65 20 69 74  ty every time it
21340 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
21341 20 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74    ** if any of t
21342 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
21343 74 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  true:.  **.  ** 
21344 20 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d     *  The right-
21345 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 63  hand side is a c
21346 6f 72 72 65 6c 61 74 65 64 20 73 75 62 71 75 65  orrelated subque
21347 72 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68  ry.  **    *  Th
21348 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
21349 65 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69  e is an expressi
2134a 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69  on list containi
2134b 6e 67 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a  ng variables.  *
2134c 2a 20 20 20 20 2a 20 20 57 65 20 61 72 65 20 69  *    *  We are i
2134d 6e 73 69 64 65 20 61 20 74 72 69 67 67 65 72 0a  nside a trigger.
2134e 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c    **.  ** If all
2134f 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 61 72   of the above ar
21350 65 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65  e false, then we
21351 20 63 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f   can run this co
21352 64 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a  de just once.  *
21353 2a 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c  * save the resul
21354 74 73 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68  ts, and reuse th
21355 65 20 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e  e same result on
21356 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f   subsequent invo
21357 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  cations..  */.  
21358 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  if( !ExprHasAnyP
21359 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2135a 50 5f 56 61 72 53 65 6c 65 63 74 29 20 26 26 20  P_VarSelect) && 
2135b 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  !pParse->pTrigge
2135c 72 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20  rTab ){.    int 
2135d 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mem = ++pParse->
2135e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
2135f 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
21360 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20  P_If, mem);.    
21361 74 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74  testAddr = sqlit
21362 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21363 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d  OP_Integer, 1, m
21364 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  em);.    assert(
21365 20 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70   testAddr>0 || p
21366 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
21367 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a  cFailed );.  }..
21368 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
21369 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
2136a 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63  TK_IN: {.      c
2136b 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20  har affinity;.  
2136c 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49      KeyInfo keyI
2136d 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61  nfo;.      int a
2136e 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ddr;        /* A
2136f 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65  ddress of OP_Ope
21370 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
21371 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  uction */.      
21372 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45  Expr *pLeft = pE
21373 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20  xpr->pLeft;..   
21374 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e     if( rMayHaveN
21375 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ull ){.        s
21376 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21377 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
21378 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20  rMayHaveNull);. 
21379 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66       }..      af
2137a 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33  finity = sqlite3
2137b 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65  ExprAffinity(pLe
2137c 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57  ft);..      /* W
2137d 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61  hether this is a
2137e 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e  n 'x IN(SELECT..
2137f 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28  .)' or an 'x IN(
21380 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20  <exprlist>)'.   
21381 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e     ** expression
21382 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74   it is handled t
21383 68 65 20 73 61 6d 65 20 77 61 79 2e 20 20 41 6e  he same way.  An
21384 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
21385 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69   is .      ** fi
21386 6c 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65  lled with single
21387 2d 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79  -field index key
21388 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  s representing t
21389 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20  he results.     
2138a 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c   ** from the SEL
2138b 45 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72  ECT or the <expr
2138c 6c 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a  list>..      **.
2138d 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20        ** If the 
2138e 27 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69  'x' expression i
2138f 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  s a column value
21390 2c 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e  , or the SELECT.
21391 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74  ...      ** stat
21392 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20  ement returns a 
21393 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68  column value, th
21394 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  en the affinity 
21395 6f 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a  of that.      **
21396 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20   column is used 
21397 74 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64  to build the ind
21398 65 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68  ex keys. If both
21399 20 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20   'x' and the.   
2139a 20 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20     ** SELECT... 
2139b 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f  statement are co
2139c 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65  lumns, then nume
2139d 72 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20  ric affinity is 
2139e 75 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66  used.      ** if
2139f 20 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68   either column h
213a0 61 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e  as NUMERIC or IN
213a1 54 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20  TEGER affinity. 
213a2 49 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20  If neither.     
213a3 20 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20   ** 'x' nor the 
213a4 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d  SELECT... statem
213a5 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c  ent are columns,
213a6 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66   then numeric af
213a7 66 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20  finity.      ** 
213a8 69 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a  is used..      *
213a9 2f 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69  /.      pExpr->i
213aa 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e  Table = pParse->
213ab 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64  nTab++;.      ad
213ac 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
213ad 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
213ae 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70  nEphemeral, pExp
213af 72 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f  r->iTable, !isRo
213b0 77 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73  wid);.      mems
213b1 65 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c 20  et(&keyInfo, 0, 
213b2 73 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 29  sizeof(keyInfo))
213b3 3b 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e  ;.      keyInfo.
213b4 6e 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20  nField = 1;..   
213b5 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
213b6 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
213b7 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
213b8 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31         /* Case 1
213b9 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 53  :     expr IN (S
213ba 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20  ELECT ...).     
213bb 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
213bc 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
213bd 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75  o write the resu
213be 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63  lts of the selec
213bf 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f  t into the tempo
213c0 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20  rary.        ** 
213c1 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20  table allocated 
213c2 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 65  and opened above
213c3 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
213c4 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20       SelectDest 
213c5 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 78  dest;.        Ex
213c6 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
213c7 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
213c8 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 20   !isRowid );.   
213c9 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
213ca 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
213cb 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 72  , SRT_Set, pExpr
213cc 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20  ->iTable);.     
213cd 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 79     dest.affinity
213ce 20 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 3b   = (u8)affinity;
213cf 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
213d0 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 26   (pExpr->iTable&
213d1 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 78  0x0000FFFF)==pEx
213d2 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20  pr->iTable );.  
213d3 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
213d4 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
213d5 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
213d6 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20  , &dest) ){.    
213d7 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
213d8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
213d9 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72    pEList = pExpr
213da 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c  ->x.pSelect->pEL
213db 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ist;.        if(
213dc 20 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21 3d   ALWAYS(pEList!=
213dd 30 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78  0 && pEList->nEx
213de 70 72 3e 30 29 20 29 7b 20 0a 20 20 20 20 20 20  pr>0) ){ .      
213df 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c      keyInfo.aCol
213e0 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 69  l[0] = sqlite3Bi
213e1 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53  naryCompareCollS
213e2 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  eq(pParse, pExpr
213e3 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 20  ->pLeft,.       
213e4 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61         pEList->a
213e5 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  [0].pExpr);.    
213e6 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
213e7 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70  e if( pExpr->x.p
213e8 4c 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20  List!=0 ){.     
213e9 20 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20     /* Case 2:   
213ea 20 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c    expr IN (exprl
213eb 69 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ist).        **.
213ec 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65          ** For e
213ed 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20  ach expression, 
213ee 62 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b  build an index k
213ef 65 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c  ey from the eval
213f0 75 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20  uation and.     
213f1 20 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69     ** store it i
213f2 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  n the temporary 
213f3 74 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e  table. If <expr>
213f4 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68   is a column, th
213f5 65 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a  en use.        *
213f6 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61  * that columns a
213f7 66 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69  ffinity when bui
213f8 6c 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73  lding index keys
213f9 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e  . If <expr> is n
213fa 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20  ot.        ** a 
213fb 63 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65  column, use nume
213fc 72 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20  ric affinity..  
213fd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
213fe 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20   int i;.        
213ff 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
21400 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
21401 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  ;.        struct
21402 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
21403 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69  pItem;.        i
21404 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a  nt r1, r2, r3;..
21405 20 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66          if( !aff
21406 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20  inity ){.       
21407 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51     affinity = SQ
21408 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20  LITE_AFF_NONE;. 
21409 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2140a 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30   keyInfo.aColl[0
2140b 5d 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  ] = sqlite3ExprC
2140c 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
2140d 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20  Expr->pLeft);.. 
2140e 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74         /* Loop t
2140f 68 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72  hrough each expr
21410 65 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c  ession in <exprl
21411 69 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20  ist>. */.       
21412 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
21413 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
21414 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71  .        r2 = sq
21415 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
21416 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20  pParse);.       
21417 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21418 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
21419 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66  , r2);.        f
2141a 6f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70  or(i=pList->nExp
2141b 72 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  r, pItem=pList->
2141c 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74  a; i>0; i--, pIt
2141d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  em++){.         
2141e 20 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74   Expr *pE2 = pIt
2141f 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  em->pExpr;.     
21420 20 20 20 20 20 69 6e 74 20 69 56 61 6c 54 6f 49       int iValToI
21421 6e 73 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  ns;..          /
21422 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
21423 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74  ion is not const
21424 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c  ant then we will
21425 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20   need to.       
21426 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68     ** disable th
21427 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 20  e test that was 
21428 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20  generated above 
21429 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a  that makes sure.
2142a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69            ** thi
2142b 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63  s code only exec
2142c 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61  utes once.  Beca
2142d 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f  use for a non-co
2142e 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20  nstant.         
2142f 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77   ** expression w
21430 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20  e need to rerun 
21431 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 74  this code each t
21432 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ime..          *
21433 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
21434 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 6c  testAddr && !sql
21435 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
21436 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20  nt(pE2) ){.     
21437 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21438 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76  beChangeToNoop(v
21439 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29  , testAddr-1, 2)
2143a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65  ;.            te
2143b 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20  stAddr = 0;.    
2143c 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
2143d 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74     /* Evaluate t
2143e 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  he expression an
2143f 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f  d insert it into
21440 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20   the temp table 
21441 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  */.          if(
21442 20 69 73 52 6f 77 69 64 20 26 26 20 73 71 6c 69   isRowid && sqli
21443 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
21444 28 70 45 32 2c 20 26 69 56 61 6c 54 6f 49 6e 73  (pE2, &iValToIns
21445 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
21446 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21447 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 49  p3(v, OP_InsertI
21448 6e 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  nt, pExpr->iTabl
21449 65 2c 20 72 32 2c 20 69 56 61 6c 54 6f 49 6e 73  e, r2, iValToIns
2144a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
2144b 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
2144c 72 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r3 = sqlite3Expr
2144d 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
2144e 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20  e, pE2, r1);.   
2144f 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52           if( isR
21450 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20  owid ){.        
21451 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21452 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75  eAddOp2(v, OP_Mu
21453 73 74 42 65 49 6e 74 2c 20 72 33 2c 0a 20 20 20  stBeInt, r3,.   
21454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21455 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
21456 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
21457 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20  ddr(v)+2);.     
21458 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21459 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2145a 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e  _Insert, pExpr->
2145b 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b  iTable, r2, r3);
2145c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
2145d 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
2145e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2145f 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
21460 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c  cord, r3, 1, r2,
21461 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a   &affinity, 1);.
21462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
21463 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
21464 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
21465 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 20  rse, r3, 1);.   
21466 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
21467 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21468 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45  OP_IdxInsert, pE
21469 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29  xpr->iTable, r2)
2146a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
2146b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2146c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
2146d 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2146e 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2146f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21470 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
21471 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20  Parse, r2);.    
21472 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 69    }.      if( !i
21473 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  sRowid ){.      
21474 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
21475 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28  ngeP4(v, addr, (
21476 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c  void *)&keyInfo,
21477 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20   P4_KEYINFO);.  
21478 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
21479 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61  k;.    }..    ca
2147a 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20  se TK_EXISTS:.  
2147b 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54    case TK_SELECT
2147c 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  :.    default: {
2147d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69  .      /* If thi
2147e 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63  s has to be a sc
2147f 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65  alar SELECT.  Ge
21480 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70  nerate code to p
21481 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  ut the.      ** 
21482 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65  value of this se
21483 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79  lect in a memory
21484 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64   cell and record
21485 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20   the number.    
21486 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f    ** of the memo
21487 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75  ry cell in iColu
21488 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20  mn.  If this is 
21489 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69 74 65  an EXISTS, write
2148a 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74  .      ** an int
2148b 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69 73  eger 0 (not exis
2148c 74 73 29 20 6f 72 20 31 20 28 65 78 69 73 74 73  ts) or 1 (exists
2148d 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79 20  ) into a memory 
2148e 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6e  cell.      ** an
2148f 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d 65  d record that me
21490 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f  mory cell in iCo
21491 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  lumn..      */. 
21492 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
21493 74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20  t Token one = { 
21494 22 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 54 6f  "1", 1 };  /* To
21495 6b 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 6c 20  ken for literal 
21496 76 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 20 20  value 1 */.     
21497 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20   Select *pSel;  
21498 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21499 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54         /* SELECT
2149a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 6e   statement to en
2149b 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 53 65  code */.      Se
2149c 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20  lectDest dest;  
2149d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2149e 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 65      /* How to de
2149f 61 6c 20 77 69 74 68 20 53 45 4c 45 43 74 20 72  al with SELECt r
214a0 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 20 20  esult */..      
214a1 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
214a2 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29  >op==TK_EXISTS )
214a3 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
214a4 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
214a5 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20  SELECT );.      
214a6 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
214a7 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c 7c 20  p==TK_EXISTS || 
214a8 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45  pExpr->op==TK_SE
214a9 4c 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 20 61  LECT );..      a
214aa 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72  ssert( ExprHasPr
214ab 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
214ac 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20  _xIsSelect) );. 
214ad 20 20 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70       pSel = pExp
214ae 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20  r->x.pSelect;.  
214af 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
214b0 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c  tDestInit(&dest,
214b1 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d   0, ++pParse->nM
214b2 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  em);.      if( p
214b3 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c  Expr->op==TK_SEL
214b4 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ECT ){.        d
214b5 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f  est.eDest = SRT_
214b6 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  Mem;.        sql
214b7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
214b8 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65  , OP_Null, 0, de
214b9 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20  st.iParm);.     
214ba 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
214bb 76 2c 20 22 49 6e 69 74 20 73 75 62 71 75 65 72  v, "Init subquer
214bc 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20  y result"));.   
214bd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
214be 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53    dest.eDest = S
214bf 52 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 20 20  RT_Exists;.     
214c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
214c1 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
214c2 65 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72  er, 0, dest.iPar
214c3 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65  m);.        Vdbe
214c4 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69  Comment((v, "Ini
214c5 74 20 45 58 49 53 54 53 20 72 65 73 75 6c 74 22  t EXISTS result"
214c6 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
214c7 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
214c8 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
214c9 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20  pSel->pLimit);. 
214ca 20 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 6d 69       pSel->pLimi
214cb 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  t = sqlite3PExpr
214cc 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45  (pParse, TK_INTE
214cd 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29  GER, 0, 0, &one)
214ce 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
214cf 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
214d0 2c 20 70 53 65 6c 2c 20 26 64 65 73 74 29 20 29  , pSel, &dest) )
214d1 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
214d2 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
214d3 20 20 72 52 65 67 20 3d 20 64 65 73 74 2e 69 50    rReg = dest.iP
214d4 61 72 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53  arm;.      ExprS
214d5 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45  etIrreducible(pE
214d6 78 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61  xpr);.      brea
214d7 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
214d8 69 66 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a  if( testAddr ){.
214d9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
214da 75 6d 70 48 65 72 65 28 76 2c 20 74 65 73 74 41  umpHere(v, testA
214db 64 64 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71  ddr-1);.  }.  sq
214dc 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
214dd 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20  p(pParse, 1);.. 
214de 20 72 65 74 75 72 6e 20 72 52 65 67 3b 0a 7d 0a   return rReg;.}.
214df 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
214e0 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a  _OMIT_SUBQUERY *
214e1 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
214e2 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
214e3 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
214e4 6f 64 65 20 66 6f 72 20 61 6e 20 49 4e 20 65 78  ode for an IN ex
214e5 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  pression..**.** 
214e6 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43       x IN (SELEC
214e7 54 20 2e 2e 2e 29 0a 2a 2a 20 20 20 20 20 20 78  T ...).**      x
214e8 20 49 4e 20 28 76 61 6c 75 65 2c 20 76 61 6c 75   IN (value, valu
214e9 65 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68  e, ...).**.** Th
214ea 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  e left-hand side
214eb 20 28 4c 48 53 29 20 69 73 20 61 20 73 63 61 6c   (LHS) is a scal
214ec 61 72 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  ar expression.  
214ed 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73  The right-hand s
214ee 69 64 65 20 28 52 48 53 29 0a 2a 2a 20 69 73 20  ide (RHS).** is 
214ef 61 6e 20 61 72 72 61 79 20 6f 66 20 7a 65 72 6f  an array of zero
214f0 20 6f 72 20 6d 6f 72 65 20 76 61 6c 75 65 73 2e   or more values.
214f1 20 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e    The expression
214f2 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20   is true if the 
214f3 4c 48 53 20 69 73 0a 2a 2a 20 63 6f 6e 74 61 69  LHS is.** contai
214f4 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52  ned within the R
214f5 48 53 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f  HS.  The value o
214f6 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
214f7 20 69 73 20 75 6e 6b 6e 6f 77 6e 20 28 4e 55 4c   is unknown (NUL
214f8 4c 29 0a 2a 2a 20 69 66 20 74 68 65 20 4c 48 53  L).** if the LHS
214f9 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69 66 20 74   is NULL or if t
214fa 68 65 20 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f  he LHS is not co
214fb 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74  ntained within t
214fc 68 65 20 52 48 53 20 61 6e 64 20 74 68 65 0a 2a  he RHS and the.*
214fd 2a 20 52 48 53 20 63 6f 6e 74 61 69 6e 73 20 6f  * RHS contains o
214fe 6e 65 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20  ne or more NULL 
214ff 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  values..**.** Th
21500 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72  is routine gener
21501 61 74 65 73 20 63 6f 64 65 20 77 69 6c 6c 20 6a  ates code will j
21502 75 6d 70 20 74 6f 20 64 65 73 74 49 66 46 61 6c  ump to destIfFal
21503 73 65 20 69 66 20 74 68 65 20 4c 48 53 20 69 73  se if the LHS is
21504 20 6e 6f 74 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e   not .** contain
21505 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52 48  ed within the RH
21506 53 2e 20 20 49 66 20 64 75 65 20 74 6f 20 4e 55  S.  If due to NU
21507 4c 4c 73 20 77 65 20 63 61 6e 6e 6f 74 20 64 65  LLs we cannot de
21508 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 4c  termine if the L
21509 48 53 0a 2a 2a 20 69 73 20 63 6f 6e 74 61 69 6e  HS.** is contain
2150a 65 64 20 69 6e 20 74 68 65 20 52 48 53 20 74 68  ed in the RHS th
2150b 65 6e 20 6a 75 6d 70 20 74 6f 20 64 65 73 74 49  en jump to destI
2150c 66 4e 75 6c 6c 2e 20 20 49 66 20 74 68 65 20 4c  fNull.  If the L
2150d 48 53 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 0a  HS is contained.
2150e 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 52 48  ** within the RH
2150f 53 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f  S then fall thro
21510 75 67 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ugh..*/.static v
21511 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
21512 6f 64 65 49 4e 28 0a 20 20 50 61 72 73 65 20 2a  odeIN(.  Parse *
21513 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
21514 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
21515 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
21516 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20  ntext */.  Expr 
21517 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  *pExpr,         
21518 20 2f 2a 20 54 68 65 20 49 4e 20 65 78 70 72 65   /* The IN expre
21519 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 64  ssion */.  int d
2151a 65 73 74 49 66 46 61 6c 73 65 2c 20 20 20 20 20  estIfFalse,     
2151b 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66   /* Jump here if
2151c 20 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e 74   LHS is not cont
2151d 61 69 6e 65 64 20 69 6e 20 74 68 65 20 52 48 53  ained in the RHS
2151e 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 49 66   */.  int destIf
2151f 4e 75 6c 6c 20 20 20 20 20 20 20 20 2f 2a 20 4a  Null        /* J
21520 75 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 20  ump here if the 
21521 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 6b 6e  results are unkn
21522 6f 77 6e 20 64 75 65 20 74 6f 20 4e 55 4c 4c 73  own due to NULLs
21523 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 52 68   */.){.  int rRh
21524 73 48 61 73 4e 75 6c 6c 20 3d 20 30 3b 20 20 2f  sHasNull = 0;  /
21525 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74 20  * Register that 
21526 69 73 20 74 72 75 65 20 69 66 20 52 48 53 20 63  is true if RHS c
21527 6f 6e 74 61 69 6e 73 20 4e 55 4c 4c 20 76 61 6c  ontains NULL val
21528 75 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 61 66  ues */.  char af
21529 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 2f  finity;        /
2152a 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 61 66 66  * Comparison aff
2152b 69 6e 69 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a  inity to use */.
2152c 20 20 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20    int eType;    
2152d 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20          /* Type 
2152e 6f 66 20 74 68 65 20 52 48 53 20 2a 2f 0a 20 20  of the RHS */.  
2152f 69 6e 74 20 72 31 3b 20 20 20 20 20 20 20 20 20  int r1;         
21530 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61        /* Tempora
21531 72 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20  ry use register 
21532 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  */.  Vdbe *v;   
21533 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
21534 61 74 65 6d 65 6e 74 20 75 6e 64 65 72 20 63 6f  atement under co
21535 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20  nstruction */.. 
21536 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
21537 52 48 53 2e 20 20 20 41 66 74 65 72 20 74 68 69  RHS.   After thi
21538 73 20 73 74 65 70 2c 20 74 68 65 20 74 61 62 6c  s step, the tabl
21539 65 20 77 69 74 68 20 63 75 72 73 6f 72 0a 20 20  e with cursor.  
2153a 2a 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  ** pExpr->iTable
2153b 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74   will contains t
2153c 68 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d  he values that m
2153d 61 6b 65 20 75 70 20 74 68 65 20 52 48 53 2e 0a  ake up the RHS..
2153e 20 20 2a 2f 0a 20 20 76 20 3d 20 70 50 61 72 73    */.  v = pPars
2153f 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65  e->pVdbe;.  asse
21540 72 74 28 20 76 21 3d 30 20 29 3b 20 20 20 20 20  rt( v!=0 );     
21541 20 20 2f 2a 20 4f 4f 4d 20 64 65 74 65 63 74 65    /* OOM detecte
21542 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20  d prior to this 
21543 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 56 64 62  routine */.  Vdb
21544 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
21545 20 22 62 65 67 69 6e 20 49 4e 20 65 78 70 72 22   "begin IN expr"
21546 29 29 3b 0a 20 20 65 54 79 70 65 20 3d 20 73 71  ));.  eType = sq
21547 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78  lite3FindInIndex
21548 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
21549 26 72 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 0a  &rRhsHasNull);..
2154a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
2154b 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20  the affinity to 
2154c 75 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20  use to create a 
2154d 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73  key from the res
2154e 75 6c 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65  ults.  ** of the
2154f 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66 66   expression. aff
21550 69 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73 20  inityStr stores 
21551 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20  a static string 
21552 73 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20 2a  suitable for.  *
21553 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52  * P4 of OP_MakeR
21554 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 61 66  ecord..  */.  af
21555 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69  finity = compari
21556 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70  sonAffinity(pExp
21557 72 29 3b 0a 0a 20 20 2f 2a 20 43 6f 64 65 20 74  r);..  /* Code t
21558 68 65 20 4c 48 53 2c 20 74 68 65 20 3c 65 78 70  he LHS, the <exp
21559 72 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20  r> from "<expr> 
2155a 49 4e 20 28 2e 2e 2e 29 22 2e 0a 20 20 2a 2f 0a  IN (...)"..  */.
2155b 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2155c 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a  hePush(pParse);.
2155d 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
2155e 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2155f 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
21560 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  ode(pParse, pExp
21561 72 2d 3e 70 4c 65 66 74 2c 20 72 31 29 3b 0a 20  r->pLeft, r1);. 
21562 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21563 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p2(v, OP_IsNull,
21564 20 72 31 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29   r1, destIfNull)
21565 3b 0a 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d  ;...  if( eType=
21566 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20  =IN_INDEX_ROWID 
21567 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  ){.    /* In thi
21568 73 20 63 61 73 65 2c 20 74 68 65 20 52 48 53 20  s case, the RHS 
21569 69 73 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20  is the ROWID of 
2156a 74 61 62 6c 65 20 62 2d 74 72 65 65 0a 20 20 20  table b-tree.   
2156b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
2156c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2156d 4d 75 73 74 42 65 49 6e 74 2c 20 72 31 2c 20 64  MustBeInt, r1, d
2156e 65 73 74 49 66 46 61 6c 73 65 29 3b 0a 20 20 20  estIfFalse);.   
2156f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21570 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
21571 74 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  ts, pExpr->iTabl
21572 65 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20  e, destIfFalse, 
21573 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  r1);.  }else{.  
21574 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73    /* In this cas
21575 65 2c 20 74 68 65 20 52 48 53 20 69 73 20 61 6e  e, the RHS is an
21576 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 0a 20   index b-tree.. 
21577 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
21578 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
21579 50 5f 41 66 66 69 6e 69 74 79 2c 20 72 31 2c 20  P_Affinity, r1, 
2157a 31 2c 20 30 2c 20 26 61 66 66 69 6e 69 74 79 2c  1, 0, &affinity,
2157b 20 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20   1);..    /* If 
2157c 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 68  the set membersh
2157d 69 70 20 74 65 73 74 20 66 61 69 6c 73 2c 20 74  ip test fails, t
2157e 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f  hen the result o
2157f 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 78  f the .    ** "x
21580 20 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65   IN (...)" expre
21581 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69  ssion must be ei
21582 74 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20  ther 0 or NULL. 
21583 49 66 20 74 68 65 20 73 65 74 0a 20 20 20 20 2a  If the set.    *
21584 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55  * contains no NU
21585 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20  LL values, then 
21586 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 30 2e  the result is 0.
21587 20 49 66 20 74 68 65 20 73 65 74 20 0a 20 20 20   If the set .   
21588 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65   ** contains one
21589 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61   or more NULL va
2158a 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72  lues, then the r
2158b 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20  esult of the.   
2158c 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69   ** expression i
2158d 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20  s also NULL..   
2158e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 52 68 73   */.    if( rRhs
2158f 48 61 73 4e 75 6c 6c 3d 3d 30 20 7c 7c 20 64 65  HasNull==0 || de
21590 73 74 49 66 46 61 6c 73 65 3d 3d 64 65 73 74 49  stIfFalse==destI
21591 66 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 2f  fNull ){.      /
21592 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75  * This branch ru
21593 6e 73 20 69 66 20 69 74 20 69 73 20 6b 6e 6f 77  ns if it is know
21594 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d  n at compile tim
21595 65 20 74 68 61 74 20 74 68 65 20 52 48 53 0a 20  e that the RHS. 
21596 20 20 20 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 63       ** cannot c
21597 6f 6e 74 61 69 6e 20 4e 55 4c 4c 20 76 61 6c 75  ontain NULL valu
21598 65 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73  es. This happens
21599 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 20   as the result. 
2159a 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f       ** of a "NO
2159b 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69  T NULL" constrai
2159c 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
2159d 73 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 20  se schema..     
2159e 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 6c 73   **.      ** Als
2159f 6f 20 72 75 6e 20 74 68 69 73 20 62 72 61 6e 63  o run this branc
215a0 68 20 69 66 20 4e 55 4c 4c 20 69 73 20 65 71 75  h if NULL is equ
215a1 69 76 61 6c 65 6e 74 20 74 6f 20 46 41 4c 53 45  ivalent to FALSE
215a2 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68  .      ** for th
215a3 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 49 4e  is particular IN
215a4 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 20   operator..     
215a5 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
215a6 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76  3VdbeAddOp4Int(v
215a7 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 70  , OP_NotFound, p
215a8 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 64 65  Expr->iTable, de
215a9 73 74 49 66 46 61 6c 73 65 2c 20 72 31 2c 20 31  stIfFalse, r1, 1
215aa 29 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  );..    }else{. 
215ab 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20       /* In this 
215ac 62 72 61 6e 63 68 2c 20 74 68 65 20 52 48 53 20  branch, the RHS 
215ad 6f 66 20 74 68 65 20 49 4e 20 6d 69 67 68 74 20  of the IN might 
215ae 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20 61  contain a NULL a
215af 6e 64 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  nd.      ** the 
215b0 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 4e 55  presence of a NU
215b1 4c 4c 20 6f 6e 20 74 68 65 20 52 48 53 20 6d 61  LL on the RHS ma
215b2 6b 65 73 20 61 20 64 69 66 66 65 72 65 6e 63 65  kes a difference
215b3 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a   in the.      **
215b4 20 6f 75 74 63 6f 6d 65 2e 0a 20 20 20 20 20 20   outcome..      
215b5 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 2c  */.      int j1,
215b6 20 6a 32 2c 20 6a 33 3b 0a 0a 20 20 20 20 20 20   j2, j3;..      
215b7 2f 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74  /* First check t
215b8 6f 20 73 65 65 20 69 66 20 74 68 65 20 4c 48 53  o see if the LHS
215b9 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e   is contained in
215ba 20 74 68 65 20 52 48 53 2e 20 20 49 66 20 73 6f   the RHS.  If so
215bb 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20  ,.      ** then 
215bc 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20  the presence of 
215bd 4e 55 4c 4c 73 20 69 6e 20 74 68 65 20 52 48 53  NULLs in the RHS
215be 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65 72   does not matter
215bf 2c 20 73 6f 20 6a 75 6d 70 0a 20 20 20 20 20 20  , so jump.      
215c0 2a 2a 20 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74  ** over all of t
215c1 68 65 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c  he code that fol
215c2 6c 6f 77 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  lows..      */. 
215c3 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
215c4 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76  3VdbeAddOp4Int(v
215c5 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70  , OP_Found, pExp
215c6 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 31  r->iTable, 0, r1
215c7 2c 20 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  , 1);..      /* 
215c8 48 65 72 65 20 77 65 20 62 65 67 69 6e 20 67 65  Here we begin ge
215c9 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74 68  nerating code th
215ca 61 74 20 72 75 6e 73 20 69 66 20 74 68 65 20 4c  at runs if the L
215cb 48 53 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20  HS is not.      
215cc 2a 2a 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74  ** contained wit
215cd 68 69 6e 20 74 68 65 20 52 48 53 2e 20 20 47 65  hin the RHS.  Ge
215ce 6e 65 72 61 74 65 20 61 64 64 69 74 69 6f 6e 61  nerate additiona
215cf 6c 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20 20  l code that.    
215d0 20 20 2a 2a 20 74 65 73 74 73 20 74 68 65 20 52    ** tests the R
215d1 48 53 20 66 6f 72 20 4e 55 4c 4c 73 2e 20 20 49  HS for NULLs.  I
215d2 66 20 74 68 65 20 52 48 53 20 63 6f 6e 74 61 69  f the RHS contai
215d3 6e 73 20 61 20 4e 55 4c 4c 20 74 68 65 6e 0a 20  ns a NULL then. 
215d4 20 20 20 20 20 2a 2a 20 6a 75 6d 70 20 74 6f 20       ** jump to 
215d5 64 65 73 74 49 66 4e 75 6c 6c 2e 20 20 49 66 20  destIfNull.  If 
215d6 74 68 65 72 65 20 61 72 65 20 6e 6f 20 4e 55 4c  there are no NUL
215d7 4c 73 20 69 6e 20 74 68 65 20 52 48 53 20 74 68  Ls in the RHS th
215d8 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6a 75 6d 70  en.      ** jump
215d9 20 74 6f 20 64 65 73 74 49 66 46 61 6c 73 65 2e   to destIfFalse.
215da 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
215db 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
215dc 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
215dd 4e 75 6c 6c 2c 20 72 52 68 73 48 61 73 4e 75 6c  Null, rRhsHasNul
215de 6c 29 3b 0a 20 20 20 20 20 20 6a 33 20 3d 20 73  l);.      j3 = s
215df 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
215e0 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Int(v, OP_Found,
215e1 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
215e2 30 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c 20  0, rRhsHasNull, 
215e3 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
215e4 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
215e5 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72  P_Integer, -1, r
215e6 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 20 20 20  RhsHasNull);.   
215e7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
215e8 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20  mpHere(v, j3);. 
215e9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
215ea 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
215eb 49 6d 6d 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c  Imm, rRhsHasNull
215ec 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
215ed 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
215ee 76 2c 20 6a 32 29 3b 0a 0a 20 20 20 20 20 20 2f  v, j2);..      /
215ef 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 61 70  * Jump to the ap
215f0 70 72 6f 70 72 69 61 74 65 20 74 61 72 67 65 74  propriate target
215f1 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68   depending on wh
215f2 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 20 20 20  ether or not.   
215f3 20 20 20 2a 2a 20 74 68 65 20 52 48 53 20 63 6f     ** the RHS co
215f4 6e 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 20 20  ntains a NULL.  
215f5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
215f6 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
215f7 2c 20 4f 50 5f 49 66 2c 20 72 52 68 73 48 61 73  , OP_If, rRhsHas
215f8 4e 75 6c 6c 2c 20 64 65 73 74 49 66 4e 75 6c 6c  Null, destIfNull
215f9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
215fa 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
215fb 5f 47 6f 74 6f 2c 20 30 2c 20 64 65 73 74 49 66  _Goto, 0, destIf
215fc 46 61 6c 73 65 29 3b 0a 0a 20 20 20 20 20 20 2f  False);..      /
215fd 2a 20 54 68 65 20 4f 50 5f 46 6f 75 6e 64 20 61  * The OP_Found a
215fe 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69  t the top of thi
215ff 73 20 62 72 61 6e 63 68 20 6a 75 6d 70 73 20 68  s branch jumps h
21600 65 72 65 20 77 68 65 6e 20 74 72 75 65 2c 20 0a  ere when true, .
21601 20 20 20 20 20 20 2a 2a 20 63 61 75 73 69 6e 67        ** causing
21602 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 49 4e 20   the overall IN 
21603 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75  expression evalu
21604 61 74 69 6f 6e 20 74 6f 20 66 61 6c 6c 20 74 68  ation to fall th
21605 72 6f 75 67 68 2e 0a 20 20 20 20 20 20 2a 2f 0a  rough..      */.
21606 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21607 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29  eJumpHere(v, j1)
21608 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
21609 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2160a 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2160b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  .  sqlite3ExprCa
2160c 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
2160d 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  );.  VdbeComment
2160e 28 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 78 70  ((v, "end IN exp
2160f 72 22 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  r"));.}.#endif /
21610 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  * SQLITE_OMIT_SU
21611 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  BQUERY */../*.**
21612 20 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d   Duplicate an 8-
21613 62 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74  byte value.*/.st
21614 61 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62  atic char *dup8b
21615 79 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f  ytes(Vdbe *v, co
21616 6e 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20  nst char *in){. 
21617 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c   char *out = sql
21618 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
21619 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
2161a 2c 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20  , 8);.  if( out 
2161b 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75  ){.    memcpy(ou
2161c 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20  t, in, 8);.  }. 
2161d 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a   return out;.}..
2161e 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
2161f 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  n instruction th
21620 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20  at will put the 
21621 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a  floating point.*
21622 2a 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65  * value describe
21623 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69  d by z[0..n-1] i
21624 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65  nto register iMe
21625 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d  m..**.** The z[]
21626 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f   string will pro
21627 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72  bably not be zer
21628 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42  o-terminated.  B
21629 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20  ut the .** z[n] 
2162a 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61  character is gua
2162b 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f  ranteed to be so
2162c 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65  mething that doe
2162d 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69  s not look.** li
2162e 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74  ke the continuat
2162f 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ion of the numbe
21630 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
21631 64 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20  d codeReal(Vdbe 
21632 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *v, const char *
21633 7a 2c 20 69 6e 74 20 6e 65 67 61 74 65 46 6c 61  z, int negateFla
21634 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20  g, int iMem){.  
21635 69 66 28 20 41 4c 57 41 59 53 28 7a 21 3d 30 29  if( ALWAYS(z!=0)
21636 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 76   ){.    double v
21637 61 6c 75 65 3b 0a 20 20 20 20 63 68 61 72 20 2a  alue;.    char *
21638 7a 56 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41  zV;.    sqlite3A
21639 74 6f 46 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a  toF(z, &value);.
2163a 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c      assert( !sql
2163b 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 65 29  ite3IsNaN(value)
2163c 20 29 3b 20 2f 2a 20 54 68 65 20 6e 65 77 20 41   ); /* The new A
2163d 74 6f 46 20 6e 65 76 65 72 20 72 65 74 75 72 6e  toF never return
2163e 73 20 4e 61 4e 20 2a 2f 0a 20 20 20 20 69 66 28  s NaN */.    if(
2163f 20 6e 65 67 61 74 65 46 6c 61 67 20 29 20 76 61   negateFlag ) va
21640 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20  lue = -value;.  
21641 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73    zV = dup8bytes
21642 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75  (v, (char*)&valu
21643 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
21644 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
21645 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 30  Real, 0, iMem, 0
21646 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0a  , zV, P4_REAL);.
21647 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65    }.}.../*.** Ge
21648 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75  nerate an instru
21649 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20  ction that will 
2164a 70 75 74 20 74 68 65 20 69 6e 74 65 67 65 72 20  put the integer 
2164b 64 65 73 63 72 69 62 65 20 62 79 0a 2a 2a 20 74  describe by.** t
2164c 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e  ext z[0..n-1] in
2164d 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d  to register iMem
2164e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20  ..**.** The z[] 
2164f 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62  string will prob
21650 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f  ably not be zero
21651 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75  -terminated.  Bu
21652 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63  t the .** z[n] c
21653 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72  haracter is guar
21654 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d  anteed to be som
21655 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73  ething that does
21656 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b   not look.** lik
21657 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69  e the continuati
21658 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  on of the number
21659 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2165a 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64 62   codeInteger(Vdb
2165b 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 78 70  e *v, Expr *pExp
2165c 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c 20  r, int negFlag, 
2165d 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28  int iMem){.  if(
2165e 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20   pExpr->flags & 
2165f 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20  EP_IntValue ){. 
21660 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70 72     int i = pExpr
21661 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20 20  ->u.iValue;.    
21662 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69 20  if( negFlag ) i 
21663 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 65  = -i;.    sqlite
21664 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
21665 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d  P_Integer, i, iM
21666 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  em);.  }else{.  
21667 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
21668 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  = pExpr->u.zToke
21669 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a  n;.    assert( z
2166a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 73  !=0 );.    if( s
2166b 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69  qlite3FitsIn64Bi
2166c 74 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29  ts(z, negFlag) )
2166d 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c 75  {.      i64 valu
2166e 65 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  e;.      char *z
2166f 56 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  V;.      sqlite3
21670 41 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75 65  Atoi64(z, &value
21671 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67  );.      if( neg
21672 46 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d  Flag ) value = -
21673 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20  value;.      zV 
21674 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28  = dup8bytes(v, (
21675 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20  char*)&value);. 
21676 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21677 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74  AddOp4(v, OP_Int
21678 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20  64, 0, iMem, 0, 
21679 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20  zV, P4_INT64);. 
2167a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2167b 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e  codeReal(v, z, n
2167c 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20  egFlag, iMem);. 
2167d 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
2167e 2a 20 43 6c 65 61 72 20 61 20 63 61 63 68 65 20  * Clear a cache 
2167f 65 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  entry..*/.static
21680 20 76 6f 69 64 20 63 61 63 68 65 45 6e 74 72 79   void cacheEntry
21681 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 61  Clear(Parse *pPa
21682 72 73 65 2c 20 73 74 72 75 63 74 20 79 43 6f 6c  rse, struct yCol
21683 43 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 66 28  Cache *p){.  if(
21684 20 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a 20   p->tempReg ){. 
21685 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e     if( pParse->n
21686 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a  TempReg<ArraySiz
21687 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52  e(pParse->aTempR
21688 65 67 29 20 29 7b 0a 20 20 20 20 20 20 70 50 61  eg) ){.      pPa
21689 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50  rse->aTempReg[pP
2168a 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b  arse->nTempReg++
2168b 5d 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20  ] = p->iReg;.   
2168c 20 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65   }.    p->tempRe
2168d 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f  g = 0;.  }.}.../
2168e 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20 74  *.** Record in t
2168f 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20  he column cache 
21690 74 68 61 74 20 61 20 70 61 72 74 69 63 75 6c 61  that a particula
21691 72 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a  r column from a.
21692 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 74 61  ** particular ta
21693 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ble is stored in
21694 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65   a particular re
21695 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  gister..*/.SQLIT
21696 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21697 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53  qlite3ExprCacheS
21698 74 6f 72 65 28 50 61 72 73 65 20 2a 70 50 61 72  tore(Parse *pPar
21699 73 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 69 6e  se, int iTab, in
2169a 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67  t iCol, int iReg
2169b 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
2169c 74 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 20  t minLru;.  int 
2169d 69 64 78 4c 72 75 3b 0a 20 20 73 74 72 75 63 74  idxLru;.  struct
2169e 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a   yColCache *p;..
2169f 20 20 61 73 73 65 72 74 28 20 69 52 65 67 3e 30    assert( iReg>0
216a0 20 29 3b 20 20 2f 2a 20 52 65 67 69 73 74 65 72   );  /* Register
216a1 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c 77   numbers are alw
216a2 61 79 73 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a  ays positive */.
216a3 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d    assert( iCol>=
216a4 2d 31 20 26 26 20 69 43 6f 6c 3c 33 32 37 36 38  -1 && iCol<32768
216a5 20 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65 20 63   );  /* Finite c
216a6 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 2a 2f  olumn numbers */
216a7 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 72 65 70  ..  /* First rep
216a8 6c 61 63 65 20 61 6e 79 20 65 78 69 73 74 69 6e  lace any existin
216a9 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 66 6f 72  g entry */.  for
216aa 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
216ab 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
216ac 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
216ad 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
216ae 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d  f( p->iReg && p-
216af 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26  >iTable==iTab &&
216b0 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f   p->iColumn==iCo
216b1 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65  l ){.      cache
216b2 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73  EntryClear(pPars
216b3 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e  e, p);.      p->
216b4 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d  iLevel = pParse-
216b5 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20  >iCacheLevel;.  
216b6 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52      p->iReg = iR
216b7 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66  eg;.      p->aff
216b8 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20  Change = 0;.    
216b9 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73    p->lru = pPars
216ba 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a  e->iCacheCnt++;.
216bb 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
216bc 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69    }.  }..  /* Fi
216bd 6e 64 20 61 6e 20 65 6d 70 74 79 20 73 6c 6f 74  nd an empty slot
216be 20 61 6e 64 20 72 65 70 6c 61 63 65 20 69 74 20   and replace it 
216bf 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d  */.  for(i=0, p=
216c0 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
216c1 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f  e; i<SQLITE_N_CO
216c2 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b  LCACHE; i++, p++
216c3 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52  ){.    if( p->iR
216c4 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  eg==0 ){.      p
216c5 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73  ->iLevel = pPars
216c6 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a  e->iCacheLevel;.
216c7 20 20 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20        p->iTable 
216c8 3d 20 69 54 61 62 3b 0a 20 20 20 20 20 20 70 2d  = iTab;.      p-
216c9 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b  >iColumn = iCol;
216ca 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d  .      p->iReg =
216cb 20 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e   iReg;.      p->
216cc 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20  affChange = 0;. 
216cd 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20       p->tempReg 
216ce 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72  = 0;.      p->lr
216cf 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63  u = pParse->iCac
216d0 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72  heCnt++;.      r
216d1 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
216d2 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74  ..  /* Replace t
216d3 68 65 20 6c 61 73 74 20 72 65 63 65 6e 74 6c 79  he last recently
216d4 20 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72   used */.  minLr
216d5 75 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a  u = 0x7fffffff;.
216d6 20 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b 0a 20    idxLru = -1;. 
216d7 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
216d8 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
216d9 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
216da 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
216db 20 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c 6d 69     if( p->lru<mi
216dc 6e 4c 72 75 20 29 7b 0a 20 20 20 20 20 20 69 64  nLru ){.      id
216dd 78 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20 20 20  xLru = i;.      
216de 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72 75 3b  minLru = p->lru;
216df 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
216e0 20 41 4c 57 41 59 53 28 69 64 78 4c 72 75 3e 3d   ALWAYS(idxLru>=
216e1 30 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70  0) ){.    p = &p
216e2 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
216e3 5b 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20 70 2d  [idxLru];.    p-
216e4 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65  >iLevel = pParse
216e5 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20  ->iCacheLevel;. 
216e6 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69     p->iTable = i
216e7 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c  Tab;.    p->iCol
216e8 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20  umn = iCol;.    
216e9 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a  p->iReg = iReg;.
216ea 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65      p->affChange
216eb 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 74 65 6d   = 0;.    p->tem
216ec 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 70 2d  pReg = 0;.    p-
216ed 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69  >lru = pParse->i
216ee 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20  CacheCnt++;.    
216ef 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f  return;.  }.}../
216f0 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 74 68  *.** Indicate th
216f1 61 74 20 61 20 72 65 67 69 73 74 65 72 20 69 73  at a register is
216f2 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74   being overwritt
216f3 65 6e 2e 20 20 50 75 72 67 65 20 74 68 65 20 72  en.  Purge the r
216f4 65 67 69 73 74 65 72 0a 2a 2a 20 66 72 6f 6d 20  egister.** from 
216f5 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
216f6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
216f7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
216f8 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28  ExprCacheRemove(
216f9 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
216fa 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt iReg){.  int 
216fb 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
216fc 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28  Cache *p;.  for(
216fd 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
216fe 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
216ff 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
21700 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
21701 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20  ( p->iReg==iReg 
21702 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e  ){.      cacheEn
21703 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c  tryClear(pParse,
21704 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52   p);.      p->iR
21705 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  eg = 0;.    }.  
21706 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d  }.}../*.** Remem
21707 62 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  ber the current 
21708 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63 6f 6e  column cache con
21709 74 65 78 74 2e 20 20 41 6e 79 20 6e 65 77 20 65  text.  Any new e
2170a 6e 74 72 69 65 73 20 61 64 64 65 64 0a 2a 2a 20  ntries added.** 
2170b 61 64 64 65 64 20 74 6f 20 74 68 65 20 63 6f 6c  added to the col
2170c 75 6d 6e 20 63 61 63 68 65 20 61 66 74 65 72 20  umn cache after 
2170d 74 68 69 73 20 63 61 6c 6c 20 61 72 65 20 72 65  this call are re
2170e 6d 6f 76 65 64 20 77 68 65 6e 20 74 68 65 0a 2a  moved when the.*
2170f 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  * corresponding 
21710 70 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53  pop occurs..*/.S
21711 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
21712 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
21713 63 68 65 50 75 73 68 28 50 61 72 73 65 20 2a 70  chePush(Parse *p
21714 50 61 72 73 65 29 7b 0a 20 20 70 50 61 72 73 65  Parse){.  pParse
21715 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b 2b 3b  ->iCacheLevel++;
21716 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
21717 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e   from the column
21718 20 63 61 63 68 65 20 61 6e 79 20 65 6e 74 72 69   cache any entri
21719 65 73 20 74 68 61 74 20 77 65 72 65 20 61 64 64  es that were add
2171a 65 64 20 73 69 6e 63 65 20 74 68 65 0a 2a 2a 20  ed since the.** 
2171b 74 68 65 20 70 72 65 76 69 6f 75 73 20 4e 20 50  the previous N P
2171c 75 73 68 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20  ush operations. 
2171d 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
2171e 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 61 63   restore the cac
2171f 68 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61  he.** to the sta
21720 74 65 20 69 74 20 77 61 73 20 69 6e 20 4e 20 50  te it was in N P
21721 75 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51  ushes ago..*/.SQ
21722 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
21723 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
21724 68 65 50 6f 70 28 50 61 72 73 65 20 2a 70 50 61  hePop(Parse *pPa
21725 72 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69  rse, int N){.  i
21726 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79  nt i;.  struct y
21727 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 61  ColCache *p;.  a
21728 73 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a 20 20  ssert( N>0 );.  
21729 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2172a 69 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e 20 29  iCacheLevel>=N )
2172b 3b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 61 63  ;.  pParse->iCac
2172c 68 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20  heLevel -= N;.  
2172d 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73  for(i=0, p=pPars
2172e 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c  e->aColCache; i<
2172f 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
21730 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20  E; i++, p++){.  
21731 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26 26    if( p->iReg &&
21732 20 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61 72 73   p->iLevel>pPars
21733 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20 29  e->iCacheLevel )
21734 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74  {.      cacheEnt
21735 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20  ryClear(pParse, 
21736 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65  p);.      p->iRe
21737 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  g = 0;.    }.  }
21738 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61  .}../*.** When a
21739 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 69   cached column i
2173a 73 20 72 65 75 73 65 64 2c 20 6d 61 6b 65 20 73  s reused, make s
2173b 75 72 65 20 74 68 61 74 20 69 74 73 20 72 65 67  ure that its reg
2173c 69 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c  ister is.** no l
2173d 6f 6e 67 65 72 20 61 76 61 69 6c 61 62 6c 65 20  onger available 
2173e 61 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 74  as a temp regist
2173f 65 72 2e 20 20 74 69 63 6b 65 74 20 23 33 38 37  er.  ticket #387
21740 39 3a 20 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a  9:  that same.**
21741 20 72 65 67 69 73 74 65 72 20 6d 69 67 68 74 20   register might 
21742 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  be in the cache 
21743 69 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63  in multiple plac
21744 65 73 2c 20 73 6f 20 62 65 20 73 75 72 65 20 74  es, so be sure t
21745 6f 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20 61 6c  o.** get them al
21746 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  l..*/.static voi
21747 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
21748 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 50 61  hePinRegister(Pa
21749 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
2174a 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b   iReg){.  int i;
2174b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  .  struct yColCa
2174c 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d  che *p;.  for(i=
2174d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f  0, p=pParse->aCo
2174e 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45  lCache; i<SQLITE
2174f 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b  _N_COLCACHE; i++
21750 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , p++){.    if( 
21751 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b  p->iReg==iReg ){
21752 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65  .      p->tempRe
21753 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  g = 0;.    }.  }
21754 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
21755 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
21756 6c 20 65 78 74 72 61 63 74 20 74 68 65 20 69 43  l extract the iC
21757 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20  olumn-th column 
21758 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20 70 54  from.** table pT
21759 61 62 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  ab and store the
2175a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e   column value in
2175b 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 41 6e   a register.  An
2175c 20 65 66 66 6f 72 74 0a 2a 2a 20 69 73 20 6d 61   effort.** is ma
2175d 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  de to store the 
2175e 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20  column value in 
2175f 72 65 67 69 73 74 65 72 20 69 52 65 67 2c 20 62  register iReg, b
21760 75 74 20 74 68 69 73 20 69 73 0a 2a 2a 20 6e 6f  ut this is.** no
21761 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 20 54  t guaranteed.  T
21762 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74  he location of t
21763 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  he column value 
21764 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
21765 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65  ** There must be
21766 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 20   an open cursor 
21767 74 6f 20 70 54 61 62 20 69 6e 20 69 54 61 62 6c  to pTab in iTabl
21768 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  e when this rout
21769 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ine.** is called
2176a 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e 3c 30 20  .  If iColumn<0 
2176b 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65 6e  then code is gen
2176c 65 72 61 74 65 64 20 74 68 61 74 20 65 78 74 72  erated that extr
2176d 61 63 74 73 20 74 68 65 20 72 6f 77 69 64 2e 0a  acts the rowid..
2176e 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2176f 6e 65 20 6d 69 67 68 74 20 61 74 74 65 6d 70 74  ne might attempt
21770 20 74 6f 20 72 65 75 73 65 20 74 68 65 20 76 61   to reuse the va
21771 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  lue of the colum
21772 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 61 6c  n that.** has al
21773 72 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65  ready been loade
21774 64 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65  d into a registe
21775 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 77 69  r.  The value wi
21776 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20  ll always.** be 
21777 75 73 65 64 20 69 66 20 69 74 20 68 61 73 20 6e  used if it has n
21778 6f 74 20 75 6e 64 65 72 67 6f 6e 65 20 61 6e 79  ot undergone any
21779 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65   affinity change
2177a 73 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20 61 6e  s.  But if.** an
2177b 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65   affinity change
2177c 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20 74   has occurred, t
2177d 68 65 6e 20 74 68 65 20 63 61 63 68 65 64 20 76  hen the cached v
2177e 61 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 62  alue will only b
2177f 65 0a 2a 2a 20 75 73 65 64 20 69 66 20 61 6c 6c  e.** used if all
21780 6f 77 41 66 66 43 68 6e 67 20 69 73 20 74 72 75  owAffChng is tru
21781 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
21782 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21783 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d  ExprCodeGetColum
21784 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
21785 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  se,   /* Parsing
21786 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61   and code genera
21787 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ting context */.
21788 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
21789 20 20 20 2f 2a 20 44 65 73 63 72 69 70 74 69 6f     /* Descriptio
2178a 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77  n of the table w
2178b 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 66 72  e are reading fr
2178c 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  om */.  int iCol
2178d 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e 64 65  umn,     /* Inde
2178e 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63  x of the table c
2178f 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  olumn */.  int i
21790 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20 54  Table,      /* T
21791 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69  he cursor pointi
21792 6e 67 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ng to the table 
21793 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20 20  */.  int iReg,  
21794 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 72        /* Store r
21795 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20  esults here */. 
21796 20 69 6e 74 20 61 6c 6c 6f 77 41 66 66 43 68 6e   int allowAffChn
21797 67 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 69  g /* True if pri
21798 6f 72 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e  or affinity chan
21799 67 65 73 20 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b  ges are OK */.){
2179a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
2179b 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
2179c 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43  t i;.  struct yC
2179d 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66  olCache *p;..  f
2179e 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65  or(i=0, p=pParse
2179f 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53  ->aColCache; i<S
217a0 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45  QLITE_N_COLCACHE
217a1 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20  ; i++, p++){.   
217a2 20 69 66 28 20 70 2d 3e 69 52 65 67 3e 30 20 26   if( p->iReg>0 &
217a3 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61  & p->iTable==iTa
217a4 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d  ble && p->iColum
217a5 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20  n==iColumn.     
217a6 20 20 20 20 20 20 26 26 20 28 21 70 2d 3e 61 66        && (!p->af
217a7 66 43 68 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77  fChange || allow
217a8 41 66 66 43 68 6e 67 29 20 29 7b 0a 20 20 20 20  AffChng) ){.    
217a9 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73    p->lru = pPars
217aa 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a  e->iCacheCnt++;.
217ab 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
217ac 72 43 61 63 68 65 50 69 6e 52 65 67 69 73 74 65  rCachePinRegiste
217ad 72 28 70 50 61 72 73 65 2c 20 70 2d 3e 69 52 65  r(pParse, p->iRe
217ae 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  g);.      return
217af 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a   p->iReg;.    }.
217b0 20 20 7d 20 20 0a 20 20 61 73 73 65 72 74 28 20    }  .  assert( 
217b1 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 43  v!=0 );.  if( iC
217b2 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 73  olumn<0 ){.    s
217b3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
217b4 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54  (v, OP_Rowid, iT
217b5 61 62 6c 65 2c 20 69 52 65 67 29 3b 0a 20 20 7d  able, iReg);.  }
217b6 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28  else if( ALWAYS(
217b7 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 20 20  pTab!=0) ){.    
217b8 69 6e 74 20 6f 70 20 3d 20 49 73 56 69 72 74 75  int op = IsVirtu
217b9 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f 56 43  al(pTab) ? OP_VC
217ba 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d  olumn : OP_Colum
217bb 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  n;.    sqlite3Vd
217bc 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20  beAddOp3(v, op, 
217bd 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c  iTable, iColumn,
217be 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69   iReg);.    sqli
217bf 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74  te3ColumnDefault
217c0 28 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c 75 6d  (v, pTab, iColum
217c1 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 0a 20 20  n, iReg);.  }.  
217c2 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
217c3 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69 54  Store(pParse, iT
217c4 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69  able, iColumn, i
217c5 52 65 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  Reg);.  return i
217c6 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  Reg;.}../*.** Cl
217c7 65 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63  ear all column c
217c8 61 63 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f  ache entries..*/
217c9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
217ca 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
217cb 43 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65  CacheClear(Parse
217cc 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74   *pParse){.  int
217cd 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f   i;.  struct yCo
217ce 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f  lCache *p;..  fo
217cf 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d  r(i=0, p=pParse-
217d0 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51  >aColCache; i<SQ
217d1 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b  LITE_N_COLCACHE;
217d2 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20   i++, p++){.    
217d3 69 66 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20  if( p->iReg ){. 
217d4 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43       cacheEntryC
217d5 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b  lear(pParse, p);
217d6 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d  .      p->iReg =
217d7 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   0;.    }.  }.}.
217d8 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68  ./*.** Record th
217d9 65 20 66 61 63 74 20 74 68 61 74 20 61 6e 20 61  e fact that an a
217da 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68  ffinity change h
217db 61 73 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69  as occurred on i
217dc 43 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65  Count.** registe
217dd 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 68  rs starting with
217de 20 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49   iStart..*/.SQLI
217df 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
217e0 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
217e1 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50  AffinityChange(P
217e2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
217e3 74 20 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43  t iStart, int iC
217e4 6f 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e  ount){.  int iEn
217e5 64 20 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f  d = iStart + iCo
217e6 75 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69  unt - 1;.  int i
217e7 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
217e8 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69  ache *p;.  for(i
217e9 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
217ea 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
217eb 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
217ec 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  +, p++){.    int
217ed 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20   r = p->iReg;.  
217ee 20 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20    if( r>=iStart 
217ef 26 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20  && r<=iEnd ){.  
217f0 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65      p->affChange
217f1 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
217f2 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
217f3 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 63  e code to move c
217f4 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69  ontent from regi
217f5 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46  sters iFrom...iF
217f6 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76  rom+nReg-1.** ov
217f7 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e  er to iTo..iTo+n
217f8 52 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 65 20  Reg-1. Keep the 
217f9 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 70 2d  column cache up-
217fa 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49  to-date..*/.SQLI
217fb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
217fc 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d  sqlite3ExprCodeM
217fd 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ove(Parse *pPars
217fe 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e  e, int iFrom, in
217ff 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29  t iTo, int nReg)
21800 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
21801 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70  uct yColCache *p
21802 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 46  ;.  if( NEVER(iF
21803 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74 75  rom==iTo) ) retu
21804 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  rn;.  sqlite3Vdb
21805 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d 3e  eAddOp3(pParse->
21806 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20  pVdbe, OP_Move, 
21807 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52 65 67  iFrom, iTo, nReg
21808 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d  );.  for(i=0, p=
21809 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
2180a 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f  e; i<SQLITE_N_CO
2180b 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b  LCACHE; i++, p++
2180c 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70  ){.    int x = p
2180d 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20  ->iReg;.    if( 
2180e 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69 46  x>=iFrom && x<iF
2180f 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20  rom+nReg ){.    
21810 20 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69 54 6f    p->iReg += iTo
21811 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20  -iFrom;.    }.  
21812 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
21813 61 74 65 20 63 6f 64 65 20 74 6f 20 63 6f 70 79  ate code to copy
21814 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65   content from re
21815 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e  gisters iFrom...
21816 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20  iFrom+nReg-1.** 
21817 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f  over to iTo..iTo
21818 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49  +nReg-1..*/.SQLI
21819 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2181a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43  sqlite3ExprCodeC
2181b 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 73  opy(Parse *pPars
2181c 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e  e, int iFrom, in
2181d 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29  t iTo, int nReg)
2181e 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
2181f 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54   NEVER(iFrom==iT
21820 6f 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  o) ) return;.  f
21821 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20  or(i=0; i<nReg; 
21822 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
21823 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72  3VdbeAddOp2(pPar
21824 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f  se->pVdbe, OP_Co
21825 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f  py, iFrom+i, iTo
21826 2b 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  +i);.  }.}../*.*
21827 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
21828 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 69 6e   any register in
21829 20 74 68 65 20 72 61 6e 67 65 20 69 46 72 6f 6d   the range iFrom
2182a 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 65  ..iTo (inclusive
2182b 29 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 73 20  ).** is used as 
2182c 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6c 75  part of the colu
2182d 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61  mn cache..*/.sta
2182e 74 69 63 20 69 6e 74 20 75 73 65 64 41 73 43 6f  tic int usedAsCo
2182f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20  lumnCache(Parse 
21830 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72  *pParse, int iFr
21831 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20  om, int iTo){.  
21832 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
21833 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20  yColCache *p;.  
21834 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73  for(i=0, p=pPars
21835 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c  e->aColCache; i<
21836 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
21837 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20  E; i++, p++){.  
21838 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65    int r = p->iRe
21839 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46  g;.    if( r>=iF
2183a 72 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20  rom && r<=iTo ) 
2183b 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
2183c 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
2183d 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20 69  ** If the last i
2183e 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65 64  nstruction coded
2183f 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c   is an ephemeral
21840 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a   copy of any of.
21841 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 73  ** the registers
21842 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65 67   in the nReg reg
21843 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67  isters beginning
21844 20 77 69 74 68 20 69 52 65 67 2c 20 74 68 65 6e   with iReg, then
21845 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65 20  .** convert the 
21846 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  last instruction
21847 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74   from OP_SCopy t
21848 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51  o OP_Copy..*/.SQ
21849 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2184a 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72  d sqlite3ExprHar
2184b 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61  dCopy(Parse *pPa
2184c 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69  rse, int iReg, i
2184d 6e 74 20 6e 52 65 67 29 7b 0a 20 20 56 64 62 65  nt nReg){.  Vdbe
2184e 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 65 20  Op *pOp;.  Vdbe 
2184f 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  *v;..  assert( p
21850 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
21851 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20  cFailed==0 );.  
21852 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
21853 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  e;.  assert( v!=
21854 30 20 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c  0 );.  pOp = sql
21855 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
21856 20 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20   -1);.  assert( 
21857 70 4f 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  pOp!=0 );.  if( 
21858 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
21859 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70 31  SCopy && pOp->p1
2185a 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e 70  >=iReg && pOp->p
2185b 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a 20  1<iReg+nReg ){. 
2185c 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d     pOp->opcode =
2185d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a   OP_Copy;.  }.}.
2185e 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2185f 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68  code to store th
21860 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69  e value of the i
21861 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69  Alias-th alias i
21862 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 61  n register.** ta
21863 72 67 65 74 2e 20 20 54 68 65 20 66 69 72 73 74  rget.  The first
21864 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61   time this is ca
21865 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20 65  lled, pExpr is e
21866 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d 70  valuated to comp
21867 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  ute.** the value
21868 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20 20   of the alias.  
21869 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f  The value is sto
2186a 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c 69  red in an auxili
2186b 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a 20  ary register.** 
2186c 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  and the number o
2186d 66 20 74 68 61 74 20 72 65 67 69 73 74 65 72 20  f that register 
2186e 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 6e  is returned.  On
2186f 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
21870 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74  s,.** the regist
21871 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65 74  er number is ret
21872 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67 65  urned without ge
21873 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f 64  nerating any cod
21874 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  e..**.** Note th
21875 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20  at in order for 
21876 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63 6f  this to work, co
21877 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65 72  de must be gener
21878 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 73  ated in the.** s
21879 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20 69  ame order that i
2187a 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a  t is executed..*
2187b 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72 65  *.** Aliases are
2187c 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69   numbered starti
2187d 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20 69  ng with 1.  So i
2187e 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65 20  Alias is in the 
2187f 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74 6f  range.** of 1 to
21880 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20   pParse->nAlias 
21881 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a  inclusive.  .**.
21882 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61  ** pParse->aAlia
21883 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63 6f  s[iAlias-1] reco
21884 72 64 73 20 74 68 65 20 72 65 67 69 73 74 65 72  rds the register
21885 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 68   number where th
21886 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68  e value.** of th
21887 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61  e iAlias-th alia
21888 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49 66  s is stored.  If
21889 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e   zero, that mean
2188a 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 6c  s that the.** al
2188b 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74 20  ias has not yet 
2188c 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a 2a  been computed..*
2188d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64  /.static int cod
2188e 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70 50  eAlias(Parse *pP
2188f 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61 73  arse, int iAlias
21890 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69  , Expr *pExpr, i
21891 6e 74 20 74 61 72 67 65 74 29 7b 0a 23 69 66 20  nt target){.#if 
21892 30 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  0.  sqlite3 *db 
21893 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
21894 69 6e 74 20 69 52 65 67 3b 0a 20 20 69 66 28 20  int iReg;.  if( 
21895 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c  pParse->nAliasAl
21896 6c 6f 63 3c 70 50 61 72 73 65 2d 3e 6e 41 6c 69  loc<pParse->nAli
21897 61 73 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  as ){.    pParse
21898 2d 3e 61 41 6c 69 61 73 20 3d 20 73 71 6c 69 74  ->aAlias = sqlit
21899 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
2189a 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 41  e(db, pParse->aA
2189b 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lias,.          
2189c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2189d 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 50         sizeof(pP
2189e 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29  arse->aAlias[0])
2189f 2a 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20  *pParse->nAlias 
218a0 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
218a1 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
218a2 64 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 41 6c  d && pParse->nAl
218a3 69 61 73 41 6c 6c 6f 63 3e 30 20 29 3b 0a 20 20  iasAlloc>0 );.  
218a4 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
218a5 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20  Failed ) return 
218a6 30 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70  0;.    memset(&p
218a7 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 70 50  Parse->aAlias[pP
218a8 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f  arse->nAliasAllo
218a9 63 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  c], 0,.         
218aa 20 20 28 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61    (pParse->nAlia
218ab 73 2d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  s-pParse->nAlias
218ac 41 6c 6c 6f 63 29 2a 73 69 7a 65 6f 66 28 70 50  Alloc)*sizeof(pP
218ad 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29  arse->aAlias[0])
218ae 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  );.    pParse->n
218af 41 6c 69 61 73 41 6c 6c 6f 63 20 3d 20 70 50 61  AliasAlloc = pPa
218b0 72 73 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 7d  rse->nAlias;.  }
218b1 0a 20 20 61 73 73 65 72 74 28 20 69 41 6c 69 61  .  assert( iAlia
218b2 73 3e 30 20 26 26 20 69 41 6c 69 61 73 3c 3d 70  s>0 && iAlias<=p
218b3 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b  Parse->nAlias );
218b4 0a 20 20 69 52 65 67 20 3d 20 70 50 61 72 73 65  .  iReg = pParse
218b5 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d  ->aAlias[iAlias-
218b6 31 5d 3b 0a 20 20 69 66 28 20 69 52 65 67 3d 3d  1];.  if( iReg==
218b7 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  0 ){.    if( pPa
218b8 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c  rse->iCacheLevel
218b9 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 52 65 67  >0 ){.      iReg
218ba 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
218bb 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
218bc 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b   pExpr, target);
218bd 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
218be 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73    iReg = ++pPars
218bf 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73  e->nMem;.      s
218c0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
218c1 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 69 52  Parse, pExpr, iR
218c2 65 67 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  eg);.      pPars
218c3 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73  e->aAlias[iAlias
218c4 2d 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 20 20  -1] = iReg;.    
218c5 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  }.  }.  return i
218c6 52 65 67 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55  Reg;.#else.  UNU
218c7 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 41  SED_PARAMETER(iA
218c8 6c 69 61 73 29 3b 0a 20 20 72 65 74 75 72 6e 20  lias);.  return 
218c9 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
218ca 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45  arget(pParse, pE
218cb 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 23 65  xpr, target);.#e
218cc 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ndif.}../*.** Ge
218cd 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f  nerate code into
218ce 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62   the current Vdb
218cf 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68  e to evaluate th
218d0 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65  e given.** expre
218d1 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20  ssion.  Attempt 
218d2 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  to store the res
218d3 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72  ults in register
218d4 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65   "target"..** Re
218d5 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65  turn the registe
218d6 72 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20  r where results 
218d7 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a  are stored..**.*
218d8 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f 75 74  * With this rout
218d9 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  ine, there is no
218da 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
218db 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20  results will.** 
218dc 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61 72  be stored in tar
218dd 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74  get.  The result
218de 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65 64   might be stored
218df 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a   in some other.*
218e0 2a 20 72 65 67 69 73 74 65 72 20 69 66 20 69 74  * register if it
218e1 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74   is convenient t
218e2 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 63 61  o do so.  The ca
218e3 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a  lling function.*
218e4 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65  * must check the
218e5 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64   return code and
218e6 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75 6c 74   move the result
218e7 73 20 74 6f 20 74 68 65 20 64 65 73 69 72 65 64  s to the desired
218e8 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f  .** register..*/
218e9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
218ea 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
218eb 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 65 20  odeTarget(Parse 
218ec 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
218ed 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74  Expr, int target
218ee 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
218ef 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f  Parse->pVdbe;  /
218f0 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72 20 63  * The VM under c
218f1 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  onstruction */. 
218f2 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20   int op;        
218f3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
218f4 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 20 63  e opcode being c
218f5 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  oded */.  int in
218f6 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20 20 20  Reg = target;   
218f7 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 73      /* Results s
218f8 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
218f9 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74  r inReg */.  int
218fa 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20 20   regFree1 = 0;  
218fb 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e         /* If non
218fc 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 20  -zero free this 
218fd 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
218fe 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46  er */.  int regF
218ff 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20 20 20  ree2 = 0;       
21900 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f    /* If non-zero
21901 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f   free this tempo
21902 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f  rary register */
21903 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20 72  .  int r1, r2, r
21904 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f 2a 20  3, r4;       /* 
21905 56 61 72 69 6f 75 73 20 72 65 67 69 73 74 65 72  Various register
21906 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71   numbers */.  sq
21907 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
21908 73 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65 20 64  se->db; /* The d
21909 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2190a 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  on */..  assert(
2190b 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72   target>0 && tar
2190c 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65  get<=pParse->nMe
2190d 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20  m );.  if( v==0 
2190e 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
2190f 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
21910 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72  cFailed );.    r
21911 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
21912 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b 0a  if( pExpr==0 ){.
21913 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c      op = TK_NULL
21914 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f  ;.  }else{.    o
21915 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20  p = pExpr->op;. 
21916 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20   }.  switch( op 
21917 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
21918 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20  GG_COLUMN: {.   
21919 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67     AggInfo *pAgg
2191a 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41  Info = pExpr->pA
2191b 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 74  ggInfo;.      st
2191c 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c  ruct AggInfo_col
2191d 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e   *pCol = &pAggIn
2191e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e  fo->aCol[pExpr->
2191f 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 66 28  iAgg];.      if(
21920 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65   !pAggInfo->dire
21921 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  ctMode ){.      
21922 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e    assert( pCol->
21923 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 20 20  iMem>0 );.      
21924 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e    inReg = pCol->
21925 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 62 72  iMem;.        br
21926 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  eak;.      }else
21927 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75   if( pAggInfo->u
21928 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a  seSortingIdx ){.
21929 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2192a 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2192b 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f  Column, pAggInfo
2192c 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20  ->sortingIdx,.  
2192d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2192e 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
2192f 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c  ->iSorterColumn,
21930 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
21931 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
21932 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77  .      /* Otherw
21933 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 20 69  ise, fall thru i
21934 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d  nto the TK_COLUM
21935 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a  N case */.    }.
21936 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55      case TK_COLU
21937 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  MN: {.      if( 
21938 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20  pExpr->iTable<0 
21939 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
2193a 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20  is only happens 
2193b 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65 63  when coding chec
2193c 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  k constraints */
2193d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2193e 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e   pParse->ckBase>
2193f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52  0 );.        inR
21940 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c  eg = pExpr->iCol
21941 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63 6b  umn + pParse->ck
21942 42 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Base;.      }els
21943 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  e{.        testc
21944 61 73 65 28 20 28 70 45 78 70 72 2d 3e 66 6c 61  ase( (pExpr->fla
21945 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 21  gs & EP_AnyAff)!
21946 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e  =0 );.        in
21947 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
21948 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70  rCodeGetColumn(p
21949 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 54  Parse, pExpr->pT
2194a 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ab,.            
2194b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2194c 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
2194d 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  umn, pExpr->iTab
2194e 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 20  le, target,.    
2194f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21950 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
21951 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41  pr->flags & EP_A
21952 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a  nyAff);.      }.
21953 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21954 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49   }.    case TK_I
21955 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20  NTEGER: {.      
21956 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70  codeInteger(v, p
21957 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74 29  Expr, 0, target)
21958 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
21959 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
2195a 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20  _FLOAT: {.      
2195b 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2195c 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2195d 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a  EP_IntValue) );.
2195e 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76        codeReal(v
2195f 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  , pExpr->u.zToke
21960 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20  n, 0, target);. 
21961 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21962 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54  }.    case TK_ST
21963 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 61 73  RING: {.      as
21964 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21965 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21966 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
21967 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21968 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
21969 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c 20  ng8, 0, target, 
2196a 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  0, pExpr->u.zTok
2196b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72  en, 0);.      br
2196c 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
2196d 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20  ase TK_NULL: {. 
2196e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2196f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
21970 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20  l, 0, target);. 
21971 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21972 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
21973 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52  _OMIT_BLOB_LITER
21974 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42  AL.    case TK_B
21975 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  LOB: {.      int
21976 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   n;.      const 
21977 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 63  char *z;.      c
21978 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20  har *zBlob;.    
21979 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2197a 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2197b 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
2197c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2197d 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b  pExpr->u.zToken[
2197e 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 70 72  0]=='x' || pExpr
2197f 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27  ->u.zToken[0]=='
21980 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  X' );.      asse
21981 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  rt( pExpr->u.zTo
21982 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a  ken[1]=='\'' );.
21983 20 20 20 20 20 20 7a 20 3d 20 26 70 45 78 70 72        z = &pExpr
21984 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20  ->u.zToken[2];. 
21985 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33       n = sqlite3
21986 53 74 72 6c 65 6e 33 30 28 7a 29 20 2d 20 31 3b  Strlen30(z) - 1;
21987 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a  .      assert( z
21988 5b 6e 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20  [n]=='\'' );.   
21989 20 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74     zBlob = sqlit
2198a 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69  e3HexToBlob(sqli
2198b 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c  te3VdbeDb(v), z,
2198c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   n);.      sqlit
2198d 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2198e 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61  OP_Blob, n/2, ta
2198f 72 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20  rget, 0, zBlob, 
21990 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P4_DYNAMIC);.   
21991 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21992 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20  #endif.    case 
21993 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20  TK_VARIABLE: {. 
21994 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70       VdbeOp *pOp
21995 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21996 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
21997 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
21998 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61 73  lue) );.      as
21999 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a  sert( pExpr->u.z
2199a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20 20  Token!=0 );.    
2199b 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
2199c 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20  >u.zToken[0]!=0 
2199d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78  );.      if( pEx
2199e 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d  pr->u.zToken[1]=
2199f 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  =0.         && (
219a0 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  pOp = sqlite3Vdb
219a1 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 29 2d 3e  eGetOp(v, -1))->
219a2 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61  opcode==OP_Varia
219a3 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26 26 20  ble.         && 
219a4 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d  pOp->p1+pOp->p3=
219a5 3d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 0a  =pExpr->iColumn.
219a6 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d           && pOp-
219a7 3e 70 32 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72  >p2+pOp->p3==tar
219a8 67 65 74 0a 20 20 20 20 20 20 20 20 20 26 26 20  get.         && 
219a9 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20  pOp->p4.z==0.   
219aa 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a     ){.        /*
219ab 20 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   If the previous
219ac 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73   instruction was
219ad 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70   a copy of the p
219ae 72 65 76 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a  revious unnamed.
219af 20 20 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d          ** param
219b0 65 74 65 72 20 69 6e 74 6f 20 74 68 65 20 70 72  eter into the pr
219b1 65 76 69 6f 75 73 20 72 65 67 69 73 74 65 72 2c  evious register,
219b2 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63   then simply inc
219b3 72 65 6d 65 6e 74 20 74 68 65 0a 20 20 20 20 20  rement the.     
219b4 20 20 20 2a 2a 20 72 65 70 65 61 74 20 63 6f 75     ** repeat cou
219b5 6e 74 20 6f 6e 20 74 68 65 20 70 72 69 6f 72 20  nt on the prior 
219b6 69 6e 73 74 72 75 63 74 69 6f 6e 20 72 61 74 68  instruction rath
219b7 65 72 20 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61  er than making a
219b8 20 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a 20   new.        ** 
219b9 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20  instruction..   
219ba 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
219bb 70 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20  pOp->p3++;.     
219bc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
219bd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
219be 33 28 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65  3(v, OP_Variable
219bf 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  , pExpr->iColumn
219c0 2c 20 74 61 72 67 65 74 2c 20 31 29 3b 0a 20 20  , target, 1);.  
219c1 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
219c2 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20  >u.zToken[1]!=0 
219c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
219c4 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
219c5 28 76 2c 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75  (v, -1, pExpr->u
219c6 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20  .zToken, 0);.   
219c7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
219c8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
219c9 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45  }.    case TK_RE
219ca 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20  GISTER: {.      
219cb 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69  inReg = pExpr->i
219cc 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65  Table;.      bre
219cd 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
219ce 73 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20  se TK_AS: {.    
219cf 20 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c    inReg = codeAl
219d0 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70  ias(pParse, pExp
219d1 72 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72  r->iTable, pExpr
219d2 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29  ->pLeft, target)
219d3 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
219d4 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
219d5 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20  ITE_OMIT_CAST.  
219d6 20 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20    case TK_CAST: 
219d7 7b 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65  {.      /* Expre
219d8 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f  ssions of the fo
219d9 72 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74  rm:   CAST(pLeft
219da 20 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20   AS token) */.  
219db 20 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f      int aff, to_
219dc 6f 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20  op;.      inReg 
219dd 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
219de 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20  eTarget(pParse, 
219df 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61  pExpr->pLeft, ta
219e0 72 67 65 74 29 3b 0a 20 20 20 20 20 20 61 73 73  rget);.      ass
219e1 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
219e2 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
219e3 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
219e4 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33     aff = sqlite3
219e5 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78  AffinityType(pEx
219e6 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20  pr->u.zToken);. 
219e7 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66       to_op = aff
219e8 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   - SQLITE_AFF_TE
219e9 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a  XT + OP_ToText;.
219ea 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
219eb 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20  _op==OP_ToText  
219ec 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45    || aff!=SQLITE
219ed 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a  _AFF_TEXT    );.
219ee 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
219ef 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20  _op==OP_ToBlob  
219f0 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45    || aff!=SQLITE
219f1 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a  _AFF_NONE    );.
219f2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
219f3 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69  _op==OP_ToNumeri
219f4 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45  c || aff!=SQLITE
219f5 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a  _AFF_NUMERIC );.
219f6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
219f7 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20  _op==OP_ToInt   
219f8 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45    || aff!=SQLITE
219f9 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a  _AFF_INTEGER );.
219fa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
219fb 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20  _op==OP_ToReal  
219fc 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45    || aff!=SQLITE
219fd 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a  _AFF_REAL    );.
219fe 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
219ff 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74  to_op==OP_ToText
21a00 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21a01 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  se( to_op==OP_To
21a02 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65  Blob );.      te
21a03 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f  stcase( to_op==O
21a04 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20  P_ToNumeric );. 
21a05 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74       testcase( t
21a06 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29  o_op==OP_ToInt )
21a07 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21a08 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65  ( to_op==OP_ToRe
21a09 61 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  al );.      if( 
21a0a 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b  inReg!=target ){
21a0b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21a0c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21a0d 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74  _SCopy, inReg, t
21a0e 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20  arget);.        
21a0f 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a  inReg = target;.
21a10 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
21a11 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
21a12 76 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29  v, to_op, inReg)
21a13 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21a14 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61  ( usedAsColumnCa
21a15 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65  che(pParse, inRe
21a16 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20  g, inReg) );.   
21a17 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
21a18 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
21a19 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c  e(pParse, inReg,
21a1a 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
21a1b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
21a1c 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  * SQLITE_OMIT_CA
21a1d 53 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54  ST */.    case T
21a1e 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54  K_LT:.    case T
21a1f 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_LE:.    case T
21a20 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54  K_GT:.    case T
21a21 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_GE:.    case T
21a22 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_NE:.    case T
21a23 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73  K_EQ: {.      as
21a24 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f  sert( TK_LT==OP_
21a25 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Lt );.      asse
21a26 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65  rt( TK_LE==OP_Le
21a27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21a28 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29  ( TK_GT==OP_Gt )
21a29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21a2a 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a  TK_GE==OP_Ge );.
21a2b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21a2c 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20  _EQ==OP_Eq );.  
21a2d 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e      assert( TK_N
21a2e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20  E==OP_Ne );.    
21a2f 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21a30 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74  TK_LT );.      t
21a31 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21a32 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LE );.      test
21a33 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20  case( op==TK_GT 
21a34 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21a35 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a  e( op==TK_GE );.
21a36 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21a37 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20  op==TK_EQ );.   
21a38 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21a39 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20  =TK_NE );.      
21a3a 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61  codeCompareOpera
21a3b 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70  nds(pParse, pExp
21a3c 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26  r->pLeft, &r1, &
21a3d 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20  regFree1,.      
21a3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a3f 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
21a40 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20  r->pRight, &r2, 
21a41 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
21a42 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50    codeCompare(pP
21a43 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
21a44 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ft, pExpr->pRigh
21a45 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20  t, op,.         
21a46 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c           r1, r2,
21a47 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53   inReg, SQLITE_S
21a48 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74  TOREP2);.      t
21a49 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
21a4a 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
21a4b 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
21a4c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
21a4d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
21a4e 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61  se TK_IS:.    ca
21a4f 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20  se TK_ISNOT: {. 
21a50 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21a51 70 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20  p==TK_IS );.    
21a52 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21a53 54 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20  TK_ISNOT );.    
21a54 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
21a55 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
21a56 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
21a57 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
21a58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
21a5a 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
21a5b 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
21a5c 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b      op = (op==TK
21a5d 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54  _IS) ? TK_EQ : T
21a5e 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65  K_NE;.      code
21a5f 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
21a60 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45  pExpr->pLeft, pE
21a61 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c  xpr->pRight, op,
21a62 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21a63 20 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67     r1, r2, inReg
21a64 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  , SQLITE_STOREP2
21a65 20 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51   | SQLITE_NULLEQ
21a66 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21a67 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
21a68 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21a69 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b  ( regFree2==0 );
21a6a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21a6b 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21a6c 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  AND:.    case TK
21a6d 5f 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _OR:.    case TK
21a6e 5f 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65 20  _PLUS:.    case 
21a6f 54 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61 73  TK_STAR:.    cas
21a70 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20  e TK_MINUS:.    
21a71 63 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20  case TK_REM:.   
21a72 20 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a   case TK_BITAND:
21a73 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54  .    case TK_BIT
21a74 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  OR:.    case TK_
21a75 53 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65 20  SLASH:.    case 
21a76 54 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20 63  TK_LSHIFT:.    c
21a77 61 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a  ase TK_RSHIFT: .
21a78 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43      case TK_CONC
21a79 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  AT: {.      asse
21a7a 72 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41  rt( TK_AND==OP_A
21a7b 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  nd );.      asse
21a7c 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72  rt( TK_OR==OP_Or
21a7d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21a7e 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64  ( TK_PLUS==OP_Ad
21a7f 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  d );.      asser
21a80 74 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f  t( TK_MINUS==OP_
21a81 53 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20 20  Subtract );.    
21a82 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45 4d    assert( TK_REM
21a83 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29  ==OP_Remainder )
21a84 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21a85 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69  TK_BITAND==OP_Bi
21a86 74 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73  tAnd );.      as
21a87 73 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d  sert( TK_BITOR==
21a88 4f 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20 20  OP_BitOr );.    
21a89 20 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c 41    assert( TK_SLA
21a8a 53 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b  SH==OP_Divide );
21a8b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21a8c 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69  K_LSHIFT==OP_Shi
21a8d 66 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20  ftLeft );.      
21a8e 61 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49 46  assert( TK_RSHIF
21a8f 54 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74  T==OP_ShiftRight
21a90 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21a91 28 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f  ( TK_CONCAT==OP_
21a92 43 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20 20  Concat );.      
21a93 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21a94 5f 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65  _AND );.      te
21a95 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f  stcase( op==TK_O
21a96 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  R );.      testc
21a97 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53  ase( op==TK_PLUS
21a98 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21a99 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53  se( op==TK_MINUS
21a9a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21a9b 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29  se( op==TK_REM )
21a9c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21a9d 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20  ( op==TK_BITAND 
21a9e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21a9f 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20  e( op==TK_BITOR 
21aa0 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21aa1 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20  e( op==TK_SLASH 
21aa2 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21aa3 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54  e( op==TK_LSHIFT
21aa4 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21aa5 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46  se( op==TK_RSHIF
21aa6 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
21aa7 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43  ase( op==TK_CONC
21aa8 41 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  AT );.      r1 =
21aa9 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21aaa 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
21aab 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
21aac 72 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20  ree1);.      r2 
21aad 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
21aae 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45  eTemp(pParse, pE
21aaf 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65  xpr->pRight, &re
21ab0 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 73  gFree2);.      s
21ab1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
21ab2 28 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20  (v, op, r2, r1, 
21ab3 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74  target);.      t
21ab4 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
21ab5 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
21ab6 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
21ab7 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
21ab8 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
21ab9 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a  se TK_UMINUS: {.
21aba 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66        Expr *pLef
21abb 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  t = pExpr->pLeft
21abc 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21abd 70 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 69  pLeft );.      i
21abe 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  f( pLeft->op==TK
21abf 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20  _FLOAT ){.      
21ac0 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
21ac1 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
21ac2 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
21ac3 3b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 52 65  ;.        codeRe
21ac4 61 6c 28 76 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a  al(v, pLeft->u.z
21ac5 54 6f 6b 65 6e 2c 20 31 2c 20 74 61 72 67 65 74  Token, 1, target
21ac6 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
21ac7 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  f( pLeft->op==TK
21ac8 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20  _INTEGER ){.    
21ac9 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28      codeInteger(
21aca 76 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 61 72  v, pLeft, 1, tar
21acb 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  get);.      }els
21acc 65 7b 0a 20 20 20 20 20 20 20 20 72 65 67 46 72  e{.        regFr
21acd 65 65 31 20 3d 20 72 31 20 3d 20 73 71 6c 69 74  ee1 = r1 = sqlit
21ace 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
21acf 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
21ad0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21ad1 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
21ad2 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 72  , r1);.        r
21ad3 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  2 = sqlite3ExprC
21ad4 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
21ad5 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
21ad6 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
21ad7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21ad8 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61  Op3(v, OP_Subtra
21ad9 63 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67  ct, r2, r1, targ
21ada 65 74 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  et);.        tes
21adb 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
21adc 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  =0 );.      }.  
21add 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67      inReg = targ
21ade 65 74 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  et;.      break;
21adf 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21ae0 54 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63  TK_BITNOT:.    c
21ae1 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20  ase TK_NOT: {.  
21ae2 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42      assert( TK_B
21ae3 49 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74  ITNOT==OP_BitNot
21ae4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21ae5 28 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74  ( TK_NOT==OP_Not
21ae6 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21ae7 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f  se( op==TK_BITNO
21ae8 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
21ae9 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20  ase( op==TK_NOT 
21aea 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71  );.      r1 = sq
21aeb 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
21aec 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
21aed 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65  >pLeft, &regFree
21aee 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  1);.      testca
21aef 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
21af0 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d  );.      inReg =
21af1 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 73   target;.      s
21af2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21af3 28 76 2c 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65  (v, op, r1, inRe
21af4 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  g);.      break;
21af5 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21af6 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63  TK_ISNULL:.    c
21af7 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
21af8 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  {.      int addr
21af9 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21afa 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73  TK_ISNULL==OP_Is
21afb 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73  Null );.      as
21afc 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  sert( TK_NOTNULL
21afd 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a  ==OP_NotNull );.
21afe 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21aff 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b  op==TK_ISNULL );
21b00 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21b01 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20   op==TK_NOTNULL 
21b02 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21b03 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21b04 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72  _Integer, 1, tar
21b05 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  get);.      r1 =
21b06 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21b07 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
21b08 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
21b09 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73  ree1);.      tes
21b0a 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21b0b 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72  =0 );.      addr
21b0c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
21b0d 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b  dOp1(v, op, r1);
21b0e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
21b0f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
21b10 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d  ddImm, target, -
21b11 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
21b12 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
21b13 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 62 72   addr);.      br
21b14 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
21b15 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  ase TK_AGG_FUNCT
21b16 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67  ION: {.      Agg
21b17 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45  Info *pInfo = pE
21b18 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20  xpr->pAggInfo;. 
21b19 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d       if( pInfo==
21b1a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  0 ){.        ass
21b1b 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
21b1c 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21b1d 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
21b1e 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
21b1f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69  rMsg(pParse, "mi
21b20 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74  suse of aggregat
21b21 65 3a 20 25 73 28 29 22 2c 20 70 45 78 70 72 2d  e: %s()", pExpr-
21b22 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
21b23 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
21b24 20 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e   inReg = pInfo->
21b25 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67  aFunc[pExpr->iAg
21b26 67 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d  g].iMem;.      }
21b27 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21b28 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21b29 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20  CONST_FUNC:.    
21b2a 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  case TK_FUNCTION
21b2b 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69  : {.      ExprLi
21b2c 73 74 20 2a 70 46 61 72 67 3b 20 20 20 20 20 20  st *pFarg;      
21b2d 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 75 6e 63   /* List of func
21b2e 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a  tion arguments *
21b2f 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 61 72  /.      int nFar
21b30 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  g;             /
21b31 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6e 63  * Number of func
21b32 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a  tion arguments *
21b33 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20  /.      FuncDef 
21b34 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 2f  *pDef;         /
21b35 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64  * The function d
21b36 65 66 69 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74  efinition object
21b37 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49   */.      int nI
21b38 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
21b39 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68   /* Length of th
21b3a 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  e function name 
21b3b 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20  in bytes */.    
21b3c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
21b3d 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  d;       /* The 
21b3e 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
21b3f 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74  .      int const
21b40 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a  Mask = 0;     /*
21b41 20 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f   Mask of functio
21b42 6e 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  n arguments that
21b43 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f   are constant */
21b44 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20  .      int i;   
21b45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21b46 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
21b47 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20  .      u8 enc = 
21b48 45 4e 43 28 64 62 29 3b 20 20 20 20 20 20 2f 2a  ENC(db);      /*
21b49 20 54 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69   The text encodi
21b4a 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20  ng used by this 
21b4b 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20  database */.    
21b4c 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
21b4d 20 3d 20 30 3b 20 20 20 20 2f 2a 20 41 20 63 6f   = 0;    /* A co
21b4e 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
21b4f 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72   */..      asser
21b50 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
21b51 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
21b52 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20  sSelect) );.    
21b53 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21b54 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b  TK_CONST_FUNC );
21b55 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21b56 20 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e   op==TK_FUNCTION
21b57 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 78   );.      if( Ex
21b58 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
21b59 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e  (pExpr, EP_Token
21b5a 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20  Only) ){.       
21b5b 20 70 46 61 72 67 20 3d 20 30 3b 0a 20 20 20 20   pFarg = 0;.    
21b5c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
21b5d 20 70 46 61 72 67 20 3d 20 70 45 78 70 72 2d 3e   pFarg = pExpr->
21b5e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d  x.pList;.      }
21b5f 0a 20 20 20 20 20 20 6e 46 61 72 67 20 3d 20 70  .      nFarg = p
21b60 46 61 72 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45  Farg ? pFarg->nE
21b61 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 61  xpr : 0;.      a
21b62 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
21b63 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21b64 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
21b65 20 20 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72       zId = pExpr
21b66 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ->u.zToken;.    
21b67 20 20 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53    nId = sqlite3S
21b68 74 72 6c 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20  trlen30(zId);.  
21b69 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74      pDef = sqlit
21b6a 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64  e3FindFunction(d
21b6b 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 46 61  b, zId, nId, nFa
21b6c 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20  rg, enc, 0);.   
21b6d 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29     if( pDef==0 )
21b6e 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21b6f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
21b70 2c 20 22 75 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74  , "unknown funct
21b71 69 6f 6e 3a 20 25 2e 2a 73 28 29 22 2c 20 6e 49  ion: %.*s()", nI
21b72 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20  d, zId);.       
21b73 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
21b74 0a 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70  .      /* Attemp
21b75 74 20 61 20 64 69 72 65 63 74 20 69 6d 70 6c 65  t a direct imple
21b76 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
21b77 20 62 75 69 6c 74 2d 69 6e 20 43 4f 41 4c 45 53   built-in COALES
21b78 43 45 28 29 20 61 6e 64 0a 20 20 20 20 20 20 2a  CE() and.      *
21b79 2a 20 49 46 4e 55 4c 4c 28 29 20 66 75 6e 63 74  * IFNULL() funct
21b7a 69 6f 6e 73 2e 20 20 54 68 69 73 20 61 76 6f 69  ions.  This avoi
21b7b 64 73 20 75 6e 6e 65 63 65 73 73 61 72 79 20 65  ds unnecessary e
21b7c 76 61 6c 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20  valation of.    
21b7d 20 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70    ** arguments p
21b7e 61 73 74 20 74 68 65 20 66 69 72 73 74 20 6e 6f  ast the first no
21b7f 6e 2d 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e  n-NULL argument.
21b80 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
21b81 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 20  if( pDef->flags 
21b82 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f  & SQLITE_FUNC_CO
21b83 41 4c 45 53 43 45 20 29 7b 0a 20 20 20 20 20 20  ALESCE ){.      
21b84 20 20 69 6e 74 20 65 6e 64 43 6f 61 6c 65 73 63    int endCoalesc
21b85 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  e = sqlite3VdbeM
21b86 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
21b87 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 46 61       assert( nFa
21b88 72 67 3e 3d 32 20 29 3b 0a 20 20 20 20 20 20 20  rg>=2 );.       
21b89 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21b8a 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d 3e  (pParse, pFarg->
21b8b 61 5b 30 5d 2e 70 45 78 70 72 2c 20 74 61 72 67  a[0].pExpr, targ
21b8c 65 74 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  et);.        for
21b8d 28 69 3d 31 3b 20 69 3c 6e 46 61 72 67 3b 20 69  (i=1; i<nFarg; i
21b8e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ++){.          s
21b8f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21b90 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20  (v, OP_NotNull, 
21b91 74 61 72 67 65 74 2c 20 65 6e 64 43 6f 61 6c 65  target, endCoale
21b92 73 63 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  sce);.          
21b93 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
21b94 52 65 6d 6f 76 65 28 70 50 61 72 73 65 2c 20 74  Remove(pParse, t
21b95 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20  arget);.        
21b96 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21b97 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a  hePush(pParse);.
21b98 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
21b99 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
21b9a 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45  , pFarg->a[i].pE
21b9b 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  xpr, target);.  
21b9c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21b9d 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72  xprCachePop(pPar
21b9e 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  se, 1);.        
21b9f 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
21ba0 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
21ba1 6c 28 76 2c 20 65 6e 64 43 6f 61 6c 65 73 63 65  l(v, endCoalesce
21ba2 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
21ba3 3b 0a 20 20 20 20 20 20 7d 0a 0a 0a 20 20 20 20  ;.      }...    
21ba4 20 20 69 66 28 20 70 46 61 72 67 20 29 7b 0a 20    if( pFarg ){. 
21ba5 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69         r1 = sqli
21ba6 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
21ba7 70 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b 0a  pParse, nFarg);.
21ba8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21ba9 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61  xprCachePush(pPa
21baa 72 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69 63  rse);     /* Tic
21bab 6b 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65  ket 2ea2425d34be
21bac 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
21bad 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c  te3ExprCodeExprL
21bae 69 73 74 28 70 50 61 72 73 65 2c 20 70 46 61 72  ist(pParse, pFar
21baf 67 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20  g, r1, 1);.     
21bb0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
21bb1 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
21bb2 29 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32  );   /* Ticket 2
21bb3 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20  ea2425d34be */. 
21bb4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
21bb5 20 20 20 20 72 31 20 3d 20 30 3b 0a 20 20 20 20      r1 = 0;.    
21bb6 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
21bb7 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
21bb8 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f  ABLE.      /* Po
21bb9 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20  ssibly overload 
21bba 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  the function if 
21bbb 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
21bbc 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61  nt is.      ** a
21bbd 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63   virtual table c
21bbe 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a  olumn..      **.
21bbf 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66        ** For inf
21bc0 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49  ix functions (LI
21bc1 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50  KE, GLOB, REGEXP
21bc2 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65  , and MATCH) use
21bc3 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65   the.      ** se
21bc4 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e  cond argument, n
21bc5 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73  ot the first, as
21bc6 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f   the argument to
21bc7 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a   test to.      *
21bc8 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61  * see if it is a
21bc9 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72   column in a vir
21bca 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69  tual table.  Thi
21bcb 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73  s is done becaus
21bcc 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c  e.      ** the l
21bcd 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69  eft operand of i
21bce 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28  nfix functions (
21bcf 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77  the operand we w
21bd0 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ant to.      ** 
21bd1 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64  control overload
21bd2 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20  ing) ends up as 
21bd3 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
21bd4 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20  ent to the.     
21bd5 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54   ** function.  T
21bd6 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41  he expression "A
21bd7 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69   glob B" is equi
21bd8 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20  valent to .     
21bd9 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20   ** "glob(B,A). 
21bda 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20   We want to use 
21bdb 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62  the A in "A glob
21bdc 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20   B" to test.    
21bdd 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f    ** for functio
21bde 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20  n overloading.  
21bdf 42 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42  But we use the B
21be0 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42   term in "glob(B
21be1 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ,A)"..      */. 
21be2 20 20 20 20 20 69 66 28 20 6e 46 61 72 67 3e 3d       if( nFarg>=
21be3 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61  2 && (pExpr->fla
21be4 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e  gs & EP_InfixFun
21be5 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  c) ){.        pD
21be6 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62  ef = sqlite3Vtab
21be7 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e  OverloadFunction
21be8 28 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67  (db, pDef, nFarg
21be9 2c 20 70 46 61 72 67 2d 3e 61 5b 31 5d 2e 70 45  , pFarg->a[1].pE
21bea 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  xpr);.      }els
21beb 65 20 69 66 28 20 6e 46 61 72 67 3e 30 20 29 7b  e if( nFarg>0 ){
21bec 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20  .        pDef = 
21bed 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c  sqlite3VtabOverl
21bee 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  oadFunction(db, 
21bef 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61  pDef, nFarg, pFa
21bf0 72 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b  rg->a[0].pExpr);
21bf1 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
21bf2 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
21bf3 3c 6e 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20 20  <nFarg; i++){.  
21bf4 20 20 20 20 20 20 69 66 28 20 69 3c 33 32 20 26        if( i<32 &
21bf5 26 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43  & sqlite3ExprIsC
21bf6 6f 6e 73 74 61 6e 74 28 70 46 61 72 67 2d 3e 61  onstant(pFarg->a
21bf7 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20  [i].pExpr) ){.  
21bf8 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73          constMas
21bf9 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20  k |= (1<<i);.   
21bfa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
21bfb 66 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20  f( (pDef->flags 
21bfc 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  & SQLITE_FUNC_NE
21bfd 45 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70  EDCOLL)!=0 && !p
21bfe 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
21bff 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
21c00 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
21c01 72 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d  rse, pFarg->a[i]
21c02 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20  .pExpr);.       
21c03 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
21c04 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73   if( pDef->flags
21c05 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
21c06 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20  EEDCOLL ){.     
21c07 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20     if( !pColl ) 
21c08 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
21c09 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20  tColl; .        
21c0a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21c0b 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c  4(v, OP_CollSeq,
21c0c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20   0, 0, 0, (char 
21c0d 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c  *)pColl, P4_COLL
21c0e 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  SEQ);.      }.  
21c0f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21c10 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63  ddOp4(v, OP_Func
21c11 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c  tion, constMask,
21c12 20 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 20   r1, target,.   
21c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c14 20 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 66       (char*)pDef
21c15 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20  , P4_FUNCDEF);. 
21c16 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21c17 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29  ChangeP5(v, (u8)
21c18 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 69 66  nFarg);.      if
21c19 28 20 6e 46 61 72 67 20 29 7b 0a 20 20 20 20 20  ( nFarg ){.     
21c1a 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
21c1b 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
21c1c 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20  e, r1, nFarg);. 
21c1d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
21c1e 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
21c1f 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
21c20 73 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a  se, r1, nFarg);.
21c21 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21c22 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
21c23 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
21c24 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53      case TK_EXIS
21c25 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  TS:.    case TK_
21c26 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20  SELECT: {.      
21c27 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21c28 5f 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20  _EXISTS );.     
21c29 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21c2a 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20  K_SELECT );.    
21c2b 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65    inReg = sqlite
21c2c 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70  3CodeSubselect(p
21c2d 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30 2c  Parse, pExpr, 0,
21c2e 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   0);.      break
21c2f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21c30 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20   TK_IN: {.      
21c31 69 6e 74 20 64 65 73 74 49 66 46 61 6c 73 65 20  int destIfFalse 
21c32 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
21c33 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
21c34 20 69 6e 74 20 64 65 73 74 49 66 4e 75 6c 6c 20   int destIfNull 
21c35 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
21c36 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
21c37 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21c38 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
21c39 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21c3a 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21c3b 49 4e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  IN(pParse, pExpr
21c3c 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 64  , destIfFalse, d
21c3d 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  estIfNull);.    
21c3e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21c3f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
21c40 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20  r, 1, target);. 
21c41 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21c42 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
21c43 64 65 73 74 49 66 46 61 6c 73 65 29 3b 0a 20 20  destIfFalse);.  
21c44 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21c45 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
21c46 6d 6d 2c 20 74 61 72 67 65 74 2c 20 30 29 3b 0a  mm, target, 0);.
21c47 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21c48 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
21c49 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20   destIfNull);.  
21c4a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21c4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
21c4c 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20  E_OMIT_SUBQUERY 
21c4d 2a 2f 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20  */...    /*.    
21c4e 2a 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20  **    x BETWEEN 
21c4f 79 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20  y AND z.    **. 
21c50 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71     ** This is eq
21c51 75 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20  uivalent to.    
21c52 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d  **.    **    x>=
21c53 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a  y AND x<=z.    *
21c54 2a 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74  *.    ** X is st
21c55 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70  ored in pExpr->p
21c56 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69  Left..    ** Y i
21c57 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70  s stored in pExp
21c58 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  r->pList->a[0].p
21c59 45 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69  Expr..    ** Z i
21c5a 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70  s stored in pExp
21c5b 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  r->pList->a[1].p
21c5c 45 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  Expr..    */.   
21c5d 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e   case TK_BETWEEN
21c5e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  : {.      Expr *
21c5f 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70  pLeft = pExpr->p
21c60 4c 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75  Left;.      stru
21c61 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
21c62 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72   *pLItem = pExpr
21c63 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20  ->x.pList->a;.  
21c64 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74      Expr *pRight
21c65 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72   = pLItem->pExpr
21c66 3b 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  ;..      codeCom
21c67 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61  pareOperands(pPa
21c68 72 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c  rse, pLeft, &r1,
21c69 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
21c6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52                pR
21c6c 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46  ight, &r2, &regF
21c6d 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73  ree2);.      tes
21c6e 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21c6f 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
21c70 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
21c71 30 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20  0 );.      r3 = 
21c72 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
21c73 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
21c74 20 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r4 = sqlite3Get
21c75 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
21c76 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
21c77 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  re(pParse, pLeft
21c78 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c  , pRight, OP_Ge,
21c79 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21c7a 20 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53     r1, r2, r3, S
21c7b 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a  QLITE_STOREP2);.
21c7c 20 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a        pLItem++;.
21c7d 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70        pRight = p
21c7e 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  LItem->pExpr;.  
21c7f 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
21c80 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
21c81 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20  , regFree2);.   
21c82 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45     r2 = sqlite3E
21c83 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
21c84 73 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67  se, pRight, &reg
21c85 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65  Free2);.      te
21c86 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
21c87 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64  ==0 );.      cod
21c88 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c  eCompare(pParse,
21c89 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20   pLeft, pRight, 
21c8a 4f 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72  OP_Le, r1, r2, r
21c8b 34 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50  4, SQLITE_STOREP
21c8c 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
21c8d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
21c8e 50 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74  P_And, r3, r4, t
21c8f 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71  arget);.      sq
21c90 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
21c91 52 65 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b  Reg(pParse, r3);
21c92 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
21c93 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
21c94 72 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20  rse, r4);.      
21c95 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21c96 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20   case TK_UPLUS: 
21c97 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20  {.      inReg = 
21c98 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
21c99 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45  arget(pParse, pE
21c9a 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67  xpr->pLeft, targ
21c9b 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  et);.      break
21c9c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73  ;.    }..    cas
21c9d 65 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 7b 0a  e TK_TRIGGER: {.
21c9e 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
21c9f 6f 70 63 6f 64 65 20 69 73 20 54 4b 5f 54 52 49  opcode is TK_TRI
21ca0 47 47 45 52 2c 20 74 68 65 6e 20 74 68 65 20 65  GGER, then the e
21ca1 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 72  xpression is a r
21ca2 65 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20 2a  eference.      *
21ca3 2a 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  * to a column in
21ca4 20 74 68 65 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c   the new.* or ol
21ca5 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  d.* pseudo-table
21ca6 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 0a 20  s available to. 
21ca7 20 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20       ** trigger 
21ca8 70 72 6f 67 72 61 6d 73 2e 20 49 6e 20 74 68 69  programs. In thi
21ca9 73 20 63 61 73 65 20 45 78 70 72 2e 69 54 61 62  s case Expr.iTab
21caa 6c 65 20 69 73 20 73 65 74 20 74 6f 20 31 20 66  le is set to 1 f
21cab 6f 72 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  or the.      ** 
21cac 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62  new.* pseudo-tab
21cad 6c 65 2c 20 6f 72 20 30 20 66 6f 72 20 74 68 65  le, or 0 for the
21cae 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61   old.* pseudo-ta
21caf 62 6c 65 2e 20 45 78 70 72 2e 69 43 6f 6c 75 6d  ble. Expr.iColum
21cb0 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65  n.      ** is se
21cb1 74 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20  t to the column 
21cb2 6f 66 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61  of the pseudo-ta
21cb3 62 6c 65 20 74 6f 20 72 65 61 64 2c 20 6f 72 20  ble to read, or 
21cb4 74 6f 20 2d 31 20 74 6f 0a 20 20 20 20 20 20 2a  to -1 to.      *
21cb5 2a 20 72 65 61 64 20 74 68 65 20 72 6f 77 69 64  * read the rowid
21cb6 20 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2a   field..      **
21cb7 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65 78  .      ** The ex
21cb8 70 72 65 73 73 69 6f 6e 20 69 73 20 69 6d 70 6c  pression is impl
21cb9 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 61 6e  emented using an
21cba 20 4f 50 5f 50 61 72 61 6d 20 6f 70 63 6f 64 65   OP_Param opcode
21cbb 2e 20 54 68 65 20 70 31 0a 20 20 20 20 20 20 2a  . The p1.      *
21cbc 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 73  * parameter is s
21cbd 65 74 20 74 6f 20 30 20 66 6f 72 20 61 6e 20 6f  et to 0 for an o
21cbe 6c 64 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e  ld.rowid referen
21cbf 63 65 2c 20 6f 72 20 74 6f 20 28 69 2b 31 29 0a  ce, or to (i+1).
21cc0 20 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 66 65        ** to refe
21cc1 72 65 6e 63 65 20 61 6e 6f 74 68 65 72 20 63 6f  rence another co
21cc2 6c 75 6d 6e 20 6f 66 20 74 68 65 20 6f 6c 64 2e  lumn of the old.
21cc3 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20  * pseudo-table, 
21cc4 77 68 65 72 65 20 0a 20 20 20 20 20 20 2a 2a 20  where .      ** 
21cc5 69 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f  i is the index o
21cc6 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 46 6f  f the column. Fo
21cc7 72 20 61 20 6e 65 77 2e 72 6f 77 69 64 20 72 65  r a new.rowid re
21cc8 66 65 72 65 6e 63 65 2c 20 70 31 20 69 73 0a 20  ference, p1 is. 
21cc9 20 20 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 28       ** set to (
21cca 6e 2b 31 29 2c 20 77 68 65 72 65 20 6e 20 69 73  n+1), where n is
21ccb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
21ccc 6f 6c 75 6d 6e 73 20 69 6e 20 65 61 63 68 20 70  olumns in each p
21ccd 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 20 20 20  seudo-table..   
21cce 20 20 20 2a 2a 20 46 6f 72 20 61 20 72 65 66 65     ** For a refe
21ccf 72 65 6e 63 65 20 74 6f 20 61 6e 79 20 6f 74 68  rence to any oth
21cd0 65 72 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  er column in the
21cd1 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61   new.* pseudo-ta
21cd2 62 6c 65 2c 20 70 31 0a 20 20 20 20 20 20 2a 2a  ble, p1.      **
21cd3 20 69 73 20 73 65 74 20 74 6f 20 28 6e 2b 32 2b   is set to (n+2+
21cd4 69 29 2c 20 77 68 65 72 65 20 6e 20 61 6e 64 20  i), where n and 
21cd5 69 20 61 72 65 20 61 73 20 64 65 66 69 6e 65 64  i are as defined
21cd6 20 70 72 65 76 69 6f 75 73 6c 79 2e 20 46 6f 72   previously. For
21cd7 0a 20 20 20 20 20 20 2a 2a 20 65 78 61 6d 70 6c  .      ** exampl
21cd8 65 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  e, if the table 
21cd9 6f 6e 20 77 68 69 63 68 20 74 72 69 67 67 65 72  on which trigger
21cda 73 20 61 72 65 20 62 65 69 6e 67 20 66 69 72 65  s are being fire
21cdb 64 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65  d is.      ** de
21cdc 63 6c 61 72 65 64 20 61 73 3a 0a 20 20 20 20 20  clared as:.     
21cdd 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 43   **.      **   C
21cde 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
21cdf 2c 20 62 29 3b 0a 20 20 20 20 20 20 2a 2a 0a 20  , b);.      **. 
21ce0 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 70 31 20       ** Then p1 
21ce1 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  is interpreted a
21ce2 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 20  s follows:.     
21ce3 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 70   **.      **   p
21ce4 31 3d 3d 30 20 20 20 2d 3e 20 20 20 20 6f 6c 64  1==0   ->    old
21ce5 2e 72 6f 77 69 64 20 20 20 20 20 70 31 3d 3d 33  .rowid     p1==3
21ce6 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e 72 6f 77     ->    new.row
21ce7 69 64 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31  id.      **   p1
21ce8 3d 3d 31 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e  ==1   ->    old.
21ce9 61 20 20 20 20 20 20 20 20 20 70 31 3d 3d 34 20  a         p1==4 
21cea 20 20 2d 3e 20 20 20 20 6e 65 77 2e 61 0a 20 20    ->    new.a.  
21ceb 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d 32 20 20      **   p1==2  
21cec 20 2d 3e 20 20 20 20 6f 6c 64 2e 62 20 20 20 20   ->    old.b    
21ced 20 20 20 20 20 70 31 3d 3d 35 20 20 20 2d 3e 20       p1==5   -> 
21cee 20 20 20 6e 65 77 2e 62 20 20 20 20 20 20 20 0a     new.b       .
21cef 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54        */.      T
21cf0 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 45 78  able *pTab = pEx
21cf1 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20  pr->pTab;.      
21cf2 69 6e 74 20 70 31 20 3d 20 70 45 78 70 72 2d 3e  int p1 = pExpr->
21cf3 69 54 61 62 6c 65 20 2a 20 28 70 54 61 62 2d 3e  iTable * (pTab->
21cf4 6e 43 6f 6c 2b 31 29 20 2b 20 31 20 2b 20 70 45  nCol+1) + 1 + pE
21cf5 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 0a 20  xpr->iColumn;.. 
21cf6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
21cf7 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 7c 7c  pr->iTable==0 ||
21cf8 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d   pExpr->iTable==
21cf9 31 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  1 );.      asser
21cfa 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  t( pExpr->iColum
21cfb 6e 3e 3d 2d 31 20 26 26 20 70 45 78 70 72 2d 3e  n>=-1 && pExpr->
21cfc 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43  iColumn<pTab->nC
21cfd 6f 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ol );.      asse
21cfe 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c  rt( pTab->iPKey<
21cff 30 20 7c 7c 20 70 45 78 70 72 2d 3e 69 43 6f 6c  0 || pExpr->iCol
21d00 75 6d 6e 21 3d 70 54 61 62 2d 3e 69 50 4b 65 79  umn!=pTab->iPKey
21d01 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21d02 28 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 28 70  ( p1>=0 && p1<(p
21d03 54 61 62 2d 3e 6e 43 6f 6c 2a 32 2b 32 29 20 29  Tab->nCol*2+2) )
21d04 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
21d05 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21d06 5f 50 61 72 61 6d 2c 20 70 31 2c 20 74 61 72 67  _Param, p1, targ
21d07 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  et);.      VdbeC
21d08 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25  omment((v, "%s.%
21d09 73 20 2d 3e 20 24 25 64 22 2c 0a 20 20 20 20 20  s -> $%d",.     
21d0a 20 20 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c     (pExpr->iTabl
21d0b 65 20 3f 20 22 6e 65 77 22 20 3a 20 22 6f 6c 64  e ? "new" : "old
21d0c 22 29 2c 0a 20 20 20 20 20 20 20 20 28 70 45 78  "),.        (pEx
21d0d 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 3f 20  pr->iColumn<0 ? 
21d0e 22 72 6f 77 69 64 22 20 3a 20 70 45 78 70 72 2d  "rowid" : pExpr-
21d0f 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70  >pTab->aCol[pExp
21d10 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d  r->iColumn].zNam
21d11 65 29 2c 0a 20 20 20 20 20 20 20 20 74 61 72 67  e),.        targ
21d12 65 74 0a 20 20 20 20 20 20 29 29 3b 0a 0a 20 20  et.      ));..  
21d13 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f      /* If the co
21d14 6c 75 6d 6e 20 68 61 73 20 52 45 41 4c 20 61 66  lumn has REAL af
21d15 66 69 6e 69 74 79 2c 20 69 74 20 6d 61 79 20 63  finity, it may c
21d16 75 72 72 65 6e 74 6c 79 20 62 65 20 73 74 6f 72  urrently be stor
21d17 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 2a  ed as an.      *
21d18 2a 20 69 6e 74 65 67 65 72 2e 20 55 73 65 20 4f  * integer. Use O
21d19 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 74  P_RealAffinity t
21d1a 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69  o make sure it i
21d1b 73 20 72 65 61 6c 6c 79 20 72 65 61 6c 2e 20 20  s really real.  
21d1c 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 45 78  */.      if( pEx
21d1d 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 0a  pr->iColumn>=0 .
21d1e 20 20 20 20 20 20 20 26 26 20 70 54 61 62 2d 3e         && pTab->
21d1f 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c  aCol[pExpr->iCol
21d20 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 53  umn].affinity==S
21d21 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20  QLITE_AFF_REAL. 
21d22 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
21d23 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21d24 31 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69  1(v, OP_RealAffi
21d25 6e 69 74 79 2c 20 74 61 72 67 65 74 29 3b 0a 20  nity, target);. 
21d26 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
21d27 61 6b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20  ak;.    }...    
21d28 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41  /*.    ** Form A
21d29 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20  :.    **   CASE 
21d2a 78 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72  x WHEN e1 THEN r
21d2b 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72  1 WHEN e2 THEN r
21d2c 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48  2 ... WHEN eN TH
21d2d 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44  EN rN ELSE y END
21d2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
21d2f 6f 72 6d 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20  orm B:.    **   
21d30 43 41 53 45 20 57 48 45 4e 20 65 31 20 54 48 45  CASE WHEN e1 THE
21d31 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45  N r1 WHEN e2 THE
21d32 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e  N r2 ... WHEN eN
21d33 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20   THEN rN ELSE y 
21d34 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  END.    **.    *
21d35 2a 20 46 6f 72 6d 20 41 20 69 73 20 63 61 6e 20  * Form A is can 
21d36 62 65 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69  be transformed i
21d37 6e 74 6f 20 74 68 65 20 65 71 75 69 76 61 6c 65  nto the equivale
21d38 6e 74 20 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c  nt form B as fol
21d39 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43  lows:.    **   C
21d3a 41 53 45 20 57 48 45 4e 20 78 3d 65 31 20 54 48  ASE WHEN x=e1 TH
21d3b 45 4e 20 72 31 20 57 48 45 4e 20 78 3d 65 32 20  EN r1 WHEN x=e2 
21d3c 54 48 45 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20  THEN r2 ....    
21d3d 2a 2a 20 20 20 20 20 20 20 20 57 48 45 4e 20 78  **        WHEN x
21d3e 3d 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45  =eN THEN rN ELSE
21d3f 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20   y END.    **.  
21d40 20 20 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78    ** X (if it ex
21d41 69 73 74 73 29 20 69 73 20 69 6e 20 70 45 78 70  ists) is in pExp
21d42 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a  r->pLeft..    **
21d43 20 59 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e   Y is in pExpr->
21d44 70 52 69 67 68 74 2e 20 20 54 68 65 20 59 20 69  pRight.  The Y i
21d45 73 20 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e  s also optional.
21d46 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
21d47 0a 20 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61  .    ** ELSE cla
21d48 75 73 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72  use and no other
21d49 20 74 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74   term matches, t
21d4a 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f  hen the result o
21d4b 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70  f the.    ** exp
21d4c 72 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a  rssion is NULL..
21d4d 20 20 20 20 2a 2a 20 45 69 20 69 73 20 69 6e 20      ** Ei is in 
21d4e 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b  pExpr->pList->a[
21d4f 69 2a 32 5d 20 61 6e 64 20 52 69 20 69 73 20 70  i*2] and Ri is p
21d50 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69  Expr->pList->a[i
21d51 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20  *2+1]..    **.  
21d52 20 20 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20    ** The result 
21d53 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
21d54 6e 20 69 73 20 74 68 65 20 52 69 20 66 6f 72 20  n is the Ri for 
21d55 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69  the first matchi
21d56 6e 67 20 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72  ng Ei,.    ** or
21d57 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
21d58 6d 61 74 63 68 69 6e 67 20 45 69 2c 20 74 68 65  matching Ei, the
21d59 20 45 4c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72   ELSE term Y, or
21d5a 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 20   if there is.   
21d5b 20 2a 2a 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d   ** no ELSE term
21d5c 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20  , NULL..    */. 
21d5d 20 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73 65     default: asse
21d5e 72 74 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 45 20  rt( op==TK_CASE 
21d5f 29 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 65  ); {.      int e
21d60 6e 64 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20  ndLabel;        
21d61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21d62 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 65  GOTO label for e
21d63 6e 64 20 6f 66 20 43 41 53 45 20 73 74 6d 74 20  nd of CASE stmt 
21d64 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78  */.      int nex
21d65 74 43 61 73 65 3b 20 20 20 20 20 20 20 20 20 20  tCase;          
21d66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f             /* GO
21d67 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65 78  TO label for nex
21d68 74 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f  t WHEN clause */
21d69 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72  .      int nExpr
21d6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
21d6b 20 20 20 20 20 20 20 20 20 2f 2a 20 32 78 20 6e           /* 2x n
21d6c 75 6d 62 65 72 20 6f 66 20 57 48 45 4e 20 74 65  umber of WHEN te
21d6d 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  rms */.      int
21d6e 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
21d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
21d70 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
21d71 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74  /.      ExprList
21d72 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20   *pEList;       
21d73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73            /* Lis
21d74 74 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20  t of WHEN terms 
21d75 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  */.      struct 
21d76 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61  ExprList_item *a
21d77 4c 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72  Listelem;  /* Ar
21d78 72 61 79 20 6f 66 20 57 48 45 4e 20 74 65 72 6d  ray of WHEN term
21d79 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20  s */.      Expr 
21d7a 6f 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 20  opCompare;      
21d7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21d7c 54 68 65 20 58 3d 3d 45 69 20 65 78 70 72 65 73  The X==Ei expres
21d7d 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78  sion */.      Ex
21d7e 70 72 20 63 61 63 68 65 58 3b 20 20 20 20 20 20  pr cacheX;      
21d7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d80 2f 2a 20 43 61 63 68 65 64 20 65 78 70 72 65 73  /* Cached expres
21d81 73 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20 20  sion X */.      
21d82 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20  Expr *pX;       
21d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d84 20 20 2f 2a 20 54 68 65 20 58 20 65 78 70 72 65    /* The X expre
21d85 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45  ssion */.      E
21d86 78 70 72 20 2a 70 54 65 73 74 20 3d 20 30 3b 20  xpr *pTest = 0; 
21d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d88 20 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d 20   /* X==Ei (form 
21d89 41 29 20 6f 72 20 6a 75 73 74 20 45 69 20 28 66  A) or just Ei (f
21d8a 6f 72 6d 20 42 29 20 2a 2f 0a 20 20 20 20 20 20  orm B) */.      
21d8b 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 69 43  VVA_ONLY( int iC
21d8c 61 63 68 65 4c 65 76 65 6c 20 3d 20 70 50 61 72  acheLevel = pPar
21d8d 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b  se->iCacheLevel;
21d8e 20 29 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74   )..      assert
21d8f 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21d90 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
21d91 53 65 6c 65 63 74 29 20 26 26 20 70 45 78 70 72  Select) && pExpr
21d92 2d 3e 78 2e 70 4c 69 73 74 20 29 3b 0a 20 20 20  ->x.pList );.   
21d93 20 20 20 61 73 73 65 72 74 28 28 70 45 78 70 72     assert((pExpr
21d94 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ->x.pList->nExpr
21d95 20 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20   % 2) == 0);.   
21d96 20 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d     assert(pExpr-
21d97 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  >x.pList->nExpr 
21d98 3e 20 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69  > 0);.      pELi
21d99 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  st = pExpr->x.pL
21d9a 69 73 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74  ist;.      aList
21d9b 65 6c 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61  elem = pEList->a
21d9c 3b 0a 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20  ;.      nExpr = 
21d9d 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
21d9e 20 20 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20       endLabel = 
21d9f 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
21da0 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69  abel(v);.      i
21da1 66 28 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e  f( (pX = pExpr->
21da2 70 4c 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20  pLeft)!=0 ){.   
21da3 20 20 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70       cacheX = *p
21da4 58 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  X;.        testc
21da5 61 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f  ase( pX->op==TK_
21da6 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
21da7 20 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e    testcase( pX->
21da8 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20  op==TK_REGISTER 
21da9 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65  );.        cache
21daa 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74  X.iTable = sqlit
21dab 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
21dac 50 61 72 73 65 2c 20 70 58 2c 20 26 72 65 67 46  Parse, pX, &regF
21dad 72 65 65 31 29 3b 0a 20 20 20 20 20 20 20 20 74  ree1);.        t
21dae 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
21daf 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  1==0 );.        
21db0 63 61 63 68 65 58 2e 6f 70 20 3d 20 54 4b 5f 52  cacheX.op = TK_R
21db1 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20  EGISTER;.       
21db2 20 6f 70 43 6f 6d 70 61 72 65 2e 6f 70 20 3d 20   opCompare.op = 
21db3 54 4b 5f 45 51 3b 0a 20 20 20 20 20 20 20 20 6f  TK_EQ;.        o
21db4 70 43 6f 6d 70 61 72 65 2e 70 4c 65 66 74 20 3d  pCompare.pLeft =
21db5 20 26 63 61 63 68 65 58 3b 0a 20 20 20 20 20 20   &cacheX;.      
21db6 20 20 70 54 65 73 74 20 3d 20 26 6f 70 43 6f 6d    pTest = &opCom
21db7 70 61 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pare;.      }.  
21db8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
21db9 45 78 70 72 3b 20 69 3d 69 2b 32 29 7b 0a 20 20  Expr; i=i+2){.  
21dba 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21dbb 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73  rCachePush(pPars
21dbc 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
21dbd 70 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  pX ){.          
21dbe 61 73 73 65 72 74 28 20 70 54 65 73 74 21 3d 30  assert( pTest!=0
21dbf 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 70   );.          op
21dc0 43 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20 3d  Compare.pRight =
21dc1 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45   aListelem[i].pE
21dc2 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  xpr;.        }el
21dc3 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54  se{.          pT
21dc4 65 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d 5b  est = aListelem[
21dc5 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
21dc6 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78 74    }.        next
21dc7 43 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56 64  Case = sqlite3Vd
21dc8 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
21dc9 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
21dca 28 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pTest->op==TK_
21dcb 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
21dcc 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
21dcd 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65  alse(pParse, pTe
21dce 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 51  st, nextCase, SQ
21dcf 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
21dd0 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
21dd1 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b  se( aListelem[i+
21dd2 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  1].pExpr->op==TK
21dd3 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20  _COLUMN );.     
21dd4 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c 69     testcase( aLi
21dd5 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70  stelem[i+1].pExp
21dd6 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54  r->op==TK_REGIST
21dd7 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ER );.        sq
21dd8 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
21dd9 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d 5b  arse, aListelem[
21dda 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72 67  i+1].pExpr, targ
21ddb 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  et);.        sql
21ddc 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21ddd 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e  , OP_Goto, 0, en
21dde 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 20  dLabel);.       
21ddf 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21de0 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b  ePop(pParse, 1);
21de1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21de2 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
21de3 28 76 2c 20 6e 65 78 74 43 61 73 65 29 3b 0a 20  (v, nextCase);. 
21de4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
21de5 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 29   pExpr->pRight )
21de6 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21de7 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70  3ExprCachePush(p
21de8 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
21de9 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
21dea 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21deb 52 69 67 68 74 2c 20 74 61 72 67 65 74 29 3b 0a  Right, target);.
21dec 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21ded 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72  xprCachePop(pPar
21dee 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65  se, 1);.      }e
21def 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
21df0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21df1 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61  , OP_Null, 0, ta
21df2 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rget);.      }. 
21df3 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d       assert( db-
21df4 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
21df5 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20   pParse->nErr>0 
21df6 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70  .           || p
21df7 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76  Parse->iCacheLev
21df8 65 6c 3d 3d 69 43 61 63 68 65 4c 65 76 65 6c 20  el==iCacheLevel 
21df9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21dfa 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
21dfb 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20  (v, endLabel);. 
21dfc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21dfd 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
21dfe 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
21dff 20 20 63 61 73 65 20 54 4b 5f 52 41 49 53 45 3a    case TK_RAISE:
21e00 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
21e01 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
21e02 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 0a 20  ==OE_Rollback . 
21e03 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78            || pEx
21e04 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45  pr->affinity==OE
21e05 5f 41 62 6f 72 74 0a 20 20 20 20 20 20 20 20 20  _Abort.         
21e06 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69    || pExpr->affi
21e07 6e 69 74 79 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20  nity==OE_Fail.  
21e08 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70           || pExp
21e09 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f  r->affinity==OE_
21e0a 49 67 6e 6f 72 65 0a 20 20 20 20 20 20 29 3b 0a  Ignore.      );.
21e0b 20 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73        if( !pPars
21e0c 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29  e->pTriggerTab )
21e0d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21e0e 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
21e0f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
21e10 20 20 20 20 20 20 20 20 20 22 52 41 49 53 45 28           "RAISE(
21e11 29 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73  ) may only be us
21e12 65 64 20 77 69 74 68 69 6e 20 61 20 74 72 69 67  ed within a trig
21e13 67 65 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20  ger-program");. 
21e14 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
21e15 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
21e16 66 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69  f( pExpr->affini
21e17 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a  ty==OE_Abort ){.
21e18 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
21e19 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b  ayAbort(pParse);
21e1a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
21e1b 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
21e1c 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21e1d 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
21e1e 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
21e1f 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e  affinity==OE_Ign
21e20 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ore ){.        s
21e21 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
21e22 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 2c  (.            v,
21e23 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
21e24 5f 4f 4b 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20  _OK, OE_Ignore, 
21e25 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  0, pExpr->u.zTok
21e26 65 6e 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c  en,0);.      }el
21e27 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
21e28 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
21e29 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  t(pParse, pExpr-
21e2a 3e 61 66 66 69 6e 69 74 79 2c 20 70 45 78 70 72  >affinity, pExpr
21e2b 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a  ->u.zToken, 0);.
21e2c 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 62        }..      b
21e2d 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
21e2e 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  if.  }.  sqlite3
21e2f 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
21e30 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29  Parse, regFree1)
21e31 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
21e32 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
21e33 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72  , regFree2);.  r
21e34 65 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a  eturn inReg;.}..
21e35 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
21e36 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20  ode to evaluate 
21e37 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  an expression an
21e38 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  d store the resu
21e39 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65  lts.** into a re
21e3a 67 69 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20  gister.  Return 
21e3b 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d  the register num
21e3c 62 65 72 20 77 68 65 72 65 20 74 68 65 20 72 65  ber where the re
21e3d 73 75 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f  sults.** are sto
21e3e 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  red..**.** If th
21e3f 65 20 72 65 67 69 73 74 65 72 20 69 73 20 61 20  e register is a 
21e40 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
21e41 65 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 64  er that can be d
21e42 65 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74  eallocated,.** t
21e43 68 65 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75  hen write its nu
21e44 6d 62 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e  mber into *pReg.
21e45 20 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20    If the result 
21e46 72 65 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a  register is not.
21e47 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20  ** a temporary, 
21e48 74 68 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74  then set *pReg t
21e49 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54  o zero..*/.SQLIT
21e4a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
21e4b 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
21e4c 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  p(Parse *pParse,
21e4d 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
21e4e 74 20 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20  t *pReg){.  int 
21e4f 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
21e50 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
21e51 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74    int r2 = sqlit
21e52 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
21e53 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
21e54 72 31 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72  r1);.  if( r2==r
21e55 31 20 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d  1 ){.    *pReg =
21e56 20 72 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20   r1;.  }else{.  
21e57 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
21e58 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
21e59 72 31 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d  r1);.    *pReg =
21e5a 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
21e5b 20 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65   r2;.}../*.** Ge
21e5c 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
21e5d 20 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65   will evaluate e
21e5e 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
21e5f 61 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a  and store the.**
21e60 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69   results in regi
21e61 73 74 65 72 20 74 61 72 67 65 74 2e 20 20 54 68  ster target.  Th
21e62 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 67 75  e results are gu
21e63 61 72 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65  aranteed to appe
21e64 61 72 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65  ar.** in registe
21e65 72 20 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c  r target..*/.SQL
21e66 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
21e67 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
21e68 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
21e69 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
21e6a 74 61 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69  target){.  int i
21e6b 6e 52 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28  nReg;..  assert(
21e6c 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72   target>0 && tar
21e6d 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65  get<=pParse->nMe
21e6e 6d 20 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73  m );.  inReg = s
21e6f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
21e70 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
21e71 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61  pr, target);.  a
21e72 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
21e73 56 64 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e  Vdbe || pParse->
21e74 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
21e75 20 29 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21   );.  if( inReg!
21e76 3d 74 61 72 67 65 74 20 26 26 20 70 50 61 72 73  =target && pPars
21e77 65 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20  e->pVdbe ){.    
21e78 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21e79 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  2(pParse->pVdbe,
21e7a 20 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67   OP_SCopy, inReg
21e7b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20  , target);.  }. 
21e7c 20 72 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a   return target;.
21e7d 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
21e7e 65 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c  e code that eval
21e7f 75 74 65 73 20 74 68 65 20 67 69 76 65 6e 20 65  utes the given e
21e80 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75  xpression and pu
21e81 74 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  ts the result.**
21e82 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72   in register tar
21e83 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20  get..**.** Also 
21e84 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
21e85 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65  he expression re
21e86 73 75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68  sults into anoth
21e87 65 72 20 22 63 61 63 68 65 22 20 72 65 67 69 73  er "cache" regis
21e88 74 65 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66  ter.** and modif
21e89 79 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  y the expression
21e8a 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78   so that the nex
21e8b 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 76 61  t time it is eva
21e8c 6c 75 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72  luated,.** the r
21e8d 65 73 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20  esult is a copy 
21e8e 6f 66 20 74 68 65 20 63 61 63 68 65 20 72 65 67  of the cache reg
21e8f 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ister..**.** Thi
21e90 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
21e91 64 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e  d for expression
21e92 73 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20  s that are used 
21e93 6d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d  multiple .** tim
21e94 65 73 2e 20 20 54 68 65 79 20 61 72 65 20 65 76  es.  They are ev
21e95 61 6c 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64  aluated once and
21e96 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   the results of 
21e97 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  the expression.*
21e98 2a 20 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f  * are reused..*/
21e99 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21e9a 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
21e9b 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73  odeAndCache(Pars
21e9c 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
21e9d 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67  *pExpr, int targ
21e9e 65 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  et){.  Vdbe *v =
21e9f 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
21ea0 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69    int inReg;.  i
21ea1 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  nReg = sqlite3Ex
21ea2 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
21ea3 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20  Expr, target);. 
21ea4 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e   assert( target>
21ea5 30 20 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 72  0 );.  /* This r
21ea6 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
21ea7 20 66 6f 72 20 74 65 72 6d 73 20 74 6f 20 49 4e   for terms to IN
21ea8 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 2e 20  SERT or UPDATE. 
21ea9 20 41 6e 64 20 74 68 65 20 6f 6e 6c 79 0a 20 20   And the only.  
21eaa 2a 2a 20 6f 74 68 65 72 20 70 6c 61 63 65 20 77  ** other place w
21eab 68 65 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73  here expressions
21eac 20 63 61 6e 20 62 65 20 63 6f 6e 76 65 72 74 65   can be converte
21ead 64 20 69 6e 74 6f 20 54 4b 5f 52 45 47 49 53 54  d into TK_REGIST
21eae 45 52 20 69 73 0a 20 20 2a 2a 20 69 6e 20 57 48  ER is.  ** in WH
21eaf 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65  ERE clause proce
21eb0 73 73 69 6e 67 2e 20 20 53 6f 20 61 73 20 63 75  ssing.  So as cu
21eb1 72 72 65 6e 74 6c 79 20 69 6d 70 6c 65 6d 65 6e  rrently implemen
21eb2 74 65 64 2c 20 74 68 65 72 65 20 69 73 0a 20 20  ted, there is.  
21eb3 2a 2a 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20  ** no way for a 
21eb4 54 4b 5f 52 45 47 49 53 54 45 52 20 74 6f 20 65  TK_REGISTER to e
21eb5 78 69 73 74 20 68 65 72 65 2e 20 20 42 75 74 20  xist here.  But 
21eb6 69 74 20 73 65 65 6d 73 20 70 72 75 64 65 6e 74  it seems prudent
21eb7 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 20 74 68   to.  ** keep th
21eb8 65 20 41 4c 57 41 59 53 28 29 20 69 6e 20 63 61  e ALWAYS() in ca
21eb9 73 65 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  se the condition
21eba 73 20 61 62 6f 76 65 20 63 68 61 6e 67 65 20 77  s above change w
21ebb 69 74 68 20 66 75 74 75 72 65 0a 20 20 2a 2a 20  ith future.  ** 
21ebc 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 6f 72  modifications or
21ebd 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 2e 20 2a   enhancements. *
21ebe 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  /.  if( ALWAYS(p
21ebf 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 52 45 47  Expr->op!=TK_REG
21ec0 49 53 54 45 52 29 20 29 7b 20 20 0a 20 20 20 20  ISTER) ){  .    
21ec1 69 6e 74 20 69 4d 65 6d 3b 0a 20 20 20 20 69 4d  int iMem;.    iM
21ec2 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
21ec3 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
21ec4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21ec5 5f 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 69 4d  _Copy, inReg, iM
21ec6 65 6d 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e  em);.    pExpr->
21ec7 69 54 61 62 6c 65 20 3d 20 69 4d 65 6d 3b 0a 20  iTable = iMem;. 
21ec8 20 20 20 70 45 78 70 72 2d 3e 6f 70 32 20 3d 20     pExpr->op2 = 
21ec9 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 70  pExpr->op;.    p
21eca 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45  Expr->op = TK_RE
21ecb 47 49 53 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65  GISTER;.  }.  re
21ecc 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f  turn inReg;.}../
21ecd 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
21ece 20 69 66 20 70 45 78 70 72 20 69 73 20 61 6e 20   if pExpr is an 
21ecf 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73  constant express
21ed0 69 6f 6e 20 74 68 61 74 20 69 73 20 61 70 70 72  ion that is appr
21ed1 6f 70 72 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66  opriate.** for f
21ed2 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20  actoring out of 
21ed3 61 20 6c 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72  a loop.  Appropr
21ed4 69 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73  iate expressions
21ed5 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a   are:.**.**    *
21ed6 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e    Any expression
21ed7 20 74 68 61 74 20 65 76 61 6c 75 61 74 65 73 20   that evaluates 
21ed8 74 6f 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f  to two or more o
21ed9 70 63 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  pcodes..**.**   
21eda 20 2a 20 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67   *  Any OP_Integ
21edb 65 72 2c 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f  er, OP_Real, OP_
21edc 53 74 72 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c  String, OP_Blob,
21edd 20 4f 50 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20   OP_Null, .**   
21ede 20 20 20 20 6f 72 20 4f 50 5f 56 61 72 69 61 62      or OP_Variab
21edf 6c 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  le that does not
21ee0 20 6e 65 65 64 20 74 6f 20 62 65 20 70 6c 61 63   need to be plac
21ee1 65 64 20 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20  ed in a .**     
21ee2 20 20 73 70 65 63 69 66 69 63 20 72 65 67 69 73    specific regis
21ee3 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ter..**.** There
21ee4 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20   is no point in 
21ee5 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 73 69  factoring out si
21ee6 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e  ngle-instruction
21ee7 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70   constant.** exp
21ee8 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 6e 65  ressions that ne
21ee9 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20  ed to be placed 
21eea 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  in a particular 
21eeb 72 65 67 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57  register.  .** W
21eec 65 20 63 6f 75 6c 64 20 66 61 63 74 6f 72 20 74  e could factor t
21eed 68 65 6d 20 6f 75 74 2c 20 62 75 74 20 74 68 65  hem out, but the
21eee 6e 20 77 65 20 77 6f 75 6c 64 20 65 6e 64 20 75  n we would end u
21eef 70 20 61 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f  p adding an.** O
21ef0 50 5f 53 43 6f 70 79 20 69 6e 73 74 72 75 63 74  P_SCopy instruct
21ef1 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 74 68 65 20  ion to move the 
21ef2 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 63  value into the c
21ef3 6f 72 72 65 63 74 20 72 65 67 69 73 74 65 72 0a  orrect register.
21ef4 2a 2a 20 6c 61 74 65 72 2e 20 20 57 65 20 6d 69  ** later.  We mi
21ef5 67 68 74 20 61 73 20 77 65 6c 6c 20 6a 75 73 74  ght as well just
21ef6 20 75 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61   use the origina
21ef7 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e  l instruction an
21ef8 64 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 4f  d.** avoid the O
21ef9 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74  P_SCopy..*/.stat
21efa 69 63 20 69 6e 74 20 69 73 41 70 70 72 6f 70 72  ic int isAppropr
21efb 69 61 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67  iateForFactoring
21efc 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28  (Expr *p){.  if(
21efd 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43   !sqlite3ExprIsC
21efe 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70  onstantNotJoin(p
21eff 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
21f00 30 3b 20 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73  0;  /* Only cons
21f01 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73  tant expressions
21f02 20 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65   are appropriate
21f03 20 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 2a   for factoring *
21f04 2f 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e  /.  }.  if( (p->
21f05 66 6c 61 67 73 20 26 20 45 50 5f 46 69 78 65 64  flags & EP_Fixed
21f06 44 65 73 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Dest)==0 ){.    
21f07 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e  return 1;  /* An
21f08 79 20 63 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f  y constant witho
21f09 75 74 20 61 20 66 69 78 65 64 20 64 65 73 74 69  ut a fixed desti
21f0a 6e 61 74 69 6f 6e 20 69 73 20 61 70 70 72 6f 70  nation is approp
21f0b 72 69 61 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77  riate */.  }.  w
21f0c 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  hile( p->op==TK_
21f0d 55 50 4c 55 53 20 29 20 70 20 3d 20 70 2d 3e 70  UPLUS ) p = p->p
21f0e 4c 65 66 74 3b 0a 20 20 73 77 69 74 63 68 28 20  Left;.  switch( 
21f0f 70 2d 3e 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66  p->op ){.#ifndef
21f10 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f   SQLITE_OMIT_BLO
21f11 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61  B_LITERAL.    ca
21f12 73 65 20 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64  se TK_BLOB:.#end
21f13 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56  if.    case TK_V
21f14 41 52 49 41 42 4c 45 3a 0a 20 20 20 20 63 61 73  ARIABLE:.    cas
21f15 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20  e TK_INTEGER:.  
21f16 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a    case TK_FLOAT:
21f17 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c  .    case TK_NUL
21f18 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  L:.    case TK_S
21f19 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74  TRING: {.      t
21f1a 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
21f1b 54 4b 5f 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20  TK_BLOB );.     
21f1c 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70   testcase( p->op
21f1d 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b  ==TK_VARIABLE );
21f1e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21f1f 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47   p->op==TK_INTEG
21f20 45 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ER );.      test
21f21 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
21f22 46 4c 4f 41 54 20 29 3b 0a 20 20 20 20 20 20 74  FLOAT );.      t
21f23 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
21f24 54 4b 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  TK_NULL );.     
21f25 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70   testcase( p->op
21f26 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20  ==TK_STRING );. 
21f27 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69       /* Single-i
21f28 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74  nstruction const
21f29 61 6e 74 73 20 77 69 74 68 20 61 20 66 69 78 65  ants with a fixe
21f2a 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 61 72  d destination ar
21f2b 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 74 74 65  e.      ** bette
21f2c 72 20 64 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20  r done in-line. 
21f2d 20 49 66 20 77 65 20 66 61 63 74 6f 72 20 74 68   If we factor th
21f2e 65 6d 2c 20 74 68 65 79 20 77 69 6c 6c 20 6a 75  em, they will ju
21f2f 73 74 20 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20  st end.      ** 
21f30 75 70 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e  up generating an
21f31 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76   OP_SCopy to mov
21f32 65 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 74  e the value to t
21f33 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20  he destination. 
21f34 20 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72       ** register
21f35 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  . */.      retur
21f36 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 0;.    }.    c
21f37 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b  ase TK_UMINUS: {
21f38 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c  .      if( p->pL
21f39 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41  eft->op==TK_FLOA
21f3a 54 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f  T || p->pLeft->o
21f3b 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b  p==TK_INTEGER ){
21f3c 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
21f3d 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
21f3e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21f3f 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
21f40 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21f41 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
21f42 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70  }../*.** If pExp
21f43 72 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20  r is a constant 
21f44 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
21f45 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
21f46 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20  or.** factoring 
21f47 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74  out of a loop, t
21f48 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74 68 65  hen evaluate the
21f49 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69   expression.** i
21f4a 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 20 61  nto a register a
21f4b 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65  nd convert the e
21f4c 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61  xpression into a
21f4d 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20   TK_REGISTER.** 
21f4e 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73  expression..*/.s
21f4f 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 43 6f  tatic int evalCo
21f50 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a  nstExpr(Walker *
21f51 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70  pWalker, Expr *p
21f52 45 78 70 72 29 7b 0a 20 20 50 61 72 73 65 20 2a  Expr){.  Parse *
21f53 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72  pParse = pWalker
21f54 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 77 69 74  ->pParse;.  swit
21f55 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b  ch( pExpr->op ){
21f56 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a  .    case TK_IN:
21f57 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47  .    case TK_REG
21f58 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 72  ISTER: {.      r
21f59 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
21f5a 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21f5b 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20  TK_FUNCTION:.   
21f5c 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e   case TK_AGG_FUN
21f5d 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20  CTION:.    case 
21f5e 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 20 7b  TK_CONST_FUNC: {
21f5f 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 72  .      /* The ar
21f60 67 75 6d 65 6e 74 73 20 74 6f 20 61 20 66 75 6e  guments to a fun
21f61 63 74 69 6f 6e 20 68 61 76 65 20 61 20 66 69 78  ction have a fix
21f62 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a  ed destination..
21f63 20 20 20 20 20 20 2a 2a 20 4d 61 72 6b 20 74 68        ** Mark th
21f64 65 6d 20 74 68 69 73 20 77 61 79 20 74 6f 20 61  em this way to a
21f65 76 6f 69 64 20 67 65 6e 65 72 61 74 65 64 20 75  void generated u
21f66 6e 6e 65 65 64 65 64 20 4f 50 5f 53 43 6f 70 79  nneeded OP_SCopy
21f67 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75  .      ** instru
21f68 63 74 69 6f 6e 73 2e 20 0a 20 20 20 20 20 20 2a  ctions. .      *
21f69 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74  /.      ExprList
21f6a 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d   *pList = pExpr-
21f6b 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20  >x.pList;.      
21f6c 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
21f6d 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
21f6e 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
21f6f 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74  .      if( pList
21f70 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
21f71 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  i = pList->nExpr
21f72 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  ;.        struct
21f73 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
21f74 70 49 74 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61  pItem = pList->a
21f75 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20  ;.        for(; 
21f76 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b  i>0; i--, pItem+
21f77 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
21f78 28 20 41 4c 57 41 59 53 28 70 49 74 65 6d 2d 3e  ( ALWAYS(pItem->
21f79 70 45 78 70 72 29 20 29 20 70 49 74 65 6d 2d 3e  pExpr) ) pItem->
21f7a 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20  pExpr->flags |= 
21f7b 45 50 5f 46 69 78 65 64 44 65 73 74 3b 0a 20 20  EP_FixedDest;.  
21f7c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
21f7d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21f7e 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 41   }.  }.  if( isA
21f7f 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 61 63  ppropriateForFac
21f80 74 6f 72 69 6e 67 28 70 45 78 70 72 29 20 29 7b  toring(pExpr) ){
21f81 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b  .    int r1 = ++
21f82 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
21f83 20 20 69 6e 74 20 72 32 3b 0a 20 20 20 20 72 32    int r2;.    r2
21f84 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
21f85 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
21f86 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 20   pExpr, r1);.   
21f87 20 69 66 28 20 4e 45 56 45 52 28 72 31 21 3d 72   if( NEVER(r1!=r
21f88 32 29 20 29 20 73 71 6c 69 74 65 33 52 65 6c 65  2) ) sqlite3Rele
21f89 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
21f8a 65 2c 20 72 31 29 3b 0a 20 20 20 20 70 45 78 70  e, r1);.    pExp
21f8b 72 2d 3e 6f 70 32 20 3d 20 70 45 78 70 72 2d 3e  r->op2 = pExpr->
21f8c 6f 70 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f  op;.    pExpr->o
21f8d 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  p = TK_REGISTER;
21f8e 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  .    pExpr->iTab
21f8f 6c 65 20 3d 20 72 32 3b 0a 20 20 20 20 72 65 74  le = r2;.    ret
21f90 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
21f91 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f   }.  return WRC_
21f92 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a  Continue;.}../*.
21f93 2a 2a 20 50 72 65 65 76 61 6c 75 61 74 65 20 63  ** Preevaluate c
21f94 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65  onstant subexpre
21f95 73 73 69 6f 6e 73 20 77 69 74 68 69 6e 20 70 45  ssions within pE
21f96 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68  xpr and store th
21f97 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20  e.** results in 
21f98 72 65 67 69 73 74 65 72 73 2e 20 20 4d 6f 64 69  registers.  Modi
21f99 66 79 20 70 45 78 70 72 20 73 6f 20 74 68 61 74  fy pExpr so that
21f9a 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 73 75   the constant su
21f9b 62 65 78 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61  bexpresions.** a
21f9c 72 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 6f  re TK_REGISTER o
21f9d 70 63 6f 64 65 73 20 74 68 61 74 20 72 65 66 65  pcodes that refe
21f9e 72 20 74 6f 20 74 68 65 20 70 72 65 63 6f 6d 70  r to the precomp
21f9f 75 74 65 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a  uted values..*/.
21fa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21fa1 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
21fa2 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72  odeConstants(Par
21fa3 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
21fa4 20 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b   *pExpr){.  Walk
21fa5 65 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43  er w;.  w.xExprC
21fa6 61 6c 6c 62 61 63 6b 20 3d 20 65 76 61 6c 43 6f  allback = evalCo
21fa7 6e 73 74 45 78 70 72 3b 0a 20 20 77 2e 78 53 65  nstExpr;.  w.xSe
21fa8 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 30  lectCallback = 0
21fa9 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70  ;.  w.pParse = p
21faa 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33  Parse;.  sqlite3
21fab 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78  WalkExpr(&w, pEx
21fac 70 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47  pr);.}.../*.** G
21fad 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
21fae 74 20 70 75 73 68 65 73 20 74 68 65 20 76 61 6c  t pushes the val
21faf 75 65 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d  ue of every elem
21fb0 65 6e 74 20 6f 66 20 74 68 65 20 67 69 76 65 6e  ent of the given
21fb1 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  .** expression l
21fb2 69 73 74 20 69 6e 74 6f 20 61 20 73 65 71 75 65  ist into a seque
21fb3 6e 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73  nce of registers
21fb4 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 74 61   beginning at ta
21fb5 72 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  rget..**.** Retu
21fb6 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
21fb7 20 65 6c 65 6d 65 6e 74 73 20 65 76 61 6c 75 61   elements evalua
21fb8 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
21fb9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
21fba 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
21fbb 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
21fbc 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
21fbd 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
21fbe 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
21fbf 2c 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65  ,   /* The expre
21fc0 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65  ssion list to be
21fc1 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
21fc2 74 61 72 67 65 74 2c 20 20 20 20 20 20 20 20 2f  target,        /
21fc3 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65  * Where to write
21fc4 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e   results */.  in
21fc5 74 20 64 6f 48 61 72 64 43 6f 70 79 20 20 20 20  t doHardCopy    
21fc6 20 2f 2a 20 4d 61 6b 65 20 61 20 68 61 72 64 20   /* Make a hard 
21fc7 63 6f 70 79 20 6f 66 20 65 76 65 72 79 20 65 6c  copy of every el
21fc8 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74  ement */.){.  st
21fc9 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
21fca 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74  em *pItem;.  int
21fcb 20 69 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28   i, n;.  assert(
21fcc 20 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 61   pList!=0 );.  a
21fcd 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20  ssert( target>0 
21fce 29 3b 0a 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e  );.  n = pList->
21fcf 6e 45 78 70 72 3b 0a 20 20 66 6f 72 28 70 49 74  nExpr;.  for(pIt
21fd0 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30  em=pList->a, i=0
21fd1 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74 65  ; i<n; i++, pIte
21fd2 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49  m++){.    if( pI
21fd3 74 65 6d 2d 3e 69 41 6c 69 61 73 20 29 7b 0a 20  tem->iAlias ){. 
21fd4 20 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d 20       int iReg = 
21fd5 63 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65  codeAlias(pParse
21fd6 2c 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 2c  , pItem->iAlias,
21fd7 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74   pItem->pExpr, t
21fd8 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 20  arget+i);.      
21fd9 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
21fda 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
21fdb 3b 0a 20 20 20 20 20 20 69 66 28 20 69 52 65 67  ;.      if( iReg
21fdc 21 3d 74 61 72 67 65 74 2b 69 20 29 7b 0a 20 20  !=target+i ){.  
21fdd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21fde 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
21fdf 6f 70 79 2c 20 69 52 65 67 2c 20 74 61 72 67 65  opy, iReg, targe
21fe0 74 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  t+i);.      }.  
21fe1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
21fe2 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
21fe3 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45  Parse, pItem->pE
21fe4 78 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a  xpr, target+i);.
21fe5 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6f      }.    if( do
21fe6 48 61 72 64 43 6f 70 79 20 26 26 20 21 70 50 61  HardCopy && !pPa
21fe7 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
21fe8 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73  ailed ){.      s
21fe9 71 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f  qlite3ExprHardCo
21fea 70 79 28 70 50 61 72 73 65 2c 20 74 61 72 67 65  py(pParse, targe
21feb 74 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  t, n);.    }.  }
21fec 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
21fed 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
21fee 6f 64 65 20 66 6f 72 20 61 20 42 45 54 57 45 45  ode for a BETWEE
21fef 4e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 0a 2a  N operator..**.*
21ff0 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79  *    x BETWEEN y
21ff1 20 41 4e 44 20 7a 0a 2a 2a 0a 2a 2a 20 54 68 65   AND z.**.** The
21ff2 20 61 62 6f 76 65 20 69 73 20 65 71 75 69 76 61   above is equiva
21ff3 6c 65 6e 74 20 74 6f 20 0a 2a 2a 0a 2a 2a 20 20  lent to .**.**  
21ff4 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a    x>=y AND x<=z.
21ff5 2a 2a 0a 2a 2a 20 43 6f 64 65 20 69 74 20 61 73  **.** Code it as
21ff6 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 63 61   such, taking ca
21ff7 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d  re to do the com
21ff8 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73 69 6f  mon subexpressio
21ff9 6e 0a 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f  n.** elementatio
21ffa 6e 20 6f 66 20 78 2e 0a 2a 2f 0a 73 74 61 74 69  n of x..*/.stati
21ffb 63 20 76 6f 69 64 20 65 78 70 72 43 6f 64 65 42  c void exprCodeB
21ffc 65 74 77 65 65 6e 28 0a 20 20 50 61 72 73 65 20  etween(.  Parse 
21ffd 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50  *pParse,    /* P
21ffe 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
21fff 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
22000 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  xt */.  Expr *pE
22001 78 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  xpr,      /* The
22002 20 42 45 54 57 45 45 4e 20 65 78 70 72 65 73 73   BETWEEN express
22003 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73  ion */.  int des
22004 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  t,         /* Ju
22005 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 20 6a  mp here if the j
22006 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 2a 2f 0a  ump is taken */.
22007 20 20 69 6e 74 20 6a 75 6d 70 49 66 54 72 75 65    int jumpIfTrue
22008 2c 20 20 20 2f 2a 20 54 61 6b 65 20 74 68 65 20  ,   /* Take the 
22009 6a 75 6d 70 20 69 66 20 74 68 65 20 42 45 54 57  jump if the BETW
2200a 45 45 4e 20 69 73 20 74 72 75 65 20 2a 2f 0a 20  EEN is true */. 
2200b 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 20   int jumpIfNull 
2200c 20 20 20 2f 2a 20 54 61 6b 65 20 74 68 65 20 6a     /* Take the j
2200d 75 6d 70 20 69 66 20 74 68 65 20 42 45 54 57 45  ump if the BETWE
2200e 45 4e 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 29 7b  EN is NULL */.){
2200f 0a 20 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b  .  Expr exprAnd;
22010 20 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20       /* The AND 
22011 6f 70 65 72 61 74 6f 72 20 69 6e 20 20 78 3e 3d  operator in  x>=
22012 79 20 41 4e 44 20 78 3c 3d 7a 20 20 2a 2f 0a 20  y AND x<=z  */. 
22013 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b 20   Expr compLeft; 
22014 20 20 20 2f 2a 20 54 68 65 20 20 78 3e 3d 79 20     /* The  x>=y 
22015 20 74 65 72 6d 20 2a 2f 0a 20 20 45 78 70 72 20   term */.  Expr 
22016 63 6f 6d 70 52 69 67 68 74 3b 20 20 20 2f 2a 20  compRight;   /* 
22017 54 68 65 20 20 78 3c 3d 7a 20 20 74 65 72 6d 20  The  x<=z  term 
22018 2a 2f 0a 20 20 45 78 70 72 20 65 78 70 72 58 3b  */.  Expr exprX;
22019 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 20 78         /* The  x
2201a 20 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20    subexpression 
2201b 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65  */.  int regFree
2201c 31 20 3d 20 30 3b 20 2f 2a 20 54 65 6d 70 6f 72  1 = 0; /* Tempor
2201d 61 72 79 20 75 73 65 20 72 65 67 69 73 74 65 72  ary use register
2201e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 21   */..  assert( !
2201f 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
22020 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
22021 65 63 74 29 20 29 3b 0a 20 20 65 78 70 72 58 20  ect) );.  exprX 
22022 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b  = *pExpr->pLeft;
22023 0a 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20  .  exprAnd.op = 
22024 54 4b 5f 41 4e 44 3b 0a 20 20 65 78 70 72 41 6e  TK_AND;.  exprAn
22025 64 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c  d.pLeft = &compL
22026 65 66 74 3b 0a 20 20 65 78 70 72 41 6e 64 2e 70  eft;.  exprAnd.p
22027 52 69 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67  Right = &compRig
22028 68 74 3b 0a 20 20 63 6f 6d 70 4c 65 66 74 2e 6f  ht;.  compLeft.o
22029 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 63 6f 6d  p = TK_GE;.  com
2202a 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65  pLeft.pLeft = &e
2202b 78 70 72 58 3b 0a 20 20 63 6f 6d 70 4c 65 66 74  xprX;.  compLeft
2202c 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  .pRight = pExpr-
2202d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  >x.pList->a[0].p
2202e 45 78 70 72 3b 0a 20 20 63 6f 6d 70 52 69 67 68  Expr;.  compRigh
2202f 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20  t.op = TK_LE;.  
22030 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20  compRight.pLeft 
22031 3d 20 26 65 78 70 72 58 3b 0a 20 20 63 6f 6d 70  = &exprX;.  comp
22032 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20 70  Right.pRight = p
22033 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  Expr->x.pList->a
22034 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 65 78 70  [1].pExpr;.  exp
22035 72 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69  rX.iTable = sqli
22036 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
22037 70 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20  pParse, &exprX, 
22038 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 65 78  &regFree1);.  ex
22039 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49  prX.op = TK_REGI
2203a 53 54 45 52 3b 0a 20 20 69 66 28 20 6a 75 6d 70  STER;.  if( jump
2203b 49 66 54 72 75 65 20 29 7b 0a 20 20 20 20 73 71  IfTrue ){.    sq
2203c 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
2203d 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64  pParse, &exprAnd
2203e 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
2203f 6c 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ll);.  }else{.  
22040 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
22041 61 6c 73 65 28 70 50 61 72 73 65 2c 20 26 65 78  alse(pParse, &ex
22042 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d  prAnd, dest, jum
22043 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20  pIfNull);.  }.  
22044 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
22045 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
22046 67 46 72 65 65 31 29 3b 0a 0a 20 20 2f 2a 20 45  gFree1);..  /* E
22047 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 20 74  nsure adequate t
22048 65 73 74 20 63 6f 76 65 72 61 67 65 20 2a 2f 0a  est coverage */.
22049 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
2204a 49 66 54 72 75 65 3d 3d 30 20 26 26 20 6a 75 6d  IfTrue==0 && jum
2204b 70 49 66 4e 75 6c 6c 3d 3d 30 20 26 26 20 72 65  pIfNull==0 && re
2204c 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 74  gFree1==0 );.  t
2204d 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 54  estcase( jumpIfT
2204e 72 75 65 3d 3d 30 20 26 26 20 6a 75 6d 70 49 66  rue==0 && jumpIf
2204f 4e 75 6c 6c 3d 3d 30 20 26 26 20 72 65 67 46 72  Null==0 && regFr
22050 65 65 31 21 3d 30 20 29 3b 0a 20 20 74 65 73 74  ee1!=0 );.  test
22051 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72 75 65  case( jumpIfTrue
22052 3d 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c  ==0 && jumpIfNul
22053 6c 21 3d 30 20 26 26 20 72 65 67 46 72 65 65 31  l!=0 && regFree1
22054 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==0 );.  testcas
22055 65 28 20 6a 75 6d 70 49 66 54 72 75 65 3d 3d 30  e( jumpIfTrue==0
22056 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d   && jumpIfNull!=
22057 30 20 26 26 20 72 65 67 46 72 65 65 31 21 3d 30  0 && regFree1!=0
22058 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
22059 6a 75 6d 70 49 66 54 72 75 65 21 3d 30 20 26 26  jumpIfTrue!=0 &&
2205a 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 26   jumpIfNull==0 &
2205b 26 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  & regFree1==0 );
2205c 0a 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d  .  testcase( jum
2205d 70 49 66 54 72 75 65 21 3d 30 20 26 26 20 6a 75  pIfTrue!=0 && ju
2205e 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 26 26 20 72  mpIfNull==0 && r
2205f 65 67 46 72 65 65 31 21 3d 30 20 29 3b 0a 20 20  egFree1!=0 );.  
22060 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
22061 54 72 75 65 21 3d 30 20 26 26 20 6a 75 6d 70 49  True!=0 && jumpI
22062 66 4e 75 6c 6c 21 3d 30 20 26 26 20 72 65 67 46  fNull!=0 && regF
22063 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 74 65 73  ree1==0 );.  tes
22064 74 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72 75  tcase( jumpIfTru
22065 65 21 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e 75  e!=0 && jumpIfNu
22066 6c 6c 21 3d 30 20 26 26 20 72 65 67 46 72 65 65  ll!=0 && regFree
22067 31 21 3d 30 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  1!=0 );.}../*.**
22068 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
22069 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70  or a boolean exp
2206a 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61  ression such tha
2206b 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65  t a jump is made
2206c 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c  .** to the label
2206d 20 22 64 65 73 74 22 20 69 66 20 74 68 65 20 65   "dest" if the e
2206e 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75  xpression is tru
2206f 65 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e 0a  e but execution.
22070 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74 72  ** continues str
22071 61 69 67 68 74 20 74 68 72 75 20 69 66 20 74 68  aight thru if th
22072 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
22073 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  false..**.** If 
22074 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65  the expression e
22075 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c  valuates to NULL
22076 20 28 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e   (neither true n
22077 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a  or false), then.
22078 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  ** take the jump
22079 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66 4e 75   if the jumpIfNu
2207a 6c 6c 20 66 6c 61 67 20 69 73 20 53 51 4c 49 54  ll flag is SQLIT
2207b 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a  E_JUMPIFNULL..**
2207c 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 64 65  .** This code de
2207d 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 66 61 63  pends on the fac
2207e 74 20 74 68 61 74 20 63 65 72 74 61 69 6e 20 74  t that certain t
2207f 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28 65 78 3a  oken values (ex:
22080 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74   TK_EQ).** are t
22081 68 65 20 73 61 6d 65 20 61 73 20 6f 70 63 6f 64  he same as opcod
22082 65 20 76 61 6c 75 65 73 20 28 65 78 3a 20 4f 50  e values (ex: OP
22083 5f 45 71 29 20 74 68 61 74 20 69 6d 70 6c 65 6d  _Eq) that implem
22084 65 6e 74 20 74 68 65 20 63 6f 72 72 65 73 70 6f  ent the correspo
22085 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69  nding.** operati
22086 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20 63 6f 6d  on.  Special com
22087 6d 65 6e 74 73 20 69 6e 20 76 64 62 65 2e 63 20  ments in vdbe.c 
22088 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  and the mkopcode
22089 68 2e 61 77 6b 20 73 63 72 69 70 74 20 69 6e 0a  h.awk script in.
2208a 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70 72 6f 63  ** the make proc
2208b 65 73 73 20 63 61 75 73 65 20 74 68 65 73 65 20  ess cause these 
2208c 76 61 6c 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e  values to align.
2208d 20 20 41 73 73 65 72 74 28 29 73 20 69 6e 20 74    Assert()s in t
2208e 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77  he code.** below
2208f 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65   verify that the
22090 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c 69   numbers are ali
22091 67 6e 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a  gned correctly..
22092 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22093 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
22094 70 72 49 66 54 72 75 65 28 50 61 72 73 65 20 2a  prIfTrue(Parse *
22095 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
22096 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69  xpr, int dest, i
22097 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a  nt jumpIfNull){.
22098 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
22099 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2209a 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72   op = 0;.  int r
2209b 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69  egFree1 = 0;.  i
2209c 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b  nt regFree2 = 0;
2209d 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a  .  int r1, r2;..
2209e 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66    assert( jumpIf
2209f 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d  Null==SQLITE_JUM
220a0 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49  PIFNULL || jumpI
220a1 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66  fNull==0 );.  if
220a2 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
220a3 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20      return;  /* 
220a4 45 78 69 73 74 61 6e 63 65 20 6f 66 20 56 44 42  Existance of VDB
220a5 45 20 63 68 65 63 6b 65 64 20 62 79 20 63 61 6c  E checked by cal
220a6 6c 65 72 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56  ler */.  if( NEV
220a7 45 52 28 70 45 78 70 72 3d 3d 30 29 20 29 20 72  ER(pExpr==0) ) r
220a8 65 74 75 72 6e 3b 20 20 2f 2a 20 4e 6f 20 77 61  eturn;  /* No wa
220a9 79 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65  y this can happe
220aa 6e 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 45 78 70  n */.  op = pExp
220ab 72 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63 68 28  r->op;.  switch(
220ac 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
220ad 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20  TK_AND: {.      
220ae 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33  int d2 = sqlite3
220af 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
220b0 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
220b1 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
220b2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
220b3 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
220b4 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  arse);.      sql
220b5 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
220b6 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
220b7 4c 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e  Left, d2,jumpIfN
220b8 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ull^SQLITE_JUMPI
220b9 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71  FNULL);.      sq
220ba 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
220bb 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
220bc 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d  Right, dest, jum
220bd 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
220be 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
220bf 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a  veLabel(v, d2);.
220c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
220c1 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65  rCachePop(pParse
220c2 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , 1);.      brea
220c3 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
220c4 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  e TK_OR: {.     
220c5 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
220c6 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
220c7 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54    sqlite3ExprIfT
220c8 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  rue(pParse, pExp
220c9 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20  r->pLeft, dest, 
220ca 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
220cb 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
220cc 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78  True(pParse, pEx
220cd 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74  pr->pRight, dest
220ce 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
220cf 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
220d0 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f  }.    case TK_NO
220d1 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  T: {.      testc
220d2 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
220d3 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
220d4 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
220d5 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
220d6 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
220d7 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72  fNull);.      br
220d8 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
220d9 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63  ase TK_LT:.    c
220da 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63  ase TK_LE:.    c
220db 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63  ase TK_GT:.    c
220dc 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63  ase TK_GE:.    c
220dd 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63  ase TK_NE:.    c
220de 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20  ase TK_EQ: {.   
220df 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54     assert( TK_LT
220e0 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20  ==OP_Lt );.     
220e1 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d   assert( TK_LE==
220e2 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 61  OP_Le );.      a
220e3 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50  ssert( TK_GT==OP
220e4 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Gt );.      ass
220e5 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47  ert( TK_GE==OP_G
220e6 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  e );.      asser
220e7 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20  t( TK_EQ==OP_Eq 
220e8 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
220e9 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b   TK_NE==OP_Ne );
220ea 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
220eb 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20   op==TK_LT );.  
220ec 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
220ed 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20  ==TK_LE );.     
220ee 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
220ef 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65  K_GT );.      te
220f0 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
220f1 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
220f2 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29  ase( op==TK_EQ )
220f3 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
220f4 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20  ( op==TK_NE );. 
220f5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
220f6 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
220f7 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
220f8 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65  eOperands(pParse
220f9 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
220fa 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a  &r1, &regFree1,.
220fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
220fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
220fd 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c    pExpr->pRight,
220fe 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29   &r2, &regFree2)
220ff 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
22100 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  are(pParse, pExp
22101 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d  r->pLeft, pExpr-
22102 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20  >pRight, op,.   
22103 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
22104 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d  1, r2, dest, jum
22105 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
22106 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
22107 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
22108 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
22109 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  2==0 );.      br
2210a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
2210b 61 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63  ase TK_IS:.    c
2210c 61 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a  ase TK_ISNOT: {.
2210d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2210e 6f 70 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20  op==TK_IS );.   
2210f 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
22110 3d 54 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20  =TK_ISNOT );.   
22111 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70     codeCompareOp
22112 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70  erands(pParse, p
22113 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31  Expr->pLeft, &r1
22114 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20  , &regFree1,.   
22115 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22116 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
22117 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72  Expr->pRight, &r
22118 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20  2, &regFree2);. 
22119 20 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54       op = (op==T
2211a 4b 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20  K_IS) ? TK_EQ : 
2211b 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64  TK_NE;.      cod
2211c 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c  eCompare(pParse,
2211d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70   pExpr->pLeft, p
2211e 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70  Expr->pRight, op
2211f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
22120 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74      r1, r2, dest
22121 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29  , SQLITE_NULLEQ)
22122 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
22123 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
22124 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
22125 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a   regFree2==0 );.
22126 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
22127 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49   }.    case TK_I
22128 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20  SNULL:.    case 
22129 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20  TK_NOTNULL: {.  
2212a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49      assert( TK_I
2212b 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c  SNULL==OP_IsNull
2212c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2212d 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50  ( TK_NOTNULL==OP
2212e 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  _NotNull );.    
2212f 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
22130 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20  TK_ISNULL );.   
22131 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
22132 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20  =TK_NOTNULL );. 
22133 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
22134 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
22135 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
22136 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a  ft, &regFree1);.
22137 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22138 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72  eAddOp2(v, op, r
22139 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20  1, dest);.      
2213a 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
2213b 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e1==0 );.      b
2213c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2213d 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
2213e 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
2213f 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
22140 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 43 6f   );.      exprCo
22141 64 65 42 65 74 77 65 65 6e 28 70 50 61 72 73 65  deBetween(pParse
22142 2c 20 70 45 78 70 72 2c 20 64 65 73 74 2c 20 31  , pExpr, dest, 1
22143 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
22144 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22145 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  }.    case TK_IN
22146 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65  : {.      int de
22147 73 74 49 66 46 61 6c 73 65 20 3d 20 73 71 6c 69  stIfFalse = sqli
22148 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
22149 28 76 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 64  (v);.      int d
2214a 65 73 74 49 66 4e 75 6c 6c 20 3d 20 6a 75 6d 70  estIfNull = jump
2214b 49 66 4e 75 6c 6c 20 3f 20 64 65 73 74 20 3a 20  IfNull ? dest : 
2214c 64 65 73 74 49 66 46 61 6c 73 65 3b 0a 20 20 20  destIfFalse;.   
2214d 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
2214e 64 65 49 4e 28 70 50 61 72 73 65 2c 20 70 45 78  deIN(pParse, pEx
2214f 70 72 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c  pr, destIfFalse,
22150 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20   destIfNull);.  
22151 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22152 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
22153 2c 20 30 2c 20 64 65 73 74 29 3b 0a 20 20 20 20  , 0, dest);.    
22154 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
22155 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 65 73  olveLabel(v, des
22156 74 49 66 46 61 6c 73 65 29 3b 0a 20 20 20 20 20  tIfFalse);.     
22157 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
22158 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
22159 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
2215a 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
2215b 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46  se, pExpr, &regF
2215c 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ree1);.      sql
2215d 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2215e 2c 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64 65 73  , OP_If, r1, des
2215f 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30  t, jumpIfNull!=0
22160 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
22161 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
22162 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
22163 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
22164 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
22165 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
22166 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
22167 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65  g(pParse, regFre
22168 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  e1);.  sqlite3Re
22169 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2216a 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 20  rse, regFree2); 
2216b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72   .}../*.** Gener
2216c 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 62  ate code for a b
2216d 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f  oolean expressio
2216e 6e 20 73 75 63 68 20 74 68 61 74 20 61 20 6a 75  n such that a ju
2216f 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f  mp is made.** to
22170 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65 73 74   the label "dest
22171 22 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  " if the express
22172 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62 75 74  ion is false but
22173 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f   execution.** co
22174 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 68 74  ntinues straight
22175 20 74 68 72 75 20 69 66 20 74 68 65 20 65 78 70   thru if the exp
22176 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 2e  ression is true.
22177 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  .**.** If the ex
22178 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74  pression evaluat
22179 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74  es to NULL (neit
2217a 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66 61 6c  her true nor fal
2217b 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70  se) then.** jump
2217c 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 69   if jumpIfNull i
2217d 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  s SQLITE_JUMPIFN
2217e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68 72 6f  ULL or fall thro
2217f 75 67 68 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c  ugh if jumpIfNul
22180 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a 53 51  l.** is 0..*/.SQ
22181 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22182 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46  d sqlite3ExprIfF
22183 61 6c 73 65 28 50 61 72 73 65 20 2a 70 50 61 72  alse(Parse *pPar
22184 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  se, Expr *pExpr,
22185 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a   int dest, int j
22186 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64  umpIfNull){.  Vd
22187 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
22188 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20  pVdbe;.  int op 
22189 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72  = 0;.  int regFr
2218a 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  ee1 = 0;.  int r
2218b 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 69  egFree2 = 0;.  i
2218c 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 73  nt r1, r2;..  as
2218d 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  sert( jumpIfNull
2218e 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  ==SQLITE_JUMPIFN
2218f 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c  ULL || jumpIfNul
22190 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e 45  l==0 );.  if( NE
22191 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75  VER(v==0) ) retu
22192 72 6e 3b 20 2f 2a 20 45 78 69 73 74 61 6e 63 65  rn; /* Existance
22193 20 6f 66 20 56 44 42 45 20 63 68 65 63 6b 65 64   of VDBE checked
22194 20 62 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20 20   by caller */.  
22195 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 20  if( pExpr==0 )  
22196 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20    return;..  /* 
22197 54 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78  The value of pEx
22198 70 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61 72  pr->op and op ar
22199 65 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f 6c  e related as fol
2219a 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  lows:.  **.  ** 
2219b 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
2219c 20 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20 20             op.  
2219d 2a 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  **       -------
2219e 2d 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d  --          ----
2219f 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 20  ------.  **     
221a0 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20    TK_ISNULL     
221a1 20 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a       OP_NotNull.
221a2 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e 4f    **       TK_NO
221a3 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f 50  TNULL         OP
221a4 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20  _IsNull.  **    
221a5 20 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20     TK_NE        
221a6 20 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a 2a        OP_Eq.  **
221a7 20 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20 20         TK_EQ    
221a8 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 0a            OP_Ne.
221a9 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 54    **       TK_GT
221aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
221ab 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  _Le.  **       T
221ac 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  K_LE            
221ad 20 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 20    OP_Gt.  **    
221ae 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20     TK_GE        
221af 20 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a 2a        OP_Lt.  **
221b0 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20 20         TK_LT    
221b1 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65 0a            OP_Ge.
221b2 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f 74    **.  ** For ot
221b3 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70 45  her values of pE
221b4 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 75  xpr->op, op is u
221b5 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e 75  ndefined and unu
221b6 73 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76 61  sed..  ** The va
221b7 6c 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f  lue of TK_ and O
221b8 50 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65  P_ constants are
221b9 20 61 72 72 61 6e 67 65 64 20 73 75 63 68 20 74   arranged such t
221ba 68 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e 20  hat we.  ** can 
221bb 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70 70  compute the mapp
221bc 69 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67 20  ing above using 
221bd 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78  the following ex
221be 70 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 41  pression..  ** A
221bf 73 73 65 72 74 28 29 73 20 76 65 72 69 66 79 20  ssert()s verify 
221c0 74 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74 61  that the computa
221c1 74 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74 2e  tion is correct.
221c2 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 70  .  */.  op = ((p
221c3 45 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 4e  Expr->op+(TK_ISN
221c4 55 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f 49  ULL&1))^1)-(TK_I
221c5 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a 20  SNULL&1);..  /* 
221c6 56 65 72 69 66 79 20 63 6f 72 72 65 63 74 20 61  Verify correct a
221c7 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f 20  lignment of TK_ 
221c8 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74  and OP_ constant
221c9 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  s.  */.  assert(
221ca 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49   pExpr->op!=TK_I
221cb 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  SNULL || op==OP_
221cc 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73  NotNull );.  ass
221cd 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
221ce 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70  TK_NOTNULL || op
221cf 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20  ==OP_IsNull );. 
221d0 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
221d1 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d  op!=TK_NE || op=
221d2 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73 65  =OP_Eq );.  asse
221d3 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
221d4 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e  K_EQ || op==OP_N
221d5 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
221d6 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 20  Expr->op!=TK_LT 
221d7 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b 0a  || op==OP_Ge );.
221d8 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
221d9 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f 70  >op!=TK_LE || op
221da 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73 73  ==OP_Gt );.  ass
221db 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
221dc 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TK_GT || op==OP_
221dd 4c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Le );.  assert( 
221de 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 45  pExpr->op!=TK_GE
221df 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 3b   || op==OP_Lt );
221e0 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70  ..  switch( pExp
221e1 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  r->op ){.    cas
221e2 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20  e TK_AND: {.    
221e3 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
221e4 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
221e5 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
221e6 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45  False(pParse, pE
221e7 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74  xpr->pLeft, dest
221e8 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
221e9 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
221ea 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
221eb 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64  pExpr->pRight, d
221ec 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
221ed 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
221ee 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
221ef 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  _OR: {.      int
221f0 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62   d2 = sqlite3Vdb
221f1 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
221f2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
221f3 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
221f4 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
221f5 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73  rCachePush(pPars
221f6 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
221f7 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
221f8 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
221f9 2c 20 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  , d2, jumpIfNull
221fa 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55  ^SQLITE_JUMPIFNU
221fb 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  LL);.      sqlit
221fc 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
221fd 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69  arse, pExpr->pRi
221fe 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  ght, dest, jumpI
221ff 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71  fNull);.      sq
22200 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
22201 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20  Label(v, d2);.  
22202 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
22203 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20  achePop(pParse, 
22204 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  1);.      break;
22205 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
22206 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20  TK_NOT: {.      
22207 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
22208 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Null==0 );.     
22209 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72   sqlite3ExprIfTr
2220a 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ue(pParse, pExpr
2220b 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a  ->pLeft, dest, j
2220c 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
2220d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2220e 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20     case TK_LT:. 
2220f 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20     case TK_LE:. 
22210 20 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20     case TK_GT:. 
22211 20 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20     case TK_GE:. 
22212 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20     case TK_NE:. 
22213 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b     case TK_EQ: {
22214 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
22215 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20   op==TK_LT );.  
22216 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
22217 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20  ==TK_LE );.     
22218 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
22219 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65  K_GT );.      te
2221a 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
2221b 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
2221c 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29  ase( op==TK_EQ )
2221d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2221e 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20  ( op==TK_NE );. 
2221f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
22220 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
22221 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
22222 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65  eOperands(pParse
22223 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
22224 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a  &r1, &regFree1,.
22225 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22226 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22227 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c    pExpr->pRight,
22228 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29   &r2, &regFree2)
22229 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
2222a 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  are(pParse, pExp
2222b 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d  r->pLeft, pExpr-
2222c 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20  >pRight, op,.   
2222d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
2222e 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d  1, r2, dest, jum
2222f 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
22230 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
22231 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
22232 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
22233 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  2==0 );.      br
22234 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
22235 61 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63  ase TK_IS:.    c
22236 61 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a  ase TK_ISNOT: {.
22237 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
22238 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53  pExpr->op==TK_IS
22239 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2223a 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
2223b 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20  K_ISNOT );.     
2223c 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72   codeCompareOper
2223d 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78  ands(pParse, pEx
2223e 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20  pr->pLeft, &r1, 
2223f 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20  &regFree1,.     
22240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22241 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
22242 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c  pr->pRight, &r2,
22243 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20   &regFree2);.   
22244 20 20 20 6f 70 20 3d 20 28 70 45 78 70 72 2d 3e     op = (pExpr->
22245 6f 70 3d 3d 54 4b 5f 49 53 29 20 3f 20 54 4b 5f  op==TK_IS) ? TK_
22246 4e 45 20 3a 20 54 4b 5f 45 51 3b 0a 20 20 20 20  NE : TK_EQ;.    
22247 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50    codeCompare(pP
22248 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
22249 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ft, pExpr->pRigh
2224a 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20  t, op,.         
2224b 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c           r1, r2,
2224c 20 64 65 73 74 2c 20 53 51 4c 49 54 45 5f 4e 55   dest, SQLITE_NU
2224d 4c 4c 45 51 29 3b 0a 20 20 20 20 20 20 74 65 73  LLEQ);.      tes
2224e 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
2224f 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
22250 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
22251 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
22252 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
22253 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20   TK_ISNULL:.    
22254 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a  case TK_NOTNULL:
22255 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
22256 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c  e( op==TK_ISNULL
22257 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
22258 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55  se( op==TK_NOTNU
22259 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  LL );.      r1 =
2225a 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2225b 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
2225c 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
2225d 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ree1);.      sql
2225e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2225f 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b  , op, r1, dest);
22260 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
22261 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
22262 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
22263 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42   }.    case TK_B
22264 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20  ETWEEN: {.      
22265 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
22266 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Null==0 );.     
22267 20 65 78 70 72 43 6f 64 65 42 65 74 77 65 65 6e   exprCodeBetween
22268 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
22269 64 65 73 74 2c 20 30 2c 20 6a 75 6d 70 49 66 4e  dest, 0, jumpIfN
2226a 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ull);.      brea
2226b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2226c 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20  e TK_IN: {.     
2226d 20 69 66 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 20   if( jumpIfNull 
2226e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2226f 65 33 45 78 70 72 43 6f 64 65 49 4e 28 70 50 61  e3ExprCodeIN(pPa
22270 72 73 65 2c 20 70 45 78 70 72 2c 20 64 65 73 74  rse, pExpr, dest
22271 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 7d  , dest);.      }
22272 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
22273 74 20 64 65 73 74 49 66 4e 75 6c 6c 20 3d 20 73  t destIfNull = s
22274 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
22275 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20  bel(v);.        
22276 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49  sqlite3ExprCodeI
22277 4e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  N(pParse, pExpr,
22278 20 64 65 73 74 2c 20 64 65 73 74 49 66 4e 75 6c   dest, destIfNul
22279 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l);.        sqli
2227a 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2227b 62 65 6c 28 76 2c 20 64 65 73 74 49 66 4e 75 6c  bel(v, destIfNul
2227c 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  l);.      }.    
2227d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2227e 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
2227f 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
22280 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
22281 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67  rse, pExpr, &reg
22282 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71  Free1);.      sq
22283 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
22284 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c  v, OP_IfNot, r1,
22285 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
22286 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73  l!=0);.      tes
22287 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
22288 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
22289 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
2228a 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
2228b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
2228c 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2228d 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
2228e 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74  gFree1);.  sqlit
2228f 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
22290 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
22291 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20  2);.}../*.** Do 
22292 61 20 64 65 65 70 20 63 6f 6d 70 61 72 69 73 6f  a deep compariso
22293 6e 20 6f 66 20 74 77 6f 20 65 78 70 72 65 73 73  n of two express
22294 69 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 74 75  ion trees.  Retu
22295 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72  rn TRUE (non-zer
22296 6f 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72  o).** if they ar
22297 65 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 20  e identical and 
22298 72 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20  return FALSE if 
22299 74 68 65 79 20 64 69 66 66 65 72 20 69 6e 20 61  they differ in a
2229a 6e 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f  ny way..**.** So
2229b 6d 65 74 69 6d 65 73 20 74 68 69 73 20 72 6f 75  metimes this rou
2229c 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e  tine will return
2229d 20 46 41 4c 53 45 20 65 76 65 6e 20 69 66 20 74   FALSE even if t
2229e 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f  he two expressio
2229f 6e 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72 65  ns.** really are
222a0 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 49 66   equivalent.  If
222a1 20 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76 65   we cannot prove
222a2 20 74 68 61 74 20 74 68 65 20 65 78 70 72 65 73   that the expres
222a3 73 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64 65  sions are.** ide
222a4 6e 74 69 63 61 6c 2c 20 77 65 20 72 65 74 75 72  ntical, we retur
222a5 6e 20 46 41 4c 53 45 20 6a 75 73 74 20 74 6f 20  n FALSE just to 
222a6 62 65 20 73 61 66 65 2e 20 20 53 6f 20 69 66 20  be safe.  So if 
222a7 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
222a8 72 65 74 75 72 6e 73 20 66 61 6c 73 65 2c 20 74  returns false, t
222a9 68 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20 72  hen you do not r
222aa 65 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20 63  eally know for c
222ab 65 72 74 61 69 6e 20 69 66 20 74 68 65 20 74 77  ertain if the tw
222ac 6f 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73  o.** expressions
222ad 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20   are the same.  
222ae 42 75 74 20 69 66 20 79 6f 75 20 67 65 74 20 61  But if you get a
222af 20 54 52 55 45 20 72 65 74 75 72 6e 2c 20 74 68   TRUE return, th
222b0 65 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62 65  en you.** can be
222b1 20 73 75 72 65 20 74 68 65 20 65 78 70 72 65 73   sure the expres
222b2 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61  sions are the sa
222b3 6d 65 2e 20 20 49 6e 20 74 68 65 20 70 6c 61 63  me.  In the plac
222b4 65 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 69 73  es where.** this
222b5 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
222b6 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 68 75  , it does not hu
222b7 72 74 20 74 6f 20 67 65 74 20 61 6e 20 65 78 74  rt to get an ext
222b8 72 61 20 46 41 4c 53 45 20 2d 20 74 68 61 74 0a  ra FALSE - that.
222b9 2a 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65  ** just might re
222ba 73 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c 69  sult in some sli
222bb 67 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f 64  ghtly slower cod
222bc 65 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69 6e  e.  But returnin
222bd 67 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63  g.** an incorrec
222be 74 20 54 52 55 45 20 63 6f 75 6c 64 20 6c 65 61  t TRUE could lea
222bf 64 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74 69  d to a malfuncti
222c0 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
222c1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
222c2 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70  3ExprCompare(Exp
222c3 72 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 42 29  r *pA, Expr *pB)
222c4 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
222c5 20 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b   pA==0||pB==0 ){
222c6 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 3d 3d  .    return pB==
222c7 70 41 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  pA;.  }.  assert
222c8 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ( !ExprHasAnyPro
222c9 70 65 72 74 79 28 70 41 2c 20 45 50 5f 54 6f 6b  perty(pA, EP_Tok
222ca 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65  enOnly|EP_Reduce
222cb 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  d) );.  assert( 
222cc 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
222cd 72 74 79 28 70 42 2c 20 45 50 5f 54 6f 6b 65 6e  rty(pB, EP_Token
222ce 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29  Only|EP_Reduced)
222cf 20 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61   );.  if( ExprHa
222d0 73 50 72 6f 70 65 72 74 79 28 70 41 2c 20 45 50  sProperty(pA, EP
222d1 5f 78 49 73 53 65 6c 65 63 74 29 20 7c 7c 20 45  _xIsSelect) || E
222d2 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
222d3 42 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  B, EP_xIsSelect)
222d4 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
222d5 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 41 2d  ;.  }.  if( (pA-
222d6 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74  >flags & EP_Dist
222d7 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61 67  inct)!=(pB->flag
222d8 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29  s & EP_Distinct)
222d9 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
222da 66 28 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e 6f  f( pA->op!=pB->o
222db 70 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  p ) return 0;.  
222dc 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 72  if( !sqlite3Expr
222dd 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 66  Compare(pA->pLef
222de 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 20  t, pB->pLeft) ) 
222df 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
222e0 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70  !sqlite3ExprComp
222e1 61 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c 20  are(pA->pRight, 
222e2 70 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65  pB->pRight) ) re
222e3 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70  turn 0;..  if( p
222e4 41 2d 3e 78 2e 70 4c 69 73 74 20 26 26 20 70 42  A->x.pList && pB
222e5 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20  ->x.pList ){.   
222e6 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74   if( pA->x.pList
222e7 2d 3e 6e 45 78 70 72 21 3d 70 42 2d 3e 78 2e 70  ->nExpr!=pB->x.p
222e8 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 20 72 65  List->nExpr ) re
222e9 74 75 72 6e 20 30 3b 0a 20 20 20 20 66 6f 72 28  turn 0;.    for(
222ea 69 3d 30 3b 20 69 3c 70 41 2d 3e 78 2e 70 4c 69  i=0; i<pA->x.pLi
222eb 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
222ec 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78  .      Expr *pEx
222ed 70 72 41 20 3d 20 70 41 2d 3e 78 2e 70 4c 69 73  prA = pA->x.pLis
222ee 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  t->a[i].pExpr;. 
222ef 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
222f0 42 20 3d 20 70 42 2d 3e 78 2e 70 4c 69 73 74 2d  B = pB->x.pList-
222f1 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
222f2 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45     if( !sqlite3E
222f3 78 70 72 43 6f 6d 70 61 72 65 28 70 45 78 70 72  xprCompare(pExpr
222f4 41 2c 20 70 45 78 70 72 42 29 20 29 20 72 65 74  A, pExprB) ) ret
222f5 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
222f6 65 6c 73 65 20 69 66 28 20 70 41 2d 3e 78 2e 70  else if( pA->x.p
222f7 4c 69 73 74 20 7c 7c 20 70 42 2d 3e 78 2e 70 4c  List || pB->x.pL
222f8 69 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ist ){.    retur
222f9 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  n 0;.  }..  if( 
222fa 70 41 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d 3e  pA->iTable!=pB->
222fb 69 54 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 43  iTable || pA->iC
222fc 6f 6c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75  olumn!=pB->iColu
222fd 6d 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  mn ) return 0;. 
222fe 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
222ff 65 72 74 79 28 70 41 2c 20 45 50 5f 49 6e 74 56  erty(pA, EP_IntV
22300 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 69 66 28  alue) ){.    if(
22301 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
22302 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c 75  y(pB, EP_IntValu
22303 65 29 20 7c 7c 20 70 41 2d 3e 75 2e 69 56 61 6c  e) || pA->u.iVal
22304 75 65 21 3d 70 42 2d 3e 75 2e 69 56 61 6c 75 65  ue!=pB->u.iValue
22305 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
22306 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   0;.    }.  }els
22307 65 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54 4b  e if( pA->op!=TK
22308 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e 75  _COLUMN && pA->u
22309 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69  .zToken ){.    i
2230a 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
2230b 74 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c  ty(pB, EP_IntVal
2230c 75 65 29 20 7c 7c 20 4e 45 56 45 52 28 70 42 2d  ue) || NEVER(pB-
2230d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 29 20 29 20  >u.zToken==0) ) 
2230e 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66  return 0;.    if
2230f 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
22310 28 70 41 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 70 42  (pA->u.zToken,pB
22311 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 21 3d 30 20 29  ->u.zToken)!=0 )
22312 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
22313 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
22314 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn 1;.}.../*.*
22315 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d  * Add a new elem
22316 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49  ent to the pAggI
22317 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61  nfo->aCol[] arra
22318 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69  y.  Return the i
22319 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e  ndex of.** the n
2231a 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74  ew element.  Ret
2231b 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e  urn a negative n
2231c 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20  umber if malloc 
2231d 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  fails..*/.static
2231e 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43   int addAggInfoC
2231f 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64  olumn(sqlite3 *d
22320 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66  b, AggInfo *pInf
22321 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70  o){.  int i;.  p
22322 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c  Info->aCol = sql
22323 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74  ite3ArrayAllocat
22324 65 28 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20  e(.       db,.  
22325 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c       pInfo->aCol
22326 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28  ,.       sizeof(
22327 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c  pInfo->aCol[0]),
22328 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20  .       3,.     
22329 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d    &pInfo->nColum
2232a 6e 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f  n,.       &pInfo
2232b 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a  ->nColumnAlloc,.
2232c 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20         &i.  );. 
2232d 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20   return i;.}    
2232e 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
2232f 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
22330 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63   pAggInfo->aFunc
22331 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72  [] array.  Retur
22332 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a  n the index of.*
22333 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e  * the new elemen
22334 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67  t.  Return a neg
22335 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20  ative number if 
22336 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f  malloc fails..*/
22337 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41  .static int addA
22338 67 67 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74  ggInfoFunc(sqlit
22339 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20  e3 *db, AggInfo 
2233a 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69  *pInfo){.  int i
2233b 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63  ;.  pInfo->aFunc
2233c 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41   = sqlite3ArrayA
2233d 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20  llocate(.       
2233e 64 62 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66  db, .       pInf
2233f 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20  o->aFunc,.      
22340 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61   sizeof(pInfo->a
22341 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20  Func[0]),.      
22342 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66   3,.       &pInf
22343 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20  o->nFunc,.      
22344 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c   &pInfo->nFuncAl
22345 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20  loc,.       &i. 
22346 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a   );.  return i;.
22347 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  }    ../*.** Thi
22348 73 20 69 73 20 74 68 65 20 78 45 78 70 72 43 61  s is the xExprCa
22349 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65  llback for a tre
2234a 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73  e walker.  It is
2234b 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c   used to.** impl
2234c 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70  ement sqlite3Exp
2234d 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
2234e 65 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74  es().  See sqlit
2234f 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
22350 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61  regates.** for a
22351 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
22352 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
22353 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72   int analyzeAggr
22354 65 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57  egate(Walker *pW
22355 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
22356 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  pr){.  int i;.  
22357 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
22358 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e   = pWalker->u.pN
22359 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  C;.  Parse *pPar
2235a 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
2235b 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  ;.  SrcList *pSr
2235c 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  cList = pNC->pSr
2235d 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f  cList;.  AggInfo
2235e 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43   *pAggInfo = pNC
2235f 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73  ->pAggInfo;..  s
22360 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
22361 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
22362 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20  AGG_COLUMN:.    
22363 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20  case TK_COLUMN: 
22364 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
22365 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
22366 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  AGG_COLUMN );.  
22367 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
22368 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
22369 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43  MN );.      /* C
2236a 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
2236b 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20  he column is in 
2236c 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  one of the table
2236d 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20  s in the FROM.  
2236e 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66      ** clause of
2236f 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71   the aggregate q
22370 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66  uery */.      if
22371 28 20 41 4c 57 41 59 53 28 70 53 72 63 4c 69 73  ( ALWAYS(pSrcLis
22372 74 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20  t!=0) ){.       
22373 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
22374 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53  item *pItem = pS
22375 72 63 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20  rcList->a;.     
22376 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53     for(i=0; i<pS
22377 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  rcList->nSrc; i+
22378 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
22379 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67         struct Ag
2237a 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b  gInfo_col *pCol;
2237b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
2237c 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  t( !ExprHasAnyPr
2237d 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2237e 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65  _TokenOnly|EP_Re
2237f 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20 20  duced) );.      
22380 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69      if( pExpr->i
22381 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43  Table==pItem->iC
22382 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20  ursor ){.       
22383 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65       /* If we re
22384 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20  ach this point, 
22385 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 70 45  it means that pE
22386 78 70 72 20 72 65 66 65 72 73 20 74 6f 20 61 20  xpr refers to a 
22387 74 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20  table.          
22388 20 20 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 20    ** that is in 
22389 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2238a 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  of the aggregate
2238b 20 71 75 65 72 79 2e 20 20 0a 20 20 20 20 20 20   query.  .      
2238c 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
2238d 20 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e 20       ** Make an 
2238e 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 63 6f  entry for the co
2238f 6c 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 6f  lumn in pAggInfo
22390 2d 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 72  ->aCol[] if ther
22391 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  e.            **
22392 20 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 79   is not an entry
22393 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a   there already..
22394 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
22395 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b             int k
22396 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43  ;.            pC
22397 6f 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61  ol = pAggInfo->a
22398 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Col;.           
22399 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 67   for(k=0; k<pAgg
2239a 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b  Info->nColumn; k
2239b 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ++, pCol++){.   
2239c 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
2239d 43 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78  Col->iTable==pEx
2239e 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20  pr->iTable &&.  
2239f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223a0 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70  pCol->iColumn==p
223a1 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b  Expr->iColumn ){
223a2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
223a3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
223a4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
223a5 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
223a6 20 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 49 6e    if( (k>=pAggIn
223a7 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20  fo->nColumn).   
223a8 20 20 20 20 20 20 20 20 20 20 26 26 20 28 6b 20            && (k 
223a9 3d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75  = addAggInfoColu
223aa 6d 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  mn(pParse->db, p
223ab 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20 20  AggInfo))>=0 .  
223ac 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20            ){.   
223ad 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20             pCol 
223ae 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  = &pAggInfo->aCo
223af 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  l[k];.          
223b0 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20 3d      pCol->pTab =
223b1 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20   pExpr->pTab;.  
223b2 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
223b3 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 78 70 72  ->iTable = pExpr
223b4 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
223b5 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43          pCol->iC
223b6 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69  olumn = pExpr->i
223b7 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20  Column;.        
223b8 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d        pCol->iMem
223b9 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
223ba 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m;.             
223bb 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
223bc 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20  lumn = -1;.     
223bd 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70           pCol->p
223be 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20  Expr = pExpr;.  
223bf 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
223c0 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70  pAggInfo->pGroup
223c1 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  By ){.          
223c2 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a        int j, n;.
223c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223c4 45 78 70 72 4c 69 73 74 20 2a 70 47 42 20 3d 20  ExprList *pGB = 
223c5 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70  pAggInfo->pGroup
223c6 42 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  By;.            
223c7 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
223c8 69 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 20  ist_item *pTerm 
223c9 3d 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20 20  = pGB->a;.      
223ca 20 20 20 20 20 20 20 20 20 20 6e 20 3d 20 70 47            n = pG
223cb 42 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  B->nExpr;.      
223cc 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
223cd 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65  0; j<n; j++, pTe
223ce 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  rm++){.         
223cf 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70           Expr *p
223d0 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  E = pTerm->pExpr
223d1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
223d2 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d      if( pE->op==
223d3 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 2d  TK_COLUMN && pE-
223d4 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e  >iTable==pExpr->
223d5 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20  iTable &&.      
223d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223d7 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78  pE->iColumn==pEx
223d8 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  pr->iColumn ){. 
223d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223da 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72     pCol->iSorter
223db 43 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20  Column = j;.    
223dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223dd 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
223de 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
223df 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
223e0 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
223e1 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
223e2 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
223e3 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  mn<0 ){.        
223e4 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53          pCol->iS
223e5 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70 41  orterColumn = pA
223e6 67 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67  ggInfo->nSorting
223e7 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20  Column++;.      
223e8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
223e9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
223ea 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20      /* There is 
223eb 6e 6f 77 20 61 6e 20 65 6e 74 72 79 20 66 6f 72  now an entry for
223ec 20 70 45 78 70 72 20 69 6e 20 70 41 67 67 49 6e   pExpr in pAggIn
223ed 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 68  fo->aCol[] (eith
223ee 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  er.            *
223ef 2a 20 62 65 63 61 75 73 65 20 69 74 20 77 61 73  * because it was
223f0 20 74 68 65 72 65 20 62 65 66 6f 72 65 20 6f 72   there before or
223f1 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74   because we just
223f2 20 63 72 65 61 74 65 64 20 69 74 29 2e 0a 20 20   created it)..  
223f3 20 20 20 20 20 20 20 20 20 20 2a 2a 20 43 6f 6e            ** Con
223f4 76 65 72 74 20 74 68 65 20 70 45 78 70 72 20 74  vert the pExpr t
223f5 6f 20 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 4f  o be a TK_AGG_CO
223f6 4c 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 74  LUMN referring t
223f7 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 20  o that.         
223f8 20 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d 3e     ** pAggInfo->
223f9 61 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20 20  aCol[] entry..  
223fa 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
223fb 20 20 20 20 20 20 20 20 20 45 78 70 72 53 65 74           ExprSet
223fc 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 70  Irreducible(pExp
223fd 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  r);.            
223fe 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20  pExpr->pAggInfo 
223ff 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20  = pAggInfo;.    
22400 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f          pExpr->o
22401 70 20 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  p = TK_AGG_COLUM
22402 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  N;.            p
22403 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 28 69 31  Expr->iAgg = (i1
22404 36 29 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  6)k;.           
22405 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
22406 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78    } /* endif pEx
22407 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65  pr->iTable==pIte
22408 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20  m->iCursor */.  
22409 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c        } /* end l
2240a 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73  oop over pSrcLis
2240b 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  t */.      }.   
2240c 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
2240d 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  une;.    }.    c
2240e 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  ase TK_AGG_FUNCT
2240f 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  ION: {.      /* 
22410 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d  The pNC->nDepth=
22411 3d 30 20 74 65 73 74 20 63 61 75 73 65 73 20 61  =0 test causes a
22412 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
22413 6e 73 20 69 6e 20 73 75 62 71 75 65 72 69 65 73  ns in subqueries
22414 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20  .      ** to be 
22415 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20  ignored */.     
22416 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68   if( pNC->nDepth
22417 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
22418 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
22419 66 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70  f pExpr is a dup
2241a 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65  licate of anothe
2241b 72 20 61 67 67 72 65 67 61 74 65 20 0a 20 20 20  r aggregate .   
2241c 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e       ** function
2241d 20 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79   that is already
2241e 20 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f   in the pAggInfo
2241f 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 20   structure.     
22420 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74     */.        st
22421 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
22422 63 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49  c *pItem = pAggI
22423 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20  nfo->aFunc;.    
22424 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
22425 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20  AggInfo->nFunc; 
22426 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
22427 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
22428 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
22429 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45  pItem->pExpr, pE
2242a 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  xpr) ){.        
2242b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2242c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2242d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d  .        if( i>=
2242e 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20  pAggInfo->nFunc 
2242f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
22430 70 45 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61  pExpr is origina
22431 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65  l.  Make a new e
22432 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f  ntry in pAggInfo
22433 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20  ->aFunc[].      
22434 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
22435 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50   u8 enc = ENC(pP
22436 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20  arse->db);.     
22437 20 20 20 20 20 69 20 3d 20 61 64 64 41 67 67 49       i = addAggI
22438 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e  nfoFunc(pParse->
22439 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20  db, pAggInfo);. 
2243a 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d           if( i>=
2243b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
2243c 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2243d 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
2243e 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
2243f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49  ;.            pI
22440 74 65 6d 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d  tem = &pAggInfo-
22441 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20 20 20 20  >aFunc[i];.     
22442 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45         pItem->pE
22443 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20  xpr = pExpr;.   
22444 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
22445 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iMem = ++pParse-
22446 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20  >nMem;.         
22447 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
22448 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
22449 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  r, EP_IntValue) 
2244a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
2244b 49 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20 73 71  Item->pFunc = sq
2244c 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
2244d 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20 20  n(pParse->db,.  
2244e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2244f 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
22450 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
22451 30 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  0(pExpr->u.zToke
22452 6e 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n),.            
22453 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 78 2e         pExpr->x.
22454 70 4c 69 73 74 20 3f 20 70 45 78 70 72 2d 3e 78  pList ? pExpr->x
22455 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20  .pList->nExpr : 
22456 30 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20  0, enc, 0);.    
22457 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70          if( pExp
22458 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69  r->flags & EP_Di
22459 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 20 20  stinct ){.      
2245a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69          pItem->i
2245b 44 69 73 74 69 6e 63 74 20 3d 20 70 50 61 72 73  Distinct = pPars
2245c 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20  e->nTab++;.     
2245d 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
2245e 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65              pIte
2245f 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d  m->iDistinct = -
22460 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  1;.            }
22461 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
22462 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f       }.        /
22463 2a 20 4d 61 6b 65 20 70 45 78 70 72 20 70 6f 69  * Make pExpr poi
22464 6e 74 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  nt to the approp
22465 72 69 61 74 65 20 70 41 67 67 49 6e 66 6f 2d 3e  riate pAggInfo->
22466 61 46 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20 20  aFunc[] entry.  
22467 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
22468 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
22469 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78  sAnyProperty(pEx
2246a 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  pr, EP_TokenOnly
2246b 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a  |EP_Reduced) );.
2246c 20 20 20 20 20 20 20 20 45 78 70 72 53 65 74 49          ExprSetI
2246d 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 70 72  rreducible(pExpr
2246e 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  );.        pExpr
2246f 2d 3e 69 41 67 67 20 3d 20 28 69 31 36 29 69 3b  ->iAgg = (i16)i;
22470 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
22471 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49  pAggInfo = pAggI
22472 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74  nfo;.        ret
22473 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
22474 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
22475 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f  .  return WRC_Co
22476 6e 74 69 6e 75 65 3b 0a 7d 0a 73 74 61 74 69 63  ntinue;.}.static
22477 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72   int analyzeAggr
22478 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74 28 57  egatesInSelect(W
22479 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20  alker *pWalker, 
2247a 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29  Select *pSelect)
2247b 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  {.  NameContext 
2247c 2a 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e  *pNC = pWalker->
2247d 75 2e 70 4e 43 3b 0a 20 20 69 66 28 20 70 4e 43  u.pNC;.  if( pNC
2247e 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a 20  ->nDepth==0 ){. 
2247f 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b     pNC->nDepth++
22480 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c  ;.    sqlite3Wal
22481 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c  kSelect(pWalker,
22482 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 70   pSelect);.    p
22483 4e 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a 20 20  NC->nDepth--;.  
22484 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
22485 6e 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ne;.  }else{.   
22486 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
22487 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  inue;.  }.}../*.
22488 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 67  ** Analyze the g
22489 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  iven expression 
2248a 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 67 67 72  looking for aggr
2248b 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
2248c 61 6e 64 0a 2a 2a 20 66 6f 72 20 76 61 72 69 61  and.** for varia
2248d 62 6c 65 73 20 74 68 61 74 20 6e 65 65 64 20 74  bles that need t
2248e 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68  o be added to th
2248f 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d  e pParse->aAgg[]
22490 20 61 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b 65 20   array..** Make 
22491 61 64 64 69 74 69 6f 6e 61 6c 20 65 6e 74 72 69  additional entri
22492 65 73 20 74 6f 20 74 68 65 20 70 50 61 72 73 65  es to the pParse
22493 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 20 61  ->aAgg[] array a
22494 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a  s necessary..**.
22495 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
22496 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63  should only be c
22497 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20  alled after the 
22498 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 62  expression has b
22499 65 65 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20  een.** analyzed 
2249a 62 79 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  by sqlite3Resolv
2249b 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2f  eExprNames()..*/
2249c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2249d 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
2249e 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
2249f 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70  s(NameContext *p
224a0 4e 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  NC, Expr *pExpr)
224a1 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20  {.  Walker w;.  
224a2 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20  w.xExprCallback 
224a3 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  = analyzeAggrega
224a4 74 65 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43  te;.  w.xSelectC
224a5 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a  allback = analyz
224a6 65 41 67 67 72 65 67 61 74 65 73 49 6e 53 65 6c  eAggregatesInSel
224a7 65 63 74 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d  ect;.  w.u.pNC =
224a8 20 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20   pNC;.  assert( 
224a9 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 21 3d 30  pNC->pSrcList!=0
224aa 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c   );.  sqlite3Wal
224ab 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29  kExpr(&w, pExpr)
224ac 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
224ad 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
224ae 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 20 66  zeAggregates() f
224af 6f 72 20 65 76 65 72 79 20 65 78 70 72 65 73 73  or every express
224b0 69 6f 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65 78 70  ion in an.** exp
224b1 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 52  ression list.  R
224b2 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
224b3 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a   of errors..**.*
224b4 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73  * If an error is
224b5 20 66 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61 6c   found, the anal
224b6 79 73 69 73 20 69 73 20 63 75 74 20 73 68 6f 72  ysis is cut shor
224b7 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
224b8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
224b9 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
224ba 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ist(NameContext 
224bb 2a 70 4e 43 2c 20 45 78 70 72 4c 69 73 74 20 2a  *pNC, ExprList *
224bc 70 4c 69 73 74 29 7b 0a 20 20 73 74 72 75 63 74  pList){.  struct
224bd 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
224be 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a  pItem;.  int i;.
224bf 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20    if( pList ){. 
224c0 20 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69     for(pItem=pLi
224c1 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c  st->a, i=0; i<pL
224c2 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  ist->nExpr; i++,
224c3 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
224c4 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
224c5 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 70 4e  yzeAggregates(pN
224c6 43 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  C, pItem->pExpr)
224c7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
224c8 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
224c9 73 69 6e 67 6c 65 20 6e 65 77 20 72 65 67 69 73  single new regis
224ca 74 65 72 20 66 6f 72 20 75 73 65 20 74 6f 20 68  ter for use to h
224cb 6f 6c 64 20 73 6f 6d 65 20 69 6e 74 65 72 6d 65  old some interme
224cc 64 69 61 74 65 20 72 65 73 75 6c 74 2e 0a 2a 2f  diate result..*/
224cd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
224ce 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65  int sqlite3GetTe
224cf 6d 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 61  mpReg(Parse *pPa
224d0 72 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72  rse){.  if( pPar
224d1 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20  se->nTempReg==0 
224d2 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b  ){.    return ++
224d3 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
224d4 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73  }.  return pPars
224d5 65 2d 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70 50  e->aTempReg[--pP
224d6 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d 3b  arse->nTempReg];
224d7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f  .}../*.** Deallo
224d8 63 61 74 65 20 61 20 72 65 67 69 73 74 65 72 2c  cate a register,
224d9 20 6d 61 6b 69 6e 67 20 61 76 61 69 6c 61 62 6c   making availabl
224da 65 20 66 6f 72 20 72 65 75 73 65 20 66 6f 72 20  e for reuse for 
224db 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 75  some other.** pu
224dc 72 70 6f 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rpose..**.** If 
224dd 61 20 72 65 67 69 73 74 65 72 20 69 73 20 63 75  a register is cu
224de 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 75 73  rrently being us
224df 65 64 20 62 79 20 74 68 65 20 63 6f 6c 75 6d 6e  ed by the column
224e0 20 63 61 63 68 65 2c 20 74 68 65 6e 0a 2a 2a 20   cache, then.** 
224e1 74 68 65 20 64 61 6c 6c 6f 63 61 74 69 6f 6e 20  the dallocation 
224e2 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69  is deferred unti
224e3 6c 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63  l the column cac
224e4 68 65 20 6c 69 6e 65 20 74 68 61 74 20 75 73 65  he line that use
224e5 73 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65  s.** the registe
224e6 72 20 62 65 63 6f 6d 65 73 20 73 74 61 6c 65 2e  r becomes stale.
224e7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
224e8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
224e9 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61  eleaseTempReg(Pa
224ea 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
224eb 20 69 52 65 67 29 7b 0a 20 20 69 66 28 20 69 52   iReg){.  if( iR
224ec 65 67 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54  eg && pParse->nT
224ed 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65  empReg<ArraySize
224ee 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65  (pParse->aTempRe
224ef 67 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  g) ){.    int i;
224f0 0a 20 20 20 20 73 74 72 75 63 74 20 79 43 6f 6c  .    struct yCol
224f1 43 61 63 68 65 20 2a 70 3b 0a 20 20 20 20 66 6f  Cache *p;.    fo
224f2 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d  r(i=0, p=pParse-
224f3 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51  >aColCache; i<SQ
224f4 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b  LITE_N_COLCACHE;
224f5 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20   i++, p++){.    
224f6 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69    if( p->iReg==i
224f7 52 65 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70  Reg ){.        p
224f8 2d 3e 74 65 6d 70 52 65 67 20 3d 20 31 3b 0a 20  ->tempReg = 1;. 
224f9 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
224fa 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
224fb 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65   pParse->aTempRe
224fc 67 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52  g[pParse->nTempR
224fd 65 67 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20  eg++] = iReg;.  
224fe 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  }.}../*.** Alloc
224ff 61 74 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74  ate or deallocat
22500 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65  e a block of nRe
22501 67 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65  g consecutive re
22502 67 69 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54  gisters.*/.SQLIT
22503 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
22504 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
22505 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
22506 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e   int nReg){.  in
22507 74 20 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50  t i, n;.  i = pP
22508 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b  arse->iRangeReg;
22509 0a 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e  .  n = pParse->n
2250a 52 61 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20  RangeReg;.  if( 
2250b 6e 52 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64  nReg<=n && !used
2250c 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50  AsColumnCache(pP
2250d 61 72 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20  arse, i, i+n-1) 
2250e 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69  ){.    pParse->i
2250f 52 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67  RangeReg += nReg
22510 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52  ;.    pParse->nR
22511 61 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b  angeReg -= nReg;
22512 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20  .  }else{.    i 
22513 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31  = pParse->nMem+1
22514 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
22515 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a  em += nReg;.  }.
22516 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51    return i;.}.SQ
22517 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22518 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65  d sqlite3Release
22519 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20  TempRange(Parse 
2251a 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65  *pParse, int iRe
2251b 67 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20  g, int nReg){.  
2251c 69 66 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d  if( nReg>pParse-
2251d 3e 6e 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20  >nRangeReg ){.  
2251e 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65    pParse->nRange
2251f 52 65 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20  Reg = nReg;.    
22520 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65  pParse->iRangeRe
22521 67 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a  g = iReg;.  }.}.
22522 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22523 20 45 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a   End of expr.c *
22524 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22525 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22526 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
22527 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22528 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65   Begin file alte
22529 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
2252a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2252b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2252c 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72  ./*.** 2005 Febr
2252d 75 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  uary 15.**.** Th
2252e 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2252f 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
22530 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
22531 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
22532 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
22533 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
22534 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
22535 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
22536 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
22537 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
22538 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
22539 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2253a 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2253b 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2253c 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2253d 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2253e 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2253f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22541 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22542 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22543 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
22544 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
22545 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ode routines tha
22546 74 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61  t used to genera
22547 74 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20  te VDBE code.** 
22548 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
22549 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20  the ALTER TABLE 
2254a 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 0a 2f 2a 0a  command..*/../*.
2254b 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
2254c 68 69 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78  his file only ex
2254d 69 73 74 73 20 69 66 20 77 65 20 61 72 65 20 6e  ists if we are n
2254e 6f 74 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a  ot omitting the.
2254f 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 6c  ** ALTER TABLE l
22550 6f 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62 75  ogic from the bu
22551 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ild..*/.#ifndef 
22552 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45  SQLITE_OMIT_ALTE
22553 52 54 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54  RTABLE.../*.** T
22554 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
22555 75 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65  used by SQL gene
22556 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65  rated to impleme
22557 6e 74 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45 52  nt the .** ALTER
22558 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20   TABLE command. 
22559 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
2255a 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20 6f  nt is the text o
2255b 66 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  f a CREATE TABLE
2255c 20 6f 72 0a 2a 2a 20 43 52 45 41 54 45 20 49 4e   or.** CREATE IN
2255d 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65  DEX command. The
2255e 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62   second is a tab
2255f 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62  le name. The tab
22560 6c 65 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 74  le name in .** t
22561 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
22562 6f 72 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  or CREATE INDEX 
22563 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70  statement is rep
22564 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20 74  laced with the t
22565 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  hird.** argument
22566 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20   and the result 
22567 72 65 74 75 72 6e 65 64 2e 20 45 78 61 6d 70 6c  returned. Exampl
22568 65 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  es:.**.** sqlite
22569 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43  _rename_table('C
2256a 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63 28  REATE TABLE abc(
2256b 61 2c 20 62 2c 20 63 29 27 2c 20 27 64 65 66 27  a, b, c)', 'def'
2256c 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45  ).**     -> 'CRE
2256d 41 54 45 20 54 41 42 4c 45 20 64 65 66 28 61 2c  ATE TABLE def(a,
2256e 20 62 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71   b, c)'.**.** sq
2256f 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  lite_rename_tabl
22570 65 28 27 43 52 45 41 54 45 20 49 4e 44 45 58 20  e('CREATE INDEX 
22571 69 20 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 64  i ON abc(a)', 'd
22572 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27  ef').**     -> '
22573 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f  CREATE INDEX i O
22574 4e 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a  N def(a, b, c)'.
22575 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
22576 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 0a  enameTableFunc(.
22577 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
22578 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
22579 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
2257a 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
2257b 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  v.){.  unsigned 
2257c 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c  char const *zSql
2257d 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2257e 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
2257f 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
22580 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d  const *zTableNam
22581 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
22582 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
22583 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20  ..  int token;. 
22584 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20   Token tname;.  
22585 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
22586 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c  nst *zCsr = zSql
22587 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b  ;.  int len = 0;
22588 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 0a  .  char *zRet;..
22589 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2258a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
2258b 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
2258c 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41  t);..  UNUSED_PA
2258d 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
2258e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e  ;..  /* The prin
2258f 63 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f  ciple used to lo
22590 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e  cate the table n
22591 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54  ame in the CREAT
22592 45 20 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73 74  E TABLE .  ** st
22593 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20  atement is that 
22594 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  the table name i
22595 73 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d  s the first non-
22596 73 70 61 63 65 20 74 6f 6b 65 6e 20 74 68 61 74  space token that
22597 0a 20 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69 61  .  ** is immedia
22598 74 65 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79  tely followed by
22599 20 61 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f 55   a TK_LP or TK_U
2259a 53 49 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2f  SING token..  */
2259b 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20  .  if( zSql ){. 
2259c 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 66     do {.      if
2259d 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20  ( !*zCsr ){.    
2259e 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f      /* Ran out o
2259f 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66  f input before f
225a0 69 6e 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 6e  inding an openin
225a1 67 20 62 72 61 63 6b 65 74 2e 20 52 65 74 75 72  g bracket. Retur
225a2 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20  n NULL. */.     
225a3 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
225a4 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f   }..      /* Sto
225a5 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61  re the token tha
225a6 74 20 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f  t zCsr points to
225a7 20 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20   in tname. */.  
225a8 20 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 28 63      tname.z = (c
225a9 68 61 72 2a 29 7a 43 73 72 3b 0a 20 20 20 20 20  har*)zCsr;.     
225aa 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a   tname.n = len;.
225ab 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63  .      /* Advanc
225ac 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65  e zCsr to the ne
225ad 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20  xt token. Store 
225ae 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20  that token type 
225af 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20  in 'token',.    
225b0 20 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e    ** and its len
225b1 67 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f  gth in 'len' (to
225b2 20 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74   be used next it
225b3 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  eration of this 
225b4 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  loop)..      */.
225b5 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20        do {.     
225b6 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a     zCsr += len;.
225b7 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71          len = sq
225b8 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43  lite3GetToken(zC
225b9 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20  sr, &token);.   
225ba 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65     } while( toke
225bb 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20  n==TK_SPACE );. 
225bc 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e       assert( len
225bd 3e 30 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c  >0 );.    } whil
225be 65 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20  e( token!=TK_LP 
225bf 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49  && token!=TK_USI
225c0 4e 47 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20  NG );..    zRet 
225c1 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
225c2 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22  (db, "%.*s\"%w\"
225c3 25 73 22 2c 20 28 28 75 38 2a 29 74 6e 61 6d 65  %s", ((u8*)tname
225c4 2e 7a 29 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c  .z) - zSql, zSql
225c5 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65  , .       zTable
225c6 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e  Name, tname.z+tn
225c7 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69  ame.n);.    sqli
225c8 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
225c9 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d  context, zRet, -
225ca 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  1, SQLITE_DYNAMI
225cb 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  C);.  }.}../*.**
225cc 20 54 68 69 73 20 43 20 66 75 6e 63 74 69 6f 6e   This C function
225cd 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 53   implements an S
225ce 51 4c 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  QL user function
225cf 20 74 68 61 74 20 69 73 20 75 73 65 64 20 62 79   that is used by
225d0 20 53 51 4c 20 63 6f 64 65 0a 2a 2a 20 67 65 6e   SQL code.** gen
225d1 65 72 61 74 65 64 20 62 79 20 74 68 65 20 41 4c  erated by the AL
225d2 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 52 45  TER TABLE ... RE
225d3 4e 41 4d 45 20 63 6f 6d 6d 61 6e 64 20 74 6f 20  NAME command to 
225d4 6d 6f 64 69 66 79 20 74 68 65 20 64 65 66 69 6e  modify the defin
225d5 69 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 6e 79 20  ition.** of any 
225d6 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
225d7 74 72 61 69 6e 74 73 20 74 68 61 74 20 75 73 65  traints that use
225d8 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
225d9 20 72 65 6e 61 6d 65 64 20 61 73 20 74 68 65 20   renamed as the 
225da 0a 2a 2a 20 70 61 72 65 6e 74 20 74 61 62 6c 65  .** parent table
225db 2e 20 49 74 20 69 73 20 70 61 73 73 65 64 20 74  . It is passed t
225dc 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 0a  hree arguments:.
225dd 2a 2a 0a 2a 2a 20 20 20 31 29 20 54 68 65 20 63  **.**   1) The c
225de 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20  omplete text of 
225df 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
225e0 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
225e1 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20   modified,.**   
225e2 32 29 20 54 68 65 20 6f 6c 64 20 6e 61 6d 65 20  2) The old name 
225e3 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  of the table bei
225e4 6e 67 20 72 65 6e 61 6d 65 64 2c 20 61 6e 64 0a  ng renamed, and.
225e5 2a 2a 20 20 20 33 29 20 54 68 65 20 6e 65 77 20  **   3) The new 
225e6 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
225e7 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 2e  e being renamed.
225e8 0a 2a 2a 0a 2a 2a 20 49 74 20 72 65 74 75 72 6e  .**.** It return
225e9 73 20 74 68 65 20 6e 65 77 20 43 52 45 41 54 45  s the new CREATE
225ea 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
225eb 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a  . For example:.*
225ec 2a 0a 2a 2a 20 20 20 73 71 6c 69 74 65 5f 72 65  *.**   sqlite_re
225ed 6e 61 6d 65 5f 70 61 72 65 6e 74 28 27 43 52 45  name_parent('CRE
225ee 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 20 52  ATE TABLE t1(a R
225ef 45 46 45 52 45 4e 43 45 53 20 74 32 29 27 2c 20  EFERENCES t2)', 
225f0 27 74 32 27 2c 20 27 74 33 27 29 0a 2a 2a 20 20  't2', 't3').**  
225f1 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20       -> 'CREATE 
225f2 54 41 42 4c 45 20 74 31 28 61 20 52 45 46 45 52  TABLE t1(a REFER
225f3 45 4e 43 45 53 20 74 33 29 27 0a 2a 2f 0a 23 69  ENCES t3)'.*/.#i
225f4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
225f5 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 73 74  T_FOREIGN_KEY.st
225f6 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65  atic void rename
225f7 50 61 72 65 6e 74 46 75 6e 63 28 0a 20 20 73 71  ParentFunc(.  sq
225f8 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
225f9 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
225fa 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
225fb 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
225fc 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
225fd 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
225fe 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
225ff 78 74 29 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75  xt);.  char *zOu
22600 74 70 75 74 20 3d 20 30 3b 0a 20 20 63 68 61 72  tput = 0;.  char
22601 20 2a 7a 52 65 73 75 6c 74 3b 0a 20 20 75 6e 73   *zResult;.  uns
22602 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
22603 20 2a 7a 49 6e 70 75 74 20 3d 20 73 71 6c 69 74   *zInput = sqlit
22604 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
22605 67 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e  gv[0]);.  unsign
22606 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  ed char const *z
22607 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Old = sqlite3_va
22608 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
22609 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  );.  unsigned ch
2260a 61 72 20 63 6f 6e 73 74 20 2a 7a 4e 65 77 20 3d  ar const *zNew =
2260b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2260c 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 0a 20  ext(argv[2]);.. 
2260d 20 75 6e 73 69 67 6e 65 64 20 63 6f 6e 73 74 20   unsigned const 
2260e 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
2260f 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
22610 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b  oken */.  int n;
22611 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22612 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
22613 67 74 68 20 6f 66 20 74 6f 6b 65 6e 20 7a 20 2a  gth of token z *
22614 2f 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 20 20  /.  int token;  
22615 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22616 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74      /* Type of t
22617 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 55 4e 55 53 45  oken */..  UNUSE
22618 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
22619 73 65 64 29 3b 0a 20 20 66 6f 72 28 7a 3d 7a 49  sed);.  for(z=zI
2261a 6e 70 75 74 3b 20 2a 7a 3b 20 7a 3d 7a 2b 6e 29  nput; *z; z=z+n)
2261b 7b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65  {.    n = sqlite
2261c 33 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 74 6f  3GetToken(z, &to
2261d 6b 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 74 6f  ken);.    if( to
2261e 6b 65 6e 3d 3d 54 4b 5f 52 45 46 45 52 45 4e 43  ken==TK_REFERENC
2261f 45 53 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72  ES ){.      char
22620 20 2a 7a 50 61 72 65 6e 74 3b 0a 20 20 20 20 20   *zParent;.     
22621 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 20   do {.        z 
22622 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 6e 20  += n;.        n 
22623 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65  = sqlite3GetToke
22624 6e 28 7a 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20  n(z, &token);.  
22625 20 20 20 20 7d 77 68 69 6c 65 28 20 74 6f 6b 65      }while( toke
22626 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 0a  n==TK_SPACE );..
22627 20 20 20 20 20 20 7a 50 61 72 65 6e 74 20 3d 20        zParent = 
22628 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
22629 28 64 62 2c 20 28 63 6f 6e 73 74 20 63 68 61 72  (db, (const char
2262a 20 2a 29 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20   *)z, n);.      
2262b 69 66 28 20 7a 50 61 72 65 6e 74 3d 3d 30 20 29  if( zParent==0 )
2262c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 73 71   break;.      sq
2262d 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 50 61  lite3Dequote(zPa
2262e 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28  rent);.      if(
2262f 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
22630 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mp((const char *
22631 29 7a 4f 6c 64 2c 20 7a 50 61 72 65 6e 74 29 20  )zOld, zParent) 
22632 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
22633 2a 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 4d  *zOut = sqlite3M
22634 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 25 2e  Printf(db, "%s%.
22635 2a 73 5c 22 25 77 5c 22 22 2c 20 0a 20 20 20 20  *s\"%w\"", .    
22636 20 20 20 20 20 20 20 20 28 7a 4f 75 74 70 75 74          (zOutput
22637 3f 7a 4f 75 74 70 75 74 3a 22 22 29 2c 20 7a 2d  ?zOutput:""), z-
22638 7a 49 6e 70 75 74 2c 20 7a 49 6e 70 75 74 2c 20  zInput, zInput, 
22639 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a 4e  (const char *)zN
2263a 65 77 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20  ew.        );.  
2263b 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2263c 72 65 65 28 64 62 2c 20 7a 4f 75 74 70 75 74 29  ree(db, zOutput)
2263d 3b 0a 20 20 20 20 20 20 20 20 7a 4f 75 74 70 75  ;.        zOutpu
2263e 74 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 20 20 20  t = zOut;.      
2263f 20 20 7a 49 6e 70 75 74 20 3d 20 26 7a 5b 6e 5d    zInput = &z[n]
22640 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
22641 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22642 2c 20 7a 50 61 72 65 6e 74 29 3b 0a 20 20 20 20  , zParent);.    
22643 7d 0a 20 20 7d 0a 0a 20 20 7a 52 65 73 75 6c 74  }.  }..  zResult
22644 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
22645 66 28 64 62 2c 20 22 25 73 25 73 22 2c 20 28 7a  f(db, "%s%s", (z
22646 4f 75 74 70 75 74 3f 7a 4f 75 74 70 75 74 3a 22  Output?zOutput:"
22647 22 29 2c 20 7a 49 6e 70 75 74 29 2c 20 0a 20 20  "), zInput), .  
22648 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
22649 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65  ext(context, zRe
2264a 73 75 6c 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45  sult, -1, SQLITE
2264b 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 73 71 6c  _DYNAMIC);.  sql
2264c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2264d 4f 75 74 70 75 74 29 3b 0a 7d 0a 23 65 6e 64 69  Output);.}.#endi
2264e 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
2264f 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f  E_OMIT_TRIGGER./
22650 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
22651 69 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 67  is used by SQL g
22652 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c  enerated to impl
22653 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c 54  ement the.** ALT
22654 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64  ER TABLE command
22655 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  . The first argu
22656 6d 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74  ment is the text
22657 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49   of a CREATE TRI
22658 47 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d 65  GGER .** stateme
22659 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69  nt. The second i
2265a 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  s a table name. 
2265b 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  The table name i
2265c 6e 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a 2a  n the CREATE .**
2265d 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
2265e 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77  nt is replaced w
2265f 69 74 68 20 74 68 65 20 74 68 69 72 64 20 61 72  ith the third ar
22660 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72  gument and the r
22661 65 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 6e  esult .** return
22662 65 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61 6c  ed. This is anal
22663 61 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 54  agous to renameT
22664 61 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 65  ableFunc() above
22665 2c 20 65 78 63 65 70 74 20 66 6f 72 20 43 52 45  , except for CRE
22666 41 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c 20  ATE.** TRIGGER, 
22667 6e 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45 58  not CREATE INDEX
22668 20 61 6e 64 20 43 52 45 41 54 45 20 54 41 42 4c   and CREATE TABL
22669 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  E..*/.static voi
2266a 64 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46  d renameTriggerF
2266b 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
2266c 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
2266d 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
2266e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2266f 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69  **argv.){.  unsi
22670 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
22671 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
22672 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
22673 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  0]);.  unsigned 
22674 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62  char const *zTab
22675 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  leName = sqlite3
22676 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
22677 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b  [1]);..  int tok
22678 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d  en;.  Token tnam
22679 65 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d 20  e;.  int dist = 
2267a 33 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  3;.  unsigned ch
2267b 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d  ar const *zCsr =
2267c 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e   zSql;.  int len
2267d 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52   = 0;.  char *zR
2267e 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  et;.  sqlite3 *d
2267f 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
22680 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
22681 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45  ntext);..  UNUSE
22682 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
22683 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  sed);..  /* The 
22684 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74  principle used t
22685 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62  o locate the tab
22686 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43  le name in the C
22687 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 20  REATE TRIGGER . 
22688 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73   ** statement is
22689 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20   that the table 
2268a 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73  name is the firs
2268b 74 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20  t token that is 
2268c 69 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 2a  immediatedly.  *
2268d 2a 20 70 72 65 63 65 64 65 64 20 62 79 20 65 69  * preceded by ei
2268e 74 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b  ther TK_ON or TK
2268f 5f 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 61  _DOT and immedia
22690 74 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62  tedly followed b
22691 79 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 4b  y one.  ** of TK
22692 5f 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20  _WHEN, TK_BEGIN 
22693 6f 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a  or TK_FOR..  */.
22694 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
22695 20 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 66    do {..      if
22696 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20  ( !*zCsr ){.    
22697 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f      /* Ran out o
22698 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66  f input before f
22699 69 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c 65  inding the table
2269a 20 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e 55   name. Return NU
2269b 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  LL. */.        r
2269c 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a  eturn;.      }..
2269d 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74        /* Store t
2269e 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43  he token that zC
2269f 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20  sr points to in 
226a0 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20  tname. */.      
226a1 74 6e 61 6d 65 2e 7a 20 3d 20 28 63 68 61 72 2a  tname.z = (char*
226a2 29 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61  )zCsr;.      tna
226a3 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20  me.n = len;..   
226a4 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43     /* Advance zC
226a5 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74  sr to the next t
226a6 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74  oken. Store that
226a7 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27   token type in '
226a8 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a  token',.      **
226a9 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20   and its length 
226aa 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20  in 'len' (to be 
226ab 75 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 74  used next iterat
226ac 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70  ion of this loop
226ad 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
226ae 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a    do {.        z
226af 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20  Csr += len;.    
226b0 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65      len = sqlite
226b1 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20  3GetToken(zCsr, 
226b2 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d  &token);.      }
226b3 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b  while( token==TK
226b4 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20  _SPACE );.      
226b5 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b  assert( len>0 );
226b6 0a 0a 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61  ..      /* Varia
226b7 62 6c 65 20 27 64 69 73 74 27 20 73 74 6f 72 65  ble 'dist' store
226b8 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
226b9 74 6f 6b 65 6e 73 20 72 65 61 64 20 73 69 6e 63  tokens read sinc
226ba 65 20 74 68 65 20 6d 6f 73 74 0a 20 20 20 20 20  e the most.     
226bb 20 2a 2a 20 72 65 63 65 6e 74 20 54 4b 5f 44 4f   ** recent TK_DO
226bc 54 20 6f 72 20 54 4b 5f 4f 4e 2e 20 54 68 69 73  T or TK_ON. This
226bd 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e   means that when
226be 20 61 20 57 48 45 4e 2c 20 46 4f 52 20 6f 72 20   a WHEN, FOR or 
226bf 42 45 47 49 4e 20 0a 20 20 20 20 20 20 2a 2a 20  BEGIN .      ** 
226c0 74 6f 6b 65 6e 20 69 73 20 72 65 61 64 20 61 6e  token is read an
226c1 64 20 27 64 69 73 74 27 20 65 71 75 61 6c 73 20  d 'dist' equals 
226c2 32 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  2, the condition
226c3 20 73 74 61 74 65 64 20 61 62 6f 76 65 0a 20 20   stated above.  
226c4 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6d 65 74      ** to be met
226c5 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
226c6 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 4f 4e   ** Note that ON
226c7 20 63 61 6e 6e 6f 74 20 62 65 20 61 20 64 61 74   cannot be a dat
226c8 61 62 61 73 65 2c 20 74 61 62 6c 65 20 6f 72 20  abase, table or 
226c9 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20 73 6f 0a  column name, so.
226ca 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20 69        ** there i
226cb 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 6f 72  s no need to wor
226cc 72 79 20 61 62 6f 75 74 20 73 79 6e 74 61 78 20  ry about syntax 
226cd 6c 69 6b 65 20 0a 20 20 20 20 20 20 2a 2a 20 22  like .      ** "
226ce 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2e  CREATE TRIGGER .
226cf 2e 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 42 45 47 49  .. ON ON.ON BEGI
226d0 4e 20 2e 2e 2e 22 20 65 74 63 2e 0a 20 20 20 20  N ..." etc..    
226d1 20 20 2a 2f 0a 20 20 20 20 20 20 64 69 73 74 2b    */.      dist+
226d2 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 74 6f 6b  +;.      if( tok
226d3 65 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74 6f  en==TK_DOT || to
226d4 6b 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0a 20 20  ken==TK_ON ){.  
226d5 20 20 20 20 20 20 64 69 73 74 20 3d 20 30 3b 0a        dist = 0;.
226d6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 77 68        }.    } wh
226d7 69 6c 65 28 20 64 69 73 74 21 3d 32 20 7c 7c 20  ile( dist!=2 || 
226d8 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45 4e 20  (token!=TK_WHEN 
226d9 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46 4f 52  && token!=TK_FOR
226da 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 42 45   && token!=TK_BE
226db 47 49 4e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  GIN) );..    /* 
226dc 56 61 72 69 61 62 6c 65 20 74 6e 61 6d 65 20 6e  Variable tname n
226dd 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  ow contains the 
226de 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 74 68  token that is th
226df 65 20 6f 6c 64 20 74 61 62 6c 65 2d 6e 61 6d 65  e old table-name
226e0 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 43  .    ** in the C
226e1 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74  REATE TRIGGER st
226e2 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a  atement..    */.
226e3 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74      zRet = sqlit
226e4 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
226e5 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 28 28  .*s\"%w\"%s", ((
226e6 75 38 2a 29 74 6e 61 6d 65 2e 7a 29 20 2d 20 7a  u8*)tname.z) - z
226e7 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20  Sql, zSql, .    
226e8 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74     zTableName, t
226e9 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b  name.z+tname.n);
226ea 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
226eb 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
226ec 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49  , zRet, -1, SQLI
226ed 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d  TE_DYNAMIC);.  }
226ee 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 21  .}.#endif   /* !
226ef 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
226f0 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  GER */../*.** Re
226f1 67 69 73 74 65 72 20 62 75 69 6c 74 2d 69 6e 20  gister built-in 
226f2 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
226f3 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74  o help implement
226f4 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2f 0a   ALTER TABLE.*/.
226f5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
226f6 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
226f7 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
226f8 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65  3 *db){.  sqlite
226f9 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
226fa 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74  "sqlite_rename_t
226fb 61 62 6c 65 22 2c 20 32 2c 20 53 51 4c 49 54 45  able", 2, SQLITE
226fc 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 20  _UTF8, 0,.      
226fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226fe 20 20 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75     renameTableFu
226ff 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64  nc, 0, 0);.#ifnd
22700 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
22701 52 49 47 47 45 52 0a 20 20 73 71 6c 69 74 65 33  RIGGER.  sqlite3
22702 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
22703 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72  sqlite_rename_tr
22704 69 67 67 65 72 22 2c 20 32 2c 20 53 51 4c 49 54  igger", 2, SQLIT
22705 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20  E_UTF8, 0,.     
22706 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22707 20 20 20 20 72 65 6e 61 6d 65 54 72 69 67 67 65      renameTrigge
22708 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 65  rFunc, 0, 0);.#e
22709 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
2270a 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
2270b 5f 4b 45 59 0a 20 20 73 71 6c 69 74 65 33 43 72  _KEY.  sqlite3Cr
2270c 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71  eateFunc(db, "sq
2270d 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 70 61 72 65  lite_rename_pare
2270e 6e 74 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55  nt", 3, SQLITE_U
2270f 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  TF8, 0,.        
22710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22711 20 72 65 6e 61 6d 65 50 61 72 65 6e 74 46 75 6e   renameParentFun
22712 63 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66  c, 0, 0);.#endif
22713 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
22714 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
22715 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 65  to create the te
22716 78 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  xt of expression
22717 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
22718 2a 0a 2a 2a 20 20 20 6e 61 6d 65 3d 3c 63 6f 6e  *.**   name=<con
22719 73 74 61 6e 74 31 3e 20 4f 52 20 6e 61 6d 65 3d  stant1> OR name=
2271a 3c 63 6f 6e 73 74 61 6e 74 32 3e 20 4f 52 20 2e  <constant2> OR .
2271b 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75  ...**.** If argu
2271c 6d 65 6e 74 20 7a 57 68 65 72 65 20 69 73 20 4e  ment zWhere is N
2271d 55 4c 4c 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e  ULL, then a poin
2271e 74 65 72 20 73 74 72 69 6e 67 20 63 6f 6e 74 61  ter string conta
2271f 69 6e 69 6e 67 20 74 68 65 20 74 65 78 74 20 0a  ining the text .
22720 2a 2a 20 22 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61  ** "name=<consta
22721 6e 74 3e 22 20 69 73 20 72 65 74 75 72 6e 65 64  nt>" is returned
22722 2c 20 77 68 65 72 65 20 3c 63 6f 6e 73 74 61 6e  , where <constan
22723 74 3e 20 69 73 20 74 68 65 20 71 75 6f 74 65 64  t> is the quoted
22724 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74   version.** of t
22725 68 65 20 73 74 72 69 6e 67 20 70 61 73 73 65 64  he string passed
22726 20 61 73 20 61 72 67 75 6d 65 6e 74 20 7a 43 6f   as argument zCo
22727 6e 73 74 61 6e 74 2e 20 54 68 65 20 72 65 74 75  nstant. The retu
22728 72 6e 65 64 20 62 75 66 66 65 72 20 69 73 0a 2a  rned buffer is.*
22729 2a 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e  * allocated usin
2272a 67 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  g sqlite3DbMallo
2272b 63 28 29 2e 20 49 74 20 69 73 20 74 68 65 20 72  c(). It is the r
2272c 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
2272d 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74   the.** caller t
2272e 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 69 74  o ensure that it
2272f 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66   is eventually f
22730 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  reed..**.** If a
22731 72 67 75 6d 65 6e 74 20 7a 57 68 65 72 65 20 69  rgument zWhere i
22732 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
22733 20 74 68 65 20 73 74 72 69 6e 67 20 72 65 74 75   the string retu
22734 72 6e 65 64 20 69 73 20 0a 2a 2a 20 22 3c 77 68  rned is .** "<wh
22735 65 72 65 3e 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f  ere> OR name=<co
22736 6e 73 74 61 6e 74 3e 22 2c 20 77 68 65 72 65 20  nstant>", where 
22737 3c 77 68 65 72 65 3e 20 69 73 20 74 68 65 20 63  <where> is the c
22738 6f 6e 74 65 6e 74 73 20 6f 66 20 7a 57 68 65 72  ontents of zWher
22739 65 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 63 61  e..** In this ca
2273a 73 65 20 7a 57 68 65 72 65 20 69 73 20 70 61 73  se zWhere is pas
2273b 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 44 62  sed to sqlite3Db
2273c 46 72 65 65 28 29 20 62 65 66 6f 72 65 20 72 65  Free() before re
2273d 74 75 72 6e 69 6e 67 2e 0a 2a 2a 20 0a 2a 2f 0a  turning..** .*/.
2273e 73 74 61 74 69 63 20 63 68 61 72 20 2a 77 68 65  static char *whe
2273f 72 65 4f 72 4e 61 6d 65 28 73 71 6c 69 74 65 33  reOrName(sqlite3
22740 20 2a 64 62 2c 20 63 68 61 72 20 2a 7a 57 68 65   *db, char *zWhe
22741 72 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 73 74  re, char *zConst
22742 61 6e 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  ant){.  char *zN
22743 65 77 3b 0a 20 20 69 66 28 20 21 7a 57 68 65 72  ew;.  if( !zWher
22744 65 20 29 7b 0a 20 20 20 20 7a 4e 65 77 20 3d 20  e ){.    zNew = 
22745 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
22746 62 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 7a 43  b, "name=%Q", zC
22747 6f 6e 73 74 61 6e 74 29 3b 0a 20 20 7d 65 6c 73  onstant);.  }els
22748 65 7b 0a 20 20 20 20 7a 4e 65 77 20 3d 20 73 71  e{.    zNew = sq
22749 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2274a 20 22 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22   "%s OR name=%Q"
2274b 2c 20 7a 57 68 65 72 65 2c 20 7a 43 6f 6e 73 74  , zWhere, zConst
2274c 61 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ant);.    sqlite
2274d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65  3DbFree(db, zWhe
2274e 72 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  re);.  }.  retur
2274f 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 23 69 66 20 21  n zNew;.}..#if !
22750 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
22751 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29  MIT_FOREIGN_KEY)
22752 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
22753 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
22754 29 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  )./*.** Generate
22755 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 57   the text of a W
22756 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20  HERE expression 
22757 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65  which can be use
22758 64 20 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a  d to select all.
22759 2a 2a 20 74 61 62 6c 65 73 20 74 68 61 74 20 68  ** tables that h
2275a 61 76 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ave foreign key 
2275b 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 61 74  constraints that
2275c 20 72 65 66 65 72 20 74 6f 20 74 61 62 6c 65 20   refer to table 
2275d 70 54 61 62 20 28 69 2e 65 2e 0a 2a 2a 20 63 6f  pTab (i.e..** co
2275e 6e 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68  nstraints for wh
2275f 69 63 68 20 70 54 61 62 20 69 73 20 74 68 65 20  ich pTab is the 
22760 70 61 72 65 6e 74 20 74 61 62 6c 65 29 20 66 72  parent table) fr
22761 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  om the sqlite_ma
22762 73 74 65 72 0a 2a 2a 20 74 61 62 6c 65 2e 0a 2a  ster.** table..*
22763 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 77  /.static char *w
22764 68 65 72 65 46 6f 72 65 69 67 6e 4b 65 79 73 28  hereForeignKeys(
22765 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
22766 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 46  able *pTab){.  F
22767 4b 65 79 20 2a 70 3b 0a 20 20 63 68 61 72 20 2a  Key *p;.  char *
22768 7a 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 66 6f  zWhere = 0;.  fo
22769 72 28 70 3d 73 71 6c 69 74 65 33 46 6b 52 65 66  r(p=sqlite3FkRef
2276a 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70  erences(pTab); p
2276b 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f 29 7b  ; p=p->pNextTo){
2276c 0a 20 20 20 20 7a 57 68 65 72 65 20 3d 20 77 68  .    zWhere = wh
2276d 65 72 65 4f 72 4e 61 6d 65 28 70 50 61 72 73 65  ereOrName(pParse
2276e 2d 3e 64 62 2c 20 7a 57 68 65 72 65 2c 20 70 2d  ->db, zWhere, p-
2276f 3e 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a  >pFrom->zName);.
22770 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 68    }.  return zWh
22771 65 72 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ere;.}.#endif../
22772 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68  *.** Generate th
22773 65 20 74 65 78 74 20 6f 66 20 61 20 57 48 45 52  e text of a WHER
22774 45 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69  E expression whi
22775 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ch can be used t
22776 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20  o select all.** 
22777 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65  temporary trigge
22778 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62  rs on table pTab
22779 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65   from the sqlite
2277a 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 74 61 62  _temp_master tab
2277b 6c 65 2e 20 49 66 0a 2a 2a 20 74 61 62 6c 65 20  le. If.** table 
2277c 70 54 61 62 20 68 61 73 20 6e 6f 20 74 65 6d 70  pTab has no temp
2277d 6f 72 61 72 79 20 74 72 69 67 67 65 72 73 2c 20  orary triggers, 
2277e 6f 72 20 69 73 20 69 74 73 65 6c 66 20 73 74 6f  or is itself sto
2277f 72 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20 74  red in the .** t
22780 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
22781 65 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  e, NULL is retur
22782 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ned..*/.static c
22783 68 61 72 20 2a 77 68 65 72 65 54 65 6d 70 54 72  har *whereTempTr
22784 69 67 67 65 72 73 28 50 61 72 73 65 20 2a 70 50  iggers(Parse *pP
22785 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
22786 62 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70  b){.  Trigger *p
22787 54 72 69 67 3b 0a 20 20 63 68 61 72 20 2a 7a 57  Trig;.  char *zW
22788 68 65 72 65 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  here = 0;.  cons
22789 74 20 53 63 68 65 6d 61 20 2a 70 54 65 6d 70 53  t Schema *pTempS
2278a 63 68 65 6d 61 20 3d 20 70 50 61 72 73 65 2d 3e  chema = pParse->
2278b 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
2278c 6d 61 3b 20 2f 2a 20 54 65 6d 70 20 64 62 20 73  ma; /* Temp db s
2278d 63 68 65 6d 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49  chema */..  /* I
2278e 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e  f the table is n
2278f 6f 74 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68  ot located in th
22790 65 20 74 65 6d 70 2d 64 62 20 28 69 6e 20 77 68  e temp-db (in wh
22791 69 63 68 20 63 61 73 65 20 4e 55 4c 4c 20 69 73  ich case NULL is
22792 20 0a 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c   .  ** returned,
22793 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   loop through th
22794 65 20 74 61 62 6c 65 73 20 6c 69 73 74 20 6f 66  e tables list of
22795 20 74 72 69 67 67 65 72 73 2e 20 46 6f 72 20 65   triggers. For e
22796 61 63 68 20 74 72 69 67 67 65 72 0a 20 20 2a 2a  ach trigger.  **
22797 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72   that is not par
22798 74 20 6f 66 20 74 68 65 20 74 65 6d 70 2d 64 62  t of the temp-db
22799 20 73 63 68 65 6d 61 2c 20 61 64 64 20 61 20 63   schema, add a c
2279a 6c 61 75 73 65 20 74 6f 20 74 68 65 20 57 48 45  lause to the WHE
2279b 52 45 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73  RE .  ** express
2279c 69 6f 6e 20 62 65 69 6e 67 20 62 75 69 6c 74 20  ion being built 
2279d 75 70 20 69 6e 20 7a 57 68 65 72 65 2e 0a 20 20  up in zWhere..  
2279e 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  */.  if( pTab->p
2279f 53 63 68 65 6d 61 21 3d 70 54 65 6d 70 53 63 68  Schema!=pTempSch
227a0 65 6d 61 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ema ){.    sqlit
227a1 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
227a2 3e 64 62 3b 0a 20 20 20 20 66 6f 72 28 70 54 72  >db;.    for(pTr
227a3 69 67 3d 73 71 6c 69 74 65 33 54 72 69 67 67 65  ig=sqlite3Trigge
227a4 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54  rList(pParse, pT
227a5 61 62 29 3b 20 70 54 72 69 67 3b 20 70 54 72 69  ab); pTrig; pTri
227a6 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b  g=pTrig->pNext){
227a7 0a 20 20 20 20 20 20 69 66 28 20 70 54 72 69 67  .      if( pTrig
227a8 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 65 6d 70  ->pSchema==pTemp
227a9 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20  Schema ){.      
227aa 20 20 7a 57 68 65 72 65 20 3d 20 77 68 65 72 65    zWhere = where
227ab 4f 72 4e 61 6d 65 28 64 62 2c 20 7a 57 68 65 72  OrName(db, zWher
227ac 65 2c 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 29  e, pTrig->zName)
227ad 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
227ae 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 68    }.  return zWh
227af 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ere;.}../*.** Ge
227b0 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64  nerate code to d
227b1 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74  rop and reload t
227b2 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72  he internal repr
227b3 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 61  esentation of ta
227b4 62 6c 65 0a 2a 2a 20 70 54 61 62 20 66 72 6f 6d  ble.** pTab from
227b5 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 69   the database, i
227b6 6e 63 6c 75 64 69 6e 67 20 74 72 69 67 67 65 72  ncluding trigger
227b7 73 20 61 6e 64 20 74 65 6d 70 6f 72 61 72 79 20  s and temporary 
227b8 74 72 69 67 67 65 72 73 2e 0a 2a 2a 20 41 72 67  triggers..** Arg
227b9 75 6d 65 6e 74 20 7a 4e 61 6d 65 20 69 73 20 74  ument zName is t
227ba 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
227bb 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61  able in the data
227bc 62 61 73 65 20 73 63 68 65 6d 61 20 61 74 0a 2a  base schema at.*
227bd 2a 20 74 68 65 20 74 69 6d 65 20 74 68 65 20 67  * the time the g
227be 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 69 73  enerated code is
227bf 20 65 78 65 63 75 74 65 64 2e 20 54 68 69 73 20   executed. This 
227c0 63 61 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74  can be different
227c1 20 66 72 6f 6d 0a 2a 2a 20 70 54 61 62 2d 3e 7a   from.** pTab->z
227c2 4e 61 6d 65 20 69 66 20 74 68 69 73 20 66 75 6e  Name if this fun
227c3 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63  ction is being c
227c4 61 6c 6c 65 64 20 74 6f 20 63 6f 64 65 20 70 61  alled to code pa
227c5 72 74 20 6f 66 20 61 6e 20 0a 2a 2a 20 22 41 4c  rt of an .** "AL
227c6 54 45 52 20 54 41 42 4c 45 20 52 45 4e 41 4d 45  TER TABLE RENAME
227c7 20 54 4f 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a   TO" statement..
227c8 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
227c9 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61  eloadTableSchema
227ca 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
227cb 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e  Table *pTab, con
227cc 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
227cd 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 63 68  .  Vdbe *v;.  ch
227ce 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 69 6e  ar *zWhere;.  in
227cf 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
227d0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
227d1 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f  x of database co
227d2 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f  ntaining pTab */
227d3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
227d4 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 54  OMIT_TRIGGER.  T
227d5 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 0a 23  rigger *pTrig;.#
227d6 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73 71 6c  endif..  v = sql
227d7 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
227d8 73 65 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  se);.  if( NEVER
227d9 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b  (v==0) ) return;
227da 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
227db 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
227dc 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
227dd 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  b) );.  iDb = sq
227de 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
227df 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
227e0 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
227e1 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
227e2 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
227e3 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
227e4 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79 20 74 61    /* Drop any ta
227e5 62 6c 65 20 74 72 69 67 67 65 72 73 20 66 72 6f  ble triggers fro
227e6 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  m the internal s
227e7 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 66 6f 72 28  chema. */.  for(
227e8 70 54 72 69 67 3d 73 71 6c 69 74 65 33 54 72 69  pTrig=sqlite3Tri
227e9 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ggerList(pParse,
227ea 20 70 54 61 62 29 3b 20 70 54 72 69 67 3b 20 70   pTab); pTrig; p
227eb 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78  Trig=pTrig->pNex
227ec 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 54 72 69  t){.    int iTri
227ed 67 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  gDb = sqlite3Sch
227ee 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
227ef 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53  e->db, pTrig->pS
227f0 63 68 65 6d 61 29 3b 0a 20 20 20 20 61 73 73 65  chema);.    asse
227f1 72 74 28 20 69 54 72 69 67 44 62 3d 3d 69 44 62  rt( iTrigDb==iDb
227f2 20 7c 7c 20 69 54 72 69 67 44 62 3d 3d 31 20 29   || iTrigDb==1 )
227f3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
227f4 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72  eAddOp4(v, OP_Dr
227f5 6f 70 54 72 69 67 67 65 72 2c 20 69 54 72 69 67  opTrigger, iTrig
227f6 44 62 2c 20 30 2c 20 30 2c 20 70 54 72 69 67 2d  Db, 0, 0, pTrig-
227f7 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a  >zName, 0);.  }.
227f8 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f  #endif..  /* Dro
227f9 70 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  p the table and 
227fa 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69  index from the i
227fb 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20  nternal schema. 
227fc 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
227fd 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72  eAddOp4(v, OP_Dr
227fe 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c  opTable, iDb, 0,
227ff 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   0, pTab->zName,
22800 20 30 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61   0);..  /* Reloa
22801 64 20 74 68 65 20 74 61 62 6c 65 2c 20 69 6e 64  d the table, ind
22802 65 78 20 61 6e 64 20 70 65 72 6d 61 6e 65 6e 74  ex and permanent
22803 20 74 72 69 67 67 65 72 20 73 63 68 65 6d 61 73   trigger schemas
22804 2e 20 2a 2f 0a 20 20 7a 57 68 65 72 65 20 3d 20  . */.  zWhere = 
22805 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70  sqlite3MPrintf(p
22806 50 61 72 73 65 2d 3e 64 62 2c 20 22 74 62 6c 5f  Parse->db, "tbl_
22807 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29  name=%Q", zName)
22808 3b 0a 20 20 69 66 28 20 21 7a 57 68 65 72 65 20  ;.  if( !zWhere 
22809 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
2280a 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2280b 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c   OP_ParseSchema,
2280c 20 69 44 62 2c 20 30 2c 20 30 2c 20 7a 57 68 65   iDb, 0, 0, zWhe
2280d 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  re, P4_DYNAMIC);
2280e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2280f 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
22810 2f 2a 20 4e 6f 77 2c 20 69 66 20 74 68 65 20 74  /* Now, if the t
22811 61 62 6c 65 20 69 73 20 6e 6f 74 20 73 74 6f 72  able is not stor
22812 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64  ed in the temp d
22813 61 74 61 62 61 73 65 2c 20 72 65 6c 6f 61 64 20  atabase, reload 
22814 61 6e 79 20 74 65 6d 70 20 0a 20 20 2a 2a 20 74  any temp .  ** t
22815 72 69 67 67 65 72 73 2e 20 44 6f 6e 27 74 20 75  riggers. Don't u
22816 73 65 20 49 4e 28 2e 2e 2e 29 20 69 6e 20 63 61  se IN(...) in ca
22817 73 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  se SQLITE_OMIT_S
22818 55 42 51 55 45 52 59 20 69 73 20 64 65 66 69 6e  UBQUERY is defin
22819 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ed. .  */.  if( 
2281a 28 7a 57 68 65 72 65 3d 77 68 65 72 65 54 65 6d  (zWhere=whereTem
2281b 70 54 72 69 67 67 65 72 73 28 70 50 61 72 73 65  pTriggers(pParse
2281c 2c 20 70 54 61 62 29 29 21 3d 30 20 29 7b 0a 20  , pTab))!=0 ){. 
2281d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2281e 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65  dOp4(v, OP_Parse
2281f 53 63 68 65 6d 61 2c 20 31 2c 20 30 2c 20 30 2c  Schema, 1, 0, 0,
22820 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41   zWhere, P4_DYNA
22821 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  MIC);.  }.#endif
22822 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
22823 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65  te code to imple
22824 6d 65 6e 74 20 74 68 65 20 22 41 4c 54 45 52 20  ment the "ALTER 
22825 54 41 42 4c 45 20 78 78 78 20 52 45 4e 41 4d 45  TABLE xxx RENAME
22826 20 54 4f 20 79 79 79 22 20 0a 2a 2a 20 63 6f 6d   TO yyy" .** com
22827 6d 61 6e 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45  mand. .*/.SQLITE
22828 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22829 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65  lite3AlterRename
2282a 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
2282b 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2282c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
2282d 74 65 78 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69  text. */.  SrcLi
2282e 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20  st *pSrc,       
2282f 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
22830 65 20 74 6f 20 72 65 6e 61 6d 65 2e 20 2a 2f 0a  e to rename. */.
22831 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20    Token *pName  
22832 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
22833 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d  he new table nam
22834 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  e. */.){.  int i
22835 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
22836 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
22837 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
22838 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68  he table */.  ch
22839 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20  ar *zDb;        
2283a 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2283b 6f 66 20 64 61 74 61 62 61 73 65 20 69 44 62 20  of database iDb 
2283c 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2283d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2283e 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 72 65  * Table being re
2283f 6e 61 6d 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  named */.  char 
22840 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20  *zName = 0;     
22841 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74 65 72       /* NULL-ter
22842 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20  minated version 
22843 6f 66 20 70 4e 61 6d 65 20 2a 2f 20 0a 20 20 73  of pName */ .  s
22844 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
22845 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61  rse->db; /* Data
22846 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
22847 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 4e 61 6d  */.  int nTabNam
22848 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
22849 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 54 46 2d  * Number of UTF-
2284a 38 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  8 characters in 
2284b 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a 20 20 63 6f  zTabName */.  co
2284c 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61  nst char *zTabNa
2284d 6d 65 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69  me;     /* Origi
2284e 6e 61 6c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  nal name of the 
2284f 74 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20  table */.  Vdbe 
22850 2a 76 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  *v;.#ifndef SQLI
22851 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
22852 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d    char *zWhere =
22853 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57   0;         /* W
22854 68 65 72 65 20 63 6c 61 75 73 65 20 74 6f 20 6c  here clause to l
22855 6f 63 61 74 65 20 74 65 6d 70 20 74 72 69 67 67  ocate temp trigg
22856 65 72 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ers */.#endif.  
22857 56 54 61 62 6c 65 20 2a 70 56 54 61 62 20 3d 20  VTable *pVTab = 
22858 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e  0;        /* Non
22859 2d 7a 65 72 6f 20 69 66 20 74 68 69 73 20 69 73  -zero if this is
2285a 20 61 20 76 2d 74 61 62 20 77 69 74 68 20 61 6e   a v-tab with an
2285b 20 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a 20 20   xRename() */.  
2285c 0a 20 20 69 66 28 20 4e 45 56 45 52 28 64 62 2d  .  if( NEVER(db-
2285d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29  >mallocFailed) )
2285e 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
2285f 65 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73 65 72  e_table;.  asser
22860 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31  t( pSrc->nSrc==1
22861 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
22862 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
22863 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65  llMutexes(pParse
22864 2d 3e 64 62 29 20 29 3b 0a 0a 20 20 70 54 61 62  ->db) );..  pTab
22865 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
22866 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
22867 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d   pSrc->a[0].zNam
22868 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44  e, pSrc->a[0].zD
22869 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20  atabase);.  if( 
2286a 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69  !pTab ) goto exi
2286b 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
2286c 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2286d 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
2286e 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
2286f 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d  Schema);.  zDb =
22870 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
22871 61 6d 65 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61  ame;..  /* Get a
22872 20 4e 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64   NULL terminated
22873 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
22874 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  new table name. 
22875 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  */.  zName = sql
22876 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
22877 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  n(db, pName);.  
22878 69 66 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74  if( !zName ) got
22879 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
2287a 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ble;..  /* Check
2287b 20 74 68 61 74 20 61 20 74 61 62 6c 65 20 6f 72   that a table or
2287c 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e   index named 'zN
2287d 61 6d 65 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c  ame' does not al
2287e 72 65 61 64 79 20 65 78 69 73 74 0a 20 20 2a 2a  ready exist.  **
2287f 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
22880 2e 20 49 66 20 73 6f 2c 20 74 68 69 73 20 69 73  . If so, this is
22881 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a   an error..  */.
22882 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
22883 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  dTable(db, zName
22884 2c 20 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74 65  , zDb) || sqlite
22885 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a  3FindIndex(db, z
22886 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20  Name, zDb) ){.  
22887 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
22888 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  g(pParse, .     
22889 20 20 20 22 74 68 65 72 65 20 69 73 20 61 6c 72     "there is alr
2288a 65 61 64 79 20 61 6e 6f 74 68 65 72 20 74 61 62  eady another tab
2288b 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 69 74 68  le or index with
2288c 20 74 68 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c   this name: %s",
2288d 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74   zName);.    got
2288e 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
2288f 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  ble;.  }..  /* M
22890 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6e  ake sure it is n
22891 6f 74 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c  ot a system tabl
22892 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c  e being altered,
22893 20 6f 72 20 61 20 72 65 73 65 72 76 65 64 20 6e   or a reserved n
22894 61 6d 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68  ame.  ** that th
22895 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67  e table is being
22896 20 72 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a   renamed to..  *
22897 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
22898 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e  trlen30(pTab->zN
22899 61 6d 65 29 3e 36 20 0a 20 20 20 26 26 20 30 3d  ame)>6 .   && 0=
2289a 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70  =sqlite3StrNICmp
2289b 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73  (pTab->zName, "s
2289c 71 6c 69 74 65 5f 22 2c 20 37 29 0a 20 20 29 7b  qlite_", 7).  ){
2289d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2289e 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
2289f 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62  ble %s may not b
228a0 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62  e altered", pTab
228a1 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
228a2 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
228a3 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  able;.  }.  if( 
228a4 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
228a5 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
228a6 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29  e(pParse, zName)
228a7 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
228a8 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
228a9 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
228aa 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
228ab 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
228ac 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
228ad 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
228ae 20 22 76 69 65 77 20 25 73 20 6d 61 79 20 6e 6f   "view %s may no
228af 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70  t be altered", p
228b0 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
228b1 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
228b2 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e  e_table;.  }.#en
228b3 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
228b4 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
228b5 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f  ZATION.  /* Invo
228b6 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61  ke the authoriza
228b7 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a  tion callback. *
228b8 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  /.  if( sqlite3A
228b9 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
228ba 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41   SQLITE_ALTER_TA
228bb 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e  BLE, zDb, pTab->
228bc 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20  zName, 0) ){.   
228bd 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
228be 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e  e_table;.  }.#en
228bf 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
228c0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
228c1 54 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69  TABLE.  if( sqli
228c2 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
228c3 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
228c4 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ab) ){.    goto 
228c5 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
228c6 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56  e;.  }.  if( IsV
228c7 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
228c8 20 20 20 20 70 56 54 61 62 20 3d 20 73 71 6c 69      pVTab = sqli
228c9 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
228ca 20 70 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20   pTab);.    if( 
228cb 70 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e 70 4d  pVTab->pVtab->pM
228cc 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 3d 3d  odule->xRename==
228cd 30 20 29 7b 0a 20 20 20 20 20 20 70 56 54 61 62  0 ){.      pVTab
228ce 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
228cf 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67  #endif..  /* Beg
228d0 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
228d1 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20 56 65   and code the Ve
228d2 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20 64  rifyCookie for d
228d3 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a 20 20  atabase iDb. .  
228d4 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 20 74  ** Then modify t
228d5 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
228d6 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c 54 45   (since the ALTE
228d7 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 65 73  R TABLE modifies
228d8 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61   the.  ** schema
228d9 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 65 6d  ). Open a statem
228da 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
228db 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
228dc 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74  a virtual.  ** t
228dd 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d  able..  */.  v =
228de 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
228df 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
228e0 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
228e1 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
228e2 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  e;.  }.  sqlite3
228e3 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
228e4 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 56 54 61  ion(pParse, pVTa
228e5 62 21 3d 30 2c 20 69 44 62 29 3b 0a 20 20 73 71  b!=0, iDb);.  sq
228e6 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
228e7 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
228e8 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
228e9 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
228ea 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52 65  , invoke the xRe
228eb 6e 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  name() function 
228ec 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20 64  if.  ** one is d
228ed 65 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65 6e  efined. The xRen
228ee 61 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 77  ame() callback w
228ef 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 6e  ill modify the n
228f0 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e 79  ames.  ** of any
228f1 20 72 65 73 6f 75 72 63 65 73 20 75 73 65 64 20   resources used 
228f2 62 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20 69  by the v-table i
228f3 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 69  mplementation (i
228f4 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a 20  ncluding other. 
228f5 20 2a 2a 20 53 51 4c 69 74 65 20 74 61 62 6c 65   ** SQLite table
228f6 73 29 20 74 68 61 74 20 61 72 65 20 69 64 65 6e  s) that are iden
228f7 74 69 66 69 65 64 20 62 79 20 74 68 65 20 6e 61  tified by the na
228f8 6d 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  me of the virtua
228f9 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69  l table..  */.#i
228fa 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
228fb 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
228fc 20 69 66 28 20 70 56 54 61 62 20 29 7b 0a 20 20   if( pVTab ){.  
228fd 20 20 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61 72    int i = ++pPar
228fe 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
228ff 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
22900 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
22901 2c 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 30  , i, 0, zName, 0
22902 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22903 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
22904 52 65 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30 2c  Rename, i, 0, 0,
22905 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 56 54  (const char*)pVT
22906 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20  ab, P4_VTAB);.  
22907 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72    sqlite3MayAbor
22908 74 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 23  t(pParse);.  }.#
22909 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 66 69 67 75  endif..  /* figu
2290a 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
2290b 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73  UTF-8 characters
2290c 20 61 72 65 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f   are in zName */
2290d 0a 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54  .  zTabName = pT
2290e 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 6e 54 61  ab->zName;.  nTa
2290f 62 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 55  bName = sqlite3U
22910 74 66 38 43 68 61 72 4c 65 6e 28 7a 54 61 62 4e  tf8CharLen(zTabN
22911 61 6d 65 2c 20 2d 31 29 3b 0a 0a 23 69 66 20 21  ame, -1);..#if !
22912 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
22913 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29  MIT_FOREIGN_KEY)
22914 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
22915 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
22916 29 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67  ).  if( db->flag
22917 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e  s&SQLITE_Foreign
22918 4b 65 79 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Keys ){.    /* I
22919 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 73 75  f foreign-key su
2291a 70 70 6f 72 74 20 69 73 20 65 6e 61 62 6c 65 64  pport is enabled
2291b 2c 20 72 65 77 72 69 74 65 20 74 68 65 20 43 52  , rewrite the CR
2291c 45 41 54 45 20 54 41 42 4c 45 20 0a 20 20 20 20  EATE TABLE .    
2291d 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 63 6f  ** statements co
2291e 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 61  rresponding to a
2291f 6c 6c 20 63 68 69 6c 64 20 74 61 62 6c 65 73 20  ll child tables 
22920 6f 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  of foreign key c
22921 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 20 20 2a  onstraints.    *
22922 2a 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  * for which the 
22923 72 65 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73  renamed table is
22924 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
22925 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28  e.  */.    if( (
22926 7a 57 68 65 72 65 3d 77 68 65 72 65 46 6f 72 65  zWhere=whereFore
22927 69 67 6e 4b 65 79 73 28 70 50 61 72 73 65 2c 20  ignKeys(pParse, 
22928 70 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20  pTab))!=0 ){.   
22929 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
2292a 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20  Parse(pParse, . 
2292b 20 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45           "UPDATE
2292c 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 53   sqlite_master S
2292d 45 54 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  ET ".           
2292e 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65     "sql = sqlite
2292f 5f 72 65 6e 61 6d 65 5f 70 61 72 65 6e 74 28 73  _rename_parent(s
22930 71 6c 2c 20 25 51 2c 20 25 51 29 20 22 0a 20 20  ql, %Q, %Q) ".  
22931 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45              "WHE
22932 52 45 20 25 73 3b 22 2c 20 7a 54 61 62 4e 61 6d  RE %s;", zTabNam
22933 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65  e, zName, zWhere
22934 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22935 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72  DbFree(db, zWher
22936 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  e);.    }.  }.#e
22937 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66  ndif..  /* Modif
22938 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  y the sqlite_mas
22939 74 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73 65  ter table to use
2293a 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
2293b 61 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ame. */.  sqlite
2293c 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
2293d 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41  rse,.      "UPDA
2293e 54 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a 23  TE %Q.%s SET ".#
2293f 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
22940 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20  T_TRIGGER.      
22941 20 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74      "sql = sqlit
22942 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73  e_rename_table(s
22943 71 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 65  ql, %Q), ".#else
22944 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20  .          "sql 
22945 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20  = CASE ".       
22946 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 20       "WHEN type 
22947 3d 20 27 74 72 69 67 67 65 72 27 20 54 48 45 4e  = 'trigger' THEN
22948 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
22949 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 22  rigger(sql, %Q)"
2294a 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c  .            "EL
2294b 53 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  SE sqlite_rename
2294c 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 20  _table(sql, %Q) 
2294d 45 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 20  END, ".#endif.  
2294e 20 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d          "tbl_nam
2294f 65 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 20  e = %Q, ".      
22950 20 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45      "name = CASE
22951 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
22952 57 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65  WHEN type='table
22953 27 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20 20  ' THEN %Q ".    
22954 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e 61          "WHEN na
22955 6d 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f  me LIKE 'sqlite_
22956 61 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e 44  autoindex%%' AND
22957 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54 48   type='index' TH
22958 45 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  EN ".           
22959 20 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f 69    "'sqlite_autoi
2295a 6e 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c 20  ndex_' || %Q || 
2295b 73 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b 31  substr(name,%d+1
2295c 38 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  8) ".           
2295d 20 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20   "ELSE name END 
2295e 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74  ".      "WHERE t
2295f 62 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22  bl_name=%Q AND "
22960 0a 20 20 20 20 20 20 20 20 20 20 22 28 74 79 70  .          "(typ
22961 65 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79 70  e='table' OR typ
22962 65 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79 70  e='index' OR typ
22963 65 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c 20  e='trigger');", 
22964 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45  .      zDb, SCHE
22965 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a  MA_TABLE(iDb), z
22966 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61  Name, zName, zNa
22967 6d 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 4c  me, .#ifndef SQL
22968 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
22969 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65  .      zName,.#e
2296a 6e 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65  ndif.      zName
2296b 2c 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62  , nTabName, zTab
2296c 4e 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64  Name.  );..#ifnd
2296d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2296e 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f  UTOINCREMENT.  /
2296f 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f  * If the sqlite_
22970 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65  sequence table e
22971 78 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 61  xists in this da
22972 74 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64  tabase, then upd
22973 61 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74  ate .  ** it wit
22974 68 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  h the new table 
22975 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  name..  */.  if(
22976 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
22977 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65  e(db, "sqlite_se
22978 71 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b  quence", zDb) ){
22979 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
2297a 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
2297b 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20          "UPDATE 
2297c 5c 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65  \"%w\".sqlite_se
2297d 71 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20  quence set name 
2297e 3d 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20  = %Q WHERE name 
2297f 3d 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a  = %Q",.        z
22980 44 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  Db, zName, pTab-
22981 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e  >zName);.  }.#en
22982 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
22983 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
22984 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
22985 72 65 20 54 45 4d 50 20 74 72 69 67 67 65 72 73  re TEMP triggers
22986 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20   on this table, 
22987 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74  modify the sqlit
22988 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 20  e_temp_master.  
22989 2a 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 20  ** table. Don't 
2298a 64 6f 20 74 68 69 73 20 69 66 20 74 68 65 20 74  do this if the t
2298b 61 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 52  able being ALTER
2298c 65 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f 63  ed is itself loc
2298d 61 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 65  ated in.  ** the
2298e 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 0a   temp database..
2298f 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65    */.  if( (zWhe
22990 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67  re=whereTempTrig
22991 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61  gers(pParse, pTa
22992 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  b))!=0 ){.    sq
22993 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
22994 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
22995 20 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65    "UPDATE sqlite
22996 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54  _temp_master SET
22997 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
22998 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e  sql = sqlite_ren
22999 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c  ame_trigger(sql,
2299a 20 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20 20   %Q), ".        
2299b 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20      "tbl_name = 
2299c 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  %Q ".           
2299d 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a 4e   "WHERE %s;", zN
2299e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65  ame, zName, zWhe
2299f 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  re);.    sqlite3
229a0 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72  DbFree(db, zWher
229a1 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  e);.  }.#endif..
229a2 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
229a3 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
229a4 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65  _KEY) && !define
229a5 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
229a6 49 47 47 45 52 29 0a 20 20 69 66 28 20 64 62 2d  IGGER).  if( db-
229a7 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f  >flags&SQLITE_Fo
229a8 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20 20  reignKeys ){.   
229a9 20 46 4b 65 79 20 2a 70 3b 0a 20 20 20 20 66 6f   FKey *p;.    fo
229aa 72 28 70 3d 73 71 6c 69 74 65 33 46 6b 52 65 66  r(p=sqlite3FkRef
229ab 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70  erences(pTab); p
229ac 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f 29 7b  ; p=p->pNextTo){
229ad 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 46  .      Table *pF
229ae 72 6f 6d 20 3d 20 70 2d 3e 70 46 72 6f 6d 3b 0a  rom = p->pFrom;.
229af 20 20 20 20 20 20 69 66 28 20 70 46 72 6f 6d 21        if( pFrom!
229b0 3d 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20  =pTab ){.       
229b1 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65   reloadTableSche
229b2 6d 61 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 46  ma(pParse, p->pF
229b3 72 6f 6d 2c 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d  rom, pFrom->zNam
229b4 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
229b5 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
229b6 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 65 6c 6f  /* Drop and relo
229b7 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ad the internal 
229b8 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 20 2a 2f  table schema. */
229b9 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63  .  reloadTableSc
229ba 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61  hema(pParse, pTa
229bb 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78 69 74  b, zName);..exit
229bc 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a 0a 20  _rename_table:. 
229bd 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
229be 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b  elete(db, pSrc);
229bf 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
229c0 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a  (db, zName);.}..
229c1 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
229c2 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 73 75 72  code to make sur
229c3 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61  e the file forma
229c4 74 20 6e 75 6d 62 65 72 20 69 73 20 61 74 20 6c  t number is at l
229c5 65 61 73 74 20 6d 69 6e 46 6f 72 6d 61 74 2e 0a  east minFormat..
229c6 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65 64  ** The generated
229c7 20 63 6f 64 65 20 77 69 6c 6c 20 69 6e 63 72 65   code will incre
229c8 61 73 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72  ase the file for
229c9 6d 61 74 20 6e 75 6d 62 65 72 20 69 66 20 6e 65  mat number if ne
229ca 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49  cessary..*/.SQLI
229cb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
229cc 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69  sqlite3MinimumFi
229cd 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65 20 2a  leFormat(Parse *
229ce 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 2c  pParse, int iDb,
229cf 20 69 6e 74 20 6d 69 6e 46 6f 72 6d 61 74 29 7b   int minFormat){
229d0 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 76 20  .  Vdbe *v;.  v 
229d1 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
229d2 28 70 50 61 72 73 65 29 3b 0a 20 20 2f 2a 20 54  (pParse);.  /* T
229d3 68 65 20 56 44 42 45 20 73 68 6f 75 6c 64 20 68  he VDBE should h
229d4 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ave been allocat
229d5 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ed before this r
229d6 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
229d7 2e 0a 20 20 2a 2a 20 49 66 20 74 68 61 74 20 61  ..  ** If that a
229d8 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65 64  llocation failed
229d9 2c 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20  , we would have 
229da 71 75 69 74 20 62 65 66 6f 72 65 20 72 65 61 63  quit before reac
229db 68 69 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70  hing this.  ** p
229dc 6f 69 6e 74 20 2a 2f 0a 20 20 69 66 28 20 41 4c  oint */.  if( AL
229dd 57 41 59 53 28 76 29 20 29 7b 0a 20 20 20 20 69  WAYS(v) ){.    i
229de 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  nt r1 = sqlite3G
229df 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
229e0 29 3b 0a 20 20 20 20 69 6e 74 20 72 32 20 3d 20  );.    int r2 = 
229e1 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
229e2 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  g(pParse);.    i
229e3 6e 74 20 6a 31 3b 0a 20 20 20 20 73 71 6c 69 74  nt j1;.    sqlit
229e4 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
229e5 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69  OP_ReadCookie, i
229e6 44 62 2c 20 72 31 2c 20 42 54 52 45 45 5f 46 49  Db, r1, BTREE_FI
229e7 4c 45 5f 46 4f 52 4d 41 54 29 3b 0a 20 20 20 20  LE_FORMAT);.    
229e8 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
229e9 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20  tree(v, iDb);.  
229ea 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
229eb 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
229ec 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20 72 32  r, minFormat, r2
229ed 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69  );.    j1 = sqli
229ee 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
229ef 20 4f 50 5f 47 65 2c 20 72 32 2c 20 30 2c 20 72   OP_Ge, r2, 0, r
229f0 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
229f1 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
229f2 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  SetCookie, iDb, 
229f3 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41  BTREE_FILE_FORMA
229f4 54 2c 20 72 32 29 3b 0a 20 20 20 20 73 71 6c 69  T, r2);.    sqli
229f5 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
229f6 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69  v, j1);.    sqli
229f7 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
229f8 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
229f9 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
229fa 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
229fb 20 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   r2);.  }.}../*.
229fc 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
229fd 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
229fe 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45   an "ALTER TABLE
229ff 20 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d   ... ADD" statem
22a00 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ent.** has been 
22a01 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74  parsed. Argument
22a02 20 70 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69 6e   pColDef contain
22a03 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68  s the text of th
22a04 65 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  e new.** column 
22a05 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a  definition..**.*
22a06 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75  * The Table stru
22a07 63 74 75 72 65 20 70 50 61 72 73 65 2d 3e 70 4e  cture pParse->pN
22a08 65 77 54 61 62 6c 65 20 77 61 73 20 65 78 74 65  ewTable was exte
22a09 6e 64 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 0a  nded to include.
22a0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d  ** the new colum
22a0b 6e 20 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67  n during parsing
22a0c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22a0d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22a0e 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f  AlterFinishAddCo
22a0f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72  lumn(Parse *pPar
22a10 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44  se, Token *pColD
22a11 65 66 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e  ef){.  Table *pN
22a12 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ew;             
22a13 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50 61 72   /* Copy of pPar
22a14 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f  se->pNewTable */
22a15 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
22a16 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22a17 54 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65  Table being alte
22a18 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  red */.  int iDb
22a19 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22a1a 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
22a1b 75 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  umber */.  const
22a1c 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20   char *zDb;     
22a1d 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
22a1e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   name */.  const
22a1f 20 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20   char *zTab;    
22a20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61       /* Table na
22a21 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  me */.  char *zC
22a22 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
22a23 20 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e    /* Null-termin
22a24 61 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69  ated column defi
22a25 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75  nition */.  Colu
22a26 6d 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20  mn *pCol;       
22a27 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77        /* The new
22a28 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70   column */.  Exp
22a29 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 20 20  r *pDflt;       
22a2a 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
22a2b 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
22a2c 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  new column */.  
22a2d 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
22a2e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22a2f 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
22a30 74 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d  tion; */..  db =
22a31 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
22a32 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
22a33 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
22a34 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  led ) return;.  
22a35 70 4e 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70  pNew = pParse->p
22a36 4e 65 77 54 61 62 6c 65 3b 0a 20 20 61 73 73 65  NewTable;.  asse
22a37 72 74 28 20 70 4e 65 77 20 29 3b 0a 0a 20 20 61  rt( pNew );..  a
22a38 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
22a39 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
22a3a 65 73 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20  es(db) );.  iDb 
22a3b 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
22a3c 6f 49 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d  oIndex(db, pNew-
22a3d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62  >pSchema);.  zDb
22a3e 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
22a3f 7a 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20  zName;.  zTab = 
22a40 26 70 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d  &pNew->zName[16]
22a41 3b 20 20 2f 2a 20 53 6b 69 70 20 74 68 65 20 22  ;  /* Skip the "
22a42 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f  sqlite_altertab_
22a43 22 20 70 72 65 66 69 78 20 6f 6e 20 74 68 65 20  " prefix on the 
22a44 6e 61 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c 20 3d  name */.  pCol =
22a45 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65   &pNew->aCol[pNe
22a46 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 70 44  w->nCol-1];.  pD
22a47 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c  flt = pCol->pDfl
22a48 74 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  t;.  pTab = sqli
22a49 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
22a4a 20 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20 20 61   zTab, zDb);.  a
22a4b 73 73 65 72 74 28 20 70 54 61 62 20 29 3b 0a 0a  ssert( pTab );..
22a4c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22a4d 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
22a4e 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  N.  /* Invoke th
22a4f 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
22a50 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69  callback. */.  i
22a51 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
22a52 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
22a53 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20  TE_ALTER_TABLE, 
22a54 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  zDb, pTab->zName
22a55 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 0) ){.    retu
22a56 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  rn;.  }.#endif..
22a57 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61    /* If the defa
22a58 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
22a59 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77 61 73  e new column was
22a5a 20 73 70 65 63 69 66 69 65 64 20 77 69 74 68 20   specified with 
22a5b 61 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61 6c 20  a .  ** literal 
22a5c 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 70  NULL, then set p
22a5d 44 66 6c 74 20 74 6f 20 30 2e 20 54 68 69 73 20  Dflt to 0. This 
22a5e 73 69 6d 70 6c 69 66 69 65 73 20 63 68 65 63 6b  simplifies check
22a5f 69 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61 6e 20  ing.  ** for an 
22a60 53 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75 6c 74  SQL NULL default
22a61 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69   below..  */.  i
22a62 66 28 20 70 44 66 6c 74 20 26 26 20 70 44 66 6c  f( pDflt && pDfl
22a63 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29  t->op==TK_NULL )
22a64 7b 0a 20 20 20 20 70 44 66 6c 74 20 3d 20 30 3b  {.    pDflt = 0;
22a65 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
22a66 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 6f   that the new co
22a67 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70 65 63  lumn is not spec
22a68 69 66 69 65 64 20 61 73 20 50 52 49 4d 41 52 59  ified as PRIMARY
22a69 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 2e 0a   KEY or UNIQUE..
22a6a 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73    ** If there is
22a6b 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73   a NOT NULL cons
22a6c 74 72 61 69 6e 74 2c 20 74 68 65 6e 20 74 68 65  traint, then the
22a6d 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66   default value f
22a6e 6f 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75  or the.  ** colu
22a6f 6d 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 4e  mn must not be N
22a70 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ULL..  */.  if( 
22a71 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 20  pCol->isPrimKey 
22a72 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
22a73 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
22a74 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 50 52 49  Cannot add a PRI
22a75 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 22  MARY KEY column"
22a76 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
22a77 20 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70   }.  if( pNew->p
22a78 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c  Index ){.    sql
22a79 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
22a7a 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64  rse, "Cannot add
22a7b 20 61 20 55 4e 49 51 55 45 20 63 6f 6c 75 6d 6e   a UNIQUE column
22a7c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ");.    return;.
22a7d 20 20 7d 0a 20 20 69 66 28 20 28 64 62 2d 3e 66    }.  if( (db->f
22a7e 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65  lags&SQLITE_Fore
22a7f 69 67 6e 4b 65 79 73 29 20 26 26 20 70 4e 65 77  ignKeys) && pNew
22a80 2d 3e 70 46 4b 65 79 20 26 26 20 70 44 66 6c 74  ->pFKey && pDflt
22a81 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
22a82 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
22a83 0a 20 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74  .        "Cannot
22a84 20 61 64 64 20 61 20 52 45 46 45 52 45 4e 43 45   add a REFERENCE
22a85 53 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f  S column with no
22a86 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 76  n-NULL default v
22a87 61 6c 75 65 22 29 3b 0a 20 20 20 20 72 65 74 75  alue");.    retu
22a88 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  rn;.  }.  if( pC
22a89 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21  ol->notNull && !
22a8a 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c  pDflt ){.    sql
22a8b 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
22a8c 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43  rse, .        "C
22a8d 61 6e 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20  annot add a NOT 
22a8e 4e 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68  NULL column with
22a8f 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e   default value N
22a90 55 4c 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72  ULL");.    retur
22a91 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73  n;.  }..  /* Ens
22a92 75 72 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  ure the default 
22a93 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f  expression is so
22a94 6d 65 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c  mething that sql
22a95 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
22a96 72 28 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e  r().  ** can han
22a97 64 6c 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55  dle (i.e. not CU
22a98 52 52 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29  RRENT_TIME etc.)
22a99 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c  .  */.  if( pDfl
22a9a 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
22a9b 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20  _value *pVal;.  
22a9c 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c    if( sqlite3Val
22a9d 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70  ueFromExpr(db, p
22a9e 44 66 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46  Dflt, SQLITE_UTF
22a9f 38 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  8, SQLITE_AFF_NO
22aa0 4e 45 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20  NE, &pVal) ){.  
22aa1 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
22aa2 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
22aa3 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
22aa4 20 20 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20    if( !pVal ){. 
22aa5 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
22aa6 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61  rMsg(pParse, "Ca
22aa7 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d  nnot add a colum
22aa8 6e 20 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74  n with non-const
22aa9 61 6e 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20  ant default");. 
22aaa 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
22aab 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61   }.    sqlite3Va
22aac 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20  lueFree(pVal);. 
22aad 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20   }..  /* Modify 
22aae 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
22aaf 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
22ab0 20 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44   zCol = sqlite3D
22ab1 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68  bStrNDup(db, (ch
22ab2 61 72 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20  ar*)pColDef->z, 
22ab3 70 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69  pColDef->n);.  i
22ab4 66 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63  f( zCol ){.    c
22ab5 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f  har *zEnd = &zCo
22ab6 6c 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b  l[pColDef->n-1];
22ab7 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 45 6e 64  .    while( zEnd
22ab8 3e 7a 43 6f 6c 20 26 26 20 28 2a 7a 45 6e 64 3d  >zCol && (*zEnd=
22ab9 3d 27 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33 49  =';' || sqlite3I
22aba 73 73 70 61 63 65 28 2a 7a 45 6e 64 29 29 20 29  sspace(*zEnd)) )
22abb 7b 0a 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d 20  {.      *zEnd-- 
22abc 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 20  = '\0';.    }.  
22abd 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
22abe 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20  arse(pParse, .  
22abf 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c 22        "UPDATE \"
22ac0 25 77 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20  %w\".%s SET ".  
22ac1 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73          "sql = s
22ac2 75 62 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20  ubstr(sql,1,%d) 
22ac3 7c 7c 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c  || ', ' || %Q ||
22ac4 20 73 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20   substr(sql,%d) 
22ac5 22 0a 20 20 20 20 20 20 20 20 22 57 48 45 52 45  ".        "WHERE
22ac6 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20   type = 'table' 
22ac7 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20  AND name = %Q", 
22ac8 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45  .      zDb, SCHE
22ac9 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70  MA_TABLE(iDb), p
22aca 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65  New->addColOffse
22acb 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61  t, zCol, pNew->a
22acc 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20  ddColOffset+1,. 
22acd 20 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b       zTab.    );
22ace 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
22acf 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20  ee(db, zCol);.  
22ad0 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64  }..  /* If the d
22ad1 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20  efault value of 
22ad2 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69  the new column i
22ad3 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74  s NULL, then set
22ad4 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 66   the file.  ** f
22ad5 6f 72 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 74  ormat to 2. If t
22ad6 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
22ad7 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75   of the new colu
22ad8 6d 6e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a  mn is not NULL,.
22ad9 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 6f    ** the file fo
22ada 72 6d 61 74 20 62 65 63 6f 6d 65 73 20 33 2e 0a  rmat becomes 3..
22adb 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d 69    */.  sqlite3Mi
22adc 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28  nimumFileFormat(
22add 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 44 66  pParse, iDb, pDf
22ade 6c 74 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 20  lt ? 3 : 2);..  
22adf 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 73 63  /* Reload the sc
22ae0 68 65 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 69  hema of the modi
22ae1 66 69 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20  fied table. */. 
22ae2 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65   reloadTableSche
22ae3 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ma(pParse, pTab,
22ae4 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d   pTab->zName);.}
22ae5 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
22ae6 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
22ae7 62 79 20 74 68 65 20 70 61 72 73 65 72 20 61 66  by the parser af
22ae8 74 65 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61  ter the table-na
22ae9 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54  me in.** an "ALT
22aea 45 52 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d  ER TABLE <table-
22aeb 6e 61 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65  name> ADD" state
22aec 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20  ment is parsed. 
22aed 41 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 72  Argument .** pSr
22aee 63 20 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e 61  c is the full-na
22aef 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
22af0 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a 2a  being altered..*
22af1 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
22af2 65 20 6d 61 6b 65 73 20 61 20 28 70 61 72 74 69  e makes a (parti
22af3 61 6c 29 20 63 6f 70 79 20 6f 66 20 74 68 65 20  al) copy of the 
22af4 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a  Table structure.
22af5 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
22af6 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20 61   being altered a
22af7 6e 64 20 73 65 74 73 20 50 61 72 73 65 2e 70 4e  nd sets Parse.pN
22af8 65 77 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e 74  ewTable to point
22af9 0a 2a 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 69  .** to it. Routi
22afa 6e 65 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68  nes called by th
22afb 65 20 70 61 72 73 65 72 20 61 73 20 74 68 65 20  e parser as the 
22afc 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  column definitio
22afd 6e 0a 2a 2a 20 69 73 20 70 61 72 73 65 64 20 28  n.** is parsed (
22afe 69 2e 65 2e 20 73 71 6c 69 74 65 33 41 64 64 43  i.e. sqlite3AddC
22aff 6f 6c 75 6d 6e 28 29 29 20 61 64 64 20 74 68 65  olumn()) add the
22b00 20 6e 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 61   new Column data
22b01 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 79   to .** the copy
22b02 2e 20 54 68 65 20 63 6f 70 79 20 6f 66 20 74 68  . The copy of th
22b03 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
22b04 65 20 69 73 20 64 65 6c 65 74 65 64 20 62 79 20  e is deleted by 
22b05 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61  tokenize.c .** a
22b06 66 74 65 72 20 70 61 72 73 69 6e 67 20 69 73 20  fter parsing is 
22b07 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20  finished..**.** 
22b08 52 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 33 41  Routine sqlite3A
22b09 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c  lterFinishAddCol
22b0a 75 6d 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 61  umn() will be ca
22b0b 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65  lled to complete
22b0c 0a 2a 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 22  .** coding the "
22b0d 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20  ALTER TABLE ... 
22b0e 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ADD" statement..
22b0f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22b10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
22b11 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d  terBeginAddColum
22b12 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
22b13 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b   SrcList *pSrc){
22b14 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a  .  Table *pNew;.
22b15 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
22b16 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
22b17 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  iDb;.  int i;.  
22b18 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71  int nAlloc;.  sq
22b19 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
22b1a 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f  se->db;..  /* Lo
22b1b 6f 6b 20 75 70 20 74 68 65 20 74 61 62 6c 65 20  ok up the table 
22b1c 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a  being altered. *
22b1d 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  /.  assert( pPar
22b1e 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30  se->pNewTable==0
22b1f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
22b20 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
22b21 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b  llMutexes(db) );
22b22 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
22b23 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65  cFailed ) goto e
22b24 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
22b25 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 73  lumn;.  pTab = s
22b26 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
22b27 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72  e(pParse, 0, pSr
22b28 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70  c->a[0].zName, p
22b29 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62  Src->a[0].zDatab
22b2a 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61  ase);.  if( !pTa
22b2b 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65  b ) goto exit_be
22b2c 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
22b2d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22b2e 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
22b2f 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  E.  if( IsVirtua
22b30 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73  l(pTab) ){.    s
22b31 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
22b32 50 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20  Parse, "virtual 
22b33 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62  tables may not b
22b34 65 20 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20  e altered");.   
22b35 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
22b36 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d  _add_column;.  }
22b37 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61  .#endif..  /* Ma
22b38 6b 65 20 73 75 72 65 20 74 68 69 73 20 69 73 20  ke sure this is 
22b39 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74  not an attempt t
22b3a 6f 20 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20  o ALTER a view. 
22b3b 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  */.  if( pTab->p
22b3c 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71  Select ){.    sq
22b3d 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
22b3e 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64  arse, "Cannot ad
22b3f 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20  d a column to a 
22b40 76 69 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f  view");.    goto
22b41 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f   exit_begin_add_
22b42 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61  column;.  }..  a
22b43 73 73 65 72 74 28 20 70 54 61 62 2d 3e 61 64 64  ssert( pTab->add
22b44 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20  ColOffset>0 );. 
22b45 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
22b46 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
22b47 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
22b48 0a 20 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79  .  /* Put a copy
22b49 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74   of the Table st
22b4a 72 75 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e  ruct in Parse.pN
22b4b 65 77 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a  ewTable for the.
22b4c 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43    ** sqlite3AddC
22b4d 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e  olumn() function
22b4e 20 61 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20   and friends to 
22b4f 6d 6f 64 69 66 79 2e 20 20 42 75 74 20 6d 6f 64  modify.  But mod
22b50 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d  ify.  ** the nam
22b51 65 20 62 79 20 61 64 64 69 6e 67 20 61 6e 20 22  e by adding an "
22b52 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f  sqlite_altertab_
22b53 22 20 70 72 65 66 69 78 2e 20 20 42 79 20 61 64  " prefix.  By ad
22b54 64 69 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70  ding this.  ** p
22b55 72 65 66 69 78 2c 20 77 65 20 69 6e 73 75 72 65  refix, we insure
22b56 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65 20 77   that the name w
22b57 69 6c 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20  ill not collide 
22b58 77 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67  with an existing
22b59 0a 20 20 2a 2a 20 74 61 62 6c 65 20 62 65 63 61  .  ** table beca
22b5a 75 73 65 20 75 73 65 72 20 74 61 62 6c 65 20 61  use user table a
22b5b 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74  re not allowed t
22b5c 6f 20 68 61 76 65 20 74 68 65 20 22 73 71 6c 69  o have the "sqli
22b5d 74 65 5f 22 0a 20 20 2a 2a 20 70 72 65 66 69 78  te_".  ** prefix
22b5e 20 6f 6e 20 74 68 65 69 72 20 6e 61 6d 65 2e 0a   on their name..
22b5f 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54    */.  pNew = (T
22b60 61 62 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 4d  able*)sqlite3DbM
22b61 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
22b62 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20  zeof(Table));.  
22b63 69 66 28 20 21 70 4e 65 77 20 29 20 67 6f 74 6f  if( !pNew ) goto
22b64 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f   exit_begin_add_
22b65 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65  column;.  pParse
22b66 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e  ->pNewTable = pN
22b67 65 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66  ew;.  pNew->nRef
22b68 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 64 62   = 1;.  pNew->db
22b69 4d 65 6d 20 3d 20 70 54 61 62 2d 3e 64 62 4d 65  Mem = pTab->dbMe
22b6a 6d 3b 0a 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20  m;.  pNew->nCol 
22b6b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20  = pTab->nCol;.  
22b6c 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43  assert( pNew->nC
22b6d 6f 6c 3e 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63  ol>0 );.  nAlloc
22b6e 20 3d 20 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c   = (((pNew->nCol
22b6f 2d 31 29 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61  -1)/8)*8)+8;.  a
22b70 73 73 65 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70  ssert( nAlloc>=p
22b71 4e 65 77 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c  New->nCol && nAl
22b72 6c 6f 63 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c  loc%8==0 && nAll
22b73 6f 63 2d 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20  oc-pNew->nCol<8 
22b74 29 3b 0a 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20  );.  pNew->aCol 
22b75 3d 20 28 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74  = (Column*)sqlit
22b76 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
22b77 62 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e  b, sizeof(Column
22b78 29 2a 6e 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65  )*nAlloc);.  pNe
22b79 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  w->zName = sqlit
22b7a 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73  e3MPrintf(db, "s
22b7b 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 25  qlite_altertab_%
22b7c 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  s", pTab->zName)
22b7d 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61  ;.  if( !pNew->a
22b7e 43 6f 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e  Col || !pNew->zN
22b7f 61 6d 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  ame ){.    db->m
22b80 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
22b81 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62  .    goto exit_b
22b82 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
22b83 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e  .  }.  memcpy(pN
22b84 65 77 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e  ew->aCol, pTab->
22b85 61 43 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c  aCol, sizeof(Col
22b86 75 6d 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29  umn)*pNew->nCol)
22b87 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
22b88 4e 65 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  New->nCol; i++){
22b89 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  .    Column *pCo
22b8a 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b  l = &pNew->aCol[
22b8b 69 5d 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e  i];.    pCol->zN
22b8c 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
22b8d 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e  trDup(db, pCol->
22b8e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c  zName);.    pCol
22b8f 2d 3e 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20  ->zColl = 0;.   
22b90 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30   pCol->zType = 0
22b91 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c  ;.    pCol->pDfl
22b92 74 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d  t = 0;.    pCol-
22b93 3e 7a 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a  >zDflt = 0;.  }.
22b94 20 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20    pNew->pSchema 
22b95 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
22b96 53 63 68 65 6d 61 3b 0a 20 20 70 4e 65 77 2d 3e  Schema;.  pNew->
22b97 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 70  addColOffset = p
22b98 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65  Tab->addColOffse
22b99 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20  t;.  pNew->nRef 
22b9a 3d 20 31 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  = 1;..  /* Begin
22b9b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61   a transaction a
22b9c 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  nd increment the
22b9d 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20   schema cookie. 
22b9e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67   */.  sqlite3Beg
22b9f 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
22ba0 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29  (pParse, 0, iDb)
22ba1 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ;.  v = sqlite3G
22ba2 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
22ba3 20 20 69 66 28 20 21 76 20 29 20 67 6f 74 6f 20    if( !v ) goto 
22ba4 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63  exit_begin_add_c
22ba5 6f 6c 75 6d 6e 3b 0a 20 20 73 71 6c 69 74 65 33  olumn;.  sqlite3
22ba6 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
22ba7 72 73 65 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74  rse, iDb);..exit
22ba8 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d  _begin_add_colum
22ba9 6e 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  n:.  sqlite3SrcL
22baa 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  istDelete(db, pS
22bab 72 63 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  rc);.  return;.}
22bac 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49  .#endif  /* SQLI
22bad 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 2a  TE_ALTER_TABLE *
22bae 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
22baf 2a 2a 20 45 6e 64 20 6f 66 20 61 6c 74 65 72 2e  ** End of alter.
22bb0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
22bb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bb3 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
22bb4 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6e  ** Begin file an
22bb5 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  alyze.c ********
22bb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bb8 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75  */./*.** 2005 Ju
22bb9 6c 79 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ly 8.**.** The a
22bba 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
22bbb 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
22bbc 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
22bbd 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
22bbe 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
22bbf 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
22bc0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
22bc1 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
22bc2 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
22bc3 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
22bc4 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
22bc5 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
22bc6 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
22bc7 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
22bc8 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
22bc9 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
22bca 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
22bcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bcf 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
22bd0 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 61   contains code a
22bd1 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
22bd2 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  he ANALYZE comma
22bd3 6e 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  nd..*/.#ifndef S
22bd4 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59  QLITE_OMIT_ANALY
22bd5 5a 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  ZE../*.** This r
22bd6 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
22bd7 20 63 6f 64 65 20 74 68 61 74 20 6f 70 65 6e 73   code that opens
22bd8 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
22bd9 31 20 74 61 62 6c 65 20 66 6f 72 0a 2a 2a 20 77  1 table for.** w
22bda 72 69 74 69 6e 67 20 77 69 74 68 20 63 75 72 73  riting with curs
22bdb 6f 72 20 69 53 74 61 74 43 75 72 2e 20 49 66 20  or iStatCur. If 
22bdc 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20  the library was 
22bdd 62 75 69 6c 74 20 77 69 74 68 20 74 68 65 0a 2a  built with the.*
22bde 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
22bdf 53 54 41 54 32 20 6d 61 63 72 6f 20 64 65 66 69  STAT2 macro defi
22be0 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 73 71  ned, then the sq
22be1 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65  lite_stat2 table
22be2 20 69 73 0a 2a 2a 20 6f 70 65 6e 65 64 20 66 6f   is.** opened fo
22be3 72 20 77 72 69 74 69 6e 67 20 75 73 69 6e 67 20  r writing using 
22be4 63 75 72 73 6f 72 20 28 69 53 74 61 74 43 75 72  cursor (iStatCur
22be5 2b 31 29 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  +1).**.** If the
22be6 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
22be7 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74 20 70 72  bles does not pr
22be8 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74 2c 20  eviously exist, 
22be9 69 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a  it is created..*
22bea 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20  * Similarly, if 
22beb 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32  the sqlite_stat2
22bec 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20   table does not 
22bed 65 78 69 73 74 20 61 6e 64 20 74 68 65 20 6c 69  exist and the li
22bee 62 72 61 72 79 0a 2a 2a 20 69 73 20 63 6f 6d 70  brary.** is comp
22bef 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45  iled with SQLITE
22bf0 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 64 65  _ENABLE_STAT2 de
22bf1 66 69 6e 65 64 2c 20 69 74 20 69 73 20 63 72 65  fined, it is cre
22bf2 61 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 41 72 67  ated. .**.** Arg
22bf3 75 6d 65 6e 74 20 7a 57 68 65 72 65 20 6d 61 79  ument zWhere may
22bf4 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   be a pointer to
22bf5 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69   a buffer contai
22bf6 6e 69 6e 67 20 61 20 74 61 62 6c 65 20 6e 61 6d  ning a table nam
22bf7 65 2c 0a 2a 2a 20 6f 72 20 69 74 20 6d 61 79 20  e,.** or it may 
22bf8 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  be a NULL pointe
22bf9 72 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  r. If it is not 
22bfa 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 65  NULL, then all e
22bfb 6e 74 72 69 65 73 20 69 6e 0a 2a 2a 20 74 68 65  ntries in.** the
22bfc 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 61 6e   sqlite_stat1 an
22bfd 64 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65  d (if applicable
22bfe 29 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74  ) sqlite_stat2 t
22bff 61 62 6c 65 73 20 61 73 73 6f 63 69 61 74 65 64  ables associated
22c00 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6e 61 6d  .** with the nam
22c01 65 64 20 74 61 62 6c 65 20 61 72 65 20 64 65 6c  ed table are del
22c02 65 74 65 64 2e 20 49 66 20 7a 57 68 65 72 65 3d  eted. If zWhere=
22c03 3d 30 2c 20 74 68 65 6e 20 63 6f 64 65 20 69 73  =0, then code is
22c04 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 74 6f   generated.** to
22c05 20 64 65 6c 65 74 65 20 61 6c 6c 20 73 74 61 74   delete all stat
22c06 20 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 0a   table entries..
22c07 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
22c08 70 65 6e 53 74 61 74 54 61 62 6c 65 28 0a 20 20  penStatTable(.  
22c09 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
22c0a 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
22c0b 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
22c0c 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20  int iDb,        
22c0d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
22c0e 61 74 61 62 61 73 65 20 77 65 20 61 72 65 20 6c  atabase we are l
22c0f 6f 6f 6b 69 6e 67 20 69 6e 20 2a 2f 0a 20 20 69  ooking in */.  i
22c10 6e 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 20  nt iStatCur,    
22c11 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74         /* Open t
22c12 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
22c13 74 61 62 6c 65 20 6f 6e 20 74 68 69 73 20 63 75  table on this cu
22c14 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rsor */.  const 
22c15 63 68 61 72 20 2a 7a 57 68 65 72 65 20 20 20 20  char *zWhere    
22c16 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 6e 74 72    /* Delete entr
22c17 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ies associated w
22c18 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 20 2a  ith this table *
22c19 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 73 74  /.){.  static st
22c1a 72 75 63 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74  ruct {.    const
22c1b 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
22c1c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
22c1d 6f 6c 73 3b 0a 20 20 7d 20 61 54 61 62 6c 65 5b  ols;.  } aTable[
22c1e 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 73 71 6c  ] = {.    { "sql
22c1f 69 74 65 5f 73 74 61 74 31 22 2c 20 22 74 62 6c  ite_stat1", "tbl
22c20 2c 69 64 78 2c 73 74 61 74 22 20 7d 2c 0a 23 69  ,idx,stat" },.#i
22c21 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
22c22 4c 45 5f 53 54 41 54 32 0a 20 20 20 20 7b 20 22  LE_STAT2.    { "
22c23 73 71 6c 69 74 65 5f 73 74 61 74 32 22 2c 20 22  sqlite_stat2", "
22c24 74 62 6c 2c 69 64 78 2c 73 61 6d 70 6c 65 6e 6f  tbl,idx,sampleno
22c25 2c 73 61 6d 70 6c 65 22 20 7d 2c 0a 23 65 6e 64  ,sample" },.#end
22c26 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 61  if.  };..  int a
22c27 52 6f 6f 74 5b 5d 20 3d 20 7b 30 2c 20 30 7d 3b  Root[] = {0, 0};
22c28 0a 20 20 75 38 20 61 43 72 65 61 74 65 54 62 6c  .  u8 aCreateTbl
22c29 5b 5d 20 3d 20 7b 30 2c 20 30 7d 3b 0a 0a 20 20  [] = {0, 0};..  
22c2a 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33  int i;.  sqlite3
22c2b 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
22c2c 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20  b;.  Db *pDb;.  
22c2d 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
22c2e 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22c2f 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72  ;.  if( v==0 ) r
22c30 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
22c31 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
22c32 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29  dsAllMutexes(db)
22c33 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
22c34 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3d 3d  lite3VdbeDb(v)==
22c35 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  db );.  pDb = &d
22c36 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20  b->aDb[iDb];..  
22c37 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
22c38 53 69 7a 65 28 61 54 61 62 6c 65 29 3b 20 69 2b  Size(aTable); i+
22c39 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  +){.    const ch
22c3a 61 72 20 2a 7a 54 61 62 20 3d 20 61 54 61 62 6c  ar *zTab = aTabl
22c3b 65 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  e[i].zName;.    
22c3c 54 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20  Table *pStat;.  
22c3d 20 20 69 66 28 20 28 70 53 74 61 74 20 3d 20 73    if( (pStat = s
22c3e 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
22c3f 64 62 2c 20 7a 54 61 62 2c 20 70 44 62 2d 3e 7a  db, zTab, pDb->z
22c40 4e 61 6d 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20  Name))==0 ){.   
22c41 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65     /* The sqlite
22c42 5f 73 74 61 74 5b 31 32 5d 20 74 61 62 6c 65 20  _stat[12] table 
22c43 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20  does not exist. 
22c44 43 72 65 61 74 65 20 69 74 2e 20 4e 6f 74 65 20  Create it. Note 
22c45 74 68 61 74 20 61 20 0a 20 20 20 20 20 20 2a 2a  that a .      **
22c46 20 73 69 64 65 2d 65 66 66 65 63 74 20 6f 66 20   side-effect of 
22c47 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
22c48 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 6f   statement is to
22c49 20 6c 65 61 76 65 20 74 68 65 20 72 6f 6f 74 70   leave the rootp
22c4a 61 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 6f 66  age .      ** of
22c4b 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69   the new table i
22c4c 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73  n register pPars
22c4d 65 2d 3e 72 65 67 52 6f 6f 74 2e 20 54 68 69 73  e->regRoot. This
22c4e 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 0a 20   is important . 
22c4f 20 20 20 20 20 2a 2a 20 62 65 63 61 75 73 65 20       ** because 
22c50 74 68 65 20 4f 70 65 6e 57 72 69 74 65 20 6f 70  the OpenWrite op
22c51 63 6f 64 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20  code below will 
22c52 62 65 20 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a  be needing it. *
22c53 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e  /.      sqlite3N
22c54 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
22c55 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 52  e,.          "CR
22c56 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 25 73  EATE TABLE %Q.%s
22c57 28 25 73 29 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d  (%s)", pDb->zNam
22c58 65 2c 20 7a 54 61 62 2c 20 61 54 61 62 6c 65 5b  e, zTab, aTable[
22c59 69 5d 2e 7a 43 6f 6c 73 0a 20 20 20 20 20 20 29  i].zCols.      )
22c5a 3b 0a 20 20 20 20 20 20 61 52 6f 6f 74 5b 69 5d  ;.      aRoot[i]
22c5b 20 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f   = pParse->regRo
22c5c 6f 74 3b 0a 20 20 20 20 20 20 61 43 72 65 61 74  ot;.      aCreat
22c5d 65 54 62 6c 5b 69 5d 20 3d 20 31 3b 0a 20 20 20  eTbl[i] = 1;.   
22c5e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
22c5f 20 54 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61   The table alrea
22c60 64 79 20 65 78 69 73 74 73 2e 20 49 66 20 7a 57  dy exists. If zW
22c61 68 65 72 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  here is not NULL
22c62 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74  , delete all ent
22c63 72 69 65 73 20 0a 20 20 20 20 20 20 2a 2a 20 61  ries .      ** a
22c64 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
22c65 68 65 20 74 61 62 6c 65 20 7a 57 68 65 72 65 2e  he table zWhere.
22c66 20 49 66 20 7a 57 68 65 72 65 20 69 73 20 4e 55   If zWhere is NU
22c67 4c 4c 2c 20 64 65 6c 65 74 65 20 74 68 65 0a 20  LL, delete the. 
22c68 20 20 20 20 20 2a 2a 20 65 6e 74 69 72 65 20 63       ** entire c
22c69 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 74  ontents of the t
22c6a 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61  able. */.      a
22c6b 52 6f 6f 74 5b 69 5d 20 3d 20 70 53 74 61 74 2d  Root[i] = pStat-
22c6c 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20 73 71 6c  >tnum;.      sql
22c6d 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50  ite3TableLock(pP
22c6e 61 72 73 65 2c 20 69 44 62 2c 20 61 52 6f 6f 74  arse, iDb, aRoot
22c6f 5b 69 5d 2c 20 31 2c 20 7a 54 61 62 29 3b 0a 20  [i], 1, zTab);. 
22c70 20 20 20 20 20 69 66 28 20 7a 57 68 65 72 65 20       if( zWhere 
22c71 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
22c72 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
22c73 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
22c74 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51   "DELETE FROM %Q
22c75 2e 25 73 20 57 48 45 52 45 20 74 62 6c 3d 25 51  .%s WHERE tbl=%Q
22c76 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  ", pDb->zName, z
22c77 54 61 62 2c 20 7a 57 68 65 72 65 0a 20 20 20 20  Tab, zWhere.    
22c78 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c      );.      }el
22c79 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  se{.        /* T
22c7a 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 5b 31  he sqlite_stat[1
22c7b 32 5d 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79  2] table already
22c7c 20 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65   exists.  Delete
22c7d 20 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20   all rows. */.  
22c7e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22c7f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
22c80 65 61 72 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69  ear, aRoot[i], i
22c81 44 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Db);.      }.   
22c82 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65   }.  }..  /* Ope
22c83 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  n the sqlite_sta
22c84 74 5b 31 32 5d 20 74 61 62 6c 65 73 20 66 6f 72  t[12] tables for
22c85 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 66   writing. */.  f
22c86 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53  or(i=0; i<ArrayS
22c87 69 7a 65 28 61 54 61 62 6c 65 29 3b 20 69 2b 2b  ize(aTable); i++
22c88 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
22c89 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f  beAddOp3(v, OP_O
22c8a 70 65 6e 57 72 69 74 65 2c 20 69 53 74 61 74 43  penWrite, iStatC
22c8b 75 72 2b 69 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20  ur+i, aRoot[i], 
22c8c 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
22c8d 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
22c8e 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 33 2c 20   -1, (char *)3, 
22c8f 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73  P4_INT32);.    s
22c90 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
22c91 50 35 28 76 2c 20 61 43 72 65 61 74 65 54 62 6c  P5(v, aCreateTbl
22c92 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  [i]);.  }.}../*.
22c93 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
22c94 20 74 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73   to do an analys
22c95 69 73 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65  is of all indice
22c96 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
22c97 68 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61  h.** a single ta
22c98 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
22c99 6f 69 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61  oid analyzeOneTa
22c9a 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
22c9b 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65  arse,   /* Parse
22c9c 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  r context */.  T
22c9d 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
22c9e 2f 2a 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69  /* Table whose i
22c9f 6e 64 69 63 65 73 20 61 72 65 20 74 6f 20 62 65  ndices are to be
22ca0 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69   analyzed */.  i
22ca1 6e 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 20  nt iStatCur,    
22ca2 2f 2a 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65  /* Index of Vdbe
22ca3 43 75 72 73 6f 72 20 74 68 61 74 20 77 72 69 74  Cursor that writ
22ca4 65 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  es the sqlite_st
22ca5 61 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  at1 table */.  i
22ca6 6e 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20  nt iMem         
22ca7 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d  /* Available mem
22ca8 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65  ory locations be
22ca9 67 69 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  gin here */.){. 
22caa 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
22cab 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 2f 2a  Parse->db;    /*
22cac 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
22cad 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
22cae 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
22caf 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74     /* An index t
22cb0 6f 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64  o being analyzed
22cb1 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75   */.  int iIdxCu
22cb2 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
22cb3 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65     /* Cursor ope
22cb4 6e 20 6f 6e 20 69 6e 64 65 78 20 62 65 69 6e 67  n on index being
22cb5 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 56   analyzed */.  V
22cb6 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
22cb7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
22cb8 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
22cb9 6e 65 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75  ne being built u
22cba 70 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  p */.  int i;   
22cbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22cbc 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
22cbd 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70  ter */.  int top
22cbe 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20  OfLoop;         
22cbf 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 70        /* The top
22cc0 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a   of the loop */.
22cc1 20 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b    int endOfLoop;
22cc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22cc3 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * The end of the
22cc4 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61   loop */.  int a
22cc5 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ddr;            
22cc6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
22cc7 64 64 72 65 73 73 20 6f 66 20 61 6e 20 69 6e 73  ddress of an ins
22cc8 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  truction */.  in
22cc9 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
22cca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
22ccb 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20  dex of database 
22ccc 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20  containing pTab 
22ccd 2a 2f 0a 20 20 69 6e 74 20 72 65 67 54 61 62 6e  */.  int regTabn
22cce 61 6d 65 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20  ame = iMem++;   
22ccf 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 63 6f    /* Register co
22cd0 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 6e  ntaining table n
22cd1 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ame */.  int reg
22cd2 49 64 78 6e 61 6d 65 20 3d 20 69 4d 65 6d 2b 2b  Idxname = iMem++
22cd3 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65  ;     /* Registe
22cd4 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 64  r containing ind
22cd5 65 78 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74  ex name */.  int
22cd6 20 72 65 67 53 61 6d 70 6c 65 6e 6f 20 3d 20 69   regSampleno = i
22cd7 4d 65 6d 2b 2b 3b 20 20 20 20 2f 2a 20 52 65 67  Mem++;    /* Reg
22cd8 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  ister containing
22cd9 20 6e 65 78 74 20 73 61 6d 70 6c 65 20 6e 75 6d   next sample num
22cda 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ber */.  int reg
22cdb 43 6f 6c 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20  Col = iMem++;   
22cdc 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74        /* Content
22cdd 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 61 6e 61   of a column ana
22cde 6c 79 7a 65 64 20 74 61 62 6c 65 20 2a 2f 0a 20  lyzed table */. 
22cdf 20 69 6e 74 20 72 65 67 52 65 63 20 3d 20 69 4d   int regRec = iM
22ce0 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a  em++;         /*
22ce1 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
22ce2 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 63 6f  g completed reco
22ce3 72 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 54  rd */.  int regT
22ce4 65 6d 70 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20  emp = iMem++;   
22ce5 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
22ce6 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20 2a  y use register *
22ce7 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  /.  int regRowid
22ce8 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20   = iMem++;      
22ce9 20 2f 2a 20 52 6f 77 69 64 20 66 6f 72 20 74 68   /* Rowid for th
22cea 65 20 69 6e 73 65 72 74 65 64 20 72 65 63 6f 72  e inserted recor
22ceb 64 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  d */..#ifdef SQL
22cec 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
22ced 0a 20 20 69 6e 74 20 72 65 67 54 65 6d 70 32 20  .  int regTemp2 
22cee 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20  = iMem++;       
22cef 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 75 73 65  /* Temporary use
22cf0 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69   register */.  i
22cf1 6e 74 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e  nt regSamplerecn
22cf2 6f 20 3d 20 69 4d 65 6d 2b 2b 3b 20 2f 2a 20 49  o = iMem++; /* I
22cf3 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 73 61 6d  ndex of next sam
22cf4 70 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f  ple to record */
22cf5 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6e 6f 20  .  int regRecno 
22cf6 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20  = iMem++;       
22cf7 2f 2a 20 43 75 72 72 65 6e 74 20 73 61 6d 70 6c  /* Current sampl
22cf8 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  e index */.  int
22cf9 20 72 65 67 4c 61 73 74 20 3d 20 69 4d 65 6d 2b   regLast = iMem+
22cfa 2b 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  +;        /* Ind
22cfb 65 78 20 6f 66 20 6c 61 73 74 20 73 61 6d 70 6c  ex of last sampl
22cfc 65 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 20  e to record */. 
22cfd 20 69 6e 74 20 72 65 67 46 69 72 73 74 20 3d 20   int regFirst = 
22cfe 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a  iMem++;       /*
22cff 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 20   Index of first 
22d00 73 61 6d 70 6c 65 20 74 6f 20 72 65 63 6f 72 64  sample to record
22d01 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20   */.#endif..  v 
22d02 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
22d03 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
22d04 76 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 54  v==0 || NEVER(pT
22d05 61 62 3d 3d 30 29 20 7c 7c 20 70 54 61 62 2d 3e  ab==0) || pTab->
22d06 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20  pIndex==0 ){.   
22d07 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61 6c 79 73   /* Do no analys
22d08 69 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 68  is for tables th
22d09 61 74 20 68 61 76 65 20 6e 6f 20 69 6e 64 69 63  at have no indic
22d0a 65 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  es */.    return
22d0b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
22d0c 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
22d0d 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20  sAllMutexes(db) 
22d0e 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
22d0f 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
22d10 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
22d11 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  a);.  assert( iD
22d12 62 3e 3d 30 20 29 3b 0a 23 69 66 6e 64 65 66 20  b>=0 );.#ifndef 
22d13 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
22d14 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20  ORIZATION.  if( 
22d15 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
22d16 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
22d17 41 4e 41 4c 59 5a 45 2c 20 70 54 61 62 2d 3e 7a  ANALYZE, pTab->z
22d18 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 64  Name, 0,.      d
22d19 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
22d1a 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  e ) ){.    retur
22d1b 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  n;.  }.#endif.. 
22d1c 20 2f 2a 20 45 73 74 61 62 6c 69 73 68 20 61 20   /* Establish a 
22d1d 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  read-lock on the
22d1e 20 74 61 62 6c 65 20 61 74 20 74 68 65 20 73 68   table at the sh
22d1f 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c  ared-cache level
22d20 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61  . */.  sqlite3Ta
22d21 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20  bleLock(pParse, 
22d22 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
22d23 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29   0, pTab->zName)
22d24 3b 0a 0a 20 20 69 49 64 78 43 75 72 20 3d 20 70  ;..  iIdxCur = p
22d25 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
22d26 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
22d27 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
22d28 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
22d29 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20  .    int nCol = 
22d2a 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20  pIdx->nColumn;. 
22d2b 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
22d2c 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
22d2d 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
22d2e 49 64 78 29 3b 0a 0a 20 20 20 20 69 66 28 20 69  Idx);..    if( i
22d2f 4d 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3e 70  Mem+1+(nCol*2)>p
22d30 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20  Parse->nMem ){. 
22d31 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
22d32 6d 20 3d 20 69 4d 65 6d 2b 31 2b 28 6e 43 6f 6c  m = iMem+1+(nCol
22d33 2a 32 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  *2);.    }..    
22d34 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72  /* Open a cursor
22d35 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f   to the index to
22d36 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f   be analyzed. */
22d37 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62  .    assert( iDb
22d38 3d 3d 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  ==sqlite3SchemaT
22d39 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 64 78 2d  oIndex(db, pIdx-
22d3a 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20 20  >pSchema) );.   
22d3b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22d3c 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  p4(v, OP_OpenRea
22d3d 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49 64 78  d, iIdxCur, pIdx
22d3e 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20  ->tnum, iDb,.   
22d3f 20 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65       (char *)pKe
22d40 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
22d41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65  NDOFF);.    Vdbe
22d42 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22  Comment((v, "%s"
22d43 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  , pIdx->zName));
22d44 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74  ..    /* Populat
22d45 65 20 74 68 65 20 72 65 67 69 73 74 65 72 73 20  e the registers 
22d46 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
22d47 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 6e  able and index n
22d48 61 6d 65 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ames. */.    if(
22d49 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 70   pTab->pIndex==p
22d4a 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Idx ){.      sql
22d4b 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
22d4c 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
22d4d 20 72 65 67 54 61 62 6e 61 6d 65 2c 20 30 2c 20   regTabname, 0, 
22d4e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pTab->zName, 0);
22d4f 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
22d50 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
22d51 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72  OP_String8, 0, r
22d52 65 67 49 64 78 6e 61 6d 65 2c 20 30 2c 20 70 49  egIdxname, 0, pI
22d53 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a  dx->zName, 0);..
22d54 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
22d55 41 42 4c 45 5f 53 54 41 54 32 0a 0a 20 20 20 20  ABLE_STAT2..    
22d56 2f 2a 20 49 66 20 74 68 69 73 20 69 74 65 72 61  /* If this itera
22d57 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70  tion of the loop
22d58 20 69 73 20 67 65 6e 65 72 61 74 69 6e 67 20 63   is generating c
22d59 6f 64 65 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74  ode to analyze t
22d5a 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20  he.    ** first 
22d5b 69 6e 64 65 78 20 69 6e 20 74 68 65 20 70 54 61  index in the pTa
22d5c 62 2d 3e 70 49 6e 64 65 78 20 6c 69 73 74 2c 20  b->pIndex list, 
22d5d 74 68 65 6e 20 72 65 67 69 73 74 65 72 20 72 65  then register re
22d5e 67 4c 61 73 74 20 68 61 73 0a 20 20 20 20 2a 2a  gLast has.    **
22d5f 20 6e 6f 74 20 62 65 65 6e 20 70 6f 70 75 6c 61   not been popula
22d60 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ted. In this cas
22d61 65 20 70 6f 70 75 6c 61 74 65 20 69 74 20 6e 6f  e populate it no
22d62 77 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  w.  */.    if( p
22d63 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 64  Tab->pIndex==pId
22d64 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  x ){.      sqlit
22d65 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22d66 4f 50 5f 49 6e 74 65 67 65 72 2c 20 53 51 4c 49  OP_Integer, SQLI
22d67 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
22d68 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f  , regSamplerecno
22d69 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22d6a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22d6b 5f 49 6e 74 65 67 65 72 2c 20 53 51 4c 49 54 45  _Integer, SQLITE
22d6c 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2a 32  _INDEX_SAMPLES*2
22d6d 2d 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20  -1, regTemp);.  
22d6e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22d6f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
22d70 67 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45  ger, SQLITE_INDE
22d71 58 5f 53 41 4d 50 4c 45 53 2a 32 2c 20 72 65 67  X_SAMPLES*2, reg
22d72 54 65 6d 70 32 29 3b 0a 0a 20 20 20 20 20 20 73  Temp2);..      s
22d73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22d74 28 76 2c 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 49  (v, OP_Count, iI
22d75 64 78 43 75 72 2c 20 72 65 67 4c 61 73 74 29 3b  dxCur, regLast);
22d76 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22d77 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
22d78 75 6c 6c 2c 20 30 2c 20 72 65 67 46 69 72 73 74  ull, 0, regFirst
22d79 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  );.      addr = 
22d7a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22d7b 33 28 76 2c 20 4f 50 5f 4c 74 2c 20 72 65 67 53  3(v, OP_Lt, regS
22d7c 61 6d 70 6c 65 72 65 63 6e 6f 2c 20 30 2c 20 72  amplerecno, 0, r
22d7d 65 67 4c 61 73 74 29 3b 0a 20 20 20 20 20 20 73  egLast);.      s
22d7e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22d7f 28 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 72  (v, OP_Divide, r
22d80 65 67 54 65 6d 70 32 2c 20 72 65 67 4c 61 73 74  egTemp2, regLast
22d81 2c 20 72 65 67 46 69 72 73 74 29 3b 0a 20 20 20  , regFirst);.   
22d82 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22d83 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 75 6c 74 69  dOp3(v, OP_Multi
22d84 70 6c 79 2c 20 72 65 67 4c 61 73 74 2c 20 72 65  ply, regLast, re
22d85 67 54 65 6d 70 2c 20 72 65 67 4c 61 73 74 29 3b  gTemp, regLast);
22d86 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22d87 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
22d88 64 64 49 6d 6d 2c 20 72 65 67 4c 61 73 74 2c 20  ddImm, regLast, 
22d89 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d  SQLITE_INDEX_SAM
22d8a 50 4c 45 53 2a 32 2d 32 29 3b 0a 20 20 20 20 20  PLES*2-2);.     
22d8b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22d8c 70 33 28 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c  p3(v, OP_Divide,
22d8d 20 20 72 65 67 54 65 6d 70 32 2c 20 72 65 67 4c    regTemp2, regL
22d8e 61 73 74 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20  ast, regLast);. 
22d8f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22d90 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
22d91 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
22d92 20 5a 65 72 6f 20 74 68 65 20 72 65 67 53 61 6d   Zero the regSam
22d93 70 6c 65 6e 6f 20 61 6e 64 20 72 65 67 52 65 63  pleno and regRec
22d94 6e 6f 20 72 65 67 69 73 74 65 72 73 2e 20 2a 2f  no registers. */
22d95 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22d96 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
22d97 65 67 65 72 2c 20 30 2c 20 72 65 67 53 61 6d 70  eger, 0, regSamp
22d98 6c 65 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74  leno);.    sqlit
22d99 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22d9a 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72  OP_Integer, 0, r
22d9b 65 67 52 65 63 6e 6f 29 3b 0a 20 20 20 20 73 71  egRecno);.    sq
22d9c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22d9d 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 46  v, OP_Copy, regF
22d9e 69 72 73 74 2c 20 72 65 67 53 61 6d 70 6c 65 72  irst, regSampler
22d9f 65 63 6e 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  ecno);.#endif.. 
22da0 20 20 20 2f 2a 20 54 68 65 20 62 6c 6f 63 6b 20     /* The block 
22da1 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  of memory cells 
22da2 69 6e 69 74 69 61 6c 69 7a 65 64 20 68 65 72 65  initialized here
22da3 20 69 73 20 75 73 65 64 20 61 73 20 66 6f 6c 6c   is used as foll
22da4 6f 77 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ows..    **.    
22da5 2a 2a 20 20 20 20 69 4d 65 6d 3a 20 20 20 20 20  **    iMem:     
22da6 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20             .    
22da7 2a 2a 20 20 20 20 20 20 20 20 54 68 65 20 74 6f  **        The to
22da8 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  tal number of ro
22da9 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ws in the table.
22daa 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
22dab 20 20 69 4d 65 6d 2b 31 20 2e 2e 20 69 4d 65 6d    iMem+1 .. iMem
22dac 2b 6e 43 6f 6c 3a 20 0a 20 20 20 20 2a 2a 20 20  +nCol: .    **  
22dad 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20        Number of 
22dae 64 69 73 74 69 6e 63 74 20 65 6e 74 72 69 65 73  distinct entries
22daf 20 69 6e 20 69 6e 64 65 78 20 63 6f 6e 73 69 64   in index consid
22db0 65 72 69 6e 67 20 74 68 65 20 0a 20 20 20 20 2a  ering the .    *
22db1 2a 20 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f  *        left-mo
22db2 73 74 20 4e 20 63 6f 6c 75 6d 6e 73 20 6f 6e 6c  st N columns onl
22db3 79 2c 20 77 68 65 72 65 20 4e 20 69 73 20 62 65  y, where N is be
22db4 74 77 65 65 6e 20 31 20 61 6e 64 20 6e 43 6f 6c  tween 1 and nCol
22db5 2c 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  , .    **       
22db6 20 69 6e 63 6c 75 73 69 76 65 2e 0a 20 20 20 20   inclusive..    
22db7 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69 4d 65  **.    **    iMe
22db8 6d 2b 6e 43 6f 6c 2b 31 20 2e 2e 20 4d 65 6d 2b  m+nCol+1 .. Mem+
22db9 32 2a 6e 43 6f 6c 3a 20 20 0a 20 20 20 20 2a 2a  2*nCol:  .    **
22dba 20 20 20 20 20 20 20 20 50 72 65 76 69 6f 75 73          Previous
22dbb 20 76 61 6c 75 65 20 6f 66 20 69 6e 64 65 78 65   value of indexe
22dbc 64 20 63 6f 6c 75 6d 6e 73 2c 20 66 72 6f 6d 20  d columns, from 
22dbd 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2e 0a 20  left to right.. 
22dbe 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c     **.    ** Cel
22dbf 6c 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20  ls iMem through 
22dc0 69 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e  iMem+nCol are in
22dc1 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20  itialized to 0. 
22dc2 54 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 0a  The others are .
22dc3 20 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a      ** initializ
22dc4 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 6e  ed to contain an
22dc5 20 53 51 4c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a   SQL NULL..    *
22dc6 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
22dc7 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <=nCol; i++){.  
22dc8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22dc9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
22dca 67 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b  ger, 0, iMem+i);
22dcb 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
22dcc 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
22dcd 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
22dce 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22dcf 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43  Null, 0, iMem+nC
22dd0 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a  ol+i+1);.    }..
22dd1 20 20 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65      /* Start the
22dd2 20 61 6e 61 6c 79 73 69 73 20 6c 6f 6f 70 2e 20   analysis loop. 
22dd3 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 74  This loop runs t
22dd4 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 65  hrough all the e
22dd5 6e 74 72 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a  ntries in.    **
22dd6 20 74 68 65 20 69 6e 64 65 78 20 62 2d 74 72 65   the index b-tre
22dd7 65 2e 20 20 2a 2f 0a 20 20 20 20 65 6e 64 4f 66  e.  */.    endOf
22dd8 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  Loop = sqlite3Vd
22dd9 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
22dda 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22ddb 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
22ddc 6e 64 2c 20 69 49 64 78 43 75 72 2c 20 65 6e 64  nd, iIdxCur, end
22ddd 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 74 6f 70  OfLoop);.    top
22dde 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33  OfLoop = sqlite3
22ddf 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
22de0 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  v);.    sqlite3V
22de1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22de2 41 64 64 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29  AddImm, iMem, 1)
22de3 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
22de4 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
22de5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22de6 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
22de7 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20  mn, iIdxCur, i, 
22de8 72 65 67 43 6f 6c 29 3b 0a 23 69 66 64 65 66 20  regCol);.#ifdef 
22de9 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
22dea 41 54 32 0a 20 20 20 20 20 20 69 66 28 20 69 3d  AT2.      if( i=
22deb 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
22dec 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72 65   Check if the re
22ded 63 6f 72 64 20 74 68 61 74 20 63 75 72 73 6f 72  cord that cursor
22dee 20 69 49 64 78 43 75 72 20 70 6f 69 6e 74 73 20   iIdxCur points 
22def 74 6f 20 63 6f 6e 74 61 69 6e 73 20 61 0a 20 20  to contains a.  
22df0 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 74        ** value t
22df1 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 73 74  hat should be st
22df2 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69  ored in the sqli
22df3 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 2e 20  te_stat2 table. 
22df4 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a  If so,.        *
22df5 2a 20 73 74 6f 72 65 20 69 74 2e 20 20 2a 2f 0a  * store it.  */.
22df6 20 20 20 20 20 20 20 20 69 6e 74 20 6e 65 20 3d          int ne =
22df7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22df8 70 33 28 76 2c 20 4f 50 5f 4e 65 2c 20 72 65 67  p3(v, OP_Ne, reg
22df9 52 65 63 6e 6f 2c 20 30 2c 20 72 65 67 53 61 6d  Recno, 0, regSam
22dfa 70 6c 65 72 65 63 6e 6f 29 3b 0a 20 20 20 20 20  plerecno);.     
22dfb 20 20 20 61 73 73 65 72 74 28 20 72 65 67 54 61     assert( regTa
22dfc 62 6e 61 6d 65 2b 31 3d 3d 72 65 67 49 64 78 6e  bname+1==regIdxn
22dfd 61 6d 65 20 0a 20 20 20 20 20 20 20 20 20 20 20  ame .           
22dfe 20 20 26 26 20 72 65 67 54 61 62 6e 61 6d 65 2b    && regTabname+
22dff 32 3d 3d 72 65 67 53 61 6d 70 6c 65 6e 6f 0a 20  2==regSampleno. 
22e00 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 72              && r
22e01 65 67 54 61 62 6e 61 6d 65 2b 33 3d 3d 72 65 67  egTabname+3==reg
22e02 43 6f 6c 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  Col.        );. 
22e03 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
22e04 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51  beChangeP5(v, SQ
22e05 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
22e06 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
22e07 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
22e08 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
22e09 67 54 61 62 6e 61 6d 65 2c 20 34 2c 20 72 65 67  gTabname, 4, reg
22e0a 52 65 63 2c 20 22 61 61 61 62 22 2c 20 30 29 3b  Rec, "aaab", 0);
22e0b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22e0c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22e0d 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53 74 61 74  _NewRowid, iStat
22e0e 43 75 72 2b 31 2c 20 72 65 67 52 6f 77 69 64 29  Cur+1, regRowid)
22e0f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
22e10 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22e11 50 5f 49 6e 73 65 72 74 2c 20 69 53 74 61 74 43  P_Insert, iStatC
22e12 75 72 2b 31 2c 20 72 65 67 52 65 63 2c 20 72 65  ur+1, regRec, re
22e13 67 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 20 20  gRowid);..      
22e14 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 6e    /* Calculate n
22e15 65 77 20 76 61 6c 75 65 73 20 66 6f 72 20 72 65  ew values for re
22e16 67 53 61 6d 70 6c 65 72 65 63 6e 6f 20 61 6e 64  gSamplerecno and
22e17 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2e 0a 20 20   regSampleno..  
22e18 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
22e19 20 2a 2a 20 20 20 73 61 6d 70 6c 65 6e 6f 20 3d   **   sampleno =
22e1a 20 73 61 6d 70 6c 65 6e 6f 20 2b 20 31 0a 20 20   sampleno + 1.  
22e1b 20 20 20 20 20 20 2a 2a 20 20 20 73 61 6d 70 6c        **   sampl
22e1c 65 72 65 63 6e 6f 20 3d 20 73 61 6d 70 6c 65 72  erecno = sampler
22e1d 65 63 6e 6f 2b 28 72 65 6d 61 69 6e 69 6e 67 20  ecno+(remaining 
22e1e 72 65 63 6f 72 64 73 29 2f 28 72 65 6d 61 69 6e  records)/(remain
22e1f 69 6e 67 20 73 61 6d 70 6c 65 73 29 0a 20 20 20  ing samples).   
22e20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
22e21 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22e22 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
22e23 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 31 29 3b  regSampleno, 1);
22e24 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22e25 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22e26 5f 53 75 62 74 72 61 63 74 2c 20 72 65 67 52 65  _Subtract, regRe
22e27 63 6e 6f 2c 20 72 65 67 4c 61 73 74 2c 20 72 65  cno, regLast, re
22e28 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 20 20  gTemp);.        
22e29 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22e2a 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
22e2b 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b 0a 20 20  regTemp, -1);.  
22e2c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22e2d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
22e2e 74 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e  teger, SQLITE_IN
22e2f 44 45 58 5f 53 41 4d 50 4c 45 53 2c 20 72 65 67  DEX_SAMPLES, reg
22e30 54 65 6d 70 32 29 3b 0a 20 20 20 20 20 20 20 20  Temp2);.        
22e31 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22e32 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74  3(v, OP_Subtract
22e33 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72  , regSampleno, r
22e34 65 67 54 65 6d 70 32 2c 20 72 65 67 54 65 6d 70  egTemp2, regTemp
22e35 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2);.        sqli
22e36 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
22e37 20 4f 50 5f 44 69 76 69 64 65 2c 20 72 65 67 54   OP_Divide, regT
22e38 65 6d 70 32 2c 20 72 65 67 54 65 6d 70 2c 20 72  emp2, regTemp, r
22e39 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 20  egTemp);.       
22e3a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22e3b 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c 20 72 65  p3(v, OP_Add, re
22e3c 67 53 61 6d 70 6c 65 72 65 63 6e 6f 2c 20 72 65  gSamplerecno, re
22e3d 67 54 65 6d 70 2c 20 72 65 67 53 61 6d 70 6c 65  gTemp, regSample
22e3e 72 65 63 6e 6f 29 3b 0a 0a 20 20 20 20 20 20 20  recno);..       
22e3f 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
22e40 48 65 72 65 28 76 2c 20 6e 65 29 3b 0a 20 20 20  Here(v, ne);.   
22e41 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22e42 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
22e43 49 6d 6d 2c 20 72 65 67 52 65 63 6e 6f 2c 20 31  Imm, regRecno, 1
22e44 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
22e45 66 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  f..      sqlite3
22e46 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22e47 5f 4e 65 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20  _Ne, regCol, 0, 
22e48 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a  iMem+nCol+i+1);.
22e49 20 20 20 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f        /**** TODO
22e4a 3a 20 20 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67  :  add collating
22e4b 20 73 65 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f   sequence *****/
22e4c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22e4d 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51  beChangeP5(v, SQ
22e4e 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
22e4f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
22e50 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
22e51 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
22e52 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  a malloc failure
22e53 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20 74   has occurred, t
22e54 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f  hen the result o
22e55 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
22e56 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 73 73 65   .      ** passe
22e57 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
22e58 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
22e59 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56  call to sqlite3V
22e5a 64 62 65 4a 75 6d 70 48 65 72 65 28 29 20 0a 20  dbeJumpHere() . 
22e5b 20 20 20 20 20 2a 2a 20 62 65 6c 6f 77 20 6d 61       ** below ma
22e5c 79 20 62 65 20 6e 65 67 61 74 69 76 65 2e 20 57  y be negative. W
22e5d 68 69 63 68 20 63 61 75 73 65 73 20 61 6e 20 61  hich causes an a
22e5e 73 73 65 72 74 28 29 20 74 6f 20 66 61 69 6c 20  ssert() to fail 
22e5f 28 6f 72 20 61 6e 0a 20 20 20 20 20 20 2a 2a 20  (or an.      ** 
22e60 6f 75 74 2d 6f 66 2d 62 6f 75 6e 64 73 20 77 72  out-of-bounds wr
22e61 69 74 65 20 69 66 20 53 51 4c 49 54 45 5f 44 45  ite if SQLITE_DE
22e62 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  BUG is not defin
22e63 65 64 29 2e 20 20 2a 2f 0a 20 20 20 20 20 20 72  ed).  */.      r
22e64 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
22e65 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22e66 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
22e67 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20  , endOfLoop);.  
22e68 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
22e69 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  l; i++){.      s
22e6a 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
22e6b 72 65 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62  re(v, sqlite3Vdb
22e6c 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d  eCurrentAddr(v)-
22e6d 28 6e 43 6f 6c 2a 32 29 29 3b 0a 20 20 20 20 20  (nCol*2));.     
22e6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22e6f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
22e70 20 69 4d 65 6d 2b 69 2b 31 2c 20 31 29 3b 0a 20   iMem+i+1, 1);. 
22e71 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22e72 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
22e73 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 69 2c  umn, iIdxCur, i,
22e74 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b   iMem+nCol+i+1);
22e75 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45  .    }..    /* E
22e76 6e 64 20 6f 66 20 74 68 65 20 61 6e 61 6c 79 73  nd of the analys
22e77 69 73 20 6c 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20  is loop. */.    
22e78 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
22e79 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66  veLabel(v, endOf
22e7a 4c 6f 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  Loop);.    sqlit
22e7b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22e7c 4f 50 5f 4e 65 78 74 2c 20 69 49 64 78 43 75 72  OP_Next, iIdxCur
22e7d 2c 20 74 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20  , topOfLoop);.  
22e7e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22e7f 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
22e80 20 69 49 64 78 43 75 72 29 3b 0a 0a 20 20 20 20   iIdxCur);..    
22e81 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 73  /* Store the res
22e82 75 6c 74 73 20 69 6e 20 73 71 6c 69 74 65 5f 73  ults in sqlite_s
22e83 74 61 74 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  tat1..    **.   
22e84 20 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69   ** The result i
22e85 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  s a single row o
22e86 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
22e87 74 31 20 74 61 62 6c 65 2e 20 20 54 68 65 20 66  t1 table.  The f
22e88 69 72 73 74 0a 20 20 20 20 2a 2a 20 74 77 6f 20  irst.    ** two 
22e89 63 6f 6c 75 6d 6e 73 20 61 72 65 20 74 68 65 20  columns are the 
22e8a 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 74 61 62  names of the tab
22e8b 6c 65 20 61 6e 64 20 69 6e 64 65 78 2e 20 20 54  le and index.  T
22e8c 68 65 20 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0a  he third column.
22e8d 20 20 20 20 2a 2a 20 69 73 20 61 20 73 74 72 69      ** is a stri
22e8e 6e 67 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61  ng composed of a
22e8f 20 6c 69 73 74 20 6f 66 20 69 6e 74 65 67 65 72   list of integer
22e90 20 73 74 61 74 69 73 74 69 63 73 20 61 62 6f 75   statistics abou
22e91 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64  t the.    ** ind
22e92 65 78 2e 20 20 54 68 65 20 66 69 72 73 74 20 69  ex.  The first i
22e93 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69  nteger in the li
22e94 73 74 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  st is the total 
22e95 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
22e96 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20  s.    ** in the 
22e97 69 6e 64 65 78 2e 20 20 54 68 65 72 65 20 69 73  index.  There is
22e98 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20   one additional 
22e99 69 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c  integer in the l
22e9a 69 73 74 20 66 6f 72 20 65 61 63 68 0a 20 20 20  ist for each.   
22e9b 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68   ** column of th
22e9c 65 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 61  e table.  This a
22e9d 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65  dditional intege
22e9e 72 20 69 73 20 61 20 67 75 65 73 73 20 6f 66 20  r is a guess of 
22e9f 68 6f 77 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20  how many.    ** 
22ea0 72 6f 77 73 20 6f 66 20 74 68 65 20 74 61 62 6c  rows of the tabl
22ea1 65 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c  e the index will
22ea2 20 73 65 6c 65 63 74 2e 20 20 49 66 20 44 20 69   select.  If D i
22ea3 73 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 64  s the count of d
22ea4 69 73 74 69 6e 63 74 0a 20 20 20 20 2a 2a 20 76  istinct.    ** v
22ea5 61 6c 75 65 73 20 61 6e 64 20 4b 20 69 73 20 74  alues and K is t
22ea6 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
22ea7 6f 66 20 72 6f 77 73 2c 20 74 68 65 6e 20 74 68  of rows, then th
22ea8 65 20 69 6e 74 65 67 65 72 20 69 73 20 63 6f 6d  e integer is com
22ea9 70 75 74 65 64 0a 20 20 20 20 2a 2a 20 61 73 3a  puted.    ** as:
22eaa 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
22eab 20 20 20 20 20 20 49 20 3d 20 28 4b 2b 44 2d 31        I = (K+D-1
22eac 29 2f 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  )/D.    **.    *
22ead 2a 20 49 66 20 4b 3d 3d 30 20 74 68 65 6e 20 6e  * If K==0 then n
22eae 6f 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20  o entry is made 
22eaf 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f  into the sqlite_
22eb0 73 74 61 74 31 20 74 61 62 6c 65 2e 20 20 0a 20  stat1 table.  . 
22eb1 20 20 20 2a 2a 20 49 66 20 4b 3e 30 20 74 68 65     ** If K>0 the
22eb2 6e 20 69 74 20 69 73 20 61 6c 77 61 79 73 20 74  n it is always t
22eb3 68 65 20 63 61 73 65 20 74 68 65 20 44 3e 30 20  he case the D>0 
22eb4 73 6f 20 64 69 76 69 73 69 6f 6e 20 62 79 20 7a  so division by z
22eb5 65 72 6f 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65  ero.    ** is ne
22eb6 76 65 72 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20  ver possible..  
22eb7 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20    */.    addr = 
22eb8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22eb9 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 69  1(v, OP_IfNot, i
22eba 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Mem);.    sqlite
22ebb 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22ebc 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 72  P_SCopy, iMem, r
22ebd 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20  egSampleno);.   
22ebe 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
22ebf 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
22ec0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
22ec1 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
22ec2 2c 20 72 65 67 54 65 6d 70 2c 20 30 2c 20 22 20  , regTemp, 0, " 
22ec3 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  ", 0);.      sql
22ec4 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22ec5 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67  , OP_Concat, reg
22ec6 54 65 6d 70 2c 20 72 65 67 53 61 6d 70 6c 65 6e  Temp, regSamplen
22ec7 6f 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b  o, regSampleno);
22ec8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22ec9 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41  beAddOp3(v, OP_A
22eca 64 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69  dd, iMem, iMem+i
22ecb 2b 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20  +1, regTemp);.  
22ecc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22ecd 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
22ece 6d 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29  mm, regTemp, -1)
22ecf 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
22ed0 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
22ed1 44 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31  Divide, iMem+i+1
22ed2 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65  , regTemp, regTe
22ed3 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  mp);.      sqlit
22ed4 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
22ed5 4f 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d  OP_ToInt, regTem
22ed6 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
22ed7 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22ed8 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d  P_Concat, regTem
22ed9 70 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20  p, regSampleno, 
22eda 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20  regSampleno);.  
22edb 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
22edc 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
22edd 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54  MakeRecord, regT
22ede 61 62 6e 61 6d 65 2c 20 33 2c 20 72 65 67 52 65  abname, 3, regRe
22edf 63 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a 20 20  c, "aaa", 0);.  
22ee0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22ee1 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  Op2(v, OP_NewRow
22ee2 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20 72 65  id, iStatCur, re
22ee3 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  gRowid);.    sql
22ee4 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22ee5 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 53 74  , OP_Insert, iSt
22ee6 61 74 43 75 72 2c 20 72 65 67 52 65 63 2c 20 72  atCur, regRec, r
22ee7 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  egRowid);.    sq
22ee8 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
22ee9 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45  5(v, OPFLAG_APPE
22eea 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ND);.    sqlite3
22eeb 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
22eec 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  addr);.  }.}../*
22eed 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
22eee 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61 75 73  e that will caus
22eef 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  e the most recen
22ef0 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 69 73  t index analysis
22ef1 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64 65 64   to.** be laoded
22ef2 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68   into internal h
22ef3 61 73 68 20 74 61 62 6c 65 73 20 77 68 65 72 65  ash tables where
22ef4 20 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e   is can be used.
22ef5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
22ef6 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 50 61 72  loadAnalysis(Par
22ef7 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
22ef8 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  iDb){.  Vdbe *v 
22ef9 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
22efa 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
22efb 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
22efc 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
22efd 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c 20 69  _LoadAnalysis, i
22efe 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  Db);.  }.}../*.*
22eff 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
22f00 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20  that will do an 
22f01 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e 20 65  analysis of an e
22f02 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 0a 2a  ntire database.*
22f03 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e  /.static void an
22f04 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 50 61  alyzeDatabase(Pa
22f05 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
22f06 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 33   iDb){.  sqlite3
22f07 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
22f08 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63  b;.  Schema *pSc
22f09 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69  hema = db->aDb[i
22f0a 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 20 20  Db].pSchema;    
22f0b 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 61 74  /* Schema of dat
22f0c 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20 48  abase iDb */.  H
22f0d 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69 6e  ashElem *k;.  in
22f0e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20 69 6e  t iStatCur;.  in
22f0f 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69 74  t iMem;..  sqlit
22f10 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
22f11 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
22f12 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43 75   iDb);.  iStatCu
22f13 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
22f14 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  ;.  pParse->nTab
22f15 20 2b 3d 20 32 3b 0a 20 20 6f 70 65 6e 53 74 61   += 2;.  openSta
22f16 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  tTable(pParse, i
22f17 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29  Db, iStatCur, 0)
22f18 3b 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73  ;.  iMem = pPars
22f19 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72  e->nMem+1;.  for
22f1a 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (k=sqliteHashFir
22f1b 73 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c  st(&pSchema->tbl
22f1c 48 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69  Hash); k; k=sqli
22f1d 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a  teHashNext(k)){.
22f1e 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
22f1f 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65  = (Table*)sqlite
22f20 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20  HashData(k);.   
22f21 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65   analyzeOneTable
22f22 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69  (pParse, pTab, i
22f23 53 74 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a  StatCur, iMem);.
22f24 20 20 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73    }.  loadAnalys
22f25 69 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  is(pParse, iDb);
22f26 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
22f27 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
22f28 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73  l do an analysis
22f29 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62   of a single tab
22f2a 6c 65 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62  le in.** a datab
22f2b 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ase..*/.static v
22f2c 6f 69 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65  oid analyzeTable
22f2d 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
22f2e 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20  Table *pTab){.  
22f2f 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69  int iDb;.  int i
22f30 53 74 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65  StatCur;..  asse
22f31 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20  rt( pTab!=0 );. 
22f32 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
22f33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
22f34 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29  exes(pParse->db)
22f35 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   );.  iDb = sqli
22f36 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
22f37 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
22f38 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73  b->pSchema);.  s
22f39 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
22f3a 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
22f3b 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74  , 0, iDb);.  iSt
22f3c 61 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e  atCur = pParse->
22f3d 6e 54 61 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e  nTab;.  pParse->
22f3e 6e 54 61 62 20 2b 3d 20 32 3b 0a 20 20 6f 70 65  nTab += 2;.  ope
22f3f 6e 53 74 61 74 54 61 62 6c 65 28 70 50 61 72 73  nStatTable(pPars
22f40 65 2c 20 69 44 62 2c 20 69 53 74 61 74 43 75 72  e, iDb, iStatCur
22f41 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
22f42 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c    analyzeOneTabl
22f43 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
22f44 69 53 74 61 74 43 75 72 2c 20 70 50 61 72 73 65  iStatCur, pParse
22f45 2d 3e 6e 4d 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61  ->nMem+1);.  loa
22f46 64 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65  dAnalysis(pParse
22f47 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  , iDb);.}../*.**
22f48 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
22f49 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63  or the ANALYZE c
22f4a 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20 70 61 72  ommand.  The par
22f4b 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
22f4c 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65 6e 20 69  outine.** when i
22f4d 74 20 72 65 63 6f 67 6e 69 7a 65 73 20 61 6e 20  t recognizes an 
22f4e 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e  ANALYZE command.
22f4f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e  .**.**        AN
22f50 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20 20  ALYZE           
22f51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f52 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20   -- 1.**        
22f53 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74 61 62 61  ANALYZE  <databa
22f54 73 65 3e 20 20 20 20 20 20 20 20 20 20 20 20 20  se>             
22f55 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20     -- 2.**      
22f56 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c 64 61 74    ANALYZE  ?<dat
22f57 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61  abase>.?<tablena
22f58 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20  me>  -- 3.**.** 
22f59 46 6f 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c  Form 1 causes al
22f5a 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c  l indices in all
22f5b 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
22f5c 73 65 73 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  ses to be analyz
22f5d 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 61 6e  ed..** Form 2 an
22f5e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63  alyzes all indic
22f5f 65 73 20 74 68 65 20 73 69 6e 67 6c 65 20 64 61  es the single da
22f60 74 61 62 61 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a  tabase named..**
22f61 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79 7a 65 73   Form 3 analyzes
22f62 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73   all indices ass
22f63 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
22f64 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f   named table..*/
22f65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22f66 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c  void sqlite3Anal
22f67 79 7a 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  yze(Parse *pPars
22f68 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  e, Token *pName1
22f69 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29  , Token *pName2)
22f6a 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
22f6b 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
22f6c 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69  int iDb;.  int i
22f6d 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44  ;.  char *z, *zD
22f6e 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  b;.  Table *pTab
22f6f 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c  ;.  Token *pTabl
22f70 65 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 52 65 61  eName;..  /* Rea
22f71 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
22f72 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72  chema. If an err
22f73 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65  or occurs, leave
22f74 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
22f75 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20  e.  ** and code 
22f76 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65  in pParse and re
22f77 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  turn NULL. */.  
22f78 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
22f79 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
22f7a 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20  xes(pParse->db) 
22f7b 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
22f7c 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53  OK!=sqlite3ReadS
22f7d 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
22f7e 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
22f7f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d  ..  assert( pNam
22f80 65 32 21 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 3d  e2!=0 || pName1=
22f81 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d  =0 );.  if( pNam
22f82 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  e1==0 ){.    /* 
22f83 46 6f 72 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65  Form 1:  Analyze
22f84 20 65 76 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20   everything */. 
22f85 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
22f86 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
22f87 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f     if( i==1 ) co
22f88 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e  ntinue;  /* Do n
22f89 6f 74 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 54  ot analyze the T
22f8a 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  EMP database */.
22f8b 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74        analyzeDat
22f8c 61 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 29  abase(pParse, i)
22f8d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
22f8e 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30  if( pName2->n==0
22f8f 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20   ){.    /* Form 
22f90 32 3a 20 20 41 6e 61 6c 79 7a 65 20 74 68 65 20  2:  Analyze the 
22f91 64 61 74 61 62 61 73 65 20 6f 72 20 74 61 62 6c  database or tabl
22f92 65 20 6e 61 6d 65 64 20 2a 2f 0a 20 20 20 20 69  e named */.    i
22f93 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  Db = sqlite3Find
22f94 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a  Db(db, pName1);.
22f95 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29      if( iDb>=0 )
22f96 7b 0a 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 44  {.      analyzeD
22f97 61 74 61 62 61 73 65 28 70 50 61 72 73 65 2c 20  atabase(pParse, 
22f98 69 44 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  iDb);.    }else{
22f99 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  .      z = sqlit
22f9a 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
22f9b 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20  db, pName1);.   
22f9c 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20     if( z ){.    
22f9d 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74      pTab = sqlit
22f9e 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50  e3LocateTable(pP
22f9f 61 72 73 65 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a  arse, 0, z, 0);.
22fa0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
22fa1 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20  bFree(db, z);.  
22fa2 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29        if( pTab )
22fa3 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c  {.          anal
22fa4 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  yzeTable(pParse,
22fa5 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20   pTab);.        
22fa6 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
22fa7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
22fa8 46 6f 72 6d 20 33 3a 20 41 6e 61 6c 79 7a 65 20  Form 3: Analyze 
22fa9 74 68 65 20 66 75 6c 6c 79 20 71 75 61 6c 69 66  the fully qualif
22faa 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a  ied table name *
22fab 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69  /.    iDb = sqli
22fac 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70  te3TwoPartName(p
22fad 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
22fae 4e 61 6d 65 32 2c 20 26 70 54 61 62 6c 65 4e 61  Name2, &pTableNa
22faf 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62  me);.    if( iDb
22fb0 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 44 62  >=0 ){.      zDb
22fb1 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
22fb2 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7a 20 3d  zName;.      z =
22fb3 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
22fb4 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65  Token(db, pTable
22fb5 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Name);.      if(
22fb6 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54   z ){.        pT
22fb7 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
22fb8 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
22fb9 30 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20  0, z, zDb);.    
22fba 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
22fbb 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20  e(db, z);.      
22fbc 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
22fbd 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54          analyzeT
22fbe 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
22fbf 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  b);.        }.  
22fc0 20 20 20 20 7d 0a 20 20 20 20 7d 20 20 20 0a 20      }.    }   . 
22fc1 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64   }.}../*.** Used
22fc2 20 74 6f 20 70 61 73 73 20 69 6e 66 6f 72 6d 61   to pass informa
22fc3 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 61 6e  tion from the an
22fc4 61 6c 79 7a 65 72 20 72 65 61 64 65 72 20 74 68  alyzer reader th
22fc5 72 6f 75 67 68 20 74 6f 20 74 68 65 0a 2a 2a 20  rough to the.** 
22fc6 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
22fc7 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
22fc8 75 63 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f  uct analysisInfo
22fc9 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 3b 0a 73   analysisInfo;.s
22fca 74 72 75 63 74 20 61 6e 61 6c 79 73 69 73 49 6e  truct analysisIn
22fcb 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  fo {.  sqlite3 *
22fcc 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  db;.  const char
22fcd 20 2a 7a 44 61 74 61 62 61 73 65 3b 0a 7d 3b 0a   *zDatabase;.};.
22fce 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c  ./*.** This call
22fcf 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
22fd0 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e  once for each in
22fd1 64 65 78 20 77 68 65 6e 20 72 65 61 64 69 6e 67  dex when reading
22fd2 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 73   the.** sqlite_s
22fd3 74 61 74 31 20 74 61 62 6c 65 2e 20 20 0a 2a 2a  tat1 table.  .**
22fd4 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 30 5d 20  .**     argv[0] 
22fd5 3d 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e  = name of the in
22fd6 64 65 78 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b  dex.**     argv[
22fd7 31 5d 20 3d 20 72 65 73 75 6c 74 73 20 6f 66 20  1] = results of 
22fd8 61 6e 61 6c 79 73 69 73 20 2d 20 6f 6e 20 69 6e  analysis - on in
22fd9 74 65 67 65 72 20 66 6f 72 20 65 61 63 68 20 63  teger for each c
22fda 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20  olumn.*/.static 
22fdb 69 6e 74 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64  int analysisLoad
22fdc 65 72 28 76 6f 69 64 20 2a 70 44 61 74 61 2c 20  er(void *pData, 
22fdd 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a  int argc, char *
22fde 2a 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f  *argv, char **No
22fdf 74 55 73 65 64 29 7b 0a 20 20 61 6e 61 6c 79 73  tUsed){.  analys
22fe0 69 73 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20  isInfo *pInfo = 
22fe1 28 61 6e 61 6c 79 73 69 73 49 6e 66 6f 2a 29 70  (analysisInfo*)p
22fe2 44 61 74 61 3b 0a 20 20 49 6e 64 65 78 20 2a 70  Data;.  Index *p
22fe3 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20 69 2c 20  Index;.  int i, 
22fe4 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  c;.  unsigned in
22fe5 74 20 76 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  t v;.  const cha
22fe6 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28  r *z;..  assert(
22fe7 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 55 4e   argc==2 );.  UN
22fe8 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
22fe9 4e 6f 74 55 73 65 64 2c 20 61 72 67 63 29 3b 0a  NotUsed, argc);.
22fea 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30 20 7c  .  if( argv==0 |
22feb 7c 20 61 72 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20  | argv[0]==0 || 
22fec 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20  argv[1]==0 ){.  
22fed 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
22fee 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74    pIndex = sqlit
22fef 65 33 46 69 6e 64 49 6e 64 65 78 28 70 49 6e 66  e3FindIndex(pInf
22ff0 6f 2d 3e 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20  o->db, argv[0], 
22ff1 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 62 61 73 65  pInfo->zDatabase
22ff2 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 3d  );.  if( pIndex=
22ff3 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
22ff4 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 61 72   0;.  }.  z = ar
22ff5 67 76 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30  gv[1];.  for(i=0
22ff6 3b 20 2a 7a 20 26 26 20 69 3c 3d 70 49 6e 64 65  ; *z && i<=pInde
22ff7 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
22ff8 7b 0a 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20  {.    v = 0;.   
22ff9 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b 30 5d 29   while( (c=z[0])
22ffa 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20  >='0' && c<='9' 
22ffb 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 76 2a 31  ){.      v = v*1
22ffc 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20  0 + c - '0';.   
22ffd 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     z++;.    }.  
22ffe 20 20 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45    pIndex->aiRowE
22fff 73 74 5b 69 5d 20 3d 20 76 3b 0a 20 20 20 20 69  st[i] = v;.    i
23000 66 28 20 2a 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b  f( *z==' ' ) z++
23001 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
23002 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
23003 65 20 49 6e 64 65 78 2e 61 53 61 6d 70 6c 65 20  e Index.aSample 
23004 76 61 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20  variable is not 
23005 4e 55 4c 4c 2c 20 64 65 6c 65 74 65 20 74 68 65  NULL, delete the
23006 20 61 53 61 6d 70 6c 65 5b 5d 20 61 72 72 61 79   aSample[] array
23007 0a 2a 2a 20 61 6e 64 20 69 74 73 20 63 6f 6e 74  .** and its cont
23008 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
23009 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2300a 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53  ite3DeleteIndexS
2300b 61 6d 70 6c 65 73 28 49 6e 64 65 78 20 2a 70 49  amples(Index *pI
2300c 64 78 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  dx){.#ifdef SQLI
2300d 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a  TE_ENABLE_STAT2.
2300e 20 20 69 66 28 20 70 49 64 78 2d 3e 61 53 61 6d    if( pIdx->aSam
2300f 70 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a  ple ){.    int j
23010 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ;.    sqlite3 *d
23011 62 4d 65 6d 20 3d 20 70 49 64 78 2d 3e 70 54 61  bMem = pIdx->pTa
23012 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 20 20  ble->dbMem;.    
23013 66 6f 72 28 6a 3d 30 3b 20 6a 3c 53 51 4c 49 54  for(j=0; j<SQLIT
23014 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b  E_INDEX_SAMPLES;
23015 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 49 6e 64   j++){.      Ind
23016 65 78 53 61 6d 70 6c 65 20 2a 70 20 3d 20 26 70  exSample *p = &p
23017 49 64 78 2d 3e 61 53 61 6d 70 6c 65 5b 6a 5d 3b  Idx->aSample[j];
23018 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65 54  .      if( p->eT
23019 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54  ype==SQLITE_TEXT
2301a 20 7c 7c 20 70 2d 3e 65 54 79 70 65 3d 3d 53 51   || p->eType==SQ
2301b 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20  LITE_BLOB ){.   
2301c 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2301d 65 65 28 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d  ee(pIdx->pTable-
2301e 3e 64 62 4d 65 6d 2c 20 70 2d 3e 75 2e 7a 29 3b  >dbMem, p->u.z);
2301f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
23020 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23021 28 64 62 4d 65 6d 2c 20 70 49 64 78 2d 3e 61 53  (dbMem, pIdx->aS
23022 61 6d 70 6c 65 29 3b 0a 20 20 20 20 70 49 64 78  ample);.    pIdx
23023 2d 3e 61 53 61 6d 70 6c 65 20 3d 20 30 3b 0a 20  ->aSample = 0;. 
23024 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45   }.#else.  UNUSE
23025 44 5f 50 41 52 41 4d 45 54 45 52 28 70 49 64 78  D_PARAMETER(pIdx
23026 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  );.#endif.}../*.
23027 2a 2a 20 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74  ** Load the cont
23028 65 6e 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ent of the sqlit
23029 65 5f 73 74 61 74 31 20 61 6e 64 20 73 71 6c 69  e_stat1 and sqli
2302a 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 73 2e  te_stat2 tables.
2302b 20 54 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73   The.** contents
2302c 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74 31   of sqlite_stat1
2302d 20 61 72 65 20 75 73 65 64 20 74 6f 20 70 6f 70   are used to pop
2302e 75 6c 61 74 65 20 74 68 65 20 49 6e 64 65 78 2e  ulate the Index.
2302f 61 69 52 6f 77 45 73 74 5b 5d 0a 2a 2a 20 61 72  aiRowEst[].** ar
23030 72 61 79 73 2e 20 54 68 65 20 63 6f 6e 74 65 6e  rays. The conten
23031 74 73 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61  ts of sqlite_sta
23032 74 32 20 61 72 65 20 75 73 65 64 20 74 6f 20 70  t2 are used to p
23033 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 49  opulate the.** I
23034 6e 64 65 78 2e 61 53 61 6d 70 6c 65 5b 5d 20 61  ndex.aSample[] a
23035 72 72 61 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rrays..**.** If 
23036 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
23037 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 70 72   table is not pr
23038 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74  esent in the dat
23039 61 62 61 73 65 2c 20 53 51 4c 49 54 45 5f 45 52  abase, SQLITE_ER
2303a 52 4f 52 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ROR.** is return
2303b 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
2303c 2c 20 65 76 65 6e 20 69 66 20 53 51 4c 49 54 45  , even if SQLITE
2303d 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 77 61  _ENABLE_STAT2 wa
2303e 73 20 64 65 66 69 6e 65 64 20 0a 2a 2a 20 64 75  s defined .** du
2303f 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  ring compilation
23040 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 5f   and the sqlite_
23041 73 74 61 74 32 20 74 61 62 6c 65 20 69 73 20 70  stat2 table is p
23042 72 65 73 65 6e 74 2c 20 6e 6f 20 64 61 74 61 20  resent, no data 
23043 69 73 20 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d  is .** read from
23044 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51   it..**.** If SQ
23045 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
23046 32 20 77 61 73 20 64 65 66 69 6e 65 64 20 64 75  2 was defined du
23047 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  ring compilation
23048 20 61 6e 64 20 74 68 65 20 0a 2a 2a 20 73 71 6c   and the .** sql
23049 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20  ite_stat2 table 
2304a 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69  is not present i
2304b 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
2304c 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 0a  SQLITE_ERROR is.
2304d 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 48 6f 77  ** returned. How
2304e 65 76 65 72 2c 20 69 6e 20 74 68 69 73 20 63 61  ever, in this ca
2304f 73 65 2c 20 64 61 74 61 20 69 73 20 72 65 61 64  se, data is read
23050 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65   from the sqlite
23051 5f 73 74 61 74 31 0a 2a 2a 20 74 61 62 6c 65 20  _stat1.** table 
23052 28 69 66 20 69 74 20 69 73 20 70 72 65 73 65 6e  (if it is presen
23053 74 29 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  t) before return
23054 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ing..**.** If an
23055 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72   OOM error occur
23056 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  s, this function
23057 20 61 6c 77 61 79 73 20 73 65 74 73 20 64 62 2d   always sets db-
23058 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a  >mallocFailed..*
23059 2a 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20  * This means if 
2305a 74 68 65 20 63 61 6c 6c 65 72 20 64 6f 65 73 20  the caller does 
2305b 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 6f  not care about o
2305c 74 68 65 72 20 65 72 72 6f 72 73 2c 20 74 68 65  ther errors, the
2305d 20 72 65 74 75 72 6e 0a 2a 2a 20 63 6f 64 65 20   return.** code 
2305e 6d 61 79 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a  may be ignored..
2305f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23060 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61  E int sqlite3Ana
23061 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65  lysisLoad(sqlite
23062 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b  3 *db, int iDb){
23063 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20  .  analysisInfo 
23064 73 49 6e 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65  sInfo;.  HashEle
23065 6d 20 2a 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53  m *i;.  char *zS
23066 71 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  ql;.  int rc;.. 
23067 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
23068 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
23069 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
2306a 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20  aDb[iDb].pBt!=0 
2306b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
2306c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
2306d 74 65 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d  tex(db->aDb[iDb]
2306e 2e 70 42 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 43  .pBt) );..  /* C
2306f 6c 65 61 72 20 61 6e 79 20 70 72 69 6f 72 20 73  lear any prior s
23070 74 61 74 69 73 74 69 63 73 20 2a 2f 0a 20 20 66  tatistics */.  f
23071 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46  or(i=sqliteHashF
23072 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 44  irst(&db->aDb[iD
23073 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  b].pSchema->idxH
23074 61 73 68 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48  ash);i;i=sqliteH
23075 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20  ashNext(i)){.   
23076 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73   Index *pIdx = s
23077 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
23078 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 66  ;.    sqlite3Def
23079 61 75 6c 74 52 6f 77 45 73 74 28 70 49 64 78 29  aultRowEst(pIdx)
2307a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  ;.    sqlite3Del
2307b 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28  eteIndexSamples(
2307c 70 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  pIdx);.  }..  /*
2307d 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73   Check to make s
2307e 75 72 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73  ure the sqlite_s
2307f 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74  tat1 table exist
23080 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e 64 62 20  s */.  sInfo.db 
23081 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44  = db;.  sInfo.zD
23082 61 74 61 62 61 73 65 20 3d 20 64 62 2d 3e 61 44  atabase = db->aD
23083 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
23084 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54  if( sqlite3FindT
23085 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65  able(db, "sqlite
23086 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 6f 2e 7a  _stat1", sInfo.z
23087 44 61 74 61 62 61 73 65 29 3d 3d 30 20 29 7b 0a  Database)==0 ){.
23088 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
23089 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20  E_ERROR;.  }..  
2308a 2f 2a 20 4c 6f 61 64 20 6e 65 77 20 73 74 61 74  /* Load new stat
2308b 69 73 74 69 63 73 20 6f 75 74 20 6f 66 20 74 68  istics out of th
2308c 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
2308d 61 62 6c 65 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d  able */.  zSql =
2308e 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2308f 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45  db, .      "SELE
23090 43 54 20 69 64 78 2c 20 73 74 61 74 20 46 52 4f  CT idx, stat FRO
23091 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74  M %Q.sqlite_stat
23092 31 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62  1", sInfo.zDatab
23093 61 73 65 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c  ase);.  if( zSql
23094 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
23095 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
23096 7d 65 6c 73 65 7b 0a 20 20 20 20 28 76 6f 69 64  }else{.    (void
23097 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
23098 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20  f(db);.    rc = 
23099 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
2309a 20 7a 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c   zSql, analysisL
2309b 6f 61 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30  oader, &sInfo, 0
2309c 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  );.    (void)sql
2309d 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
2309e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
2309f 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20  ree(db, zSql);. 
230a0 20 7d 0a 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74   }...  /* Load t
230a1 68 65 20 73 74 61 74 69 73 74 69 63 73 20 66 72  he statistics fr
230a2 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  om the sqlite_st
230a3 61 74 32 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 69  at2 table. */.#i
230a4 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
230a5 4c 45 5f 53 54 41 54 32 0a 20 20 69 66 28 20 72  LE_STAT2.  if( r
230a6 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
230a7 21 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c  !sqlite3FindTabl
230a8 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74  e(db, "sqlite_st
230a9 61 74 32 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74  at2", sInfo.zDat
230aa 61 62 61 73 65 29 20 29 7b 0a 20 20 20 20 72 63  abase) ){.    rc
230ab 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
230ac 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
230ad 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
230ae 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
230af 74 6d 74 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 53  tmt = 0;..    zS
230b0 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  ql = sqlite3MPri
230b1 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20  ntf(db, .       
230b2 20 22 53 45 4c 45 43 54 20 69 64 78 2c 73 61 6d   "SELECT idx,sam
230b3 70 6c 65 6e 6f 2c 73 61 6d 70 6c 65 20 46 52 4f  pleno,sample FRO
230b4 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74  M %Q.sqlite_stat
230b5 32 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62  2", sInfo.zDatab
230b6 61 73 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  ase);.    if( !z
230b7 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Sql ){.      rc 
230b8 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
230b9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
230ba 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
230bb 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
230bc 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
230bd 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c  prepare(db, zSql
230be 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29  , -1, &pStmt, 0)
230bf 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71  ;.      (void)sq
230c0 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
230c1 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
230c2 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29  DbFree(db, zSql)
230c3 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
230c4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
230c5 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71  {.      (void)sq
230c6 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
230c7 62 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  b);.      while(
230c8 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53   sqlite3_step(pS
230c9 74 6d 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  tmt)==SQLITE_ROW
230ca 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72   ){.        char
230cb 20 2a 7a 49 6e 64 65 78 20 3d 20 28 63 68 61 72   *zIndex = (char
230cc 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   *)sqlite3_colum
230cd 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 30 29  n_text(pStmt, 0)
230ce 3b 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78 20  ;.        Index 
230cf 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46  *pIdx = sqlite3F
230d0 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 49 6e  indIndex(db, zIn
230d1 64 65 78 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61  dex, sInfo.zData
230d2 62 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 69  base);.        i
230d3 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
230d4 20 20 20 20 20 69 6e 74 20 69 53 61 6d 70 6c 65       int iSample
230d5 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
230d6 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 31 29 3b  n_int(pStmt, 1);
230d7 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
230d8 65 33 20 2a 64 62 4d 65 6d 20 3d 20 70 49 64 78  e3 *dbMem = pIdx
230d9 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b  ->pTable->dbMem;
230da 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
230db 74 28 20 64 62 4d 65 6d 3d 3d 64 62 20 7c 7c 20  t( dbMem==db || 
230dc 64 62 4d 65 6d 3d 3d 30 20 29 3b 0a 20 20 20 20  dbMem==0 );.    
230dd 20 20 20 20 20 20 69 66 28 20 69 53 61 6d 70 6c        if( iSampl
230de 65 3c 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53  e<SQLITE_INDEX_S
230df 41 4d 50 4c 45 53 20 26 26 20 69 53 61 6d 70 6c  AMPLES && iSampl
230e0 65 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e>=0 ){.        
230e1 20 20 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20      int eType = 
230e2 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
230e3 79 70 65 28 70 53 74 6d 74 2c 20 32 29 3b 0a 0a  ype(pStmt, 2);..
230e4 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
230e5 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30  pIdx->aSample==0
230e6 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
230e7 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
230e8 6e 74 20 73 7a 20 3d 20 73 69 7a 65 6f 66 28 49  nt sz = sizeof(I
230e9 6e 64 65 78 53 61 6d 70 6c 65 29 2a 53 51 4c 49  ndexSample)*SQLI
230ea 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
230eb 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
230ec 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20 3d 20  pIdx->aSample = 
230ed 28 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 29 73  (IndexSample *)s
230ee 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
230ef 72 6f 28 64 62 4d 65 6d 2c 20 73 7a 29 3b 0a 20  ro(dbMem, sz);. 
230f0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
230f1 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d   pIdx->aSample==
230f2 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
230f3 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
230f4 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
230f5 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
230f6 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
230f7 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  }.            }.
230f8 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
230f9 65 72 74 28 20 70 49 64 78 2d 3e 61 53 61 6d 70  ert( pIdx->aSamp
230fa 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  le );.          
230fb 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20    {.            
230fc 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 70    IndexSample *p
230fd 53 61 6d 70 6c 65 20 3d 20 26 70 49 64 78 2d 3e  Sample = &pIdx->
230fe 61 53 61 6d 70 6c 65 5b 69 53 61 6d 70 6c 65 5d  aSample[iSample]
230ff 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
23100 70 53 61 6d 70 6c 65 2d 3e 65 54 79 70 65 20 3d  pSample->eType =
23101 20 28 75 38 29 65 54 79 70 65 3b 0a 20 20 20 20   (u8)eType;.    
23102 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 54            if( eT
23103 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45  ype==SQLITE_INTE
23104 47 45 52 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51  GER || eType==SQ
23105 4c 49 54 45 5f 46 4c 4f 41 54 20 29 7b 0a 20 20  LITE_FLOAT ){.  
23106 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53                pS
23107 61 6d 70 6c 65 2d 3e 75 2e 72 20 3d 20 73 71 6c  ample->u.r = sql
23108 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62  ite3_column_doub
23109 6c 65 28 70 53 74 6d 74 2c 20 32 29 3b 0a 20 20  le(pStmt, 2);.  
2310a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
2310b 65 20 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c  e if( eType==SQL
2310c 49 54 45 5f 54 45 58 54 20 7c 7c 20 65 54 79 70  ITE_TEXT || eTyp
2310d 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  e==SQLITE_BLOB )
2310e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2310f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
23110 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
23111 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  (.              
23112 20 20 20 20 20 20 28 65 54 79 70 65 3d 3d 53 51        (eType==SQ
23113 4c 49 54 45 5f 42 4c 4f 42 29 20 3f 0a 20 20 20  LITE_BLOB) ?.   
23114 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23115 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
23116 62 6c 6f 62 28 70 53 74 6d 74 2c 20 32 29 3a 0a  blob(pStmt, 2):.
23117 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23118 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
23119 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 32  mn_text(pStmt, 2
2311a 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
2311b 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20    );.           
2311c 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c       int n = sql
2311d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
2311e 73 28 70 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20  s(pStmt, 2);.   
2311f 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
23120 20 6e 3e 32 34 20 29 7b 0a 20 20 20 20 20 20 20   n>24 ){.       
23121 20 20 20 20 20 20 20 20 20 20 20 6e 20 3d 20 32             n = 2
23122 34 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  4;.             
23123 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
23124 20 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 42       pSample->nB
23125 79 74 65 20 3d 20 28 75 38 29 6e 3b 0a 20 20 20  yte = (u8)n;.   
23126 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 61               pSa
23127 6d 70 6c 65 2d 3e 75 2e 7a 20 3d 20 73 71 6c 69  mple->u.z = sqli
23128 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
23129 62 4d 65 6d 2c 20 6e 29 3b 0a 20 20 20 20 20 20  bMem, n);.      
2312a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 53            if( pS
2312b 61 6d 70 6c 65 2d 3e 75 2e 7a 20 29 7b 0a 20 20  ample->u.z ){.  
2312c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2312d 6d 65 6d 63 70 79 28 70 53 61 6d 70 6c 65 2d 3e  memcpy(pSample->
2312e 75 2e 7a 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20  u.z, z, n);.    
2312f 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
23130 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
23131 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
23132 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
23133 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
23134 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
23135 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
23136 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
23137 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
23138 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
23139 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  }.      rc = sql
2313a 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
2313b 74 6d 74 29 3b 0a 20 20 20 20 20 20 28 76 6f 69  tmt);.      (voi
2313c 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2313d 6e 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  n(db);.    }.  }
2313e 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 72  .#endif..  if( r
2313f 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
23140 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
23141 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
23142 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
23143 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
23144 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 20  TE_OMIT_ANALYZE 
23145 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
23146 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6e 61 6c 79  *** End of analy
23147 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ze.c ***********
23148 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23149 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2314a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2314b 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61  *** Begin file a
2314c 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ttach.c ********
2314d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2314e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2314f 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41  **/./*.** 2003 A
23150 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
23151 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
23152 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
23153 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
23154 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
23155 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
23156 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
23157 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
23158 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
23159 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2315a 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2315b 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
2315c 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
2315d 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2315e 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2315f 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
23160 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
23161 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
23162 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23163 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23164 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23165 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23166 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
23167 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
23168 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
23169 6e 74 20 74 68 65 20 41 54 54 41 43 48 20 61 6e  nt the ATTACH an
2316a 64 20 44 45 54 41 43 48 20 63 6f 6d 6d 61 6e 64  d DETACH command
2316b 73 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  s..*/..#ifndef S
2316c 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
2316d 48 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20  H./*.** Resolve 
2316e 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  an expression th
2316f 61 74 20 77 61 73 20 70 61 72 74 20 6f 66 20 61  at was part of a
23170 6e 20 41 54 54 41 43 48 20 6f 72 20 44 45 54 41  n ATTACH or DETA
23171 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  CH statement. Th
23172 69 73 0a 2a 2a 20 69 73 20 73 6c 69 67 68 74 6c  is.** is slightl
23173 79 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  y different from
23174 20 72 65 73 6f 6c 76 69 6e 67 20 61 20 6e 6f 72   resolving a nor
23175 6d 61 6c 20 53 51 4c 20 65 78 70 72 65 73 73 69  mal SQL expressi
23176 6f 6e 2c 20 62 65 63 61 75 73 65 20 73 69 6d 70  on, because simp
23177 6c 65 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65 72  le.** identifier
23178 73 20 61 72 65 20 74 72 65 61 74 65 64 20 61 73  s are treated as
23179 20 73 74 72 69 6e 67 73 2c 20 6e 6f 74 20 70 6f   strings, not po
2317a 73 73 69 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e 61  ssible column na
2317b 6d 65 73 20 6f 72 20 61 6c 69 61 73 65 73 2e 0a  mes or aliases..
2317c 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 69 66 20 74 68  **.** i.e. if th
2317d 65 20 70 61 72 73 65 72 20 73 65 65 73 3a 0a 2a  e parser sees:.*
2317e 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20  *.**     ATTACH 
2317f 44 41 54 41 42 41 53 45 20 61 62 63 20 41 53 20  DATABASE abc AS 
23180 64 65 66 0a 2a 2a 0a 2a 2a 20 69 74 20 74 72 65  def.**.** it tre
23181 61 74 73 20 74 68 65 20 74 77 6f 20 65 78 70 72  ats the two expr
23182 65 73 73 69 6f 6e 73 20 61 73 20 6c 69 74 65 72  essions as liter
23183 61 6c 20 73 74 72 69 6e 67 73 20 27 61 62 63 27  al strings 'abc'
23184 20 61 6e 64 20 27 64 65 66 27 20 69 6e 73 74 65   and 'def' inste
23185 61 64 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67  ad of.** looking
23186 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20 6f 66 20   for columns of 
23187 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a 2a  the same name..*
23188 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 61  *.** This only a
23189 70 70 6c 69 65 73 20 74 6f 20 74 68 65 20 72 6f  pplies to the ro
2318a 6f 74 20 6e 6f 64 65 20 6f 66 20 70 45 78 70 72  ot node of pExpr
2318b 2c 20 73 6f 20 74 68 65 20 73 74 61 74 65 6d 65  , so the stateme
2318c 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54  nt:.**.**     AT
2318d 54 41 43 48 20 44 41 54 41 42 41 53 45 20 61 62  TACH DATABASE ab
2318e 63 7c 7c 64 65 66 20 41 53 20 27 64 62 32 27 0a  c||def AS 'db2'.
2318f 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20  **.** will fail 
23190 62 65 63 61 75 73 65 20 6e 65 69 74 68 65 72 20  because neither 
23191 61 62 63 20 6f 72 20 64 65 66 20 63 61 6e 20 62  abc or def can b
23192 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73  e resolved..*/.s
23193 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76  tatic int resolv
23194 65 41 74 74 61 63 68 45 78 70 72 28 4e 61 6d 65  eAttachExpr(Name
23195 43 6f 6e 74 65 78 74 20 2a 70 4e 61 6d 65 2c 20  Context *pName, 
23196 45 78 70 72 20 2a 70 45 78 70 72 29 0a 7b 0a 20  Expr *pExpr).{. 
23197 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
23198 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 45 78 70 72  _OK;.  if( pExpr
23199 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70   ){.    if( pExp
2319a 72 2d 3e 6f 70 21 3d 54 4b 5f 49 44 20 29 7b 0a  r->op!=TK_ID ){.
2319b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2319c 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
2319d 65 73 28 70 4e 61 6d 65 2c 20 70 45 78 70 72 29  es(pName, pExpr)
2319e 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
2319f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 73 71  SQLITE_OK && !sq
231a0 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
231a1 61 6e 74 28 70 45 78 70 72 29 20 29 7b 0a 20 20  ant(pExpr) ){.  
231a2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
231a3 6f 72 4d 73 67 28 70 4e 61 6d 65 2d 3e 70 50 61  orMsg(pName->pPa
231a4 72 73 65 2c 20 22 69 6e 76 61 6c 69 64 20 6e 61  rse, "invalid na
231a5 6d 65 3a 20 5c 22 25 73 5c 22 22 2c 20 70 45 78  me: \"%s\"", pEx
231a6 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20  pr->u.zToken);. 
231a7 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
231a8 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
231a9 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
231aa 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
231ab 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20   TK_STRING;.    
231ac 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
231ad 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53  c;.}../*.** An S
231ae 51 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  QL user-function
231af 20 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64   registered to d
231b0 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e  o the work of an
231b1 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e   ATTACH statemen
231b2 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20  t. The.** three 
231b3 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
231b4 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64   function come d
231b5 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 6e 20  irectly from an 
231b6 61 74 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74  attach statement
231b7 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41  :.**.**     ATTA
231b8 43 48 20 44 41 54 41 42 41 53 45 20 78 20 41 53  CH DATABASE x AS
231b9 20 79 20 4b 45 59 20 7a 0a 2a 2a 0a 2a 2a 20 20   y KEY z.**.**  
231ba 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65     SELECT sqlite
231bb 5f 61 74 74 61 63 68 28 78 2c 20 79 2c 20 7a 29  _attach(x, y, z)
231bc 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
231bd 74 69 6f 6e 61 6c 20 22 4b 45 59 20 7a 22 20 73  tional "KEY z" s
231be 79 6e 74 61 78 20 69 73 20 6f 6d 69 74 74 65 64  yntax is omitted
231bf 2c 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 69 73  , an SQL NULL is
231c0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a   passed as the.*
231c1 2a 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  * third argument
231c2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
231c3 20 61 74 74 61 63 68 46 75 6e 63 28 0a 20 20 73   attachFunc(.  s
231c4 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
231c5 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
231c6 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
231c7 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
231c8 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
231c9 20 72 63 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74   rc = 0;.  sqlit
231ca 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
231cb 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
231cc 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63  le(context);.  c
231cd 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
231ce 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
231cf 7a 46 69 6c 65 3b 0a 20 20 44 62 20 2a 61 4e 65  zFile;.  Db *aNe
231d0 77 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 44  w;.  char *zErrD
231d1 79 6e 20 3d 20 30 3b 0a 0a 20 20 55 4e 55 53 45  yn = 0;..  UNUSE
231d2 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
231d3 73 65 64 29 3b 0a 0a 20 20 7a 46 69 6c 65 20 3d  sed);..  zFile =
231d4 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
231d5 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
231d6 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e  t(argv[0]);.  zN
231d7 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ame = (const cha
231d8 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
231d9 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
231da 0a 20 20 69 66 28 20 7a 46 69 6c 65 3d 3d 30 20  .  if( zFile==0 
231db 29 20 7a 46 69 6c 65 20 3d 20 22 22 3b 0a 20 20  ) zFile = "";.  
231dc 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a  if( zName==0 ) z
231dd 4e 61 6d 65 20 3d 20 22 22 3b 0a 0a 20 20 2f 2a  Name = "";..  /*
231de 20 43 68 65 63 6b 20 66 6f 72 20 74 68 65 20 66   Check for the f
231df 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73 3a  ollowing errors:
231e0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 2a  .  **.  **     *
231e1 20 54 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68   Too many attach
231e2 65 64 20 64 61 74 61 62 61 73 65 73 2c 0a 20 20  ed databases,.  
231e3 2a 2a 20 20 20 20 20 2a 20 54 72 61 6e 73 61 63  **     * Transac
231e4 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 6f  tion currently o
231e5 70 65 6e 0a 20 20 2a 2a 20 20 20 20 20 2a 20 53  pen.  **     * S
231e6 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73  pecified databas
231e7 65 20 6e 61 6d 65 20 61 6c 72 65 61 64 79 20 62  e name already b
231e8 65 69 6e 67 20 75 73 65 64 2e 0a 20 20 2a 2f 0a  eing used..  */.
231e9 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 3d 64    if( db->nDb>=d
231ea 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
231eb 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d  _LIMIT_ATTACHED]
231ec 2b 32 20 29 7b 0a 20 20 20 20 7a 45 72 72 44 79  +2 ){.    zErrDy
231ed 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  n = sqlite3MPrin
231ee 74 66 28 64 62 2c 20 22 74 6f 6f 20 6d 61 6e 79  tf(db, "too many
231ef 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
231f0 73 65 73 20 2d 20 6d 61 78 20 25 64 22 2c 20 0a  ses - max %d", .
231f1 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74        db->aLimit
231f2 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54  [SQLITE_LIMIT_AT
231f3 54 41 43 48 45 44 5d 0a 20 20 20 20 29 3b 0a 20  TACHED].    );. 
231f4 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
231f5 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rror;.  }.  if( 
231f6 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20  !db->autoCommit 
231f7 29 7b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20 3d  ){.    zErrDyn =
231f8 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
231f9 64 62 2c 20 22 63 61 6e 6e 6f 74 20 41 54 54 41  db, "cannot ATTA
231fa 43 48 20 64 61 74 61 62 61 73 65 20 77 69 74 68  CH database with
231fb 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29  in transaction")
231fc 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63  ;.    goto attac
231fd 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 66  h_error;.  }.  f
231fe 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
231ff 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61  b; i++){.    cha
23200 72 20 2a 7a 20 3d 20 64 62 2d 3e 61 44 62 5b 69  r *z = db->aDb[i
23201 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 61 73 73  ].zName;.    ass
23202 65 72 74 28 20 7a 20 26 26 20 7a 4e 61 6d 65 20  ert( z && zName 
23203 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
23204 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 4e 61  e3StrICmp(z, zNa
23205 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
23206 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
23207 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 64 61  3MPrintf(db, "da
23208 74 61 62 61 73 65 20 25 73 20 69 73 20 61 6c 72  tabase %s is alr
23209 65 61 64 79 20 69 6e 20 75 73 65 22 2c 20 7a 4e  eady in use", zN
2320a 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ame);.      goto
2320b 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
2320c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
2320d 6c 6c 6f 63 61 74 65 20 74 68 65 20 6e 65 77 20  llocate the new 
2320e 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 62 2d  entry in the db-
2320f 3e 61 44 62 5b 5d 20 61 72 72 61 79 20 61 6e 64  >aDb[] array and
23210 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20   initialise the 
23211 73 63 68 65 6d 61 0a 20 20 2a 2a 20 68 61 73 68  schema.  ** hash
23212 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
23213 69 66 28 20 64 62 2d 3e 61 44 62 3d 3d 64 62 2d  if( db->aDb==db-
23214 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20  >aDbStatic ){.  
23215 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    aNew = sqlite3
23216 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
23217 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30  sizeof(db->aDb[0
23218 5d 29 2a 33 20 29 3b 0a 20 20 20 20 69 66 28 20  ])*3 );.    if( 
23219 61 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  aNew==0 ) return
2321a 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 4e 65  ;.    memcpy(aNe
2321b 77 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65  w, db->aDb, size
2321c 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 32  of(db->aDb[0])*2
2321d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2321e 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  aNew = sqlite3Db
2321f 52 65 61 6c 6c 6f 63 28 64 62 2c 20 64 62 2d 3e  Realloc(db, db->
23220 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e  aDb, sizeof(db->
23221 61 44 62 5b 30 5d 29 2a 28 64 62 2d 3e 6e 44 62  aDb[0])*(db->nDb
23222 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20 61  +1) );.    if( a
23223 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  New==0 ) return;
23224 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 20 3d  .  }.  db->aDb =
23225 20 61 4e 65 77 3b 0a 20 20 61 4e 65 77 20 3d 20   aNew;.  aNew = 
23226 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62  &db->aDb[db->nDb
23227 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 4e 65 77  ];.  memset(aNew
23228 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 61 4e 65  , 0, sizeof(*aNe
23229 77 29 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  w));..  /* Open 
2322a 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
2322b 65 2e 20 49 66 20 74 68 65 20 62 74 72 65 65 20  e. If the btree 
2322c 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  is successfully 
2322d 6f 70 65 6e 65 64 2c 20 75 73 65 0a 20 20 2a 2a  opened, use.  **
2322e 20 69 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68   it to obtain th
2322f 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
23230 61 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  a. At this point
23231 20 74 68 65 20 73 63 68 65 6d 61 20 6d 61 79 0a   the schema may.
23232 20 20 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20    ** or may not 
23233 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a  be initialised..
23234 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
23235 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
23236 64 62 2c 20 7a 46 69 6c 65 2c 20 30 2c 20 53 51  db, zFile, 0, SQ
23237 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
23238 48 45 5f 53 49 5a 45 2c 0a 20 20 20 20 20 20 20  HE_SIZE,.       
23239 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2323a 20 20 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67      db->openFlag
2323b 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s | SQLITE_OPEN_
2323c 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20 20  MAIN_DB,.       
2323d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2323e 20 20 20 20 26 61 4e 65 77 2d 3e 70 42 74 29 3b      &aNew->pBt);
2323f 0a 20 20 64 62 2d 3e 6e 44 62 2b 2b 3b 0a 20 20  .  db->nDb++;.  
23240 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43  if( rc==SQLITE_C
23241 4f 4e 53 54 52 41 49 4e 54 20 29 7b 0a 20 20 20  ONSTRAINT ){.   
23242 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
23243 4f 52 3b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20  OR;.    zErrDyn 
23244 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
23245 28 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 69  (db, "database i
23246 73 20 61 6c 72 65 61 64 79 20 61 74 74 61 63 68  s already attach
23247 65 64 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ed");.  }else if
23248 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
23249 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50  ){.    Pager *pP
2324a 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e  ager;.    aNew->
2324b 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65  pSchema = sqlite
2324c 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 61  3SchemaGet(db, a
2324d 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69  New->pBt);.    i
2324e 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d  f( !aNew->pSchem
2324f 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  a ){.      rc = 
23250 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
23251 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77    }else if( aNew
23252 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
23253 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e  format && aNew->
23254 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e  pSchema->enc!=EN
23255 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 7a  C(db) ){.      z
23256 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33  ErrDyn = sqlite3
23257 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20  MPrintf(db, .   
23258 20 20 20 20 20 22 61 74 74 61 63 68 65 64 20 64       "attached d
23259 61 74 61 62 61 73 65 73 20 6d 75 73 74 20 75 73  atabases must us
2325a 65 20 74 68 65 20 73 61 6d 65 20 74 65 78 74 20  e the same text 
2325b 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e  encoding as main
2325c 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20   database");.   
2325d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
2325e 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20  RROR;.    }.    
2325f 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
23260 42 74 72 65 65 50 61 67 65 72 28 61 4e 65 77 2d  BtreePager(aNew-
23261 3e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  >pBt);.    sqlit
23262 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
23263 64 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64  de(pPager, db->d
23264 66 6c 74 4c 6f 63 6b 4d 6f 64 65 29 3b 0a 20 20  fltLockMode);.  
23265 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f    sqlite3PagerJo
23266 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72  urnalMode(pPager
23267 2c 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61  , db->dfltJourna
23268 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 61 4e  lMode);.  }.  aN
23269 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  ew->zName = sqli
2326a 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2326b 7a 4e 61 6d 65 29 3b 0a 20 20 61 4e 65 77 2d 3e  zName);.  aNew->
2326c 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33  safety_level = 3
2326d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41  ;..#if SQLITE_HA
2326e 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 20 72 63  S_CODEC.  if( rc
2326f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
23270 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71     extern int sq
23271 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68  lite3CodecAttach
23272 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20  (sqlite3*, int, 
23273 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
23274 29 3b 0a 20 20 20 20 65 78 74 65 72 6e 20 76 6f  );.    extern vo
23275 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47  id sqlite3CodecG
23276 65 74 4b 65 79 28 73 71 6c 69 74 65 33 2a 2c 20  etKey(sqlite3*, 
23277 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74  int, void**, int
23278 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79  *);.    int nKey
23279 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b 65 79  ;.    char *zKey
2327a 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d 20 73 71  ;.    int t = sq
2327b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2327c 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 73  (argv[2]);.    s
2327d 77 69 74 63 68 28 20 74 20 29 7b 0a 20 20 20 20  witch( t ){.    
2327e 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
2327f 54 45 47 45 52 3a 0a 20 20 20 20 20 20 63 61 73  TEGER:.      cas
23280 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 0a  e SQLITE_FLOAT:.
23281 20 20 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20          zErrDyn 
23282 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
23283 70 28 64 62 2c 20 22 49 6e 76 61 6c 69 64 20 6b  p(db, "Invalid k
23284 65 79 20 76 61 6c 75 65 22 29 3b 0a 20 20 20 20  ey value");.    
23285 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
23286 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 62  ERROR;.        b
23287 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 0a 20  reak;.        . 
23288 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45       case SQLITE
23289 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 63 61 73  _TEXT:.      cas
2328a 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20  e SQLITE_BLOB:. 
2328b 20 20 20 20 20 20 20 6e 4b 65 79 20 3d 20 73 71         nKey = sq
2328c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
2328d 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20  s(argv[2]);.    
2328e 20 20 20 20 7a 4b 65 79 20 3d 20 28 63 68 61 72      zKey = (char
2328f 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
23290 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d 29 3b 0a  _blob(argv[2]);.
23291 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
23292 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 28  ite3CodecAttach(
23293 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a  db, db->nDb-1, z
23294 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20  Key, nKey);.    
23295 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20      break;..    
23296 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55    case SQLITE_NU
23297 4c 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e  LL:.        /* N
23298 6f 20 6b 65 79 20 73 70 65 63 69 66 69 65 64 2e  o key specified.
23299 20 20 55 73 65 20 74 68 65 20 6b 65 79 20 66 72    Use the key fr
2329a 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  om the main data
2329b 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  base */.        
2329c 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b  sqlite3CodecGetK
2329d 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a  ey(db, 0, (void*
2329e 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b  *)&zKey, &nKey);
2329f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
232a0 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68  lite3CodecAttach
232a1 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20  (db, db->nDb-1, 
232a2 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20  zKey, nKey);.   
232a3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
232a4 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
232a5 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 77  /* If the file w
232a6 61 73 20 6f 70 65 6e 65 64 20 73 75 63 63 65 73  as opened succes
232a7 73 66 75 6c 6c 79 2c 20 72 65 61 64 20 74 68 65  sfully, read the
232a8 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65 20   schema for the 
232a9 6e 65 77 20 64 61 74 61 62 61 73 65 2e 0a 20 20  new database..  
232aa 2a 2a 20 49 66 20 74 68 69 73 20 66 61 69 6c 73  ** If this fails
232ab 2c 20 6f 72 20 69 66 20 6f 70 65 6e 69 6e 67 20  , or if opening 
232ac 74 68 65 20 66 69 6c 65 20 66 61 69 6c 65 64 2c  the file failed,
232ad 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20   then close the 
232ae 66 69 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72  file and .  ** r
232af 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72 79 20  emove the entry 
232b0 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 61 44 62  from the db->aDb
232b1 5b 5d 20 61 72 72 61 79 2e 20 69 2e 65 2e 20 70  [] array. i.e. p
232b2 75 74 20 65 76 65 72 79 74 68 69 6e 67 20 62 61  ut everything ba
232b3 63 6b 20 74 68 65 20 77 61 79 0a 20 20 2a 2a 20  ck the way.  ** 
232b4 77 65 20 66 6f 75 6e 64 20 69 74 2e 0a 20 20 2a  we found it..  *
232b5 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
232b6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 28 76 6f  TE_OK ){.    (vo
232b7 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
232b8 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69  On(db);.    sqli
232b9 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
232ba 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  (db);.    rc = s
232bb 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26  qlite3Init(db, &
232bc 7a 45 72 72 44 79 6e 29 3b 0a 20 20 20 20 73 71  zErrDyn);.    sq
232bd 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41  lite3BtreeLeaveA
232be 6c 6c 28 64 62 29 3b 0a 20 20 20 20 28 76 6f 69  ll(db);.    (voi
232bf 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
232c0 66 66 28 64 62 29 3b 0a 20 20 7d 0a 20 20 69 66  ff(db);.  }.  if
232c1 28 20 72 63 20 29 7b 0a 20 20 20 20 69 6e 74 20  ( rc ){.    int 
232c2 69 44 62 20 3d 20 64 62 2d 3e 6e 44 62 20 2d 20  iDb = db->nDb - 
232c3 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  1;.    assert( i
232c4 44 62 3e 3d 32 20 29 3b 0a 20 20 20 20 69 66 28  Db>=2 );.    if(
232c5 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42   db->aDb[iDb].pB
232c6 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
232c7 65 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62 2d  e3BtreeClose(db-
232c8 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 3b 0a  >aDb[iDb].pBt);.
232c9 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
232ca 62 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20  b].pBt = 0;.    
232cb 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70    db->aDb[iDb].p
232cc 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20 20  Schema = 0;.    
232cd 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  }.    sqlite3Res
232ce 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
232cf 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d  (db, 0);.    db-
232d0 3e 6e 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20  >nDb = iDb;.    
232d1 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
232d2 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  OMEM || rc==SQLI
232d3 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29  TE_IOERR_NOMEM )
232d4 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
232d5 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
232d6 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
232d7 65 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b 0a  e(db, zErrDyn);.
232d8 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20        zErrDyn = 
232d9 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
232da 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  b, "out of memor
232db 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  y");.    }else i
232dc 66 28 20 7a 45 72 72 44 79 6e 3d 3d 30 20 29 7b  f( zErrDyn==0 ){
232dd 0a 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d  .      zErrDyn =
232de 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
232df 64 62 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f  db, "unable to o
232e0 70 65 6e 20 64 61 74 61 62 61 73 65 3a 20 25 73  pen database: %s
232e1 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20 7d  ", zFile);.    }
232e2 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68  .    goto attach
232e3 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 0a 20  _error;.  }.  . 
232e4 20 72 65 74 75 72 6e 3b 0a 0a 61 74 74 61 63 68   return;..attach
232e5 5f 65 72 72 6f 72 3a 0a 20 20 2f 2a 20 52 65 74  _error:.  /* Ret
232e6 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 66 20  urn an error if 
232e7 77 65 20 67 65 74 20 68 65 72 65 20 2a 2f 0a 20  we get here */. 
232e8 20 69 66 28 20 7a 45 72 72 44 79 6e 20 29 7b 0a   if( zErrDyn ){.
232e9 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
232ea 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
232eb 2c 20 7a 45 72 72 44 79 6e 2c 20 2d 31 29 3b 0a  , zErrDyn, -1);.
232ec 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
232ed 65 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b 0a  e(db, zErrDyn);.
232ee 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 20 73    }.  if( rc ) s
232ef 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
232f0 72 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65 78 74  ror_code(context
232f1 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  , rc);.}../*.** 
232f2 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e 63  An SQL user-func
232f3 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
232f4 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f  to do the work o
232f5 66 20 61 6e 20 44 45 54 41 43 48 20 73 74 61 74  f an DETACH stat
232f6 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74 68  ement. The.** th
232f7 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  ree arguments to
232f8 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 6f   the function co
232f9 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d  me directly from
232fa 20 61 20 64 65 74 61 63 68 20 73 74 61 74 65 6d   a detach statem
232fb 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44  ent:.**.**     D
232fc 45 54 41 43 48 20 44 41 54 41 42 41 53 45 20 78  ETACH DATABASE x
232fd 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
232fe 54 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28  T sqlite_detach(
232ff 78 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  x).*/.static voi
23300 64 20 64 65 74 61 63 68 46 75 6e 63 28 0a 20 20  d detachFunc(.  
23301 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
23302 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
23303 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
23304 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
23305 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
23306 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20  *zName = (const 
23307 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
23308 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
23309 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ]);.  sqlite3 *d
2330a 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
2330b 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
2330c 6e 74 65 78 74 29 3b 0a 20 20 69 6e 74 20 69 3b  ntext);.  int i;
2330d 0a 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 0a  .  Db *pDb = 0;.
2330e 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d    char zErr[128]
2330f 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
23310 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
23311 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20  .  if( zName==0 
23312 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 20 20  ) zName = "";.  
23313 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
23314 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 44  Db; i++){.    pD
23315 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b  b = &db->aDb[i];
23316 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42  .    if( pDb->pB
23317 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  t==0 ) continue;
23318 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
23319 53 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61  StrICmp(pDb->zNa
2331a 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20  me, zName)==0 ) 
2331b 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66  break;.  }..  if
2331c 28 20 69 3e 3d 64 62 2d 3e 6e 44 62 20 29 7b 0a  ( i>=db->nDb ){.
2331d 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
2331e 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
2331f 29 2c 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68  ),zErr, "no such
23320 20 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c 20   database: %s", 
23321 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
23322 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   detach_error;. 
23323 20 7d 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a   }.  if( i<2 ){.
23324 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
23325 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
23326 29 2c 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20  ),zErr, "cannot 
23327 64 65 74 61 63 68 20 64 61 74 61 62 61 73 65 20  detach database 
23328 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  %s", zName);.   
23329 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72   goto detach_err
2332a 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64  or;.  }.  if( !d
2332b 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
2332c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
2332d 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
2332e 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20  r), zErr,.      
2332f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
23330 63 61 6e 6e 6f 74 20 44 45 54 41 43 48 20 64 61  cannot DETACH da
23331 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 74 72  tabase within tr
23332 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20  ansaction");.   
23333 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72   goto detach_err
23334 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  or;.  }.  if( sq
23335 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65  lite3BtreeIsInRe
23336 61 64 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74  adTrans(pDb->pBt
23337 29 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65  ) || sqlite3Btre
23338 65 49 73 49 6e 42 61 63 6b 75 70 28 70 44 62 2d  eIsInBackup(pDb-
23339 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 73 71 6c  >pBt) ){.    sql
2333a 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
2333b 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c  zeof(zErr),zErr,
2333c 20 22 64 61 74 61 62 61 73 65 20 25 73 20 69 73   "database %s is
2333d 20 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61 6d 65 29   locked", zName)
2333e 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63  ;.    goto detac
2333f 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20  h_error;.  }..  
23340 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
23341 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 70  e(pDb->pBt);.  p
23342 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 70  Db->pBt = 0;.  p
23343 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b  Db->pSchema = 0;
23344 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49  .  sqlite3ResetI
23345 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62  nternalSchema(db
23346 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  , 0);.  return;.
23347 0a 64 65 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20  .detach_error:. 
23348 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
23349 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
2334a 45 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a  Err, -1);.}../*.
2334b 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75 72  ** This procedur
2334c 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45  e generates VDBE
2334d 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67   code for a sing
2334e 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  le invocation of
2334f 20 65 69 74 68 65 72 20 74 68 65 0a 2a 2a 20 73   either the.** s
23350 71 6c 69 74 65 5f 64 65 74 61 63 68 28 29 20 6f  qlite_detach() o
23351 72 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28  r sqlite_attach(
23352 29 20 53 51 4c 20 75 73 65 72 20 66 75 6e 63 74  ) SQL user funct
23353 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ions..*/.static 
23354 76 6f 69 64 20 63 6f 64 65 41 74 74 61 63 68 28  void codeAttach(
23355 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23356 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ,       /* The p
23357 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
23358 0a 20 20 69 6e 74 20 74 79 70 65 2c 20 20 20 20  .  int type,    
23359 20 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65          /* Eithe
2335a 72 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20  r SQLITE_ATTACH 
2335b 6f 72 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48  or SQLITE_DETACH
2335c 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70   */.  FuncDef *p
2335d 46 75 6e 63 2c 20 20 20 20 20 20 2f 2a 20 46 75  Func,      /* Fu
2335e 6e 63 44 65 66 20 77 72 61 70 70 65 72 20 66 6f  ncDef wrapper fo
2335f 72 20 64 65 74 61 63 68 46 75 6e 63 28 29 20 6f  r detachFunc() o
23360 72 20 61 74 74 61 63 68 46 75 6e 63 28 29 20 2a  r attachFunc() *
23361 2f 0a 20 20 45 78 70 72 20 2a 70 41 75 74 68 41  /.  Expr *pAuthA
23362 72 67 2c 20 20 20 20 20 20 2f 2a 20 45 78 70 72  rg,      /* Expr
23363 65 73 73 69 6f 6e 20 74 6f 20 70 61 73 73 20 74  ession to pass t
23364 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  o authorization 
23365 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 45 78  callback */.  Ex
23366 70 72 20 2a 70 46 69 6c 65 6e 61 6d 65 2c 20 20  pr *pFilename,  
23367 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
23368 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
23369 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20   Expr *pDbname, 
2336a 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
2336b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f   the database to
2336c 20 75 73 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20   use internally 
2336d 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4b 65 79 20  */.  Expr *pKey 
2336e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2336f 61 62 61 73 65 20 6b 65 79 20 66 6f 72 20 65 6e  abase key for en
23370 63 72 79 70 74 69 6f 6e 20 65 78 74 65 6e 73 69  cryption extensi
23371 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  on */.){.  int r
23372 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  c;.  NameContext
23373 20 73 4e 61 6d 65 3b 0a 20 20 56 64 62 65 20 2a   sName;.  Vdbe *
23374 76 3b 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62  v;.  sqlite3* db
23375 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
23376 20 69 6e 74 20 72 65 67 41 72 67 73 3b 0a 0a 20   int regArgs;.. 
23377 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65 2c 20   memset(&sName, 
23378 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f  0, sizeof(NameCo
23379 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65  ntext));.  sName
2337a 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
2337b 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20 20 20 20  ;..  if( .      
2337c 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
2337d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78   resolveAttachEx
2337e 70 72 28 26 73 4e 61 6d 65 2c 20 70 46 69 6c 65  pr(&sName, pFile
2337f 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20  name)) ||.      
23380 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
23381 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78   resolveAttachEx
23382 70 72 28 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61  pr(&sName, pDbna
23383 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51  me)) ||.      SQ
23384 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
23385 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72  esolveAttachExpr
23386 28 26 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0a  (&sName, pKey)).
23387 20 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d    ){.    pParse-
23388 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74  >nErr++;.    got
23389 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20  o attach_end;.  
2338a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2338b 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
2338c 54 49 4f 4e 0a 20 20 69 66 28 20 70 41 75 74 68  TION.  if( pAuth
2338d 41 72 67 20 29 7b 0a 20 20 20 20 63 68 61 72 20  Arg ){.    char 
2338e 2a 7a 41 75 74 68 41 72 67 20 3d 20 70 41 75 74  *zAuthArg = pAut
2338f 68 41 72 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  hArg->u.zToken;.
23390 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 7a 41      if( NEVER(zA
23391 75 74 68 41 72 67 3d 3d 30 29 20 29 7b 0a 20 20  uthArg==0) ){.  
23392 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f      goto attach_
23393 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  end;.    }.    r
23394 63 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43  c = sqlite3AuthC
23395 68 65 63 6b 28 70 50 61 72 73 65 2c 20 74 79 70  heck(pParse, typ
23396 65 2c 20 7a 41 75 74 68 41 72 67 2c 20 30 2c 20  e, zAuthArg, 0, 
23397 30 29 3b 0a 20 20 20 20 69 66 28 72 63 21 3d 53  0);.    if(rc!=S
23398 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
23399 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e    goto attach_en
2339a 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  d;.    }.  }.#en
2339b 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2339c 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
2339d 20 2a 2f 0a 0a 0a 20 20 76 20 3d 20 73 71 6c 69   */...  v = sqli
2339e 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2339f 65 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20  e);.  regArgs = 
233a0 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
233a1 6e 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a  nge(pParse, 4);.
233a2 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
233a3 65 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e  e(pParse, pFilen
233a4 61 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20  ame, regArgs);. 
233a5 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
233a6 28 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65  (pParse, pDbname
233a7 2c 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20  , regArgs+1);.  
233a8 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
233a9 70 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65  pParse, pKey, re
233aa 67 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73  gArgs+2);..  ass
233ab 65 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61  ert( v || db->ma
233ac 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
233ad 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
233ae 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
233af 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30  , OP_Function, 0
233b0 2c 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e  , regArgs+3-pFun
233b1 63 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73  c->nArg, regArgs
233b2 2b 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  +3);.    assert(
233b3 20 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d 31   pFunc->nArg==-1
233b4 20 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72 67   || (pFunc->nArg
233b5 26 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e 6e  &0xff)==pFunc->n
233b6 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  Arg );.    sqlit
233b7 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
233b8 2c 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e 41  , (u8)(pFunc->nA
233b9 72 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rg));.    sqlite
233ba 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
233bb 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46 75   -1, (char *)pFu
233bc 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b  nc, P4_FUNCDEF);
233bd 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e  ..    /* Code an
233be 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72 20   OP_Expire. For 
233bf 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 65 6d  an ATTACH statem
233c0 65 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20 74  ent, set P1 to t
233c1 72 75 65 20 28 65 78 70 69 72 65 20 74 68 69 73  rue (expire this
233c2 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  .    ** statemen
233c3 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45 54  t only). For DET
233c4 41 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20 66  ACH, set it to f
233c5 61 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c 6c  alse (expire all
233c6 20 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a 2a   existing.    **
233c7 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20 20   statements)..  
233c8 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
233c9 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
233ca 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d 3d  _Expire, (type==
233cb 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29 3b  SQLITE_ATTACH));
233cc 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f 65  .  }.  .attach_e
233cd 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70  nd:.  sqlite3Exp
233ce 72 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69 6c  rDelete(db, pFil
233cf 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  ename);.  sqlite
233d0 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
233d1 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  pDbname);.  sqli
233d2 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
233d3 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , pKey);.}../*.*
233d4 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20  * Called by the 
233d5 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c  parser to compil
233d6 65 20 61 20 44 45 54 41 43 48 20 73 74 61 74 65  e a DETACH state
233d7 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ment..**.**     
233d8 44 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a 2a  DETACH pDbname.*
233d9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
233da 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74   void sqlite3Det
233db 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73  ach(Parse *pPars
233dc 65 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65  e, Expr *pDbname
233dd 29 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63  ){.  static Func
233de 44 65 66 20 64 65 74 61 63 68 5f 66 75 6e 63 20  Def detach_func 
233df 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20  = {.    1,      
233e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72            /* nAr
233e1 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f  g */.    SQLITE_
233e2 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50  UTF8,      /* iP
233e3 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c  refEnc */.    0,
233e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
233e5 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20  /* flags */.    
233e6 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
233e7 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a    /* pUserData *
233e8 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
233e9 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
233ea 20 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 46 75   */.    detachFu
233eb 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75  nc,       /* xFu
233ec 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  nc */.    0,    
233ed 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
233ee 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Step */.    0,  
233ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
233f0 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20   xFinalize */.  
233f1 20 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68    "sqlite_detach
233f2 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a  ",  /* zName */.
233f3 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
233f4 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a        /* pHash *
233f5 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74  /.  };.  codeAtt
233f6 61 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49  ach(pParse, SQLI
233f7 54 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74 61  TE_DETACH, &deta
233f8 63 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d 65  ch_func, pDbname
233f9 2c 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65 29  , 0, 0, pDbname)
233fa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65  ;.}../*.** Calle
233fb 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
233fc 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54  to compile an AT
233fd 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a  TACH statement..
233fe 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48  **.**     ATTACH
233ff 20 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b 45   p AS pDbname KE
23400 59 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54 45  Y pKey.*/.SQLITE
23401 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23402 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73  lite3Attach(Pars
23403 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
23404 2a 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d  *p, Expr *pDbnam
23405 65 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b 0a  e, Expr *pKey){.
23406 20 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66    static FuncDef
23407 20 61 74 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b   attach_func = {
23408 0a 20 20 20 20 33 2c 20 20 20 20 20 20 20 20 20  .    3,         
23409 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a         /* nArg *
2340a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46  /.    SQLITE_UTF
2340b 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66  8,      /* iPref
2340c 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  Enc */.    0,   
2340d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2340e 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20  flags */.    0, 
2340f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23410 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20  * pUserData */. 
23411 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
23412 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f       /* pNext */
23413 0a 20 20 20 20 61 74 74 61 63 68 46 75 6e 63 2c  .    attachFunc,
23414 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20         /* xFunc 
23415 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
23416 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65           /* xSte
23417 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  p */.    0,     
23418 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
23419 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22  inalize */.    "
2341a 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20  sqlite_attach", 
2341b 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20   /* zName */.   
2341c 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
2341d 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20     /* pHash */. 
2341e 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68   };.  codeAttach
2341f 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
23420 41 54 54 41 43 48 2c 20 26 61 74 74 61 63 68 5f  ATTACH, &attach_
23421 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e  func, p, p, pDbn
23422 61 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65  ame, pKey);.}.#e
23423 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
23424 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f  MIT_ATTACH */../
23425 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
23426 61 20 44 62 46 69 78 65 72 20 73 74 72 75 63 74  a DbFixer struct
23427 75 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ure.  This routi
23428 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  ne must be calle
23429 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61  d prior.** to pa
2342a 73 73 69 6e 67 20 74 68 65 20 73 74 72 75 63 74  ssing the struct
2342b 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68  ure to one of th
2342c 65 20 73 71 6c 69 74 65 46 69 78 41 41 41 41 28  e sqliteFixAAAA(
2342d 29 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77  ) routines below
2342e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
2342f 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74  rn value indicat
23430 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  es whether or no
23431 74 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72 65  t fixation is re
23432 71 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a  quired.  TRUE.**
23433 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65 65   means we do nee
23434 64 20 74 6f 20 66 69 78 20 74 68 65 20 64 61 74  d to fix the dat
23435 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73  abase references
23436 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77 65  , FALSE means we
23437 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49   do not..*/.SQLI
23438 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
23439 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a 20  qlite3FixInit(. 
2343a 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20   DbFixer *pFix, 
2343b 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78 65       /* The fixe
2343c 72 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69  r to be initiali
2343d 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  zed */.  Parse *
2343e 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
2343f 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77  Error messages w
23440 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 68  ill be written h
23441 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ere */.  int iDb
23442 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
23443 54 68 69 73 20 69 73 20 74 68 65 20 64 61 74 61  This is the data
23444 62 61 73 65 20 74 68 61 74 20 6d 75 73 74 20 62  base that must b
23445 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  e used */.  cons
23446 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20  t char *zType,  
23447 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67  /* "view", "trig
23448 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22  ger", or "index"
23449 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65   */.  const Toke
2344a 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d  n *pName  /* Nam
2344b 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74  e of the view, t
2344c 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78  rigger, or index
2344d 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
2344e 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e 45 56   *db;..  if( NEV
2344f 45 52 28 69 44 62 3c 30 29 20 7c 7c 20 69 44 62  ER(iDb<0) || iDb
23450 3d 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==1 ) return 0;.
23451 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
23452 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  b;.  assert( db-
23453 3e 6e 44 62 3e 69 44 62 20 29 3b 0a 20 20 70 46  >nDb>iDb );.  pF
23454 69 78 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61  ix->pParse = pPa
23455 72 73 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 44 62  rse;.  pFix->zDb
23456 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
23457 7a 4e 61 6d 65 3b 0a 20 20 70 46 69 78 2d 3e 7a  zName;.  pFix->z
23458 54 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20 20  Type = zType;.  
23459 70 46 69 78 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e  pFix->pName = pN
2345a 61 6d 65 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ame;.  return 1;
2345b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
2345c 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72  llowing set of r
2345d 6f 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68 72  outines walk thr
2345e 6f 75 67 68 20 74 68 65 20 70 61 72 73 65 20 74  ough the parse t
2345f 72 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0a 2a  ree and assign.*
23460 2a 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74  * a specific dat
23461 61 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61 62  abase to all tab
23462 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68  le references wh
23463 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ere the database
23464 20 6e 61 6d 65 0a 2a 2a 20 77 61 73 20 6c 65 66   name.** was lef
23465 74 20 75 6e 73 70 65 63 69 66 69 65 64 20 69 6e  t unspecified in
23466 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
23467 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  L statement.  Th
23468 65 20 70 46 69 78 20 73 74 72 75 63 74 75 72 65  e pFix structure
23469 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65  .** must have be
2346a 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62  en initialized b
2346b 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
2346c 6f 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  o sqlite3FixInit
2346d 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ()..**.** These 
2346e 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
2346f 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  d to make sure t
23470 68 61 74 20 61 6e 20 69 6e 64 65 78 2c 20 74 72  hat an index, tr
23471 69 67 67 65 72 2c 20 6f 72 0a 2a 2a 20 76 69 65  igger, or.** vie
23472 77 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  w in one databas
23473 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 66 65 72  e does not refer
23474 20 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 61   to objects in a
23475 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62   different datab
23476 61 73 65 2e 0a 2a 2a 20 28 45 78 63 65 70 74 69  ase..** (Excepti
23477 6f 6e 3a 20 69 6e 64 69 63 65 73 2c 20 74 72 69  on: indices, tri
23478 67 67 65 72 73 2c 20 61 6e 64 20 76 69 65 77 73  ggers, and views
23479 20 69 6e 20 74 68 65 20 54 45 4d 50 20 64 61 74   in the TEMP dat
2347a 61 62 61 73 65 20 61 72 65 0a 2a 2a 20 61 6c 6c  abase are.** all
2347b 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f  owed to refer to
2347c 20 61 6e 79 74 68 69 6e 67 2e 29 20 20 49 66 20   anything.)  If 
2347d 61 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 65  a reference is e
2347e 78 70 6c 69 63 69 74 6c 79 20 6d 61 64 65 0a 2a  xplicitly made.*
2347f 2a 20 74 6f 20 61 6e 20 6f 62 6a 65 63 74 20 69  * to an object i
23480 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61  n a different da
23481 74 61 62 61 73 65 2c 20 61 6e 20 65 72 72 6f 72  tabase, an error
23482 20 6d 65 73 73 61 67 65 20 69 73 20 61 64 64 65   message is adde
23483 64 20 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2d 3e  d to.** pParse->
23484 7a 45 72 72 4d 73 67 20 61 6e 64 20 74 68 65 73  zErrMsg and thes
23485 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
23486 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20  n non-zero.  If 
23487 65 76 65 72 79 74 68 69 6e 67 0a 2a 2a 20 63 68  everything.** ch
23488 65 63 6b 73 20 6f 75 74 2c 20 74 68 65 73 65 20  ecks out, these 
23489 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
2348a 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  0..*/.SQLITE_PRI
2348b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2348c 46 69 78 53 72 63 4c 69 73 74 28 0a 20 20 44 62  FixSrcList(.  Db
2348d 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20  Fixer *pFix,    
2348e 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66     /* Context of
2348f 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f   the fixation */
23490 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73  .  SrcList *pLis
23491 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53  t       /* The S
23492 6f 75 72 63 65 20 6c 69 73 74 20 74 6f 20 63 68  ource list to ch
23493 65 63 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20 2a  eck and modify *
23494 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
23495 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
23496 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
23497 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a  t_item *pItem;..
23498 20 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73    if( NEVER(pLis
23499 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30  t==0) ) return 0
2349a 3b 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d 3e  ;.  zDb = pFix->
2349b 7a 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  zDb;.  for(i=0, 
2349c 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20  pItem=pList->a; 
2349d 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  i<pList->nSrc; i
2349e 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2349f 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61    if( pItem->zDa
234a0 74 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20 20  tabase==0 ){.   
234a1 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62     pItem->zDatab
234a2 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ase = sqlite3DbS
234a3 74 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61 72  trDup(pFix->pPar
234a4 73 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20 20  se->db, zDb);.  
234a5 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
234a6 74 65 33 53 74 72 49 43 6d 70 28 70 49 74 65 6d  te3StrICmp(pItem
234a7 2d 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62 29  ->zDatabase,zDb)
234a8 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=0 ){.      sql
234a9 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46 69  ite3ErrorMsg(pFi
234aa 78 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20 20  x->pParse,.     
234ab 20 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e 6f      "%s %T canno
234ac 74 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a 65  t reference obje
234ad 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 20  cts in database 
234ae 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70 46  %s",.         pF
234af 69 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78 2d  ix->zType, pFix-
234b0 3e 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a  >pName, pItem->z
234b1 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20  Database);.     
234b2 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
234b3 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
234b4 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
234b5 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
234b6 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
234b7 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
234b8 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20  FixSelect(pFix, 
234b9 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20  pItem->pSelect) 
234ba 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
234bb 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
234bc 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  pr(pFix, pItem->
234bd 70 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31 3b  pOn) ) return 1;
234be 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65  .#endif.  }.  re
234bf 74 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21 64  turn 0;.}.#if !d
234c0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
234c1 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66  IT_VIEW) || !def
234c2 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
234c3 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54 45  _TRIGGER).SQLITE
234c4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
234c5 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a 20  ite3FixSelect(. 
234c6 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20   DbFixer *pFix, 
234c7 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
234c8 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e   of the fixation
234c9 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
234ca 65 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54 68  elect      /* Th
234cb 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
234cc 6e 74 20 74 6f 20 62 65 20 66 69 78 65 64 20 74  nt to be fixed t
234cd 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a  o one database *
234ce 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53  /.){.  while( pS
234cf 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66 28  elect ){.    if(
234d0 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c   sqlite3FixExprL
234d1 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63  ist(pFix, pSelec
234d2 74 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20 20  t->pEList) ){.  
234d3 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
234d4 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
234d5 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 70 46  te3FixSrcList(pF
234d6 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  ix, pSelect->pSr
234d7 63 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  c) ){.      retu
234d8 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
234d9 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
234da 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74  pr(pFix, pSelect
234db 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20  ->pWhere) ){.   
234dc 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
234dd 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
234de 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20  e3FixExpr(pFix, 
234df 70 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e 67  pSelect->pHaving
234e0 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
234e1 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  n 1;.    }.    p
234e2 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74  Select = pSelect
234e3 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20  ->pPrior;.  }.  
234e4 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49  return 0;.}.SQLI
234e5 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
234e6 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a 20  qlite3FixExpr(. 
234e7 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20   DbFixer *pFix, 
234e8 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f      /* Context o
234e9 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a  f the fixation *
234ea 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  /.  Expr *pExpr 
234eb 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
234ec 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66  pression to be f
234ed 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61  ixed to one data
234ee 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69  base */.){.  whi
234ef 6c 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20  le( pExpr ){.   
234f0 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50   if( ExprHasAnyP
234f1 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
234f2 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20 62  P_TokenOnly) ) b
234f3 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 45 78  reak;.    if( Ex
234f4 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
234f5 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
234f6 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  t) ){.      if( 
234f7 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74  sqlite3FixSelect
234f8 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 78 2e  (pFix, pExpr->x.
234f9 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72  pSelect) ) retur
234fa 6e 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  n 1;.    }else{.
234fb 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
234fc 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69  3FixExprList(pFi
234fd 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  x, pExpr->x.pLis
234fe 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  t) ) return 1;. 
234ff 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
23500 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
23501 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
23502 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
23503 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45   1;.    }.    pE
23504 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  xpr = pExpr->pLe
23505 66 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ft;.  }.  return
23506 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49   0;.}.SQLITE_PRI
23507 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
23508 46 69 78 45 78 70 72 4c 69 73 74 28 0a 20 20 44  FixExprList(.  D
23509 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20  bFixer *pFix,   
2350a 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20    /* Context of 
2350b 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a  the fixation */.
2350c 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2350d 74 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72  t    /* The expr
2350e 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78  ession to be fix
2350f 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61  ed to one databa
23510 73 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  se */.){.  int i
23511 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ;.  struct ExprL
23512 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
23513 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
23514 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f  ) return 0;.  fo
23515 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69  r(i=0, pItem=pLi
23516 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e  st->a; i<pList->
23517 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
23518 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  m++){.    if( sq
23519 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69  lite3FixExpr(pFi
2351a 78 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  x, pItem->pExpr)
2351b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2351c 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
2351d 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
2351e 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
2351f 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
23520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
23521 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69  nt sqlite3FixTri
23522 67 67 65 72 53 74 65 70 28 0a 20 20 44 62 46 69  ggerStep(.  DbFi
23523 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f  xer *pFix,     /
23524 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
23525 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54   fixation */.  T
23526 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65  riggerStep *pSte
23527 70 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72  p /* The trigger
23528 20 73 74 65 70 20 62 65 20 66 69 78 65 64 20 74   step be fixed t
23529 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a  o one database *
2352a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53  /.){.  while( pS
2352b 74 65 70 20 29 7b 0a 20 20 20 20 69 66 28 20 73  tep ){.    if( s
2352c 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
2352d 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 53 65  pFix, pStep->pSe
2352e 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72  lect) ){.      r
2352f 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
23530 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
23531 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 74 65  xExpr(pFix, pSte
23532 70 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20  p->pWhere) ){.  
23533 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
23534 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
23535 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70  te3FixExprList(p
23536 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 45 78 70  Fix, pStep->pExp
23537 72 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20  rList) ){.      
23538 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
23539 20 20 20 20 70 53 74 65 70 20 3d 20 70 53 74 65      pStep = pSte
2353a 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  p->pNext;.  }.  
2353b 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
2353c 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
2353d 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 74 74 61 63  *** End of attac
2353e 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
2353f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23541 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
23542 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61  *** Begin file a
23543 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  uth.c **********
23544 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23545 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23546 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a  **/./*.** 2003 J
23547 61 6e 75 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20  anuary 11.**.** 
23548 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
23549 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
2354a 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
2354b 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
2354c 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
2354d 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
2354e 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
2354f 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
23550 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
23551 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
23552 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
23553 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
23554 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
23555 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
23556 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
23557 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
23558 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
23559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2355a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2355b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2355c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2355d 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
2355e 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
2355f 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
23560 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65  ement the sqlite
23561 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
23562 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69 73  ().** API.  This
23563 20 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e 20   facility is an 
23564 6f 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72 65  optional feature
23565 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e   of the library.
23566 20 20 45 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79    Embedded.** sy
23567 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f  stems that do no
23568 74 20 6e 65 65 64 20 74 68 69 73 20 66 61 63 69  t need this faci
23569 6c 69 74 79 20 6d 61 79 20 6f 6d 69 74 20 69 74  lity may omit it
2356a 20 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a   by recompiling.
2356b 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 77  ** the library w
2356c 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49  ith -DSQLITE_OMI
2356d 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d  T_AUTHORIZATION=
2356e 31 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20  1.*/../*.** All 
2356f 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74  of the code in t
23570 68 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20  his file may be 
23571 6f 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e  omitted by defin
23572 69 6e 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  ing a single.** 
23573 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65  macro..*/.#ifnde
23574 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
23575 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a  THORIZATION../*.
23576 2a 2a 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20  ** Set or clear 
23577 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f  the access autho
23578 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
23579 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63  n..**.** The acc
2357a 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  ess authorizatio
2357b 6e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  n function is be
2357c 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74   called during t
2357d 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a  he compilation.*
2357e 2a 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66  * phase to verif
2357f 79 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20  y that the user 
23580 68 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20  has read and/or 
23581 77 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72  write access per
23582 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61  mission on.** va
23583 72 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20  rious fields of 
23584 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
23585 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
23586 74 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75  t to the auth fu
23587 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63  nction.** is a c
23588 6f 70 79 20 6f 66 20 74 68 65 20 33 72 64 20 61  opy of the 3rd a
23589 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
2358a 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65  routine.  The se
2358b 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
2358c 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
2358d 63 74 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20  ction is one of 
2358e 74 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a  these constants:
2358f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  .**.**       SQL
23590 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58  ITE_CREATE_INDEX
23591 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
23592 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a  _CREATE_TABLE.**
23593 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
23594 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a  EATE_TEMP_INDEX.
23595 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
23596 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c  CREATE_TEMP_TABL
23597 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  E.**       SQLIT
23598 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
23599 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
2359a 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
2359b 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
2359c 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52  SQLITE_CREATE_TR
2359d 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
2359e 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45  QLITE_CREATE_VIE
2359f 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  W.**       SQLIT
235a0 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20  E_DELETE.**     
235a1 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e    SQLITE_DROP_IN
235a2 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  DEX.**       SQL
235a3 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a  ITE_DROP_TABLE.*
235a4 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
235a5 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a  ROP_TEMP_INDEX.*
235a6 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
235a7 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a  ROP_TEMP_TABLE.*
235a8 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
235a9 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ROP_TEMP_TRIGGER
235aa 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
235ab 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a  _DROP_TEMP_VIEW.
235ac 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
235ad 44 52 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20  DROP_TRIGGER.** 
235ae 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
235af 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
235b0 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a  SQLITE_INSERT.**
235b1 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52         SQLITE_PR
235b2 41 47 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51  AGMA.**       SQ
235b3 4c 49 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20  LITE_READ.**    
235b4 20 20 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54     SQLITE_SELECT
235b5 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
235b6 5f 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20  _TRANSACTION.** 
235b7 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44        SQLITE_UPD
235b8 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  ATE.**.** The th
235b9 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61  ird and fourth a
235ba 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
235bb 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72  auth function ar
235bc 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a  e the name of.**
235bd 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74   the table and t
235be 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61  he column that a
235bf 72 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65  re being accesse
235c0 64 2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e  d.  The auth fun
235c1 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20  ction.** should 
235c2 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51  return either SQ
235c3 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f  LITE_OK, SQLITE_
235c4 44 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f  DENY, or SQLITE_
235c5 49 47 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53  IGNORE.  If.** S
235c6 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
235c7 72 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74  rned, it means t
235c8 68 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c  hat access is al
235c9 6c 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44  lowed.  SQLITE_D
235ca 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61  ENY.** means tha
235cb 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  t the SQL statem
235cc 65 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72  ent will never-r
235cd 75 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33  un - the sqlite3
235ce 5f 65 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20  _exec() call.** 
235cf 77 69 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68  will return with
235d0 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49   an error.  SQLI
235d1 54 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20  TE_IGNORE means 
235d2 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61  that the SQL sta
235d3 74 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64  tement.** should
235d4 20 72 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74   run but attempt
235d5 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 70  s to read the sp
235d6 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77  ecified column w
235d7 69 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a  ill return NULL.
235d8 2a 2a 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20  ** and attempts 
235d9 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c  to write the col
235da 75 6d 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f  umn will be igno
235db 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69  red..**.** Setti
235dc 6e 67 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  ng the auth func
235dd 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73  tion to NULL dis
235de 61 62 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e  ables this hook.
235df 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a    The default.**
235e0 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20   setting of the 
235e1 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73  auth function is
235e2 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45   NULL..*/.SQLITE
235e3 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
235e4 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
235e5 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
235e6 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
235e7 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
235e8 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
235e9 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
235ea 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69  st char*),.  voi
235eb 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  d *pArg.){.  sql
235ec 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
235ed 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
235ee 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68  b->xAuth = xAuth
235ef 3b 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67  ;.  db->pAuthArg
235f0 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
235f1 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
235f2 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
235f3 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
235f4 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
235f5 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
235f6 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  E_OK;.}../*.** W
235f7 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rite an error me
235f8 73 73 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73  ssage into pPars
235f9 65 2d 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20  e->zErrMsg that 
235fa 65 78 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68  explains that th
235fb 65 0a 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69  e.** user-suppli
235fc 65 64 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ed authorization
235fd 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
235fe 65 64 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61  ed an illegal va
235ff 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lue..*/.static v
23600 6f 69 64 20 73 71 6c 69 74 65 41 75 74 68 42 61  oid sqliteAuthBa
23601 64 52 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73  dReturnCode(Pars
23602 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71  e *pParse){.  sq
23603 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
23604 61 72 73 65 2c 20 22 61 75 74 68 6f 72 69 7a 65  arse, "authorize
23605 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 22 29 3b  r malfunction");
23606 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20  .  pParse->rc = 
23607 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a  SQLITE_ERROR;.}.
23608 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
23609 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
2360a 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 70 65 72  callback for per
2360b 6d 69 73 73 69 6f 6e 20 74 6f 20 72 65 61 64 20  mission to read 
2360c 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 20 66 72 6f 6d  column zCol from
2360d 0a 2a 2a 20 74 61 62 6c 65 20 7a 54 61 62 20 69  .** table zTab i
2360e 6e 20 64 61 74 61 62 61 73 65 20 7a 44 62 2e 20  n database zDb. 
2360f 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73  This function as
23610 73 75 6d 65 73 20 74 68 61 74 20 61 6e 20 61 75  sumes that an au
23611 74 68 6f 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63  thorization.** c
23612 61 6c 6c 62 61 63 6b 20 68 61 73 20 62 65 65 6e  allback has been
23613 20 72 65 67 69 73 74 65 72 65 64 20 28 69 2e 65   registered (i.e
23614 2e 20 74 68 61 74 20 73 71 6c 69 74 65 33 2e 78  . that sqlite3.x
23615 41 75 74 68 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  Auth is not NULL
23616 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49  )..**.** If SQLI
23617 54 45 5f 49 47 4e 4f 52 45 20 69 73 20 72 65 74  TE_IGNORE is ret
23618 75 72 6e 65 64 20 61 6e 64 20 70 45 78 70 72 20  urned and pExpr 
23619 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
2361a 6e 20 70 45 78 70 72 20 69 73 20 63 68 61 6e 67  n pExpr is chang
2361b 65 64 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20  ed.** to an SQL 
2361c 4e 55 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 2e  NULL expression.
2361d 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70   Otherwise, if p
2361e 45 78 70 72 20 69 73 20 4e 55 4c 4c 2c 20 74 68  Expr is NULL, th
2361f 65 6e 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  en SQLITE_IGNORE
23620 0a 2a 2a 20 69 73 20 74 72 65 61 74 65 64 20 61  .** is treated a
23621 73 20 53 51 4c 49 54 45 5f 44 45 4e 59 2e 20 49  s SQLITE_DENY. I
23622 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65  n this case an e
23623 72 72 6f 72 20 69 73 20 6c 65 66 74 20 69 6e 20  rror is left in 
23624 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  pParse..*/.SQLIT
23625 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
23626 6c 69 74 65 33 41 75 74 68 52 65 61 64 43 6f 6c  lite3AuthReadCol
23627 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
23628 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
23629 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
2362a 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63  r context */.  c
2362b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c  onst char *zTab,
2362c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2362d 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  * Table name */.
2362e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
2362f 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
23630 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65    /* Column name
23631 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 20 20 20   */.  int iDb   
23632 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23633 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
23634 66 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64 61 74  f containing dat
23635 61 62 61 73 65 2e 20 2a 2f 0a 29 7b 0a 20 20 73  abase. */.){.  s
23636 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
23637 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f  rse->db;       /
23638 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
23639 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62  e */.  char *zDb
2363a 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
2363b 7a 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f  zName; /* Name o
2363c 66 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  f attached datab
2363d 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ase */.  int rc;
2363e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2363f 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68           /* Auth
23640 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
23641 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 72 63 20 3d   code */..  rc =
23642 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70   db->xAuth(db->p
23643 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54 45 5f  AuthArg, SQLITE_
23644 52 45 41 44 2c 20 7a 54 61 62 2c 7a 43 6f 6c 2c  READ, zTab,zCol,
23645 7a 44 62 2c 70 50 61 72 73 65 2d 3e 7a 41 75 74  zDb,pParse->zAut
23646 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28  hContext);.  if(
23647 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59   rc==SQLITE_DENY
23648 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
23649 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21 3d 30 20  nDb>2 || iDb!=0 
2364a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2364b 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2364c 20 22 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25   "access to %s.%
2364d 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74  s.%s is prohibit
2364e 65 64 22 2c 7a 44 62 2c 7a 54 61 62 2c 7a 43 6f  ed",zDb,zTab,zCo
2364f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  l);.    }else{. 
23650 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
23651 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63  rMsg(pParse, "ac
23652 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20 69 73  cess to %s.%s is
23653 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20 7a 54   prohibited", zT
23654 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  ab, zCol);.    }
23655 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
23656 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20  = SQLITE_AUTH;. 
23657 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53   }else if( rc!=S
23658 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 26 26 20  QLITE_IGNORE && 
23659 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2365a 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 42  .    sqliteAuthB
2365b 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61  adReturnCode(pPa
2365c 72 73 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rse);.  }.  retu
2365d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2365e 54 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c 64  The pExpr should
2365f 20 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20   be a TK_COLUMN 
23660 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65  expression.  The
23661 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20   table referred 
23662 74 6f 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61 62  to.** is in pTab
23663 4c 69 73 74 20 6f 72 20 65 6c 73 65 20 69 74 20  List or else it 
23664 69 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c  is the NEW or OL
23665 44 20 74 61 62 6c 65 20 6f 66 20 61 20 74 72 69  D table of a tri
23666 67 67 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b  gger.  .** Check
23667 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 69 73   to see if it is
23668 20 4f 4b 20 74 6f 20 72 65 61 64 20 74 68 69 73   OK to read this
23669 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75   particular colu
2366a 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  mn..**.** If the
2366b 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72   auth function r
2366c 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47  eturns SQLITE_IG
2366d 4e 4f 52 45 2c 20 63 68 61 6e 67 65 20 74 68 65  NORE, change the
2366e 20 54 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69   TK_COLUMN .** i
2366f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20  nstruction into 
23670 61 20 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74  a TK_NULL.  If t
23671 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  he auth function
23672 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
23673 44 45 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65  DENY,.** then ge
23674 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e  nerate an error.
23675 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23676 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
23677 75 74 68 52 65 61 64 28 0a 20 20 50 61 72 73 65  uthRead(.  Parse
23678 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
23679 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
2367a 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
2367b 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
2367c 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
2367d 69 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75 74  ion to check aut
2367e 68 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f  horization on */
2367f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  .  Schema *pSche
23680 6d 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ma,      /* The 
23681 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78  schema of the ex
23682 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72  pression */.  Sr
23683 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20  cList *pTabList 
23684 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65      /* All table
23685 20 74 68 61 74 20 70 45 78 70 72 20 6d 69 67 68   that pExpr migh
23686 74 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b  t refer to */.){
23687 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23688 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 54   pParse->db;.  T
23689 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20  able *pTab = 0; 
2368a 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2368b 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a  e being read */.
2368c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
2368d 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ol;     /* Name 
2368e 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66  of the column of
2368f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
23690 69 6e 74 20 69 53 72 63 3b 20 20 20 20 20 20 20  int iSrc;       
23691 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
23692 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 20  n pTabList->a[] 
23693 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72  of table being r
23694 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ead */.  int iDb
23695 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
23696 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74  * The index of t
23697 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
23698 65 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65 72  expression refer
23699 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 43  s to */.  int iC
2369a 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
2369b 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
2369c 6d 6e 20 69 6e 20 74 61 62 6c 65 20 2a 2f 0a 0a  mn in table */..
2369d 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d    if( db->xAuth=
2369e 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
2369f 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
236a0 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
236a1 2d 3e 64 62 2c 20 70 53 63 68 65 6d 61 29 3b 0a  ->db, pSchema);.
236a2 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20    if( iDb<0 ){. 
236a3 20 20 20 2f 2a 20 41 6e 20 61 74 74 65 6d 70 74     /* An attempt
236a4 20 74 6f 20 72 65 61 64 20 61 20 63 6f 6c 75 6d   to read a colum
236a5 6e 20 6f 75 74 20 6f 66 20 61 20 73 75 62 71 75  n out of a subqu
236a6 65 72 79 20 6f 72 20 6f 74 68 65 72 0a 20 20 20  ery or other.   
236a7 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61   ** temporary ta
236a8 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ble. */.    retu
236a9 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  rn;.  }..  asser
236aa 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  t( pExpr->op==TK
236ab 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72  _COLUMN || pExpr
236ac 2d 3e 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52  ->op==TK_TRIGGER
236ad 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d   );.  if( pExpr-
236ae 3e 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20  >op==TK_TRIGGER 
236af 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50  ){.    pTab = pP
236b0 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
236b1 62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  b;.  }else{.    
236b2 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74  assert( pTabList
236b3 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 53 72 63   );.    for(iSrc
236b4 3d 30 3b 20 41 4c 57 41 59 53 28 69 53 72 63 3c  =0; ALWAYS(iSrc<
236b5 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b  pTabList->nSrc);
236b6 20 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 20 20   iSrc++){.      
236b7 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  if( pExpr->iTabl
236b8 65 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  e==pTabList->a[i
236b9 53 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 7b 0a  Src].iCursor ){.
236ba 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
236bb 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d  TabList->a[iSrc]
236bc 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 62  .pTab;.        b
236bd 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
236be 20 20 7d 0a 20 20 7d 0a 20 20 69 43 6f 6c 20 3d    }.  }.  iCol =
236bf 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
236c0 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 61  .  if( NEVER(pTa
236c1 62 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a  b==0) ) return;.
236c2 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29  .  if( iCol>=0 )
236c3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 43  {.    assert( iC
236c4 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b  ol<pTab->nCol );
236c5 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62  .    zCol = pTab
236c6 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61  ->aCol[iCol].zNa
236c7 6d 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  me;.  }else if( 
236c8 70 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29  pTab->iPKey>=0 )
236c9 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  {.    assert( pT
236ca 61 62 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e  ab->iPKey<pTab->
236cb 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c  nCol );.    zCol
236cc 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54   = pTab->aCol[pT
236cd 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65  ab->iPKey].zName
236ce 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
236cf 43 6f 6c 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20  Col = "ROWID";. 
236d0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62   }.  assert( iDb
236d1 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
236d2 44 62 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  Db );.  if( SQLI
236d3 54 45 5f 49 47 4e 4f 52 45 3d 3d 73 71 6c 69 74  TE_IGNORE==sqlit
236d4 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 70 50  e3AuthReadCol(pP
236d5 61 72 73 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  arse, pTab->zNam
236d6 65 2c 20 7a 43 6f 6c 2c 20 69 44 62 29 20 29 7b  e, zCol, iDb) ){
236d7 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d  .    pExpr->op =
236d8 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a   TK_NULL;.  }.}.
236d9 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74  ./*.** Do an aut
236da 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b  horization check
236db 20 75 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20   using the code 
236dc 61 6e 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69  and arguments gi
236dd 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  ven.  Return.** 
236de 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b  either SQLITE_OK
236df 20 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54   (zero) or SQLIT
236e0 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49  E_IGNORE or SQLI
236e1 54 45 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c  TE_DENY.  If SQL
236e2 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72  ITE_DENY.** is r
236e3 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68  eturned, then th
236e4 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e  e error count an
236e5 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  d error message 
236e6 69 6e 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a  in pParse are.**
236e7 20 6d 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70   modified approp
236e8 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  riately..*/.SQLI
236e9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
236ea 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
236eb 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
236ec 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20  ,.  int code,.  
236ed 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67  const char *zArg
236ee 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  1,.  const char 
236ef 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20  *zArg2,.  const 
236f0 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20  char *zArg3.){. 
236f1 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
236f2 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
236f3 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74   rc;..  /* Don't
236f4 20 64 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a   do any authoriz
236f5 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20  ation checks if 
236f6 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
236f7 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a  initialising.  *
236f8 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 72 73  * or if the pars
236f9 65 72 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f  er is being invo
236fa 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ked from within 
236fb 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
236fc 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  vtab..  */.  if(
236fd 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c   db->init.busy |
236fe 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41  | IN_DECLARE_VTA
236ff 42 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  B ){.    return 
23700 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
23701 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d    if( db->xAuth=
23702 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
23703 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
23704 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68    rc = db->xAuth
23705 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 63  (db->pAuthArg, c
23706 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 67  ode, zArg1, zArg
23707 32 2c 20 7a 41 72 67 33 2c 20 70 50 61 72 73 65  2, zArg3, pParse
23708 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b  ->zAuthContext);
23709 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2370a 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 71  E_DENY ){.    sq
2370b 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2370c 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f  arse, "not autho
2370d 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 61  rized");.    pPa
2370e 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
2370f 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69  _AUTH;.  }else i
23710 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
23711 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49   && rc!=SQLITE_I
23712 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 20  GNORE ){.    rc 
23713 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20  = SQLITE_DENY;. 
23714 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61 64     sqliteAuthBad
23715 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73  ReturnCode(pPars
23716 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
23717 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75   rc;.}../*.** Pu
23718 73 68 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74  sh an authorizat
23719 69 6f 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66  ion context.  Af
2371a 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
2371b 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a   is called, the.
2371c 2a 2a 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e  ** zArg3 argumen
2371d 74 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69  t to authorizati
2371e 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c  on callbacks wil
2371f 6c 20 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e  l be zContext un
23720 74 69 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20  til.** popped.  
23721 4f 72 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c  Or if pParse==0,
23722 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
23723 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c   a no-op..*/.SQL
23724 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
23725 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
23726 65 78 74 50 75 73 68 28 0a 20 20 50 61 72 73 65  extPush(.  Parse
23727 20 2a 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68   *pParse,.  Auth
23728 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  Context *pContex
23729 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t, .  const char
2372a 20 2a 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20   *zContext.){.  
2372b 61 73 73 65 72 74 28 20 70 50 61 72 73 65 20 29  assert( pParse )
2372c 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50  ;.  pContext->pP
2372d 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2372e 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68   pContext->zAuth
2372f 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65  Context = pParse
23730 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a  ->zAuthContext;.
23731 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43    pParse->zAuthC
23732 6f 6e 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78  ontext = zContex
23733 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20  t;.}../*.** Pop 
23734 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  an authorization
23735 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77 61   context that wa
23736 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 75 73  s previously pus
23737 68 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65  hed.** by sqlite
23738 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68  3AuthContextPush
23739 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2373a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
2373b 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75  uthContextPop(Au
2373c 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  thContext *pCont
2373d 65 78 74 29 7b 0a 20 20 69 66 28 20 70 43 6f 6e  ext){.  if( pCon
2373e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 29 7b 0a  text->pParse ){.
2373f 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50      pContext->pP
23740 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
23741 78 74 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 7a  xt = pContext->z
23742 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20  AuthContext;.   
23743 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73   pContext->pPars
23744 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 23 65  e = 0;.  }.}..#e
23745 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
23746 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
23747 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  N */../*********
23748 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 75 74  ***** End of aut
23749 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
2374a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2374b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2374c 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2374d 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2374e 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a   build.c *******
2374f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23751 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
23752 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
23753 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
23754 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
23755 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
23756 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
23757 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
23758 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
23759 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2375a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2375b 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2375c 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2375d 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2375e 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2375f 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
23760 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
23761 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
23762 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
23763 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
23764 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
23765 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23766 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23767 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23768 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
23769 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2376a 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e  ns C code routin
2376b 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c  es that are call
2376c 65 64 20 62 79 20 74 68 65 20 53 51 4c 69 74 65  ed by the SQLite
2376d 20 70 61 72 73 65 72 0a 2a 2a 20 77 68 65 6e 20   parser.** when 
2376e 73 79 6e 74 61 78 20 72 75 6c 65 73 20 61 72 65  syntax rules are
2376f 20 72 65 64 75 63 65 64 2e 20 20 54 68 65 20 72   reduced.  The r
23770 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
23771 66 69 6c 65 20 68 61 6e 64 6c 65 20 74 68 65 0a  file handle the.
23772 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b 69 6e  ** following kin
23773 64 73 20 6f 66 20 53 51 4c 20 73 79 6e 74 61 78  ds of SQL syntax
23774 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41  :.**.**     CREA
23775 54 45 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20  TE TABLE.**     
23776 44 52 4f 50 20 54 41 42 4c 45 0a 2a 2a 20 20 20  DROP TABLE.**   
23777 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 0a 2a    CREATE INDEX.*
23778 2a 20 20 20 20 20 44 52 4f 50 20 49 4e 44 45 58  *     DROP INDEX
23779 0a 2a 2a 20 20 20 20 20 63 72 65 61 74 69 6e 67  .**     creating
2377a 20 49 44 20 6c 69 73 74 73 0a 2a 2a 20 20 20 20   ID lists.**    
2377b 20 42 45 47 49 4e 20 54 52 41 4e 53 41 43 54 49   BEGIN TRANSACTI
2377c 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d 49 54  ON.**     COMMIT
2377d 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b  .**     ROLLBACK
2377e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .*/../*.** This 
2377f 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
23780 64 20 77 68 65 6e 20 61 20 6e 65 77 20 53 51 4c  d when a new SQL
23781 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65   statement is be
23782 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 62 65  ginning to.** be
23783 20 70 61 72 73 65 64 2e 20 20 49 6e 69 74 69 61   parsed.  Initia
23784 6c 69 7a 65 20 74 68 65 20 70 50 61 72 73 65 20  lize the pParse 
23785 73 74 72 75 63 74 75 72 65 20 61 73 20 6e 65 65  structure as nee
23786 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ded..*/.SQLITE_P
23787 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23788 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 61  te3BeginParse(Pa
23789 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
2378a 20 65 78 70 6c 61 69 6e 46 6c 61 67 29 7b 0a 20   explainFlag){. 
2378b 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
2378c 20 3d 20 28 75 38 29 65 78 70 6c 61 69 6e 46 6c   = (u8)explainFl
2378d 61 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56  ag;.  pParse->nV
2378e 61 72 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64  ar = 0;.}..#ifnd
2378f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
23790 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a  HARED_CACHE./*.*
23791 2a 20 54 68 65 20 54 61 62 6c 65 4c 6f 63 6b 20  * The TableLock 
23792 73 74 72 75 63 74 75 72 65 20 69 73 20 6f 6e 6c  structure is onl
23793 79 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71  y used by the sq
23794 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29  lite3TableLock()
23795 20 61 6e 64 0a 2a 2a 20 63 6f 64 65 54 61 62 6c   and.** codeTabl
23796 65 4c 6f 63 6b 73 28 29 20 66 75 6e 63 74 69 6f  eLocks() functio
23797 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61  ns..*/.struct Ta
23798 62 6c 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74 20  bleLock {.  int 
23799 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
2379a 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2379b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2379c 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b  table to be lock
2379d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  ed */.  int iTab
2379e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2379f 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  The root page of
237a0 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
237a1 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20   locked */.  u8 
237a2 69 73 57 72 69 74 65 4c 6f 63 6b 3b 20 20 20 20  isWriteLock;    
237a3 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 77 72    /* True for wr
237a4 69 74 65 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65  ite lock.  False
237a5 20 66 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b   for a read lock
237a6 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
237a7 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 4e 61   *zName;   /* Na
237a8 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
237a9 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  */.};../*.** Rec
237aa 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61  ord the fact tha
237ab 74 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 6f 63  t we want to loc
237ac 6b 20 61 20 74 61 62 6c 65 20 61 74 20 72 75 6e  k a table at run
237ad 2d 74 69 6d 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  -time.  .**.** T
237ae 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c  he table to be l
237af 6f 63 6b 65 64 20 68 61 73 20 72 6f 6f 74 20 70  ocked has root p
237b0 61 67 65 20 69 54 61 62 20 61 6e 64 20 69 73 20  age iTab and is 
237b1 66 6f 75 6e 64 20 69 6e 20 64 61 74 61 62 61 73  found in databas
237b2 65 20 69 44 62 2e 0a 2a 2a 20 41 20 72 65 61 64  e iDb..** A read
237b3 20 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b   or a write lock
237b4 20 63 61 6e 20 62 65 20 74 61 6b 65 6e 20 64 65   can be taken de
237b5 70 65 6e 64 69 6e 67 20 6f 6e 20 69 73 57 72 69  pending on isWri
237b6 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  telock..**.** Th
237b7 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20  is routine just 
237b8 72 65 63 6f 72 64 73 20 74 68 65 20 66 61 63 74  records the fact
237b9 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 69   that the lock i
237ba 73 20 64 65 73 69 72 65 64 2e 20 20 54 68 65 0a  s desired.  The.
237bb 2a 2a 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20  ** code to make 
237bc 74 68 65 20 6c 6f 63 6b 20 6f 63 63 75 72 20 69  the lock occur i
237bd 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  s generated by a
237be 20 6c 61 74 65 72 20 63 61 6c 6c 20 74 6f 0a 2a   later call to.*
237bf 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73  * codeTableLocks
237c0 28 29 20 77 68 69 63 68 20 6f 63 63 75 72 73 20  () which occurs 
237c1 64 75 72 69 6e 67 20 73 71 6c 69 74 65 33 46 69  during sqlite3Fi
237c2 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2f  nishCoding()..*/
237c3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
237c4 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c  void sqlite3Tabl
237c5 65 4c 6f 63 6b 28 0a 20 20 50 61 72 73 65 20 2a  eLock(.  Parse *
237c6 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50  pParse,     /* P
237c7 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
237c8 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20  /.  int iDb,    
237c9 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
237ca 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
237cb 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
237cc 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  able to lock */.
237cd 20 20 69 6e 74 20 69 54 61 62 2c 20 20 20 20 20    int iTab,     
237ce 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
237cf 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
237d0 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b  table to be lock
237d1 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72 69  ed */.  u8 isWri
237d2 74 65 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 54 72  teLock,    /* Tr
237d3 75 65 20 66 6f 72 20 61 20 77 72 69 74 65 20 6c  ue for a write l
237d4 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ock */.  const c
237d5 68 61 72 20 2a 7a 4e 61 6d 65 20 20 2f 2a 20 4e  har *zName  /* N
237d6 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
237d7 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
237d8 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f  .){.  Parse *pTo
237d9 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33  plevel = sqlite3
237da 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
237db 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 69 3b 0a  arse);.  int i;.
237dc 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 0a 20 20    int nBytes;.  
237dd 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0a 20 20  TableLock *p;.  
237de 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29  assert( iDb>=0 )
237df 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
237e0 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c  pToplevel->nTabl
237e1 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20  eLock; i++){.   
237e2 20 70 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c 2d   p = &pToplevel-
237e3 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a  >aTableLock[i];.
237e4 20 20 20 20 69 66 28 20 70 2d 3e 69 44 62 3d 3d      if( p->iDb==
237e5 69 44 62 20 26 26 20 70 2d 3e 69 54 61 62 3d 3d  iDb && p->iTab==
237e6 69 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70 2d  iTab ){.      p-
237e7 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28  >isWriteLock = (
237e8 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c  p->isWriteLock |
237e9 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a  | isWriteLock);.
237ea 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
237eb 20 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65    }.  }..  nByte
237ec 73 20 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c 65  s = sizeof(Table
237ed 4c 6f 63 6b 29 20 2a 20 28 70 54 6f 70 6c 65 76  Lock) * (pToplev
237ee 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31  el->nTableLock+1
237ef 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e  );.  pToplevel->
237f0 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 0a 20 20 20  aTableLock =.   
237f1 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c     sqlite3DbReal
237f2 6c 6f 63 4f 72 46 72 65 65 28 70 54 6f 70 6c 65  locOrFree(pTople
237f3 76 65 6c 2d 3e 64 62 2c 20 70 54 6f 70 6c 65 76  vel->db, pToplev
237f4 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20  el->aTableLock, 
237f5 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70  nBytes);.  if( p
237f6 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65  Toplevel->aTable
237f7 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d 20  Lock ){.    p = 
237f8 26 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62  &pToplevel->aTab
237f9 6c 65 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65 6c  leLock[pToplevel
237fa 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b  ->nTableLock++];
237fb 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d 20 69 44  .    p->iDb = iD
237fc 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 20 3d  b;.    p->iTab =
237fd 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 73   iTab;.    p->is
237fe 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69 73 57 72  WriteLock = isWr
237ff 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 70 2d 3e  iteLock;.    p->
23800 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20  zName = zName;. 
23801 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 70   }else{.    pTop
23802 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63  level->nTableLoc
23803 6b 20 3d 20 30 3b 0a 20 20 20 20 70 54 6f 70 6c  k = 0;.    pTopl
23804 65 76 65 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  evel->db->malloc
23805 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
23806 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e  }../*.** Code an
23807 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e   OP_TableLock in
23808 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61  struction for ea
23809 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20  ch table locked 
2380a 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d  by the.** statem
2380b 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20  ent (configured 
2380c 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  by calls to sqli
2380d 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e  te3TableLock()).
2380e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2380f 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50  codeTableLocks(P
23810 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
23811 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a   int i;.  Vdbe *
23812 70 56 64 62 65 3b 20 0a 0a 20 20 70 56 64 62 65  pVdbe; ..  pVdbe
23813 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
23814 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73  e(pParse);.  ass
23815 65 72 74 28 20 70 56 64 62 65 21 3d 30 20 29 3b  ert( pVdbe!=0 );
23816 20 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 56 64   /* sqlite3GetVd
23817 62 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 3a 20  be cannot fail: 
23818 56 44 42 45 20 61 6c 72 65 61 64 79 20 61 6c 6c  VDBE already all
23819 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 66 6f 72  ocated */..  for
2381a 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
2381b 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29  nTableLock; i++)
2381c 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b 20  {.    TableLock 
2381d 2a 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 54  *p = &pParse->aT
2381e 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20  ableLock[i];.   
2381f 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44 62   int p1 = p->iDb
23820 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23821 65 41 64 64 4f 70 34 28 70 56 64 62 65 2c 20 4f  eAddOp4(pVdbe, O
23822 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c  P_TableLock, p1,
23823 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73 57   p->iTab, p->isW
23824 72 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 20  riteLock,.      
23825 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23826 70 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41  p->zName, P4_STA
23827 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73  TIC);.  }.}.#els
23828 65 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64 65  e.  #define code
23829 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23 65  TableLocks(x).#e
2382a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
2382b 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2382c 65 64 20 61 66 74 65 72 20 61 20 73 69 6e 67 6c  ed after a singl
2382d 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
2382e 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72 73  has been.** pars
2382f 65 64 20 61 6e 64 20 61 20 56 44 42 45 20 70 72  ed and a VDBE pr
23830 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65  ogram to execute
23831 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20   that statement 
23832 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 70  has been.** prep
23833 61 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74  ared.  This rout
23834 69 6e 65 20 70 75 74 73 20 74 68 65 20 66 69 6e  ine puts the fin
23835 69 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20 6f  ishing touches o
23836 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70 72  n the.** VDBE pr
23837 6f 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74 73  ogram and resets
23838 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75   the pParse stru
23839 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e 65  cture for the ne
2383a 78 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a  xt.** parse..**.
2383b 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  ** Note that if 
2383c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65  an error occurre
2383d 64 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74  d, it might be t
2383e 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a 20  he case that.** 
2383f 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61 73  no VDBE code was
23840 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 53   generated..*/.S
23841 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
23842 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  id sqlite3Finish
23843 43 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70 50  Coding(Parse *pP
23844 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
23845 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b   *db;.  Vdbe *v;
23846 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
23847 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  >db;.  if( db->m
23848 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
23849 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72  turn;.  if( pPar
2384a 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 74  se->nested ) ret
2384b 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73  urn;.  if( pPars
2384c 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e  e->nErr ) return
2384d 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 79  ;..  /* Begin by
2384e 20 67 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d 65   generating some
2384f 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64   termination cod
23850 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  e at the end of 
23851 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72  the.  ** vdbe pr
23852 6f 67 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d  ogram.  */.  v =
23853 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
23854 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72  pParse);.  asser
23855 74 28 20 21 70 50 61 72 73 65 2d 3e 69 73 4d 75  t( !pParse->isMu
23856 6c 74 69 57 72 69 74 65 20 0a 20 20 20 20 20 20  ltiWrite .      
23857 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 41   || sqlite3VdbeA
23858 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 76 2c  ssertMayAbort(v,
23859 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72   pParse->mayAbor
2385a 74 29 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  t));.  if( v ){.
2385b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2385c 64 64 4f 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp0(v, OP_Halt
2385d 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
2385e 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61  ookie mask conta
2385f 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20  ins one bit for 
23860 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69  each database fi
23861 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20  le open..    ** 
23862 28 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61  (Bit 0 is for ma
23863 69 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72  in, bit 1 is for
23864 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f   temp, and so fo
23865 72 74 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a  rth.)  Bits are.
23866 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65      ** set for e
23867 61 63 68 20 64 61 74 61 62 61 73 65 20 74 68 61  ach database tha
23868 74 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65  t is used.  Gene
23869 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61  rate code to sta
2386a 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e  rt a.    ** tran
2386b 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20  saction on each 
2386c 75 73 65 64 20 64 61 74 61 62 61 73 65 20 61 6e  used database an
2386d 64 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 20  d to verify the 
2386e 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20  schema cookie.  
2386f 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65    ** on each use
23870 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  d database..    
23871 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73  */.    if( pPars
23872 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20  e->cookieGoto>0 
23873 29 7b 0a 20 20 20 20 20 20 75 33 32 20 6d 61 73  ){.      u32 mas
23874 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  k;.      int iDb
23875 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
23876 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70  dbeJumpHere(v, p
23877 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
23878 6f 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  o-1);.      for(
23879 69 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69  iDb=0, mask=1; i
2387a 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b  Db<db->nDb; mask
2387b 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20  <<=1, iDb++){.  
2387c 20 20 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20        if( (mask 
2387d 26 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  & pParse->cookie
2387e 4d 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69  Mask)==0 ) conti
2387f 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nue;.        sql
23880 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
23881 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  e(v, iDb);.     
23882 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23883 64 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61  dOp2(v,OP_Transa
23884 63 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73  ction, iDb, (mas
23885 6b 20 26 20 70 50 61 72 73 65 2d 3e 77 72 69 74  k & pParse->writ
23886 65 4d 61 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20  eMask)!=0);.    
23887 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74      if( db->init
23888 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20  .busy==0 ){.    
23889 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2388a 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 56 65 72  eAddOp2(v,OP_Ver
2388b 69 66 79 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  ifyCookie, iDb, 
2388c 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61  pParse->cookieVa
2388d 6c 75 65 5b 69 44 62 5d 29 3b 0a 20 20 20 20 20  lue[iDb]);.     
2388e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66     }.      }.#if
2388f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23890 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
23891 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e      {.        in
23892 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t i;.        for
23893 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
23894 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b  nVtabLock; i++){
23895 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
23896 2a 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29  *vtab = (char *)
23897 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65  sqlite3GetVTable
23898 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56  (db, pParse->apV
23899 74 61 62 4c 6f 63 6b 5b 69 5d 29 3b 0a 20 20 20  tabLock[i]);.   
2389a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2389b 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
2389c 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c 20  Begin, 0, 0, 0, 
2389d 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a  vtab, P4_VTAB);.
2389e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2389f 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c    pParse->nVtabL
238a0 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ock = 0;.      }
238a1 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f  .#endif..      /
238a2 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 63  * Once all the c
238a3 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65 65 6e  ookies have been
238a4 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 74 72   verified and tr
238a5 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 65  ansactions opene
238a6 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62 74  d, .      ** obt
238a7 61 69 6e 20 74 68 65 20 72 65 71 75 69 72 65 64  ain the required
238a8 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54 68   table-locks. Th
238a9 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75 6e  is is a no-op un
238aa 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20 20 20  less the .      
238ab 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  ** shared-cache 
238ac 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c  feature is enabl
238ad 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
238ae 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b     codeTableLock
238af 73 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20  s(pParse);..    
238b0 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
238b1 61 6e 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  any AUTOINCREMEN
238b2 54 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  T data structure
238b3 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  s required..    
238b4 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
238b5 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42  e3AutoincrementB
238b6 65 67 69 6e 28 70 50 61 72 73 65 29 3b 0a 0a 20  egin(pParse);.. 
238b7 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c       /* Finally,
238b8 20 6a 75 6d 70 20 62 61 63 6b 20 74 6f 20 74 68   jump back to th
238b9 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
238ba 68 65 20 65 78 65 63 75 74 61 62 6c 65 20 63 6f  he executable co
238bb 64 65 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  de. */.      sql
238bc 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
238bd 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50  , OP_Goto, 0, pP
238be 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f  arse->cookieGoto
238bf 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20  );.    }.  }... 
238c0 20 2f 2a 20 47 65 74 20 74 68 65 20 56 44 42 45   /* Get the VDBE
238c1 20 70 72 6f 67 72 61 6d 20 72 65 61 64 79 20 66   program ready f
238c2 6f 72 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a  or execution.  *
238c3 2f 0a 20 20 69 66 28 20 76 20 26 26 20 41 4c 57  /.  if( v && ALW
238c4 41 59 53 28 70 50 61 72 73 65 2d 3e 6e 45 72 72  AYS(pParse->nErr
238c5 3d 3d 30 29 20 26 26 20 21 64 62 2d 3e 6d 61 6c  ==0) && !db->mal
238c6 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 23 69 66  locFailed ){.#if
238c7 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
238c8 0a 20 20 20 20 46 49 4c 45 20 2a 74 72 61 63 65  .    FILE *trace
238c9 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20   = (db->flags & 
238ca 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65  SQLITE_VdbeTrace
238cb 29 21 3d 30 20 3f 20 73 74 64 6f 75 74 20 3a 20  )!=0 ? stdout : 
238cc 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  0;.    sqlite3Vd
238cd 62 65 54 72 61 63 65 28 76 2c 20 74 72 61 63 65  beTrace(v, trace
238ce 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73  );.#endif.    as
238cf 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43  sert( pParse->iC
238d0 61 63 68 65 4c 65 76 65 6c 3d 3d 30 20 29 3b 20  acheLevel==0 ); 
238d1 20 2f 2a 20 44 69 73 61 62 6c 65 73 20 61 6e 64   /* Disables and
238d2 20 72 65 2d 65 6e 61 62 6c 65 73 20 6d 61 74 63   re-enables matc
238d3 68 20 2a 2f 0a 20 20 20 20 2f 2a 20 41 20 6d 69  h */.    /* A mi
238d4 6e 69 6d 75 6d 20 6f 66 20 6f 6e 65 20 63 75 72  nimum of one cur
238d5 73 6f 72 20 69 73 20 72 65 71 75 69 72 65 64 20  sor is required 
238d6 69 66 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  if autoincrement
238d7 20 69 73 20 75 73 65 64 0a 20 20 20 20 2a 20 20   is used.    *  
238d8 53 65 65 20 74 69 63 6b 65 74 20 5b 61 36 39 36  See ticket [a696
238d9 33 37 39 63 31 66 30 38 38 36 36 5d 20 2a 2f 0a  379c1f08866] */.
238da 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
238db 70 41 69 6e 63 21 3d 30 20 26 26 20 70 50 61 72  pAinc!=0 && pPar
238dc 73 65 2d 3e 6e 54 61 62 3d 3d 30 20 29 20 70 50  se->nTab==0 ) pP
238dd 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a  arse->nTab = 1;.
238de 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
238df 61 6b 65 52 65 61 64 79 28 76 2c 20 70 50 61 72  akeReady(v, pPar
238e0 73 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72 73 65  se->nVar, pParse
238e1 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20  ->nMem,.        
238e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
238e3 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2c 20 70   pParse->nTab, p
238e4 50 61 72 73 65 2d 3e 6e 4d 61 78 41 72 67 2c 20  Parse->nMaxArg, 
238e5 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 2c  pParse->explain,
238e6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
238e7 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
238e8 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 26  ->isMultiWrite &
238e9 26 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f  & pParse->mayAbo
238ea 72 74 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  rt);.    pParse-
238eb 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e  >rc = SQLITE_DON
238ec 45 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63  E;.    pParse->c
238ed 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a  olNamesSet = 0;.
238ee 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72    }else if( pPar
238ef 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  se->rc==SQLITE_O
238f0 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  K ){.    pParse-
238f1 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
238f2 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65  OR;.  }.  pParse
238f3 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50  ->nTab = 0;.  pP
238f4 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a  arse->nMem = 0;.
238f5 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d    pParse->nSet =
238f6 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56   0;.  pParse->nV
238f7 61 72 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65  ar = 0;.  pParse
238f8 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30  ->cookieMask = 0
238f9 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  ;.  pParse->cook
238fa 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f  ieGoto = 0;.}../
238fb 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72  *.** Run the par
238fc 73 65 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ser and code gen
238fd 65 72 61 74 6f 72 20 72 65 63 75 72 73 69 76 65  erator recursive
238fe 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67  ly in order to g
238ff 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20  enerate.** code 
23900 66 6f 72 20 74 68 65 20 53 51 4c 20 73 74 61 74  for the SQL stat
23901 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f  ement given onto
23902 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
23903 70 50 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a  pParse context.*
23904 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65  * currently unde
23905 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20  r construction. 
23906 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 72   When the parser
23907 20 69 73 20 72 75 6e 20 72 65 63 75 72 73 69 76   is run recursiv
23908 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c  ely.** this way,
23909 20 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61   the final OP_Ha
2390a 6c 74 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64  lt is not append
2390b 65 64 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69  ed and other ini
2390c 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61  tialization.** a
2390d 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20  nd finalization 
2390e 73 74 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65  steps are omitte
2390f 64 20 62 65 63 61 75 73 65 20 74 68 6f 73 65 20  d because those 
23910 61 72 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20  are handling by 
23911 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74  the.** outermost
23912 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e   parser..**.** N
23913 6f 74 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  ot everything is
23914 20 6e 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73   nestable.  This
23915 20 66 61 63 69 6c 69 74 79 20 69 73 20 64 65 73   facility is des
23916 69 67 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a  igned to permit.
23917 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  ** INSERT, UPDAT
23918 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70  E, and DELETE op
23919 65 72 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74  erations against
2391a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20   SQLITE_MASTER. 
2391b 20 55 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20   Use.** care if 
2391c 79 6f 75 20 64 65 63 69 64 65 20 74 6f 20 74 72  you decide to tr
2391d 79 20 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f  y to use this ro
2391e 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f  utine for some o
2391f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a  ther purposes..*
23920 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23921 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73   void sqlite3Nes
23922 74 65 64 50 61 72 73 65 28 50 61 72 73 65 20 2a  tedParse(Parse *
23923 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
23924 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
23925 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
23926 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20  .  char *zSql;. 
23927 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d   char *zErrMsg =
23928 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   0;.  sqlite3 *d
23929 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2392a 23 20 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a  # define SAVE_SZ
2392b 20 20 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29    (sizeof(Parse)
2392c 20 2d 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73   - offsetof(Pars
2392d 65 2c 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20  e,nVar)).  char 
2392e 73 61 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d  saveBuf[SAVE_SZ]
2392f 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ;..  if( pParse-
23930 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a  >nErr ) return;.
23931 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
23932 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20  ->nested<10 );  
23933 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c  /* Nesting shoul
23934 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d  d only be of lim
23935 69 74 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20  ited depth */.  
23936 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
23937 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20  rmat);.  zSql = 
23938 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
23939 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
2393a 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
2393b 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b    if( zSql==0 ){
2393c 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f  .    return;   /
2393d 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20  * A malloc must 
2393e 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20  have failed */. 
2393f 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73   }.  pParse->nes
23940 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28  ted++;.  memcpy(
23941 73 61 76 65 42 75 66 2c 20 26 70 50 61 72 73 65  saveBuf, &pParse
23942 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29  ->nVar, SAVE_SZ)
23943 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72  ;.  memset(&pPar
23944 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56  se->nVar, 0, SAV
23945 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33  E_SZ);.  sqlite3
23946 52 75 6e 50 61 72 73 65 72 28 70 50 61 72 73 65  RunParser(pParse
23947 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67  , zSql, &zErrMsg
23948 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
23949 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b  ee(db, zErrMsg);
2394a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2394b 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65  (db, zSql);.  me
2394c 6d 63 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56  mcpy(&pParse->nV
2394d 61 72 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56  ar, saveBuf, SAV
2394e 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d  E_SZ);.  pParse-
2394f 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a  >nested--;.}../*
23950 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69  .** Locate the i
23951 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75  n-memory structu
23952 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65  re that describe
23953 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  s a particular d
23954 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65  atabase.** table
23955 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20   given the name 
23956 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e  of that table an
23957 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74  d (optionally) t
23958 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a  he name of the.*
23959 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  * database conta
2395a 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e  ining the table.
2395b 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66    Return NULL if
2395c 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a   not found..**.*
2395d 2a 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 69  * If zDatabase i
2395e 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73  s 0, all databas
2395f 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64 20  es are searched 
23960 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e  for the table an
23961 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d  d the.** first m
23962 61 74 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73  atching table is
23963 20 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20   returned.  (No 
23964 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70  checking for dup
23965 6c 69 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20  licate table.** 
23966 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20  names is done.) 
23967 20 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65   The search orde
23968 72 20 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c  r is TEMP first,
23969 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e   then MAIN, then
2396a 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72   any.** auxiliar
2396b 79 20 64 61 74 61 62 61 73 65 73 20 61 64 64 65  y databases adde
2396c 64 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41  d using the ATTA
2396d 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a  CH command..**.*
2396e 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74  * See also sqlit
2396f 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e  e3LocateTable().
23970 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23971 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
23972 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74  3FindTable(sqlit
23973 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
23974 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74  ar *zName, const
23975 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65   char *zDatabase
23976 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20  ){.  Table *p = 
23977 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  0;.  int i;.  in
23978 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72  t nName;.  asser
23979 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20  t( zName!=0 );. 
2397a 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2397b 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
2397c 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45  .  for(i=OMIT_TE
2397d 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  MPDB; i<db->nDb;
2397e 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a   i++){.    int j
2397f 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a   = (i<2) ? i^1 :
23980 20 69 3b 20 20 20 2f 2a 20 53 65 61 72 63 68 20   i;   /* Search 
23981 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e  TEMP before MAIN
23982 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 61 74   */.    if( zDat
23983 61 62 61 73 65 21 3d 30 20 26 26 20 73 71 6c 69  abase!=0 && sqli
23984 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74 61  te3StrICmp(zData
23985 62 61 73 65 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d  base, db->aDb[j]
23986 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e  .zName) ) contin
23987 75 65 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  ue;.    p = sqli
23988 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d  te3HashFind(&db-
23989 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[j].pSchema-
2398a 3e 74 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  >tblHash, zName,
2398b 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   nName);.    if(
2398c 20 70 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a   p ) break;.  }.
2398d 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
2398e 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20  *.** Locate the 
2398f 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74  in-memory struct
23990 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62  ure that describ
23991 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  es a particular 
23992 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c  database.** tabl
23993 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65  e given the name
23994 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 61   of that table a
23995 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20  nd (optionally) 
23996 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a  the name of the.
23997 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  ** database cont
23998 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
23999 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
2399a 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 20 20 41 6c  f not found.  Al
2399b 73 6f 20 6c 65 61 76 65 20 61 6e 0a 2a 2a 20 65  so leave an.** e
2399c 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20  rror message in 
2399d 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e  pParse->zErrMsg.
2399e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65  .**.** The diffe
2399f 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68  rence between th
239a0 69 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 73  is routine and s
239a1 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
239a2 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a  ) is that this.*
239a3 2a 20 72 6f 75 74 69 6e 65 20 6c 65 61 76 65 73  * routine leaves
239a4 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
239a5 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72  e in pParse->zEr
239a6 72 4d 73 67 20 77 68 65 72 65 0a 2a 2a 20 73 71  rMsg where.** sq
239a7 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29  lite3FindTable()
239a8 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 53 51   does not..*/.SQ
239a9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62  LITE_PRIVATE Tab
239aa 6c 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74  le *sqlite3Locat
239ab 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20  eTable(.  Parse 
239ac 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
239ad 20 2f 2a 20 63 6f 6e 74 65 78 74 20 69 6e 20 77   /* context in w
239ae 68 69 63 68 20 74 6f 20 72 65 70 6f 72 74 20 65  hich to report e
239af 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69  rrors */.  int i
239b0 73 56 69 65 77 2c 20 20 20 20 20 20 20 20 20 20  sView,          
239b1 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 6f 6f    /* True if loo
239b2 6b 69 6e 67 20 66 6f 72 20 61 20 56 49 45 57 20  king for a VIEW 
239b3 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 54 41  rather than a TA
239b4 42 4c 45 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  BLE */.  const c
239b5 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
239b6 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
239b7 61 62 6c 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b  able we are look
239b8 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e  ing for */.  con
239b9 73 74 20 63 68 61 72 20 2a 7a 44 62 61 73 65 20  st char *zDbase 
239ba 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
239bb 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 69  he database.  Mi
239bc 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29  ght be NULL */.)
239bd 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a 20  {.  Table *p;.. 
239be 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74   /* Read the dat
239bf 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66  abase schema. If
239c0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
239c1 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72  , leave an error
239c2 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e   message.  ** an
239c3 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65  d code in pParse
239c4 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c   and return NULL
239c5 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54  . */.  if( SQLIT
239c6 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
239c7 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
239c8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
239c9 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69  .  }..  p = sqli
239ca 74 65 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61  te3FindTable(pPa
239cb 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20  rse->db, zName, 
239cc 7a 44 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  zDbase);.  if( p
239cd 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ==0 ){.    const
239ce 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 69 73   char *zMsg = is
239cf 56 69 65 77 20 3f 20 22 6e 6f 20 73 75 63 68 20  View ? "no such 
239d0 76 69 65 77 22 20 3a 20 22 6e 6f 20 73 75 63 68  view" : "no such
239d1 20 74 61 62 6c 65 22 3b 0a 20 20 20 20 69 66 28   table";.    if(
239d2 20 7a 44 62 61 73 65 20 29 7b 0a 20 20 20 20 20   zDbase ){.     
239d3 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
239d4 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73  (pParse, "%s: %s
239d5 2e 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 44 62 61  .%s", zMsg, zDba
239d6 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  se, zName);.    
239d7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
239d8 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
239d9 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a  rse, "%s: %s", z
239da 4d 73 67 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  Msg, zName);.   
239db 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63   }.    pParse->c
239dc 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a  heckSchema = 1;.
239dd 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
239de 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20  }../*.** Locate 
239df 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74  the in-memory st
239e0 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73  ructure that des
239e1 63 72 69 62 65 73 20 0a 2a 2a 20 61 20 70 61 72  cribes .** a par
239e2 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20 67 69  ticular index gi
239e3 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ven the name of 
239e4 74 68 61 74 20 69 6e 64 65 78 0a 2a 2a 20 61 6e  that index.** an
239e5 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  d the name of th
239e6 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  e database that 
239e7 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6e 64  contains the ind
239e8 65 78 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55  ex..** Return NU
239e9 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e  LL if not found.
239ea 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62  .**.** If zDatab
239eb 61 73 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61  ase is 0, all da
239ec 74 61 62 61 73 65 73 20 61 72 65 20 73 65 61 72  tabases are sear
239ed 63 68 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20  ched for the.** 
239ee 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 66 69  table and the fi
239ef 72 73 74 20 6d 61 74 63 68 69 6e 67 20 69 6e 64  rst matching ind
239f0 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ex is returned. 
239f1 20 28 4e 6f 20 63 68 65 63 6b 69 6e 67 0a 2a 2a   (No checking.**
239f2 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20 69   for duplicate i
239f3 6e 64 65 78 20 6e 61 6d 65 73 20 69 73 20 64 6f  ndex names is do
239f4 6e 65 2e 29 20 20 54 68 65 20 73 65 61 72 63 68  ne.)  The search
239f5 20 6f 72 64 65 72 20 69 73 0a 2a 2a 20 54 45 4d   order is.** TEM
239f6 50 20 66 69 72 73 74 2c 20 74 68 65 6e 20 4d 41  P first, then MA
239f7 49 4e 2c 20 74 68 65 6e 20 61 6e 79 20 61 75 78  IN, then any aux
239f8 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73  iliary databases
239f9 20 61 64 64 65 64 0a 2a 2a 20 75 73 69 6e 67 20   added.** using 
239fa 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61  the ATTACH comma
239fb 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nd..*/.SQLITE_PR
239fc 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c  IVATE Index *sql
239fd 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 73 71  ite3FindIndex(sq
239fe 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
239ff 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f   char *zName, co
23a00 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a  nst char *zDb){.
23a01 20 20 49 6e 64 65 78 20 2a 70 20 3d 20 30 3b 0a    Index *p = 0;.
23a02 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
23a03 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
23a04 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
23a05 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50   for(i=OMIT_TEMP
23a06 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  DB; i<db->nDb; i
23a07 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d  ++){.    int j =
23a08 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69   (i<2) ? i^1 : i
23a09 3b 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45 4d  ;  /* Search TEM
23a0a 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f  P before MAIN */
23a0b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63  .    Schema *pSc
23a0c 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 6a  hema = db->aDb[j
23a0d 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 61  ].pSchema;.    a
23a0e 73 73 65 72 74 28 20 70 53 63 68 65 6d 61 20 29  ssert( pSchema )
23a0f 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 26 26  ;.    if( zDb &&
23a10 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
23a11 7a 44 62 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e  zDb, db->aDb[j].
23a12 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75  zName) ) continu
23a13 65 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  e;.    p = sqlit
23a14 65 33 48 61 73 68 46 69 6e 64 28 26 70 53 63 68  e3HashFind(&pSch
23a15 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e  ema->idxHash, zN
23a16 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
23a17 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a   if( p ) break;.
23a18 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
23a19 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d  }../*.** Reclaim
23a1a 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64   the memory used
23a1b 20 62 79 20 61 6e 20 69 6e 64 65 78 0a 2a 2f 0a   by an index.*/.
23a1c 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
23a1d 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b  Index(Index *p){
23a1e 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23a1f 20 70 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 4d 65   p->pTable->dbMe
23a20 6d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  m;.#ifndef SQLIT
23a21 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 20  E_OMIT_ANALYZE. 
23a22 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e   sqlite3DeleteIn
23a23 64 65 78 53 61 6d 70 6c 65 73 28 70 29 3b 0a 23  dexSamples(p);.#
23a24 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 44  endif.  sqlite3D
23a25 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 43 6f  bFree(db, p->zCo
23a26 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 33  lAff);.  sqlite3
23a27 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d  DbFree(db, p);.}
23a28 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  ../*.** Remove t
23a29 68 65 20 67 69 76 65 6e 20 69 6e 64 65 78 20 66  he given index f
23a2a 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 68 61  rom the index ha
23a2b 73 68 20 74 61 62 6c 65 2c 20 61 6e 64 20 66 72  sh table, and fr
23a2c 65 65 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f 72 79  ee.** its memory
23a2d 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a   structures..**.
23a2e 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 69 73 20  ** The index is 
23a2f 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65  removed from the
23a30 20 64 61 74 61 62 61 73 65 20 68 61 73 68 20 74   database hash t
23a31 61 62 6c 65 73 20 62 75 74 0a 2a 2a 20 69 74 20  ables but.** it 
23a32 69 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65 64 20  is not unlinked 
23a33 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 74  from the Table t
23a34 68 61 74 20 69 74 20 69 6e 64 65 78 65 73 2e 0a  hat it indexes..
23a35 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20 66 72 6f  ** Unlinking fro
23a36 6d 20 74 68 65 20 54 61 62 6c 65 20 6d 75 73 74  m the Table must
23a37 20 62 65 20 64 6f 6e 65 20 62 79 20 74 68 65 20   be done by the 
23a38 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
23a39 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
23a3a 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e   sqlite3DeleteIn
23a3b 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b 0a 20  dex(Index *p){. 
23a3c 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b 0a 20 20   Index *pOld;.  
23a3d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
23a3e 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 0a 20  e = p->zName;.. 
23a3f 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48   pOld = sqlite3H
23a40 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 70 53  ashInsert(&p->pS
23a41 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20  chema->idxHash, 
23a42 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
23a43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23a44 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33    sqlite3Strlen3
23a45 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b 0a 20 20  0(zName), 0);.  
23a46 61 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d 30 20  assert( pOld==0 
23a47 7c 7c 20 70 4f 6c 64 3d 3d 70 20 29 3b 0a 20 20  || pOld==p );.  
23a48 66 72 65 65 49 6e 64 65 78 28 70 29 3b 0a 7d 0a  freeIndex(p);.}.
23a49 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 69  ./*.** For the i
23a4a 6e 64 65 78 20 63 61 6c 6c 65 64 20 7a 49 64 78  ndex called zIdx
23a4b 4e 61 6d 65 20 77 68 69 63 68 20 69 73 20 66 6f  Name which is fo
23a4c 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  und in the datab
23a4d 61 73 65 20 69 44 62 2c 0a 2a 2a 20 75 6e 6c 69  ase iDb,.** unli
23a4e 6b 65 20 74 68 61 74 20 69 6e 64 65 78 20 66 72  ke that index fr
23a4f 6f 6d 20 69 74 73 20 54 61 62 6c 65 20 74 68 65  om its Table the
23a50 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 69 6e 64  n remove the ind
23a51 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69  ex from.** the i
23a52 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 20  ndex hash table 
23a53 61 6e 64 20 66 72 65 65 20 61 6c 6c 20 6d 65 6d  and free all mem
23a54 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 20 61  ory structures a
23a55 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74  ssociated.** wit
23a56 68 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a  h the index..*/.
23a57 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23a58 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  oid sqlite3Unlin
23a59 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28  kAndDeleteIndex(
23a5a 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
23a5b 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   iDb, const char
23a5c 20 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a 20 20 49   *zIdxName){.  I
23a5d 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20  ndex *pIndex;.  
23a5e 69 6e 74 20 6c 65 6e 3b 0a 20 20 48 61 73 68 20  int len;.  Hash 
23a5f 2a 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44  *pHash = &db->aD
23a60 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
23a61 69 64 78 48 61 73 68 3b 0a 0a 20 20 6c 65 6e 20  idxHash;..  len 
23a62 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
23a63 30 28 7a 49 64 78 4e 61 6d 65 29 3b 0a 20 20 70  0(zIdxName);.  p
23a64 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 48  Index = sqlite3H
23a65 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c  ashInsert(pHash,
23a66 20 7a 49 64 78 4e 61 6d 65 2c 20 6c 65 6e 2c 20   zIdxName, len, 
23a67 30 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78  0);.  if( pIndex
23a68 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64   ){.    if( pInd
23a69 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64  ex->pTable->pInd
23a6a 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20  ex==pIndex ){.  
23a6b 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62      pIndex->pTab
23a6c 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e  le->pIndex = pIn
23a6d 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  dex->pNext;.    
23a6e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64  }else{.      Ind
23a6f 65 78 20 2a 70 3b 0a 20 20 20 20 20 20 2f 2a 20  ex *p;.      /* 
23a70 4a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66  Justification of
23a71 20 41 4c 57 41 59 53 28 29 3b 20 20 54 68 65 20   ALWAYS();  The 
23a72 69 6e 64 65 78 20 6d 75 73 74 20 62 65 20 6f 6e  index must be on
23a73 20 74 68 65 20 6c 69 73 74 20 6f 66 0a 20 20 20   the list of.   
23a74 20 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 2a     ** indices. *
23a75 2f 0a 20 20 20 20 20 20 70 20 3d 20 70 49 6e 64  /.      p = pInd
23a76 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64  ex->pTable->pInd
23a77 65 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  ex;.      while(
23a78 20 41 4c 57 41 59 53 28 70 29 20 26 26 20 70 2d   ALWAYS(p) && p-
23a79 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 20 29  >pNext!=pIndex )
23a7a 7b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20  { p = p->pNext; 
23a7b 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41  }.      if( ALWA
23a7c 59 53 28 70 20 26 26 20 70 2d 3e 70 4e 65 78 74  YS(p && p->pNext
23a7d 3d 3d 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20  ==pIndex) ){.   
23a7e 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20       p->pNext = 
23a7f 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20  pIndex->pNext;. 
23a80 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
23a81 20 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65   freeIndex(pInde
23a82 78 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c  x);.  }.  db->fl
23a83 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e  ags |= SQLITE_In
23a84 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a  ternChanges;.}..
23a85 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20  /*.** Erase all 
23a86 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69  schema informati
23a87 6f 6e 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d  on from the in-m
23a88 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65  emory hash table
23a89 73 20 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c 65  s of.** a single
23a8a 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73   database.  This
23a8b 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
23a8c 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65  ed to reclaim me
23a8d 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 74  mory.** before t
23a8e 68 65 20 64 61 74 61 62 61 73 65 20 63 6c 6f 73  he database clos
23a8f 65 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 20  es.  It is also 
23a90 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20  called during a 
23a91 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 74  rollback.** if t
23a92 68 65 72 65 20 77 65 72 65 20 73 63 68 65 6d 61  here were schema
23a93 20 63 68 61 6e 67 65 73 20 64 75 72 69 6e 67 20   changes during 
23a94 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
23a95 6f 72 20 69 66 20 61 0a 2a 2a 20 73 63 68 65 6d  or if a.** schem
23a96 61 2d 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74 63  a-cookie mismatc
23a97 68 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20  h occurs..**.** 
23a98 49 66 20 69 44 62 3d 3d 30 20 74 68 65 6e 20 72  If iDb==0 then r
23a99 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61  eset the interna
23a9a 6c 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73 20  l schema tables 
23a9b 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65  for all database
23a9c 0a 2a 2a 20 66 69 6c 65 73 2e 20 20 49 66 20 69  .** files.  If i
23a9d 44 62 3e 3d 31 20 74 68 65 6e 20 72 65 73 65 74  Db>=1 then reset
23a9e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63   the internal sc
23a9f 68 65 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74 68  hema for only th
23aa0 65 0a 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c 65  e.** single file
23aa1 20 69 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a 53   indicated..*/.S
23aa2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
23aa3 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49  id sqlite3ResetI
23aa4 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71  nternalSchema(sq
23aa5 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
23aa6 44 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  Db){.  int i, j;
23aa7 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
23aa8 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
23aa9 20 29 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d 3d   );..  if( iDb==
23aaa 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
23aab 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62  BtreeEnterAll(db
23aac 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69  );.  }.  for(i=i
23aad 44 62 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  Db; i<db->nDb; i
23aae 2b 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62  ++){.    Db *pDb
23aaf 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a   = &db->aDb[i];.
23ab0 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63      if( pDb->pSc
23ab1 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 61 73  hema ){.      as
23ab2 73 65 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70 44  sert(i==1 || (pD
23ab3 62 2d 3e 70 42 74 20 26 26 20 73 71 6c 69 74 65  b->pBt && sqlite
23ab4 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
23ab5 28 70 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20 20  (pDb->pBt)));.  
23ab6 20 20 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d      sqlite3Schem
23ab7 61 46 72 65 65 28 70 44 62 2d 3e 70 53 63 68 65  aFree(pDb->pSche
23ab8 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ma);.    }.    i
23ab9 66 28 20 69 44 62 3e 30 20 29 20 72 65 74 75 72  f( iDb>0 ) retur
23aba 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
23abb 20 69 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62 2d   iDb==0 );.  db-
23abc 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54  >flags &= ~SQLIT
23abd 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b  E_InternChanges;
23abe 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e  .  sqlite3VtabUn
23abf 6c 6f 63 6b 4c 69 73 74 28 64 62 29 3b 0a 20 20  lockList(db);.  
23ac0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
23ac1 65 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a 20  eAll(db);..  /* 
23ac2 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  If one or more o
23ac3 66 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  f the auxiliary 
23ac4 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 68  database files h
23ac5 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c 0a  as been closed,.
23ac6 20 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76 65    ** then remove
23ac7 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20 61   them from the a
23ac8 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
23ac9 65 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b 65  e list.  We take
23aca 20 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72 74   the.  ** opport
23acb 75 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69 73  unity to do this
23acc 20 68 65 72 65 20 73 69 6e 63 65 20 77 65 20 68   here since we h
23acd 61 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65 64  ave just deleted
23ace 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a 2a   all of the.  **
23acf 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61 62   schema hash tab
23ad0 6c 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f 72  les and therefor
23ad1 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f  e do not have to
23ad2 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65   make any change
23ad3 73 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f 66  s.  ** to any of
23ad4 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a 20   those tables.. 
23ad5 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d 32 3b   */.  for(i=j=2;
23ad6 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
23ad7 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20  {.    struct Db 
23ad8 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b  *pDb = &db->aDb[
23ad9 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d  i];.    if( pDb-
23ada 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  >pBt==0 ){.     
23adb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
23adc 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  b, pDb->zName);.
23add 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65        pDb->zName
23ade 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e 74   = 0;.      cont
23adf 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  inue;.    }.    
23ae0 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20 20  if( j<i ){.     
23ae1 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 62   db->aDb[j] = db
23ae2 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d 0a  ->aDb[i];.    }.
23ae3 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 6d      j++;.  }.  m
23ae4 65 6d 73 65 74 28 26 64 62 2d 3e 61 44 62 5b 6a  emset(&db->aDb[j
23ae5 5d 2c 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d 6a  ], 0, (db->nDb-j
23ae6 29 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  )*sizeof(db->aDb
23ae7 5b 6a 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44 62  [j]));.  db->nDb
23ae8 20 3d 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d 3e   = j;.  if( db->
23ae9 6e 44 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61 44  nDb<=2 && db->aD
23aea 62 21 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63  b!=db->aDbStatic
23aeb 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64   ){.    memcpy(d
23aec 62 2d 3e 61 44 62 53 74 61 74 69 63 2c 20 64 62  b->aDbStatic, db
23aed 2d 3e 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66 28  ->aDb, 2*sizeof(
23aee 64 62 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20 20  db->aDb[0]));.  
23aef 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23af0 64 62 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20 20  db, db->aDb);.  
23af1 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e    db->aDb = db->
23af2 61 44 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a 7d  aDbStatic;.  }.}
23af3 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
23af4 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77  tine is called w
23af5 68 65 6e 20 61 20 63 6f 6d 6d 69 74 20 6f 63 63  hen a commit occ
23af6 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  urs..*/.SQLITE_P
23af7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23af8 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61  te3CommitInterna
23af9 6c 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33  lChanges(sqlite3
23afa 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 66 6c 61   *db){.  db->fla
23afb 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e  gs &= ~SQLITE_In
23afc 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a  ternChanges;.}..
23afd 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20  /*.** Clear the 
23afe 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f  column names fro
23aff 6d 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65  m a table or vie
23b00 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  w..*/.static voi
23b01 64 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c  d sqliteResetCol
23b02 75 6d 6e 4e 61 6d 65 73 28 54 61 62 6c 65 20 2a  umnNames(Table *
23b03 70 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69  pTable){.  int i
23b04 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c  ;.  Column *pCol
23b05 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
23b06 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b  = pTable->dbMem;
23b07 0a 20 20 74 65 73 74 63 61 73 65 28 20 64 62 3d  .  testcase( db=
23b08 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
23b09 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20 69  pTable!=0 );.  i
23b0a 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62 6c  f( (pCol = pTabl
23b0b 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a 20  e->aCol)!=0 ){. 
23b0c 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54     for(i=0; i<pT
23b0d 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c  able->nCol; i++,
23b0e 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   pCol++){.      
23b0f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
23b10 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pCol->zName);.
23b11 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
23b12 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c  rDelete(db, pCol
23b13 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20  ->pDflt);.      
23b14 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
23b15 2c 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29 3b 0a  , pCol->zDflt);.
23b16 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
23b17 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54  ree(db, pCol->zT
23b18 79 70 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ype);.      sqli
23b19 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
23b1a 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  ol->zColl);.    
23b1b 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
23b1c 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ree(db, pTable->
23b1d 61 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54 61  aCol);.  }.  pTa
23b1e 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20  ble->aCol = 0;. 
23b1f 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
23b20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  0;.}../*.** Remo
23b21 76 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61  ve the memory da
23b22 74 61 20 73 74 72 75 63 74 75 72 65 73 20 61 73  ta structures as
23b23 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
23b24 65 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65  e given.** Table
23b25 2e 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72  .  No changes ar
23b26 65 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62  e made to disk b
23b27 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  y this routine..
23b28 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
23b29 6e 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20  ne just deletes 
23b2a 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75  the data structu
23b2b 72 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74  re.  It does not
23b2c 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74   unlink.** the t
23b2d 61 62 6c 65 20 64 61 74 61 20 73 74 72 75 63 74  able data struct
23b2e 75 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73  ure from the has
23b2f 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69 74  h table.  But it
23b30 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a 2a   does destroy.**
23b31 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72   memory structur
23b32 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63 65  es of the indice
23b33 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65  s and foreign ke
23b34 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ys associated wi
23b35 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  th .** the table
23b36 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23b37 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23b38 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c  DeleteTable(Tabl
23b39 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49 6e  e *pTable){.  In
23b3a 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70 4e  dex *pIndex, *pN
23b3b 65 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ext;.  sqlite3 *
23b3c 64 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 6c  db;..  if( pTabl
23b3d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  e==0 ) return;. 
23b3e 20 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64 62   db = pTable->db
23b3f 4d 65 6d 3b 0a 20 20 74 65 73 74 63 61 73 65 28  Mem;.  testcase(
23b40 20 64 62 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20   db==0 );..  /* 
23b41 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68  Do not delete th
23b42 65 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 74 68  e table until th
23b43 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
23b44 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e 20  t reaches zero. 
23b45 2a 2f 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52 65  */.  pTable->nRe
23b46 66 2d 2d 3b 0a 20 20 69 66 28 20 70 54 61 62 6c  f--;.  if( pTabl
23b47 65 2d 3e 6e 52 65 66 3e 30 20 29 7b 0a 20 20 20  e->nRef>0 ){.   
23b48 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
23b49 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e  ssert( pTable->n
23b4a 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  Ref==0 );..  /* 
23b4b 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69 63  Delete all indic
23b4c 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
23b4d 74 68 20 74 68 69 73 20 74 61 62 6c 65 0a 20 20  th this table.  
23b4e 2a 2f 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 20  */.  for(pIndex 
23b4f 3d 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  = pTable->pIndex
23b50 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78  ; pIndex; pIndex
23b51 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65  =pNext){.    pNe
23b52 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65  xt = pIndex->pNe
23b53 78 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  xt;.    assert( 
23b54 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 3d  pIndex->pSchema=
23b55 3d 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61  =pTable->pSchema
23b56 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   );.    sqlite3D
23b57 65 6c 65 74 65 49 6e 64 65 78 28 70 49 6e 64 65  eleteIndex(pInde
23b58 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65  x);.  }..  /* De
23b59 6c 65 74 65 20 61 6e 79 20 66 6f 72 65 69 67 6e  lete any foreign
23b5a 20 6b 65 79 73 20 61 74 74 61 63 68 65 64 20 74   keys attached t
23b5b 6f 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f  o this table. */
23b5c 0a 20 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65  .  sqlite3FkDele
23b5d 74 65 28 70 54 61 62 6c 65 29 3b 0a 0a 20 20 2f  te(pTable);..  /
23b5e 2a 20 44 65 6c 65 74 65 20 74 68 65 20 54 61 62  * Delete the Tab
23b5f 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 74 73  le structure its
23b60 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  elf..  */.  sqli
23b61 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d  teResetColumnNam
23b62 65 73 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71  es(pTable);.  sq
23b63 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
23b64 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a  pTable->zName);.
23b65 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23b66 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c  db, pTable->zCol
23b67 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  Aff);.  sqlite3S
23b68 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
23b69 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29  pTable->pSelect)
23b6a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
23b6b 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 73 71  _OMIT_CHECK.  sq
23b6c 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
23b6d 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68 65  db, pTable->pChe
23b6e 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  ck);.#endif.  sq
23b6f 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 70  lite3VtabClear(p
23b70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65  Table);.  sqlite
23b71 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62  3DbFree(db, pTab
23b72 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  le);.}../*.** Un
23b73 6c 69 6e 6b 20 74 68 65 20 67 69 76 65 6e 20 74  link the given t
23b74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 68 61  able from the ha
23b75 73 68 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68  sh tables and th
23b76 65 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20  e delete the.** 
23b77 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  table structure 
23b78 77 69 74 68 20 61 6c 6c 20 69 74 73 20 69 6e 64  with all its ind
23b79 69 63 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e  ices and foreign
23b7a 20 6b 65 79 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45   keys..*/.SQLITE
23b7b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23b7c 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
23b7d 6c 65 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65  leteTable(sqlite
23b7e 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20  3 *db, int iDb, 
23b7f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
23b80 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a  Name){.  Table *
23b81 70 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a 20  p;.  Db *pDb;.. 
23b82 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29   assert( db!=0 )
23b83 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
23b84 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
23b85 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  b );.  assert( z
23b86 54 61 62 4e 61 6d 65 20 26 26 20 7a 54 61 62 4e  TabName && zTabN
23b87 61 6d 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62 20  ame[0] );.  pDb 
23b88 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
23b89 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61  .  p = sqlite3Ha
23b8a 73 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e 70  shInsert(&pDb->p
23b8b 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c  Schema->tblHash,
23b8c 20 7a 54 61 62 4e 61 6d 65 2c 0a 20 20 20 20 20   zTabName,.     
23b8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23b8e 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e     sqlite3Strlen
23b8f 33 30 28 7a 54 61 62 4e 61 6d 65 29 2c 30 29 3b  30(zTabName),0);
23b90 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
23b91 54 61 62 6c 65 28 70 29 3b 0a 20 20 64 62 2d 3e  Table(p);.  db->
23b92 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
23b93 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d  InternChanges;.}
23b94 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20  ../*.** Given a 
23b95 74 6f 6b 65 6e 2c 20 72 65 74 75 72 6e 20 61 20  token, return a 
23b96 73 74 72 69 6e 67 20 74 68 61 74 20 63 6f 6e 73  string that cons
23b97 69 73 74 73 20 6f 66 20 74 68 65 20 74 65 78 74  ists of the text
23b98 20 6f 66 20 74 68 61 74 0a 2a 2a 20 74 6f 6b 65   of that.** toke
23b99 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  n.  Space to hol
23b9a 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  d the returned s
23b9b 74 72 69 6e 67 0a 2a 2a 20 69 73 20 6f 62 74 61  tring.** is obta
23b9c 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
23b9d 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73  Malloc() and mus
23b9e 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68  t be freed by th
23b9f 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e  e calling.** fun
23ba0 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79  ction..**.** Any
23ba1 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61 72 6b 73   quotation marks
23ba2 20 28 65 78 3a 20 20 22 6e 61 6d 65 22 2c 20 27   (ex:  "name", '
23ba3 6e 61 6d 65 27 2c 20 5b 6e 61 6d 65 5d 2c 20 6f  name', [name], o
23ba4 72 20 60 6e 61 6d 65 60 29 20 74 68 61 74 0a 2a  r `name`) that.*
23ba5 2a 20 73 75 72 72 6f 75 6e 64 20 74 68 65 20 62  * surround the b
23ba6 6f 64 79 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  ody of the token
23ba7 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a   are removed..**
23ba8 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f  .** Tokens are o
23ba9 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65  ften just pointe
23baa 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67  rs into the orig
23bab 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61 6e  inal SQL text an
23bac 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 20  d so.** are not 
23bad 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20  \000 terminated 
23bae 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72 73  and are not pers
23baf 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65 74  istent.  The ret
23bb0 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20  urned string.** 
23bb1 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74  is \000 terminat
23bb2 65 64 20 61 6e 64 20 69 73 20 70 65 72 73 69 73  ed and is persis
23bb3 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tent..*/.SQLITE_
23bb4 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
23bb5 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
23bb6 65 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  en(sqlite3 *db, 
23bb7 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20  Token *pName){. 
23bb8 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
23bb9 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20  if( pName ){.   
23bba 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
23bbb 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
23bbc 68 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70  har*)pName->z, p
23bbd 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71  Name->n);.    sq
23bbe 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61  lite3Dequote(zNa
23bbf 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  me);.  }else{.  
23bc0 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d    zName = 0;.  }
23bc1 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b  .  return zName;
23bc2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74  .}../*.** Open t
23bc3 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
23bc4 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69 6e   table stored in
23bc5 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72   database number
23bc6 20 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74   iDb for.** writ
23bc7 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20 69  ing. The table i
23bc8 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 63  s opened using c
23bc9 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49  ursor 0..*/.SQLI
23bca 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
23bcb 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65  sqlite3OpenMaste
23bcc 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 2c  rTable(Parse *p,
23bcd 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62   int iDb){.  Vdb
23bce 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
23bcf 74 56 64 62 65 28 70 29 3b 0a 20 20 73 71 6c 69  tVdbe(p);.  sqli
23bd0 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  te3TableLock(p, 
23bd1 69 44 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  iDb, MASTER_ROOT
23bd2 2c 20 31 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  , 1, SCHEMA_TABL
23bd3 45 28 69 44 62 29 29 3b 0a 20 20 73 71 6c 69 74  E(iDb));.  sqlit
23bd4 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
23bd5 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c  OP_OpenWrite, 0,
23bd6 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44   MASTER_ROOT, iD
23bd7 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
23bd8 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
23bd9 20 28 63 68 61 72 20 2a 29 35 2c 20 50 34 5f 49   (char *)5, P4_I
23bda 4e 54 33 32 29 3b 20 20 2f 2a 20 35 20 63 6f 6c  NT32);  /* 5 col
23bdb 75 6d 6e 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  umn table */.  i
23bdc 66 28 20 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b  f( p->nTab==0 ){
23bdd 0a 20 20 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31  .    p->nTab = 1
23bde 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ;.  }.}../*.** P
23bdf 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70  arameter zName p
23be0 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74  oints to a nul-t
23be1 65 72 6d 69 6e 61 74 65 64 20 62 75 66 66 65 72  erminated buffer
23be2 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
23be3 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74  name.** of a dat
23be4 61 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22  abase ("main", "
23be5 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d  temp" or the nam
23be6 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64  e of an attached
23be7 20 64 62 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75   db). This.** fu
23be8 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
23be9 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
23bea 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 69  named database i
23beb 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a  n db->aDb[], or.
23bec 2a 2a 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d  ** -1 if the nam
23bed 65 64 20 64 62 20 63 61 6e 6e 6f 74 20 62 65 20  ed db cannot be 
23bee 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  found..*/.SQLITE
23bef 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
23bf0 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73  ite3FindDbName(s
23bf1 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
23bf2 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a  t char *zName){.
23bf3 20 20 69 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20    int i = -1;   
23bf4 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
23bf5 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 66  e number */.  if
23bf6 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 44  ( zName ){.    D
23bf7 62 20 2a 70 44 62 3b 0a 20 20 20 20 69 6e 74 20  b *pDb;.    int 
23bf8 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
23bf9 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  n30(zName);.    
23bfa 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31  for(i=(db->nDb-1
23bfb 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b  ), pDb=&db->aDb[
23bfc 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70  i]; i>=0; i--, p
23bfd 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 28  Db--){.      if(
23bfe 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c   (!OMIT_TEMPDB |
23bff 7c 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73  | i!=1 ) && n==s
23c00 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
23c01 44 62 2d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20  Db->zName) && . 
23c02 20 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c 69           0==sqli
23c03 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e  te3StrICmp(pDb->
23c04 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b  zName, zName) ){
23c05 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23c06 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
23c07 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a  }.  return i;.}.
23c08 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e  ./*.** The token
23c09 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73   *pName contains
23c0a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64   the name of a d
23c0b 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20  atabase (either 
23c0c 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65  "main" or.** "te
23c0d 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20  mp" or the name 
23c0e 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  of an attached d
23c0f 62 29 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65  b). This routine
23c10 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
23c11 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d  index of the nam
23c12 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64  ed database in d
23c13 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20  b->aDb[], or -1 
23c14 69 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20  if the named db 
23c15 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  .** does not exi
23c16 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  st..*/.SQLITE_PR
23c17 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
23c18 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 20  3FindDb(sqlite3 
23c19 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  *db, Token *pNam
23c1a 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  e){.  int i;    
23c1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c1c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
23c1d 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f  tabase number */
23c1e 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
23c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
23c21 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67  we are searching
23c22 20 66 6f 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20   for */.  zName 
23c23 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
23c24 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
23c25 29 3b 0a 20 20 69 20 3d 20 73 71 6c 69 74 65 33  );.  i = sqlite3
23c26 46 69 6e 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a  FindDbName(db, z
23c27 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
23c28 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65  DbFree(db, zName
23c29 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d  );.  return i;.}
23c2a 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f  ../* The table o
23c2b 72 20 76 69 65 77 20 6f 72 20 74 72 69 67 67 65  r view or trigge
23c2c 72 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65 64  r name is passed
23c2d 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
23c2e 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70   via tokens.** p
23c2f 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32  Name1 and pName2
23c30 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e  . If the table n
23c31 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75  ame was fully qu
23c32 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61  alified, for exa
23c33 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41  mple:.**.** CREA
23c34 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79 79  TE TABLE xxx.yyy
23c35 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54   (...);.** .** T
23c36 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65  hen pName1 is se
23c37 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20 70  t to "xxx" and p
23c38 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e 20  Name2 "yyy". On 
23c39 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20 69  the other hand i
23c3a 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e  f.** the table n
23c3b 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79  ame is not fully
23c3c 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e   qualified, i.e.
23c3d 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54  :.**.** CREATE T
23c3e 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a  ABLE yyy(...);.*
23c3f 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31  *.** Then pName1
23c40 20 69 73 20 73 65 74 20 74 6f 20 22 79 79 79 22   is set to "yyy"
23c41 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22   and pName2 is "
23c42 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  "..**.** This ro
23c43 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 2a  utine sets the *
23c44 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72  ppUnqual pointer
23c45 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
23c46 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f   token (pName1 o
23c47 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61  r.** pName2) tha
23c48 74 20 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71  t stores the unq
23c49 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e  ualified table n
23c4a 61 6d 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20  ame.  The index 
23c4b 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  of the.** databa
23c4c 73 65 20 22 78 78 78 22 20 69 73 20 72 65 74 75  se "xxx" is retu
23c4d 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
23c4e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
23c4f 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a  te3TwoPartName(.
23c50 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
23c51 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
23c52 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61   and code genera
23c53 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ting context */.
23c54 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c    Token *pName1,
23c55 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 78 78        /* The "xx
23c56 78 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 22  x" in the name "
23c57 78 78 78 2e 79 79 79 22 20 6f 72 20 22 78 78 78  xxx.yyy" or "xxx
23c58 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  " */.  Token *pN
23c59 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20 54 68  ame2,      /* Th
23c5a 65 20 22 79 79 79 22 20 69 6e 20 74 68 65 20 6e  e "yyy" in the n
23c5b 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 2a 2f  ame "xxx.yyy" */
23c5c 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75  .  Token **pUnqu
23c5d 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  al     /* Write 
23c5e 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  the unqualified 
23c5f 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68 65 72 65  object name here
23c60 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62   */.){.  int iDb
23c61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
23c62 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
23c63 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a   holding the obj
23c64 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ect */.  sqlite3
23c65 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
23c66 62 3b 0a 0a 20 20 69 66 28 20 41 4c 57 41 59 53  b;..  if( ALWAYS
23c67 28 70 4e 61 6d 65 32 21 3d 30 29 20 26 26 20 70  (pName2!=0) && p
23c68 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20  Name2->n>0 ){.  
23c69 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
23c6a 75 73 79 20 29 20 7b 0a 20 20 20 20 20 20 73 71  usy ) {.      sq
23c6b 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
23c6c 61 72 73 65 2c 20 22 63 6f 72 72 75 70 74 20 64  arse, "corrupt d
23c6d 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20 20 20  atabase");.     
23c6e 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
23c6f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31  .      return -1
23c70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 55 6e  ;.    }.    *pUn
23c71 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20  qual = pName2;. 
23c72 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
23c73 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65  FindDb(db, pName
23c74 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c  1);.    if( iDb<
23c75 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
23c76 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
23c77 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61  e, "unknown data
23c78 62 61 73 65 20 25 54 22 2c 20 70 4e 61 6d 65 31  base %T", pName1
23c79 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
23c7a 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72  >nErr++;.      r
23c7b 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a  eturn -1;.    }.
23c7c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
23c7d 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44  ert( db->init.iD
23c7e 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74  b==0 || db->init
23c7f 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 69 44 62  .busy );.    iDb
23c80 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b   = db->init.iDb;
23c81 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20  .    *pUnqual = 
23c82 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65  pName1;.  }.  re
23c83 74 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a  turn iDb;.}../*.
23c84 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
23c85 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b  is used to check
23c86 20 69 66 20 74 68 65 20 55 54 46 2d 38 20 73 74   if the UTF-8 st
23c87 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20 61 20  ring zName is a 
23c88 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69  legal.** unquali
23c89 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20 61 20  fied name for a 
23c8a 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a 65 63  new schema objec
23c8b 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c  t (table, index,
23c8c 20 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67   view or.** trig
23c8d 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20  ger). All names 
23c8e 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65 70 74  are legal except
23c8f 20 74 68 6f 73 65 20 74 68 61 74 20 62 65 67 69   those that begi
23c90 6e 20 77 69 74 68 20 74 68 65 20 73 74 72 69 6e  n with the strin
23c91 67 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22 20 28  g.** "sqlite_" (
23c92 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65 72 20  in upper, lower 
23c93 6f 72 20 6d 69 78 65 64 20 63 61 73 65 29 2e 20  or mixed case). 
23c94 54 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20  This portion of 
23c95 74 68 65 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a  the namespace.**
23c96 20 69 73 20 72 65 73 65 72 76 65 64 20 66 6f 72   is reserved for
23c97 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a   internal use..*
23c98 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23c99 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63   int sqlite3Chec
23c9a 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73  kObjectName(Pars
23c9b 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
23c9c 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20   char *zName){. 
23c9d 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64 62   if( !pParse->db
23c9e 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70  ->init.busy && p
23c9f 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
23ca0 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28   .          && (
23ca1 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67  pParse->db->flag
23ca2 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65  s & SQLITE_Write
23ca3 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20  Schema)==0.     
23ca4 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74       && 0==sqlit
23ca5 65 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65  e3StrNICmp(zName
23ca6 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 20  , "sqlite_", 7) 
23ca7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
23ca8 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23ca9 6f 62 6a 65 63 74 20 6e 61 6d 65 20 72 65 73 65  object name rese
23caa 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61  rved for interna
23cab 6c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d  l use: %s", zNam
23cac 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  e);.    return S
23cad 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
23cae 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
23caf 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65  _OK;.}../*.** Be
23cb0 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67  gin constructing
23cb1 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72 65 70   a new table rep
23cb2 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d  resentation in m
23cb3 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 0a  emory.  This is.
23cb4 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f 66 20  ** the first of 
23cb5 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e 20 72  several action r
23cb6 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67 65 74  outines that get
23cb7 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f   called in respo
23cb8 6e 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41  nse.** to a CREA
23cb9 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
23cba 6e 74 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c  nt.  In particul
23cbb 61 72 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ar, this routine
23cbc 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66   is called.** af
23cbd 74 65 72 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e  ter seeing token
23cbe 73 20 22 43 52 45 41 54 45 22 20 61 6e 64 20 22  s "CREATE" and "
23cbf 54 41 42 4c 45 22 20 61 6e 64 20 74 68 65 20 74  TABLE" and the t
23cc0 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 69  able name. The i
23cc1 73 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73  sTemp.** flag is
23cc2 20 74 72 75 65 20 69 66 20 74 68 65 20 74 61 62   true if the tab
23cc3 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f  le should be sto
23cc4 72 65 64 20 69 6e 20 74 68 65 20 61 75 78 69 6c  red in the auxil
23cc5 69 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a  iary database.**
23cc6 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66   file instead of
23cc7 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
23cc8 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69  abase file.  Thi
23cc9 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68  s is normally th
23cca 65 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74  e case.** when t
23ccb 68 65 20 22 54 45 4d 50 22 20 6f 72 20 22 54 45  he "TEMP" or "TE
23ccc 4d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f 72 64  MPORARY" keyword
23ccd 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65   occurs in betwe
23cce 65 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61 6e 64  en.** CREATE and
23ccf 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68   TABLE..**.** Th
23cd0 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f  e new table reco
23cd1 72 64 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  rd is initialize
23cd2 64 20 61 6e 64 20 70 75 74 20 69 6e 20 70 50 61  d and put in pPa
23cd3 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a  rse->pNewTable..
23cd4 2a 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20 74 68  ** As more of th
23cd5 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  e CREATE TABLE s
23cd6 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73  tatement is pars
23cd7 65 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 61  ed, additional a
23cd8 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65  ction.** routine
23cd9 73 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64  s will be called
23cda 20 74 6f 20 61 64 64 20 6d 6f 72 65 20 69 6e 66   to add more inf
23cdb 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 69 73  ormation to this
23cdc 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74   record..** At t
23cdd 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52  he end of the CR
23cde 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
23cdf 6d 65 6e 74 2c 20 74 68 65 20 73 71 6c 69 74 65  ment, the sqlite
23ce0 33 45 6e 64 54 61 62 6c 65 28 29 20 72 6f 75 74  3EndTable() rout
23ce1 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ine.** is called
23ce2 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
23ce3 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66   construction of
23ce4 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72   the new table r
23ce5 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ecord..*/.SQLITE
23ce6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23ce7 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
23ce8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23ce9 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f  ,   /* Parser co
23cea 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ntext */.  Token
23ceb 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46   *pName1,   /* F
23cec 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65  irst part of the
23ced 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
23cee 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20  le or view */.  
23cef 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20  Token *pName2,  
23cf0 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20   /* Second part 
23cf1 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  of the name of t
23cf2 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  he table or view
23cf3 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70   */.  int isTemp
23cf4 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ,      /* True i
23cf5 66 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50  f this is a TEMP
23cf6 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
23cf7 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f 2a 20  isView,      /* 
23cf8 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
23cf9 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20  a VIEW */.  int 
23cfa 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20  isVirtual,   /* 
23cfb 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
23cfc 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c 65 20  a VIRTUAL table 
23cfd 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20  */.  int noErr  
23cfe 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68        /* Do noth
23cff 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61 6c 72  ing if table alr
23d00 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29  eady exists */.)
23d01 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c  {.  Table *pTabl
23d02 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  e;.  char *zName
23d03 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d   = 0; /* The nam
23d04 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  e of the new tab
23d05 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  le */.  sqlite3 
23d06 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
23d07 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  ;.  Vdbe *v;.  i
23d08 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
23d09 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62  /* Database numb
23d0a 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  er to create the
23d0b 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54   table in */.  T
23d0c 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20  oken *pName;    
23d0d 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e  /* Unqualified n
23d0e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
23d0f 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20   to create */.. 
23d10 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72   /* The table or
23d11 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20 63 72   view name to cr
23d12 65 61 74 65 20 69 73 20 70 61 73 73 65 64 20 74  eate is passed t
23d13 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76  o this routine v
23d14 69 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70  ia tokens.  ** p
23d15 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32  Name1 and pName2
23d16 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e  . If the table n
23d17 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75  ame was fully qu
23d18 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61  alified, for exa
23d19 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  mple:.  **.  ** 
23d1a 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78  CREATE TABLE xxx
23d1b 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a  .yyy (...);.  **
23d1c 20 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d   .  ** Then pNam
23d1d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 78 78  e1 is set to "xx
23d1e 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79  x" and pName2 "y
23d1f 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65  yy". On the othe
23d20 72 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a 20 74  r hand if.  ** t
23d21 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73  he table name is
23d22 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69   not fully quali
23d23 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a  fied, i.e.:.  **
23d24 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42  .  ** CREATE TAB
23d25 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a  LE yyy(...);.  *
23d26 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d  *.  ** Then pNam
23d27 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79 79  e1 is set to "yy
23d28 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73  y" and pName2 is
23d29 20 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   ""..  **.  ** T
23d2a 68 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20 73 65  he call below se
23d2b 74 73 20 74 68 65 20 70 4e 61 6d 65 20 70 6f 69  ts the pName poi
23d2c 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74  nter to point at
23d2d 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d   the token (pNam
23d2e 65 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d 65  e1 or.  ** pName
23d2f 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 74  2) that stores t
23d30 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74  he unqualified t
23d31 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 76  able name. The v
23d32 61 72 69 61 62 6c 65 20 69 44 62 20 69 73 0a 20  ariable iDb is. 
23d33 20 2a 2a 20 73 65 74 20 74 6f 20 74 68 65 20 69   ** set to the i
23d34 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
23d35 62 61 73 65 20 74 68 61 74 20 74 68 65 20 74 61  base that the ta
23d36 62 6c 65 20 6f 72 20 76 69 65 77 20 69 73 20 74  ble or view is t
23d37 6f 20 62 65 0a 20 20 2a 2a 20 63 72 65 61 74 65  o be.  ** create
23d38 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44 62  d in..  */.  iDb
23d39 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72   = sqlite3TwoPar
23d3a 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
23d3b 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70  ame1, pName2, &p
23d3c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62  Name);.  if( iDb
23d3d 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  <0 ) return;.  i
23d3e 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
23d3f 26 26 20 69 73 54 65 6d 70 20 26 26 20 69 44 62  && isTemp && iDb
23d40 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  >1 ){.    /* If 
23d41 63 72 65 61 74 69 6e 67 20 61 20 74 65 6d 70 20  creating a temp 
23d42 74 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20  table, the name 
23d43 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c 69  may not be quali
23d44 66 69 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69  fied */.    sqli
23d45 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
23d46 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74  se, "temporary t
23d47 61 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62  able name must b
23d48 65 20 75 6e 71 75 61 6c 69 66 69 65 64 22 29 3b  e unqualified");
23d49 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
23d4a 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d  .  if( !OMIT_TEM
23d4b 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 20  PDB && isTemp ) 
23d4c 69 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72  iDb = 1;..  pPar
23d4d 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d  se->sNameToken =
23d4e 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65   *pName;.  zName
23d4f 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
23d50 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d  omToken(db, pNam
23d51 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d  e);.  if( zName=
23d52 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
23d53 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
23d54 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74  lite3CheckObject
23d55 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61  Name(pParse, zNa
23d56 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  me) ){.    goto 
23d57 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
23d58 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  r;.  }.  if( db-
23d59 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29 20 69  >init.iDb==1 ) i
23d5a 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64  sTemp = 1;.#ifnd
23d5b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
23d5c 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61  UTHORIZATION.  a
23d5d 73 73 65 72 74 28 20 28 69 73 54 65 6d 70 20 26  ssert( (isTemp &
23d5e 20 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20   1)==isTemp );. 
23d5f 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b   {.    int code;
23d60 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d  .    char *zDb =
23d61 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
23d62 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ame;.    if( sql
23d63 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
23d64 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53  arse, SQLITE_INS
23d65 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ERT, SCHEMA_TABL
23d66 45 28 69 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44  E(isTemp), 0, zD
23d67 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
23d68 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
23d69 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  or;.    }.    if
23d6a 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
23d6b 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
23d6c 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a  DB && isTemp ){.
23d6d 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
23d6e 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
23d6f 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65  P_VIEW;.      }e
23d70 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  lse{.        cod
23d71 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  e = SQLITE_CREAT
23d72 45 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a  E_VIEW;.      }.
23d73 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23d74 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
23d75 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20  B && isTemp ){. 
23d76 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
23d77 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
23d78 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65  _TABLE;.      }e
23d79 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  lse{.        cod
23d7a 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  e = SQLITE_CREAT
23d7b 45 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d  E_TABLE;.      }
23d7c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
23d7d 69 73 56 69 72 74 75 61 6c 20 26 26 20 73 71 6c  isVirtual && sql
23d7e 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
23d7f 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d  arse, code, zNam
23d80 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20  e, 0, zDb) ){.  
23d81 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74      goto begin_t
23d82 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20  able_error;.    
23d83 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
23d84 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
23d85 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20   new table name 
23d86 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65  does not collide
23d87 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e   with an existin
23d88 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20  g.  ** index or 
23d89 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68  table name in th
23d8a 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e  e same database.
23d8b 20 20 49 73 73 75 65 20 61 6e 20 65 72 72 6f 72    Issue an error
23d8c 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20 2a 2a   message if.  **
23d8d 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20 65 78   it does. The ex
23d8e 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20 74 68  ception is if th
23d8f 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e  e statement bein
23d90 67 20 70 61 72 73 65 64 20 77 61 73 20 70 61 73  g parsed was pas
23d91 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73  sed.  ** to an s
23d92 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
23d93 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74  tab() call. In t
23d94 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20 74 68  hat case only th
23d95 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20  e column names. 
23d96 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20 77 69   ** and types wi
23d97 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f 20 74  ll be used, so t
23d98 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
23d99 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61 6d 65  to test for name
23d9a 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69  space.  ** colli
23d9b 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  sions..  */.  if
23d9c 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  ( !IN_DECLARE_VT
23d9d 41 42 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51  AB ){.    if( SQ
23d9e 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
23d9f 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
23da0 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  e) ){.      goto
23da1 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
23da2 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54  or;.    }.    pT
23da3 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 46 69  able = sqlite3Fi
23da4 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d  ndTable(db, zNam
23da5 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e, db->aDb[iDb].
23da6 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  zName);.    if( 
23da7 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  pTable ){.      
23da8 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20  if( !noErr ){.  
23da9 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
23daa 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
23dab 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64 79 20  able %T already 
23dac 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b  exists", pName);
23dad 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67  .      }.      g
23dae 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f  oto begin_table_
23daf 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20  error;.    }.   
23db0 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
23db1 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
23db2 20 30 29 21 3d 30 20 26 26 20 28 69 44 62 3d 3d   0)!=0 && (iDb==
23db3 30 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62  0 || !db->init.b
23db4 75 73 79 29 20 29 7b 0a 20 20 20 20 20 20 73 71  usy) ){.      sq
23db5 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
23db6 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 73 20  arse, "there is 
23db7 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 65 78  already an index
23db8 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d   named %s", zNam
23db9 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  e);.      goto b
23dba 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72  egin_table_error
23dbb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  ;.    }.  }..  p
23dbc 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44  Table = sqlite3D
23dbd 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
23dbe 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a  sizeof(Table));.
23dbf 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20    if( pTable==0 
23dc0 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
23dc1 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
23dc2 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
23dc3 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
23dc4 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a  pParse->nErr++;.
23dc5 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74      goto begin_t
23dc6 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  able_error;.  }.
23dc7 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20    pTable->zName 
23dc8 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c  = zName;.  pTabl
23dc9 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20  e->iPKey = -1;. 
23dca 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61   pTable->pSchema
23dcb 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
23dcc 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c  pSchema;.  pTabl
23dcd 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70  e->nRef = 1;.  p
23dce 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 20 3d 20 30  Table->dbMem = 0
23dcf 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
23dd0 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30  se->pNewTable==0
23dd1 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e   );.  pParse->pN
23dd2 65 77 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65  ewTable = pTable
23dd3 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
23dd4 69 73 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c  is the magic sql
23dd5 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
23dd6 6c 65 20 75 73 65 64 20 62 79 20 61 75 74 6f 69  le used by autoi
23dd7 6e 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74  ncrement,.  ** t
23dd8 68 65 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69  hen record a poi
23dd9 6e 74 65 72 20 74 6f 20 74 68 69 73 20 74 61 62  nter to this tab
23dda 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  le in the main d
23ddb 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
23ddc 65 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49  e.  ** so that I
23ddd 4e 53 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74  NSERT can find t
23dde 68 65 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e  he table easily.
23ddf 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
23de0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
23de1 43 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70  CREMENT.  if( !p
23de2 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26  Parse->nested &&
23de3 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22   strcmp(zName, "
23de4 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22  sqlite_sequence"
23de5 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 61 62  )==0 ){.    pTab
23de6 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65  le->pSchema->pSe
23de7 71 54 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20  qTab = pTable;. 
23de8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
23de9 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
23dea 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 77   the code that w
23deb 69 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 20 74  ill insert the t
23dec 61 62 6c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f  able record into
23ded 0a 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45  .  ** the SQLITE
23dee 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20  _MASTER table.  
23def 4e 6f 74 65 20 69 6e 20 70 61 72 74 69 63 75 6c  Note in particul
23df0 61 72 20 74 68 61 74 20 77 65 20 6d 75 73 74 20  ar that we must 
23df1 67 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e  go ahead.  ** an
23df2 64 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 72  d allocate the r
23df3 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72  ecord number for
23df4 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79   the table entry
23df5 20 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 61 6e   now.  Before an
23df6 79 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b  y.  ** PRIMARY K
23df7 45 59 20 6f 72 20 55 4e 49 51 55 45 20 6b 65 79  EY or UNIQUE key
23df8 77 6f 72 64 73 20 61 72 65 20 70 61 72 73 65 64  words are parsed
23df9 2e 20 20 54 68 6f 73 65 20 6b 65 79 77 6f 72 64  .  Those keyword
23dfa 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a  s will cause.  *
23dfb 2a 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20  * indices to be 
23dfc 63 72 65 61 74 65 64 20 61 6e 64 20 74 68 65 20  created and the 
23dfd 74 61 62 6c 65 20 72 65 63 6f 72 64 20 6d 75 73  table record mus
23dfe 74 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 74 68  t come before th
23dff 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e  e .  ** indices.
23e00 20 20 48 65 6e 63 65 2c 20 74 68 65 20 72 65 63    Hence, the rec
23e01 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ord number for t
23e02 68 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65  he table must be
23e03 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20   allocated.  ** 
23e04 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  now..  */.  if( 
23e05 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26  !db->init.busy &
23e06 26 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65  & (v = sqlite3Ge
23e07 74 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d  tVdbe(pParse))!=
23e08 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b  0 ){.    int j1;
23e09 0a 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f 72  .    int fileFor
23e0a 6d 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 65 67  mat;.    int reg
23e0b 31 2c 20 72 65 67 32 2c 20 72 65 67 33 3b 0a 20  1, reg2, reg3;. 
23e0c 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
23e0d 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
23e0e 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a  arse, 0, iDb);..
23e0f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
23e10 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
23e11 0a 20 20 20 20 69 66 28 20 69 73 56 69 72 74 75  .    if( isVirtu
23e12 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  al ){.      sqli
23e13 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
23e14 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20   OP_VBegin);.   
23e15 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
23e16 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 66 6f  * If the file fo
23e17 72 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e  rmat and encodin
23e18 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  g in the databas
23e19 65 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20  e have not been 
23e1a 73 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74  set, .    ** set
23e1b 20 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a   them now..    *
23e1c 2f 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50 61  /.    reg1 = pPa
23e1d 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20  rse->regRowid = 
23e1e 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
23e1f 20 20 20 20 72 65 67 32 20 3d 20 70 50 61 72 73      reg2 = pPars
23e20 65 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70  e->regRoot = ++p
23e21 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
23e22 20 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73 65   reg3 = ++pParse
23e23 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
23e24 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
23e25 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20   OP_ReadCookie, 
23e26 69 44 62 2c 20 72 65 67 33 2c 20 42 54 52 45 45  iDb, reg3, BTREE
23e27 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 29 3b 0a 20  _FILE_FORMAT);. 
23e28 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
23e29 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
23e2a 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65  .    j1 = sqlite
23e2b 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
23e2c 50 5f 49 66 2c 20 72 65 67 33 29 3b 0a 20 20 20  P_If, reg3);.   
23e2d 20 66 69 6c 65 46 6f 72 6d 61 74 20 3d 20 28 64   fileFormat = (d
23e2e 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
23e2f 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 29  E_LegacyFileFmt)
23e30 21 3d 30 20 3f 0a 20 20 20 20 20 20 20 20 20 20  !=0 ?.          
23e31 20 20 20 20 20 20 20 20 31 20 3a 20 53 51 4c 49          1 : SQLI
23e32 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d  TE_MAX_FILE_FORM
23e33 41 54 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  AT;.    sqlite3V
23e34 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
23e35 49 6e 74 65 67 65 72 2c 20 66 69 6c 65 46 6f 72  Integer, fileFor
23e36 6d 61 74 2c 20 72 65 67 33 29 3b 0a 20 20 20 20  mat, reg3);.    
23e37 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23e38 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69  3(v, OP_SetCooki
23e39 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 46 49  e, iDb, BTREE_FI
23e3a 4c 45 5f 46 4f 52 4d 41 54 2c 20 72 65 67 33 29  LE_FORMAT, reg3)
23e3b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23e3c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
23e3d 74 65 67 65 72 2c 20 45 4e 43 28 64 62 29 2c 20  teger, ENC(db), 
23e3e 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74  reg3);.    sqlit
23e3f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
23e40 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44  OP_SetCookie, iD
23e41 62 2c 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e  b, BTREE_TEXT_EN
23e42 43 4f 44 49 4e 47 2c 20 72 65 67 33 29 3b 0a 20  CODING, reg3);. 
23e43 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
23e44 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a  mpHere(v, j1);..
23e45 20 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73 74      /* This just
23e46 20 63 72 65 61 74 65 73 20 61 20 70 6c 61 63 65   creates a place
23e47 2d 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20 69  -holder record i
23e48 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
23e49 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a  ter table..    *
23e4a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 63 72 65  * The record cre
23e4b 61 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63 6f  ated does not co
23e4c 6e 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20 79  ntain anything y
23e4d 65 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20  et.  It will be 
23e4e 72 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a 20  replaced.    ** 
23e4f 62 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74 72  by the real entr
23e50 79 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72 61  y in code genera
23e51 74 65 64 20 61 74 20 73 71 6c 69 74 65 33 45 6e  ted at sqlite3En
23e52 64 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2a  dTable()..    **
23e53 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 69  .    ** The rowi
23e54 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65 6e  d for the new en
23e55 74 72 79 20 69 73 20 6c 65 66 74 20 69 6e 20 72  try is left in r
23e56 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e  egister pParse->
23e57 72 65 67 52 6f 77 69 64 2e 0a 20 20 20 20 2a 2a  regRowid..    **
23e58 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   The root page n
23e59 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77  umber of the new
23e5a 20 74 61 62 6c 65 20 69 73 20 6c 65 66 74 20 69   table is left i
23e5b 6e 20 72 65 67 20 70 50 61 72 73 65 2d 3e 72 65  n reg pParse->re
23e5c 67 52 6f 6f 74 2e 0a 20 20 20 20 2a 2a 20 54 68  gRoot..    ** Th
23e5d 65 20 72 6f 77 69 64 20 61 6e 64 20 72 6f 6f 74  e rowid and root
23e5e 20 70 61 67 65 20 6e 75 6d 62 65 72 20 76 61 6c   page number val
23e5f 75 65 73 20 61 72 65 20 6e 65 65 64 65 64 20 62  ues are needed b
23e60 79 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 0a  y the code that.
23e61 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 45 6e      ** sqlite3En
23e62 64 54 61 62 6c 65 20 77 69 6c 6c 20 67 65 6e 65  dTable will gene
23e63 72 61 74 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  rate..    */.#if
23e64 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
23e65 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21  _OMIT_VIEW) || !
23e66 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
23e67 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
23e68 29 0a 20 20 20 20 69 66 28 20 69 73 56 69 65 77  ).    if( isView
23e69 20 7c 7c 20 69 73 56 69 72 74 75 61 6c 20 29 7b   || isVirtual ){
23e6a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
23e6b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
23e6c 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 32 29  nteger, 0, reg2)
23e6d 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64  ;.    }else.#end
23e6e 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 73  if.    {.      s
23e6f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23e70 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 54 61 62  (v, OP_CreateTab
23e71 6c 65 2c 20 69 44 62 2c 20 72 65 67 32 29 3b 0a  le, iDb, reg2);.
23e72 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
23e73 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65  3OpenMasterTable
23e74 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
23e75 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23e76 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp2(v, OP_NewRo
23e77 77 69 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a 20  wid, 0, reg1);. 
23e78 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23e79 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
23e7a 20 30 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73   0, reg3);.    s
23e7b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
23e7c 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30  (v, OP_Insert, 0
23e7d 2c 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0a 20  , reg3, reg1);. 
23e7e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
23e7f 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
23e80 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71  _APPEND);.    sq
23e81 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
23e82 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20  v, OP_Close);.  
23e83 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28  }..  /* Normal (
23e84 6e 6f 6e 2d 65 72 72 6f 72 29 20 72 65 74 75 72  non-error) retur
23e85 6e 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b 0a  n. */.  return;.
23e86 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f  .  /* If an erro
23e87 72 20 6f 63 63 75 72 73 2c 20 77 65 20 6a 75 6d  r occurs, we jum
23e88 70 20 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e 5f  p here */.begin_
23e89 74 61 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20 73  table_error:.  s
23e8a 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
23e8b 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72   zName);.  retur
23e8c 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  n;.}../*.** This
23e8d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74   macro is used t
23e8e 6f 20 63 6f 6d 70 61 72 65 20 74 77 6f 20 73 74  o compare two st
23e8f 72 69 6e 67 73 20 69 6e 20 61 20 63 61 73 65 2d  rings in a case-
23e90 69 6e 73 65 6e 73 69 74 69 76 65 20 6d 61 6e 6e  insensitive mann
23e91 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 73 6c 69  er..** It is sli
23e92 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 61  ghtly faster tha
23e93 6e 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  n calling sqlite
23e94 33 53 74 72 49 43 6d 70 28 29 20 64 69 72 65 63  3StrICmp() direc
23e95 74 6c 79 2c 20 62 75 74 0a 2a 2a 20 70 72 6f 64  tly, but.** prod
23e96 75 63 65 73 20 6c 61 72 67 65 72 20 63 6f 64 65  uces larger code
23e97 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a  ..**.** WARNING:
23e98 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 6e   This macro is n
23e99 6f 74 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69  ot compatible wi
23e9a 74 68 20 74 68 65 20 73 74 72 63 6d 70 28 29 20  th the strcmp() 
23e9b 66 61 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20 72 65  family. It.** re
23e9c 74 75 72 6e 73 20 74 72 75 65 20 69 66 20 74 68  turns true if th
23e9d 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20 61 72  e two strings ar
23e9e 65 20 65 71 75 61 6c 2c 20 6f 74 68 65 72 77 69  e equal, otherwi
23e9f 73 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65  se false..*/.#de
23ea0 66 69 6e 65 20 53 54 52 49 43 4d 50 28 78 2c 20  fine STRICMP(x, 
23ea1 79 29 20 28 5c 0a 73 71 6c 69 74 65 33 55 70 70  y) (\.sqlite3Upp
23ea2 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69  erToLower[*(unsi
23ea3 67 6e 65 64 20 63 68 61 72 20 2a 29 28 78 29 5d  gned char *)(x)]
23ea4 3d 3d 20 20 20 5c 0a 73 71 6c 69 74 65 33 55 70  ==   \.sqlite3Up
23ea5 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73  perToLower[*(uns
23ea6 69 67 6e 65 64 20 63 68 61 72 20 2a 29 28 79 29  igned char *)(y)
23ea7 5d 20 20 20 20 20 5c 0a 26 26 20 73 71 6c 69 74  ]     \.&& sqlit
23ea8 65 33 53 74 72 49 43 6d 70 28 28 78 29 2b 31 2c  e3StrICmp((x)+1,
23ea9 28 79 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a  (y)+1)==0 )../*.
23eaa 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 63 6f 6c  ** Add a new col
23eab 75 6d 6e 20 74 6f 20 74 68 65 20 74 61 62 6c 65  umn to the table
23eac 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67   currently being
23ead 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2a   constructed..**
23eae 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63  .** The parser c
23eaf 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
23eb0 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  e once for each 
23eb1 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69  column declarati
23eb2 6f 6e 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41 54  on.** in a CREAT
23eb3 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
23eb4 74 2e 20 20 73 71 6c 69 74 65 33 53 74 61 72 74  t.  sqlite3Start
23eb5 54 61 62 6c 65 28 29 20 67 65 74 73 20 63 61 6c  Table() gets cal
23eb6 6c 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 6f 20  led.** first to 
23eb7 67 65 74 20 74 68 69 6e 67 73 20 67 6f 69 6e 67  get things going
23eb8 2e 20 20 54 68 65 6e 20 74 68 69 73 20 72 6f 75  .  Then this rou
23eb9 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
23eba 6f 72 20 65 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d  or each.** colum
23ebb 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
23ebc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23ebd 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65  3AddColumn(Parse
23ebe 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
23ebf 2a 70 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65  *pName){.  Table
23ec0 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20   *p;.  int i;.  
23ec1 63 68 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d  char *z;.  Colum
23ec2 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74  n *pCol;.  sqlit
23ec3 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
23ec4 3e 64 62 3b 0a 20 20 69 66 28 20 28 70 20 3d 20  >db;.  if( (p = 
23ec5 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
23ec6 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  e)==0 ) return;.
23ec7 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  #if SQLITE_MAX_C
23ec8 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 6e  OLUMN.  if( p->n
23ec9 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69 74  Col+1>db->aLimit
23eca 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
23ecb 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c  LUMN] ){.    sql
23ecc 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23ecd 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63  rse, "too many c
23ece 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73 22 2c 20 70  olumns on %s", p
23ecf 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ->zName);.    re
23ed0 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
23ed1 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61  .  z = sqlite3Na
23ed2 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
23ed3 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d  pName);.  if( z=
23ed4 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  =0 ) return;.  f
23ed5 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  or(i=0; i<p->nCo
23ed6 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  l; i++){.    if(
23ed7 20 53 54 52 49 43 4d 50 28 7a 2c 20 70 2d 3e 61   STRICMP(z, p->a
23ed8 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b  Col[i].zName) ){
23ed9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
23eda 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23edb 64 75 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d 6e  duplicate column
23edc 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b 0a   name: %s", z);.
23edd 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
23ede 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20  ree(db, z);.    
23edf 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
23ee0 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e 43    }.  if( (p->nC
23ee1 6f 6c 20 26 20 30 78 37 29 3d 3d 30 20 29 7b 0a  ol & 0x7)==0 ){.
23ee2 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77      Column *aNew
23ee3 3b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c  ;.    aNew = sql
23ee4 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
23ee5 2c 70 2d 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f  ,p->aCol,(p->nCo
23ee6 6c 2b 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e 61  l+8)*sizeof(p->a
23ee7 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66  Col[0]));.    if
23ee8 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20  ( aNew==0 ){.   
23ee9 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23eea 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72  (db, z);.      r
23eeb 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
23eec 20 70 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77 3b   p->aCol = aNew;
23eed 0a 20 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26 70  .  }.  pCol = &p
23eee 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b  ->aCol[p->nCol];
23eef 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c 20  .  memset(pCol, 
23ef0 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f  0, sizeof(p->aCo
23ef1 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e  l[0]));.  pCol->
23ef2 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f  zName = z;. .  /
23ef3 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
23ef4 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64 2c   type specified,
23ef5 20 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74 68   columns have th
23ef6 65 20 64 65 66 61 75 6c 74 20 61 66 66 69 6e 69  e default affini
23ef7 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e 20  ty.  ** 'NONE'. 
23ef8 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74 79  If there is a ty
23ef9 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 74 68  pe specified, th
23efa 65 6e 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  en sqlite3AddCol
23efb 75 6d 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a 20  umnType() will. 
23efc 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e 65   ** be called ne
23efd 78 74 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d 3e  xt to set pCol->
23efe 61 66 66 69 6e 69 74 79 20 63 6f 72 72 65 63 74  affinity correct
23eff 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d  ly..  */.  pCol-
23f00 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49  >affinity = SQLI
23f01 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 70  TE_AFF_NONE;.  p
23f02 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a  ->nCol++;.}../*.
23f03 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
23f04 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
23f05 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e   parser while in
23f06 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a   the middle of.*
23f07 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 41  * parsing a CREA
23f08 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
23f09 6e 74 2e 20 20 41 20 22 4e 4f 54 20 4e 55 4c 4c  nt.  A "NOT NULL
23f0a 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 73  " constraint has
23f0b 0a 2a 2a 20 62 65 65 6e 20 73 65 65 6e 20 6f 6e  .** been seen on
23f0c 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 69 73   a column.  This
23f0d 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
23f0e 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67 20 6f  e notNull flag o
23f0f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20  n.** the column 
23f10 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20  currently under 
23f11 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f  construction..*/
23f12 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23f13 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 4e  void sqlite3AddN
23f14 6f 74 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 50  otNull(Parse *pP
23f15 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72 6f  arse, int onErro
23f16 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  r){.  Table *p;.
23f17 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e    p = pParse->pN
23f18 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
23f19 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e  ==0 || NEVER(p->
23f1a 6e 43 6f 6c 3c 31 29 20 29 20 72 65 74 75 72 6e  nCol<1) ) return
23f1b 3b 0a 20 20 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e  ;.  p->aCol[p->n
23f1c 43 6f 6c 2d 31 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d  Col-1].notNull =
23f1d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 7d 0a   (u8)onError;.}.
23f1e 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 65 20  ./*.** Scan the 
23f1f 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6e 61 6d 65  column type name
23f20 20 7a 54 79 70 65 20 28 6c 65 6e 67 74 68 20 6e   zType (length n
23f21 54 79 70 65 29 20 61 6e 64 20 72 65 74 75 72 6e  Type) and return
23f22 20 74 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74   the.** associat
23f23 65 64 20 61 66 66 69 6e 69 74 79 20 74 79 70 65  ed affinity type
23f24 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
23f25 74 69 6e 65 20 64 6f 65 73 20 61 20 63 61 73 65  tine does a case
23f26 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 65 61  -independent sea
23f27 72 63 68 20 6f 66 20 7a 54 79 70 65 20 66 6f 72  rch of zType for
23f28 20 74 68 65 20 0a 2a 2a 20 73 75 62 73 74 72 69   the .** substri
23f29 6e 67 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ngs in the follo
23f2a 77 69 6e 67 20 74 61 62 6c 65 2e 20 49 66 20 6f  wing table. If o
23f2b 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72  ne of the substr
23f2c 69 6e 67 73 20 69 73 0a 2a 2a 20 66 6f 75 6e 64  ings is.** found
23f2d 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  , the correspond
23f2e 69 6e 67 20 61 66 66 69 6e 69 74 79 20 69 73 20  ing affinity is 
23f2f 72 65 74 75 72 6e 65 64 2e 20 49 66 20 7a 54 79  returned. If zTy
23f30 70 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 6d  pe contains.** m
23f31 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20  ore than one of 
23f32 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 2c 20  the substrings, 
23f33 65 6e 74 72 69 65 73 20 74 6f 77 61 72 64 20 74  entries toward t
23f34 68 65 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 74 68  he top of .** th
23f35 65 20 74 61 62 6c 65 20 74 61 6b 65 20 70 72 69  e table take pri
23f36 6f 72 69 74 79 2e 20 46 6f 72 20 65 78 61 6d 70  ority. For examp
23f37 6c 65 2c 20 69 66 20 7a 54 79 70 65 20 69 73 20  le, if zType is 
23f38 27 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20 53  'BLOBINT', .** S
23f39 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
23f3a 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  R is returned..*
23f3b 2a 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67 20 20  *.** Substring  
23f3c 20 20 20 7c 20 41 66 66 69 6e 69 74 79 0a 2a 2a     | Affinity.**
23f3d 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
23f3e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23f3f 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 20 20 20 20  -.** 'INT'      
23f40 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
23f41 49 4e 54 45 47 45 52 0a 2a 2a 20 27 43 48 41 52  INTEGER.** 'CHAR
23f42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
23f43 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 43  E_AFF_TEXT.** 'C
23f44 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53 51  LOB'        | SQ
23f45 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a  LITE_AFF_TEXT.**
23f46 20 27 54 45 58 54 27 20 20 20 20 20 20 20 20 7c   'TEXT'        |
23f47 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
23f48 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 20 20 20 20  .** 'BLOB'      
23f49 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e    | SQLITE_AFF_N
23f4a 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c 27 20 20 20  ONE.** 'REAL'   
23f4b 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
23f4c 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41 27  F_REAL.** 'FLOA'
23f4d 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
23f4e 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 44 4f  _AFF_REAL.** 'DO
23f4f 55 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c  UB'        | SQL
23f50 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 0a  ITE_AFF_REAL.**.
23f51 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68  ** If none of th
23f52 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20  e substrings in 
23f53 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65 20  the above table 
23f54 61 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53 51  are found,.** SQ
23f55 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
23f56 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
23f57 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23f58 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69  char sqlite3Affi
23f59 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 63  nityType(const c
23f5a 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 33 32  har *zIn){.  u32
23f5b 20 68 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 61   h = 0;.  char a
23f5c 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
23f5d 4e 55 4d 45 52 49 43 3b 0a 0a 20 20 69 66 28 20  NUMERIC;..  if( 
23f5e 7a 49 6e 20 29 20 77 68 69 6c 65 28 20 7a 49 6e  zIn ) while( zIn
23f5f 5b 30 5d 20 29 7b 0a 20 20 20 20 68 20 3d 20 28  [0] ){.    h = (
23f60 68 3c 3c 38 29 20 2b 20 73 71 6c 69 74 65 33 55  h<<8) + sqlite3U
23f61 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 2a 7a 49  pperToLower[(*zI
23f62 6e 29 26 30 78 66 66 5d 3b 0a 20 20 20 20 7a 49  n)&0xff];.    zI
23f63 6e 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3d 3d  n++;.    if( h==
23f64 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 68 27 3c  (('c'<<24)+('h'<
23f65 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 72  <16)+('a'<<8)+'r
23f66 27 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  ') ){           
23f67 20 20 2f 2a 20 43 48 41 52 20 2a 2f 0a 20 20 20    /* CHAR */.   
23f68 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
23f69 41 46 46 5f 54 45 58 54 3b 20 0a 20 20 20 20 7d  AFF_TEXT; .    }
23f6a 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 63  else if( h==(('c
23f6b 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29  '<<24)+('l'<<16)
23f6c 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 29  +('o'<<8)+'b') )
23f6d 7b 20 20 20 20 20 20 20 2f 2a 20 43 4c 4f 42 20  {       /* CLOB 
23f6e 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  */.      aff = S
23f6f 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a  QLITE_AFF_TEXT;.
23f70 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d      }else if( h=
23f71 3d 28 28 27 74 27 3c 3c 32 34 29 2b 28 27 65 27  =(('t'<<24)+('e'
23f72 3c 3c 31 36 29 2b 28 27 78 27 3c 3c 38 29 2b 27  <<16)+('x'<<8)+'
23f73 74 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 20  t') ){       /* 
23f74 54 45 58 54 20 2a 2f 0a 20 20 20 20 20 20 61 66  TEXT */.      af
23f75 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54  f = SQLITE_AFF_T
23f76 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  EXT;.    }else i
23f77 66 28 20 68 3d 3d 28 28 27 62 27 3c 3c 32 34 29  f( h==(('b'<<24)
23f78 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c  +('l'<<16)+('o'<
23f79 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20  <8)+'b')        
23f7a 20 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 20    /* BLOB */.   
23f7b 20 20 20 20 20 26 26 20 28 61 66 66 3d 3d 53 51       && (aff==SQ
23f7c 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
23f7d 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f   || aff==SQLITE_
23f7e 41 46 46 5f 52 45 41 4c 29 20 29 7b 0a 20 20 20  AFF_REAL) ){.   
23f7f 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
23f80 41 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64 65  AFF_NONE;.#ifnde
23f81 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
23f82 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20  OATING_POINT.   
23f83 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28   }else if( h==((
23f84 27 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31  'r'<<24)+('e'<<1
23f85 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29  6)+('a'<<8)+'l')
23f86 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41            /* REA
23f87 4c 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20  L */.        && 
23f88 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
23f89 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20  NUMERIC ){.     
23f8a 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46   aff = SQLITE_AF
23f8b 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73  F_REAL;.    }els
23f8c 65 20 69 66 28 20 68 3d 3d 28 28 27 66 27 3c 3c  e if( h==(('f'<<
23f8d 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27  24)+('l'<<16)+('
23f8e 6f 27 3c 3c 38 29 2b 27 61 27 29 20 20 20 20 20  o'<<8)+'a')     
23f8f 20 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a       /* FLOA */.
23f90 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d          && aff==
23f91 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
23f92 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20  IC ){.      aff 
23f93 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  = SQLITE_AFF_REA
23f94 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  L;.    }else if(
23f95 20 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29 2b 28   h==(('d'<<24)+(
23f96 27 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c 3c 38  'o'<<16)+('u'<<8
23f97 29 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 20  )+'b')          
23f98 2f 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20 20 20  /* DOUB */.     
23f99 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54     && aff==SQLIT
23f9a 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b  E_AFF_NUMERIC ){
23f9b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c  .      aff = SQL
23f9c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 65  ITE_AFF_REAL;.#e
23f9d 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 69  ndif.    }else i
23f9e 66 28 20 28 68 26 30 78 30 30 46 46 46 46 46 46  f( (h&0x00FFFFFF
23f9f 29 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b 28 27  )==(('i'<<16)+('
23fa0 6e 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20  n'<<8)+'t') ){  
23fa1 20 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 20    /* INT */.    
23fa2 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
23fa3 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  FF_INTEGER;.    
23fa4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
23fa5 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 66 66   }..  return aff
23fa6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
23fa7 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
23fa8 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
23fa9 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64  while in the mid
23faa 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e  dle of.** parsin
23fab 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  g a CREATE TABLE
23fac 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
23fad 20 70 46 69 72 73 74 20 74 6f 6b 65 6e 20 69 73   pFirst token is
23fae 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f   the first.** to
23faf 6b 65 6e 20 69 6e 20 74 68 65 20 73 65 71 75 65  ken in the seque
23fb0 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 74 68  nce of tokens th
23fb1 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65 20  at describe the 
23fb2 74 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63  type of the.** c
23fb3 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20  olumn currently 
23fb4 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
23fb5 6f 6e 2e 20 20 20 70 4c 61 73 74 20 69 73 20 74  on.   pLast is t
23fb6 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a  he last token.**
23fb7 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65   in the sequence
23fb8 2e 20 20 55 73 65 20 74 68 69 73 20 69 6e 66 6f  .  Use this info
23fb9 72 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 74  rmation to const
23fba 72 75 63 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a  ruct a string.**
23fbb 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
23fbc 68 65 20 74 79 70 65 6e 61 6d 65 20 6f 66 20 74  he typename of t
23fbd 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 74  he column and st
23fbe 6f 72 65 20 74 68 61 74 20 73 74 72 69 6e 67 0a  ore that string.
23fbf 2a 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20  ** in zType..*/ 
23fc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23fc1 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43  void sqlite3AddC
23fc2 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 20  olumnType(Parse 
23fc3 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a  *pParse, Token *
23fc4 70 54 79 70 65 29 7b 0a 20 20 54 61 62 6c 65 20  pType){.  Table 
23fc5 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  *p;.  Column *pC
23fc6 6f 6c 3b 0a 0a 20 20 70 20 3d 20 70 50 61 72 73  ol;..  p = pPars
23fc7 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
23fc8 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45  if( p==0 || NEVE
23fc9 52 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 72  R(p->nCol<1) ) r
23fca 65 74 75 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20  eturn;.  pCol = 
23fcb 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c  &p->aCol[p->nCol
23fcc 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  -1];.  assert( p
23fcd 43 6f 6c 2d 3e 7a 54 79 70 65 3d 3d 30 20 29 3b  Col->zType==0 );
23fce 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d  .  pCol->zType =
23fcf 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
23fd0 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62  Token(pParse->db
23fd1 2c 20 70 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c  , pType);.  pCol
23fd2 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c  ->affinity = sql
23fd3 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
23fd4 28 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 7d  (pCol->zType);.}
23fd5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72  ../*.** The expr
23fd6 65 73 73 69 6f 6e 20 69 73 20 74 68 65 20 64 65  ession is the de
23fd7 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
23fd8 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
23fd9 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a  y added column.*
23fda 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63  * of the table c
23fdb 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63  urrently under c
23fdc 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a  onstruction..**.
23fdd 2a 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65  ** Default value
23fde 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75 73   expressions mus
23fdf 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20  t be constant.  
23fe0 52 61 69 73 65 20 61 6e 20 65 78 63 65 70 74 69  Raise an excepti
23fe1 6f 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 73  on if this.** is
23fe2 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2e 0a 2a   not the case..*
23fe3 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
23fe4 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  e is called by t
23fe5 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20  he parser while 
23fe6 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
23fe7 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52  .** parsing a CR
23fe8 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
23fe9 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
23fea 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23feb 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61  ite3AddDefaultVa
23fec 6c 75 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  lue(Parse *pPars
23fed 65 2c 20 45 78 70 72 53 70 61 6e 20 2a 70 53 70  e, ExprSpan *pSp
23fee 61 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  an){.  Table *p;
23fef 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
23ff0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23ff1 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70   pParse->db;.  p
23ff2 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
23ff3 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 21 3d 30  able;.  if( p!=0
23ff4 20 29 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20 26   ){.    pCol = &
23ff5 28 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c  (p->aCol[p->nCol
23ff6 2d 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 73  -1]);.    if( !s
23ff7 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
23ff8 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 70  tantOrFunction(p
23ff9 53 70 61 6e 2d 3e 70 45 78 70 72 29 20 29 7b 0a  Span->pExpr) ){.
23ffa 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
23ffb 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 64  orMsg(pParse, "d
23ffc 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20  efault value of 
23ffd 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69 73 20 6e  column [%s] is n
23ffe 6f 74 20 63 6f 6e 73 74 61 6e 74 22 2c 0a 20 20  ot constant",.  
23fff 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e          pCol->zN
24000 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ame);.    }else{
24001 0a 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79  .      /* A copy
24002 20 6f 66 20 70 45 78 70 72 20 69 73 20 75 73 65   of pExpr is use
24003 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  d instead of the
24004 20 6f 72 69 67 69 6e 61 6c 2c 20 61 73 20 70 45   original, as pE
24005 78 70 72 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20  xpr contains.   
24006 20 20 20 2a 2a 20 74 6f 6b 65 6e 73 20 74 68 61     ** tokens tha
24007 74 20 70 6f 69 6e 74 20 74 6f 20 76 6f 6c 61 74  t point to volat
24008 69 6c 65 20 6d 65 6d 6f 72 79 2e 20 54 68 65 20  ile memory. The 
24009 27 73 70 61 6e 27 20 6f 66 20 74 68 65 20 65 78  'span' of the ex
2400a 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a  pression.      *
2400b 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 62 79  * is required by
2400c 20 70 72 61 67 6d 61 20 74 61 62 6c 65 5f 69 6e   pragma table_in
2400d 66 6f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  fo..      */.   
2400e 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
2400f 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70  lete(db, pCol->p
24010 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43 6f  Dflt);.      pCo
24011 6c 2d 3e 70 44 66 6c 74 20 3d 20 73 71 6c 69 74  l->pDflt = sqlit
24012 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53  e3ExprDup(db, pS
24013 70 61 6e 2d 3e 70 45 78 70 72 2c 20 45 58 50 52  pan->pExpr, EXPR
24014 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
24015 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
24016 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74  (db, pCol->zDflt
24017 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a  );.      pCol->z
24018 44 66 6c 74 20 3d 20 73 71 6c 69 74 65 33 44 62  Dflt = sqlite3Db
24019 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61  StrNDup(db, (cha
2401a 72 2a 29 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74  r*)pSpan->zStart
2401b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2401c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2401d 20 20 20 20 20 20 20 28 69 6e 74 29 28 70 53 70         (int)(pSp
2401e 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 70 61 6e  an->zEnd - pSpan
2401f 2d 3e 7a 53 74 61 72 74 29 29 3b 0a 20 20 20 20  ->zStart));.    
24020 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  }.  }.  sqlite3E
24021 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 53  xprDelete(db, pS
24022 70 61 6e 2d 3e 70 45 78 70 72 29 3b 0a 7d 0a 0a  pan->pExpr);.}..
24023 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 20  /*.** Designate 
24024 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
24025 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20  for the table.  
24026 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20  pList is a list 
24027 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20  of names .** of 
24028 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f 72  columns that for
24029 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65  m the primary ke
2402a 79 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 20  y.  If pList is 
2402b 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a  NULL, then the.*
2402c 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  * most recently 
2402d 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20  added column of 
2402e 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65  the table is the
2402f 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a   primary key..**
24030 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e 20  .** A table can 
24031 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65  have at most one
24032 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49   primary key.  I
24033 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65  f the table alre
24034 61 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 69  ady has.** a pri
24035 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74 68  mary key (and th
24036 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64  is is the second
24037 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74 68   primary key) th
24038 65 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a 20  en create an.** 
24039 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  error..**.** If 
2403a 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
2403b 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 63  is on a single c
2403c 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 61  olumn whose data
2403d 74 79 70 65 20 69 73 20 49 4e 54 45 47 45 52 2c  type is INTEGER,
2403e 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c 6c  .** then we will
2403f 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 61 74   try to use that
24040 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72   column as the r
24041 6f 77 69 64 2e 20 20 53 65 74 20 74 68 65 20 54  owid.  Set the T
24042 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 69  able.iPKey.** fi
24043 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65  eld of the table
24044 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
24045 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69 6e  ion to be the in
24046 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e  dex of the.** IN
24047 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
24048 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65  Y column.  Table
24049 2e 69 50 4b 65 79 20 69 73 20 73 65 74 20 74 6f  .iPKey is set to
2404a 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 0a   -1 if there is.
2404b 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52  ** no INTEGER PR
2404c 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a  IMARY KEY..**.**
2404d 20 49 66 20 74 68 65 20 6b 65 79 20 69 73 20 6e   If the key is n
2404e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  ot an INTEGER PR
2404f 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20  IMARY KEY, then 
24050 63 72 65 61 74 65 20 61 20 75 6e 69 71 75 65 0a  create a unique.
24051 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68 65  ** index for the
24052 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 20   key.  No index 
24053 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 49  is created for I
24054 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
24055 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  EYs..*/.SQLITE_P
24056 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
24057 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79  te3AddPrimaryKey
24058 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
24059 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  e,    /* Parsing
2405a 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
2405b 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  prList *pList,  
2405c 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c 64  /* List of field
2405d 20 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e 64   names to be ind
2405e 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  exed */.  int on
2405f 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57  Error,      /* W
24060 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 61  hat to do with a
24061 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66   uniqueness conf
24062 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 75  lict */.  int au
24063 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54  toInc,      /* T
24064 72 75 65 20 69 66 20 74 68 65 20 41 55 54 4f 49  rue if the AUTOI
24065 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64  NCREMENT keyword
24066 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
24067 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 20   int sortOrder  
24068 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f     /* SQLITE_SO_
24069 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 4f  ASC or SQLITE_SO
2406a 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 61  _DESC */.){.  Ta
2406b 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72  ble *pTab = pPar
2406c 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
2406d 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 30   char *zType = 0
2406e 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d  ;.  int iCol = -
2406f 31 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61 62  1, i;.  if( pTab
24070 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52  ==0 || IN_DECLAR
24071 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70 72  E_VTAB ) goto pr
24072 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a  imary_key_exit;.
24073 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46    if( pTab->tabF
24074 6c 61 67 73 20 26 20 54 46 5f 48 61 73 50 72 69  lags & TF_HasPri
24075 6d 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20 73  maryKey ){.    s
24076 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
24077 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 74  Parse, .      "t
24078 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 20  able \"%s\" has 
24079 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 72  more than one pr
2407a 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61 62  imary key", pTab
2407b 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
2407c 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65  to primary_key_e
2407d 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d  xit;.  }.  pTab-
2407e 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f  >tabFlags |= TF_
2407f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a 20  HasPrimaryKey;. 
24080 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b   if( pList==0 ){
24081 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62  .    iCol = pTab
24082 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20  ->nCol - 1;.    
24083 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
24084 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a  .isPrimKey = 1;.
24085 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72    }else{.    for
24086 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
24087 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
24088 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43    for(iCol=0; iC
24089 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69  ol<pTab->nCol; i
2408a 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Col++){.        
2408b 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2408c 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  mp(pList->a[i].z
2408d 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  Name, pTab->aCol
2408e 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30  [iCol].zName)==0
2408f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72   ){.          br
24090 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
24091 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
24092 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c   iCol<pTab->nCol
24093 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62   ){.        pTab
24094 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50  ->aCol[iCol].isP
24095 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20  rimKey = 1;.    
24096 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
24097 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 31  ( pList->nExpr>1
24098 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20   ) iCol = -1;.  
24099 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20  }.  if( iCol>=0 
2409a 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  && iCol<pTab->nC
2409b 6f 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20  ol ){.    zType 
2409c 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  = pTab->aCol[iCo
2409d 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 20  l].zType;.  }.  
2409e 69 66 28 20 7a 54 79 70 65 20 26 26 20 73 71 6c  if( zType && sql
2409f 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79 70  ite3StrICmp(zTyp
240a0 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d 30  e, "INTEGER")==0
240a1 0a 20 20 20 20 20 20 20 20 26 26 20 73 6f 72 74  .        && sort
240a2 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53 4f  Order==SQLITE_SO
240a3 5f 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61 62  _ASC ){.    pTab
240a4 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a  ->iPKey = iCol;.
240a5 20 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e      pTab->keyCon
240a6 66 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b  f = (u8)onError;
240a7 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 75 74  .    assert( aut
240a8 6f 49 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49  oInc==0 || autoI
240a9 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54 61  nc==1 );.    pTa
240aa 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 61  b->tabFlags |= a
240ab 75 74 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e  utoInc*TF_Autoin
240ac 63 72 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65  crement;.  }else
240ad 20 69 66 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a   if( autoInc ){.
240ae 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
240af 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
240b0 54 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  T.    sqlite3Err
240b1 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 41  orMsg(pParse, "A
240b2 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20  UTOINCREMENT is 
240b3 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20  only allowed on 
240b4 61 6e 20 22 0a 20 20 20 20 20 20 20 22 49 4e 54  an ".       "INT
240b5 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
240b6 22 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c  ");.#endif.  }el
240b7 73 65 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  se{.    Index *p
240b8 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ;.    p = sqlite
240b9 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61  3CreateIndex(pPa
240ba 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 70 4c  rse, 0, 0, 0, pL
240bb 69 73 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c  ist, onError, 0,
240bc 20 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c 20 30   0, sortOrder, 0
240bd 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  );.    if( p ){.
240be 20 20 20 20 20 20 70 2d 3e 61 75 74 6f 49 6e 64        p->autoInd
240bf 65 78 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20  ex = 2;.    }.  
240c0 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d    pList = 0;.  }
240c1 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78  ..primary_key_ex
240c2 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70  it:.  sqlite3Exp
240c3 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
240c4 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a  se->db, pList);.
240c5 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a    return;.}../*.
240c6 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 45  ** Add a new CHE
240c7 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f  CK constraint to
240c8 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65   the table curre
240c9 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
240ca 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ruction..*/.SQLI
240cb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
240cc 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43  sqlite3AddCheckC
240cd 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72  onstraint(.  Par
240ce 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
240cf 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
240d0 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 68  t */.  Expr *pCh
240d1 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20  eckExpr  /* The 
240d2 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e  check expression
240d3 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
240d4 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
240d5 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  b;.#ifndef SQLIT
240d6 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54  E_OMIT_CHECK.  T
240d7 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
240d8 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
240d9 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21 49    if( pTab && !I
240da 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
240db 7b 0a 20 20 20 20 70 54 61 62 2d 3e 70 43 68 65  {.    pTab->pChe
240dc 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ck = sqlite3Expr
240dd 41 6e 64 28 64 62 2c 20 70 54 61 62 2d 3e 70 43  And(db, pTab->pC
240de 68 65 63 6b 2c 20 70 43 68 65 63 6b 45 78 70 72  heck, pCheckExpr
240df 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
240e0 66 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65  f.  {.    sqlite
240e1 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
240e2 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 20 20 7d  pCheckExpr);.  }
240e3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
240e4 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63  e collation func
240e5 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74  tion of the most
240e6 20 72 65 63 65 6e 74 6c 79 20 70 61 72 73 65 64   recently parsed
240e7 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a   table column.**
240e8 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65 71 20   to the CollSeq 
240e9 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  given..*/.SQLITE
240ea 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
240eb 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54  lite3AddCollateT
240ec 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ype(Parse *pPars
240ed 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  e, Token *pToken
240ee 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
240ef 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a   int i;.  char *
240f0 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  zColl;          
240f1 20 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20      /* Dequoted 
240f2 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f  name of collatio
240f3 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20  n sequence */.  
240f4 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20  sqlite3 *db;..  
240f5 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d  if( (p = pParse-
240f6 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29  >pNewTable)==0 )
240f7 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70   return;.  i = p
240f8 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d  ->nCol-1;.  db =
240f9 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a   pParse->db;.  z
240fa 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61  Coll = sqlite3Na
240fb 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
240fc 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21  pToken);.  if( !
240fd 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a  zColl ) return;.
240fe 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4c 6f  .  if( sqlite3Lo
240ff 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  cateCollSeq(pPar
24100 73 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20  se, zColl) ){.  
24101 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
24102 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43     p->aCol[i].zC
24103 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 0a  oll = zColl;.  .
24104 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f      /* If the co
24105 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 64  lumn is declared
24106 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d   as "<name> PRIM
24107 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20  ARY KEY COLLATE 
24108 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a 20  <type>",.    ** 
24109 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d 61  then an index ma
2410a 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65 61  y have been crea
2410b 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c 75  ted on this colu
2410c 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20  mn before the.  
2410d 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 74    ** collation t
2410e 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20 43  ype was added. C
2410f 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20 69  orrect this if i
24110 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20  t is the case.. 
24111 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 49     */.    for(pI
24112 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49  dx=p->pIndex; pI
24113 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
24114 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73  Next){.      ass
24115 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  ert( pIdx->nColu
24116 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69  mn==1 );.      i
24117 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  f( pIdx->aiColum
24118 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 20  n[0]==i ){.     
24119 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b     pIdx->azColl[
2411a 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e  0] = p->aCol[i].
2411b 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  zColl;.      }. 
2411c 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
2411d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2411e 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a  db, zColl);.  }.
2411f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
24120 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
24121 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
24122 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 61  uence for databa
24123 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a 2a  se native text.*
24124 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e 74  * encoding ident
24125 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74 72  ified by the str
24126 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74  ing zName, lengt
24127 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49  h nName..**.** I
24128 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
24129 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2412a 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  ce is not availa
2412b 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61 69  ble, or not avai
2412c 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20  lable.** in the 
2412d 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20  database native 
2412e 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63 6f  encoding, the co
2412f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20  llation factory 
24130 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a  is invoked to.**
24131 20 72 65 71 75 65 73 74 20 69 74 2e 20 49 66 20   request it. If 
24132 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
24133 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 73  ctory does not s
24134 75 70 70 6c 79 20 73 75 63 68 20 61 20 73 65 71  upply such a seq
24135 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74 68  uence,.** and th
24136 65 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 76  e sequence is av
24137 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68  ailable in anoth
24138 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  er text encoding
24139 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a 2a  , then that is.*
2413a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65  * returned inste
2413b 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20  ad..**.** If no 
2413c 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  versions of the 
2413d 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74  requested collat
2413e 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61 72  ions sequence ar
2413f 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 0a  e available, or.
24140 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f 72  ** another error
24141 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73   occurs, NULL is
24142 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e   returned and an
24143 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
24144 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 70  ritten into.** p
24145 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  Parse..**.** Thi
24146 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 77  s routine is a w
24147 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71  rapper around sq
24148 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
24149 28 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ().  This routin
2414a 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 65  e.** invokes the
2414b 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f   collation facto
2414c 72 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20  ry if the named 
2414d 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f 74  collation cannot
2414e 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e 64   be found.** and
2414f 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72   generates an er
24150 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a  ror message..**.
24151 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c  ** See also: sql
24152 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
24153 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c  ), sqlite3GetCol
24154 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54 45  lSeq().*/.SQLITE
24155 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
24156 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43   *sqlite3LocateC
24157 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50  ollSeq(Parse *pP
24158 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
24159 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 73 71 6c 69   *zName){.  sqli
2415a 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2415b 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 20 3d  ->db;.  u8 enc =
2415c 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 20 69   ENC(db);.  u8 i
2415d 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e 69 6e  nitbusy = db->in
2415e 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c 6c 53  it.busy;.  CollS
2415f 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 70 43  eq *pColl;..  pC
24160 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
24161 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63  dCollSeq(db, enc
24162 2c 20 7a 4e 61 6d 65 2c 20 69 6e 69 74 62 75 73  , zName, initbus
24163 79 29 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 62  y);.  if( !initb
24164 75 73 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 7c  usy && (!pColl |
24165 7c 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20  | !pColl->xCmp) 
24166 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  ){.    pColl = s
24167 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
24168 28 64 62 2c 20 65 6e 63 2c 20 70 43 6f 6c 6c 2c  (db, enc, pColl,
24169 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   zName);.    if(
2416a 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
2416b 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2416c 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
2416d 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  h collation sequ
2416e 65 6e 63 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65  ence: %s", zName
2416f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
24170 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a  return pColl;.}.
24171 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
24172 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
24173 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63  increment the sc
24174 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a  hema cookie..**.
24175 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 20 63 6f  ** The schema co
24176 6f 6b 69 65 20 69 73 20 75 73 65 64 20 74 6f 20  okie is used to 
24177 64 65 74 65 72 6d 69 6e 65 20 77 68 65 6e 20 74  determine when t
24178 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68  he schema for th
24179 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 68  e.** database ch
2417a 61 6e 67 65 73 2e 20 20 41 66 74 65 72 20 65 61  anges.  After ea
2417b 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ch schema change
2417c 2c 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 61 6c  , the cookie val
2417d 75 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20  ue.** changes.  
2417e 57 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 66  When a process f
2417f 69 72 73 74 20 72 65 61 64 73 20 74 68 65 20 73  irst reads the s
24180 63 68 65 6d 61 20 69 74 20 72 65 63 6f 72 64 73  chema it records
24181 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20   the.** cookie. 
24182 20 54 68 65 72 65 61 66 74 65 72 2c 20 77 68 65   Thereafter, whe
24183 6e 65 76 65 72 20 69 74 20 67 6f 65 73 20 74 6f  never it goes to
24184 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
24185 62 61 73 65 2c 0a 2a 2a 20 69 74 20 63 68 65 63  base,.** it chec
24186 6b 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f  ks the cookie to
24187 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73   make sure the s
24188 63 68 65 6d 61 20 68 61 73 20 6e 6f 74 20 63 68  chema has not ch
24189 61 6e 67 65 64 0a 2a 2a 20 73 69 6e 63 65 20 69  anged.** since i
2418a 74 20 77 61 73 20 6c 61 73 74 20 72 65 61 64 2e  t was last read.
2418b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 70 6c 61 6e  .**.** This plan
2418c 20 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65   is not complete
2418d 6c 79 20 62 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e  ly bullet-proof.
2418e 20 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65    It is possible
2418f 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 63 68 65   for.** the sche
24190 6d 61 20 74 6f 20 63 68 61 6e 67 65 20 6d 75 6c  ma to change mul
24191 74 69 70 6c 65 20 74 69 6d 65 73 20 61 6e 64 20  tiple times and 
24192 66 6f 72 20 74 68 65 20 63 6f 6f 6b 69 65 20 74  for the cookie t
24193 6f 20 62 65 0a 2a 2a 20 73 65 74 20 62 61 63 6b  o be.** set back
24194 20 74 6f 20 70 72 69 6f 72 20 76 61 6c 75 65 2e   to prior value.
24195 20 20 42 75 74 20 73 63 68 65 6d 61 20 63 68 61    But schema cha
24196 6e 67 65 73 20 61 72 65 20 69 6e 66 72 65 71 75  nges are infrequ
24197 65 6e 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70  ent.** and the p
24198 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 68 69  robability of hi
24199 74 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63  tting the same c
2419a 6f 6f 6b 69 65 20 76 61 6c 75 65 20 69 73 20 6f  ookie value is o
2419b 6e 6c 79 0a 2a 2a 20 31 20 63 68 61 6e 63 65 20  nly.** 1 chance 
2419c 69 6e 20 32 5e 33 32 2e 20 20 53 6f 20 77 65 27  in 2^32.  So we'
2419d 72 65 20 73 61 66 65 20 65 6e 6f 75 67 68 2e 0a  re safe enough..
2419e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2419f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 68  E void sqlite3Ch
241a0 61 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72 73 65  angeCookie(Parse
241a1 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
241a2 62 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73  b){.  int r1 = s
241a3 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
241a4 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
241a5 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
241a6 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 20  ->db;.  Vdbe *v 
241a7 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
241a8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
241a9 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
241aa 65 72 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  er, db->aDb[iDb]
241ab 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  .pSchema->schema
241ac 5f 63 6f 6f 6b 69 65 2b 31 2c 20 72 31 29 3b 0a  _cookie+1, r1);.
241ad 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
241ae 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f  Op3(v, OP_SetCoo
241af 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f  kie, iDb, BTREE_
241b0 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20  SCHEMA_VERSION, 
241b1 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  r1);.  sqlite3Re
241b2 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
241b3 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a  rse, r1);.}../*.
241b4 2a 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20 6e  ** Measure the n
241b5 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
241b6 65 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f 75  ers needed to ou
241b7 74 70 75 74 20 74 68 65 20 67 69 76 65 6e 0a 2a  tput the given.*
241b8 2a 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 54  * identifier.  T
241b9 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e  he number return
241ba 65 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79 20  ed includes any 
241bb 71 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a 20 62  quotes used.** b
241bc 75 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c  ut does not incl
241bd 75 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 65 72  ude the null ter
241be 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  minator..**.** T
241bf 68 65 20 65 73 74 69 6d 61 74 65 20 69 73 20 63  he estimate is c
241c0 6f 6e 73 65 72 76 61 74 69 76 65 2e 20 20 49 74  onservative.  It
241c1 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72   might be larger
241c2 20 74 68 61 74 20 77 68 61 74 20 69 73 0a 2a 2a   that what is.**
241c3 20 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 2e 0a   really needed..
241c4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 64  */.static int id
241c5 65 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 74 20  entLength(const 
241c6 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  char *z){.  int 
241c7 6e 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 2a 7a  n;.  for(n=0; *z
241c8 3b 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 20  ; n++, z++){.   
241c9 20 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 7b 20   if( *z=='"' ){ 
241ca 6e 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 65 74  n++; }.  }.  ret
241cb 75 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a  urn n + 2;.}../*
241cc 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
241cd 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69  rameter is a poi
241ce 6e 74 65 72 20 74 6f 20 61 6e 20 6f 75 74 70 75  nter to an outpu
241cf 74 20 62 75 66 66 65 72 2e 20 54 68 65 20 73 65  t buffer. The se
241d0 63 6f 6e 64 20 0a 2a 2a 20 70 61 72 61 6d 65 74  cond .** paramet
241d1 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  er is a pointer 
241d2 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  to an integer th
241d3 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
241d4 6f 66 66 73 65 74 20 61 74 0a 2a 2a 20 77 68 69  offset at.** whi
241d5 63 68 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f  ch to write into
241d6 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
241d7 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  er. This functio
241d8 6e 20 63 6f 70 69 65 73 20 74 68 65 0a 2a 2a 20  n copies the.** 
241d9 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  nul-terminated s
241da 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f  tring pointed to
241db 20 62 79 20 74 68 65 20 74 68 69 72 64 20 70 61   by the third pa
241dc 72 61 6d 65 74 65 72 2c 20 7a 53 69 67 6e 65 64  rameter, zSigned
241dd 49 64 65 6e 74 2c 0a 2a 2a 20 74 6f 20 74 68 65  Ident,.** to the
241de 20 73 70 65 63 69 66 69 65 64 20 6f 66 66 73 65   specified offse
241df 74 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20  t in the buffer 
241e0 61 6e 64 20 75 70 64 61 74 65 73 20 2a 70 49 64  and updates *pId
241e1 78 20 74 6f 20 72 65 66 65 72 0a 2a 2a 20 74 6f  x to refer.** to
241e2 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
241e3 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 62  after the last b
241e4 79 74 65 20 77 72 69 74 74 65 6e 20 62 65 66 6f  yte written befo
241e5 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a  re returning..**
241e6 20 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69   .** If the stri
241e7 6e 67 20 7a 53 69 67 6e 65 64 49 64 65 6e 74 20  ng zSignedIdent 
241e8 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c  consists entirel
241e9 79 20 6f 66 20 61 6c 70 68 61 2d 6e 75 6d 65 72  y of alpha-numer
241ea 69 63 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  ic.** characters
241eb 2c 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e  , does not begin
241ec 20 77 69 74 68 20 61 20 64 69 67 69 74 20 61 6e   with a digit an
241ed 64 20 69 73 20 6e 6f 74 20 61 6e 20 53 51 4c 20  d is not an SQL 
241ee 6b 65 79 77 6f 72 64 2c 0a 2a 2a 20 74 68 65 6e  keyword,.** then
241ef 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 6f   it is copied to
241f0 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
241f1 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 69 74  er exactly as it
241f2 20 69 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a   is. Otherwise,.
241f3 2a 2a 20 69 74 20 69 73 20 71 75 6f 74 65 64 20  ** it is quoted 
241f4 75 73 69 6e 67 20 64 6f 75 62 6c 65 2d 71 75 6f  using double-quo
241f5 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tes..*/.static v
241f6 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 68 61  oid identPut(cha
241f7 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 2c  r *z, int *pIdx,
241f8 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 64   char *zSignedId
241f9 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ent){.  unsigned
241fa 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20   char *zIdent = 
241fb 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
241fc 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 20  zSignedIdent;.  
241fd 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75  int i, j, needQu
241fe 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 78  ote;.  i = *pIdx
241ff 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49  ;..  for(j=0; zI
24200 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20  dent[j]; j++){. 
24201 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
24202 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 6a 5d  salnum(zIdent[j]
24203 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d  ) && zIdent[j]!=
24204 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  '_' ) break;.  }
24205 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 73  .  needQuote = s
24206 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 49  qlite3Isdigit(zI
24207 64 65 6e 74 5b 30 5d 29 20 7c 7c 20 73 71 6c 69  dent[0]) || sqli
24208 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 7a  te3KeywordCode(z
24209 49 64 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f 49 44  Ident, j)!=TK_ID
2420a 3b 0a 20 20 69 66 28 20 21 6e 65 65 64 51 75 6f  ;.  if( !needQuo
2420b 74 65 20 29 7b 0a 20 20 20 20 6e 65 65 64 51 75  te ){.    needQu
2420c 6f 74 65 20 3d 20 7a 49 64 65 6e 74 5b 6a 5d 3b  ote = zIdent[j];
2420d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 65 65 64  .  }..  if( need
2420e 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d  Quote ) z[i++] =
2420f 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b   '"';.  for(j=0;
24210 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29   zIdent[j]; j++)
24211 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a  {.    z[i++] = z
24212 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66  Ident[j];.    if
24213 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27  ( zIdent[j]=='"'
24214 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b   ) z[i++] = '"';
24215 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51  .  }.  if( needQ
24216 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20  uote ) z[i++] = 
24217 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b  '"';.  z[i] = 0;
24218 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a  .  *pIdx = i;.}.
24219 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2421a 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
2421b 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72  tatement appropr
2421c 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 69 76  iate for the giv
2421d 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65  en.** table.  Me
2421e 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
2421f 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 74 61   text of the sta
24220 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e  tement is obtain
24221 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  ed.** from sqlit
24222 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75  eMalloc() and mu
24223 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  st be freed by t
24224 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
24225 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ion..*/.static c
24226 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65  har *createTable
24227 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Stmt(sqlite3 *db
24228 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69  , Table *p){.  i
24229 6e 74 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68  nt i, k, n;.  ch
2422a 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61  ar *zStmt;.  cha
2422b 72 20 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c  r *zSep, *zSep2,
2422c 20 2a 7a 45 6e 64 3b 0a 20 20 43 6f 6c 75 6d 6e   *zEnd;.  Column
2422d 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b   *pCol;.  n = 0;
2422e 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d  .  for(pCol = p-
2422f 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d  >aCol, i=0; i<p-
24230 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c  >nCol; i++, pCol
24231 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64  ++){.    n += id
24232 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e  entLength(pCol->
24233 7a 4e 61 6d 65 29 20 2b 20 35 3b 0a 20 20 7d 0a  zName) + 5;.  }.
24234 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67    n += identLeng
24235 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  th(p->zName);.  
24236 69 66 28 20 6e 3c 35 30 20 29 7b 20 0a 20 20 20  if( n<50 ){ .   
24237 20 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 20 20   zSep = "";.    
24238 7a 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20  zSep2 = ",";.   
24239 20 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d   zEnd = ")";.  }
2423a 65 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d  else{.    zSep =
2423b 20 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65   "\n  ";.    zSe
2423c 70 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20  p2 = ",\n  ";.  
2423d 20 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a    zEnd = "\n)";.
2423e 20 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20    }.  n += 35 + 
2423f 36 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74  6*p->nCol;.  zSt
24240 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  mt = sqlite3Mall
24241 6f 63 28 20 6e 20 29 3b 0a 20 20 69 66 28 20 7a  oc( n );.  if( z
24242 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64  Stmt==0 ){.    d
24243 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
24244 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
24245 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
24246 5f 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74  _snprintf(n, zSt
24247 6d 74 2c 20 22 43 52 45 41 54 45 20 54 41 42 4c  mt, "CREATE TABL
24248 45 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71 6c 69  E ");.  k = sqli
24249 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 53 74 6d  te3Strlen30(zStm
2424a 74 29 3b 0a 20 20 69 64 65 6e 74 50 75 74 28 7a  t);.  identPut(z
2424b 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61  Stmt, &k, p->zNa
2424c 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b  me);.  zStmt[k++
2424d 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72 28 70  ] = '(';.  for(p
2424e 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30  Col=p->aCol, i=0
2424f 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  ; i<p->nCol; i++
24250 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73  , pCol++){.    s
24251 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
24252 20 2a 20 63 6f 6e 73 74 20 61 7a 54 79 70 65 5b   * const azType[
24253 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ] = {.        /*
24254 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
24255 20 20 20 20 2a 2f 20 22 20 54 45 58 54 22 2c 0a      */ " TEXT",.
24256 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54          /* SQLIT
24257 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 2a 2f  E_AFF_NONE    */
24258 20 22 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20   "",.        /* 
24259 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
2425a 49 43 20 2a 2f 20 22 20 4e 55 4d 22 2c 0a 20 20  IC */ " NUM",.  
2425b 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f        /* SQLITE_
2425c 41 46 46 5f 49 4e 54 45 47 45 52 20 2a 2f 20 22  AFF_INTEGER */ "
2425d 20 49 4e 54 22 2c 0a 20 20 20 20 20 20 20 20 2f   INT",.        /
2425e 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  * SQLITE_AFF_REA
2425f 4c 20 20 20 20 2a 2f 20 22 20 52 45 41 4c 22 0a  L    */ " REAL".
24260 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6c      };.    int l
24261 65 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  en;.    const ch
24262 61 72 20 2a 7a 54 79 70 65 3b 0a 0a 20 20 20 20  ar *zType;..    
24263 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
24264 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c  (n-k, &zStmt[k],
24265 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20 2b 3d   zSep);.    k +=
24266 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
24267 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20 20 20  (&zStmt[k]);.   
24268 20 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b 0a 20   zSep = zSep2;. 
24269 20 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d     identPut(zStm
2426a 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a 4e 61  t, &k, pCol->zNa
2426b 6d 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  me);.    assert(
2426c 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2d   pCol->affinity-
2426d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
2426e 3e 3d 20 30 20 29 3b 0a 20 20 20 20 61 73 73 65  >= 0 );.    asse
2426f 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  rt( pCol->affini
24270 74 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  ty-SQLITE_AFF_TE
24271 58 54 20 3c 20 73 69 7a 65 6f 66 28 61 7a 54 79  XT < sizeof(azTy
24272 70 65 29 2f 73 69 7a 65 6f 66 28 61 7a 54 79 70  pe)/sizeof(azTyp
24273 65 5b 30 5d 29 20 29 3b 0a 20 20 20 20 74 65 73  e[0]) );.    tes
24274 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66  tcase( pCol->aff
24275 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46  inity==SQLITE_AF
24276 46 5f 54 45 58 54 20 29 3b 0a 20 20 20 20 74 65  F_TEXT );.    te
24277 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66  stcase( pCol->af
24278 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
24279 46 46 5f 4e 4f 4e 45 20 29 3b 0a 20 20 20 20 74  FF_NONE );.    t
2427a 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61  estcase( pCol->a
2427b 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
2427c 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20  AFF_NUMERIC );. 
2427d 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f     testcase( pCo
2427e 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  l->affinity==SQL
2427f 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20  ITE_AFF_INTEGER 
24280 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
24281 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d   pCol->affinity=
24282 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c  =SQLITE_AFF_REAL
24283 20 29 3b 0a 20 20 20 20 0a 20 20 20 20 7a 54 79   );.    .    zTy
24284 70 65 20 3d 20 61 7a 54 79 70 65 5b 70 43 6f 6c  pe = azType[pCol
24285 2d 3e 61 66 66 69 6e 69 74 79 20 2d 20 53 51 4c  ->affinity - SQL
24286 49 54 45 5f 41 46 46 5f 54 45 58 54 5d 3b 0a 20  ITE_AFF_TEXT];. 
24287 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33     len = sqlite3
24288 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29 3b  Strlen30(zType);
24289 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f  .    assert( pCo
2428a 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  l->affinity==SQL
2428b 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 0a 20 20  ITE_AFF_NONE .  
2428c 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43 6f            || pCo
2428d 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 73 71 6c  l->affinity==sql
2428e 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
2428f 28 7a 54 79 70 65 29 20 29 3b 0a 20 20 20 20 6d  (zType) );.    m
24290 65 6d 63 70 79 28 26 7a 53 74 6d 74 5b 6b 5d 2c  emcpy(&zStmt[k],
24291 20 7a 54 79 70 65 2c 20 6c 65 6e 29 3b 0a 20 20   zType, len);.  
24292 20 20 6b 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20    k += len;.    
24293 61 73 73 65 72 74 28 20 6b 3c 3d 6e 20 29 3b 0a  assert( k<=n );.
24294 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e    }.  sqlite3_sn
24295 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74  printf(n-k, &zSt
24296 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e  mt[k], "%s", zEn
24297 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 53 74  d);.  return zSt
24298 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  mt;.}../*.** Thi
24299 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2429a 6c 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68  led to report th
2429b 65 20 66 69 6e 61 6c 20 22 29 22 20 74 68 61 74  e final ")" that
2429c 20 74 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61   terminates.** a
2429d 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
2429e 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  atement..**.** T
2429f 68 65 20 74 61 62 6c 65 20 73 74 72 75 63 74 75  he table structu
242a0 72 65 20 74 68 61 74 20 6f 74 68 65 72 20 61 63  re that other ac
242a1 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68 61  tion routines ha
242a2 76 65 20 62 65 65 6e 20 62 75 69 6c 64 69 6e 67  ve been building
242a3 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20  .** is added to 
242a4 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73  the internal has
242a5 68 20 74 61 62 6c 65 73 2c 20 61 73 73 75 6d 69  h tables, assumi
242a6 6e 67 20 6e 6f 20 65 72 72 6f 72 73 20 68 61 76  ng no errors hav
242a7 65 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a  e.** occurred..*
242a8 2a 0a 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f  *.** An entry fo
242a9 72 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6d  r the table is m
242aa 61 64 65 20 69 6e 20 74 68 65 20 6d 61 73 74 65  ade in the maste
242ab 72 20 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c  r table on disk,
242ac 20 75 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20   unless.** this 
242ad 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74  is a temporary t
242ae 61 62 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74  able or db->init
242af 2e 62 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20  .busy==1.  When 
242b0 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31  db->init.busy==1
242b1 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 77 65 20  .** it means we 
242b2 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20  are reading the 
242b3 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
242b4 62 6c 65 20 62 65 63 61 75 73 65 20 77 65 20 6a  ble because we j
242b5 75 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64  ust.** connected
242b6 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
242b7 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20   or because the 
242b8 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
242b9 62 6c 65 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e  ble has.** recen
242ba 74 6c 79 20 63 68 61 6e 67 65 64 2c 20 73 6f 20  tly changed, so 
242bb 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68  the entry for th
242bc 69 73 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79  is table already
242bd 20 65 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68   exists in.** th
242be 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
242bf 74 61 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f  table.  We do no
242c0 74 20 77 61 6e 74 20 74 6f 20 63 72 65 61 74 65  t want to create
242c1 20 69 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a   it again..**.**
242c2 20 49 66 20 74 68 65 20 70 53 65 6c 65 63 74 20   If the pSelect 
242c3 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20  argument is not 
242c4 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74  NULL, it means t
242c5 68 61 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hat this routine
242c6 0a 2a 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 74  .** was called t
242c7 6f 20 63 72 65 61 74 65 20 61 20 74 61 62 6c 65  o create a table
242c8 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20   generated from 
242c9 61 20 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 41  a .** "CREATE TA
242ca 42 4c 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43  BLE ... AS SELEC
242cb 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74  T ..." statement
242cc 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  .  The column na
242cd 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65  mes of.** the ne
242ce 77 20 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74  w table will mat
242cf 63 68 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ch the result se
242d0 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e  t of the SELECT.
242d1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
242d2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
242d3 6e 64 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  ndTable(.  Parse
242d4 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
242d5 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
242d6 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
242d7 70 43 6f 6e 73 2c 20 20 20 20 20 20 20 20 20 20  pCons,          
242d8 20 2f 2a 20 54 68 65 20 27 2c 27 20 74 6f 6b 65   /* The ',' toke
242d9 6e 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74  n after the last
242da 20 63 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f   column defn. */
242db 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20  .  Token *pEnd, 
242dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
242dd 65 20 66 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65  e final ')' toke
242de 6e 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20  n in the CREATE 
242df 54 41 42 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63  TABLE */.  Selec
242e0 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20  t *pSelect      
242e1 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f     /* Select fro
242e2 6d 20 61 20 22 43 52 45 41 54 45 20 2e 2e 2e 20  m a "CREATE ... 
242e3 41 53 20 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b  AS SELECT" */.){
242e4 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73  .  Table *p;.  s
242e5 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
242e6 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
242e7 44 62 3b 0a 0a 20 20 69 66 28 20 28 70 45 6e 64  Db;..  if( (pEnd
242e8 3d 3d 30 20 26 26 20 70 53 65 6c 65 63 74 3d 3d  ==0 && pSelect==
242e9 30 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  0) || db->malloc
242ea 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
242eb 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20  turn;.  }.  p = 
242ec 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
242ed 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  e;.  if( p==0 ) 
242ee 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72  return;..  asser
242ef 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  t( !db->init.bus
242f0 79 20 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b  y || !pSelect );
242f1 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ..  iDb = sqlite
242f2 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
242f3 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  b, p->pSchema);.
242f4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
242f5 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20  OMIT_CHECK.  /* 
242f6 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e  Resolve names in
242f7 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74   all CHECK const
242f8 72 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e  raint expression
242f9 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  s..  */.  if( p-
242fa 3e 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53  >pCheck ){.    S
242fb 72 63 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20  rcList sSrc;    
242fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
242fd 2a 20 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66  * Fake SrcList f
242fe 6f 72 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  or pParse->pNewT
242ff 61 62 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65  able */.    Name
24300 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
24301 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
24302 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ame context for 
24303 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
24304 65 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74  e */..    memset
24305 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
24306 28 73 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73  (sNC));.    mems
24307 65 74 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a  et(&sSrc, 0, siz
24308 65 6f 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20  eof(sSrc));.    
24309 73 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20  sSrc.nSrc = 1;. 
2430a 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61     sSrc.a[0].zNa
2430b 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20  me = p->zName;. 
2430c 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61     sSrc.a[0].pTa
2430d 62 20 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e  b = p;.    sSrc.
2430e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d  a[0].iCursor = -
2430f 31 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73  1;.    sNC.pPars
24310 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20  e = pParse;.    
24311 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26  sNC.pSrcList = &
24312 73 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73  sSrc;.    sNC.is
24313 43 68 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69  Check = 1;.    i
24314 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
24315 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
24316 20 70 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20   p->pCheck) ){. 
24317 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
24318 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   }.  }.#endif /*
24319 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2431a 5f 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a  _OMIT_CHECK) */.
2431b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d  .  /* If the db-
2431c 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20  >init.busy is 1 
2431d 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20  it means we are 
2431e 72 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20  reading the SQL 
2431f 6f 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71  off the.  ** "sq
24320 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20  lite_master" or 
24321 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73  "sqlite_temp_mas
24322 74 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68  ter" table on th
24323 65 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20  e disk..  ** So 
24324 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20  do not write to 
24325 74 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20  the disk again. 
24326 20 45 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f   Extract the roo
24327 74 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20  t page number.  
24328 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
24329 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e   from the db->in
2432a 69 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64  it.newTnum field
2432b 2e 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d  .  (The page num
2432c 62 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20  ber.  ** should 
2432d 68 61 76 65 20 62 65 65 6e 20 70 75 74 20 74 68  have been put th
2432e 65 72 65 20 62 79 20 74 68 65 20 73 71 6c 69 74  ere by the sqlit
2432f 65 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e  eOpenCb routine.
24330 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ).  */.  if( db-
24331 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
24332 20 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e    p->tnum = db->
24333 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20  init.newTnum;.  
24334 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69  }..  /* If not i
24335 6e 69 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65  nitializing, the
24336 6e 20 63 72 65 61 74 65 20 61 20 72 65 63 6f 72  n create a recor
24337 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61  d for the new ta
24338 62 6c 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ble.  ** in the 
24339 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
2433a 62 6c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ble of the datab
2433b 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ase..  **.  ** I
2433c 66 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50  f this is a TEMP
2433d 4f 52 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69  ORARY table, wri
2433e 74 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74  te the entry int
2433f 6f 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a  o the auxiliary.
24340 20 20 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61    ** file instea
24341 64 20 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61  d of into the ma
24342 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
24343 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  ..  */.  if( !db
24344 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
24345 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64     int n;.    Vd
24346 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20  be *v;.    char 
24347 2a 7a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76  *zType;    /* "v
24348 69 65 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20  iew" or "table" 
24349 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79  */.    char *zTy
2434a 70 65 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22  pe2;   /* "VIEW"
2434b 20 6f 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20   or "TABLE" */. 
2434c 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20     char *zStmt; 
2434d 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68     /* Text of th
2434e 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f  e CREATE TABLE o
2434f 72 20 43 52 45 41 54 45 20 56 49 45 57 20 73 74  r CREATE VIEW st
24350 61 74 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20  atement */..    
24351 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
24352 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
24353 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20  if( NEVER(v==0) 
24354 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 73  ) return;..    s
24355 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
24356 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 29  (v, OP_Close, 0)
24357 3b 0a 0a 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a  ;..    /* .    *
24358 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 7a 54 79  * Initialize zTy
24359 70 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 76  pe for the new v
2435a 69 65 77 20 6f 72 20 74 61 62 6c 65 2e 0a 20 20  iew or table..  
2435b 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e    */.    if( p->
2435c 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20  pSelect==0 ){.  
2435d 20 20 20 20 2f 2a 20 41 20 72 65 67 75 6c 61 72      /* A regular
2435e 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20   table */.      
2435f 7a 54 79 70 65 20 3d 20 22 74 61 62 6c 65 22 3b  zType = "table";
24360 0a 20 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20  .      zType2 = 
24361 22 54 41 42 4c 45 22 3b 0a 23 69 66 6e 64 65 66  "TABLE";.#ifndef
24362 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
24363 57 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  W.    }else{.   
24364 20 20 20 2f 2a 20 41 20 76 69 65 77 20 2a 2f 0a     /* A view */.
24365 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 76        zType = "v
24366 69 65 77 22 3b 0a 20 20 20 20 20 20 7a 54 79 70  iew";.      zTyp
24367 65 32 20 3d 20 22 56 49 45 57 22 3b 0a 23 65 6e  e2 = "VIEW";.#en
24368 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  dif.    }..    /
24369 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 43  * If this is a C
2436a 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 20 41  REATE TABLE xx A
2436b 53 20 53 45 4c 45 43 54 20 2e 2e 2e 2c 20 65 78  S SELECT ..., ex
2436c 65 63 75 74 65 20 74 68 65 20 53 45 4c 45 43 54  ecute the SELECT
2436d 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  .    ** statemen
2436e 74 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68  t to populate th
2436f 65 20 6e 65 77 20 74 61 62 6c 65 2e 20 54 68 65  e new table. The
24370 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
24371 72 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a  r for the.    **
24372 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20 69 6e   new table is in
24373 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65   register pParse
24374 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a  ->regRoot..    *
24375 2a 0a 20 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68  *.    ** Once th
24376 65 20 53 45 4c 45 43 54 20 68 61 73 20 62 65 65  e SELECT has bee
24377 6e 20 63 6f 64 65 64 20 62 79 20 73 71 6c 69 74  n coded by sqlit
24378 65 33 53 65 6c 65 63 74 28 29 2c 20 69 74 20 69  e3Select(), it i
24379 73 20 69 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75  s in a.    ** su
2437a 69 74 61 62 6c 65 20 73 74 61 74 65 20 74 6f 20  itable state to 
2437b 71 75 65 72 79 20 66 6f 72 20 74 68 65 20 63 6f  query for the co
2437c 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74  lumn names and t
2437d 79 70 65 73 20 74 6f 20 62 65 20 75 73 65 64 0a  ypes to be used.
2437e 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 6e 65      ** by the ne
2437f 77 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a  w table..    **.
24380 20 20 20 20 2a 2a 20 41 20 73 68 61 72 65 64 2d      ** A shared-
24381 63 61 63 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b  cache write-lock
24382 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
24383 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65   to write to the
24384 20 6e 65 77 20 74 61 62 6c 65 2c 0a 20 20 20 20   new table,.    
24385 2a 2a 20 61 73 20 61 20 73 63 68 65 6d 61 2d 6c  ** as a schema-l
24386 6f 63 6b 20 6d 75 73 74 20 68 61 76 65 20 61 6c  ock must have al
24387 72 65 61 64 79 20 62 65 65 6e 20 6f 62 74 61 69  ready been obtai
24388 6e 65 64 20 74 6f 20 63 72 65 61 74 65 20 69 74  ned to create it
24389 2e 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61  . Since.    ** a
2438a 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63   schema-lock exc
2438b 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20  ludes all other 
2438c 64 61 74 61 62 61 73 65 20 75 73 65 72 73 2c 20  database users, 
2438d 74 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77  the write-lock w
2438e 6f 75 6c 64 0a 20 20 20 20 2a 2a 20 62 65 20 72  ould.    ** be r
2438f 65 64 75 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f  edundant..    */
24390 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74  .    if( pSelect
24391 20 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74   ){.      Select
24392 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20  Dest dest;.     
24393 20 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b   Table *pSelTab;
24394 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70  ..      assert(p
24395 50 61 72 73 65 2d 3e 6e 54 61 62 3d 3d 31 29 3b  Parse->nTab==1);
24396 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
24397 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f  beAddOp3(v, OP_O
24398 70 65 6e 57 72 69 74 65 2c 20 31 2c 20 70 50 61  penWrite, 1, pPa
24399 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44  rse->regRoot, iD
2439a 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
2439b 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2439c 20 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73   1);.      pPars
2439d 65 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20  e->nTab = 2;.   
2439e 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2439f 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20  DestInit(&dest, 
243a0 53 52 54 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20  SRT_Table, 1);. 
243a1 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
243a2 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ct(pParse, pSele
243a3 63 74 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20  ct, &dest);.    
243a4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
243a5 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
243a6 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   1);.      if( p
243a7 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29  Parse->nErr==0 )
243a8 7b 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61  {.        pSelTa
243a9 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c  b = sqlite3Resul
243aa 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61  tSetOfSelect(pPa
243ab 72 73 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20  rse, pSelect);. 
243ac 20 20 20 20 20 20 20 69 66 28 20 70 53 65 6c 54         if( pSelT
243ad 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ab==0 ) return;.
243ae 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
243af 70 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20  p->aCol==0 );.  
243b0 20 20 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20        p->nCol = 
243b1 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20  pSelTab->nCol;. 
243b2 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d         p->aCol =
243b3 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a   pSelTab->aCol;.
243b4 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d          pSelTab-
243b5 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20  >nCol = 0;.     
243b6 20 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c     pSelTab->aCol
243b7 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71   = 0;.        sq
243b8 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
243b9 28 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20  (pSelTab);.     
243ba 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
243bb 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d   Compute the com
243bc 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
243bd 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d 65  e CREATE stateme
243be 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53  nt */.    if( pS
243bf 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 7a  elect ){.      z
243c0 53 74 6d 74 20 3d 20 63 72 65 61 74 65 54 61 62  Stmt = createTab
243c1 6c 65 53 74 6d 74 28 64 62 2c 20 70 29 3b 0a 20  leStmt(db, p);. 
243c2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
243c3 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e  n = (int)(pEnd->
243c4 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  z - pParse->sNam
243c5 65 54 6f 6b 65 6e 2e 7a 29 20 2b 20 31 3b 0a 20  eToken.z) + 1;. 
243c6 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c       zStmt = sql
243c7 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
243c8 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41  .          "CREA
243c9 54 45 20 25 73 20 25 2e 2a 73 22 2c 20 7a 54 79  TE %s %.*s", zTy
243ca 70 65 32 2c 20 6e 2c 20 70 50 61 72 73 65 2d 3e  pe2, n, pParse->
243cb 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20  sNameToken.z.   
243cc 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20     );.    }..   
243cd 20 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74   /* A slot for t
243ce 68 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c  he record has al
243cf 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63  ready been alloc
243d0 61 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20 20  ated in the .   
243d1 20 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45   ** SQLITE_MASTE
243d2 52 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73  R table.  We jus
243d3 74 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65  t need to update
243d4 20 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20   that slot with 
243d5 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 69  all.    ** the i
243d6 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76 65  nformation we've
243d7 20 63 6f 6c 6c 65 63 74 65 64 2e 0a 20 20 20 20   collected..    
243d8 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  */.    sqlite3Ne
243d9 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
243da 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20  ,.      "UPDATE 
243db 25 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20  %Q.%s ".        
243dc 20 22 53 45 54 20 74 79 70 65 3d 27 25 73 27 2c   "SET type='%s',
243dd 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61   name=%Q, tbl_na
243de 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d  me=%Q, rootpage=
243df 23 25 64 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20  #%d, sql=%Q ".  
243e0 20 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69       "WHERE rowi
243e1 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62  d=#%d",.      db
243e2 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
243e3 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
243e4 44 62 29 2c 0a 20 20 20 20 20 20 7a 54 79 70 65  Db),.      zType
243e5 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65  ,.      p->zName
243e6 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65  ,.      p->zName
243e7 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ,.      pParse->
243e8 72 65 67 52 6f 6f 74 2c 0a 20 20 20 20 20 20 7a  regRoot,.      z
243e9 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61 72  Stmt,.      pPar
243ea 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20 20  se->regRowid.   
243eb 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   );.    sqlite3D
243ec 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29  bFree(db, zStmt)
243ed 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
243ee 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
243ef 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66  , iDb);..#ifndef
243f0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
243f1 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f  OINCREMENT.    /
243f2 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
243f3 66 20 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65  f we need to cre
243f4 61 74 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65  ate an sqlite_se
243f5 71 75 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72  quence table for
243f6 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20  .    ** keeping 
243f7 74 72 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63  track of autoinc
243f8 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20  rement keys..   
243f9 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 74   */.    if( p->t
243fa 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
243fb 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20  oincrement ){.  
243fc 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64      Db *pDb = &d
243fd 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20  b->aDb[iDb];.   
243fe 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68     if( pDb->pSch
243ff 65 6d 61 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20  ema->pSeqTab==0 
24400 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
24401 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
24402 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
24403 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25 51  "CREATE TABLE %Q
24404 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  .sqlite_sequence
24405 28 6e 61 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20  (name,seq)",.   
24406 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d         pDb->zNam
24407 65 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20  e.        );.   
24408 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
24409 66 0a 0a 20 20 20 20 2f 2a 20 52 65 70 61 72 73  f..    /* Repars
2440a 65 20 65 76 65 72 79 74 68 69 6e 67 20 74 6f 20  e everything to 
2440b 75 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72  update our inter
2440c 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75  nal data structu
2440d 72 65 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  res */.    sqlit
2440e 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2440f 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20  OP_ParseSchema, 
24410 69 44 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  iDb, 0, 0,.     
24411 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74     sqlite3MPrint
24412 66 28 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d  f(db, "tbl_name=
24413 27 25 71 27 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c  '%q'",p->zName),
24414 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20   P4_DYNAMIC);.  
24415 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65  }...  /* Add the
24416 20 74 61 62 6c 65 20 74 6f 20 74 68 65 20 69 6e   table to the in
24417 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e  -memory represen
24418 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61  tation of the da
24419 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  tabase..  */.  i
2441a 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
2441b 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
2441c 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d 61 20  Old;.    Schema 
2441d 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e 70 53  *pSchema = p->pS
2441e 63 68 65 6d 61 3b 0a 20 20 20 20 70 4f 6c 64 20  chema;.    pOld 
2441f 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
24420 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62  ert(&pSchema->tb
24421 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  lHash, p->zName,
24422 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
24423 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
24424 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d  lite3Strlen30(p-
24425 3e 7a 4e 61 6d 65 29 2c 70 29 3b 0a 20 20 20 20  >zName),p);.    
24426 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20  if( pOld ){.    
24427 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c    assert( p==pOl
24428 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20  d );  /* Malloc 
24429 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64  must have failed
2442a 20 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65   inside HashInse
2442b 72 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62  rt() */.      db
2442c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2442d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
2442e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
2442f 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20  se->pNewTable = 
24430 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 54 61 62 6c  0;.    db->nTabl
24431 65 2b 2b 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61  e++;.    db->fla
24432 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
24433 65 72 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66  ernChanges;..#if
24434 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24435 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 20  _ALTERTABLE.    
24436 69 66 28 20 21 70 2d 3e 70 53 65 6c 65 63 74 20  if( !p->pSelect 
24437 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
24438 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f  har *zName = (co
24439 6e 73 74 20 63 68 61 72 20 2a 29 70 50 61 72 73  nst char *)pPars
2443a 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b  e->sNameToken.z;
2443b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65  .      int nName
2443c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2443d 21 70 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e  !pSelect && pCon
2443e 73 20 26 26 20 70 45 6e 64 20 29 3b 0a 20 20 20  s && pEnd );.   
2443f 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d     if( pCons->z=
24440 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  =0 ){.        pC
24441 6f 6e 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20  ons = pEnd;.    
24442 20 20 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20    }.      nName 
24443 3d 20 28 69 6e 74 29 28 28 63 6f 6e 73 74 20 63  = (int)((const c
24444 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d  har *)pCons->z -
24445 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70   zName);.      p
24446 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d  ->addColOffset =
24447 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66   13 + sqlite3Utf
24448 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20  8CharLen(zName, 
24449 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65  nName);.    }.#e
2444a 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  ndif.  }.}..#ifn
2444b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2444c 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  VIEW./*.** The p
2444d 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
2444e 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65   routine in orde
2444f 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  r to create a ne
24450 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45  w VIEW.*/.SQLITE
24451 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
24452 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28  lite3CreateView(
24453 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
24454 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72  ,     /* The par
24455 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
24456 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c    Token *pBegin,
24457 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41       /* The CREA
24458 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65  TE token that be
24459 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65  gins the stateme
2445a 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
2445b 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68  Name1,     /* Th
2445c 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c  e token that hol
2445d 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ds the name of t
2445e 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b  he view */.  Tok
2445f 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20  en *pName2,     
24460 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61  /* The token tha
24461 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  t holds the name
24462 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a   of the view */.
24463 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
24464 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54  t,   /* A SELECT
24465 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
24466 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20  will become the 
24467 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e  new view */.  in
24468 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20  t isTemp,       
24469 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54   /* TRUE for a T
2446a 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f  EMPORARY view */
2446b 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20  .  int noErr    
2446c 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73        /* Suppres
2446d 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  s error messages
2446e 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 79   if VIEW already
2446f 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20   exists */.){.  
24470 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20  Table *p;.  int 
24471 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  n;.  const char 
24472 2a 7a 3b 0a 20 20 54 6f 6b 65 6e 20 73 45 6e 64  *z;.  Token sEnd
24473 3b 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78  ;.  DbFixer sFix
24474 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  ;.  Token *pName
24475 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73  ;.  int iDb;.  s
24476 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
24477 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20  rse->db;..  if( 
24478 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30 20 29  pParse->nVar>0 )
24479 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2447a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70  orMsg(pParse, "p
2447b 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 6f  arameters are no
2447c 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76 69 65  t allowed in vie
2447d 77 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ws");.    sqlite
2447e 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2447f 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  , pSelect);.    
24480 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71  return;.  }.  sq
24481 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
24482 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
24483 70 4e 61 6d 65 32 2c 20 69 73 54 65 6d 70 2c 20  pName2, isTemp, 
24484 31 2c 20 30 2c 20 6e 6f 45 72 72 29 3b 0a 20 20  1, 0, noErr);.  
24485 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
24486 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d  Table;.  if( p==
24487 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
24488 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
24489 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72   pSelect);.    r
2448a 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73  eturn;.  }.  ass
2448b 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72  ert( pParse->nEr
2448c 72 3d 3d 30 20 29 3b 20 2f 2a 20 49 66 20 73 71  r==0 ); /* If sq
2448d 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 20  lite3StartTable 
2448e 72 65 74 75 72 6e 20 6e 6f 6e 2d 4e 55 4c 4c 20  return non-NULL 
2448f 74 68 65 6e 0a 20 20 20 20 20 20 20 20 20 20 20  then.           
24490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24491 20 20 2a 2a 20 74 68 65 72 65 20 63 6f 75 6c 64    ** there could
24492 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 61   not have been a
24493 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c  n error */.  sql
24494 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
24495 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
24496 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b  pName2, &pName);
24497 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
24498 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
24499 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  , p->pSchema);. 
2449a 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49   if( sqlite3FixI
2449b 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73  nit(&sFix, pPars
2449c 65 2c 20 69 44 62 2c 20 22 76 69 65 77 22 2c 20  e, iDb, "view", 
2449d 70 4e 61 6d 65 29 0a 20 20 20 20 26 26 20 73 71  pName).    && sq
2449e 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 26  lite3FixSelect(&
2449f 73 46 69 78 2c 20 70 53 65 6c 65 63 74 29 0a 20  sFix, pSelect). 
244a0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
244a1 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
244a2 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65  pSelect);.    re
244a3 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  turn;.  }..  /* 
244a4 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  Make a copy of t
244a5 68 65 20 65 6e 74 69 72 65 20 53 45 4c 45 43 54  he entire SELECT
244a6 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
244a7 64 65 66 69 6e 65 73 20 74 68 65 20 76 69 65 77  defines the view
244a8 2e 0a 20 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c  ..  ** This will
244a9 20 66 6f 72 63 65 20 61 6c 6c 20 74 68 65 20 45   force all the E
244aa 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75  xpr.token.z valu
244ab 65 73 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63  es to be dynamic
244ac 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61  ally.  ** alloca
244ad 74 65 64 20 72 61 74 68 65 72 20 74 68 61 6e 20  ted rather than 
244ae 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69 6e 70  point to the inp
244af 75 74 20 73 74 72 69 6e 67 20 2d 20 77 68 69 63  ut string - whic
244b0 68 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 2a  h means that.  *
244b1 2a 20 74 68 65 79 20 77 69 6c 6c 20 70 65 72 73  * they will pers
244b2 69 73 74 20 61 66 74 65 72 20 74 68 65 20 63 75  ist after the cu
244b3 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  rrent sqlite3_ex
244b4 65 63 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e  ec() call return
244b5 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 53 65  s..  */.  p->pSe
244b6 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
244b7 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 65 6c  lectDup(db, pSel
244b8 65 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44  ect, EXPRDUP_RED
244b9 55 43 45 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  UCE);.  sqlite3S
244ba 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
244bb 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28 20  pSelect);.  if( 
244bc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
244bd 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
244be 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 69    }.  if( !db->i
244bf 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
244c0 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
244c1 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
244c2 2c 20 70 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , p);.  }..  /* 
244c3 4c 6f 63 61 74 65 20 74 68 65 20 65 6e 64 20 6f  Locate the end o
244c4 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 45  f the CREATE VIE
244c5 57 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4d 61  W statement.  Ma
244c6 6b 65 20 73 45 6e 64 20 70 6f 69 6e 74 20 74 6f  ke sEnd point to
244c7 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 2e 0a 20  .  ** the end.. 
244c8 20 2a 2f 0a 20 20 73 45 6e 64 20 3d 20 70 50 61   */.  sEnd = pPa
244c9 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 3b  rse->sLastToken;
244ca 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 73 45  .  if( ALWAYS(sE
244cb 6e 64 2e 7a 5b 30 5d 21 3d 30 29 20 26 26 20 73  nd.z[0]!=0) && s
244cc 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b  End.z[0]!=';' ){
244cd 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73  .    sEnd.z += s
244ce 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e  End.n;.  }.  sEn
244cf 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 28  d.n = 0;.  n = (
244d0 69 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20 70 42  int)(sEnd.z - pB
244d1 65 67 69 6e 2d 3e 7a 29 3b 0a 20 20 7a 20 3d 20  egin->z);.  z = 
244d2 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69  pBegin->z;.  whi
244d3 6c 65 28 20 41 4c 57 41 59 53 28 6e 3e 30 29 20  le( ALWAYS(n>0) 
244d4 26 26 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  && sqlite3Isspac
244d5 65 28 7a 5b 6e 2d 31 5d 29 20 29 7b 20 6e 2d 2d  e(z[n-1]) ){ n--
244d6 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20 26  ; }.  sEnd.z = &
244d7 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 2e 6e  z[n-1];.  sEnd.n
244d8 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 65 20   = 1;..  /* Use 
244d9 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28  sqlite3EndTable(
244da 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 69 65  ) to add the vie
244db 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 45 5f  w to the SQLITE_
244dc 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a 2f 0a  MASTER table */.
244dd 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c    sqlite3EndTabl
244de 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 73 45  e(pParse, 0, &sE
244df 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  nd, 0);.  return
244e0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
244e1 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a  LITE_OMIT_VIEW *
244e2 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
244e3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
244e4 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
244e5 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
244e6 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 68  LTABLE)./*.** Th
244e7 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
244e8 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 61 6c  e pTable is real
244e9 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c 6c  ly a VIEW.  Fill
244ea 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66   in the names of
244eb 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20  .** the columns 
244ec 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e 20 74  of the view in t
244ed 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 63 74  he pTable struct
244ee 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ure.  Return the
244ef 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65 72   number.** of er
244f0 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72 72  rors.  If an err
244f1 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76 65  or is seen leave
244f2 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
244f3 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72  e in pParse->zEr
244f4 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rMsg..*/.SQLITE_
244f5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
244f6 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
244f7 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50 61  Names(Parse *pPa
244f8 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
244f9 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 53  le){.  Table *pS
244fa 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66 61  elTab;   /* A fa
244fb 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68  ke table from wh
244fc 69 63 68 20 77 65 20 67 65 74 20 74 68 65 20 72  ich we get the r
244fd 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 53  esult set */.  S
244fe 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 20  elect *pSel;    
244ff 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65 20   /* Copy of the 
24500 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70 6c  SELECT that impl
24501 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 20  ements the view 
24502 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20  */.  int nErr = 
24503 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  0;     /* Number
24504 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75   of errors encou
24505 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ntered */.  int 
24506 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
24507 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 6f 6c   Temporarily hol
24508 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ds the number of
24509 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 6e 65   cursors assigne
2450a 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  d */.  sqlite3 *
2450b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2450c 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2450d 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d 61 6c  nnection for mal
2450e 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  loc errors */.  
2450f 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69  int (*xAuth)(voi
24510 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
24511 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
24512 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
24513 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73 73 65   char*);..  asse
24514 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 0a 23  rt( pTable );..#
24515 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24516 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
24517 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 74 61    if( sqlite3Vta
24518 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50 61  bCallConnect(pPa
24519 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29 7b 0a  rse, pTable) ){.
2451a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2451b 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69  E_ERROR;.  }.  i
2451c 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2451d 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ble) ) return 0;
2451e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
2451f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
24520 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 69 76  W.  /* A positiv
24521 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68 65  e nCol means the
24522 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 66   columns names f
24523 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 72 65  or this view are
24524 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6b 6e  .  ** already kn
24525 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  own..  */.  if( 
24526 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 20 29  pTable->nCol>0 )
24527 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a   return 0;..  /*
24528 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c   A negative nCol
24529 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 6d 61   is a special ma
2452a 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 68 61  rker meaning tha
2452b 74 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74  t we are current
2452c 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74  ly.  ** trying t
2452d 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 63 6f  o compute the co
2452e 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 66 20  lumn names.  If 
2452f 77 65 20 65 6e 74 65 72 20 74 68 69 73 20 72 6f  we enter this ro
24530 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a 2a 20  utine with.  ** 
24531 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c 2c  a negative nCol,
24532 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 6f 72   it means two or
24533 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f 72 6d   more views form
24534 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 74 68   a loop, like th
24535 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  is:.  **.  **   
24536 20 20 43 52 45 41 54 45 20 56 49 45 57 20 6f 6e    CREATE VIEW on
24537 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52  e AS SELECT * FR
24538 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 20 20  OM two;.  **    
24539 20 43 52 45 41 54 45 20 56 49 45 57 20 74 77 6f   CREATE VIEW two
2453a 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f   AS SELECT * FRO
2453b 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a  M one;.  **.  **
2453c 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 65 20 65   Actually, the e
2453d 72 72 6f 72 20 61 62 6f 76 65 20 69 73 20 6e 6f  rror above is no
2453e 77 20 63 61 75 67 68 74 20 70 72 69 6f 72 20 74  w caught prior t
2453f 6f 20 72 65 61 63 68 69 6e 67 20 74 68 69 73 20  o reaching this 
24540 70 6f 69 6e 74 2e 0a 20 20 2a 2a 20 42 75 74 20  point..  ** But 
24541 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65  the following te
24542 73 74 20 69 73 20 73 74 69 6c 6c 20 69 6d 70 6f  st is still impo
24543 72 74 61 6e 74 20 61 73 20 69 74 20 64 6f 65 73  rtant as it does
24544 20 63 6f 6d 65 20 75 70 0a 20 20 2a 2a 20 69 6e   come up.  ** in
24545 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
24546 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20 43    ** .  **     C
24547 52 45 41 54 45 20 54 41 42 4c 45 20 6d 61 69 6e  REATE TABLE main
24548 2e 65 78 31 28 61 29 3b 0a 20 20 2a 2a 20 20 20  .ex1(a);.  **   
24549 20 20 43 52 45 41 54 45 20 54 45 4d 50 20 56 49    CREATE TEMP VI
2454a 45 57 20 65 78 31 20 41 53 20 53 45 4c 45 43 54  EW ex1 AS SELECT
2454b 20 61 20 46 52 4f 4d 20 65 78 31 3b 0a 20 20 2a   a FROM ex1;.  *
2454c 2a 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  *     SELECT * F
2454d 52 4f 4d 20 74 65 6d 70 2e 65 78 31 3b 0a 20 20  ROM temp.ex1;.  
2454e 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d  */.  if( pTable-
2454f 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 73  >nCol<0 ){.    s
24550 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
24551 50 61 72 73 65 2c 20 22 76 69 65 77 20 25 73 20  Parse, "view %s 
24552 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20 64 65  is circularly de
24553 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 2d 3e  fined", pTable->
24554 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  zName);.    retu
24555 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65  rn 1;.  }.  asse
24556 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  rt( pTable->nCol
24557 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  >=0 );..  /* If 
24558 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c  we get this far,
24559 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e 65 65   it means we nee
2455a 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65  d to compute the
2455b 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a 20 20   table names..  
2455c 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
2455d 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
2455e 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63  ResultSetOfSelec
2455f 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e 64 20  t() will expand 
24560 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 6c 65  any.  ** "*" ele
24561 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72 65 73  ments in the res
24562 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68 65 20  ults set of the 
24563 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 61 73  view and will as
24564 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 20 2a  sign cursors.  *
24565 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e 74  * to the element
24566 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  s of the FROM cl
24567 61 75 73 65 2e 20 20 42 75 74 20 77 65 20 64 6f  ause.  But we do
24568 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 65 20   not want these 
24569 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20  changes.  ** to 
2456a 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20 53  be permanent.  S
2456b 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f  o the computatio
2456c 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20 63  n is done on a c
2456d 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43  opy of the SELEC
2456e 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74  T.  ** statement
2456f 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
24570 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 20 61  e view..  */.  a
24571 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70  ssert( pTable->p
24572 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 65 6c  Select );.  pSel
24573 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
24574 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  Dup(db, pTable->
24575 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69  pSelect, 0);.  i
24576 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 75  f( pSel ){.    u
24577 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  8 enableLookasid
24578 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  e = db->lookasid
24579 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20 20  e.bEnabled;.    
2457a 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  n = pParse->nTab
2457b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63  ;.    sqlite3Src
2457c 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72  ListAssignCursor
2457d 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e  s(pParse, pSel->
2457e 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c  pSrc);.    pTabl
2457f 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20  e->nCol = -1;.  
24580 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
24581 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23 69  bEnabled = 0;.#i
24582 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24583 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
24584 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e      xAuth = db->
24585 78 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78  xAuth;.    db->x
24586 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53  Auth = 0;.    pS
24587 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52  elTab = sqlite3R
24588 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74  esultSetOfSelect
24589 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a  (pParse, pSel);.
2458a 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
2458b 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20  xAuth;.#else.   
2458c 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74   pSelTab = sqlit
2458d 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
2458e 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ect(pParse, pSel
2458f 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 62  );.#endif.    db
24590 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
24591 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f  bled = enableLoo
24592 6b 61 73 69 64 65 3b 0a 20 20 20 20 70 50 61 72  kaside;.    pPar
24593 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 20  se->nTab = n;.  
24594 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29 7b    if( pSelTab ){
24595 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
24596 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29  Table->aCol==0 )
24597 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e  ;.      pTable->
24598 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  nCol = pSelTab->
24599 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 62  nCol;.      pTab
2459a 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54  le->aCol = pSelT
2459b 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20  ab->aCol;.      
2459c 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20  pSelTab->nCol = 
2459d 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62  0;.      pSelTab
2459e 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->aCol = 0;.    
2459f 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
245a0 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a 20  able(pSelTab);. 
245a1 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63       pTable->pSc
245a2 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 44  hema->flags |= D
245a3 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0a  B_UnresetViews;.
245a4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
245a5 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
245a6 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b  0;.      nErr++;
245a7 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
245a8 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
245a9 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65 6c  b, pSel);.  } el
245aa 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b  se {.    nErr++;
245ab 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
245ac 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20  QLITE_OMIT_VIEW 
245ad 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72  */.  return nErr
245ae 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;  .}.#endif /* 
245af 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
245b0 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
245b1 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
245b2 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
245b3 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
245b4 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a  ITE_OMIT_VIEW./*
245b5 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f  .** Clear the co
245b6 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20  lumn names from 
245b7 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61  every VIEW in da
245b8 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73  tabase idx..*/.s
245b9 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
245ba 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71  eViewResetAll(sq
245bb 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
245bc 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  dx){.  HashElem 
245bd 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73  *i;.  if( !DbHas
245be 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78  Property(db, idx
245bf 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  , DB_UnresetView
245c0 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  s) ) return;.  f
245c1 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46  or(i=sqliteHashF
245c2 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64  irst(&db->aDb[id
245c3 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  x].pSchema->tblH
245c4 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65  ash); i;i=sqlite
245c5 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
245c6 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
245c7 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69  sqliteHashData(i
245c8 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  );.    if( pTab-
245c9 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
245ca 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c    sqliteResetCol
245cb 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b 0a  umnNames(pTab);.
245cc 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c      }.  }.  DbCl
245cd 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20  earProperty(db, 
245ce 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56  idx, DB_UnresetV
245cf 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  iews);.}.#else.#
245d0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69   define sqliteVi
245d1 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a  ewResetAll(A,B).
245d2 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
245d3 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f  _OMIT_VIEW */../
245d4 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
245d5 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  on is called by 
245d6 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75  the VDBE to adju
245d7 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  st the internal 
245d8 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62  schema.** used b
245d9 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68  y SQLite when th
245da 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f  e btree layer mo
245db 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74  ves a table root
245dc 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f   page. The.** ro
245dd 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62  ot-page of a tab
245de 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64  le or index in d
245df 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20  atabase iDb has 
245e0 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72  changed from iFr
245e1 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a  om.** to iTo..**
245e2 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38  .** Ticket #1728
245e3 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61  :  The symbol ta
245e4 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20  ble might still 
245e5 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74  contain informat
245e6 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73  ion.** on tables
245e7 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20   and/or indices 
245e8 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f  that are the pro
245e9 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65  cess of being de
245ea 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75  leted..** If you
245eb 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e   are unlucky, on
245ec 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74  e of those delet
245ed 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61  ed indices or ta
245ee 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61  bles might.** ha
245ef 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74  ve the same root
245f0 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74  page number as t
245f1 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72  he real table or
245f2 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a   index that is.*
245f3 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20  * being moved.  
245f4 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f  So we cannot sto
245f5 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65  p searching afte
245f6 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63  r the first matc
245f7 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68  h .** because th
245f8 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69  e first match mi
245f9 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f  ght be for one o
245fa 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e  f the deleted in
245fb 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c  dices.** or tabl
245fc 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74  es and not the t
245fd 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20  able/index that 
245fe 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e  is actually bein
245ff 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d  g moved..** We m
24600 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f  ust continue loo
24601 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74  ping until all t
24602 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65  ables and indice
24603 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61  s with.** rootpa
24604 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62  ge==iFrom have b
24605 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f  een converted to
24606 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65   have a rootpage
24607 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72   of iTo.** in or
24608 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69  der to be certai
24609 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68  n that we got th
2460a 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a  e right one..*/.
2460b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2460c 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 53  MIT_AUTOVACUUM.S
2460d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2460e 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61  id sqlite3RootPa
2460f 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 2c  geMoved(Db *pDb,
24610 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20   int iFrom, int 
24611 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  iTo){.  HashElem
24612 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20   *pElem;.  Hash 
24613 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 68  *pHash;..  pHash
24614 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61   = &pDb->pSchema
24615 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72  ->tblHash;.  for
24616 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73  (pElem=sqliteHas
24617 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70  hFirst(pHash); p
24618 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69  Elem; pElem=sqli
24619 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d  teHashNext(pElem
2461a 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  )){.    Table *p
2461b 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Tab = sqliteHash
2461c 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20  Data(pElem);.   
2461d 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d   if( pTab->tnum=
2461e 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20  =iFrom ){.      
2461f 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f  pTab->tnum = iTo
24620 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48  ;.    }.  }.  pH
24621 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68  ash = &pDb->pSch
24622 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20  ema->idxHash;.  
24623 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65  for(pElem=sqlite
24624 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29  HashFirst(pHash)
24625 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73  ; pElem; pElem=s
24626 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45  qliteHashNext(pE
24627 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78  lem)){.    Index
24628 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48   *pIdx = sqliteH
24629 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a  ashData(pElem);.
2462a 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e      if( pIdx->tn
2462b 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  um==iFrom ){.   
2462c 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20     pIdx->tnum = 
2462d 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  iTo;.    }.  }.}
2462e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
2462f 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61  rite code to era
24630 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  se the table wit
24631 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62  h root-page iTab
24632 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65  le from database
24633 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72   iDb..** Also wr
24634 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69  ite code to modi
24635 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  fy the sqlite_ma
24636 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69  ster table and i
24637 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a  nternal schema.*
24638 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65  * if a root-page
24639 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c   of another tabl
2463a 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68  e is moved by th
2463b 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68  e btree-layer wh
2463c 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20  ilst.** erasing 
2463d 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e  iTable (this can
2463e 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20   happen with an 
2463f 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
24640 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69  base)..*/ .stati
24641 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f  c void destroyRo
24642 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50  otPage(Parse *pP
24643 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65  arse, int iTable
24644 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64  , int iDb){.  Vd
24645 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
24646 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
24647 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
24648 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
24649 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  rse);.  sqlite3V
2464a 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2464b 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c  Destroy, iTable,
2464c 20 72 31 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c   r1, iDb);.  sql
2464d 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
2464e 72 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  rse);.#ifndef SQ
2464f 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
24650 43 55 55 4d 0a 20 20 2f 2a 20 4f 50 5f 44 65 73  CUUM.  /* OP_Des
24651 74 72 6f 79 20 73 74 6f 72 65 73 20 61 6e 20 69  troy stores an i
24652 6e 20 69 6e 74 65 67 65 72 20 72 31 2e 20 49 66  n integer r1. If
24653 20 74 68 69 73 20 69 6e 74 65 67 65 72 0a 20 20   this integer.  
24654 2a 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ** is non-zero, 
24655 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 72  then it is the r
24656 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
24657 6f 66 20 61 20 74 61 62 6c 65 20 6d 6f 76 65 64  of a table moved
24658 20 74 6f 0a 20 20 2a 2a 20 6c 6f 63 61 74 69 6f   to.  ** locatio
24659 6e 20 69 54 61 62 6c 65 2e 20 54 68 65 20 66 6f  n iTable. The fo
2465a 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6d 6f 64  llowing code mod
2465b 69 66 69 65 73 20 74 68 65 20 73 71 6c 69 74 65  ifies the sqlite
2465c 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 6f  _master table to
2465d 0a 20 20 2a 2a 20 72 65 66 6c 65 63 74 20 74 68  .  ** reflect th
2465e 69 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  is..  **.  ** Th
2465f 65 20 22 23 4e 4e 4e 22 20 69 6e 20 74 68 65 20  e "#NNN" in the 
24660 53 51 4c 20 69 73 20 61 20 73 70 65 63 69 61 6c  SQL is a special
24661 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 6d   constant that m
24662 65 61 6e 73 20 77 68 61 74 65 76 65 72 20 76 61  eans whatever va
24663 6c 75 65 0a 20 20 2a 2a 20 69 73 20 69 6e 20 72  lue.  ** is in r
24664 65 67 69 73 74 65 72 20 4e 4e 4e 2e 20 20 53 65  egister NNN.  Se
24665 65 20 67 72 61 6d 6d 61 72 20 72 75 6c 65 73 20  e grammar rules 
24666 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
24667 74 68 65 20 54 4b 5f 52 45 47 49 53 54 45 52 0a  the TK_REGISTER.
24668 20 20 2a 2a 20 74 6f 6b 65 6e 20 66 6f 72 20 61    ** token for a
24669 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2466a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71  ation..  */.  sq
2466b 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2466c 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22  (pParse, .     "
2466d 55 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54  UPDATE %Q.%s SET
2466e 20 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45   rootpage=%d WHE
2466f 52 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70  RE #%d AND rootp
24670 61 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70  age=#%d",.     p
24671 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69  Parse->db->aDb[i
24672 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d  Db].zName, SCHEM
24673 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54  A_TABLE(iDb), iT
24674 61 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23  able, r1, r1);.#
24675 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52  endif.  sqlite3R
24676 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
24677 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a  arse, r1);.}../*
24678 0a 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20 63  .** Write VDBE c
24679 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74 61 62  ode to erase tab
2467a 6c 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20  le pTab and all 
2467b 61 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63  associated indic
2467c 65 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43  es on disk..** C
2467d 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20 74 68  ode to update th
2467e 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
2467f 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72  tables and inter
24680 6e 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e  nal schema defin
24681 69 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73  itions.** in cas
24682 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65  e a root-page be
24683 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68  longing to anoth
24684 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65  er table is move
24685 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20 6c  d by the btree l
24686 61 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20  ayer.** is also 
24687 61 64 64 65 64 20 28 74 68 69 73 20 63 61 6e 20  added (this can 
24688 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61  happen with an a
24689 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
2468a 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ase)..*/.static 
2468b 76 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c  void destroyTabl
2468c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2468d 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23   Table *pTab){.#
2468e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2468f 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49  T_AUTOVACUUM.  I
24690 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e  ndex *pIdx;.  in
24691 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
24692 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
24693 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
24694 53 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72  Schema);.  destr
24695 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
24696 65 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69  e, pTab->tnum, i
24697 44 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d  Db);.  for(pIdx=
24698 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
24699 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2469a 4e 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72  Next){.    destr
2469b 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
2469c 65 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69  e, pIdx->tnum, i
2469d 44 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  Db);.  }.#else. 
2469e 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62   /* If the datab
2469f 61 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d  ase may be auto-
246a0 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28  vacuum capable (
246a1 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  if SQLITE_OMIT_A
246a2 55 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69  UTOVACUUM.  ** i
246a3 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20  s not defined), 
246a4 74 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72  then it is impor
246a5 74 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f  tant to call OP_
246a6 44 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20  Destroy on the. 
246a7 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e   ** table and in
246a8 64 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69  dex root-pages i
246a9 6e 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e  n order, startin
246aa 67 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72  g with the numer
246ab 69 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72  ically .  ** lar
246ac 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e  gest root-page n
246ad 75 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61 72  umber. This guar
246ae 61 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65  antees that none
246af 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67   of the root-pag
246b0 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65  es.  ** to be de
246b1 73 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63  stroyed is reloc
246b2 61 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69  ated by an earli
246b3 65 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69  er OP_Destroy. i
246b4 2e 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20  .e. if the.  ** 
246b5 66 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63  following were c
246b6 6f 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  oded:.  **.  ** 
246b7 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20  OP_Destroy 4 0. 
246b8 20 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f   ** ....  ** OP_
246b9 44 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a  Destroy 5 0.  **
246ba 0a 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70  .  ** and root p
246bb 61 67 65 20 35 20 68 61 70 70 65 6e 65 64 20 74  age 5 happened t
246bc 6f 20 62 65 20 74 68 65 20 6c 61 72 67 65 73 74  o be the largest
246bd 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
246be 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61  r in the.  ** da
246bf 74 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f  tabase, then roo
246c0 74 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62  t page 5 would b
246c1 65 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20  e moved to page 
246c2 34 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22  4 by the .  ** "
246c3 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20  OP_Destroy 4 0" 
246c4 6f 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73  opcode. The subs
246c5 65 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72  equent "OP_Destr
246c6 6f 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69  oy 5 0" would hi
246c7 74 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69  t.  ** a free-li
246c8 73 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20  st page..  */.  
246c9 69 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d  int iTab = pTab-
246ca 3e 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65  >tnum;.  int iDe
246cb 73 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20  stroyed = 0;..  
246cc 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20  while( 1 ){.    
246cd 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
246ce 20 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20   int iLargest = 
246cf 30 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73  0;..    if( iDes
246d0 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61  troyed==0 || iTa
246d1 62 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a  b<iDestroyed ){.
246d2 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d        iLargest =
246d3 20 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20   iTab;.    }.   
246d4 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
246d5 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
246d6 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
246d7 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 20  .      int iIdx 
246d8 3d 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20  = pIdx->tnum;.  
246d9 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78      assert( pIdx
246da 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d  ->pSchema==pTab-
246db 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20  >pSchema );.    
246dc 20 20 69 66 28 20 28 69 44 65 73 74 72 6f 79 65    if( (iDestroye
246dd 64 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44  d==0 || (iIdx<iD
246de 65 73 74 72 6f 79 65 64 29 29 20 26 26 20 69 49  estroyed)) && iI
246df 64 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20  dx>iLargest ){. 
246e0 20 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20         iLargest 
246e1 3d 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a  = iIdx;.      }.
246e2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c      }.    if( iL
246e3 61 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20  argest==0 ){.   
246e4 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
246e5 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
246e6 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
246e7 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
246e8 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
246e9 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 73  hema);.      des
246ea 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61  troyRootPage(pPa
246eb 72 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69  rse, iLargest, i
246ec 44 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74  Db);.      iDest
246ed 72 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74  royed = iLargest
246ee 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
246ef 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  if.}../*.** This
246f0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
246f1 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72  ed to do the wor
246f2 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c  k of a DROP TABL
246f3 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  E statement..** 
246f4 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  pName is the nam
246f5 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
246f6 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f  o be dropped..*/
246f7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
246f8 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
246f9 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61  Table(Parse *pPa
246fa 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e  rse, SrcList *pN
246fb 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c  ame, int isView,
246fc 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54   int noErr){.  T
246fd 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64  able *pTab;.  Vd
246fe 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
246ff 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
24700 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20  b;.  int iDb;.. 
24701 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
24702 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
24703 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
24704 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  e;.  }.  assert(
24705 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30   pParse->nErr==0
24706 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e   );.  assert( pN
24707 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ame->nSrc==1 );.
24708 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
24709 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72  LocateTable(pPar
2470a 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20 20 20  se, isView, .   
2470b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2470c 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e           pName->
2470d 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d  a[0].zName, pNam
2470e 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73  e->a[0].zDatabas
2470f 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d  e);..  if( pTab=
24710 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 6f  =0 ){.    if( no
24711 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Err ){.      sql
24712 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70  ite3ErrorClear(p
24713 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Parse);.    }.  
24714 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
24715 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 44  _table;.  }.  iD
24716 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
24717 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
24718 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
24719 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
2471a 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
2471b 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73  .  /* If pTab is
2471c 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2471d 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74 43 6f  , call ViewGetCo
2471e 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f 20 65  lumnNames() to e
2471f 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20 69 73  nsure.  ** it is
24720 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20   initialized..  
24721 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  */.  if( IsVirtu
24722 61 6c 28 70 54 61 62 29 20 26 26 20 73 71 6c 69  al(pTab) && sqli
24723 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
24724 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
24725 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ab) ){.    goto 
24726 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
24727 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
24728 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
24729 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69  ZATION.  {.    i
2472a 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 6f 6e  nt code;.    con
2472b 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20  st char *zTab = 
2472c 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
2472d 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  );.    const cha
2472e 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  r *zDb = db->aDb
2472f 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
24730 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
24731 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  g2 = 0;.    if( 
24732 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
24733 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
24734 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c  DELETE, zTab, 0,
24735 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 67 6f   zDb)){.      go
24736 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
24737 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  le;.    }.    if
24738 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
24739 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
2473a 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a  DB && iDb==1 ){.
2473b 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
2473c 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
2473d 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73  VIEW;.      }els
2473e 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20  e{.        code 
2473f 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49  = SQLITE_DROP_VI
24740 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e  EW;.      }.#ifn
24741 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
24742 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
24743 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69 72   }else if( IsVir
24744 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
24745 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
24746 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0a 20  E_DROP_VTABLE;. 
24747 20 20 20 20 20 7a 41 72 67 32 20 3d 20 73 71 6c       zArg2 = sql
24748 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62  ite3GetVTable(db
24749 2c 20 70 54 61 62 29 2d 3e 70 4d 6f 64 2d 3e 7a  , pTab)->pMod->z
2474a 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20  Name;.#endif.   
2474b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
2474c 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
2474d 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20  & iDb==1 ){.    
2474e 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
2474f 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c  E_DROP_TEMP_TABL
24750 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  E;.      }else{.
24751 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
24752 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45  QLITE_DROP_TABLE
24753 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
24754 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
24755 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
24756 20 63 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61   code, pTab->zNa
24757 6d 65 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20  me, zArg2, zDb) 
24758 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
24759 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20  it_drop_table;. 
2475a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
2475b 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2475c 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c  arse, SQLITE_DEL
2475d 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ETE, pTab->zName
2475e 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  , 0, zDb) ){.   
2475f 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
24760 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20  p_table;.    }. 
24761 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
24762 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
24763 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71  pTab->zName, "sq
24764 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b  lite_", 7)==0 ){
24765 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
24766 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
24767 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62  ble %s may not b
24768 65 20 64 72 6f 70 70 65 64 22 2c 20 70 54 61 62  e dropped", pTab
24769 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
2476a 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
2476b 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  le;.  }..#ifndef
2476c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
2476d 57 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 44 52  W.  /* Ensure DR
2476e 4f 50 20 54 41 42 4c 45 20 69 73 20 6e 6f 74 20  OP TABLE is not 
2476f 75 73 65 64 20 6f 6e 20 61 20 76 69 65 77 2c 20  used on a view, 
24770 61 6e 64 20 44 52 4f 50 20 56 49 45 57 20 69 73  and DROP VIEW is
24771 20 6e 6f 74 20 75 73 65 64 0a 20 20 2a 2a 20 6f   not used.  ** o
24772 6e 20 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  n a table..  */.
24773 20 20 69 66 28 20 69 73 56 69 65 77 20 26 26 20    if( isView && 
24774 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  pTab->pSelect==0
24775 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
24776 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
24777 22 75 73 65 20 44 52 4f 50 20 54 41 42 4c 45 20  "use DROP TABLE 
24778 74 6f 20 64 65 6c 65 74 65 20 74 61 62 6c 65 20  to delete table 
24779 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  %s", pTab->zName
2477a 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
2477b 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d  _drop_table;.  }
2477c 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20 26  .  if( !isView &
2477d 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  & pTab->pSelect 
2477e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2477f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24780 75 73 65 20 44 52 4f 50 20 56 49 45 57 20 74 6f  use DROP VIEW to
24781 20 64 65 6c 65 74 65 20 76 69 65 77 20 25 73 22   delete view %s"
24782 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
24783 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
24784 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65  op_table;.  }.#e
24785 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  ndif..  /* Gener
24786 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f  ate code to remo
24787 76 65 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f  ve the table fro
24788 6d 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62  m the master tab
24789 6c 65 0a 20 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e  le.  ** on disk.
2478a 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69  .  */.  v = sqli
2478b 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2478c 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  e);.  if( v ){. 
2478d 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69     Trigger *pTri
2478e 67 67 65 72 3b 0a 20 20 20 20 44 62 20 2a 70 44  gger;.    Db *pD
2478f 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
24790 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ];.    sqlite3Be
24791 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
24792 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62  n(pParse, 1, iDb
24793 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
24794 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
24795 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56  ABLE.    if( IsV
24796 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
24797 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24798 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42  eAddOp0(v, OP_VB
24799 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  egin);.    }.#en
2479a 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 46  dif.    sqlite3F
2479b 6b 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73  kDropTable(pPars
2479c 65 2c 20 70 4e 61 6d 65 2c 20 70 54 61 62 29 3b  e, pName, pTab);
2479d 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c  ..    /* Drop al
2479e 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63  l triggers assoc
2479f 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74  iated with the t
247a0 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70  able being dropp
247a1 65 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20  ed. Code.    ** 
247a2 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  is generated to 
247a3 72 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66  remove entries f
247a4 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  rom sqlite_maste
247a5 72 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20  r and/or.    ** 
247a6 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
247a7 65 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  er if required..
247a8 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67      */.    pTrig
247a9 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ger = sqlite3Tri
247aa 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ggerList(pParse,
247ab 20 70 54 61 62 29 3b 0a 20 20 20 20 77 68 69 6c   pTab);.    whil
247ac 65 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  e( pTrigger ){. 
247ad 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72       assert( pTr
247ae 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d  igger->pSchema==
247af 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c  pTab->pSchema ||
247b0 20 0a 20 20 20 20 20 20 20 20 20 20 70 54 72 69   .          pTri
247b1 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64  gger->pSchema==d
247b2 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
247b3 61 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  a );.      sqlit
247b4 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72  e3DropTriggerPtr
247b5 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
247b6 72 29 3b 0a 20 20 20 20 20 20 70 54 72 69 67 67  r);.      pTrigg
247b7 65 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 70  er = pTrigger->p
247b8 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  Next;.    }..#if
247b9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
247ba 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20  _AUTOINCREMENT. 
247bb 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79     /* Remove any
247bc 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20   entries of the 
247bd 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
247be 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64  table associated
247bf 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65   with.    ** the
247c0 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f   table being dro
247c1 70 70 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f  pped. This is do
247c2 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 61  ne before the ta
247c3 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 0a 20  ble is dropped. 
247c4 20 20 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72     ** at the btr
247c5 65 65 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73  ee level, in cas
247c6 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  e the sqlite_seq
247c7 75 65 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64  uence table need
247c8 73 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65  s to.    ** move
247c9 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
247ca 74 68 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61  the drop (can ha
247cb 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63  ppen in auto-vac
247cc 75 75 6d 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a  uum mode)..    *
247cd 2f 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e  /.    if( pTab->
247ce 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75  tabFlags & TF_Au
247cf 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20  toincrement ){. 
247d0 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74       sqlite3Nest
247d1 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
247d2 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20          "DELETE 
247d3 46 52 4f 4d 20 25 73 2e 73 71 6c 69 74 65 5f 73  FROM %s.sqlite_s
247d4 65 71 75 65 6e 63 65 20 57 48 45 52 45 20 6e 61  equence WHERE na
247d5 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20  me=%Q",.        
247d6 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62  pDb->zName, pTab
247d7 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b  ->zName.      );
247d8 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
247d9 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53     /* Drop all S
247da 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
247db 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74  le and index ent
247dc 72 69 65 73 20 74 68 61 74 20 72 65 66 65 72 20  ries that refer 
247dd 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61  to the.    ** ta
247de 62 6c 65 2e 20 54 68 65 20 70 72 6f 67 72 61 6d  ble. The program
247df 20 6e 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f   name loops thro
247e0 75 67 68 20 74 68 65 20 6d 61 73 74 65 72 20 74  ugh the master t
247e1 61 62 6c 65 20 61 6e 64 20 64 65 6c 65 74 65 73  able and deletes
247e2 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20 72 6f  .    ** every ro
247e3 77 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f  w that refers to
247e4 20 61 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20   a table of the 
247e5 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 74 68 65  same name as the
247e6 20 6f 6e 65 20 62 65 69 6e 67 0a 20 20 20 20 2a   one being.    *
247e7 2a 20 64 72 6f 70 70 65 64 2e 20 54 72 69 67 67  * dropped. Trigg
247e8 65 72 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20  ers are handled 
247e9 73 65 70 65 72 61 74 65 6c 79 20 62 65 63 61 75  seperately becau
247ea 73 65 20 61 20 74 72 69 67 67 65 72 20 63 61 6e  se a trigger can
247eb 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74   be.    ** creat
247ec 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64  ed in the temp d
247ed 61 74 61 62 61 73 65 20 74 68 61 74 20 72 65 66  atabase that ref
247ee 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 20 69  ers to a table i
247ef 6e 20 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a  n another.    **
247f0 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a   database..    *
247f1 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  /.    sqlite3Nes
247f2 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
247f3 20 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54   .        "DELET
247f4 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45  E FROM %Q.%s WHE
247f5 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61  RE tbl_name=%Q a
247f6 6e 64 20 74 79 70 65 21 3d 27 74 72 69 67 67 65  nd type!='trigge
247f7 72 27 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62  r'",.        pDb
247f8 2d 3e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ->zName, SCHEMA_
247f9 54 41 42 4c 45 28 69 44 62 29 2c 20 70 54 61 62  TABLE(iDb), pTab
247fa 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f  ->zName);..    /
247fb 2a 20 44 72 6f 70 20 61 6e 79 20 73 74 61 74 69  * Drop any stati
247fc 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65 20 73  stics from the s
247fd 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
247fe 65 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 20  e, if it exists 
247ff 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
24800 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
24801 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20  "sqlite_stat1", 
24802 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
24803 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  me) ){.      sql
24804 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
24805 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
24806 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
24807 73 71 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45  sqlite_stat1 WHE
24808 52 45 20 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d  RE tbl=%Q", pDb-
24809 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e  >zName, pTab->zN
2480a 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ame.      );.   
2480b 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56   }..    if( !isV
2480c 69 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61  iew && !IsVirtua
2480d 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
2480e 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50   destroyTable(pP
2480f 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
24810 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76   }..    /* Remov
24811 65 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72  e the table entr
24812 79 20 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20  y from SQLite's 
24813 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20  internal schema 
24814 61 6e 64 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a  and modify.    *
24815 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  * the schema coo
24816 6b 69 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  kie..    */.    
24817 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
24818 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ab) ){.      sql
24819 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2481a 2c 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 69  , OP_VDestroy, i
2481b 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e  Db, 0, 0, pTab->
2481c 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d  zName, 0);.    }
2481d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2481e 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f  AddOp4(v, OP_Dro
2481f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20  pTable, iDb, 0, 
24820 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  0, pTab->zName, 
24821 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  0);.    sqlite3C
24822 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
24823 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20  se, iDb);.  }.  
24824 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41  sqliteViewResetA
24825 6c 6c 28 64 62 2c 20 69 44 62 29 3b 0a 0a 65 78  ll(db, iDb);..ex
24826 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3a 0a 20  it_drop_table:. 
24827 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
24828 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29  elete(db, pName)
24829 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2482a 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
2482b 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  d to create a ne
2482c 77 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e  w foreign key on
2482d 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75   the table.** cu
2482e 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
2482f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72  nstruction.  pFr
24830 6f 6d 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73  omCol determines
24831 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a   which columns.*
24832 2a 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74  * in the current
24833 20 74 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20   table point to 
24834 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e  the foreign key.
24835 20 20 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30    If pFromCol==0
24836 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74   then.** connect
24837 20 74 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20   the key to the 
24838 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65  last column inse
24839 72 74 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68  rted.  pTo is th
2483a 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65  e name of.** the
2483b 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20   table referred 
2483c 74 6f 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61  to.  pToCol is a
2483d 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20   list of tables 
2483e 69 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20  in the other.** 
2483f 70 54 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74  pTo table that t
24840 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70  he foreign key p
24841 6f 69 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73  oints to.  flags
24842 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a   contains all.**
24843 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
24844 75 74 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20  ut the conflict 
24845 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72  resolution algor
24846 69 74 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a  ithms specified.
24847 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c  ** in the ON DEL
24848 45 54 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61  ETE, ON UPDATE a
24849 6e 64 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61  nd ON INSERT cla
2484a 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46  uses..**.** An F
2484b 4b 65 79 20 73 74 72 75 63 74 75 72 65 20 69 73  Key structure is
2484c 20 63 72 65 61 74 65 64 20 61 6e 64 20 61 64 64   created and add
2484d 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ed to the table 
2484e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64  currently.** und
2484f 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
24850 69 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70  in the pParse->p
24851 4e 65 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 0a  NewTable field..
24852 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 65 69 67  **.** The foreig
24853 6e 20 6b 65 79 20 69 73 20 73 65 74 20 66 6f 72  n key is set for
24854 20 49 4d 4d 45 44 49 41 54 45 20 70 72 6f 63 65   IMMEDIATE proce
24855 73 73 69 6e 67 2e 20 20 41 20 73 75 62 73 65 71  ssing.  A subseq
24856 75 65 6e 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20  uent call.** to 
24857 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65  sqlite3DeferFore
24858 69 67 6e 4b 65 79 28 29 20 6d 69 67 68 74 20 63  ignKey() might c
24859 68 61 6e 67 65 20 74 68 69 73 20 74 6f 20 44 45  hange this to DE
2485a 46 45 52 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54  FERRED..*/.SQLIT
2485b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2485c 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65  qlite3CreateFore
2485d 69 67 6e 4b 65 79 28 0a 20 20 50 61 72 73 65 20  ignKey(.  Parse 
2485e 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
2485f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
24860 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
24861 2a 70 46 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43  *pFromCol,  /* C
24862 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74  olumns in this t
24863 61 62 6c 65 20 74 68 61 74 20 70 6f 69 6e 74 20  able that point 
24864 74 6f 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a  to other table *
24865 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20  /.  Token *pTo, 
24866 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
24867 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 61   of the other ta
24868 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ble */.  ExprLis
24869 74 20 2a 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a  t *pToCol,    /*
2486a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   Columns in the 
2486b 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20  other table */. 
2486c 20 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20   int flags      
2486d 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63        /* Conflic
2486e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67  t resolution alg
2486f 6f 72 69 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20  orithms. */.){. 
24870 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
24871 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64  Parse->db;.#ifnd
24872 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
24873 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 46 4b 65  OREIGN_KEY.  FKe
24874 79 20 2a 70 46 4b 65 79 20 3d 20 30 3b 0a 20 20  y *pFKey = 0;.  
24875 46 4b 65 79 20 2a 70 4e 65 78 74 54 6f 3b 0a 20  FKey *pNextTo;. 
24876 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72   Table *p = pPar
24877 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
24878 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 6e   int nByte;.  in
24879 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  t i;.  int nCol;
2487a 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61  .  char *z;..  a
2487b 73 73 65 72 74 28 20 70 54 6f 21 3d 30 20 29 3b  ssert( pTo!=0 );
2487c 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 49  .  if( p==0 || I
2487d 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
2487e 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
2487f 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20  if( pFromCol==0 
24880 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20  ){.    int iCol 
24881 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20  = p->nCol-1;.   
24882 20 69 66 28 20 4e 45 56 45 52 28 69 43 6f 6c 3c   if( NEVER(iCol<
24883 30 29 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  0) ) goto fk_end
24884 3b 0a 20 20 20 20 69 66 28 20 70 54 6f 43 6f 6c  ;.    if( pToCol
24885 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70   && pToCol->nExp
24886 72 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71  r!=1 ){.      sq
24887 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24888 61 72 73 65 2c 20 22 66 6f 72 65 69 67 6e 20 6b  arse, "foreign k
24889 65 79 20 6f 6e 20 25 73 22 0a 20 20 20 20 20 20  ey on %s".      
2488a 20 20 20 22 20 73 68 6f 75 6c 64 20 72 65 66 65     " should refe
2488b 72 65 6e 63 65 20 6f 6e 6c 79 20 6f 6e 65 20 63  rence only one c
2488c 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 25  olumn of table %
2488d 54 22 2c 0a 20 20 20 20 20 20 20 20 20 70 2d 3e  T",.         p->
2488e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
2488f 2c 20 70 54 6f 29 3b 0a 20 20 20 20 20 20 67 6f  , pTo);.      go
24890 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 7d  to fk_end;.    }
24891 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0a 20  .    nCol = 1;. 
24892 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 43 6f   }else if( pToCo
24893 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78  l && pToCol->nEx
24894 70 72 21 3d 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45  pr!=pFromCol->nE
24895 78 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  xpr ){.    sqlit
24896 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
24897 65 2c 0a 20 20 20 20 20 20 20 20 22 6e 75 6d 62  e,.        "numb
24898 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
24899 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f 65   foreign key doe
2489a 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20  s not match the 
2489b 6e 75 6d 62 65 72 20 6f 66 20 22 0a 20 20 20 20  number of ".    
2489c 20 20 20 20 22 63 6f 6c 75 6d 6e 73 20 69 6e 20      "columns in 
2489d 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 74  the referenced t
2489e 61 62 6c 65 22 29 3b 0a 20 20 20 20 67 6f 74 6f  able");.    goto
2489f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 65 6c 73 65   fk_end;.  }else
248a0 7b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 46 72  {.    nCol = pFr
248a1 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 3b 0a 20 20  omCol->nExpr;.  
248a2 7d 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65  }.  nByte = size
248a3 6f 66 28 2a 70 46 4b 65 79 29 20 2b 20 28 6e 43  of(*pFKey) + (nC
248a4 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28 70 46 4b  ol-1)*sizeof(pFK
248a5 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b 20 70  ey->aCol[0]) + p
248a6 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69 66 28  To->n + 1;.  if(
248a7 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66   pToCol ){.    f
248a8 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 43 6f 6c  or(i=0; i<pToCol
248a9 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
248aa 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71       nByte += sq
248ab 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54  lite3Strlen30(pT
248ac 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  oCol->a[i].zName
248ad 29 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  ) + 1;.    }.  }
248ae 0a 20 20 70 46 4b 65 79 20 3d 20 73 71 6c 69 74  .  pFKey = sqlit
248af 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
248b0 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66  b, nByte );.  if
248b1 28 20 70 46 4b 65 79 3d 3d 30 20 29 7b 0a 20 20  ( pFKey==0 ){.  
248b2 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20    goto fk_end;. 
248b3 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 70 46 72 6f   }.  pFKey->pFro
248b4 6d 20 3d 20 70 3b 0a 20 20 70 46 4b 65 79 2d 3e  m = p;.  pFKey->
248b5 70 4e 65 78 74 46 72 6f 6d 20 3d 20 70 2d 3e 70  pNextFrom = p->p
248b6 46 4b 65 79 3b 0a 20 20 7a 20 3d 20 28 63 68 61  FKey;.  z = (cha
248b7 72 2a 29 26 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b  r*)&pFKey->aCol[
248b8 6e 43 6f 6c 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e  nCol];.  pFKey->
248b9 7a 54 6f 20 3d 20 7a 3b 0a 20 20 6d 65 6d 63 70  zTo = z;.  memcp
248ba 79 28 7a 2c 20 70 54 6f 2d 3e 7a 2c 20 70 54 6f  y(z, pTo->z, pTo
248bb 2d 3e 6e 29 3b 0a 20 20 7a 5b 70 54 6f 2d 3e 6e  ->n);.  z[pTo->n
248bc 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  ] = 0;.  sqlite3
248bd 44 65 71 75 6f 74 65 28 7a 29 3b 0a 20 20 7a 20  Dequote(z);.  z 
248be 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 70  += pTo->n+1;.  p
248bf 46 4b 65 79 2d 3e 6e 43 6f 6c 20 3d 20 6e 43 6f  FKey->nCol = nCo
248c0 6c 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 43 6f  l;.  if( pFromCo
248c1 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 70 46 4b 65  l==0 ){.    pFKe
248c2 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d  y->aCol[0].iFrom
248c3 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20   = p->nCol-1;.  
248c4 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69  }else{.    for(i
248c5 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
248c6 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  {.      int j;. 
248c7 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
248c8 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20  p->nCol; j++){. 
248c9 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
248ca 65 33 53 74 72 49 43 6d 70 28 70 2d 3e 61 43 6f  e3StrICmp(p->aCo
248cb 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 46 72 6f  l[j].zName, pFro
248cc 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  mCol->a[i].zName
248cd 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
248ce 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d    pFKey->aCol[i]
248cf 2e 69 46 72 6f 6d 20 3d 20 6a 3b 0a 20 20 20 20  .iFrom = j;.    
248d0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
248d1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
248d2 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 2d 3e 6e       if( j>=p->n
248d3 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Col ){.        s
248d4 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
248d5 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
248d6 20 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6c 75 6d    "unknown colum
248d7 6e 20 5c 22 25 73 5c 22 20 69 6e 20 66 6f 72 65  n \"%s\" in fore
248d8 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69  ign key definiti
248d9 6f 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20  on", .          
248da 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a  pFromCol->a[i].z
248db 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67  Name);.        g
248dc 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20  oto fk_end;.    
248dd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
248de 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20  if( pToCol ){.  
248df 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
248e0 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
248e1 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
248e2 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61  rlen30(pToCol->a
248e3 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
248e4 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d    pFKey->aCol[i]
248e5 2e 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20  .zCol = z;.     
248e6 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43 6f   memcpy(z, pToCo
248e7 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e  l->a[i].zName, n
248e8 29 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d 20  );.      z[n] = 
248e9 30 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 2b  0;.      z += n+
248ea 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
248eb 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64  FKey->isDeferred
248ec 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 61   = 0;.  pFKey->a
248ed 41 63 74 69 6f 6e 5b 30 5d 20 3d 20 28 75 38 29  Action[0] = (u8)
248ee 28 66 6c 61 67 73 20 26 20 30 78 66 66 29 3b 20  (flags & 0xff); 
248ef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e             /* ON
248f0 20 44 45 4c 45 54 45 20 61 63 74 69 6f 6e 20 2a   DELETE action *
248f1 2f 0a 20 20 70 46 4b 65 79 2d 3e 61 41 63 74 69  /.  pFKey->aActi
248f2 6f 6e 5b 31 5d 20 3d 20 28 75 38 29 28 28 66 6c  on[1] = (u8)((fl
248f3 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78 66  ags >> 8 ) & 0xf
248f4 66 29 3b 20 20 20 20 2f 2a 20 4f 4e 20 55 50 44  f);    /* ON UPD
248f5 41 54 45 20 61 63 74 69 6f 6e 20 2a 2f 0a 0a 20  ATE action */.. 
248f6 20 70 4e 65 78 74 54 6f 20 3d 20 28 46 4b 65 79   pNextTo = (FKey
248f7 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 6e   *)sqlite3HashIn
248f8 73 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61  sert(&p->pSchema
248f9 2d 3e 66 6b 65 79 48 61 73 68 2c 20 0a 20 20 20  ->fkeyHash, .   
248fa 20 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 73     pFKey->zTo, s
248fb 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
248fc 46 4b 65 79 2d 3e 7a 54 6f 29 2c 20 28 76 6f 69  FKey->zTo), (voi
248fd 64 20 2a 29 70 46 4b 65 79 0a 20 20 29 3b 0a 20  d *)pFKey.  );. 
248fe 20 69 66 28 20 70 4e 65 78 74 54 6f 3d 3d 70 46   if( pNextTo==pF
248ff 4b 65 79 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  Key ){.    db->m
24900 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
24901 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
24902 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 78  ;.  }.  if( pNex
24903 74 54 6f 20 29 7b 0a 20 20 20 20 61 73 73 65 72  tTo ){.    asser
24904 74 28 20 70 4e 65 78 74 54 6f 2d 3e 70 50 72 65  t( pNextTo->pPre
24905 76 54 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70 46  vTo==0 );.    pF
24906 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 70  Key->pNextTo = p
24907 4e 65 78 74 54 6f 3b 0a 20 20 20 20 70 4e 65 78  NextTo;.    pNex
24908 74 54 6f 2d 3e 70 50 72 65 76 54 6f 20 3d 20 70  tTo->pPrevTo = p
24909 46 4b 65 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  FKey;.  }..  /* 
2490a 4c 69 6e 6b 20 74 68 65 20 66 6f 72 65 69 67 6e  Link the foreign
2490b 20 6b 65 79 20 74 6f 20 74 68 65 20 74 61 62 6c   key to the tabl
2490c 65 20 61 73 20 74 68 65 20 6c 61 73 74 20 73 74  e as the last st
2490d 65 70 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 46  ep..  */.  p->pF
2490e 4b 65 79 20 3d 20 70 46 4b 65 79 3b 0a 20 20 70  Key = pFKey;.  p
2490f 46 4b 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e  FKey = 0;..fk_en
24910 64 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  d:.  sqlite3DbFr
24911 65 65 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 23  ee(db, pFKey);.#
24912 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
24913 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  d(SQLITE_OMIT_FO
24914 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20  REIGN_KEY) */.  
24915 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
24916 65 6c 65 74 65 28 64 62 2c 20 70 46 72 6f 6d 43  elete(db, pFromC
24917 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  ol);.  sqlite3Ex
24918 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
24919 20 70 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a   pToCol);.}../*.
2491a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2491b 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  is called when a
2491c 6e 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  n INITIALLY IMME
2491d 44 49 41 54 45 20 6f 72 20 49 4e 49 54 49 41 4c  DIATE or INITIAL
2491e 4c 59 20 44 45 46 45 52 52 45 44 0a 2a 2a 20 63  LY DEFERRED.** c
2491f 6c 61 75 73 65 20 69 73 20 73 65 65 6e 20 61 73  lause is seen as
24920 20 70 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69   part of a forei
24921 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f  gn key definitio
24922 6e 2e 20 20 54 68 65 20 69 73 44 65 66 65 72 72  n.  The isDeferr
24923 65 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  ed.** parameter 
24924 69 73 20 31 20 66 6f 72 20 49 4e 49 54 49 41 4c  is 1 for INITIAL
24925 4c 59 20 44 45 46 45 52 52 45 44 20 61 6e 64 20  LY DEFERRED and 
24926 30 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20  0 for INITIALLY 
24927 49 4d 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 54 68  IMMEDIATE..** Th
24928 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68  e behavior of th
24929 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
2492a 63 72 65 61 74 65 64 20 66 6f 72 65 69 67 6e 20  created foreign 
2492b 6b 65 79 20 69 73 20 61 64 6a 75 73 74 65 64 0a  key is adjusted.
2492c 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a  ** accordingly..
2492d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2492e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
2492f 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61  ferForeignKey(Pa
24930 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
24931 20 69 73 44 65 66 65 72 72 65 64 29 7b 0a 23 69   isDeferred){.#i
24932 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24933 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
24934 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 46  Table *pTab;.  F
24935 4b 65 79 20 2a 70 46 4b 65 79 3b 0a 20 20 69 66  Key *pFKey;.  if
24936 28 20 28 70 54 61 62 20 3d 20 70 50 61 72 73 65  ( (pTab = pParse
24937 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20  ->pNewTable)==0 
24938 7c 7c 20 28 70 46 4b 65 79 20 3d 20 70 54 61 62  || (pFKey = pTab
24939 2d 3e 70 46 4b 65 79 29 3d 3d 30 20 29 20 72 65  ->pFKey)==0 ) re
2493a 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
2493b 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c  isDeferred==0 ||
2493c 20 69 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29   isDeferred==1 )
2493d 3b 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33  ; /* EV: R-30323
2493e 2d 32 31 39 31 37 20 2a 2f 0a 20 20 70 46 4b 65  -21917 */.  pFKe
2493f 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20  y->isDeferred = 
24940 28 75 38 29 69 73 44 65 66 65 72 72 65 64 3b 0a  (u8)isDeferred;.
24941 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
24942 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
24943 61 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e  at will erase an
24944 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a  d refill index *
24945 70 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a  pIdx.  This is.*
24946 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61  * used to initia
24947 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65  lize a newly cre
24948 61 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f  ated index or to
24949 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a   recompute the.*
2494a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20  * content of an 
2494b 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  index in respons
2494c 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63  e to a REINDEX c
2494d 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66  ommand..**.** if
2494e 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20   memRootPage is 
2494f 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74  not negative, it
24950 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
24951 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a  index is newly.*
24952 2a 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20  * created.  The 
24953 72 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69  register specifi
24954 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67  ed by memRootPag
24955 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a  e contains the.*
24956 2a 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  * root page numb
24957 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  er of the index.
24958 20 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65    If memRootPage
24959 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
2495a 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  en.** the index 
2495b 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61  already exists a
2495c 6e 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72  nd must be clear
2495d 65 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20  ed before being 
2495e 72 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20  refilled and.** 
2495f 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
24960 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65  mber of the inde
24961 78 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  x is taken from 
24962 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f  pIndex->tnum..*/
24963 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
24964 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28  ite3RefillIndex(
24965 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49  Parse *pParse, I
24966 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e  ndex *pIndex, in
24967 74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a  t memRootPage){.
24968 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
24969 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20  pIndex->pTable; 
2496a 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68   /* The table th
2496b 61 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f  at is indexed */
2496c 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50  .  int iTab = pP
2496d 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20  arse->nTab++;   
2496e 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f    /* Btree curso
2496f 72 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20  r used for pTab 
24970 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20  */.  int iIdx = 
24971 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20  pParse->nTab++; 
24972 20 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72      /* Btree cur
24973 73 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e  sor used for pIn
24974 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  dex */.  int add
24975 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  r1;             
24976 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
24977 73 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f  ss of top of loo
24978 70 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b  p */.  int tnum;
24979 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2497a 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61        /* Root pa
2497b 67 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20  ge of index */. 
2497c 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
2497d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2497e 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2497f 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75   into this virtu
24980 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20  al machine */.  
24981 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20  KeyInfo *pKey;  
24982 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24983 2a 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e  * KeyInfo for in
24984 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  dex */.  int reg
24985 49 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20  IdxKey;         
24986 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
24987 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ters containing 
24988 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f  the index key */
24989 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64  .  int regRecord
2498a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2498b 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f    /* Register ho
2498c 6c 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64  lding assemblied
2498d 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f   index record */
2498e 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2498f 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20   pParse->db;    
24990 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
24991 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
24992 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69    int iDb = sqli
24993 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
24994 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  (db, pIndex->pSc
24995 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20  hema);..#ifndef 
24996 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
24997 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20  ORIZATION.  if( 
24998 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
24999 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2499a 52 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d  REINDEX, pIndex-
2499b 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20  >zName, 0,.     
2499c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2499d 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74  ame ) ){.    ret
2499e 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  urn;.  }.#endif.
2499f 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20  .  /* Require a 
249a0 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68  write-lock on th
249a1 65 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f  e table to perfo
249a2 72 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  rm this operatio
249a3 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61  n */.  sqlite3Ta
249a4 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20  bleLock(pParse, 
249a5 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
249a6 20 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29   1, pTab->zName)
249a7 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  ;..  v = sqlite3
249a8 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
249a9 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
249aa 74 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52  turn;.  if( memR
249ab 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20  ootPage>=0 ){.  
249ac 20 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74    tnum = memRoot
249ad 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Page;.  }else{. 
249ae 20 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78     tnum = pIndex
249af 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69  ->tnum;.    sqli
249b0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
249b1 20 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c   OP_Clear, tnum,
249b2 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65   iDb);.  }.  pKe
249b3 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
249b4 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
249b5 70 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74  pIndex);.  sqlit
249b6 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
249b7 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49  OP_OpenWrite, iI
249b8 64 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a  dx, tnum, iDb, .
249b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
249ba 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79      (char *)pKey
249bb 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
249bc 44 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d  DOFF);.  if( mem
249bd 52 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20  RootPage>=0 ){. 
249be 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
249bf 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20  angeP5(v, 1);.  
249c0 7d 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  }.  sqlite3OpenT
249c1 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61  able(pParse, iTa
249c2 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50  b, iDb, pTab, OP
249c3 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64  _OpenRead);.  ad
249c4 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
249c5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
249c6 77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a  wind, iTab, 0);.
249c7 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71    regRecord = sq
249c8 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
249c9 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64  pParse);.  regId
249ca 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65  xKey = sqlite3Ge
249cb 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70  nerateIndexKey(p
249cc 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69  Parse, pIndex, i
249cd 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20  Tab, regRecord, 
249ce 31 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78  1);.  if( pIndex
249cf 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
249d0 6e 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  ne ){.    const 
249d1 69 6e 74 20 72 65 67 52 6f 77 69 64 20 3d 20 72  int regRowid = r
249d2 65 67 49 64 78 4b 65 79 20 2b 20 70 49 6e 64 65  egIdxKey + pInde
249d3 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  x->nColumn;.    
249d4 63 6f 6e 73 74 20 69 6e 74 20 6a 32 20 3d 20 73  const int j2 = s
249d5 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
249d6 74 41 64 64 72 28 76 29 20 2b 20 32 3b 0a 20 20  tAddr(v) + 2;.  
249d7 20 20 76 6f 69 64 20 2a 20 63 6f 6e 73 74 20 70    void * const p
249d8 52 65 67 4b 65 79 20 3d 20 53 51 4c 49 54 45 5f  RegKey = SQLITE_
249d9 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67 49 64  INT_TO_PTR(regId
249da 78 4b 65 79 29 3b 0a 0a 20 20 20 20 2f 2a 20 54  xKey);..    /* T
249db 68 65 20 72 65 67 69 73 74 65 72 73 20 61 63 63  he registers acc
249dc 65 73 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f  essed by the OP_
249dd 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20  IsUnique opcode 
249de 77 65 72 65 20 61 6c 6c 6f 63 61 74 65 64 0a 20  were allocated. 
249df 20 20 20 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69     ** using sqli
249e0 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
249e1 29 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20  ) inside of the 
249e2 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49  sqlite3GenerateI
249e3 6e 64 65 78 4b 65 79 28 29 0a 20 20 20 20 2a 2a  ndexKey().    **
249e4 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 4a 75 73   call above. Jus
249e5 74 20 62 65 66 6f 72 65 20 74 68 61 74 20 66 75  t before that fu
249e6 6e 63 74 69 6f 6e 20 77 61 73 20 66 72 65 65 64  nction was freed
249e7 20 74 68 65 79 20 77 65 72 65 20 72 65 6c 65 61   they were relea
249e8 73 65 64 0a 20 20 20 20 2a 2a 20 28 6d 61 64 65  sed.    ** (made
249e9 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68   available to th
249ea 65 20 63 6f 6d 70 69 6c 65 72 20 66 6f 72 20 72  e compiler for r
249eb 65 75 73 65 29 20 75 73 69 6e 67 20 0a 20 20 20  euse) using .   
249ec 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 6c 65 61   ** sqlite3Relea
249ed 73 65 54 65 6d 70 52 61 6e 67 65 28 29 2e 20 53  seTempRange(). S
249ee 6f 20 69 6e 20 73 6f 6d 65 20 77 61 79 73 20 68  o in some ways h
249ef 61 76 69 6e 67 20 74 68 65 20 4f 50 5f 49 73 55  aving the OP_IsU
249f0 6e 69 71 75 65 0a 20 20 20 20 2a 2a 20 6f 70 63  nique.    ** opc
249f1 6f 64 65 20 75 73 65 20 74 68 65 20 76 61 6c 75  ode use the valu
249f2 65 73 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e  es stored within
249f3 20 73 65 65 6d 73 20 64 61 6e 67 65 72 6f 75 73   seems dangerous
249f4 2e 20 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65  . However, since
249f5 0a 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 62  .    ** we can b
249f6 65 20 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f  e sure that no o
249f7 74 68 65 72 20 74 65 6d 70 20 72 65 67 69 73 74  ther temp regist
249f8 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 61 6c  ers have been al
249f9 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a 2a 20 73  located.    ** s
249fa 69 6e 63 65 20 73 71 6c 69 74 65 33 52 65 6c 65  ince sqlite3Rele
249fb 61 73 65 54 65 6d 70 52 61 6e 67 65 28 29 20 77  aseTempRange() w
249fc 61 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73  as called, it is
249fd 20 73 61 66 65 20 74 6f 20 64 6f 20 73 6f 2e 0a   safe to do so..
249fe 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
249ff 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
24a00 4f 50 5f 49 73 55 6e 69 71 75 65 2c 20 69 49 64  OP_IsUnique, iId
24a01 78 2c 20 6a 32 2c 20 72 65 67 52 6f 77 69 64 2c  x, j2, regRowid,
24a02 20 70 52 65 67 4b 65 79 2c 20 50 34 5f 49 4e 54   pRegKey, P4_INT
24a03 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  32);.    sqlite3
24a04 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 0a  HaltConstraint(.
24a05 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20          pParse, 
24a06 4f 45 5f 41 62 6f 72 74 2c 20 22 69 6e 64 65 78  OE_Abort, "index
24a07 65 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e  ed columns are n
24a08 6f 74 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53  ot unique", P4_S
24a09 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71  TATIC);.  }.  sq
24a0a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24a0b 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
24a0c 20 69 49 64 78 2c 20 72 65 67 52 65 63 6f 72 64   iIdx, regRecord
24a0d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
24a0e 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
24a0f 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54  AG_USESEEKRESULT
24a10 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
24a11 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
24a12 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  e, regRecord);. 
24a13 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24a14 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  p2(v, OP_Next, i
24a15 54 61 62 2c 20 61 64 64 72 31 2b 31 29 3b 0a 20  Tab, addr1+1);. 
24a16 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
24a17 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a  Here(v, addr1);.
24a18 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24a19 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
24a1a 20 69 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65   iTab);.  sqlite
24a1b 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
24a1c 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 29 3b 0a  P_Close, iIdx);.
24a1d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
24a1e 61 20 6e 65 77 20 69 6e 64 65 78 20 66 6f 72 20  a new index for 
24a1f 61 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20 20 70  an SQL table.  p
24a20 4e 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69 73 20  Name1.pName2 is 
24a21 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
24a22 69 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20 70 54  index .** and pT
24a23 62 6c 4c 69 73 74 20 69 73 20 74 68 65 20 6e 61  blList is the na
24a24 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
24a25 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e  that is to be in
24a26 64 65 78 65 64 2e 20 20 42 6f 74 68 20 77 69 6c  dexed.  Both wil
24a27 6c 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20 66 6f  l .** be NULL fo
24a28 72 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20  r a primary key 
24a29 6f 72 20 61 6e 20 69 6e 64 65 78 20 74 68 61 74  or an index that
24a2a 20 69 73 20 63 72 65 61 74 65 64 20 74 6f 20 73   is created to s
24a2b 61 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e 49 51  atisfy a.** UNIQ
24a2c 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20  UE constraint.  
24a2d 49 66 20 70 54 61 62 6c 65 20 61 6e 64 20 70 49  If pTable and pI
24a2e 6e 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c 20 75  ndex are NULL, u
24a2f 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  se pParse->pNewT
24a30 61 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65 20 74  able.** as the t
24a31 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78  able to be index
24a32 65 64 2e 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  ed.  pParse->pNe
24a33 77 54 61 62 6c 65 20 69 73 20 61 20 74 61 62 6c  wTable is a tabl
24a34 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 63 75 72  e that is.** cur
24a35 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e  rently being con
24a36 73 74 72 75 63 74 65 64 20 62 79 20 61 20 43 52  structed by a CR
24a37 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
24a38 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73  ment..**.** pLis
24a39 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 63  t is a list of c
24a3a 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64  olumns to be ind
24a3b 65 78 65 64 2e 20 20 70 4c 69 73 74 20 77 69 6c  exed.  pList wil
24a3c 6c 20 62 65 20 4e 55 4c 4c 20 69 66 20 74 68 69  l be NULL if thi
24a3d 73 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d 61 72  s.** is a primar
24a3e 79 20 6b 65 79 20 6f 72 20 75 6e 69 71 75 65 2d  y key or unique-
24a3f 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68  constraint on th
24a40 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 6f  e most recent co
24a41 6c 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f  lumn added.** to
24a42 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65   the table curre
24a43 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
24a44 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a  ruction.  .**.**
24a45 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 69 73   If the index is
24a46 20 63 72 65 61 74 65 64 20 73 75 63 63 65 73 73   created success
24a47 66 75 6c 6c 79 2c 20 72 65 74 75 72 6e 20 61 20  fully, return a 
24a48 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
24a49 65 77 20 49 6e 64 65 78 0a 2a 2a 20 73 74 72 75  ew Index.** stru
24a4a 63 74 75 72 65 2e 20 54 68 69 73 20 69 73 20 75  cture. This is u
24a4b 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 41 64  sed by sqlite3Ad
24a4c 64 50 72 69 6d 61 72 79 4b 65 79 28 29 20 74 6f  dPrimaryKey() to
24a4d 20 6d 61 72 6b 20 74 68 65 20 69 6e 64 65 78 0a   mark the index.
24a4e 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 73  ** as the tables
24a4f 20 70 72 69 6d 61 72 79 20 6b 65 79 20 28 49 6e   primary key (In
24a50 64 65 78 2e 61 75 74 6f 49 6e 64 65 78 3d 3d 32  dex.autoIndex==2
24a51 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
24a52 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c 69  VATE Index *sqli
24a53 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 0a  te3CreateIndex(.
24a54 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
24a55 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e 66 6f       /* All info
24a56 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
24a57 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 54 6f  is parse */.  To
24a58 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20  ken *pName1,    
24a59 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f   /* First part o
24a5a 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61  f index name. Ma
24a5b 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54  y be NULL */.  T
24a5c 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
24a5d 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74    /* Second part
24a5e 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20   of index name. 
24a5f 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
24a60 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c 4e 61   SrcList *pTblNa
24a61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20  me, /* Table to 
24a62 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61 72 73  index. Use pPars
24a63 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 66 20  e->pNewTable if 
24a64 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  0 */.  ExprList 
24a65 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 20 6c  *pList,   /* A l
24a66 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74  ist of columns t
24a67 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a  o be indexed */.
24a68 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
24a69 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74       /* OE_Abort
24a6a 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f  , OE_Ignore, OE_
24a6b 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e  Replace, or OE_N
24a6c 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  one */.  Token *
24a6d 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a 20 54  pStart,     /* T
24a6e 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20  he CREATE token 
24a6f 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 69 73  that begins this
24a70 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
24a71 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20  Token *pEnd,    
24a72 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20 74 68     /* The ")" th
24a73 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20 43 52  at closes the CR
24a74 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65  EATE INDEX state
24a75 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f  ment */.  int so
24a76 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f 2a 20  rtOrder,     /* 
24a77 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 70 72  Sort order of pr
24a78 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e 20 70  imary key when p
24a79 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20 20  List==NULL */.  
24a7a 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 20 20  int ifNotExist  
24a7b 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f 72     /* Omit error
24a7c 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65 61 64   if index alread
24a7d 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
24a7e 20 49 6e 64 65 78 20 2a 70 52 65 74 20 3d 20 30   Index *pRet = 0
24a7f 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ;     /* Pointer
24a80 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20   to return */.  
24a81 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b  Table *pTab = 0;
24a82 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
24a83 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20   be indexed */. 
24a84 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 20 3d   Index *pIndex =
24a85 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 64   0;   /* The ind
24a86 65 78 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  ex to be created
24a87 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
24a88 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 61  e = 0;     /* Na
24a89 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  me of the index 
24a8a 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20  */.  int nName; 
24a8b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
24a8c 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
24a8d 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20  s in zName */.  
24a8e 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f 6b 65  int i, j;.  Toke
24a8f 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20 20 20  n nullId;       
24a90 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e 20 66   /* Fake token f
24a91 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 44 20 6c  or an empty ID l
24a92 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 78 65 72  ist */.  DbFixer
24a93 20 73 46 69 78 3b 20 20 20 20 20 20 20 20 2f 2a   sFix;        /*
24a94 20 46 6f 72 20 61 73 73 69 67 6e 69 6e 67 20 64   For assigning d
24a95 61 74 61 62 61 73 65 20 6e 61 6d 65 73 20 74 6f  atabase names to
24a96 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74   pTable */.  int
24a97 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 20   sortOrderMask; 
24a98 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f 72 20    /* 1 to honor 
24a99 44 45 53 43 20 69 6e 20 69 6e 64 65 78 2e 20 20  DESC in index.  
24a9a 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a 2f 0a  0 to ignore. */.
24a9b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
24a9c 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62  pParse->db;.  Db
24a9d 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20   *pDb;          
24a9e 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66     /* The specif
24a9f 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  ic table contain
24aa0 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65 64 20  ing the indexed 
24aa1 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
24aa2 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
24aa3 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
24aa4 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74  he database that
24aa5 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 65   is being writte
24aa6 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  n */.  Token *pN
24aa7 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a 20 55  ame = 0;    /* U
24aa8 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20  nqualified name 
24aa9 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20  of the index to 
24aaa 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 74 72 75  create */.  stru
24aab 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
24aac 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f 2a 20   *pListItem; /* 
24aad 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
24aae 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20   pList */.  int 
24aaf 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45 78 74  nCol;.  int nExt
24ab0 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  ra = 0;.  char *
24ab1 7a 45 78 74 72 61 3b 0a 0a 20 20 61 73 73 65 72  zExtra;..  asser
24ab2 74 28 20 70 53 74 61 72 74 3d 3d 30 20 7c 7c 20  t( pStart==0 || 
24ab3 70 45 6e 64 21 3d 30 20 29 3b 20 2f 2a 20 70 45  pEnd!=0 ); /* pE
24ab4 6e 64 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 4e  nd must be non-N
24ab5 55 4c 4c 20 69 66 20 70 53 74 61 72 74 20 69 73  ULL if pStart is
24ab6 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
24ab7 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b  arse->nErr==0 );
24ab8 20 20 20 20 20 20 2f 2a 20 4e 65 76 65 72 20 63        /* Never c
24ab9 61 6c 6c 65 64 20 77 69 74 68 20 70 72 69 6f 72  alled with prior
24aba 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 66 28   errors */.  if(
24abb 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
24abc 64 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f  d || IN_DECLARE_
24abd 56 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74 6f  VTAB ){.    goto
24abe 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
24abf 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51  ex;.  }.  if( SQ
24ac0 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
24ac1 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
24ac2 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  e) ){.    goto e
24ac3 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24ac4 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ;.  }..  /*.  **
24ac5 20 46 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20   Find the table 
24ac6 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e  that is to be in
24ac7 64 65 78 65 64 2e 20 20 52 65 74 75 72 6e 20 65  dexed.  Return e
24ac8 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75 6e  arly if not foun
24ac9 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  d..  */.  if( pT
24aca 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20 20  blName!=0 ){..  
24acb 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74 77 6f    /* Use the two
24acc 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61 6d 65  -part index name
24acd 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
24ace 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 20 20  e database .    
24acf 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72  ** to search for
24ad0 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46 69 78   the table. 'Fix
24ad1 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  ' the table name
24ad2 20 74 6f 20 74 68 69 73 20 64 62 0a 20 20 20 20   to this db.    
24ad3 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69 6e  ** before lookin
24ad4 67 20 75 70 20 74 68 65 20 74 61 62 6c 65 2e 0a  g up the table..
24ad5 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
24ad6 74 28 20 70 4e 61 6d 65 31 20 26 26 20 70 4e 61  t( pName1 && pNa
24ad7 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 62 20 3d  me2 );.    iDb =
24ad8 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
24ad9 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ame(pParse, pNam
24ada 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61  e1, pName2, &pNa
24adb 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62  me);.    if( iDb
24adc 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63  <0 ) goto exit_c
24add 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 23 69  reate_index;..#i
24ade 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24adf 54 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a 20  T_TEMPDB.    /* 
24ae0 49 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d  If the index nam
24ae1 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69 65  e was unqualifie
24ae2 64 2c 20 63 68 65 63 6b 20 69 66 20 74 68 65 20  d, check if the 
24ae3 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  the table.    **
24ae4 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c 65   is a temp table
24ae5 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68 65  . If so, set the
24ae6 20 64 61 74 61 62 61 73 65 20 74 6f 20 31 2e 20   database to 1. 
24ae7 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 0a 20  Do not do this. 
24ae8 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 69 61 6c     ** if initial
24ae9 69 73 69 6e 67 20 61 20 64 61 74 61 62 61 73 65  ising a database
24aea 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a   schema..    */.
24aeb 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69      if( !db->ini
24aec 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20  t.busy ){.      
24aed 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72  pTab = sqlite3Sr
24aee 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72  cListLookup(pPar
24aef 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a 20  se, pTblName);. 
24af0 20 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d       if( pName2-
24af1 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26 26  >n==0 && pTab &&
24af2 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d   pTab->pSchema==
24af3 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
24af4 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 44  ma ){.        iD
24af5 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  b = 1;.      }. 
24af6 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
24af7 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49   if( sqlite3FixI
24af8 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73  nit(&sFix, pPars
24af9 65 2c 20 69 44 62 2c 20 22 69 6e 64 65 78 22 2c  e, iDb, "index",
24afa 20 70 4e 61 6d 65 29 20 26 26 0a 20 20 20 20 20   pName) &&.     
24afb 20 20 20 73 71 6c 69 74 65 33 46 69 78 53 72 63     sqlite3FixSrc
24afc 4c 69 73 74 28 26 73 46 69 78 2c 20 70 54 62 6c  List(&sFix, pTbl
24afd 4e 61 6d 65 29 0a 20 20 20 20 29 7b 0a 20 20 20  Name).    ){.   
24afe 20 20 20 2f 2a 20 42 65 63 61 75 73 65 20 74 68     /* Because th
24aff 65 20 70 61 72 73 65 72 20 63 6f 6e 73 74 72 75  e parser constru
24b00 63 74 73 20 70 54 62 6c 4e 61 6d 65 20 66 72 6f  cts pTblName fro
24b01 6d 20 61 20 73 69 6e 67 6c 65 20 69 64 65 6e 74  m a single ident
24b02 69 66 69 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20  ifier,.      ** 
24b03 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73  sqlite3FixSrcLis
24b04 74 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c  t can never fail
24b05 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  . */.      asser
24b06 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  t(0);.    }.    
24b07 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pTab = sqlite3Lo
24b08 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
24b09 2c 20 30 2c 20 70 54 62 6c 4e 61 6d 65 2d 3e 61  , 0, pTblName->a
24b0a 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20  [0].zName, .    
24b0b 20 20 20 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b      pTblName->a[
24b0c 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
24b0d 20 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20     if( !pTab || 
24b0e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
24b0f 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
24b10 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 61  ate_index;.    a
24b11 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69  ssert( db->aDb[i
24b12 44 62 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 54 61  Db].pSchema==pTa
24b13 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  b->pSchema );.  
24b14 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
24b15 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20  t( pName==0 );. 
24b16 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73 65     pTab = pParse
24b17 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 20  ->pNewTable;.   
24b18 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74   if( !pTab ) got
24b19 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24b1a 64 65 78 3b 0a 20 20 20 20 69 44 62 20 3d 20 73  dex;.    iDb = s
24b1b 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
24b1c 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
24b1d 63 68 65 6d 61 29 3b 0a 20 20 7d 0a 20 20 70 44  chema);.  }.  pD
24b1e 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
24b1f 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54  ];..  assert( pT
24b20 61 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ab!=0 );.  asser
24b21 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  t( pParse->nErr=
24b22 3d 30 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  =0 );.  if( sqli
24b23 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62  te3StrNICmp(pTab
24b24 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  ->zName, "sqlite
24b25 5f 22 2c 20 37 29 3d 3d 30 20 0a 20 20 20 20 20  _", 7)==0 .     
24b26 20 20 26 26 20 6d 65 6d 63 6d 70 28 26 70 54 61    && memcmp(&pTa
24b27 62 2d 3e 7a 4e 61 6d 65 5b 37 5d 2c 22 61 6c 74  b->zName[7],"alt
24b28 65 72 74 61 62 5f 22 2c 39 29 21 3d 30 20 29 7b  ertab_",9)!=0 ){
24b29 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
24b2a 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
24b2b 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62  ble %s may not b
24b2c 65 20 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62  e indexed", pTab
24b2d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
24b2e 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24b2f 6e 64 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  ndex;.  }.#ifnde
24b30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
24b31 45 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  EW.  if( pTab->p
24b32 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71  Select ){.    sq
24b33 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24b34 61 72 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79  arse, "views may
24b35 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22   not be indexed"
24b36 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
24b37 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
24b38 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65   }.#endif.#ifnde
24b39 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
24b3a 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
24b3b 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
24b3c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
24b3d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
24b3e 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20  "virtual tables 
24b3f 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78  may not be index
24b40 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ed");.    goto e
24b41 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24b42 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
24b43 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65  /*.  ** Find the
24b44 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64   name of the ind
24b45 65 78 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74  ex.  Make sure t
24b46 68 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  here is not alre
24b47 61 64 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a  ady another.  **
24b48 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20   index or table 
24b49 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61  with the same na
24b4a 6d 65 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  me.  .  **.  ** 
24b4b 45 78 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77  Exception:  If w
24b4c 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68  e are reading th
24b4d 65 20 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61  e names of perma
24b4e 6e 65 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f  nent indices fro
24b4f 6d 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  m the.  ** sqlit
24b50 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28  e_master table (
24b51 62 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68  because some oth
24b52 65 72 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67  er process chang
24b53 65 64 20 74 68 65 20 73 63 68 65 6d 61 29 20 61  ed the schema) a
24b54 6e 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74  nd.  ** one of t
24b55 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63  he index names c
24b56 6f 6c 6c 69 64 65 73 20 77 69 74 68 20 74 68 65  ollides with the
24b57 20 6e 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f   name of a tempo
24b58 72 61 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20  rary table or.  
24b59 2a 2a 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77  ** index, then w
24b5a 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20  e will continue 
24b5b 74 6f 20 70 72 6f 63 65 73 73 20 74 68 69 73 20  to process this 
24b5c 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  index..  **.  **
24b5d 20 49 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20   If pName==0 it 
24b5e 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 61 72  means that we ar
24b5f 65 0a 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77  e.  ** dealing w
24b60 69 74 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65  ith a primary ke
24b61 79 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73  y or UNIQUE cons
24b62 74 72 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65  traint.  We have
24b63 20 74 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20   to invent our. 
24b64 20 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20   ** own name..  
24b65 2a 2f 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29  */.  if( pName )
24b66 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71  {.    zName = sq
24b67 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
24b68 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20  en(db, pName);. 
24b69 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20     if( zName==0 
24b6a 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61  ) goto exit_crea
24b6b 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 66  te_index;.    if
24b6c 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
24b6d 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e  ite3CheckObjectN
24b6e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d  ame(pParse, zNam
24b6f 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  e) ){.      goto
24b70 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
24b71 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ex;.    }.    if
24b72 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
24b73 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71   ){.      if( sq
24b74 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
24b75 62 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20  b, zName, 0)!=0 
24b76 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
24b77 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
24b78 65 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72  e, "there is alr
24b79 65 61 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d  eady a table nam
24b7a 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  ed %s", zName);.
24b7b 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69          goto exi
24b7c 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24b7d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
24b7e 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
24b7f 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65  dIndex(db, zName
24b80 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30  , pDb->zName)!=0
24b81 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69   ){.      if( !i
24b82 66 4e 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20  fNotExist ){.   
24b83 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
24b84 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e  rMsg(pParse, "in
24b85 64 65 78 20 25 73 20 61 6c 72 65 61 64 79 20 65  dex %s already e
24b86 78 69 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  xists", zName);.
24b87 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f        }.      go
24b88 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24b89 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  ndex;.    }.  }e
24b8a 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a  lse{.    int n;.
24b8b 20 20 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70      Index *pLoop
24b8c 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d  ;.    for(pLoop=
24b8d 70 54 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d  pTab->pIndex, n=
24b8e 31 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d  1; pLoop; pLoop=
24b8f 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b  pLoop->pNext, n+
24b90 2b 29 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d  +){}.    zName =
24b91 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
24b92 64 62 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f  db, "sqlite_auto
24b93 69 6e 64 65 78 5f 25 73 5f 25 64 22 2c 20 70 54  index_%s_%d", pT
24b94 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20  ab->zName, n);. 
24b95 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20     if( zName==0 
24b96 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
24b97 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24b98 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
24b99 20 43 68 65 63 6b 20 66 6f 72 20 61 75 74 68 6f   Check for autho
24b9a 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65 61  rization to crea
24b9b 74 65 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a  te an index..  *
24b9c 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
24b9d 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
24b9e 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73  ION.  {.    cons
24b9f 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 70 44  t char *zDb = pD
24ba0 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66  b->zName;.    if
24ba1 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
24ba2 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
24ba3 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41  E_INSERT, SCHEMA
24ba4 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 30 2c 20  _TABLE(iDb), 0, 
24ba5 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  zDb) ){.      go
24ba6 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24ba7 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
24ba8 69 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  i = SQLITE_CREAT
24ba9 45 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28  E_INDEX;.    if(
24baa 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
24bab 20 69 44 62 3d 3d 31 20 29 20 69 20 3d 20 53 51   iDb==1 ) i = SQ
24bac 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
24bad 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20  _INDEX;.    if( 
24bae 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
24baf 28 70 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d  (pParse, i, zNam
24bb0 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  e, pTab->zName, 
24bb1 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  zDb) ){.      go
24bb2 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24bb3 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ndex;.    }.  }.
24bb4 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20  #endif..  /* If 
24bb5 70 4c 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61  pList==0, it mea
24bb6 6e 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ns this routine 
24bb7 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61  was called to ma
24bb8 6b 65 20 61 20 70 72 69 6d 61 72 79 0a 20 20 2a  ke a primary.  *
24bb9 2a 20 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65  * key out of the
24bba 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64   last column add
24bbb 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ed to the table 
24bbc 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
24bbd 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61  on..  ** So crea
24bbe 74 65 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74  te a fake list t
24bbf 6f 20 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e  o simulate this.
24bc0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73  .  */.  if( pLis
24bc1 74 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c  t==0 ){.    null
24bc2 49 64 2e 7a 20 3d 20 70 54 61 62 2d 3e 61 43 6f  Id.z = pTab->aCo
24bc3 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 5d 2e  l[pTab->nCol-1].
24bc4 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75 6c 6c 49  zName;.    nullI
24bc5 64 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  d.n = sqlite3Str
24bc6 6c 65 6e 33 30 28 28 63 68 61 72 2a 29 6e 75 6c  len30((char*)nul
24bc7 6c 49 64 2e 7a 29 3b 0a 20 20 20 20 70 4c 69 73  lId.z);.    pLis
24bc8 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
24bc9 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
24bca 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 0, 0);.    if(
24bcb 20 70 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f   pList==0 ) goto
24bcc 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
24bcd 65 78 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ex;.    sqlite3E
24bce 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70  xprListSetName(p
24bcf 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 26 6e  Parse, pList, &n
24bd0 75 6c 6c 49 64 2c 20 30 29 3b 0a 20 20 20 20 70  ullId, 0);.    p
24bd1 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f  List->a[0].sortO
24bd2 72 64 65 72 20 3d 20 28 75 38 29 73 6f 72 74 4f  rder = (u8)sortO
24bd3 72 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rder;.  }..  /* 
24bd4 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  Figure out how m
24bd5 61 6e 79 20 62 79 74 65 73 20 6f 66 20 73 70 61  any bytes of spa
24bd6 63 65 20 61 72 65 20 72 65 71 75 69 72 65 64 20  ce are required 
24bd7 74 6f 20 73 74 6f 72 65 20 65 78 70 6c 69 63 69  to store explici
24bd8 74 6c 79 0a 20 20 2a 2a 20 73 70 65 63 69 66 69  tly.  ** specifi
24bd9 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
24bda 75 65 6e 63 65 20 6e 61 6d 65 73 2e 0a 20 20 2a  uence names..  *
24bdb 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
24bdc 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
24bdd 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78  ){.    Expr *pEx
24bde 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  pr = pList->a[i]
24bdf 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20  .pExpr;.    if( 
24be0 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 43  pExpr ){.      C
24be1 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
24be2 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20  pExpr->pColl;.  
24be3 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 70 43      /* Either pC
24be4 6f 6c 6c 21 3d 30 20 6f 72 20 74 68 65 72 65 20  oll!=0 or there 
24be5 77 61 73 20 61 6e 20 4f 4f 4d 20 66 61 69 6c 75  was an OOM failu
24be6 72 65 2e 20 20 42 75 74 20 69 66 20 61 6e 20 4f  re.  But if an O
24be7 4f 4d 0a 20 20 20 20 20 20 2a 2a 20 66 61 69 6c  OM.      ** fail
24be8 75 72 65 20 77 65 20 68 61 76 65 20 71 75 69 74  ure we have quit
24be9 20 62 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67   before reaching
24bea 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 2a 2f 0a   this point. */.
24beb 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
24bec 28 70 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20  (pColl) ){.     
24bed 20 20 20 6e 45 78 74 72 61 20 2b 3d 20 28 31 20     nExtra += (1 
24bee 2b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  + sqlite3Strlen3
24bef 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 29  0(pColl->zName))
24bf0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
24bf1 20 20 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20    }..  /* .  ** 
24bf2 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 69 6e 64  Allocate the ind
24bf3 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 0a 20  ex structure. . 
24bf4 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71   */.  nName = sq
24bf5 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
24bf6 61 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70  ame);.  nCol = p
24bf7 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70  List->nExpr;.  p
24bf8 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44  Index = sqlite3D
24bf9 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
24bfa 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49 6e  .      sizeof(In
24bfb 64 65 78 29 20 2b 20 20 20 20 20 20 20 20 20 20  dex) +          
24bfc 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72      /* Index str
24bfd 75 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 20  ucture  */.     
24bfe 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f   sizeof(int)*nCo
24bff 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l +           /*
24c00 20 49 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e 20   Index.aiColumn 
24c01 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f    */.      sizeo
24c02 66 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29 20  f(int)*(nCol+1) 
24c03 2b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  +       /* Index
24c04 2e 61 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a 20  .aiRowEst   */. 
24c05 20 20 20 20 20 73 69 7a 65 6f 66 28 63 68 61 72       sizeof(char
24c06 20 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20   *)*nCol +      
24c07 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f 6c    /* Index.azCol
24c08 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73  l     */.      s
24c09 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20 2b  izeof(u8)*nCol +
24c0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
24c0b 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20  ndex.aSortOrder 
24c0c 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b  */.      nName +
24c0d 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20   1 +            
24c0e 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 7a        /* Index.z
24c0f 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20 20  Name      */.   
24c10 20 20 20 6e 45 78 74 72 61 20 20 20 20 20 20 20     nExtra       
24c11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24c12 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  /* Collation seq
24c13 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a 20  uence names */. 
24c14 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
24c15 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
24c16 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
24c17 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  te_index;.  }.  
24c18 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d  pIndex->azColl =
24c19 20 28 63 68 61 72 2a 2a 29 28 26 70 49 6e 64 65   (char**)(&pInde
24c1a 78 5b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d  x[1]);.  pIndex-
24c1b 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74  >aiColumn = (int
24c1c 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a 43   *)(&pIndex->azC
24c1d 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49  oll[nCol]);.  pI
24c1e 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 3d  ndex->aiRowEst =
24c1f 20 28 75 6e 73 69 67 6e 65 64 20 2a 29 28 26 70   (unsigned *)(&p
24c20 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  Index->aiColumn[
24c21 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78  nCol]);.  pIndex
24c22 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28  ->aSortOrder = (
24c23 75 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61  u8 *)(&pIndex->a
24c24 69 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29  iRowEst[nCol+1])
24c25 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d  ;.  pIndex->zNam
24c26 65 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49  e = (char *)(&pI
24c27 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  ndex->aSortOrder
24c28 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74 72  [nCol]);.  zExtr
24c29 61 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49  a = (char *)(&pI
24c2a 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d  ndex->zName[nNam
24c2b 65 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28  e+1]);.  memcpy(
24c2c 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a  pIndex->zName, z
24c2d 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a  Name, nName+1);.
24c2e 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65    pIndex->pTable
24c2f 20 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 65   = pTab;.  pInde
24c30 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69  x->nColumn = pLi
24c31 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e  st->nExpr;.  pIn
24c32 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 28  dex->onError = (
24c33 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 70 49  u8)onError;.  pI
24c34 6e 64 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20  ndex->autoIndex 
24c35 3d 20 28 75 38 29 28 70 4e 61 6d 65 3d 3d 30 29  = (u8)(pName==0)
24c36 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 70 53 63 68  ;.  pIndex->pSch
24c37 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  ema = db->aDb[iD
24c38 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 0a 20 20 2f  b].pSchema;..  /
24c39 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
24c3a 66 20 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e 6f  f we should hono
24c3b 72 20 44 45 53 43 20 72 65 71 75 65 73 74 73 20  r DESC requests 
24c3c 6f 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73  on index columns
24c3d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 62 2d  .  */.  if( pDb-
24c3e 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
24c3f 6f 72 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20 20  ormat>=4 ){.    
24c40 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20  sortOrderMask = 
24c41 2d 31 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20 44  -1;   /* Honor D
24c42 45 53 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a  ESC */.  }else{.
24c43 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73      sortOrderMas
24c44 6b 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67 6e  k = 0;    /* Ign
24c45 6f 72 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d 0a  ore DESC */.  }.
24c46 0a 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 6e  .  /* Scan the n
24c47 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75  ames of the colu
24c48 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  mns of the table
24c49 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61   to be indexed a
24c4a 6e 64 0a 20 20 2a 2a 20 6c 6f 61 64 20 74 68 65  nd.  ** load the
24c4b 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 20   column indices 
24c4c 69 6e 74 6f 20 74 68 65 20 49 6e 64 65 78 20 73  into the Index s
24c4d 74 72 75 63 74 75 72 65 2e 20 20 52 65 70 6f 72  tructure.  Repor
24c4e 74 20 61 6e 20 65 72 72 6f 72 0a 20 20 2a 2a 20  t an error.  ** 
24c4f 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 69 73  if any column is
24c50 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2a   not found..  **
24c51 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 20 41 64 64  .  ** TODO:  Add
24c52 20 61 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20   a test to make 
24c53 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 61  sure that the sa
24c54 6d 65 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74  me column is not
24c55 20 6e 61 6d 65 64 0a 20 20 2a 2a 20 6d 6f 72 65   named.  ** more
24c56 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 69   than once withi
24c57 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78  n the same index
24c58 2e 20 20 4f 6e 6c 79 20 74 68 65 20 66 69 72 73  .  Only the firs
24c59 74 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 20  t instance of.  
24c5a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69  ** the column wi
24c5b 6c 6c 20 65 76 65 72 20 62 65 20 75 73 65 64 20  ll ever be used 
24c5c 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72  by the optimizer
24c5d 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 75 73 69  .  Note that usi
24c5e 6e 67 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65  ng the.  ** same
24c5f 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65 20 74 68 61   column more tha
24c60 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f 74 20 62 65  n once cannot be
24c61 20 61 6e 20 65 72 72 6f 72 20 62 65 63 61 75 73   an error becaus
24c62 65 20 74 68 61 74 20 77 6f 75 6c 64 20 0a 20 20  e that would .  
24c63 2a 2a 20 62 72 65 61 6b 20 62 61 63 6b 77 61 72  ** break backwar
24c64 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ds compatibility
24c65 20 2d 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62   - it needs to b
24c66 65 20 61 20 77 61 72 6e 69 6e 67 2e 0a 20 20 2a  e a warning..  *
24c67 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 69  /.  for(i=0, pLi
24c68 73 74 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b  stItem=pList->a;
24c69 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
24c6a 20 69 2b 2b 2c 20 70 4c 69 73 74 49 74 65 6d 2b   i++, pListItem+
24c6b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  +){.    const ch
24c6c 61 72 20 2a 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70  ar *zColName = p
24c6d 4c 69 73 74 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b  ListItem->zName;
24c6e 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 54 61  .    Column *pTa
24c6f 62 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20 72 65  bCol;.    int re
24c70 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72  questedSortOrder
24c71 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c  ;.    char *zCol
24c72 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
24c73 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f       /* Collatio
24c74 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20  n sequence name 
24c75 2a 2f 0a 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c  */..    for(j=0,
24c76 20 70 54 61 62 43 6f 6c 3d 70 54 61 62 2d 3e 61   pTabCol=pTab->a
24c77 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f  Col; j<pTab->nCo
24c78 6c 3b 20 6a 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b  l; j++, pTabCol+
24c79 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  +){.      if( sq
24c7a 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 43 6f  lite3StrICmp(zCo
24c7b 6c 4e 61 6d 65 2c 20 70 54 61 62 43 6f 6c 2d 3e  lName, pTabCol->
24c7c 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61  zName)==0 ) brea
24c7d 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
24c7e 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29   j>=pTab->nCol )
24c7f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
24c80 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
24c81 22 74 61 62 6c 65 20 25 73 20 68 61 73 20 6e 6f  "table %s has no
24c82 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73   column named %s
24c83 22 2c 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d  ",.        pTab-
24c84 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65  >zName, zColName
24c85 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  );.      goto ex
24c86 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24c87 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65  .    }.    pInde
24c88 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d  x->aiColumn[i] =
24c89 20 6a 3b 0a 20 20 20 20 2f 2a 20 4a 75 73 74 69   j;.    /* Justi
24c8a 66 69 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  fication of the 
24c8b 41 4c 57 41 59 53 28 70 4c 69 73 74 49 74 65 6d  ALWAYS(pListItem
24c8c 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 3a  ->pExpr->pColl):
24c8d 20 20 42 65 63 61 75 73 65 20 6f 66 0a 20 20 20    Because of.   
24c8e 20 2a 2a 20 74 68 65 20 77 61 79 20 74 68 65 20   ** the way the 
24c8f 22 69 64 78 6c 69 73 74 22 20 6e 6f 6e 2d 74 65  "idxlist" non-te
24c90 72 6d 69 6e 61 6c 20 69 73 20 63 6f 6e 73 74 72  rminal is constr
24c91 75 63 74 65 64 20 62 79 20 74 68 65 20 70 61 72  ucted by the par
24c92 73 65 72 2c 0a 20 20 20 20 2a 2a 20 69 66 20 70  ser,.    ** if p
24c93 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20  ListItem->pExpr 
24c94 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e  is not null then
24c95 20 65 69 74 68 65 72 20 70 4c 69 73 74 49 74 65   either pListIte
24c96 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 0a  m->pExpr->pColl.
24c97 20 20 20 20 2a 2a 20 6d 75 73 74 20 65 78 69 73      ** must exis
24c98 74 20 6f 72 20 65 6c 73 65 20 74 68 65 72 65 20  t or else there 
24c99 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 61  must have been a
24c9a 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75  n OOM error.  Bu
24c9b 74 20 69 66 20 74 68 65 72 65 0a 20 20 20 20 2a  t if there.    *
24c9c 2a 20 77 61 73 20 61 6e 20 4f 4f 4d 20 65 72 72  * was an OOM err
24c9d 6f 72 2c 20 77 65 20 77 6f 75 6c 64 20 6e 65 76  or, we would nev
24c9e 65 72 20 72 65 61 63 68 20 74 68 69 73 20 70 6f  er reach this po
24c9f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  int. */.    if( 
24ca0 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72  pListItem->pExpr
24ca1 20 26 26 20 41 4c 57 41 59 53 28 70 4c 69 73 74   && ALWAYS(pList
24ca2 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f  Item->pExpr->pCo
24ca3 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ll) ){.      int
24ca4 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7a 43   nColl;.      zC
24ca5 6f 6c 6c 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d  oll = pListItem-
24ca6 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a  >pExpr->pColl->z
24ca7 4e 61 6d 65 3b 0a 20 20 20 20 20 20 6e 43 6f 6c  Name;.      nCol
24ca8 6c 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  l = sqlite3Strle
24ca9 6e 33 30 28 7a 43 6f 6c 6c 29 20 2b 20 31 3b 0a  n30(zColl) + 1;.
24caa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 45        assert( nE
24cab 78 74 72 61 3e 3d 6e 43 6f 6c 6c 20 29 3b 0a 20  xtra>=nColl );. 
24cac 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 45 78 74       memcpy(zExt
24cad 72 61 2c 20 7a 43 6f 6c 6c 2c 20 6e 43 6f 6c 6c  ra, zColl, nColl
24cae 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d  );.      zColl =
24caf 20 7a 45 78 74 72 61 3b 0a 20 20 20 20 20 20 7a   zExtra;.      z
24cb0 45 78 74 72 61 20 2b 3d 20 6e 43 6f 6c 6c 3b 0a  Extra += nColl;.
24cb1 20 20 20 20 20 20 6e 45 78 74 72 61 20 2d 3d 20        nExtra -= 
24cb2 6e 43 6f 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65  nColl;.    }else
24cb3 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20  {.      zColl = 
24cb4 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43  pTab->aCol[j].zC
24cb5 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 21  oll;.      if( !
24cb6 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  zColl ){.       
24cb7 20 7a 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   zColl = db->pDf
24cb8 6c 74 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20  ltColl->zName;. 
24cb9 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
24cba 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62   if( !db->init.b
24cbb 75 73 79 20 26 26 20 21 73 71 6c 69 74 65 33 4c  usy && !sqlite3L
24cbc 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61  ocateCollSeq(pPa
24cbd 72 73 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20  rse, zColl) ){. 
24cbe 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
24cbf 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
24cc0 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61   }.    pIndex->a
24cc1 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c  zColl[i] = zColl
24cc2 3b 0a 20 20 20 20 72 65 71 75 65 73 74 65 64 53  ;.    requestedS
24cc3 6f 72 74 4f 72 64 65 72 20 3d 20 70 4c 69 73 74  ortOrder = pList
24cc4 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20  Item->sortOrder 
24cc5 26 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b  & sortOrderMask;
24cc6 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f  .    pIndex->aSo
24cc7 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 28 75 38  rtOrder[i] = (u8
24cc8 29 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72  )requestedSortOr
24cc9 64 65 72 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  der;.  }.  sqlit
24cca 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28  e3DefaultRowEst(
24ccb 70 49 6e 64 65 78 29 3b 0a 0a 20 20 69 66 28 20  pIndex);..  if( 
24ccc 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e  pTab==pParse->pN
24ccd 65 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f  ewTable ){.    /
24cce 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68  * This routine h
24ccf 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 74  as been called t
24cd0 6f 20 63 72 65 61 74 65 20 61 6e 20 61 75 74 6f  o create an auto
24cd1 6d 61 74 69 63 20 69 6e 64 65 78 20 61 73 20 61  matic index as a
24cd2 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 20 6f  .    ** result o
24cd3 66 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  f a PRIMARY KEY 
24cd4 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73 65  or UNIQUE clause
24cd5 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 64 65 66   on a column def
24cd6 69 6e 69 74 69 6f 6e 2c 20 6f 72 0a 20 20 20 20  inition, or.    
24cd7 2a 2a 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59  ** a PRIMARY KEY
24cd8 20 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73   or UNIQUE claus
24cd9 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  e following the 
24cda 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  column definitio
24cdb 6e 73 2e 0a 20 20 20 20 2a 2a 20 69 2e 65 2e 20  ns..    ** i.e. 
24cdc 6f 6e 65 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20  one of:.    **. 
24cdd 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42     ** CREATE TAB
24cde 4c 45 20 74 28 78 20 50 52 49 4d 41 52 59 20 4b  LE t(x PRIMARY K
24cdf 45 59 2c 20 79 29 3b 0a 20 20 20 20 2a 2a 20 43  EY, y);.    ** C
24ce0 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 2c  REATE TABLE t(x,
24ce1 20 79 2c 20 55 4e 49 51 55 45 28 78 2c 20 79 29   y, UNIQUE(x, y)
24ce2 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  );.    **.    **
24ce3 20 45 69 74 68 65 72 20 77 61 79 2c 20 63 68 65   Either way, che
24ce4 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
24ce5 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68   table already h
24ce6 61 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65 78  as such an index
24ce7 2e 20 49 66 0a 20 20 20 20 2a 2a 20 73 6f 2c 20  . If.    ** so, 
24ce8 64 6f 6e 27 74 20 62 6f 74 68 65 72 20 63 72 65  don't bother cre
24ce9 61 74 69 6e 67 20 74 68 69 73 20 6f 6e 65 2e 20  ating this one. 
24cea 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65  This only applie
24ceb 73 20 74 6f 0a 20 20 20 20 2a 2a 20 61 75 74 6f  s to.    ** auto
24cec 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65  matically create
24ced 64 20 69 6e 64 69 63 65 73 2e 20 55 73 65 72 73  d indices. Users
24cee 20 63 61 6e 20 64 6f 20 61 73 20 74 68 65 79 20   can do as they 
24cef 77 69 73 68 20 77 69 74 68 0a 20 20 20 20 2a 2a  wish with.    **
24cf0 20 65 78 70 6c 69 63 69 74 20 69 6e 64 69 63 65   explicit indice
24cf1 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s..    **.    **
24cf2 20 54 77 6f 20 55 4e 49 51 55 45 20 6f 72 20 50   Two UNIQUE or P
24cf3 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73 74  RIMARY KEY const
24cf4 72 61 69 6e 74 73 20 61 72 65 20 63 6f 6e 73 69  raints are consi
24cf5 64 65 72 65 64 20 65 71 75 69 76 61 6c 65 6e 74  dered equivalent
24cf6 0a 20 20 20 20 2a 2a 20 28 61 6e 64 20 74 68 75  .    ** (and thu
24cf7 73 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68  s suppressing th
24cf8 65 20 73 65 63 6f 6e 64 20 6f 6e 65 29 20 65 76  e second one) ev
24cf9 65 6e 20 69 66 20 74 68 65 79 20 68 61 76 65 20  en if they have 
24cfa 64 69 66 66 65 72 65 6e 74 0a 20 20 20 20 2a 2a  different.    **
24cfb 20 73 6f 72 74 20 6f 72 64 65 72 73 2e 0a 20 20   sort orders..  
24cfc 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74    **.    ** If t
24cfd 68 65 72 65 20 61 72 65 20 64 69 66 66 65 72 65  here are differe
24cfe 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  nt collating seq
24cff 75 65 6e 63 65 73 20 6f 72 20 69 66 20 74 68 65  uences or if the
24d00 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 20 20   columns of.    
24d01 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  ** the constrain
24d02 74 20 6f 63 63 75 72 20 69 6e 20 64 69 66 66 65  t occur in diffe
24d03 72 65 6e 74 20 6f 72 64 65 72 73 2c 20 74 68 65  rent orders, the
24d04 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  n the constraint
24d05 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 63 6f 6e  s are.    ** con
24d06 73 69 64 65 72 65 64 20 64 69 73 74 69 6e 63 74  sidered distinct
24d07 20 61 6e 64 20 62 6f 74 68 20 72 65 73 75 6c 74   and both result
24d08 20 69 6e 20 73 65 70 61 72 61 74 65 20 69 6e 64   in separate ind
24d09 69 63 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ices..    */.   
24d0a 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
24d0b 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
24d0c 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
24d0d 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
24d0e 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20  {.      int k;. 
24d0f 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
24d10 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
24d11 6f 6e 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73  one );.      ass
24d12 65 72 74 28 20 70 49 64 78 2d 3e 61 75 74 6f 49  ert( pIdx->autoI
24d13 6e 64 65 78 20 29 3b 0a 20 20 20 20 20 20 61 73  ndex );.      as
24d14 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 6f 6e  sert( pIndex->on
24d15 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29  Error!=OE_None )
24d16 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  ;..      if( pId
24d17 78 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 49 6e 64  x->nColumn!=pInd
24d18 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 20 63 6f  ex->nColumn ) co
24d19 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 66 6f  ntinue;.      fo
24d1a 72 28 6b 3d 30 3b 20 6b 3c 70 49 64 78 2d 3e 6e  r(k=0; k<pIdx->n
24d1b 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 29 7b 0a 20 20  Column; k++){.  
24d1c 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
24d1d 20 2a 7a 31 3b 0a 20 20 20 20 20 20 20 20 63 6f   *z1;.        co
24d1e 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20  nst char *z2;.  
24d1f 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
24d20 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e  aiColumn[k]!=pIn
24d21 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d  dex->aiColumn[k]
24d22 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
24d23 20 20 7a 31 20 3d 20 70 49 64 78 2d 3e 61 7a 43    z1 = pIdx->azC
24d24 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20  oll[k];.        
24d25 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43  z2 = pIndex->azC
24d26 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20  oll[k];.        
24d27 69 66 28 20 7a 31 21 3d 7a 32 20 26 26 20 73 71  if( z1!=z2 && sq
24d28 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31 2c  lite3StrICmp(z1,
24d29 20 7a 32 29 20 29 20 62 72 65 61 6b 3b 0a 20 20   z2) ) break;.  
24d2a 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
24d2b 6b 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  k==pIdx->nColumn
24d2c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
24d2d 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70  pIdx->onError!=p
24d2e 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29  Index->onError )
24d2f 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
24d30 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  his constraint c
24d31 72 65 61 74 65 73 20 74 68 65 20 73 61 6d 65 20  reates the same 
24d32 69 6e 64 65 78 20 61 73 20 61 20 70 72 65 76 69  index as a previ
24d33 6f 75 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ous.          **
24d34 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 70 65 63   constraint spec
24d35 69 66 69 65 64 20 73 6f 6d 65 77 68 65 72 65 20  ified somewhere 
24d36 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41  in the CREATE TA
24d37 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20  BLE statement.. 
24d38 20 20 20 20 20 20 20 20 20 2a 2a 20 48 6f 77 65           ** Howe
24d39 76 65 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c  ver the ON CONFL
24d3a 49 43 54 20 63 6c 61 75 73 65 73 20 61 72 65 20  ICT clauses are 
24d3b 64 69 66 66 65 72 65 6e 74 2e 20 49 66 20 62 6f  different. If bo
24d3c 74 68 20 74 68 69 73 20 0a 20 20 20 20 20 20 20  th this .       
24d3d 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74     ** constraint
24d3e 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75   and the previou
24d3f 73 20 65 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e  s equivalent con
24d40 73 74 72 61 69 6e 74 20 68 61 76 65 20 65 78 70  straint have exp
24d41 6c 69 63 69 74 0a 20 20 20 20 20 20 20 20 20 20  licit.          
24d42 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63  ** ON CONFLICT c
24d43 6c 61 75 73 65 73 20 74 68 69 73 20 69 73 20 61  lauses this is a
24d44 6e 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69  n error. Otherwi
24d45 73 65 2c 20 75 73 65 20 74 68 65 0a 20 20 20 20  se, use the.    
24d46 20 20 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69        ** explici
24d47 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 65  tly specified be
24d48 68 61 76 69 6f 75 72 20 66 6f 72 20 74 68 65 20  haviour for the 
24d49 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20 20  index..         
24d4a 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66   */.          if
24d4b 28 20 21 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  ( !(pIdx->onErro
24d4c 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c  r==OE_Default ||
24d4d 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72   pIndex->onError
24d4e 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29 20 29 7b  ==OE_Default) ){
24d4f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
24d50 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
24d51 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
24d52 20 20 20 20 20 20 22 63 6f 6e 66 6c 69 63 74 69        "conflicti
24d53 6e 67 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63  ng ON CONFLICT c
24d54 6c 61 75 73 65 73 20 73 70 65 63 69 66 69 65 64  lauses specified
24d55 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  ", 0);.         
24d56 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
24d57 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d   pIdx->onError==
24d58 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
24d59 20 20 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e            pIdx->
24d5a 6f 6e 45 72 72 6f 72 20 3d 20 70 49 6e 64 65 78  onError = pIndex
24d5b 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20  ->onError;.     
24d5c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
24d5d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78  .        goto ex
24d5e 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24d5f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
24d60 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68   }..  /* Link th
24d61 65 20 6e 65 77 20 49 6e 64 65 78 20 73 74 72 75  e new Index stru
24d62 63 74 75 72 65 20 74 6f 20 69 74 73 20 74 61 62  cture to its tab
24d63 6c 65 20 61 6e 64 20 74 6f 20 74 68 65 20 6f 74  le and to the ot
24d64 68 65 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f  her.  ** in-memo
24d65 72 79 20 64 61 74 61 62 61 73 65 20 73 74 72 75  ry database stru
24d66 63 74 75 72 65 73 2e 20 0a 20 20 2a 2f 0a 20 20  ctures. .  */.  
24d67 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
24d68 79 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  y ){.    Index *
24d69 70 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  p;.    p = sqlit
24d6a 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 49  e3HashInsert(&pI
24d6b 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69  ndex->pSchema->i
24d6c 64 78 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20  dxHash, .       
24d6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d6e 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65     pIndex->zName
24d6f 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
24d70 30 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29  0(pIndex->zName)
24d71 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
24d72 20 20 20 20 20 20 20 20 20 20 20 20 70 49 6e 64              pInd
24d73 65 78 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29  ex);.    if( p )
24d74 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
24d75 70 3d 3d 70 49 6e 64 65 78 20 29 3b 20 20 2f 2a  p==pIndex );  /*
24d76 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76   Malloc must hav
24d77 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20 20  e failed */.    
24d78 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
24d79 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 67 6f  ed = 1;.      go
24d7a 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24d7b 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
24d7c 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
24d7d 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
24d7e 73 3b 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e  s;.    if( pTblN
24d7f 61 6d 65 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ame!=0 ){.      
24d80 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20 64  pIndex->tnum = d
24d81 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b  b->init.newTnum;
24d82 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
24d83 20 49 66 20 74 68 65 20 64 62 2d 3e 69 6e 69 74   If the db->init
24d84 2e 62 75 73 79 20 69 73 20 30 20 74 68 65 6e 20  .busy is 0 then 
24d85 63 72 65 61 74 65 20 74 68 65 20 69 6e 64 65 78  create the index
24d86 20 6f 6e 20 64 69 73 6b 2e 20 20 54 68 69 73 0a   on disk.  This.
24d87 20 20 2a 2a 20 69 6e 76 6f 6c 76 65 73 20 77 72    ** involves wr
24d88 69 74 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20  iting the index 
24d89 69 6e 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20  into the master 
24d8a 74 61 62 6c 65 20 61 6e 64 20 66 69 6c 6c 69 6e  table and fillin
24d8b 67 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 69 6e  g in the.  ** in
24d8c 64 65 78 20 77 69 74 68 20 74 68 65 20 63 75 72  dex with the cur
24d8d 72 65 6e 74 20 74 61 62 6c 65 20 63 6f 6e 74 65  rent table conte
24d8e 6e 74 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  nts..  **.  ** T
24d8f 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  he db->init.busy
24d90 20 69 73 20 30 20 77 68 65 6e 20 74 68 65 20 75   is 0 when the u
24d91 73 65 72 20 66 69 72 73 74 20 65 6e 74 65 72 73  ser first enters
24d92 20 61 20 43 52 45 41 54 45 20 49 4e 44 45 58 20   a CREATE INDEX 
24d93 0a 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20  .  ** command.  
24d94 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73  db->init.busy is
24d95 20 31 20 77 68 65 6e 20 61 20 64 61 74 61 62 61   1 when a databa
24d96 73 65 20 69 73 20 6f 70 65 6e 65 64 20 61 6e 64  se is opened and
24d97 20 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 49 4e   .  ** CREATE IN
24d98 44 45 58 20 73 74 61 74 65 6d 65 6e 74 73 20 61  DEX statements a
24d99 72 65 20 72 65 61 64 20 6f 75 74 20 6f 66 20 74  re read out of t
24d9a 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  he master table.
24d9b 20 20 49 6e 0a 20 20 2a 2a 20 74 68 65 20 6c 61    In.  ** the la
24d9c 74 74 65 72 20 63 61 73 65 20 74 68 65 20 69 6e  tter case the in
24d9d 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69 73  dex already exis
24d9e 74 73 20 6f 6e 20 64 69 73 6b 2c 20 77 68 69 63  ts on disk, whic
24d9f 68 20 69 73 20 77 68 79 0a 20 20 2a 2a 20 77 65  h is why.  ** we
24da0 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 72   don't want to r
24da1 65 63 72 65 61 74 65 20 69 74 2e 0a 20 20 2a 2a  ecreate it..  **
24da2 0a 20 20 2a 2a 20 49 66 20 70 54 62 6c 4e 61 6d  .  ** If pTblNam
24da3 65 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 68  e==0 it means th
24da4 69 73 20 69 6e 64 65 78 20 69 73 20 67 65 6e 65  is index is gene
24da5 72 61 74 65 64 20 61 73 20 61 20 70 72 69 6d 61  rated as a prima
24da6 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 72 20 55  ry key.  ** or U
24da7 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
24da8 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 41 42   of a CREATE TAB
24da9 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53  LE statement.  S
24daa 69 6e 63 65 20 74 68 65 20 74 61 62 6c 65 0a 20  ince the table. 
24dab 20 2a 2a 20 68 61 73 20 6a 75 73 74 20 62 65 65   ** has just bee
24dac 6e 20 63 72 65 61 74 65 64 2c 20 69 74 20 63 6f  n created, it co
24dad 6e 74 61 69 6e 73 20 6e 6f 20 64 61 74 61 20 61  ntains no data a
24dae 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e 69  nd the index ini
24daf 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a  tialization.  **
24db0 20 73 74 65 70 20 63 61 6e 20 62 65 20 73 6b 69   step can be ski
24db1 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73  pped..  */.  els
24db2 65 7b 20 2f 2a 20 69 66 28 20 64 62 2d 3e 69 6e  e{ /* if( db->in
24db3 69 74 2e 62 75 73 79 3d 3d 30 20 29 20 2a 2f 0a  it.busy==0 ) */.
24db4 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20      Vdbe *v;.   
24db5 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20   char *zStmt;.  
24db6 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70    int iMem = ++p
24db7 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20  Parse->nMem;..  
24db8 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
24db9 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
24dba 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74    if( v==0 ) got
24dbb 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24dbc 64 65 78 3b 0a 0a 0a 20 20 20 20 2f 2a 20 43 72  dex;...    /* Cr
24dbd 65 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61 67  eate the rootpag
24dbe 65 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a  e for the index.
24dbf 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
24dc0 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
24dc1 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c  ation(pParse, 1,
24dc2 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
24dc3 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
24dc4 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 2c 20  OP_CreateIndex, 
24dc5 69 44 62 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20  iDb, iMem);..   
24dc6 20 2f 2a 20 47 61 74 68 65 72 20 74 68 65 20 63   /* Gather the c
24dc7 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20  omplete text of 
24dc8 74 68 65 20 43 52 45 41 54 45 20 49 4e 44 45 58  the CREATE INDEX
24dc9 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f 0a   statement into.
24dca 20 20 20 20 2a 2a 20 74 68 65 20 7a 53 74 6d 74      ** the zStmt
24dcb 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2f   variable.    */
24dcc 0a 20 20 20 20 69 66 28 20 70 53 74 61 72 74 20  .    if( pStart 
24dcd 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
24dce 20 70 45 6e 64 21 3d 30 20 29 3b 0a 20 20 20 20   pEnd!=0 );.    
24dcf 20 20 2f 2a 20 41 20 6e 61 6d 65 64 20 69 6e 64    /* A named ind
24dd0 65 78 20 77 69 74 68 20 61 6e 20 65 78 70 6c 69  ex with an expli
24dd1 63 69 74 20 43 52 45 41 54 45 20 49 4e 44 45 58  cit CREATE INDEX
24dd2 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
24dd3 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69      zStmt = sqli
24dd4 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
24dd5 43 52 45 41 54 45 25 73 20 49 4e 44 45 58 20 25  CREATE%s INDEX %
24dd6 2e 2a 73 22 2c 0a 20 20 20 20 20 20 20 20 6f 6e  .*s",.        on
24dd7 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f  Error==OE_None ?
24dd8 20 22 22 20 3a 20 22 20 55 4e 49 51 55 45 22 2c   "" : " UNIQUE",
24dd9 0a 20 20 20 20 20 20 20 20 70 45 6e 64 2d 3e 7a  .        pEnd->z
24dda 20 2d 20 70 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c   - pName->z + 1,
24ddb 0a 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e  .        pName->
24ddc 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  z);.    }else{. 
24ddd 20 20 20 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d       /* An autom
24dde 61 74 69 63 20 69 6e 64 65 78 20 63 72 65 61 74  atic index creat
24ddf 65 64 20 62 79 20 61 20 50 52 49 4d 41 52 59 20  ed by a PRIMARY 
24de0 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6f  KEY or UNIQUE co
24de1 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20  nstraint */.    
24de2 20 20 2f 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c    /* zStmt = sql
24de3 69 74 65 33 4d 50 72 69 6e 74 66 28 22 22 29 3b  ite3MPrintf("");
24de4 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20   */.      zStmt 
24de5 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 0;.    }..    
24de6 2f 2a 20 41 64 64 20 61 6e 20 65 6e 74 72 79 20  /* Add an entry 
24de7 69 6e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  in sqlite_master
24de8 20 66 6f 72 20 74 68 69 73 20 69 6e 64 65 78 0a   for this index.
24de9 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
24dea 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
24deb 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22  arse, .        "
24dec 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e 25  INSERT INTO %Q.%
24ded 73 20 56 41 4c 55 45 53 28 27 69 6e 64 65 78 27  s VALUES('index'
24dee 2c 25 51 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22  ,%Q,%Q,#%d,%Q);"
24def 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44  ,.        db->aD
24df0 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43  b[iDb].zName, SC
24df1 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
24df2 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d  .        pIndex-
24df3 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  >zName,.        
24df4 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  pTab->zName,.   
24df5 20 20 20 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20       iMem,.     
24df6 20 20 20 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a     zStmt.    );.
24df7 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
24df8 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20  e(db, zStmt);.. 
24df9 20 20 20 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69     /* Fill the i
24dfa 6e 64 65 78 20 77 69 74 68 20 64 61 74 61 20 61  ndex with data a
24dfb 6e 64 20 72 65 70 61 72 73 65 20 74 68 65 20 73  nd reparse the s
24dfc 63 68 65 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f  chema. Code an O
24dfd 50 5f 45 78 70 69 72 65 0a 20 20 20 20 2a 2a 20  P_Expire.    ** 
24dfe 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c  to invalidate al
24dff 6c 20 70 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73  l pre-compiled s
24e00 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a  tatements..    *
24e01 2f 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e 61  /.    if( pTblNa
24e02 6d 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  me ){.      sqli
24e03 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 70  te3RefillIndex(p
24e04 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69  Parse, pIndex, i
24e05 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Mem);.      sqli
24e06 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28  te3ChangeCookie(
24e07 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
24e08 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24e09 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73  ddOp4(v, OP_Pars
24e0a 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c  eSchema, iDb, 0,
24e0b 20 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c   0,.         sql
24e0c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
24e0d 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70 49 6e  "name='%q'", pIn
24e0e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f  dex->zName), P4_
24e0f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
24e10 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24e11 31 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20  1(v, OP_Expire, 
24e12 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  0);.    }.  }.. 
24e13 20 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20   /* When adding 
24e14 61 6e 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20  an index to the 
24e15 6c 69 73 74 20 6f 66 20 69 6e 64 69 63 65 73 20  list of indices 
24e16 66 6f 72 20 61 20 74 61 62 6c 65 2c 20 6d 61 6b  for a table, mak
24e17 65 0a 20 20 2a 2a 20 73 75 72 65 20 61 6c 6c 20  e.  ** sure all 
24e18 69 6e 64 69 63 65 73 20 6c 61 62 65 6c 65 64 20  indices labeled 
24e19 4f 45 5f 52 65 70 6c 61 63 65 20 63 6f 6d 65 20  OE_Replace come 
24e1a 61 66 74 65 72 20 61 6c 6c 20 74 68 6f 73 65 20  after all those 
24e1b 6c 61 62 65 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f  labeled.  ** OE_
24e1c 49 67 6e 6f 72 65 2e 20 20 54 68 69 73 20 69 73  Ignore.  This is
24e1d 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 74   necessary for t
24e1e 68 65 20 63 6f 72 72 65 63 74 20 63 6f 6e 73 74  he correct const
24e1f 72 61 69 6e 74 20 63 68 65 63 6b 0a 20 20 2a 2a  raint check.  **
24e20 20 70 72 6f 63 65 73 73 69 6e 67 20 28 69 6e 20   processing (in 
24e21 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43  sqlite3GenerateC
24e22 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28  onstraintChecks(
24e23 29 29 20 61 73 20 70 61 72 74 20 6f 66 0a 20 20  )) as part of.  
24e24 2a 2a 20 55 50 44 41 54 45 20 61 6e 64 20 49 4e  ** UPDATE and IN
24e25 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  SERT statements.
24e26 20 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62    .  */.  if( db
24e27 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 70  ->init.busy || p
24e28 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  TblName==0 ){.  
24e29 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f    if( onError!=O
24e2a 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54 61  E_Replace || pTa
24e2b 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 20 20  b->pIndex==0.   
24e2c 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d 3e 70        || pTab->p
24e2d 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d  Index->onError==
24e2e 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20 20 20  OE_Replace){.   
24e2f 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74     pIndex->pNext
24e30 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b   = pTab->pIndex;
24e31 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 49 6e  .      pTab->pIn
24e32 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20  dex = pIndex;.  
24e33 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49    }else{.      I
24e34 6e 64 65 78 20 2a 70 4f 74 68 65 72 20 3d 20 70  ndex *pOther = p
24e35 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20  Tab->pIndex;.   
24e36 20 20 20 77 68 69 6c 65 28 20 70 4f 74 68 65 72     while( pOther
24e37 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f 74 68 65  ->pNext && pOthe
24e38 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 72 6f  r->pNext->onErro
24e39 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b  r!=OE_Replace ){
24e3a 0a 20 20 20 20 20 20 20 20 70 4f 74 68 65 72 20  .        pOther 
24e3b 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b  = pOther->pNext;
24e3c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
24e3d 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70  Index->pNext = p
24e3e 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20  Other->pNext;.  
24e3f 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78      pOther->pNex
24e40 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20  t = pIndex;.    
24e41 7d 0a 20 20 20 20 70 52 65 74 20 3d 20 70 49 6e  }.    pRet = pIn
24e42 64 65 78 3b 0a 20 20 20 20 70 49 6e 64 65 78 20  dex;.    pIndex 
24e43 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  = 0;.  }..  /* C
24e44 6c 65 61 6e 20 75 70 20 62 65 66 6f 72 65 20 65  lean up before e
24e45 78 69 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63  xiting */.exit_c
24e46 72 65 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69  reate_index:.  i
24e47 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  f( pIndex ){.   
24e48 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49   sqlite3_free(pI
24e49 6e 64 65 78 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a  ndex->zColAff);.
24e4a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
24e4b 65 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0a 20  e(db, pIndex);. 
24e4c 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
24e4d 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
24e4e 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
24e4f 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
24e50 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 73  , pTblName);.  s
24e51 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
24e52 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72   zName);.  retur
24e53 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pRet;.}../*.**
24e54 20 46 69 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e   Fill the Index.
24e55 61 69 52 6f 77 45 73 74 5b 5d 20 61 72 72 61 79  aiRowEst[] array
24e56 20 77 69 74 68 20 64 65 66 61 75 6c 74 20 69 6e   with default in
24e57 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f  formation - info
24e58 72 6d 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65  rmation.** to be
24e59 20 75 73 65 64 20 77 68 65 6e 20 77 65 20 68 61   used when we ha
24e5a 76 65 20 6e 6f 74 20 72 75 6e 20 74 68 65 20 41  ve not run the A
24e5b 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a  NALYZE command..
24e5c 2a 2a 0a 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30  **.** aiRowEst[0
24e5d 5d 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f 20  ] is suppose to 
24e5e 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62  contain the numb
24e5f 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69  er of elements i
24e60 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20  n the index..** 
24e61 53 69 6e 63 65 20 77 65 20 64 6f 20 6e 6f 74 20  Since we do not 
24e62 6b 6e 6f 77 2c 20 67 75 65 73 73 20 31 20 6d 69  know, guess 1 mi
24e63 6c 6c 69 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74  llion.  aiRowEst
24e64 5b 31 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61  [1] is an estima
24e65 74 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d  te of the.** num
24e66 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74  ber of rows in t
24e67 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61  he table that ma
24e68 74 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c  tch any particul
24e69 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a  ar value of the.
24e6a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20  ** first column 
24e6b 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 61  of the index.  a
24e6c 69 52 6f 77 45 73 74 5b 32 5d 20 69 73 20 61 6e  iRowEst[2] is an
24e6d 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65   estimate of the
24e6e 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f   number.** of ro
24e6f 77 73 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e  ws that match an
24e70 79 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6d  y particular com
24e71 62 69 6e 69 61 74 69 6f 6e 20 6f 66 20 74 68 65  biniation of the
24e72 20 66 69 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73   first 2 columns
24e73 0a 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78  .** of the index
24e74 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e  .  And so forth.
24e75 20 20 49 74 20 6d 75 73 74 20 61 6c 77 61 79 73    It must always
24e76 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61   be the case tha
24e77 74 0a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  t.*.**          
24e78 20 61 69 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69   aiRowEst[N]<=ai
24e79 52 6f 77 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20  RowEst[N-1].**  
24e7a 20 20 20 20 20 20 20 20 20 61 69 52 6f 77 45 73           aiRowEs
24e7b 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70  t[N]>=1.**.** Ap
24e7c 61 72 74 20 66 72 6f 6d 20 74 68 61 74 2c 20 77  art from that, w
24e7d 65 20 68 61 76 65 20 6c 69 74 74 6c 65 20 74 6f  e have little to
24e7e 20 67 6f 20 6f 6e 20 62 65 73 69 64 65 73 20 69   go on besides i
24e7f 6e 74 75 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a  ntuition as to.*
24e80 2a 20 68 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d  * how aiRowEst[]
24e81 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69   should be initi
24e82 61 6c 69 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d  alized.  The num
24e83 62 65 72 73 20 67 65 6e 65 72 61 74 65 64 20 68  bers generated h
24e84 65 72 65 0a 2a 2a 20 61 72 65 20 62 61 73 65 64  ere.** are based
24e85 20 6f 6e 20 74 79 70 69 63 61 6c 20 76 61 6c 75   on typical valu
24e86 65 73 20 66 6f 75 6e 64 20 69 6e 20 61 63 74 75  es found in actu
24e87 61 6c 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53  al indices..*/.S
24e88 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
24e89 69 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c  id sqlite3Defaul
24e8a 74 52 6f 77 45 73 74 28 49 6e 64 65 78 20 2a 70  tRowEst(Index *p
24e8b 49 64 78 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  Idx){.  unsigned
24e8c 20 2a 61 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f   *a = pIdx->aiRo
24e8d 77 45 73 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  wEst;.  int i;. 
24e8e 20 61 73 73 65 72 74 28 20 61 21 3d 30 20 29 3b   assert( a!=0 );
24e8f 0a 20 20 61 5b 30 5d 20 3d 20 31 30 30 30 30 30  .  a[0] = 100000
24e90 30 3b 0a 20 20 66 6f 72 28 69 3d 70 49 64 78 2d  0;.  for(i=pIdx-
24e91 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20  >nColumn; i>=5; 
24e92 69 2d 2d 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d  i--){.    a[i] =
24e93 20 35 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28   5;.  }.  while(
24e94 20 69 3e 3d 31 20 29 7b 0a 20 20 20 20 61 5b 69   i>=1 ){.    a[i
24e95 5d 20 3d 20 31 31 20 2d 20 69 3b 0a 20 20 20 20  ] = 11 - i;.    
24e96 69 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  i--;.  }.  if( p
24e97 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45  Idx->onError!=OE
24e98 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 61 5b 70  _None ){.    a[p
24e99 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20  Idx->nColumn] = 
24e9a 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
24e9b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
24e9c 6c 20 64 72 6f 70 20 61 6e 20 65 78 69 73 74 69  l drop an existi
24e9d 6e 67 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20  ng named index. 
24e9e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
24e9f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
24ea0 44 52 4f 50 20 49 4e 44 45 58 20 73 74 61 74 65  DROP INDEX state
24ea1 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
24ea2 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
24ea3 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61  ite3DropIndex(Pa
24ea4 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63  rse *pParse, Src
24ea5 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74  List *pName, int
24ea6 20 69 66 45 78 69 73 74 73 29 7b 0a 20 20 49 6e   ifExists){.  In
24ea7 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 56  dex *pIndex;.  V
24ea8 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65  dbe *v;.  sqlite
24ea9 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
24eaa 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a  db;.  int iDb;..
24eab 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
24eac 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 20 20 2f  ->nErr==0 );   /
24ead 2a 20 4e 65 76 65 72 20 63 61 6c 6c 65 64 20 77  * Never called w
24eae 69 74 68 20 70 72 69 6f 72 20 65 72 72 6f 72 73  ith prior errors
24eaf 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   */.  if( db->ma
24eb0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
24eb1 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
24eb2 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61 73  _index;.  }.  as
24eb3 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72  sert( pName->nSr
24eb4 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53 51  c==1 );.  if( SQ
24eb5 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
24eb6 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
24eb7 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  e) ){.    goto e
24eb8 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
24eb9 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73    }.  pIndex = s
24eba 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
24ebb 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e  db, pName->a[0].
24ebc 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b  zName, pName->a[
24ebd 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
24ebe 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29   if( pIndex==0 )
24ebf 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78 69  {.    if( !ifExi
24ec0 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  sts ){.      sql
24ec1 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
24ec2 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e  rse, "no such in
24ec3 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65 2c  dex: %S", pName,
24ec4 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70   0);.    }.    p
24ec5 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65  Parse->checkSche
24ec6 6d 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f  ma = 1;.    goto
24ec7 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
24ec8 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 64  ;.  }.  if( pInd
24ec9 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 7b  ex->autoIndex ){
24eca 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
24ecb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e  rMsg(pParse, "in
24ecc 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
24ecd 69 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20 20  ith UNIQUE ".   
24ece 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20 4b     "or PRIMARY K
24ecf 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61  EY constraint ca
24ed0 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22  nnot be dropped"
24ed1 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  , 0);.    goto e
24ed2 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
24ed3 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
24ed4 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
24ed5 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  (db, pIndex->pSc
24ed6 68 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20 53  hema);.#ifndef S
24ed7 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
24ed8 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
24ed9 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49   int code = SQLI
24eda 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a 20  TE_DROP_INDEX;. 
24edb 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
24edc 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b   pIndex->pTable;
24edd 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
24ede 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *zDb = db->aDb[i
24edf 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
24ee0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20  onst char *zTab 
24ee1 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
24ee2 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Db);.    if( sql
24ee3 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
24ee4 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c  arse, SQLITE_DEL
24ee5 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44  ETE, zTab, 0, zD
24ee6 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
24ee7 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
24ee8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
24ee9 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
24eea 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  iDb ) code = SQL
24eeb 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e  ITE_DROP_TEMP_IN
24eec 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  DEX;.    if( sql
24eed 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
24eee 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e 64  arse, code, pInd
24eef 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  ex->zName, pTab-
24ef0 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a  >zName, zDb) ){.
24ef1 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
24ef2 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20  drop_index;.    
24ef3 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
24ef4 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
24ef5 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 69   to remove the i
24ef6 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74 68  ndex and from th
24ef7 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a  e master table *
24ef8 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
24ef9 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
24efa 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
24efb 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
24efc 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
24efd 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 1, iDb);.    s
24efe 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
24eff 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
24f00 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51   "DELETE FROM %Q
24f01 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  .%s WHERE name=%
24f02 51 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61  Q",.       db->a
24f03 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
24f04 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
24f05 2c 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d  ,.       pIndex-
24f06 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20  >zName.    );.  
24f07 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
24f08 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69  dTable(db, "sqli
24f09 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e 61  te_stat1", db->a
24f0a 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29  Db[iDb].zName) )
24f0b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e  {.      sqlite3N
24f0c 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
24f0d 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45  e,.        "DELE
24f0e 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74  TE FROM %Q.sqlit
24f0f 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 69 64  e_stat1 WHERE id
24f10 78 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 64  x=%Q",.        d
24f11 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
24f12 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  e, pIndex->zName
24f13 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a  .      );.    }.
24f14 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
24f15 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
24f16 69 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72 6f  iDb);.    destro
24f17 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
24f18 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c 20  , pIndex->tnum, 
24f19 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
24f1a 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
24f1b 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44 62  P_DropIndex, iDb
24f1c 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e  , 0, 0, pIndex->
24f1d 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 0a  zName, 0);.  }..
24f1e 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3a  exit_drop_index:
24f1f 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
24f20 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d  tDelete(db, pNam
24f21 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41 72  e);.}../*.** pAr
24f22 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ray is a pointer
24f23 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20   to an array of 
24f24 6f 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20 6f  objects.  Each o
24f25 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20  bject in the.** 
24f26 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72 79  array is szEntry
24f27 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
24f28 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   This routine al
24f29 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
24f2a 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65 20 65   object on the e
24f2b 6e 64 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e  nd of the array.
24f2c 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79 20  .**.** *pnEntry 
24f2d 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
24f2e 20 65 6e 74 72 69 65 73 20 61 6c 72 65 61 64 79   entries already
24f2f 20 69 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c 6c   in use.  *pnAll
24f30 6f 63 20 69 73 0a 2a 2a 20 74 68 65 20 70 72 65  oc is.** the pre
24f31 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
24f32 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72  d size of the ar
24f33 72 61 79 2e 20 20 69 6e 69 74 53 69 7a 65 20 69  ray.  initSize i
24f34 73 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74  s the.** suggest
24f35 65 64 20 69 6e 69 74 69 61 6c 20 61 72 72 61 79  ed initial array
24f36 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   size allocation
24f37 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65  ..**.** The inde
24f38 78 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74  x of the new ent
24f39 72 79 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ry is returned i
24f3a 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 54  n *pIdx..**.** T
24f3b 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
24f3c 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
24f3d 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 62   the array of ob
24f3e 6a 65 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a 20  jects.  This.** 
24f3f 6d 69 67 68 74 20 62 65 20 74 68 65 20 73 61 6d  might be the sam
24f40 65 20 61 73 20 74 68 65 20 70 41 72 72 61 79 20  e as the pArray 
24f41 70 61 72 61 6d 65 74 65 72 20 6f 72 20 69 74 20  parameter or it 
24f42 6d 69 67 68 74 20 62 65 20 61 20 64 69 66 66 65  might be a diffe
24f43 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  rent.** pointer 
24f44 69 66 20 74 68 65 20 61 72 72 61 79 20 77 61 73  if the array was
24f45 20 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c   resized..*/.SQL
24f46 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
24f47 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c   *sqlite3ArrayAl
24f48 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74 65  locate(.  sqlite
24f49 33 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 43  3 *db,      /* C
24f4a 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74  onnection to not
24f4b 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61  ify of malloc fa
24f4c 69 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 64  ilures */.  void
24f4d 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f 2a   *pArray,     /*
24f4e 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74   Array of object
24f4f 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65 61  s.  Might be rea
24f50 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 6e  llocated */.  in
24f51 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 20  t szEntry,      
24f52 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20  /* Size of each 
24f53 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61 72  object in the ar
24f54 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 69  ray */.  int ini
24f55 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53 75  tSize,     /* Su
24f56 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20  ggested initial 
24f57 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65  allocation, in e
24f58 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  lements */.  int
24f59 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20 2f   *pnEntry,     /
24f5a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65  * Number of obje
24f5b 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  cts currently in
24f5c 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70   use */.  int *p
24f5d 6e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20 43  nAlloc,     /* C
24f5e 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74  urrent size of t
24f5f 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  he allocation, i
24f60 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  n elements */.  
24f61 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20 20  int *pIdx       
24f62 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 69    /* Write the i
24f63 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73 6c  ndex of a new sl
24f64 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ot here */.){.  
24f65 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 2a  char *z;.  if( *
24f66 70 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41 6c  pnEntry >= *pnAl
24f67 6c 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  loc ){.    void 
24f68 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e  *pNew;.    int n
24f69 65 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77 53  ewSize;.    newS
24f6a 69 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63 29  ize = (*pnAlloc)
24f6b 2a 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a 20  *2 + initSize;. 
24f6c 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
24f6d 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70  3DbRealloc(db, p
24f6e 41 72 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a 73  Array, newSize*s
24f6f 7a 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28  zEntry);.    if(
24f70 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
24f71 20 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20 20    *pIdx = -1;.  
24f72 20 20 20 20 72 65 74 75 72 6e 20 70 41 72 72 61      return pArra
24f73 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 6e  y;.    }.    *pn
24f74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  Alloc = sqlite3D
24f75 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
24f76 70 4e 65 77 29 2f 73 7a 45 6e 74 72 79 3b 0a 20  pNew)/szEntry;. 
24f77 20 20 20 70 41 72 72 61 79 20 3d 20 70 4e 65 77     pArray = pNew
24f78 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61  ;.  }.  z = (cha
24f79 72 2a 29 70 41 72 72 61 79 3b 0a 20 20 6d 65 6d  r*)pArray;.  mem
24f7a 73 65 74 28 26 7a 5b 2a 70 6e 45 6e 74 72 79 20  set(&z[*pnEntry 
24f7b 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30 2c 20 73  * szEntry], 0, s
24f7c 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70 49 64 78  zEntry);.  *pIdx
24f7d 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 2b   = *pnEntry;.  +
24f7e 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 72 65 74  +*pnEntry;.  ret
24f7f 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d 0a 0a 2f  urn pArray;.}../
24f80 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65  *.** Append a ne
24f81 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
24f82 20 67 69 76 65 6e 20 49 64 4c 69 73 74 2e 20 20   given IdList.  
24f83 43 72 65 61 74 65 20 61 20 6e 65 77 20 49 64 4c  Create a new IdL
24f84 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62  ist if.** need b
24f85 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 49  e..**.** A new I
24f86 64 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e 65  dList is returne
24f87 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6d 61  d, or NULL if ma
24f88 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f  lloc() fails..*/
24f89 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
24f8a 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49  IdList *sqlite3I
24f8b 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69  dListAppend(sqli
24f8c 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20  te3 *db, IdList 
24f8d 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70  *pList, Token *p
24f8e 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 69 3b  Token){.  int i;
24f8f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
24f90 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73  ){.    pList = s
24f91 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
24f92 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 64  ro(db, sizeof(Id
24f93 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28  List) );.    if(
24f94 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
24f95 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d  rn 0;.    pList-
24f96 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 7d  >nAlloc = 0;.  }
24f97 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 73 71  .  pList->a = sq
24f98 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61  lite3ArrayAlloca
24f99 74 65 28 0a 20 20 20 20 20 20 64 62 2c 0a 20 20  te(.      db,.  
24f9a 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c 0a 20 20      pList->a,.  
24f9b 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 69 73 74      sizeof(pList
24f9c 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 20 20 35  ->a[0]),.      5
24f9d 2c 0a 20 20 20 20 20 20 26 70 4c 69 73 74 2d 3e  ,.      &pList->
24f9e 6e 49 64 2c 0a 20 20 20 20 20 20 26 70 4c 69 73  nId,.      &pLis
24f9f 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20  t->nAlloc,.     
24fa0 20 26 69 0a 20 20 29 3b 0a 20 20 69 66 28 20 69   &i.  );.  if( i
24fa1 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <0 ){.    sqlite
24fa2 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
24fa3 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65  , pList);.    re
24fa4 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c  turn 0;.  }.  pL
24fa5 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20  ist->a[i].zName 
24fa6 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
24fa7 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65  mToken(db, pToke
24fa8 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69  n);.  return pLi
24fa9 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  st;.}../*.** Del
24faa 65 74 65 20 61 6e 20 49 64 4c 69 73 74 2e 0a 2a  ete an IdList..*
24fab 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24fac 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64 4c   void sqlite3IdL
24fad 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  istDelete(sqlite
24fae 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70  3 *db, IdList *p
24faf 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  List){.  int i;.
24fb0 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
24fb1 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
24fb2 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64  =0; i<pList->nId
24fb3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
24fb4 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
24fb5 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  ist->a[i].zName)
24fb6 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
24fb7 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d  bFree(db, pList-
24fb8 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  >a);.  sqlite3Db
24fb9 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b  Free(db, pList);
24fba 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
24fbb 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70 4c   the index in pL
24fbc 69 73 74 20 6f 66 20 74 68 65 20 69 64 65 6e 74  ist of the ident
24fbd 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 64 2e  ifier named zId.
24fbe 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a 20 69    Return -1.** i
24fbf 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  f not found..*/.
24fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
24fc1 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  nt sqlite3IdList
24fc2 49 6e 64 65 78 28 49 64 4c 69 73 74 20 2a 70 4c  Index(IdList *pL
24fc3 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ist, const char 
24fc4 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69  *zName){.  int i
24fc5 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
24fc6 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
24fc7 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
24fc8 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nId; i++){.   
24fc9 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
24fca 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  Cmp(pList->a[i].
24fcb 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30  zName, zName)==0
24fcc 20 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 7d   ) return i;.  }
24fcd 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a  .  return -1;.}.
24fce 0a 2f 2a 0a 2a 2a 20 45 78 70 61 6e 64 20 74 68  ./*.** Expand th
24fcf 65 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  e space allocate
24fd0 64 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  d for the given 
24fd1 53 72 63 4c 69 73 74 20 6f 62 6a 65 63 74 20 62  SrcList object b
24fd2 79 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 6e 45  y.** creating nE
24fd3 78 74 72 61 20 6e 65 77 20 73 6c 6f 74 73 20 62  xtra new slots b
24fd4 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 53 74 61  eginning at iSta
24fd5 72 74 2e 20 20 69 53 74 61 72 74 20 69 73 20 7a  rt.  iStart is z
24fd6 65 72 6f 20 62 61 73 65 64 2e 0a 2a 2a 20 4e 65  ero based..** Ne
24fd7 77 20 73 6c 6f 74 73 20 61 72 65 20 7a 65 72 6f  w slots are zero
24fd8 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  ed..**.** For ex
24fd9 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 20 61  ample, suppose a
24fda 20 53 72 63 4c 69 73 74 20 69 6e 69 74 69 61 6c   SrcList initial
24fdb 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20  ly contains two 
24fdc 65 6e 74 72 69 65 73 3a 20 41 2c 42 2e 0a 2a 2a  entries: A,B..**
24fdd 20 54 6f 20 61 70 70 65 6e 64 20 33 20 6e 65 77   To append 3 new
24fde 20 65 6e 74 72 69 65 73 20 6f 6e 74 6f 20 74 68   entries onto th
24fdf 65 20 65 6e 64 2c 20 64 6f 20 74 68 69 73 3a 0a  e end, do this:.
24fe0 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  **.**    sqlite3
24fe1 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64  SrcListEnlarge(d
24fe2 62 2c 20 70 53 72 63 6c 69 73 74 2c 20 33 2c 20  b, pSrclist, 3, 
24fe3 32 29 3b 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  2);.**.** After 
24fe4 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 20 69  the call above i
24fe5 74 20 77 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 3a  t would contain:
24fe6 20 20 41 2c 20 42 2c 20 6e 69 6c 2c 20 6e 69 6c    A, B, nil, nil
24fe7 2c 20 6e 69 6c 2e 0a 2a 2a 20 49 66 20 74 68 65  , nil..** If the
24fe8 20 69 53 74 61 72 74 20 61 72 67 75 6d 65 6e 74   iStart argument
24fe9 20 68 61 64 20 62 65 65 6e 20 31 20 69 6e 73 74   had been 1 inst
24fea 65 61 64 20 6f 66 20 32 2c 20 74 68 65 6e 20 74  ead of 2, then t
24feb 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 77 6f 75  he result.** wou
24fec 6c 64 20 68 61 76 65 20 62 65 65 6e 3a 20 20 41  ld have been:  A
24fed 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c  , nil, nil, nil,
24fee 20 42 2e 20 20 54 6f 20 70 72 65 70 65 6e 64 20   B.  To prepend 
24fef 74 68 65 20 6e 65 77 20 73 6c 6f 74 73 2c 0a 2a  the new slots,.*
24ff0 2a 20 74 68 65 20 69 53 74 61 72 74 20 76 61 6c  * the iStart val
24ff1 75 65 20 77 6f 75 6c 64 20 62 65 20 30 2e 20 20  ue would be 0.  
24ff2 54 68 65 20 72 65 73 75 6c 74 20 74 68 65 6e 20  The result then 
24ff3 77 6f 75 6c 64 0a 2a 2a 20 62 65 3a 20 6e 69 6c  would.** be: nil
24ff4 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 2c 20 42  , nil, nil, A, B
24ff5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d  ..**.** If a mem
24ff6 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
24ff7 61 69 6c 73 20 74 68 65 20 53 72 63 4c 69 73 74  ails the SrcList
24ff8 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 20 20   is unchanged.  
24ff9 54 68 65 0a 2a 2a 20 64 62 2d 3e 6d 61 6c 6c 6f  The.** db->mallo
24ffa 63 46 61 69 6c 65 64 20 66 6c 61 67 20 77 69 6c  cFailed flag wil
24ffb 6c 20 62 65 20 73 65 74 20 74 6f 20 74 72 75 65  l be set to true
24ffc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24ffd 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
24ffe 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
24fff 67 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ge(.  sqlite3 *d
25000 62 2c 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  b,       /* Data
25001 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
25002 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 4f 4f 4d  to notify of OOM
25003 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 53 72 63   errors */.  Src
25004 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
25005 2f 2a 20 54 68 65 20 53 72 63 4c 69 73 74 20 74  /* The SrcList t
25006 6f 20 62 65 20 65 6e 6c 61 72 67 65 64 20 2a 2f  o be enlarged */
25007 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20  .  int nExtra,  
25008 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
25009 6f 66 20 6e 65 77 20 73 6c 6f 74 73 20 74 6f 20  of new slots to 
2500a 61 64 64 20 74 6f 20 70 53 72 63 2d 3e 61 5b 5d  add to pSrc->a[]
2500b 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72 74   */.  int iStart
2500c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2500d 78 20 69 6e 20 70 53 72 63 2d 3e 61 5b 5d 20 6f  x in pSrc->a[] o
2500e 66 20 66 69 72 73 74 20 6e 65 77 20 73 6c 6f 74  f first new slot
2500f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
25010 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65  .  /* Sanity che
25011 63 6b 69 6e 67 20 6f 6e 20 63 61 6c 6c 69 6e 67  cking on calling
25012 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20   parameters */. 
25013 20 61 73 73 65 72 74 28 20 69 53 74 61 72 74 3e   assert( iStart>
25014 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
25015 6e 45 78 74 72 61 3e 3d 31 20 29 3b 0a 20 20 61  nExtra>=1 );.  a
25016 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29  ssert( pSrc!=0 )
25017 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61  ;.  assert( iSta
25018 72 74 3c 3d 70 53 72 63 2d 3e 6e 53 72 63 20 29  rt<=pSrc->nSrc )
25019 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
2501a 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 70 61 63   additional spac
2501b 65 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20  e if needed */. 
2501c 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 2b   if( pSrc->nSrc+
2501d 6e 45 78 74 72 61 3e 70 53 72 63 2d 3e 6e 41 6c  nExtra>pSrc->nAl
2501e 6c 6f 63 20 29 7b 0a 20 20 20 20 53 72 63 4c 69  loc ){.    SrcLi
2501f 73 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e  st *pNew;.    in
25020 74 20 6e 41 6c 6c 6f 63 20 3d 20 70 53 72 63 2d  t nAlloc = pSrc-
25021 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3b 0a 20 20  >nSrc+nExtra;.  
25022 20 20 69 6e 74 20 6e 47 6f 74 3b 0a 20 20 20 20    int nGot;.    
25023 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
25024 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 53 72 63  Realloc(db, pSrc
25025 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
25026 20 73 69 7a 65 6f 66 28 2a 70 53 72 63 29 20 2b   sizeof(*pSrc) +
25027 20 28 6e 41 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65   (nAlloc-1)*size
25028 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 20 29  of(pSrc->a[0]) )
25029 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d  ;.    if( pNew==
2502a 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
2502b 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
2502c 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74  led );.      ret
2502d 75 72 6e 20 70 53 72 63 3b 0a 20 20 20 20 7d 0a  urn pSrc;.    }.
2502e 20 20 20 20 70 53 72 63 20 3d 20 70 4e 65 77 3b      pSrc = pNew;
2502f 0a 20 20 20 20 6e 47 6f 74 20 3d 20 28 73 71 6c  .    nGot = (sql
25030 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
25031 28 64 62 2c 20 70 4e 65 77 29 20 2d 20 73 69 7a  (db, pNew) - siz
25032 65 6f 66 28 2a 70 53 72 63 29 29 2f 73 69 7a 65  eof(*pSrc))/size
25033 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2b 31  of(pSrc->a[0])+1
25034 3b 0a 20 20 20 20 70 53 72 63 2d 3e 6e 41 6c 6c  ;.    pSrc->nAll
25035 6f 63 20 3d 20 28 75 31 36 29 6e 47 6f 74 3b 0a  oc = (u16)nGot;.
25036 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 65    }..  /* Move e
25037 78 69 73 74 69 6e 67 20 73 6c 6f 74 73 20 74 68  xisting slots th
25038 61 74 20 63 6f 6d 65 20 61 66 74 65 72 20 74 68  at come after th
25039 65 20 6e 65 77 6c 79 20 69 6e 73 65 72 74 65 64  e newly inserted
2503a 20 73 6c 6f 74 73 0a 20 20 2a 2a 20 6f 75 74 20   slots.  ** out 
2503b 6f 66 20 74 68 65 20 77 61 79 20 2a 2f 0a 20 20  of the way */.  
2503c 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63  for(i=pSrc->nSrc
2503d 2d 31 3b 20 69 3e 3d 69 53 74 61 72 74 3b 20 69  -1; i>=iStart; i
2503e 2d 2d 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61  --){.    pSrc->a
2503f 5b 69 2b 6e 45 78 74 72 61 5d 20 3d 20 70 53 72  [i+nExtra] = pSr
25040 63 2d 3e 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 70  c->a[i];.  }.  p
25041 53 72 63 2d 3e 6e 53 72 63 20 2b 3d 20 28 69 31  Src->nSrc += (i1
25042 36 29 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a 20  6)nExtra;..  /* 
25043 5a 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20 61  Zero the newly a
25044 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 2a  llocated slots *
25045 2f 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72 63  /.  memset(&pSrc
25046 2d 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c 20  ->a[iStart], 0, 
25047 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30  sizeof(pSrc->a[0
25048 5d 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66 6f  ])*nExtra);.  fo
25049 72 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69 53  r(i=iStart; i<iS
2504a 74 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b 2b  tart+nExtra; i++
2504b 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69  ){.    pSrc->a[i
2504c 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a  ].iCursor = -1;.
2504d 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e    }..  /* Return
2504e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2504f 65 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c 69  e enlarged SrcLi
25050 73 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70  st */.  return p
25051 53 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41  Src;.}.../*.** A
25052 70 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 6c  ppend a new tabl
25053 65 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 69  e name to the gi
25054 76 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 72  ven SrcList.  Cr
25055 65 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c 69  eate a new SrcLi
25056 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65  st if.** need be
25057 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69  .  A new entry i
25058 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65  s created in the
25059 20 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 66   SrcList even if
2505a 20 70 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c 2e   pTable is NULL.
2505b 0a 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73 74  .**.** A SrcList
2505c 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72   is returned, or
2505d 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69   NULL if there i
2505e 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20  s an OOM error. 
2505f 20 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a   The returned.**
25060 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 62   SrcList might b
25061 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  e the same as th
25062 65 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 77  e SrcList that w
25063 61 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20 6d  as input or it m
25064 69 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65 77  ight be.** a new
25065 20 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f 4d   one.  If an OOM
25066 20 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63 75   error does occu
25067 72 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69  rs, then the pri
25068 6f 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69 73  or value of pLis
25069 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 70  t.** that is inp
2506a 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ut to this routi
2506b 6e 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  ne is automatica
2506c 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a  lly freed..**.**
2506d 20 49 66 20 70 44 61 74 61 62 61 73 65 20 69 73   If pDatabase is
2506e 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65   not null, it me
2506f 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 62  ans that the tab
25070 6c 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e  le has an option
25071 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6e  al.** database n
25072 61 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 6b  ame prefix.  Lik
25073 65 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 61  e this:  "databa
25074 73 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 20  se.table".  The 
25075 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69  pDatabase.** poi
25076 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65  nts to the table
25077 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70 54   name and the pT
25078 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 74  able points to t
25079 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
2507a 2e 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73 74  ..** The SrcList
2507b 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64  .a[].zName field
2507c 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
2507d 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77  the table name w
2507e 68 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f  hich might.** co
2507f 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20 28  me from pTable (
25080 69 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20  if pDatabase is 
25081 4e 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44  NULL) or from pD
25082 61 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72  atabase.  .** Sr
25083 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62  cList.a[].zDatab
25084 61 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69  ase is filled wi
25085 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
25086 6e 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65  name from pTable
25087 2c 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c  ,.** or with NUL
25088 4c 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73 65  L if no database
25089 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 2a   is specified..*
2508a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f  *.** In other wo
2508b 72 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b  rds, if call lik
2508c 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
2508d 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
2508e 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 42  ListAppend(D,A,B
2508f 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20  ,0);.**.** Then 
25090 42 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d  B is a table nam
25091 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  e and the databa
25092 73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70 65  se name is unspe
25093 63 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c 6c  cified.  If call
25094 65 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a  ed.** like this:
25095 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  .**.**         s
25096 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
25097 65 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a  end(D,A,B,C);.**
25098 0a 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 68  .** Then C is th
25099 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64  e table name and
2509a 20 42 20 69 73 20 74 68 65 20 64 61 74 61 62 61   B is the databa
2509b 73 65 20 6e 61 6d 65 2e 20 20 49 66 20 43 20 69  se name.  If C i
2509c 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 74 68 65  s defined.** the
2509d 6e 20 73 6f 20 69 73 20 42 2e 20 20 49 6e 20 6f  n so is B.  In o
2509e 74 68 65 72 20 77 6f 72 64 73 2c 20 77 65 20 6e  ther words, we n
2509f 65 76 65 72 20 68 61 76 65 20 61 20 63 61 73 65  ever have a case
250a0 20 77 68 65 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20   where:.**.**   
250a1 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
250a2 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 30  ListAppend(D,A,0
250a3 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 42 6f 74 68 20  ,C);.**.** Both 
250a4 70 54 61 62 6c 65 20 61 6e 64 20 70 44 61 74 61  pTable and pData
250a5 62 61 73 65 20 61 72 65 20 61 73 73 75 6d 65 64  base are assumed
250a6 20 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20   to be quoted.  
250a7 54 68 65 79 20 61 72 65 20 64 65 71 75 6f 74 65  They are dequote
250a8 64 0a 2a 2a 20 62 65 66 6f 72 65 20 62 65 69 6e  d.** before bein
250a9 67 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53  g added to the S
250aa 72 63 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  rcList..*/.SQLIT
250ab 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73  E_PRIVATE SrcLis
250ac 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  t *sqlite3SrcLis
250ad 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c 69 74  tAppend(.  sqlit
250ae 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f  e3 *db,        /
250af 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  * Connection to 
250b0 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63  notify of malloc
250b1 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 53   failures */.  S
250b2 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  rcList *pList,  
250b3 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f 20     /* Append to 
250b4 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20 4e 55  this SrcList. NU
250b5 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e 65 77  LL creates a new
250b6 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54 6f   SrcList */.  To
250b7 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20  ken *pTable,    
250b8 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61 70    /* Table to ap
250b9 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  pend */.  Token 
250ba 2a 70 44 61 74 61 62 61 73 65 20 20 20 20 2f 2a  *pDatabase    /*
250bb 20 44 61 74 61 62 61 73 65 20 6f 66 20 74 68 65   Database of the
250bc 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73   table */.){.  s
250bd 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
250be 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 73 73  em *pItem;.  ass
250bf 65 72 74 28 20 70 44 61 74 61 62 61 73 65 3d 3d  ert( pDatabase==
250c0 30 20 7c 7c 20 70 54 61 62 6c 65 21 3d 30 20 29  0 || pTable!=0 )
250c1 3b 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76  ;  /* Cannot hav
250c2 65 20 43 20 77 69 74 68 6f 75 74 20 42 20 2a 2f  e C without B */
250c3 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
250c4 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73  ){.    pList = s
250c5 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
250c6 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 53 72  ro(db, sizeof(Sr
250c7 63 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66  cList) );.    if
250c8 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  ( pList==0 ) ret
250c9 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 74  urn 0;.    pList
250ca 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20  ->nAlloc = 1;.  
250cb 7d 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69  }.  pList = sqli
250cc 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67  te3SrcListEnlarg
250cd 65 28 64 62 2c 20 70 4c 69 73 74 2c 20 31 2c 20  e(db, pList, 1, 
250ce 70 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 0a 20 20  pList->nSrc);.  
250cf 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
250d0 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  iled ){.    sqli
250d1 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
250d2 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20  (db, pList);.   
250d3 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
250d4 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d   pItem = &pList-
250d5 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63 2d 31  >a[pList->nSrc-1
250d6 5d 3b 0a 20 20 69 66 28 20 70 44 61 74 61 62 61  ];.  if( pDataba
250d7 73 65 20 26 26 20 70 44 61 74 61 62 61 73 65 2d  se && pDatabase-
250d8 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 61  >z==0 ){.    pDa
250d9 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 7d 0a  tabase = 0;.  }.
250da 20 20 69 66 28 20 70 44 61 74 61 62 61 73 65 20    if( pDatabase 
250db 29 7b 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54  ){.    Token *pT
250dc 65 6d 70 20 3d 20 70 44 61 74 61 62 61 73 65 3b  emp = pDatabase;
250dd 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20 3d  .    pDatabase =
250de 20 70 54 61 62 6c 65 3b 0a 20 20 20 20 70 54 61   pTable;.    pTa
250df 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0a 20 20 7d  ble = pTemp;.  }
250e0 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20  .  pItem->zName 
250e1 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
250e2 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c  mToken(db, pTabl
250e3 65 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a 44 61  e);.  pItem->zDa
250e4 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33  tabase = sqlite3
250e5 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
250e6 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20 20  , pDatabase);.  
250e7 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a  return pList;.}.
250e8 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 56 64  ./*.** Assign Vd
250e9 62 65 43 75 72 73 6f 72 20 69 6e 64 65 78 20 6e  beCursor index n
250ea 75 6d 62 65 72 73 20 74 6f 20 61 6c 6c 20 74 61  umbers to all ta
250eb 62 6c 65 73 20 69 6e 20 61 20 53 72 63 4c 69 73  bles in a SrcLis
250ec 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  t.*/.SQLITE_PRIV
250ed 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
250ee 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72  SrcListAssignCur
250ef 73 6f 72 73 28 50 61 72 73 65 20 2a 70 50 61 72  sors(Parse *pPar
250f0 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c 69  se, SrcList *pLi
250f1 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  st){.  int i;.  
250f2 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
250f3 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 73  tem *pItem;.  as
250f4 73 65 72 74 28 70 4c 69 73 74 20 7c 7c 20 70 50  sert(pList || pP
250f5 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
250f6 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
250f7 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72  pList ){.    for
250f8 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73  (i=0, pItem=pLis
250f9 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
250fa 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
250fb 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49  +){.      if( pI
250fc 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3e 3d 30 20  tem->iCursor>=0 
250fd 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70  ) break;.      p
250fe 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20  Item->iCursor = 
250ff 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
25100 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
25101 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
25102 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
25103 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
25104 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70  pParse, pItem->p
25105 53 65 6c 65 63 74 2d 3e 70 53 72 63 29 3b 0a 20  Select->pSrc);. 
25106 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
25107 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
25108 20 61 6e 20 65 6e 74 69 72 65 20 53 72 63 4c 69   an entire SrcLi
25109 73 74 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c 6c  st including all
2510a 20 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72   its substructur
2510b 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2510c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2510d 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73  3SrcListDelete(s
2510e 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c  qlite3 *db, SrcL
2510f 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
25110 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53  nt i;.  struct S
25111 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
25112 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d  em;.  if( pList=
25113 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  =0 ) return;.  f
25114 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  or(pItem=pList->
25115 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  a, i=0; i<pList-
25116 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65  >nSrc; i++, pIte
25117 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  m++){.    sqlite
25118 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65  3DbFree(db, pIte
25119 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  m->zDatabase);. 
2511a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2511b 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  (db, pItem->zNam
2511c 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  e);.    sqlite3D
2511d 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d  bFree(db, pItem-
2511e 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 73 71  >zAlias);.    sq
2511f 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
25120 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a  pItem->zIndex);.
25121 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
25122 65 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 54  eTable(pItem->pT
25123 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ab);.    sqlite3
25124 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
25125 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29   pItem->pSelect)
25126 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
25127 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65  rDelete(db, pIte
25128 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c  m->pOn);.    sql
25129 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
2512a 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 55 73 69  (db, pItem->pUsi
2512b 6e 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ng);.  }.  sqlit
2512c 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69  e3DbFree(db, pLi
2512d 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  st);.}../*.** Th
2512e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2512f 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
25130 65 72 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20  er to add a new 
25131 74 65 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20 65  term to the.** e
25132 6e 64 20 6f 66 20 61 20 67 72 6f 77 69 6e 67 20  nd of a growing 
25133 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68  FROM clause.  Th
25134 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20  e "p" parameter 
25135 69 73 20 74 68 65 20 70 61 72 74 20 6f 66 0a 2a  is the part of.*
25136 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
25137 65 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61  e that has alrea
25138 64 79 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63  dy been construc
25139 74 65 64 2e 20 20 22 70 22 20 69 73 20 4e 55 4c  ted.  "p" is NUL
2513a 4c 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20  L.** if this is 
2513b 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 6f  the first term o
2513c 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
2513d 65 2e 20 20 70 54 61 62 6c 65 20 61 6e 64 20 70  e.  pTable and p
2513e 44 61 74 61 62 61 73 65 0a 2a 2a 20 61 72 65 20  Database.** are 
2513f 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
25140 74 61 62 6c 65 20 61 6e 64 20 64 61 74 61 62 61  table and databa
25141 73 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20  se named in the 
25142 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
25143 2e 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 69  ..** pDatabase i
25144 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 64 61  s NULL if the da
25145 74 61 62 61 73 65 20 6e 61 6d 65 20 71 75 61 6c  tabase name qual
25146 69 66 69 65 72 20 69 73 20 6d 69 73 73 69 6e 67  ifier is missing
25147 20 2d 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c 20   - the.** usual 
25148 63 61 73 65 2e 20 20 49 66 20 74 68 65 20 74 65  case.  If the te
25149 72 6d 20 68 61 73 20 61 20 61 6c 69 61 73 2c 20  rm has a alias, 
2514a 74 68 65 6e 20 70 41 6c 69 61 73 20 70 6f 69 6e  then pAlias poin
2514b 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 69  ts to the.** ali
2514c 61 73 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74 68  as token.  If th
2514d 65 20 74 65 72 6d 20 69 73 20 61 20 73 75 62 71  e term is a subq
2514e 75 65 72 79 2c 20 74 68 65 6e 20 70 53 75 62 71  uery, then pSubq
2514f 75 65 72 79 20 69 73 20 74 68 65 0a 2a 2a 20 53  uery is the.** S
25150 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
25151 74 68 61 74 20 74 68 65 20 73 75 62 71 75 65 72  that the subquer
25152 79 20 65 6e 63 6f 64 65 73 2e 20 20 54 68 65 20  y encodes.  The 
25153 70 54 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70 44  pTable and.** pD
25154 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65  atabase paramete
25155 72 73 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72 20  rs are NULL for 
25156 73 75 62 71 75 65 72 69 65 73 2e 20 20 54 68 65  subqueries.  The
25157 20 70 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67 0a   pOn and pUsing.
25158 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  ** parameters ar
25159 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
2515a 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e   the ON and USIN
2515b 47 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a  G clauses..**.**
2515c 20 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53 72   Return a new Sr
2515d 63 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63 6f  cList which enco
2515e 64 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d 20  des is the FROM 
2515f 77 69 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a 20  with the new.** 
25160 74 65 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a 53  term added..*/.S
25161 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72  QLITE_PRIVATE Sr
25162 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72  cList *sqlite3Sr
25163 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54  cListAppendFromT
25164 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50  erm(.  Parse *pP
25165 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
25166 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
25167 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
25168 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
25169 2a 20 54 68 65 20 6c 65 66 74 20 70 61 72 74 20  * The left part 
2516a 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
2516b 73 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e 20  se already seen 
2516c 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62  */.  Token *pTab
2516d 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  le,          /* 
2516e 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  Name of the tabl
2516f 65 20 74 6f 20 61 64 64 20 74 6f 20 74 68 65 20  e to add to the 
25170 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
25171 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73   Token *pDatabas
25172 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  e,       /* Name
25173 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
25174 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62   containing pTab
25175 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  le */.  Token *p
25176 41 6c 69 61 73 2c 20 20 20 20 20 20 20 20 20 20  Alias,          
25177 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e  /* The right-han
25178 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 41 53  d side of the AS
25179 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 2a   subexpression *
2517a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62  /.  Select *pSub
2517b 71 75 65 72 79 2c 20 20 20 20 20 20 2f 2a 20 41  query,      /* A
2517c 20 73 75 62 71 75 65 72 79 20 75 73 65 64 20 69   subquery used i
2517d 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62  n place of a tab
2517e 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70  le name */.  Exp
2517f 72 20 2a 70 4f 6e 2c 20 20 20 20 20 20 20 20 20  r *pOn,         
25180 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20 63       /* The ON c
25181 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20  lause of a join 
25182 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 55 73  */.  IdList *pUs
25183 69 6e 67 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ing          /* 
25184 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  The USING clause
25185 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 29 7b   of a join */.){
25186 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
25187 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
25188 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
25189 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
2518a 20 21 70 20 26 26 20 28 70 4f 6e 20 7c 7c 20 70   !p && (pOn || p
2518b 55 73 69 6e 67 29 20 29 7b 0a 20 20 20 20 73 71  Using) ){.    sq
2518c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2518d 61 72 73 65 2c 20 22 61 20 4a 4f 49 4e 20 63 6c  arse, "a JOIN cl
2518e 61 75 73 65 20 69 73 20 72 65 71 75 69 72 65 64  ause is required
2518f 20 62 65 66 6f 72 65 20 25 73 22 2c 20 0a 20 20   before %s", .  
25190 20 20 20 20 28 70 4f 6e 20 3f 20 22 4f 4e 22 20      (pOn ? "ON" 
25191 3a 20 22 55 53 49 4e 47 22 29 0a 20 20 20 20 29  : "USING").    )
25192 3b 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 65 6e  ;.    goto appen
25193 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a 20 20  d_from_error;.  
25194 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 53  }.  p = sqlite3S
25195 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c  rcListAppend(db,
25196 20 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 61 74   p, pTable, pDat
25197 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d  abase);.  if( p=
25198 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e 6e  =0 || NEVER(p->n
25199 53 72 63 3d 3d 30 29 20 29 7b 0a 20 20 20 20 67  Src==0) ){.    g
2519a 6f 74 6f 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f  oto append_from_
2519b 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 49 74  error;.  }.  pIt
2519c 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53  em = &p->a[p->nS
2519d 72 63 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  rc-1];.  assert(
2519e 20 70 41 6c 69 61 73 21 3d 30 20 29 3b 0a 20 20   pAlias!=0 );.  
2519f 69 66 28 20 70 41 6c 69 61 73 2d 3e 6e 20 29 7b  if( pAlias->n ){
251a0 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  .    pItem->zAli
251a1 61 73 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  as = sqlite3Name
251a2 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 41  FromToken(db, pA
251a3 6c 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70 49 74  lias);.  }.  pIt
251a4 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53  em->pSelect = pS
251a5 75 62 71 75 65 72 79 3b 0a 20 20 70 49 74 65 6d  ubquery;.  pItem
251a6 2d 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20 70  ->pOn = pOn;.  p
251a7 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 70  Item->pUsing = p
251a8 55 73 69 6e 67 3b 0a 20 20 72 65 74 75 72 6e 20  Using;.  return 
251a9 70 3b 0a 0a 20 61 70 70 65 6e 64 5f 66 72 6f 6d  p;.. append_from
251aa 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74  _error:.  assert
251ab 28 20 70 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69  ( p==0 );.  sqli
251ac 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
251ad 2c 20 70 4f 6e 29 3b 0a 20 20 73 71 6c 69 74 65  , pOn);.  sqlite
251ae 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
251af 2c 20 70 55 73 69 6e 67 29 3b 0a 20 20 73 71 6c  , pUsing);.  sql
251b0 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
251b1 28 64 62 2c 20 70 53 75 62 71 75 65 72 79 29 3b  (db, pSubquery);
251b2 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
251b3 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e 44  /*.** Add an IND
251b4 45 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 49  EXED BY or NOT I
251b5 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 74 6f  NDEXED clause to
251b6 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
251b7 6c 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c 65  ly added .** ele
251b8 6d 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 72  ment of the sour
251b9 63 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20 61  ce-list passed a
251ba 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
251bb 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ument..*/.SQLITE
251bc 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
251bd 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65  lite3SrcListInde
251be 78 65 64 42 79 28 50 61 72 73 65 20 2a 70 50 61  xedBy(Parse *pPa
251bf 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c  rse, SrcList *p,
251c0 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64   Token *pIndexed
251c1 42 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  By){.  assert( p
251c2 49 6e 64 65 78 65 64 42 79 21 3d 30 20 29 3b 0a  IndexedBy!=0 );.
251c3 20 20 69 66 28 20 70 20 26 26 20 41 4c 57 41 59    if( p && ALWAY
251c4 53 28 70 2d 3e 6e 53 72 63 3e 30 29 20 29 7b 0a  S(p->nSrc>0) ){.
251c5 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
251c6 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
251c7 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31   &p->a[p->nSrc-1
251c8 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ];.    assert( p
251c9 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64  Item->notIndexed
251ca 3d 3d 30 20 26 26 20 70 49 74 65 6d 2d 3e 7a 49  ==0 && pItem->zI
251cb 6e 64 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 69  ndex==0 );.    i
251cc 66 28 20 70 49 6e 64 65 78 65 64 42 79 2d 3e 6e  f( pIndexedBy->n
251cd 3d 3d 31 20 26 26 20 21 70 49 6e 64 65 78 65 64  ==1 && !pIndexed
251ce 42 79 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 2f  By->z ){.      /
251cf 2a 20 41 20 22 4e 4f 54 20 49 4e 44 45 58 45 44  * A "NOT INDEXED
251d0 22 20 63 6c 61 75 73 65 20 77 61 73 20 73 75 70  " clause was sup
251d1 70 6c 69 65 64 2e 20 53 65 65 20 70 61 72 73 65  plied. See parse
251d2 2e 79 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e  .y .      ** con
251d3 73 74 72 75 63 74 20 22 69 6e 64 65 78 65 64 5f  struct "indexed_
251d4 6f 70 74 22 20 66 6f 72 20 64 65 74 61 69 6c 73  opt" for details
251d5 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d  . */.      pItem
251d6 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 31  ->notIndexed = 1
251d7 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
251d8 20 20 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78     pItem->zIndex
251d9 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
251da 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e  omToken(pParse->
251db 64 62 2c 20 70 49 6e 64 65 78 65 64 42 79 29 3b  db, pIndexedBy);
251dc 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
251dd 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 64 69 6e  .** When buildin
251de 67 20 75 70 20 61 20 46 52 4f 4d 20 63 6c 61 75  g up a FROM clau
251df 73 65 20 69 6e 20 74 68 65 20 70 61 72 73 65 72  se in the parser
251e0 2c 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61  , the join opera
251e1 74 6f 72 0a 2a 2a 20 69 73 20 69 6e 69 74 69 61  tor.** is initia
251e2 6c 6c 79 20 61 74 74 61 63 68 65 64 20 74 6f 20  lly attached to 
251e3 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  the left operand
251e4 2e 20 20 42 75 74 20 74 68 65 20 63 6f 64 65 20  .  But the code 
251e5 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 78 70  generator.** exp
251e6 65 63 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70  ects the join op
251e7 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e 20  erator to be on 
251e8 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  the right operan
251e9 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  d.  This routine
251ea 0a 2a 2a 20 53 68 69 66 74 73 20 61 6c 6c 20 6a  .** Shifts all j
251eb 6f 69 6e 20 6f 70 65 72 61 74 6f 72 73 20 66 72  oin operators fr
251ec 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74  om left to right
251ed 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 46   for an entire F
251ee 52 4f 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e 0a 2a  ROM.** clause..*
251ef 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 53 75  *.** Example: Su
251f0 70 70 6f 73 65 20 74 68 65 20 6a 6f 69 6e 20 69  ppose the join i
251f1 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
251f2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 41 20 6e  **           A n
251f3 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69  atural cross joi
251f4 6e 20 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70  n B.**.** The op
251f5 65 72 61 74 6f 72 20 69 73 20 22 6e 61 74 75 72  erator is "natur
251f6 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 22 2e 20  al cross join". 
251f7 20 54 68 65 20 41 20 61 6e 64 20 42 20 6f 70 65   The A and B ope
251f8 72 61 6e 64 73 20 61 72 65 20 73 74 6f 72 65 64  rands are stored
251f9 0a 2a 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d 20 61  .** in p->a[0] a
251fa 6e 64 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 73 70  nd p->a[1], resp
251fb 65 63 74 69 76 65 6c 79 2e 20 20 54 68 65 20 70  ectively.  The p
251fc 61 72 73 65 72 20 69 6e 69 74 69 61 6c 6c 79 20  arser initially 
251fd 73 74 6f 72 65 73 20 74 68 65 0a 2a 2a 20 6f 70  stores the.** op
251fe 65 72 61 74 6f 72 20 77 69 74 68 20 41 2e 20 20  erator with A.  
251ff 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 69  This routine shi
25200 66 74 73 20 74 68 61 74 20 6f 70 65 72 61 74 6f  fts that operato
25201 72 20 6f 76 65 72 20 74 6f 20 42 2e 0a 2a 2f 0a  r over to B..*/.
25202 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
25203 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
25204 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
25205 53 72 63 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69  SrcList *p){.  i
25206 66 28 20 70 20 26 26 20 70 2d 3e 61 20 29 7b 0a  f( p && p->a ){.
25207 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
25208 6f 72 28 69 3d 70 2d 3e 6e 53 72 63 2d 31 3b 20  or(i=p->nSrc-1; 
25209 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  i>0; i--){.     
2520a 20 70 2d 3e 61 5b 69 5d 2e 6a 6f 69 6e 74 79 70   p->a[i].jointyp
2520b 65 20 3d 20 70 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f  e = p->a[i-1].jo
2520c 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 0a 20 20  intype;.    }.  
2520d 20 20 70 2d 3e 61 5b 30 5d 2e 6a 6f 69 6e 74 79    p->a[0].jointy
2520e 70 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  pe = 0;.  }.}../
2520f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61  *.** Begin a tra
25210 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49  nsaction.*/.SQLI
25211 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
25212 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e  sqlite3BeginTran
25213 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70  saction(Parse *p
25214 50 61 72 73 65 2c 20 69 6e 74 20 74 79 70 65 29  Parse, int type)
25215 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
25216 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e  .  Vdbe *v;.  in
25217 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  t i;..  assert( 
25218 70 50 61 72 73 65 21 3d 30 20 29 3b 0a 20 20 64  pParse!=0 );.  d
25219 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2521a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
2521b 29 3b 0a 2f 2a 20 20 69 66 28 20 64 62 2d 3e 61  );./*  if( db->a
2521c 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72  Db[0].pBt==0 ) r
2521d 65 74 75 72 6e 3b 20 2a 2f 0a 20 20 69 66 28 20  eturn; */.  if( 
2521e 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2521f 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
25220 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 42 45  TRANSACTION, "BE
25221 47 49 4e 22 2c 20 30 2c 20 30 29 20 29 7b 0a 20  GIN", 0, 0) ){. 
25222 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
25223 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
25224 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
25225 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a  f( !v ) return;.
25226 20 20 69 66 28 20 74 79 70 65 21 3d 54 4b 5f 44    if( type!=TK_D
25227 45 46 45 52 52 45 44 20 29 7b 0a 20 20 20 20 66  EFERRED ){.    f
25228 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
25229 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  b; i++){.      s
2522a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2522b 28 76 2c 20 4f 50 5f 54 72 61 6e 73 61 63 74 69  (v, OP_Transacti
2522c 6f 6e 2c 20 69 2c 20 28 74 79 70 65 3d 3d 54 4b  on, i, (type==TK
2522d 5f 45 58 43 4c 55 53 49 56 45 29 2b 31 29 3b 0a  _EXCLUSIVE)+1);.
2522e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2522f 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 29  eUsesBtree(v, i)
25230 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
25231 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25232 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74  v, OP_AutoCommit
25233 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , 0, 0);.}../*.*
25234 2a 20 43 6f 6d 6d 69 74 20 61 20 74 72 61 6e 73  * Commit a trans
25235 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45  action.*/.SQLITE
25236 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
25237 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73  lite3CommitTrans
25238 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50  action(Parse *pP
25239 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
2523a 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b   *db;.  Vdbe *v;
2523b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ..  assert( pPar
2523c 73 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20  se!=0 );.  db = 
2523d 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
2523e 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 2f  sert( db!=0 );./
2523f 2a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 30  *  if( db->aDb[0
25240 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72  ].pBt==0 ) retur
25241 6e 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  n; */.  if( sqli
25242 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
25243 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  rse, SQLITE_TRAN
25244 53 41 43 54 49 4f 4e 2c 20 22 43 4f 4d 4d 49 54  SACTION, "COMMIT
25245 22 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  ", 0, 0) ){.    
25246 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20  return;.  }.  v 
25247 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
25248 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
25249 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
2524a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2524b 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20  _AutoCommit, 1, 
2524c 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  0);.  }.}../*.**
2524d 20 52 6f 6c 6c 62 61 63 6b 20 61 20 74 72 61 6e   Rollback a tran
2524e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54  saction.*/.SQLIT
2524f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
25250 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72  qlite3RollbackTr
25251 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20  ansaction(Parse 
25252 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69  *pParse){.  sqli
25253 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20  te3 *db;.  Vdbe 
25254 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  *v;..  assert( p
25255 50 61 72 73 65 21 3d 30 20 29 3b 0a 20 20 64 62  Parse!=0 );.  db
25256 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
25257 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29   assert( db!=0 )
25258 3b 0a 2f 2a 20 20 69 66 28 20 64 62 2d 3e 61 44  ;./*  if( db->aD
25259 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65  b[0].pBt==0 ) re
2525a 74 75 72 6e 3b 20 2a 2f 0a 20 20 69 66 28 20 73  turn; */.  if( s
2525b 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2525c 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54  pParse, SQLITE_T
2525d 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f 4c  RANSACTION, "ROL
2525e 4c 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29 7b  LBACK", 0, 0) ){
2525f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
25260 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
25261 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
25262 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71   if( v ){.    sq
25263 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25264 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74  v, OP_AutoCommit
25265 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  , 1, 1);.  }.}..
25266 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
25267 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
25268 20 74 68 65 20 70 61 72 73 65 72 20 77 68 65 6e   the parser when
25269 20 69 74 20 70 61 72 73 65 73 20 61 20 63 6f 6d   it parses a com
2526a 6d 61 6e 64 20 74 6f 20 63 72 65 61 74 65 2c 0a  mand to create,.
2526b 2a 2a 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f  ** release or ro
2526c 6c 6c 62 61 63 6b 20 61 6e 20 53 51 4c 20 73 61  llback an SQL sa
2526d 76 65 70 6f 69 6e 74 2e 20 0a 2a 2f 0a 53 51 4c  vepoint. .*/.SQL
2526e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2526f 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e   sqlite3Savepoin
25270 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
25271 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a   int op, Token *
25272 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  pName){.  char *
25273 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
25274 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61  ameFromToken(pPa
25275 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b  rse->db, pName);
25276 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a  .  if( zName ){.
25277 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71      Vdbe *v = sq
25278 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
25279 72 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  rse);.#ifndef SQ
2527a 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
2527b 49 5a 41 54 49 4f 4e 0a 20 20 20 20 73 74 61 74  IZATION.    stat
2527c 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61  ic const char *a
2527d 7a 5b 5d 20 3d 20 7b 20 22 42 45 47 49 4e 22 2c  z[] = { "BEGIN",
2527e 20 22 52 45 4c 45 41 53 45 22 2c 20 22 52 4f 4c   "RELEASE", "ROL
2527f 4c 42 41 43 4b 22 20 7d 3b 0a 20 20 20 20 61 73  LBACK" };.    as
25280 73 65 72 74 28 20 21 53 41 56 45 50 4f 49 4e 54  sert( !SAVEPOINT
25281 5f 42 45 47 49 4e 20 26 26 20 53 41 56 45 50 4f  _BEGIN && SAVEPO
25282 49 4e 54 5f 52 45 4c 45 41 53 45 3d 3d 31 20 26  INT_RELEASE==1 &
25283 26 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  & SAVEPOINT_ROLL
25284 42 41 43 4b 3d 3d 32 20 29 3b 0a 23 65 6e 64 69  BACK==2 );.#endi
25285 66 0a 20 20 20 20 69 66 28 20 21 76 20 7c 7c 20  f.    if( !v || 
25286 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
25287 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
25288 53 41 56 45 50 4f 49 4e 54 2c 20 61 7a 5b 6f 70  SAVEPOINT, az[op
25289 5d 2c 20 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a  ], zName, 0) ){.
2528a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2528b 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ree(pParse->db, 
2528c 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65  zName);.      re
2528d 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
2528e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2528f 34 28 76 2c 20 4f 50 5f 53 61 76 65 70 6f 69 6e  4(v, OP_Savepoin
25290 74 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 7a 4e 61  t, op, 0, 0, zNa
25291 6d 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  me, P4_DYNAMIC);
25292 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  .  }.}../*.** Ma
25293 6b 65 20 73 75 72 65 20 74 68 65 20 54 45 4d 50  ke sure the TEMP
25294 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
25295 6e 20 61 6e 64 20 61 76 61 69 6c 61 62 6c 65 20  n and available 
25296 66 6f 72 20 75 73 65 2e 20 20 52 65 74 75 72 6e  for use.  Return
25297 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
25298 66 20 65 72 72 6f 72 73 2e 20 20 4c 65 61 76 65  f errors.  Leave
25299 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61   any error messa
2529a 67 65 73 20 69 6e 20 74 68 65 20 70 50 61 72 73  ges in the pPars
2529b 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  e structure..*/.
2529c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2529d 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65  nt sqlite3OpenTe
2529e 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 73 65  mpDatabase(Parse
2529f 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
252a0 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
252a1 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d  e->db;.  if( db-
252a2 3e 61 44 62 5b 31 5d 2e 70 42 74 3d 3d 30 20 26  >aDb[1].pBt==0 &
252a3 26 20 21 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  & !pParse->expla
252a4 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  in ){.    int rc
252a5 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  ;.    static con
252a6 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 0a  st int flags = .
252a7 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
252a8 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
252a9 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  |.          SQLI
252aa 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c  TE_OPEN_CREATE |
252ab 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
252ac 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
252ad 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c   |.          SQL
252ae 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
252af 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 20  NCLOSE |.       
252b0 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54     SQLITE_OPEN_T
252b1 45 4d 50 5f 44 42 3b 0a 0a 20 20 20 20 72 63 20  EMP_DB;..    rc 
252b2 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61  = sqlite3BtreeFa
252b3 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 30 2c 20  ctory(db, 0, 0, 
252b4 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43  SQLITE_DEFAULT_C
252b5 41 43 48 45 5f 53 49 5a 45 2c 20 66 6c 61 67 73  ACHE_SIZE, flags
252b6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
252b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252b8 20 20 20 26 64 62 2d 3e 61 44 62 5b 31 5d 2e 70     &db->aDb[1].p
252b9 42 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  Bt);.    if( rc!
252ba 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
252bb 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
252bc 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 61  Msg(pParse, "una
252bd 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 74 65  ble to open a te
252be 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
252bf 20 22 0a 20 20 20 20 20 20 20 20 22 66 69 6c 65   ".        "file
252c0 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 6d   for storing tem
252c1 70 6f 72 61 72 79 20 74 61 62 6c 65 73 22 29 3b  porary tables");
252c2 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72  .      pParse->r
252c3 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 72 65  c = rc;.      re
252c4 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
252c5 20 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e 66    assert( (db->f
252c6 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49 6e  lags & SQLITE_In
252c7 54 72 61 6e 73 29 3d 3d 30 20 7c 7c 20 64 62 2d  Trans)==0 || db-
252c8 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 3b 0a 20  >autoCommit );. 
252c9 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61     assert( db->a
252ca 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b  Db[1].pSchema );
252cb 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
252cc 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 73 71 6c  rJournalMode(sql
252cd 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64  ite3BtreePager(d
252ce 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 2c 0a  b->aDb[1].pBt),.
252cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252d0 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
252d1 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29  dfltJournalMode)
252d2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
252d3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
252d4 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 68  ate VDBE code th
252d5 61 74 20 77 69 6c 6c 20 76 65 72 69 66 79 20 74  at will verify t
252d6 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
252d7 20 61 6e 64 20 73 74 61 72 74 0a 2a 2a 20 61 20   and start.** a 
252d8 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e  read-transaction
252d9 20 66 6f 72 20 61 6c 6c 20 6e 61 6d 65 64 20 64   for all named d
252da 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 2a  atabase files..*
252db 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f 72  *.** It is impor
252dc 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 73 63  tant that all sc
252dd 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 62 65 20  hema cookies be 
252de 76 65 72 69 66 69 65 64 20 61 6e 64 20 61 6c 6c  verified and all
252df 0a 2a 2a 20 72 65 61 64 20 74 72 61 6e 73 61 63  .** read transac
252e0 74 69 6f 6e 73 20 62 65 20 73 74 61 72 74 65 64  tions be started
252e1 20 62 65 66 6f 72 65 20 61 6e 79 74 68 69 6e 67   before anything
252e2 20 65 6c 73 65 20 68 61 70 70 65 6e 73 20 69 6e   else happens in
252e3 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 70 72 6f  .** the VDBE pro
252e4 67 72 61 6d 2e 20 20 42 75 74 20 74 68 69 73 20  gram.  But this 
252e5 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 63  routine can be c
252e6 61 6c 6c 65 64 20 61 66 74 65 72 20 6d 75 63 68  alled after much
252e7 20 6f 74 68 65 72 0a 2a 2a 20 63 6f 64 65 20 68   other.** code h
252e8 61 73 20 62 65 65 6e 20 67 65 6e 65 72 61 74 65  as been generate
252e9 64 2e 20 20 53 6f 20 68 65 72 65 20 69 73 20 77  d.  So here is w
252ea 68 61 74 20 77 65 20 64 6f 3a 0a 2a 2a 0a 2a 2a  hat we do:.**.**
252eb 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
252ec 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
252ed 63 61 6c 6c 65 64 2c 20 77 65 20 63 6f 64 65 20  called, we code 
252ee 61 6e 20 4f 50 5f 47 6f 74 6f 20 74 68 61 74 0a  an OP_Goto that.
252ef 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70 20 74 6f 20  ** will jump to 
252f0 61 20 73 75 62 72 6f 75 74 69 6e 65 20 61 74 20  a subroutine at 
252f1 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70  the end of the p
252f2 72 6f 67 72 61 6d 2e 20 20 54 68 65 6e 20 77 65  rogram.  Then we
252f3 0a 2a 2a 20 72 65 63 6f 72 64 20 65 76 65 72 79  .** record every
252f4 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 6e   database that n
252f5 65 65 64 73 20 69 74 73 20 73 63 68 65 6d 61 20  eeds its schema 
252f6 76 65 72 69 66 69 65 64 20 69 6e 20 74 68 65 0a  verified in the.
252f7 2a 2a 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  ** pParse->cooki
252f8 65 4d 61 73 6b 20 66 69 65 6c 64 2e 20 20 4c 61  eMask field.  La
252f9 74 65 72 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f  ter, after all o
252fa 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 62 65  ther code has be
252fb 65 6e 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 2c  en.** generated,
252fc 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20   the subroutine 
252fd 74 68 61 74 20 64 6f 65 73 20 74 68 65 20 63 6f  that does the co
252fe 6f 6b 69 65 20 76 65 72 69 66 69 63 61 74 69 6f  okie verificatio
252ff 6e 73 20 61 6e 64 0a 2a 2a 20 73 74 61 72 74 73  ns and.** starts
25300 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
25301 73 20 77 69 6c 6c 20 62 65 20 63 6f 64 65 64 20  s will be coded 
25302 61 6e 64 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20  and the OP_Goto 
25303 50 32 20 76 61 6c 75 65 0a 2a 2a 20 77 69 6c 6c  P2 value.** will
25304 20 62 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e   be made to poin
25305 74 20 74 6f 20 74 68 61 74 20 73 75 62 72 6f 75  t to that subrou
25306 74 69 6e 65 2e 20 20 54 68 65 20 67 65 6e 65 72  tine.  The gener
25307 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20  ation of the.** 
25308 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61 74  cookie verificat
25309 69 6f 6e 20 73 75 62 72 6f 75 74 69 6e 65 20 63  ion subroutine c
2530a 6f 64 65 20 68 61 70 70 65 6e 73 20 69 6e 20 73  ode happens in s
2530b 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
2530c 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  ng()..**.** If i
2530d 44 62 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 74  Db<0 then code t
2530e 68 65 20 4f 50 5f 47 6f 74 6f 20 6f 6e 6c 79 20  he OP_Goto only 
2530f 2d 20 64 6f 6e 27 74 20 73 65 74 20 66 6c 61 67  - don't set flag
25310 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 0a 2a   to verify the.*
25311 2a 20 73 63 68 65 6d 61 20 6f 6e 20 61 6e 79 20  * schema on any 
25312 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 73  databases.  This
25313 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
25314 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 4f 50 5f  position the OP_
25315 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c 79 20 69 6e  Goto.** early in
25316 20 74 68 65 20 63 6f 64 65 2c 20 62 65 66 6f 72   the code, befor
25317 65 20 77 65 20 6b 6e 6f 77 20 69 66 20 61 6e 79  e we know if any
25318 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 73   database tables
25319 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a 2a   will be used..*
2531a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2531b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
2531c 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61  eVerifySchema(Pa
2531d 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
2531e 20 69 44 62 29 7b 0a 20 20 50 61 72 73 65 20 2a   iDb){.  Parse *
2531f 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69  pToplevel = sqli
25320 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
25321 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 69 66 28  (pParse);..  if(
25322 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b   pToplevel->cook
25323 69 65 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20 20  ieGoto==0 ){.   
25324 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
25325 65 33 47 65 74 56 64 62 65 28 70 54 6f 70 6c 65  e3GetVdbe(pTople
25326 76 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 76 3d  vel);.    if( v=
25327 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 20 2f 2a  =0 ) return;  /*
25328 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65   This only happe
25329 6e 73 20 69 66 20 74 68 65 72 65 20 77 61 73 20  ns if there was 
2532a 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 2a 2f  a prior error */
2532b 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e  .    pToplevel->
2532c 63 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 73 71 6c  cookieGoto = sql
2532d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2532e 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29  , OP_Goto, 0, 0)
2532f 2b 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 44  +1;.  }.  if( iD
25330 62 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  b>=0 ){.    sqli
25331 74 65 33 20 2a 64 62 20 3d 20 70 54 6f 70 6c 65  te3 *db = pTople
25332 76 65 6c 2d 3e 64 62 3b 0a 20 20 20 20 69 6e 74  vel->db;.    int
25333 20 6d 61 73 6b 3b 0a 0a 20 20 20 20 61 73 73 65   mask;..    asse
25334 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20  rt( iDb<db->nDb 
25335 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  );.    assert( d
25336 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 21  b->aDb[iDb].pBt!
25337 3d 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b 0a  =0 || iDb==1 );.
25338 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c      assert( iDb<
25339 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
2533a 48 45 44 2b 32 20 29 3b 0a 20 20 20 20 6d 61 73  HED+2 );.    mas
2533b 6b 20 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 20 20  k = 1<<iDb;.    
2533c 69 66 28 20 28 70 54 6f 70 6c 65 76 65 6c 2d 3e  if( (pToplevel->
2533d 63 6f 6f 6b 69 65 4d 61 73 6b 20 26 20 6d 61 73  cookieMask & mas
2533e 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  k)==0 ){.      p
2533f 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65  Toplevel->cookie
25340 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20  Mask |= mask;.  
25341 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 63      pToplevel->c
25342 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 20  ookieValue[iDb] 
25343 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
25344 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
25345 6f 6f 6b 69 65 3b 0a 20 20 20 20 20 20 69 66 28  ookie;.      if(
25346 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
25347 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20   iDb==1 ){.     
25348 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65     sqlite3OpenTe
25349 6d 70 44 61 74 61 62 61 73 65 28 70 54 6f 70 6c  mpDatabase(pTopl
2534a 65 76 65 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20  evel);.      }. 
2534b 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
2534c 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20  * Generate VDBE 
2534d 63 6f 64 65 20 74 68 61 74 20 70 72 65 70 61 72  code that prepar
2534e 65 73 20 66 6f 72 20 64 6f 69 6e 67 20 61 6e 20  es for doing an 
2534f 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 0a 2a  operation that.*
25350 2a 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74  * might change t
25351 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
25352 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
25353 73 74 61 72 74 73 20 61 20 6e 65 77 20 74 72 61  starts a new tra
25354 6e 73 61 63 74 69 6f 6e 20 69 66 20 77 65 20 61  nsaction if we a
25355 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 77  re not already w
25356 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73  ithin.** a trans
25357 61 63 74 69 6f 6e 2e 20 20 49 66 20 77 65 20 61  action.  If we a
25358 72 65 20 61 6c 72 65 61 64 79 20 77 69 74 68 69  re already withi
25359 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  n a transaction,
2535a 20 74 68 65 6e 20 61 20 63 68 65 63 6b 70 6f 69   then a checkpoi
2535b 6e 74 0a 2a 2a 20 69 73 20 73 65 74 20 69 66 20  nt.** is set if 
2535c 74 68 65 20 73 65 74 53 74 61 74 65 6d 65 6e 74  the setStatement
2535d 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72   parameter is tr
2535e 75 65 2e 20 20 41 20 63 68 65 63 6b 70 6f 69 6e  ue.  A checkpoin
2535f 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73  t should.** be s
25360 65 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e  et for operation
25361 73 20 74 68 61 74 20 6d 69 67 68 74 20 66 61 69  s that might fai
25362 6c 20 28 64 75 65 20 74 6f 20 61 20 63 6f 6e 73  l (due to a cons
25363 74 72 61 69 6e 74 29 20 70 61 72 74 20 6f 66 0a  traint) part of.
25364 2a 2a 20 74 68 65 20 77 61 79 20 74 68 72 6f 75  ** the way throu
25365 67 68 20 61 6e 64 20 77 68 69 63 68 20 77 69 6c  gh and which wil
25366 6c 20 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20 73  l need to undo s
25367 6f 6d 65 20 77 72 69 74 65 73 20 77 69 74 68 6f  ome writes witho
25368 75 74 20 68 61 76 69 6e 67 20 74 6f 0a 2a 2a 20  ut having to.** 
25369 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 77 68 6f  rollback the who
2536a 6c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  le transaction. 
2536b 20 46 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20   For operations 
2536c 77 68 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72  where all constr
2536d 61 69 6e 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20  aints.** can be 
2536e 63 68 65 63 6b 65 64 20 62 65 66 6f 72 65 20 61  checked before a
2536f 6e 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d  ny changes are m
25370 61 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  ade to the datab
25371 61 73 65 2c 20 69 74 20 69 73 20 6e 65 76 65 72  ase, it is never
25372 0a 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f  .** necessary to
25373 20 75 6e 64 6f 20 61 20 77 72 69 74 65 20 61 6e   undo a write an
25374 64 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74  d the checkpoint
25375 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73   should not be s
25376 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  et..*/.SQLITE_PR
25377 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
25378 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
25379 61 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61  ation(Parse *pPa
2537a 72 73 65 2c 20 69 6e 74 20 73 65 74 53 74 61 74  rse, int setStat
2537b 65 6d 65 6e 74 2c 20 69 6e 74 20 69 44 62 29 7b  ement, int iDb){
2537c 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65  .  Parse *pTople
2537d 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72  vel = sqlite3Par
2537e 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73  seToplevel(pPars
2537f 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f 64  e);.  sqlite3Cod
25380 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
25381 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 70 54  arse, iDb);.  pT
25382 6f 70 6c 65 76 65 6c 2d 3e 77 72 69 74 65 4d 61  oplevel->writeMa
25383 73 6b 20 7c 3d 20 31 3c 3c 69 44 62 3b 0a 20 20  sk |= 1<<iDb;.  
25384 70 54 6f 70 6c 65 76 65 6c 2d 3e 69 73 4d 75 6c  pToplevel->isMul
25385 74 69 57 72 69 74 65 20 7c 3d 20 73 65 74 53 74  tiWrite |= setSt
25386 61 74 65 6d 65 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  atement;.}../*.*
25387 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20  * Indicate that 
25388 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 63 75  the statement cu
25389 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
2538a 6e 73 74 72 75 63 74 69 6f 6e 20 6d 69 67 68 74  nstruction might
2538b 20 77 72 69 74 65 0a 2a 2a 20 6d 6f 72 65 20 74   write.** more t
2538c 68 61 6e 20 6f 6e 65 20 65 6e 74 72 79 20 28 65  han one entry (e
2538d 78 61 6d 70 6c 65 3a 20 64 65 6c 65 74 69 6e 67  xample: deleting
2538e 20 6f 6e 65 20 72 6f 77 20 74 68 65 6e 20 69 6e   one row then in
2538f 73 65 72 74 69 6e 67 20 61 6e 6f 74 68 65 72 2c  serting another,
25390 0a 2a 2a 20 69 6e 73 65 72 74 69 6e 67 20 6d 75  .** inserting mu
25391 6c 74 69 70 6c 65 20 72 6f 77 73 20 69 6e 20 61  ltiple rows in a
25392 20 74 61 62 6c 65 2c 20 6f 72 20 69 6e 73 65 72   table, or inser
25393 74 69 6e 67 20 61 20 72 6f 77 20 61 6e 64 20 69  ting a row and i
25394 6e 64 65 78 20 65 6e 74 72 69 65 73 2e 29 0a 2a  ndex entries.).*
25395 2a 20 49 66 20 61 6e 20 61 62 6f 72 74 20 6f 63  * If an abort oc
25396 63 75 72 73 20 61 66 74 65 72 20 73 6f 6d 65 20  curs after some 
25397 6f 66 20 74 68 65 73 65 20 77 72 69 74 65 73 20  of these writes 
25398 68 61 76 65 20 63 6f 6d 70 6c 65 74 65 64 2c 20  have completed, 
25399 74 68 65 6e 20 69 74 20 77 69 6c 6c 0a 2a 2a 20  then it will.** 
2539a 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  be necessary to 
2539b 75 6e 64 6f 20 74 68 65 20 63 6f 6d 70 6c 65 74  undo the complet
2539c 65 64 20 77 72 69 74 65 73 2e 0a 2a 2f 0a 53 51  ed writes..*/.SQ
2539d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2539e 64 20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57 72  d sqlite3MultiWr
2539f 69 74 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ite(Parse *pPars
253a0 65 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f  e){.  Parse *pTo
253a1 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33  plevel = sqlite3
253a2 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
253a3 61 72 73 65 29 3b 0a 20 20 70 54 6f 70 6c 65 76  arse);.  pToplev
253a4 65 6c 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65  el->isMultiWrite
253a5 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20   = 1;.}../* .** 
253a6 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  The code generat
253a7 6f 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  or calls this ro
253a8 75 74 69 6e 65 20 69 66 20 69 73 20 64 69 73 63  utine if is disc
253a9 6f 76 65 72 73 20 74 68 61 74 20 69 74 20 69 73  overs that it is
253aa 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  .** possible to 
253ab 61 62 6f 72 74 20 61 20 73 74 61 74 65 6d 65 6e  abort a statemen
253ac 74 20 70 72 69 6f 72 20 74 6f 20 63 6f 6d 70 6c  t prior to compl
253ad 65 74 69 6f 6e 2e 20 20 49 6e 20 6f 72 64 65 72  etion.  In order
253ae 20 74 6f 20 0a 2a 2a 20 70 65 72 66 6f 72 6d 20   to .** perform 
253af 74 68 69 73 20 61 62 6f 72 74 20 77 69 74 68 6f  this abort witho
253b0 75 74 20 63 6f 72 72 75 70 74 69 6e 67 20 74 68  ut corrupting th
253b1 65 20 64 61 74 61 62 61 73 65 2c 20 77 65 20 6e  e database, we n
253b2 65 65 64 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 73  eed to make.** s
253b3 75 72 65 20 74 68 61 74 20 74 68 65 20 73 74 61  ure that the sta
253b4 74 65 6d 65 6e 74 20 69 73 20 70 72 6f 74 65 63  tement is protec
253b5 74 65 64 20 62 79 20 61 20 73 74 61 74 65 6d 65  ted by a stateme
253b6 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  nt transaction..
253b7 2a 2a 0a 2a 2a 20 54 65 63 68 6e 69 63 61 6c 6c  **.** Technicall
253b8 79 2c 20 77 65 20 6f 6e 6c 79 20 6e 65 65 64 20  y, we only need 
253b9 74 6f 20 73 65 74 20 74 68 65 20 6d 61 79 41 62  to set the mayAb
253ba 6f 72 74 20 66 6c 61 67 20 69 66 20 74 68 65 0a  ort flag if the.
253bb 2a 2a 20 69 73 4d 75 6c 74 69 57 72 69 74 65 20  ** isMultiWrite 
253bc 66 6c 61 67 20 77 61 73 20 70 72 65 76 69 6f 75  flag was previou
253bd 73 6c 79 20 73 65 74 2e 20 20 54 68 65 72 65 20  sly set.  There 
253be 69 73 20 61 20 74 69 6d 65 20 64 65 70 65 6e 64  is a time depend
253bf 65 6e 63 79 0a 2a 2a 20 73 75 63 68 20 74 68 61  ency.** such tha
253c0 74 20 74 68 65 20 61 62 6f 72 74 20 6d 75 73 74  t the abort must
253c1 20 6f 63 63 75 72 20 61 66 74 65 72 20 74 68 65   occur after the
253c2 20 6d 75 6c 74 69 77 72 69 74 65 2e 20 20 54 68   multiwrite.  Th
253c3 69 73 20 6d 61 6b 65 73 0a 2a 2a 20 73 6f 6d 65  is makes.** some
253c4 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 76 6f   statements invo
253c5 6c 76 69 6e 67 20 74 68 65 20 52 45 50 4c 41 43  lving the REPLAC
253c6 45 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c  E conflict resol
253c7 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 0a  ution algorithm.
253c8 2a 2a 20 67 6f 20 61 20 6c 69 74 74 6c 65 20 66  ** go a little f
253c9 61 73 74 65 72 2e 20 20 42 75 74 20 74 61 6b 69  aster.  But taki
253ca 6e 67 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20  ng advantage of 
253cb 74 68 69 73 20 74 69 6d 65 20 64 65 70 65 6e 64  this time depend
253cc 65 6e 63 79 0a 2a 2a 20 6d 61 6b 65 73 20 69 74  ency.** makes it
253cd 20 6d 6f 72 65 20 64 69 66 66 69 63 75 6c 74 20   more difficult 
253ce 74 6f 20 70 72 6f 76 65 20 74 68 61 74 20 74 68  to prove that th
253cf 65 20 63 6f 64 65 20 69 73 20 63 6f 72 72 65 63  e code is correc
253d0 74 20 28 69 6e 20 0a 2a 2a 20 70 61 72 74 69 63  t (in .** partic
253d1 75 6c 61 72 2c 20 69 74 20 70 72 65 76 65 6e 74  ular, it prevent
253d2 73 20 75 73 20 66 72 6f 6d 20 77 72 69 74 69 6e  s us from writin
253d3 67 20 61 6e 20 65 66 66 65 63 74 69 76 65 0a 2a  g an effective.*
253d4 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
253d5 20 6f 66 20 73 71 6c 69 74 65 33 41 73 73 65 72   of sqlite3Asser
253d6 74 4d 61 79 41 62 6f 72 74 28 29 29 20 61 6e 64  tMayAbort()) and
253d7 20 73 6f 20 77 65 20 68 61 76 65 20 63 68 6f 73   so we have chos
253d8 65 6e 0a 2a 2a 20 74 6f 20 74 61 6b 65 20 74 68  en.** to take th
253d9 65 20 73 61 66 65 20 72 6f 75 74 65 20 61 6e 64  e safe route and
253da 20 73 6b 69 70 20 74 68 65 20 6f 70 74 69 6d 69   skip the optimi
253db 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  zation..*/.SQLIT
253dc 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
253dd 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50  qlite3MayAbort(P
253de 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
253df 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65   Parse *pTopleve
253e0 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65  l = sqlite3Parse
253e1 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29  Toplevel(pParse)
253e2 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6d  ;.  pToplevel->m
253e3 61 79 41 62 6f 72 74 20 3d 20 31 3b 0a 7d 0a 0a  ayAbort = 1;.}..
253e4 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50  /*.** Code an OP
253e5 5f 48 61 6c 74 20 74 68 61 74 20 63 61 75 73 65  _Halt that cause
253e6 73 20 74 68 65 20 76 64 62 65 20 74 6f 20 72 65  s the vdbe to re
253e7 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43  turn an SQLITE_C
253e8 4f 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 65 72 72  ONSTRAINT.** err
253e9 6f 72 2e 20 54 68 65 20 6f 6e 45 72 72 6f 72 20  or. The onError 
253ea 70 61 72 61 6d 65 74 65 72 20 64 65 74 65 72 6d  parameter determ
253eb 69 6e 65 73 20 77 68 69 63 68 20 28 69 66 20 61  ines which (if a
253ec 6e 79 29 20 6f 66 20 74 68 65 20 73 74 61 74 65  ny) of the state
253ed 6d 65 6e 74 0a 2a 2a 20 61 6e 64 2f 6f 72 20 63  ment.** and/or c
253ee 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
253ef 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63  on is rolled bac
253f0 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  k..*/.SQLITE_PRI
253f1 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
253f2 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28  3HaltConstraint(
253f3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
253f4 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 63 68 61 72  nt onError, char
253f5 20 2a 70 34 2c 20 69 6e 74 20 70 34 74 79 70 65   *p4, int p4type
253f6 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  ){.  Vdbe *v = s
253f7 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
253f8 61 72 73 65 29 3b 0a 20 20 69 66 28 20 6f 6e 45  arse);.  if( onE
253f9 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 29  rror==OE_Abort )
253fa 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79  {.    sqlite3May
253fb 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20  Abort(pParse);. 
253fc 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
253fd 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c  AddOp4(v, OP_Hal
253fe 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  t, SQLITE_CONSTR
253ff 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 30  AINT, onError, 0
25400 2c 20 70 34 2c 20 70 34 74 79 70 65 29 3b 0a 7d  , p4, p4type);.}
25401 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
25402 20 73 65 65 20 69 66 20 70 49 6e 64 65 78 20 75   see if pIndex u
25403 73 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  ses the collatin
25404 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c  g sequence pColl
25405 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75  .  Return.** tru
25406 65 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e 64  e if it does and
25407 20 66 61 6c 73 65 20 69 66 20 69 74 20 64 6f 65   false if it doe
25408 73 20 6e 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65  s not..*/.#ifnde
25409 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45  f SQLITE_OMIT_RE
2540a 49 4e 44 45 58 0a 73 74 61 74 69 63 20 69 6e 74  INDEX.static int
2540b 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28   collationMatch(
2540c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
2540d 6c 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  l, Index *pIndex
2540e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  ){.  int i;.  as
2540f 73 65 72 74 28 20 7a 43 6f 6c 6c 21 3d 30 20 29  sert( zColl!=0 )
25410 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
25411 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  Index->nColumn; 
25412 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  i++){.    const 
25413 63 68 61 72 20 2a 7a 20 3d 20 70 49 6e 64 65 78  char *z = pIndex
25414 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20  ->azColl[i];.   
25415 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 29 3b   assert( z!=0 );
25416 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69  .    if( 0==sqli
25417 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43  te3StrICmp(z, zC
25418 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72 65  oll) ){.      re
25419 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2541a 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2541b 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
2541c 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69  compute all indi
2541d 63 65 73 20 6f 66 20 70 54 61 62 20 74 68 61 74  ces of pTab that
2541e 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69   use the collati
2541f 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c  ng sequence pCol
25420 6c 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d  l..** If pColl==
25421 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65  0 then recompute
25422 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
25423 70 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  pTab..*/.#ifndef
25424 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49   SQLITE_OMIT_REI
25425 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64  NDEX.static void
25426 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50 61   reindexTable(Pa
25427 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
25428 6c 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20 63  le *pTab, char c
25429 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20  onst *zColl){.  
2542a 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20  Index *pIndex;  
2542b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2542c 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74  n index associat
2542d 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a  ed with pTab */.
2542e 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54  .  for(pIndex=pT
2542f 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64  ab->pIndex; pInd
25430 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65  ex; pIndex=pInde
25431 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  x->pNext){.    i
25432 66 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63  f( zColl==0 || c
25433 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43  ollationMatch(zC
25434 6f 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0a  oll, pIndex) ){.
25435 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
25436 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
25437 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
25438 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
25439 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65  .      sqlite3Be
2543a 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
2543b 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
2543c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2543d 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72  RefillIndex(pPar
2543e 73 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b  se, pIndex, -1);
2543f 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e  .    }.  }.}.#en
25440 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d  dif../*.** Recom
25441 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73  pute all indices
25442 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69   of all tables i
25443 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20  n all databases 
25444 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e 64  where the.** ind
25445 69 63 65 73 20 75 73 65 20 74 68 65 20 63 6f 6c  ices use the col
25446 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
25447 70 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c  pColl.  If pColl
25448 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75  ==0 then recompu
25449 74 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63 65  te.** all indice
2544a 73 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a 2f  s everywhere..*/
2544b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2544c 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61  OMIT_REINDEX.sta
2544d 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78  tic void reindex
2544e 44 61 74 61 62 61 73 65 73 28 50 61 72 73 65 20  Databases(Parse 
2544f 2a 70 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f  *pParse, char co
25450 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44  nst *zColl){.  D
25451 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20  b *pDb;         
25452 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
25453 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
25454 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
25455 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25456 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
25457 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f   index number */
25458 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25459 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f   pParse->db;   /
2545a 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
2545b 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 48  onnection */.  H
2545c 61 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20 20  ashElem *k;     
2545d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
2545e 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74  r looping over t
2545f 61 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f 0a  ables in pDb */.
25460 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
25461 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25462 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20   A table in the 
25463 64 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20 66  database */..  f
25464 6f 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64 62  or(iDb=0, pDb=db
25465 2d 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e 6e  ->aDb; iDb<db->n
25466 44 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b 2b  Db; iDb++, pDb++
25467 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
25468 44 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72  Db!=0 );.    for
25469 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (k=sqliteHashFir
2546a 73 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61  st(&pDb->pSchema
2546b 2d 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b 20  ->tblHash);  k; 
2546c 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  k=sqliteHashNext
2546d 28 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61 62  (k)){.      pTab
2546e 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74   = (Table*)sqlit
2546f 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20  eHashData(k);.  
25470 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65      reindexTable
25471 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a  (pParse, pTab, z
25472 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Coll);.    }.  }
25473 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
25474 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
25475 6f 72 20 74 68 65 20 52 45 49 4e 44 45 58 20 63  or the REINDEX c
25476 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  ommand..**.**   
25477 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 20 20       REINDEX    
25478 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25479 20 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a 20          -- 1.** 
2547a 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20         REINDEX  
2547b 3c 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20 20  <collation>     
2547c 20 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a 2a            -- 2.*
2547d 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58  *        REINDEX
2547e 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c    ?<database>.?<
2547f 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20 33  tablename>  -- 3
25480 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44  .**        REIND
25481 45 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e  EX  ?<database>.
25482 3f 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d 2d  ?<indexname>  --
25483 20 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20   4.**.** Form 1 
25484 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69 63  causes all indic
25485 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63 68  es in all attach
25486 65 64 20 64 61 74 61 62 61 73 65 73 20 74 6f 20  ed databases to 
25487 62 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20 46  be rebuilt..** F
25488 6f 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20 61  orm 2 rebuilds a
25489 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c  ll indices in al
2548a 6c 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74  l databases that
2548b 20 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a 2a   use the named.*
2548c 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  * collating func
2548d 74 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20 61  tion.  Forms 3 a
2548e 6e 64 20 34 20 72 65 62 75 69 6c 64 20 74 68 65  nd 4 rebuild the
2548f 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72 20   named index or 
25490 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 61  all.** indices a
25491 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
25492 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a  he named table..
25493 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
25494 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 53  E_OMIT_REINDEX.S
25495 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
25496 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65  id sqlite3Reinde
25497 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  x(Parse *pParse,
25498 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
25499 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a  Token *pName2){.
2549a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2549b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2549c 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
2549d 6e 63 65 20 74 6f 20 62 65 20 72 65 69 6e 64 65  nce to be reinde
2549e 78 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  xed, or NULL */.
2549f 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20    char *z;      
254a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
254a1 20 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65   Name of a table
254a2 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63   or index */.  c
254a3 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20  onst char *zDb; 
254a4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
254a5 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
254a6 73 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  se */.  Table *p
254a7 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tab;            
254a8 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69      /* A table i
254a9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  n the database *
254aa 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  /.  Index *pInde
254ab 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
254ac 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f  /* An index asso
254ad 63 69 61 74 65 64 20 77 69 74 68 20 70 54 61 62  ciated with pTab
254ae 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
254af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254b0 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
254b1 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a  e index number *
254b2 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
254b3 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
254b4 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
254b5 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
254b6 54 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b  Token *pObjName;
254b7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
254b8 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
254b9 20 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 65 20   or index to be 
254ba 72 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a 20 20  reindexed */..  
254bb 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61  /* Read the data
254bc 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20  base schema. If 
254bd 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
254be 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20   leave an error 
254bf 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64  message.  ** and
254c0 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20   code in pParse 
254c1 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  and return NULL.
254c2 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
254c3 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
254c4 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
254c5 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
254c6 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d  }..  if( pName1=
254c7 3d 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65  =0 ){.    reinde
254c8 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72 73  xDatabases(pPars
254c9 65 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72  e, 0);.    retur
254ca 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 4e  n;.  }else if( N
254cb 45 56 45 52 28 70 4e 61 6d 65 32 3d 3d 30 29 20  EVER(pName2==0) 
254cc 7c 7c 20 70 4e 61 6d 65 32 2d 3e 7a 3d 3d 30 20  || pName2->z==0 
254cd 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f  ){.    char *zCo
254ce 6c 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ll;.    assert( 
254cf 70 4e 61 6d 65 31 2d 3e 7a 20 29 3b 0a 20 20 20  pName1->z );.   
254d0 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   zColl = sqlite3
254d1 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50  NameFromToken(pP
254d2 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 31  arse->db, pName1
254d3 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 6f 6c  );.    if( !zCol
254d4 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  l ) return;.    
254d5 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  pColl = sqlite3F
254d6 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45  indCollSeq(db, E
254d7 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30  NC(db), zColl, 0
254d8 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  );.    if( pColl
254d9 20 29 7b 0a 20 20 20 20 20 20 72 65 69 6e 64 65   ){.      reinde
254da 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72 73  xDatabases(pPars
254db 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20  e, zColl);.     
254dc 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
254dd 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20  b, zColl);.     
254de 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
254df 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
254e0 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d  (db, zColl);.  }
254e1 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
254e2 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72  TwoPartName(pPar
254e3 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
254e4 65 32 2c 20 26 70 4f 62 6a 4e 61 6d 65 29 3b 0a  e2, &pObjName);.
254e5 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65    if( iDb<0 ) re
254e6 74 75 72 6e 3b 0a 20 20 7a 20 3d 20 73 71 6c 69  turn;.  z = sqli
254e7 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
254e8 28 64 62 2c 20 70 4f 62 6a 4e 61 6d 65 29 3b 0a  (db, pObjName);.
254e9 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
254ea 75 72 6e 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d  urn;.  zDb = db-
254eb 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
254ec 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
254ed 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
254ee 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 70 54  , zDb);.  if( pT
254ef 61 62 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65  ab ){.    reinde
254f0 78 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  xTable(pParse, p
254f1 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Tab, 0);.    sql
254f2 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
254f3 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
254f4 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71   }.  pIndex = sq
254f5 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
254f6 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 73 71  b, z, zDb);.  sq
254f7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
254f8 7a 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78  z);.  if( pIndex
254f9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
254fa 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
254fb 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
254fc 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  b);.    sqlite3R
254fd 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73  efillIndex(pPars
254fe 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a  e, pIndex, -1);.
254ff 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
25500 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
25501 67 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c  g(pParse, "unabl
25502 65 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68  e to identify th
25503 65 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 72  e object to be r
25504 65 69 6e 64 65 78 65 64 22 29 3b 0a 7d 0a 23 65  eindexed");.}.#e
25505 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
25506 72 6e 20 61 20 64 79 6e 61 6d 69 63 6c 79 20 61  rn a dynamicly a
25507 6c 6c 6f 63 61 74 65 64 20 4b 65 79 49 6e 66 6f  llocated KeyInfo
25508 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
25509 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 77  can be used.** w
2550a 69 74 68 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20  ith OP_OpenRead 
2550b 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20  or OP_OpenWrite 
2550c 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 62 61  to access databa
2550d 73 65 20 69 6e 64 65 78 20 70 49 64 78 2e 0a 2a  se index pIdx..*
2550e 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
2550f 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ul, a pointer to
25510 20 74 68 65 20 6e 65 77 20 73 74 72 75 63 74 75   the new structu
25511 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  re is returned. 
25512 49 6e 20 74 68 69 73 20 63 61 73 65 0a 2a 2a 20  In this case.** 
25513 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65  the caller is re
25514 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61  sponsible for ca
25515 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 44 62 46  lling sqlite3DbF
25516 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 74 68 65  ree(db, ) on the
25517 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20 70 6f   returned .** po
25518 69 6e 74 65 72 2e 20 49 66 20 61 6e 20 65 72 72  inter. If an err
25519 6f 72 20 6f 63 63 75 72 73 20 28 6f 75 74 20 6f  or occurs (out o
2551a 66 20 6d 65 6d 6f 72 79 20 6f 72 20 6d 69 73 73  f memory or miss
2551b 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a  ing collation .*
2551c 2a 20 73 65 71 75 65 6e 63 65 29 2c 20 4e 55 4c  * sequence), NUL
2551d 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  L is returned an
2551e 64 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 70  d the state of p
2551f 50 61 72 73 65 20 75 70 64 61 74 65 64 20 74 6f  Parse updated to
25520 20 72 65 66 6c 65 63 74 0a 2a 2a 20 74 68 65 20   reflect.** the 
25521 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
25522 5f 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f  _PRIVATE KeyInfo
25523 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65   *sqlite3IndexKe
25524 79 69 6e 66 6f 28 50 61 72 73 65 20 2a 70 50 61  yinfo(Parse *pPa
25525 72 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 64 78  rse, Index *pIdx
25526 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
25527 74 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e  t nCol = pIdx->n
25528 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e 42  Column;.  int nB
25529 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 4b 65  ytes = sizeof(Ke
2552a 79 49 6e 66 6f 29 20 2b 20 28 6e 43 6f 6c 2d 31  yInfo) + (nCol-1
2552b 29 2a 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  )*sizeof(CollSeq
2552c 2a 29 20 2b 20 6e 43 6f 6c 3b 0a 20 20 73 71 6c  *) + nCol;.  sql
2552d 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2552e 65 2d 3e 64 62 3b 0a 20 20 4b 65 79 49 6e 66 6f  e->db;.  KeyInfo
2552f 20 2a 70 4b 65 79 20 3d 20 28 4b 65 79 49 6e 66   *pKey = (KeyInf
25530 6f 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  o *)sqlite3DbMal
25531 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74  locZero(db, nByt
25532 65 73 29 3b 0a 0a 20 20 69 66 28 20 70 4b 65 79  es);..  if( pKey
25533 20 29 7b 0a 20 20 20 20 70 4b 65 79 2d 3e 64 62   ){.    pKey->db
25534 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
25535 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72     pKey->aSortOr
25536 64 65 72 20 3d 20 28 75 38 20 2a 29 26 28 70 4b  der = (u8 *)&(pK
25537 65 79 2d 3e 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29  ey->aColl[nCol])
25538 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 26 70  ;.    assert( &p
25539 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Key->aSortOrder[
2553a 6e 43 6f 6c 5d 3d 3d 26 28 28 28 75 38 20 2a 29  nCol]==&(((u8 *)
2553b 70 4b 65 79 29 5b 6e 42 79 74 65 73 5d 29 20 29  pKey)[nBytes]) )
2553c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
2553d 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
2553e 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d     char *zColl =
2553f 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d   pIdx->azColl[i]
25540 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
25541 7a 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 70  zColl );.      p
25542 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20  Key->aColl[i] = 
25543 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
25544 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f  lSeq(pParse, zCo
25545 6c 6c 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d  ll);.      pKey-
25546 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d  >aSortOrder[i] =
25547 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65   pIdx->aSortOrde
25548 72 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  r[i];.    }.    
25549 70 4b 65 79 2d 3e 6e 46 69 65 6c 64 20 3d 20 28  pKey->nField = (
2554a 75 31 36 29 6e 43 6f 6c 3b 0a 20 20 7d 0a 0a 20  u16)nCol;.  }.. 
2554b 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2554c 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
2554d 44 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79 29  DbFree(db, pKey)
2554e 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 30 3b 0a  ;.    pKey = 0;.
2554f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4b 65    }.  return pKe
25550 79 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  y;.}../*********
25551 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 75 69  ***** End of bui
25552 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ld.c ***********
25553 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25555 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
25556 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
25557 20 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a   callback.c ****
25558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2555a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
2555b 20 4d 61 79 20 32 33 20 0a 2a 2a 0a 2a 2a 20 54   May 23 .**.** T
2555c 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2555d 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2555e 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2555f 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
25560 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
25561 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
25562 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
25563 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
25564 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
25565 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
25566 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
25567 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
25568 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
25569 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2556a 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2556b 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2556c 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2556d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2556e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2556f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25571 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
25572 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
25573 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20   functions used 
25574 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 69 6e  to access the in
25575 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c  ternal hash tabl
25576 65 73 0a 2a 2a 20 6f 66 20 75 73 65 72 20 64 65  es.** of user de
25577 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20  fined functions 
25578 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  and collation se
25579 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a  quences..*/.../*
2557a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 27  .** Invoke the '
2557b 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64  collation needed
2557c 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 72 65  ' callback to re
2557d 71 75 65 73 74 20 61 20 63 6f 6c 6c 61 74 69 6f  quest a collatio
2557e 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 69 6e  n sequence.** in
2557f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 65 6e   the encoding en
25580 63 20 6f 66 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c  c of name zName,
25581 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a   length nName..*
25582 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61  /.static void ca
25583 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 73 71 6c  llCollNeeded(sql
25584 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 6e  ite3 *db, int en
25585 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  c, const char *z
25586 4e 61 6d 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Name){.  assert(
25587 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65   !db->xCollNeede
25588 64 20 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e  d || !db->xCollN
25589 65 65 64 65 64 31 36 20 29 3b 0a 20 20 69 66 28  eeded16 );.  if(
2558a 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
2558b 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45   ){.    char *zE
2558c 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  xternal = sqlite
2558d 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e  3DbStrDup(db, zN
2558e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  ame);.    if( !z
2558f 45 78 74 65 72 6e 61 6c 20 29 20 72 65 74 75 72  External ) retur
25590 6e 3b 0a 20 20 20 20 64 62 2d 3e 78 43 6f 6c 6c  n;.    db->xColl
25591 4e 65 65 64 65 64 28 64 62 2d 3e 70 43 6f 6c 6c  Needed(db->pColl
25592 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20 65  NeededArg, db, e
25593 6e 63 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a  nc, zExternal);.
25594 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
25595 65 28 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c 29  e(db, zExternal)
25596 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
25597 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
25598 20 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e    if( db->xCollN
25599 65 65 64 65 64 31 36 20 29 7b 0a 20 20 20 20 63  eeded16 ){.    c
2559a 68 61 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74 65  har const *zExte
2559b 72 6e 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65  rnal;.    sqlite
2559c 33 5f 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d 20  3_value *pTmp = 
2559d 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
2559e 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  db);.    sqlite3
2559f 56 61 6c 75 65 53 65 74 53 74 72 28 70 54 6d 70  ValueSetStr(pTmp
255a0 2c 20 2d 31 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  , -1, zName, SQL
255a1 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
255a2 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7a 45  _STATIC);.    zE
255a3 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  xternal = sqlite
255a4 33 56 61 6c 75 65 54 65 78 74 28 70 54 6d 70 2c  3ValueText(pTmp,
255a5 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
255a6 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20 7a 45  IVE);.    if( zE
255a7 78 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20  xternal ){.     
255a8 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
255a9 31 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  16(db->pCollNeed
255aa 65 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74 29  edArg, db, (int)
255ab 45 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72 6e  ENC(db), zExtern
255ac 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  al);.    }.    s
255ad 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
255ae 70 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  pTmp);.  }.#endi
255af 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
255b0 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
255b1 64 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  d if the collati
255b2 6f 6e 20 66 61 63 74 6f 72 79 20 66 61 69 6c 73  on factory fails
255b3 20 74 6f 20 64 65 6c 69 76 65 72 20 61 0a 2a 2a   to deliver a.**
255b4 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74   collation funct
255b5 69 6f 6e 20 69 6e 20 74 68 65 20 62 65 73 74 20  ion in the best 
255b6 65 6e 63 6f 64 69 6e 67 20 62 75 74 20 74 68 65  encoding but the
255b7 72 65 20 6d 61 79 20 62 65 20 6f 74 68 65 72 20  re may be other 
255b8 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74  versions.** of t
255b9 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75  his collation fu
255ba 6e 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74 68 65  nction (for othe
255bb 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 73  r text encodings
255bc 29 20 61 76 61 69 6c 61 62 6c 65 2e 20 55 73 65  ) available. Use
255bd 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 73 65   one.** of these
255be 20 69 6e 73 74 65 61 64 20 69 66 20 74 68 65 79   instead if they
255bf 20 65 78 69 73 74 2e 20 41 76 6f 69 64 20 61 20   exist. Avoid a 
255c0 55 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d 31 36  UTF-8 <-> UTF-16
255c1 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 0a 2a   conversion if.*
255c2 2a 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73  * possible..*/.s
255c3 74 61 74 69 63 20 69 6e 74 20 73 79 6e 74 68 43  tatic int synthC
255c4 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 20 2a  ollSeq(sqlite3 *
255c5 64 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  db, CollSeq *pCo
255c6 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  ll){.  CollSeq *
255c7 70 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72 20 2a  pColl2;.  char *
255c8 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  z = pColl->zName
255c9 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 61  ;.  int i;.  sta
255ca 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 45 6e  tic const u8 aEn
255cb 63 5b 5d 20 3d 20 7b 20 53 51 4c 49 54 45 5f 55  c[] = { SQLITE_U
255cc 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55  TF16BE, SQLITE_U
255cd 54 46 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f 55  TF16LE, SQLITE_U
255ce 54 46 38 20 7d 3b 0a 20 20 66 6f 72 28 69 3d 30  TF8 };.  for(i=0
255cf 3b 20 69 3c 33 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<3; i++){.   
255d0 20 70 43 6f 6c 6c 32 20 3d 20 73 71 6c 69 74 65   pColl2 = sqlite
255d1 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
255d2 20 61 45 6e 63 5b 69 5d 2c 20 7a 2c 20 30 29 3b   aEnc[i], z, 0);
255d3 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 32 2d  .    if( pColl2-
255d4 3e 78 43 6d 70 21 3d 30 20 29 7b 0a 20 20 20 20  >xCmp!=0 ){.    
255d5 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c 6c 2c 20    memcpy(pColl, 
255d6 70 43 6f 6c 6c 32 2c 20 73 69 7a 65 6f 66 28 43  pColl2, sizeof(C
255d7 6f 6c 6c 53 65 71 29 29 3b 0a 20 20 20 20 20 20  ollSeq));.      
255d8 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b  pColl->xDel = 0;
255d9 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e           /* Do n
255da 6f 74 20 63 6f 70 79 20 74 68 65 20 64 65 73 74  ot copy the dest
255db 72 75 63 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20  ructor */.      
255dc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
255dd 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
255de 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
255df 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  R;.}../*.** This
255e0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73   function is res
255e1 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76  ponsible for inv
255e2 6f 6b 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74  oking the collat
255e3 69 6f 6e 20 66 61 63 74 6f 72 79 20 63 61 6c 6c  ion factory call
255e4 62 61 63 6b 0a 2a 2a 20 6f 72 20 73 75 62 73 74  back.** or subst
255e5 69 74 75 74 69 6e 67 20 61 20 63 6f 6c 6c 61 74  ituting a collat
255e6 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ion sequence of 
255e7 61 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f  a different enco
255e8 64 69 6e 67 20 77 68 65 6e 20 74 68 65 0a 2a 2a  ding when the.**
255e9 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61   requested colla
255ea 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73  tion sequence is
255eb 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69   not available i
255ec 6e 20 74 68 65 20 64 65 73 69 72 65 64 20 65 6e  n the desired en
255ed 63 6f 64 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49  coding..** .** I
255ee 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f it is not NULL
255ef 2c 20 74 68 65 6e 20 70 43 6f 6c 6c 20 6d 75 73  , then pColl mus
255f0 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 64  t point to the d
255f1 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65  atabase native e
255f2 6e 63 6f 64 69 6e 67 20 0a 2a 2a 20 63 6f 6c 6c  ncoding .** coll
255f3 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
255f4 69 74 68 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20  ith name zName, 
255f5 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a  length nName..**
255f6 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
255f7 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 74  alue is either t
255f8 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
255f9 75 65 6e 63 65 20 74 6f 20 62 65 20 75 73 65 64  uence to be used
255fa 20 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20   in database.** 
255fb 64 62 20 66 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e  db for collation
255fc 20 74 79 70 65 20 6e 61 6d 65 20 7a 4e 61 6d 65   type name zName
255fd 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2c 20  , length nName, 
255fe 6f 72 20 4e 55 4c 4c 2c 20 69 66 20 6e 6f 20 63  or NULL, if no c
255ff 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75  ollation.** sequ
25600 65 6e 63 65 20 63 61 6e 20 62 65 20 66 6f 75 6e  ence can be foun
25601 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  d..**.** See als
25602 6f 3a 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  o: sqlite3Locate
25603 43 6f 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69 74  CollSeq(), sqlit
25604 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 29 0a  e3FindCollSeq().
25605 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
25606 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
25607 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 0a 20 20  e3GetCollSeq(.  
25608 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20 20  sqlite3* db,    
25609 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2560a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2560b 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
2560c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2560d 68 65 20 64 65 73 69 72 65 64 20 65 6e 63 6f 64  he desired encod
2560e 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c  ing for the coll
2560f 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a  ating sequence *
25610 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  /.  CollSeq *pCo
25611 6c 6c 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ll,       /* Col
25612 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
25613 77 69 74 68 20 6e 61 74 69 76 65 20 65 6e 63 6f  with native enco
25614 64 69 6e 67 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  ding, or NULL */
25615 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
25616 4e 61 6d 65 20 20 20 20 20 2f 2a 20 43 6f 6c 6c  Name     /* Coll
25617 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6e  ating sequence n
25618 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c  ame */.){.  Coll
25619 53 65 71 20 2a 70 3b 0a 0a 20 20 70 20 3d 20 70  Seq *p;..  p = p
2561a 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 21 70 20 29  Coll;.  if( !p )
2561b 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  {.    p = sqlite
2561c 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
2561d 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b   enc, zName, 0);
2561e 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 7c 7c  .  }.  if( !p ||
2561f 20 21 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20   !p->xCmp ){.   
25620 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e   /* No collation
25621 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 69   sequence of thi
25622 73 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 20  s type for this 
25623 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 67 69  encoding is regi
25624 73 74 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20 43  stered..    ** C
25625 61 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  all the collatio
25626 6e 20 66 61 63 74 6f 72 79 20 74 6f 20 73 65 65  n factory to see
25627 20 69 66 20 69 74 20 63 61 6e 20 73 75 70 70 6c   if it can suppl
25628 79 20 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a 20  y us with one.. 
25629 20 20 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 6f     */.    callCo
2562a 6c 6c 4e 65 65 64 65 64 28 64 62 2c 20 65 6e 63  llNeeded(db, enc
2562b 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 20  , zName);.    p 
2562c 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  = sqlite3FindCol
2562d 6c 53 65 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e  lSeq(db, enc, zN
2562e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  ame, 0);.  }.  i
2562f 66 28 20 70 20 26 26 20 21 70 2d 3e 78 43 6d 70  f( p && !p->xCmp
25630 20 26 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71   && synthCollSeq
25631 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 70  (db, p) ){.    p
25632 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65   = 0;.  }.  asse
25633 72 74 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d  rt( !p || p->xCm
25634 70 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  p );.  return p;
25635 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
25636 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
25637 20 6f 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20   on a collation 
25638 73 65 71 75 65 6e 63 65 20 62 65 66 6f 72 65 20  sequence before 
25639 69 74 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a  it is used to.**
2563a 20 63 68 65 63 6b 20 74 68 61 74 20 69 74 20 69   check that it i
2563b 73 20 64 65 66 69 6e 65 64 2e 20 41 6e 20 75 6e  s defined. An un
2563c 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f  defined collatio
2563d 6e 20 73 65 71 75 65 6e 63 65 20 65 78 69 73 74  n sequence exist
2563e 73 20 77 68 65 6e 0a 2a 2a 20 61 20 64 61 74 61  s when.** a data
2563f 62 61 73 65 20 69 73 20 6c 6f 61 64 65 64 20 74  base is loaded t
25640 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 72 65 66  hat contains ref
25641 65 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c 6c 61  erences to colla
25642 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 0a 2a  tion sequences.*
25643 2a 20 74 68 61 74 20 68 61 76 65 20 6e 6f 74 20  * that have not 
25644 62 65 65 6e 20 64 65 66 69 6e 65 64 20 62 79 20  been defined by 
25645 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
25646 6f 6c 6c 61 74 69 6f 6e 28 29 20 65 74 63 2e 0a  ollation() etc..
25647 2a 2a 0a 2a 2a 20 49 66 20 72 65 71 75 69 72 65  **.** If require
25648 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  d, this routine 
25649 63 61 6c 6c 73 20 74 68 65 20 27 63 6f 6c 6c 61  calls the 'colla
2564a 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c  tion needed' cal
2564b 6c 62 61 63 6b 20 74 6f 0a 2a 2a 20 72 65 71 75  lback to.** requ
2564c 65 73 74 20 61 20 64 65 66 69 6e 69 74 69 6f 6e  est a definition
2564d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e   of the collatin
2564e 67 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 74  g sequence. If t
2564f 68 69 73 20 64 6f 65 73 6e 27 74 20 77 6f 72 6b  his doesn't work
25650 2c 20 0a 2a 2a 20 61 6e 20 65 71 75 69 76 61 6c  , .** an equival
25651 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ent collating se
25652 71 75 65 6e 63 65 20 74 68 61 74 20 75 73 65 73  quence that uses
25653 20 61 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67   a text encoding
25654 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72   different.** fr
25655 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  om the main data
25656 62 61 73 65 20 69 73 20 73 75 62 73 74 69 74 75  base is substitu
25657 74 65 64 2c 20 69 66 20 6f 6e 65 20 69 73 20 61  ted, if one is a
25658 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  vailable..*/.SQL
25659 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2565a 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c  sqlite3CheckColl
2565b 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73  Seq(Parse *pPars
2565c 65 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  e, CollSeq *pCol
2565d 6c 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20  l){.  if( pColl 
2565e 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
2565f 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c  r *zName = pColl
25660 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 73 71 6c  ->zName;.    sql
25661 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
25662 65 2d 3e 64 62 3b 0a 20 20 20 20 43 6f 6c 6c 53  e->db;.    CollS
25663 65 71 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 47  eq *p = sqlite3G
25664 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e  etCollSeq(db, EN
25665 43 28 64 62 29 2c 20 70 43 6f 6c 6c 2c 20 7a 4e  C(db), pColl, zN
25666 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70  ame);.    if( !p
25667 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
25668 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
25669 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61  , "no such colla
2566a 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25  tion sequence: %
2566b 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
2566c 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
2566d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
2566e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2566f 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
25670 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 7d 0a 20  ==pColl );.  }. 
25671 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
25672 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  K;.}..../*.** Lo
25673 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  cate and return 
25674 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  an entry from th
25675 65 20 64 62 2e 61 43 6f 6c 6c 53 65 71 20 68 61  e db.aCollSeq ha
25676 73 68 20 74 61 62 6c 65 2e 20 49 66 20 74 68 65  sh table. If the
25677 20 65 6e 74 72 79 0a 2a 2a 20 73 70 65 63 69 66   entry.** specif
25678 69 65 64 20 62 79 20 7a 4e 61 6d 65 20 61 6e 64  ied by zName and
25679 20 6e 4e 61 6d 65 20 69 73 20 6e 6f 74 20 66 6f   nName is not fo
2567a 75 6e 64 20 61 6e 64 20 70 61 72 61 6d 65 74 65  und and paramete
2567b 72 20 27 63 72 65 61 74 65 27 20 69 73 0a 2a 2a  r 'create' is.**
2567c 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65 61   true, then crea
2567d 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 2e 20  te a new entry. 
2567e 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e  Otherwise return
2567f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61 63   NULL..**.** Eac
25680 68 20 70 6f 69 6e 74 65 72 20 73 74 6f 72 65 64  h pointer stored
25681 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e   in the sqlite3.
25682 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74 61  aCollSeq hash ta
25683 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a  ble contains an.
25684 2a 2a 20 61 72 72 61 79 20 6f 66 20 74 68 72 65  ** array of thre
25685 65 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74  e CollSeq struct
25686 75 72 65 73 2e 20 54 68 65 20 66 69 72 73 74 20  ures. The first 
25687 69 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  is the collation
25688 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 70 72 65   sequence.** pre
25689 66 66 65 72 72 65 64 20 66 6f 72 20 55 54 46 2d  fferred for UTF-
2568a 38 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 55 54  8, the second UT
2568b 46 2d 31 36 6c 65 2c 20 61 6e 64 20 74 68 65 20  F-16le, and the 
2568c 74 68 69 72 64 20 55 54 46 2d 31 36 62 65 2e 0a  third UTF-16be..
2568d 2a 2a 0a 2a 2a 20 53 74 6f 72 65 64 20 69 6d 6d  **.** Stored imm
2568e 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74  ediately after t
2568f 68 65 20 74 68 72 65 65 20 63 6f 6c 6c 61 74 69  he three collati
25690 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 69 73 20  on sequences is 
25691 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65  a copy of.** the
25692 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
25693 6e 63 65 20 6e 61 6d 65 2e 20 41 20 70 6f 69 6e  nce name. A poin
25694 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 69  ter to this stri
25695 6e 67 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a  ng is stored in.
25696 2a 2a 20 65 61 63 68 20 63 6f 6c 6c 61 74 69 6f  ** each collatio
25697 6e 20 73 65 71 75 65 6e 63 65 20 73 74 72 75 63  n sequence struc
25698 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ture..*/.static 
25699 43 6f 6c 6c 53 65 71 20 2a 66 69 6e 64 43 6f 6c  CollSeq *findCol
2569a 6c 53 65 71 45 6e 74 72 79 28 0a 20 20 73 71 6c  lSeqEntry(.  sql
2569b 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2569c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2569d 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
2569e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2569f 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
256a0 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
256a1 71 75 65 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20  quence */.  int 
256a2 63 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20  create          
256a3 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65    /* Create a ne
256a4 77 20 65 6e 74 72 79 20 69 66 20 74 72 75 65 20  w entry if true 
256a5 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  */.){.  CollSeq 
256a6 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 6e 4e  *pColl;.  int nN
256a7 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
256a8 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
256a9 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48  pColl = sqlite3H
256aa 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f  ashFind(&db->aCo
256ab 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  llSeq, zName, nN
256ac 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 30 3d 3d  ame);..  if( 0==
256ad 70 43 6f 6c 6c 20 26 26 20 63 72 65 61 74 65 20  pColl && create 
256ae 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  ){.    pColl = s
256af 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
256b0 72 6f 28 64 62 2c 20 33 2a 73 69 7a 65 6f 66 28  ro(db, 3*sizeof(
256b1 2a 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20  *pColl) + nName 
256b2 2b 20 31 20 29 3b 0a 20 20 20 20 69 66 28 20 70  + 1 );.    if( p
256b3 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 43 6f  Coll ){.      Co
256b4 6c 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 30 3b  llSeq *pDel = 0;
256b5 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e  .      pColl[0].
256b6 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26  zName = (char*)&
256b7 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20  pColl[3];.      
256b8 70 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53  pColl[0].enc = S
256b9 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
256ba 20 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65    pColl[1].zName
256bb 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c   = (char*)&pColl
256bc 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  [3];.      pColl
256bd 5b 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45  [1].enc = SQLITE
256be 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 20 20  _UTF16LE;.      
256bf 70 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d  pColl[2].zName =
256c0 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33   (char*)&pColl[3
256c1 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32  ];.      pColl[2
256c2 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ].enc = SQLITE_U
256c3 54 46 31 36 42 45 3b 0a 20 20 20 20 20 20 6d 65  TF16BE;.      me
256c4 6d 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e  mcpy(pColl[0].zN
256c5 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
256c6 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b  e);.      pColl[
256c7 30 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20  0].zName[nName] 
256c8 3d 20 30 3b 0a 20 20 20 20 20 20 70 44 65 6c 20  = 0;.      pDel 
256c9 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
256ca 65 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ert(&db->aCollSe
256cb 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d  q, pColl[0].zNam
256cc 65 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29  e, nName, pColl)
256cd 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61  ;..      /* If a
256ce 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
256cf 65 20 6f 63 63 75 72 72 65 64 20 69 6e 20 73 71  e occurred in sq
256d0 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
256d1 29 2c 20 69 74 20 77 69 6c 6c 20 0a 20 20 20 20  ), it will .    
256d2 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20    ** return the 
256d3 70 43 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74 6f  pColl pointer to
256d4 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 65 63   be deleted (bec
256d5 61 75 73 65 20 69 74 20 77 61 73 6e 27 74 20 61  ause it wasn't a
256d6 64 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f  dded.      ** to
256d7 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 29   the hash table)
256d8 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
256d9 20 61 73 73 65 72 74 28 20 70 44 65 6c 3d 3d 30   assert( pDel==0
256da 20 7c 7c 20 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20   || pDel==pColl 
256db 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65  );.      if( pDe
256dc 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  l!=0 ){.        
256dd 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
256de 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71   = 1;.        sq
256df 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
256e0 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 70  pDel);.        p
256e1 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  Coll = 0;.      
256e2 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
256e3 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  turn pColl;.}../
256e4 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
256e5 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61  Name points to a
256e6 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 73   UTF-8 encoded s
256e7 74 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79 74 65  tring nName byte
256e8 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75 72  s long..** Retur
256e9 6e 20 74 68 65 20 43 6f 6c 6c 53 65 71 2a 20 70  n the CollSeq* p
256ea 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20 63  ointer for the c
256eb 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
256ec 65 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a  e named zName.**
256ed 20 66 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e   for the encodin
256ee 67 20 27 65 6e 63 27 20 66 72 6f 6d 20 74 68 65  g 'enc' from the
256ef 20 64 61 74 61 62 61 73 65 20 27 64 62 27 2e 0a   database 'db'..
256f0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 6e 74  **.** If the ent
256f1 72 79 20 73 70 65 63 69 66 69 65 64 20 69 73 20  ry specified is 
256f2 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 27 63  not found and 'c
256f3 72 65 61 74 65 27 20 69 73 20 74 72 75 65 2c 20  reate' is true, 
256f4 74 68 65 6e 20 63 72 65 61 74 65 20 61 0a 2a 2a  then create a.**
256f5 20 6e 65 77 20 65 6e 74 72 79 2e 20 20 4f 74 68   new entry.  Oth
256f6 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55  erwise return NU
256f7 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 70 61  LL..**.** A sepa
256f8 72 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 71  rate function sq
256f9 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
256fa 65 71 28 29 20 69 73 20 61 20 77 72 61 70 70 65  eq() is a wrappe
256fb 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 69 73  r around.** this
256fc 20 72 6f 75 74 69 6e 65 2e 20 20 73 71 6c 69 74   routine.  sqlit
256fd 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
256fe 29 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f  ) invokes the co
256ff 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 0a  llation factory.
25700 2a 2a 20 69 66 20 6e 65 63 65 73 73 61 72 79 20  ** if necessary 
25701 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e  and generates an
25702 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
25703 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  f the collating 
25704 73 65 71 75 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e  sequence.** cann
25705 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2a 0a  ot be found..**.
25706 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c  ** See also: sql
25707 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
25708 71 28 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43  q(), sqlite3GetC
25709 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c 49  ollSeq().*/.SQLI
2570a 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
2570b 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43  eq *sqlite3FindC
2570c 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65  ollSeq(.  sqlite
2570d 33 20 2a 64 62 2c 0a 20 20 75 38 20 65 6e 63 2c  3 *db,.  u8 enc,
2570e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2570f 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65 61  Name,.  int crea
25710 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  te.){.  CollSeq 
25711 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e  *pColl;.  if( zN
25712 61 6d 65 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c  ame ){.    pColl
25713 20 3d 20 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e   = findCollSeqEn
25714 74 72 79 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 63  try(db, zName, c
25715 72 65 61 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  reate);.  }else{
25716 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d  .    pColl = db-
25717 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a  >pDfltColl;.  }.
25718 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45    assert( SQLITE
25719 5f 55 54 46 38 3d 3d 31 20 26 26 20 53 51 4c 49  _UTF8==1 && SQLI
2571a 54 45 5f 55 54 46 31 36 4c 45 3d 3d 32 20 26 26  TE_UTF16LE==2 &&
2571b 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3d   SQLITE_UTF16BE=
2571c 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =3 );.  assert( 
2571d 65 6e 63 3e 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc>=SQLITE_UTF8
2571e 20 26 26 20 65 6e 63 3c 3d 53 51 4c 49 54 45 5f   && enc<=SQLITE_
2571f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28  UTF16BE );.  if(
25720 20 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b   pColl ) pColl +
25721 3d 20 65 6e 63 2d 31 3b 0a 20 20 72 65 74 75 72  = enc-1;.  retur
25722 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44  n pColl;.}../* D
25723 75 72 69 6e 67 20 74 68 65 20 73 65 61 72 63 68  uring the search
25724 20 66 6f 72 20 74 68 65 20 62 65 73 74 20 66 75   for the best fu
25725 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
25726 6e 2c 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  n, this procedur
25727 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74  e.** is called t
25728 6f 20 74 65 73 74 20 68 6f 77 20 77 65 6c 6c 20  o test how well 
25729 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73  the function pas
2572a 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
2572b 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74   argument.** mat
2572c 63 68 65 73 20 74 68 65 20 72 65 71 75 65 73 74  ches the request
2572d 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20   for a function 
2572e 77 69 74 68 20 6e 41 72 67 20 61 72 67 75 6d 65  with nArg argume
2572f 6e 74 73 20 69 6e 20 61 20 73 79 73 74 65 6d 0a  nts in a system.
25730 2a 2a 20 74 68 61 74 20 75 73 65 73 20 65 6e 63  ** that uses enc
25731 6f 64 69 6e 67 20 65 6e 63 2e 20 54 68 65 20 76  oding enc. The v
25732 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e  alue returned in
25733 64 69 63 61 74 65 73 20 68 6f 77 20 77 65 6c 6c  dicates how well
25734 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 20   the.** request 
25735 69 73 20 6d 61 74 63 68 65 64 2e 20 41 20 68 69  is matched. A hi
25736 67 68 65 72 20 76 61 6c 75 65 20 69 6e 64 69 63  gher value indic
25737 61 74 65 73 20 61 20 62 65 74 74 65 72 20 6d 61  ates a better ma
25738 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  tch..**.** The r
25739 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73  eturned value is
2573a 20 61 6c 77 61 79 73 20 62 65 74 77 65 65 6e 20   always between 
2573b 30 20 61 6e 64 20 36 2c 20 61 73 20 66 6f 6c 6c  0 and 6, as foll
2573c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 30 3a 20 4e 6f  ows:.**.** 0: No
2573d 74 20 61 20 6d 61 74 63 68 2c 20 6f 72 20 69 66  t a match, or if
2573e 20 6e 41 72 67 3c 30 20 61 6e 64 20 74 68 65 20   nArg<0 and the 
2573f 66 75 6e 63 74 69 6f 6e 20 69 73 20 68 61 73 20  function is has 
25740 6e 6f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  no implementatio
25741 6e 2e 0a 2a 2a 20 31 3a 20 41 20 76 61 72 69 61  n..** 1: A varia
25742 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75  ble arguments fu
25743 6e 63 74 69 6f 6e 20 74 68 61 74 20 70 72 65 66  nction that pref
25744 65 72 73 20 55 54 46 2d 38 20 77 68 65 6e 20 61  ers UTF-8 when a
25745 20 55 54 46 2d 31 36 0a 2a 2a 20 20 20 20 65 6e   UTF-16.**    en
25746 63 6f 64 69 6e 67 20 69 73 20 72 65 71 75 65 73  coding is reques
25747 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72  ted, or vice ver
25748 73 61 2e 0a 2a 2a 20 32 3a 20 41 20 76 61 72 69  sa..** 2: A vari
25749 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66  able arguments f
2574a 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65  unction that use
2574b 73 20 55 54 46 2d 31 36 42 45 20 77 68 65 6e 20  s UTF-16BE when 
2574c 55 54 46 2d 31 36 4c 45 20 69 73 0a 2a 2a 20 20  UTF-16LE is.**  
2574d 20 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20    requested, or 
2574e 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 33  vice versa..** 3
2574f 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67  : A variable arg
25750 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20  uments function 
25751 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 74  using the same t
25752 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a  ext encoding..**
25753 20 34 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77   4: A function w
25754 69 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75  ith the exact nu
25755 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
25756 73 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74  s requested that
25757 0a 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20 55  .**    prefers U
25758 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d  TF-8 when a UTF-
25759 31 36 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72  16 encoding is r
2575a 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63  equested, or vic
2575b 65 20 76 65 72 73 61 2e 0a 2a 2a 20 35 3a 20 41  e versa..** 5: A
2575c 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
2575d 68 65 20 65 78 61 63 74 20 6e 75 6d 62 65 72 20  he exact number 
2575e 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71  of arguments req
2575f 75 65 73 74 65 64 20 74 68 61 74 0a 2a 2a 20 20  uested that.**  
25760 20 20 70 72 65 66 65 72 73 20 55 54 46 2d 31 36    prefers UTF-16
25761 4c 45 20 77 68 65 6e 20 55 54 46 2d 31 36 42 45  LE when UTF-16BE
25762 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f   is requested, o
25763 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a  r vice versa..**
25764 20 36 3a 20 41 6e 20 65 78 61 63 74 20 6d 61 74   6: An exact mat
25765 63 68 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63  ch..**.*/.static
25766 20 69 6e 74 20 6d 61 74 63 68 51 75 61 6c 69 74   int matchQualit
25767 79 28 46 75 6e 63 44 65 66 20 2a 70 2c 20 69 6e  y(FuncDef *p, in
25768 74 20 6e 41 72 67 2c 20 75 38 20 65 6e 63 29 7b  t nArg, u8 enc){
25769 0a 20 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30  .  int match = 0
2576a 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 41 72 67 3d  ;.  if( p->nArg=
2576b 3d 2d 31 20 7c 7c 20 70 2d 3e 6e 41 72 67 3d 3d  =-1 || p->nArg==
2576c 6e 41 72 67 20 0a 20 20 20 7c 7c 20 28 6e 41 72  nArg .   || (nAr
2576d 67 3d 3d 2d 31 20 26 26 20 28 70 2d 3e 78 46 75  g==-1 && (p->xFu
2576e 6e 63 21 3d 30 20 7c 7c 20 70 2d 3e 78 53 74 65  nc!=0 || p->xSte
2576f 70 21 3d 30 29 29 0a 20 20 29 7b 0a 20 20 20 20  p!=0)).  ){.    
25770 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 69  match = 1;.    i
25771 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67  f( p->nArg==nArg
25772 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a   || nArg==-1 ){.
25773 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b        match = 4;
25774 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65  .    }.    if( e
25775 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20  nc==p->iPrefEnc 
25776 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b  ){.      match +
25777 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65  = 2;.    }.    e
25778 6c 73 65 20 69 66 28 20 28 65 6e 63 3d 3d 53 51  lse if( (enc==SQ
25779 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 26 26 20  LITE_UTF16LE && 
2577a 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c  p->iPrefEnc==SQL
2577b 49 54 45 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a  ITE_UTF16BE) ||.
2577c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 6e               (en
2577d 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
2577e 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63  E && p->iPrefEnc
2577f 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
25780 29 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68  ) ){.      match
25781 20 2b 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d   += 1;.    }.  }
25782 0a 20 20 72 65 74 75 72 6e 20 6d 61 74 63 68 3b  .  return match;
25783 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68  .}../*.** Search
25784 20 61 20 46 75 6e 63 44 65 66 48 61 73 68 20 66   a FuncDefHash f
25785 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69  or a function wi
25786 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d  th the given nam
25787 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20  e.  Return.** a 
25788 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d  pointer to the m
25789 61 74 63 68 69 6e 67 20 46 75 6e 63 44 65 66 20  atching FuncDef 
2578a 69 66 20 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69  if found, or 0 i
2578b 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
2578c 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46  tch..*/.static F
2578d 75 6e 63 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e  uncDef *function
2578e 53 65 61 72 63 68 28 0a 20 20 46 75 6e 63 44 65  Search(.  FuncDe
2578f 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f  fHash *pHash,  /
25790 2a 20 48 61 73 68 20 74 61 62 6c 65 20 74 6f 20  * Hash table to 
25791 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20  search */.  int 
25792 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h,              
25793 20 2f 2a 20 48 61 73 68 20 6f 66 20 74 68 65 20   /* Hash of the 
25794 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
25795 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 2f  char *zFunc,   /
25796 2a 20 4e 61 6d 65 20 6f 66 20 66 75 6e 63 74 69  * Name of functi
25797 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e  on */.  int nFun
25798 63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  c            /* 
25799 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
2579a 69 6e 20 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20  in zFunc */.){. 
2579b 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 66   FuncDef *p;.  f
2579c 6f 72 28 70 3d 70 48 61 73 68 2d 3e 61 5b 68 5d  or(p=pHash->a[h]
2579d 3b 20 70 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29  ; p; p=p->pHash)
2579e 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2579f 33 53 74 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61  3StrNICmp(p->zNa
257a0 6d 65 2c 20 7a 46 75 6e 63 2c 20 6e 46 75 6e 63  me, zFunc, nFunc
257a1 29 3d 3d 30 20 26 26 20 70 2d 3e 7a 4e 61 6d 65  )==0 && p->zName
257a2 5b 6e 46 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20  [nFunc]==0 ){.  
257a3 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20      return p;.  
257a4 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
257a5 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73   0;.}../*.** Ins
257a6 65 72 74 20 61 20 6e 65 77 20 46 75 6e 63 44 65  ert a new FuncDe
257a7 66 20 69 6e 74 6f 20 61 20 46 75 6e 63 44 65 66  f into a FuncDef
257a8 48 61 73 68 20 68 61 73 68 20 74 61 62 6c 65 2e  Hash hash table.
257a9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
257aa 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
257ab 75 6e 63 44 65 66 49 6e 73 65 72 74 28 0a 20 20  uncDefInsert(.  
257ac 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61  FuncDefHash *pHa
257ad 73 68 2c 20 20 2f 2a 20 54 68 65 20 68 61 73 68  sh,  /* The hash
257ae 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
257af 68 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20  h to insert */. 
257b0 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 20   FuncDef *pDef  
257b1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e        /* The fun
257b2 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
257b3 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 29 7b   to insert */.){
257b4 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 4f 74 68  .  FuncDef *pOth
257b5 65 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20  er;.  int nName 
257b6 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
257b7 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a  0(pDef->zName);.
257b8 20 20 75 38 20 63 31 20 3d 20 28 75 38 29 70 44    u8 c1 = (u8)pD
257b9 65 66 2d 3e 7a 4e 61 6d 65 5b 30 5d 3b 0a 20 20  ef->zName[0];.  
257ba 69 6e 74 20 68 20 3d 20 28 73 71 6c 69 74 65 33  int h = (sqlite3
257bb 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 31 5d  UpperToLower[c1]
257bc 20 2b 20 6e 4e 61 6d 65 29 20 25 20 41 72 72 61   + nName) % Arra
257bd 79 53 69 7a 65 28 70 48 61 73 68 2d 3e 61 29 3b  ySize(pHash->a);
257be 0a 20 20 70 4f 74 68 65 72 20 3d 20 66 75 6e 63  .  pOther = func
257bf 74 69 6f 6e 53 65 61 72 63 68 28 70 48 61 73 68  tionSearch(pHash
257c0 2c 20 68 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65  , h, pDef->zName
257c1 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  , nName);.  if( 
257c2 70 4f 74 68 65 72 20 29 7b 0a 20 20 20 20 61 73  pOther ){.    as
257c3 73 65 72 74 28 20 70 4f 74 68 65 72 21 3d 70 44  sert( pOther!=pD
257c4 65 66 20 26 26 20 70 4f 74 68 65 72 2d 3e 70 4e  ef && pOther->pN
257c5 65 78 74 21 3d 70 44 65 66 20 29 3b 0a 20 20 20  ext!=pDef );.   
257c6 20 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 70   pDef->pNext = p
257c7 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20  Other->pNext;.  
257c8 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20    pOther->pNext 
257c9 3d 20 70 44 65 66 3b 0a 20 20 7d 65 6c 73 65 7b  = pDef;.  }else{
257ca 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78 74  .    pDef->pNext
257cb 20 3d 20 30 3b 0a 20 20 20 20 70 44 65 66 2d 3e   = 0;.    pDef->
257cc 70 48 61 73 68 20 3d 20 70 48 61 73 68 2d 3e 61  pHash = pHash->a
257cd 5b 68 5d 3b 0a 20 20 20 20 70 48 61 73 68 2d 3e  [h];.    pHash->
257ce 61 5b 68 5d 20 3d 20 70 44 65 66 3b 0a 20 20 7d  a[h] = pDef;.  }
257cf 0a 7d 0a 20 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a 20  .}.  .  ../*.** 
257d0 4c 6f 63 61 74 65 20 61 20 75 73 65 72 20 66 75  Locate a user fu
257d1 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 6e  nction given a n
257d2 61 6d 65 2c 20 61 20 6e 75 6d 62 65 72 20 6f 66  ame, a number of
257d3 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 61   arguments and a
257d4 20 66 6c 61 67 0a 2a 2a 20 69 6e 64 69 63 61 74   flag.** indicat
257d5 69 6e 67 20 77 68 65 74 68 65 72 20 74 68 65 20  ing whether the 
257d6 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 73  function prefers
257d7 20 55 54 46 2d 31 36 20 6f 76 65 72 20 55 54 46   UTF-16 over UTF
257d8 2d 38 2e 20 20 52 65 74 75 72 6e 20 61 0a 2a 2a  -8.  Return a.**
257d9 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
257da 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
257db 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  e that defines t
257dc 68 61 74 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 72  hat function, or
257dd 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20   return.** NULL 
257de 69 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  if the function 
257df 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a  does not exist..
257e0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 72 65  **.** If the cre
257e1 61 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74  ateFlag argument
257e2 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61   is true, then a
257e3 20 6e 65 77 20 28 62 6c 61 6e 6b 29 20 46 75 6e   new (blank) Fun
257e4 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 72  cDef.** structur
257e5 65 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64  e is created and
257e6 20 6c 69 6b 65 64 20 69 6e 74 6f 20 74 68 65 20   liked into the 
257e7 22 64 62 22 20 73 74 72 75 63 74 75 72 65 20 69  "db" structure i
257e8 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 69  f a.** no matchi
257e9 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 76  ng function prev
257ea 69 6f 75 73 6c 79 20 65 78 69 73 74 65 64 2e 20  iously existed. 
257eb 20 57 68 65 6e 20 63 72 65 61 74 65 46 6c 61 67   When createFlag
257ec 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20   is true.** and 
257ed 74 68 65 20 6e 41 72 67 20 70 61 72 61 6d 65 74  the nArg paramet
257ee 65 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 6f  er is -1, then o
257ef 6e 6c 79 20 61 20 66 75 6e 63 74 69 6f 6e 20 74  nly a function t
257f0 68 61 74 20 61 63 63 65 70 74 73 0a 2a 2a 20 61  hat accepts.** a
257f1 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ny number of arg
257f2 75 6d 65 6e 74 73 20 77 69 6c 6c 20 62 65 20 72  uments will be r
257f3 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
257f4 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20  f createFlag is 
257f5 66 61 6c 73 65 20 61 6e 64 20 6e 41 72 67 20 69  false and nArg i
257f6 73 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 66  s -1, then the f
257f7 69 72 73 74 20 76 61 6c 69 64 0a 2a 2a 20 66 75  irst valid.** fu
257f8 6e 63 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73 20  nction found is 
257f9 72 65 74 75 72 6e 65 64 2e 20 20 41 20 66 75 6e  returned.  A fun
257fa 63 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 69  ction is valid i
257fb 66 20 65 69 74 68 65 72 20 78 46 75 6e 63 0a 2a  f either xFunc.*
257fc 2a 20 6f 72 20 78 53 74 65 70 20 69 73 20 6e 6f  * or xStep is no
257fd 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66  n-zero..**.** If
257fe 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 66   createFlag is f
257ff 61 6c 73 65 2c 20 74 68 65 6e 20 61 20 66 75 6e  alse, then a fun
25800 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72  ction with the r
25801 65 71 75 69 72 65 64 20 6e 61 6d 65 20 61 6e 64  equired name and
25802 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  .** number of ar
25803 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 20 72  guments may be r
25804 65 74 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20  eturned even if 
25805 74 68 65 20 65 54 65 78 74 52 65 70 20 66 6c 61  the eTextRep fla
25806 67 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61  g does not.** ma
25807 74 63 68 20 74 68 61 74 20 72 65 71 75 65 73 74  tch that request
25808 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
25809 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73  IVATE FuncDef *s
2580a 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
2580b 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
2580c 62 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f  b,       /* An o
2580d 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  pen database */.
2580e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2580f 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ame, /* Name of 
25810 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 4e  the function.  N
25811 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74  ot null-terminat
25812 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d  ed */.  int nNam
25813 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  e,         /* Nu
25814 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
25815 72 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 2a  rs in the name *
25816 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20  /.  int nArg,   
25817 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
25818 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   of arguments.  
25819 2d 31 20 6d 65 61 6e 73 20 61 6e 79 20 6e 75 6d  -1 means any num
2581a 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c  ber */.  u8 enc,
2581b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2581c 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 6e  referred text en
2581d 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  coding */.  int 
2581e 63 72 65 61 74 65 46 6c 61 67 20 20 20 20 20 2f  createFlag     /
2581f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 65 6e 74  * Create new ent
25820 72 79 20 69 66 20 74 72 75 65 20 61 6e 64 20 64  ry if true and d
25821 6f 65 73 20 6e 6f 74 20 6f 74 68 65 72 77 69 73  oes not otherwis
25822 65 20 65 78 69 73 74 20 2a 2f 0a 29 7b 0a 20 20  e exist */.){.  
25823 46 75 6e 63 44 65 66 20 2a 70 3b 20 20 20 20 20  FuncDef *p;     
25824 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
25825 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46 75  variable */.  Fu
25826 6e 63 44 65 66 20 2a 70 42 65 73 74 20 3d 20 30  ncDef *pBest = 0
25827 3b 20 2f 2a 20 42 65 73 74 20 6d 61 74 63 68 20  ; /* Best match 
25828 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a  found so far */.
25829 20 20 69 6e 74 20 62 65 73 74 53 63 6f 72 65 20    int bestScore 
2582a 3d 20 30 3b 20 20 2f 2a 20 53 63 6f 72 65 20 6f  = 0;  /* Score o
2582b 66 20 62 65 73 74 20 6d 61 74 63 68 20 2a 2f 0a  f best match */.
2582c 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20    int h;        
2582d 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61        /* Hash va
2582e 6c 75 65 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72  lue */...  asser
2582f 74 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  t( enc==SQLITE_U
25830 54 46 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49  TF8 || enc==SQLI
25831 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c 20 65 6e  TE_UTF16LE || en
25832 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
25833 45 20 29 3b 0a 20 20 68 20 3d 20 28 73 71 6c 69  E );.  h = (sqli
25834 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
25835 28 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20  (u8)zName[0]] + 
25836 6e 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69  nName) % ArraySi
25837 7a 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b  ze(db->aFunc.a);
25838 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61  ..  /* First sea
25839 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68 20  rch for a match 
2583a 61 6d 6f 6e 67 73 74 20 74 68 65 20 61 70 70 6c  amongst the appl
2583b 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2583c 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  functions..  */.
2583d 20 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65    p = functionSe
2583e 61 72 63 68 28 26 64 62 2d 3e 61 46 75 6e 63 2c  arch(&db->aFunc,
2583f 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65   h, zName, nName
25840 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  );.  while( p ){
25841 0a 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d  .    int score =
25842 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c   matchQuality(p,
25843 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20   nArg, enc);.   
25844 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53   if( score>bestS
25845 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70 42  core ){.      pB
25846 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 62  est = p;.      b
25847 65 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65  estScore = score
25848 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20  ;.    }.    p = 
25849 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20  p->pNext;.  }.. 
2584a 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 20   /* If no match 
2584b 69 73 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68  is found, search
2584c 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 66 75   the built-in fu
2584d 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20  nctions..  **.  
2584e 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 63 72  ** Except, if cr
2584f 65 61 74 65 46 6c 61 67 20 69 73 20 74 72 75 65  eateFlag is true
25850 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
25851 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20  t we are trying 
25852 74 6f 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20  to.  ** install 
25853 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20  a new function. 
25854 20 57 68 61 74 65 76 65 72 20 46 75 6e 63 44 65   Whatever FuncDe
25855 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72  f structure is r
25856 65 74 75 72 6e 65 64 20 77 69 6c 6c 0a 20 20 2a  eturned will.  *
25857 2a 20 68 61 76 65 20 66 69 65 6c 64 73 20 6f 76  * have fields ov
25858 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 6e  erwritten with n
25859 65 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ew information a
2585a 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
2585b 68 65 0a 20 20 2a 2a 20 6e 65 77 20 66 75 6e 63  he.  ** new func
2585c 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20 46  tion.  But the F
2585d 75 6e 63 44 65 66 73 20 66 6f 72 20 62 75 69 6c  uncDefs for buil
2585e 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 61  t-in functions a
2585f 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20  re read-only..  
25860 2a 2a 20 53 6f 20 77 65 20 6d 75 73 74 20 6e 6f  ** So we must no
25861 74 20 73 65 61 72 63 68 20 66 6f 72 20 62 75 69  t search for bui
25862 6c 74 2d 69 6e 73 20 77 68 65 6e 20 63 72 65 61  lt-ins when crea
25863 74 69 6e 67 20 61 20 6e 65 77 20 66 75 6e 63 74  ting a new funct
25864 69 6f 6e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28  ion..  */ .  if(
25865 20 21 63 72 65 61 74 65 46 6c 61 67 20 26 26 20   !createFlag && 
25866 21 70 42 65 73 74 20 29 7b 0a 20 20 20 20 46 75  !pBest ){.    Fu
25867 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
25868 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44   = &GLOBAL(FuncD
25869 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47  efHash, sqlite3G
2586a 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b  lobalFunctions);
2586b 0a 20 20 20 20 70 20 3d 20 66 75 6e 63 74 69 6f  .    p = functio
2586c 6e 53 65 61 72 63 68 28 70 48 61 73 68 2c 20 68  nSearch(pHash, h
2586d 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
2586e 0a 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b  .    while( p ){
2586f 0a 20 20 20 20 20 20 69 6e 74 20 73 63 6f 72 65  .      int score
25870 20 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28   = matchQuality(
25871 70 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20  p, nArg, enc);. 
25872 20 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e 62       if( score>b
25873 65 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20 20  estScore ){.    
25874 20 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20      pBest = p;. 
25875 20 20 20 20 20 20 20 62 65 73 74 53 63 6f 72 65         bestScore
25876 20 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 20 20   = score;.      
25877 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70  }.      p = p->p
25878 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Next;.    }.  }.
25879 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72 65  .  /* If the cre
2587a 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65  ateFlag paramete
2587b 72 20 69 73 20 74 72 75 65 20 61 6e 64 20 74 68  r is true and th
2587c 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74  e search did not
2587d 20 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a 20   reveal an.  ** 
2587e 65 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20  exact match for 
2587f 74 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65 72  the name, number
25880 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e   of arguments an
25881 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e  d encoding, then
25882 20 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77 20   add a.  ** new 
25883 65 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61 73  entry to the has
25884 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74 75  h table and retu
25885 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  rn it..  */.  if
25886 28 20 63 72 65 61 74 65 46 6c 61 67 20 26 26 20  ( createFlag && 
25887 28 62 65 73 74 53 63 6f 72 65 3c 36 20 7c 7c 20  (bestScore<6 || 
25888 70 42 65 73 74 2d 3e 6e 41 72 67 21 3d 6e 41 72  pBest->nArg!=nAr
25889 67 29 20 26 26 20 0a 20 20 20 20 20 20 28 70 42  g) && .      (pB
2588a 65 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  est = sqlite3DbM
2588b 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2588c 7a 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e 61  zeof(*pBest)+nNa
2588d 6d 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20 20 20  me+1))!=0 ){.   
2588e 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20   pBest->zName = 
2588f 28 63 68 61 72 20 2a 29 26 70 42 65 73 74 5b 31  (char *)&pBest[1
25890 5d 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 6e 41  ];.    pBest->nA
25891 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b 0a  rg = (u16)nArg;.
25892 20 20 20 20 70 42 65 73 74 2d 3e 69 50 72 65 66      pBest->iPref
25893 45 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 20 20 6d  Enc = enc;.    m
25894 65 6d 63 70 79 28 70 42 65 73 74 2d 3e 7a 4e 61  emcpy(pBest->zNa
25895 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  me, zName, nName
25896 29 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a 4e  );.    pBest->zN
25897 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a  ame[nName] = 0;.
25898 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44      sqlite3FuncD
25899 65 66 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 46  efInsert(&db->aF
2589a 75 6e 63 2c 20 70 42 65 73 74 29 3b 0a 20 20 7d  unc, pBest);.  }
2589b 0a 0a 20 20 69 66 28 20 70 42 65 73 74 20 26 26  ..  if( pBest &&
2589c 20 28 70 42 65 73 74 2d 3e 78 53 74 65 70 20 7c   (pBest->xStep |
2589d 7c 20 70 42 65 73 74 2d 3e 78 46 75 6e 63 20 7c  | pBest->xFunc |
2589e 7c 20 63 72 65 61 74 65 46 6c 61 67 29 20 29 7b  | createFlag) ){
2589f 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 65 73  .    return pBes
258a0 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
258a1 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  0;.}../*.** Free
258a2 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 68   all resources h
258a3 65 6c 64 20 62 79 20 74 68 65 20 73 63 68 65 6d  eld by the schem
258a4 61 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65  a structure. The
258a5 20 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e 74 20   void* argument 
258a6 70 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61 20 53  points.** at a S
258a7 63 68 65 6d 61 20 73 74 72 75 63 74 2e 20 54 68  chema struct. Th
258a8 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
258a9 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65   not call sqlite
258aa 33 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f 6e  3DbFree(db, ) on
258ab 20 74 68 65 20 0a 2a 2a 20 70 6f 69 6e 74 65 72   the .** pointer
258ac 20 69 74 73 65 6c 66 2c 20 69 74 20 6a 75 73 74   itself, it just
258ad 20 63 6c 65 61 6e 73 20 75 70 20 73 75 62 73 69   cleans up subsi
258ae 64 75 61 72 79 20 72 65 73 6f 75 72 63 65 73 20  duary resources 
258af 28 69 2e 65 2e 20 74 68 65 20 63 6f 6e 74 65 6e  (i.e. the conten
258b0 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 63 68  ts.** of the sch
258b1 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73 29  ema hash tables)
258b2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 63 68 65  ..**.** The Sche
258b3 6d 61 2e 63 61 63 68 65 5f 73 69 7a 65 20 76 61  ma.cache_size va
258b4 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 6c  riable is not cl
258b5 65 61 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eared..*/.SQLITE
258b6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
258b7 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28  lite3SchemaFree(
258b8 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48 61 73 68  void *p){.  Hash
258b9 20 74 65 6d 70 31 3b 0a 20 20 48 61 73 68 20 74   temp1;.  Hash t
258ba 65 6d 70 32 3b 0a 20 20 48 61 73 68 45 6c 65 6d  emp2;.  HashElem
258bb 20 2a 70 45 6c 65 6d 3b 0a 20 20 53 63 68 65 6d   *pElem;.  Schem
258bc 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 63  a *pSchema = (Sc
258bd 68 65 6d 61 20 2a 29 70 3b 0a 0a 20 20 74 65 6d  hema *)p;..  tem
258be 70 31 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74 62  p1 = pSchema->tb
258bf 6c 48 61 73 68 3b 0a 20 20 74 65 6d 70 32 20 3d  lHash;.  temp2 =
258c0 20 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61   pSchema->trigHa
258c1 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73  sh;.  sqlite3Has
258c2 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61 2d 3e  hInit(&pSchema->
258c3 74 72 69 67 48 61 73 68 29 3b 0a 20 20 73 71 6c  trigHash);.  sql
258c4 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70  ite3HashClear(&p
258c5 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29  Schema->idxHash)
258c6 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71  ;.  for(pElem=sq
258c7 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74  liteHashFirst(&t
258c8 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70 45  emp2); pElem; pE
258c9 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  lem=sqliteHashNe
258ca 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20  xt(pElem)){.    
258cb 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
258cc 67 67 65 72 28 30 2c 20 28 54 72 69 67 67 65 72  gger(0, (Trigger
258cd 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
258ce 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20 20  (pElem));.  }.  
258cf 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
258d0 28 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c 69  (&temp2);.  sqli
258d1 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 63  te3HashInit(&pSc
258d2 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 0a  hema->tblHash);.
258d3 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69    for(pElem=sqli
258d4 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65 6d  teHashFirst(&tem
258d5 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65  p1); pElem; pEle
258d6 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  m=sqliteHashNext
258d7 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61  (pElem)){.    Ta
258d8 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
258d9 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d  teHashData(pElem
258da 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
258db 54 61 62 2d 3e 64 62 4d 65 6d 3d 3d 30 20 29 3b  Tab->dbMem==0 );
258dc 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
258dd 74 65 54 61 62 6c 65 28 70 54 61 62 29 3b 0a 20  teTable(pTab);. 
258de 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68   }.  sqlite3Hash
258df 43 6c 65 61 72 28 26 74 65 6d 70 31 29 3b 0a 20  Clear(&temp1);. 
258e0 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
258e1 72 28 26 70 53 63 68 65 6d 61 2d 3e 66 6b 65 79  r(&pSchema->fkey
258e2 48 61 73 68 29 3b 0a 20 20 70 53 63 68 65 6d 61  Hash);.  pSchema
258e3 2d 3e 70 53 65 71 54 61 62 20 3d 20 30 3b 0a 20  ->pSeqTab = 0;. 
258e4 20 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20   pSchema->flags 
258e5 26 3d 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61  &= ~DB_SchemaLoa
258e6 64 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69  ded;.}../*.** Fi
258e7 6e 64 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  nd and return th
258e8 65 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61  e schema associa
258e9 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65  ted with a BTree
258ea 2e 20 20 43 72 65 61 74 65 0a 2a 2a 20 61 20 6e  .  Create.** a n
258eb 65 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73  ew one if necess
258ec 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ary..*/.SQLITE_P
258ed 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73  RIVATE Schema *s
258ee 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28  qlite3SchemaGet(
258ef 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 72  sqlite3 *db, Btr
258f0 65 65 20 2a 70 42 74 29 7b 0a 20 20 53 63 68 65  ee *pBt){.  Sche
258f1 6d 61 20 2a 20 70 3b 0a 20 20 69 66 28 20 70 42  ma * p;.  if( pB
258f2 74 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 53 63  t ){.    p = (Sc
258f3 68 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 42 74  hema *)sqlite3Bt
258f4 72 65 65 53 63 68 65 6d 61 28 70 42 74 2c 20 73  reeSchema(pBt, s
258f5 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 2c 20 73  izeof(Schema), s
258f6 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65  qlite3SchemaFree
258f7 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
258f8 70 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71  p = (Schema *)sq
258f9 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
258fa 73 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 29 3b  sizeof(Schema));
258fb 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 29 7b  .  }.  if( !p ){
258fc 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
258fd 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c  ailed = 1;.  }el
258fe 73 65 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66 69  se if ( 0==p->fi
258ff 6c 65 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20 20  le_format ){.   
25900 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
25901 28 26 70 2d 3e 74 62 6c 48 61 73 68 29 3b 0a 20  (&p->tblHash);. 
25902 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e     sqlite3HashIn
25903 69 74 28 26 70 2d 3e 69 64 78 48 61 73 68 29 3b  it(&p->idxHash);
25904 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68  .    sqlite3Hash
25905 49 6e 69 74 28 26 70 2d 3e 74 72 69 67 48 61 73  Init(&p->trigHas
25906 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48  h);.    sqlite3H
25907 61 73 68 49 6e 69 74 28 26 70 2d 3e 66 6b 65 79  ashInit(&p->fkey
25908 48 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 65 6e  Hash);.    p->en
25909 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
2590a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
2590b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
2590c 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 61 6c 6c 62  *** End of callb
2590d 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ack.c **********
2590e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2590f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25910 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
25911 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64  *** Begin file d
25912 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  elete.c ********
25913 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25914 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25915 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
25916 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
25917 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
25918 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
25919 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2591a 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2591b 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2591c 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2591d 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2591e 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2591f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
25920 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
25921 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
25922 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
25923 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
25924 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
25925 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
25926 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
25927 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
25928 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
25929 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2592a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2592b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2592c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
2592d 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
2592e 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73   C code routines
2592f 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64   that are called
25930 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a   by the parser.*
25931 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65  * in order to ge
25932 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
25933 44 45 4c 45 54 45 20 46 52 4f 4d 20 73 74 61 74  DELETE FROM stat
25934 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  ements..*/../*.*
25935 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20  * Look up every 
25936 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61  table that is na
25937 6d 65 64 20 69 6e 20 70 53 72 63 2e 20 20 49 66  med in pSrc.  If
25938 20 61 6e 79 20 74 61 62 6c 65 20 69 73 20 6e 6f   any table is no
25939 74 20 66 6f 75 6e 64 2c 0a 2a 2a 20 61 64 64 20  t found,.** add 
2593a 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
2593b 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   to pParse->zErr
2593c 4d 73 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  Msg and return N
2593d 55 4c 4c 2e 20 20 49 66 20 61 6c 6c 20 74 61 62  ULL.  If all tab
2593e 6c 65 73 0a 2a 2a 20 61 72 65 20 66 6f 75 6e 64  les.** are found
2593f 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  , return a point
25940 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 74  er to the last t
25941 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
25942 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
25943 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
25944 6b 75 70 28 50 61 72 73 65 20 2a 70 50 61 72 73  kup(Parse *pPars
25945 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  e, SrcList *pSrc
25946 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  ){.  struct SrcL
25947 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
25948 3d 20 70 53 72 63 2d 3e 61 3b 0a 20 20 54 61 62  = pSrc->a;.  Tab
25949 6c 65 20 2a 70 54 61 62 3b 0a 20 20 61 73 73 65  le *pTab;.  asse
2594a 72 74 28 20 70 49 74 65 6d 20 26 26 20 70 53 72  rt( pItem && pSr
2594b 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  c->nSrc==1 );.  
2594c 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pTab = sqlite3Lo
2594d 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
2594e 2c 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  , 0, pItem->zNam
2594f 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62  e, pItem->zDatab
25950 61 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ase);.  sqlite3D
25951 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d  eleteTable(pItem
25952 2d 3e 70 54 61 62 29 3b 0a 20 20 70 49 74 65 6d  ->pTab);.  pItem
25953 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20  ->pTab = pTab;. 
25954 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
25955 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20   pTab->nRef++;. 
25956 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
25957 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28  IndexedByLookup(
25958 70 50 61 72 73 65 2c 20 70 49 74 65 6d 29 20 29  pParse, pItem) )
25959 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a  {.    pTab = 0;.
2595a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54 61    }.  return pTa
2595b 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  b;.}../*.** Chec
2595c 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  k to make sure t
2595d 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 69  he given table i
2595e 73 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66 20  s writable.  If 
2595f 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 77 72 69  it is not.** wri
25960 74 61 62 6c 65 2c 20 67 65 6e 65 72 61 74 65 20  table, generate 
25961 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
25962 20 61 6e 64 20 72 65 74 75 72 6e 20 31 2e 20 20   and return 1.  
25963 49 66 20 69 74 20 69 73 0a 2a 2a 20 77 72 69 74  If it is.** writ
25964 61 62 6c 65 20 72 65 74 75 72 6e 20 30 3b 0a 2a  able return 0;.*
25965 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
25966 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 65   int sqlite3IsRe
25967 61 64 4f 6e 6c 79 28 50 61 72 73 65 20 2a 70 50  adOnly(Parse *pP
25968 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
25969 62 2c 20 69 6e 74 20 76 69 65 77 4f 6b 29 7b 0a  b, int viewOk){.
2596a 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 73 20    /* A table is 
2596b 6e 6f 74 20 77 72 69 74 61 62 6c 65 20 75 6e 64  not writable und
2596c 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  er the following
2596d 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a   circumstances:.
2596e 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 31 29 20 49    **.  **   1) I
2596f 74 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74  t is a virtual t
25970 61 62 6c 65 20 61 6e 64 20 6e 6f 20 69 6d 70 6c  able and no impl
25971 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
25972 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64  e xUpdate method
25973 0a 20 20 2a 2a 20 20 20 20 20 20 68 61 73 20 62  .  **      has b
25974 65 65 6e 20 70 72 6f 76 69 64 65 64 2c 20 6f 72  een provided, or
25975 0a 20 20 2a 2a 20 20 20 32 29 20 49 74 20 69 73  .  **   2) It is
25976 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20   a system table 
25977 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73  (i.e. sqlite_mas
25978 74 65 72 29 2c 20 74 68 69 73 20 63 61 6c 6c 20  ter), this call 
25979 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 20 20 20 20  is not.  **     
2597a 20 70 61 72 74 20 6f 66 20 61 20 6e 65 73 74 65   part of a neste
2597b 64 20 70 61 72 73 65 20 61 6e 64 20 77 72 69 74  d parse and writ
2597c 61 62 6c 65 5f 73 63 68 65 6d 61 20 70 72 61 67  able_schema prag
2597d 6d 61 20 68 61 73 20 6e 6f 74 20 0a 20 20 2a 2a  ma has not .  **
2597e 20 20 20 20 20 20 62 65 65 6e 20 73 70 65 63 69        been speci
2597f 66 69 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  fied..  **.  ** 
25980 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 20 6c  In either case l
25981 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  eave an error me
25982 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 20  ssage in pParse 
25983 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  and return non-z
25984 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ero..  */.  if( 
25985 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
25986 29 20 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74  ) .     && sqlit
25987 65 33 47 65 74 56 54 61 62 6c 65 28 70 50 61 72  e3GetVTable(pPar
25988 73 65 2d 3e 64 62 2c 20 70 54 61 62 29 2d 3e 70  se->db, pTab)->p
25989 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 55  Mod->pModule->xU
2598a 70 64 61 74 65 3d 3d 30 20 29 0a 20 20 20 7c 7c  pdate==0 ).   ||
2598b 20 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61   ( (pTab->tabFla
2598c 67 73 20 26 20 54 46 5f 52 65 61 64 6f 6e 6c 79  gs & TF_Readonly
2598d 29 21 3d 30 0a 20 20 20 20 20 26 26 20 28 70 50  )!=0.     && (pP
2598e 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  arse->db->flags 
2598f 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63  & SQLITE_WriteSc
25990 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 26 26  hema)==0.     &&
25991 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
25992 3d 30 20 29 0a 20 20 29 7b 0a 20 20 20 20 73 71  =0 ).  ){.    sq
25993 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
25994 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
25995 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66  may not be modif
25996 69 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  ied", pTab->zNam
25997 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
25998 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
25999 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
2599a 20 20 69 66 28 20 21 76 69 65 77 4f 6b 20 26 26    if( !viewOk &&
2599b 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
2599c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2599d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 63 61  orMsg(pParse,"ca
2599e 6e 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73 20 62  nnot modify %s b
2599f 65 63 61 75 73 65 20 69 74 20 69 73 20 61 20 76  ecause it is a v
259a0 69 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65  iew",pTab->zName
259a1 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
259a2 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
259a3 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 23 69 66 20  turn 0;.}...#if 
259a4 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
259a5 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64  OMIT_VIEW) && !d
259a6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
259a7 49 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a 2a  IT_TRIGGER)./*.*
259a8 2a 20 45 76 61 6c 75 61 74 65 20 61 20 76 69 65  * Evaluate a vie
259a9 77 20 61 6e 64 20 73 74 6f 72 65 20 69 74 73 20  w and store its 
259aa 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 65 70 68  result in an eph
259ab 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 20 20 54  emeral table.  T
259ac 68 65 0a 2a 2a 20 70 57 68 65 72 65 20 61 72 67  he.** pWhere arg
259ad 75 6d 65 6e 74 20 69 73 20 61 6e 20 6f 70 74 69  ument is an opti
259ae 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73  onal WHERE claus
259af 65 20 74 68 61 74 20 72 65 73 74 72 69 63 74 73  e that restricts
259b0 20 74 68 65 0a 2a 2a 20 73 65 74 20 6f 66 20 72   the.** set of r
259b1 6f 77 73 20 69 6e 20 74 68 65 20 76 69 65 77 20  ows in the view 
259b2 74 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 61  that are to be a
259b3 64 64 65 64 20 74 6f 20 74 68 65 20 65 70 68 65  dded to the ephe
259b4 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a  meral table..*/.
259b5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
259b6 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72  oid sqlite3Mater
259b7 69 61 6c 69 7a 65 56 69 65 77 28 0a 20 20 50 61  ializeView(.  Pa
259b8 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
259b9 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
259ba 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
259bb 20 2a 70 56 69 65 77 2c 20 20 20 20 20 20 20 20   *pView,        
259bc 2f 2a 20 56 69 65 77 20 64 65 66 69 6e 69 74 69  /* View definiti
259bd 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  on */.  Expr *pW
259be 68 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  here,        /* 
259bf 4f 70 74 69 6f 6e 61 6c 20 57 48 45 52 45 20 63  Optional WHERE c
259c0 6c 61 75 73 65 20 74 6f 20 62 65 20 61 64 64 65  lause to be adde
259c1 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20  d */.  int iCur 
259c2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
259c3 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
259c4 20 65 70 68 65 6d 65 72 69 61 6c 20 74 61 62 6c   ephemerial tabl
259c5 65 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74  e */.){.  Select
259c6 44 65 73 74 20 64 65 73 74 3b 0a 20 20 53 65 6c  Dest dest;.  Sel
259c7 65 63 74 20 2a 70 44 75 70 3b 0a 20 20 73 71 6c  ect *pDup;.  sql
259c8 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
259c9 65 2d 3e 64 62 3b 0a 0a 20 20 70 44 75 70 20 3d  e->db;..  pDup =
259ca 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
259cb 70 28 64 62 2c 20 70 56 69 65 77 2d 3e 70 53 65  p(db, pView->pSe
259cc 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  lect, 0);.  if( 
259cd 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 53 72  pWhere ){.    Sr
259ce 63 4c 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20 20  cList *pFrom;.  
259cf 20 20 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20    .    pWhere = 
259d0 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
259d1 62 2c 20 70 57 68 65 72 65 2c 20 30 29 3b 0a 20  b, pWhere, 0);. 
259d2 20 20 20 70 46 72 6f 6d 20 3d 20 73 71 6c 69 74     pFrom = sqlit
259d3 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
259d4 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  db, 0, 0, 0);.  
259d5 20 20 69 66 28 20 70 46 72 6f 6d 20 29 7b 0a 20    if( pFrom ){. 
259d6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72       assert( pFr
259d7 6f 6d 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  om->nSrc==1 );. 
259d8 20 20 20 20 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d       pFrom->a[0]
259d9 2e 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65  .zAlias = sqlite
259da 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 56  3DbStrDup(db, pV
259db 69 65 77 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  iew->zName);.   
259dc 20 20 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70     pFrom->a[0].p
259dd 53 65 6c 65 63 74 20 3d 20 70 44 75 70 3b 0a 20  Select = pDup;. 
259de 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72       assert( pFr
259df 6f 6d 2d 3e 61 5b 30 5d 2e 70 4f 6e 3d 3d 30 20  om->a[0].pOn==0 
259e0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
259e1 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 55 73   pFrom->a[0].pUs
259e2 69 6e 67 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65  ing==0 );.    }e
259e3 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
259e4 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
259e5 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 7d 0a  b, pDup);.    }.
259e6 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74      pDup = sqlit
259e7 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72  e3SelectNew(pPar
259e8 73 65 2c 20 30 2c 20 70 46 72 6f 6d 2c 20 70 57  se, 0, pFrom, pW
259e9 68 65 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30  here, 0, 0, 0, 0
259ea 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 73  , 0, 0);.  }.  s
259eb 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
259ec 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f  Init(&dest, SRT_
259ed 45 70 68 65 6d 54 61 62 2c 20 69 43 75 72 29 3b  EphemTab, iCur);
259ee 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
259ef 28 70 50 61 72 73 65 2c 20 70 44 75 70 2c 20 26  (pParse, pDup, &
259f0 64 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  dest);.  sqlite3
259f1 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
259f2 20 70 44 75 70 29 3b 0a 7d 0a 23 65 6e 64 69 66   pDup);.}.#endif
259f3 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
259f4 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26  ITE_OMIT_VIEW) &
259f5 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
259f6 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20  E_OMIT_TRIGGER) 
259f7 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
259f8 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50  SQLITE_ENABLE_UP
259f9 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49  DATE_DELETE_LIMI
259fa 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  T) && !defined(S
259fb 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
259fc 45 52 59 29 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ERY)./*.** Gener
259fd 61 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ate an expressio
259fe 6e 20 74 72 65 65 20 74 6f 20 69 6d 70 6c 65 6d  n tree to implem
259ff 65 6e 74 20 74 68 65 20 57 48 45 52 45 2c 20 4f  ent the WHERE, O
25a00 52 44 45 52 20 42 59 2c 0a 2a 2a 20 61 6e 64 20  RDER BY,.** and 
25a01 4c 49 4d 49 54 2f 4f 46 46 53 45 54 20 70 6f 72  LIMIT/OFFSET por
25a02 74 69 6f 6e 20 6f 66 20 44 45 4c 45 54 45 20 61  tion of DELETE a
25a03 6e 64 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  nd UPDATE statem
25a04 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ents..**.**     
25a05 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c  DELETE FROM tabl
25a06 65 5f 77 78 79 7a 20 57 48 45 52 45 20 61 3c 35  e_wxyz WHERE a<5
25a07 20 4f 52 44 45 52 20 42 59 20 61 20 4c 49 4d 49   ORDER BY a LIMI
25a08 54 20 31 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20  T 1;.**         
25a09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a0a 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f     \____________
25a0b 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a  ______________/.
25a0c 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
25a0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a0e 20 70 4c 69 6d 69 74 57 68 65 72 65 20 28 70 49   pLimitWhere (pI
25a0f 6e 43 6c 61 75 73 65 29 0a 2a 2f 0a 53 51 4c 49  nClause).*/.SQLI
25a10 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
25a11 2a 73 71 6c 69 74 65 33 4c 69 6d 69 74 57 68 65  *sqlite3LimitWhe
25a12 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  re(.  Parse *pPa
25a13 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
25a14 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
25a15 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
25a16 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20  cList *pSrc,    
25a17 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68             /* th
25a18 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d  e FROM clause --
25a19 20 77 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f   which tables to
25a1a 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20   scan */.  Expr 
25a1b 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20  *pWhere,        
25a1c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
25a1d 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61  HERE clause.  Ma
25a1e 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45  y be null */.  E
25a1f 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
25a20 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  y,          /* T
25a21 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
25a22 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c  se.  May be null
25a23 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d   */.  Expr *pLim
25a24 69 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  it,             
25a25 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20     /* The LIMIT 
25a26 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
25a27 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 20 2a  null */.  Expr *
25a28 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20  pOffset,        
25a29 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 46         /* The OF
25a2a 46 53 45 54 20 63 6c 61 75 73 65 2e 20 20 4d 61  FSET clause.  Ma
25a2b 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 63  y be null */.  c
25a2c 68 61 72 20 2a 7a 53 74 6d 74 54 79 70 65 20 20  har *zStmtType  
25a2d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
25a2e 69 74 68 65 72 20 44 45 4c 45 54 45 20 6f 72 20  ither DELETE or 
25a2f 55 50 44 41 54 45 2e 20 20 46 6f 72 20 65 72 72  UPDATE.  For err
25a30 6f 72 20 6d 65 73 73 61 67 65 73 2e 20 2a 2f 0a  or messages. */.
25a31 29 7b 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  ){.  Expr *pWher
25a32 65 52 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20  eRowid = NULL;  
25a33 20 20 2f 2a 20 57 48 45 52 45 20 72 6f 77 69 64    /* WHERE rowid
25a34 20 2e 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70   .. */.  Expr *p
25a35 49 6e 43 6c 61 75 73 65 20 3d 20 4e 55 4c 4c 3b  InClause = NULL;
25a36 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 72        /* WHERE r
25a37 6f 77 69 64 20 49 4e 20 28 20 73 65 6c 65 63 74  owid IN ( select
25a38 20 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53   ) */.  Expr *pS
25a39 65 6c 65 63 74 52 6f 77 69 64 20 3d 20 4e 55 4c  electRowid = NUL
25a3a 4c 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 72  L;   /* SELECT r
25a3b 6f 77 69 64 20 2e 2e 2e 20 2a 2f 0a 20 20 45 78  owid ... */.  Ex
25a3c 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d  prList *pEList =
25a3d 20 4e 55 4c 4c 3b 20 20 20 20 20 2f 2a 20 45 78   NULL;     /* Ex
25a3e 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f  pression list co
25a3f 6e 74 61 6e 69 6e 67 20 6f 6e 6c 79 20 70 53 65  ntaning only pSe
25a40 6c 65 63 74 52 6f 77 69 64 20 2a 2f 0a 20 20 53  lectRowid */.  S
25a41 72 63 4c 69 73 74 20 2a 70 53 65 6c 65 63 74 53  rcList *pSelectS
25a42 72 63 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 53  rc = NULL;  /* S
25a43 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d  ELECT rowid FROM
25a44 20 78 20 2e 2e 2e 20 28 64 75 70 20 6f 66 20 70   x ... (dup of p
25a45 53 72 63 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74  Src) */.  Select
25a46 20 2a 70 53 65 6c 65 63 74 20 3d 20 4e 55 4c 4c   *pSelect = NULL
25a47 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65  ;      /* Comple
25a48 74 65 20 53 45 4c 45 43 54 20 74 72 65 65 20 2a  te SELECT tree *
25a49 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  /..  /* Check th
25a4a 61 74 20 74 68 65 72 65 20 69 73 6e 27 74 20 61  at there isn't a
25a4b 6e 20 4f 52 44 45 52 20 42 59 20 77 69 74 68 6f  n ORDER BY witho
25a4c 75 74 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  ut a LIMIT claus
25a4d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f  e..  */.  if( pO
25a4e 72 64 65 72 42 79 20 26 26 20 28 70 4c 69 6d 69  rderBy && (pLimi
25a4f 74 20 3d 3d 20 30 29 20 29 20 7b 0a 20 20 20 20  t == 0) ) {.    
25a50 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
25a51 70 50 61 72 73 65 2c 20 22 4f 52 44 45 52 20 42  pParse, "ORDER B
25a52 59 20 77 69 74 68 6f 75 74 20 4c 49 4d 49 54 20  Y without LIMIT 
25a53 6f 6e 20 25 73 22 2c 20 7a 53 74 6d 74 54 79 70  on %s", zStmtTyp
25a54 65 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  e);.    pParse->
25a55 70 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a  parseError = 1;.
25a56 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77      goto limit_w
25a57 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a  here_cleanup_2;.
25a58 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 6f 6e 6c    }..  /* We onl
25a59 79 20 6e 65 65 64 20 74 6f 20 67 65 6e 65 72 61  y need to genera
25a5a 74 65 20 61 20 73 65 6c 65 63 74 20 65 78 70 72  te a select expr
25a5b 65 73 73 69 6f 6e 20 69 66 20 74 68 65 72 65 0a  ession if there.
25a5c 20 20 2a 2a 20 69 73 20 61 20 6c 69 6d 69 74 2f    ** is a limit/
25a5d 6f 66 66 73 65 74 20 74 65 72 6d 20 74 6f 20 65  offset term to e
25a5e 6e 66 6f 72 63 65 2e 0a 20 20 2a 2f 0a 20 20 69  nforce..  */.  i
25a5f 66 28 20 70 4c 69 6d 69 74 20 3d 3d 20 30 20 29  f( pLimit == 0 )
25a60 20 7b 0a 20 20 20 20 2f 2a 20 69 66 20 70 4c 69   {.    /* if pLi
25a61 6d 69 74 20 69 73 20 6e 75 6c 6c 2c 20 70 4f 66  mit is null, pOf
25a62 66 73 65 74 20 77 69 6c 6c 20 61 6c 77 61 79 73  fset will always
25a63 20 62 65 20 6e 75 6c 6c 20 61 73 20 77 65 6c 6c   be null as well
25a64 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
25a65 20 70 4f 66 66 73 65 74 20 3d 3d 20 30 20 29 3b   pOffset == 0 );
25a66 0a 20 20 20 20 72 65 74 75 72 6e 20 70 57 68 65  .    return pWhe
25a67 72 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65  re;.  }..  /* Ge
25a68 6e 65 72 61 74 65 20 61 20 73 65 6c 65 63 74 20  nerate a select 
25a69 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
25a6a 74 6f 20 65 6e 66 6f 72 63 65 20 74 68 65 20 6c  to enforce the l
25a6b 69 6d 69 74 2f 6f 66 66 73 65 74 20 0a 20 20 2a  imit/offset .  *
25a6c 2a 20 74 65 72 6d 20 66 6f 72 20 74 68 65 20 44  * term for the D
25a6d 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20  ELETE or UPDATE 
25a6e 73 74 61 74 65 6d 65 6e 74 2e 20 20 46 6f 72 20  statement.  For 
25a6f 65 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 20 20 20  example:.  **   
25a70 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c  DELETE FROM tabl
25a71 65 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31  e_a WHERE col1=1
25a72 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c   ORDER BY col2 L
25a73 49 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a  IMIT 1 OFFSET 1.
25a74 20 20 2a 2a 20 62 65 63 6f 6d 65 73 3a 0a 20 20    ** becomes:.  
25a75 2a 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d  **   DELETE FROM
25a76 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20 72   table_a WHERE r
25a77 6f 77 69 64 20 49 4e 20 28 20 0a 20 20 2a 2a 20  owid IN ( .  ** 
25a78 20 20 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64      SELECT rowid
25a79 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48   FROM table_a WH
25a7a 45 52 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52  ERE col1=1 ORDER
25a7b 20 42 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31   BY col2 LIMIT 1
25a7c 20 4f 46 46 53 45 54 20 31 0a 20 20 2a 2a 20 20   OFFSET 1.  **  
25a7d 20 29 3b 0a 20 20 2a 2f 0a 0a 20 20 70 53 65 6c   );.  */..  pSel
25a7e 65 63 74 52 6f 77 69 64 20 3d 20 73 71 6c 69 74  ectRowid = sqlit
25a7f 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
25a80 54 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29  TK_ROW, 0, 0, 0)
25a81 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 52  ;.  if( pSelectR
25a82 6f 77 69 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f  owid == 0 ) goto
25a83 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65   limit_where_cle
25a84 61 6e 75 70 5f 32 3b 0a 20 20 70 45 4c 69 73 74  anup_2;.  pEList
25a85 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
25a86 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
25a87 20 30 2c 20 70 53 65 6c 65 63 74 52 6f 77 69 64   0, pSelectRowid
25a88 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20  );.  if( pEList 
25a89 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69  == 0 ) goto limi
25a8a 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
25a8b 32 3b 0a 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61  2;..  /* duplica
25a8c 74 65 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  te the FROM clau
25a8d 73 65 20 61 73 20 69 74 20 69 73 20 6e 65 65 64  se as it is need
25a8e 65 64 20 62 79 20 62 6f 74 68 20 74 68 65 20 44  ed by both the D
25a8f 45 4c 45 54 45 2f 55 50 44 41 54 45 20 74 72 65  ELETE/UPDATE tre
25a90 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53  e.  ** and the S
25a91 45 4c 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a  ELECT subtree. *
25a92 2f 0a 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d  /.  pSelectSrc =
25a93 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
25a94 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  up(pParse->db, p
25a95 53 72 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  Src, 0);.  if( p
25a96 53 65 6c 65 63 74 53 72 63 20 3d 3d 20 30 20 29  SelectSrc == 0 )
25a97 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78   {.    sqlite3Ex
25a98 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  prListDelete(pPa
25a99 72 73 65 2d 3e 64 62 2c 20 70 45 4c 69 73 74 29  rse->db, pEList)
25a9a 3b 0a 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74  ;.    goto limit
25a9b 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32  _where_cleanup_2
25a9c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65  ;.  }..  /* gene
25a9d 72 61 74 65 20 74 68 65 20 53 45 4c 45 43 54 20  rate the SELECT 
25a9e 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
25a9f 20 2a 2f 0a 20 20 70 53 65 6c 65 63 74 20 3d 20   */.  pSelect = 
25aa0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
25aa1 28 70 50 61 72 73 65 2c 70 45 4c 69 73 74 2c 70  (pParse,pEList,p
25aa2 53 65 6c 65 63 74 53 72 63 2c 70 57 68 65 72 65  SelectSrc,pWhere
25aa3 2c 30 2c 30 2c 0a 20 20 20 20 20 20 20 20 20 20  ,0,0,.          
25aa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25aa5 20 20 20 70 4f 72 64 65 72 42 79 2c 30 2c 70 4c     pOrderBy,0,pL
25aa6 69 6d 69 74 2c 70 4f 66 66 73 65 74 29 3b 0a 20  imit,pOffset);. 
25aa7 20 69 66 28 20 70 53 65 6c 65 63 74 20 3d 3d 20   if( pSelect == 
25aa8 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  0 ) return 0;.. 
25aa9 20 2f 2a 20 6e 6f 77 20 67 65 6e 65 72 61 74 65   /* now generate
25aaa 20 74 68 65 20 6e 65 77 20 57 48 45 52 45 20 72   the new WHERE r
25aab 6f 77 69 64 20 49 4e 20 63 6c 61 75 73 65 20 66  owid IN clause f
25aac 6f 72 20 74 68 65 20 44 45 4c 45 54 45 2f 55 44  or the DELETE/UD
25aad 50 41 54 45 20 2a 2f 0a 20 20 70 57 68 65 72 65  PATE */.  pWhere
25aae 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 50  Rowid = sqlite3P
25aaf 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
25ab0 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ROW, 0, 0, 0);. 
25ab1 20 69 66 28 20 70 57 68 65 72 65 52 6f 77 69 64   if( pWhereRowid
25ab2 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d   == 0 ) goto lim
25ab3 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
25ab4 5f 31 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 20  _1;.  pInClause 
25ab5 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
25ab6 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 57  Parse, TK_IN, pW
25ab7 68 65 72 65 52 6f 77 69 64 2c 20 30 2c 20 30 29  hereRowid, 0, 0)
25ab8 3b 0a 20 20 69 66 28 20 70 49 6e 43 6c 61 75 73  ;.  if( pInClaus
25ab9 65 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69  e == 0 ) goto li
25aba 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75  mit_where_cleanu
25abb 70 5f 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75 73  p_1;..  pInClaus
25abc 65 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 70  e->x.pSelect = p
25abd 53 65 6c 65 63 74 3b 0a 20 20 70 49 6e 43 6c 61  Select;.  pInCla
25abe 75 73 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  use->flags |= EP
25abf 5f 78 49 73 53 65 6c 65 63 74 3b 0a 20 20 73 71  _xIsSelect;.  sq
25ac0 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
25ac1 68 74 28 70 50 61 72 73 65 2c 20 70 49 6e 43 6c  ht(pParse, pInCl
25ac2 61 75 73 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  ause);.  return 
25ac3 70 49 6e 43 6c 61 75 73 65 3b 0a 0a 20 20 2f 2a  pInClause;..  /*
25ac4 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20   something went 
25ac5 77 72 6f 6e 67 2e 20 63 6c 65 61 6e 20 75 70 20  wrong. clean up 
25ac6 61 6e 79 74 68 69 6e 67 20 61 6c 6c 6f 63 61 74  anything allocat
25ac7 65 64 2e 20 2a 2f 0a 6c 69 6d 69 74 5f 77 68 65  ed. */.limit_whe
25ac8 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3a 0a 20 20  re_cleanup_1:.  
25ac9 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
25aca 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
25acb 70 53 65 6c 65 63 74 29 3b 0a 20 20 72 65 74 75  pSelect);.  retu
25acc 72 6e 20 30 3b 0a 0a 6c 69 6d 69 74 5f 77 68 65  rn 0;..limit_whe
25acd 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3a 0a 20 20  re_cleanup_2:.  
25ace 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
25acf 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 57  e(pParse->db, pW
25ad0 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  here);.  sqlite3
25ad1 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70  ExprListDelete(p
25ad2 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65  Parse->db, pOrde
25ad3 72 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  rBy);.  sqlite3E
25ad4 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65  xprDelete(pParse
25ad5 2d 3e 64 62 2c 20 70 4c 69 6d 69 74 29 3b 0a 20  ->db, pLimit);. 
25ad6 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
25ad7 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
25ad8 4f 66 66 73 65 74 29 3b 0a 20 20 72 65 74 75 72  Offset);.  retur
25ad9 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  n 0;.}.#endif /*
25ada 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
25adb 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45  ENABLE_UPDATE_DE
25adc 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21  LETE_LIMIT) && !
25add 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
25ade 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f  MIT_SUBQUERY) */
25adf 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
25ae0 20 63 6f 64 65 20 66 6f 72 20 61 20 44 45 4c 45   code for a DELE
25ae1 54 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e  TE FROM statemen
25ae2 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c  t..**.**     DEL
25ae3 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77  ETE FROM table_w
25ae4 78 79 7a 20 57 48 45 52 45 20 61 3c 35 20 41 4e  xyz WHERE a<5 AN
25ae5 44 20 62 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a  D b NOT NULL;.**
25ae6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ae7 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20   \________/     
25ae8 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
25ae9 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20  ___/.**         
25aea 20 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73           pTabLis
25aeb 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  t              p
25aec 57 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Where.*/.SQLITE_
25aed 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
25aee 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 0a  ite3DeleteFrom(.
25aef 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
25af0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
25af1 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
25af2 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
25af3 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68  bList,     /* Th
25af4 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69  e table from whi
25af5 63 68 20 77 65 20 73 68 6f 75 6c 64 20 64 65 6c  ch we should del
25af6 65 74 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20  ete things */.  
25af7 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20  Expr *pWhere    
25af8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
25af9 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  ERE clause.  May
25afa 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20   be null */.){. 
25afb 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
25afc 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
25afd 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20  irtual database 
25afe 65 6e 67 69 6e 65 20 2a 2f 0a 20 20 54 61 62 6c  engine */.  Tabl
25aff 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
25b00 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
25b01 66 72 6f 6d 20 77 68 69 63 68 20 72 65 63 6f 72  from which recor
25b02 64 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74  ds will be delet
25b03 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
25b04 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 2f  ar *zDb;       /
25b05 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
25b06 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20  se holding pTab 
25b07 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 2c 20 61 64  */.  int end, ad
25b08 64 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41  dr = 0;     /* A
25b09 20 63 6f 75 70 6c 65 20 61 64 64 72 65 73 73 65   couple addresse
25b0a 73 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63  s of generated c
25b0b 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  ode */.  int i; 
25b0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b0d 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
25b0e 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
25b0f 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49  pWInfo;     /* I
25b10 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
25b11 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
25b12 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  e */.  Index *pI
25b13 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  dx;           /*
25b14 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
25b15 72 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 65  r indices of the
25b16 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
25b17 69 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20  iCur;           
25b18 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f     /* VDBE Curso
25b19 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61  r number for pTa
25b1a 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  b */.  sqlite3 *
25b1b 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  db;           /*
25b1c 20 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73   Main database s
25b1d 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 41 75  tructure */.  Au
25b1e 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65  thContext sConte
25b1f 78 74 3b 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a  xt;  /* Authoriz
25b20 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f  ation context */
25b21 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73  .  NameContext s
25b22 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  NC;       /* Nam
25b23 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73  e context to res
25b24 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73  olve expressions
25b25 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62   in */.  int iDb
25b26 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
25b27 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62  /* Database numb
25b28 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 43  er */.  int memC
25b29 6e 74 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 2f  nt = -1;       /
25b2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73  * Memory cell us
25b2b 65 64 20 66 6f 72 20 63 68 61 6e 67 65 20 63 6f  ed for change co
25b2c 75 6e 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  unting */.  int 
25b2d 72 63 61 75 74 68 3b 20 20 20 20 20 20 20 20 20  rcauth;         
25b2e 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75     /* Value retu
25b2f 72 6e 65 64 20 62 79 20 61 75 74 68 6f 72 69 7a  rned by authoriz
25b30 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a  ation callback *
25b31 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
25b32 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
25b33 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20   int isView;    
25b34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25b35 20 54 72 75 65 20 69 66 20 61 74 74 65 6d 70 74   True if attempt
25b36 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72  ing to delete fr
25b37 6f 6d 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 54  om a view */.  T
25b38 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
25b39 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ;           /* L
25b3a 69 73 74 20 6f 66 20 74 61 62 6c 65 20 74 72 69  ist of table tri
25b3b 67 67 65 72 73 2c 20 69 66 20 72 65 71 75 69 72  ggers, if requir
25b3c 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20  ed */.#endif..  
25b3d 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 78 74  memset(&sContext
25b3e 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e  , 0, sizeof(sCon
25b3f 74 65 78 74 29 29 3b 0a 20 20 64 62 20 3d 20 70  text));.  db = p
25b40 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
25b41 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
25b42 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
25b43 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65  d ){.    goto de
25b44 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75  lete_from_cleanu
25b45 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  p;.  }.  assert(
25b46 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d   pTabList->nSrc=
25b47 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61  =1 );..  /* Loca
25b48 74 65 20 74 68 65 20 74 61 62 6c 65 20 77 68 69  te the table whi
25b49 63 68 20 77 65 20 77 61 6e 74 20 74 6f 20 64 65  ch we want to de
25b4a 6c 65 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c  lete.  This tabl
25b4b 65 20 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a  e has to be.  **
25b4c 20 70 75 74 20 69 6e 20 61 6e 20 53 72 63 4c 69   put in an SrcLi
25b4d 73 74 20 73 74 72 75 63 74 75 72 65 20 62 65 63  st structure bec
25b4e 61 75 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65  ause some of the
25b4f 20 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 0a   subroutines we.
25b50 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c    ** will be cal
25b51 6c 69 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65  ling are designe
25b52 64 20 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d  d to work with m
25b53 75 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61  ultiple tables a
25b54 6e 64 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61  nd expect.  ** a
25b55 6e 20 53 72 63 4c 69 73 74 2a 20 70 61 72 61 6d  n SrcList* param
25b56 65 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20  eter instead of 
25b57 6a 75 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61  just a Table* pa
25b58 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20  rameter..  */.  
25b59 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72  pTab = sqlite3Sr
25b5a 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72  cListLookup(pPar
25b5b 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20  se, pTabList);. 
25b5c 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20   if( pTab==0 )  
25b5d 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d  goto delete_from
25b5e 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20  _cleanup;..  /* 
25b5f 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65  Figure out if we
25b60 20 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65   have any trigge
25b61 72 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61  rs and if the ta
25b62 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64  ble being.  ** d
25b63 65 6c 65 74 65 64 20 66 72 6f 6d 20 69 73 20 61  eleted from is a
25b64 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64   view.  */.#ifnd
25b65 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
25b66 52 49 47 47 45 52 0a 20 20 70 54 72 69 67 67 65  RIGGER.  pTrigge
25b67 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67  r = sqlite3Trigg
25b68 65 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c  ersExist(pParse,
25b69 20 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45   pTab, TK_DELETE
25b6a 2c 20 30 2c 20 30 29 3b 0a 20 20 69 73 56 69 65  , 0, 0);.  isVie
25b6b 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  w = pTab->pSelec
25b6c 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65  t!=0;.#else.# de
25b6d 66 69 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a  fine pTrigger 0.
25b6e 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
25b6f 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  0.#endif.#ifdef 
25b70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
25b71 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a  .# undef isView.
25b72 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
25b73 30 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  0.#endif..  /* I
25b74 66 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79  f pTab is really
25b75 20 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75   a view, make su
25b76 72 65 20 69 74 20 68 61 73 20 62 65 65 6e 20 69  re it has been i
25b77 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f  nitialized..  */
25b78 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69  .  if( sqlite3Vi
25b79 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
25b7a 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29  (pParse, pTab) )
25b7b 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74  {.    goto delet
25b7c 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
25b7d 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74    }..  if( sqlit
25b7e 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61  e3IsReadOnly(pPa
25b7f 72 73 65 2c 20 70 54 61 62 2c 20 28 70 54 72 69  rse, pTab, (pTri
25b80 67 67 65 72 3f 31 3a 30 29 29 20 29 7b 0a 20 20  gger?1:0)) ){.  
25b81 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72    goto delete_fr
25b82 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  om_cleanup;.  }.
25b83 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
25b84 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
25b85 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
25b86 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64  .  assert( iDb<d
25b87 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 62 20  b->nDb );.  zDb 
25b88 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
25b89 4e 61 6d 65 3b 0a 20 20 72 63 61 75 74 68 20 3d  Name;.  rcauth =
25b8a 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
25b8b 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
25b8c 5f 44 45 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a  _DELETE, pTab->z
25b8d 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 3b 0a 20  Name, 0, zDb);. 
25b8e 20 61 73 73 65 72 74 28 20 72 63 61 75 74 68 3d   assert( rcauth=
25b8f 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63  =SQLITE_OK || rc
25b90 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e  auth==SQLITE_DEN
25b91 59 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c  Y || rcauth==SQL
25b92 49 54 45 5f 49 47 4e 4f 52 45 20 29 3b 0a 20 20  ITE_IGNORE );.  
25b93 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49  if( rcauth==SQLI
25b94 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 67  TE_DENY ){.    g
25b95 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
25b96 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
25b97 73 73 65 72 74 28 21 69 73 56 69 65 77 20 7c 7c  ssert(!isView ||
25b98 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 20 20 2f   pTrigger);..  /
25b99 2a 20 41 73 73 69 67 6e 20 20 63 75 72 73 6f 72  * Assign  cursor
25b9a 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 74   number to the t
25b9b 61 62 6c 65 20 61 6e 64 20 61 6c 6c 20 69 74 73  able and all its
25b9c 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20   indices..  */. 
25b9d 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73   assert( pTabLis
25b9e 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  t->nSrc==1 );.  
25b9f 69 43 75 72 20 3d 20 70 54 61 62 4c 69 73 74 2d  iCur = pTabList-
25ba0 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20  >a[0].iCursor = 
25ba1 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
25ba2 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
25ba3 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
25ba4 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
25ba5 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  {.    pParse->nT
25ba6 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ab++;.  }..  /* 
25ba7 53 74 61 72 74 20 74 68 65 20 76 69 65 77 20 63  Start the view c
25ba8 6f 6e 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69 66  ontext.  */.  if
25ba9 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
25baa 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
25bab 78 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 26  xtPush(pParse, &
25bac 73 43 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e  sContext, pTab->
25bad 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  zName);.  }..  /
25bae 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69  * Begin generati
25baf 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  ng code..  */.  
25bb0 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
25bb1 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
25bb2 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f  ( v==0 ){.    go
25bb3 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  to delete_from_c
25bb4 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
25bb5 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
25bb6 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62  ==0 ) sqlite3Vdb
25bb7 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29  eCountChanges(v)
25bb8 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ;.  sqlite3Begin
25bb9 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
25bba 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a  Parse, 1, iDb);.
25bbb 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
25bbc 74 72 79 69 6e 67 20 74 6f 20 64 65 6c 65 74 65  trying to delete
25bbd 20 66 72 6f 6d 20 61 20 76 69 65 77 2c 20 72 65   from a view, re
25bbe 61 6c 69 7a 65 20 74 68 61 74 20 76 69 65 77 20  alize that view 
25bbf 69 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65  into.  ** a ephe
25bc0 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a  meral table..  *
25bc1 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
25bc2 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
25bc3 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
25bc4 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
25bc5 29 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29  ).  if( isView )
25bc6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 74  {.    sqlite3Mat
25bc7 65 72 69 61 6c 69 7a 65 56 69 65 77 28 70 50 61  erializeView(pPa
25bc8 72 73 65 2c 20 70 54 61 62 2c 20 70 57 68 65 72  rse, pTab, pWher
25bc9 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65  e, iCur);.  }.#e
25bca 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  ndif..  /* Resol
25bcb 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  ve the column na
25bcc 6d 65 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  mes in the WHERE
25bcd 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20   clause..  */.  
25bce 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
25bcf 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
25bd0 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61  sNC.pParse = pPa
25bd1 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c  rse;.  sNC.pSrcL
25bd2 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a  ist = pTabList;.
25bd3 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
25bd4 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
25bd5 4e 43 2c 20 70 57 68 65 72 65 29 20 29 7b 0a 20  NC, pWhere) ){. 
25bd6 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66     goto delete_f
25bd7 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  rom_cleanup;.  }
25bd8 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
25bd9 65 20 74 68 65 20 63 6f 75 6e 74 65 72 20 6f 66  e the counter of
25bda 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
25bdb 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 69 66 0a  ows deleted, if.
25bdc 20 20 2a 2a 20 77 65 20 61 72 65 20 63 6f 75 6e    ** we are coun
25bdd 74 69 6e 67 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a  ting rows..  */.
25bde 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
25bdf 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
25be0 77 73 20 29 7b 0a 20 20 20 20 6d 65 6d 43 6e 74  ws ){.    memCnt
25be1 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
25be2 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  m;.    sqlite3Vd
25be3 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
25be4 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65 6d 43 6e  nteger, 0, memCn
25be5 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  t);.  }..#ifndef
25be6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55   SQLITE_OMIT_TRU
25be7 4e 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49  NCATE_OPTIMIZATI
25be8 4f 4e 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20  ON.  /* Special 
25be9 63 61 73 65 3a 20 41 20 44 45 4c 45 54 45 20 77  case: A DELETE w
25bea 69 74 68 6f 75 74 20 61 20 57 48 45 52 45 20 63  ithout a WHERE c
25beb 6c 61 75 73 65 20 64 65 6c 65 74 65 73 20 65 76  lause deletes ev
25bec 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a 2a 20 49  erything..  ** I
25bed 74 20 69 73 20 65 61 73 69 65 72 20 6a 75 73 74  t is easier just
25bee 20 74 6f 20 65 72 61 73 65 20 74 68 65 20 77 68   to erase the wh
25bef 6f 6c 65 20 74 61 62 6c 65 2e 20 50 72 69 6f 72  ole table. Prior
25bf0 20 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e   to version 3.6.
25bf1 35 2c 0a 20 20 2a 2a 20 74 68 69 73 20 6f 70 74  5,.  ** this opt
25bf2 69 6d 69 7a 61 74 69 6f 6e 20 63 61 75 73 65 64  imization caused
25bf3 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20   the row change 
25bf4 63 6f 75 6e 74 20 28 74 68 65 20 76 61 6c 75 65  count (the value
25bf5 20 72 65 74 75 72 6e 65 64 20 62 79 20 0a 20 20   returned by .  
25bf6 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20  ** API function 
25bf7 73 71 6c 69 74 65 33 5f 63 6f 75 6e 74 5f 63 68  sqlite3_count_ch
25bf8 61 6e 67 65 73 29 20 74 6f 20 62 65 20 73 65 74  anges) to be set
25bf9 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 20 20 2a   incorrectly.  *
25bfa 2f 0a 20 20 69 66 28 20 72 63 61 75 74 68 3d 3d  /.  if( rcauth==
25bfb 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 57 68  SQLITE_OK && pWh
25bfc 65 72 65 3d 3d 30 20 26 26 20 21 70 54 72 69 67  ere==0 && !pTrig
25bfd 67 65 72 20 26 26 20 21 49 73 56 69 72 74 75 61  ger && !IsVirtua
25bfe 6c 28 70 54 61 62 29 20 0a 20 20 20 26 26 20 30  l(pTab) .   && 0
25bff 3d 3d 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69  ==sqlite3FkRequi
25c00 72 65 64 28 70 50 61 72 73 65 2c 20 70 54 61 62  red(pParse, pTab
25c01 2c 20 30 2c 20 30 29 0a 20 20 29 7b 0a 20 20 20  , 0, 0).  ){.   
25c02 20 61 73 73 65 72 74 28 20 21 69 73 56 69 65 77   assert( !isView
25c03 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
25c04 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
25c05 43 6c 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e 75  Clear, pTab->tnu
25c06 6d 2c 20 69 44 62 2c 20 6d 65 6d 43 6e 74 2c 0a  m, iDb, memCnt,.
25c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c08 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
25c09 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
25c0a 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62     for(pIdx=pTab
25c0b 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
25c0c 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
25c0d 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
25c0e 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d   pIdx->pSchema==
25c0f 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
25c10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
25c11 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
25c12 6c 65 61 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d  lear, pIdx->tnum
25c13 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20  , iDb);.    }.  
25c14 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
25c15 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e  SQLITE_OMIT_TRUN
25c16 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  CATE_OPTIMIZATIO
25c17 4e 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 75 73  N */.  /* The us
25c18 75 61 6c 20 63 61 73 65 3a 20 54 68 65 72 65 20  ual case: There 
25c19 69 73 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  is a WHERE claus
25c1a 65 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20  e so we have to 
25c1b 73 63 61 6e 20 74 68 72 6f 75 67 68 0a 20 20 2a  scan through.  *
25c1c 2a 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  * the table and 
25c1d 70 69 63 6b 20 77 68 69 63 68 20 72 65 63 6f 72  pick which recor
25c1e 64 73 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20  ds to delete..  
25c1f 2a 2f 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69  */.  {.    int i
25c20 52 6f 77 53 65 74 20 3d 20 2b 2b 70 50 61 72 73  RowSet = ++pPars
25c21 65 2d 3e 6e 4d 65 6d 3b 20 20 20 2f 2a 20 52 65  e->nMem;   /* Re
25c22 67 69 73 74 65 72 20 66 6f 72 20 72 6f 77 73 65  gister for rowse
25c23 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 64 65 6c  t of rows to del
25c24 65 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ete */.    int i
25c25 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65  Rowid = ++pParse
25c26 2d 3e 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 55 73  ->nMem;    /* Us
25c27 65 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 72  ed for storing r
25c28 6f 77 69 64 20 76 61 6c 75 65 73 2e 20 2a 2f 0a  owid values. */.
25c29 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64      int regRowid
25c2a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
25c2b 20 20 20 20 2f 2a 20 41 63 74 75 61 6c 20 72 65      /* Actual re
25c2c 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e  gister containin
25c2d 67 20 72 6f 77 69 64 73 20 2a 2f 0a 0a 20 20 20  g rowids */..   
25c2e 20 2f 2a 20 43 6f 6c 6c 65 63 74 20 72 6f 77 69   /* Collect rowi
25c2f 64 73 20 6f 66 20 65 76 65 72 79 20 72 6f 77 20  ds of every row 
25c30 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 20  to be deleted.. 
25c31 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
25c32 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25c33 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 6f 77 53  P_Null, 0, iRowS
25c34 65 74 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f 20  et);.    pWInfo 
25c35 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
25c36 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  gin(pParse, pTab
25c37 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 30 2c 57  List, pWhere,0,W
25c38 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f  HERE_DUPLICATES_
25c39 4f 4b 29 3b 0a 20 20 20 20 69 66 28 20 70 57 49  OK);.    if( pWI
25c3a 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64 65  nfo==0 ) goto de
25c3b 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75  lete_from_cleanu
25c3c 70 3b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20  p;.    regRowid 
25c3d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
25c3e 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73  eGetColumn(pPars
25c3f 65 2c 20 70 54 61 62 2c 20 2d 31 2c 20 69 43 75  e, pTab, -1, iCu
25c40 72 2c 20 69 52 6f 77 69 64 2c 20 30 29 3b 0a 20  r, iRowid, 0);. 
25c41 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25c42 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65  dOp2(v, OP_RowSe
25c43 74 41 64 64 2c 20 69 52 6f 77 53 65 74 2c 20 72  tAdd, iRowSet, r
25c44 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66  egRowid);.    if
25c45 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
25c46 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29  LITE_CountRows )
25c47 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
25c48 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25c49 41 64 64 49 6d 6d 2c 20 6d 65 6d 43 6e 74 2c 20  AddImm, memCnt, 
25c4a 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  1);.    }.    sq
25c4b 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
25c4c 49 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  Info);..    /* D
25c4d 65 6c 65 74 65 20 65 76 65 72 79 20 69 74 65 6d  elete every item
25c4e 20 77 68 6f 73 65 20 6b 65 79 20 77 61 73 20 77   whose key was w
25c4f 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c 69  ritten to the li
25c50 73 74 20 64 75 72 69 6e 67 20 74 68 65 0a 20 20  st during the.  
25c51 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63    ** database sc
25c52 61 6e 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  an.  We have to 
25c53 64 65 6c 65 74 65 20 69 74 65 6d 73 20 61 66 74  delete items aft
25c54 65 72 20 74 68 65 20 73 63 61 6e 20 69 73 20 63  er the scan is c
25c55 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 62  omplete.    ** b
25c56 65 63 61 75 73 65 20 64 65 6c 65 74 69 6e 67 20  ecause deleting 
25c57 61 6e 20 69 74 65 6d 20 63 61 6e 20 63 68 61 6e  an item can chan
25c58 67 65 20 74 68 65 20 73 63 61 6e 20 6f 72 64 65  ge the scan orde
25c59 72 2e 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20 3d  r.  */.    end =
25c5a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
25c5b 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f  Label(v);..    /
25c5c 2a 20 55 6e 6c 65 73 73 20 74 68 69 73 20 69 73  * Unless this is
25c5d 20 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 63 75   a view, open cu
25c5e 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20 74 61  rsors for the ta
25c5f 62 6c 65 20 77 65 20 61 72 65 20 0a 20 20 20 20  ble we are .    
25c60 2a 2a 20 64 65 6c 65 74 69 6e 67 20 66 72 6f 6d  ** deleting from
25c61 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e 64   and all its ind
25c62 69 63 65 73 2e 20 49 66 20 74 68 69 73 20 69 73  ices. If this is
25c63 20 61 20 76 69 65 77 2c 20 74 68 65 6e 20 74 68   a view, then th
25c64 65 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 65 66  e.    ** only ef
25c65 66 65 63 74 20 74 68 69 73 20 73 74 61 74 65 6d  fect this statem
25c66 65 6e 74 20 68 61 73 20 69 73 20 74 6f 20 66 69  ent has is to fi
25c67 72 65 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f  re the INSTEAD O
25c68 46 20 0a 20 20 20 20 2a 2a 20 74 72 69 67 67 65  F .    ** trigge
25c69 72 73 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  rs.  */.    if( 
25c6a 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20  !isView ){.     
25c6b 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
25c6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 50 61 72  eAndIndices(pPar
25c6d 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20  se, pTab, iCur, 
25c6e 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20  OP_OpenWrite);. 
25c6f 20 20 20 7d 0a 0a 20 20 20 20 61 64 64 72 20 3d     }..    addr =
25c70 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25c71 70 33 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 52  p3(v, OP_RowSetR
25c72 65 61 64 2c 20 69 52 6f 77 53 65 74 2c 20 65 6e  ead, iRowSet, en
25c73 64 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20  d, iRowid);..   
25c74 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 72   /* Delete the r
25c75 6f 77 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ow */.#ifndef SQ
25c76 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
25c77 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49  LTABLE.    if( I
25c78 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
25c79 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
25c7a 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e  ar *pVTab = (con
25c7b 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
25c7c 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70  3GetVTable(db, p
25c7d 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Tab);.      sqli
25c7e 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61  te3VtabMakeWrita
25c7f 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
25c80 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
25c81 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
25c82 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 31 2c 20  _VUpdate, 0, 1, 
25c83 69 52 6f 77 69 64 2c 20 70 56 54 61 62 2c 20 50  iRowid, pVTab, P
25c84 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20 73  4_VTAB);.      s
25c85 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70  qlite3MayAbort(p
25c86 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 65 6c 73  Parse);.    }els
25c87 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20  e.#endif.    {. 
25c88 20 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 20 3d       int count =
25c89 20 28 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64   (pParse->nested
25c8a 3d 3d 30 29 3b 20 20 20 20 2f 2a 20 54 72 75 65  ==0);    /* True
25c8b 20 74 6f 20 63 6f 75 6e 74 20 63 68 61 6e 67 65   to count change
25c8c 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  s */.      sqlit
25c8d 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
25c8e 65 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ete(pParse, pTab
25c8f 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 2c 20  , iCur, iRowid, 
25c90 63 6f 75 6e 74 2c 20 70 54 72 69 67 67 65 72 2c  count, pTrigger,
25c91 20 4f 45 5f 44 65 66 61 75 6c 74 29 3b 0a 20 20   OE_Default);.  
25c92 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20    }..    /* End 
25c93 6f 66 20 74 68 65 20 64 65 6c 65 74 65 20 6c 6f  of the delete lo
25c94 6f 70 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  op */.    sqlite
25c95 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25c96 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29  P_Goto, 0, addr)
25c97 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25c98 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
25c99 20 65 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 43   end);..    /* C
25c9a 6c 6f 73 65 20 74 68 65 20 63 75 72 73 6f 72 73  lose the cursors
25c9b 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 61 62   open on the tab
25c9c 6c 65 20 61 6e 64 20 69 74 73 20 69 6e 64 65 78  le and its index
25c9d 65 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21  es. */.    if( !
25c9e 69 73 56 69 65 77 20 26 26 20 21 49 73 56 69 72  isView && !IsVir
25c9f 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
25ca0 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64      for(i=1, pId
25ca1 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
25ca2 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d  pIdx; i++, pIdx=
25ca3 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
25ca4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25ca5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
25ca6 6f 73 65 2c 20 69 43 75 72 20 2b 20 69 2c 20 70  ose, iCur + i, p
25ca7 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20  Idx->tnum);.    
25ca8 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
25ca9 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
25caa 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a  P_Close, iCur);.
25cab 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
25cac 55 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 74  Update the sqlit
25cad 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
25cae 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65 20   by storing the 
25caf 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a 20  content of the. 
25cb0 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69   ** maximum rowi
25cb1 64 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73  d counter values
25cb2 20 72 65 63 6f 72 64 65 64 20 77 68 69 6c 65 20   recorded while 
25cb3 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20  inserting into. 
25cb4 20 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e   ** autoincremen
25cb5 74 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20  t tables..  */. 
25cb6 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
25cb7 74 65 64 3d 3d 30 20 26 26 20 70 50 61 72 73 65  ted==0 && pParse
25cb8 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3d 3d 30  ->pTriggerTab==0
25cb9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   ){.    sqlite3A
25cba 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28  utoincrementEnd(
25cbb 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20  pParse);.  }..  
25cbc 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  /* Return the nu
25cbd 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61  mber of rows tha
25cbe 74 20 77 65 72 65 20 64 65 6c 65 74 65 64 2e 20  t were deleted. 
25cbf 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
25cc0 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74  is .  ** generat
25cc1 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65  ing code because
25cc2 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71   of a call to sq
25cc3 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
25cc4 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20  (), do not.  ** 
25cc5 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  invoke the callb
25cc6 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  ack function..  
25cc7 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c  */.  if( (db->fl
25cc8 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74  ags&SQLITE_Count
25cc9 52 6f 77 73 29 20 26 26 20 21 70 50 61 72 73 65  Rows) && !pParse
25cca 2d 3e 6e 65 73 74 65 64 20 26 26 20 21 70 50 61  ->nested && !pPa
25ccb 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
25ccc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
25ccd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25cce 52 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 43 6e  ResultRow, memCn
25ccf 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  t, 1);.    sqlit
25cd0 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
25cd1 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
25cd2 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
25cd3 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
25cd4 4e 41 4d 45 2c 20 22 72 6f 77 73 20 64 65 6c 65  NAME, "rows dele
25cd5 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ted", SQLITE_STA
25cd6 54 49 43 29 3b 0a 20 20 7d 0a 0a 64 65 6c 65 74  TIC);.  }..delet
25cd7 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a 0a  e_from_cleanup:.
25cd8 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e    sqlite3AuthCon
25cd9 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78  textPop(&sContex
25cda 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  t);.  sqlite3Src
25cdb 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
25cdc 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  TabList);.  sqli
25cdd 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
25cde 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74  , pWhere);.  ret
25cdf 75 72 6e 3b 0a 7d 0a 2f 2a 20 4d 61 6b 65 20 73  urn;.}./* Make s
25ce0 75 72 65 20 22 69 73 56 69 65 77 22 20 61 6e 64  ure "isView" and
25ce1 20 6f 74 68 65 72 20 6d 61 63 72 6f 73 20 64 65   other macros de
25ce2 66 69 6e 65 64 20 61 62 6f 76 65 20 61 72 65 20  fined above are 
25ce3 75 6e 64 65 66 69 6e 65 64 2e 20 4f 74 68 65 72  undefined. Other
25ce4 77 69 73 65 0a 2a 2a 20 74 68 65 6c 79 20 6d 61  wise.** thely ma
25ce5 79 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68  y interfere with
25ce6 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20   compilation of 
25ce7 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  other functions 
25ce8 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20  in this file.** 
25ce9 28 6f 72 20 69 6e 20 61 6e 6f 74 68 65 72 20 66  (or in another f
25cea 69 6c 65 2c 20 69 66 20 74 68 69 73 20 66 69 6c  ile, if this fil
25ceb 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f  e becomes part o
25cec 66 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69  f the amalgamati
25ced 6f 6e 29 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20  on).  */.#ifdef 
25cee 69 73 56 69 65 77 0a 20 23 75 6e 64 65 66 20 69  isView. #undef i
25cef 73 56 69 65 77 0a 23 65 6e 64 69 66 0a 23 69 66  sView.#endif.#if
25cf0 64 65 66 20 70 54 72 69 67 67 65 72 0a 20 23 75  def pTrigger. #u
25cf1 6e 64 65 66 20 70 54 72 69 67 67 65 72 0a 23 65  ndef pTrigger.#e
25cf2 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
25cf3 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
25cf4 65 73 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  es VDBE code tha
25cf5 74 20 63 61 75 73 65 73 20 61 20 73 69 6e 67 6c  t causes a singl
25cf6 65 20 72 6f 77 20 6f 66 20 61 0a 2a 2a 20 73 69  e row of a.** si
25cf7 6e 67 6c 65 20 74 61 62 6c 65 20 74 6f 20 62 65  ngle table to be
25cf8 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   deleted..**.** 
25cf9 54 68 65 20 56 44 42 45 20 6d 75 73 74 20 62 65  The VDBE must be
25cfa 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   in a particular
25cfb 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73   state when this
25cfc 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
25cfd 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72 65  ed..** These are
25cfe 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74   the requirement
25cff 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41  s:.**.**   1.  A
25d00 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
25d01 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70  or pointing to p
25d02 54 61 62 2c 20 74 68 65 20 74 61 62 6c 65 20 63  Tab, the table c
25d03 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f  ontaining the ro
25d04 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20 62 65  w.**       to be
25d05 20 64 65 6c 65 74 65 64 2c 20 6d 75 73 74 20 62   deleted, must b
25d06 65 20 6f 70 65 6e 65 64 20 61 73 20 63 75 72 73  e opened as curs
25d07 6f 72 20 6e 75 6d 62 65 72 20 24 69 43 75 72 2e  or number $iCur.
25d08 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61  .**.**   2.  Rea
25d09 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20  d/write cursors 
25d0a 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  for all indices 
25d0b 6f 66 20 70 54 61 62 20 6d 75 73 74 20 62 65 20  of pTab must be 
25d0c 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20  open as.**      
25d0d 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 62   cursor number b
25d0e 61 73 65 2b 69 20 66 6f 72 20 74 68 65 20 69 2d  ase+i for the i-
25d0f 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  th index..**.** 
25d10 20 20 33 2e 20 20 54 68 65 20 72 65 63 6f 72 64    3.  The record
25d11 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72   number of the r
25d12 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  ow to be deleted
25d13 20 6d 75 73 74 20 62 65 20 73 74 6f 72 65 64 20   must be stored 
25d14 69 6e 0a 2a 2a 20 20 20 20 20 20 20 6d 65 6d 6f  in.**       memo
25d15 72 79 20 63 65 6c 6c 20 69 52 6f 77 69 64 2e 0a  ry cell iRowid..
25d16 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
25d17 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64  ne generates cod
25d18 65 20 74 6f 20 72 65 6d 6f 76 65 20 62 6f 74 68  e to remove both
25d19 20 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72   the table recor
25d1a 64 20 61 6e 64 20 61 6c 6c 20 0a 2a 2a 20 69 6e  d and all .** in
25d1b 64 65 78 20 65 6e 74 72 69 65 73 20 74 68 61 74  dex entries that
25d1c 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 72   point to that r
25d1d 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ecord..*/.SQLITE
25d1e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
25d1f 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
25d20 44 65 6c 65 74 65 28 0a 20 20 50 61 72 73 65 20  Delete(.  Parse 
25d21 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
25d22 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
25d23 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
25d24 2c 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65  ,       /* Table
25d25 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
25d26 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65  row to be delete
25d27 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c  d */.  int iCur,
25d28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
25d29 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
25d2a 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
25d2b 74 20 69 52 6f 77 69 64 2c 20 20 20 20 20 20 20  t iRowid,       
25d2c 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
25d2d 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
25d2e 65 20 72 6f 77 69 64 20 74 6f 20 64 65 6c 65 74  e rowid to delet
25d2f 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74  e */.  int count
25d30 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  ,         /* If 
25d31 6e 6f 6e 2d 7a 65 72 6f 2c 20 69 6e 63 72 65 6d  non-zero, increm
25d32 65 6e 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e  ent the row chan
25d33 67 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  ge counter */.  
25d34 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
25d35 72 2c 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72  r, /* List of tr
25d36 69 67 67 65 72 73 20 74 6f 20 28 70 6f 74 65 6e  iggers to (poten
25d37 74 69 61 6c 6c 79 29 20 66 69 72 65 20 2a 2f 0a  tially) fire */.
25d38 20 20 69 6e 74 20 6f 6e 63 6f 6e 66 20 20 20 20    int onconf    
25d39 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
25d3a 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69  ON CONFLICT poli
25d3b 63 79 20 66 6f 72 20 74 72 69 67 67 65 72 73 20  cy for triggers 
25d3c 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  */.){.  Vdbe *v 
25d3d 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
25d3e 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20          /* Vdbe 
25d3f 2a 2f 0a 20 20 69 6e 74 20 69 4f 6c 64 20 3d 20  */.  int iOld = 
25d40 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
25d41 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65       /* First re
25d42 67 69 73 74 65 72 20 69 6e 20 4f 4c 44 2e 2a 20  gister in OLD.* 
25d43 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69  array */.  int i
25d44 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20  Label;          
25d45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
25d46 62 65 6c 20 72 65 73 6f 6c 76 65 64 20 74 6f 20  bel resolved to 
25d47 65 6e 64 20 6f 66 20 67 65 6e 65 72 61 74 65 64  end of generated
25d48 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 56   code */..  /* V
25d49 64 62 65 20 69 73 20 67 75 61 72 61 6e 74 65 65  dbe is guarantee
25d4a 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 61  d to have been a
25d4b 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
25d4c 20 73 74 61 67 65 2e 20 2a 2f 0a 20 20 61 73 73   stage. */.  ass
25d4d 65 72 74 28 20 76 20 29 3b 0a 0a 20 20 2f 2a 20  ert( v );..  /* 
25d4e 53 65 65 6b 20 63 75 72 73 6f 72 20 69 43 75 72  Seek cursor iCur
25d4f 20 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 64   to the row to d
25d50 65 6c 65 74 65 2e 20 49 66 20 74 68 69 73 20 72  elete. If this r
25d51 6f 77 20 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69  ow no longer exi
25d52 73 74 73 20 0a 20 20 2a 2a 20 28 74 68 69 73 20  sts .  ** (this 
25d53 63 61 6e 20 68 61 70 70 65 6e 20 69 66 20 61 20  can happen if a 
25d54 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20  trigger program 
25d55 68 61 73 20 61 6c 72 65 61 64 79 20 64 65 6c 65  has already dele
25d56 74 65 64 20 69 74 29 2c 20 64 6f 0a 20 20 2a 2a  ted it), do.  **
25d57 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
25d58 64 65 6c 65 74 65 20 69 74 20 6f 72 20 66 69 72  delete it or fir
25d59 65 20 61 6e 79 20 44 45 4c 45 54 45 20 74 72 69  e any DELETE tri
25d5a 67 67 65 72 73 2e 20 20 2a 2f 0a 20 20 69 4c 61  ggers.  */.  iLa
25d5b 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62  bel = sqlite3Vdb
25d5c 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
25d5d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25d5e 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
25d5f 74 73 2c 20 69 43 75 72 2c 20 69 4c 61 62 65 6c  ts, iCur, iLabel
25d60 2c 20 69 52 6f 77 69 64 29 3b 0a 20 0a 20 20 2f  , iRowid);. .  /
25d61 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61  * If there are a
25d62 6e 79 20 74 72 69 67 67 65 72 73 20 74 6f 20 66  ny triggers to f
25d63 69 72 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20  ire, allocate a 
25d64 72 61 6e 67 65 20 6f 66 20 72 65 67 69 73 74 65  range of registe
25d65 72 73 20 74 6f 0a 20 20 2a 2a 20 75 73 65 20 66  rs to.  ** use f
25d66 6f 72 20 74 68 65 20 6f 6c 64 2e 2a 20 72 65 66  or the old.* ref
25d67 65 72 65 6e 63 65 73 20 69 6e 20 74 68 65 20 74  erences in the t
25d68 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20 20 69  riggers.  */.  i
25d69 66 28 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75  f( sqlite3FkRequ
25d6a 69 72 65 64 28 70 50 61 72 73 65 2c 20 70 54 61  ired(pParse, pTa
25d6b 62 2c 20 30 2c 20 30 29 20 7c 7c 20 70 54 72 69  b, 0, 0) || pTri
25d6c 67 67 65 72 20 29 7b 0a 20 20 20 20 75 33 32 20  gger ){.    u32 
25d6d 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  mask;           
25d6e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
25d6f 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d  k of OLD.* colum
25d70 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 20  ns in use */.   
25d71 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
25d72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25d73 2a 20 49 74 65 72 61 74 6f 72 20 75 73 65 64 20  * Iterator used 
25d74 77 68 69 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67  while populating
25d75 20 4f 4c 44 2e 2a 20 2a 2f 0a 0a 20 20 20 20 2f   OLD.* */..    /
25d76 2a 20 54 4f 44 4f 3a 20 43 6f 75 6c 64 20 75 73  * TODO: Could us
25d77 65 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  e temporary regi
25d78 73 74 65 72 73 20 68 65 72 65 2e 20 41 6c 73 6f  sters here. Also
25d79 20 63 6f 75 6c 64 20 61 74 74 65 6d 70 74 20 74   could attempt t
25d7a 6f 0a 20 20 20 20 2a 2a 20 61 76 6f 69 64 20 63  o.    ** avoid c
25d7b 6f 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  opying the conte
25d7c 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 77 69 64  nts of the rowid
25d7d 20 72 65 67 69 73 74 65 72 2e 20 20 2a 2f 0a 20   register.  */. 
25d7e 20 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65     mask = sqlite
25d7f 33 54 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28  3TriggerOldmask(
25d80 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
25d81 2c 20 30 2c 20 70 54 61 62 2c 20 6f 6e 63 6f 6e  , 0, pTab, oncon
25d82 66 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20  f);.    mask |= 
25d83 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b  sqlite3FkOldmask
25d84 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
25d85 20 20 20 20 69 4f 6c 64 20 3d 20 70 50 61 72 73      iOld = pPars
25d86 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70  e->nMem+1;.    p
25d87 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 28  Parse->nMem += (
25d88 31 20 2b 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b  1 + pTab->nCol);
25d89 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74  ..    /* Populat
25d8a 65 20 74 68 65 20 4f 4c 44 2e 2a 20 70 73 65 75  e the OLD.* pseu
25d8b 64 6f 2d 74 61 62 6c 65 20 72 65 67 69 73 74 65  do-table registe
25d8c 72 20 61 72 72 61 79 2e 20 54 68 65 73 65 20 76  r array. These v
25d8d 61 6c 75 65 73 20 77 69 6c 6c 20 62 65 20 0a 20  alues will be . 
25d8e 20 20 20 2a 2a 20 75 73 65 64 20 62 79 20 61 6e     ** used by an
25d8f 79 20 42 45 46 4f 52 45 20 61 6e 64 20 41 46 54  y BEFORE and AFT
25d90 45 52 20 74 72 69 67 67 65 72 73 20 74 68 61 74  ER triggers that
25d91 20 65 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 20   exist.  */.    
25d92 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25d93 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 52  2(v, OP_Copy, iR
25d94 6f 77 69 64 2c 20 69 4f 6c 64 29 3b 0a 20 20 20  owid, iOld);.   
25d95 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f   for(iCol=0; iCo
25d96 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43  l<pTab->nCol; iC
25d97 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  ol++){.      if(
25d98 20 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66   mask==0xfffffff
25d99 66 20 7c 7c 20 6d 61 73 6b 26 28 31 3c 3c 69 43  f || mask&(1<<iC
25d9a 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ol) ){.        i
25d9b 6e 74 20 69 54 61 72 67 65 74 20 3d 20 69 4f 6c  nt iTarget = iOl
25d9c 64 20 2b 20 69 43 6f 6c 20 2b 20 31 3b 0a 20 20  d + iCol + 1;.  
25d9d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25d9e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
25d9f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 43 6f 6c  lumn, iCur, iCol
25da0 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 20 20  , iTarget);.    
25da1 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d      sqlite3Colum
25da2 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62  nDefault(v, pTab
25da3 2c 20 69 43 6f 6c 2c 20 69 54 61 72 67 65 74 29  , iCol, iTarget)
25da4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
25da5 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 42  .    /* Invoke B
25da6 45 46 4f 52 45 20 44 45 4c 45 54 45 20 74 72 69  EFORE DELETE tri
25da7 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 2a  gger programs. *
25da8 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  /.    sqlite3Cod
25da9 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72  eRowTrigger(pPar
25daa 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 0a 20  se, pTrigger, . 
25dab 20 20 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45         TK_DELETE
25dac 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46  , 0, TRIGGER_BEF
25dad 4f 52 45 2c 20 70 54 61 62 2c 20 69 4f 6c 64 2c  ORE, pTab, iOld,
25dae 20 6f 6e 63 6f 6e 66 2c 20 69 4c 61 62 65 6c 0a   onconf, iLabel.
25daf 20 20 20 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53      );..    /* S
25db0 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74  eek the cursor t
25db1 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20  o the row to be 
25db2 64 65 6c 65 74 65 64 20 61 67 61 69 6e 2e 20 49  deleted again. I
25db3 74 20 6d 61 79 20 62 65 20 74 68 61 74 0a 20 20  t may be that.  
25db4 20 20 2a 2a 20 74 68 65 20 42 45 46 4f 52 45 20    ** the BEFORE 
25db5 74 72 69 67 67 65 72 73 20 63 6f 64 65 64 20 61  triggers coded a
25db6 62 6f 76 65 20 68 61 76 65 20 61 6c 72 65 61 64  bove have alread
25db7 79 20 72 65 6d 6f 76 65 64 20 74 68 65 20 72 6f  y removed the ro
25db8 77 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64  w.    ** being d
25db9 65 6c 65 74 65 64 2e 20 44 6f 20 6e 6f 74 20 61  eleted. Do not a
25dba 74 74 65 6d 70 74 20 74 6f 20 64 65 6c 65 74 65  ttempt to delete
25dbb 20 74 68 65 20 72 6f 77 20 61 20 73 65 63 6f 6e   the row a secon
25dbc 64 20 74 69 6d 65 2c 20 61 6e 64 20 0a 20 20 20  d time, and .   
25dbd 20 2a 2a 20 64 6f 20 6e 6f 74 20 66 69 72 65 20   ** do not fire 
25dbe 41 46 54 45 52 20 74 72 69 67 67 65 72 73 2e 20  AFTER triggers. 
25dbf 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
25dc0 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
25dc1 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c  NotExists, iCur,
25dc2 20 69 4c 61 62 65 6c 2c 20 69 52 6f 77 69 64 29   iLabel, iRowid)
25dc3 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 46 4b 20  ;..    /* Do FK 
25dc4 70 72 6f 63 65 73 73 69 6e 67 2e 20 54 68 69 73  processing. This
25dc5 20 63 61 6c 6c 20 63 68 65 63 6b 73 20 74 68 61   call checks tha
25dc6 74 20 61 6e 79 20 46 4b 20 63 6f 6e 73 74 72 61  t any FK constra
25dc7 69 6e 74 73 20 74 68 61 74 0a 20 20 20 20 2a 2a  ints that.    **
25dc8 20 72 65 66 65 72 20 74 6f 20 74 68 69 73 20 74   refer to this t
25dc9 61 62 6c 65 20 28 69 2e 65 2e 20 63 6f 6e 73 74  able (i.e. const
25dca 72 61 69 6e 74 73 20 61 74 74 61 63 68 65 64 20  raints attached 
25dcb 74 6f 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29  to other tables)
25dcc 20 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74   .    ** are not
25dcd 20 76 69 6f 6c 61 74 65 64 20 62 79 20 64 65 6c   violated by del
25dce 65 74 69 6e 67 20 74 68 69 73 20 72 6f 77 2e 20  eting this row. 
25dcf 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 46   */.    sqlite3F
25dd0 6b 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 70  kCheck(pParse, p
25dd1 54 61 62 2c 20 69 4f 6c 64 2c 20 30 29 3b 0a 20  Tab, iOld, 0);. 
25dd2 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20   }..  /* Delete 
25dd3 74 68 65 20 69 6e 64 65 78 20 61 6e 64 20 74 61  the index and ta
25dd4 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 53 6b 69  ble entries. Ski
25dd5 70 20 74 68 69 73 20 73 74 65 70 20 69 66 20 70  p this step if p
25dd6 54 61 62 20 69 73 20 72 65 61 6c 6c 79 0a 20 20  Tab is really.  
25dd7 2a 2a 20 61 20 76 69 65 77 20 28 69 6e 20 77 68  ** a view (in wh
25dd8 69 63 68 20 63 61 73 65 20 74 68 65 20 6f 6e 6c  ich case the onl
25dd9 79 20 65 66 66 65 63 74 20 6f 66 20 74 68 65 20  y effect of the 
25dda 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
25ddb 20 69 73 20 74 6f 0a 20 20 2a 2a 20 66 69 72 65   is to.  ** fire
25ddc 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20   the INSTEAD OF 
25ddd 74 72 69 67 67 65 72 73 29 2e 20 20 2a 2f 20 0a  triggers).  */ .
25dde 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c    if( pTab->pSel
25ddf 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ect==0 ){.    sq
25de0 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
25de1 49 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72  IndexDelete(pPar
25de2 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20  se, pTab, iCur, 
25de3 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
25de4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25de5 44 65 6c 65 74 65 2c 20 69 43 75 72 2c 20 28 63  Delete, iCur, (c
25de6 6f 75 6e 74 3f 4f 50 46 4c 41 47 5f 4e 43 48 41  ount?OPFLAG_NCHA
25de7 4e 47 45 3a 30 29 29 3b 0a 20 20 20 20 69 66 28  NGE:0));.    if(
25de8 20 63 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20   count ){.      
25de9 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
25dea 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d  eP4(v, -1, pTab-
25deb 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49  >zName, P4_STATI
25dec 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  C);.    }.  }.. 
25ded 20 2f 2a 20 44 6f 20 61 6e 79 20 4f 4e 20 43 41   /* Do any ON CA
25dee 53 43 41 44 45 2c 20 53 45 54 20 4e 55 4c 4c 20  SCADE, SET NULL 
25def 6f 72 20 53 45 54 20 44 45 46 41 55 4c 54 20 6f  or SET DEFAULT o
25df0 70 65 72 61 74 69 6f 6e 73 20 72 65 71 75 69 72  perations requir
25df1 65 64 20 74 6f 0a 20 20 2a 2a 20 68 61 6e 64 6c  ed to.  ** handl
25df2 65 20 72 6f 77 73 20 28 70 6f 73 73 69 62 6c 79  e rows (possibly
25df3 20 69 6e 20 6f 74 68 65 72 20 74 61 62 6c 65 73   in other tables
25df4 29 20 74 68 61 74 20 72 65 66 65 72 20 76 69 61  ) that refer via
25df5 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 20   a foreign key. 
25df6 20 2a 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 6a   ** to the row j
25df7 75 73 74 20 64 65 6c 65 74 65 64 2e 20 2a 2f 20  ust deleted. */ 
25df8 0a 20 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69  .  sqlite3FkActi
25df9 6f 6e 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ons(pParse, pTab
25dfa 2c 20 30 2c 20 69 4f 6c 64 29 3b 0a 0a 20 20 2f  , 0, iOld);..  /
25dfb 2a 20 49 6e 76 6f 6b 65 20 41 46 54 45 52 20 44  * Invoke AFTER D
25dfc 45 4c 45 54 45 20 74 72 69 67 67 65 72 20 70 72  ELETE trigger pr
25dfd 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20 73 71 6c  ograms. */.  sql
25dfe 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
25dff 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67  er(pParse, pTrig
25e00 67 65 72 2c 20 0a 20 20 20 20 20 20 54 4b 5f 44  ger, .      TK_D
25e01 45 4c 45 54 45 2c 20 30 2c 20 54 52 49 47 47 45  ELETE, 0, TRIGGE
25e02 52 5f 41 46 54 45 52 2c 20 70 54 61 62 2c 20 69  R_AFTER, pTab, i
25e03 4f 6c 64 2c 20 6f 6e 63 6f 6e 66 2c 20 69 4c 61  Old, onconf, iLa
25e04 62 65 6c 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 4a  bel.  );..  /* J
25e05 75 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 20  ump here if the 
25e06 72 6f 77 20 68 61 64 20 61 6c 72 65 61 64 79 20  row had already 
25e07 62 65 65 6e 20 64 65 6c 65 74 65 64 20 62 65 66  been deleted bef
25e08 6f 72 65 20 61 6e 79 20 42 45 46 4f 52 45 0a 20  ore any BEFORE. 
25e09 20 2a 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67   ** trigger prog
25e0a 72 61 6d 73 20 77 65 72 65 20 69 6e 76 6f 6b 65  rams were invoke
25e0b 64 2e 20 4f 72 20 69 66 20 61 20 74 72 69 67 67  d. Or if a trigg
25e0c 65 72 20 70 72 6f 67 72 61 6d 20 74 68 72 6f 77  er program throw
25e0d 73 20 61 20 0a 20 20 2a 2a 20 52 41 49 53 45 28  s a .  ** RAISE(
25e0e 49 47 4e 4f 52 45 29 20 65 78 63 65 70 74 69 6f  IGNORE) exceptio
25e0f 6e 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  n.  */.  sqlite3
25e10 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
25e11 28 76 2c 20 69 4c 61 62 65 6c 29 3b 0a 7d 0a 0a  (v, iLabel);.}..
25e12 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
25e13 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 42  ne generates VDB
25e14 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75 73  E code that caus
25e15 65 73 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 20  es the deletion 
25e16 6f 66 20 61 6c 6c 0a 2a 2a 20 69 6e 64 65 78 20  of all.** index 
25e17 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74  entries associat
25e18 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65  ed with a single
25e19 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65   row of a single
25e1a 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
25e1b 65 20 56 44 42 45 20 6d 75 73 74 20 62 65 20 69  e VDBE must be i
25e1c 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  n a particular s
25e1d 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 72  tate when this r
25e1e 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
25e1f 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 74  ..** These are t
25e20 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 3a  he requirements:
25e21 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72  .**.**   1.  A r
25e22 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
25e23 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 61   pointing to pTa
25e24 62 2c 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e  b, the table con
25e25 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a  taining the row.
25e26 2a 2a 20 20 20 20 20 20 20 74 6f 20 62 65 20 64  **       to be d
25e27 65 6c 65 74 65 64 2c 20 6d 75 73 74 20 62 65 20  eleted, must be 
25e28 6f 70 65 6e 65 64 20 61 73 20 63 75 72 73 6f 72  opened as cursor
25e29 20 6e 75 6d 62 65 72 20 22 69 43 75 72 22 2e 0a   number "iCur"..
25e2a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 64  **.**   2.  Read
25e2b 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 66  /write cursors f
25e2c 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f  or all indices o
25e2d 66 20 70 54 61 62 20 6d 75 73 74 20 62 65 20 6f  f pTab must be o
25e2e 70 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 20  pen as.**       
25e2f 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43  cursor number iC
25e30 75 72 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74  ur+i for the i-t
25e31 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20  h index..**.**  
25e32 20 33 2e 20 20 54 68 65 20 22 69 43 75 72 22 20   3.  The "iCur" 
25e33 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70  cursor must be p
25e34 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72  ointing to the r
25e35 6f 77 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  ow that is to be
25e36 0a 2a 2a 20 20 20 20 20 20 20 64 65 6c 65 74 65  .**       delete
25e37 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
25e38 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25e39 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
25e3a 78 44 65 6c 65 74 65 28 0a 20 20 50 61 72 73 65  xDelete(.  Parse
25e3b 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
25e3c 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
25e3d 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
25e3e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
25e3f 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20  *pTab,       /* 
25e40 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  Table containing
25e41 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64   the row to be d
25e42 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  eleted */.  int 
25e43 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f  iCur,          /
25e44 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
25e45 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f  for the table */
25e46 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20  .  int *aRegIdx 
25e47 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 64 65        /* Only de
25e48 6c 65 74 65 20 69 66 20 61 52 65 67 49 64 78 21  lete if aRegIdx!
25e49 3d 30 20 26 26 20 61 52 65 67 49 64 78 5b 69 5d  =0 && aRegIdx[i]
25e4a 3e 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  >0 */.){.  int i
25e4b 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  ;.  Index *pIdx;
25e4c 0a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 66 6f  .  int r1;..  fo
25e4d 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62  r(i=1, pIdx=pTab
25e4e 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
25e4f 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e  i++, pIdx=pIdx->
25e50 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
25e51 61 52 65 67 49 64 78 21 3d 30 20 26 26 20 61 52  aRegIdx!=0 && aR
25e52 65 67 49 64 78 5b 69 2d 31 5d 3d 3d 30 20 29 20  egIdx[i-1]==0 ) 
25e53 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 31  continue;.    r1
25e54 20 3d 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61   = sqlite3Genera
25e55 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73  teIndexKey(pPars
25e56 65 2c 20 70 49 64 78 2c 20 69 43 75 72 2c 20 30  e, pIdx, iCur, 0
25e57 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
25e58 33 56 64 62 65 41 64 64 4f 70 33 28 70 50 61 72  3VdbeAddOp3(pPar
25e59 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 49 64  se->pVdbe, OP_Id
25e5a 78 44 65 6c 65 74 65 2c 20 69 43 75 72 2b 69 2c  xDelete, iCur+i,
25e5b 20 72 31 2c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   r1,pIdx->nColum
25e5c 6e 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  n+1);.  }.}../*.
25e5d 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
25e5e 20 74 68 61 74 20 77 69 6c 6c 20 61 73 73 65 6d   that will assem
25e5f 62 6c 65 20 61 6e 20 69 6e 64 65 78 20 6b 65 79  ble an index key
25e60 20 61 6e 64 20 70 75 74 20 69 74 20 69 6e 20 72   and put it in r
25e61 65 67 69 73 74 65 72 0a 2a 2a 20 72 65 67 4f 75  egister.** regOu
25e62 74 2e 20 20 54 68 65 20 6b 65 79 20 77 69 74 68  t.  The key with
25e63 20 62 65 20 66 6f 72 20 69 6e 64 65 78 20 70 49   be for index pI
25e64 64 78 20 77 68 69 63 68 20 69 73 20 61 6e 20 69  dx which is an i
25e65 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a  ndex on pTab..**
25e66 20 69 43 75 72 20 69 73 20 74 68 65 20 69 6e 64   iCur is the ind
25e67 65 78 20 6f 66 20 61 20 63 75 72 73 6f 72 20 6f  ex of a cursor o
25e68 70 65 6e 20 6f 6e 20 74 68 65 20 70 54 61 62 20  pen on the pTab 
25e69 74 61 62 6c 65 20 61 6e 64 20 70 6f 69 6e 74 69  table and pointi
25e6a 6e 67 20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e 74  ng to.** the ent
25e6b 72 79 20 74 68 61 74 20 6e 65 65 64 73 20 69 6e  ry that needs in
25e6c 64 65 78 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65  dexing..**.** Re
25e6d 74 75 72 6e 20 61 20 72 65 67 69 73 74 65 72 20  turn a register 
25e6e 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73 20  number which is 
25e6f 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 62  the first in a b
25e70 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 72 65 67 69 73  lock of.** regis
25e71 74 65 72 73 20 74 68 61 74 20 68 6f 6c 64 73 20  ters that holds 
25e72 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  the elements of 
25e73 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 20 20  the index key.  
25e74 54 68 65 0a 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20  The.** block of 
25e75 72 65 67 69 73 74 65 72 73 20 68 61 73 20 61 6c  registers has al
25e76 72 65 61 64 79 20 62 65 65 6e 20 64 65 61 6c 6c  ready been deall
25e77 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 74 69  ocated by the ti
25e78 6d 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  me.** this routi
25e79 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53  ne returns..*/.S
25e7a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
25e7b 74 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  t sqlite3Generat
25e7c 65 49 6e 64 65 78 4b 65 79 28 0a 20 20 50 61 72  eIndexKey(.  Par
25e7d 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
25e7e 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
25e7f 78 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70  xt */.  Index *p
25e80 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  Idx,       /* Th
25e81 65 20 69 6e 64 65 78 20 66 6f 72 20 77 68 69 63  e index for whic
25e82 68 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  h to generate a 
25e83 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75  key */.  int iCu
25e84 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  r,          /* C
25e85 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
25e86 20 74 68 65 20 70 49 64 78 2d 3e 70 54 61 62 6c   the pIdx->pTabl
25e87 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
25e88 20 72 65 67 4f 75 74 2c 20 20 20 20 20 20 20 20   regOut,        
25e89 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77  /* Write the new
25e8a 20 69 6e 64 65 78 20 6b 65 79 20 74 6f 20 74 68   index key to th
25e8b 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  is register */. 
25e8c 20 69 6e 74 20 64 6f 4d 61 6b 65 52 65 63 20 20   int doMakeRec  
25e8d 20 20 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 4f      /* Run the O
25e8e 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73  P_MakeRecord ins
25e8f 74 72 75 63 74 69 6f 6e 20 69 66 20 74 72 75 65  truction if true
25e90 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
25e91 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
25e92 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 54 61 62  ;.  int j;.  Tab
25e93 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d  le *pTab = pIdx-
25e94 3e 70 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 72  >pTable;.  int r
25e95 65 67 42 61 73 65 3b 0a 20 20 69 6e 74 20 6e 43  egBase;.  int nC
25e96 6f 6c 3b 0a 0a 20 20 6e 43 6f 6c 20 3d 20 70 49  ol;..  nCol = pI
25e97 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 72  dx->nColumn;.  r
25e98 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33  egBase = sqlite3
25e99 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
25e9a 72 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20  rse, nCol+1);.  
25e9b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25e9c 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
25e9d 43 75 72 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f  Cur, regBase+nCo
25e9e 6c 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  l);.  for(j=0; j
25e9f 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  <nCol; j++){.   
25ea0 20 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d   int idx = pIdx-
25ea1 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20  >aiColumn[j];.  
25ea2 20 20 69 66 28 20 69 64 78 3d 3d 70 54 61 62 2d    if( idx==pTab-
25ea3 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
25ea4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25ea5 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
25ea6 65 67 42 61 73 65 2b 6e 43 6f 6c 2c 20 72 65 67  egBase+nCol, reg
25ea7 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 7d 65 6c  Base+j);.    }el
25ea8 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
25ea9 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
25eaa 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20  P_Column, iCur, 
25eab 69 64 78 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b  idx, regBase+j);
25eac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f  .      sqlite3Co
25ead 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70  lumnDefault(v, p
25eae 54 61 62 2c 20 69 64 78 2c 20 2d 31 29 3b 0a 20  Tab, idx, -1);. 
25eaf 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64     }.  }.  if( d
25eb0 6f 4d 61 6b 65 52 65 63 20 29 7b 0a 20 20 20 20  oMakeRec ){.    
25eb1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25eb2 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
25eb3 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  rd, regBase, nCo
25eb4 6c 2b 31 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20  l+1, regOut);.  
25eb5 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
25eb6 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 73 71 6c  ngeP4(v, -1, sql
25eb7 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
25eb8 79 53 74 72 28 76 2c 20 70 49 64 78 29 2c 20 30  yStr(v, pIdx), 0
25eb9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
25eba 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
25ebb 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  hange(pParse, re
25ebc 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a  gBase, nCol+1);.
25ebd 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c    }.  sqlite3Rel
25ebe 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
25ebf 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e  arse, regBase, n
25ec0 43 6f 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e  Col+1);.  return
25ec1 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 2a   regBase;.}../**
25ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
25ec3 20 6f 66 20 64 65 6c 65 74 65 2e 63 20 2a 2a 2a   of delete.c ***
25ec4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ec5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ec6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
25ec7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
25ec8 69 6e 20 66 69 6c 65 20 66 75 6e 63 2e 63 20 2a  in file func.c *
25ec9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25eca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ecb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
25ecc 2a 2a 20 32 30 30 32 20 46 65 62 72 75 61 72 79  ** 2002 February
25ecd 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   23.**.** The au
25ece 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
25ecf 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
25ed0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
25ed1 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
25ed2 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
25ed3 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
25ed4 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
25ed5 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
25ed6 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
25ed7 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
25ed8 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
25ed9 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
25eda 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
25edb 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
25edc 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
25edd 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
25ede 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
25edf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ee1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ee2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ee3 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
25ee4 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
25ee5 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
25ee6 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20  plement various 
25ee7 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  SQL.** functions
25ee8 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a   of SQLite.  .**
25ee9 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c  .** There is onl
25eea 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73  y one exported s
25eeb 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69  ymbol in this fi
25eec 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f  le - the functio
25eed 6e 0a 2a 2a 20 73 71 6c 69 74 65 52 65 67 69 73  n.** sqliteRegis
25eee 74 65 72 42 75 69 6c 64 69 6e 46 75 6e 63 74 69  terBuildinFuncti
25eef 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74  ons() found at t
25ef0 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65  he bottom of the
25ef1 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74   file..** All ot
25ef2 68 65 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c  her code has fil
25ef3 65 20 73 63 6f 70 65 2e 0a 2a 2f 0a 0a 2f 2a 0a  e scope..*/../*.
25ef4 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f  ** Return the co
25ef5 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
25ef6 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
25ef7 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a   a function..*/.
25ef8 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a  static CollSeq *
25ef9 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f  sqlite3GetFuncCo
25efa 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 5f 63 6f  llSeq(sqlite3_co
25efb 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
25efc 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 65 78  .  return contex
25efd 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  t->pColl;.}../*.
25efe 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
25eff 6e 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61 67 67  n of the non-agg
25f00 72 65 67 61 74 65 20 6d 69 6e 28 29 20 61 6e 64  regate min() and
25f01 20 6d 61 78 28 29 20 66 75 6e 63 74 69 6f 6e 73   max() functions
25f02 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
25f03 6d 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20 73 71  minmaxFunc(.  sq
25f04 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
25f05 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
25f06 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
25f07 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
25f08 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d 61 73  int i;.  int mas
25f09 6b 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d  k;    /* 0 for m
25f0a 69 6e 28 29 20 6f 72 20 30 78 66 66 66 66 66 66  in() or 0xffffff
25f0b 66 66 20 66 6f 72 20 6d 61 78 28 29 20 2a 2f 0a  ff for max() */.
25f0c 20 20 69 6e 74 20 69 42 65 73 74 3b 0a 20 20 43    int iBest;.  C
25f0d 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a  ollSeq *pColl;..
25f0e 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3e 31    assert( argc>1
25f0f 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 73 71 6c   );.  mask = sql
25f10 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63  ite3_user_data(c
25f11 6f 6e 74 65 78 74 29 3d 3d 30 20 3f 20 30 20 3a  ontext)==0 ? 0 :
25f12 20 2d 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73   -1;.  pColl = s
25f13 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c  qlite3GetFuncCol
25f14 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20  lSeq(context);. 
25f15 20 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 20 29   assert( pColl )
25f16 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 61 73 6b  ;.  assert( mask
25f17 3d 3d 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20  ==-1 || mask==0 
25f18 29 3b 0a 20 20 69 42 65 73 74 20 3d 20 30 3b 0a  );.  iBest = 0;.
25f19 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
25f1a 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
25f1b 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )==SQLITE_NULL )
25f1c 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
25f1d 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29  =1; i<argc; i++)
25f1e 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
25f1f 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
25f20 76 5b 69 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[i])==SQLITE_NU
25f21 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  LL ) return;.   
25f22 20 69 66 28 20 28 73 71 6c 69 74 65 33 4d 65 6d   if( (sqlite3Mem
25f23 43 6f 6d 70 61 72 65 28 61 72 67 76 5b 69 42 65  Compare(argv[iBe
25f24 73 74 5d 2c 20 61 72 67 76 5b 69 5d 2c 20 70 43  st], argv[i], pC
25f25 6f 6c 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b  oll)^mask)>=0 ){
25f26 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
25f27 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 20 20   mask==0 );.    
25f28 20 20 69 42 65 73 74 20 3d 20 69 3b 0a 20 20 20    iBest = i;.   
25f29 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
25f2a 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f  _result_value(co
25f2b 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 42 65 73  ntext, argv[iBes
25f2c 74 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  t]);.}../*.** Re
25f2d 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 6f 66  turn the type of
25f2e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   the argument..*
25f2f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 79  /.static void ty
25f30 70 65 6f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69  peofFunc(.  sqli
25f31 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
25f32 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
25f33 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
25f34 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
25f35 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
25f36 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   0;.  UNUSED_PAR
25f37 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
25f38 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74  .  switch( sqlit
25f39 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
25f3a 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61  gv[0]) ){.    ca
25f3b 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
25f3c 52 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65 72 22  R: z = "integer"
25f3d 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73  ; break;.    cas
25f3e 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 20  e SQLITE_TEXT:  
25f3f 20 20 7a 20 3d 20 22 74 65 78 74 22 3b 20 20 20    z = "text";   
25f40 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
25f41 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20   SQLITE_FLOAT:  
25f42 20 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20 20 20   z = "real";    
25f43 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
25f44 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20  SQLITE_BLOB:    
25f45 7a 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20 20 62  z = "blob";    b
25f46 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c  reak;.    defaul
25f47 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 7a  t:             z
25f48 20 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72   = "null";    br
25f49 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  eak;.  }.  sqlit
25f4a 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
25f4b 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53  ontext, z, -1, S
25f4c 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
25f4d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  .../*.** Impleme
25f4e 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  ntation of the l
25f4f 65 6e 67 74 68 28 29 20 66 75 6e 63 74 69 6f 6e  ength() function
25f50 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
25f51 6c 65 6e 67 74 68 46 75 6e 63 28 0a 20 20 73 71  lengthFunc(.  sq
25f52 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
25f53 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
25f54 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
25f55 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
25f56 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65  int len;..  asse
25f57 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20  rt( argc==1 );. 
25f58 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
25f59 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63  R(argc);.  switc
25f5a 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  h( sqlite3_value
25f5b 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29  _type(argv[0]) )
25f5c 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
25f5d 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73 65  E_BLOB:.    case
25f5e 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a   SQLITE_INTEGER:
25f5f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
25f60 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20  _FLOAT: {.      
25f61 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
25f62 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69  nt(context, sqli
25f63 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
25f64 61 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20  argv[0]));.     
25f65 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
25f66 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
25f67 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73  XT: {.      cons
25f68 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
25f69 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  *z = sqlite3_val
25f6a 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
25f6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30  ;.      if( z==0
25f6c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20   ) return;.     
25f6d 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20   len = 0;.      
25f6e 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20  while( *z ){.   
25f6f 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20       len++;.    
25f70 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
25f71 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 7d  UTF8(z);.      }
25f72 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
25f73 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78  esult_int(contex
25f74 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20 62  t, len);.      b
25f75 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
25f76 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
25f77 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25f78 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20  null(context);. 
25f79 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
25f7a 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  }.  }.}../*.** I
25f7b 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
25f7c 20 74 68 65 20 61 62 73 28 29 20 66 75 6e 63 74   the abs() funct
25f7d 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ion.*/.static vo
25f7e 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c 69 74  id absFunc(sqlit
25f7f 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
25f80 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
25f81 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
25f82 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rgv){.  assert( 
25f83 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55  argc==1 );.  UNU
25f84 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72  SED_PARAMETER(ar
25f85 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73  gc);.  switch( s
25f86 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
25f87 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20  e(argv[0]) ){.  
25f88 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
25f89 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 69  TEGER: {.      i
25f8a 36 34 20 69 56 61 6c 20 3d 20 73 71 6c 69 74 65  64 iVal = sqlite
25f8b 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
25f8c 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66  gv[0]);.      if
25f8d 28 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20  ( iVal<0 ){.    
25f8e 20 20 20 20 69 66 28 20 28 69 56 61 6c 3c 3c 31      if( (iVal<<1
25f8f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
25f90 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25f91 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
25f92 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f  "integer overflo
25f93 77 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20  w", -1);.       
25f94 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
25f95 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 56 61     }.        iVa
25f96 6c 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20  l = -iVal;.     
25f97 20 7d 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65   } .      sqlite
25f98 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
25f99 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20  ontext, iVal);. 
25f9a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
25f9b 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
25f9c 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  E_NULL: {.      
25f9d 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
25f9e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ull(context);.  
25f9f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
25fa0 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
25fa1 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61        double rVa
25fa2 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
25fa3 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d  e_double(argv[0]
25fa4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 56 61  );.      if( rVa
25fa5 6c 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 56  l<0 ) rVal = -rV
25fa6 61 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  al;.      sqlite
25fa7 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
25fa8 63 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a  context, rVal);.
25fa9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
25faa 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
25fab 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
25fac 66 20 74 68 65 20 73 75 62 73 74 72 28 29 20 66  f the substr() f
25fad 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73  unction..**.** s
25fae 75 62 73 74 72 28 78 2c 70 31 2c 70 32 29 20 20  ubstr(x,p1,p2)  
25faf 72 65 74 75 72 6e 73 20 70 32 20 63 68 61 72 61  returns p2 chara
25fb0 63 74 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 67  cters of x[] beg
25fb1 69 6e 6e 69 6e 67 20 77 69 74 68 20 70 31 2e 0a  inning with p1..
25fb2 2a 2a 20 70 31 20 69 73 20 31 2d 69 6e 64 65 78  ** p1 is 1-index
25fb3 65 64 2e 20 20 53 6f 20 73 75 62 73 74 72 28 78  ed.  So substr(x
25fb4 2c 31 2c 31 29 20 72 65 74 75 72 6e 73 20 74 68  ,1,1) returns th
25fb5 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65  e first characte
25fb6 72 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20 78  r.** of x.  If x
25fb7 20 69 73 20 74 65 78 74 2c 20 74 68 65 6e 20 77   is text, then w
25fb8 65 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e 74  e actually count
25fb9 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
25fba 73 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20  s..** If x is a 
25fbb 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 63 6f  blob, then we co
25fbc 75 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a  unt bytes..**.**
25fbd 20 49 66 20 70 31 20 69 73 20 6e 65 67 61 74 69   If p1 is negati
25fbe 76 65 2c 20 74 68 65 6e 20 77 65 20 62 65 67 69  ve, then we begi
25fbf 6e 20 61 62 73 28 70 31 29 20 66 72 6f 6d 20 74  n abs(p1) from t
25fc0 68 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a  he end of x[]..*
25fc1 2a 0a 2a 2a 20 49 66 20 70 32 20 69 73 20 6e 65  *.** If p2 is ne
25fc2 67 61 74 69 76 65 2c 20 72 65 74 75 72 6e 20 74  gative, return t
25fc3 68 65 20 70 32 20 63 68 61 72 61 63 74 65 72 73  he p2 characters
25fc4 20 70 72 65 63 65 65 64 69 6e 67 20 70 31 2e 0a   preceeding p1..
25fc5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
25fc6 75 62 73 74 72 46 75 6e 63 28 0a 20 20 73 71 6c  ubstrFunc(.  sql
25fc7 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25fc8 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
25fc9 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
25fca 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
25fcb 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
25fcc 61 72 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20 75  ar *z;.  const u
25fcd 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 32  nsigned char *z2
25fce 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69  ;.  int len;.  i
25fcf 6e 74 20 70 30 74 79 70 65 3b 0a 20 20 69 36 34  nt p0type;.  i64
25fd0 20 70 31 2c 20 70 32 3b 0a 20 20 69 6e 74 20 6e   p1, p2;.  int n
25fd1 65 67 50 32 20 3d 20 30 3b 0a 0a 20 20 61 73 73  egP2 = 0;..  ass
25fd2 65 72 74 28 20 61 72 67 63 3d 3d 33 20 7c 7c 20  ert( argc==3 || 
25fd3 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28  argc==2 );.  if(
25fd4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25fd5 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51  ype(argv[1])==SQ
25fd6 4c 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 7c 7c 20  LITE_NULL.   || 
25fd7 28 61 72 67 63 3d 3d 33 20 26 26 20 73 71 6c 69  (argc==3 && sqli
25fd8 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
25fd9 72 67 76 5b 32 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[2])==SQLITE_
25fda 4e 55 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20 72  NULL).  ){.    r
25fdb 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 30 74  eturn;.  }.  p0t
25fdc 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ype = sqlite3_va
25fdd 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
25fde 29 3b 0a 20 20 70 31 20 3d 20 73 71 6c 69 74 65  );.  p1 = sqlite
25fdf 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
25fe0 5b 31 5d 29 3b 0a 20 20 69 66 28 20 70 30 74 79  [1]);.  if( p0ty
25fe1 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20  pe==SQLITE_BLOB 
25fe2 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c  ){.    len = sql
25fe3 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
25fe4 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 7a  (argv[0]);.    z
25fe5 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25fe6 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a  _blob(argv[0]);.
25fe7 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72      if( z==0 ) r
25fe8 65 74 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 72  eturn;.    asser
25fe9 74 28 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 33 5f  t( len==sqlite3_
25fea 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
25feb 5b 30 5d 29 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  [0]) );.  }else{
25fec 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  .    z = sqlite3
25fed 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
25fee 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d  [0]);.    if( z=
25fef 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
25ff0 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 69 66   len = 0;.    if
25ff1 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20  ( p1<0 ){.      
25ff2 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c  for(z2=z; *z2; l
25ff3 65 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 53  en++){.        S
25ff4 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
25ff5 7a 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  z2);.      }.   
25ff6 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 61 72 67   }.  }.  if( arg
25ff7 63 3d 3d 33 20 29 7b 0a 20 20 20 20 70 32 20 3d  c==3 ){.    p2 =
25ff8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
25ff9 6e 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  nt(argv[2]);.   
25ffa 20 69 66 28 20 70 32 3c 30 20 29 7b 0a 20 20 20   if( p2<0 ){.   
25ffb 20 20 20 70 32 20 3d 20 2d 70 32 3b 0a 20 20 20     p2 = -p2;.   
25ffc 20 20 20 6e 65 67 50 32 20 3d 20 31 3b 0a 20 20     negP2 = 1;.  
25ffd 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
25ffe 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f   p2 = sqlite3_co
25fff 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
26000 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d 69 74  context)->aLimit
26001 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
26002 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 69 66 28  NGTH];.  }.  if(
26003 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 70 31 20   p1<0 ){.    p1 
26004 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20  += len;.    if( 
26005 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32  p1<0 ){.      p2
26006 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 69 66   += p1;.      if
26007 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b  ( p2<0 ) p2 = 0;
26008 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20  .      p1 = 0;. 
26009 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
2600a 20 70 31 3e 30 20 29 7b 0a 20 20 20 20 70 31 2d   p1>0 ){.    p1-
2600b 2d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  -;.  }else if( p
2600c 32 3e 30 20 29 7b 0a 20 20 20 20 70 32 2d 2d 3b  2>0 ){.    p2--;
2600d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 50 32  .  }.  if( negP2
2600e 20 29 7b 0a 20 20 20 20 70 31 20 2d 3d 20 70 32   ){.    p1 -= p2
2600f 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30 20 29  ;.    if( p1<0 )
26010 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 70 31  {.      p2 += p1
26011 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a  ;.      p1 = 0;.
26012 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
26013 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 32 3e  rt( p1>=0 && p2>
26014 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 30 74 79  =0 );.  if( p0ty
26015 70 65 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20  pe!=SQLITE_BLOB 
26016 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a  ){.    while( *z
26017 20 26 26 20 70 31 20 29 7b 0a 20 20 20 20 20 20   && p1 ){.      
26018 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
26019 28 7a 29 3b 0a 20 20 20 20 20 20 70 31 2d 2d 3b  (z);.      p1--;
2601a 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 7a  .    }.    for(z
2601b 32 3d 7a 3b 20 2a 7a 32 20 26 26 20 70 32 3b 20  2=z; *z2 && p2; 
2601c 70 32 2d 2d 29 7b 0a 20 20 20 20 20 20 53 51 4c  p2--){.      SQL
2601d 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32  ITE_SKIP_UTF8(z2
2601e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2601f 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
26020 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
26021 29 7a 2c 20 28 69 6e 74 29 28 7a 32 2d 7a 29 2c  )z, (int)(z2-z),
26022 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
26023 54 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  T);.  }else{.   
26024 20 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e 20 29   if( p1+p2>len )
26025 7b 0a 20 20 20 20 20 20 70 32 20 3d 20 6c 65 6e  {.      p2 = len
26026 2d 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70  -p1;.      if( p
26027 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 20  2<0 ) p2 = 0;.  
26028 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
26029 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74  result_blob(cont
2602a 65 78 74 2c 20 28 63 68 61 72 2a 29 26 7a 5b 70  ext, (char*)&z[p
2602b 31 5d 2c 20 28 69 6e 74 29 70 32 2c 20 53 51 4c  1], (int)p2, SQL
2602c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
2602d 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
2602e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
2602f 68 65 20 72 6f 75 6e 64 28 29 20 66 75 6e 63 74  he round() funct
26030 69 6f 6e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ion.*/.#ifndef S
26031 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
26032 49 4e 47 5f 50 4f 49 4e 54 0a 73 74 61 74 69 63  ING_POINT.static
26033 20 76 6f 69 64 20 72 6f 75 6e 64 46 75 6e 63 28   void roundFunc(
26034 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
26035 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72  *context, int ar
26036 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
26037 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 69 6e 74  e **argv){.  int
26038 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 75 62 6c 65   n = 0;.  double
26039 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 42 75 66   r;.  char *zBuf
2603a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
2603b 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29  ==1 || argc==2 )
2603c 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20  ;.  if( argc==2 
2603d 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  ){.    if( SQLIT
2603e 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f  E_NULL==sqlite3_
2603f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
26040 31 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  1]) ) return;.  
26041 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    n = sqlite3_va
26042 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29  lue_int(argv[1])
26043 3b 0a 20 20 20 20 69 66 28 20 6e 3e 33 30 20 29  ;.    if( n>30 )
26044 20 6e 20 3d 20 33 30 3b 0a 20 20 20 20 69 66 28   n = 30;.    if(
26045 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20   n<0 ) n = 0;.  
26046 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  }.  if( sqlite3_
26047 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
26048 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
26049 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 72 20 3d   ) return;.  r =
2604a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
2604b 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a  ouble(argv[0]);.
2604c 20 20 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33    zBuf = sqlite3
2604d 5f 6d 70 72 69 6e 74 66 28 22 25 2e 2a 66 22 2c  _mprintf("%.*f",
2604e 6e 2c 72 29 3b 0a 20 20 69 66 28 20 7a 42 75 66  n,r);.  if( zBuf
2604f 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
26050 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
26051 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
26052 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
26053 69 74 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 26  ite3AtoF(zBuf, &
26054 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  r);.    sqlite3_
26055 66 72 65 65 28 7a 42 75 66 29 3b 0a 20 20 20 20  free(zBuf);.    
26056 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
26057 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72  ouble(context, r
26058 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  );.  }.}.#endif.
26059 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
2605a 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 73  nByte bytes of s
2605b 70 61 63 65 20 75 73 69 6e 67 20 73 71 6c 69 74  pace using sqlit
2605c 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 66 20  e3_malloc(). If 
2605d 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  the.** allocatio
2605e 6e 20 66 61 69 6c 73 2c 20 63 61 6c 6c 20 73 71  n fails, call sq
2605f 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
26060 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20 6e 6f  or_nomem() to no
26061 74 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61  tify.** the data
26062 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74  base handle that
26063 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61   malloc() has fa
26064 69 6c 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20  iled and return 
26065 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 6e 42 79 74  NULL..** If nByt
26066 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  e is larger than
26067 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 74 72   the maximum str
26068 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 65 6e 67  ing or blob leng
26069 74 68 2c 20 74 68 65 6e 0a 2a 2a 20 72 61 69 73  th, then.** rais
2606a 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42  e an SQLITE_TOOB
2606b 49 47 20 65 78 63 65 70 74 69 6f 6e 20 61 6e 64  IG exception and
2606c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f   return NULL..*/
2606d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f  .static void *co
2606e 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c 69  ntextMalloc(sqli
2606f 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
26070 74 65 78 74 2c 20 69 36 34 20 6e 42 79 74 65 29  text, i64 nByte)
26071 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73  {.  char *z;.  s
26072 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
26073 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
26074 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
26075 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65  .  assert( nByte
26076 3e 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  >0 );.  testcase
26077 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61 4c 69  ( nByte==db->aLi
26078 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
26079 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 65  _LENGTH] );.  te
2607a 73 74 63 61 73 65 28 20 6e 42 79 74 65 3d 3d 64  stcase( nByte==d
2607b 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
2607c 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31  _LIMIT_LENGTH]+1
2607d 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e   );.  if( nByte>
2607e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
2607f 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
26080 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
26081 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
26082 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ig(context);.   
26083 20 7a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b   z = 0;.  }else{
26084 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  .    z = sqlite3
26085 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 6e 42 79 74  Malloc((int)nByt
26086 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 29  e);.    if( !z )
26087 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
26088 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
26089 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
2608a 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2608b 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  z;.}../*.** Impl
2608c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
2608d 65 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c 6f  e upper() and lo
2608e 77 65 72 28 29 20 53 51 4c 20 66 75 6e 63 74 69  wer() SQL functi
2608f 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ons..*/.static v
26090 6f 69 64 20 75 70 70 65 72 46 75 6e 63 28 73 71  oid upperFunc(sq
26091 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26092 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
26093 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
26094 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20  **argv){.  char 
26095 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  *z1;.  const cha
26096 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20  r *z2;.  int i, 
26097 6e 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  n;.  UNUSED_PARA
26098 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a  METER(argc);.  z
26099 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  2 = (char*)sqlit
2609a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2609b 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71  gv[0]);.  n = sq
2609c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
2609d 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a  s(argv[0]);.  /*
2609e 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
2609f 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28   call to _bytes(
260a0 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c  ) does not inval
260a1 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28  idate the _text(
260a2 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61  ) pointer */.  a
260a3 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72  ssert( z2==(char
260a4 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
260a5 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b  text(argv[0]) );
260a6 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20  .  if( z2 ){.   
260a7 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c   z1 = contextMal
260a8 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69  loc(context, ((i
260a9 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66  64)n)+1);.    if
260aa 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65  ( z1 ){.      me
260ab 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31  mcpy(z1, z2, n+1
260ac 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
260ad 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  ; z1[i]; i++){. 
260ae 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 28         z1[i] = (
260af 63 68 61 72 29 73 71 6c 69 74 65 33 54 6f 75 70  char)sqlite3Toup
260b0 70 65 72 28 7a 31 5b 69 5d 29 3b 0a 20 20 20 20  per(z1[i]);.    
260b1 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
260b2 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
260b3 6e 74 65 78 74 2c 20 7a 31 2c 20 2d 31 2c 20 73  ntext, z1, -1, s
260b4 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
260b5 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63    }.  }.}.static
260b6 20 76 6f 69 64 20 6c 6f 77 65 72 46 75 6e 63 28   void lowerFunc(
260b7 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
260b8 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72  *context, int ar
260b9 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
260ba 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 75 38 20  e **argv){.  u8 
260bb 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  *z1;.  const cha
260bc 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20  r *z2;.  int i, 
260bd 6e 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  n;.  UNUSED_PARA
260be 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a  METER(argc);.  z
260bf 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  2 = (char*)sqlit
260c0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
260c1 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71  gv[0]);.  n = sq
260c2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
260c3 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a  s(argv[0]);.  /*
260c4 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
260c5 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28   call to _bytes(
260c6 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c  ) does not inval
260c7 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28  idate the _text(
260c8 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61  ) pointer */.  a
260c9 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72  ssert( z2==(char
260ca 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
260cb 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b  text(argv[0]) );
260cc 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20  .  if( z2 ){.   
260cd 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c   z1 = contextMal
260ce 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69  loc(context, ((i
260cf 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66  64)n)+1);.    if
260d0 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65  ( z1 ){.      me
260d1 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31  mcpy(z1, z2, n+1
260d2 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
260d3 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  ; z1[i]; i++){. 
260d4 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 73         z1[i] = s
260d5 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 7a 31  qlite3Tolower(z1
260d6 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  [i]);.      }.  
260d7 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
260d8 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
260d9 20 28 63 68 61 72 20 2a 29 7a 31 2c 20 2d 31 2c   (char *)z1, -1,
260da 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
260db 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 23 69      }.  }.}...#i
260dc 66 20 30 20 20 2f 2a 20 54 68 69 73 20 66 75 6e  f 0  /* This fun
260dd 63 74 69 6f 6e 20 69 73 20 6e 65 76 65 72 20 75  ction is never u
260de 73 65 64 2e 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68  sed. */./*.** Th
260df 65 20 43 4f 41 4c 45 53 43 45 28 29 20 61 6e 64  e COALESCE() and
260e0 20 49 46 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69   IFNULL() functi
260e1 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 65 20 69  ons used to be i
260e2 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 73 68  mplemented as sh
260e3 6f 77 6e 0a 2a 2a 20 68 65 72 65 2e 20 20 42 75  own.** here.  Bu
260e4 74 20 6e 6f 77 20 74 68 65 79 20 61 72 65 20 69  t now they are i
260e5 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 56 44  mplemented as VD
260e6 42 45 20 63 6f 64 65 20 73 6f 20 74 68 61 74 20  BE code so that 
260e7 75 6e 75 73 65 64 20 61 72 67 75 6d 65 6e 74 73  unused arguments
260e8 0a 2a 2a 20 64 6f 20 6e 6f 74 20 68 61 76 65 20  .** do not have 
260e9 74 6f 20 62 65 20 63 6f 6d 70 75 74 65 64 2e 20  to be computed. 
260ea 20 54 68 69 73 20 6c 65 67 61 63 79 20 69 6d 70   This legacy imp
260eb 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 72  lementation is r
260ec 65 74 61 69 6e 65 64 20 61 73 0a 2a 2a 20 63 6f  etained as.** co
260ed 6d 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20  mment..*/./*.** 
260ee 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
260ef 66 20 74 68 65 20 49 46 4e 55 4c 4c 28 29 2c 20  f the IFNULL(), 
260f0 4e 56 4c 28 29 2c 20 61 6e 64 20 43 4f 41 4c 45  NVL(), and COALE
260f1 53 43 45 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e  SCE() functions.
260f2 20 20 0a 2a 2a 20 41 6c 6c 20 74 68 72 65 65 20    .** All three 
260f3 64 6f 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e  do the same thin
260f4 67 2e 20 20 54 68 65 79 20 72 65 74 75 72 6e 20  g.  They return 
260f5 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55  the first non-NU
260f6 4c 4c 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a  LL.** argument..
260f7 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
260f8 66 6e 75 6c 6c 46 75 6e 63 28 0a 20 20 73 71 6c  fnullFunc(.  sql
260f9 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
260fa 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
260fb 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
260fc 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
260fd 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
260fe 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20   i<argc; i++){. 
260ff 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55     if( SQLITE_NU
26100 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  LL!=sqlite3_valu
26101 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29 20  e_type(argv[i]) 
26102 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
26103 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f  _result_value(co
26104 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 5d 29 3b  ntext, argv[i]);
26105 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
26106 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
26107 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a   /* NOT USED */.
26108 23 64 65 66 69 6e 65 20 69 66 6e 75 6c 6c 46 75  #define ifnullFu
26109 6e 63 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20  nc versionFunc  
2610a 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 66   /* Substitute f
2610b 75 6e 63 74 69 6f 6e 20 2d 20 6e 65 76 65 72 20  unction - never 
2610c 63 61 6c 6c 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  called */../*.**
2610d 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
2610e 6f 66 20 72 61 6e 64 6f 6d 28 29 2e 20 20 52 65  of random().  Re
2610f 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 69 6e  turn a random in
26110 74 65 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74  teger.  .*/.stat
26111 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 46 75  ic void randomFu
26112 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
26113 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
26114 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
26115 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
26116 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73  *NotUsed2.){.  s
26117 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 3b 0a 20  qlite_int64 r;. 
26118 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
26119 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
2611a 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
2611b 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65  _randomness(size
2611c 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20 20 69 66  of(r), &r);.  if
2611d 28 20 72 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ( r<0 ){.    /* 
2611e 57 65 20 6e 65 65 64 20 74 6f 20 70 72 65 76 65  We need to preve
2611f 6e 74 20 61 20 72 61 6e 64 6f 6d 20 6e 75 6d 62  nt a random numb
26120 65 72 20 6f 66 20 30 78 38 30 30 30 30 30 30 30  er of 0x80000000
26121 30 30 30 30 30 30 30 30 20 0a 20 20 20 20 2a 2a  00000000 .    **
26122 20 28 6f 72 20 2d 39 32 32 33 33 37 32 30 33 36   (or -9223372036
26123 38 35 34 37 37 35 38 30 38 29 20 73 69 6e 63 65  854775808) since
26124 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 61 62 73   when you do abs
26125 28 29 20 6f 66 20 74 68 61 74 0a 20 20 20 20 2a  () of that.    *
26126 2a 20 6e 75 6d 62 65 72 20 6f 66 20 79 6f 75 20  * number of you 
26127 67 65 74 20 74 68 65 20 73 61 6d 65 20 76 61 6c  get the same val
26128 75 65 20 62 61 63 6b 20 61 67 61 69 6e 2e 20 20  ue back again.  
26129 54 6f 20 64 6f 20 74 68 69 73 0a 20 20 20 20 2a  To do this.    *
2612a 2a 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20  * in a way that 
2612b 69 73 20 74 65 73 74 61 62 6c 65 2c 20 6d 61 73  is testable, mas
2612c 6b 20 74 68 65 20 73 69 67 6e 20 62 69 74 20 6f  k the sign bit o
2612d 66 66 20 6f 66 20 6e 65 67 61 74 69 76 65 0a 20  ff of negative. 
2612e 20 20 20 2a 2a 20 76 61 6c 75 65 73 2c 20 72 65     ** values, re
2612f 73 75 6c 74 69 6e 67 20 69 6e 20 61 20 70 6f 73  sulting in a pos
26130 69 74 69 76 65 20 76 61 6c 75 65 2e 20 20 54 68  itive value.  Th
26131 65 6e 20 74 61 6b 65 20 74 68 65 20 0a 20 20 20  en take the .   
26132 20 2a 2a 20 32 73 20 63 6f 6d 70 6c 65 6d 65 6e   ** 2s complemen
26133 74 20 6f 66 20 74 68 61 74 20 70 6f 73 69 74 69  t of that positi
26134 76 65 20 76 61 6c 75 65 2e 20 20 54 68 65 20 65  ve value.  The e
26135 6e 64 20 72 65 73 75 6c 74 20 63 61 6e 0a 20 20  nd result can.  
26136 20 20 2a 2a 20 74 68 65 72 65 66 6f 72 65 20 62    ** therefore b
26137 65 20 6e 6f 20 6c 65 73 73 20 74 68 61 6e 20 2d  e no less than -
26138 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
26139 38 30 37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  807..    */.    
2613a 72 20 3d 20 2d 28 72 20 5e 20 28 28 28 73 71 6c  r = -(r ^ (((sql
2613b 69 74 65 33 5f 69 6e 74 36 34 29 31 29 3c 3c 36  ite3_int64)1)<<6
2613c 33 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  3));.  }.  sqlit
2613d 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
2613e 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a  context, r);.}..
2613f 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
26140 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 62 6c  tion of randombl
26141 6f 62 28 4e 29 2e 20 20 52 65 74 75 72 6e 20 61  ob(N).  Return a
26142 20 72 61 6e 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20   random blob.** 
26143 74 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 20  that is N bytes 
26144 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  long..*/.static 
26145 76 6f 69 64 20 72 61 6e 64 6f 6d 42 6c 6f 62 28  void randomBlob(
26146 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
26147 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
26148 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
26149 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
2614a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e  ){.  int n;.  un
2614b 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 3b 0a  signed char *p;.
2614c 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
2614d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  1 );.  UNUSED_PA
2614e 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
2614f 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
26150 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b  ue_int(argv[0]);
26151 0a 20 20 69 66 28 20 6e 3c 31 20 29 7b 0a 20 20  .  if( n<1 ){.  
26152 20 20 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70    n = 1;.  }.  p
26153 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63   = contextMalloc
26154 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b 0a 20 20  (context, n);.  
26155 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c  if( p ){.    sql
26156 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
26157 6e 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  n, p);.    sqlit
26158 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63  e3_result_blob(c
26159 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 70  ontext, (char*)p
2615a 2c 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  , n, sqlite3_fre
2615b 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
2615c 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
2615d 6f 66 20 74 68 65 20 6c 61 73 74 5f 69 6e 73 65  of the last_inse
2615e 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 66  rt_rowid() SQL f
2615f 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65  unction.  The re
26160 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 73  turn.** value is
26161 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
26162 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
26163 73 65 72 74 5f 72 6f 77 69 64 28 29 20 41 50 49  sert_rowid() API
26164 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
26165 61 74 69 63 20 76 6f 69 64 20 6c 61 73 74 5f 69  atic void last_i
26166 6e 73 65 72 74 5f 72 6f 77 69 64 28 0a 20 20 73  nsert_rowid(.  s
26167 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
26168 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20  context, .  int 
26169 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69  NotUsed, .  sqli
2616a 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
2616b 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65  sed2.){.  sqlite
2616c 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
2616d 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2616e 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e  e(context);.  UN
2616f 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
26170 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
26171 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  2);.  sqlite3_re
26172 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
26173 78 74 2c 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  xt, sqlite3_last
26174 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 64 62  _insert_rowid(db
26175 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ));.}../*.** Imp
26176 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
26177 68 65 20 63 68 61 6e 67 65 73 28 29 20 53 51 4c  he changes() SQL
26178 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
26179 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
2617a 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20 74  the.** same as t
2617b 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  he sqlite3_chang
2617c 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f  es() API functio
2617d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
2617e 64 20 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c  d changes(.  sql
2617f 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
26180 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
26181 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
26182 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
26183 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
26184 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
26185 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
26186 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44  ntext);.  UNUSED
26187 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
26188 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
26189 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2618a 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  _int(context, sq
2618b 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 64 62  lite3_changes(db
2618c 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ));.}../*.** Imp
2618d 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
2618e 68 65 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  he total_changes
2618f 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  () SQL function.
26190 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
26191 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d  ue is.** the sam
26192 65 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33  e as the sqlite3
26193 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
26194 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   API function..*
26195 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f  /.static void to
26196 74 61 6c 5f 63 68 61 6e 67 65 73 28 0a 20 20 73  tal_changes(.  s
26197 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
26198 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
26199 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
2619a 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
2619b 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  d2.){.  sqlite3 
2619c 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
2619d 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
2619e 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53  context);.  UNUS
2619f 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
261a0 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
261a1 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
261a2 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20  lt_int(context, 
261a3 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
261a4 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f  anges(db));.}../
261a5 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65  *.** A structure
261a6 20 64 65 66 69 6e 69 6e 67 20 68 6f 77 20 74 6f   defining how to
261a7 20 64 6f 20 47 4c 4f 42 2d 73 74 79 6c 65 20 63   do GLOB-style c
261a8 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f 0a 73  omparisons..*/.s
261a9 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
261aa 6f 20 7b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c  o {.  u8 matchAl
261ab 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65  l;.  u8 matchOne
261ac 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74 3b  ;.  u8 matchSet;
261ad 0a 20 20 75 38 20 6e 6f 43 61 73 65 3b 0a 7d 3b  .  u8 noCase;.};
261ae 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 4c 49 4b 45  ../*.** For LIKE
261af 20 61 6e 64 20 47 4c 4f 42 20 6d 61 74 63 68 69   and GLOB matchi
261b0 6e 67 20 6f 6e 20 45 42 43 44 49 43 20 6d 61 63  ng on EBCDIC mac
261b1 68 69 6e 65 73 2c 20 61 73 73 75 6d 65 20 74 68  hines, assume th
261b2 61 74 20 65 76 65 72 79 0a 2a 2a 20 63 68 61 72  at every.** char
261b3 61 63 74 65 72 20 69 73 20 65 78 61 63 74 6c 79  acter is exactly
261b4 20 6f 6e 65 20 62 79 74 65 20 69 6e 20 73 69 7a   one byte in siz
261b5 65 2e 20 20 41 6c 73 6f 2c 20 61 6c 6c 20 63 68  e.  Also, all ch
261b6 61 72 61 63 74 65 72 73 20 61 72 65 0a 2a 2a 20  aracters are.** 
261b7 61 62 6c 65 20 74 6f 20 70 61 72 74 69 63 69 70  able to particip
261b8 61 74 65 20 69 6e 20 75 70 70 65 72 2d 63 61 73  ate in upper-cas
261b9 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63 61 73 65 20  e-to-lower-case 
261ba 6d 61 70 70 69 6e 67 73 20 69 6e 20 45 42 43 44  mappings in EBCD
261bb 49 43 0a 2a 2a 20 77 68 65 72 65 61 73 20 6f 6e  IC.** whereas on
261bc 6c 79 20 63 68 61 72 61 63 74 65 72 73 20 6c 65  ly characters le
261bd 73 73 20 74 68 61 6e 20 30 78 38 30 20 64 6f 20  ss than 0x80 do 
261be 69 6e 20 41 53 43 49 49 2e 0a 2a 2f 0a 23 69 66  in ASCII..*/.#if
261bf 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
261c0 45 42 43 44 49 43 29 0a 23 20 64 65 66 69 6e 65  EBCDIC).# define
261c1 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
261c2 28 41 2c 43 29 20 20 20 20 28 2a 28 41 2b 2b 29  (A,C)    (*(A++)
261c3 29 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f 67 55  ).# define GlogU
261c4 70 70 65 72 54 6f 4c 6f 77 65 72 28 41 29 20 20  pperToLower(A)  
261c5 20 20 20 41 20 3d 20 73 71 6c 69 74 65 33 55 70     A = sqlite3Up
261c6 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 0a 23 65  perToLower[A].#e
261c7 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f  lse.# define Glo
261c8 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 41 29  gUpperToLower(A)
261c9 20 20 20 20 20 69 66 28 20 41 3c 30 78 38 30 20       if( A<0x80 
261ca 29 7b 20 41 20 3d 20 73 71 6c 69 74 65 33 55 70  ){ A = sqlite3Up
261cb 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 3b 20 7d  perToLower[A]; }
261cc 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69 63 20  .#endif..static 
261cd 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d  const struct com
261ce 70 61 72 65 49 6e 66 6f 20 67 6c 6f 62 49 6e 66  pareInfo globInf
261cf 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 3f 27 2c 20  o = { '*', '?', 
261d0 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a 20 54 68 65  '[', 0 };./* The
261d1 20 63 6f 72 72 65 63 74 20 53 51 4c 2d 39 32 20   correct SQL-92 
261d2 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20  behavior is for 
261d3 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
261d4 72 20 74 6f 20 69 67 6e 6f 72 65 0a 2a 2a 20 63  r to ignore.** c
261d5 61 73 65 2e 20 20 54 68 75 73 20 20 27 61 27 20  ase.  Thus  'a' 
261d6 4c 49 4b 45 20 27 41 27 20 77 6f 75 6c 64 20 62  LIKE 'A' would b
261d7 65 20 74 72 75 65 2e 20 2a 2f 0a 73 74 61 74 69  e true. */.stati
261d8 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63  c const struct c
261d9 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49  ompareInfo likeI
261da 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 27 25 27 2c  nfoNorm = { '%',
261db 20 27 5f 27 2c 20 20 20 30 2c 20 31 20 7d 3b 0a   '_',   0, 1 };.
261dc 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 41 53  /* If SQLITE_CAS
261dd 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45  E_SENSITIVE_LIKE
261de 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
261df 6e 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61  n the LIKE opera
261e0 74 6f 72 0a 2a 2a 20 69 73 20 63 61 73 65 20 73  tor.** is case s
261e1 65 6e 73 69 74 69 76 65 20 63 61 75 73 69 6e 67  ensitive causing
261e2 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20 74 6f   'a' LIKE 'A' to
261e3 20 62 65 20 66 61 6c 73 65 20 2a 2f 0a 73 74 61   be false */.sta
261e4 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
261e5 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b   compareInfo lik
261e6 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 27 25 27  eInfoAlt = { '%'
261e7 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 20 7d 3b  , '_',   0, 0 };
261e8 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ../*.** Compare 
261e9 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69 6e 67  two UTF-8 string
261ea 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79 20 77  s for equality w
261eb 68 65 72 65 20 74 68 65 20 66 69 72 73 74 20 73  here the first s
261ec 74 72 69 6e 67 20 63 61 6e 0a 2a 2a 20 70 6f 74  tring can.** pot
261ed 65 6e 74 69 61 6c 6c 79 20 62 65 20 61 20 22 67  entially be a "g
261ee 6c 6f 62 22 20 65 78 70 72 65 73 73 69 6f 6e 2e  lob" expression.
261ef 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 31    Return true (1
261f0 29 20 69 66 20 74 68 65 79 0a 2a 2a 20 61 72 65  ) if they.** are
261f1 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 66 61   the same and fa
261f2 6c 73 65 20 28 30 29 20 69 66 20 74 68 65 79 20  lse (0) if they 
261f3 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a  are different..*
261f4 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e 67 20 72 75  *.** Globbing ru
261f5 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  les:.**.**      
261f6 27 2a 27 20 20 20 20 20 20 20 4d 61 74 63 68 65  '*'       Matche
261f7 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 20 6f  s any sequence o
261f8 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 63  f zero or more c
261f9 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  haracters..**.**
261fa 20 20 20 20 20 20 27 3f 27 20 20 20 20 20 20 20        '?'       
261fb 4d 61 74 63 68 65 73 20 65 78 61 63 74 6c 79 20  Matches exactly 
261fc 6f 6e 65 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  one character..*
261fd 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20 20  *.**     [...]  
261fe 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20      Matches one 
261ff 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d 20 74  character from t
26200 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74  he enclosed list
26201 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
26202 20 20 20 20 20 20 63 68 61 72 61 63 74 65 72 73        characters
26203 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 5e 2e 2e  ..**.**     [^..
26204 2e 5d 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f  .]     Matches o
26205 6e 65 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74  ne character not
26206 20 69 6e 20 74 68 65 20 65 6e 63 6c 6f 73 65 64   in the enclosed
26207 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74   list..**.** Wit
26208 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e 64 20  h the [...] and 
26209 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e 67 2c  [^...] matching,
2620a 20 61 20 27 5d 27 20 63 68 61 72 61 63 74 65 72   a ']' character
2620b 20 63 61 6e 20 62 65 20 69 6e 63 6c 75 64 65 64   can be included
2620c 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73 74 20  .** in the list 
2620d 62 79 20 6d 61 6b 69 6e 67 20 69 74 20 74 68 65  by making it the
2620e 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
2620f 20 61 66 74 65 72 20 27 5b 27 20 6f 72 20 27 5e   after '[' or '^
26210 27 2e 20 20 41 0a 2a 2a 20 72 61 6e 67 65 20 6f  '.  A.** range o
26211 66 20 63 68 61 72 61 63 74 65 72 73 20 63 61 6e  f characters can
26212 20 62 65 20 73 70 65 63 69 66 69 65 64 20 75 73   be specified us
26213 69 6e 67 20 27 2d 27 2e 20 20 45 78 61 6d 70 6c  ing '-'.  Exampl
26214 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d 22 20 6d 61  e:.** "[a-z]" ma
26215 74 63 68 65 73 20 61 6e 79 20 73 69 6e 67 6c 65  tches any single
26216 20 6c 6f 77 65 72 2d 63 61 73 65 20 6c 65 74 74   lower-case lett
26217 65 72 2e 20 20 54 6f 20 6d 61 74 63 68 20 61 20  er.  To match a 
26218 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a 20 69 74 20  '-', make.** it 
26219 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  the last charact
2621a 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a  er in the list..
2621b 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2621c 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20 71 75  ne is usually qu
2621d 69 63 6b 2c 20 62 75 74 20 63 61 6e 20 62 65 20  ick, but can be 
2621e 4e 2a 2a 32 20 69 6e 20 74 68 65 20 77 6f 72 73  N**2 in the wors
2621f 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 69  t case..**.** Hi
26220 6e 74 73 3a 20 74 6f 20 6d 61 74 63 68 20 27 2a  nts: to match '*
26221 27 20 6f 72 20 27 3f 27 2c 20 70 75 74 20 74 68  ' or '?', put th
26222 65 6d 20 69 6e 20 22 5b 5d 22 2e 20 20 4c 69 6b  em in "[]".  Lik
26223 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
26224 20 20 20 20 20 20 61 62 63 5b 2a 5d 78 79 7a 20        abc[*]xyz 
26225 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 22         Matches "
26226 61 62 63 2a 78 79 7a 22 20 6f 6e 6c 79 0a 2a 2f  abc*xyz" only.*/
26227 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 74 74  .static int patt
26228 65 72 6e 43 6f 6d 70 61 72 65 28 0a 20 20 63 6f  ernCompare(.  co
26229 6e 73 74 20 75 38 20 2a 7a 50 61 74 74 65 72 6e  nst u8 *zPattern
2622a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2622b 2a 20 54 68 65 20 67 6c 6f 62 20 70 61 74 74 65  * The glob patte
2622c 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 38  rn */.  const u8
2622d 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 20 20   *zString,      
2622e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2622f 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72  string to compar
26230 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 67 6c  e against the gl
26231 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 74  ob */.  const st
26232 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
26233 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66 6f   *pInfo, /* Info
26234 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 68 6f  rmation about ho
26235 77 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70  w to do the comp
26236 61 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69  are */.  const i
26237 6e 74 20 65 73 63 20 20 20 20 20 20 20 20 20 20  nt esc          
26238 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
26239 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65   escape characte
2623a 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 2c  r */.){.  int c,
2623b 20 63 32 3b 0a 20 20 69 6e 74 20 69 6e 76 65 72   c2;.  int inver
2623c 74 3b 0a 20 20 69 6e 74 20 73 65 65 6e 3b 0a 20  t;.  int seen;. 
2623d 20 75 38 20 6d 61 74 63 68 4f 6e 65 20 3d 20 70   u8 matchOne = p
2623e 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b 0a  Info->matchOne;.
2623f 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 20 3d 20    u8 matchAll = 
26240 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c 3b  pInfo->matchAll;
26241 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74 20 3d  .  u8 matchSet =
26242 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 53 65 74   pInfo->matchSet
26243 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 20 3d 20  ;.  u8 noCase = 
26244 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b 20 0a  pInfo->noCase; .
26245 20 20 69 6e 74 20 70 72 65 76 45 73 63 61 70 65    int prevEscape
26246 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75   = 0;     /* Tru
26247 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75  e if the previou
26248 73 20 63 68 61 72 61 63 74 65 72 20 77 61 73 20  s character was 
26249 27 65 73 63 61 70 65 27 20 2a 2f 0a 0a 20 20 77  'escape' */..  w
2624a 68 69 6c 65 28 20 28 63 20 3d 20 73 71 6c 69 74  hile( (c = sqlit
2624b 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
2624c 65 72 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29 21  ern,&zPattern))!
2624d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70  =0 ){.    if( !p
2624e 72 65 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d  revEscape && c==
2624f 6d 61 74 63 68 41 6c 6c 20 29 7b 0a 20 20 20 20  matchAll ){.    
26250 20 20 77 68 69 6c 65 28 20 28 63 3d 73 71 6c 69    while( (c=sqli
26251 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
26252 74 65 72 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29  tern,&zPattern))
26253 20 3d 3d 20 6d 61 74 63 68 41 6c 6c 0a 20 20 20   == matchAll.   
26254 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 63              || c
26255 20 3d 3d 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a   == matchOne ){.
26256 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 6d          if( c==m
26257 61 74 63 68 4f 6e 65 20 26 26 20 73 71 6c 69 74  atchOne && sqlit
26258 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69  e3Utf8Read(zStri
26259 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30  ng, &zString)==0
2625a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
2625b 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
2625c 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
2625d 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20  if( c==0 ){.    
2625e 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2625f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
26260 3d 65 73 63 20 29 7b 0a 20 20 20 20 20 20 20 20  =esc ){.        
26261 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  c = sqlite3Utf8R
26262 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a  ead(zPattern, &z
26263 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20  Pattern);.      
26264 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20    if( c==0 ){.  
26265 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
26266 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
26267 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d    }else if( c==m
26268 61 74 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20  atchSet ){.     
26269 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d 3d     assert( esc==
2626a 30 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  0 );         /* 
2626b 54 68 69 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f  This is GLOB, no
2626c 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20  t LIKE */.      
2626d 20 20 61 73 73 65 72 74 28 20 6d 61 74 63 68 53    assert( matchS
2626e 65 74 3c 30 78 38 30 20 29 3b 20 20 2f 2a 20 27  et<0x80 );  /* '
2626f 5b 27 20 69 73 20 61 20 73 69 6e 67 6c 65 2d 62  [' is a single-b
26270 79 74 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f  yte character */
26271 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
26272 2a 7a 53 74 72 69 6e 67 20 26 26 20 70 61 74 74  *zString && patt
26273 65 72 6e 43 6f 6d 70 61 72 65 28 26 7a 50 61 74  ernCompare(&zPat
26274 74 65 72 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67  tern[-1],zString
26275 2c 70 49 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29  ,pInfo,esc)==0 )
26276 7b 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  {.          SQLI
26277 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 53 74  TE_SKIP_UTF8(zSt
26278 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d  ring);.        }
26279 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2627a 2a 7a 53 74 72 69 6e 67 21 3d 30 3b 0a 20 20 20  *zString!=0;.   
2627b 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65     }.      while
2627c 28 20 28 63 32 20 3d 20 73 71 6c 69 74 65 33 55  ( (c2 = sqlite3U
2627d 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
2627e 26 7a 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b  &zString))!=0 ){
2627f 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43  .        if( noC
26280 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
26281 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
26282 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20  r(c2);.         
26283 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
26284 72 28 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r(c);.          
26285 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26  while( c2 != 0 &
26286 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20  & c2 != c ){.   
26287 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
26288 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
26289 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29  tring, &zString)
2628a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 47 6c  ;.            Gl
2628b 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63  ogUpperToLower(c
2628c 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  2);.          }.
2628d 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2628e 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
2628f 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 21 3d  c2 != 0 && c2 !=
26290 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20   c ){.          
26291 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
26292 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20  f8Read(zString, 
26293 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20  &zString);.     
26294 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
26295 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 32 3d  .        if( c2=
26296 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
26297 20 20 20 20 20 20 20 69 66 28 20 70 61 74 74 65         if( patte
26298 72 6e 43 6f 6d 70 61 72 65 28 7a 50 61 74 74 65  rnCompare(zPatte
26299 72 6e 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f  rn,zString,pInfo
2629a 2c 65 73 63 29 20 29 20 72 65 74 75 72 6e 20 31  ,esc) ) return 1
2629b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2629c 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65  return 0;.    }e
2629d 6c 73 65 20 69 66 28 20 21 70 72 65 76 45 73 63  lse if( !prevEsc
2629e 61 70 65 20 26 26 20 63 3d 3d 6d 61 74 63 68 4f  ape && c==matchO
2629f 6e 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ne ){.      if( 
262a0 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
262a1 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e  zString, &zStrin
262a2 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  g)==0 ){.       
262a3 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
262a4 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
262a5 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a   c==matchSet ){.
262a6 20 20 20 20 20 20 69 6e 74 20 70 72 69 6f 72 5f        int prior_
262a7 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73  c = 0;.      ass
262a8 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b 20 20  ert( esc==0 );  
262a9 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f    /* This only o
262aa 63 63 75 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20  ccurs for GLOB, 
262ab 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20  not LIKE */.    
262ac 20 20 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20    seen = 0;.    
262ad 20 20 69 6e 76 65 72 74 20 3d 20 30 3b 0a 20 20    invert = 0;.  
262ae 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
262af 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
262b0 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20   &zString);.    
262b1 20 20 69 66 28 20 63 3d 3d 30 20 29 20 72 65 74    if( c==0 ) ret
262b2 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 63 32 20  urn 0;.      c2 
262b3 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
262b4 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61  d(zPattern, &zPa
262b5 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 69 66  ttern);.      if
262b6 28 20 63 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20  ( c2=='^' ){.   
262b7 20 20 20 20 20 69 6e 76 65 72 74 20 3d 20 31 3b       invert = 1;
262b8 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71  .        c2 = sq
262b9 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
262ba 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72  attern, &zPatter
262bb 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
262bc 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b    if( c2==']' ){
262bd 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
262be 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a  ']' ) seen = 1;.
262bf 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c          c2 = sql
262c0 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
262c1 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e  ttern, &zPattern
262c2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
262c3 20 77 68 69 6c 65 28 20 63 32 20 26 26 20 63 32   while( c2 && c2
262c4 21 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20 20 20  !=']' ){.       
262c5 20 69 66 28 20 63 32 3d 3d 27 2d 27 20 26 26 20   if( c2=='-' && 
262c6 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 27 5d 27  zPattern[0]!=']'
262c7 20 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21   && zPattern[0]!
262c8 3d 30 20 26 26 20 70 72 69 6f 72 5f 63 3e 30 20  =0 && prior_c>0 
262c9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 32 20  ){.          c2 
262ca 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
262cb 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61  d(zPattern, &zPa
262cc 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20  ttern);.        
262cd 20 20 69 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63    if( c>=prior_c
262ce 20 26 26 20 63 3c 3d 63 32 20 29 20 73 65 65 6e   && c<=c2 ) seen
262cf 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
262d0 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20  prior_c = 0;.   
262d1 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
262d2 20 20 20 20 20 20 69 66 28 20 63 3d 3d 63 32 20        if( c==c2 
262d3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
262d4 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  een = 1;.       
262d5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
262d6 72 69 6f 72 5f 63 20 3d 20 63 32 3b 0a 20 20 20  rior_c = c2;.   
262d7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
262d8 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
262d9 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a  ead(zPattern, &z
262da 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20  Pattern);.      
262db 7d 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d  }.      if( c2==
262dc 30 20 7c 7c 20 28 73 65 65 6e 20 5e 20 69 6e 76  0 || (seen ^ inv
262dd 65 72 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ert)==0 ){.     
262de 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
262df 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
262e0 66 28 20 65 73 63 3d 3d 63 20 26 26 20 21 70 72  f( esc==c && !pr
262e1 65 76 45 73 63 61 70 65 20 29 7b 0a 20 20 20 20  evEscape ){.    
262e2 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 31    prevEscape = 1
262e3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
262e4 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55     c2 = sqlite3U
262e5 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
262e6 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20   &zString);.    
262e7 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a    if( noCase ){.
262e8 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
262e9 72 54 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20  rToLower(c);.   
262ea 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f       GlogUpperTo
262eb 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20  Lower(c2);.     
262ec 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 21 3d   }.      if( c!=
262ed 63 32 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  c2 ){.        re
262ee 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
262ef 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70 65        prevEscape
262f0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
262f1 20 20 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e    return *zStrin
262f2 67 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  g==0;.}../*.** C
262f3 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
262f4 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68  of times that th
262f5 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  e LIKE operator 
262f6 28 6f 72 20 47 4c 4f 42 20 77 68 69 63 68 20 69  (or GLOB which i
262f7 73 0a 2a 2a 20 6a 75 73 74 20 61 20 76 61 72 69  s.** just a vari
262f8 61 74 69 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67  ation of LIKE) g
262f9 65 74 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 69  ets called.  Thi
262fa 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  s is used for te
262fb 73 74 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a  sting.** only..*
262fc 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
262fd 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
262fe 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65  int sqlite3_like
262ff 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64  _count = 0;.#end
26300 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  if.../*.** Imple
26301 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
26302 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63   like() SQL func
26303 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63  tion.  This func
26304 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a  tion implements.
26305 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20  ** the build-in 
26306 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20  LIKE operator.  
26307 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
26308 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
26309 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 74  on is the.** pat
2630a 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63  tern and the sec
2630b 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
2630c 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c  the string.  So,
2630d 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
2630e 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  nts:.**.**      
2630f 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20   A LIKE B.**.** 
26310 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  is implemented a
26311 73 20 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a  s like(B,A)..**.
26312 2a 2a 20 54 68 69 73 20 73 61 6d 65 20 66 75 6e  ** This same fun
26313 63 74 69 6f 6e 20 28 77 69 74 68 20 61 20 64 69  ction (with a di
26314 66 66 65 72 65 6e 74 20 63 6f 6d 70 61 72 65 49  fferent compareI
26315 6e 66 6f 20 73 74 72 75 63 74 75 72 65 29 20 63  nfo structure) c
26316 6f 6d 70 75 74 65 73 0a 2a 2a 20 74 68 65 20 47  omputes.** the G
26317 4c 4f 42 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f  LOB operator..*/
26318 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 69 6b  .static void lik
26319 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
2631a 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
2631b 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  t, .  int argc, 
2631c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2631d 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e   **argv.){.  con
2631e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2631f 20 2a 7a 41 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74   *zA, *zB;.  int
26320 20 65 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 69   escape = 0;.  i
26321 6e 74 20 6e 50 61 74 3b 0a 20 20 73 71 6c 69 74  nt nPat;.  sqlit
26322 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
26323 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
26324 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20  le(context);..  
26325 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  zB = sqlite3_val
26326 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
26327 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 65 33  ;.  zA = sqlite3
26328 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
26329 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69  [1]);..  /* Limi
2632a 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  t the length of 
2632b 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  the LIKE or GLOB
2632c 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 6f 69   pattern to avoi
2632d 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20  d problems.  ** 
2632e 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 69 6f  of deep recursio
2632f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69  n and N*N behavi
26330 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d  or in patternCom
26331 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e  pare()..  */.  n
26332 50 61 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Pat = sqlite3_va
26333 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
26334 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20  ]);.  testcase( 
26335 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74  nPat==db->aLimit
26336 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49  [SQLITE_LIMIT_LI
26337 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
26338 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  H] );.  testcase
26339 28 20 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d  ( nPat==db->aLim
2633a 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
2633b 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
2633c 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20  GTH]+1 );.  if( 
2633d 6e 50 61 74 20 3e 20 64 62 2d 3e 61 4c 69 6d 69  nPat > db->aLimi
2633e 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
2633f 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47  IKE_PATTERN_LENG
26340 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  TH] ){.    sqlit
26341 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
26342 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f  context, "LIKE o
26343 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74  r GLOB pattern t
26344 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29  oo complex", -1)
26345 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
26346 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 42 3d 3d  }.  assert( zB==
26347 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
26348 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20  xt(argv[0]) );  
26349 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 64 69 64 20  /* Encoding did 
2634a 6e 6f 74 20 63 68 61 6e 67 65 20 2a 2f 0a 0a 20  not change */.. 
2634b 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a   if( argc==3 ){.
2634c 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70      /* The escap
2634d 65 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69  e character stri
2634e 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 73 74 20  ng must consist 
2634f 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d  of a single UTF-
26350 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20  8 character..   
26351 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72   ** Otherwise, r
26352 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a  eturn an error..
26353 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74      */.    const
26354 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
26355 7a 45 73 63 20 3d 20 73 71 6c 69 74 65 33 5f 76  zEsc = sqlite3_v
26356 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32  alue_text(argv[2
26357 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 73 63  ]);.    if( zEsc
26358 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
26359 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 74 66    if( sqlite3Utf
2635a 38 43 68 61 72 4c 65 6e 28 28 63 68 61 72 2a 29  8CharLen((char*)
2635b 7a 45 73 63 2c 20 2d 31 29 21 3d 31 20 29 7b 0a  zEsc, -1)!=1 ){.
2635c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
2635d 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
2635e 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22  xt, .          "
2635f 45 53 43 41 50 45 20 65 78 70 72 65 73 73 69 6f  ESCAPE expressio
26360 6e 20 6d 75 73 74 20 62 65 20 61 20 73 69 6e 67  n must be a sing
26361 6c 65 20 63 68 61 72 61 63 74 65 72 22 2c 20 2d  le character", -
26362 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  1);.      return
26363 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 73 63 61  ;.    }.    esca
26364 70 65 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  pe = sqlite3Utf8
26365 52 65 61 64 28 7a 45 73 63 2c 20 26 7a 45 73 63  Read(zEsc, &zEsc
26366 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 41 20  );.  }.  if( zA 
26367 26 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 74 72  && zB ){.    str
26368 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
26369 2a 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  *pInfo = sqlite3
2636a 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
2636b 78 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  xt);.#ifdef SQLI
2636c 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69  TE_TEST.    sqli
2636d 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b  te3_like_count++
2636e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20  ;.#endif.    .  
2636f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
26370 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 61  _int(context, pa
26371 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 42 2c  tternCompare(zB,
26372 20 7a 41 2c 20 70 49 6e 66 6f 2c 20 65 73 63 61   zA, pInfo, esca
26373 70 65 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  pe));.  }.}../*.
26374 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
26375 6e 20 6f 66 20 74 68 65 20 4e 55 4c 4c 49 46 28  n of the NULLIF(
26376 78 2c 79 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  x,y) function.  
26377 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68  The result is th
26378 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
26379 65 6e 74 20 69 66 20 74 68 65 20 61 72 67 75 6d  ent if the argum
2637a 65 6e 74 73 20 61 72 65 20 64 69 66 66 65 72 65  ents are differe
2637b 6e 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  nt.  The result 
2637c 69 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 0a 2a  is NULL if the.*
2637d 2a 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  * arguments are 
2637e 65 71 75 61 6c 20 74 6f 20 65 61 63 68 20 6f 74  equal to each ot
2637f 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  her..*/.static v
26380 6f 69 64 20 6e 75 6c 6c 69 66 46 75 6e 63 28 0a  oid nullifFunc(.
26381 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
26382 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
26383 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
26384 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
26385 76 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  v.){.  CollSeq *
26386 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
26387 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f  etFuncCollSeq(co
26388 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44  ntext);.  UNUSED
26389 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
2638a 65 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ed);.  if( sqlit
2638b 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67  e3MemCompare(arg
2638c 76 5b 30 5d 2c 20 61 72 67 76 5b 31 5d 2c 20 70  v[0], argv[1], p
2638d 43 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20  Coll)!=0 ){.    
2638e 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
2638f 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  alue(context, ar
26390 67 76 5b 30 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  gv[0]);.  }.}../
26391 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
26392 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
26393 65 5f 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63  e_version() func
26394 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c  tion.  The resul
26395 74 20 69 73 20 74 68 65 20 76 65 72 73 69 6f 6e  t is the version
26396 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 69 74  .** of the SQLit
26397 65 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69  e library that i
26398 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  s running..*/.st
26399 61 74 69 63 20 76 6f 69 64 20 76 65 72 73 69 6f  atic void versio
2639a 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  nFunc(.  sqlite3
2639b 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
2639c 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
2639d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
2639e 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a  e **NotUsed2.){.
2639f 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
263a0 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
263a1 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65  Used2);.  sqlite
263a2 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
263a3 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76  ntext, sqlite3_v
263a4 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53 51 4c 49  ersion, -1, SQLI
263a5 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f  TE_STATIC);.}../
263a6 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
263a7 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
263a8 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 20 66 75  e_source_id() fu
263a9 6e 63 74 69 6f 6e 2e 20 54 68 65 20 72 65 73 75  nction. The resu
263aa 6c 74 20 69 73 20 61 20 73 74 72 69 6e 67 0a 2a  lt is a string.*
263ab 2a 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65  * that identifie
263ac 73 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72  s the particular
263ad 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
263ae 73 6f 75 72 63 65 20 63 6f 64 65 20 75 73 65 64  source code used
263af 20 74 6f 20 62 75 69 6c 64 0a 2a 2a 20 53 51 4c   to build.** SQL
263b0 69 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ite..*/.static v
263b1 6f 69 64 20 73 6f 75 72 63 65 69 64 46 75 6e 63  oid sourceidFunc
263b2 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
263b3 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
263b4 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
263b5 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
263b6 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55  otUsed2.){.  UNU
263b7 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
263b8 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
263b9 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
263ba 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
263bb 2c 20 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f  , SQLITE_SOURCE_
263bc 49 44 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53  ID, -1, SQLITE_S
263bd 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 72  TATIC);.}../* Ar
263be 72 61 79 20 66 6f 72 20 63 6f 6e 76 65 72 74 69  ray for converti
263bf 6e 67 20 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74  ng from half-byt
263c0 65 73 20 28 6e 79 62 62 6c 65 73 29 20 69 6e 74  es (nybbles) int
263c1 6f 20 41 53 43 49 49 20 68 65 78 0a 2a 2a 20 64  o ASCII hex.** d
263c2 69 67 69 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63  igits. */.static
263c3 20 63 6f 6e 73 74 20 63 68 61 72 20 68 65 78 64   const char hexd
263c4 69 67 69 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30  igits[] = {.  '0
263c5 27 2c 20 27 31 27 2c 20 27 32 27 2c 20 27 33 27  ', '1', '2', '3'
263c6 2c 20 27 34 27 2c 20 27 35 27 2c 20 27 36 27 2c  , '4', '5', '6',
263c7 20 27 37 27 2c 0a 20 20 27 38 27 2c 20 27 39 27   '7',.  '8', '9'
263c8 2c 20 27 41 27 2c 20 27 42 27 2c 20 27 43 27 2c  , 'A', 'B', 'C',
263c9 20 27 44 27 2c 20 27 45 27 2c 20 27 46 27 20 0a   'D', 'E', 'F' .
263ca 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49  };../*.** EXPERI
263cb 4d 45 4e 54 41 4c 20 2d 20 54 68 69 73 20 69 73  MENTAL - This is
263cc 20 6e 6f 74 20 61 6e 20 6f 66 66 69 63 69 61 6c   not an official
263cd 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
263ce 69 6e 74 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a  interface may.**
263cf 20 63 68 61 6e 67 65 2e 20 20 54 68 69 73 20 66   change.  This f
263d0 75 6e 63 74 69 6f 6e 20 6d 61 79 20 64 69 73 61  unction may disa
263d1 70 70 65 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77  ppear.  Do not w
263d2 72 69 74 65 20 63 6f 64 65 20 74 68 61 74 20 64  rite code that d
263d3 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69  epends.** on thi
263d4 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  s function..**.*
263d5 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
263d6 20 6f 66 20 74 68 65 20 51 55 4f 54 45 28 29 20   of the QUOTE() 
263d7 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20  function.  This 
263d8 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61  function takes a
263d9 20 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d   single.** argum
263da 65 6e 74 2e 20 20 49 66 20 74 68 65 20 61 72 67  ent.  If the arg
263db 75 6d 65 6e 74 20 69 73 20 6e 75 6d 65 72 69 63  ument is numeric
263dc 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
263dd 75 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ue is the same a
263de 73 0a 2a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e  s.** the argumen
263df 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d  t.  If the argum
263e0 65 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  ent is NULL, the
263e1 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
263e2 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22   the string.** "
263e3 4e 55 4c 4c 22 2e 20 20 4f 74 68 65 72 77 69 73  NULL".  Otherwis
263e4 65 2c 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  e, the argument 
263e5 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73  is enclosed in s
263e6 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 77 69 74  ingle quotes wit
263e7 68 0a 2a 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74  h.** single-quot
263e8 65 20 65 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74  e escapes..*/.st
263e9 61 74 69 63 20 76 6f 69 64 20 71 75 6f 74 65 46  atic void quoteF
263ea 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
263eb 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
263ec 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
263ed 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
263ee 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
263ef 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
263f0 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
263f1 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f  switch( sqlite3_
263f2 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
263f3 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20  0]) ){.    case 
263f4 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a  SQLITE_INTEGER:.
263f5 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
263f6 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73  FLOAT: {.      s
263f7 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
263f8 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  lue(context, arg
263f9 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 62 72 65  v[0]);.      bre
263fa 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
263fb 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20  se SQLITE_BLOB: 
263fc 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54  {.      char *zT
263fd 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63  ext = 0;.      c
263fe 68 61 72 20 63 6f 6e 73 74 20 2a 7a 42 6c 6f 62  har const *zBlob
263ff 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
26400 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a  _blob(argv[0]);.
26401 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6f 62 20        int nBlob 
26402 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
26403 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
26404 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 42        assert( zB
26405 6c 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  lob==sqlite3_val
26406 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29  ue_blob(argv[0])
26407 20 29 3b 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69   ); /* No encodi
26408 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20  ng change */.   
26409 20 20 20 7a 54 65 78 74 20 3d 20 28 63 68 61 72     zText = (char
2640a 20 2a 29 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63   *)contextMalloc
2640b 28 63 6f 6e 74 65 78 74 2c 20 28 32 2a 28 69 36  (context, (2*(i6
2640c 34 29 6e 42 6c 6f 62 29 2b 34 29 3b 20 0a 20 20  4)nBlob)+4); .  
2640d 20 20 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b      if( zText ){
2640e 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  .        int i;.
2640f 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
26410 20 69 3c 6e 42 6c 6f 62 3b 20 69 2b 2b 29 7b 0a   i<nBlob; i++){.
26411 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b            zText[
26412 28 69 2a 32 29 2b 32 5d 20 3d 20 68 65 78 64 69  (i*2)+2] = hexdi
26413 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 3e 3e  gits[(zBlob[i]>>
26414 34 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20  4)&0x0F];.      
26415 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b      zText[(i*2)+
26416 33 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28  3] = hexdigits[(
26417 7a 42 6c 6f 62 5b 69 5d 29 26 30 78 30 46 5d 3b  zBlob[i])&0x0F];
26418 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
26419 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a     zText[(nBlob*
2641a 32 29 2b 32 5d 20 3d 20 27 5c 27 27 3b 0a 20 20  2)+2] = '\'';.  
2641b 20 20 20 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c        zText[(nBl
2641c 6f 62 2a 32 29 2b 33 5d 20 3d 20 27 5c 30 27 3b  ob*2)+3] = '\0';
2641d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 30  .        zText[0
2641e 5d 20 3d 20 27 58 27 3b 0a 20 20 20 20 20 20 20  ] = 'X';.       
2641f 20 7a 54 65 78 74 5b 31 5d 20 3d 20 27 5c 27 27   zText[1] = '\''
26420 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
26421 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
26422 6e 74 65 78 74 2c 20 7a 54 65 78 74 2c 20 2d 31  ntext, zText, -1
26423 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
26424 4e 54 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  NT);.        sql
26425 69 74 65 33 5f 66 72 65 65 28 7a 54 65 78 74 29  ite3_free(zText)
26426 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
26427 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
26428 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58   case SQLITE_TEX
26429 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  T: {.      int i
2642a 2c 6a 3b 0a 20 20 20 20 20 20 75 36 34 20 6e 3b  ,j;.      u64 n;
2642b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  .      const uns
2642c 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 72 67  igned char *zArg
2642d 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2642e 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
2642f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a        char *z;..
26430 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 3d 3d        if( zArg==
26431 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
26432 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 30 3b 20    for(i=0, n=0; 
26433 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 20 69  zArg[i]; i++){ i
26434 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27  f( zArg[i]=='\''
26435 20 29 20 6e 2b 2b 3b 20 7d 0a 20 20 20 20 20 20   ) n++; }.      
26436 7a 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  z = contextMallo
26437 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
26438 29 69 29 2b 28 28 69 36 34 29 6e 29 2b 33 29 3b  )i)+((i64)n)+3);
26439 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a  .      if( z ){.
2643a 20 20 20 20 20 20 20 20 7a 5b 30 5d 20 3d 20 27          z[0] = '
2643b 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  \'';.        for
2643c 28 69 3d 30 2c 20 6a 3d 31 3b 20 7a 41 72 67 5b  (i=0, j=1; zArg[
2643d 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
2643e 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 41 72      z[j++] = zAr
2643f 67 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  g[i];.          
26440 69 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27  if( zArg[i]=='\'
26441 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ' ){.           
26442 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a   z[j++] = '\'';.
26443 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
26444 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 5b      }.        z[
26445 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20  j++] = '\'';.   
26446 20 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20       z[j] = 0;. 
26447 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
26448 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
26449 78 74 2c 20 7a 2c 20 6a 2c 20 73 71 6c 69 74 65  xt, z, j, sqlite
2644a 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d  3_free);.      }
2644b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2644c 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
2644d 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
2644e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2644f 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
26450 4c 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20  LITE_NULL );.   
26451 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
26452 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
26453 22 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54  "NULL", 4, SQLIT
26454 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
26455 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
26456 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  }.}../*.** The h
26457 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ex() function.  
26458 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 61 72  Interpret the ar
26459 67 75 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62  gument as a blob
2645a 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68  .  Return.** a h
2645b 65 78 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65  exadecimal rende
2645c 72 69 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f  ring as text..*/
2645d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 78  .static void hex
2645e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
2645f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
26460 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
26461 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
26462 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  argv.){.  int i,
26463 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   n;.  const unsi
26464 67 6e 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62  gned char *pBlob
26465 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20  ;.  char *zHex, 
26466 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  *z;.  assert( ar
26467 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
26468 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
26469 29 3b 0a 20 20 70 42 6c 6f 62 20 3d 20 73 71 6c  );.  pBlob = sql
2646a 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
2646b 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20  argv[0]);.  n = 
2646c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2646d 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
2646e 61 73 73 65 72 74 28 20 70 42 6c 6f 62 3d 3d 73  assert( pBlob==s
2646f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
26470 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f  b(argv[0]) );  /
26471 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68  * No encoding ch
26472 61 6e 67 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48  ange */.  z = zH
26473 65 78 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c  ex = contextMall
26474 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36  oc(context, ((i6
26475 34 29 6e 29 2a 32 20 2b 20 31 29 3b 0a 20 20 69  4)n)*2 + 1);.  i
26476 66 28 20 7a 48 65 78 20 29 7b 0a 20 20 20 20 66  f( zHex ){.    f
26477 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b  or(i=0; i<n; i++
26478 2c 20 70 42 6c 6f 62 2b 2b 29 7b 0a 20 20 20 20  , pBlob++){.    
26479 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
2647a 63 20 3d 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20  c = *pBlob;.    
2647b 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69    *(z++) = hexdi
2647c 67 69 74 73 5b 28 63 3e 3e 34 29 26 30 78 66 5d  gits[(c>>4)&0xf]
2647d 3b 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d  ;.      *(z++) =
2647e 20 68 65 78 64 69 67 69 74 73 5b 63 26 30 78 66   hexdigits[c&0xf
2647f 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 20  ];.    }.    *z 
26480 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
26481 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
26482 74 65 78 74 2c 20 7a 48 65 78 2c 20 6e 2a 32 2c  text, zHex, n*2,
26483 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
26484 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
26485 20 7a 65 72 6f 62 6c 6f 62 28 4e 29 20 66 75 6e   zeroblob(N) fun
26486 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20  ction returns a 
26487 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62  zero-filled blob
26488 20 6f 66 20 73 69 7a 65 20 4e 20 62 79 74 65 73   of size N bytes
26489 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2648a 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 28 0a 20   zeroblobFunc(. 
2648b 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2648c 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
2648d 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
2648e 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
2648f 0a 20 20 69 36 34 20 6e 3b 0a 20 20 73 71 6c 69  .  i64 n;.  sqli
26490 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
26491 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
26492 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
26493 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
26494 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
26495 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e  METER(argc);.  n
26496 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
26497 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b  _int64(argv[0]);
26498 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d  .  testcase( n==
26499 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
2649a 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
2649b 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
2649c 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
2649d 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
2649e 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 3e 64  ]+1 );.  if( n>d
2649f 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
264a0 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
264a1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
264a2 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
264a3 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 65  g(context);.  }e
264a4 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
264a5 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
264a6 28 63 6f 6e 74 65 78 74 2c 20 28 69 6e 74 29 6e  (context, (int)n
264a7 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
264a8 54 68 65 20 72 65 70 6c 61 63 65 28 29 20 66 75  The replace() fu
264a9 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65 65 20 61  nction.  Three a
264aa 72 67 75 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c  rguments are all
264ab 20 73 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a   strings: call.*
264ac 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64  * them A, B, and
264ad 20 43 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69   C. The result i
264ae 73 20 61 6c 73 6f 20 61 20 73 74 72 69 6e 67 20  s also a string 
264af 77 68 69 63 68 20 69 73 20 64 65 72 69 76 65 64  which is derived
264b0 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79 20 72 65  .** from A by re
264b1 70 6c 61 63 69 6e 67 20 65 76 65 72 79 20 6f 63  placing every oc
264b2 63 75 72 61 6e 63 65 20 6f 66 20 42 20 77 69 74  curance of B wit
264b3 68 20 43 2e 20 20 54 68 65 20 6d 61 74 63 68 0a  h C.  The match.
264b4 2a 2a 20 6d 75 73 74 20 62 65 20 65 78 61 63 74  ** must be exact
264b5 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  .  Collating seq
264b6 75 65 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 75  uences are not u
264b7 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  sed..*/.static v
264b8 6f 69 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28  oid replaceFunc(
264b9 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
264ba 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
264bb 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
264bc 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
264bd 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
264be 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 72 3b 20  ned char *zStr; 
264bf 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
264c0 70 75 74 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a  put string A */.
264c1 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
264c2 20 63 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b   char *zPattern;
264c3 20 20 20 20 2f 2a 20 54 68 65 20 70 61 74 74 65      /* The patte
264c4 72 6e 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20  rn string B */. 
264c5 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
264c6 63 68 61 72 20 2a 7a 52 65 70 3b 20 20 20 20 20  char *zRep;     
264c7 20 20 20 2f 2a 20 54 68 65 20 72 65 70 6c 61 63     /* The replac
264c8 65 6d 65 6e 74 20 73 74 72 69 6e 67 20 43 20 2a  ement string C *
264c9 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
264ca 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20  r *zOut;        
264cb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74        /* The out
264cc 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74  put */.  int nSt
264cd 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
264ce 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74    /* Size of zSt
264cf 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74  r */.  int nPatt
264d0 65 72 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ern;            
264d1 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 50 61 74 74  /* Size of zPatt
264d2 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ern */.  int nRe
264d3 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
264d4 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65    /* Size of zRe
264d5 70 20 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b  p */.  i64 nOut;
264d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
264d7 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20  /* Maximum size 
264d8 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74  of zOut */.  int
264d9 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20   loopLimit;     
264da 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53        /* Last zS
264db 74 72 5b 5d 20 74 68 61 74 20 6d 69 67 68 74 20  tr[] that might 
264dc 6d 61 74 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d  match zPattern[]
264dd 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20   */.  int i, j; 
264de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
264df 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
264e0 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  */..  assert( ar
264e1 67 63 3d 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45  gc==3 );.  UNUSE
264e2 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
264e3 29 3b 0a 20 20 7a 53 74 72 20 3d 20 73 71 6c 69  );.  zStr = sqli
264e4 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
264e5 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a  rgv[0]);.  if( z
264e6 53 74 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Str==0 ) return;
264e7 0a 20 20 6e 53 74 72 20 3d 20 73 71 6c 69 74 65  .  nStr = sqlite
264e8 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
264e9 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74  gv[0]);.  assert
264ea 28 20 7a 53 74 72 3d 3d 73 71 6c 69 74 65 33 5f  ( zStr==sqlite3_
264eb 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
264ec 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e  0]) );  /* No en
264ed 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f  coding change */
264ee 0a 20 20 7a 50 61 74 74 65 72 6e 20 3d 20 73 71  .  zPattern = sq
264ef 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
264f0 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28  (argv[1]);.  if(
264f1 20 7a 50 61 74 74 65 72 6e 3d 3d 30 20 29 7b 0a   zPattern==0 ){.
264f2 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
264f3 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
264f4 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[1])==SQLITE_
264f5 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 20 20  NULL.           
264f6 20 7c 7c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74   || sqlite3_cont
264f7 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
264f8 6e 74 65 78 74 29 2d 3e 6d 61 6c 6c 6f 63 46 61  ntext)->mallocFa
264f9 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75  iled );.    retu
264fa 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 50  rn;.  }.  if( zP
264fb 61 74 74 65 72 6e 5b 30 5d 3d 3d 30 20 29 7b 0a  attern[0]==0 ){.
264fc 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
264fd 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
264fe 72 67 76 5b 31 5d 29 21 3d 53 51 4c 49 54 45 5f  rgv[1])!=SQLITE_
264ff 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 73 71 6c 69  NULL );.    sqli
26500 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
26501 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30  (context, argv[0
26502 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ]);.    return;.
26503 20 20 7d 0a 20 20 6e 50 61 74 74 65 72 6e 20 3d    }.  nPattern =
26504 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
26505 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20  ytes(argv[1]);. 
26506 20 61 73 73 65 72 74 28 20 7a 50 61 74 74 65 72   assert( zPatter
26507 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  n==sqlite3_value
26508 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 20 29  _text(argv[1]) )
26509 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e  ;  /* No encodin
2650a 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 52  g change */.  zR
2650b 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ep = sqlite3_val
2650c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29  ue_text(argv[2])
2650d 3b 0a 20 20 69 66 28 20 7a 52 65 70 3d 3d 30 20  ;.  if( zRep==0 
2650e 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 52 65 70  ) return;.  nRep
2650f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
26510 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b  _bytes(argv[2]);
26511 0a 20 20 61 73 73 65 72 74 28 20 7a 52 65 70 3d  .  assert( zRep=
26512 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
26513 65 78 74 28 61 72 67 76 5b 32 5d 29 20 29 3b 0a  ext(argv[2]) );.
26514 20 20 6e 4f 75 74 20 3d 20 6e 53 74 72 20 2b 20    nOut = nStr + 
26515 31 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 75  1;.  assert( nOu
26516 74 3c 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  t<SQLITE_MAX_LEN
26517 47 54 48 20 29 3b 0a 20 20 7a 4f 75 74 20 3d 20  GTH );.  zOut = 
26518 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
26519 6e 74 65 78 74 2c 20 28 69 36 34 29 6e 4f 75 74  ntext, (i64)nOut
2651a 29 3b 0a 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30  );.  if( zOut==0
2651b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
2651c 20 20 7d 0a 20 20 6c 6f 6f 70 4c 69 6d 69 74 20    }.  loopLimit 
2651d 3d 20 6e 53 74 72 20 2d 20 6e 50 61 74 74 65 72  = nStr - nPatter
2651e 6e 3b 20 20 0a 20 20 66 6f 72 28 69 3d 6a 3d 30  n;  .  for(i=j=0
2651f 3b 20 69 3c 3d 6c 6f 6f 70 4c 69 6d 69 74 3b 20  ; i<=loopLimit; 
26520 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 53  i++){.    if( zS
26521 74 72 5b 69 5d 21 3d 7a 50 61 74 74 65 72 6e 5b  tr[i]!=zPattern[
26522 30 5d 20 7c 7c 20 6d 65 6d 63 6d 70 28 26 7a 53  0] || memcmp(&zS
26523 74 72 5b 69 5d 2c 20 7a 50 61 74 74 65 72 6e 2c  tr[i], zPattern,
26524 20 6e 50 61 74 74 65 72 6e 29 20 29 7b 0a 20 20   nPattern) ){.  
26525 20 20 20 20 7a 4f 75 74 5b 6a 2b 2b 5d 20 3d 20      zOut[j++] = 
26526 7a 53 74 72 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c  zStr[i];.    }el
26527 73 65 7b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f  se{.      u8 *zO
26528 6c 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ld;.      sqlite
26529 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
2652a 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2652b 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  e(context);.    
2652c 20 20 6e 4f 75 74 20 2b 3d 20 6e 52 65 70 20 2d    nOut += nRep -
2652d 20 6e 50 61 74 74 65 72 6e 3b 0a 20 20 20 20 20   nPattern;.     
2652e 20 74 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d   testcase( nOut-
2652f 31 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  1==db->aLimit[SQ
26530 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
26531 48 5d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  H] );.      test
26532 63 61 73 65 28 20 6e 4f 75 74 2d 32 3d 3d 64 62  case( nOut-2==db
26533 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
26534 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b  LIMIT_LENGTH] );
26535 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 75 74 2d  .      if( nOut-
26536 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  1>db->aLimit[SQL
26537 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
26538 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ] ){.        sql
26539 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2653a 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74  r_toobig(context
2653b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2653c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 75  e3DbFree(db, zOu
2653d 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  t);.        retu
2653e 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
2653f 20 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20    zOld = zOut;. 
26540 20 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69       zOut = sqli
26541 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74  te3_realloc(zOut
26542 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20  , (int)nOut);.  
26543 20 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20      if( zOut==0 
26544 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
26545 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
26546 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
26547 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
26548 62 46 72 65 65 28 64 62 2c 20 7a 4f 6c 64 29 3b  bFree(db, zOld);
26549 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b  .        return;
2654a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
2654b 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20  emcpy(&zOut[j], 
2654c 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20 20 20  zRep, nRep);.   
2654d 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20 20     j += nRep;.  
2654e 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74 65 72      i += nPatter
2654f 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n-1;.    }.  }. 
26550 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74 72 2d   assert( j+nStr-
26551 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20 6d  i+1==nOut );.  m
26552 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20  emcpy(&zOut[j], 
26553 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72 2d 69  &zStr[i], nStr-i
26554 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 20 2d  );.  j += nStr -
26555 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3c   i;.  assert( j<
26556 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 74 5b  =nOut );.  zOut[
26557 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  j] = 0;.  sqlite
26558 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
26559 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 4f  ntext, (char*)zO
2655a 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66  ut, j, sqlite3_f
2655b 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ree);.}../*.** I
2655c 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2655d 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c 54 52   the TRIM(), LTR
2655e 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49 4d 28  IM(), and RTRIM(
2655f 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20  ) functions..** 
26560 54 68 65 20 75 73 65 72 64 61 74 61 20 69 73 20  The userdata is 
26561 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74 72 69  0x1 for left tri
26562 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67 68 74  m, 0x2 for right
26563 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72 20 62   trim, 0x3 for b
26564 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  oth..*/.static v
26565 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a 20 20  oid trimFunc(.  
26566 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
26567 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
26568 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
26569 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2656a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
2656b 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20   char *zIn;     
2656c 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72      /* Input str
2656d 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75  ing */.  const u
2656e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 43  nsigned char *zC
2656f 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20 53 65  harSet;    /* Se
26570 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  t of characters 
26571 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74  to trim */.  int
26572 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20   nIn;           
26573 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26574 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
26575 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a 20 20  s in input */.  
26576 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
26577 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26578 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 66 74    /* 1: trimleft
26579 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 20 20    2: trimright  
2657a 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74  3: trim */.  int
2657b 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
2657c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2657d 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
2657e 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
2657f 72 20 2a 61 4c 65 6e 20 3d 20 30 3b 20 20 20 20  r *aLen = 0;    
26580 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
26581 6f 66 20 65 61 63 68 20 63 68 61 72 61 63 74 65  of each characte
26582 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f  r in zCharSet */
26583 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
26584 20 2a 2a 61 7a 43 68 61 72 20 3d 20 30 3b 20 20   **azChar = 0;  
26585 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69 64 75       /* Individu
26586 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  al characters in
26587 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 69   zCharSet */.  i
26588 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20 20 20  nt nChar;       
26589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2658a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
2658b 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61  aracters in zCha
2658c 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 73  rSet */..  if( s
2658d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
2658e 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
2658f 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 72  TE_NULL ){.    r
26590 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49 6e  eturn;.  }.  zIn
26591 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
26592 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
26593 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20 72    if( zIn==0 ) r
26594 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20 73  eturn;.  nIn = s
26595 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
26596 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61  es(argv[0]);.  a
26597 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71 6c 69  ssert( zIn==sqli
26598 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
26599 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  rgv[0]) );.  if(
2659a 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20   argc==1 ){.    
2659b 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
2659c 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e 4f 6e  igned char lenOn
2659d 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 20 20  e[] = { 1 };.   
2659e 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64   static unsigned
2659f 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a   char * const az
265a0 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 22  One[] = { (u8*)"
265a1 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61 72 20   " };.    nChar 
265a2 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20 3d 20  = 1;.    aLen = 
265a3 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20 20  (u8*)lenOne;.   
265a4 20 61 7a 43 68 61 72 20 3d 20 28 75 6e 73 69 67   azChar = (unsig
265a5 6e 65 64 20 63 68 61 72 20 2a 2a 29 61 7a 4f 6e  ned char **)azOn
265a6 65 3b 0a 20 20 20 20 7a 43 68 61 72 53 65 74 20  e;.    zCharSet 
265a7 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
265a8 20 28 7a 43 68 61 72 53 65 74 20 3d 20 73 71 6c   (zCharSet = sql
265a9 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
265aa 61 72 67 76 5b 31 5d 29 29 3d 3d 30 20 29 7b 0a  argv[1]))==0 ){.
265ab 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65      return;.  }e
265ac 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75  lse{.    const u
265ad 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b  nsigned char *z;
265ae 0a 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72  .    for(z=zChar
265af 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a  Set, nChar=0; *z
265b0 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20  ; nChar++){.    
265b1 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
265b2 46 38 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20  F8(z);.    }.   
265b3 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a   if( nChar>0 ){.
265b4 20 20 20 20 20 20 61 7a 43 68 61 72 20 3d 20 63        azChar = c
265b5 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
265b6 74 65 78 74 2c 20 28 28 69 36 34 29 6e 43 68 61  text, ((i64)nCha
265b7 72 29 2a 28 73 69 7a 65 6f 66 28 63 68 61 72 2a  r)*(sizeof(char*
265b8 29 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 66 28  )+1));.      if(
265b9 20 61 7a 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20   azChar==0 ){.  
265ba 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
265bb 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4c 65 6e      }.      aLen
265bc 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
265bd 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68 61 72  r*)&azChar[nChar
265be 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 7a 3d 7a  ];.      for(z=z
265bf 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d 30  CharSet, nChar=0
265c0 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a  ; *z; nChar++){.
265c1 20 20 20 20 20 20 20 20 61 7a 43 68 61 72 5b 6e          azChar[n
265c2 43 68 61 72 5d 20 3d 20 28 75 6e 73 69 67 6e 65  Char] = (unsigne
265c3 64 20 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20  d char *)z;.    
265c4 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
265c5 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 20  UTF8(z);.       
265c6 20 61 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d 20 28   aLen[nChar] = (
265c7 75 38 29 28 7a 20 2d 20 61 7a 43 68 61 72 5b 6e  u8)(z - azChar[n
265c8 43 68 61 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  Char]);.      }.
265c9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
265ca 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 66  nChar>0 ){.    f
265cb 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 50 54  lags = SQLITE_PT
265cc 52 5f 54 4f 5f 49 4e 54 28 73 71 6c 69 74 65 33  R_TO_INT(sqlite3
265cd 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
265ce 78 74 29 29 3b 0a 20 20 20 20 69 66 28 20 66 6c  xt));.    if( fl
265cf 61 67 73 20 26 20 31 20 29 7b 0a 20 20 20 20 20  ags & 1 ){.     
265d0 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b   while( nIn>0 ){
265d1 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 65 6e  .        int len
265d2 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f   = 0;.        fo
265d3 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20  r(i=0; i<nChar; 
265d4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
265d5 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20  len = aLen[i];. 
265d6 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e           if( len
265d7 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28  <=nIn && memcmp(
265d8 7a 49 6e 2c 20 61 7a 43 68 61 72 5b 69 5d 2c 20  zIn, azChar[i], 
265d9 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b  len)==0 ) break;
265da 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
265db 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72 20     if( i>=nChar 
265dc 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
265dd 20 7a 49 6e 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20   zIn += len;.   
265de 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b       nIn -= len;
265df 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
265e0 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 32     if( flags & 2
265e1 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   ){.      while(
265e2 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20   nIn>0 ){.      
265e3 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20    int len = 0;. 
265e4 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
265e5 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20  i<nChar; i++){. 
265e6 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61           len = a
265e7 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  Len[i];.        
265e8 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26    if( len<=nIn &
265e9 26 20 6d 65 6d 63 6d 70 28 26 7a 49 6e 5b 6e 49  & memcmp(&zIn[nI
265ea 6e 2d 6c 65 6e 5d 2c 61 7a 43 68 61 72 5b 69 5d  n-len],azChar[i]
265eb 2c 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b  ,len)==0 ) break
265ec 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
265ed 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72      if( i>=nChar
265ee 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
265ef 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20    nIn -= len;.  
265f0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
265f1 69 66 28 20 7a 43 68 61 72 53 65 74 20 29 7b 0a  if( zCharSet ){.
265f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
265f3 65 65 28 61 7a 43 68 61 72 29 3b 0a 20 20 20 20  ee(azChar);.    
265f4 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
265f5 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
265f6 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 49 6e 2c  ext, (char*)zIn,
265f7 20 6e 49 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41   nIn, SQLITE_TRA
265f8 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a 0a 23 69 66  NSIENT);.}...#if
265f9 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44  def SQLITE_SOUND
265fa 45 58 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  EX./*.** Compute
265fb 20 74 68 65 20 73 6f 75 6e 64 65 78 20 65 6e 63   the soundex enc
265fc 6f 64 69 6e 67 20 6f 66 20 61 20 77 6f 72 64 2e  oding of a word.
265fd 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
265fe 73 6f 75 6e 64 65 78 46 75 6e 63 28 0a 20 20 73  soundexFunc(.  s
265ff 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
26600 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
26601 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
26602 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
26603 20 63 68 61 72 20 7a 52 65 73 75 6c 74 5b 38 5d   char zResult[8]
26604 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 49  ;.  const u8 *zI
26605 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  n;.  int i, j;. 
26606 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
26607 73 69 67 6e 65 64 20 63 68 61 72 20 69 43 6f 64  signed char iCod
26608 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 30  e[] = {.    0, 0
26609 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2660a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2660b 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20  0, 0, 0, 0,.    
2660c 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2660d 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2660e 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
2660f 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
26610 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
26611 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
26612 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c   0,.    0, 0, 0,
26613 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
26614 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
26615 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30  , 0, 0,.    0, 0
26616 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c  , 1, 2, 3, 0, 1,
26617 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20   2, 0, 0, 2, 2, 
26618 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20  4, 5, 5, 0,.    
26619 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30  1, 2, 6, 2, 3, 0
2661a 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c  , 1, 0, 2, 0, 2,
2661b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
2661c 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20      0, 0, 1, 2, 
2661d 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30  3, 0, 1, 2, 0, 0
2661e 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c  , 2, 2, 4, 5, 5,
2661f 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c   0,.    1, 2, 6,
26620 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20   2, 3, 0, 1, 0, 
26621 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30  2, 0, 2, 0, 0, 0
26622 2c 20 30 2c 20 30 2c 0a 20 20 7d 3b 0a 20 20 61  , 0, 0,.  };.  a
26623 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
26624 3b 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 73  ;.  zIn = (u8*)s
26625 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
26626 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66  t(argv[0]);.  if
26627 28 20 7a 49 6e 3d 3d 30 20 29 20 7a 49 6e 20 3d  ( zIn==0 ) zIn =
26628 20 28 75 38 2a 29 22 22 3b 0a 20 20 66 6f 72 28   (u8*)"";.  for(
26629 69 3d 30 3b 20 7a 49 6e 5b 69 5d 20 26 26 20 21  i=0; zIn[i] && !
2662a 73 71 6c 69 74 65 33 49 73 61 6c 70 68 61 28 7a  sqlite3Isalpha(z
2662b 49 6e 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  In[i]); i++){}. 
2662c 20 69 66 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20   if( zIn[i] ){. 
2662d 20 20 20 75 38 20 70 72 65 76 63 6f 64 65 20 3d     u8 prevcode =
2662e 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78   iCode[zIn[i]&0x
2662f 37 66 5d 3b 0a 20 20 20 20 7a 52 65 73 75 6c 74  7f];.    zResult
26630 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 54 6f 75  [0] = sqlite3Tou
26631 70 70 65 72 28 7a 49 6e 5b 69 5d 29 3b 0a 20 20  pper(zIn[i]);.  
26632 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 34 20 26    for(j=1; j<4 &
26633 26 20 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  & zIn[i]; i++){.
26634 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d        int code =
26635 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78   iCode[zIn[i]&0x
26636 37 66 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 63  7f];.      if( c
26637 6f 64 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ode>0 ){.       
26638 20 69 66 28 20 63 6f 64 65 21 3d 70 72 65 76 63   if( code!=prevc
26639 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ode ){.         
2663a 20 70 72 65 76 63 6f 64 65 20 3d 20 63 6f 64 65   prevcode = code
2663b 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 52 65 73  ;.          zRes
2663c 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 63 6f 64 65 20  ult[j++] = code 
2663d 2b 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d  + '0';.        }
2663e 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2663f 20 20 20 20 20 20 70 72 65 76 63 6f 64 65 20 3d        prevcode =
26640 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
26641 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 6a 3c 34  }.    while( j<4
26642 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 73 75 6c   ){.      zResul
26643 74 5b 6a 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20  t[j++] = '0';.  
26644 20 20 7d 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b    }.    zResult[
26645 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  j] = 0;.    sqli
26646 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
26647 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75 6c 74  context, zResult
26648 2c 20 34 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  , 4, SQLITE_TRAN
26649 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b  SIENT);.  }else{
2664a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2664b 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
2664c 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20 53 51 4c  , "?000", 4, SQL
2664d 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d  ITE_STATIC);.  }
2664e 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
2664f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
26650 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a  OAD_EXTENSION./*
26651 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e 20 74  .** A function t
26652 68 61 74 20 6c 6f 61 64 73 20 61 20 73 68 61 72  hat loads a shar
26653 65 64 2d 6c 69 62 72 61 72 79 20 65 78 74 65 6e  ed-library exten
26654 73 69 6f 6e 20 74 68 65 6e 20 72 65 74 75 72 6e  sion then return
26655 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69  s NULL..*/.stati
26656 63 20 76 6f 69 64 20 6c 6f 61 64 45 78 74 28 73  c void loadExt(s
26657 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
26658 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
26659 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
2665a 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 6f 6e 73   **argv){.  cons
2665b 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 20 3d 20  t char *zFile = 
2665c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
2665d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2665e 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e  (argv[0]);.  con
2665f 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 3b 0a  st char *zProc;.
26660 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
26661 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
26662 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
26663 74 29 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  t);.  char *zErr
26664 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  Msg = 0;..  if( 
26665 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 7a  argc==2 ){.    z
26666 50 72 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68  Proc = (const ch
26667 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
26668 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
26669 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
2666a 50 72 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Proc = 0;.  }.  
2666b 69 66 28 20 7a 46 69 6c 65 20 26 26 20 73 71 6c  if( zFile && sql
2666c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  ite3_load_extens
2666d 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a  ion(db, zFile, z
2666e 50 72 6f 63 2c 20 26 7a 45 72 72 4d 73 67 29 20  Proc, &zErrMsg) 
2666f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
26670 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
26671 65 78 74 2c 20 7a 45 72 72 4d 73 67 2c 20 2d 31  ext, zErrMsg, -1
26672 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
26673 72 65 65 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20  ree(zErrMsg);.  
26674 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  }.}.#endif.../*.
26675 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
26676 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
26677 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20  structure holds 
26678 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61  the context of a
26679 0a 2a 2a 20 73 75 6d 28 29 20 6f 72 20 61 76 67  .** sum() or avg
2667a 28 29 20 61 67 67 72 65 67 61 74 65 20 63 6f 6d  () aggregate com
2667b 70 75 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  putation..*/.typ
2667c 65 64 65 66 20 73 74 72 75 63 74 20 53 75 6d 43  edef struct SumC
2667d 74 78 20 53 75 6d 43 74 78 3b 0a 73 74 72 75 63  tx SumCtx;.struc
2667e 74 20 53 75 6d 43 74 78 20 7b 0a 20 20 64 6f 75  t SumCtx {.  dou
2667f 62 6c 65 20 72 53 75 6d 3b 20 20 20 20 20 20 2f  ble rSum;      /
26680 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  * Floating point
26681 20 73 75 6d 20 2a 2f 0a 20 20 69 36 34 20 69 53   sum */.  i64 iS
26682 75 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49  um;         /* I
26683 6e 74 65 67 65 72 20 73 75 6d 20 2a 2f 20 20 20  nteger sum */   
26684 0a 20 20 69 36 34 20 63 6e 74 3b 20 20 20 20 20  .  i64 cnt;     
26685 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
26686 66 20 65 6c 65 6d 65 6e 74 73 20 73 75 6d 6d 65  f elements summe
26687 64 20 2a 2f 0a 20 20 75 38 20 6f 76 65 72 66 6c  d */.  u8 overfl
26688 6f 77 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ow;      /* True
26689 20 69 66 20 69 6e 74 65 67 65 72 20 6f 76 65 72   if integer over
2668a 66 6c 6f 77 20 73 65 65 6e 20 2a 2f 0a 20 20 75  flow seen */.  u
2668b 38 20 61 70 70 72 6f 78 3b 20 20 20 20 20 20 20  8 approx;       
2668c 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 6e 2d   /* True if non-
2668d 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 61  integer value wa
2668e 73 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 73  s input to the s
2668f 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  um */.};../*.** 
26690 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f  Routines used to
26691 20 63 6f 6d 70 75 74 65 20 74 68 65 20 73 75 6d   compute the sum
26692 2c 20 61 76 65 72 61 67 65 2c 20 61 6e 64 20 74  , average, and t
26693 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  otal..**.** The 
26694 53 55 4d 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  SUM() function f
26695 6f 6c 6c 6f 77 73 20 74 68 65 20 28 62 72 6f 6b  ollows the (brok
26696 65 6e 29 20 53 51 4c 20 73 74 61 6e 64 61 72 64  en) SQL standard
26697 20 77 68 69 63 68 20 6d 65 61 6e 73 0a 2a 2a 20   which means.** 
26698 74 68 61 74 20 69 74 20 72 65 74 75 72 6e 73 20  that it returns 
26699 4e 55 4c 4c 20 69 66 20 69 74 20 73 75 6d 73 20  NULL if it sums 
2669a 6f 76 65 72 20 6e 6f 20 69 6e 70 75 74 73 2e 20  over no inputs. 
2669b 20 54 4f 54 41 4c 20 72 65 74 75 72 6e 73 0a 2a   TOTAL returns.*
2669c 2a 20 30 2e 30 20 69 6e 20 74 68 61 74 20 63 61  * 0.0 in that ca
2669d 73 65 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e  se.  In addition
2669e 2c 20 54 4f 54 41 4c 20 61 6c 77 61 79 73 20 72  , TOTAL always r
2669f 65 74 75 72 6e 73 20 61 20 66 6c 6f 61 74 20 77  eturns a float w
266a0 68 65 72 65 0a 2a 2a 20 53 55 4d 20 6d 69 67 68  here.** SUM migh
266a1 74 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65  t return an inte
266a2 67 65 72 20 69 66 20 69 74 20 6e 65 76 65 72 20  ger if it never 
266a3 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 66 6c 6f  encounters a flo
266a4 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76  ating point.** v
266a5 61 6c 75 65 2e 20 20 54 4f 54 41 4c 20 6e 65 76  alue.  TOTAL nev
266a6 65 72 20 66 61 69 6c 73 2c 20 62 75 74 20 53 55  er fails, but SU
266a7 4d 20 6d 69 67 68 74 20 74 68 72 6f 75 67 68 20  M might through 
266a8 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66 0a  an exception if.
266a9 2a 2a 20 69 74 20 6f 76 65 72 66 6c 6f 77 73 20  ** it overflows 
266aa 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73  an integer..*/.s
266ab 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d 53 74  tatic void sumSt
266ac 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ep(sqlite3_conte
266ad 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
266ae 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
266af 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
266b0 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 69 6e 74  SumCtx *p;.  int
266b1 20 74 79 70 65 3b 0a 20 20 61 73 73 65 72 74 28   type;.  assert(
266b2 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e   argc==1 );.  UN
266b3 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
266b4 72 67 63 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69  rgc);.  p = sqli
266b5 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
266b6 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
266b7 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 74 79  izeof(*p));.  ty
266b8 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pe = sqlite3_val
266b9 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
266ba 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
266bb 70 20 26 26 20 74 79 70 65 21 3d 53 51 4c 49 54  p && type!=SQLIT
266bc 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 2d  E_NULL ){.    p-
266bd 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20  >cnt++;.    if( 
266be 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54  type==SQLITE_INT
266bf 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 69 36  EGER ){.      i6
266c0 34 20 76 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  4 v = sqlite3_va
266c1 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
266c2 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75  ]);.      p->rSu
266c3 6d 20 2b 3d 20 76 3b 0a 20 20 20 20 20 20 69 66  m += v;.      if
266c4 28 20 28 70 2d 3e 61 70 70 72 6f 78 7c 70 2d 3e  ( (p->approx|p->
266c5 6f 76 65 72 66 6c 6f 77 29 3d 3d 30 20 29 7b 0a  overflow)==0 ){.
266c6 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65 77          i64 iNew
266c7 53 75 6d 20 3d 20 70 2d 3e 69 53 75 6d 20 2b 20  Sum = p->iSum + 
266c8 76 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73  v;.        int s
266c9 31 20 3d 20 28 69 6e 74 29 28 70 2d 3e 69 53 75  1 = (int)(p->iSu
266ca 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34  m >> (sizeof(i64
266cb 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20 20  )*8-1));.       
266cc 20 69 6e 74 20 73 32 20 3d 20 28 69 6e 74 29 28   int s2 = (int)(
266cd 76 20 20 20 20 20 20 20 3e 3e 20 28 73 69 7a 65  v       >> (size
266ce 6f 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20  of(i64)*8-1));. 
266cf 20 20 20 20 20 20 20 69 6e 74 20 73 33 20 3d 20         int s3 = 
266d0 28 69 6e 74 29 28 69 4e 65 77 53 75 6d 20 3e 3e  (int)(iNewSum >>
266d1 20 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d   (sizeof(i64)*8-
266d2 31 29 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  1));.        p->
266d3 6f 76 65 72 66 6c 6f 77 20 3d 20 28 28 73 31 26  overflow = ((s1&
266d4 73 32 26 7e 73 33 29 20 7c 20 28 7e 73 31 26 7e  s2&~s3) | (~s1&~
266d5 73 32 26 73 33 29 29 3f 31 3a 30 3b 0a 20 20 20  s2&s3))?1:0;.   
266d6 20 20 20 20 20 70 2d 3e 69 53 75 6d 20 3d 20 69       p->iSum = i
266d7 4e 65 77 53 75 6d 3b 0a 20 20 20 20 20 20 7d 0a  NewSum;.      }.
266d8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
266d9 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69   p->rSum += sqli
266da 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
266db 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
266dc 20 70 2d 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a   p->approx = 1;.
266dd 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74      }.  }.}.stat
266de 69 63 20 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c  ic void sumFinal
266df 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ize(sqlite3_cont
266e0 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
266e1 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20   SumCtx *p;.  p 
266e2 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  = sqlite3_aggreg
266e3 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
266e4 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ext, 0);.  if( p
266e5 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a   && p->cnt>0 ){.
266e6 20 20 20 20 69 66 28 20 70 2d 3e 6f 76 65 72 66      if( p->overf
266e7 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  low ){.      sql
266e8 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
266e9 72 28 63 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67  r(context,"integ
266ea 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29  er overflow",-1)
266eb 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
266ec 70 2d 3e 61 70 70 72 6f 78 20 29 7b 0a 20 20 20  p->approx ){.   
266ed 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
266ee 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
266ef 2c 20 70 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20  , p->rSum);.    
266f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
266f1 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
266f2 34 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53  4(context, p->iS
266f3 75 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  um);.    }.  }.}
266f4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 76 67  .static void avg
266f5 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  Finalize(sqlite3
266f6 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
266f7 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b  t){.  SumCtx *p;
266f8 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61  .  p = sqlite3_a
266f9 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
266fa 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
266fb 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e  if( p && p->cnt>
266fc 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
266fd 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63  _result_double(c
266fe 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f  ontext, p->rSum/
266ff 28 64 6f 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b  (double)p->cnt);
26700 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
26701 69 64 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65  id totalFinalize
26702 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
26703 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75   *context){.  Su
26704 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73  mCtx *p;.  p = s
26705 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
26706 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
26707 2c 20 30 29 3b 0a 20 20 2f 2a 20 28 64 6f 75 62  , 0);.  /* (doub
26708 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20  le)0 In case of 
26709 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
2670a 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f  TING_POINT... */
2670b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
2670c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
2670d 2c 20 70 20 3f 20 70 2d 3e 72 53 75 6d 20 3a 20  , p ? p->rSum : 
2670e 28 64 6f 75 62 6c 65 29 30 29 3b 0a 7d 0a 0a 2f  (double)0);.}../
2670f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
26710 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  ng structure kee
26711 70 73 20 74 72 61 63 6b 20 6f 66 20 73 74 61 74  ps track of stat
26712 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  e information fo
26713 72 20 74 68 65 0a 2a 2a 20 63 6f 75 6e 74 28 29  r the.** count()
26714 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
26715 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ion..*/.typedef 
26716 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20  struct CountCtx 
26717 43 6f 75 6e 74 43 74 78 3b 0a 73 74 72 75 63 74  CountCtx;.struct
26718 20 43 6f 75 6e 74 43 74 78 20 7b 0a 20 20 69 36   CountCtx {.  i6
26719 34 20 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52  4 n;.};../*.** R
2671a 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65  outines to imple
2671b 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 28 29  ment the count()
2671c 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
2671d 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
2671e 6f 69 64 20 63 6f 75 6e 74 53 74 65 70 28 73 71  oid countStep(sq
2671f 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26720 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
26721 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
26722 2a 2a 61 72 67 76 29 7b 0a 20 20 43 6f 75 6e 74  **argv){.  Count
26723 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71  Ctx *p;.  p = sq
26724 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
26725 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
26726 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20   sizeof(*p));.  
26727 69 66 28 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20  if( (argc==0 || 
26728 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c  SQLITE_NULL!=sql
26729 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2672a 61 72 67 76 5b 30 5d 29 29 20 26 26 20 70 20 29  argv[0])) && p )
2672b 7b 0a 20 20 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20  {.    p->n++;.  
2672c 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2672d 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
2672e 44 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74  D.  /* The sqlit
2672f 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
26730 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  nt() function is
26731 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 42 75   deprecated.  Bu
26732 74 20 6a 75 73 74 20 74 6f 20 6d 61 6b 65 0a 20  t just to make. 
26733 20 2a 2a 20 73 75 72 65 20 69 74 20 73 74 69 6c   ** sure it stil
26734 6c 20 6f 70 65 72 61 74 65 73 20 63 6f 72 72 65  l operates corre
26735 63 74 6c 79 2c 20 76 65 72 69 66 79 20 74 68 61  ctly, verify tha
26736 74 20 69 74 73 20 63 6f 75 6e 74 20 61 67 72 65  t its count agre
26737 65 73 20 77 69 74 68 20 6f 75 72 20 0a 20 20 2a  es with our .  *
26738 2a 20 69 6e 74 65 72 6e 61 6c 20 63 6f 75 6e 74  * internal count
26739 20 77 68 65 6e 20 75 73 69 6e 67 20 63 6f 75 6e   when using coun
2673a 74 28 2a 29 20 61 6e 64 20 77 68 65 6e 20 74 68  t(*) and when th
2673b 65 20 74 6f 74 61 6c 20 63 6f 75 6e 74 20 63 61  e total count ca
2673c 6e 20 62 65 0a 20 20 2a 2a 20 65 78 70 72 65 73  n be.  ** expres
2673d 73 65 64 20 61 73 20 61 20 33 32 2d 62 69 74 20  sed as a 32-bit 
2673e 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 61 73  integer. */.  as
2673f 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c  sert( argc==1 ||
26740 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 3e 30 78   p==0 || p->n>0x
26741 37 66 66 66 66 66 66 66 0a 20 20 20 20 20 20 20  7fffffff.       
26742 20 20 20 7c 7c 20 70 2d 3e 6e 3d 3d 73 71 6c 69     || p->n==sqli
26743 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
26744 75 6e 74 28 63 6f 6e 74 65 78 74 29 20 29 3b 0a  unt(context) );.
26745 23 65 6e 64 69 66 0a 7d 20 20 20 0a 73 74 61 74  #endif.}   .stat
26746 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e  ic void countFin
26747 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
26748 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
26749 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a  .  CountCtx *p;.
2674a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67    p = sqlite3_ag
2674b 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
2674c 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73  context, 0);.  s
2674d 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
2674e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f  t64(context, p ?
2674f 20 70 2d 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f   p->n : 0);.}../
26750 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f  *.** Routines to
26751 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29   implement min()
26752 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65   and max() aggre
26753 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  gate functions..
26754 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
26755 69 6e 6d 61 78 53 74 65 70 28 0a 20 20 73 71 6c  inmaxStep(.  sql
26756 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
26757 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f  ntext, .  int No
26758 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65  tUsed, .  sqlite
26759 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
2675a 7b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 20 20 3d  {.  Mem *pArg  =
2675b 20 28 4d 65 6d 20 2a 29 61 72 67 76 5b 30 5d 3b   (Mem *)argv[0];
2675c 0a 20 20 4d 65 6d 20 2a 70 42 65 73 74 3b 0a 20  .  Mem *pBest;. 
2675d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2675e 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69  R(NotUsed);..  i
2675f 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
26760 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d  _type(argv[0])==
26761 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65  SQLITE_NULL ) re
26762 74 75 72 6e 3b 0a 20 20 70 42 65 73 74 20 3d 20  turn;.  pBest = 
26763 28 4d 65 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61  (Mem *)sqlite3_a
26764 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
26765 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66  (context, sizeof
26766 28 2a 70 42 65 73 74 29 29 3b 0a 20 20 69 66 28  (*pBest));.  if(
26767 20 21 70 42 65 73 74 20 29 20 72 65 74 75 72 6e   !pBest ) return
26768 3b 0a 0a 20 20 69 66 28 20 70 42 65 73 74 2d 3e  ;..  if( pBest->
26769 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 6e 74  flags ){.    int
2676a 20 6d 61 78 3b 0a 20 20 20 20 69 6e 74 20 63 6d   max;.    int cm
2676b 70 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  p;.    CollSeq *
2676c 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
2676d 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f  etFuncCollSeq(co
2676e 6e 74 65 78 74 29 3b 0a 20 20 20 20 2f 2a 20 54  ntext);.    /* T
2676f 68 69 73 20 73 74 65 70 20 66 75 6e 63 74 69 6f  his step functio
26770 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20 62 6f  n is used for bo
26771 74 68 20 74 68 65 20 6d 69 6e 28 29 20 61 6e 64  th the min() and
26772 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 74 65   max() aggregate
26773 73 2c 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 6e  s,.    ** the on
26774 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  ly difference be
26775 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 62 65  tween the two be
26776 69 6e 67 20 74 68 61 74 20 74 68 65 20 73 65 6e  ing that the sen
26777 73 65 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  se of the.    **
26778 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 69   comparison is i
26779 6e 76 65 72 74 65 64 2e 20 46 6f 72 20 74 68 65  nverted. For the
2677a 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 74 65   max() aggregate
2677b 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c  , the.    ** sql
2677c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29  ite3_user_data()
2677d 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2677e 73 20 28 76 6f 69 64 20 2a 29 2d 31 2e 20 46 6f  s (void *)-1. Fo
2677f 72 20 6d 69 6e 28 29 20 69 74 0a 20 20 20 20 2a  r min() it.    *
26780 2a 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64 20  * returns (void 
26781 2a 29 64 62 2c 20 77 68 65 72 65 20 64 62 20 69  *)db, where db i
26782 73 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64  s the sqlite3* d
26783 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72 2e  atabase pointer.
26784 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 66 6f 72  .    ** Therefor
26785 65 20 74 68 65 20 6e 65 78 74 20 73 74 61 74 65  e the next state
26786 6d 65 6e 74 20 73 65 74 73 20 76 61 72 69 61 62  ment sets variab
26787 6c 65 20 27 6d 61 78 27 20 74 6f 20 31 20 66 6f  le 'max' to 1 fo
26788 72 20 74 68 65 20 6d 61 78 28 29 0a 20 20 20 20  r the max().    
26789 2a 2a 20 61 67 67 72 65 67 61 74 65 2c 20 6f 72  ** aggregate, or
2678a 20 30 20 66 6f 72 20 6d 69 6e 28 29 2e 0a 20 20   0 for min()..  
2678b 20 20 2a 2f 0a 20 20 20 20 6d 61 78 20 3d 20 73    */.    max = s
2678c 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
2678d 28 63 6f 6e 74 65 78 74 29 21 3d 30 3b 0a 20 20  (context)!=0;.  
2678e 20 20 63 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d    cmp = sqlite3M
2678f 65 6d 43 6f 6d 70 61 72 65 28 70 42 65 73 74 2c  emCompare(pBest,
26790 20 70 41 72 67 2c 20 70 43 6f 6c 6c 29 3b 0a 20   pArg, pColl);. 
26791 20 20 20 69 66 28 20 28 6d 61 78 20 26 26 20 63     if( (max && c
26792 6d 70 3c 30 29 20 7c 7c 20 28 21 6d 61 78 20 26  mp<0) || (!max &
26793 26 20 63 6d 70 3e 30 29 20 29 7b 0a 20 20 20 20  & cmp>0) ){.    
26794 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
26795 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72 67  Copy(pBest, pArg
26796 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
26797 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
26798 65 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c 20  eMemCopy(pBest, 
26799 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  pArg);.  }.}.sta
2679a 74 69 63 20 76 6f 69 64 20 6d 69 6e 4d 61 78 46  tic void minMaxF
2679b 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
2679c 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2679d 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ){.  sqlite3_val
2679e 75 65 20 2a 70 52 65 73 3b 0a 20 20 70 52 65 73  ue *pRes;.  pRes
2679f 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75   = (sqlite3_valu
267a0 65 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72  e *)sqlite3_aggr
267a1 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
267a2 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28  ntext, 0);.  if(
267a3 20 70 52 65 73 20 29 7b 0a 20 20 20 20 69 66 28   pRes ){.    if(
267a4 20 41 4c 57 41 59 53 28 70 52 65 73 2d 3e 66 6c   ALWAYS(pRes->fl
267a5 61 67 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71  ags) ){.      sq
267a6 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
267a7 75 65 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 73  ue(context, pRes
267a8 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
267a9 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
267aa 73 65 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a  se(pRes);.  }.}.
267ab 0a 2f 2a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e  ./*.** group_con
267ac 63 61 74 28 45 58 50 52 2c 20 3f 53 45 50 41 52  cat(EXPR, ?SEPAR
267ad 41 54 4f 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63  ATOR?).*/.static
267ae 20 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61   void groupConca
267af 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33  tStep(.  sqlite3
267b0 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
267b1 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
267b2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
267b3 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
267b4 20 63 68 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53   char *zVal;.  S
267b5 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b  trAccum *pAccum;
267b6 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
267b7 53 65 70 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c  Sep;.  int nVal,
267b8 20 6e 53 65 70 3b 0a 20 20 61 73 73 65 72 74 28   nSep;.  assert(
267b9 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63   argc==1 || argc
267ba 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c  ==2 );.  if( sql
267bb 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
267bc 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
267bd 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a  _NULL ) return;.
267be 20 20 70 41 63 63 75 6d 20 3d 20 28 53 74 72 41    pAccum = (StrA
267bf 63 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67  ccum*)sqlite3_ag
267c0 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
267c1 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28  context, sizeof(
267c2 2a 70 41 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66  *pAccum));..  if
267c3 28 20 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20  ( pAccum ){.    
267c4 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
267c5 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
267c6 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
267c7 3b 0a 20 20 20 20 69 6e 74 20 66 69 72 73 74 54  ;.    int firstT
267c8 65 72 6d 20 3d 20 70 41 63 63 75 6d 2d 3e 75 73  erm = pAccum->us
267c9 65 4d 61 6c 6c 6f 63 3d 3d 30 3b 0a 20 20 20 20  eMalloc==0;.    
267ca 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f  pAccum->useMallo
267cb 63 20 3d 20 31 3b 0a 20 20 20 20 70 41 63 63 75  c = 1;.    pAccu
267cc 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 2d  m->mxAlloc = db-
267cd 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
267ce 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20  IMIT_LENGTH];.  
267cf 20 20 69 66 28 20 21 66 69 72 73 74 54 65 72 6d    if( !firstTerm
267d0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72   ){.      if( ar
267d1 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20  gc==2 ){.       
267d2 20 7a 53 65 70 20 3d 20 28 63 68 61 72 2a 29 73   zSep = (char*)s
267d3 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
267d4 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  t(argv[1]);.    
267d5 20 20 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74      nSep = sqlit
267d6 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
267d7 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d  rgv[1]);.      }
267d8 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53  else{.        zS
267d9 65 70 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20  ep = ",";.      
267da 20 20 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20    nSep = 1;.    
267db 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
267dc 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
267dd 70 41 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53  pAccum, zSep, nS
267de 65 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  ep);.    }.    z
267df 56 61 6c 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  Val = (char*)sql
267e0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
267e1 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 56  argv[0]);.    nV
267e2 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
267e3 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
267e4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  );.    sqlite3St
267e5 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
267e6 63 75 6d 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29  cum, zVal, nVal)
267e7 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
267e8 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 46  oid groupConcatF
267e9 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
267ea 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
267eb 29 7b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70  ){.  StrAccum *p
267ec 41 63 63 75 6d 3b 0a 20 20 70 41 63 63 75 6d 20  Accum;.  pAccum 
267ed 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  = sqlite3_aggreg
267ee 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
267ef 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ext, 0);.  if( p
267f0 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 69 66 28  Accum ){.    if(
267f1 20 70 41 63 63 75 6d 2d 3e 74 6f 6f 42 69 67 20   pAccum->tooBig 
267f2 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
267f3 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
267f4 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20  obig(context);. 
267f5 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 63     }else if( pAc
267f6 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  cum->mallocFaile
267f7 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  d ){.      sqlit
267f8 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
267f9 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
267fa 20 20 20 20 7d 65 6c 73 65 7b 20 20 20 20 0a 20      }else{    . 
267fb 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
267fc 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
267fd 2c 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  , sqlite3StrAccu
267fe 6d 46 69 6e 69 73 68 28 70 41 63 63 75 6d 29 2c  mFinish(pAccum),
267ff 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20   -1, .          
26800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26801 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
26802 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
26803 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
26804 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c 20 6f  registered all o
26805 66 20 74 68 65 20 61 62 6f 76 65 20 43 20 66 75  f the above C fu
26806 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a 2a  nctions as SQL.*
26807 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  * functions.  Th
26808 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  is should be the
26809 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e   only routine in
2680a 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 0a   this file with.
2680b 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b  ** external link
2680c 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  age..*/.SQLITE_P
2680d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2680e 74 65 33 52 65 67 69 73 74 65 72 42 75 69 6c 74  te3RegisterBuilt
2680f 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  inFunctions(sqli
26810 74 65 33 20 2a 64 62 29 7b 0a 23 69 66 6e 64 65  te3 *db){.#ifnde
26811 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c  f SQLITE_OMIT_AL
26812 54 45 52 54 41 42 4c 45 0a 20 20 73 71 6c 69 74  TERTABLE.  sqlit
26813 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73  e3AlterFunctions
26814 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69  (db);.#endif.  i
26815 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
26816 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 6e 74 20  iled ){.    int 
26817 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65  rc = sqlite3_ove
26818 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64  rload_function(d
26819 62 2c 20 22 4d 41 54 43 48 22 2c 20 32 29 3b 0a  b, "MATCH", 2);.
2681a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
2681b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
2681c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
2681d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
2681e 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ITE_NOMEM ){.   
2681f 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
26820 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  led = 1;.    }. 
26821 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20   }.}../*.** Set 
26822 74 68 65 20 4c 49 4b 45 4f 50 54 20 66 6c 61 67  the LIKEOPT flag
26823 20 6f 6e 20 74 68 65 20 32 2d 61 72 67 75 6d 65   on the 2-argume
26824 6e 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  nt function with
26825 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e   the given name.
26826 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
26827 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 73  setLikeOptFlag(s
26828 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
26829 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 75  t char *zName, u
2682a 38 20 66 6c 61 67 56 61 6c 29 7b 0a 20 20 46 75  8 flagVal){.  Fu
2682b 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 70  ncDef *pDef;.  p
2682c 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Def = sqlite3Fin
2682d 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e  dFunction(db, zN
2682e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ame, sqlite3Strl
2682f 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 0a 20 20 20  en30(zName),.   
26830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26831 20 20 20 20 20 20 20 20 20 20 32 2c 20 53 51 4c            2, SQL
26832 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20  ITE_UTF8, 0);.  
26833 69 66 28 20 41 4c 57 41 59 53 28 70 44 65 66 29  if( ALWAYS(pDef)
26834 20 29 7b 0a 20 20 20 20 70 44 65 66 2d 3e 66 6c   ){.    pDef->fl
26835 61 67 73 20 3d 20 66 6c 61 67 56 61 6c 3b 0a 20  ags = flagVal;. 
26836 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69   }.}../*.** Regi
26837 73 74 65 72 20 74 68 65 20 62 75 69 6c 74 2d 69  ster the built-i
26838 6e 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20  n LIKE and GLOB 
26839 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20  functions.  The 
2683a 63 61 73 65 53 65 6e 73 69 74 69 76 65 0a 2a 2a  caseSensitive.**
2683b 20 70 61 72 61 6d 65 74 65 72 20 64 65 74 65 72   parameter deter
2683c 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72  mines whether or
2683d 20 6e 6f 74 20 74 68 65 20 4c 49 4b 45 20 6f 70   not the LIKE op
2683e 65 72 61 74 6f 72 20 69 73 20 63 61 73 65 0a 2a  erator is case.*
2683f 2a 20 73 65 6e 73 69 74 69 76 65 2e 20 20 47 4c  * sensitive.  GL
26840 4f 42 20 69 73 20 61 6c 77 61 79 73 20 63 61 73  OB is always cas
26841 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2f 0a  e sensitive..*/.
26842 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
26843 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
26844 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73  terLikeFunctions
26845 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
26846 74 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 29  t caseSensitive)
26847 7b 0a 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61  {.  struct compa
26848 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20  reInfo *pInfo;. 
26849 20 69 66 28 20 63 61 73 65 53 65 6e 73 69 74 69   if( caseSensiti
2684a 76 65 20 29 7b 0a 20 20 20 20 70 49 6e 66 6f 20  ve ){.    pInfo 
2684b 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72  = (struct compar
2684c 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f  eInfo*)&likeInfo
2684d 41 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Alt;.  }else{.  
2684e 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63    pInfo = (struc
2684f 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26  t compareInfo*)&
26850 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 3b 0a 20 20  likeInfoNorm;.  
26851 7d 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74  }.  sqlite3Creat
26852 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22  eFunc(db, "like"
26853 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
26854 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63   pInfo, likeFunc
26855 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
26856 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
26857 20 22 6c 69 6b 65 22 2c 20 33 2c 20 53 51 4c 49   "like", 3, SQLI
26858 54 45 5f 41 4e 59 2c 20 70 49 6e 66 6f 2c 20 6c  TE_ANY, pInfo, l
26859 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  ikeFunc, 0, 0);.
2685a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
2685b 75 6e 63 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20  unc(db, "glob", 
2685c 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 0a  2, SQLITE_ANY, .
2685d 20 20 20 20 20 20 28 73 74 72 75 63 74 20 63 6f        (struct co
2685e 6d 70 61 72 65 49 6e 66 6f 2a 29 26 67 6c 6f 62  mpareInfo*)&glob
2685f 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20  Info, likeFunc, 
26860 30 2c 30 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f  0,0);.  setLikeO
26861 70 74 46 6c 61 67 28 64 62 2c 20 22 67 6c 6f 62  ptFlag(db, "glob
26862 22 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  ", SQLITE_FUNC_L
26863 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e  IKE | SQLITE_FUN
26864 43 5f 43 41 53 45 29 3b 0a 20 20 73 65 74 4c 69  C_CASE);.  setLi
26865 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 6c  keOptFlag(db, "l
26866 69 6b 65 22 2c 20 0a 20 20 20 20 20 20 63 61 73  ike", .      cas
26867 65 53 65 6e 73 69 74 69 76 65 20 3f 20 28 53 51  eSensitive ? (SQ
26868 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c  LITE_FUNC_LIKE |
26869 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53   SQLITE_FUNC_CAS
2686a 45 29 20 3a 20 53 51 4c 49 54 45 5f 46 55 4e 43  E) : SQLITE_FUNC
2686b 5f 4c 49 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _LIKE);.}../*.**
2686c 20 70 45 78 70 72 20 70 6f 69 6e 74 73 20 74 6f   pExpr points to
2686d 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77   an expression w
2686e 68 69 63 68 20 69 6d 70 6c 65 6d 65 6e 74 73 20  hich implements 
2686f 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 66 0a  a function.  If.
26870 2a 2a 20 69 74 20 69 73 20 61 70 70 72 6f 70 72  ** it is appropr
26871 69 61 74 65 20 74 6f 20 61 70 70 6c 79 20 74 68  iate to apply th
26872 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74  e LIKE optimizat
26873 69 6f 6e 20 74 6f 20 74 68 61 74 20 66 75 6e 63  ion to that func
26874 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 73 65 74  tion.** then set
26875 20 61 57 63 5b 30 5d 20 74 68 72 6f 75 67 68 20   aWc[0] through 
26876 61 57 63 5b 32 5d 20 74 6f 20 74 68 65 20 77 69  aWc[2] to the wi
26877 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 72  ldcard character
26878 73 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  s and.** return 
26879 54 52 55 45 2e 20 20 49 66 20 74 68 65 20 66 75  TRUE.  If the fu
2687a 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 20  nction is not a 
2687b 4c 49 4b 45 2d 73 74 79 6c 65 20 66 75 6e 63 74  LIKE-style funct
2687c 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75  ion then.** retu
2687d 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a 53 51 4c  rn FALSE..*/.SQL
2687e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2687f 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e  sqlite3IsLikeFun
26880 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64  ction(sqlite3 *d
26881 62 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  b, Expr *pExpr, 
26882 69 6e 74 20 2a 70 49 73 4e 6f 63 61 73 65 2c 20  int *pIsNocase, 
26883 63 68 61 72 20 2a 61 57 63 29 7b 0a 20 20 46 75  char *aWc){.  Fu
26884 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 69  ncDef *pDef;.  i
26885 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  f( pExpr->op!=TK
26886 5f 46 55 4e 43 54 49 4f 4e 20 0a 20 20 20 7c 7c  _FUNCTION .   ||
26887 20 21 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74   !pExpr->x.pList
26888 20 0a 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 78   .   || pExpr->x
26889 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32  .pList->nExpr!=2
2688a 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
2688b 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
2688c 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2688d 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
2688e 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 70 44 65  Select) );.  pDe
2688f 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46  f = sqlite3FindF
26890 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45 78 70  unction(db, pExp
26891 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 0a 20 20  r->u.zToken, .  
26892 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26893 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
26894 65 33 53 74 72 6c 65 6e 33 30 28 70 45 78 70 72  e3Strlen30(pExpr
26895 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 20 20  ->u.zToken),.   
26896 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26897 20 20 20 20 20 20 20 20 20 20 32 2c 20 53 51 4c            2, SQL
26898 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20  ITE_UTF8, 0);.  
26899 69 66 28 20 4e 45 56 45 52 28 70 44 65 66 3d 3d  if( NEVER(pDef==
2689a 30 29 20 7c 7c 20 28 70 44 65 66 2d 3e 66 6c 61  0) || (pDef->fla
2689b 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43  gs & SQLITE_FUNC
2689c 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 20  _LIKE)==0 ){.   
2689d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
2689e 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 28    /* The memcpy(
2689f 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 75  ) statement assu
268a0 6d 65 73 20 74 68 61 74 20 74 68 65 20 77 69 6c  mes that the wil
268a1 64 63 61 72 64 20 63 68 61 72 61 63 74 65 72 73  dcard characters
268a2 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 69   are.  ** the fi
268a3 72 73 74 20 74 68 72 65 65 20 73 74 61 74 65 6d  rst three statem
268a4 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70  ents in the comp
268a5 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72  areInfo structur
268a6 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 73  e.  The.  ** ass
268a7 65 72 74 73 28 29 20 74 68 61 74 20 66 6f 6c 6c  erts() that foll
268a8 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 61  ow verify that a
268a9 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20  ssumption.  */. 
268aa 20 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 44 65   memcpy(aWc, pDe
268ab 66 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 33 29  f->pUserData, 3)
268ac 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 68 61  ;.  assert( (cha
268ad 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20  r*)&likeInfoAlt 
268ae 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49  == (char*)&likeI
268af 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20  nfoAlt.matchAll 
268b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 28 28  );.  assert( &((
268b1 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41  char*)&likeInfoA
268b2 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a  lt)[1] == (char*
268b3 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61  )&likeInfoAlt.ma
268b4 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 65  tchOne );.  asse
268b5 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69  rt( &((char*)&li
268b6 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d  keInfoAlt)[2] ==
268b7 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
268b8 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 3b  oAlt.matchSet );
268b9 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d 20  .  *pIsNocase = 
268ba 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53  (pDef->flags & S
268bb 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29  QLITE_FUNC_CASE)
268bc 3d 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ==0;.  return 1;
268bd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 6c  .}../*.** All al
268be 6c 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66  l of the FuncDef
268bf 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 74   structures in t
268c0 68 65 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b  he aBuiltinFunc[
268c1 5d 20 61 72 72 61 79 20 61 62 6f 76 65 0a 2a 2a  ] array above.**
268c2 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 66   to the global f
268c3 75 6e 63 74 69 6f 6e 20 68 61 73 68 20 74 61 62  unction hash tab
268c4 6c 65 2e 20 20 54 68 69 73 20 6f 63 63 75 72 73  le.  This occurs
268c5 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 20 28   at start-time (
268c6 61 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 75 65  as.** a conseque
268c7 6e 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67 20 73  nce of calling s
268c8 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
268c9 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  e())..**.** Afte
268ca 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  r this routine r
268cb 75 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  uns.*/.SQLITE_PR
268cc 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
268cd 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c  e3RegisterGlobal
268ce 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b  Functions(void){
268cf 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  /*.  ** The f
268d0 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 68  ollowing array h
268d1 6f 6c 64 73 20 46 75 6e 63 44 65 66 20 73 74 72  olds FuncDef str
268d2 75 63 74 75 72 65 73 20 66 6f 72 20 61 6c 6c 20  uctures for all 
268d3 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73  of the functions
268d4 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 69 6e  .  ** defined in
268d5 20 74 68 69 73 20 66 69 6c 65 2e 0a 20 20 2a 2a   this file..  **
268d6 0a 20 20 2a 2a 20 54 68 65 20 61 72 72 61 79 20  .  ** The array 
268d7 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 61  cannot be consta
268d8 6e 74 20 73 69 6e 63 65 20 63 68 61 6e 67 65 73  nt since changes
268d9 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65   are made to the
268da 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e 70 48  .  ** FuncDef.pH
268db 61 73 68 20 65 6c 65 6d 65 6e 74 73 20 61 74 20  ash elements at 
268dc 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 54 68 65  start-time.  The
268dd 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 69   elements of thi
268de 73 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 72 65  s array.  ** are
268df 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74 65 72   read-only after
268e0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
268e1 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 20 2a  is complete..  *
268e2 2f 0a 20 20 73 74 61 74 69 63 20 53 51 4c 49 54  /.  static SQLIT
268e3 45 5f 57 53 44 20 46 75 6e 63 44 65 66 20 61 42  E_WSD FuncDef aB
268e4 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d 20 7b  uiltinFunc[] = {
268e5 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74  .    FUNCTION(lt
268e6 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  rim,            
268e7 20 20 31 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46    1, 1, 0, trimF
268e8 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20  unc         ),. 
268e9 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69     FUNCTION(ltri
268ea 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
268eb 32 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e  2, 1, 0, trimFun
268ec 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  c         ),.   
268ed 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c   FUNCTION(rtrim,
268ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
268ef 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20   2, 0, trimFunc 
268f0 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
268f1 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20  UNCTION(rtrim,  
268f2 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 32              2, 2
268f3 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20  , 0, trimFunc   
268f4 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
268f5 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20  CTION(trim,     
268f6 20 20 20 20 20 20 20 20 20 20 31 2c 20 33 2c 20            1, 3, 
268f7 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
268f8 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
268f9 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20  ION(trim,       
268fa 20 20 20 20 20 20 20 20 32 2c 20 33 2c 20 30 2c          2, 3, 0,
268fb 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
268fc 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
268fd 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20  N(min,          
268fe 20 20 20 20 20 2d 31 2c 20 30 2c 20 31 2c 20 6d       -1, 0, 1, m
268ff 69 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20  inmaxFunc       
26900 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
26901 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  min,            
26902 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 30 20 20      0, 0, 1, 0  
26903 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c                ),
26904 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 6d  .    AGGREGATE(m
26905 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  in,             
26906 20 20 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61    1, 0, 1, minma
26907 78 53 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d  xStep,      minM
26908 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20  axFinalize ),.  
26909 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20    FUNCTION(max, 
2690a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31                -1
2690b 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75  , 1, 1, minmaxFu
2690c 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  nc       ),.    
2690d 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20  FUNCTION(max,   
2690e 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
2690f 31 2c 20 31 2c 20 30 20 20 20 20 20 20 20 20 20  1, 1, 0         
26910 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 41 47         ),.    AG
26911 47 52 45 47 41 54 45 28 6d 61 78 2c 20 20 20 20  GREGATE(max,    
26912 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 31 2c             1, 1,
26913 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20   1, minmaxStep, 
26914 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c       minMaxFinal
26915 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e 43 54  ize ),.    FUNCT
26916 49 4f 4e 28 74 79 70 65 6f 66 2c 20 20 20 20 20  ION(typeof,     
26917 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
26918 20 74 79 70 65 6f 66 46 75 6e 63 20 20 20 20 20   typeofFunc     
26919 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
2691a 4e 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 20  N(length,       
2691b 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c        1, 0, 0, l
2691c 65 6e 67 74 68 46 75 6e 63 20 20 20 20 20 20 20  engthFunc       
2691d 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
2691e 73 75 62 73 74 72 2c 20 20 20 20 20 20 20 20 20  substr,         
2691f 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 73 75 62      2, 0, 0, sub
26920 73 74 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c  strFunc       ),
26921 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75  .    FUNCTION(su
26922 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  bstr,           
26923 20 20 33 2c 20 30 2c 20 30 2c 20 73 75 62 73 74    3, 0, 0, subst
26924 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  rFunc       ),. 
26925 20 20 20 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c     FUNCTION(abs,
26926 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26927 31 2c 20 30 2c 20 30 2c 20 61 62 73 46 75 6e 63  1, 0, 0, absFunc
26928 20 20 20 20 20 20 20 20 20 20 29 2c 0a 23 69 66            ),.#if
26929 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2692a 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
2692b 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75      FUNCTION(rou
2692c 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nd,             
2692d 20 31 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46   1, 0, 0, roundF
2692e 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20  unc        ),.  
2692f 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64    FUNCTION(round
26930 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  ,              2
26931 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e  , 0, 0, roundFun
26932 63 20 20 20 20 20 20 20 20 29 2c 0a 23 65 6e 64  c        ),.#end
26933 69 66 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  if.    FUNCTION(
26934 75 70 70 65 72 2c 20 20 20 20 20 20 20 20 20 20  upper,          
26935 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 75 70 70      1, 0, 0, upp
26936 65 72 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c  erFunc        ),
26937 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f  .    FUNCTION(lo
26938 77 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  wer,            
26939 20 20 31 2c 20 30 2c 20 30 2c 20 6c 6f 77 65 72    1, 0, 0, lower
2693a 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20  Func        ),. 
2693b 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c     FUNCTION(coal
2693c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 20 20  esce,           
2693d 31 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20  1, 0, 0, 0      
2693e 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20            ),.   
2693f 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73   FUNCTION(coales
26940 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 30 2c  ce,           0,
26941 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20   0, 0, 0        
26942 20 20 20 20 20 20 20 20 29 2c 0a 2f 2a 20 20 46          ),./*  F
26943 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65  UNCTION(coalesce
26944 2c 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30  ,          -1, 0
26945 2c 20 30 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 20  , 0, ifnullFunc 
26946 20 20 20 20 20 20 29 2c 20 2a 2f 0a 20 20 20 20        ), */.    
26947 7b 2d 31 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c  {-1,SQLITE_UTF8,
26948 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c  SQLITE_FUNC_COAL
26949 45 53 43 45 2c 30 2c 30 2c 69 66 6e 75 6c 6c 46  ESCE,0,0,ifnullF
2694a 75 6e 63 2c 30 2c 30 2c 22 63 6f 61 6c 65 73 63  unc,0,0,"coalesc
2694b 65 22 2c 30 7d 2c 0a 20 20 20 20 46 55 4e 43 54  e",0},.    FUNCT
2694c 49 4f 4e 28 68 65 78 2c 20 20 20 20 20 20 20 20  ION(hex,        
2694d 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
2694e 20 68 65 78 46 75 6e 63 20 20 20 20 20 20 20 20   hexFunc        
2694f 20 20 29 2c 0a 2f 2a 20 20 46 55 4e 43 54 49 4f    ),./*  FUNCTIO
26950 4e 28 69 66 6e 75 6c 6c 2c 20 20 20 20 20 20 20  N(ifnull,       
26951 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 69        2, 0, 0, i
26952 66 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20 20 20  fnullFunc       
26953 29 2c 20 2a 2f 0a 20 20 20 20 7b 32 2c 53 51 4c  ), */.    {2,SQL
26954 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f  ITE_UTF8,SQLITE_
26955 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 2c 30 2c  FUNC_COALESCE,0,
26956 30 2c 69 66 6e 75 6c 6c 46 75 6e 63 2c 30 2c 30  0,ifnullFunc,0,0
26957 2c 22 69 66 6e 75 6c 6c 22 2c 30 7d 2c 0a 20 20  ,"ifnull",0},.  
26958 20 20 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f    FUNCTION(rando
26959 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30  m,             0
2695a 2c 20 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 46 75  , 0, 0, randomFu
2695b 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  nc       ),.    
2695c 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 62  FUNCTION(randomb
2695d 6c 6f 62 2c 20 20 20 20 20 20 20 20 20 31 2c 20  lob,         1, 
2695e 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62  0, 0, randomBlob
2695f 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
26960 4e 43 54 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20  NCTION(nullif,  
26961 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c             2, 0,
26962 20 31 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20 20   1, nullifFunc  
26963 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26964 54 49 4f 4e 28 73 71 6c 69 74 65 5f 76 65 72 73  TION(sqlite_vers
26965 69 6f 6e 2c 20 20 20 20 20 30 2c 20 30 2c 20 30  ion,     0, 0, 0
26966 2c 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20 20  , versionFunc   
26967 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
26968 4f 4e 28 73 71 6c 69 74 65 5f 73 6f 75 72 63 65  ON(sqlite_source
26969 5f 69 64 2c 20 20 20 30 2c 20 30 2c 20 30 2c 20  _id,   0, 0, 0, 
2696a 73 6f 75 72 63 65 69 64 46 75 6e 63 20 20 20 20  sourceidFunc    
2696b 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
2696c 28 71 75 6f 74 65 2c 20 20 20 20 20 20 20 20 20  (quote,         
2696d 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 71 75       1, 0, 0, qu
2696e 6f 74 65 46 75 6e 63 20 20 20 20 20 20 20 20 29  oteFunc        )
2696f 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c  ,.    FUNCTION(l
26970 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
26971 2c 20 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73 74  ,  0, 0, 0, last
26972 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c 0a  _insert_rowid),.
26973 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 68 61      FUNCTION(cha
26974 6e 67 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  nges,           
26975 20 30 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67 65   0, 0, 0, change
26976 73 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20  s          ),.  
26977 20 20 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61 6c    FUNCTION(total
26978 5f 63 68 61 6e 67 65 73 2c 20 20 20 20 20 20 30  _changes,      0
26979 2c 20 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 68  , 0, 0, total_ch
2697a 61 6e 67 65 73 20 20 20 20 29 2c 0a 20 20 20 20  anges    ),.    
2697b 46 55 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63 65  FUNCTION(replace
2697c 2c 20 20 20 20 20 20 20 20 20 20 20 20 33 2c 20  ,            3, 
2697d 30 2c 20 30 2c 20 72 65 70 6c 61 63 65 46 75 6e  0, 0, replaceFun
2697e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55  c      ),.    FU
2697f 4e 43 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c  NCTION(zeroblob,
26980 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
26981 20 30 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63   0, zeroblobFunc
26982 20 20 20 20 20 29 2c 0a 20 20 23 69 66 64 65 66       ),.  #ifdef
26983 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a   SQLITE_SOUNDEX.
26984 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f 75      FUNCTION(sou
26985 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20  ndex,           
26986 20 31 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64 65   1, 0, 0, sounde
26987 78 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20  xFunc      ),.  
26988 23 65 6e 64 69 66 0a 20 20 23 69 66 6e 64 65 66  #endif.  #ifndef
26989 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
2698a 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20  D_EXTENSION.    
2698b 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78  FUNCTION(load_ex
2698c 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c 20  tension,     1, 
2698d 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20  0, 0, loadExt   
2698e 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
2698f 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65  NCTION(load_exte
26990 6e 73 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30 2c  nsion,     2, 0,
26991 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20   0, loadExt     
26992 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 69 66       ),.  #endif
26993 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 73  .    AGGREGATE(s
26994 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  um,             
26995 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74    1, 0, 0, sumSt
26996 65 70 2c 20 20 20 20 20 20 20 20 20 73 75 6d 46  ep,         sumF
26997 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20  inalize    ),.  
26998 20 20 41 47 47 52 45 47 41 54 45 28 74 6f 74 61    AGGREGATE(tota
26999 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31  l,             1
2699a 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c  , 0, 0, sumStep,
2699b 20 20 20 20 20 20 20 20 20 74 6f 74 61 6c 46 69           totalFi
2699c 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20  nalize    ),.   
2699d 20 41 47 47 52 45 47 41 54 45 28 61 76 67 2c 20   AGGREGATE(avg, 
2699e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
2699f 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20   0, 0, sumStep, 
269a0 20 20 20 20 20 20 20 20 61 76 67 46 69 6e 61 6c          avgFinal
269a1 69 7a 65 20 20 20 20 29 2c 0a 20 2f 2a 20 41 47  ize    ),. /* AG
269a2 47 52 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20  GREGATE(count,  
269a3 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c             0, 0,
269a4 20 30 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20   0, countStep,  
269a5 20 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69       countFinali
269a6 7a 65 20 20 29 2c 20 2a 2f 0a 20 20 20 20 7b 30  ze  ), */.    {0
269a7 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c  ,SQLITE_UTF8,SQL
269a8 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 2c 30  ITE_FUNC_COUNT,0
269a9 2c 30 2c 30 2c 63 6f 75 6e 74 53 74 65 70 2c 63  ,0,0,countStep,c
269aa 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 2c 22 63 6f  ountFinalize,"co
269ab 75 6e 74 22 2c 30 7d 2c 0a 20 20 20 20 41 47 47  unt",0},.    AGG
269ac 52 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20  REGATE(count,   
269ad 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
269ae 30 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20  0, countStep,   
269af 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a      countFinaliz
269b0 65 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47  e  ),.    AGGREG
269b1 41 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74  ATE(group_concat
269b2 2c 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20  ,      1, 0, 0, 
269b3 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c  groupConcatStep,
269b4 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61   groupConcatFina
269b5 6c 69 7a 65 29 2c 0a 20 20 20 20 41 47 47 52 45  lize),.    AGGRE
269b6 47 41 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61  GATE(group_conca
269b7 74 2c 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c  t,      2, 0, 0,
269b8 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70   groupConcatStep
269b9 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e  , groupConcatFin
269ba 61 6c 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c  alize),.  .    L
269bb 49 4b 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c  IKEFUNC(glob, 2,
269bc 20 26 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49   &globInfo, SQLI
269bd 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c  TE_FUNC_LIKE|SQL
269be 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a  ITE_FUNC_CASE),.
269bf 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f    #ifdef SQLITE_
269c0 43 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c  CASE_SENSITIVE_L
269c1 49 4b 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43  IKE.    LIKEFUNC
269c2 28 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49  (like, 2, &likeI
269c3 6e 66 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46  nfoAlt, SQLITE_F
269c4 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f  UNC_LIKE|SQLITE_
269c5 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20 20  FUNC_CASE),.    
269c6 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33  LIKEFUNC(like, 3
269c7 2c 20 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20  , &likeInfoAlt, 
269c8 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
269c9 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53  |SQLITE_FUNC_CAS
269ca 45 29 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20 20  E),.  #else.    
269cb 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32  LIKEFUNC(like, 2
269cc 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c  , &likeInfoNorm,
269cd 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
269ce 45 29 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43  E),.    LIKEFUNC
269cf 28 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49  (like, 3, &likeI
269d0 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f  nfoNorm, SQLITE_
269d1 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65  FUNC_LIKE),.  #e
269d2 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74  ndif.  };..  int
269d3 20 69 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73   i;.  FuncDefHas
269d4 68 20 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42  h *pHash = &GLOB
269d5 41 4c 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20  AL(FuncDefHash, 
269d6 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e  sqlite3GlobalFun
269d7 63 74 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44  ctions);.  FuncD
269d8 65 66 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e  ef *aFunc = (Fun
269d9 63 44 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75  cDef*)&GLOBAL(Fu
269da 6e 63 44 65 66 2c 20 61 42 75 69 6c 74 69 6e 46  ncDef, aBuiltinF
269db 75 6e 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30  unc);..  for(i=0
269dc 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42  ; i<ArraySize(aB
269dd 75 69 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b  uiltinFunc); i++
269de 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75  ){.    sqlite3Fu
269df 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73  ncDefInsert(pHas
269e0 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20  h, &aFunc[i]);. 
269e1 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69   }.  sqlite3Regi
269e2 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63  sterDateTimeFunc
269e3 74 69 6f 6e 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  tions();.}../***
269e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
269e5 6f 66 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a  of func.c ******
269e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
269e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
269ea 6e 20 66 69 6c 65 20 66 6b 65 79 2e 63 20 2a 2a  n file fkey.c **
269eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
269ee 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
269ef 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
269f0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
269f1 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
269f2 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
269f3 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
269f4 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
269f5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
269f6 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
269f7 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
269f8 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
269f9 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
269fa 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
269fb 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
269fc 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
269fd 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
269fe 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
269ff 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
26a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
26a04 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
26a05 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 62 79  ins code used by
26a06 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 74 6f   the compiler to
26a07 20 61 64 64 20 66 6f 72 65 69 67 6e 20 6b 65 79   add foreign key
26a08 0a 2a 2a 20 73 75 70 70 6f 72 74 20 74 6f 20 63  .** support to c
26a09 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74  ompiled SQL stat
26a0a 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 0a 23 69 66 6e  ements..*/..#ifn
26a0b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
26a0c 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 23 69 66 6e  FOREIGN_KEY.#ifn
26a0d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
26a0e 54 52 49 47 47 45 52 0a 0a 2f 2a 0a 2a 2a 20 44  TRIGGER../*.** D
26a0f 65 66 65 72 72 65 64 20 61 6e 64 20 49 6d 6d 65  eferred and Imme
26a10 64 69 61 74 65 20 46 4b 73 0a 2a 2a 20 2d 2d 2d  diate FKs.** ---
26a11 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26a12 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 46 6f  -------.**.** Fo
26a13 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 53 51  reign keys in SQ
26a14 4c 69 74 65 20 63 6f 6d 65 20 69 6e 20 74 77 6f  Lite come in two
26a15 20 66 6c 61 76 6f 75 72 73 3a 20 64 65 66 65 72   flavours: defer
26a16 72 65 64 20 61 6e 64 20 69 6d 6d 65 64 69 61 74  red and immediat
26a17 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6d 6d 65  e..** If an imme
26a18 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  diate foreign ke
26a19 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  y constraint is 
26a1a 76 69 6f 6c 61 74 65 64 2c 20 53 51 4c 49 54 45  violated, SQLITE
26a1b 5f 43 4f 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 69  _CONSTRAINT.** i
26a1c 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
26a1d 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
26a1e 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
26a1f 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66   rolled back. If
26a20 20 61 20 0a 2a 2a 20 64 65 66 65 72 72 65 64 20   a .** deferred 
26a21 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
26a22 74 72 61 69 6e 74 20 69 73 20 76 69 6f 6c 61 74  traint is violat
26a23 65 64 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 69 73  ed, no action is
26a24 20 74 61 6b 65 6e 20 0a 2a 2a 20 69 6d 6d 65 64   taken .** immed
26a25 69 61 74 65 6c 79 2e 20 48 6f 77 65 76 65 72 20  iately. However 
26a26 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  if the applicati
26a27 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63  on attempts to c
26a28 6f 6d 6d 69 74 20 74 68 65 20 0a 2a 2a 20 74 72  ommit the .** tr
26a29 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65  ansaction before
26a2a 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 73   fixing the cons
26a2b 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
26a2c 2c 20 74 68 65 20 61 74 74 65 6d 70 74 20 66 61  , the attempt fa
26a2d 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 65 72  ils..**.** Defer
26a2e 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  red constraints 
26a2f 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  are implemented 
26a30 75 73 69 6e 67 20 61 20 73 69 6d 70 6c 65 20 63  using a simple c
26a31 6f 75 6e 74 65 72 20 61 73 73 6f 63 69 61 74 65  ounter associate
26a32 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 64 61  d.** with the da
26a33 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54  tabase handle. T
26a34 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 73 65  he counter is se
26a35 74 20 74 6f 20 7a 65 72 6f 20 65 61 63 68 20 74  t to zero each t
26a36 69 6d 65 20 61 20 0a 2a 2a 20 64 61 74 61 62 61  ime a .** databa
26a37 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  se transaction i
26a38 73 20 6f 70 65 6e 65 64 2e 20 45 61 63 68 20 74  s opened. Each t
26a39 69 6d 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20  ime a statement 
26a3a 69 73 20 65 78 65 63 75 74 65 64 20 0a 2a 2a 20  is executed .** 
26a3b 74 68 61 74 20 63 61 75 73 65 73 20 61 20 66 6f  that causes a fo
26a3c 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74  reign key violat
26a3d 69 6f 6e 2c 20 74 68 65 20 63 6f 75 6e 74 65 72  ion, the counter
26a3e 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e   is incremented.
26a3f 20 45 61 63 68 0a 2a 2a 20 74 69 6d 65 20 61 20   Each.** time a 
26a40 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65  statement is exe
26a41 63 75 74 65 64 20 74 68 61 74 20 72 65 6d 6f 76  cuted that remov
26a42 65 73 20 61 6e 20 65 78 69 73 74 69 6e 67 20 76  es an existing v
26a43 69 6f 6c 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  iolation from.**
26a44 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
26a45 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 64 65  he counter is de
26a46 63 72 65 6d 65 6e 74 65 64 2e 20 57 68 65 6e 20  cremented. When 
26a47 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
26a48 69 73 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2c  is.** committed,
26a49 20 74 68 65 20 63 6f 6d 6d 69 74 20 66 61 69 6c   the commit fail
26a4a 73 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74  s if the current
26a4b 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f   value of the co
26a4c 75 6e 74 65 72 20 69 73 0a 2a 2a 20 67 72 65 61  unter is.** grea
26a4d 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 20 54  ter than zero. T
26a4e 68 69 73 20 73 63 68 65 6d 65 20 68 61 73 20 74  his scheme has t
26a4f 77 6f 20 62 69 67 20 64 72 61 77 62 61 63 6b 73  wo big drawbacks
26a50 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 57 68 65 6e  :.**.**   * When
26a51 20 61 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20   a commit fails 
26a52 64 75 65 20 74 6f 20 61 20 64 65 66 65 72 72 65  due to a deferre
26a53 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  d foreign key co
26a54 6e 73 74 72 61 69 6e 74 2c 20 0a 2a 2a 20 20 20  nstraint, .**   
26a55 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61    there is no wa
26a56 79 20 74 6f 20 74 65 6c 6c 20 77 68 69 63 68 20  y to tell which 
26a57 66 6f 72 65 69 67 6e 20 63 6f 6e 73 74 72 61 69  foreign constrai
26a58 6e 74 20 69 73 20 6e 6f 74 20 73 61 74 69 73 66  nt is not satisf
26a59 69 65 64 2c 0a 2a 2a 20 20 20 20 20 6f 72 20 77  ied,.**     or w
26a5a 68 69 63 68 20 72 6f 77 20 69 74 20 69 73 20 6e  hich row it is n
26a5b 6f 74 20 73 61 74 69 73 66 69 65 64 20 66 6f 72  ot satisfied for
26a5c 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74  ..**.**   * If t
26a5d 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
26a5e 61 69 6e 73 20 66 6f 72 65 69 67 6e 20 6b 65 79  ains foreign key
26a5f 20 76 69 6f 6c 61 74 69 6f 6e 73 20 77 68 65 6e   violations when
26a60 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 74 72 61   the .**     tra
26a61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
26a62 65 64 2c 20 74 68 69 73 20 6d 61 79 20 63 61 75  ed, this may cau
26a63 73 65 20 74 68 65 20 6d 65 63 68 61 6e 69 73 6d  se the mechanism
26a64 20 74 6f 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e   to malfunction.
26a65 0a 2a 2a 0a 2a 2a 20 44 65 73 70 69 74 65 20 74  .**.** Despite t
26a66 68 65 73 65 20 70 72 6f 62 6c 65 6d 73 2c 20 74  hese problems, t
26a67 68 69 73 20 61 70 70 72 6f 61 63 68 20 69 73 20  his approach is 
26a68 61 64 6f 70 74 65 64 20 61 73 20 69 74 20 73 65  adopted as it se
26a69 65 6d 73 20 73 69 6d 70 6c 65 72 0a 2a 2a 20 74  ems simpler.** t
26a6a 68 61 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74  han the alternat
26a6b 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 53 45  ives..**.** INSE
26a6c 52 54 20 6f 70 65 72 61 74 69 6f 6e 73 3a 0a 2a  RT operations:.*
26a6d 2a 0a 2a 2a 20 20 20 49 2e 31 29 20 46 6f 72 20  *.**   I.1) For 
26a6e 65 61 63 68 20 46 4b 20 66 6f 72 20 77 68 69 63  each FK for whic
26a6f 68 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74  h the table is t
26a70 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2c 20  he child table, 
26a71 73 65 61 72 63 68 0a 2a 2a 20 20 20 20 20 20 20  search.**       
26a72 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26a73 65 20 66 6f 72 20 61 20 6d 61 74 63 68 2e 20 49  e for a match. I
26a74 66 20 6e 6f 6e 65 20 69 73 20 66 6f 75 6e 64 20  f none is found 
26a75 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 0a 2a 2a  increment the.**
26a76 20 20 20 20 20 20 20 20 63 6f 6e 73 74 72 61 69          constrai
26a77 6e 74 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a  nt counter..**.*
26a78 2a 20 20 20 49 2e 32 29 20 46 6f 72 20 65 61 63  *   I.2) For eac
26a79 68 20 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74  h FK for which t
26a7a 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  he table is the 
26a7b 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 0a 2a  parent table, .*
26a7c 2a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 20  *        search 
26a7d 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
26a7e 66 6f 72 20 72 6f 77 73 20 74 68 61 74 20 63 6f  for rows that co
26a7f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20  rrespond to the 
26a80 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 72 6f  new.**        ro
26a81 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  w in the parent 
26a82 74 61 62 6c 65 2e 20 44 65 63 72 65 6d 65 6e 74  table. Decrement
26a83 20 74 68 65 20 63 6f 75 6e 74 65 72 20 66 6f 72   the counter for
26a84 20 65 61 63 68 20 72 6f 77 0a 2a 2a 20 20 20 20   each row.**    
26a85 20 20 20 20 66 6f 75 6e 64 20 28 61 73 20 74 68      found (as th
26a86 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  e constraint is 
26a87 6e 6f 77 20 73 61 74 69 73 66 69 65 64 29 2e 0a  now satisfied)..
26a88 2a 2a 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65  **.** DELETE ope
26a89 72 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20  rations:.**.**  
26a8a 20 44 2e 31 29 20 46 6f 72 20 65 61 63 68 20 46   D.1) For each F
26a8b 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  K for which the 
26a8c 74 61 62 6c 65 20 69 73 20 74 68 65 20 63 68 69  table is the chi
26a8d 6c 64 20 74 61 62 6c 65 2c 20 0a 2a 2a 20 20 20  ld table, .**   
26a8e 20 20 20 20 20 73 65 61 72 63 68 20 74 68 65 20       search the 
26a8f 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66 6f 72  parent table for
26a90 20 61 20 72 6f 77 20 74 68 61 74 20 63 6f 72 72   a row that corr
26a91 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a  esponds to the .
26a92 2a 2a 20 20 20 20 20 20 20 20 64 65 6c 65 74 65  **        delete
26a93 64 20 72 6f 77 20 69 6e 20 74 68 65 20 63 68 69  d row in the chi
26a94 6c 64 20 74 61 62 6c 65 2e 20 49 66 20 73 75 63  ld table. If suc
26a95 68 20 61 20 72 6f 77 20 69 73 20 6e 6f 74 20 66  h a row is not f
26a96 6f 75 6e 64 2c 20 0a 2a 2a 20 20 20 20 20 20 20  ound, .**       
26a97 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63   decrement the c
26a98 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20  ounter..**.**   
26a99 44 2e 32 29 20 46 6f 72 20 65 61 63 68 20 46 4b  D.2) For each FK
26a9a 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 74   for which the t
26a9b 61 62 6c 65 20 69 73 20 74 68 65 20 70 61 72 65  able is the pare
26a9c 6e 74 20 74 61 62 6c 65 2c 20 73 65 61 72 63 68  nt table, search
26a9d 20 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20   .**        the 
26a9e 63 68 69 6c 64 20 74 61 62 6c 65 20 66 6f 72 20  child table for 
26a9f 72 6f 77 73 20 74 68 61 74 20 63 6f 72 72 65 73  rows that corres
26aa0 70 6f 6e 64 20 74 6f 20 74 68 65 20 64 65 6c 65  pond to the dele
26aa1 74 65 64 20 72 6f 77 20 0a 2a 2a 20 20 20 20 20  ted row .**     
26aa2 20 20 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74     in the parent
26aa3 20 74 61 62 6c 65 2e 20 46 6f 72 20 65 61 63 68   table. For each
26aa4 20 66 6f 75 6e 64 20 69 6e 63 72 65 6d 65 6e 74   found increment
26aa5 20 74 68 65 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a   the counter..**
26aa6 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 72 61  .** UPDATE opera
26aa7 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 41  tions:.**.**   A
26aa8 6e 20 55 50 44 41 54 45 20 63 6f 6d 6d 61 6e 64  n UPDATE command
26aa9 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20 61   requires that a
26aaa 6c 6c 20 34 20 73 74 65 70 73 20 61 62 6f 76 65  ll 4 steps above
26aab 20 61 72 65 20 74 61 6b 65 6e 2c 20 62 75 74 20   are taken, but 
26aac 6f 6e 6c 79 0a 2a 2a 20 20 20 66 6f 72 20 46 4b  only.**   for FK
26aad 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72   constraints for
26aae 20 77 68 69 63 68 20 74 68 65 20 61 66 66 65 63   which the affec
26aaf 74 65 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20  ted columns are 
26ab0 61 63 74 75 61 6c 6c 79 20 0a 2a 2a 20 20 20 6d  actually .**   m
26ab1 6f 64 69 66 69 65 64 20 28 76 61 6c 75 65 73 20  odified (values 
26ab2 6d 75 73 74 20 62 65 20 63 6f 6d 70 61 72 65 64  must be compared
26ab3 20 61 74 20 72 75 6e 74 69 6d 65 29 2e 0a 2a 2a   at runtime)..**
26ab4 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 49 2e  .** Note that I.
26ab5 31 20 61 6e 64 20 44 2e 31 20 61 72 65 20 76 65  1 and D.1 are ve
26ab6 72 79 20 73 69 6d 69 6c 61 72 20 6f 70 65 72 61  ry similar opera
26ab7 74 69 6f 6e 73 2c 20 61 73 20 61 72 65 20 49 2e  tions, as are I.
26ab8 32 20 61 6e 64 20 44 2e 32 2e 0a 2a 2a 20 54 68  2 and D.2..** Th
26ab9 69 73 20 73 69 6d 70 6c 69 66 69 65 73 20 74 68  is simplifies th
26aba 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
26abb 20 61 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f   a bit..**.** Fo
26abc 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
26abd 66 20 69 6d 6d 65 64 69 61 74 65 20 46 4b 20 63  f immediate FK c
26abe 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20  onstraints, the 
26abf 4f 52 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c  OR REPLACE confl
26ac0 69 63 74 0a 2a 2a 20 72 65 73 6f 6c 75 74 69 6f  ict.** resolutio
26ac1 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  n is considered 
26ac2 74 6f 20 64 65 6c 65 74 65 20 72 6f 77 73 20 62  to delete rows b
26ac3 65 66 6f 72 65 20 74 68 65 20 6e 65 77 20 72 6f  efore the new ro
26ac4 77 20 69 73 20 69 6e 73 65 72 74 65 64 2e 0a 2a  w is inserted..*
26ac5 2a 20 49 66 20 61 20 64 65 6c 65 74 65 20 63 61  * If a delete ca
26ac6 75 73 65 64 20 62 79 20 4f 52 20 52 45 50 4c 41  used by OR REPLA
26ac7 43 45 20 76 69 6f 6c 61 74 65 73 20 61 6e 20 46  CE violates an F
26ac8 4b 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 61 6e  K constraint, an
26ac9 20 65 78 63 65 70 74 69 6f 6e 0a 2a 2a 20 69 73   exception.** is
26aca 20 74 68 72 6f 77 6e 2c 20 65 76 65 6e 20 69 66   thrown, even if
26acb 20 74 68 65 20 46 4b 20 63 6f 6e 73 74 72 61 69   the FK constrai
26acc 6e 74 20 77 6f 75 6c 64 20 62 65 20 73 61 74 69  nt would be sati
26acd 73 66 69 65 64 20 61 66 74 65 72 20 74 68 65 20  sfied after the 
26ace 6e 65 77 20 0a 2a 2a 20 72 6f 77 20 69 73 20 69  new .** row is i
26acf 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  nserted..**.** I
26ad0 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74 72 61  mmediate constra
26ad1 69 6e 74 73 20 61 72 65 20 75 73 75 61 6c 6c 79  ints are usually
26ad2 20 68 61 6e 64 6c 65 64 20 73 69 6d 69 6c 61 72   handled similar
26ad3 6c 79 2e 20 54 68 65 20 6f 6e 6c 79 20 64 69 66  ly. The only dif
26ad4 66 65 72 65 6e 63 65 20 0a 2a 2a 20 69 73 20 74  ference .** is t
26ad5 68 61 74 20 74 68 65 20 63 6f 75 6e 74 65 72 20  hat the counter 
26ad6 75 73 65 64 20 69 73 20 73 74 6f 72 65 64 20 61  used is stored a
26ad7 73 20 70 61 72 74 20 6f 66 20 65 61 63 68 20 69  s part of each i
26ad8 6e 64 69 76 69 64 75 61 6c 20 73 74 61 74 65 6d  ndividual statem
26ad9 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 28 73  ent.** object (s
26ada 74 72 75 63 74 20 56 64 62 65 29 2e 20 49 66 2c  truct Vdbe). If,
26adb 20 61 66 74 65 72 20 74 68 65 20 73 74 61 74 65   after the state
26adc 6d 65 6e 74 20 68 61 73 20 72 75 6e 2c 20 69 74  ment has run, it
26add 73 20 69 6d 6d 65 64 69 61 74 65 0a 2a 2a 20 63  s immediate.** c
26ade 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65  onstraint counte
26adf 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  r is greater tha
26ae0 6e 20 7a 65 72 6f 2c 20 69 74 20 72 65 74 75 72  n zero, it retur
26ae1 6e 73 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  ns SQLITE_CONSTR
26ae2 41 49 4e 54 0a 2a 2a 20 61 6e 64 20 74 68 65 20  AINT.** and the 
26ae3 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
26ae4 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20  ction is rolled 
26ae5 62 61 63 6b 2e 20 41 6e 20 65 78 63 65 70 74 69  back. An excepti
26ae6 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54 0a  on is an INSERT.
26ae7 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  ** statement tha
26ae8 74 20 69 6e 73 65 72 74 73 20 61 20 73 69 6e 67  t inserts a sing
26ae9 6c 65 20 72 6f 77 20 6f 6e 6c 79 20 28 6e 6f 20  le row only (no 
26aea 74 72 69 67 67 65 72 73 29 2e 20 49 6e 20 74 68  triggers). In th
26aeb 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 6e 73 74  is case,.** inst
26aec 65 61 64 20 6f 66 20 75 73 69 6e 67 20 61 20 63  ead of using a c
26aed 6f 75 6e 74 65 72 2c 20 61 6e 20 65 78 63 65 70  ounter, an excep
26aee 74 69 6f 6e 20 69 73 20 74 68 72 6f 77 6e 20 69  tion is thrown i
26aef 6d 6d 65 64 69 61 74 65 6c 79 20 69 66 20 74 68  mmediately if th
26af0 65 0a 2a 2a 20 49 4e 53 45 52 54 20 76 69 6f 6c  e.** INSERT viol
26af1 61 74 65 73 20 61 20 66 6f 72 65 69 67 6e 20 6b  ates a foreign k
26af2 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54  ey constraint. T
26af3 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79  his is necessary
26af4 20 61 73 20 73 75 63 68 0a 2a 2a 20 61 6e 20 49   as such.** an I
26af5 4e 53 45 52 54 20 64 6f 65 73 20 6e 6f 74 20 6f  NSERT does not o
26af6 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20  pen a statement 
26af7 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
26af8 2a 2a 20 54 4f 44 4f 3a 20 48 6f 77 20 73 68 6f  ** TODO: How sho
26af9 75 6c 64 20 64 72 6f 70 70 69 6e 67 20 61 20 74  uld dropping a t
26afa 61 62 6c 65 20 62 65 20 68 61 6e 64 6c 65 64 3f  able be handled?
26afb 20 48 6f 77 20 73 68 6f 75 6c 64 20 72 65 6e 61   How should rena
26afc 6d 69 6e 67 20 61 20 0a 2a 2a 20 74 61 62 6c 65  ming a .** table
26afd 20 62 65 20 68 61 6e 64 6c 65 64 3f 0a 2a 2a 0a   be handled?.**.
26afe 2a 2a 0a 2a 2a 20 51 75 65 72 79 20 41 50 49 20  **.** Query API 
26aff 4e 6f 74 65 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d  Notes.** -------
26b00 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 42  --------.**.** B
26b01 65 66 6f 72 65 20 63 6f 64 69 6e 67 20 61 6e 20  efore coding an 
26b02 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
26b03 20 72 6f 77 20 6f 70 65 72 61 74 69 6f 6e 2c 20   row operation, 
26b04 74 68 65 20 63 6f 64 65 2d 67 65 6e 65 72 61 74  the code-generat
26b05 6f 72 0a 2a 2a 20 66 6f 72 20 74 68 6f 73 65 20  or.** for those 
26b06 74 77 6f 20 6f 70 65 72 61 74 69 6f 6e 73 20 6e  two operations n
26b07 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65  eeds to know whe
26b08 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
26b09 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65 71  operation.** req
26b0a 75 69 72 65 73 20 61 6e 79 20 46 4b 20 70 72 6f  uires any FK pro
26b0b 63 65 73 73 69 6e 67 20 61 6e 64 2c 20 69 66 20  cessing and, if 
26b0c 73 6f 2c 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e  so, which column
26b0d 73 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  s of the origina
26b0e 6c 0a 2a 2a 20 72 6f 77 20 61 72 65 20 72 65 71  l.** row are req
26b0f 75 69 72 65 64 20 62 79 20 74 68 65 20 46 4b 20  uired by the FK 
26b10 70 72 6f 63 65 73 73 69 6e 67 20 56 44 42 45 20  processing VDBE 
26b11 63 6f 64 65 20 28 69 2e 65 2e 20 69 66 20 46 4b  code (i.e. if FK
26b12 73 20 77 65 72 65 0a 2a 2a 20 69 6d 70 6c 65 6d  s were.** implem
26b13 65 6e 74 65 64 20 75 73 69 6e 67 20 74 72 69 67  ented using trig
26b14 67 65 72 73 2c 20 77 68 69 63 68 20 6f 66 20 74  gers, which of t
26b15 68 65 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73  he old.* columns
26b16 20 77 6f 75 6c 64 20 62 65 20 0a 2a 2a 20 61 63   would be .** ac
26b17 63 65 73 73 65 64 29 2e 20 4e 6f 20 69 6e 66 6f  cessed). No info
26b18 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69  rmation is requi
26b19 72 65 64 20 62 79 20 74 68 65 20 63 6f 64 65 2d  red by the code-
26b1a 67 65 6e 65 72 61 74 6f 72 20 62 65 66 6f 72 65  generator before
26b1b 0a 2a 2a 20 63 6f 64 69 6e 67 20 61 6e 20 49 4e  .** coding an IN
26b1c 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e 20  SERT operation. 
26b1d 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 75 73  The functions us
26b1e 65 64 20 62 79 20 74 68 65 20 55 50 44 41 54 45  ed by the UPDATE
26b1f 2f 44 45 4c 45 54 45 0a 2a 2a 20 67 65 6e 65 72  /DELETE.** gener
26b20 61 74 69 6f 6e 20 63 6f 64 65 20 74 6f 20 71 75  ation code to qu
26b21 65 72 79 20 66 6f 72 20 74 68 69 73 20 69 6e 66  ery for this inf
26b22 6f 72 6d 61 74 69 6f 6e 20 61 72 65 3a 0a 2a 2a  ormation are:.**
26b23 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 6b 52  .**   sqlite3FkR
26b24 65 71 75 69 72 65 64 28 29 20 2d 20 54 65 73 74  equired() - Test
26b25 20 74 6f 20 73 65 65 20 69 66 20 46 4b 20 70 72   to see if FK pr
26b26 6f 63 65 73 73 69 6e 67 20 69 73 20 72 65 71 75  ocessing is requ
26b27 69 72 65 64 2e 0a 2a 2a 20 20 20 73 71 6c 69 74  ired..**   sqlit
26b28 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 29 20 20 2d  e3FkOldmask()  -
26b29 20 51 75 65 72 79 20 66 6f 72 20 74 68 65 20 73   Query for the s
26b2a 65 74 20 6f 66 20 72 65 71 75 69 72 65 64 20 6f  et of required o
26b2b 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a  ld.* columns..**
26b2c 0a 2a 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 6c  .**.** Externall
26b2d 79 20 61 63 63 65 73 73 69 62 6c 65 20 6d 6f 64  y accessible mod
26b2e 75 6c 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ule functions.**
26b2f 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
26b30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b31 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 20  -------.**.**   
26b32 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 29  sqlite3FkCheck()
26b33 20 20 20 20 2d 20 43 68 65 63 6b 20 66 6f 72 20      - Check for 
26b34 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c  foreign key viol
26b35 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 73 71 6c  ations..**   sql
26b36 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 29 20  ite3FkActions() 
26b37 20 2d 20 43 6f 64 65 20 74 72 69 67 67 65 72 73   - Code triggers
26b38 20 66 6f 72 20 4f 4e 20 55 50 44 41 54 45 2f 4f   for ON UPDATE/O
26b39 4e 20 44 45 4c 45 54 45 20 61 63 74 69 6f 6e 73  N DELETE actions
26b3a 2e 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 6b  ..**   sqlite3Fk
26b3b 44 65 6c 65 74 65 28 29 20 20 20 2d 20 44 65 6c  Delete()   - Del
26b3c 65 74 65 20 61 6e 20 46 4b 65 79 20 73 74 72 75  ete an FKey stru
26b3d 63 74 75 72 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  cture..*/../*.**
26b3e 20 56 44 42 45 20 43 61 6c 6c 69 6e 67 20 43 6f   VDBE Calling Co
26b3f 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20 2d 2d 2d 2d  nvention.** ----
26b40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b41 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c  ---.**.** Exampl
26b42 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 6f 72 20 74  e:.**.**   For t
26b43 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 49 4e 53  he following INS
26b44 45 52 54 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a  ERT statement:.*
26b45 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
26b46 54 41 42 4c 45 20 74 31 28 61 2c 20 62 20 49 4e  TABLE t1(a, b IN
26b47 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
26b48 59 2c 20 63 29 3b 0a 2a 2a 20 20 20 20 20 49 4e  Y, c);.**     IN
26b49 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
26b4a 55 45 53 28 31 2c 20 32 2c 20 33 2e 31 29 3b 0a  UES(1, 2, 3.1);.
26b4b 2a 2a 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72  **.**   Register
26b4c 20 28 78 29 3a 20 20 20 20 20 20 20 20 32 20 20   (x):        2  
26b4d 20 20 28 74 79 70 65 20 69 6e 74 65 67 65 72 29    (type integer)
26b4e 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20 28  .**   Register (
26b4f 78 2b 31 29 3a 20 20 20 20 20 20 31 20 20 20 20  x+1):      1    
26b50 28 74 79 70 65 20 69 6e 74 65 67 65 72 29 0a 2a  (type integer).*
26b51 2a 20 20 20 52 65 67 69 73 74 65 72 20 28 78 2b  *   Register (x+
26b52 32 29 3a 20 20 20 20 20 20 4e 55 4c 4c 20 28 74  2):      NULL (t
26b53 79 70 65 20 4e 55 4c 4c 29 0a 2a 2a 20 20 20 52  ype NULL).**   R
26b54 65 67 69 73 74 65 72 20 28 78 2b 33 29 3a 20 20  egister (x+3):  
26b55 20 20 20 20 33 2e 31 20 20 28 74 79 70 65 20 72      3.1  (type r
26b56 65 61 6c 29 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  eal).*/../*.** A
26b57 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
26b58 73 74 72 61 69 6e 74 20 72 65 71 75 69 72 65 73  straint requires
26b59 20 74 68 61 74 20 74 68 65 20 6b 65 79 20 63 6f   that the key co
26b5a 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72  lumns in the par
26b5b 65 6e 74 0a 2a 2a 20 74 61 62 6c 65 20 61 72 65  ent.** table are
26b5c 20 63 6f 6c 6c 65 63 74 69 76 65 6c 79 20 73 75   collectively su
26b5d 62 6a 65 63 74 20 74 6f 20 61 20 55 4e 49 51 55  bject to a UNIQU
26b5e 45 20 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59  E or PRIMARY KEY
26b5f 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2a 20   constraint..** 
26b60 47 69 76 65 6e 20 74 68 61 74 20 70 50 61 72 65  Given that pPare
26b61 6e 74 20 69 73 20 74 68 65 20 70 61 72 65 6e 74  nt is the parent
26b62 20 74 61 62 6c 65 20 66 6f 72 20 66 6f 72 65 69   table for forei
26b63 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
26b64 74 20 70 46 4b 65 79 2c 20 0a 2a 2a 20 73 65 61  t pFKey, .** sea
26b65 72 63 68 20 74 68 65 20 73 63 68 65 6d 61 20 61  rch the schema a
26b66 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e   unique index on
26b67 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20   the parent key 
26b68 63 6f 6c 75 6d 6e 73 2e 20 0a 2a 2a 0a 2a 2a 20  columns. .**.** 
26b69 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 7a  If successful, z
26b6a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ero is returned.
26b6b 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20 6b   If the parent k
26b6c 65 79 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52  ey is an INTEGER
26b6d 20 50 52 49 4d 41 52 59 20 0a 2a 2a 20 4b 45 59   PRIMARY .** KEY
26b6e 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 6f 75   column, then ou
26b6f 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70  tput variable *p
26b70 70 49 64 78 20 69 73 20 73 65 74 20 74 6f 20 4e  pIdx is set to N
26b71 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ULL. Otherwise, 
26b72 2a 70 70 49 64 78 20 0a 2a 2a 20 69 73 20 73 65  *ppIdx .** is se
26b73 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
26b74 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20  e unique index. 
26b75 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70  .** .** If the p
26b76 61 72 65 6e 74 20 6b 65 79 20 63 6f 6e 73 69 73  arent key consis
26b77 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 63  ts of a single c
26b78 6f 6c 75 6d 6e 20 28 74 68 65 20 66 6f 72 65 69  olumn (the forei
26b79 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
26b7a 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20 63 6f  t.** is not a co
26b7b 6d 70 6f 73 69 74 65 20 66 6f 72 65 69 67 6e 20  mposite foreign 
26b7c 6b 65 79 29 2c 20 6f 75 74 70 75 74 20 76 61 72  key), output var
26b7d 69 61 62 6c 65 20 2a 70 61 69 43 6f 6c 20 69 73  iable *paiCol is
26b7e 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a   set to NULL..**
26b7f 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69   Otherwise, it i
26b80 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  s set to point t
26b81 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61  o an allocated a
26b82 72 72 61 79 20 6f 66 20 73 69 7a 65 20 4e 2c 20  rray of size N, 
26b83 77 68 65 72 65 0a 2a 2a 20 4e 20 69 73 20 74 68  where.** N is th
26b84 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
26b85 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e  mns in the paren
26b86 74 20 6b 65 79 2e 20 54 68 65 20 66 69 72 73 74  t key. The first
26b87 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a   element of the.
26b88 2a 2a 20 61 72 72 61 79 20 69 73 20 74 68 65 20  ** array is the 
26b89 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 68 69  index of the chi
26b8a 6c 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  ld table column 
26b8b 74 68 61 74 20 69 73 20 6d 61 70 70 65 64 20 62  that is mapped b
26b8c 79 20 74 68 65 20 46 4b 0a 2a 2a 20 63 6f 6e 73  y the FK.** cons
26b8d 74 72 61 69 6e 74 20 74 6f 20 74 68 65 20 70 61  traint to the pa
26b8e 72 65 6e 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d  rent table colum
26b8f 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  n stored in the 
26b90 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
26b91 0a 2a 2a 20 6f 66 20 69 6e 64 65 78 20 2a 70 70  .** of index *pp
26b92 49 64 78 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  Idx. The second 
26b93 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  element of the a
26b94 72 72 61 79 20 69 73 20 74 68 65 20 69 6e 64 65  rray is the inde
26b95 78 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69 6c  x of the.** chil
26b96 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74  d table column t
26b97 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
26b98 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 65  to the second le
26b99 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  ft-most column o
26b9a 66 0a 2a 2a 20 2a 70 70 49 64 78 2c 20 61 6e 64  f.** *ppIdx, and
26b9b 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66   so on..**.** If
26b9c 20 74 68 65 20 72 65 71 75 69 72 65 64 20 69 6e   the required in
26b9d 64 65 78 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  dex cannot be fo
26b9e 75 6e 64 2c 20 65 69 74 68 65 72 20 62 65 63 61  und, either beca
26b9f 75 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  use:.**.**   1) 
26ba0 54 68 65 20 6e 61 6d 65 64 20 70 61 72 65 6e 74  The named parent
26ba1 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 64 6f 20   key columns do 
26ba2 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 0a 2a 2a  not exist, or.**
26ba3 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 6e 61 6d  .**   2) The nam
26ba4 65 64 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  ed parent key co
26ba5 6c 75 6d 6e 73 20 64 6f 20 65 78 69 73 74 2c 20  lumns do exist, 
26ba6 62 75 74 20 61 72 65 20 6e 6f 74 20 73 75 62 6a  but are not subj
26ba7 65 63 74 20 74 6f 20 61 0a 2a 2a 20 20 20 20 20  ect to a.**     
26ba8 20 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41   UNIQUE or PRIMA
26ba9 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e  RY KEY constrain
26baa 74 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 33 29  t, or.**.**   3)
26bab 20 4e 6f 20 70 61 72 65 6e 74 20 6b 65 79 20 63   No parent key c
26bac 6f 6c 75 6d 6e 73 20 77 65 72 65 20 70 72 6f 76  olumns were prov
26bad 69 64 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20  ided explicitly 
26bae 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a  as part of the.*
26baf 2a 20 20 20 20 20 20 66 6f 72 65 69 67 6e 20 6b  *      foreign k
26bb0 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 61  ey definition, a
26bb1 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  nd the parent ta
26bb2 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76  ble does not hav
26bb3 65 20 61 0a 2a 2a 20 20 20 20 20 20 50 52 49 4d  e a.**      PRIM
26bb4 41 52 59 20 4b 45 59 2c 20 6f 72 0a 2a 2a 0a 2a  ARY KEY, or.**.*
26bb5 2a 20 20 20 34 29 20 4e 6f 20 70 61 72 65 6e 74  *   4) No parent
26bb6 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 77 65 72   key columns wer
26bb7 65 20 70 72 6f 76 69 64 65 64 20 65 78 70 6c 69  e provided expli
26bb8 63 69 74 6c 79 20 61 73 20 70 61 72 74 20 6f 66  citly as part of
26bb9 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 66 6f 72   the.**      for
26bba 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74  eign key definit
26bbb 69 6f 6e 2c 20 61 6e 64 20 74 68 65 20 50 52 49  ion, and the PRI
26bbc 4d 41 52 59 20 4b 45 59 20 6f 66 20 74 68 65 20  MARY KEY of the 
26bbd 70 61 72 65 6e 74 20 74 61 62 6c 65 20 0a 2a 2a  parent table .**
26bbe 20 20 20 20 20 20 63 6f 6e 73 69 73 74 73 20 6f        consists o
26bbf 66 20 61 20 61 20 64 69 66 66 65 72 65 6e 74 20  f a a different 
26bc0 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
26bc1 73 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20 6b  s to the child k
26bc2 65 79 20 69 6e 20 0a 2a 2a 20 20 20 20 20 20 74  ey in .**      t
26bc3 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 0a  he child table..
26bc4 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 6f 6e 2d 7a  **.** then non-z
26bc5 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2c  ero is returned,
26bc6 20 61 6e 64 20 61 20 22 66 6f 72 65 69 67 6e 20   and a "foreign 
26bc7 6b 65 79 20 6d 69 73 6d 61 74 63 68 22 20 65 72  key mismatch" er
26bc8 72 6f 72 20 6c 6f 61 64 65 64 0a 2a 2a 20 69 6e  ror loaded.** in
26bc9 74 6f 20 70 50 61 72 73 65 2e 20 49 66 20 61 6e  to pParse. If an
26bca 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72   OOM error occur
26bcb 73 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72  s, non-zero is r
26bcc 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 0a  eturned and the.
26bcd 2a 2a 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  ** pParse->db->m
26bce 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
26bcf 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74   is set..*/.stat
26bd0 69 63 20 69 6e 74 20 6c 6f 63 61 74 65 46 6b 65  ic int locateFke
26bd1 79 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  yIndex(.  Parse 
26bd2 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
26bd3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
26bd4 73 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74  se context to st
26bd5 6f 72 65 20 61 6e 79 20 65 72 72 6f 72 20 69 6e  ore any error in
26bd6 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 50 61   */.  Table *pPa
26bd7 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20  rent,           
26bd8 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20        /* Parent 
26bd9 74 61 62 6c 65 20 6f 66 20 46 4b 20 63 6f 6e 73  table of FK cons
26bda 74 72 61 69 6e 74 20 70 46 4b 65 79 20 2a 2f 0a  traint pFKey */.
26bdb 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20    FKey *pFKey,  
26bdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bdd 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79    /* Foreign key
26bde 20 74 6f 20 66 69 6e 64 20 69 6e 64 65 78 20 66   to find index f
26bdf 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 2a  or */.  Index **
26be0 70 70 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  ppIdx,          
26be1 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
26be2 55 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20  Unique index on 
26be3 70 61 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a  parent table */.
26be4 20 20 69 6e 74 20 2a 2a 70 61 69 43 6f 6c 20 20    int **paiCol  
26be5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26be6 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 70 20 6f 66    /* OUT: Map of
26be7 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 69   index columns i
26be8 6e 20 70 46 4b 65 79 20 2a 2f 0a 29 7b 0a 20 20  n pFKey */.){.  
26be9 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b  Index *pIdx = 0;
26bea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26beb 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
26bec 72 65 74 75 72 6e 20 76 69 61 20 2a 70 70 49 64  return via *ppId
26bed 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f  x */.  int *aiCo
26bee 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  l = 0;          
26bef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
26bf0 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76 69  lue to return vi
26bf1 61 20 2a 70 61 69 43 6f 6c 20 2a 2f 0a 20 20 69  a *paiCol */.  i
26bf2 6e 74 20 6e 43 6f 6c 20 3d 20 70 46 4b 65 79 2d  nt nCol = pFKey-
26bf3 3e 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  >nCol;          
26bf4 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
26bf5 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 61 72 65 6e  columns in paren
26bf6 74 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72 20  t key */.  char 
26bf7 2a 7a 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 61  *zKey = pFKey->a
26bf8 43 6f 6c 5b 30 5d 2e 7a 43 6f 6c 3b 20 20 20 2f  Col[0].zCol;   /
26bf9 2a 20 4e 61 6d 65 20 6f 66 20 6c 65 66 74 2d 6d  * Name of left-m
26bfa 6f 73 74 20 70 61 72 65 6e 74 20 6b 65 79 20 63  ost parent key c
26bfb 6f 6c 75 6d 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54  olumn */..  /* T
26bfc 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
26bfd 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 7a 65 72  ponsible for zer
26bfe 6f 69 6e 67 20 6f 75 74 70 75 74 20 70 61 72 61  oing output para
26bff 6d 65 74 65 72 73 2e 20 2a 2f 0a 20 20 61 73 73  meters. */.  ass
26c00 65 72 74 28 20 70 70 49 64 78 20 26 26 20 2a 70  ert( ppIdx && *p
26c01 70 49 64 78 3d 3d 30 20 29 3b 0a 20 20 61 73 73  pIdx==0 );.  ass
26c02 65 72 74 28 20 21 70 61 69 43 6f 6c 20 7c 7c 20  ert( !paiCol || 
26c03 2a 70 61 69 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20  *paiCol==0 );.  
26c04 61 73 73 65 72 74 28 20 70 50 61 72 73 65 20 29  assert( pParse )
26c05 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
26c06 69 73 20 61 20 6e 6f 6e 2d 63 6f 6d 70 6f 73 69  is a non-composi
26c07 74 65 20 28 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  te (single colum
26c08 6e 29 20 66 6f 72 65 69 67 6e 20 6b 65 79 2c 20  n) foreign key, 
26c09 63 68 65 63 6b 20 69 66 20 69 74 20 0a 20 20 2a  check if it .  *
26c0a 2a 20 6d 61 70 73 20 74 6f 20 74 68 65 20 49 4e  * maps to the IN
26c0b 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
26c0c 59 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65  Y of table pPare
26c0d 6e 74 2e 20 49 66 20 73 6f 2c 20 6c 65 61 76 65  nt. If so, leave
26c0e 20 2a 70 70 49 64 78 20 0a 20 20 2a 2a 20 61 6e   *ppIdx .  ** an
26c0f 64 20 2a 70 61 69 43 6f 6c 20 73 65 74 20 74 6f  d *paiCol set to
26c10 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
26c11 20 65 61 72 6c 79 2e 20 0a 20 20 2a 2a 0a 20 20   early. .  **.  
26c12 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f  ** Otherwise, fo
26c13 72 20 61 20 63 6f 6d 70 6f 73 69 74 65 20 66 6f  r a composite fo
26c14 72 65 69 67 6e 20 6b 65 79 20 28 6d 6f 72 65 20  reign key (more 
26c15 74 68 61 6e 20 6f 6e 65 20 63 6f 6c 75 6d 6e 29  than one column)
26c16 2c 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20  , allocate.  ** 
26c17 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 61 69  space for the ai
26c18 43 6f 6c 20 61 72 72 61 79 20 28 72 65 74 75 72  Col array (retur
26c19 6e 65 64 20 76 69 61 20 6f 75 74 70 75 74 20 70  ned via output p
26c1a 61 72 61 6d 65 74 65 72 20 2a 70 61 69 43 6f 6c  arameter *paiCol
26c1b 29 2e 0a 20 20 2a 2a 20 4e 6f 6e 2d 63 6f 6d 70  )..  ** Non-comp
26c1c 6f 73 69 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  osite foreign ke
26c1d 79 73 20 64 6f 20 6e 6f 74 20 72 65 71 75 69 72  ys do not requir
26c1e 65 20 74 68 65 20 61 69 43 6f 6c 20 61 72 72 61  e the aiCol arra
26c1f 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 43  y..  */.  if( nC
26c20 6f 6c 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  ol==1 ){.    /* 
26c21 54 68 65 20 46 4b 20 6d 61 70 73 20 74 6f 20 74  The FK maps to t
26c22 68 65 20 49 50 4b 20 69 66 20 61 6e 79 20 6f 66  he IPK if any of
26c23 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
26c24 72 65 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a 0a  re true:.    **.
26c25 20 20 20 20 2a 2a 20 20 20 31 29 20 54 68 65 72      **   1) Ther
26c26 65 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20  e is an INTEGER 
26c27 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75  PRIMARY KEY colu
26c28 6d 6e 20 61 6e 64 20 74 68 65 20 46 4b 20 69 73  mn and the FK is
26c29 20 69 6d 70 6c 69 63 69 74 6c 79 20 0a 20 20 20   implicitly .   
26c2a 20 2a 2a 20 20 20 20 20 20 6d 61 70 70 65 64 20   **      mapped 
26c2b 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  to the primary k
26c2c 65 79 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72  ey of table pPar
26c2d 65 6e 74 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 20  ent, or.    **  
26c2e 20 32 29 20 54 68 65 20 46 4b 20 69 73 20 65 78   2) The FK is ex
26c2f 70 6c 69 63 69 74 6c 79 20 6d 61 70 70 65 64 20  plicitly mapped 
26c30 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c  to a column decl
26c31 61 72 65 64 20 61 73 20 49 4e 54 45 47 45 52 0a  ared as INTEGER.
26c32 20 20 20 20 2a 2a 20 20 20 20 20 20 50 52 49 4d      **      PRIM
26c33 41 52 59 20 4b 45 59 2e 0a 20 20 20 20 2a 2f 0a  ARY KEY..    */.
26c34 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74 2d      if( pParent-
26c35 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20  >iPKey>=0 ){.   
26c36 20 20 20 69 66 28 20 21 7a 4b 65 79 20 29 20 72     if( !zKey ) r
26c37 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 69  eturn 0;.      i
26c38 66 28 20 21 73 71 6c 69 74 65 33 53 74 72 49 43  f( !sqlite3StrIC
26c39 6d 70 28 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c  mp(pParent->aCol
26c3a 5b 70 50 61 72 65 6e 74 2d 3e 69 50 4b 65 79 5d  [pParent->iPKey]
26c3b 2e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29 20 29 20  .zName, zKey) ) 
26c3c 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
26c3d 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 69 43    }else if( paiC
26c3e 6f 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ol ){.    assert
26c3f 28 20 6e 43 6f 6c 3e 31 20 29 3b 0a 20 20 20 20  ( nCol>1 );.    
26c40 61 69 43 6f 6c 20 3d 20 28 69 6e 74 20 2a 29 73  aiCol = (int *)s
26c41 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
26c42 77 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6e 43  w(pParse->db, nC
26c43 6f 6c 2a 73 69 7a 65 6f 66 28 69 6e 74 29 29 3b  ol*sizeof(int));
26c44 0a 20 20 20 20 69 66 28 20 21 61 69 43 6f 6c 20  .    if( !aiCol 
26c45 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
26c46 2a 70 61 69 43 6f 6c 20 3d 20 61 69 43 6f 6c 3b  *paiCol = aiCol;
26c47 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 49 64 78  .  }..  for(pIdx
26c48 3d 70 50 61 72 65 6e 74 2d 3e 70 49 6e 64 65 78  =pParent->pIndex
26c49 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
26c4a 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  x->pNext){.    i
26c4b 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  f( pIdx->nColumn
26c4c 3d 3d 6e 43 6f 6c 20 26 26 20 70 49 64 78 2d 3e  ==nCol && pIdx->
26c4d 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
26c4e 20 29 7b 20 0a 20 20 20 20 20 20 2f 2a 20 70 49   ){ .      /* pI
26c4f 64 78 20 69 73 20 61 20 55 4e 49 51 55 45 20 69  dx is a UNIQUE i
26c50 6e 64 65 78 20 28 6f 72 20 61 20 50 52 49 4d 41  ndex (or a PRIMA
26c51 52 59 20 4b 45 59 29 20 61 6e 64 20 68 61 73 20  RY KEY) and has 
26c52 74 68 65 20 72 69 67 68 74 20 6e 75 6d 62 65 72  the right number
26c53 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 63 6f 6c  .      ** of col
26c54 75 6d 6e 73 2e 20 49 66 20 65 61 63 68 20 69 6e  umns. If each in
26c55 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 63 6f 72  dexed column cor
26c56 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 20 66 6f  responds to a fo
26c57 72 65 69 67 6e 20 6b 65 79 0a 20 20 20 20 20 20  reign key.      
26c58 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 46 4b  ** column of pFK
26c59 65 79 2c 20 74 68 65 6e 20 74 68 69 73 20 69 6e  ey, then this in
26c5a 64 65 78 20 69 73 20 61 20 77 69 6e 6e 65 72 2e  dex is a winner.
26c5b 20 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 20    */..      if( 
26c5c 7a 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  zKey==0 ){.     
26c5d 20 20 20 2f 2a 20 49 66 20 7a 4b 65 79 20 69 73     /* If zKey is
26c5e 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73   NULL, then this
26c5f 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20   foreign key is 
26c60 69 6d 70 6c 69 63 69 74 6c 79 20 6d 61 70 70 65  implicitly mappe
26c61 64 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a  d to .        **
26c62 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
26c63 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65 6e   of table pParen
26c64 74 2e 20 54 68 65 20 50 52 49 4d 41 52 59 20 4b  t. The PRIMARY K
26c65 45 59 20 69 6e 64 65 78 20 6d 61 79 20 62 65 20  EY index may be 
26c66 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 64 65 6e  .        ** iden
26c67 74 69 66 69 65 64 20 62 79 20 74 68 65 20 74 65  tified by the te
26c68 73 74 20 28 49 6e 64 65 78 2e 61 75 74 6f 49 6e  st (Index.autoIn
26c69 64 65 78 3d 3d 32 29 2e 20 20 2a 2f 0a 20 20 20  dex==2).  */.   
26c6a 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
26c6b 75 74 6f 49 6e 64 65 78 3d 3d 32 20 29 7b 0a 20  utoIndex==2 ){. 
26c6c 20 20 20 20 20 20 20 20 20 69 66 28 20 61 69 43           if( aiC
26c6d 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ol ){.          
26c6e 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20    int i;.       
26c6f 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
26c70 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61 69 43 6f 6c  nCol; i++) aiCol
26c71 5b 69 5d 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f  [i] = pFKey->aCo
26c72 6c 5b 69 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20  l[i].iFrom;.    
26c73 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
26c74 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
26c75 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
26c76 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 7a 4b          /* If zK
26c77 65 79 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20  ey is non-NULL, 
26c78 74 68 65 6e 20 74 68 69 73 20 66 6f 72 65 69 67  then this foreig
26c79 6e 20 6b 65 79 20 77 61 73 20 64 65 63 6c 61 72  n key was declar
26c7a 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a  ed to.        **
26c7b 20 6d 61 70 20 74 6f 20 61 6e 20 65 78 70 6c 69   map to an expli
26c7c 63 69 74 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75  cit list of colu
26c7d 6d 6e 73 20 69 6e 20 74 61 62 6c 65 20 70 50 61  mns in table pPa
26c7e 72 65 6e 74 2e 20 43 68 65 63 6b 20 69 66 20 74  rent. Check if t
26c7f 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  his.        ** i
26c80 6e 64 65 78 20 6d 61 74 63 68 65 73 20 74 68 6f  ndex matches tho
26c81 73 65 20 63 6f 6c 75 6d 6e 73 2e 20 41 6c 73 6f  se columns. Also
26c82 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65  , check that the
26c83 20 69 6e 64 65 78 20 75 73 65 73 0a 20 20 20 20   index uses.    
26c84 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75      ** the defau
26c85 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  lt collation seq
26c86 75 65 6e 63 65 73 20 66 6f 72 20 65 61 63 68 20  uences for each 
26c87 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 20 20 20  column. */.     
26c88 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20     int i, j;.   
26c89 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
26c8a 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
26c8b 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d        int iCol =
26c8c 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
26c8d 69 5d 3b 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  i];     /* Index
26c8e 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 61   of column in pa
26c8f 72 65 6e 74 20 74 62 6c 20 2a 2f 0a 20 20 20 20  rent tbl */.    
26c90 20 20 20 20 20 20 63 68 61 72 20 2a 7a 44 66 6c        char *zDfl
26c91 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  tColl;          
26c92 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 2e 20          /* Def. 
26c93 63 6f 6c 6c 61 74 69 6f 6e 20 66 6f 72 20 63 6f  collation for co
26c94 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  lumn */.        
26c95 20 20 63 68 61 72 20 2a 7a 49 64 78 43 6f 6c 3b    char *zIdxCol;
26c96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c97 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 69      /* Name of i
26c98 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f  ndexed column */
26c99 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ..          /* I
26c9a 66 20 74 68 65 20 69 6e 64 65 78 20 75 73 65 73  f the index uses
26c9b 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71   a collation seq
26c9c 75 65 6e 63 65 20 74 68 61 74 20 69 73 20 64 69  uence that is di
26c9d 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 20 20 20  fferent from.   
26c9e 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65         ** the de
26c9f 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20  fault collation 
26ca0 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65  sequence for the
26ca1 20 63 6f 6c 75 6d 6e 2c 20 74 68 69 73 20 69 6e   column, this in
26ca2 64 65 78 20 69 73 0a 20 20 20 20 20 20 20 20 20  dex is.         
26ca3 20 2a 2a 20 75 6e 75 73 61 62 6c 65 2e 20 42 61   ** unusable. Ba
26ca4 69 6c 20 6f 75 74 20 65 61 72 6c 79 20 69 6e 20  il out early in 
26ca5 74 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20  this case.  */. 
26ca6 20 20 20 20 20 20 20 20 20 7a 44 66 6c 74 43 6f           zDfltCo
26ca7 6c 6c 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 43  ll = pParent->aC
26ca8 6f 6c 5b 69 43 6f 6c 5d 2e 7a 43 6f 6c 6c 3b 0a  ol[iCol].zColl;.
26ca9 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 7a            if( !z
26caa 44 66 6c 74 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  DfltColl ){.    
26cab 20 20 20 20 20 20 20 20 7a 44 66 6c 74 43 6f 6c          zDfltCol
26cac 6c 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20  l = "BINARY";.  
26cad 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
26cae 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
26caf 74 72 49 43 6d 70 28 70 49 64 78 2d 3e 61 7a 43  trICmp(pIdx->azC
26cb0 6f 6c 6c 5b 69 5d 2c 20 7a 44 66 6c 74 43 6f 6c  oll[i], zDfltCol
26cb1 6c 29 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20  l) ) break;..   
26cb2 20 20 20 20 20 20 20 7a 49 64 78 43 6f 6c 20 3d         zIdxCol =
26cb3 20 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 69   pParent->aCol[i
26cb4 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Col].zName;.    
26cb5 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
26cb6 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  <nCol; j++){.   
26cb7 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
26cb8 69 74 65 33 53 74 72 49 43 6d 70 28 70 46 4b 65  ite3StrICmp(pFKe
26cb9 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 2c  y->aCol[j].zCol,
26cba 20 7a 49 64 78 43 6f 6c 29 3d 3d 30 20 29 7b 0a   zIdxCol)==0 ){.
26cbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
26cbc 28 20 61 69 43 6f 6c 20 29 20 61 69 43 6f 6c 5b  ( aiCol ) aiCol[
26cbd 69 5d 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  i] = pFKey->aCol
26cbe 5b 6a 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20  [j].iFrom;.     
26cbf 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
26cc0 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
26cc1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
26cc2 20 20 20 20 69 66 28 20 6a 3d 3d 6e 43 6f 6c 20      if( j==nCol 
26cc3 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
26cc4 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69   }.        if( i
26cc5 3d 3d 6e 43 6f 6c 20 29 20 62 72 65 61 6b 3b 20  ==nCol ) break; 
26cc6 20 20 20 20 20 2f 2a 20 70 49 64 78 20 69 73 20       /* pIdx is 
26cc7 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20  usable */.      
26cc8 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  }.    }.  }..  i
26cc9 66 28 20 21 70 49 64 78 20 29 7b 0a 20 20 20 20  f( !pIdx ){.    
26cca 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64 69 73  if( !pParse->dis
26ccb 61 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b 0a  ableTriggers ){.
26ccc 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
26ccd 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 66  orMsg(pParse, "f
26cce 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61  oreign key misma
26ccf 74 63 68 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20  tch");.    }.   
26cd0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
26cd1 50 61 72 73 65 2d 3e 64 62 2c 20 61 69 43 6f 6c  Parse->db, aiCol
26cd2 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
26cd3 0a 20 20 7d 0a 0a 20 20 2a 70 70 49 64 78 20 3d  .  }..  *ppIdx =
26cd4 20 70 49 64 78 3b 0a 20 20 72 65 74 75 72 6e 20   pIdx;.  return 
26cd5 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
26cd6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
26cd7 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 77 20 69  led when a row i
26cd8 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  s inserted into 
26cd9 6f 72 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20  or deleted from 
26cda 74 68 65 20 0a 2a 2a 20 63 68 69 6c 64 20 74 61  the .** child ta
26cdb 62 6c 65 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b  ble of foreign k
26cdc 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 46  ey constraint pF
26cdd 4b 65 79 2e 20 49 66 20 61 6e 20 53 51 4c 20 55  Key. If an SQL U
26cde 50 44 41 54 45 20 69 73 20 65 78 65 63 75 74 65  PDATE is execute
26cdf 64 20 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 68 69  d .** on the chi
26ce0 6c 64 20 74 61 62 6c 65 20 6f 66 20 70 46 4b 65  ld table of pFKe
26ce1 79 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  y, this function
26ce2 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 77 69 63   is invoked twic
26ce3 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 0a 2a  e for each row.*
26ce4 2a 20 61 66 66 65 63 74 65 64 20 2d 20 6f 6e 63  * affected - onc
26ce5 65 20 74 6f 20 22 64 65 6c 65 74 65 22 20 74 68  e to "delete" th
26ce6 65 20 6f 6c 64 20 72 6f 77 2c 20 61 6e 64 20 74  e old row, and t
26ce7 68 65 6e 20 61 67 61 69 6e 20 74 6f 20 22 69 6e  hen again to "in
26ce8 73 65 72 74 22 20 74 68 65 0a 2a 2a 20 6e 65 77  sert" the.** new
26ce9 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68   row..**.** Each
26cea 20 74 69 6d 65 20 69 74 20 69 73 20 63 61 6c 6c   time it is call
26ceb 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ed, this functio
26cec 6e 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45  n generates VDBE
26ced 20 63 6f 64 65 20 74 6f 20 6c 6f 63 61 74 65 20   code to locate 
26cee 74 68 65 0a 2a 2a 20 72 6f 77 20 69 6e 20 74 68  the.** row in th
26cef 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 74  e parent table t
26cf0 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
26cf1 74 6f 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  to the row being
26cf2 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 0a   inserted into .
26cf3 2a 2a 20 6f 72 20 64 65 6c 65 74 65 64 20 66 72  ** or deleted fr
26cf4 6f 6d 20 74 68 65 20 63 68 69 6c 64 20 74 61 62  om the child tab
26cf5 6c 65 2e 20 49 66 20 74 68 65 20 70 61 72 65 6e  le. If the paren
26cf6 74 20 72 6f 77 20 63 61 6e 20 62 65 20 66 6f 75  t row can be fou
26cf7 6e 64 2c 20 6e 6f 20 0a 2a 2a 20 73 70 65 63 69  nd, no .** speci
26cf8 61 6c 20 61 63 74 69 6f 6e 20 69 73 20 74 61 6b  al action is tak
26cf9 65 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  en. Otherwise, i
26cfa 66 20 74 68 65 20 70 61 72 65 6e 74 20 72 6f 77  f the parent row
26cfb 20 63 61 6e 20 2a 6e 6f 74 2a 20 62 65 0a 2a 2a   can *not* be.**
26cfc 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 70 61   found in the pa
26cfd 72 65 6e 74 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a  rent table:.**.*
26cfe 2a 20 20 20 4f 70 65 72 61 74 69 6f 6e 20 7c 20  *   Operation | 
26cff 46 4b 20 74 79 70 65 20 20 20 7c 20 41 63 74 69  FK type   | Acti
26d00 6f 6e 20 74 61 6b 65 6e 0a 2a 2a 20 20 20 2d 2d  on taken.**   --
26d01 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d02 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d03 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d04 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d05 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 49 4e  --------.**   IN
26d06 53 45 52 54 20 20 20 20 20 20 69 6d 6d 65 64 69  SERT      immedi
26d07 61 74 65 20 20 20 49 6e 63 72 65 6d 65 6e 74 20  ate   Increment 
26d08 74 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63  the "immediate c
26d09 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65  onstraint counte
26d0a 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45  r"..**.**   DELE
26d0b 54 45 20 20 20 20 20 20 69 6d 6d 65 64 69 61 74  TE      immediat
26d0c 65 20 20 20 44 65 63 72 65 6d 65 6e 74 20 74 68  e   Decrement th
26d0d 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e  e "immediate con
26d0e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22  straint counter"
26d0f 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52 54  ..**.**   INSERT
26d10 20 20 20 20 20 20 64 65 66 65 72 72 65 64 20 20        deferred  
26d11 20 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20    Increment the 
26d12 22 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72  "deferred constr
26d13 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a  aint counter"..*
26d14 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45 20 20 20  *.**   DELETE   
26d15 20 20 20 64 65 66 65 72 72 65 64 20 20 20 20 44     deferred    D
26d16 65 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64 65  ecrement the "de
26d17 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
26d18 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a  t counter"..**.*
26d19 2a 20 54 68 65 73 65 20 6f 70 65 72 61 74 69 6f  * These operatio
26d1a 6e 73 20 61 72 65 20 69 64 65 6e 74 69 66 69 65  ns are identifie
26d1b 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74  d in the comment
26d1c 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74   at the top of t
26d1d 68 69 73 20 66 69 6c 65 20 0a 2a 2a 20 28 66 6b  his file .** (fk
26d1e 65 79 2e 63 29 20 61 73 20 22 49 2e 31 22 20 61  ey.c) as "I.1" a
26d1f 6e 64 20 22 44 2e 31 22 2e 0a 2a 2f 0a 73 74 61  nd "D.1"..*/.sta
26d20 74 69 63 20 76 6f 69 64 20 66 6b 4c 6f 6f 6b 75  tic void fkLooku
26d21 70 50 61 72 65 6e 74 28 0a 20 20 50 61 72 73 65  pParent(.  Parse
26d22 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
26d23 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
26d24 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20  t */.  int iDb, 
26d25 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26d26 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73  Index of databas
26d27 65 20 68 6f 75 73 69 6e 67 20 70 54 61 62 20 2a  e housing pTab *
26d28 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
26d29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
26d2a 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 46 4b 20  ent table of FK 
26d2b 70 46 4b 65 79 20 2a 2f 0a 20 20 49 6e 64 65 78  pFKey */.  Index
26d2c 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20 20   *pIdx,         
26d2d 20 2f 2a 20 55 6e 69 71 75 65 20 69 6e 64 65 78   /* Unique index
26d2e 20 6f 6e 20 70 61 72 65 6e 74 20 6b 65 79 20 63   on parent key c
26d2f 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 61 62 20 2a  olumns in pTab *
26d30 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c  /.  FKey *pFKey,
26d31 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
26d32 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
26d33 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69  int */.  int *ai
26d34 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Col,           /
26d35 2a 20 4d 61 70 20 66 72 6f 6d 20 70 61 72 65 6e  * Map from paren
26d36 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 74 6f  t key columns to
26d37 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c   child table col
26d38 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65  umns */.  int re
26d39 67 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 20  gData,          
26d3a 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 72  /* Address of ar
26d3b 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 63  ray containing c
26d3c 68 69 6c 64 20 74 61 62 6c 65 20 72 6f 77 20 2a  hild table row *
26d3d 2f 0a 20 20 69 6e 74 20 6e 49 6e 63 72 2c 20 20  /.  int nIncr,  
26d3e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63            /* Inc
26d3f 72 65 6d 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e  rement constrain
26d40 74 20 63 6f 75 6e 74 65 72 20 62 79 20 74 68 69  t counter by thi
26d41 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 67 6e  s */.  int isIgn
26d42 6f 72 65 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ore          /* 
26d43 49 66 20 74 72 75 65 2c 20 70 72 65 74 65 6e 64  If true, pretend
26d44 20 70 54 61 62 20 63 6f 6e 74 61 69 6e 73 20 61   pTab contains a
26d45 6c 6c 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 2a  ll NULL values *
26d46 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
26d47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d49 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
26d4a 69 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20  iable */.  Vdbe 
26d4b 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
26d4c 64 62 65 28 70 50 61 72 73 65 29 3b 20 20 20 20  dbe(pParse);    
26d4d 20 20 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20       /* Vdbe to 
26d4e 61 64 64 20 63 6f 64 65 20 74 6f 20 2a 2f 0a 20  add code to */. 
26d4f 20 69 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72   int iCur = pPar
26d50 73 65 2d 3e 6e 54 61 62 20 2d 20 31 3b 20 20 20  se->nTab - 1;   
26d51 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
26d52 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f 20 75  rsor number to u
26d53 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 6b 20  se */.  int iOk 
26d54 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
26d55 65 4c 61 62 65 6c 28 76 29 3b 20 20 20 20 20 20  eLabel(v);      
26d56 20 20 2f 2a 20 6a 75 6d 70 20 68 65 72 65 20 69    /* jump here i
26d57 66 20 70 61 72 65 6e 74 20 6b 65 79 20 66 6f 75  f parent key fou
26d58 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 6e  nd */..  /* If n
26d59 49 6e 63 72 20 69 73 20 6c 65 73 73 20 74 68 61  Incr is less tha
26d5a 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 63 68 65  n zero, then che
26d5b 63 6b 20 61 74 20 72 75 6e 74 69 6d 65 20 69 66  ck at runtime if
26d5c 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 0a 20   there are any. 
26d5d 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   ** outstanding 
26d5e 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 72  constraints to r
26d5f 65 73 6f 6c 76 65 2e 20 49 66 20 74 68 65 72 65  esolve. If there
26d60 20 61 72 65 20 6e 6f 74 2c 20 74 68 65 72 65 20   are not, there 
26d61 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20  is no need.  ** 
26d62 74 6f 20 63 68 65 63 6b 20 69 66 20 64 65 6c 65  to check if dele
26d63 74 69 6e 67 20 74 68 69 73 20 72 6f 77 20 72 65  ting this row re
26d64 73 6f 6c 76 65 73 20 61 6e 79 20 6f 75 74 73 74  solves any outst
26d65 61 6e 64 69 6e 67 20 76 69 6f 6c 61 74 69 6f 6e  anding violation
26d66 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 68 65  s..  **.  ** Che
26d67 63 6b 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65  ck if any of the
26d68 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   key columns in 
26d69 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
26d6a 72 6f 77 20 61 72 65 20 4e 55 4c 4c 2e 20 49 66  row are NULL. If
26d6b 20 0a 20 20 2a 2a 20 61 6e 79 20 61 72 65 2c 20   .  ** any are, 
26d6c 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61  then the constra
26d6d 69 6e 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65  int is considere
26d6e 64 20 73 61 74 69 73 66 69 65 64 2e 20 4e 6f 20  d satisfied. No 
26d6f 6e 65 65 64 20 74 6f 20 0a 20 20 2a 2a 20 73 65  need to .  ** se
26d70 61 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68  arch for a match
26d71 69 6e 67 20 72 6f 77 20 69 6e 20 74 68 65 20 70  ing row in the p
26d72 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 2a 2f  arent table.  */
26d73 0a 20 20 69 66 28 20 6e 49 6e 63 72 3c 30 20 29  .  if( nIncr<0 )
26d74 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
26d75 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b  eAddOp2(v, OP_Fk
26d76 49 66 5a 65 72 6f 2c 20 70 46 4b 65 79 2d 3e 69  IfZero, pFKey->i
26d77 73 44 65 66 65 72 72 65 64 2c 20 69 4f 6b 29 3b  sDeferred, iOk);
26d78 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
26d79 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69  i<pFKey->nCol; i
26d7a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 52 65  ++){.    int iRe
26d7b 67 20 3d 20 61 69 43 6f 6c 5b 69 5d 20 2b 20 72  g = aiCol[i] + r
26d7c 65 67 44 61 74 61 20 2b 20 31 3b 0a 20 20 20 20  egData + 1;.    
26d7d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26d7e 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  2(v, OP_IsNull, 
26d7f 69 52 65 67 2c 20 69 4f 6b 29 3b 0a 20 20 7d 0a  iReg, iOk);.  }.
26d80 0a 20 20 69 66 28 20 69 73 49 67 6e 6f 72 65 3d  .  if( isIgnore=
26d81 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49  =0 ){.    if( pI
26d82 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  dx==0 ){.      /
26d83 2a 20 49 66 20 70 49 64 78 20 69 73 20 4e 55 4c  * If pIdx is NUL
26d84 4c 2c 20 74 68 65 6e 20 74 68 65 20 70 61 72 65  L, then the pare
26d85 6e 74 20 6b 65 79 20 69 73 20 74 68 65 20 49 4e  nt key is the IN
26d86 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
26d87 59 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  Y.      ** colum
26d88 6e 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  n of the parent 
26d89 74 61 62 6c 65 20 28 74 61 62 6c 65 20 70 54 61  table (table pTa
26d8a 62 29 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e  b).  */.      in
26d8b 74 20 69 4d 75 73 74 42 65 49 6e 74 3b 20 20 20  t iMustBeInt;   
26d8c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
26d8d 64 64 72 65 73 73 20 6f 66 20 4d 75 73 74 42 65  ddress of MustBe
26d8e 49 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  Int instruction 
26d8f 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67  */.      int reg
26d90 54 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 47 65  Temp = sqlite3Ge
26d91 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
26d92 3b 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 49 6e  ;.  .      /* In
26d93 76 6f 6b 65 20 4d 75 73 74 42 65 49 6e 74 20 74  voke MustBeInt t
26d94 6f 20 63 6f 65 72 63 65 20 74 68 65 20 63 68 69  o coerce the chi
26d95 6c 64 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20  ld key value to 
26d96 61 6e 20 69 6e 74 65 67 65 72 20 28 69 2e 65 2e  an integer (i.e.
26d97 20 0a 20 20 20 20 20 20 2a 2a 20 61 70 70 6c 79   .      ** apply
26d98 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
26d99 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 29   the parent key)
26d9a 2e 20 49 66 20 74 68 69 73 20 66 61 69 6c 73 2c  . If this fails,
26d9b 20 74 68 65 6e 20 74 68 65 72 65 0a 20 20 20 20   then there.    
26d9c 20 20 2a 2a 20 69 73 20 6e 6f 20 6d 61 74 63 68    ** is no match
26d9d 69 6e 67 20 70 61 72 65 6e 74 20 6b 65 79 2e 20  ing parent key. 
26d9e 42 65 66 6f 72 65 20 75 73 69 6e 67 20 4d 75 73  Before using Mus
26d9f 74 42 65 49 6e 74 2c 20 6d 61 6b 65 20 61 20 63  tBeInt, make a c
26da0 6f 70 79 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20  opy of.      ** 
26da1 74 68 65 20 76 61 6c 75 65 2e 20 4f 74 68 65 72  the value. Other
26da2 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75 65 20  wise, the value 
26da3 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68  inserted into th
26da4 65 20 63 68 69 6c 64 20 6b 65 79 20 63 6f 6c 75  e child key colu
26da5 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c  mn.      ** will
26da6 20 68 61 76 65 20 49 4e 54 45 47 45 52 20 61 66   have INTEGER af
26da7 66 69 6e 69 74 79 20 61 70 70 6c 69 65 64 20 74  finity applied t
26da8 6f 20 69 74 2c 20 77 68 69 63 68 20 6d 61 79 20  o it, which may 
26da9 6e 6f 74 20 62 65 20 63 6f 72 72 65 63 74 2e 20  not be correct. 
26daa 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
26dab 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26dac 50 5f 53 43 6f 70 79 2c 20 61 69 43 6f 6c 5b 30  P_SCopy, aiCol[0
26dad 5d 2b 31 2b 72 65 67 44 61 74 61 2c 20 72 65 67  ]+1+regData, reg
26dae 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 69 4d 75  Temp);.      iMu
26daf 73 74 42 65 49 6e 74 20 3d 20 73 71 6c 69 74 65  stBeInt = sqlite
26db0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26db1 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67  P_MustBeInt, reg
26db2 54 65 6d 70 2c 20 30 29 3b 0a 20 20 0a 20 20 20  Temp, 0);.  .   
26db3 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 72     /* If the par
26db4 65 6e 74 20 74 61 62 6c 65 20 69 73 20 74 68 65  ent table is the
26db5 20 73 61 6d 65 20 61 73 20 74 68 65 20 63 68 69   same as the chi
26db6 6c 64 20 74 61 62 6c 65 2c 20 61 6e 64 20 77 65  ld table, and we
26db7 20 61 72 65 20 61 62 6f 75 74 0a 20 20 20 20 20   are about.     
26db8 20 2a 2a 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74   ** to increment
26db9 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d   the constraint-
26dba 63 6f 75 6e 74 65 72 20 28 69 2e 65 2e 20 74 68  counter (i.e. th
26dbb 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
26dbc 6f 70 65 72 61 74 69 6f 6e 29 2c 0a 20 20 20 20  operation),.    
26dbd 20 20 2a 2a 20 74 68 65 6e 20 63 68 65 63 6b 20    ** then check 
26dbe 69 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  if the row being
26dbf 20 69 6e 73 65 72 74 65 64 20 6d 61 74 63 68 65   inserted matche
26dc0 73 20 69 74 73 65 6c 66 2e 20 49 66 20 73 6f 2c  s itself. If so,
26dc1 20 64 6f 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a   do not.      **
26dc2 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63   increment the c
26dc3 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65  onstraint-counte
26dc4 72 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  r.  */.      if(
26dc5 20 70 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46   pTab==pFKey->pF
26dc6 72 6f 6d 20 26 26 20 6e 49 6e 63 72 3d 3d 31 20  rom && nIncr==1 
26dc7 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
26dc8 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
26dc9 4f 50 5f 45 71 2c 20 72 65 67 44 61 74 61 2c 20  OP_Eq, regData, 
26dca 69 4f 6b 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20  iOk, regTemp);. 
26dcb 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20       }.  .      
26dcc 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
26dcd 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69  (pParse, iCur, i
26dce 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65  Db, pTab, OP_Ope
26dcf 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 73 71  nRead);.      sq
26dd0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
26dd1 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
26dd2 20 69 43 75 72 2c 20 30 2c 20 72 65 67 54 65 6d   iCur, 0, regTem
26dd3 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
26dd4 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26dd5 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 4f 6b 29 3b  P_Goto, 0, iOk);
26dd6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26dd7 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 73 71  beJumpHere(v, sq
26dd8 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
26dd9 41 64 64 72 28 76 29 2d 32 29 3b 0a 20 20 20 20  Addr(v)-2);.    
26dda 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
26ddb 70 48 65 72 65 28 76 2c 20 69 4d 75 73 74 42 65  pHere(v, iMustBe
26ddc 49 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Int);.      sqli
26ddd 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
26dde 67 28 70 50 61 72 73 65 2c 20 72 65 67 54 65 6d  g(pParse, regTem
26ddf 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  p);.    }else{. 
26de0 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20       int nCol = 
26de1 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 0a 20 20 20  pFKey->nCol;.   
26de2 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 20 3d     int regTemp =
26de3 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
26de4 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f  ange(pParse, nCo
26de5 6c 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65  l);.      int re
26de6 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65  gRec = sqlite3Ge
26de7 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
26de8 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20  ;.      KeyInfo 
26de9 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49  *pKey = sqlite3I
26dea 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
26deb 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 0a 20 20  se, pIdx);.  .  
26dec 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26ded 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
26dee 52 65 61 64 2c 20 69 43 75 72 2c 20 70 49 64 78  Read, iCur, pIdx
26def 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20  ->tnum, iDb);.  
26df0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
26df1 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28  hangeP4(v, -1, (
26df2 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
26df3 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
26df4 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
26df5 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
26df6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26df7 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
26df8 6f 70 79 2c 20 61 69 43 6f 6c 5b 69 5d 2b 31 2b  opy, aiCol[i]+1+
26df9 72 65 67 44 61 74 61 2c 20 72 65 67 54 65 6d 70  regData, regTemp
26dfa 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a  +i);.      }.  .
26dfb 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
26dfc 70 61 72 65 6e 74 20 74 61 62 6c 65 20 69 73 20  parent table is 
26dfd 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
26dfe 63 68 69 6c 64 20 74 61 62 6c 65 2c 20 61 6e 64  child table, and
26dff 20 77 65 20 61 72 65 20 61 62 6f 75 74 0a 20 20   we are about.  
26e00 20 20 20 20 2a 2a 20 74 6f 20 69 6e 63 72 65 6d      ** to increm
26e01 65 6e 74 20 74 68 65 20 63 6f 6e 73 74 72 61 69  ent the constrai
26e02 6e 74 2d 63 6f 75 6e 74 65 72 20 28 69 2e 65 2e  nt-counter (i.e.
26e03 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45   this is an INSE
26e04 52 54 20 6f 70 65 72 61 74 69 6f 6e 29 2c 0a 20  RT operation),. 
26e05 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 68 65       ** then che
26e06 63 6b 20 69 66 20 74 68 65 20 72 6f 77 20 62 65  ck if the row be
26e07 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6d 61 74  ing inserted mat
26e08 63 68 65 73 20 69 74 73 65 6c 66 2e 20 49 66 20  ches itself. If 
26e09 73 6f 2c 20 64 6f 20 6e 6f 74 0a 20 20 20 20 20  so, do not.     
26e0a 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 74 68   ** increment th
26e0b 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75  e constraint-cou
26e0c 6e 74 65 72 2e 20 20 2a 2f 0a 20 20 20 20 20 20  nter.  */.      
26e0d 69 66 28 20 70 54 61 62 3d 3d 70 46 4b 65 79 2d  if( pTab==pFKey-
26e0e 3e 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72 3d  >pFrom && nIncr=
26e0f 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  =1 ){.        in
26e10 74 20 69 4a 75 6d 70 20 3d 20 73 71 6c 69 74 65  t iJump = sqlite
26e11 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
26e12 28 76 29 20 2b 20 6e 43 6f 6c 20 2b 20 31 3b 0a  (v) + nCol + 1;.
26e13 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
26e14 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
26e15 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 68           int iCh
26e16 69 6c 64 20 3d 20 61 69 43 6f 6c 5b 69 5d 2b 31  ild = aiCol[i]+1
26e17 2b 72 65 67 44 61 74 61 3b 0a 20 20 20 20 20 20  +regData;.      
26e18 20 20 20 20 69 6e 74 20 69 50 61 72 65 6e 74 20      int iParent 
26e19 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
26e1a 5b 69 5d 2b 31 2b 72 65 67 44 61 74 61 3b 0a 20  [i]+1+regData;. 
26e1b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
26e1c 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
26e1d 5f 4e 65 2c 20 69 43 68 69 6c 64 2c 20 69 4a 75  _Ne, iChild, iJu
26e1e 6d 70 2c 20 69 50 61 72 65 6e 74 29 3b 0a 20 20  mp, iParent);.  
26e1f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
26e20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26e21 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
26e22 20 69 4f 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20   iOk);.      }. 
26e23 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56   .      sqlite3V
26e24 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26e25 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54  MakeRecord, regT
26e26 65 6d 70 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65  emp, nCol, regRe
26e27 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  c);.      sqlite
26e28 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
26e29 20 2d 31 2c 20 73 71 6c 69 74 65 33 49 6e 64 65   -1, sqlite3Inde
26e2a 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20  xAffinityStr(v, 
26e2b 70 49 64 78 29 2c 20 30 29 3b 0a 20 20 20 20 20  pIdx), 0);.     
26e2c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26e2d 70 34 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e  p4Int(v, OP_Foun
26e2e 64 2c 20 69 43 75 72 2c 20 69 4f 6b 2c 20 72 65  d, iCur, iOk, re
26e2f 67 52 65 63 2c 20 30 29 3b 0a 20 20 0a 20 20 20  gRec, 0);.  .   
26e30 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
26e31 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
26e32 20 72 65 67 52 65 63 29 3b 0a 20 20 20 20 20 20   regRec);.      
26e33 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
26e34 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
26e35 72 65 67 54 65 6d 70 2c 20 6e 43 6f 6c 29 3b 0a  regTemp, nCol);.
26e36 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
26e37 20 21 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72   !pFKey->isDefer
26e38 72 65 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e  red && !pParse->
26e39 70 54 6f 70 6c 65 76 65 6c 20 26 26 20 21 70 50  pToplevel && !pP
26e3a 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69  arse->isMultiWri
26e3b 74 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70 65  te ){.    /* Spe
26e3c 63 69 61 6c 20 63 61 73 65 3a 20 49 66 20 74 68  cial case: If th
26e3d 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
26e3e 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77  statement that w
26e3f 69 6c 6c 20 69 6e 73 65 72 74 20 65 78 61 63 74  ill insert exact
26e40 6c 79 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 72 6f  ly.    ** one ro
26e41 77 20 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65  w into the table
26e42 2c 20 72 61 69 73 65 20 61 20 63 6f 6e 73 74 72  , raise a constr
26e43 61 69 6e 74 20 69 6d 6d 65 64 69 61 74 65 6c 79  aint immediately
26e44 20 69 6e 73 74 65 61 64 20 6f 66 0a 20 20 20 20   instead of.    
26e45 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  ** incrementing 
26e46 61 20 63 6f 75 6e 74 65 72 2e 20 54 68 69 73 20  a counter. This 
26e47 69 73 20 6e 65 63 65 73 73 61 72 79 20 61 73 20  is necessary as 
26e48 74 68 65 20 56 4d 20 63 6f 64 65 20 69 73 20 62  the VM code is b
26e49 65 69 6e 67 0a 20 20 20 20 2a 2a 20 67 65 6e 65  eing.    ** gene
26e4a 72 61 74 65 64 20 66 6f 72 20 77 69 6c 6c 20 6e  rated for will n
26e4b 6f 74 20 6f 70 65 6e 20 61 20 73 74 61 74 65 6d  ot open a statem
26e4c 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
26e4d 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
26e4e 20 6e 49 6e 63 72 3d 3d 31 20 29 3b 0a 20 20 20   nIncr==1 );.   
26e4f 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73   sqlite3HaltCons
26e50 74 72 61 69 6e 74 28 0a 20 20 20 20 20 20 20 20  traint(.        
26e51 70 50 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74  pParse, OE_Abort
26e52 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  , "foreign key c
26e53 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
26e54 22 2c 20 50 34 5f 53 54 41 54 49 43 0a 20 20 20  ", P4_STATIC.   
26e55 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
26e56 20 69 66 28 20 6e 49 6e 63 72 3e 30 20 26 26 20   if( nIncr>0 && 
26e57 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
26e58 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  d==0 ){.      sq
26e59 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76  lite3ParseToplev
26e5a 65 6c 28 70 50 61 72 73 65 29 2d 3e 6d 61 79 41  el(pParse)->mayA
26e5b 62 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  bort = 1;.    }.
26e5c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26e5d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 43 6f  ddOp2(v, OP_FkCo
26e5e 75 6e 74 65 72 2c 20 70 46 4b 65 79 2d 3e 69 73  unter, pFKey->is
26e5f 44 65 66 65 72 72 65 64 2c 20 6e 49 6e 63 72 29  Deferred, nIncr)
26e60 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
26e61 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
26e62 28 76 2c 20 69 4f 6b 29 3b 0a 20 20 73 71 6c 69  (v, iOk);.  sqli
26e63 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
26e64 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29   OP_Close, iCur)
26e65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
26e66 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
26e67 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63  ed to generate c
26e68 6f 64 65 20 65 78 65 63 75 74 65 64 20 77 68 65  ode executed whe
26e69 6e 20 61 20 72 6f 77 20 69 73 20 64 65 6c 65 74  n a row is delet
26e6a 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70  ed.** from the p
26e6b 61 72 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 66  arent table of f
26e6c 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
26e6d 72 61 69 6e 74 20 70 46 4b 65 79 20 61 6e 64 2c  raint pFKey and,
26e6e 20 69 66 20 70 46 4b 65 79 20 69 73 20 0a 2a 2a   if pFKey is .**
26e6f 20 64 65 66 65 72 72 65 64 2c 20 77 68 65 6e 20   deferred, when 
26e70 61 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65  a row is inserte
26e71 64 20 69 6e 74 6f 20 74 68 65 20 73 61 6d 65 20  d into the same 
26e72 74 61 62 6c 65 2e 20 57 68 65 6e 20 67 65 6e 65  table. When gene
26e73 72 61 74 69 6e 67 0a 2a 2a 20 63 6f 64 65 20 66  rating.** code f
26e74 6f 72 20 61 6e 20 53 51 4c 20 55 50 44 41 54 45  or an SQL UPDATE
26e75 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 73   operation, this
26e76 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65   function may be
26e77 20 63 61 6c 6c 65 64 20 74 77 69 63 65 20 2d 0a   called twice -.
26e78 2a 2a 20 6f 6e 63 65 20 74 6f 20 22 64 65 6c 65  ** once to "dele
26e79 74 65 22 20 74 68 65 20 6f 6c 64 20 72 6f 77 20  te" the old row 
26e7a 61 6e 64 20 6f 6e 63 65 20 74 6f 20 22 69 6e 73  and once to "ins
26e7b 65 72 74 22 20 74 68 65 20 6e 65 77 20 72 6f 77  ert" the new row
26e7c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ..**.** The code
26e7d 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68   generated by th
26e7e 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 63 61 6e  is function scan
26e7f 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 72 6f  s through the ro
26e80 77 73 20 69 6e 20 74 68 65 20 63 68 69 6c 64 0a  ws in the child.
26e81 2a 2a 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f  ** table that co
26e82 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20  rrespond to the 
26e83 70 61 72 65 6e 74 20 74 61 62 6c 65 20 72 6f 77  parent table row
26e84 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20 6f   being deleted o
26e85 72 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 20 46  r inserted..** F
26e86 6f 72 20 65 61 63 68 20 63 68 69 6c 64 20 72 6f  or each child ro
26e87 77 20 66 6f 75 6e 64 2c 20 6f 6e 65 20 6f 66 20  w found, one of 
26e88 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 63  the following ac
26e89 74 69 6f 6e 73 20 69 73 20 74 61 6b 65 6e 3a 0a  tions is taken:.
26e8a 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61 74 69 6f  **.**   Operatio
26e8b 6e 20 7c 20 46 4b 20 74 79 70 65 20 20 20 7c 20  n | FK type   | 
26e8c 41 63 74 69 6f 6e 20 74 61 6b 65 6e 0a 2a 2a 20  Action taken.** 
26e8d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
26e8e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26e8f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26e90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26e91 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
26e92 20 20 44 45 4c 45 54 45 20 20 20 20 20 20 69 6d    DELETE      im
26e93 6d 65 64 69 61 74 65 20 20 20 49 6e 63 72 65 6d  mediate   Increm
26e94 65 6e 74 20 74 68 65 20 22 69 6d 6d 65 64 69 61  ent the "immedia
26e95 74 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  te constraint co
26e96 75 6e 74 65 72 22 2e 0a 2a 2a 20 20 20 20 20 20  unter"..**      
26e97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26e98 20 20 20 20 20 4f 72 2c 20 69 66 20 74 68 65 20       Or, if the 
26e99 4f 4e 20 28 55 50 44 41 54 45 7c 44 45 4c 45 54  ON (UPDATE|DELET
26e9a 45 29 20 61 63 74 69 6f 6e 20 69 73 20 52 45 53  E) action is RES
26e9b 54 52 49 43 54 2c 0a 2a 2a 20 20 20 20 20 20 20  TRICT,.**       
26e9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26e9d 20 20 20 20 74 68 72 6f 77 20 61 20 22 66 6f 72      throw a "for
26e9e 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
26e9f 69 6e 74 20 66 61 69 6c 65 64 22 20 65 78 63 65  int failed" exce
26ea0 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49  ption..**.**   I
26ea1 4e 53 45 52 54 20 20 20 20 20 20 69 6d 6d 65 64  NSERT      immed
26ea2 69 61 74 65 20 20 20 44 65 63 72 65 6d 65 6e 74  iate   Decrement
26ea3 20 74 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20   the "immediate 
26ea4 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
26ea5 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c  er"..**.**   DEL
26ea6 45 54 45 20 20 20 20 20 20 64 65 66 65 72 72 65  ETE      deferre
26ea7 64 20 20 20 20 49 6e 63 72 65 6d 65 6e 74 20 74  d    Increment t
26ea8 68 65 20 22 64 65 66 65 72 72 65 64 20 63 6f 6e  he "deferred con
26ea9 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22  straint counter"
26eaa 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
26eab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f                 O
26eac 72 2c 20 69 66 20 74 68 65 20 4f 4e 20 28 55 50  r, if the ON (UP
26ead 44 41 54 45 7c 44 45 4c 45 54 45 29 20 61 63 74  DATE|DELETE) act
26eae 69 6f 6e 20 69 73 20 52 45 53 54 52 49 43 54 2c  ion is RESTRICT,
26eaf 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
26eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
26eb1 72 6f 77 20 61 20 22 66 6f 72 65 69 67 6e 20 6b  row a "foreign k
26eb2 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  ey constraint fa
26eb3 69 6c 65 64 22 20 65 78 63 65 70 74 69 6f 6e 2e  iled" exception.
26eb4 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52 54 20  .**.**   INSERT 
26eb5 20 20 20 20 20 64 65 66 65 72 72 65 64 20 20 20       deferred   
26eb6 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 22   Decrement the "
26eb7 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61  deferred constra
26eb8 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a  int counter"..**
26eb9 0a 2a 2a 20 54 68 65 73 65 20 6f 70 65 72 61 74  .** These operat
26eba 69 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 66  ions are identif
26ebb 69 65 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65  ied in the comme
26ebc 6e 74 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66  nt at the top of
26ebd 20 74 68 69 73 20 66 69 6c 65 20 0a 2a 2a 20 28   this file .** (
26ebe 66 6b 65 79 2e 63 29 20 61 73 20 22 49 2e 32 22  fkey.c) as "I.2"
26ebf 20 61 6e 64 20 22 44 2e 32 22 2e 0a 2a 2f 0a 73   and "D.2"..*/.s
26ec0 74 61 74 69 63 20 76 6f 69 64 20 66 6b 53 63 61  tatic void fkSca
26ec1 6e 43 68 69 6c 64 72 65 6e 28 0a 20 20 50 61 72  nChildren(.  Par
26ec2 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
26ec3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26ec4 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  Parse context */
26ec5 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
26ec6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26ec7 20 20 20 2f 2a 20 53 72 63 4c 69 73 74 20 63 6f     /* SrcList co
26ec8 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62  ntaining the tab
26ec9 6c 65 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20  le to scan */.  
26eca 54 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 49  Table *pTab,.  I
26ecb 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20  ndex *pIdx,     
26ecc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26ecd 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e  * Foreign key in
26ece 64 65 78 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70  dex */.  FKey *p
26ecf 46 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20  FKey,           
26ed0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 65           /* Fore
26ed1 69 67 6e 20 6b 65 79 20 72 65 6c 61 74 69 6f 6e  ign key relation
26ed2 73 68 69 70 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  ship */.  int *a
26ed3 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  iCol,           
26ed4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70            /* Map
26ed5 20 66 72 6f 6d 20 70 49 64 78 20 63 6f 6c 73 20   from pIdx cols 
26ed6 74 6f 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63  to child table c
26ed7 6f 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ols */.  int reg
26ed8 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 20 20  Data,           
26ed9 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65           /* Refe
26eda 72 65 6e 63 65 64 20 74 61 62 6c 65 20 64 61 74  renced table dat
26edb 61 20 73 74 61 72 74 73 20 68 65 72 65 20 2a 2f  a starts here */
26edc 0a 20 20 69 6e 74 20 6e 49 6e 63 72 20 20 20 20  .  int nIncr    
26edd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26ede 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 74 6f 20     /* Amount to 
26edf 69 6e 63 72 65 6d 65 6e 74 20 64 65 66 65 72 72  increment deferr
26ee0 65 64 20 63 6f 75 6e 74 65 72 20 62 79 20 2a 2f  ed counter by */
26ee1 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
26ee2 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
26ee3 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
26ee4 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e  e handle */.  in
26ee5 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
26ee6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26ee7 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
26ee8 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  le */.  Expr *pW
26ee9 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20  here = 0;       
26eea 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45          /* WHERE
26eeb 20 63 6c 61 75 73 65 20 74 6f 20 73 63 61 6e 20   clause to scan 
26eec 77 69 74 68 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  with */.  NameCo
26eed 6e 74 65 78 74 20 73 4e 61 6d 65 43 6f 6e 74 65  ntext sNameConte
26eee 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  xt;       /* Con
26eef 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73  text used to res
26ef0 6f 6c 76 65 20 57 48 45 52 45 20 63 6c 61 75 73  olve WHERE claus
26ef1 65 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f  e */.  WhereInfo
26ef2 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20   *pWInfo;       
26ef3 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
26ef4 74 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65  t used by sqlite
26ef5 33 57 68 65 72 65 58 58 58 28 29 20 2a 2f 0a 20  3WhereXXX() */. 
26ef6 20 69 6e 74 20 69 46 6b 49 66 5a 65 72 6f 20 3d   int iFkIfZero =
26ef7 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
26ef8 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f   /* Address of O
26ef9 50 5f 46 6b 49 66 5a 65 72 6f 20 2a 2f 0a 20 20  P_FkIfZero */.  
26efa 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
26efb 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
26efc 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 49  ;..  assert( !pI
26efd 64 78 20 7c 7c 20 70 49 64 78 2d 3e 70 54 61 62  dx || pIdx->pTab
26efe 6c 65 3d 3d 70 54 61 62 20 29 3b 0a 0a 20 20 69  le==pTab );..  i
26eff 66 28 20 6e 49 6e 63 72 3c 30 20 29 7b 0a 20 20  f( nIncr<0 ){.  
26f00 20 20 69 46 6b 49 66 5a 65 72 6f 20 3d 20 73 71    iFkIfZero = sq
26f01 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26f02 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c 20  v, OP_FkIfZero, 
26f03 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
26f04 64 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  d, 0);.  }..  /*
26f05 20 43 72 65 61 74 65 20 61 6e 20 45 78 70 72 20   Create an Expr 
26f06 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74  object represent
26f07 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72 65  ing an SQL expre
26f08 73 73 69 6f 6e 20 6c 69 6b 65 3a 0a 20 20 2a 2a  ssion like:.  **
26f09 0a 20 20 2a 2a 20 20 20 3c 70 61 72 65 6e 74 2d  .  **   <parent-
26f0a 6b 65 79 31 3e 20 3d 20 3c 63 68 69 6c 64 2d 6b  key1> = <child-k
26f0b 65 79 31 3e 20 41 4e 44 20 3c 70 61 72 65 6e 74  ey1> AND <parent
26f0c 2d 6b 65 79 32 3e 20 3d 20 3c 63 68 69 6c 64 2d  -key2> = <child-
26f0d 6b 65 79 32 3e 20 2e 2e 2e 0a 20 20 2a 2a 0a 20  key2> ....  **. 
26f0e 20 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f   ** The collatio
26f0f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20  n sequence used 
26f10 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72 69 73  for the comparis
26f11 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 74 68 61  on should be tha
26f12 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 70 61  t of.  ** the pa
26f13 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  rent key columns
26f14 2e 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f  . The affinity o
26f15 66 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  f the parent key
26f16 20 63 6f 6c 75 6d 6e 20 73 68 6f 75 6c 64 0a 20   column should. 
26f17 20 2a 2a 20 62 65 20 61 70 70 6c 69 65 64 20 74   ** be applied t
26f18 6f 20 65 61 63 68 20 63 68 69 6c 64 20 6b 65 79  o each child key
26f19 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 74 68   value before th
26f1a 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 74 61 6b  e comparison tak
26f1b 65 73 20 70 6c 61 63 65 2e 0a 20 20 2a 2f 0a 20  es place..  */. 
26f1c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b 65   for(i=0; i<pFKe
26f1d 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  y->nCol; i++){. 
26f1e 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20     Expr *pLeft; 
26f1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f20 20 2f 2a 20 56 61 6c 75 65 20 66 72 6f 6d 20 70   /* Value from p
26f21 61 72 65 6e 74 20 74 61 62 6c 65 20 72 6f 77 20  arent table row 
26f22 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 52 69  */.    Expr *pRi
26f23 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ght;            
26f24 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 72       /* Column r
26f25 65 66 20 74 6f 20 63 68 69 6c 64 20 74 61 62 6c  ef to child tabl
26f26 65 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70  e */.    Expr *p
26f27 45 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq;             
26f28 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73         /* Expres
26f29 73 69 6f 6e 20 28 70 4c 65 66 74 20 3d 20 70 52  sion (pLeft = pR
26f2a 69 67 68 74 29 20 2a 2f 0a 20 20 20 20 69 6e 74  ight) */.    int
26f2b 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   iCol;          
26f2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
26f2d 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e  dex of column in
26f2e 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 20   child table */ 
26f2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
26f30 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  *zCol;          
26f31 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f     /* Name of co
26f32 6c 75 6d 6e 20 69 6e 20 63 68 69 6c 64 20 74 61  lumn in child ta
26f33 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 70 4c 65 66  ble */..    pLef
26f34 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  t = sqlite3Expr(
26f35 64 62 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c  db, TK_REGISTER,
26f36 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65   0);.    if( pLe
26f37 66 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53  ft ){.      /* S
26f38 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  et the collation
26f39 20 73 65 71 75 65 6e 63 65 20 61 6e 64 20 61 66   sequence and af
26f3a 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 4c 48  finity of the LH
26f3b 53 20 6f 66 20 65 61 63 68 20 54 4b 5f 45 51 0a  S of each TK_EQ.
26f3c 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73        ** express
26f3d 69 6f 6e 20 74 6f 20 74 68 65 20 70 61 72 65 6e  ion to the paren
26f3e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 64 65 66  t key column def
26f3f 61 75 6c 74 73 2e 20 20 2a 2f 0a 20 20 20 20 20  aults.  */.     
26f40 20 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20   if( pIdx ){.   
26f41 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f       Column *pCo
26f42 6c 3b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 20  l;.        iCol 
26f43 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
26f44 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 70 43 6f  [i];.        pCo
26f45 6c 20 3d 20 26 70 49 64 78 2d 3e 70 54 61 62 6c  l = &pIdx->pTabl
26f46 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20  e->aCol[iCol];. 
26f47 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 69 54         pLeft->iT
26f48 61 62 6c 65 20 3d 20 72 65 67 44 61 74 61 2b 69  able = regData+i
26f49 43 6f 6c 2b 31 3b 0a 20 20 20 20 20 20 20 20 70  Col+1;.        p
26f4a 4c 65 66 74 2d 3e 61 66 66 69 6e 69 74 79 20 3d  Left->affinity =
26f4b 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3b   pCol->affinity;
26f4c 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e  .        pLeft->
26f4d 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c  pColl = sqlite3L
26f4e 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61  ocateCollSeq(pPa
26f4f 72 73 65 2c 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c  rse, pCol->zColl
26f50 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
26f51 20 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 69          pLeft->i
26f52 54 61 62 6c 65 20 3d 20 72 65 67 44 61 74 61 3b  Table = regData;
26f53 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e  .        pLeft->
26f54 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54  affinity = SQLIT
26f55 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20  E_AFF_INTEGER;. 
26f56 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
26f57 20 69 43 6f 6c 20 3d 20 61 69 43 6f 6c 20 3f 20   iCol = aiCol ? 
26f58 61 69 43 6f 6c 5b 69 5d 20 3a 20 70 46 4b 65 79  aiCol[i] : pFKey
26f59 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 3b  ->aCol[0].iFrom;
26f5a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6f  .    assert( iCo
26f5b 6c 3e 3d 30 20 29 3b 0a 20 20 20 20 7a 43 6f 6c  l>=0 );.    zCol
26f5c 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d   = pFKey->pFrom-
26f5d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
26f5e 65 3b 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20  e;.    pRight = 
26f5f 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20  sqlite3Expr(db, 
26f60 54 4b 5f 49 44 2c 20 7a 43 6f 6c 29 3b 0a 20 20  TK_ID, zCol);.  
26f61 20 20 70 45 71 20 3d 20 73 71 6c 69 74 65 33 50    pEq = sqlite3P
26f62 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
26f63 45 51 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  EQ, pLeft, pRigh
26f64 74 2c 20 30 29 3b 0a 20 20 20 20 70 57 68 65 72  t, 0);.    pWher
26f65 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  e = sqlite3ExprA
26f66 6e 64 28 64 62 2c 20 70 57 68 65 72 65 2c 20 70  nd(db, pWhere, p
26f67 45 71 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  Eq);.  }..  /* I
26f68 66 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c  f the child tabl
26f69 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
26f6a 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26f6b 65 2c 20 61 6e 64 20 74 68 69 73 20 73 63 61 6e  e, and this scan
26f6c 0a 20 20 2a 2a 20 69 73 20 74 61 6b 69 6e 67 20  .  ** is taking 
26f6d 70 6c 61 63 65 20 61 73 20 70 61 72 74 20 6f 66  place as part of
26f6e 20 61 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74   a DELETE operat
26f6f 69 6f 6e 20 28 6f 70 65 72 61 74 69 6f 6e 20 44  ion (operation D
26f70 2e 32 29 2c 20 6f 6d 69 74 20 74 68 65 0a 20 20  .2), omit the.  
26f71 2a 2a 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c  ** row being del
26f72 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20 73 63  eted from the sc
26f73 61 6e 20 62 79 20 61 64 64 69 6e 67 20 28 24 72  an by adding ($r
26f74 6f 77 69 64 20 21 3d 20 72 6f 77 69 64 29 20 74  owid != rowid) t
26f75 6f 20 74 68 65 20 57 48 45 52 45 20 0a 20 20 2a  o the WHERE .  *
26f76 2a 20 63 6c 61 75 73 65 2c 20 77 68 65 72 65 20  * clause, where 
26f77 24 72 6f 77 69 64 20 69 73 20 74 68 65 20 72 6f  $rowid is the ro
26f78 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20 62  wid of the row b
26f79 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 20 2a  eing deleted.  *
26f7a 2f 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70 46  /.  if( pTab==pF
26f7b 4b 65 79 2d 3e 70 46 72 6f 6d 20 26 26 20 6e 49  Key->pFrom && nI
26f7c 6e 63 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70  ncr>0 ){.    Exp
26f7d 72 20 2a 70 45 71 3b 20 20 20 20 20 20 20 20 20  r *pEq;         
26f7e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
26f7f 70 72 65 73 73 69 6f 6e 20 28 70 4c 65 66 74 20  pression (pLeft 
26f80 3d 20 70 52 69 67 68 74 29 20 2a 2f 0a 20 20 20  = pRight) */.   
26f81 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 20   Expr *pLeft;   
26f82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26f83 2a 20 56 61 6c 75 65 20 66 72 6f 6d 20 70 61 72  * Value from par
26f84 65 6e 74 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f  ent table row */
26f85 0a 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68  .    Expr *pRigh
26f86 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
26f87 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 72 65 66     /* Column ref
26f88 20 74 6f 20 63 68 69 6c 64 20 74 61 62 6c 65 20   to child table 
26f89 2a 2f 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 73  */.    pLeft = s
26f8a 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54  qlite3Expr(db, T
26f8b 4b 5f 52 45 47 49 53 54 45 52 2c 20 30 29 3b 0a  K_REGISTER, 0);.
26f8c 20 20 20 20 70 52 69 67 68 74 20 3d 20 73 71 6c      pRight = sql
26f8d 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
26f8e 43 4f 4c 55 4d 4e 2c 20 30 29 3b 0a 20 20 20 20  COLUMN, 0);.    
26f8f 69 66 28 20 70 4c 65 66 74 20 26 26 20 70 52 69  if( pLeft && pRi
26f90 67 68 74 20 29 7b 0a 20 20 20 20 20 20 70 4c 65  ght ){.      pLe
26f91 66 74 2d 3e 69 54 61 62 6c 65 20 3d 20 72 65 67  ft->iTable = reg
26f92 44 61 74 61 3b 0a 20 20 20 20 20 20 70 4c 65 66  Data;.      pLef
26f93 74 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51  t->affinity = SQ
26f94 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
26f95 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e  ;.      pRight->
26f96 69 54 61 62 6c 65 20 3d 20 70 53 72 63 2d 3e 61  iTable = pSrc->a
26f97 5b 30 5d 2e 69 43 75 72 73 6f 72 3b 0a 20 20 20  [0].iCursor;.   
26f98 20 20 20 70 52 69 67 68 74 2d 3e 69 43 6f 6c 75     pRight->iColu
26f99 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  mn = -1;.    }. 
26f9a 20 20 20 70 45 71 20 3d 20 73 71 6c 69 74 65 33     pEq = sqlite3
26f9b 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
26f9c 5f 4e 45 2c 20 70 4c 65 66 74 2c 20 70 52 69 67  _NE, pLeft, pRig
26f9d 68 74 2c 20 30 29 3b 0a 20 20 20 20 70 57 68 65  ht, 0);.    pWhe
26f9e 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
26f9f 41 6e 64 28 64 62 2c 20 70 57 68 65 72 65 2c 20  And(db, pWhere, 
26fa0 70 45 71 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  pEq);.  }..  /* 
26fa1 52 65 73 6f 6c 76 65 20 74 68 65 20 72 65 66 65  Resolve the refe
26fa2 72 65 6e 63 65 73 20 69 6e 20 74 68 65 20 57 48  rences in the WH
26fa3 45 52 45 20 63 6c 61 75 73 65 2e 20 2a 2f 0a 20  ERE clause. */. 
26fa4 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65 43 6f   memset(&sNameCo
26fa5 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66  ntext, 0, sizeof
26fa6 28 4e 61 6d 65 43 6f 6e 74 65 78 74 29 29 3b 0a  (NameContext));.
26fa7 20 20 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70    sNameContext.p
26fa8 53 72 63 4c 69 73 74 20 3d 20 70 53 72 63 3b 0a  SrcList = pSrc;.
26fa9 20 20 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70    sNameContext.p
26faa 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
26fab 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65    sqlite3Resolve
26fac 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 61 6d 65  ExprNames(&sName
26fad 43 6f 6e 74 65 78 74 2c 20 70 57 68 65 72 65 29  Context, pWhere)
26fae 3b 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 56  ;..  /* Create V
26faf 44 42 45 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f  DBE to loop thro
26fb0 75 67 68 20 74 68 65 20 65 6e 74 72 69 65 73 20  ugh the entries 
26fb1 69 6e 20 70 53 72 63 20 74 68 61 74 20 6d 61 74  in pSrc that mat
26fb2 63 68 20 74 68 65 20 57 48 45 52 45 0a 20 20 2a  ch the WHERE.  *
26fb3 2a 20 63 6c 61 75 73 65 2e 20 49 66 20 74 68 65  * clause. If the
26fb4 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6e   constraint is n
26fb5 6f 74 20 64 65 66 65 72 72 65 64 2c 20 74 68 72  ot deferred, thr
26fb6 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  ow an exception 
26fb7 66 6f 72 0a 20 20 2a 2a 20 65 61 63 68 20 72 6f  for.  ** each ro
26fb8 77 20 66 6f 75 6e 64 2e 20 4f 74 68 65 72 77 69  w found. Otherwi
26fb9 73 65 2c 20 66 6f 72 20 64 65 66 65 72 72 65 64  se, for deferred
26fba 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 69 6e   constraints, in
26fbb 63 72 65 6d 65 6e 74 20 74 68 65 0a 20 20 2a 2a  crement the.  **
26fbc 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72   deferred constr
26fbd 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 62 79 20  aint counter by 
26fbe 6e 49 6e 63 72 20 66 6f 72 20 65 61 63 68 20 72  nIncr for each r
26fbf 6f 77 20 73 65 6c 65 63 74 65 64 2e 20 20 2a 2f  ow selected.  */
26fc0 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69  .  pWInfo = sqli
26fc1 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
26fc2 61 72 73 65 2c 20 70 53 72 63 2c 20 70 57 68 65  arse, pSrc, pWhe
26fc3 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28  re, 0, 0);.  if(
26fc4 20 6e 49 6e 63 72 3e 30 20 26 26 20 70 46 4b 65   nIncr>0 && pFKe
26fc5 79 2d 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 30  y->isDeferred==0
26fc6 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   ){.    sqlite3P
26fc7 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61  arseToplevel(pPa
26fc8 72 73 65 29 2d 3e 6d 61 79 41 62 6f 72 74 20 3d  rse)->mayAbort =
26fc9 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   1;.  }.  sqlite
26fca 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26fcb 50 5f 46 6b 43 6f 75 6e 74 65 72 2c 20 70 46 4b  P_FkCounter, pFK
26fcc 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 2c 20  ey->isDeferred, 
26fcd 6e 49 6e 63 72 29 3b 0a 20 20 69 66 28 20 70 57  nIncr);.  if( pW
26fce 49 6e 66 6f 20 29 7b 0a 20 20 20 20 73 71 6c 69  Info ){.    sqli
26fcf 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
26fd0 66 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  fo);.  }..  /* C
26fd1 6c 65 61 6e 20 75 70 20 74 68 65 20 57 48 45 52  lean up the WHER
26fd2 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 75  E clause constru
26fd3 63 74 65 64 20 61 62 6f 76 65 2e 20 2a 2f 0a 20  cted above. */. 
26fd4 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
26fd5 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a  te(db, pWhere);.
26fd6 20 20 69 66 28 20 69 46 6b 49 66 5a 65 72 6f 20    if( iFkIfZero 
26fd7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
26fd8 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 46  beJumpHere(v, iF
26fd9 6b 49 66 5a 65 72 6f 29 3b 0a 20 20 7d 0a 7d 0a  kIfZero);.  }.}.
26fda 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
26fdb 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70  tion returns a p
26fdc 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 68 65  ointer to the he
26fdd 61 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c  ad of a linked l
26fde 69 73 74 20 6f 66 20 46 4b 0a 2a 2a 20 63 6f 6e  ist of FK.** con
26fdf 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69  straints for whi
26fe0 63 68 20 74 61 62 6c 65 20 70 54 61 62 20 69 73  ch table pTab is
26fe1 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26fe2 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a  e. For example,.
26fe3 2a 2a 20 67 69 76 65 6e 20 74 68 65 20 66 6f 6c  ** given the fol
26fe4 6c 6f 77 69 6e 67 20 73 63 68 65 6d 61 3a 0a 2a  lowing schema:.*
26fe5 2a 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41  *.**   CREATE TA
26fe6 42 4c 45 20 74 31 28 61 20 50 52 49 4d 41 52 59  BLE t1(a PRIMARY
26fe7 20 4b 45 59 29 3b 0a 2a 2a 20 20 20 43 52 45 41   KEY);.**   CREA
26fe8 54 45 20 54 41 42 4c 45 20 74 32 28 62 20 52 45  TE TABLE t2(b RE
26fe9 46 45 52 45 4e 43 45 53 20 74 31 28 61 29 3b 0a  FERENCES t1(a);.
26fea 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 68  **.** Calling th
26feb 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  is function with
26fec 20 74 61 62 6c 65 20 22 74 31 22 20 61 73 20 61   table "t1" as a
26fed 6e 20 61 72 67 75 6d 65 6e 74 20 72 65 74 75 72  n argument retur
26fee 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  ns a pointer.** 
26fef 74 6f 20 74 68 65 20 46 4b 65 79 20 73 74 72 75  to the FKey stru
26ff0 63 74 75 72 65 20 72 65 70 72 65 73 65 6e 74 69  cture representi
26ff1 6e 67 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  ng the foreign k
26ff2 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e  ey constraint on
26ff3 20 74 61 62 6c 65 0a 2a 2a 20 22 74 32 22 2e 20   table.** "t2". 
26ff4 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e  Calling this fun
26ff5 63 74 69 6f 6e 20 77 69 74 68 20 22 74 32 22 20  ction with "t2" 
26ff6 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  as the argument 
26ff7 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 61 0a 2a  would return a.*
26ff8 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28  * NULL pointer (
26ff9 61 73 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  as there are no 
26ffa 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66  FK constraints f
26ffb 6f 72 20 77 68 69 63 68 20 74 32 20 69 73 20 74  or which t2 is t
26ffc 68 65 20 70 61 72 65 6e 74 0a 2a 2a 20 74 61 62  he parent.** tab
26ffd 6c 65 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  le)..*/.SQLITE_P
26ffe 52 49 56 41 54 45 20 46 4b 65 79 20 2a 73 71 6c  RIVATE FKey *sql
26fff 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
27000 28 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20  (Table *pTab){. 
27001 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c   int nName = sql
27002 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61  ite3Strlen30(pTa
27003 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74  b->zName);.  ret
27004 75 72 6e 20 28 46 4b 65 79 20 2a 29 73 71 6c 69  urn (FKey *)sqli
27005 74 65 33 48 61 73 68 46 69 6e 64 28 26 70 54 61  te3HashFind(&pTa
27006 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6b 65 79  b->pSchema->fkey
27007 48 61 73 68 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Hash, pTab->zNam
27008 65 2c 20 6e 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a  e, nName);.}../*
27009 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
2700a 72 67 75 6d 65 6e 74 20 69 73 20 61 20 54 72 69  rgument is a Tri
2700b 67 67 65 72 20 73 74 72 75 63 74 75 72 65 20 61  gger structure a
2700c 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20  llocated by the 
2700d 0a 2a 2a 20 66 6b 41 63 74 69 6f 6e 54 72 69 67  .** fkActionTrig
2700e 67 65 72 28 29 20 72 6f 75 74 69 6e 65 2e 20 54  ger() routine. T
2700f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c  his function del
27010 65 74 65 73 20 74 68 65 20 54 72 69 67 67 65 72  etes the Trigger
27011 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 61 6e   structure.** an
27012 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 73 75 62  d all of its sub
27013 2d 63 6f 6d 70 6f 6e 65 6e 74 73 2e 0a 2a 2a 0a  -components..**.
27014 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72 20 73  ** The Trigger s
27015 74 72 75 63 74 75 72 65 20 6f 72 20 61 6e 79 20  tructure or any 
27016 6f 66 20 69 74 73 20 73 75 62 2d 63 6f 6d 70 6f  of its sub-compo
27017 6e 65 6e 74 73 20 6d 61 79 20 62 65 20 61 6c 6c  nents may be all
27018 6f 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74  ocated from.** t
27019 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66  he lookaside buf
2701a 66 65 72 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f  fer belonging to
2701b 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
2701c 20 64 62 4d 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69   dbMem..*/.stati
2701d 63 20 76 6f 69 64 20 66 6b 54 72 69 67 67 65 72  c void fkTrigger
2701e 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a  Delete(sqlite3 *
2701f 64 62 4d 65 6d 2c 20 54 72 69 67 67 65 72 20 2a  dbMem, Trigger *
27020 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
27021 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a     TriggerStep *
27022 70 53 74 65 70 20 3d 20 70 2d 3e 73 74 65 70 5f  pStep = p->step_
27023 6c 69 73 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  list;.    sqlite
27024 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 4d 65  3ExprDelete(dbMe
27025 6d 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65  m, pStep->pWhere
27026 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
27027 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 4d  prListDelete(dbM
27028 65 6d 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72  em, pStep->pExpr
27029 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  List);.    sqlit
2702a 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2702b 62 4d 65 6d 2c 20 70 53 74 65 70 2d 3e 70 53 65  bMem, pStep->pSe
2702c 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  lect);.    sqlit
2702d 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 4d  e3ExprDelete(dbM
2702e 65 6d 2c 20 70 2d 3e 70 57 68 65 6e 29 3b 0a 20  em, p->pWhen);. 
2702f 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
27030 28 64 62 4d 65 6d 2c 20 70 29 3b 0a 20 20 7d 0a  (dbMem, p);.  }.
27031 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
27032 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
27033 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64   to generate cod
27034 65 20 74 68 61 74 20 72 75 6e 73 20 77 68 65 6e  e that runs when
27035 20 74 61 62 6c 65 20 70 54 61 62 20 69 73 0a 2a   table pTab is.*
27036 2a 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20  * being dropped 
27037 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
27038 65 2e 20 54 68 65 20 53 72 63 4c 69 73 74 20 70  e. The SrcList p
27039 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
2703a 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ond argument.** 
2703b 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
2703c 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67   contains a sing
2703d 6c 65 20 65 6e 74 72 79 20 67 75 61 72 61 6e 74  le entry guarant
2703e 65 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74  eed to resolve t
2703f 6f 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 2e  o.** table pTab.
27040 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c  .**.** Normally,
27041 20 6e 6f 20 63 6f 64 65 20 69 73 20 72 65 71 75   no code is requ
27042 69 72 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 69  ired. However, i
27043 66 20 65 69 74 68 65 72 0a 2a 2a 0a 2a 2a 20 20  f either.**.**  
27044 20 28 61 29 20 54 68 65 20 74 61 62 6c 65 20 69   (a) The table i
27045 73 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  s the parent tab
27046 6c 65 20 6f 66 20 61 20 46 4b 20 63 6f 6e 73 74  le of a FK const
27047 72 61 69 6e 74 2c 20 6f 72 0a 2a 2a 20 20 20 28  raint, or.**   (
27048 62 29 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  b) The table is 
27049 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
2704a 6f 66 20 61 20 64 65 66 65 72 72 65 64 20 46 4b  of a deferred FK
2704b 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20   constraint and 
2704c 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 64  it is.**       d
2704d 65 74 65 72 6d 69 6e 65 64 20 61 74 20 72 75 6e  etermined at run
2704e 74 69 6d 65 20 74 68 61 74 20 74 68 65 72 65 20  time that there 
2704f 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  are outstanding 
27050 64 65 66 65 72 72 65 64 20 46 4b 20 0a 2a 2a 20  deferred FK .** 
27051 20 20 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74        constraint
27052 20 76 69 6f 6c 61 74 69 6f 6e 73 20 69 6e 20 74   violations in t
27053 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 0a  he database,.**.
27054 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 71 75 69  ** then the equi
27055 76 61 6c 65 6e 74 20 6f 66 20 22 44 45 4c 45 54  valent of "DELET
27056 45 20 46 52 4f 4d 20 3c 74 62 6c 3e 22 20 69 73  E FROM <tbl>" is
27057 20 65 78 65 63 75 74 65 64 20 62 65 66 6f 72 65   executed before
27058 20 64 72 6f 70 70 69 6e 67 0a 2a 2a 20 74 68 65   dropping.** the
27059 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20   table from the 
2705a 64 61 74 61 62 61 73 65 2e 20 54 72 69 67 67 65  database. Trigge
2705b 72 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20  rs are disabled 
2705c 77 68 69 6c 65 20 72 75 6e 6e 69 6e 67 20 74 68  while running th
2705d 69 73 0a 2a 2a 20 44 45 4c 45 54 45 2c 20 62 75  is.** DELETE, bu
2705e 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63  t foreign key ac
2705f 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 2e 0a 2a  tions are not..*
27060 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
27061 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 44   void sqlite3FkD
27062 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a  ropTable(Parse *
27063 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20  pParse, SrcList 
27064 2a 70 4e 61 6d 65 2c 20 54 61 62 6c 65 20 2a 70  *pName, Table *p
27065 54 61 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  Tab){.  sqlite3 
27066 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
27067 3b 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61  ;.  if( (db->fla
27068 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  gs&SQLITE_Foreig
27069 6e 4b 65 79 73 29 20 26 26 20 21 49 73 56 69 72  nKeys) && !IsVir
2706a 74 75 61 6c 28 70 54 61 62 29 20 26 26 20 21 70  tual(pTab) && !p
2706b 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
2706c 20 20 20 20 69 6e 74 20 69 53 6b 69 70 20 3d 20      int iSkip = 
2706d 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  0;.    Vdbe *v =
2706e 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2706f 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 61 73  pParse);..    as
27070 73 65 72 74 28 20 76 20 29 3b 20 20 20 20 20 20  sert( v );      
27071 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
27072 44 42 45 20 68 61 73 20 61 6c 72 65 61 64 79 20  DBE has already 
27073 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a  been allocated *
27074 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
27075 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54  3FkReferences(pT
27076 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ab)==0 ){.      
27077 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20  /* Search for a 
27078 64 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e  deferred foreign
27079 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
2707a 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 74  for which this t
2707b 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 69 73  able.      ** is
2707c 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65   the child table
2707d 2e 20 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20  . If one cannot 
2707e 62 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e  be found, return
2707f 20 77 69 74 68 6f 75 74 20 0a 20 20 20 20 20 20   without .      
27080 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e  ** generating an
27081 79 20 56 44 42 45 20 63 6f 64 65 2e 20 49 66 20  y VDBE code. If 
27082 6f 6e 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  one can be found
27083 2c 20 74 68 65 6e 20 6a 75 6d 70 20 6f 76 65 72  , then jump over
27084 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 65 6e  .      ** the en
27085 74 69 72 65 20 44 45 4c 45 54 45 20 69 66 20 74  tire DELETE if t
27086 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73  here are no outs
27087 74 61 6e 64 69 6e 67 20 64 65 66 65 72 72 65 64  tanding deferred
27088 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 20   constraints.   
27089 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 69 73 20     ** when this 
2708a 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 75 6e  statement is run
2708b 2e 20 20 2a 2f 0a 20 20 20 20 20 20 46 4b 65 79  .  */.      FKey
2708c 20 2a 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70   *p;.      for(p
2708d 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b  =pTab->pFKey; p;
2708e 20 70 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29   p=p->pNextFrom)
2708f 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d  {.        if( p-
27090 3e 69 73 44 65 66 65 72 72 65 64 20 29 20 62 72  >isDeferred ) br
27091 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
27092 20 20 20 69 66 28 20 21 70 20 29 20 72 65 74 75     if( !p ) retu
27093 72 6e 3b 0a 20 20 20 20 20 20 69 53 6b 69 70 20  rn;.      iSkip 
27094 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
27095 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
27096 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27097 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72  p2(v, OP_FkIfZer
27098 6f 2c 20 31 2c 20 69 53 6b 69 70 29 3b 0a 20 20  o, 1, iSkip);.  
27099 20 20 7d 0a 0a 20 20 20 20 70 50 61 72 73 65 2d    }..    pParse-
2709a 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72 73  >disableTriggers
2709b 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 1;.    sqlite
2709c 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72  3DeleteFrom(pPar
2709d 73 65 2c 20 73 71 6c 69 74 65 33 53 72 63 4c 69  se, sqlite3SrcLi
2709e 73 74 44 75 70 28 64 62 2c 20 70 4e 61 6d 65 2c  stDup(db, pName,
2709f 20 30 29 2c 20 30 29 3b 0a 20 20 20 20 70 50 61   0), 0);.    pPa
270a0 72 73 65 2d 3e 64 69 73 61 62 6c 65 54 72 69 67  rse->disableTrig
270a1 67 65 72 73 20 3d 20 30 3b 0a 0a 20 20 20 20 2f  gers = 0;..    /
270a2 2a 20 49 66 20 74 68 65 20 44 45 4c 45 54 45 20  * If the DELETE 
270a3 68 61 73 20 67 65 6e 65 72 61 74 65 64 20 69 6d  has generated im
270a4 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20  mediate foreign 
270a5 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 0a  key constraint .
270a6 20 20 20 20 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e      ** violation
270a7 73 2c 20 68 61 6c 74 20 74 68 65 20 56 44 42 45  s, halt the VDBE
270a8 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
270a9 72 72 6f 72 20 61 74 20 74 68 69 73 20 70 6f 69  rror at this poi
270aa 6e 74 2c 20 62 65 66 6f 72 65 0a 20 20 20 20 2a  nt, before.    *
270ab 2a 20 61 6e 79 20 6d 6f 64 69 66 69 63 61 74 69  * any modificati
270ac 6f 6e 73 20 74 6f 20 74 68 65 20 73 63 68 65 6d  ons to the schem
270ad 61 20 61 72 65 20 6d 61 64 65 2e 20 54 68 69 73  a are made. This
270ae 20 69 73 20 62 65 63 61 75 73 65 20 73 74 61 74   is because stat
270af 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 74 72 61  ement.    ** tra
270b0 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f  nsactions are no
270b1 74 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61  t able to rollba
270b2 63 6b 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ck schema change
270b3 73 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  s.  */.    sqlit
270b4 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
270b5 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c 20 30 2c 20  OP_FkIfZero, 0, 
270b6 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
270b7 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a 20 20  ntAddr(v)+2);.  
270b8 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e    sqlite3HaltCon
270b9 73 74 72 61 69 6e 74 28 0a 20 20 20 20 20 20 20  straint(.       
270ba 20 70 50 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72   pParse, OE_Abor
270bb 74 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20  t, "foreign key 
270bc 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
270bd 64 22 2c 20 50 34 5f 53 54 41 54 49 43 0a 20 20  d", P4_STATIC.  
270be 20 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 53    );..    if( iS
270bf 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  kip ){.      sql
270c0 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
270c1 61 62 65 6c 28 76 2c 20 69 53 6b 69 70 29 3b 0a  abel(v, iSkip);.
270c2 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
270c3 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
270c4 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
270c5 69 6e 73 65 72 74 69 6e 67 2c 20 64 65 6c 65 74  inserting, delet
270c6 69 6e 67 20 6f 72 20 75 70 64 61 74 69 6e 67 20  ing or updating 
270c7 61 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 61 62 6c  a row of.** tabl
270c8 65 20 70 54 61 62 20 74 6f 20 67 65 6e 65 72 61  e pTab to genera
270c9 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 6f 20  te VDBE code to 
270ca 70 65 72 66 6f 72 6d 20 66 6f 72 65 69 67 6e 20  perform foreign 
270cb 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 0a  key constraint .
270cc 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f  ** processing fo
270cd 72 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e  r the operation.
270ce 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 44 45 4c  .**.** For a DEL
270cf 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70  ETE operation, p
270d0 61 72 61 6d 65 74 65 72 20 72 65 67 4f 6c 64 20  arameter regOld 
270d1 69 73 20 70 61 73 73 65 64 20 74 68 65 20 69 6e  is passed the in
270d2 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69  dex of the.** fi
270d3 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20  rst register in 
270d4 61 6e 20 61 72 72 61 79 20 6f 66 20 28 70 54 61  an array of (pTa
270d5 62 2d 3e 6e 43 6f 6c 2b 31 29 20 72 65 67 69 73  b->nCol+1) regis
270d6 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ters containing 
270d7 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20  the.** rowid of 
270d8 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65  the row being de
270d9 6c 65 74 65 64 2c 20 66 6f 6c 6c 6f 77 65 64 20  leted, followed 
270da 62 79 20 65 61 63 68 20 6f 66 20 74 68 65 20 63  by each of the c
270db 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 0a 2a 2a 20  olumn values.** 
270dc 6f 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  of the row being
270dd 20 64 65 6c 65 74 65 64 2c 20 66 72 6f 6d 20 6c   deleted, from l
270de 65 66 74 20 74 6f 20 72 69 67 68 74 2e 20 50 61  eft to right. Pa
270df 72 61 6d 65 74 65 72 20 72 65 67 4e 65 77 20 69  rameter regNew i
270e0 73 20 70 61 73 73 65 64 0a 2a 2a 20 7a 65 72 6f  s passed.** zero
270e1 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a   in this case..*
270e2 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 49 4e 53 45  *.** For an INSE
270e3 52 54 20 6f 70 65 72 61 74 69 6f 6e 2c 20 72 65  RT operation, re
270e4 67 4f 6c 64 20 69 73 20 70 61 73 73 65 64 20 7a  gOld is passed z
270e5 65 72 6f 20 61 6e 64 20 72 65 67 4e 65 77 20 69  ero and regNew i
270e6 73 20 70 61 73 73 65 64 20 74 68 65 0a 2a 2a 20  s passed the.** 
270e7 66 69 72 73 74 20 72 65 67 69 73 74 65 72 20 6f  first register o
270e8 66 20 61 6e 20 61 72 72 61 79 20 6f 66 20 28 70  f an array of (p
270e9 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 20 72 65 67  Tab->nCol+1) reg
270ea 69 73 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e  isters containin
270eb 67 20 74 68 65 20 6e 65 77 0a 2a 2a 20 72 6f 77  g the new.** row
270ec 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72   data..**.** For
270ed 20 61 6e 20 55 50 44 41 54 45 20 6f 70 65 72 61   an UPDATE opera
270ee 74 69 6f 6e 2c 20 74 68 69 73 20 66 75 6e 63 74  tion, this funct
270ef 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 77  ion is called tw
270f0 69 63 65 2e 20 4f 6e 63 65 20 62 65 66 6f 72 65  ice. Once before
270f1 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  .** the original
270f2 20 72 65 63 6f 72 64 20 69 73 20 64 65 6c 65 74   record is delet
270f3 65 64 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c  ed from the tabl
270f4 65 20 75 73 69 6e 67 20 74 68 65 20 63 61 6c 6c  e using the call
270f5 69 6e 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 0a 2a  ing convention.*
270f6 2a 20 64 65 73 63 72 69 62 65 64 20 66 6f 72 20  * described for 
270f7 44 45 4c 45 54 45 2e 20 54 68 65 6e 20 61 67 61  DELETE. Then aga
270f8 69 6e 20 61 66 74 65 72 20 74 68 65 20 6f 72 69  in after the ori
270f9 67 69 6e 61 6c 20 72 65 63 6f 72 64 20 69 73 20  ginal record is 
270fa 64 65 6c 65 74 65 64 0a 2a 2a 20 62 75 74 20 62  deleted.** but b
270fb 65 66 6f 72 65 20 74 68 65 20 6e 65 77 20 72 65  efore the new re
270fc 63 6f 72 64 20 69 73 20 69 6e 73 65 72 74 65 64  cord is inserted
270fd 20 75 73 69 6e 67 20 74 68 65 20 49 4e 53 45 52   using the INSER
270fe 54 20 63 6f 6e 76 65 6e 74 69 6f 6e 2e 20 0a 2a  T convention. .*
270ff 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
27100 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 43   void sqlite3FkC
27101 68 65 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70  heck(.  Parse *p
27102 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
27103 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
27104 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
27105 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
27106 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27107 20 52 6f 77 20 69 73 20 62 65 69 6e 67 20 64 65   Row is being de
27108 6c 65 74 65 64 20 66 72 6f 6d 20 74 68 69 73 20  leted from this 
27109 74 61 62 6c 65 20 2a 2f 20 0a 20 20 69 6e 74 20  table */ .  int 
2710a 72 65 67 4f 6c 64 2c 20 20 20 20 20 20 20 20 20  regOld,         
2710b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2710c 72 65 76 69 6f 75 73 20 72 6f 77 20 64 61 74 61  revious row data
2710d 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20   is stored here 
2710e 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4e 65 77 20  */.  int regNew 
2710f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27110 20 20 20 20 20 2f 2a 20 4e 65 77 20 72 6f 77 20       /* New row 
27111 64 61 74 61 20 69 73 20 73 74 6f 72 65 64 20 68  data is stored h
27112 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ere */.){.  sqli
27113 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
27114 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44  ->db;       /* D
27115 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
27116 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
27117 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27118 20 20 20 20 2f 2a 20 56 4d 20 74 6f 20 77 72 69      /* VM to wri
27119 74 65 20 63 6f 64 65 20 74 6f 20 2a 2f 0a 20 20  te code to */.  
2711a 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20  FKey *pFKey;    
2711b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2711c 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61  /* Used to itera
2711d 74 65 20 74 68 72 6f 75 67 68 20 46 4b 73 20 2a  te through FKs *
2711e 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
2711f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27120 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
27121 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
27122 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 63 6f  ing pTab */.  co
27123 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
27124 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27125 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
27126 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61  e containing pTa
27127 62 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 67 6e  b */.  int isIgn
27128 6f 72 65 45 72 72 6f 72 73 20 3d 20 70 50 61 72  oreErrors = pPar
27129 73 65 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67  se->disableTrigg
2712a 65 72 73 3b 0a 0a 20 20 2f 2a 20 45 78 61 63 74  ers;..  /* Exact
2712b 6c 79 20 6f 6e 65 20 6f 66 20 72 65 67 4f 6c 64  ly one of regOld
2712c 20 61 6e 64 20 72 65 67 4e 65 77 20 73 68 6f 75   and regNew shou
2712d 6c 64 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  ld be non-zero. 
2712e 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 72 65  */.  assert( (re
2712f 67 4f 6c 64 3d 3d 30 29 21 3d 28 72 65 67 4e 65  gOld==0)!=(regNe
27130 77 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  w==0) );..  /* I
27131 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79 73 20 61  f foreign-keys a
27132 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74 68 69  re disabled, thi
27133 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
27134 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20  no-op. */.  if( 
27135 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  (db->flags&SQLIT
27136 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 29 3d 3d  E_ForeignKeys)==
27137 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76  0 ) return;..  v
27138 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
27139 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 44 62  e(pParse);.  iDb
2713a 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2713b 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
2713c 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44  ->pSchema);.  zD
2713d 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  b = db->aDb[iDb]
2713e 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 4c 6f  .zName;..  /* Lo
2713f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74  op through all t
27140 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  he foreign key c
27141 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77  onstraints for w
27142 68 69 63 68 20 70 54 61 62 20 69 73 20 74 68 65  hich pTab is the
27143 0a 20 20 2a 2a 20 63 68 69 6c 64 20 74 61 62 6c  .  ** child tabl
27144 65 20 28 74 68 65 20 74 61 62 6c 65 20 74 68 61  e (the table tha
27145 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  t the foreign ke
27146 79 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 73 20  y definition is 
27147 70 61 72 74 20 6f 66 29 2e 20 20 2a 2f 0a 20 20  part of).  */.  
27148 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e  for(pFKey=pTab->
27149 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46  pFKey; pFKey; pF
2714a 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74  Key=pFKey->pNext
2714b 46 72 6f 6d 29 7b 0a 20 20 20 20 54 61 62 6c 65  From){.    Table
2714c 20 2a 70 54 6f 3b 20 20 20 20 20 20 20 20 20 20   *pTo;          
2714d 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65           /* Pare
2714e 6e 74 20 74 61 62 6c 65 20 6f 66 20 66 6f 72 65  nt table of fore
2714f 69 67 6e 20 6b 65 79 20 70 46 4b 65 79 20 2a 2f  ign key pFKey */
27150 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
27151 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
27152 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 6e 20 6b     /* Index on k
27153 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 54  ey columns in pT
27154 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 61 69  o */.    int *ai
27155 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 69 6e  Free = 0;.    in
27156 74 20 2a 61 69 43 6f 6c 3b 0a 20 20 20 20 69 6e  t *aiCol;.    in
27157 74 20 69 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20  t iCol;.    int 
27158 69 3b 0a 20 20 20 20 69 6e 74 20 69 73 49 67 6e  i;.    int isIgn
27159 6f 72 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  ore = 0;..    /*
2715a 20 46 69 6e 64 20 74 68 65 20 70 61 72 65 6e 74   Find the parent
2715b 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 66   table of this f
2715c 6f 72 65 69 67 6e 20 6b 65 79 2e 20 41 6c 73 6f  oreign key. Also
2715d 20 66 69 6e 64 20 61 20 75 6e 69 71 75 65 20 69   find a unique i
2715e 6e 64 65 78 20 0a 20 20 20 20 2a 2a 20 6f 6e 20  ndex .    ** on 
2715f 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63  the parent key c
27160 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61  olumns in the pa
27161 72 65 6e 74 20 74 61 62 6c 65 2e 20 49 66 20 65  rent table. If e
27162 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a  ither of these .
27163 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 74      ** schema it
27164 65 6d 73 20 63 61 6e 6e 6f 74 20 62 65 20 6c 6f  ems cannot be lo
27165 63 61 74 65 64 2c 20 73 65 74 20 61 6e 20 65 72  cated, set an er
27166 72 6f 72 20 69 6e 20 70 50 61 72 73 65 20 61 6e  ror in pParse an
27167 64 20 72 65 74 75 72 6e 20 0a 20 20 20 20 2a 2a  d return .    **
27168 20 65 61 72 6c 79 2e 20 20 2a 2f 0a 20 20 20 20   early.  */.    
27169 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  if( pParse->disa
2716a 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b 0a 20  bleTriggers ){. 
2716b 20 20 20 20 20 70 54 6f 20 3d 20 73 71 6c 69 74       pTo = sqlit
2716c 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
2716d 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 7a 44 62 29  pFKey->zTo, zDb)
2716e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2716f 20 20 20 70 54 6f 20 3d 20 73 71 6c 69 74 65 33     pTo = sqlite3
27170 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72  LocateTable(pPar
27171 73 65 2c 20 30 2c 20 70 46 4b 65 79 2d 3e 7a 54  se, 0, pFKey->zT
27172 6f 2c 20 7a 44 62 29 3b 0a 20 20 20 20 7d 0a 20  o, zDb);.    }. 
27173 20 20 20 69 66 28 20 21 70 54 6f 20 7c 7c 20 6c     if( !pTo || l
27174 6f 63 61 74 65 46 6b 65 79 49 6e 64 65 78 28 70  ocateFkeyIndex(p
27175 50 61 72 73 65 2c 20 70 54 6f 2c 20 70 46 4b 65  Parse, pTo, pFKe
27176 79 2c 20 26 70 49 64 78 2c 20 26 61 69 46 72 65  y, &pIdx, &aiFre
27177 65 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  e) ){.      if( 
27178 21 69 73 49 67 6e 6f 72 65 45 72 72 6f 72 73 20  !isIgnoreErrors 
27179 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
2717a 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  led ) return;.  
2717b 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
2717c 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
2717d 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20 7c  pFKey->nCol==1 |
2717e 7c 20 28 61 69 46 72 65 65 20 26 26 20 70 49 64  | (aiFree && pId
2717f 78 29 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 61  x) );..    if( a
27180 69 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 61  iFree ){.      a
27181 69 43 6f 6c 20 3d 20 61 69 46 72 65 65 3b 0a 20  iCol = aiFree;. 
27182 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27183 69 43 6f 6c 20 3d 20 70 46 4b 65 79 2d 3e 61 43  iCol = pFKey->aC
27184 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20  ol[0].iFrom;.   
27185 20 20 20 61 69 43 6f 6c 20 3d 20 26 69 43 6f 6c     aiCol = &iCol
27186 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
27187 69 3d 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43  i=0; i<pFKey->nC
27188 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
27189 69 66 28 20 61 69 43 6f 6c 5b 69 5d 3d 3d 70 54  if( aiCol[i]==pT
2718a 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
2718b 20 20 20 20 20 61 69 43 6f 6c 5b 69 5d 20 3d 20       aiCol[i] = 
2718c 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e  -1;.      }.#ifn
2718d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2718e 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
2718f 20 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 70      /* Request p
27190 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 72 65 61  ermission to rea
27191 64 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  d the parent key
27192 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 74 68 65   columns. If the
27193 20 0a 20 20 20 20 20 20 2a 2a 20 61 75 74 68 6f   .      ** autho
27194 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
27195 6b 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  k returns SQLITE
27196 5f 49 47 4e 4f 52 45 2c 20 62 65 68 61 76 65 20  _IGNORE, behave 
27197 61 73 20 69 66 20 61 6e 79 0a 20 20 20 20 20 20  as if any.      
27198 2a 2a 20 76 61 6c 75 65 73 20 72 65 61 64 20 66  ** values read f
27199 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 74  rom the parent t
2719a 61 62 6c 65 20 61 72 65 20 4e 55 4c 4c 2e 20 2a  able are NULL. *
2719b 2f 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e  /.      if( db->
2719c 78 41 75 74 68 20 29 7b 0a 20 20 20 20 20 20 20  xAuth ){.       
2719d 20 69 6e 74 20 72 63 61 75 74 68 3b 0a 20 20 20   int rcauth;.   
2719e 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20       char *zCol 
2719f 3d 20 70 54 6f 2d 3e 61 43 6f 6c 5b 70 49 64 78  = pTo->aCol[pIdx
271a0 20 3f 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   ? pIdx->aiColum
271a1 6e 5b 69 5d 20 3a 20 70 54 6f 2d 3e 69 50 4b 65  n[i] : pTo->iPKe
271a2 79 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  y].zName;.      
271a3 20 20 72 63 61 75 74 68 20 3d 20 73 71 6c 69 74    rcauth = sqlit
271a4 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 70 50  e3AuthReadCol(pP
271a5 61 72 73 65 2c 20 70 54 6f 2d 3e 7a 4e 61 6d 65  arse, pTo->zName
271a6 2c 20 7a 43 6f 6c 2c 20 69 44 62 29 3b 0a 20 20  , zCol, iDb);.  
271a7 20 20 20 20 20 20 69 73 49 67 6e 6f 72 65 20 3d        isIgnore =
271a8 20 28 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45   (rcauth==SQLITE
271a9 5f 49 47 4e 4f 52 45 29 3b 0a 20 20 20 20 20 20  _IGNORE);.      
271aa 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a  }.#endif.    }..
271ab 20 20 20 20 2f 2a 20 54 61 6b 65 20 61 20 73 68      /* Take a sh
271ac 61 72 65 64 2d 63 61 63 68 65 20 61 64 76 69 73  ared-cache advis
271ad 6f 72 79 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  ory read-lock on
271ae 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
271af 65 2e 20 41 6c 6c 6f 63 61 74 65 20 0a 20 20 20  e. Allocate .   
271b0 20 2a 2a 20 61 20 63 75 72 73 6f 72 20 74 6f 20   ** a cursor to 
271b1 75 73 65 20 74 6f 20 73 65 61 72 63 68 20 74 68  use to search th
271b2 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20 6f  e unique index o
271b3 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  n the parent key
271b4 20 63 6f 6c 75 6d 6e 73 20 0a 20 20 20 20 2a 2a   columns .    **
271b5 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74   in the parent t
271b6 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 73 71  able.  */.    sq
271b7 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70  lite3TableLock(p
271b8 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 6f 2d  Parse, iDb, pTo-
271b9 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 6f 2d 3e 7a  >tnum, 0, pTo->z
271ba 4e 61 6d 65 29 3b 0a 20 20 20 20 70 50 61 72 73  Name);.    pPars
271bb 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 0a 20 20 20 20  e->nTab++;..    
271bc 69 66 28 20 72 65 67 4f 6c 64 21 3d 30 20 29 7b  if( regOld!=0 ){
271bd 0a 20 20 20 20 20 20 2f 2a 20 41 20 72 6f 77 20  .      /* A row 
271be 69 73 20 62 65 69 6e 67 20 72 65 6d 6f 76 65 64  is being removed
271bf 20 66 72 6f 6d 20 74 68 65 20 63 68 69 6c 64 20   from the child 
271c0 74 61 62 6c 65 2e 20 53 65 61 72 63 68 20 66 6f  table. Search fo
271c1 72 20 74 68 65 20 70 61 72 65 6e 74 2e 0a 20 20  r the parent..  
271c2 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61      ** If the pa
271c3 72 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 65 78  rent does not ex
271c4 69 73 74 2c 20 72 65 6d 6f 76 69 6e 67 20 74 68  ist, removing th
271c5 65 20 63 68 69 6c 64 20 72 6f 77 20 72 65 73 6f  e child row reso
271c6 6c 76 65 73 20 61 6e 20 0a 20 20 20 20 20 20 2a  lves an .      *
271c7 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 66 6f  * outstanding fo
271c8 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
271c9 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2e 20  aint violation. 
271ca 2a 2f 0a 20 20 20 20 20 20 66 6b 4c 6f 6f 6b 75  */.      fkLooku
271cb 70 50 61 72 65 6e 74 28 70 50 61 72 73 65 2c 20  pParent(pParse, 
271cc 69 44 62 2c 20 70 54 6f 2c 20 70 49 64 78 2c 20  iDb, pTo, pIdx, 
271cd 70 46 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65  pFKey, aiCol, re
271ce 67 4f 6c 64 2c 20 2d 31 2c 69 73 49 67 6e 6f 72  gOld, -1,isIgnor
271cf 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
271d0 28 20 72 65 67 4e 65 77 21 3d 30 20 29 7b 0a 20  ( regNew!=0 ){. 
271d1 20 20 20 20 20 2f 2a 20 41 20 72 6f 77 20 69 73       /* A row is
271d2 20 62 65 69 6e 67 20 61 64 64 65 64 20 74 6f 20   being added to 
271d3 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e  the child table.
271d4 20 49 66 20 61 20 70 61 72 65 6e 74 20 72 6f 77   If a parent row
271d5 20 63 61 6e 6e 6f 74 0a 20 20 20 20 20 20 2a 2a   cannot.      **
271d6 20 62 65 20 66 6f 75 6e 64 2c 20 61 64 64 69 6e   be found, addin
271d7 67 20 74 68 65 20 63 68 69 6c 64 20 72 6f 77 20  g the child row 
271d8 68 61 73 20 76 69 6f 6c 61 74 65 64 20 74 68 65  has violated the
271d9 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20   FK constraint. 
271da 2a 2f 20 0a 20 20 20 20 20 20 66 6b 4c 6f 6f 6b  */ .      fkLook
271db 75 70 50 61 72 65 6e 74 28 70 50 61 72 73 65 2c  upParent(pParse,
271dc 20 69 44 62 2c 20 70 54 6f 2c 20 70 49 64 78 2c   iDb, pTo, pIdx,
271dd 20 70 46 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72   pFKey, aiCol, r
271de 65 67 4e 65 77 2c 20 2b 31 2c 69 73 49 67 6e 6f  egNew, +1,isIgno
271df 72 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  re);.    }..    
271e0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
271e1 2c 20 61 69 46 72 65 65 29 3b 0a 20 20 7d 0a 0a  , aiFree);.  }..
271e2 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67    /* Loop throug
271e3 68 20 61 6c 6c 20 74 68 65 20 66 6f 72 65 69 67  h all the foreig
271e4 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
271e5 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20  s that refer to 
271e6 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
271e7 66 6f 72 28 70 46 4b 65 79 20 3d 20 73 71 6c 69  for(pFKey = sqli
271e8 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28  te3FkReferences(
271e9 70 54 61 62 29 3b 20 70 46 4b 65 79 3b 20 70 46  pTab); pFKey; pF
271ea 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74  Key=pFKey->pNext
271eb 54 6f 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  To){.    Index *
271ec 70 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20  pIdx = 0;       
271ed 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67         /* Foreig
271ee 6e 20 6b 65 79 20 69 6e 64 65 78 20 66 6f 72 20  n key index for 
271ef 70 46 4b 65 79 20 2a 2f 0a 20 20 20 20 53 72 63  pFKey */.    Src
271f0 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 20 20  List *pSrc;.    
271f1 69 6e 74 20 2a 61 69 43 6f 6c 20 3d 20 30 3b 0a  int *aiCol = 0;.
271f2 0a 20 20 20 20 69 66 28 20 21 70 46 4b 65 79 2d  .    if( !pFKey-
271f3 3e 69 73 44 65 66 65 72 72 65 64 20 26 26 20 21  >isDeferred && !
271f4 70 50 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65  pParse->pTopleve
271f5 6c 20 26 26 20 21 70 50 61 72 73 65 2d 3e 69 73  l && !pParse->is
271f6 4d 75 6c 74 69 57 72 69 74 65 20 29 7b 0a 20 20  MultiWrite ){.  
271f7 20 20 20 20 61 73 73 65 72 74 28 20 72 65 67 4f      assert( regO
271f8 6c 64 3d 3d 30 20 26 26 20 72 65 67 4e 65 77 21  ld==0 && regNew!
271f9 3d 30 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 49  =0 );.      /* I
271fa 6e 73 65 72 74 69 6e 67 20 61 20 73 69 6e 67 6c  nserting a singl
271fb 65 20 72 6f 77 20 69 6e 74 6f 20 61 20 70 61 72  e row into a par
271fc 65 6e 74 20 74 61 62 6c 65 20 63 61 6e 6e 6f 74  ent table cannot
271fd 20 63 61 75 73 65 20 61 6e 20 69 6d 6d 65 64 69   cause an immedi
271fe 61 74 65 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72  ate.      ** for
271ff 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69  eign key violati
27200 6f 6e 2e 20 53 6f 20 64 6f 20 6e 6f 74 68 69 6e  on. So do nothin
27201 67 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20  g in this case. 
27202 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e   */.      contin
27203 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ue;.    }..    i
27204 66 28 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64  f( locateFkeyInd
27205 65 78 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ex(pParse, pTab,
27206 20 70 46 4b 65 79 2c 20 26 70 49 64 78 2c 20 26   pFKey, &pIdx, &
27207 61 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  aiCol) ){.      
27208 69 66 28 20 21 69 73 49 67 6e 6f 72 65 45 72 72  if( !isIgnoreErr
27209 6f 72 73 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ors || db->mallo
2720a 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
2720b 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
2720c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
2720d 72 74 28 20 61 69 43 6f 6c 20 7c 7c 20 70 46 4b  rt( aiCol || pFK
2720e 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 0a  ey->nCol==1 );..
2720f 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20      /* Create a 
27210 53 72 63 4c 69 73 74 20 73 74 72 75 63 74 75 72  SrcList structur
27211 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 73  e containing a s
27212 69 6e 67 6c 65 20 74 61 62 6c 65 20 28 74 68 65  ingle table (the
27213 20 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 74   table .    ** t
27214 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74  he foreign key t
27215 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 74 68  hat refers to th
27216 69 73 20 74 61 62 6c 65 20 69 73 20 61 74 74 61  is table is atta
27217 63 68 65 64 20 74 6f 29 2e 20 54 68 69 73 0a 20  ched to). This. 
27218 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65     ** is require
27219 64 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65  d for the sqlite
2721a 33 57 68 65 72 65 58 58 58 28 29 20 69 6e 74 65  3WhereXXX() inte
2721b 72 66 61 63 65 2e 20 20 2a 2f 0a 20 20 20 20 70  rface.  */.    p
2721c 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Src = sqlite3Src
2721d 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20 30  ListAppend(db, 0
2721e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 0, 0);.    if(
2721f 20 70 53 72 63 20 29 7b 0a 20 20 20 20 20 20 73   pSrc ){.      s
27220 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
27221 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63  em *pItem = pSrc
27222 2d 3e 61 3b 0a 20 20 20 20 20 20 70 49 74 65 6d  ->a;.      pItem
27223 2d 3e 70 54 61 62 20 3d 20 70 46 4b 65 79 2d 3e  ->pTab = pFKey->
27224 70 46 72 6f 6d 3b 0a 20 20 20 20 20 20 70 49 74  pFrom;.      pIt
27225 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 70 46 4b 65  em->zName = pFKe
27226 79 2d 3e 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3b  y->pFrom->zName;
27227 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 54  .      pItem->pT
27228 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  ab->nRef++;.    
27229 20 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72    pItem->iCursor
2722a 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2722b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20  +;.  .      if( 
2722c 72 65 67 4e 65 77 21 3d 30 20 29 7b 0a 20 20 20  regNew!=0 ){.   
2722d 20 20 20 20 20 66 6b 53 63 61 6e 43 68 69 6c 64       fkScanChild
2722e 72 65 6e 28 70 50 61 72 73 65 2c 20 70 53 72 63  ren(pParse, pSrc
2722f 2c 20 70 54 61 62 2c 20 70 49 64 78 2c 20 70 46  , pTab, pIdx, pF
27230 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4e  Key, aiCol, regN
27231 65 77 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 7d  ew, -1);.      }
27232 0a 20 20 20 20 20 20 69 66 28 20 72 65 67 4f 6c  .      if( regOl
27233 64 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  d!=0 ){.        
27234 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
27235 20 52 45 53 54 52 49 43 54 20 61 63 74 69 6f 6e   RESTRICT action
27236 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20   configured for 
27237 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72  the current oper
27238 61 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  ation.        **
27239 20 6f 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74   on the parent t
2723a 61 62 6c 65 20 6f 66 20 74 68 69 73 20 46 4b 2c  able of this FK,
2723b 20 74 68 65 6e 20 74 68 72 6f 77 20 61 6e 20 65   then throw an e
2723c 78 63 65 70 74 69 6f 6e 20 0a 20 20 20 20 20 20  xception .      
2723d 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79    ** immediately
2723e 20 69 66 20 74 68 65 20 46 4b 20 63 6f 6e 73 74   if the FK const
2723f 72 61 69 6e 74 20 69 73 20 76 69 6f 6c 61 74 65  raint is violate
27240 64 2c 20 65 76 65 6e 20 69 66 20 74 68 69 73 20  d, even if this 
27241 69 73 20 61 0a 20 20 20 20 20 20 20 20 2a 2a 20  is a.        ** 
27242 64 65 66 65 72 72 65 64 20 74 72 69 67 67 65 72  deferred trigger
27243 2e 20 54 68 61 74 27 73 20 77 68 61 74 20 52 45  . That's what RE
27244 53 54 52 49 43 54 20 6d 65 61 6e 73 2e 20 54 6f  STRICT means. To
27245 20 64 65 66 65 72 20 63 68 65 63 6b 69 6e 67 0a   defer checking.
27246 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63          ** the c
27247 6f 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 20 46  onstraint, the F
27248 4b 20 73 68 6f 75 6c 64 20 73 70 65 63 69 66 79  K should specify
27249 20 4e 4f 20 41 43 54 49 4f 4e 20 28 72 65 70 72   NO ACTION (repr
2724a 65 73 65 6e 74 65 64 0a 20 20 20 20 20 20 20 20  esented.        
2724b 2a 2a 20 75 73 69 6e 67 20 4f 45 5f 4e 6f 6e 65  ** using OE_None
2724c 29 2e 20 4e 4f 20 41 43 54 49 4f 4e 20 69 73 20  ). NO ACTION is 
2724d 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 2a 2f  the default.  */
2724e 0a 20 20 20 20 20 20 20 20 66 6b 53 63 61 6e 43  .        fkScanC
2724f 68 69 6c 64 72 65 6e 28 70 50 61 72 73 65 2c 20  hildren(pParse, 
27250 70 53 72 63 2c 20 70 54 61 62 2c 20 70 49 64 78  pSrc, pTab, pIdx
27251 2c 20 70 46 4b 65 79 2c 20 61 69 43 6f 6c 2c 20  , pFKey, aiCol, 
27252 72 65 67 4f 6c 64 2c 20 31 29 3b 0a 20 20 20 20  regOld, 1);.    
27253 20 20 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d    }.      pItem-
27254 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20  >zName = 0;.    
27255 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
27256 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29  Delete(db, pSrc)
27257 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
27258 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 69  te3DbFree(db, ai
27259 43 6f 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 64 65  Col);.  }.}..#de
2725a 66 69 6e 65 20 43 4f 4c 55 4d 4e 5f 4d 41 53 4b  fine COLUMN_MASK
2725b 28 78 29 20 28 28 28 78 29 3e 33 31 29 20 3f 20  (x) (((x)>31) ? 
2725c 30 78 66 66 66 66 66 66 66 66 20 3a 20 28 28 75  0xffffffff : ((u
2725d 33 32 29 31 3c 3c 28 78 29 29 29 0a 0a 2f 2a 0a  32)1<<(x)))../*.
2725e 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2725f 20 69 73 20 63 61 6c 6c 65 64 20 62 65 66 6f 72   is called befor
27260 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64  e generating cod
27261 65 20 74 6f 20 75 70 64 61 74 65 20 6f 72 20 64  e to update or d
27262 65 6c 65 74 65 20 61 20 0a 2a 2a 20 72 6f 77 20  elete a .** row 
27263 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 61 62  contained in tab
27264 6c 65 20 70 54 61 62 2e 0a 2a 2f 0a 53 51 4c 49  le pTab..*/.SQLI
27265 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
27266 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28  qlite3FkOldmask(
27267 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
27268 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
27269 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
2726a 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
2726b 70 54 61 62 20 20 20 20 20 20 20 20 20 20 20 20  pTab            
2726c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2726d 65 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64  e being modified
2726e 20 2a 2f 0a 29 7b 0a 20 20 75 33 32 20 6d 61 73   */.){.  u32 mas
2726f 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61  k = 0;.  if( pPa
27270 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53  rse->db->flags&S
27271 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79  QLITE_ForeignKey
27272 73 20 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70  s ){.    FKey *p
27273 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
27274 20 66 6f 72 28 70 3d 70 54 61 62 2d 3e 70 46 4b   for(p=pTab->pFK
27275 65 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  ey; p; p=p->pNex
27276 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 66 6f  tFrom){.      fo
27277 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c  r(i=0; i<p->nCol
27278 3b 20 69 2b 2b 29 20 6d 61 73 6b 20 7c 3d 20 43  ; i++) mask |= C
27279 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 70 2d 3e 61 43  OLUMN_MASK(p->aC
2727a 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 29 3b 0a 20 20  ol[i].iFrom);.  
2727b 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 3d 73 71    }.    for(p=sq
2727c 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65  lite3FkReference
2727d 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d  s(pTab); p; p=p-
2727e 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 20  >pNextTo){.     
2727f 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30   Index *pIdx = 0
27280 3b 0a 20 20 20 20 20 20 6c 6f 63 61 74 65 46 6b  ;.      locateFk
27281 65 79 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  eyIndex(pParse, 
27282 70 54 61 62 2c 20 70 2c 20 26 70 49 64 78 2c 20  pTab, p, &pIdx, 
27283 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  0);.      if( pI
27284 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f  dx ){.        fo
27285 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e  r(i=0; i<pIdx->n
27286 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 20 6d 61 73  Column; i++) mas
27287 6b 20 7c 3d 20 43 4f 4c 55 4d 4e 5f 4d 41 53 4b  k |= COLUMN_MASK
27288 28 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  (pIdx->aiColumn[
27289 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i]);.      }.   
2728a 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2728b 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  mask;.}../*.** T
2728c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2728d 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 67 65  called before ge
2728e 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74 6f  nerating code to
2728f 20 75 70 64 61 74 65 20 6f 72 20 64 65 6c 65 74   update or delet
27290 65 20 61 20 0a 2a 2a 20 72 6f 77 20 63 6f 6e 74  e a .** row cont
27291 61 69 6e 65 64 20 69 6e 20 74 61 62 6c 65 20 70  ained in table p
27292 54 61 62 2e 20 49 66 20 74 68 65 20 6f 70 65 72  Tab. If the oper
27293 61 74 69 6f 6e 20 69 73 20 61 20 44 45 4c 45 54  ation is a DELET
27294 45 2c 20 74 68 65 6e 0a 2a 2a 20 70 61 72 61 6d  E, then.** param
27295 65 74 65 72 20 61 43 68 61 6e 67 65 20 69 73 20  eter aChange is 
27296 70 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 76 61  passed a NULL va
27297 6c 75 65 2e 20 46 6f 72 20 61 6e 20 55 50 44 41  lue. For an UPDA
27298 54 45 2c 20 61 43 68 61 6e 67 65 20 70 6f 69 6e  TE, aChange poin
27299 74 73 0a 2a 2a 20 74 6f 20 61 6e 20 61 72 72 61  ts.** to an arra
2729a 79 20 6f 66 20 73 69 7a 65 20 4e 2c 20 77 68 65  y of size N, whe
2729b 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62  re N is the numb
2729c 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
2729d 20 74 61 62 6c 65 20 70 54 61 62 2e 0a 2a 2a 20   table pTab..** 
2729e 49 66 20 74 68 65 20 69 27 74 68 20 63 6f 6c 75  If the i'th colu
2729f 6d 6e 20 69 73 20 6e 6f 74 20 6d 6f 64 69 66 69  mn is not modifi
272a0 65 64 20 62 79 20 74 68 65 20 55 50 44 41 54 45  ed by the UPDATE
272a1 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65  , then the corre
272a2 73 70 6f 6e 64 69 6e 67 20 0a 2a 2a 20 65 6e 74  sponding .** ent
272a3 72 79 20 69 6e 20 74 68 65 20 61 43 68 61 6e 67  ry in the aChang
272a4 65 5b 5d 20 61 72 72 61 79 20 69 73 20 73 65 74  e[] array is set
272a5 20 74 6f 20 2d 31 2e 20 49 66 20 74 68 65 20 63   to -1. If the c
272a6 6f 6c 75 6d 6e 20 69 73 20 6d 6f 64 69 66 69 65  olumn is modifie
272a7 64 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  d,.** the value 
272a8 69 73 20 30 20 6f 72 20 67 72 65 61 74 65 72 2e  is 0 or greater.
272a9 20 50 61 72 61 6d 65 74 65 72 20 63 68 6e 67 52   Parameter chngR
272aa 6f 77 69 64 20 69 73 20 73 65 74 20 74 6f 20 74  owid is set to t
272ab 72 75 65 20 69 66 20 74 68 65 0a 2a 2a 20 55 50  rue if the.** UP
272ac 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 6d  DATE statement m
272ad 6f 64 69 66 69 65 73 20 74 68 65 20 72 6f 77 69  odifies the rowi
272ae 64 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  d fields of the 
272af 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  table..**.** If 
272b0 61 6e 79 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  any foreign key 
272b1 70 72 6f 63 65 73 73 69 6e 67 20 77 69 6c 6c 20  processing will 
272b2 62 65 20 72 65 71 75 69 72 65 64 2c 20 74 68 69  be required, thi
272b3 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
272b4 6e 73 0a 2a 2a 20 74 72 75 65 2e 20 49 66 20 74  ns.** true. If t
272b5 68 65 72 65 20 69 73 20 6e 6f 20 66 6f 72 65 69  here is no forei
272b6 67 6e 20 6b 65 79 20 72 65 6c 61 74 65 64 20 70  gn key related p
272b7 72 6f 63 65 73 73 69 6e 67 2c 20 74 68 69 73 20  rocessing, this 
272b8 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 72 65 74  function .** ret
272b9 75 72 6e 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53  urns false..*/.S
272ba 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
272bb 74 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69  t sqlite3FkRequi
272bc 72 65 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50  red(.  Parse *pP
272bd 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
272be 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
272bf 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
272c0 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
272c1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
272c2 54 61 62 6c 65 20 62 65 69 6e 67 20 6d 6f 64 69  Table being modi
272c3 66 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  fied */.  int *a
272c4 43 68 61 6e 67 65 2c 20 20 20 20 20 20 20 20 20  Change,         
272c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e            /* Non
272c6 2d 4e 55 4c 4c 20 66 6f 72 20 55 50 44 41 54 45  -NULL for UPDATE
272c7 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20   operations */. 
272c8 20 69 6e 74 20 63 68 6e 67 52 6f 77 69 64 20 20   int chngRowid  
272c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
272ca 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44   /* True for UPD
272cb 41 54 45 20 74 68 61 74 20 61 66 66 65 63 74 73  ATE that affects
272cc 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69   rowid */.){.  i
272cd 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  f( pParse->db->f
272ce 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65  lags&SQLITE_Fore
272cf 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 69  ignKeys ){.    i
272d0 66 28 20 21 61 43 68 61 6e 67 65 20 29 7b 0a 20  f( !aChange ){. 
272d1 20 20 20 20 20 2f 2a 20 41 20 44 45 4c 45 54 45       /* A DELETE
272d2 20 6f 70 65 72 61 74 69 6f 6e 2e 20 46 6f 72 65   operation. Fore
272d3 69 67 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69  ign key processi
272d4 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 20 69  ng is required i
272d5 66 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20  f the .      ** 
272d6 74 61 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f  table in questio
272d7 6e 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20  n is either the 
272d8 63 68 69 6c 64 20 6f 72 20 70 61 72 65 6e 74 20  child or parent 
272d9 74 61 62 6c 65 20 66 6f 72 20 61 6e 79 20 0a 20  table for any . 
272da 20 20 20 20 20 2a 2a 20 66 6f 72 65 69 67 6e 20       ** foreign 
272db 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20  key constraint. 
272dc 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
272dd 20 28 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72   (sqlite3FkRefer
272de 65 6e 63 65 73 28 70 54 61 62 29 20 7c 7c 20 70  ences(pTab) || p
272df 54 61 62 2d 3e 70 46 4b 65 79 29 3b 0a 20 20 20  Tab->pFKey);.   
272e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
272e1 20 54 68 69 73 20 69 73 20 61 6e 20 55 50 44 41   This is an UPDA
272e2 54 45 2e 20 46 6f 72 65 69 67 6e 20 6b 65 79 20  TE. Foreign key 
272e3 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 6f 6e  processing is on
272e4 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 20 74  ly required if t
272e5 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 70 65 72  he.      ** oper
272e6 61 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 6f  ation modifies o
272e7 6e 65 20 6f 72 20 6d 6f 72 65 20 63 68 69 6c 64  ne or more child
272e8 20 6f 72 20 70 61 72 65 6e 74 20 6b 65 79 20 63   or parent key c
272e9 6f 6c 75 6d 6e 73 2e 20 2a 2f 0a 20 20 20 20 20  olumns. */.     
272ea 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 46 4b   int i;.      FK
272eb 65 79 20 2a 70 3b 0a 0a 20 20 20 20 20 20 2f 2a  ey *p;..      /*
272ec 20 43 68 65 63 6b 20 69 66 20 61 6e 79 20 63 68   Check if any ch
272ed 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20  ild key columns 
272ee 61 72 65 20 62 65 69 6e 67 20 6d 6f 64 69 66 69  are being modifi
272ef 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72  ed. */.      for
272f0 28 70 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20  (p=pTab->pFKey; 
272f1 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 46 72 6f  p; p=p->pNextFro
272f2 6d 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  m){.        for(
272f3 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
272f4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
272f5 69 6e 74 20 69 43 68 69 6c 64 4b 65 79 20 3d 20  int iChildKey = 
272f6 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d  p->aCol[i].iFrom
272f7 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
272f8 61 43 68 61 6e 67 65 5b 69 43 68 69 6c 64 4b 65  aChange[iChildKe
272f9 79 5d 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 31  y]>=0 ) return 1
272fa 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
272fb 69 43 68 69 6c 64 4b 65 79 3d 3d 70 54 61 62 2d  iChildKey==pTab-
272fc 3e 69 50 4b 65 79 20 26 26 20 63 68 6e 67 52 6f  >iPKey && chngRo
272fd 77 69 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  wid ) return 1;.
272fe 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
272ff 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63  }..      /* Chec
27300 6b 20 69 66 20 61 6e 79 20 70 61 72 65 6e 74 20  k if any parent 
27301 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20  key columns are 
27302 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20  being modified. 
27303 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 73  */.      for(p=s
27304 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63  qlite3FkReferenc
27305 65 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70  es(pTab); p; p=p
27306 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20  ->pNextTo){.    
27307 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
27308 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
27309 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4b          char *zK
2730a 65 79 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e  ey = p->aCol[i].
2730b 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20  zCol;.          
2730c 69 6e 74 20 69 4b 65 79 3b 0a 20 20 20 20 20 20  int iKey;.      
2730d 20 20 20 20 66 6f 72 28 69 4b 65 79 3d 30 3b 20      for(iKey=0; 
2730e 69 4b 65 79 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iKey<pTab->nCol;
2730f 20 69 4b 65 79 2b 2b 29 7b 0a 20 20 20 20 20 20   iKey++){.      
27310 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43        Column *pC
27311 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c  ol = &pTab->aCol
27312 5b 69 4b 65 79 5d 3b 0a 20 20 20 20 20 20 20 20  [iKey];.        
27313 20 20 20 20 69 66 28 20 28 7a 4b 65 79 20 3f 20      if( (zKey ? 
27314 21 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  !sqlite3StrICmp(
27315 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 4b 65  pCol->zName, zKe
27316 79 29 20 3a 20 70 43 6f 6c 2d 3e 69 73 50 72 69  y) : pCol->isPri
27317 6d 4b 65 79 29 20 29 7b 0a 20 20 20 20 20 20 20  mKey) ){.       
27318 20 20 20 20 20 20 20 69 66 28 20 61 43 68 61 6e         if( aChan
27319 67 65 5b 69 4b 65 79 5d 3e 3d 30 20 29 20 72 65  ge[iKey]>=0 ) re
2731a 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20  turn 1;.        
2731b 20 20 20 20 20 20 69 66 28 20 69 4b 65 79 3d 3d        if( iKey==
2731c 70 54 61 62 2d 3e 69 50 4b 65 79 20 26 26 20 63  pTab->iPKey && c
2731d 68 6e 67 52 6f 77 69 64 20 29 20 72 65 74 75 72  hngRowid ) retur
2731e 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20  n 1;.           
2731f 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
27320 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
27321 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
27322 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
27323 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
27324 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20   called when an 
27325 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
27326 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 62 65   operation is be
27327 69 6e 67 20 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64  ing .** compiled
27328 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 2c 20   on table pTab, 
27329 77 68 69 63 68 20 69 73 20 74 68 65 20 70 61 72  which is the par
2732a 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 66 6f 72  ent table of for
2732b 65 69 67 6e 2d 6b 65 79 20 70 46 4b 65 79 2e 0a  eign-key pFKey..
2732c 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e  ** If the curren
2732d 74 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61  t operation is a
2732e 6e 20 55 50 44 41 54 45 2c 20 74 68 65 6e 20 74  n UPDATE, then t
2732f 68 65 20 70 43 68 61 6e 67 65 73 20 70 61 72 61  he pChanges para
27330 6d 65 74 65 72 20 69 73 0a 2a 2a 20 70 61 73 73  meter is.** pass
27331 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ed a pointer to 
27332 74 68 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75  the list of colu
27333 6d 6e 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69  mns being modifi
27334 65 64 2e 20 49 66 20 69 74 20 69 73 20 61 0a 2a  ed. If it is a.*
27335 2a 20 44 45 4c 45 54 45 2c 20 70 43 68 61 6e 67  * DELETE, pChang
27336 65 73 20 69 73 20 70 61 73 73 65 64 20 61 20 4e  es is passed a N
27337 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
27338 2a 2a 20 49 74 20 72 65 74 75 72 6e 73 20 61 20  ** It returns a 
27339 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 72 69  pointer to a Tri
2733a 67 67 65 72 20 73 74 72 75 63 74 75 72 65 20 63  gger structure c
2733b 6f 6e 74 61 69 6e 69 6e 67 20 61 20 74 72 69 67  ontaining a trig
2733c 67 65 72 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e  ger.** equivalen
2733d 74 20 74 6f 20 74 68 65 20 4f 4e 20 55 50 44 41  t to the ON UPDA
2733e 54 45 20 6f 72 20 4f 4e 20 44 45 4c 45 54 45 20  TE or ON DELETE 
2733f 61 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64  action specified
27340 20 62 79 20 70 46 4b 65 79 2e 0a 2a 2a 20 49 66   by pFKey..** If
27341 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 22   the action is "
27342 4e 4f 20 41 43 54 49 4f 4e 22 20 6f 72 20 22 52  NO ACTION" or "R
27343 45 53 54 52 49 43 54 22 2c 20 74 68 65 6e 20 61  ESTRICT", then a
27344 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73   NULL pointer is
27345 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 28 74 68  .** returned (th
27346 65 73 65 20 61 63 74 69 6f 6e 73 20 72 65 71 75  ese actions requ
27347 69 72 65 20 6e 6f 20 73 70 65 63 69 61 6c 20 68  ire no special h
27348 61 6e 64 6c 69 6e 67 20 62 79 20 74 68 65 20 74  andling by the t
27349 72 69 67 67 65 72 73 0a 2a 2a 20 73 75 62 2d 73  riggers.** sub-s
2734a 79 73 74 65 6d 2c 20 63 6f 64 65 20 66 6f 72 20  ystem, code for 
2734b 74 68 65 6d 20 69 73 20 63 72 65 61 74 65 64 20  them is created 
2734c 62 79 20 66 6b 53 63 61 6e 43 68 69 6c 64 72 65  by fkScanChildre
2734d 6e 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  n())..**.** For 
2734e 65 78 61 6d 70 6c 65 2c 20 69 66 20 70 46 4b 65  example, if pFKe
2734f 79 20 69 73 20 74 68 65 20 66 6f 72 65 69 67 6e  y is the foreign
27350 20 6b 65 79 20 61 6e 64 20 70 54 61 62 20 69 73   key and pTab is
27351 20 74 61 62 6c 65 20 22 70 22 20 69 6e 20 0a 2a   table "p" in .*
27352 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * the following 
27353 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 20 20  schema:.**.**   
27354 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 28 70  CREATE TABLE p(p
27355 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a  k PRIMARY KEY);.
27356 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  **   CREATE TABL
27357 45 20 63 28 63 6b 20 52 45 46 45 52 45 4e 43 45  E c(ck REFERENCE
27358 53 20 70 20 4f 4e 20 44 45 4c 45 54 45 20 43 41  S p ON DELETE CA
27359 53 43 41 44 45 29 3b 0a 2a 2a 0a 2a 2a 20 74 68  SCADE);.**.** th
2735a 65 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  en the returned 
2735b 74 72 69 67 67 65 72 20 73 74 72 75 63 74 75 72  trigger structur
2735c 65 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20  e is equivalent 
2735d 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52 45 41  to:.**.**   CREA
2735e 54 45 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 44  TE TRIGGER ... D
2735f 45 4c 45 54 45 20 4f 4e 20 70 20 42 45 47 49 4e  ELETE ON p BEGIN
27360 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46  .**     DELETE F
27361 52 4f 4d 20 63 20 57 48 45 52 45 20 63 6b 20 3d  ROM c WHERE ck =
27362 20 6f 6c 64 2e 70 6b 3b 0a 2a 2a 20 20 20 45 4e   old.pk;.**   EN
27363 44 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  D;.**.** The ret
27364 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 69 73  urned pointer is
27365 20 63 61 63 68 65 64 20 61 73 20 70 61 72 74 20   cached as part 
27366 6f 66 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  of the foreign k
27367 65 79 20 6f 62 6a 65 63 74 2e 20 49 74 0a 2a 2a  ey object. It.**
27368 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66   is eventually f
27369 72 65 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20  reed along with 
2736a 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20  the rest of the 
2736b 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 62 6a 65  foreign key obje
2736c 63 74 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65  ct by .** sqlite
2736d 33 46 6b 44 65 6c 65 74 65 28 29 2e 0a 2a 2f 0a  3FkDelete()..*/.
2736e 73 74 61 74 69 63 20 54 72 69 67 67 65 72 20 2a  static Trigger *
2736f 66 6b 41 63 74 69 6f 6e 54 72 69 67 67 65 72 28  fkActionTrigger(
27370 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
27371 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
27372 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
27373 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
27374 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  pTab,           
27375 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
27376 65 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 20  e being updated 
27377 6f 72 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20  or deleted from 
27378 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79  */.  FKey *pFKey
27379 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2737a 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20       /* Foreign 
2737b 6b 65 79 20 74 6f 20 67 65 74 20 61 63 74 69 6f  key to get actio
2737c 6e 20 66 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c  n for */.  ExprL
2737d 69 73 74 20 2a 70 43 68 61 6e 67 65 73 20 20 20  ist *pChanges   
2737e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68             /* Ch
2737f 61 6e 67 65 2d 6c 69 73 74 20 66 6f 72 20 55 50  ange-list for UP
27380 44 41 54 45 2c 20 4e 55 4c 4c 20 66 6f 72 20 44  DATE, NULL for D
27381 45 4c 45 54 45 20 2a 2f 0a 29 7b 0a 20 20 73 71  ELETE */.){.  sq
27382 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
27383 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a  se->db;       /*
27384 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
27385 20 2a 2f 0a 20 20 69 6e 74 20 61 63 74 69 6f 6e   */.  int action
27386 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
27387 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
27388 4f 45 5f 4e 6f 6e 65 2c 20 4f 45 5f 43 61 73 63  OE_None, OE_Casc
27389 61 64 65 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72  ade etc. */.  Tr
2738a 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
2738b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2738c 20 54 72 69 67 67 65 72 20 64 65 66 69 6e 69 74   Trigger definit
2738d 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  ion to return */
2738e 0a 20 20 69 6e 74 20 69 41 63 74 69 6f 6e 20 3d  .  int iAction =
2738f 20 28 70 43 68 61 6e 67 65 73 21 3d 30 29 3b 20   (pChanges!=0); 
27390 20 20 20 2f 2a 20 31 20 66 6f 72 20 55 50 44 41     /* 1 for UPDA
27391 54 45 2c 20 30 20 66 6f 72 20 44 45 4c 45 54 45  TE, 0 for DELETE
27392 20 2a 2f 0a 0a 20 20 61 63 74 69 6f 6e 20 3d 20   */..  action = 
27393 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 69  pFKey->aAction[i
27394 41 63 74 69 6f 6e 5d 3b 0a 20 20 70 54 72 69 67  Action];.  pTrig
27395 67 65 72 20 3d 20 70 46 4b 65 79 2d 3e 61 70 54  ger = pFKey->apT
27396 72 69 67 67 65 72 5b 69 41 63 74 69 6f 6e 5d 3b  rigger[iAction];
27397 0a 0a 20 20 69 66 28 20 61 63 74 69 6f 6e 21 3d  ..  if( action!=
27398 4f 45 5f 4e 6f 6e 65 20 26 26 20 21 70 54 72 69  OE_None && !pTri
27399 67 67 65 72 20 29 7b 0a 20 20 20 20 75 38 20 65  gger ){.    u8 e
2739a 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 20  nableLookaside; 
2739b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70            /* Cop
2739c 79 20 6f 66 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  y of db->lookasi
2739d 64 65 2e 62 45 6e 61 62 6c 65 64 20 2a 2f 0a 20  de.bEnabled */. 
2739e 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a     char const *z
2739f 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20  From;           
273a0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 68 69 6c   /* Name of chil
273a1 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69  d table */.    i
273a2 6e 74 20 6e 46 72 6f 6d 3b 20 20 20 20 20 20 20  nt nFrom;       
273a3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
273a4 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20  Length in bytes 
273a5 6f 66 20 7a 46 72 6f 6d 20 2a 2f 0a 20 20 20 20  of zFrom */.    
273a6 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b  Index *pIdx = 0;
273a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
273a8 20 50 61 72 65 6e 74 20 6b 65 79 20 69 6e 64 65   Parent key inde
273a9 78 20 66 6f 72 20 74 68 69 73 20 46 4b 20 2a 2f  x for this FK */
273aa 0a 20 20 20 20 69 6e 74 20 2a 61 69 43 6f 6c 20  .    int *aiCol 
273ab 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
273ac 20 20 20 2f 2a 20 63 68 69 6c 64 20 74 61 62 6c     /* child tabl
273ad 65 20 63 6f 6c 73 20 2d 3e 20 70 61 72 65 6e 74  e cols -> parent
273ae 20 6b 65 79 20 63 6f 6c 73 20 2a 2f 0a 20 20 20   key cols */.   
273af 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
273b0 74 65 70 20 3d 20 30 3b 20 20 20 20 20 20 20 20  tep = 0;        
273b1 2f 2a 20 46 69 72 73 74 20 28 6f 6e 6c 79 29 20  /* First (only) 
273b2 73 74 65 70 20 6f 66 20 74 72 69 67 67 65 72 20  step of trigger 
273b3 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20 45  program */.    E
273b4 78 70 72 20 2a 70 57 68 65 72 65 20 3d 20 30 3b  xpr *pWhere = 0;
273b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
273b6 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20  WHERE clause of 
273b7 74 72 69 67 67 65 72 20 73 74 65 70 20 2a 2f 0a  trigger step */.
273b8 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
273b9 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ist = 0;        
273ba 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 6c 69 73    /* Changes lis
273bb 74 20 69 66 20 4f 4e 20 55 50 44 41 54 45 20 43  t if ON UPDATE C
273bc 41 53 43 41 44 45 20 2a 2f 0a 20 20 20 20 53 65  ASCADE */.    Se
273bd 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20  lect *pSelect = 
273be 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  0;          /* I
273bf 66 20 52 45 53 54 52 49 43 54 2c 20 22 53 45 4c  f RESTRICT, "SEL
273c0 45 43 54 20 52 41 49 53 45 28 2e 2e 2e 29 22 20  ECT RAISE(...)" 
273c1 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20  */.    int i;   
273c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
273c3 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
273c4 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20   variable */.   
273c5 20 45 78 70 72 20 2a 70 57 68 65 6e 20 3d 20 30   Expr *pWhen = 0
273c6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
273c7 2a 20 57 48 45 4e 20 63 6c 61 75 73 65 20 66 6f  * WHEN clause fo
273c8 72 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f  r the trigger */
273c9 0a 0a 20 20 20 20 69 66 28 20 6c 6f 63 61 74 65  ..    if( locate
273ca 46 6b 65 79 49 6e 64 65 78 28 70 50 61 72 73 65  FkeyIndex(pParse
273cb 2c 20 70 54 61 62 2c 20 70 46 4b 65 79 2c 20 26  , pTab, pFKey, &
273cc 70 49 64 78 2c 20 26 61 69 43 6f 6c 29 20 29 20  pIdx, &aiCol) ) 
273cd 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 61 73  return 0;.    as
273ce 73 65 72 74 28 20 61 69 43 6f 6c 20 7c 7c 20 70  sert( aiCol || p
273cf 46 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20 29 3b  FKey->nCol==1 );
273d0 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
273d1 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b  <pFKey->nCol; i+
273d2 2b 29 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20  +){.      Token 
273d3 74 4f 6c 64 20 3d 20 7b 20 22 6f 6c 64 22 2c 20  tOld = { "old", 
273d4 33 20 7d 3b 20 20 2f 2a 20 4c 69 74 65 72 61 6c  3 };  /* Literal
273d5 20 22 6f 6c 64 22 20 74 6f 6b 65 6e 20 2a 2f 0a   "old" token */.
273d6 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 4e 65 77        Token tNew
273d7 20 3d 20 7b 20 22 6e 65 77 22 2c 20 33 20 7d 3b   = { "new", 3 };
273d8 20 20 2f 2a 20 4c 69 74 65 72 61 6c 20 22 6e 65    /* Literal "ne
273d9 77 22 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  w" token */.    
273da 20 20 54 6f 6b 65 6e 20 74 46 72 6f 6d 43 6f 6c    Token tFromCol
273db 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
273dc 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20   Name of column 
273dd 69 6e 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a  in child table *
273de 2f 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 54  /.      Token tT
273df 6f 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  oCol;           
273e0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63      /* Name of c
273e1 6f 6c 75 6d 6e 20 69 6e 20 70 61 72 65 6e 74 20  olumn in parent 
273e2 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69  table */.      i
273e3 6e 74 20 69 46 72 6f 6d 43 6f 6c 3b 20 20 20 20  nt iFromCol;    
273e4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 64             /* Id
273e5 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63  x of column in c
273e6 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hild table */.  
273e7 20 20 20 20 45 78 70 72 20 2a 70 45 71 3b 20 20      Expr *pEq;  
273e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
273e9 2f 2a 20 74 46 72 6f 6d 43 6f 6c 20 3d 20 4f 4c  /* tFromCol = OL
273ea 44 2e 74 54 6f 43 6f 6c 20 2a 2f 0a 0a 20 20 20  D.tToCol */..   
273eb 20 20 20 69 46 72 6f 6d 43 6f 6c 20 3d 20 61 69     iFromCol = ai
273ec 43 6f 6c 20 3f 20 61 69 43 6f 6c 5b 69 5d 20 3a  Col ? aiCol[i] :
273ed 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e   pFKey->aCol[0].
273ee 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20 61 73 73  iFrom;.      ass
273ef 65 72 74 28 20 69 46 72 6f 6d 43 6f 6c 3e 3d 30  ert( iFromCol>=0
273f0 20 29 3b 0a 20 20 20 20 20 20 74 54 6f 43 6f 6c   );.      tToCol
273f1 2e 7a 20 3d 20 70 49 64 78 20 3f 20 70 54 61 62  .z = pIdx ? pTab
273f2 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43  ->aCol[pIdx->aiC
273f3 6f 6c 75 6d 6e 5b 69 5d 5d 2e 7a 4e 61 6d 65 20  olumn[i]].zName 
273f4 3a 20 22 6f 69 64 22 3b 0a 20 20 20 20 20 20 74  : "oid";.      t
273f5 46 72 6f 6d 43 6f 6c 2e 7a 20 3d 20 70 46 4b 65  FromCol.z = pFKe
273f6 79 2d 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69  y->pFrom->aCol[i
273f7 46 72 6f 6d 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a  FromCol].zName;.
273f8 0a 20 20 20 20 20 20 74 54 6f 43 6f 6c 2e 6e 20  .      tToCol.n 
273f9 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
273fa 30 28 74 54 6f 43 6f 6c 2e 7a 29 3b 0a 20 20 20  0(tToCol.z);.   
273fb 20 20 20 74 46 72 6f 6d 43 6f 6c 2e 6e 20 3d 20     tFromCol.n = 
273fc 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
273fd 74 46 72 6f 6d 43 6f 6c 2e 7a 29 3b 0a 0a 20 20  tFromCol.z);..  
273fe 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68      /* Create th
273ff 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 4f 4c  e expression "OL
27400 44 2e 7a 54 6f 43 6f 6c 20 3d 20 7a 46 72 6f 6d  D.zToCol = zFrom
27401 43 6f 6c 22 2e 20 49 74 20 69 73 20 69 6d 70 6f  Col". It is impo
27402 72 74 61 6e 74 0a 20 20 20 20 20 20 2a 2a 20 74  rtant.      ** t
27403 68 61 74 20 74 68 65 20 22 4f 4c 44 2e 7a 54 6f  hat the "OLD.zTo
27404 43 6f 6c 22 20 74 65 72 6d 20 69 73 20 6f 6e 20  Col" term is on 
27405 74 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20 3d  the LHS of the =
27406 20 6f 70 65 72 61 74 6f 72 2c 20 73 6f 0a 20 20   operator, so.  
27407 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20      ** that the 
27408 61 66 66 69 6e 69 74 79 20 61 6e 64 20 63 6f 6c  affinity and col
27409 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2740a 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2740b 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 72  the.      ** par
2740c 65 6e 74 20 74 61 62 6c 65 20 61 72 65 20 75 73  ent table are us
2740d 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61  ed for the compa
2740e 72 69 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  rison. */.      
2740f 70 45 71 20 3d 20 73 71 6c 69 74 65 33 50 45 78  pEq = sqlite3PEx
27410 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 51  pr(pParse, TK_EQ
27411 2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ,.          sqli
27412 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
27413 20 54 4b 5f 44 4f 54 2c 20 0a 20 20 20 20 20 20   TK_DOT, .      
27414 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78        sqlite3PEx
27415 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44  pr(pParse, TK_ID
27416 2c 20 30 2c 20 30 2c 20 26 74 4f 6c 64 29 2c 0a  , 0, 0, &tOld),.
27417 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
27418 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
27419 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74   TK_ID, 0, 0, &t
2741a 54 6f 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20  ToCol).         
2741b 20 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20   , 0),.         
2741c 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2741d 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20  arse, TK_ID, 0, 
2741e 30 2c 20 26 74 46 72 6f 6d 43 6f 6c 29 0a 20 20  0, &tFromCol).  
2741f 20 20 20 20 2c 20 30 29 3b 0a 20 20 20 20 20 20      , 0);.      
27420 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
27421 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57 68 65  ExprAnd(db, pWhe
27422 72 65 2c 20 70 45 71 29 3b 0a 0a 20 20 20 20 20  re, pEq);..     
27423 20 2f 2a 20 46 6f 72 20 4f 4e 20 55 50 44 41 54   /* For ON UPDAT
27424 45 2c 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65  E, construct the
27425 20 6e 65 78 74 20 74 65 72 6d 20 6f 66 20 74 68   next term of th
27426 65 20 57 48 45 4e 20 63 6c 61 75 73 65 2e 0a 20  e WHEN clause.. 
27427 20 20 20 20 20 2a 2a 20 54 68 65 20 66 69 6e 61       ** The fina
27428 6c 20 57 48 45 4e 20 63 6c 61 75 73 65 20 77 69  l WHEN clause wi
27429 6c 6c 20 62 65 20 6c 69 6b 65 20 74 68 69 73 3a  ll be like this:
2742a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
2742b 2a 2a 20 20 20 20 57 48 45 4e 20 4e 4f 54 28 6f  **    WHEN NOT(o
2742c 6c 64 2e 63 6f 6c 31 20 49 53 20 6e 65 77 2e 63  ld.col1 IS new.c
2742d 6f 6c 31 20 41 4e 44 20 2e 2e 2e 20 41 4e 44 20  ol1 AND ... AND 
2742e 6f 6c 64 2e 63 6f 6c 4e 20 49 53 20 6e 65 77 2e  old.colN IS new.
2742f 63 6f 6c 4e 29 0a 20 20 20 20 20 20 2a 2f 0a 20  colN).      */. 
27430 20 20 20 20 20 69 66 28 20 70 43 68 61 6e 67 65       if( pChange
27431 73 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 71  s ){.        pEq
27432 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
27433 70 50 61 72 73 65 2c 20 54 4b 5f 49 53 2c 0a 20  pParse, TK_IS,. 
27434 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27435 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
27436 54 4b 5f 44 4f 54 2c 20 0a 20 20 20 20 20 20 20  TK_DOT, .       
27437 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
27438 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
27439 44 2c 20 30 2c 20 30 2c 20 26 74 4f 6c 64 29 2c  D, 0, 0, &tOld),
2743a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
2743b 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2743c 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
2743d 20 26 74 54 6f 43 6f 6c 29 2c 0a 20 20 20 20 20   &tToCol),.     
2743e 20 20 20 20 20 20 20 20 20 30 29 2c 0a 20 20 20           0),.   
2743f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27440 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
27441 5f 44 4f 54 2c 20 0a 20 20 20 20 20 20 20 20 20  _DOT, .         
27442 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70       sqlite3PExp
27443 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
27444 20 30 2c 20 30 2c 20 26 74 4e 65 77 29 2c 0a 20   0, 0, &tNew),. 
27445 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
27446 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
27447 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
27448 74 54 6f 43 6f 6c 29 2c 0a 20 20 20 20 20 20 20  tToCol),.       
27449 20 20 20 20 20 20 20 30 29 2c 0a 20 20 20 20 20         0),.     
2744a 20 20 20 20 20 20 20 30 29 3b 0a 20 20 20 20 20         0);.     
2744b 20 20 20 70 57 68 65 6e 20 3d 20 73 71 6c 69 74     pWhen = sqlit
2744c 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57  e3ExprAnd(db, pW
2744d 68 65 6e 2c 20 70 45 71 29 3b 0a 20 20 20 20 20  hen, pEq);.     
2744e 20 7d 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20   }.  .      if( 
2744f 61 63 74 69 6f 6e 21 3d 4f 45 5f 52 65 73 74 72  action!=OE_Restr
27450 69 63 74 20 26 26 20 28 61 63 74 69 6f 6e 21 3d  ict && (action!=
27451 4f 45 5f 43 61 73 63 61 64 65 20 7c 7c 20 70 43  OE_Cascade || pC
27452 68 61 6e 67 65 73 29 20 29 7b 0a 20 20 20 20 20  hanges) ){.     
27453 20 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20     Expr *pNew;. 
27454 20 20 20 20 20 20 20 69 66 28 20 61 63 74 69 6f         if( actio
27455 6e 3d 3d 4f 45 5f 43 61 73 63 61 64 65 20 29 7b  n==OE_Cascade ){
27456 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 20  .          pNew 
27457 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
27458 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 0a  Parse, TK_DOT, .
27459 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2745a 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2745b 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74   TK_ID, 0, 0, &t
2745c 4e 65 77 29 2c 0a 20 20 20 20 20 20 20 20 20 20  New),.          
2745d 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70    sqlite3PExpr(p
2745e 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
2745f 20 30 2c 20 26 74 54 6f 43 6f 6c 29 0a 20 20 20   0, &tToCol).   
27460 20 20 20 20 20 20 20 2c 20 30 29 3b 0a 20 20 20         , 0);.   
27461 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61       }else if( a
27462 63 74 69 6f 6e 3d 3d 4f 45 5f 53 65 74 44 66 6c  ction==OE_SetDfl
27463 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45  t ){.          E
27464 78 70 72 20 2a 70 44 66 6c 74 20 3d 20 70 46 4b  xpr *pDflt = pFK
27465 65 79 2d 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b  ey->pFrom->aCol[
27466 69 46 72 6f 6d 43 6f 6c 5d 2e 70 44 66 6c 74 3b  iFromCol].pDflt;
27467 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
27468 44 66 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Dflt ){.        
27469 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
2746a 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 44  e3ExprDup(db, pD
2746b 66 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  flt, 0);.       
2746c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2746d 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
2746e 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2746f 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c  , TK_NULL, 0, 0,
27470 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   0);.          }
27471 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
27472 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d            pNew =
27473 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
27474 61 72 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30  arse, TK_NULL, 0
27475 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
27476 20 7d 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74   }.        pList
27477 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
27478 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
27479 20 70 4c 69 73 74 2c 20 70 4e 65 77 29 3b 0a 20   pList, pNew);. 
2747a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2747b 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50  prListSetName(pP
2747c 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 26 74 46  arse, pList, &tF
2747d 72 6f 6d 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20  romCol, 0);.    
2747e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
2747f 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
27480 61 69 43 6f 6c 29 3b 0a 0a 20 20 20 20 7a 46 72  aiCol);..    zFr
27481 6f 6d 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f  om = pFKey->pFro
27482 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 46  m->zName;.    nF
27483 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 53 74 72  rom = sqlite3Str
27484 6c 65 6e 33 30 28 7a 46 72 6f 6d 29 3b 0a 0a 20  len30(zFrom);.. 
27485 20 20 20 69 66 28 20 61 63 74 69 6f 6e 3d 3d 4f     if( action==O
27486 45 5f 52 65 73 74 72 69 63 74 20 29 7b 0a 20 20  E_Restrict ){.  
27487 20 20 20 20 54 6f 6b 65 6e 20 74 46 72 6f 6d 3b      Token tFrom;
27488 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 61  .      Expr *pRa
27489 69 73 65 3b 20 0a 0a 20 20 20 20 20 20 74 46 72  ise; ..      tFr
2748a 6f 6d 2e 7a 20 3d 20 7a 46 72 6f 6d 3b 0a 20 20  om.z = zFrom;.  
2748b 20 20 20 20 74 46 72 6f 6d 2e 6e 20 3d 20 6e 46      tFrom.n = nF
2748c 72 6f 6d 3b 0a 20 20 20 20 20 20 70 52 61 69 73  rom;.      pRais
2748d 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  e = sqlite3Expr(
2748e 64 62 2c 20 54 4b 5f 52 41 49 53 45 2c 20 22 66  db, TK_RAISE, "f
2748f 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
27490 72 61 69 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a  raint failed");.
27491 20 20 20 20 20 20 69 66 28 20 70 52 61 69 73 65        if( pRaise
27492 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 61 69   ){.        pRai
27493 73 65 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 4f  se->affinity = O
27494 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d  E_Abort;.      }
27495 0a 20 20 20 20 20 20 70 53 65 6c 65 63 74 20 3d  .      pSelect =
27496 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
27497 77 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  w(pParse, .     
27498 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
27499 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2749a 65 2c 20 30 2c 20 70 52 61 69 73 65 29 2c 0a 20  e, 0, pRaise),. 
2749b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2749c 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62  SrcListAppend(db
2749d 2c 20 30 2c 20 26 74 46 72 6f 6d 2c 20 30 29 2c  , 0, &tFrom, 0),
2749e 0a 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72  .          pWher
2749f 65 2c 0a 20 20 20 20 20 20 20 20 20 20 30 2c 20  e,.          0, 
274a0 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0a 20 20  0, 0, 0, 0, 0.  
274a1 20 20 20 20 29 3b 0a 20 20 20 20 20 20 70 57 68      );.      pWh
274a2 65 72 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  ere = 0;.    }..
274a3 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 20 63 75      /* In the cu
274a4 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61  rrent implementa
274a5 74 69 6f 6e 2c 20 70 54 61 62 2d 3e 64 62 4d 65  tion, pTab->dbMe
274a6 6d 3d 3d 30 20 66 6f 72 20 61 6c 6c 20 74 61 62  m==0 for all tab
274a7 6c 65 73 20 65 78 63 65 70 74 0a 20 20 20 20 2a  les except.    *
274a8 2a 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20  * for temporary 
274a9 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 64  tables used to d
274aa 65 73 63 72 69 62 65 20 73 75 62 71 75 65 72 69  escribe subqueri
274ab 65 73 2e 20 20 41 6e 64 20 74 65 6d 70 6f 72 61  es.  And tempora
274ac 72 79 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 73  ry.    ** tables
274ad 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 66 6f 72   do not have for
274ae 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
274af 69 6e 74 73 2e 20 20 48 65 6e 63 65 2c 20 70 54  ints.  Hence, pT
274b0 61 62 2d 3e 64 62 4d 65 6d 0a 20 20 20 20 2a 2a  ab->dbMem.    **
274b1 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62   should always b
274b2 65 20 30 20 74 68 65 72 65 2e 0a 20 20 20 20 2a  e 0 there..    *
274b3 2f 0a 20 20 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b  /.    enableLook
274b4 61 73 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  aside = db->look
274b5 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a  aside.bEnabled;.
274b6 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
274b7 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a  e.bEnabled = 0;.
274b8 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20  .    pTrigger = 
274b9 28 54 72 69 67 67 65 72 20 2a 29 73 71 6c 69 74  (Trigger *)sqlit
274ba 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
274bb 62 2c 20 0a 20 20 20 20 20 20 20 20 73 69 7a 65  b, .        size
274bc 6f 66 28 54 72 69 67 67 65 72 29 20 2b 20 20 20  of(Trigger) +   
274bd 20 20 20 20 20 20 2f 2a 20 73 74 72 75 63 74 20        /* struct 
274be 54 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 20  Trigger */.     
274bf 20 20 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65     sizeof(Trigge
274c0 72 53 74 65 70 29 20 2b 20 20 20 20 20 2f 2a 20  rStep) +     /* 
274c1 53 69 6e 67 6c 65 20 73 74 65 70 20 69 6e 20 74  Single step in t
274c2 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a  rigger program *
274c3 2f 0a 20 20 20 20 20 20 20 20 6e 46 72 6f 6d 20  /.        nFrom 
274c4 2b 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20  + 1             
274c5 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
274c6 20 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a   pStep->target.z
274c7 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69   */.    );.    i
274c8 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
274c9 20 20 20 20 20 70 53 74 65 70 20 3d 20 70 54 72       pStep = pTr
274ca 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74  igger->step_list
274cb 20 3d 20 28 54 72 69 67 67 65 72 53 74 65 70 20   = (TriggerStep 
274cc 2a 29 26 70 54 72 69 67 67 65 72 5b 31 5d 3b 0a  *)&pTrigger[1];.
274cd 20 20 20 20 20 20 70 53 74 65 70 2d 3e 74 61 72        pStep->tar
274ce 67 65 74 2e 7a 20 3d 20 28 63 68 61 72 20 2a 29  get.z = (char *)
274cf 26 70 53 74 65 70 5b 31 5d 3b 0a 20 20 20 20 20  &pStep[1];.     
274d0 20 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 6e   pStep->target.n
274d1 20 3d 20 6e 46 72 6f 6d 3b 0a 20 20 20 20 20 20   = nFrom;.      
274d2 6d 65 6d 63 70 79 28 28 63 68 61 72 20 2a 29 70  memcpy((char *)p
274d3 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a 2c 20  Step->target.z, 
274d4 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 29 3b 0a 20  zFrom, nFrom);. 
274d5 20 0a 20 20 20 20 20 20 70 53 74 65 70 2d 3e 70   .      pStep->p
274d6 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
274d7 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 72  xprDup(db, pWher
274d8 65 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43  e, EXPRDUP_REDUC
274d9 45 29 3b 0a 20 20 20 20 20 20 70 53 74 65 70 2d  E);.      pStep-
274da 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c  >pExprList = sql
274db 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
274dc 64 62 2c 20 70 4c 69 73 74 2c 20 45 58 50 52 44  db, pList, EXPRD
274dd 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20  UP_REDUCE);.    
274de 20 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74    pStep->pSelect
274df 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
274e0 44 75 70 28 64 62 2c 20 70 53 65 6c 65 63 74 2c  Dup(db, pSelect,
274e1 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
274e2 3b 0a 20 20 20 20 20 20 69 66 28 20 70 57 68 65  ;.      if( pWhe
274e3 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 57 68  n ){.        pWh
274e4 65 6e 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  en = sqlite3PExp
274e5 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
274e6 2c 20 70 57 68 65 6e 2c 20 30 2c 20 30 29 3b 0a  , pWhen, 0, 0);.
274e7 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72          pTrigger
274e8 2d 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65  ->pWhen = sqlite
274e9 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 57 68  3ExprDup(db, pWh
274ea 65 6e 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  en, EXPRDUP_REDU
274eb 43 45 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  CE);.      }.   
274ec 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 65 6e   }..    /* Re-en
274ed 61 62 6c 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69  able the lookasi
274ee 64 65 20 62 75 66 66 65 72 2c 20 69 66 20 69 74  de buffer, if it
274ef 20 77 61 73 20 64 69 73 61 62 6c 65 64 20 65 61   was disabled ea
274f0 72 6c 69 65 72 2e 20 2a 2f 0a 20 20 20 20 64 62  rlier. */.    db
274f1 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
274f2 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f  bled = enableLoo
274f3 6b 61 73 69 64 65 3b 0a 0a 20 20 20 20 73 71 6c  kaside;..    sql
274f4 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
274f5 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 20 20  b, pWhere);.    
274f6 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
274f7 65 28 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20 20  e(db, pWhen);.  
274f8 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
274f9 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73  tDelete(db, pLis
274fa 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  t);.    sqlite3S
274fb 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
274fc 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 69 66  pSelect);.    if
274fd 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
274fe 65 64 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 66  ed==1 ){.      f
274ff 6b 54 72 69 67 67 65 72 44 65 6c 65 74 65 28 64  kTriggerDelete(d
27500 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20  b, pTrigger);.  
27501 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
27502 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28    }..    switch(
27503 20 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20   action ){.     
27504 20 63 61 73 65 20 4f 45 5f 52 65 73 74 72 69 63   case OE_Restric
27505 74 3a 0a 20 20 20 20 20 20 20 20 70 53 74 65 70  t:.        pStep
27506 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54  ->op = TK_SELECT
27507 3b 20 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ; .        break
27508 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  ;.      case OE_
27509 43 61 73 63 61 64 65 3a 20 0a 20 20 20 20 20 20  Cascade: .      
2750a 20 20 69 66 28 20 21 70 43 68 61 6e 67 65 73 20    if( !pChanges 
2750b 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 70 53  ){ .          pS
2750c 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 44 45 4c  tep->op = TK_DEL
2750d 45 54 45 3b 20 0a 20 20 20 20 20 20 20 20 20 20  ETE; .          
2750e 62 72 65 61 6b 3b 20 0a 20 20 20 20 20 20 20 20  break; .        
2750f 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
27510 0a 20 20 20 20 20 20 20 20 70 53 74 65 70 2d 3e  .        pStep->
27511 6f 70 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b 0a  op = TK_UPDATE;.
27512 20 20 20 20 7d 0a 20 20 20 20 70 53 74 65 70 2d      }.    pStep-
27513 3e 70 54 72 69 67 20 3d 20 70 54 72 69 67 67 65  >pTrig = pTrigge
27514 72 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 2d  r;.    pTrigger-
27515 3e 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d  >pSchema = pTab-
27516 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 54  >pSchema;.    pT
27517 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65  rigger->pTabSche
27518 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
27519 6d 61 3b 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61  ma;.    pFKey->a
2751a 70 54 72 69 67 67 65 72 5b 69 41 63 74 69 6f 6e  pTrigger[iAction
2751b 5d 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 20 20  ] = pTrigger;.  
2751c 20 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d    pTrigger->op =
2751d 20 28 70 43 68 61 6e 67 65 73 20 3f 20 54 4b 5f   (pChanges ? TK_
2751e 55 50 44 41 54 45 20 3a 20 54 4b 5f 44 45 4c 45  UPDATE : TK_DELE
2751f 54 45 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  TE);.  }..  retu
27520 72 6e 20 70 54 72 69 67 67 65 72 3b 0a 7d 0a 0a  rn pTrigger;.}..
27521 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
27522 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ion is called wh
27523 65 6e 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 75  en deleting or u
27524 70 64 61 74 69 6e 67 20 61 20 72 6f 77 20 74 6f  pdating a row to
27525 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 61 6e   implement.** an
27526 79 20 72 65 71 75 69 72 65 64 20 43 41 53 43 41  y required CASCA
27527 44 45 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20  DE, SET NULL or 
27528 53 45 54 20 44 45 46 41 55 4c 54 20 61 63 74 69  SET DEFAULT acti
27529 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
2752a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2752b 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 0a 20 20  te3FkActions(.  
2752c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2752d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2752e 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  /* Parse context
2752f 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
27530 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
27531 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62        /* Table b
27532 65 69 6e 67 20 75 70 64 61 74 65 64 20 6f 72 20  eing updated or 
27533 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 2a 2f 0a  deleted from */.
27534 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61    ExprList *pCha
27535 6e 67 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  nges,           
27536 20 20 2f 2a 20 43 68 61 6e 67 65 2d 6c 69 73 74    /* Change-list
27537 20 66 6f 72 20 55 50 44 41 54 45 2c 20 4e 55 4c   for UPDATE, NUL
27538 4c 20 66 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a  L for DELETE */.
27539 20 20 69 6e 74 20 72 65 67 4f 6c 64 20 20 20 20    int regOld    
2753a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2753b 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
2753c 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67  array containing
2753d 20 6f 6c 64 20 72 6f 77 20 2a 2f 0a 29 7b 0a 20   old row */.){. 
2753e 20 2f 2a 20 49 66 20 66 6f 72 65 69 67 6e 2d 6b   /* If foreign-k
2753f 65 79 20 73 75 70 70 6f 72 74 20 69 73 20 65 6e  ey support is en
27540 61 62 6c 65 64 2c 20 69 74 65 72 61 74 65 20 74  abled, iterate t
27541 68 72 6f 75 67 68 20 61 6c 6c 20 46 4b 73 20 74  hrough all FKs t
27542 68 61 74 20 0a 20 20 2a 2a 20 72 65 66 65 72 20  hat .  ** refer 
27543 74 6f 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49  to table pTab. I
27544 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 63  f there is an ac
27545 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20  tion associated 
27546 77 69 74 68 20 74 68 65 20 46 4b 20 0a 20 20 2a  with the FK .  *
27547 2a 20 66 6f 72 20 74 68 69 73 20 6f 70 65 72 61  * for this opera
27548 74 69 6f 6e 20 28 65 69 74 68 65 72 20 75 70 64  tion (either upd
27549 61 74 65 20 6f 72 20 64 65 6c 65 74 65 29 2c 20  ate or delete), 
2754a 69 6e 76 6f 6b 65 20 74 68 65 20 61 73 73 6f 63  invoke the assoc
2754b 69 61 74 65 64 20 0a 20 20 2a 2a 20 74 72 69 67  iated .  ** trig
2754c 67 65 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e  ger sub-program.
2754d 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73    */.  if( pPars
2754e 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  e->db->flags&SQL
2754f 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20  ITE_ForeignKeys 
27550 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70 46 4b  ){.    FKey *pFK
27551 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
27552 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
27553 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20   variable */.   
27554 20 66 6f 72 28 70 46 4b 65 79 20 3d 20 73 71 6c   for(pFKey = sql
27555 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
27556 28 70 54 61 62 29 3b 20 70 46 4b 65 79 3b 20 70  (pTab); pFKey; p
27557 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78  FKey=pFKey->pNex
27558 74 54 6f 29 7b 0a 20 20 20 20 20 20 54 72 69 67  tTo){.      Trig
27559 67 65 72 20 2a 70 41 63 74 69 6f 6e 20 3d 20 66  ger *pAction = f
2755a 6b 41 63 74 69 6f 6e 54 72 69 67 67 65 72 28 70  kActionTrigger(p
2755b 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 46 4b  Parse, pTab, pFK
2755c 65 79 2c 20 70 43 68 61 6e 67 65 73 29 3b 0a 20  ey, pChanges);. 
2755d 20 20 20 20 20 69 66 28 20 70 41 63 74 69 6f 6e       if( pAction
2755e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2755f 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
27560 72 44 69 72 65 63 74 28 70 50 61 72 73 65 2c 20  rDirect(pParse, 
27561 70 41 63 74 69 6f 6e 2c 20 70 54 61 62 2c 20 72  pAction, pTab, r
27562 65 67 4f 6c 64 2c 20 4f 45 5f 41 62 6f 72 74 2c  egOld, OE_Abort,
27563 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
27564 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66   }.  }.}..#endif
27565 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54   /* ifndef SQLIT
27566 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a  E_OMIT_TRIGGER *
27567 2f 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c  /../*.** Free al
27568 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  l memory associa
27569 74 65 64 20 77 69 74 68 20 66 6f 72 65 69 67 6e  ted with foreign
2756a 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 73   key definitions
2756b 20 61 74 74 61 63 68 65 64 20 74 6f 0a 2a 2a 20   attached to.** 
2756c 74 61 62 6c 65 20 70 54 61 62 2e 20 52 65 6d 6f  table pTab. Remo
2756d 76 65 20 74 68 65 20 64 65 6c 65 74 65 64 20 66  ve the deleted f
2756e 6f 72 65 69 67 6e 20 6b 65 79 73 20 66 72 6f 6d  oreign keys from
2756f 20 74 68 65 20 53 63 68 65 6d 61 2e 66 6b 65 79   the Schema.fkey
27570 48 61 73 68 0a 2a 2a 20 68 61 73 68 20 74 61 62  Hash.** hash tab
27571 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
27572 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
27573 65 33 46 6b 44 65 6c 65 74 65 28 54 61 62 6c 65  e3FkDelete(Table
27574 20 2a 70 54 61 62 29 7b 0a 20 20 46 4b 65 79 20   *pTab){.  FKey 
27575 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20  *pFKey;         
27576 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
27577 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
27578 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74  */.  FKey *pNext
27579 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2757a 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
2757b 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d  pFKey->pNextFrom
2757c 20 2a 2f 0a 0a 20 20 66 6f 72 28 70 46 4b 65 79   */..  for(pFKey
2757d 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 46  =pTab->pFKey; pF
2757e 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74  Key; pFKey=pNext
2757f 29 7b 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76  ){..    /* Remov
27580 65 20 74 68 65 20 46 4b 20 66 72 6f 6d 20 74 68  e the FK from th
27581 65 20 66 6b 65 79 48 61 73 68 20 68 61 73 68 20  e fkeyHash hash 
27582 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66  table. */.    if
27583 28 20 70 46 4b 65 79 2d 3e 70 50 72 65 76 54 6f  ( pFKey->pPrevTo
27584 20 29 7b 0a 20 20 20 20 20 20 70 46 4b 65 79 2d   ){.      pFKey-
27585 3e 70 50 72 65 76 54 6f 2d 3e 70 4e 65 78 74 54  >pPrevTo->pNextT
27586 6f 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74  o = pFKey->pNext
27587 54 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  To;.    }else{. 
27588 20 20 20 20 20 76 6f 69 64 20 2a 64 61 74 61 20       void *data 
27589 3d 20 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 2d  = (void *)pFKey-
2758a 3e 70 4e 65 78 74 54 6f 3b 0a 20 20 20 20 20 20  >pNextTo;.      
2758b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
2758c 28 64 61 74 61 20 3f 20 70 46 4b 65 79 2d 3e 70  (data ? pFKey->p
2758d 4e 65 78 74 54 6f 2d 3e 7a 54 6f 20 3a 20 70 46  NextTo->zTo : pF
2758e 4b 65 79 2d 3e 7a 54 6f 29 3b 0a 20 20 20 20 20  Key->zTo);.     
2758f 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
27590 72 74 28 26 70 54 61 62 2d 3e 70 53 63 68 65 6d  rt(&pTab->pSchem
27591 61 2d 3e 66 6b 65 79 48 61 73 68 2c 20 7a 2c 20  a->fkeyHash, z, 
27592 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
27593 7a 29 2c 20 64 61 74 61 29 3b 0a 20 20 20 20 7d  z), data);.    }
27594 0a 20 20 20 20 69 66 28 20 70 46 4b 65 79 2d 3e  .    if( pFKey->
27595 70 4e 65 78 74 54 6f 20 29 7b 0a 20 20 20 20 20  pNextTo ){.     
27596 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 2d   pFKey->pNextTo-
27597 3e 70 50 72 65 76 54 6f 20 3d 20 70 46 4b 65 79  >pPrevTo = pFKey
27598 2d 3e 70 50 72 65 76 54 6f 3b 0a 20 20 20 20 7d  ->pPrevTo;.    }
27599 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20  ..    /* Delete 
2759a 61 6e 79 20 74 72 69 67 67 65 72 73 20 63 72 65  any triggers cre
2759b 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ated to implemen
2759c 74 20 61 63 74 69 6f 6e 73 20 66 6f 72 20 74 68  t actions for th
2759d 69 73 20 46 4b 2e 20 2a 2f 0a 23 69 66 6e 64 65  is FK. */.#ifnde
2759e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2759f 49 47 47 45 52 0a 20 20 20 20 66 6b 54 72 69 67  IGGER.    fkTrig
275a0 67 65 72 44 65 6c 65 74 65 28 70 54 61 62 2d 3e  gerDelete(pTab->
275a1 64 62 4d 65 6d 2c 20 70 46 4b 65 79 2d 3e 61 70  dbMem, pFKey->ap
275a2 54 72 69 67 67 65 72 5b 30 5d 29 3b 0a 20 20 20  Trigger[0]);.   
275a3 20 66 6b 54 72 69 67 67 65 72 44 65 6c 65 74 65   fkTriggerDelete
275a4 28 70 54 61 62 2d 3e 64 62 4d 65 6d 2c 20 70 46  (pTab->dbMem, pF
275a5 4b 65 79 2d 3e 61 70 54 72 69 67 67 65 72 5b 31  Key->apTrigger[1
275a6 5d 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  ]);.#endif..    
275a7 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32  /* EV: R-30323-2
275a8 31 39 31 37 20 45 61 63 68 20 66 6f 72 65 69 67  1917 Each foreig
275a9 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
275aa 20 69 6e 20 53 51 4c 69 74 65 20 69 73 0a 20 20   in SQLite is.  
275ab 20 20 2a 2a 20 63 6c 61 73 73 69 66 69 65 64 20    ** classified 
275ac 61 73 20 65 69 74 68 65 72 20 69 6d 6d 65 64 69  as either immedi
275ad 61 74 65 20 6f 72 20 64 65 66 65 72 72 65 64 2e  ate or deferred.
275ae 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
275af 72 74 28 20 70 46 4b 65 79 2d 3e 69 73 44 65 66  rt( pFKey->isDef
275b0 65 72 72 65 64 3d 3d 30 20 7c 7c 20 70 46 4b 65  erred==0 || pFKe
275b1 79 2d 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 31  y->isDeferred==1
275b2 20 29 3b 0a 0a 20 20 20 20 70 4e 65 78 74 20 3d   );..    pNext =
275b3 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f   pFKey->pNextFro
275b4 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  m;.    sqlite3Db
275b5 46 72 65 65 28 70 54 61 62 2d 3e 64 62 4d 65 6d  Free(pTab->dbMem
275b6 2c 20 70 46 4b 65 79 29 3b 0a 20 20 7d 0a 7d 0a  , pFKey);.  }.}.
275b7 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 66  #endif /* ifndef
275b8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
275b9 45 49 47 4e 5f 4b 45 59 20 2a 2f 0a 0a 2f 2a 2a  EIGN_KEY */../**
275ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
275bb 20 6f 66 20 66 6b 65 79 2e 63 20 2a 2a 2a 2a 2a   of fkey.c *****
275bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
275bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
275be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
275bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
275c0 69 6e 20 66 69 6c 65 20 69 6e 73 65 72 74 2e 63  in file insert.c
275c1 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
275c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
275c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
275c4 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
275c5 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
275c6 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
275c7 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
275c8 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
275c9 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
275ca 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
275cb 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
275cc 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
275cd 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
275ce 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
275cf 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
275d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
275d1 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
275d2 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
275d3 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
275d4 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
275d5 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
275d6 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
275d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
275d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
275d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
275da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
275db 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
275dc 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
275dd 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61   routines that a
275de 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  re called by the
275df 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61   parser.** to ha
275e0 6e 64 6c 65 20 49 4e 53 45 52 54 20 73 74 61 74  ndle INSERT stat
275e1 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65  ements in SQLite
275e2 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  ..*/../*.** Gene
275e3 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
275e4 69 6c 6c 20 6f 70 65 6e 20 61 20 74 61 62 6c 65  ill open a table
275e5 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2f   for reading..*/
275e6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
275e7 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e  void sqlite3Open
275e8 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
275e9 70 2c 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65  p,       /* Gene
275ea 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74  rate code into t
275eb 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e  his VDBE */.  in
275ec 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 2f 2a  t iCur,       /*
275ed 20 54 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   The cursor numb
275ee 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  er of the table 
275ef 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
275f0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
275f1 62 61 73 65 20 69 6e 64 65 78 20 69 6e 20 73 71  base index in sq
275f2 6c 69 74 65 33 2e 61 44 62 5b 5d 20 2a 2f 0a 20  lite3.aDb[] */. 
275f3 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
275f4 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f   /* The table to
275f5 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
275f6 69 6e 74 20 6f 70 63 6f 64 65 20 20 20 20 20 20  int opcode      
275f7 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f  /* OP_OpenRead o
275f8 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a  r OP_OpenWrite *
275f9 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  /.){.  Vdbe *v;.
275fa 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
275fb 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 3b 0a  pTab) ) return;.
275fc 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
275fd 56 64 62 65 28 70 29 3b 0a 20 20 61 73 73 65 72  Vdbe(p);.  asser
275fe 74 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  t( opcode==OP_Op
275ff 65 6e 57 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64  enWrite || opcod
27600 65 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29  e==OP_OpenRead )
27601 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ;.  sqlite3Table
27602 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 70 54 61  Lock(p, iDb, pTa
27603 62 2d 3e 74 6e 75 6d 2c 20 28 6f 70 63 6f 64 65  b->tnum, (opcode
27604 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3f  ==OP_OpenWrite)?
27605 31 3a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  1:0, pTab->zName
27606 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
27607 41 64 64 4f 70 33 28 76 2c 20 6f 70 63 6f 64 65  AddOp3(v, opcode
27608 2c 20 69 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e  , iCur, pTab->tn
27609 75 6d 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69  um, iDb);.  sqli
2760a 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
2760b 76 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 49 4e  v, -1, SQLITE_IN
2760c 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e  T_TO_PTR(pTab->n
2760d 43 6f 6c 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b  Col), P4_INT32);
2760e 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28  .  VdbeComment((
2760f 76 2c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a  v, "%s", pTab->z
27610 4e 61 6d 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Name));.}../*.**
27611 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
27612 72 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20  r to the column 
27613 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
27614 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
27615 69 6e 64 65 78 0a 2a 2a 20 70 49 64 78 2e 20 41  index.** pIdx. A
27616 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
27617 20 73 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20   string has one 
27618 63 68 61 72 61 63 74 65 72 20 66 6f 72 20 65 61  character for ea
27619 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 0a 2a 2a  ch column in .**
2761a 20 74 68 65 20 74 61 62 6c 65 2c 20 61 63 63 6f   the table, acco
2761b 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61 66 66  rding to the aff
2761c 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c  inity of the col
2761d 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20 43 68 61 72  umn:.**.**  Char
2761e 61 63 74 65 72 20 20 20 20 20 20 43 6f 6c 75 6d  acter      Colum
2761f 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 20 2d  n affinity.**  -
27620 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27621 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
27622 20 20 27 61 27 20 20 20 20 20 20 20 20 20 20 20    'a'           
27623 20 54 45 58 54 0a 2a 2a 20 20 27 62 27 20 20 20   TEXT.**  'b'   
27624 20 20 20 20 20 20 20 20 20 4e 4f 4e 45 0a 2a 2a           NONE.**
27625 20 20 27 63 27 20 20 20 20 20 20 20 20 20 20 20    'c'           
27626 20 4e 55 4d 45 52 49 43 0a 2a 2a 20 20 27 64 27   NUMERIC.**  'd'
27627 20 20 20 20 20 20 20 20 20 20 20 20 49 4e 54 45              INTE
27628 47 45 52 0a 2a 2a 20 20 27 65 27 20 20 20 20 20  GER.**  'e'     
27629 20 20 20 20 20 20 20 52 45 41 4c 0a 2a 2a 0a 2a         REAL.**.*
2762a 2a 20 41 6e 20 65 78 74 72 61 20 27 62 27 20 69  * An extra 'b' i
2762b 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  s appended to th
2762c 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 74 72  e end of the str
2762d 69 6e 67 20 74 6f 20 63 6f 76 65 72 20 74 68 65  ing to cover the
2762e 0a 2a 2a 20 72 6f 77 69 64 20 74 68 61 74 20 61  .** rowid that a
2762f 70 70 65 61 72 73 20 61 73 20 74 68 65 20 6c 61  ppears as the la
27630 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65  st column in eve
27631 72 79 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  ry index..**.** 
27632 4d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 62  Memory for the b
27633 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
27634 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65   the column inde
27635 78 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  x affinity strin
27636 67 0a 2a 2a 20 69 73 20 6d 61 6e 61 67 65 64 20  g.** is managed 
27637 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72  along with the r
27638 65 73 74 20 6f 66 20 74 68 65 20 49 6e 64 65 78  est of the Index
27639 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 77   structure. It w
2763a 69 6c 6c 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73  ill be.** releas
2763b 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65 33 44  ed when sqlite3D
2763c 65 6c 65 74 65 49 6e 64 65 78 28 29 20 69 73 20  eleteIndex() is 
2763d 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  called..*/.SQLIT
2763e 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
2763f 63 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64  char *sqlite3Ind
27640 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64  exAffinityStr(Vd
27641 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70 49  be *v, Index *pI
27642 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64 78  dx){.  if( !pIdx
27643 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20  ->zColAff ){.   
27644 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 69   /* The first ti
27645 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69  me a column affi
27646 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20  nity string for 
27647 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64  a particular ind
27648 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 71  ex is.    ** req
27649 75 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c  uired, it is all
2764a 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c  ocated and popul
2764b 61 74 65 64 20 68 65 72 65 2e 20 49 74 20 69 73  ated here. It is
2764c 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73 0a   then stored as.
2764d 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72 20      ** a member 
2764e 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  of the Index str
2764f 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73 65  ucture for subse
27650 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20 2a  quent use..    *
27651 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f 6c  *.    ** The col
27652 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
27653 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61  ing will eventua
27654 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 62  lly be deleted b
27655 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 44  y.    ** sqliteD
27656 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68 65  eleteIndex() whe
27657 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75  n the Index stru
27658 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20  cture itself is 
27659 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20 75  cleaned.    ** u
2765a 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  p..    */.    in
2765b 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  t n;.    Table *
2765c 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61  pTab = pIdx->pTa
2765d 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ble;.    sqlite3
2765e 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64   *db = sqlite3Vd
2765f 62 65 44 62 28 76 29 3b 0a 20 20 20 20 70 49 64  beDb(v);.    pId
27660 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68  x->zColAff = (ch
27661 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  ar *)sqlite3Mall
27662 6f 63 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  oc(pIdx->nColumn
27663 2b 32 29 3b 0a 20 20 20 20 69 66 28 20 21 70 49  +2);.    if( !pI
27664 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20  dx->zColAff ){. 
27665 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
27666 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
27667 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
27668 0a 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c  .    for(n=0; n<
27669 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e  pIdx->nColumn; n
2766a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d  ++){.      pIdx-
2766b 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54  >zColAff[n] = pT
2766c 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61  ab->aCol[pIdx->a
2766d 69 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69  iColumn[n]].affi
2766e 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nity;.    }.    
2766f 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b  pIdx->zColAff[n+
27670 2b 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  +] = SQLITE_AFF_
27671 4e 4f 4e 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e  NONE;.    pIdx->
27672 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a  zColAff[n] = 0;.
27673 20 20 7d 0a 20 0a 20 20 72 65 74 75 72 6e 20 70    }. .  return p
27674 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 3b 0a 7d 0a  Idx->zColAff;.}.
27675 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50 34 20 6f 66  ./*.** Set P4 of
27676 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
27677 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f  ly inserted opco
27678 64 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 61  de to a column a
27679 66 66 69 6e 69 74 79 0a 2a 2a 20 73 74 72 69 6e  ffinity.** strin
2767a 67 20 66 6f 72 20 74 61 62 6c 65 20 70 54 61 62  g for table pTab
2767b 2e 20 41 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  . A column affin
2767c 69 74 79 20 73 74 72 69 6e 67 20 68 61 73 20 6f  ity string has o
2767d 6e 65 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20  ne character.** 
2767e 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
2767f 69 6e 64 65 78 65 64 20 62 79 20 74 68 65 20 69  indexed by the i
27680 6e 64 65 78 2c 20 61 63 63 6f 72 64 69 6e 67 20  ndex, according 
27681 74 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  to the affinity 
27682 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e  of the.** column
27683 3a 0a 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74  :.**.**  Charact
27684 65 72 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61  er      Column a
27685 66 66 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d  ffinity.**  ----
27686 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27687 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27  ----------.**  '
27688 61 27 20 20 20 20 20 20 20 20 20 20 20 20 54 45  a'            TE
27689 58 54 0a 2a 2a 20 20 27 62 27 20 20 20 20 20 20  XT.**  'b'      
2768a 20 20 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27        NONE.**  '
2768b 63 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 55  c'            NU
2768c 4d 45 52 49 43 0a 2a 2a 20 20 27 64 27 20 20 20  MERIC.**  'd'   
2768d 20 20 20 20 20 20 20 20 20 49 4e 54 45 47 45 52           INTEGER
2768e 0a 2a 2a 20 20 27 65 27 20 20 20 20 20 20 20 20  .**  'e'        
2768f 20 20 20 20 52 45 41 4c 0a 2a 2f 0a 53 51 4c 49      REAL.*/.SQLI
27690 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
27691 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69  sqlite3TableAffi
27692 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 76 2c  nityStr(Vdbe *v,
27693 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
27694 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 69   /* The first ti
27695 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69  me a column affi
27696 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20  nity string for 
27697 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62  a particular tab
27698 6c 65 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 69  le.  ** is requi
27699 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63  red, it is alloc
2769a 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74  ated and populat
2769b 65 64 20 68 65 72 65 2e 20 49 74 20 69 73 20 74  ed here. It is t
2769c 68 65 6e 20 0a 20 20 2a 2a 20 73 74 6f 72 65 64  hen .  ** stored
2769d 20 61 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 20   as a member of 
2769e 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
2769f 75 72 65 20 66 6f 72 20 73 75 62 73 65 71 75 65  ure for subseque
276a0 6e 74 20 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a  nt use..  **.  *
276a1 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66  * The column aff
276a2 69 6e 69 74 79 20 73 74 72 69 6e 67 20 77 69 6c  inity string wil
276a3 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20  l eventually be 
276a4 64 65 6c 65 74 65 64 20 62 79 0a 20 20 2a 2a 20  deleted by.  ** 
276a5 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
276a6 6c 65 28 29 20 77 68 65 6e 20 74 68 65 20 54 61  le() when the Ta
276a7 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 74  ble structure it
276a8 73 65 6c 66 20 69 73 20 63 6c 65 61 6e 65 64 20  self is cleaned 
276a9 75 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  up..  */.  if( !
276aa 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b  pTab->zColAff ){
276ab 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41  .    char *zColA
276ac 66 66 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  ff;.    int i;. 
276ad 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
276ae 20 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76   sqlite3VdbeDb(v
276af 29 3b 0a 0a 20 20 20 20 7a 43 6f 6c 41 66 66 20  );..    zColAff 
276b0 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
276b1 33 4d 61 6c 6c 6f 63 28 70 54 61 62 2d 3e 6e 43  3Malloc(pTab->nC
276b2 6f 6c 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 21  ol+1);.    if( !
276b3 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20  zColAff ){.     
276b4 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
276b5 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74  d = 1;.      ret
276b6 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  urn;.    }..    
276b7 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
276b8 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
276b9 20 20 20 7a 43 6f 6c 41 66 66 5b 69 5d 20 3d 20     zColAff[i] = 
276ba 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66  pTab->aCol[i].af
276bb 66 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20  finity;.    }.  
276bc 20 20 7a 43 6f 6c 41 66 66 5b 70 54 61 62 2d 3e    zColAff[pTab->
276bd 6e 43 6f 6c 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20  nCol] = '\0';.. 
276be 20 20 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66     pTab->zColAff
276bf 20 3d 20 7a 43 6f 6c 41 66 66 3b 0a 20 20 7d 0a   = zColAff;.  }.
276c0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
276c1 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54  angeP4(v, -1, pT
276c2 61 62 2d 3e 7a 43 6f 6c 41 66 66 2c 20 30 29 3b  ab->zColAff, 0);
276c3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
276c4 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65   non-zero if the
276c5 20 74 61 62 6c 65 20 70 54 61 62 20 69 6e 20 64   table pTab in d
276c6 61 74 61 62 61 73 65 20 69 44 62 20 6f 72 20 61  atabase iDb or a
276c7 6e 79 20 6f 66 20 69 74 73 20 69 6e 64 69 63 65  ny of its indice
276c8 73 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f  s.** have been o
276c9 70 65 6e 65 64 20 61 74 20 61 6e 79 20 70 6f 69  pened at any poi
276ca 6e 74 20 69 6e 20 74 68 65 20 56 44 42 45 20 70  nt in the VDBE p
276cb 72 6f 67 72 61 6d 20 62 65 67 69 6e 6e 69 6e 67  rogram beginning
276cc 20 61 74 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20   at location.** 
276cd 69 53 74 61 72 74 41 64 64 72 20 74 68 72 6f 75  iStartAddr throu
276ce 67 68 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ght the end of t
276cf 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 69  he program.  Thi
276d0 73 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 65  s is used to see
276d1 20 69 66 20 0a 2a 2a 20 61 20 73 74 61 74 65 6d   if .** a statem
276d2 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ent of the form 
276d3 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 69   "INSERT INTO <i
276d4 44 62 2c 20 70 54 61 62 3e 20 53 45 4c 45 43 54  Db, pTab> SELECT
276d5 20 2e 2e 2e 22 20 63 61 6e 20 0a 2a 2a 20 72 75   ..." can .** ru
276d6 6e 20 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20  n without using 
276d7 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
276d8 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 73 20  for the results 
276d9 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20 0a  of the SELECT. .
276da 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
276db 61 64 73 54 61 62 6c 65 28 50 61 72 73 65 20 2a  adsTable(Parse *
276dc 70 2c 20 69 6e 74 20 69 53 74 61 72 74 41 64 64  p, int iStartAdd
276dd 72 2c 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c  r, int iDb, Tabl
276de 65 20 2a 70 54 61 62 29 7b 0a 20 20 56 64 62 65  e *pTab){.  Vdbe
276df 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
276e0 56 64 62 65 28 70 29 3b 0a 20 20 69 6e 74 20 69  Vdbe(p);.  int i
276e1 3b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 73  ;.  int iEnd = s
276e2 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
276e3 74 41 64 64 72 28 76 29 3b 0a 23 69 66 6e 64 65  tAddr(v);.#ifnde
276e4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
276e5 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61  RTUALTABLE.  VTa
276e6 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 49 73 56  ble *pVTab = IsV
276e7 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 73  irtual(pTab) ? s
276e8 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
276e9 70 2d 3e 64 62 2c 20 70 54 61 62 29 20 3a 20 30  p->db, pTab) : 0
276ea 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 66 6f 72 28  ;.#endif..  for(
276eb 69 3d 69 53 74 61 72 74 41 64 64 72 3b 20 69 3c  i=iStartAddr; i<
276ec 69 45 6e 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  iEnd; i++){.    
276ed 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71  VdbeOp *pOp = sq
276ee 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
276ef 2c 20 69 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , i);.    assert
276f0 28 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 20 20  ( pOp!=0 );.    
276f1 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
276f2 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 26 26 20  =OP_OpenRead && 
276f3 70 4f 70 2d 3e 70 33 3d 3d 69 44 62 20 29 7b 0a  pOp->p3==iDb ){.
276f4 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e        Index *pIn
276f5 64 65 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 74  dex;.      int t
276f6 6e 75 6d 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  num = pOp->p2;. 
276f7 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70       if( tnum==p
276f8 54 61 62 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20 20  Tab->tnum ){.   
276f9 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
276fa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
276fb 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49  (pIndex=pTab->pI
276fc 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49  ndex; pIndex; pI
276fd 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65  ndex=pIndex->pNe
276fe 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  xt){.        if(
276ff 20 74 6e 75 6d 3d 3d 70 49 6e 64 65 78 2d 3e 74   tnum==pIndex->t
27700 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  num ){.         
27701 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
27702 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
27703 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
27704 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
27705 42 4c 45 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  BLE.    if( pOp-
27706 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65  >opcode==OP_VOpe
27707 6e 20 26 26 20 70 4f 70 2d 3e 70 34 2e 70 56 74  n && pOp->p4.pVt
27708 61 62 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20 20  ab==pVTab ){.   
27709 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
2770a 70 34 2e 70 56 74 61 62 21 3d 30 20 29 3b 0a 20  p4.pVtab!=0 );. 
2770b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
2770c 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41  ->p4type==P4_VTA
2770d 42 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  B );.      retur
2770e 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  n 1;.    }.#endi
2770f 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  f.  }.  return 0
27710 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
27711 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
27712 52 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 4c 6f 63  REMENT./*.** Loc
27713 61 74 65 20 6f 72 20 63 72 65 61 74 65 20 61 6e  ate or create an
27714 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72   AutoincInfo str
27715 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65  ucture associate
27716 64 20 77 69 74 68 20 74 61 62 6c 65 20 70 54 61  d with table pTa
27717 62 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 69 6e  b.** which is in
27718 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 20   database iDb.  
27719 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73  Return the regis
2771a 74 65 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ter number for t
2771b 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74  he register.** t
2771c 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6d 61  hat holds the ma
2771d 78 69 6d 75 6d 20 72 6f 77 69 64 2e 0a 2a 2a 0a  ximum rowid..**.
2771e 2a 2a 20 54 68 65 72 65 20 69 73 20 61 74 20 6d  ** There is at m
2771f 6f 73 74 20 6f 6e 65 20 41 75 74 6f 69 6e 63 49  ost one AutoincI
27720 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 65  nfo structure pe
27721 72 20 74 61 62 6c 65 20 65 76 65 6e 20 69 66 20  r table even if 
27722 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 61 62 6c  the.** same tabl
27723 65 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65  e is autoincreme
27724 6e 74 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  nted multiple ti
27725 6d 65 73 20 64 75 65 20 74 6f 20 69 6e 73 65 72  mes due to inser
27726 74 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 72 69  ts within.** tri
27727 67 67 65 72 73 2e 20 20 41 20 6e 65 77 20 41 75  ggers.  A new Au
27728 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63 74  toincInfo struct
27729 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 69  ure is created i
2772a 66 20 74 68 69 73 20 69 73 20 74 68 65 0a 2a 2a  f this is the.**
2772b 20 66 69 72 73 74 20 75 73 65 20 6f 66 20 74 61   first use of ta
2772c 62 6c 65 20 70 54 61 62 2e 20 20 4f 6e 20 32 6e  ble pTab.  On 2n
2772d 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
2772e 20 75 73 65 73 2c 20 74 68 65 20 6f 72 69 67 69   uses, the origi
2772f 6e 61 6c 0a 2a 2a 20 41 75 74 6f 69 6e 63 49 6e  nal.** AutoincIn
27730 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
27731 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 65  used..**.** Thre
27732 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
27733 6e 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  ns are allocated
27734 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 52  :.**.**   (1)  R
27735 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20  egister to hold 
27736 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
27737 70 54 61 62 20 74 61 62 6c 65 2e 0a 2a 2a 20 20  pTab table..**  
27738 20 28 32 29 20 20 52 65 67 69 73 74 65 72 20 74   (2)  Register t
27739 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 78 69 6d  o hold the maxim
2773a 75 6d 20 52 4f 57 49 44 20 6f 66 20 70 54 61 62  um ROWID of pTab
2773b 2e 0a 2a 2a 20 20 20 28 33 29 20 20 52 65 67 69  ..**   (3)  Regi
2773c 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ster to hold the
2773d 20 72 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65   rowid in sqlite
2773e 5f 73 65 71 75 65 6e 63 65 20 6f 66 20 70 54 61  _sequence of pTa
2773f 62 0a 2a 2a 0a 2a 2a 20 54 68 65 20 32 6e 64 20  b.**.** The 2nd 
27740 72 65 67 69 73 74 65 72 20 69 73 20 74 68 65 20  register is the 
27741 6f 6e 65 20 74 68 61 74 20 69 73 20 72 65 74 75  one that is retu
27742 72 6e 65 64 2e 20 20 54 68 61 74 20 69 73 20 61  rned.  That is a
27743 6c 6c 20 74 68 65 0a 2a 2a 20 69 6e 73 65 72 74  ll the.** insert
27744 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74   routine needs t
27745 6f 20 6b 6e 6f 77 20 61 62 6f 75 74 2e 0a 2a 2f  o know about..*/
27746 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f  .static int auto
27747 49 6e 63 42 65 67 69 6e 28 0a 20 20 50 61 72 73  IncBegin(.  Pars
27748 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
27749 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2774a 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
2774b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2774c 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
2774d 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
2774e 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
2774f 61 62 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ab         /* Th
27750 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 77  e table we are w
27751 72 69 74 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a  riting to */.){.
27752 20 20 69 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b    int memId = 0;
27753 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
27754 72 20 68 6f 6c 64 69 6e 67 20 6d 61 78 69 6d 75  r holding maximu
27755 6d 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28  m rowid */.  if(
27756 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20   pTab->tabFlags 
27757 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65  & TF_Autoincreme
27758 6e 74 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20  nt ){.    Parse 
27759 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c  *pToplevel = sql
2775a 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
2775b 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 41  l(pParse);.    A
2775c 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 49 6e 66  utoincInfo *pInf
2775d 6f 3b 0a 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20  o;..    pInfo = 
2775e 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63  pToplevel->pAinc
2775f 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 49 6e  ;.    while( pIn
27760 66 6f 20 26 26 20 70 49 6e 66 6f 2d 3e 70 54 61  fo && pInfo->pTa
27761 62 21 3d 70 54 61 62 20 29 7b 20 70 49 6e 66 6f  b!=pTab ){ pInfo
27762 20 3d 20 70 49 6e 66 6f 2d 3e 70 4e 65 78 74 3b   = pInfo->pNext;
27763 20 7d 0a 20 20 20 20 69 66 28 20 70 49 6e 66 6f   }.    if( pInfo
27764 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  ==0 ){.      pIn
27765 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
27766 6c 6c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e  llocRaw(pParse->
27767 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66  db, sizeof(*pInf
27768 6f 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  o));.      if( p
27769 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e  Info==0 ) return
2776a 20 30 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d   0;.      pInfo-
2776b 3e 70 4e 65 78 74 20 3d 20 70 54 6f 70 6c 65 76  >pNext = pToplev
2776c 65 6c 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 20  el->pAinc;.     
2776d 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e   pToplevel->pAin
2776e 63 20 3d 20 70 49 6e 66 6f 3b 0a 20 20 20 20 20  c = pInfo;.     
2776f 20 70 49 6e 66 6f 2d 3e 70 54 61 62 20 3d 20 70   pInfo->pTab = p
27770 54 61 62 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f  Tab;.      pInfo
27771 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20  ->iDb = iDb;.   
27772 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d     pToplevel->nM
27773 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20  em++;           
27774 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
27775 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20  er to hold name 
27776 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  of table */.    
27777 20 20 70 49 6e 66 6f 2d 3e 72 65 67 43 74 72 20    pInfo->regCtr 
27778 3d 20 2b 2b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e  = ++pToplevel->n
27779 4d 65 6d 3b 20 20 2f 2a 20 4d 61 78 20 72 6f 77  Mem;  /* Max row
2777a 69 64 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  id register */. 
2777b 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e       pToplevel->
2777c 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20  nMem++;         
2777d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69           /* Rowi
2777e 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75  d in sqlite_sequ
2777f 65 6e 63 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  ence */.    }.  
27780 20 20 6d 65 6d 49 64 20 3d 20 70 49 6e 66 6f 2d    memId = pInfo-
27781 3e 72 65 67 43 74 72 3b 0a 20 20 7d 0a 20 20 72  >regCtr;.  }.  r
27782 65 74 75 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a  eturn memId;.}..
27783 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
27784 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64  ne generates cod
27785 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 69 74  e that will init
27786 69 61 6c 69 7a 65 20 61 6c 6c 20 6f 66 20 74 68  ialize all of th
27787 65 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 75 73  e.** register us
27788 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 69 6e  ed by the autoin
27789 63 72 65 6d 65 6e 74 20 74 72 61 63 6b 65 72 2e  crement tracker.
2778a 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49    .*/.SQLITE_PRI
2778b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2778c 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65  3AutoincrementBe
2778d 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  gin(Parse *pPars
2778e 65 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66  e){.  AutoincInf
2778f 6f 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  o *p;           
27790 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
27791 61 62 6f 75 74 20 61 6e 20 41 55 54 4f 49 4e 43  about an AUTOINC
27792 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 73 71 6c 69  REMENT */.  sqli
27793 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
27794 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61  ->db;  /* The da
27795 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
27796 6e 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20  n */.  Db *pDb; 
27797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27798 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 6e    /* Database on
27799 6c 79 20 61 75 74 6f 69 6e 63 20 74 61 62 6c 65  ly autoinc table
2779a 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 49 64 3b   */.  int memId;
2779b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2779c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
2779d 64 69 6e 67 20 6d 61 78 20 72 6f 77 69 64 20 2a  ding max rowid *
2779e 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20  /.  int addr;   
2779f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
277a0 2a 20 41 20 56 44 42 45 20 61 64 64 72 65 73 73  * A VDBE address
277a1 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20   */.  Vdbe *v = 
277a2 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
277a3 20 2f 2a 20 56 44 42 45 20 75 6e 64 65 72 20 63   /* VDBE under c
277a4 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a  onstruction */..
277a5 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
277a6 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
277a7 64 20 64 75 72 69 6e 67 20 74 72 69 67 67 65 72  d during trigger
277a8 2d 67 65 6e 65 72 61 74 69 6f 6e 2e 20 20 49 74  -generation.  It
277a9 20 69 73 0a 20 20 2a 2a 20 6f 6e 6c 79 20 63 61   is.  ** only ca
277aa 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f  lled from the to
277ab 70 2d 6c 65 76 65 6c 20 2a 2f 0a 20 20 61 73 73  p-level */.  ass
277ac 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 54 72  ert( pParse->pTr
277ad 69 67 67 65 72 54 61 62 3d 3d 30 20 29 3b 0a 20  iggerTab==0 );. 
277ae 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 3d   assert( pParse=
277af 3d 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70  =sqlite3ParseTop
277b0 6c 65 76 65 6c 28 70 50 61 72 73 65 29 20 29 3b  level(pParse) );
277b1 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b  ..  assert( v );
277b2 20 20 20 2f 2a 20 57 65 20 66 61 69 6c 65 64 20     /* We failed 
277b3 6c 6f 6e 67 20 61 67 6f 20 69 66 20 74 68 69 73  long ago if this
277b4 20 69 73 20 6e 6f 74 20 73 6f 20 2a 2f 0a 20 20   is not so */.  
277b5 66 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e  for(p = pParse->
277b6 70 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d  pAinc; p; p = p-
277b7 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 44 62  >pNext){.    pDb
277b8 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69   = &db->aDb[p->i
277b9 44 62 5d 3b 0a 20 20 20 20 6d 65 6d 49 64 20 3d  Db];.    memId =
277ba 20 70 2d 3e 72 65 67 43 74 72 3b 0a 20 20 20 20   p->regCtr;.    
277bb 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
277bc 28 70 50 61 72 73 65 2c 20 30 2c 20 70 2d 3e 69  (pParse, 0, p->i
277bd 44 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  Db, pDb->pSchema
277be 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70  ->pSeqTab, OP_Op
277bf 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 61 64 64  enRead);.    add
277c0 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  r = sqlite3VdbeC
277c1 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
277c2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
277c3 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
277c4 67 38 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20  g8, 0, memId-1, 
277c5 30 2c 20 70 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  0, p->pTab->zNam
277c6 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  e, 0);.    sqlit
277c7 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
277c8 4f 50 5f 52 65 77 69 6e 64 2c 20 30 2c 20 61 64  OP_Rewind, 0, ad
277c9 64 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr+9);.    sqlit
277ca 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
277cb 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c  OP_Column, 0, 0,
277cc 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c   memId);.    sql
277cd 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
277ce 2c 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31  , OP_Ne, memId-1
277cf 2c 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64 29  , addr+7, memId)
277d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
277d1 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c  eChangeP5(v, SQL
277d2 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
277d3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
277d4 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
277d5 69 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29 3b  id, 0, memId+1);
277d6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
277d7 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
277d8 75 6d 6e 2c 20 30 2c 20 31 2c 20 6d 65 6d 49 64  umn, 0, 1, memId
277d9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
277da 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
277db 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2b 39 29 3b  oto, 0, addr+9);
277dc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
277dd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
277de 74 2c 20 30 2c 20 61 64 64 72 2b 32 29 3b 0a 20  t, 0, addr+2);. 
277df 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
277e0 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
277e1 65 72 2c 20 30 2c 20 6d 65 6d 49 64 29 3b 0a 20  er, 0, memId);. 
277e2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
277e3 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp0(v, OP_Close
277e4 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
277e5 55 70 64 61 74 65 20 74 68 65 20 6d 61 78 69 6d  Update the maxim
277e6 75 6d 20 72 6f 77 69 64 20 66 6f 72 20 61 6e 20  um rowid for an 
277e7 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61  autoincrement ca
277e8 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  lculation..**.**
277e9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
277ea 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77  ould be called w
277eb 68 65 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74  hen the top of t
277ec 68 65 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61  he stack holds a
277ed 0a 2a 2a 20 6e 65 77 20 72 6f 77 69 64 20 74 68  .** new rowid th
277ee 61 74 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62  at is about to b
277ef 65 20 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20  e inserted.  If 
277f0 74 68 61 74 20 6e 65 77 20 72 6f 77 69 64 20 69  that new rowid i
277f1 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e  s.** larger than
277f2 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77   the maximum row
277f3 69 64 20 69 6e 20 74 68 65 20 6d 65 6d 49 64 20  id in the memId 
277f4 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65  memory cell, the
277f5 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  n the.** memory 
277f6 63 65 6c 6c 20 69 73 20 75 70 64 61 74 65 64 2e  cell is updated.
277f7 20 20 54 68 65 20 73 74 61 63 6b 20 69 73 20 75    The stack is u
277f8 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61  nchanged..*/.sta
277f9 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63  tic void autoInc
277fa 53 74 65 70 28 50 61 72 73 65 20 2a 70 50 61 72  Step(Parse *pPar
277fb 73 65 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69  se, int memId, i
277fc 6e 74 20 72 65 67 52 6f 77 69 64 29 7b 0a 20 20  nt regRowid){.  
277fd 69 66 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20  if( memId>0 ){. 
277fe 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
277ff 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64  dOp2(pParse->pVd
27800 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d  be, OP_MemMax, m
27801 65 6d 49 64 2c 20 72 65 67 52 6f 77 69 64 29 3b  emId, regRowid);
27802 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
27803 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72  is routine gener
27804 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 6e 65  ates the code ne
27805 65 64 65 64 20 74 6f 20 77 72 69 74 65 20 61 75  eded to write au
27806 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 6d  toincrement.** m
27807 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 76 61 6c  aximum rowid val
27808 75 65 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  ues back into th
27809 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
2780a 65 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 20 45  e register..** E
2780b 76 65 72 79 20 73 74 61 74 65 6d 65 6e 74 20 74  very statement t
2780c 68 61 74 20 6d 69 67 68 74 20 64 6f 20 61 6e 20  hat might do an 
2780d 49 4e 53 45 52 54 20 69 6e 74 6f 20 61 6e 20 61  INSERT into an a
2780e 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20  utoincrement.** 
2780f 74 61 62 6c 65 20 28 65 69 74 68 65 72 20 64 69  table (either di
27810 72 65 63 74 6c 79 20 6f 72 20 74 68 72 6f 75 67  rectly or throug
27811 68 20 74 72 69 67 67 65 72 73 29 20 6e 65 65 64  h triggers) need
27812 73 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 0a 2a  s to call this.*
27813 2a 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 62  * routine just b
27814 65 66 6f 72 65 20 74 68 65 20 22 65 78 69 74 22  efore the "exit"
27815 20 63 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   code..*/.SQLITE
27816 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
27817 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65  lite3Autoincreme
27818 6e 74 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61  ntEnd(Parse *pPa
27819 72 73 65 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49  rse){.  AutoincI
2781a 6e 66 6f 20 2a 70 3b 0a 20 20 56 64 62 65 20 2a  nfo *p;.  Vdbe *
2781b 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2781c 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e;.  sqlite3 *db
2781d 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
2781e 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20    assert( v );. 
2781f 20 66 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d   for(p = pParse-
27820 3e 70 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70  >pAinc; p; p = p
27821 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 44 62  ->pNext){.    Db
27822 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
27823 5b 70 2d 3e 69 44 62 5d 3b 0a 20 20 20 20 69 6e  [p->iDb];.    in
27824 74 20 6a 31 2c 20 6a 32 2c 20 6a 33 2c 20 6a 34  t j1, j2, j3, j4
27825 2c 20 6a 35 3b 0a 20 20 20 20 69 6e 74 20 69 52  , j5;.    int iR
27826 65 63 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 49  ec;.    int memI
27827 64 20 3d 20 70 2d 3e 72 65 67 43 74 72 3b 0a 0a  d = p->regCtr;..
27828 20 20 20 20 69 52 65 63 20 3d 20 73 71 6c 69 74      iRec = sqlit
27829 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2782a 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rse);.    sqlite
2782b 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
2782c 65 2c 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70 44  e, 0, p->iDb, pD
2782d 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71  b->pSchema->pSeq
2782e 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  Tab, OP_OpenWrit
2782f 65 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c  e);.    j1 = sql
27830 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
27831 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 6d 65  , OP_NotNull, me
27832 6d 49 64 2b 31 29 3b 0a 20 20 20 20 6a 32 20 3d  mId+1);.    j2 =
27833 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27834 70 30 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 29  p0(v, OP_Rewind)
27835 3b 0a 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74  ;.    j3 = sqlit
27836 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27837 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c  OP_Column, 0, 0,
27838 20 69 52 65 63 29 3b 0a 20 20 20 20 6a 34 20 3d   iRec);.    j4 =
27839 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2783a 70 33 28 76 2c 20 4f 50 5f 45 71 2c 20 6d 65 6d  p3(v, OP_Eq, mem
2783b 49 64 2d 31 2c 20 30 2c 20 69 52 65 63 29 3b 0a  Id-1, 0, iRec);.
2783c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2783d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
2783e 2c 20 30 2c 20 6a 33 29 3b 0a 20 20 20 20 73 71  , 0, j3);.    sq
2783f 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
27840 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 73 71  e(v, j2);.    sq
27841 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27842 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
27843 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20  0, memId+1);.   
27844 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j5 = sqlite3Vdb
27845 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
27846 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  to);.    sqlite3
27847 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
27848 6a 34 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j4);.    sqlite3
27849 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2784a 5f 52 6f 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64  _Rowid, 0, memId
2784b 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +1);.    sqlite3
2784c 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2784d 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j1);.    sqlite3
2784e 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2784f 6a 35 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j5);.    sqlite3
27850 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
27851 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d  _MakeRecord, mem
27852 49 64 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b 0a  Id-1, 2, iRec);.
27853 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27854 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
27855 72 74 2c 20 30 2c 20 69 52 65 63 2c 20 6d 65 6d  rt, 0, iRec, mem
27856 49 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  Id+1);.    sqlit
27857 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
27858 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
27859 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2785a 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c  eAddOp0(v, OP_Cl
2785b 6f 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ose);.    sqlite
2785c 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2785d 70 50 61 72 73 65 2c 20 69 52 65 63 29 3b 0a 20  pParse, iRec);. 
2785e 20 7d 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a   }.}.#else./*.**
2785f 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   If SQLITE_OMIT_
27860 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73  AUTOINCREMENT is
27861 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74   defined, then t
27862 68 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65  he three routine
27863 73 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 61  s.** above are a
27864 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64  ll no-ops.*/.# d
27865 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 67  efine autoIncBeg
27866 69 6e 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 20  in(A,B,C) (0).# 
27867 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 74  define autoIncSt
27868 65 70 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66  ep(A,B,C).#endif
27869 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2786a 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f  AUTOINCREMENT */
2786b 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  .../* Forward de
2786c 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61  claration */.sta
2786d 74 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69  tic int xferOpti
2786e 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  mization(.  Pars
2786f 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
27870 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
27871 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
27872 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f  pDest,         /
27873 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65 20 61  * The table we a
27874 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74  re inserting int
27875 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  o */.  Select *p
27876 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20  Select,      /* 
27877 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
27878 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  nt to use as the
27879 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a   data source */.
2787a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
2787b 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74          /* How t
2787c 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61  o handle constra
2787d 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  int errors */.  
2787e 69 6e 74 20 69 44 62 44 65 73 74 20 20 20 20 20  int iDbDest     
2787f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
27880 61 62 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a  abase of pDest *
27881 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  /.);../*.** This
27882 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
27883 20 74 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f   to handle SQL o
27884 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
27885 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  forms:.**.**    
27886 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c  insert into TABL
27887 45 20 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65  E (IDLIST) value
27888 73 28 45 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20  s(EXPRLIST).**  
27889 20 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41    insert into TA
2788a 42 4c 45 20 28 49 44 4c 49 53 54 29 20 73 65 6c  BLE (IDLIST) sel
2788b 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44  ect.**.** The ID
2788c 4c 49 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  LIST following t
2788d 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73  he table name is
2788e 20 61 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c   always optional
2788f 2e 20 20 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a  .  If omitted,.*
27890 2a 20 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66  * then a list of
27891 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72   all columns for
27892 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 75   the table is su
27893 62 73 74 69 74 75 74 65 64 2e 20 20 54 68 65 20  bstituted.  The 
27894 49 44 4c 49 53 54 0a 2a 2a 20 61 70 70 65 61 72  IDLIST.** appear
27895 73 20 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e  s in the pColumn
27896 20 70 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f   parameter.  pCo
27897 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20  lumn is NULL if 
27898 49 44 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65  IDLIST is omitte
27899 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69  d..**.** The pLi
2789a 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 6f 6c  st parameter hol
2789b 64 73 20 45 58 50 52 4c 49 53 54 20 69 6e 20 74  ds EXPRLIST in t
2789c 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20 6f 66  he first form of
2789d 20 74 68 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73   the INSERT.** s
2789e 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20  tatement above, 
2789f 61 6e 64 20 70 53 65 6c 65 63 74 20 69 73 20 4e  and pSelect is N
278a0 55 4c 4c 2e 20 20 46 6f 72 20 74 68 65 20 73 65  ULL.  For the se
278a1 63 6f 6e 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74  cond form, pList
278a2 20 69 73 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20   is.** NULL and 
278a3 70 53 65 6c 65 63 74 20 69 73 20 61 20 70 6f 69  pSelect is a poi
278a4 6e 74 65 72 20 74 6f 20 74 68 65 20 73 65 6c 65  nter to the sele
278a5 63 74 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65  ct statement use
278a6 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a  d to generate.**
278a7 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 6e   data for the in
278a8 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sert..**.** The 
278a9 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 66  code generated f
278aa 6f 6c 6c 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f  ollows one of fo
278ab 75 72 20 74 65 6d 70 6c 61 74 65 73 2e 20 20 46  ur templates.  F
278ac 6f 72 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73  or a simple.** s
278ad 65 6c 65 63 74 20 77 69 74 68 20 64 61 74 61 20  elect with data 
278ae 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41  coming from a VA
278af 4c 55 45 53 20 63 6c 61 75 73 65 2c 20 74 68 65  LUES clause, the
278b0 20 63 6f 64 65 20 65 78 65 63 75 74 65 73 0a 2a   code executes.*
278b1 2a 20 6f 6e 63 65 20 73 74 72 61 69 67 68 74 20  * once straight 
278b2 64 6f 77 6e 20 74 68 72 6f 75 67 68 2e 20 20 50  down through.  P
278b3 73 65 75 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f  seudo-code follo
278b4 77 73 20 28 77 65 20 63 61 6c 6c 20 74 68 69 73  ws (we call this
278b5 0a 2a 2a 20 74 68 65 20 22 31 73 74 20 74 65 6d  .** the "1st tem
278b6 70 6c 61 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20  plate"):.**.**  
278b7 20 20 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74         open writ
278b8 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62  e cursor to <tab
278b9 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69  le> and its indi
278ba 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70  ces.**         p
278bb 75 74 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73  uts VALUES claus
278bc 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e  e expressions on
278bd 74 6f 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20  to the stack.** 
278be 20 20 20 20 20 20 20 20 77 72 69 74 65 20 74 68          write th
278bf 65 20 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f  e resulting reco
278c0 72 64 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a  rd into <table>.
278c1 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
278c2 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72  up.**.** The thr
278c3 65 65 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d  ee remaining tem
278c4 70 6c 61 74 65 73 20 61 73 73 75 6d 65 20 74 68  plates assume th
278c5 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f  e statement is o
278c6 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
278c7 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c     INSERT INTO <
278c8 74 61 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e  table> SELECT ..
278c9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
278ca 45 4c 45 43 54 20 63 6c 61 75 73 65 20 69 73 20  ELECT clause is 
278cb 6f 66 20 74 68 65 20 72 65 73 74 72 69 63 74 65  of the restricte
278cc 64 20 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a  d form "SELECT *
278cd 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20   FROM <table2>" 
278ce 2d 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f  -.** in other wo
278cf 72 64 73 20 69 66 20 74 68 65 20 53 45 4c 45 43  rds if the SELEC
278d0 54 20 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75  T pulls all colu
278d1 6d 6e 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c  mns from a singl
278d2 65 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74  e table.** and t
278d3 68 65 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45  here is no WHERE
278d4 20 6f 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f   or LIMIT or GRO
278d5 55 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42  UP BY or ORDER B
278d6 59 20 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a  Y clauses, and.*
278d7 2a 20 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e  * if <table2> an
278d8 64 20 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64  d <table1> are d
278d9 69 73 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62  istinct tables b
278da 75 74 20 68 61 76 65 20 69 64 65 6e 74 69 63 61  ut have identica
278db 6c 0a 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e  l.** schemas, in
278dc 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20  cluding all the 
278dd 73 61 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68  same indices, th
278de 65 6e 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74  en a special opt
278df 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20  imization.** is 
278e0 69 6e 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70  invoked that cop
278e1 69 65 73 20 72 61 77 20 72 65 63 6f 72 64 73 20  ies raw records 
278e2 66 72 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76  from <table2> ov
278e3 65 72 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a  er to <table1>..
278e4 2a 2a 20 53 65 65 20 74 68 65 20 78 66 65 72 4f  ** See the xferO
278e5 70 74 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75  ptimization() fu
278e6 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69  nction for the i
278e7 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
278e8 20 74 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74   this.** templat
278e9 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  e.  This is the 
278ea 32 6e 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a  2nd template..**
278eb 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e  .**         open
278ec 20 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20   a write cursor 
278ed 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  to <table>.**   
278ee 20 20 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20        open read 
278ef 63 75 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65  cursor on <table
278f0 32 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72  2>.**         tr
278f1 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72  ansfer all recor
278f2 64 73 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f  ds in <table2> o
278f3 76 65 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  ver to <table>.*
278f4 2a 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20  *         close 
278f5 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20  cursors.**      
278f6 20 20 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78     foreach index
278f7 20 6f 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20   on <table>.**  
278f8 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20           open a 
278f9 77 72 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20  write cursor on 
278fa 74 68 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65  the <table> inde
278fb 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f  x.**           o
278fc 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f  pen a read curso
278fd 72 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70  r on the corresp
278fe 6f 6e 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20  onding <table2> 
278ff 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20  index.**        
27900 20 20 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20     transfer all 
27901 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
27902 20 72 65 61 64 20 74 6f 20 74 68 65 20 77 72 69   read to the wri
27903 74 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20  te cursors.**   
27904 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75          close cu
27905 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
27906 20 65 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a   end foreach.**.
27907 2a 2a 20 54 68 65 20 33 72 64 20 74 65 6d 70 6c  ** The 3rd templ
27908 61 74 65 20 69 73 20 66 6f 72 20 77 68 65 6e 20  ate is for when 
27909 74 68 65 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c  the second templ
2790a 61 74 65 20 64 6f 65 73 20 6e 6f 74 20 61 70 70  ate does not app
2790b 6c 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53 45  ly.** and the SE
2790c 4c 45 43 54 20 63 6c 61 75 73 65 20 64 6f 65 73  LECT clause does
2790d 20 6e 6f 74 20 72 65 61 64 20 66 72 6f 6d 20 3c   not read from <
2790e 74 61 62 6c 65 3e 20 61 74 20 61 6e 79 20 74 69  table> at any ti
2790f 6d 65 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72  me..** The gener
27910 61 74 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77  ated code follow
27911 73 20 74 68 69 73 20 74 65 6d 70 6c 61 74 65 3a  s this template:
27912 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  .**.**         E
27913 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20  OF <- 0.**      
27914 20 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20     X <- A.**    
27915 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20       goto B.**  
27916 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72      A: setup for
27917 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20   the SELECT.**  
27918 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72         loop over
27919 20 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65   the rows in the
2791a 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
2791b 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 73       load values
2791c 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20   into registers 
2791d 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20  R..R+n.**       
2791e 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20      yield X.**  
2791f 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a         end loop.
27920 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
27921 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c  up after the SEL
27922 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  ECT.**         E
27923 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20  OF <- 1.**      
27924 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20     yield X.**   
27925 20 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20        goto A.** 
27926 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 77 72 69       B: open wri
27927 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61  te cursor to <ta
27928 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64  ble> and its ind
27929 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20  ices.**      C: 
2792a 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
2792b 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44     if EOF goto D
2792c 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65  .**         inse
2792d 72 74 20 74 68 65 20 73 65 6c 65 63 74 20 72 65  rt the select re
2792e 73 75 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65  sult into <table
2792f 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a  > from R..R+n.**
27930 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a           goto C.
27931 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e  **      D: clean
27932 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68  up.**.** The 4th
27933 20 74 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65   template is use
27934 64 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20  d if the insert 
27935 73 74 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20  statement takes 
27936 69 74 73 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72  its.** values fr
27937 6f 6d 20 61 20 53 45 4c 45 43 54 20 62 75 74 20  om a SELECT but 
27938 74 68 65 20 64 61 74 61 20 69 73 20 62 65 69 6e  the data is bein
27939 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
2793a 61 20 74 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20  a table.** that 
2793b 69 73 20 61 6c 73 6f 20 72 65 61 64 20 61 73 20  is also read as 
2793c 70 61 72 74 20 6f 66 20 74 68 65 20 53 45 4c 45  part of the SELE
2793d 43 54 2e 20 20 49 6e 20 74 68 65 20 74 68 69 72  CT.  In the thir
2793e 64 20 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61  d form,.** we ha
2793f 76 65 20 74 6f 20 75 73 65 20 61 20 69 6e 74 65  ve to use a inte
27940 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 74  rmediate table t
27941 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  o store the resu
27942 6c 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65  lts of.** the se
27943 6c 65 63 74 2e 20 20 54 68 65 20 74 65 6d 70 6c  lect.  The templ
27944 61 74 65 20 69 73 20 6c 69 6b 65 20 74 68 69 73  ate is like this
27945 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
27946 45 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20  EOF <- 0.**     
27947 20 20 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20      X <- A.**   
27948 20 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20        goto B.** 
27949 20 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f       A: setup fo
2794a 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20  r the SELECT.** 
2794b 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65          loop ove
2794c 72 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20  r the tables in 
2794d 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20  the SELECT.**   
2794e 20 20 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c          load val
2794f 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  ue into register
27950 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20   R..R+n.**      
27951 20 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20       yield X.** 
27952 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70          end loop
27953 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61  .**         clea
27954 6e 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45  nup after the SE
27955 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
27956 45 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20  EOF <- 1.**     
27957 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20      yield X.**  
27958 20 20 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f         halt-erro
27959 72 0a 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65  r.**      B: ope
2795a 6e 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20  n temp table.** 
2795b 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a       L: yield X.
2795c 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
2795d 46 20 67 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20  F goto M.**     
2795e 20 20 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66      insert row f
2795f 72 6f 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20  rom R..R+n into 
27960 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20  temp table.**   
27961 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20        goto L.** 
27962 20 20 20 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69       M: open wri
27963 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61  te cursor to <ta
27964 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64  ble> and its ind
27965 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ices.**         
27966 72 65 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c  rewind temp tabl
27967 65 0a 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f  e.**      C: loo
27968 70 20 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69  p over rows of i
27969 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
2796a 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  e.**           t
2796b 72 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66  ransfer values f
2796c 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65  orm intermediate
2796d 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62   table into <tab
2796e 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  le>.**         e
2796f 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20  nd loop.**      
27970 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51  D: cleanup.*/.SQ
27971 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
27972 64 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28  d sqlite3Insert(
27973 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
27974 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
27975 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
27976 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
27977 74 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  t,    /* Name of
27978 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
27979 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
2797a 6e 67 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ng */.  ExprList
2797b 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a   *pList,      /*
2797c 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20   List of values 
2797d 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
2797e 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
2797f 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53  ect,      /* A S
27980 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
27981 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64 61  to use as the da
27982 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49  ta source */.  I
27983 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20  dList *pColumn, 
27984 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
27985 61 6d 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69  ames correspondi
27986 6e 67 20 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f  ng to IDLIST. */
27987 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20  .  int onError  
27988 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
27989 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72  to handle constr
2798a 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29  aint errors */.)
2798b 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
2798c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2798d 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73   main database s
2798e 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 54 61  tructure */.  Ta
2798f 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
27990 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
27991 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e   to insert into.
27992 20 20 61 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20    aka TABLE */. 
27993 20 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20   char *zTab;    
27994 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
27995 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f  f the table into
27996 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69 6e   which we are in
27997 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e  serting */.  con
27998 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  st char *zDb;   
27999 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
2799a 65 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69  e database holdi
2799b 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  ng this table */
2799c 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78  .  int i, j, idx
2799d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70  ;        /* Loop
2799e 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56   counters */.  V
2799f 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
279a0 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
279a1 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20   code into this 
279a2 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
279a3 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
279a4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  ;          /* Fo
279a5 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69  r looping over i
279a6 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20 74 61  ndices of the ta
279a7 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
279a8 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  lumn;          /
279a9 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
279aa 6d 6e 73 20 69 6e 20 74 68 65 20 64 61 74 61 20  mns in the data 
279ab 2a 2f 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e  */.  int nHidden
279ac 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75   = 0;      /* Nu
279ad 6d 62 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63  mber of hidden c
279ae 6f 6c 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20  olumns if TABLE 
279af 69 73 20 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20  is virtual */.  
279b0 69 6e 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b  int baseCur = 0;
279b1 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
279b2 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
279b3 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65  pTab */.  int ke
279b4 79 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20  yColumn = -1;   
279b5 2f 2a 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69  /* Column that i
279b6 73 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52  s the INTEGER PR
279b7 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69  IMARY KEY */.  i
279b8 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20  nt endOfLoop;   
279b9 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f       /* Label fo
279ba 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  r the end of the
279bb 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20   insertion loop 
279bc 2a 2f 0a 20 20 69 6e 74 20 75 73 65 54 65 6d 70  */.  int useTemp
279bd 54 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74  Table = 0; /* St
279be 6f 72 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c  ore SELECT resul
279bf 74 73 20 69 6e 20 69 6e 74 65 72 6d 65 64 69 61  ts in intermedia
279c0 74 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  te table */.  in
279c1 74 20 73 72 63 54 61 62 20 3d 20 30 3b 20 20 20  t srcTab = 0;   
279c2 20 20 20 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65      /* Data come
279c3 73 20 66 72 6f 6d 20 74 68 69 73 20 74 65 6d 70  s from this temp
279c4 6f 72 61 72 79 20 63 75 72 73 6f 72 20 69 66 20  orary cursor if 
279c5 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  >=0 */.  int add
279c6 72 49 6e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f  rInsTop = 0;   /
279c7 2a 20 4a 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20  * Jump to label 
279c8 22 44 22 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  "D" */.  int add
279c9 72 43 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f  rCont = 0;     /
279ca 2a 20 54 6f 70 20 6f 66 20 69 6e 73 65 72 74 20  * Top of insert 
279cb 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 22 43 22 20  loop. Label "C" 
279cc 69 6e 20 74 65 6d 70 6c 61 74 65 73 20 33 20 61  in templates 3 a
279cd 6e 64 20 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64  nd 4 */.  int ad
279ce 64 72 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20  drSelect = 0;   
279cf 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 63 6f  /* Address of co
279d0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70  routine that imp
279d1 6c 65 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45  lements the SELE
279d2 43 54 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  CT */.  SelectDe
279d3 73 74 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a  st dest;      /*
279d4 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72   Destination for
279d5 20 53 45 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f   SELECT on rhs o
279d6 66 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e  f INSERT */.  in
279d7 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
279d8 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
279d9 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
279da 20 54 41 42 4c 45 20 2a 2f 0a 20 20 44 62 20 2a   TABLE */.  Db *
279db 70 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  pDb;            
279dc 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
279dd 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62  e containing tab
279de 6c 65 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65  le being inserte
279df 64 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  d into */.  int 
279e0 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 30 3b 20  appendFlag = 0; 
279e1 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
279e2 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c   insert is likel
279e3 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
279e4 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73  d */..  /* Regis
279e5 74 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ter allocations 
279e6 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 6f 6d  */.  int regFrom
279e7 53 65 6c 65 63 74 20 3d 20 30 3b 2f 2a 20 42 61  Select = 0;/* Ba
279e8 73 65 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  se register for 
279e9 64 61 74 61 20 63 6f 6d 69 6e 67 20 66 72 6f 6d  data coming from
279ea 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74   SELECT */.  int
279eb 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 30 3b   regAutoinc = 0;
279ec 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
279ed 6f 6c 64 69 6e 67 20 74 68 65 20 41 55 54 4f 49  olding the AUTOI
279ee 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72  NCREMENT counter
279ef 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77   */.  int regRow
279f0 43 6f 75 6e 74 20 3d 20 30 3b 20 20 2f 2a 20 4d  Count = 0;  /* M
279f1 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20  emory cell used 
279f2 66 6f 72 20 74 68 65 20 72 6f 77 20 63 6f 75 6e  for the row coun
279f3 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ter */.  int reg
279f4 49 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Ins;           /
279f5 2a 20 42 6c 6f 63 6b 20 6f 66 20 72 65 67 73 20  * Block of regs 
279f6 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 2b 64 61  holding rowid+da
279f7 74 61 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65  ta being inserte
279f8 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f  d */.  int regRo
279f9 77 69 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  wid;         /* 
279fa 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e  registers holdin
279fb 67 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 2a  g insert rowid *
279fc 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b  /.  int regData;
279fd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67            /* reg
279fe 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 66 69  ister holding fi
279ff 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e  rst column to in
27a00 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65  sert */.  int re
27a01 67 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20  gRecord;        
27a02 2f 2a 20 48 6f 6c 64 73 20 74 68 65 20 61 73 73  /* Holds the ass
27a03 65 6d 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f  emblied row reco
27a04 72 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45  rd */.  int regE
27a05 6f 66 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  of = 0;       /*
27a06 20 52 65 67 69 73 74 65 72 20 72 65 63 6f 72 64   Register record
27a07 69 6e 67 20 65 6e 64 20 6f 66 20 53 45 4c 45 43  ing end of SELEC
27a08 54 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  T data */.  int 
27a09 2a 61 52 65 67 49 64 78 20 3d 20 30 3b 20 20 20  *aRegIdx = 0;   
27a0a 20 20 2f 2a 20 4f 6e 65 20 72 65 67 69 73 74 65    /* One registe
27a0b 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65  r allocated to e
27a0c 61 63 68 20 69 6e 64 65 78 20 2a 2f 0a 0a 23 69  ach index */..#i
27a0d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
27a0e 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20  T_TRIGGER.  int 
27a0f 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20  isView;         
27a10 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
27a11 69 66 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  if attempting to
27a12 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 76   insert into a v
27a13 69 65 77 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  iew */.  Trigger
27a14 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20   *pTrigger;     
27a15 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
27a16 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62  triggers on pTab
27a17 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f  , if required */
27a18 0a 20 20 69 6e 74 20 74 6d 61 73 6b 3b 20 20 20  .  int tmask;   
27a19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27a1a 2a 20 4d 61 73 6b 20 6f 66 20 74 72 69 67 67 65  * Mask of trigge
27a1b 72 20 74 69 6d 65 73 20 2a 2f 0a 23 65 6e 64 69  r times */.#endi
27a1c 66 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  f..  db = pParse
27a1d 2d 3e 64 62 3b 0a 20 20 6d 65 6d 73 65 74 28 26  ->db;.  memset(&
27a1e 64 65 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28  dest, 0, sizeof(
27a1f 64 65 73 74 29 29 3b 0a 20 20 69 66 28 20 70 50  dest));.  if( pP
27a20 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
27a21 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
27a22 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  {.    goto inser
27a23 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  t_cleanup;.  }..
27a24 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
27a25 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
27a26 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65   we will be inse
27a27 72 74 69 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d  rting new inform
27a28 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73  ation..  */.  as
27a29 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e  sert( pTabList->
27a2a 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61  nSrc==1 );.  zTa
27a2b 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  b = pTabList->a[
27a2c 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20  0].zName;.  if( 
27a2d 4e 45 56 45 52 28 7a 54 61 62 3d 3d 30 29 20 29  NEVER(zTab==0) )
27a2e 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
27a2f 61 6e 75 70 3b 0a 20 20 70 54 61 62 20 3d 20 73  anup;.  pTab = s
27a30 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
27a31 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62  kup(pParse, pTab
27a32 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61  List);.  if( pTa
27a33 62 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  b==0 ){.    goto
27a34 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
27a35 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c  .  }.  iDb = sql
27a36 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
27a37 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
27a38 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ema);.  assert( 
27a39 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
27a3a 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
27a3b 69 44 62 5d 3b 0a 20 20 7a 44 62 20 3d 20 70 44  iDb];.  zDb = pD
27a3c 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20  b->zName;.  if( 
27a3d 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
27a3e 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
27a3f 49 4e 53 45 52 54 2c 20 70 54 61 62 2d 3e 7a 4e  INSERT, pTab->zN
27a40 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  ame, 0, zDb) ){.
27a41 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
27a42 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
27a43 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66  /* Figure out if
27a44 20 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69   we have any tri
27a45 67 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65  ggers and if the
27a46 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a   table being.  *
27a47 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  * inserted into 
27a48 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23  is a view.  */.#
27a49 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27a4a 49 54 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72  IT_TRIGGER.  pTr
27a4b 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54  igger = sqlite3T
27a4c 72 69 67 67 65 72 73 45 78 69 73 74 28 70 50 61  riggersExist(pPa
27a4d 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 49 4e  rse, pTab, TK_IN
27a4e 53 45 52 54 2c 20 30 2c 20 26 74 6d 61 73 6b 29  SERT, 0, &tmask)
27a4f 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54 61  ;.  isView = pTa
27a50 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23  b->pSelect!=0;.#
27a51 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70 54  else.# define pT
27a52 72 69 67 67 65 72 20 30 0a 23 20 64 65 66 69 6e  rigger 0.# defin
27a53 65 20 74 6d 61 73 6b 20 30 0a 23 20 64 65 66 69  e tmask 0.# defi
27a54 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
27a55 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
27a56 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64  _OMIT_VIEW.# und
27a57 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69  ef isView.# defi
27a58 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
27a59 69 66 0a 20 20 61 73 73 65 72 74 28 20 28 70 54  if.  assert( (pT
27a5a 72 69 67 67 65 72 20 26 26 20 74 6d 61 73 6b 29  rigger && tmask)
27a5b 20 7c 7c 20 28 70 54 72 69 67 67 65 72 3d 3d 30   || (pTrigger==0
27a5c 20 26 26 20 74 6d 61 73 6b 3d 3d 30 29 20 29 3b   && tmask==0) );
27a5d 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69  ..  /* If pTab i
27a5e 73 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c  s really a view,
27a5f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 68 61   make sure it ha
27a60 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  s been initializ
27a61 65 64 2e 0a 20 20 2a 2a 20 56 69 65 77 47 65 74  ed..  ** ViewGet
27a62 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 69 73  ColumnNames() is
27a63 20 61 20 6e 6f 2d 6f 70 20 69 66 20 70 54 61 62   a no-op if pTab
27a64 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 28   is not a view (
27a65 6f 72 20 76 69 72 74 75 61 6c 20 0a 20 20 2a 2a  or virtual .  **
27a66 20 6d 6f 64 75 6c 65 20 74 61 62 6c 65 29 2e 0a   module table)..
27a67 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
27a68 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
27a69 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
27a6a 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69  b) ){.    goto i
27a6b 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
27a6c 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20   }..  /* Ensure 
27a6d 74 68 61 74 3a 0a 20 20 2a 20 20 28 61 29 20 74  that:.  *  (a) t
27a6e 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
27a6f 72 65 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20  read-only, .  * 
27a70 20 28 62 29 20 74 68 61 74 20 69 66 20 69 74 20   (b) that if it 
27a71 69 73 20 61 20 76 69 65 77 20 74 68 65 6e 20 4f  is a view then O
27a72 4e 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72  N INSERT trigger
27a73 73 20 65 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69  s exist.  */.  i
27a74 66 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64  f( sqlite3IsRead
27a75 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61  Only(pParse, pTa
27a76 62 2c 20 74 6d 61 73 6b 29 20 29 7b 0a 20 20 20  b, tmask) ){.   
27a77 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
27a78 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
27a79 41 6c 6c 6f 63 61 74 65 20 61 20 56 44 42 45 0a  Allocate a VDBE.
27a7a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
27a7b 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
27a7c 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
27a7d 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
27a7e 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73  nup;.  if( pPars
27a7f 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73  e->nested==0 ) s
27a80 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
27a81 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c  hanges(v);.  sql
27a82 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
27a83 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
27a84 70 53 65 6c 65 63 74 20 7c 7c 20 70 54 72 69 67  pSelect || pTrig
27a85 67 65 72 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e  ger, iDb);..#ifn
27a86 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27a87 58 46 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66  XFER_OPT.  /* If
27a88 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
27a89 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20  s of the form.  
27a8a 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e  **.  **       IN
27a8b 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65  SERT INTO <table
27a8c 31 3e 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  1> SELECT * FROM
27a8d 20 3c 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a   <table2>;.  **.
27a8e 20 20 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 61    ** Then specia
27a8f 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20  l optimizations 
27a90 63 61 6e 20 62 65 20 61 70 70 6c 69 65 64 20 74  can be applied t
27a91 68 61 74 20 6d 61 6b 65 20 74 68 65 20 74 72 61  hat make the tra
27a92 6e 73 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 20  nsfer.  ** very 
27a93 66 61 73 74 20 61 6e 64 20 77 68 69 63 68 20 72  fast and which r
27a94 65 64 75 63 65 20 66 72 61 67 6d 65 6e 74 61 74  educe fragmentat
27a95 69 6f 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a  ion of indices..
27a96 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69    **.  ** This i
27a97 73 20 74 68 65 20 32 6e 64 20 74 65 6d 70 6c 61  s the 2nd templa
27a98 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  te..  */.  if( p
27a99 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65  Column==0 && xfe
27a9a 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50  rOptimization(pP
27a9b 61 72 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c  arse, pTab, pSel
27a9c 65 63 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44  ect, onError, iD
27a9d 62 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  b) ){.    assert
27a9e 28 20 21 70 54 72 69 67 67 65 72 20 29 3b 0a 20  ( !pTrigger );. 
27a9f 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
27aa0 3d 3d 30 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ==0 );.    goto 
27aa1 69 6e 73 65 72 74 5f 65 6e 64 3b 0a 20 20 7d 0a  insert_end;.  }.
27aa2 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
27aa3 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a  _OMIT_XFER_OPT *
27aa4 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  /..  /* If this 
27aa5 69 73 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d  is an AUTOINCREM
27aa6 45 4e 54 20 74 61 62 6c 65 2c 20 6c 6f 6f 6b 20  ENT table, look 
27aa7 75 70 20 74 68 65 20 73 65 71 75 65 6e 63 65 20  up the sequence 
27aa8 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 20 20  number in the.  
27aa9 2a 2a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  ** sqlite_sequen
27aaa 63 65 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f  ce table and sto
27aab 72 65 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20  re it in memory 
27aac 63 65 6c 6c 20 72 65 67 41 75 74 6f 69 6e 63 2e  cell regAutoinc.
27aad 0a 20 20 2a 2f 0a 20 20 72 65 67 41 75 74 6f 69  .  */.  regAutoi
27aae 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69  nc = autoIncBegi
27aaf 6e 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  n(pParse, iDb, p
27ab0 54 61 62 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75  Tab);..  /* Figu
27ab1 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
27ab2 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20  columns of data 
27ab3 61 72 65 20 73 75 70 70 6c 69 65 64 2e 20 20 49  are supplied.  I
27ab4 66 20 74 68 65 20 64 61 74 61 0a 20 20 2a 2a 20  f the data.  ** 
27ab5 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61  is coming from a
27ab6 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
27ab7 74 2c 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65  t, then generate
27ab8 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68   a co-routine th
27ab9 61 74 0a 20 20 2a 2a 20 70 72 6f 64 75 63 65 73  at.  ** produces
27aba 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
27abb 20 74 68 65 20 53 45 4c 45 43 54 20 6f 6e 20 65   the SELECT on e
27abc 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20  ach invocation. 
27abd 20 54 68 65 0a 20 20 2a 2a 20 63 6f 2d 72 6f 75   The.  ** co-rou
27abe 74 69 6e 65 20 69 73 20 74 68 65 20 63 6f 6d 6d  tine is the comm
27abf 6f 6e 20 68 65 61 64 65 72 20 74 6f 20 74 68 65  on header to the
27ac0 20 33 72 64 20 61 6e 64 20 34 74 68 20 74 65 6d   3rd and 4th tem
27ac1 70 6c 61 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  plates..  */.  i
27ac2 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  f( pSelect ){.  
27ac3 20 20 2f 2a 20 44 61 74 61 20 69 73 20 63 6f 6d    /* Data is com
27ac4 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43  ing from a SELEC
27ac5 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64  T.  Generate cod
27ac6 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  e to implement t
27ac7 68 61 74 20 53 45 4c 45 43 54 0a 20 20 20 20 2a  hat SELECT.    *
27ac8 2a 20 61 73 20 61 20 63 6f 2d 72 6f 75 74 69 6e  * as a co-routin
27ac9 65 2e 20 20 54 68 65 20 63 6f 64 65 20 69 73 20  e.  The code is 
27aca 63 6f 6d 6d 6f 6e 20 74 6f 20 62 6f 74 68 20 74  common to both t
27acb 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 0a 20  he 3rd and 4th. 
27acc 20 20 20 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a     ** templates:
27acd 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
27ace 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 0a         EOF <- 0.
27acf 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 58      **         X
27ad0 20 3c 2d 20 41 0a 20 20 20 20 2a 2a 20 20 20 20   <- A.    **    
27ad1 20 20 20 20 20 67 6f 74 6f 20 42 0a 20 20 20 20       goto B.    
27ad2 2a 2a 20 20 20 20 20 20 41 3a 20 73 65 74 75 70  **      A: setup
27ad3 20 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0a   for the SELECT.
27ad4 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c      **         l
27ad5 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 74 61 62  oop over the tab
27ad6 6c 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43  les in the SELEC
27ad7 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  T.    **        
27ad8 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e     load value in
27ad9 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52  to register R..R
27ada 2b 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  +n.    **       
27adb 20 20 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20      yield X.    
27adc 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c  **         end l
27add 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  oop.    **      
27ade 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65 72     cleanup after
27adf 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20   the SELECT.    
27ae0 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c  **         EOF <
27ae1 2d 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  - 1.    **      
27ae2 20 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a     yield X.    *
27ae3 2a 20 20 20 20 20 20 20 20 20 68 61 6c 74 2d 65  *         halt-e
27ae4 72 72 6f 72 0a 20 20 20 20 2a 2a 0a 20 20 20 20  rror.    **.    
27ae5 2a 2a 20 4f 6e 20 65 61 63 68 20 69 6e 76 6f 63  ** On each invoc
27ae6 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 2d  ation of the co-
27ae7 72 6f 75 74 69 6e 65 2c 20 69 74 20 70 75 74 73  routine, it puts
27ae8 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
27ae9 20 74 68 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45   the.    ** SELE
27aea 43 54 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72  CT result into r
27aeb 65 67 69 73 74 65 72 73 20 64 65 73 74 2e 69 4d  egisters dest.iM
27aec 65 6d 2e 2e 2e 64 65 73 74 2e 69 4d 65 6d 2b 64  em...dest.iMem+d
27aed 65 73 74 2e 6e 4d 65 6d 2d 31 2e 0a 20 20 20 20  est.nMem-1..    
27aee 2a 2a 20 28 54 68 65 73 65 20 6f 75 74 70 75 74  ** (These output
27aef 20 72 65 67 69 73 74 65 72 73 20 61 72 65 20 61   registers are a
27af0 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69  llocated by sqli
27af1 74 65 33 53 65 6c 65 63 74 28 29 2e 29 20 20 57  te3Select().)  W
27af2 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 53  hen.    ** the S
27af3 45 4c 45 43 54 20 63 6f 6d 70 6c 65 74 65 73 2c  ELECT completes,
27af4 20 69 74 20 73 65 74 73 20 74 68 65 20 45 4f 46   it sets the EOF
27af5 20 66 6c 61 67 20 73 74 6f 72 65 64 20 69 6e 20   flag stored in 
27af6 72 65 67 45 6f 66 2e 0a 20 20 20 20 2a 2f 0a 20  regEof..    */. 
27af7 20 20 20 69 6e 74 20 72 63 2c 20 6a 31 3b 0a 0a     int rc, j1;..
27af8 20 20 20 20 72 65 67 45 6f 66 20 3d 20 2b 2b 70      regEof = ++p
27af9 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
27afa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27afb 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
27afc 2c 20 30 2c 20 72 65 67 45 6f 66 29 3b 20 20 20  , 0, regEof);   
27afd 20 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 30 20 2a     /* EOF <- 0 *
27afe 2f 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  /.    VdbeCommen
27aff 74 28 28 76 2c 20 22 53 45 4c 45 43 54 20 65 6f  t((v, "SELECT eo
27b00 66 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 73  f flag"));.    s
27b01 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
27b02 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f  Init(&dest, SRT_
27b03 43 6f 72 6f 75 74 69 6e 65 2c 20 2b 2b 70 50 61  Coroutine, ++pPa
27b04 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  rse->nMem);.    
27b05 61 64 64 72 53 65 6c 65 63 74 20 3d 20 73 71 6c  addrSelect = sql
27b06 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
27b07 64 64 72 28 76 29 2b 32 3b 0a 20 20 20 20 73 71  ddr(v)+2;.    sq
27b08 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27b09 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 61  v, OP_Integer, a
27b0a 64 64 72 53 65 6c 65 63 74 2d 31 2c 20 64 65 73  ddrSelect-1, des
27b0b 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 6a 31  t.iParm);.    j1
27b0c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
27b0d 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
27b0e 20 30 2c 20 30 29 3b 0a 20 20 20 20 56 64 62 65   0, 0);.    Vdbe
27b0f 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d  Comment((v, "Jum
27b10 70 20 6f 76 65 72 20 53 45 4c 45 43 54 20 63 6f  p over SELECT co
27b11 72 6f 75 74 69 6e 65 22 29 29 3b 0a 0a 20 20 20  routine"));..   
27b12 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20   /* Resolve the 
27b13 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74  expressions in t
27b14 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
27b15 65 6e 74 20 61 6e 64 20 65 78 65 63 75 74 65 20  ent and execute 
27b16 69 74 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  it. */.    rc = 
27b17 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
27b18 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26  arse, pSelect, &
27b19 64 65 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72  dest);.    asser
27b1a 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  t( pParse->nErr=
27b1b 3d 30 20 7c 7c 20 72 63 20 29 3b 0a 20 20 20 20  =0 || rc );.    
27b1c 69 66 28 20 72 63 20 7c 7c 20 4e 45 56 45 52 28  if( rc || NEVER(
27b1d 70 50 61 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c  pParse->nErr) ||
27b1e 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
27b1f 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  d ){.      goto 
27b20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
27b21 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
27b22 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27b23 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65  P_Integer, 1, re
27b24 67 45 6f 66 29 3b 20 20 20 20 20 20 20 20 20 2f  gEof);         /
27b25 2a 20 45 4f 46 20 3c 2d 20 31 20 2a 2f 0a 20 20  * EOF <- 1 */.  
27b26 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27b27 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
27b28 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 20 20 20   dest.iParm);   
27b29 2f 2a 20 79 69 65 6c 64 20 58 20 2a 2f 0a 20 20  /* yield X */.  
27b2a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27b2b 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op2(v, OP_Halt, 
27b2c 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 2c  SQLITE_INTERNAL,
27b2d 20 4f 45 5f 41 62 6f 72 74 29 3b 0a 20 20 20 20   OE_Abort);.    
27b2e 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
27b2f 22 45 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 63  "End of SELECT c
27b30 6f 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20  oroutine"));.   
27b31 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
27b32 48 65 72 65 28 76 2c 20 6a 31 29 3b 20 20 20 20  Here(v, j1);    
27b33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27b34 20 20 20 20 20 20 2f 2a 20 6c 61 62 65 6c 20 42        /* label B
27b35 3a 20 2a 2f 0a 0a 20 20 20 20 72 65 67 46 72 6f  : */..    regFro
27b36 6d 53 65 6c 65 63 74 20 3d 20 64 65 73 74 2e 69  mSelect = dest.i
27b37 4d 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Mem;.    assert(
27b38 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
27b39 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20   );.    nColumn 
27b3a 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
27b3b 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73  t->nExpr;.    as
27b3c 73 65 72 74 28 20 64 65 73 74 2e 6e 4d 65 6d 3d  sert( dest.nMem=
27b3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 0a 20 20 20  =nColumn );..   
27b3e 20 2f 2a 20 53 65 74 20 75 73 65 54 65 6d 70 54   /* Set useTempT
27b3f 61 62 6c 65 20 74 6f 20 54 52 55 45 20 69 66 20  able to TRUE if 
27b40 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
27b41 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
27b42 6e 74 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64  nt.    ** should
27b43 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   be written into
27b44 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62   a temporary tab
27b45 6c 65 20 28 74 65 6d 70 6c 61 74 65 20 34 29 2e  le (template 4).
27b46 20 20 53 65 74 20 74 6f 0a 20 20 20 20 2a 2a 20    Set to.    ** 
27b47 46 41 4c 53 45 20 69 66 20 65 61 63 68 2a 20 72  FALSE if each* r
27b48 6f 77 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ow of the SELECT
27b49 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20   can be written 
27b4a 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 0a 20 20  directly into.  
27b4b 20 20 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61    ** the destina
27b4c 74 69 6f 6e 20 74 61 62 6c 65 20 28 74 65 6d 70  tion table (temp
27b4d 6c 61 74 65 20 33 29 2e 0a 20 20 20 20 2a 2a 0a  late 3)..    **.
27b4e 20 20 20 20 2a 2a 20 41 20 74 65 6d 70 20 74 61      ** A temp ta
27b4f 62 6c 65 20 6d 75 73 74 20 62 65 20 75 73 65 64  ble must be used
27b50 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
27b51 69 6e 67 20 75 70 64 61 74 65 64 20 69 73 20 61  ing updated is a
27b52 6c 73 6f 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 6f  lso one.    ** o
27b53 66 20 74 68 65 20 74 61 62 6c 65 73 20 62 65 69  f the tables bei
27b54 6e 67 20 72 65 61 64 20 62 79 20 74 68 65 20 53  ng read by the S
27b55 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
27b56 20 20 41 6c 73 6f 20 75 73 65 20 61 20 0a 20 20    Also use a .  
27b57 20 20 2a 2a 20 74 65 6d 70 20 74 61 62 6c 65 20    ** temp table 
27b58 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 72  in the case of r
27b59 6f 77 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20  ow triggers..   
27b5a 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69   */.    if( pTri
27b5b 67 67 65 72 20 7c 7c 20 72 65 61 64 73 54 61 62  gger || readsTab
27b5c 6c 65 28 70 50 61 72 73 65 2c 20 61 64 64 72 53  le(pParse, addrS
27b5d 65 6c 65 63 74 2c 20 69 44 62 2c 20 70 54 61 62  elect, iDb, pTab
27b5e 29 20 29 7b 0a 20 20 20 20 20 20 75 73 65 54 65  ) ){.      useTe
27b5f 6d 70 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20  mpTable = 1;.   
27b60 20 7d 0a 0a 20 20 20 20 69 66 28 20 75 73 65 54   }..    if( useT
27b61 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
27b62 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
27b63 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74  coroutine to ext
27b64 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ract information
27b65 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54   from the SELECT
27b66 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 64  .      ** and ad
27b67 64 20 69 74 20 74 6f 20 61 20 74 72 61 6e 73 69  d it to a transi
27b68 65 6e 74 20 74 61 62 6c 65 20 73 72 63 54 61 62  ent table srcTab
27b69 2e 20 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  .  The code gene
27b6a 72 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 68  rated.      ** h
27b6b 65 72 65 20 69 73 20 66 72 6f 6d 20 74 68 65 20  ere is from the 
27b6c 34 74 68 20 74 65 6d 70 6c 61 74 65 3a 0a 20 20  4th template:.  
27b6d 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
27b6e 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d       B: open tem
27b6f 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  p table.      **
27b70 20 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58        L: yield X
27b71 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
27b72 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0a    if EOF goto M.
27b73 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
27b74 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d   insert row from
27b75 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d   R..R+n into tem
27b76 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  p table.      **
27b77 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a           goto L.
27b78 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 4d 3a        **      M:
27b79 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   ....      */.  
27b7a 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b 20      int regRec; 
27b7b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
27b7c 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 70 61 63  ster to hold pac
27b7d 6b 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  ked record */.  
27b7e 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 52      int regTempR
27b7f 6f 77 69 64 3b 20 20 20 20 2f 2a 20 52 65 67 69  owid;    /* Regi
27b80 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 65 6d  ster to hold tem
27b81 70 20 74 61 62 6c 65 20 52 4f 57 49 44 20 2a 2f  p table ROWID */
27b82 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 54  .      int addrT
27b83 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  op;         /* L
27b84 61 62 65 6c 20 22 4c 22 20 2a 2f 0a 20 20 20 20  abel "L" */.    
27b85 20 20 69 6e 74 20 61 64 64 72 49 66 3b 20 20 20    int addrIf;   
27b86 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
27b87 73 20 6f 66 20 6a 75 6d 70 20 74 6f 20 4d 20 2a  s of jump to M *
27b88 2f 0a 0a 20 20 20 20 20 20 73 72 63 54 61 62 20  /..      srcTab 
27b89 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
27b8a 3b 0a 20 20 20 20 20 20 72 65 67 52 65 63 20 3d  ;.      regRec =
27b8b 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
27b8c 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
27b8d 20 20 72 65 67 54 65 6d 70 52 6f 77 69 64 20 3d    regTempRowid =
27b8e 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
27b8f 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
27b90 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27b91 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70  Op2(v, OP_OpenEp
27b92 68 65 6d 65 72 61 6c 2c 20 73 72 63 54 61 62 2c  hemeral, srcTab,
27b93 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
27b94 20 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 74   addrTop = sqlit
27b95 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
27b96 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69  OP_Yield, dest.i
27b97 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 61 64 64  Parm);.      add
27b98 72 49 66 20 3d 20 73 71 6c 69 74 65 33 56 64 62  rIf = sqlite3Vdb
27b99 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
27b9a 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20 20 20 20  , regEof);.     
27b9b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27b9c 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
27b9d 6f 72 64 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65  ord, regFromSele
27b9e 63 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67  ct, nColumn, reg
27b9f 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Rec);.      sqli
27ba0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27ba1 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 73 72   OP_NewRowid, sr
27ba2 63 54 61 62 2c 20 72 65 67 54 65 6d 70 52 6f 77  cTab, regTempRow
27ba3 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
27ba4 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27ba5 4f 50 5f 49 6e 73 65 72 74 2c 20 73 72 63 54 61  OP_Insert, srcTa
27ba6 62 2c 20 72 65 67 52 65 63 2c 20 72 65 67 54 65  b, regRec, regTe
27ba7 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  mpRowid);.      
27ba8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27ba9 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
27baa 20 61 64 64 72 54 6f 70 29 3b 0a 20 20 20 20 20   addrTop);.     
27bab 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
27bac 48 65 72 65 28 76 2c 20 61 64 64 72 49 66 29 3b  Here(v, addrIf);
27bad 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
27bae 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
27baf 72 73 65 2c 20 72 65 67 52 65 63 29 3b 0a 20 20  rse, regRec);.  
27bb0 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
27bb1 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
27bb2 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29 3b  , regTempRowid);
27bb3 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
27bb4 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
27bb5 68 65 20 63 61 73 65 20 69 66 20 74 68 65 20 64  he case if the d
27bb6 61 74 61 20 66 6f 72 20 74 68 65 20 49 4e 53 45  ata for the INSE
27bb7 52 54 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f  RT is coming fro
27bb8 6d 20 61 20 56 41 4c 55 45 53 0a 20 20 20 20 2a  m a VALUES.    *
27bb9 2a 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a  * clause.    */.
27bba 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
27bbb 73 4e 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  sNC;.    memset(
27bbc 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
27bbd 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70  sNC));.    sNC.p
27bbe 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
27bbf 20 20 20 20 73 72 63 54 61 62 20 3d 20 2d 31 3b      srcTab = -1;
27bc0 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 73 65  .    assert( use
27bc1 54 65 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  TempTable==0 );.
27bc2 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c      nColumn = pL
27bc3 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78  ist ? pList->nEx
27bc4 70 72 20 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28  pr : 0;.    for(
27bc5 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20  i=0; i<nColumn; 
27bc6 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
27bc7 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
27bc8 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c  prNames(&sNC, pL
27bc9 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
27bca 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
27bcb 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
27bcc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
27bcd 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
27bce 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  re the number of
27bcf 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
27bd0 73 6f 75 72 63 65 20 64 61 74 61 20 6d 61 74 63  source data matc
27bd1 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 20  hes the number. 
27bd2 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74   ** of columns t
27bd3 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e  o be inserted in
27bd4 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  to the table..  
27bd5 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  */.  if( IsVirtu
27bd6 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
27bd7 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
27bd8 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
27bd9 20 20 20 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49     nHidden += (I
27bda 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70  sHiddenColumn(&p
27bdb 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20  Tab->aCol[i]) ? 
27bdc 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  1 : 0);.    }.  
27bdd 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d  }.  if( pColumn=
27bde 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26  =0 && nColumn &&
27bdf 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d   nColumn!=(pTab-
27be0 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29  >nCol-nHidden) )
27be1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
27be2 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
27be3 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 53 20        "table %S 
27be4 68 61 73 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62  has %d columns b
27be5 75 74 20 25 64 20 76 61 6c 75 65 73 20 77 65 72  ut %d values wer
27be6 65 20 73 75 70 70 6c 69 65 64 22 2c 0a 20 20 20  e supplied",.   
27be7 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c      pTabList, 0,
27be8 20 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69 64   pTab->nCol-nHid
27be9 64 65 6e 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  den, nColumn);. 
27bea 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
27beb 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
27bec 28 20 70 43 6f 6c 75 6d 6e 21 3d 30 20 26 26 20  ( pColumn!=0 && 
27bed 6e 43 6f 6c 75 6d 6e 21 3d 70 43 6f 6c 75 6d 6e  nColumn!=pColumn
27bee 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 73 71 6c  ->nId ){.    sql
27bef 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
27bf0 72 73 65 2c 20 22 25 64 20 76 61 6c 75 65 73 20  rse, "%d values 
27bf1 66 6f 72 20 25 64 20 63 6f 6c 75 6d 6e 73 22 2c  for %d columns",
27bf2 20 6e 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 75 6d   nColumn, pColum
27bf3 6e 2d 3e 6e 49 64 29 3b 0a 20 20 20 20 67 6f 74  n->nId);.    got
27bf4 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
27bf5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
27bf6 68 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  he INSERT statem
27bf7 65 6e 74 20 69 6e 63 6c 75 64 65 64 20 61 6e 20  ent included an 
27bf8 49 44 4c 49 53 54 20 74 65 72 6d 2c 20 74 68 65  IDLIST term, the
27bf9 6e 20 6d 61 6b 65 20 73 75 72 65 0a 20 20 2a 2a  n make sure.  **
27bfa 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66   all elements of
27bfb 20 74 68 65 20 49 44 4c 49 53 54 20 72 65 61 6c   the IDLIST real
27bfc 6c 79 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 6f  ly are columns o
27bfd 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  f the table and 
27bfe 0a 20 20 2a 2a 20 72 65 6d 65 6d 62 65 72 20 74  .  ** remember t
27bff 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65  he column indice
27c00 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  s..  **.  ** If 
27c01 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e  the table has an
27c02 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
27c03 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   KEY column and 
27c04 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a  that column.  **
27c05 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65   is named in the
27c06 20 49 44 4c 49 53 54 2c 20 74 68 65 6e 20 72 65   IDLIST, then re
27c07 63 6f 72 64 20 69 6e 20 74 68 65 20 6b 65 79 43  cord in the keyC
27c08 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 0a 20  olumn variable. 
27c09 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 69 6e   ** the index in
27c0a 74 6f 20 49 44 4c 49 53 54 20 6f 66 20 74 68 65  to IDLIST of the
27c0b 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c   primary key col
27c0c 75 6d 6e 2e 20 20 6b 65 79 43 6f 6c 75 6d 6e 20  umn.  keyColumn 
27c0d 69 73 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65  is.  ** the inde
27c0e 78 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79  x of the primary
27c0f 20 6b 65 79 20 61 73 20 69 74 20 61 70 70 65 61   key as it appea
27c10 72 73 20 69 6e 20 49 44 4c 49 53 54 2c 20 6e 6f  rs in IDLIST, no
27c11 74 20 61 73 0a 20 20 2a 2a 20 69 73 20 61 70 70  t as.  ** is app
27c12 65 61 72 73 20 69 6e 20 74 68 65 20 6f 72 69 67  ears in the orig
27c13 69 6e 61 6c 20 74 61 62 6c 65 2e 20 20 28 54 68  inal table.  (Th
27c14 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70  e index of the p
27c15 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20  rimary.  ** key 
27c16 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
27c17 74 61 62 6c 65 20 69 73 20 70 54 61 62 2d 3e 69  table is pTab->i
27c18 50 4b 65 79 2e 29 0a 20 20 2a 2f 0a 20 20 69 66  PKey.).  */.  if
27c19 28 20 70 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ( pColumn ){.   
27c1a 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c   for(i=0; i<pCol
27c1b 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a  umn->nId; i++){.
27c1c 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61        pColumn->a
27c1d 5b 69 5d 2e 69 64 78 20 3d 20 2d 31 3b 0a 20 20  [i].idx = -1;.  
27c1e 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
27c1f 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b   i<pColumn->nId;
27c20 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72   i++){.      for
27c21 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  (j=0; j<pTab->nC
27c22 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; j++){.      
27c23 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
27c24 49 43 6d 70 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b  ICmp(pColumn->a[
27c25 69 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  i].zName, pTab->
27c26 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d  aCol[j].zName)==
27c27 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  0 ){.          p
27c28 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78  Column->a[i].idx
27c29 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20   = j;.          
27c2a 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( j==pTab->iPK
27c2b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
27c2c 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b    keyColumn = i;
27c2d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
27c2e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27c2f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
27c30 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61        if( j>=pTa
27c31 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
27c32 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
27c33 52 6f 77 69 64 28 70 43 6f 6c 75 6d 6e 2d 3e 61  Rowid(pColumn->a
27c34 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  [i].zName) ){.  
27c35 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d          keyColum
27c36 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 7d  n = i;.        }
27c37 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
27c38 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
27c39 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25  pParse, "table %
27c3a 53 20 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20  S has no column 
27c3b 6e 61 6d 65 64 20 25 73 22 2c 0a 20 20 20 20 20  named %s",.     
27c3c 20 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73           pTabLis
27c3d 74 2c 20 30 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 61  t, 0, pColumn->a
27c3e 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
27c3f 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45        pParse->nE
27c40 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  rr++;.          
27c41 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
27c42 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nup;.        }. 
27c43 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
27c44 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
27c45 69 73 20 6e 6f 20 49 44 4c 49 53 54 20 74 65 72  is no IDLIST ter
27c46 6d 20 62 75 74 20 74 68 65 20 74 61 62 6c 65 20  m but the table 
27c47 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20 70  has an integer p
27c48 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 2c  rimary.  ** key,
27c49 20 74 68 65 20 73 65 74 20 74 68 65 20 6b 65 79   the set the key
27c4a 43 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 20  Column variable 
27c4b 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  to the primary k
27c4c 65 79 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a  ey column index.
27c4d 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67    ** in the orig
27c4e 69 6e 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e  inal table defin
27c4f 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  ition..  */.  if
27c50 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20  ( pColumn==0 && 
27c51 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20  nColumn>0 ){.   
27c52 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 70 54 61   keyColumn = pTa
27c53 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 7d 0a 20 20  b->iPKey;.  }.  
27c54 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69    .  /* Initiali
27c55 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  ze the count of 
27c56 72 6f 77 73 20 74 6f 20 62 65 20 69 6e 73 65 72  rows to be inser
27c57 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ted.  */.  if( d
27c58 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
27c59 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20  E_CountRows ){. 
27c5a 20 20 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d     regRowCount =
27c5b 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
27c5c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27c5d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
27c5e 65 67 65 72 2c 20 30 2c 20 72 65 67 52 6f 77 43  eger, 0, regRowC
27c5f 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ount);.  }..  /*
27c60 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
27c61 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 74 68 65  a view, open the
27c62 20 74 61 62 6c 65 20 61 6e 64 20 61 6e 64 20 61   table and and a
27c63 6c 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ll indices */.  
27c64 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20  if( !isView ){. 
27c65 20 20 20 69 6e 74 20 6e 49 64 78 3b 0a 0a 20 20     int nIdx;..  
27c66 20 20 62 61 73 65 43 75 72 20 3d 20 70 50 61 72    baseCur = pPar
27c67 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 6e 49  se->nTab;.    nI
27c68 64 78 20 3d 20 73 71 6c 69 74 65 33 4f 70 65 6e  dx = sqlite3Open
27c69 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28  TableAndIndices(
27c6a 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
27c6b 73 65 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72  seCur, OP_OpenWr
27c6c 69 74 65 29 3b 0a 20 20 20 20 61 52 65 67 49 64  ite);.    aRegId
27c6d 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
27c6e 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
27c6f 66 28 69 6e 74 29 2a 28 6e 49 64 78 2b 31 29 29  f(int)*(nIdx+1))
27c70 3b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64  ;.    if( aRegId
27c71 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  x==0 ){.      go
27c72 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
27c73 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  p;.    }.    for
27c74 28 69 3d 30 3b 20 69 3c 6e 49 64 78 3b 20 69 2b  (i=0; i<nIdx; i+
27c75 2b 29 7b 0a 20 20 20 20 20 20 61 52 65 67 49 64  +){.      aRegId
27c76 78 5b 69 5d 20 3d 20 2b 2b 70 50 61 72 73 65 2d  x[i] = ++pParse-
27c77 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  >nMem;.    }.  }
27c78 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74  ..  /* This is t
27c79 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6d 61  he top of the ma
27c7a 69 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f  in insertion loo
27c7b 70 20 2a 2f 0a 20 20 69 66 28 20 75 73 65 54 65  p */.  if( useTe
27c7c 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f  mpTable ){.    /
27c7d 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64  * This block cod
27c7e 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f  es the top of lo
27c7f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f  op only.  The co
27c80 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74  mplete loop is t
27c81 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77  he.    ** follow
27c82 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28  ing pseudocode (
27c83 74 65 6d 70 6c 61 74 65 20 34 29 3a 0a 20 20 20  template 4):.   
27c84 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
27c85 20 20 20 72 65 77 69 6e 64 20 74 65 6d 70 20 74     rewind temp t
27c86 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20 20  able.    **     
27c87 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72 6f   C: loop over ro
27c88 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 69 61  ws of intermedia
27c89 74 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  te table.    ** 
27c8a 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66            transf
27c8b 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69  er values form i
27c8c 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
27c8d 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 20  e into <table>. 
27c8e 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65 6e     **         en
27c8f 64 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20  d loop.    **   
27c90 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f     D: ....    */
27c91 0a 20 20 20 20 61 64 64 72 49 6e 73 54 6f 70 20  .    addrInsTop 
27c92 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
27c93 4f 70 31 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op1(v, OP_Rewind
27c94 2c 20 73 72 63 54 61 62 29 3b 0a 20 20 20 20 61  , srcTab);.    a
27c95 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65  ddrCont = sqlite
27c96 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
27c97 28 76 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  (v);.  }else if(
27c98 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
27c99 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f  /* This block co
27c9a 64 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c  des the top of l
27c9b 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63  oop only.  The c
27c9c 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20  omplete loop is 
27c9d 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f  the.    ** follo
27c9e 77 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20  wing pseudocode 
27c9f 28 74 65 6d 70 6c 61 74 65 20 33 29 3a 0a 20 20  (template 3):.  
27ca0 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
27ca1 20 43 3a 20 79 69 65 6c 64 20 58 0a 20 20 20 20   C: yield X.    
27ca2 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
27ca3 46 20 67 6f 74 6f 20 44 0a 20 20 20 20 2a 2a 20  F goto D.    ** 
27ca4 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74          insert t
27ca5 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74  he select result
27ca6 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72   into <table> fr
27ca7 6f 6d 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a 2a  om R..R+n.    **
27ca8 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a           goto C.
27ca9 20 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e      **      D: .
27caa 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64  ...    */.    ad
27cab 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33  drCont = sqlite3
27cac 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
27cad 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61  _Yield, dest.iPa
27cae 72 6d 29 3b 0a 20 20 20 20 61 64 64 72 49 6e 73  rm);.    addrIns
27caf 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Top = sqlite3Vdb
27cb0 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
27cb1 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20 7d 0a 0a  , regEof);.  }..
27cb2 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65    /* Allocate re
27cb3 67 69 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64  gisters for hold
27cb4 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 6f 66  ing the rowid of
27cb5 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 0a 20 20   the new row,.  
27cb6 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ** the content o
27cb7 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 20 61  f the new row, a
27cb8 6e 64 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65  nd the assemblie
27cb9 64 20 72 6f 77 20 72 65 63 6f 72 64 2e 0a 20 20  d row record..  
27cba 2a 2f 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d  */.  regRecord =
27cbb 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
27cbc 0a 20 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65  .  regRowid = re
27cbd 67 49 6e 73 20 3d 20 70 50 61 72 73 65 2d 3e 6e  gIns = pParse->n
27cbe 4d 65 6d 2b 31 3b 0a 20 20 70 50 61 72 73 65 2d  Mem+1;.  pParse-
27cbf 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e  >nMem += pTab->n
27cc0 43 6f 6c 20 2b 20 31 3b 0a 20 20 69 66 28 20 49  Col + 1;.  if( I
27cc1 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
27cc2 7b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 2b 2b  {.    regRowid++
27cc3 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
27cc4 65 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 67 44  em++;.  }.  regD
27cc5 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64 2b 31  ata = regRowid+1
27cc6 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20  ;..  /* Run the 
27cc7 42 45 46 4f 52 45 20 61 6e 64 20 49 4e 53 54 45  BEFORE and INSTE
27cc8 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 2c 20  AD OF triggers, 
27cc9 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
27cca 0a 20 20 2a 2f 0a 20 20 65 6e 64 4f 66 4c 6f 6f  .  */.  endOfLoo
27ccb 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  p = sqlite3VdbeM
27ccc 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69  akeLabel(v);.  i
27ccd 66 28 20 74 6d 61 73 6b 20 26 20 54 52 49 47 47  f( tmask & TRIGG
27cce 45 52 5f 42 45 46 4f 52 45 20 29 7b 0a 20 20 20  ER_BEFORE ){.   
27ccf 20 69 6e 74 20 72 65 67 43 6f 6c 73 20 3d 20 73   int regCols = s
27cd0 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
27cd1 67 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  ge(pParse, pTab-
27cd2 3e 6e 43 6f 6c 2b 31 29 3b 0a 0a 20 20 20 20 2f  >nCol+1);..    /
27cd3 2a 20 62 75 69 6c 64 20 74 68 65 20 4e 45 57 2e  * build the NEW.
27cd4 2a 20 72 65 66 65 72 65 6e 63 65 20 72 6f 77 2e  * reference row.
27cd5 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 74    Note that if t
27cd6 68 65 72 65 20 69 73 20 61 6e 20 49 4e 54 45 47  here is an INTEG
27cd7 45 52 0a 20 20 20 20 2a 2a 20 50 52 49 4d 41 52  ER.    ** PRIMAR
27cd8 59 20 4b 45 59 20 69 6e 74 6f 20 77 68 69 63 68  Y KEY into which
27cd9 20 61 20 4e 55 4c 4c 20 69 73 20 62 65 69 6e 67   a NULL is being
27cda 20 69 6e 73 65 72 74 65 64 2c 20 74 68 61 74 20   inserted, that 
27cdb 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 0a 20 20 20  NULL will be.   
27cdc 20 2a 2a 20 74 72 61 6e 73 6c 61 74 65 64 20 69   ** translated i
27cdd 6e 74 6f 20 61 20 75 6e 69 71 75 65 20 49 44 20  nto a unique ID 
27cde 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 20 42 75  for the row.  Bu
27cdf 74 20 6f 6e 20 61 20 42 45 46 4f 52 45 20 74 72  t on a BEFORE tr
27ce0 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20 77 65  igger,.    ** we
27ce1 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 77 68 61   do not know wha
27ce2 74 20 74 68 65 20 75 6e 69 71 75 65 20 49 44 20  t the unique ID 
27ce3 77 69 6c 6c 20 62 65 20 28 62 65 63 61 75 73 65  will be (because
27ce4 20 74 68 65 20 69 6e 73 65 72 74 20 68 61 73 0a   the insert has.
27ce5 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 70 70 65      ** not happe
27ce6 6e 65 64 20 79 65 74 29 20 73 6f 20 77 65 20 73  ned yet) so we s
27ce7 75 62 73 74 69 74 75 74 65 20 61 20 72 6f 77 69  ubstitute a rowi
27ce8 64 20 6f 66 20 2d 31 0a 20 20 20 20 2a 2f 0a 20  d of -1.    */. 
27ce9 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e     if( keyColumn
27cea 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  <0 ){.      sqli
27ceb 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27cec 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c   OP_Integer, -1,
27ced 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 7d   regCols);.    }
27cee 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
27cef 6a 31 3b 0a 20 20 20 20 20 20 69 66 28 20 75 73  j1;.      if( us
27cf0 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
27cf1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27cf2 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
27cf3 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65  lumn, srcTab, ke
27cf4 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 43 6f 6c 73  yColumn, regCols
27cf5 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
27cf6 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
27cf7 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f  pSelect==0 );  /
27cf8 2a 20 4f 74 68 65 72 77 69 73 65 20 75 73 65 54  * Otherwise useT
27cf9 65 6d 70 54 61 62 6c 65 20 69 73 20 74 72 75 65  empTable is true
27cfa 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
27cfb 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
27cfc 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79  se, pList->a[key
27cfd 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72  Column].pExpr, r
27cfe 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 7d  egCols);.      }
27cff 0a 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69  .      j1 = sqli
27d00 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
27d01 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67   OP_NotNull, reg
27d02 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 73 71 6c  Cols);.      sql
27d03 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27d04 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31  , OP_Integer, -1
27d05 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20  , regCols);.    
27d06 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
27d07 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20  pHere(v, j1);.  
27d08 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27d09 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
27d0a 42 65 49 6e 74 2c 20 72 65 67 43 6f 6c 73 29 3b  BeInt, regCols);
27d0b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
27d0c 61 6e 6e 6f 74 20 68 61 76 65 20 74 72 69 67 67  annot have trigg
27d0d 65 72 73 20 6f 6e 20 61 20 76 69 72 74 75 61 6c  ers on a virtual
27d0e 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 77 65   table. If it we
27d0f 72 65 20 70 6f 73 73 69 62 6c 65 2c 0a 20 20 20  re possible,.   
27d10 20 2a 2a 20 74 68 69 73 20 62 6c 6f 63 6b 20 77   ** this block w
27d11 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 61 63 63  ould have to acc
27d12 6f 75 6e 74 20 66 6f 72 20 68 69 64 64 65 6e 20  ount for hidden 
27d13 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20  column..    */. 
27d14 20 20 20 61 73 73 65 72 74 28 20 21 49 73 56 69     assert( !IsVi
27d15 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 0a  rtual(pTab) );..
27d16 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68      /* Create th
27d17 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 61 74  e new column dat
27d18 61 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  a.    */.    for
27d19 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
27d1a 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
27d1b 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29  if( pColumn==0 )
27d1c 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 69 3b  {.        j = i;
27d1d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
27d1e 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
27d1f 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a  <pColumn->nId; j
27d20 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
27d21 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d  f( pColumn->a[j]
27d22 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b  .idx==i ) break;
27d23 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
27d24 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f   }.      if( pCo
27d25 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75  lumn && j>=pColu
27d26 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20  mn->nId ){.     
27d27 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
27d28 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  de(pParse, pTab-
27d29 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20  >aCol[i].pDflt, 
27d2a 72 65 67 43 6f 6c 73 2b 69 2b 31 29 3b 0a 20 20  regCols+i+1);.  
27d2b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73      }else if( us
27d2c 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
27d2d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27d2e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
27d2f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c  lumn, srcTab, j,
27d30 20 72 65 67 43 6f 6c 73 2b 69 2b 31 29 3b 20 0a   regCols+i+1); .
27d31 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27d32 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65       assert( pSe
27d33 6c 65 63 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74  lect==0 ); /* Ot
27d34 68 65 72 77 69 73 65 20 75 73 65 54 65 6d 70 54  herwise useTempT
27d35 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f 0a  able is true */.
27d36 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
27d37 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28  xprCodeAndCache(
27d38 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61  pParse, pList->a
27d39 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 43 6f  [j].pExpr, regCo
27d3a 6c 73 2b 69 2b 31 29 3b 0a 20 20 20 20 20 20 7d  ls+i+1);.      }
27d3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
27d3c 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53  f this is an INS
27d3d 45 52 54 20 6f 6e 20 61 20 76 69 65 77 20 77 69  ERT on a view wi
27d3e 74 68 20 61 6e 20 49 4e 53 54 45 41 44 20 4f 46  th an INSTEAD OF
27d3f 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 2c   INSERT trigger,
27d40 0a 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 61  .    ** do not a
27d41 74 74 65 6d 70 74 20 61 6e 79 20 63 6f 6e 76 65  ttempt any conve
27d42 72 73 69 6f 6e 73 20 62 65 66 6f 72 65 20 61 73  rsions before as
27d43 73 65 6d 62 6c 69 6e 67 20 74 68 65 20 72 65 63  sembling the rec
27d44 6f 72 64 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74  ord..    ** If t
27d45 68 69 73 20 69 73 20 61 20 72 65 61 6c 20 74 61  his is a real ta
27d46 62 6c 65 2c 20 61 74 74 65 6d 70 74 20 63 6f 6e  ble, attempt con
27d47 76 65 72 73 69 6f 6e 73 20 61 73 20 72 65 71 75  versions as requ
27d48 69 72 65 64 20 62 79 20 74 68 65 0a 20 20 20 20  ired by the.    
27d49 2a 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  ** table column 
27d4a 61 66 66 69 6e 69 74 69 65 73 2e 0a 20 20 20 20  affinities..    
27d4b 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  */.    if( !isVi
27d4c 65 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ew ){.      sqli
27d4d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27d4e 20 4f 50 5f 41 66 66 69 6e 69 74 79 2c 20 72 65   OP_Affinity, re
27d4f 67 43 6f 6c 73 2b 31 2c 20 70 54 61 62 2d 3e 6e  gCols+1, pTab->n
27d50 43 6f 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Col);.      sqli
27d51 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79  te3TableAffinity
27d52 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20 20  Str(v, pTab);.  
27d53 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65    }..    /* Fire
27d54 20 42 45 46 4f 52 45 20 6f 72 20 49 4e 53 54 45   BEFORE or INSTE
27d55 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a  AD OF triggers *
27d56 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  /.    sqlite3Cod
27d57 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72  eRowTrigger(pPar
27d58 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b  se, pTrigger, TK
27d59 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52 49 47  _INSERT, 0, TRIG
27d5a 47 45 52 5f 42 45 46 4f 52 45 2c 20 0a 20 20 20  GER_BEFORE, .   
27d5b 20 20 20 20 20 70 54 61 62 2c 20 72 65 67 43 6f       pTab, regCo
27d5c 6c 73 2d 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 2c  ls-pTab->nCol-1,
27d5d 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c   onError, endOfL
27d5e 6f 6f 70 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  oop);..    sqlit
27d5f 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
27d60 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 43 6f  ge(pParse, regCo
27d61 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31  ls, pTab->nCol+1
27d62 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 75 73  );.  }..  /* Pus
27d63 68 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  h the record num
27d64 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ber for the new 
27d65 65 6e 74 72 79 20 6f 6e 74 6f 20 74 68 65 20 73  entry onto the s
27d66 74 61 63 6b 2e 20 20 54 68 65 0a 20 20 2a 2a 20  tack.  The.  ** 
27d67 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73  record number is
27d68 20 61 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65   a randomly gene
27d69 72 61 74 65 20 69 6e 74 65 67 65 72 20 63 72 65  rate integer cre
27d6a 61 74 65 64 20 62 79 20 4e 65 77 52 6f 77 69 64  ated by NewRowid
27d6b 0a 20 20 2a 2a 20 65 78 63 65 70 74 20 77 68 65  .  ** except whe
27d6c 6e 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  n the table has 
27d6d 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
27d6e 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20 69  RY KEY column, i
27d6f 6e 20 77 68 69 63 68 0a 20 20 2a 2a 20 63 61 73  n which.  ** cas
27d70 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  e the record num
27d71 62 65 72 20 69 73 20 74 68 65 20 73 61 6d 65 20  ber is the same 
27d72 61 73 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20  as that column. 
27d73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 56  .  */.  if( !isV
27d74 69 65 77 20 29 7b 0a 20 20 20 20 69 66 28 20 49  iew ){.    if( I
27d75 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
27d76 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72  {.      /* The r
27d77 6f 77 20 74 68 61 74 20 74 68 65 20 56 55 70 64  ow that the VUpd
27d78 61 74 65 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20  ate opcode will 
27d79 64 65 6c 65 74 65 3a 20 6e 6f 6e 65 20 2a 2f 0a  delete: none */.
27d7a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27d7b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
27d7c 6c 6c 2c 20 30 2c 20 72 65 67 49 6e 73 29 3b 0a  ll, 0, regIns);.
27d7d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6b 65      }.    if( ke
27d7e 79 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20  yColumn>=0 ){.  
27d7f 20 20 20 20 69 66 28 20 75 73 65 54 65 6d 70 54      if( useTempT
27d80 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
27d81 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27d82 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
27d83 73 72 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d  srcTab, keyColum
27d84 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  n, regRowid);.  
27d85 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53      }else if( pS
27d86 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
27d87 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27d88 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
27d89 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6b 65  regFromSelect+ke
27d8a 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69  yColumn, regRowi
27d8b 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  d);.      }else{
27d8c 0a 20 20 20 20 20 20 20 20 56 64 62 65 4f 70 20  .        VdbeOp 
27d8d 2a 70 4f 70 3b 0a 20 20 20 20 20 20 20 20 73 71  *pOp;.        sq
27d8e 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
27d8f 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b  arse, pList->a[k
27d90 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c  eyColumn].pExpr,
27d91 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
27d92 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65      pOp = sqlite
27d93 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31  3VdbeGetOp(v, -1
27d94 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 41  );.        if( A
27d95 4c 57 41 59 53 28 70 4f 70 29 20 26 26 20 70 4f  LWAYS(pOp) && pO
27d96 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75  p->opcode==OP_Nu
27d97 6c 6c 20 26 26 20 21 49 73 56 69 72 74 75 61 6c  ll && !IsVirtual
27d98 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
27d99 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67 20 3d      appendFlag =
27d9a 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   1;.          pO
27d9b 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e  p->opcode = OP_N
27d9c 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 20  ewRowid;.       
27d9d 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 62 61 73     pOp->p1 = bas
27d9e 65 43 75 72 3b 0a 20 20 20 20 20 20 20 20 20 20  eCur;.          
27d9f 70 4f 70 2d 3e 70 32 20 3d 20 72 65 67 52 6f 77  pOp->p2 = regRow
27da0 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f  id;.          pO
27da1 70 2d 3e 70 33 20 3d 20 72 65 67 41 75 74 6f 69  p->p3 = regAutoi
27da2 6e 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  nc;.        }.  
27da3 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49      }.      /* I
27da4 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  f the PRIMARY KE
27da5 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  Y expression is 
27da6 4e 55 4c 4c 2c 20 74 68 65 6e 20 75 73 65 20 4f  NULL, then use O
27da7 50 5f 4e 65 77 52 6f 77 69 64 0a 20 20 20 20 20  P_NewRowid.     
27da8 20 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20   ** to generate 
27da9 61 20 75 6e 69 71 75 65 20 70 72 69 6d 61 72 79  a unique primary
27daa 20 6b 65 79 20 76 61 6c 75 65 2e 0a 20 20 20 20   key value..    
27dab 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21    */.      if( !
27dac 61 70 70 65 6e 64 46 6c 61 67 20 29 7b 0a 20 20  appendFlag ){.  
27dad 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20        int j1;.  
27dae 20 20 20 20 20 20 69 66 28 20 21 49 73 56 69 72        if( !IsVir
27daf 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
27db0 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c          j1 = sql
27db1 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
27db2 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65  , OP_NotNull, re
27db3 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20  gRowid);.       
27db4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27db5 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp3(v, OP_NewRo
27db6 77 69 64 2c 20 62 61 73 65 43 75 72 2c 20 72 65  wid, baseCur, re
27db7 67 52 6f 77 69 64 2c 20 72 65 67 41 75 74 6f 69  gRowid, regAutoi
27db8 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  nc);.          s
27db9 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
27dba 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20  re(v, j1);.     
27dbb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27dbc 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
27dbd 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
27dbe 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  v);.          sq
27dbf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27dc0 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
27dc1 67 52 6f 77 69 64 2c 20 6a 31 2b 32 29 3b 0a 20  gRowid, j1+2);. 
27dc2 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
27dc3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27dc4 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
27dc5 6e 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  nt, regRowid);. 
27dc6 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
27dc7 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
27dc8 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71  Tab) ){.      sq
27dc9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27dca 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
27dcb 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65  egRowid);.    }e
27dcc 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
27dcd 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27dce 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62 61 73  OP_NewRowid, bas
27dcf 65 43 75 72 2c 20 72 65 67 52 6f 77 69 64 2c 20  eCur, regRowid, 
27dd0 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20  regAutoinc);.   
27dd1 20 20 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20     appendFlag = 
27dd2 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 75 74  1;.    }.    aut
27dd3 6f 49 6e 63 53 74 65 70 28 70 50 61 72 73 65 2c  oIncStep(pParse,
27dd4 20 72 65 67 41 75 74 6f 69 6e 63 2c 20 72 65 67   regAutoinc, reg
27dd5 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20  Rowid);..    /* 
27dd6 50 75 73 68 20 6f 6e 74 6f 20 74 68 65 20 73 74  Push onto the st
27dd7 61 63 6b 2c 20 64 61 74 61 20 66 6f 72 20 61 6c  ack, data for al
27dd8 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  l columns of the
27dd9 20 6e 65 77 20 65 6e 74 72 79 2c 20 62 65 67 69   new entry, begi
27dda 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 77 69 74  nning.    ** wit
27ddb 68 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75  h the first colu
27ddc 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  mn..    */.    n
27ddd 48 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20  Hidden = 0;.    
27dde 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
27ddf 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
27de0 20 20 20 69 6e 74 20 69 52 65 67 53 74 6f 72 65     int iRegStore
27de1 20 3d 20 72 65 67 52 6f 77 69 64 2b 31 2b 69 3b   = regRowid+1+i;
27de2 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 70 54  .      if( i==pT
27de3 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
27de4 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75       /* The valu
27de5 65 20 6f 66 20 74 68 65 20 49 4e 54 45 47 45 52  e of the INTEGER
27de6 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
27de7 75 6d 6e 20 69 73 20 61 6c 77 61 79 73 20 61 20  umn is always a 
27de8 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2a  NULL..        **
27de9 20 57 68 65 6e 65 76 65 72 20 74 68 69 73 20 63   Whenever this c
27dea 6f 6c 75 6d 6e 20 69 73 20 72 65 61 64 2c 20 74  olumn is read, t
27deb 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
27dec 20 77 69 6c 6c 20 62 65 20 73 75 62 73 74 69 74   will be substit
27ded 75 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  uted.        ** 
27dee 69 6e 20 69 74 73 20 70 6c 61 63 65 2e 20 20 53  in its place.  S
27def 6f 20 77 69 6c 6c 20 66 69 6c 6c 20 74 68 69 73  o will fill this
27df0 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 61 20 4e   column with a N
27df1 55 4c 4c 20 74 6f 20 61 76 6f 69 64 0a 20 20 20  ULL to avoid.   
27df2 20 20 20 20 20 2a 2a 20 74 61 6b 69 6e 67 20 75       ** taking u
27df3 70 20 64 61 74 61 20 73 70 61 63 65 20 77 69 74  p data space wit
27df4 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  h information th
27df5 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  at will never be
27df6 20 75 73 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20   used. */.      
27df7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27df8 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
27df9 30 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20  0, iRegStore);. 
27dfa 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
27dfb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
27dfc 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b  f( pColumn==0 ){
27dfd 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 73 48  .        if( IsH
27dfe 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61  iddenColumn(&pTa
27dff 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 29 7b 0a 20  b->aCol[i]) ){. 
27e00 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
27e01 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
27e02 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20   );.          j 
27e03 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20  = -1;.          
27e04 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20 20 20  nHidden++;.     
27e05 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27e06 20 20 20 20 6a 20 3d 20 69 20 2d 20 6e 48 69 64      j = i - nHid
27e07 64 65 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  den;.        }. 
27e08 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27e09 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
27e0a 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b  Column->nId; j++
27e0b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
27e0c 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69   pColumn->a[j].i
27e0d 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20  dx==i ) break;. 
27e0e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
27e0f 0a 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20 7c  .      if( j<0 |
27e10 7c 20 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 7c 7c 20  | nColumn==0 || 
27e11 28 70 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70  (pColumn && j>=p
27e12 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a  Column->nId) ){.
27e13 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
27e14 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
27e15 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44  pTab->aCol[i].pD
27e16 66 6c 74 2c 20 69 52 65 67 53 74 6f 72 65 29 3b  flt, iRegStore);
27e17 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
27e18 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b   useTempTable ){
27e19 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27e1a 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
27e1b 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c  _Column, srcTab,
27e1c 20 6a 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 20   j, iRegStore); 
27e1d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
27e1e 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
27e1f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27e20 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
27e21 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74  y, regFromSelect
27e22 2b 6a 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a  +j, iRegStore);.
27e23 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27e24 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
27e25 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69  Code(pParse, pLi
27e26 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20  st->a[j].pExpr, 
27e27 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20  iRegStore);.    
27e28 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
27e29 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
27e2a 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61  to check constra
27e2b 69 6e 74 73 20 61 6e 64 20 67 65 6e 65 72 61 74  ints and generat
27e2c 65 20 69 6e 64 65 78 20 6b 65 79 73 20 61 6e 64  e index keys and
27e2d 0a 20 20 20 20 2a 2a 20 64 6f 20 74 68 65 20 69  .    ** do the i
27e2e 6e 73 65 72 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  nsertion..    */
27e2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
27e30 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
27e31 45 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74  E.    if( IsVirt
27e32 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
27e33 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
27e34 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68  VTab = (const ch
27e35 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74 56  ar *)sqlite3GetV
27e36 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b  Table(db, pTab);
27e37 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74  .      sqlite3Vt
27e38 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70  abMakeWritable(p
27e39 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
27e3a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27e3b 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64  ddOp4(v, OP_VUpd
27e3c 61 74 65 2c 20 31 2c 20 70 54 61 62 2d 3e 6e 43  ate, 1, pTab->nC
27e3d 6f 6c 2b 32 2c 20 72 65 67 49 6e 73 2c 20 70 56  ol+2, regIns, pV
27e3e 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  Tab, P4_VTAB);. 
27e3f 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41       sqlite3MayA
27e40 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20  bort(pParse);.  
27e41 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
27e42 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69     {.      int i
27e43 73 52 65 70 6c 61 63 65 3b 20 20 20 20 2f 2a 20  sReplace;    /* 
27e44 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 63  Set to true if c
27e45 6f 6e 73 74 72 61 69 6e 74 73 20 6d 61 79 20 63  onstraints may c
27e46 61 75 73 65 20 61 20 72 65 70 6c 61 63 65 20 2a  ause a replace *
27e47 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
27e48 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
27e49 74 43 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20  tChecks(pParse, 
27e4a 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72  pTab, baseCur, r
27e4b 65 67 49 6e 73 2c 20 61 52 65 67 49 64 78 2c 0a  egIns, aRegIdx,.
27e4c 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c            keyCol
27e4d 75 6d 6e 3e 3d 30 2c 20 30 2c 20 6f 6e 45 72 72  umn>=0, 0, onErr
27e4e 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 26  or, endOfLoop, &
27e4f 69 73 52 65 70 6c 61 63 65 0a 20 20 20 20 20 20  isReplace.      
27e50 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27e51 46 6b 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  FkCheck(pParse, 
27e52 70 54 61 62 2c 20 30 2c 20 72 65 67 49 6e 73 29  pTab, 0, regIns)
27e53 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43  ;.      sqlite3C
27e54 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e  ompleteInsertion
27e55 28 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72  (.          pPar
27e56 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75  se, pTab, baseCu
27e57 72 2c 20 72 65 67 49 6e 73 2c 20 61 52 65 67 49  r, regIns, aRegI
27e58 64 78 2c 20 30 2c 20 61 70 70 65 6e 64 46 6c 61  dx, 0, appendFla
27e59 67 2c 20 69 73 52 65 70 6c 61 63 65 3d 3d 30 0a  g, isReplace==0.
27e5a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20        );.    }. 
27e5b 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20   }..  /* Update 
27e5c 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77  the count of row
27e5d 73 20 74 68 61 74 20 61 72 65 20 69 6e 73 65 72  s that are inser
27e5e 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ted.  */.  if( (
27e5f 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
27e60 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30  TE_CountRows)!=0
27e61 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
27e62 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27e63 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f  AddImm, regRowCo
27e64 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20  unt, 1);.  }..  
27e65 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a  if( pTrigger ){.
27e66 20 20 20 20 2f 2a 20 43 6f 64 65 20 41 46 54 45      /* Code AFTE
27e67 52 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20  R triggers */.  
27e68 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77    sqlite3CodeRow
27e69 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
27e6a 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 49 4e 53  pTrigger, TK_INS
27e6b 45 52 54 2c 20 30 2c 20 54 52 49 47 47 45 52 5f  ERT, 0, TRIGGER_
27e6c 41 46 54 45 52 2c 20 0a 20 20 20 20 20 20 20 20  AFTER, .        
27e6d 70 54 61 62 2c 20 72 65 67 44 61 74 61 2d 32 2d  pTab, regData-2-
27e6e 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 6f 6e 45 72  pTab->nCol, onEr
27e6f 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  ror, endOfLoop);
27e70 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 62  .  }..  /* The b
27e71 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6d 61 69  ottom of the mai
27e72 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70  n insertion loop
27e73 2c 20 69 66 20 74 68 65 20 64 61 74 61 20 73 6f  , if the data so
27e74 75 72 63 65 0a 20 20 2a 2a 20 69 73 20 61 20 53  urce.  ** is a S
27e75 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
27e76 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
27e77 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
27e78 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20  v, endOfLoop);. 
27e79 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c   if( useTempTabl
27e7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
27e7b 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27e7c 5f 4e 65 78 74 2c 20 73 72 63 54 61 62 2c 20 61  _Next, srcTab, a
27e7d 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 73 71  ddrCont);.    sq
27e7e 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
27e7f 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f 70 29  e(v, addrInsTop)
27e80 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27e81 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
27e82 6f 73 65 2c 20 73 72 63 54 61 62 29 3b 0a 20 20  ose, srcTab);.  
27e83 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63  }else if( pSelec
27e84 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
27e85 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27e86 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 43 6f  _Goto, 0, addrCo
27e87 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
27e88 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
27e89 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20 7d  addrInsTop);.  }
27e8a 0a 0a 20 20 69 66 28 20 21 49 73 56 69 72 74 75  ..  if( !IsVirtu
27e8b 61 6c 28 70 54 61 62 29 20 26 26 20 21 69 73 56  al(pTab) && !isV
27e8c 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6c  iew ){.    /* Cl
27e8d 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20 6f  ose all tables o
27e8e 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c  pened */.    sql
27e8f 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
27e90 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 62 61 73 65  , OP_Close, base
27e91 43 75 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 64  Cur);.    for(id
27e92 78 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  x=1, pIdx=pTab->
27e93 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
27e94 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
27e95 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  idx++){.      sq
27e96 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
27e97 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 64 78  v, OP_Close, idx
27e98 2b 62 61 73 65 43 75 72 29 3b 0a 20 20 20 20 7d  +baseCur);.    }
27e99 0a 20 20 7d 0a 0a 69 6e 73 65 72 74 5f 65 6e 64  .  }..insert_end
27e9a 3a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  :.  /* Update th
27e9b 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
27e9c 65 20 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69  e table by stori
27e9d 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ng the content o
27e9e 66 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d  f the.  ** maxim
27e9f 75 6d 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72  um rowid counter
27ea0 20 76 61 6c 75 65 73 20 72 65 63 6f 72 64 65 64   values recorded
27ea1 20 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67   while inserting
27ea2 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 75 74 6f 69   into.  ** autoi
27ea3 6e 63 72 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e  ncrement tables.
27ea4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72  .  */.  if( pPar
27ea5 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26  se->nested==0 &&
27ea6 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65   pParse->pTrigge
27ea7 72 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73  rTab==0 ){.    s
27ea8 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
27ea9 65 6e 74 45 6e 64 28 70 50 61 72 73 65 29 3b 0a  entEnd(pParse);.
27eaa 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52    }..  /*.  ** R
27eab 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
27eac 20 6f 66 20 72 6f 77 73 20 69 6e 73 65 72 74 65   of rows inserte
27ead 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  d. If this routi
27eae 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65  ne is .  ** gene
27eaf 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61  rating code beca
27eb0 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f  use of a call to
27eb1 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
27eb2 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20  rse(), do not.  
27eb3 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61  ** invoke the ca
27eb4 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e  llback function.
27eb5 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d  .  */.  if( (db-
27eb6 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f  >flags&SQLITE_Co
27eb7 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50 61  untRows) && !pPa
27eb8 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 21  rse->nested && !
27eb9 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
27eba 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Tab ){.    sqlit
27ebb 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27ebc 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72 65  OP_ResultRow, re
27ebd 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20  gRowCount, 1);. 
27ebe 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
27ebf 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
27ec0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
27ec1 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
27ec2 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72  COLNAME_NAME, "r
27ec3 6f 77 73 20 69 6e 73 65 72 74 65 64 22 2c 20 53  ows inserted", S
27ec4 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
27ec5 20 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61 6e   }..insert_clean
27ec6 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63  up:.  sqlite3Src
27ec7 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
27ec8 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  TabList);.  sqli
27ec9 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
27eca 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
27ecb 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
27ecc 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29  ete(db, pSelect)
27ecd 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73  ;.  sqlite3IdLis
27ece 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c  tDelete(db, pCol
27ecf 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  umn);.  sqlite3D
27ed0 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49 64  bFree(db, aRegId
27ed1 78 29 3b 0a 7d 0a 0a 2f 2a 20 4d 61 6b 65 20 73  x);.}../* Make s
27ed2 75 72 65 20 22 69 73 56 69 65 77 22 20 61 6e 64  ure "isView" and
27ed3 20 6f 74 68 65 72 20 6d 61 63 72 6f 73 20 64 65   other macros de
27ed4 66 69 6e 65 64 20 61 62 6f 76 65 20 61 72 65 20  fined above are 
27ed5 75 6e 64 65 66 69 6e 65 64 2e 20 4f 74 68 65 72  undefined. Other
27ed6 77 69 73 65 0a 2a 2a 20 74 68 65 6c 79 20 6d 61  wise.** thely ma
27ed7 79 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68  y interfere with
27ed8 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20   compilation of 
27ed9 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  other functions 
27eda 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20  in this file.** 
27edb 28 6f 72 20 69 6e 20 61 6e 6f 74 68 65 72 20 66  (or in another f
27edc 69 6c 65 2c 20 69 66 20 74 68 69 73 20 66 69 6c  ile, if this fil
27edd 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f  e becomes part o
27ede 66 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69  f the amalgamati
27edf 6f 6e 29 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20  on).  */.#ifdef 
27ee0 69 73 56 69 65 77 0a 20 23 75 6e 64 65 66 20 69  isView. #undef i
27ee1 73 56 69 65 77 0a 23 65 6e 64 69 66 0a 23 69 66  sView.#endif.#if
27ee2 64 65 66 20 70 54 72 69 67 67 65 72 0a 20 23 75  def pTrigger. #u
27ee3 6e 64 65 66 20 70 54 72 69 67 67 65 72 0a 23 65  ndef pTrigger.#e
27ee4 6e 64 69 66 0a 23 69 66 64 65 66 20 74 6d 61 73  ndif.#ifdef tmas
27ee5 6b 0a 20 23 75 6e 64 65 66 20 74 6d 61 73 6b 0a  k. #undef tmask.
27ee6 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 47  #endif.../*.** G
27ee7 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
27ee8 64 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68  do constraint ch
27ee9 65 63 6b 73 20 70 72 69 6f 72 20 74 6f 20 61 6e  ecks prior to an
27eea 20 49 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50   INSERT or an UP
27eeb 44 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  DATE..**.** The 
27eec 69 6e 70 75 74 20 69 73 20 61 20 72 61 6e 67 65  input is a range
27eed 20 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20   of consecutive 
27eee 72 65 67 69 73 74 65 72 73 20 61 73 20 66 6f 6c  registers as fol
27eef 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31  lows:.**.**    1
27ef0 2e 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20  .  The rowid of 
27ef1 74 68 65 20 72 6f 77 20 61 66 74 65 72 20 74 68  the row after th
27ef2 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  e update..**.** 
27ef3 20 20 20 32 2e 20 20 54 68 65 20 64 61 74 61 20     2.  The data 
27ef4 69 6e 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c  in the first col
27ef5 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79  umn of the entry
27ef6 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74   after the updat
27ef7 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20  e..**.**    i.  
27ef8 44 61 74 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65  Data from middle
27ef9 20 63 6f 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a   columns....**.*
27efa 2a 20 20 20 20 4e 2e 20 20 54 68 65 20 64 61 74  *    N.  The dat
27efb 61 20 69 6e 20 74 68 65 20 6c 61 73 74 20 63 6f  a in the last co
27efc 6c 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72  lumn of the entr
27efd 79 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61  y after the upda
27efe 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  te..**.** The re
27eff 67 52 6f 77 69 64 20 70 61 72 61 6d 65 74 65 72  gRowid parameter
27f00 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
27f01 20 74 68 65 20 72 65 67 69 73 74 65 72 20 63 6f   the register co
27f02 6e 74 61 69 6e 69 6e 67 20 28 31 29 2e 0a 2a 2a  ntaining (1)..**
27f03 0a 2a 2a 20 49 66 20 69 73 55 70 64 61 74 65 20  .** If isUpdate 
27f04 69 73 20 74 72 75 65 20 61 6e 64 20 72 6f 77 69  is true and rowi
27f05 64 43 68 6e 67 20 69 73 20 6e 6f 6e 2d 7a 65 72  dChng is non-zer
27f06 6f 2c 20 74 68 65 6e 20 72 6f 77 69 64 43 68 6e  o, then rowidChn
27f07 67 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68  g contains.** th
27f08 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20 72  e address of a r
27f09 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69  egister containi
27f0a 6e 67 20 74 68 65 20 72 6f 77 69 64 20 62 65 66  ng the rowid bef
27f0b 6f 72 65 20 74 68 65 20 75 70 64 61 74 65 20 74  ore the update t
27f0c 61 6b 65 73 0a 2a 2a 20 70 6c 61 63 65 2e 20 69  akes.** place. i
27f0d 73 55 70 64 61 74 65 20 69 73 20 74 72 75 65 20  sUpdate is true 
27f0e 66 6f 72 20 55 50 44 41 54 45 73 20 61 6e 64 20  for UPDATEs and 
27f0f 66 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54  false for INSERT
27f10 73 2e 20 49 66 20 69 73 55 70 64 61 74 65 0a 2a  s. If isUpdate.*
27f11 2a 20 69 73 20 66 61 6c 73 65 2c 20 69 6e 64 69  * is false, indi
27f12 63 61 74 69 6e 67 20 61 6e 20 49 4e 53 45 52 54  cating an INSERT
27f13 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e   statement, then
27f14 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 6f 77 69   a non-zero rowi
27f15 64 43 68 6e 67 20 0a 2a 2a 20 69 6e 64 69 63 61  dChng .** indica
27f16 74 65 73 20 74 68 61 74 20 74 68 65 20 72 6f 77  tes that the row
27f17 69 64 20 77 61 73 20 65 78 70 6c 69 63 69 74 6c  id was explicitl
27f18 79 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70  y specified as p
27f19 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e  art of the.** IN
27f1a 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  SERT statement. 
27f1b 49 66 20 72 6f 77 69 64 43 68 6e 67 20 69 73 20  If rowidChng is 
27f1c 66 61 6c 73 65 2c 20 69 74 20 6d 65 61 6e 73 20  false, it means 
27f1d 74 68 61 74 20 20 74 68 65 20 72 6f 77 69 64 20  that  the rowid 
27f1e 69 73 0a 2a 2a 20 63 6f 6d 70 75 74 65 64 20 61  is.** computed a
27f1f 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20  utomatically in 
27f20 61 6e 20 69 6e 73 65 72 74 20 6f 72 20 74 68 61  an insert or tha
27f21 74 20 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75  t the rowid valu
27f22 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 6d 6f 64  e is not .** mod
27f23 69 66 69 65 64 20 62 79 20 61 6e 20 75 70 64 61  ified by an upda
27f24 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  te..**.** The co
27f25 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  de generated by 
27f26 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f  this routine sto
27f27 72 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74  re new index ent
27f28 72 69 65 73 20 69 6e 74 6f 0a 2a 2a 20 72 65 67  ries into.** reg
27f29 69 73 74 65 72 73 20 69 64 65 6e 74 69 66 69 65  isters identifie
27f2a 64 20 62 79 20 61 52 65 67 49 64 78 5b 5d 2e 20  d by aRegIdx[]. 
27f2b 20 4e 6f 20 69 6e 64 65 78 20 65 6e 74 72 79 20   No index entry 
27f2c 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 0a 2a  is created for.*
27f2d 2a 20 69 6e 64 69 63 65 73 20 77 68 65 72 65 20  * indices where 
27f2e 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30 2e 20 20  aRegIdx[i]==0.  
27f2f 54 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64  The order of ind
27f30 69 63 65 73 20 69 6e 20 61 52 65 67 49 64 78 5b  ices in aRegIdx[
27f31 5d 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65  ] is.** the same
27f32 20 61 73 20 74 68 65 20 6f 72 64 65 72 20 6f 66   as the order of
27f33 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20   indices on the 
27f34 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 69  linked list of i
27f35 6e 64 69 63 65 73 0a 2a 2a 20 61 74 74 61 63 68  ndices.** attach
27f36 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e  ed to the table.
27f37 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
27f38 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65 72 61 74  ine also generat
27f39 65 73 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b  es code to check
27f3a 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 4e   constraints.  N
27f3b 4f 54 20 4e 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43  OT NULL,.** CHEC
27f3c 4b 2c 20 61 6e 64 20 55 4e 49 51 55 45 20 63 6f  K, and UNIQUE co
27f3d 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c  nstraints are al
27f3e 6c 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20 61  l checked.  If a
27f3f 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
27f40 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 61  s,.** then the a
27f41 70 70 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f  ppropriate actio
27f42 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20  n is performed. 
27f43 20 54 68 65 72 65 20 61 72 65 20 66 69 76 65 20   There are five 
27f44 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 61 63 74 69  possible.** acti
27f45 6f 6e 73 3a 20 52 4f 4c 4c 42 41 43 4b 2c 20 41  ons: ROLLBACK, A
27f46 42 4f 52 54 2c 20 46 41 49 4c 2c 20 52 45 50 4c  BORT, FAIL, REPL
27f47 41 43 45 2c 20 61 6e 64 20 49 47 4e 4f 52 45 2e  ACE, and IGNORE.
27f48 0a 2a 2a 0a 2a 2a 20 20 43 6f 6e 73 74 72 61 69  .**.**  Constrai
27f49 6e 74 20 74 79 70 65 20 20 41 63 74 69 6f 6e 20  nt type  Action 
27f4a 20 20 20 20 20 20 57 68 61 74 20 48 61 70 70 65        What Happe
27f4b 6e 73 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ns.**  ---------
27f4c 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ------  --------
27f4d 2d 2d 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --   -----------
27f4e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27f4f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
27f50 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
27f51 20 20 20 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20     ROLLBACK     
27f52 54 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  The current tran
27f53 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
27f54 64 20 62 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20  d back and.**   
27f55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f56 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
27f57 69 74 65 33 5f 65 78 65 63 28 29 20 72 65 74 75  ite3_exec() retu
27f58 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  rns immediately 
27f59 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20 20  with a.**       
27f5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f5b 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
27f5c 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 43  code of SQLITE_C
27f5d 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a  ONSTRAINT..**.**
27f5e 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
27f5f 20 20 20 41 42 4f 52 54 20 20 20 20 20 20 20 20     ABORT        
27f60 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73  Back out changes
27f61 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e   from the curren
27f62 74 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20  t command.**    
27f63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f64 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e 6c 79              only
27f65 20 28 64 6f 20 6e 6f 74 20 64 6f 20 61 20 63 6f   (do not do a co
27f66 6d 70 6c 65 74 65 20 72 6f 6c 6c 62 61 63 6b 29  mplete rollback)
27f67 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20   then.**        
27f68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f69 20 20 20 20 20 20 20 20 63 61 75 73 65 20 73 71          cause sq
27f6a 6c 69 74 65 33 5f 65 78 65 63 28 29 20 74 6f 20  lite3_exec() to 
27f6b 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65  return immediate
27f6c 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ly.**           
27f6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f6e 20 20 20 20 20 77 69 74 68 20 53 51 4c 49 54 45       with SQLITE
27f6f 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a  _CONSTRAINT..**.
27f70 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20  **  any         
27f71 20 20 20 20 20 46 41 49 4c 20 20 20 20 20 20 20       FAIL       
27f72 20 20 53 71 6c 69 74 65 5f 65 78 65 63 28 29 20    Sqlite_exec() 
27f73 72 65 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74  returns immediat
27f74 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20  ely with a.**   
27f75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f76 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
27f77 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49  urn code of SQLI
27f78 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 20 20  TE_CONSTRAINT.  
27f79 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  The.**          
27f7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f7b 20 20 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f        transactio
27f7c 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20  n is not rolled 
27f7d 62 61 63 6b 20 61 6e 64 20 61 6e 79 0a 2a 2a 20  back and any.** 
27f7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
27f80 72 69 6f 72 20 63 68 61 6e 67 65 73 20 61 72 65  rior changes are
27f81 20 72 65 74 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a   retained..**.**
27f82 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
27f83 20 20 20 49 47 4e 4f 52 45 20 20 20 20 20 20 20     IGNORE       
27f84 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  The record numbe
27f85 72 20 61 6e 64 20 64 61 74 61 20 69 73 20 70 6f  r and data is po
27f86 70 70 65 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20  pped from.**    
27f87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f88 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
27f89 73 74 61 63 6b 20 61 6e 64 20 74 68 65 72 65 20  stack and there 
27f8a 69 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20  is an immediate 
27f8b 6a 75 6d 70 0a 2a 2a 20 20 20 20 20 20 20 20 20  jump.**         
27f8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f8d 20 20 20 20 20 20 20 74 6f 20 6c 61 62 65 6c 20         to label 
27f8e 69 67 6e 6f 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a  ignoreDest..**.*
27f8f 2a 20 20 4e 4f 54 20 4e 55 4c 4c 20 20 20 20 20  *  NOT NULL     
27f90 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20 20      REPLACE     
27f91 20 54 68 65 20 4e 55 4c 4c 20 76 61 6c 75 65 20   The NULL value 
27f92 69 73 20 72 65 70 6c 61 63 65 20 62 79 20 74 68  is replace by th
27f93 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20  e default.**    
27f94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f95 20 20 20 20 20 20 20 20 20 20 20 20 76 61 6c 75              valu
27f96 65 20 66 6f 72 20 74 68 61 74 20 63 6f 6c 75 6d  e for that colum
27f97 6e 2e 20 20 49 66 20 74 68 65 20 64 65 66 61 75  n.  If the defau
27f98 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20  lt value.**     
27f99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f9a 20 20 20 20 20 20 20 20 20 20 20 69 73 20 4e 55             is NU
27f9b 4c 4c 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69  LL, the action i
27f9c 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42  s the same as AB
27f9d 4f 52 54 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51  ORT..**.**  UNIQ
27f9e 55 45 20 20 20 20 20 20 20 20 20 20 20 52 45 50  UE           REP
27f9f 4c 41 43 45 20 20 20 20 20 20 54 68 65 20 6f 74  LACE      The ot
27fa0 68 65 72 20 72 6f 77 20 74 68 61 74 20 63 6f 6e  her row that con
27fa1 66 6c 69 63 74 73 20 77 69 74 68 20 74 68 65 20  flicts with the 
27fa2 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  row.**          
27fa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27fa4 20 20 20 20 20 20 62 65 69 6e 67 20 69 6e 73 65        being inse
27fa5 72 74 65 64 20 69 73 20 72 65 6d 6f 76 65 64 2e  rted is removed.
27fa6 0a 2a 2a 0a 2a 2a 20 20 43 48 45 43 4b 20 20 20  .**.**  CHECK   
27fa7 20 20 20 20 20 20 20 20 20 52 45 50 4c 41 43 45           REPLACE
27fa8 20 20 20 20 20 20 49 6c 6c 65 67 61 6c 2e 20 20        Illegal.  
27fa9 54 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61  The results in a
27faa 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a  n exception..**.
27fab 2a 2a 20 57 68 69 63 68 20 61 63 74 69 6f 6e 20  ** Which action 
27fac 74 6f 20 74 61 6b 65 20 69 73 20 64 65 74 65 72  to take is deter
27fad 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6f 76 65  mined by the ove
27fae 72 72 69 64 65 45 72 72 6f 72 20 70 61 72 61 6d  rrideError param
27faf 65 74 65 72 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f  eter..** Or if o
27fb0 76 65 72 72 69 64 65 45 72 72 6f 72 3d 3d 4f 45  verrideError==OE
27fb1 5f 44 65 66 61 75 6c 74 2c 20 74 68 65 6e 20 74  _Default, then t
27fb2 68 65 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72  he pParse->onErr
27fb3 6f 72 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  or parameter.** 
27fb4 69 73 20 75 73 65 64 2e 20 20 4f 72 20 69 66 20  is used.  Or if 
27fb5 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d  pParse->onError=
27fb6 3d 4f 45 5f 44 65 66 61 75 6c 74 20 74 68 65 6e  =OE_Default then
27fb7 20 74 68 65 20 6f 6e 45 72 72 6f 72 20 76 61 6c   the onError val
27fb8 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f  ue.** for the co
27fb9 6e 73 74 72 61 69 6e 74 20 69 73 20 75 73 65 64  nstraint is used
27fba 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
27fbb 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  ing routine must
27fbc 20 6f 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69   open a read/wri
27fbd 74 65 20 63 75 72 73 6f 72 20 66 6f 72 20 70 54  te cursor for pT
27fbe 61 62 20 77 69 74 68 0a 2a 2a 20 63 75 72 73 6f  ab with.** curso
27fbf 72 20 6e 75 6d 62 65 72 20 22 62 61 73 65 43 75  r number "baseCu
27fc0 72 22 2e 20 20 41 6c 6c 20 69 6e 64 69 63 65 73  r".  All indices
27fc1 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20 61 6c   of pTab must al
27fc2 73 6f 20 68 61 76 65 20 6f 70 65 6e 0a 2a 2a 20  so have open.** 
27fc3 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
27fc4 72 73 20 77 69 74 68 20 63 75 72 73 6f 72 20 6e  rs with cursor n
27fc5 75 6d 62 65 72 20 62 61 73 65 43 75 72 2b 69 20  umber baseCur+i 
27fc6 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 75 72  for the i-th cur
27fc7 73 6f 72 2e 0a 2a 2a 20 45 78 63 65 70 74 2c 20  sor..** Except, 
27fc8 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70  if there is no p
27fc9 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 61 20  ossibility of a 
27fca 52 45 50 4c 41 43 45 20 61 63 74 69 6f 6e 20 74  REPLACE action t
27fcb 68 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 64  hen.** cursors d
27fcc 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  o not need to be
27fcd 20 6f 70 65 6e 20 66 6f 72 20 69 6e 64 69 63 65   open for indice
27fce 73 20 77 68 65 72 65 20 61 52 65 67 49 64 78 5b  s where aRegIdx[
27fcf 69 5d 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45  i]==0..*/.SQLITE
27fd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
27fd1 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e  lite3GenerateCon
27fd2 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 0a 20  straintChecks(. 
27fd3 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
27fd4 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
27fd5 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
27fd6 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
27fd7 20 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c 65      /* the table
27fd8 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61   into which we a
27fd9 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a  re inserting */.
27fda 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20    int baseCur,  
27fdb 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
27fdc 66 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63  f a read/write c
27fdd 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61  ursor pointing a
27fde 74 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20  t pTab */.  int 
27fdf 72 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20  regRowid,       
27fe0 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20  /* Index of the 
27fe1 72 61 6e 67 65 20 6f 66 20 69 6e 70 75 74 20 72  range of input r
27fe2 65 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e  egisters */.  in
27fe3 74 20 2a 61 52 65 67 49 64 78 2c 20 20 20 20 20  t *aRegIdx,     
27fe4 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73    /* Register us
27fe5 65 64 20 62 79 20 65 61 63 68 20 69 6e 64 65 78  ed by each index
27fe6 2e 20 20 30 20 66 6f 72 20 75 6e 75 73 65 64 20  .  0 for unused 
27fe7 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74  indices */.  int
27fe8 20 72 6f 77 69 64 43 68 6e 67 2c 20 20 20 20 20   rowidChng,     
27fe9 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
27fea 72 6f 77 69 64 20 6d 69 67 68 74 20 63 6f 6c 6c  rowid might coll
27feb 69 64 65 20 77 69 74 68 20 65 78 69 73 74 69 6e  ide with existin
27fec 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74  g entry */.  int
27fed 20 69 73 55 70 64 61 74 65 2c 20 20 20 20 20 20   isUpdate,      
27fee 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44   /* True for UPD
27fef 41 54 45 2c 20 46 61 6c 73 65 20 66 6f 72 20 49  ATE, False for I
27ff0 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6f  NSERT */.  int o
27ff1 76 65 72 72 69 64 65 45 72 72 6f 72 2c 20 20 2f  verrideError,  /
27ff2 2a 20 4f 76 65 72 72 69 64 65 20 6f 6e 45 72 72  * Override onErr
27ff3 6f 72 20 74 6f 20 74 68 69 73 20 69 66 20 6e 6f  or to this if no
27ff4 74 20 4f 45 5f 44 65 66 61 75 6c 74 20 2a 2f 0a  t OE_Default */.
27ff5 20 20 69 6e 74 20 69 67 6e 6f 72 65 44 65 73 74    int ignoreDest
27ff6 2c 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  ,     /* Jump to
27ff7 20 74 68 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61   this label on a
27ff8 6e 20 4f 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f  n OE_Ignore reso
27ff9 6c 75 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  lution */.  int 
27ffa 2a 70 62 4d 61 79 52 65 70 6c 61 63 65 20 20 20  *pbMayReplace   
27ffb 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74  /* OUT: Set to t
27ffc 72 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e  rue if constrain
27ffd 74 20 6d 61 79 20 63 61 75 73 65 20 61 20 72 65  t may cause a re
27ffe 70 6c 61 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  place */.){.  in
27fff 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
28000 20 20 2f 2a 20 6c 6f 6f 70 20 63 6f 75 6e 74 65    /* loop counte
28001 72 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  r */.  Vdbe *v; 
28002 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44             /* VD
28003 42 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  BE under constru
28004 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  tion */.  int nC
28005 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ol;           /*
28006 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
28007 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72  ns */.  int onEr
28008 72 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  ror;        /* C
28009 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69  onflict resoluti
2800a 6f 6e 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20  on strategy */. 
2800b 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20   int j1;        
2800c 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 73       /* Addresss
2800d 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63   of jump instruc
2800e 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 32  tion */.  int j2
2800f 20 3d 20 30 2c 20 6a 33 3b 20 20 20 20 20 2f 2a   = 0, j3;     /*
28010 20 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a 75   Addresses of ju
28011 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  mp instructions 
28012 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
28013 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
28014 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
28015 66 69 72 73 74 20 64 61 74 61 20 63 6f 6c 75 6d  first data colum
28016 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  n */.  int iCur;
28017 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
28018 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ble cursor numbe
28019 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  r */.  Index *pI
2801a 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50  dx;         /* P
2801b 6f 69 6e 74 65 72 20 74 6f 20 6f 6e 65 20 6f 66  ointer to one of
2801c 20 74 68 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a   the indices */.
2801d 20 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63    int seenReplac
2801e 65 20 3d 20 30 3b 20 2f 2a 20 54 72 75 65 20 69  e = 0; /* True i
2801f 66 20 52 45 50 4c 41 43 45 20 69 73 20 75 73 65  f REPLACE is use
28020 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 49 4e 54  d to resolve INT
28021 20 50 4b 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a   PK conflict */.
28022 20 20 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69    int regOldRowi
28023 64 20 3d 20 28 72 6f 77 69 64 43 68 6e 67 20 26  d = (rowidChng &
28024 26 20 69 73 55 70 64 61 74 65 29 20 3f 20 72 6f  & isUpdate) ? ro
28025 77 69 64 43 68 6e 67 20 3a 20 72 65 67 52 6f 77  widChng : regRow
28026 69 64 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74  id;..  v = sqlit
28027 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
28028 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  );.  assert( v!=
28029 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2802a 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  Tab->pSelect==0 
2802b 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62 6c  );  /* This tabl
2802c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57 20  e is not a VIEW 
2802d 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61 62  */.  nCol = pTab
2802e 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61 74  ->nCol;.  regDat
2802f 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31  a = regRowid + 1
28030 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c  ;..  /* Test all
28031 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
28032 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f  aints..  */.  fo
28033 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
28034 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d  ++){.    if( i==
28035 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
28036 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
28037 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72 72 6f 72     }.    onError
28038 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d   = pTab->aCol[i]
28039 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20 69 66  .notNull;.    if
2803a 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f  ( onError==OE_No
2803b 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ne ) continue;. 
2803c 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 45     if( overrideE
2803d 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74  rror!=OE_Default
2803e 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f   ){.      onErro
2803f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f  r = overrideErro
28040 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  r;.    }else if(
28041 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66   onError==OE_Def
28042 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e  ault ){.      on
28043 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74  Error = OE_Abort
28044 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
28045 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
28046 61 63 65 20 26 26 20 70 54 61 62 2d 3e 61 43 6f  ace && pTab->aCo
28047 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20 29 7b  l[i].pDflt==0 ){
28048 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
28049 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d   OE_Abort;.    }
2804a 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45  .    assert( onE
2804b 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63  rror==OE_Rollbac
2804c 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  k || onError==OE
2804d 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f  _Abort || onErro
2804e 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20  r==OE_Fail.     
2804f 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f     || onError==O
28050 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72  E_Ignore || onEr
28051 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror==OE_Replace 
28052 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f  );.    switch( o
28053 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20  nError ){.      
28054 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20  case OE_Abort:. 
28055 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61         sqlite3Ma
28056 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a  yAbort(pParse);.
28057 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f        case OE_Ro
28058 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61  llback:.      ca
28059 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20  se OE_Fail: {.  
2805a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67        char *zMsg
2805b 3b 0a 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73  ;.        j1 = s
2805c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2805d 28 76 2c 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c  (v, OP_HaltIfNul
2805e 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
2805f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28060 20 20 20 20 20 53 51 4c 49 54 45 5f 43 4f 4e 53       SQLITE_CONS
28061 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c  TRAINT, onError,
28062 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20   regData+i);.   
28063 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
28064 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73  te3MPrintf(pPars
28065 65 2d 3e 64 62 2c 20 22 25 73 2e 25 73 20 6d 61  e->db, "%s.%s ma
28066 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a  y not be NULL",.
28067 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28068 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
28069 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  ab->zName, pTab-
2806a 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b  >aCol[i].zName);
2806b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2806c 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2806d 2d 31 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e  -1, zMsg, P4_DYN
2806e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62  AMIC);.        b
2806f 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
28070 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f      case OE_Igno
28071 72 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  re: {.        sq
28072 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
28073 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
28074 67 44 61 74 61 2b 69 2c 20 69 67 6e 6f 72 65 44  gData+i, ignoreD
28075 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72  est);.        br
28076 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
28077 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
28078 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e        assert( on
28079 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
2807a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 6a 31 20  e );.        j1 
2807b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2807c 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  Op1(v, OP_NotNul
2807d 6c 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20  l, regData+i);. 
2807e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2807f 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
28080 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66  Tab->aCol[i].pDf
28081 6c 74 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a  lt, regData+i);.
28082 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28083 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
28084 31 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  1);.        brea
28085 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
28086 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20  .  }..  /* Test 
28087 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  all CHECK constr
28088 61 69 6e 74 73 0a 20 20 2a 2f 0a 23 69 66 6e 64  aints.  */.#ifnd
28089 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
2808a 48 45 43 4b 0a 20 20 69 66 28 20 70 54 61 62 2d  HECK.  if( pTab-
2808b 3e 70 43 68 65 63 6b 20 26 26 20 28 70 50 61 72  >pCheck && (pPar
2808c 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  se->db->flags & 
2808d 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65  SQLITE_IgnoreChe
2808e 63 6b 73 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  cks)==0 ){.    i
2808f 6e 74 20 61 6c 6c 4f 6b 20 3d 20 73 71 6c 69 74  nt allOk = sqlit
28090 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
28091 76 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  v);.    pParse->
28092 63 6b 42 61 73 65 20 3d 20 72 65 67 44 61 74 61  ckBase = regData
28093 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
28094 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
28095 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 61 6c  pTab->pCheck, al
28096 6c 4f 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  lOk, SQLITE_JUMP
28097 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 6f 6e 45  IFNULL);.    onE
28098 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45  rror = overrideE
28099 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74  rror!=OE_Default
2809a 20 3f 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72   ? overrideError
2809b 20 3a 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   : OE_Abort;.   
2809c 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
2809d 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20  _Ignore ){.     
2809e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2809f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
280a0 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20  , ignoreDest);. 
280a1 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
280a2 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74  sqlite3HaltConst
280a3 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20 6f 6e  raint(pParse, on
280a4 45 72 72 6f 72 2c 20 30 2c 20 30 29 3b 0a 20 20  Error, 0, 0);.  
280a5 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
280a6 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
280a7 76 2c 20 61 6c 6c 4f 6b 29 3b 0a 20 20 7d 0a 23  v, allOk);.  }.#
280a8 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
280a9 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  d(SQLITE_OMIT_CH
280aa 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  ECK) */..  /* If
280ab 20 77 65 20 68 61 76 65 20 61 6e 20 49 4e 54 45   we have an INTE
280ac 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
280ad 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70   make sure the p
280ae 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20  rimary key.  ** 
280af 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  of the new recor
280b0 64 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69  d does not previ
280b1 6f 75 73 6c 79 20 65 78 69 73 74 2e 20 20 45 78  ously exist.  Ex
280b2 63 65 70 74 2c 20 69 66 20 74 68 69 73 0a 20 20  cept, if this.  
280b3 2a 2a 20 69 73 20 61 6e 20 55 50 44 41 54 45 20  ** is an UPDATE 
280b4 61 6e 64 20 74 68 65 20 70 72 69 6d 61 72 79 20  and the primary 
280b5 6b 65 79 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  key is not chang
280b6 69 6e 67 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e  ing, that is OK.
280b7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 6f 77 69  .  */.  if( rowi
280b8 64 43 68 6e 67 20 29 7b 0a 20 20 20 20 6f 6e 45  dChng ){.    onE
280b9 72 72 6f 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79  rror = pTab->key
280ba 43 6f 6e 66 3b 0a 20 20 20 20 69 66 28 20 6f 76  Conf;.    if( ov
280bb 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f  errideError!=OE_
280bc 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20  Default ){.     
280bd 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72   onError = overr
280be 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65  ideError;.    }e
280bf 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d  lse if( onError=
280c0 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
280c1 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f       onError = O
280c2 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
280c3 20 20 20 0a 20 20 20 20 69 66 28 20 69 73 55 70     .    if( isUp
280c4 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 6a 32  date ){.      j2
280c5 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
280c6 64 4f 70 33 28 76 2c 20 4f 50 5f 45 71 2c 20 72  dOp3(v, OP_Eq, r
280c7 65 67 52 6f 77 69 64 2c 20 30 2c 20 72 6f 77 69  egRowid, 0, rowi
280c8 64 43 68 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20  dChng);.    }.  
280c9 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64    j3 = sqlite3Vd
280ca 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
280cb 6f 74 45 78 69 73 74 73 2c 20 62 61 73 65 43 75  otExists, baseCu
280cc 72 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 29 3b  r, 0, regRowid);
280cd 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45  .    switch( onE
280ce 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 64 65  rror ){.      de
280cf 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
280d0 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
280d1 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ort;.        /* 
280d2 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74  Fall thru into t
280d3 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a  he next case */.
280d4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
280d5 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a  se OE_Rollback:.
280d6 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62        case OE_Ab
280d7 6f 72 74 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ort:.      case 
280d8 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20  OE_Fail: {.     
280d9 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f     sqlite3HaltCo
280da 6e 73 74 72 61 69 6e 74 28 0a 20 20 20 20 20 20  nstraint(.      
280db 20 20 20 20 70 50 61 72 73 65 2c 20 6f 6e 45 72      pParse, onEr
280dc 72 6f 72 2c 20 22 50 52 49 4d 41 52 59 20 4b 45  ror, "PRIMARY KE
280dd 59 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65  Y must be unique
280de 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
280df 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
280e0 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
280e1 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a 20   OE_Replace: {. 
280e2 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
280e3 72 65 20 61 72 65 20 44 45 4c 45 54 45 20 74 72  re are DELETE tr
280e4 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74  iggers on this t
280e5 61 62 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 20  able and the.   
280e6 20 20 20 20 20 2a 2a 20 72 65 63 75 72 73 69 76       ** recursiv
280e7 65 2d 74 72 69 67 67 65 72 73 20 66 6c 61 67 20  e-triggers flag 
280e8 69 73 20 73 65 74 2c 20 63 61 6c 6c 20 47 65 6e  is set, call Gen
280e9 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 29  erateRowDelete()
280ea 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 72   to.        ** r
280eb 65 6d 6f 76 65 20 74 68 65 20 63 6f 6e 66 6c 69  emove the confli
280ec 63 74 69 6e 67 20 72 6f 77 20 66 72 6f 6d 20 74  cting row from t
280ed 68 65 20 74 68 65 20 74 61 62 6c 65 2e 20 54 68  he the table. Th
280ee 69 73 20 77 69 6c 6c 20 66 69 72 65 0a 20 20 20  is will fire.   
280ef 20 20 20 20 20 2a 2a 20 74 68 65 20 74 72 69 67       ** the trig
280f0 67 65 72 73 20 61 6e 64 20 72 65 6d 6f 76 65 20  gers and remove 
280f1 62 6f 74 68 20 74 68 65 20 74 61 62 6c 65 20 61  both the table a
280f2 6e 64 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20  nd index b-tree 
280f3 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20  entries..       
280f4 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f   **.        ** O
280f5 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
280f6 72 65 20 61 72 65 20 6e 6f 20 74 72 69 67 67 65  re are no trigge
280f7 72 73 20 6f 72 20 74 68 65 20 72 65 63 75 72 73  rs or the recurs
280f8 69 76 65 2d 74 72 69 67 67 65 72 73 0a 20 20 20  ive-triggers.   
280f9 20 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20       ** flag is 
280fa 6e 6f 74 20 73 65 74 2c 20 63 61 6c 6c 20 47 65  not set, call Ge
280fb 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65  nerateRowIndexDe
280fc 6c 65 74 65 28 29 2e 20 54 68 69 73 20 72 65 6d  lete(). This rem
280fd 6f 76 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  oves.        ** 
280fe 74 68 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65  the index b-tree
280ff 20 65 6e 74 72 69 65 73 20 6f 6e 6c 79 2e 20 54   entries only. T
28100 68 65 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20  he table b-tree 
28101 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 0a 20  entry will be . 
28102 20 20 20 20 20 20 20 2a 2a 20 72 65 70 6c 61 63         ** replac
28103 65 64 20 62 79 20 74 68 65 20 6e 65 77 20 65 6e  ed by the new en
28104 74 72 79 20 77 68 65 6e 20 69 74 20 69 73 20 69  try when it is i
28105 6e 73 65 72 74 65 64 2e 20 20 2a 2f 0a 20 20 20  nserted.  */.   
28106 20 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 54       Trigger *pT
28107 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 20 20  rigger = 0;.    
28108 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
28109 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
2810a 5f 52 65 63 54 72 69 67 67 65 72 73 20 29 7b 0a  _RecTriggers ){.
2810b 20 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67            pTrigg
2810c 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  er = sqlite3Trig
2810d 67 65 72 73 45 78 69 73 74 28 70 50 61 72 73 65  gersExist(pParse
2810e 2c 20 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54  , pTab, TK_DELET
2810f 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  E, 0, 0);.      
28110 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
28111 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 70 50  te3MultiWrite(pP
28112 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 69  arse);.        i
28113 66 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20 73  f( pTrigger || s
28114 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64  qlite3FkRequired
28115 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30  (pParse, pTab, 0
28116 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  , 0) ){.        
28117 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74    sqlite3Generat
28118 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20 20 20  eRowDelete(.    
28119 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
2811a 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c  , pTab, baseCur,
2811b 20 72 65 67 52 6f 77 69 64 2c 20 30 2c 20 70 54   regRowid, 0, pT
2811c 72 69 67 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61  rigger, OE_Repla
2811d 63 65 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a  ce.          );.
2811e 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2811f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28120 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
28121 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70  Delete(pParse, p
28122 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 30 29  Tab, baseCur, 0)
28123 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
28124 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65 20      seenReplace 
28125 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = 1;.        bre
28126 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
28127 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65    case OE_Ignore
28128 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  : {.        asse
28129 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65 3d  rt( seenReplace=
2812a 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  =0 );.        sq
2812b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2812c 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
2812d 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20  gnoreDest);.    
2812e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2812f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
28130 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
28131 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 69 66 28  (v, j3);.    if(
28132 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20 20   isUpdate ){.   
28133 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
28134 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20  mpHere(v, j2);. 
28135 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
28136 65 73 74 20 61 6c 6c 20 55 4e 49 51 55 45 20 63  est all UNIQUE c
28137 6f 6e 73 74 72 61 69 6e 74 73 20 62 79 20 63 72  onstraints by cr
28138 65 61 74 69 6e 67 20 65 6e 74 72 69 65 73 20 66  eating entries f
28139 6f 72 20 65 61 63 68 20 55 4e 49 51 55 45 0a 20  or each UNIQUE. 
2813a 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d 61   ** index and ma
2813b 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 64  king sure that d
2813c 75 70 6c 69 63 61 74 65 20 65 6e 74 72 69 65 73  uplicate entries
2813d 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   do not already 
2813e 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41 64 64 20  exist..  ** Add 
2813f 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 73 20  the new records 
28140 74 6f 20 74 68 65 20 69 6e 64 69 63 65 73 20 61  to the indices a
28141 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20 20  s we go..  */.  
28142 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64 78  for(iCur=0, pIdx
28143 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
28144 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
28145 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b 0a  pNext, iCur++){.
28146 20 20 20 20 69 6e 74 20 72 65 67 49 64 78 3b 0a      int regIdx;.
28147 20 20 20 20 69 6e 74 20 72 65 67 52 3b 0a 0a 20      int regR;.. 
28148 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b 69     if( aRegIdx[i
28149 43 75 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  Cur]==0 ) contin
2814a 75 65 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e 75  ue;  /* Skip unu
2814b 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a  sed indices */..
2814c 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20      /* Create a 
2814d 6b 65 79 20 66 6f 72 20 61 63 63 65 73 73 69 6e  key for accessin
2814e 67 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  g the index entr
2814f 79 20 2a 2f 0a 20 20 20 20 72 65 67 49 64 78 20  y */.    regIdx 
28150 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
28151 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49  Range(pParse, pI
28152 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a  dx->nColumn+1);.
28153 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
28154 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
28155 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 64  +){.      int id
28156 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  x = pIdx->aiColu
28157 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  mn[i];.      if(
28158 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b 65   idx==pTab->iPKe
28159 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  y ){.        sql
2815a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2815b 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 52  , OP_SCopy, regR
2815c 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29 3b  owid, regIdx+i);
2815d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2815e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2815f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
28160 6f 70 79 2c 20 72 65 67 44 61 74 61 2b 69 64 78  opy, regData+idx
28161 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20  , regIdx+i);.   
28162 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
28163 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28164 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65  (v, OP_SCopy, re
28165 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69  gRowid, regIdx+i
28166 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
28167 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
28168 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 49 64  akeRecord, regId
28169 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  x, pIdx->nColumn
2816a 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 43 75 72  +1, aRegIdx[iCur
2816b 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ]);.    sqlite3V
2816c 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
2816d 31 2c 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41  1, sqlite3IndexA
2816e 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49  ffinityStr(v, pI
2816f 64 78 29 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  dx), 0);.    sql
28170 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
28171 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
28172 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78  se, regIdx, pIdx
28173 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20  ->nColumn+1);.. 
28174 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77     /* Find out w
28175 68 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61  hat action to ta
28176 6b 65 20 69 6e 20 63 61 73 65 20 74 68 65 72 65  ke in case there
28177 20 69 73 20 61 6e 20 69 6e 64 65 78 69 6e 67 20   is an indexing 
28178 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 20 20  conflict */.    
28179 6f 6e 45 72 72 6f 72 20 3d 20 70 49 64 78 2d 3e  onError = pIdx->
2817a 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 69 66 28  onError;.    if(
2817b 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e   onError==OE_Non
2817c 65 20 29 7b 20 0a 20 20 20 20 20 20 73 71 6c 69  e ){ .      sqli
2817d 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
2817e 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 49  nge(pParse, regI
2817f 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  dx, pIdx->nColum
28180 6e 2b 31 29 3b 0a 20 20 20 20 20 20 63 6f 6e 74  n+1);.      cont
28181 69 6e 75 65 3b 20 20 2f 2a 20 70 49 64 78 20 69  inue;  /* pIdx i
28182 73 20 6e 6f 74 20 61 20 55 4e 49 51 55 45 20 69  s not a UNIQUE i
28183 6e 64 65 78 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  ndex */.    }.  
28184 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 45 72    if( overrideEr
28185 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror!=OE_Default 
28186 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
28187 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72   = overrideError
28188 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
28189 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
2818a 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45  ult ){.      onE
2818b 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
2818c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
2818d 65 65 6e 52 65 70 6c 61 63 65 20 29 7b 0a 20 20  eenReplace ){.  
2818e 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d      if( onError=
2818f 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 20 6f 6e 45  =OE_Ignore ) onE
28190 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63  rror = OE_Replac
28191 65 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66  e;.      else if
28192 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61  ( onError==OE_Fa
28193 69 6c 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f  il ) onError = O
28194 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
28195 20 20 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b     .    /* Check
28196 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 6e   to see if the n
28197 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79 20 77  ew index entry w
28198 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 20 2a 2f  ill be unique */
28199 0a 20 20 20 20 72 65 67 52 20 3d 20 73 71 6c 69  .    regR = sqli
2819a 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2819b 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  arse);.    sqlit
2819c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2819d 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 4f 6c 64  OP_SCopy, regOld
2819e 52 6f 77 69 64 2c 20 72 65 67 52 29 3b 0a 20 20  Rowid, regR);.  
2819f 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64    j3 = sqlite3Vd
281a0 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49  beAddOp4(v, OP_I
281a1 73 55 6e 69 71 75 65 2c 20 62 61 73 65 43 75 72  sUnique, baseCur
281a2 2b 69 43 75 72 2b 31 2c 20 30 2c 0a 20 20 20 20  +iCur+1, 0,.    
281a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
281a4 20 20 20 20 20 20 20 72 65 67 52 2c 20 53 51 4c         regR, SQL
281a5 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72  ITE_INT_TO_PTR(r
281a6 65 67 49 64 78 29 2c 0a 20 20 20 20 20 20 20 20  egIdx),.        
281a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
281a8 20 20 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20     P4_INT32);.  
281a9 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
281aa 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
281ab 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e  , regIdx, pIdx->
281ac 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20  nColumn+1);..   
281ad 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
281ae 65 20 74 68 61 74 20 65 78 65 63 75 74 65 73 20  e that executes 
281af 69 66 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78  if the new index
281b0 20 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 75 6e   entry is not un
281b1 69 71 75 65 20 2a 2f 0a 20 20 20 20 61 73 73 65  ique */.    asse
281b2 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  rt( onError==OE_
281b3 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72  Rollback || onEr
281b4 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c  ror==OE_Abort ||
281b5 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69   onError==OE_Fai
281b6 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45  l.        || onE
281b7 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20  rror==OE_Ignore 
281b8 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  || onError==OE_R
281b9 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73 77  eplace );.    sw
281ba 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b  itch( onError ){
281bb 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52  .      case OE_R
281bc 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63  ollback:.      c
281bd 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20  ase OE_Abort:.  
281be 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c      case OE_Fail
281bf 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  : {.        int 
281c0 6a 3b 0a 20 20 20 20 20 20 20 20 53 74 72 41 63  j;.        StrAc
281c1 63 75 6d 20 65 72 72 4d 73 67 3b 0a 20 20 20 20  cum errMsg;.    
281c2 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
281c3 7a 53 65 70 3b 0a 20 20 20 20 20 20 20 20 63 68  zSep;.        ch
281c4 61 72 20 2a 7a 45 72 72 3b 0a 0a 20 20 20 20 20  ar *zErr;..     
281c5 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
281c6 75 6d 49 6e 69 74 28 26 65 72 72 4d 73 67 2c 20  umInit(&errMsg, 
281c7 30 2c 20 30 2c 20 32 30 30 29 3b 0a 20 20 20 20  0, 0, 200);.    
281c8 20 20 20 20 65 72 72 4d 73 67 2e 64 62 20 3d 20      errMsg.db = 
281c9 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20  pParse->db;.    
281ca 20 20 20 20 7a 53 65 70 20 3d 20 70 49 64 78 2d      zSep = pIdx-
281cb 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 63 6f  >nColumn>1 ? "co
281cc 6c 75 6d 6e 73 20 22 20 3a 20 22 63 6f 6c 75 6d  lumns " : "colum
281cd 6e 20 22 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  n ";.        for
281ce 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43  (j=0; j<pIdx->nC
281cf 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  olumn; j++){.   
281d0 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f         char *zCo
281d1 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70  l = pTab->aCol[p
281d2 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
281d3 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ].zName;.       
281d4 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
281d5 75 6d 41 70 70 65 6e 64 28 26 65 72 72 4d 73 67  umAppend(&errMsg
281d6 2c 20 7a 53 65 70 2c 20 2d 31 29 3b 0a 20 20 20  , zSep, -1);.   
281d7 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 22 2c         zSep = ",
281d8 20 22 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ";.          sq
281d9 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
281da 65 6e 64 28 26 65 72 72 4d 73 67 2c 20 7a 43 6f  end(&errMsg, zCo
281db 6c 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  l, -1);.        
281dc 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
281dd 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
281de 26 65 72 72 4d 73 67 2c 0a 20 20 20 20 20 20 20  &errMsg,.       
281df 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75       pIdx->nColu
281e0 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20 6e 6f 74  mn>1 ? " are not
281e1 20 75 6e 69 71 75 65 22 20 3a 20 22 20 69 73 20   unique" : " is 
281e2 6e 6f 74 20 75 6e 69 71 75 65 22 2c 20 2d 31 29  not unique", -1)
281e3 3b 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d  ;.        zErr =
281e4 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
281e5 46 69 6e 69 73 68 28 26 65 72 72 4d 73 67 29 3b  Finish(&errMsg);
281e6 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
281e7 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 70  HaltConstraint(p
281e8 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20  Parse, onError, 
281e9 7a 45 72 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  zErr, 0);.      
281ea 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
281eb 65 72 72 4d 73 67 2e 64 62 2c 20 7a 45 72 72 29  errMsg.db, zErr)
281ec 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
281ed 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
281ee 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b  ase OE_Ignore: {
281ef 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
281f0 20 73 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20   seenReplace==0 
281f1 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
281f2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
281f3 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f  OP_Goto, 0, igno
281f4 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20  reDest);.       
281f5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
281f6 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
281f7 0a 20 20 20 20 20 20 20 20 54 72 69 67 67 65 72  .        Trigger
281f8 20 2a 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a   *pTrigger = 0;.
281f9 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
281fa 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
281fb 61 63 65 20 29 3b 0a 20 20 20 20 20 20 20 20 73  ace );.        s
281fc 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65  qlite3MultiWrite
281fd 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
281fe 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62    if( pParse->db
281ff 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52  ->flags&SQLITE_R
28200 65 63 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20  ecTriggers ){.  
28201 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72          pTrigger
28202 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
28203 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
28204 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c  pTab, TK_DELETE,
28205 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
28206 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
28207 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
28208 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  te(.            
28209 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
2820a 73 65 43 75 72 2c 20 72 65 67 52 2c 20 30 2c 20  seCur, regR, 0, 
2820b 70 54 72 69 67 67 65 72 2c 20 4f 45 5f 52 65 70  pTrigger, OE_Rep
2820c 6c 61 63 65 0a 20 20 20 20 20 20 20 20 29 3b 0a  lace.        );.
2820d 20 20 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c          seenRepl
2820e 61 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ace = 1;.       
2820f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
28210 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
28211 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
28212 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j3);.    sqlite
28213 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
28214 70 50 61 72 73 65 2c 20 72 65 67 52 29 3b 0a 20  pParse, regR);. 
28215 20 7d 0a 20 20 0a 20 20 69 66 28 20 70 62 4d 61   }.  .  if( pbMa
28216 79 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20  yReplace ){.    
28217 2a 70 62 4d 61 79 52 65 70 6c 61 63 65 20 3d 20  *pbMayReplace = 
28218 73 65 65 6e 52 65 70 6c 61 63 65 3b 0a 20 20 7d  seenReplace;.  }
28219 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2821a 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
2821b 20 63 6f 64 65 20 74 6f 20 66 69 6e 69 73 68 20   code to finish 
2821c 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50  the INSERT or UP
2821d 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 0a 2a  DATE operation.*
2821e 2a 20 74 68 61 74 20 77 61 73 20 73 74 61 72 74  * that was start
2821f 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63 61  ed by a prior ca
28220 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e  ll to sqlite3Gen
28221 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
28222 68 65 63 6b 73 2e 0a 2a 2a 20 41 20 63 6f 6e 73  hecks..** A cons
28223 65 63 75 74 69 76 65 20 72 61 6e 67 65 20 6f 66  ecutive range of
28224 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74   registers start
28225 69 6e 67 20 61 74 20 72 65 67 52 6f 77 69 64 20  ing at regRowid 
28226 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20  contains the.** 
28227 72 6f 77 69 64 20 61 6e 64 20 74 68 65 20 63 6f  rowid and the co
28228 6e 74 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65  ntent to be inse
28229 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rted..**.** The 
2822a 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69  arguments to thi
2822b 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64  s routine should
2822c 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20   be the same as 
2822d 74 68 65 20 66 69 72 73 74 20 73 69 78 0a 2a 2a  the first six.**
2822e 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 73 71   arguments to sq
2822f 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e  lite3GenerateCon
28230 73 74 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a  straintChecks..*
28231 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
28232 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
28233 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a  pleteInsertion(.
28234 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
28235 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
28236 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
28237 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
28238 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c       /* the tabl
28239 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20  e into which we 
2823a 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f  are inserting */
2823b 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20  .  int baseCur, 
2823c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2823d 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  of a read/write 
2823e 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
2823f 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74  at pTab */.  int
28240 20 72 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20   regRowid,      
28241 20 2f 2a 20 52 61 6e 67 65 20 6f 66 20 63 6f 6e   /* Range of con
28242 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  tent */.  int *a
28243 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a  RegIdx,       /*
28244 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20 62   Register used b
28245 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30  y each index.  0
28246 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69   for unused indi
28247 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55  ces */.  int isU
28248 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20  pdate,       /* 
28249 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c  True for UPDATE,
2824a 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52   False for INSER
2824b 54 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e  T */.  int appen
2824c 64 42 69 61 73 2c 20 20 20 20 20 2f 2a 20 54 72  dBias,     /* Tr
2824d 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69  ue if this is li
2824e 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70  kely to be an ap
2824f 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 75 73  pend */.  int us
28250 65 53 65 65 6b 52 65 73 75 6c 74 20 20 20 2f 2a  eSeekResult   /*
28251 20 54 72 75 65 20 74 6f 20 73 65 74 20 74 68 65   True to set the
28252 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66   USESEEKRESULT f
28253 6c 61 67 20 6f 6e 20 4f 50 5f 5b 49 64 78 5d 49  lag on OP_[Idx]I
28254 6e 73 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e  nsert */.){.  in
28255 74 20 69 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  t i;.  Vdbe *v;.
28256 20 20 69 6e 74 20 6e 49 64 78 3b 0a 20 20 49 6e    int nIdx;.  In
28257 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 75 38 20  dex *pIdx;.  u8 
28258 70 69 6b 5f 66 6c 61 67 73 3b 0a 20 20 69 6e 74  pik_flags;.  int
28259 20 72 65 67 44 61 74 61 3b 0a 20 20 69 6e 74 20   regData;.  int 
2825a 72 65 67 52 65 63 3b 0a 0a 20 20 76 20 3d 20 73  regRec;..  v = s
2825b 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2825c 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28  arse);.  assert(
2825d 20 76 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   v!=0 );.  asser
2825e 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  t( pTab->pSelect
2825f 3d 3d 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20  ==0 );  /* This 
28260 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56  table is not a V
28261 49 45 57 20 2a 2f 0a 20 20 66 6f 72 28 6e 49 64  IEW */.  for(nId
28262 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  x=0, pIdx=pTab->
28263 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
28264 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
28265 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20 66 6f 72 28  nIdx++){}.  for(
28266 69 3d 6e 49 64 78 2d 31 3b 20 69 3e 3d 30 3b 20  i=nIdx-1; i>=0; 
28267 69 2d 2d 29 7b 0a 20 20 20 20 69 66 28 20 61 52  i--){.    if( aR
28268 65 67 49 64 78 5b 69 5d 3d 3d 30 20 29 20 63 6f  egIdx[i]==0 ) co
28269 6e 74 69 6e 75 65 3b 0a 20 20 20 20 73 71 6c 69  ntinue;.    sqli
2826a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2826b 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 62   OP_IdxInsert, b
2826c 61 73 65 43 75 72 2b 69 2b 31 2c 20 61 52 65 67  aseCur+i+1, aReg
2826d 49 64 78 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  Idx[i]);.    if(
2826e 20 75 73 65 53 65 65 6b 52 65 73 75 6c 74 20 29   useSeekResult )
2826f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
28270 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
28271 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53  PFLAG_USESEEKRES
28272 55 4c 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ULT);.    }.  }.
28273 20 20 72 65 67 44 61 74 61 20 3d 20 72 65 67 52    regData = regR
28274 6f 77 69 64 20 2b 20 31 3b 0a 20 20 72 65 67 52  owid + 1;.  regR
28275 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ec = sqlite3GetT
28276 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
28277 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28278 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
28279 63 6f 72 64 2c 20 72 65 67 44 61 74 61 2c 20 70  cord, regData, p
2827a 54 61 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65  Tab->nCol, regRe
2827b 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62  c);.  sqlite3Tab
2827c 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  leAffinityStr(v,
2827d 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65   pTab);.  sqlite
2827e 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
2827f 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
28280 20 72 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e   regData, pTab->
28281 6e 43 6f 6c 29 3b 0a 20 20 69 66 28 20 70 50 61  nCol);.  if( pPa
28282 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20  rse->nested ){. 
28283 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30     pik_flags = 0
28284 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
28285 69 6b 5f 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41  ik_flags = OPFLA
28286 47 5f 4e 43 48 41 4e 47 45 3b 0a 20 20 20 20 70  G_NCHANGE;.    p
28287 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 28 69 73 55  ik_flags |= (isU
28288 70 64 61 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55  pdate?OPFLAG_ISU
28289 50 44 41 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53  PDATE:OPFLAG_LAS
2828a 54 52 4f 57 49 44 29 3b 0a 20 20 7d 0a 20 20 69  TROWID);.  }.  i
2828b 66 28 20 61 70 70 65 6e 64 42 69 61 73 20 29 7b  f( appendBias ){
2828c 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c  .    pik_flags |
2828d 3d 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 3b  = OPFLAG_APPEND;
2828e 0a 20 20 7d 0a 20 20 69 66 28 20 75 73 65 53 65  .  }.  if( useSe
2828f 65 6b 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20  ekResult ){.    
28290 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46  pik_flags |= OPF
28291 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
28292 54 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  T;.  }.  sqlite3
28293 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
28294 5f 49 6e 73 65 72 74 2c 20 62 61 73 65 43 75 72  _Insert, baseCur
28295 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77  , regRec, regRow
28296 69 64 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72  id);.  if( !pPar
28297 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20  se->nested ){.  
28298 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
28299 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61  ngeP4(v, -1, pTa
2829a 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41  b->zName, P4_STA
2829b 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  TIC);.  }.  sqli
2829c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2829d 76 2c 20 70 69 6b 5f 66 6c 61 67 73 29 3b 0a 7d  v, pik_flags);.}
2829e 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2829f 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
282a0 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 66 6f 72  open cursors for
282a1 20 61 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72   a table and for
282a2 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20   all.** indices 
282a3 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20  of that table.  
282a4 54 68 65 20 22 62 61 73 65 43 75 72 22 20 70 61  The "baseCur" pa
282a5 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 63  rameter is the c
282a6 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65  ursor number use
282a7 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62  d.** for the tab
282a8 6c 65 2e 20 20 49 6e 64 69 63 65 73 20 61 72 65  le.  Indices are
282a9 20 6f 70 65 6e 65 64 20 6f 6e 20 73 75 62 73 65   opened on subse
282aa 71 75 65 6e 74 20 63 75 72 73 6f 72 73 2e 0a 2a  quent cursors..*
282ab 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
282ac 6e 75 6d 62 65 72 20 6f 66 20 69 6e 64 69 63 65  number of indice
282ad 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  s on the table..
282ae 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
282af 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65  E int sqlite3Ope
282b0 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73  nTableAndIndices
282b1 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
282b2 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  e,   /* Parsing 
282b3 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
282b4 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a  le *pTab,     /*
282b5 20 54 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65   Table to be ope
282b6 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73  ned */.  int bas
282b7 65 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72  eCur,     /* Cur
282b8 73 6f 72 20 6e 75 6d 62 65 72 20 61 73 73 69 67  sor number assig
282b9 6e 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ned to the table
282ba 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 20 20 20 20   */.  int op    
282bb 20 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65         /* OP_Ope
282bc 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e  nRead or OP_Open
282bd 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  Write */.){.  in
282be 74 20 69 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  t i;.  int iDb;.
282bf 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
282c0 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28   Vdbe *v;..  if(
282c1 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
282c2 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
282c3 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
282c4 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
282c5 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
282c6 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69  ema);.  v = sqli
282c7 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
282c8 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  e);.  assert( v!
282c9 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f  =0 );.  sqlite3O
282ca 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
282cb 20 62 61 73 65 43 75 72 2c 20 69 44 62 2c 20 70   baseCur, iDb, p
282cc 54 61 62 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28  Tab, op);.  for(
282cd 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=1, pIdx=pTab->
282ce 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
282cf 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
282d0 69 2b 2b 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66  i++){.    KeyInf
282d1 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  o *pKey = sqlite
282d2 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
282d3 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20  arse, pIdx);.   
282d4 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70   assert( pIdx->p
282d5 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
282d6 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c  chema );.    sql
282d7 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
282d8 2c 20 6f 70 2c 20 69 2b 62 61 73 65 43 75 72 2c  , op, i+baseCur,
282d9 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62   pIdx->tnum, iDb
282da 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
282db 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
282dc 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Key, P4_KEYINFO_
282dd 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64  HANDOFF);.    Vd
282de 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
282df 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29  s", pIdx->zName)
282e0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  );.  }.  if( pPa
282e1 72 73 65 2d 3e 6e 54 61 62 3c 62 61 73 65 43 75  rse->nTab<baseCu
282e2 72 2b 69 20 29 7b 0a 20 20 20 20 70 50 61 72 73  r+i ){.    pPars
282e3 65 2d 3e 6e 54 61 62 20 3d 20 62 61 73 65 43 75  e->nTab = baseCu
282e4 72 2b 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  r+i;.  }.  retur
282e5 6e 20 69 2d 31 3b 0a 7d 0a 0a 0a 23 69 66 64 65  n i-1;.}...#ifde
282e6 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
282e7 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
282e8 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  g global variabl
282e9 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  e is incremented
282ea 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a   whenever the.**
282eb 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
282ec 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20  zation is used. 
282ed 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f   This is used fo
282ee 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72  r testing.** pur
282ef 70 6f 73 65 73 20 6f 6e 6c 79 20 2d 20 74 6f 20  poses only - to 
282f0 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 74 72  make sure the tr
282f1 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  ansfer optimizat
282f2 69 6f 6e 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 73  ion really.** is
282f3 20 68 61 70 70 65 6e 69 6e 67 20 77 68 65 6e 20   happening when 
282f4 69 74 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f  it is suppose to
282f5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
282f6 69 6e 74 20 73 71 6c 69 74 65 33 5f 78 66 65 72  int sqlite3_xfer
282f7 6f 70 74 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69  opt_count;.#endi
282f8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  f /* SQLITE_TEST
282f9 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51   */...#ifndef SQ
282fa 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f  LITE_OMIT_XFER_O
282fb 50 54 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  PT./*.** Check t
282fc 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65  o collation name
282fd 73 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 79  s to see if they
282fe 20 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e   are compatible.
282ff 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78  .*/.static int x
28300 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c  ferCompatibleCol
28301 6c 61 74 69 6f 6e 28 63 6f 6e 73 74 20 63 68 61  lation(const cha
28302 72 20 2a 7a 31 2c 20 63 6f 6e 73 74 20 63 68 61  r *z1, const cha
28303 72 20 2a 7a 32 29 7b 0a 20 20 69 66 28 20 7a 31  r *z2){.  if( z1
28304 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
28305 6e 20 7a 32 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69  n z2==0;.  }.  i
28306 66 28 20 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20  f( z2==0 ){.    
28307 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
28308 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74  return sqlite3St
28309 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30  rICmp(z1, z2)==0
2830a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ;.}.../*.** Chec
2830b 6b 20 74 6f 20 73 65 65 20 69 66 20 69 6e 64 65  k to see if inde
2830c 78 20 70 53 72 63 20 69 73 20 63 6f 6d 70 61 74  x pSrc is compat
2830d 69 62 6c 65 20 61 73 20 61 20 73 6f 75 72 63 65  ible as a source
2830e 20 6f 66 20 64 61 74 61 0a 2a 2a 20 66 6f 72 20   of data.** for 
2830f 69 6e 64 65 78 20 70 44 65 73 74 20 69 6e 20 61  index pDest in a
28310 6e 20 69 6e 73 65 72 74 20 74 72 61 6e 73 66 65  n insert transfe
28311 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  r optimization. 
28312 20 54 68 65 20 72 75 6c 65 73 0a 2a 2a 20 66 6f   The rules.** fo
28313 72 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 69  r a compatible i
28314 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  ndex:.**.**    *
28315 20 20 20 54 68 65 20 69 6e 64 65 78 20 69 73 20     The index is 
28316 6f 76 65 72 20 74 68 65 20 73 61 6d 65 20 73 65  over the same se
28317 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  t of columns.** 
28318 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20     *   The same 
28319 44 45 53 43 20 61 6e 64 20 41 53 43 20 6d 61 72  DESC and ASC mar
2831a 6b 69 6e 67 73 20 6f 63 63 75 72 73 20 6f 6e 20  kings occurs on 
2831b 61 6c 6c 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20  all columns.**  
2831c 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 6f    *   The same o
2831d 6e 45 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e  nError processin
2831e 67 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f  g (OE_Abort, OE_
2831f 49 67 6e 6f 72 65 2c 20 65 74 63 29 0a 2a 2a 20  Ignore, etc).** 
28320 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20     *   The same 
28321 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
28322 63 65 20 6f 6e 20 65 61 63 68 20 63 6f 6c 75 6d  ce on each colum
28323 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n.*/.static int 
28324 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e  xferCompatibleIn
28325 64 65 78 28 49 6e 64 65 78 20 2a 70 44 65 73 74  dex(Index *pDest
28326 2c 20 49 6e 64 65 78 20 2a 70 53 72 63 29 7b 0a  , Index *pSrc){.
28327 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72    int i;.  asser
28328 74 28 20 70 44 65 73 74 20 26 26 20 70 53 72 63  t( pDest && pSrc
28329 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   );.  assert( pD
2832a 65 73 74 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72  est->pTable!=pSr
2832b 63 2d 3e 70 54 61 62 6c 65 20 29 3b 0a 20 20 69  c->pTable );.  i
2832c 66 28 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d  f( pDest->nColum
2832d 6e 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e  n!=pSrc->nColumn
2832e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2832f 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
28330 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
28331 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ns */.  }.  if( 
28332 70 44 65 73 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d  pDest->onError!=
28333 70 53 72 63 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b  pSrc->onError ){
28334 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
28335 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f   /* Different co
28336 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f  nflict resolutio
28337 6e 20 73 74 72 61 74 65 67 69 65 73 20 2a 2f 0a  n strategies */.
28338 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
28339 3c 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pSrc->nColumn; 
2833a 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 53  i++){.    if( pS
2833b 72 63 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 21  rc->aiColumn[i]!
2833c 3d 70 44 65 73 74 2d 3e 61 69 43 6f 6c 75 6d 6e  =pDest->aiColumn
2833d 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74  [i] ){.      ret
2833e 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66  urn 0;   /* Diff
2833f 65 72 65 6e 74 20 63 6f 6c 75 6d 6e 73 20 69 6e  erent columns in
28340 64 65 78 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20  dexed */.    }. 
28341 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61 53 6f     if( pSrc->aSo
28342 72 74 4f 72 64 65 72 5b 69 5d 21 3d 70 44 65 73  rtOrder[i]!=pDes
28343 74 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  t->aSortOrder[i]
28344 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
28345 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65   0;   /* Differe
28346 6e 74 20 73 6f 72 74 20 6f 72 64 65 72 73 20 2a  nt sort orders *
28347 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  /.    }.    if( 
28348 21 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43  !xferCompatibleC
28349 6f 6c 6c 61 74 69 6f 6e 28 70 53 72 63 2d 3e 61  ollation(pSrc->a
2834a 7a 43 6f 6c 6c 5b 69 5d 2c 70 44 65 73 74 2d 3e  zColl[i],pDest->
2834b 61 7a 43 6f 6c 6c 5b 69 5d 29 20 29 7b 0a 20 20  azColl[i]) ){.  
2834c 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
2834d 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c  /* Different col
2834e 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
2834f 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   */.    }.  }.. 
28350 20 2f 2a 20 49 66 20 6e 6f 20 74 65 73 74 20 61   /* If no test a
28351 62 6f 76 65 20 66 61 69 6c 73 20 74 68 65 6e 20  bove fails then 
28352 74 68 65 20 69 6e 64 69 63 65 73 20 6d 75 73 74  the indices must
28353 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 2a   be compatible *
28354 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  /.  return 1;.}.
28355 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
28356 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69  he transfer opti
28357 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e 53 45  mization on INSE
28358 52 54 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a  RTs of the form.
28359 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54  **.**     INSERT
2835a 20 49 4e 54 4f 20 74 61 62 31 20 53 45 4c 45 43   INTO tab1 SELEC
2835b 54 20 2a 20 46 52 4f 4d 20 74 61 62 32 3b 0a 2a  T * FROM tab2;.*
2835c 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6d 69  *.** This optimi
2835d 7a 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61  zation is only a
2835e 74 74 65 6d 70 74 65 64 20 69 66 0a 2a 2a 0a 2a  ttempted if.**.*
2835f 2a 20 20 20 20 28 31 29 20 20 74 61 62 31 20 61  *    (1)  tab1 a
28360 6e 64 20 74 61 62 32 20 68 61 76 65 20 69 64 65  nd tab2 have ide
28361 6e 74 69 63 61 6c 20 73 63 68 65 6d 61 73 20 69  ntical schemas i
28362 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65  ncluding all the
28363 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 61 6d 65  .**         same
28364 20 69 6e 64 69 63 65 73 20 61 6e 64 20 63 6f 6e   indices and con
28365 73 74 72 61 69 6e 74 73 0a 2a 2a 0a 2a 2a 20 20  straints.**.**  
28366 20 20 28 32 29 20 20 74 61 62 31 20 61 6e 64 20    (2)  tab1 and 
28367 74 61 62 32 20 61 72 65 20 64 69 66 66 65 72 65  tab2 are differe
28368 6e 74 20 74 61 62 6c 65 73 0a 2a 2a 0a 2a 2a 20  nt tables.**.** 
28369 20 20 20 28 33 29 20 20 54 68 65 72 65 20 6d 75     (3)  There mu
2836a 73 74 20 62 65 20 6e 6f 20 74 72 69 67 67 65 72  st be no trigger
2836b 73 20 6f 6e 20 74 61 62 31 0a 2a 2a 0a 2a 2a 20  s on tab1.**.** 
2836c 20 20 20 28 34 29 20 20 54 68 65 20 72 65 73 75     (4)  The resu
2836d 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45  lt set of the SE
2836e 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
2836f 73 20 22 2a 22 0a 2a 2a 0a 2a 2a 20 20 20 20 28  s "*".**.**    (
28370 35 29 20 20 54 68 65 20 53 45 4c 45 43 54 20 73  5)  The SELECT s
28371 74 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 6f 20  tatement has no 
28372 57 48 45 52 45 2c 20 48 41 56 49 4e 47 2c 20 4f  WHERE, HAVING, O
28373 52 44 45 52 20 42 59 2c 20 47 52 4f 55 50 20 42  RDER BY, GROUP B
28374 59 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 72  Y,.**         or
28375 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a   LIMIT clause..*
28376 2a 0a 2a 2a 20 20 20 20 28 36 29 20 20 54 68 65  *.**    (6)  The
28377 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
28378 74 20 69 73 20 61 20 73 69 6d 70 6c 65 20 28 6e  t is a simple (n
28379 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 29 20 73  ot a compound) s
2837a 65 6c 65 63 74 20 74 68 61 74 0a 2a 2a 20 20 20  elect that.**   
2837b 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 6f        contains o
2837c 6e 6c 79 20 74 61 62 32 20 69 6e 20 69 74 73 20  nly tab2 in its 
2837d 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 0a 2a  FROM clause.**.*
2837e 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20 66 6f  * This method fo
2837f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74  r implementing t
28380 68 65 20 49 4e 53 45 52 54 20 74 72 61 6e 73 66  he INSERT transf
28381 65 72 73 20 72 61 77 20 72 65 63 6f 72 64 73 20  ers raw records 
28382 66 72 6f 6d 0a 2a 2a 20 74 61 62 32 20 6f 76 65  from.** tab2 ove
28383 72 20 74 6f 20 74 61 62 31 2e 20 20 54 68 65 20  r to tab1.  The 
28384 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20  columns are not 
28385 64 65 63 6f 64 65 64 2e 20 20 52 61 77 20 72 65  decoded.  Raw re
28386 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74 68  cords from.** th
28387 65 20 69 6e 64 69 63 65 73 20 6f 66 20 74 61 62  e indices of tab
28388 32 20 61 72 65 20 74 72 61 6e 73 66 65 72 65 64  2 are transfered
28389 20 74 6f 20 74 61 62 31 20 61 73 20 77 65 6c 6c   to tab1 as well
2838a 2e 20 20 49 6e 20 73 6f 20 64 6f 69 6e 67 2c 0a  .  In so doing,.
2838b 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  ** the resulting
2838c 20 74 61 62 31 20 68 61 73 20 6d 75 63 68 20 6c   tab1 has much l
2838d 65 73 73 20 66 72 61 67 6d 65 6e 74 61 74 69 6f  ess fragmentatio
2838e 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
2838f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 54 52  utine returns TR
28390 55 45 20 69 66 20 74 68 65 20 6f 70 74 69 6d 69  UE if the optimi
28391 7a 61 74 69 6f 6e 20 69 73 20 61 74 74 65 6d 70  zation is attemp
28392 74 65 64 2e 20 20 49 66 20 61 6e 79 0a 2a 2a 20  ted.  If any.** 
28393 6f 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  of the condition
28394 73 20 61 62 6f 76 65 20 66 61 69 6c 20 73 6f 20  s above fail so 
28395 74 68 61 74 20 74 68 65 20 6f 70 74 69 6d 69 7a  that the optimiz
28396 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ation should not
28397 0a 2a 2a 20 62 65 20 61 74 74 65 6d 70 74 65 64  .** be attempted
28398 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
28399 69 6e 65 20 72 65 74 75 72 6e 73 20 46 41 4c 53  ine returns FALS
2839a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  E..*/.static int
2839b 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f   xferOptimizatio
2839c 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
2839d 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61  se,        /* Pa
2839e 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
2839f 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20    Table *pDest, 
283a0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
283a1 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65  able we are inse
283a2 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20  rting into */.  
283a3 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
283a4 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43        /* A SELEC
283a5 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75  T statement to u
283a6 73 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73  se as the data s
283a7 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  ource */.  int o
283a8 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20  nError,         
283a9 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c   /* How to handl
283aa 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72  e constraint err
283ab 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ors */.  int iDb
283ac 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f  Dest           /
283ad 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f  * The database o
283ae 66 20 70 44 65 73 74 20 2a 2f 0a 29 7b 0a 20 20  f pDest */.){.  
283af 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
283b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
283b1 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73   /* The result s
283b2 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  et of the SELECT
283b3 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 72   */.  Table *pSr
283b4 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
283b5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
283b6 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
283b7 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 43 54  clause of SELECT
283b8 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 53 72   */.  Index *pSr
283b9 63 49 64 78 2c 20 2a 70 44 65 73 74 49 64 78 3b  cIdx, *pDestIdx;
283ba 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
283bb 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e   and destination
283bc 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 73 74   indices */.  st
283bd 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
283be 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 2f  m *pItem;      /
283bf 2a 20 41 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20  * An element of 
283c0 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 20 2a 2f  pSelect->pSrc */
283c1 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
283c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
283c3 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
283c4 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ter */.  int iDb
283c5 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Src;            
283c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
283c7 20 64 61 74 61 62 61 73 65 20 6f 66 20 70 53 72   database of pSr
283c8 63 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c  c */.  int iSrc,
283c9 20 69 44 65 73 74 3b 20 20 20 20 20 20 20 20 20   iDest;         
283ca 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
283cb 72 73 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 61  rs from source a
283cc 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a  nd destination *
283cd 2f 0a 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61  /.  int addr1, a
283ce 64 64 72 32 3b 20 20 20 20 20 20 20 20 20 20 20  ddr2;           
283cf 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 61 64 64       /* Loop add
283d0 72 65 73 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20  resses */.  int 
283d1 65 6d 70 74 79 44 65 73 74 54 65 73 74 3b 20 20  emptyDestTest;  
283d2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
283d3 41 64 64 72 65 73 73 20 6f 66 20 74 65 73 74 20  Address of test 
283d4 66 6f 72 20 65 6d 70 74 79 20 70 44 65 73 74 20  for empty pDest 
283d5 2a 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79 53 72  */.  int emptySr
283d6 63 54 65 73 74 3b 20 20 20 20 20 20 20 20 20 20  cTest;          
283d7 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
283d8 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70   of test for emp
283d9 74 79 20 70 53 72 63 20 2a 2f 0a 20 20 56 64 62  ty pSrc */.  Vdb
283da 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
283db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
283dc 20 54 68 65 20 56 44 42 45 20 77 65 20 61 72 65   The VDBE we are
283dd 20 62 75 69 6c 64 69 6e 67 20 2a 2f 0a 20 20 4b   building */.  K
283de 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20  eyInfo *pKey;   
283df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
283e0 2f 2a 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69  /* Key informati
283e1 6f 6e 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 20  on for an index 
283e2 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f  */.  int regAuto
283e3 69 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  inc;            
283e4 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
283e5 72 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79  register used by
283e6 20 41 55 54 4f 49 4e 43 20 2a 2f 0a 20 20 69 6e   AUTOINC */.  in
283e7 74 20 64 65 73 74 48 61 73 55 6e 69 71 75 65 49  t destHasUniqueI
283e8 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  dx = 0;        /
283e9 2a 20 54 72 75 65 20 69 66 20 70 44 65 73 74 20  * True if pDest 
283ea 68 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64  has a UNIQUE ind
283eb 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44  ex */.  int regD
283ec 61 74 61 2c 20 72 65 67 52 6f 77 69 64 3b 20 20  ata, regRowid;  
283ed 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
283ee 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 64 61  sters holding da
283ef 74 61 20 61 6e 64 20 72 6f 77 69 64 20 2a 2f 0a  ta and rowid */.
283f0 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 3d 3d  .  if( pSelect==
283f1 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
283f2 30 3b 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20  0;   /* Must be 
283f3 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 49 4e 53  of the form  INS
283f4 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c  ERT INTO ... SEL
283f5 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20  ECT ... */.  }. 
283f6 20 69 66 28 20 73 71 6c 69 74 65 33 54 72 69 67   if( sqlite3Trig
283f7 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  gerList(pParse, 
283f8 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 72 65  pDest) ){.    re
283f9 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62  turn 0;   /* tab
283fa 31 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20  1 must not have 
283fb 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a  triggers */.  }.
283fc 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
283fd 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
283fe 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 74 61  .  if( pDest->ta
283ff 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
28400 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ual ){.    retur
28401 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d  n 0;   /* tab1 m
28402 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72  ust not be a vir
28403 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
28404 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 6f  }.#endif.  if( o
28405 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
28406 6c 74 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f  lt ){.    onErro
28407 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r = OE_Abort;.  
28408 7d 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21  }.  if( onError!
28409 3d 4f 45 5f 41 62 6f 72 74 20 26 26 20 6f 6e 45  =OE_Abort && onE
2840a 72 72 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62 61 63  rror!=OE_Rollbac
2840b 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  k ){.    return 
2840c 30 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 64  0;   /* Cannot d
2840d 6f 20 4f 52 20 52 45 50 4c 41 43 45 20 6f 72 20  o OR REPLACE or 
2840e 4f 52 20 49 47 4e 4f 52 45 20 6f 72 20 4f 52 20  OR IGNORE or OR 
2840f 46 41 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20 61 73  FAIL */.  }.  as
28410 73 65 72 74 28 70 53 65 6c 65 63 74 2d 3e 70 53  sert(pSelect->pS
28411 72 63 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f 63 61  rc);   /* alloca
28412 74 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 72  ted even if ther
28413 65 20 69 73 20 6e 6f 20 46 52 4f 4d 20 63 6c 61  e is no FROM cla
28414 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 65  use */.  if( pSe
28415 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 6e 53 72 63  lect->pSrc->nSrc
28416 21 3d 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=1 ){.    retur
28417 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63  n 0;   /* FROM c
28418 6c 61 75 73 65 20 6d 75 73 74 20 68 61 76 65 20  lause must have 
28419 65 78 61 63 74 6c 79 20 6f 6e 65 20 74 65 72 6d  exactly one term
2841a 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53   */.  }.  if( pS
2841b 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 61 5b 30  elect->pSrc->a[0
2841c 5d 2e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ].pSelect ){.   
2841d 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
2841e 46 52 4f 4d 20 63 6c 61 75 73 65 20 63 61 6e 6e  FROM clause cann
2841f 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 75 62  ot contain a sub
28420 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69  query */.  }.  i
28421 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65  f( pSelect->pWhe
28422 72 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  re ){.    return
28423 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
28424 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 57  may not have a W
28425 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
28426 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74   }.  if( pSelect
28427 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
28428 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
28429 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
2842a 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59  have an ORDER BY
2842b 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20   clause */.  }. 
2842c 20 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65 65 64 20   /* Do not need 
2842d 74 6f 20 74 65 73 74 20 66 6f 72 20 61 20 48 41  to test for a HA
2842e 56 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 49 66  VING clause.  If
2842f 20 48 41 56 49 4e 47 20 69 73 20 70 72 65 73 65   HAVING is prese
28430 6e 74 20 62 75 74 0a 20 20 2a 2a 20 74 68 65 72  nt but.  ** ther
28431 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59  e is no ORDER BY
28432 2c 20 77 65 20 77 69 6c 6c 20 67 65 74 20 61 6e  , we will get an
28433 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28   error. */.  if(
28434 20 70 53 65 6c 65 63 74 2d 3e 70 47 72 6f 75 70   pSelect->pGroup
28435 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  By ){.    return
28436 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
28437 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 47  may not have a G
28438 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a  ROUP BY clause *
28439 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c  /.  }.  if( pSel
2843a 65 63 74 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20  ect->pLimit ){. 
2843b 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
2843c 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74  * SELECT may not
2843d 20 68 61 76 65 20 61 20 4c 49 4d 49 54 20 63 6c   have a LIMIT cl
2843e 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 61 73  ause */.  }.  as
2843f 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d 3e 70  sert( pSelect->p
28440 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 2f 2a  Offset==0 );  /*
28441 20 4d 75 73 74 20 62 65 20 73 6f 20 69 66 20 70   Must be so if p
28442 4c 69 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20 69 66  Limit==0 */.  if
28443 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  ( pSelect->pPrio
28444 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
28445 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d  0;   /* SELECT m
28446 61 79 20 6e 6f 74 20 62 65 20 61 20 63 6f 6d 70  ay not be a comp
28447 6f 75 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20 20  ound query */.  
28448 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  }.  if( pSelect-
28449 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44  >selFlags & SF_D
2844a 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 72  istinct ){.    r
2844b 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45  eturn 0;   /* SE
2844c 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62 65 20  LECT may not be 
2844d 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a  DISTINCT */.  }.
2844e 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65    pEList = pSele
2844f 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73  ct->pEList;.  as
28450 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20  sert( pEList!=0 
28451 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  );.  if( pEList-
28452 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20  >nExpr!=1 ){.   
28453 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
28454 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d  The result set m
28455 75 73 74 20 68 61 76 65 20 65 78 61 63 74 6c 79  ust have exactly
28456 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20   one column */. 
28457 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c   }.  assert( pEL
28458 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20  ist->a[0].pExpr 
28459 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  );.  if( pEList-
2845a 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21  >a[0].pExpr->op!
2845b 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72  =TK_ALL ){.    r
2845c 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68  eturn 0;   /* Th
2845d 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73  e result set mus
2845e 74 20 62 65 20 74 68 65 20 73 70 65 63 69 61 6c  t be the special
2845f 20 6f 70 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f   operator "*" */
28460 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68  .  }..  /* At th
28461 69 73 20 70 6f 69 6e 74 20 77 65 20 68 61 76 65  is point we have
28462 20 65 73 74 61 62 6c 69 73 68 65 64 20 74 68 61   established tha
28463 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  t the statement 
28464 69 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63  is of the.  ** c
28465 6f 72 72 65 63 74 20 73 79 6e 74 61 63 74 69 63  orrect syntactic
28466 20 66 6f 72 6d 20 74 6f 20 70 61 72 74 69 63 69   form to partici
28467 70 61 74 65 20 69 6e 20 74 68 69 73 20 6f 70 74  pate in this opt
28468 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a  imization.  Now.
28469 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20    ** we have to 
2846a 63 68 65 63 6b 20 74 68 65 20 73 65 6d 61 6e 74  check the semant
2846b 69 63 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65  ics..  */.  pIte
2846c 6d 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  m = pSelect->pSr
2846d 63 2d 3e 61 3b 0a 20 20 70 53 72 63 20 3d 20 73  c->a;.  pSrc = s
2846e 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
2846f 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74  e(pParse, 0, pIt
28470 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d  em->zName, pItem
28471 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
28472 69 66 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20  if( pSrc==0 ){. 
28473 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
28474 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 64 6f  * FROM clause do
28475 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  es not contain a
28476 20 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20   real table */. 
28477 20 7d 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 70   }.  if( pSrc==p
28478 44 65 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75  Dest ){.    retu
28479 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20  rn 0;   /* tab1 
2847a 61 6e 64 20 74 61 62 32 20 6d 61 79 20 6e 6f 74  and tab2 may not
2847b 20 62 65 20 74 68 65 20 73 61 6d 65 20 74 61 62   be the same tab
2847c 6c 65 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65  le */.  }.#ifnde
2847d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2847e 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
2847f 20 70 53 72 63 2d 3e 74 61 62 46 6c 61 67 73 20   pSrc->tabFlags 
28480 26 20 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a  & TF_Virtual ){.
28481 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
28482 2f 2a 20 74 61 62 32 20 6d 75 73 74 20 6e 6f 74  /* tab2 must not
28483 20 62 65 20 61 20 76 69 72 74 75 61 6c 20 74 61   be a virtual ta
28484 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69  ble */.  }.#endi
28485 66 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70 53  f.  if( pSrc->pS
28486 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74  elect ){.    ret
28487 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32  urn 0;   /* tab2
28488 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 76 69   may not be a vi
28489 65 77 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ew */.  }.  if( 
2848a 70 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72  pDest->nCol!=pSr
2848b 63 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 72  c->nCol ){.    r
2848c 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e 75  eturn 0;   /* Nu
2848d 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2848e 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65  must be the same
2848f 20 69 6e 20 74 61 62 31 20 61 6e 64 20 74 61 62   in tab1 and tab
28490 32 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  2 */.  }.  if( p
28491 44 65 73 74 2d 3e 69 50 4b 65 79 21 3d 70 53 72  Dest->iPKey!=pSr
28492 63 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  c->iPKey ){.    
28493 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 42  return 0;   /* B
28494 6f 74 68 20 74 61 62 6c 65 73 20 6d 75 73 74 20  oth tables must 
28495 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 49 4e  have the same IN
28496 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
28497 59 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69  Y */.  }.  for(i
28498 3d 30 3b 20 69 3c 70 44 65 73 74 2d 3e 6e 43 6f  =0; i<pDest->nCo
28499 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  l; i++){.    if(
2849a 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e   pDest->aCol[i].
2849b 61 66 66 69 6e 69 74 79 21 3d 70 53 72 63 2d 3e  affinity!=pSrc->
2849c 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79  aCol[i].affinity
2849d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2849e 20 30 3b 20 20 20 20 2f 2a 20 41 66 66 69 6e 69   0;    /* Affini
2849f 74 79 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  ty must be the s
284a0 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d  ame on all colum
284a1 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ns */.    }.    
284a2 69 66 28 20 21 78 66 65 72 43 6f 6d 70 61 74 69  if( !xferCompati
284a3 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 44 65  bleCollation(pDe
284a4 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c  st->aCol[i].zCol
284a5 6c 2c 20 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d  l, pSrc->aCol[i]
284a6 2e 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20  .zColl) ){.     
284a7 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a   return 0;    /*
284a8 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
284a9 6e 63 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  nce must be the 
284aa 73 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75  same on all colu
284ab 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  mns */.    }.   
284ac 20 69 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c   if( pDest->aCol
284ad 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21  [i].notNull && !
284ae 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f  pSrc->aCol[i].no
284af 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72  tNull ){.      r
284b0 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74  eturn 0;    /* t
284b1 61 62 32 20 6d 75 73 74 20 62 65 20 4e 4f 54 20  ab2 must be NOT 
284b2 4e 55 4c 4c 20 69 66 20 74 61 62 31 20 69 73 20  NULL if tab1 is 
284b3 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  */.    }.  }.  f
284b4 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44 65 73  or(pDestIdx=pDes
284b5 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74  t->pIndex; pDest
284b6 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44  Idx; pDestIdx=pD
284b7 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  estIdx->pNext){.
284b8 20 20 20 20 69 66 28 20 70 44 65 73 74 49 64 78      if( pDestIdx
284b9 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
284ba 6e 65 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74  ne ){.      dest
284bb 48 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20 31  HasUniqueIdx = 1
284bc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
284bd 70 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49  pSrcIdx=pSrc->pI
284be 6e 64 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70  ndex; pSrcIdx; p
284bf 53 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e  SrcIdx=pSrcIdx->
284c0 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66  pNext){.      if
284c1 28 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65  ( xferCompatible
284c2 49 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20  Index(pDestIdx, 
284c3 70 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b  pSrcIdx) ) break
284c4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
284c5 70 53 72 63 49 64 78 3d 3d 30 20 29 7b 0a 20 20  pSrcIdx==0 ){.  
284c6 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
284c7 20 2f 2a 20 70 44 65 73 74 49 64 78 20 68 61 73   /* pDestIdx has
284c8 20 6e 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e   no correspondin
284c9 67 20 69 6e 64 65 78 20 69 6e 20 70 53 72 63 20  g index in pSrc 
284ca 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66  */.    }.  }.#if
284cb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
284cc 5f 43 48 45 43 4b 0a 20 20 69 66 28 20 70 44 65  _CHECK.  if( pDe
284cd 73 74 2d 3e 70 43 68 65 63 6b 20 26 26 20 21 73  st->pCheck && !s
284ce 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
284cf 65 28 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20  e(pSrc->pCheck, 
284d0 70 44 65 73 74 2d 3e 70 43 68 65 63 6b 29 20 29  pDest->pCheck) )
284d1 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
284d2 20 20 2f 2a 20 54 61 62 6c 65 73 20 68 61 76 65    /* Tables have
284d3 20 64 69 66 66 65 72 65 6e 74 20 43 48 45 43 4b   different CHECK
284d4 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 54   constraints.  T
284d5 69 63 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a 20  icket #2252 */. 
284d6 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
284d7 49 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66  If we get this f
284d8 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 65 69 74  ar, it means eit
284d9 68 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  her:.  **.  **  
284da 20 20 2a 20 20 20 57 65 20 63 61 6e 20 61 6c 77    *   We can alw
284db 61 79 73 20 64 6f 20 74 68 65 20 74 72 61 6e 73  ays do the trans
284dc 66 65 72 20 69 66 20 74 68 65 20 74 61 62 6c 65  fer if the table
284dd 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 20 20 2a   contains an.  *
284de 2a 20 20 20 20 20 20 20 20 61 6e 20 69 6e 74 65  *        an inte
284df 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 0a  ger primary key.
284e0 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20    **.  **    *  
284e1 20 57 65 20 63 61 6e 20 63 6f 6e 64 69 74 69 6f   We can conditio
284e2 6e 61 6c 6c 79 20 64 6f 20 74 68 65 20 74 72 61  nally do the tra
284e3 6e 73 66 65 72 20 69 66 20 74 68 65 20 64 65 73  nsfer if the des
284e4 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20  tination.  **   
284e5 20 20 20 20 20 74 61 62 6c 65 20 69 73 20 65 6d       table is em
284e6 70 74 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66  pty..  */.#ifdef
284e7 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73   SQLITE_TEST.  s
284e8 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63  qlite3_xferopt_c
284e9 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
284ea 20 69 44 62 53 72 63 20 3d 20 73 71 6c 69 74 65   iDbSrc = sqlite
284eb 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
284ec 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2d  Parse->db, pSrc-
284ed 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d  >pSchema);.  v =
284ee 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
284ef 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
284f0 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65  e3CodeVerifySche
284f1 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 53 72  ma(pParse, iDbSr
284f2 63 29 3b 0a 20 20 69 53 72 63 20 3d 20 70 50 61  c);.  iSrc = pPa
284f3 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69  rse->nTab++;.  i
284f4 44 65 73 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e  Dest = pParse->n
284f5 54 61 62 2b 2b 3b 0a 20 20 72 65 67 41 75 74 6f  Tab++;.  regAuto
284f6 69 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67  inc = autoIncBeg
284f7 69 6e 28 70 50 61 72 73 65 2c 20 69 44 62 44 65  in(pParse, iDbDe
284f8 73 74 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 71  st, pDest);.  sq
284f9 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
284fa 50 61 72 73 65 2c 20 69 44 65 73 74 2c 20 69 44  Parse, iDest, iD
284fb 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20 4f 50  bDest, pDest, OP
284fc 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 69  _OpenWrite);.  i
284fd 66 28 20 28 70 44 65 73 74 2d 3e 69 50 4b 65 79  f( (pDest->iPKey
284fe 3c 30 20 26 26 20 70 44 65 73 74 2d 3e 70 49 6e  <0 && pDest->pIn
284ff 64 65 78 21 3d 30 29 20 7c 7c 20 64 65 73 74 48  dex!=0) || destH
28500 61 73 55 6e 69 71 75 65 49 64 78 20 29 7b 0a 20  asUniqueIdx ){. 
28501 20 20 20 2f 2a 20 49 66 20 74 61 62 6c 65 73 20     /* If tables 
28502 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 49  do not have an I
28503 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
28504 45 59 20 61 6e 64 20 74 68 65 72 65 0a 20 20 20  EY and there.   
28505 20 2a 2a 20 61 72 65 20 69 6e 64 69 63 65 73 20   ** are indices 
28506 74 6f 20 62 65 20 63 6f 70 69 65 64 20 61 6e 64  to be copied and
28507 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
28508 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20   is not empty,. 
28509 20 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f     ** we have to
2850a 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72   disallow the tr
2850b 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  ansfer optimizat
2850c 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 0a  ion because the.
2850d 20 20 20 20 2a 2a 20 74 68 65 20 72 6f 77 69 64      ** the rowid
2850e 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 77  s might change w
2850f 68 69 63 68 20 77 69 6c 6c 20 6d 65 73 73 20 75  hich will mess u
28510 70 20 69 6e 64 65 78 69 6e 67 2e 0a 20 20 20 20  p indexing..    
28511 2a 2a 0a 20 20 20 20 2a 2a 20 4f 72 20 69 66 20  **.    ** Or if 
28512 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
28513 68 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64  has a UNIQUE ind
28514 65 78 20 61 6e 64 20 69 73 20 6e 6f 74 20 65 6d  ex and is not em
28515 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20 61  pty,.    ** we a
28516 6c 73 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65  lso disallow the
28517 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
28518 7a 61 74 69 6f 6e 20 62 65 63 61 75 73 65 20 77  zation because w
28519 65 20 63 61 6e 6e 6f 74 0a 20 20 20 20 2a 2a 20  e cannot.    ** 
2851a 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20  insure that all 
2851b 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 75  entries in the u
2851c 6e 69 6f 6e 20 6f 66 20 44 45 53 54 20 61 6e 64  nion of DEST and
2851d 20 53 52 43 20 77 69 6c 6c 20 62 65 0a 20 20 20   SRC will be.   
2851e 20 2a 2a 20 75 6e 69 71 75 65 2e 0a 20 20 20 20   ** unique..    
2851f 2a 2f 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  */.    addr1 = s
28520 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28521 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
28522 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 65 6d  Dest, 0);.    em
28523 70 74 79 44 65 73 74 54 65 73 74 20 3d 20 73 71  ptyDestTest = sq
28524 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
28525 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30  v, OP_Goto, 0, 0
28526 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
28527 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
28528 64 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  dr1);.  }else{. 
28529 20 20 20 65 6d 70 74 79 44 65 73 74 54 65 73 74     emptyDestTest
2852a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 0;.  }.  sqli
2852b 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
2852c 72 73 65 2c 20 69 53 72 63 2c 20 69 44 62 53 72  rse, iSrc, iDbSr
2852d 63 2c 20 70 53 72 63 2c 20 4f 50 5f 4f 70 65 6e  c, pSrc, OP_Open
2852e 52 65 61 64 29 3b 0a 20 20 65 6d 70 74 79 53 72  Read);.  emptySr
2852f 63 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56  cTest = sqlite3V
28530 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28531 52 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29  Rewind, iSrc, 0)
28532 3b 0a 20 20 72 65 67 44 61 74 61 20 3d 20 73 71  ;.  regData = sq
28533 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
28534 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 52 6f  pParse);.  regRo
28535 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74  wid = sqlite3Get
28536 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
28537 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50  .  if( pDest->iP
28538 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 64  Key>=0 ){.    ad
28539 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
2853a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
2853b 77 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f  wid, iSrc, regRo
2853c 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72 32 20  wid);.    addr2 
2853d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2853e 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
2853f 73 74 73 2c 20 69 44 65 73 74 2c 20 30 2c 20 72  sts, iDest, 0, r
28540 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  egRowid);.    sq
28541 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
28542 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50 61  int(.        pPa
28543 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 22 50  rse, onError, "P
28544 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20  RIMARY KEY must 
28545 62 65 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53  be unique", P4_S
28546 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
28547 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
28548 76 2c 20 61 64 64 72 32 29 3b 0a 20 20 20 20 61  v, addr2);.    a
28549 75 74 6f 49 6e 63 53 74 65 70 28 70 50 61 72 73  utoIncStep(pPars
2854a 65 2c 20 72 65 67 41 75 74 6f 69 6e 63 2c 20 72  e, regAutoinc, r
2854b 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  egRowid);.  }els
2854c 65 20 69 66 28 20 70 44 65 73 74 2d 3e 70 49 6e  e if( pDest->pIn
2854d 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64  dex==0 ){.    ad
2854e 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
2854f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
28550 77 52 6f 77 69 64 2c 20 69 44 65 73 74 2c 20 72  wRowid, iDest, r
28551 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  egRowid);.  }els
28552 65 7b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  e{.    addr1 = s
28553 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28554 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53  (v, OP_Rowid, iS
28555 72 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  rc, regRowid);. 
28556 20 20 20 61 73 73 65 72 74 28 20 28 70 44 65 73     assert( (pDes
28557 74 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  t->tabFlags & TF
28558 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 3d  _Autoincrement)=
28559 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  =0 );.  }.  sqli
2855a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2855b 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20 69 53 72   OP_RowData, iSr
2855c 63 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20 73  c, regData);.  s
2855d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2855e 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69  (v, OP_Insert, i
2855f 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c 20 72  Dest, regData, r
28560 65 67 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69  egRowid);.  sqli
28561 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
28562 76 2c 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  v, OPFLAG_NCHANG
28563 45 7c 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57  E|OPFLAG_LASTROW
28564 49 44 7c 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  ID|OPFLAG_APPEND
28565 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
28566 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
28567 70 44 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 30 29  pDest->zName, 0)
28568 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
28569 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
2856a 2c 20 69 53 72 63 2c 20 61 64 64 72 31 29 3b 0a  , iSrc, addr1);.
2856b 20 20 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70    for(pDestIdx=p
2856c 44 65 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44  Dest->pIndex; pD
2856d 65 73 74 49 64 78 3b 20 70 44 65 73 74 49 64 78  estIdx; pDestIdx
2856e 3d 70 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74  =pDestIdx->pNext
2856f 29 7b 0a 20 20 20 20 66 6f 72 28 70 53 72 63 49  ){.    for(pSrcI
28570 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b  dx=pSrc->pIndex;
28571 20 41 4c 57 41 59 53 28 70 53 72 63 49 64 78 29   ALWAYS(pSrcIdx)
28572 3b 20 70 53 72 63 49 64 78 3d 70 53 72 63 49 64  ; pSrcIdx=pSrcId
28573 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
28574 20 69 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69   if( xferCompati
28575 62 6c 65 49 6e 64 65 78 28 70 44 65 73 74 49 64  bleIndex(pDestId
28576 78 2c 20 70 53 72 63 49 64 78 29 20 29 20 62 72  x, pSrcIdx) ) br
28577 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  eak;.    }.    a
28578 73 73 65 72 74 28 20 70 53 72 63 49 64 78 20 29  ssert( pSrcIdx )
28579 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2857a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
2857b 6f 73 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20  ose, iSrc, 0);. 
2857c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2857d 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
2857e 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20  , iDest, 0);.   
2857f 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49   pKey = sqlite3I
28580 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
28581 73 65 2c 20 70 53 72 63 49 64 78 29 3b 0a 20 20  se, pSrcIdx);.  
28582 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28583 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65  Op4(v, OP_OpenRe
28584 61 64 2c 20 69 53 72 63 2c 20 70 53 72 63 49 64  ad, iSrc, pSrcId
28585 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 53 72 63 2c  x->tnum, iDbSrc,
28586 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28587 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
28588 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  ey, P4_KEYINFO_H
28589 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62  ANDOFF);.    Vdb
2858a 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
2858b 22 2c 20 70 53 72 63 49 64 78 2d 3e 7a 4e 61 6d  ", pSrcIdx->zNam
2858c 65 29 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20  e));.    pKey = 
2858d 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
2858e 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 44 65 73  nfo(pParse, pDes
2858f 74 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  tIdx);.    sqlit
28590 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
28591 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 44  OP_OpenWrite, iD
28592 65 73 74 2c 20 70 44 65 73 74 49 64 78 2d 3e 74  est, pDestIdx->t
28593 6e 75 6d 2c 20 69 44 62 44 65 73 74 2c 0a 20 20  num, iDbDest,.  
28594 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28595 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c      (char*)pKey,
28596 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
28597 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  OFF);.    VdbeCo
28598 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20  mment((v, "%s", 
28599 70 44 65 73 74 49 64 78 2d 3e 7a 4e 61 6d 65 29  pDestIdx->zName)
2859a 29 3b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  );.    addr1 = s
2859b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2859c 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
2859d 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Src, 0);.    sql
2859e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2859f 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 69 53 72  , OP_RowKey, iSr
285a0 63 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20 20  c, regData);.   
285a1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
285a2 70 33 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p3(v, OP_IdxInse
285a3 72 74 2c 20 69 44 65 73 74 2c 20 72 65 67 44 61  rt, iDest, regDa
285a4 74 61 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  ta, 1);.    sqli
285a5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
285a6 20 4f 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20   OP_Next, iSrc, 
285a7 61 64 64 72 31 2b 31 29 3b 0a 20 20 20 20 73 71  addr1+1);.    sq
285a8 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
285a9 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d  e(v, addr1);.  }
285aa 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
285ab 6d 70 48 65 72 65 28 76 2c 20 65 6d 70 74 79 53  mpHere(v, emptyS
285ac 72 63 54 65 73 74 29 3b 0a 20 20 73 71 6c 69 74  rcTest);.  sqlit
285ad 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
285ae 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69  (pParse, regRowi
285af 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  d);.  sqlite3Rel
285b0 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
285b1 73 65 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20  se, regData);.  
285b2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
285b3 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
285b4 53 72 63 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  Src, 0);.  sqlit
285b5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
285b6 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c  OP_Close, iDest,
285b7 20 30 29 3b 0a 20 20 69 66 28 20 65 6d 70 74 79   0);.  if( empty
285b8 44 65 73 74 54 65 73 74 20 29 7b 0a 20 20 20 20  DestTest ){.    
285b9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
285ba 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  2(v, OP_Halt, SQ
285bb 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20  LITE_OK, 0);.   
285bc 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
285bd 48 65 72 65 28 76 2c 20 65 6d 70 74 79 44 65 73  Here(v, emptyDes
285be 74 54 65 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  tTest);.    sqli
285bf 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
285c0 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74   OP_Close, iDest
285c1 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
285c2 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
285c3 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d   return 1;.  }.}
285c4 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
285c5 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20  E_OMIT_XFER_OPT 
285c6 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
285c7 2a 2a 2a 20 45 6e 64 20 6f 66 20 69 6e 73 65 72  *** End of inser
285c8 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
285c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285cb 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
285cc 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c  *** Begin file l
285cd 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  egacy.c ********
285ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285d0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
285d1 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
285d2 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
285d3 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
285d4 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
285d5 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
285d6 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
285d7 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
285d8 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
285d9 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
285da 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
285db 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
285dc 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
285dd 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
285de 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
285df 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
285e0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
285e1 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
285e2 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
285e3 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
285e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61  **********.** Ma
285e8 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  in file for the 
285e9 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
285ea 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
285eb 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d   this file.** im
285ec 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67  plement the prog
285ed 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65  rammer interface
285ee 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e   to the library.
285ef 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a    Routines in.**
285f0 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65   other files are
285f1 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
285f2 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20  e by SQLite and 
285f3 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
285f4 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65   accessed by use
285f5 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  rs of the librar
285f6 79 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78  y..*/.../*.** Ex
285f7 65 63 75 74 65 20 53 51 4c 20 63 6f 64 65 2e 20  ecute SQL code. 
285f8 20 52 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74   Return one of t
285f9 68 65 20 53 51 4c 49 54 45 5f 20 73 75 63 63 65  he SQLITE_ succe
285fa 73 73 2f 66 61 69 6c 75 72 65 0a 2a 2a 20 63 6f  ss/failure.** co
285fb 64 65 73 2e 20 20 41 6c 73 6f 20 77 72 69 74 65  des.  Also write
285fc 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
285fd 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  e into memory ob
285fe 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d  tained from.** m
285ff 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65  alloc() and make
28600 20 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74   *pzErrMsg point
28601 20 74 6f 20 74 68 61 74 20 6d 65 73 73 61 67 65   to that message
28602 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
28603 51 4c 20 69 73 20 61 20 71 75 65 72 79 2c 20 74  QL is a query, t
28604 68 65 6e 20 66 6f 72 20 65 61 63 68 20 72 6f 77  hen for each row
28605 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 72 65   in the query re
28606 73 75 6c 74 0a 2a 2a 20 74 68 65 20 78 43 61 6c  sult.** the xCal
28607 6c 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e  lback() function
28608 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 70 41 72   is called.  pAr
28609 67 20 62 65 63 6f 6d 65 73 20 74 68 65 20 66 69  g becomes the fi
2860a 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
2860b 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29 2e 20  to xCallback(). 
2860c 20 49 66 20 78 43 61 6c 6c 62 61 63 6b 3d 4e 55   If xCallback=NU
2860d 4c 4c 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c 62  LL then no callb
2860e 61 63 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65  ack.** is invoke
2860f 64 2c 20 65 76 65 6e 20 66 6f 72 20 71 75 65 72  d, even for quer
28610 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ies..*/.SQLITE_A
28611 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
28612 78 65 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  xec(.  sqlite3 *
28613 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
28614 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
28615 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  se on which the 
28616 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a  SQL executes */.
28617 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
28618 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ql,           /*
28619 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65   The SQL to be e
2861a 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 73 71 6c  xecuted */.  sql
2861b 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 20 78 43  ite3_callback xC
2861c 61 6c 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e 76 6f  allback, /* Invo
2861d 6b 65 20 74 68 69 73 20 63 61 6c 6c 62 61 63 6b  ke this callback
2861e 20 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 76 6f   routine */.  vo
2861f 69 64 20 2a 70 41 72 67 2c 20 20 20 20 20 20 20  id *pArg,       
28620 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
28621 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78  st argument to x
28622 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a 20 20  Callback() */.  
28623 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20  char **pzErrMsg 
28624 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
28625 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61  rite error messa
28626 67 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ges here */.){. 
28627 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
28628 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  _OK;         /* 
28629 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
2862a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65   const char *zLe
2862b 66 74 6f 76 65 72 3b 20 20 20 20 20 20 2f 2a 20  ftover;      /* 
2862c 54 61 69 6c 20 6f 66 20 75 6e 70 72 6f 63 65 73  Tail of unproces
2862d 73 65 64 20 53 51 4c 20 2a 2f 0a 20 20 73 71 6c  sed SQL */.  sql
2862e 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
2862f 20 3d 20 30 3b 20 20 20 20 2f 2a 20 54 68 65 20   = 0;    /* The 
28630 63 75 72 72 65 6e 74 20 53 51 4c 20 73 74 61 74  current SQL stat
28631 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20  ement */.  char 
28632 2a 2a 61 7a 43 6f 6c 73 20 3d 20 30 3b 20 20 20  **azCols = 0;   
28633 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 20         /* Names 
28634 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
28635 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 74 72  s */.  int nRetr
28636 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  y = 0;          
28637 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
28638 72 65 74 72 79 20 61 74 74 65 6d 70 74 73 20 2a  retry attempts *
28639 2f 0a 20 20 69 6e 74 20 63 61 6c 6c 62 61 63 6b  /.  int callback
2863a 49 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  IsInit;         
2863b 2f 2a 20 54 72 75 65 20 69 66 20 63 61 6c 6c 62  /* True if callb
2863c 61 63 6b 20 64 61 74 61 20 69 73 20 69 6e 69 74  ack data is init
2863d 69 61 6c 69 7a 65 64 20 2a 2f 0a 0a 20 20 69 66  ialized */..  if
2863e 28 20 7a 53 71 6c 3d 3d 30 20 29 20 7a 53 71 6c  ( zSql==0 ) zSql
2863f 20 3d 20 22 22 3b 0a 0a 20 20 73 71 6c 69 74 65   = "";..  sqlite
28640 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
28641 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69  ->mutex);.  sqli
28642 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
28643 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 77 68  ITE_OK, 0);.  wh
28644 69 6c 65 28 20 28 72 63 3d 3d 53 51 4c 49 54 45  ile( (rc==SQLITE
28645 5f 4f 4b 20 7c 7c 20 28 72 63 3d 3d 53 51 4c 49  _OK || (rc==SQLI
28646 54 45 5f 53 43 48 45 4d 41 20 26 26 20 28 2b 2b  TE_SCHEMA && (++
28647 6e 52 65 74 72 79 29 3c 32 29 29 20 26 26 20 7a  nRetry)<2)) && z
28648 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e  Sql[0] ){.    in
28649 74 20 6e 43 6f 6c 3b 0a 20 20 20 20 63 68 61 72  t nCol;.    char
2864a 20 2a 2a 61 7a 56 61 6c 73 20 3d 20 30 3b 0a 0a   **azVals = 0;..
2864b 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20      pStmt = 0;. 
2864c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
2864d 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c  prepare(db, zSql
2864e 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 26 7a  , -1, &pStmt, &z
2864f 4c 65 66 74 6f 76 65 72 29 3b 0a 20 20 20 20 61  Leftover);.    a
28650 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
28651 45 5f 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d 30  E_OK || pStmt==0
28652 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d   );.    if( rc!=
28653 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
28654 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
28655 20 7d 0a 20 20 20 20 69 66 28 20 21 70 53 74 6d   }.    if( !pStm
28656 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 74 68  t ){.      /* th
28657 69 73 20 68 61 70 70 65 6e 73 20 66 6f 72 20 61  is happens for a
28658 20 63 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69 74   comment or whit
28659 65 2d 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  e-space */.     
2865a 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65   zSql = zLeftove
2865b 72 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  r;.      continu
2865c 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61  e;.    }..    ca
2865d 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 3d 20 30  llbackIsInit = 0
2865e 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 73 71 6c  ;.    nCol = sql
2865f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
28660 74 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 77  t(pStmt);..    w
28661 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 20  hile( 1 ){.     
28662 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 72 63   int i;.      rc
28663 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
28664 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20 2f  pStmt);..      /
28665 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c  * Invoke the cal
28666 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69  lback function i
28667 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  f required */.  
28668 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63      if( xCallbac
28669 6b 20 26 26 20 28 53 51 4c 49 54 45 5f 52 4f 57  k && (SQLITE_ROW
2866a 3d 3d 72 63 20 7c 7c 20 0a 20 20 20 20 20 20 20  ==rc || .       
2866b 20 20 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45 3d     (SQLITE_DONE=
2866c 3d 72 63 20 26 26 20 21 63 61 6c 6c 62 61 63 6b  =rc && !callback
2866d 49 73 49 6e 69 74 0a 20 20 20 20 20 20 20 20 20  IsInit.         
2866e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2866f 20 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53    && db->flags&S
28670 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61  QLITE_NullCallba
28671 63 6b 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ck)) ){.        
28672 69 66 28 20 21 63 61 6c 6c 62 61 63 6b 49 73 49  if( !callbackIsI
28673 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  nit ){.         
28674 20 61 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65   azCols = sqlite
28675 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
28676 2c 20 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28  , 2*nCol*sizeof(
28677 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 2b 20 31  const char*) + 1
28678 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
28679 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20 20   azCols==0 ){.  
2867a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
2867b 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  xec_out;.       
2867c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 66     }.          f
2867d 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
2867e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
2867f 20 20 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28 63    azCols[i] = (c
28680 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f  har *)sqlite3_co
28681 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c  lumn_name(pStmt,
28682 20 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   i);.           
28683 20 2f 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53   /* sqlite3VdbeS
28684 65 74 43 6f 6c 4e 61 6d 65 28 29 20 69 6e 73 74  etColName() inst
28685 61 6c 6c 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  alls column name
28686 73 20 61 73 20 55 54 46 38 0a 20 20 20 20 20 20  s as UTF8.      
28687 20 20 20 20 20 20 2a 2a 20 73 74 72 69 6e 67 73        ** strings
28688 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20   so there is no 
28689 77 61 79 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  way for sqlite3_
2868a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 74 6f  column_name() to
2868b 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20   fail. */.      
2868c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 7a        assert( az
2868d 43 6f 6c 73 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  Cols[i]!=0 );.  
2868e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2868f 20 20 20 20 63 61 6c 6c 62 61 63 6b 49 73 49 6e      callbackIsIn
28690 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  it = 1;.        
28691 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  }.        if( rc
28692 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
28693 20 20 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73            azVals
28694 20 3d 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d   = &azCols[nCol]
28695 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
28696 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
28697 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
28698 7a 56 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72  zVals[i] = (char
28699 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   *)sqlite3_colum
2869a 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 69 29  n_text(pStmt, i)
2869b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
2869c 28 20 21 61 7a 56 61 6c 73 5b 69 5d 20 26 26 20  ( !azVals[i] && 
2869d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2869e 79 70 65 28 70 53 74 6d 74 2c 20 69 29 21 3d 53  ype(pStmt, i)!=S
2869f 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20  QLITE_NULL ){.  
286a0 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
286a1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
286a2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
286a3 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20  goto exec_out;. 
286a4 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
286a5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
286a6 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78   }.        if( x
286a7 43 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e  Callback(pArg, n
286a8 43 6f 6c 2c 20 61 7a 56 61 6c 73 2c 20 61 7a 43  Col, azVals, azC
286a9 6f 6c 73 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ols) ){.        
286aa 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42    rc = SQLITE_AB
286ab 4f 52 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ORT;.          s
286ac 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
286ad 7a 65 28 28 56 64 62 65 20 2a 29 70 53 74 6d 74  ze((Vdbe *)pStmt
286ae 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 74  );.          pSt
286af 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  mt = 0;.        
286b0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
286b1 62 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2c  b, SQLITE_ABORT,
286b2 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67   0);.          g
286b3 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20  oto exec_out;.  
286b4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
286b5 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
286b6 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
286b7 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
286b8 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56  3VdbeFinalize((V
286b9 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20  dbe *)pStmt);.  
286ba 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b        pStmt = 0;
286bb 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
286bc 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29  =SQLITE_SCHEMA )
286bd 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 52 65 74  {.          nRet
286be 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ry = 0;.        
286bf 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76    zSql = zLeftov
286c0 65 72 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68  er;.          wh
286c1 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70  ile( sqlite3Issp
286c2 61 63 65 28 7a 53 71 6c 5b 30 5d 29 20 29 20 7a  ace(zSql[0]) ) z
286c3 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  Sql++;.        }
286c4 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
286c5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
286c6 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
286c7 28 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 20 20  (db, azCols);.  
286c8 20 20 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20 20    azCols = 0;.  
286c9 7d 0a 0a 65 78 65 63 5f 6f 75 74 3a 0a 20 20 69  }..exec_out:.  i
286ca 66 28 20 70 53 74 6d 74 20 29 20 73 71 6c 69 74  f( pStmt ) sqlit
286cb 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28  e3VdbeFinalize((
286cc 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20  Vdbe *)pStmt);. 
286cd 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
286ce 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20 72  b, azCols);..  r
286cf 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
286d0 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 69 66  it(db, rc);.  if
286d1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
286d2 26 26 20 41 4c 57 41 59 53 28 72 63 3d 3d 73 71  && ALWAYS(rc==sq
286d3 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62  lite3_errcode(db
286d4 29 29 20 26 26 20 70 7a 45 72 72 4d 73 67 20 29  )) && pzErrMsg )
286d5 7b 0a 20 20 20 20 69 6e 74 20 6e 45 72 72 4d 73  {.    int nErrMs
286d6 67 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 53  g = 1 + sqlite3S
286d7 74 72 6c 65 6e 33 30 28 73 71 6c 69 74 65 33 5f  trlen30(sqlite3_
286d8 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20  errmsg(db));.   
286d9 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c   *pzErrMsg = sql
286da 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 45 72 72 4d  ite3Malloc(nErrM
286db 73 67 29 3b 0a 20 20 20 20 69 66 28 20 2a 70 7a  sg);.    if( *pz
286dc 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
286dd 6d 65 6d 63 70 79 28 2a 70 7a 45 72 72 4d 73 67  memcpy(*pzErrMsg
286de 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
286df 28 64 62 29 2c 20 6e 45 72 72 4d 73 67 29 3b 0a  (db), nErrMsg);.
286e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
286e1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
286e2 45 4d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  EM;.      sqlite
286e3 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
286e4 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20  E_NOMEM, 0);.   
286e5 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70   }.  }else if( p
286e6 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 2a  zErrMsg ){.    *
286e7 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  pzErrMsg = 0;.  
286e8 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72 63  }..  assert( (rc
286e9 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72  &db->errMask)==r
286ea 63 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  c );.  sqlite3_m
286eb 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
286ec 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
286ed 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  rc;.}../********
286ee 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 65  ****** End of le
286ef 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  gacy.c *********
286f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
286f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
286f2 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
286f3 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
286f4 65 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a  e loadext.c ****
286f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
286f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
286f7 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
286f8 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20 54  6 June 7.**.** T
286f9 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
286fa 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
286fb 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
286fc 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
286fd 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
286fe 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
286ff 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
28700 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
28701 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
28702 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
28703 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
28704 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
28705 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
28706 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
28707 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
28708 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
28709 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2870a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2870b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2870c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2870d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2870e 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
2870f 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
28710 64 65 20 75 73 65 64 20 74 6f 20 64 79 6e 61 6d  de used to dynam
28711 69 63 61 6c 6c 79 20 6c 6f 61 64 20 65 78 74 65  ically load exte
28712 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 74  nsions into.** t
28713 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
28714 79 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  y..*/..#ifndef S
28715 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 23 64 65  QLITE_CORE.  #de
28716 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  fine SQLITE_CORE
28717 20 31 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 74   1  /* Disable t
28718 68 65 20 41 50 49 20 72 65 64 65 66 69 6e 69 74  he API redefinit
28719 69 6f 6e 20 69 6e 20 73 71 6c 69 74 65 33 65 78  ion in sqlite3ex
2871a 74 2e 68 20 2a 2f 0a 23 65 6e 64 69 66 0a 2f 2a  t.h */.#endif./*
2871b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
2871c 63 6c 75 64 65 20 73 71 6c 69 74 65 33 65 78 74  clude sqlite3ext
2871d 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
2871e 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a   of loadext.c **
2871f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
28720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
28721 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33  gin file sqlite3
28722 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ext.h **********
28723 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28724 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
28725 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20 37 0a  .** 2006 June 7.
28726 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
28727 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
28728 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
28729 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2872a 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2872b 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2872c 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2872d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2872e 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2872f 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
28730 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
28731 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
28732 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
28733 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
28734 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
28735 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
28736 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
28737 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
28738 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28739 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2873a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2873b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2873c 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
2873d 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 53  le defines the S
2873e 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 20  QLite interface 
2873f 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a 20 73 68  for use by.** sh
28740 61 72 65 64 20 6c 69 62 72 61 72 69 65 73 20 74  ared libraries t
28741 68 61 74 20 77 61 6e 74 20 74 6f 20 62 65 20 69  hat want to be i
28742 6d 70 6f 72 74 65 64 20 61 73 20 65 78 74 65 6e  mported as exten
28743 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 61 6e  sions into.** an
28744 20 53 51 4c 69 74 65 20 69 6e 73 74 61 6e 63 65   SQLite instance
28745 2e 20 20 53 68 61 72 65 64 20 6c 69 62 72 61 72  .  Shared librar
28746 69 65 73 20 74 68 61 74 20 69 6e 74 65 6e 64 20  ies that intend 
28747 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a 2a 2a 20  to be loaded.** 
28748 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20 62 79  as extensions by
28749 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 23   SQLite should #
2874a 69 6e 63 6c 75 64 65 20 74 68 69 73 20 66 69 6c  include this fil
2874b 65 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 2a 2a  e instead of .**
2874c 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2f 0a 23   sqlite3.h..*/.#
2874d 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 45  ifndef _SQLITE3E
2874e 58 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53  XT_H_.#define _S
2874f 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a 0a 74 79  QLITE3EXT_H_..ty
28750 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
28751 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
28752 73 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  s sqlite3_api_ro
28753 75 74 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54  utines;../*.** T
28754 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
28755 75 63 74 75 72 65 20 68 6f 6c 64 73 20 70 6f 69  ucture holds poi
28756 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 6f 66 20  nters to all of 
28757 74 68 65 20 53 51 4c 69 74 65 20 41 50 49 0a 2a  the SQLite API.*
28758 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a  * routines..**.*
28759 2a 20 57 41 52 4e 49 4e 47 3a 20 20 49 6e 20 6f  * WARNING:  In o
2875a 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e  rder to maintain
2875b 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
2875c 74 69 62 69 6c 69 74 79 2c 20 61 64 64 20 6e 65  tibility, add ne
2875d 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20  w.** interfaces 
2875e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
2875f 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c  is structure onl
28760 79 2e 20 20 49 66 20 79 6f 75 20 69 6e 73 65 72  y.  If you inser
28761 74 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61  t new.** interfa
28762 63 65 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  ces in the middl
28763 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
28764 75 72 65 2c 20 74 68 65 6e 20 6f 6c 64 65 72 20  ure, then older 
28765 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 76 65 72  different.** ver
28766 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
28767 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65  will not be able
28768 20 74 6f 20 6c 6f 61 64 20 65 61 63 68 20 6f 74   to load each ot
28769 68 65 72 73 27 20 73 68 61 72 65 64 0a 2a 2a 20  hers' shared.** 
2876a 6c 69 62 72 61 72 69 65 73 21 0a 2a 2f 0a 73 74  libraries!.*/.st
2876b 72 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69  ruct sqlite3_api
2876c 5f 72 6f 75 74 69 6e 65 73 20 7b 0a 20 20 76 6f  _routines {.  vo
2876d 69 64 20 2a 20 28 2a 61 67 67 72 65 67 61 74 65  id * (*aggregate
2876e 5f 63 6f 6e 74 65 78 74 29 28 73 71 6c 69 74 65  _context)(sqlite
2876f 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20 6e  3_context*,int n
28770 42 79 74 65 73 29 3b 0a 20 20 69 6e 74 20 20 28  Bytes);.  int  (
28771 2a 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  *aggregate_count
28772 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
28773 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  t*);.  int  (*bi
28774 6e 64 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  nd_blob)(sqlite3
28775 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74  _stmt*,int,const
28776 20 76 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f 69   void*,int n,voi
28777 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
28778 69 6e 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75 62  int  (*bind_doub
28779 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  le)(sqlite3_stmt
2877a 2a 2c 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0a 20  *,int,double);. 
2877b 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74   int  (*bind_int
2877c 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
2877d 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  int,int);.  int 
2877e 20 28 2a 62 69 6e 64 5f 69 6e 74 36 34 29 28 73   (*bind_int64)(s
2877f 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
28780 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a  ,sqlite_int64);.
28781 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 6e 75    int  (*bind_nu
28782 6c 6c 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ll)(sqlite3_stmt
28783 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  *,int);.  int  (
28784 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  *bind_parameter_
28785 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73  count)(sqlite3_s
28786 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  tmt*);.  int  (*
28787 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
28788 6e 64 65 78 29 28 73 71 6c 69 74 65 33 5f 73 74  ndex)(sqlite3_st
28789 6d 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 7a  mt*,const char*z
2878a 4e 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20 63  Name);.  const c
2878b 68 61 72 20 2a 20 28 2a 62 69 6e 64 5f 70 61 72  har * (*bind_par
2878c 61 6d 65 74 65 72 5f 6e 61 6d 65 29 28 73 71 6c  ameter_name)(sql
2878d 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
2878e 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74  .  int  (*bind_t
2878f 65 78 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ext)(sqlite3_stm
28790 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  t*,int,const cha
28791 72 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29  r*,int n,void(*)
28792 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20  (void*));.  int 
28793 20 28 2a 62 69 6e 64 5f 74 65 78 74 31 36 29 28   (*bind_text16)(
28794 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
28795 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
28796 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
28797 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
28798 5f 76 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f  _value)(sqlite3_
28799 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  stmt*,int,const 
2879a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2879b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f 68  .  int  (*busy_h
2879c 61 6e 64 6c 65 72 29 28 73 71 6c 69 74 65 33 2a  andler)(sqlite3*
2879d 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e  ,int(*)(void*,in
2879e 74 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74  t),void*);.  int
2879f 20 20 28 2a 62 75 73 79 5f 74 69 6d 65 6f 75 74    (*busy_timeout
287a0 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 6d  )(sqlite3*,int m
287a1 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 68 61  s);.  int  (*cha
287a2 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a 29 3b  nges)(sqlite3*);
287a3 0a 20 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65 29  .  int  (*close)
287a4 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e  (sqlite3*);.  in
287a5 74 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  t  (*collation_n
287a6 65 65 64 65 64 29 28 73 71 6c 69 74 65 33 2a 2c  eeded)(sqlite3*,
287a7 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f  void*,void(*)(vo
287a8 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
287a9 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
287aa 63 68 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20 20  char*));.  int  
287ab 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  (*collation_need
287ac 65 64 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 76  ed16)(sqlite3*,v
287ad 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  oid*,void(*)(voi
287ae 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
287af 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76  eTextRep,const v
287b0 6f 69 64 2a 29 29 3b 0a 20 20 63 6f 6e 73 74 20  oid*));.  const 
287b1 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  void * (*column_
287b2 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74  blob)(sqlite3_st
287b3 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
287b4 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62   int  (*column_b
287b5 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f 73 74  ytes)(sqlite3_st
287b6 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
287b7 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62   int  (*column_b
287b8 79 74 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f  ytes16)(sqlite3_
287b9 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b  stmt*,int iCol);
287ba 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e  .  int  (*column
287bb 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f  _count)(sqlite3_
287bc 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 63  stmt*pStmt);.  c
287bd 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f  onst char * (*co
287be 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
287bf 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  me)(sqlite3_stmt
287c0 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
287c1 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  void * (*column_
287c2 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 29  database_name16)
287c3 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
287c4 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  nt);.  const cha
287c5 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63  r * (*column_dec
287c6 6c 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73  ltype)(sqlite3_s
287c7 74 6d 74 2a 2c 69 6e 74 20 69 29 3b 0a 20 20 63  tmt*,int i);.  c
287c8 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f  onst void * (*co
287c9 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 29  lumn_decltype16)
287ca 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
287cb 6e 74 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28  nt);.  double  (
287cc 2a 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 29 28  *column_double)(
287cd 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
287ce 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20  t iCol);.  int  
287cf 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28 73 71  (*column_int)(sq
287d0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
287d1 69 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 5f  iCol);.  sqlite_
287d2 69 6e 74 36 34 20 20 28 2a 63 6f 6c 75 6d 6e 5f  int64  (*column_
287d3 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73  int64)(sqlite3_s
287d4 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
287d5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28    const char * (
287d6 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28 73 71  *column_name)(sq
287d7 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
287d8 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ;.  const void *
287d9 20 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36   (*column_name16
287da 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
287db 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  int);.  const ch
287dc 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72  ar * (*column_or
287dd 69 67 69 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74  igin_name)(sqlit
287de 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
287df 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
287e0 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
287e1 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  me16)(sqlite3_st
287e2 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73  mt*,int);.  cons
287e3 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d  t char * (*colum
287e4 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 28 73 71  n_table_name)(sq
287e5 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
287e6 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ;.  const void *
287e7 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f   (*column_table_
287e8 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f  name16)(sqlite3_
287e9 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f  stmt*,int);.  co
287ea 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
287eb 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78  r * (*column_tex
287ec 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t)(sqlite3_stmt*
287ed 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f  ,int iCol);.  co
287ee 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c  nst void * (*col
287ef 75 6d 6e 5f 74 65 78 74 31 36 29 28 73 71 6c 69  umn_text16)(sqli
287f0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
287f1 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  ol);.  int  (*co
287f2 6c 75 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69 74  lumn_type)(sqlit
287f3 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f  e3_stmt*,int iCo
287f4 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  l);.  sqlite3_va
287f5 6c 75 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76 61  lue* (*column_va
287f6 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  lue)(sqlite3_stm
287f7 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
287f8 76 6f 69 64 20 2a 20 28 2a 63 6f 6d 6d 69 74 5f  void * (*commit_
287f9 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 69  hook)(sqlite3*,i
287fa 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69  nt(*)(void*),voi
287fb 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  d*);.  int  (*co
287fc 6d 70 6c 65 74 65 29 28 63 6f 6e 73 74 20 63 68  mplete)(const ch
287fd 61 72 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20  ar*sql);.  int  
287fe 28 2a 63 6f 6d 70 6c 65 74 65 31 36 29 28 63 6f  (*complete16)(co
287ff 6e 73 74 20 76 6f 69 64 2a 73 71 6c 29 3b 0a 20  nst void*sql);. 
28800 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 63   int  (*create_c
28801 6f 6c 6c 61 74 69 6f 6e 29 28 73 71 6c 69 74 65  ollation)(sqlite
28802 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
28803 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28  nt,void*,int(*)(
28804 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
28805 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
28806 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20  void*));.  int  
28807 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  (*create_collati
28808 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63  on16)(sqlite3*,c
28809 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
2880a 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64  oid*,int(*)(void
2880b 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2880c 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2880d 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72  *));.  int  (*cr
2880e 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 29 28 73  eate_function)(s
2880f 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
28810 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69 64  ar*,int,int,void
28811 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28  *,void (*xFunc)(
28812 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28813 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
28814 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53 74  ue**),void (*xSt
28815 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
28816 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
28817 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28  _value**),void (
28818 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33  *xFinal)(sqlite3
28819 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20 69  _context*));.  i
2881a 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e  nt  (*create_fun
2881b 63 74 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33  ction16)(sqlite3
2881c 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
2881d 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64  t,int,void*,void
2881e 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
2881f 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
28820 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
28821 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
28822 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
28823 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
28824 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61  **),void (*xFina
28825 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
28826 78 74 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 63  xt*));.  int (*c
28827 72 65 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73 71  reate_module)(sq
28828 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
28829 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  r*,const sqlite3
2882a 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29 3b  _module*,void*);
2882b 0a 20 20 69 6e 74 20 20 28 2a 64 61 74 61 5f 63  .  int  (*data_c
2882c 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74  ount)(sqlite3_st
2882d 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c  mt*pStmt);.  sql
2882e 69 74 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e 64  ite3 * (*db_hand
2882f 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  le)(sqlite3_stmt
28830 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63 6c  *);.  int (*decl
28831 61 72 65 5f 76 74 61 62 29 28 73 71 6c 69 74 65  are_vtab)(sqlite
28832 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  3*,const char*);
28833 0a 20 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c 65  .  int  (*enable
28834 5f 73 68 61 72 65 64 5f 63 61 63 68 65 29 28 69  _shared_cache)(i
28835 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 72  nt);.  int  (*er
28836 72 63 6f 64 65 29 28 73 71 6c 69 74 65 33 2a 64  rcode)(sqlite3*d
28837 62 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b);.  const char
28838 20 2a 20 28 2a 65 72 72 6d 73 67 29 28 73 71 6c   * (*errmsg)(sql
28839 69 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  ite3*);.  const 
2883a 76 6f 69 64 20 2a 20 28 2a 65 72 72 6d 73 67 31  void * (*errmsg1
2883b 36 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  6)(sqlite3*);.  
2883c 69 6e 74 20 20 28 2a 65 78 65 63 29 28 73 71 6c  int  (*exec)(sql
2883d 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
2883e 2a 2c 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61  *,sqlite3_callba
2883f 63 6b 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a 29  ck,void*,char**)
28840 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78 70 69 72  ;.  int  (*expir
28841 65 64 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ed)(sqlite3_stmt
28842 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69 6e  *);.  int  (*fin
28843 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 73  alize)(sqlite3_s
28844 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f  tmt*pStmt);.  vo
28845 69 64 20 20 28 2a 66 72 65 65 29 28 76 6f 69 64  id  (*free)(void
28846 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72  *);.  void  (*fr
28847 65 65 5f 74 61 62 6c 65 29 28 63 68 61 72 2a 2a  ee_table)(char**
28848 72 65 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20 20  result);.  int  
28849 28 2a 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  (*get_autocommit
2884a 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76  )(sqlite3*);.  v
2884b 6f 69 64 20 2a 20 28 2a 67 65 74 5f 61 75 78 64  oid * (*get_auxd
2884c 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ata)(sqlite3_con
2884d 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  text*,int);.  in
2884e 74 20 20 28 2a 67 65 74 5f 74 61 62 6c 65 29 28  t  (*get_table)(
2884f 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
28850 68 61 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e 74  har*,char***,int
28851 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b 0a  *,int*,char**);.
28852 20 20 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c 5f    int  (*global_
28853 72 65 63 6f 76 65 72 29 28 76 6f 69 64 29 3b 0a  recover)(void);.
28854 20 20 76 6f 69 64 20 20 28 2a 69 6e 74 65 72 72    void  (*interr
28855 75 70 74 78 29 28 73 71 6c 69 74 65 33 2a 29 3b  uptx)(sqlite3*);
28856 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
28857 20 28 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72   (*last_insert_r
28858 6f 77 69 64 29 28 73 71 6c 69 74 65 33 2a 29 3b  owid)(sqlite3*);
28859 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
2885a 28 2a 6c 69 62 76 65 72 73 69 6f 6e 29 28 76 6f  (*libversion)(vo
2885b 69 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c 69  id);.  int  (*li
2885c 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 29  bversion_number)
2885d 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 2a  (void);.  void *
2885e 28 2a 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a  (*malloc)(int);.
2885f 20 20 63 68 61 72 20 2a 20 28 2a 6d 70 72 69 6e    char * (*mprin
28860 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  tf)(const char*,
28861 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f  ...);.  int  (*o
28862 70 65 6e 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  pen)(const char*
28863 2c 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69  ,sqlite3**);.  i
28864 6e 74 20 20 28 2a 6f 70 65 6e 31 36 29 28 63 6f  nt  (*open16)(co
28865 6e 73 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74 65  nst void*,sqlite
28866 33 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70  3**);.  int  (*p
28867 72 65 70 61 72 65 29 28 73 71 6c 69 74 65 33 2a  repare)(sqlite3*
28868 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
28869 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c  ,sqlite3_stmt**,
2886a 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20  const char**);. 
2886b 20 69 6e 74 20 20 28 2a 70 72 65 70 61 72 65 31   int  (*prepare1
2886c 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  6)(sqlite3*,cons
2886d 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69  t void*,int,sqli
2886e 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74  te3_stmt**,const
2886f 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69 64   void**);.  void
28870 20 2a 20 28 2a 70 72 6f 66 69 6c 65 29 28 73 71   * (*profile)(sq
28871 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76  lite3*,void(*)(v
28872 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  oid*,const char*
28873 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c  ,sqlite_uint64),
28874 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20  void*);.  void  
28875 28 2a 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  (*progress_handl
28876 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  er)(sqlite3*,int
28877 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76  ,int(*)(void*),v
28878 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28  oid*);.  void *(
28879 2a 72 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c  *realloc)(void*,
2887a 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 72  int);.  int  (*r
2887b 65 73 65 74 29 28 73 71 6c 69 74 65 33 5f 73 74  eset)(sqlite3_st
2887c 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69  mt*pStmt);.  voi
2887d 64 20 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f 62  d  (*result_blob
2887e 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
2887f 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  t*,const void*,i
28880 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  nt,void(*)(void*
28881 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65  ));.  void  (*re
28882 73 75 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71 6c  sult_double)(sql
28883 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64 6f  ite3_context*,do
28884 75 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20 28  uble);.  void  (
28885 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 29 28 73  *result_error)(s
28886 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
28887 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29  const char*,int)
28888 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
28889 6c 74 5f 65 72 72 6f 72 31 36 29 28 73 71 6c 69  lt_error16)(sqli
2888a 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e  te3_context*,con
2888b 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20  st void*,int);. 
2888c 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f   void  (*result_
2888d 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  int)(sqlite3_con
2888e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f  text*,int);.  vo
2888f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74  id  (*result_int
28890 36 34 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  64)(sqlite3_cont
28891 65 78 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36  ext*,sqlite_int6
28892 34 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65  4);.  void  (*re
28893 73 75 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69 74  sult_null)(sqlit
28894 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20  e3_context*);.  
28895 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74  void  (*result_t
28896 65 78 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ext)(sqlite3_con
28897 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72  text*,const char
28898 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f  *,int,void(*)(vo
28899 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28  id*));.  void  (
2889a 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36 29 28  *result_text16)(
2889b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2889c 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
2889d 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
2889e 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
2889f 6c 74 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c  lt_text16be)(sql
288a0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f  ite3_context*,co
288a1 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f  nst void*,int,vo
288a2 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20  id(*)(void*));. 
288a3 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f   void  (*result_
288a4 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65  text16le)(sqlite
288a5 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74  3_context*,const
288a6 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28   void*,int,void(
288a7 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f  *)(void*));.  vo
288a8 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 76 61 6c  id  (*result_val
288a9 75 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ue)(sqlite3_cont
288aa 65 78 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ext*,sqlite3_val
288ab 75 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28  ue*);.  void * (
288ac 2a 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28  *rollback_hook)(
288ad 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29  sqlite3*,void(*)
288ae 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a  (void*),void*);.
288af 20 20 69 6e 74 20 20 28 2a 73 65 74 5f 61 75 74    int  (*set_aut
288b0 68 6f 72 69 7a 65 72 29 28 73 71 6c 69 74 65 33  horizer)(sqlite3
288b1 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69  *,int(*)(void*,i
288b2 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  nt,const char*,c
288b3 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
288b4 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
288b5 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f  r*),void*);.  vo
288b6 69 64 20 20 28 2a 73 65 74 5f 61 75 78 64 61 74  id  (*set_auxdat
288b7 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  a)(sqlite3_conte
288b8 78 74 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f  xt*,int,void*,vo
288b9 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  id (*)(void*));.
288ba 20 20 63 68 61 72 20 2a 20 28 2a 73 6e 70 72 69    char * (*snpri
288bb 6e 74 66 29 28 69 6e 74 2c 63 68 61 72 2a 2c 63  ntf)(int,char*,c
288bc 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
288bd 0a 20 20 69 6e 74 20 20 28 2a 73 74 65 70 29 28  .  int  (*step)(
288be 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
288bf 20 20 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f 63    int  (*table_c
288c0 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29 28  olumn_metadata)(
288c1 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
288c2 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
288c3 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61  ,const char*,cha
288c4 72 20 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20 63  r const**,char c
288c5 6f 6e 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a  onst**,int*,int*
288c6 2c 69 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20  ,int*);.  void  
288c7 28 2a 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70  (*thread_cleanup
288c8 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20  )(void);.  int  
288c9 28 2a 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 29  (*total_changes)
288ca 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f  (sqlite3*);.  vo
288cb 69 64 20 2a 20 28 2a 74 72 61 63 65 29 28 73 71  id * (*trace)(sq
288cc 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 78 54 72  lite3*,void(*xTr
288cd 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ace)(void*,const
288ce 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a   char*),void*);.
288cf 20 20 69 6e 74 20 20 28 2a 74 72 61 6e 73 66 65    int  (*transfe
288d0 72 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69  r_bindings)(sqli
288d1 74 65 33 5f 73 74 6d 74 2a 2c 73 71 6c 69 74 65  te3_stmt*,sqlite
288d2 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 76 6f 69 64  3_stmt*);.  void
288d3 20 2a 20 28 2a 75 70 64 61 74 65 5f 68 6f 6f 6b   * (*update_hook
288d4 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28  )(sqlite3*,void(
288d5 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 20 2c 63 68  *)(void*,int ,ch
288d6 61 72 20 63 6f 6e 73 74 2a 2c 63 68 61 72 20 63  ar const*,char c
288d7 6f 6e 73 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74  onst*,sqlite_int
288d8 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f  64),void*);.  vo
288d9 69 64 20 2a 20 28 2a 75 73 65 72 5f 64 61 74 61  id * (*user_data
288da 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
288db 74 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69  t*);.  const voi
288dc 64 20 2a 20 28 2a 76 61 6c 75 65 5f 62 6c 6f 62  d * (*value_blob
288dd 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
288de 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75  );.  int  (*valu
288df 65 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65 33  e_bytes)(sqlite3
288e0 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
288e1 20 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 31 36   (*value_bytes16
288e2 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
288e3 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 76  );.  double  (*v
288e4 61 6c 75 65 5f 64 6f 75 62 6c 65 29 28 73 71 6c  alue_double)(sql
288e5 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
288e6 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74  int  (*value_int
288e7 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
288e8 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  );.  sqlite_int6
288e9 34 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 36 34  4  (*value_int64
288ea 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
288eb 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75  );.  int  (*valu
288ec 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 29 28  e_numeric_type)(
288ed 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
288ee 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
288ef 64 20 63 68 61 72 20 2a 20 28 2a 76 61 6c 75 65  d char * (*value
288f0 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76  _text)(sqlite3_v
288f1 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  alue*);.  const 
288f2 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74  void * (*value_t
288f3 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 76  ext16)(sqlite3_v
288f4 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  alue*);.  const 
288f5 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74  void * (*value_t
288f6 65 78 74 31 36 62 65 29 28 73 71 6c 69 74 65 33  ext16be)(sqlite3
288f7 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73  _value*);.  cons
288f8 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65  t void * (*value
288f9 5f 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74  _text16le)(sqlit
288fa 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e  e3_value*);.  in
288fb 74 20 20 28 2a 76 61 6c 75 65 5f 74 79 70 65 29  t  (*value_type)
288fc 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
288fd 3b 0a 20 20 63 68 61 72 20 2a 28 2a 76 6d 70 72  ;.  char *(*vmpr
288fe 69 6e 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72  intf)(const char
288ff 2a 2c 76 61 5f 6c 69 73 74 29 3b 0a 20 20 2f 2a  *,va_list);.  /*
28900 20 41 64 64 65 64 20 3f 3f 3f 20 2a 2f 0a 20 20   Added ??? */.  
28901 69 6e 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f 66  int (*overload_f
28902 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33  unction)(sqlite3
28903 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
28904 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41  FuncName, int nA
28905 72 67 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20  rg);.  /* Added 
28906 62 79 20 33 2e 33 2e 31 33 20 2a 2f 0a 20 20 69  by 3.3.13 */.  i
28907 6e 74 20 28 2a 70 72 65 70 61 72 65 5f 76 32 29  nt (*prepare_v2)
28908 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
28909 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  char*,int,sqlite
2890a 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63  3_stmt**,const c
2890b 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  har**);.  int (*
2890c 70 72 65 70 61 72 65 31 36 5f 76 32 29 28 73 71  prepare16_v2)(sq
2890d 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69  lite3*,const voi
2890e 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73  d*,int,sqlite3_s
2890f 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  tmt**,const void
28910 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 63 6c 65  **);.  int (*cle
28911 61 72 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c  ar_bindings)(sql
28912 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 2f  ite3_stmt*);.  /
28913 2a 20 41 64 64 65 64 20 62 79 20 33 2e 34 2e 31  * Added by 3.4.1
28914 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 72 65 61   */.  int (*crea
28915 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 29 28 73 71  te_module_v2)(sq
28916 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
28917 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  r*,const sqlite3
28918 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 2c 76  _module*,void*,v
28919 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28  oid (*xDestroy)(
2891a 76 6f 69 64 20 2a 29 29 3b 0a 20 20 2f 2a 20 41  void *));.  /* A
2891b 64 64 65 64 20 62 79 20 33 2e 35 2e 30 20 2a 2f  dded by 3.5.0 */
2891c 0a 20 20 69 6e 74 20 28 2a 62 69 6e 64 5f 7a 65  .  int (*bind_ze
2891d 72 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f  roblob)(sqlite3_
2891e 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  stmt*,int,int);.
2891f 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 62 79 74    int (*blob_byt
28920 65 73 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  es)(sqlite3_blob
28921 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62  *);.  int (*blob
28922 5f 63 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f  _close)(sqlite3_
28923 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  blob*);.  int (*
28924 62 6c 6f 62 5f 6f 70 65 6e 29 28 73 71 6c 69 74  blob_open)(sqlit
28925 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
28926 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
28927 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f  t char*,sqlite3_
28928 69 6e 74 36 34 2c 69 6e 74 2c 73 71 6c 69 74 65  int64,int,sqlite
28929 33 5f 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69 6e 74  3_blob**);.  int
2892a 20 28 2a 62 6c 6f 62 5f 72 65 61 64 29 28 73 71   (*blob_read)(sq
2892b 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f 69 64  lite3_blob*,void
2892c 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e  *,int,int);.  in
2892d 74 20 28 2a 62 6c 6f 62 5f 77 72 69 74 65 29 28  t (*blob_write)(
2892e 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 63 6f  sqlite3_blob*,co
2892f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e  nst void*,int,in
28930 74 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61  t);.  int (*crea
28931 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 29  te_collation_v2)
28932 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
28933 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c  char*,int,void*,
28934 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
28935 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
28936 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 76 6f  ,const void*),vo
28937 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20  id(*)(void*));. 
28938 20 69 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e 74   int (*file_cont
28939 72 6f 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  rol)(sqlite3*,co
2893a 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f  nst char*,int,vo
2893b 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  id*);.  sqlite3_
2893c 69 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 68  int64 (*memory_h
2893d 69 67 68 77 61 74 65 72 29 28 69 6e 74 29 3b 0a  ighwater)(int);.
2893e 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
2893f 28 2a 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28 76  (*memory_used)(v
28940 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  oid);.  sqlite3_
28941 6d 75 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f 61  mutex *(*mutex_a
28942 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f  lloc)(int);.  vo
28943 69 64 20 28 2a 6d 75 74 65 78 5f 65 6e 74 65 72  id (*mutex_enter
28944 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  )(sqlite3_mutex*
28945 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65  );.  void (*mute
28946 78 5f 66 72 65 65 29 28 73 71 6c 69 74 65 33 5f  x_free)(sqlite3_
28947 6d 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20  mutex*);.  void 
28948 28 2a 6d 75 74 65 78 5f 6c 65 61 76 65 29 28 73  (*mutex_leave)(s
28949 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
2894a 20 20 69 6e 74 20 28 2a 6d 75 74 65 78 5f 74 72    int (*mutex_tr
2894b 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  y)(sqlite3_mutex
2894c 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 6f 70 65 6e  *);.  int (*open
2894d 5f 76 32 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  _v2)(const char*
2894e 2c 73 71 6c 69 74 65 33 2a 2a 2c 69 6e 74 2c 63  ,sqlite3**,int,c
2894f 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69  onst char*);.  i
28950 6e 74 20 28 2a 72 65 6c 65 61 73 65 5f 6d 65 6d  nt (*release_mem
28951 6f 72 79 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69  ory)(int);.  voi
28952 64 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72  d (*result_error
28953 5f 6e 6f 6d 65 6d 29 28 73 71 6c 69 74 65 33 5f  _nomem)(sqlite3_
28954 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69  context*);.  voi
28955 64 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72  d (*result_error
28956 5f 74 6f 6f 62 69 67 29 28 73 71 6c 69 74 65 33  _toobig)(sqlite3
28957 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e  _context*);.  in
28958 74 20 28 2a 73 6c 65 65 70 29 28 69 6e 74 29 3b  t (*sleep)(int);
28959 0a 20 20 76 6f 69 64 20 28 2a 73 6f 66 74 5f 68  .  void (*soft_h
2895a 65 61 70 5f 6c 69 6d 69 74 29 28 69 6e 74 29 3b  eap_limit)(int);
2895b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
2895c 28 2a 76 66 73 5f 66 69 6e 64 29 28 63 6f 6e 73  (*vfs_find)(cons
2895d 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20  t char*);.  int 
2895e 28 2a 76 66 73 5f 72 65 67 69 73 74 65 72 29 28  (*vfs_register)(
2895f 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 69 6e 74  sqlite3_vfs*,int
28960 29 3b 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 75  );.  int (*vfs_u
28961 6e 72 65 67 69 73 74 65 72 29 28 73 71 6c 69 74  nregister)(sqlit
28962 65 33 5f 76 66 73 2a 29 3b 0a 20 20 69 6e 74 20  e3_vfs*);.  int 
28963 28 2a 78 74 68 72 65 61 64 73 61 66 65 29 28 76  (*xthreadsafe)(v
28964 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72  oid);.  void (*r
28965 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 29 28  esult_zeroblob)(
28966 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28967 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a  ,int);.  void (*
28968 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
28969 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
2896a 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  xt*,int);.  int 
2896b 28 2a 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 29 28  (*test_control)(
2896c 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 20 20 76 6f 69  int, ...);.  voi
2896d 64 20 28 2a 72 61 6e 64 6f 6d 6e 65 73 73 29 28  d (*randomness)(
2896e 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71  int,void*);.  sq
2896f 6c 69 74 65 33 20 2a 28 2a 63 6f 6e 74 65 78 74  lite3 *(*context
28970 5f 64 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69  _db_handle)(sqli
28971 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20  te3_context*);. 
28972 20 69 6e 74 20 28 2a 65 78 74 65 6e 64 65 64 5f   int (*extended_
28973 72 65 73 75 6c 74 5f 63 6f 64 65 73 29 28 73 71  result_codes)(sq
28974 6c 69 74 65 33 2a 2c 69 6e 74 29 3b 0a 20 20 69  lite3*,int);.  i
28975 6e 74 20 28 2a 6c 69 6d 69 74 29 28 73 71 6c 69  nt (*limit)(sqli
28976 74 65 33 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20  te3*,int,int);. 
28977 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 28   sqlite3_stmt *(
28978 2a 6e 65 78 74 5f 73 74 6d 74 29 28 73 71 6c 69  *next_stmt)(sqli
28979 74 65 33 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d  te3*,sqlite3_stm
2897a 74 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  t*);.  const cha
2897b 72 20 2a 28 2a 73 71 6c 29 28 73 71 6c 69 74 65  r *(*sql)(sqlite
2897c 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20  3_stmt*);.  int 
2897d 28 2a 73 74 61 74 75 73 29 28 69 6e 74 2c 69 6e  (*status)(int,in
2897e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 29 3b 0a 7d 3b  t*,int*,int);.};
2897f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
28980 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 72 65 64  owing macros red
28981 65 66 69 6e 65 20 74 68 65 20 41 50 49 20 72 6f  efine the API ro
28982 75 74 69 6e 65 73 20 73 6f 20 74 68 61 74 20 74  utines so that t
28983 68 65 79 20 61 72 65 0a 2a 2a 20 72 65 64 69 72  hey are.** redir
28984 65 63 74 65 64 20 74 68 72 6f 75 67 68 74 20 74  ected throught t
28985 68 65 20 67 6c 6f 62 61 6c 20 73 71 6c 69 74 65  he global sqlite
28986 33 5f 61 70 69 20 73 74 72 75 63 74 75 72 65 2e  3_api structure.
28987 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  .**.** This head
28988 65 72 20 66 69 6c 65 20 69 73 20 61 6c 73 6f 20  er file is also 
28989 75 73 65 64 20 62 79 20 74 68 65 20 6c 6f 61 64  used by the load
2898a 65 78 74 2e 63 20 73 6f 75 72 63 65 20 66 69 6c  ext.c source fil
2898b 65 0a 2a 2a 20 28 70 61 72 74 20 6f 66 20 74 68  e.** (part of th
2898c 65 20 6d 61 69 6e 20 53 51 4c 69 74 65 20 6c 69  e main SQLite li
2898d 62 72 61 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65  brary - not an e
2898e 78 74 65 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61  xtension) so tha
2898f 74 0a 2a 2a 20 69 74 20 63 61 6e 20 67 65 74 20  t.** it can get 
28990 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 73 71  access to the sq
28991 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
28992 65 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  es structure.** 
28993 64 65 66 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74  definition.  But
28994 20 74 68 65 20 6d 61 69 6e 20 6c 69 62 72 61 72   the main librar
28995 79 20 64 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20  y does not want 
28996 74 6f 20 72 65 64 65 66 69 6e 65 0a 2a 2a 20 74  to redefine.** t
28997 68 65 20 41 50 49 2e 20 20 53 6f 20 74 68 65 20  he API.  So the 
28998 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63  redefinition mac
28999 72 6f 73 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c  ros are only val
2899a 69 64 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c  id if the.** SQL
2899b 49 54 45 5f 43 4f 52 45 20 6d 61 63 72 6f 73 20  ITE_CORE macros 
2899c 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
2899d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2899e 43 4f 52 45 0a 23 64 65 66 69 6e 65 20 73 71 6c  CORE.#define sql
2899f 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
289a0 6f 6e 74 65 78 74 20 20 20 20 20 20 73 71 6c 69  ontext      sqli
289a1 74 65 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61  te3_api->aggrega
289a2 74 65 5f 63 6f 6e 74 65 78 74 0a 23 69 66 6e 64  te_context.#ifnd
289a3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
289a4 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e  EPRECATED.#defin
289a5 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  e sqlite3_aggreg
289a6 61 74 65 5f 63 6f 75 6e 74 20 20 20 20 20 20 20  ate_count       
289a7 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67   sqlite3_api->ag
289a8 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 0a 23 65  gregate_count.#e
289a9 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndif.#define sql
289aa 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 20  ite3_bind_blob  
289ab 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
289ac 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 62 6c  te3_api->bind_bl
289ad 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ob.#define sqlit
289ae 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 20 20  e3_bind_double  
289af 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
289b0 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 64 6f 75 62  3_api->bind_doub
289b1 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
289b2 65 33 5f 62 69 6e 64 5f 69 6e 74 20 20 20 20 20  e3_bind_int     
289b3 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
289b4 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a  3_api->bind_int.
289b5 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
289b6 62 69 6e 64 5f 69 6e 74 36 34 20 20 20 20 20 20  bind_int64      
289b7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
289b8 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 36 34 0a 23  pi->bind_int64.#
289b9 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
289ba 69 6e 64 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20  ind_null        
289bb 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
289bc 69 2d 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65  i->bind_null.#de
289bd 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
289be 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
289bf 74 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  t   sqlite3_api-
289c0 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
289c1 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  count.#define sq
289c2 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
289c3 65 74 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c  eter_index   sql
289c4 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70  ite3_api->bind_p
289c5 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 0a 23  arameter_index.#
289c6 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
289c7 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
289c8 6d 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  me    sqlite3_ap
289c9 69 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  i->bind_paramete
289ca 72 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73  r_name.#define s
289cb 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
289cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
289cd 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
289ce 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  text.#define sql
289cf 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
289d0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
289d1 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65  te3_api->bind_te
289d2 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  xt16.#define sql
289d3 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 20  ite3_bind_value 
289d4 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
289d5 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 76 61  te3_api->bind_va
289d6 6c 75 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  lue.#define sqli
289d7 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
289d8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
289d9 65 33 5f 61 70 69 2d 3e 62 75 73 79 5f 68 61 6e  e3_api->busy_han
289da 64 6c 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  dler.#define sql
289db 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75  ite3_busy_timeou
289dc 74 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  t           sqli
289dd 74 65 33 5f 61 70 69 2d 3e 62 75 73 79 5f 74 69  te3_api->busy_ti
289de 6d 65 6f 75 74 0a 23 64 65 66 69 6e 65 20 73 71  meout.#define sq
289df 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 20 20 20  lite3_changes   
289e0 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
289e1 69 74 65 33 5f 61 70 69 2d 3e 63 68 61 6e 67 65  ite3_api->change
289e2 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  s.#define sqlite
289e3 33 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20  3_close         
289e4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
289e5 5f 61 70 69 2d 3e 63 6c 6f 73 65 0a 23 64 65 66  _api->close.#def
289e6 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  ine sqlite3_coll
289e7 61 74 69 6f 6e 5f 6e 65 65 64 65 64 20 20 20 20  ation_needed    
289e8 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
289e9 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
289ea 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
289eb 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
289ec 64 31 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f  d16     sqlite3_
289ed 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  api->collation_n
289ee 65 65 64 65 64 31 36 0a 23 64 65 66 69 6e 65 20  eeded16.#define 
289ef 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
289f0 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 73  lob            s
289f1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
289f2 6d 6e 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20  mn_blob.#define 
289f3 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
289f4 79 74 65 73 20 20 20 20 20 20 20 20 20 20 20 73  ytes           s
289f5 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
289f6 6d 6e 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65  mn_bytes.#define
289f7 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
289f8 62 79 74 65 73 31 36 20 20 20 20 20 20 20 20 20  bytes16         
289f9 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
289fa 75 6d 6e 5f 62 79 74 65 73 31 36 0a 23 64 65 66  umn_bytes16.#def
289fb 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
289fc 6d 6e 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20  mn_count        
289fd 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
289fe 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65  column_count.#de
289ff 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28a00 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
28a01 65 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  e   sqlite3_api-
28a02 3e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  >column_database
28a03 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71  _name.#define sq
28a04 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
28a05 61 62 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c  abase_name16 sql
28a06 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
28a07 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36  _database_name16
28a08 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28a09 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
28a0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28a0b 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c  api->column_decl
28a0c 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  type.#define sql
28a0d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
28a0e 74 79 70 65 31 36 20 20 20 20 20 20 73 71 6c 69  type16      sqli
28a0f 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
28a10 64 65 63 6c 74 79 70 65 31 36 0a 23 64 65 66 69  decltype16.#defi
28a11 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28a12 6e 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20  n_double        
28a13 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28a14 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23 64 65  olumn_double.#de
28a15 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28a16 75 6d 6e 5f 69 6e 74 20 20 20 20 20 20 20 20 20  umn_int         
28a17 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a18 3e 63 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66  >column_int.#def
28a19 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28a1a 6d 6e 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20  mn_int64        
28a1b 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28a1c 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23 64 65  column_int64.#de
28a1d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28a1e 75 6d 6e 5f 6e 61 6d 65 20 20 20 20 20 20 20 20  umn_name        
28a1f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a20 3e 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65  >column_name.#de
28a21 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28a22 75 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20  umn_name16      
28a23 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a24 3e 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23  >column_name16.#
28a25 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28a26 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
28a27 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  e     sqlite3_ap
28a28 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  i->column_origin
28a29 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71  _name.#define sq
28a2a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
28a2b 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 73 71 6c  gin_name16   sql
28a2c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
28a2d 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 0a 23  _origin_name16.#
28a2e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28a2f 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
28a30 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28a31 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  i->column_table_
28a32 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
28a33 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
28a34 65 5f 6e 61 6d 65 31 36 20 20 20 20 73 71 6c 69  e_name16    sqli
28a35 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
28a36 74 61 62 6c 65 5f 6e 61 6d 65 31 36 0a 23 64 65  table_name16.#de
28a37 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28a38 75 6d 6e 5f 74 65 78 74 20 20 20 20 20 20 20 20  umn_text        
28a39 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a3a 3e 63 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23 64 65  >column_text.#de
28a3b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28a3c 75 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20  umn_text16      
28a3d 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a3e 3e 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 0a 23  >column_text16.#
28a3f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28a40 6f 6c 75 6d 6e 5f 74 79 70 65 20 20 20 20 20 20  olumn_type      
28a41 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28a42 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23  i->column_type.#
28a43 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28a44 6f 6c 75 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20  olumn_value     
28a45 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28a46 69 2d 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a  i->column_value.
28a47 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a48 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20  commit_hook     
28a49 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28a4a 70 69 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a  pi->commit_hook.
28a4b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a4c 63 6f 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20  complete        
28a4d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28a4e 70 69 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23 64 65  pi->complete.#de
28a4f 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d  fine sqlite3_com
28a50 70 6c 65 74 65 31 36 20 20 20 20 20 20 20 20 20  plete16         
28a51 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28a52 3e 63 6f 6d 70 6c 65 74 65 31 36 0a 23 64 65 66  >complete16.#def
28a53 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ine sqlite3_crea
28a54 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20 20  te_collation    
28a55 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28a56 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
28a57 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28a58 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
28a59 6e 31 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f  n16     sqlite3_
28a5a 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c  api->create_coll
28a5b 61 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20  ation16.#define 
28a5c 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
28a5d 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 20 20 73  unction        s
28a5e 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
28a5f 74 65 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66  te_function.#def
28a60 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ine sqlite3_crea
28a61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20  te_function16   
28a62 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28a63 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
28a64 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
28a65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20  3_create_module 
28a66 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28a67 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64  _api->create_mod
28a68 75 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ule.#define sqli
28a69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
28a6a 65 5f 76 32 20 20 20 20 20 20 20 73 71 6c 69 74  e_v2       sqlit
28a6b 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d  e3_api->create_m
28a6c 6f 64 75 6c 65 5f 76 32 0a 23 64 65 66 69 6e 65  odule_v2.#define
28a6d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f   sqlite3_data_co
28a6e 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  unt             
28a6f 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 61 74  sqlite3_api->dat
28a70 61 5f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20  a_count.#define 
28a71 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c  sqlite3_db_handl
28a72 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  e              s
28a73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 68  qlite3_api->db_h
28a74 61 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  andle.#define sq
28a75 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
28a76 61 62 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  ab           sql
28a77 69 74 65 33 5f 61 70 69 2d 3e 64 65 63 6c 61 72  ite3_api->declar
28a78 65 5f 76 74 61 62 0a 23 64 65 66 69 6e 65 20 73  e_vtab.#define s
28a79 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68  qlite3_enable_sh
28a7a 61 72 65 64 5f 63 61 63 68 65 20 20 20 20 73 71  ared_cache    sq
28a7b 6c 69 74 65 33 5f 61 70 69 2d 3e 65 6e 61 62 6c  lite3_api->enabl
28a7c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 0a 23  e_shared_cache.#
28a7d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65  define sqlite3_e
28a7e 72 72 63 6f 64 65 20 20 20 20 20 20 20 20 20 20  rrcode          
28a7f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28a80 69 2d 3e 65 72 72 63 6f 64 65 0a 23 64 65 66 69  i->errcode.#defi
28a81 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  ne sqlite3_errms
28a82 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
28a83 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65    sqlite3_api->e
28a84 72 72 6d 73 67 0a 23 64 65 66 69 6e 65 20 73 71  rrmsg.#define sq
28a85 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20  lite3_errmsg16  
28a86 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28a87 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67  ite3_api->errmsg
28a88 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
28a89 65 33 5f 65 78 65 63 20 20 20 20 20 20 20 20 20  e3_exec         
28a8a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28a8b 33 5f 61 70 69 2d 3e 65 78 65 63 0a 23 69 66 6e  3_api->exec.#ifn
28a8c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28a8d 44 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69  DEPRECATED.#defi
28a8e 6e 65 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  ne sqlite3_expir
28a8f 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
28a90 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65    sqlite3_api->e
28a91 78 70 69 72 65 64 0a 23 65 6e 64 69 66 0a 23 64  xpired.#endif.#d
28a92 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69  efine sqlite3_fi
28a93 6e 61 6c 69 7a 65 20 20 20 20 20 20 20 20 20 20  nalize          
28a94 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28a95 2d 3e 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66 69  ->finalize.#defi
28a96 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20  ne sqlite3_free 
28a97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28a98 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66    sqlite3_api->f
28a99 72 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ree.#define sqli
28a9a 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20  te3_free_table  
28a9b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28a9c 65 33 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61 62  e3_api->free_tab
28a9d 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
28a9e 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  e3_get_autocommi
28a9f 74 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  t         sqlite
28aa0 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f 63  3_api->get_autoc
28aa1 6f 6d 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71  ommit.#define sq
28aa2 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
28aa3 61 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  a            sql
28aa4 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75  ite3_api->get_au
28aa5 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71  xdata.#define sq
28aa6 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20  lite3_get_table 
28aa7 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28aa8 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74 61  ite3_api->get_ta
28aa9 62 6c 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ble.#ifndef SQLI
28aaa 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
28aab 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ED.#define sqlit
28aac 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65  e3_global_recove
28aad 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  r         sqlite
28aae 33 5f 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f 72 65  3_api->global_re
28aaf 63 6f 76 65 72 0a 23 65 6e 64 69 66 0a 23 64 65  cover.#endif.#de
28ab0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 69 6e 74  fine sqlite3_int
28ab1 65 72 72 75 70 74 20 20 20 20 20 20 20 20 20 20  errupt          
28ab2 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28ab3 3e 69 6e 74 65 72 72 75 70 74 78 0a 23 64 65 66  >interruptx.#def
28ab4 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  ine sqlite3_last
28ab5 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 20 20 20  _insert_rowid   
28ab6 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28ab7 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
28ab8 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  d.#define sqlite
28ab9 33 5f 6c 69 62 76 65 72 73 69 6f 6e 20 20 20 20  3_libversion    
28aba 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28abb 5f 61 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e  _api->libversion
28abc 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28abd 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
28abe 65 72 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  er      sqlite3_
28abf 61 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 5f  api->libversion_
28ac0 6e 75 6d 62 65 72 0a 23 64 65 66 69 6e 65 20 73  number.#define s
28ac1 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 20 20  qlite3_malloc   
28ac2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28ac3 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f  lite3_api->mallo
28ac4 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  c.#define sqlite
28ac5 33 5f 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20  3_mprintf       
28ac6 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28ac7 5f 61 70 69 2d 3e 6d 70 72 69 6e 74 66 0a 23 64  _api->mprintf.#d
28ac8 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70  efine sqlite3_op
28ac9 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  en              
28aca 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28acb 2d 3e 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73  ->open.#define s
28acc 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20  qlite3_open16   
28acd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28ace 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 31  lite3_api->open1
28acf 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
28ad0 33 5f 70 72 65 70 61 72 65 20 20 20 20 20 20 20  3_prepare       
28ad1 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28ad2 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 0a 23 64  _api->prepare.#d
28ad3 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
28ad4 65 70 61 72 65 31 36 20 20 20 20 20 20 20 20 20  epare16         
28ad5 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28ad6 2d 3e 70 72 65 70 61 72 65 31 36 0a 23 64 65 66  ->prepare16.#def
28ad7 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
28ad8 61 72 65 5f 76 32 20 20 20 20 20 20 20 20 20 20  are_v2          
28ad9 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28ada 70 72 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69  prepare_v2.#defi
28adb 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ne sqlite3_prepa
28adc 72 65 31 36 5f 76 32 20 20 20 20 20 20 20 20 20  re16_v2         
28add 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70    sqlite3_api->p
28ade 72 65 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66  repare16_v2.#def
28adf 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66  ine sqlite3_prof
28ae0 69 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ile             
28ae1 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28ae2 70 72 6f 66 69 6c 65 0a 23 64 65 66 69 6e 65 20  profile.#define 
28ae3 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
28ae4 5f 68 61 6e 64 6c 65 72 20 20 20 20 20 20 20 73  _handler       s
28ae5 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 67  qlite3_api->prog
28ae6 72 65 73 73 5f 68 61 6e 64 6c 65 72 0a 23 64 65  ress_handler.#de
28ae7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 61  fine sqlite3_rea
28ae8 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20  lloc            
28ae9 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28aea 3e 72 65 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65  >realloc.#define
28aeb 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 20 20   sqlite3_reset  
28aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28aed 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
28aee 65 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  et.#define sqlit
28aef 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 20  e3_result_blob  
28af0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28af1 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 62 6c  3_api->result_bl
28af2 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ob.#define sqlit
28af3 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
28af4 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28af5 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 64 6f  3_api->result_do
28af6 75 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  uble.#define sql
28af7 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
28af8 72 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  r           sqli
28af9 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
28afa 65 72 72 6f 72 0a 23 64 65 66 69 6e 65 20 73 71  error.#define sq
28afb 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
28afc 6f 72 31 36 20 20 20 20 20 20 20 20 20 73 71 6c  or16         sql
28afd 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
28afe 5f 65 72 72 6f 72 31 36 0a 23 64 65 66 69 6e 65  _error16.#define
28aff 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28b00 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  int             
28b01 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
28b02 75 6c 74 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20  ult_int.#define 
28b03 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
28b04 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20 73  nt64           s
28b05 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
28b06 6c 74 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65  lt_int64.#define
28b07 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28b08 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  null            
28b09 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
28b0a 75 6c 74 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65  ult_null.#define
28b0b 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28b0c 74 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20  text            
28b0d 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
28b0e 75 6c 74 5f 74 65 78 74 0a 23 64 65 66 69 6e 65  ult_text.#define
28b0f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28b10 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20  text16          
28b11 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
28b12 75 6c 74 5f 74 65 78 74 31 36 0a 23 64 65 66 69  ult_text16.#defi
28b13 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
28b14 74 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20  t_text16be      
28b15 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
28b16 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 0a 23  esult_text16be.#
28b17 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
28b18 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20  esult_text16le  
28b19 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28b1a 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36  i->result_text16
28b1b 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
28b1c 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 20  e3_result_value 
28b1d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28b1e 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 76 61  3_api->result_va
28b1f 6c 75 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  lue.#define sqli
28b20 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f  te3_rollback_hoo
28b21 6b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  k          sqlit
28b22 65 33 5f 61 70 69 2d 3e 72 6f 6c 6c 62 61 63 6b  e3_api->rollback
28b23 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71  _hook.#define sq
28b24 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
28b25 69 7a 65 72 20 20 20 20 20 20 20 20 20 73 71 6c  izer         sql
28b26 69 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75  ite3_api->set_au
28b27 74 68 6f 72 69 7a 65 72 0a 23 64 65 66 69 6e 65  thorizer.#define
28b28 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78   sqlite3_set_aux
28b29 64 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20  data            
28b2a 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74  sqlite3_api->set
28b2b 5f 61 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65  _auxdata.#define
28b2c 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
28b2d 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f               
28b2e 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6e 70  sqlite3_api->snp
28b2f 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71  rintf.#define sq
28b30 6c 69 74 65 33 5f 73 74 65 70 20 20 20 20 20 20  lite3_step      
28b31 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28b32 69 74 65 33 5f 61 70 69 2d 3e 73 74 65 70 0a 23  ite3_api->step.#
28b33 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
28b34 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61  able_column_meta
28b35 64 61 74 61 20 20 73 71 6c 69 74 65 33 5f 61 70  data  sqlite3_ap
28b36 69 2d 3e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f  i->table_column_
28b37 6d 65 74 61 64 61 74 61 0a 23 64 65 66 69 6e 65  metadata.#define
28b38 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f   sqlite3_thread_
28b39 63 6c 65 61 6e 75 70 20 20 20 20 20 20 20 20 20  cleanup         
28b3a 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 68 72  sqlite3_api->thr
28b3b 65 61 64 5f 63 6c 65 61 6e 75 70 0a 23 64 65 66  ead_cleanup.#def
28b3c 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  ine sqlite3_tota
28b3d 6c 5f 63 68 61 6e 67 65 73 20 20 20 20 20 20 20  l_changes       
28b3e 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28b3f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 0a 23 64  total_changes.#d
28b40 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72  efine sqlite3_tr
28b41 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ace             
28b42 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28b43 2d 3e 74 72 61 63 65 0a 23 69 66 6e 64 65 66 20  ->trace.#ifndef 
28b44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
28b45 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73  ECATED.#define s
28b46 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f  qlite3_transfer_
28b47 62 69 6e 64 69 6e 67 73 20 20 20 20 20 20 73 71  bindings      sq
28b48 6c 69 74 65 33 5f 61 70 69 2d 3e 74 72 61 6e 73  lite3_api->trans
28b49 66 65 72 5f 62 69 6e 64 69 6e 67 73 0a 23 65 6e  fer_bindings.#en
28b4a 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dif.#define sqli
28b4b 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 20  te3_update_hook 
28b4c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28b4d 65 33 5f 61 70 69 2d 3e 75 70 64 61 74 65 5f 68  e3_api->update_h
28b4e 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ook.#define sqli
28b4f 74 65 33 5f 75 73 65 72 5f 64 61 74 61 20 20 20  te3_user_data   
28b50 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28b51 65 33 5f 61 70 69 2d 3e 75 73 65 72 5f 64 61 74  e3_api->user_dat
28b52 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  a.#define sqlite
28b53 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 20 20 20  3_value_blob    
28b54 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28b55 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62  _api->value_blob
28b56 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28b57 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 20 20 20  _value_bytes    
28b58 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28b59 61 70 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73  api->value_bytes
28b5a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28b5b 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 20 20  _value_bytes16  
28b5c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28b5d 61 70 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73  api->value_bytes
28b5e 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
28b5f 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
28b60 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28b61 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 64 6f 75  3_api->value_dou
28b62 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
28b63 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 20 20  te3_value_int   
28b64 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28b65 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e  e3_api->value_in
28b66 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
28b67 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 20 20 20  3_value_int64   
28b68 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28b69 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 36  _api->value_int6
28b6a 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  4.#define sqlite
28b6b 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
28b6c 74 79 70 65 20 20 20 20 20 73 71 6c 69 74 65 33  type     sqlite3
28b6d 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65  _api->value_nume
28b6e 72 69 63 5f 74 79 70 65 0a 23 64 65 66 69 6e 65  ric_type.#define
28b6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
28b70 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ext             
28b71 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
28b72 75 65 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20  ue_text.#define 
28b73 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
28b74 78 74 31 36 20 20 20 20 20 20 20 20 20 20 20 73  xt16           s
28b75 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
28b76 65 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65  e_text16.#define
28b77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
28b78 65 78 74 31 36 62 65 20 20 20 20 20 20 20 20 20  ext16be         
28b79 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
28b7a 75 65 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66  ue_text16be.#def
28b7b 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ine sqlite3_valu
28b7c 65 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20  e_text16le      
28b7d 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28b7e 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 0a 23  value_text16le.#
28b7f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
28b80 61 6c 75 65 5f 74 79 70 65 20 20 20 20 20 20 20  alue_type       
28b81 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28b82 69 2d 3e 76 61 6c 75 65 5f 74 79 70 65 0a 23 64  i->value_type.#d
28b83 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 6d  efine sqlite3_vm
28b84 70 72 69 6e 74 66 20 20 20 20 20 20 20 20 20 20  printf          
28b85 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28b86 2d 3e 76 6d 70 72 69 6e 74 66 0a 23 64 65 66 69  ->vmprintf.#defi
28b87 6e 65 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  ne sqlite3_overl
28b88 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20  oad_function    
28b89 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f    sqlite3_api->o
28b8a 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
28b8b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28b8c 5f 70 72 65 70 61 72 65 5f 76 32 20 20 20 20 20  _prepare_v2     
28b8d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28b8e 61 70 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a  api->prepare_v2.
28b8f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28b90 70 72 65 70 61 72 65 31 36 5f 76 32 20 20 20 20  prepare16_v2    
28b91 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28b92 70 69 2d 3e 70 72 65 70 61 72 65 31 36 5f 76 32  pi->prepare16_v2
28b93 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28b94 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 20  _clear_bindings 
28b95 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28b96 61 70 69 2d 3e 63 6c 65 61 72 5f 62 69 6e 64 69  api->clear_bindi
28b97 6e 67 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ngs.#define sqli
28b98 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  te3_bind_zeroblo
28b99 62 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  b          sqlit
28b9a 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 7a 65 72  e3_api->bind_zer
28b9b 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71  oblob.#define sq
28b9c 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73  lite3_blob_bytes
28b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28b9e 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62  ite3_api->blob_b
28b9f 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c  ytes.#define sql
28ba0 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20  ite3_blob_close 
28ba1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28ba2 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c  te3_api->blob_cl
28ba3 6f 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ose.#define sqli
28ba4 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20  te3_blob_open   
28ba5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28ba6 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65  e3_api->blob_ope
28ba7 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  n.#define sqlite
28ba8 33 5f 62 6c 6f 62 5f 72 65 61 64 20 20 20 20 20  3_blob_read     
28ba9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28baa 5f 61 70 69 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a  _api->blob_read.
28bab 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28bac 62 6c 6f 62 5f 77 72 69 74 65 20 20 20 20 20 20  blob_write      
28bad 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28bae 70 69 2d 3e 62 6c 6f 62 5f 77 72 69 74 65 0a 23  pi->blob_write.#
28baf 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28bb0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
28bb1 76 32 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  v2    sqlite3_ap
28bb2 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  i->create_collat
28bb3 69 6f 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  ion_v2.#define s
28bb4 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
28bb5 72 6f 6c 20 20 20 20 20 20 20 20 20 20 20 73 71  rol           sq
28bb6 6c 69 74 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f  lite3_api->file_
28bb7 63 6f 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20  control.#define 
28bb8 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68  sqlite3_memory_h
28bb9 69 67 68 77 61 74 65 72 20 20 20 20 20 20 20 73  ighwater       s
28bba 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f  qlite3_api->memo
28bbb 72 79 5f 68 69 67 68 77 61 74 65 72 0a 23 64 65  ry_highwater.#de
28bbc 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d  fine sqlite3_mem
28bbd 6f 72 79 5f 75 73 65 64 20 20 20 20 20 20 20 20  ory_used        
28bbe 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28bbf 3e 6d 65 6d 6f 72 79 5f 75 73 65 64 0a 23 64 65  >memory_used.#de
28bc0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
28bc1 65 78 5f 61 6c 6c 6f 63 20 20 20 20 20 20 20 20  ex_alloc        
28bc2 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28bc3 3e 6d 75 74 65 78 5f 61 6c 6c 6f 63 0a 23 64 65  >mutex_alloc.#de
28bc4 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
28bc5 65 78 5f 65 6e 74 65 72 20 20 20 20 20 20 20 20  ex_enter        
28bc6 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28bc7 3e 6d 75 74 65 78 5f 65 6e 74 65 72 0a 23 64 65  >mutex_enter.#de
28bc8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
28bc9 65 78 5f 66 72 65 65 20 20 20 20 20 20 20 20 20  ex_free         
28bca 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28bcb 3e 6d 75 74 65 78 5f 66 72 65 65 0a 23 64 65 66  >mutex_free.#def
28bcc 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
28bcd 78 5f 6c 65 61 76 65 20 20 20 20 20 20 20 20 20  x_leave         
28bce 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28bcf 6d 75 74 65 78 5f 6c 65 61 76 65 0a 23 64 65 66  mutex_leave.#def
28bd0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
28bd1 78 5f 74 72 79 20 20 20 20 20 20 20 20 20 20 20  x_try           
28bd2 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28bd3 6d 75 74 65 78 5f 74 72 79 0a 23 64 65 66 69 6e  mutex_try.#defin
28bd4 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  e sqlite3_open_v
28bd5 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
28bd6 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70   sqlite3_api->op
28bd7 65 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71  en_v2.#define sq
28bd8 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
28bd9 6d 6f 72 79 20 20 20 20 20 20 20 20 20 73 71 6c  mory         sql
28bda 69 74 65 33 5f 61 70 69 2d 3e 72 65 6c 65 61 73  ite3_api->releas
28bdb 65 5f 6d 65 6d 6f 72 79 0a 23 64 65 66 69 6e 65  e_memory.#define
28bdc 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28bdd 65 72 72 6f 72 5f 6e 6f 6d 65 6d 20 20 20 20 20  error_nomem     
28bde 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
28bdf 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a  ult_error_nomem.
28be0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28be1 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
28be2 62 69 67 20 20 20 20 73 71 6c 69 74 65 33 5f 61  big    sqlite3_a
28be3 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72  pi->result_error
28be4 5f 74 6f 6f 62 69 67 0a 23 64 65 66 69 6e 65 20  _toobig.#define 
28be5 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 20 20 20  sqlite3_sleep   
28be6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28be7 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6c 65 65  qlite3_api->slee
28be8 70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  p.#define sqlite
28be9 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
28bea 74 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  t        sqlite3
28beb 5f 61 70 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f  _api->soft_heap_
28bec 6c 69 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71  limit.#define sq
28bed 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 20 20  lite3_vfs_find  
28bee 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28bef 69 74 65 33 5f 61 70 69 2d 3e 76 66 73 5f 66 69  ite3_api->vfs_fi
28bf0 6e 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nd.#define sqlit
28bf1 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 20  e3_vfs_register 
28bf2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28bf3 33 5f 61 70 69 2d 3e 76 66 73 5f 72 65 67 69 73  3_api->vfs_regis
28bf4 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ter.#define sqli
28bf5 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74  te3_vfs_unregist
28bf6 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  er         sqlit
28bf7 65 33 5f 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65  e3_api->vfs_unre
28bf8 67 69 73 74 65 72 0a 23 64 65 66 69 6e 65 20 73  gister.#define s
28bf9 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66  qlite3_threadsaf
28bfa 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  e             sq
28bfb 6c 69 74 65 33 5f 61 70 69 2d 3e 78 74 68 72 65  lite3_api->xthre
28bfc 61 64 73 61 66 65 0a 23 64 65 66 69 6e 65 20 73  adsafe.#define s
28bfd 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65  qlite3_result_ze
28bfe 72 6f 62 6c 6f 62 20 20 20 20 20 20 20 20 73 71  roblob        sq
28bff 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
28c00 74 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69  t_zeroblob.#defi
28c01 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
28c02 74 5f 65 72 72 6f 72 5f 63 6f 64 65 20 20 20 20  t_error_code    
28c03 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
28c04 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
28c05 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c06 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 20 20 20  _test_control   
28c07 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c08 61 70 69 2d 3e 74 65 73 74 5f 63 6f 6e 74 72 6f  api->test_contro
28c09 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  l.#define sqlite
28c0a 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 20 20 20 20  3_randomness    
28c0b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28c0c 5f 61 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73  _api->randomness
28c0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c0e 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
28c0f 6c 65 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  le      sqlite3_
28c10 61 70 69 2d 3e 63 6f 6e 74 65 78 74 5f 64 62 5f  api->context_db_
28c11 68 61 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73  handle.#define s
28c12 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
28c13 72 65 73 75 6c 74 5f 63 6f 64 65 73 20 20 73 71  result_codes  sq
28c14 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 74 65 6e  lite3_api->exten
28c15 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
28c16 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c17 5f 6c 69 6d 69 74 20 20 20 20 20 20 20 20 20 20  _limit          
28c18 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c19 61 70 69 2d 3e 6c 69 6d 69 74 0a 23 64 65 66 69  api->limit.#defi
28c1a 6e 65 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f  ne sqlite3_next_
28c1b 73 74 6d 74 20 20 20 20 20 20 20 20 20 20 20 20  stmt            
28c1c 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6e    sqlite3_api->n
28c1d 65 78 74 5f 73 74 6d 74 0a 23 64 65 66 69 6e 65  ext_stmt.#define
28c1e 20 73 71 6c 69 74 65 33 5f 73 71 6c 20 20 20 20   sqlite3_sql    
28c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28c20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 71 6c  sqlite3_api->sql
28c21 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c22 5f 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20  _status         
28c23 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c24 61 70 69 2d 3e 73 74 61 74 75 73 0a 23 65 6e 64  api->status.#end
28c25 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52  if /* SQLITE_COR
28c26 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51  E */..#define SQ
28c27 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49  LITE_EXTENSION_I
28c28 4e 49 54 31 20 20 20 20 20 63 6f 6e 73 74 20 73  NIT1     const s
28c29 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
28c2a 6e 65 73 20 2a 73 71 6c 69 74 65 33 5f 61 70 69  nes *sqlite3_api
28c2b 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 51   = 0;.#define SQ
28c2c 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49  LITE_EXTENSION_I
28c2d 4e 49 54 32 28 76 29 20 20 73 71 6c 69 74 65 33  NIT2(v)  sqlite3
28c2e 5f 61 70 69 20 3d 20 76 3b 0a 0a 23 65 6e 64 69  _api = v;..#endi
28c2f 66 20 2f 2a 20 5f 53 51 4c 49 54 45 33 45 58 54  f /* _SQLITE3EXT
28c30 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
28c31 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73  ******* End of s
28c32 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a  qlite3ext.h ****
28c33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c35 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
28c36 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
28c37 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
28c38 20 6f 66 66 20 69 6e 20 6c 6f 61 64 65 78 74 2e   off in loadext.
28c39 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
28c3a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66  ******/..#ifndef
28c3b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
28c3c 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a  D_EXTENSION../*.
28c3d 2a 2a 20 53 6f 6d 65 20 41 50 49 20 72 6f 75 74  ** Some API rout
28c3e 69 6e 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64  ines are omitted
28c3f 20 77 68 65 6e 20 76 61 72 69 6f 75 73 20 66 65   when various fe
28c40 61 74 75 72 65 73 20 61 72 65 0a 2a 2a 20 65 78  atures are.** ex
28c41 63 6c 75 64 65 64 20 66 72 6f 6d 20 61 20 62 75  cluded from a bu
28c42 69 6c 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ild of SQLite.  
28c43 53 75 62 73 74 69 74 75 74 65 20 61 20 4e 55 4c  Substitute a NUL
28c44 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 66 6f 72  L pointer.** for
28c45 20 61 6e 79 20 6d 69 73 73 69 6e 67 20 41 50 49   any missing API
28c46 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  s..*/.#ifndef SQ
28c47 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55  LITE_ENABLE_COLU
28c48 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65  MN_METADATA.# de
28c49 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28c4a 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
28c4b 65 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73  e   0.# define s
28c4c 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61  qlite3_column_da
28c4d 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a  tabase_name16 0.
28c4e 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28c4f 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
28c50 6d 65 20 20 20 20 20 20 30 0a 23 20 64 65 66 69  me      0.# defi
28c51 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28c52 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20  n_table_name16  
28c53 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
28c54 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
28c55 69 6e 5f 6e 61 6d 65 20 20 20 20 20 30 0a 23 20  in_name     0.# 
28c56 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28c57 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
28c58 65 31 36 20 20 20 30 0a 23 20 64 65 66 69 6e 65  e16   0.# define
28c59 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63   sqlite3_table_c
28c5a 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20  olumn_metadata  
28c5b 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  0.#endif..#ifdef
28c5c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
28c5d 48 4f 52 49 5a 41 54 49 4f 4e 0a 23 20 64 65 66  HORIZATION.# def
28c5e 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  ine sqlite3_set_
28c5f 61 75 74 68 6f 72 69 7a 65 72 20 20 20 20 20 20  authorizer      
28c60 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66     0.#endif..#if
28c61 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28c62 55 54 46 31 36 0a 23 20 64 65 66 69 6e 65 20 73  UTF16.# define s
28c63 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
28c64 31 36 20 20 20 20 20 20 20 20 20 20 20 20 30 0a  16            0.
28c65 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28c66 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
28c67 64 31 36 20 20 20 20 20 30 0a 23 20 64 65 66 69  d16     0.# defi
28c68 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28c69 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20 20  n_decltype16    
28c6a 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
28c6b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
28c6c 31 36 20 20 20 20 20 20 20 20 20 20 30 0a 23 20  16          0.# 
28c6d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28c6e 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 20 20 20 20  olumn_text16    
28c6f 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
28c70 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
28c71 65 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  e16             
28c72 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
28c73 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
28c74 69 6f 6e 31 36 20 20 20 20 20 30 0a 23 20 64 65  ion16     0.# de
28c75 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
28c76 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20  ate_function16  
28c77 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
28c78 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20  qlite3_errmsg16 
28c79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a                0.
28c7a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28c7b 5f 6f 70 65 6e 31 36 20 20 20 20 20 20 20 20 20  _open16         
28c7c 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
28c7d 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ne sqlite3_prepa
28c7e 72 65 31 36 20 20 20 20 20 20 20 20 20 20 20 20  re16            
28c7f 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
28c80 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
28c81 32 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20  2           0.# 
28c82 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
28c83 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20  esult_error16   
28c84 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
28c85 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28c86 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20  text16          
28c87 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
28c88 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
28c89 62 65 20 20 20 20 20 20 20 20 30 0a 23 20 64 65  be        0.# de
28c8a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
28c8b 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20  ult_text16le    
28c8c 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
28c8d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
28c8e 74 31 36 20 20 20 20 20 20 20 20 20 20 20 30 0a  t16           0.
28c8f 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28c90 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20  _value_text16be 
28c91 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
28c92 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
28c93 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20  _text16le       
28c94 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
28c95 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
28c96 62 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20  base_name16 0.# 
28c97 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28c98 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
28c99 31 36 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  16    0.# define
28c9a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28c9b 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20  origin_name16   
28c9c 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  0.#endif..#ifdef
28c9d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
28c9e 50 4c 45 54 45 0a 23 20 64 65 66 69 6e 65 20 73  PLETE.# define s
28c9f 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20  qlite3_complete 
28ca0 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
28ca1 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 30 0a  e3_complete16 0.
28ca2 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
28ca3 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
28ca4 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 23 20 64  ESS_CALLBACK.# d
28ca5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
28ca6 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 20 30  ogress_handler 0
28ca7 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
28ca8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
28ca9 55 41 4c 54 41 42 4c 45 0a 23 20 64 65 66 69 6e  UALTABLE.# defin
28caa 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
28cab 5f 6d 6f 64 75 6c 65 20 30 0a 23 20 64 65 66 69  _module 0.# defi
28cac 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
28cad 65 5f 6d 6f 64 75 6c 65 5f 76 32 20 30 0a 23 20  e_module_v2 0.# 
28cae 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
28caf 65 63 6c 61 72 65 5f 76 74 61 62 20 30 0a 23 65  eclare_vtab 0.#e
28cb0 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
28cb1 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
28cb2 43 41 43 48 45 0a 23 20 64 65 66 69 6e 65 20 73  CACHE.# define s
28cb3 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68  qlite3_enable_sh
28cb4 61 72 65 64 5f 63 61 63 68 65 20 30 0a 23 65 6e  ared_cache 0.#en
28cb5 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
28cb6 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 23 20  TE_OMIT_TRACE.# 
28cb7 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
28cb8 72 6f 66 69 6c 65 20 20 20 20 20 20 20 30 0a 23  rofile       0.#
28cb9 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28cba 74 72 61 63 65 20 20 20 20 20 20 20 20 20 30 0a  trace         0.
28cbb 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
28cbc 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54  QLITE_OMIT_GET_T
28cbd 41 42 4c 45 0a 23 20 64 65 66 69 6e 65 20 73 71  ABLE.# define sq
28cbe 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
28cbf 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
28cc0 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
28cc1 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23       0.#endif..#
28cc2 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
28cc3 54 5f 49 4e 43 52 42 4c 4f 42 0a 23 64 65 66 69  T_INCRBLOB.#defi
28cc4 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
28cc5 7a 65 72 6f 62 6c 6f 62 20 20 30 0a 23 64 65 66  zeroblob  0.#def
28cc6 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
28cc7 5f 62 79 74 65 73 20 20 20 20 20 30 0a 23 64 65  _bytes     0.#de
28cc8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
28cc9 62 5f 63 6c 6f 73 65 20 20 20 20 20 30 0a 23 64  b_close     0.#d
28cca 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
28ccb 6f 62 5f 6f 70 65 6e 20 20 20 20 20 20 30 0a 23  ob_open      0.#
28ccc 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
28ccd 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20 30 0a  lob_read      0.
28cce 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28ccf 62 6c 6f 62 5f 77 72 69 74 65 20 20 20 20 20 30  blob_write     0
28cd0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
28cd1 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
28cd2 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
28cd3 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20  pointers to all 
28cd4 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69  SQLite API routi
28cd5 6e 65 73 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  nes..** A pointe
28cd6 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  r to this struct
28cd7 75 72 65 20 69 73 20 70 61 73 73 65 64 20 69 6e  ure is passed in
28cd8 74 6f 20 65 78 74 65 6e 73 69 6f 6e 73 20 77 68  to extensions wh
28cd9 65 6e 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 6c  en they are.** l
28cda 6f 61 64 65 64 20 73 6f 20 74 68 61 74 20 74 68  oaded so that th
28cdb 65 20 65 78 74 65 6e 73 69 6f 6e 20 63 61 6e 20  e extension can 
28cdc 6d 61 6b 65 20 63 61 6c 6c 73 20 62 61 63 6b 20  make calls back 
28cdd 69 6e 74 6f 20 74 68 65 20 53 51 4c 69 74 65 0a  into the SQLite.
28cde 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a  ** library..**.*
28cdf 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20 6e 65  * When adding ne
28ce0 77 20 41 50 49 73 2c 20 61 64 64 20 74 68 65 6d  w APIs, add them
28ce1 20 74 6f 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f   to the bottom o
28ce2 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
28ce3 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  .** in order to 
28ce4 70 72 65 73 65 72 76 65 20 62 61 63 6b 77 61 72  preserve backwar
28ce5 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ds compatibility
28ce6 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f  ..**.** Extensio
28ce7 6e 73 20 74 68 61 74 20 75 73 65 20 6e 65 77 65  ns that use newe
28ce8 72 20 41 50 49 73 20 73 68 6f 75 6c 64 20 66 69  r APIs should fi
28ce9 72 73 74 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20  rst call the.** 
28cea 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
28ceb 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d  on_number() to m
28cec 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
28ced 65 20 41 50 49 20 74 68 65 79 0a 2a 2a 20 69 6e  e API they.** in
28cee 74 65 6e 64 20 74 6f 20 75 73 65 20 69 73 20 73  tend to use is s
28cef 75 70 70 6f 72 74 65 64 20 62 79 20 74 68 65 20  upported by the 
28cf0 6c 69 62 72 61 72 79 2e 20 20 45 78 74 65 6e 73  library.  Extens
28cf1 69 6f 6e 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 61  ions should.** a
28cf2 6c 73 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b  lso check to mak
28cf3 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
28cf4 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66  pointer to the f
28cf5 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f  unction is.** no
28cf6 74 20 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61  t NULL before ca
28cf7 6c 6c 69 6e 67 20 69 74 2e 0a 2a 2f 0a 73 74 61  lling it..*/.sta
28cf8 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
28cf9 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73  3_api_routines s
28cfa 71 6c 69 74 65 33 41 70 69 73 20 3d 20 7b 0a 20  qlite3Apis = {. 
28cfb 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
28cfc 74 65 5f 63 6f 6e 74 65 78 74 2c 0a 23 69 66 6e  te_context,.#ifn
28cfd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28cfe 44 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c  DEPRECATED.  sql
28cff 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
28d00 6f 75 6e 74 2c 0a 23 65 6c 73 65 0a 20 20 30 2c  ount,.#else.  0,
28d01 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
28d02 33 5f 62 69 6e 64 5f 62 6c 6f 62 2c 0a 20 20 73  3_bind_blob,.  s
28d03 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62  qlite3_bind_doub
28d04 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  le,.  sqlite3_bi
28d05 6e 64 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65  nd_int,.  sqlite
28d06 33 5f 62 69 6e 64 5f 69 6e 74 36 34 2c 0a 20 20  3_bind_int64,.  
28d07 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
28d08 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  l,.  sqlite3_bin
28d09 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
28d0a 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  t,.  sqlite3_bin
28d0b 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
28d0c 78 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  x,.  sqlite3_bin
28d0d 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
28d0e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
28d0f 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33  _text,.  sqlite3
28d10 5f 62 69 6e 64 5f 74 65 78 74 31 36 2c 0a 20 20  _bind_text16,.  
28d11 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c  sqlite3_bind_val
28d12 75 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75  ue,.  sqlite3_bu
28d13 73 79 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71  sy_handler,.  sq
28d14 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f  lite3_busy_timeo
28d15 75 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 68  ut,.  sqlite3_ch
28d16 61 6e 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33  anges,.  sqlite3
28d17 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69 74 65  _close,.  sqlite
28d18 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
28d19 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ed,.  sqlite3_co
28d1a 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
28d1b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28d1c 6d 6e 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74  mn_blob,.  sqlit
28d1d 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 2c  e3_column_bytes,
28d1e 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28d1f 6e 5f 62 79 74 65 73 31 36 2c 0a 20 20 73 71 6c  n_bytes16,.  sql
28d20 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
28d21 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t,.  sqlite3_col
28d22 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
28d23 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  e,.  sqlite3_col
28d24 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
28d25 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
28d26 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 2c 0a  olumn_decltype,.
28d27 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28d28 5f 64 65 63 6c 74 79 70 65 31 36 2c 0a 20 20 73  _decltype16,.  s
28d29 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f  qlite3_column_do
28d2a 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  uble,.  sqlite3_
28d2b 63 6f 6c 75 6d 6e 5f 69 6e 74 2c 0a 20 20 73 71  column_int,.  sq
28d2c 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
28d2d 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  64,.  sqlite3_co
28d2e 6c 75 6d 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c  lumn_name,.  sql
28d2f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
28d30 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
28d31 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
28d32 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28d33 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36  mn_origin_name16
28d34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28d35 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 0a 20  mn_table_name,. 
28d36 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28d37 74 61 62 6c 65 5f 6e 61 6d 65 31 36 2c 0a 20 20  table_name16,.  
28d38 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
28d39 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ext,.  sqlite3_c
28d3a 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 2c 0a 20 20  olumn_text16,.  
28d3b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
28d3c 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ype,.  sqlite3_c
28d3d 6f 6c 75 6d 6e 5f 76 61 6c 75 65 2c 0a 20 20 73  olumn_value,.  s
28d3e 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
28d3f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ok,.  sqlite3_co
28d40 6d 70 6c 65 74 65 2c 0a 20 20 73 71 6c 69 74 65  mplete,.  sqlite
28d41 33 5f 63 6f 6d 70 6c 65 74 65 31 36 2c 0a 20 20  3_complete16,.  
28d42 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
28d43 6f 6c 6c 61 74 69 6f 6e 2c 0a 20 20 73 71 6c 69  ollation,.  sqli
28d44 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
28d45 74 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65  tion16,.  sqlite
28d46 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
28d47 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  n,.  sqlite3_cre
28d48 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 2c 0a  ate_function16,.
28d49 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
28d4a 5f 6d 6f 64 75 6c 65 2c 0a 20 20 73 71 6c 69 74  _module,.  sqlit
28d4b 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 2c 0a 20  e3_data_count,. 
28d4c 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64   sqlite3_db_hand
28d4d 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 65  le,.  sqlite3_de
28d4e 63 6c 61 72 65 5f 76 74 61 62 2c 0a 20 20 73 71  clare_vtab,.  sq
28d4f 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
28d50 72 65 64 5f 63 61 63 68 65 2c 0a 20 20 73 71 6c  red_cache,.  sql
28d51 69 74 65 33 5f 65 72 72 63 6f 64 65 2c 0a 20 20  ite3_errcode,.  
28d52 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 2c 0a  sqlite3_errmsg,.
28d53 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67    sqlite3_errmsg
28d54 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 78  16,.  sqlite3_ex
28d55 65 63 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ec,.#ifndef SQLI
28d56 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
28d57 45 44 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 70  ED.  sqlite3_exp
28d58 69 72 65 64 2c 0a 23 65 6c 73 65 0a 20 20 30 2c  ired,.#else.  0,
28d59 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
28d5a 33 5f 66 69 6e 61 6c 69 7a 65 2c 0a 20 20 73 71  3_finalize,.  sq
28d5b 6c 69 74 65 33 5f 66 72 65 65 2c 0a 20 20 73 71  lite3_free,.  sq
28d5c 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
28d5d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ,.  sqlite3_get_
28d5e 61 75 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20 73 71  autocommit,.  sq
28d5f 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
28d60 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74  a,.  sqlite3_get
28d61 5f 74 61 62 6c 65 2c 0a 20 20 30 2c 20 20 20 20  _table,.  0,    
28d62 20 2f 2a 20 57 61 73 20 73 71 6c 69 74 65 33 5f   /* Was sqlite3_
28d63 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 29  global_recover()
28d64 2c 20 62 75 74 20 74 68 61 74 20 66 75 6e 63 74  , but that funct
28d65 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65  ion is deprecate
28d66 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  d */.  sqlite3_i
28d67 6e 74 65 72 72 75 70 74 2c 0a 20 20 73 71 6c 69  nterrupt,.  sqli
28d68 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f  te3_last_insert_
28d69 72 6f 77 69 64 2c 0a 20 20 73 71 6c 69 74 65 33  rowid,.  sqlite3
28d6a 5f 6c 69 62 76 65 72 73 69 6f 6e 2c 0a 20 20 73  _libversion,.  s
28d6b 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
28d6c 6e 5f 6e 75 6d 62 65 72 2c 0a 20 20 73 71 6c 69  n_number,.  sqli
28d6d 74 65 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20 73 71  te3_malloc,.  sq
28d6e 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 2c 0a 20  lite3_mprintf,. 
28d6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 2c 0a 20   sqlite3_open,. 
28d70 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 2c   sqlite3_open16,
28d71 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  .  sqlite3_prepa
28d72 72 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72  re,.  sqlite3_pr
28d73 65 70 61 72 65 31 36 2c 0a 20 20 73 71 6c 69 74  epare16,.  sqlit
28d74 65 33 5f 70 72 6f 66 69 6c 65 2c 0a 20 20 73 71  e3_profile,.  sq
28d75 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
28d76 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65  andler,.  sqlite
28d77 33 5f 72 65 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c  3_realloc,.  sql
28d78 69 74 65 33 5f 72 65 73 65 74 2c 0a 20 20 73 71  ite3_reset,.  sq
28d79 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
28d7a 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  b,.  sqlite3_res
28d7b 75 6c 74 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71  ult_double,.  sq
28d7c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
28d7d 6f 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  or,.  sqlite3_re
28d7e 73 75 6c 74 5f 65 72 72 6f 72 31 36 2c 0a 20 20  sult_error16,.  
28d7f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
28d80 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  nt,.  sqlite3_re
28d81 73 75 6c 74 5f 69 6e 74 36 34 2c 0a 20 20 73 71  sult_int64,.  sq
28d82 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
28d83 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  l,.  sqlite3_res
28d84 75 6c 74 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69  ult_text,.  sqli
28d85 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
28d86 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  6,.  sqlite3_res
28d87 75 6c 74 5f 74 65 78 74 31 36 62 65 2c 0a 20 20  ult_text16be,.  
28d88 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
28d89 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74  ext16le,.  sqlit
28d8a 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 2c  e3_result_value,
28d8b 0a 20 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  .  sqlite3_rollb
28d8c 61 63 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69  ack_hook,.  sqli
28d8d 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
28d8e 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 65  er,.  sqlite3_se
28d8f 74 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c  t_auxdata,.  sql
28d90 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 2c 0a 20  ite3_snprintf,. 
28d91 20 73 71 6c 69 74 65 33 5f 73 74 65 70 2c 0a 20   sqlite3_step,. 
28d92 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63   sqlite3_table_c
28d93 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 2c 0a  olumn_metadata,.
28d94 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28d95 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20  MIT_DEPRECATED. 
28d96 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f   sqlite3_thread_
28d97 63 6c 65 61 6e 75 70 2c 0a 23 65 6c 73 65 0a 20  cleanup,.#else. 
28d98 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c   0,.#endif.  sql
28d99 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
28d9a 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 72  es,.  sqlite3_tr
28d9b 61 63 65 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c  ace,.#ifndef SQL
28d9c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41  ITE_OMIT_DEPRECA
28d9d 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f 74 72  TED.  sqlite3_tr
28d9e 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 2c  ansfer_bindings,
28d9f 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64  .#else.  0,.#end
28da0 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64  if.  sqlite3_upd
28da1 61 74 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69  ate_hook,.  sqli
28da2 74 65 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20  te3_user_data,. 
28da3 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
28da4 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  lob,.  sqlite3_v
28da5 61 6c 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71  alue_bytes,.  sq
28da6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
28da7 73 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  s16,.  sqlite3_v
28da8 61 6c 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73  alue_double,.  s
28da9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
28daa 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
28dab 65 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74  e_int64,.  sqlit
28dac 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63  e3_value_numeric
28dad 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  _type,.  sqlite3
28dae 5f 76 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73  _value_text,.  s
28daf 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
28db0 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  t16,.  sqlite3_v
28db1 61 6c 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20  alue_text16be,. 
28db2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
28db3 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74  ext16le,.  sqlit
28db4 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20  e3_value_type,. 
28db5 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74   sqlite3_vmprint
28db6 66 2c 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65  f,.  /*.  ** The
28db7 20 6f 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65   original API se
28db8 74 20 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c  t ends here.  Al
28db9 6c 20 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e  l extensions can
28dba 20 63 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f   call any.  ** o
28dbb 66 20 74 68 65 20 41 50 49 73 20 61 62 6f 76 65  f the APIs above
28dbc 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74   provided that t
28dbd 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f  he pointer is no
28dbe 74 20 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a  t NULL.  But.  *
28dbf 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  * before calling
28dc0 20 41 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f   APIs that follo
28dc1 77 2c 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f  w, extension sho
28dc2 75 6c 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20  uld check the.  
28dc3 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  ** sqlite3_libve
28dc4 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74  rsion_number() t
28dc5 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79  o make sure they
28dc6 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
28dc7 68 0a 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79  h.  ** a library
28dc8 20 74 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f   that is new eno
28dc9 75 67 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ugh to support t
28dca 68 61 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a  hat API..  *****
28dcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28dcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28dcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28dce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28dcf 2a 2a 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ****.  */.  sqli
28dd0 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
28dd1 63 74 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a  ction,..  /*.  *
28dd2 2a 20 41 64 64 65 64 20 61 66 74 65 72 20 33 2e  * Added after 3.
28dd3 33 2e 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  3.13.  */.  sqli
28dd4 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a  te3_prepare_v2,.
28dd5 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72    sqlite3_prepar
28dd6 65 31 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65  e16_v2,.  sqlite
28dd7 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73  3_clear_bindings
28dd8 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64  ,..  /*.  ** Add
28dd9 65 64 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a  ed for 3.4.1.  *
28dda 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  /.  sqlite3_crea
28ddb 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20  te_module_v2,.. 
28ddc 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66   /*.  ** Added f
28ddd 6f 72 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20  or 3.5.0.  */.  
28dde 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72  sqlite3_bind_zer
28ddf 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33  oblob,.  sqlite3
28de0 5f 62 6c 6f 62 5f 62 79 74 65 73 2c 0a 20 20 73  _blob_bytes,.  s
28de1 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
28de2 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  e,.  sqlite3_blo
28de3 62 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65  b_open,.  sqlite
28de4 33 5f 62 6c 6f 62 5f 72 65 61 64 2c 0a 20 20 73  3_blob_read,.  s
28de5 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
28de6 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  e,.  sqlite3_cre
28de7 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
28de8 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ,.  sqlite3_file
28de9 5f 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69  _control,.  sqli
28dea 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77  te3_memory_highw
28deb 61 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ater,.  sqlite3_
28dec 6d 65 6d 6f 72 79 5f 75 73 65 64 2c 0a 23 69 66  memory_used,.#if
28ded 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
28dee 5f 4f 4d 49 54 0a 20 20 30 2c 20 0a 20 20 30 2c  _OMIT.  0, .  0,
28def 20 0a 20 20 30 2c 0a 20 20 30 2c 0a 20 20 30 2c   .  0,.  0,.  0,
28df0 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33  .#else.  sqlite3
28df1 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20  _mutex_alloc,.  
28df2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
28df3 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ter,.  sqlite3_m
28df4 75 74 65 78 5f 66 72 65 65 2c 0a 20 20 73 71 6c  utex_free,.  sql
28df5 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
28df6 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ,.  sqlite3_mute
28df7 78 5f 74 72 79 2c 0a 23 65 6e 64 69 66 0a 20 20  x_try,.#endif.  
28df8 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 2c  sqlite3_open_v2,
28df9 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  .  sqlite3_relea
28dfa 73 65 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73 71 6c  se_memory,.  sql
28dfb 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
28dfc 72 5f 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c 69 74  r_nomem,.  sqlit
28dfd 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
28dfe 74 6f 6f 62 69 67 2c 0a 20 20 73 71 6c 69 74 65  toobig,.  sqlite
28dff 33 5f 73 6c 65 65 70 2c 0a 20 20 73 71 6c 69 74  3_sleep,.  sqlit
28e00 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
28e01 69 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  it,.  sqlite3_vf
28e02 73 5f 66 69 6e 64 2c 0a 20 20 73 71 6c 69 74 65  s_find,.  sqlite
28e03 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 2c 0a  3_vfs_register,.
28e04 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e    sqlite3_vfs_un
28e05 72 65 67 69 73 74 65 72 2c 0a 0a 20 20 2f 2a 0a  register,..  /*.
28e06 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
28e07 2e 35 2e 38 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  .5.8.  */.  sqli
28e08 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 2c 0a  te3_threadsafe,.
28e09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
28e0a 5f 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c  _zeroblob,.  sql
28e0b 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
28e0c 72 5f 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65  r_code,.  sqlite
28e0d 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 2c 0a  3_test_control,.
28e0e 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
28e0f 6e 65 73 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ness,.  sqlite3_
28e10 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
28e11 65 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64  e,..  /*.  ** Ad
28e12 64 65 64 20 66 6f 72 20 33 2e 36 2e 30 0a 20 20  ded for 3.6.0.  
28e13 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 74  */.  sqlite3_ext
28e14 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64  ended_result_cod
28e15 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69  es,.  sqlite3_li
28e16 6d 69 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6e  mit,.  sqlite3_n
28e17 65 78 74 5f 73 74 6d 74 2c 0a 20 20 73 71 6c 69  ext_stmt,.  sqli
28e18 74 65 33 5f 73 71 6c 2c 0a 20 20 73 71 6c 69 74  te3_sql,.  sqlit
28e19 65 33 5f 73 74 61 74 75 73 2c 0a 7d 3b 0a 0a 2f  e3_status,.};../
28e1a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
28e1b 6c 6f 61 64 20 61 6e 20 53 51 4c 69 74 65 20 65  load an SQLite e
28e1c 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79  xtension library
28e1d 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68   contained in th
28e1e 65 20 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e  e file.** zFile.
28e1f 20 20 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e    The entry poin
28e20 74 20 69 73 20 7a 50 72 6f 63 2e 20 20 7a 50 72  t is zProc.  zPr
28e21 6f 63 20 6d 61 79 20 62 65 20 30 20 69 6e 20 77  oc may be 0 in w
28e22 68 69 63 68 20 63 61 73 65 20 61 0a 2a 2a 20 64  hich case a.** d
28e23 65 66 61 75 6c 74 20 65 6e 74 72 79 20 70 6f 69  efault entry poi
28e24 6e 74 20 6e 61 6d 65 20 28 73 71 6c 69 74 65 33  nt name (sqlite3
28e25 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 29  _extension_init)
28e26 20 69 73 20 75 73 65 64 2e 20 20 55 73 65 0a 2a   is used.  Use.*
28e27 2a 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74  * of the default
28e28 20 6e 61 6d 65 20 69 73 20 72 65 63 6f 6d 6d 65   name is recomme
28e29 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  nded..**.** Retu
28e2a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
28e2b 73 75 63 63 65 73 73 20 61 6e 64 20 53 51 4c 49  success and SQLI
28e2c 54 45 5f 45 52 52 4f 52 20 69 66 20 73 6f 6d 65  TE_ERROR if some
28e2d 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
28e2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72  ..**.** If an er
28e2f 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70  ror occurs and p
28e30 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30  zErrMsg is not 0
28e31 2c 20 74 68 65 6e 20 66 69 6c 6c 20 2a 70 7a 45  , then fill *pzE
28e32 72 72 4d 73 67 20 77 69 74 68 20 0a 2a 2a 20 65  rrMsg with .** e
28e33 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78  rror message tex
28e34 74 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  t.  The calling 
28e35 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
28e36 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79  free this memory
28e37 0a 2a 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20 73  .** by calling s
28e38 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
28e39 20 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   )..*/.static in
28e3a 74 20 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74  t sqlite3LoadExt
28e3b 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  ension(.  sqlite
28e3c 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
28e3d 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65  /* Load the exte
28e3e 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20  nsion into this 
28e3f 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
28e40 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
28e41 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f  har *zFile,    /
28e42 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68  * Name of the sh
28e43 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e  ared library con
28e44 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f  taining extensio
28e45 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
28e46 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20  r *zProc,    /* 
28e47 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 55 73  Entry point.  Us
28e48 65 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  e "sqlite3_exten
28e49 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30 20  sion_init" if 0 
28e4a 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  */.  char **pzEr
28e4b 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75  rMsg       /* Pu
28e4c 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  t error message 
28e4d 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f  here if not 0 */
28e4e 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  .){.  sqlite3_vf
28e4f 73 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70 56  s *pVfs = db->pV
28e50 66 73 3b 0a 20 20 76 6f 69 64 20 2a 68 61 6e 64  fs;.  void *hand
28e51 6c 65 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69  le;.  int (*xIni
28e52 74 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72  t)(sqlite3*,char
28e53 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  **,const sqlite3
28e54 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b  _api_routines*);
28e55 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67  .  char *zErrmsg
28e56 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 2a 61   = 0;.  void **a
28e57 48 61 6e 64 6c 65 3b 0a 20 20 63 6f 6e 73 74 20  Handle;.  const 
28e58 69 6e 74 20 6e 4d 73 67 20 3d 20 33 30 30 3b 0a  int nMsg = 300;.
28e59 0a 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20  .  if( pzErrMsg 
28e5a 29 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ) *pzErrMsg = 0;
28e5b 0a 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31  ..  /* Ticket #1
28e5c 38 36 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61  863.  To avoid a
28e5d 20 63 72 65 61 74 69 6e 67 20 73 65 63 75 72 69   creating securi
28e5e 74 79 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  ty problems for 
28e5f 6f 6c 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69  older.  ** appli
28e60 63 61 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c  cations that rel
28e61 69 6e 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65  ink against newe
28e62 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  r versions of SQ
28e63 4c 69 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61  Lite, the.  ** a
28e64 62 69 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f  bility to run lo
28e65 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  ad_extension is 
28e66 74 75 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65  turned off by de
28e67 66 61 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a  fault.  One.  **
28e68 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74   must call sqlit
28e69 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65  e3_enable_load_e
28e6a 78 74 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75  xtension() to tu
28e6b 72 6e 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  rn on extension.
28e6c 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f    ** loading.  O
28e6d 74 68 65 72 77 69 73 65 20 79 6f 75 20 67 65 74  therwise you get
28e6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
28e6f 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rror..  */.  if(
28e70 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
28e71 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69  LITE_LoadExtensi
28e72 6f 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  on)==0 ){.    if
28e73 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
28e74 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
28e75 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
28e76 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22  "not authorized"
28e77 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
28e78 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
28e79 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72  ;.  }..  if( zPr
28e7a 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72  oc==0 ){.    zPr
28e7b 6f 63 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78  oc = "sqlite3_ex
28e7c 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20  tension_init";. 
28e7d 20 7d 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73   }..  handle = s
28e7e 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70  qlite3OsDlOpen(p
28e7f 56 66 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69  Vfs, zFile);.  i
28e80 66 28 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a  f( handle==0 ){.
28e81 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
28e82 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 6d 73   ){.      zErrms
28e83 67 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b  g = sqlite3Stack
28e84 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 4d  AllocZero(db, nM
28e85 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  sg);.      if( z
28e86 45 72 72 6d 73 67 20 29 7b 0a 20 20 20 20 20 20  Errmsg ){.      
28e87 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
28e88 74 66 28 6e 4d 73 67 2c 20 7a 45 72 72 6d 73 67  tf(nMsg, zErrmsg
28e89 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  , .            "
28e8a 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 73  unable to open s
28e8b 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25  hared library [%
28e8c 73 5d 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20  s]", zFile);.   
28e8d 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c       sqlite3OsDl
28e8e 45 72 72 6f 72 28 70 56 66 73 2c 20 6e 4d 73 67  Error(pVfs, nMsg
28e8f 2d 31 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  -1, zErrmsg);.  
28e90 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
28e91 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
28e92 70 28 30 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20  p(0, zErrmsg);. 
28e93 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
28e94 61 63 6b 46 72 65 65 28 64 62 2c 20 7a 45 72 72  ackFree(db, zErr
28e95 6d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  msg);.      }.  
28e96 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53    }.    return S
28e97 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
28e98 0a 20 20 78 49 6e 69 74 20 3d 20 28 69 6e 74 28  .  xInit = (int(
28e99 2a 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72  *)(sqlite3*,char
28e9a 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  **,const sqlite3
28e9b 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29  _api_routines*))
28e9c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28e9d 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 53      sqlite3OsDlS
28e9e 79 6d 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 2c  ym(pVfs, handle,
28e9f 20 7a 50 72 6f 63 29 3b 0a 20 20 69 66 28 20 78   zProc);.  if( x
28ea0 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Init==0 ){.    i
28ea1 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
28ea2 20 20 20 20 20 7a 45 72 72 6d 73 67 20 3d 20 73       zErrmsg = s
28ea3 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
28ea4 5a 65 72 6f 28 64 62 2c 20 6e 4d 73 67 29 3b 0a  Zero(db, nMsg);.
28ea5 20 20 20 20 20 20 69 66 28 20 7a 45 72 72 6d 73        if( zErrms
28ea6 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  g ){.        sql
28ea7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4d  ite3_snprintf(nM
28ea8 73 67 2c 20 7a 45 72 72 6d 73 67 2c 0a 20 20 20  sg, zErrmsg,.   
28ea9 20 20 20 20 20 20 20 20 20 22 6e 6f 20 65 6e 74           "no ent
28eaa 72 79 20 70 6f 69 6e 74 20 5b 25 73 5d 20 69 6e  ry point [%s] in
28eab 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
28eac 5b 25 73 5d 22 2c 20 7a 50 72 6f 63 2c 7a 46 69  [%s]", zProc,zFi
28ead 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  le);.        sql
28eae 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56  ite3OsDlError(pV
28eaf 66 73 2c 20 6e 4d 73 67 2d 31 2c 20 7a 45 72 72  fs, nMsg-1, zErr
28eb0 6d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70  msg);.        *p
28eb1 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
28eb2 33 44 62 53 74 72 44 75 70 28 30 2c 20 7a 45 72  3DbStrDup(0, zEr
28eb3 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 73  rmsg);.        s
28eb4 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28  qlite3StackFree(
28eb5 64 62 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  db, zErrmsg);.  
28eb6 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
28eb7 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66  te3OsDlClose(pVf
28eb8 73 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20  s, handle);.    
28eb9 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  }.    return SQL
28eba 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c  ITE_ERROR;.  }el
28ebb 73 65 20 69 66 28 20 78 49 6e 69 74 28 64 62 2c  se if( xInit(db,
28ebc 20 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69   &zErrmsg, &sqli
28ebd 74 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20  te3Apis) ){.    
28ebe 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a  if( pzErrMsg ){.
28ebf 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
28ec0 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
28ec1 66 28 22 65 72 72 6f 72 20 64 75 72 69 6e 67 20  f("error during 
28ec2 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 3a 20  initialization: 
28ec3 25 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20  %s", zErrmsg);. 
28ec4 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
28ec5 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a  _free(zErrmsg);.
28ec6 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43      sqlite3OsDlC
28ec7 6c 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c  lose(pVfs, handl
28ec8 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  e);.    return S
28ec9 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
28eca 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68  ..  /* Append th
28ecb 65 20 6e 65 77 20 73 68 61 72 65 64 20 6c 69 62  e new shared lib
28ecc 72 61 72 79 20 68 61 6e 64 6c 65 20 74 6f 20 74  rary handle to t
28ecd 68 65 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  he db->aExtensio
28ece 6e 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20 61 48  n array. */.  aH
28ecf 61 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 44  andle = sqlite3D
28ed0 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
28ed1 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 28  sizeof(handle)*(
28ed2 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 31  db->nExtension+1
28ed3 29 29 3b 0a 20 20 69 66 28 20 61 48 61 6e 64 6c  ));.  if( aHandl
28ed4 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
28ed5 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
28ed6 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6e  .  }.  if( db->n
28ed7 45 78 74 65 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20  Extension>0 ){. 
28ed8 20 20 20 6d 65 6d 63 70 79 28 61 48 61 6e 64 6c     memcpy(aHandl
28ed9 65 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  e, db->aExtensio
28eda 6e 2c 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65  n, sizeof(handle
28edb 29 2a 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  )*db->nExtension
28edc 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
28edd 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61  DbFree(db, db->a
28ede 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 64 62  Extension);.  db
28edf 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 20 3d 20 61  ->aExtension = a
28ee0 48 61 6e 64 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61  Handle;..  db->a
28ee1 45 78 74 65 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45  Extension[db->nE
28ee2 78 74 65 6e 73 69 6f 6e 2b 2b 5d 20 3d 20 68 61  xtension++] = ha
28ee3 6e 64 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53  ndle;.  return S
28ee4 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49  QLITE_OK;.}.SQLI
28ee5 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
28ee6 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  e3_load_extensio
28ee7 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
28ee8 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ,          /* Lo
28ee9 61 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  ad the extension
28eea 20 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62   into this datab
28eeb 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
28eec 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
28eed 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d  zFile,    /* Nam
28eee 65 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20  e of the shared 
28eef 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69  library containi
28ef0 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a  ng extension */.
28ef1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
28ef2 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79  roc,    /* Entry
28ef3 20 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71   point.  Use "sq
28ef4 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
28ef5 69 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20  init" if 0 */.  
28ef6 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20  char **pzErrMsg 
28ef7 20 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72        /* Put err
28ef8 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20  or message here 
28ef9 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20  if not 0 */.){. 
28efa 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
28efb 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
28efc 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20  b->mutex);.  rc 
28efd 3d 20 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74  = sqlite3LoadExt
28efe 65 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65  ension(db, zFile
28eff 2c 20 7a 50 72 6f 63 2c 20 70 7a 45 72 72 4d 73  , zProc, pzErrMs
28f00 67 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  g);.  rc = sqlit
28f01 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
28f02 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
28f03 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
28f04 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
28f05 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
28f06 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
28f07 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  n the database c
28f08 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
28f09 73 69 6e 67 20 69 6e 20 6f 72 64 65 72 0a 2a 2a  sing in order.**
28f0a 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 6c 6f 61   to clean up loa
28f0b 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 0a 2a  ded extensions.*
28f0c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
28f0d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f   void sqlite3Clo
28f0e 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c  seExtensions(sql
28f0f 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
28f10 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   i;.  assert( sq
28f11 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
28f12 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
28f13 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
28f14 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29  nExtension; i++)
28f15 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44  {.    sqlite3OsD
28f16 6c 43 6c 6f 73 65 28 64 62 2d 3e 70 56 66 73 2c  lClose(db->pVfs,
28f17 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b   db->aExtension[
28f18 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  i]);.  }.  sqlit
28f19 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d  e3DbFree(db, db-
28f1a 3e 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 7d 0a  >aExtension);.}.
28f1b 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72  ./*.** Enable or
28f1c 20 64 69 73 61 62 6c 65 20 65 78 74 65 6e 73 69   disable extensi
28f1d 6f 6e 20 6c 6f 61 64 69 6e 67 2e 20 20 45 78 74  on loading.  Ext
28f1e 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69  ension loading i
28f1f 73 20 64 69 73 61 62 6c 65 64 20 62 79 0a 2a 2a  s disabled by.**
28f20 20 64 65 66 61 75 6c 74 20 73 6f 20 61 73 20 6e   default so as n
28f21 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72  ot to open secur
28f22 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64  ity holes in old
28f23 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e  er applications.
28f24 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
28f25 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  nt sqlite3_enabl
28f26 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e_load_extension
28f27 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
28f28 74 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69  t onoff){.  sqli
28f29 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
28f2a 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66  db->mutex);.  if
28f2b 28 20 6f 6e 6f 66 66 20 29 7b 0a 20 20 20 20 64  ( onoff ){.    d
28f2c 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
28f2d 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
28f2e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
28f2f 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
28f30 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
28f31 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  n;.  }.  sqlite3
28f32 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
28f33 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
28f34 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
28f35 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
28f36 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
28f37 53 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  SION */../*.** T
28f38 68 65 20 61 75 74 6f 2d 65 78 74 65 6e 73 69 6f  he auto-extensio
28f39 6e 20 63 6f 64 65 20 61 64 64 65 64 20 72 65 67  n code added reg
28f3a 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
28f3b 65 72 20 6f 72 20 6e 6f 74 20 65 78 74 65 6e 73  er or not extens
28f3c 69 6f 6e 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 69  ion.** loading i
28f3d 73 20 73 75 70 70 6f 72 74 65 64 2e 20 20 57 65  s supported.  We
28f3e 20 6e 65 65 64 20 61 20 64 75 6d 6d 79 20 73 71   need a dummy sq
28f3f 6c 69 74 65 33 41 70 69 73 20 70 6f 69 6e 74 65  lite3Apis pointe
28f40 72 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f  r for that.** co
28f41 64 65 20 69 66 20 72 65 67 75 6c 61 72 20 65 78  de if regular ex
28f42 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
28f43 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
28f44 2e 20 20 54 68 69 73 20 69 73 20 74 68 61 74 0a  .  This is that.
28f45 2a 2a 20 64 75 6d 6d 79 20 70 6f 69 6e 74 65 72  ** dummy pointer
28f46 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
28f47 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
28f48 45 4e 53 49 4f 4e 0a 73 74 61 74 69 63 20 63 6f  ENSION.static co
28f49 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
28f4a 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33  routines sqlite3
28f4b 41 70 69 73 20 3d 20 7b 20 30 20 7d 3b 0a 23 65  Apis = { 0 };.#e
28f4c 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ndif.../*.** The
28f4d 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63   following objec
28f4e 74 20 68 6f 6c 64 73 20 74 68 65 20 6c 69 73 74  t holds the list
28f4f 20 6f 66 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   of automaticall
28f50 79 20 6c 6f 61 64 65 64 0a 2a 2a 20 65 78 74 65  y loaded.** exte
28f51 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  nsions..**.** Th
28f52 69 73 20 6c 69 73 74 20 69 73 20 73 68 61 72 65  is list is share
28f53 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73  d across threads
28f54 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55  .  The SQLITE_MU
28f55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
28f56 52 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20  R.** mutex must 
28f57 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 61 63  be held while ac
28f58 63 65 73 73 69 6e 67 20 74 68 69 73 20 6c 69 73  cessing this lis
28f59 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
28f5a 72 75 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f  ruct sqlite3Auto
28f5b 45 78 74 4c 69 73 74 20 73 71 6c 69 74 65 33 41  ExtList sqlite3A
28f5c 75 74 6f 45 78 74 4c 69 73 74 3b 0a 73 74 61 74  utoExtList;.stat
28f5d 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  ic SQLITE_WSD st
28f5e 72 75 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f  ruct sqlite3Auto
28f5f 45 78 74 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20  ExtList {.  int 
28f60 6e 45 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  nExt;           
28f61 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
28f62 65 6e 74 72 69 65 73 20 69 6e 20 61 45 78 74 5b  entries in aExt[
28f63 5d 20 2a 2f 20 20 20 20 20 20 20 20 20 20 0a 20  ] */          . 
28f64 20 76 6f 69 64 20 28 2a 2a 61 45 78 74 29 28 76   void (**aExt)(v
28f65 6f 69 64 29 3b 20 20 20 2f 2a 20 50 6f 69 6e 74  oid);   /* Point
28f66 65 72 73 20 74 6f 20 74 68 65 20 65 78 74 65 6e  ers to the exten
28f67 73 69 6f 6e 20 69 6e 69 74 20 66 75 6e 63 74 69  sion init functi
28f68 6f 6e 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33  ons */.} sqlite3
28f69 41 75 74 6f 65 78 74 20 3d 20 7b 20 30 2c 20 30  Autoext = { 0, 0
28f6a 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64   };../* The "wsd
28f6b 41 75 74 6f 65 78 74 22 20 6d 61 63 72 6f 20 77  Autoext" macro w
28f6c 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74  ill resolve to t
28f6d 68 65 20 61 75 74 6f 65 78 74 65 6e 73 69 6f 6e  he autoextension
28f6e 0a 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72  .** state vector
28f6f 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73  .  If writable s
28f70 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e  tatic data is un
28f71 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65  supported on the
28f72 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68   target,.** we h
28f73 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  ave to locate th
28f74 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61  e state vector a
28f75 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20  t run-time.  In 
28f76 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a  the more common.
28f77 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72  ** case where wr
28f78 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
28f79 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c  ta is supported,
28f7a 20 77 73 64 53 74 61 74 20 63 61 6e 20 72 65 66   wsdStat can ref
28f7b 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74  er directly.** t
28f7c 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 41 75  o the "sqlite3Au
28f7d 74 6f 65 78 74 22 20 73 74 61 74 65 20 76 65 63  toext" state vec
28f7e 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f  tor declared abo
28f7f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ve..*/.#ifdef SQ
28f80 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20  LITE_OMIT_WSD.# 
28f81 64 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78  define wsdAutoex
28f82 74 49 6e 69 74 20 5c 0a 20 20 73 71 6c 69 74 65  tInit \.  sqlite
28f83 33 41 75 74 6f 45 78 74 4c 69 73 74 20 2a 78 20  3AutoExtList *x 
28f84 3d 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65  = &GLOBAL(sqlite
28f85 33 41 75 74 6f 45 78 74 4c 69 73 74 2c 73 71 6c  3AutoExtList,sql
28f86 69 74 65 33 41 75 74 6f 65 78 74 29 0a 23 20 64  ite3Autoext).# d
28f87 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74  efine wsdAutoext
28f88 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65   x[0].#else.# de
28f89 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 49  fine wsdAutoextI
28f8a 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64  nit.# define wsd
28f8b 41 75 74 6f 65 78 74 20 73 71 6c 69 74 65 33 41  Autoext sqlite3A
28f8c 75 74 6f 65 78 74 0a 23 65 6e 64 69 66 0a 0a 0a  utoext.#endif...
28f8d 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
28f8e 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b   statically link
28f8f 65 64 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61  ed extension tha
28f90 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
28f91 6c 79 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20  ly.** loaded by 
28f92 65 76 65 72 79 20 6e 65 77 20 64 61 74 61 62 61  every new databa
28f93 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
28f94 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
28f95 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78   sqlite3_auto_ex
28f96 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78  tension(void (*x
28f97 49 6e 69 74 29 28 76 6f 69 64 29 29 7b 0a 20 20  Init)(void)){.  
28f98 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
28f99 4f 4b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  OK;.#ifndef SQLI
28f9a 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
28f9b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
28f9c 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20  initialize();.  
28f9d 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
28f9e 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65  turn rc;.  }else
28f9f 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20  .#endif.  {.    
28fa0 69 6e 74 20 69 3b 0a 23 69 66 20 53 51 4c 49 54  int i;.#if SQLIT
28fa1 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20  E_THREADSAFE.   
28fa2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
28fa3 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
28fa4 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
28fa5 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
28fa6 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20  STER);.#endif.  
28fa7 20 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74    wsdAutoextInit
28fa8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
28fa9 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
28faa 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
28fab 3c 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74  <wsdAutoext.nExt
28fac 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
28fad 28 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78  ( wsdAutoext.aEx
28fae 74 5b 69 5d 3d 3d 78 49 6e 69 74 20 29 20 62 72  t[i]==xInit ) br
28faf 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
28fb0 66 28 20 69 3d 3d 77 73 64 41 75 74 6f 65 78 74  f( i==wsdAutoext
28fb1 2e 6e 45 78 74 20 29 7b 0a 20 20 20 20 20 20 69  .nExt ){.      i
28fb2 6e 74 20 6e 42 79 74 65 20 3d 20 28 77 73 64 41  nt nByte = (wsdA
28fb3 75 74 6f 65 78 74 2e 6e 45 78 74 2b 31 29 2a 73  utoext.nExt+1)*s
28fb4 69 7a 65 6f 66 28 77 73 64 41 75 74 6f 65 78 74  izeof(wsdAutoext
28fb5 2e 61 45 78 74 5b 30 5d 29 3b 0a 20 20 20 20 20  .aExt[0]);.     
28fb6 20 76 6f 69 64 20 28 2a 2a 61 4e 65 77 29 28 76   void (**aNew)(v
28fb7 6f 69 64 29 3b 0a 20 20 20 20 20 20 61 4e 65 77  oid);.      aNew
28fb8 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
28fb9 6f 63 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45  oc(wsdAutoext.aE
28fba 78 74 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20  xt, nByte);.    
28fbb 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b    if( aNew==0 ){
28fbc 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
28fbd 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
28fbe 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
28fbf 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
28fc0 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20   = aNew;.       
28fc1 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
28fc2 5b 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74  [wsdAutoext.nExt
28fc3 5d 20 3d 20 78 49 6e 69 74 3b 0a 20 20 20 20 20  ] = xInit;.     
28fc4 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45     wsdAutoext.nE
28fc5 78 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  xt++;.      }.  
28fc6 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
28fc7 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
28fc8 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  x);.    assert( 
28fc9 28 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b  (rc&0xff)==rc );
28fca 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
28fcb 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73    }.}../*.** Res
28fcc 65 74 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  et the automatic
28fcd 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
28fce 6e 67 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f  ng mechanism..*/
28fcf 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
28fd0 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61   sqlite3_reset_a
28fd1 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f  uto_extension(vo
28fd2 69 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  id){.#ifndef SQL
28fd3 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
28fd4 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
28fd5 69 6e 69 74 69 61 6c 69 7a 65 28 29 3d 3d 53 51  initialize()==SQ
28fd6 4c 49 54 45 5f 4f 4b 20 29 0a 23 65 6e 64 69 66  LITE_OK ).#endif
28fd7 0a 20 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  .  {.#if SQLITE_
28fd8 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73  THREADSAFE.    s
28fd9 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
28fda 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
28fdb 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
28fdc 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
28fdd 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ER);.#endif.    
28fde 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a  wsdAutoextInit;.
28fdf 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
28fe0 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a  x_enter(mutex);.
28fe1 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
28fe2 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74  (wsdAutoext.aExt
28fe3 29 3b 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78  );.    wsdAutoex
28fe4 74 2e 61 45 78 74 20 3d 20 30 3b 0a 20 20 20 20  t.aExt = 0;.    
28fe5 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20  wsdAutoext.nExt 
28fe6 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
28fe7 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
28fe8 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ex);.  }.}../*.*
28fe9 2a 20 4c 6f 61 64 20 61 6c 6c 20 61 75 74 6f 6d  * Load all autom
28fea 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e  atic extensions.
28feb 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69  .**.** If anythi
28fec 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 73  ng goes wrong, s
28fed 65 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 74  et an error in t
28fee 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
28fef 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
28ff0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
28ff1 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78  qlite3AutoLoadEx
28ff2 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33  tensions(sqlite3
28ff3 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *db){.  int i;.
28ff4 20 20 69 6e 74 20 67 6f 20 3d 20 31 3b 0a 20 20    int go = 1;.  
28ff5 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c  int (*xInit)(sql
28ff6 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e  ite3*,char**,con
28ff7 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
28ff8 6f 75 74 69 6e 65 73 2a 29 3b 0a 0a 20 20 77 73  outines*);..  ws
28ff9 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20  dAutoextInit;.  
28ffa 69 66 28 20 77 73 64 41 75 74 6f 65 78 74 2e 6e  if( wsdAutoext.n
28ffb 45 78 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  Ext==0 ){.    /*
28ffc 20 43 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61   Common case: ea
28ffd 72 6c 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20  rly out without 
28ffe 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20  every having to 
28fff 61 63 71 75 69 72 65 20 61 20 6d 75 74 65 78 20  acquire a mutex 
29000 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  */.    return;. 
29001 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 67 6f   }.  for(i=0; go
29002 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72  ; i++){.    char
29003 20 2a 7a 45 72 72 6d 73 67 3b 0a 23 69 66 20 53   *zErrmsg;.#if S
29004 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
29005 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
29006 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
29007 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
29008 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
29009 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69  C_MASTER);.#endi
2900a 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  f.    sqlite3_mu
2900b 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
2900c 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 77 73 64  ;.    if( i>=wsd
2900d 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a  Autoext.nExt ){.
2900e 20 20 20 20 20 20 78 49 6e 69 74 20 3d 20 30 3b        xInit = 0;
2900f 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20  .      go = 0;. 
29010 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29011 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28  xInit = (int(*)(
29012 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c  sqlite3*,char**,
29013 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
29014 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20  i_routines*)).  
29015 20 20 20 20 20 20 20 20 20 20 20 20 77 73 64 41              wsdA
29016 75 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3b 0a  utoext.aExt[i];.
29017 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
29018 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
29019 74 65 78 29 3b 0a 20 20 20 20 7a 45 72 72 6d 73  tex);.    zErrms
2901a 67 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 78  g = 0;.    if( x
2901b 49 6e 69 74 20 26 26 20 78 49 6e 69 74 28 64 62  Init && xInit(db
2901c 2c 20 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c  , &zErrmsg, &sql
2901d 69 74 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20  ite3Apis) ){.   
2901e 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
2901f 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  db, SQLITE_ERROR
29020 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 61  ,.            "a
29021 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69  utomatic extensi
29022 6f 6e 20 6c 6f 61 64 69 6e 67 20 66 61 69 6c 65  on loading faile
29023 64 3a 20 25 73 22 2c 20 7a 45 72 72 6d 73 67 29  d: %s", zErrmsg)
29024 3b 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a  ;.      go = 0;.
29025 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
29026 33 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b  3_free(zErrmsg);
29027 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  .  }.}../*******
29028 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c  ******* End of l
29029 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  oadext.c *******
2902a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2902b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2902c 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2902d 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2902e 6c 65 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a  le pragma.c ****
2902f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29031 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
29032 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a  03 April 6.**.**
29033 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
29034 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
29035 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
29036 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
29037 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
29038 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
29039 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2903a 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2903b 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2903c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2903d 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2903e 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2903f 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
29040 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
29041 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
29042 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
29043 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
29044 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29045 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29046 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29047 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29048 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
29049 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2904a 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70  code used to imp
2904b 6c 65 6d 65 6e 74 20 74 68 65 20 50 52 41 47 4d  lement the PRAGM
2904c 41 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 0a 2f  A command..*/../
2904d 2a 20 49 67 6e 6f 72 65 20 74 68 69 73 20 77 68  * Ignore this wh
2904e 6f 6c 65 20 66 69 6c 65 20 69 66 20 70 72 61 67  ole file if prag
2904f 6d 61 73 20 61 72 65 20 64 69 73 61 62 6c 65 64  mas are disabled
29050 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
29051 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41  (SQLITE_OMIT_PRA
29052 47 4d 41 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  GMA)../*.** Inte
29053 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
29054 73 74 72 69 6e 67 20 61 73 20 61 20 73 61 66 65  string as a safe
29055 74 79 20 6c 65 76 65 6c 2e 20 20 52 65 74 75 72  ty level.  Retur
29056 6e 20 30 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a 20  n 0 for OFF,.** 
29057 31 20 66 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52 4d  1 for ON or NORM
29058 41 4c 20 61 6e 64 20 32 20 66 6f 72 20 46 55 4c  AL and 2 for FUL
29059 4c 2e 20 20 52 65 74 75 72 6e 20 31 20 66 6f 72  L.  Return 1 for
2905a 20 61 6e 20 65 6d 70 74 79 20 6f 72 20 0a 2a 2a   an empty or .**
2905b 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 73 74   unrecognized st
2905c 72 69 6e 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  ring argument..*
2905d 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
2905e 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  he values return
2905f 65 64 20 61 72 65 20 6f 6e 65 20 6c 65 73 73 20  ed are one less 
29060 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20  that the values 
29061 74 68 61 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  that.** should b
29062 65 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73 71  e passed into sq
29063 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66  lite3BtreeSetSaf
29064 65 74 79 4c 65 76 65 6c 28 29 2e 20 20 54 68 65  etyLevel().  The
29065 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 73   is done.** to s
29066 75 70 70 6f 72 74 20 6c 65 67 61 63 79 20 53 51  upport legacy SQ
29067 4c 20 63 6f 64 65 2e 20 20 54 68 65 20 73 61 66  L code.  The saf
29068 65 74 79 20 6c 65 76 65 6c 20 75 73 65 64 20 74  ety level used t
29069 6f 20 62 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20  o be boolean.** 
2906a 61 6e 64 20 6f 6c 64 65 72 20 73 63 72 69 70 74  and older script
2906b 73 20 6d 61 79 20 68 61 76 65 20 75 73 65 64 20  s may have used 
2906c 6e 75 6d 62 65 72 73 20 30 20 66 6f 72 20 4f 46  numbers 0 for OF
2906d 46 20 61 6e 64 20 31 20 66 6f 72 20 4f 4e 2e 0a  F and 1 for ON..
2906e 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 67 65 74  */.static u8 get
2906f 53 61 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e 73  SafetyLevel(cons
29070 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 20 20  t char *z){.    
29071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29072 20 20 20 20 20 20 20 20 20 2f 2a 20 31 32 33 34           /* 1234
29073 35 36 37 38 39 20 31 32 33 34 35 36 37 38 39 20  56789 123456789 
29074 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
29075 74 20 63 68 61 72 20 7a 54 65 78 74 5b 5d 20 3d  t char zText[] =
29076 20 22 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74 72   "onoffalseyestr
29077 75 65 66 75 6c 6c 22 3b 0a 20 20 73 74 61 74 69  uefull";.  stati
29078 63 20 63 6f 6e 73 74 20 75 38 20 69 4f 66 66 73  c const u8 iOffs
29079 65 74 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20 32 2c  et[] = {0, 1, 2,
2907a 20 34 2c 20 39 2c 20 31 32 2c 20 31 36 7d 3b 0a   4, 9, 12, 16};.
2907b 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2907c 38 20 69 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b 32  8 iLength[] = {2
2907d 2c 20 32 2c 20 33 2c 20 35 2c 20 33 2c 20 34 2c  , 2, 3, 5, 3, 4,
2907e 20 34 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f   4};.  static co
2907f 6e 73 74 20 75 38 20 69 56 61 6c 75 65 5b 5d 20  nst u8 iValue[] 
29080 3d 20 20 7b 31 2c 20 30 2c 20 30 2c 20 30 2c 20  =  {1, 0, 0, 0, 
29081 31 2c 20 31 2c 20 32 7d 3b 0a 20 20 69 6e 74 20  1, 1, 2};.  int 
29082 69 2c 20 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69  i, n;.  if( sqli
29083 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29  te3Isdigit(*z) )
29084 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 75 38  {.    return (u8
29085 29 61 74 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20 20  )atoi(z);.  }.  
29086 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
29087 6e 33 30 28 7a 29 3b 0a 20 20 66 6f 72 28 69 3d  n30(z);.  for(i=
29088 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 69  0; i<ArraySize(i
29089 4c 65 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a 20  Length); i++){. 
2908a 20 20 20 69 66 28 20 69 4c 65 6e 67 74 68 5b 69     if( iLength[i
2908b 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53  ]==n && sqlite3S
2908c 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 69  trNICmp(&zText[i
2908d 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d  Offset[i]],z,n)=
2908e 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
2908f 72 6e 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20 20  rn iValue[i];.  
29090 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
29091 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74   1;.}../*.** Int
29092 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
29093 20 73 74 72 69 6e 67 20 61 73 20 61 20 62 6f 6f   string as a boo
29094 6c 65 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73  lean value..*/.s
29095 74 61 74 69 63 20 75 38 20 67 65 74 42 6f 6f 6c  tatic u8 getBool
29096 65 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ean(const char *
29097 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 65 74  z){.  return get
29098 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 29 26 31  SafetyLevel(z)&1
29099 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  ;.}../*.** Inter
2909a 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73  pret the given s
2909b 74 72 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b 69  tring as a locki
2909c 6e 67 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a 2a  ng mode value..*
2909d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
2909e 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e 73  LockingMode(cons
2909f 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66  t char *z){.  if
290a0 28 20 7a 20 29 7b 0a 20 20 20 20 69 66 28 20 30  ( z ){.    if( 0
290a1 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
290a2 28 7a 2c 20 22 65 78 63 6c 75 73 69 76 65 22 29  (z, "exclusive")
290a3 20 29 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f   ) return PAGER_
290a4 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
290a5 55 53 49 56 45 3b 0a 20 20 20 20 69 66 28 20 30  USIVE;.    if( 0
290a6 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
290a7 28 7a 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29 20  (z, "normal") ) 
290a8 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43  return PAGER_LOC
290a9 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3b  KINGMODE_NORMAL;
290aa 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 50 41  .  }.  return PA
290ab 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
290ac 51 55 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64 65  QUERY;.}..#ifnde
290ad 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
290ae 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49  TOVACUUM./*.** I
290af 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76  nterpret the giv
290b0 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 6e 20  en string as an 
290b1 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
290b2 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   value..**.** Th
290b3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 69  e following stri
290b4 6e 67 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66 75  ngs, "none", "fu
290b5 6c 6c 22 20 61 6e 64 20 22 69 6e 63 72 65 6d 65  ll" and "increme
290b6 6e 74 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61 63  ntal" are .** ac
290b7 63 65 70 74 61 62 6c 65 2c 20 61 73 20 61 72 65  ceptable, as are
290b8 20 74 68 65 69 72 20 6e 75 6d 65 72 69 63 20 65   their numeric e
290b9 71 75 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20 31  quivalents: 0, 1
290ba 20 61 6e 64 20 32 20 72 65 73 70 65 63 74 69 76   and 2 respectiv
290bb 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ely..*/.static i
290bc 6e 74 20 67 65 74 41 75 74 6f 56 61 63 75 75 6d  nt getAutoVacuum
290bd 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
290be 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
290bf 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
290c0 70 28 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20 72  p(z, "none") ) r
290c1 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f  eturn BTREE_AUTO
290c2 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20 69  VACUUM_NONE;.  i
290c3 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  f( 0==sqlite3Str
290c4 49 43 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29 20  ICmp(z, "full") 
290c5 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41  ) return BTREE_A
290c6 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b 0a  UTOVACUUM_FULL;.
290c7 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33    if( 0==sqlite3
290c8 53 74 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63 72  StrICmp(z, "incr
290c9 65 6d 65 6e 74 61 6c 22 29 20 29 20 72 65 74 75  emental") ) retu
290ca 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  rn BTREE_AUTOVAC
290cb 55 55 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d 20  UUM_INCR;.  i = 
290cc 61 74 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75 72  atoi(z);.  retur
290cd 6e 20 28 75 38 29 28 28 69 3e 3d 30 26 26 69 3c  n (u8)((i>=0&&i<
290ce 3d 32 29 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e 64  =2)?i:0);.}.#end
290cf 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c  if /* ifndef SQL
290d0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
290d1 55 55 4d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  UUM */..#ifndef 
290d2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45  SQLITE_OMIT_PAGE
290d3 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20  R_PRAGMAS./*.** 
290d4 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69  Interpret the gi
290d5 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20  ven string as a 
290d6 74 65 6d 70 20 64 62 20 6c 6f 63 61 74 69 6f 6e  temp db location
290d7 2e 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20 66  . Return 1 for f
290d8 69 6c 65 0a 2a 2a 20 62 61 63 6b 65 64 20 74 65  ile.** backed te
290d9 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
290da 73 2c 20 32 20 66 6f 72 20 74 68 65 20 52 65 64  s, 2 for the Red
290db 2d 42 6c 61 63 6b 20 74 72 65 65 20 69 6e 20 6d  -Black tree in m
290dc 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a  emory database.*
290dd 2a 20 61 6e 64 20 30 20 74 6f 20 75 73 65 20 74  * and 0 to use t
290de 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  he compile-time 
290df 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74  default..*/.stat
290e0 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 53 74  ic int getTempSt
290e1 6f 72 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ore(const char *
290e2 7a 29 7b 0a 20 20 69 66 28 20 7a 5b 30 5d 3e 3d  z){.  if( z[0]>=
290e3 27 30 27 20 26 26 20 7a 5b 30 5d 3c 3d 27 32 27  '0' && z[0]<='2'
290e4 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a   ){.    return z
290e5 5b 30 5d 20 2d 20 27 30 27 3b 0a 20 20 7d 65 6c  [0] - '0';.  }el
290e6 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  se if( sqlite3St
290e7 72 49 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22 29  rICmp(z, "file")
290e8 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
290e9 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 1;.  }else if(
290ea 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
290eb 7a 2c 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30 20  z, "memory")==0 
290ec 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b  ){.    return 2;
290ed 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
290ee 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65  turn 0;.  }.}.#e
290ef 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50  ndif /* SQLITE_P
290f0 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a  AGER_PRAGMAS */.
290f1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
290f2 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
290f3 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64  AS./*.** Invalid
290f4 61 74 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65  ate temp storage
290f5 2c 20 65 69 74 68 65 72 20 77 68 65 6e 20 74 68  , either when th
290f6 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65 20 69  e temp storage i
290f7 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 66 72 6f  s changed.** fro
290f8 6d 20 64 65 66 61 75 6c 74 2c 20 6f 72 20 77 68  m default, or wh
290f9 65 6e 20 27 66 69 6c 65 27 20 61 6e 64 20 74 68  en 'file' and th
290fa 65 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72  e temp_store_dir
290fb 65 63 74 6f 72 79 20 68 61 73 20 63 68 61 6e 67  ectory has chang
290fc 65 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ed.*/.static int
290fd 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53   invalidateTempS
290fe 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70 50  torage(Parse *pP
290ff 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
29100 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
29101 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62  b;.  if( db->aDb
29102 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20 20  [1].pBt!=0 ){.  
29103 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43    if( !db->autoC
29104 6f 6d 6d 69 74 20 7c 7c 20 73 71 6c 69 74 65 33  ommit || sqlite3
29105 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61  BtreeIsInReadTra
29106 6e 73 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42  ns(db->aDb[1].pB
29107 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  t) ){.      sqli
29108 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
29109 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 73  se, "temporary s
2910a 74 6f 72 61 67 65 20 63 61 6e 6e 6f 74 20 62 65  torage cannot be
2910b 20 63 68 61 6e 67 65 64 20 22 0a 20 20 20 20 20   changed ".     
2910c 20 20 20 22 66 72 6f 6d 20 77 69 74 68 69 6e 20     "from within 
2910d 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b  a transaction");
2910e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
2910f 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
29110 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  }.    sqlite3Btr
29111 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b  eeClose(db->aDb[
29112 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 64 62 2d  1].pBt);.    db-
29113 3e 61 44 62 5b 31 5d 2e 70 42 74 20 3d 20 30 3b  >aDb[1].pBt = 0;
29114 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65  .    sqlite3Rese
29115 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
29116 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65  db, 0);.  }.  re
29117 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
29118 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
29119 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  TE_PAGER_PRAGMAS
2911a 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
2911b 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
2911c 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66 20  RAGMAS./*.** If 
2911d 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73  the TEMP databas
2911e 65 20 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73 65  e is open, close
2911f 20 69 74 20 61 6e 64 20 6d 61 72 6b 20 74 68 65   it and mark the
29120 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
29121 0a 2a 2a 20 61 73 20 6e 65 65 64 69 6e 67 20 72  .** as needing r
29122 65 6c 6f 61 64 69 6e 67 2e 20 20 54 68 69 73 20  eloading.  This 
29123 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 77 68 65  must be done whe
29124 6e 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c 49  n using the SQLI
29125 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 2a 2a  TE_TEMP_STORE.**
29126 20 6f 72 20 44 45 46 41 55 4c 54 5f 54 45 4d 50   or DEFAULT_TEMP
29127 5f 53 54 4f 52 45 20 70 72 61 67 6d 61 73 2e 0a  _STORE pragmas..
29128 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68  */.static int ch
29129 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65 28  angeTempStorage(
2912a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
2912b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6f 72  onst char *zStor
2912c 61 67 65 54 79 70 65 29 7b 0a 20 20 69 6e 74 20  ageType){.  int 
2912d 74 73 20 3d 20 67 65 74 54 65 6d 70 53 74 6f 72  ts = getTempStor
2912e 65 28 7a 53 74 6f 72 61 67 65 54 79 70 65 29 3b  e(zStorageType);
2912f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
29130 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
29131 66 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72  f( db->temp_stor
29132 65 3d 3d 74 73 20 29 20 72 65 74 75 72 6e 20 53  e==ts ) return S
29133 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
29134 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74  invalidateTempSt
29135 6f 72 61 67 65 28 20 70 50 61 72 73 65 20 29 20  orage( pParse ) 
29136 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  != SQLITE_OK ){.
29137 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
29138 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 64  E_ERROR;.  }.  d
29139 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20  b->temp_store = 
2913a 28 75 38 29 74 73 3b 0a 20 20 72 65 74 75 72 6e  (u8)ts;.  return
2913b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
2913c 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50  ndif /* SQLITE_P
2913d 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a  AGER_PRAGMAS */.
2913e 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2913f 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e 20 61  code to return a
29140 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72 20   single integer 
29141 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  value..*/.static
29142 20 76 6f 69 64 20 72 65 74 75 72 6e 53 69 6e 67   void returnSing
29143 6c 65 49 6e 74 28 50 61 72 73 65 20 2a 70 50 61  leInt(Parse *pPa
29144 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rse, const char 
29145 2a 7a 4c 61 62 65 6c 2c 20 69 36 34 20 76 61 6c  *zLabel, i64 val
29146 75 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  ue){.  Vdbe *v =
29147 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
29148 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 6d  pParse);.  int m
29149 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
2914a 4d 65 6d 3b 0a 20 20 69 36 34 20 2a 70 49 36 34  Mem;.  i64 *pI64
2914b 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2914c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64 62  ocRaw(pParse->db
2914d 2c 20 73 69 7a 65 6f 66 28 76 61 6c 75 65 29 29  , sizeof(value))
2914e 3b 0a 20 20 69 66 28 20 70 49 36 34 20 29 7b 0a  ;.  if( pI64 ){.
2914f 20 20 20 20 6d 65 6d 63 70 79 28 70 49 36 34 2c      memcpy(pI64,
29150 20 26 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66 28   &value, sizeof(
29151 76 61 6c 75 65 29 29 3b 0a 20 20 7d 0a 20 20 73  value));.  }.  s
29152 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29153 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c  (v, OP_Int64, 0,
29154 20 6d 65 6d 2c 20 30 2c 20 28 63 68 61 72 2a 29   mem, 0, (char*)
29155 70 49 36 34 2c 20 50 34 5f 49 4e 54 36 34 29 3b  pI64, P4_INT64);
29156 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  .  sqlite3VdbeSe
29157 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
29158 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29159 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
2915a 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61 62  LNAME_NAME, zLab
2915b 65 6c 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  el, SQLITE_STATI
2915c 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  C);.  sqlite3Vdb
2915d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
2915e 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31 29  sultRow, mem, 1)
2915f 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
29160 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52  ITE_OMIT_FLAG_PR
29161 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65 63  AGMAS./*.** Chec
29162 6b 20 74 6f 20 73 65 65 20 69 66 20 7a 52 69 67  k to see if zRig
29163 68 74 20 61 6e 64 20 7a 4c 65 66 74 20 72 65 66  ht and zLeft ref
29164 65 72 20 74 6f 20 61 20 70 72 61 67 6d 61 20 74  er to a pragma t
29165 68 61 74 20 71 75 65 72 69 65 73 0a 2a 2a 20 6f  hat queries.** o
29166 72 20 63 68 61 6e 67 65 73 20 6f 6e 65 20 6f 66  r changes one of
29167 20 74 68 65 20 66 6c 61 67 73 20 69 6e 20 64 62   the flags in db
29168 2d 3e 66 6c 61 67 73 2e 20 20 52 65 74 75 72 6e  ->flags.  Return
29169 20 31 20 69 66 20 73 6f 20 61 6e 64 20 30 20 69   1 if so and 0 i
2916a 66 20 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f 2c 20  f not..** Also, 
2916b 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72  implement the pr
2916c 61 67 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  agma..*/.static 
2916d 69 6e 74 20 66 6c 61 67 50 72 61 67 6d 61 28 50  int flagPragma(P
2916e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
2916f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c  nst char *zLeft,
29170 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69   const char *zRi
29171 67 68 74 29 7b 0a 20 20 73 74 61 74 69 63 20 63  ght){.  static c
29172 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50 72 61  onst struct sPra
29173 67 6d 61 54 79 70 65 20 7b 0a 20 20 20 20 63 6f  gmaType {.    co
29174 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
29175 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
29176 20 70 72 61 67 6d 61 20 2a 2f 0a 20 20 20 20 69   pragma */.    i
29177 6e 74 20 6d 61 73 6b 3b 20 20 20 20 20 20 20 20  nt mask;        
29178 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 74     /* Mask for t
29179 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 76 61 6c  he db->flags val
2917a 75 65 20 2a 2f 0a 20 20 7d 20 61 50 72 61 67 6d  ue */.  } aPragm
2917b 61 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 66  a[] = {.    { "f
2917c 75 6c 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73  ull_column_names
2917d 22 2c 20 20 20 20 20 20 20 20 53 51 4c 49 54 45  ",        SQLITE
2917e 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20 7d  _FullColNames  }
2917f 2c 0a 20 20 20 20 7b 20 22 73 68 6f 72 74 5f 63  ,.    { "short_c
29180 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20  olumn_names",   
29181 20 20 20 20 53 51 4c 49 54 45 5f 53 68 6f 72 74      SQLITE_Short
29182 43 6f 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20 20 20  ColNames },.    
29183 7b 20 22 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73  { "count_changes
29184 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53 51  ",            SQ
29185 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20  LITE_CountRows  
29186 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 65 6d 70     },.    { "emp
29187 74 79 5f 72 65 73 75 6c 74 5f 63 61 6c 6c 62 61  ty_result_callba
29188 63 6b 73 22 2c 20 20 20 53 51 4c 49 54 45 5f 4e  cks",   SQLITE_N
29189 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 7d 2c 0a  ullCallback  },.
2918a 20 20 20 20 7b 20 22 6c 65 67 61 63 79 5f 66 69      { "legacy_fi
2918b 6c 65 5f 66 6f 72 6d 61 74 22 2c 20 20 20 20 20  le_format",     
2918c 20 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46    SQLITE_LegacyF
2918d 69 6c 65 46 6d 74 20 7d 2c 0a 20 20 20 20 7b 20  ileFmt },.    { 
2918e 22 66 75 6c 6c 66 73 79 6e 63 22 2c 20 20 20 20  "fullfsync",    
2918f 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
29190 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20 20  TE_FullFSync    
29191 20 7d 2c 0a 20 20 20 20 7b 20 22 72 65 76 65 72   },.    { "rever
29192 73 65 5f 75 6e 6f 72 64 65 72 65 64 5f 73 65 6c  se_unordered_sel
29193 65 63 74 73 22 2c 20 53 51 4c 49 54 45 5f 52 65  ects", SQLITE_Re
29194 76 65 72 73 65 4f 72 64 65 72 20 20 7d 2c 0a 23  verseOrder  },.#
29195 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
29196 55 47 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74 72  UG.    { "sql_tr
29197 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20 20  ace",           
29198 20 20 20 20 20 53 51 4c 49 54 45 5f 53 71 6c 54       SQLITE_SqlT
29199 72 61 63 65 20 20 20 20 20 20 7d 2c 0a 20 20 20  race      },.   
2919a 20 7b 20 22 76 64 62 65 5f 6c 69 73 74 69 6e 67   { "vdbe_listing
2919b 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 53  ",             S
2919c 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e  QLITE_VdbeListin
2919d 67 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76 64  g   },.    { "vd
2919e 62 65 5f 74 72 61 63 65 22 2c 20 20 20 20 20 20  be_trace",      
2919f 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
291a0 56 64 62 65 54 72 61 63 65 20 20 20 20 20 7d 2c  VdbeTrace     },
291a1 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
291a2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
291a3 4b 0a 20 20 20 20 7b 20 22 69 67 6e 6f 72 65 5f  K.    { "ignore_
291a4 63 68 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e 74  check_constraint
291a5 73 22 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72  s", SQLITE_Ignor
291a6 65 43 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e 64  eChecks  },.#end
291a7 69 66 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f  if.    /* The fo
291a8 6c 6c 6f 77 69 6e 67 20 69 73 20 56 45 52 59 20  llowing is VERY 
291a9 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f 0a  experimental */.
291aa 20 20 20 20 7b 20 22 77 72 69 74 61 62 6c 65 5f      { "writable_
291ab 73 63 68 65 6d 61 22 2c 20 20 20 20 20 20 20 20  schema",        
291ac 20 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63    SQLITE_WriteSc
291ad 68 65 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63 6f  hema|SQLITE_Reco
291ae 76 65 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20 20  veryMode },.    
291af 7b 20 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63 6b  { "omit_readlock
291b0 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53 51  ",            SQ
291b1 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20  LITE_NoReadlock 
291b2 20 20 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54 4f     },..    /* TO
291b3 44 4f 3a 20 4d 61 79 62 65 20 69 74 20 73 68 6f  DO: Maybe it sho
291b4 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62  uldn't be possib
291b5 6c 65 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  le to change the
291b6 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64   ReadUncommitted
291b7 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 66 20  .    ** flag if 
291b8 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 63  there are any ac
291b9 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 2e  tive statements.
291ba 20 2a 2f 0a 20 20 20 20 7b 20 22 72 65 61 64 5f   */.    { "read_
291bb 75 6e 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20 20  uncommitted",   
291bc 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 65 61        SQLITE_Rea
291bd 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c 0a  dUncommitted },.
291be 20 20 20 20 7b 20 22 72 65 63 75 72 73 69 76 65      { "recursive
291bf 5f 74 72 69 67 67 65 72 73 22 2c 20 20 20 20 20  _triggers",     
291c0 20 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67    SQLITE_RecTrig
291c1 67 65 72 73 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20  gers },..    /* 
291c2 54 68 69 73 20 66 6c 61 67 20 6d 61 79 20 6f 6e  This flag may on
291c3 6c 79 20 62 65 20 73 65 74 20 69 66 20 62 6f 74  ly be set if bot
291c4 68 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 61 6e  h foreign-key an
291c5 64 20 74 72 69 67 67 65 72 20 73 75 70 70 6f 72  d trigger suppor
291c6 74 0a 20 20 20 20 2a 2a 20 61 72 65 20 70 72 65  t.    ** are pre
291c7 73 65 6e 74 20 69 6e 20 74 68 65 20 62 75 69 6c  sent in the buil
291c8 64 2e 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69  d.  */.#if !defi
291c9 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
291ca 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20  FOREIGN_KEY) && 
291cb 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
291cc 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20  OMIT_TRIGGER).  
291cd 20 20 7b 20 22 66 6f 72 65 69 67 6e 5f 6b 65 79    { "foreign_key
291ce 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  s",             
291cf 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
291d0 79 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7d  ys },.#endif.  }
291d1 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e  ;.  int i;.  con
291d2 73 74 20 73 74 72 75 63 74 20 73 50 72 61 67 6d  st struct sPragm
291d3 61 54 79 70 65 20 2a 70 3b 0a 20 20 66 6f 72 28  aType *p;.  for(
291d4 69 3d 30 2c 20 70 3d 61 50 72 61 67 6d 61 3b 20  i=0, p=aPragma; 
291d5 69 3c 41 72 72 61 79 53 69 7a 65 28 61 50 72 61  i<ArraySize(aPra
291d6 67 6d 61 29 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b  gma); i++, p++){
291d7 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
291d8 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 70  StrICmp(zLeft, p
291d9 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  ->zName)==0 ){. 
291da 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
291db 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
291dc 20 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20       Vdbe *v;.  
291dd 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
291de 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
291df 20 20 20 20 20 20 61 73 73 65 72 74 28 20 76 21        assert( v!
291e0 3d 30 20 29 3b 20 20 2f 2a 20 41 6c 72 65 61 64  =0 );  /* Alread
291e1 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73  y allocated by s
291e2 71 6c 69 74 65 33 50 72 61 67 6d 61 28 29 20 2a  qlite3Pragma() *
291e3 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41  /.      if( ALWA
291e4 59 53 28 76 29 20 29 7b 0a 20 20 20 20 20 20 20  YS(v) ){.       
291e5 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 29   if( zRight==0 )
291e6 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
291e7 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
291e8 73 65 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 28 64  se, p->zName, (d
291e9 62 2d 3e 66 6c 61 67 73 20 26 20 70 2d 3e 6d 61  b->flags & p->ma
291ea 73 6b 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  sk)!=0 );.      
291eb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
291ec 20 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 70 2d     int mask = p-
291ed 3e 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  >mask;          
291ee 2f 2a 20 4d 61 73 6b 20 6f 66 20 62 69 74 73 20  /* Mask of bits 
291ef 74 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 2e  to set or clear.
291f0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66   */.          if
291f1 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  ( db->autoCommit
291f2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
291f3 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65     /* Foreign ke
291f4 79 20 73 75 70 70 6f 72 74 20 6d 61 79 20 6e 6f  y support may no
291f5 74 20 62 65 20 65 6e 61 62 6c 65 64 20 6f 72 20  t be enabled or 
291f6 64 69 73 61 62 6c 65 64 20 77 68 69 6c 65 20 6e  disabled while n
291f7 6f 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  ot.            *
291f8 2a 20 69 6e 20 61 75 74 6f 2d 63 6f 6d 6d 69 74  * in auto-commit
291f9 20 6d 6f 64 65 2e 20 20 2a 2f 0a 20 20 20 20 20   mode.  */.     
291fa 20 20 20 20 20 20 20 6d 61 73 6b 20 26 3d 20 7e         mask &= ~
291fb 28 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  (SQLITE_ForeignK
291fc 65 79 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  eys);.          
291fd 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  }..          if(
291fe 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67   getBoolean(zRig
291ff 68 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ht) ){.         
29200 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20     db->flags |= 
29201 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  mask;.          
29202 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
29203 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20     db->flags &= 
29204 7e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20  ~mask;.         
29205 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   }..          /*
29206 20 4d 61 6e 79 20 6f 66 20 74 68 65 20 66 6c 61   Many of the fla
29207 67 2d 70 72 61 67 6d 61 73 20 6d 6f 64 69 66 79  g-pragmas modify
29208 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
29209 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 0a  ted by the SQL .
2920a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d            ** com
2920b 70 69 6c 65 72 20 28 65 67 2e 20 63 6f 75 6e 74  piler (eg. count
2920c 5f 63 68 61 6e 67 65 73 29 2e 20 53 6f 20 61 64  _changes). So ad
2920d 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 6f 20 65  d an opcode to e
2920e 78 70 69 72 65 20 61 6c 6c 0a 20 20 20 20 20 20  xpire all.      
2920f 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20      ** compiled 
29210 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61  SQL statements a
29211 66 74 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61  fter modifying a
29212 20 70 72 61 67 6d 61 20 76 61 6c 75 65 2e 0a 20   pragma value.. 
29213 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
29214 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29215 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78  eAddOp2(v, OP_Ex
29216 70 69 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20  pire, 0, 0);.   
29217 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
29218 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
29219 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2921a 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 0;.}.#endif /
2921b 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  * SQLITE_OMIT_FL
2921c 41 47 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f  AG_PRAGMAS */../
2921d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 68 75  *.** Return a hu
2921e 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 6e 61 6d  man-readable nam
2921f 65 20 66 6f 72 20 61 20 63 6f 6e 73 74 72 61 69  e for a constrai
29220 6e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 63  nt resolution ac
29221 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tion..*/.#ifndef
29222 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
29223 45 49 47 4e 5f 4b 45 59 0a 73 74 61 74 69 63 20  EIGN_KEY.static 
29224 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 63 74 69  const char *acti
29225 6f 6e 4e 61 6d 65 28 75 38 20 61 63 74 69 6f 6e  onName(u8 action
29226 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
29227 2a 7a 4e 61 6d 65 3b 0a 20 20 73 77 69 74 63 68  *zName;.  switch
29228 28 20 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  ( action ){.    
29229 63 61 73 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 3a  case OE_SetNull:
2922a 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20 4e    zName = "SET N
2922b 55 4c 4c 22 3b 20 20 20 20 20 20 20 20 62 72 65  ULL";        bre
2922c 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f  ak;.    case OE_
2922d 53 65 74 44 66 6c 74 3a 20 20 7a 4e 61 6d 65 20  SetDflt:  zName 
2922e 3d 20 22 53 45 54 20 44 45 46 41 55 4c 54 22 3b  = "SET DEFAULT";
2922f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29230 63 61 73 65 20 4f 45 5f 43 61 73 63 61 64 65 3a  case OE_Cascade:
29231 20 20 7a 4e 61 6d 65 20 3d 20 22 43 41 53 43 41    zName = "CASCA
29232 44 45 22 3b 20 20 20 20 20 20 20 20 20 62 72 65  DE";         bre
29233 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f  ak;.    case OE_
29234 52 65 73 74 72 69 63 74 3a 20 7a 4e 61 6d 65 20  Restrict: zName 
29235 3d 20 22 52 45 53 54 52 49 43 54 22 3b 20 20 20  = "RESTRICT";   
29236 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29237 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20  default:        
29238 20 20 7a 4e 61 6d 65 20 3d 20 22 4e 4f 20 41 43    zName = "NO AC
29239 54 49 4f 4e 22 3b 20 20 0a 20 20 20 20 20 20 20  TION";  .       
2923a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
2923b 73 73 65 72 74 28 20 61 63 74 69 6f 6e 3d 3d 4f  ssert( action==O
2923c 45 5f 4e 6f 6e 65 20 29 3b 20 62 72 65 61 6b 3b  E_None ); break;
2923d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e  .  }.  return zN
2923e 61 6d 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ame;.}.#endif../
2923f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 70  *.** Process a p
29240 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e  ragma statement.
29241 20 20 0a 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61 73    .**.** Pragmas
29242 20 61 72 65 20 6f 66 20 74 68 69 73 20 66 6f 72   are of this for
29243 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 52  m:.**.**      PR
29244 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
29245 69 64 20 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a  id [= value].**.
29246 2a 2a 20 54 68 65 20 69 64 65 6e 74 69 66 69 65  ** The identifie
29247 72 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20  r might also be 
29248 61 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 76  a string.  The v
29249 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67  alue is a string
2924a 2c 20 61 6e 64 0a 2a 2a 20 69 64 65 6e 74 69 66  , and.** identif
2924b 69 65 72 2c 20 6f 72 20 61 20 6e 75 6d 62 65 72  ier, or a number
2924c 2e 20 20 49 66 20 6d 69 6e 75 73 46 6c 61 67 20  .  If minusFlag 
2924d 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
2924e 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20  e value is.** a 
2924f 6e 75 6d 62 65 72 20 74 68 61 74 20 77 61 73 20  number that was 
29250 70 72 65 63 65 64 65 64 20 62 79 20 61 20 6d 69  preceded by a mi
29251 6e 75 73 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20  nus sign..**.** 
29252 49 66 20 74 68 65 20 6c 65 66 74 20 73 69 64 65  If the left side
29253 20 69 73 20 22 64 61 74 61 62 61 73 65 2e 69 64   is "database.id
29254 22 20 74 68 65 6e 20 70 49 64 31 20 69 73 20 74  " then pId1 is t
29255 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
29256 0a 2a 2a 20 61 6e 64 20 70 49 64 32 20 69 73 20  .** and pId2 is 
29257 74 68 65 20 69 64 2e 20 20 49 66 20 74 68 65 20  the id.  If the 
29258 6c 65 66 74 20 73 69 64 65 20 69 73 20 6a 75 73  left side is jus
29259 74 20 22 69 64 22 20 74 68 65 6e 20 70 49 64 31  t "id" then pId1
2925a 20 69 73 20 74 68 65 0a 2a 2a 20 69 64 20 61 6e   is the.** id an
2925b 64 20 70 49 64 32 20 69 73 20 61 6e 79 20 65 6d  d pId2 is any em
2925c 70 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53  pty string..*/.S
2925d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2925e 69 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61  id sqlite3Pragma
2925f 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
29260 65 2c 20 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64  e, .  Token *pId
29261 31 2c 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72  1,        /* Fir
29262 73 74 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61  st part of [data
29263 62 61 73 65 2e 5d 69 64 20 66 69 65 6c 64 20 2a  base.]id field *
29264 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 32 2c  /.  Token *pId2,
29265 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
29266 64 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62  d part of [datab
29267 61 73 65 2e 5d 69 64 20 66 69 65 6c 64 2c 20 6f  ase.]id field, o
29268 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65  r NULL */.  Toke
29269 6e 20 2a 70 56 61 6c 75 65 2c 20 20 20 20 20 20  n *pValue,      
2926a 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 3c 76 61  /* Token for <va
2926b 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  lue>, or NULL */
2926c 0a 20 20 69 6e 74 20 6d 69 6e 75 73 46 6c 61 67  .  int minusFlag
2926d 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
2926e 66 20 61 20 27 2d 27 20 73 69 67 6e 20 70 72 65  f a '-' sign pre
2926f 63 65 64 65 64 20 3c 76 61 6c 75 65 3e 20 2a 2f  ceded <value> */
29270 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4c 65 66  .){.  char *zLef
29271 74 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  t = 0;       /* 
29272 4e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  Nul-terminated U
29273 54 46 2d 38 20 73 74 72 69 6e 67 20 3c 69 64 3e  TF-8 string <id>
29274 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 69 67   */.  char *zRig
29275 68 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  ht = 0;      /* 
29276 4e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  Nul-terminated U
29277 54 46 2d 38 20 73 74 72 69 6e 67 20 3c 76 61 6c  TF-8 string <val
29278 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  ue>, or NULL */.
29279 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2927a 62 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20  b = 0;   /* The 
2927b 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  database name */
2927c 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 3b 20 20  .  Token *pId;  
2927d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
2927e 6e 74 65 72 20 74 6f 20 3c 69 64 3e 20 74 6f 6b  nter to <id> tok
2927f 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  en */.  int iDb;
29280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
29281 2a 20 44 61 74 61 62 61 73 65 20 69 6e 64 65 78  * Database index
29282 20 66 6f 72 20 3c 64 61 74 61 62 61 73 65 3e 20   for <database> 
29283 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
29284 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
29285 20 44 62 20 2a 70 44 62 3b 0a 20 20 56 64 62 65   Db *pDb;.  Vdbe
29286 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
29287 64 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dbe = sqlite3Vdb
29288 65 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20 69  eCreate(db);.  i
29289 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
2928a 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ;.  pParse->nMem
2928b 20 3d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65   = 2;..  /* Inte
2928c 72 70 72 65 74 20 74 68 65 20 5b 64 61 74 61 62  rpret the [datab
2928d 61 73 65 2e 5d 20 70 61 72 74 20 6f 66 20 74 68  ase.] part of th
2928e 65 20 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65  e pragma stateme
2928f 6e 74 2e 20 69 44 62 20 69 73 20 74 68 65 0a 20  nt. iDb is the. 
29290 20 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65   ** index of the
29291 20 64 61 74 61 62 61 73 65 20 74 68 69 73 20 70   database this p
29292 72 61 67 6d 61 20 69 73 20 62 65 69 6e 67 20 61  ragma is being a
29293 70 70 6c 69 65 64 20 74 6f 20 69 6e 20 64 62 2e  pplied to in db.
29294 61 44 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62 20  aDb[]. */.  iDb 
29295 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
29296 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 49 64  Name(pParse, pId
29297 31 2c 20 70 49 64 32 2c 20 26 70 49 64 29 3b 0a  1, pId2, &pId);.
29298 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65    if( iDb<0 ) re
29299 74 75 72 6e 3b 0a 20 20 70 44 62 20 3d 20 26 64  turn;.  pDb = &d
2929a 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20  b->aDb[iDb];..  
2929b 2f 2a 20 49 66 20 74 68 65 20 74 65 6d 70 20 64  /* If the temp d
2929c 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e  atabase has been
2929d 20 65 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d 65   explicitly name
2929e 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  d as part of the
2929f 20 0a 20 20 2a 2a 20 70 72 61 67 6d 61 2c 20 6d   .  ** pragma, m
292a0 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6f  ake sure it is o
292a1 70 65 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  pen. .  */.  if(
292a2 20 69 44 62 3d 3d 31 20 26 26 20 73 71 6c 69 74   iDb==1 && sqlit
292a3 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61  e3OpenTempDataba
292a4 73 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  se(pParse) ){.  
292a5 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
292a6 20 7a 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33   zLeft = sqlite3
292a7 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
292a8 2c 20 70 49 64 29 3b 0a 20 20 69 66 28 20 21 7a  , pId);.  if( !z
292a9 4c 65 66 74 20 29 20 72 65 74 75 72 6e 3b 0a 20  Left ) return;. 
292aa 20 69 66 28 20 6d 69 6e 75 73 46 6c 61 67 20 29   if( minusFlag )
292ab 7b 0a 20 20 20 20 7a 52 69 67 68 74 20 3d 20 73  {.    zRight = s
292ac 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
292ad 2c 20 22 2d 25 54 22 2c 20 70 56 61 6c 75 65 29  , "-%T", pValue)
292ae 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
292af 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4e  Right = sqlite3N
292b0 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
292b1 20 70 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20   pValue);.  }.. 
292b2 20 61 73 73 65 72 74 28 20 70 49 64 32 20 29 3b   assert( pId2 );
292b3 0a 20 20 7a 44 62 20 3d 20 70 49 64 32 2d 3e 6e  .  zDb = pId2->n
292b4 3e 30 20 3f 20 70 44 62 2d 3e 7a 4e 61 6d 65 20  >0 ? pDb->zName 
292b5 3a 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  : 0;.  if( sqlit
292b6 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
292b7 73 65 2c 20 53 51 4c 49 54 45 5f 50 52 41 47 4d  se, SQLITE_PRAGM
292b8 41 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74  A, zLeft, zRight
292b9 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f  , zDb) ){.    go
292ba 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
292bb 20 7d 0a 20 0a 23 69 66 6e 64 65 66 20 53 51 4c   }. .#ifndef SQL
292bc 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
292bd 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a  RAGMAS.  /*.  **
292be 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
292bf 73 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68  se.]default_cach
292c0 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41  e_size.  **  PRA
292c1 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 64  GMA [database.]d
292c2 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a  efault_cache_siz
292c3 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  e=N.  **.  ** Th
292c4 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70  e first form rep
292c5 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74  orts the current
292c6 20 70 65 72 73 69 73 74 65 6e 74 20 73 65 74 74   persistent sett
292c7 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ing for the.  **
292c8 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
292c9 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  .  The value ret
292ca 75 72 6e 65 64 20 69 73 20 74 68 65 20 6d 61 78  urned is the max
292cb 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 20  imum number of. 
292cc 20 2a 2a 20 70 61 67 65 73 20 69 6e 20 74 68 65   ** pages in the
292cd 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 54 68   page cache.  Th
292ce 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65  e second form se
292cf 74 73 20 62 6f 74 68 20 74 68 65 20 63 75 72 72  ts both the curr
292d0 65 6e 74 0a 20 20 2a 2a 20 70 61 67 65 20 63 61  ent.  ** page ca
292d1 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 20 61  che size value a
292d2 6e 64 20 74 68 65 20 70 65 72 73 69 73 74 65 6e  nd the persisten
292d3 74 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a  t page cache siz
292d4 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 73 74 6f  e value.  ** sto
292d5 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  red in the datab
292d6 61 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20  ase file..  **. 
292d7 20 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20   ** The default 
292d8 63 61 63 68 65 20 73 69 7a 65 20 69 73 20 73 74  cache size is st
292d9 6f 72 65 64 20 69 6e 20 6d 65 74 61 2d 76 61 6c  ored in meta-val
292da 75 65 20 32 20 6f 66 20 70 61 67 65 20 31 20 6f  ue 2 of page 1 o
292db 66 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  f the.  ** datab
292dc 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 63  ase file.  The c
292dd 61 63 68 65 20 73 69 7a 65 20 69 73 20 61 63 74  ache size is act
292de 75 61 6c 6c 79 20 74 68 65 20 61 62 73 6f 6c 75  ually the absolu
292df 74 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a  te value of.  **
292e0 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63   this memory loc
292e1 61 74 69 6f 6e 2e 20 20 54 68 65 20 73 69 67 6e  ation.  The sign
292e2 20 6f 66 20 6d 65 74 61 2d 76 61 6c 75 65 20 32   of meta-value 2
292e3 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 65 0a   determines the.
292e4 20 20 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73    ** synchronous
292e5 20 73 65 74 74 69 6e 67 2e 20 20 41 20 6e 65 67   setting.  A neg
292e6 61 74 69 76 65 20 76 61 6c 75 65 20 6d 65 61 6e  ative value mean
292e7 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  s synchronous is
292e8 20 6f 66 66 0a 20 20 2a 2a 20 61 6e 64 20 61 20   off.  ** and a 
292e9 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 6d  positive value m
292ea 65 61 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73  eans synchronous
292eb 20 69 73 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69   is on..  */.  i
292ec 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
292ed 70 28 7a 4c 65 66 74 2c 22 64 65 66 61 75 6c 74  p(zLeft,"default
292ee 5f 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30  _cache_size")==0
292ef 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
292f0 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
292f1 67 65 74 43 61 63 68 65 53 69 7a 65 5b 5d 20 3d  getCacheSize[] =
292f2 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 54 72   {.      { OP_Tr
292f3 61 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c  ansaction, 0, 0,
292f4 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 20          0},     
292f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292f6 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20      /* 0 */.    
292f7 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69    { OP_ReadCooki
292f8 65 2c 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20  e,  0, 1,       
292f9 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43   BTREE_DEFAULT_C
292fa 41 43 48 45 5f 53 49 5a 45 7d 2c 20 20 2f 2a 20  ACHE_SIZE},  /* 
292fb 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  1 */.      { OP_
292fc 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20  IfPos,       1, 
292fd 37 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  7,        0},.  
292fe 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72      { OP_Integer
292ff 2c 20 20 20 20 20 30 2c 20 32 2c 20 20 20 20 20  ,     0, 2,     
29300 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     0},.      { O
29301 50 5f 53 75 62 74 72 61 63 74 2c 20 20 20 20 31  P_Subtract,    1
29302 2c 20 32 2c 20 20 20 20 20 20 20 20 31 7d 2c 0a  , 2,        1},.
29303 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73        { OP_IfPos
29304 2c 20 20 20 20 20 20 20 31 2c 20 37 2c 20 20 20  ,       1, 7,   
29305 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b       0},.      {
29306 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20   OP_Integer,    
29307 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d   0, 1,        0}
29308 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
29309 20 20 20 20 20 20 20 20 20 20 2f 2a 20 36 20 2a            /* 6 *
2930a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73  /.      { OP_Res
2930b 75 6c 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20  ultRow,   1, 1, 
2930c 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d         0},.    }
2930d 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a  ;.    int addr;.
2930e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
2930f 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
29310 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
29311 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  out;.    sqlite3
29312 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
29313 20 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 21   iDb);.    if( !
29314 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
29315 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
29316 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
29317 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29318 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
29319 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 61  OLNAME_NAME, "ca
2931a 63 68 65 5f 73 69 7a 65 22 2c 20 53 51 4c 49 54  che_size", SQLIT
2931b 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
2931c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
2931d 20 32 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d   2;.      addr =
2931e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2931f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
29320 7a 65 28 67 65 74 43 61 63 68 65 53 69 7a 65 29  ze(getCacheSize)
29321 2c 20 67 65 74 43 61 63 68 65 53 69 7a 65 29 3b  , getCacheSize);
29322 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29323 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
29324 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  dr, iDb);.      
29325 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29326 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 69  eP1(v, addr+1, i
29327 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
29328 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
29329 2c 20 61 64 64 72 2b 36 2c 20 53 51 4c 49 54 45  , addr+6, SQLITE
2932a 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
2932b 49 5a 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  IZE);.    }else{
2932c 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20  .      int size 
2932d 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a  = atoi(zRight);.
2932e 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30        if( size<0
2932f 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b   ) size = -size;
29330 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65  .      sqlite3Be
29331 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
29332 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
29333 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29334 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29335 5f 49 6e 74 65 67 65 72 2c 20 73 69 7a 65 2c 20  _Integer, size, 
29336 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
29337 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
29338 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44  P_ReadCookie, iD
29339 62 2c 20 32 2c 20 42 54 52 45 45 5f 44 45 46 41  b, 2, BTREE_DEFA
2933a 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b  ULT_CACHE_SIZE);
2933b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
2933c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2933d 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 32 2c 20  v, OP_IfPos, 2, 
2933e 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
2933f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29340 50 5f 49 6e 74 65 67 65 72 2c 20 2d 73 69 7a 65  P_Integer, -size
29341 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
29342 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
29343 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20  v, addr);.      
29344 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29345 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69  3(v, OP_SetCooki
29346 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 44 45  e, iDb, BTREE_DE
29347 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
29348 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 44 62 2d  , 1);.      pDb-
29349 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f  >pSchema->cache_
2934a 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20  size = size;.   
2934b 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53     sqlite3BtreeS
2934c 65 74 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d  etCacheSize(pDb-
2934d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65  >pBt, pDb->pSche
2934e 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b  ma->cache_size);
2934f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
29350 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
29351 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67  A [database.]pag
29352 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41  e_size.  **  PRA
29353 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70  GMA [database.]p
29354 61 67 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a  age_size=N.  **.
29355 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66    ** The first f
29356 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20  orm reports the 
29357 63 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20  current setting 
29358 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 64 61 74  for the.  ** dat
29359 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20  abase page size 
2935a 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 20 73  in bytes.  The s
2935b 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20  econd form sets 
2935c 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
2935d 65 20 70 61 67 65 20 73 69 7a 65 20 76 61 6c 75  e page size valu
2935e 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 63 61  e.  The value ca
2935f 6e 20 6f 6e 6c 79 20 62 65 20 73 65 74 20 69 66  n only be set if
29360 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61  .  ** the databa
29361 73 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  se has not yet b
29362 65 65 6e 20 63 72 65 61 74 65 64 2e 0a 20 20 2a  een created..  *
29363 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
29364 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61  trICmp(zLeft,"pa
29365 67 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a  ge_size")==0 ){.
29366 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
29367 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 61   pDb->pBt;.    a
29368 73 73 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b  ssert( pBt!=0 );
29369 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74  .    if( !zRight
2936a 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69   ){.      int si
2936b 7a 65 20 3d 20 41 4c 57 41 59 53 28 70 42 74 29  ze = ALWAYS(pBt)
2936c 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   ? sqlite3BtreeG
2936d 65 74 50 61 67 65 53 69 7a 65 28 70 42 74 29 20  etPageSize(pBt) 
2936e 3a 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  : 0;.      retur
2936f 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73  nSingleInt(pPars
29370 65 2c 20 22 70 61 67 65 5f 73 69 7a 65 22 2c 20  e, "page_size", 
29371 73 69 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  size);.    }else
29372 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f  {.      /* Mallo
29373 63 20 6d 61 79 20 66 61 69 6c 20 77 68 65 6e 20  c may fail when 
29374 73 65 74 74 69 6e 67 20 74 68 65 20 70 61 67 65  setting the page
29375 2d 73 69 7a 65 2c 20 61 73 20 74 68 65 72 65 20  -size, as there 
29376 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0a 20  is an internal. 
29377 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 74       ** buffer t
29378 68 61 74 20 74 68 65 20 70 61 67 65 72 20 6d 6f  hat the pager mo
29379 64 75 6c 65 20 72 65 73 69 7a 65 73 20 75 73 69  dule resizes usi
2937a 6e 67 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  ng sqlite3_reall
2937b 6f 63 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  oc()..      */. 
2937c 20 20 20 20 20 64 62 2d 3e 6e 65 78 74 50 61 67       db->nextPag
2937d 65 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69  esize = atoi(zRi
2937e 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ght);.      if( 
2937f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71  SQLITE_NOMEM==sq
29380 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
29381 65 53 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e 6e  eSize(pBt, db->n
29382 65 78 74 50 61 67 65 73 69 7a 65 2c 20 2d 31 2c  extPagesize, -1,
29383 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 64   0) ){.        d
29384 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
29385 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
29386 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a   }.  }else..  /*
29387 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
29388 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67  atabase.]max_pag
29389 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50 52  e_count.  **  PR
2938a 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
2938b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d 4e  max_page_count=N
2938c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  **.  ** The f
2938d 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74  irst form report
2938e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  s the current se
2938f 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20  tting for the.  
29390 2a 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  ** maximum numbe
29391 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
29392 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
29393 20 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63 6f    The .  ** seco
29394 6e 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74 73  nd form attempts
29395 20 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20   to change this 
29396 73 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a 20  setting.  Both. 
29397 20 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72 6e   ** forms return
29398 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
29399 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ting..  */.  if(
2939a 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2939b 7a 4c 65 66 74 2c 22 6d 61 78 5f 70 61 67 65 5f  zLeft,"max_page_
2939c 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20  count")==0 ){.  
2939d 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70    Btree *pBt = p
2939e 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e 74  Db->pBt;.    int
2939f 20 6e 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20 20   newMax = 0;.   
293a0 20 61 73 73 65 72 74 28 20 70 42 74 21 3d 30 20   assert( pBt!=0 
293a1 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68  );.    if( zRigh
293a2 74 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61  t ){.      newMa
293a3 78 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29  x = atoi(zRight)
293a4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
293a5 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a 20  ALWAYS(pBt) ){. 
293a6 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 73 71       newMax = sq
293a7 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67  lite3BtreeMaxPag
293a8 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77 4d  eCount(pBt, newM
293a9 61 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ax);.    }.    r
293aa 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
293ab 50 61 72 73 65 2c 20 22 6d 61 78 5f 70 61 67 65  Parse, "max_page
293ac 5f 63 6f 75 6e 74 22 2c 20 6e 65 77 4d 61 78 29  _count", newMax)
293ad 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a  ;.  }else..  /*.
293ae 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
293af 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 63 6f 75  tabase.]page_cou
293b0 6e 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74  nt.  **.  ** Ret
293b1 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
293b2 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73  f pages in the s
293b3 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73  pecified databas
293b4 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  e..  */.  if( sq
293b5 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
293b6 66 74 2c 22 70 61 67 65 5f 63 6f 75 6e 74 22 29  ft,"page_count")
293b7 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ==0 ){.    int i
293b8 52 65 67 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Reg;.    if( sql
293b9 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
293ba 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
293bb 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71  agma_out;.    sq
293bc 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
293bd 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44  chema(pParse, iD
293be 62 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 2b  b);.    iReg = +
293bf 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
293c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
293c1 64 4f 70 32 28 76 2c 20 4f 50 5f 50 61 67 65 63  dOp2(v, OP_Pagec
293c2 6f 75 6e 74 2c 20 69 44 62 2c 20 69 52 65 67 29  ount, iDb, iReg)
293c3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
293c4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
293c5 73 75 6c 74 52 6f 77 2c 20 69 52 65 67 2c 20 31  sultRow, iReg, 1
293c6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
293c7 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
293c8 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
293c9 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
293ca 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
293cb 2c 20 22 70 61 67 65 5f 63 6f 75 6e 74 22 2c 20  , "page_count", 
293cc 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
293cd 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
293ce 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
293cf 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f  base.]locking_mo
293d0 64 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  de.  **  PRAGMA 
293d1 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69  [database.]locki
293d2 6e 67 5f 6d 6f 64 65 20 3d 20 28 6e 6f 72 6d 61  ng_mode = (norma
293d3 6c 7c 65 78 63 6c 75 73 69 76 65 29 0a 20 20 2a  l|exclusive).  *
293d4 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
293d5 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6c 6f  trICmp(zLeft,"lo
293d6 63 6b 69 6e 67 5f 6d 6f 64 65 22 29 3d 3d 30 20  cking_mode")==0 
293d7 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
293d8 72 20 2a 7a 52 65 74 20 3d 20 22 6e 6f 72 6d 61  r *zRet = "norma
293d9 6c 22 3b 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64  l";.    int eMod
293da 65 20 3d 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f  e = getLockingMo
293db 64 65 28 7a 52 69 67 68 74 29 3b 0a 0a 20 20 20  de(zRight);..   
293dc 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20   if( pId2->n==0 
293dd 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  && eMode==PAGER_
293de 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52  LOCKINGMODE_QUER
293df 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69  Y ){.      /* Si
293e0 6d 70 6c 65 20 22 50 52 41 47 4d 41 20 6c 6f 63  mple "PRAGMA loc
293e1 6b 69 6e 67 5f 6d 6f 64 65 3b 22 20 73 74 61 74  king_mode;" stat
293e2 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61  ement. This is a
293e3 20 71 75 65 72 79 20 66 6f 72 0a 20 20 20 20 20   query for.     
293e4 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20   ** the current 
293e5 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20  default locking 
293e6 6d 6f 64 65 20 28 77 68 69 63 68 20 6d 61 79 20  mode (which may 
293e7 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f 0a  be different to.
293e8 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63        ** the loc
293e9 6b 69 6e 67 2d 6d 6f 64 65 20 6f 66 20 74 68 65  king-mode of the
293ea 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 29 2e   main database).
293eb 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
293ec 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c 74  eMode = db->dflt
293ed 4c 6f 63 6b 4d 6f 64 65 3b 0a 20 20 20 20 7d 65  LockMode;.    }e
293ee 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72  lse{.      Pager
293ef 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20   *pPager;.      
293f0 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 29  if( pId2->n==0 )
293f1 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
293f2 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  s indicates that
293f3 20 6e 6f 20 64 61 74 61 62 61 73 65 20 6e 61 6d   no database nam
293f4 65 20 77 61 73 20 73 70 65 63 69 66 69 65 64 20  e was specified 
293f5 61 73 20 70 61 72 74 0a 20 20 20 20 20 20 20 20  as part.        
293f6 2a 2a 20 6f 66 20 74 68 65 20 50 52 41 47 4d 41  ** of the PRAGMA
293f7 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68 69   command. In thi
293f8 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63 6b 69  s case the locki
293f9 6e 67 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65 0a  ng-mode must be.
293fa 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6f          ** set o
293fb 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64  n all attached d
293fc 61 74 61 62 61 73 65 73 2c 20 61 73 20 77 65 6c  atabases, as wel
293fd 6c 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64 62  l as the main db
293fe 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a   file..        *
293ff 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 6c 73  *.        ** Als
29400 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 33 2e 64  o, the sqlite3.d
29401 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 76 61 72 69  fltLockMode vari
29402 61 62 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74  able is set so t
29403 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  hat.        ** a
29404 6e 79 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20  ny subsequently 
29405 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
29406 65 73 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20  es also use the 
29407 73 70 65 63 69 66 69 65 64 0a 20 20 20 20 20 20  specified.      
29408 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64    ** locking mod
29409 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
2940a 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20        int ii;.  
2940b 20 20 20 20 20 20 61 73 73 65 72 74 28 70 44 62        assert(pDb
2940c 3d 3d 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a  ==&db->aDb[0]);.
2940d 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 32          for(ii=2
2940e 3b 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69  ; ii<db->nDb; ii
2940f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ++){.          p
29410 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
29411 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44  treePager(db->aD
29412 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20  b[ii].pBt);.    
29413 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
29414 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50  erLockingMode(pP
29415 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20  ager, eMode);.  
29416 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
29417 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  db->dfltLockMode
29418 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20   = (u8)eMode;.  
29419 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
2941a 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
2941b 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29  ePager(pDb->pBt)
2941c 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  ;.      eMode = 
2941d 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
2941e 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20  ingMode(pPager, 
2941f 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a 20  eMode);.    }.. 
29420 20 20 20 61 73 73 65 72 74 28 65 4d 6f 64 65 3d     assert(eMode=
29421 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f  =PAGER_LOCKINGMO
29422 44 45 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64 65  DE_NORMAL||eMode
29423 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
29424 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a  ODE_EXCLUSIVE);.
29425 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 50      if( eMode==P
29426 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
29427 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20  _EXCLUSIVE ){.  
29428 20 20 20 20 7a 52 65 74 20 3d 20 22 65 78 63 6c      zRet = "excl
29429 75 73 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20 20  usive";.    }.  
2942a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2942b 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
2942c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2942d 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
2942e 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f  OLNAME_NAME, "lo
2942f 63 6b 69 6e 67 5f 6d 6f 64 65 22 2c 20 53 51 4c  cking_mode", SQL
29430 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29431 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29432 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
29433 2c 20 30 2c 20 31 2c 20 30 2c 20 7a 52 65 74 2c  , 0, 1, 0, zRet,
29434 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
29435 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29436 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31  _ResultRow, 1, 1
29437 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a  );.  }else..  /*
29438 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
29439 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c  atabase.]journal
2943a 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41 47  _mode.  **  PRAG
2943b 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f  MA [database.]jo
2943c 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 28 64 65  urnal_mode = (de
2943d 6c 65 74 65 7c 70 65 72 73 69 73 74 7c 6f 66 66  lete|persist|off
2943e 7c 74 72 75 6e 63 61 74 65 7c 6d 65 6d 6f 72 79  |truncate|memory
2943f 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ).  */.  if( sql
29440 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29441 74 2c 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22  t,"journal_mode"
29442 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
29443 65 4d 6f 64 65 3b 0a 20 20 20 20 73 74 61 74 69  eMode;.    stati
29444 63 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61  c char * const a
29445 7a 4d 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a  zModeName[] = {.
29446 20 20 20 20 20 20 22 64 65 6c 65 74 65 22 2c 20        "delete", 
29447 22 70 65 72 73 69 73 74 22 2c 20 22 6f 66 66 22  "persist", "off"
29448 2c 20 22 74 72 75 6e 63 61 74 65 22 2c 20 22 6d  , "truncate", "m
29449 65 6d 6f 72 79 22 0a 20 20 20 20 7d 3b 0a 0a 20  emory".    };.. 
2944a 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30     if( zRight==0
2944b 20 29 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20   ){.      eMode 
2944c 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  = PAGER_JOURNALM
2944d 4f 44 45 5f 51 55 45 52 59 3b 0a 20 20 20 20 7d  ODE_QUERY;.    }
2944e 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
2944f 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
29450 6e 33 30 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  n30(zRight);.   
29451 20 20 20 65 4d 6f 64 65 20 3d 20 73 69 7a 65 6f     eMode = sizeo
29452 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 29 2f 73 69  f(azModeName)/si
29453 7a 65 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 5b  zeof(azModeName[
29454 30 5d 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 77  0]) - 1;.      w
29455 68 69 6c 65 28 20 65 4d 6f 64 65 3e 3d 30 20 26  hile( eMode>=0 &
29456 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
29457 70 28 7a 52 69 67 68 74 2c 20 61 7a 4d 6f 64 65  p(zRight, azMode
29458 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 6e 29 21  Name[eMode], n)!
29459 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 65 4d  =0 ){.        eM
2945a 6f 64 65 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20  ode--;.      }. 
2945b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64     }.    if( pId
2945c 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65  2->n==0 && eMode
2945d 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
2945e 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20 20  ODE_QUERY ){.   
2945f 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52     /* Simple "PR
29460 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  AGMA journal_mod
29461 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  e;" statement. T
29462 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20 66  his is a query f
29463 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  or.      ** the 
29464 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74 20  current default 
29465 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 28 77 68  journal mode (wh
29466 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66 65  ich may be diffe
29467 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  rent to.      **
29468 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64   the journal-mod
29469 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61  e of the main da
2946a 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20 2a  tabase)..      *
2946b 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  /.      eMode = 
2946c 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d  db->dfltJournalM
2946d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ode;.    }else{.
2946e 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61        Pager *pPa
2946f 67 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ger;.      if( p
29470 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20  Id2->n==0 ){.   
29471 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64       /* This ind
29472 69 63 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64  icates that no d
29473 61 74 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73  atabase name was
29474 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61   specified as pa
29475 72 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66  rt.        ** of
29476 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d   the PRAGMA comm
29477 61 6e 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  and. In this cas
29478 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  e the journal-mo
29479 64 65 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20  de must be.     
2947a 20 20 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c     ** set on all
2947b 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
2947c 73 65 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20  ses, as well as 
2947d 74 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65  the main db file
2947e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
2947f 20 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68       ** Also, th
29480 65 20 73 71 6c 69 74 65 33 2e 64 66 6c 74 4a 6f  e sqlite3.dfltJo
29481 75 72 6e 61 6c 4d 6f 64 65 20 76 61 72 69 61 62  urnalMode variab
29482 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74 68 61  le is set so tha
29483 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79  t.        ** any
29484 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 61 74   subsequently at
29485 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
29486 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20 73 70   also use the sp
29487 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 20 20  ecified.        
29488 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e  ** journal mode.
29489 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
2948a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20      int ii;.    
2948b 20 20 20 20 61 73 73 65 72 74 28 70 44 62 3d 3d      assert(pDb==
2948c 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20  &db->aDb[0]);.  
2948d 20 20 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20        for(ii=1; 
2948e 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b  ii<db->nDb; ii++
2948f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
29490 20 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74   db->aDb[ii].pBt
29491 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
29492 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
29493 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61  BtreePager(db->a
29494 44 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20  Db[ii].pBt);.   
29495 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29496 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65  PagerJournalMode
29497 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b  (pPager, eMode);
29498 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
29499 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
2949a 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f  b->dfltJournalMo
2949b 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a  de = (u8)eMode;.
2949c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50        }.      pP
2949d 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
2949e 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42  reePager(pDb->pB
2949f 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20  t);.      eMode 
294a0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  = sqlite3PagerJo
294a1 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72  urnalMode(pPager
294a2 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a  , eMode);.    }.
294a3 20 20 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64      assert( eMod
294a4 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
294a5 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 20 20 20  MODE_DELETE.    
294a6 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
294a7 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
294a8 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 20  LMODE_TRUNCATE. 
294a9 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
294aa 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
294ab 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
294ac 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  .              |
294ad 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
294ae 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 20  OURNALMODE_OFF. 
294af 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
294b0 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
294b1 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
294b2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
294b3 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
294b4 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
294b5 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
294b6 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
294b7 2c 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22  , "journal_mode"
294b8 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
294b9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
294ba 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
294bb 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20  ring8, 0, 1, 0, 
294bc 0a 20 20 20 20 20 20 20 20 20 20 20 61 7a 4d 6f  .           azMo
294bd 64 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 50  deName[eMode], P
294be 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  4_STATIC);.    s
294bf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
294c0 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
294c1 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65  , 1, 1);.  }else
294c2 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
294c3 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a  GMA [database.]j
294c4 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69  ournal_size_limi
294c5 74 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  t.  **  PRAGMA [
294c6 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61  database.]journa
294c7 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 3d 4e 0a 20  l_size_limit=N. 
294c8 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20   **.  ** Get or 
294c9 73 65 74 20 74 68 65 20 73 69 7a 65 20 6c 69 6d  set the size lim
294ca 69 74 20 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a  it on rollback j
294cb 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 20 20  ournal files..  
294cc 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
294cd 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a  StrICmp(zLeft,"j
294ce 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69  ournal_size_limi
294cf 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61  t")==0 ){.    Pa
294d0 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71  ger *pPager = sq
294d1 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
294d2 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69  pDb->pBt);.    i
294d3 36 34 20 69 4c 69 6d 69 74 20 3d 20 2d 32 3b 0a  64 iLimit = -2;.
294d4 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 29      if( zRight )
294d5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41  {.      sqlite3A
294d6 74 6f 69 36 34 28 7a 52 69 67 68 74 2c 20 26 69  toi64(zRight, &i
294d7 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 69 66  Limit);.      if
294d8 28 20 69 4c 69 6d 69 74 3c 2d 31 20 29 20 69 4c  ( iLimit<-1 ) iL
294d9 69 6d 69 74 20 3d 20 2d 31 3b 0a 20 20 20 20 7d  imit = -1;.    }
294da 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 73 71  .    iLimit = sq
294db 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
294dc 6c 53 69 7a 65 4c 69 6d 69 74 28 70 50 61 67 65  lSizeLimit(pPage
294dd 72 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  r, iLimit);.    
294de 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
294df 70 50 61 72 73 65 2c 20 22 6a 6f 75 72 6e 61 6c  pParse, "journal
294e0 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 2c 20 69 4c  _size_limit", iL
294e1 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a  imit);.  }else..
294e2 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
294e3 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
294e4 4d 41 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a  MAS */..  /*.  *
294e5 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
294e6 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d  ase.]auto_vacuum
294e7 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
294e8 61 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61  atabase.]auto_va
294e9 63 75 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a  cuum=N.  **.  **
294ea 20 47 65 74 20 6f 72 20 73 65 74 20 74 68 65 20   Get or set the 
294eb 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74  value of the dat
294ec 61 62 61 73 65 20 27 61 75 74 6f 2d 76 61 63 75  abase 'auto-vacu
294ed 75 6d 27 20 70 61 72 61 6d 65 74 65 72 2e 0a 20  um' parameter.. 
294ee 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73   ** The value is
294ef 20 6f 6e 65 20 6f 66 3a 20 20 30 20 4e 4f 4e 45   one of:  0 NONE
294f0 20 31 20 46 55 4c 4c 20 32 20 49 4e 43 52 45 4d   1 FULL 2 INCREM
294f1 45 4e 54 41 4c 0a 20 20 2a 2f 0a 23 69 66 6e 64  ENTAL.  */.#ifnd
294f2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
294f3 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20  UTOVACUUM.  if( 
294f4 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
294f5 4c 65 66 74 2c 22 61 75 74 6f 5f 76 61 63 75 75  Left,"auto_vacuu
294f6 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74  m")==0 ){.    Bt
294f7 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e  ree *pBt = pDb->
294f8 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pBt;.    assert(
294f9 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 69   pBt!=0 );.    i
294fa 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
294fb 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a  hema(pParse) ){.
294fc 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d        goto pragm
294fd 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  a_out;.    }.   
294fe 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
294ff 20 20 20 20 20 20 69 6e 74 20 61 75 74 6f 5f 76        int auto_v
29500 61 63 75 75 6d 3b 0a 20 20 20 20 20 20 69 66 28  acuum;.      if(
29501 20 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a   ALWAYS(pBt) ){.
29502 20 20 20 20 20 20 20 20 20 61 75 74 6f 5f 76 61           auto_va
29503 63 75 75 6d 20 3d 20 73 71 6c 69 74 65 33 42 74  cuum = sqlite3Bt
29504 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
29505 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  (pBt);.      }el
29506 73 65 7b 0a 20 20 20 20 20 20 20 20 20 61 75 74  se{.         aut
29507 6f 5f 76 61 63 75 75 6d 20 3d 20 53 51 4c 49 54  o_vacuum = SQLIT
29508 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41  E_DEFAULT_AUTOVA
29509 43 55 55 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  CUUM;.      }.  
2950a 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
2950b 49 6e 74 28 70 50 61 72 73 65 2c 20 22 61 75 74  Int(pParse, "aut
2950c 6f 5f 76 61 63 75 75 6d 22 2c 20 61 75 74 6f 5f  o_vacuum", auto_
2950d 76 61 63 75 75 6d 29 3b 0a 20 20 20 20 7d 65 6c  vacuum);.    }el
2950e 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 41  se{.      int eA
2950f 75 74 6f 20 3d 20 67 65 74 41 75 74 6f 56 61 63  uto = getAutoVac
29510 75 75 6d 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  uum(zRight);.   
29511 20 20 20 61 73 73 65 72 74 28 20 65 41 75 74 6f     assert( eAuto
29512 3e 3d 30 20 26 26 20 65 41 75 74 6f 3c 3d 32 20  >=0 && eAuto<=2 
29513 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65 78  );.      db->nex
29514 74 41 75 74 6f 76 61 63 20 3d 20 28 75 38 29 65  tAutovac = (u8)e
29515 41 75 74 6f 3b 0a 20 20 20 20 20 20 69 66 28 20  Auto;.      if( 
29516 41 4c 57 41 59 53 28 65 41 75 74 6f 3e 3d 30 29  ALWAYS(eAuto>=0)
29517 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
29518 61 6c 6c 20 53 65 74 41 75 74 6f 56 61 63 75 75  all SetAutoVacuu
29519 6d 28 29 20 74 6f 20 73 65 74 20 69 6e 69 74 69  m() to set initi
2951a 61 6c 69 7a 65 20 74 68 65 20 69 6e 74 65 72 6e  alize the intern
2951b 61 6c 20 61 75 74 6f 20 61 6e 64 0a 20 20 20 20  al auto and.    
2951c 20 20 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63 75      ** incr-vacu
2951d 75 6d 20 66 6c 61 67 73 2e 20 54 68 69 73 20 69  um flags. This i
2951e 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 63 61  s required in ca
2951f 73 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  se this connecti
29520 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 72  on.        ** cr
29521 65 61 74 65 73 20 74 68 65 20 64 61 74 61 62 61  eates the databa
29522 73 65 20 66 69 6c 65 2e 20 49 74 20 69 73 20 69  se file. It is i
29523 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 69 74  mportant that it
29524 20 69 73 20 63 72 65 61 74 65 64 0a 20 20 20 20   is created.    
29525 20 20 20 20 2a 2a 20 61 73 20 61 6e 20 61 75 74      ** as an aut
29526 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65  o-vacuum capable
29527 20 64 62 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a   db..        */.
29528 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d          int rc =
29529 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
2952a 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74 2c 20  AutoVacuum(pBt, 
2952b 65 41 75 74 6f 29 3b 0a 20 20 20 20 20 20 20 20  eAuto);.        
2952c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2952d 4b 20 26 26 20 28 65 41 75 74 6f 3d 3d 31 20 7c  K && (eAuto==1 |
2952e 7c 20 65 41 75 74 6f 3d 3d 32 29 20 29 7b 0a 20  | eAuto==2) ){. 
2952f 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e           /* When
29530 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75 74   setting the aut
29531 6f 5f 76 61 63 75 75 6d 20 6d 6f 64 65 20 74 6f  o_vacuum mode to
29532 20 65 69 74 68 65 72 20 22 66 75 6c 6c 22 20 6f   either "full" o
29533 72 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  r .          ** 
29534 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 2c 20 77  "incremental", w
29535 72 69 74 65 20 74 68 65 20 76 61 6c 75 65 20 6f  rite the value o
29536 66 20 6d 65 74 61 5b 36 5d 20 69 6e 20 74 68 65  f meta[6] in the
29537 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20   database.      
29538 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65 66      ** file. Bef
29539 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 6d  ore writing to m
2953a 65 74 61 5b 36 5d 2c 20 63 68 65 63 6b 20 74 68  eta[6], check th
2953b 61 74 20 6d 65 74 61 5b 33 5d 20 69 6e 64 69 63  at meta[3] indic
2953c 61 74 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a  ates.          *
2953d 2a 20 74 68 61 74 20 74 68 69 73 20 72 65 61 6c  * that this real
2953e 6c 79 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61  ly is an auto-va
2953f 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64 61 74  cuum capable dat
29540 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20  abase..         
29541 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74   */.          st
29542 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
29543 70 4c 69 73 74 20 73 65 74 4d 65 74 61 36 5b 5d  pList setMeta6[]
29544 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20   = {.           
29545 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f   { OP_Transactio
29546 6e 2c 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  n,    0,        
29547 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
29548 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20      0},    /* 0 
29549 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
2954a 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20   OP_ReadCookie, 
2954b 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 31      0,         1
2954c 2c 20 20 20 20 20 20 20 20 20 42 54 52 45 45 5f  ,         BTREE_
2954d 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47  LARGEST_ROOT_PAG
2954e 45 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  E},.            
2954f 7b 20 4f 50 5f 49 66 2c 20 20 20 20 20 20 20 20  { OP_If,        
29550 20 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20       1,         
29551 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
29552 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a     0},    /* 2 *
29553 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
29554 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20  OP_Halt,        
29555 20 20 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45     SQLITE_OK, OE
29556 5f 41 62 6f 72 74 2c 20 20 20 20 20 20 20 20 20  _Abort,         
29557 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a   0},    /* 3 */.
29558 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29559 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20  _Integer,       
2955a 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20   0,         1,  
2955b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
2955c 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20  },    /* 4 */.  
2955d 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
2955e 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30  etCookie,      0
2955f 2c 20 20 20 20 20 20 20 20 20 42 54 52 45 45 5f  ,         BTREE_
29560 49 4e 43 52 5f 56 41 43 55 55 4d 2c 20 31 7d 2c  INCR_VACUUM, 1},
29561 20 20 20 20 2f 2a 20 35 20 2a 2f 0a 20 20 20 20      /* 5 */.    
29562 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20        };.       
29563 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20     int iAddr;.  
29564 20 20 20 20 20 20 20 20 69 41 64 64 72 20 3d 20          iAddr = 
29565 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29566 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a  List(v, ArraySiz
29567 65 28 73 65 74 4d 65 74 61 36 29 2c 20 73 65 74  e(setMeta6), set
29568 4d 65 74 61 36 29 3b 0a 20 20 20 20 20 20 20 20  Meta6);.        
29569 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2956a 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2c 20  ngeP1(v, iAddr, 
2956b 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iDb);.          
2956c 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2956d 65 50 31 28 76 2c 20 69 41 64 64 72 2b 31 2c 20  eP1(v, iAddr+1, 
2956e 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iDb);.          
2956f 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29570 65 50 32 28 76 2c 20 69 41 64 64 72 2b 32 2c 20  eP2(v, iAddr+2, 
29571 69 41 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20  iAddr+4);.      
29572 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29573 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72  hangeP1(v, iAddr
29574 2b 34 2c 20 65 41 75 74 6f 2d 31 29 3b 0a 20 20  +4, eAuto-1);.  
29575 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29576 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69  dbeChangeP1(v, i
29577 41 64 64 72 2b 35 2c 20 69 44 62 29 3b 0a 20 20  Addr+5, iDb);.  
29578 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29579 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
2957a 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  iDb);.        }.
2957b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2957c 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20  }else.#endif..  
2957d 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  /*.  **  PRAGMA 
2957e 5b 64 61 74 61 62 61 73 65 2e 5d 69 6e 63 72 65  [database.]incre
2957f 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 28 4e 29  mental_vacuum(N)
29580 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 6f 20 4e 20  .  **.  ** Do N 
29581 73 74 65 70 73 20 6f 66 20 69 6e 63 72 65 6d 65  steps of increme
29582 6e 74 61 6c 20 76 61 63 75 75 6d 69 6e 67 20 6f  ntal vacuuming o
29583 6e 20 61 20 64 61 74 61 62 61 73 65 2e 0a 20 20  n a database..  
29584 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
29585 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
29586 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  M.  if( sqlite3S
29587 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 69 6e  trICmp(zLeft,"in
29588 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d  cremental_vacuum
29589 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ")==0 ){.    int
2958a 20 69 4c 69 6d 69 74 2c 20 61 64 64 72 3b 0a 20   iLimit, addr;. 
2958b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
2958c 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
2958d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
2958e 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d  ragma_out;.    }
2958f 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d  .    if( zRight=
29590 3d 30 20 7c 7c 20 21 73 71 6c 69 74 65 33 47 65  =0 || !sqlite3Ge
29591 74 49 6e 74 33 32 28 7a 52 69 67 68 74 2c 20 26  tInt32(zRight, &
29592 69 4c 69 6d 69 74 29 20 7c 7c 20 69 4c 69 6d 69  iLimit) || iLimi
29593 74 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 4c  t<=0 ){.      iL
29594 69 6d 69 74 20 3d 20 30 78 37 66 66 66 66 66 66  imit = 0x7ffffff
29595 66 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  f;.    }.    sql
29596 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
29597 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
29598 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  0, iDb);.    sql
29599 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2959a 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 4c  , OP_Integer, iL
2959b 69 6d 69 74 2c 20 31 29 3b 0a 20 20 20 20 61 64  imit, 1);.    ad
2959c 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
2959d 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 6e 63  AddOp1(v, OP_Inc
2959e 72 56 61 63 75 75 6d 2c 20 69 44 62 29 3b 0a 20  rVacuum, iDb);. 
2959f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
295a0 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp1(v, OP_Resul
295a1 74 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20 73 71  tRow, 1);.    sq
295a2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
295a3 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 31 2c  v, OP_AddImm, 1,
295a4 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   -1);.    sqlite
295a5 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
295a6 50 5f 49 66 50 6f 73 2c 20 31 2c 20 61 64 64 72  P_IfPos, 1, addr
295a7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
295a8 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
295a9 64 72 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  dr);.  }else.#en
295aa 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
295ab 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
295ac 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a  RAGMAS.  /*.  **
295ad 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
295ae 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 0a 20  se.]cache_size. 
295af 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
295b0 61 62 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a  abase.]cache_siz
295b1 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  e=N.  **.  ** Th
295b2 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70  e first form rep
295b3 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74  orts the current
295b4 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 66   local setting f
295b5 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65  or the.  ** page
295b6 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68   cache size.  Th
295b7 65 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20  e local setting 
295b8 63 61 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74  can be different
295b9 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 70   from.  ** the p
295ba 65 72 73 69 73 74 65 6e 74 20 63 61 63 68 65 20  ersistent cache 
295bb 73 69 7a 65 20 76 61 6c 75 65 20 74 68 61 74 20  size value that 
295bc 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  is stored in the
295bd 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66   database.  ** f
295be 69 6c 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65  ile itself.  The
295bf 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
295c0 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  is the maximum n
295c1 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61  umber of.  ** pa
295c2 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20  ges in the page 
295c3 63 61 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f  cache.  The seco
295c4 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65  nd form sets the
295c5 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20 70 61 67 65   local.  ** page
295c6 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75   cache size valu
295c7 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  e.  It does not 
295c8 63 68 61 6e 67 65 20 74 68 65 20 70 65 72 73 69  change the persi
295c9 73 74 65 6e 74 0a 20 20 2a 2a 20 63 61 63 68 65  stent.  ** cache
295ca 20 73 69 7a 65 20 73 74 6f 72 65 64 20 6f 6e 20   size stored on 
295cb 74 68 65 20 64 69 73 6b 20 73 6f 20 74 68 65 20  the disk so the 
295cc 63 61 63 68 65 20 73 69 7a 65 20 77 69 6c 6c 20  cache size will 
295cd 72 65 76 65 72 74 0a 20 20 2a 2a 20 74 6f 20 69  revert.  ** to i
295ce 74 73 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  ts default value
295cf 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
295d0 73 65 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64  se is closed and
295d1 20 72 65 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 20   reopened..  ** 
295d2 4e 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f  N should be a po
295d3 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2e 0a  sitive integer..
295d4 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
295d5 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
295d6 22 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30  "cache_size")==0
295d7 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
295d8 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
295d9 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
295da 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28  gma_out;.    if(
295db 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
295dc 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
295dd 74 28 70 50 61 72 73 65 2c 20 22 63 61 63 68 65  t(pParse, "cache
295de 5f 73 69 7a 65 22 2c 20 70 44 62 2d 3e 70 53 63  _size", pDb->pSc
295df 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
295e0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
295e1 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61      int size = a
295e2 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  toi(zRight);.   
295e3 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20     if( size<0 ) 
295e4 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20  size = -size;.  
295e5 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
295e6 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73  ->cache_size = s
295e7 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ize;.      sqlit
295e8 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
295e9 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
295ea 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
295eb 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20  e_size);.    }. 
295ec 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
295ed 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f  *   PRAGMA temp_
295ee 73 74 6f 72 65 0a 20 20 2a 2a 20 20 20 50 52 41  store.  **   PRA
295ef 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 20 3d  GMA temp_store =
295f0 20 22 64 65 66 61 75 6c 74 22 7c 22 6d 65 6d 6f   "default"|"memo
295f1 72 79 22 7c 22 66 69 6c 65 22 0a 20 20 2a 2a 0a  ry"|"file".  **.
295f2 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73    ** Return or s
295f3 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c  et the local val
295f4 75 65 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73  ue of the temp_s
295f5 74 6f 72 65 20 66 6c 61 67 2e 20 20 43 68 61 6e  tore flag.  Chan
295f6 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f  ging.  ** the lo
295f7 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e  cal value does n
295f8 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20  ot make changes 
295f9 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65  to the disk file
295fa 20 61 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74   and the default
295fb 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c  .  ** value will
295fc 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 68 65   be restored the
295fd 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 64   next time the d
295fe 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
295ff 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74  d..  **.  ** Not
29600 65 20 74 68 61 74 20 69 74 20 69 73 20 70 6f 73  e that it is pos
29601 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 6c 69  sible for the li
29602 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69  brary compile-ti
29603 6d 65 20 6f 70 74 69 6f 6e 73 20 74 6f 0a 20 20  me options to.  
29604 2a 2a 20 6f 76 65 72 72 69 64 65 20 74 68 69 73  ** override this
29605 20 73 65 74 74 69 6e 67 0a 20 20 2a 2f 0a 20 20   setting.  */.  
29606 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
29607 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f  mp(zLeft, "temp_
29608 73 74 6f 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20  store")==0 ){.  
29609 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
2960a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e  .      returnSin
2960b 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22  gleInt(pParse, "
2960c 74 65 6d 70 5f 73 74 6f 72 65 22 2c 20 64 62 2d  temp_store", db-
2960d 3e 74 65 6d 70 5f 73 74 6f 72 65 29 3b 0a 20 20  >temp_store);.  
2960e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
2960f 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65  hangeTempStorage
29610 28 70 50 61 72 73 65 2c 20 7a 52 69 67 68 74 29  (pParse, zRight)
29611 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
29612 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41  .  /*.  **   PRA
29613 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64  GMA temp_store_d
29614 69 72 65 63 74 6f 72 79 0a 20 20 2a 2a 20 20 20  irectory.  **   
29615 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72  PRAGMA temp_stor
29616 65 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 22 22  e_directory = ""
29617 7c 22 64 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65  |"directory_name
29618 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  ".  **.  ** Retu
29619 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f  rn or set the lo
2961a 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65  cal value of the
2961b 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65   temp_store_dire
2961c 63 74 6f 72 79 20 66 6c 61 67 2e 20 20 43 68 61  ctory flag.  Cha
2961d 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 76  nging.  ** the v
2961e 61 6c 75 65 20 73 65 74 73 20 61 20 73 70 65 63  alue sets a spec
2961f 69 66 69 63 20 64 69 72 65 63 74 6f 72 79 20 74  ific directory t
29620 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 65  o be used for te
29621 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 20  mporary files.. 
29622 20 2a 2a 20 53 65 74 74 69 6e 67 20 74 6f 20 61   ** Setting to a
29623 20 6e 75 6c 6c 20 73 74 72 69 6e 67 20 72 65 76   null string rev
29624 65 72 74 73 20 74 6f 20 74 68 65 20 64 65 66 61  erts to the defa
29625 75 6c 74 20 74 65 6d 70 6f 72 61 72 79 20 64 69  ult temporary di
29626 72 65 63 74 6f 72 79 20 73 65 61 72 63 68 2e 0a  rectory search..
29627 20 20 2a 2a 20 49 66 20 74 65 6d 70 6f 72 61 72    ** If temporar
29628 79 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 63  y directory is c
29629 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 69 6e 76  hanged, then inv
2962a 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61  alidateTempStora
2962b 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20  ge..  **.  */.  
2962c 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2962d 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f  mp(zLeft, "temp_
2962e 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22  store_directory"
2962f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
29630 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
29631 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d   if( sqlite3_tem
29632 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20  p_directory ){. 
29633 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29634 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
29635 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
29636 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
29637 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
29638 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20  NAME, .         
29639 20 20 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64     "temp_store_d
2963a 69 72 65 63 74 6f 72 79 22 2c 20 53 51 4c 49 54  irectory", SQLIT
2963b 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
2963c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2963d 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
2963e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 73 71 6c  g8, 0, 1, 0, sql
2963f 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
29640 6f 72 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ory, 0);.       
29641 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29642 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
29643 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20  ow, 1, 1);.     
29644 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69   }.    }else{.#i
29645 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29646 54 5f 57 53 44 0a 20 20 20 20 20 20 69 66 28 20  T_WSD.      if( 
29647 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20  zRight[0] ){.   
29648 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
29649 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20       int res;.  
2964a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2964b 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70  e3OsAccess(db->p
2964c 56 66 73 2c 20 7a 52 69 67 68 74 2c 20 53 51 4c  Vfs, zRight, SQL
2964d 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
2964e 52 49 54 45 2c 20 26 72 65 73 29 3b 0a 20 20 20  RITE, &res);.   
2964f 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
29650 49 54 45 5f 4f 4b 20 7c 7c 20 72 65 73 3d 3d 30  ITE_OK || res==0
29651 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
29652 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
29653 61 72 73 65 2c 20 22 6e 6f 74 20 61 20 77 72 69  arse, "not a wri
29654 74 61 62 6c 65 20 64 69 72 65 63 74 6f 72 79 22  table directory"
29655 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
29656 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
29657 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
29658 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45        if( SQLITE
29659 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20  _TEMP_STORE==0. 
2965a 20 20 20 20 20 20 7c 7c 20 28 53 51 4c 49 54 45        || (SQLITE
2965b 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 20 26  _TEMP_STORE==1 &
2965c 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65  & db->temp_store
2965d 3c 3d 31 29 0a 20 20 20 20 20 20 20 7c 7c 20 28  <=1).       || (
2965e 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
2965f 45 3d 3d 32 20 26 26 20 64 62 2d 3e 74 65 6d 70  E==2 && db->temp
29660 5f 73 74 6f 72 65 3d 3d 31 29 0a 20 20 20 20 20  _store==1).     
29661 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61   ){.        inva
29662 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67  lidateTempStorag
29663 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  e(pParse);.     
29664 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
29665 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 74 65  _free(sqlite3_te
29666 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20  mp_directory);. 
29667 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b       if( zRight[
29668 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  0] ){.        sq
29669 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
2966a 74 6f 72 79 20 3d 20 73 71 6c 69 74 65 33 44 62  tory = sqlite3Db
2966b 53 74 72 44 75 70 28 30 2c 20 7a 52 69 67 68 74  StrDup(0, zRight
2966c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2966d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2966e 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
2966f 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64   0;.      }.#end
29670 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
29671 54 5f 57 53 44 20 2a 2f 0a 20 20 20 20 7d 0a 20  T_WSD */.    }. 
29672 20 7d 65 6c 73 65 0a 0a 23 69 66 20 21 64 65 66   }else..#if !def
29673 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
29674 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
29675 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28  ).#  if defined(
29676 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20  __APPLE__).#    
29677 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e  define SQLITE_EN
29678 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
29679 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20  LE 1.#  else.#  
2967a 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
2967b 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
2967c 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a  TYLE 0.#  endif.
2967d 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
2967e 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
2967f 5f 53 54 59 4c 45 0a 20 20 2f 2a 0a 20 20 20 2a  _STYLE.  /*.   *
29680 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  *   PRAGMA [data
29681 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79  base.]lock_proxy
29682 5f 66 69 6c 65 0a 20 20 20 2a 2a 20 20 20 50 52  _file.   **   PR
29683 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
29684 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20  lock_proxy_file 
29685 3d 20 22 3a 61 75 74 6f 3a 22 7c 22 6c 6f 63 6b  = ":auto:"|"lock
29686 5f 66 69 6c 65 5f 70 61 74 68 22 0a 20 20 20 2a  _file_path".   *
29687 2a 0a 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f  *.   ** Return o
29688 72 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  r set the value 
29689 6f 66 20 74 68 65 20 6c 6f 63 6b 5f 70 72 6f 78  of the lock_prox
2968a 79 5f 66 69 6c 65 20 66 6c 61 67 2e 20 20 43 68  y_file flag.  Ch
2968b 61 6e 67 69 6e 67 0a 20 20 20 2a 2a 20 74 68 65  anging.   ** the
2968c 20 76 61 6c 75 65 20 73 65 74 73 20 61 20 73 70   value sets a sp
2968d 65 63 69 66 69 63 20 66 69 6c 65 20 74 6f 20 62  ecific file to b
2968e 65 20 75 73 65 64 20 66 6f 72 20 64 61 74 61 62  e used for datab
2968f 61 73 65 20 61 63 63 65 73 73 20 6c 6f 63 6b 73  ase access locks
29690 2e 0a 20 20 20 2a 2a 0a 20 20 20 2a 2f 0a 20 20  ..   **.   */.  
29691 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
29692 6d 70 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f  mp(zLeft, "lock_
29693 70 72 6f 78 79 5f 66 69 6c 65 22 29 3d 3d 30 20  proxy_file")==0 
29694 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  ){.    if( !zRig
29695 68 74 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65  ht ){.      Page
29696 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69  r *pPager = sqli
29697 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44  te3BtreePager(pD
29698 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 63  b->pBt);.      c
29699 68 61 72 20 2a 70 72 6f 78 79 5f 66 69 6c 65 5f  har *proxy_file_
2969a 70 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  path = NULL;.   
2969b 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
2969c 2a 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33  *pFile = sqlite3
2969d 50 61 67 65 72 46 69 6c 65 28 70 50 61 67 65 72  PagerFile(pPager
2969e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2969f 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46  OsFileControl(pF
296a0 69 6c 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f  ile, SQLITE_GET_
296a1 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0a  LOCKPROXYFILE, .
296a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
296a3 20 20 20 20 20 20 20 20 20 20 20 26 70 72 6f 78             &prox
296a4 79 5f 66 69 6c 65 5f 70 61 74 68 29 3b 0a 20 20  y_file_path);.  
296a5 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 70      .      if( p
296a6 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68 20 29  roxy_file_path )
296a7 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
296a8 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
296a9 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  v, 1);.        s
296aa 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
296ab 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
296ac 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20  ME_NAME, .      
296ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
296ae 20 20 20 20 20 20 20 20 22 6c 6f 63 6b 5f 70 72          "lock_pr
296af 6f 78 79 5f 66 69 6c 65 22 2c 20 53 51 4c 49 54  oxy_file", SQLIT
296b0 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
296b1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
296b2 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
296b3 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 70 72 6f  g8, 0, 1, 0, pro
296b4 78 79 5f 66 69 6c 65 5f 70 61 74 68 2c 20 30 29  xy_file_path, 0)
296b5 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
296b6 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
296b7 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
296b8 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1);.      }.    
296b9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67  }else{.      Pag
296ba 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c  er *pPager = sql
296bb 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
296bc 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20  Db->pBt);.      
296bd 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
296be 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ile = sqlite3Pag
296bf 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a  erFile(pPager);.
296c0 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
296c1 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b       if( zRight[
296c2 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  0] ){.        re
296c3 73 3d 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  s=sqlite3OsFileC
296c4 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51  ontrol(pFile, SQ
296c5 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_SET_LOCKPRO
296c6 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20  XYFILE, .       
296c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
296c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 52                zR
296c9 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 20 65  ight);.      } e
296ca 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 72 65  lse {.        re
296cb 73 3d 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  s=sqlite3OsFileC
296cc 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51  ontrol(pFile, SQ
296cd 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_SET_LOCKPRO
296ce 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20  XYFILE, .       
296cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
296d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55                NU
296d1 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  LL);.      }.   
296d2 20 20 20 69 66 28 20 72 65 73 21 3d 53 51 4c 49     if( res!=SQLI
296d3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
296d4 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
296d5 28 70 50 61 72 73 65 2c 20 22 66 61 69 6c 65 64  (pParse, "failed
296d6 20 74 6f 20 73 65 74 20 6c 6f 63 6b 20 70 72 6f   to set lock pro
296d7 78 79 20 66 69 6c 65 22 29 3b 0a 20 20 20 20 20  xy file");.     
296d8 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f     goto pragma_o
296d9 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
296da 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
296db 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
296dc 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
296dd 2a 2f 20 20 20 20 20 20 0a 20 20 20 20 0a 20 20  */      .    .  
296de 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  /*.  **   PRAGMA
296df 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e 63   [database.]sync
296e0 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20 20 20 50  hronous.  **   P
296e1 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
296e2 5d 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46  ]synchronous=OFF
296e3 7c 4f 4e 7c 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a  |ON|NORMAL|FULL.
296e4 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    **.  ** Return
296e5 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61   or set the loca
296e6 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73  l value of the s
296e7 79 6e 63 68 72 6f 6e 6f 75 73 20 66 6c 61 67 2e  ynchronous flag.
296e8 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20    Changing.  ** 
296e9 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20  the local value 
296ea 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63 68  does not make ch
296eb 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 69 73  anges to the dis
296ec 6b 20 66 69 6c 65 20 61 6e 64 20 74 68 65 0a 20  k file and the. 
296ed 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75   ** default valu
296ee 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72  e will be restor
296ef 65 64 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65  ed the next time
296f0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
296f1 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 2e 0a 20 20  .  ** opened..  
296f2 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
296f3 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 73  StrICmp(zLeft,"s
296f4 79 6e 63 68 72 6f 6e 6f 75 73 22 29 3d 3d 30 20  ynchronous")==0 
296f5 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
296f6 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
296f7 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
296f8 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20  ma_out;.    if( 
296f9 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
296fa 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74   returnSingleInt
296fb 28 70 50 61 72 73 65 2c 20 22 73 79 6e 63 68 72  (pParse, "synchr
296fc 6f 6e 6f 75 73 22 2c 20 70 44 62 2d 3e 73 61 66  onous", pDb->saf
296fd 65 74 79 5f 6c 65 76 65 6c 2d 31 29 3b 0a 20 20  ety_level-1);.  
296fe 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
296ff 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
29700 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  it ){.        sq
29701 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
29702 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20  arse, .         
29703 20 20 20 22 53 61 66 65 74 79 20 6c 65 76 65 6c     "Safety level
29704 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e   may not be chan
29705 67 65 64 20 69 6e 73 69 64 65 20 61 20 74 72 61  ged inside a tra
29706 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20  nsaction");.    
29707 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
29708 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76   pDb->safety_lev
29709 65 6c 20 3d 20 67 65 74 53 61 66 65 74 79 4c 65  el = getSafetyLe
2970a 76 65 6c 28 7a 52 69 67 68 74 29 2b 31 3b 0a 20  vel(zRight)+1;. 
2970b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2970c 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
2970d 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
2970e 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
2970f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
29710 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20 20  _FLAG_PRAGMAS.  
29711 69 66 28 20 66 6c 61 67 50 72 61 67 6d 61 28 70  if( flagPragma(p
29712 50 61 72 73 65 2c 20 7a 4c 65 66 74 2c 20 7a 52  Parse, zLeft, zR
29713 69 67 68 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20  ight) ){.    /* 
29714 54 68 65 20 66 6c 61 67 50 72 61 67 6d 61 28 29  The flagPragma()
29715 20 73 75 62 72 6f 75 74 69 6e 65 20 61 6c 73 6f   subroutine also
29716 20 67 65 6e 65 72 61 74 65 73 20 61 6e 79 20 6e   generates any n
29717 65 63 65 73 73 61 72 79 20 63 6f 64 65 0a 20 20  ecessary code.  
29718 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f    ** there is no
29719 74 68 69 6e 67 20 6d 6f 72 65 20 74 6f 20 64 6f  thing more to do
2971a 20 68 65 72 65 20 2a 2f 0a 20 20 7d 65 6c 73 65   here */.  }else
2971b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2971c 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47  E_OMIT_FLAG_PRAG
2971d 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  MAS */..#ifndef 
2971e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45  SQLITE_OMIT_SCHE
2971f 4d 41 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a  MA_PRAGMAS.  /*.
29720 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 61    **   PRAGMA ta
29721 62 6c 65 5f 69 6e 66 6f 28 3c 74 61 62 6c 65 3e  ble_info(<table>
29722 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  ).  **.  ** Retu
29723 72 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  rn a single row 
29724 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
29725 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62  of the named tab
29726 6c 65 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20  le. The columns 
29727 6f 66 0a 20 20 2a 2a 20 74 68 65 20 72 65 74 75  of.  ** the retu
29728 72 6e 65 64 20 64 61 74 61 20 73 65 74 20 61 72  rned data set ar
29729 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 69 64  e:.  **.  ** cid
2972a 3a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  :        Column 
2972b 69 64 20 28 6e 75 6d 62 65 72 65 64 20 66 72 6f  id (numbered fro
2972c 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2c  m left to right,
2972d 20 73 74 61 72 74 69 6e 67 20 61 74 20 30 29 0a   starting at 0).
2972e 20 20 2a 2a 20 6e 61 6d 65 3a 20 20 20 20 20 20    ** name:      
2972f 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a   Column name.  *
29730 2a 20 74 79 70 65 3a 20 20 20 20 20 20 20 43 6f  * type:       Co
29731 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e  lumn declaration
29732 20 74 79 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e   type..  ** notn
29733 75 6c 6c 3a 20 20 20 20 54 72 75 65 20 69 66 20  ull:    True if 
29734 27 4e 4f 54 20 4e 55 4c 4c 27 20 69 73 20 70 61  'NOT NULL' is pa
29735 72 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 64 65 63  rt of column dec
29736 6c 61 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 66  laration.  ** df
29737 6c 74 5f 76 61 6c 75 65 3a 20 54 68 65 20 64 65  lt_value: The de
29738 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
29739 74 68 65 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 61  the column, if a
2973a 6e 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ny..  */.  if( s
2973b 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
2973c 65 66 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66 6f  eft, "table_info
2973d 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
2973e 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
2973f 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ab;.    if( sqli
29740 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
29741 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
29742 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61  gma_out;.    pTa
29743 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
29744 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c  able(db, zRight,
29745 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
29746 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Tab ){.      int
29747 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 48   i;.      int nH
29748 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  idden = 0;.     
29749 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
2974a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2974b 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 36 29  SetNumCols(v, 6)
2974c 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
2974d 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 20 20  nMem = 6;.      
2974e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2974f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
29750 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c  AME_NAME, "cid",
29751 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
29752 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29753 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
29754 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
29755 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
29756 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
29757 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
29758 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41  Name(v, 2, COLNA
29759 4d 45 5f 4e 41 4d 45 2c 20 22 74 79 70 65 22 2c  ME_NAME, "type",
2975a 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2975b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2975c 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2975d 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  3, COLNAME_NAME,
2975e 20 22 6e 6f 74 6e 75 6c 6c 22 2c 20 53 51 4c 49   "notnull", SQLI
2975f 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
29760 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29761 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f  ColName(v, 4, CO
29762 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66 6c  LNAME_NAME, "dfl
29763 74 5f 76 61 6c 75 65 22 2c 20 53 51 4c 49 54 45  t_value", SQLITE
29764 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
29765 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29766 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e  lName(v, 5, COLN
29767 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c 20  AME_NAME, "pk", 
29768 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
29769 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 69 65        sqlite3Vie
2976a 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
2976b 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
2976c 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 43       for(i=0, pC
2976d 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69  ol=pTab->aCol; i
2976e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
2976f 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
29770 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e 43     if( IsHiddenC
29771 6f 6c 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a 20  olumn(pCol) ){. 
29772 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e           nHidden
29773 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f  ++;.          co
29774 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
29775 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
29776 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29777 50 5f 49 6e 74 65 67 65 72 2c 20 69 2d 6e 48 69  P_Integer, i-nHi
29778 64 64 65 6e 2c 20 31 29 3b 0a 20 20 20 20 20 20  dden, 1);.      
29779 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2977a 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
2977b 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c  8, 0, 2, 0, pCol
2977c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
2977d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2977e 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
2977f 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20  ing8, 0, 3, 0,. 
29780 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
29781 7a 54 79 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54  zType ? pCol->zT
29782 79 70 65 20 3a 20 22 22 2c 20 30 29 3b 0a 20 20  ype : "", 0);.  
29783 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29784 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
29785 74 65 67 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e 6f  teger, (pCol->no
29786 74 4e 75 6c 6c 20 3f 20 31 20 3a 20 30 29 2c 20  tNull ? 1 : 0), 
29787 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  4);.        if( 
29788 70 43 6f 6c 2d 3e 7a 44 66 6c 74 20 29 7b 0a 20  pCol->zDflt ){. 
29789 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2978a 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2978b 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 35 2c 20  _String8, 0, 5, 
2978c 30 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 2d 3e  0, (char*)pCol->
2978d 7a 44 66 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20  zDflt, 0);.     
2978e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2978f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29790 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
29791 2c 20 30 2c 20 35 29 3b 0a 20 20 20 20 20 20 20  , 0, 5);.       
29792 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
29793 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29794 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c  OP_Integer, pCol
29795 2d 3e 69 73 50 72 69 6d 4b 65 79 2c 20 36 29 3b  ->isPrimKey, 6);
29796 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29797 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29798 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 36  _ResultRow, 1, 6
29799 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2979a 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20  .  }else..  if( 
2979b 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2979c 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e 66  Left, "index_inf
2979d 6f 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  o")==0 && zRight
2979e 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   ){.    Index *p
2979f 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  Idx;.    Table *
297a0 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71  pTab;.    if( sq
297a1 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
297a2 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
297a3 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70  ragma_out;.    p
297a4 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Idx = sqlite3Fin
297a5 64 49 6e 64 65 78 28 64 62 2c 20 7a 52 69 67 68  dIndex(db, zRigh
297a6 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28  t, zDb);.    if(
297a7 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69   pIdx ){.      i
297a8 6e 74 20 69 3b 0a 20 20 20 20 20 20 70 54 61 62  nt i;.      pTab
297a9 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b   = pIdx->pTable;
297aa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
297ab 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
297ac 33 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  3);.      pParse
297ad 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
297ae 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
297af 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
297b0 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71  LNAME_NAME, "seq
297b1 6e 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  no", SQLITE_STAT
297b2 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
297b3 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
297b4 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 1, COLNAME_N
297b5 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49  AME, "cid", SQLI
297b6 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
297b7 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
297b8 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f  ColName(v, 2, CO
297b9 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
297ba 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
297bb 43 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  C);.      for(i=
297bc 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; i<pIdx->nColu
297bd 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
297be 20 20 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49 64    int cnum = pId
297bf 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a  x->aiColumn[i];.
297c0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
297c1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
297c2 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a  Integer, i, 1);.
297c3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
297c4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
297c5 49 6e 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20 32  Integer, cnum, 2
297c6 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
297c7 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63 6e  t( pTab->nCol>cn
297c8 75 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  um );.        sq
297c9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
297ca 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
297cb 2c 20 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61 43  , 3, 0, pTab->aC
297cc 6f 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c 20  ol[cnum].zName, 
297cd 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
297ce 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
297cf 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
297d0 2c 20 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 3);.      }.  
297d1 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69    }.  }else..  i
297d2 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
297d3 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f  p(zLeft, "index_
297d4 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69  list")==0 && zRi
297d5 67 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78  ght ){.    Index
297d6 20 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c   *pIdx;.    Tabl
297d7 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
297d8 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
297d9 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
297da 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
297db 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
297dc 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
297dd 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20  ight, zDb);.    
297de 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
297df 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
297e0 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
297e1 20 20 20 20 70 49 64 78 20 3d 20 70 54 61 62 2d      pIdx = pTab-
297e2 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 69  >pIndex;.      i
297e3 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
297e4 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20     int i = 0; . 
297e5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
297e6 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
297e7 33 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72  3);.        pPar
297e8 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20  se->nMem = 3;.  
297e9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
297ea 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
297eb 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
297ec 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54  "seq", SQLITE_ST
297ed 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
297ee 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
297ef 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
297f0 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c  ME_NAME, "name",
297f1 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
297f2 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
297f3 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
297f4 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 2, COLNAME_NAM
297f5 45 2c 20 22 75 6e 69 71 75 65 22 2c 20 53 51 4c  E, "unique", SQL
297f6 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
297f7 20 20 20 20 20 77 68 69 6c 65 28 70 49 64 78 29       while(pIdx)
297f8 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
297f9 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
297fa 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
297fb 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
297fc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
297fd 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
297fe 2c 20 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e  , 2, 0, pIdx->zN
297ff 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
29800 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29801 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
29802 65 72 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  er, pIdx->onErro
29803 72 21 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0a  r!=OE_None, 3);.
29804 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29805 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29806 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
29807 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b  3);.          ++
29808 69 3b 0a 20 20 20 20 20 20 20 20 20 20 70 49 64  i;.          pId
29809 78 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b  x = pIdx->pNext;
2980a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2980b 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
2980c 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
2980d 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64  trICmp(zLeft, "d
2980e 61 74 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d  atabase_list")==
2980f 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
29810 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
29811 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
29812 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
29813 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  out;.    sqlite3
29814 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
29815 2c 20 33 29 3b 0a 20 20 20 20 70 50 61 72 73 65  , 3);.    pParse
29816 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
29817 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29818 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
29819 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c  AME_NAME, "seq",
2981a 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2981b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2981c 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
2981d 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
2981e 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  name", SQLITE_ST
2981f 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  ATIC);.    sqlit
29820 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29821 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
29822 41 4d 45 2c 20 22 66 69 6c 65 22 2c 20 53 51 4c  AME, "file", SQL
29823 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29824 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
29825 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
29826 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
29827 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  pBt==0 ) continu
29828 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
29829 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
2982a 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  e!=0 );.      sq
2982b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2982c 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
2982d 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
2982e 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2982f 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
29830 32 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d  2, 0, db->aDb[i]
29831 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  .zName, 0);.    
29832 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29833 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
29834 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20  8, 0, 3, 0,.    
29835 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
29836 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64  reeGetFilename(d
29837 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c 20  b->aDb[i].pBt), 
29838 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
29839 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2983a 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
2983b 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  3);.    }.  }els
2983c 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  e..  if( sqlite3
2983d 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
2983e 63 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22 29  collation_list")
2983f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ==0 ){.    int i
29840 20 3d 20 30 3b 0a 20 20 20 20 48 61 73 68 45 6c   = 0;.    HashEl
29841 65 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74  em *p;.    sqlit
29842 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
29843 28 76 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72  (v, 2);.    pPar
29844 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20  se->nMem = 2;.  
29845 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29846 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
29847 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71  LNAME_NAME, "seq
29848 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
29849 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2984a 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2984b 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
2984c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
2984d 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72  STATIC);.    for
2984e 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (p=sqliteHashFir
2984f 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71  st(&db->aCollSeq
29850 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61  ); p; p=sqliteHa
29851 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20  shNext(p)){.    
29852 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
29853 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71   = (CollSeq *)sq
29854 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b  liteHashData(p);
29855 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29856 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
29857 6e 74 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29 3b  nteger, i++, 1);
29858 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29859 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
2985a 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c  tring8, 0, 2, 0,
2985b 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 30   pColl->zName, 0
2985c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2985d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2985e 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32  _ResultRow, 1, 2
2985f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
29860 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
29861 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52  E_OMIT_SCHEMA_PR
29862 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65  AGMAS */..#ifnde
29863 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
29864 52 45 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28 20  REIGN_KEY.  if( 
29865 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29866 4c 65 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f 6b  Left, "foreign_k
29867 65 79 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20  ey_list")==0 && 
29868 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 46 4b  zRight ){.    FK
29869 65 79 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61 62  ey *pFK;.    Tab
2986a 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66  le *pTab;.    if
2986b 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
2986c 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
2986d 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
2986e 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
2986f 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
29870 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20  Right, zDb);.   
29871 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
29872 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
29873 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
29874 20 20 20 20 20 70 46 4b 20 3d 20 70 54 61 62 2d       pFK = pTab-
29875 3e 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 69 66  >pFKey;.      if
29876 28 20 70 46 4b 20 29 7b 0a 20 20 20 20 20 20 20  ( pFK ){.       
29877 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20   int i = 0; .   
29878 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29879 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38 29  SetNumCols(v, 8)
2987a 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  ;.        pParse
2987b 2d 3e 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20 20  ->nMem = 8;.    
2987c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2987d 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
2987e 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69  COLNAME_NAME, "i
2987f 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  d", SQLITE_STATI
29880 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  C);.        sqli
29881 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
29882 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
29883 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c  NAME, "seq", SQL
29884 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29885 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29886 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c  SetColName(v, 2,
29887 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
29888 74 61 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53  table", SQLITE_S
29889 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
2988a 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2988b 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e  lName(v, 3, COLN
2988c 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22  AME_NAME, "from"
2988d 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2988e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2988f 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
29890 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 4, COLNAME_NA
29891 4d 45 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54 45  ME, "to", SQLITE
29892 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
29893 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29894 43 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f  ColName(v, 5, CO
29895 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f  LNAME_NAME, "on_
29896 75 70 64 61 74 65 22 2c 20 53 51 4c 49 54 45 5f  update", SQLITE_
29897 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
29898 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
29899 6f 6c 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f 4c  olName(v, 6, COL
2989a 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 64  NAME_NAME, "on_d
2989b 65 6c 65 74 65 22 2c 20 53 51 4c 49 54 45 5f 53  elete", SQLITE_S
2989c 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
2989d 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2989e 6c 4e 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c 4e  lName(v, 7, COLN
2989f 41 4d 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63 68  AME_NAME, "match
298a0 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
298a1 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  );.        while
298a2 28 70 46 4b 29 7b 0a 20 20 20 20 20 20 20 20 20  (pFK){.         
298a3 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20   int j;.        
298a4 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b    for(j=0; j<pFK
298a5 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
298a6 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
298a7 7a 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f 6c  zCol = pFK->aCol
298a8 5b 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20 20  [j].zCol;.      
298a9 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e 44        char *zOnD
298aa 65 6c 65 74 65 20 3d 20 28 63 68 61 72 20 2a 29  elete = (char *)
298ab 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e  actionName(pFK->
298ac 61 41 63 74 69 6f 6e 5b 30 5d 29 3b 0a 20 20 20  aAction[0]);.   
298ad 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
298ae 4f 6e 55 70 64 61 74 65 20 3d 20 28 63 68 61 72  OnUpdate = (char
298af 20 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46   *)actionName(pF
298b0 4b 2d 3e 61 41 63 74 69 6f 6e 5b 31 5d 29 3b 0a  K->aAction[1]);.
298b1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
298b2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
298b3 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
298b4 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
298b5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
298b6 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
298b7 20 6a 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20   j, 2);.        
298b8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
298b9 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
298ba 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 46  ng8, 0, 3, 0, pF
298bb 4b 2d 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20 20  K->zTo, 0);.    
298bc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
298bd 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
298be 53 74 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20 30  String8, 0, 4, 0
298bf 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
298c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
298c1 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d 3e  pTab->aCol[pFK->
298c2 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a  aCol[j].iFrom].z
298c3 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
298c4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
298c5 65 41 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c 20  eAddOp4(v, zCol 
298c6 3f 20 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20 4f  ? OP_String8 : O
298c7 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30 2c  P_Null, 0, 5, 0,
298c8 20 7a 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20   zCol, 0);.     
298c9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
298ca 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
298cb 74 72 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30 2c  tring8, 0, 6, 0,
298cc 20 7a 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b 0a   zOnUpdate, 0);.
298cd 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
298ce 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
298cf 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
298d0 37 2c 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65 2c  7, 0, zOnDelete,
298d1 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
298d2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
298d3 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
298d4 2c 20 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e 45  , 0, 8, 0, "NONE
298d5 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  ", 0);.         
298d6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
298d7 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
298d8 74 52 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20 20  tRow, 1, 8);.   
298d9 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
298da 20 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20     ++i;.        
298db 20 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e 65    pFK = pFK->pNe
298dc 78 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20  xtFrom;.        
298dd 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
298de 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
298df 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
298e0 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
298e1 45 59 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  EY) */..#ifndef 
298e2 4e 44 45 42 55 47 0a 20 20 69 66 28 20 73 71 6c  NDEBUG.  if( sql
298e3 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
298e4 74 2c 20 22 70 61 72 73 65 72 5f 74 72 61 63 65  t, "parser_trace
298e5 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
298e6 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20   zRight ){.     
298e7 20 69 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28   if( getBoolean(
298e8 7a 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  zRight) ){.     
298e9 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
298ea 54 72 61 63 65 28 73 74 64 65 72 72 2c 20 22 70  Trace(stderr, "p
298eb 61 72 73 65 72 3a 20 22 29 3b 0a 20 20 20 20 20  arser: ");.     
298ec 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
298ed 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72 61  sqlite3ParserTra
298ee 63 65 28 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  ce(0, 0);.      
298ef 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
298f0 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 69  #endif..  /* Rei
298f1 6e 73 74 61 6c 6c 20 74 68 65 20 4c 49 4b 45 20  nstall the LIKE 
298f2 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f  and GLOB functio
298f3 6e 73 2e 20 20 54 68 65 20 76 61 72 69 61 6e 74  ns.  The variant
298f4 20 6f 66 20 4c 49 4b 45 0a 20 20 2a 2a 20 75 73   of LIKE.  ** us
298f5 65 64 20 77 69 6c 6c 20 62 65 20 63 61 73 65 20  ed will be case 
298f6 73 65 6e 73 69 74 69 76 65 20 6f 72 20 6e 6f 74  sensitive or not
298f7 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
298f8 65 20 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 69 66  e RHS..  */.  if
298f9 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
298fa 28 7a 4c 65 66 74 2c 20 22 63 61 73 65 5f 73 65  (zLeft, "case_se
298fb 6e 73 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d 3d  nsitive_like")==
298fc 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 52 69  0 ){.    if( zRi
298fd 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ght ){.      sql
298fe 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65  ite3RegisterLike
298ff 46 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 67 65  Functions(db, ge
29900 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29  tBoolean(zRight)
29901 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
29902 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
29903 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
29904 5f 45 52 52 4f 52 5f 4d 41 58 0a 23 20 64 65 66  _ERROR_MAX.# def
29905 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  ine SQLITE_INTEG
29906 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52  RITY_CHECK_ERROR
29907 5f 4d 41 58 20 31 30 30 0a 23 65 6e 64 69 66 0a  _MAX 100.#endif.
29908 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
29909 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
2990a 48 45 43 4b 0a 20 20 2f 2a 20 50 72 61 67 6d 61  HECK.  /* Pragma
2990b 20 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 20 69   "quick_check" i
2990c 73 20 61 6e 20 65 78 70 65 72 69 6d 65 6e 74 61  s an experimenta
2990d 6c 20 72 65 64 75 63 65 64 20 76 65 72 73 69 6f  l reduced versio
2990e 6e 20 6f 66 20 0a 20 20 2a 2a 20 69 6e 74 65 67  n of .  ** integ
2990f 72 69 74 79 5f 63 68 65 63 6b 20 64 65 73 69 67  rity_check desig
29910 6e 65 64 20 74 6f 20 64 65 74 65 63 74 20 6d 6f  ned to detect mo
29911 73 74 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  st database corr
29912 75 70 74 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68  uption.  ** with
29913 6f 75 74 20 6d 6f 73 74 20 6f 66 20 74 68 65 20  out most of the 
29914 6f 76 65 72 68 65 61 64 20 6f 66 20 61 20 66 75  overhead of a fu
29915 6c 6c 20 69 6e 74 65 67 72 69 74 79 2d 63 68 65  ll integrity-che
29916 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ck..  */.  if( s
29917 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
29918 65 66 74 2c 20 22 69 6e 74 65 67 72 69 74 79 5f  eft, "integrity_
29919 63 68 65 63 6b 22 29 3d 3d 30 0a 20 20 20 7c 7c  check")==0.   ||
2991a 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2991b 7a 4c 65 66 74 2c 20 22 71 75 69 63 6b 5f 63 68  zLeft, "quick_ch
2991c 65 63 6b 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20  eck")==0 .  ){. 
2991d 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 61 64 64     int i, j, add
2991e 72 2c 20 6d 78 45 72 72 3b 0a 0a 20 20 20 20 2f  r, mxErr;..    /
2991f 2a 20 43 6f 64 65 20 74 68 61 74 20 61 70 70 65  * Code that appe
29920 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ars at the end o
29921 66 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20  f the integrity 
29922 63 68 65 63 6b 2e 20 20 49 66 20 6e 6f 20 65 72  check.  If no er
29923 72 6f 72 0a 20 20 20 20 2a 2a 20 6d 65 73 73 61  ror.    ** messa
29924 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 67 65  ges have been ge
29925 6e 65 72 61 74 65 64 2c 20 6f 75 74 70 75 74 20  nerated, output 
29926 4f 4b 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6f  OK.  Otherwise o
29927 75 74 70 75 74 20 74 68 65 0a 20 20 20 20 2a 2a  utput the.    **
29928 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20   error message. 
29929 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63     */.    static
2992a 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73   const VdbeOpLis
2992b 74 20 65 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b 0a  t endCode[] = {.
2992c 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d        { OP_AddIm
2992d 6d 2c 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20  m,      1, 0,   
2992e 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30       0},    /* 0
2992f 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49   */.      { OP_I
29930 66 4e 65 67 2c 20 20 20 20 20 20 20 31 2c 20 30  fNeg,       1, 0
29931 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
29932 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20  /* 1 */.      { 
29933 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
29934 30 2c 20 33 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 3,        0},
29935 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20      /* 2 */.    
29936 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77    { OP_ResultRow
29937 2c 20 20 20 33 2c 20 31 2c 20 20 20 20 20 20 20  ,   3, 1,       
29938 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 0a 20 20 20   0},.    };..   
29939 20 69 6e 74 20 69 73 51 75 69 63 6b 20 3d 20 28   int isQuick = (
2993a 7a 4c 65 66 74 5b 30 5d 3d 3d 27 71 27 29 3b 0a  zLeft[0]=='q');.
2993b 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
2993c 7a 65 20 74 68 65 20 56 44 42 45 20 70 72 6f 67  ze the VDBE prog
2993d 72 61 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20 73  ram */.    if( s
2993e 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
2993f 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
29940 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
29941 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36  pParse->nMem = 6
29942 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29943 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
29944 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
29945 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
29946 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
29947 20 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63   "integrity_chec
29948 6b 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  k", SQLITE_STATI
29949 43 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20  C);..    /* Set 
2994a 74 68 65 20 6d 61 78 69 6d 75 6d 20 65 72 72 6f  the maximum erro
2994b 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 6d  r count */.    m
2994c 78 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e  xErr = SQLITE_IN
2994d 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52  TEGRITY_CHECK_ER
2994e 52 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66 28  ROR_MAX;.    if(
2994f 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20   zRight ){.     
29950 20 6d 78 45 72 72 20 3d 20 61 74 6f 69 28 7a 52   mxErr = atoi(zR
29951 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28  ight);.      if(
29952 20 6d 78 45 72 72 3c 3d 30 20 29 7b 0a 20 20 20   mxErr<=0 ){.   
29953 20 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c       mxErr = SQL
29954 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  ITE_INTEGRITY_CH
29955 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20  ECK_ERROR_MAX;. 
29956 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
29957 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29958 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
29959 2c 20 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f 2a  , mxErr, 1);  /*
2995a 20 72 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65 72   reg[1] holds er
2995b 72 6f 72 73 20 6c 65 66 74 20 2a 2f 0a 0a 20 20  rors left */..  
2995c 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67    /* Do an integ
2995d 72 69 74 79 20 63 68 65 63 6b 20 6f 6e 20 65 61  rity check on ea
2995e 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ch database file
2995f 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
29960 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
29961 7b 0a 20 20 20 20 20 20 48 61 73 68 45 6c 65 6d  {.      HashElem
29962 20 2a 78 3b 0a 20 20 20 20 20 20 48 61 73 68 20   *x;.      Hash 
29963 2a 70 54 62 6c 73 3b 0a 20 20 20 20 20 20 69 6e  *pTbls;.      in
29964 74 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20  t cnt = 0;..    
29965 20 20 69 66 28 20 4f 4d 49 54 5f 54 45 4d 50 44    if( OMIT_TEMPD
29966 42 20 26 26 20 69 3d 3d 31 20 29 20 63 6f 6e 74  B && i==1 ) cont
29967 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 73 71 6c  inue;..      sql
29968 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
29969 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b  hema(pParse, i);
2996a 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
2996b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2996c 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b  v, OP_IfPos, 1);
2996d 20 2f 2a 20 48 61 6c 74 20 69 66 20 6f 75 74 20   /* Halt if out 
2996e 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  of errors */.   
2996f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29970 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  dOp2(v, OP_Halt,
29971 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71   0, 0);.      sq
29972 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
29973 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20  e(v, addr);..   
29974 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65     /* Do an inte
29975 67 72 69 74 79 20 63 68 65 63 6b 20 6f 66 20 74  grity check of t
29976 68 65 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20  he B-Tree.      
29977 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 42 65 67 69  **.      ** Begi
29978 6e 20 62 79 20 66 69 6c 6c 69 6e 67 20 72 65 67  n by filling reg
29979 69 73 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e 2e  isters 2, 3, ...
2997a 20 77 69 74 68 20 74 68 65 20 72 6f 6f 74 20 70   with the root p
2997b 61 67 65 73 20 6e 75 6d 62 65 72 73 0a 20 20 20  ages numbers.   
2997c 20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61     ** for all ta
2997d 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73  bles and indices
2997e 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
2997f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
29980 20 70 54 62 6c 73 20 3d 20 26 64 62 2d 3e 61 44   pTbls = &db->aD
29981 62 5b 69 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62  b[i].pSchema->tb
29982 6c 48 61 73 68 3b 0a 20 20 20 20 20 20 66 6f 72  lHash;.      for
29983 28 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (x=sqliteHashFir
29984 73 74 28 70 54 62 6c 73 29 3b 20 78 3b 20 78 3d  st(pTbls); x; x=
29985 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78  sqliteHashNext(x
29986 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c  )){.        Tabl
29987 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65  e *pTab = sqlite
29988 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20  HashData(x);.   
29989 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
2998a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2998b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2998c 50 5f 49 6e 74 65 67 65 72 2c 20 70 54 61 62 2d  P_Integer, pTab-
2998d 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20  >tnum, 2+cnt);. 
2998e 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20         cnt++;.  
2998f 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70        for(pIdx=p
29990 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
29991 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
29992 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ext){.          
29993 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29994 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
29995 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b 63   pIdx->tnum, 2+c
29996 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63  nt);.          c
29997 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nt++;.        }.
29998 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
29999 2a 20 4d 61 6b 65 20 73 75 72 65 20 73 75 66 66  * Make sure suff
2999a 69 63 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  icient number of
2999b 20 72 65 67 69 73 74 65 72 73 20 68 61 76 65 20   registers have 
2999c 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a  been allocated *
2999d 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72  /.      if( pPar
2999e 73 65 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74 2b 34  se->nMem < cnt+4
2999f 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72   ){.        pPar
299a0 73 65 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74 2b 34  se->nMem = cnt+4
299a1 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
299a2 20 2f 2a 20 44 6f 20 74 68 65 20 62 2d 74 72 65   /* Do the b-tre
299a3 65 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63  e integrity chec
299a4 6b 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ks */.      sqli
299a5 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
299a6 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 2c   OP_IntegrityCk,
299a7 20 32 2c 20 63 6e 74 2c 20 31 29 3b 0a 20 20 20   2, cnt, 1);.   
299a8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
299a9 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 69 29  angeP5(v, (u8)i)
299aa 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
299ab 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
299ac 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32  (v, OP_IsNull, 2
299ad 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
299ae 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
299af 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20  _String8, 0, 3, 
299b0 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  0,.         sqli
299b1 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
299b2 2a 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20  *** in database 
299b3 25 73 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61  %s ***\n", db->a
299b4 44 62 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20  Db[i].zName),.  
299b5 20 20 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49         P4_DYNAMI
299b6 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
299b7 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
299b8 50 5f 4d 6f 76 65 2c 20 32 2c 20 34 2c 20 31 29  P_Move, 2, 4, 1)
299b9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
299ba 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
299bb 43 6f 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32 29  Concat, 4, 3, 2)
299bc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
299bd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
299be 52 65 73 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29  ResultRow, 2, 1)
299bf 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
299c0 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
299c1 64 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ddr);..      /* 
299c2 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68  Make sure all th
299c3 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 63 6f  e indices are co
299c4 6e 73 74 72 75 63 74 65 64 20 63 6f 72 72 65 63  nstructed correc
299c5 74 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  tly..      */.  
299c6 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65      for(x=sqlite
299c7 48 61 73 68 46 69 72 73 74 28 70 54 62 6c 73 29  HashFirst(pTbls)
299c8 3b 20 78 20 26 26 20 21 69 73 51 75 69 63 6b 3b  ; x && !isQuick;
299c9 20 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   x=sqliteHashNex
299ca 74 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54  t(x)){.        T
299cb 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c  able *pTab = sql
299cc 69 74 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a  iteHashData(x);.
299cd 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70          Index *p
299ce 49 64 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  Idx;.        int
299cf 20 6c 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20   loopTop;..     
299d0 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e     if( pTab->pIn
299d1 64 65 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  dex==0 ) continu
299d2 65 3b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20  e;.        addr 
299d3 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
299d4 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op1(v, OP_IfPos,
299d5 20 31 29 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66   1);  /* Stop if
299d6 20 6f 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a   out of errors *
299d7 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
299d8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
299d9 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20  P_Halt, 0, 0);. 
299da 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
299db 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
299dc 64 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  dr);.        sql
299dd 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
299de 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20  Indices(pParse, 
299df 70 54 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e  pTab, 1, OP_Open
299e0 52 65 61 64 29 3b 0a 20 20 20 20 20 20 20 20 73  Read);.        s
299e1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
299e2 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
299e3 30 2c 20 32 29 3b 20 20 2f 2a 20 72 65 67 28 32  0, 2);  /* reg(2
299e4 29 20 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74  ) will count ent
299e5 72 69 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  ries */.        
299e6 6c 6f 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74 65  loopTop = sqlite
299e7 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
299e8 50 5f 52 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b  P_Rewind, 1, 0);
299e9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
299ea 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
299eb 5f 41 64 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20  _AddImm, 2, 1); 
299ec 20 20 2f 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65    /* increment e
299ed 6e 74 72 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20  ntry count */.  
299ee 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70        for(j=0, p
299ef 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
299f0 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
299f1 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a  x->pNext, j++){.
299f2 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6d            int jm
299f3 70 32 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  p2;.          in
299f4 74 20 72 31 3b 0a 20 20 20 20 20 20 20 20 20 20  t r1;.          
299f5 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
299f6 65 4f 70 4c 69 73 74 20 69 64 78 45 72 72 5b 5d  eOpList idxErr[]
299f7 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20   = {.           
299f8 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20   { OP_AddImm,   
299f9 20 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20     1, -1,  0},. 
299fa 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
299fb 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20  String8,     0, 
299fc 20 33 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31   3,  0},    /* 1
299fd 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
299fe 7b 20 4f 50 5f 52 6f 77 69 64 2c 20 20 20 20 20  { OP_Rowid,     
299ff 20 20 31 2c 20 20 34 2c 20 20 30 7d 2c 0a 20 20    1,  4,  0},.  
29a00 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
29a01 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20  tring8,     0,  
29a02 35 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20  5,  0},    /* 3 
29a03 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
29a04 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
29a05 20 30 2c 20 20 36 2c 20 20 30 7d 2c 20 20 20 20   0,  6,  0},    
29a06 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 4 */.        
29a07 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c      { OP_Concat,
29a08 20 20 20 20 20 20 34 2c 20 20 33 2c 20 20 33 7d        4,  3,  3}
29a09 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
29a0a 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20  OP_Concat,      
29a0b 35 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20  5,  3,  3},.    
29a0c 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e          { OP_Con
29a0d 63 61 74 2c 20 20 20 20 20 20 36 2c 20 20 33 2c  cat,      6,  3,
29a0e 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    3},.          
29a0f 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77    { OP_ResultRow
29a10 2c 20 20 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0a  ,   3,  1,  0},.
29a11 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29a12 5f 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c  _IfPos,       1,
29a13 20 20 30 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20    0,  0},    /* 
29a14 39 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  9 */.           
29a15 20 7b 20 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20   { OP_Halt,     
29a16 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 0a 20     0,  0,  0},. 
29a17 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20           };.    
29a18 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
29a19 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b  e3GenerateIndexK
29a1a 65 79 28 70 50 61 72 73 65 2c 20 70 49 64 78 2c  ey(pParse, pIdx,
29a1b 20 31 2c 20 33 2c 20 30 29 3b 0a 20 20 20 20 20   1, 3, 0);.     
29a1c 20 20 20 20 20 6a 6d 70 32 20 3d 20 73 71 6c 69       jmp2 = sqli
29a1d 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74  te3VdbeAddOp4Int
29a1e 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 6a 2b  (v, OP_Found, j+
29a1f 32 2c 20 30 2c 20 72 31 2c 20 70 49 64 78 2d 3e  2, 0, r1, pIdx->
29a20 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20  nColumn+1);.    
29a21 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
29a22 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
29a23 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 69  t(v, ArraySize(i
29a24 64 78 45 72 72 29 2c 20 69 64 78 45 72 72 29 3b  dxErr), idxErr);
29a25 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
29a26 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
29a27 2c 20 61 64 64 72 2b 31 2c 20 22 72 6f 77 69 64  , addr+1, "rowid
29a28 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a   ", P4_STATIC);.
29a29 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29a2a 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
29a2b 20 61 64 64 72 2b 33 2c 20 22 20 6d 69 73 73 69   addr+3, " missi
29a2c 6e 67 20 66 72 6f 6d 20 69 6e 64 65 78 20 22 2c  ng from index ",
29a2d 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
29a2e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29a2f 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64  beChangeP4(v, ad
29a30 64 72 2b 34 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  dr+4, pIdx->zNam
29a31 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
29a32 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29a33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
29a34 61 64 64 72 2b 39 29 3b 0a 20 20 20 20 20 20 20  addr+9);.       
29a35 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
29a36 6d 70 48 65 72 65 28 76 2c 20 6a 6d 70 32 29 3b  mpHere(v, jmp2);
29a37 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29a38 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29a39 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
29a3a 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29 3b 0a   1, loopTop+1);.
29a3b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29a3c 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6c  dbeJumpHere(v, l
29a3d 6f 6f 70 54 6f 70 29 3b 0a 20 20 20 20 20 20 20  oopTop);.       
29a3e 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70   for(j=0, pIdx=p
29a3f 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
29a40 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
29a41 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  ext, j++){.     
29a42 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
29a43 74 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6e 74  t VdbeOpList cnt
29a44 49 64 78 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  Idx[] = {.      
29a45 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65         { OP_Inte
29a46 67 65 72 2c 20 20 20 20 20 20 30 2c 20 20 33 2c  ger,      0,  3,
29a47 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    0},.          
29a48 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20     { OP_Rewind, 
29a49 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d        0,  0,  0}
29a4a 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20  ,  /* 1 */.     
29a4b 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64          { OP_Add
29a4c 49 6d 6d 2c 20 20 20 20 20 20 20 33 2c 20 20 31  Imm,       3,  1
29a4d 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  0},.         
29a4e 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20      { OP_Next,  
29a4f 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30         0,  0,  0
29a50 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20  },  /* 3 */.    
29a51 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 45 71           { OP_Eq
29a52 2c 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 20  ,           2,  
29a53 30 2c 20 20 33 7d 2c 20 20 2f 2a 20 34 20 2a 2f  0,  3},  /* 4 */
29a54 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
29a55 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20  OP_AddImm,      
29a56 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20   1, -1,  0},.   
29a57 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
29a58 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c 20  tring8,      0, 
29a59 20 32 2c 20 20 30 7d 2c 20 20 2f 2a 20 36 20 2a   2,  0},  /* 6 *
29a5a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  /.             {
29a5b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
29a5c 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20 20 2f    0,  3,  0},  /
29a5d 2a 20 37 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 7 */.         
29a5e 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c      { OP_Concat,
29a5f 20 20 20 20 20 20 20 33 2c 20 20 32 2c 20 20 32         3,  2,  2
29a60 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  },.             
29a61 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  { OP_ResultRow, 
29a62 20 20 20 32 2c 20 20 31 2c 20 20 30 7d 2c 0a 20     2,  1,  0},. 
29a63 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20           };.    
29a64 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
29a65 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
29a66 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 0a  , OP_IfPos, 1);.
29a67 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29a68 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29a69 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20  P_Halt, 0, 0);. 
29a6a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29a6b 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
29a6c 61 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20  addr);.         
29a6d 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
29a6e 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20  dbeAddOpList(v, 
29a6f 41 72 72 61 79 53 69 7a 65 28 63 6e 74 49 64 78  ArraySize(cntIdx
29a70 29 2c 20 63 6e 74 49 64 78 29 3b 0a 20 20 20 20  ), cntIdx);.    
29a71 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29a72 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
29a73 72 2b 31 2c 20 6a 2b 32 29 3b 0a 20 20 20 20 20  r+1, j+2);.     
29a74 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29a75 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72  ChangeP2(v, addr
29a76 2b 31 2c 20 61 64 64 72 2b 34 29 3b 0a 20 20 20  +1, addr+4);.   
29a77 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29a78 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
29a79 64 72 2b 33 2c 20 6a 2b 32 29 3b 0a 20 20 20 20  dr+3, j+2);.    
29a7a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29a7b 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64  eChangeP2(v, add
29a7c 72 2b 33 2c 20 61 64 64 72 2b 32 29 3b 0a 20 20  r+3, addr+2);.  
29a7d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29a7e 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
29a7f 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20  ddr+4);.        
29a80 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29a81 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 36 2c  ngeP4(v, addr+6,
29a82 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
29a83 20 20 20 20 20 20 20 22 77 72 6f 6e 67 20 23 20         "wrong # 
29a84 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 69 6e  of entries in in
29a85 64 65 78 20 22 2c 20 50 34 5f 53 54 41 54 49 43  dex ", P4_STATIC
29a86 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
29a87 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
29a88 28 76 2c 20 61 64 64 72 2b 37 2c 20 70 49 64 78  (v, addr+7, pIdx
29a89 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ->zName, P4_STAT
29a8a 49 43 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  IC);.        }. 
29a8b 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 20 20       } .    }.  
29a8c 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
29a8d 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c  VdbeAddOpList(v,
29a8e 20 41 72 72 61 79 53 69 7a 65 28 65 6e 64 43 6f   ArraySize(endCo
29a8f 64 65 29 2c 20 65 6e 64 43 6f 64 65 29 3b 0a 20  de), endCode);. 
29a90 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
29a91 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2c 20  angeP2(v, addr, 
29a92 2d 6d 78 45 72 72 29 3b 0a 20 20 20 20 73 71 6c  -mxErr);.    sql
29a93 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
29a94 28 76 2c 20 61 64 64 72 2b 31 29 3b 0a 20 20 20  (v, addr+1);.   
29a95 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
29a96 67 65 50 34 28 76 2c 20 61 64 64 72 2b 32 2c 20  geP4(v, addr+2, 
29a97 22 6f 6b 22 2c 20 50 34 5f 53 54 41 54 49 43 29  "ok", P4_STATIC)
29a98 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  ;.  }else.#endif
29a99 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
29a9a 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20  INTEGRITY_CHECK 
29a9b 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
29a9c 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20  TE_OMIT_UTF16.  
29a9d 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  /*.  **   PRAGMA
29a9e 20 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 20   encoding.  **  
29a9f 20 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67   PRAGMA encoding
29aa0 20 3d 20 22 75 74 66 2d 38 22 7c 22 75 74 66 2d   = "utf-8"|"utf-
29aa1 31 36 22 7c 22 75 74 66 2d 31 36 6c 65 22 7c 22  16"|"utf-16le"|"
29aa2 75 74 66 2d 31 36 62 65 22 0a 20 20 2a 2a 0a 20  utf-16be".  **. 
29aa3 20 2a 2a 20 49 6e 20 69 74 73 20 66 69 72 73 74   ** In its first
29aa4 20 66 6f 72 6d 2c 20 74 68 69 73 20 70 72 61 67   form, this prag
29aa5 6d 61 20 72 65 74 75 72 6e 73 20 74 68 65 20 65  ma returns the e
29aa6 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 6d  ncoding of the m
29aa7 61 69 6e 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  ain.  ** databas
29aa8 65 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61  e. If the databa
29aa9 73 65 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61  se is not initia
29aaa 6c 69 7a 65 64 2c 20 69 74 20 69 73 20 69 6e 69  lized, it is ini
29aab 74 69 61 6c 69 7a 65 64 20 6e 6f 77 2e 0a 20 20  tialized now..  
29aac 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  **.  ** The seco
29aad 6e 64 20 66 6f 72 6d 20 6f 66 20 74 68 69 73 20  nd form of this 
29aae 70 72 61 67 6d 61 20 69 73 20 61 20 6e 6f 2d 6f  pragma is a no-o
29aaf 70 20 69 66 20 74 68 65 20 6d 61 69 6e 20 64 61  p if the main da
29ab0 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a  tabase file.  **
29ab1 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79   has not already
29ab2 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
29ab3 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
29ab4 69 74 20 73 65 74 73 20 74 68 65 20 64 65 66 61  it sets the defa
29ab5 75 6c 74 0a 20 20 2a 2a 20 65 6e 63 6f 64 69 6e  ult.  ** encodin
29ab6 67 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75  g that will be u
29ab7 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  sed for the main
29ab8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
29ab9 66 20 61 20 6e 65 77 20 66 69 6c 65 0a 20 20 2a  f a new file.  *
29aba 2a 20 69 73 20 63 72 65 61 74 65 64 2e 20 49 66  * is created. If
29abb 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 61 69   an existing mai
29abc 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
29abd 69 73 20 6f 70 65 6e 65 64 2c 20 74 68 65 6e 20  is opened, then 
29abe 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75 6c 74  the.  ** default
29abf 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66   text encoding f
29ac0 6f 72 20 74 68 65 20 65 78 69 73 74 69 6e 67 20  or the existing 
29ac1 64 61 74 61 62 61 73 65 20 69 73 20 75 73 65 64  database is used
29ac2 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 49 6e 20  ..  ** .  ** In 
29ac3 61 6c 6c 20 63 61 73 65 73 20 6e 65 77 20 64 61  all cases new da
29ac4 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20  tabases created 
29ac5 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48  using the ATTACH
29ac6 20 63 6f 6d 6d 61 6e 64 20 61 72 65 0a 20 20 2a   command are.  *
29ac7 2a 20 63 72 65 61 74 65 64 20 74 6f 20 75 73 65  * created to use
29ac8 20 74 68 65 20 73 61 6d 65 20 64 65 66 61 75 6c   the same defaul
29ac9 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  t text encoding 
29aca 61 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  as the main data
29acb 62 61 73 65 2e 20 49 66 0a 20 20 2a 2a 20 74 68  base. If.  ** th
29acc 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
29acd 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69  has not been ini
29ace 74 69 61 6c 69 7a 65 64 20 61 6e 64 2f 6f 72 20  tialized and/or 
29acf 63 72 65 61 74 65 64 20 77 68 65 6e 20 41 54 54  created when ATT
29ad0 41 43 48 0a 20 20 2a 2a 20 69 73 20 65 78 65 63  ACH.  ** is exec
29ad1 75 74 65 64 2c 20 74 68 69 73 20 69 73 20 64 6f  uted, this is do
29ad2 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 41 54  ne before the AT
29ad3 54 41 43 48 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  TACH operation..
29ad4 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 74 68 65    **.  ** In the
29ad5 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 74 68 69   second form thi
29ad6 73 20 70 72 61 67 6d 61 20 73 65 74 73 20 74 68  s pragma sets th
29ad7 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  e text encoding 
29ad8 74 6f 20 62 65 20 75 73 65 64 20 69 6e 0a 20 20  to be used in.  
29ad9 2a 2a 20 6e 65 77 20 64 61 74 61 62 61 73 65 20  ** new database 
29ada 66 69 6c 65 73 20 63 72 65 61 74 65 64 20 75 73  files created us
29adb 69 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73  ing this databas
29adc 65 20 68 61 6e 64 6c 65 2e 20 49 74 20 69 73 20  e handle. It is 
29add 6f 6e 6c 79 0a 20 20 2a 2a 20 75 73 65 66 75 6c  only.  ** useful
29ade 20 69 66 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65   if invoked imme
29adf 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 68  diately after th
29ae0 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
29ae1 69 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  i.  */.  if( sql
29ae2 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29ae3 74 2c 20 22 65 6e 63 6f 64 69 6e 67 22 29 3d 3d  t, "encoding")==
29ae4 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20  0 ){.    static 
29ae5 63 6f 6e 73 74 20 73 74 72 75 63 74 20 45 6e 63  const struct Enc
29ae6 4e 61 6d 65 20 7b 0a 20 20 20 20 20 20 63 68 61  Name {.      cha
29ae7 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  r *zName;.      
29ae8 75 38 20 65 6e 63 3b 0a 20 20 20 20 7d 20 65 6e  u8 enc;.    } en
29ae9 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 0a 20 20 20  cnames[] = {.   
29aea 20 20 20 7b 20 22 55 54 46 38 22 2c 20 20 20 20     { "UTF8",    
29aeb 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20 20   SQLITE_UTF8    
29aec 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22      },.      { "
29aed 55 54 46 2d 38 22 2c 20 20 20 20 53 51 4c 49 54  UTF-8",    SQLIT
29aee 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 7d 2c  E_UTF8        },
29aef 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c 65    /* Must be ele
29af0 6d 65 6e 74 20 5b 31 5d 20 2a 2f 0a 20 20 20 20  ment [1] */.    
29af1 20 20 7b 20 22 55 54 46 2d 31 36 6c 65 22 2c 20    { "UTF-16le", 
29af2 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20  SQLITE_UTF16LE  
29af3 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62     },  /* Must b
29af4 65 20 65 6c 65 6d 65 6e 74 20 5b 32 5d 20 2a 2f  e element [2] */
29af5 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36  .      { "UTF-16
29af6 62 65 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  be", SQLITE_UTF1
29af7 36 42 45 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d  6BE     },  /* M
29af8 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b  ust be element [
29af9 33 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55  3] */.      { "U
29afa 54 46 31 36 6c 65 22 2c 20 20 53 51 4c 49 54 45  TF16le",  SQLITE
29afb 5f 55 54 46 31 36 4c 45 20 20 20 20 20 7d 2c 0a  _UTF16LE     },.
29afc 20 20 20 20 20 20 7b 20 22 55 54 46 31 36 62 65        { "UTF16be
29afd 22 2c 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ",  SQLITE_UTF16
29afe 42 45 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20  BE     },.      
29aff 7b 20 22 55 54 46 2d 31 36 22 2c 20 20 20 30 20  { "UTF-16",   0 
29b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b01 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54   }, /* SQLITE_UT
29b02 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a 20 20 20  F16NATIVE */.   
29b03 20 20 20 7b 20 22 55 54 46 31 36 22 2c 20 20 20     { "UTF16",   
29b04 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
29b05 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45      }, /* SQLITE
29b06 5f 55 54 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a  _UTF16NATIVE */.
29b07 20 20 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20        { 0, 0 }. 
29b08 20 20 20 7d 3b 0a 20 20 20 20 63 6f 6e 73 74 20     };.    const 
29b09 73 74 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 2a  struct EncName *
29b0a 70 45 6e 63 3b 0a 20 20 20 20 69 66 28 20 21 7a  pEnc;.    if( !z
29b0b 52 69 67 68 74 20 29 7b 20 20 20 20 2f 2a 20 22  Right ){    /* "
29b0c 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 22  PRAGMA encoding"
29b0d 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71   */.      if( sq
29b0e 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
29b0f 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
29b10 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20  ragma_out;.     
29b11 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
29b12 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
29b13 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
29b14 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
29b15 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 65  COLNAME_NAME, "e
29b16 6e 63 6f 64 69 6e 67 22 2c 20 53 51 4c 49 54 45  ncoding", SQLITE
29b17 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
29b18 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29b19 32 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  2(v, OP_String8,
29b1a 20 30 2c 20 31 29 3b 0a 20 20 20 20 20 20 61 73   0, 1);.      as
29b1b 73 65 72 74 28 20 65 6e 63 6e 61 6d 65 73 5b 53  sert( encnames[S
29b1c 51 4c 49 54 45 5f 55 54 46 38 5d 2e 65 6e 63 3d  QLITE_UTF8].enc=
29b1d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a  =SQLITE_UTF8 );.
29b1e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 6e        assert( en
29b1f 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f 55 54  cnames[SQLITE_UT
29b20 46 31 36 4c 45 5d 2e 65 6e 63 3d 3d 53 51 4c 49  F16LE].enc==SQLI
29b21 54 45 5f 55 54 46 31 36 4c 45 20 29 3b 0a 20 20  TE_UTF16LE );.  
29b22 20 20 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e      assert( encn
29b23 61 6d 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 31  ames[SQLITE_UTF1
29b24 36 42 45 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54 45  6BE].enc==SQLITE
29b25 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20 20  _UTF16BE );.    
29b26 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29b27 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 65 6e 63  ngeP4(v, -1, enc
29b28 6e 61 6d 65 73 5b 45 4e 43 28 70 50 61 72 73 65  names[ENC(pParse
29b29 2d 3e 64 62 29 5d 2e 7a 4e 61 6d 65 2c 20 50 34  ->db)].zName, P4
29b2a 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
29b2b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29b2c 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
29b2d 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 7d 65  w, 1, 1);.    }e
29b2e 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20  lse{            
29b2f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 22              /* "
29b30 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20  PRAGMA encoding 
29b31 3d 20 58 58 58 22 20 2a 2f 0a 20 20 20 20 20 20  = XXX" */.      
29b32 2f 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 20 74  /* Only change t
29b33 68 65 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69  he value of sqli
29b34 74 65 2e 65 6e 63 20 69 66 20 74 68 65 20 64 61  te.enc if the da
29b35 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69 73  tabase handle is
29b36 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e   not.      ** in
29b37 69 74 69 61 6c 69 7a 65 64 2e 20 49 66 20 74 68  itialized. If th
29b38 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
29b39 65 78 69 73 74 73 2c 20 74 68 65 20 6e 65 77 20  exists, the new 
29b3a 73 71 6c 69 74 65 2e 65 6e 63 20 76 61 6c 75 65  sqlite.enc value
29b3b 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  .      ** will b
29b3c 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68  e overwritten wh
29b3d 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20 69 73  en the schema is
29b3e 20 6e 65 78 74 20 6c 6f 61 64 65 64 2e 20 49 66   next loaded. If
29b3f 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 20 20 20   it does not.   
29b40 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78     ** already ex
29b41 69 73 74 73 2c 20 69 74 20 77 69 6c 6c 20 62 65  ists, it will be
29b42 20 63 72 65 61 74 65 64 20 74 6f 20 75 73 65 20   created to use 
29b43 74 68 65 20 6e 65 77 20 65 6e 63 6f 64 69 6e 67  the new encoding
29b44 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f   value..      */
29b45 0a 20 20 20 20 20 20 69 66 28 20 0a 20 20 20 20  .      if( .    
29b46 20 20 20 20 21 28 44 62 48 61 73 50 72 6f 70 65      !(DbHasPrope
29b47 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f 53 63  rty(db, 0, DB_Sc
29b48 68 65 6d 61 4c 6f 61 64 65 64 29 29 20 7c 7c 20  hemaLoaded)) || 
29b49 0a 20 20 20 20 20 20 20 20 44 62 48 61 73 50 72  .        DbHasPr
29b4a 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44 42  operty(db, 0, DB
29b4b 5f 45 6d 70 74 79 29 20 0a 20 20 20 20 20 20 29  _Empty) .      )
29b4c 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 45  {.        for(pE
29b4d 6e 63 3d 26 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b  nc=&encnames[0];
29b4e 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45   pEnc->zName; pE
29b4f 6e 63 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  nc++){.         
29b50 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
29b51 74 72 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 70  trICmp(zRight, p
29b52 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0a 20  Enc->zName) ){. 
29b53 20 20 20 20 20 20 20 20 20 20 20 45 4e 43 28 70             ENC(p
29b54 50 61 72 73 65 2d 3e 64 62 29 20 3d 20 70 45 6e  Parse->db) = pEn
29b55 63 2d 3e 65 6e 63 20 3f 20 70 45 6e 63 2d 3e 65  c->enc ? pEnc->e
29b56 6e 63 20 3a 20 53 51 4c 49 54 45 5f 55 54 46 31  nc : SQLITE_UTF1
29b57 36 4e 41 54 49 56 45 3b 0a 20 20 20 20 20 20 20  6NATIVE;.       
29b58 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29b59 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
29b5a 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  }.        if( !p
29b5b 45 6e 63 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20  Enc->zName ){.  
29b5c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
29b5d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
29b5e 22 75 6e 73 75 70 70 6f 72 74 65 64 20 65 6e 63  "unsupported enc
29b5f 6f 64 69 6e 67 3a 20 25 73 22 2c 20 7a 52 69 67  oding: %s", zRig
29b60 68 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ht);.        }. 
29b61 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
29b62 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
29b63 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
29b64 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
29b65 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  ITE_OMIT_SCHEMA_
29b66 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 0a  VERSION_PRAGMAS.
29b67 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    /*.  **   PRAG
29b68 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 63  MA [database.]sc
29b69 68 65 6d 61 5f 76 65 72 73 69 6f 6e 0a 20 20 2a  hema_version.  *
29b6a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  *   PRAGMA [data
29b6b 62 61 73 65 2e 5d 73 63 68 65 6d 61 5f 76 65 72  base.]schema_ver
29b6c 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e  sion = <integer>
29b6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 50 52 41  .  **.  **   PRA
29b6e 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 75  GMA [database.]u
29b6f 73 65 72 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a  ser_version.  **
29b70 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
29b71 61 73 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f  ase.]user_versio
29b72 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20  n = <integer>.  
29b73 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 70 72 61 67  **.  ** The prag
29b74 6d 61 27 73 20 73 63 68 65 6d 61 5f 76 65 72 73  ma's schema_vers
29b75 69 6f 6e 20 61 6e 64 20 75 73 65 72 5f 76 65 72  ion and user_ver
29b76 73 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74 6f  sion are used to
29b77 20 73 65 74 20 6f 72 20 67 65 74 0a 20 20 2a 2a   set or get.  **
29b78 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
29b79 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e  e schema-version
29b7a 20 61 6e 64 20 75 73 65 72 2d 76 65 72 73 69 6f   and user-versio
29b7b 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
29b7c 20 42 6f 74 68 0a 20 20 2a 2a 20 74 68 65 20 73   Both.  ** the s
29b7d 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e  chema-version an
29b7e 64 20 74 68 65 20 75 73 65 72 2d 76 65 72 73 69  d the user-versi
29b7f 6f 6e 20 61 72 65 20 33 32 2d 62 69 74 20 73 69  on are 32-bit si
29b80 67 6e 65 64 20 69 6e 74 65 67 65 72 73 0a 20 20  gned integers.  
29b81 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ** stored in the
29b82 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72   database header
29b83 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
29b84 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 69 73  schema-cookie is
29b85 20 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6d 61   usually only ma
29b86 6e 69 70 75 6c 61 74 65 64 20 69 6e 74 65 72 6e  nipulated intern
29b87 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20  ally by SQLite. 
29b88 49 74 0a 20 20 2a 2a 20 69 73 20 69 6e 63 72 65  It.  ** is incre
29b89 6d 65 6e 74 65 64 20 62 79 20 53 51 4c 69 74 65  mented by SQLite
29b8a 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 64 61   whenever the da
29b8b 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73  tabase schema is
29b8c 20 6d 6f 64 69 66 69 65 64 20 28 62 79 0a 20 20   modified (by.  
29b8d 2a 2a 20 63 72 65 61 74 69 6e 67 20 6f 72 20 64  ** creating or d
29b8e 72 6f 70 70 69 6e 67 20 61 20 74 61 62 6c 65 20  ropping a table 
29b8f 6f 72 20 69 6e 64 65 78 29 2e 20 54 68 65 20 73  or index). The s
29b90 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 69 73  chema version is
29b91 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 53 51   used by.  ** SQ
29b92 4c 69 74 65 20 65 61 63 68 20 74 69 6d 65 20 61  Lite each time a
29b93 20 71 75 65 72 79 20 69 73 20 65 78 65 63 75 74   query is execut
29b94 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61  ed to ensure tha
29b95 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63  t the internal c
29b96 61 63 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65  ache.  ** of the
29b97 20 73 63 68 65 6d 61 20 75 73 65 64 20 77 68 65   schema used whe
29b98 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20  n compiling the 
29b99 53 51 4c 20 71 75 65 72 79 20 6d 61 74 63 68 65  SQL query matche
29b9a 73 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 0a  s the schema of.
29b9b 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73    ** the databas
29b9c 65 20 61 67 61 69 6e 73 74 20 77 68 69 63 68 20  e against which 
29b9d 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 65  the compiled que
29b9e 72 79 20 69 73 20 61 63 74 75 61 6c 6c 79 20 65  ry is actually e
29b9f 78 65 63 75 74 65 64 2e 0a 20 20 2a 2a 20 53 75  xecuted..  ** Su
29ba0 62 76 65 72 74 69 6e 67 20 74 68 69 73 20 6d 65  bverting this me
29ba1 63 68 61 6e 69 73 6d 20 62 79 20 75 73 69 6e 67  chanism by using
29ba2 20 22 50 52 41 47 4d 41 20 73 63 68 65 6d 61 5f   "PRAGMA schema_
29ba3 76 65 72 73 69 6f 6e 22 20 74 6f 20 6d 6f 64 69  version" to modi
29ba4 66 79 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65  fy.  ** the sche
29ba5 6d 61 2d 76 65 72 73 69 6f 6e 20 69 73 20 70 6f  ma-version is po
29ba6 74 65 6e 74 69 61 6c 6c 79 20 64 61 6e 67 65 72  tentially danger
29ba7 6f 75 73 20 61 6e 64 20 6d 61 79 20 6c 65 61 64  ous and may lead
29ba8 20 74 6f 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2a   to program.  **
29ba9 20 63 72 61 73 68 65 73 20 6f 72 20 64 61 74 61   crashes or data
29baa 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e  base corruption.
29bab 20 55 73 65 20 77 69 74 68 20 63 61 75 74 69 6f   Use with cautio
29bac 6e 21 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  n!.  **.  ** The
29bad 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 20 69 73   user-version is
29bae 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e   not used intern
29baf 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20  ally by SQLite. 
29bb0 49 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 62  It may be used b
29bb1 79 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61 74 69  y.  ** applicati
29bb2 6f 6e 73 20 66 6f 72 20 61 6e 79 20 70 75 72 70  ons for any purp
29bb3 6f 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ose..  */.  if( 
29bb4 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29bb5 4c 65 66 74 2c 20 22 73 63 68 65 6d 61 5f 76 65  Left, "schema_ve
29bb6 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c  rsion")==0 .   |
29bb7 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
29bb8 28 7a 4c 65 66 74 2c 20 22 75 73 65 72 5f 76 65  (zLeft, "user_ve
29bb9 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c  rsion")==0 .   |
29bba 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
29bbb 28 7a 4c 65 66 74 2c 20 22 66 72 65 65 6c 69 73  (zLeft, "freelis
29bbc 74 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 0a 20 20  t_count")==0 .  
29bbd 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b  ){.    int iCook
29bbe 69 65 3b 20 20 20 2f 2a 20 43 6f 6f 6b 69 65 20  ie;   /* Cookie 
29bbf 69 6e 64 65 78 2e 20 31 20 66 6f 72 20 73 63 68  index. 1 for sch
29bc0 65 6d 61 2d 63 6f 6f 6b 69 65 2c 20 36 20 66 6f  ema-cookie, 6 fo
29bc1 72 20 75 73 65 72 2d 63 6f 6f 6b 69 65 2e 20 2a  r user-cookie. *
29bc2 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
29bc3 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
29bc4 62 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20  b);.    switch( 
29bc5 7a 4c 65 66 74 5b 30 5d 20 29 7b 0a 20 20 20 20  zLeft[0] ){.    
29bc6 20 20 63 61 73 65 20 27 66 27 3a 20 63 61 73 65    case 'f': case
29bc7 20 27 46 27 3a 0a 20 20 20 20 20 20 20 20 69 43   'F':.        iC
29bc8 6f 6f 6b 69 65 20 3d 20 42 54 52 45 45 5f 46 52  ookie = BTREE_FR
29bc9 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20  EE_PAGE_COUNT;. 
29bca 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
29bcb 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 63 61      case 's': ca
29bcc 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20  se 'S':.        
29bcd 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52 45 45 5f  iCookie = BTREE_
29bce 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 3b 0a  SCHEMA_VERSION;.
29bcf 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
29bd0 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20       default:.  
29bd1 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20        iCookie = 
29bd2 42 54 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49  BTREE_USER_VERSI
29bd3 4f 4e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  ON;.        brea
29bd4 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  k;.    }..    if
29bd5 28 20 7a 52 69 67 68 74 20 26 26 20 69 43 6f 6f  ( zRight && iCoo
29bd6 6b 69 65 21 3d 42 54 52 45 45 5f 46 52 45 45 5f  kie!=BTREE_FREE_
29bd7 50 41 47 45 5f 43 4f 55 4e 54 20 29 7b 0a 20 20  PAGE_COUNT ){.  
29bd8 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
29bd9 20 73 70 65 63 69 66 69 65 64 20 63 6f 6f 6b 69   specified cooki
29bda 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20  e value */.     
29bdb 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
29bdc 62 65 4f 70 4c 69 73 74 20 73 65 74 43 6f 6f 6b  beOpList setCook
29bdd 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  ie[] = {.       
29bde 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f   { OP_Transactio
29bdf 6e 2c 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d  n,    0,  1,  0}
29be0 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20  ,    /* 0 */.   
29be1 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65       { OP_Intege
29be2 72 2c 20 20 20 20 20 20 20 20 30 2c 20 20 31 2c  r,        0,  1,
29be3 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f    0},    /* 1 */
29be4 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65  .        { OP_Se
29be5 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c  tCookie,      0,
29be6 20 20 30 2c 20 20 31 7d 2c 20 20 20 20 2f 2a 20    0,  1},    /* 
29be7 32 20 2a 2f 0a 20 20 20 20 20 20 7d 3b 0a 20 20  2 */.      };.  
29be8 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73      int addr = s
29be9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
29bea 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
29beb 28 73 65 74 43 6f 6f 6b 69 65 29 2c 20 73 65 74  (setCookie), set
29bec 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73  Cookie);.      s
29bed 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
29bee 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29  P1(v, addr, iDb)
29bef 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29bf0 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
29bf1 64 64 72 2b 31 2c 20 61 74 6f 69 28 7a 52 69 67  ddr+1, atoi(zRig
29bf2 68 74 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ht));.      sqli
29bf3 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
29bf4 76 2c 20 61 64 64 72 2b 32 2c 20 69 44 62 29 3b  v, addr+2, iDb);
29bf5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29bf6 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64  beChangeP2(v, ad
29bf7 64 72 2b 32 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a  dr+2, iCookie);.
29bf8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29bf9 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73 70 65   /* Read the spe
29bfa 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20 76 61  cified cookie va
29bfb 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 73 74 61  lue */.      sta
29bfc 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
29bfd 4c 69 73 74 20 72 65 61 64 43 6f 6f 6b 69 65 5b  List readCookie[
29bfe 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b 20  ] = {.        { 
29bff 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20  OP_Transaction, 
29c00 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20      0,  0,  0}, 
29c01 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20     /* 0 */.     
29c02 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b     { OP_ReadCook
29c03 69 65 2c 20 20 20 20 20 20 30 2c 20 20 31 2c 20  ie,      0,  1, 
29c04 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a   0},    /* 1 */.
29c05 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73          { OP_Res
29c06 75 6c 74 52 6f 77 2c 20 20 20 20 20 20 20 31 2c  ultRow,       1,
29c07 20 20 31 2c 20 20 30 7d 0a 20 20 20 20 20 20 7d    1,  0}.      }
29c08 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  ;.      int addr
29c09 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
29c0a 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79  dOpList(v, Array
29c0b 53 69 7a 65 28 72 65 61 64 43 6f 6f 6b 69 65 29  Size(readCookie)
29c0c 2c 20 72 65 61 64 43 6f 6f 6b 69 65 29 3b 0a 20  , readCookie);. 
29c0d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29c0e 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
29c0f 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
29c10 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29c11 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 69 44 62  1(v, addr+1, iDb
29c12 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29c13 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20  VdbeChangeP3(v, 
29c14 61 64 64 72 2b 31 2c 20 69 43 6f 6f 6b 69 65 29  addr+1, iCookie)
29c15 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29c16 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
29c17 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
29c18 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29c19 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
29c1a 41 4d 45 2c 20 7a 4c 65 66 74 2c 20 53 51 4c 49  AME, zLeft, SQLI
29c1b 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
29c1c 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e     }.  }else.#en
29c1d 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
29c1e 49 54 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f  IT_SCHEMA_VERSIO
29c1f 4e 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  N_PRAGMAS */..#i
29c20 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
29c21 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e  _DEBUG) || defin
29c22 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
29c23 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 70 6f 72 74    /*.  ** Report
29c24 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
29c25 74 65 20 6f 66 20 66 69 6c 65 20 6c 6f 67 73 20  te of file logs 
29c26 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65  for all database
29c27 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  s.  */.  if( sql
29c28 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29c29 74 2c 20 22 6c 6f 63 6b 5f 73 74 61 74 75 73 22  t, "lock_status"
29c2a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74  )==0 ){.    stat
29c2b 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
29c2c 6f 6e 73 74 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b  onst azLockName[
29c2d 5d 20 3d 20 7b 0a 20 20 20 20 20 20 22 75 6e 6c  ] = {.      "unl
29c2e 6f 63 6b 65 64 22 2c 20 22 73 68 61 72 65 64 22  ocked", "shared"
29c2f 2c 20 22 72 65 73 65 72 76 65 64 22 2c 20 22 70  , "reserved", "p
29c30 65 6e 64 69 6e 67 22 2c 20 22 65 78 63 6c 75 73  ending", "exclus
29c31 69 76 65 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20  ive".    };.    
29c32 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74  int i;.    sqlit
29c33 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
29c34 28 76 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72  (v, 2);.    pPar
29c35 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20  se->nMem = 2;.  
29c36 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29c37 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
29c38 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 61 74  LNAME_NAME, "dat
29c39 61 62 61 73 65 22 2c 20 53 51 4c 49 54 45 5f 53  abase", SQLITE_S
29c3a 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
29c3b 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
29c3c 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
29c3d 4e 41 4d 45 2c 20 22 73 74 61 74 75 73 22 2c 20  NAME, "status", 
29c3e 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
29c3f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
29c40 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
29c41 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a      Btree *pBt;.
29c42 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61        Pager *pPa
29c43 67 65 72 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  ger;.      const
29c44 20 63 68 61 72 20 2a 7a 53 74 61 74 65 20 3d 20   char *zState = 
29c45 22 75 6e 6b 6e 6f 77 6e 22 3b 0a 20 20 20 20 20  "unknown";.     
29c46 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 66   int j;.      if
29c47 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61  ( db->aDb[i].zNa
29c48 6d 65 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  me==0 ) continue
29c49 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29c4a 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
29c4b 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30  String8, 0, 1, 0
29c4c 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61  , db->aDb[i].zNa
29c4d 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  me, P4_STATIC);.
29c4e 20 20 20 20 20 20 70 42 74 20 3d 20 64 62 2d 3e        pBt = db->
29c4f 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
29c50 20 20 69 66 28 20 70 42 74 3d 3d 30 20 7c 7c 20    if( pBt==0 || 
29c51 28 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  (pPager = sqlite
29c52 33 42 74 72 65 65 50 61 67 65 72 28 70 42 74 29  3BtreePager(pBt)
29c53 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
29c54 7a 53 74 61 74 65 20 3d 20 22 63 6c 6f 73 65 64  zState = "closed
29c55 22 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  ";.      }else i
29c56 66 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  f( sqlite3_file_
29c57 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 69 20 3f 20  control(db, i ? 
29c58 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
29c59 20 3a 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20   : 0, .         
29c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c5b 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
29c5c 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41  TE_FCNTL_LOCKSTA
29c5d 54 45 2c 20 26 6a 29 3d 3d 53 51 4c 49 54 45 5f  TE, &j)==SQLITE_
29c5e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 7a  OK ){.         z
29c5f 53 74 61 74 65 20 3d 20 61 7a 4c 6f 63 6b 4e 61  State = azLockNa
29c60 6d 65 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  me[j];.      }. 
29c61 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29c62 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
29c63 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 7a  ing8, 0, 2, 0, z
29c64 53 74 61 74 65 2c 20 50 34 5f 53 54 41 54 49 43  State, P4_STATIC
29c65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29c66 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29c67 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32  _ResultRow, 1, 2
29c68 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73  );.    }..  }els
29c69 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51  e.#endif..#if SQ
29c6a 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20  LITE_HAS_CODEC. 
29c6b 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
29c6c 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b 65 79 22  Cmp(zLeft, "key"
29c6d 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
29c6e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6b 65  {.    sqlite3_ke
29c6f 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 73 71  y(db, zRight, sq
29c70 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 52  lite3Strlen30(zR
29c71 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a  ight));.  }else.
29c72 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
29c73 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 72 65 6b  ICmp(zLeft, "rek
29c74 65 79 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  ey")==0 && zRigh
29c75 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
29c76 5f 72 65 6b 65 79 28 64 62 2c 20 7a 52 69 67 68  _rekey(db, zRigh
29c77 74 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  t, sqlite3Strlen
29c78 33 30 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d  30(zRight));.  }
29c79 65 6c 73 65 0a 20 20 69 66 28 20 7a 52 69 67 68  else.  if( zRigh
29c7a 74 20 26 26 20 28 73 71 6c 69 74 65 33 53 74 72  t && (sqlite3Str
29c7b 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 68 65 78  ICmp(zLeft, "hex
29c7c 6b 65 79 22 29 3d 3d 30 20 7c 7c 0a 20 20 20 20  key")==0 ||.    
29c7d 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
29c7e 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29c7f 74 2c 20 22 68 65 78 72 65 6b 65 79 22 29 3d 3d  t, "hexrekey")==
29c80 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c  0) ){.    int i,
29c81 20 68 31 2c 20 68 32 3b 0a 20 20 20 20 63 68 61   h1, h2;.    cha
29c82 72 20 7a 4b 65 79 5b 34 30 5d 3b 0a 20 20 20 20  r zKey[40];.    
29c83 66 6f 72 28 69 3d 30 3b 20 28 68 31 20 3d 20 7a  for(i=0; (h1 = z
29c84 52 69 67 68 74 5b 69 5d 29 21 3d 30 20 26 26 20  Right[i])!=0 && 
29c85 28 68 32 20 3d 20 7a 52 69 67 68 74 5b 69 2b 31  (h2 = zRight[i+1
29c86 5d 29 21 3d 30 3b 20 69 2b 3d 32 29 7b 0a 20 20  ])!=0; i+=2){.  
29c87 20 20 20 20 68 31 20 2b 3d 20 39 2a 28 31 26 28      h1 += 9*(1&(
29c88 68 31 3e 3e 36 29 29 3b 0a 20 20 20 20 20 20 68  h1>>6));.      h
29c89 32 20 2b 3d 20 39 2a 28 31 26 28 68 32 3e 3e 36  2 += 9*(1&(h2>>6
29c8a 29 29 3b 0a 20 20 20 20 20 20 7a 4b 65 79 5b 69  ));.      zKey[i
29c8b 2f 32 5d 20 3d 20 28 68 32 20 26 20 30 78 30 66  /2] = (h2 & 0x0f
29c8c 29 20 7c 20 28 28 68 31 20 26 20 30 78 66 29 3c  ) | ((h1 & 0xf)<
29c8d 3c 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  <4);.    }.    i
29c8e 66 28 20 28 7a 4c 65 66 74 5b 33 5d 20 26 20 30  f( (zLeft[3] & 0
29c8f 78 66 29 3d 3d 30 78 62 20 29 7b 0a 20 20 20 20  xf)==0xb ){.    
29c90 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 64 62    sqlite3_key(db
29c91 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a 20 20  , zKey, i/2);.  
29c92 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
29c93 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 64 62 2c  qlite3_rekey(db,
29c94 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20   zKey, i/2);.   
29c95 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
29c96 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53  f.#if SQLITE_HAS
29c97 5f 43 4f 44 45 43 20 7c 7c 20 64 65 66 69 6e 65  _CODEC || define
29c98 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
29c99 43 45 52 4f 44 29 0a 20 20 69 66 28 20 73 71 6c  CEROD).  if( sql
29c9a 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29c9b 74 2c 20 22 61 63 74 69 76 61 74 65 5f 65 78 74  t, "activate_ext
29c9c 65 6e 73 69 6f 6e 73 22 29 3d 3d 30 20 29 7b 0a  ensions")==0 ){.
29c9d 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43  #if SQLITE_HAS_C
29c9e 4f 44 45 43 0a 20 20 20 20 69 66 28 20 73 71 6c  ODEC.    if( sql
29c9f 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 52 69  ite3StrNICmp(zRi
29ca0 67 68 74 2c 20 22 73 65 65 2d 22 2c 20 34 29 3d  ght, "see-", 4)=
29ca1 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 78 74 65  =0 ){.      exte
29ca2 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  rn void sqlite3_
29ca3 61 63 74 69 76 61 74 65 5f 73 65 65 28 63 6f 6e  activate_see(con
29ca4 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20  st char*);.     
29ca5 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74   sqlite3_activat
29ca6 65 5f 73 65 65 28 26 7a 52 69 67 68 74 5b 34 5d  e_see(&zRight[4]
29ca7 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
29ca8 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
29ca9 41 42 4c 45 5f 43 45 52 4f 44 0a 20 20 20 20 69  ABLE_CEROD.    i
29caa 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  f( sqlite3StrNIC
29cab 6d 70 28 7a 52 69 67 68 74 2c 20 22 63 65 72 6f  mp(zRight, "cero
29cac 64 2d 22 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20  d-", 6)==0 ){.  
29cad 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20      extern void 
29cae 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65  sqlite3_activate
29caf 5f 63 65 72 6f 64 28 63 6f 6e 73 74 20 63 68 61  _cerod(const cha
29cb0 72 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r*);.      sqlit
29cb1 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65 72 6f  e3_activate_cero
29cb2 64 28 26 7a 52 69 67 68 74 5b 36 5d 29 3b 0a 20  d(&zRight[6]);. 
29cb3 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65     }.#endif.  }e
29cb4 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 0a 20 20  lse.#endif.. .  
29cb5 7b 2f 2a 20 45 6d 70 74 79 20 45 4c 53 45 20 63  {/* Empty ELSE c
29cb6 6c 61 75 73 65 20 2a 2f 7d 0a 0a 20 20 2f 2a 20  lause */}..  /* 
29cb7 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70 69 72  Code an OP_Expir
29cb8 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  e at the end of 
29cb9 65 61 63 68 20 50 52 41 47 4d 41 20 70 72 6f 67  each PRAGMA prog
29cba 72 61 6d 20 74 6f 20 63 61 75 73 65 0a 20 20 2a  ram to cause.  *
29cbb 2a 20 74 68 65 20 56 44 42 45 20 69 6d 70 6c 65  * the VDBE imple
29cbc 6d 65 6e 74 69 6e 67 20 74 68 65 20 70 72 61 67  menting the prag
29cbd 6d 61 20 74 6f 20 65 78 70 69 72 65 2e 20 4d 6f  ma to expire. Mo
29cbe 73 74 20 28 61 6c 6c 3f 29 20 70 72 61 67 6d 61  st (all?) pragma
29cbf 73 0a 20 20 2a 2a 20 61 72 65 20 6f 6e 6c 79 20  s.  ** are only 
29cc0 76 61 6c 69 64 20 66 6f 72 20 61 20 73 69 6e 67  valid for a sing
29cc1 6c 65 20 65 78 65 63 75 74 69 6f 6e 2e 0a 20 20  le execution..  
29cc2 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
29cc3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70  AddOp2(v, OP_Exp
29cc4 69 72 65 2c 20 31 2c 20 30 29 3b 0a 0a 20 20 2f  ire, 1, 0);..  /
29cc5 2a 0a 20 20 2a 2a 20 52 65 73 65 74 20 74 68 65  *.  ** Reset the
29cc6 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2c 20 69   safety level, i
29cc7 6e 20 63 61 73 65 20 74 68 65 20 66 75 6c 6c 66  n case the fullf
29cc8 73 79 6e 63 20 66 6c 61 67 20 6f 72 20 73 79 6e  sync flag or syn
29cc9 63 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20 73 65  chronous.  ** se
29cca 74 74 69 6e 67 20 63 68 61 6e 67 65 64 2e 0a 20  tting changed.. 
29ccb 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
29ccc 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
29ccd 41 47 4d 41 53 0a 20 20 69 66 28 20 64 62 2d 3e  AGMAS.  if( db->
29cce 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20  autoCommit ){.  
29ccf 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
29cd0 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 44 62  tSafetyLevel(pDb
29cd1 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 73 61 66 65  ->pBt, pDb->safe
29cd2 74 79 5f 6c 65 76 65 6c 2c 0a 20 20 20 20 20 20  ty_level,.      
29cd3 20 20 20 20 20 20 20 20 20 28 64 62 2d 3e 66 6c           (db->fl
29cd4 61 67 73 26 53 51 4c 49 54 45 5f 46 75 6c 6c 46  ags&SQLITE_FullF
29cd5 53 79 6e 63 29 21 3d 30 29 3b 0a 20 20 7d 0a 23  Sync)!=0);.  }.#
29cd6 65 6e 64 69 66 0a 70 72 61 67 6d 61 5f 6f 75 74  endif.pragma_out
29cd7 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  :.  sqlite3DbFre
29cd8 65 28 64 62 2c 20 7a 4c 65 66 74 29 3b 0a 20 20  e(db, zLeft);.  
29cd9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
29cda 2c 20 7a 52 69 67 68 74 29 3b 0a 7d 0a 0a 23 65  , zRight);.}..#e
29cdb 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
29cdc 4d 49 54 5f 50 52 41 47 4d 41 20 2a 2f 0a 0a 2f  MIT_PRAGMA */../
29cdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
29cde 6e 64 20 6f 66 20 70 72 61 67 6d 61 2e 63 20 2a  nd of pragma.c *
29cdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ce1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
29ce2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
29ce3 65 67 69 6e 20 66 69 6c 65 20 70 72 65 70 61 72  egin file prepar
29ce4 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
29ce5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ce6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
29ce7 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 35  *.** 2005 May 25
29ce8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
29ce9 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
29cea 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
29ceb 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
29cec 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
29ced 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
29cee 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
29cef 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
29cf0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
29cf1 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
29cf2 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
29cf3 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
29cf4 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
29cf5 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
29cf6 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
29cf7 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
29cf8 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
29cf9 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
29cfa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29cfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29cfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29cfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
29cfe 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
29cff 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d  tains the implem
29d00 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
29d01 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
29d02 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c 20  ).** interface, 
29d03 61 6e 64 20 72 6f 75 74 69 6e 65 73 20 74 68 61  and routines tha
29d04 74 20 63 6f 6e 74 72 69 62 75 74 65 20 74 6f 20  t contribute to 
29d05 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 61 74 61  loading the data
29d06 62 61 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20 66  base schema.** f
29d07 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2f 0a 0a 2f 2a  rom disk..*/../*
29d08 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 69  .** Fill the Ini
29d09 74 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20  tData structure 
29d0a 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
29d0b 73 73 61 67 65 20 74 68 61 74 20 69 6e 64 69 63  ssage that indic
29d0c 61 74 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65  ates.** that the
29d0d 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
29d0e 72 75 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rupt..*/.static 
29d0f 76 6f 69 64 20 63 6f 72 72 75 70 74 53 63 68 65  void corruptSche
29d10 6d 61 28 0a 20 20 49 6e 69 74 44 61 74 61 20 2a  ma(.  InitData *
29d11 70 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 49 6e  pData,     /* In
29d12 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e  itialization con
29d13 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  text */.  const 
29d14 63 68 61 72 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f  char *zObj,    /
29d15 2a 20 4f 62 6a 65 63 74 20 62 65 69 6e 67 20 70  * Object being p
29d16 61 72 73 65 64 20 61 74 20 74 68 65 20 70 6f 69  arsed at the poi
29d17 6e 74 20 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20  nt of error */. 
29d18 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78   const char *zEx
29d19 74 72 61 20 20 20 2f 2a 20 45 72 72 6f 72 20 69  tra   /* Error i
29d1a 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b  nformation */.){
29d1b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
29d1c 20 70 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 66   pData->db;.  if
29d1d 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
29d1e 6c 65 64 20 26 26 20 28 64 62 2d 3e 66 6c 61 67  led && (db->flag
29d1f 73 20 26 20 53 51 4c 49 54 45 5f 52 65 63 6f 76  s & SQLITE_Recov
29d20 65 72 79 4d 6f 64 65 29 3d 3d 30 20 29 7b 0a 20  eryMode)==0 ){. 
29d21 20 20 20 69 66 28 20 7a 4f 62 6a 3d 3d 30 20 29     if( zObj==0 )
29d22 20 7a 4f 62 6a 20 3d 20 22 3f 22 3b 0a 20 20 20   zObj = "?";.   
29d23 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
29d24 67 28 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73  g(pData->pzErrMs
29d25 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 22 6d 61  g, db,.      "ma
29d26 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65  lformed database
29d27 20 73 63 68 65 6d 61 20 28 25 73 29 22 2c 20 7a   schema (%s)", z
29d28 4f 62 6a 29 3b 0a 20 20 20 20 69 66 28 20 7a 45  Obj);.    if( zE
29d29 78 74 72 61 20 29 7b 0a 20 20 20 20 20 20 2a 70  xtra ){.      *p
29d2a 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 20 3d  Data->pzErrMsg =
29d2b 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
29d2c 28 64 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45  (db, *pData->pzE
29d2d 72 72 4d 73 67 2c 20 0a 20 20 20 20 20 20 20 20  rrMsg, .        
29d2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29d2f 20 20 20 20 20 20 20 20 20 22 25 73 20 2d 20 25           "%s - %
29d30 73 22 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72  s", *pData->pzEr
29d31 72 4d 73 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20  rMsg, zExtra);. 
29d32 20 20 20 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61     }.  }.  pData
29d33 2d 3e 72 63 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f  ->rc = db->mallo
29d34 63 46 61 69 6c 65 64 20 3f 20 53 51 4c 49 54 45  cFailed ? SQLITE
29d35 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45 5f  _NOMEM : SQLITE_
29d36 43 4f 52 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a  CORRUPT;.}../*.*
29d37 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 63 61  * This is the ca
29d38 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 66  llback routine f
29d39 6f 72 20 74 68 65 20 63 6f 64 65 20 74 68 61 74  or the code that
29d3a 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65   initializes the
29d3b 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 53  .** database.  S
29d3c 65 65 20 73 71 6c 69 74 65 33 49 6e 69 74 28 29  ee sqlite3Init()
29d3d 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74   below for addit
29d3e 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
29d3f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  n..** This routi
29d40 6e 65 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65  ne is also calle
29d41 64 20 66 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61  d from the OP_Pa
29d42 72 73 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65  rseSchema opcode
29d43 20 6f 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a   of the VDBE..**
29d44 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c 62 61 63  .** Each callbac
29d45 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66  k contains the f
29d46 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61  ollowing informa
29d47 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  tion:.**.**     
29d48 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f  argv[0] = name o
29d49 66 20 74 68 69 6e 67 20 62 65 69 6e 67 20 63 72  f thing being cr
29d4a 65 61 74 65 64 0a 2a 2a 20 20 20 20 20 61 72 67  eated.**     arg
29d4b 76 5b 31 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65  v[1] = root page
29d4c 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c   number for tabl
29d4d 65 20 6f 72 20 69 6e 64 65 78 2e 20 30 20 66 6f  e or index. 0 fo
29d4e 72 20 74 72 69 67 67 65 72 20 6f 72 20 76 69 65  r trigger or vie
29d4f 77 2e 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 32  w..**     argv[2
29d50 5d 20 3d 20 53 51 4c 20 74 65 78 74 20 66 6f 72  ] = SQL text for
29d51 20 74 68 65 20 43 52 45 41 54 45 20 73 74 61 74   the CREATE stat
29d52 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c  ement..**.*/.SQL
29d53 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
29d54 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62  sqlite3InitCallb
29d55 61 63 6b 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c  ack(void *pInit,
29d56 20 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20   int argc, char 
29d57 2a 2a 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e  **argv, char **N
29d58 6f 74 55 73 65 64 29 7b 0a 20 20 49 6e 69 74 44  otUsed){.  InitD
29d59 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 49 6e  ata *pData = (In
29d5a 69 74 44 61 74 61 2a 29 70 49 6e 69 74 3b 0a 20  itData*)pInit;. 
29d5b 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
29d5c 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  Data->db;.  int 
29d5d 69 44 62 20 3d 20 70 44 61 74 61 2d 3e 69 44 62  iDb = pData->iDb
29d5e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
29d5f 63 3d 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==3 );.  UNUSED
29d60 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
29d61 73 65 64 2c 20 61 72 67 63 29 3b 0a 20 20 61 73  sed, argc);.  as
29d62 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
29d63 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
29d64 65 78 29 20 29 3b 0a 20 20 44 62 43 6c 65 61 72  ex) );.  DbClear
29d65 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62  Property(db, iDb
29d66 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20 69  , DB_Empty);.  i
29d67 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
29d68 6c 65 64 20 29 7b 0a 20 20 20 20 63 6f 72 72 75  led ){.    corru
29d69 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20  ptSchema(pData, 
29d6a 61 72 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 20  argv[0], 0);.   
29d6b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a   return 1;.  }..
29d6c 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
29d6d 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
29d6e 29 3b 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30  );.  if( argv==0
29d6f 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f   ) return 0;   /
29d70 2a 20 4d 69 67 68 74 20 68 61 70 70 65 6e 20 69  * Might happen i
29d71 66 20 45 4d 50 54 59 5f 52 45 53 55 4c 54 5f 43  f EMPTY_RESULT_C
29d72 41 4c 4c 42 41 43 4b 53 20 61 72 65 20 6f 6e 20  ALLBACKS are on 
29d73 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 5b 31 5d  */.  if( argv[1]
29d74 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72 72 75  ==0 ){.    corru
29d75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20  ptSchema(pData, 
29d76 61 72 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 7d  argv[0], 0);.  }
29d77 65 6c 73 65 20 69 66 28 20 61 72 67 76 5b 32 5d  else if( argv[2]
29d78 20 26 26 20 61 72 67 76 5b 32 5d 5b 30 5d 20 29   && argv[2][0] )
29d79 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68  {.    /* Call th
29d7a 65 20 70 61 72 73 65 72 20 74 6f 20 70 72 6f 63  e parser to proc
29d7b 65 73 73 20 61 20 43 52 45 41 54 45 20 54 41 42  ess a CREATE TAB
29d7c 4c 45 2c 20 49 4e 44 45 58 20 6f 72 20 56 49 45  LE, INDEX or VIE
29d7d 57 2e 0a 20 20 20 20 2a 2a 20 42 75 74 20 62 65  W..    ** But be
29d7e 63 61 75 73 65 20 64 62 2d 3e 69 6e 69 74 2e 62  cause db->init.b
29d7f 75 73 79 20 69 73 20 73 65 74 20 74 6f 20 31 2c  usy is set to 1,
29d80 20 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 69 73   no VDBE code is
29d81 20 67 65 6e 65 72 61 74 65 64 0a 20 20 20 20 2a   generated.    *
29d82 2a 20 6f 72 20 65 78 65 63 75 74 65 64 2e 20 20  * or executed.  
29d83 41 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 64  All the parser d
29d84 6f 65 73 20 69 73 20 62 75 69 6c 64 20 74 68 65  oes is build the
29d85 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 20   internal data. 
29d86 20 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 73     ** structures
29d87 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74   that describe t
29d88 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c  he table, index,
29d89 20 6f 72 20 76 69 65 77 2e 0a 20 20 20 20 2a 2f   or view..    */
29d8a 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 3b  .    char *zErr;
29d8b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
29d8c 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
29d8d 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 64 62  t.busy );.    db
29d8e 2d 3e 69 6e 69 74 2e 69 44 62 20 3d 20 69 44 62  ->init.iDb = iDb
29d8f 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6e  ;.    db->init.n
29d90 65 77 54 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72  ewTnum = atoi(ar
29d91 67 76 5b 31 5d 29 3b 0a 20 20 20 20 64 62 2d 3e  gv[1]);.    db->
29d92 69 6e 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67  init.orphanTrigg
29d93 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  er = 0;.    rc =
29d94 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
29d95 2c 20 61 72 67 76 5b 32 5d 2c 20 30 2c 20 30 2c  , argv[2], 0, 0,
29d96 20 26 7a 45 72 72 29 3b 0a 20 20 20 20 64 62 2d   &zErr);.    db-
29d97 3e 69 6e 69 74 2e 69 44 62 20 3d 20 30 3b 0a 20  >init.iDb = 0;. 
29d98 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
29d99 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 7a 45 72 72  QLITE_OK || zErr
29d9a 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 53  ==0 );.    if( S
29d9b 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a  QLITE_OK!=rc ){.
29d9c 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e        if( db->in
29d9d 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72  it.orphanTrigger
29d9e 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
29d9f 72 74 28 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20  rt( iDb==1 );.  
29da0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29da1 20 20 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 72     pData->rc = r
29da2 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  c;.        if( r
29da3 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
29da4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  ){.          db-
29da5 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
29da6 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
29da7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
29da8 49 4e 54 45 52 52 55 50 54 20 26 26 20 72 63 21  INTERRUPT && rc!
29da9 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29  =SQLITE_LOCKED )
29daa 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 72 72  {.          corr
29dab 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c  uptSchema(pData,
29dac 20 61 72 67 76 5b 30 5d 2c 20 7a 45 72 72 29 3b   argv[0], zErr);
29dad 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29dae 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
29daf 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
29db0 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
29db1 69 66 28 20 61 72 67 76 5b 30 5d 3d 3d 30 20 29  if( argv[0]==0 )
29db2 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53 63 68  {.    corruptSch
29db3 65 6d 61 28 70 44 61 74 61 2c 20 30 2c 20 30 29  ema(pData, 0, 0)
29db4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
29db5 2a 20 49 66 20 74 68 65 20 53 51 4c 20 63 6f 6c  * If the SQL col
29db6 75 6d 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74 20  umn is blank it 
29db7 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 61 6e  means this is an
29db8 20 69 6e 64 65 78 20 74 68 61 74 0a 20 20 20 20   index that.    
29db9 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 20 74  ** was created t
29dba 6f 20 62 65 20 74 68 65 20 50 52 49 4d 41 52 59  o be the PRIMARY
29dbb 20 4b 45 59 20 6f 72 20 74 6f 20 66 75 6c 66 69   KEY or to fulfi
29dbc 6c 6c 20 61 20 55 4e 49 51 55 45 0a 20 20 20 20  ll a UNIQUE.    
29dbd 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f  ** constraint fo
29dbe 72 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  r a CREATE TABLE
29dbf 2e 20 20 54 68 65 20 69 6e 64 65 78 20 73 68 6f  .  The index sho
29dc0 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79  uld have already
29dc1 0a 20 20 20 20 2a 2a 20 62 65 65 6e 20 63 72 65  .    ** been cre
29dc2 61 74 65 64 20 77 68 65 6e 20 77 65 20 70 72 6f  ated when we pro
29dc3 63 65 73 73 65 64 20 74 68 65 20 43 52 45 41 54  cessed the CREAT
29dc4 45 20 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77 65  E TABLE.  All we
29dc5 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20   have.    ** to 
29dc6 64 6f 20 68 65 72 65 20 69 73 20 72 65 63 6f 72  do here is recor
29dc7 64 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  d the root page 
29dc8 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20  number for that 
29dc9 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20  index..    */.  
29dca 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
29dcb 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 73 71  .    pIndex = sq
29dcc 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
29dcd 62 2c 20 61 72 67 76 5b 30 5d 2c 20 64 62 2d 3e  b, argv[0], db->
29dce 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
29dcf 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 3d  .    if( pIndex=
29dd0 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  =0 ){.      /* T
29dd1 68 69 73 20 63 61 6e 20 6f 63 63 75 72 20 69 66  his can occur if
29dd2 20 74 68 65 72 65 20 65 78 69 73 74 73 20 61 6e   there exists an
29dd3 20 69 6e 64 65 78 20 6f 6e 20 61 20 54 45 4d 50   index on a TEMP
29dd4 20 74 61 62 6c 65 20 77 68 69 63 68 0a 20 20 20   table which.   
29dd5 20 20 20 2a 2a 20 68 61 73 20 74 68 65 20 73 61     ** has the sa
29dd6 6d 65 20 6e 61 6d 65 20 61 73 20 61 6e 6f 74 68  me name as anoth
29dd7 65 72 20 69 6e 64 65 78 20 6f 6e 20 61 20 70 65  er index on a pe
29dd8 72 6d 61 6e 65 6e 74 20 69 6e 64 65 78 2e 20 20  rmanent index.  
29dd9 53 69 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74  Since.      ** t
29dda 68 65 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62  he permanent tab
29ddb 6c 65 20 69 73 20 68 69 64 64 65 6e 20 62 79 20  le is hidden by 
29ddc 74 68 65 20 54 45 4d 50 20 74 61 62 6c 65 2c 20  the TEMP table, 
29ddd 77 65 20 63 61 6e 20 61 6c 73 6f 0a 20 20 20 20  we can also.    
29dde 20 20 2a 2a 20 73 61 66 65 6c 79 20 69 67 6e 6f    ** safely igno
29ddf 72 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20  re the index on 
29de0 74 68 65 20 70 65 72 6d 61 6e 65 6e 74 20 74 61  the permanent ta
29de1 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ble..      */.  
29de2 20 20 20 20 2f 2a 20 44 6f 20 4e 6f 74 68 69 6e      /* Do Nothin
29de3 67 20 2a 2f 3b 0a 20 20 20 20 7d 65 6c 73 65 20  g */;.    }else 
29de4 69 66 28 20 73 71 6c 69 74 65 33 47 65 74 49 6e  if( sqlite3GetIn
29de5 74 33 32 28 61 72 67 76 5b 31 5d 2c 20 26 70 49  t32(argv[1], &pI
29de6 6e 64 65 78 2d 3e 74 6e 75 6d 29 3d 3d 30 20 29  ndex->tnum)==0 )
29de7 7b 0a 20 20 20 20 20 20 63 6f 72 72 75 70 74 53  {.      corruptS
29de8 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67  chema(pData, arg
29de9 76 5b 30 5d 2c 20 22 69 6e 76 61 6c 69 64 20 72  v[0], "invalid r
29dea 6f 6f 74 70 61 67 65 22 29 3b 0a 20 20 20 20 7d  ootpage");.    }
29deb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
29dec 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
29ded 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 64 61  t to read the da
29dee 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 6e  tabase schema an
29def 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74  d initialize int
29df0 65 72 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 73 74  ernal.** data st
29df1 72 75 63 74 75 72 65 73 20 66 6f 72 20 61 20 73  ructures for a s
29df2 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66  ingle database f
29df3 69 6c 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20  ile.  The index 
29df4 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  of the.** databa
29df5 73 65 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e  se file is given
29df6 20 62 79 20 69 44 62 2e 20 20 69 44 62 3d 3d 30   by iDb.  iDb==0
29df7 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 68 65   is used for the
29df8 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73   main.** databas
29df9 65 2e 20 20 69 44 62 3d 3d 31 20 73 68 6f 75 6c  e.  iDb==1 shoul
29dfa 64 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e  d never be used.
29dfb 20 20 69 44 62 3e 3d 32 20 69 73 20 75 73 65 64    iDb>=2 is used
29dfc 20 66 6f 72 0a 2a 2a 20 61 75 78 69 6c 69 61 72   for.** auxiliar
29dfd 79 20 64 61 74 61 62 61 73 65 73 2e 20 20 52 65  y databases.  Re
29dfe 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  turn one of the 
29dff 53 51 4c 49 54 45 5f 20 65 72 72 6f 72 20 63 6f  SQLITE_ error co
29e00 64 65 73 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61  des to.** indica
29e01 74 65 20 73 75 63 63 65 73 73 20 6f 72 20 66 61  te success or fa
29e02 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ilure..*/.static
29e03 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
29e04 4f 6e 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  One(sqlite3 *db,
29e05 20 69 6e 74 20 69 44 62 2c 20 63 68 61 72 20 2a   int iDb, char *
29e06 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e  *pzErrMsg){.  in
29e07 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  t rc;.  int i;. 
29e08 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 54 61 62   int size;.  Tab
29e09 6c 65 20 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a  le *pTab;.  Db *
29e0a 70 44 62 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  pDb;.  char cons
29e0b 74 20 2a 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69  t *azArg[4];.  i
29e0c 6e 74 20 6d 65 74 61 5b 35 5d 3b 0a 20 20 49 6e  nt meta[5];.  In
29e0d 69 74 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b  itData initData;
29e0e 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
29e0f 4d 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20  MasterSchema;.  
29e10 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73  char const *zMas
29e11 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41  terName = SCHEMA
29e12 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 69  _TABLE(iDb);.  i
29e13 6e 74 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63  nt openedTransac
29e14 74 69 6f 6e 20 3d 20 30 3b 0a 0a 20 20 2f 2a 0a  tion = 0;..  /*.
29e15 20 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20    ** The master 
29e16 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 68  database table h
29e17 61 73 20 61 20 73 74 72 75 63 74 75 72 65 20 6c  as a structure l
29e18 69 6b 65 20 74 68 69 73 0a 20 20 2a 2f 0a 20 20  ike this.  */.  
29e19 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
29e1a 72 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b  r master_schema[
29e1b 5d 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54  ] = .     "CREAT
29e1c 45 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 6d  E TABLE sqlite_m
29e1d 61 73 74 65 72 28 5c 6e 22 0a 20 20 20 20 20 22  aster(\n".     "
29e1e 20 20 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a    type text,\n".
29e1f 20 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78       "  name tex
29e20 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 62  t,\n".     "  tb
29e21 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a  l_name text,\n".
29e22 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67 65       "  rootpage
29e23 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20   integer,\n".   
29e24 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22    "  sql text\n"
29e25 0a 20 20 20 20 20 22 29 22 0a 20 20 3b 0a 23 69  .     ")".  ;.#i
29e26 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29e27 54 5f 54 45 4d 50 44 42 0a 20 20 73 74 61 74 69  T_TEMPDB.  stati
29e28 63 20 63 6f 6e 73 74 20 63 68 61 72 20 74 65 6d  c const char tem
29e29 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b  p_master_schema[
29e2a 5d 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54  ] = .     "CREAT
29e2b 45 20 54 45 4d 50 20 54 41 42 4c 45 20 73 71 6c  E TEMP TABLE sql
29e2c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 28  ite_temp_master(
29e2d 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 79 70 65  \n".     "  type
29e2e 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22   text,\n".     "
29e2f 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a    name text,\n".
29e30 20 20 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65       "  tbl_name
29e31 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22   text,\n".     "
29e32 20 20 72 6f 6f 74 70 61 67 65 20 69 6e 74 65 67    rootpage integ
29e33 65 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73  er,\n".     "  s
29e34 71 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20 20 20  ql text\n".     
29e35 22 29 22 0a 20 20 3b 0a 23 65 6c 73 65 0a 20 20  ")".  ;.#else.  
29e36 23 64 65 66 69 6e 65 20 74 65 6d 70 5f 6d 61 73  #define temp_mas
29e37 74 65 72 5f 73 63 68 65 6d 61 20 30 0a 23 65 6e  ter_schema 0.#en
29e38 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 69  dif..  assert( i
29e39 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
29e3a 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
29e3b 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  ( db->aDb[iDb].p
29e3c 53 63 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65  Schema );.  asse
29e3d 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
29e3e 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
29e3f 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ) );.  assert( i
29e40 44 62 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65 33  Db==1 || sqlite3
29e41 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
29e42 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
29e43 29 20 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73 74  ) );..  /* zMast
29e44 65 72 53 63 68 65 6d 61 20 61 6e 64 20 7a 49 6e  erSchema and zIn
29e45 69 74 53 63 72 69 70 74 20 61 72 65 20 73 65 74  itScript are set
29e46 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
29e47 20 6d 61 73 74 65 72 20 73 63 68 65 6d 61 0a 20   master schema. 
29e48 20 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69   ** and initiali
29e49 73 61 74 69 6f 6e 20 73 63 72 69 70 74 20 61 70  sation script ap
29e4a 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68  propriate for th
29e4b 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67  e database being
29e4c 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73 65  .  ** initialise
29e4d 64 2e 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20 69  d. zMasterName i
29e4e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
29e4f 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a  e master table..
29e50 20 20 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49 54    */.  if( !OMIT
29e51 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d  _TEMPDB && iDb==
29e52 31 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72  1 ){.    zMaster
29e53 53 63 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d 61  Schema = temp_ma
29e54 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d  ster_schema;.  }
29e55 65 6c 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74 65  else{.    zMaste
29e56 72 53 63 68 65 6d 61 20 3d 20 6d 61 73 74 65 72  rSchema = master
29e57 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20 7a  _schema;.  }.  z
29e58 4d 61 73 74 65 72 4e 61 6d 65 20 3d 20 53 43 48  MasterName = SCH
29e59 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
29e5a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20  .  /* Construct 
29e5b 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65  the schema table
29e5c 73 2e 20 20 2a 2f 0a 20 20 61 7a 41 72 67 5b 30  s.  */.  azArg[0
29e5d 5d 20 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65 3b  ] = zMasterName;
29e5e 0a 20 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22 31  .  azArg[1] = "1
29e5f 22 3b 0a 20 20 61 7a 41 72 67 5b 32 5d 20 3d 20  ";.  azArg[2] = 
29e60 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20  zMasterSchema;. 
29e61 20 61 7a 41 72 67 5b 33 5d 20 3d 20 30 3b 0a 20   azArg[3] = 0;. 
29e62 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64   initData.db = d
29e63 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 69 44  b;.  initData.iD
29e64 62 20 3d 20 69 44 62 3b 0a 20 20 69 6e 69 74 44  b = iDb;.  initD
29e65 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f  ata.rc = SQLITE_
29e66 4f 4b 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70  OK;.  initData.p
29e67 7a 45 72 72 4d 73 67 20 3d 20 70 7a 45 72 72 4d  zErrMsg = pzErrM
29e68 73 67 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69  sg;.  (void)sqli
29e69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
29e6a 3b 0a 20 20 73 71 6c 69 74 65 33 49 6e 69 74 43  ;.  sqlite3InitC
29e6b 61 6c 6c 62 61 63 6b 28 26 69 6e 69 74 44 61 74  allback(&initDat
29e6c 61 2c 20 33 2c 20 28 63 68 61 72 20 2a 2a 29 61  a, 3, (char **)a
29e6d 7a 41 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f 69  zArg, 0);.  (voi
29e6e 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
29e6f 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 69 6e 69  n(db);.  if( ini
29e70 74 44 61 74 61 2e 72 63 20 29 7b 0a 20 20 20 20  tData.rc ){.    
29e71 72 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63  rc = initData.rc
29e72 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ;.    goto error
29e73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62  _out;.  }.  pTab
29e74 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
29e75 62 6c 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 4e  ble(db, zMasterN
29e76 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  ame, db->aDb[iDb
29e77 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ].zName);.  if( 
29e78 41 4c 57 41 59 53 28 70 54 61 62 29 20 29 7b 0a  ALWAYS(pTab) ){.
29e79 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61      pTab->tabFla
29e7a 67 73 20 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c  gs |= TF_Readonl
29e7b 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65  y;.  }..  /* Cre
29e7c 61 74 65 20 61 20 63 75 72 73 6f 72 20 74 6f 20  ate a cursor to 
29e7d 68 6f 6c 64 20 74 68 65 20 64 61 74 61 62 61 73  hold the databas
29e7e 65 20 6f 70 65 6e 0a 20 20 2a 2f 0a 20 20 70 44  e open.  */.  pD
29e7f 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
29e80 5d 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 42  ];.  if( pDb->pB
29e81 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t==0 ){.    if( 
29e82 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
29e83 41 4c 57 41 59 53 28 69 44 62 3d 3d 31 29 20 29  ALWAYS(iDb==1) )
29e84 7b 0a 20 20 20 20 20 20 44 62 53 65 74 50 72 6f  {.      DbSetPro
29e85 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f  perty(db, 1, DB_
29e86 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20  SchemaLoaded);. 
29e87 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
29e88 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
29e89 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
29e8a 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72   not already a r
29e8b 65 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61  ead-only (or rea
29e8c 64 2d 77 72 69 74 65 29 20 74 72 61 6e 73 61 63  d-write) transac
29e8d 74 69 6f 6e 20 6f 70 65 6e 65 64 0a 20 20 2a 2a  tion opened.  **
29e8e 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 64   on the b-tree d
29e8f 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 20 6f 6e  atabase, open on
29e90 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 6e  e now. If a tran
29e91 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65  saction is opene
29e92 64 2c 20 69 74 20 0a 20 20 2a 2a 20 77 69 6c 6c  d, it .  ** will
29e93 20 62 65 20 63 6c 6f 73 65 64 20 62 65 66 6f 72   be closed befor
29e94 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
29e95 72 65 74 75 72 6e 73 2e 20 20 2a 2f 0a 20 20 73  returns.  */.  s
29e96 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
29e97 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 69 66  (pDb->pBt);.  if
29e98 28 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 49  ( !sqlite3BtreeI
29e99 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62  sInReadTrans(pDb
29e9a 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 72 63  ->pBt) ){.    rc
29e9b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42   = sqlite3BtreeB
29e9c 65 67 69 6e 54 72 61 6e 73 28 70 44 62 2d 3e 70  eginTrans(pDb->p
29e9d 42 74 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  Bt, 0);.    if( 
29e9e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
29e9f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
29ea0 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67  tString(pzErrMsg
29ea1 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
29ea2 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
29ea3 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f        goto inito
29ea4 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  ne_error_out;.  
29ea5 20 20 7d 0a 20 20 20 20 6f 70 65 6e 65 64 54 72    }.    openedTr
29ea6 61 6e 73 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20  ansaction = 1;. 
29ea7 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65   }..  /* Get the
29ea8 20 64 61 74 61 62 61 73 65 20 6d 65 74 61 20 69   database meta i
29ea9 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a  nformation..  **
29eaa 0a 20 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65  .  ** Meta value
29eab 73 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73  s are as follows
29eac 3a 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30  :.  **    meta[0
29ead 5d 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69  ]   Schema cooki
29eae 65 2e 20 20 43 68 61 6e 67 65 73 20 77 69 74 68  e.  Changes with
29eaf 20 65 61 63 68 20 73 63 68 65 6d 61 20 63 68 61   each schema cha
29eb0 6e 67 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74  nge..  **    met
29eb1 61 5b 31 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d  a[1]   File form
29eb2 61 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79  at of schema lay
29eb3 65 72 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  er..  **    meta
29eb4 5b 32 5d 20 20 20 53 69 7a 65 20 6f 66 20 74 68  [2]   Size of th
29eb5 65 20 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20  e page cache..  
29eb6 2a 2a 20 20 20 20 6d 65 74 61 5b 33 5d 20 20 20  **    meta[3]   
29eb7 4c 61 72 67 65 73 74 20 72 6f 6f 74 70 61 67 65  Largest rootpage
29eb8 20 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75   (auto/incr_vacu
29eb9 75 6d 20 6d 6f 64 65 29 0a 20 20 2a 2a 20 20 20  um mode).  **   
29eba 20 6d 65 74 61 5b 34 5d 20 20 20 44 62 20 74 65   meta[4]   Db te
29ebb 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 31 3a 55  xt encoding. 1:U
29ebc 54 46 2d 38 20 32 3a 55 54 46 2d 31 36 4c 45 20  TF-8 2:UTF-16LE 
29ebd 33 3a 55 54 46 2d 31 36 42 45 0a 20 20 2a 2a 20  3:UTF-16BE.  ** 
29ebe 20 20 20 6d 65 74 61 5b 35 5d 20 20 20 55 73 65     meta[5]   Use
29ebf 72 20 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20  r version.  **  
29ec0 20 20 6d 65 74 61 5b 36 5d 20 20 20 49 6e 63 72    meta[6]   Incr
29ec1 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d  emental vacuum m
29ec2 6f 64 65 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  ode.  **    meta
29ec3 5b 37 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a  [7]   unused.  *
29ec4 2a 20 20 20 20 6d 65 74 61 5b 38 5d 20 20 20 75  *    meta[8]   u
29ec5 6e 75 73 65 64 0a 20 20 2a 2a 20 20 20 20 6d 65  nused.  **    me
29ec6 74 61 5b 39 5d 20 20 20 75 6e 75 73 65 64 0a 20  ta[9]   unused. 
29ec7 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54   **.  ** Note: T
29ec8 68 65 20 23 64 65 66 69 6e 65 64 20 53 51 4c 49  he #defined SQLI
29ec9 54 45 5f 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20  TE_UTF* symbols 
29eca 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 63  in sqliteInt.h c
29ecb 6f 72 72 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a  orrespond to.  *
29ecc 2a 20 74 68 65 20 70 6f 73 73 69 62 6c 65 20 76  * the possible v
29ecd 61 6c 75 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d  alues of meta[4]
29ece 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
29ecf 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 6d 65  ; i<ArraySize(me
29ed0 74 61 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  ta); i++){.    s
29ed1 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
29ed2 74 61 28 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31  ta(pDb->pBt, i+1
29ed3 2c 20 28 75 33 32 20 2a 29 26 6d 65 74 61 5b 69  , (u32 *)&meta[i
29ed4 5d 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70  ]);.  }.  pDb->p
29ed5 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
29ed6 6f 6f 6b 69 65 20 3d 20 6d 65 74 61 5b 42 54 52  ookie = meta[BTR
29ed7 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f  EE_SCHEMA_VERSIO
29ed8 4e 2d 31 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f  N-1];..  /* If o
29ed9 70 65 6e 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70  pening a non-emp
29eda 74 79 20 64 61 74 61 62 61 73 65 2c 20 63 68 65  ty database, che
29edb 63 6b 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f  ck the text enco
29edc 64 69 6e 67 2e 20 46 6f 72 20 74 68 65 0a 20 20  ding. For the.  
29edd 2a 2a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  ** main database
29ede 2c 20 73 65 74 20 73 71 6c 69 74 65 33 2e 65 6e  , set sqlite3.en
29edf 63 20 74 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e  c to the encodin
29ee0 67 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61  g of the main da
29ee1 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72  tabase..  ** For
29ee2 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 2c   an attached db,
29ee3 20 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   it is an error 
29ee4 69 66 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  if the encoding 
29ee5 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a  is not the same.
29ee6 20 20 2a 2a 20 61 73 20 73 71 6c 69 74 65 33 2e    ** as sqlite3.
29ee7 65 6e 63 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  enc..  */.  if( 
29ee8 6d 65 74 61 5b 42 54 52 45 45 5f 54 45 58 54 5f  meta[BTREE_TEXT_
29ee9 45 4e 43 4f 44 49 4e 47 2d 31 5d 20 29 7b 20 20  ENCODING-1] ){  
29eea 2f 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  /* text encoding
29eeb 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 44 62 3d   */.    if( iDb=
29eec 3d 30 20 29 7b 0a 20 20 20 20 20 20 75 38 20 65  =0 ){.      u8 e
29eed 6e 63 6f 64 69 6e 67 3b 0a 20 20 20 20 20 20 2f  ncoding;.      /
29eee 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 74 68 65  * If opening the
29eef 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20   main database, 
29ef0 73 65 74 20 45 4e 43 28 64 62 29 2e 20 2a 2f 0a  set ENC(db). */.
29ef1 20 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 3d        encoding =
29ef2 20 28 75 38 29 6d 65 74 61 5b 42 54 52 45 45 5f   (u8)meta[BTREE_
29ef3 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d  TEXT_ENCODING-1]
29ef4 20 26 20 33 3b 0a 20 20 20 20 20 20 69 66 28 20   & 3;.      if( 
29ef5 65 6e 63 6f 64 69 6e 67 3d 3d 30 20 29 20 65 6e  encoding==0 ) en
29ef6 63 6f 64 69 6e 67 20 3d 20 53 51 4c 49 54 45 5f  coding = SQLITE_
29ef7 55 54 46 38 3b 0a 20 20 20 20 20 20 45 4e 43 28  UTF8;.      ENC(
29ef8 64 62 29 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  db) = encoding;.
29ef9 20 20 20 20 20 20 64 62 2d 3e 70 44 66 6c 74 43        db->pDfltC
29efa 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
29efb 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c  dCollSeq(db, SQL
29efc 49 54 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52  ITE_UTF8, "BINAR
29efd 59 22 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73  Y", 0);.    }els
29efe 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6f  e{.      /* If o
29eff 70 65 6e 69 6e 67 20 61 6e 20 61 74 74 61 63 68  pening an attach
29f00 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  ed database, the
29f01 20 65 6e 63 6f 64 69 6e 67 20 6d 75 63 68 20 6d   encoding much m
29f02 61 74 63 68 20 45 4e 43 28 64 62 29 20 2a 2f 0a  atch ENC(db) */.
29f03 20 20 20 20 20 20 69 66 28 20 6d 65 74 61 5b 42        if( meta[B
29f04 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49  TREE_TEXT_ENCODI
29f05 4e 47 2d 31 5d 21 3d 45 4e 43 28 64 62 29 20 29  NG-1]!=ENC(db) )
29f06 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
29f07 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72  3SetString(pzErr
29f08 4d 73 67 2c 20 64 62 2c 20 22 61 74 74 61 63 68  Msg, db, "attach
29f09 65 64 20 64 61 74 61 62 61 73 65 73 20 6d 75 73  ed databases mus
29f0a 74 20 75 73 65 20 74 68 65 20 73 61 6d 65 22 0a  t use the same".
29f0b 20 20 20 20 20 20 20 20 20 20 20 20 22 20 74 65              " te
29f0c 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d  xt encoding as m
29f0d 61 69 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0a  ain database");.
29f0e 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
29f0f 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
29f10 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
29f11 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 20  error_out;.     
29f12 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
29f13 7b 0a 20 20 20 20 44 62 53 65 74 50 72 6f 70 65  {.    DbSetPrope
29f14 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f  rty(db, iDb, DB_
29f15 45 6d 70 74 79 29 3b 0a 20 20 7d 0a 20 20 70 44  Empty);.  }.  pD
29f16 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 20  b->pSchema->enc 
29f17 3d 20 45 4e 43 28 64 62 29 3b 0a 0a 20 20 69 66  = ENC(db);..  if
29f18 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
29f19 63 61 63 68 65 5f 73 69 7a 65 3d 3d 30 20 29 7b  cache_size==0 ){
29f1a 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 74 61  .    size = meta
29f1b 5b 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43  [BTREE_DEFAULT_C
29f1c 41 43 48 45 5f 53 49 5a 45 2d 31 5d 3b 0a 20 20  ACHE_SIZE-1];.  
29f1d 20 20 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b    if( size==0 ){
29f1e 20 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44   size = SQLITE_D
29f1f 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
29f20 45 3b 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a  E; }.    if( siz
29f21 65 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69  e<0 ) size = -si
29f22 7a 65 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63  ze;.    pDb->pSc
29f23 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
29f24 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 73 71 6c   = size;.    sql
29f25 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
29f26 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20  eSize(pDb->pBt, 
29f27 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
29f28 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a  che_size);.  }..
29f29 20 20 2f 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66    /*.  ** file_f
29f2a 6f 72 6d 61 74 3d 3d 31 20 20 20 20 56 65 72 73  ormat==1    Vers
29f2b 69 6f 6e 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20  ion 3.0.0..  ** 
29f2c 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20  file_format==2  
29f2d 20 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e    Version 3.1.3.
29f2e 20 20 2f 2f 20 41 4c 54 45 52 20 54 41 42 4c 45    // ALTER TABLE
29f2f 20 41 44 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a   ADD COLUMN.  **
29f30 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20   file_format==3 
29f31 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34     Version 3.1.4
29f32 2e 20 20 2f 2f 20 64 69 74 74 6f 20 62 75 74 20  .  // ditto but 
29f33 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65  with non-NULL de
29f34 66 61 75 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65  faults.  ** file
29f35 5f 66 6f 72 6d 61 74 3d 3d 34 20 20 20 20 56 65  _format==4    Ve
29f36 72 73 69 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f  rsion 3.3.0.  //
29f37 20 44 45 53 43 20 69 6e 64 69 63 65 73 2e 20 20   DESC indices.  
29f38 42 6f 6f 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74  Boolean constant
29f39 73 0a 20 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53  s.  */.  pDb->pS
29f3a 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
29f3b 61 74 20 3d 20 28 75 38 29 6d 65 74 61 5b 42 54  at = (u8)meta[BT
29f3c 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2d  REE_FILE_FORMAT-
29f3d 31 5d 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70  1];.  if( pDb->p
29f3e 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72  Schema->file_for
29f3f 6d 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44  mat==0 ){.    pD
29f40 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  b->pSchema->file
29f41 5f 66 6f 72 6d 61 74 20 3d 20 31 3b 0a 20 20 7d  _format = 1;.  }
29f42 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68  .  if( pDb->pSch
29f43 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
29f44 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45  >SQLITE_MAX_FILE
29f45 5f 46 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 73  _FORMAT ){.    s
29f46 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
29f47 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 75  pzErrMsg, db, "u
29f48 6e 73 75 70 70 6f 72 74 65 64 20 66 69 6c 65 20  nsupported file 
29f49 66 6f 72 6d 61 74 22 29 3b 0a 20 20 20 20 72 63  format");.    rc
29f4a 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
29f4b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e  .    goto initon
29f4c 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d  e_error_out;.  }
29f4d 0a 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 32  ..  /* Ticket #2
29f4e 38 30 34 3a 20 20 57 68 65 6e 20 77 65 20 6f 70  804:  When we op
29f4f 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69 6e  en a database in
29f50 20 74 68 65 20 6e 65 77 65 72 20 66 69 6c 65 20   the newer file 
29f51 66 6f 72 6d 61 74 2c 0a 20 20 2a 2a 20 63 6c 65  format,.  ** cle
29f52 61 72 20 74 68 65 20 6c 65 67 61 63 79 5f 66 69  ar the legacy_fi
29f53 6c 65 5f 66 6f 72 6d 61 74 20 70 72 61 67 6d 61  le_format pragma
29f54 20 66 6c 61 67 20 73 6f 20 74 68 61 74 20 61 20   flag so that a 
29f55 56 41 43 55 55 4d 20 77 69 6c 6c 0a 20 20 2a 2a  VACUUM will.  **
29f56 20 6e 6f 74 20 64 6f 77 6e 67 72 61 64 65 20 74   not downgrade t
29f57 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  he database and 
29f58 74 68 75 73 20 69 6e 76 61 6c 69 64 61 74 65 20  thus invalidate 
29f59 61 6e 79 20 64 65 73 63 65 6e 64 69 6e 67 0a 20  any descending. 
29f5a 20 2a 2a 20 69 6e 64 69 63 65 73 20 74 68 61 74   ** indices that
29f5b 20 74 68 65 20 75 73 65 72 20 6d 69 67 68 74 20   the user might 
29f5c 68 61 76 65 20 63 72 65 61 74 65 64 2e 0a 20 20  have created..  
29f5d 2a 2f 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20  */.  if( iDb==0 
29f5e 26 26 20 6d 65 74 61 5b 42 54 52 45 45 5f 46 49  && meta[BTREE_FI
29f5f 4c 45 5f 46 4f 52 4d 41 54 2d 31 5d 3e 3d 34 20  LE_FORMAT-1]>=4 
29f60 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  ){.    db->flags
29f61 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 65 67 61   &= ~SQLITE_Lega
29f62 63 79 46 69 6c 65 46 6d 74 3b 0a 20 20 7d 0a 0a  cyFileFmt;.  }..
29f63 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73 63    /* Read the sc
29f64 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hema information
29f65 20 6f 75 74 20 6f 66 20 74 68 65 20 73 63 68 65   out of the sche
29f66 6d 61 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20  ma tables.  */. 
29f67 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
29f68 74 2e 62 75 73 79 20 29 3b 0a 20 20 7b 0a 20 20  t.busy );.  {.  
29f69 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20    char *zSql;.  
29f6a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
29f6b 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20  MPrintf(db, .   
29f6c 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d       "SELECT nam
29f6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
29f6e 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 22 2c 0a   FROM '%q'.%s",.
29f6f 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b          db->aDb[
29f70 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73  iDb].zName, zMas
29f71 74 65 72 4e 61 6d 65 29 3b 0a 20 20 20 20 28 76  terName);.    (v
29f72 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
29f73 79 4f 66 66 28 64 62 29 3b 0a 23 69 66 6e 64 65  yOff(db);.#ifnde
29f74 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
29f75 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
29f76 7b 0a 20 20 20 20 20 20 69 6e 74 20 28 2a 78 41  {.      int (*xA
29f77 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  uth)(void*,int,c
29f78 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
29f79 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
29f7a 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  r*,const char*);
29f7b 0a 20 20 20 20 20 20 78 41 75 74 68 20 3d 20 64  .      xAuth = d
29f7c 62 2d 3e 78 41 75 74 68 3b 0a 20 20 20 20 20 20  b->xAuth;.      
29f7d 64 62 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23  db->xAuth = 0;.#
29f7e 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d  endif.      rc =
29f7f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
29f80 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49  , zSql, sqlite3I
29f81 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e  nitCallback, &in
29f82 69 74 44 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e  itData, 0);.#ifn
29f83 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
29f84 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
29f85 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
29f86 78 41 75 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e  xAuth;.    }.#en
29f87 64 69 66 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  dif.    if( rc==
29f88 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d  SQLITE_OK ) rc =
29f89 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20   initData.rc;.  
29f8a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
29f8b 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20  afetyOn(db);.   
29f8c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
29f8d 62 2c 20 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65  b, zSql);.#ifnde
29f8e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  f SQLITE_OMIT_AN
29f8f 41 4c 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63  ALYZE.    if( rc
29f90 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
29f91 20 20 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c       sqlite3Anal
29f92 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62  ysisLoad(db, iDb
29f93 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
29f94 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
29f95 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
29f96 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
29f97 4d 45 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  MEM;.    sqlite3
29f98 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
29f99 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a  ema(db, 0);.  }.
29f9a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
29f9b 5f 4f 4b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67  _OK || (db->flag
29f9c 73 26 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  s&SQLITE_Recover
29f9d 79 4d 6f 64 65 29 29 7b 0a 20 20 20 20 2f 2a 20  yMode)){.    /* 
29f9e 42 6c 61 63 6b 20 6d 61 67 69 63 3a 20 49 66 20  Black magic: If 
29f9f 74 68 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76  the SQLITE_Recov
29fa0 65 72 79 4d 6f 64 65 20 66 6c 61 67 20 69 73 20  eryMode flag is 
29fa1 73 65 74 2c 20 74 68 65 6e 20 63 6f 6e 73 69 64  set, then consid
29fa2 65 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63  er.    ** the sc
29fa3 68 65 6d 61 20 6c 6f 61 64 65 64 2c 20 65 76 65  hema loaded, eve
29fa4 6e 20 69 66 20 65 72 72 6f 72 73 20 6f 63 63 75  n if errors occu
29fa5 72 72 65 64 2e 20 49 6e 20 74 68 69 73 20 73 69  rred. In this si
29fa6 74 75 61 74 69 6f 6e 20 74 68 65 20 0a 20 20 20  tuation the .   
29fa7 20 2a 2a 20 63 75 72 72 65 6e 74 20 73 71 6c 69   ** current sqli
29fa8 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70  te3_prepare() op
29fa9 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69  eration will fai
29faa 6c 2c 20 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f  l, but the follo
29fab 77 69 6e 67 20 6f 6e 65 0a 20 20 20 20 2a 2a 20  wing one.    ** 
29fac 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20  will attempt to 
29fad 63 6f 6d 70 69 6c 65 20 74 68 65 20 73 75 70 70  compile the supp
29fae 6c 69 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61  lied statement a
29faf 67 61 69 6e 73 74 20 77 68 61 74 65 76 65 72 20  gainst whatever 
29fb0 73 75 62 73 65 74 0a 20 20 20 20 2a 2a 20 6f 66  subset.    ** of
29fb1 20 74 68 65 20 73 63 68 65 6d 61 20 77 61 73 20   the schema was 
29fb2 6c 6f 61 64 65 64 20 62 65 66 6f 72 65 20 74 68  loaded before th
29fb3 65 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  e error occurred
29fb4 2e 20 54 68 65 20 70 72 69 6d 61 72 79 0a 20 20  . The primary.  
29fb5 20 20 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66 20    ** purpose of 
29fb6 74 68 69 73 20 69 73 20 74 6f 20 61 6c 6c 6f 77  this is to allow
29fb7 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 73   access to the s
29fb8 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
29fb9 6c 65 0a 20 20 20 20 2a 2a 20 65 76 65 6e 20 77  le.    ** even w
29fba 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73  hen its contents
29fbb 20 68 61 76 65 20 62 65 65 6e 20 63 6f 72 72 75   have been corru
29fbc 70 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  pted..    */.   
29fbd 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28 64   DbSetProperty(d
29fbe 62 2c 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d  b, iDb, DB_Schem
29fbf 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20 20 72 63  aLoaded);.    rc
29fc0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
29fc1 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  }..  /* Jump her
29fc2 65 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 20 74  e for an error t
29fc3 68 61 74 20 6f 63 63 75 72 73 20 61 66 74 65 72  hat occurs after
29fc4 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6c   successfully al
29fc5 6c 6f 63 61 74 69 6e 67 0a 20 20 2a 2a 20 63 75  locating.  ** cu
29fc6 72 4d 61 69 6e 20 61 6e 64 20 63 61 6c 6c 69 6e  rMain and callin
29fc7 67 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  g sqlite3BtreeEn
29fc8 74 65 72 28 29 2e 20 46 6f 72 20 61 6e 20 65 72  ter(). For an er
29fc9 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 0a  ror that occurs.
29fca 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 61 74    ** before that
29fcb 20 70 6f 69 6e 74 2c 20 6a 75 6d 70 20 74 6f 20   point, jump to 
29fcc 65 72 72 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f 0a  error_out..  */.
29fcd 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75  initone_error_ou
29fce 74 3a 0a 20 20 69 66 28 20 6f 70 65 6e 65 64 54  t:.  if( openedT
29fcf 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20  ransaction ){.  
29fd0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f    sqlite3BtreeCo
29fd1 6d 6d 69 74 28 70 44 62 2d 3e 70 42 74 29 3b 0a  mmit(pDb->pBt);.
29fd2 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
29fd3 65 65 4c 65 61 76 65 28 70 44 62 2d 3e 70 42 74  eeLeave(pDb->pBt
29fd4 29 3b 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20  );..error_out:. 
29fd5 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
29fd6 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c  NOMEM || rc==SQL
29fd7 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
29fd8 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
29fd9 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
29fda 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
29fdb 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
29fdc 65 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66  e all database f
29fdd 69 6c 65 73 20 2d 20 74 68 65 20 6d 61 69 6e 20  iles - the main 
29fde 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
29fdf 68 65 20 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20  he file.** used 
29fe0 74 6f 20 73 74 6f 72 65 20 74 65 6d 70 6f 72 61  to store tempora
29fe1 72 79 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 61  ry tables, and a
29fe2 6e 79 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 61  ny additional da
29fe3 74 61 62 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20  tabase files.** 
29fe4 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 41 54  created using AT
29fe5 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 73 2e  TACH statements.
29fe6 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65    Return a succe
29fe7 73 73 20 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a  ss code.  If an.
29fe8 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  ** error occurs,
29fe9 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20   write an error 
29fea 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a  message into *pz
29feb 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66  ErrMsg..**.** Af
29fec 74 65 72 20 61 20 64 61 74 61 62 61 73 65 20 69  ter a database i
29fed 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74  s initialized, t
29fee 68 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  he DB_SchemaLoad
29fef 65 64 20 62 69 74 20 69 73 20 73 65 74 0a 2a 2a  ed bit is set.**
29ff0 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74   bit is set in t
29ff1 68 65 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f  he flags field o
29ff2 66 20 74 68 65 20 44 62 20 73 74 72 75 63 74 75  f the Db structu
29ff3 72 65 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  re. If the datab
29ff4 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20  ase.** file was 
29ff5 6f 66 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20  of zero-length, 
29ff6 74 68 65 6e 20 74 68 65 20 44 42 5f 45 6d 70 74  then the DB_Empt
29ff7 79 20 66 6c 61 67 20 69 73 20 61 6c 73 6f 20 73  y flag is also s
29ff8 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  et..*/.SQLITE_PR
29ff9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
29ffa 33 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  3Init(sqlite3 *d
29ffb 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  b, char **pzErrM
29ffc 73 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63  sg){.  int i, rc
29ffd 3b 0a 20 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69  ;.  int commit_i
29ffe 6e 74 65 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e  nternal = !(db->
29fff 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
2a000 65 72 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a  ernChanges);.  .
2a001 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2a002 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
2a003 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
2a004 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 64  = SQLITE_OK;.  d
2a005 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31  b->init.busy = 1
2a006 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ;.  for(i=0; rc=
2a007 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
2a008 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
2a009 20 20 20 69 66 28 20 44 62 48 61 73 50 72 6f 70     if( DbHasProp
2a00a 65 72 74 79 28 64 62 2c 20 69 2c 20 44 42 5f 53  erty(db, i, DB_S
2a00b 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 7c 7c 20  chemaLoaded) || 
2a00c 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b  i==1 ) continue;
2a00d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2a00e 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 69 2c 20  3InitOne(db, i, 
2a00f 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69  pzErrMsg);.    i
2a010 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73  f( rc ){.      s
2a011 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
2a012 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 69 29  nalSchema(db, i)
2a013 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
2a014 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f  * Once all the o
2a015 74 68 65 72 20 64 61 74 61 62 61 73 65 73 20 68  ther databases h
2a016 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ave been initial
2a017 69 73 65 64 2c 20 6c 6f 61 64 20 74 68 65 20 73  ised, load the s
2a018 63 68 65 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74  chema.  ** for t
2a019 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
2a01a 2e 20 54 68 69 73 20 69 73 20 6c 6f 61 64 65 64  . This is loaded
2a01b 20 6c 61 73 74 2c 20 61 73 20 74 68 65 20 54 45   last, as the TE
2a01c 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  MP database.  **
2a01d 20 73 63 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74   schema may cont
2a01e 61 69 6e 20 72 65 66 65 72 65 6e 63 65 73 20 74  ain references t
2a01f 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 6f 74 68  o objects in oth
2a020 65 72 20 64 61 74 61 62 61 73 65 73 2e 0a 20 20  er databases..  
2a021 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2a022 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20  E_OMIT_TEMPDB.  
2a023 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2a024 4b 20 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e  K && ALWAYS(db->
2a025 6e 44 62 3e 31 29 0a 20 20 20 20 20 20 20 20 20  nDb>1).         
2a026 20 20 20 20 20 20 20 20 20 20 20 26 26 20 21 44             && !D
2a027 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c  bHasProperty(db,
2a028 20 31 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61   1, DB_SchemaLoa
2a029 64 65 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  ded) ){.    rc =
2a02a 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28   sqlite3InitOne(
2a02b 64 62 2c 20 31 2c 20 70 7a 45 72 72 4d 73 67 29  db, 1, pzErrMsg)
2a02c 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
2a02d 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
2a02e 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2a02f 28 64 62 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20  (db, 1);.    }. 
2a030 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d   }.#endif..  db-
2a031 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a  >init.busy = 0;.
2a032 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2a033 5f 4f 4b 20 26 26 20 63 6f 6d 6d 69 74 5f 69 6e  _OK && commit_in
2a034 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71  ternal ){.    sq
2a035 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72  lite3CommitInter
2a036 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a  nalChanges(db);.
2a037 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
2a038 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ; .}../*.** This
2a039 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
2a03a 2d 6f 70 20 69 66 20 74 68 65 20 64 61 74 61 62  -op if the datab
2a03b 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 61 6c  ase schema is al
2a03c 72 65 61 64 79 20 69 6e 69 74 69 61 6c 69 73 65  ready initialise
2a03d 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  d..** Otherwise,
2a03e 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6c   the schema is l
2a03f 6f 61 64 65 64 2e 20 41 6e 20 65 72 72 6f 72 20  oaded. An error 
2a040 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
2a041 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2a042 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
2a043 65 61 64 53 63 68 65 6d 61 28 50 61 72 73 65 20  eadSchema(Parse 
2a044 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20  *pParse){.  int 
2a045 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
2a046 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2a047 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
2a048 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
2a049 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
2a04a 65 78 29 20 29 3b 0a 20 20 69 66 28 20 21 64 62  ex) );.  if( !db
2a04b 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2a04c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
2a04d 6e 69 74 28 64 62 2c 20 26 70 50 61 72 73 65 2d  nit(db, &pParse-
2a04e 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 20  >zErrMsg);.  }. 
2a04f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2a050 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  OK ){.    pParse
2a051 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 70  ->rc = rc;.    p
2a052 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
2a053 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
2a054 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  }.../*.** Check 
2a055 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 69  schema cookies i
2a056 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 2e  n all databases.
2a057 20 20 49 66 20 61 6e 79 20 63 6f 6f 6b 69 65 20    If any cookie 
2a058 69 73 20 6f 75 74 0a 2a 2a 20 6f 66 20 64 61 74  is out.** of dat
2a059 65 20 73 65 74 20 70 50 61 72 73 65 2d 3e 72 63  e set pParse->rc
2a05a 20 74 6f 20 53 51 4c 49 54 45 5f 53 43 48 45 4d   to SQLITE_SCHEM
2a05b 41 2e 20 20 49 66 20 61 6c 6c 20 73 63 68 65 6d  A.  If all schem
2a05c 61 20 63 6f 6f 6b 69 65 73 0a 2a 2a 20 6d 61 6b  a cookies.** mak
2a05d 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 20  e no changes to 
2a05e 70 50 61 72 73 65 2d 3e 72 63 2e 0a 2a 2f 0a 73  pParse->rc..*/.s
2a05f 74 61 74 69 63 20 76 6f 69 64 20 73 63 68 65 6d  tatic void schem
2a060 61 49 73 56 61 6c 69 64 28 50 61 72 73 65 20 2a  aIsValid(Parse *
2a061 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74  pParse){.  sqlit
2a062 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2a063 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  >db;.  int iDb;.
2a064 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
2a065 63 6f 6f 6b 69 65 3b 0a 0a 20 20 61 73 73 65 72  cookie;..  asser
2a066 74 28 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b  t( pParse->check
2a067 53 63 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65  Schema );.  asse
2a068 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
2a069 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
2a06a 29 20 29 3b 0a 20 20 66 6f 72 28 69 44 62 3d 30  ) );.  for(iDb=0
2a06b 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69  ; iDb<db->nDb; i
2a06c 44 62 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6f  Db++){.    int o
2a06d 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f 6e  penedTransaction
2a06e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
2a06f 20 54 72 75 65 20 69 66 20 61 20 74 72 61 6e 73   True if a trans
2a070 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64  action is opened
2a071 20 2a 2f 0a 20 20 20 20 42 74 72 65 65 20 2a 70   */.    Btree *p
2a072 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Bt = db->aDb[iDb
2a073 5d 2e 70 42 74 3b 20 20 20 20 20 2f 2a 20 42 74  ].pBt;     /* Bt
2a074 72 65 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  ree database to 
2a075 72 65 61 64 20 63 6f 6f 6b 69 65 20 66 72 6f 6d  read cookie from
2a076 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 42 74 3d   */.    if( pBt=
2a077 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  =0 ) continue;..
2a078 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
2a079 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61  is not already a
2a07a 20 72 65 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72   read-only (or r
2a07b 65 61 64 2d 77 72 69 74 65 29 20 74 72 61 6e 73  ead-write) trans
2a07c 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 0a 20 20  action opened.  
2a07d 20 20 2a 2a 20 6f 6e 20 74 68 65 20 62 2d 74 72    ** on the b-tr
2a07e 65 65 20 64 61 74 61 62 61 73 65 2c 20 6f 70 65  ee database, ope
2a07f 6e 20 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20  n one now. If a 
2a080 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
2a081 70 65 6e 65 64 2c 20 69 74 20 0a 20 20 20 20 2a  pened, it .    *
2a082 2a 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64  * will be closed
2a083 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74   immediately aft
2a084 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 6d  er reading the m
2a085 65 74 61 2d 76 61 6c 75 65 2e 20 2a 2f 0a 20 20  eta-value. */.  
2a086 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42 74    if( !sqlite3Bt
2a087 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
2a088 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72  (pBt) ){.      r
2a089 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
2a08a 42 65 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20  BeginTrans(pBt, 
2a08b 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
2a08c 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c  ==SQLITE_NOMEM |
2a08d 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  | rc==SQLITE_IOE
2a08e 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  RR_NOMEM ){.    
2a08f 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
2a090 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
2a091 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  }.      if( rc!=
2a092 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2a093 72 6e 3b 0a 20 20 20 20 20 20 6f 70 65 6e 65 64  rn;.      opened
2a094 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 31 3b  Transaction = 1;
2a095 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52  .    }..    /* R
2a096 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 63  ead the schema c
2a097 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64  ookie from the d
2a098 61 74 61 62 61 73 65 2e 20 49 66 20 69 74 20 64  atabase. If it d
2a099 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68  oes not match th
2a09a 65 20 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 20  e .    ** value 
2a09b 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f  stored as part o
2a09c 66 20 74 68 65 20 69 6e 20 74 68 65 20 69 6e 2d  f the in the in-
2a09d 6d 65 6d 6f 72 79 20 73 63 68 65 6d 61 20 72 65  memory schema re
2a09e 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 0a 20 20  presentation,.  
2a09f 20 20 2a 2a 20 73 65 74 20 50 61 72 73 65 2e 72    ** set Parse.r
2a0a0 63 20 74 6f 20 53 51 4c 49 54 45 5f 53 43 48 45  c to SQLITE_SCHE
2a0a1 4d 41 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  MA. */.    sqlit
2a0a2 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
2a0a3 42 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41  Bt, BTREE_SCHEMA
2a0a4 5f 56 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a  _VERSION, (u32 *
2a0a5 29 26 63 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 69  )&cookie);.    i
2a0a6 66 28 20 63 6f 6f 6b 69 65 21 3d 64 62 2d 3e 61  f( cookie!=db->a
2a0a7 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
2a0a8 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 29  >schema_cookie )
2a0a9 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
2a0aa 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45  rc = SQLITE_SCHE
2a0ab 4d 41 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  MA;.    }..    /
2a0ac 2a 20 43 6c 6f 73 65 20 74 68 65 20 74 72 61 6e  * Close the tran
2a0ad 73 61 63 74 69 6f 6e 2c 20 69 66 20 6f 6e 65 20  saction, if one 
2a0ae 77 61 73 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20  was opened. */. 
2a0af 20 20 20 69 66 28 20 6f 70 65 6e 65 64 54 72 61     if( openedTra
2a0b0 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nsaction ){.    
2a0b1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f    sqlite3BtreeCo
2a0b2 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 7d  mmit(pBt);.    }
2a0b3 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
2a0b4 6e 76 65 72 74 20 61 20 73 63 68 65 6d 61 20 70  nvert a schema p
2a0b5 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20  ointer into the 
2a0b6 69 44 62 20 69 6e 64 65 78 20 74 68 61 74 20 69  iDb index that i
2a0b7 6e 64 69 63 61 74 65 73 0a 2a 2a 20 77 68 69 63  ndicates.** whic
2a0b8 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  h database file 
2a0b9 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 74 68 65  in db->aDb[] the
2a0ba 20 73 63 68 65 6d 61 20 72 65 66 65 72 73 20 74   schema refers t
2a0bb 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  o..**.** If the 
2a0bc 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 69 73  same database is
2a0bd 20 61 74 74 61 63 68 65 64 20 6d 6f 72 65 20 74   attached more t
2a0be 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 66 69  han once, the fi
2a0bf 72 73 74 0a 2a 2a 20 61 74 74 61 63 68 65 64 20  rst.** attached 
2a0c0 64 61 74 61 62 61 73 65 20 69 73 20 72 65 74 75  database is retu
2a0c1 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
2a0c2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2a0c3 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2a0c4 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63  (sqlite3 *db, Sc
2a0c5 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 29 7b 0a  hema *pSchema){.
2a0c6 20 20 69 6e 74 20 69 20 3d 20 2d 31 30 30 30 30    int i = -10000
2a0c7 30 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 53 63  00;..  /* If pSc
2a0c8 68 65 6d 61 20 69 73 20 4e 55 4c 4c 2c 20 74 68  hema is NULL, th
2a0c9 65 6e 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30  en return -10000
2a0ca 30 30 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73  00. This happens
2a0cb 20 77 68 65 6e 20 63 6f 64 65 20 69 6e 20 0a 20   when code in . 
2a0cc 20 2a 2a 20 65 78 70 72 2e 63 20 69 73 20 74 72   ** expr.c is tr
2a0cd 79 69 6e 67 20 74 6f 20 72 65 73 6f 6c 76 65 20  ying to resolve 
2a0ce 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61  a reference to a
2a0cf 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65   transient table
2a0d0 20 28 69 2e 65 2e 20 6f 6e 65 0a 20 20 2a 2a 20   (i.e. one.  ** 
2a0d1 63 72 65 61 74 65 64 20 62 79 20 61 20 73 75 62  created by a sub
2a0d2 2d 73 65 6c 65 63 74 29 2e 20 49 6e 20 74 68 69  -select). In thi
2a0d3 73 20 63 61 73 65 20 74 68 65 20 72 65 74 75 72  s case the retur
2a0d4 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  n value of this 
2a0d5 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73  .  ** function s
2a0d6 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 75  hould never be u
2a0d7 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  sed..  **.  ** W
2a0d8 65 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30  e return -100000
2a0d9 30 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  0 instead of the
2a0da 20 6d 6f 72 65 20 75 73 75 61 6c 20 2d 31 20 73   more usual -1 s
2a0db 69 6d 70 6c 79 20 62 65 63 61 75 73 65 20 75 73  imply because us
2a0dc 69 6e 67 0a 20 20 2a 2a 20 2d 31 30 30 30 30 30  ing.  ** -100000
2a0dd 30 20 61 73 20 74 68 65 20 69 6e 63 6f 72 72 65  0 as the incorre
2a0de 63 74 20 69 6e 64 65 78 20 69 6e 74 6f 20 64 62  ct index into db
2a0df 2d 3e 61 44 62 5b 5d 20 69 73 20 6d 75 63 68 20  ->aDb[] is much 
2a0e0 0a 20 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65 6c  .  ** more likel
2a0e1 79 20 74 6f 20 63 61 75 73 65 20 61 20 73 65 67  y to cause a seg
2a0e2 66 61 75 6c 74 20 74 68 61 6e 20 2d 31 20 28 6f  fault than -1 (o
2a0e3 66 20 63 6f 75 72 73 65 20 74 68 65 72 65 20 61  f course there a
2a0e4 72 65 20 61 73 73 65 72 74 28 29 0a 20 20 2a 2a  re assert().  **
2a0e5 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 6f 2c   statements too,
2a0e6 20 62 75 74 20 69 74 20 6e 65 76 65 72 20 68 75   but it never hu
2a0e7 72 74 73 20 74 6f 20 70 6c 61 79 20 74 68 65 20  rts to play the 
2a0e8 6f 64 64 73 29 2e 0a 20 20 2a 2f 0a 20 20 61 73  odds)..  */.  as
2a0e9 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
2a0ea 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
2a0eb 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 53 63  ex) );.  if( pSc
2a0ec 68 65 6d 61 20 29 7b 0a 20 20 20 20 66 6f 72 28  hema ){.    for(
2a0ed 69 3d 30 3b 20 41 4c 57 41 59 53 28 69 3c 64 62  i=0; ALWAYS(i<db
2a0ee 2d 3e 6e 44 62 29 3b 20 69 2b 2b 29 7b 0a 20 20  ->nDb); i++){.  
2a0ef 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
2a0f0 69 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68  i].pSchema==pSch
2a0f1 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 62  ema ){.        b
2a0f2 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2a0f3 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
2a0f4 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d 3e 6e 44  i>=0 && i<db->nD
2a0f5 62 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  b );.  }.  retur
2a0f6 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n i;.}../*.** Co
2a0f7 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 38 20  mpile the UTF-8 
2a0f8 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
2a0f9 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20  ement zSql into 
2a0fa 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  a statement hand
2a0fb 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
2a0fc 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  t sqlite3Prepare
2a0fd 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2a0fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a0ff 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
2a100 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  . */.  const cha
2a101 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
2a102 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
2a103 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
2a104 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
2a105 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2a106 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
2a107 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
2a108 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67   int saveSqlFlag
2a109 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ,          /* Tr
2a10a 75 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74  ue to copy SQL t
2a10b 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ext into the sql
2a10c 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 56  ite3_stmt */.  V
2a10d 64 62 65 20 2a 70 52 65 70 72 65 70 61 72 65 2c  dbe *pReprepare,
2a10e 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 62           /* VM b
2a10f 65 69 6e 67 20 72 65 70 72 65 70 61 72 65 64 20  eing reprepared 
2a110 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
2a111 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
2a112 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
2a113 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
2a114 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2a115 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
2a116 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ail       /* OUT
2a117 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20  : End of parsed 
2a118 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50  string */.){.  P
2a119 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
2a11a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2a11b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2a11c 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d   char *zErrMsg =
2a11d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 72   0;        /* Er
2a11e 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20  ror message */. 
2a11f 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2a120 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  _OK;       /* Re
2a121 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 69  sult code */.  i
2a122 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2a123 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2a124 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 2f   counter */..  /
2a125 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 70  * Allocate the p
2a126 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2a127 2f 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c  /.  pParse = sql
2a128 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65  ite3StackAllocZe
2a129 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  ro(db, sizeof(*p
2a12a 50 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70  Parse));.  if( p
2a12b 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20  Parse==0 ){.    
2a12c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2a12d 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  M;.    goto end_
2a12e 70 72 65 70 61 72 65 3b 0a 20 20 7d 0a 20 20 70  prepare;.  }.  p
2a12f 50 61 72 73 65 2d 3e 70 52 65 70 72 65 70 61 72  Parse->pReprepar
2a130 65 20 3d 20 70 52 65 70 72 65 70 61 72 65 3b 0a  e = pReprepare;.
2a131 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
2a132 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20  fetyOn(db) ){.  
2a133 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49    rc = SQLITE_MI
2a134 53 55 53 45 3b 0a 20 20 20 20 67 6f 74 6f 20 65  SUSE;.    goto e
2a135 6e 64 5f 70 72 65 70 61 72 65 3b 0a 20 20 7d 0a  nd_prepare;.  }.
2a136 20 20 61 73 73 65 72 74 28 20 70 70 53 74 6d 74    assert( ppStmt
2a137 20 26 26 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29   && *ppStmt==0 )
2a138 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
2a139 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
2a13a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2a13b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
2a13c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f  ->mutex) );..  /
2a13d 2a 20 43 68 65 63 6b 20 74 6f 20 76 65 72 69 66  * Check to verif
2a13e 79 20 74 68 61 74 20 69 74 20 69 73 20 70 6f 73  y that it is pos
2a13f 73 69 62 6c 65 20 74 6f 20 67 65 74 20 61 20 72  sible to get a r
2a140 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 0a  ead lock on all.
2a141 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63    ** database sc
2a142 68 65 6d 61 73 2e 20 20 54 68 65 20 69 6e 61 62  hemas.  The inab
2a143 69 6c 69 74 79 20 74 6f 20 67 65 74 20 61 20 72  ility to get a r
2a144 65 61 64 20 6c 6f 63 6b 20 69 6e 64 69 63 61 74  ead lock indicat
2a145 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 73 6f 6d  es that.  ** som
2a146 65 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  e other database
2a147 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68   connection is h
2a148 6f 6c 64 69 6e 67 20 61 20 77 72 69 74 65 2d 6c  olding a write-l
2a149 6f 63 6b 2c 20 77 68 69 63 68 20 69 6e 0a 20 20  ock, which in.  
2a14a 2a 2a 20 74 75 72 6e 20 6d 65 61 6e 73 20 74 68  ** turn means th
2a14b 61 74 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e  at the other con
2a14c 6e 65 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65  nection has made
2a14d 20 75 6e 63 6f 6d 6d 69 74 74 65 64 20 63 68 61   uncommitted cha
2a14e 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65  nges.  ** to the
2a14f 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2a 0a 20 20   schema..  **.  
2a150 2a 2a 20 57 65 72 65 20 77 65 20 74 6f 20 70 72  ** Were we to pr
2a151 6f 63 65 65 64 20 61 6e 64 20 70 72 65 70 61 72  oceed and prepar
2a152 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  e the statement 
2a153 61 67 61 69 6e 73 74 20 74 68 65 20 75 6e 63 6f  against the unco
2a154 6d 6d 69 74 74 65 64 0a 20 20 2a 2a 20 73 63 68  mmitted.  ** sch
2a155 65 6d 61 20 63 68 61 6e 67 65 73 20 61 6e 64 20  ema changes and 
2a156 69 66 20 74 68 6f 73 65 20 73 63 68 65 6d 61 20  if those schema 
2a157 63 68 61 6e 67 65 73 20 61 72 65 20 73 75 62 73  changes are subs
2a158 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 0a  equently rolled.
2a159 20 20 2a 2a 20 62 61 63 6b 20 61 6e 64 20 64 69    ** back and di
2a15a 66 66 65 72 65 6e 74 20 63 68 61 6e 67 65 73 20  fferent changes 
2a15b 61 72 65 20 6d 61 64 65 20 69 6e 20 74 68 65 69  are made in thei
2a15c 72 20 70 6c 61 63 65 2c 20 74 68 65 6e 20 77 68  r place, then wh
2a15d 65 6e 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65  en this.  ** pre
2a15e 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
2a15f 67 6f 65 73 20 74 6f 20 72 75 6e 20 74 68 65 20  goes to run the 
2a160 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 77 6f  schema cookie wo
2a161 75 6c 64 20 66 61 69 6c 20 74 6f 20 64 65 74 65  uld fail to dete
2a162 63 74 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65  ct.  ** the sche
2a163 6d 61 20 63 68 61 6e 67 65 2e 20 20 44 69 73 61  ma change.  Disa
2a164 73 74 65 72 20 77 6f 75 6c 64 20 66 6f 6c 6c 6f  ster would follo
2a165 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  w..  **.  ** Thi
2a166 73 20 74 68 72 65 61 64 20 69 73 20 63 75 72 72  s thread is curr
2a167 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 6d 75  ently holding mu
2a168 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20 42 74 72  texes on all Btr
2a169 65 65 73 20 28 62 65 63 61 75 73 65 0a 20 20 2a  ees (because.  *
2a16a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
2a16b 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20  BtreeEnterAll() 
2a16c 69 6e 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e  in sqlite3LockAn
2a16d 64 50 72 65 70 61 72 65 28 29 29 20 73 6f 20 69  dPrepare()) so i
2a16e 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f  t.  ** is not po
2a16f 73 73 69 62 6c 65 20 66 6f 72 20 61 6e 6f 74 68  ssible for anoth
2a170 65 72 20 74 68 72 65 61 64 20 74 6f 20 73 74 61  er thread to sta
2a171 72 74 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20  rt a new schema 
2a172 63 68 61 6e 67 65 0a 20 20 2a 2a 20 77 68 69 6c  change.  ** whil
2a173 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
2a174 73 20 72 75 6e 6e 69 6e 67 2e 20 20 48 65 6e 63  s running.  Henc
2a175 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65  e, we do not nee
2a176 64 20 74 6f 20 68 6f 6c 64 20 0a 20 20 2a 2a 20  d to hold .  ** 
2a177 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 63 68  locks on the sch
2a178 65 6d 61 2c 20 77 65 20 6a 75 73 74 20 6e 65 65  ema, we just nee
2a179 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 6e  d to make sure n
2a17a 6f 62 6f 64 79 20 65 6c 73 65 20 69 73 20 0a 20  obody else is . 
2a17b 20 2a 2a 20 68 6f 6c 64 69 6e 67 20 74 68 65 6d   ** holding them
2a17c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ..  **.  ** Note
2a17d 20 74 68 61 74 20 73 65 74 74 69 6e 67 20 52 45   that setting RE
2a17e 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 6f  AD_UNCOMMITTED o
2a17f 76 65 72 72 69 64 65 73 20 6d 6f 73 74 20 6c 6f  verrides most lo
2a180 63 6b 20 64 65 74 65 63 74 69 6f 6e 2c 0a 20 20  ck detection,.  
2a181 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 20 2a  ** but it does *
2a182 6e 6f 74 2a 20 6f 76 65 72 72 69 64 65 20 73 63  not* override sc
2a183 68 65 6d 61 20 6c 6f 63 6b 20 64 65 74 65 63 74  hema lock detect
2a184 69 6f 6e 2c 20 73 6f 20 74 68 69 73 20 61 6c 6c  ion, so this all
2a185 20 73 74 69 6c 6c 0a 20 20 2a 2a 20 77 6f 72 6b   still.  ** work
2a186 73 20 65 76 65 6e 20 69 66 20 52 45 41 44 5f 55  s even if READ_U
2a187 4e 43 4f 4d 4d 49 54 54 45 44 20 69 73 20 73 65  NCOMMITTED is se
2a188 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  t..  */.  for(i=
2a189 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
2a18a 2b 29 20 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  +) {.    Btree *
2a18b 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
2a18c 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42  .pBt;.    if( pB
2a18d 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t ){.      asser
2a18e 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
2a18f 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 29 20 29  oldsMutex(pBt) )
2a190 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
2a191 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c  ite3BtreeSchemaL
2a192 6f 63 6b 65 64 28 70 42 74 29 3b 0a 20 20 20 20  ocked(pBt);.    
2a193 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
2a194 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2a195 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  zDb = db->aDb[i]
2a196 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
2a197 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2a198 20 72 63 2c 20 22 64 61 74 61 62 61 73 65 20 73   rc, "database s
2a199 63 68 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a  chema is locked:
2a19a 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20   %s", zDb);.    
2a19b 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
2a19c 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
2a19d 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
2a19e 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
2a19f 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
2a1a0 74 74 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  tted );.        
2a1a1 67 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65  goto end_prepare
2a1a2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2a1a3 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 74    }..  sqlite3Vt
2a1a4 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29  abUnlockList(db)
2a1a5 3b 0a 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 20  ;..  pParse->db 
2a1a6 3d 20 64 62 3b 0a 20 20 69 66 28 20 6e 42 79 74  = db;.  if( nByt
2a1a7 65 73 3e 3d 30 20 26 26 20 28 6e 42 79 74 65 73  es>=0 && (nBytes
2a1a8 3d 3d 30 20 7c 7c 20 7a 53 71 6c 5b 6e 42 79 74  ==0 || zSql[nByt
2a1a9 65 73 2d 31 5d 21 3d 30 29 20 29 7b 0a 20 20 20  es-1]!=0) ){.   
2a1aa 20 63 68 61 72 20 2a 7a 53 71 6c 43 6f 70 79 3b   char *zSqlCopy;
2a1ab 0a 20 20 20 20 69 6e 74 20 6d 78 4c 65 6e 20 3d  .    int mxLen =
2a1ac 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
2a1ad 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e  TE_LIMIT_SQL_LEN
2a1ae 47 54 48 5d 3b 0a 20 20 20 20 74 65 73 74 63 61  GTH];.    testca
2a1af 73 65 28 20 6e 42 79 74 65 73 3d 3d 6d 78 4c 65  se( nBytes==mxLe
2a1b0 6e 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  n );.    testcas
2a1b1 65 28 20 6e 42 79 74 65 73 3d 3d 6d 78 4c 65 6e  e( nBytes==mxLen
2a1b2 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 42  +1 );.    if( nB
2a1b3 79 74 65 73 3e 6d 78 4c 65 6e 20 29 7b 0a 20 20  ytes>mxLen ){.  
2a1b4 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2a1b5 28 64 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f 42  (db, SQLITE_TOOB
2a1b6 49 47 2c 20 22 73 74 61 74 65 6d 65 6e 74 20 74  IG, "statement t
2a1b7 6f 6f 20 6c 6f 6e 67 22 29 3b 0a 20 20 20 20 20  oo long");.     
2a1b8 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2a1b9 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
2a1ba 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
2a1bb 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54  piExit(db, SQLIT
2a1bc 45 5f 54 4f 4f 42 49 47 29 3b 0a 20 20 20 20 20  E_TOOBIG);.     
2a1bd 20 67 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72   goto end_prepar
2a1be 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71  e;.    }.    zSq
2a1bf 6c 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 44  lCopy = sqlite3D
2a1c0 62 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 53 71  bStrNDup(db, zSq
2a1c1 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  l, nBytes);.    
2a1c2 69 66 28 20 7a 53 71 6c 43 6f 70 79 20 29 7b 0a  if( zSqlCopy ){.
2a1c3 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e        sqlite3Run
2a1c4 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a  Parser(pParse, z
2a1c5 53 71 6c 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73  SqlCopy, &zErrMs
2a1c6 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
2a1c7 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c  3DbFree(db, zSql
2a1c8 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 70 50 61  Copy);.      pPa
2a1c9 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53  rse->zTail = &zS
2a1ca 71 6c 5b 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c  ql[pParse->zTail
2a1cb 2d 7a 53 71 6c 43 6f 70 79 5d 3b 0a 20 20 20 20  -zSqlCopy];.    
2a1cc 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61  }else{.      pPa
2a1cd 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53  rse->zTail = &zS
2a1ce 71 6c 5b 6e 42 79 74 65 73 5d 3b 0a 20 20 20 20  ql[nBytes];.    
2a1cf 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  }.  }else{.    s
2a1d0 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28  qlite3RunParser(
2a1d1 70 50 61 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a  pParse, zSql, &z
2a1d2 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 0a 20 20  ErrMsg);.  }..  
2a1d3 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2a1d4 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72  iled ){.    pPar
2a1d5 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
2a1d6 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
2a1d7 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c   pParse->rc==SQL
2a1d8 49 54 45 5f 44 4f 4e 45 20 29 20 70 50 61 72 73  ITE_DONE ) pPars
2a1d9 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  e->rc = SQLITE_O
2a1da 4b 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  K;.  if( pParse-
2a1db 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 29 7b 0a  >checkSchema ){.
2a1dc 20 20 20 20 73 63 68 65 6d 61 49 73 56 61 6c 69      schemaIsVali
2a1dd 64 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20  d(pParse);.  }. 
2a1de 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d   if( pParse->rc=
2a1df 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29  =SQLITE_SCHEMA )
2a1e0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  {.    sqlite3Res
2a1e1 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2a1e2 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  (db, 0);.  }.  i
2a1e3 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2a1e4 6c 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73  led ){.    pPars
2a1e5 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  e->rc = SQLITE_N
2a1e6 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
2a1e7 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2a 70  pzTail ){.    *p
2a1e8 7a 54 61 69 6c 20 3d 20 70 50 61 72 73 65 2d 3e  zTail = pParse->
2a1e9 7a 54 61 69 6c 3b 0a 20 20 7d 0a 20 20 72 63 20  zTail;.  }.  rc 
2a1ea 3d 20 70 50 61 72 73 65 2d 3e 72 63 3b 0a 0a 23  = pParse->rc;..#
2a1eb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2a1ec 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28  IT_EXPLAIN.  if(
2a1ed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
2a1ee 26 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  & pParse->pVdbe 
2a1ef 26 26 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  && pParse->expla
2a1f0 69 6e 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  in ){.    static
2a1f1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f   const char * co
2a1f2 6e 73 74 20 61 7a 43 6f 6c 4e 61 6d 65 5b 5d 20  nst azColName[] 
2a1f3 3d 20 7b 0a 20 20 20 20 20 20 20 22 61 64 64 72  = {.       "addr
2a1f4 22 2c 20 22 6f 70 63 6f 64 65 22 2c 20 22 70 31  ", "opcode", "p1
2a1f5 22 2c 20 22 70 32 22 2c 20 22 70 33 22 2c 20 22  ", "p2", "p3", "
2a1f6 70 34 22 2c 20 22 70 35 22 2c 20 22 63 6f 6d 6d  p4", "p5", "comm
2a1f7 65 6e 74 22 2c 0a 20 20 20 20 20 20 20 22 6f 72  ent",.       "or
2a1f8 64 65 72 22 2c 20 22 66 72 6f 6d 22 2c 20 22 64  der", "from", "d
2a1f9 65 74 61 69 6c 22 0a 20 20 20 20 7d 3b 0a 20 20  etail".    };.  
2a1fa 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20 6d 78    int iFirst, mx
2a1fb 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  ;.    if( pParse
2a1fc 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a  ->explain==2 ){.
2a1fd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a1fe 65 53 65 74 4e 75 6d 43 6f 6c 73 28 70 50 61 72  eSetNumCols(pPar
2a1ff 73 65 2d 3e 70 56 64 62 65 2c 20 33 29 3b 0a 20  se->pVdbe, 3);. 
2a200 20 20 20 20 20 69 46 69 72 73 74 20 3d 20 38 3b       iFirst = 8;
2a201 0a 20 20 20 20 20 20 6d 78 20 3d 20 31 31 3b 0a  .      mx = 11;.
2a202 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2a203 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
2a204 75 6d 43 6f 6c 73 28 70 50 61 72 73 65 2d 3e 70  umCols(pParse->p
2a205 56 64 62 65 2c 20 38 29 3b 0a 20 20 20 20 20 20  Vdbe, 8);.      
2a206 69 46 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20  iFirst = 0;.    
2a207 20 20 6d 78 20 3d 20 38 3b 0a 20 20 20 20 7d 0a    mx = 8;.    }.
2a208 20 20 20 20 66 6f 72 28 69 3d 69 46 69 72 73 74      for(i=iFirst
2a209 3b 20 69 3c 6d 78 3b 20 69 2b 2b 29 7b 0a 20 20  ; i<mx; i++){.  
2a20a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2a20b 65 74 43 6f 6c 4e 61 6d 65 28 70 50 61 72 73 65  etColName(pParse
2a20c 2d 3e 70 56 64 62 65 2c 20 69 2d 69 46 69 72 73  ->pVdbe, i-iFirs
2a20d 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  t, COLNAME_NAME,
2a20e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a20f 20 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 43               azC
2a210 6f 6c 4e 61 6d 65 5b 69 5d 2c 20 53 51 4c 49 54  olName[i], SQLIT
2a211 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d  E_STATIC);.    }
2a212 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
2a213 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
2a214 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72  Off(db) ){.    r
2a215 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
2a216 45 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  E;.  }..  assert
2a217 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  ( db->init.busy=
2a218 3d 30 20 7c 7c 20 73 61 76 65 53 71 6c 46 6c 61  =0 || saveSqlFla
2a219 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 62  g==0 );.  if( db
2a21a 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29  ->init.busy==0 )
2a21b 7b 0a 20 20 20 20 56 64 62 65 20 2a 70 56 64 62  {.    Vdbe *pVdb
2a21c 65 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  e = pParse->pVdb
2a21d 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  e;.    sqlite3Vd
2a21e 62 65 53 65 74 53 71 6c 28 70 56 64 62 65 2c 20  beSetSql(pVdbe, 
2a21f 7a 53 71 6c 2c 20 28 69 6e 74 29 28 70 50 61 72  zSql, (int)(pPar
2a220 73 65 2d 3e 7a 54 61 69 6c 2d 7a 53 71 6c 29 2c  se->zTail-zSql),
2a221 20 73 61 76 65 53 71 6c 46 6c 61 67 29 3b 0a 20   saveSqlFlag);. 
2a222 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d   }.  if( pParse-
2a223 3e 70 56 64 62 65 20 26 26 20 28 72 63 21 3d 53  >pVdbe && (rc!=S
2a224 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e  QLITE_OK || db->
2a225 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b  mallocFailed) ){
2a226 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a227 46 69 6e 61 6c 69 7a 65 28 70 50 61 72 73 65 2d  Finalize(pParse-
2a228 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 61 73 73  >pVdbe);.    ass
2a229 65 72 74 28 21 28 2a 70 70 53 74 6d 74 29 29 3b  ert(!(*ppStmt));
2a22a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
2a22b 70 53 74 6d 74 20 3d 20 28 73 71 6c 69 74 65 33  pStmt = (sqlite3
2a22c 5f 73 74 6d 74 2a 29 70 50 61 72 73 65 2d 3e 70  _stmt*)pParse->p
2a22d 56 64 62 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  Vdbe;.  }..  if(
2a22e 20 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20   zErrMsg ){.    
2a22f 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2a230 20 72 63 2c 20 22 25 73 22 2c 20 7a 45 72 72 4d   rc, "%s", zErrM
2a231 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sg);.    sqlite3
2a232 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d  DbFree(db, zErrM
2a233 73 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  sg);.  }else{.  
2a234 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2a235 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a  b, rc, 0);.  }..
2a236 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20    /* Delete any 
2a237 54 72 69 67 67 65 72 50 72 67 20 73 74 72 75 63  TriggerPrg struc
2a238 74 75 72 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  tures allocated 
2a239 77 68 69 6c 65 20 70 61 72 73 69 6e 67 20 74 68  while parsing th
2a23a 69 73 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  is statement. */
2a23b 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73 65  .  while( pParse
2a23c 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20 29 7b  ->pTriggerPrg ){
2a23d 0a 20 20 20 20 54 72 69 67 67 65 72 50 72 67 20  .    TriggerPrg 
2a23e 2a 70 54 20 3d 20 70 50 61 72 73 65 2d 3e 70 54  *pT = pParse->pT
2a23f 72 69 67 67 65 72 50 72 67 3b 0a 20 20 20 20 70  riggerPrg;.    p
2a240 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50  Parse->pTriggerP
2a241 72 67 20 3d 20 70 54 2d 3e 70 4e 65 78 74 3b 0a  rg = pT->pNext;.
2a242 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
2a243 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c  rogramDelete(db,
2a244 20 70 54 2d 3e 70 50 72 6f 67 72 61 6d 2c 20 30   pT->pProgram, 0
2a245 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
2a246 46 72 65 65 28 64 62 2c 20 70 54 29 3b 0a 20 20  Free(db, pT);.  
2a247 7d 0a 0a 65 6e 64 5f 70 72 65 70 61 72 65 3a 0a  }..end_prepare:.
2a248 0a 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46  .  sqlite3StackF
2a249 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b  ree(db, pParse);
2a24a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
2a24b 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
2a24c 20 20 61 73 73 65 72 74 28 20 28 72 63 26 64 62    assert( (rc&db
2a24d 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29  ->errMask)==rc )
2a24e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2a24f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
2a250 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72  te3LockAndPrepar
2a251 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2a252 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2a253 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
2a254 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  e. */.  const ch
2a255 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
2a256 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
2a257 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
2a258 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
2a259 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
2a25a 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
2a25b 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
2a25c 20 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61    int saveSqlFla
2a25d 67 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  g,          /* T
2a25e 72 75 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20  rue to copy SQL 
2a25f 74 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71  text into the sq
2a260 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20  lite3_stmt */.  
2a261 56 64 62 65 20 2a 70 4f 6c 64 2c 20 20 20 20 20  Vdbe *pOld,     
2a262 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20            /* VM 
2a263 62 65 69 6e 67 20 72 65 70 72 65 70 61 72 65 64  being reprepared
2a264 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
2a265 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
2a266 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
2a267 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
2a268 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
2a269 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
2a26a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
2a26b 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
2a26c 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
2a26d 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
2a26e 28 20 70 70 53 74 6d 74 21 3d 30 20 29 3b 0a 20  ( ppStmt!=0 );. 
2a26f 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20   *ppStmt = 0;.  
2a270 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65  if( !sqlite3Safe
2a271 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b  tyCheckOk(db) ){
2a272 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2a273 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
2a274 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2a275 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2a276 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
2a277 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72  nterAll(db);.  r
2a278 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61  c = sqlite3Prepa
2a279 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79  re(db, zSql, nBy
2a27a 74 65 73 2c 20 73 61 76 65 53 71 6c 46 6c 61 67  tes, saveSqlFlag
2a27b 2c 20 70 4f 6c 64 2c 20 70 70 53 74 6d 74 2c 20  , pOld, ppStmt, 
2a27c 70 7a 54 61 69 6c 29 3b 0a 20 20 69 66 28 20 72  pzTail);.  if( r
2a27d 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c==SQLITE_SCHEMA
2a27e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2a27f 66 69 6e 61 6c 69 7a 65 28 2a 70 70 53 74 6d 74  finalize(*ppStmt
2a280 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
2a281 74 65 33 50 72 65 70 61 72 65 28 64 62 2c 20 7a  te3Prepare(db, z
2a282 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73 61 76  Sql, nBytes, sav
2a283 65 53 71 6c 46 6c 61 67 2c 20 70 4f 6c 64 2c 20  eSqlFlag, pOld, 
2a284 70 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29 3b  ppStmt, pzTail);
2a285 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
2a286 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
2a287 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2a288 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2a289 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2a28a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 72 75 6e 20 74  }../*.** Rerun t
2a28b 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f  he compilation o
2a28c 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66  f a statement af
2a28d 74 65 72 20 61 20 73 63 68 65 6d 61 20 63 68 61  ter a schema cha
2a28e 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  nge..**.** If th
2a28f 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73  e statement is s
2a290 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 63 6f  uccessfully reco
2a291 6d 70 69 6c 65 64 2c 20 72 65 74 75 72 6e 20 53  mpiled, return S
2a292 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77  QLITE_OK. Otherw
2a293 69 73 65 2c 0a 2a 2a 20 69 66 20 74 68 65 20 73  ise,.** if the s
2a294 74 61 74 65 6d 65 6e 74 20 63 61 6e 6e 6f 74 20  tatement cannot 
2a295 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 62 65  be recompiled be
2a296 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20 63 6f  cause another co
2a297 6e 6e 65 63 74 69 6f 6e 20 68 61 73 0a 2a 2a 20  nnection has.** 
2a298 6c 6f 63 6b 65 64 20 74 68 65 20 73 71 6c 69 74  locked the sqlit
2a299 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2c  e3_master table,
2a29a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
2a29b 4f 43 4b 45 44 2e 20 49 66 20 61 6e 79 20 6f 74  OCKED. If any ot
2a29c 68 65 72 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63  her error.** occ
2a29d 75 72 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  urs, return SQLI
2a29e 54 45 5f 53 43 48 45 4d 41 2e 0a 2a 2f 0a 53 51  TE_SCHEMA..*/.SQ
2a29f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2a2a0 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72   sqlite3Reprepar
2a2a1 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  e(Vdbe *p){.  in
2a2a2 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
2a2a3 73 74 6d 74 20 2a 70 4e 65 77 3b 0a 20 20 63 6f  stmt *pNew;.  co
2a2a4 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  nst char *zSql;.
2a2a5 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
2a2a6 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2a2a7 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c  3_mutex_held(sql
2a2a8 69 74 65 33 56 64 62 65 44 62 28 70 29 2d 3e 6d  ite3VdbeDb(p)->m
2a2a9 75 74 65 78 29 20 29 3b 0a 20 20 7a 53 71 6c 20  utex) );.  zSql 
2a2aa 3d 20 73 71 6c 69 74 65 33 5f 73 71 6c 28 28 73  = sqlite3_sql((s
2a2ab 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 70 29  qlite3_stmt *)p)
2a2ac 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 71 6c  ;.  assert( zSql
2a2ad 21 3d 30 20 29 3b 20 20 2f 2a 20 52 65 70 72 65  !=0 );  /* Repre
2a2ae 70 61 72 65 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  pare only called
2a2af 20 66 6f 72 20 70 72 65 70 61 72 65 5f 76 32 28   for prepare_v2(
2a2b0 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a  ) statements */.
2a2b1 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64    db = sqlite3Vd
2a2b2 62 65 44 62 28 70 29 3b 0a 20 20 61 73 73 65 72  beDb(p);.  asser
2a2b3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
2a2b4 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
2a2b5 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
2a2b6 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65  e3LockAndPrepare
2a2b7 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 30  (db, zSql, -1, 0
2a2b8 2c 20 70 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a  , p, &pNew, 0);.
2a2b9 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
2a2ba 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
2a2bb 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62  OMEM ){.      db
2a2bc 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2a2bd 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73   1;.    }.    as
2a2be 73 65 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b  sert( pNew==0 );
2a2bf 0a 20 20 20 20 72 65 74 75 72 6e 20 28 72 63 3d  .    return (rc=
2a2c0 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 20  =SQLITE_LOCKED) 
2a2c1 3f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  ? SQLITE_LOCKED 
2a2c2 3a 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b  : SQLITE_SCHEMA;
2a2c3 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
2a2c4 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b  sert( pNew!=0 );
2a2c5 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
2a2c6 62 65 53 77 61 70 28 28 56 64 62 65 2a 29 70 4e  beSwap((Vdbe*)pN
2a2c7 65 77 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65  ew, p);.  sqlite
2a2c8 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
2a2c9 73 28 70 4e 65 77 2c 20 28 73 71 6c 69 74 65 33  s(pNew, (sqlite3
2a2ca 5f 73 74 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c  _stmt*)p);.  sql
2a2cb 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65  ite3VdbeResetSte
2a2cc 70 52 65 73 75 6c 74 28 28 56 64 62 65 2a 29 70  pResult((Vdbe*)p
2a2cd 4e 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  New);.  sqlite3V
2a2ce 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
2a2cf 65 2a 29 70 4e 65 77 29 3b 0a 20 20 72 65 74 75  e*)pNew);.  retu
2a2d0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2a2d1 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73  ../*.** Two vers
2a2d2 69 6f 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69  ions of the offi
2a2d3 63 69 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63  cial API.  Legac
2a2d4 79 20 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20  y and new use.  
2a2d5 49 6e 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a  In the legacy.**
2a2d6 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72   version, the or
2a2d7 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20  iginal SQL text 
2a2d8 69 73 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20  is not saved in 
2a2d9 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
2a2da 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f  tement.** and so
2a2db 20 69 66 20 61 20 73 63 68 65 6d 61 20 63 68 61   if a schema cha
2a2dc 6e 67 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49  nge occurs, SQLI
2a2dd 54 45 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74  TE_SCHEMA is ret
2a2de 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  urned by.** sqli
2a2df 74 65 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20  te3_step().  In 
2a2e0 74 68 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c  the new version,
2a2e1 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
2a2e2 4c 20 74 65 78 74 20 69 73 20 72 65 74 61 69 6e  L text is retain
2a2e3 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74  ed.** and the st
2a2e4 61 74 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d  atement is autom
2a2e5 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69  atically recompi
2a2e6 6c 65 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61  led if an schema
2a2e7 20 63 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72   change.** occur
2a2e8 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
2a2e9 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
2a2ea 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20  pare(.  sqlite3 
2a2eb 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2a2ec 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
2a2ed 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ndle. */.  const
2a2ee 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
2a2ef 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e       /* UTF-8 en
2a2f0 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
2a2f1 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ent. */.  int nB
2a2f2 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
2a2f3 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
2a2f4 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
2a2f5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
2a2f6 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
2a2f7 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
2a2f8 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
2a2f9 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2a2fa 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
2a2fb 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ail       /* OUT
2a2fc 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20  : End of parsed 
2a2fd 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  string */.){.  i
2a2fe 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71  nt rc;.  rc = sq
2a2ff 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
2a300 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74  are(db,zSql,nByt
2a301 65 73 2c 30 2c 30 2c 70 70 53 74 6d 74 2c 70 7a  es,0,0,ppStmt,pz
2a302 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28  Tail);.  assert(
2a303 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
2a304 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a  | ppStmt==0 || *
2a305 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a  ppStmt==0 );  /*
2a306 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31 20   VERIFY: F13021 
2a307 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  */.  return rc;.
2a308 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
2a309 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2a30a 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  _v2(.  sqlite3 *
2a30b 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2a30c 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
2a30d 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  dle. */.  const 
2a30e 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
2a30f 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63      /* UTF-8 enc
2a310 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
2a311 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  nt. */.  int nBy
2a312 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
2a313 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
2a314 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
2a315 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
2a316 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
2a317 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
2a318 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
2a319 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
2a31a 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
2a31b 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
2a31c 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
2a31d 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
2a31e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  t rc;.  rc = sql
2a31f 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
2a320 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65  re(db,zSql,nByte
2a321 73 2c 31 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54  s,1,0,ppStmt,pzT
2a322 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ail);.  assert( 
2a323 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
2a324 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70   ppStmt==0 || *p
2a325 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  pStmt==0 );  /* 
2a326 56 45 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a  VERIFY: F13021 *
2a327 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  /.  return rc;.}
2a328 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
2a329 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
2a32a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65 20 55  ** Compile the U
2a32b 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 53 51  TF-16 encoded SQ
2a32c 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71 6c  L statement zSql
2a32d 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65 6e   into a statemen
2a32e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61  t handle..*/.sta
2a32f 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 50  tic int sqlite3P
2a330 72 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69  repare16(.  sqli
2a331 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2a332 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2a333 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20  e handle. */ .  
2a334 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c  const void *zSql
2a335 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46  ,         /* UTF
2a336 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73  -8 encoded SQL s
2a337 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69  tatement. */.  i
2a338 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20  nt nBytes,      
2a339 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
2a33a 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79  th of zSql in by
2a33b 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61  tes. */.  int sa
2a33c 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20  veSqlFlag,      
2a33d 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73      /* True to s
2a33e 61 76 65 20 53 51 4c 20 74 65 78 74 20 69 6e 74  ave SQL text int
2a33f 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  o the sqlite3_st
2a340 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  mt */.  sqlite3_
2a341 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
2a342 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e    /* OUT: A poin
2a343 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61  ter to the prepa
2a344 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  red statement */
2a345 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a  .  const void **
2a346 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
2a347 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
2a348 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ed string */.){.
2a349 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69    /* This functi
2a34a 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 77 6f 72  on currently wor
2a34b 6b 73 20 62 79 20 66 69 72 73 74 20 74 72 61 6e  ks by first tran
2a34c 73 66 6f 72 6d 69 6e 67 20 74 68 65 20 55 54 46  sforming the UTF
2a34d 2d 31 36 0a 20 20 2a 2a 20 65 6e 63 6f 64 65 64  -16.  ** encoded
2a34e 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38   string to UTF-8
2a34f 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 69 6e 67 20  , then invoking 
2a350 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
2a351 29 2e 20 54 68 65 0a 20 20 2a 2a 20 74 72 69 63  ). The.  ** tric
2a352 6b 79 20 62 69 74 20 69 73 20 66 69 67 75 72 69  ky bit is figuri
2a353 6e 67 20 6f 75 74 20 74 68 65 20 70 6f 69 6e 74  ng out the point
2a354 65 72 20 74 6f 20 72 65 74 75 72 6e 20 69 6e 20  er to return in 
2a355 2a 70 7a 54 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20  *pzTail..  */.  
2a356 63 68 61 72 20 2a 7a 53 71 6c 38 3b 0a 20 20 63  char *zSql8;.  c
2a357 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 69 6c  onst char *zTail
2a358 38 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20  8 = 0;.  int rc 
2a359 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
2a35a 61 73 73 65 72 74 28 20 70 70 53 74 6d 74 20 29  assert( ppStmt )
2a35b 3b 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b  ;.  *ppStmt = 0;
2a35c 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53  .  if( !sqlite3S
2a35d 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29  afetyCheckOk(db)
2a35e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2a35f 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
2a360 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
2a361 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
2a362 78 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71  x);.  zSql8 = sq
2a363 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64 62  lite3Utf16to8(db
2a364 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b  , zSql, nBytes);
2a365 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a  .  if( zSql8 ){.
2a366 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2a367 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64  LockAndPrepare(d
2a368 62 2c 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73 61  b, zSql8, -1, sa
2a369 76 65 53 71 6c 46 6c 61 67 2c 20 30 2c 20 70 70  veSqlFlag, 0, pp
2a36a 53 74 6d 74 2c 20 26 7a 54 61 69 6c 38 29 3b 0a  Stmt, &zTail8);.
2a36b 20 20 7d 0a 0a 20 20 69 66 28 20 7a 54 61 69 6c    }..  if( zTail
2a36c 38 20 26 26 20 70 7a 54 61 69 6c 20 29 7b 0a 20  8 && pzTail ){. 
2a36d 20 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33     /* If sqlite3
2a36e 5f 70 72 65 70 61 72 65 20 72 65 74 75 72 6e 73  _prepare returns
2a36f 20 61 20 74 61 69 6c 20 70 6f 69 6e 74 65 72 2c   a tail pointer,
2a370 20 77 65 20 63 61 6c 63 75 6c 61 74 65 20 74 68   we calculate th
2a371 65 0a 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c  e.    ** equival
2a372 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f  ent pointer into
2a373 20 74 68 65 20 55 54 46 2d 31 36 20 73 74 72 69   the UTF-16 stri
2a374 6e 67 20 62 79 20 63 6f 75 6e 74 69 6e 67 20 74  ng by counting t
2a375 68 65 20 75 6e 69 63 6f 64 65 0a 20 20 20 20 2a  he unicode.    *
2a376 2a 20 63 68 61 72 61 63 74 65 72 73 20 62 65 74  * characters bet
2a377 77 65 65 6e 20 7a 53 71 6c 38 20 61 6e 64 20 7a  ween zSql8 and z
2a378 54 61 69 6c 38 2c 20 61 6e 64 20 74 68 65 6e 20  Tail8, and then 
2a379 72 65 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e  returning a poin
2a37a 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  ter.    ** the s
2a37b 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  ame number of ch
2a37c 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68  aracters into th
2a37d 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e  e UTF-16 string.
2a37e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
2a37f 63 68 61 72 73 5f 70 61 72 73 65 64 20 3d 20 73  chars_parsed = s
2a380 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
2a381 6e 28 7a 53 71 6c 38 2c 20 28 69 6e 74 29 28 7a  n(zSql8, (int)(z
2a382 54 61 69 6c 38 2d 7a 53 71 6c 38 29 29 3b 0a 20  Tail8-zSql8));. 
2a383 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 28 75 38     *pzTail = (u8
2a384 20 2a 29 7a 53 71 6c 20 2b 20 73 71 6c 69 74 65   *)zSql + sqlite
2a385 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28 7a 53  3Utf16ByteLen(zS
2a386 71 6c 2c 20 63 68 61 72 73 5f 70 61 72 73 65 64  ql, chars_parsed
2a387 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2a388 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 38  DbFree(db, zSql8
2a389 29 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ); .  rc = sqlit
2a38a 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
2a38b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
2a38c 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2a38d 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
2a38e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76  ;.}../*.** Two v
2a38f 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 6f  ersions of the o
2a390 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20 4c 65  fficial API.  Le
2a391 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75 73 65  gacy and new use
2a392 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79  .  In the legacy
2a393 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74 68 65  .** version, the
2a394 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65   original SQL te
2a395 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65 64 20  xt is not saved 
2a396 69 6e 20 74 68 65 20 70 72 65 70 61 72 65 64 20  in the prepared 
2a397 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64  statement.** and
2a398 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d 61 20   so if a schema 
2a399 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c 20 53  change occurs, S
2a39a 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 73 20  QLITE_SCHEMA is 
2a39b 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
2a39c 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20  qlite3_step().  
2a39d 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72 73 69  In the new versi
2a39e 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  on, the original
2a39f 20 53 51 4c 20 74 65 78 74 20 69 73 20 72 65 74   SQL text is ret
2a3a0 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65  ained.** and the
2a3a1 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 75   statement is au
2a3a2 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f  tomatically reco
2a3a3 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73 63 68  mpiled if an sch
2a3a4 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20 6f 63  ema change.** oc
2a3a5 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  curs..*/.SQLITE_
2a3a6 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2a3a7 70 72 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c  prepare16(.  sql
2a3a8 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2a3a9 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2a3aa 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20  se handle. */ . 
2a3ab 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71   const void *zSq
2a3ac 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54  l,         /* UT
2a3ad 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  F-8 encoded SQL 
2a3ae 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
2a3af 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
2a3b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
2a3b1 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
2a3b2 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ytes. */.  sqlit
2a3b3 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
2a3b4 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70  ,    /* OUT: A p
2a3b5 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72  ointer to the pr
2a3b6 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2a3b7 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
2a3b8 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20   **pzTail       
2a3b9 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70  /* OUT: End of p
2a3ba 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a  arsed string */.
2a3bb 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72  ){.  int rc;.  r
2a3bc 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61  c = sqlite3Prepa
2a3bd 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c 6e 42 79  re16(db,zSql,nBy
2a3be 74 65 73 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54  tes,0,ppStmt,pzT
2a3bf 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ail);.  assert( 
2a3c0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
2a3c1 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70   ppStmt==0 || *p
2a3c2 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  pStmt==0 );  /* 
2a3c3 56 45 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a  VERIFY: F13021 *
2a3c4 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  /.  return rc;.}
2a3c5 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a3c6 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
2a3c7 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  6_v2(.  sqlite3 
2a3c8 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2a3c9 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
2a3ca 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73  ndle. */ .  cons
2a3cb 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20  t void *zSql,   
2a3cc 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
2a3cd 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
2a3ce 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
2a3cf 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
2a3d0 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
2a3d1 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
2a3d2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
2a3d3 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
2a3d4 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
2a3d5 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
2a3d6 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
2a3d7 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a   const void **pz
2a3d8 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
2a3d9 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
2a3da 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
2a3db 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73  int rc;.  rc = s
2a3dc 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28  qlite3Prepare16(
2a3dd 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 31  db,zSql,nBytes,1
2a3de 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
2a3df 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
2a3e0 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74  QLITE_OK || ppSt
2a3e1 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74  mt==0 || *ppStmt
2a3e2 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46  ==0 );  /* VERIF
2a3e3 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72  Y: F13021 */.  r
2a3e4 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e  eturn rc;.}..#en
2a3e5 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2a3e6 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a  IT_UTF16 */../**
2a3e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2a3e8 20 6f 66 20 70 72 65 70 61 72 65 2e 63 20 2a 2a   of prepare.c **
2a3e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a3ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a3eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2a3ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2a3ed 69 6e 20 66 69 6c 65 20 73 65 6c 65 63 74 2e 63  in file select.c
2a3ee 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2a3ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2a3f1 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
2a3f2 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
2a3f3 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2a3f4 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2a3f5 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2a3f6 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2a3f7 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2a3f8 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2a3f9 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2a3fa 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2a3fb 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2a3fc 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2a3fd 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2a3fe 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2a3ff 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2a400 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2a401 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2a402 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2a403 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2a404 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a405 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a406 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a407 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a408 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
2a409 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
2a40a 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61   routines that a
2a40b 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  re called by the
2a40c 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61   parser.** to ha
2a40d 6e 64 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74  ndle SELECT stat
2a40e 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65  ements in SQLite
2a40f 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  ..*/.../*.** Del
2a410 65 74 65 20 61 6c 6c 20 74 68 65 20 63 6f 6e 74  ete all the cont
2a411 65 6e 74 20 6f 66 20 61 20 53 65 6c 65 63 74 20  ent of a Select 
2a412 73 74 72 75 63 74 75 72 65 20 62 75 74 20 64 6f  structure but do
2a413 20 6e 6f 74 20 64 65 61 6c 6c 6f 63 61 74 65 0a   not deallocate.
2a414 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  ** the select st
2a415 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a  ructure itself..
2a416 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
2a417 6c 65 61 72 53 65 6c 65 63 74 28 73 71 6c 69 74  learSelect(sqlit
2a418 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a  e3 *db, Select *
2a419 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  p){.  sqlite3Exp
2a41a 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2a41b 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73 71  p->pEList);.  sq
2a41c 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
2a41d 74 65 28 64 62 2c 20 70 2d 3e 70 53 72 63 29 3b  te(db, p->pSrc);
2a41e 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2a41f 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 57 68 65  lete(db, p->pWhe
2a420 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  re);.  sqlite3Ex
2a421 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
2a422 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20   p->pGroupBy);. 
2a423 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2a424 74 65 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e  te(db, p->pHavin
2a425 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  g);.  sqlite3Exp
2a426 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2a427 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  p->pOrderBy);.  
2a428 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2a429 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f  ete(db, p->pPrio
2a42a 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  r);.  sqlite3Exp
2a42b 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
2a42c 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69 74 65  Limit);.  sqlite
2a42d 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2a42e 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a  p->pOffset);.}..
2a42f 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
2a430 20 61 20 53 65 6c 65 63 74 44 65 73 74 20 73 74   a SelectDest st
2a431 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
2a432 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2a433 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2a434 74 49 6e 69 74 28 53 65 6c 65 63 74 44 65 73 74  tInit(SelectDest
2a435 20 2a 70 44 65 73 74 2c 20 69 6e 74 20 65 44 65   *pDest, int eDe
2a436 73 74 2c 20 69 6e 74 20 69 50 61 72 6d 29 7b 0a  st, int iParm){.
2a437 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 3d    pDest->eDest =
2a438 20 28 75 38 29 65 44 65 73 74 3b 0a 20 20 70 44   (u8)eDest;.  pD
2a439 65 73 74 2d 3e 69 50 61 72 6d 20 3d 20 69 50 61  est->iParm = iPa
2a43a 72 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 61 66 66  rm;.  pDest->aff
2a43b 69 6e 69 74 79 20 3d 20 30 3b 0a 20 20 70 44 65  inity = 0;.  pDe
2a43c 73 74 2d 3e 69 4d 65 6d 20 3d 20 30 3b 0a 20 20  st->iMem = 0;.  
2a43d 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 30 3b  pDest->nMem = 0;
2a43e 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .}.../*.** Alloc
2a43f 61 74 65 20 61 20 6e 65 77 20 53 65 6c 65 63 74  ate a new Select
2a440 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72   structure and r
2a441 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
2a442 74 6f 20 74 68 61 74 0a 2a 2a 20 73 74 72 75 63  to that.** struc
2a443 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
2a444 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a  PRIVATE Select *
2a445 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
2a446 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2a447 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
2a448 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2a449 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2a44a 73 74 2c 20 20 20 20 20 2f 2a 20 77 68 69 63 68  st,     /* which
2a44b 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 69 6e 63 6c   columns to incl
2a44c 75 64 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c  ude in the resul
2a44d 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
2a44e 70 53 72 63 2c 20 20 20 20 20 20 20 20 2f 2a 20  pSrc,        /* 
2a44f 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2a450 2d 2d 20 77 68 69 63 68 20 74 61 62 6c 65 73 20  -- which tables 
2a451 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70  to scan */.  Exp
2a452 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20  r *pWhere,      
2a453 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20     /* the WHERE 
2a454 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
2a455 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 2c 20  List *pGroupBy, 
2a456 20 20 2f 2a 20 74 68 65 20 47 52 4f 55 50 20 42    /* the GROUP B
2a457 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  Y clause */.  Ex
2a458 70 72 20 2a 70 48 61 76 69 6e 67 2c 20 20 20 20  pr *pHaving,    
2a459 20 20 20 20 2f 2a 20 74 68 65 20 48 41 56 49 4e      /* the HAVIN
2a45a 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  G clause */.  Ex
2a45b 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
2a45c 2c 20 20 20 2f 2a 20 74 68 65 20 4f 52 44 45 52  ,   /* the ORDER
2a45d 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
2a45e 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74 2c 20  int isDistinct, 
2a45f 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66        /* true if
2a460 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65   the DISTINCT ke
2a461 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74  yword is present
2a462 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d   */.  Expr *pLim
2a463 69 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  it,         /* L
2a464 49 4d 49 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c  IMIT value.  NUL
2a465 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64  L means not used
2a466 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66   */.  Expr *pOff
2a467 73 65 74 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  set         /* O
2a468 46 46 53 45 54 20 76 61 6c 75 65 2e 20 20 4e 55  FFSET value.  NU
2a469 4c 4c 20 6d 65 61 6e 73 20 6e 6f 20 6f 66 66 73  LL means no offs
2a46a 65 74 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63  et */.){.  Selec
2a46b 74 20 2a 70 4e 65 77 3b 0a 20 20 53 65 6c 65 63  t *pNew;.  Selec
2a46c 74 20 73 74 61 6e 64 69 6e 3b 0a 20 20 73 71 6c  t standin;.  sql
2a46d 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2a46e 65 2d 3e 64 62 3b 0a 20 20 70 4e 65 77 20 3d 20  e->db;.  pNew = 
2a46f 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2a470 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  ero(db, sizeof(*
2a471 70 4e 65 77 29 20 29 3b 0a 20 20 61 73 73 65 72  pNew) );.  asser
2a472 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
2a473 6c 65 64 20 7c 7c 20 21 70 4f 66 66 73 65 74 20  led || !pOffset 
2a474 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20 2f 2a 20  || pLimit ); /* 
2a475 4f 46 46 53 45 54 20 69 6d 70 6c 69 65 73 20 4c  OFFSET implies L
2a476 49 4d 49 54 20 2a 2f 0a 20 20 69 66 28 20 70 4e  IMIT */.  if( pN
2a477 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65  ew==0 ){.    pNe
2a478 77 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a 20 20  w = &standin;.  
2a479 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30    memset(pNew, 0
2a47a 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29  , sizeof(*pNew))
2a47b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 4c 69  ;.  }.  if( pELi
2a47c 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 45 4c  st==0 ){.    pEL
2a47d 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
2a47e 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2a47f 73 65 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78  se, 0, sqlite3Ex
2a480 70 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30 29 29  pr(db,TK_ALL,0))
2a481 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 45  ;.  }.  pNew->pE
2a482 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20  List = pEList;. 
2a483 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 70 53   pNew->pSrc = pS
2a484 72 63 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65  rc;.  pNew->pWhe
2a485 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 70  re = pWhere;.  p
2a486 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20  New->pGroupBy = 
2a487 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 4e 65 77  pGroupBy;.  pNew
2a488 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70 48 61 76  ->pHaving = pHav
2a489 69 6e 67 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72  ing;.  pNew->pOr
2a48a 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79  derBy = pOrderBy
2a48b 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61  ;.  pNew->selFla
2a48c 67 73 20 3d 20 69 73 44 69 73 74 69 6e 63 74 20  gs = isDistinct 
2a48d 3f 20 53 46 5f 44 69 73 74 69 6e 63 74 20 3a 20  ? SF_Distinct : 
2a48e 30 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20  0;.  pNew->op = 
2a48f 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20 70 4e 65  TK_SELECT;.  pNe
2a490 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d  w->pLimit = pLim
2a491 69 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66  it;.  pNew->pOff
2a492 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20  set = pOffset;. 
2a493 20 61 73 73 65 72 74 28 20 70 4f 66 66 73 65 74   assert( pOffset
2a494 3d 3d 30 20 7c 7c 20 70 4c 69 6d 69 74 21 3d 30  ==0 || pLimit!=0
2a495 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72   );.  pNew->addr
2a496 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31  OpenEphm[0] = -1
2a497 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
2a498 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a  enEphm[1] = -1;.
2a499 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e    pNew->addrOpen
2a49a 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[2] = -1;.  
2a49b 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2a49c 69 6c 65 64 20 29 20 7b 0a 20 20 20 20 63 6c 65  iled ) {.    cle
2a49d 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70 4e 65  arSelect(db, pNe
2a49e 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77  w);.    if( pNew
2a49f 21 3d 26 73 74 61 6e 64 69 6e 20 29 20 73 71 6c  !=&standin ) sql
2a4a0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2a4a1 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d  New);.    pNew =
2a4a2 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
2a4a3 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
2a4a4 44 65 6c 65 74 65 20 74 68 65 20 67 69 76 65 6e  Delete the given
2a4a5 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72   Select structur
2a4a6 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73  e and all of its
2a4a7 20 73 75 62 73 74 72 75 63 74 75 72 65 73 2e 0a   substructures..
2a4a8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2a4a9 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
2a4aa 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74  lectDelete(sqlit
2a4ab 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a  e3 *db, Select *
2a4ac 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
2a4ad 20 20 20 63 6c 65 61 72 53 65 6c 65 63 74 28 64     clearSelect(d
2a4ae 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  b, p);.    sqlit
2a4af 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
2a4b0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  .  }.}../*.** Gi
2a4b1 76 65 6e 20 31 20 74 6f 20 33 20 69 64 65 6e 74  ven 1 to 3 ident
2a4b2 69 66 69 65 72 73 20 70 72 65 63 65 65 64 69 6e  ifiers preceedin
2a4b3 67 20 74 68 65 20 4a 4f 49 4e 20 6b 65 79 77 6f  g the JOIN keywo
2a4b4 72 64 2c 20 64 65 74 65 72 6d 69 6e 65 20 74 68  rd, determine th
2a4b5 65 0a 2a 2a 20 74 79 70 65 20 6f 66 20 6a 6f 69  e.** type of joi
2a4b6 6e 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e  n.  Return an in
2a4b7 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 74  teger constant t
2a4b8 68 61 74 20 65 78 70 72 65 73 73 65 73 20 74 68  hat expresses th
2a4b9 61 74 20 74 79 70 65 0a 2a 2a 20 69 6e 20 74 65  at type.** in te
2a4ba 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  rms of the follo
2a4bb 77 69 6e 67 20 62 69 74 20 76 61 6c 75 65 73 3a  wing bit values:
2a4bc 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4a 54 5f 49 4e  .**.**     JT_IN
2a4bd 4e 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 43 52  NER.**     JT_CR
2a4be 4f 53 53 0a 2a 2a 20 20 20 20 20 4a 54 5f 4f 55  OSS.**     JT_OU
2a4bf 54 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 4e 41  TER.**     JT_NA
2a4c0 54 55 52 41 4c 0a 2a 2a 20 20 20 20 20 4a 54 5f  TURAL.**     JT_
2a4c1 4c 45 46 54 0a 2a 2a 20 20 20 20 20 4a 54 5f 52  LEFT.**     JT_R
2a4c2 49 47 48 54 0a 2a 2a 0a 2a 2a 20 41 20 66 75 6c  IGHT.**.** A ful
2a4c3 6c 20 6f 75 74 65 72 20 6a 6f 69 6e 20 69 73 20  l outer join is 
2a4c4 74 68 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  the combination 
2a4c5 6f 66 20 4a 54 5f 4c 45 46 54 20 61 6e 64 20 4a  of JT_LEFT and J
2a4c6 54 5f 52 49 47 48 54 2e 0a 2a 2a 0a 2a 2a 20 49  T_RIGHT..**.** I
2a4c7 66 20 61 6e 20 69 6c 6c 65 67 61 6c 20 6f 72 20  f an illegal or 
2a4c8 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e  unsupported join
2a4c9 20 74 79 70 65 20 69 73 20 73 65 65 6e 2c 20 74   type is seen, t
2a4ca 68 65 6e 20 73 74 69 6c 6c 20 72 65 74 75 72 6e  hen still return
2a4cb 0a 2a 2a 20 61 20 6a 6f 69 6e 20 74 79 70 65 2c  .** a join type,
2a4cc 20 62 75 74 20 70 75 74 20 61 6e 20 65 72 72 6f   but put an erro
2a4cd 72 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 20  r in the pParse 
2a4ce 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
2a4cf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2a4d0 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65   sqlite3JoinType
2a4d1 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2a4d2 54 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f 6b 65 6e  Token *pA, Token
2a4d3 20 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a 70 43 29   *pB, Token *pC)
2a4d4 7b 0a 20 20 69 6e 74 20 6a 6f 69 6e 74 79 70 65  {.  int jointype
2a4d5 20 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e 20 2a 61   = 0;.  Token *a
2a4d6 70 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f 6b 65 6e  pAll[3];.  Token
2a4d7 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20   *p;.           
2a4d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a4d9 20 20 2f 2a 20 20 20 30 31 32 33 34 35 36 37 38    /*   012345678
2a4da 39 20 31 32 33 34 35 36 37 38 39 20 31 32 33 34  9 123456789 1234
2a4db 35 36 37 38 39 20 31 32 33 20 2a 2f 0a 20 20 73  56789 123 */.  s
2a4dc 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2a4dd 20 7a 4b 65 79 54 65 78 74 5b 5d 20 3d 20 22 6e   zKeyText[] = "n
2a4de 61 74 75 72 61 6c 65 66 74 6f 75 74 65 72 69 67  aturaleftouterig
2a4df 68 74 66 75 6c 6c 69 6e 6e 65 72 63 72 6f 73 73  htfullinnercross
2a4e0 22 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ";.  static cons
2a4e1 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 75  t struct {.    u
2a4e2 38 20 69 3b 20 20 20 20 20 20 20 20 2f 2a 20 42  8 i;        /* B
2a4e3 65 67 69 6e 6e 69 6e 67 20 6f 66 20 6b 65 79 77  eginning of keyw
2a4e4 6f 72 64 20 74 65 78 74 20 69 6e 20 7a 4b 65 79  ord text in zKey
2a4e5 54 65 78 74 5b 5d 20 2a 2f 0a 20 20 20 20 75 38  Text[] */.    u8
2a4e6 20 6e 43 68 61 72 3b 20 20 20 20 2f 2a 20 4c 65   nChar;    /* Le
2a4e7 6e 67 74 68 20 6f 66 20 74 68 65 20 6b 65 79 77  ngth of the keyw
2a4e8 6f 72 64 20 69 6e 20 63 68 61 72 61 63 74 65 72  ord in character
2a4e9 73 20 2a 2f 0a 20 20 20 20 75 38 20 63 6f 64 65  s */.    u8 code
2a4ea 3b 20 20 20 20 20 2f 2a 20 4a 6f 69 6e 20 74 79  ;     /* Join ty
2a4eb 70 65 20 6d 61 73 6b 20 2a 2f 0a 20 20 7d 20 61  pe mask */.  } a
2a4ec 4b 65 79 77 6f 72 64 5b 5d 20 3d 20 7b 0a 20 20  Keyword[] = {.  
2a4ed 20 20 2f 2a 20 6e 61 74 75 72 61 6c 20 2a 2f 20    /* natural */ 
2a4ee 7b 20 30 2c 20 20 37 2c 20 4a 54 5f 4e 41 54 55  { 0,  7, JT_NATU
2a4ef 52 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  RAL             
2a4f0 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 6c 65 66     },.    /* lef
2a4f1 74 20 20 20 20 2a 2f 20 7b 20 36 2c 20 20 34 2c  t    */ { 6,  4,
2a4f2 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f 55 54 45   JT_LEFT|JT_OUTE
2a4f3 52 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20  R          },.  
2a4f4 20 20 2f 2a 20 6f 75 74 65 72 20 20 20 2a 2f 20    /* outer   */ 
2a4f5 7b 20 31 30 2c 20 35 2c 20 4a 54 5f 4f 55 54 45  { 10, 5, JT_OUTE
2a4f6 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
2a4f7 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 72 69 67     },.    /* rig
2a4f8 68 74 20 20 20 2a 2f 20 7b 20 31 34 2c 20 35 2c  ht   */ { 14, 5,
2a4f9 20 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54   JT_RIGHT|JT_OUT
2a4fa 45 52 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20  ER         },.  
2a4fb 20 20 2f 2a 20 66 75 6c 6c 20 20 20 20 2a 2f 20    /* full    */ 
2a4fc 7b 20 31 39 2c 20 34 2c 20 4a 54 5f 4c 45 46 54  { 19, 4, JT_LEFT
2a4fd 7c 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54  |JT_RIGHT|JT_OUT
2a4fe 45 52 20 7d 2c 0a 20 20 20 20 2f 2a 20 69 6e 6e  ER },.    /* inn
2a4ff 65 72 20 20 20 2a 2f 20 7b 20 32 33 2c 20 35 2c  er   */ { 23, 5,
2a500 20 4a 54 5f 49 4e 4e 45 52 20 20 20 20 20 20 20   JT_INNER       
2a501 20 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20             },.  
2a502 20 20 2f 2a 20 63 72 6f 73 73 20 20 20 2a 2f 20    /* cross   */ 
2a503 7b 20 32 38 2c 20 35 2c 20 4a 54 5f 49 4e 4e 45  { 28, 5, JT_INNE
2a504 52 7c 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 20  R|JT_CROSS      
2a505 20 20 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74     },.  };.  int
2a506 20 69 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c 5b 30   i, j;.  apAll[0
2a507 5d 20 3d 20 70 41 3b 0a 20 20 61 70 41 6c 6c 5b  ] = pA;.  apAll[
2a508 31 5d 20 3d 20 70 42 3b 0a 20 20 61 70 41 6c 6c  1] = pB;.  apAll
2a509 5b 32 5d 20 3d 20 70 43 3b 0a 20 20 66 6f 72 28  [2] = pC;.  for(
2a50a 69 3d 30 3b 20 69 3c 33 20 26 26 20 61 70 41 6c  i=0; i<3 && apAl
2a50b 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  l[i]; i++){.    
2a50c 70 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a 20 20  p = apAll[i];.  
2a50d 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72    for(j=0; j<Arr
2a50e 61 79 53 69 7a 65 28 61 4b 65 79 77 6f 72 64 29  aySize(aKeyword)
2a50f 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
2a510 28 20 70 2d 3e 6e 3d 3d 61 4b 65 79 77 6f 72 64  ( p->n==aKeyword
2a511 5b 6a 5d 2e 6e 43 68 61 72 20 0a 20 20 20 20 20  [j].nChar .     
2a512 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 53       && sqlite3S
2a513 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 70  trNICmp((char*)p
2a514 2d 3e 7a 2c 20 26 7a 4b 65 79 54 65 78 74 5b 61  ->z, &zKeyText[a
2a515 4b 65 79 77 6f 72 64 5b 6a 5d 2e 69 5d 2c 20 70  Keyword[j].i], p
2a516 2d 3e 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ->n)==0 ){.     
2a517 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20 61     jointype |= a
2a518 4b 65 79 77 6f 72 64 5b 6a 5d 2e 63 6f 64 65 3b  Keyword[j].code;
2a519 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2a51a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2a51b 20 20 74 65 73 74 63 61 73 65 28 20 6a 3d 3d 30    testcase( j==0
2a51c 20 7c 7c 20 6a 3d 3d 31 20 7c 7c 20 6a 3d 3d 32   || j==1 || j==2
2a51d 20 7c 7c 20 6a 3d 3d 33 20 7c 7c 20 6a 3d 3d 34   || j==3 || j==4
2a51e 20 7c 7c 20 6a 3d 3d 35 20 7c 7c 20 6a 3d 3d 36   || j==5 || j==6
2a51f 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3e 3d 41   );.    if( j>=A
2a520 72 72 61 79 53 69 7a 65 28 61 4b 65 79 77 6f 72  rraySize(aKeywor
2a521 64 29 20 29 7b 0a 20 20 20 20 20 20 6a 6f 69 6e  d) ){.      join
2a522 74 79 70 65 20 7c 3d 20 4a 54 5f 45 52 52 4f 52  type |= JT_ERROR
2a523 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2a524 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 0a 20     }.  }.  if(. 
2a525 20 20 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20      (jointype & 
2a526 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54  (JT_INNER|JT_OUT
2a527 45 52 29 29 3d 3d 28 4a 54 5f 49 4e 4e 45 52 7c  ER))==(JT_INNER|
2a528 4a 54 5f 4f 55 54 45 52 29 20 7c 7c 0a 20 20 20  JT_OUTER) ||.   
2a529 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54    (jointype & JT
2a52a 5f 45 52 52 4f 52 29 21 3d 30 0a 20 20 29 7b 0a  _ERROR)!=0.  ){.
2a52b 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2a52c 7a 53 70 20 3d 20 22 20 22 3b 0a 20 20 20 20 61  zSp = " ";.    a
2a52d 73 73 65 72 74 28 20 70 42 21 3d 30 20 29 3b 0a  ssert( pB!=0 );.
2a52e 20 20 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b      if( pC==0 ){
2a52f 20 7a 53 70 2b 2b 3b 20 7d 0a 20 20 20 20 73 71   zSp++; }.    sq
2a530 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2a531 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 6f  arse, "unknown o
2a532 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f  r unsupported jo
2a533 69 6e 20 74 79 70 65 3a 20 22 0a 20 20 20 20 20  in type: ".     
2a534 20 20 22 25 54 20 25 54 25 73 25 54 22 2c 20 70    "%T %T%s%T", p
2a535 41 2c 20 70 42 2c 20 7a 53 70 2c 20 70 43 29 3b  A, pB, zSp, pC);
2a536 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20  .    jointype = 
2a537 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 65 6c 73  JT_INNER;.  }els
2a538 65 20 69 66 28 20 28 6a 6f 69 6e 74 79 70 65 20  e if( (jointype 
2a539 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 20 0a  & JT_OUTER)!=0 .
2a53a 20 20 20 20 20 20 20 20 20 26 26 20 28 6a 6f 69           && (joi
2a53b 6e 74 79 70 65 20 26 20 28 4a 54 5f 4c 45 46 54  ntype & (JT_LEFT
2a53c 7c 4a 54 5f 52 49 47 48 54 29 29 21 3d 4a 54 5f  |JT_RIGHT))!=JT_
2a53d 4c 45 46 54 20 29 7b 0a 20 20 20 20 73 71 6c 69  LEFT ){.    sqli
2a53e 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2a53f 73 65 2c 20 0a 20 20 20 20 20 20 22 52 49 47 48  se, .      "RIGH
2a540 54 20 61 6e 64 20 46 55 4c 4c 20 4f 55 54 45 52  T and FULL OUTER
2a541 20 4a 4f 49 4e 73 20 61 72 65 20 6e 6f 74 20 63   JOINs are not c
2a542 75 72 72 65 6e 74 6c 79 20 73 75 70 70 6f 72 74  urrently support
2a543 65 64 22 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79  ed");.    jointy
2a544 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20  pe = JT_INNER;. 
2a545 20 7d 0a 20 20 72 65 74 75 72 6e 20 6a 6f 69 6e   }.  return join
2a546 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  type;.}../*.** R
2a547 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
2a548 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61  of a column in a
2a549 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20   table.  Return 
2a54a 2d 31 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e  -1 if the column
2a54b 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61  .** is not conta
2a54c 69 6e 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c  ined in the tabl
2a54d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2a54e 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 54 61 62   columnIndex(Tab
2a54f 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20  le *pTab, const 
2a550 63 68 61 72 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69  char *zCol){.  i
2a551 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
2a552 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
2a553 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
2a554 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62  ite3StrICmp(pTab
2a555 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c  ->aCol[i].zName,
2a556 20 7a 43 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75   zCol)==0 ) retu
2a557 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn i;.  }.  retu
2a558 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn -1;.}../*.** 
2a559 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2a55a 20 75 73 65 64 20 74 6f 20 61 64 64 20 74 65 72   used to add ter
2a55b 6d 73 20 69 6d 70 6c 69 65 64 20 62 79 20 4a 4f  ms implied by JO
2a55c 49 4e 20 73 79 6e 74 61 78 20 74 6f 20 74 68 65  IN syntax to the
2a55d 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65  .** WHERE clause
2a55e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 61   expression of a
2a55f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2a560 74 2e 20 54 68 65 20 6e 65 77 20 74 65 72 6d 2c  t. The new term,
2a561 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 41 4e 44   which.** is AND
2a562 65 64 20 77 69 74 68 20 74 68 65 20 65 78 69 73  ed with the exis
2a563 74 69 6e 67 20 57 48 45 52 45 20 63 6c 61 75 73  ting WHERE claus
2a564 65 2c 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  e, is of the for
2a565 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 74 61 62  m:.**.**    (tab
2a566 31 2e 63 6f 6c 31 20 3d 20 74 61 62 32 2e 63 6f  1.col1 = tab2.co
2a567 6c 32 29 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20  l2).**.** where 
2a568 74 61 62 31 20 69 73 20 74 68 65 20 69 53 72 63  tab1 is the iSrc
2a569 27 74 68 20 74 61 62 6c 65 20 69 6e 20 53 72 63  'th table in Src
2a56a 4c 69 73 74 20 70 53 72 63 20 61 6e 64 20 74 61  List pSrc and ta
2a56b 62 32 20 69 73 20 74 68 65 20 0a 2a 2a 20 28 69  b2 is the .** (i
2a56c 53 72 63 2b 31 29 27 74 68 2e 20 43 6f 6c 75 6d  Src+1)'th. Colum
2a56d 6e 20 63 6f 6c 31 20 69 73 20 63 6f 6c 75 6d 6e  n col1 is column
2a56e 20 69 43 6f 6c 4c 65 66 74 20 6f 66 20 74 61 62   iColLeft of tab
2a56f 31 2c 20 61 6e 64 20 63 6f 6c 32 20 69 73 0a 2a  1, and col2 is.*
2a570 2a 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 52 69 67  * column iColRig
2a571 68 74 20 6f 66 20 74 61 62 32 2e 0a 2a 2f 0a 73  ht of tab2..*/.s
2a572 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 57 68  tatic void addWh
2a573 65 72 65 54 65 72 6d 28 0a 20 20 50 61 72 73 65  ereTerm(.  Parse
2a574 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2a575 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2a576 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2a577 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
2a578 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2a579 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61     /* List of ta
2a57a 62 6c 65 73 20 69 6e 20 46 52 4f 4d 20 63 6c 61  bles in FROM cla
2a57b 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72  use */.  int iSr
2a57c 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
2a57d 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2a57e 78 20 6f 66 20 66 69 72 73 74 20 74 61 62 6c 65  x of first table
2a57f 20 74 6f 20 6a 6f 69 6e 20 69 6e 20 70 53 72 63   to join in pSrc
2a580 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 4c 65   */.  int iColLe
2a581 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ft,             
2a582 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2a583 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 66 69 72 73  f column in firs
2a584 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  t table */.  int
2a585 20 69 43 6f 6c 52 69 67 68 74 2c 20 20 20 20 20   iColRight,     
2a586 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a587 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
2a588 69 6e 20 73 65 63 6f 6e 64 20 74 61 62 6c 65 20  in second table 
2a589 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 75 74 65 72  */.  int isOuter
2a58a 4a 6f 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Join,           
2a58b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2a58c 74 68 69 73 20 69 73 20 61 6e 20 4f 55 54 45 52  this is an OUTER
2a58d 20 6a 6f 69 6e 20 2a 2f 0a 20 20 45 78 70 72 20   join */.  Expr 
2a58e 2a 2a 70 70 57 68 65 72 65 20 20 20 20 20 20 20  **ppWhere       
2a58f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e             /* IN
2a590 2f 4f 55 54 3a 20 54 68 65 20 57 48 45 52 45 20  /OUT: The WHERE 
2a591 63 6c 61 75 73 65 20 74 6f 20 61 64 64 20 74 6f  clause to add to
2a592 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
2a593 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2a594 62 3b 0a 20 20 45 78 70 72 20 2a 70 45 31 3b 0a  b;.  Expr *pE1;.
2a595 20 20 45 78 70 72 20 2a 70 45 32 3b 0a 20 20 45    Expr *pE2;.  E
2a596 78 70 72 20 2a 70 45 71 3b 0a 0a 20 20 61 73 73  xpr *pEq;..  ass
2a597 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e  ert( pSrc->nSrc>
2a598 28 69 53 72 63 2b 31 29 20 29 3b 0a 20 20 61 73  (iSrc+1) );.  as
2a599 73 65 72 74 28 20 70 53 72 63 2d 3e 61 5b 69 53  sert( pSrc->a[iS
2a59a 72 63 5d 2e 70 54 61 62 20 29 3b 0a 20 20 61 73  rc].pTab );.  as
2a59b 73 65 72 74 28 20 70 53 72 63 2d 3e 61 5b 69 53  sert( pSrc->a[iS
2a59c 72 63 2b 31 5d 2e 70 54 61 62 20 29 3b 0a 0a 20  rc+1].pTab );.. 
2a59d 20 70 45 31 20 3d 20 73 71 6c 69 74 65 33 43 72   pE1 = sqlite3Cr
2a59e 65 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 28 64  eateColumnExpr(d
2a59f 62 2c 20 70 53 72 63 2c 20 69 53 72 63 2c 20 69  b, pSrc, iSrc, i
2a5a0 43 6f 6c 4c 65 66 74 29 3b 0a 20 20 70 45 32 20  ColLeft);.  pE2 
2a5a1 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 43  = sqlite3CreateC
2a5a2 6f 6c 75 6d 6e 45 78 70 72 28 64 62 2c 20 70 53  olumnExpr(db, pS
2a5a3 72 63 2c 20 69 53 72 63 2b 31 2c 20 69 43 6f 6c  rc, iSrc+1, iCol
2a5a4 52 69 67 68 74 29 3b 0a 0a 20 20 70 45 71 20 3d  Right);..  pEq =
2a5a5 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2a5a6 61 72 73 65 2c 20 54 4b 5f 45 51 2c 20 70 45 31  arse, TK_EQ, pE1
2a5a7 2c 20 70 45 32 2c 20 30 29 3b 0a 20 20 69 66 28  , pE2, 0);.  if(
2a5a8 20 70 45 71 20 26 26 20 69 73 4f 75 74 65 72 4a   pEq && isOuterJ
2a5a9 6f 69 6e 20 29 7b 0a 20 20 20 20 45 78 70 72 53  oin ){.    ExprS
2a5aa 65 74 50 72 6f 70 65 72 74 79 28 70 45 71 2c 20  etProperty(pEq, 
2a5ab 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20  EP_FromJoin);.  
2a5ac 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2a5ad 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
2a5ae 71 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  q, EP_TokenOnly|
2a5af 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20  EP_Reduced) );. 
2a5b0 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75     ExprSetIrredu
2a5b1 63 69 62 6c 65 28 70 45 71 29 3b 0a 20 20 20 20  cible(pEq);.    
2a5b2 70 45 71 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54  pEq->iRightJoinT
2a5b3 61 62 6c 65 20 3d 20 28 69 31 36 29 70 45 32 2d  able = (i16)pE2-
2a5b4 3e 69 54 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 2a  >iTable;.  }.  *
2a5b5 70 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65  ppWhere = sqlite
2a5b6 33 45 78 70 72 41 6e 64 28 64 62 2c 20 2a 70 70  3ExprAnd(db, *pp
2a5b7 57 68 65 72 65 2c 20 70 45 71 29 3b 0a 7d 0a 0a  Where, pEq);.}..
2a5b8 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 50  /*.** Set the EP
2a5b9 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72  _FromJoin proper
2a5ba 74 79 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20  ty on all terms 
2a5bb 6f 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  of the given exp
2a5bc 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 41 6e 64 20  ression..** And 
2a5bd 73 65 74 20 74 68 65 20 45 78 70 72 2e 69 52 69  set the Expr.iRi
2a5be 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74 6f 20  ghtJoinTable to 
2a5bf 69 54 61 62 6c 65 20 66 6f 72 20 65 76 65 72 79  iTable for every
2a5c0 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20   term in the.** 
2a5c1 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a  expression..**.*
2a5c2 2a 20 54 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69  * The EP_FromJoi
2a5c3 6e 20 70 72 6f 70 65 72 74 79 20 69 73 20 75 73  n property is us
2a5c4 65 64 20 6f 6e 20 74 65 72 6d 73 20 6f 66 20 61  ed on terms of a
2a5c5 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  n expression to 
2a5c6 74 65 6c 6c 0a 2a 2a 20 74 68 65 20 4c 45 46 54  tell.** the LEFT
2a5c7 20 4f 55 54 45 52 20 4a 4f 49 4e 20 70 72 6f 63   OUTER JOIN proc
2a5c8 65 73 73 69 6e 67 20 6c 6f 67 69 63 20 74 68 61  essing logic tha
2a5c9 74 20 74 68 69 73 20 74 65 72 6d 20 69 73 20 70  t this term is p
2a5ca 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6a 6f  art of the.** jo
2a5cb 69 6e 20 72 65 73 74 72 69 63 74 69 6f 6e 20 73  in restriction s
2a5cc 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
2a5cd 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
2a5ce 73 65 20 61 6e 64 20 6e 6f 74 20 61 20 70 61 72  se and not a par
2a5cf 74 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 6f 72 65  t.** of the more
2a5d0 20 67 65 6e 65 72 61 6c 20 57 48 45 52 45 20 63   general WHERE c
2a5d1 6c 61 75 73 65 2e 20 20 54 68 65 73 65 20 74 65  lause.  These te
2a5d2 72 6d 73 20 61 72 65 20 6d 6f 76 65 64 20 6f 76  rms are moved ov
2a5d3 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 57 48 45  er to the.** WHE
2a5d4 52 45 20 63 6c 61 75 73 65 20 64 75 72 69 6e 67  RE clause during
2a5d5 20 6a 6f 69 6e 20 70 72 6f 63 65 73 73 69 6e 67   join processing
2a5d6 20 62 75 74 20 77 65 20 6e 65 65 64 20 74 6f 20   but we need to 
2a5d7 72 65 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68  remember that th
2a5d8 65 79 0a 2a 2a 20 6f 72 69 67 69 6e 61 74 65 64  ey.** originated
2a5d9 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53   in the ON or US
2a5da 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  ING clause..**.*
2a5db 2a 20 54 68 65 20 45 78 70 72 2e 69 52 69 67 68  * The Expr.iRigh
2a5dc 74 4a 6f 69 6e 54 61 62 6c 65 20 74 65 6c 6c 73  tJoinTable tells
2a5dd 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2a5de 65 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68 61  e processing tha
2a5df 74 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73  t the.** express
2a5e0 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 20 74  ion depends on t
2a5e1 61 62 6c 65 20 69 52 69 67 68 74 4a 6f 69 6e 54  able iRightJoinT
2a5e2 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 61  able even if tha
2a5e3 74 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 0a 2a  t table is not.*
2a5e4 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 6d 65 6e  * explicitly men
2a5e5 74 69 6f 6e 65 64 20 69 6e 20 74 68 65 20 65 78  tioned in the ex
2a5e6 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 61 74 20  pression.  That 
2a5e7 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6e  information is n
2a5e8 65 65 64 65 64 0a 2a 2a 20 66 6f 72 20 63 61 73  eeded.** for cas
2a5e9 65 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  es like this:.**
2a5ea 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  .**    SELECT * 
2a5eb 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49  FROM t1 LEFT JOI
2a5ec 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e  N t2 ON t1.a=t2.
2a5ed 62 20 41 4e 44 20 74 31 2e 78 3d 35 0a 2a 2a 0a  b AND t1.x=5.**.
2a5ee 2a 2a 20 54 68 65 20 77 68 65 72 65 20 63 6c 61  ** The where cla
2a5ef 75 73 65 20 6e 65 65 64 73 20 74 6f 20 64 65 66  use needs to def
2a5f0 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20  er the handling 
2a5f1 6f 66 20 74 68 65 20 74 31 2e 78 3d 35 0a 2a 2a  of the t1.x=5.**
2a5f2 20 74 65 72 6d 20 75 6e 74 69 6c 20 61 66 74 65   term until afte
2a5f3 72 20 74 68 65 20 74 32 20 6c 6f 6f 70 20 6f 66  r the t2 loop of
2a5f4 20 74 68 65 20 6a 6f 69 6e 2e 20 20 49 6e 20 74   the join.  In t
2a5f5 68 61 74 20 77 61 79 2c 20 61 0a 2a 2a 20 4e 55  hat way, a.** NU
2a5f6 4c 4c 20 74 32 20 72 6f 77 20 77 69 6c 6c 20 62  LL t2 row will b
2a5f7 65 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e 65  e inserted whene
2a5f8 76 65 72 20 74 31 2e 78 21 3d 35 2e 20 20 49 66  ver t1.x!=5.  If
2a5f9 20 77 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65   we do not.** de
2a5fa 66 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67  fer the handling
2a5fb 20 6f 66 20 74 31 2e 78 3d 35 2c 20 69 74 20 77   of t1.x=5, it w
2a5fc 69 6c 6c 20 62 65 20 70 72 6f 63 65 73 73 65 64  ill be processed
2a5fd 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20   immediately.** 
2a5fe 61 66 74 65 72 20 74 68 65 20 74 31 20 6c 6f 6f  after the t1 loo
2a5ff 70 20 61 6e 64 20 72 6f 77 73 20 77 69 74 68 20  p and rows with 
2a600 74 31 2e 78 21 3d 35 20 77 69 6c 6c 20 6e 65 76  t1.x!=5 will nev
2a601 65 72 20 61 70 70 65 61 72 20 69 6e 0a 2a 2a 20  er appear in.** 
2a602 74 68 65 20 6f 75 74 70 75 74 2c 20 77 68 69 63  the output, whic
2a603 68 20 69 73 20 69 6e 63 6f 72 72 65 63 74 2e 0a  h is incorrect..
2a604 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
2a605 65 74 4a 6f 69 6e 45 78 70 72 28 45 78 70 72 20  etJoinExpr(Expr 
2a606 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 29 7b  *p, int iTable){
2a607 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
2a608 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
2a609 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69  ty(p, EP_FromJoi
2a60a 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  n);.    assert( 
2a60b 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
2a60c 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f  rty(p, EP_TokenO
2a60d 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20  nly|EP_Reduced) 
2a60e 29 3b 0a 20 20 20 20 45 78 70 72 53 65 74 49 72  );.    ExprSetIr
2a60f 72 65 64 75 63 69 62 6c 65 28 70 29 3b 0a 20 20  reducible(p);.  
2a610 20 20 70 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54    p->iRightJoinT
2a611 61 62 6c 65 20 3d 20 28 69 31 36 29 69 54 61 62  able = (i16)iTab
2a612 6c 65 3b 0a 20 20 20 20 73 65 74 4a 6f 69 6e 45  le;.    setJoinE
2a613 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 69 54  xpr(p->pLeft, iT
2a614 61 62 6c 65 29 3b 0a 20 20 20 20 70 20 3d 20 70  able);.    p = p
2a615 2d 3e 70 52 69 67 68 74 3b 0a 20 20 7d 20 0a 7d  ->pRight;.  } .}
2a616 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2a617 74 69 6e 65 20 70 72 6f 63 65 73 73 65 73 20 74  tine processes t
2a618 68 65 20 6a 6f 69 6e 20 69 6e 66 6f 72 6d 61 74  he join informat
2a619 69 6f 6e 20 66 6f 72 20 61 20 53 45 4c 45 43 54  ion for a SELECT
2a61a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 4f   statement..** O
2a61b 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
2a61c 73 65 73 20 61 72 65 20 63 6f 6e 76 65 72 74 65  ses are converte
2a61d 64 20 69 6e 74 6f 20 65 78 74 72 61 20 74 65 72  d into extra ter
2a61e 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  ms of the WHERE 
2a61f 63 6c 61 75 73 65 2e 0a 2a 2a 20 4e 41 54 55 52  clause..** NATUR
2a620 41 4c 20 6a 6f 69 6e 73 20 61 6c 73 6f 20 63 72  AL joins also cr
2a621 65 61 74 65 20 65 78 74 72 61 20 57 48 45 52 45  eate extra WHERE
2a622 20 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a   clause terms..*
2a623 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 6f  *.** The terms o
2a624 66 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  f a FROM clause 
2a625 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  are contained in
2a626 20 74 68 65 20 53 65 6c 65 63 74 2e 70 53 72 63   the Select.pSrc
2a627 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54   structure..** T
2a628 68 65 20 6c 65 66 74 20 6d 6f 73 74 20 74 61 62  he left most tab
2a629 6c 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20  le is the first 
2a62a 65 6e 74 72 79 20 69 6e 20 53 65 6c 65 63 74 2e  entry in Select.
2a62b 70 53 72 63 2e 20 20 54 68 65 20 72 69 67 68 74  pSrc.  The right
2a62c 2d 6d 6f 73 74 0a 2a 2a 20 74 61 62 6c 65 20 69  -most.** table i
2a62d 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  s the last entry
2a62e 2e 20 20 54 68 65 20 6a 6f 69 6e 20 6f 70 65 72  .  The join oper
2a62f 61 74 6f 72 20 69 73 20 68 65 6c 64 20 69 6e 20  ator is held in 
2a630 74 68 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20  the entry to.** 
2a631 74 68 65 20 6c 65 66 74 2e 20 20 54 68 75 73 20  the left.  Thus 
2a632 65 6e 74 72 79 20 30 20 63 6f 6e 74 61 69 6e 73  entry 0 contains
2a633 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74   the join operat
2a634 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 69 6e 20  or for the join 
2a635 62 65 74 77 65 65 6e 0a 2a 2a 20 65 6e 74 72 69  between.** entri
2a636 65 73 20 30 20 61 6e 64 20 31 2e 20 20 41 6e 79  es 0 and 1.  Any
2a637 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
2a638 75 73 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  uses associated 
2a639 77 69 74 68 20 74 68 65 20 6a 6f 69 6e 20 61 72  with the join ar
2a63a 65 0a 2a 2a 20 61 6c 73 6f 20 61 74 74 61 63 68  e.** also attach
2a63b 65 64 20 74 6f 20 74 68 65 20 6c 65 66 74 20 65  ed to the left e
2a63c 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ntry..**.** This
2a63d 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2a63e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
2a63f 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65  rrors encountere
2a640 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
2a641 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f   sqliteProcessJo
2a642 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  in(Parse *pParse
2a643 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
2a644 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20  SrcList *pSrc;  
2a645 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a646 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20 69 6e  /* All tables in
2a647 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2a648 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20   */.  int i, j; 
2a649 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a64a 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2a64b 75 6e 74 65 72 73 20 2a 2f 0a 20 20 73 74 72 75  unters */.  stru
2a64c 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2a64d 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c  *pLeft;     /* L
2a64e 65 66 74 20 74 61 62 6c 65 20 62 65 69 6e 67 20  eft table being 
2a64f 6a 6f 69 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75  joined */.  stru
2a650 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2a651 2a 70 52 69 67 68 74 3b 20 20 20 20 2f 2a 20 52  *pRight;    /* R
2a652 69 67 68 74 20 74 61 62 6c 65 20 62 65 69 6e 67  ight table being
2a653 20 6a 6f 69 6e 65 64 20 2a 2f 0a 0a 20 20 70 53   joined */..  pS
2a654 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20  rc = p->pSrc;.  
2a655 70 4c 65 66 74 20 3d 20 26 70 53 72 63 2d 3e 61  pLeft = &pSrc->a
2a656 5b 30 5d 3b 0a 20 20 70 52 69 67 68 74 20 3d 20  [0];.  pRight = 
2a657 26 70 4c 65 66 74 5b 31 5d 3b 0a 20 20 66 6f 72  &pLeft[1];.  for
2a658 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53  (i=0; i<pSrc->nS
2a659 72 63 2d 31 3b 20 69 2b 2b 2c 20 70 52 69 67 68  rc-1; i++, pRigh
2a65a 74 2b 2b 2c 20 70 4c 65 66 74 2b 2b 29 7b 0a 20  t++, pLeft++){. 
2a65b 20 20 20 54 61 62 6c 65 20 2a 70 4c 65 66 74 54     Table *pLeftT
2a65c 61 62 20 3d 20 70 4c 65 66 74 2d 3e 70 54 61 62  ab = pLeft->pTab
2a65d 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 52 69  ;.    Table *pRi
2a65e 67 68 74 54 61 62 20 3d 20 70 52 69 67 68 74 2d  ghtTab = pRight-
2a65f 3e 70 54 61 62 3b 0a 20 20 20 20 69 6e 74 20 69  >pTab;.    int i
2a660 73 4f 75 74 65 72 3b 0a 0a 20 20 20 20 69 66 28  sOuter;..    if(
2a661 20 4e 45 56 45 52 28 70 4c 65 66 74 54 61 62 3d   NEVER(pLeftTab=
2a662 3d 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d  =0 || pRightTab=
2a663 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  =0) ) continue;.
2a664 20 20 20 20 69 73 4f 75 74 65 72 20 3d 20 28 70      isOuter = (p
2a665 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20  Right->jointype 
2a666 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b 0a  & JT_OUTER)!=0;.
2a667 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65  .    /* When the
2a668 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64   NATURAL keyword
2a669 20 69 73 20 70 72 65 73 65 6e 74 2c 20 61 64 64   is present, add
2a66a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
2a66b 72 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65  rms for.    ** e
2a66c 76 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74  very column that
2a66d 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20   the two tables 
2a66e 68 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a  have in common..
2a66f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
2a670 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20  Right->jointype 
2a671 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a  & JT_NATURAL ){.
2a672 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74        if( pRight
2a673 2d 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d  ->pOn || pRight-
2a674 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20  >pUsing ){.     
2a675 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2a676 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 4e 41  sg(pParse, "a NA
2a677 54 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e  TURAL join may n
2a678 6f 74 20 68 61 76 65 20 22 0a 20 20 20 20 20 20  ot have ".      
2a679 20 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55       "an ON or U
2a67a 53 49 4e 47 20 63 6c 61 75 73 65 22 2c 20 30 29  SING clause", 0)
2a67b 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2a67c 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
2a67d 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65    for(j=0; j<pLe
2a67e 66 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  ftTab->nCol; j++
2a67f 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
2a680 2a 7a 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61  *zName = pLeftTa
2a681 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65  b->aCol[j].zName
2a682 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 52  ;.        int iR
2a683 69 67 68 74 43 6f 6c 20 3d 20 63 6f 6c 75 6d 6e  ightCol = column
2a684 49 6e 64 65 78 28 70 52 69 67 68 74 54 61 62 2c  Index(pRightTab,
2a685 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
2a686 20 69 66 28 20 69 52 69 67 68 74 43 6f 6c 3e 3d   if( iRightCol>=
2a687 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  0 ){.          a
2a688 64 64 57 68 65 72 65 54 65 72 6d 28 70 50 61 72  ddWhereTerm(pPar
2a689 73 65 2c 20 70 53 72 63 2c 20 69 2c 20 6a 2c 20  se, pSrc, i, j, 
2a68a 69 52 69 67 68 74 43 6f 6c 2c 20 69 73 4f 75 74  iRightCol, isOut
2a68b 65 72 2c 20 26 70 2d 3e 70 57 68 65 72 65 29 3b  er, &p->pWhere);
2a68c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2a68d 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
2a68e 20 44 69 73 61 6c 6c 6f 77 20 62 6f 74 68 20 4f   Disallow both O
2a68f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
2a690 73 65 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ses in the same 
2a691 6a 6f 69 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  join.    */.    
2a692 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20  if( pRight->pOn 
2a693 26 26 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e  && pRight->pUsin
2a694 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  g ){.      sqlit
2a695 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2a696 65 2c 20 22 63 61 6e 6e 6f 74 20 68 61 76 65 20  e, "cannot have 
2a697 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55 53 49 4e  both ON and USIN
2a698 47 20 22 0a 20 20 20 20 20 20 20 20 22 63 6c 61  G ".        "cla
2a699 75 73 65 73 20 69 6e 20 74 68 65 20 73 61 6d 65  uses in the same
2a69a 20 6a 6f 69 6e 22 29 3b 0a 20 20 20 20 20 20 72   join");.      r
2a69b 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a  eturn 1;.    }..
2a69c 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 4f      /* Add the O
2a69d 4e 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20  N clause to the 
2a69e 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52 45  end of the WHERE
2a69f 20 63 6c 61 75 73 65 2c 20 63 6f 6e 6e 65 63 74   clause, connect
2a6a0 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 61 6e 20  ed by.    ** an 
2a6a1 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20  AND operator..  
2a6a2 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69    */.    if( pRi
2a6a3 67 68 74 2d 3e 70 4f 6e 20 29 7b 0a 20 20 20 20  ght->pOn ){.    
2a6a4 20 20 69 66 28 20 69 73 4f 75 74 65 72 20 29 20    if( isOuter ) 
2a6a5 73 65 74 4a 6f 69 6e 45 78 70 72 28 70 52 69 67  setJoinExpr(pRig
2a6a6 68 74 2d 3e 70 4f 6e 2c 20 70 52 69 67 68 74 2d  ht->pOn, pRight-
2a6a7 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >iCursor);.     
2a6a8 20 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c   p->pWhere = sql
2a6a9 69 74 65 33 45 78 70 72 41 6e 64 28 70 50 61 72  ite3ExprAnd(pPar
2a6aa 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 57 68 65 72  se->db, p->pWher
2a6ab 65 2c 20 70 52 69 67 68 74 2d 3e 70 4f 6e 29 3b  e, pRight->pOn);
2a6ac 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 70  .      pRight->p
2a6ad 4f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  On = 0;.    }.. 
2a6ae 20 20 20 2f 2a 20 43 72 65 61 74 65 20 65 78 74     /* Create ext
2a6af 72 61 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20  ra terms on the 
2a6b0 57 48 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72  WHERE clause for
2a6b1 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6e 61 6d   each column nam
2a6b2 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  ed.    ** in the
2a6b3 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 20 20   USING clause.  
2a6b4 45 78 61 6d 70 6c 65 3a 20 49 66 20 74 68 65 20  Example: If the 
2a6b5 74 77 6f 20 74 61 62 6c 65 73 20 74 6f 20 62 65  two tables to be
2a6b6 20 6a 6f 69 6e 65 64 20 61 72 65 20 0a 20 20 20   joined are .   
2a6b7 20 2a 2a 20 41 20 61 6e 64 20 42 20 61 6e 64 20   ** A and B and 
2a6b8 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  the USING clause
2a6b9 20 6e 61 6d 65 73 20 58 2c 20 59 2c 20 61 6e 64   names X, Y, and
2a6ba 20 5a 2c 20 74 68 65 6e 20 61 64 64 20 74 68 69   Z, then add thi
2a6bb 73 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  s.    ** to the 
2a6bc 57 48 45 52 45 20 63 6c 61 75 73 65 3a 20 20 20  WHERE clause:   
2a6bd 20 41 2e 58 3d 42 2e 58 20 41 4e 44 20 41 2e 59   A.X=B.X AND A.Y
2a6be 3d 42 2e 59 20 41 4e 44 20 41 2e 5a 3d 42 2e 5a  =B.Y AND A.Z=B.Z
2a6bf 0a 20 20 20 20 2a 2a 20 52 65 70 6f 72 74 20 61  .    ** Report a
2a6c0 6e 20 65 72 72 6f 72 20 69 66 20 61 6e 79 20 63  n error if any c
2a6c1 6f 6c 75 6d 6e 20 6d 65 6e 74 69 6f 6e 65 64 20  olumn mentioned 
2a6c2 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61  in the USING cla
2a6c3 75 73 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f  use is.    ** no
2a6c4 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 62  t contained in b
2a6c5 6f 74 68 20 74 61 62 6c 65 73 20 74 6f 20 62 65  oth tables to be
2a6c6 20 6a 6f 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a   joined..    */.
2a6c7 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
2a6c8 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20  pUsing ){.      
2a6c9 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  IdList *pList = 
2a6ca 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 3b 0a  pRight->pUsing;.
2a6cb 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
2a6cc 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a 2b 2b  <pList->nId; j++
2a6cd 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
2a6ce 2a 7a 4e 61 6d 65 20 3d 20 70 4c 69 73 74 2d 3e  *zName = pList->
2a6cf 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  a[j].zName;.    
2a6d0 20 20 20 20 69 6e 74 20 69 4c 65 66 74 43 6f 6c      int iLeftCol
2a6d1 20 3d 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70   = columnIndex(p
2a6d2 4c 65 66 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3b  LeftTab, zName);
2a6d3 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 52 69  .        int iRi
2a6d4 67 68 74 43 6f 6c 20 3d 20 63 6f 6c 75 6d 6e 49  ghtCol = columnI
2a6d5 6e 64 65 78 28 70 52 69 67 68 74 54 61 62 2c 20  ndex(pRightTab, 
2a6d6 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
2a6d7 69 66 28 20 69 4c 65 66 74 43 6f 6c 3c 30 20 7c  if( iLeftCol<0 |
2a6d8 7c 20 69 52 69 67 68 74 43 6f 6c 3c 30 20 29 7b  | iRightCol<0 ){
2a6d9 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2a6da 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2a6db 65 2c 20 22 63 61 6e 6e 6f 74 20 6a 6f 69 6e 20  e, "cannot join 
2a6dc 75 73 69 6e 67 20 63 6f 6c 75 6d 6e 20 25 73 20  using column %s 
2a6dd 2d 20 63 6f 6c 75 6d 6e 20 22 0a 20 20 20 20 20  - column ".     
2a6de 20 20 20 20 20 20 20 22 6e 6f 74 20 70 72 65 73         "not pres
2a6df 65 6e 74 20 69 6e 20 62 6f 74 68 20 74 61 62 6c  ent in both tabl
2a6e0 65 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  es", zName);.   
2a6e1 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
2a6e2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2a6e3 20 20 20 61 64 64 57 68 65 72 65 54 65 72 6d 28     addWhereTerm(
2a6e4 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20 69 2c  pParse, pSrc, i,
2a6e5 20 69 4c 65 66 74 43 6f 6c 2c 20 69 52 69 67 68   iLeftCol, iRigh
2a6e6 74 43 6f 6c 2c 20 69 73 4f 75 74 65 72 2c 20 26  tCol, isOuter, &
2a6e7 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20  p->pWhere);.    
2a6e8 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
2a6e9 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
2a6ea 2a 2a 20 49 6e 73 65 72 74 20 63 6f 64 65 20 69  ** Insert code i
2a6eb 6e 74 6f 20 22 76 22 20 74 68 61 74 20 77 69 6c  nto "v" that wil
2a6ec 6c 20 70 75 73 68 20 74 68 65 20 72 65 63 6f 72  l push the recor
2a6ed 64 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20  d on the top of 
2a6ee 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69 6e 74  the.** stack int
2a6ef 6f 20 74 68 65 20 73 6f 72 74 65 72 2e 0a 2a 2f  o the sorter..*/
2a6f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 75 73  .static void pus
2a6f1 68 4f 6e 74 6f 53 6f 72 74 65 72 28 0a 20 20 50  hOntoSorter(.  P
2a6f2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2a6f3 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20        /* Parser 
2a6f4 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
2a6f5 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c  rList *pOrderBy,
2a6f6 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
2a6f7 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
2a6f8 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
2a6f9 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68         /* The wh
2a6fa 6f 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  ole SELECT state
2a6fb 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ment */.  int re
2a6fc 67 44 61 74 61 20 20 20 20 20 20 20 20 20 20 20  gData           
2a6fd 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
2a6fe 64 69 6e 67 20 64 61 74 61 20 74 6f 20 62 65 20  ding data to be 
2a6ff 73 6f 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 56  sorted */.){.  V
2a700 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2a701 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6e 45  >pVdbe;.  int nE
2a702 78 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  xpr = pOrderBy->
2a703 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20 72 65 67  nExpr;.  int reg
2a704 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65  Base = sqlite3Ge
2a705 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
2a706 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a 20 20 69  e, nExpr+2);.  i
2a707 6e 74 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73  nt regRecord = s
2a708 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2a709 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
2a70a 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
2a70b 72 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  r(pParse);.  sql
2a70c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
2a70d 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4f 72  List(pParse, pOr
2a70e 64 65 72 42 79 2c 20 72 65 67 42 61 73 65 2c 20  derBy, regBase, 
2a70f 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  0);.  sqlite3Vdb
2a710 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65  eAddOp2(v, OP_Se
2a711 71 75 65 6e 63 65 2c 20 70 4f 72 64 65 72 42 79  quence, pOrderBy
2a712 2d 3e 69 45 43 75 72 73 6f 72 2c 20 72 65 67 42  ->iECursor, regB
2a713 61 73 65 2b 6e 45 78 70 72 29 3b 0a 20 20 73 71  ase+nExpr);.  sq
2a714 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
2a715 65 28 70 50 61 72 73 65 2c 20 72 65 67 44 61 74  e(pParse, regDat
2a716 61 2c 20 72 65 67 42 61 73 65 2b 6e 45 78 70 72  a, regBase+nExpr
2a717 2b 31 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65  +1, 1);.  sqlite
2a718 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2a719 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
2a71a 67 42 61 73 65 2c 20 6e 45 78 70 72 20 2b 20 32  gBase, nExpr + 2
2a71b 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
2a71c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a71d 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
2a71e 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43  t, pOrderBy->iEC
2a71f 75 72 73 6f 72 2c 20 72 65 67 52 65 63 6f 72 64  ursor, regRecord
2a720 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
2a721 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2a722 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  e, regRecord);. 
2a723 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2a724 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2a725 20 72 65 67 42 61 73 65 2c 20 6e 45 78 70 72 2b   regBase, nExpr+
2a726 32 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63  2);.  if( pSelec
2a727 74 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  t->iLimit ){.   
2a728 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64 64 72   int addr1, addr
2a729 32 3b 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d 69  2;.    int iLimi
2a72a 74 3b 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65  t;.    if( pSele
2a72b 63 74 2d 3e 69 4f 66 66 73 65 74 20 29 7b 0a 20  ct->iOffset ){. 
2a72c 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 53       iLimit = pS
2a72d 65 6c 65 63 74 2d 3e 69 4f 66 66 73 65 74 2b 31  elect->iOffset+1
2a72e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2a72f 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c     iLimit = pSel
2a730 65 63 74 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20  ect->iLimit;.   
2a731 20 7d 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73   }.    addr1 = s
2a732 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2a733 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69  (v, OP_IfZero, i
2a734 4c 69 6d 69 74 29 3b 0a 20 20 20 20 73 71 6c 69  Limit);.    sqli
2a735 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a736 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4c 69 6d   OP_AddImm, iLim
2a737 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 61 64 64  it, -1);.    add
2a738 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r2 = sqlite3Vdbe
2a739 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74  AddOp0(v, OP_Got
2a73a 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  o);.    sqlite3V
2a73b 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2a73c 64 64 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  ddr1);.    sqlit
2a73d 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2a73e 4f 50 5f 4c 61 73 74 2c 20 70 4f 72 64 65 72 42  OP_Last, pOrderB
2a73f 79 2d 3e 69 45 43 75 72 73 6f 72 29 3b 0a 20 20  y->iECursor);.  
2a740 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a741 4f 70 31 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65  Op1(v, OP_Delete
2a742 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  , pOrderBy->iECu
2a743 72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  rsor);.    sqlit
2a744 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2a745 2c 20 61 64 64 72 32 29 3b 0a 20 20 20 20 70 53  , addr2);.    pS
2a746 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 20 3d 20  elect->iLimit = 
2a747 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
2a748 41 64 64 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c  Add code to impl
2a749 65 6d 65 6e 74 20 74 68 65 20 4f 46 46 53 45 54  ement the OFFSET
2a74a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2a74b 63 6f 64 65 4f 66 66 73 65 74 28 0a 20 20 56 64  codeOffset(.  Vd
2a74c 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20 20  be *v,          
2a74d 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2a74e 20 69 6e 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f   into this VM */
2a74f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
2a750 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
2a751 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  CT statement bei
2a752 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e  ng coded */.  in
2a753 74 20 69 43 6f 6e 74 69 6e 75 65 20 20 20 20 20  t iContinue     
2a754 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
2a755 73 6b 69 70 20 74 68 65 20 63 75 72 72 65 6e 74  skip the current
2a756 20 72 65 63 6f 72 64 20 2a 2f 0a 29 7b 0a 20 20   record */.){.  
2a757 69 66 28 20 70 2d 3e 69 4f 66 66 73 65 74 20 26  if( p->iOffset &
2a758 26 20 69 43 6f 6e 74 69 6e 75 65 21 3d 30 20 29  & iContinue!=0 )
2a759 7b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a  {.    int addr;.
2a75a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a75b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
2a75c 6d 6d 2c 20 70 2d 3e 69 4f 66 66 73 65 74 2c 20  mm, p->iOffset, 
2a75d 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20  -1);.    addr = 
2a75e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a75f 31 28 76 2c 20 4f 50 5f 49 66 4e 65 67 2c 20 70  1(v, OP_IfNeg, p
2a760 2d 3e 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  ->iOffset);.    
2a761 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a762 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2a763 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20   iContinue);.   
2a764 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2a765 20 22 73 6b 69 70 20 4f 46 46 53 45 54 20 72 65   "skip OFFSET re
2a766 63 6f 72 64 73 22 29 29 3b 0a 20 20 20 20 73 71  cords"));.    sq
2a767 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2a768 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a  e(v, addr);.  }.
2a769 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64  }../*.** Add cod
2a76a 65 20 74 68 61 74 20 77 69 6c 6c 20 63 68 65 63  e that will chec
2a76b 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  k to make sure t
2a76c 68 65 20 4e 20 72 65 67 69 73 74 65 72 73 20 73  he N registers s
2a76d 74 61 72 74 69 6e 67 20 61 74 20 69 4d 65 6d 0a  tarting at iMem.
2a76e 2a 2a 20 66 6f 72 6d 20 61 20 64 69 73 74 69 6e  ** form a distin
2a76f 63 74 20 65 6e 74 72 79 2e 20 20 69 54 61 62 20  ct entry.  iTab 
2a770 69 73 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64  is a sorting ind
2a771 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 70 72  ex that holds pr
2a772 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 73 65 65 6e  eviously.** seen
2a773 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66   combinations of
2a774 20 74 68 65 20 4e 20 76 61 6c 75 65 73 2e 20 20   the N values.  
2a775 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 6d  A new entry is m
2a776 61 64 65 20 69 6e 20 69 54 61 62 0a 2a 2a 20 69  ade in iTab.** i
2a777 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 4e 20  f the current N 
2a778 76 61 6c 75 65 73 20 61 72 65 20 6e 65 77 2e 0a  values are new..
2a779 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 74 6f 20  **.** A jump to 
2a77a 61 64 64 72 52 65 70 65 61 74 20 69 73 20 6d 61  addrRepeat is ma
2a77b 64 65 20 61 6e 64 20 74 68 65 20 4e 2b 31 20 76  de and the N+1 v
2a77c 61 6c 75 65 73 20 61 72 65 20 70 6f 70 70 65 64  alues are popped
2a77d 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 74 61   from the.** sta
2a77e 63 6b 20 69 66 20 74 68 65 20 74 6f 70 20 4e 20  ck if the top N 
2a77f 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74  elements are not
2a780 20 64 69 73 74 69 6e 63 74 2e 0a 2a 2f 0a 73 74   distinct..*/.st
2a781 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 44 69  atic void codeDi
2a782 73 74 69 6e 63 74 28 0a 20 20 50 61 72 73 65 20  stinct(.  Parse 
2a783 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
2a784 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65  Parsing and code
2a785 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74   generating cont
2a786 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ext */.  int iTa
2a787 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  b,          /* A
2a788 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 75   sorting index u
2a789 73 65 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20  sed to test for 
2a78a 64 69 73 74 69 6e 63 74 6e 65 73 73 20 2a 2f 0a  distinctness */.
2a78b 20 20 69 6e 74 20 61 64 64 72 52 65 70 65 61 74    int addrRepeat
2a78c 2c 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ,    /* Jump to 
2a78d 68 65 72 65 20 69 66 20 6e 6f 74 20 64 69 73 74  here if not dist
2a78e 69 6e 63 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c  inct */.  int N,
2a78f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a790 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  Number of elemen
2a791 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d  ts */.  int iMem
2a792 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
2a793 72 73 74 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29  rst element */.)
2a794 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  {.  Vdbe *v;.  i
2a795 6e 74 20 72 31 3b 0a 0a 20 20 76 20 3d 20 70 50  nt r1;..  v = pP
2a796 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 72  arse->pVdbe;.  r
2a797 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
2a798 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2a799 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a79a 70 34 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e  p4Int(v, OP_Foun
2a79b 64 2c 20 69 54 61 62 2c 20 61 64 64 72 52 65 70  d, iTab, addrRep
2a79c 65 61 74 2c 20 69 4d 65 6d 2c 20 4e 29 3b 0a 20  eat, iMem, N);. 
2a79d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a79e 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
2a79f 6f 72 64 2c 20 69 4d 65 6d 2c 20 4e 2c 20 72 31  ord, iMem, N, r1
2a7a0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2a7a1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
2a7a2 49 6e 73 65 72 74 2c 20 69 54 61 62 2c 20 72 31  Insert, iTab, r1
2a7a3 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
2a7a4 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2a7a5 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  e, r1);.}../*.**
2a7a6 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72   Generate an err
2a7a7 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65 6e 20  or message when 
2a7a8 61 20 53 45 4c 45 43 54 20 69 73 20 75 73 65 64  a SELECT is used
2a7a9 20 77 69 74 68 69 6e 20 61 20 73 75 62 65 78 70   within a subexp
2a7aa 72 65 73 73 69 6f 6e 0a 2a 2a 20 28 65 78 61 6d  ression.** (exam
2a7ab 70 6c 65 3a 20 20 22 61 20 49 4e 20 28 53 45 4c  ple:  "a IN (SEL
2a7ac 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 6c 65  ECT * FROM table
2a7ad 29 22 29 20 62 75 74 20 69 74 20 68 61 73 20 6d  )") but it has m
2a7ae 6f 72 65 20 74 68 61 6e 20 31 20 72 65 73 75 6c  ore than 1 resul
2a7af 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20 57 65  t.** column.  We
2a7b0 20 64 6f 20 74 68 69 73 20 69 6e 20 61 20 73 75   do this in a su
2a7b1 62 72 6f 75 74 69 6e 65 20 62 65 63 61 75 73 65  broutine because
2a7b2 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72   the error occur
2a7b3 73 20 69 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  s in multiple.**
2a7b4 20 70 6c 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74   places..*/.stat
2a7b5 69 63 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 4d  ic int checkForM
2a7b6 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74  ultiColumnSelect
2a7b7 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65 20 2a  Error(.  Parse *
2a7b8 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
2a7b9 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 2e 20   Parse context. 
2a7ba 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
2a7bb 2a 70 44 65 73 74 2c 20 20 20 2f 2a 20 44 65 73  *pDest,   /* Des
2a7bc 74 69 6e 61 74 69 6f 6e 20 6f 66 20 53 45 4c 45  tination of SELE
2a7bd 43 54 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20  CT results */.  
2a7be 69 6e 74 20 6e 45 78 70 72 20 20 20 20 20 20 20  int nExpr       
2a7bf 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2a7c0 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
2a7c1 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 45 4c   returned by SEL
2a7c2 45 43 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ECT */.){.  int 
2a7c3 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65  eDest = pDest->e
2a7c4 44 65 73 74 3b 0a 20 20 69 66 28 20 6e 45 78 70  Dest;.  if( nExp
2a7c5 72 3e 31 20 26 26 20 28 65 44 65 73 74 3d 3d 53  r>1 && (eDest==S
2a7c6 52 54 5f 4d 65 6d 20 7c 7c 20 65 44 65 73 74 3d  RT_Mem || eDest=
2a7c7 3d 53 52 54 5f 53 65 74 29 20 29 7b 0a 20 20 20  =SRT_Set) ){.   
2a7c8 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2a7c9 28 70 50 61 72 73 65 2c 20 22 6f 6e 6c 79 20 61  (pParse, "only a
2a7ca 20 73 69 6e 67 6c 65 20 72 65 73 75 6c 74 20 61   single result a
2a7cb 6c 6c 6f 77 65 64 20 66 6f 72 20 22 0a 20 20 20  llowed for ".   
2a7cc 20 20 20 20 22 61 20 53 45 4c 45 43 54 20 74 68      "a SELECT th
2a7cd 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e  at is part of an
2a7ce 20 65 78 70 72 65 73 73 69 6f 6e 22 29 3b 0a 20   expression");. 
2a7cf 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
2a7d0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
2a7d1 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
2a7d2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
2a7d3 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65  nerates the code
2a7d4 20 66 6f 72 20 74 68 65 20 69 6e 73 69 64 65 20   for the inside 
2a7d5 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f  of the inner loo
2a7d6 70 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54  p.** of a SELECT
2a7d7 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 54 61  ..**.** If srcTa
2a7d8 62 20 61 6e 64 20 6e 43 6f 6c 75 6d 6e 20 61 72  b and nColumn ar
2a7d9 65 20 62 6f 74 68 20 7a 65 72 6f 2c 20 74 68 65  e both zero, the
2a7da 6e 20 74 68 65 20 70 45 4c 69 73 74 20 65 78 70  n the pEList exp
2a7db 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  ressions.** are 
2a7dc 65 76 61 6c 75 61 74 65 64 20 69 6e 20 6f 72 64  evaluated in ord
2a7dd 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 64 61  er to get the da
2a7de 74 61 20 66 6f 72 20 74 68 69 73 20 72 6f 77 2e  ta for this row.
2a7df 20 20 49 66 20 6e 43 6f 6c 75 6d 6e 3e 30 0a 2a    If nColumn>0.*
2a7e0 2a 20 74 68 65 6e 20 64 61 74 61 20 69 73 20 70  * then data is p
2a7e1 75 6c 6c 65 64 20 66 72 6f 6d 20 73 72 63 54 61  ulled from srcTa
2a7e2 62 20 61 6e 64 20 70 45 4c 69 73 74 20 69 73 20  b and pEList is 
2a7e3 75 73 65 64 20 6f 6e 6c 79 20 74 6f 20 67 65 74  used only to get
2a7e4 20 74 68 65 0a 2a 2a 20 64 61 74 61 74 79 70 65   the.** datatype
2a7e5 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  s for each colum
2a7e6 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
2a7e7 64 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f  d selectInnerLoo
2a7e8 70 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  p(.  Parse *pPar
2a7e9 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
2a7ea 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
2a7eb 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
2a7ec 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
2a7ed 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  /* The complete 
2a7ee 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
2a7ef 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a   being coded */.
2a7f0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2a7f1 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  st,       /* Lis
2a7f2 74 20 6f 66 20 76 61 6c 75 65 73 20 62 65 69 6e  t of values bein
2a7f3 67 20 65 78 74 72 61 63 74 65 64 20 2a 2f 0a 20  g extracted */. 
2a7f4 20 69 6e 74 20 73 72 63 54 61 62 2c 20 20 20 20   int srcTab,    
2a7f5 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 6c 6c           /* Pull
2a7f6 20 64 61 74 61 20 66 72 6f 6d 20 74 68 69 73 20   data from this 
2a7f7 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
2a7f8 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20  Column,         
2a7f9 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2a7fa 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73  columns in the s
2a7fb 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20  ource table */. 
2a7fc 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2a7fd 72 42 79 2c 20 20 20 20 20 2f 2a 20 49 66 20 6e  rBy,     /* If n
2a7fe 6f 74 20 4e 55 4c 4c 2c 20 73 6f 72 74 20 72 65  ot NULL, sort re
2a7ff 73 75 6c 74 73 20 75 73 69 6e 67 20 74 68 69 73  sults using this
2a800 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 64 69   key */.  int di
2a801 73 74 69 6e 63 74 2c 20 20 20 20 20 20 20 20 20  stinct,         
2a802 20 20 2f 2a 20 49 66 20 3e 3d 30 2c 20 6d 61 6b    /* If >=0, mak
2a803 65 20 73 75 72 65 20 72 65 73 75 6c 74 73 20 61  e sure results a
2a804 72 65 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20  re distinct */. 
2a805 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65   SelectDest *pDe
2a806 73 74 2c 20 20 20 20 20 20 2f 2a 20 48 6f 77 20  st,      /* How 
2a807 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68  to dispose of th
2a808 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69  e results */.  i
2a809 6e 74 20 69 43 6f 6e 74 69 6e 75 65 2c 20 20 20  nt iContinue,   
2a80a 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
2a80b 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  ere to continue 
2a80c 77 69 74 68 20 6e 65 78 74 20 72 6f 77 20 2a 2f  with next row */
2a80d 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20  .  int iBreak   
2a80e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75             /* Ju
2a80f 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b  mp here to break
2a810 20 6f 75 74 20 6f 66 20 74 68 65 20 69 6e 6e 65   out of the inne
2a811 72 20 6c 6f 6f 70 20 2a 2f 0a 29 7b 0a 20 20 56  r loop */.){.  V
2a812 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2a813 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b  >pVdbe;.  int i;
2a814 0a 20 20 69 6e 74 20 68 61 73 44 69 73 74 69 6e  .  int hasDistin
2a815 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ct;        /* Tr
2a816 75 65 20 69 66 20 74 68 65 20 44 49 53 54 49 4e  ue if the DISTIN
2a817 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72  CT keyword is pr
2a818 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72  esent */.  int r
2a819 65 67 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20  egResult;       
2a81a 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20         /* Start 
2a81b 6f 66 20 6d 65 6d 6f 72 79 20 68 6f 6c 64 69 6e  of memory holdin
2a81c 67 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  g result set */.
2a81d 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44    int eDest = pD
2a81e 65 73 74 2d 3e 65 44 65 73 74 3b 20 20 20 2f 2a  est->eDest;   /*
2a81f 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20   How to dispose 
2a820 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20  of results */.  
2a821 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73  int iParm = pDes
2a822 74 2d 3e 69 50 61 72 6d 3b 20 20 20 2f 2a 20 46  t->iParm;   /* F
2a823 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
2a824 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64   disposal method
2a825 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c   */.  int nResul
2a826 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  tCol;           
2a827 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
2a828 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f  esult columns */
2a829 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b  ..  assert( v );
2a82a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d  .  if( NEVER(v==
2a82b 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  0) ) return;.  a
2a82c 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30  ssert( pEList!=0
2a82d 20 29 3b 0a 20 20 68 61 73 44 69 73 74 69 6e 63   );.  hasDistinc
2a82e 74 20 3d 20 64 69 73 74 69 6e 63 74 3e 3d 30 3b  t = distinct>=0;
2a82f 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d  .  if( pOrderBy=
2a830 3d 30 20 26 26 20 21 68 61 73 44 69 73 74 69 6e  =0 && !hasDistin
2a831 63 74 20 29 7b 0a 20 20 20 20 63 6f 64 65 4f 66  ct ){.    codeOf
2a832 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74  fset(v, p, iCont
2a833 69 6e 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  inue);.  }..  /*
2a834 20 50 75 6c 6c 20 74 68 65 20 72 65 71 75 65 73   Pull the reques
2a835 74 65 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a  ted columns..  *
2a836 2f 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e  /.  if( nColumn>
2a837 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74  0 ){.    nResult
2a838 43 6f 6c 20 3d 20 6e 43 6f 6c 75 6d 6e 3b 0a 20  Col = nColumn;. 
2a839 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 52 65 73   }else{.    nRes
2a83a 75 6c 74 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d  ultCol = pEList-
2a83b 3e 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 69 66  >nExpr;.  }.  if
2a83c 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30  ( pDest->iMem==0
2a83d 20 29 7b 0a 20 20 20 20 70 44 65 73 74 2d 3e 69   ){.    pDest->i
2a83e 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Mem = pParse->nM
2a83f 65 6d 2b 31 3b 0a 20 20 20 20 70 44 65 73 74 2d  em+1;.    pDest-
2a840 3e 6e 4d 65 6d 20 3d 20 6e 52 65 73 75 6c 74 43  >nMem = nResultC
2a841 6f 6c 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  ol;.    pParse->
2a842 6e 4d 65 6d 20 2b 3d 20 6e 52 65 73 75 6c 74 43  nMem += nResultC
2a843 6f 6c 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20  ol;.  }else{ .  
2a844 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74 2d    assert( pDest-
2a845 3e 6e 4d 65 6d 3d 3d 6e 52 65 73 75 6c 74 43 6f  >nMem==nResultCo
2a846 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 67 52 65  l );.  }.  regRe
2a847 73 75 6c 74 20 3d 20 70 44 65 73 74 2d 3e 69 4d  sult = pDest->iM
2a848 65 6d 3b 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d  em;.  if( nColum
2a849 6e 3e 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  n>0 ){.    for(i
2a84a 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69  =0; i<nColumn; i
2a84b 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
2a84c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2a84d 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61  OP_Column, srcTa
2a84e 62 2c 20 69 2c 20 72 65 67 52 65 73 75 6c 74 2b  b, i, regResult+
2a84f 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  i);.    }.  }els
2a850 65 20 69 66 28 20 65 44 65 73 74 21 3d 53 52 54  e if( eDest!=SRT
2a851 5f 45 78 69 73 74 73 20 29 7b 0a 20 20 20 20 2f  _Exists ){.    /
2a852 2a 20 49 66 20 74 68 65 20 64 65 73 74 69 6e 61  * If the destina
2a853 74 69 6f 6e 20 69 73 20 61 6e 20 45 58 49 53 54  tion is an EXIST
2a854 53 28 2e 2e 2e 29 20 65 78 70 72 65 73 73 69 6f  S(...) expressio
2a855 6e 2c 20 74 68 65 20 61 63 74 75 61 6c 0a 20 20  n, the actual.  
2a856 20 20 2a 2a 20 76 61 6c 75 65 73 20 72 65 74 75    ** values retu
2a857 72 6e 65 64 20 62 79 20 74 68 65 20 53 45 4c 45  rned by the SELE
2a858 43 54 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69  CT are not requi
2a859 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  red..    */.    
2a85a 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2a85b 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20  Clear(pParse);. 
2a85c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
2a85d 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  deExprList(pPars
2a85e 65 2c 20 70 45 4c 69 73 74 2c 20 72 65 67 52 65  e, pEList, regRe
2a85f 73 75 6c 74 2c 20 65 44 65 73 74 3d 3d 53 52 54  sult, eDest==SRT
2a860 5f 4f 75 74 70 75 74 29 3b 0a 20 20 7d 0a 20 20  _Output);.  }.  
2a861 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 75 6c  nColumn = nResul
2a862 74 43 6f 6c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  tCol;..  /* If t
2a863 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  he DISTINCT keyw
2a864 6f 72 64 20 77 61 73 20 70 72 65 73 65 6e 74 20  ord was present 
2a865 6f 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  on the SELECT st
2a866 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 61 6e 64  atement.  ** and
2a867 20 74 68 69 73 20 72 6f 77 20 68 61 73 20 62 65   this row has be
2a868 65 6e 20 73 65 65 6e 20 62 65 66 6f 72 65 2c 20  en seen before, 
2a869 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6d 61 6b 65  then do not make
2a86a 20 74 68 69 73 20 72 6f 77 0a 20 20 2a 2a 20 70   this row.  ** p
2a86b 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c  art of the resul
2a86c 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 68 61  t..  */.  if( ha
2a86d 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20  sDistinct ){.   
2a86e 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
2a86f 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
2a870 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  ( pEList->nExpr=
2a871 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20  =nColumn );.    
2a872 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50 61  codeDistinct(pPa
2a873 72 73 65 2c 20 64 69 73 74 69 6e 63 74 2c 20 69  rse, distinct, i
2a874 43 6f 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d  Continue, nColum
2a875 6e 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20  n, regResult);. 
2a876 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d     if( pOrderBy=
2a877 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 64 65  =0 ){.      code
2a878 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f  Offset(v, p, iCo
2a879 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 7d 0a 20  ntinue);.    }. 
2a87a 20 7d 0a 0a 20 20 69 66 28 20 63 68 65 63 6b 46   }..  if( checkF
2a87b 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c  orMultiColumnSel
2a87c 65 63 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c  ectError(pParse,
2a87d 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e   pDest, pEList->
2a87e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 72 65  nExpr) ){.    re
2a87f 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  turn;.  }..  swi
2a880 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a 20 20  tch( eDest ){.  
2a881 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64    /* In this mod
2a882 65 2c 20 77 72 69 74 65 20 65 61 63 68 20 71 75  e, write each qu
2a883 65 72 79 20 72 65 73 75 6c 74 20 74 6f 20 74 68  ery result to th
2a884 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 65 6d  e key of the tem
2a885 70 6f 72 61 72 79 0a 20 20 20 20 2a 2a 20 74 61  porary.    ** ta
2a886 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a  ble iParm..    *
2a887 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2a888 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
2a889 45 4c 45 43 54 0a 20 20 20 20 63 61 73 65 20 53  ELECT.    case S
2a88a 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20 20 20  RT_Union: {.    
2a88b 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20    int r1;.      
2a88c 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
2a88d 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
2a88e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a88f 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
2a890 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73  keRecord, regRes
2a891 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31  ult, nColumn, r1
2a892 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2a893 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2a894 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72  _IdxInsert, iPar
2a895 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  m, r1);.      sq
2a896 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2a897 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2a898 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2a899 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 73    }..    /* Cons
2a89a 74 72 75 63 74 20 61 20 72 65 63 6f 72 64 20 66  truct a record f
2a89b 72 6f 6d 20 74 68 65 20 71 75 65 72 79 20 72 65  rom the query re
2a89c 73 75 6c 74 2c 20 62 75 74 20 69 6e 73 74 65 61  sult, but instea
2a89d 64 20 6f 66 0a 20 20 20 20 2a 2a 20 73 61 76 69  d of.    ** savi
2a89e 6e 67 20 74 68 61 74 20 72 65 63 6f 72 64 2c 20  ng that record, 
2a89f 75 73 65 20 69 74 20 61 73 20 61 20 6b 65 79 20  use it as a key 
2a8a0 74 6f 20 64 65 6c 65 74 65 20 65 6c 65 6d 65 6e  to delete elemen
2a8a1 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74  ts from.    ** t
2a8a2 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
2a8a3 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f  le iParm..    */
2a8a4 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78  .    case SRT_Ex
2a8a5 63 65 70 74 3a 20 7b 0a 20 20 20 20 20 20 73 71  cept: {.      sq
2a8a6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2a8a7 76 2c 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c  v, OP_IdxDelete,
2a8a8 20 69 50 61 72 6d 2c 20 72 65 67 52 65 73 75 6c   iParm, regResul
2a8a9 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  t, nColumn);.   
2a8aa 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a8ab 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53  #endif..    /* S
2a8ac 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
2a8ad 61 73 20 64 61 74 61 20 75 73 69 6e 67 20 61 20  as data using a 
2a8ae 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20  unique key..    
2a8af 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
2a8b0 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
2a8b1 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
2a8b2 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
2a8b3 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2a8b4 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2a8b5 74 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d  testcase( eDest=
2a8b6 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20  =SRT_Table );.  
2a8b7 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
2a8b8 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61  est==SRT_EphemTa
2a8b9 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  b );.      sqlit
2a8ba 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2a8bb 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
2a8bc 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d  egResult, nColum
2a8bd 6e 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 69 66  n, r1);.      if
2a8be 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
2a8bf 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f        pushOntoSo
2a8c0 72 74 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72  rter(pParse, pOr
2a8c1 64 65 72 42 79 2c 20 70 2c 20 72 31 29 3b 0a 20  derBy, p, r1);. 
2a8c2 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2a8c3 20 20 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c      int r2 = sql
2a8c4 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
2a8c5 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
2a8c6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a8c7 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  2(v, OP_NewRowid
2a8c8 2c 20 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20  , iParm, r2);.  
2a8c9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a8ca 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
2a8cb 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 31 2c  sert, iParm, r1,
2a8cc 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r2);.        sq
2a8cd 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2a8ce 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45  5(v, OPFLAG_APPE
2a8cf 4e 44 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ND);.        sql
2a8d0 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2a8d1 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a  eg(pParse, r2);.
2a8d2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2a8d3 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2a8d4 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2a8d5 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2a8d6 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
2a8d7 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2a8d8 59 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61  Y.    /* If we a
2a8d9 72 65 20 63 72 65 61 74 69 6e 67 20 61 20 73 65  re creating a se
2a8da 74 20 66 6f 72 20 61 6e 20 22 65 78 70 72 20 49  t for an "expr I
2a8db 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20  N (SELECT ...)" 
2a8dc 63 6f 6e 73 74 72 75 63 74 2c 0a 20 20 20 20 2a  construct,.    *
2a8dd 2a 20 74 68 65 6e 20 74 68 65 72 65 20 73 68 6f  * then there sho
2a8de 75 6c 64 20 62 65 20 61 20 73 69 6e 67 6c 65 20  uld be a single 
2a8df 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74 61 63  item on the stac
2a8e0 6b 2e 20 20 57 72 69 74 65 20 74 68 69 73 0a 20  k.  Write this. 
2a8e1 20 20 20 2a 2a 20 69 74 65 6d 20 69 6e 74 6f 20     ** item into 
2a8e2 74 68 65 20 73 65 74 20 74 61 62 6c 65 20 77 69  the set table wi
2a8e3 74 68 20 62 6f 67 75 73 20 64 61 74 61 2e 0a 20  th bogus data.. 
2a8e4 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
2a8e5 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20 20 20  RT_Set: {.      
2a8e6 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d  assert( nColumn=
2a8e7 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  =1 );.      p->a
2a8e8 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65  ffinity = sqlite
2a8e9 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
2a8ea 28 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  (pEList->a[0].pE
2a8eb 78 70 72 2c 20 70 44 65 73 74 2d 3e 61 66 66 69  xpr, pDest->affi
2a8ec 6e 69 74 79 29 3b 0a 20 20 20 20 20 20 69 66 28  nity);.      if(
2a8ed 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20   pOrderBy ){.   
2a8ee 20 20 20 20 20 2f 2a 20 41 74 20 66 69 72 73 74       /* At first
2a8ef 20 67 6c 61 6e 63 65 20 79 6f 75 20 77 6f 75 6c   glance you woul
2a8f0 64 20 74 68 69 6e 6b 20 77 65 20 63 6f 75 6c 64  d think we could
2a8f1 20 6f 70 74 69 6d 69 7a 65 20 6f 75 74 20 74 68   optimize out th
2a8f2 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44  e.        ** ORD
2a8f3 45 52 20 42 59 20 69 6e 20 74 68 69 73 20 63 61  ER BY in this ca
2a8f4 73 65 20 73 69 6e 63 65 20 74 68 65 20 6f 72 64  se since the ord
2a8f5 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
2a8f6 20 74 68 65 20 73 65 74 0a 20 20 20 20 20 20 20   the set.       
2a8f7 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74   ** does not mat
2a8f8 74 65 72 2e 20 20 42 75 74 20 74 68 65 72 65 20  ter.  But there 
2a8f9 6d 69 67 68 74 20 62 65 20 61 20 4c 49 4d 49 54  might be a LIMIT
2a8fa 20 63 6c 61 75 73 65 2c 20 69 6e 20 77 68 69 63   clause, in whic
2a8fb 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73  h.        ** cas
2a8fc 65 20 74 68 65 20 6f 72 64 65 72 20 64 6f 65 73  e the order does
2a8fd 20 6d 61 74 74 65 72 20 2a 2f 0a 20 20 20 20 20   matter */.     
2a8fe 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65     pushOntoSorte
2a8ff 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  r(pParse, pOrder
2a900 42 79 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74  By, p, regResult
2a901 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2a902 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d          int r1 =
2a903 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2a904 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2a905 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a906 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp4(v, OP_Make
2a907 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c  Record, regResul
2a908 74 2c 20 31 2c 20 72 31 2c 20 26 70 2d 3e 61 66  t, 1, r1, &p->af
2a909 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20  finity, 1);.    
2a90a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2a90b 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
2a90c 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 52 65  ge(pParse, regRe
2a90d 73 75 6c 74 2c 20 31 29 3b 0a 20 20 20 20 20 20  sult, 1);.      
2a90e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a90f 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
2a910 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 31 29 3b  ert, iParm, r1);
2a911 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2a912 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2a913 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20  Parse, r1);.    
2a914 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
2a915 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
2a916 66 20 61 6e 79 20 72 6f 77 20 65 78 69 73 74 20  f any row exist 
2a917 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
2a918 74 2c 20 72 65 63 6f 72 64 20 74 68 61 74 20 66  t, record that f
2a919 61 63 74 20 61 6e 64 20 61 62 6f 72 74 2e 0a 20  act and abort.. 
2a91a 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
2a91b 52 54 5f 45 78 69 73 74 73 3a 20 7b 0a 20 20 20  RT_Exists: {.   
2a91c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2a91d 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2a91e 65 72 2c 20 31 2c 20 69 50 61 72 6d 29 3b 0a 20  er, 1, iParm);. 
2a91f 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49       /* The LIMI
2a920 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65  T clause will te
2a921 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70  rminate the loop
2a922 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20   for us */.     
2a923 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
2a924 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
2a925 20 61 20 73 63 61 6c 61 72 20 73 65 6c 65 63 74   a scalar select
2a926 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66   that is part of
2a927 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20   an expression, 
2a928 74 68 65 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72  then.    ** stor
2a929 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e  e the results in
2a92a 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
2a92b 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64   memory cell and
2a92c 20 62 72 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a   break out.    *
2a92d 2a 20 6f 66 20 74 68 65 20 73 63 61 6e 20 6c 6f  * of the scan lo
2a92e 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  op..    */.    c
2a92f 61 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20  ase SRT_Mem: {. 
2a930 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f       assert( nCo
2a931 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20  lumn==1 );.     
2a932 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
2a933 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74  .        pushOnt
2a934 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20  oSorter(pParse, 
2a935 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67  pOrderBy, p, reg
2a936 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d  Result);.      }
2a937 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
2a938 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
2a939 65 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 73  e(pParse, regRes
2a93a 75 6c 74 2c 20 69 50 61 72 6d 2c 20 31 29 3b 0a  ult, iParm, 1);.
2a93b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c          /* The L
2a93c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c  IMIT clause will
2a93d 20 6a 75 6d 70 20 6f 75 74 20 6f 66 20 74 68 65   jump out of the
2a93e 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a   loop for us */.
2a93f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2a940 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2a941 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
2a942 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2a943 59 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 6e  Y */..    /* Sen
2a944 64 20 74 68 65 20 64 61 74 61 20 74 6f 20 74 68  d the data to th
2a945 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
2a946 69 6f 6e 20 6f 72 20 74 6f 20 61 20 73 75 62 72  ion or to a subr
2a947 6f 75 74 69 6e 65 2e 20 20 49 6e 20 74 68 65 0a  outine.  In the.
2a948 20 20 20 20 2a 2a 20 63 61 73 65 20 6f 66 20 61      ** case of a
2a949 20 73 75 62 72 6f 75 74 69 6e 65 2c 20 74 68 65   subroutine, the
2a94a 20 73 75 62 72 6f 75 74 69 6e 65 20 69 74 73 65   subroutine itse
2a94b 6c 66 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  lf is responsibl
2a94c 65 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 6f 70  e for.    ** pop
2a94d 70 69 6e 67 20 74 68 65 20 64 61 74 61 20 66 72  ping the data fr
2a94e 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 0a 20 20  om the stack..  
2a94f 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
2a950 54 5f 43 6f 72 6f 75 74 69 6e 65 3a 0a 20 20 20  T_Coroutine:.   
2a951 20 63 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74   case SRT_Output
2a952 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61  : {.      testca
2a953 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43  se( eDest==SRT_C
2a954 6f 72 6f 75 74 69 6e 65 20 29 3b 0a 20 20 20 20  oroutine );.    
2a955 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73    testcase( eDes
2a956 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 3b  t==SRT_Output );
2a957 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
2a958 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69  rBy ){.        i
2a959 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  nt r1 = sqlite3G
2a95a 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2a95b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2a95c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2a95d 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
2a95e 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d  egResult, nColum
2a95f 6e 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  n, r1);.        
2a960 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70  pushOntoSorter(p
2a961 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c  Parse, pOrderBy,
2a962 20 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20   p, r1);.       
2a963 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2a964 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2a965 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  1);.      }else 
2a966 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43  if( eDest==SRT_C
2a967 6f 72 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20 20  oroutine ){.    
2a968 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a969 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2a96a 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  d, pDest->iParm)
2a96b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2a96c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2a96d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2a96e 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 65 73  esultRow, regRes
2a96f 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  ult, nColumn);. 
2a970 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2a971 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
2a972 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  hange(pParse, re
2a973 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e  gResult, nColumn
2a974 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2a975 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23   break;.    }..#
2a976 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
2a977 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
2a978 0a 20 20 20 20 2f 2a 20 44 69 73 63 61 72 64 20  .    /* Discard 
2a979 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 54 68  the results.  Th
2a97a 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 53  is is used for S
2a97b 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
2a97c 20 69 6e 73 69 64 65 0a 20 20 20 20 2a 2a 20 74   inside.    ** t
2a97d 68 65 20 62 6f 64 79 20 6f 66 20 61 20 54 52 49  he body of a TRI
2a97e 47 47 45 52 2e 20 20 54 68 65 20 70 75 72 70 6f  GGER.  The purpo
2a97f 73 65 20 6f 66 20 73 75 63 68 20 73 65 6c 65 63  se of such selec
2a980 74 73 20 69 73 20 74 6f 20 63 61 6c 6c 0a 20 20  ts is to call.  
2a981 20 20 2a 2a 20 75 73 65 72 2d 64 65 66 69 6e 65    ** user-define
2a982 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  d functions that
2a983 20 68 61 76 65 20 73 69 64 65 20 65 66 66 65 63   have side effec
2a984 74 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 63  ts.  We do not c
2a985 61 72 65 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74  are.    ** about
2a986 20 74 68 65 20 61 63 74 75 61 6c 20 72 65 73 75   the actual resu
2a987 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63  lts of the selec
2a988 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65  t..    */.    de
2a989 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61  fault: {.      a
2a98a 73 73 65 72 74 28 20 65 44 65 73 74 3d 3d 53 52  ssert( eDest==SR
2a98b 54 5f 44 69 73 63 61 72 64 20 29 3b 0a 20 20 20  T_Discard );.   
2a98c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a98d 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a  #endif.  }..  /*
2a98e 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64   Jump to the end
2a98f 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 66 20   of the loop if 
2a990 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65 61  the LIMIT is rea
2a991 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ched..  */.  if(
2a992 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20   p->iLimit ){.  
2a993 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72    assert( pOrder
2a994 42 79 3d 3d 30 20 29 3b 20 20 2f 2a 20 49 66 20  By==0 );  /* If 
2a995 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45  there is an ORDE
2a996 52 20 42 59 2c 20 74 68 65 20 63 61 6c 6c 20 74  R BY, the call t
2a997 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o.              
2a998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
2a999 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28   pushOntoSorter(
2a99a 29 20 77 6f 75 6c 64 20 68 61 76 65 20 63 6c 65  ) would have cle
2a99b 61 72 65 64 20 70 2d 3e 69 4c 69 6d 69 74 20 2a  ared p->iLimit *
2a99c 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
2a99d 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66  eAddOp3(v, OP_If
2a99e 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c  Zero, p->iLimit,
2a99f 20 69 42 72 65 61 6b 2c 20 2d 31 29 3b 0a 20 20   iBreak, -1);.  
2a9a0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  }.}../*.** Given
2a9a1 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c   an expression l
2a9a2 69 73 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20  ist, generate a 
2a9a3 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
2a9a4 65 20 74 68 61 74 20 72 65 63 6f 72 64 73 0a 2a  e that records.*
2a9a5 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  * the collating 
2a9a6 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 61 63  sequence for eac
2a9a7 68 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  h expression in 
2a9a8 74 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 20  that expression 
2a9a9 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  list..**.** If t
2a9aa 68 65 20 45 78 70 72 4c 69 73 74 20 69 73 20 61  he ExprList is a
2a9ab 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52  n ORDER BY or GR
2a9ac 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 74 68  OUP BY clause th
2a9ad 65 6e 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  en the resulting
2a9ae 0a 2a 2a 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  .** KeyInfo stru
2a9af 63 74 75 72 65 20 69 73 20 61 70 70 72 6f 70 72  cture is appropr
2a9b0 69 61 74 65 20 66 6f 72 20 69 6e 69 74 69 61 6c  iate for initial
2a9b1 69 7a 69 6e 67 20 61 20 76 69 72 74 75 61 6c 20  izing a virtual 
2a9b2 69 6e 64 65 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c  index to.** impl
2a9b3 65 6d 65 6e 74 20 74 68 61 74 20 63 6c 61 75 73  ement that claus
2a9b4 65 2e 20 20 49 66 20 74 68 65 20 45 78 70 72 4c  e.  If the ExprL
2a9b5 69 73 74 20 69 73 20 74 68 65 20 72 65 73 75 6c  ist is the resul
2a9b6 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43  t set of a SELEC
2a9b7 54 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 4b 65  T.** then the Ke
2a9b8 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
2a9b9 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
2a9ba 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  or initializing 
2a9bb 61 20 76 69 72 74 75 61 6c 0a 2a 2a 20 69 6e 64  a virtual.** ind
2a9bc 65 78 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ex to implement 
2a9bd 61 20 44 49 53 54 49 4e 43 54 20 74 65 73 74 2e  a DISTINCT test.
2a9be 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20  .**.** Space to 
2a9bf 68 6f 6c 64 20 74 68 65 20 4b 65 79 49 6e 66 6f  hold the KeyInfo
2a9c0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 6f 62   structure is ob
2a9c1 74 61 69 6e 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  tain from malloc
2a9c2 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a  .  The calling.*
2a9c3 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65  * function is re
2a9c4 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65  sponsible for se
2a9c5 65 69 6e 67 20 74 68 61 74 20 74 68 69 73 20 73  eing that this s
2a9c6 74 72 75 63 74 75 72 65 20 69 73 20 65 76 65 6e  tructure is even
2a9c7 74 75 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e  tually.** freed.
2a9c8 20 20 41 64 64 20 74 68 65 20 4b 65 79 49 6e 66    Add the KeyInf
2a9c9 6f 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 74  o structure to t
2a9ca 68 65 20 50 34 20 66 69 65 6c 64 20 6f 66 20 61  he P4 field of a
2a9cb 6e 20 6f 70 63 6f 64 65 20 75 73 69 6e 67 0a 2a  n opcode using.*
2a9cc 2a 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  * P4_KEYINFO_HAN
2a9cd 44 4f 46 46 20 69 73 20 74 68 65 20 75 73 75 61  DOFF is the usua
2a9ce 6c 20 77 61 79 20 6f 66 20 64 65 61 6c 69 6e 67  l way of dealing
2a9cf 20 77 69 74 68 20 74 68 69 73 2e 0a 2a 2f 0a 73   with this..*/.s
2a9d0 74 61 74 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b  tatic KeyInfo *k
2a9d1 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69  eyInfoFromExprLi
2a9d2 73 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  st(Parse *pParse
2a9d3 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73  , ExprList *pLis
2a9d4 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  t){.  sqlite3 *d
2a9d5 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2a9d6 20 20 69 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b    int nExpr;.  K
2a9d7 65 79 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20  eyInfo *pInfo;. 
2a9d8 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2a9d9 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
2a9da 69 6e 74 20 69 3b 0a 0a 20 20 6e 45 78 70 72 20  int i;..  nExpr 
2a9db 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a  = pList->nExpr;.
2a9dc 20 20 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65    pInfo = sqlite
2a9dd 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2a9de 2c 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29  , sizeof(*pInfo)
2a9df 20 2b 20 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66   + nExpr*(sizeof
2a9e0 28 43 6f 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b  (CollSeq*)+1) );
2a9e1 0a 20 20 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a  .  if( pInfo ){.
2a9e2 20 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74      pInfo->aSort
2a9e3 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70 49  Order = (u8*)&pI
2a9e4 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72  nfo->aColl[nExpr
2a9e5 5d 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 46  ];.    pInfo->nF
2a9e6 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 45 78 70  ield = (u16)nExp
2a9e7 72 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 65 6e  r;.    pInfo->en
2a9e8 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20  c = ENC(db);.   
2a9e9 20 70 49 6e 66 6f 2d 3e 64 62 20 3d 20 64 62 3b   pInfo->db = db;
2a9ea 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  .    for(i=0, pI
2a9eb 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c  tem=pList->a; i<
2a9ec 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
2a9ed 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c  m++){.      Coll
2a9ee 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20  Seq *pColl;.    
2a9ef 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2a9f0 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
2a9f1 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  rse, pItem->pExp
2a9f2 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  r);.      if( !p
2a9f3 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
2a9f4 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
2a9f5 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  tColl;.      }. 
2a9f6 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c       pInfo->aCol
2a9f7 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20  l[i] = pColl;.  
2a9f8 20 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74      pInfo->aSort
2a9f9 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d  Order[i] = pItem
2a9fa 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  ->sortOrder;.   
2a9fb 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2a9fc 70 49 6e 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  pInfo;.}.../*.**
2a9fd 20 49 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f   If the inner lo
2a9fe 6f 70 20 77 61 73 20 67 65 6e 65 72 61 74 65 64  op was generated
2a9ff 20 75 73 69 6e 67 20 61 20 6e 6f 6e 2d 6e 75 6c   using a non-nul
2aa00 6c 20 70 4f 72 64 65 72 42 79 20 61 72 67 75 6d  l pOrderBy argum
2aa01 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ent,.** then the
2aa02 20 72 65 73 75 6c 74 73 20 77 65 72 65 20 70 6c   results were pl
2aa03 61 63 65 64 20 69 6e 20 61 20 73 6f 72 74 65 72  aced in a sorter
2aa04 2e 20 20 41 66 74 65 72 20 74 68 65 20 6c 6f 6f  .  After the loo
2aa05 70 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64 0a  p is terminated.
2aa06 2a 2a 20 77 65 20 6e 65 65 64 20 74 6f 20 72 75  ** we need to ru
2aa07 6e 20 74 68 65 20 73 6f 72 74 65 72 20 61 6e 64  n the sorter and
2aa08 20 6f 75 74 70 75 74 20 74 68 65 20 72 65 73 75   output the resu
2aa09 6c 74 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  lts.  The follow
2aa0a 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67  ing.** routine g
2aa0b 65 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f 64  enerates the cod
2aa0c 65 20 6e 65 65 64 65 64 20 74 6f 20 64 6f 20 74  e needed to do t
2aa0d 68 61 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  hat..*/.static v
2aa0e 6f 69 64 20 67 65 6e 65 72 61 74 65 53 6f 72 74  oid generateSort
2aa0f 54 61 69 6c 28 0a 20 20 50 61 72 73 65 20 2a 70  Tail(.  Parse *p
2aa10 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72  Parse,    /* Par
2aa11 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2aa12 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20    Select *p,    
2aa13 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
2aa14 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  T statement */. 
2aa15 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20   Vdbe *v,       
2aa16 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
2aa17 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44  ode into this VD
2aa18 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  BE */.  int nCol
2aa19 75 6d 6e 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d  umn,      /* Num
2aa1a 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f  ber of columns o
2aa1b 66 20 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65  f data */.  Sele
2aa1c 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 2f 2a  ctDest *pDest /*
2aa1d 20 57 72 69 74 65 20 74 68 65 20 73 6f 72 74 65   Write the sorte
2aa1e 64 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a  d results here *
2aa1f 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 42  /.){.  int addrB
2aa20 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64  reak = sqlite3Vd
2aa21 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20  beMakeLabel(v); 
2aa22 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
2aa23 20 74 6f 20 65 78 69 74 20 6c 6f 6f 70 20 2a 2f   to exit loop */
2aa24 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 69  .  int addrConti
2aa25 6e 75 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  nue = sqlite3Vdb
2aa26 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20  eMakeLabel(v);  
2aa27 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 66 6f 72  /* Jump here for
2aa28 20 6e 65 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20   next cycle */. 
2aa29 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 69 6e 74   int addr;.  int
2aa2a 20 69 54 61 62 3b 0a 20 20 69 6e 74 20 70 73 65   iTab;.  int pse
2aa2b 75 64 6f 54 61 62 20 3d 20 30 3b 0a 20 20 45 78  udoTab = 0;.  Ex
2aa2c 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
2aa2d 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a   = p->pOrderBy;.
2aa2e 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70  .  int eDest = p
2aa2f 44 65 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69  Dest->eDest;.  i
2aa30 6e 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73 74  nt iParm = pDest
2aa31 2d 3e 69 50 61 72 6d 3b 0a 0a 20 20 69 6e 74 20  ->iParm;..  int 
2aa32 72 65 67 52 6f 77 3b 0a 20 20 69 6e 74 20 72 65  regRow;.  int re
2aa33 67 52 6f 77 69 64 3b 0a 0a 20 20 69 54 61 62 20  gRowid;..  iTab 
2aa34 3d 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  = pOrderBy->iECu
2aa35 72 73 6f 72 3b 0a 20 20 72 65 67 52 6f 77 20 3d  rsor;.  regRow =
2aa36 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2aa37 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  eg(pParse);.  if
2aa38 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  ( eDest==SRT_Out
2aa39 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52  put || eDest==SR
2aa3a 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a 20  T_Coroutine ){. 
2aa3b 20 20 20 70 73 65 75 64 6f 54 61 62 20 3d 20 70     pseudoTab = p
2aa3c 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
2aa3d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2aa3e 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 50  dOp3(v, OP_OpenP
2aa3f 73 65 75 64 6f 2c 20 70 73 65 75 64 6f 54 61 62  seudo, pseudoTab
2aa40 2c 20 72 65 67 52 6f 77 2c 20 6e 43 6f 6c 75 6d  , regRow, nColum
2aa41 6e 29 3b 0a 20 20 20 20 72 65 67 52 6f 77 69 64  n);.    regRowid
2aa42 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
2aa43 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71     regRowid = sq
2aa44 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2aa45 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 61  pParse);.  }.  a
2aa46 64 64 72 20 3d 20 31 20 2b 20 73 71 6c 69 74 65  ddr = 1 + sqlite
2aa47 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2aa48 50 5f 53 6f 72 74 2c 20 69 54 61 62 2c 20 61 64  P_Sort, iTab, ad
2aa49 64 72 42 72 65 61 6b 29 3b 0a 20 20 63 6f 64 65  drBreak);.  code
2aa4a 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 61 64 64  Offset(v, p, add
2aa4b 72 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73 71  rContinue);.  sq
2aa4c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2aa4d 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54  v, OP_Column, iT
2aa4e 61 62 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ab, pOrderBy->nE
2aa4f 78 70 72 20 2b 20 31 2c 20 72 65 67 52 6f 77 29  xpr + 1, regRow)
2aa50 3b 0a 20 20 73 77 69 74 63 68 28 20 65 44 65 73  ;.  switch( eDes
2aa51 74 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 52  t ){.    case SR
2aa52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73  T_Table:.    cas
2aa53 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20  e SRT_EphemTab: 
2aa54 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
2aa55 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 54 61 62  ( eDest==SRT_Tab
2aa56 6c 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  le );.      test
2aa57 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54  case( eDest==SRT
2aa58 5f 45 70 68 65 6d 54 61 62 20 29 3b 0a 20 20 20  _EphemTab );.   
2aa59 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2aa5a 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp2(v, OP_NewRo
2aa5b 77 69 64 2c 20 69 50 61 72 6d 2c 20 72 65 67 52  wid, iParm, regR
2aa5c 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c  owid);.      sql
2aa5d 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2aa5e 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61  , OP_Insert, iPa
2aa5f 72 6d 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52  rm, regRow, regR
2aa60 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c  owid);.      sql
2aa61 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
2aa62 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e  (v, OPFLAG_APPEN
2aa63 44 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  D);.      break;
2aa64 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
2aa65 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2aa66 45 52 59 0a 20 20 20 20 63 61 73 65 20 53 52 54  ERY.    case SRT
2aa67 5f 53 65 74 3a 20 7b 0a 20 20 20 20 20 20 61 73  _Set: {.      as
2aa68 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31  sert( nColumn==1
2aa69 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
2aa6a 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2aa6b 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
2aa6c 67 52 6f 77 2c 20 31 2c 20 72 65 67 52 6f 77 69  gRow, 1, regRowi
2aa6d 64 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c  d, &p->affinity,
2aa6e 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
2aa6f 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
2aa70 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
2aa71 2c 20 72 65 67 52 6f 77 2c 20 31 29 3b 0a 20 20  , regRow, 1);.  
2aa72 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2aa73 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
2aa74 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 65  nsert, iParm, re
2aa75 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 62  gRowid);.      b
2aa76 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2aa77 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a  case SRT_Mem: {.
2aa78 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43        assert( nC
2aa79 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20  olumn==1 );.    
2aa7a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2aa7b 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65  eMove(pParse, re
2aa7c 67 52 6f 77 2c 20 69 50 61 72 6d 2c 20 31 29 3b  gRow, iParm, 1);
2aa7d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49  .      /* The LI
2aa7e 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20  MIT clause will 
2aa7f 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f  terminate the lo
2aa80 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20  op for us */.   
2aa81 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2aa82 23 65 6e 64 69 66 0a 20 20 20 20 64 65 66 61 75  #endif.    defau
2aa83 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  lt: {.      int 
2aa84 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  i;.      assert(
2aa85 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70   eDest==SRT_Outp
2aa86 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54  ut || eDest==SRT
2aa87 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b 20 0a 20  _Coroutine ); . 
2aa88 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65       testcase( e
2aa89 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74  Dest==SRT_Output
2aa8a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2aa8b 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43  se( eDest==SRT_C
2aa8c 6f 72 6f 75 74 69 6e 65 20 29 3b 0a 20 20 20 20  oroutine );.    
2aa8d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
2aa8e 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
2aa8f 20 20 20 20 61 73 73 65 72 74 28 20 72 65 67 52      assert( regR
2aa90 6f 77 21 3d 70 44 65 73 74 2d 3e 69 4d 65 6d 2b  ow!=pDest->iMem+
2aa91 69 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  i );.        sql
2aa92 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2aa93 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 73 65  , OP_Column, pse
2aa94 75 64 6f 54 61 62 2c 20 69 2c 20 70 44 65 73 74  udoTab, i, pDest
2aa95 2d 3e 69 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 20  ->iMem+i);.     
2aa96 20 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20     if( i==0 ){. 
2aa97 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2aa98 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2aa99 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48  OPFLAG_CLEARCACH
2aa9a 45 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  E);.        }.  
2aa9b 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2aa9c 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
2aa9d 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
2aa9e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2aa9f 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
2aaa0 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43 6f  pDest->iMem, nCo
2aaa1 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 73  lumn);.        s
2aaa2 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
2aaa3 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
2aaa4 61 72 73 65 2c 20 70 44 65 73 74 2d 3e 69 4d 65  arse, pDest->iMe
2aaa5 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  m, nColumn);.   
2aaa6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2aaa7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2aaa8 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2aaa9 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a   pDest->iParm);.
2aaaa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2aaab 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
2aaac 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2aaad 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2aaae 65 67 52 6f 77 29 3b 0a 20 20 73 71 6c 69 74 65  egRow);.  sqlite
2aaaf 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2aab0 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64  pParse, regRowid
2aab1 29 3b 0a 0a 20 20 2f 2a 20 4c 49 4d 49 54 20 68  );..  /* LIMIT h
2aab2 61 73 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e  as been implemen
2aab3 74 65 64 20 62 79 20 74 68 65 20 70 75 73 68 4f  ted by the pushO
2aab4 6e 74 6f 53 6f 72 74 65 72 28 29 20 72 6f 75 74  ntoSorter() rout
2aab5 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ine..  */.  asse
2aab6 72 74 28 20 70 2d 3e 69 4c 69 6d 69 74 3d 3d 30  rt( p->iLimit==0
2aab7 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f   );..  /* The bo
2aab8 74 74 6f 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70  ttom of the loop
2aab9 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
2aaba 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2aabb 76 2c 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 29  v, addrContinue)
2aabc 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2aabd 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
2aabe 2c 20 69 54 61 62 2c 20 61 64 64 72 29 3b 0a 20  , iTab, addr);. 
2aabf 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2aac0 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
2aac1 42 72 65 61 6b 29 3b 0a 20 20 69 66 28 20 65 44  Break);.  if( eD
2aac2 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
2aac3 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f  || eDest==SRT_Co
2aac4 72 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 73  routine ){.    s
2aac5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2aac6 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 73  (v, OP_Close, ps
2aac7 65 75 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 7d  eudoTab, 0);.  }
2aac8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2aac9 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
2aaca 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
2aacb 67 20 74 68 65 20 27 64 65 63 6c 61 72 61 74 69  g the 'declarati
2aacc 6f 6e 20 74 79 70 65 27 20 6f 66 20 74 68 65 0a  on type' of the.
2aacd 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  ** expression pE
2aace 78 70 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20  xpr. The string 
2aacf 6d 61 79 20 62 65 20 74 72 65 61 74 65 64 20 61  may be treated a
2aad0 73 20 73 74 61 74 69 63 20 62 79 20 74 68 65 20  s static by the 
2aad1 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  caller..**.** Th
2aad2 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79  e declaration ty
2aad3 70 65 20 69 73 20 74 68 65 20 65 78 61 63 74 20  pe is the exact 
2aad4 64 61 74 61 74 79 70 65 20 64 65 66 69 6e 69 74  datatype definit
2aad5 69 6f 6e 20 65 78 74 72 61 63 74 65 64 20 66 72  ion extracted fr
2aad6 6f 6d 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e  om the.** origin
2aad7 61 6c 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  al CREATE TABLE 
2aad8 73 74 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65  statement if the
2aad9 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
2aada 20 63 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20   column. The.** 
2aadb 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
2aadc 20 66 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65   for a ROWID fie
2aadd 6c 64 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45  ld is INTEGER. E
2aade 78 61 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65  xactly when an e
2aadf 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20  xpression.** is 
2aae0 63 6f 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c  considered a col
2aae1 75 6d 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c  umn can be compl
2aae2 65 78 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e  ex in the presen
2aae3 63 65 20 6f 66 20 73 75 62 71 75 65 72 69 65 73  ce of subqueries
2aae4 2e 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d  . The.** result-
2aae5 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69  set expression i
2aae6 6e 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c  n all of the fol
2aae7 6c 6f 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74  lowing SELECT st
2aae8 61 74 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20  atements is .** 
2aae9 63 6f 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c  considered a col
2aaea 75 6d 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63  umn by this func
2aaeb 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45  tion..**.**   SE
2aaec 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62  LECT col FROM tb
2aaed 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28  l;.**   SELECT (
2aaee 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20  SELECT col FROM 
2aaef 74 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54  tbl;.**   SELECT
2aaf0 20 28 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f   (SELECT col FRO
2aaf1 4d 20 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c  M tbl);.**   SEL
2aaf2 45 43 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45  ECT abc FROM (SE
2aaf3 4c 45 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20  LECT col AS abc 
2aaf4 46 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a  FROM tbl);.** .*
2aaf5 2a 20 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f  * The declaratio
2aaf6 6e 20 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65  n type for any e
2aaf7 78 70 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20  xpression other 
2aaf8 74 68 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73  than a column is
2aaf9 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63   NULL..*/.static
2aafa 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c   const char *col
2aafb 75 6d 6e 54 79 70 65 28 0a 20 20 4e 61 6d 65 43  umnType(.  NameC
2aafc 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20  ontext *pNC, .  
2aafd 45 78 70 72 20 2a 70 45 78 70 72 2c 0a 20 20 63  Expr *pExpr,.  c
2aafe 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72  onst char **pzOr
2aaff 69 67 69 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20  iginDb,.  const 
2ab00 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54  char **pzOriginT
2ab01 61 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ab,.  const char
2ab02 20 2a 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29   **pzOriginCol.)
2ab03 7b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  {.  char const *
2ab04 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61  zType = 0;.  cha
2ab05 72 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e  r const *zOrigin
2ab06 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63  Db = 0;.  char c
2ab07 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62  onst *zOriginTab
2ab08 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 0;.  char con
2ab09 73 74 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d  st *zOriginCol =
2ab0a 20 30 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69   0;.  int j;.  i
2ab0b 66 28 20 4e 45 56 45 52 28 70 45 78 70 72 3d 3d  f( NEVER(pExpr==
2ab0c 30 29 20 7c 7c 20 70 4e 43 2d 3e 70 53 72 63 4c  0) || pNC->pSrcL
2ab0d 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ist==0 ) return 
2ab0e 30 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45  0;..  switch( pE
2ab0f 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63  xpr->op ){.    c
2ab10 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  ase TK_AGG_COLUM
2ab11 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  N:.    case TK_C
2ab12 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 2f  OLUMN: {.      /
2ab13 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
2ab14 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 4c 6f   is a column. Lo
2ab15 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 74  cate the table t
2ab16 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 62 65 69  he column is bei
2ab17 6e 67 0a 20 20 20 20 20 20 2a 2a 20 65 78 74 72  ng.      ** extr
2ab18 61 63 74 65 64 20 66 72 6f 6d 20 69 6e 20 4e 61  acted from in Na
2ab19 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72 63 4c 69  meContext.pSrcLi
2ab1a 73 74 2e 20 54 68 69 73 20 74 61 62 6c 65 20 6d  st. This table m
2ab1b 61 79 20 62 65 20 72 65 61 6c 0a 20 20 20 20 20  ay be real.     
2ab1c 20 2a 2a 20 64 61 74 61 62 61 73 65 20 74 61 62   ** database tab
2ab1d 6c 65 20 6f 72 20 61 20 73 75 62 71 75 65 72 79  le or a subquery
2ab1e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2ab1f 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30   Table *pTab = 0
2ab20 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2ab21 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
2ab22 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72 61 63  column is extrac
2ab23 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20  ted from */.    
2ab24 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d 20 30    Select *pS = 0
2ab25 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2ab26 20 53 65 6c 65 63 74 20 74 68 65 20 63 6f 6c 75   Select the colu
2ab27 6d 6e 20 69 73 20 65 78 74 72 61 63 74 65 64 20  mn is extracted 
2ab28 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e  from */.      in
2ab29 74 20 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e  t iCol = pExpr->
2ab2a 69 43 6f 6c 75 6d 6e 3b 20 20 2f 2a 20 49 6e 64  iColumn;  /* Ind
2ab2b 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20  ex of column in 
2ab2c 70 54 61 62 20 2a 2f 0a 20 20 20 20 20 20 74 65  pTab */.      te
2ab2d 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
2ab2e 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
2ab2f 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2ab30 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
2ab31 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  K_COLUMN );.    
2ab32 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20    while( pNC && 
2ab33 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20  !pTab ){.       
2ab34 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
2ab35 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69  st = pNC->pSrcLi
2ab36 73 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  st;.        for(
2ab37 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69 73 74 2d 3e  j=0;j<pTabList->
2ab38 6e 53 72 63 20 26 26 20 70 54 61 62 4c 69 73 74  nSrc && pTabList
2ab39 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 21 3d  ->a[j].iCursor!=
2ab3a 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 6a 2b  pExpr->iTable;j+
2ab3b 2b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +);.        if( 
2ab3c 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  j<pTabList->nSrc
2ab3d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54   ){.          pT
2ab3e 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  ab = pTabList->a
2ab3f 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20  [j].pTab;.      
2ab40 20 20 20 20 70 53 20 3d 20 70 54 61 62 4c 69 73      pS = pTabLis
2ab41 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63 74 3b  t->a[j].pSelect;
2ab42 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
2ab43 20 20 20 20 20 20 20 20 20 20 70 4e 43 20 3d 20            pNC = 
2ab44 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pNC->pNext;.    
2ab45 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20      }.      }.. 
2ab46 20 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30       if( pTab==0
2ab47 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41   ){.        /* A
2ab48 74 20 6f 6e 65 20 74 69 6d 65 2c 20 63 6f 64 65  t one time, code
2ab49 20 73 75 63 68 20 61 73 20 22 53 45 4c 45 43 54   such as "SELECT
2ab4a 20 6e 65 77 2e 78 22 20 77 69 74 68 69 6e 20 61   new.x" within a
2ab4b 20 74 72 69 67 67 65 72 20 77 6f 75 6c 64 0a 20   trigger would. 
2ab4c 20 20 20 20 20 20 20 2a 2a 20 63 61 75 73 65 20         ** cause 
2ab4d 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 74  this condition t
2ab4e 6f 20 72 75 6e 2e 20 20 53 69 6e 63 65 20 74 68  o run.  Since th
2ab4f 65 6e 2c 20 77 65 20 68 61 76 65 20 72 65 73 74  en, we have rest
2ab50 72 75 63 74 75 72 65 64 20 68 6f 77 0a 20 20 20  ructured how.   
2ab51 20 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20       ** trigger 
2ab52 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74 65  code is generate
2ab53 64 20 61 6e 64 20 73 6f 20 74 68 69 73 20 63 6f  d and so this co
2ab54 6e 64 69 74 69 6f 6e 20 69 73 20 6e 6f 20 6c 6f  ndition is no lo
2ab55 6e 67 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a  nger .        **
2ab56 20 70 6f 73 73 69 62 6c 65 2e 20 48 6f 77 65 76   possible. Howev
2ab57 65 72 2c 20 69 74 20 63 61 6e 20 73 74 69 6c 6c  er, it can still
2ab58 20 62 65 20 74 72 75 65 20 66 6f 72 20 73 74 61   be true for sta
2ab59 74 65 6d 65 6e 74 73 20 6c 69 6b 65 0a 20 20 20  tements like.   
2ab5a 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c       ** the foll
2ab5b 6f 77 69 6e 67 3a 0a 20 20 20 20 20 20 20 20 2a  owing:.        *
2ab5c 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 43  *.        **   C
2ab5d 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63  REATE TABLE t1(c
2ab5e 6f 6c 20 49 4e 54 45 47 45 52 29 3b 0a 20 20 20  ol INTEGER);.   
2ab5f 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54       **   SELECT
2ab60 20 28 53 45 4c 45 43 54 20 74 31 2e 63 6f 6c 29   (SELECT t1.col)
2ab61 20 46 52 4f 4d 20 46 52 4f 4d 20 74 31 3b 0a 20   FROM FROM t1;. 
2ab62 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
2ab63 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6c 75 6d 6e    ** when column
2ab64 54 79 70 65 28 29 20 69 73 20 63 61 6c 6c 65 64  Type() is called
2ab65 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   on the expressi
2ab66 6f 6e 20 22 74 31 2e 63 6f 6c 22 20 69 6e 20 74  on "t1.col" in t
2ab67 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  he .        ** s
2ab68 75 62 2d 73 65 6c 65 63 74 2e 20 49 6e 20 74 68  ub-select. In th
2ab69 69 73 20 63 61 73 65 2c 20 73 65 74 20 74 68 65  is case, set the
2ab6a 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 74 6f 20   column type to 
2ab6b 4e 55 4c 4c 2c 20 65 76 65 6e 0a 20 20 20 20 20  NULL, even.     
2ab6c 20 20 20 2a 2a 20 74 68 6f 75 67 68 20 69 74 20     ** though it 
2ab6d 73 68 6f 75 6c 64 20 72 65 61 6c 6c 79 20 62 65  should really be
2ab6e 20 22 49 4e 54 45 47 45 52 22 2e 0a 20 20 20 20   "INTEGER"..    
2ab6f 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
2ab70 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20  * This is not a 
2ab71 70 72 6f 62 6c 65 6d 2c 20 61 73 20 74 68 65 20  problem, as the 
2ab72 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6f 66 20 22  column type of "
2ab73 74 31 2e 63 6f 6c 22 20 69 73 20 6e 65 76 65 72  t1.col" is never
2ab74 0a 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 64  .        ** used
2ab75 2e 20 57 68 65 6e 20 63 6f 6c 75 6d 6e 54 79 70  . When columnTyp
2ab76 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  e() is called on
2ab77 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2ab78 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 28 53 45  .        ** "(SE
2ab79 4c 45 43 54 20 74 31 2e 63 6f 6c 29 22 2c 20 74  LECT t1.col)", t
2ab7a 68 65 20 63 6f 72 72 65 63 74 20 74 79 70 65 20  he correct type 
2ab7b 69 73 20 72 65 74 75 72 6e 65 64 20 28 73 65 65  is returned (see
2ab7c 20 74 68 65 20 54 4b 5f 53 45 4c 45 43 54 0a 20   the TK_SELECT. 
2ab7d 20 20 20 20 20 20 20 2a 2a 20 62 72 61 6e 63 68         ** branch
2ab7e 20 62 65 6c 6f 77 2e 20 20 2a 2f 0a 20 20 20 20   below.  */.    
2ab7f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ab80 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74   }..      assert
2ab81 28 20 70 54 61 62 20 26 26 20 70 45 78 70 72 2d  ( pTab && pExpr-
2ab82 3e 70 54 61 62 3d 3d 70 54 61 62 20 29 3b 0a 20  >pTab==pTab );. 
2ab83 20 20 20 20 20 69 66 28 20 70 53 20 29 7b 0a 20       if( pS ){. 
2ab84 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 74         /* The "t
2ab85 61 62 6c 65 22 20 69 73 20 61 63 74 75 61 6c 6c  able" is actuall
2ab86 79 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f  y a sub-select o
2ab87 72 20 61 20 76 69 65 77 20 69 6e 20 74 68 65 20  r a view in the 
2ab88 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 20 20 20  FROM clause.    
2ab89 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 53 45      ** of the SE
2ab8a 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  LECT statement. 
2ab8b 52 65 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61  Return the decla
2ab8c 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 20  ration type and 
2ab8d 6f 72 69 67 69 6e 0a 20 20 20 20 20 20 20 20 2a  origin.        *
2ab8e 2a 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72  * data for the r
2ab8f 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e  esult-set column
2ab90 20 6f 66 20 74 68 65 20 73 75 62 2d 73 65 6c 65   of the sub-sele
2ab91 63 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ct..        */. 
2ab92 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59         if( ALWAY
2ab93 53 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f  S(iCol>=0 && iCo
2ab94 6c 3c 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  l<pS->pEList->nE
2ab95 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  xpr) ){.        
2ab96 20 20 2f 2a 20 49 66 20 69 43 6f 6c 20 69 73 20    /* If iCol is 
2ab97 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20  less than zero, 
2ab98 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73  then the express
2ab99 69 6f 6e 20 72 65 71 75 65 73 74 73 20 74 68 65  ion requests the
2ab9a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 6f  .          ** ro
2ab9b 77 69 64 20 6f 66 20 74 68 65 20 73 75 62 2d 73  wid of the sub-s
2ab9c 65 6c 65 63 74 20 6f 72 20 76 69 65 77 2e 20 54  elect or view. T
2ab9d 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 69  his expression i
2ab9e 73 20 6c 65 67 61 6c 20 28 73 65 65 20 0a 20 20  s legal (see .  
2ab9f 20 20 20 20 20 20 20 20 2a 2a 20 74 65 73 74 20          ** test 
2aba0 63 61 73 65 20 6d 69 73 63 32 2e 32 2e 32 29 20  case misc2.2.2) 
2aba1 2d 20 69 74 20 61 6c 77 61 79 73 20 65 76 61 6c  - it always eval
2aba2 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 2e 0a 20  uates to NULL.. 
2aba3 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
2aba4 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78        NameContex
2aba5 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 20 20 20  t sNC;.         
2aba6 20 45 78 70 72 20 2a 70 20 3d 20 70 53 2d 3e 70   Expr *p = pS->p
2aba7 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70  EList->a[iCol].p
2aba8 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Expr;.          
2aba9 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
2abaa 53 2d 3e 70 53 72 63 3b 0a 20 20 20 20 20 20 20  S->pSrc;.       
2abab 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70     sNC.pNext = p
2abac 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e  NC;.          sN
2abad 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e  C.pParse = pNC->
2abae 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20  pParse;.        
2abaf 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e    zType = column
2abb0 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a  Type(&sNC, p, &z
2abb1 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67  OriginDb, &zOrig
2abb2 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43  inTab, &zOriginC
2abb3 6f 6c 29 3b 20 0a 20 20 20 20 20 20 20 20 7d 0a  ol); .        }.
2abb4 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2abb5 41 4c 57 41 59 53 28 70 54 61 62 2d 3e 70 53 63  ALWAYS(pTab->pSc
2abb6 68 65 6d 61 29 20 29 7b 0a 20 20 20 20 20 20 20  hema) ){.       
2abb7 20 2f 2a 20 41 20 72 65 61 6c 20 74 61 62 6c 65   /* A real table
2abb8 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
2abb9 72 74 28 20 21 70 53 20 29 3b 0a 20 20 20 20 20  rt( !pS );.     
2abba 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20     if( iCol<0 ) 
2abbb 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b  iCol = pTab->iPK
2abbc 65 79 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ey;.        asse
2abbd 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c 20  rt( iCol==-1 || 
2abbe 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c  (iCol>=0 && iCol
2abbf 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b 0a  <pTab->nCol) );.
2abc0 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
2abc1 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
2abc2 7a 54 79 70 65 20 3d 20 22 49 4e 54 45 47 45 52  zType = "INTEGER
2abc3 22 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72  ";.          zOr
2abc4 69 67 69 6e 43 6f 6c 20 3d 20 22 72 6f 77 69 64  iginCol = "rowid
2abc5 22 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ";.        }else
2abc6 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70  {.          zTyp
2abc7 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  e = pTab->aCol[i
2abc8 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20  Col].zType;.    
2abc9 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f 6c        zOriginCol
2abca 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
2abcb 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ol].zName;.     
2abcc 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 4f 72     }.        zOr
2abcd 69 67 69 6e 54 61 62 20 3d 20 70 54 61 62 2d 3e  iginTab = pTab->
2abce 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69  zName;.        i
2abcf 66 28 20 70 4e 43 2d 3e 70 50 61 72 73 65 20 29  f( pNC->pParse )
2abd0 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
2abd1 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
2abd2 65 6d 61 54 6f 49 6e 64 65 78 28 70 4e 43 2d 3e  emaToIndex(pNC->
2abd3 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
2abd4 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ->pSchema);.    
2abd5 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 44 62 20        zOriginDb 
2abd6 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64  = pNC->pParse->d
2abd7 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2abd8 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
2abd9 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
2abda 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
2abdb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2abdc 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b  UERY.    case TK
2abdd 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20  _SELECT: {.     
2abde 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
2abdf 6f 6e 20 69 73 20 61 20 73 75 62 2d 73 65 6c 65  on is a sub-sele
2abe0 63 74 2e 20 52 65 74 75 72 6e 20 74 68 65 20 64  ct. Return the d
2abe1 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
2abe2 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 6f 72 69  and.      ** ori
2abe3 67 69 6e 20 69 6e 66 6f 20 66 6f 72 20 74 68 65  gin info for the
2abe4 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 69   single column i
2abe5 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2abe6 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a 20   of the SELECT. 
2abe7 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e       ** statemen
2abe8 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t..      */.    
2abe9 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
2abea 43 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20  C;.      Select 
2abeb 2a 70 53 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  *pS = pExpr->x.p
2abec 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 45 78  Select;.      Ex
2abed 70 72 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69  pr *p = pS->pELi
2abee 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
2abef 20 20 20 20 20 20 61 73 73 65 72 74 28 20 45 78        assert( Ex
2abf0 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2abf1 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2abf2 74 29 20 29 3b 0a 20 20 20 20 20 20 73 4e 43 2e  t) );.      sNC.
2abf3 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70  pSrcList = pS->p
2abf4 53 72 63 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70  Src;.      sNC.p
2abf5 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20 20 20 20  Next = pNC;.    
2abf6 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
2abf7 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20  NC->pParse;.    
2abf8 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e    zType = column
2abf9 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a  Type(&sNC, p, &z
2abfa 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67  OriginDb, &zOrig
2abfb 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43  inTab, &zOriginC
2abfc 6f 6c 29 3b 20 0a 20 20 20 20 20 20 62 72 65 61  ol); .      brea
2abfd 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
2abfe 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 70 7a 4f    }.  .  if( pzO
2abff 72 69 67 69 6e 44 62 20 29 7b 0a 20 20 20 20 61  riginDb ){.    a
2ac00 73 73 65 72 74 28 20 70 7a 4f 72 69 67 69 6e 54  ssert( pzOriginT
2ac01 61 62 20 26 26 20 70 7a 4f 72 69 67 69 6e 43 6f  ab && pzOriginCo
2ac02 6c 20 29 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67  l );.    *pzOrig
2ac03 69 6e 44 62 20 3d 20 7a 4f 72 69 67 69 6e 44 62  inDb = zOriginDb
2ac04 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 54  ;.    *pzOriginT
2ac05 61 62 20 3d 20 7a 4f 72 69 67 69 6e 54 61 62 3b  ab = zOriginTab;
2ac06 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 43 6f  .    *pzOriginCo
2ac07 6c 20 3d 20 7a 4f 72 69 67 69 6e 43 6f 6c 3b 0a  l = zOriginCol;.
2ac08 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 54 79    }.  return zTy
2ac09 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  pe;.}../*.** Gen
2ac0a 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
2ac0b 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44  will tell the VD
2ac0c 42 45 20 74 68 65 20 64 65 63 6c 61 72 61 74 69  BE the declarati
2ac0d 6f 6e 20 74 79 70 65 73 20 6f 66 20 63 6f 6c 75  on types of colu
2ac0e 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65  mns.** in the re
2ac0f 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74 61  sult set..*/.sta
2ac10 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74  tic void generat
2ac11 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 0a 20 20  eColumnTypes(.  
2ac12 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2ac13 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
2ac14 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
2ac15 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f  st *pTabList,  /
2ac16 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  * List of tables
2ac17 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2ac18 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70  pEList    /* Exp
2ac19 72 65 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e  ressions definin
2ac1a 67 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  g the result set
2ac1b 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53   */.){.#ifndef S
2ac1c 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54  QLITE_OMIT_DECLT
2ac1d 59 50 45 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  YPE.  Vdbe *v = 
2ac1e 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2ac1f 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43 6f   int i;.  NameCo
2ac20 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 73 4e 43  ntext sNC;.  sNC
2ac21 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62  .pSrcList = pTab
2ac22 4c 69 73 74 3b 0a 20 20 73 4e 43 2e 70 50 61 72  List;.  sNC.pPar
2ac23 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 66  se = pParse;.  f
2ac24 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74  or(i=0; i<pEList
2ac25 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
2ac26 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70 45 4c     Expr *p = pEL
2ac27 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ist->a[i].pExpr;
2ac28 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2ac29 2a 7a 54 79 70 65 3b 0a 23 69 66 64 65 66 20 53  *zType;.#ifdef S
2ac2a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
2ac2b 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 20 20 20  UMN_METADATA.   
2ac2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72   const char *zOr
2ac2d 69 67 44 62 20 3d 20 30 3b 0a 20 20 20 20 63 6f  igDb = 0;.    co
2ac2e 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 54  nst char *zOrigT
2ac2f 61 62 20 3d 20 30 3b 0a 20 20 20 20 63 6f 6e 73  ab = 0;.    cons
2ac30 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 43 6f 6c  t char *zOrigCol
2ac31 20 3d 20 30 3b 0a 20 20 20 20 7a 54 79 70 65 20   = 0;.    zType 
2ac32 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e  = columnType(&sN
2ac33 43 2c 20 70 2c 20 26 7a 4f 72 69 67 44 62 2c 20  C, p, &zOrigDb, 
2ac34 26 7a 4f 72 69 67 54 61 62 2c 20 26 7a 4f 72 69  &zOrigTab, &zOri
2ac35 67 43 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 54  gCol);..    /* T
2ac36 68 65 20 76 64 62 65 20 6d 75 73 74 20 6d 61 6b  he vdbe must mak
2ac37 65 20 69 74 73 20 6f 77 6e 20 63 6f 70 79 20 6f  e its own copy o
2ac38 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 74 79 70  f the column-typ
2ac39 65 20 61 6e 64 20 6f 74 68 65 72 20 0a 20 20 20  e and other .   
2ac3a 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69   ** column speci
2ac3b 66 69 63 20 73 74 72 69 6e 67 73 2c 20 69 6e 20  fic strings, in 
2ac3c 63 61 73 65 20 74 68 65 20 73 63 68 65 6d 61 20  case the schema 
2ac3d 69 73 20 72 65 73 65 74 20 62 65 66 6f 72 65 20  is reset before 
2ac3e 74 68 69 73 0a 20 20 20 20 2a 2a 20 76 69 72 74  this.    ** virt
2ac3f 75 61 6c 20 6d 61 63 68 69 6e 65 20 69 73 20 64  ual machine is d
2ac40 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  eleted..    */. 
2ac41 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2ac42 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
2ac43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 2c  OLNAME_DATABASE,
2ac44 20 7a 4f 72 69 67 44 62 2c 20 53 51 4c 49 54 45   zOrigDb, SQLITE
2ac45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
2ac46 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2ac47 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
2ac48 4e 41 4d 45 5f 54 41 42 4c 45 2c 20 7a 4f 72 69  NAME_TABLE, zOri
2ac49 67 54 61 62 2c 20 53 51 4c 49 54 45 5f 54 52 41  gTab, SQLITE_TRA
2ac4a 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c  NSIENT);.    sql
2ac4b 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2ac4c 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
2ac4d 5f 43 4f 4c 55 4d 4e 2c 20 7a 4f 72 69 67 43 6f  _COLUMN, zOrigCo
2ac4e 6c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  l, SQLITE_TRANSI
2ac4f 45 4e 54 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  ENT);.#else.    
2ac50 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79  zType = columnTy
2ac51 70 65 28 26 73 4e 43 2c 20 70 2c 20 30 2c 20 30  pe(&sNC, p, 0, 0
2ac52 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  , 0);.#endif.   
2ac53 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2ac54 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
2ac55 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 2c 20 7a  NAME_DECLTYPE, z
2ac56 54 79 70 65 2c 20 53 51 4c 49 54 45 5f 54 52 41  Type, SQLITE_TRA
2ac57 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 23 65 6e  NSIENT);.  }.#en
2ac58 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2ac59 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a 7d  IT_DECLTYPE */.}
2ac5a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2ac5b 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
2ac5c 74 65 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68  tell the VDBE th
2ac5d 65 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d  e names of colum
2ac5e 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73  ns.** in the res
2ac5f 75 6c 74 20 73 65 74 2e 20 20 54 68 69 73 20 69  ult set.  This i
2ac60 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73  nformation is us
2ac61 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68  ed to provide th
2ac62 65 0a 2a 2a 20 61 7a 43 6f 6c 5b 5d 20 76 61 6c  e.** azCol[] val
2ac63 75 65 73 20 69 6e 20 74 68 65 20 63 61 6c 6c 62  ues in the callb
2ac64 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ack..*/.static v
2ac65 6f 69 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75  oid generateColu
2ac66 6d 6e 4e 61 6d 65 73 28 0a 20 20 50 61 72 73 65  mnNames(.  Parse
2ac67 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f   *pParse,      /
2ac68 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
2ac69 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2ac6a 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73  TabList,  /* Lis
2ac6b 74 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20  t of tables */. 
2ac6c 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2ac6d 74 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69  t    /* Expressi
2ac6e 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65  ons defining the
2ac6f 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29   result set */.)
2ac70 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
2ac71 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2ac72 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 71 6c 69 74  nt i, j;.  sqlit
2ac73 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2ac74 3e 64 62 3b 0a 20 20 69 6e 74 20 66 75 6c 6c 4e  >db;.  int fullN
2ac75 61 6d 65 73 2c 20 73 68 6f 72 74 4e 61 6d 65 73  ames, shortNames
2ac76 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
2ac77 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
2ac78 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
2ac79 6e 20 45 58 50 4c 41 49 4e 2c 20 73 6b 69 70 20  n EXPLAIN, skip 
2ac7a 74 68 69 73 20 73 74 65 70 20 2a 2f 0a 20 20 69  this step */.  i
2ac7b 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  f( pParse->expla
2ac7c 69 6e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  in ){.    return
2ac7d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
2ac7e 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e  if( pParse->colN
2ac7f 61 6d 65 73 53 65 74 20 7c 7c 20 4e 45 56 45 52  amesSet || NEVER
2ac80 28 76 3d 3d 30 29 20 7c 7c 20 64 62 2d 3e 6d 61  (v==0) || db->ma
2ac81 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
2ac82 75 72 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63  urn;.  pParse->c
2ac83 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 31 3b 0a  olNamesSet = 1;.
2ac84 20 20 66 75 6c 6c 4e 61 6d 65 73 20 3d 20 28 64    fullNames = (d
2ac85 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2ac86 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21  E_FullColNames)!
2ac87 3d 30 3b 0a 20 20 73 68 6f 72 74 4e 61 6d 65 73  =0;.  shortNames
2ac88 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20   = (db->flags & 
2ac89 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
2ac8a 61 6d 65 73 29 21 3d 30 3b 0a 20 20 73 71 6c 69  ames)!=0;.  sqli
2ac8b 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
2ac8c 73 28 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78  s(v, pEList->nEx
2ac8d 70 72 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  pr);.  for(i=0; 
2ac8e 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  i<pEList->nExpr;
2ac8f 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20   i++){.    Expr 
2ac90 2a 70 3b 0a 20 20 20 20 70 20 3d 20 70 45 4c 69  *p;.    p = pELi
2ac91 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  st->a[i].pExpr;.
2ac92 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d      if( NEVER(p=
2ac93 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  =0) ) continue;.
2ac94 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e      if( pEList->
2ac95 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29 7b 0a 20 20  a[i].zName ){.  
2ac96 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
2ac97 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  = pEList->a[i].z
2ac98 4e 61 6d 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  Name;.      sqli
2ac99 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2ac9a 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, i, COLNAME_
2ac9b 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  NAME, zName, SQL
2ac9c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
2ac9d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
2ac9e 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
2ac9f 7c 7c 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47  || p->op==TK_AGG
2aca0 5f 43 4f 4c 55 4d 4e 29 20 26 26 20 70 54 61 62  _COLUMN) && pTab
2aca1 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 54 61  List ){.      Ta
2aca2 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20  ble *pTab;.     
2aca3 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 0a 20 20 20   char *zCol;.   
2aca4 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d     int iCol = p-
2aca5 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
2aca6 66 6f 72 28 6a 3d 30 3b 20 41 4c 57 41 59 53 28  for(j=0; ALWAYS(
2aca7 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  j<pTabList->nSrc
2aca8 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ); j++){.       
2aca9 20 69 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 61   if( pTabList->a
2acaa 5b 6a 5d 2e 69 43 75 72 73 6f 72 3d 3d 70 2d 3e  [j].iCursor==p->
2acab 69 54 61 62 6c 65 20 29 20 62 72 65 61 6b 3b 0a  iTable ) break;.
2acac 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
2acad 73 65 72 74 28 20 6a 3c 70 54 61 62 4c 69 73 74  sert( j<pTabList
2acae 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20  ->nSrc );.      
2acaf 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d  pTab = pTabList-
2acb0 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20  >a[j].pTab;.    
2acb1 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 69    if( iCol<0 ) i
2acb2 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65  Col = pTab->iPKe
2acb3 79 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  y;.      assert(
2acb4 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43   iCol==-1 || (iC
2acb5 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54  ol>=0 && iCol<pT
2acb6 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20  ab->nCol) );.   
2acb7 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
2acb8 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20  .        zCol = 
2acb9 22 72 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 7d  "rowid";.      }
2acba 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 43  else{.        zC
2acbb 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ol = pTab->aCol[
2acbc 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  iCol].zName;.   
2acbd 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
2acbe 73 68 6f 72 74 4e 61 6d 65 73 20 26 26 20 21 66  shortNames && !f
2acbf 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20  ullNames ){.    
2acc0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2acc1 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
2acc2 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20  COLNAME_NAME, . 
2acc3 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2acc4 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
2acc5 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61  EList->a[i].zSpa
2acc6 6e 29 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  n), SQLITE_DYNAM
2acc7 49 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  IC);.      }else
2acc8 20 69 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20 29   if( fullNames )
2acc9 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
2acca 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20  zName = 0;.     
2accb 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
2accc 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
2accd 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  s.%s", pTab->zNa
2acce 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20  me, zCol);.     
2accf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2acd0 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
2acd1 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61  OLNAME_NAME, zNa
2acd2 6d 65 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  me, SQLITE_DYNAM
2acd3 49 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  IC);.      }else
2acd4 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2acd5 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2acd6 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, i, COLNAME_NA
2acd7 4d 45 2c 20 7a 43 6f 6c 2c 20 53 51 4c 49 54 45  ME, zCol, SQLITE
2acd8 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
2acd9 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
2acda 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2acdb 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
2acdc 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2acdd 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2acde 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
2acdf 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61  EList->a[i].zSpa
2ace0 6e 29 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  n), SQLITE_DYNAM
2ace1 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  IC);.    }.  }. 
2ace2 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54   generateColumnT
2ace3 79 70 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ypes(pParse, pTa
2ace4 62 4c 69 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a  bList, pEList);.
2ace5 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2ace6 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
2ace7 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 4e 61 6d  SELECT./*.** Nam
2ace8 65 20 6f 66 20 74 68 65 20 63 6f 6e 6e 65 63 74  e of the connect
2ace9 69 6f 6e 20 6f 70 65 72 61 74 6f 72 2c 20 75 73  ion operator, us
2acea 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73  ed for error mes
2aceb 73 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  sages..*/.static
2acec 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 65 6c   const char *sel
2aced 65 63 74 4f 70 4e 61 6d 65 28 69 6e 74 20 69 64  ectOpName(int id
2acee 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  ){.  char *z;.  
2acef 73 77 69 74 63 68 28 20 69 64 20 29 7b 0a 20 20  switch( id ){.  
2acf0 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20 20    case TK_ALL:  
2acf1 20 20 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 20       z = "UNION 
2acf2 41 4c 4c 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20  ALL";   break;. 
2acf3 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 52     case TK_INTER
2acf4 53 45 43 54 3a 20 7a 20 3d 20 22 49 4e 54 45 52  SECT: z = "INTER
2acf5 53 45 43 54 22 3b 20 20 20 62 72 65 61 6b 3b 0a  SECT";   break;.
2acf6 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 43 45      case TK_EXCE
2acf7 50 54 3a 20 20 20 20 7a 20 3d 20 22 45 58 43 45  PT:    z = "EXCE
2acf8 50 54 22 3b 20 20 20 20 20 20 62 72 65 61 6b 3b  PT";      break;
2acf9 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20  .    default:   
2acfa 20 20 20 20 20 20 20 20 7a 20 3d 20 22 55 4e 49          z = "UNI
2acfb 4f 4e 22 3b 20 20 20 20 20 20 20 62 72 65 61 6b  ON";       break
2acfc 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
2acfd 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
2acfe 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
2acff 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a  ND_SELECT */../*
2ad00 0a 2a 2a 20 47 69 76 65 6e 20 61 20 61 6e 20 65  .** Given a an e
2ad01 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 28  xpression list (
2ad02 77 68 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20  which is really 
2ad03 74 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72  the list of expr
2ad04 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20  essions.** that 
2ad05 66 6f 72 6d 20 74 68 65 20 72 65 73 75 6c 74 20  form the result 
2ad06 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20  set of a SELECT 
2ad07 73 74 61 74 65 6d 65 6e 74 29 20 63 6f 6d 70 75  statement) compu
2ad08 74 65 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a  te appropriate.*
2ad09 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66  * column names f
2ad0a 6f 72 20 61 20 74 61 62 6c 65 20 74 68 61 74 20  or a table that 
2ad0b 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65  would hold the e
2ad0c 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a  xpression list..
2ad0d 2a 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 6c 75 6d 6e  **.** All column
2ad0e 20 6e 61 6d 65 73 20 77 69 6c 6c 20 62 65 20 75   names will be u
2ad0f 6e 69 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c  nique..**.** Onl
2ad10 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  y the column nam
2ad11 65 73 20 61 72 65 20 63 6f 6d 70 75 74 65 64 2e  es are computed.
2ad12 20 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 2c 20    Column.zType, 
2ad13 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 2c 0a 2a 2a  Column.zColl,.**
2ad14 20 61 6e 64 20 6f 74 68 65 72 20 66 69 65 6c 64   and other field
2ad15 73 20 6f 66 20 43 6f 6c 75 6d 6e 20 61 72 65 20  s of Column are 
2ad16 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  zeroed..**.** Re
2ad17 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
2ad18 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 61  n success.  If a
2ad19 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
2ad1a 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  on error occurs,
2ad1b 0a 2a 2a 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69  .** store NULL i
2ad1c 6e 20 2a 70 61 43 6f 6c 20 61 6e 64 20 30 20 69  n *paCol and 0 i
2ad1d 6e 20 2a 70 6e 43 6f 6c 20 61 6e 64 20 72 65 74  n *pnCol and ret
2ad1e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2ad1f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2ad20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f  selectColumnsFro
2ad21 6d 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72  mExprList(.  Par
2ad22 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2ad23 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2ad24 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
2ad25 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
2ad26 20 20 20 20 20 2f 2a 20 45 78 70 72 20 6c 69 73       /* Expr lis
2ad27 74 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20  t from which to 
2ad28 64 65 72 69 76 65 20 63 6f 6c 75 6d 6e 20 6e 61  derive column na
2ad29 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  mes */.  int *pn
2ad2a 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Col,            
2ad2b 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75   /* Write the nu
2ad2c 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2ad2d 68 65 72 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e  here */.  Column
2ad2e 20 2a 2a 70 61 43 6f 6c 20 20 20 20 20 20 20 20   **paCol        
2ad2f 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e    /* Write the n
2ad30 65 77 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 68  ew column list h
2ad31 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ere */.){.  sqli
2ad32 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2ad33 2d 3e 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62  ->db;   /* Datab
2ad34 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2ad35 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  /.  int i, j;   
2ad36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad37 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
2ad38 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20   */.  int cnt;  
2ad39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad3a 20 20 2f 2a 20 49 6e 64 65 78 20 61 64 64 65 64    /* Index added
2ad3b 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 6e 61 6d   to make the nam
2ad3c 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20 43 6f  e unique */.  Co
2ad3d 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20 2a 70 43 6f  lumn *aCol, *pCo
2ad3e 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72  l;        /* For
2ad3f 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 72 65   looping over re
2ad40 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a  sult columns */.
2ad41 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20    int nCol;     
2ad42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ad43 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
2ad44 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
2ad45 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a   set */.  Expr *
2ad46 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
2ad47 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
2ad48 69 6f 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  ion for a single
2ad49 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 2a   result column *
2ad4a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  /.  char *zName;
2ad4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad4c 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  /* Column name *
2ad4d 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20  /.  int nName;  
2ad4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad4f 2f 2a 20 53 69 7a 65 20 6f 66 20 6e 61 6d 65 20  /* Size of name 
2ad50 69 6e 20 7a 4e 61 6d 65 5b 5d 20 2a 2f 0a 0a 20  in zName[] */.. 
2ad51 20 2a 70 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 20 3d   *pnCol = nCol =
2ad52 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a   pEList->nExpr;.
2ad53 20 20 61 43 6f 6c 20 3d 20 2a 70 61 43 6f 6c 20    aCol = *paCol 
2ad54 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2ad55 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2ad56 28 61 43 6f 6c 5b 30 5d 29 2a 6e 43 6f 6c 29 3b  (aCol[0])*nCol);
2ad57 0a 20 20 69 66 28 20 61 43 6f 6c 3d 3d 30 20 29  .  if( aCol==0 )
2ad58 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2ad59 4f 4d 45 4d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  OMEM;.  for(i=0,
2ad5a 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43   pCol=aCol; i<nC
2ad5b 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; i++, pCol++)
2ad5c 7b 0a 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20  {.    /* Get an 
2ad5d 61 70 70 72 6f 70 72 69 61 74 65 20 6e 61 6d 65  appropriate name
2ad5e 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a   for the column.
2ad5f 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70      */.    p = p
2ad60 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  EList->a[i].pExp
2ad61 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  r;.    assert( p
2ad62 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 45  ->pRight==0 || E
2ad63 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2ad64 2d 3e 70 52 69 67 68 74 2c 20 45 50 5f 49 6e 74  ->pRight, EP_Int
2ad65 56 61 6c 75 65 29 0a 20 20 20 20 20 20 20 20 20  Value).         
2ad66 20 20 20 20 20 20 7c 7c 20 70 2d 3e 70 52 69 67        || p->pRig
2ad67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20  ht->u.zToken==0 
2ad68 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 75 2e  || p->pRight->u.
2ad69 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0a  zToken[0]!=0 );.
2ad6a 20 20 20 20 69 66 28 20 28 7a 4e 61 6d 65 20 3d      if( (zName =
2ad6b 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e   pEList->a[i].zN
2ad6c 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ame)!=0 ){.     
2ad6d 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d   /* If the colum
2ad6e 6e 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 22 41  n contains an "A
2ad6f 53 20 3c 6e 61 6d 65 3e 22 20 70 68 72 61 73 65  S <name>" phrase
2ad70 2c 20 75 73 65 20 3c 6e 61 6d 65 3e 20 61 73 20  , use <name> as 
2ad71 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  the name */.    
2ad72 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
2ad73 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e  3DbStrDup(db, zN
2ad74 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ame);.    }else{
2ad75 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 43 6f  .      Expr *pCo
2ad76 6c 45 78 70 72 20 3d 20 70 3b 20 20 2f 2a 20 54  lExpr = p;  /* T
2ad77 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  he expression th
2ad78 61 74 20 69 73 20 74 68 65 20 72 65 73 75 6c 74  at is the result
2ad79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a   column name */.
2ad7a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
2ad7b 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  b;         /* Ta
2ad7c 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ble associated w
2ad7d 69 74 68 20 74 68 69 73 20 65 78 70 72 65 73 73  ith this express
2ad7e 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  ion */.      whi
2ad7f 6c 65 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70  le( pColExpr->op
2ad80 3d 3d 54 4b 5f 44 4f 54 20 29 20 70 43 6f 6c 45  ==TK_DOT ) pColE
2ad81 78 70 72 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e  xpr = pColExpr->
2ad82 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66  pRight;.      if
2ad83 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d  ( pColExpr->op==
2ad84 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 41 4c 57  TK_COLUMN && ALW
2ad85 41 59 53 28 70 43 6f 6c 45 78 70 72 2d 3e 70 54  AYS(pColExpr->pT
2ad86 61 62 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20  ab!=0) ){.      
2ad87 20 20 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d 6e 73    /* For columns
2ad88 20 75 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20   use the column 
2ad89 6e 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  name name */.   
2ad8a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
2ad8b 70 43 6f 6c 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  pColExpr->iColum
2ad8c 6e 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  n;.        pTab 
2ad8d 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62  = pColExpr->pTab
2ad8e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43  ;.        if( iC
2ad8f 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54  ol<0 ) iCol = pT
2ad90 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20  ab->iPKey;.     
2ad91 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
2ad92 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
2ad93 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  s",.            
2ad94 20 20 20 20 20 69 43 6f 6c 3e 3d 30 20 3f 20 70       iCol>=0 ? p
2ad95 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
2ad96 7a 4e 61 6d 65 20 3a 20 22 72 6f 77 69 64 22 29  zName : "rowid")
2ad97 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
2ad98 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d  ( pColExpr->op==
2ad99 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 20  TK_ID ){.       
2ad9a 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2ad9b 73 50 72 6f 70 65 72 74 79 28 70 43 6f 6c 45 78  sProperty(pColEx
2ad9c 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
2ad9d 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d   );.        zNam
2ad9e 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
2ad9f 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70 43 6f  tf(db, "%s", pCo
2ada0 6c 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29  lExpr->u.zToken)
2ada1 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2ada2 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68         /* Use th
2ada3 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 78 74 20  e original text 
2ada4 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 65 78  of the column ex
2ada5 70 72 65 73 73 69 6f 6e 20 61 73 20 69 74 73 20  pression as its 
2ada6 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  name */.        
2ada7 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
2ada8 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c  Printf(db, "%s",
2ada9 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53   pEList->a[i].zS
2adaa 70 61 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pan);.      }.  
2adab 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
2adac 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2adad 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2adae 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  ree(db, zName);.
2adaf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2adb0 20 7d 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20   }..    /* Make 
2adb1 73 75 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  sure the column 
2adb2 6e 61 6d 65 20 69 73 20 75 6e 69 71 75 65 2e 20  name is unique. 
2adb3 20 49 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20   If the name is 
2adb4 6e 6f 74 20 75 6e 69 71 75 65 2c 0a 20 20 20 20  not unique,.    
2adb5 2a 2a 20 61 70 70 65 6e 64 20 61 20 69 6e 74 65  ** append a inte
2adb6 67 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20  ger to the name 
2adb7 73 6f 20 74 68 61 74 20 69 74 20 62 65 63 6f 6d  so that it becom
2adb8 65 73 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a  es unique..    *
2adb9 2f 0a 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71  /.    nName = sq
2adba 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
2adbb 61 6d 65 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  ame);.    for(j=
2adbc 63 6e 74 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29  cnt=0; j<i; j++)
2adbd 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
2adbe 74 65 33 53 74 72 49 43 6d 70 28 61 43 6f 6c 5b  te3StrICmp(aCol[
2adbf 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29  j].zName, zName)
2adc0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ==0 ){.        c
2adc1 68 61 72 20 2a 7a 4e 65 77 4e 61 6d 65 3b 0a 20  har *zNewName;. 
2adc2 20 20 20 20 20 20 20 7a 4e 61 6d 65 5b 6e 4e 61         zName[nNa
2adc3 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  me] = 0;.       
2adc4 20 7a 4e 65 77 4e 61 6d 65 20 3d 20 73 71 6c 69   zNewName = sqli
2adc5 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2adc6 25 73 3a 25 64 22 2c 20 7a 4e 61 6d 65 2c 20 2b  %s:%d", zName, +
2adc7 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73  +cnt);.        s
2adc8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2adc9 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
2adca 20 7a 4e 61 6d 65 20 3d 20 7a 4e 65 77 4e 61 6d   zName = zNewNam
2adcb 65 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 2d  e;.        j = -
2adcc 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  1;.        if( z
2adcd 4e 61 6d 65 3d 3d 30 20 29 20 62 72 65 61 6b 3b  Name==0 ) break;
2adce 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2adcf 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d     pCol->zName =
2add0 20 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 69 66   zName;.  }.  if
2add1 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2add2 65 64 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d  ed ){.    for(j=
2add3 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20  0; j<i; j++){.  
2add4 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2add5 65 28 64 62 2c 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e  e(db, aCol[j].zN
2add6 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ame);.    }.    
2add7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2add8 2c 20 61 43 6f 6c 29 3b 0a 20 20 20 20 2a 70 61  , aCol);.    *pa
2add9 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 2a 70 6e  Col = 0;.    *pn
2adda 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Col = 0;.    ret
2addb 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2addc 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
2addd 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
2adde 2a 2a 20 41 64 64 20 74 79 70 65 20 61 6e 64 20  ** Add type and 
2addf 63 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d  collation inform
2ade0 61 74 69 6f 6e 20 74 6f 20 61 20 63 6f 6c 75 6d  ation to a colum
2ade1 6e 20 6c 69 73 74 20 62 61 73 65 64 20 6f 6e 0a  n list based on.
2ade2 2a 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  ** a SELECT stat
2ade3 65 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  ement..** .** Th
2ade4 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 70 72  e column list pr
2ade5 65 73 75 6d 61 62 6c 79 20 63 61 6d 65 20 66 72  esumably came fr
2ade6 6f 6d 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 4e  om selectColumnN
2ade7 61 6d 65 73 46 72 6f 6d 45 78 70 72 4c 69 73 74  amesFromExprList
2ade8 28 29 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d  ()..** The colum
2ade9 6e 20 6c 69 73 74 20 68 61 73 20 6f 6e 6c 79 20  n list has only 
2adea 6e 61 6d 65 73 2c 20 6e 6f 74 20 74 79 70 65 73  names, not types
2adeb 20 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 20   or collations. 
2adec 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   This.** routine
2aded 20 67 6f 65 73 20 74 68 72 6f 75 67 68 20 61 6e   goes through an
2adee 64 20 61 64 64 73 20 74 68 65 20 74 79 70 65 73  d adds the types
2adef 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e   and collations.
2adf0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2adf1 69 6e 65 20 72 65 71 75 69 72 65 73 20 74 68 61  ine requires tha
2adf2 74 20 61 6c 6c 20 69 64 65 6e 74 69 66 69 65 72  t all identifier
2adf3 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a  s in the SELECT.
2adf4 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 62 65 20  ** statement be 
2adf5 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61  resolved..*/.sta
2adf6 74 69 63 20 76 6f 69 64 20 73 65 6c 65 63 74 41  tic void selectA
2adf7 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43  ddColumnTypeAndC
2adf8 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  ollation(.  Pars
2adf9 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2adfa 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2adfb 74 65 78 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  texts */.  int n
2adfc 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Col,            
2adfd 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
2adfe 6c 75 6d 6e 73 20 2a 2f 0a 20 20 43 6f 6c 75 6d  lumns */.  Colum
2adff 6e 20 2a 61 43 6f 6c 2c 20 20 20 20 20 20 20 20  n *aCol,        
2ae00 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75   /* List of colu
2ae01 6d 6e 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  mns */.  Select 
2ae02 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f  *pSelect       /
2ae03 2a 20 53 45 4c 45 43 54 20 75 73 65 64 20 74 6f  * SELECT used to
2ae04 20 64 65 74 65 72 6d 69 6e 65 20 74 79 70 65 73   determine types
2ae05 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20   and collations 
2ae06 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
2ae07 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2ae08 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
2ae09 73 4e 43 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70  sNC;.  Column *p
2ae0a 43 6f 6c 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  Col;.  CollSeq *
2ae0b 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a  pColl;.  int i;.
2ae0c 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 73 74 72    Expr *p;.  str
2ae0d 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
2ae0e 6d 20 2a 61 3b 0a 0a 20 20 61 73 73 65 72 74 28  m *a;..  assert(
2ae0f 20 70 53 65 6c 65 63 74 21 3d 30 20 29 3b 0a 20   pSelect!=0 );. 
2ae10 20 61 73 73 65 72 74 28 20 28 70 53 65 6c 65 63   assert( (pSelec
2ae11 74 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  t->selFlags & SF
2ae12 5f 52 65 73 6f 6c 76 65 64 29 21 3d 30 20 29 3b  _Resolved)!=0 );
2ae13 0a 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 3d  .  assert( nCol=
2ae14 3d 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74  =pSelect->pEList
2ae15 2d 3e 6e 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d  ->nExpr || db->m
2ae16 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2ae17 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2ae18 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
2ae19 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
2ae1a 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
2ae1b 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
2ae1c 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a   pSelect->pSrc;.
2ae1d 20 20 61 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70    a = pSelect->p
2ae1e 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 66 6f 72 28  EList->a;.  for(
2ae1f 69 3d 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20  i=0, pCol=aCol; 
2ae20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  i<nCol; i++, pCo
2ae21 6c 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61 5b  l++){.    p = a[
2ae22 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 43  i].pExpr;.    pC
2ae23 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69  ol->zType = sqli
2ae24 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2ae25 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c  columnType(&sNC,
2ae26 20 70 2c 20 30 2c 20 30 2c 20 30 29 29 3b 0a 20   p, 0, 0, 0));. 
2ae27 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74     pCol->affinit
2ae28 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  y = sqlite3ExprA
2ae29 66 66 69 6e 69 74 79 28 70 29 3b 0a 20 20 20 20  ffinity(p);.    
2ae2a 69 66 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  if( pCol->affini
2ae2b 74 79 3d 3d 30 20 29 20 70 43 6f 6c 2d 3e 61 66  ty==0 ) pCol->af
2ae2c 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
2ae2d 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 70 43  AFF_NONE;.    pC
2ae2e 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
2ae2f 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
2ae30 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f   p);.    if( pCo
2ae31 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c  ll ){.      pCol
2ae32 2d 3e 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65  ->zColl = sqlite
2ae33 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 43  3DbStrDup(db, pC
2ae34 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  oll->zName);.   
2ae35 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2ae36 47 69 76 65 6e 20 61 20 53 45 4c 45 43 54 20 73  Given a SELECT s
2ae37 74 61 74 65 6d 65 6e 74 2c 20 67 65 6e 65 72 61  tatement, genera
2ae38 74 65 20 61 20 54 61 62 6c 65 20 73 74 72 75 63  te a Table struc
2ae39 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
2ae3a 62 65 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  bes.** the resul
2ae3b 74 20 73 65 74 20 6f 66 20 74 68 61 74 20 53 45  t set of that SE
2ae3c 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  LECT..*/.SQLITE_
2ae3d 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
2ae3e 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f  qlite3ResultSetO
2ae3f 66 53 65 6c 65 63 74 28 50 61 72 73 65 20 2a 70  fSelect(Parse *p
2ae40 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70  Parse, Select *p
2ae41 53 65 6c 65 63 74 29 7b 0a 20 20 54 61 62 6c 65  Select){.  Table
2ae42 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65   *pTab;.  sqlite
2ae43 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2ae44 64 62 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 46  db;.  int savedF
2ae45 6c 61 67 73 3b 0a 0a 20 20 73 61 76 65 64 46 6c  lags;..  savedFl
2ae46 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b  ags = db->flags;
2ae47 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20  .  db->flags &= 
2ae48 7e 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e  ~SQLITE_FullColN
2ae49 61 6d 65 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67  ames;.  db->flag
2ae4a 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72  s |= SQLITE_Shor
2ae4b 74 43 6f 6c 4e 61 6d 65 73 3b 0a 20 20 73 71 6c  tColNames;.  sql
2ae4c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 70  ite3SelectPrep(p
2ae4d 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
2ae4e 30 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  0);.  if( pParse
2ae4f 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 20  ->nErr ) return 
2ae50 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c  0;.  while( pSel
2ae51 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 53  ect->pPrior ) pS
2ae52 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d  elect = pSelect-
2ae53 3e 70 50 72 69 6f 72 3b 0a 20 20 64 62 2d 3e 66  >pPrior;.  db->f
2ae54 6c 61 67 73 20 3d 20 73 61 76 65 64 46 6c 61 67  lags = savedFlag
2ae55 73 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  s;.  pTab = sqli
2ae56 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2ae57 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65  db, sizeof(Table
2ae58 29 20 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  ) );.  if( pTab=
2ae59 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
2ae5a 20 30 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 65   0;.  }.  /* The
2ae5b 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65   sqlite3ResultSe
2ae5c 74 4f 66 53 65 6c 65 63 74 28 29 20 69 73 20 6f  tOfSelect() is o
2ae5d 6e 6c 79 20 75 73 65 64 20 6e 20 63 6f 6e 74 65  nly used n conte
2ae5e 78 74 73 20 77 68 65 72 65 20 6c 6f 6f 6b 61 73  xts where lookas
2ae5f 69 64 65 0a 20 20 2a 2a 20 69 73 20 64 69 73 61  ide.  ** is disa
2ae60 62 6c 65 64 2c 20 73 6f 20 77 65 20 6d 69 67 68  bled, so we migh
2ae61 74 20 61 73 20 77 65 6c 6c 20 68 61 72 64 2d 63  t as well hard-c
2ae62 6f 64 65 20 70 54 61 62 2d 3e 64 62 4d 65 6d 20  ode pTab->dbMem 
2ae63 74 6f 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73  to NULL. */.  as
2ae64 73 65 72 74 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73  sert( db->lookas
2ae65 69 64 65 2e 62 45 6e 61 62 6c 65 64 3d 3d 30 20  ide.bEnabled==0 
2ae66 29 3b 0a 20 20 70 54 61 62 2d 3e 64 62 4d 65 6d  );.  pTab->dbMem
2ae67 20 3d 20 30 3b 0a 20 20 70 54 61 62 2d 3e 6e 52   = 0;.  pTab->nR
2ae68 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62 2d 3e  ef = 1;.  pTab->
2ae69 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 73 65 6c  zName = 0;.  sel
2ae6a 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78  ectColumnsFromEx
2ae6b 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
2ae6c 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20  Select->pEList, 
2ae6d 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70 54  &pTab->nCol, &pT
2ae6e 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 73 65 6c  ab->aCol);.  sel
2ae6f 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  ectAddColumnType
2ae70 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61  AndCollation(pPa
2ae71 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  rse, pTab->nCol,
2ae72 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65   pTab->aCol, pSe
2ae73 6c 65 63 74 29 3b 0a 20 20 70 54 61 62 2d 3e 69  lect);.  pTab->i
2ae74 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 69 66 28  PKey = -1;.  if(
2ae75 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2ae76 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
2ae77 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62  DeleteTable(pTab
2ae78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
2ae79 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54  .  }.  return pT
2ae7a 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  ab;.}../*.** Get
2ae7b 20 61 20 56 44 42 45 20 66 6f 72 20 74 68 65 20   a VDBE for the 
2ae7c 67 69 76 65 6e 20 70 61 72 73 65 72 20 63 6f 6e  given parser con
2ae7d 74 65 78 74 2e 20 20 43 72 65 61 74 65 20 61 20  text.  Create a 
2ae7e 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73  new one if neces
2ae7f 73 61 72 79 2e 0a 2a 2a 20 49 66 20 61 6e 20 65  sary..** If an e
2ae80 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74  rror occurs, ret
2ae81 75 72 6e 20 4e 55 4c 4c 20 61 6e 64 20 6c 65 61  urn NULL and lea
2ae82 76 65 20 61 20 6d 65 73 73 61 67 65 20 69 6e 20  ve a message in 
2ae83 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  pParse..*/.SQLIT
2ae84 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a  E_PRIVATE Vdbe *
2ae85 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 50  sqlite3GetVdbe(P
2ae86 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
2ae87 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2ae88 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 66 28 20  e->pVdbe;.  if( 
2ae89 76 3d 3d 30 20 29 7b 0a 20 20 20 20 76 20 3d 20  v==0 ){.    v = 
2ae8a 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20  pParse->pVdbe = 
2ae8b 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74  sqlite3VdbeCreat
2ae8c 65 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 23  e(pParse->db);.#
2ae8d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2ae8e 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28  IT_TRACE.    if(
2ae8f 20 76 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69   v ){.      sqli
2ae90 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
2ae91 20 4f 50 5f 54 72 61 63 65 29 3b 0a 20 20 20 20   OP_Trace);.    
2ae92 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  }.#endif.  }.  r
2ae93 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn v;.}.../*.
2ae94 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 69  ** Compute the i
2ae95 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
2ae96 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  t fields of the 
2ae97 53 45 4c 45 43 54 20 62 61 73 65 64 20 6f 6e 20  SELECT based on 
2ae98 74 68 65 0a 2a 2a 20 70 4c 69 6d 69 74 20 61 6e  the.** pLimit an
2ae99 64 20 70 4f 66 66 73 65 74 20 65 78 70 72 65 73  d pOffset expres
2ae9a 73 69 6f 6e 73 2e 20 20 70 4c 69 6d 69 74 20 61  sions.  pLimit a
2ae9b 6e 64 20 70 4f 66 66 73 65 74 20 68 6f 6c 64 20  nd pOffset hold 
2ae9c 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a  the expressions.
2ae9d 2a 2a 20 74 68 61 74 20 61 70 70 65 61 72 20 69  ** that appear i
2ae9e 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  n the original S
2ae9f 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74  QL statement aft
2aea0 65 72 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64  er the LIMIT and
2aea1 20 4f 46 46 53 45 54 0a 2a 2a 20 6b 65 79 77 6f   OFFSET.** keywo
2aea2 72 64 73 2e 20 20 4f 72 20 4e 55 4c 4c 20 69 66  rds.  Or NULL if
2aea3 20 74 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20   those keywords 
2aea4 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 69 4c 69  are omitted. iLi
2aea5 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20  mit and iOffset 
2aea6 0a 2a 2a 20 61 72 65 20 74 68 65 20 69 6e 74 65  .** are the inte
2aea7 67 65 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73  ger memory regis
2aea8 74 65 72 20 6e 75 6d 62 65 72 73 20 66 6f 72 20  ter numbers for 
2aea9 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20 74 6f  counters used to
2aeaa 20 63 6f 6d 70 75 74 65 20 0a 2a 2a 20 74 68 65   compute .** the
2aeab 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65   limit and offse
2aeac 74 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  t.  If there is 
2aead 6e 6f 20 6c 69 6d 69 74 20 61 6e 64 2f 6f 72 20  no limit and/or 
2aeae 6f 66 66 73 65 74 2c 20 74 68 65 6e 20 0a 2a 2a  offset, then .**
2aeaf 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66   iLimit and iOff
2aeb0 73 65 74 20 61 72 65 20 6e 65 67 61 74 69 76 65  set are negative
2aeb1 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
2aeb2 74 69 6e 65 20 63 68 61 6e 67 65 73 20 74 68 65  tine changes the
2aeb3 20 76 61 6c 75 65 73 20 6f 66 20 69 4c 69 6d 69   values of iLimi
2aeb4 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 6f 6e  t and iOffset on
2aeb5 6c 79 20 69 66 0a 2a 2a 20 61 20 6c 69 6d 69 74  ly if.** a limit
2aeb6 20 6f 72 20 6f 66 66 73 65 74 20 69 73 20 64 65   or offset is de
2aeb7 66 69 6e 65 64 20 62 79 20 70 4c 69 6d 69 74 20  fined by pLimit 
2aeb8 61 6e 64 20 70 4f 66 66 73 65 74 2e 20 20 69 4c  and pOffset.  iL
2aeb9 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 69 4f 66 66  imit and.** iOff
2aeba 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20  set should have 
2aebb 62 65 65 6e 20 70 72 65 73 65 74 20 74 6f 20 61  been preset to a
2aebc 70 70 72 6f 70 72 69 61 74 65 20 64 65 66 61 75  ppropriate defau
2aebd 6c 74 20 76 61 6c 75 65 73 0a 2a 2a 20 28 75 73  lt values.** (us
2aebe 75 61 6c 6c 79 20 62 75 74 20 6e 6f 74 20 61 6c  ually but not al
2aebf 77 61 79 73 20 2d 31 29 20 70 72 69 6f 72 20 74  ways -1) prior t
2aec0 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  o calling this r
2aec1 6f 75 74 69 6e 65 2e 0a 2a 2a 20 4f 6e 6c 79 20  outine..** Only 
2aec2 69 66 20 70 4c 69 6d 69 74 21 3d 30 20 6f 72 20  if pLimit!=0 or 
2aec3 70 4f 66 66 73 65 74 21 3d 30 20 64 6f 20 74 68  pOffset!=0 do th
2aec4 65 20 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72  e limit register
2aec5 73 20 67 65 74 0a 2a 2a 20 72 65 64 65 66 69 6e  s get.** redefin
2aec6 65 64 2e 20 20 54 68 65 20 55 4e 49 4f 4e 20 41  ed.  The UNION A
2aec7 4c 4c 20 6f 70 65 72 61 74 6f 72 20 75 73 65 73  LL operator uses
2aec8 20 74 68 69 73 20 70 72 6f 70 65 72 74 79 20 74   this property t
2aec9 6f 20 66 6f 72 63 65 0a 2a 2a 20 74 68 65 20 72  o force.** the r
2aeca 65 75 73 65 20 6f 66 20 74 68 65 20 73 61 6d 65  euse of the same
2aecb 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65   limit and offse
2aecc 74 20 72 65 67 69 73 74 65 72 73 20 61 63 72 6f  t registers acro
2aecd 73 73 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 53  ss multiple.** S
2aece 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
2aecf 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2aed0 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
2aed1 69 73 74 65 72 73 28 50 61 72 73 65 20 2a 70 50  isters(Parse *pP
2aed2 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c  arse, Select *p,
2aed3 20 69 6e 74 20 69 42 72 65 61 6b 29 7b 0a 20 20   int iBreak){.  
2aed4 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69  Vdbe *v = 0;.  i
2aed5 6e 74 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  nt iLimit = 0;. 
2aed6 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 20 20   int iOffset;.  
2aed7 69 6e 74 20 61 64 64 72 31 2c 20 6e 3b 0a 20 20  int addr1, n;.  
2aed8 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 20  if( p->iLimit ) 
2aed9 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 0a 20  return;..  /* . 
2aeda 20 2a 2a 20 22 4c 49 4d 49 54 20 2d 31 22 20 61   ** "LIMIT -1" a
2aedb 6c 77 61 79 73 20 73 68 6f 77 73 20 61 6c 6c 20  lways shows all 
2aedc 72 6f 77 73 2e 20 20 54 68 65 72 65 20 69 73 20  rows.  There is 
2aedd 73 6f 6d 65 0a 20 20 2a 2a 20 63 6f 6e 74 72 61  some.  ** contra
2aede 76 65 72 73 79 20 61 62 6f 75 74 20 77 68 61 74  versy about what
2aedf 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 65 68   the correct beh
2aee0 61 76 69 6f 72 20 73 68 6f 75 6c 64 20 62 65 2e  avior should be.
2aee1 0a 20 20 2a 2a 20 54 68 65 20 63 75 72 72 65 6e  .  ** The curren
2aee2 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
2aee3 20 69 6e 74 65 72 70 72 65 74 73 20 22 4c 49 4d   interprets "LIM
2aee4 49 54 20 30 22 20 74 6f 20 6d 65 61 6e 0a 20 20  IT 0" to mean.  
2aee5 2a 2a 20 6e 6f 20 72 6f 77 73 2e 0a 20 20 2a 2f  ** no rows..  */
2aee6 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  .  sqlite3ExprCa
2aee7 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29  cheClear(pParse)
2aee8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  ;.  assert( p->p
2aee9 4f 66 66 73 65 74 3d 3d 30 20 7c 7c 20 70 2d 3e  Offset==0 || p->
2aeea 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20 69  pLimit!=0 );.  i
2aeeb 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  f( p->pLimit ){.
2aeec 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20      p->iLimit = 
2aeed 69 4c 69 6d 69 74 20 3d 20 2b 2b 70 50 61 72 73  iLimit = ++pPars
2aeee 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 76 20 3d  e->nMem;.    v =
2aeef 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2aef0 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28  pParse);.    if(
2aef1 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72   NEVER(v==0) ) r
2aef2 65 74 75 72 6e 3b 20 20 2f 2a 20 56 44 42 45 20  eturn;  /* VDBE 
2aef3 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65  should have alre
2aef4 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ady been allocat
2aef5 65 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  ed */.    if( sq
2aef6 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
2aef7 65 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 26 6e  er(p->pLimit, &n
2aef8 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
2aef9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2aefa 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6e 2c 20 69  OP_Integer, n, i
2aefb 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 56 64  Limit);.      Vd
2aefc 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c  beComment((v, "L
2aefd 49 4d 49 54 20 63 6f 75 6e 74 65 72 22 29 29 3b  IMIT counter"));
2aefe 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 30 20  .      if( n==0 
2aeff 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2af00 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2af01 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 72 65  OP_Goto, 0, iBre
2af02 61 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ak);.      }.   
2af03 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
2af04 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2af05 61 72 73 65 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c  arse, p->pLimit,
2af06 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20   iLimit);.      
2af07 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2af08 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e  1(v, OP_MustBeIn
2af09 74 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  t, iLimit);.    
2af0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2af0b 2c 20 22 4c 49 4d 49 54 20 63 6f 75 6e 74 65 72  , "LIMIT counter
2af0c 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  "));.      sqlit
2af0d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2af0e 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69  OP_IfZero, iLimi
2af0f 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  t, iBreak);.    
2af10 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 66  }.    if( p->pOf
2af11 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 70 2d  fset ){.      p-
2af12 3e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 66 73  >iOffset = iOffs
2af13 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  et = ++pParse->n
2af14 4d 65 6d 3b 0a 20 20 20 20 20 20 70 50 61 72 73  Mem;.      pPars
2af15 65 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20 20 2f 2a 20  e->nMem++;   /* 
2af16 41 6c 6c 6f 63 61 74 65 20 61 6e 20 65 78 74 72  Allocate an extr
2af17 61 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 6c  a register for l
2af18 69 6d 69 74 2b 6f 66 66 73 65 74 20 2a 2f 0a 20  imit+offset */. 
2af19 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2af1a 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d 3e  Code(pParse, p->
2af1b 70 4f 66 66 73 65 74 2c 20 69 4f 66 66 73 65 74  pOffset, iOffset
2af1c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2af1d 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2af1e 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4f 66 66  _MustBeInt, iOff
2af1f 73 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65  set);.      Vdbe
2af20 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 46 46  Comment((v, "OFF
2af21 53 45 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a  SET counter"));.
2af22 20 20 20 20 20 20 61 64 64 72 31 20 3d 20 73 71        addr1 = sq
2af23 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2af24 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4f 66  v, OP_IfPos, iOf
2af25 66 73 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  fset);.      sql
2af26 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2af27 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2af28 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20   iOffset);.     
2af29 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2af2a 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a  Here(v, addr1);.
2af2b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2af2c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64  eAddOp3(v, OP_Ad
2af2d 64 2c 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73  d, iLimit, iOffs
2af2e 65 74 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a  et, iOffset+1);.
2af2f 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2af30 74 28 28 76 2c 20 22 4c 49 4d 49 54 2b 4f 46 46  t((v, "LIMIT+OFF
2af31 53 45 54 22 29 29 3b 0a 20 20 20 20 20 20 61 64  SET"));.      ad
2af32 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
2af33 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
2af34 50 6f 73 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20  Pos, iLimit);.  
2af35 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2af36 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2af37 67 65 72 2c 20 2d 31 2c 20 69 4f 66 66 73 65 74  ger, -1, iOffset
2af38 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  +1);.      sqlit
2af39 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2af3a 2c 20 61 64 64 72 31 29 3b 0a 20 20 20 20 7d 0a  , addr1);.    }.
2af3b 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53    }.}..#ifndef S
2af3c 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
2af3d 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a  UND_SELECT./*.**
2af3e 20 52 65 74 75 72 6e 20 74 68 65 20 61 70 70 72   Return the appr
2af3f 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e  opriate collatin
2af40 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  g sequence for t
2af41 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d  he iCol-th colum
2af42 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 73 75  n of.** the resu
2af43 6c 74 20 73 65 74 20 66 6f 72 20 74 68 65 20 63  lt set for the c
2af44 6f 6d 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20 73  ompound-select s
2af45 74 61 74 65 6d 65 6e 74 20 22 70 22 2e 20 20 52  tatement "p".  R
2af46 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a  eturn NULL if.**
2af47 20 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20   the column has 
2af48 6e 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61  no default colla
2af49 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a  ting sequence..*
2af4a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69  *.** The collati
2af4b 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
2af4c 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  the compound sel
2af4d 65 63 74 20 69 73 20 74 61 6b 65 6e 20 66 72 6f  ect is taken fro
2af4e 6d 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f  m the.** left-mo
2af4f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 73  st term of the s
2af50 65 6c 65 63 74 20 74 68 61 74 20 68 61 73 20 61  elect that has a
2af51 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2af52 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43  nce..*/.static C
2af53 6f 6c 6c 53 65 71 20 2a 6d 75 6c 74 69 53 65 6c  ollSeq *multiSel
2af54 65 63 74 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  ectCollSeq(Parse
2af55 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74   *pParse, Select
2af56 20 2a 70 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0a   *p, int iCol){.
2af57 20 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 74 3b    CollSeq *pRet;
2af58 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72  .  if( p->pPrior
2af59 20 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 6d   ){.    pRet = m
2af5a 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65  ultiSelectCollSe
2af5b 71 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 50 72  q(pParse, p->pPr
2af5c 69 6f 72 2c 20 69 43 6f 6c 29 3b 0a 20 20 7d 65  ior, iCol);.  }e
2af5d 6c 73 65 7b 0a 20 20 20 20 70 52 65 74 20 3d 20  lse{.    pRet = 
2af5e 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
2af5f 20 69 43 6f 6c 3e 3d 30 20 29 3b 0a 20 20 69 66   iCol>=0 );.  if
2af60 28 20 70 52 65 74 3d 3d 30 20 26 26 20 69 43 6f  ( pRet==0 && iCo
2af61 6c 3c 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  l<p->pEList->nEx
2af62 70 72 20 29 7b 0a 20 20 20 20 70 52 65 74 20 3d  pr ){.    pRet =
2af63 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
2af64 53 65 71 28 70 50 61 72 73 65 2c 20 70 2d 3e 70  Seq(pParse, p->p
2af65 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70  EList->a[iCol].p
2af66 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Expr);.  }.  ret
2af67 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 23 65 6e 64  urn pRet;.}.#end
2af68 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2af69 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
2af6a 54 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  T */../* Forward
2af6b 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74   reference */.st
2af6c 61 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65  atic int multiSe
2af6d 6c 65 63 74 4f 72 64 65 72 42 79 28 0a 20 20 50  lectOrderBy(.  P
2af6e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2af6f 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2af70 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
2af71 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
2af72 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d     /* The right-
2af73 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20  most of SELECTs 
2af74 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20  to be coded */. 
2af75 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65   SelectDest *pDe
2af76 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74  st     /* What t
2af77 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72 79 20  o do with query 
2af78 72 65 73 75 6c 74 73 20 2a 2f 0a 29 3b 0a 0a 0a  results */.);...
2af79 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2af7a 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
2af7b 45 43 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  ECT./*.** This r
2af7c 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2af7d 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 63 6f   to process a co
2af7e 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 66 6f 72  mpound query for
2af7f 6d 20 66 72 6f 6d 0a 2a 2a 20 74 77 6f 20 6f 72  m from.** two or
2af80 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 71   more separate q
2af81 75 65 72 69 65 73 20 75 73 69 6e 67 20 55 4e 49  ueries using UNI
2af82 4f 4e 2c 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 45  ON, UNION ALL, E
2af83 58 43 45 50 54 2c 20 6f 72 0a 2a 2a 20 49 4e 54  XCEPT, or.** INT
2af84 45 52 53 45 43 54 0a 2a 2a 0a 2a 2a 20 22 70 22  ERSECT.**.** "p"
2af85 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 72   points to the r
2af86 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 74 68 65  ight-most of the
2af87 20 74 77 6f 20 71 75 65 72 69 65 73 2e 20 20 74   two queries.  t
2af88 68 65 20 71 75 65 72 79 20 6f 6e 20 74 68 65 0a  he query on the.
2af89 2a 2a 20 6c 65 66 74 20 69 73 20 70 2d 3e 70 50  ** left is p->pP
2af8a 72 69 6f 72 2e 20 20 54 68 65 20 6c 65 66 74 20  rior.  The left 
2af8b 71 75 65 72 79 20 63 6f 75 6c 64 20 61 6c 73 6f  query could also
2af8c 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71   be a compound q
2af8d 75 65 72 79 0a 2a 2a 20 69 6e 20 77 68 69 63 68  uery.** in which
2af8e 20 63 61 73 65 20 74 68 69 73 20 72 6f 75 74 69   case this routi
2af8f 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65  ne will be calle
2af90 64 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 0a  d recursively. .
2af91 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  **.** The result
2af92 73 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 71  s of the total q
2af93 75 65 72 79 20 61 72 65 20 74 6f 20 62 65 20 77  uery are to be w
2af94 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 64 65  ritten into a de
2af95 73 74 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20  stination.** of 
2af96 74 79 70 65 20 65 44 65 73 74 20 77 69 74 68 20  type eDest with 
2af97 70 61 72 61 6d 65 74 65 72 20 69 50 61 72 6d 2e  parameter iParm.
2af98 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 20 31  .**.** Example 1
2af99 3a 20 20 43 6f 6e 73 69 64 65 72 20 61 20 74 68  :  Consider a th
2af9a 72 65 65 2d 77 61 79 20 63 6f 6d 70 6f 75 6e 64  ree-way compound
2af9b 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a   SQL statement..
2af9c 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
2af9d 20 61 20 46 52 4f 4d 20 74 31 20 55 4e 49 4f 4e   a FROM t1 UNION
2af9e 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d 20 74   SELECT b FROM t
2af9f 32 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 63  2 UNION SELECT c
2afa0 20 46 52 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a 20 54   FROM t3.**.** T
2afa1 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73  his statement is
2afa2 20 70 61 72 73 65 64 20 75 70 20 61 73 20 66 6f   parsed up as fo
2afa3 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  llows:.**.**    
2afa4 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74   SELECT c FROM t
2afa5 33 0a 2a 2a 20 20 20 20 20 20 7c 0a 2a 2a 20 20  3.**      |.**  
2afa6 20 20 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45 4c      `----->  SEL
2afa7 45 43 54 20 62 20 46 52 4f 4d 20 74 32 0a 2a 2a  ECT b FROM t2.**
2afa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afa9 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  |.**            
2afaa 20 20 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20 53 45      `------>  SE
2afab 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 31 0a 2a  LECT a FROM t1.*
2afac 2a 0a 2a 2a 20 54 68 65 20 61 72 72 6f 77 73 20  *.** The arrows 
2afad 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d 20 61  in the diagram a
2afae 62 6f 76 65 20 72 65 70 72 65 73 65 6e 74 20 74  bove represent t
2afaf 68 65 20 53 65 6c 65 63 74 2e 70 50 72 69 6f 72  he Select.pPrior
2afb0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 53 6f 20   pointer..** So 
2afb1 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  if this routine 
2afb2 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70  is called with p
2afb3 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 33   equal to the t3
2afb4 20 71 75 65 72 79 2c 20 74 68 65 6e 0a 2a 2a 20   query, then.** 
2afb5 70 50 72 69 6f 72 20 77 69 6c 6c 20 62 65 20 74  pPrior will be t
2afb6 68 65 20 74 32 20 71 75 65 72 79 2e 20 20 70 2d  he t2 query.  p-
2afb7 3e 6f 70 20 77 69 6c 6c 20 62 65 20 54 4b 5f 55  >op will be TK_U
2afb8 4e 49 4f 4e 20 69 6e 20 74 68 69 73 20 63 61 73  NION in this cas
2afb9 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65 20  e..**.** Notice 
2afba 74 68 61 74 20 62 65 63 61 75 73 65 20 6f 66 20  that because of 
2afbb 74 68 65 20 77 61 79 20 53 51 4c 69 74 65 20 70  the way SQLite p
2afbc 61 72 73 65 73 20 63 6f 6d 70 6f 75 6e 64 20 53  arses compound S
2afbd 45 4c 45 43 54 73 2c 20 74 68 65 0a 2a 2a 20 69  ELECTs, the.** i
2afbe 6e 64 69 76 69 64 75 61 6c 20 73 65 6c 65 63 74  ndividual select
2afbf 73 20 61 6c 77 61 79 73 20 67 72 6f 75 70 20 66  s always group f
2afc0 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68  rom left to righ
2afc1 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
2afc2 20 6d 75 6c 74 69 53 65 6c 65 63 74 28 0a 20 20   multiSelect(.  
2afc3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2afc4 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2afc5 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
2afc6 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
2afc7 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
2afc8 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73  -most of SELECTs
2afc9 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a   to be coded */.
2afca 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44    SelectDest *pD
2afcb 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20  est     /* What 
2afcc 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72 79  to do with query
2afcd 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20   results */.){. 
2afce 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2afcf 5f 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65 73  _OK;   /* Succes
2afd0 73 20 63 6f 64 65 20 66 72 6f 6d 20 61 20 73 75  s code from a su
2afd1 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 53 65  broutine */.  Se
2afd2 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20  lect *pPrior;   
2afd3 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20 53      /* Another S
2afd4 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65 6c  ELECT immediatel
2afd5 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a 2f  y to our left */
2afd6 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
2afd7 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65           /* Gene
2afd8 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68 69  rate code to thi
2afd9 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c 65  s VDBE */.  Sele
2afda 63 74 44 65 73 74 20 64 65 73 74 3b 20 20 20 20  ctDest dest;    
2afdb 20 20 2f 2a 20 41 6c 74 65 72 6e 61 74 69 76 65    /* Alternative
2afdc 20 64 61 74 61 20 64 65 73 74 69 6e 61 74 69 6f   data destinatio
2afdd 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  n */.  Select *p
2afde 44 65 6c 65 74 65 20 3d 20 30 3b 20 20 2f 2a 20  Delete = 0;  /* 
2afdf 43 68 61 69 6e 20 6f 66 20 73 69 6d 70 6c 65 20  Chain of simple 
2afe0 73 65 6c 65 63 74 73 20 74 6f 20 64 65 6c 65 74  selects to delet
2afe1 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
2afe2 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  db;          /* 
2afe3 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2afe4 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b  ion */..  /* Mak
2afe5 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20  e sure there is 
2afe6 6e 6f 20 4f 52 44 45 52 20 42 59 20 6f 72 20 4c  no ORDER BY or L
2afe7 49 4d 49 54 20 63 6c 61 75 73 65 20 6f 6e 20 70  IMIT clause on p
2afe8 72 69 6f 72 20 53 45 4c 45 43 54 73 2e 20 20 4f  rior SELECTs.  O
2afe9 6e 6c 79 0a 20 20 2a 2a 20 74 68 65 20 6c 61 73  nly.  ** the las
2afea 74 20 28 72 69 67 68 74 2d 6d 6f 73 74 29 20 53  t (right-most) S
2afeb 45 4c 45 43 54 20 69 6e 20 74 68 65 20 73 65 72  ELECT in the ser
2afec 69 65 73 20 6d 61 79 20 68 61 76 65 20 61 6e 20  ies may have an 
2afed 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49  ORDER BY or LIMI
2afee 54 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  T..  */.  assert
2afef 28 20 70 20 26 26 20 70 2d 3e 70 50 72 69 6f 72  ( p && p->pPrior
2aff0 20 29 3b 20 20 2f 2a 20 43 61 6c 6c 69 6e 67 20   );  /* Calling 
2aff1 66 75 6e 63 74 69 6f 6e 20 67 75 61 72 61 6e 74  function guarant
2aff2 65 65 73 20 74 68 69 73 20 6d 75 63 68 20 2a 2f  ees this much */
2aff3 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
2aff4 64 62 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20 70  db;.  pPrior = p
2aff5 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65  ->pPrior;.  asse
2aff6 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67  rt( pPrior->pRig
2aff7 68 74 6d 6f 73 74 21 3d 70 50 72 69 6f 72 20 29  htmost!=pPrior )
2aff8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69  ;.  assert( pPri
2aff9 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d  or->pRightmost==
2affa 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 29 3b  p->pRightmost );
2affb 0a 20 20 64 65 73 74 20 3d 20 2a 70 44 65 73 74  .  dest = *pDest
2affc 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e  ;.  if( pPrior->
2affd 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2affe 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2afff 70 50 61 72 73 65 2c 22 4f 52 44 45 52 20 42 59  pParse,"ORDER BY
2b000 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20 63   clause should c
2b001 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f 74  ome after %s not
2b002 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20 20   before",.      
2b003 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e  selectOpName(p->
2b004 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 31  op));.    rc = 1
2b005 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69  ;.    goto multi
2b006 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d  _select_end;.  }
2b007 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70  .  if( pPrior->p
2b008 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  Limit ){.    sql
2b009 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2b00a 72 73 65 2c 22 4c 49 4d 49 54 20 63 6c 61 75 73  rse,"LIMIT claus
2b00b 65 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66  e should come af
2b00c 74 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72  ter %s not befor
2b00d 65 22 2c 0a 20 20 20 20 20 20 73 65 6c 65 63 74  e",.      select
2b00e 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a  OpName(p->op));.
2b00f 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20      rc = 1;.    
2b010 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
2b011 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76 20  t_end;.  }..  v 
2b012 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2b013 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65  (pParse);.  asse
2b014 72 74 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a 20  rt( v!=0 );  /* 
2b015 54 68 65 20 56 44 42 45 20 61 6c 72 65 61 64 79  The VDBE already
2b016 20 63 72 65 61 74 65 64 20 62 79 20 63 61 6c 6c   created by call
2b017 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ing function */.
2b018 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65  .  /* Create the
2b019 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 74 65 6d   destination tem
2b01a 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69 66 20  porary table if 
2b01b 6e 65 63 65 73 73 61 72 79 0a 20 20 2a 2f 0a 20  necessary.  */. 
2b01c 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d   if( dest.eDest=
2b01d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b  =SRT_EphemTab ){
2b01e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
2b01f 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 73 71  pEList );.    sq
2b020 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b021 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
2b022 72 61 6c 2c 20 64 65 73 74 2e 69 50 61 72 6d 2c  ral, dest.iParm,
2b023 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
2b024 72 29 3b 0a 20 20 20 20 64 65 73 74 2e 65 44 65  r);.    dest.eDe
2b025 73 74 20 3d 20 53 52 54 5f 54 61 62 6c 65 3b 0a  st = SRT_Table;.
2b026 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
2b027 75 72 65 20 61 6c 6c 20 53 45 4c 45 43 54 73 20  ure all SELECTs 
2b028 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  in the statement
2b029 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e   have the same n
2b02a 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
2b02b 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 69 72 20  s.  ** in their 
2b02c 72 65 73 75 6c 74 20 73 65 74 73 2e 0a 20 20 2a  result sets..  *
2b02d 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  /.  assert( p->p
2b02e 45 4c 69 73 74 20 26 26 20 70 50 72 69 6f 72 2d  EList && pPrior-
2b02f 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 69 66 28  >pEList );.  if(
2b030 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
2b031 72 21 3d 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73  r!=pPrior->pELis
2b032 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  t->nExpr ){.    
2b033 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2b034 70 50 61 72 73 65 2c 20 22 53 45 4c 45 43 54 73  pParse, "SELECTs
2b035 20 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6e 64   to the left and
2b036 20 72 69 67 68 74 20 6f 66 20 25 73 22 0a 20 20   right of %s".  
2b037 20 20 20 20 22 20 64 6f 20 6e 6f 74 20 68 61 76      " do not hav
2b038 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65  e the same numbe
2b039 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75  r of result colu
2b03a 6d 6e 73 22 2c 20 73 65 6c 65 63 74 4f 70 4e 61  mns", selectOpNa
2b03b 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20  me(p->op));.    
2b03c 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f  rc = 1;.    goto
2b03d 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e   multi_select_en
2b03e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d  d;.  }..  /* Com
2b03f 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 20 74 68  pound SELECTs th
2b040 61 74 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52  at have an ORDER
2b041 20 42 59 20 63 6c 61 75 73 65 20 61 72 65 20 68   BY clause are h
2b042 61 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65 6c  andled separatel
2b043 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  y..  */.  if( p-
2b044 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
2b045 20 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c   return multiSel
2b046 65 63 74 4f 72 64 65 72 42 79 28 70 50 61 72 73  ectOrderBy(pPars
2b047 65 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a 20 20  e, p, pDest);.  
2b048 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
2b049 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65   code for the le
2b04a 66 74 20 61 6e 64 20 72 69 67 68 74 20 53 45 4c  ft and right SEL
2b04b 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ECT statements..
2b04c 20 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 70    */.  switch( p
2b04d 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ->op ){.    case
2b04e 20 54 4b 5f 41 4c 4c 3a 20 7b 0a 20 20 20 20 20   TK_ALL: {.     
2b04f 20 69 6e 74 20 61 64 64 72 20 3d 20 30 3b 0a 20   int addr = 0;. 
2b050 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50       assert( !pP
2b051 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 3b 0a  rior->pLimit );.
2b052 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4c        pPrior->pL
2b053 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74  imit = p->pLimit
2b054 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e  ;.      pPrior->
2b055 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66  pOffset = p->pOf
2b056 66 73 65 74 3b 0a 20 20 20 20 20 20 72 63 20 3d  fset;.      rc =
2b057 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2b058 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26  Parse, pPrior, &
2b059 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e  dest);.      p->
2b05a 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
2b05b 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30    p->pOffset = 0
2b05c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
2b05d 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d  {.        goto m
2b05e 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b  ulti_select_end;
2b05f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
2b060 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20  ->pPrior = 0;.  
2b061 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20      p->iLimit = 
2b062 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 3b 0a  pPrior->iLimit;.
2b063 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74        p->iOffset
2b064 20 3d 20 70 50 72 69 6f 72 2d 3e 69 4f 66 66 73   = pPrior->iOffs
2b065 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  et;.      if( p-
2b066 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20  >iLimit ){.     
2b067 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2b068 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2b069 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69  P_IfZero, p->iLi
2b06a 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 56 64  mit);.        Vd
2b06b 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a  beComment((v, "J
2b06c 75 6d 70 20 61 68 65 61 64 20 69 66 20 4c 49 4d  ump ahead if LIM
2b06d 49 54 20 72 65 61 63 68 65 64 22 29 29 3b 0a 20  IT reached"));. 
2b06e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
2b06f 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
2b070 70 50 61 72 73 65 2c 20 70 2c 20 26 64 65 73 74  pParse, p, &dest
2b071 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2b072 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc!=SQLITE_OK
2b073 20 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74   );.      pDelet
2b074 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  e = p->pPrior;. 
2b075 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
2b076 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 69   pPrior;.      i
2b077 66 28 20 61 64 64 72 20 29 7b 0a 20 20 20 20 20  f( addr ){.     
2b078 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
2b079 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
2b07a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
2b07b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2b07c 63 61 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 0a  case TK_EXCEPT:.
2b07d 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4e 49 4f      case TK_UNIO
2b07e 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 75  N: {.      int u
2b07f 6e 69 6f 6e 54 61 62 3b 20 20 20 20 2f 2a 20 43  nionTab;    /* C
2b080 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
2b081 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61  the temporary ta
2b082 62 6c 65 20 68 6f 6c 64 69 6e 67 20 72 65 73 75  ble holding resu
2b083 6c 74 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 6f  lt */.      u8 o
2b084 70 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  p = 0;       /* 
2b085 4f 6e 65 20 6f 66 20 74 68 65 20 53 52 54 5f 20  One of the SRT_ 
2b086 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 61 70  operations to ap
2b087 70 6c 79 20 74 6f 20 73 65 6c 66 20 2a 2f 0a 20  ply to self */. 
2b088 20 20 20 20 20 69 6e 74 20 70 72 69 6f 72 4f 70       int priorOp
2b089 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 53 52 54  ;     /* The SRT
2b08a 5f 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61  _ operation to a
2b08b 70 70 6c 79 20 74 6f 20 70 72 69 6f 72 20 73 65  pply to prior se
2b08c 6c 65 63 74 73 20 2a 2f 0a 20 20 20 20 20 20 45  lects */.      E
2b08d 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f  xpr *pLimit, *pO
2b08e 66 66 73 65 74 3b 20 2f 2a 20 53 61 76 65 64 20  ffset; /* Saved 
2b08f 76 61 6c 75 65 73 20 6f 66 20 70 2d 3e 6e 4c 69  values of p->nLi
2b090 6d 69 74 20 61 6e 64 20 70 2d 3e 6e 4f 66 66 73  mit and p->nOffs
2b091 65 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  et */.      int 
2b092 61 64 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65  addr;.      Sele
2b093 63 74 44 65 73 74 20 75 6e 69 6f 6e 64 65 73 74  ctDest uniondest
2b094 3b 0a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ;..      testcas
2b095 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43  e( p->op==TK_EXC
2b096 45 50 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  EPT );.      tes
2b097 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b  tcase( p->op==TK
2b098 5f 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20  _UNION );.      
2b099 70 72 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55 6e  priorOp = SRT_Un
2b09a 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 64  ion;.      if( d
2b09b 65 73 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f 72  est.eDest==prior
2b09c 4f 70 20 26 26 20 41 4c 57 41 59 53 28 21 70 2d  Op && ALWAYS(!p-
2b09d 3e 70 4c 69 6d 69 74 20 26 26 21 70 2d 3e 70 4f  >pLimit &&!p->pO
2b09e 66 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20  ffset) ){.      
2b09f 20 20 2f 2a 20 57 65 20 63 61 6e 20 72 65 75 73    /* We can reus
2b0a0 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  e a temporary ta
2b0a1 62 6c 65 20 67 65 6e 65 72 61 74 65 64 20 62 79  ble generated by
2b0a2 20 61 20 53 45 4c 45 43 54 20 74 6f 20 6f 75 72   a SELECT to our
2b0a3 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 69 67 68  .        ** righ
2b0a4 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  t..        */.  
2b0a5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
2b0a6 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d 70 20 29  >pRightmost!=p )
2b0a7 3b 20 20 2f 2a 20 43 61 6e 20 6f 6e 6c 79 20 68  ;  /* Can only h
2b0a8 61 70 70 65 6e 20 66 6f 72 20 6c 65 66 74 77 61  appen for leftwa
2b0a9 72 64 20 65 6c 65 6d 65 6e 74 73 0a 20 20 20 20  rd elements.    
2b0aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b0ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b0ac 20 2a 2a 20 6f 66 20 61 20 33 2d 77 61 79 20 6f   ** of a 3-way o
2b0ad 72 20 6d 6f 72 65 20 63 6f 6d 70 6f 75 6e 64 20  r more compound 
2b0ae 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
2b0af 74 28 20 70 2d 3e 70 4c 69 6d 69 74 3d 3d 30 20  t( p->pLimit==0 
2b0b0 29 3b 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61  );      /* Not a
2b0b1 6c 6c 6f 77 65 64 20 6f 6e 20 6c 65 66 74 77 61  llowed on leftwa
2b0b2 72 64 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  rd elements */. 
2b0b3 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2b0b4 2d 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20  ->pOffset==0 ); 
2b0b5 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77      /* Not allow
2b0b6 65 64 20 6f 6e 20 6c 65 66 74 77 61 72 64 20 65  ed on leftward e
2b0b7 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  lements */.     
2b0b8 20 20 20 75 6e 69 6f 6e 54 61 62 20 3d 20 64 65     unionTab = de
2b0b9 73 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20  st.iParm;.      
2b0ba 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
2b0bb 2a 20 57 65 20 77 69 6c 6c 20 6e 65 65 64 20 74  * We will need t
2b0bc 6f 20 63 72 65 61 74 65 20 6f 75 72 20 6f 77 6e  o create our own
2b0bd 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2b0be 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 20 20 20   to hold the.   
2b0bf 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6d 65 64       ** intermed
2b0c0 69 61 74 65 20 72 65 73 75 6c 74 73 2e 0a 20 20  iate results..  
2b0c1 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2b0c2 20 75 6e 69 6f 6e 54 61 62 20 3d 20 70 50 61 72   unionTab = pPar
2b0c3 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
2b0c4 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
2b0c5 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20  OrderBy==0 );.  
2b0c6 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
2b0c7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b0c8 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2b0c9 61 6c 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29  al, unionTab, 0)
2b0ca 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2b0cb 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  ( p->addrOpenEph
2b0cc 6d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20  m[0] == -1 );.  
2b0cd 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65        p->addrOpe
2b0ce 6e 45 70 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b  nEphm[0] = addr;
2b0cf 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 52 69 67  .        p->pRig
2b0d0 68 74 6d 6f 73 74 2d 3e 73 65 6c 46 6c 61 67 73  htmost->selFlags
2b0d1 20 7c 3d 20 53 46 5f 55 73 65 73 45 70 68 65 6d   |= SF_UsesEphem
2b0d2 65 72 61 6c 3b 0a 20 20 20 20 20 20 20 20 61 73  eral;.        as
2b0d3 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
2b0d4 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
2b0d5 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45    /* Code the SE
2b0d6 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
2b0d7 74 6f 20 6f 75 72 20 6c 65 66 74 0a 20 20 20 20  to our left.    
2b0d8 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72    */.      asser
2b0d9 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4f 72 64  t( !pPrior->pOrd
2b0da 65 72 42 79 20 29 3b 0a 20 20 20 20 20 20 73 71  erBy );.      sq
2b0db 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
2b0dc 6e 69 74 28 26 75 6e 69 6f 6e 64 65 73 74 2c 20  nit(&uniondest, 
2b0dd 70 72 69 6f 72 4f 70 2c 20 75 6e 69 6f 6e 54 61  priorOp, unionTa
2b0de 62 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  b);.      rc = s
2b0df 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2b0e0 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 75 6e  rse, pPrior, &un
2b0e1 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20 20 20  iondest);.      
2b0e2 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
2b0e3 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
2b0e4 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
2b0e5 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20  ..      /* Code 
2b0e6 74 68 65 20 63 75 72 72 65 6e 74 20 53 45 4c 45  the current SELE
2b0e7 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20  CT statement.   
2b0e8 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
2b0e9 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54  p->op==TK_EXCEPT
2b0ea 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 70 20 3d   ){.        op =
2b0eb 20 53 52 54 5f 45 78 63 65 70 74 3b 0a 20 20 20   SRT_Except;.   
2b0ec 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2b0ed 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d    assert( p->op=
2b0ee 3d 54 4b 5f 55 4e 49 4f 4e 20 29 3b 0a 20 20 20  =TK_UNION );.   
2b0ef 20 20 20 20 20 6f 70 20 3d 20 53 52 54 5f 55 6e       op = SRT_Un
2b0f0 69 6f 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ion;.      }.   
2b0f1 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30     p->pPrior = 0
2b0f2 3b 0a 20 20 20 20 20 20 70 4c 69 6d 69 74 20 3d  ;.      pLimit =
2b0f3 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20   p->pLimit;.    
2b0f4 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b    p->pLimit = 0;
2b0f5 0a 20 20 20 20 20 20 70 4f 66 66 73 65 74 20 3d  .      pOffset =
2b0f6 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20   p->pOffset;.   
2b0f7 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
2b0f8 30 3b 0a 20 20 20 20 20 20 75 6e 69 6f 6e 64 65  0;.      unionde
2b0f9 73 74 2e 65 44 65 73 74 20 3d 20 6f 70 3b 0a 20  st.eDest = op;. 
2b0fa 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2b0fb 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2b0fc 70 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b 0a  p, &uniondest);.
2b0fd 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b0fe 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
2b0ff 0a 20 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20  .      /* Query 
2b100 66 6c 61 74 74 65 6e 69 6e 67 20 69 6e 20 73 71  flattening in sq
2b101 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 6d 69  lite3Select() mi
2b102 67 68 74 20 72 65 66 69 6c 6c 20 70 2d 3e 70 4f  ght refill p->pO
2b103 72 64 65 72 42 79 2e 0a 20 20 20 20 20 20 2a 2a  rderBy..      **
2b104 20 42 65 20 73 75 72 65 20 74 6f 20 64 65 6c 65   Be sure to dele
2b105 74 65 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20  te p->pOrderBy, 
2b106 74 68 65 72 65 66 6f 72 65 2c 20 74 6f 20 61 76  therefore, to av
2b107 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61  oid a memory lea
2b108 6b 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  k. */.      sqli
2b109 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2b10a 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42  e(db, p->pOrderB
2b10b 79 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74  y);.      pDelet
2b10c 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  e = p->pPrior;. 
2b10d 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
2b10e 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70   pPrior;.      p
2b10f 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  ->pOrderBy = 0;.
2b110 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2b111 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
2b112 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 2d  Limit);.      p-
2b113 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74  >pLimit = pLimit
2b114 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73  ;.      p->pOffs
2b115 65 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20  et = pOffset;.  
2b116 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20      p->iLimit = 
2b117 30 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66  0;.      p->iOff
2b118 73 65 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20  set = 0;..      
2b119 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 64  /* Convert the d
2b11a 61 74 61 20 69 6e 20 74 68 65 20 74 65 6d 70 6f  ata in the tempo
2b11b 72 61 72 79 20 74 61 62 6c 65 20 69 6e 74 6f 20  rary table into 
2b11c 77 68 61 74 65 76 65 72 20 66 6f 72 6d 0a 20 20  whatever form.  
2b11d 20 20 20 20 2a 2a 20 69 74 20 69 73 20 74 68 61      ** it is tha
2b11e 74 20 77 65 20 63 75 72 72 65 6e 74 6c 79 20 6e  t we currently n
2b11f 65 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  eed..      */.  
2b120 20 20 20 20 61 73 73 65 72 74 28 20 75 6e 69 6f      assert( unio
2b121 6e 54 61 62 3d 3d 64 65 73 74 2e 69 50 61 72 6d  nTab==dest.iParm
2b122 20 7c 7c 20 64 65 73 74 2e 65 44 65 73 74 21 3d   || dest.eDest!=
2b123 70 72 69 6f 72 4f 70 20 29 3b 0a 20 20 20 20 20  priorOp );.     
2b124 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 21   if( dest.eDest!
2b125 3d 70 72 69 6f 72 4f 70 20 29 7b 0a 20 20 20 20  =priorOp ){.    
2b126 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69      int iCont, i
2b127 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20  Break, iStart;. 
2b128 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2b129 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20  ->pEList );.    
2b12a 20 20 20 20 69 66 28 20 64 65 73 74 2e 65 44 65      if( dest.eDe
2b12b 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29  st==SRT_Output )
2b12c 7b 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c 65  {.          Sele
2b12d 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a  ct *pFirst = p;.
2b12e 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
2b12f 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20   pFirst->pPrior 
2b130 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 73  ) pFirst = pFirs
2b131 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20  t->pPrior;.     
2b132 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c       generateCol
2b133 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
2b134 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69   0, pFirst->pELi
2b135 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  st);.        }. 
2b136 20 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d 20         iBreak = 
2b137 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2b138 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  abel(v);.       
2b139 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33   iCont = sqlite3
2b13a 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2b13b 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
2b13c 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28  eLimitRegisters(
2b13d 70 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61  pParse, p, iBrea
2b13e 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  k);.        sqli
2b13f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b140 20 4f 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69 6f   OP_Rewind, unio
2b141 6e 54 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a 20  nTab, iBreak);. 
2b142 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20         iStart = 
2b143 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2b144 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20  ntAddr(v);.     
2b145 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
2b146 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  op(pParse, p, p-
2b147 3e 70 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54 61  >pEList, unionTa
2b148 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  b, p->pEList->nE
2b149 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  xpr,.           
2b14a 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
2b14b 2d 31 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74  -1, &dest, iCont
2b14c 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
2b14d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2b14e 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43  solveLabel(v, iC
2b14f 6f 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ont);.        sq
2b150 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b151 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 75 6e 69 6f  v, OP_Next, unio
2b152 6e 54 61 62 2c 20 69 53 74 61 72 74 29 3b 0a 20  nTab, iStart);. 
2b153 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2b154 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2b155 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
2b156 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b157 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
2b158 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a  , unionTab, 0);.
2b159 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2b15a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
2b15b 65 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20  efault: assert( 
2b15c 70 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53  p->op==TK_INTERS
2b15d 45 43 54 20 29 3b 20 7b 0a 20 20 20 20 20 20 69  ECT ); {.      i
2b15e 6e 74 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20  nt tab1, tab2;. 
2b15f 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20       int iCont, 
2b160 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a  iBreak, iStart;.
2b161 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d        Expr *pLim
2b162 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20  it, *pOffset;.  
2b163 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
2b164 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 69      SelectDest i
2b165 6e 74 65 72 73 65 63 74 64 65 73 74 3b 0a 20 20  ntersectdest;.  
2b166 20 20 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 20      int r1;..   
2b167 20 20 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20     /* INTERSECT 
2b168 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  is different fro
2b169 6d 20 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e  m the others sin
2b16a 63 65 20 69 74 20 72 65 71 75 69 72 65 73 0a 20  ce it requires. 
2b16b 20 20 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70       ** two temp
2b16c 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48  orary tables.  H
2b16d 65 6e 63 65 20 69 74 20 68 61 73 20 69 74 73 20  ence it has its 
2b16e 6f 77 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e  own case.  Begin
2b16f 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c  .      ** by all
2b170 6f 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c  ocating the tabl
2b171 65 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e  es we will need.
2b172 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2b173 74 61 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e  tab1 = pParse->n
2b174 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62  Tab++;.      tab
2b175 32 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  2 = pParse->nTab
2b176 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ++;.      assert
2b177 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30  ( p->pOrderBy==0
2b178 20 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20   );..      addr 
2b179 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2b17a 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70  Op2(v, OP_OpenEp
2b17b 68 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30  hemeral, tab1, 0
2b17c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2b17d 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
2b17e 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20  [0] == -1 );.   
2b17f 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70     p->addrOpenEp
2b180 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20  hm[0] = addr;.  
2b181 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73      p->pRightmos
2b182 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  t->selFlags |= S
2b183 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b  F_UsesEphemeral;
2b184 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2b185 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20  ->pEList );..   
2b186 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53     /* Code the S
2b187 45 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65  ELECTs to our le
2b188 66 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72  ft into temporar
2b189 79 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a  y table "tab1"..
2b18a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
2b18b 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2b18c 49 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74 64  Init(&intersectd
2b18d 65 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20  est, SRT_Union, 
2b18e 74 61 62 31 29 3b 0a 20 20 20 20 20 20 72 63 20  tab1);.      rc 
2b18f 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
2b190 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20  pParse, pPrior, 
2b191 26 69 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b  &intersectdest);
2b192 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
2b193 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
2b194 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
2b195 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
2b196 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65  * Code the curre
2b197 6e 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74  nt SELECT into t
2b198 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22  emporary table "
2b199 74 61 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20  tab2".      */. 
2b19a 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
2b19b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b19c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2b19d 6c 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20  l, tab2, 0);.   
2b19e 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64     assert( p->ad
2b19f 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d  drOpenEphm[1] ==
2b1a0 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e   -1 );.      p->
2b1a1 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20  addrOpenEphm[1] 
2b1a2 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d  = addr;.      p-
2b1a3 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20  >pPrior = 0;.   
2b1a4 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70     pLimit = p->p
2b1a5 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
2b1a6 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
2b1a7 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70    pOffset = p->p
2b1a8 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d  Offset;.      p-
2b1a9 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >pOffset = 0;.  
2b1aa 20 20 20 20 69 6e 74 65 72 73 65 63 74 64 65 73      intersectdes
2b1ab 74 2e 69 50 61 72 6d 20 3d 20 74 61 62 32 3b 0a  t.iParm = tab2;.
2b1ac 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2b1ad 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
2b1ae 20 70 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65   p, &intersectde
2b1af 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  st);.      testc
2b1b0 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
2b1b1 4f 4b 20 29 3b 0a 20 20 20 20 20 20 70 44 65 6c  OK );.      pDel
2b1b2 65 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b  ete = p->pPrior;
2b1b3 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  .      p->pPrior
2b1b4 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20   = pPrior;.     
2b1b5 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2b1b6 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74  te(db, p->pLimit
2b1b7 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  );.      p->pLim
2b1b8 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20  it = pLimit;.   
2b1b9 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
2b1ba 70 4f 66 66 73 65 74 3b 0a 0a 20 20 20 20 20 20  pOffset;..      
2b1bb 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2b1bc 20 74 6f 20 74 61 6b 65 20 74 68 65 20 69 6e 74   to take the int
2b1bd 65 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65  ersection of the
2b1be 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a 20   two temporary. 
2b1bf 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e 0a       ** tables..
2b1c0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
2b1c1 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74  ssert( p->pEList
2b1c2 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65   );.      if( de
2b1c3 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  st.eDest==SRT_Ou
2b1c4 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20  tput ){.        
2b1c5 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d  Select *pFirst =
2b1c6 20 70 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c   p;.        whil
2b1c7 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f  e( pFirst->pPrio
2b1c8 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69  r ) pFirst = pFi
2b1c9 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  rst->pPrior;.   
2b1ca 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c       generateCol
2b1cb 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
2b1cc 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69   0, pFirst->pELi
2b1cd 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  st);.      }.   
2b1ce 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69     iBreak = sqli
2b1cf 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2b1d0 28 76 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e 74  (v);.      iCont
2b1d1 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2b1d2 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
2b1d3 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65    computeLimitRe
2b1d4 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20  gisters(pParse, 
2b1d5 70 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  p, iBreak);.    
2b1d6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b1d7 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
2b1d8 2c 20 74 61 62 31 2c 20 69 42 72 65 61 6b 29 3b  , tab1, iBreak);
2b1d9 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
2b1da 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2b1db 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 53 74  arse);.      iSt
2b1dc 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  art = sqlite3Vdb
2b1dd 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
2b1de 77 4b 65 79 2c 20 74 61 62 31 2c 20 72 31 29 3b  wKey, tab1, r1);
2b1df 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b1e0 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f  beAddOp4Int(v, O
2b1e1 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 62 32  P_NotFound, tab2
2b1e2 2c 20 69 43 6f 6e 74 2c 20 72 31 2c 20 30 29 3b  , iCont, r1, 0);
2b1e3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
2b1e4 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2b1e5 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  rse, r1);.      
2b1e6 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28  selectInnerLoop(
2b1e7 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45  pParse, p, p->pE
2b1e8 4c 69 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e 70  List, tab1, p->p
2b1e9 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20  EList->nExpr,.  
2b1ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b1eb 20 20 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74      0, -1, &dest
2b1ec 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29  , iCont, iBreak)
2b1ed 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b1ee 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2b1ef 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20  v, iCont);.     
2b1f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b1f1 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74  p2(v, OP_Next, t
2b1f2 61 62 31 2c 20 69 53 74 61 72 74 29 3b 0a 20 20  ab1, iStart);.  
2b1f3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2b1f4 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
2b1f5 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71  Break);.      sq
2b1f6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b1f7 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62  v, OP_Close, tab
2b1f8 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  2, 0);.      sql
2b1f9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b1fa 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31  , OP_Close, tab1
2b1fb 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , 0);.      brea
2b1fc 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
2b1fd 2f 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c 61  /* Compute colla
2b1fe 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 75  ting sequences u
2b1ff 73 65 64 20 62 79 20 0a 20 20 2a 2a 20 74 65 6d  sed by .  ** tem
2b200 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e 65  porary tables ne
2b201 65 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  eded to implemen
2b202 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  t the compound s
2b203 65 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74 61  elect..  ** Atta
2b204 63 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  ch the KeyInfo s
2b205 74 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c 20  tructure to all 
2b206 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
2b207 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
2b208 20 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e 20   section is run 
2b209 62 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  by the right-mos
2b20a 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  t SELECT stateme
2b20b 6e 74 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 45  nt only..  ** SE
2b20c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
2b20d 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77 61  to the left alwa
2b20e 79 73 20 73 6b 69 70 20 74 68 69 73 20 70 61 72  ys skip this par
2b20f 74 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f  t.  The right-mo
2b210 73 74 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 6d  st.  ** SELECT m
2b211 69 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20 74  ight also skip t
2b212 68 69 73 20 70 61 72 74 20 69 66 20 69 74 20 68  his part if it h
2b213 61 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 63  as no ORDER BY c
2b214 6c 61 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20 6e  lause and.  ** n
2b215 6f 20 74 65 6d 70 20 74 61 62 6c 65 73 20 61 72  o temp tables ar
2b216 65 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f  e required..  */
2b217 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61  .  if( p->selFla
2b218 67 73 20 26 20 53 46 5f 55 73 65 73 45 70 68 65  gs & SF_UsesEphe
2b219 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74  meral ){.    int
2b21a 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
2b21b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2b21c 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
2b21d 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
2b21e 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  nfo;            
2b21f 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
2b220 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 72 65  uence for the re
2b221 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20  sult set */.    
2b222 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20 20  Select *pLoop;  
2b223 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b224 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72   For looping thr
2b225 6f 75 67 68 20 53 45 4c 45 43 54 20 73 74 61 74  ough SELECT stat
2b226 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43 6f  ements */.    Co
2b227 6c 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20  llSeq **apColl; 
2b228 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
2b229 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75  or looping throu
2b22a 67 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  gh pKeyInfo->aCo
2b22b 6c 6c 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ll[] */.    int 
2b22c 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nCol;           
2b22d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2b22e 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
2b22f 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  n result set */.
2b230 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
2b231 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29 3b  pRightmost==p );
2b232 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e 70  .    nCol = p->p
2b233 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  EList->nExpr;.  
2b234 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c    pKeyInfo = sql
2b235 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2b236 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  (db,.           
2b237 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65              size
2b238 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e 43  of(*pKeyInfo)+nC
2b239 6f 6c 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  ol*(sizeof(CollS
2b23a 65 71 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20 20  eq*) + 1));.    
2b23b 69 66 28 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b  if( !pKeyInfo ){
2b23c 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
2b23d 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
2b23e 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
2b23f 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20 20  t_end;.    }..  
2b240 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20    pKeyInfo->enc 
2b241 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70  = ENC(db);.    p
2b242 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20  KeyInfo->nField 
2b243 3d 20 28 75 31 36 29 6e 43 6f 6c 3b 0a 0a 20 20  = (u16)nCol;..  
2b244 20 20 66 6f 72 28 69 3d 30 2c 20 61 70 43 6f 6c    for(i=0, apCol
2b245 6c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  l=pKeyInfo->aCol
2b246 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20  l; i<nCol; i++, 
2b247 61 70 43 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20  apColl++){.     
2b248 20 2a 61 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69   *apColl = multi
2b249 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50  SelectCollSeq(pP
2b24a 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20  arse, p, i);.   
2b24b 20 20 20 69 66 28 20 30 3d 3d 2a 61 70 43 6f 6c     if( 0==*apCol
2b24c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 61 70  l ){.        *ap
2b24d 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
2b24e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
2b24f 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 70 4c 6f    }..    for(pLo
2b250 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f  op=p; pLoop; pLo
2b251 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72  op=pLoop->pPrior
2b252 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  ){.      for(i=0
2b253 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<2; i++){.   
2b254 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20       int addr = 
2b255 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45  pLoop->addrOpenE
2b256 70 68 6d 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  phm[i];.        
2b257 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20  if( addr<0 ){.  
2b258 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 5b 30          /* If [0
2b259 5d 20 69 73 20 75 6e 75 73 65 64 20 74 68 65 6e  ] is unused then
2b25a 20 5b 31 5d 20 69 73 20 61 6c 73 6f 20 75 6e 75   [1] is also unu
2b25b 73 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e 0a  sed.  So we can.
2b25c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 77            ** alw
2b25d 61 79 73 20 73 61 66 65 6c 79 20 61 62 6f 72 74  ays safely abort
2b25e 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20   as soon as the 
2b25f 66 69 72 73 74 20 75 6e 75 73 65 64 20 73 6c 6f  first unused slo
2b260 74 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20  t is found */.  
2b261 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2b262 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45  pLoop->addrOpenE
2b263 70 68 6d 5b 31 5d 3c 30 20 29 3b 0a 20 20 20 20  phm[1]<0 );.    
2b264 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b265 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
2b266 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2b267 50 32 28 76 2c 20 61 64 64 72 2c 20 6e 43 6f 6c  P2(v, addr, nCol
2b268 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2b269 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2b26a 2c 20 61 64 64 72 2c 20 28 63 68 61 72 2a 29 70  , addr, (char*)p
2b26b 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  KeyInfo, P4_KEYI
2b26c 4e 46 4f 29 3b 0a 20 20 20 20 20 20 20 20 70 4c  NFO);.        pL
2b26d 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  oop->addrOpenEph
2b26e 6d 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20  m[i] = -1;.     
2b26f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
2b270 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2b271 4b 65 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a 6d  KeyInfo);.  }..m
2b272 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3a  ulti_select_end:
2b273 0a 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d  .  pDest->iMem =
2b274 20 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 70 44   dest.iMem;.  pD
2b275 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 64 65 73 74  est->nMem = dest
2b276 2e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33  .nMem;.  sqlite3
2b277 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
2b278 20 70 44 65 6c 65 74 65 29 3b 0a 20 20 72 65 74   pDelete);.  ret
2b279 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
2b27a 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2b27b 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20  COMPOUND_SELECT 
2b27c 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61  */../*.** Code a
2b27d 6e 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74  n output subrout
2b27e 69 6e 65 20 66 6f 72 20 61 20 63 6f 72 6f 75 74  ine for a corout
2b27f 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ine implementati
2b280 6f 6e 20 6f 66 20 61 0a 2a 2a 20 53 45 4c 45 43  on of a.** SELEC
2b281 54 20 73 74 61 74 6d 65 6e 74 2e 0a 2a 2a 0a 2a  T statment..**.*
2b282 2a 20 54 68 65 20 64 61 74 61 20 74 6f 20 62 65  * The data to be
2b283 20 6f 75 74 70 75 74 20 69 73 20 63 6f 6e 74 61   output is conta
2b284 69 6e 65 64 20 69 6e 20 70 49 6e 2d 3e 69 4d 65  ined in pIn->iMe
2b285 6d 2e 20 20 54 68 65 72 65 20 61 72 65 0a 2a 2a  m.  There are.**
2b286 20 70 49 6e 2d 3e 6e 4d 65 6d 20 63 6f 6c 75 6d   pIn->nMem colum
2b287 6e 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74 2e  ns to be output.
2b288 20 20 70 44 65 73 74 20 69 73 20 77 68 65 72 65    pDest is where
2b289 20 74 68 65 20 6f 75 74 70 75 74 20 73 68 6f 75   the output shou
2b28a 6c 64 0a 2a 2a 20 62 65 20 73 65 6e 74 2e 0a 2a  ld.** be sent..*
2b28b 2a 0a 2a 2a 20 72 65 67 52 65 74 75 72 6e 20 69  *.** regReturn i
2b28c 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
2b28d 74 68 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c  the register hol
2b28e 64 69 6e 67 20 74 68 65 20 73 75 62 72 6f 75 74  ding the subrout
2b28f 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61 64  ine.** return ad
2b290 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  dress..**.** If 
2b291 72 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20 69  regPrev>0 then i
2b292 74 20 69 73 20 61 20 74 68 65 20 66 69 72 73 74  t is a the first
2b293 20 72 65 67 69 73 74 65 72 20 69 6e 20 61 20 76   register in a v
2b294 65 63 74 6f 72 20 74 68 61 74 0a 2a 2a 20 72 65  ector that.** re
2b295 63 6f 72 64 73 20 74 68 65 20 70 72 65 76 69 6f  cords the previo
2b296 75 73 20 6f 75 74 70 75 74 2e 20 20 6d 65 6d 5b  us output.  mem[
2b297 72 65 67 50 72 65 76 5d 20 69 73 20 61 20 66 6c  regPrev] is a fl
2b298 61 67 20 74 68 61 74 20 69 73 20 66 61 6c 73 65  ag that is false
2b299 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 68 61 73  .** if there has
2b29a 20 62 65 65 6e 20 6e 6f 20 70 72 65 76 69 6f 75   been no previou
2b29b 73 20 6f 75 74 70 75 74 2e 20 20 49 66 20 72 65  s output.  If re
2b29c 67 50 72 65 76 3e 30 20 74 68 65 6e 20 63 6f 64  gPrev>0 then cod
2b29d 65 20 69 73 0a 2a 2a 20 67 65 6e 65 72 61 74 65  e is.** generate
2b29e 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 64 75  d to suppress du
2b29f 70 6c 69 63 61 74 65 73 2e 20 20 70 4b 65 79 49  plicates.  pKeyI
2b2a0 6e 66 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20  nfo is used for 
2b2a1 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 6b 65 79  comparing.** key
2b2a2 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  s..**.** If the 
2b2a3 4c 49 4d 49 54 20 66 6f 75 6e 64 20 69 6e 20 70  LIMIT found in p
2b2a4 2d 3e 69 4c 69 6d 69 74 20 69 73 20 72 65 61 63  ->iLimit is reac
2b2a5 68 65 64 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69  hed, jump immedi
2b2a6 61 74 65 6c 79 20 74 6f 0a 2a 2a 20 69 42 72 65  ately to.** iBre
2b2a7 61 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ak..*/.static in
2b2a8 74 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74  t generateOutput
2b2a9 53 75 62 72 6f 75 74 69 6e 65 28 0a 20 20 50 61  Subroutine(.  Pa
2b2aa 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2b2ab 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2b2ac 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
2b2ad 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
2b2ae 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
2b2af 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ECT statement */
2b2b0 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
2b2b1 49 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  In,        /* Co
2b2b2 72 6f 75 74 69 6e 65 20 73 75 70 70 6c 79 69 6e  routine supplyin
2b2b3 67 20 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65  g data */.  Sele
2b2b4 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20  ctDest *pDest,  
2b2b5 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20      /* Where to 
2b2b6 73 65 6e 64 20 74 68 65 20 64 61 74 61 20 2a 2f  send the data */
2b2b7 0a 20 20 69 6e 74 20 72 65 67 52 65 74 75 72 6e  .  int regReturn
2b2b8 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
2b2b9 65 20 72 65 74 75 72 6e 20 61 64 64 72 65 73 73  e return address
2b2ba 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69   register */.  i
2b2bb 6e 74 20 72 65 67 50 72 65 76 2c 20 20 20 20 20  nt regPrev,     
2b2bc 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f         /* Previo
2b2bd 75 73 20 72 65 73 75 6c 74 20 72 65 67 69 73 74  us result regist
2b2be 65 72 2e 20 20 4e 6f 20 75 6e 69 71 75 65 6e 65  er.  No uniquene
2b2bf 73 73 20 69 66 20 30 20 2a 2f 0a 20 20 4b 65 79  ss if 0 */.  Key
2b2c0 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20  Info *pKeyInfo, 
2b2c1 20 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70       /* For comp
2b2c2 61 72 69 6e 67 20 77 69 74 68 20 70 72 65 76 69  aring with previ
2b2c3 6f 75 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69  ous entry */.  i
2b2c4 6e 74 20 70 34 74 79 70 65 2c 20 20 20 20 20 20  nt p4type,      
2b2c5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 34         /* The p4
2b2c6 20 74 79 70 65 20 66 6f 72 20 70 4b 65 79 49 6e   type for pKeyIn
2b2c7 66 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65  fo */.  int iBre
2b2c8 61 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ak              
2b2c9 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20  /* Jump here if 
2b2ca 77 65 20 68 69 74 20 74 68 65 20 4c 49 4d 49 54  we hit the LIMIT
2b2cb 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
2b2cc 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2b2cd 3b 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  ;.  int iContinu
2b2ce 65 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 0a  e;.  int addr;..
2b2cf 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
2b2d0 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2b2d1 76 29 3b 0a 20 20 69 43 6f 6e 74 69 6e 75 65 20  v);.  iContinue 
2b2d2 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2b2d3 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f 2a  eLabel(v);..  /*
2b2d4 20 53 75 70 70 72 65 73 73 20 64 75 70 6c 69 63   Suppress duplic
2b2d5 61 74 65 73 20 66 6f 72 20 55 4e 49 4f 4e 2c 20  ates for UNION, 
2b2d6 45 58 43 45 50 54 2c 20 61 6e 64 20 49 4e 54 45  EXCEPT, and INTE
2b2d7 52 53 45 43 54 20 0a 20 20 2a 2f 0a 20 20 69 66  RSECT .  */.  if
2b2d8 28 20 72 65 67 50 72 65 76 20 29 7b 0a 20 20 20  ( regPrev ){.   
2b2d9 20 69 6e 74 20 6a 31 2c 20 6a 32 3b 0a 20 20 20   int j1, j2;.   
2b2da 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
2b2db 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
2b2dc 4e 6f 74 2c 20 72 65 67 50 72 65 76 29 3b 0a 20  Not, regPrev);. 
2b2dd 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56     j2 = sqlite3V
2b2de 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2b2df 43 6f 6d 70 61 72 65 2c 20 70 49 6e 2d 3e 69 4d  Compare, pIn->iM
2b2e0 65 6d 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70  em, regPrev+1, p
2b2e1 49 6e 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20  In->nMem,.      
2b2e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2e3 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
2b2e4 4b 65 79 49 6e 66 6f 2c 20 70 34 74 79 70 65 29  KeyInfo, p4type)
2b2e5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2b2e6 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75  eAddOp3(v, OP_Ju
2b2e7 6d 70 2c 20 6a 32 2b 32 2c 20 69 43 6f 6e 74 69  mp, j2+2, iConti
2b2e8 6e 75 65 2c 20 6a 32 2b 32 29 3b 0a 20 20 20 20  nue, j2+2);.    
2b2e9 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2b2ea 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20  ere(v, j1);.    
2b2eb 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43  sqlite3ExprCodeC
2b2ec 6f 70 79 28 70 50 61 72 73 65 2c 20 70 49 6e 2d  opy(pParse, pIn-
2b2ed 3e 69 4d 65 6d 2c 20 72 65 67 50 72 65 76 2b 31  >iMem, regPrev+1
2b2ee 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  , pIn->nMem);.  
2b2ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b2f0 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2b2f1 72 2c 20 31 2c 20 72 65 67 50 72 65 76 29 3b 0a  r, 1, regPrev);.
2b2f2 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65    }.  if( pParse
2b2f3 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
2b2f4 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a  ed ) return 0;..
2b2f5 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 74 68    /* Suppress th
2b2f6 65 20 74 68 65 20 66 69 72 73 74 20 4f 46 46 53  e the first OFFS
2b2f7 45 54 20 65 6e 74 72 69 65 73 20 69 66 20 74 68  ET entries if th
2b2f8 65 72 65 20 69 73 20 61 6e 20 4f 46 46 53 45 54  ere is an OFFSET
2b2f9 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20 63   clause.  */.  c
2b2fa 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20  odeOffset(v, p, 
2b2fb 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20 73  iContinue);..  s
2b2fc 77 69 74 63 68 28 20 70 44 65 73 74 2d 3e 65 44  witch( pDest->eD
2b2fd 65 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 53 74  est ){.    /* St
2b2fe 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 61  ore the result a
2b2ff 73 20 64 61 74 61 20 75 73 69 6e 67 20 61 20 75  s data using a u
2b300 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20 2a  nique key..    *
2b301 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 54  /.    case SRT_T
2b302 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53  able:.    case S
2b303 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20  RT_EphemTab: {. 
2b304 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71       int r1 = sq
2b305 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2b306 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69  pParse);.      i
2b307 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47  nt r2 = sqlite3G
2b308 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2b309 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2b30a 65 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d  e( pDest->eDest=
2b30b 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20  =SRT_Table );.  
2b30c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 44      testcase( pD
2b30d 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2b30e 45 70 68 65 6d 54 61 62 20 29 3b 0a 20 20 20 20  EphemTab );.    
2b30f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b310 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
2b311 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  cord, pIn->iMem,
2b312 20 70 49 6e 2d 3e 6e 4d 65 6d 2c 20 72 31 29 3b   pIn->nMem, r1);
2b313 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b314 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2b315 65 77 52 6f 77 69 64 2c 20 70 44 65 73 74 2d 3e  ewRowid, pDest->
2b316 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20 20  iParm, r2);.    
2b317 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b318 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
2b319 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20  , pDest->iParm, 
2b31a 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 73  r1, r2);.      s
2b31b 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2b31c 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
2b31d 45 4e 44 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  END);.      sqli
2b31e 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2b31f 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20  g(pParse, r2);. 
2b320 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
2b321 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2b322 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72  e, r1);.      br
2b323 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e  eak;.    }..#ifn
2b324 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2b325 53 55 42 51 55 45 52 59 0a 20 20 20 20 2f 2a 20  SUBQUERY.    /* 
2b326 49 66 20 77 65 20 61 72 65 20 63 72 65 61 74 69  If we are creati
2b327 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20  ng a set for an 
2b328 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54  "expr IN (SELECT
2b329 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74   ...)" construct
2b32a 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68  ,.    ** then th
2b32b 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20  ere should be a 
2b32c 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74  single item on t
2b32d 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74 65  he stack.  Write
2b32e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 74 65   this.    ** ite
2b32f 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74 20 74  m into the set t
2b330 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75 73 20  able with bogus 
2b331 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  data..    */.   
2b332 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b   case SRT_Set: {
2b333 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 20  .      int r1;. 
2b334 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e       assert( pIn
2b335 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20 20  ->nMem==1 );.   
2b336 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d     p->affinity =
2b337 20 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74   .         sqlit
2b338 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
2b339 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30  y(p->pEList->a[0
2b33a 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d 3e  ].pExpr, pDest->
2b33b 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20 20  affinity);.     
2b33c 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2b33d 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2b33e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b33f 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
2b340 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d 3e  akeRecord, pIn->
2b341 69 4d 65 6d 2c 20 31 2c 20 72 31 2c 20 26 70 2d  iMem, 1, r1, &p-
2b342 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20  >affinity, 1);. 
2b343 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2b344 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2b345 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d  nge(pParse, pIn-
2b346 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 20  >iMem, 1);.     
2b347 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b348 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p2(v, OP_IdxInse
2b349 72 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  rt, pDest->iParm
2b34a 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
2b34b 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2b34c 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
2b34d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b34e 20 7d 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e 65   }..#if 0  /* Ne
2b34f 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 6e  ver occurs on an
2b350 20 4f 52 44 45 52 20 42 59 20 71 75 65 72 79 20   ORDER BY query 
2b351 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79  */.    /* If any
2b352 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68   row exist in th
2b353 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65  e result set, re
2b354 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 20 61  cord that fact a
2b355 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f  nd abort..    */
2b356 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78  .    case SRT_Ex
2b357 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71  ists: {.      sq
2b358 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b359 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
2b35a 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b  , pDest->iParm);
2b35b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49  .      /* The LI
2b35c 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20  MIT clause will 
2b35d 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f  terminate the lo
2b35e 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20  op for us */.   
2b35f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2b360 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49  #endif..    /* I
2b361 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61 6c  f this is a scal
2b362 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20 69  ar select that i
2b363 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70  s part of an exp
2b364 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20 20  ression, then.  
2b365 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72    ** store the r
2b366 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61 70  esults in the ap
2b367 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72 79  propriate memory
2b368 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b 20   cell and break 
2b369 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  out.    ** of th
2b36a 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20  e scan loop..   
2b36b 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
2b36c 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73  _Mem: {.      as
2b36d 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d  sert( pIn->nMem=
2b36e 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =1 );.      sqli
2b36f 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
2b370 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65  pParse, pIn->iMe
2b371 6d 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c  m, pDest->iParm,
2b372 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68   1);.      /* Th
2b373 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77  e LIMIT clause w
2b374 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66 20  ill jump out of 
2b375 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20  the loop for us 
2b376 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  */.      break;.
2b377 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
2b378 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2b379 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a  MIT_SUBQUERY */.
2b37a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75  .    /* The resu
2b37b 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 69  lts are stored i
2b37c 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  n a sequence of 
2b37d 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a  registers.    **
2b37e 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 44 65   starting at pDe
2b37f 73 74 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 6e 20  st->iMem.  Then 
2b380 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 79  the co-routine y
2b381 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ields..    */.  
2b382 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f 75    case SRT_Corou
2b383 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20 69 66  tine: {.      if
2b384 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30  ( pDest->iMem==0
2b385 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 73   ){.        pDes
2b386 74 2d 3e 69 4d 65 6d 20 3d 20 73 71 6c 69 74 65  t->iMem = sqlite
2b387 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
2b388 61 72 73 65 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29  arse, pIn->nMem)
2b389 3b 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 2d  ;.        pDest-
2b38a 3e 6e 4d 65 6d 20 3d 20 70 49 6e 2d 3e 6e 4d 65  >nMem = pIn->nMe
2b38b 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  m;.      }.     
2b38c 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2b38d 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49 6e  Move(pParse, pIn
2b38e 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 69  ->iMem, pDest->i
2b38f 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d 65 6d  Mem, pDest->nMem
2b390 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b391 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2b392 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69  _Yield, pDest->i
2b393 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 62 72 65  Parm);.      bre
2b394 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
2b395 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65  * If none of the
2b396 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 74 68 65   above, then the
2b397 20 72 65 73 75 6c 74 20 64 65 73 74 69 6e 61 74   result destinat
2b398 69 6f 6e 20 6d 75 73 74 20 62 65 0a 20 20 20 20  ion must be.    
2b399 2a 2a 20 53 52 54 5f 4f 75 74 70 75 74 2e 20 20  ** SRT_Output.  
2b39a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2b39b 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 77 69 74  never called wit
2b39c 68 20 61 6e 79 20 6f 74 68 65 72 0a 20 20 20 20  h any other.    
2b39d 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6f  ** destination o
2b39e 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e  ther than the on
2b39f 65 73 20 68 61 6e 64 6c 65 64 20 61 62 6f 76 65  es handled above
2b3a0 20 6f 72 20 53 52 54 5f 4f 75 74 70 75 74 2e 0a   or SRT_Output..
2b3a1 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f      **.    ** Fo
2b3a2 72 20 53 52 54 5f 4f 75 74 70 75 74 2c 20 72 65  r SRT_Output, re
2b3a3 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64  sults are stored
2b3a4 20 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f   in a sequence o
2b3a5 66 20 72 65 67 69 73 74 65 72 73 2e 20 20 0a 20  f registers.  . 
2b3a6 20 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 4f     ** Then the O
2b3a7 50 5f 52 65 73 75 6c 74 52 6f 77 20 6f 70 63 6f  P_ResultRow opco
2b3a8 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 61  de is used to ca
2b3a9 75 73 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  use sqlite3_step
2b3aa 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 74  () to.    ** ret
2b3ab 75 72 6e 20 74 68 65 20 6e 65 78 74 20 72 6f 77  urn the next row
2b3ac 20 6f 66 20 72 65 73 75 6c 74 2e 0a 20 20 20 20   of result..    
2b3ad 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  */.    default: 
2b3ae 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2b3af 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
2b3b0 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20 20  T_Output );.    
2b3b1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b3b2 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
2b3b3 52 6f 77 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20  Row, pIn->iMem, 
2b3b4 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  pIn->nMem);.    
2b3b5 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2b3b6 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
2b3b7 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d  (pParse, pIn->iM
2b3b8 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a  em, pIn->nMem);.
2b3b9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b3ba 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d   }.  }..  /* Jum
2b3bb 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  p to the end of 
2b3bc 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 20  the loop if the 
2b3bd 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68 65 64  LIMIT is reached
2b3be 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
2b3bf 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 73 71  iLimit ){.    sq
2b3c0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2b3c1 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d  v, OP_IfZero, p-
2b3c2 3e 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 2c  >iLimit, iBreak,
2b3c3 20 2d 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   -1);.  }..  /* 
2b3c4 47 65 6e 65 72 61 74 65 20 74 68 65 20 73 75 62  Generate the sub
2b3c5 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 0a 20  routine return. 
2b3c6 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2b3c7 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2b3c8 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73   iContinue);.  s
2b3c9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2b3ca 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72  (v, OP_Return, r
2b3cb 65 67 52 65 74 75 72 6e 29 3b 0a 0a 20 20 72 65  egReturn);..  re
2b3cc 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a  turn addr;.}../*
2b3cd 0a 2a 2a 20 41 6c 74 65 72 6e 61 74 69 76 65 20  .** Alternative 
2b3ce 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20  compound select 
2b3cf 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 66  code generator f
2b3d0 6f 72 20 63 61 73 65 73 20 77 68 65 6e 20 74 68  or cases when th
2b3d1 65 72 65 0a 2a 2a 20 69 73 20 61 6e 20 4f 52 44  ere.** is an ORD
2b3d2 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a  ER BY clause..**
2b3d3 0a 2a 2a 20 57 65 20 61 73 73 75 6d 65 20 61 20  .** We assume a 
2b3d4 71 75 65 72 79 20 6f 66 20 74 68 65 20 66 6f 6c  query of the fol
2b3d5 6c 6f 77 69 6e 67 20 66 6f 72 6d 3a 0a 2a 2a 0a  lowing form:.**.
2b3d6 2a 2a 20 20 20 20 20 20 3c 73 65 6c 65 63 74 41  **      <selectA
2b3d7 3e 20 20 3c 6f 70 65 72 61 74 6f 72 3e 20 20 3c  >  <operator>  <
2b3d8 73 65 6c 65 63 74 42 3e 20 20 4f 52 44 45 52 20  selectB>  ORDER 
2b3d9 42 59 20 3c 6f 72 64 65 72 62 79 6c 69 73 74 3e  BY <orderbylist>
2b3da 0a 2a 2a 0a 2a 2a 20 3c 6f 70 65 72 61 74 6f 72  .**.** <operator
2b3db 3e 20 69 73 20 6f 6e 65 20 6f 66 20 55 4e 49 4f  > is one of UNIO
2b3dc 4e 20 41 4c 4c 2c 20 55 4e 49 4f 4e 2c 20 45 58  N ALL, UNION, EX
2b3dd 43 45 50 54 2c 20 6f 72 20 49 4e 54 45 52 53 45  CEPT, or INTERSE
2b3de 43 54 2e 20 20 54 68 65 20 69 64 65 61 0a 2a 2a  CT.  The idea.**
2b3df 20 69 73 20 74 6f 20 63 6f 64 65 20 62 6f 74 68   is to code both
2b3e0 20 3c 73 65 6c 65 63 74 41 3e 20 61 6e 64 20 3c   <selectA> and <
2b3e1 73 65 6c 65 63 74 42 3e 20 77 69 74 68 20 74 68  selectB> with th
2b3e2 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2b3e3 65 20 61 73 0a 2a 2a 20 63 6f 2d 72 6f 75 74 69  e as.** co-routi
2b3e4 6e 65 73 2e 20 20 54 68 65 6e 20 72 75 6e 20 74  nes.  Then run t
2b3e5 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 73 20 69  he co-routines i
2b3e6 6e 20 70 61 72 61 6c 6c 65 6c 20 61 6e 64 20 6d  n parallel and m
2b3e7 65 72 67 65 20 74 68 65 20 72 65 73 75 6c 74 73  erge the results
2b3e8 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f 75 74  .** into the out
2b3e9 70 75 74 2e 20 20 49 6e 20 61 64 64 69 74 69 6f  put.  In additio
2b3ea 6e 20 74 6f 20 74 68 65 20 74 77 6f 20 63 6f 72  n to the two cor
2b3eb 6f 75 74 69 6e 65 73 20 28 63 61 6c 6c 65 64 20  outines (called 
2b3ec 73 65 6c 65 63 74 41 20 61 6e 64 0a 2a 2a 20 73  selectA and.** s
2b3ed 65 6c 65 63 74 42 29 20 74 68 65 72 65 20 61 72  electB) there ar
2b3ee 65 20 37 20 73 75 62 72 6f 75 74 69 6e 65 73 3a  e 7 subroutines:
2b3ef 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20  .**.**    outA: 
2b3f0 20 20 20 4d 6f 76 65 20 74 68 65 20 6f 75 74 70     Move the outp
2b3f1 75 74 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ut of the select
2b3f2 41 20 63 6f 72 6f 75 74 69 6e 65 20 69 6e 74 6f  A coroutine into
2b3f3 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20   the output.**  
2b3f4 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68             of th
2b3f5 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79  e compound query
2b3f6 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 75 74 42 3a  ..**.**    outB:
2b3f7 20 20 20 20 4d 6f 76 65 20 74 68 65 20 6f 75 74      Move the out
2b3f8 70 75 74 20 6f 66 20 74 68 65 20 73 65 6c 65 63  put of the selec
2b3f9 74 42 20 63 6f 72 6f 75 74 69 6e 65 20 69 6e 74  tB coroutine int
2b3fa 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20  o the output.** 
2b3fb 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
2b3fc 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72  he compound quer
2b3fd 79 2e 20 20 28 4f 6e 6c 79 20 67 65 6e 65 72 61  y.  (Only genera
2b3fe 74 65 64 20 66 6f 72 20 55 4e 49 4f 4e 20 61 6e  ted for UNION an
2b3ff 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
2b400 20 55 4e 49 4f 4e 20 41 4c 4c 2e 20 20 45 58 43   UNION ALL.  EXC
2b401 45 50 54 20 61 6e 64 20 49 4e 53 45 52 54 53 45  EPT and INSERTSE
2b402 43 54 20 6e 65 76 65 72 20 6f 75 74 70 75 74 20  CT never output 
2b403 61 20 72 6f 77 20 74 68 61 74 0a 2a 2a 20 20 20  a row that.**   
2b404 20 20 20 20 20 20 20 20 20 20 61 70 70 65 61 72            appear
2b405 73 20 6f 6e 6c 79 20 69 6e 20 42 2e 29 0a 2a 2a  s only in B.).**
2b406 0a 2a 2a 20 20 20 20 41 6c 74 42 3a 20 20 20 20  .**    AltB:    
2b407 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72  Called when ther
2b408 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62  e is data from b
2b409 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61  oth coroutines a
2b40a 6e 64 20 41 3c 42 2e 0a 2a 2a 0a 2a 2a 20 20 20  nd A<B..**.**   
2b40b 20 41 65 71 42 3a 20 20 20 20 43 61 6c 6c 65 64   AeqB:    Called
2b40c 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 64   when there is d
2b40d 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f  ata from both co
2b40e 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3d 3d  routines and A==
2b40f 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 67 74 42  B..**.**    AgtB
2b410 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e  :    Called when
2b411 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20 66   there is data f
2b412 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69  rom both corouti
2b413 6e 65 73 20 61 6e 64 20 41 3e 42 2e 0a 2a 2a 0a  nes and A>B..**.
2b414 2a 2a 20 20 20 20 45 6f 66 41 3a 20 20 20 20 43  **    EofA:    C
2b415 61 6c 6c 65 64 20 77 68 65 6e 20 64 61 74 61 20  alled when data 
2b416 69 73 20 65 78 68 61 75 73 74 65 64 20 66 72 6f  is exhausted fro
2b417 6d 20 73 65 6c 65 63 74 41 2e 0a 2a 2a 0a 2a 2a  m selectA..**.**
2b418 20 20 20 20 45 6f 66 42 3a 20 20 20 20 43 61 6c      EofB:    Cal
2b419 6c 65 64 20 77 68 65 6e 20 64 61 74 61 20 69 73  led when data is
2b41a 20 65 78 68 61 75 73 74 65 64 20 66 72 6f 6d 20   exhausted from 
2b41b 73 65 6c 65 63 74 42 2e 0a 2a 2a 0a 2a 2a 20 54  selectB..**.** T
2b41c 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
2b41d 6e 20 6f 66 20 74 68 65 20 6c 61 74 74 65 72 20  n of the latter 
2b41e 66 69 76 65 20 73 75 62 72 6f 75 74 69 6e 65 73  five subroutines
2b41f 20 64 65 70 65 6e 64 20 6f 6e 20 77 68 69 63 68   depend on which
2b420 20 0a 2a 2a 20 3c 6f 70 65 72 61 74 6f 72 3e 20   .** <operator> 
2b421 69 73 20 75 73 65 64 3a 0a 2a 2a 0a 2a 2a 0a 2a  is used:.**.**.*
2b422 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 55 4e  *             UN
2b423 49 4f 4e 20 41 4c 4c 20 20 20 20 20 20 20 20 20  ION ALL         
2b424 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20  UNION           
2b425 20 45 58 43 45 50 54 20 20 20 20 20 20 20 20 20   EXCEPT         
2b426 20 49 4e 54 45 52 53 45 43 54 0a 2a 2a 20 20 20   INTERSECT.**   
2b427 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d         ---------
2b428 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----  ----------
2b429 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d  -------  -------
2b42a 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d  -------  -------
2b42b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20  ----------.**   
2b42c 41 6c 74 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65  AltB:   outA, ne
2b42d 78 74 41 20 20 20 20 20 20 6f 75 74 41 2c 20 6e  xtA      outA, n
2b42e 65 78 74 41 20 20 20 20 20 20 20 6f 75 74 41 2c  extA       outA,
2b42f 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 6e   nextA         n
2b430 65 78 74 41 0a 2a 2a 0a 2a 2a 20 20 20 41 65 71  extA.**.**   Aeq
2b431 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41  B:   outA, nextA
2b432 20 20 20 20 20 20 20 20 20 6e 65 78 74 41 20 20           nextA  
2b433 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 41             nextA
2b434 20 20 20 20 20 20 20 20 20 6f 75 74 41 2c 20 6e           outA, n
2b435 65 78 74 41 0a 2a 2a 0a 2a 2a 20 20 20 41 67 74  extA.**.**   Agt
2b436 42 3a 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 42  B:   outB, nextB
2b437 20 20 20 20 20 20 6f 75 74 42 2c 20 6e 65 78 74        outB, next
2b438 42 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 42  B          nextB
2b439 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74              next
2b43a 42 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66 41 3a 20  B.**.**   EofA: 
2b43b 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20    outB, nextB   
2b43c 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20     outB, nextB  
2b43d 20 20 20 20 20 20 20 20 68 61 6c 74 20 20 20 20          halt    
2b43e 20 20 20 20 20 20 20 20 20 68 61 6c 74 0a 2a 2a           halt.**
2b43f 0a 2a 2a 20 20 20 45 6f 66 42 3a 20 20 20 6f 75  .**   EofB:   ou
2b440 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 6f  tA, nextA      o
2b441 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
2b442 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20   outA, nextA    
2b443 20 20 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20       halt.**.** 
2b444 49 6e 20 74 68 65 20 41 6c 74 42 2c 20 41 65 71  In the AltB, Aeq
2b445 42 2c 20 61 6e 64 20 41 67 74 42 20 73 75 62 72  B, and AgtB subr
2b446 6f 75 74 69 6e 65 73 2c 20 61 6e 20 45 4f 46 20  outines, an EOF 
2b447 6f 6e 20 41 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e  on A following n
2b448 65 78 74 41 0a 2a 2a 20 63 61 75 73 65 73 20 61  extA.** causes a
2b449 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70  n immediate jump
2b44a 20 74 6f 20 45 6f 66 41 20 61 6e 64 20 61 6e 20   to EofA and an 
2b44b 45 4f 46 20 6f 6e 20 42 20 66 6f 6c 6c 6f 77 69  EOF on B followi
2b44c 6e 67 20 6e 65 78 74 42 20 63 61 75 73 65 73 0a  ng nextB causes.
2b44d 2a 2a 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20  ** an immediate 
2b44e 6a 75 6d 70 20 74 6f 20 45 6f 66 42 2e 20 20 57  jump to EofB.  W
2b44f 69 74 68 69 6e 20 45 6f 66 41 20 61 6e 64 20 45  ithin EofA and E
2b450 6f 66 42 2c 20 61 6e 64 20 45 4f 46 20 6f 6e 20  ofB, and EOF on 
2b451 65 6e 74 72 79 20 6f 72 0a 2a 2a 20 66 6f 6c 6c  entry or.** foll
2b452 6f 77 69 6e 67 20 6e 65 78 74 58 20 63 61 75 73  owing nextX caus
2b453 65 73 20 61 20 6a 75 6d 70 20 74 6f 20 74 68 65  es a jump to the
2b454 20 65 6e 64 20 6f 66 20 74 68 65 20 73 65 6c 65   end of the sele
2b455 63 74 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a  ct processing..*
2b456 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 72  *.** Duplicate r
2b457 65 6d 6f 76 61 6c 20 69 6e 20 74 68 65 20 55 4e  emoval in the UN
2b458 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 61 6e 64  ION, EXCEPT, and
2b459 20 49 4e 54 45 52 53 45 43 54 20 63 61 73 65 73   INTERSECT cases
2b45a 20 69 73 20 68 61 6e 64 6c 65 64 0a 2a 2a 20 77   is handled.** w
2b45b 69 74 68 69 6e 20 74 68 65 20 6f 75 74 70 75 74  ithin the output
2b45c 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 54 68   subroutine.  Th
2b45d 65 20 72 65 67 50 72 65 76 20 72 65 67 69 73 74  e regPrev regist
2b45e 65 72 20 73 65 74 20 68 6f 6c 64 73 20 74 68 65  er set holds the
2b45f 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 6f   previously.** o
2b460 75 74 70 75 74 20 76 61 6c 75 65 2e 20 20 41 20  utput value.  A 
2b461 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d 61  comparison is ma
2b462 64 65 20 61 67 61 69 6e 73 74 20 74 68 69 73 20  de against this 
2b463 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 6f 75  value and the ou
2b464 74 70 75 74 0a 2a 2a 20 69 73 20 73 6b 69 70 70  tput.** is skipp
2b465 65 64 20 69 66 20 74 68 65 20 6e 65 78 74 20 72  ed if the next r
2b466 65 73 75 6c 74 73 20 77 6f 75 6c 64 20 62 65 20  esults would be 
2b467 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
2b468 70 72 65 76 69 6f 75 73 2e 0a 2a 2a 0a 2a 2a 20  previous..**.** 
2b469 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  The implementati
2b46a 6f 6e 20 70 6c 61 6e 20 69 73 20 74 6f 20 69 6d  on plan is to im
2b46b 70 6c 65 6d 65 6e 74 20 74 68 65 20 74 77 6f 20  plement the two 
2b46c 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 73  coroutines and s
2b46d 65 76 65 6e 0a 2a 2a 20 73 75 62 72 6f 75 74 69  even.** subrouti
2b46e 6e 65 73 20 66 69 72 73 74 2c 20 74 68 65 6e 20  nes first, then 
2b46f 70 75 74 20 74 68 65 20 63 6f 6e 74 72 6f 6c 20  put the control 
2b470 6c 6f 67 69 63 20 61 74 20 74 68 65 20 62 6f 74  logic at the bot
2b471 74 6f 6d 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a  tom.  Like this:
2b472 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
2b473 67 6f 74 6f 20 49 6e 69 74 0a 2a 2a 20 20 20 20  goto Init.**    
2b474 20 63 6f 41 3a 20 63 6f 72 6f 75 74 69 6e 65 20   coA: coroutine 
2b475 66 6f 72 20 6c 65 66 74 20 71 75 65 72 79 20 28  for left query (
2b476 41 29 0a 2a 2a 20 20 20 20 20 63 6f 42 3a 20 63  A).**     coB: c
2b477 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 69 67  oroutine for rig
2b478 68 74 20 71 75 65 72 79 20 28 42 29 0a 2a 2a 20  ht query (B).** 
2b479 20 20 20 6f 75 74 41 3a 20 6f 75 74 70 75 74 20     outA: output 
2b47a 6f 6e 65 20 72 6f 77 20 6f 66 20 41 0a 2a 2a 20  one row of A.** 
2b47b 20 20 20 6f 75 74 42 3a 20 6f 75 74 70 75 74 20     outB: output 
2b47c 6f 6e 65 20 72 6f 77 20 6f 66 20 42 20 28 55 4e  one row of B (UN
2b47d 49 4f 4e 20 61 6e 64 20 55 4e 49 4f 4e 20 41 4c  ION and UNION AL
2b47e 4c 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 20 45 6f  L only).**    Eo
2b47f 66 41 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 45 6f  fA: ....**    Eo
2b480 66 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41 6c  fB: ....**    Al
2b481 74 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41 65  tB: ....**    Ae
2b482 71 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41 67  qB: ....**    Ag
2b483 74 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 49 6e  tB: ....**    In
2b484 69 74 3a 20 69 6e 69 74 69 61 6c 69 7a 65 20 63  it: initialize c
2b485 6f 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65  oroutine registe
2b486 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 79  rs.**          y
2b487 69 65 6c 64 20 63 6f 41 0a 2a 2a 20 20 20 20 20  ield coA.**     
2b488 20 20 20 20 20 69 66 20 65 6f 66 28 41 29 20 67       if eof(A) g
2b489 6f 74 6f 20 45 6f 66 41 0a 2a 2a 20 20 20 20 20  oto EofA.**     
2b48a 20 20 20 20 20 79 69 65 6c 64 20 63 6f 42 0a 2a       yield coB.*
2b48b 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 65 6f  *          if eo
2b48c 66 28 42 29 20 67 6f 74 6f 20 45 6f 66 42 0a 2a  f(B) goto EofB.*
2b48d 2a 20 20 20 20 43 6d 70 72 3a 20 43 6f 6d 70 61  *    Cmpr: Compa
2b48e 72 65 20 41 2c 20 42 0a 2a 2a 20 20 20 20 20 20  re A, B.**      
2b48f 20 20 20 20 4a 75 6d 70 20 41 6c 74 42 2c 20 41      Jump AltB, A
2b490 65 71 42 2c 20 41 67 74 42 0a 2a 2a 20 20 20 20  eqB, AgtB.**    
2b491 20 45 6e 64 3a 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20   End: ....**.** 
2b492 57 65 20 63 61 6c 6c 20 41 6c 74 42 2c 20 41 65  We call AltB, Ae
2b493 71 42 2c 20 41 67 74 42 2c 20 45 6f 66 41 2c 20  qB, AgtB, EofA, 
2b494 61 6e 64 20 45 6f 66 42 20 22 73 75 62 72 6f 75  and EofB "subrou
2b495 74 69 6e 65 73 22 20 62 75 74 20 74 68 65 79 20  tines" but they 
2b496 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 63 74 75 61  are not.** actua
2b497 6c 6c 79 20 63 61 6c 6c 65 64 20 75 73 69 6e 67  lly called using
2b498 20 47 6f 73 75 62 20 61 6e 64 20 74 68 65 79 20   Gosub and they 
2b499 64 6f 20 6e 6f 74 20 52 65 74 75 72 6e 2e 20 20  do not Return.  
2b49a 45 6f 66 41 20 61 6e 64 20 45 6f 66 42 20 6c 6f  EofA and EofB lo
2b49b 6f 70 0a 2a 2a 20 75 6e 74 69 6c 20 61 6c 6c 20  op.** until all 
2b49c 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74 65  data is exhauste
2b49d 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74  d then jump to t
2b49e 68 65 20 22 65 6e 64 22 20 6c 61 62 65 2e 20 20  he "end" labe.  
2b49f 41 6c 74 42 2c 20 41 65 71 42 2c 0a 2a 2a 20 61  AltB, AeqB,.** a
2b4a0 6e 64 20 41 67 74 42 20 6a 75 6d 70 20 74 6f 20  nd AgtB jump to 
2b4a1 65 69 74 68 65 72 20 4c 32 20 6f 72 20 74 6f 20  either L2 or to 
2b4a2 6f 6e 65 20 6f 66 20 45 6f 66 41 20 6f 72 20 45  one of EofA or E
2b4a3 6f 66 42 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ofB..*/.#ifndef 
2b4a4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
2b4a5 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 73 74 61 74  OUND_SELECT.stat
2b4a6 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65  ic int multiSele
2b4a7 63 74 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72  ctOrderBy(.  Par
2b4a8 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2b4a9 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2b4aa 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
2b4ab 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
2b4ac 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f   /* The right-mo
2b4ad 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f  st of SELECTs to
2b4ae 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53   be coded */.  S
2b4af 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74  electDest *pDest
2b4b0 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20       /* What to 
2b4b1 64 6f 20 77 69 74 68 20 71 75 65 72 79 20 72 65  do with query re
2b4b2 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  sults */.){.  in
2b4b3 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
2b4b4 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2b4b5 74 65 72 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ters */.  Select
2b4b6 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20   *pPrior;       
2b4b7 2f 2a 20 41 6e 6f 74 68 65 72 20 53 45 4c 45 43  /* Another SELEC
2b4b8 54 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  T immediately to
2b4b9 20 6f 75 72 20 6c 65 66 74 20 2a 2f 0a 20 20 56   our left */.  V
2b4ba 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
2b4bb 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2b4bc 20 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 44   code to this VD
2b4bd 42 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  BE */.  SelectDe
2b4be 73 74 20 64 65 73 74 41 3b 20 20 20 20 20 2f 2a  st destA;     /*
2b4bf 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72   Destination for
2b4c0 20 63 6f 72 6f 75 74 69 6e 65 20 41 20 2a 2f 0a   coroutine A */.
2b4c1 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73    SelectDest des
2b4c2 74 42 3b 20 20 20 20 20 2f 2a 20 44 65 73 74 69  tB;     /* Desti
2b4c3 6e 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f 75  nation for corou
2b4c4 74 69 6e 65 20 42 20 2a 2f 0a 20 20 69 6e 74 20  tine B */.  int 
2b4c5 72 65 67 41 64 64 72 41 3b 20 20 20 20 20 20 20  regAddrA;       
2b4c6 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67    /* Address reg
2b4c7 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74  ister for select
2b4c8 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a  -A coroutine */.
2b4c9 20 20 69 6e 74 20 72 65 67 45 6f 66 41 3b 20 20    int regEofA;  
2b4ca 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 20          /* Flag 
2b4cb 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e  to indicate when
2b4cc 20 73 65 6c 65 63 74 2d 41 20 69 73 20 63 6f 6d   select-A is com
2b4cd 70 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 72  plete */.  int r
2b4ce 65 67 41 64 64 72 42 3b 20 20 20 20 20 20 20 20  egAddrB;        
2b4cf 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 69   /* Address regi
2b4d0 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74 2d  ster for select-
2b4d1 42 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20  B coroutine */. 
2b4d2 20 69 6e 74 20 72 65 67 45 6f 66 42 3b 20 20 20   int regEofB;   
2b4d3 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 20 74         /* Flag t
2b4d4 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20  o indicate when 
2b4d5 73 65 6c 65 63 74 2d 42 20 69 73 20 63 6f 6d 70  select-B is comp
2b4d6 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64  lete */.  int ad
2b4d7 64 72 53 65 6c 65 63 74 41 3b 20 20 20 20 20 20  drSelectA;      
2b4d8 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
2b4d9 65 20 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f 75  e select-A corou
2b4da 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64  tine */.  int ad
2b4db 64 72 53 65 6c 65 63 74 42 3b 20 20 20 20 20 20  drSelectB;      
2b4dc 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
2b4dd 65 20 73 65 6c 65 63 74 2d 42 20 63 6f 72 6f 75  e select-B corou
2b4de 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  tine */.  int re
2b4df 67 4f 75 74 41 3b 20 20 20 20 20 20 20 20 20 20  gOutA;          
2b4e0 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 69 73  /* Address regis
2b4e1 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75 74 70  ter for the outp
2b4e2 75 74 2d 41 20 73 75 62 72 6f 75 74 69 6e 65 20  ut-A subroutine 
2b4e3 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 74 42  */.  int regOutB
2b4e4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  ;          /* Ad
2b4e5 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66  dress register f
2b4e6 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2d 42 20  or the output-B 
2b4e7 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
2b4e8 69 6e 74 20 61 64 64 72 4f 75 74 41 3b 20 20 20  int addrOutA;   
2b4e9 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2b4ea 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 2d 41   of the output-A
2b4eb 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
2b4ec 20 69 6e 74 20 61 64 64 72 4f 75 74 42 20 3d 20   int addrOutB = 
2b4ed 30 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  0;     /* Addres
2b4ee 73 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 2d  s of the output-
2b4ef 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  B subroutine */.
2b4f0 20 20 69 6e 74 20 61 64 64 72 45 6f 66 41 3b 20    int addrEofA; 
2b4f1 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2b4f2 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ss of the select
2b4f3 2d 41 2d 65 78 68 61 75 73 74 65 64 20 73 75 62  -A-exhausted sub
2b4f4 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2b4f5 20 61 64 64 72 45 6f 66 42 3b 20 20 20 20 20 20   addrEofB;      
2b4f6 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
2b4f7 20 74 68 65 20 73 65 6c 65 63 74 2d 42 2d 65 78   the select-B-ex
2b4f8 68 61 75 73 74 65 64 20 73 75 62 72 6f 75 74 69  hausted subrouti
2b4f9 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
2b4fa 41 6c 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  AltB;         /*
2b4fb 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2b4fc 41 3c 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  A<B subroutine *
2b4fd 2f 0a 20 20 69 6e 74 20 61 64 64 72 41 65 71 42  /.  int addrAeqB
2b4fe 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64  ;         /* Add
2b4ff 72 65 73 73 20 6f 66 20 74 68 65 20 41 3d 3d 42  ress of the A==B
2b500 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
2b501 20 69 6e 74 20 61 64 64 72 41 67 74 42 3b 20 20   int addrAgtB;  
2b502 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2b503 73 20 6f 66 20 74 68 65 20 41 3e 42 20 73 75 62  s of the A>B sub
2b504 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2b505 20 72 65 67 4c 69 6d 69 74 41 3b 20 20 20 20 20   regLimitA;     
2b506 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67 69     /* Limit regi
2b507 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74 2d  ster for select-
2b508 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 69  A */.  int regLi
2b509 6d 69 74 42 3b 20 20 20 20 20 20 20 20 2f 2a 20  mitB;        /* 
2b50a 4c 69 6d 69 74 20 72 65 67 69 73 74 65 72 20 66  Limit register f
2b50b 6f 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a 20  or select-A */. 
2b50c 20 69 6e 74 20 72 65 67 50 72 65 76 3b 20 20 20   int regPrev;   
2b50d 20 20 20 20 20 20 20 2f 2a 20 41 20 72 61 6e 67         /* A rang
2b50e 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 74  e of registers t
2b50f 6f 20 68 6f 6c 64 20 70 72 65 76 69 6f 75 73 20  o hold previous 
2b510 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  output */.  int 
2b511 73 61 76 65 64 4c 69 6d 69 74 3b 20 20 20 20 20  savedLimit;     
2b512 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65    /* Saved value
2b513 20 6f 66 20 70 2d 3e 69 4c 69 6d 69 74 20 2a 2f   of p->iLimit */
2b514 0a 20 20 69 6e 74 20 73 61 76 65 64 4f 66 66 73  .  int savedOffs
2b515 65 74 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65  et;      /* Save
2b516 64 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4f  d value of p->iO
2b517 66 66 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6c  ffset */.  int l
2b518 61 62 65 6c 43 6d 70 72 3b 20 20 20 20 20 20 20  abelCmpr;       
2b519 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68   /* Label for th
2b51a 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6d  e start of the m
2b51b 65 72 67 65 20 61 6c 67 6f 72 69 74 68 6d 20 2a  erge algorithm *
2b51c 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 45 6e 64  /.  int labelEnd
2b51d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62  ;         /* Lab
2b51e 65 6c 20 66 6f 72 20 74 68 65 20 65 6e 64 20 6f  el for the end o
2b51f 66 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 53 45  f the overall SE
2b520 4c 45 43 54 20 73 74 6d 74 20 2a 2f 0a 20 20 69  LECT stmt */.  i
2b521 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20  nt j1;          
2b522 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 69 6e 73       /* Jump ins
2b523 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20 67  tructions that g
2b524 65 74 20 72 65 74 61 72 67 65 74 74 65 64 20 2a  et retargetted *
2b525 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20  /.  int op;     
2b526 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
2b527 20 6f 66 20 54 4b 5f 41 4c 4c 2c 20 54 4b 5f 55   of TK_ALL, TK_U
2b528 4e 49 4f 4e 2c 20 54 4b 5f 45 58 43 45 50 54 2c  NION, TK_EXCEPT,
2b529 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 2a 2f   TK_INTERSECT */
2b52a 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
2b52b 44 75 70 20 3d 20 30 3b 20 2f 2a 20 43 6f 6d 70  Dup = 0; /* Comp
2b52c 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  arison informati
2b52d 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65  on for duplicate
2b52e 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a 20 20 4b 65   removal */.  Ke
2b52f 79 49 6e 66 6f 20 2a 70 4b 65 79 4d 65 72 67 65  yInfo *pKeyMerge
2b530 3b 20 20 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f  ;   /* Compariso
2b531 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  n information fo
2b532 72 20 6d 65 72 67 69 6e 67 20 72 6f 77 73 20 2a  r merging rows *
2b533 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
2b534 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2b535 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2b536 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2b537 70 4f 72 64 65 72 42 79 3b 20 20 20 2f 2a 20 54  pOrderBy;   /* T
2b538 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2b539 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64  se */.  int nOrd
2b53a 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  erBy;         /*
2b53b 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
2b53c 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59   in the ORDER BY
2b53d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
2b53e 20 2a 61 50 65 72 6d 75 74 65 3b 20 20 20 20 20   *aPermute;     
2b53f 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72     /* Mapping fr
2b540 6f 6d 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  om ORDER BY term
2b541 73 20 74 6f 20 72 65 73 75 6c 74 20 73 65 74 20  s to result set 
2b542 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 61 73  columns */..  as
2b543 73 65 72 74 28 20 70 2d 3e 70 4f 72 64 65 72 42  sert( p->pOrderB
2b544 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  y!=0 );.  assert
2b545 28 20 70 4b 65 79 44 75 70 3d 3d 30 20 29 3b 20  ( pKeyDup==0 ); 
2b546 2f 2a 20 22 4d 61 6e 61 67 65 64 22 20 63 6f 64  /* "Managed" cod
2b547 65 20 6e 65 65 64 73 20 74 68 69 73 2e 20 20 54  e needs this.  T
2b548 69 63 6b 65 74 20 23 33 33 38 32 2e 20 2a 2f 0a  icket #3382. */.
2b549 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2b54a 62 3b 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d  b;.  v = pParse-
2b54b 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65 72 74  >pVdbe;.  assert
2b54c 28 20 76 21 3d 30 20 29 3b 20 20 20 20 20 20 20  ( v!=0 );       
2b54d 2f 2a 20 41 6c 72 65 61 64 79 20 74 68 72 6f 77  /* Already throw
2b54e 6e 20 74 68 65 20 65 72 72 6f 72 20 69 66 20 56  n the error if V
2b54f 44 42 45 20 61 6c 6c 6f 63 20 66 61 69 6c 65 64  DBE alloc failed
2b550 20 2a 2f 0a 20 20 6c 61 62 65 6c 45 6e 64 20 3d   */.  labelEnd =
2b551 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2b552 4c 61 62 65 6c 28 76 29 3b 0a 20 20 6c 61 62 65  Label(v);.  labe
2b553 6c 43 6d 70 72 20 3d 20 73 71 6c 69 74 65 33 56  lCmpr = sqlite3V
2b554 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2b555 0a 0a 0a 20 20 2f 2a 20 50 61 74 63 68 20 75 70  ...  /* Patch up
2b556 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2b557 61 75 73 65 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d  ause.  */.  op =
2b558 20 70 2d 3e 6f 70 3b 20 20 0a 20 20 70 50 72 69   p->op;  .  pPri
2b559 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a  or = p->pPrior;.
2b55a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72    assert( pPrior
2b55b 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b  ->pOrderBy==0 );
2b55c 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d  .  pOrderBy = p-
2b55d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 61 73 73  >pOrderBy;.  ass
2b55e 65 72 74 28 20 70 4f 72 64 65 72 42 79 20 29 3b  ert( pOrderBy );
2b55f 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f  .  nOrderBy = pO
2b560 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 0a  rderBy->nExpr;..
2b561 20 20 2f 2a 20 46 6f 72 20 6f 70 65 72 61 74 6f    /* For operato
2b562 72 73 20 6f 74 68 65 72 20 74 68 61 6e 20 55 4e  rs other than UN
2b563 49 4f 4e 20 41 4c 4c 20 77 65 20 68 61 76 65 20  ION ALL we have 
2b564 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
2b565 74 0a 20 20 2a 2a 20 74 68 65 20 4f 52 44 45 52  t.  ** the ORDER
2b566 20 42 59 20 63 6c 61 75 73 65 20 63 6f 76 65 72   BY clause cover
2b567 73 20 65 76 65 72 79 20 74 65 72 6d 20 6f 66 20  s every term of 
2b568 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20  the result set. 
2b569 20 41 64 64 0a 20 20 2a 2a 20 74 65 72 6d 73 20   Add.  ** terms 
2b56a 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  to the ORDER BY 
2b56b 63 6c 61 75 73 65 20 61 73 20 6e 65 63 65 73 73  clause as necess
2b56c 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ary..  */.  if( 
2b56d 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20  op!=TK_ALL ){.  
2b56e 20 20 66 6f 72 28 69 3d 31 3b 20 64 62 2d 3e 6d    for(i=1; db->m
2b56f 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26  allocFailed==0 &
2b570 26 20 69 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e  & i<=p->pEList->
2b571 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
2b572 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
2b573 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
2b574 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70        for(j=0, p
2b575 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61  Item=pOrderBy->a
2b576 3b 20 6a 3c 6e 4f 72 64 65 72 42 79 3b 20 6a 2b  ; j<nOrderBy; j+
2b577 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
2b578 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74       assert( pIt
2b579 65 6d 2d 3e 69 43 6f 6c 3e 30 20 29 3b 0a 20 20  em->iCol>0 );.  
2b57a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
2b57b 3e 69 43 6f 6c 3d 3d 69 20 29 20 62 72 65 61 6b  >iCol==i ) break
2b57c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2b57d 69 66 28 20 6a 3d 3d 6e 4f 72 64 65 72 42 79 20  if( j==nOrderBy 
2b57e 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20  ){.        Expr 
2b57f 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45  *pNew = sqlite3E
2b580 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45 47  xpr(db, TK_INTEG
2b581 45 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ER, 0);.        
2b582 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65  if( pNew==0 ) re
2b583 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2b584 4d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d  M;.        pNew-
2b585 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74  >flags |= EP_Int
2b586 56 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 70  Value;.        p
2b587 4e 65 77 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20  New->u.iValue = 
2b588 69 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 64 65  i;.        pOrde
2b589 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  rBy = sqlite3Exp
2b58a 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2b58b 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 4e  se, pOrderBy, pN
2b58c 65 77 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 72  ew);.        pOr
2b58d 64 65 72 42 79 2d 3e 61 5b 6e 4f 72 64 65 72 42  derBy->a[nOrderB
2b58e 79 2b 2b 5d 2e 69 43 6f 6c 20 3d 20 28 75 31 36  y++].iCol = (u16
2b58f 29 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  )i;.      }.    
2b590 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70  }.  }..  /* Comp
2b591 75 74 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ute the comparis
2b592 6f 6e 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61  on permutation a
2b593 6e 64 20 6b 65 79 69 6e 66 6f 20 74 68 61 74 20  nd keyinfo that 
2b594 69 73 20 75 73 65 64 20 77 69 74 68 0a 20 20 2a  is used with.  *
2b595 2a 20 74 68 65 20 70 65 72 6d 75 74 61 74 69 6f  * the permutatio
2b596 6e 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  n used to determ
2b597 69 6e 65 20 69 66 20 74 68 65 20 6e 65 78 74 0a  ine if the next.
2b598 20 20 2a 2a 20 72 6f 77 20 6f 66 20 72 65 73 75    ** row of resu
2b599 6c 74 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20 73  lts comes from s
2b59a 65 6c 65 63 74 41 20 6f 72 20 73 65 6c 65 63 74  electA or select
2b59b 42 2e 20 20 41 6c 73 6f 20 61 64 64 20 65 78 70  B.  Also add exp
2b59c 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61  licit.  ** colla
2b59d 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 4f 52 44  tions to the ORD
2b59e 45 52 20 42 59 20 63 6c 61 75 73 65 20 74 65 72  ER BY clause ter
2b59f 6d 73 20 73 6f 20 74 68 61 74 20 77 68 65 6e 20  ms so that when 
2b5a0 74 68 65 20 73 75 62 71 75 65 72 69 65 73 0a 20  the subqueries. 
2b5a1 20 2a 2a 20 74 6f 20 74 68 65 20 72 69 67 68 74   ** to the right
2b5a2 20 61 6e 64 20 74 68 65 20 6c 65 66 74 20 61 72   and the left ar
2b5a3 65 20 65 76 61 6c 75 61 74 65 64 2c 20 74 68 65  e evaluated, the
2b5a4 79 20 75 73 65 20 74 68 65 20 63 6f 72 72 65 63  y use the correc
2b5a5 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e  t.  ** collation
2b5a6 2e 0a 20 20 2a 2f 0a 20 20 61 50 65 72 6d 75 74  ..  */.  aPermut
2b5a7 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  e = sqlite3DbMal
2b5a8 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
2b5a9 66 28 69 6e 74 29 2a 6e 4f 72 64 65 72 42 79 29  f(int)*nOrderBy)
2b5aa 3b 0a 20 20 69 66 28 20 61 50 65 72 6d 75 74 65  ;.  if( aPermute
2b5ab 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45   ){.    struct E
2b5ac 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
2b5ad 74 65 6d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  tem;.    for(i=0
2b5ae 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79  , pItem=pOrderBy
2b5af 2d 3e 61 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b  ->a; i<nOrderBy;
2b5b0 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
2b5b1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
2b5b2 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20 20 26 26 20  tem->iCol>0  && 
2b5b3 70 49 74 65 6d 2d 3e 69 43 6f 6c 3c 3d 70 2d 3e  pItem->iCol<=p->
2b5b4 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b  pEList->nExpr );
2b5b5 0a 20 20 20 20 20 20 61 50 65 72 6d 75 74 65 5b  .      aPermute[
2b5b6 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 69 43 6f 6c  i] = pItem->iCol
2b5b7 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   - 1;.    }.    
2b5b8 70 4b 65 79 4d 65 72 67 65 20 3d 0a 20 20 20 20  pKeyMerge =.    
2b5b9 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f    sqlite3DbMallo
2b5ba 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2b5bb 2a 70 4b 65 79 4d 65 72 67 65 29 2b 6e 4f 72 64  *pKeyMerge)+nOrd
2b5bc 65 72 42 79 2a 28 73 69 7a 65 6f 66 28 43 6f 6c  erBy*(sizeof(Col
2b5bd 6c 53 65 71 2a 29 2b 31 29 29 3b 0a 20 20 20 20  lSeq*)+1));.    
2b5be 69 66 28 20 70 4b 65 79 4d 65 72 67 65 20 29 7b  if( pKeyMerge ){
2b5bf 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65  .      pKeyMerge
2b5c0 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28  ->aSortOrder = (
2b5c1 75 38 2a 29 26 70 4b 65 79 4d 65 72 67 65 2d 3e  u8*)&pKeyMerge->
2b5c2 61 43 6f 6c 6c 5b 6e 4f 72 64 65 72 42 79 5d 3b  aColl[nOrderBy];
2b5c3 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65  .      pKeyMerge
2b5c4 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
2b5c5 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 20 20  nOrderBy;.      
2b5c6 70 4b 65 79 4d 65 72 67 65 2d 3e 65 6e 63 20 3d  pKeyMerge->enc =
2b5c7 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 20 20   ENC(db);.      
2b5c8 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65  for(i=0; i<nOrde
2b5c9 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  rBy; i++){.     
2b5ca 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
2b5cb 6c 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20  l;.        Expr 
2b5cc 2a 70 54 65 72 6d 20 3d 20 70 4f 72 64 65 72 42  *pTerm = pOrderB
2b5cd 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  y->a[i].pExpr;. 
2b5ce 20 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d         if( pTerm
2b5cf 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
2b5d0 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20  Collate ){.     
2b5d1 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 54 65       pColl = pTe
2b5d2 72 6d 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  rm->pColl;.     
2b5d3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2b5d4 20 20 20 20 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74      pColl = mult
2b5d5 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70  iSelectCollSeq(p
2b5d6 50 61 72 73 65 2c 20 70 2c 20 61 50 65 72 6d 75  Parse, p, aPermu
2b5d7 74 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20  te[i]);.        
2b5d8 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c    pTerm->flags |
2b5d9 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b  = EP_ExpCollate;
2b5da 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d  .          pTerm
2b5db 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b  ->pColl = pColl;
2b5dc 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2b5dd 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43     pKeyMerge->aC
2b5de 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a  oll[i] = pColl;.
2b5df 20 20 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67          pKeyMerg
2b5e0 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  e->aSortOrder[i]
2b5e1 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69   = pOrderBy->a[i
2b5e2 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  ].sortOrder;.   
2b5e3 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
2b5e4 73 65 7b 0a 20 20 20 20 70 4b 65 79 4d 65 72 67  se{.    pKeyMerg
2b5e5 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e = 0;.  }..  /*
2b5e6 20 52 65 61 74 74 61 63 68 20 74 68 65 20 4f 52   Reattach the OR
2b5e7 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74 6f  DER BY clause to
2b5e8 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a 2f   the query..  */
2b5e9 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d  .  p->pOrderBy =
2b5ea 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 50 72   pOrderBy;.  pPr
2b5eb 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20  ior->pOrderBy = 
2b5ec 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2b5ed 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  up(pParse->db, p
2b5ee 4f 72 64 65 72 42 79 2c 20 30 29 3b 0a 0a 20 20  OrderBy, 0);..  
2b5ef 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 72 61  /* Allocate a ra
2b5f0 6e 67 65 20 6f 66 20 74 65 6d 70 6f 72 61 72 79  nge of temporary
2b5f1 20 72 65 67 69 73 74 65 72 73 20 61 6e 64 20 74   registers and t
2b5f2 68 65 20 4b 65 79 49 6e 66 6f 20 6e 65 65 64 65  he KeyInfo neede
2b5f3 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6c  d.  ** for the l
2b5f4 6f 67 69 63 20 74 68 61 74 20 72 65 6d 6f 76 65  ogic that remove
2b5f5 73 20 64 75 70 6c 69 63 61 74 65 20 72 65 73 75  s duplicate resu
2b5f6 6c 74 20 72 6f 77 73 20 77 68 65 6e 20 74 68 65  lt rows when the
2b5f7 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69  .  ** operator i
2b5f8 73 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c  s UNION, EXCEPT,
2b5f9 20 6f 72 20 49 4e 54 45 52 53 45 43 54 20 28 62   or INTERSECT (b
2b5fa 75 74 20 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c 4c  ut not UNION ALL
2b5fb 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70  )..  */.  if( op
2b5fc 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20  ==TK_ALL ){.    
2b5fd 72 65 67 50 72 65 76 20 3d 20 30 3b 0a 20 20 7d  regPrev = 0;.  }
2b5fe 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 45  else{.    int nE
2b5ff 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d  xpr = p->pEList-
2b600 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65  >nExpr;.    asse
2b601 72 74 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e 45  rt( nOrderBy>=nE
2b602 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  xpr || db->mallo
2b603 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72  cFailed );.    r
2b604 65 67 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33  egPrev = sqlite3
2b605 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
2b606 72 73 65 2c 20 6e 45 78 70 72 2b 31 29 3b 0a 20  rse, nExpr+1);. 
2b607 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b608 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2b609 65 72 2c 20 30 2c 20 72 65 67 50 72 65 76 29 3b  er, 0, regPrev);
2b60a 0a 20 20 20 20 70 4b 65 79 44 75 70 20 3d 20 73  .    pKeyDup = s
2b60b 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2b60c 72 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20  ro(db,.         
2b60d 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28           sizeof(
2b60e 2a 70 4b 65 79 44 75 70 29 20 2b 20 6e 45 78 70  *pKeyDup) + nExp
2b60f 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65  r*(sizeof(CollSe
2b610 71 2a 29 2b 31 29 20 29 3b 0a 20 20 20 20 69 66  q*)+1) );.    if
2b611 28 20 70 4b 65 79 44 75 70 20 29 7b 0a 20 20 20  ( pKeyDup ){.   
2b612 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f 72     pKeyDup->aSor
2b613 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70  tOrder = (u8*)&p
2b614 4b 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 6e 45  KeyDup->aColl[nE
2b615 78 70 72 5d 3b 0a 20 20 20 20 20 20 70 4b 65 79  xpr];.      pKey
2b616 44 75 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75  Dup->nField = (u
2b617 31 36 29 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  16)nExpr;.      
2b618 70 4b 65 79 44 75 70 2d 3e 65 6e 63 20 3d 20 45  pKeyDup->enc = E
2b619 4e 43 28 64 62 29 3b 0a 20 20 20 20 20 20 66 6f  NC(db);.      fo
2b61a 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20  r(i=0; i<nExpr; 
2b61b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 4b  i++){.        pK
2b61c 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 69 5d 20  eyDup->aColl[i] 
2b61d 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c  = multiSelectCol
2b61e 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2c 20  lSeq(pParse, p, 
2b61f 69 29 3b 0a 20 20 20 20 20 20 20 20 70 4b 65 79  i);.        pKey
2b620 44 75 70 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Dup->aSortOrder[
2b621 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  i] = 0;.      }.
2b622 20 20 20 20 7d 0a 20 20 7d 0a 20 0a 20 20 2f 2a      }.  }. .  /*
2b623 20 53 65 70 61 72 61 74 65 20 74 68 65 20 6c 65   Separate the le
2b624 66 74 20 61 6e 64 20 74 68 65 20 72 69 67 68 74  ft and the right
2b625 20 71 75 65 72 79 20 66 72 6f 6d 20 6f 6e 65 20   query from one 
2b626 61 6e 6f 74 68 65 72 0a 20 20 2a 2f 0a 20 20 70  another.  */.  p
2b627 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20  ->pPrior = 0;.  
2b628 70 50 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f  pPrior->pRightmo
2b629 73 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  st = 0;.  sqlite
2b62a 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f  3ResolveOrderGro
2b62b 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 2c 20  upBy(pParse, p, 
2b62c 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52  p->pOrderBy, "OR
2b62d 44 45 52 22 29 3b 0a 20 20 69 66 28 20 70 50 72  DER");.  if( pPr
2b62e 69 6f 72 2d 3e 70 50 72 69 6f 72 3d 3d 30 20 29  ior->pPrior==0 )
2b62f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  {.    sqlite3Res
2b630 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
2b631 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
2b632 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42   pPrior->pOrderB
2b633 79 2c 20 22 4f 52 44 45 52 22 29 3b 0a 20 20 7d  y, "ORDER");.  }
2b634 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74  ..  /* Compute t
2b635 68 65 20 6c 69 6d 69 74 20 72 65 67 69 73 74 65  he limit registe
2b636 72 73 20 2a 2f 0a 20 20 63 6f 6d 70 75 74 65 4c  rs */.  computeL
2b637 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50  imitRegisters(pP
2b638 61 72 73 65 2c 20 70 2c 20 6c 61 62 65 6c 45 6e  arse, p, labelEn
2b639 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69  d);.  if( p->iLi
2b63a 6d 69 74 20 26 26 20 6f 70 3d 3d 54 4b 5f 41 4c  mit && op==TK_AL
2b63b 4c 20 29 7b 0a 20 20 20 20 72 65 67 4c 69 6d 69  L ){.    regLimi
2b63c 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  tA = ++pParse->n
2b63d 4d 65 6d 3b 0a 20 20 20 20 72 65 67 4c 69 6d 69  Mem;.    regLimi
2b63e 74 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  tB = ++pParse->n
2b63f 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
2b640 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b641 5f 43 6f 70 79 2c 20 70 2d 3e 69 4f 66 66 73 65  _Copy, p->iOffse
2b642 74 20 3f 20 70 2d 3e 69 4f 66 66 73 65 74 2b 31  t ? p->iOffset+1
2b643 20 3a 20 70 2d 3e 69 4c 69 6d 69 74 2c 0a 20 20   : p->iLimit,.  
2b644 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b645 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b646 72 65 67 4c 69 6d 69 74 41 29 3b 0a 20 20 20 20  regLimitA);.    
2b647 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b648 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65  2(v, OP_Copy, re
2b649 67 4c 69 6d 69 74 41 2c 20 72 65 67 4c 69 6d 69  gLimitA, regLimi
2b64a 74 42 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  tB);.  }else{.  
2b64b 20 20 72 65 67 4c 69 6d 69 74 41 20 3d 20 72 65    regLimitA = re
2b64c 67 4c 69 6d 69 74 42 20 3d 20 30 3b 0a 20 20 7d  gLimitB = 0;.  }
2b64d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2b64e 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d  lete(db, p->pLim
2b64f 69 74 29 3b 0a 20 20 70 2d 3e 70 4c 69 6d 69 74  it);.  p->pLimit
2b650 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45   = 0;.  sqlite3E
2b651 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
2b652 3e 70 4f 66 66 73 65 74 29 3b 0a 20 20 70 2d 3e  >pOffset);.  p->
2b653 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 0a 20 20  pOffset = 0;..  
2b654 72 65 67 41 64 64 72 41 20 3d 20 2b 2b 70 50 61  regAddrA = ++pPa
2b655 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67  rse->nMem;.  reg
2b656 45 6f 66 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d  EofA = ++pParse-
2b657 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 41 64 64 72  >nMem;.  regAddr
2b658 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  B = ++pParse->nM
2b659 65 6d 3b 0a 20 20 72 65 67 45 6f 66 42 20 3d 20  em;.  regEofB = 
2b65a 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2b65b 20 20 72 65 67 4f 75 74 41 20 3d 20 2b 2b 70 50    regOutA = ++pP
2b65c 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65  arse->nMem;.  re
2b65d 67 4f 75 74 42 20 3d 20 2b 2b 70 50 61 72 73 65  gOutB = ++pParse
2b65e 2d 3e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65  ->nMem;.  sqlite
2b65f 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
2b660 26 64 65 73 74 41 2c 20 53 52 54 5f 43 6f 72 6f  &destA, SRT_Coro
2b661 75 74 69 6e 65 2c 20 72 65 67 41 64 64 72 41 29  utine, regAddrA)
2b662 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2b663 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 42  tDestInit(&destB
2b664 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c  , SRT_Coroutine,
2b665 20 72 65 67 41 64 64 72 42 29 3b 0a 0a 20 20 2f   regAddrB);..  /
2b666 2a 20 4a 75 6d 70 20 70 61 73 74 20 74 68 65 20  * Jump past the 
2b667 76 61 72 69 6f 75 73 20 73 75 62 72 6f 75 74 69  various subrouti
2b668 6e 65 73 20 61 6e 64 20 63 6f 72 6f 75 74 69 6e  nes and coroutin
2b669 65 73 20 74 6f 20 74 68 65 20 6d 61 69 6e 0a 20  es to the main. 
2b66a 20 2a 2a 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20   ** merge loop. 
2b66b 20 2a 2f 0a 20 20 6a 31 20 3d 20 73 71 6c 69 74   */.  j1 = sqlit
2b66c 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
2b66d 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 61 64 64 72  OP_Goto);.  addr
2b66e 53 65 6c 65 63 74 41 20 3d 20 73 71 6c 69 74 65  SelectA = sqlite
2b66f 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2b670 28 76 29 3b 0a 0a 0a 20 20 2f 2a 20 47 65 6e 65  (v);...  /* Gene
2b671 72 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65  rate a coroutine
2b672 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65   to evaluate the
2b673 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2b674 74 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6c 65  t to the.  ** le
2b675 66 74 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75  ft of the compou
2b676 6e 64 20 6f 70 65 72 61 74 6f 72 20 2d 20 74 68  nd operator - th
2b677 65 20 22 41 22 20 73 65 6c 65 63 74 2e 0a 20 20  e "A" select..  
2b678 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  */.  VdbeNoopCom
2b679 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69 6e 20  ment((v, "Begin 
2b67a 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65  coroutine for le
2b67b 66 74 20 53 45 4c 45 43 54 22 29 29 3b 0a 20 20  ft SELECT"));.  
2b67c 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 20 3d  pPrior->iLimit =
2b67d 20 72 65 67 4c 69 6d 69 74 41 3b 0a 20 20 73 71   regLimitA;.  sq
2b67e 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
2b67f 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 64 65 73  se, pPrior, &des
2b680 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  tA);.  sqlite3Vd
2b681 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2b682 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f  nteger, 1, regEo
2b683 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  fA);.  sqlite3Vd
2b684 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
2b685 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b  ield, regAddrA);
2b686 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65  .  VdbeNoopComme
2b687 6e 74 28 28 76 2c 20 22 45 6e 64 20 63 6f 72 6f  nt((v, "End coro
2b688 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 53  utine for left S
2b689 45 4c 45 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20  ELECT"));..  /* 
2b68a 47 65 6e 65 72 61 74 65 20 61 20 63 6f 72 6f 75  Generate a corou
2b68b 74 69 6e 65 20 74 6f 20 65 76 61 6c 75 61 74 65  tine to evaluate
2b68c 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2b68d 65 6d 65 6e 74 20 6f 6e 20 0a 20 20 2a 2a 20 74  ement on .  ** t
2b68e 68 65 20 72 69 67 68 74 20 2d 20 74 68 65 20 22  he right - the "
2b68f 42 22 20 73 65 6c 65 63 74 0a 20 20 2a 2f 0a 20  B" select.  */. 
2b690 20 61 64 64 72 53 65 6c 65 63 74 42 20 3d 20 73   addrSelectB = s
2b691 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2b692 74 41 64 64 72 28 76 29 3b 0a 20 20 56 64 62 65  tAddr(v);.  Vdbe
2b693 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
2b694 22 42 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e 65  "Begin coroutine
2b695 20 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43   for right SELEC
2b696 54 22 29 29 3b 0a 20 20 73 61 76 65 64 4c 69 6d  T"));.  savedLim
2b697 69 74 20 3d 20 70 2d 3e 69 4c 69 6d 69 74 3b 0a  it = p->iLimit;.
2b698 20 20 73 61 76 65 64 4f 66 66 73 65 74 20 3d 20    savedOffset = 
2b699 70 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 70 2d  p->iOffset;.  p-
2b69a 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69 6d  >iLimit = regLim
2b69b 69 74 42 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65  itB;.  p->iOffse
2b69c 74 20 3d 20 30 3b 20 20 0a 20 20 73 71 6c 69 74  t = 0;  .  sqlit
2b69d 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
2b69e 20 70 2c 20 26 64 65 73 74 42 29 3b 0a 20 20 70   p, &destB);.  p
2b69f 2d 3e 69 4c 69 6d 69 74 20 3d 20 73 61 76 65 64  ->iLimit = saved
2b6a0 4c 69 6d 69 74 3b 0a 20 20 70 2d 3e 69 4f 66 66  Limit;.  p->iOff
2b6a1 73 65 74 20 3d 20 73 61 76 65 64 4f 66 66 73 65  set = savedOffse
2b6a2 74 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  t;.  sqlite3Vdbe
2b6a3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2b6a4 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 42  eger, 1, regEofB
2b6a5 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2b6a6 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
2b6a7 6c 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20  ld, regAddrB);. 
2b6a8 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
2b6a9 28 28 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74  ((v, "End corout
2b6aa 69 6e 65 20 66 6f 72 20 72 69 67 68 74 20 53 45  ine for right SE
2b6ab 4c 45 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47  LECT"));..  /* G
2b6ac 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75  enerate a subrou
2b6ad 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74  tine that output
2b6ae 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  s the current ro
2b6af 77 20 6f 66 20 74 68 65 20 41 0a 20 20 2a 2a 20  w of the A.  ** 
2b6b0 73 65 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65  select as the ne
2b6b1 78 74 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66  xt output row of
2b6b2 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65   the compound se
2b6b3 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64 62  lect..  */.  Vdb
2b6b4 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
2b6b5 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e 65   "Output routine
2b6b6 20 66 6f 72 20 41 22 29 29 3b 0a 20 20 61 64 64   for A"));.  add
2b6b7 72 4f 75 74 41 20 3d 20 67 65 6e 65 72 61 74 65  rOutA = generate
2b6b8 4f 75 74 70 75 74 53 75 62 72 6f 75 74 69 6e 65  OutputSubroutine
2b6b9 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
2b6ba 20 20 20 20 20 20 20 20 20 20 70 2c 20 26 64 65            p, &de
2b6bb 73 74 41 2c 20 70 44 65 73 74 2c 20 72 65 67 4f  stA, pDest, regO
2b6bc 75 74 41 2c 0a 20 20 20 20 20 20 20 20 20 20 20  utA,.           
2b6bd 20 20 20 20 20 20 72 65 67 50 72 65 76 2c 20 70        regPrev, p
2b6be 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45 59 49 4e  KeyDup, P4_KEYIN
2b6bf 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 6c 61 62 65  FO_HANDOFF, labe
2b6c0 6c 45 6e 64 29 3b 0a 20 20 0a 20 20 2f 2a 20 47  lEnd);.  .  /* G
2b6c1 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75  enerate a subrou
2b6c2 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74  tine that output
2b6c3 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  s the current ro
2b6c4 77 20 6f 66 20 74 68 65 20 42 0a 20 20 2a 2a 20  w of the B.  ** 
2b6c5 73 65 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65  select as the ne
2b6c6 78 74 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66  xt output row of
2b6c7 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65   the compound se
2b6c8 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  lect..  */.  if(
2b6c9 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f   op==TK_ALL || o
2b6ca 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20  p==TK_UNION ){. 
2b6cb 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65     VdbeNoopComme
2b6cc 6e 74 28 28 76 2c 20 22 4f 75 74 70 75 74 20 72  nt((v, "Output r
2b6cd 6f 75 74 69 6e 65 20 66 6f 72 20 42 22 29 29 3b  outine for B"));
2b6ce 0a 20 20 20 20 61 64 64 72 4f 75 74 42 20 3d 20  .    addrOutB = 
2b6cf 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75  generateOutputSu
2b6d0 62 72 6f 75 74 69 6e 65 28 70 50 61 72 73 65 2c  broutine(pParse,
2b6d1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b6d2 20 20 70 2c 20 26 64 65 73 74 42 2c 20 70 44 65    p, &destB, pDe
2b6d3 73 74 2c 20 72 65 67 4f 75 74 42 2c 0a 20 20 20  st, regOutB,.   
2b6d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
2b6d5 67 50 72 65 76 2c 20 70 4b 65 79 44 75 70 2c 20  gPrev, pKeyDup, 
2b6d6 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49  P4_KEYINFO_STATI
2b6d7 43 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20  C, labelEnd);.  
2b6d8 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
2b6d9 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f   a subroutine to
2b6da 20 72 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65   run when the re
2b6db 73 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63  sults from selec
2b6dc 74 20 41 0a 20 20 2a 2a 20 61 72 65 20 65 78 68  t A.  ** are exh
2b6dd 61 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20  austed and only 
2b6de 64 61 74 61 20 69 6e 20 73 65 6c 65 63 74 20 42  data in select B
2b6df 20 72 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20   remains..  */. 
2b6e0 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
2b6e1 28 28 76 2c 20 22 65 6f 66 2d 41 20 73 75 62 72  ((v, "eof-A subr
2b6e2 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 69 66 28  outine"));.  if(
2b6e3 20 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 7c   op==TK_EXCEPT |
2b6e4 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45  | op==TK_INTERSE
2b6e5 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f  CT ){.    addrEo
2b6e6 66 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  fA = sqlite3Vdbe
2b6e7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2b6e8 6f 2c 20 30 2c 20 6c 61 62 65 6c 45 6e 64 29 3b  o, 0, labelEnd);
2b6e9 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20  .  }else{  .    
2b6ea 61 64 64 72 45 6f 66 41 20 3d 20 73 71 6c 69 74  addrEofA = sqlit
2b6eb 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b6ec 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20  OP_If, regEofB, 
2b6ed 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73  labelEnd);.    s
2b6ee 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b6ef 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
2b6f0 67 4f 75 74 42 2c 20 61 64 64 72 4f 75 74 42 29  gOutB, addrOutB)
2b6f1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2b6f2 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69  eAddOp1(v, OP_Yi
2b6f3 65 6c 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a  eld, regAddrB);.
2b6f4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b6f5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
2b6f6 2c 20 30 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a  , 0, addrEofA);.
2b6f7 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61    }..  /* Genera
2b6f8 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
2b6f9 74 6f 20 72 75 6e 20 77 68 65 6e 20 74 68 65 20  to run when the 
2b6fa 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c  results from sel
2b6fb 65 63 74 20 42 0a 20 20 2a 2a 20 61 72 65 20 65  ect B.  ** are e
2b6fc 78 68 61 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c  xhausted and onl
2b6fd 79 20 64 61 74 61 20 69 6e 20 73 65 6c 65 63 74  y data in select
2b6fe 20 41 20 72 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f   A remains..  */
2b6ff 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e  .  if( op==TK_IN
2b700 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20 20 61  TERSECT ){.    a
2b701 64 64 72 45 6f 66 42 20 3d 20 61 64 64 72 45 6f  ddrEofB = addrEo
2b702 66 41 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20  fA;.  }else{  . 
2b703 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65     VdbeNoopComme
2b704 6e 74 28 28 76 2c 20 22 65 6f 66 2d 42 20 73 75  nt((v, "eof-B su
2b705 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20  broutine"));.   
2b706 20 61 64 64 72 45 6f 66 42 20 3d 20 73 71 6c 69   addrEofB = sqli
2b707 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b708 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c   OP_If, regEofA,
2b709 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20   labelEnd);.    
2b70a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b70b 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2b70c 65 67 4f 75 74 41 2c 20 61 64 64 72 4f 75 74 41  egOutA, addrOutA
2b70d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2b70e 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
2b70f 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b  ield, regAddrA);
2b710 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b711 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2b712 6f 2c 20 30 2c 20 61 64 64 72 45 6f 66 42 29 3b  o, 0, addrEofB);
2b713 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2b714 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64  ate code to hand
2b715 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20 41  le the case of A
2b716 3c 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f  <B.  */.  VdbeNo
2b717 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41  opComment((v, "A
2b718 2d 6c 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  -lt-B subroutine
2b719 22 29 29 3b 0a 20 20 61 64 64 72 41 6c 74 42 20  "));.  addrAltB 
2b71a 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2b71b 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
2b71c 20 72 65 67 4f 75 74 41 2c 20 61 64 64 72 4f 75   regOutA, addrOu
2b71d 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  tA);.  sqlite3Vd
2b71e 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
2b71f 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b  ield, regAddrA);
2b720 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2b721 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp2(v, OP_If, r
2b722 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41  egEofA, addrEofA
2b723 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2b724 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2b725 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29  o, 0, labelCmpr)
2b726 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ;..  /* Generate
2b727 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20   code to handle 
2b728 74 68 65 20 63 61 73 65 20 6f 66 20 41 3d 3d 42  the case of A==B
2b729 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d  .  */.  if( op==
2b72a 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 61 64  TK_ALL ){.    ad
2b72b 64 72 41 65 71 42 20 3d 20 61 64 64 72 41 6c 74  drAeqB = addrAlt
2b72c 42 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  B;.  }else if( o
2b72d 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20  p==TK_INTERSECT 
2b72e 29 7b 0a 20 20 20 20 61 64 64 72 41 65 71 42 20  ){.    addrAeqB 
2b72f 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 20 20 20  = addrAltB;.    
2b730 61 64 64 72 41 6c 74 42 2b 2b 3b 0a 20 20 7d 65  addrAltB++;.  }e
2b731 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 4e 6f 6f  lse{.    VdbeNoo
2b732 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d  pComment((v, "A-
2b733 65 71 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22  eq-B subroutine"
2b734 29 29 3b 0a 20 20 20 20 61 64 64 72 41 65 71 42  ));.    addrAeqB
2b735 20 3d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   =.    sqlite3Vd
2b736 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
2b737 69 65 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b  ield, regAddrA);
2b738 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b739 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c  AddOp2(v, OP_If,
2b73a 20 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f   regEofA, addrEo
2b73b 66 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  fA);.    sqlite3
2b73c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b73d 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43  _Goto, 0, labelC
2b73e 6d 70 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  mpr);.  }..  /* 
2b73f 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
2b740 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61 73 65   handle the case
2b741 20 6f 66 20 41 3e 42 0a 20 20 2a 2f 0a 20 20 56   of A>B.  */.  V
2b742 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
2b743 76 2c 20 22 41 2d 67 74 2d 42 20 73 75 62 72 6f  v, "A-gt-B subro
2b744 75 74 69 6e 65 22 29 29 3b 0a 20 20 61 64 64 72  utine"));.  addr
2b745 41 67 74 42 20 3d 20 73 71 6c 69 74 65 33 56 64  AgtB = sqlite3Vd
2b746 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2b747 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41  ;.  if( op==TK_A
2b748 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e 49  LL || op==TK_UNI
2b749 4f 4e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ON ){.    sqlite
2b74a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b74b 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42  P_Gosub, regOutB
2b74c 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 7d  , addrOutB);.  }
2b74d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2b74e 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
2b74f 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20 73  , regAddrB);.  s
2b750 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b751 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f  (v, OP_If, regEo
2b752 66 42 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a 20  fB, addrEofB);. 
2b753 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b754 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
2b755 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20  , labelCmpr);.. 
2b756 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 72 75   /* This code ru
2b757 6e 73 20 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69  ns once to initi
2b758 61 6c 69 7a 65 20 65 76 65 72 79 74 68 69 6e 67  alize everything
2b759 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
2b75a 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2b75b 6a 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  j1);.  sqlite3Vd
2b75c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2b75d 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 45 6f  nteger, 0, regEo
2b75e 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  fA);.  sqlite3Vd
2b75f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2b760 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 45 6f  nteger, 0, regEo
2b761 66 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  fB);.  sqlite3Vd
2b762 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2b763 6f 73 75 62 2c 20 72 65 67 41 64 64 72 41 2c 20  osub, regAddrA, 
2b764 61 64 64 72 53 65 6c 65 63 74 41 29 3b 0a 20 20  addrSelectA);.  
2b765 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b766 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2b767 65 67 41 64 64 72 42 2c 20 61 64 64 72 53 65 6c  egAddrB, addrSel
2b768 65 63 74 42 29 3b 0a 20 20 73 71 6c 69 74 65 33  ectB);.  sqlite3
2b769 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b76a 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 61 64  _If, regEofA, ad
2b76b 64 72 45 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74  drEofA);.  sqlit
2b76c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b76d 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20  OP_If, regEofB, 
2b76e 61 64 64 72 45 6f 66 42 29 3b 0a 0a 20 20 2f 2a  addrEofB);..  /*
2b76f 20 49 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 6d   Implement the m
2b770 61 69 6e 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20  ain merge loop. 
2b771 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2b772 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2b773 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20 73   labelCmpr);.  s
2b774 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2b775 28 76 2c 20 4f 50 5f 50 65 72 6d 75 74 61 74 69  (v, OP_Permutati
2b776 6f 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68  on, 0, 0, 0, (ch
2b777 61 72 2a 29 61 50 65 72 6d 75 74 65 2c 20 50 34  ar*)aPermute, P4
2b778 5f 49 4e 54 41 52 52 41 59 29 3b 0a 20 20 73 71  _INTARRAY);.  sq
2b779 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2b77a 76 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 64  v, OP_Compare, d
2b77b 65 73 74 41 2e 69 4d 65 6d 2c 20 64 65 73 74 42  estA.iMem, destB
2b77c 2e 69 4d 65 6d 2c 20 6e 4f 72 64 65 72 42 79 2c  .iMem, nOrderBy,
2b77d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b77e 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
2b77f 29 70 4b 65 79 4d 65 72 67 65 2c 20 50 34 5f 4b  )pKeyMerge, P4_K
2b780 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
2b781 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2b782 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c  dOp3(v, OP_Jump,
2b783 20 61 64 64 72 41 6c 74 42 2c 20 61 64 64 72 41   addrAltB, addrA
2b784 65 71 42 2c 20 61 64 64 72 41 67 74 42 29 3b 0a  eqB, addrAgtB);.
2b785 0a 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 65  .  /* Release te
2b786 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72  mporary register
2b787 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67  s.  */.  if( reg
2b788 50 72 65 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  Prev ){.    sqli
2b789 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
2b78a 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 50  nge(pParse, regP
2b78b 72 65 76 2c 20 6e 4f 72 64 65 72 42 79 2b 31 29  rev, nOrderBy+1)
2b78c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70  ;.  }..  /* Jump
2b78d 20 74 6f 20 74 68 65 20 74 68 69 73 20 70 6f 69   to the this poi
2b78e 6e 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74  nt in order to t
2b78f 65 72 6d 69 6e 61 74 65 20 74 68 65 20 71 75 65  erminate the que
2b790 72 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ry..  */.  sqlit
2b791 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2b792 65 6c 28 76 2c 20 6c 61 62 65 6c 45 6e 64 29 3b  el(v, labelEnd);
2b793 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e  ..  /* Set the n
2b794 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20  umber of output 
2b795 63 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69  columns.  */.  i
2b796 66 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d  f( pDest->eDest=
2b797 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20  =SRT_Output ){. 
2b798 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73     Select *pFirs
2b799 74 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20  t = pPrior;.    
2b79a 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70  while( pFirst->p
2b79b 50 72 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d  Prior ) pFirst =
2b79c 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b   pFirst->pPrior;
2b79d 0a 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c  .    generateCol
2b79e 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
2b79f 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69   0, pFirst->pELi
2b7a0 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  st);.  }..  /* R
2b7a1 65 61 73 73 65 6d 62 6c 79 20 74 68 65 20 63 6f  eassembly the co
2b7a2 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 73 6f 20  mpound query so 
2b7a3 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20  that it will be 
2b7a4 66 72 65 65 64 20 63 6f 72 72 65 63 74 6c 79 0a  freed correctly.
2b7a5 20 20 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c    ** by the call
2b7a6 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ing function */.
2b7a7 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20    if( p->pPrior 
2b7a8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
2b7a9 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2b7aa 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20  ->pPrior);.  }. 
2b7ab 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72   p->pPrior = pPr
2b7ac 69 6f 72 3b 0a 0a 20 20 2f 2a 2a 2a 20 54 42 44  ior;..  /*** TBD
2b7ad 3a 20 20 49 6e 73 65 72 74 20 73 75 62 72 6f 75  :  Insert subrou
2b7ae 74 69 6e 65 20 63 61 6c 6c 73 20 74 6f 20 63 6c  tine calls to cl
2b7af 6f 73 65 20 63 75 72 73 6f 72 73 20 6f 6e 20 69  ose cursors on i
2b7b0 6e 63 6f 6d 70 6c 65 74 65 0a 20 20 2a 2a 2a 2a  ncomplete.  ****
2b7b1 20 73 75 62 71 75 65 72 69 65 73 20 2a 2a 2a 2a   subqueries ****
2b7b2 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  /.  return SQLIT
2b7b3 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  E_OK;.}.#endif..
2b7b4 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2b7b5 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2b7b6 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  Y) || !defined(S
2b7b7 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
2b7b8 0a 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c  ./* Forward Decl
2b7b9 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74  arations */.stat
2b7ba 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78 70  ic void substExp
2b7bb 72 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 2c 20  rList(sqlite3*, 
2b7bc 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20  ExprList*, int, 
2b7bd 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74  ExprList*);.stat
2b7be 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65 6c  ic void substSel
2b7bf 65 63 74 28 73 71 6c 69 74 65 33 2a 2c 20 53 65  ect(sqlite3*, Se
2b7c0 6c 65 63 74 20 2a 2c 20 69 6e 74 2c 20 45 78 70  lect *, int, Exp
2b7c1 72 4c 69 73 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  rList *);../*.**
2b7c2 20 53 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68   Scan through th
2b7c3 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  e expression pEx
2b7c4 70 72 2e 20 20 52 65 70 6c 61 63 65 20 65 76 65  pr.  Replace eve
2b7c5 72 79 20 72 65 66 65 72 65 6e 63 65 20 74 6f 0a  ry reference to.
2b7c6 2a 2a 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  ** a column in t
2b7c7 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 54 61 62  able number iTab
2b7c8 6c 65 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f  le with a copy o
2b7c9 66 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68  f the iColumn-th
2b7ca 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 70 45 4c  .** entry in pEL
2b7cb 69 73 74 2e 20 20 28 42 75 74 20 6c 65 61 76 65  ist.  (But leave
2b7cc 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
2b7cd 68 65 20 52 4f 57 49 44 20 63 6f 6c 75 6d 6e 20  he ROWID column 
2b7ce 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 29 0a  .** unchanged.).
2b7cf 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2b7d0 6e 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  ne is part of th
2b7d1 65 20 66 6c 61 74 74 65 6e 69 6e 67 20 70 72 6f  e flattening pro
2b7d2 63 65 64 75 72 65 2e 20 20 41 20 73 75 62 71 75  cedure.  A subqu
2b7d3 65 72 79 0a 2a 2a 20 77 68 6f 73 65 20 72 65 73  ery.** whose res
2b7d4 75 6c 74 20 73 65 74 20 69 73 20 64 65 66 69 6e  ult set is defin
2b7d5 65 64 20 62 79 20 70 45 4c 69 73 74 20 61 70 70  ed by pEList app
2b7d6 65 61 72 73 20 61 73 20 65 6e 74 72 79 20 69 6e  ears as entry in
2b7d7 20 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61   the.** FROM cla
2b7d8 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20  use of a SELECT 
2b7d9 73 75 63 68 20 74 68 61 74 20 74 68 65 20 56 44  such that the VD
2b7da 42 45 20 63 75 72 73 6f 72 20 61 73 73 69 67 6e  BE cursor assign
2b7db 65 64 20 74 6f 20 74 68 61 74 0a 2a 2a 20 46 4f  ed to that.** FO
2b7dc 52 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20  RM clause entry 
2b7dd 69 73 20 69 54 61 62 6c 65 2e 20 20 54 68 69 73  is iTable.  This
2b7de 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20 74 68   routine make th
2b7df 65 20 6e 65 63 65 73 73 61 72 79 20 0a 2a 2a 20  e necessary .** 
2b7e0 63 68 61 6e 67 65 73 20 74 6f 20 70 45 78 70 72  changes to pExpr
2b7e1 20 73 6f 20 74 68 61 74 20 69 74 20 72 65 66 65   so that it refe
2b7e2 72 73 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74  rs directly to t
2b7e3 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 0a  he source table.
2b7e4 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  ** of the subque
2b7e5 72 79 20 72 61 74 68 65 72 20 74 68 65 20 72 65  ry rather the re
2b7e6 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20  sult set of the 
2b7e7 73 75 62 71 75 65 72 79 2e 0a 2a 2f 0a 73 74 61  subquery..*/.sta
2b7e8 74 69 63 20 45 78 70 72 20 2a 73 75 62 73 74 45  tic Expr *substE
2b7e9 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  xpr(.  sqlite3 *
2b7ea 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 52 65  db,        /* Re
2b7eb 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f  port malloc erro
2b7ec 72 73 20 74 6f 20 74 68 69 73 20 63 6f 6e 6e 65  rs to this conne
2b7ed 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20  ction */.  Expr 
2b7ee 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 2f  *pExpr,        /
2b7ef 2a 20 45 78 70 72 20 69 6e 20 77 68 69 63 68 20  * Expr in which 
2b7f0 73 75 62 73 74 69 74 75 74 69 6f 6e 20 6f 63 63  substitution occ
2b7f1 75 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  urs */.  int iTa
2b7f2 62 6c 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ble,         /* 
2b7f3 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75 62 73  Table to be subs
2b7f4 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45 78 70  tituted */.  Exp
2b7f5 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20  rList *pEList   
2b7f6 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 65   /* Substitute e
2b7f7 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 29 7b  xpressions */.){
2b7f8 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20  .  if( pExpr==0 
2b7f9 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
2b7fa 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2b7fb 43 4f 4c 55 4d 4e 20 26 26 20 70 45 78 70 72 2d  COLUMN && pExpr-
2b7fc 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20  >iTable==iTable 
2b7fd 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72  ){.    if( pExpr
2b7fe 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20  ->iColumn<0 ){. 
2b7ff 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
2b800 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 7d 65   TK_NULL;.    }e
2b801 6c 73 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20  lse{.      Expr 
2b802 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 61 73 73  *pNew;.      ass
2b803 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 26  ert( pEList!=0 &
2b804 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  & pExpr->iColumn
2b805 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  <pEList->nExpr )
2b806 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b807 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20  pExpr->pLeft==0 
2b808 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  && pExpr->pRight
2b809 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4e 65  ==0 );.      pNe
2b80a 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  w = sqlite3ExprD
2b80b 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61  up(db, pEList->a
2b80c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d  [pExpr->iColumn]
2b80d 2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20  .pExpr, 0);.    
2b80e 20 20 69 66 28 20 70 4e 65 77 20 26 26 20 70 45    if( pNew && pE
2b80f 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20 20  xpr->pColl ){.  
2b810 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c        pNew->pCol
2b811 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c  l = pExpr->pColl
2b812 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2b813 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2b814 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20  e(db, pExpr);.  
2b815 20 20 20 20 70 45 78 70 72 20 3d 20 70 4e 65 77      pExpr = pNew
2b816 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
2b817 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66  .    pExpr->pLef
2b818 74 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62  t = substExpr(db
2b819 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
2b81a 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2b81b 0a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67  .    pExpr->pRig
2b81c 68 74 20 3d 20 73 75 62 73 74 45 78 70 72 28 64  ht = substExpr(d
2b81d 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  b, pExpr->pRight
2b81e 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
2b81f 29 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48  );.    if( ExprH
2b820 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2b821 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2b822 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53 65  ){.      substSe
2b823 6c 65 63 74 28 64 62 2c 20 70 45 78 70 72 2d 3e  lect(db, pExpr->
2b824 78 2e 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c  x.pSelect, iTabl
2b825 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  e, pEList);.    
2b826 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 75 62  }else{.      sub
2b827 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70  stExprList(db, p
2b828 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 20 69  Expr->x.pList, i
2b829 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
2b82a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2b82b 72 6e 20 70 45 78 70 72 3b 0a 7d 0a 73 74 61 74  rn pExpr;.}.stat
2b82c 69 63 20 76 6f 69 64 20 73 75 62 73 74 45 78 70  ic void substExp
2b82d 72 4c 69 73 74 28 0a 20 20 73 71 6c 69 74 65 33  rList(.  sqlite3
2b82e 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a   *db,         /*
2b82f 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65   Report malloc e
2b830 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20  rrors here */.  
2b831 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
2b832 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20       /* List to 
2b833 73 63 61 6e 20 61 6e 64 20 69 6e 20 77 68 69 63  scan and in whic
2b834 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69  h to make substi
2b835 74 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69  tutes */.  int i
2b836 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20  Table,          
2b837 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73  /* Table to be s
2b838 75 62 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20  ubstituted */.  
2b839 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2b83a 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75       /* Substitu
2b83b 74 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a  te values */.){.
2b83c 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
2b83d 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
2b83e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2b83f 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
2b840 29 7b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b  ){.    pList->a[
2b841 69 5d 2e 70 45 78 70 72 20 3d 20 73 75 62 73 74  i].pExpr = subst
2b842 45 78 70 72 28 64 62 2c 20 70 4c 69 73 74 2d 3e  Expr(db, pList->
2b843 61 5b 69 5d 2e 70 45 78 70 72 2c 20 69 54 61 62  a[i].pExpr, iTab
2b844 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d  le, pEList);.  }
2b845 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  .}.static void s
2b846 75 62 73 74 53 65 6c 65 63 74 28 0a 20 20 73 71  ubstSelect(.  sq
2b847 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2b848 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c     /* Report mal
2b849 6c 6f 63 20 65 72 72 6f 72 73 20 68 65 72 65 20  loc errors here 
2b84a 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
2b84b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c            /* SEL
2b84c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  ECT statement in
2b84d 20 77 68 69 63 68 20 74 6f 20 6d 61 6b 65 20 73   which to make s
2b84e 75 62 73 74 69 74 75 74 69 6f 6e 73 20 2a 2f 0a  ubstitutions */.
2b84f 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
2b850 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
2b851 74 6f 20 62 65 20 72 65 70 6c 61 63 65 64 20 2a  to be replaced *
2b852 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2b853 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75 62 73  List     /* Subs
2b854 74 69 74 75 74 65 20 76 61 6c 75 65 73 20 2a 2f  titute values */
2b855 0a 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  .){.  SrcList *p
2b856 53 72 63 3b 0a 20 20 73 74 72 75 63 74 20 53 72  Src;.  struct Sr
2b857 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
2b858 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  m;.  int i;.  if
2b859 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( !p ) return;. 
2b85a 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
2b85b 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69 54  b, p->pEList, iT
2b85c 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
2b85d 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
2b85e 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20  b, p->pGroupBy, 
2b85f 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2b860 0a 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74  .  substExprList
2b861 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  (db, p->pOrderBy
2b862 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
2b863 29 3b 0a 20 20 70 2d 3e 70 48 61 76 69 6e 67 20  );.  p->pHaving 
2b864 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20  = substExpr(db, 
2b865 70 2d 3e 70 48 61 76 69 6e 67 2c 20 69 54 61 62  p->pHaving, iTab
2b866 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 70  le, pEList);.  p
2b867 2d 3e 70 57 68 65 72 65 20 3d 20 73 75 62 73 74  ->pWhere = subst
2b868 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 57 68 65  Expr(db, p->pWhe
2b869 72 65 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  re, iTable, pELi
2b86a 73 74 29 3b 0a 20 20 73 75 62 73 74 53 65 6c 65  st);.  substSele
2b86b 63 74 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72  ct(db, p->pPrior
2b86c 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
2b86d 29 3b 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70  );.  pSrc = p->p
2b86e 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Src;.  assert( p
2b86f 53 72 63 20 29 3b 20 20 2f 2a 20 45 76 65 6e 20  Src );  /* Even 
2b870 66 6f 72 20 28 53 45 4c 45 43 54 20 31 29 20 77  for (SELECT 1) w
2b871 65 20 68 61 76 65 3a 20 70 53 72 63 21 3d 30 20  e have: pSrc!=0 
2b872 62 75 74 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d  but pSrc->nSrc==
2b873 30 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59  0 */.  if( ALWAY
2b874 53 28 70 53 72 63 29 20 29 7b 0a 20 20 20 20 66  S(pSrc) ){.    f
2b875 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c  or(i=pSrc->nSrc,
2b876 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20   pItem=pSrc->a; 
2b877 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b  i>0; i--, pItem+
2b878 2b 29 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53  +){.      substS
2b879 65 6c 65 63 74 28 64 62 2c 20 70 49 74 65 6d 2d  elect(db, pItem-
2b87a 3e 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c 65  >pSelect, iTable
2b87b 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d  , pEList);.    }
2b87c 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
2b87d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2b87e 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20  _OMIT_SUBQUERY) 
2b87f 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
2b880 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 2a 2f  TE_OMIT_VIEW) */
2b881 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
2b882 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2b883 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  ERY) || !defined
2b884 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
2b885 57 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  W)./*.** This ro
2b886 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74  utine attempts t
2b887 6f 20 66 6c 61 74 74 65 6e 20 73 75 62 71 75 65  o flatten subque
2b888 72 69 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f  ries in order to
2b889 20 73 70 65 65 64 0a 2a 2a 20 65 78 65 63 75 74   speed.** execut
2b88a 69 6f 6e 2e 20 20 49 74 20 72 65 74 75 72 6e 73  ion.  It returns
2b88b 20 31 20 69 66 20 69 74 20 6d 61 6b 65 73 20 63   1 if it makes c
2b88c 68 61 6e 67 65 73 20 61 6e 64 20 30 20 69 66 20  hanges and 0 if 
2b88d 6e 6f 20 66 6c 61 74 74 65 6e 69 6e 67 0a 2a 2a  no flattening.**
2b88e 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54   occurs..**.** T
2b88f 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65  o understand the
2b890 20 63 6f 6e 63 65 70 74 20 6f 66 20 66 6c 61 74   concept of flat
2b891 74 65 6e 69 6e 67 2c 20 63 6f 6e 73 69 64 65 72  tening, consider
2b892 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   the following.*
2b893 2a 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20  * query:.**.**  
2b894 20 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d     SELECT a FROM
2b895 20 28 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20   (SELECT x+y AS 
2b896 61 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  a FROM t1 WHERE 
2b897 7a 3c 31 30 30 29 20 57 48 45 52 45 20 61 3e 35  z<100) WHERE a>5
2b898 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  .**.** The defau
2b899 6c 74 20 77 61 79 20 6f 66 20 69 6d 70 6c 65 6d  lt way of implem
2b89a 65 6e 74 69 6e 67 20 74 68 69 73 20 71 75 65 72  enting this quer
2b89b 79 20 69 73 20 74 6f 20 65 78 65 63 75 74 65 20  y is to execute 
2b89c 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 20  the.** subquery 
2b89d 66 69 72 73 74 20 61 6e 64 20 73 74 6f 72 65 20  first and store 
2b89e 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61  the results in a
2b89f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2b8a0 2c 20 74 68 65 6e 0a 2a 2a 20 72 75 6e 20 74 68  , then.** run th
2b8a1 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 6f 6e  e outer query on
2b8a2 20 74 68 61 74 20 74 65 6d 70 6f 72 61 72 79 20   that temporary 
2b8a3 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72 65 71  table.  This req
2b8a4 75 69 72 65 73 20 74 77 6f 0a 2a 2a 20 70 61 73  uires two.** pas
2b8a5 73 65 73 20 6f 76 65 72 20 74 68 65 20 64 61 74  ses over the dat
2b8a6 61 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c  a.  Furthermore,
2b8a7 20 62 65 63 61 75 73 65 20 74 68 65 20 74 65 6d   because the tem
2b8a8 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 2a 2a 20  porary table.** 
2b8a9 68 61 73 20 6e 6f 20 69 6e 64 69 63 65 73 2c 20  has no indices, 
2b8aa 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2b8ab 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75   on the outer qu
2b8ac 65 72 79 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a  ery cannot be.**
2b8ad 20 6f 70 74 69 6d 69 7a 65 64 2e 0a 2a 2a 0a 2a   optimized..**.*
2b8ae 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
2b8af 74 74 65 6d 70 74 73 20 74 6f 20 72 65 77 72 69  ttempts to rewri
2b8b0 74 65 20 71 75 65 72 69 65 73 20 73 75 63 68 20  te queries such 
2b8b1 61 73 20 74 68 65 20 61 62 6f 76 65 20 69 6e 74  as the above int
2b8b2 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 66 6c  o.** a single fl
2b8b3 61 74 20 73 65 6c 65 63 74 2c 20 6c 69 6b 65 20  at select, like 
2b8b4 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
2b8b5 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20 61 20  SELECT x+y AS a 
2b8b6 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 7a 3c  FROM t1 WHERE z<
2b8b7 31 30 30 20 41 4e 44 20 61 3e 35 0a 2a 2a 0a 2a  100 AND a>5.**.*
2b8b8 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72  * The code gener
2b8b9 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 73 69  ated for this si
2b8ba 6d 70 69 66 69 63 61 74 69 6f 6e 20 67 69 76 65  mpification give
2b8bb 73 20 74 68 65 20 73 61 6d 65 20 72 65 73 75 6c  s the same resul
2b8bc 74 0a 2a 2a 20 62 75 74 20 6f 6e 6c 79 20 68 61  t.** but only ha
2b8bd 73 20 74 6f 20 73 63 61 6e 20 74 68 65 20 64 61  s to scan the da
2b8be 74 61 20 6f 6e 63 65 2e 20 20 41 6e 64 20 62 65  ta once.  And be
2b8bf 63 61 75 73 65 20 69 6e 64 69 63 65 73 20 6d 69  cause indices mi
2b8c0 67 68 74 20 0a 2a 2a 20 65 78 69 73 74 20 6f 6e  ght .** exist on
2b8c1 20 74 68 65 20 74 61 62 6c 65 20 74 31 2c 20 61   the table t1, a
2b8c2 20 63 6f 6d 70 6c 65 74 65 20 73 63 61 6e 20 6f   complete scan o
2b8c3 66 20 74 68 65 20 64 61 74 61 20 6d 69 67 68 74  f the data might
2b8c4 20 62 65 0a 2a 2a 20 61 76 6f 69 64 65 64 2e 0a   be.** avoided..
2b8c5 2a 2a 0a 2a 2a 20 46 6c 61 74 74 65 6e 69 6e 67  **.** Flattening
2b8c6 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74   is only attempt
2b8c7 65 64 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65  ed if all of the
2b8c8 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74   following are t
2b8c9 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29  rue:.**.**   (1)
2b8ca 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 61    The subquery a
2b8cb 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  nd the outer que
2b8cc 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 75  ry do not both u
2b8cd 73 65 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a  se aggregates..*
2b8ce 2a 0a 2a 2a 20 20 20 28 32 29 20 20 54 68 65 20  *.**   (2)  The 
2b8cf 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20  subquery is not 
2b8d0 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f 72 20  an aggregate or 
2b8d1 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
2b8d2 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a  is not a join..*
2b8d3 2a 0a 2a 2a 20 20 20 28 33 29 20 20 54 68 65 20  *.**   (3)  The 
2b8d4 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20  subquery is not 
2b8d5 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  the right operan
2b8d6 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74 65  d of a left oute
2b8d7 72 20 6a 6f 69 6e 0a 2a 2a 20 20 20 20 20 20 20  r join.**       
2b8d8 20 28 4f 72 69 67 69 6e 61 6c 6c 79 20 74 69 63   (Originally tic
2b8d9 6b 65 74 20 23 33 30 36 2e 20 20 53 74 72 65 6e  ket #306.  Stren
2b8da 67 68 74 65 6e 65 64 20 62 79 20 74 69 63 6b 65  ghtened by ticke
2b8db 74 20 23 33 33 30 30 29 0a 2a 2a 0a 2a 2a 20 20  t #3300).**.**  
2b8dc 20 28 34 29 20 20 54 68 65 20 73 75 62 71 75 65   (4)  The subque
2b8dd 72 79 20 69 73 20 6e 6f 74 20 44 49 53 54 49 4e  ry is not DISTIN
2b8de 43 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20  CT or the outer 
2b8df 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a  query is not a j
2b8e0 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29  oin..**.**   (5)
2b8e1 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69    The subquery i
2b8e2 73 20 6e 6f 74 20 44 49 53 54 49 4e 43 54 20 6f  s not DISTINCT o
2b8e3 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
2b8e4 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a  y does not use.*
2b8e5 2a 20 20 20 20 20 20 20 20 61 67 67 72 65 67 61  *        aggrega
2b8e6 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29  tes..**.**   (6)
2b8e7 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64    The subquery d
2b8e8 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72  oes not use aggr
2b8e9 65 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75  egates or the ou
2b8ea 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74  ter query is not
2b8eb 0a 2a 2a 20 20 20 20 20 20 20 20 44 49 53 54 49  .**        DISTI
2b8ec 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 37 29  NCT..**.**   (7)
2b8ed 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 68    The subquery h
2b8ee 61 73 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65  as a FROM clause
2b8ef 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 38 29 20 20 54  ..**.**   (8)  T
2b8f0 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
2b8f1 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f   not use LIMIT o
2b8f2 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
2b8f3 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e  y is not a join.
2b8f4 0a 2a 2a 0a 2a 2a 20 20 20 28 39 29 20 20 54 68  .**.**   (9)  Th
2b8f5 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20  e subquery does 
2b8f6 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72  not use LIMIT or
2b8f7 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2b8f8 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a 2a   does not use.**
2b8f9 20 20 20 20 20 20 20 20 61 67 67 72 65 67 61 74          aggregat
2b8fa 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 30 29 20  es..**.**  (10) 
2b8fb 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
2b8fc 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72 65  es not use aggre
2b8fd 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75 74  gates or the out
2b8fe 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  er query does no
2b8ff 74 0a 2a 2a 20 20 20 20 20 20 20 20 75 73 65 20  t.**        use 
2b900 4c 49 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20 20 28 31  LIMIT..**.**  (1
2b901 31 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  1)  The subquery
2b902 20 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71   and the outer q
2b903 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68  uery do not both
2b904 20 68 61 76 65 20 4f 52 44 45 52 20 42 59 20 63   have ORDER BY c
2b905 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28  lauses..**.**  (
2b906 31 32 29 20 20 4e 6f 74 20 69 6d 70 6c 65 6d 65  12)  Not impleme
2b907 6e 74 65 64 2e 20 20 53 75 62 73 75 6d 65 64 20  nted.  Subsumed 
2b908 69 6e 74 6f 20 72 65 73 74 72 69 63 74 69 6f 6e  into restriction
2b909 20 28 33 29 2e 20 20 57 61 73 20 70 72 65 76 69   (3).  Was previ
2b90a 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20  ously.**        
2b90b 61 20 73 65 70 61 72 61 74 65 20 72 65 73 74 72  a separate restr
2b90c 69 63 74 69 6f 6e 20 64 65 72 69 76 69 6e 67 20  iction deriving 
2b90d 66 72 6f 6d 20 74 69 63 6b 65 74 20 23 33 35 30  from ticket #350
2b90e 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 33 29 20 20 54  ..**.**  (13)  T
2b90f 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  he subquery and 
2b910 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 20 6e  outer query do n
2b911 6f 74 20 62 6f 74 68 20 75 73 65 20 4c 49 4d 49  ot both use LIMI
2b912 54 0a 2a 2a 0a 2a 2a 20 20 28 31 34 29 20 20 54  T.**.**  (14)  T
2b913 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
2b914 20 6e 6f 74 20 75 73 65 20 4f 46 46 53 45 54 0a   not use OFFSET.
2b915 2a 2a 0a 2a 2a 20 20 28 31 35 29 20 20 54 68 65  **.**  (15)  The
2b916 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
2b917 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 63 6f  not part of a co
2b918 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 6f 72  mpound select or
2b919 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 73   the.**        s
2b91a 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  ubquery does not
2b91b 20 68 61 76 65 20 62 6f 74 68 20 61 6e 20 4f 52   have both an OR
2b91c 44 45 52 20 42 59 20 61 6e 64 20 61 20 4c 49 4d  DER BY and a LIM
2b91d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 20 20  IT clause..**   
2b91e 20 20 20 20 20 28 53 65 65 20 74 69 63 6b 65 74       (See ticket
2b91f 20 23 32 33 33 39 29 0a 2a 2a 0a 2a 2a 20 20 28   #2339).**.**  (
2b920 31 36 29 20 20 54 68 65 20 6f 75 74 65 72 20 71  16)  The outer q
2b921 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e 20 61  uery is not an a
2b922 67 67 72 65 67 61 74 65 20 6f 72 20 74 68 65 20  ggregate or the 
2b923 73 75 62 71 75 65 72 79 20 64 6f 65 73 0a 2a 2a  subquery does.**
2b924 20 20 20 20 20 20 20 20 6e 6f 74 20 63 6f 6e 74          not cont
2b925 61 69 6e 20 4f 52 44 45 52 20 42 59 2e 20 20 28  ain ORDER BY.  (
2b926 54 69 63 6b 65 74 20 23 32 39 34 32 29 20 20 54  Ticket #2942)  T
2b927 68 69 73 20 75 73 65 64 20 74 6f 20 6e 6f 74 20  his used to not 
2b928 6d 61 74 74 65 72 0a 2a 2a 20 20 20 20 20 20 20  matter.**       
2b929 20 75 6e 74 69 6c 20 77 65 20 69 6e 74 72 6f 64   until we introd
2b92a 75 63 65 64 20 74 68 65 20 67 72 6f 75 70 5f 63  uced the group_c
2b92b 6f 6e 63 61 74 28 29 20 66 75 6e 63 74 69 6f 6e  oncat() function
2b92c 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 28 31 37 29 20  .  .**.**  (17) 
2b92d 20 54 68 65 20 73 75 62 2d 71 75 65 72 79 20 69   The sub-query i
2b92e 73 20 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64  s not a compound
2b92f 20 73 65 6c 65 63 74 2c 20 6f 72 20 69 74 20 69   select, or it i
2b930 73 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 0a 2a  s a UNION ALL .*
2b931 2a 20 20 20 20 20 20 20 20 63 6f 6d 70 6f 75 6e  *        compoun
2b932 64 20 63 6c 61 75 73 65 20 6d 61 64 65 20 75 70  d clause made up
2b933 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 6e 6f 6e   entirely of non
2b934 2d 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69  -aggregate queri
2b935 65 73 2c 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20  es, and .**     
2b936 20 20 20 74 68 65 20 70 61 72 65 6e 74 20 71 75     the parent qu
2b937 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ery:.**.**      
2b938 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 69 74 73      * is not its
2b939 65 6c 66 20 70 61 72 74 20 6f 66 20 61 20 63 6f  elf part of a co
2b93a 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 0a 2a  mpound select,.*
2b93b 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69 73 20  *          * is 
2b93c 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74 65  not an aggregate
2b93d 20 6f 72 20 44 49 53 54 49 4e 43 54 20 71 75 65   or DISTINCT que
2b93e 72 79 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ry, and.**      
2b93f 20 20 20 20 2a 20 68 61 73 20 6e 6f 20 6f 74 68      * has no oth
2b940 65 72 20 74 61 62 6c 65 73 20 6f 72 20 73 75 62  er tables or sub
2b941 2d 73 65 6c 65 63 74 73 20 69 6e 20 74 68 65 20  -selects in the 
2b942 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  FROM clause..**.
2b943 2a 2a 20 20 20 20 20 20 20 20 54 68 65 20 70 61  **        The pa
2b944 72 65 6e 74 20 61 6e 64 20 73 75 62 2d 71 75 65  rent and sub-que
2b945 72 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 57  ry may contain W
2b946 48 45 52 45 20 63 6c 61 75 73 65 73 2e 20 53 75  HERE clauses. Su
2b947 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 20 20 20 20  bject to.**     
2b948 20 20 20 72 75 6c 65 73 20 28 31 31 29 2c 20 28     rules (11), (
2b949 31 33 29 20 61 6e 64 20 28 31 34 29 2c 20 74 68  13) and (14), th
2b94a 65 79 20 6d 61 79 20 61 6c 73 6f 20 63 6f 6e 74  ey may also cont
2b94b 61 69 6e 20 4f 52 44 45 52 20 42 59 2c 0a 2a 2a  ain ORDER BY,.**
2b94c 20 20 20 20 20 20 20 20 4c 49 4d 49 54 20 61 6e          LIMIT an
2b94d 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73  d OFFSET clauses
2b94e 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 38 29 20 20 49  ..**.**  (18)  I
2b94f 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20  f the sub-query 
2b950 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  is a compound se
2b951 6c 65 63 74 2c 20 74 68 65 6e 20 61 6c 6c 20 74  lect, then all t
2b952 65 72 6d 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20  erms of the.**  
2b953 20 20 20 20 20 20 4f 52 44 45 52 20 62 79 20 63        ORDER by c
2b954 6c 61 75 73 65 20 6f 66 20 74 68 65 20 70 61 72  lause of the par
2b955 65 6e 74 20 6d 75 73 74 20 62 65 20 73 69 6d 70  ent must be simp
2b956 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  le references to
2b957 20 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6c 75   .**        colu
2b958 6d 6e 73 20 6f 66 20 74 68 65 20 73 75 62 2d 71  mns of the sub-q
2b959 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 39  uery..**.**  (19
2b95a 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2b95b 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d  does not use LIM
2b95c 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20  IT or the outer 
2b95d 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a  query does not.*
2b95e 2a 20 20 20 20 20 20 20 20 68 61 76 65 20 61 20  *        have a 
2b95f 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2a  WHERE clause..**
2b960 0a 2a 2a 20 20 28 32 30 29 20 20 49 66 20 74 68  .**  (20)  If th
2b961 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 61  e sub-query is a
2b962 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
2b963 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74 20 6e  , then it must n
2b964 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20  ot use.**       
2b965 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
2b966 75 73 65 2e 20 20 54 69 63 6b 65 74 20 23 33 37  use.  Ticket #37
2b967 37 33 2e 20 20 57 65 20 63 6f 75 6c 64 20 72 65  73.  We could re
2b968 6c 61 78 20 74 68 69 73 20 63 6f 6e 73 74 72 61  lax this constra
2b969 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 73 6f  int.**        so
2b96a 6d 65 77 68 61 74 20 62 79 20 73 61 79 69 6e 67  mewhat by saying
2b96b 20 74 68 61 74 20 74 68 65 20 74 65 72 6d 73 20   that the terms 
2b96c 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
2b96d 63 6c 61 75 73 65 20 6d 75 73 74 0a 2a 2a 20 20  clause must.**  
2b96e 20 20 20 20 20 20 61 70 70 65 61 72 20 61 73 20        appear as 
2b96f 75 6e 6d 6f 64 69 66 69 65 64 20 72 65 73 75 6c  unmodified resul
2b970 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  t columns in the
2b971 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20 20 42   outer query.  B
2b972 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 68 61 76  ut.**        hav
2b973 65 20 6f 74 68 65 72 20 6f 70 74 69 6d 69 7a 61  e other optimiza
2b974 74 69 6f 6e 73 20 69 6e 20 6d 69 6e 64 20 74 6f  tions in mind to
2b975 20 64 65 61 6c 20 77 69 74 68 20 74 68 61 74 20   deal with that 
2b976 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  case..**.** In t
2b977 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65  his routine, the
2b978 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20 69   "p" parameter i
2b979 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
2b97a 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a  he outer query..
2b97b 2a 2a 20 54 68 65 20 73 75 62 71 75 65 72 79 20  ** The subquery 
2b97c 69 73 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 46  is p->pSrc->a[iF
2b97d 72 6f 6d 5d 2e 20 20 69 73 41 67 67 20 69 73 20  rom].  isAgg is 
2b97e 74 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65  true if the oute
2b97f 72 20 71 75 65 72 79 0a 2a 2a 20 75 73 65 73 20  r query.** uses 
2b980 61 67 67 72 65 67 61 74 65 73 20 61 6e 64 20 73  aggregates and s
2b981 75 62 71 75 65 72 79 49 73 41 67 67 20 69 73 20  ubqueryIsAgg is 
2b982 74 72 75 65 20 69 66 20 74 68 65 20 73 75 62 71  true if the subq
2b983 75 65 72 79 20 75 73 65 73 20 61 67 67 72 65 67  uery uses aggreg
2b984 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 66  ates..**.** If f
2b985 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f 74  lattening is not
2b986 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 69 73   attempted, this
2b987 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
2b988 2d 6f 70 20 61 6e 64 20 72 65 74 75 72 6e 73 20  -op and returns 
2b989 30 2e 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e  0..** If flatten
2b98a 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65 64  ing is attempted
2b98b 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
2b98c 74 75 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20 41  turns 1..**.** A
2b98d 6c 6c 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ll of the expres
2b98e 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d 75  sion analysis mu
2b98f 73 74 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74 68  st occur on both
2b990 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2b991 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62 71   and.** the subq
2b992 75 65 72 79 20 62 65 66 6f 72 65 20 74 68 69 73  uery before this
2b993 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a 2a   routine runs..*
2b994 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61  /.static int fla
2b995 74 74 65 6e 53 75 62 71 75 65 72 79 28 0a 20 20  ttenSubquery(.  
2b996 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2b997 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2b998 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
2b999 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
2b99a 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20    /* The parent 
2b99b 6f 72 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20  or outer SELECT 
2b99c 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69  statement */.  i
2b99d 6e 74 20 69 46 72 6f 6d 2c 20 20 20 20 20 20 20  nt iFrom,       
2b99e 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
2b99f 70 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20  p->pSrc->a[] of 
2b9a0 74 68 65 20 69 6e 6e 65 72 20 73 75 62 71 75 65  the inner subque
2b9a1 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67  ry */.  int isAg
2b9a2 67 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g,           /* 
2b9a3 54 72 75 65 20 69 66 20 6f 75 74 65 72 20 53 45  True if outer SE
2b9a4 4c 45 43 54 20 75 73 65 73 20 61 67 67 72 65 67  LECT uses aggreg
2b9a5 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f  ate functions */
2b9a6 0a 20 20 69 6e 74 20 73 75 62 71 75 65 72 79 49  .  int subqueryI
2b9a7 73 41 67 67 20 20 20 20 2f 2a 20 54 72 75 65 20  sAgg    /* True 
2b9a8 69 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  if the subquery 
2b9a9 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66  uses aggregate f
2b9aa 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20  unctions */.){. 
2b9ab 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61   const char *zSa
2b9ac 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  vedAuthContext =
2b9ad 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
2b9ae 6e 74 65 78 74 3b 0a 20 20 53 65 6c 65 63 74 20  ntext;.  Select 
2b9af 2a 70 50 61 72 65 6e 74 3b 0a 20 20 53 65 6c 65  *pParent;.  Sele
2b9b0 63 74 20 2a 70 53 75 62 3b 20 20 20 20 20 20 20  ct *pSub;       
2b9b1 2f 2a 20 54 68 65 20 69 6e 6e 65 72 20 71 75 65  /* The inner que
2b9b2 72 79 20 6f 72 20 22 73 75 62 71 75 65 72 79 22  ry or "subquery"
2b9b3 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
2b9b4 75 62 31 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69  ub1;      /* Poi
2b9b5 6e 74 65 72 20 74 6f 20 74 68 65 20 72 69 67 68  nter to the righ
2b9b6 74 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e 20  tmost select in 
2b9b7 73 75 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20 53  sub-query */.  S
2b9b8 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20  rcList *pSrc;   
2b9b9 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63     /* The FROM c
2b9ba 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74  lause of the out
2b9bb 65 72 20 71 75 65 72 79 20 2a 2f 0a 20 20 53 72  er query */.  Sr
2b9bc 63 4c 69 73 74 20 2a 70 53 75 62 53 72 63 3b 20  cList *pSubSrc; 
2b9bd 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
2b9be 61 75 73 65 20 6f 66 20 74 68 65 20 73 75 62 71  ause of the subq
2b9bf 75 65 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69  uery */.  ExprLi
2b9c0 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f 2a  st *pList;    /*
2b9c1 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   The result set 
2b9c2 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  of the outer que
2b9c3 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72  ry */.  int iPar
2b9c4 65 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56  ent;        /* V
2b9c5 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2b9c6 72 20 6f 66 20 74 68 65 20 70 53 75 62 20 72 65  r of the pSub re
2b9c7 73 75 6c 74 20 73 65 74 20 74 65 6d 70 20 74 61  sult set temp ta
2b9c8 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  ble */.  int i; 
2b9c9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b9ca 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
2b9cb 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20    Expr *pWhere; 
2b9cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b9cd 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
2b9ce 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75  clause */.  stru
2b9cf 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2b9d0 2a 70 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a 20  *pSubitem;   /* 
2b9d1 54 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a  The subquery */.
2b9d2 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2b9d3 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f  pParse->db;..  /
2b9d4 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
2b9d5 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20  f flattening is 
2b9d6 70 65 72 6d 69 74 74 65 64 2e 20 20 52 65 74 75  permitted.  Retu
2b9d7 72 6e 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20 2a  rn 0 if not..  *
2b9d8 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  /.  assert( p!=0
2b9d9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
2b9da 3e 70 50 72 69 6f 72 3d 3d 30 20 29 3b 20 20 2f  >pPrior==0 );  /
2b9db 2a 20 55 6e 61 62 6c 65 20 74 6f 20 66 6c 61 74  * Unable to flat
2b9dc 74 65 6e 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65  ten compound que
2b9dd 72 69 65 73 20 2a 2f 0a 20 20 70 53 72 63 20 3d  ries */.  pSrc =
2b9de 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65   p->pSrc;.  asse
2b9df 72 74 28 20 70 53 72 63 20 26 26 20 69 46 72 6f  rt( pSrc && iFro
2b9e0 6d 3e 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70 53  m>=0 && iFrom<pS
2b9e1 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 70 53  rc->nSrc );.  pS
2b9e2 75 62 69 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e  ubitem = &pSrc->
2b9e3 61 5b 69 46 72 6f 6d 5d 3b 0a 20 20 69 50 61 72  a[iFrom];.  iPar
2b9e4 65 6e 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ent = pSubitem->
2b9e5 69 43 75 72 73 6f 72 3b 0a 20 20 70 53 75 62 20  iCursor;.  pSub 
2b9e6 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c  = pSubitem->pSel
2b9e7 65 63 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ect;.  assert( p
2b9e8 53 75 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  Sub!=0 );.  if( 
2b9e9 69 73 41 67 67 20 26 26 20 73 75 62 71 75 65 72  isAgg && subquer
2b9ea 79 49 73 41 67 67 20 29 20 72 65 74 75 72 6e 20  yIsAgg ) return 
2b9eb 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2b9ec 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2b9ed 6e 20 28 31 29 20 20 2a 2f 0a 20 20 69 66 28 20  n (1)  */.  if( 
2b9ee 73 75 62 71 75 65 72 79 49 73 41 67 67 20 26 26  subqueryIsAgg &&
2b9ef 20 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 29 20   pSrc->nSrc>1 ) 
2b9f0 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
2b9f1 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2b9f2 6e 20 28 32 29 20 20 2a 2f 0a 20 20 70 53 75 62  n (2)  */.  pSub
2b9f3 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63  Src = pSub->pSrc
2b9f4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 75 62  ;.  assert( pSub
2b9f5 53 72 63 20 29 3b 0a 20 20 2f 2a 20 50 72 69 6f  Src );.  /* Prio
2b9f6 72 20 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e 31  r to version 3.1
2b9f7 2e 32 2c 20 77 68 65 6e 20 4c 49 4d 49 54 20 61  .2, when LIMIT a
2b9f8 6e 64 20 4f 46 46 53 45 54 20 68 61 64 20 74 6f  nd OFFSET had to
2b9f9 20 62 65 20 73 69 6d 70 6c 65 20 63 6f 6e 73 74   be simple const
2b9fa 61 6e 74 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20 61  ants,.  ** not a
2b9fb 72 62 69 74 72 61 72 79 20 65 78 70 72 65 73 73  rbitrary express
2b9fc 73 69 6f 6e 73 2c 20 77 65 20 61 6c 6c 6f 77 65  sions, we allowe
2b9fd 64 20 73 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e 67  d some combining
2b9fe 20 6f 66 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46   of LIMIT and OF
2b9ff 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 75 73  FSET.  ** becaus
2ba00 65 20 74 68 65 79 20 63 6f 75 6c 64 20 62 65 20  e they could be 
2ba01 63 6f 6d 70 75 74 65 64 20 61 74 20 63 6f 6d 70  computed at comp
2ba02 69 6c 65 2d 74 69 6d 65 2e 20 20 42 75 74 20 77  ile-time.  But w
2ba03 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46  hen LIMIT and OF
2ba04 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 6d 65  FSET.  ** became
2ba05 20 61 72 62 69 74 72 61 72 79 20 65 78 70 72 65   arbitrary expre
2ba06 73 73 69 6f 6e 73 2c 20 77 65 20 77 65 72 65 20  ssions, we were 
2ba07 66 6f 72 63 65 64 20 74 6f 20 61 64 64 20 72 65  forced to add re
2ba08 73 74 72 69 63 74 69 6f 6e 73 20 28 31 33 29 0a  strictions (13).
2ba09 20 20 2a 2a 20 61 6e 64 20 28 31 34 29 2e 20 2a    ** and (14). *
2ba0a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c  /.  if( pSub->pL
2ba0b 69 6d 69 74 20 26 26 20 70 2d 3e 70 4c 69 6d 69  imit && p->pLimi
2ba0c 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  t ) return 0;   
2ba0d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2ba0e 73 74 72 69 63 74 69 6f 6e 20 28 31 33 29 20 2a  striction (13) *
2ba0f 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f  /.  if( pSub->pO
2ba10 66 66 73 65 74 20 29 20 72 65 74 75 72 6e 20 30  ffset ) return 0
2ba11 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ba12 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2ba13 73 74 72 69 63 74 69 6f 6e 20 28 31 34 29 20 2a  striction (14) *
2ba14 2f 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68  /.  if( p->pRigh
2ba15 74 6d 6f 73 74 20 26 26 20 70 53 75 62 2d 3e 70  tmost && pSub->p
2ba16 4c 69 6d 69 74 20 26 26 20 70 53 75 62 2d 3e 70  Limit && pSub->p
2ba17 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72  OrderBy ){.    r
2ba18 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
2ba19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba1b 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2ba1c 6f 6e 20 28 31 35 29 20 2a 2f 0a 20 20 7d 0a 20  on (15) */.  }. 
2ba1d 20 69 66 28 20 70 53 75 62 53 72 63 2d 3e 6e 53   if( pSubSrc->nS
2ba1e 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  rc==0 ) return 0
2ba1f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ba20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
2ba21 69 63 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0a 20  iction (7)  */. 
2ba22 20 69 66 28 20 28 28 70 53 75 62 2d 3e 73 65 6c   if( ((pSub->sel
2ba23 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69  Flags & SF_Disti
2ba24 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53 75 62 2d  nct)!=0 || pSub-
2ba25 3e 70 4c 69 6d 69 74 29 20 0a 20 20 20 20 20 20  >pLimit) .      
2ba26 20 20 20 26 26 20 28 70 53 72 63 2d 3e 6e 53 72     && (pSrc->nSr
2ba27 63 3e 31 20 7c 7c 20 69 73 41 67 67 29 20 29 7b  c>1 || isAgg) ){
2ba28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
2ba29 74 72 69 63 74 69 6f 6e 73 20 28 34 29 28 35 29  trictions (4)(5)
2ba2a 28 38 29 28 39 29 20 2a 2f 0a 20 20 20 20 20 72  (8)(9) */.     r
2ba2b 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 0a  eturn 0;       .
2ba2c 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 73 65    }.  if( (p->se
2ba2d 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
2ba2e 69 6e 63 74 29 21 3d 30 20 26 26 20 73 75 62 71  inct)!=0 && subq
2ba2f 75 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20  ueryIsAgg ){.   
2ba30 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20    return 0;     
2ba31 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2ba32 6f 6e 20 28 36 29 20 20 2a 2f 0a 20 20 7d 0a 20  on (6)  */.  }. 
2ba33 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79   if( p->pOrderBy
2ba34 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72   && pSub->pOrder
2ba35 42 79 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72  By ){.     retur
2ba36 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
2ba37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ba39 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31  * Restriction (1
2ba3a 31 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  1) */.  }.  if( 
2ba3b 69 73 41 67 67 20 26 26 20 70 53 75 62 2d 3e 70  isAgg && pSub->p
2ba3c 4f 72 64 65 72 42 79 20 29 20 72 65 74 75 72 6e  OrderBy ) return
2ba3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2ba3e 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2ba3f 6e 20 28 31 36 29 20 2a 2f 0a 20 20 69 66 28 20  n (16) */.  if( 
2ba40 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20  pSub->pLimit && 
2ba41 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 75  p->pWhere ) retu
2ba42 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
2ba43 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2ba44 6e 20 28 31 39 29 20 2a 2f 0a 0a 20 20 2f 2a 20  n (19) */..  /* 
2ba45 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54  OBSOLETE COMMENT
2ba46 20 31 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69 63   1:.  ** Restric
2ba47 74 69 6f 6e 20 33 3a 20 20 49 66 20 74 68 65 20  tion 3:  If the 
2ba48 73 75 62 71 75 65 72 79 20 69 73 20 61 20 6a 6f  subquery is a jo
2ba49 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68  in, make sure th
2ba4a 65 20 73 75 62 71 75 65 72 79 20 69 73 20 0a 20  e subquery is . 
2ba4b 20 2a 2a 20 6e 6f 74 20 75 73 65 64 20 61 73 20   ** not used as 
2ba4c 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  the right operan
2ba4d 64 20 6f 66 20 61 6e 20 6f 75 74 65 72 20 6a 6f  d of an outer jo
2ba4e 69 6e 2e 20 20 45 78 61 6d 70 6c 65 73 20 6f 66  in.  Examples of
2ba4f 20 77 68 79 20 74 68 69 73 0a 20 20 2a 2a 20 69   why this.  ** i
2ba50 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20  s not allowed:. 
2ba51 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
2ba52 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a   t1 LEFT OUTER J
2ba53 4f 49 4e 20 28 74 32 20 4a 4f 49 4e 20 74 33 29  OIN (t2 JOIN t3)
2ba54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65  .  **.  ** If we
2ba55 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62 6f   flatten the abo
2ba56 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65 74  ve, we would get
2ba57 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
2ba58 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55 54 45     (t1 LEFT OUTE
2ba59 52 20 4a 4f 49 4e 20 74 32 29 20 4a 4f 49 4e 20  R JOIN t2) JOIN 
2ba5a 74 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20 77 68 69  t3.  **.  ** whi
2ba5b 63 68 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c  ch is not at all
2ba5c 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e   the same thing.
2ba5d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 42 53 4f 4c  .  **.  ** OBSOL
2ba5e 45 54 45 20 43 4f 4d 4d 45 4e 54 20 32 3a 0a 20  ETE COMMENT 2:. 
2ba5f 20 2a 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   ** Restriction 
2ba60 31 32 3a 20 20 49 66 20 74 68 65 20 73 75 62 71  12:  If the subq
2ba61 75 65 72 79 20 69 73 20 74 68 65 20 72 69 67 68  uery is the righ
2ba62 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 6c  t operand of a l
2ba63 65 66 74 20 6f 75 74 65 72 0a 20 20 2a 2a 20 6a  eft outer.  ** j
2ba64 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  oin, make sure t
2ba65 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73 20  he subquery has 
2ba66 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  no WHERE clause.
2ba67 0a 20 20 2a 2a 20 41 6e 20 65 78 61 6d 70 6c 65  .  ** An example
2ba68 73 20 6f 66 20 77 68 79 20 74 68 69 73 20 69 73  s of why this is
2ba69 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20 20   not allowed:.  
2ba6a 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
2ba6b 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f  t1 LEFT OUTER JO
2ba6c 49 4e 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f  IN (SELECT * FRO
2ba6d 4d 20 74 32 20 57 48 45 52 45 20 74 32 2e 78 3e  M t2 WHERE t2.x>
2ba6e 30 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  0).  **.  ** If 
2ba6f 77 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61  we flatten the a
2ba70 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67  bove, we would g
2ba71 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  et.  **.  **    
2ba72 20 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55       (t1 LEFT OU
2ba73 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 57 48 45  TER JOIN t2) WHE
2ba74 52 45 20 74 32 2e 78 3e 30 0a 20 20 2a 2a 0a 20  RE t2.x>0.  **. 
2ba75 20 2a 2a 20 42 75 74 20 74 68 65 20 74 32 2e 78   ** But the t2.x
2ba76 3e 30 20 74 65 73 74 20 77 69 6c 6c 20 61 6c 77  >0 test will alw
2ba77 61 79 73 20 66 61 69 6c 20 6f 6e 20 61 20 4e 55  ays fail on a NU
2ba78 4c 4c 20 72 6f 77 20 6f 66 20 74 32 2c 20 77 68  LL row of t2, wh
2ba79 69 63 68 0a 20 20 2a 2a 20 65 66 66 65 63 74 69  ich.  ** effecti
2ba7a 76 65 6c 79 20 63 6f 6e 76 65 72 74 73 20 74 68  vely converts th
2ba7b 65 20 4f 55 54 45 52 20 4a 4f 49 4e 20 69 6e 74  e OUTER JOIN int
2ba7c 6f 20 61 6e 20 49 4e 4e 45 52 20 4a 4f 49 4e 2e  o an INNER JOIN.
2ba7d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 48 49 53 20  .  **.  ** THIS 
2ba7e 4f 56 45 52 52 49 44 45 53 20 4f 42 53 4f 4c 45  OVERRIDES OBSOLE
2ba7f 54 45 20 43 4f 4d 4d 45 4e 54 53 20 31 20 41 4e  TE COMMENTS 1 AN
2ba80 44 20 32 20 41 42 4f 56 45 3a 0a 20 20 2a 2a 20  D 2 ABOVE:.  ** 
2ba81 54 69 63 6b 65 74 20 23 33 33 30 30 20 73 68 6f  Ticket #3300 sho
2ba82 77 73 20 74 68 61 74 20 66 6c 61 74 74 65 6e 69  ws that flatteni
2ba83 6e 67 20 74 68 65 20 72 69 67 68 74 20 74 65 72  ng the right ter
2ba84 6d 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e  m of a LEFT JOIN
2ba85 0a 20 20 2a 2a 20 69 73 20 66 72 61 75 67 68 74  .  ** is fraught
2ba86 20 77 69 74 68 20 64 61 6e 67 65 72 2e 20 20 42   with danger.  B
2ba87 65 73 74 20 74 6f 20 61 76 6f 69 64 20 74 68 65  est to avoid the
2ba88 20 77 68 6f 6c 65 20 74 68 69 6e 67 2e 20 20 49   whole thing.  I
2ba89 66 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75  f the.  ** subqu
2ba8a 65 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74  ery is the right
2ba8b 20 74 65 72 6d 20 6f 66 20 61 20 4c 45 46 54 20   term of a LEFT 
2ba8c 4a 4f 49 4e 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  JOIN, then do no
2ba8d 74 20 66 6c 61 74 74 65 6e 2e 0a 20 20 2a 2f 0a  t flatten..  */.
2ba8e 20 20 69 66 28 20 28 70 53 75 62 69 74 65 6d 2d    if( (pSubitem-
2ba8f 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f  >jointype & JT_O
2ba90 55 54 45 52 29 21 3d 30 20 29 7b 0a 20 20 20 20  UTER)!=0 ){.    
2ba91 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
2ba92 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2ba93 31 37 3a 20 49 66 20 74 68 65 20 73 75 62 2d 71  17: If the sub-q
2ba94 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75  uery is a compou
2ba95 6e 64 20 53 45 4c 45 43 54 2c 20 74 68 65 6e 20  nd SELECT, then 
2ba96 69 74 20 6d 75 73 74 0a 20 20 2a 2a 20 75 73 65  it must.  ** use
2ba97 20 6f 6e 6c 79 20 74 68 65 20 55 4e 49 4f 4e 20   only the UNION 
2ba98 41 4c 4c 20 6f 70 65 72 61 74 6f 72 2e 20 41 6e  ALL operator. An
2ba99 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 69  d none of the si
2ba9a 6d 70 6c 65 20 73 65 6c 65 63 74 20 71 75 65 72  mple select quer
2ba9b 69 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 6d 61  ies.  ** that ma
2ba9c 6b 65 20 75 70 20 74 68 65 20 63 6f 6d 70 6f 75  ke up the compou
2ba9d 6e 64 20 53 45 4c 45 43 54 20 61 72 65 20 61 6c  nd SELECT are al
2ba9e 6c 6f 77 65 64 20 74 6f 20 62 65 20 61 67 67 72  lowed to be aggr
2ba9f 65 67 61 74 65 20 6f 72 20 64 69 73 74 69 6e 63  egate or distinc
2baa0 74 0a 20 20 2a 2a 20 71 75 65 72 69 65 73 2e 0a  t.  ** queries..
2baa1 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d    */.  if( pSub-
2baa2 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 69  >pPrior ){.    i
2baa3 66 28 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42  f( pSub->pOrderB
2baa4 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  y ){.      retur
2baa5 6e 20 30 3b 20 20 2f 2a 20 52 65 73 74 72 69 63  n 0;  /* Restric
2baa6 74 69 6f 6e 20 32 30 20 2a 2f 0a 20 20 20 20 7d  tion 20 */.    }
2baa7 0a 20 20 20 20 69 66 28 20 69 73 41 67 67 20 7c  .    if( isAgg |
2baa8 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  | (p->selFlags &
2baa9 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30   SF_Distinct)!=0
2baaa 20 7c 7c 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d   || pSrc->nSrc!=
2baab 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  1 ){.      retur
2baac 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  n 0;.    }.    f
2baad 6f 72 28 70 53 75 62 31 3d 70 53 75 62 3b 20 70  or(pSub1=pSub; p
2baae 53 75 62 31 3b 20 70 53 75 62 31 3d 70 53 75 62  Sub1; pSub1=pSub
2baaf 31 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20  1->pPrior){.    
2bab0 20 20 74 65 73 74 63 61 73 65 28 20 28 70 53 75    testcase( (pSu
2bab1 62 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28  b1->selFlags & (
2bab2 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41  SF_Distinct|SF_A
2bab3 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44  ggregate))==SF_D
2bab4 69 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 20  istinct );.     
2bab5 20 74 65 73 74 63 61 73 65 28 20 28 70 53 75 62   testcase( (pSub
2bab6 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53  1->selFlags & (S
2bab7 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67  F_Distinct|SF_Ag
2bab8 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 41 67  gregate))==SF_Ag
2bab9 67 72 65 67 61 74 65 20 29 3b 0a 20 20 20 20 20  gregate );.     
2baba 20 69 66 28 20 28 70 53 75 62 31 2d 3e 73 65 6c   if( (pSub1->sel
2babb 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74  Flags & (SF_Dist
2babc 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74  inct|SF_Aggregat
2babd 65 29 29 21 3d 30 0a 20 20 20 20 20 20 20 7c 7c  e))!=0.       ||
2babe 20 28 70 53 75 62 31 2d 3e 70 50 72 69 6f 72 20   (pSub1->pPrior 
2babf 26 26 20 70 53 75 62 31 2d 3e 6f 70 21 3d 54 4b  && pSub1->op!=TK
2bac0 5f 41 4c 4c 29 20 0a 20 20 20 20 20 20 20 7c 7c  _ALL) .       ||
2bac1 20 4e 45 56 45 52 28 70 53 75 62 31 2d 3e 70 53   NEVER(pSub1->pS
2bac2 72 63 3d 3d 30 29 20 7c 7c 20 70 53 75 62 31 2d  rc==0) || pSub1-
2bac3 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 0a 20  >pSrc->nSrc!=1. 
2bac4 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
2bac5 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2bac6 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
2bac7 52 65 73 74 72 69 63 74 69 6f 6e 20 31 38 2e 20  Restriction 18. 
2bac8 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4f  */.    if( p->pO
2bac9 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20  rderBy ){.      
2baca 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 66 6f  int ii;.      fo
2bacb 72 28 69 69 3d 30 3b 20 69 69 3c 70 2d 3e 70 4f  r(ii=0; ii<p->pO
2bacc 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
2bacd 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  i++){.        if
2bace 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 61  ( p->pOrderBy->a
2bacf 5b 69 69 5d 2e 69 43 6f 6c 3d 3d 30 20 29 20 72  [ii].iCol==0 ) r
2bad0 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
2bad1 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2bad2 2a 2a 2a 2a 20 49 66 20 77 65 20 72 65 61 63 68  **** If we reach
2bad3 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 66 6c 61   this point, fla
2bad4 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69  ttening is permi
2bad5 74 74 65 64 2e 20 2a 2a 2a 2a 2a 2f 0a 0a 20 20  tted. *****/..  
2bad6 2f 2a 20 41 75 74 68 6f 72 69 7a 65 20 74 68 65  /* Authorize the
2bad7 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70   subquery */.  p
2bad8 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
2bad9 65 78 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ext = pSubitem->
2bada 7a 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65 33  zName;.  sqlite3
2badb 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2badc 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c  , SQLITE_SELECT,
2badd 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 70 50 61   0, 0, 0);.  pPa
2bade 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
2badf 74 20 3d 20 7a 53 61 76 65 64 41 75 74 68 43 6f  t = zSavedAuthCo
2bae0 6e 74 65 78 74 3b 0a 0a 20 20 2f 2a 20 49 66 20  ntext;..  /* If 
2bae1 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73  the sub-query is
2bae2 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
2bae3 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  CT statement, th
2bae4 65 6e 20 28 62 79 20 72 65 73 74 72 69 63 74 69  en (by restricti
2bae5 6f 6e 73 0a 20 20 2a 2a 20 31 37 20 61 6e 64 20  ons.  ** 17 and 
2bae6 31 38 20 61 62 6f 76 65 29 20 69 74 20 6d 75 73  18 above) it mus
2bae7 74 20 62 65 20 61 20 55 4e 49 4f 4e 20 41 4c 4c  t be a UNION ALL
2bae8 20 61 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20   and the parent 
2bae9 71 75 65 72 79 20 6d 75 73 74 20 0a 20 20 2a 2a  query must .  **
2baea 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a   be of the form:
2baeb 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53  .  **.  **     S
2baec 45 4c 45 43 54 20 3c 65 78 70 72 2d 6c 69 73 74  ELECT <expr-list
2baed 3e 20 46 52 4f 4d 20 28 3c 73 75 62 2d 71 75 65  > FROM (<sub-que
2baee 72 79 3e 29 20 3c 77 68 65 72 65 2d 63 6c 61 75  ry>) <where-clau
2baef 73 65 3e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 66  se> .  **.  ** f
2baf0 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 79 20 4f  ollowed by any O
2baf1 52 44 45 52 20 42 59 2c 20 4c 49 4d 49 54 20 61  RDER BY, LIMIT a
2baf2 6e 64 2f 6f 72 20 4f 46 46 53 45 54 20 63 6c 61  nd/or OFFSET cla
2baf3 75 73 65 73 2e 20 54 68 69 73 20 62 6c 6f 63 6b  uses. This block
2baf4 0a 20 20 2a 2a 20 63 72 65 61 74 65 73 20 4e 2d  .  ** creates N-
2baf5 31 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20  1 copies of the 
2baf6 70 61 72 65 6e 74 20 71 75 65 72 79 20 77 69 74  parent query wit
2baf7 68 6f 75 74 20 61 6e 79 20 4f 52 44 45 52 20 42  hout any ORDER B
2baf8 59 2c 20 4c 49 4d 49 54 20 6f 72 20 0a 20 20 2a  Y, LIMIT or .  *
2baf9 2a 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73  * OFFSET clauses
2bafa 20 61 6e 64 20 6a 6f 69 6e 73 20 74 68 65 6d 20   and joins them 
2bafb 74 6f 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64  to the left-hand
2bafc 2d 73 69 64 65 20 6f 66 20 74 68 65 20 6f 72 69  -side of the ori
2bafd 67 69 6e 61 6c 0a 20 20 2a 2a 20 75 73 69 6e 67  ginal.  ** using
2bafe 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61   UNION ALL opera
2baff 74 6f 72 73 2e 20 49 6e 20 74 68 69 73 20 63 61  tors. In this ca
2bb00 73 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62  se N is the numb
2bb01 65 72 20 6f 66 20 73 69 6d 70 6c 65 0a 20 20 2a  er of simple.  *
2bb02 2a 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  * select stateme
2bb03 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 6f  nts in the compo
2bb04 75 6e 64 20 73 75 62 2d 71 75 65 72 79 2e 0a 20  und sub-query.. 
2bb05 20 2a 2a 0a 20 20 2a 2a 20 45 78 61 6d 70 6c 65   **.  ** Example
2bb06 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2bb07 53 45 4c 45 43 54 20 61 2b 31 20 46 52 4f 4d 20  SELECT a+1 FROM 
2bb08 28 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45  (.  **        SE
2bb09 4c 45 43 54 20 78 20 46 52 4f 4d 20 74 61 62 0a  LECT x FROM tab.
2bb0a 20 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49 4f    **        UNIO
2bb0b 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20  N ALL.  **      
2bb0c 20 20 53 45 4c 45 43 54 20 79 20 46 52 4f 4d 20    SELECT y FROM 
2bb0d 74 61 62 0a 20 20 2a 2a 20 20 20 20 20 20 20 20  tab.  **        
2bb0e 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20  UNION ALL.  **  
2bb0f 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 62 73        SELECT abs
2bb10 28 7a 2a 32 29 20 46 52 4f 4d 20 74 61 62 32 0a  (z*2) FROM tab2.
2bb11 20 20 2a 2a 20 20 20 20 20 29 20 57 48 45 52 45    **     ) WHERE
2bb12 20 61 21 3d 35 20 4f 52 44 45 52 20 42 59 20 31   a!=5 ORDER BY 1
2bb13 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 72 61 6e 73  .  **.  ** Trans
2bb14 66 6f 72 6d 65 64 20 69 6e 74 6f 3a 0a 20 20 2a  formed into:.  *
2bb15 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43  *.  **     SELEC
2bb16 54 20 78 2b 31 20 46 52 4f 4d 20 74 61 62 20 57  T x+1 FROM tab W
2bb17 48 45 52 45 20 78 2b 31 21 3d 35 0a 20 20 2a 2a  HERE x+1!=5.  **
2bb18 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20       UNION ALL. 
2bb19 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 79   **     SELECT y
2bb1a 2b 31 20 46 52 4f 4d 20 74 61 62 20 57 48 45 52  +1 FROM tab WHER
2bb1b 45 20 79 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20  E y+1!=5.  **   
2bb1c 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a    UNION ALL.  **
2bb1d 20 20 20 20 20 53 45 4c 45 43 54 20 61 62 73 28       SELECT abs(
2bb1e 7a 2a 32 29 2b 31 20 46 52 4f 4d 20 74 61 62 32  z*2)+1 FROM tab2
2bb1f 20 57 48 45 52 45 20 61 62 73 28 7a 2a 32 29 2b   WHERE abs(z*2)+
2bb20 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 4f 52  1!=5.  **     OR
2bb21 44 45 52 20 42 59 20 31 0a 20 20 2a 2a 0a 20 20  DER BY 1.  **.  
2bb22 2a 2a 20 57 65 20 63 61 6c 6c 20 74 68 69 73 20  ** We call this 
2bb23 74 68 65 20 22 63 6f 6d 70 6f 75 6e 64 2d 73 75  the "compound-su
2bb24 62 71 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e  bquery flattenin
2bb25 67 22 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70  g"..  */.  for(p
2bb26 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72  Sub=pSub->pPrior
2bb27 3b 20 70 53 75 62 3b 20 70 53 75 62 3d 70 53 75  ; pSub; pSub=pSu
2bb28 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20  b->pPrior){.    
2bb29 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20  Select *pNew;.  
2bb2a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
2bb2b 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
2bb2c 42 79 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4c  By;.    Expr *pL
2bb2d 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74  imit = p->pLimit
2bb2e 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 50  ;.    Select *pP
2bb2f 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72  rior = p->pPrior
2bb30 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42  ;.    p->pOrderB
2bb31 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 53  y = 0;.    p->pS
2bb32 72 63 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70  rc = 0;.    p->p
2bb33 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 70  Prior = 0;.    p
2bb34 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->pLimit = 0;.  
2bb35 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
2bb36 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2c  SelectDup(db, p,
2bb37 20 30 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 69 6d   0);.    p->pLim
2bb38 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20  it = pLimit;.   
2bb39 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70   p->pOrderBy = p
2bb3a 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 70 2d 3e  OrderBy;.    p->
2bb3b 70 53 72 63 20 3d 20 70 53 72 63 3b 0a 20 20 20  pSrc = pSrc;.   
2bb3c 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 41 4c 4c 3b   p->op = TK_ALL;
2bb3d 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f  .    p->pRightmo
2bb3e 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  st = 0;.    if( 
2bb3f 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pNew==0 ){.     
2bb40 20 70 4e 65 77 20 3d 20 70 50 72 69 6f 72 3b 0a   pNew = pPrior;.
2bb41 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2bb42 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20   pNew->pPrior = 
2bb43 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 4e  pPrior;.      pN
2bb44 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d  ew->pRightmost =
2bb45 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d   0;.    }.    p-
2bb46 3e 70 50 72 69 6f 72 20 3d 20 70 4e 65 77 3b 0a  >pPrior = pNew;.
2bb47 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
2bb48 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
2bb49 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42  n 1;.  }..  /* B
2bb4a 65 67 69 6e 20 66 6c 61 74 74 65 6e 69 6e 67 20  egin flattening 
2bb4b 74 68 65 20 69 46 72 6f 6d 2d 74 68 20 65 6e 74  the iFrom-th ent
2bb4c 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  ry of the FROM c
2bb4d 6c 61 75 73 65 20 0a 20 20 2a 2a 20 69 6e 20 74  lause .  ** in t
2bb4e 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a  he outer query..
2bb4f 20 20 2a 2f 0a 20 20 70 53 75 62 20 3d 20 70 53    */.  pSub = pS
2bb50 75 62 31 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ub1 = pSubitem->
2bb51 70 53 65 6c 65 63 74 3b 0a 0a 20 20 2f 2a 20 44  pSelect;..  /* D
2bb52 65 6c 65 74 65 20 74 68 65 20 74 72 61 6e 73 69  elete the transi
2bb53 65 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63 74  ent table struct
2bb54 75 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ure associated w
2bb55 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 75 62  ith the.  ** sub
2bb56 71 75 65 72 79 0a 20 20 2a 2f 0a 20 20 73 71 6c  query.  */.  sql
2bb57 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2bb58 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61  Subitem->zDataba
2bb59 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  se);.  sqlite3Db
2bb5a 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74 65  Free(db, pSubite
2bb5b 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  m->zName);.  sql
2bb5c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2bb5d 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73 29  Subitem->zAlias)
2bb5e 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 44  ;.  pSubitem->zD
2bb5f 61 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 70  atabase = 0;.  p
2bb60 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d  Subitem->zName =
2bb61 20 30 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e   0;.  pSubitem->
2bb62 7a 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 70 53  zAlias = 0;.  pS
2bb63 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20  ubitem->pSelect 
2bb64 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 65 66 65 72  = 0;..  /* Defer
2bb65 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54 61   deleting the Ta
2bb66 62 6c 65 20 6f 62 6a 65 63 74 20 61 73 73 6f 63  ble object assoc
2bb67 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 20  iated with the. 
2bb68 20 2a 2a 20 73 75 62 71 75 65 72 79 20 75 6e 74   ** subquery unt
2bb69 69 6c 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69  il code generati
2bb6a 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 70 6c  on is.  ** compl
2bb6b 65 74 65 2c 20 73 69 6e 63 65 20 74 68 65 72 65  ete, since there
2bb6c 20 6d 61 79 20 73 74 69 6c 6c 20 65 78 69 73 74   may still exist
2bb6d 20 45 78 70 72 2e 70 54 61 62 20 65 6e 74 72 69   Expr.pTab entri
2bb6e 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 72 65 66  es that.  ** ref
2bb6f 65 72 20 74 6f 20 74 68 65 20 73 75 62 71 75 65  er to the subque
2bb70 72 79 20 65 76 65 6e 20 61 66 74 65 72 20 66 6c  ry even after fl
2bb71 61 74 74 65 6e 69 6e 67 2e 20 20 54 69 63 6b 65  attening.  Ticke
2bb72 74 20 23 33 33 34 36 2e 0a 20 20 2a 2a 0a 20 20  t #3346..  **.  
2bb73 2a 2a 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61  ** pSubitem->pTa
2bb74 62 20 69 73 20 61 6c 77 61 79 73 20 6e 6f 6e 2d  b is always non-
2bb75 4e 55 4c 4c 20 62 79 20 74 65 73 74 20 72 65 73  NULL by test res
2bb76 74 72 69 63 74 69 6f 6e 73 20 61 6e 64 20 74 65  trictions and te
2bb77 73 74 73 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a  sts above..  */.
2bb78 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53 75    if( ALWAYS(pSu
2bb79 62 69 74 65 6d 2d 3e 70 54 61 62 21 3d 30 29 20  bitem->pTab!=0) 
2bb7a 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
2bb7b 61 62 54 6f 44 65 6c 20 3d 20 70 53 75 62 69 74  abToDel = pSubit
2bb7c 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 66  em->pTab;.    if
2bb7d 28 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52 65  ( pTabToDel->nRe
2bb7e 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 50 61  f==1 ){.      Pa
2bb7f 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d  rse *pToplevel =
2bb80 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70   sqlite3ParseTop
2bb81 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20  level(pParse);. 
2bb82 20 20 20 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e       pTabToDel->
2bb83 70 4e 65 78 74 5a 6f 6d 62 69 65 20 3d 20 70 54  pNextZombie = pT
2bb84 6f 70 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65  oplevel->pZombie
2bb85 54 61 62 3b 0a 20 20 20 20 20 20 70 54 6f 70 6c  Tab;.      pTopl
2bb86 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61 62  evel->pZombieTab
2bb87 20 3d 20 70 54 61 62 54 6f 44 65 6c 3b 0a 20 20   = pTabToDel;.  
2bb88 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2bb89 54 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 2d 2d  TabToDel->nRef--
2bb8a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 75 62  ;.    }.    pSub
2bb8b 69 74 65 6d 2d 3e 70 54 61 62 20 3d 20 30 3b 0a  item->pTab = 0;.
2bb8c 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f    }..  /* The fo
2bb8d 6c 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20 72 75 6e  llowing loop run
2bb8e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  s once for each 
2bb8f 74 65 72 6d 20 69 6e 20 61 20 63 6f 6d 70 6f 75  term in a compou
2bb90 6e 64 2d 73 75 62 71 75 65 72 79 0a 20 20 2a 2a  nd-subquery.  **
2bb91 20 66 6c 61 74 74 65 6e 69 6e 67 20 28 61 73 20   flattening (as 
2bb92 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 29  described above)
2bb93 2e 20 20 49 66 20 77 65 20 61 72 65 20 64 6f 69  .  If we are doi
2bb94 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 20 6b  ng a different k
2bb95 69 6e 64 0a 20 20 2a 2a 20 6f 66 20 66 6c 61 74  ind.  ** of flat
2bb96 74 65 6e 69 6e 67 20 2d 20 61 20 66 6c 61 74 74  tening - a flatt
2bb97 65 6e 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  ening other than
2bb98 20 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71   a compound-subq
2bb99 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e 67 20  uery flattening 
2bb9a 2d 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 69 73  -.  ** then this
2bb9b 20 6c 6f 6f 70 20 6f 6e 6c 79 20 72 75 6e 73 20   loop only runs 
2bb9c 6f 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  once..  **.  ** 
2bb9d 54 68 69 73 20 6c 6f 6f 70 20 6d 6f 76 65 73 20  This loop moves 
2bb9e 61 6c 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  all of the FROM 
2bb9f 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
2bba0 73 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74 68  subquery into th
2bba1 65 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20  e.  ** the FROM 
2bba2 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75  clause of the ou
2bba3 74 65 72 20 71 75 65 72 79 2e 20 20 42 65 66 6f  ter query.  Befo
2bba4 72 65 20 64 6f 69 6e 67 20 74 68 69 73 2c 20 72  re doing this, r
2bba5 65 6d 65 6d 62 65 72 0a 20 20 2a 2a 20 74 68 65  emember.  ** the
2bba6 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   cursor number f
2bba7 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  or the original 
2bba8 6f 75 74 65 72 20 71 75 65 72 79 20 46 52 4f 4d  outer query FROM
2bba9 20 65 6c 65 6d 65 6e 74 20 69 6e 0a 20 20 2a 2a   element in.  **
2bbaa 20 69 50 61 72 65 6e 74 2e 20 20 54 68 65 20 69   iParent.  The i
2bbab 50 61 72 65 6e 74 20 63 75 72 73 6f 72 20 77 69  Parent cursor wi
2bbac 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  ll never be used
2bbad 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 6f  .  Subsequent co
2bbae 64 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61  de.  ** will sca
2bbaf 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 6f  n expressions lo
2bbb0 6f 6b 69 6e 67 20 66 6f 72 20 69 50 61 72 65 6e  oking for iParen
2bbb1 74 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e 64  t references and
2bbb2 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 74 68   replace.  ** th
2bbb3 6f 73 65 20 72 65 66 65 72 65 6e 63 65 73 20 77  ose references w
2bbb4 69 74 68 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ith expressions 
2bbb5 74 68 61 74 20 72 65 73 6f 6c 76 65 20 74 6f 20  that resolve to 
2bbb6 74 68 65 20 73 75 62 71 75 65 72 79 20 46 52 4f  the subquery FRO
2bbb7 4d 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20  M.  ** elements 
2bbb8 77 65 20 61 72 65 20 6e 6f 77 20 63 6f 70 79 69  we are now copyi
2bbb9 6e 67 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f  ng in..  */.  fo
2bbba 72 28 70 50 61 72 65 6e 74 3d 70 3b 20 70 50 61  r(pParent=p; pPa
2bbbb 72 65 6e 74 3b 20 70 50 61 72 65 6e 74 3d 70 50  rent; pParent=pP
2bbbc 61 72 65 6e 74 2d 3e 70 50 72 69 6f 72 2c 20 70  arent->pPrior, p
2bbbd 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72  Sub=pSub->pPrior
2bbbe 29 7b 0a 20 20 20 20 69 6e 74 20 6e 53 75 62 53  ){.    int nSubS
2bbbf 72 63 3b 0a 20 20 20 20 75 38 20 6a 6f 69 6e 74  rc;.    u8 joint
2bbc0 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 53 75  ype = 0;.    pSu
2bbc1 62 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72  bSrc = pSub->pSr
2bbc2 63 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63  c;     /* FROM c
2bbc3 6c 61 75 73 65 20 6f 66 20 73 75 62 71 75 65 72  lause of subquer
2bbc4 79 20 2a 2f 0a 20 20 20 20 6e 53 75 62 53 72 63  y */.    nSubSrc
2bbc5 20 3d 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63   = pSubSrc->nSrc
2bbc6 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  ;  /* Number of 
2bbc7 74 65 72 6d 73 20 69 6e 20 73 75 62 71 75 65 72  terms in subquer
2bbc8 79 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  y FROM clause */
2bbc9 0a 20 20 20 20 70 53 72 63 20 3d 20 70 50 61 72  .    pSrc = pPar
2bbca 65 6e 74 2d 3e 70 53 72 63 3b 20 20 20 20 20 2f  ent->pSrc;     /
2bbcb 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  * FROM clause of
2bbcc 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2bbcd 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20 70 53 72   */..    if( pSr
2bbce 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  c ){.      asser
2bbcf 74 28 20 70 50 61 72 65 6e 74 3d 3d 70 20 29 3b  t( pParent==p );
2bbd0 20 20 2f 2a 20 46 69 72 73 74 20 74 69 6d 65 20    /* First time 
2bbd1 74 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70  through the loop
2bbd2 20 2a 2f 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79   */.      jointy
2bbd3 70 65 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 6a  pe = pSubitem->j
2bbd4 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 65 6c  ointype;.    }el
2bbd5 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
2bbd6 28 20 70 50 61 72 65 6e 74 21 3d 70 20 29 3b 20  ( pParent!=p ); 
2bbd7 20 2f 2a 20 32 6e 64 20 61 6e 64 20 73 75 62 73   /* 2nd and subs
2bbd8 65 71 75 65 6e 74 20 74 69 6d 65 73 20 74 68 72  equent times thr
2bbd9 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  ough the loop */
2bbda 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 50  .      pSrc = pP
2bbdb 61 72 65 6e 74 2d 3e 70 53 72 63 20 3d 20 73 71  arent->pSrc = sq
2bbdc 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
2bbdd 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b  nd(db, 0, 0, 0);
2bbde 0a 20 20 20 20 20 20 69 66 28 20 70 53 72 63 3d  .      if( pSrc=
2bbdf 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  =0 ){.        as
2bbe0 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63  sert( db->malloc
2bbe1 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20  Failed );.      
2bbe2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2bbe3 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
2bbe4 68 65 20 73 75 62 71 75 65 72 79 20 75 73 65 73  he subquery uses
2bbe5 20 61 20 73 69 6e 67 6c 65 20 73 6c 6f 74 20 6f   a single slot o
2bbe6 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
2bbe7 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 0a 20  e of the outer. 
2bbe8 20 20 20 2a 2a 20 71 75 65 72 79 2e 20 20 49 66     ** query.  If
2bbe9 20 74 68 65 20 73 75 62 71 75 65 72 79 20 68 61   the subquery ha
2bbea 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  s more than one 
2bbeb 65 6c 65 6d 65 6e 74 20 69 6e 20 69 74 73 20 46  element in its F
2bbec 52 4f 4d 20 63 6c 61 75 73 65 2c 0a 20 20 20 20  ROM clause,.    
2bbed 2a 2a 20 74 68 65 6e 20 65 78 70 61 6e 64 20 74  ** then expand t
2bbee 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 74  he outer query t
2bbef 6f 20 6d 61 6b 65 20 73 70 61 63 65 20 66 6f 72  o make space for
2bbf0 20 69 74 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20   it to hold all 
2bbf1 65 6c 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a 20  elements.    ** 
2bbf2 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  of the subquery.
2bbf3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45  .    **.    ** E
2bbf4 78 61 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20  xample:.    **. 
2bbf5 20 20 20 2a 2a 20 20 20 20 53 45 4c 45 43 54 20     **    SELECT 
2bbf6 2a 20 46 52 4f 4d 20 74 61 62 41 2c 20 28 53 45  * FROM tabA, (SE
2bbf7 4c 45 43 54 20 2a 20 46 52 4f 4d 20 73 75 62 31  LECT * FROM sub1
2bbf8 2c 20 73 75 62 32 29 2c 20 74 61 62 42 3b 0a 20  , sub2), tabB;. 
2bbf9 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
2bbfa 20 6f 75 74 65 72 20 71 75 65 72 79 20 68 61 73   outer query has
2bbfb 20 33 20 73 6c 6f 74 73 20 69 6e 20 69 74 73 20   3 slots in its 
2bbfc 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f 6e  FROM clause.  On
2bbfd 65 20 73 6c 6f 74 20 6f 66 20 74 68 65 0a 20 20  e slot of the.  
2bbfe 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79    ** outer query
2bbff 20 28 74 68 65 20 6d 69 64 64 6c 65 20 73 6c 6f   (the middle slo
2bc00 74 29 20 69 73 20 75 73 65 64 20 62 79 20 74 68  t) is used by th
2bc01 65 20 73 75 62 71 75 65 72 79 2e 20 20 54 68 65  e subquery.  The
2bc02 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 62 6c 6f   next.    ** blo
2bc03 63 6b 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20  ck of code will 
2bc04 65 78 70 61 6e 64 20 74 68 65 20 6f 75 74 20 71  expand the out q
2bc05 75 65 72 79 20 74 6f 20 34 20 73 6c 6f 74 73 2e  uery to 4 slots.
2bc06 20 20 54 68 65 20 6d 69 64 64 6c 65 0a 20 20 20    The middle.   
2bc07 20 2a 2a 20 73 6c 6f 74 20 69 73 20 65 78 70 61   ** slot is expa
2bc08 6e 64 65 64 20 74 6f 20 74 77 6f 20 73 6c 6f 74  nded to two slot
2bc09 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61  s in order to ma
2bc0a 6b 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  ke space for the
2bc0b 0a 20 20 20 20 2a 2a 20 74 77 6f 20 65 6c 65 6d  .    ** two elem
2bc0c 65 6e 74 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  ents in the FROM
2bc0d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73   clause of the s
2bc0e 75 62 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f 0a  ubquery..    */.
2bc0f 20 20 20 20 69 66 28 20 6e 53 75 62 53 72 63 3e      if( nSubSrc>
2bc10 31 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 65  1 ){.      pPare
2bc11 6e 74 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 20  nt->pSrc = pSrc 
2bc12 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
2bc13 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53 72 63  Enlarge(db, pSrc
2bc14 2c 20 6e 53 75 62 53 72 63 2d 31 2c 69 46 72 6f  , nSubSrc-1,iFro
2bc15 6d 2b 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20  m+1);.      if( 
2bc16 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2bc17 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
2bc18 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
2bc19 0a 0a 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 65  ..    /* Transfe
2bc1a 72 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  r the FROM claus
2bc1b 65 20 74 65 72 6d 73 20 66 72 6f 6d 20 74 68 65  e terms from the
2bc1c 20 73 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74   subquery into t
2bc1d 68 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20  he.    ** outer 
2bc1e 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  query..    */.  
2bc1f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 75    for(i=0; i<nSu
2bc20 62 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  bSrc; i++){.    
2bc21 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
2bc22 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 2d 3e  elete(db, pSrc->
2bc23 61 5b 69 2b 69 46 72 6f 6d 5d 2e 70 55 73 69 6e  a[i+iFrom].pUsin
2bc24 67 29 3b 0a 20 20 20 20 20 20 70 53 72 63 2d 3e  g);.      pSrc->
2bc25 61 5b 69 2b 69 46 72 6f 6d 5d 20 3d 20 70 53 75  a[i+iFrom] = pSu
2bc26 62 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  bSrc->a[i];.    
2bc27 20 20 6d 65 6d 73 65 74 28 26 70 53 75 62 53 72    memset(&pSubSr
2bc28 63 2d 3e 61 5b 69 5d 2c 20 30 2c 20 73 69 7a 65  c->a[i], 0, size
2bc29 6f 66 28 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d  of(pSubSrc->a[i]
2bc2a 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53  ));.    }.    pS
2bc2b 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 6a 6f 69  rc->a[iFrom].joi
2bc2c 6e 74 79 70 65 20 3d 20 6a 6f 69 6e 74 79 70 65  ntype = jointype
2bc2d 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20  ;.  .    /* Now 
2bc2e 62 65 67 69 6e 20 73 75 62 73 74 69 74 75 74 69  begin substituti
2bc2f 6e 67 20 73 75 62 71 75 65 72 79 20 72 65 73 75  ng subquery resu
2bc30 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt set expressio
2bc31 6e 73 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 72  ns for .    ** r
2bc32 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
2bc33 20 69 50 61 72 65 6e 74 20 69 6e 20 74 68 65 20   iParent in the 
2bc34 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20 20  outer query..   
2bc35 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 45 78 61 6d   ** .    ** Exam
2bc36 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ple:.    **.    
2bc37 2a 2a 20 20 20 53 45 4c 45 43 54 20 61 2b 35 2c  **   SELECT a+5,
2bc38 20 62 2a 31 30 20 46 52 4f 4d 20 28 53 45 4c 45   b*10 FROM (SELE
2bc39 43 54 20 78 2a 33 20 41 53 20 61 2c 20 79 2b 31  CT x*3 AS a, y+1
2bc3a 30 20 41 53 20 62 20 46 52 4f 4d 20 74 31 29 20  0 AS b FROM t1) 
2bc3b 57 48 45 52 45 20 61 3e 62 3b 0a 20 20 20 20 2a  WHERE a>b;.    *
2bc3c 2a 20 20 20 5c 20 20 20 20 20 20 20 20 20 20 20  *   \           
2bc3d 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f            \_____
2bc3e 5f 5f 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65 72  ________ subquer
2bc3f 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20  y __________/   
2bc40 20 20 20 20 20 20 20 2f 0a 20 20 20 20 2a 2a 20         /.    ** 
2bc41 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f     \____________
2bc42 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20  _________ outer 
2bc43 71 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  query __________
2bc44 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2bc45 5f 5f 5f 5f 2f 0a 20 20 20 20 2a 2a 0a 20 20 20  ____/.    **.   
2bc46 20 2a 2a 20 57 65 20 6c 6f 6f 6b 20 61 74 20 65   ** We look at e
2bc47 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  very expression 
2bc48 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  in the outer que
2bc49 72 79 20 61 6e 64 20 65 76 65 72 79 20 70 6c 61  ry and every pla
2bc4a 63 65 20 77 65 20 73 65 65 0a 20 20 20 20 2a 2a  ce we see.    **
2bc4b 20 22 61 22 20 77 65 20 73 75 62 73 74 69 74 75   "a" we substitu
2bc4c 74 65 20 22 78 2a 33 22 20 61 6e 64 20 65 76 65  te "x*3" and eve
2bc4d 72 79 20 70 6c 61 63 65 20 77 65 20 73 65 65 20  ry place we see 
2bc4e 22 62 22 20 77 65 20 73 75 62 73 74 69 74 75 74  "b" we substitut
2bc4f 65 20 22 79 2b 31 30 22 2e 0a 20 20 20 20 2a 2f  e "y+10"..    */
2bc50 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 50 61  .    pList = pPa
2bc51 72 65 6e 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20  rent->pEList;.  
2bc52 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
2bc53 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
2bc54 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74  .      if( pList
2bc55 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20  ->a[i].zName==0 
2bc56 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  ){.        const
2bc57 20 63 68 61 72 20 2a 7a 53 70 61 6e 20 3d 20 70   char *zSpan = p
2bc58 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e  List->a[i].zSpan
2bc59 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 41 4c  ;.        if( AL
2bc5a 57 41 59 53 28 7a 53 70 61 6e 29 20 29 7b 0a 20  WAYS(zSpan) ){. 
2bc5b 20 20 20 20 20 20 20 20 20 70 4c 69 73 74 2d 3e           pList->
2bc5c 61 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c  a[i].zName = sql
2bc5d 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
2bc5e 20 7a 53 70 61 6e 29 3b 0a 20 20 20 20 20 20 20   zSpan);.       
2bc5f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
2bc60 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 4c 69  .    substExprLi
2bc61 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  st(db, pParent->
2bc62 70 45 4c 69 73 74 2c 20 69 50 61 72 65 6e 74 2c  pEList, iParent,
2bc63 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
2bc64 20 20 20 20 69 66 28 20 69 73 41 67 67 20 29 7b      if( isAgg ){
2bc65 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72  .      substExpr
2bc66 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74  List(db, pParent
2bc67 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69 50 61 72  ->pGroupBy, iPar
2bc68 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73  ent, pSub->pELis
2bc69 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e  t);.      pParen
2bc6a 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62  t->pHaving = sub
2bc6b 73 74 45 78 70 72 28 64 62 2c 20 70 50 61 72 65  stExpr(db, pPare
2bc6c 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61  nt->pHaving, iPa
2bc6d 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
2bc6e 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  st);.    }.    i
2bc6f 66 28 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42  f( pSub->pOrderB
2bc70 79 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  y ){.      asser
2bc71 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64  t( pParent->pOrd
2bc72 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20  erBy==0 );.     
2bc73 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72   pParent->pOrder
2bc74 42 79 20 3d 20 70 53 75 62 2d 3e 70 4f 72 64 65  By = pSub->pOrde
2bc75 72 42 79 3b 0a 20 20 20 20 20 20 70 53 75 62 2d  rBy;.      pSub-
2bc76 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20  >pOrderBy = 0;. 
2bc77 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61     }else if( pPa
2bc78 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20 29  rent->pOrderBy )
2bc79 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  {.      substExp
2bc7a 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e  rList(db, pParen
2bc7b 74 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 50 61  t->pOrderBy, iPa
2bc7c 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
2bc7d 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  st);.    }.    i
2bc7e 66 28 20 70 53 75 62 2d 3e 70 57 68 65 72 65 20  f( pSub->pWhere 
2bc7f 29 7b 0a 20 20 20 20 20 20 70 57 68 65 72 65 20  ){.      pWhere 
2bc80 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
2bc81 28 64 62 2c 20 70 53 75 62 2d 3e 70 57 68 65 72  (db, pSub->pWher
2bc82 65 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  e, 0);.    }else
2bc83 7b 0a 20 20 20 20 20 20 70 57 68 65 72 65 20 3d  {.      pWhere =
2bc84 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
2bc85 28 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20  ( subqueryIsAgg 
2bc86 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
2bc87 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e   pParent->pHavin
2bc88 67 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50  g==0 );.      pP
2bc89 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d  arent->pHaving =
2bc8a 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65   pParent->pWhere
2bc8b 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
2bc8c 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65 72 65  >pWhere = pWhere
2bc8d 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
2bc8e 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62 73 74  >pHaving = subst
2bc8f 45 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74  Expr(db, pParent
2bc90 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72 65  ->pHaving, iPare
2bc91 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74  nt, pSub->pEList
2bc92 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  );.      pParent
2bc93 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69  ->pHaving = sqli
2bc94 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70  te3ExprAnd(db, p
2bc95 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 2c  Parent->pHaving,
2bc96 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2bc97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc98 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2bc99 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 48 61  up(db, pSub->pHa
2bc9a 76 69 6e 67 2c 20 30 29 29 3b 0a 20 20 20 20 20  ving, 0));.     
2bc9b 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
2bc9c 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 3b  ->pGroupBy==0 );
2bc9d 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
2bc9e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74  pGroupBy = sqlit
2bc9f 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
2bca0 2c 20 70 53 75 62 2d 3e 70 47 72 6f 75 70 42 79  , pSub->pGroupBy
2bca1 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
2bca2 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
2bca3 70 57 68 65 72 65 20 3d 20 73 75 62 73 74 45 78  pWhere = substEx
2bca4 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  pr(db, pParent->
2bca5 70 57 68 65 72 65 2c 20 69 50 61 72 65 6e 74 2c  pWhere, iParent,
2bca6 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
2bca7 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
2bca8 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
2bca9 78 70 72 41 6e 64 28 64 62 2c 20 70 50 61 72 65  xprAnd(db, pPare
2bcaa 6e 74 2d 3e 70 57 68 65 72 65 2c 20 70 57 68 65  nt->pWhere, pWhe
2bcab 72 65 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  re);.    }.  .  
2bcac 20 20 2f 2a 20 54 68 65 20 66 6c 61 74 74 65 6e    /* The flatten
2bcad 65 64 20 71 75 65 72 79 20 69 73 20 64 69 73 74  ed query is dist
2bcae 69 6e 63 74 20 69 66 20 65 69 74 68 65 72 20 74  inct if either t
2bcaf 68 65 20 69 6e 6e 65 72 20 6f 72 20 74 68 65 0a  he inner or the.
2bcb0 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65      ** outer que
2bcb1 72 79 20 69 73 20 64 69 73 74 69 6e 63 74 2e 20  ry is distinct. 
2bcb2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 72  .    */.    pPar
2bcb3 65 6e 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d  ent->selFlags |=
2bcb4 20 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20   pSub->selFlags 
2bcb5 26 20 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20  & SF_Distinct;. 
2bcb6 20 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20   .    /*.    ** 
2bcb7 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d 20  SELECT ... FROM 
2bcb8 28 53 45 4c 45 43 54 20 2e 2e 2e 20 4c 49 4d 49  (SELECT ... LIMI
2bcb9 54 20 61 20 4f 46 46 53 45 54 20 62 29 20 4c 49  T a OFFSET b) LI
2bcba 4d 49 54 20 78 20 4f 46 46 53 45 54 20 79 3b 0a  MIT x OFFSET y;.
2bcbb 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e      **.    ** On
2bcbc 65 20 69 73 20 74 65 6d 70 74 65 64 20 74 6f 20  e is tempted to 
2bcbd 74 72 79 20 74 6f 20 61 64 64 20 61 20 61 6e 64  try to add a and
2bcbe 20 62 20 74 6f 20 63 6f 6d 62 69 6e 65 20 74 68   b to combine th
2bcbf 65 20 6c 69 6d 69 74 73 2e 20 20 42 75 74 20 74  e limits.  But t
2bcc0 68 69 73 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20  his.    ** does 
2bcc1 6e 6f 74 20 77 6f 72 6b 20 69 66 20 65 69 74 68  not work if eith
2bcc2 65 72 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61  er limit is nega
2bcc3 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tive..    */.   
2bcc4 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69   if( pSub->pLimi
2bcc5 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 65  t ){.      pPare
2bcc6 6e 74 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 53 75  nt->pLimit = pSu
2bcc7 62 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20  b->pLimit;.     
2bcc8 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 3d 20   pSub->pLimit = 
2bcc9 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  0;.    }.  }..  
2bcca 2f 2a 20 46 69 6e 69 61 6c 6c 79 2c 20 64 65 6c  /* Finially, del
2bccb 65 74 65 20 77 68 61 74 20 69 73 20 6c 65 66 74  ete what is left
2bccc 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
2bccd 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2a   and return.  **
2bcce 20 73 75 63 63 65 73 73 2e 0a 20 20 2a 2f 0a 20   success..  */. 
2bccf 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2bcd0 6c 65 74 65 28 64 62 2c 20 70 53 75 62 31 29 3b  lete(db, pSub1);
2bcd1 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ..  return 1;.}.
2bcd2 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2bcd3 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
2bcd4 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66  UBQUERY) || !def
2bcd5 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2bcd6 5f 56 49 45 57 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _VIEW) */../*.**
2bcd7 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 53 45 4c   Analyze the SEL
2bcd8 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 61  ECT statement pa
2bcd9 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d  ssed as an argum
2bcda 65 6e 74 20 74 6f 20 73 65 65 20 69 66 20 69 74  ent to see if it
2bcdb 0a 2a 2a 20 69 73 20 61 20 6d 69 6e 28 29 20 6f  .** is a min() o
2bcdc 72 20 6d 61 78 28 29 20 71 75 65 72 79 2e 20 52  r max() query. R
2bcdd 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45  eturn WHERE_ORDE
2bcde 52 42 59 5f 4d 49 4e 20 6f 72 20 57 48 45 52 45  RBY_MIN or WHERE
2bcdf 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 69 66 20  _ORDERBY_MAX if 
2bce0 0a 2a 2a 20 69 74 20 69 73 2c 20 6f 72 20 30 20  .** it is, or 0 
2bce1 6f 74 68 65 72 77 69 73 65 2e 20 41 74 20 70 72  otherwise. At pr
2bce2 65 73 65 6e 74 2c 20 61 20 71 75 65 72 79 20 69  esent, a query i
2bce3 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  s considered to 
2bce4 62 65 0a 2a 2a 20 61 20 6d 69 6e 28 29 2f 6d 61  be.** a min()/ma
2bce5 78 28 29 20 71 75 65 72 79 20 69 66 3a 0a 2a 2a  x() query if:.**
2bce6 0a 2a 2a 20 20 20 31 2e 20 54 68 65 72 65 20 69  .**   1. There i
2bce7 73 20 61 20 73 69 6e 67 6c 65 20 6f 62 6a 65 63  s a single objec
2bce8 74 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  t in the FROM cl
2bce9 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e  ause..**.**   2.
2bcea 20 54 68 65 72 65 20 69 73 20 61 20 73 69 6e 67   There is a sing
2bceb 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  le expression in
2bcec 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c   the result set,
2bced 20 61 6e 64 20 69 74 20 69 73 0a 2a 2a 20 20 20   and it is.**   
2bcee 20 20 20 65 69 74 68 65 72 20 6d 69 6e 28 78 29     either min(x)
2bcef 20 6f 72 20 6d 61 78 28 78 29 2c 20 77 68 65 72   or max(x), wher
2bcf0 65 20 78 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20  e x is a column 
2bcf1 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a 73 74  reference..*/.st
2bcf2 61 74 69 63 20 75 38 20 6d 69 6e 4d 61 78 51 75  atic u8 minMaxQu
2bcf3 65 72 79 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a  ery(Select *p){.
2bcf4 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20    Expr *pExpr;. 
2bcf5 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2bcf6 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a  t = p->pEList;..
2bcf7 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45    if( pEList->nE
2bcf8 78 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20  xpr!=1 ) return 
2bcf9 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f  WHERE_ORDERBY_NO
2bcfa 52 4d 41 4c 3b 0a 20 20 70 45 78 70 72 20 3d 20  RMAL;.  pExpr = 
2bcfb 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2bcfc 70 72 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  pr;.  if( pExpr-
2bcfd 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43  >op!=TK_AGG_FUNC
2bcfe 54 49 4f 4e 20 29 20 72 65 74 75 72 6e 20 30 3b  TION ) return 0;
2bcff 0a 20 20 69 66 28 20 4e 45 56 45 52 28 45 78 70  .  if( NEVER(Exp
2bd00 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
2bd01 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
2bd02 29 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  )) ) return 0;. 
2bd03 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d   pEList = pExpr-
2bd04 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20  >x.pList;.  if( 
2bd05 70 45 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 45 4c  pEList==0 || pEL
2bd06 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20  ist->nExpr!=1 ) 
2bd07 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
2bd08 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2bd09 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 43  pr->op!=TK_AGG_C
2bd0a 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20 57  OLUMN ) return W
2bd0b 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52  HERE_ORDERBY_NOR
2bd0c 4d 41 4c 3b 0a 20 20 61 73 73 65 72 74 28 20 21  MAL;.  assert( !
2bd0d 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2bd0e 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c  pExpr, EP_IntVal
2bd0f 75 65 29 20 29 3b 0a 20 20 69 66 28 20 73 71 6c  ue) );.  if( sql
2bd10 69 74 65 33 53 74 72 49 43 6d 70 28 70 45 78 70  ite3StrICmp(pExp
2bd11 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 69 6e  r->u.zToken,"min
2bd12 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ")==0 ){.    ret
2bd13 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42  urn WHERE_ORDERB
2bd14 59 5f 4d 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69  Y_MIN;.  }else i
2bd15 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2bd16 70 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  p(pExpr->u.zToke
2bd17 6e 2c 22 6d 61 78 22 29 3d 3d 30 20 29 7b 0a 20  n,"max")==0 ){. 
2bd18 20 20 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f     return WHERE_
2bd19 4f 52 44 45 52 42 59 5f 4d 41 58 3b 0a 20 20 7d  ORDERBY_MAX;.  }
2bd1a 0a 20 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f  .  return WHERE_
2bd1b 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a  ORDERBY_NORMAL;.
2bd1c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 6c  }../*.** The sel
2bd1d 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70 61  ect statement pa
2bd1e 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
2bd1f 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e  t argument is an
2bd20 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79   aggregate query
2bd21 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
2bd22 61 72 67 6d 65 6e 74 20 69 73 20 74 68 65 20 61  argment is the a
2bd23 73 73 6f 63 69 61 74 65 64 20 61 67 67 72 65 67  ssociated aggreg
2bd24 61 74 65 2d 69 6e 66 6f 20 6f 62 6a 65 63 74 2e  ate-info object.
2bd25 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   This .** functi
2bd26 6f 6e 20 74 65 73 74 73 20 69 66 20 74 68 65 20  on tests if the 
2bd27 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 68 65  SELECT is of the
2bd28 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 53   form:.**.**   S
2bd29 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 46  ELECT count(*) F
2bd2a 52 4f 4d 20 3c 74 62 6c 3e 0a 2a 2a 0a 2a 2a 20  ROM <tbl>.**.** 
2bd2b 77 68 65 72 65 20 74 61 62 6c 65 20 69 73 20 61  where table is a
2bd2c 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c   database table,
2bd2d 20 6e 6f 74 20 61 20 73 75 62 2d 73 65 6c 65 63   not a sub-selec
2bd2e 74 20 6f 72 20 76 69 65 77 2e 20 49 66 20 74 68  t or view. If th
2bd2f 65 20 71 75 65 72 79 0a 2a 2a 20 64 6f 65 73 20  e query.** does 
2bd30 6d 61 74 63 68 20 74 68 69 73 20 70 61 74 74 65  match this patte
2bd31 72 6e 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74  rn, then a point
2bd32 65 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20  er to the Table 
2bd33 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74  object represent
2bd34 69 6e 67 0a 2a 2a 20 3c 74 62 6c 3e 20 69 73 20  ing.** <tbl> is 
2bd35 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
2bd36 69 73 65 2c 20 30 20 69 73 20 72 65 74 75 72 6e  ise, 0 is return
2bd37 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61  ed..*/.static Ta
2bd38 62 6c 65 20 2a 69 73 53 69 6d 70 6c 65 43 6f 75  ble *isSimpleCou
2bd39 6e 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 41 67  nt(Select *p, Ag
2bd3a 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29  gInfo *pAggInfo)
2bd3b 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  {.  Table *pTab;
2bd3c 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a  .  Expr *pExpr;.
2bd3d 0a 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70  .  assert( !p->p
2bd3e 47 72 6f 75 70 42 79 20 29 3b 0a 0a 20 20 69 66  GroupBy );..  if
2bd3f 28 20 70 2d 3e 70 57 68 65 72 65 20 7c 7c 20 70  ( p->pWhere || p
2bd40 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21  ->pEList->nExpr!
2bd41 3d 31 20 0a 20 20 20 7c 7c 20 70 2d 3e 70 53 72  =1 .   || p->pSr
2bd42 63 2d 3e 6e 53 72 63 21 3d 31 20 7c 7c 20 70 2d  c->nSrc!=1 || p-
2bd43 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c  >pSrc->a[0].pSel
2bd44 65 63 74 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  ect.  ){.    ret
2bd45 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61  urn 0;.  }.  pTa
2bd46 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30  b = p->pSrc->a[0
2bd47 5d 2e 70 54 61 62 3b 0a 20 20 70 45 78 70 72 20  ].pTab;.  pExpr 
2bd48 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30  = p->pEList->a[0
2bd49 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65 72  ].pExpr;.  asser
2bd4a 74 28 20 70 54 61 62 20 26 26 20 21 70 54 61 62  t( pTab && !pTab
2bd4b 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 70 45 78  ->pSelect && pEx
2bd4c 70 72 20 29 3b 0a 0a 20 20 69 66 28 20 49 73 56  pr );..  if( IsV
2bd4d 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72  irtual(pTab) ) r
2bd4e 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70  eturn 0;.  if( p
2bd4f 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47  Expr->op!=TK_AGG
2bd50 5f 46 55 4e 43 54 49 4f 4e 20 29 20 72 65 74 75  _FUNCTION ) retu
2bd51 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 70 41 67  rn 0;.  if( (pAg
2bd52 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 2e  gInfo->aFunc[0].
2bd53 70 46 75 6e 63 2d 3e 66 6c 61 67 73 26 53 51 4c  pFunc->flags&SQL
2bd54 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 29 3d  ITE_FUNC_COUNT)=
2bd55 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
2bd56 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67   if( pExpr->flag
2bd57 73 26 45 50 5f 44 69 73 74 69 6e 63 74 20 29 20  s&EP_Distinct ) 
2bd58 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 72 65 74  return 0;..  ret
2bd59 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a  urn pTab;.}../*.
2bd5a 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65  ** If the source
2bd5b 2d 6c 69 73 74 20 69 74 65 6d 20 70 61 73 73 65  -list item passe
2bd5c 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
2bd5d 20 77 61 73 20 61 75 67 6d 65 6e 74 65 64 20 77   was augmented w
2bd5e 69 74 68 20 61 6e 0a 2a 2a 20 49 4e 44 45 58 45  ith an.** INDEXE
2bd5f 44 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65  D BY clause, the
2bd60 6e 20 74 72 79 20 74 6f 20 6c 6f 63 61 74 65 20  n try to locate 
2bd61 74 68 65 20 73 70 65 63 69 66 69 65 64 20 69 6e  the specified in
2bd62 64 65 78 2e 20 49 66 20 74 68 65 72 65 0a 2a 2a  dex. If there.**
2bd63 20 77 61 73 20 73 75 63 68 20 61 20 63 6c 61 75   was such a clau
2bd64 73 65 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 64  se and the named
2bd65 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 62 65   index cannot be
2bd66 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 0a   found, return .
2bd67 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  ** SQLITE_ERROR 
2bd68 61 6e 64 20 6c 65 61 76 65 20 61 6e 20 65 72 72  and leave an err
2bd69 6f 72 20 69 6e 20 70 50 61 72 73 65 2e 20 4f 74  or in pParse. Ot
2bd6a 68 65 72 77 69 73 65 2c 20 70 6f 70 75 6c 61 74  herwise, populat
2bd6b 65 20 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 70 49 6e  e .** pFrom->pIn
2bd6c 64 65 78 20 61 6e 64 20 72 65 74 75 72 6e 20 53  dex and return S
2bd6d 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
2bd6e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2bd6f 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79  sqlite3IndexedBy
2bd70 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 70 50  Lookup(Parse *pP
2bd71 61 72 73 65 2c 20 73 74 72 75 63 74 20 53 72 63  arse, struct Src
2bd72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d  List_item *pFrom
2bd73 29 7b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e  ){.  if( pFrom->
2bd74 70 54 61 62 20 26 26 20 70 46 72 6f 6d 2d 3e 7a  pTab && pFrom->z
2bd75 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 54 61 62  Index ){.    Tab
2bd76 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d  le *pTab = pFrom
2bd77 2d 3e 70 54 61 62 3b 0a 20 20 20 20 63 68 61 72  ->pTab;.    char
2bd78 20 2a 7a 49 6e 64 65 78 20 3d 20 70 46 72 6f 6d   *zIndex = pFrom
2bd79 2d 3e 7a 49 6e 64 65 78 3b 0a 20 20 20 20 49 6e  ->zIndex;.    In
2bd7a 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 66  dex *pIdx;.    f
2bd7b 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
2bd7c 6e 64 65 78 3b 20 0a 20 20 20 20 20 20 20 20 70  ndex; .        p
2bd7d 49 64 78 20 26 26 20 73 71 6c 69 74 65 33 53 74  Idx && sqlite3St
2bd7e 72 49 43 6d 70 28 70 49 64 78 2d 3e 7a 4e 61 6d  rICmp(pIdx->zNam
2bd7f 65 2c 20 7a 49 6e 64 65 78 29 3b 20 0a 20 20 20  e, zIndex); .   
2bd80 20 20 20 20 20 70 49 64 78 3d 70 49 64 78 2d 3e       pIdx=pIdx->
2bd81 70 4e 65 78 74 0a 20 20 20 20 29 3b 0a 20 20 20  pNext.    );.   
2bd82 20 69 66 28 20 21 70 49 64 78 20 29 7b 0a 20 20   if( !pIdx ){.  
2bd83 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2bd84 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
2bd85 73 75 63 68 20 69 6e 64 65 78 3a 20 25 73 22 2c  such index: %s",
2bd86 20 7a 49 6e 64 65 78 2c 20 30 29 3b 0a 20 20 20   zIndex, 0);.   
2bd87 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2bd88 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20  _ERROR;.    }.  
2bd89 20 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20    pFrom->pIndex 
2bd8a 3d 20 70 49 64 78 3b 0a 20 20 7d 0a 20 20 72 65  = pIdx;.  }.  re
2bd8b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2bd8c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2bd8d 75 74 69 6e 65 20 69 73 20 61 20 57 61 6c 6b 65  utine is a Walke
2bd8e 72 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 22  r callback for "
2bd8f 65 78 70 61 6e 64 69 6e 67 22 20 61 20 53 45 4c  expanding" a SEL
2bd90 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
2bd91 2a 20 22 45 78 70 61 6e 64 69 6e 67 22 20 6d 65  * "Expanding" me
2bd92 61 6e 73 20 74 6f 20 64 6f 20 74 68 65 20 66 6f  ans to do the fo
2bd93 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
2bd94 20 20 28 31 29 20 20 4d 61 6b 65 20 73 75 72 65    (1)  Make sure
2bd95 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
2bd96 62 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 61  bers have been a
2bd97 73 73 69 67 6e 65 64 20 74 6f 20 65 76 65 72 79  ssigned to every
2bd98 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6c 65 6d  .**         elem
2bd99 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  ent of the FROM 
2bd9a 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  clause..**.**   
2bd9b 20 28 32 29 20 20 46 69 6c 6c 20 69 6e 20 74 68   (2)  Fill in th
2bd9c 65 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e  e pTabList->a[].
2bd9d 70 54 61 62 20 66 69 65 6c 64 73 20 69 6e 20 74  pTab fields in t
2bd9e 68 65 20 53 72 63 4c 69 73 74 20 74 68 61 74 20  he SrcList that 
2bd9f 0a 2a 2a 20 20 20 20 20 20 20 20 20 64 65 66 69  .**         defi
2bda0 6e 65 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  nes FROM clause.
2bda1 20 20 57 68 65 6e 20 76 69 65 77 73 20 61 70 70    When views app
2bda2 65 61 72 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ear in the FROM 
2bda3 63 6c 61 75 73 65 2c 0a 2a 2a 20 20 20 20 20 20  clause,.**      
2bda4 20 20 20 66 69 6c 6c 20 70 54 61 62 4c 69 73 74     fill pTabList
2bda5 2d 3e 61 5b 5d 2e 70 53 65 6c 65 63 74 20 77 69  ->a[].pSelect wi
2bda6 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  th a copy of the
2bda7 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2bda8 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 68 61  t.**         tha
2bda9 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
2bdaa 20 76 69 65 77 2e 20 20 41 20 63 6f 70 79 20 69   view.  A copy i
2bdab 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 76 69  s made of the vi
2bdac 65 77 27 73 20 53 45 4c 45 43 54 0a 2a 2a 20 20  ew's SELECT.**  
2bdad 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74         statement
2bdae 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20   so that we can 
2bdaf 66 72 65 65 6c 79 20 6d 6f 64 69 66 79 20 6f 72  freely modify or
2bdb0 20 64 65 6c 65 74 65 20 74 68 61 74 20 73 74 61   delete that sta
2bdb1 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20  tement.**       
2bdb2 20 20 77 69 74 68 6f 75 74 20 77 6f 72 72 79 69    without worryi
2bdb3 6e 67 20 61 62 6f 75 74 20 6d 65 73 73 69 6e 67  ng about messing
2bdb4 20 75 70 20 74 68 65 20 70 72 65 73 69 73 74 65   up the presiste
2bdb5 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  nt representatio
2bdb6 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 66 20  n.**         of 
2bdb7 74 68 65 20 76 69 65 77 2e 0a 2a 2a 0a 2a 2a 20  the view..**.** 
2bdb8 20 20 20 28 33 29 20 20 41 64 64 20 74 65 72 6d     (3)  Add term
2bdb9 73 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63  s to the WHERE c
2bdba 6c 61 75 73 65 20 74 6f 20 61 63 63 6f 6d 6f 64  lause to accomod
2bdbb 61 74 65 20 74 68 65 20 4e 41 54 55 52 41 4c 20  ate the NATURAL 
2bdbc 6b 65 79 77 6f 72 64 0a 2a 2a 20 20 20 20 20 20  keyword.**      
2bdbd 20 20 20 6f 6e 20 6a 6f 69 6e 73 20 61 6e 64 20     on joins and 
2bdbe 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  the ON and USING
2bdbf 20 63 6c 61 75 73 65 20 6f 66 20 6a 6f 69 6e 73   clause of joins
2bdc0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20  ..**.**    (4)  
2bdc1 53 63 61 6e 20 74 68 65 20 6c 69 73 74 20 6f 66  Scan the list of
2bdc2 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
2bdc3 72 65 73 75 6c 74 20 73 65 74 20 28 70 45 4c 69  result set (pELi
2bdc4 73 74 29 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 20  st) looking.**  
2bdc5 20 20 20 20 20 20 20 66 6f 72 20 69 6e 73 74 61         for insta
2bdc6 6e 63 65 73 20 6f 66 20 74 68 65 20 22 2a 22 20  nces of the "*" 
2bdc7 6f 70 65 72 61 74 6f 72 20 6f 72 20 74 68 65 20  operator or the 
2bdc8 54 41 42 4c 45 2e 2a 20 6f 70 65 72 61 74 6f 72  TABLE.* operator
2bdc9 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 49 66 20  ..**         If 
2bdca 66 6f 75 6e 64 2c 20 65 78 70 61 6e 64 20 65 61  found, expand ea
2bdcb 63 68 20 22 2a 22 20 74 6f 20 62 65 20 65 76 65  ch "*" to be eve
2bdcc 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65  ry column in eve
2bdcd 72 79 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20  ry table.**     
2bdce 20 20 20 20 61 6e 64 20 54 41 42 4c 45 2e 2a 20      and TABLE.* 
2bdcf 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75  to be every colu
2bdd0 6d 6e 20 69 6e 20 54 41 42 4c 45 2e 0a 2a 2a 0a  mn in TABLE..**.
2bdd1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
2bdd2 6c 65 63 74 45 78 70 61 6e 64 65 72 28 57 61 6c  lectExpander(Wal
2bdd3 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65  ker *pWalker, Se
2bdd4 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72 73  lect *p){.  Pars
2bdd5 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c  e *pParse = pWal
2bdd6 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 69  ker->pParse;.  i
2bdd7 6e 74 20 69 2c 20 6a 2c 20 6b 3b 0a 20 20 53 72  nt i, j, k;.  Sr
2bdd8 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b  cList *pTabList;
2bdd9 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
2bdda 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 53 72  ist;.  struct Sr
2bddb 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f  cList_item *pFro
2bddc 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  m;.  sqlite3 *db
2bddd 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
2bdde 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2bddf 46 61 69 6c 65 64 20 20 29 7b 0a 20 20 20 20 72  Failed  ){.    r
2bde0 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
2bde1 0a 20 20 7d 0a 20 20 69 66 28 20 4e 45 56 45 52  .  }.  if( NEVER
2bde2 28 70 2d 3e 70 53 72 63 3d 3d 30 29 20 7c 7c 20  (p->pSrc==0) || 
2bde3 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
2bde4 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20 29  F_Expanded)!=0 )
2bde5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  {.    return WRC
2bde6 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 2d  _Prune;.  }.  p-
2bde7 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
2bde8 45 78 70 61 6e 64 65 64 3b 0a 20 20 70 54 61 62  Expanded;.  pTab
2bde9 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  List = p->pSrc;.
2bdea 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45    pEList = p->pE
2bdeb 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65  List;..  /* Make
2bdec 20 73 75 72 65 20 63 75 72 73 6f 72 20 6e 75 6d   sure cursor num
2bded 62 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 61  bers have been a
2bdee 73 73 69 67 6e 65 64 20 74 6f 20 61 6c 6c 20 65  ssigned to all e
2bdef 6e 74 72 69 65 73 20 69 6e 0a 20 20 2a 2a 20 74  ntries in.  ** t
2bdf0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
2bdf1 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
2bdf2 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73  tement..  */.  s
2bdf3 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73  qlite3SrcListAss
2bdf4 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73  ignCursors(pPars
2bdf5 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 0a 20  e, pTabList);.. 
2bdf6 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72   /* Look up ever
2bdf7 79 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 69 6e  y table named in
2bdf8 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2bdf9 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e 20   of the select. 
2bdfa 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 6e 74 72   If.  ** an entr
2bdfb 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  y of the FROM cl
2bdfc 61 75 73 65 20 69 73 20 61 20 73 75 62 71 75 65  ause is a subque
2bdfd 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  ry instead of a 
2bdfe 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2c 0a 20  table or view,. 
2bdff 20 2a 2a 20 74 68 65 6e 20 63 72 65 61 74 65 20   ** then create 
2be00 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c  a transient tabl
2be01 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 64  e structure to d
2be02 65 73 63 72 69 62 65 20 74 68 65 20 73 75 62 71  escribe the subq
2be03 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  uery..  */.  for
2be04 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61 62  (i=0, pFrom=pTab
2be05 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62 4c  List->a; i<pTabL
2be06 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
2be07 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 54 61  pFrom++){.    Ta
2be08 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69  ble *pTab;.    i
2be09 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 21 3d  f( pFrom->pTab!=
2be0a 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  0 ){.      /* Th
2be0b 69 73 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73  is statement has
2be0c 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72   already been pr
2be0d 65 70 61 72 65 64 2e 20 20 54 68 65 72 65 20 69  epared.  There i
2be0e 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 20 20  s no need.      
2be0f 2a 2a 20 74 6f 20 67 6f 20 66 75 72 74 68 65 72  ** to go further
2be10 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  . */.      asser
2be11 74 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20 20  t( i==0 );.     
2be12 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
2be13 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
2be14 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30   pFrom->zName==0
2be15 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ){.#ifndef SQLI
2be16 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
2be17 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
2be18 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53 65  Sel = pFrom->pSe
2be19 6c 65 63 74 3b 0a 20 20 20 20 20 20 2f 2a 20 41  lect;.      /* A
2be1a 20 73 75 62 2d 71 75 65 72 79 20 69 6e 20 74 68   sub-query in th
2be1b 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
2be1c 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20   a SELECT */.   
2be1d 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 21     assert( pSel!
2be1e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  =0 );.      asse
2be1f 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d  rt( pFrom->pTab=
2be20 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
2be21 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57  te3WalkSelect(pW
2be22 61 6c 6b 65 72 2c 20 70 53 65 6c 29 3b 0a 20 20  alker, pSel);.  
2be23 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20      pFrom->pTab 
2be24 3d 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33  = pTab = sqlite3
2be25 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2be26 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b   sizeof(Table));
2be27 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 3d  .      if( pTab=
2be28 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  =0 ) return WRC_
2be29 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70 54 61  Abort;.      pTa
2be2a 62 2d 3e 64 62 4d 65 6d 20 3d 20 64 62 2d 3e 6c  b->dbMem = db->l
2be2b 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
2be2c 64 20 3f 20 64 62 20 3a 20 30 3b 0a 20 20 20 20  d ? db : 0;.    
2be2d 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31    pTab->nRef = 1
2be2e 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e  ;.      pTab->zN
2be2f 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
2be30 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65  intf(db, "sqlite
2be31 5f 73 75 62 71 75 65 72 79 5f 25 70 5f 22 2c 20  _subquery_%p_", 
2be32 28 76 6f 69 64 2a 29 70 54 61 62 29 3b 0a 20 20  (void*)pTab);.  
2be33 20 20 20 20 77 68 69 6c 65 28 20 70 53 65 6c 2d      while( pSel-
2be34 3e 70 50 72 69 6f 72 20 29 7b 20 70 53 65 6c 20  >pPrior ){ pSel 
2be35 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b 20  = pSel->pPrior; 
2be36 7d 0a 20 20 20 20 20 20 73 65 6c 65 63 74 43 6f  }.      selectCo
2be37 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69 73  lumnsFromExprLis
2be38 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e  t(pParse, pSel->
2be39 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d 3e 6e  pEList, &pTab->n
2be3a 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43 6f 6c  Col, &pTab->aCol
2be3b 29 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 69  );.      pTab->i
2be3c 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 20 20 20  PKey = -1;.     
2be3d 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20   pTab->tabFlags 
2be3e 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 3b  |= TF_Ephemeral;
2be3f 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73  .#endif.    }els
2be40 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f  e{.      /* An o
2be41 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f 72  rdinary table or
2be42 20 76 69 65 77 20 6e 61 6d 65 20 69 6e 20 74 68   view name in th
2be43 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  e FROM clause */
2be44 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2be45 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b  From->pTab==0 );
2be46 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54  .      pFrom->pT
2be47 61 62 20 3d 20 70 54 61 62 20 3d 20 0a 20 20 20  ab = pTab = .   
2be48 20 20 20 20 20 73 71 6c 69 74 65 33 4c 6f 63 61       sqlite3Loca
2be49 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 30  teTable(pParse,0
2be4a 2c 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 2c 70 46  ,pFrom->zName,pF
2be4b 72 6f 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  rom->zDatabase);
2be4c 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 3d  .      if( pTab=
2be4d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  =0 ) return WRC_
2be4e 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70 54 61  Abort;.      pTa
2be4f 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69 66 20 21  b->nRef++;.#if !
2be50 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2be51 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
2be52 66 69 6e 65 64 20 28 53 51 4c 49 54 45 5f 4f 4d  fined (SQLITE_OM
2be53 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
2be54 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 2d  .      if( pTab-
2be55 3e 70 53 65 6c 65 63 74 20 7c 7c 20 49 73 56 69  >pSelect || IsVi
2be56 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
2be57 20 20 20 20 20 20 20 2f 2a 20 57 65 20 72 65 61         /* We rea
2be58 63 68 20 68 65 72 65 20 69 66 20 74 68 65 20 6e  ch here if the n
2be59 61 6d 65 64 20 74 61 62 6c 65 20 69 73 20 61 20  amed table is a 
2be5a 72 65 61 6c 6c 79 20 61 20 76 69 65 77 20 2a 2f  really a view */
2be5b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
2be5c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
2be5d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
2be5e 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 57 52  Tab) ) return WR
2be5f 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20  C_Abort;.       
2be60 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e   assert( pFrom->
2be61 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20  pSelect==0 );.  
2be62 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 53 65        pFrom->pSe
2be63 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
2be64 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54 61 62  lectDup(db, pTab
2be65 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20  ->pSelect, 0);. 
2be66 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61         sqlite3Wa
2be67 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72  lkSelect(pWalker
2be68 2c 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74  , pFrom->pSelect
2be69 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
2be6a 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  f.    }..    /* 
2be6b 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65 78  Locate the index
2be6c 20 6e 61 6d 65 64 20 62 79 20 74 68 65 20 49 4e   named by the IN
2be6d 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 2c  DEXED BY clause,
2be6e 20 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20   if any. */.    
2be6f 69 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65 78  if( sqlite3Index
2be70 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73  edByLookup(pPars
2be71 65 2c 20 70 46 72 6f 6d 29 20 29 7b 0a 20 20 20  e, pFrom) ){.   
2be72 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
2be73 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ort;.    }.  }..
2be74 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 4e 41 54    /* Process NAT
2be75 55 52 41 4c 20 6b 65 79 77 6f 72 64 73 2c 20 61  URAL keywords, a
2be76 6e 64 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20  nd ON and USING 
2be77 63 6c 61 75 73 65 73 20 6f 66 20 6a 6f 69 6e 73  clauses of joins
2be78 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
2be79 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
2be7a 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f   sqliteProcessJo
2be7b 69 6e 28 70 50 61 72 73 65 2c 20 70 29 20 29 7b  in(pParse, p) ){
2be7c 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f  .    return WRC_
2be7d 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Abort;.  }..  /*
2be7e 20 46 6f 72 20 65 76 65 72 79 20 22 2a 22 20 74   For every "*" t
2be7f 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 74 68  hat occurs in th
2be80 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20 69  e column list, i
2be81 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73 20  nsert the names 
2be82 6f 66 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c 75  of.  ** all colu
2be83 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65  mns in all table
2be84 73 2e 20 20 41 6e 64 20 66 6f 72 20 65 76 65 72  s.  And for ever
2be85 79 20 54 41 42 4c 45 2e 2a 20 69 6e 73 65 72 74  y TABLE.* insert
2be86 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20   the names.  ** 
2be87 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69  of all columns i
2be88 6e 20 54 41 42 4c 45 2e 20 20 54 68 65 20 70 61  n TABLE.  The pa
2be89 72 73 65 72 20 69 6e 73 65 72 74 65 64 20 61 20  rser inserted a 
2be8a 73 70 65 63 69 61 6c 20 65 78 70 72 65 73 73 69  special expressi
2be8b 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 20 74 68 65  on.  ** with the
2be8c 20 54 4b 5f 41 4c 4c 20 6f 70 65 72 61 74 6f 72   TK_ALL operator
2be8d 20 66 6f 72 20 65 61 63 68 20 22 2a 22 20 74 68   for each "*" th
2be8e 61 74 20 69 74 20 66 6f 75 6e 64 20 69 6e 20 74  at it found in t
2be8f 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0a  he column list..
2be90 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    ** The followi
2be91 6e 67 20 63 6f 64 65 20 6a 75 73 74 20 68 61 73  ng code just has
2be92 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 54   to locate the T
2be93 4b 5f 41 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e  K_ALL expression
2be94 73 20 61 6e 64 20 65 78 70 61 6e 64 0a 20 20 2a  s and expand.  *
2be95 2a 20 65 61 63 68 20 6f 6e 65 20 74 6f 20 74 68  * each one to th
2be96 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f  e list of all co
2be97 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62  lumns in all tab
2be98 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  les..  **.  ** T
2be99 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 20 6a 75  he first loop ju
2be9a 73 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65  st checks to see
2be9b 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   if there are an
2be9c 79 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 73 0a  y "*" operators.
2be9d 20 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 20 65    ** that need e
2be9e 78 70 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20  xpanding..  */. 
2be9f 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69   for(k=0; k<pELi
2bea0 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b  st->nExpr; k++){
2bea1 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20  .    Expr *pE = 
2bea2 70 45 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45 78  pEList->a[k].pEx
2bea3 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 2d 3e  pr;.    if( pE->
2bea4 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72 65  op==TK_ALL ) bre
2bea5 61 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ak;.    assert( 
2bea6 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c  pE->op!=TK_DOT |
2bea7 7c 20 70 45 2d 3e 70 52 69 67 68 74 21 3d 30 20  | pE->pRight!=0 
2bea8 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2bea9 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c  E->op!=TK_DOT ||
2beaa 20 28 70 45 2d 3e 70 4c 65 66 74 21 3d 30 20 26   (pE->pLeft!=0 &
2beab 26 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d  & pE->pLeft->op=
2beac 3d 54 4b 5f 49 44 29 20 29 3b 0a 20 20 20 20 69  =TK_ID) );.    i
2bead 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f  f( pE->op==TK_DO
2beae 54 20 26 26 20 70 45 2d 3e 70 52 69 67 68 74 2d  T && pE->pRight-
2beaf 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72  >op==TK_ALL ) br
2beb0 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6b  eak;.  }.  if( k
2beb1 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  <pEList->nExpr )
2beb2 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  {.    /*.    ** 
2beb3 49 66 20 77 65 20 67 65 74 20 68 65 72 65 20 69  If we get here i
2beb4 74 20 6d 65 61 6e 73 20 74 68 65 20 72 65 73 75  t means the resu
2beb5 6c 74 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20  lt set contains 
2beb6 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 22 2a 22 0a  one or more "*".
2beb7 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 73      ** operators
2beb8 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65   that need to be
2beb9 20 65 78 70 61 6e 64 65 64 2e 20 20 4c 6f 6f 70   expanded.  Loop
2beba 20 74 68 72 6f 75 67 68 20 65 61 63 68 20 65 78   through each ex
2bebb 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20  pression.    ** 
2bebc 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
2bebd 74 20 61 6e 64 20 65 78 70 61 6e 64 20 74 68 65  t and expand the
2bebe 6d 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20  m one by one..  
2bebf 20 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20    */.    struct 
2bec0 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61  ExprList_item *a
2bec1 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20   = pEList->a;.  
2bec2 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77    ExprList *pNew
2bec3 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 66 6c   = 0;.    int fl
2bec4 61 67 73 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  ags = pParse->db
2bec5 2d 3e 66 6c 61 67 73 3b 0a 20 20 20 20 69 6e 74  ->flags;.    int
2bec6 20 6c 6f 6e 67 4e 61 6d 65 73 20 3d 20 28 66 6c   longNames = (fl
2bec7 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c  ags & SQLITE_Ful
2bec8 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 0a 20 20  lColNames)!=0.  
2bec9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2beca 20 20 20 20 26 26 20 28 66 6c 61 67 73 20 26 20      && (flags & 
2becb 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
2becc 61 6d 65 73 29 3d 3d 30 3b 0a 0a 20 20 20 20 66  ames)==0;..    f
2becd 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74  or(k=0; k<pEList
2bece 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20  ->nExpr; k++){. 
2becf 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20       Expr *pE = 
2bed0 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[k].pExpr;.    
2bed1 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f 70    assert( pE->op
2bed2 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e  !=TK_DOT || pE->
2bed3 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20  pRight!=0 );.   
2bed4 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 21 3d 54     if( pE->op!=T
2bed5 4b 5f 41 4c 4c 20 26 26 20 28 70 45 2d 3e 6f 70  K_ALL && (pE->op
2bed6 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e  !=TK_DOT || pE->
2bed7 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f 41  pRight->op!=TK_A
2bed8 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  LL) ){.        /
2bed9 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c 61  * This particula
2beda 72 20 65 78 70 72 65 73 73 69 6f 6e 20 64 6f 65  r expression doe
2bedb 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  s not need to be
2bedc 20 65 78 70 61 6e 64 65 64 2e 0a 20 20 20 20 20   expanded..     
2bedd 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e     */.        pN
2bede 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
2bedf 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2bee0 65 2c 20 70 4e 65 77 2c 20 61 5b 6b 5d 2e 70 45  e, pNew, a[k].pE
2bee1 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66  xpr);.        if
2bee2 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( pNew ){.      
2bee3 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77      pNew->a[pNew
2bee4 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 4e 61 6d 65  ->nExpr-1].zName
2bee5 20 3d 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 3b 0a 20   = a[k].zName;. 
2bee6 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 61           pNew->a
2bee7 5b 70 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e  [pNew->nExpr-1].
2bee8 7a 53 70 61 6e 20 3d 20 61 5b 6b 5d 2e 7a 53 70  zSpan = a[k].zSp
2bee9 61 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 61 5b  an;.          a[
2beea 6b 5d 2e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20  k].zName = 0;.  
2beeb 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 53 70          a[k].zSp
2beec 61 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  an = 0;.        
2beed 7d 0a 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 70  }.        a[k].p
2beee 45 78 70 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  Expr = 0;.      
2beef 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
2bef0 2a 20 54 68 69 73 20 65 78 70 72 65 73 73 69 6f  * This expressio
2bef1 6e 20 69 73 20 61 20 22 2a 22 20 6f 72 20 61 20  n is a "*" or a 
2bef2 22 54 41 42 4c 45 2e 2a 22 20 61 6e 64 20 6e 65  "TABLE.*" and ne
2bef3 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 20 20  eds to be.      
2bef4 20 20 2a 2a 20 65 78 70 61 6e 64 65 64 2e 20 2a    ** expanded. *
2bef5 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 61  /.        int ta
2bef6 62 6c 65 53 65 65 6e 20 3d 20 30 3b 20 20 20 20  bleSeen = 0;    
2bef7 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 77 68    /* Set to 1 wh
2bef8 65 6e 20 54 41 42 4c 45 20 6d 61 74 63 68 65 73  en TABLE matches
2bef9 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72   */.        char
2befa 20 2a 7a 54 4e 61 6d 65 3b 20 20 20 20 20 20 20   *zTName;       
2befb 20 20 20 20 20 2f 2a 20 74 65 78 74 20 6f 66 20       /* text of 
2befc 6e 61 6d 65 20 6f 66 20 54 41 42 4c 45 20 2a 2f  name of TABLE */
2befd 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 2d  .        if( pE-
2befe 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 7b 0a 20  >op==TK_DOT ){. 
2beff 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2bf00 20 70 45 2d 3e 70 4c 65 66 74 21 3d 30 20 29 3b   pE->pLeft!=0 );
2bf01 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
2bf02 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2bf03 72 74 79 28 70 45 2d 3e 70 4c 65 66 74 2c 20 45  rty(pE->pLeft, E
2bf04 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
2bf05 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20           zTName 
2bf06 3d 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a  = pE->pLeft->u.z
2bf07 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7d  Token;.        }
2bf08 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2bf09 7a 54 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20  zTName = 0;.    
2bf0a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f      }.        fo
2bf0b 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61  r(i=0, pFrom=pTa
2bf0c 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62  bList->a; i<pTab
2bf0d 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
2bf0e 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 20   pFrom++){.     
2bf0f 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
2bf10 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a   = pFrom->pTab;.
2bf11 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
2bf12 7a 54 61 62 4e 61 6d 65 20 3d 20 70 46 72 6f 6d  zTabName = pFrom
2bf13 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20  ->zAlias;.      
2bf14 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65      if( zTabName
2bf15 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2bf16 20 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54     zTabName = pT
2bf17 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ab->zName;.     
2bf18 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2bf19 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2bf1a 61 69 6c 65 64 20 29 20 62 72 65 61 6b 3b 0a 20  ailed ) break;. 
2bf1b 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 4e           if( zTN
2bf1c 61 6d 65 20 26 26 20 73 71 6c 69 74 65 33 53 74  ame && sqlite3St
2bf1d 72 49 43 6d 70 28 7a 54 4e 61 6d 65 2c 20 7a 54  rICmp(zTName, zT
2bf1e 61 62 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20  abName)!=0 ){.  
2bf1f 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
2bf20 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ue;.          }.
2bf21 20 20 20 20 20 20 20 20 20 20 74 61 62 6c 65 53            tableS
2bf22 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  een = 1;.       
2bf23 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54     for(j=0; j<pT
2bf24 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a  ab->nCol; j++){.
2bf25 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72              Expr
2bf26 20 2a 70 45 78 70 72 2c 20 2a 70 52 69 67 68 74   *pExpr, *pRight
2bf27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  ;.            ch
2bf28 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61 62  ar *zName = pTab
2bf29 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b  ->aCol[j].zName;
2bf2a 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61  .            cha
2bf2b 72 20 2a 7a 43 6f 6c 6e 61 6d 65 3b 20 20 2f 2a  r *zColname;  /*
2bf2c 20 54 68 65 20 63 6f 6d 70 75 74 65 64 20 63 6f   The computed co
2bf2d 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  lumn name */.   
2bf2e 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
2bf2f 54 6f 46 72 65 65 3b 20 20 20 2f 2a 20 4d 61 6c  ToFree;   /* Mal
2bf30 6c 6f 63 65 64 20 73 74 72 69 6e 67 20 74 68 61  loced string tha
2bf31 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72  t needs to be fr
2bf32 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  eed */.         
2bf33 20 20 20 54 6f 6b 65 6e 20 73 43 6f 6c 6e 61 6d     Token sColnam
2bf34 65 3b 20 20 2f 2a 20 43 6f 6d 70 75 74 65 64 20  e;  /* Computed 
2bf35 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 61  column name as a
2bf36 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 20 20 20   token */..     
2bf37 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 63         /* If a c
2bf38 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b 65 64 20  olumn is marked 
2bf39 61 73 20 27 68 69 64 64 65 6e 27 20 28 63 75 72  as 'hidden' (cur
2bf3a 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f 73 73  rently only poss
2bf3b 69 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20  ible.           
2bf3c 20 2a 2a 20 66 6f 72 20 76 69 72 74 75 61 6c 20   ** for virtual 
2bf3d 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74 20  tables), do not 
2bf3e 69 6e 63 6c 75 64 65 20 69 74 20 69 6e 20 74 68  include it in th
2bf3f 65 20 65 78 70 61 6e 64 65 64 0a 20 20 20 20 20  e expanded.     
2bf40 20 20 20 20 20 20 20 2a 2a 20 72 65 73 75 6c 74         ** result
2bf41 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20 20 20 20  -set list..     
2bf42 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2bf43 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64        if( IsHidd
2bf44 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e  enColumn(&pTab->
2bf45 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20 20  aCol[j]) ){.    
2bf46 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2bf47 28 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  (IsVirtual(pTab)
2bf48 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
2bf49 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2bf4a 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
2bf4b 20 20 20 20 20 20 69 66 28 20 69 3e 30 20 26 26        if( i>0 &&
2bf4c 20 7a 54 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20   zTName==0 ){.  
2bf4d 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72 75              stru
2bf4e 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2bf4f 2a 70 4c 65 66 74 20 3d 20 26 70 54 61 62 4c 69  *pLeft = &pTabLi
2bf50 73 74 2d 3e 61 5b 69 2d 31 5d 3b 0a 20 20 20 20  st->a[i-1];.    
2bf51 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28 70            if( (p
2bf52 4c 65 66 74 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65  Left[1].jointype
2bf53 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 29 21 3d   & JT_NATURAL)!=
2bf54 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  0 &&.           
2bf55 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6c               col
2bf56 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66 74 2d 3e  umnIndex(pLeft->
2bf57 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20  pTab, zName)>=0 
2bf58 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2bf59 20 20 20 2f 2a 20 49 6e 20 61 20 4e 41 54 55 52     /* In a NATUR
2bf5a 41 4c 20 6a 6f 69 6e 2c 20 6f 6d 69 74 20 74 68  AL join, omit th
2bf5b 65 20 6a 6f 69 6e 20 63 6f 6c 75 6d 6e 73 20 66  e join columns f
2bf5c 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 20 20 20  rom the .       
2bf5d 20 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c           ** tabl
2bf5e 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 2a  e on the right *
2bf5f 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
2bf60 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2bf61 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2bf62 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
2bf63 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78  lite3IdListIndex
2bf64 28 70 4c 65 66 74 5b 31 5d 2e 70 55 73 69 6e 67  (pLeft[1].pUsing
2bf65 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20  , zName)>=0 ){. 
2bf66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2bf67 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77 69 74 68  * In a join with
2bf68 20 61 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2c   a USING clause,
2bf69 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73 20 69 6e   omit columns in
2bf6a 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20   the.           
2bf6b 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 63 6c       ** using cl
2bf6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65 20 74 61  ause from the ta
2bf6d 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74  ble on the right
2bf6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  . */.           
2bf6f 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2bf70 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
2bf71 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2bf72 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74 20           pRight 
2bf73 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  = sqlite3Expr(db
2bf74 2c 20 54 4b 5f 49 44 2c 20 7a 4e 61 6d 65 29 3b  , TK_ID, zName);
2bf75 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 43 6f  .            zCo
2bf76 6c 6e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20  lname = zName;. 
2bf77 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46 72             zToFr
2bf78 65 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ee = 0;.        
2bf79 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65      if( longName
2bf7a 73 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e 6e  s || pTabList->n
2bf7b 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20 20  Src>1 ){.       
2bf7c 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65         Expr *pLe
2bf7d 66 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ft;.            
2bf7e 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65    pLeft = sqlite
2bf7f 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44 2c  3Expr(db, TK_ID,
2bf80 20 7a 54 61 62 4e 61 6d 65 29 3b 0a 20 20 20 20   zTabName);.    
2bf81 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 20            pExpr 
2bf82 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2bf83 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70  Parse, TK_DOT, p
2bf84 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29  Left, pRight, 0)
2bf85 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2bf86 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 29 7b  if( longNames ){
2bf87 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2bf88 20 7a 43 6f 6c 6e 61 6d 65 20 3d 20 73 71 6c 69   zColname = sqli
2bf89 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2bf8a 25 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d 65  %s.%s", zTabName
2bf8b 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
2bf8c 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46 72 65            zToFre
2bf8d 65 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a 20 20  e = zColname;.  
2bf8e 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2bf8f 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
2bf90 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2bf91 45 78 70 72 20 3d 20 70 52 69 67 68 74 3b 0a 20  Expr = pRight;. 
2bf92 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2bf93 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20           pNew = 
2bf94 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2bf95 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4e  ppend(pParse, pN
2bf96 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  ew, pExpr);.    
2bf97 20 20 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d 65          sColname
2bf98 2e 7a 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a 20  .z = zColname;. 
2bf99 20 20 20 20 20 20 20 20 20 20 20 73 43 6f 6c 6e             sColn
2bf9a 61 6d 65 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53  ame.n = sqlite3S
2bf9b 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 6e 61 6d 65  trlen30(zColname
2bf9c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
2bf9d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65  qlite3ExprListSe
2bf9e 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
2bf9f 65 77 2c 20 26 73 43 6f 6c 6e 61 6d 65 2c 20 30  ew, &sColname, 0
2bfa0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
2bfa1 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2bfa2 20 7a 54 6f 46 72 65 65 29 3b 0a 20 20 20 20 20   zToFree);.     
2bfa3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2bfa4 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 74 61  .        if( !ta
2bfa5 62 6c 65 53 65 65 6e 20 29 7b 0a 20 20 20 20 20  bleSeen ){.     
2bfa6 20 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65 20       if( zTName 
2bfa7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
2bfa8 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2bfa9 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
2bfaa 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 4e 61  table: %s", zTNa
2bfab 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  me);.          }
2bfac 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2bfad 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2bfae 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 74 61  g(pParse, "no ta
2bfaf 62 6c 65 73 20 73 70 65 63 69 66 69 65 64 22 29  bles specified")
2bfb0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2bfb1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2bfb2 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2bfb3 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2bfb4 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20  db, pEList);.   
2bfb5 20 70 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e 65   p->pEList = pNe
2bfb6 77 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54  w;.  }.#if SQLIT
2bfb7 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69  E_MAX_COLUMN.  i
2bfb8 66 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20  f( p->pEList && 
2bfb9 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
2bfba 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
2bfbb 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
2bfbc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2bfbd 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2bfbe 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e  "too many column
2bfbf 73 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 22  s in result set"
2bfc0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
2bfc1 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
2bfc2 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4e 6f  nue;.}../*.** No
2bfc3 2d 6f 70 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  -op routine for 
2bfc4 74 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77  the parse-tree w
2bfc5 61 6c 6b 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  alker..**.** Whe
2bfc6 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
2bfc7 73 20 74 68 65 20 57 61 6c 6b 65 72 2e 78 45 78  s the Walker.xEx
2bfc8 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e 20  prCallback then 
2bfc9 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73  expression trees
2bfca 0a 2a 2a 20 61 72 65 20 77 61 6c 6b 65 64 20 77  .** are walked w
2bfcb 69 74 68 6f 75 74 20 61 6e 79 20 61 63 74 69 6f  ithout any actio
2bfcc 6e 73 20 62 65 69 6e 67 20 74 61 6b 65 6e 20 61  ns being taken a
2bfcd 74 20 65 61 63 68 20 6e 6f 64 65 2e 20 20 50 72  t each node.  Pr
2bfce 65 73 75 6d 61 62 6c 79 2c 0a 2a 2a 20 77 68 65  esumably,.** whe
2bfcf 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
2bfd0 73 20 75 73 65 64 20 66 6f 72 20 57 61 6c 6b 65  s used for Walke
2bfd1 72 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20  r.xExprCallback 
2bfd2 74 68 65 6e 20 0a 2a 2a 20 57 61 6c 6b 65 72 2e  then .** Walker.
2bfd3 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
2bfd4 69 73 20 73 65 74 20 74 6f 20 64 6f 20 73 6f 6d  is set to do som
2bfd5 65 74 68 69 6e 67 20 75 73 65 66 75 6c 20 66 6f  ething useful fo
2bfd6 72 20 65 76 65 72 79 20 0a 2a 2a 20 73 75 62 71  r every .** subq
2bfd7 75 65 72 79 20 69 6e 20 74 68 65 20 70 61 72 73  uery in the pars
2bfd8 65 72 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74  er tree..*/.stat
2bfd9 69 63 20 69 6e 74 20 65 78 70 72 57 61 6c 6b 4e  ic int exprWalkN
2bfda 6f 6f 70 28 57 61 6c 6b 65 72 20 2a 4e 6f 74 55  oop(Walker *NotU
2bfdb 73 65 64 2c 20 45 78 70 72 20 2a 4e 6f 74 55 73  sed, Expr *NotUs
2bfdc 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  ed2){.  UNUSED_P
2bfdd 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
2bfde 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
2bfdf 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
2bfe0 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  nue;.}../*.** Th
2bfe1 69 73 20 72 6f 75 74 69 6e 65 20 22 65 78 70 61  is routine "expa
2bfe2 6e 64 73 22 20 61 20 53 45 4c 45 43 54 20 73 74  nds" a SELECT st
2bfe3 61 74 65 6d 65 6e 74 20 61 6e 64 20 61 6c 6c 20  atement and all 
2bfe4 6f 66 20 69 74 73 20 73 75 62 71 75 65 72 69 65  of its subquerie
2bfe5 73 2e 0a 2a 2a 20 46 6f 72 20 61 64 64 69 74 69  s..** For additi
2bfe6 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
2bfe7 20 6f 6e 20 77 68 61 74 20 69 74 20 6d 65 61 6e   on what it mean
2bfe8 73 20 74 6f 20 22 65 78 70 61 6e 64 22 20 61 20  s to "expand" a 
2bfe9 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d  SELECT.** statem
2bfea 65 6e 74 2c 20 73 65 65 20 74 68 65 20 63 6f 6d  ent, see the com
2bfeb 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 65 6c 65  ment on the sele
2bfec 63 74 45 78 70 61 6e 64 20 77 6f 72 6b 65 72 20  ctExpand worker 
2bfed 63 61 6c 6c 62 61 63 6b 20 61 62 6f 76 65 2e 0a  callback above..
2bfee 2a 2a 0a 2a 2a 20 45 78 70 61 6e 64 69 6e 67 20  **.** Expanding 
2bfef 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
2bff0 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20  nt is the first 
2bff1 73 74 65 70 20 69 6e 20 70 72 6f 63 65 73 73 69  step in processi
2bff2 6e 67 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73  ng a.** SELECT s
2bff3 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 53  tatement.  The S
2bff4 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2bff5 6d 75 73 74 20 62 65 20 65 78 70 61 6e 64 65 64  must be expanded
2bff6 20 62 65 66 6f 72 65 0a 2a 2a 20 6e 61 6d 65 20   before.** name 
2bff7 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 70 65  resolution is pe
2bff8 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  rformed..**.** I
2bff9 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20  f anything goes 
2bffa 77 72 6f 6e 67 2c 20 61 6e 20 65 72 72 6f 72 20  wrong, an error 
2bffb 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74  message is writt
2bffc 65 6e 20 69 6e 74 6f 20 70 50 61 72 73 65 2e 0a  en into pParse..
2bffd 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66  ** The calling f
2bffe 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65 74 65  unction can dete
2bfff 63 74 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 62  ct the problem b
2c000 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 70 50 61  y looking at pPa
2c001 72 73 65 2d 3e 6e 45 72 72 0a 2a 2a 20 61 6e 64  rse->nErr.** and
2c002 2f 6f 72 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  /or pParse->db->
2c003 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2f  mallocFailed..*/
2c004 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
2c005 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64  ite3SelectExpand
2c006 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2c007 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29  Select *pSelect)
2c008 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20  {.  Walker w;.  
2c009 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  w.xSelectCallbac
2c00a 6b 20 3d 20 73 65 6c 65 63 74 45 78 70 61 6e 64  k = selectExpand
2c00b 65 72 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c  er;.  w.xExprCal
2c00c 6c 62 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b  lback = exprWalk
2c00d 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65  Noop;.  w.pParse
2c00e 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c   = pParse;.  sql
2c00f 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26  ite3WalkSelect(&
2c010 77 2c 20 70 53 65 6c 65 63 74 29 3b 0a 7d 0a 0a  w, pSelect);.}..
2c011 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c012 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 2f 2a  OMIT_SUBQUERY./*
2c013 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 57 61  .** This is a Wa
2c014 6c 6b 65 72 2e 78 53 65 6c 65 63 74 43 61 6c 6c  lker.xSelectCall
2c015 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 66 6f  back callback fo
2c016 72 20 74 68 65 20 73 71 6c 69 74 65 33 53 65 6c  r the sqlite3Sel
2c017 65 63 74 54 79 70 65 49 6e 66 6f 28 29 0a 2a 2a  ectTypeInfo().**
2c018 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
2c019 2a 20 46 6f 72 20 65 61 63 68 20 46 52 4f 4d 2d  * For each FROM-
2c01a 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 2c  clause subquery,
2c01b 20 61 64 64 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70   add Column.zTyp
2c01c 65 20 61 6e 64 20 43 6f 6c 75 6d 6e 2e 7a 43 6f  e and Column.zCo
2c01d 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  ll.** informatio
2c01e 6e 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73  n to the Table s
2c01f 74 72 75 63 74 75 72 65 20 74 68 61 74 20 72 65  tructure that re
2c020 70 72 65 73 65 6e 74 73 20 74 68 65 20 72 65 73  presents the res
2c021 75 6c 74 20 73 65 74 0a 2a 2a 20 6f 66 20 74 68  ult set.** of th
2c022 61 74 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a  at subquery..**.
2c023 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74 72  ** The Table str
2c024 75 63 74 75 72 65 20 74 68 61 74 20 72 65 70 72  ucture that repr
2c025 65 73 65 6e 74 73 20 74 68 65 20 72 65 73 75 6c  esents the resul
2c026 74 20 73 65 74 20 77 61 73 20 63 6f 6e 73 74 72  t set was constr
2c027 75 63 74 65 64 0a 2a 2a 20 62 79 20 73 65 6c 65  ucted.** by sele
2c028 63 74 45 78 70 61 6e 64 65 72 28 29 20 62 75 74  ctExpander() but
2c029 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 63 6f   the type and co
2c02a 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74  llation informat
2c02b 69 6f 6e 20 77 61 73 20 6f 6d 69 74 74 65 64 0a  ion was omitted.
2c02c 2a 2a 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74  ** at that point
2c02d 20 62 65 63 61 75 73 65 20 69 64 65 6e 74 69 66   because identif
2c02e 69 65 72 73 20 68 61 64 20 6e 6f 74 20 79 65 74  iers had not yet
2c02f 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20   been resolved. 
2c030 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   This.** routine
2c031 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
2c032 20 69 64 65 6e 74 69 66 69 65 72 20 72 65 73 6f   identifier reso
2c033 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  lution..*/.stati
2c034 63 20 69 6e 74 20 73 65 6c 65 63 74 41 64 64 53  c int selectAddS
2c035 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f 28  ubqueryTypeInfo(
2c036 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
2c037 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50   Select *p){.  P
2c038 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20 20  arse *pParse;.  
2c039 69 6e 74 20 69 3b 0a 20 20 53 72 63 4c 69 73 74  int i;.  SrcList
2c03a 20 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 74   *pTabList;.  st
2c03b 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2c03c 6d 20 2a 70 46 72 6f 6d 3b 0a 0a 20 20 61 73 73  m *pFrom;..  ass
2c03d 65 72 74 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73  ert( p->selFlags
2c03e 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29   & SF_Resolved )
2c03f 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
2c040 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 48 61  selFlags & SF_Ha
2c041 73 54 79 70 65 49 6e 66 6f 29 3d 3d 30 20 29 3b  sTypeInfo)==0 );
2c042 0a 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c  .  p->selFlags |
2c043 3d 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f  = SF_HasTypeInfo
2c044 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 61  ;.  pParse = pWa
2c045 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20  lker->pParse;.  
2c046 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53  pTabList = p->pS
2c047 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  rc;.  for(i=0, p
2c048 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61  From=pTabList->a
2c049 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
2c04a 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b  rc; i++, pFrom++
2c04b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
2c04c 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62  ab = pFrom->pTab
2c04d 3b 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59 53  ;.    if( ALWAYS
2c04e 28 70 54 61 62 21 3d 30 29 20 26 26 20 28 70 54  (pTab!=0) && (pT
2c04f 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
2c050 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20  F_Ephemeral)!=0 
2c051 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 75  ){.      /* A su
2c052 62 2d 71 75 65 72 79 20 69 6e 20 74 68 65 20 46  b-query in the F
2c053 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20  ROM clause of a 
2c054 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20  SELECT */.      
2c055 53 65 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20 70  Select *pSel = p
2c056 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20  From->pSelect;. 
2c057 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65       assert( pSe
2c058 6c 20 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  l );.      while
2c059 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20 29  ( pSel->pPrior )
2c05a 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70 50   pSel = pSel->pP
2c05b 72 69 6f 72 3b 0a 20 20 20 20 20 20 73 65 6c 65  rior;.      sele
2c05c 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41  ctAddColumnTypeA
2c05d 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 72  ndCollation(pPar
2c05e 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20  se, pTab->nCol, 
2c05f 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c  pTab->aCol, pSel
2c060 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
2c061 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
2c062 75 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f  ue;.}.#endif.../
2c063 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2c064 65 20 61 64 64 73 20 64 61 74 61 74 79 70 65 20  e adds datatype 
2c065 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  and collating se
2c066 71 75 65 6e 63 65 20 69 6e 66 6f 72 6d 61 74 69  quence informati
2c067 6f 6e 20 74 6f 0a 2a 2a 20 74 68 65 20 54 61 62  on to.** the Tab
2c068 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 6f 66  le structures of
2c069 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65   all FROM-clause
2c06a 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 61   subqueries in a
2c06b 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65  .** SELECT state
2c06c 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20  ment..**.** Use 
2c06d 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 66 74  this routine aft
2c06e 65 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69  er name resoluti
2c06f 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
2c070 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  id sqlite3Select
2c071 41 64 64 54 79 70 65 49 6e 66 6f 28 50 61 72 73  AddTypeInfo(Pars
2c072 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63  e *pParse, Selec
2c073 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 23 69 66  t *pSelect){.#if
2c074 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2c075 5f 53 55 42 51 55 45 52 59 0a 20 20 57 61 6c 6b  _SUBQUERY.  Walk
2c076 65 72 20 77 3b 0a 20 20 77 2e 78 53 65 6c 65 63  er w;.  w.xSelec
2c077 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65  tCallback = sele
2c078 63 74 41 64 64 53 75 62 71 75 65 72 79 54 79 70  ctAddSubqueryTyp
2c079 65 49 6e 66 6f 3b 0a 20 20 77 2e 78 45 78 70 72  eInfo;.  w.xExpr
2c07a 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 57  Callback = exprW
2c07b 61 6c 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50 61  alkNoop;.  w.pPa
2c07c 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
2c07d 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
2c07e 74 28 26 77 2c 20 70 53 65 6c 65 63 74 29 3b 0a  t(&w, pSelect);.
2c07f 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  #endif.}.../*.**
2c080 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
2c081 74 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  ts of a SELECT s
2c082 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 70 72 6f  tatement for pro
2c083 63 65 73 73 69 6e 67 2e 20 20 54 68 65 0a 2a 2a  cessing.  The.**
2c084 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 63   following is ac
2c085 63 6f 6d 70 6c 69 73 68 65 64 3a 0a 2a 2a 0a 2a  complished:.**.*
2c086 2a 20 20 20 20 20 2a 20 20 56 44 42 45 20 43 75  *     *  VDBE Cu
2c087 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65  rsor numbers are
2c088 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c 6c   assigned to all
2c089 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 74 65 72   FROM-clause ter
2c08a 6d 73 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 45 70  ms..**     *  Ep
2c08b 68 65 6d 65 72 61 6c 20 54 61 62 6c 65 20 6f 62  hemeral Table ob
2c08c 6a 65 63 74 73 20 61 72 65 20 63 72 65 61 74 65  jects are create
2c08d 64 20 66 6f 72 20 61 6c 6c 20 46 52 4f 4d 2d 63  d for all FROM-c
2c08e 6c 61 75 73 65 20 73 75 62 71 75 65 72 69 65 73  lause subqueries
2c08f 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 4f 4e 20 61  ..**     *  ON a
2c090 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73  nd USING clauses
2c091 20 61 72 65 20 73 68 69 66 74 65 64 20 69 6e 74   are shifted int
2c092 6f 20 57 48 45 52 45 20 73 74 61 74 65 6d 65 6e  o WHERE statemen
2c093 74 73 0a 2a 2a 20 20 20 20 20 2a 20 20 57 69 6c  ts.**     *  Wil
2c094 64 63 61 72 64 73 20 22 2a 22 20 61 6e 64 20 22  dcards "*" and "
2c095 54 41 42 4c 45 2e 2a 22 20 69 6e 20 72 65 73 75  TABLE.*" in resu
2c096 6c 74 20 73 65 74 73 20 61 72 65 20 65 78 70 61  lt sets are expa
2c097 6e 64 65 64 2e 0a 2a 2a 20 20 20 20 20 2a 20 20  nded..**     *  
2c098 49 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20 65  Identifiers in e
2c099 78 70 72 65 73 73 69 6f 6e 20 61 72 65 20 6d 61  xpression are ma
2c09a 74 63 68 65 64 20 74 6f 20 74 61 62 6c 65 73 2e  tched to tables.
2c09b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2c09c 69 6e 65 20 61 63 74 73 20 72 65 63 75 72 73 69  ine acts recursi
2c09d 76 65 6c 79 20 6f 6e 20 61 6c 6c 20 73 75 62 71  vely on all subq
2c09e 75 65 72 69 65 73 20 77 69 74 68 69 6e 20 74 68  ueries within th
2c09f 65 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c  e SELECT..*/.SQL
2c0a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2c0a1 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72   sqlite3SelectPr
2c0a2 65 70 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ep(.  Parse *pPa
2c0a3 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
2c0a4 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
2c0a5 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
2c0a6 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
2c0a7 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
2c0a8 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64  tement being cod
2c0a9 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  ed. */.  NameCon
2c0aa 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20  text *pOuterNC  
2c0ab 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
2c0ac 66 6f 72 20 63 6f 6e 74 61 69 6e 65 72 20 2a 2f  for container */
2c0ad 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
2c0ae 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  b;.  if( NEVER(p
2c0af 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  ==0) ) return;. 
2c0b0 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
2c0b1 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c  ;.  if( p->selFl
2c0b2 61 67 73 20 26 20 53 46 5f 48 61 73 54 79 70 65  ags & SF_HasType
2c0b3 49 6e 66 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20  Info ) return;. 
2c0b4 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
2c0b5 70 61 6e 64 28 70 50 61 72 73 65 2c 20 70 29 3b  pand(pParse, p);
2c0b6 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
2c0b7 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
2c0b8 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
2c0b9 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  ;.  sqlite3Resol
2c0ba 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 50  veSelectNames(pP
2c0bb 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e  arse, p, pOuterN
2c0bc 43 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  C);.  if( pParse
2c0bd 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
2c0be 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
2c0bf 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  urn;.  sqlite3Se
2c0c0 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66 6f 28  lectAddTypeInfo(
2c0c1 70 50 61 72 73 65 2c 20 70 29 3b 0a 7d 0a 0a 2f  pParse, p);.}../
2c0c2 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61  *.** Reset the a
2c0c3 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c  ggregate accumul
2c0c4 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ator..**.** The 
2c0c5 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75  aggregate accumu
2c0c6 6c 61 74 6f 72 20 69 73 20 61 20 73 65 74 20 6f  lator is a set o
2c0c7 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74  f memory cells t
2c0c8 68 61 74 20 68 6f 6c 64 0a 2a 2a 20 69 6e 74 65  hat hold.** inte
2c0c9 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c 74 73  rmediate results
2c0ca 20 77 68 69 6c 65 20 63 61 6c 63 75 6c 61 74 69   while calculati
2c0cb 6e 67 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e  ng an aggregate.
2c0cc 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
2c0cd 65 20 73 69 6d 70 6c 79 20 73 74 6f 72 65 73 20  e simply stores 
2c0ce 4e 55 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f 66 20  NULLs in all of 
2c0cf 74 68 6f 73 65 20 6d 65 6d 6f 72 79 20 63 65 6c  those memory cel
2c0d0 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ls..*/.static vo
2c0d1 69 64 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61  id resetAccumula
2c0d2 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  tor(Parse *pPars
2c0d3 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  e, AggInfo *pAgg
2c0d4 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76  Info){.  Vdbe *v
2c0d5 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2c0d6 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  ;.  int i;.  str
2c0d7 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63  uct AggInfo_func
2c0d8 20 2a 70 46 75 6e 63 3b 0a 20 20 69 66 28 20 70   *pFunc;.  if( p
2c0d9 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2b 70  AggInfo->nFunc+p
2c0da 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e  AggInfo->nColumn
2c0db 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
2c0dc 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  n;.  }.  for(i=0
2c0dd 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 43  ; i<pAggInfo->nC
2c0de 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
2c0df 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c0e0 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
2c0e1 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  , pAggInfo->aCol
2c0e2 5b 69 5d 2e 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20  [i].iMem);.  }. 
2c0e3 20 66 6f 72 28 70 46 75 6e 63 3d 70 41 67 67 49   for(pFunc=pAggI
2c0e4 6e 66 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d 30 3b  nfo->aFunc, i=0;
2c0e5 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75   i<pAggInfo->nFu
2c0e6 6e 63 3b 20 69 2b 2b 2c 20 70 46 75 6e 63 2b 2b  nc; i++, pFunc++
2c0e7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2c0e8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2c0e9 75 6c 6c 2c 20 30 2c 20 70 46 75 6e 63 2d 3e 69  ull, 0, pFunc->i
2c0ea 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 46  Mem);.    if( pF
2c0eb 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 3e 3d  unc->iDistinct>=
2c0ec 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  0 ){.      Expr 
2c0ed 2a 70 45 20 3d 20 70 46 75 6e 63 2d 3e 70 45 78  *pE = pFunc->pEx
2c0ee 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  pr;.      assert
2c0ef 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2c0f0 74 79 28 70 45 2c 20 45 50 5f 78 49 73 53 65 6c  ty(pE, EP_xIsSel
2c0f1 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66  ect) );.      if
2c0f2 28 20 70 45 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30  ( pE->x.pList==0
2c0f3 20 7c 7c 20 70 45 2d 3e 78 2e 70 4c 69 73 74 2d   || pE->x.pList-
2c0f4 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20  >nExpr!=1 ){.   
2c0f5 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2c0f6 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 44 49  rMsg(pParse, "DI
2c0f7 53 54 49 4e 43 54 20 61 67 67 72 65 67 61 74 65  STINCT aggregate
2c0f8 73 20 6d 75 73 74 20 68 61 76 65 20 65 78 61 63  s must have exac
2c0f9 74 6c 79 20 6f 6e 65 20 22 0a 20 20 20 20 20 20  tly one ".      
2c0fa 20 20 20 20 20 22 61 72 67 75 6d 65 6e 74 22 29       "argument")
2c0fb 3b 0a 20 20 20 20 20 20 20 20 70 46 75 6e 63 2d  ;.        pFunc-
2c0fc 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b  >iDistinct = -1;
2c0fd 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2c0fe 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
2c0ff 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66  KeyInfo = keyInf
2c100 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50  oFromExprList(pP
2c101 61 72 73 65 2c 20 70 45 2d 3e 78 2e 70 4c 69 73  arse, pE->x.pLis
2c102 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
2c103 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2c104 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2c105 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69  l, pFunc->iDisti
2c106 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  nct, 0, 0,.     
2c107 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c108 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
2c109 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  Info, P4_KEYINFO
2c10a 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20  _HANDOFF);.     
2c10b 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
2c10c 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
2c10d 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65 20   OP_AggFinalize 
2c10e 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65 72 79  opcode for every
2c10f 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
2c110 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 41 67  ion.** in the Ag
2c111 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  gInfo structure.
2c112 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2c113 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74  finalizeAggFunct
2c114 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50 61 72  ions(Parse *pPar
2c115 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
2c116 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
2c117 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2c118 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
2c119 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
2c11a 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69 3d 30  c *pF;.  for(i=0
2c11b 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61  , pF=pAggInfo->a
2c11c 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f  Func; i<pAggInfo
2c11d 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46  ->nFunc; i++, pF
2c11e 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73  ++){.    ExprLis
2c11f 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70  t *pList = pF->p
2c120 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20  Expr->x.pList;. 
2c121 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2c122 48 61 73 50 72 6f 70 65 72 74 79 28 70 46 2d 3e  HasProperty(pF->
2c123 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2c124 65 63 74 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  ect) );.    sqli
2c125 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2c126 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20 70 46   OP_AggFinal, pF
2c127 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20 3f 20  ->iMem, pList ? 
2c128 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30  pList->nExpr : 0
2c129 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2c12a 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64             (void
2c12b 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34 5f  *)pF->pFunc, P4_
2c12c 46 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a 7d 0a  FUNCDEF);.  }.}.
2c12d 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74 68  ./*.** Update th
2c12e 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d 65  e accumulator me
2c12f 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 61  mory cells for a
2c130 6e 20 61 67 67 72 65 67 61 74 65 20 62 61 73 65  n aggregate base
2c131 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72  d on.** the curr
2c132 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74  ent cursor posit
2c133 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
2c134 6f 69 64 20 75 70 64 61 74 65 41 63 63 75 6d 75  oid updateAccumu
2c135 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61  lator(Parse *pPa
2c136 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41  rse, AggInfo *pA
2c137 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20  ggInfo){.  Vdbe 
2c138 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2c139 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  be;.  int i;.  s
2c13a 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
2c13b 6e 63 20 2a 70 46 3b 0a 20 20 73 74 72 75 63 74  nc *pF;.  struct
2c13c 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43   AggInfo_col *pC
2c13d 3b 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64  ;..  pAggInfo->d
2c13e 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20  irectMode = 1;. 
2c13f 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2c140 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a  eClear(pParse);.
2c141 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41    for(i=0, pF=pA
2c142 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69  ggInfo->aFunc; i
2c143 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63  <pAggInfo->nFunc
2c144 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20  ; i++, pF++){.  
2c145 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20    int nArg;.    
2c146 69 6e 74 20 61 64 64 72 4e 65 78 74 20 3d 20 30  int addrNext = 0
2c147 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67 67  ;.    int regAgg
2c148 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a  ;.    ExprList *
2c149 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70  pList = pF->pExp
2c14a 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20  r->x.pList;.    
2c14b 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2c14c 50 72 6f 70 65 72 74 79 28 70 46 2d 3e 70 45 78  Property(pF->pEx
2c14d 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
2c14e 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69  ) );.    if( pLi
2c14f 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 41 72 67  st ){.      nArg
2c150 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
2c151 0a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d 20  .      regAgg = 
2c152 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
2c153 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 41 72 67  nge(pParse, nArg
2c154 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c155 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74  ExprCodeExprList
2c156 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20  (pParse, pList, 
2c157 72 65 67 41 67 67 2c 20 30 29 3b 0a 20 20 20 20  regAgg, 0);.    
2c158 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 41 72  }else{.      nAr
2c159 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 67  g = 0;.      reg
2c15a 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Agg = 0;.    }. 
2c15b 20 20 20 69 66 28 20 70 46 2d 3e 69 44 69 73 74     if( pF->iDist
2c15c 69 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20  inct>=0 ){.     
2c15d 20 61 64 64 72 4e 65 78 74 20 3d 20 73 71 6c 69   addrNext = sqli
2c15e 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2c15f 28 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  (v);.      asser
2c160 74 28 20 6e 41 72 67 3d 3d 31 20 29 3b 0a 20 20  t( nArg==1 );.  
2c161 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63 74      codeDistinct
2c162 28 70 50 61 72 73 65 2c 20 70 46 2d 3e 69 44 69  (pParse, pF->iDi
2c163 73 74 69 6e 63 74 2c 20 61 64 64 72 4e 65 78 74  stinct, addrNext
2c164 2c 20 31 2c 20 72 65 67 41 67 67 29 3b 0a 20 20  , 1, regAgg);.  
2c165 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e    }.    if( pF->
2c166 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53  pFunc->flags & S
2c167 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
2c168 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c  OLL ){.      Col
2c169 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b  lSeq *pColl = 0;
2c16a 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78  .      struct Ex
2c16b 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
2c16c 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  em;.      int j;
2c16d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2c16e 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 70  List!=0 );  /* p
2c16f 4c 69 73 74 21 3d 30 20 69 66 20 70 46 2d 3e 70  List!=0 if pF->p
2c170 46 75 6e 63 20 68 61 73 20 4e 45 45 44 43 4f 4c  Func has NEEDCOL
2c171 4c 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6a  L */.      for(j
2c172 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d  =0, pItem=pList-
2c173 3e 61 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a 3c  >a; !pColl && j<
2c174 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65 6d  nArg; j++, pItem
2c175 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f  ++){.        pCo
2c176 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
2c177 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2c178 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20  pItem->pExpr);. 
2c179 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2c17a 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
2c17b 20 20 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72 73     pColl = pPars
2c17c 65 2d 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c  e->db->pDfltColl
2c17d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c17e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c17f 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c  4(v, OP_CollSeq,
2c180 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20   0, 0, 0, (char 
2c181 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c  *)pColl, P4_COLL
2c182 53 45 51 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  SEQ);.    }.    
2c183 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c184 34 28 76 2c 20 4f 50 5f 41 67 67 53 74 65 70 2c  4(v, OP_AggStep,
2c185 20 30 2c 20 72 65 67 41 67 67 2c 20 70 46 2d 3e   0, regAgg, pF->
2c186 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20  iMem,.          
2c187 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69              (voi
2c188 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34  d*)pF->pFunc, P4
2c189 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 73  _FUNCDEF);.    s
2c18a 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2c18b 50 35 28 76 2c 20 28 75 38 29 6e 41 72 67 29 3b  P5(v, (u8)nArg);
2c18c 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
2c18d 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61  aseTempRange(pPa
2c18e 72 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41 72  rse, regAgg, nAr
2c18f 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  g);.    sqlite3E
2c190 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
2c191 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
2c192 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20  egAgg, nArg);.  
2c193 20 20 69 66 28 20 61 64 64 72 4e 65 78 74 20 29    if( addrNext )
2c194 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2c195 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2c196 76 2c 20 61 64 64 72 4e 65 78 74 29 3b 0a 20 20  v, addrNext);.  
2c197 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c198 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65  acheClear(pParse
2c199 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  );.    }.  }.  f
2c19a 6f 72 28 69 3d 30 2c 20 70 43 3d 70 41 67 67 49  or(i=0, pC=pAggI
2c19b 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70 41 67  nfo->aCol; i<pAg
2c19c 67 49 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75 6c 61  gInfo->nAccumula
2c19d 74 6f 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b 29 7b  tor; i++, pC++){
2c19e 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2c19f 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43 2d  Code(pParse, pC-
2c1a0 3e 70 45 78 70 72 2c 20 70 43 2d 3e 69 4d 65 6d  >pExpr, pC->iMem
2c1a1 29 3b 0a 20 20 7d 0a 20 20 70 41 67 67 49 6e 66  );.  }.  pAggInf
2c1a2 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20  o->directMode = 
2c1a3 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  0;.  sqlite3Expr
2c1a4 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
2c1a5 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  e);.}../*.** Gen
2c1a6 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
2c1a7 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2c1a8 65 6e 74 20 67 69 76 65 6e 20 69 6e 20 74 68 65  ent given in the
2c1a9 20 70 20 61 72 67 75 6d 65 6e 74 2e 20 20 0a 2a   p argument.  .*
2c1aa 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73  *.** The results
2c1ab 20 61 72 65 20 64 69 73 74 72 69 62 75 74 65 64   are distributed
2c1ac 20 69 6e 20 76 61 72 69 6f 75 73 20 77 61 79 73   in various ways
2c1ad 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
2c1ae 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66  e.** contents of
2c1af 20 74 68 65 20 53 65 6c 65 63 74 44 65 73 74 20   the SelectDest 
2c1b0 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65  structure pointe
2c1b1 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e 74  d to by argument
2c1b2 20 70 44 65 73 74 0a 2a 2a 20 61 73 20 66 6f 6c   pDest.** as fol
2c1b3 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lows:.**.**     
2c1b4 70 44 65 73 74 2d 3e 65 44 65 73 74 20 20 20 20  pDest->eDest    
2c1b5 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 2d 2d  Result.**     --
2c1b6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d  ----------    --
2c1b7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c1b8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c1b9 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20  ---------.**    
2c1ba 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20   SRT_Output     
2c1bb 20 47 65 6e 65 72 61 74 65 20 61 20 72 6f 77 20   Generate a row 
2c1bc 6f 66 20 6f 75 74 70 75 74 20 28 75 73 69 6e 67  of output (using
2c1bd 20 74 68 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f   the OP_ResultRo
2c1be 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  w.**            
2c1bf 20 20 20 20 20 20 20 20 20 6f 70 63 6f 64 65 29           opcode)
2c1c0 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e   for each row in
2c1c1 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
2c1c2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 4d  .**.**     SRT_M
2c1c3 65 6d 20 20 20 20 20 20 20 20 20 4f 6e 6c 79 20  em         Only 
2c1c4 76 61 6c 69 64 20 69 66 20 74 68 65 20 72 65 73  valid if the res
2c1c5 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c 65 20  ult is a single 
2c1c6 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 20 20 20 20 20  column..**      
2c1c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
2c1c8 74 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63  tore the first c
2c1c9 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 69 72  olumn of the fir
2c1ca 73 74 20 72 65 73 75 6c 74 20 72 6f 77 0a 2a 2a  st result row.**
2c1cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1cc 20 20 20 20 20 69 6e 20 72 65 67 69 73 74 65 72       in register
2c1cd 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 74 68   pDest->iParm th
2c1ce 65 6e 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 72  en abandon the r
2c1cf 65 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  est.**          
2c1d0 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68             of th
2c1d1 65 20 71 75 65 72 79 2e 20 20 54 68 69 73 20 64  e query.  This d
2c1d2 65 73 74 69 6e 61 74 69 6f 6e 20 69 6d 70 6c 69  estination impli
2c1d3 65 73 20 22 4c 49 4d 49 54 20 31 22 2e 0a 2a 2a  es "LIMIT 1"..**
2c1d4 0a 2a 2a 20 20 20 20 20 53 52 54 5f 53 65 74 20  .**     SRT_Set 
2c1d5 20 20 20 20 20 20 20 20 54 68 65 20 72 65 73 75          The resu
2c1d6 6c 74 20 6d 75 73 74 20 62 65 20 61 20 73 69 6e  lt must be a sin
2c1d7 67 6c 65 20 63 6f 6c 75 6d 6e 2e 20 20 53 74 6f  gle column.  Sto
2c1d8 72 65 20 65 61 63 68 0a 2a 2a 20 20 20 20 20 20  re each.**      
2c1d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
2c1da 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 61 73 20  ow of result as 
2c1db 74 68 65 20 6b 65 79 20 69 6e 20 74 61 62 6c 65  the key in table
2c1dc 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 20 0a   pDest->iParm. .
2c1dd 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2c1de 20 20 20 20 20 20 20 41 70 70 6c 79 20 74 68 65         Apply the
2c1df 20 61 66 66 69 6e 69 74 79 20 70 44 65 73 74 2d   affinity pDest-
2c1e0 3e 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65  >affinity before
2c1e1 20 73 74 6f 72 69 6e 67 0a 2a 2a 20 20 20 20 20   storing.**     
2c1e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1e3 72 65 73 75 6c 74 73 2e 20 20 55 73 65 64 20 74  results.  Used t
2c1e4 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 49 4e 20  o implement "IN 
2c1e5 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 0a 2a  (SELECT ...)"..*
2c1e6 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 55 6e 69  *.**     SRT_Uni
2c1e7 6f 6e 20 20 20 20 20 20 20 53 74 6f 72 65 20 72  on       Store r
2c1e8 65 73 75 6c 74 73 20 61 73 20 61 20 6b 65 79 20  esults as a key 
2c1e9 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74  in a temporary t
2c1ea 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72  able pDest->iPar
2c1eb 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  m..**.**     SRT
2c1ec 5f 45 78 63 65 70 74 20 20 20 20 20 20 52 65 6d  _Except      Rem
2c1ed 6f 76 65 20 72 65 73 75 6c 74 73 20 66 72 6f 6d  ove results from
2c1ee 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
2c1ef 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72  able pDest->iPar
2c1f0 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  m..**.**     SRT
2c1f1 5f 54 61 62 6c 65 20 20 20 20 20 20 20 53 74 6f  _Table       Sto
2c1f2 72 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74 65  re results in te
2c1f3 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44  mporary table pD
2c1f4 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 20 20  est->iParm..**  
2c1f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1f6 20 20 20 54 68 69 73 20 69 73 20 6c 69 6b 65 20     This is like 
2c1f7 53 52 54 5f 45 70 68 65 6d 54 61 62 20 65 78 63  SRT_EphemTab exc
2c1f8 65 70 74 20 74 68 61 74 20 74 68 65 20 74 61 62  ept that the tab
2c1f9 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  le.**           
2c1fa 20 20 20 20 20 20 20 20 20 20 69 73 20 61 73 73            is ass
2c1fb 75 6d 65 64 20 74 6f 20 61 6c 72 65 61 64 79 20  umed to already 
2c1fc 62 65 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20  be open..**.**  
2c1fd 20 20 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20     SRT_EphemTab 
2c1fe 20 20 20 43 72 65 61 74 65 20 61 6e 20 74 65 6d     Create an tem
2c1ff 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44 65  porary table pDe
2c200 73 74 2d 3e 69 50 61 72 6d 20 61 6e 64 20 73 74  st->iParm and st
2c201 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ore.**          
2c202 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72             the r
2c203 65 73 75 6c 74 20 74 68 65 72 65 2e 20 54 68 65  esult there. The
2c204 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20   cursor is left 
2c205 6f 70 65 6e 20 61 66 74 65 72 0a 2a 2a 20 20 20  open after.**   
2c206 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c207 20 20 72 65 74 75 72 6e 69 6e 67 2e 20 20 54 68    returning.  Th
2c208 69 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f 54  is is like SRT_T
2c209 61 62 6c 65 20 65 78 63 65 70 74 20 74 68 61 74  able except that
2c20a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2c20b 20 20 20 20 20 20 20 20 74 68 69 73 20 64 65 73          this des
2c20c 74 69 6e 61 74 69 6f 6e 20 75 73 65 73 20 4f 50  tination uses OP
2c20d 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 74  _OpenEphemeral t
2c20e 6f 20 63 72 65 61 74 65 0a 2a 2a 20 20 20 20 20  o create.**     
2c20f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c210 74 68 65 20 74 61 62 6c 65 20 66 69 72 73 74 2e  the table first.
2c211 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 43  .**.**     SRT_C
2c212 6f 72 6f 75 74 69 6e 65 20 20 20 47 65 6e 65 72  oroutine   Gener
2c213 61 74 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65  ate a co-routine
2c214 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 61 20   that returns a 
2c215 6e 65 77 20 72 6f 77 20 6f 66 0a 2a 2a 20 20 20  new row of.**   
2c216 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c217 20 20 72 65 73 75 6c 74 73 20 65 61 63 68 20 74    results each t
2c218 69 6d 65 20 69 74 20 69 73 20 69 6e 76 6f 6b 65  ime it is invoke
2c219 64 2e 20 20 54 68 65 20 65 6e 74 72 79 20 70 6f  d.  The entry po
2c21a 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  int.**          
2c21b 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68             of th
2c21c 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20  e co-routine is 
2c21d 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
2c21e 65 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e  er pDest->iParm.
2c21f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45  .**.**     SRT_E
2c220 78 69 73 74 73 20 20 20 20 20 20 53 74 6f 72 65  xists      Store
2c221 20 61 20 31 20 69 6e 20 6d 65 6d 6f 72 79 20 63   a 1 in memory c
2c222 65 6c 6c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ell pDest->iParm
2c223 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 0a 2a   if the result.*
2c224 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2c225 20 20 20 20 20 20 73 65 74 20 69 73 20 6e 6f 74        set is not
2c226 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 20 20   empty..**.**   
2c227 20 20 53 52 54 5f 44 69 73 63 61 72 64 20 20 20    SRT_Discard   
2c228 20 20 54 68 72 6f 77 20 74 68 65 20 72 65 73 75    Throw the resu
2c229 6c 74 73 20 61 77 61 79 2e 20 20 54 68 69 73 20  lts away.  This 
2c22a 69 73 20 75 73 65 64 20 62 79 20 53 45 4c 45 43  is used by SELEC
2c22b 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  T.**            
2c22c 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d 65           stateme
2c22d 6e 74 73 20 77 69 74 68 69 6e 20 74 72 69 67 67  nts within trigg
2c22e 65 72 73 20 77 68 6f 73 65 20 6f 6e 6c 79 20 70  ers whose only p
2c22f 75 72 70 6f 73 65 20 69 73 0a 2a 2a 20 20 20 20  urpose is.**    
2c230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c231 20 74 68 65 20 73 69 64 65 2d 65 66 66 65 63 74   the side-effect
2c232 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  s of functions..
2c233 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2c234 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ne returns the n
2c235 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e  umber of errors.
2c236 20 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73 20    If any errors 
2c237 61 72 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72  are.** encounter
2c238 65 64 2c 20 74 68 65 6e 20 61 6e 20 61 70 70 72  ed, then an appr
2c239 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65  opriate error me
2c23a 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 69 6e  ssage is left in
2c23b 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72  .** pParse->zErr
2c23c 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  Msg..**.** This 
2c23d 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f 54  routine does NOT
2c23e 20 66 72 65 65 20 74 68 65 20 53 65 6c 65 63 74   free the Select
2c23f 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65   structure passe
2c240 64 20 69 6e 2e 20 20 54 68 65 0a 2a 2a 20 63 61  d in.  The.** ca
2c241 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6e  lling function n
2c242 65 65 64 73 20 74 6f 20 64 6f 20 74 68 61 74 2e  eeds to do that.
2c243 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2c244 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65  TE int sqlite3Se
2c245 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70  lect(.  Parse *p
2c246 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
2c247 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2c248 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
2c249 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
2c24a 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2c24b 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63  tatement being c
2c24c 6f 64 65 64 2e 20 2a 2f 0a 20 20 53 65 6c 65 63  oded. */.  Selec
2c24d 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 20 20  tDest *pDest    
2c24e 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
2c24f 77 69 74 68 20 74 68 65 20 71 75 65 72 79 20 72  with the query r
2c250 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69  esults */.){.  i
2c251 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
2c252 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2c253 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 65 72  unters */.  Wher
2c254 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20  eInfo *pWInfo;  
2c255 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f     /* Return fro
2c256 6d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  m sqlite3WhereBe
2c257 67 69 6e 28 29 20 2a 2f 0a 20 20 56 64 62 65 20  gin() */.  Vdbe 
2c258 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2c259 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
2c25a 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72 20 63   machine under c
2c25b 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  onstruction */. 
2c25c 20 69 6e 74 20 69 73 41 67 67 3b 20 20 20 20 20   int isAgg;     
2c25d 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2c25e 66 6f 72 20 73 65 6c 65 63 74 20 6c 69 73 74 73  for select lists
2c25f 20 6c 69 6b 65 20 22 63 6f 75 6e 74 28 2a 29 22   like "count(*)"
2c260 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2c261 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a 20  pEList;      /* 
2c262 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  List of columns 
2c263 74 6f 20 65 78 74 72 61 63 74 2e 20 2a 2f 0a 20  to extract. */. 
2c264 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
2c265 73 74 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  st;     /* List 
2c266 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 73 65 6c  of tables to sel
2c267 65 63 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 45 78  ect from */.  Ex
2c268 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20  pr *pWhere;     
2c269 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2c26a 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  E clause.  May b
2c26b 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72  e NULL */.  Expr
2c26c 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20  List *pOrderBy; 
2c26d 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
2c26e 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  BY clause.  May 
2c26f 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70  be NULL */.  Exp
2c270 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b  rList *pGroupBy;
2c271 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50      /* The GROUP
2c272 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79   BY clause.  May
2c273 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78   be NULL */.  Ex
2c274 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20  pr *pHaving;    
2c275 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49       /* The HAVI
2c276 4e 47 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  NG clause.  May 
2c277 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74  be NULL */.  int
2c278 20 69 73 44 69 73 74 69 6e 63 74 3b 20 20 20 20   isDistinct;    
2c279 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
2c27a 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  he DISTINCT keyw
2c27b 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
2c27c 2f 0a 20 20 69 6e 74 20 64 69 73 74 69 6e 63 74  /.  int distinct
2c27d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  ;          /* Ta
2c27e 62 6c 65 20 74 6f 20 75 73 65 20 66 6f 72 20 74  ble to use for t
2c27f 68 65 20 64 69 73 74 69 6e 63 74 20 73 65 74 20  he distinct set 
2c280 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b  */.  int rc = 1;
2c281 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
2c282 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 66  alue to return f
2c283 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f  rom this functio
2c284 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53  n */.  int addrS
2c285 6f 72 74 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a  ortIndex;     /*
2c286 20 41 64 64 72 65 73 73 20 6f 66 20 61 6e 20 4f   Address of an O
2c287 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
2c288 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  instruction */. 
2c289 20 41 67 67 49 6e 66 6f 20 73 41 67 67 49 6e 66   AggInfo sAggInf
2c28a 6f 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72  o;      /* Infor
2c28b 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 61  mation used by a
2c28c 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
2c28d 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 3b 20   */.  int iEnd; 
2c28e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c28f 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 65  Address of the e
2c290 6e 64 20 6f 66 20 74 68 65 20 71 75 65 72 79 20  nd of the query 
2c291 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2c292 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
2c293 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
2c294 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 64 62 20  ection */..  db 
2c295 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2c296 69 66 28 20 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e  if( p==0 || db->
2c297 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
2c298 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a  pParse->nErr ){.
2c299 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2c29a 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  }.  if( sqlite3A
2c29b 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2c29c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c 20   SQLITE_SELECT, 
2c29d 30 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75 72  0, 0, 0) ) retur
2c29e 6e 20 31 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73  n 1;.  memset(&s
2c29f 41 67 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  AggInfo, 0, size
2c2a0 6f 66 28 73 41 67 67 49 6e 66 6f 29 29 3b 0a 0a  of(sAggInfo));..
2c2a1 20 20 69 66 28 20 49 67 6e 6f 72 61 62 6c 65 4f    if( IgnorableO
2c2a2 72 64 65 72 62 79 28 70 44 65 73 74 29 20 29 7b  rderby(pDest) ){
2c2a3 0a 20 20 20 20 61 73 73 65 72 74 28 70 44 65 73  .    assert(pDes
2c2a4 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 78  t->eDest==SRT_Ex
2c2a5 69 73 74 73 20 7c 7c 20 70 44 65 73 74 2d 3e 65  ists || pDest->e
2c2a6 44 65 73 74 3d 3d 53 52 54 5f 55 6e 69 6f 6e 20  Dest==SRT_Union 
2c2a7 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 70  || .           p
2c2a8 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54  Dest->eDest==SRT
2c2a9 5f 45 78 63 65 70 74 20 7c 7c 20 70 44 65 73 74  _Except || pDest
2c2aa 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73  ->eDest==SRT_Dis
2c2ab 63 61 72 64 29 3b 0a 20 20 20 20 2f 2a 20 49 66  card);.    /* If
2c2ac 20 4f 52 44 45 52 20 42 59 20 6d 61 6b 65 73 20   ORDER BY makes 
2c2ad 6e 6f 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e  no difference in
2c2ae 20 74 68 65 20 6f 75 74 70 75 74 20 74 68 65 6e   the output then
2c2af 20 6e 65 69 74 68 65 72 20 64 6f 65 73 0a 20 20   neither does.  
2c2b0 20 20 2a 2a 20 44 49 53 54 49 4e 43 54 20 73 6f    ** DISTINCT so
2c2b1 20 69 74 20 63 61 6e 20 62 65 20 72 65 6d 6f 76   it can be remov
2c2b2 65 64 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20 73  ed too. */.    s
2c2b3 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2c2b4 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64  lete(db, p->pOrd
2c2b5 65 72 42 79 29 3b 0a 20 20 20 20 70 2d 3e 70 4f  erBy);.    p->pO
2c2b6 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20  rderBy = 0;.    
2c2b7 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20 7e  p->selFlags &= ~
2c2b8 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d  SF_Distinct;.  }
2c2b9 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
2c2ba 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c 20  Prep(pParse, p, 
2c2bb 30 29 3b 0a 20 20 70 4f 72 64 65 72 42 79 20 3d  0);.  pOrderBy =
2c2bc 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20   p->pOrderBy;.  
2c2bd 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53  pTabList = p->pS
2c2be 72 63 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70  rc;.  pEList = p
2c2bf 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20  ->pEList;.  if( 
2c2c0 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
2c2c1 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2c2c2 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c   ){.    goto sel
2c2c3 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 69  ect_end;.  }.  i
2c2c4 73 41 67 67 20 3d 20 28 70 2d 3e 73 65 6c 46 6c  sAgg = (p->selFl
2c2c5 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61  ags & SF_Aggrega
2c2c6 74 65 29 21 3d 30 3b 0a 20 20 61 73 73 65 72 74  te)!=0;.  assert
2c2c7 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 0a  ( pEList!=0 );..
2c2c8 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72    /* Begin gener
2c2c9 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f  ating code..  */
2c2ca 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
2c2cb 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
2c2cc 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f   if( v==0 ) goto
2c2cd 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20   select_end;..  
2c2ce 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2c2cf 20 66 6f 72 20 61 6c 6c 20 73 75 62 2d 71 75 65   for all sub-que
2c2d0 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  ries in the FROM
2c2d1 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 23 69 66   clause.  */.#if
2c2d2 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2c2d3 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20  _OMIT_SUBQUERY) 
2c2d4 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
2c2d5 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 20 20  TE_OMIT_VIEW).  
2c2d6 66 6f 72 28 69 3d 30 3b 20 21 70 2d 3e 70 50 72  for(i=0; !p->pPr
2c2d7 69 6f 72 20 26 26 20 69 3c 70 54 61 62 4c 69 73  ior && i<pTabLis
2c2d8 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  t->nSrc; i++){. 
2c2d9 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
2c2da 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20  t_item *pItem = 
2c2db 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 3b  &pTabList->a[i];
2c2dc 0a 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20  .    SelectDest 
2c2dd 64 65 73 74 3b 0a 20 20 20 20 53 65 6c 65 63 74  dest;.    Select
2c2de 20 2a 70 53 75 62 20 3d 20 70 49 74 65 6d 2d 3e   *pSub = pItem->
2c2df 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 69 6e 74  pSelect;.    int
2c2e0 20 69 73 41 67 67 53 75 62 3b 0a 0a 20 20 20 20   isAggSub;..    
2c2e1 69 66 28 20 70 53 75 62 3d 3d 30 20 7c 7c 20 70  if( pSub==0 || p
2c2e2 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65  Item->isPopulate
2c2e3 64 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20  d ) continue;.. 
2c2e4 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20     /* Increment 
2c2e5 50 61 72 73 65 2e 6e 48 65 69 67 68 74 20 62 79  Parse.nHeight by
2c2e6 20 74 68 65 20 68 65 69 67 68 74 20 6f 66 20 74   the height of t
2c2e7 68 65 20 6c 61 72 67 65 73 74 20 65 78 70 72 65  he largest expre
2c2e8 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 72 65  ssion.    ** tre
2c2e9 65 20 72 65 66 65 72 65 64 20 74 6f 20 62 79 20  e refered to by 
2c2ea 74 68 69 73 2c 20 74 68 65 20 70 61 72 65 6e 74  this, the parent
2c2eb 20 73 65 6c 65 63 74 2e 20 54 68 65 20 63 68 69   select. The chi
2c2ec 6c 64 20 73 65 6c 65 63 74 0a 20 20 20 20 2a 2a  ld select.    **
2c2ed 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 65 78 70   may contain exp
2c2ee 72 65 73 73 69 6f 6e 20 74 72 65 65 73 20 6f 66  ression trees of
2c2ef 20 61 74 20 6d 6f 73 74 0a 20 20 20 20 2a 2a 20   at most.    ** 
2c2f0 28 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52  (SQLITE_MAX_EXPR
2c2f1 5f 44 45 50 54 48 2d 50 61 72 73 65 2e 6e 48 65  _DEPTH-Parse.nHe
2c2f2 69 67 68 74 29 20 68 65 69 67 68 74 2e 20 54 68  ight) height. Th
2c2f3 69 73 20 69 73 20 61 20 62 69 74 0a 20 20 20 20  is is a bit.    
2c2f4 2a 2a 20 6d 6f 72 65 20 63 6f 6e 73 65 72 76 61  ** more conserva
2c2f5 74 69 76 65 20 74 68 61 6e 20 6e 65 63 65 73 73  tive than necess
2c2f6 61 72 79 2c 20 62 75 74 20 6d 75 63 68 20 65 61  ary, but much ea
2c2f7 73 69 65 72 20 74 68 61 6e 20 65 6e 66 6f 72 63  sier than enforc
2c2f8 69 6e 67 0a 20 20 20 20 2a 2a 20 61 6e 20 65 78  ing.    ** an ex
2c2f9 61 63 74 20 6c 69 6d 69 74 2e 0a 20 20 20 20 2a  act limit..    *
2c2fa 2f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48  /.    pParse->nH
2c2fb 65 69 67 68 74 20 2b 3d 20 73 71 6c 69 74 65 33  eight += sqlite3
2c2fc 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 74  SelectExprHeight
2c2fd 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65  (p);..    /* Che
2c2fe 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
2c2ff 20 73 75 62 71 75 65 72 79 20 63 61 6e 20 62 65   subquery can be
2c300 20 61 62 73 6f 72 62 65 64 20 69 6e 74 6f 20 74   absorbed into t
2c301 68 65 20 70 61 72 65 6e 74 2e 20 2a 2f 0a 20 20  he parent. */.  
2c302 20 20 69 73 41 67 67 53 75 62 20 3d 20 28 70 53    isAggSub = (pS
2c303 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  ub->selFlags & S
2c304 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d 30 3b  F_Aggregate)!=0;
2c305 0a 20 20 20 20 69 66 28 20 66 6c 61 74 74 65 6e  .    if( flatten
2c306 53 75 62 71 75 65 72 79 28 70 50 61 72 73 65 2c  Subquery(pParse,
2c307 20 70 2c 20 69 2c 20 69 73 41 67 67 2c 20 69 73   p, i, isAgg, is
2c308 41 67 67 53 75 62 29 20 29 7b 0a 20 20 20 20 20  AggSub) ){.     
2c309 20 69 66 28 20 69 73 41 67 67 53 75 62 20 29 7b   if( isAggSub ){
2c30a 0a 20 20 20 20 20 20 20 20 69 73 41 67 67 20 3d  .        isAgg =
2c30b 20 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 73   1;.        p->s
2c30c 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67  elFlags |= SF_Ag
2c30d 67 72 65 67 61 74 65 3b 0a 20 20 20 20 20 20 7d  gregate;.      }
2c30e 0a 20 20 20 20 20 20 69 20 3d 20 2d 31 3b 0a 20  .      i = -1;. 
2c30f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2c310 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2c311 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54  tInit(&dest, SRT
2c312 5f 45 70 68 65 6d 54 61 62 2c 20 70 49 74 65 6d  _EphemTab, pItem
2c313 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->iCursor);.    
2c314 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d    assert( pItem-
2c315 3e 69 73 50 6f 70 75 6c 61 74 65 64 3d 3d 30 20  >isPopulated==0 
2c316 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c317 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2c318 53 75 62 2c 20 26 64 65 73 74 29 3b 0a 20 20 20  Sub, &dest);.   
2c319 20 20 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75     pItem->isPopu
2c31a 6c 61 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  lated = 1;.    }
2c31b 0a 20 20 20 20 69 66 28 20 2f 2a 70 50 61 72 73  .    if( /*pPars
2c31c 65 2d 3e 6e 45 72 72 20 7c 7c 2a 2f 20 64 62 2d  e->nErr ||*/ db-
2c31d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2c31e 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65  .      goto sele
2c31f 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20  ct_end;.    }.  
2c320 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68    pParse->nHeigh
2c321 74 20 2d 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  t -= sqlite3Sele
2c322 63 74 45 78 70 72 48 65 69 67 68 74 28 70 29 3b  ctExprHeight(p);
2c323 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20  .    pTabList = 
2c324 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 69 66 28  p->pSrc;.    if(
2c325 20 21 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72   !IgnorableOrder
2c326 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20  by(pDest) ){.   
2c327 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d     pOrderBy = p-
2c328 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 7d  >pOrderBy;.    }
2c329 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74 20 3d 20  .  }.  pEList = 
2c32a 70 2d 3e 70 45 4c 69 73 74 3b 0a 23 65 6e 64 69  p->pEList;.#endi
2c32b 66 0a 20 20 70 57 68 65 72 65 20 3d 20 70 2d 3e  f.  pWhere = p->
2c32c 70 57 68 65 72 65 3b 0a 20 20 70 47 72 6f 75 70  pWhere;.  pGroup
2c32d 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79  By = p->pGroupBy
2c32e 3b 0a 20 20 70 48 61 76 69 6e 67 20 3d 20 70 2d  ;.  pHaving = p-
2c32f 3e 70 48 61 76 69 6e 67 3b 0a 20 20 69 73 44 69  >pHaving;.  isDi
2c330 73 74 69 6e 63 74 20 3d 20 28 70 2d 3e 73 65 6c  stinct = (p->sel
2c331 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69  Flags & SF_Disti
2c332 6e 63 74 29 21 3d 30 3b 0a 0a 23 69 66 6e 64 65  nct)!=0;..#ifnde
2c333 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  f SQLITE_OMIT_CO
2c334 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20  MPOUND_SELECT.  
2c335 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
2c336 72 65 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  re a sequence of
2c337 20 71 75 65 72 69 65 73 2c 20 64 6f 20 74 68 65   queries, do the
2c338 20 65 61 72 6c 69 65 72 20 6f 6e 65 73 20 66 69   earlier ones fi
2c339 72 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  rst..  */.  if( 
2c33a 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  p->pPrior ){.   
2c33b 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f   if( p->pRightmo
2c33c 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 53  st==0 ){.      S
2c33d 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 2c 20 2a 70  elect *pLoop, *p
2c33e 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 20  Right = 0;.     
2c33f 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
2c340 20 20 20 20 69 6e 74 20 6d 78 53 65 6c 65 63 74      int mxSelect
2c341 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 4c 6f 6f  ;.      for(pLoo
2c342 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f  p=p; pLoop; pLoo
2c343 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 2c  p=pLoop->pPrior,
2c344 20 63 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 20   cnt++){.       
2c345 20 70 4c 6f 6f 70 2d 3e 70 52 69 67 68 74 6d 6f   pLoop->pRightmo
2c346 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  st = p;.        
2c347 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 20 3d 20 70  pLoop->pNext = p
2c348 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 70  Right;.        p
2c349 52 69 67 68 74 20 3d 20 70 4c 6f 6f 70 3b 0a 20  Right = pLoop;. 
2c34a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 78 53       }.      mxS
2c34b 65 6c 65 63 74 20 3d 20 64 62 2d 3e 61 4c 69 6d  elect = db->aLim
2c34c 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
2c34d 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 5d  COMPOUND_SELECT]
2c34e 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 78 53 65  ;.      if( mxSe
2c34f 6c 65 63 74 20 26 26 20 63 6e 74 3e 6d 78 53 65  lect && cnt>mxSe
2c350 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  lect ){.        
2c351 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2c352 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e  pParse, "too man
2c353 79 20 74 65 72 6d 73 20 69 6e 20 63 6f 6d 70 6f  y terms in compo
2c354 75 6e 64 20 53 45 4c 45 43 54 22 29 3b 0a 20 20  und SELECT");.  
2c355 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2c356 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2c357 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65    return multiSe
2c358 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20  lect(pParse, p, 
2c359 70 44 65 73 74 29 3b 0a 20 20 7d 0a 23 65 6e 64  pDest);.  }.#end
2c35a 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 72 69 74  if..  /* If writ
2c35b 69 6e 67 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 72  ing to memory or
2c35c 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 73 65   generating a se
2c35d 74 0a 20 20 2a 2a 20 6f 6e 6c 79 20 61 20 73 69  t.  ** only a si
2c35e 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6d 61 79 20  ngle column may 
2c35f 62 65 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a  be output..  */.
2c360 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2c361 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 69  MIT_SUBQUERY.  i
2c362 66 28 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74 69  f( checkForMulti
2c363 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72 6f  ColumnSelectErro
2c364 72 28 70 50 61 72 73 65 2c 20 70 44 65 73 74 2c  r(pParse, pDest,
2c365 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20   pEList->nExpr) 
2c366 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65  ){.    goto sele
2c367 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64  ct_end;.  }.#end
2c368 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 6f 73 73  if..  /* If poss
2c369 69 62 6c 65 2c 20 72 65 77 72 69 74 65 20 74 68  ible, rewrite th
2c36a 65 20 71 75 65 72 79 20 74 6f 20 75 73 65 20 47  e query to use G
2c36b 52 4f 55 50 20 42 59 20 69 6e 73 74 65 61 64 20  ROUP BY instead 
2c36c 6f 66 20 44 49 53 54 49 4e 43 54 2e 0a 20 20 2a  of DISTINCT..  *
2c36d 2a 20 47 52 4f 55 50 20 42 59 20 6d 69 67 68 74  * GROUP BY might
2c36e 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2c 20 44   use an index, D
2c36f 49 53 54 49 4e 43 54 20 6e 65 76 65 72 20 64 6f  ISTINCT never do
2c370 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  es..  */.  asser
2c371 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d 3d  t( p->pGroupBy==
2c372 30 20 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67  0 || (p->selFlag
2c373 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65  s & SF_Aggregate
2c374 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  )!=0 );.  if( (p
2c375 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46  ->selFlags & (SF
2c376 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67  _Distinct|SF_Agg
2c377 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 73  regate))==SF_Dis
2c378 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 70 2d 3e  tinct ){.    p->
2c379 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74  pGroupBy = sqlit
2c37a 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
2c37b 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30 29 3b  , p->pEList, 0);
2c37c 0a 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20  .    pGroupBy = 
2c37d 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20  p->pGroupBy;.   
2c37e 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20   p->selFlags &= 
2c37f 7e 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20  ~SF_Distinct;.  
2c380 20 20 69 73 44 69 73 74 69 6e 63 74 20 3d 20 30    isDistinct = 0
2c381 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
2c382 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52  here is an ORDER
2c383 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e   BY clause, then
2c384 20 74 68 69 73 20 73 6f 72 74 69 6e 67 0a 20 20   this sorting.  
2c385 2a 2a 20 69 6e 64 65 78 20 6d 69 67 68 74 20 65  ** index might e
2c386 6e 64 20 75 70 20 62 65 69 6e 67 20 75 6e 75 73  nd up being unus
2c387 65 64 20 69 66 20 74 68 65 20 64 61 74 61 20 63  ed if the data c
2c388 61 6e 20 62 65 20 0a 20 20 2a 2a 20 65 78 74 72  an be .  ** extr
2c389 61 63 74 65 64 20 69 6e 20 70 72 65 2d 73 6f 72  acted in pre-sor
2c38a 74 65 64 20 6f 72 64 65 72 2e 20 20 49 66 20 74  ted order.  If t
2c38b 68 61 74 20 69 73 20 74 68 65 20 63 61 73 65 2c  hat is the case,
2c38c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 4f   then the.  ** O
2c38d 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
2c38e 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c  instruction will
2c38f 20 62 65 20 63 68 61 6e 67 65 64 20 74 6f 20 61   be changed to a
2c390 6e 20 4f 50 5f 4e 6f 6f 70 20 6f 6e 63 65 0a 20  n OP_Noop once. 
2c391 20 2a 2a 20 77 65 20 66 69 67 75 72 65 20 6f 75   ** we figure ou
2c392 74 20 74 68 61 74 20 74 68 65 20 73 6f 72 74 69  t that the sorti
2c393 6e 67 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20  ng index is not 
2c394 6e 65 65 64 65 64 2e 20 20 54 68 65 20 61 64 64  needed.  The add
2c395 72 53 6f 72 74 49 6e 64 65 78 0a 20 20 2a 2a 20  rSortIndex.  ** 
2c396 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64  variable is used
2c397 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74   to facilitate t
2c398 68 61 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2f  hat change..  */
2c399 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20  .  if( pOrderBy 
2c39a 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  ){.    KeyInfo *
2c39b 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 70 4b  pKeyInfo;.    pK
2c39c 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f  eyInfo = keyInfo
2c39d 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61  FromExprList(pPa
2c39e 72 73 65 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a  rse, pOrderBy);.
2c39f 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69 45      pOrderBy->iE
2c3a0 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d  Cursor = pParse-
2c3a1 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 2d 3e  >nTab++;.    p->
2c3a2 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20  addrOpenEphm[2] 
2c3a3 3d 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20  = addrSortIndex 
2c3a4 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  =.      sqlite3V
2c3a5 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2c3a6 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 0a 20  OpenEphemeral,. 
2c3a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3a8 20 20 20 20 20 20 20 20 20 20 70 4f 72 64 65 72            pOrder
2c3a9 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 70 4f  By->iECursor, pO
2c3aa 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 2b 32 2c  rderBy->nExpr+2,
2c3ab 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
2c3ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2c3ad 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20  char*)pKeyInfo, 
2c3ae 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
2c3af 46 46 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  FF);.  }else{.  
2c3b0 20 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20    addrSortIndex 
2c3b1 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  = -1;.  }..  /* 
2c3b2 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 69 73  If the output is
2c3b3 20 64 65 73 74 69 6e 65 64 20 66 6f 72 20 61 20   destined for a 
2c3b4 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c  temporary table,
2c3b5 20 6f 70 65 6e 20 74 68 61 74 20 74 61 62 6c 65   open that table
2c3b6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65  ..  */.  if( pDe
2c3b7 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45  st->eDest==SRT_E
2c3b8 70 68 65 6d 54 61 62 20 29 7b 0a 20 20 20 20 73  phemTab ){.    s
2c3b9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c3ba 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  (v, OP_OpenEphem
2c3bb 65 72 61 6c 2c 20 70 44 65 73 74 2d 3e 69 50 61  eral, pDest->iPa
2c3bc 72 6d 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  rm, pEList->nExp
2c3bd 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  r);.  }..  /* Se
2c3be 74 20 74 68 65 20 6c 69 6d 69 74 65 72 2e 0a 20  t the limiter.. 
2c3bf 20 2a 2f 0a 20 20 69 45 6e 64 20 3d 20 73 71 6c   */.  iEnd = sql
2c3c0 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2c3c1 6c 28 76 29 3b 0a 20 20 63 6f 6d 70 75 74 65 4c  l(v);.  computeL
2c3c2 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50  imitRegisters(pP
2c3c3 61 72 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b 0a  arse, p, iEnd);.
2c3c4 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 20 76 69 72  .  /* Open a vir
2c3c5 74 75 61 6c 20 69 6e 64 65 78 20 74 6f 20 75 73  tual index to us
2c3c6 65 20 66 6f 72 20 74 68 65 20 64 69 73 74 69 6e  e for the distin
2c3c7 63 74 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 69  ct set..  */.  i
2c3c8 66 28 20 69 73 44 69 73 74 69 6e 63 74 20 29 7b  f( isDistinct ){
2c3c9 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
2c3ca 65 79 49 6e 66 6f 3b 0a 20 20 20 20 61 73 73 65  eyInfo;.    asse
2c3cb 72 74 28 20 69 73 41 67 67 20 7c 7c 20 70 47 72  rt( isAgg || pGr
2c3cc 6f 75 70 42 79 20 29 3b 0a 20 20 20 20 64 69 73  oupBy );.    dis
2c3cd 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e  tinct = pParse->
2c3ce 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 4b 65 79  nTab++;.    pKey
2c3cf 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72  Info = keyInfoFr
2c3d0 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
2c3d1 65 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  e, p->pEList);. 
2c3d2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c3d3 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp4(v, OP_OpenE
2c3d4 70 68 65 6d 65 72 61 6c 2c 20 64 69 73 74 69 6e  phemeral, distin
2c3d5 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20  ct, 0, 0,.      
2c3d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3d7 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66    (char*)pKeyInf
2c3d8 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  o, P4_KEYINFO_HA
2c3d9 4e 44 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65 7b  NDOFF);.  }else{
2c3da 0a 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d 20  .    distinct = 
2c3db 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 67  -1;.  }..  /* Ag
2c3dc 67 72 65 67 61 74 65 20 61 6e 64 20 6e 6f 6e 2d  gregate and non-
2c3dd 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65  aggregate querie
2c3de 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20 64 69  s are handled di
2c3df 66 66 65 72 65 6e 74 6c 79 20 2a 2f 0a 20 20 69  fferently */.  i
2c3e0 66 28 20 21 69 73 41 67 67 20 26 26 20 70 47 72  f( !isAgg && pGr
2c3e1 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20  oupBy==0 ){.    
2c3e2 2f 2a 20 54 68 69 73 20 63 61 73 65 20 69 73 20  /* This case is 
2c3e3 66 6f 72 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74  for non-aggregat
2c3e4 65 20 71 75 65 72 69 65 73 0a 20 20 20 20 2a 2a  e queries.    **
2c3e5 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62   Begin the datab
2c3e6 61 73 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f 0a  ase scan.    */.
2c3e7 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c      pWInfo = sql
2c3e8 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70  ite3WhereBegin(p
2c3e9 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
2c3ea 20 70 57 68 65 72 65 2c 20 26 70 4f 72 64 65 72   pWhere, &pOrder
2c3eb 42 79 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  By, 0);.    if( 
2c3ec 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f  pWInfo==0 ) goto
2c3ed 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20   select_end;..  
2c3ee 20 20 2f 2a 20 49 66 20 73 6f 72 74 69 6e 67 20    /* If sorting 
2c3ef 69 6e 64 65 78 20 74 68 61 74 20 77 61 73 20 63  index that was c
2c3f0 72 65 61 74 65 64 20 62 79 20 61 20 70 72 69 6f  reated by a prio
2c3f1 72 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  r OP_OpenEphemer
2c3f2 61 6c 20 0a 20 20 20 20 2a 2a 20 69 6e 73 74 72  al .    ** instr
2c3f3 75 63 74 69 6f 6e 20 65 6e 64 65 64 20 75 70 20  uction ended up 
2c3f4 6e 6f 74 20 62 65 69 6e 67 20 6e 65 65 64 65 64  not being needed
2c3f5 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20 74 68  , then change th
2c3f6 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  e OP_OpenEphemer
2c3f7 61 6c 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 61  al.    ** into a
2c3f8 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a 20 20 20 20 2a  n OP_Noop..    *
2c3f9 2f 0a 20 20 20 20 69 66 28 20 61 64 64 72 53 6f  /.    if( addrSo
2c3fa 72 74 49 6e 64 65 78 3e 3d 30 20 26 26 20 70 4f  rtIndex>=0 && pO
2c3fb 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20  rderBy==0 ){.   
2c3fc 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2c3fd 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64  angeToNoop(v, ad
2c3fe 64 72 53 6f 72 74 49 6e 64 65 78 2c 20 31 29 3b  drSortIndex, 1);
2c3ff 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70  .      p->addrOp
2c400 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a  enEphm[2] = -1;.
2c401 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 55 73      }..    /* Us
2c402 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 69  e the standard i
2c403 6e 6e 65 72 20 6c 6f 6f 70 0a 20 20 20 20 2a 2f  nner loop.    */
2c404 0a 20 20 20 20 61 73 73 65 72 74 28 21 69 73 44  .    assert(!isD
2c405 69 73 74 69 6e 63 74 29 3b 0a 20 20 20 20 73 65  istinct);.    se
2c406 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50  lectInnerLoop(pP
2c407 61 72 73 65 2c 20 70 2c 20 70 45 4c 69 73 74 2c  arse, p, pEList,
2c408 20 30 2c 20 30 2c 20 70 4f 72 64 65 72 42 79 2c   0, 0, pOrderBy,
2c409 20 2d 31 2c 20 70 44 65 73 74 2c 0a 20 20 20 20   -1, pDest,.    
2c40a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c40b 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75  pWInfo->iContinu
2c40c 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61  e, pWInfo->iBrea
2c40d 6b 29 3b 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20  k);..    /* End 
2c40e 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61  the database sca
2c40f 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20  n loop..    */. 
2c410 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45     sqlite3WhereE
2c411 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 65  nd(pWInfo);.  }e
2c412 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  lse{.    /* This
2c413 20 69 73 20 74 68 65 20 70 72 6f 63 65 73 73 69   is the processi
2c414 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65  ng for aggregate
2c415 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 20 20   queries */.    
2c416 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
2c417 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74      /* Name cont
2c418 65 78 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69  ext for processi
2c419 6e 67 20 61 67 67 72 65 67 61 74 65 20 69 6e 66  ng aggregate inf
2c41a 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  ormation */.    
2c41b 69 6e 74 20 69 41 4d 65 6d 3b 20 20 20 20 20 20  int iAMem;      
2c41c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d      /* First Mem
2c41d 20 61 64 64 72 65 73 73 20 66 6f 72 20 73 74 6f   address for sto
2c41e 72 69 6e 67 20 63 75 72 72 65 6e 74 20 47 52 4f  ring current GRO
2c41f 55 50 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74  UP BY */.    int
2c420 20 69 42 4d 65 6d 3b 20 20 20 20 20 20 20 20 20   iBMem;         
2c421 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d 20 61 64   /* First Mem ad
2c422 64 72 65 73 73 20 66 6f 72 20 70 72 65 76 69 6f  dress for previo
2c423 75 73 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20  us GROUP BY */. 
2c424 20 20 20 69 6e 74 20 69 55 73 65 46 6c 61 67 3b     int iUseFlag;
2c425 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61 64         /* Mem ad
2c426 64 72 65 73 73 20 68 6f 6c 64 69 6e 67 20 66 6c  dress holding fl
2c427 61 67 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  ag indicating th
2c428 61 74 20 61 74 20 6c 65 61 73 74 0a 20 20 20 20  at at least.    
2c429 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c42a 20 20 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20 6f      ** one row o
2c42b 66 20 74 68 65 20 69 6e 70 75 74 20 74 6f 20 74  f the input to t
2c42c 68 65 20 61 67 67 72 65 67 61 74 6f 72 20 68 61  he aggregator ha
2c42d 73 20 62 65 65 6e 0a 20 20 20 20 20 20 20 20 20  s been.         
2c42e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
2c42f 2a 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20  * processed */. 
2c430 20 20 20 69 6e 74 20 69 41 62 6f 72 74 46 6c 61     int iAbortFla
2c431 67 3b 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61 64  g;     /* Mem ad
2c432 64 72 65 73 73 20 77 68 69 63 68 20 63 61 75 73  dress which caus
2c433 65 73 20 71 75 65 72 79 20 61 62 6f 72 74 20 69  es query abort i
2c434 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20  f positive */.  
2c435 20 20 69 6e 74 20 67 72 6f 75 70 42 79 53 6f 72    int groupBySor
2c436 74 3b 20 20 20 20 2f 2a 20 52 6f 77 73 20 63 6f  t;    /* Rows co
2c437 6d 65 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 69  me from source i
2c438 6e 20 47 52 4f 55 50 20 42 59 20 6f 72 64 65 72  n GROUP BY order
2c439 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72   */.    int addr
2c43a 45 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 45  End;        /* E
2c43b 6e 64 20 6f 66 20 70 72 6f 63 65 73 73 69 6e 67  nd of processing
2c43c 20 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54   for this SELECT
2c43d 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f   */..    /* Remo
2c43e 76 65 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20 61  ve any and all a
2c43f 6c 69 61 73 65 73 20 62 65 74 77 65 65 6e 20 74  liases between t
2c440 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e  he result set an
2c441 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 47 52 4f  d the.    ** GRO
2c442 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 20  UP BY clause..  
2c443 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72    */.    if( pGr
2c444 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 69  oupBy ){.      i
2c445 6e 74 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20  nt k;           
2c446 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c447 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
2c448 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70        struct Exp
2c449 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
2c44a 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69  m;  /* For loopi
2c44b 6e 67 20 6f 76 65 72 20 65 78 70 72 65 73 73 69  ng over expressi
2c44c 6f 6e 20 69 6e 20 61 20 6c 69 73 74 20 2a 2f 0a  on in a list */.
2c44d 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70 2d 3e  .      for(k=p->
2c44e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70  pEList->nExpr, p
2c44f 49 74 65 6d 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e  Item=p->pEList->
2c450 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49 74  a; k>0; k--, pIt
2c451 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70  em++){.        p
2c452 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30  Item->iAlias = 0
2c453 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c454 66 6f 72 28 6b 3d 70 47 72 6f 75 70 42 79 2d 3e  for(k=pGroupBy->
2c455 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 47 72  nExpr, pItem=pGr
2c456 6f 75 70 42 79 2d 3e 61 3b 20 6b 3e 30 3b 20 6b  oupBy->a; k>0; k
2c457 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
2c458 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c        pItem->iAl
2c459 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ias = 0;.      }
2c45a 0a 20 20 20 20 7d 0a 0a 20 0a 20 20 20 20 2f 2a  .    }.. .    /*
2c45b 20 43 72 65 61 74 65 20 61 20 6c 61 62 65 6c 20   Create a label 
2c45c 74 6f 20 6a 75 6d 70 20 74 6f 20 77 68 65 6e 20  to jump to when 
2c45d 77 65 20 77 61 6e 74 20 74 6f 20 61 62 6f 72 74  we want to abort
2c45e 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20   the query */.  
2c45f 20 20 61 64 64 72 45 6e 64 20 3d 20 73 71 6c 69    addrEnd = sqli
2c460 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2c461 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e  (v);..    /* Con
2c462 76 65 72 74 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6e  vert TK_COLUMN n
2c463 6f 64 65 73 20 69 6e 74 6f 20 54 4b 5f 41 47 47  odes into TK_AGG
2c464 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 6d 61 6b 65  _COLUMN and make
2c465 20 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 20 20   entries in.    
2c466 2a 2a 20 73 41 67 67 49 6e 66 6f 20 66 6f 72 20  ** sAggInfo for 
2c467 61 6c 6c 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  all TK_AGG_FUNCT
2c468 49 4f 4e 20 6e 6f 64 65 73 20 69 6e 20 65 78 70  ION nodes in exp
2c469 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a  ressions of the.
2c46a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74      ** SELECT st
2c46b 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a  atement..    */.
2c46c 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c      memset(&sNC,
2c46d 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
2c46e 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65  ;.    sNC.pParse
2c46f 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73   = pParse;.    s
2c470 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54  NC.pSrcList = pT
2c471 61 62 4c 69 73 74 3b 0a 20 20 20 20 73 4e 43 2e  abList;.    sNC.
2c472 70 41 67 67 49 6e 66 6f 20 3d 20 26 73 41 67 67  pAggInfo = &sAgg
2c473 49 6e 66 6f 3b 0a 20 20 20 20 73 41 67 67 49 6e  Info;.    sAggIn
2c474 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  fo.nSortingColum
2c475 6e 20 3d 20 70 47 72 6f 75 70 42 79 20 3f 20 70  n = pGroupBy ? p
2c476 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2b 31  GroupBy->nExpr+1
2c477 20 3a 20 30 3b 0a 20 20 20 20 73 41 67 67 49 6e   : 0;.    sAggIn
2c478 66 6f 2e 70 47 72 6f 75 70 42 79 20 3d 20 70 47  fo.pGroupBy = pG
2c479 72 6f 75 70 42 79 3b 0a 20 20 20 20 73 71 6c 69  roupBy;.    sqli
2c47a 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
2c47b 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 45 4c 69  gList(&sNC, pELi
2c47c 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  st);.    sqlite3
2c47d 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69  ExprAnalyzeAggLi
2c47e 73 74 28 26 73 4e 43 2c 20 70 4f 72 64 65 72 42  st(&sNC, pOrderB
2c47f 79 29 3b 0a 20 20 20 20 69 66 28 20 70 48 61 76  y);.    if( pHav
2c480 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ing ){.      sql
2c481 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
2c482 67 67 72 65 67 61 74 65 73 28 26 73 4e 43 2c 20  ggregates(&sNC, 
2c483 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 7d 0a  pHaving);.    }.
2c484 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 41 63      sAggInfo.nAc
2c485 63 75 6d 75 6c 61 74 6f 72 20 3d 20 73 41 67 67  cumulator = sAgg
2c486 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20  Info.nColumn;.  
2c487 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67    for(i=0; i<sAg
2c488 67 49 6e 66 6f 2e 6e 46 75 6e 63 3b 20 69 2b 2b  gInfo.nFunc; i++
2c489 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
2c48a 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
2c48b 79 28 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63  y(sAggInfo.aFunc
2c48c 5b 69 5d 2e 70 45 78 70 72 2c 20 45 50 5f 78 49  [i].pExpr, EP_xI
2c48d 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20  sSelect) );.    
2c48e 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61    sqlite3ExprAna
2c48f 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43  lyzeAggList(&sNC
2c490 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63  , sAggInfo.aFunc
2c491 5b 69 5d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69  [i].pExpr->x.pLi
2c492 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  st);.    }.    i
2c493 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2c494 6c 65 64 20 29 20 67 6f 74 6f 20 73 65 6c 65 63  led ) goto selec
2c495 74 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20 50  t_end;..    /* P
2c496 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61 67  rocessing for ag
2c497 67 72 65 67 61 74 65 73 20 77 69 74 68 20 47 52  gregates with GR
2c498 4f 55 50 20 42 59 20 69 73 20 76 65 72 79 20 64  OUP BY is very d
2c499 69 66 66 65 72 65 6e 74 20 61 6e 64 0a 20 20 20  ifferent and.   
2c49a 20 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f   ** much more co
2c49b 6d 70 6c 65 78 20 74 68 61 6e 20 61 67 67 72 65  mplex than aggre
2c49c 67 61 74 65 73 20 77 69 74 68 6f 75 74 20 61 20  gates without a 
2c49d 47 52 4f 55 50 20 42 59 2e 0a 20 20 20 20 2a 2f  GROUP BY..    */
2c49e 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42  .    if( pGroupB
2c49f 79 20 29 7b 0a 20 20 20 20 20 20 4b 65 79 49 6e  y ){.      KeyIn
2c4a0 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 2f  fo *pKeyInfo;  /
2c4a1 2a 20 4b 65 79 69 6e 67 20 69 6e 66 6f 72 6d 61  * Keying informa
2c4a2 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 67 72 6f  tion for the gro
2c4a3 75 70 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a  up by clause */.
2c4a4 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 20 20 20        int j1;   
2c4a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 2d 76            /* A-v
2c4a6 73 2d 42 20 63 6f 6d 70 61 72 69 73 69 6f 6e 20  s-B comparision 
2c4a7 6a 75 6d 70 20 2a 2f 0a 20 20 20 20 20 20 69 6e  jump */.      in
2c4a8 74 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 3b  t addrOutputRow;
2c4a9 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 73 75    /* Start of su
2c4aa 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6f 75  broutine that ou
2c4ab 74 70 75 74 73 20 61 20 72 65 73 75 6c 74 20 72  tputs a result r
2c4ac 6f 77 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ow */.      int 
2c4ad 72 65 67 4f 75 74 70 75 74 52 6f 77 3b 20 20 20  regOutputRow;   
2c4ae 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 72 65 73  /* Return addres
2c4af 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 6f  s register for o
2c4b0 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65  utput subroutine
2c4b1 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
2c4b2 64 72 53 65 74 41 62 6f 72 74 3b 20 20 20 2f 2a  drSetAbort;   /*
2c4b3 20 53 65 74 20 74 68 65 20 61 62 6f 72 74 20 66   Set the abort f
2c4b4 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e 20 2a  lag and return *
2c4b5 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  /.      int addr
2c4b6 54 6f 70 4f 66 4c 6f 6f 70 3b 20 20 2f 2a 20 54  TopOfLoop;  /* T
2c4b7 6f 70 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  op of the input 
2c4b8 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 69 6e  loop */.      in
2c4b9 74 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64 78  t addrSortingIdx
2c4ba 3b 20 2f 2a 20 54 68 65 20 4f 50 5f 4f 70 65 6e  ; /* The OP_Open
2c4bb 45 70 68 65 6d 65 72 61 6c 20 66 6f 72 20 74 68  Ephemeral for th
2c4bc 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  e sorting index 
2c4bd 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
2c4be 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  rReset;      /* 
2c4bf 53 75 62 72 6f 75 74 69 6e 65 20 66 6f 72 20 72  Subroutine for r
2c4c0 65 73 65 74 74 69 6e 67 20 74 68 65 20 61 63 63  esetting the acc
2c4c1 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20  umulator */.    
2c4c2 20 20 69 6e 74 20 72 65 67 52 65 73 65 74 3b 20    int regReset; 
2c4c3 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
2c4c4 61 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72  address register
2c4c5 20 66 6f 72 20 72 65 73 65 74 20 73 75 62 72 6f   for reset subro
2c4c6 75 74 69 6e 65 20 2a 2f 0a 0a 20 20 20 20 20 20  utine */..      
2c4c7 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
2c4c8 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
2c4c9 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64 20 61   we might need a
2c4ca 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74   sorting index t
2c4cb 6f 0a 20 20 20 20 20 20 2a 2a 20 69 6d 70 6c 65  o.      ** imple
2c4cc 6d 65 6e 74 20 69 74 2e 20 20 41 6c 6c 6f 63 61  ment it.  Alloca
2c4cd 74 65 20 74 68 61 74 20 73 6f 72 74 69 6e 67 20  te that sorting 
2c4ce 69 6e 64 65 78 20 6e 6f 77 2e 20 20 49 66 20 69  index now.  If i
2c4cf 74 20 74 75 72 6e 73 20 6f 75 74 0a 20 20 20 20  t turns out.    
2c4d0 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f 20    ** that we do 
2c4d1 6e 6f 74 20 6e 65 65 64 20 69 74 20 61 66 74 65  not need it afte
2c4d2 72 20 61 6c 6c 2c 20 74 68 65 20 4f 70 65 6e 45  r all, the OpenE
2c4d3 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63  phemeral instruc
2c4d4 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 77 69  tion.      ** wi
2c4d5 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20  ll be converted 
2c4d6 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e 20 20 0a 20  into a Noop.  . 
2c4d7 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 41       */.      sA
2c4d8 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64  ggInfo.sortingId
2c4d9 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  x = pParse->nTab
2c4da 2b 2b 3b 0a 20 20 20 20 20 20 70 4b 65 79 49 6e  ++;.      pKeyIn
2c4db 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d  fo = keyInfoFrom
2c4dc 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
2c4dd 20 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20   pGroupBy);.    
2c4de 20 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64 78    addrSortingIdx
2c4df 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2c4e0 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp4(v, OP_OpenE
2c4e1 70 68 65 6d 65 72 61 6c 2c 20 0a 20 20 20 20 20  phemeral, .     
2c4e2 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f       sAggInfo.so
2c4e3 72 74 69 6e 67 49 64 78 2c 20 73 41 67 67 49 6e  rtingIdx, sAggIn
2c4e4 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  fo.nSortingColum
2c4e5 6e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 30 2c  n, .          0,
2c4e6 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
2c4e7 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
2c4e8 44 4f 46 46 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  DOFF);..      /*
2c4e9 20 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d 6f   Initialize memo
2c4ea 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 75 73 65  ry locations use
2c4eb 64 20 62 79 20 47 52 4f 55 50 20 42 59 20 61 67  d by GROUP BY ag
2c4ec 67 72 65 67 61 74 65 20 70 72 6f 63 65 73 73 69  gregate processi
2c4ed 6e 67 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ng.      */.    
2c4ee 20 20 69 55 73 65 46 6c 61 67 20 3d 20 2b 2b 70    iUseFlag = ++p
2c4ef 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2c4f0 20 20 20 69 41 62 6f 72 74 46 6c 61 67 20 3d 20     iAbortFlag = 
2c4f1 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2c4f2 20 20 20 20 20 20 72 65 67 4f 75 74 70 75 74 52        regOutputR
2c4f3 6f 77 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  ow = ++pParse->n
2c4f4 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64 72 4f  Mem;.      addrO
2c4f5 75 74 70 75 74 52 6f 77 20 3d 20 73 71 6c 69 74  utputRow = sqlit
2c4f6 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2c4f7 76 29 3b 0a 20 20 20 20 20 20 72 65 67 52 65 73  v);.      regRes
2c4f8 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  et = ++pParse->n
2c4f9 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64 72 52  Mem;.      addrR
2c4fa 65 73 65 74 20 3d 20 73 71 6c 69 74 65 33 56 64  eset = sqlite3Vd
2c4fb 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2c4fc 20 20 20 20 20 20 69 41 4d 65 6d 20 3d 20 70 50        iAMem = pP
2c4fd 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a  arse->nMem + 1;.
2c4fe 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
2c4ff 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d 3e  em += pGroupBy->
2c500 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 69 42 4d  nExpr;.      iBM
2c501 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  em = pParse->nMe
2c502 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 50 61  m + 1;.      pPa
2c503 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72  rse->nMem += pGr
2c504 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20  oupBy->nExpr;.  
2c505 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c506 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2c507 67 65 72 2c 20 30 2c 20 69 41 62 6f 72 74 46 6c  ger, 0, iAbortFl
2c508 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  ag);.      VdbeC
2c509 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 6c 65 61  omment((v, "clea
2c50a 72 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29 3b  r abort flag"));
2c50b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c50c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2c50d 6e 74 65 67 65 72 2c 20 30 2c 20 69 55 73 65 46  nteger, 0, iUseF
2c50e 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65  lag);.      Vdbe
2c50f 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64  Comment((v, "ind
2c510 69 63 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f  icate accumulato
2c511 72 20 65 6d 70 74 79 22 29 29 3b 0a 0a 20 20 20  r empty"));..   
2c512 20 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 6c 6f     /* Begin a lo
2c513 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74  op that will ext
2c514 72 61 63 74 20 61 6c 6c 20 73 6f 75 72 63 65 20  ract all source 
2c515 72 6f 77 73 20 69 6e 20 47 52 4f 55 50 20 42 59  rows in GROUP BY
2c516 20 6f 72 64 65 72 2e 0a 20 20 20 20 20 20 2a 2a   order..      **
2c517 20 54 68 69 73 20 6d 69 67 68 74 20 69 6e 76 6f   This might invo
2c518 6c 76 65 20 74 77 6f 20 73 65 70 61 72 61 74 65  lve two separate
2c519 20 6c 6f 6f 70 73 20 77 69 74 68 20 61 6e 20 4f   loops with an O
2c51a 50 5f 53 6f 72 74 20 69 6e 20 62 65 74 77 65 65  P_Sort in betwee
2c51b 6e 2c 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69  n, or.      ** i
2c51c 74 20 6d 69 67 68 74 20 62 65 20 61 20 73 69 6e  t might be a sin
2c51d 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74 20 75 73  gle loop that us
2c51e 65 73 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 65  es an index to e
2c51f 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69  xtract informati
2c520 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74  on.      ** in t
2c521 68 65 20 72 69 67 68 74 20 6f 72 64 65 72 20 74  he right order t
2c522 6f 20 62 65 67 69 6e 20 77 69 74 68 2e 0a 20 20  o begin with..  
2c523 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
2c524 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c525 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 52  , OP_Gosub, regR
2c526 65 73 65 74 2c 20 61 64 64 72 52 65 73 65 74 29  eset, addrReset)
2c527 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d  ;.      pWInfo =
2c528 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
2c529 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
2c52a 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70 47  ist, pWhere, &pG
2c52b 72 6f 75 70 42 79 2c 20 30 29 3b 0a 20 20 20 20  roupBy, 0);.    
2c52c 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20    if( pWInfo==0 
2c52d 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e  ) goto select_en
2c52e 64 3b 0a 20 20 20 20 20 20 69 66 28 20 70 47 72  d;.      if( pGr
2c52f 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20  oupBy==0 ){.    
2c530 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 74 69 6d      /* The optim
2c531 69 7a 65 72 20 69 73 20 61 62 6c 65 20 74 6f 20  izer is able to 
2c532 64 65 6c 69 76 65 72 20 72 6f 77 73 20 69 6e 20  deliver rows in 
2c533 67 72 6f 75 70 20 62 79 20 6f 72 64 65 72 20 73  group by order s
2c534 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 20  o.        ** we 
2c535 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 73  do not have to s
2c536 6f 72 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70 65  ort.  The OP_Ope
2c537 6e 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65  nEphemeral table
2c538 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20   will be.       
2c539 20 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c 61   ** cancelled la
2c53a 74 65 72 20 62 65 63 61 75 73 65 20 77 65 20 73  ter because we s
2c53b 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73 65  till need to use
2c53c 20 74 68 65 20 70 4b 65 79 49 6e 66 6f 0a 20 20   the pKeyInfo.  
2c53d 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2c53e 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70   pGroupBy = p->p
2c53f 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20 20  GroupBy;.       
2c540 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 30   groupBySort = 0
2c541 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2c542 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 73 20 61         /* Rows a
2c543 72 65 20 63 6f 6d 69 6e 67 20 6f 75 74 20 69 6e  re coming out in
2c544 20 75 6e 64 65 74 65 72 6d 69 6e 65 64 20 6f 72   undetermined or
2c545 64 65 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f  der.  We have to
2c546 20 70 75 73 68 0a 20 20 20 20 20 20 20 20 2a 2a   push.        **
2c547 20 65 61 63 68 20 72 6f 77 20 69 6e 74 6f 20 61   each row into a
2c548 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 20   sorting index, 
2c549 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 66 69  terminate the fi
2c54a 72 73 74 20 6c 6f 6f 70 2c 0a 20 20 20 20 20 20  rst loop,.      
2c54b 20 20 2a 2a 20 74 68 65 6e 20 6c 6f 6f 70 20 6f    ** then loop o
2c54c 76 65 72 20 74 68 65 20 73 6f 72 74 69 6e 67 20  ver the sorting 
2c54d 69 6e 64 65 78 20 69 6e 20 6f 72 64 65 72 20 74  index in order t
2c54e 6f 20 67 65 74 20 74 68 65 20 6f 75 74 70 75 74  o get the output
2c54f 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 73  .        ** in s
2c550 6f 72 74 65 64 20 6f 72 64 65 72 0a 20 20 20 20  orted order.    
2c551 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
2c552 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20 20 20  nt regBase;.    
2c553 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72      int regRecor
2c554 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  d;.        int n
2c555 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  Col;.        int
2c556 20 6e 47 72 6f 75 70 42 79 3b 0a 0a 20 20 20 20   nGroupBy;..    
2c557 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20      groupBySort 
2c558 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 6e 47 72  = 1;.        nGr
2c559 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79  oupBy = pGroupBy
2c55a 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ->nExpr;.       
2c55b 20 6e 43 6f 6c 20 3d 20 6e 47 72 6f 75 70 42 79   nCol = nGroupBy
2c55c 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 6a 20   + 1;.        j 
2c55d 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20 20  = nGroupBy+1;.  
2c55e 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2c55f 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d  <sAggInfo.nColum
2c560 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
2c561 20 20 20 69 66 28 20 73 41 67 67 49 6e 66 6f 2e     if( sAggInfo.
2c562 61 43 6f 6c 5b 69 5d 2e 69 53 6f 72 74 65 72 43  aCol[i].iSorterC
2c563 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20  olumn>=j ){.    
2c564 20 20 20 20 20 20 20 20 6e 43 6f 6c 2b 2b 3b 0a          nCol++;.
2c565 20 20 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b              j++;
2c566 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2c567 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
2c568 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33  egBase = sqlite3
2c569 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
2c56a 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20  rse, nCol);.    
2c56b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c56c 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65  acheClear(pParse
2c56d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2c56e 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
2c56f 73 74 28 70 50 61 72 73 65 2c 20 70 47 72 6f 75  st(pParse, pGrou
2c570 70 42 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29  pBy, regBase, 0)
2c571 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c572 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c573 50 5f 53 65 71 75 65 6e 63 65 2c 20 73 41 67 67  P_Sequence, sAgg
2c574 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c  Info.sortingIdx,
2c575 72 65 67 42 61 73 65 2b 6e 47 72 6f 75 70 42 79  regBase+nGroupBy
2c576 29 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 6e  );.        j = n
2c577 47 72 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20 20  GroupBy+1;.     
2c578 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41     for(i=0; i<sA
2c579 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20  ggInfo.nColumn; 
2c57a 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
2c57b 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63  struct AggInfo_c
2c57c 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 73 41 67 67  ol *pCol = &sAgg
2c57d 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 3b 0a 20 20  Info.aCol[i];.  
2c57e 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c          if( pCol
2c57f 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3e  ->iSorterColumn>
2c580 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =j ){.          
2c581 20 20 69 6e 74 20 72 31 20 3d 20 6a 20 2b 20 72    int r1 = j + r
2c582 65 67 42 61 73 65 3b 0a 20 20 20 20 20 20 20 20  egBase;.        
2c583 20 20 20 20 69 6e 74 20 72 32 3b 0a 0a 20 20 20      int r2;..   
2c584 20 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71           r2 = sq
2c585 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
2c586 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 0a  Column(pParse, .
2c587 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c588 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2c589 43 6f 6c 2d 3e 70 54 61 62 2c 20 70 43 6f 6c 2d  Col->pTab, pCol-
2c58a 3e 69 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 2d 3e  >iColumn, pCol->
2c58b 69 54 61 62 6c 65 2c 20 72 31 2c 20 30 29 3b 0a  iTable, r1, 0);.
2c58c 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
2c58d 72 31 21 3d 72 32 20 29 7b 0a 20 20 20 20 20 20  r1!=r2 ){.      
2c58e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c58f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c590 53 43 6f 70 79 2c 20 72 32 2c 20 72 31 29 3b 0a  SCopy, r2, r1);.
2c591 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2c592 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20            j++;. 
2c593 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2c594 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65 67     }.        reg
2c595 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33  Record = sqlite3
2c596 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2c597 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
2c598 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2c599 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
2c59a 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2c 20 72  regBase, nCol, r
2c59b 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 20 20 20  egRecord);.     
2c59c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c59d 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp2(v, OP_IdxIn
2c59e 73 65 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73  sert, sAggInfo.s
2c59f 6f 72 74 69 6e 67 49 64 78 2c 20 72 65 67 52 65  ortingIdx, regRe
2c5a0 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73  cord);.        s
2c5a1 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2c5a2 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
2c5a3 52 65 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20  Record);.       
2c5a4 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2c5a5 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2c5a6 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 29 3b   regBase, nCol);
2c5a7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c5a8 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29  WhereEnd(pWInfo)
2c5a9 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c5aa 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c5ab 50 5f 53 6f 72 74 2c 20 73 41 67 67 49 6e 66 6f  P_Sort, sAggInfo
2c5ac 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64  .sortingIdx, add
2c5ad 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 56  rEnd);.        V
2c5ae 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2c5af 47 52 4f 55 50 20 42 59 20 73 6f 72 74 22 29 29  GROUP BY sort"))
2c5b0 3b 0a 20 20 20 20 20 20 20 20 73 41 67 67 49 6e  ;.        sAggIn
2c5b1 66 6f 2e 75 73 65 53 6f 72 74 69 6e 67 49 64 78  fo.useSortingIdx
2c5b2 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71   = 1;.        sq
2c5b3 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
2c5b4 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ear(pParse);.   
2c5b5 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 45     }..      /* E
2c5b6 76 61 6c 75 61 74 65 20 74 68 65 20 63 75 72 72  valuate the curr
2c5b7 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74 65 72  ent GROUP BY ter
2c5b8 6d 73 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20  ms and store in 
2c5b9 62 30 2c 20 62 31 2c 20 62 32 2e 2e 2e 0a 20 20  b0, b1, b2....  
2c5ba 20 20 20 20 2a 2a 20 28 62 30 20 69 73 20 6d 65      ** (b0 is me
2c5bb 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 69 42  mory location iB
2c5bc 4d 65 6d 2b 30 2c 20 62 31 20 69 73 20 69 42 4d  Mem+0, b1 is iBM
2c5bd 65 6d 2b 31 2c 20 61 6e 64 20 73 6f 20 66 6f 72  em+1, and so for
2c5be 74 68 29 0a 20 20 20 20 20 20 2a 2a 20 54 68 65  th).      ** The
2c5bf 6e 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63 75  n compare the cu
2c5c0 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74  rrent GROUP BY t
2c5c1 65 72 6d 73 20 61 67 61 69 6e 73 74 20 74 68 65  erms against the
2c5c2 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73 0a   GROUP BY terms.
2c5c3 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68        ** from th
2c5c4 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77 20 63  e previous row c
2c5c5 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20  urrently stored 
2c5c6 69 6e 20 61 30 2c 20 61 31 2c 20 61 32 2e 2e 2e  in a0, a1, a2...
2c5c7 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2c5c8 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 20 3d 20  addrTopOfLoop = 
2c5c9 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2c5ca 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20  ntAddr(v);.     
2c5cb 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2c5cc 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a  eClear(pParse);.
2c5cd 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
2c5ce 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72  <pGroupBy->nExpr
2c5cf 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
2c5d0 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74 20  if( groupBySort 
2c5d1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
2c5d2 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2c5d3 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 41 67  , OP_Column, sAg
2c5d4 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
2c5d5 2c 20 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20  , j, iBMem+j);. 
2c5d6 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
2c5d7 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f          sAggInfo
2c5d8 2e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b  .directMode = 1;
2c5d9 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2c5da 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2c5db 65 2c 20 70 47 72 6f 75 70 42 79 2d 3e 61 5b 6a  e, pGroupBy->a[j
2c5dc 5d 2e 70 45 78 70 72 2c 20 69 42 4d 65 6d 2b 6a  ].pExpr, iBMem+j
2c5dd 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2c5de 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
2c5df 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2c5e0 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 69 41 4d 65  OP_Compare, iAMe
2c5e1 6d 2c 20 69 42 4d 65 6d 2c 20 70 47 72 6f 75 70  m, iBMem, pGroup
2c5e2 42 79 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20  By->nExpr,.     
2c5e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c5e4 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
2c5e5 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  Info, P4_KEYINFO
2c5e6 29 3b 0a 20 20 20 20 20 20 6a 31 20 3d 20 73 71  );.      j1 = sq
2c5e7 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2c5e8 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73  Addr(v);.      s
2c5e9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2c5ea 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 31 2b  (v, OP_Jump, j1+
2c5eb 31 2c 20 30 2c 20 6a 31 2b 31 29 3b 0a 0a 20 20  1, 0, j1+1);..  
2c5ec 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
2c5ed 63 6f 64 65 20 74 68 61 74 20 72 75 6e 73 20 77  code that runs w
2c5ee 68 65 6e 65 76 65 72 20 74 68 65 20 47 52 4f 55  henever the GROU
2c5ef 50 20 42 59 20 63 68 61 6e 67 65 73 2e 0a 20 20  P BY changes..  
2c5f0 20 20 20 20 2a 2a 20 43 68 61 6e 67 65 73 20 69      ** Changes i
2c5f1 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 61  n the GROUP BY a
2c5f2 72 65 20 64 65 74 65 63 74 65 64 20 62 79 20 74  re detected by t
2c5f3 68 65 20 70 72 65 76 69 6f 75 73 20 63 6f 64 65  he previous code
2c5f4 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 2e  .      ** block.
2c5f5 20 20 49 66 20 74 68 65 72 65 20 77 65 72 65 20    If there were 
2c5f6 6e 6f 20 63 68 61 6e 67 65 73 2c 20 74 68 69 73  no changes, this
2c5f7 20 62 6c 6f 63 6b 20 69 73 20 73 6b 69 70 70 65   block is skippe
2c5f8 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  d..      **.    
2c5f9 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 63    ** This code c
2c5fa 6f 70 69 65 73 20 63 75 72 72 65 6e 74 20 67 72  opies current gr
2c5fb 6f 75 70 20 62 79 20 74 65 72 6d 73 20 69 6e 20  oup by terms in 
2c5fc 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e 0a 20 20 20  b0,b1,b2,....   
2c5fd 20 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20 61 30     ** over to a0
2c5fe 2c 61 31 2c 61 32 2e 20 20 49 74 20 74 68 65 6e  ,a1,a2.  It then
2c5ff 20 63 61 6c 6c 73 20 74 68 65 20 6f 75 74 70 75   calls the outpu
2c600 74 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20 20  t subroutine.   
2c601 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 73     ** and resets
2c602 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 61   the aggregate a
2c603 63 63 75 6d 75 6c 61 74 6f 72 20 72 65 67 69 73  ccumulator regis
2c604 74 65 72 73 20 69 6e 20 70 72 65 70 61 72 61 74  ters in preparat
2c605 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72  ion.      ** for
2c606 20 74 68 65 20 6e 65 78 74 20 47 52 4f 55 50 20   the next GROUP 
2c607 42 59 20 62 61 74 63 68 2e 0a 20 20 20 20 20 20  BY batch..      
2c608 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2c609 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61  ExprCodeMove(pPa
2c60a 72 73 65 2c 20 69 42 4d 65 6d 2c 20 69 41 4d 65  rse, iBMem, iAMe
2c60b 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78  m, pGroupBy->nEx
2c60c 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  pr);.      sqlit
2c60d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c60e 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
2c60f 70 75 74 52 6f 77 2c 20 61 64 64 72 4f 75 74 70  putRow, addrOutp
2c610 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64  utRow);.      Vd
2c611 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6f  beComment((v, "o
2c612 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 22 29 29  utput one row"))
2c613 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c614 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c615 49 66 50 6f 73 2c 20 69 41 62 6f 72 74 46 6c 61  IfPos, iAbortFla
2c616 67 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20  g, addrEnd);.   
2c617 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2c618 76 2c 20 22 63 68 65 63 6b 20 61 62 6f 72 74 20  v, "check abort 
2c619 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20 20 73  flag"));.      s
2c61a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c61b 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
2c61c 67 52 65 73 65 74 2c 20 61 64 64 72 52 65 73 65  gReset, addrRese
2c61d 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  t);.      VdbeCo
2c61e 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 73 65 74  mment((v, "reset
2c61f 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b   accumulator"));
2c620 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74  ..      /* Updat
2c621 65 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  e the aggregate 
2c622 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 62 61 73  accumulators bas
2c623 65 64 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e  ed on the conten
2c624 74 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68  t of.      ** th
2c625 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 20 20  e current row.  
2c626 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
2c627 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2c628 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 75  (v, j1);.      u
2c629 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72  pdateAccumulator
2c62a 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
2c62b 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  fo);.      sqlit
2c62c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c62d 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69  OP_Integer, 1, i
2c62e 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20 20 20  UseFlag);.      
2c62f 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2c630 22 69 6e 64 69 63 61 74 65 20 64 61 74 61 20 69  "indicate data i
2c631 6e 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29 29  n accumulator"))
2c632 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 45 6e 64 20  ;..      /* End 
2c633 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 20 20  of the loop.    
2c634 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 67    */.      if( g
2c635 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20  roupBySort ){.  
2c636 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c637 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
2c638 78 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72  xt, sAggInfo.sor
2c639 74 69 6e 67 49 64 78 2c 20 61 64 64 72 54 6f 70  tingIdx, addrTop
2c63a 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 20 20 7d  OfLoop);.      }
2c63b 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
2c63c 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
2c63d 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 73  Info);.        s
2c63e 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2c63f 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f  ToNoop(v, addrSo
2c640 72 74 69 6e 67 49 64 78 2c 20 31 29 3b 0a 20 20  rtingIdx, 1);.  
2c641 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
2c642 4f 75 74 70 75 74 20 74 68 65 20 66 69 6e 61 6c  Output the final
2c643 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 0a 20   row of result. 
2c644 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2c645 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c646 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
2c647 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72 4f  OutputRow, addrO
2c648 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20  utputRow);.     
2c649 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2c64a 20 22 6f 75 74 70 75 74 20 66 69 6e 61 6c 20 72   "output final r
2c64b 6f 77 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ow"));..      /*
2c64c 20 4a 75 6d 70 20 6f 76 65 72 20 74 68 65 20 73   Jump over the s
2c64d 75 62 72 6f 75 74 69 6e 65 73 0a 20 20 20 20 20  ubroutines.     
2c64e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2c64f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c650 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 45  P_Goto, 0, addrE
2c651 6e 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47  nd);..      /* G
2c652 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75  enerate a subrou
2c653 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74  tine that output
2c654 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  s a single row o
2c655 66 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20  f the result.   
2c656 20 20 20 2a 2a 20 73 65 74 2e 20 20 54 68 69 73     ** set.  This
2c657 20 73 75 62 72 6f 75 74 69 6e 65 20 66 69 72 73   subroutine firs
2c658 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 69  t looks at the i
2c659 55 73 65 46 6c 61 67 2e 20 20 49 66 20 69 55 73  UseFlag.  If iUs
2c65a 65 46 6c 61 67 0a 20 20 20 20 20 20 2a 2a 20 69  eFlag.      ** i
2c65b 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
2c65c 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68  qual to zero, th
2c65d 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69 73 20  e subroutine is 
2c65e 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20 20 20  a no-op.  If.   
2c65f 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73     ** the proces
2c660 73 69 6e 67 20 63 61 6c 6c 73 20 66 6f 72 20 74  sing calls for t
2c661 68 65 20 71 75 65 72 79 20 74 6f 20 61 62 6f 72  he query to abor
2c662 74 2c 20 74 68 69 73 20 73 75 62 72 6f 75 74 69  t, this subrouti
2c663 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63 72  ne.      ** incr
2c664 65 6d 65 6e 74 73 20 74 68 65 20 69 41 62 6f 72  ements the iAbor
2c665 74 46 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f 63  tFlag memory loc
2c666 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 72 65 74  ation before ret
2c667 75 72 6e 69 6e 67 20 69 6e 0a 20 20 20 20 20 20  urning in.      
2c668 2a 2a 20 6f 72 64 65 72 20 74 6f 20 73 69 67 6e  ** order to sign
2c669 61 6c 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  al the caller to
2c66a 20 61 62 6f 72 74 2e 0a 20 20 20 20 20 20 2a 2f   abort..      */
2c66b 0a 20 20 20 20 20 20 61 64 64 72 53 65 74 41 62  .      addrSetAb
2c66c 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ort = sqlite3Vdb
2c66d 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
2c66e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c66f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2c670 6e 74 65 67 65 72 2c 20 31 2c 20 69 41 62 6f 72  nteger, 1, iAbor
2c671 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64  tFlag);.      Vd
2c672 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73  beComment((v, "s
2c673 65 74 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  et abort flag"))
2c674 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c675 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2c676 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70 75  Return, regOutpu
2c677 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 73 71 6c  tRow);.      sql
2c678 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
2c679 61 62 65 6c 28 76 2c 20 61 64 64 72 4f 75 74 70  abel(v, addrOutp
2c67a 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 61 64  utRow);.      ad
2c67b 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71  drOutputRow = sq
2c67c 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2c67d 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73  Addr(v);.      s
2c67e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c67f 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 55  (v, OP_IfPos, iU
2c680 73 65 46 6c 61 67 2c 20 61 64 64 72 4f 75 74 70  seFlag, addrOutp
2c681 75 74 52 6f 77 2b 32 29 3b 0a 20 20 20 20 20 20  utRow+2);.      
2c682 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2c683 22 47 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20  "Groupby result 
2c684 67 65 6e 65 72 61 74 6f 72 20 65 6e 74 72 79 20  generator entry 
2c685 70 6f 69 6e 74 22 29 29 3b 0a 20 20 20 20 20 20  point"));.      
2c686 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c687 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20  1(v, OP_Return, 
2c688 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20  regOutputRow);. 
2c689 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 67 67       finalizeAgg
2c68a 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 73 65  Functions(pParse
2c68b 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
2c68c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
2c68d 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
2c68e 48 61 76 69 6e 67 2c 20 61 64 64 72 4f 75 74 70  Having, addrOutp
2c68f 75 74 52 6f 77 2b 31 2c 20 53 51 4c 49 54 45 5f  utRow+1, SQLITE_
2c690 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
2c691 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
2c692 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  op(pParse, p, p-
2c693 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70  >pEList, 0, 0, p
2c694 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20 20 20  OrderBy,.       
2c695 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
2c696 69 73 74 69 6e 63 74 2c 20 70 44 65 73 74 2c 0a  istinct, pDest,.
2c697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c698 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75 74        addrOutput
2c699 52 6f 77 2b 31 2c 20 61 64 64 72 53 65 74 41 62  Row+1, addrSetAb
2c69a 6f 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ort);.      sqli
2c69b 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2c69c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f   OP_Return, regO
2c69d 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20  utputRow);.     
2c69e 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2c69f 20 22 65 6e 64 20 67 72 6f 75 70 62 79 20 72 65   "end groupby re
2c6a0 73 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 22 29  sult generator")
2c6a1 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e  );..      /* Gen
2c6a2 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
2c6a3 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65 73  ne that will res
2c6a4 65 74 20 74 68 65 20 67 72 6f 75 70 2d 62 79 20  et the group-by 
2c6a5 61 63 63 75 6d 75 6c 61 74 6f 72 0a 20 20 20 20  accumulator.    
2c6a6 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
2c6a7 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2c6a8 65 6c 28 76 2c 20 61 64 64 72 52 65 73 65 74 29  el(v, addrReset)
2c6a9 3b 0a 20 20 20 20 20 20 72 65 73 65 74 41 63 63  ;.      resetAcc
2c6aa 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65 2c  umulator(pParse,
2c6ab 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20   &sAggInfo);.   
2c6ac 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c6ad 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72  dOp1(v, OP_Retur
2c6ae 6e 2c 20 72 65 67 52 65 73 65 74 29 3b 0a 20 20  n, regReset);.  
2c6af 20 20 20 0a 20 20 20 20 7d 20 2f 2a 20 65 6e 64     .    } /* end
2c6b0 69 66 20 70 47 72 6f 75 70 42 79 2e 20 20 42 65  if pGroupBy.  Be
2c6b1 67 69 6e 20 61 67 67 72 65 67 61 74 65 20 71 75  gin aggregate qu
2c6b2 65 72 69 65 73 20 77 69 74 68 6f 75 74 20 47 52  eries without GR
2c6b3 4f 55 50 20 42 59 3a 20 2a 2f 0a 20 20 20 20 65  OUP BY: */.    e
2c6b4 6c 73 65 20 7b 0a 20 20 20 20 20 20 45 78 70 72  lse {.      Expr
2c6b5 4c 69 73 74 20 2a 70 44 65 6c 20 3d 20 30 3b 0a  List *pDel = 0;.
2c6b6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2c6b7 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 20  MIT_BTREECOUNT. 
2c6b8 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
2c6b9 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 54 61  ;.      if( (pTa
2c6ba 62 20 3d 20 69 73 53 69 6d 70 6c 65 43 6f 75 6e  b = isSimpleCoun
2c6bb 74 28 70 2c 20 26 73 41 67 67 49 6e 66 6f 29 29  t(p, &sAggInfo))
2c6bc 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  !=0 ){.        /
2c6bd 2a 20 49 66 20 69 73 53 69 6d 70 6c 65 43 6f 75  * If isSimpleCou
2c6be 6e 74 28 29 20 72 65 74 75 72 6e 73 20 61 20 70  nt() returns a p
2c6bf 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 61 62 6c  ointer to a Tabl
2c6c0 65 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65  e structure, the
2c6c1 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  n.        ** the
2c6c2 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69   SQL statement i
2c6c3 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20  s of the form:. 
2c6c4 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
2c6c5 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 63 6f    **   SELECT co
2c6c6 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74 62 6c  unt(*) FROM <tbl
2c6c7 3e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  >.        **.   
2c6c8 20 20 20 20 20 2a 2a 20 77 68 65 72 65 20 74 68       ** where th
2c6c9 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
2c6ca 65 20 72 65 74 75 72 6e 65 64 20 72 65 70 72 65  e returned repre
2c6cb 73 65 6e 74 73 20 74 61 62 6c 65 20 3c 74 62 6c  sents table <tbl
2c6cc 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  >..        **.  
2c6cd 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 73 74        ** This st
2c6ce 61 74 65 6d 65 6e 74 20 69 73 20 73 6f 20 63 6f  atement is so co
2c6cf 6d 6d 6f 6e 20 74 68 61 74 20 69 74 20 69 73 20  mmon that it is 
2c6d0 6f 70 74 69 6d 69 7a 65 64 20 73 70 65 63 69 61  optimized specia
2c6d1 6c 6c 79 2e 20 54 68 65 0a 20 20 20 20 20 20 20  lly. The.       
2c6d2 20 2a 2a 20 4f 50 5f 43 6f 75 6e 74 20 69 6e 73   ** OP_Count ins
2c6d3 74 72 75 63 74 69 6f 6e 20 69 73 20 65 78 65 63  truction is exec
2c6d4 75 74 65 64 20 65 69 74 68 65 72 20 6f 6e 20 74  uted either on t
2c6d5 68 65 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20  he intkey table 
2c6d6 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  that.        ** 
2c6d7 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74  contains the dat
2c6d8 61 20 66 6f 72 20 74 61 62 6c 65 20 3c 74 62 6c  a for table <tbl
2c6d9 3e 20 6f 72 20 6f 6e 20 6f 6e 65 20 6f 66 20 69  > or on one of i
2c6da 74 73 20 69 6e 64 65 78 65 73 2e 20 49 74 0a 20  ts indexes. It. 
2c6db 20 20 20 20 20 20 20 2a 2a 20 69 73 20 62 65 74         ** is bet
2c6dc 74 65 72 20 74 6f 20 65 78 65 63 75 74 65 20 74  ter to execute t
2c6dd 68 65 20 6f 70 20 6f 6e 20 61 6e 20 69 6e 64 65  he op on an inde
2c6de 78 2c 20 61 73 20 69 6e 64 65 78 65 73 20 61 72  x, as indexes ar
2c6df 65 20 61 6c 6d 6f 73 74 0a 20 20 20 20 20 20 20  e almost.       
2c6e0 20 2a 2a 20 61 6c 77 61 79 73 20 73 70 72 65 61   ** always sprea
2c6e1 64 20 61 63 72 6f 73 73 20 6c 65 73 73 20 70 61  d across less pa
2c6e2 67 65 73 20 74 68 61 6e 20 74 68 65 69 72 20 63  ges than their c
2c6e3 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62  orresponding tab
2c6e4 6c 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  les..        */.
2c6e5 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e          const in
2c6e6 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
2c6e7 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
2c6e8 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
2c6e9 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20  Schema);.       
2c6ea 20 63 6f 6e 73 74 20 69 6e 74 20 69 43 73 72 20   const int iCsr 
2c6eb 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2c6ec 3b 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20  ;     /* Cursor 
2c6ed 74 6f 20 73 63 61 6e 20 62 2d 74 72 65 65 20 2a  to scan b-tree *
2c6ee 2f 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78 20  /.        Index 
2c6ef 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20  *pIdx;          
2c6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c6f1 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
2c6f2 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 4b  ble */.        K
2c6f3 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
2c6f4 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2c6f5 20 20 20 20 2f 2a 20 4b 65 79 69 6e 66 6f 20 66      /* Keyinfo f
2c6f6 6f 72 20 73 63 61 6e 6e 65 64 20 69 6e 64 65 78  or scanned index
2c6f7 20 2a 2f 0a 20 20 20 20 20 20 20 20 49 6e 64 65   */.        Inde
2c6f8 78 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 20 20  x *pBest = 0;   
2c6f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c6fa 20 2f 2a 20 42 65 73 74 20 69 6e 64 65 78 20 66   /* Best index f
2c6fb 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20  ound so far */. 
2c6fc 20 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6f 74         int iRoot
2c6fd 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 20 20   = pTab->tnum;  
2c6fe 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2c6ff 6f 6f 74 20 70 61 67 65 20 6f 66 20 73 63 61 6e  oot page of scan
2c700 6e 65 64 20 62 2d 74 72 65 65 20 2a 2f 0a 0a 20  ned b-tree */.. 
2c701 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
2c702 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70  deVerifySchema(p
2c703 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20  Parse, iDb);.   
2c704 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c       sqlite3Tabl
2c705 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44  eLock(pParse, iD
2c706 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30  b, pTab->tnum, 0
2c707 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2c708 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 65 61 72  .        /* Sear
2c709 63 68 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78  ch for the index
2c70a 20 74 68 61 74 20 68 61 73 20 74 68 65 20 6c 65   that has the le
2c70b 61 73 74 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f  ast amount of co
2c70c 6c 75 6d 6e 73 2e 20 49 66 0a 20 20 20 20 20 20  lumns. If.      
2c70d 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 73 75    ** there is su
2c70e 63 68 20 61 6e 20 69 6e 64 65 78 2c 20 61 6e 64  ch an index, and
2c70f 20 69 74 20 68 61 73 20 6c 65 73 73 20 63 6f 6c   it has less col
2c710 75 6d 6e 73 20 74 68 61 6e 20 74 68 65 20 74 61  umns than the ta
2c711 62 6c 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64  ble.        ** d
2c712 6f 65 73 2c 20 74 68 65 6e 20 77 65 20 63 61 6e  oes, then we can
2c713 20 61 73 73 75 6d 65 20 74 68 61 74 20 69 74 20   assume that it 
2c714 63 6f 6e 73 75 6d 65 73 20 6c 65 73 73 20 73 70  consumes less sp
2c715 61 63 65 20 6f 6e 20 64 69 73 6b 20 61 6e 64 0a  ace on disk and.
2c716 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20          ** will 
2c717 74 68 65 72 65 66 6f 72 65 20 62 65 20 63 68 65  therefore be che
2c718 61 70 65 72 20 74 6f 20 73 63 61 6e 20 74 6f 20  aper to scan to 
2c719 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 71 75  determine the qu
2c71a 65 72 79 20 72 65 73 75 6c 74 2e 0a 20 20 20 20  ery result..    
2c71b 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63      ** In this c
2c71c 61 73 65 20 73 65 74 20 69 52 6f 6f 74 20 74 6f  ase set iRoot to
2c71d 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
2c71e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64  umber of the ind
2c71f 65 78 20 62 2d 74 72 65 65 0a 20 20 20 20 20 20  ex b-tree.      
2c720 20 20 2a 2a 20 61 6e 64 20 70 4b 65 79 49 6e 66    ** and pKeyInf
2c721 6f 20 74 6f 20 74 68 65 20 4b 65 79 49 6e 66 6f  o to the KeyInfo
2c722 20 73 74 72 75 63 74 75 72 65 20 72 65 71 75 69   structure requi
2c723 72 65 64 20 74 6f 20 6e 61 76 69 67 61 74 65 20  red to navigate 
2c724 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  the.        ** i
2c725 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20 2a 2a  ndex..        **
2c726 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20 70  .        ** In p
2c727 72 61 63 74 69 63 65 20 74 68 65 20 4b 65 79 49  ractice the KeyI
2c728 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 77 69  nfo structure wi
2c729 6c 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20  ll not be used. 
2c72a 49 74 20 69 73 20 6f 6e 6c 79 20 0a 20 20 20 20  It is only .    
2c72b 20 20 20 20 2a 2a 20 70 61 73 73 65 64 20 74 6f      ** passed to
2c72c 20 6b 65 65 70 20 4f 50 5f 4f 70 65 6e 52 65 61   keep OP_OpenRea
2c72d 64 20 68 61 70 70 79 2e 0a 20 20 20 20 20 20 20  d happy..       
2c72e 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28   */.        for(
2c72f 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
2c730 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
2c731 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
2c732 20 20 20 20 20 20 69 66 28 20 21 70 42 65 73 74        if( !pBest
2c733 20 7c 7c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   || pIdx->nColum
2c734 6e 3c 70 42 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e  n<pBest->nColumn
2c735 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2c736 70 42 65 73 74 20 3d 20 70 49 64 78 3b 0a 20 20  pBest = pIdx;.  
2c737 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c738 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2c739 70 42 65 73 74 20 26 26 20 70 42 65 73 74 2d 3e  pBest && pBest->
2c73a 6e 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43  nColumn<pTab->nC
2c73b 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ol ){.          
2c73c 69 52 6f 6f 74 20 3d 20 70 42 65 73 74 2d 3e 74  iRoot = pBest->t
2c73d 6e 75 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 70  num;.          p
2c73e 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65  KeyInfo = sqlite
2c73f 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
2c740 61 72 73 65 2c 20 70 42 65 73 74 29 3b 0a 20 20  arse, pBest);.  
2c741 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
2c742 20 2f 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d   /* Open a read-
2c743 6f 6e 6c 79 20 63 75 72 73 6f 72 2c 20 65 78 65  only cursor, exe
2c744 63 75 74 65 20 74 68 65 20 4f 50 5f 43 6f 75 6e  cute the OP_Coun
2c745 74 2c 20 63 6c 6f 73 65 20 74 68 65 20 63 75 72  t, close the cur
2c746 73 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  sor. */.        
2c747 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c748 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  3(v, OP_OpenRead
2c749 2c 20 69 43 73 72 2c 20 69 52 6f 6f 74 2c 20 69  , iCsr, iRoot, i
2c74a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Db);.        if(
2c74b 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20   pKeyInfo ){.   
2c74c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2c74d 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
2c74e 2c 20 28 63 68 61 72 20 2a 29 70 4b 65 79 49 6e  , (char *)pKeyIn
2c74f 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  fo, P4_KEYINFO_H
2c750 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20  ANDOFF);.       
2c751 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
2c752 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c753 4f 50 5f 43 6f 75 6e 74 2c 20 69 43 73 72 2c 20  OP_Count, iCsr, 
2c754 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 30  sAggInfo.aFunc[0
2c755 5d 2e 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20  ].iMem);.       
2c756 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c757 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
2c758 69 43 73 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  iCsr);.      }el
2c759 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
2c75a 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f  ITE_OMIT_BTREECO
2c75b 55 4e 54 20 2a 2f 0a 20 20 20 20 20 20 7b 0a 20  UNT */.      {. 
2c75c 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20         /* Check 
2c75d 69 66 20 74 68 65 20 71 75 65 72 79 20 69 73 20  if the query is 
2c75e 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  of one of the fo
2c75f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a 20  llowing forms:. 
2c760 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
2c761 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 69    **   SELECT mi
2c762 6e 28 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20  n(x) FROM ....  
2c763 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43        **   SELEC
2c764 54 20 6d 61 78 28 78 29 20 46 52 4f 4d 20 2e 2e  T max(x) FROM ..
2c765 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
2c766 20 20 20 20 20 2a 2a 20 49 66 20 69 74 20 69 73       ** If it is
2c767 2c 20 74 68 65 6e 20 61 73 6b 20 74 68 65 20 63  , then ask the c
2c768 6f 64 65 20 69 6e 20 77 68 65 72 65 2e 63 20 74  ode in where.c t
2c769 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 73 6f 72  o attempt to sor
2c76a 74 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20  t results.      
2c76b 20 20 2a 2a 20 61 73 20 69 66 20 74 68 65 72 65    ** as if there
2c76c 20 77 61 73 20 61 6e 20 22 4f 52 44 45 52 20 4f   was an "ORDER O
2c76d 4e 20 78 22 20 6f 72 20 22 4f 52 44 45 52 20 4f  N x" or "ORDER O
2c76e 4e 20 78 20 44 45 53 43 22 20 63 6c 61 75 73 65  N x DESC" clause
2c76f 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 66  . .        ** If
2c770 20 77 68 65 72 65 2e 63 20 69 73 20 61 62 6c 65   where.c is able
2c771 20 74 6f 20 70 72 6f 64 75 63 65 20 72 65 73 75   to produce resu
2c772 6c 74 73 20 73 6f 72 74 65 64 20 69 6e 20 74 68  lts sorted in th
2c773 69 73 20 6f 72 64 65 72 2c 20 74 68 65 6e 0a 20  is order, then. 
2c774 20 20 20 20 20 20 20 2a 2a 20 61 64 64 20 76 64         ** add vd
2c775 62 65 20 63 6f 64 65 20 74 6f 20 62 72 65 61 6b  be code to break
2c776 20 6f 75 74 20 6f 66 20 74 68 65 20 70 72 6f 63   out of the proc
2c777 65 73 73 69 6e 67 20 6c 6f 6f 70 20 61 66 74 65  essing loop afte
2c778 72 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a  r the .        *
2c779 2a 20 66 69 72 73 74 20 69 74 65 72 61 74 69 6f  * first iteratio
2c77a 6e 20 28 73 69 6e 63 65 20 74 68 65 20 66 69 72  n (since the fir
2c77b 73 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  st iteration of 
2c77c 74 68 65 20 6c 6f 6f 70 20 69 73 20 0a 20 20 20  the loop is .   
2c77d 20 20 20 20 20 2a 2a 20 67 75 61 72 61 6e 74 65       ** guarante
2c77e 65 64 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e  ed to operate on
2c77f 20 74 68 65 20 72 6f 77 20 77 69 74 68 20 74 68   the row with th
2c780 65 20 6d 69 6e 69 6d 75 6d 20 6f 72 20 6d 61 78  e minimum or max
2c781 69 6d 75 6d 20 0a 20 20 20 20 20 20 20 20 2a 2a  imum .        **
2c782 20 76 61 6c 75 65 20 6f 66 20 78 2c 20 74 68 65   value of x, the
2c783 20 6f 6e 6c 79 20 72 6f 77 20 72 65 71 75 69 72   only row requir
2c784 65 64 29 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ed)..        **.
2c785 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73 70 65          ** A spe
2c786 63 69 61 6c 20 66 6c 61 67 20 6d 75 73 74 20 62  cial flag must b
2c787 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  e passed to sqli
2c788 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20  te3WhereBegin() 
2c789 74 6f 20 73 6c 69 67 68 74 6c 79 0a 20 20 20 20  to slightly.    
2c78a 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 20 62 65      ** modify be
2c78b 68 61 76 69 6f 75 72 20 61 73 20 66 6f 6c 6c 6f  haviour as follo
2c78c 77 73 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ws:.        **. 
2c78d 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 49 66         **   + If
2c78e 20 74 68 65 20 71 75 65 72 79 20 69 73 20 61 20   the query is a 
2c78f 22 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 22 2c  "SELECT min(x)",
2c790 20 74 68 65 6e 20 74 68 65 20 6c 6f 6f 70 20 63   then the loop c
2c791 6f 64 65 64 20 62 79 0a 20 20 20 20 20 20 20 20  oded by.        
2c792 2a 2a 20 20 20 20 20 77 68 65 72 65 2e 63 20 73  **     where.c s
2c793 68 6f 75 6c 64 20 6e 6f 74 20 69 74 65 72 61 74  hould not iterat
2c794 65 20 6f 76 65 72 20 61 6e 79 20 76 61 6c 75 65  e over any value
2c795 73 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 76 61  s with a NULL va
2c796 6c 75 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 20  lue.        **  
2c797 20 20 20 66 6f 72 20 78 2e 0a 20 20 20 20 20 20     for x..      
2c798 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2c799 20 20 2b 20 54 68 65 20 6f 70 74 69 6d 69 7a 65    + The optimize
2c79a 72 20 63 6f 64 65 20 69 6e 20 77 68 65 72 65 2e  r code in where.
2c79b 63 20 28 74 68 65 20 74 68 69 6e 67 20 74 68 61  c (the thing tha
2c79c 74 20 64 65 63 69 64 65 73 20 77 68 69 63 68 0a  t decides which.
2c79d 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 69          **     i
2c79e 6e 64 65 78 20 6f 72 20 69 6e 64 69 63 65 73 20  ndex or indices 
2c79f 74 6f 20 75 73 65 29 20 73 68 6f 75 6c 64 20 70  to use) should p
2c7a0 6c 61 63 65 20 61 20 64 69 66 66 65 72 65 6e 74  lace a different
2c7a1 20 70 72 69 6f 72 69 74 79 20 6f 6e 20 0a 20 20   priority on .  
2c7a2 20 20 20 20 20 20 2a 2a 20 20 20 20 20 73 61 74        **     sat
2c7a3 69 73 66 79 69 6e 67 20 74 68 65 20 27 4f 52 44  isfying the 'ORD
2c7a4 45 52 20 42 59 27 20 63 6c 61 75 73 65 20 74 68  ER BY' clause th
2c7a5 61 6e 20 69 74 20 64 6f 65 73 20 69 6e 20 6f 74  an it does in ot
2c7a6 68 65 72 20 63 61 73 65 73 2e 0a 20 20 20 20 20  her cases..     
2c7a7 20 20 20 2a 2a 20 20 20 20 20 52 65 66 65 72 20     **     Refer 
2c7a8 74 6f 20 63 6f 64 65 20 61 6e 64 20 63 6f 6d 6d  to code and comm
2c7a9 65 6e 74 73 20 69 6e 20 77 68 65 72 65 2e 63 20  ents in where.c 
2c7aa 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20 20 20  for details..   
2c7ab 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2c7ac 45 78 70 72 4c 69 73 74 20 2a 70 4d 69 6e 4d 61  ExprList *pMinMa
2c7ad 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 75  x = 0;.        u
2c7ae 38 20 66 6c 61 67 20 3d 20 6d 69 6e 4d 61 78 51  8 flag = minMaxQ
2c7af 75 65 72 79 28 70 29 3b 0a 20 20 20 20 20 20 20  uery(p);.       
2c7b0 20 69 66 28 20 66 6c 61 67 20 29 7b 0a 20 20 20   if( flag ){.   
2c7b1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
2c7b2 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2c7b3 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  p->pEList->a[0].
2c7b4 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2c7b5 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 20 20  ect) );.        
2c7b6 20 20 70 4d 69 6e 4d 61 78 20 3d 20 73 71 6c 69    pMinMax = sqli
2c7b7 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
2c7b8 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  b, p->pEList->a[
2c7b9 30 5d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  0].pExpr->x.pLis
2c7ba 74 2c 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  t,0);.          
2c7bb 70 44 65 6c 20 3d 20 70 4d 69 6e 4d 61 78 3b 0a  pDel = pMinMax;.
2c7bc 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4d            if( pM
2c7bd 69 6e 4d 61 78 20 26 26 20 21 64 62 2d 3e 6d 61  inMax && !db->ma
2c7be 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2c7bf 20 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d 61            pMinMa
2c7c0 78 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65  x->a[0].sortOrde
2c7c1 72 20 3d 20 66 6c 61 67 21 3d 57 48 45 52 45 5f  r = flag!=WHERE_
2c7c2 4f 52 44 45 52 42 59 5f 4d 49 4e 20 3f 31 3a 30  ORDERBY_MIN ?1:0
2c7c3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d  ;.            pM
2c7c4 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 70 45 78 70  inMax->a[0].pExp
2c7c5 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d  r->op = TK_COLUM
2c7c6 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  N;.          }. 
2c7c7 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20         }.  .    
2c7c8 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65      /* This case
2c7c9 20 72 75 6e 73 20 69 66 20 74 68 65 20 61 67 67   runs if the agg
2c7ca 72 65 67 61 74 65 20 68 61 73 20 6e 6f 20 47 52  regate has no GR
2c7cb 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20  OUP BY clause.  
2c7cc 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  The.        ** p
2c7cd 72 6f 63 65 73 73 69 6e 67 20 69 73 20 6d 75 63  rocessing is muc
2c7ce 68 20 73 69 6d 70 6c 65 72 20 73 69 6e 63 65 20  h simpler since 
2c7cf 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20  there is only a 
2c7d0 73 69 6e 67 6c 65 20 72 6f 77 0a 20 20 20 20 20  single row.     
2c7d1 20 20 20 2a 2a 20 6f 66 20 6f 75 74 70 75 74 2e     ** of output.
2c7d2 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
2c7d3 20 20 20 20 72 65 73 65 74 41 63 63 75 6d 75 6c      resetAccumul
2c7d4 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41  ator(pParse, &sA
2c7d5 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  ggInfo);.       
2c7d6 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
2c7d7 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72  3WhereBegin(pPar
2c7d8 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57  se, pTabList, pW
2c7d9 68 65 72 65 2c 20 26 70 4d 69 6e 4d 61 78 2c 20  here, &pMinMax, 
2c7da 66 6c 61 67 29 3b 0a 20 20 20 20 20 20 20 20 69  flag);.        i
2c7db 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 7b 0a  f( pWInfo==0 ){.
2c7dc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2c7dd 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2c7de 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 20  db, pDel);.     
2c7df 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74       goto select
2c7e0 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _end;.        }.
2c7e1 20 20 20 20 20 20 20 20 75 70 64 61 74 65 41 63          updateAc
2c7e2 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
2c7e3 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
2c7e4 20 20 20 20 20 20 69 66 28 20 21 70 4d 69 6e 4d        if( !pMinM
2c7e5 61 78 20 26 26 20 66 6c 61 67 20 29 7b 0a 20 20  ax && flag ){.  
2c7e6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c7e7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c7e8 47 6f 74 6f 2c 20 30 2c 20 70 57 49 6e 66 6f 2d  Goto, 0, pWInfo-
2c7e9 3e 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20  >iBreak);.      
2c7ea 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2c7eb 28 76 2c 20 22 25 73 28 29 20 62 79 20 69 6e 64  (v, "%s() by ind
2c7ec 65 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ex",.           
2c7ed 20 20 20 20 20 28 66 6c 61 67 3d 3d 57 48 45 52       (flag==WHER
2c7ee 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3f 22 6d  E_ORDERBY_MIN?"m
2c7ef 69 6e 22 3a 22 6d 61 78 22 29 29 29 3b 0a 20 20  in":"max")));.  
2c7f0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c7f1 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
2c7f2 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  pWInfo);.       
2c7f3 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63   finalizeAggFunc
2c7f4 74 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 26 73  tions(pParse, &s
2c7f5 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
2c7f6 7d 0a 0a 20 20 20 20 20 20 70 4f 72 64 65 72 42  }..      pOrderB
2c7f7 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  y = 0;.      sql
2c7f8 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2c7f9 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c  pParse, pHaving,
2c7fa 20 61 64 64 72 45 6e 64 2c 20 53 51 4c 49 54 45   addrEnd, SQLITE
2c7fb 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2c7fc 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
2c7fd 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
2c7fe 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20  ->pEList, 0, 0, 
2c7ff 30 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20  0, -1, .        
2c800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44                pD
2c801 65 73 74 2c 20 61 64 64 72 45 6e 64 2c 20 61 64  est, addrEnd, ad
2c802 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 73 71  drEnd);.      sq
2c803 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
2c804 65 74 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20  ete(db, pDel);. 
2c805 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2c806 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2c807 28 76 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20  (v, addrEnd);.  
2c808 20 20 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20    .  } /* endif 
2c809 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20  aggregate query 
2c80a 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  */..  /* If ther
2c80b 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  e is an ORDER BY
2c80c 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 77 65   clause, then we
2c80d 20 6e 65 65 64 20 74 6f 20 73 6f 72 74 20 74 68   need to sort th
2c80e 65 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20 61  e results.  ** a
2c80f 6e 64 20 73 65 6e 64 20 74 68 65 6d 20 74 6f 20  nd send them to 
2c810 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 65  the callback one
2c811 20 62 79 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20   by one..  */.  
2c812 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a  if( pOrderBy ){.
2c813 20 20 20 20 67 65 6e 65 72 61 74 65 53 6f 72 74      generateSort
2c814 54 61 69 6c 28 70 50 61 72 73 65 2c 20 70 2c 20  Tail(pParse, p, 
2c815 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  v, pEList->nExpr
2c816 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a 20  , pDest);.  }.. 
2c817 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
2c818 20 73 6b 69 70 20 74 68 69 73 20 71 75 65 72 79   skip this query
2c819 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
2c81a 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2c81b 76 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20  v, iEnd);..  /* 
2c81c 54 68 65 20 53 45 4c 45 43 54 20 77 61 73 20 73  The SELECT was s
2c81d 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 64 65  uccessfully code
2c81e 64 2e 20 20 20 53 65 74 20 74 68 65 20 72 65 74  d.   Set the ret
2c81f 75 72 6e 20 63 6f 64 65 20 74 6f 20 30 0a 20 20  urn code to 0.  
2c820 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20 6e  ** to indicate n
2c821 6f 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20  o errors..  */. 
2c822 20 72 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43   rc = 0;..  /* C
2c823 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 20 74 6f 20  ontrol jumps to 
2c824 68 65 72 65 20 69 66 20 61 6e 20 65 72 72 6f 72  here if an error
2c825 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20   is encountered 
2c826 61 62 6f 76 65 2c 20 6f 72 20 75 70 6f 6e 0a 20  above, or upon. 
2c827 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 63   ** successful c
2c828 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 53 45 4c  oding of the SEL
2c829 45 43 54 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63 74  ECT..  */.select
2c82a 5f 65 6e 64 3a 0a 0a 20 20 2f 2a 20 49 64 65 6e  _end:..  /* Iden
2c82b 74 69 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  tify column name
2c82c 73 20 69 66 20 72 65 73 75 6c 74 73 20 6f 66 20  s if results of 
2c82d 74 68 65 20 53 45 4c 45 43 54 20 61 72 65 20 74  the SELECT are t
2c82e 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 20 20 2a  o be output..  *
2c82f 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
2c830 54 45 5f 4f 4b 20 26 26 20 70 44 65 73 74 2d 3e  TE_OK && pDest->
2c831 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
2c832 74 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74  t ){.    generat
2c833 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  eColumnNames(pPa
2c834 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
2c835 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 73  EList);.  }..  s
2c836 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2c837 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29 3b   sAggInfo.aCol);
2c838 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2c839 28 64 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46  (db, sAggInfo.aF
2c83a 75 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  unc);.  return r
2c83b 63 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65  c;.}..#if define
2c83c 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
2c83d 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /*.*************
2c83e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c83f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c841 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c842 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
2c843 69 6e 67 20 63 6f 64 65 20 69 73 20 75 73 65 64  ing code is used
2c844 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
2c845 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
2c846 20 20 54 68 65 20 63 6f 64 65 0a 2a 2a 20 74 68    The code.** th
2c847 61 74 20 66 6f 6c 6c 6f 77 73 20 64 6f 65 73 20  at follows does 
2c848 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 6e 6f  not appear in no
2c849 72 6d 61 6c 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a  rmal builds..**.
2c84a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
2c84b 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 70 72  s are used to pr
2c84c 69 6e 74 20 6f 75 74 20 74 68 65 20 63 6f 6e 74  int out the cont
2c84d 65 6e 74 20 6f 66 20 61 6c 6c 20 6f 72 20 70 61  ent of all or pa
2c84e 72 74 20 6f 66 20 61 20 0a 2a 2a 20 70 61 72 73  rt of a .** pars
2c84f 65 20 73 74 72 75 63 74 75 72 65 73 20 73 75 63  e structures suc
2c850 68 20 61 73 20 53 65 6c 65 63 74 20 6f 72 20 45  h as Select or E
2c851 78 70 72 2e 20 20 53 75 63 68 20 70 72 69 6e 74  xpr.  Such print
2c852 6f 75 74 73 20 61 72 65 20 75 73 65 66 75 6c 0a  outs are useful.
2c853 2a 2a 20 66 6f 72 20 68 65 6c 70 69 6e 67 20 74  ** for helping t
2c854 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 77 68 61  o understand wha
2c855 74 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 69  t is happening i
2c856 6e 73 69 64 65 20 74 68 65 20 63 6f 64 65 20 67  nside the code g
2c857 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 64 75 72 69  enerator.** duri
2c858 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e  ng the execution
2c859 20 6f 66 20 63 6f 6d 70 6c 65 78 20 53 45 4c 45   of complex SELE
2c85a 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  CT statements..*
2c85b 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
2c85c 6e 65 20 61 72 65 20 6e 6f 74 20 63 61 6c 6c 65  ne are not calle
2c85d 64 20 61 6e 79 77 68 65 72 65 20 66 72 6f 6d 20  d anywhere from 
2c85e 77 69 74 68 69 6e 20 74 68 65 20 6e 6f 72 6d 61  within the norma
2c85f 6c 0a 2a 2a 20 63 6f 64 65 20 62 61 73 65 2e 20  l.** code base. 
2c860 20 54 68 65 6e 20 61 72 65 20 69 6e 74 65 6e 64   Then are intend
2c861 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20  ed to be called 
2c862 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
2c863 64 65 62 75 67 67 65 72 0a 2a 2a 20 6f 72 20 66  debugger.** or f
2c864 72 6f 6d 20 74 65 6d 70 6f 72 61 72 79 20 22 70  rom temporary "p
2c865 72 69 6e 74 66 22 20 73 74 61 74 65 6d 65 6e 74  rintf" statement
2c866 73 20 69 6e 73 65 72 74 65 64 20 66 6f 72 20 64  s inserted for d
2c867 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  ebugging..*/.SQL
2c868 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2c869 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70   sqlite3PrintExp
2c86a 72 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66  r(Expr *p){.  if
2c86b 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
2c86c 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75  ty(p, EP_IntValu
2c86d 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65  e) && p->u.zToke
2c86e 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  n ){.    sqlite3
2c86f 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 25 73  DebugPrintf("(%s
2c870 22 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b  ", p->u.zToken);
2c871 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
2c872 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2c873 28 22 28 25 64 22 2c 20 70 2d 3e 6f 70 29 3b 0a  ("(%d", p->op);.
2c874 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4c 65    }.  if( p->pLe
2c875 66 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ft ){.    sqlite
2c876 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 22  3DebugPrintf(" "
2c877 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
2c878 69 6e 74 45 78 70 72 28 70 2d 3e 70 4c 65 66 74  intExpr(p->pLeft
2c879 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
2c87a 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71  pRight ){.    sq
2c87b 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2c87c 28 22 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  (" ");.    sqlit
2c87d 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70  e3PrintExpr(p->p
2c87e 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 73 71  Right);.  }.  sq
2c87f 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2c880 28 22 29 22 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  (")");.}.SQLITE_
2c881 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2c882 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73  ite3PrintExprLis
2c883 74 28 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73  t(ExprList *pLis
2c884 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  t){.  int i;.  f
2c885 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
2c886 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
2c887 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2c888 70 72 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  pr(pList->a[i].p
2c889 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20 69  Expr);.    if( i
2c88a 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20  <pList->nExpr-1 
2c88b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2c88c 44 65 62 75 67 50 72 69 6e 74 66 28 22 2c 20 22  DebugPrintf(", "
2c88d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53  );.    }.  }.}.S
2c88e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2c88f 69 64 20 73 71 6c 69 74 65 33 50 72 69 6e 74 53  id sqlite3PrintS
2c890 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70 2c  elect(Select *p,
2c891 20 69 6e 74 20 69 6e 64 65 6e 74 29 7b 0a 20 20   int indent){.  
2c892 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2c893 74 66 28 22 25 2a 73 53 45 4c 45 43 54 28 25 70  tf("%*sSELECT(%p
2c894 29 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 2c  ) ", indent, "",
2c895 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 72   p);.  sqlite3Pr
2c896 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70  intExprList(p->p
2c897 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  EList);.  sqlite
2c898 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e  3DebugPrintf("\n
2c899 22 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53 72  ");.  if( p->pSr
2c89a 63 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  c ){.    char *z
2c89b 50 72 65 66 69 78 3b 0a 20 20 20 20 69 6e 74 20  Prefix;.    int 
2c89c 69 3b 0a 20 20 20 20 7a 50 72 65 66 69 78 20 3d  i;.    zPrefix =
2c89d 20 22 46 52 4f 4d 22 3b 0a 20 20 20 20 66 6f 72   "FROM";.    for
2c89e 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d  (i=0; i<p->pSrc-
2c89f 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nSrc; i++){.   
2c8a0 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
2c8a1 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20  t_item *pItem = 
2c8a2 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a  &p->pSrc->a[i];.
2c8a3 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62        sqlite3Deb
2c8a4 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 22 2c  ugPrintf("%*s ",
2c8a5 20 69 6e 64 65 6e 74 2b 36 2c 20 7a 50 72 65 66   indent+6, zPref
2c8a6 69 78 29 3b 0a 20 20 20 20 20 20 7a 50 72 65 66  ix);.      zPref
2c8a7 69 78 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 69  ix = "";.      i
2c8a8 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  f( pItem->pSelec
2c8a9 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
2c8aa 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2c8ab 22 28 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20  "(\n");.        
2c8ac 73 71 6c 69 74 65 33 50 72 69 6e 74 53 65 6c 65  sqlite3PrintSele
2c8ad 63 74 28 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  ct(pItem->pSelec
2c8ae 74 2c 20 69 6e 64 65 6e 74 2b 31 30 29 3b 0a 20  t, indent+10);. 
2c8af 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
2c8b0 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 29 22  bugPrintf("%*s)"
2c8b1 2c 20 69 6e 64 65 6e 74 2b 38 2c 20 22 22 29 3b  , indent+8, "");
2c8b2 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
2c8b3 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 7b   pItem->zName ){
2c8b4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c8b5 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 73 22  DebugPrintf("%s"
2c8b6 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  , pItem->zName);
2c8b7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2c8b8 66 28 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 29  f( pItem->pTab )
2c8b9 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2c8ba 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 74  3DebugPrintf("(t
2c8bb 61 62 6c 65 3a 20 25 73 29 22 2c 20 70 49 74 65  able: %s)", pIte
2c8bc 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  m->pTab->zName);
2c8bd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2c8be 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73  f( pItem->zAlias
2c8bf 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2c8c0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2c8c1 20 41 53 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e   AS %s", pItem->
2c8c2 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 20 20 7d  zAlias);.      }
2c8c3 0a 20 20 20 20 20 20 69 66 28 20 69 3c 70 2d 3e  .      if( i<p->
2c8c4 70 53 72 63 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a  pSrc->nSrc-1 ){.
2c8c5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2c8c6 65 62 75 67 50 72 69 6e 74 66 28 22 2c 22 29 3b  ebugPrintf(",");
2c8c7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2c8c8 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2c8c9 66 28 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 20  f("\n");.    }. 
2c8ca 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65   }.  if( p->pWhe
2c8cb 72 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  re ){.    sqlite
2c8cc 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a  3DebugPrintf("%*
2c8cd 73 20 57 48 45 52 45 20 22 2c 20 69 6e 64 65 6e  s WHERE ", inden
2c8ce 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69  t, "");.    sqli
2c8cf 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e  te3PrintExpr(p->
2c8d0 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c  pWhere);.    sql
2c8d1 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2c8d2 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28  "\n");.  }.  if(
2c8d3 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a   p->pGroupBy ){.
2c8d4 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2c8d5 50 72 69 6e 74 66 28 22 25 2a 73 20 47 52 4f 55  Printf("%*s GROU
2c8d6 50 20 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20  P BY ", indent, 
2c8d7 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "");.    sqlite3
2c8d8 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d  PrintExprList(p-
2c8d9 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20  >pGroupBy);.    
2c8da 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2c8db 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20  tf("\n");.  }.  
2c8dc 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 29  if( p->pHaving )
2c8dd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
2c8de 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 48 41  ugPrintf("%*s HA
2c8df 56 49 4e 47 20 22 2c 20 69 6e 64 65 6e 74 2c 20  VING ", indent, 
2c8e0 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "");.    sqlite3
2c8e1 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 48 61  PrintExpr(p->pHa
2c8e2 76 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  ving);.    sqlit
2c8e3 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c  e3DebugPrintf("\
2c8e4 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n");.  }.  if( p
2c8e5 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
2c8e6 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2c8e7 69 6e 74 66 28 22 25 2a 73 20 4f 52 44 45 52 20  intf("%*s ORDER 
2c8e8 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  BY ", indent, ""
2c8e9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
2c8ea 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70  intExprList(p->p
2c8eb 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 73 71  OrderBy);.    sq
2c8ec 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2c8ed 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 7d 0a 2f 2a  ("\n");.  }.}./*
2c8ee 20 45 6e 64 20 6f 66 20 74 68 65 20 73 74 72 75   End of the stru
2c8ef 63 74 75 72 65 20 64 65 62 75 67 20 70 72 69 6e  cture debug prin
2c8f0 74 69 6e 67 20 63 6f 64 65 0a 2a 2a 2a 2a 2a 2a  ting code.******
2c8f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8f5 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20  *******/.#endif 
2c8f6 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
2c8f7 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e  E_TEST) || defin
2c8f8 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
2c8f9 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2c8fa 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 65 6c 65  **** End of sele
2c8fb 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ct.c ***********
2c8fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8fe 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2c8ff 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2c900 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  table.c ********
2c901 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c902 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c903 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
2c904 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
2c905 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2c906 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2c907 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2c908 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2c909 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2c90a 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2c90b 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2c90c 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2c90d 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2c90e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2c90f 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2c910 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2c911 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2c912 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2c913 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2c914 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2c915 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2c916 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2c917 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c918 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c919 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c91a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2c91b 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2c91c 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65  s the sqlite3_ge
2c91d 74 5f 74 61 62 6c 65 28 29 20 61 6e 64 20 73 71  t_table() and sq
2c91e 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
2c91f 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ().** interface 
2c920 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 73 65  routines.  These
2c921 20 61 72 65 20 6a 75 73 74 20 77 72 61 70 70 65   are just wrappe
2c922 72 73 20 61 72 6f 75 6e 64 20 74 68 65 20 6d 61  rs around the ma
2c923 69 6e 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  in.** interface 
2c924 72 6f 75 74 69 6e 65 20 6f 66 20 73 71 6c 69 74  routine of sqlit
2c925 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a 0a 2a 2a  e3_exec()..**.**
2c926 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
2c927 61 72 65 20 69 6e 20 61 20 73 65 70 61 72 61 74  are in a separat
2c928 65 20 66 69 6c 65 73 20 73 6f 20 74 68 61 74 20  e files so that 
2c929 74 68 65 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65  they will not be
2c92a 20 6c 69 6e 6b 65 64 0a 2a 2a 20 69 66 20 74 68   linked.** if th
2c92b 65 79 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e  ey are not used.
2c92c 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
2c92d 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42  ITE_OMIT_GET_TAB
2c92e 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73  LE../*.** This s
2c92f 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
2c930 20 74 6f 20 70 61 73 73 20 64 61 74 61 20 66 72   to pass data fr
2c931 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  om sqlite3_get_t
2c932 61 62 6c 65 28 29 20 74 68 72 6f 75 67 68 0a 2a  able() through.*
2c933 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  * to the callbac
2c934 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  k function is us
2c935 65 73 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20  es to build the 
2c936 72 65 73 75 6c 74 2e 0a 2a 2f 0a 74 79 70 65 64  result..*/.typed
2c937 65 66 20 73 74 72 75 63 74 20 54 61 62 52 65 73  ef struct TabRes
2c938 75 6c 74 20 7b 0a 20 20 63 68 61 72 20 2a 2a 61  ult {.  char **a
2c939 7a 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 41 63  zResult;   /* Ac
2c93a 63 75 6d 75 6c 61 74 65 64 20 6f 75 74 70 75 74  cumulated output
2c93b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72   */.  char *zErr
2c93c 4d 73 67 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f  Msg;     /* Erro
2c93d 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 2c 20  r message text, 
2c93e 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
2c93f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c  rs */.  int nAll
2c940 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 6c  oc;        /* Sl
2c941 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ots allocated fo
2c942 72 20 61 7a 52 65 73 75 6c 74 5b 5d 20 2a 2f 0a  r azResult[] */.
2c943 20 20 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20    int nRow;     
2c944 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2c945 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65  f rows in the re
2c946 73 75 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  sult */.  int nC
2c947 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20  olumn;       /* 
2c948 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
2c949 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
2c94a 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20  */.  int nData; 
2c94b 20 20 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73          /* Slots
2c94c 20 75 73 65 64 20 69 6e 20 61 7a 52 65 73 75 6c   used in azResul
2c94d 74 5b 5d 2e 20 20 28 6e 52 6f 77 2b 31 29 2a 6e  t[].  (nRow+1)*n
2c94e 43 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20  Column */.  int 
2c94f 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  rc;            /
2c950 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72  * Return code fr
2c951 6f 6d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  om sqlite3_exec(
2c952 29 20 2a 2f 0a 7d 20 54 61 62 52 65 73 75 6c 74  ) */.} TabResult
2c953 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  ;../*.** This ro
2c954 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
2c955 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f  once for each ro
2c956 77 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  w in the result 
2c957 74 61 62 6c 65 2e 20 20 49 74 73 20 6a 6f 62 0a  table.  Its job.
2c958 2a 2a 20 69 73 20 74 6f 20 66 69 6c 6c 20 69 6e  ** is to fill in
2c959 20 74 68 65 20 54 61 62 52 65 73 75 6c 74 20 73   the TabResult s
2c95a 74 72 75 63 74 75 72 65 20 61 70 70 72 6f 70 72  tructure appropr
2c95b 69 61 74 65 6c 79 2c 20 61 6c 6c 6f 63 61 74 69  iately, allocati
2c95c 6e 67 20 6e 65 77 0a 2a 2a 20 6d 65 6d 6f 72 79  ng new.** memory
2c95d 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
2c95e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
2c95f 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63  ite3_get_table_c
2c960 62 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 69 6e  b(void *pArg, in
2c961 74 20 6e 43 6f 6c 2c 20 63 68 61 72 20 2a 2a 61  t nCol, char **a
2c962 72 67 76 2c 20 63 68 61 72 20 2a 2a 63 6f 6c 76  rgv, char **colv
2c963 29 7b 0a 20 20 54 61 62 52 65 73 75 6c 74 20 2a  ){.  TabResult *
2c964 70 20 3d 20 28 54 61 62 52 65 73 75 6c 74 2a 29  p = (TabResult*)
2c965 70 41 72 67 3b 20 20 2f 2a 20 52 65 73 75 6c 74  pArg;  /* Result
2c966 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a   accumulator */.
2c967 20 20 69 6e 74 20 6e 65 65 64 3b 20 20 20 20 20    int need;     
2c968 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c969 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 6e 65 65      /* Slots nee
2c96a 64 65 64 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75  ded in p->azResu
2c96b 6c 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  lt[] */.  int i;
2c96c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c96d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2c96e 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
2c96f 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
2c970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c971 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63     /* A single c
2c972 6f 6c 75 6d 6e 20 6f 66 20 72 65 73 75 6c 74 20  olumn of result 
2c973 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  */..  /* Make su
2c974 72 65 20 74 68 65 72 65 20 69 73 20 65 6e 6f 75  re there is enou
2c975 67 68 20 73 70 61 63 65 20 69 6e 20 70 2d 3e 61  gh space in p->a
2c976 7a 52 65 73 75 6c 74 20 74 6f 20 68 6f 6c 64 20  zResult to hold 
2c977 65 76 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20  everything.  ** 
2c978 77 65 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d  we need to remem
2c979 62 65 72 20 66 72 6f 6d 20 74 68 69 73 20 69 6e  ber from this in
2c97a 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  vocation of the 
2c97b 63 61 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20  callback..  */. 
2c97c 20 69 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20   if( p->nRow==0 
2c97d 26 26 20 61 72 67 76 21 3d 30 20 29 7b 0a 20 20  && argv!=0 ){.  
2c97e 20 20 6e 65 65 64 20 3d 20 6e 43 6f 6c 2a 32 3b    need = nCol*2;
2c97f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65  .  }else{.    ne
2c980 65 64 20 3d 20 6e 43 6f 6c 3b 0a 20 20 7d 0a 20  ed = nCol;.  }. 
2c981 20 69 66 28 20 70 2d 3e 6e 44 61 74 61 20 2b 20   if( p->nData + 
2c982 6e 65 65 64 20 3e 20 70 2d 3e 6e 41 6c 6c 6f 63  need > p->nAlloc
2c983 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 61   ){.    char **a
2c984 7a 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c  zNew;.    p->nAl
2c985 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a  loc = p->nAlloc*
2c986 32 20 2b 20 6e 65 65 64 3b 0a 20 20 20 20 61 7a  2 + need;.    az
2c987 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  New = sqlite3_re
2c988 61 6c 6c 6f 63 28 20 70 2d 3e 61 7a 52 65 73 75  alloc( p->azResu
2c989 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  lt, sizeof(char*
2c98a 29 2a 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  )*p->nAlloc );. 
2c98b 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20     if( azNew==0 
2c98c 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61  ) goto malloc_fa
2c98d 69 6c 65 64 3b 0a 20 20 20 20 70 2d 3e 61 7a 52  iled;.    p->azR
2c98e 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20  esult = azNew;. 
2c98f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73   }..  /* If this
2c990 20 69 73 20 74 68 65 20 66 69 72 73 74 20 72 6f   is the first ro
2c991 77 2c 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65  w, then generate
2c992 20 61 6e 20 65 78 74 72 61 20 72 6f 77 20 63 6f   an extra row co
2c993 6e 74 61 69 6e 69 6e 67 0a 20 20 2a 2a 20 74 68  ntaining.  ** th
2c994 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63  e names of all c
2c995 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  olumns..  */.  i
2c996 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29 7b  f( p->nRow==0 ){
2c997 0a 20 20 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20  .    p->nColumn 
2c998 3d 20 6e 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28  = nCol;.    for(
2c999 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
2c99a 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  ){.      z = sql
2c99b 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
2c99c 22 2c 20 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20 20  ", colv[i]);.   
2c99d 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f     if( z==0 ) go
2c99e 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64  to malloc_failed
2c99f 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73  ;.      p->azRes
2c9a0 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20  ult[p->nData++] 
2c9a1 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  = z;.    }.  }el
2c9a2 73 65 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 75 6d  se if( p->nColum
2c9a3 6e 21 3d 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 73  n!=nCol ){.    s
2c9a4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a  qlite3_free(p->z
2c9a5 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e  ErrMsg);.    p->
2c9a6 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
2c9a7 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20  3_mprintf(.     
2c9a8 20 20 22 73 71 6c 69 74 65 33 5f 67 65 74 5f 74    "sqlite3_get_t
2c9a9 61 62 6c 65 28 29 20 63 61 6c 6c 65 64 20 77 69  able() called wi
2c9aa 74 68 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 69  th two or more i
2c9ab 6e 63 6f 6d 70 61 74 69 62 6c 65 20 71 75 65 72  ncompatible quer
2c9ac 69 65 73 22 0a 20 20 20 20 29 3b 0a 20 20 20 20  ies".    );.    
2c9ad 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  p->rc = SQLITE_E
2c9ae 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e  RROR;.    return
2c9af 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f   1;.  }..  /* Co
2c9b0 70 79 20 6f 76 65 72 20 74 68 65 20 72 6f 77 20  py over the row 
2c9b1 64 61 74 61 0a 20 20 2a 2f 0a 20 20 69 66 28 20  data.  */.  if( 
2c9b2 61 72 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 66  argv!=0 ){.    f
2c9b3 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
2c9b4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
2c9b5 61 72 67 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20  argv[i]==0 ){.  
2c9b6 20 20 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20        z = 0;.   
2c9b7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2c9b8 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
2c9b9 33 53 74 72 6c 65 6e 33 30 28 61 72 67 76 5b 69  3Strlen30(argv[i
2c9ba 5d 29 2b 31 3b 0a 20 20 20 20 20 20 20 20 7a 20  ])+1;.        z 
2c9bb 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
2c9bc 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ( n );.        i
2c9bd 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d  f( z==0 ) goto m
2c9be 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20  alloc_failed;.  
2c9bf 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20        memcpy(z, 
2c9c0 61 72 67 76 5b 69 5d 2c 20 6e 29 3b 0a 20 20 20  argv[i], n);.   
2c9c1 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 61 7a     }.      p->az
2c9c2 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b  Result[p->nData+
2c9c3 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20  +] = z;.    }.  
2c9c4 20 20 70 2d 3e 6e 52 6f 77 2b 2b 3b 0a 20 20 7d    p->nRow++;.  }
2c9c5 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6d 61  .  return 0;..ma
2c9c6 6c 6c 6f 63 5f 66 61 69 6c 65 64 3a 0a 20 20 70  lloc_failed:.  p
2c9c7 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
2c9c8 4d 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  MEM;.  return 1;
2c9c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  .}../*.** Query 
2c9ca 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 42  the database.  B
2c9cb 75 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e  ut instead of in
2c9cc 76 6f 6b 69 6e 67 20 61 20 63 61 6c 6c 62 61 63  voking a callbac
2c9cd 6b 20 66 6f 72 20 65 61 63 68 20 72 6f 77 2c 0a  k for each row,.
2c9ce 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66 6f 72 20  ** malloc() for 
2c9cf 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  space to hold th
2c9d0 65 20 72 65 73 75 6c 74 20 61 6e 64 20 72 65 74  e result and ret
2c9d1 75 72 6e 20 74 68 65 20 65 6e 74 69 72 65 20 72  urn the entire r
2c9d2 65 73 75 6c 74 73 0a 2a 2a 20 61 74 20 74 68 65  esults.** at the
2c9d3 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74   conclusion of t
2c9d4 68 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  he call..**.** T
2c9d5 68 65 20 72 65 73 75 6c 74 20 74 68 61 74 20 69  he result that i
2c9d6 73 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 2a 2a  s written to ***
2c9d7 70 61 7a 52 65 73 75 6c 74 20 69 73 20 68 65 6c  pazResult is hel
2c9d8 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  d in memory obta
2c9d9 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c  ined.** from mal
2c9da 6c 6f 63 28 29 2e 20 20 42 75 74 20 74 68 65 20  loc().  But the 
2c9db 63 61 6c 6c 65 72 20 63 61 6e 6e 6f 74 20 66 72  caller cannot fr
2c9dc 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 64  ee this memory d
2c9dd 69 72 65 63 74 6c 79 2e 20 20 0a 2a 2a 20 49 6e  irectly.  .** In
2c9de 73 74 65 61 64 2c 20 74 68 65 20 65 6e 74 69 72  stead, the entir
2c9df 65 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 62  e table should b
2c9e0 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  e passed to sqli
2c9e1 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
2c9e2 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c   when.** the cal
2c9e3 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69  ling procedure i
2c9e4 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67  s finished using
2c9e5 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41   it..*/.SQLITE_A
2c9e6 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  PI int sqlite3_g
2c9e7 65 74 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69  et_table(.  sqli
2c9e8 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2c9e9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
2c9ea 61 74 61 62 61 73 65 20 6f 6e 20 77 68 69 63 68  atabase on which
2c9eb 20 74 68 65 20 53 51 4c 20 65 78 65 63 75 74 65   the SQL execute
2c9ec 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
2c9ed 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
2c9ee 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f     /* The SQL to
2c9ef 20 62 65 20 65 78 65 63 75 74 65 64 20 2a 2f 0a   be executed */.
2c9f0 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73    char ***pazRes
2c9f1 75 6c 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ult,          /*
2c9f2 20 57 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   Write the resul
2c9f3 74 20 74 61 62 6c 65 20 68 65 72 65 20 2a 2f 0a  t table here */.
2c9f4 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20    int *pnRow,   
2c9f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c9f6 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65   Write the numbe
2c9f7 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  r of rows in the
2c9f8 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a   result here */.
2c9f9 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c    int *pnColumn,
2c9fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c9fb 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65   Write the numbe
2c9fc 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  r of columns of 
2c9fd 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20  result here */. 
2c9fe 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
2c9ff 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ca00 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73  Write error mess
2ca01 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ages here */.){.
2ca02 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 61 62 52    int rc;.  TabR
2ca03 65 73 75 6c 74 20 72 65 73 3b 0a 0a 20 20 2a 70  esult res;..  *p
2ca04 61 7a 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20  azResult = 0;.  
2ca05 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20 29 20 2a  if( pnColumn ) *
2ca06 70 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20  pnColumn = 0;.  
2ca07 69 66 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52  if( pnRow ) *pnR
2ca08 6f 77 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 7a  ow = 0;.  if( pz
2ca09 45 72 72 4d 73 67 20 29 20 2a 70 7a 45 72 72 4d  ErrMsg ) *pzErrM
2ca0a 73 67 20 3d 20 30 3b 0a 20 20 72 65 73 2e 7a 45  sg = 0;.  res.zE
2ca0b 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 72 65 73  rrMsg = 0;.  res
2ca0c 2e 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 72 65 73  .nRow = 0;.  res
2ca0d 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20  .nColumn = 0;.  
2ca0e 72 65 73 2e 6e 44 61 74 61 20 3d 20 31 3b 0a 20  res.nData = 1;. 
2ca0f 20 72 65 73 2e 6e 41 6c 6c 6f 63 20 3d 20 32 30   res.nAlloc = 20
2ca10 3b 0a 20 20 72 65 73 2e 72 63 20 3d 20 53 51 4c  ;.  res.rc = SQL
2ca11 49 54 45 5f 4f 4b 3b 0a 20 20 72 65 73 2e 61 7a  ITE_OK;.  res.az
2ca12 52 65 73 75 6c 74 20 3d 20 73 71 6c 69 74 65 33  Result = sqlite3
2ca13 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 63  _malloc(sizeof(c
2ca14 68 61 72 2a 29 2a 72 65 73 2e 6e 41 6c 6c 6f 63  har*)*res.nAlloc
2ca15 20 29 3b 0a 20 20 69 66 28 20 72 65 73 2e 61 7a   );.  if( res.az
2ca16 52 65 73 75 6c 74 3d 3d 30 20 29 7b 0a 20 20 20  Result==0 ){.   
2ca17 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20    db->errCode = 
2ca18 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2ca19 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2ca1a 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65  _NOMEM;.  }.  re
2ca1b 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20  s.azResult[0] = 
2ca1c 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  0;.  rc = sqlite
2ca1d 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c  3_exec(db, zSql,
2ca1e 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
2ca1f 6c 65 5f 63 62 2c 20 26 72 65 73 2c 20 70 7a 45  le_cb, &res, pzE
2ca20 72 72 4d 73 67 29 3b 0a 20 20 61 73 73 65 72 74  rrMsg);.  assert
2ca21 28 20 73 69 7a 65 6f 66 28 72 65 73 2e 61 7a 52  ( sizeof(res.azR
2ca22 65 73 75 6c 74 5b 30 5d 29 3e 3d 20 73 69 7a 65  esult[0])>= size
2ca23 6f 66 28 72 65 73 2e 6e 44 61 74 61 29 20 29 3b  of(res.nData) );
2ca24 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b  .  res.azResult[
2ca25 30 5d 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 5f  0] = SQLITE_INT_
2ca26 54 4f 5f 50 54 52 28 72 65 73 2e 6e 44 61 74 61  TO_PTR(res.nData
2ca27 29 3b 0a 20 20 69 66 28 20 28 72 63 26 30 78 66  );.  if( (rc&0xf
2ca28 66 29 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54  f)==SQLITE_ABORT
2ca29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2ca2a 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73 2e  free_table(&res.
2ca2b 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20  azResult[1]);.  
2ca2c 20 20 69 66 28 20 72 65 73 2e 7a 45 72 72 4d 73    if( res.zErrMs
2ca2d 67 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  g ){.      if( p
2ca2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
2ca2f 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2ca30 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  *pzErrMsg);.    
2ca31 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
2ca32 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
2ca33 22 25 73 22 2c 72 65 73 2e 7a 45 72 72 4d 73 67  "%s",res.zErrMsg
2ca34 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2ca35 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65   sqlite3_free(re
2ca36 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  s.zErrMsg);.    
2ca37 7d 0a 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64  }.    db->errCod
2ca38 65 20 3d 20 72 65 73 2e 72 63 3b 20 20 2f 2a 20  e = res.rc;  /* 
2ca39 41 73 73 75 6d 65 20 33 32 2d 62 69 74 20 61 73  Assume 32-bit as
2ca3a 73 69 67 6e 6d 65 6e 74 20 69 73 20 61 74 6f 6d  signment is atom
2ca3b 69 63 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  ic */.    return
2ca3c 20 72 65 73 2e 72 63 3b 0a 20 20 7d 0a 20 20 73   res.rc;.  }.  s
2ca3d 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65 73 2e  qlite3_free(res.
2ca3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20  zErrMsg);.  if( 
2ca3f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2ca40 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
2ca41 65 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52  e_table(&res.azR
2ca42 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 72  esult[1]);.    r
2ca43 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
2ca44 69 66 28 20 72 65 73 2e 6e 41 6c 6c 6f 63 3e 72  if( res.nAlloc>r
2ca45 65 73 2e 6e 44 61 74 61 20 29 7b 0a 20 20 20 20  es.nData ){.    
2ca46 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20  char **azNew;.  
2ca47 20 20 61 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65    azNew = sqlite
2ca48 33 5f 72 65 61 6c 6c 6f 63 28 20 72 65 73 2e 61  3_realloc( res.a
2ca49 7a 52 65 73 75 6c 74 2c 20 73 69 7a 65 6f 66 28  zResult, sizeof(
2ca4a 63 68 61 72 2a 29 2a 72 65 73 2e 6e 44 61 74 61  char*)*res.nData
2ca4b 20 29 3b 0a 20 20 20 20 69 66 28 20 61 7a 4e 65   );.    if( azNe
2ca4c 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  w==0 ){.      sq
2ca4d 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
2ca4e 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31  (&res.azResult[1
2ca4f 5d 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 65 72  ]);.      db->er
2ca50 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e  rCode = SQLITE_N
2ca51 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 72 65 74 75  OMEM;.      retu
2ca52 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2ca53 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 73 2e 61  .    }.    res.a
2ca54 7a 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b  zResult = azNew;
2ca55 0a 20 20 7d 0a 20 20 2a 70 61 7a 52 65 73 75 6c  .  }.  *pazResul
2ca56 74 20 3d 20 26 72 65 73 2e 61 7a 52 65 73 75 6c  t = &res.azResul
2ca57 74 5b 31 5d 3b 0a 20 20 69 66 28 20 70 6e 43 6f  t[1];.  if( pnCo
2ca58 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e  lumn ) *pnColumn
2ca59 20 3d 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a   = res.nColumn;.
2ca5a 20 20 69 66 28 20 70 6e 52 6f 77 20 29 20 2a 70    if( pnRow ) *p
2ca5b 6e 52 6f 77 20 3d 20 72 65 73 2e 6e 52 6f 77 3b  nRow = res.nRow;
2ca5c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2ca5d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2ca5e 69 6e 65 20 66 72 65 65 73 20 74 68 65 20 73 70  ine frees the sp
2ca5f 61 63 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ace the sqlite3_
2ca60 67 65 74 5f 74 61 62 6c 65 28 29 20 6d 61 6c 6c  get_table() mall
2ca61 6f 63 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oced..*/.SQLITE_
2ca62 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2ca63 5f 66 72 65 65 5f 74 61 62 6c 65 28 0a 20 20 63  _free_table(.  c
2ca64 68 61 72 20 2a 2a 61 7a 52 65 73 75 6c 74 20 20  har **azResult  
2ca65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
2ca66 75 6c 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f  ult returned fro
2ca67 6d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67  m from sqlite3_g
2ca68 65 74 5f 74 61 62 6c 65 28 29 20 2a 2f 0a 29 7b  et_table() */.){
2ca69 0a 20 20 69 66 28 20 61 7a 52 65 73 75 6c 74 20  .  if( azResult 
2ca6a 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e 3b  ){.    int i, n;
2ca6b 0a 20 20 20 20 61 7a 52 65 73 75 6c 74 2d 2d 3b  .    azResult--;
2ca6c 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 7a 52  .    assert( azR
2ca6d 65 73 75 6c 74 21 3d 30 20 29 3b 0a 20 20 20 20  esult!=0 );.    
2ca6e 6e 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54  n = SQLITE_PTR_T
2ca6f 4f 5f 49 4e 54 28 61 7a 52 65 73 75 6c 74 5b 30  O_INT(azResult[0
2ca70 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b  ]);.    for(i=1;
2ca71 20 69 3c 6e 3b 20 69 2b 2b 29 7b 20 69 66 28 20   i<n; i++){ if( 
2ca72 61 7a 52 65 73 75 6c 74 5b 69 5d 20 29 20 73 71  azResult[i] ) sq
2ca73 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 52 65 73  lite3_free(azRes
2ca74 75 6c 74 5b 69 5d 29 3b 20 7d 0a 20 20 20 20 73  ult[i]); }.    s
2ca75 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 52 65  qlite3_free(azRe
2ca76 73 75 6c 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65  sult);.  }.}..#e
2ca77 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2ca78 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 20 2a 2f  MIT_GET_TABLE */
2ca79 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2ca7a 2a 20 45 6e 64 20 6f 66 20 74 61 62 6c 65 2e 63  * End of table.c
2ca7b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2ca7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca7e 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
2ca7f 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 72 69  * Begin file tri
2ca80 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  gger.c *********
2ca81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca83 2f 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  /./*.**.** The a
2ca84 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2ca85 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2ca86 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2ca87 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2ca88 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2ca89 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2ca8a 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2ca8b 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2ca8c 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2ca8d 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2ca8e 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2ca8f 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2ca90 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2ca91 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2ca92 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2ca93 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2ca94 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2ca95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca99 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
2ca9a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d   contains the im
2ca9b 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
2ca9c 20 54 52 49 47 47 45 52 73 0a 2a 2f 0a 0a 23 69   TRIGGERs.*/..#i
2ca9d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2ca9e 54 5f 54 52 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20  T_TRIGGER./*.** 
2ca9f 44 65 6c 65 74 65 20 61 20 6c 69 6e 6b 65 64 20  Delete a linked 
2caa0 6c 69 73 74 20 6f 66 20 54 72 69 67 67 65 72 53  list of TriggerS
2caa1 74 65 70 20 73 74 72 75 63 74 75 72 65 73 2e 0a  tep structures..
2caa2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2caa3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
2caa4 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
2caa5 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69  sqlite3 *db, Tri
2caa6 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
2caa7 65 72 53 74 65 70 29 7b 0a 20 20 77 68 69 6c 65  erStep){.  while
2caa8 28 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29  ( pTriggerStep )
2caa9 7b 0a 20 20 20 20 54 72 69 67 67 65 72 53 74 65  {.    TriggerSte
2caaa 70 20 2a 20 70 54 6d 70 20 3d 20 70 54 72 69 67  p * pTmp = pTrig
2caab 67 65 72 53 74 65 70 3b 0a 20 20 20 20 70 54 72  gerStep;.    pTr
2caac 69 67 67 65 72 53 74 65 70 20 3d 20 70 54 72 69  iggerStep = pTri
2caad 67 67 65 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b  ggerStep->pNext;
2caae 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ..    sqlite3Exp
2caaf 72 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70  rDelete(db, pTmp
2cab0 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73  ->pWhere);.    s
2cab1 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2cab2 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70  lete(db, pTmp->p
2cab3 45 78 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 73  ExprList);.    s
2cab4 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2cab5 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 53 65  te(db, pTmp->pSe
2cab6 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  lect);.    sqlit
2cab7 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
2cab8 62 2c 20 70 54 6d 70 2d 3e 70 49 64 4c 69 73 74  b, pTmp->pIdList
2cab9 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 44  );..    sqlite3D
2caba 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70 29 3b  bFree(db, pTmp);
2cabb 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  .  }.}../*.** Gi
2cabc 76 65 6e 20 74 61 62 6c 65 20 70 54 61 62 2c 20  ven table pTab, 
2cabd 72 65 74 75 72 6e 20 61 20 6c 69 73 74 20 6f 66  return a list of
2cabe 20 61 6c 6c 20 74 68 65 20 74 72 69 67 67 65 72   all the trigger
2cabf 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 0a 2a  s attached to .*
2cac0 2a 20 74 68 65 20 74 61 62 6c 65 2e 20 54 68 65  * the table. The
2cac1 20 6c 69 73 74 20 69 73 20 63 6f 6e 6e 65 63 74   list is connect
2cac2 65 64 20 62 79 20 54 72 69 67 67 65 72 2e 70 4e  ed by Trigger.pN
2cac3 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2a  ext pointers..**
2cac4 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 74  .** All of the t
2cac5 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 20  riggers on pTab 
2cac6 74 68 61 74 20 61 72 65 20 69 6e 20 74 68 65 20  that are in the 
2cac7 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 61 73  same database as
2cac8 20 70 54 61 62 0a 2a 2a 20 61 72 65 20 61 6c 72   pTab.** are alr
2cac9 65 61 64 79 20 61 74 74 61 63 68 65 64 20 74 6f  eady attached to
2caca 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 2e   pTab->pTrigger.
2cacb 20 20 42 75 74 20 74 68 65 72 65 20 6d 69 67 68    But there migh
2cacc 74 20 62 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a  t be additional.
2cacd 2a 2a 20 74 72 69 67 67 65 72 73 20 6f 6e 20 70  ** triggers on p
2cace 54 61 62 20 69 6e 20 74 68 65 20 54 45 4d 50 20  Tab in the TEMP 
2cacf 73 63 68 65 6d 61 2e 20 20 54 68 69 73 20 72 6f  schema.  This ro
2cad0 75 74 69 6e 65 20 70 72 65 70 65 6e 64 73 20 61  utine prepends a
2cad1 6c 6c 0a 2a 2a 20 54 45 4d 50 20 74 72 69 67 67  ll.** TEMP trigg
2cad2 65 72 73 20 6f 6e 20 70 54 61 62 20 74 6f 20 74  ers on pTab to t
2cad3 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
2cad4 74 68 65 20 70 54 61 62 2d 3e 70 54 72 69 67 67  the pTab->pTrigg
2cad5 65 72 20 6c 69 73 74 0a 2a 2a 20 61 6e 64 20 72  er list.** and r
2cad6 65 74 75 72 6e 73 20 74 68 65 20 63 6f 6d 62 69  eturns the combi
2cad7 6e 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ned list..**.** 
2cad8 54 6f 20 73 74 61 74 65 20 69 74 20 61 6e 6f 74  To state it anot
2cad9 68 65 72 20 77 61 79 3a 20 20 54 68 69 73 20 72  her way:  This r
2cada 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
2cadb 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74 72 69   list of all tri
2cadc 67 67 65 72 73 0a 2a 2a 20 74 68 61 74 20 66 69  ggers.** that fi
2cadd 72 65 20 6f 66 66 20 6f 66 20 70 54 61 62 2e 20  re off of pTab. 
2cade 20 54 68 65 20 6c 69 73 74 20 77 69 6c 6c 20 69   The list will i
2cadf 6e 63 6c 75 64 65 20 61 6e 79 20 54 45 4d 50 20  nclude any TEMP 
2cae0 74 72 69 67 67 65 72 73 20 6f 6e 0a 2a 2a 20 70  triggers on.** p
2cae1 54 61 62 20 61 73 20 77 65 6c 6c 20 61 73 20 74  Tab as well as t
2cae2 68 65 20 74 72 69 67 67 65 72 73 20 6c 69 73 65  he triggers lise
2cae3 64 20 69 6e 20 70 54 61 62 2d 3e 70 54 72 69 67  d in pTab->pTrig
2cae4 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ger..*/.SQLITE_P
2cae5 52 49 56 41 54 45 20 54 72 69 67 67 65 72 20 2a  RIVATE Trigger *
2cae6 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69  sqlite3TriggerLi
2cae7 73 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  st(Parse *pParse
2cae8 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
2cae9 20 20 53 63 68 65 6d 61 20 2a 20 63 6f 6e 73 74    Schema * const
2caea 20 70 54 6d 70 53 63 68 65 6d 61 20 3d 20 70 50   pTmpSchema = pP
2caeb 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d  arse->db->aDb[1]
2caec 2e 70 53 63 68 65 6d 61 3b 0a 20 20 54 72 69 67  .pSchema;.  Trig
2caed 67 65 72 20 2a 70 4c 69 73 74 20 3d 20 30 3b 20  ger *pList = 0; 
2caee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2caef 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67   /* List of trig
2caf0 67 65 72 73 20 74 6f 20 72 65 74 75 72 6e 20 2a  gers to return *
2caf1 2f 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  /..  if( pParse-
2caf2 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72 73  >disableTriggers
2caf3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2caf4 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 54 6d  ;.  }..  if( pTm
2caf5 70 53 63 68 65 6d 61 21 3d 70 54 61 62 2d 3e 70  pSchema!=pTab->p
2caf6 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 48 61  Schema ){.    Ha
2caf7 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66  shElem *p;.    f
2caf8 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46  or(p=sqliteHashF
2caf9 69 72 73 74 28 26 70 54 6d 70 53 63 68 65 6d 61  irst(&pTmpSchema
2cafa 2d 3e 74 72 69 67 48 61 73 68 29 3b 20 70 3b 20  ->trigHash); p; 
2cafb 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  p=sqliteHashNext
2cafc 28 70 29 29 7b 0a 20 20 20 20 20 20 54 72 69 67  (p)){.      Trig
2cafd 67 65 72 20 2a 70 54 72 69 67 20 3d 20 28 54 72  ger *pTrig = (Tr
2cafe 69 67 67 65 72 20 2a 29 73 71 6c 69 74 65 48 61  igger *)sqliteHa
2caff 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20  shData(p);.     
2cb00 20 69 66 28 20 70 54 72 69 67 2d 3e 70 54 61 62   if( pTrig->pTab
2cb01 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
2cb02 63 68 65 6d 61 0a 20 20 20 20 20 20 20 26 26 20  chema.       && 
2cb03 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
2cb04 70 28 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20  p(pTrig->table, 
2cb05 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 0a 20 20  pTab->zName) .  
2cb06 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70      ){.        p
2cb07 54 72 69 67 2d 3e 70 4e 65 78 74 20 3d 20 28 70  Trig->pNext = (p
2cb08 4c 69 73 74 20 3f 20 70 4c 69 73 74 20 3a 20 70  List ? pList : p
2cb09 54 61 62 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a  Tab->pTrigger);.
2cb0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20          pList = 
2cb0b 70 54 72 69 67 3b 0a 20 20 20 20 20 20 7d 0a 20  pTrig;.      }. 
2cb0c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
2cb0d 72 6e 20 28 70 4c 69 73 74 20 3f 20 70 4c 69 73  rn (pList ? pLis
2cb0e 74 20 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67  t : pTab->pTrigg
2cb0f 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  er);.}../*.** Th
2cb10 69 73 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  is is called by 
2cb11 74 68 65 20 70 61 72 73 65 72 20 77 68 65 6e 20  the parser when 
2cb12 69 74 20 73 65 65 73 20 61 20 43 52 45 41 54 45  it sees a CREATE
2cb13 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
2cb14 6e 74 0a 2a 2a 20 75 70 20 74 6f 20 74 68 65 20  nt.** up to the 
2cb15 70 6f 69 6e 74 20 6f 66 20 74 68 65 20 42 45 47  point of the BEG
2cb16 49 4e 20 62 65 66 6f 72 65 20 74 68 65 20 74 72  IN before the tr
2cb17 69 67 67 65 72 20 61 63 74 69 6f 6e 73 2e 20 20  igger actions.  
2cb18 41 20 54 72 69 67 67 65 72 0a 2a 2a 20 73 74 72  A Trigger.** str
2cb19 75 63 74 75 72 65 20 69 73 20 67 65 6e 65 72 61  ucture is genera
2cb1a 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ted based on the
2cb1b 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61   information ava
2cb1c 69 6c 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65  ilable and store
2cb1d 64 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e  d.** in pParse->
2cb1e 70 4e 65 77 54 72 69 67 67 65 72 2e 20 20 41 66  pNewTrigger.  Af
2cb1f 74 65 72 20 74 68 65 20 74 72 69 67 67 65 72 20  ter the trigger 
2cb20 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65  actions have bee
2cb21 6e 20 70 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a  n parsed, the.**
2cb22 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72   sqlite3FinishTr
2cb23 69 67 67 65 72 28 29 20 66 75 6e 63 74 69 6f 6e  igger() function
2cb24 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f   is called to co
2cb25 6d 70 6c 65 74 65 20 74 68 65 20 74 72 69 67 67  mplete the trigg
2cb26 65 72 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69  er.** constructi
2cb27 6f 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53  on process..*/.S
2cb28 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2cb29 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54  id sqlite3BeginT
2cb2a 72 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20  rigger(.  Parse 
2cb2b 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
2cb2c 20 54 68 65 20 70 61 72 73 65 20 63 6f 6e 74 65   The parse conte
2cb2d 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
2cb2e 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
2cb2f 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
2cb30 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54  Name1,      /* T
2cb31 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
2cb32 72 69 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65  rigger */.  Toke
2cb33 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20  n *pName2,      
2cb34 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
2cb35 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  he trigger */.  
2cb36 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20  int tr_tm,      
2cb37 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
2cb38 5f 42 45 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45  _BEFORE, TK_AFTE
2cb39 52 2c 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f  R, TK_INSTEAD */
2cb3a 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
2cb3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
2cb3c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55   TK_INSERT, TK_U
2cb3d 50 44 41 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45  PDATE, TK_DELETE
2cb3e 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43   */.  IdList *pC
2cb3f 6f 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c  olumns,   /* col
2cb40 75 6d 6e 20 6c 69 73 74 20 69 66 20 74 68 69 73  umn list if this
2cb41 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46   is an UPDATE OF
2cb42 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 72   trigger */.  Sr
2cb43 63 4c 69 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d  cList *pTableNam
2cb44 65 2c 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  e,/* The name of
2cb45 20 74 68 65 20 74 61 62 6c 65 2f 76 69 65 77 20   the table/view 
2cb46 74 68 65 20 74 72 69 67 67 65 72 20 61 70 70 6c  the trigger appl
2cb47 69 65 73 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72  ies to */.  Expr
2cb48 20 2a 70 57 68 65 6e 2c 20 20 20 20 20 20 20 20   *pWhen,        
2cb49 2f 2a 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a  /* WHEN clause *
2cb4a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20  /.  int isTemp, 
2cb4b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2cb4c 69 66 20 74 68 65 20 54 45 4d 50 4f 52 41 52 59  if the TEMPORARY
2cb4d 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
2cb4e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45  ent */.  int noE
2cb4f 72 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rr           /* 
2cb50 53 75 70 70 72 65 73 73 20 65 72 72 6f 72 73 20  Suppress errors 
2cb51 69 66 20 74 68 65 20 74 72 69 67 67 65 72 20 61  if the trigger a
2cb52 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f  lready exists */
2cb53 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70  .){.  Trigger *p
2cb54 54 72 69 67 67 65 72 20 3d 20 30 3b 20 20 2f 2a  Trigger = 0;  /*
2cb55 20 54 68 65 20 6e 65 77 20 74 72 69 67 67 65 72   The new trigger
2cb56 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2cb57 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
2cb58 20 54 61 62 6c 65 20 74 68 61 74 20 74 68 65 20   Table that the 
2cb59 74 72 69 67 67 65 72 20 66 69 72 65 73 20 6f 66  trigger fires of
2cb5a 66 20 6f 66 20 2a 2f 0a 20 20 63 68 61 72 20 2a  f of */.  char *
2cb5b 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20  zName = 0;      
2cb5c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2cb5d 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 73 71   trigger */.  sq
2cb5e 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2cb5f 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20  se->db;  /* The 
2cb60 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2cb61 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ion */.  int iDb
2cb62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2cb63 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2cb64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 74 72   to store the tr
2cb65 69 67 67 65 72 20 69 6e 20 2a 2f 0a 20 20 54 6f  igger in */.  To
2cb66 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 20  ken *pName;     
2cb67 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 71        /* The unq
2cb68 75 61 6c 69 66 69 65 64 20 64 62 20 6e 61 6d 65  ualified db name
2cb69 20 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46   */.  DbFixer sF
2cb6a 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ix;           /*
2cb6b 20 53 74 61 74 65 20 76 65 63 74 6f 72 20 66 6f   State vector fo
2cb6c 72 20 74 68 65 20 44 42 20 66 69 78 65 72 20 2a  r the DB fixer *
2cb6d 2f 0a 20 20 69 6e 74 20 69 54 61 62 44 62 3b 20  /.  int iTabDb; 
2cb6e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2cb6f 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
2cb70 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
2cb71 62 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  b */..  assert( 
2cb72 70 4e 61 6d 65 31 21 3d 30 20 29 3b 20 20 20 2f  pName1!=0 );   /
2cb73 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69 67 68  * pName1->z migh
2cb74 74 20 62 65 20 4e 55 4c 4c 2c 20 62 75 74 20 6e  t be NULL, but n
2cb75 6f 74 20 70 4e 61 6d 65 31 20 69 74 73 65 6c 66  ot pName1 itself
2cb76 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4e   */.  assert( pN
2cb77 61 6d 65 32 21 3d 30 20 29 3b 0a 20 20 61 73 73  ame2!=0 );.  ass
2cb78 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45  ert( op==TK_INSE
2cb79 52 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 44  RT || op==TK_UPD
2cb7a 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45  ATE || op==TK_DE
2cb7b 4c 45 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74  LETE );.  assert
2cb7c 28 20 6f 70 3e 30 20 26 26 20 6f 70 3c 30 78 66  ( op>0 && op<0xf
2cb7d 66 20 29 3b 0a 20 20 69 66 28 20 69 73 54 65 6d  f );.  if( isTem
2cb7e 70 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 54  p ){.    /* If T
2cb7f 45 4d 50 20 77 61 73 20 73 70 65 63 69 66 69 65  EMP was specifie
2cb80 64 2c 20 74 68 65 6e 20 74 68 65 20 74 72 69 67  d, then the trig
2cb81 67 65 72 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74  ger name may not
2cb82 20 62 65 20 71 75 61 6c 69 66 69 65 64 2e 20 2a   be qualified. *
2cb83 2f 0a 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32  /.    if( pName2
2cb84 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  ->n>0 ){.      s
2cb85 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2cb86 50 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72  Parse, "temporar
2cb87 79 20 74 72 69 67 67 65 72 20 6d 61 79 20 6e 6f  y trigger may no
2cb88 74 20 68 61 76 65 20 71 75 61 6c 69 66 69 65 64  t have qualified
2cb89 20 6e 61 6d 65 22 29 3b 0a 20 20 20 20 20 20 67   name");.      g
2cb8a 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2cb8b 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  nup;.    }.    i
2cb8c 44 62 20 3d 20 31 3b 0a 20 20 20 20 70 4e 61 6d  Db = 1;.    pNam
2cb8d 65 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 65  e = pName1;.  }e
2cb8e 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75  lse{.    /* Figu
2cb8f 72 65 20 6f 75 74 20 74 68 65 20 64 62 20 74 68  re out the db th
2cb90 61 74 20 74 68 65 20 74 68 65 20 74 72 69 67 67  at the the trigg
2cb91 65 72 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74  er will be creat
2cb92 65 64 20 69 6e 20 2a 2f 0a 20 20 20 20 69 44 62  ed in */.    iDb
2cb93 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72   = sqlite3TwoPar
2cb94 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
2cb95 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70  ame1, pName2, &p
2cb96 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69  Name);.    if( i
2cb97 44 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  Db<0 ){.      go
2cb98 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
2cb99 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  up;.    }.  }.. 
2cb9a 20 2f 2a 20 49 66 20 74 68 65 20 74 72 69 67 67   /* If the trigg
2cb9b 65 72 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75  er name was unqu
2cb9c 61 6c 69 66 69 65 64 2c 20 61 6e 64 20 74 68 65  alified, and the
2cb9d 20 74 61 62 6c 65 20 69 73 20 61 20 74 65 6d 70   table is a temp
2cb9e 20 74 61 62 6c 65 2c 0a 20 20 2a 2a 20 74 68 65   table,.  ** the
2cb9f 6e 20 73 65 74 20 69 44 62 20 74 6f 20 31 20 74  n set iDb to 1 t
2cba0 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 72 69  o create the tri
2cba1 67 67 65 72 20 69 6e 20 74 68 65 20 74 65 6d 70  gger in the temp
2cba2 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a  orary database..
2cba3 20 20 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 53    ** If sqlite3S
2cba4 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 29 20 72  rcListLookup() r
2cba5 65 74 75 72 6e 73 20 30 2c 20 69 6e 64 69 63 61  eturns 0, indica
2cba6 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 64  ting the table d
2cba7 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 78 69  oes not.  ** exi
2cba8 73 74 2c 20 74 68 65 20 65 72 72 6f 72 20 69 73  st, the error is
2cba9 20 63 61 75 67 68 74 20 62 79 20 74 68 65 20 62   caught by the b
2cbaa 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f  lock below..  */
2cbab 0a 20 20 69 66 28 20 21 70 54 61 62 6c 65 4e 61  .  if( !pTableNa
2cbac 6d 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  me || db->malloc
2cbad 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
2cbae 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
2cbaf 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d  up;.  }.  pTab =
2cbb0 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c   sqlite3SrcListL
2cbb1 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54  ookup(pParse, pT
2cbb2 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28  ableName);.  if(
2cbb3 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26   pName2->n==0 &&
2cbb4 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70   pTab && pTab->p
2cbb5 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b  Schema==db->aDb[
2cbb6 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  1].pSchema ){.  
2cbb7 20 20 69 44 62 20 3d 20 31 3b 0a 20 20 7d 0a 0a    iDb = 1;.  }..
2cbb8 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20    /* Ensure the 
2cbb9 74 61 62 6c 65 20 6e 61 6d 65 20 6d 61 74 63 68  table name match
2cbba 65 73 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  es database name
2cbbb 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 74 61   and that the ta
2cbbc 62 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  ble exists */.  
2cbbd 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2cbbe 69 6c 65 64 20 29 20 67 6f 74 6f 20 74 72 69 67  iled ) goto trig
2cbbf 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 61  ger_cleanup;.  a
2cbc0 73 73 65 72 74 28 20 70 54 61 62 6c 65 4e 61 6d  ssert( pTableNam
2cbc1 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  e->nSrc==1 );.  
2cbc2 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e  if( sqlite3FixIn
2cbc3 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65  it(&sFix, pParse
2cbc4 2c 20 69 44 62 2c 20 22 74 72 69 67 67 65 72 22  , iDb, "trigger"
2cbc5 2c 20 70 4e 61 6d 65 29 20 26 26 20 0a 20 20 20  , pName) && .   
2cbc6 20 20 20 73 71 6c 69 74 65 33 46 69 78 53 72 63     sqlite3FixSrc
2cbc7 4c 69 73 74 28 26 73 46 69 78 2c 20 70 54 61 62  List(&sFix, pTab
2cbc8 6c 65 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67  leName) ){.    g
2cbc9 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2cbca 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20  nup;.  }.  pTab 
2cbcb 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
2cbcc 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70  Lookup(pParse, p
2cbcd 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66  TableName);.  if
2cbce 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 2f  ( !pTab ){.    /
2cbcf 2a 20 54 68 65 20 74 61 62 6c 65 20 64 6f 65 73  * The table does
2cbd0 20 6e 6f 74 20 65 78 69 73 74 2e 20 2a 2f 0a 20   not exist. */. 
2cbd1 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e     if( db->init.
2cbd2 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  iDb==1 ){.      
2cbd3 2f 2a 20 54 69 63 6b 65 74 20 23 33 38 31 30 2e  /* Ticket #3810.
2cbd4 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 72 6d 61 6c  .      ** Normal
2cbd5 6c 79 2c 20 77 68 65 6e 65 76 65 72 20 61 20 74  ly, whenever a t
2cbd6 61 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 2c  able is dropped,
2cbd7 20 61 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20   all associated 
2cbd8 74 72 69 67 67 65 72 73 20 61 72 65 0a 20 20 20  triggers are.   
2cbd9 20 20 20 2a 2a 20 64 72 6f 70 70 65 64 20 74 6f     ** dropped to
2cbda 6f 2e 20 20 42 75 74 20 69 66 20 61 20 54 45 4d  o.  But if a TEM
2cbdb 50 20 74 72 69 67 67 65 72 20 69 73 20 63 72 65  P trigger is cre
2cbdc 61 74 65 64 20 6f 6e 20 61 20 6e 6f 6e 2d 54 45  ated on a non-TE
2cbdd 4d 50 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a  MP table.      *
2cbde 2a 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20  * and the table 
2cbdf 69 73 20 64 72 6f 70 70 65 64 20 62 79 20 61 20  is dropped by a 
2cbe0 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61  different databa
2cbe1 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74  se connection, t
2cbe2 68 65 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 67  he.      ** trig
2cbe3 67 65 72 20 69 73 20 6e 6f 74 20 76 69 73 69 62  ger is not visib
2cbe4 6c 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  le to the databa
2cbe5 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  se connection th
2cbe6 61 74 20 64 6f 65 73 20 74 68 65 0a 20 20 20 20  at does the.    
2cbe7 20 20 2a 2a 20 64 72 6f 70 20 73 6f 20 74 68 65    ** drop so the
2cbe8 20 74 72 69 67 67 65 72 20 63 61 6e 6e 6f 74 20   trigger cannot 
2cbe9 62 65 20 64 72 6f 70 70 65 64 2e 20 20 54 68 69  be dropped.  Thi
2cbea 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a  s results in an.
2cbeb 20 20 20 20 20 20 2a 2a 20 22 6f 72 70 68 61 6e        ** "orphan
2cbec 65 64 20 74 72 69 67 67 65 72 22 20 2d 20 61 20  ed trigger" - a 
2cbed 74 72 69 67 67 65 72 20 77 68 6f 73 65 20 61 73  trigger whose as
2cbee 73 6f 63 69 61 74 65 64 20 74 61 62 6c 65 20 69  sociated table i
2cbef 73 20 6d 69 73 73 69 6e 67 2e 0a 20 20 20 20 20  s missing..     
2cbf0 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e   */.      db->in
2cbf1 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72  it.orphanTrigger
2cbf2 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
2cbf3 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
2cbf4 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
2cbf5 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
2cbf6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2cbf7 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2cbf8 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 74 72  cannot create tr
2cbf9 69 67 67 65 72 73 20 6f 6e 20 76 69 72 74 75 61  iggers on virtua
2cbfa 6c 20 74 61 62 6c 65 73 22 29 3b 0a 20 20 20 20  l tables");.    
2cbfb 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
2cbfc 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
2cbfd 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 74  Check that the t
2cbfe 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73 20 6e  rigger name is n
2cbff 6f 74 20 72 65 73 65 72 76 65 64 20 61 6e 64 20  ot reserved and 
2cc00 74 68 61 74 20 6e 6f 20 74 72 69 67 67 65 72 20  that no trigger 
2cc01 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 70 65 63  of the.  ** spec
2cc02 69 66 69 65 64 20 6e 61 6d 65 20 65 78 69 73 74  ified name exist
2cc03 73 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73  s */.  zName = s
2cc04 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
2cc05 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
2cc06 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 7c 7c 20    if( !zName || 
2cc07 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
2cc08 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
2cc09 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29  e(pParse, zName)
2cc0a 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69   ){.    goto tri
2cc0b 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
2cc0c 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 48  }.  if( sqlite3H
2cc0d 61 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44  ashFind(&(db->aD
2cc0e 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
2cc0f 74 72 69 67 48 61 73 68 29 2c 0a 20 20 20 20 20  trigHash),.     
2cc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cc11 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53   zName, sqlite3S
2cc12 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 29 20  trlen30(zName)) 
2cc13 29 7b 0a 20 20 20 20 69 66 28 20 21 6e 6f 45 72  ){.    if( !noEr
2cc14 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
2cc15 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2cc16 65 2c 20 22 74 72 69 67 67 65 72 20 25 54 20 61  e, "trigger %T a
2cc17 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c 20  lready exists", 
2cc18 70 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pName);.    }.  
2cc19 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
2cc1a 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
2cc1b 2a 20 44 6f 20 6e 6f 74 20 63 72 65 61 74 65 20  * Do not create 
2cc1c 61 20 74 72 69 67 67 65 72 20 6f 6e 20 61 20 73  a trigger on a s
2cc1d 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 20  ystem table */. 
2cc1e 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
2cc1f 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65  ICmp(pTab->zName
2cc20 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d  , "sqlite_", 7)=
2cc21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
2cc22 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2cc23 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65  , "cannot create
2cc24 20 74 72 69 67 67 65 72 20 6f 6e 20 73 79 73 74   trigger on syst
2cc25 65 6d 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20  em table");.    
2cc26 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a  pParse->nErr++;.
2cc27 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
2cc28 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
2cc29 20 2f 2a 20 49 4e 53 54 45 41 44 20 6f 66 20 74   /* INSTEAD of t
2cc2a 72 69 67 67 65 72 73 20 61 72 65 20 6f 6e 6c 79  riggers are only
2cc2b 20 66 6f 72 20 76 69 65 77 73 20 61 6e 64 20 76   for views and v
2cc2c 69 65 77 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72  iews only suppor
2cc2d 74 20 49 4e 53 54 45 41 44 0a 20 20 2a 2a 20 6f  t INSTEAD.  ** o
2cc2e 66 20 74 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f  f triggers..  */
2cc2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
2cc30 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 21 3d 54  lect && tr_tm!=T
2cc31 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20 20 20  K_INSTEAD ){.   
2cc32 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2cc33 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
2cc34 20 63 72 65 61 74 65 20 25 73 20 74 72 69 67 67   create %s trigg
2cc35 65 72 20 6f 6e 20 76 69 65 77 3a 20 25 53 22 2c  er on view: %S",
2cc36 20 0a 20 20 20 20 20 20 20 20 28 74 72 5f 74 6d   .        (tr_tm
2cc37 20 3d 3d 20 54 4b 5f 42 45 46 4f 52 45 29 3f 22   == TK_BEFORE)?"
2cc38 42 45 46 4f 52 45 22 3a 22 41 46 54 45 52 22 2c  BEFORE":"AFTER",
2cc39 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b   pTableName, 0);
2cc3a 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
2cc3b 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
2cc3c 20 69 66 28 20 21 70 54 61 62 2d 3e 70 53 65 6c   if( !pTab->pSel
2cc3d 65 63 74 20 26 26 20 74 72 5f 74 6d 3d 3d 54 4b  ect && tr_tm==TK
2cc3e 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20  _INSTEAD ){.    
2cc3f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2cc40 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
2cc41 63 72 65 61 74 65 20 49 4e 53 54 45 41 44 20 4f  create INSTEAD O
2cc42 46 22 0a 20 20 20 20 20 20 20 20 22 20 74 72 69  F".        " tri
2cc43 67 67 65 72 20 6f 6e 20 74 61 62 6c 65 3a 20 25  gger on table: %
2cc44 53 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20  S", pTableName, 
2cc45 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69  0);.    goto tri
2cc46 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
2cc47 7d 0a 20 20 69 54 61 62 44 62 20 3d 20 73 71 6c  }.  iTabDb = sql
2cc48 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2cc49 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
2cc4a 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ema);..#ifndef S
2cc4b 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2cc4c 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
2cc4d 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49   int code = SQLI
2cc4e 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45  TE_CREATE_TRIGGE
2cc4f 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  R;.    const cha
2cc50 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  r *zDb = db->aDb
2cc51 5b 69 54 61 62 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  [iTabDb].zName;.
2cc52 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2cc53 7a 44 62 54 72 69 67 20 3d 20 69 73 54 65 6d 70  zDbTrig = isTemp
2cc54 20 3f 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e   ? db->aDb[1].zN
2cc55 61 6d 65 20 3a 20 7a 44 62 3b 0a 20 20 20 20 69  ame : zDb;.    i
2cc56 66 28 20 69 54 61 62 44 62 3d 3d 31 20 7c 7c 20  f( iTabDb==1 || 
2cc57 69 73 54 65 6d 70 20 29 20 63 6f 64 65 20 3d 20  isTemp ) code = 
2cc58 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
2cc59 4d 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20  MP_TRIGGER;.    
2cc5a 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
2cc5b 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64  heck(pParse, cod
2cc5c 65 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  e, zName, pTab->
2cc5d 7a 4e 61 6d 65 2c 20 7a 44 62 54 72 69 67 29 20  zName, zDbTrig) 
2cc5e 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72  ){.      goto tr
2cc5f 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
2cc60 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
2cc61 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2cc62 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53  arse, SQLITE_INS
2cc63 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ERT, SCHEMA_TABL
2cc64 45 28 69 54 61 62 44 62 29 2c 30 2c 7a 44 62 29  E(iTabDb),0,zDb)
2cc65 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72  ){.      goto tr
2cc66 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
2cc67 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
2cc68 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 4f 46  .  /* INSTEAD OF
2cc69 20 74 72 69 67 67 65 72 73 20 63 61 6e 20 6f 6e   triggers can on
2cc6a 6c 79 20 61 70 70 65 61 72 20 6f 6e 20 76 69 65  ly appear on vie
2cc6b 77 73 20 61 6e 64 20 42 45 46 4f 52 45 20 74 72  ws and BEFORE tr
2cc6c 69 67 67 65 72 73 0a 20 20 2a 2a 20 63 61 6e 6e  iggers.  ** cann
2cc6d 6f 74 20 61 70 70 65 61 72 20 6f 6e 20 76 69 65  ot appear on vie
2cc6e 77 73 2e 20 20 53 6f 20 77 65 20 6d 69 67 68 74  ws.  So we might
2cc6f 20 61 73 20 77 65 6c 6c 20 74 72 61 6e 73 6c 61   as well transla
2cc70 74 65 20 65 76 65 72 79 0a 20 20 2a 2a 20 49 4e  te every.  ** IN
2cc71 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72  STEAD OF trigger
2cc72 20 69 6e 74 6f 20 61 20 42 45 46 4f 52 45 20 74   into a BEFORE t
2cc73 72 69 67 67 65 72 2e 20 20 49 74 20 73 69 6d 70  rigger.  It simp
2cc74 6c 69 66 69 65 73 20 63 6f 64 65 0a 20 20 2a 2a  lifies code.  **
2cc75 20 65 6c 73 65 77 68 65 72 65 2e 0a 20 20 2a 2f   elsewhere..  */
2cc76 0a 20 20 69 66 20 28 74 72 5f 74 6d 20 3d 3d 20  .  if (tr_tm == 
2cc77 54 4b 5f 49 4e 53 54 45 41 44 29 7b 0a 20 20 20  TK_INSTEAD){.   
2cc78 20 74 72 5f 74 6d 20 3d 20 54 4b 5f 42 45 46 4f   tr_tm = TK_BEFO
2cc79 52 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 75  RE;.  }..  /* Bu
2cc7a 69 6c 64 20 74 68 65 20 54 72 69 67 67 65 72 20  ild the Trigger 
2cc7b 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 70 54 72 69  object */.  pTri
2cc7c 67 67 65 72 20 3d 20 28 54 72 69 67 67 65 72 2a  gger = (Trigger*
2cc7d 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  )sqlite3DbMalloc
2cc7e 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
2cc7f 54 72 69 67 67 65 72 29 29 3b 0a 20 20 69 66 28  Trigger));.  if(
2cc80 20 70 54 72 69 67 67 65 72 3d 3d 30 20 29 20 67   pTrigger==0 ) g
2cc81 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2cc82 6e 75 70 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  nup;.  pTrigger-
2cc83 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a  >zName = zName;.
2cc84 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 70    zName = 0;.  p
2cc85 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 20 3d  Trigger->table =
2cc86 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
2cc87 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2d  (db, pTableName-
2cc88 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  >a[0].zName);.  
2cc89 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d  pTrigger->pSchem
2cc8a 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
2cc8b 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 72 69  .pSchema;.  pTri
2cc8c 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61  gger->pTabSchema
2cc8d 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
2cc8e 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 6f 70  ;.  pTrigger->op
2cc8f 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70 54 72   = (u8)op;.  pTr
2cc90 69 67 67 65 72 2d 3e 74 72 5f 74 6d 20 3d 20 74  igger->tr_tm = t
2cc91 72 5f 74 6d 3d 3d 54 4b 5f 42 45 46 4f 52 45 20  r_tm==TK_BEFORE 
2cc92 3f 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  ? TRIGGER_BEFORE
2cc93 20 3a 20 54 52 49 47 47 45 52 5f 41 46 54 45 52   : TRIGGER_AFTER
2cc94 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 57  ;.  pTrigger->pW
2cc95 68 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70  hen = sqlite3Exp
2cc96 72 44 75 70 28 64 62 2c 20 70 57 68 65 6e 2c 20  rDup(db, pWhen, 
2cc97 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b  EXPRDUP_REDUCE);
2cc98 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f  .  pTrigger->pCo
2cc99 6c 75 6d 6e 73 20 3d 20 73 71 6c 69 74 65 33 49  lumns = sqlite3I
2cc9a 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 43 6f  dListDup(db, pCo
2cc9b 6c 75 6d 6e 73 29 3b 0a 20 20 61 73 73 65 72 74  lumns);.  assert
2cc9c 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72  ( pParse->pNewTr
2cc9d 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 70 50  igger==0 );.  pP
2cc9e 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
2cc9f 72 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 0a 74  r = pTrigger;..t
2cca0 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a 0a  rigger_cleanup:.
2cca1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2cca2 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71  db, zName);.  sq
2cca3 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
2cca4 74 65 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d  te(db, pTableNam
2cca5 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c  e);.  sqlite3IdL
2cca6 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43  istDelete(db, pC
2cca7 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74  olumns);.  sqlit
2cca8 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2cca9 20 70 57 68 65 6e 29 3b 0a 20 20 69 66 28 20 21   pWhen);.  if( !
2ccaa 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
2ccab 67 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ger ){.    sqlit
2ccac 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  e3DeleteTrigger(
2ccad 64 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20  db, pTrigger);. 
2ccae 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
2ccaf 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  rt( pParse->pNew
2ccb0 54 72 69 67 67 65 72 3d 3d 70 54 72 69 67 67 65  Trigger==pTrigge
2ccb1 72 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  r );.  }.}../*.*
2ccb2 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2ccb3 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
2ccb4 6c 6c 20 6f 66 20 74 68 65 20 74 72 69 67 67 65  ll of the trigge
2ccb5 72 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62  r actions have b
2ccb6 65 65 6e 20 70 61 72 73 65 64 0a 2a 2a 20 69 6e  een parsed.** in
2ccb7 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65   order to comple
2ccb8 74 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f  te the process o
2ccb9 66 20 62 75 69 6c 64 69 6e 67 20 74 68 65 20 74  f building the t
2ccba 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  rigger..*/.SQLIT
2ccbb 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2ccbc 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
2ccbd 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ger(.  Parse *pP
2ccbe 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
2ccbf 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
2ccc0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
2ccc1 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 2f 2a  p *pStepList, /*
2ccc2 20 54 68 65 20 74 72 69 67 67 65 72 65 64 20 70   The triggered p
2ccc3 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65  rogram */.  Toke
2ccc4 6e 20 2a 70 41 6c 6c 20 20 20 20 20 20 20 20 20  n *pAll         
2ccc5 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 68 61      /* Token tha
2ccc6 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  t describes the 
2ccc7 63 6f 6d 70 6c 65 74 65 20 43 52 45 41 54 45 20  complete CREATE 
2ccc8 54 52 49 47 47 45 52 20 2a 2f 0a 29 7b 0a 20 20  TRIGGER */.){.  
2ccc9 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d  Trigger *pTrig =
2ccca 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
2cccb 67 67 65 72 3b 20 20 20 20 2f 2a 20 54 72 69 67  gger;    /* Trig
2cccc 67 65 72 20 62 65 69 6e 67 20 66 69 6e 69 73 68  ger being finish
2cccd 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  ed */.  char *zN
2ccce 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
2cccf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ccd0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 72 69 67   /* Name of trig
2ccd1 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ger */.  sqlite3
2ccd2 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2ccd3 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
2ccd4 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2ccd5 65 20 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73  e */.  DbFixer s
2ccd6 46 69 78 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20  Fix;.  int iDb; 
2ccd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ccd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ccd9 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74  /* Database cont
2ccda 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67  aining the trigg
2ccdb 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e 61  er */.  Token na
2ccdc 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20  meToken;        
2ccdd 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 6e 61     /* Trigger na
2ccde 6d 65 20 66 6f 72 20 65 72 72 6f 72 20 72 65 70  me for error rep
2ccdf 6f 72 74 69 6e 67 20 2a 2f 0a 0a 20 20 70 54 72  orting */..  pTr
2cce0 69 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ig = pParse->pNe
2cce1 77 54 72 69 67 67 65 72 3b 0a 20 20 70 50 61 72  wTrigger;.  pPar
2cce2 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20  se->pNewTrigger 
2cce3 3d 20 30 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  = 0;.  if( NEVER
2cce4 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 29 20 7c  (pParse->nErr) |
2cce5 7c 20 21 70 54 72 69 67 20 29 20 67 6f 74 6f 20  | !pTrig ) goto 
2cce6 74 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c  triggerfinish_cl
2cce7 65 61 6e 75 70 3b 0a 20 20 7a 4e 61 6d 65 20 3d  eanup;.  zName =
2cce8 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a 20   pTrig->zName;. 
2cce9 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
2ccea 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
2cceb 73 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70  se->db, pTrig->p
2ccec 53 63 68 65 6d 61 29 3b 0a 20 20 70 54 72 69 67  Schema);.  pTrig
2cced 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d 20 70 53  ->step_list = pS
2ccee 74 65 70 4c 69 73 74 3b 0a 20 20 77 68 69 6c 65  tepList;.  while
2ccef 28 20 70 53 74 65 70 4c 69 73 74 20 29 7b 0a 20  ( pStepList ){. 
2ccf0 20 20 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 54     pStepList->pT
2ccf1 72 69 67 20 3d 20 70 54 72 69 67 3b 0a 20 20 20  rig = pTrig;.   
2ccf2 20 70 53 74 65 70 4c 69 73 74 20 3d 20 70 53 74   pStepList = pSt
2ccf3 65 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20  epList->pNext;. 
2ccf4 20 7d 0a 20 20 6e 61 6d 65 54 6f 6b 65 6e 2e 7a   }.  nameToken.z
2ccf5 20 3d 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 3b   = pTrig->zName;
2ccf6 0a 20 20 6e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d  .  nameToken.n =
2ccf7 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2ccf8 28 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 3b 0a 20  (nameToken.z);. 
2ccf9 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49   if( sqlite3FixI
2ccfa 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73  nit(&sFix, pPars
2ccfb 65 2c 20 69 44 62 2c 20 22 74 72 69 67 67 65 72  e, iDb, "trigger
2ccfc 22 2c 20 26 6e 61 6d 65 54 6f 6b 65 6e 29 20 0a  ", &nameToken) .
2ccfd 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c            && sql
2ccfe 69 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74  ite3FixTriggerSt
2ccff 65 70 28 26 73 46 69 78 2c 20 70 54 72 69 67 2d  ep(&sFix, pTrig-
2cd00 3e 73 74 65 70 5f 6c 69 73 74 29 20 29 7b 0a 20  >step_list) ){. 
2cd01 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 66     goto triggerf
2cd02 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20  inish_cleanup;. 
2cd03 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61   }..  /* if we a
2cd04 72 65 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a  re not initializ
2cd05 69 6e 67 2c 20 61 6e 64 20 74 68 69 73 20 74 72  ing, and this tr
2cd06 69 67 67 65 72 20 69 73 20 6e 6f 74 20 6f 6e 20  igger is not on 
2cd07 61 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 0a 20  a TEMP table, . 
2cd08 20 2a 2a 20 62 75 69 6c 64 20 74 68 65 20 73 71   ** build the sq
2cd09 6c 69 74 65 5f 6d 61 73 74 65 72 20 65 6e 74 72  lite_master entr
2cd0a 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  y.  */.  if( !db
2cd0b 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2cd0c 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20     Vdbe *v;.    
2cd0d 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 2f 2a  char *z;..    /*
2cd0e 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 20 69   Make an entry i
2cd0f 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
2cd10 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  ter table */.   
2cd11 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2cd12 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
2cd13 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f   if( v==0 ) goto
2cd14 20 74 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63   triggerfinish_c
2cd15 6c 65 61 6e 75 70 3b 0a 20 20 20 20 73 71 6c 69  leanup;.    sqli
2cd16 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
2cd17 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
2cd18 2c 20 69 44 62 29 3b 0a 20 20 20 20 7a 20 3d 20  , iDb);.    z = 
2cd19 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
2cd1a 28 64 62 2c 20 28 63 68 61 72 2a 29 70 41 6c 6c  (db, (char*)pAll
2cd1b 2d 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20  ->z, pAll->n);. 
2cd1c 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
2cd1d 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
2cd1e 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54       "INSERT INT
2cd1f 4f 20 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27  O %Q.%s VALUES('
2cd20 74 72 69 67 67 65 72 27 2c 25 51 2c 25 51 2c 30  trigger',%Q,%Q,0
2cd21 2c 27 43 52 45 41 54 45 20 54 52 49 47 47 45 52  ,'CREATE TRIGGER
2cd22 20 25 71 27 29 22 2c 0a 20 20 20 20 20 20 20 64   %q')",.       d
2cd23 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2cd24 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  e, SCHEMA_TABLE(
2cd25 69 44 62 29 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20  iDb), zName,.   
2cd26 20 20 20 20 70 54 72 69 67 2d 3e 74 61 62 6c 65      pTrig->table
2cd27 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , z);.    sqlite
2cd28 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a  3DbFree(db, z);.
2cd29 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
2cd2a 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
2cd2b 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
2cd2c 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2cd2d 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69  P_ParseSchema, i
2cd2e 44 62 2c 20 30 2c 20 30 2c 20 73 71 6c 69 74 65  Db, 0, 0, sqlite
2cd2f 33 4d 50 72 69 6e 74 66 28 0a 20 20 20 20 20 20  3MPrintf(.      
2cd30 20 20 64 62 2c 20 22 74 79 70 65 3d 27 74 72 69    db, "type='tri
2cd31 67 67 65 72 27 20 41 4e 44 20 6e 61 6d 65 3d 27  gger' AND name='
2cd32 25 71 27 22 2c 20 7a 4e 61 6d 65 29 2c 20 50 34  %q'", zName), P4
2cd33 5f 44 59 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a  _DYNAMIC.    );.
2cd34 20 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 69    }..  if( db->i
2cd35 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
2cd36 54 72 69 67 67 65 72 20 2a 70 4c 69 6e 6b 20 3d  Trigger *pLink =
2cd37 20 70 54 72 69 67 3b 0a 20 20 20 20 48 61 73 68   pTrig;.    Hash
2cd38 20 2a 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61   *pHash = &db->a
2cd39 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
2cd3a 3e 74 72 69 67 48 61 73 68 3b 0a 20 20 20 20 70  >trigHash;.    p
2cd3b 54 72 69 67 20 3d 20 73 71 6c 69 74 65 33 48 61  Trig = sqlite3Ha
2cd3c 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20  shInsert(pHash, 
2cd3d 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74  zName, sqlite3St
2cd3e 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 70  rlen30(zName), p
2cd3f 54 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 70  Trig);.    if( p
2cd40 54 72 69 67 20 29 7b 0a 20 20 20 20 20 20 64 62  Trig ){.      db
2cd41 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2cd42 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   1;.    }else if
2cd43 28 20 70 4c 69 6e 6b 2d 3e 70 53 63 68 65 6d 61  ( pLink->pSchema
2cd44 3d 3d 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68  ==pLink->pTabSch
2cd45 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 54 61 62  ema ){.      Tab
2cd46 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20  le *pTab;.      
2cd47 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53  int n = sqlite3S
2cd48 74 72 6c 65 6e 33 30 28 70 4c 69 6e 6b 2d 3e 74  trlen30(pLink->t
2cd49 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 70 54 61  able);.      pTa
2cd4a 62 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  b = sqlite3HashF
2cd4b 69 6e 64 28 26 70 4c 69 6e 6b 2d 3e 70 54 61 62  ind(&pLink->pTab
2cd4c 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c  Schema->tblHash,
2cd4d 20 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65 2c 20 6e   pLink->table, n
2cd4e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2cd4f 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20   pTab!=0 );.    
2cd50 20 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d    pLink->pNext =
2cd51 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b   pTab->pTrigger;
2cd52 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 54 72  .      pTab->pTr
2cd53 69 67 67 65 72 20 3d 20 70 4c 69 6e 6b 3b 0a 20  igger = pLink;. 
2cd54 20 20 20 7d 0a 20 20 7d 0a 0a 74 72 69 67 67 65     }.  }..trigge
2cd55 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a  rfinish_cleanup:
2cd56 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
2cd57 54 72 69 67 67 65 72 28 64 62 2c 20 70 54 72 69  Trigger(db, pTri
2cd58 67 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70  g);.  assert( !p
2cd59 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2cd5a 65 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  er );.  sqlite3D
2cd5b 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70  eleteTriggerStep
2cd5c 28 64 62 2c 20 70 53 74 65 70 4c 69 73 74 29 3b  (db, pStepList);
2cd5d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61  .}../*.** Turn a
2cd5e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2cd5f 74 20 28 74 68 61 74 20 74 68 65 20 70 53 65 6c  t (that the pSel
2cd60 65 63 74 20 70 61 72 61 6d 65 74 65 72 20 70 6f  ect parameter po
2cd61 69 6e 74 73 20 74 6f 29 20 69 6e 74 6f 0a 2a 2a  ints to) into.**
2cd62 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70 2e   a trigger step.
2cd63 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
2cd64 65 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 53  er to a TriggerS
2cd65 74 65 70 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  tep structure..*
2cd66 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20  *.** The parser 
2cd67 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
2cd68 6e 65 20 77 68 65 6e 20 69 74 20 66 69 6e 64 73  ne when it finds
2cd69 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2cd6a 65 6e 74 20 69 6e 0a 2a 2a 20 62 6f 64 79 20 6f  ent in.** body o
2cd6b 66 20 61 20 54 52 49 47 47 45 52 2e 20 20 0a 2a  f a TRIGGER.  .*
2cd6c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2cd6d 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
2cd6e 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65  lite3TriggerSele
2cd6f 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a  ctStep(sqlite3 *
2cd70 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  db, Select *pSel
2cd71 65 63 74 29 7b 0a 20 20 54 72 69 67 67 65 72 53  ect){.  TriggerS
2cd72 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
2cd73 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
2cd74 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2cd75 6f 66 28 54 72 69 67 67 65 72 53 74 65 70 29 29  of(TriggerStep))
2cd76 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
2cd77 53 74 65 70 3d 3d 30 20 29 20 7b 0a 20 20 20 20  Step==0 ) {.    
2cd78 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2cd79 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29  ete(db, pSelect)
2cd7a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
2cd7b 20 20 7d 0a 20 20 70 54 72 69 67 67 65 72 53 74    }.  pTriggerSt
2cd7c 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45  ep->op = TK_SELE
2cd7d 43 54 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74  CT;.  pTriggerSt
2cd7e 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53  ep->pSelect = pS
2cd7f 65 6c 65 63 74 3b 0a 20 20 70 54 72 69 67 67 65  elect;.  pTrigge
2cd80 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20  rStep->orconf = 
2cd81 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20 72 65  OE_Default;.  re
2cd82 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
2cd83 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  p;.}../*.** Allo
2cd84 63 61 74 65 20 73 70 61 63 65 20 74 6f 20 68 6f  cate space to ho
2cd85 6c 64 20 61 20 6e 65 77 20 74 72 69 67 67 65 72  ld a new trigger
2cd86 20 73 74 65 70 2e 20 20 54 68 65 20 61 6c 6c 6f   step.  The allo
2cd87 63 61 74 65 64 20 73 70 61 63 65 0a 2a 2a 20 68  cated space.** h
2cd88 6f 6c 64 73 20 62 6f 74 68 20 74 68 65 20 54 72  olds both the Tr
2cd89 69 67 67 65 72 53 74 65 70 20 6f 62 6a 65 63 74  iggerStep object
2cd8a 20 61 6e 64 20 74 68 65 20 54 72 69 67 67 65 72   and the Trigger
2cd8b 53 74 65 70 2e 74 61 72 67 65 74 2e 7a 20 73 74  Step.target.z st
2cd8c 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ring..**.** If a
2cd8d 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75  n OOM error occu
2cd8e 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  rs, NULL is retu
2cd8f 72 6e 65 64 20 61 6e 64 20 64 62 2d 3e 6d 61 6c  rned and db->mal
2cd90 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 73 65 74  locFailed is set
2cd91 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 72 69 67  ..*/.static Trig
2cd92 67 65 72 53 74 65 70 20 2a 74 72 69 67 67 65 72  gerStep *trigger
2cd93 53 74 65 70 41 6c 6c 6f 63 61 74 65 28 0a 20 20  StepAllocate(.  
2cd94 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2cd95 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2cd96 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2cd97 6f 6e 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 20  on */.  u8 op,  
2cd98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cd99 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 6f      /* Trigger o
2cd9a 70 63 6f 64 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e  pcode */.  Token
2cd9b 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20   *pName         
2cd9c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2cd9d 72 67 65 74 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a  rget name */.){.
2cd9e 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
2cd9f 54 72 69 67 67 65 72 53 74 65 70 3b 0a 0a 20 20  TriggerStep;..  
2cda0 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73  pTriggerStep = s
2cda1 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2cda2 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72  ro(db, sizeof(Tr
2cda3 69 67 67 65 72 53 74 65 70 29 20 2b 20 70 4e 61  iggerStep) + pNa
2cda4 6d 65 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 70 54  me->n);.  if( pT
2cda5 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20  riggerStep ){.  
2cda6 20 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 68 61    char *z = (cha
2cda7 72 2a 29 26 70 54 72 69 67 67 65 72 53 74 65 70  r*)&pTriggerStep
2cda8 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  [1];.    memcpy(
2cda9 7a 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61  z, pName->z, pNa
2cdaa 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 72 69  me->n);.    pTri
2cdab 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74  ggerStep->target
2cdac 2e 7a 20 3d 20 7a 3b 0a 20 20 20 20 70 54 72 69  .z = z;.    pTri
2cdad 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74  ggerStep->target
2cdae 2e 6e 20 3d 20 70 4e 61 6d 65 2d 3e 6e 3b 0a 20  .n = pName->n;. 
2cdaf 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
2cdb0 3e 6f 70 20 3d 20 6f 70 3b 0a 20 20 7d 0a 20 20  >op = op;.  }.  
2cdb1 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
2cdb2 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75  tep;.}../*.** Bu
2cdb3 69 6c 64 20 61 20 74 72 69 67 67 65 72 20 73 74  ild a trigger st
2cdb4 65 70 20 6f 75 74 20 6f 66 20 61 6e 20 49 4e 53  ep out of an INS
2cdb5 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ERT statement.  
2cdb6 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
2cdb7 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 65 77 20 74  .** to the new t
2cdb8 72 69 67 67 65 72 20 73 74 65 70 2e 0a 2a 2a 0a  rigger step..**.
2cdb9 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
2cdba 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
2cdbb 20 77 68 65 6e 20 69 74 20 73 65 65 73 20 61 6e   when it sees an
2cdbc 20 49 4e 53 45 52 54 20 69 6e 73 69 64 65 20 74   INSERT inside t
2cdbd 68 65 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20  he.** body of a 
2cdbe 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49  trigger..*/.SQLI
2cdbf 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67  TE_PRIVATE Trigg
2cdc0 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
2cdc1 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
2cdc2 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2cdc3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
2cdc4 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2cdc5 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  on */.  Token *p
2cdc6 54 61 62 6c 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e  TableName,  /* N
2cdc7 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2cdc8 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 69   into which we i
2cdc9 6e 73 65 72 74 20 2a 2f 0a 20 20 49 64 4c 69 73  nsert */.  IdLis
2cdca 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f  t *pColumn,    /
2cdcb 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  * List of column
2cdcc 73 20 69 6e 20 70 54 61 62 6c 65 4e 61 6d 65 20  s in pTableName 
2cdcd 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a  to insert into *
2cdce 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2cdcf 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 56  List,   /* The V
2cdd0 41 4c 55 45 20 63 6c 61 75 73 65 3a 20 61 20 6c  ALUE clause: a l
2cdd1 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ist of values to
2cdd2 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
2cdd3 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
2cdd4 74 2c 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43  t,    /* A SELEC
2cdd5 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  T statement that
2cdd6 20 73 75 70 70 6c 69 65 73 20 76 61 6c 75 65 73   supplies values
2cdd7 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20   */.  u8 orconf 
2cdd8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2cdd9 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69   conflict algori
2cdda 74 68 6d 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f  thm (OE_Abort, O
2cddb 45 5f 52 65 70 6c 61 63 65 2c 20 65 74 63 2e 29  E_Replace, etc.)
2cddc 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72   */.){.  Trigger
2cddd 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74  Step *pTriggerSt
2cdde 65 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 45  ep;..  assert(pE
2cddf 4c 69 73 74 20 3d 3d 20 30 20 7c 7c 20 70 53 65  List == 0 || pSe
2cde0 6c 65 63 74 20 3d 3d 20 30 29 3b 0a 20 20 61 73  lect == 0);.  as
2cde1 73 65 72 74 28 70 45 4c 69 73 74 20 21 3d 20 30  sert(pEList != 0
2cde2 20 7c 7c 20 70 53 65 6c 65 63 74 20 21 3d 20 30   || pSelect != 0
2cde3 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2cde4 69 6c 65 64 29 3b 0a 0a 20 20 70 54 72 69 67 67  iled);..  pTrigg
2cde5 65 72 53 74 65 70 20 3d 20 74 72 69 67 67 65 72  erStep = trigger
2cde6 53 74 65 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c  StepAllocate(db,
2cde7 20 54 4b 5f 49 4e 53 45 52 54 2c 20 70 54 61 62   TK_INSERT, pTab
2cde8 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70  leName);.  if( p
2cde9 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
2cdea 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
2cdeb 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  >pSelect = sqlit
2cdec 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
2cded 70 53 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50  pSelect, EXPRDUP
2cdee 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54  _REDUCE);.    pT
2cdef 72 69 67 67 65 72 53 74 65 70 2d 3e 70 49 64 4c  riggerStep->pIdL
2cdf0 69 73 74 20 3d 20 70 43 6f 6c 75 6d 6e 3b 0a 20  ist = pColumn;. 
2cdf1 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
2cdf2 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c  >pExprList = sql
2cdf3 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
2cdf4 64 62 2c 20 70 45 4c 69 73 74 2c 20 45 58 50 52  db, pEList, EXPR
2cdf5 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2cdf6 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
2cdf7 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a  rconf = orconf;.
2cdf8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
2cdf9 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
2cdfa 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20  (db, pColumn);. 
2cdfb 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
2cdfc 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2cdfd 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  EList);.  sqlite
2cdfe 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2cdff 2c 20 70 53 65 6c 65 63 74 29 3b 0a 0a 20 20 72  , pSelect);..  r
2ce00 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74  eturn pTriggerSt
2ce01 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  ep;.}../*.** Con
2ce02 73 74 72 75 63 74 20 61 20 74 72 69 67 67 65 72  struct a trigger
2ce03 20 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65   step that imple
2ce04 6d 65 6e 74 73 20 61 6e 20 55 50 44 41 54 45 20  ments an UPDATE 
2ce05 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
2ce06 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
2ce07 72 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65  r to that trigge
2ce08 72 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72  r step.  The par
2ce09 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
2ce0a 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a  outine when it.*
2ce0b 2a 20 73 65 65 73 20 61 6e 20 55 50 44 41 54 45  * sees an UPDATE
2ce0c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64   statement insid
2ce0d 65 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20  e the body of a 
2ce0e 43 52 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a  CREATE TRIGGER..
2ce0f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2ce10 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73  E TriggerStep *s
2ce11 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64  qlite3TriggerUpd
2ce12 61 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74  ateStep(.  sqlit
2ce13 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2ce14 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2ce15 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2ce16 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d  Token *pTableNam
2ce17 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  e,   /* Name of 
2ce18 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
2ce19 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 45 78 70  updated */.  Exp
2ce1a 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
2ce1b 20 20 2f 2a 20 54 68 65 20 53 45 54 20 63 6c 61    /* The SET cla
2ce1c 75 73 65 3a 20 6c 69 73 74 20 6f 66 20 63 6f 6c  use: list of col
2ce1d 75 6d 6e 20 61 6e 64 20 6e 65 77 20 76 61 6c 75  umn and new valu
2ce1e 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  es */.  Expr *pW
2ce1f 68 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  here,        /* 
2ce20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2ce21 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20   */.  u8 orconf 
2ce22 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ce23 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72  e conflict algor
2ce24 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c  ithm. (OE_Abort,
2ce25 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 65 74 63 29   OE_Ignore, etc)
2ce26 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72   */.){.  Trigger
2ce27 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74  Step *pTriggerSt
2ce28 65 70 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53  ep;..  pTriggerS
2ce29 74 65 70 20 3d 20 74 72 69 67 67 65 72 53 74 65  tep = triggerSte
2ce2a 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b  pAllocate(db, TK
2ce2b 5f 55 50 44 41 54 45 2c 20 70 54 61 62 6c 65 4e  _UPDATE, pTableN
2ce2c 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69  ame);.  if( pTri
2ce2d 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20  ggerStep ){.    
2ce2e 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45  pTriggerStep->pE
2ce2f 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65  xprList = sqlite
2ce30 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
2ce31 20 70 45 4c 69 73 74 2c 20 45 58 50 52 44 55 50   pEList, EXPRDUP
2ce32 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54  _REDUCE);.    pT
2ce33 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65  riggerStep->pWhe
2ce34 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
2ce35 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20  Dup(db, pWhere, 
2ce36 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b  EXPRDUP_REDUCE);
2ce37 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
2ce38 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f  p->orconf = orco
2ce39 6e 66 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  nf;.  }.  sqlite
2ce3a 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2ce3b 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73  db, pEList);.  s
2ce3c 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2ce3d 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
2ce3e 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
2ce3f 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  tep;.}../*.** Co
2ce40 6e 73 74 72 75 63 74 20 61 20 74 72 69 67 67 65  nstruct a trigge
2ce41 72 20 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c  r step that impl
2ce42 65 6d 65 6e 74 73 20 61 20 44 45 4c 45 54 45 20  ements a DELETE 
2ce43 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
2ce44 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
2ce45 72 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65  r to that trigge
2ce46 72 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72  r step.  The par
2ce47 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
2ce48 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a  outine when it.*
2ce49 2a 20 73 65 65 73 20 61 20 44 45 4c 45 54 45 20  * sees a DELETE 
2ce4a 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65  statement inside
2ce4b 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43   the body of a C
2ce4c 52 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a  REATE TRIGGER..*
2ce4d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2ce4e 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
2ce4f 6c 69 74 65 33 54 72 69 67 67 65 72 44 65 6c 65  lite3TriggerDele
2ce50 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65  teStep(.  sqlite
2ce51 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2ce52 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2ce53 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f  nnection */.  To
2ce54 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ken *pTableName,
2ce55 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
2ce56 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 72 6f  le from which ro
2ce57 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 2a  ws are deleted *
2ce58 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
2ce59 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2ce5a 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2ce5b 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53  */.){.  TriggerS
2ce5c 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
2ce5d 70 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74  p;..  pTriggerSt
2ce5e 65 70 20 3d 20 74 72 69 67 67 65 72 53 74 65 70  ep = triggerStep
2ce5f 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f  Allocate(db, TK_
2ce60 44 45 4c 45 54 45 2c 20 70 54 61 62 6c 65 4e 61  DELETE, pTableNa
2ce61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67  me);.  if( pTrig
2ce62 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70  gerStep ){.    p
2ce63 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68  TriggerStep->pWh
2ce64 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
2ce65 72 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c  rDup(db, pWhere,
2ce66 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
2ce67 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
2ce68 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f  ep->orconf = OE_
2ce69 44 65 66 61 75 6c 74 3b 0a 20 20 7d 0a 20 20 73  Default;.  }.  s
2ce6a 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2ce6b 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
2ce6c 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
2ce6d 74 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  tep;.}../* .** R
2ce6e 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74  ecursively delet
2ce6f 65 20 61 20 54 72 69 67 67 65 72 20 73 74 72 75  e a Trigger stru
2ce70 63 74 75 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cture.*/.SQLITE_
2ce71 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2ce72 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
2ce73 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54  r(sqlite3 *db, T
2ce74 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
2ce75 29 7b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  ){.  if( pTrigge
2ce76 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
2ce77 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
2ce78 69 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54  iggerStep(db, pT
2ce79 72 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73  rigger->step_lis
2ce7a 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  t);.  sqlite3DbF
2ce7b 72 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72  ree(db, pTrigger
2ce7c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  ->zName);.  sqli
2ce7d 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
2ce7e 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a  rigger->table);.
2ce7f 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2ce80 65 74 65 28 64 62 2c 20 70 54 72 69 67 67 65 72  ete(db, pTrigger
2ce81 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 73 71 6c 69  ->pWhen);.  sqli
2ce82 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
2ce83 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 43  db, pTrigger->pC
2ce84 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74  olumns);.  sqlit
2ce85 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72  e3DbFree(db, pTr
2ce86 69 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  igger);.}../*.**
2ce87 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2ce88 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 72 6f 70  s called to drop
2ce89 20 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20   a trigger from 
2ce8a 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
2ce8b 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ema. .**.** This
2ce8c 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 64   may be called d
2ce8d 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65  irectly from the
2ce8e 20 70 61 72 73 65 72 20 61 6e 64 20 74 68 65 72   parser and ther
2ce8f 65 66 6f 72 65 20 69 64 65 6e 74 69 66 69 65 73  efore identifies
2ce90 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20  .** the trigger 
2ce91 62 79 20 6e 61 6d 65 2e 20 20 54 68 65 20 73 71  by name.  The sq
2ce92 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72  lite3DropTrigger
2ce93 50 74 72 28 29 20 72 6f 75 74 69 6e 65 20 64 6f  Ptr() routine do
2ce94 65 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6a  es the.** same j
2ce95 6f 62 20 61 73 20 74 68 69 73 20 72 6f 75 74 69  ob as this routi
2ce96 6e 65 20 65 78 63 65 70 74 20 69 74 20 74 61 6b  ne except it tak
2ce97 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  es a pointer to 
2ce98 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 69  the trigger.** i
2ce99 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 74 72  nstead of the tr
2ce9a 69 67 67 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a  igger name..**/.
2ce9b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2ce9c 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
2ce9d 72 69 67 67 65 72 28 50 61 72 73 65 20 2a 70 50  rigger(Parse *pP
2ce9e 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
2ce9f 4e 61 6d 65 2c 20 69 6e 74 20 6e 6f 45 72 72 29  Name, int noErr)
2cea0 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  {.  Trigger *pTr
2cea1 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74  igger = 0;.  int
2cea2 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   i;.  const char
2cea3 20 2a 7a 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63   *zDb;.  const c
2cea4 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e  har *zName;.  in
2cea5 74 20 6e 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74  t nName;.  sqlit
2cea6 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2cea7 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  >db;..  if( db->
2cea8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
2cea9 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72  oto drop_trigger
2ceaa 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20  _cleanup;.  if( 
2ceab 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
2ceac 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
2cead 72 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  rse) ){.    goto
2ceae 20 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c   drop_trigger_cl
2ceaf 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 61 73  eanup;.  }..  as
2ceb0 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72  sert( pName->nSr
2ceb1 63 3d 3d 31 20 29 3b 0a 20 20 7a 44 62 20 3d 20  c==1 );.  zDb = 
2ceb2 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74  pName->a[0].zDat
2ceb3 61 62 61 73 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d  abase;.  zName =
2ceb4 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61   pName->a[0].zNa
2ceb5 6d 65 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71  me;.  nName = sq
2ceb6 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
2ceb7 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d  ame);.  for(i=OM
2ceb8 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d  IT_TEMPDB; i<db-
2ceb9 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
2ceba 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20  int j = (i<2) ? 
2cebb 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61  i^1 : i;  /* Sea
2cebc 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20  rch TEMP before 
2cebd 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20  MAIN */.    if( 
2cebe 7a 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74  zDb && sqlite3St
2cebf 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d  rICmp(db->aDb[j]
2cec0 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 20 63  .zName, zDb) ) c
2cec1 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 54 72  ontinue;.    pTr
2cec2 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 48  igger = sqlite3H
2cec3 61 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44  ashFind(&(db->aD
2cec4 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72  b[j].pSchema->tr
2cec5 69 67 48 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20  igHash), zName, 
2cec6 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  nName);.    if( 
2cec7 70 54 72 69 67 67 65 72 20 29 20 62 72 65 61 6b  pTrigger ) break
2cec8 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 54 72  ;.  }.  if( !pTr
2cec9 69 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28  igger ){.    if(
2ceca 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20   !noErr ){.     
2cecb 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2cecc 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
2cecd 68 20 74 72 69 67 67 65 72 3a 20 25 53 22 2c 20  h trigger: %S", 
2cece 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d  pName, 0);.    }
2cecf 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74  .    goto drop_t
2ced0 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2ced1 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 72 6f    }.  sqlite3Dro
2ced2 70 54 72 69 67 67 65 72 50 74 72 28 70 50 61 72  pTriggerPtr(pPar
2ced3 73 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 0a  se, pTrigger);..
2ced4 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65  drop_trigger_cle
2ced5 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53  anup:.  sqlite3S
2ced6 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
2ced7 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   pName);.}../*.*
2ced8 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
2ced9 65 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20  er to the Table 
2ceda 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68  structure for th
2cedb 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 20 74  e table that a t
2cedc 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 73 65 74  rigger.** is set
2cedd 20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54   on..*/.static T
2cede 61 62 6c 65 20 2a 74 61 62 6c 65 4f 66 54 72 69  able *tableOfTri
2cedf 67 67 65 72 28 54 72 69 67 67 65 72 20 2a 70 54  gger(Trigger *pT
2cee0 72 69 67 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e  rigger){.  int n
2cee1 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2cee2 33 30 28 70 54 72 69 67 67 65 72 2d 3e 74 61 62  30(pTrigger->tab
2cee3 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  le);.  return sq
2cee4 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70  lite3HashFind(&p
2cee5 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68  Trigger->pTabSch
2cee6 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54  ema->tblHash, pT
2cee7 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20 6e  rigger->table, n
2cee8 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f  );.}.../*.** Dro
2cee9 70 20 61 20 74 72 69 67 67 65 72 20 67 69 76 65  p a trigger give
2ceea 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
2ceeb 68 61 74 20 74 72 69 67 67 65 72 2e 20 0a 2a 2f  hat trigger. .*/
2ceec 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ceed 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
2ceee 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65  TriggerPtr(Parse
2ceef 20 2a 70 50 61 72 73 65 2c 20 54 72 69 67 67 65   *pParse, Trigge
2cef0 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20  r *pTrigger){.  
2cef1 54 61 62 6c 65 20 20 20 2a 70 54 61 62 6c 65 3b  Table   *pTable;
2cef2 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
2cef3 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2cef4 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  se->db;.  int iD
2cef5 62 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  b;..  iDb = sqli
2cef6 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2cef7 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72  (pParse->db, pTr
2cef8 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 29 3b  igger->pSchema);
2cef9 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
2cefa 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
2cefb 20 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 74   );.  pTable = t
2cefc 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54  ableOfTrigger(pT
2cefd 72 69 67 67 65 72 29 3b 0a 20 20 61 73 73 65 72  rigger);.  asser
2cefe 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 20 20 61  t( pTable );.  a
2ceff 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70  ssert( pTable->p
2cf00 53 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72  Schema==pTrigger
2cf01 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 69 44 62  ->pSchema || iDb
2cf02 3d 3d 31 20 29 3b 0a 23 69 66 6e 64 65 66 20 53  ==1 );.#ifndef S
2cf03 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2cf04 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
2cf05 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49   int code = SQLI
2cf06 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 3b  TE_DROP_TRIGGER;
2cf07 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2cf08 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *zDb = db->aDb[i
2cf09 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
2cf0a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20  onst char *zTab 
2cf0b 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
2cf0c 44 62 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62  Db);.    if( iDb
2cf0d 3d 3d 31 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  ==1 ) code = SQL
2cf0e 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52  ITE_DROP_TEMP_TR
2cf0f 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73  IGGER;.    if( s
2cf10 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2cf11 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 54  pParse, code, pT
2cf12 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 70  rigger->zName, p
2cf13 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44  Table->zName, zD
2cf14 62 29 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69  b) ||.      sqli
2cf15 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2cf16 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
2cf17 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62  TE, zTab, 0, zDb
2cf18 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
2cf19 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  n;.    }.  }.#en
2cf1a 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  dif..  /* Genera
2cf1b 74 65 20 63 6f 64 65 20 74 6f 20 64 65 73 74 72  te code to destr
2cf1c 6f 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  oy the database 
2cf1d 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74 72  record of the tr
2cf1e 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73  igger..  */.  as
2cf1f 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20  sert( pTable!=0 
2cf20 29 3b 0a 20 20 69 66 28 20 28 76 20 3d 20 73 71  );.  if( (v = sq
2cf21 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2cf22 72 73 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  rse))!=0 ){.    
2cf23 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 20 73 74  int base;.    st
2cf24 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
2cf25 70 4c 69 73 74 20 64 72 6f 70 54 72 69 67 67 65  pList dropTrigge
2cf26 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20  r[] = {.      { 
2cf27 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 30  OP_Rewind,     0
2cf28 2c 20 41 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a  , ADDR(9),  0},.
2cf29 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
2cf2a 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20  g8,    0, 1,    
2cf2b 20 20 20 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a      0}, /* 1 */.
2cf2c 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d        { OP_Colum
2cf2d 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20  n,     0, 1,    
2cf2e 20 20 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20      2},.      { 
2cf2f 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32  OP_Ne,         2
2cf30 2c 20 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a  , ADDR(8),  1},.
2cf31 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
2cf32 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20  g8,    0, 1,    
2cf33 20 20 20 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74      0}, /* 4: "t
2cf34 72 69 67 67 65 72 22 20 2a 2f 0a 20 20 20 20 20  rigger" */.     
2cf35 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20   { OP_Column,   
2cf36 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 32    0, 0,        2
2cf37 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
2cf38 2c 20 20 20 20 20 20 20 20 20 32 2c 20 41 44 44  ,         2, ADD
2cf39 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20  R(8),  1},.     
2cf3a 20 7b 20 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20   { OP_Delete,   
2cf3b 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30    0, 0,        0
2cf3c 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
2cf3d 78 74 2c 20 20 20 20 20 20 20 30 2c 20 41 44 44  xt,       0, ADD
2cf3e 52 28 31 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20  R(1),  0}, /* 8 
2cf3f 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73  */.    };..    s
2cf40 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
2cf41 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
2cf42 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 0, iDb);.    s
2cf43 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72  qlite3OpenMaster
2cf44 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
2cf45 62 29 3b 0a 20 20 20 20 62 61 73 65 20 3d 20 73  b);.    base = s
2cf46 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
2cf47 69 73 74 28 76 2c 20 20 41 72 72 61 79 53 69 7a  ist(v,  ArraySiz
2cf48 65 28 64 72 6f 70 54 72 69 67 67 65 72 29 2c 20  e(dropTrigger), 
2cf49 64 72 6f 70 54 72 69 67 67 65 72 29 3b 0a 20 20  dropTrigger);.  
2cf4a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2cf4b 6e 67 65 50 34 28 76 2c 20 62 61 73 65 2b 31 2c  ngeP4(v, base+1,
2cf4c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2cf4d 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
2cf4e 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
2cf4f 20 62 61 73 65 2b 34 2c 20 22 74 72 69 67 67 65   base+4, "trigge
2cf50 72 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  r", P4_STATIC);.
2cf51 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
2cf52 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
2cf53 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
2cf54 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2cf55 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a  P_Close, 0, 0);.
2cf56 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2cf57 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70  ddOp4(v, OP_Drop
2cf58 54 72 69 67 67 65 72 2c 20 69 44 62 2c 20 30 2c  Trigger, iDb, 0,
2cf59 20 30 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e   0, pTrigger->zN
2cf5a 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  ame, 0);.    if(
2cf5b 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3c 33 20   pParse->nMem<3 
2cf5c 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
2cf5d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 7d  >nMem = 3;.    }
2cf5e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
2cf5f 6d 6f 76 65 20 61 20 74 72 69 67 67 65 72 20 66  move a trigger f
2cf60 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
2cf61 6c 65 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74  les of the sqlit
2cf62 65 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  e* pointer..*/.S
2cf63 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2cf64 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
2cf65 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72  AndDeleteTrigger
2cf66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
2cf67 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
2cf68 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 48 61 73  r *zName){.  Has
2cf69 68 20 2a 70 48 61 73 68 20 3d 20 26 28 64 62 2d  h *pHash = &(db-
2cf6a 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
2cf6b 61 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20  a->trigHash);.  
2cf6c 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
2cf6d 72 3b 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20  r;.  pTrigger = 
2cf6e 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
2cf6f 74 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20  t(pHash, zName, 
2cf70 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2cf71 7a 4e 61 6d 65 29 2c 20 30 29 3b 0a 20 20 69 66  zName), 0);.  if
2cf72 28 20 41 4c 57 41 59 53 28 70 54 72 69 67 67 65  ( ALWAYS(pTrigge
2cf73 72 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54  r) ){.    if( pT
2cf74 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d  rigger->pSchema=
2cf75 3d 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53  =pTrigger->pTabS
2cf76 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 54  chema ){.      T
2cf77 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 74 61 62  able *pTab = tab
2cf78 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69  leOfTrigger(pTri
2cf79 67 67 65 72 29 3b 0a 20 20 20 20 20 20 54 72 69  gger);.      Tri
2cf7a 67 67 65 72 20 2a 2a 70 70 3b 0a 20 20 20 20 20  gger **pp;.     
2cf7b 20 66 6f 72 28 70 70 3d 26 70 54 61 62 2d 3e 70   for(pp=&pTab->p
2cf7c 54 72 69 67 67 65 72 3b 20 2a 70 70 21 3d 70 54  Trigger; *pp!=pT
2cf7d 72 69 67 67 65 72 3b 20 70 70 3d 26 28 28 2a 70  rigger; pp=&((*p
2cf7e 70 29 2d 3e 70 4e 65 78 74 29 29 3b 0a 20 20 20  p)->pNext));.   
2cf7f 20 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e     *pp = (*pp)->
2cf80 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  pNext;.    }.   
2cf81 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
2cf82 69 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 67  igger(db, pTrigg
2cf83 65 72 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61  er);.    db->fla
2cf84 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
2cf85 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 0a  ernChanges;.  }.
2cf86 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20  }../*.** pEList 
2cf87 69 73 20 74 68 65 20 53 45 54 20 63 6c 61 75 73  is the SET claus
2cf88 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73  e of an UPDATE s
2cf89 74 61 74 65 6d 65 6e 74 2e 20 20 45 61 63 68 20  tatement.  Each 
2cf8a 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 45 4c 69  entry.** in pELi
2cf8b 73 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  st is of the for
2cf8c 6d 61 74 20 3c 69 64 3e 3d 3c 65 78 70 72 3e 2e  mat <id>=<expr>.
2cf8d 20 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20    If any of the 
2cf8e 65 6e 74 72 69 65 73 0a 2a 2a 20 69 6e 20 70 45  entries.** in pE
2cf8f 4c 69 73 74 20 68 61 76 65 20 61 6e 20 3c 69 64  List have an <id
2cf90 3e 20 77 68 69 63 68 20 6d 61 74 63 68 65 73 20  > which matches 
2cf91 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 6e  an identifier in
2cf92 20 70 49 64 4c 69 73 74 2c 0a 2a 2a 20 74 68 65   pIdList,.** the
2cf93 6e 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20  n return TRUE.  
2cf94 49 66 20 70 49 64 4c 69 73 74 3d 3d 4e 55 4c 4c  If pIdList==NULL
2cf95 2c 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 6e  , then it is con
2cf96 73 69 64 65 72 65 64 20 61 0a 2a 2a 20 77 69 6c  sidered a.** wil
2cf97 64 63 61 72 64 20 74 68 61 74 20 6d 61 74 63 68  dcard that match
2cf98 65 73 20 61 6e 79 74 68 69 6e 67 2e 20 20 4c 69  es anything.  Li
2cf99 6b 65 77 69 73 65 20 69 66 20 70 45 4c 69 73 74  kewise if pEList
2cf9a 3d 3d 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20 69  ==NULL then.** i
2cf9b 74 20 6d 61 74 63 68 65 73 20 61 6e 79 74 68 69  t matches anythi
2cf9c 6e 67 20 73 6f 20 61 6c 77 61 79 73 20 72 65 74  ng so always ret
2cf9d 75 72 6e 20 74 72 75 65 2e 20 20 52 65 74 75 72  urn true.  Retur
2cf9e 6e 20 66 61 6c 73 65 20 6f 6e 6c 79 0a 2a 2a 20  n false only.** 
2cf9f 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
2cfa0 61 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  atch..*/.static 
2cfa1 69 6e 74 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f  int checkColumnO
2cfa2 76 65 72 6c 61 70 28 49 64 4c 69 73 74 20 2a 70  verlap(IdList *p
2cfa3 49 64 4c 69 73 74 2c 20 45 78 70 72 4c 69 73 74  IdList, ExprList
2cfa4 20 2a 70 45 4c 69 73 74 29 7b 0a 20 20 69 6e 74   *pEList){.  int
2cfa5 20 65 3b 0a 20 20 69 66 28 20 70 49 64 4c 69 73   e;.  if( pIdLis
2cfa6 74 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 45  t==0 || NEVER(pE
2cfa7 4c 69 73 74 3d 3d 30 29 20 29 20 72 65 74 75 72  List==0) ) retur
2cfa8 6e 20 31 3b 0a 20 20 66 6f 72 28 65 3d 30 3b 20  n 1;.  for(e=0; 
2cfa9 65 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  e<pEList->nExpr;
2cfaa 20 65 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   e++){.    if( s
2cfab 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
2cfac 78 28 70 49 64 4c 69 73 74 2c 20 70 45 4c 69 73  x(pIdList, pELis
2cfad 74 2d 3e 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d  t->a[e].zName)>=
2cfae 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
2cfaf 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d  }.  return 0; .}
2cfb0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
2cfb1 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74 72 69   list of all tri
2cfb2 67 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70  ggers on table p
2cfb3 54 61 62 20 69 66 20 74 68 65 72 65 20 65 78 69  Tab if there exi
2cfb4 73 74 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20  sts at least.** 
2cfb5 6f 6e 65 20 74 72 69 67 67 65 72 20 74 68 61 74  one trigger that
2cfb6 20 6d 75 73 74 20 62 65 20 66 69 72 65 64 20 77   must be fired w
2cfb7 68 65 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e  hen an operation
2cfb8 20 6f 66 20 74 79 70 65 20 27 6f 70 27 20 69 73   of type 'op' is
2cfb9 20 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 6f   .** performed o
2cfba 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64  n the table, and
2cfbb 2c 20 69 66 20 74 68 61 74 20 6f 70 65 72 61 74  , if that operat
2cfbc 69 6f 6e 20 69 73 20 61 6e 20 55 50 44 41 54 45  ion is an UPDATE
2cfbd 2c 20 69 66 20 61 74 0a 2a 2a 20 6c 65 61 73 74  , if at.** least
2cfbe 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6c 75   one of the colu
2cfbf 6d 6e 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20  mns in pChanges 
2cfc0 69 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65  is being modifie
2cfc1 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2cfc2 56 41 54 45 20 54 72 69 67 67 65 72 20 2a 73 71  VATE Trigger *sq
2cfc3 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69  lite3TriggersExi
2cfc4 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
2cfc5 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
2cfc6 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
2cfc7 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
2cfc8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2cfc9 68 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6e  he table the con
2cfca 74 61 69 6e 73 20 74 68 65 20 74 72 69 67 67 65  tains the trigge
2cfcb 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20  rs */.  int op, 
2cfcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cfcd 2f 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c  /* one of TK_DEL
2cfce 45 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  ETE, TK_INSERT, 
2cfcf 54 4b 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20 45  TK_UPDATE */.  E
2cfd0 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
2cfd1 73 2c 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e  s,     /* Column
2cfd2 73 20 74 68 61 74 20 63 68 61 6e 67 65 20 69 6e  s that change in
2cfd3 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
2cfd4 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ment */.  int *p
2cfd5 4d 61 73 6b 20 20 20 20 20 20 20 20 20 20 20 20  Mask            
2cfd6 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f    /* OUT: Mask o
2cfd7 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  f TRIGGER_BEFORE
2cfd8 7c 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a  |TRIGGER_AFTER *
2cfd9 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 20  /.){.  int mask 
2cfda 3d 20 30 3b 0a 20 20 54 72 69 67 67 65 72 20 2a  = 0;.  Trigger *
2cfdb 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 54  pList = sqlite3T
2cfdc 72 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73  riggerList(pPars
2cfdd 65 2c 20 70 54 61 62 29 3b 0a 20 20 54 72 69 67  e, pTab);.  Trig
2cfde 67 65 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  ger *p;.  assert
2cfdf 28 20 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 49 73  ( pList==0 || Is
2cfe0 56 69 72 74 75 61 6c 28 70 54 61 62 29 3d 3d 30  Virtual(pTab)==0
2cfe1 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73   );.  for(p=pLis
2cfe2 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  t; p; p=p->pNext
2cfe3 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70  ){.    if( p->op
2cfe4 3d 3d 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c  ==op && checkCol
2cfe5 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43  umnOverlap(p->pC
2cfe6 6f 6c 75 6d 6e 73 2c 20 70 43 68 61 6e 67 65 73  olumns, pChanges
2cfe7 29 20 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20  ) ){.      mask 
2cfe8 7c 3d 20 70 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20  |= p->tr_tm;.   
2cfe9 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 61   }.  }.  if( pMa
2cfea 73 6b 20 29 7b 0a 20 20 20 20 2a 70 4d 61 73 6b  sk ){.    *pMask
2cfeb 20 3d 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 72   = mask;.  }.  r
2cfec 65 74 75 72 6e 20 28 6d 61 73 6b 20 3f 20 70 4c  eturn (mask ? pL
2cfed 69 73 74 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  ist : 0);.}../*.
2cfee 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70  ** Convert the p
2cfef 53 74 65 70 2d 3e 74 61 72 67 65 74 20 74 6f 6b  Step->target tok
2cff0 65 6e 20 69 6e 74 6f 20 61 20 53 72 63 4c 69 73  en into a SrcLis
2cff1 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70  t and return a p
2cff2 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 61  ointer.** to tha
2cff3 74 20 53 72 63 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a  t SrcList..**.**
2cff4 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64   This routine ad
2cff5 64 73 20 61 20 73 70 65 63 69 66 69 63 20 64 61  ds a specific da
2cff6 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 69 66 20  tabase name, if 
2cff7 6e 65 65 64 65 64 2c 20 74 6f 20 74 68 65 20 74  needed, to the t
2cff8 61 72 67 65 74 20 77 68 65 6e 0a 2a 2a 20 66 6f  arget when.** fo
2cff9 72 6d 69 6e 67 20 74 68 65 20 53 72 63 4c 69 73  rming the SrcLis
2cffa 74 2e 20 20 54 68 69 73 20 70 72 65 76 65 6e 74  t.  This prevent
2cffb 73 20 61 20 74 72 69 67 67 65 72 20 69 6e 20 6f  s a trigger in o
2cffc 6e 65 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d  ne database from
2cffd 0a 2a 2a 20 72 65 66 65 72 72 69 6e 67 20 74 6f  .** referring to
2cffe 20 61 20 74 61 72 67 65 74 20 69 6e 20 61 6e 6f   a target in ano
2cfff 74 68 65 72 20 64 61 74 61 62 61 73 65 2e 20 20  ther database.  
2d000 41 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20  An exception is 
2d001 77 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 69 67  when the.** trig
2d002 67 65 72 20 69 73 20 69 6e 20 54 45 4d 50 20 69  ger is in TEMP i
2d003 6e 20 77 68 69 63 68 20 63 61 73 65 20 69 74 20  n which case it 
2d004 63 61 6e 20 72 65 66 65 72 20 74 6f 20 61 6e 79  can refer to any
2d005 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20   other database 
2d006 69 74 0a 2a 2a 20 77 61 6e 74 73 2e 0a 2a 2f 0a  it.** wants..*/.
2d007 73 74 61 74 69 63 20 53 72 63 4c 69 73 74 20 2a  static SrcList *
2d008 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 0a 20  targetSrcList(. 
2d009 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2d00a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2d00b 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2d00c 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
2d00d 53 74 65 70 20 20 20 2f 2a 20 54 68 65 20 74 72  Step   /* The tr
2d00e 69 67 67 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  igger containing
2d00f 20 74 68 65 20 74 61 72 67 65 74 20 74 6f 6b 65   the target toke
2d010 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44  n */.){.  int iD
2d011 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
2d012 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * Index of the d
2d013 61 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 2a  atabase to use *
2d014 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
2d015 63 3b 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c  c;       /* SrcL
2d016 69 73 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e  ist to be return
2d017 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20  ed */..  pSrc = 
2d018 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
2d019 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  pend(pParse->db,
2d01a 20 30 2c 20 26 70 53 74 65 70 2d 3e 74 61 72 67   0, &pStep->targ
2d01b 65 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53  et, 0);.  if( pS
2d01c 72 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  rc ){.    assert
2d01d 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e 30 20 29  ( pSrc->nSrc>0 )
2d01e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53  ;.    assert( pS
2d01f 72 63 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 20 20  rc->a!=0 );.    
2d020 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
2d021 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
2d022 65 2d 3e 64 62 2c 20 70 53 74 65 70 2d 3e 70 54  e->db, pStep->pT
2d023 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  rig->pSchema);. 
2d024 20 20 20 69 66 28 20 69 44 62 3d 3d 30 20 7c 7c     if( iDb==0 ||
2d025 20 69 44 62 3e 3d 32 20 29 7b 0a 20 20 20 20 20   iDb>=2 ){.     
2d026 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2d027 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20  Parse->db;.     
2d028 20 61 73 73 65 72 74 28 20 69 44 62 3c 70 50 61   assert( iDb<pPa
2d029 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a  rse->db->nDb );.
2d02a 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 70 53        pSrc->a[pS
2d02b 72 63 2d 3e 6e 53 72 63 2d 31 5d 2e 7a 44 61 74  rc->nSrc-1].zDat
2d02c 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44  abase = sqlite3D
2d02d 62 53 74 72 44 75 70 28 64 62 2c 20 64 62 2d 3e  bStrDup(db, db->
2d02e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
2d02f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2d030 75 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a  urn pSrc;.}../*.
2d031 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45  ** Generate VDBE
2d032 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74   code for the st
2d033 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 20  atements inside 
2d034 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 73 69  the body of a si
2d035 6e 67 6c 65 20 0a 2a 2a 20 74 72 69 67 67 65 72  ngle .** trigger
2d036 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2d037 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72  codeTriggerProgr
2d038 61 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  am(.  Parse *pPa
2d039 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
2d03a 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
2d03b 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67  ntext */.  Trigg
2d03c 65 72 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73  erStep *pStepLis
2d03d 74 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  t,   /* List of 
2d03e 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64  statements insid
2d03f 65 20 74 68 65 20 74 72 69 67 67 65 72 20 62 6f  e the trigger bo
2d040 64 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f  dy */.  int orco
2d041 6e 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nf              
2d042 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 61 6c    /* Conflict al
2d043 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f  gorithm. (OE_Abo
2d044 72 74 2c 20 65 74 63 29 20 2a 2f 20 20 0a 29 7b  rt, etc) */  .){
2d045 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
2d046 70 53 74 65 70 3b 0a 20 20 56 64 62 65 20 2a 76  pStep;.  Vdbe *v
2d047 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2d048 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2d049 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
2d04a 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
2d04b 3e 70 54 72 69 67 67 65 72 54 61 62 20 26 26 20  >pTriggerTab && 
2d04c 70 50 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65  pParse->pTopleve
2d04d 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  l );.  assert( p
2d04e 53 74 65 70 4c 69 73 74 20 29 3b 0a 20 20 61 73  StepList );.  as
2d04f 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20  sert( v!=0 );.  
2d050 66 6f 72 28 70 53 74 65 70 3d 70 53 74 65 70 4c  for(pStep=pStepL
2d051 69 73 74 3b 20 70 53 74 65 70 3b 20 70 53 74 65  ist; pStep; pSte
2d052 70 3d 70 53 74 65 70 2d 3e 70 4e 65 78 74 29 7b  p=pStep->pNext){
2d053 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  .    /* Figure o
2d054 75 74 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49  ut the ON CONFLI
2d055 43 54 20 70 6f 6c 69 63 79 20 74 68 61 74 20 77  CT policy that w
2d056 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20  ill be used for 
2d057 74 68 69 73 20 73 74 65 70 0a 20 20 20 20 2a 2a  this step.    **
2d058 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20   of the trigger 
2d059 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 65 20  program. If the 
2d05a 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63  statement that c
2d05b 61 75 73 65 64 20 74 68 69 73 20 74 72 69 67 67  aused this trigg
2d05c 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20 66 69 72  er.    ** to fir
2d05d 65 20 68 61 64 20 61 6e 20 65 78 70 6c 69 63 69  e had an explici
2d05e 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 2c 20 74  t ON CONFLICT, t
2d05f 68 65 6e 20 75 73 65 20 69 74 2e 20 4f 74 68 65  hen use it. Othe
2d060 72 77 69 73 65 2c 20 75 73 65 0a 20 20 20 20 2a  rwise, use.    *
2d061 2a 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43  * the ON CONFLIC
2d062 54 20 70 6f 6c 69 63 79 20 74 68 61 74 20 77 61  T policy that wa
2d063 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70  s specified as p
2d064 61 72 74 20 6f 66 20 74 68 65 20 74 72 69 67 67  art of the trigg
2d065 65 72 0a 20 20 20 20 2a 2a 20 73 74 65 70 20 73  er.    ** step s
2d066 74 61 74 65 6d 65 6e 74 2e 20 45 78 61 6d 70 6c  tatement. Exampl
2d067 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e:.    **.    **
2d068 20 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45     CREATE TRIGGE
2d069 52 20 41 46 54 45 52 20 49 4e 53 45 52 54 20 4f  R AFTER INSERT O
2d06a 4e 20 74 31 20 42 45 47 49 4e 3b 0a 20 20 20 20  N t1 BEGIN;.    
2d06b 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20 4f 52  **     INSERT OR
2d06c 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 74 32   REPLACE INTO t2
2d06d 20 56 41 4c 55 45 53 28 6e 65 77 2e 61 2c 20 6e   VALUES(new.a, n
2d06e 65 77 2e 62 29 3b 0a 20 20 20 20 2a 2a 20 20 20  ew.b);.    **   
2d06f 45 4e 44 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20  END;.    **.    
2d070 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  **   INSERT INTO
2d071 20 74 31 20 2e 2e 2e 20 3b 20 20 20 20 20 20 20   t1 ... ;       
2d072 20 20 20 20 20 2d 2d 20 69 6e 73 65 72 74 20 69       -- insert i
2d073 6e 74 6f 20 74 32 20 75 73 65 73 20 52 45 50 4c  nto t2 uses REPL
2d074 41 43 45 20 70 6f 6c 69 63 79 0a 20 20 20 20 2a  ACE policy.    *
2d075 2a 20 20 20 49 4e 53 45 52 54 20 4f 52 20 49 47  *   INSERT OR IG
2d076 4e 4f 52 45 20 49 4e 54 4f 20 74 31 20 2e 2e 2e  NORE INTO t1 ...
2d077 20 3b 20 20 2d 2d 20 69 6e 73 65 72 74 20 69 6e   ;  -- insert in
2d078 74 6f 20 74 32 20 75 73 65 73 20 49 47 4e 4f 52  to t2 uses IGNOR
2d079 45 20 70 6f 6c 69 63 79 0a 20 20 20 20 2a 2f 0a  E policy.    */.
2d07a 20 20 20 20 70 50 61 72 73 65 2d 3e 65 4f 72 63      pParse->eOrc
2d07b 6f 6e 66 20 3d 20 28 6f 72 63 6f 6e 66 3d 3d 4f  onf = (orconf==O
2d07c 45 5f 44 65 66 61 75 6c 74 29 3f 70 53 74 65 70  E_Default)?pStep
2d07d 2d 3e 6f 72 63 6f 6e 66 3a 28 75 38 29 6f 72 63  ->orconf:(u8)orc
2d07e 6f 6e 66 3b 0a 0a 20 20 20 20 73 77 69 74 63 68  onf;..    switch
2d07f 28 20 70 53 74 65 70 2d 3e 6f 70 20 29 7b 0a 20  ( pStep->op ){. 
2d080 20 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 44       case TK_UPD
2d081 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  ATE: {.        s
2d082 71 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61  qlite3Update(pPa
2d083 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
2d084 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50  targetSrcList(pP
2d085 61 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20  arse, pStep),.  
2d086 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2d087 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
2d088 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c  Step->pExprList,
2d089 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20   0), .          
2d08a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2d08b 62 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65  b, pStep->pWhere
2d08c 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20  , 0), .         
2d08d 20 70 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66   pParse->eOrconf
2d08e 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
2d08f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d090 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b   }.      case TK
2d091 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20 20 20 20  _INSERT: {.     
2d092 20 20 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74     sqlite3Insert
2d093 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
2d094 20 20 20 20 74 61 72 67 65 74 53 72 63 4c 69 73      targetSrcLis
2d095 74 28 70 50 61 72 73 65 2c 20 70 53 74 65 70 29  t(pParse, pStep)
2d096 2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ,.          sqli
2d097 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
2d098 62 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c  b, pStep->pExprL
2d099 69 73 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20  ist, 0), .      
2d09a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2d09b 74 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e  tDup(db, pStep->
2d09c 70 53 65 6c 65 63 74 2c 20 30 29 2c 20 0a 20 20  pSelect, 0), .  
2d09d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 49          sqlite3I
2d09e 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 74  dListDup(db, pSt
2d09f 65 70 2d 3e 70 49 64 4c 69 73 74 29 2c 20 0a 20  ep->pIdList), . 
2d0a0 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d           pParse-
2d0a1 3e 65 4f 72 63 6f 6e 66 0a 20 20 20 20 20 20 20  >eOrconf.       
2d0a2 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   );.        brea
2d0a3 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
2d0a4 20 63 61 73 65 20 54 4b 5f 44 45 4c 45 54 45 3a   case TK_DELETE:
2d0a5 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
2d0a6 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50 61  e3DeleteFrom(pPa
2d0a7 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
2d0a8 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50  targetSrcList(pP
2d0a9 61 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20  arse, pStep),.  
2d0aa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2d0ab 78 70 72 44 75 70 28 64 62 2c 20 70 53 74 65 70  xprDup(db, pStep
2d0ac 2d 3e 70 57 68 65 72 65 2c 20 30 29 0a 20 20 20  ->pWhere, 0).   
2d0ad 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
2d0ae 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2d0af 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 61 73       default: as
2d0b0 73 65 72 74 28 20 70 53 74 65 70 2d 3e 6f 70 3d  sert( pStep->op=
2d0b1 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 20 7b 0a  =TK_SELECT ); {.
2d0b2 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 44 65          SelectDe
2d0b3 73 74 20 73 44 65 73 74 3b 0a 20 20 20 20 20 20  st sDest;.      
2d0b4 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
2d0b5 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  t = sqlite3Selec
2d0b6 74 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e  tDup(db, pStep->
2d0b7 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20  pSelect, 0);.   
2d0b8 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
2d0b9 63 74 44 65 73 74 49 6e 69 74 28 26 73 44 65 73  ctDestInit(&sDes
2d0ba 74 2c 20 53 52 54 5f 44 69 73 63 61 72 64 2c 20  t, SRT_Discard, 
2d0bb 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
2d0bc 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2d0bd 2c 20 70 53 65 6c 65 63 74 2c 20 26 73 44 65 73  , pSelect, &sDes
2d0be 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
2d0bf 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2d0c0 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  db, pSelect);.  
2d0c1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d0c2 20 20 20 7d 0a 20 20 20 20 7d 20 0a 20 20 20 20     }.    } .    
2d0c3 69 66 28 20 70 53 74 65 70 2d 3e 6f 70 21 3d 54  if( pStep->op!=T
2d0c4 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20  K_SELECT ){.    
2d0c5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d0c6 4f 70 30 28 76 2c 20 4f 50 5f 52 65 73 65 74 43  Op0(v, OP_ResetC
2d0c7 6f 75 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ount);.    }.  }
2d0c8 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ..  return 0;.}.
2d0c9 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
2d0ca 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EBUG./*.** This 
2d0cb 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
2d0cc 20 74 6f 20 61 64 64 20 56 64 62 65 43 6f 6d 6d   to add VdbeComm
2d0cd 65 6e 74 28 29 20 61 6e 6e 6f 74 61 74 69 6f 6e  ent() annotation
2d0ce 73 20 74 6f 20 61 20 56 44 42 45 0a 2a 2a 20 70  s to a VDBE.** p
2d0cf 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 20 6e 6f  rogram. It is no
2d0d0 74 20 75 73 65 64 20 69 6e 20 70 72 6f 64 75 63  t used in produc
2d0d1 74 69 6f 6e 20 63 6f 64 65 2c 20 6f 6e 6c 79 20  tion code, only 
2d0d2 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a  for debugging..*
2d0d3 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
2d0d4 68 61 72 20 2a 6f 6e 45 72 72 6f 72 54 65 78 74  har *onErrorText
2d0d5 28 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20  (int onError){. 
2d0d6 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72   switch( onError
2d0d7 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f   ){.    case OE_
2d0d8 41 62 6f 72 74 3a 20 20 20 20 72 65 74 75 72 6e  Abort:    return
2d0d9 20 22 61 62 6f 72 74 22 3b 0a 20 20 20 20 63 61   "abort";.    ca
2d0da 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 20  se OE_Rollback: 
2d0db 72 65 74 75 72 6e 20 22 72 6f 6c 6c 62 61 63 6b  return "rollback
2d0dc 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 46  ";.    case OE_F
2d0dd 61 69 6c 3a 20 20 20 20 20 72 65 74 75 72 6e 20  ail:     return 
2d0de 22 66 61 69 6c 22 3b 0a 20 20 20 20 63 61 73 65  "fail";.    case
2d0df 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 20 72 65   OE_Replace:  re
2d0e0 74 75 72 6e 20 22 72 65 70 6c 61 63 65 22 3b 0a  turn "replace";.
2d0e1 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f      case OE_Igno
2d0e2 72 65 3a 20 20 20 72 65 74 75 72 6e 20 22 69 67  re:   return "ig
2d0e3 6e 6f 72 65 22 3b 0a 20 20 20 20 63 61 73 65 20  nore";.    case 
2d0e4 4f 45 5f 44 65 66 61 75 6c 74 3a 20 20 72 65 74  OE_Default:  ret
2d0e5 75 72 6e 20 22 64 65 66 61 75 6c 74 22 3b 0a 20  urn "default";. 
2d0e6 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 6e 2f 61   }.  return "n/a
2d0e7 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ";.}.#endif../*.
2d0e8 2a 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  ** Parse context
2d0e9 20 73 74 72 75 63 74 75 72 65 20 70 46 72 6f 6d   structure pFrom
2d0ea 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 75   has just been u
2d0eb 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
2d0ec 73 75 62 2d 76 64 62 65 0a 2a 2a 20 28 74 72 69  sub-vdbe.** (tri
2d0ed 67 67 65 72 20 70 72 6f 67 72 61 6d 29 2e 20 49  gger program). I
2d0ee 66 20 61 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  f an error has o
2d0ef 63 63 75 72 72 65 64 2c 20 74 72 61 6e 73 66 65  ccurred, transfe
2d0f0 72 20 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74  r error informat
2d0f1 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 70 46 72 6f  ion.** from pFro
2d0f2 6d 20 74 6f 20 70 54 6f 2e 0a 2a 2f 0a 73 74 61  m to pTo..*/.sta
2d0f3 74 69 63 20 76 6f 69 64 20 74 72 61 6e 73 66 65  tic void transfe
2d0f4 72 50 61 72 73 65 45 72 72 6f 72 28 50 61 72 73  rParseError(Pars
2d0f5 65 20 2a 70 54 6f 2c 20 50 61 72 73 65 20 2a 70  e *pTo, Parse *p
2d0f6 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  From){.  assert(
2d0f7 20 70 46 72 6f 6d 2d 3e 7a 45 72 72 4d 73 67 3d   pFrom->zErrMsg=
2d0f8 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 6e 45 72  =0 || pFrom->nEr
2d0f9 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
2d0fa 54 6f 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 7c  To->zErrMsg==0 |
2d0fb 7c 20 70 54 6f 2d 3e 6e 45 72 72 20 29 3b 0a 20  | pTo->nErr );. 
2d0fc 20 69 66 28 20 70 54 6f 2d 3e 6e 45 72 72 3d 3d   if( pTo->nErr==
2d0fd 30 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 45  0 ){.    pTo->zE
2d0fe 72 72 4d 73 67 20 3d 20 70 46 72 6f 6d 2d 3e 7a  rrMsg = pFrom->z
2d0ff 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 54 6f 2d  ErrMsg;.    pTo-
2d100 3e 6e 45 72 72 20 3d 20 70 46 72 6f 6d 2d 3e 6e  >nErr = pFrom->n
2d101 45 72 72 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Err;.  }else{.  
2d102 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2d103 70 46 72 6f 6d 2d 3e 64 62 2c 20 70 46 72 6f 6d  pFrom->db, pFrom
2d104 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a  ->zErrMsg);.  }.
2d105 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
2d106 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 20 6e  and populate a n
2d107 65 77 20 54 72 69 67 67 65 72 50 72 67 20 6f 62  ew TriggerPrg ob
2d108 6a 65 63 74 20 77 69 74 68 20 61 20 73 75 62 2d  ject with a sub-
2d109 70 72 6f 67 72 61 6d 20 0a 2a 2a 20 69 6d 70 6c  program .** impl
2d10a 65 6d 65 6e 74 69 6e 67 20 74 72 69 67 67 65 72  ementing trigger
2d10b 20 70 54 72 69 67 67 65 72 20 77 69 74 68 20 4f   pTrigger with O
2d10c 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63  N CONFLICT polic
2d10d 79 20 6f 72 63 6f 6e 66 2e 0a 2a 2f 0a 73 74 61  y orconf..*/.sta
2d10e 74 69 63 20 54 72 69 67 67 65 72 50 72 67 20 2a  tic TriggerPrg *
2d10f 63 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 0a  codeRowTrigger(.
2d110 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2d111 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
2d112 74 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20  t parse context 
2d113 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
2d114 72 69 67 67 65 72 2c 20 20 20 2f 2a 20 54 72 69  rigger,   /* Tri
2d115 67 67 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a  gger to code */.
2d116 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2d117 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2d118 62 6c 65 20 70 54 72 69 67 67 65 72 20 69 73 20  ble pTrigger is 
2d119 61 74 74 61 63 68 65 64 20 74 6f 20 2a 2f 0a 20  attached to */. 
2d11a 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20   int orconf     
2d11b 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46        /* ON CONF
2d11c 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 6f 20 63  LICT policy to c
2d11d 6f 64 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  ode trigger prog
2d11e 72 61 6d 20 77 69 74 68 20 2a 2f 0a 29 7b 0a 20  ram with */.){. 
2d11f 20 50 61 72 73 65 20 2a 70 54 6f 70 20 3d 20 73   Parse *pTop = s
2d120 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
2d121 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 73  vel(pParse);.  s
2d122 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2d123 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 44 61  rse->db;   /* Da
2d124 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
2d125 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70  .  TriggerPrg *p
2d126 50 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Prg;           /
2d127 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
2d128 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  n */.  Expr *pWh
2d129 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  en = 0;         
2d12a 20 20 20 2f 2a 20 44 75 70 6c 69 63 61 74 65 20     /* Duplicate 
2d12b 6f 66 20 74 72 69 67 67 65 72 20 57 48 45 4e 20  of trigger WHEN 
2d12c 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
2d12d 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
2d12e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2d12f 65 6d 70 6f 72 61 72 79 20 56 4d 20 2a 2f 0a 20  emporary VM */. 
2d130 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
2d131 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2d132 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  Name context for
2d133 20 73 75 62 2d 76 64 62 65 20 2a 2f 0a 20 20 53   sub-vdbe */.  S
2d134 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67  ubProgram *pProg
2d135 72 61 6d 20 3d 20 30 3b 20 20 20 2f 2a 20 53 75  ram = 0;   /* Su
2d136 62 2d 76 64 62 65 20 66 6f 72 20 74 72 69 67 67  b-vdbe for trigg
2d137 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  er program */.  
2d138 50 61 72 73 65 20 2a 70 53 75 62 50 61 72 73 65  Parse *pSubParse
2d139 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ;           /* P
2d13a 61 72 73 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  arse context for
2d13b 20 73 75 62 2d 76 64 62 65 20 2a 2f 0a 20 20 69   sub-vdbe */.  i
2d13c 6e 74 20 69 45 6e 64 54 72 69 67 67 65 72 20 3d  nt iEndTrigger =
2d13d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61   0;        /* La
2d13e 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69  bel to jump to i
2d13f 66 20 57 48 45 4e 20 69 73 20 66 61 6c 73 65 20  f WHEN is false 
2d140 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54  */..  assert( pT
2d141 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3d 3d 30  rigger->zName==0
2d142 20 7c 7c 20 70 54 61 62 3d 3d 74 61 62 6c 65 4f   || pTab==tableO
2d143 66 54 72 69 67 67 65 72 28 70 54 72 69 67 67 65  fTrigger(pTrigge
2d144 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  r) );..  /* Allo
2d145 63 61 74 65 20 74 68 65 20 54 72 69 67 67 65 72  cate the Trigger
2d146 50 72 67 20 61 6e 64 20 53 75 62 50 72 6f 67 72  Prg and SubProgr
2d147 61 6d 20 6f 62 6a 65 63 74 73 2e 20 54 6f 20 65  am objects. To e
2d148 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 79 0a  nsure that they.
2d149 20 20 2a 2a 20 61 72 65 20 66 72 65 65 64 20 69    ** are freed i
2d14a 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
2d14b 73 2c 20 6c 69 6e 6b 20 74 68 65 6d 20 69 6e 74  s, link them int
2d14c 6f 20 74 68 65 20 50 61 72 73 65 2e 70 54 72 69  o the Parse.pTri
2d14d 67 67 65 72 50 72 67 20 0a 20 20 2a 2a 20 6c 69  ggerPrg .  ** li
2d14e 73 74 20 6f 66 20 74 68 65 20 74 6f 70 2d 6c 65  st of the top-le
2d14f 76 65 6c 20 50 61 72 73 65 20 6f 62 6a 65 63 74  vel Parse object
2d150 20 73 6f 6f 6e 65 72 20 72 61 74 68 65 72 20 74   sooner rather t
2d151 68 61 6e 20 6c 61 74 65 72 2e 20 20 2a 2f 0a 20  han later.  */. 
2d152 20 70 50 72 67 20 3d 20 73 71 6c 69 74 65 33 44   pPrg = sqlite3D
2d153 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2d154 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 50 72  sizeof(TriggerPr
2d155 67 29 29 3b 0a 20 20 69 66 28 20 21 70 50 72 67  g));.  if( !pPrg
2d156 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
2d157 50 72 67 2d 3e 70 4e 65 78 74 20 3d 20 70 54 6f  Prg->pNext = pTo
2d158 70 2d 3e 70 54 72 69 67 67 65 72 50 72 67 3b 0a  p->pTriggerPrg;.
2d159 20 20 70 54 6f 70 2d 3e 70 54 72 69 67 67 65 72    pTop->pTrigger
2d15a 50 72 67 20 3d 20 70 50 72 67 3b 0a 20 20 70 50  Prg = pPrg;.  pP
2d15b 72 67 2d 3e 70 50 72 6f 67 72 61 6d 20 3d 20 70  rg->pProgram = p
2d15c 50 72 6f 67 72 61 6d 20 3d 20 73 71 6c 69 74 65  Program = sqlite
2d15d 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2d15e 2c 20 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67  , sizeof(SubProg
2d15f 72 61 6d 29 29 3b 0a 20 20 69 66 28 20 21 70 50  ram));.  if( !pP
2d160 72 6f 67 72 61 6d 20 29 20 72 65 74 75 72 6e 20  rogram ) return 
2d161 30 3b 0a 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e  0;.  pProgram->n
2d162 52 65 66 20 3d 20 31 3b 0a 20 20 70 50 72 67 2d  Ref = 1;.  pPrg-
2d163 3e 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 69  >pTrigger = pTri
2d164 67 67 65 72 3b 0a 20 20 70 50 72 67 2d 3e 6f 72  gger;.  pPrg->or
2d165 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20  conf = orconf;. 
2d166 20 70 50 72 67 2d 3e 6f 6c 64 6d 61 73 6b 20 3d   pPrg->oldmask =
2d167 20 30 78 66 66 66 66 66 66 66 66 3b 0a 0a 20 20   0xffffffff;..  
2d168 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20  /* Allocate and 
2d169 70 6f 70 75 6c 61 74 65 20 61 20 6e 65 77 20 50  populate a new P
2d16a 61 72 73 65 20 63 6f 6e 74 65 78 74 20 74 6f 20  arse context to 
2d16b 75 73 65 20 66 6f 72 20 63 6f 64 69 6e 67 20 74  use for coding t
2d16c 68 65 20 0a 20 20 2a 2a 20 74 72 69 67 67 65 72  he .  ** trigger
2d16d 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 20 2a   sub-program.  *
2d16e 2f 0a 20 20 70 53 75 62 50 61 72 73 65 20 3d 20  /.  pSubParse = 
2d16f 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
2d170 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2d171 28 50 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20  (Parse));.  if( 
2d172 21 70 53 75 62 50 61 72 73 65 20 29 20 72 65 74  !pSubParse ) ret
2d173 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28  urn 0;.  memset(
2d174 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
2d175 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61  sNC));.  sNC.pPa
2d176 72 73 65 20 3d 20 70 53 75 62 50 61 72 73 65 3b  rse = pSubParse;
2d177 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 64 62  .  pSubParse->db
2d178 20 3d 20 64 62 3b 0a 20 20 70 53 75 62 50 61 72   = db;.  pSubPar
2d179 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
2d17a 3d 20 70 54 61 62 3b 0a 20 20 70 53 75 62 50 61  = pTab;.  pSubPa
2d17b 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3d  rse->pToplevel =
2d17c 20 70 54 6f 70 3b 0a 20 20 70 53 75 62 50 61 72   pTop;.  pSubPar
2d17d 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
2d17e 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61   = pTrigger->zNa
2d17f 6d 65 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d  me;.  pSubParse-
2d180 3e 65 54 72 69 67 67 65 72 4f 70 20 3d 20 70 54  >eTriggerOp = pT
2d181 72 69 67 67 65 72 2d 3e 6f 70 3b 0a 0a 20 20 76  rigger->op;..  v
2d182 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2d183 65 28 70 53 75 62 50 61 72 73 65 29 3b 0a 20 20  e(pSubParse);.  
2d184 69 66 28 20 76 20 29 7b 0a 20 20 20 20 56 64 62  if( v ){.    Vdb
2d185 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53 74  eComment((v, "St
2d186 61 72 74 3a 20 25 73 2e 25 73 20 28 25 73 20 25  art: %s.%s (%s %
2d187 73 25 73 25 73 20 4f 4e 20 25 73 29 22 2c 20 0a  s%s%s ON %s)", .
2d188 20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e        pTrigger->
2d189 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f 72 54 65  zName, onErrorTe
2d18a 78 74 28 6f 72 63 6f 6e 66 29 2c 0a 20 20 20 20  xt(orconf),.    
2d18b 20 20 28 70 54 72 69 67 67 65 72 2d 3e 74 72 5f    (pTrigger->tr_
2d18c 74 6d 3d 3d 54 52 49 47 47 45 52 5f 42 45 46 4f  tm==TRIGGER_BEFO
2d18d 52 45 20 3f 20 22 42 45 46 4f 52 45 22 20 3a 20  RE ? "BEFORE" : 
2d18e 22 41 46 54 45 52 22 29 2c 0a 20 20 20 20 20 20  "AFTER"),.      
2d18f 20 20 28 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d    (pTrigger->op=
2d190 3d 54 4b 5f 55 50 44 41 54 45 20 3f 20 22 55 50  =TK_UPDATE ? "UP
2d191 44 41 54 45 22 20 3a 20 22 22 29 2c 0a 20 20 20  DATE" : ""),.   
2d192 20 20 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e       (pTrigger->
2d193 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 3f 20  op==TK_INSERT ? 
2d194 22 49 4e 53 45 52 54 22 20 3a 20 22 22 29 2c 0a  "INSERT" : ""),.
2d195 20 20 20 20 20 20 20 20 28 70 54 72 69 67 67 65          (pTrigge
2d196 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45  r->op==TK_DELETE
2d197 20 3f 20 22 44 45 4c 45 54 45 22 20 3a 20 22 22   ? "DELETE" : ""
2d198 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  ),.      pTab->z
2d199 4e 61 6d 65 0a 20 20 20 20 29 29 3b 0a 23 69 66  Name.    ));.#if
2d19a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2d19b 5f 54 52 41 43 45 0a 20 20 20 20 73 71 6c 69 74  _TRACE.    sqlit
2d19c 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2d19d 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 73 71 6c  , -1, .      sql
2d19e 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2d19f 22 2d 2d 20 54 52 49 47 47 45 52 20 25 73 22 2c  "-- TRIGGER %s",
2d1a0 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2d1a1 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43 0a 20 20  ), P4_DYNAMIC.  
2d1a2 20 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20    );.#endif..   
2d1a3 20 2f 2a 20 49 66 20 6f 6e 65 20 77 61 73 20 73   /* If one was s
2d1a4 70 65 63 69 66 69 65 64 2c 20 63 6f 64 65 20 74  pecified, code t
2d1a5 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 2e 20  he WHEN clause. 
2d1a6 49 66 20 69 74 20 65 76 61 6c 75 61 74 65 73 20  If it evaluates 
2d1a7 74 6f 20 66 61 6c 73 65 0a 20 20 20 20 2a 2a 20  to false.    ** 
2d1a8 28 6f 72 20 4e 55 4c 4c 29 20 74 68 65 20 73 75  (or NULL) the su
2d1a9 62 2d 76 64 62 65 20 69 73 20 69 6d 6d 65 64 69  b-vdbe is immedi
2d1aa 61 74 65 6c 79 20 68 61 6c 74 65 64 20 62 79 20  ately halted by 
2d1ab 6a 75 6d 70 69 6e 67 20 74 6f 20 74 68 65 20 0a  jumping to the .
2d1ac 20 20 20 20 2a 2a 20 4f 50 5f 48 61 6c 74 20 69      ** OP_Halt i
2d1ad 6e 73 65 72 74 65 64 20 61 74 20 74 68 65 20 65  nserted at the e
2d1ae 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61  nd of the progra
2d1af 6d 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  m.  */.    if( p
2d1b0 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20 29  Trigger->pWhen )
2d1b1 7b 0a 20 20 20 20 20 20 70 57 68 65 6e 20 3d 20  {.      pWhen = 
2d1b2 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2d1b3 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68  b, pTrigger->pWh
2d1b4 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  en, 0);.      if
2d1b5 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c  ( SQLITE_OK==sql
2d1b6 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
2d1b7 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 6e  ames(&sNC, pWhen
2d1b8 29 20 0a 20 20 20 20 20 20 20 26 26 20 64 62 2d  ) .       && db-
2d1b9 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30  >mallocFailed==0
2d1ba 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20   .      ){.     
2d1bb 20 20 20 69 45 6e 64 54 72 69 67 67 65 72 20 3d     iEndTrigger =
2d1bc 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2d1bd 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
2d1be 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
2d1bf 61 6c 73 65 28 70 53 75 62 50 61 72 73 65 2c 20  alse(pSubParse, 
2d1c0 70 57 68 65 6e 2c 20 69 45 6e 64 54 72 69 67 67  pWhen, iEndTrigg
2d1c1 65 72 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  er, SQLITE_JUMPI
2d1c2 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a  FNULL);.      }.
2d1c3 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2d1c4 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65  rDelete(db, pWhe
2d1c5 6e 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  n);.    }..    /
2d1c6 2a 20 43 6f 64 65 20 74 68 65 20 74 72 69 67 67  * Code the trigg
2d1c7 65 72 20 70 72 6f 67 72 61 6d 20 69 6e 74 6f 20  er program into 
2d1c8 74 68 65 20 73 75 62 2d 76 64 62 65 2e 20 2a 2f  the sub-vdbe. */
2d1c9 0a 20 20 20 20 63 6f 64 65 54 72 69 67 67 65 72  .    codeTrigger
2d1ca 50 72 6f 67 72 61 6d 28 70 53 75 62 50 61 72 73  Program(pSubPars
2d1cb 65 2c 20 70 54 72 69 67 67 65 72 2d 3e 73 74 65  e, pTrigger->ste
2d1cc 70 5f 6c 69 73 74 2c 20 6f 72 63 6f 6e 66 29 3b  p_list, orconf);
2d1cd 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20  ..    /* Insert 
2d1ce 61 6e 20 4f 50 5f 48 61 6c 74 20 61 74 20 74 68  an OP_Halt at th
2d1cf 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 75 62  e end of the sub
2d1d0 2d 70 72 6f 67 72 61 6d 2e 20 2a 2f 0a 20 20 20  -program. */.   
2d1d1 20 69 66 28 20 69 45 6e 64 54 72 69 67 67 65 72   if( iEndTrigger
2d1d2 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2d1d3 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2d1d4 6c 28 76 2c 20 69 45 6e 64 54 72 69 67 67 65 72  l(v, iEndTrigger
2d1d5 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2d1d6 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
2d1d7 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a 20 20 20 20  , OP_Halt);.    
2d1d8 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2d1d9 22 45 6e 64 3a 20 25 73 2e 25 73 22 2c 20 70 54  "End: %s.%s", pT
2d1da 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 6f  rigger->zName, o
2d1db 6e 45 72 72 6f 72 54 65 78 74 28 6f 72 63 6f 6e  nErrorText(orcon
2d1dc 66 29 29 29 3b 0a 0a 20 20 20 20 74 72 61 6e 73  f)));..    trans
2d1dd 66 65 72 50 61 72 73 65 45 72 72 6f 72 28 70 50  ferParseError(pP
2d1de 61 72 73 65 2c 20 70 53 75 62 50 61 72 73 65 29  arse, pSubParse)
2d1df 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
2d1e0 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b  llocFailed==0 ){
2d1e1 0a 20 20 20 20 20 20 70 50 72 6f 67 72 61 6d 2d  .      pProgram-
2d1e2 3e 61 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  >aOp = sqlite3Vd
2d1e3 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 76 2c  beTakeOpArray(v,
2d1e4 20 26 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 2c   &pProgram->nOp,
2d1e5 20 26 70 54 6f 70 2d 3e 6e 4d 61 78 41 72 67 29   &pTop->nMaxArg)
2d1e6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 72 6f  ;.    }.    pPro
2d1e7 67 72 61 6d 2d 3e 6e 4d 65 6d 20 3d 20 70 53 75  gram->nMem = pSu
2d1e8 62 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  bParse->nMem;.  
2d1e9 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72    pProgram->nCsr
2d1ea 20 3d 20 70 53 75 62 50 61 72 73 65 2d 3e 6e 54   = pSubParse->nT
2d1eb 61 62 3b 0a 20 20 20 20 70 50 72 6f 67 72 61 6d  ab;.    pProgram
2d1ec 2d 3e 74 6f 6b 65 6e 20 3d 20 28 76 6f 69 64 20  ->token = (void 
2d1ed 2a 29 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20  *)pTrigger;.    
2d1ee 70 50 72 67 2d 3e 6f 6c 64 6d 61 73 6b 20 3d 20  pPrg->oldmask = 
2d1ef 70 53 75 62 50 61 72 73 65 2d 3e 6f 6c 64 6d 61  pSubParse->oldma
2d1f0 73 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  sk;.    sqlite3V
2d1f1 64 62 65 44 65 6c 65 74 65 28 76 29 3b 0a 20 20  dbeDelete(v);.  
2d1f2 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 53  }..  assert( !pS
2d1f3 75 62 50 61 72 73 65 2d 3e 70 41 69 6e 63 20 20  ubParse->pAinc  
2d1f4 20 20 20 20 20 26 26 20 21 70 53 75 62 50 61 72       && !pSubPar
2d1f5 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 29  se->pZombieTab )
2d1f6 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 53 75  ;.  assert( !pSu
2d1f7 62 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  bParse->pTrigger
2d1f8 50 72 67 20 26 26 20 21 70 53 75 62 50 61 72 73  Prg && !pSubPars
2d1f9 65 2d 3e 6e 4d 61 78 41 72 67 20 29 3b 0a 20 20  e->nMaxArg );.  
2d1fa 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65  sqlite3StackFree
2d1fb 28 64 62 2c 20 70 53 75 62 50 61 72 73 65 29 3b  (db, pSubParse);
2d1fc 0a 0a 20 20 72 65 74 75 72 6e 20 70 50 72 67 3b  ..  return pPrg;
2d1fd 0a 7d 0a 20 20 20 20 0a 2f 2a 0a 2a 2a 20 52 65  .}.    ./*.** Re
2d1fe 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
2d1ff 6f 20 61 20 54 72 69 67 67 65 72 50 72 67 20 6f  o a TriggerPrg o
2d200 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 69 6e 67  bject containing
2d201 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d   the sub-program
2d202 20 66 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72 20   for.** trigger 
2d203 70 54 72 69 67 67 65 72 20 77 69 74 68 20 64 65  pTrigger with de
2d204 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43  fault ON CONFLIC
2d205 54 20 61 6c 67 6f 72 69 74 68 6d 20 6f 72 63 6f  T algorithm orco
2d206 6e 66 2e 20 49 66 20 6e 6f 20 73 75 63 68 0a 2a  nf. If no such.*
2d207 2a 20 54 72 69 67 67 65 72 50 72 67 20 6f 62 6a  * TriggerPrg obj
2d208 65 63 74 20 65 78 69 73 74 73 2c 20 61 20 6e 65  ect exists, a ne
2d209 77 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f  w object is allo
2d20a 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61  cated and popula
2d20b 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 62 65  ted before.** be
2d20c 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f  ing returned..*/
2d20d 0a 73 74 61 74 69 63 20 54 72 69 67 67 65 72 50  .static TriggerP
2d20e 72 67 20 2a 67 65 74 52 6f 77 54 72 69 67 67 65  rg *getRowTrigge
2d20f 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
2d210 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 43 75 72  se,       /* Cur
2d211 72 65 6e 74 20 70 61 72 73 65 20 63 6f 6e 74 65  rent parse conte
2d212 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20  xt */.  Trigger 
2d213 2a 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a 20  *pTrigger,   /* 
2d214 54 72 69 67 67 65 72 20 74 6f 20 63 6f 64 65 20  Trigger to code 
2d215 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2d216 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2d217 20 74 61 62 6c 65 20 74 72 69 67 67 65 72 20 70   table trigger p
2d218 54 72 69 67 67 65 72 20 69 73 20 61 74 74 61 63  Trigger is attac
2d219 68 65 64 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  hed to */.  int 
2d21a 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20  orconf          
2d21b 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20   /* ON CONFLICT 
2d21c 61 6c 67 6f 72 69 74 68 6d 2e 20 2a 2f 0a 29 7b  algorithm. */.){
2d21d 0a 20 20 50 61 72 73 65 20 2a 70 52 6f 6f 74 20  .  Parse *pRoot 
2d21e 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
2d21f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
2d220 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 50    TriggerPrg *pP
2d221 72 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  rg;..  assert( p
2d222 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3d 3d  Trigger->zName==
2d223 30 20 7c 7c 20 70 54 61 62 3d 3d 74 61 62 6c 65  0 || pTab==table
2d224 4f 66 54 72 69 67 67 65 72 28 70 54 72 69 67 67  OfTrigger(pTrigg
2d225 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 74 20  er) );..  /* It 
2d226 6d 61 79 20 62 65 20 74 68 61 74 20 74 68 69 73  may be that this
2d227 20 74 72 69 67 67 65 72 20 68 61 73 20 61 6c 72   trigger has alr
2d228 65 61 64 79 20 62 65 65 6e 20 63 6f 64 65 64 20  eady been coded 
2d229 28 6f 72 20 69 73 20 69 6e 20 74 68 65 0a 20 20  (or is in the.  
2d22a 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 62 65  ** process of be
2d22b 69 6e 67 20 63 6f 64 65 64 29 2e 20 49 66 20 74  ing coded). If t
2d22c 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c  his is the case,
2d22d 20 74 68 65 6e 20 61 6e 20 65 6e 74 72 79 20 77   then an entry w
2d22e 69 74 68 0a 20 20 2a 2a 20 61 20 6d 61 74 63 68  ith.  ** a match
2d22f 69 6e 67 20 54 72 69 67 67 65 72 50 72 67 2e 70  ing TriggerPrg.p
2d230 54 72 69 67 67 65 72 20 66 69 65 6c 64 20 77 69  Trigger field wi
2d231 6c 6c 20 62 65 20 70 72 65 73 65 6e 74 20 73 6f  ll be present so
2d232 6d 65 77 68 65 72 65 0a 20 20 2a 2a 20 69 6e 20  mewhere.  ** in 
2d233 74 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 67  the Parse.pTrigg
2d234 65 72 50 72 67 20 6c 69 73 74 2e 20 53 65 61 72  erPrg list. Sear
2d235 63 68 20 66 6f 72 20 73 75 63 68 20 61 6e 20 65  ch for such an e
2d236 6e 74 72 79 2e 20 20 2a 2f 0a 20 20 66 6f 72 28  ntry.  */.  for(
2d237 70 50 72 67 3d 70 52 6f 6f 74 2d 3e 70 54 72 69  pPrg=pRoot->pTri
2d238 67 67 65 72 50 72 67 3b 20 0a 20 20 20 20 20 20  ggerPrg; .      
2d239 70 50 72 67 20 26 26 20 28 70 50 72 67 2d 3e 70  pPrg && (pPrg->p
2d23a 54 72 69 67 67 65 72 21 3d 70 54 72 69 67 67 65  Trigger!=pTrigge
2d23b 72 20 7c 7c 20 70 50 72 67 2d 3e 6f 72 63 6f 6e  r || pPrg->orcon
2d23c 66 21 3d 6f 72 63 6f 6e 66 29 3b 20 0a 20 20 20  f!=orconf); .   
2d23d 20 20 20 70 50 72 67 3d 70 50 72 67 2d 3e 70 4e     pPrg=pPrg->pN
2d23e 65 78 74 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49  ext.  );..  /* I
2d23f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 54 72  f an existing Tr
2d240 69 67 67 65 72 50 72 67 20 63 6f 75 6c 64 20 6e  iggerPrg could n
2d241 6f 74 20 62 65 20 6c 6f 63 61 74 65 64 2c 20 63  ot be located, c
2d242 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 2e  reate a new one.
2d243 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 72 67 20   */.  if( !pPrg 
2d244 29 7b 0a 20 20 20 20 70 50 72 67 20 3d 20 63 6f  ){.    pPrg = co
2d245 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  deRowTrigger(pPa
2d246 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 70  rse, pTrigger, p
2d247 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20  Tab, orconf);.  
2d248 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 50 72 67  }..  return pPrg
2d249 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
2d24a 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65  ate code for the
2d24b 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
2d24c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2d24d 20 74 72 69 67 67 65 72 20 70 20 6f 6e 20 0a 2a   trigger p on .*
2d24e 2a 20 74 61 62 6c 65 20 70 54 61 62 2e 20 54 68  * table pTab. Th
2d24f 65 20 72 65 67 2c 20 6f 72 63 6f 6e 66 20 61 6e  e reg, orconf an
2d250 64 20 69 67 6e 6f 72 65 4a 75 6d 70 20 70 61 72  d ignoreJump par
2d251 61 6d 65 74 65 72 73 20 70 61 73 73 65 64 20 74  ameters passed t
2d252 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69  o this.** functi
2d253 6f 6e 20 61 72 65 20 74 68 65 20 73 61 6d 65 20  on are the same 
2d254 61 73 20 74 68 6f 73 65 20 64 65 73 63 72 69 62  as those describ
2d255 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  ed in the header
2d256 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a   function for.**
2d257 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
2d258 72 69 67 67 65 72 28 29 0a 2a 2f 0a 53 51 4c 49  rigger().*/.SQLI
2d259 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2d25a 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
2d25b 69 67 67 65 72 44 69 72 65 63 74 28 0a 20 20 50  iggerDirect(.  P
2d25c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2d25d 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e      /* Parse con
2d25e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65  text */.  Trigge
2d25f 72 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 2f  r *p,          /
2d260 2a 20 54 72 69 67 67 65 72 20 74 6f 20 63 6f 64  * Trigger to cod
2d261 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  e */.  Table *pT
2d262 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ab,         /* T
2d263 68 65 20 74 61 62 6c 65 20 74 6f 20 63 6f 64 65  he table to code
2d264 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 2a   triggers from *
2d265 2f 0a 20 20 69 6e 74 20 72 65 67 2c 20 20 20 20  /.  int reg,    
2d266 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 20           /* Reg 
2d267 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67  array containing
2d268 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e 45 57 2e 2a   OLD.* and NEW.*
2d269 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74   values */.  int
2d26a 20 6f 72 63 6f 6e 66 2c 20 20 20 20 20 20 20 20   orconf,        
2d26b 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54    /* ON CONFLICT
2d26c 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74   policy */.  int
2d26d 20 69 67 6e 6f 72 65 4a 75 6d 70 20 20 20 20 20   ignoreJump     
2d26e 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e    /* Instruction
2d26f 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20   to jump to for 
2d270 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f  RAISE(IGNORE) */
2d271 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
2d272 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2d273 50 61 72 73 65 29 3b 20 2f 2a 20 4d 61 69 6e 20  Parse); /* Main 
2d274 56 4d 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50  VM */.  TriggerP
2d275 72 67 20 2a 70 50 72 67 3b 0a 20 20 70 50 72 67  rg *pPrg;.  pPrg
2d276 20 3d 20 67 65 74 52 6f 77 54 72 69 67 67 65 72   = getRowTrigger
2d277 28 70 50 61 72 73 65 2c 20 70 2c 20 70 54 61 62  (pParse, p, pTab
2d278 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 61 73 73  , orconf);.  ass
2d279 65 72 74 28 20 70 50 72 67 20 7c 7c 20 70 50 61  ert( pPrg || pPa
2d27a 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 70 50 61  rse->nErr || pPa
2d27b 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
2d27c 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43  ailed );..  /* C
2d27d 6f 64 65 20 74 68 65 20 4f 50 5f 50 72 6f 67 72  ode the OP_Progr
2d27e 61 6d 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65  am opcode in the
2d27f 20 70 61 72 65 6e 74 20 56 44 42 45 2e 20 50 34   parent VDBE. P4
2d280 20 6f 66 20 74 68 65 20 4f 50 5f 50 72 6f 67 72   of the OP_Progr
2d281 61 6d 20 0a 20 20 2a 2a 20 69 73 20 61 20 70 6f  am .  ** is a po
2d282 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 75 62  inter to the sub
2d283 2d 76 64 62 65 20 63 6f 6e 74 61 69 6e 69 6e 67  -vdbe containing
2d284 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f   the trigger pro
2d285 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 69 66 28 20  gram.  */.  if( 
2d286 70 50 72 67 20 29 7b 0a 20 20 20 20 73 71 6c 69  pPrg ){.    sqli
2d287 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2d288 20 4f 50 5f 50 72 6f 67 72 61 6d 2c 20 72 65 67   OP_Program, reg
2d289 2c 20 69 67 6e 6f 72 65 4a 75 6d 70 2c 20 2b 2b  , ignoreJump, ++
2d28a 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20  pParse->nMem);. 
2d28b 20 20 20 70 50 72 67 2d 3e 70 50 72 6f 67 72 61     pPrg->pProgra
2d28c 6d 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 73  m->nRef++;.    s
2d28d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2d28e 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74  P4(v, -1, (const
2d28f 20 63 68 61 72 20 2a 29 70 50 72 67 2d 3e 70 50   char *)pPrg->pP
2d290 72 6f 67 72 61 6d 2c 20 50 34 5f 53 55 42 50 52  rogram, P4_SUBPR
2d291 4f 47 52 41 4d 29 3b 0a 20 20 20 20 56 64 62 65  OGRAM);.    Vdbe
2d292 43 6f 6d 6d 65 6e 74 28 0a 20 20 20 20 20 20 20  Comment(.       
2d293 20 28 76 2c 20 22 43 61 6c 6c 3a 20 25 73 2e 25   (v, "Call: %s.%
2d294 73 22 2c 20 28 70 2d 3e 7a 4e 61 6d 65 3f 70 2d  s", (p->zName?p-
2d295 3e 7a 4e 61 6d 65 3a 22 66 6b 65 79 22 29 2c 20  >zName:"fkey"), 
2d296 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f 72 63 6f  onErrorText(orco
2d297 6e 66 29 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 53  nf)));..    /* S
2d298 65 74 20 74 68 65 20 50 35 20 6f 70 65 72 61 6e  et the P5 operan
2d299 64 20 6f 66 20 74 68 65 20 4f 50 5f 50 72 6f 67  d of the OP_Prog
2d29a 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ram instruction 
2d29b 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 0a 20  to non-zero if. 
2d29c 20 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20     ** recursive 
2d29d 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68  invocation of th
2d29e 69 73 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  is trigger progr
2d29f 61 6d 20 69 73 20 64 69 73 61 6c 6c 6f 77 65 64  am is disallowed
2d2a0 2e 20 52 65 63 75 72 73 69 76 65 0a 20 20 20 20  . Recursive.    
2d2a1 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 69 73  ** invocation is
2d2a2 20 64 69 73 61 6c 6c 6f 77 65 64 20 69 66 20 28   disallowed if (
2d2a3 61 29 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72  a) the sub-progr
2d2a4 61 6d 20 69 73 20 72 65 61 6c 6c 79 20 61 20 74  am is really a t
2d2a5 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20 6e  rigger,.    ** n
2d2a6 6f 74 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79  ot a foreign key
2d2a7 20 61 63 74 69 6f 6e 2c 20 61 6e 64 20 28 62 29   action, and (b)
2d2a8 20 74 68 65 20 66 6c 61 67 20 74 6f 20 65 6e 61   the flag to ena
2d2a9 62 6c 65 20 72 65 63 75 72 73 69 76 65 20 74 72  ble recursive tr
2d2aa 69 67 67 65 72 73 0a 20 20 20 20 2a 2a 20 69 73  iggers.    ** is
2d2ab 20 63 6c 65 61 72 2e 20 20 2a 2f 0a 20 20 20 20   clear.  */.    
2d2ac 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2d2ad 65 50 35 28 76 2c 20 28 75 38 29 28 70 2d 3e 7a  eP5(v, (u8)(p->z
2d2ae 4e 61 6d 65 20 26 26 20 21 28 70 50 61 72 73 65  Name && !(pParse
2d2af 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49  ->db->flags&SQLI
2d2b0 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 29 29  TE_RecTriggers))
2d2b1 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2d2b2 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74  This is called t
2d2b3 6f 20 63 6f 64 65 20 74 68 65 20 72 65 71 75 69  o code the requi
2d2b4 72 65 64 20 46 4f 52 20 45 41 43 48 20 52 4f 57  red FOR EACH ROW
2d2b5 20 74 72 69 67 67 65 72 73 20 66 6f 72 20 61 6e   triggers for an
2d2b6 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 6f 6e   operation.** on
2d2b7 20 74 61 62 6c 65 20 70 54 61 62 2e 20 54 68 65   table pTab. The
2d2b8 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 63 6f   operation to co
2d2b9 64 65 20 74 72 69 67 67 65 72 73 20 66 6f 72 20  de triggers for 
2d2ba 28 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20  (INSERT, UPDATE 
2d2bb 6f 72 20 44 45 4c 45 54 45 29 0a 2a 2a 20 69 73  or DELETE).** is
2d2bc 20 67 69 76 65 6e 20 62 79 20 74 68 65 20 6f 70   given by the op
2d2bd 20 70 61 72 61 6d 61 74 65 72 2e 20 54 68 65 20   paramater. The 
2d2be 74 72 5f 74 6d 20 70 61 72 61 6d 65 74 65 72 20  tr_tm parameter 
2d2bf 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68  determines wheth
2d2c0 65 72 20 74 68 65 0a 2a 2a 20 42 45 46 4f 52 45  er the.** BEFORE
2d2c1 20 6f 72 20 41 46 54 45 52 20 74 72 69 67 67 65   or AFTER trigge
2d2c2 72 73 20 61 72 65 20 63 6f 64 65 64 2e 20 49 66  rs are coded. If
2d2c3 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69   the operation i
2d2c4 73 20 61 6e 20 55 50 44 41 54 45 2c 20 74 68 65  s an UPDATE, the
2d2c5 6e 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 70  n.** parameter p
2d2c6 43 68 61 6e 67 65 73 20 69 73 20 70 61 73 73 65  Changes is passe
2d2c7 64 20 74 68 65 20 6c 69 73 74 20 6f 66 20 63 6f  d the list of co
2d2c8 6c 75 6d 6e 73 20 62 65 69 6e 67 20 6d 6f 64 69  lumns being modi
2d2c9 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  fied..**.** If t
2d2ca 68 65 72 65 20 61 72 65 20 6e 6f 20 74 72 69 67  here are no trig
2d2cb 67 65 72 73 20 74 68 61 74 20 66 69 72 65 20 61  gers that fire a
2d2cc 74 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  t the specified 
2d2cd 74 69 6d 65 20 66 6f 72 20 74 68 65 20 73 70 65  time for the spe
2d2ce 63 69 66 69 65 64 0a 2a 2a 20 6f 70 65 72 61 74  cified.** operat
2d2cf 69 6f 6e 20 6f 6e 20 70 54 61 62 2c 20 74 68 69  ion on pTab, thi
2d2d0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
2d2d1 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  no-op..**.** The
2d2d2 20 72 65 67 20 61 72 67 75 6d 65 6e 74 20 69 73   reg argument is
2d2d3 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
2d2d4 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
2d2d5 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65  array of registe
2d2d6 72 73 20 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 74  rs .** that cont
2d2d7 61 69 6e 20 74 68 65 20 76 61 6c 75 65 73 20 73  ain the values s
2d2d8 75 62 73 74 69 74 75 74 65 64 20 66 6f 72 20 74  ubstituted for t
2d2d9 68 65 20 6e 65 77 2e 2a 20 61 6e 64 20 6f 6c 64  he new.* and old
2d2da 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 0a 2a 2a  .* references.**
2d2db 20 69 6e 20 74 68 65 20 74 72 69 67 67 65 72 20   in the trigger 
2d2dc 70 72 6f 67 72 61 6d 2e 20 49 66 20 4e 20 69 73  program. If N is
2d2dd 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
2d2de 6f 6c 75 6d 6e 73 20 69 6e 20 74 61 62 6c 65 20  olumns in table 
2d2df 70 54 61 62 0a 2a 2a 20 28 61 20 63 6f 70 79 20  pTab.** (a copy 
2d2e0 6f 66 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 2c 20  of pTab->nCol), 
2d2e1 74 68 65 6e 20 72 65 67 69 73 74 65 72 73 20 61  then registers a
2d2e2 72 65 20 70 6f 70 75 6c 61 74 65 64 20 61 73 20  re populated as 
2d2e3 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
2d2e4 20 52 65 67 69 73 74 65 72 20 20 20 20 20 20 20   Register       
2d2e5 43 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 20 2d 2d  Contains.**   --
2d2e6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d2e7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d2e8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d2e9 2d 2d 2d 2d 0a 2a 2a 20 20 20 72 65 67 2b 30 20  ----.**   reg+0 
2d2ea 20 20 20 20 20 20 20 20 20 4f 4c 44 2e 72 6f 77           OLD.row
2d2eb 69 64 0a 2a 2a 20 20 20 72 65 67 2b 31 20 20 20  id.**   reg+1   
2d2ec 20 20 20 20 20 20 20 4f 4c 44 2e 2a 20 76 61 6c         OLD.* val
2d2ed 75 65 20 6f 66 20 6c 65 66 74 2d 6d 6f 73 74 20  ue of left-most 
2d2ee 63 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0a 2a  column of pTab.*
2d2ef 2a 20 20 20 2e 2e 2e 20 20 20 20 20 20 20 20 20  *   ...         
2d2f0 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 72 65 67 2b     ....**   reg+
2d2f1 4e 20 20 20 20 20 20 20 20 20 20 4f 4c 44 2e 2a  N          OLD.*
2d2f2 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 2d   value of right-
2d2f3 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 70  most column of p
2d2f4 54 61 62 0a 2a 2a 20 20 20 72 65 67 2b 4e 2b 31  Tab.**   reg+N+1
2d2f5 20 20 20 20 20 20 20 20 4e 45 57 2e 72 6f 77 69          NEW.rowi
2d2f6 64 0a 2a 2a 20 20 20 72 65 67 2b 4e 2b 32 20 20  d.**   reg+N+2  
2d2f7 20 20 20 20 20 20 4f 4c 44 2e 2a 20 76 61 6c 75        OLD.* valu
2d2f8 65 20 6f 66 20 6c 65 66 74 2d 6d 6f 73 74 20 63  e of left-most c
2d2f9 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0a 2a 2a  olumn of pTab.**
2d2fa 20 20 20 2e 2e 2e 20 20 20 20 20 20 20 20 20 20     ...          
2d2fb 20 20 2e 2e 2e 0a 2a 2a 20 20 20 72 65 67 2b 4e    ....**   reg+N
2d2fc 2b 4e 2b 31 20 20 20 20 20 20 4e 45 57 2e 2a 20  +N+1      NEW.* 
2d2fd 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 2d 6d  value of right-m
2d2fe 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 54  ost column of pT
2d2ff 61 62 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 4f 4e 20  ab.**.** For ON 
2d300 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 73 2c  DELETE triggers,
2d301 20 74 68 65 20 72 65 67 69 73 74 65 72 73 20 63   the registers c
2d302 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4e 45  ontaining the NE
2d303 57 2e 2a 20 76 61 6c 75 65 73 20 77 69 6c 6c 0a  W.* values will.
2d304 2a 2a 20 6e 65 76 65 72 20 62 65 20 61 63 63 65  ** never be acce
2d305 73 73 65 64 20 62 79 20 74 68 65 20 74 72 69 67  ssed by the trig
2d306 67 65 72 20 70 72 6f 67 72 61 6d 2c 20 73 6f 20  ger program, so 
2d307 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c  they are not all
2d308 6f 63 61 74 65 64 20 6f 72 20 0a 2a 2a 20 70 6f  ocated or .** po
2d309 70 75 6c 61 74 65 64 20 62 79 20 74 68 65 20 63  pulated by the c
2d30a 61 6c 6c 65 72 20 28 74 68 65 72 65 20 69 73 20  aller (there is 
2d30b 6e 6f 20 64 61 74 61 20 74 6f 20 70 6f 70 75 6c  no data to popul
2d30c 61 74 65 20 74 68 65 6d 20 77 69 74 68 20 61 6e  ate them with an
2d30d 79 77 61 79 29 2e 20 0a 2a 2a 20 53 69 6d 69 6c  yway). .** Simil
2d30e 61 72 6c 79 2c 20 66 6f 72 20 4f 4e 20 49 4e 53  arly, for ON INS
2d30f 45 52 54 20 74 72 69 67 67 65 72 73 20 74 68 65  ERT triggers the
2d310 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69   values stored i
2d311 6e 20 74 68 65 20 4f 4c 44 2e 2a 20 72 65 67 69  n the OLD.* regi
2d312 73 74 65 72 73 0a 2a 2a 20 61 72 65 20 6e 65 76  sters.** are nev
2d313 65 72 20 61 63 63 65 73 73 65 64 2c 20 61 6e 64  er accessed, and
2d314 20 73 6f 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f   so are not allo
2d315 63 61 74 65 64 20 62 79 20 74 68 65 20 63 61 6c  cated by the cal
2d316 6c 65 72 2e 20 53 6f 2c 20 66 6f 72 20 61 6e 0a  ler. So, for an.
2d317 2a 2a 20 4f 4e 20 49 4e 53 45 52 54 20 74 72 69  ** ON INSERT tri
2d318 67 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65 20  gger, the value 
2d319 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
2d31a 75 6e 63 74 69 6f 6e 20 61 73 20 70 61 72 61 6d  unction as param
2d31b 65 74 65 72 20 72 65 67 0a 2a 2a 20 69 73 20 6e  eter reg.** is n
2d31c 6f 74 20 61 20 72 65 61 64 61 62 6c 65 20 72 65  ot a readable re
2d31d 67 69 73 74 65 72 2c 20 61 6c 74 68 6f 75 67 68  gister, although
2d31e 20 72 65 67 69 73 74 65 72 73 20 28 72 65 67 2b   registers (reg+
2d31f 4e 29 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 28  N) through .** (
2d320 72 65 67 2b 4e 2b 4e 2b 31 29 20 61 72 65 2e 0a  reg+N+N+1) are..
2d321 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20  **.** Parameter 
2d322 6f 72 63 6f 6e 66 20 69 73 20 74 68 65 20 64 65  orconf is the de
2d323 66 61 75 6c 74 20 63 6f 6e 66 6c 69 63 74 20 72  fault conflict r
2d324 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69  esolution algori
2d325 74 68 6d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74  thm for the.** t
2d326 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 74  rigger program t
2d327 6f 20 75 73 65 20 28 52 45 50 4c 41 43 45 2c 20  o use (REPLACE, 
2d328 49 47 4e 4f 52 45 20 65 74 63 2e 29 2e 20 50 61  IGNORE etc.). Pa
2d329 72 61 6d 65 74 65 72 20 69 67 6e 6f 72 65 4a 75  rameter ignoreJu
2d32a 6d 70 0a 2a 2a 20 69 73 20 74 68 65 20 69 6e 73  mp.** is the ins
2d32b 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 63 6f  truction that co
2d32c 6e 74 72 6f 6c 20 73 68 6f 75 6c 64 20 6a 75 6d  ntrol should jum
2d32d 70 20 74 6f 20 69 66 20 61 20 74 72 69 67 67 65  p to if a trigge
2d32e 72 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 72 61 69  r program.** rai
2d32f 73 65 73 20 61 6e 20 49 47 4e 4f 52 45 20 65 78  ses an IGNORE ex
2d330 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ception..*/.SQLI
2d331 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2d332 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
2d333 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a  igger(.  Parse *
2d334 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
2d335 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
2d336 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  /.  Trigger *pTr
2d337 69 67 67 65 72 2c 20 20 20 2f 2a 20 4c 69 73 74  igger,   /* List
2d338 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20   of triggers on 
2d339 74 61 62 6c 65 20 70 54 61 62 20 2a 2f 0a 20 20  table pTab */.  
2d33a 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
2d33b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
2d33c 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53  K_UPDATE, TK_INS
2d33d 45 52 54 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a  ERT, TK_DELETE *
2d33e 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
2d33f 68 61 6e 67 65 73 2c 20 20 2f 2a 20 43 68 61 6e  hanges,  /* Chan
2d340 67 65 73 20 6c 69 73 74 20 66 6f 72 20 61 6e 79  ges list for any
2d341 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67 67   UPDATE OF trigg
2d342 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 74 72 5f  ers */.  int tr_
2d343 74 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  tm,           /*
2d344 20 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f   One of TRIGGER_
2d345 42 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f  BEFORE, TRIGGER_
2d346 41 46 54 45 52 20 2a 2f 0a 20 20 54 61 62 6c 65  AFTER */.  Table
2d347 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
2d348 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
2d349 63 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 72  code triggers fr
2d34a 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 2c  om */.  int reg,
2d34b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d34c 54 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  The first in an 
2d34d 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65  array of registe
2d34e 72 73 20 28 73 65 65 20 61 62 6f 76 65 29 20 2a  rs (see above) *
2d34f 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 2c 20  /.  int orconf, 
2d350 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43           /* ON C
2d351 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a  ONFLICT policy *
2d352 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75  /.  int ignoreJu
2d353 6d 70 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 74  mp       /* Inst
2d354 72 75 63 74 69 6f 6e 20 74 6f 20 6a 75 6d 70 20  ruction to jump 
2d355 74 6f 20 66 6f 72 20 52 41 49 53 45 28 49 47 4e  to for RAISE(IGN
2d356 4f 52 45 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  ORE) */.){.  Tri
2d357 67 67 65 72 20 2a 70 3b 20 20 20 20 20 20 20 20  gger *p;        
2d358 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65    /* Used to ite
2d359 72 61 74 65 20 74 68 72 6f 75 67 68 20 70 54 72  rate through pTr
2d35a 69 67 67 65 72 20 6c 69 73 74 20 2a 2f 0a 0a 20  igger list */.. 
2d35b 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f   assert( op==TK_
2d35c 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b  UPDATE || op==TK
2d35d 5f 49 4e 53 45 52 54 20 7c 7c 20 6f 70 3d 3d 54  _INSERT || op==T
2d35e 4b 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 61 73  K_DELETE );.  as
2d35f 73 65 72 74 28 20 74 72 5f 74 6d 3d 3d 54 52 49  sert( tr_tm==TRI
2d360 47 47 45 52 5f 42 45 46 4f 52 45 20 7c 7c 20 74  GGER_BEFORE || t
2d361 72 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f 41 46  r_tm==TRIGGER_AF
2d362 54 45 52 20 29 3b 0a 20 20 61 73 73 65 72 74 28  TER );.  assert(
2d363 20 28 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 29   (op==TK_UPDATE)
2d364 3d 3d 28 70 43 68 61 6e 67 65 73 21 3d 30 29 20  ==(pChanges!=0) 
2d365 29 3b 0a 0a 20 20 66 6f 72 28 70 3d 70 54 72 69  );..  for(p=pTri
2d366 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  gger; p; p=p->pN
2d367 65 78 74 29 7b 0a 0a 20 20 20 20 2f 2a 20 53 61  ext){..    /* Sa
2d368 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 3a 20 20  nity checking:  
2d369 54 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74  The schema for t
2d36a 68 65 20 74 72 69 67 67 65 72 20 61 6e 64 20 66  he trigger and f
2d36b 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 72 65  or the table are
2d36c 0a 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 64  .    ** always d
2d36d 65 66 69 6e 65 64 2e 20 20 54 68 65 20 74 72 69  efined.  The tri
2d36e 67 67 65 72 20 6d 75 73 74 20 62 65 20 69 6e 20  gger must be in 
2d36f 74 68 65 20 73 61 6d 65 20 73 63 68 65 6d 61 20  the same schema 
2d370 61 73 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20  as the table.   
2d371 20 2a 2a 20 6f 72 20 65 6c 73 65 20 69 74 20 6d   ** or else it m
2d372 75 73 74 20 62 65 20 61 20 54 45 4d 50 20 74 72  ust be a TEMP tr
2d373 69 67 67 65 72 2e 20 2a 2f 0a 20 20 20 20 61 73  igger. */.    as
2d374 73 65 72 74 28 20 70 2d 3e 70 53 63 68 65 6d 61  sert( p->pSchema
2d375 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  !=0 );.    asser
2d376 74 28 20 70 2d 3e 70 54 61 62 53 63 68 65 6d 61  t( p->pTabSchema
2d377 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  !=0 );.    asser
2d378 74 28 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  t( p->pSchema==p
2d379 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 0a 20 20  ->pTabSchema .  
2d37a 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 70 53 63         || p->pSc
2d37b 68 65 6d 61 3d 3d 70 50 61 72 73 65 2d 3e 64 62  hema==pParse->db
2d37c 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61  ->aDb[1].pSchema
2d37d 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65   );..    /* Dete
2d37e 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 77 65  rmine whether we
2d37f 20 73 68 6f 75 6c 64 20 63 6f 64 65 20 74 68 69   should code thi
2d380 73 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20  s trigger */.   
2d381 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20 0a   if( p->op==op .
2d382 20 20 20 20 20 26 26 20 70 2d 3e 74 72 5f 74 6d       && p->tr_tm
2d383 3d 3d 74 72 5f 74 6d 20 0a 20 20 20 20 20 26 26  ==tr_tm .     &&
2d384 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72   checkColumnOver
2d385 6c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c  lap(p->pColumns,
2d386 20 70 43 68 61 6e 67 65 73 29 0a 20 20 20 20 29   pChanges).    )
2d387 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43  {.      sqlite3C
2d388 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44 69 72  odeRowTriggerDir
2d389 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 70  ect(pParse, p, p
2d38a 54 61 62 2c 20 72 65 67 2c 20 6f 72 63 6f 6e 66  Tab, reg, orconf
2d38b 2c 20 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20  , ignoreJump);. 
2d38c 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
2d38d 2a 20 54 72 69 67 67 65 72 73 20 66 69 72 65 64  * Triggers fired
2d38e 20 62 79 20 55 50 44 41 54 45 20 6f 72 20 44 45   by UPDATE or DE
2d38f 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20  LETE statements 
2d390 6d 61 79 20 61 63 63 65 73 73 20 76 61 6c 75 65  may access value
2d391 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 74  s stored.** in t
2d392 68 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d  he old.* pseudo-
2d393 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63  table. This func
2d394 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 33  tion returns a 3
2d395 32 2d 62 69 74 20 62 69 74 6d 61 73 6b 0a 2a 2a  2-bit bitmask.**
2d396 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 69 63   indicating whic
2d397 68 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  h columns of the
2d398 20 6f 6c 64 2e 2a 20 74 61 62 6c 65 20 61 63 74   old.* table act
2d399 75 61 6c 6c 79 20 61 72 65 20 75 73 65 64 20 62  ually are used b
2d39a 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 54  y.** triggers. T
2d39b 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
2d39c 6d 61 79 20 62 65 20 75 73 65 64 20 62 79 20 74  may be used by t
2d39d 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 61 76 6f  he caller to avo
2d39e 69 64 20 68 61 76 69 6e 67 0a 2a 2a 20 74 6f 20  id having.** to 
2d39f 6c 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20  load the entire 
2d3a0 6f 6c 64 2e 2a 20 72 65 63 6f 72 64 20 69 6e 74  old.* record int
2d3a1 6f 20 6d 65 6d 6f 72 79 20 77 68 65 6e 20 65 78  o memory when ex
2d3a2 65 63 75 74 69 6e 67 20 61 6e 20 55 50 44 41 54  ecuting an UPDAT
2d3a3 45 0a 2a 2a 20 6f 72 20 44 45 4c 45 54 45 20 63  E.** or DELETE c
2d3a4 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 42 69  ommand..**.** Bi
2d3a5 74 20 30 20 6f 66 20 74 68 65 20 72 65 74 75 72  t 0 of the retur
2d3a6 6e 65 64 20 6d 61 73 6b 20 69 73 20 73 65 74 20  ned mask is set 
2d3a7 69 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  if the left-most
2d3a8 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a   column of the.*
2d3a9 2a 20 74 61 62 6c 65 20 6d 61 79 20 62 65 20 61  * table may be a
2d3aa 63 63 65 73 73 65 64 20 75 73 69 6e 67 20 61 6e  ccessed using an
2d3ab 20 6f 6c 64 2e 3c 63 6f 6c 3e 20 72 65 66 65 72   old.<col> refer
2d3ac 65 6e 63 65 2e 20 42 69 74 20 31 20 69 73 20 73  ence. Bit 1 is s
2d3ad 65 74 20 69 66 0a 2a 2a 20 74 68 65 20 73 65 63  et if.** the sec
2d3ae 6f 6e 64 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c  ond leftmost col
2d3af 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 65 71  umn value is req
2d3b0 75 69 72 65 64 2c 20 61 6e 64 20 73 6f 20 6f 6e  uired, and so on
2d3b1 2e 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72  . If there.** ar
2d3b2 65 20 6d 6f 72 65 20 74 68 61 6e 20 33 32 20 63  e more than 32 c
2d3b3 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61  olumns in the ta
2d3b4 62 6c 65 2c 20 61 6e 64 20 61 74 20 6c 65 61 73  ble, and at leas
2d3b5 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6c  t one of the col
2d3b6 75 6d 6e 73 0a 2a 2a 20 77 69 74 68 20 61 6e 20  umns.** with an 
2d3b7 69 6e 64 65 78 20 67 72 65 61 74 65 72 20 74 68  index greater th
2d3b8 61 6e 20 33 32 20 6d 61 79 20 62 65 20 61 63 63  an 32 may be acc
2d3b9 65 73 73 65 64 2c 20 30 78 66 66 66 66 66 66 66  essed, 0xfffffff
2d3ba 66 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  f is returned..*
2d3bb 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70  *.** It is not p
2d3bc 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 74 65 72  ossible to deter
2d3bd 6d 69 6e 65 20 69 66 20 74 68 65 20 6f 6c 64 2e  mine if the old.
2d3be 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20  rowid column is 
2d3bf 61 63 63 65 73 73 65 64 0a 2a 2a 20 62 79 20 74  accessed.** by t
2d3c0 72 69 67 67 65 72 73 2e 20 54 68 65 20 63 61 6c  riggers. The cal
2d3c1 6c 65 72 20 6d 75 73 74 20 61 6c 77 61 79 73 20  ler must always 
2d3c2 61 73 73 75 6d 65 20 74 68 61 74 20 69 74 20 69  assume that it i
2d3c3 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69  s..**.** There i
2d3c4 73 20 6e 6f 20 65 71 75 69 76 61 6c 65 6e 74 20  s no equivalent 
2d3c5 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 6e 65 77  function for new
2d3c6 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a  .* references..*
2d3c7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2d3c8 20 75 33 32 20 73 71 6c 69 74 65 33 54 72 69 67   u32 sqlite3Trig
2d3c9 67 65 72 4f 6c 64 6d 61 73 6b 28 0a 20 20 50 61  gerOldmask(.  Pa
2d3ca 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2d3cb 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
2d3cc 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ext */.  Trigger
2d3cd 20 2a 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a   *pTrigger,   /*
2d3ce 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   List of trigger
2d3cf 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20  s on table pTab 
2d3d0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2d3d1 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 43 68 61  Changes,  /* Cha
2d3d2 6e 67 65 73 20 6c 69 73 74 20 66 6f 72 20 61 6e  nges list for an
2d3d3 79 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67  y UPDATE OF trig
2d3d4 67 65 72 73 20 2a 2f 0a 20 20 54 61 62 6c 65 20  gers */.  Table 
2d3d5 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f  *pTab,         /
2d3d6 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 63  * The table to c
2d3d7 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 72 6f  ode triggers fro
2d3d8 6d 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e  m */.  int orcon
2d3d9 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  f           /* D
2d3da 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49  efault ON CONFLI
2d3db 43 54 20 70 6f 6c 69 63 79 20 66 6f 72 20 74 72  CT policy for tr
2d3dc 69 67 67 65 72 20 73 74 65 70 73 20 2a 2f 0a 29  igger steps */.)
2d3dd 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70  {.  const int op
2d3de 20 3d 20 70 43 68 61 6e 67 65 73 20 3f 20 54 4b   = pChanges ? TK
2d3df 5f 55 50 44 41 54 45 20 3a 20 54 4b 5f 44 45 4c  _UPDATE : TK_DEL
2d3e0 45 54 45 3b 0a 20 20 75 33 32 20 6d 61 73 6b 20  ETE;.  u32 mask 
2d3e1 3d 20 30 3b 0a 20 20 54 72 69 67 67 65 72 20 2a  = 0;.  Trigger *
2d3e2 70 3b 0a 0a 20 20 66 6f 72 28 70 3d 70 54 72 69  p;..  for(p=pTri
2d3e3 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  gger; p; p=p->pN
2d3e4 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  ext){.    if( p-
2d3e5 3e 6f 70 3d 3d 6f 70 20 26 26 20 63 68 65 63 6b  >op==op && check
2d3e6 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d  ColumnOverlap(p-
2d3e7 3e 70 43 6f 6c 75 6d 6e 73 2c 70 43 68 61 6e 67  >pColumns,pChang
2d3e8 65 73 29 20 29 7b 0a 20 20 20 20 20 20 54 72 69  es) ){.      Tri
2d3e9 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b 0a 20  ggerPrg *pPrg;. 
2d3ea 20 20 20 20 20 70 50 72 67 20 3d 20 67 65 74 52       pPrg = getR
2d3eb 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
2d3ec 2c 20 70 2c 20 70 54 61 62 2c 20 6f 72 63 6f 6e  , p, pTab, orcon
2d3ed 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50  f);.      if( pP
2d3ee 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61  rg ){.        ma
2d3ef 73 6b 20 7c 3d 20 70 50 72 67 2d 3e 6f 6c 64 6d  sk |= pPrg->oldm
2d3f0 61 73 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ask;.      }.   
2d3f1 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
2d3f2 20 6d 61 73 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66   mask;.}..#endif
2d3f3 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
2d3f4 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2d3f5 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
2d3f6 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 72 69  ***** End of tri
2d3f7 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  gger.c *********
2d3f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d3f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d3fa 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2d3fb 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2d3fc 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a   update.c ******
2d3fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d3fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d3ff 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
2d400 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
2d401 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2d402 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2d403 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2d404 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2d405 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2d406 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2d407 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2d408 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2d409 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2d40a 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2d40b 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2d40c 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2d40d 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2d40e 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2d40f 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2d410 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2d411 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2d412 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2d413 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d414 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d415 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d416 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
2d417 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2d418 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e  ns C code routin
2d419 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c  es that are call
2d41a 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
2d41b 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 55 50  .** to handle UP
2d41c 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e  DATE statements.
2d41d 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
2d41e 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2d41f 54 41 42 4c 45 0a 2f 2a 20 46 6f 72 77 61 72 64  TABLE./* Forward
2d420 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a   declaration */.
2d421 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61  static void upda
2d422 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0a  teVirtualTable(.
2d423 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2d424 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2d425 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2d426 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
2d427 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  ,       /* The v
2d428 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20  irtual table to 
2d429 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20  be modified */. 
2d42a 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2d42b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
2d42c 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
2d42d 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
2d42e 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75  es,  /* The colu
2d42f 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e  mns to change in
2d430 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74   the UPDATE stat
2d431 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  ement */.  Expr 
2d432 2a 70 52 6f 77 69 64 45 78 70 72 2c 20 20 20 20  *pRowidExpr,    
2d433 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73  /* Expression us
2d434 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20  ed to recompute 
2d435 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  the rowid */.  i
2d436 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20  nt *aXRef,      
2d437 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66      /* Mapping f
2d438 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70  rom columns of p
2d439 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69  Tab to entries i
2d43a 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20  n pChanges */.  
2d43b 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20  Expr *pWhere    
2d43c 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c       /* WHERE cl
2d43d 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41  ause of the UPDA
2d43e 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  TE statement */.
2d43f 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  );.#endif /* SQL
2d440 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2d441 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  TABLE */../*.** 
2d442 54 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  The most recentl
2d443 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74  y coded instruct
2d444 69 6f 6e 20 77 61 73 20 61 6e 20 4f 50 5f 43 6f  ion was an OP_Co
2d445 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65  lumn to retrieve
2d446 20 74 68 65 0a 2a 2a 20 69 2d 74 68 20 63 6f 6c   the.** i-th col
2d447 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 70 54 61  umn of table pTa
2d448 62 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  b. This routine 
2d449 73 65 74 73 20 74 68 65 20 50 34 20 70 61 72 61  sets the P4 para
2d44a 6d 65 74 65 72 20 6f 66 20 74 68 65 20 0a 2a 2a  meter of the .**
2d44b 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74 68   OP_Column to th
2d44c 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2c  e default value,
2d44d 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54   if any..**.** T
2d44e 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2d44f 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20   of a column is 
2d450 73 70 65 63 69 66 69 65 64 20 62 79 20 61 20 44  specified by a D
2d451 45 46 41 55 4c 54 20 63 6c 61 75 73 65 20 69 6e  EFAULT clause in
2d452 20 74 68 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20   the .** column 
2d453 64 65 66 69 6e 69 74 69 6f 6e 2e 20 54 68 69 73  definition. This
2d454 20 77 61 73 20 65 69 74 68 65 72 20 73 75 70 70   was either supp
2d455 6c 69 65 64 20 62 79 20 74 68 65 20 75 73 65 72  lied by the user
2d456 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 0a   when the table.
2d457 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2c 20  ** was created, 
2d458 6f 72 20 61 64 64 65 64 20 6c 61 74 65 72 20 74  or added later t
2d459 6f 20 74 68 65 20 74 61 62 6c 65 20 64 65 66 69  o the table defi
2d45a 6e 69 74 69 6f 6e 20 62 79 20 61 6e 20 41 4c 54  nition by an ALT
2d45b 45 52 20 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d 6d  ER TABLE.** comm
2d45c 61 6e 64 2e 20 49 66 20 74 68 65 20 6c 61 74 74  and. If the latt
2d45d 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  er, then the row
2d45e 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20  -records in the 
2d45f 74 61 62 6c 65 20 62 74 72 65 65 20 6f 6e 20 64  table btree on d
2d460 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 63  isk.** may not c
2d461 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20 66  ontain a value f
2d462 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e  or the column an
2d463 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  d the default va
2d464 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66 72  lue, taken.** fr
2d465 6f 6d 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  om the P4 parame
2d466 74 65 72 20 6f 66 20 74 68 65 20 4f 50 5f 43 6f  ter of the OP_Co
2d467 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e  lumn instruction
2d468 2c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  , is returned in
2d469 73 74 65 61 64 2e 0a 2a 2a 20 49 66 20 74 68 65  stead..** If the
2d46a 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e 20 61 6c   former, then al
2d46b 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 61 72  l row-records ar
2d46c 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  e guaranteed to 
2d46d 69 6e 63 6c 75 64 65 20 61 20 76 61 6c 75 65 0a  include a value.
2d46e 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ** for the colum
2d46f 6e 20 61 6e 64 20 74 68 65 20 50 34 20 76 61 6c  n and the P4 val
2d470 75 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  ue is not requir
2d471 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e  ed..**.** Column
2d472 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 63 72 65   definitions cre
2d473 61 74 65 64 20 62 79 20 61 6e 20 41 4c 54 45 52  ated by an ALTER
2d474 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 6d   TABLE command m
2d475 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 0a 2a 2a  ay only have .**
2d476 20 6c 69 74 65 72 61 6c 20 64 65 66 61 75 6c 74   literal default
2d477 20 76 61 6c 75 65 73 20 73 70 65 63 69 66 69 65   values specifie
2d478 64 3a 20 61 20 6e 75 6d 62 65 72 2c 20 6e 75 6c  d: a number, nul
2d479 6c 20 6f 72 20 61 20 73 74 72 69 6e 67 2e 20 28  l or a string. (
2d47a 49 66 20 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f 6d  If a more.** com
2d47b 70 6c 69 63 61 74 65 64 20 64 65 66 61 75 6c 74  plicated default
2d47c 20 65 78 70 72 65 73 73 69 6f 6e 20 76 61 6c 75   expression valu
2d47d 65 20 77 61 73 20 70 72 6f 76 69 64 65 64 2c 20  e was provided, 
2d47e 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 20  it is evaluated 
2d47f 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 4c 54  .** when the ALT
2d480 45 52 20 54 41 42 4c 45 20 69 73 20 65 78 65 63  ER TABLE is exec
2d481 75 74 65 64 20 61 6e 64 20 6f 6e 65 20 6f 66 20  uted and one of 
2d482 74 68 65 20 6c 69 74 65 72 61 6c 20 76 61 6c 75  the literal valu
2d483 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e  es written.** in
2d484 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  to the sqlite_ma
2d485 73 74 65 72 20 74 61 62 6c 65 2e 29 0a 2a 2a 0a  ster table.).**.
2d486 2a 2a 20 54 68 65 72 65 66 6f 72 65 2c 20 74 68  ** Therefore, th
2d487 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69  e P4 parameter i
2d488 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20  s only required 
2d489 69 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  if the default v
2d48a 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20  alue for.** the 
2d48b 63 6f 6c 75 6d 6e 20 69 73 20 61 20 6c 69 74 65  column is a lite
2d48c 72 61 6c 20 6e 75 6d 62 65 72 2c 20 73 74 72 69  ral number, stri
2d48d 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20 54 68 65 20  ng or null. The 
2d48e 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d  sqlite3ValueFrom
2d48f 45 78 70 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69  Expr().** functi
2d490 6f 6e 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66  on is capable of
2d491 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68   transforming th
2d492 65 73 65 20 74 79 70 65 73 20 6f 66 20 65 78 70  ese types of exp
2d493 72 65 73 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a  ressions into.**
2d494 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
2d495 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  bjects..**.** If
2d496 20 70 61 72 61 6d 65 74 65 72 20 69 52 65 67 20   parameter iReg 
2d497 69 73 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c  is not negative,
2d498 20 63 6f 64 65 20 61 6e 20 4f 50 5f 52 65 61 6c   code an OP_Real
2d499 41 66 66 69 6e 69 74 79 20 69 6e 73 74 72 75 63  Affinity instruc
2d49a 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 72 65 67 69 73  tion.** on regis
2d49b 74 65 72 20 69 52 65 67 2e 20 54 68 69 73 20 69  ter iReg. This i
2d49c 73 20 75 73 65 64 20 77 68 65 6e 20 61 6e 20 65  s used when an e
2d49d 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 67 65  quivalent intege
2d49e 72 20 76 61 6c 75 65 20 69 73 20 0a 2a 2a 20 73  r value is .** s
2d49f 74 6f 72 65 64 20 69 6e 20 70 6c 61 63 65 20 6f  tored in place o
2d4a0 66 20 61 6e 20 38 2d 62 79 74 65 20 66 6c 6f 61  f an 8-byte floa
2d4a1 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
2d4a2 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 61 76   in order to sav
2d4a3 65 20 0a 2a 2a 20 73 70 61 63 65 2e 0a 2a 2f 0a  e .** space..*/.
2d4a4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2d4a5 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d  oid sqlite3Colum
2d4a6 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 2a 76  nDefault(Vdbe *v
2d4a7 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69  , Table *pTab, i
2d4a8 6e 74 20 69 2c 20 69 6e 74 20 69 52 65 67 29 7b  nt i, int iReg){
2d4a9 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21  .  assert( pTab!
2d4aa 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 54 61  =0 );.  if( !pTa
2d4ab 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  b->pSelect ){.  
2d4ac 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2d4ad 2a 70 56 61 6c 75 65 3b 0a 20 20 20 20 75 38 20  *pValue;.    u8 
2d4ae 65 6e 63 20 3d 20 45 4e 43 28 73 71 6c 69 74 65  enc = ENC(sqlite
2d4af 33 56 64 62 65 44 62 28 76 29 29 3b 0a 20 20 20  3VdbeDb(v));.   
2d4b0 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20   Column *pCol = 
2d4b1 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a  &pTab->aCol[i];.
2d4b2 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2d4b3 28 76 2c 20 22 25 73 2e 25 73 22 2c 20 70 54 61  (v, "%s.%s", pTa
2d4b4 62 2d 3e 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d 3e  b->zName, pCol->
2d4b5 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61 73 73  zName));.    ass
2d4b6 65 72 74 28 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  ert( i<pTab->nCo
2d4b7 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  l );.    sqlite3
2d4b8 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71  ValueFromExpr(sq
2d4b9 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c 20  lite3VdbeDb(v), 
2d4ba 70 43 6f 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e 63  pCol->pDflt, enc
2d4bb 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2d4bc 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
2d4bd 2d 3e 61 66 66 69 6e 69 74 79 2c 20 26 70 56 61  ->affinity, &pVa
2d4be 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20 70 56  lue);.    if( pV
2d4bf 61 6c 75 65 20 29 7b 0a 20 20 20 20 20 20 73 71  alue ){.      sq
2d4c0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2d4c1 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74 20  4(v, -1, (const 
2d4c2 63 68 61 72 20 2a 29 70 56 61 6c 75 65 2c 20 50  char *)pValue, P
2d4c3 34 5f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 23 69  4_MEM);.    }.#i
2d4c4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2d4c5 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
2d4c6 0a 20 20 20 20 69 66 28 20 69 52 65 67 3e 3d 30  .    if( iReg>=0
2d4c7 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   && pTab->aCol[i
2d4c8 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  ].affinity==SQLI
2d4c9 54 45 5f 41 46 46 5f 52 45 41 4c 20 29 7b 0a 20  TE_AFF_REAL ){. 
2d4ca 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2d4cb 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 61  AddOp1(v, OP_Rea
2d4cc 6c 41 66 66 69 6e 69 74 79 2c 20 69 52 65 67 29  lAffinity, iReg)
2d4cd 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
2d4ce 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63   }.}../*.** Proc
2d4cf 65 73 73 20 61 6e 20 55 50 44 41 54 45 20 73 74  ess an UPDATE st
2d4d0 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
2d4d1 20 55 50 44 41 54 45 20 4f 52 20 49 47 4e 4f 52   UPDATE OR IGNOR
2d4d2 45 20 74 61 62 6c 65 5f 77 78 79 7a 20 53 45 54  E table_wxyz SET
2d4d3 20 61 3d 62 2c 20 63 3d 64 20 57 48 45 52 45 20   a=b, c=d WHERE 
2d4d4 65 3c 35 20 41 4e 44 20 66 20 4e 4f 54 20 4e 55  e<5 AND f NOT NU
2d4d5 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  LL;.**          
2d4d6 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f 5f  \_______/ \_____
2d4d7 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  ___/     \______
2d4d8 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f  /       \_______
2d4d9 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20 20  _________/.*    
2d4da 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20          onError 
2d4db 20 20 70 54 61 62 4c 69 73 74 20 20 20 20 20 20    pTabList      
2d4dc 70 43 68 61 6e 67 65 73 20 20 20 20 20 20 20 20  pChanges        
2d4dd 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53       pWhere.*/.S
2d4de 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2d4df 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 65  id sqlite3Update
2d4e0 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2d4e1 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
2d4e2 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2d4e3 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2d4e4 54 61 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20  TabList,     /* 
2d4e5 54 68 65 20 74 61 62 6c 65 20 69 6e 20 77 68 69  The table in whi
2d4e6 63 68 20 77 65 20 73 68 6f 75 6c 64 20 63 68 61  ch we should cha
2d4e7 6e 67 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20  nge things */.  
2d4e8 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
2d4e9 65 73 2c 20 20 20 20 2f 2a 20 54 68 69 6e 67 73  es,    /* Things
2d4ea 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 20 2a   to be changed *
2d4eb 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
2d4ec 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
2d4ed 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
2d4ee 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a   May be null */.
2d4ef 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20    int onError   
2d4f0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
2d4f1 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72  to handle constr
2d4f2 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29  aint errors */.)
2d4f3 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  {.  int i, j;   
2d4f4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2d4f5 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
2d4f6 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
2d4f7 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
2d4f8 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61 74  able to be updat
2d4f9 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ed */.  int addr
2d4fa 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f   = 0;          /
2d4fb 2a 20 56 44 42 45 20 69 6e 73 74 72 75 63 74 69  * VDBE instructi
2d4fc 6f 6e 20 61 64 64 72 65 73 73 20 6f 66 20 74 68  on address of th
2d4fd 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6c  e start of the l
2d4fe 6f 6f 70 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e  oop */.  WhereIn
2d4ff 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20  fo *pWInfo;     
2d500 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
2d501 62 6f 75 74 20 74 68 65 20 57 48 45 52 45 20 63  bout the WHERE c
2d502 6c 61 75 73 65 20 2a 2f 0a 20 20 56 64 62 65 20  lause */.  Vdbe 
2d503 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2d504 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
2d505 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
2d506 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
2d507 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
2d508 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
2d509 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e   indices */.  in
2d50a 74 20 6e 49 64 78 3b 20 20 20 20 20 20 20 20 20  t nIdx;         
2d50b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2d50c 66 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 6e  f indices that n
2d50d 65 65 64 20 75 70 64 61 74 69 6e 67 20 2a 2f 0a  eed updating */.
2d50e 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
2d50f 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
2d510 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
2d511 66 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c 69  f pTab */.  sqli
2d512 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
2d513 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2d514 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  se structure */.
2d515 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 3d    int *aRegIdx =
2d516 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20   0;      /* One 
2d517 72 65 67 69 73 74 65 72 20 61 73 73 69 67 6e 65  register assigne
2d518 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78 20  d to each index 
2d519 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f  to be updated */
2d51a 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 20 3d 20  .  int *aXRef = 
2d51b 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 58 52  0;        /* aXR
2d51c 65 66 5b 69 5d 20 69 73 20 74 68 65 20 69 6e 64  ef[i] is the ind
2d51d 65 78 20 69 6e 20 70 43 68 61 6e 67 65 73 2d 3e  ex in pChanges->
2d51e 61 5b 5d 20 6f 66 20 74 68 65 0a 20 20 20 20 20  a[] of the.     
2d51f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d520 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73      ** an expres
2d521 73 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d 74  sion for the i-t
2d522 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
2d523 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 20 20 20  table..         
2d524 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d525 2a 2a 20 61 58 52 65 66 5b 69 5d 3d 3d 2d 31 20  ** aXRef[i]==-1 
2d526 69 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75  if the i-th colu
2d527 6d 6e 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  mn is not change
2d528 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 63 68 6e 67  d. */.  int chng
2d529 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 2f  Rowid;         /
2d52a 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 72 65  * True if the re
2d52b 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 62  cord number is b
2d52c 65 69 6e 67 20 63 68 61 6e 67 65 64 20 2a 2f 0a  eing changed */.
2d52d 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 45 78    Expr *pRowidEx
2d52e 70 72 20 3d 20 30 3b 20 20 2f 2a 20 45 78 70 72  pr = 0;  /* Expr
2d52f 65 73 73 69 6f 6e 20 64 65 66 69 6e 69 6e 67 20  ession defining 
2d530 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  the new record n
2d531 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f  umber */.  int o
2d532 70 65 6e 41 6c 6c 20 3d 20 30 3b 20 20 20 20 20  penAll = 0;     
2d533 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c 6c    /* True if all
2d534 20 69 6e 64 69 63 65 73 20 6e 65 65 64 20 74 6f   indices need to
2d535 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
2d536 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e  AuthContext sCon
2d537 74 65 78 74 3b 20 20 2f 2a 20 54 68 65 20 61 75  text;  /* The au
2d538 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  thorization cont
2d539 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  ext */.  NameCon
2d53a 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20  text sNC;       
2d53b 2f 2a 20 54 68 65 20 6e 61 6d 65 2d 63 6f 6e 74  /* The name-cont
2d53c 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20 65  ext to resolve e
2d53d 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f  xpressions in */
2d53e 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
2d53f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2d540 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
2d541 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
2d542 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e   updated */.  in
2d543 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20  t j1;           
2d544 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 65       /* Addresse
2d545 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75  s of jump instru
2d546 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ctions */.  int 
2d547 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20  okOnePass;      
2d548 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6f     /* True for o
2d549 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68  ne-pass algorith
2d54a 6d 20 77 69 74 68 6f 75 74 20 74 68 65 20 46 49  m without the FI
2d54b 46 4f 20 2a 2f 0a 20 20 69 6e 74 20 68 61 73 46  FO */.  int hasF
2d54c 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  K;             /
2d54d 2a 20 54 72 75 65 20 69 66 20 66 6f 72 65 69 67  * True if foreig
2d54e 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e 67  n key processing
2d54f 20 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a   is required */.
2d550 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2d551 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69  OMIT_TRIGGER.  i
2d552 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20 20 20  nt isView;      
2d553 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2d554 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20  rying to update 
2d555 61 20 76 69 65 77 20 2a 2f 0a 20 20 54 72 69 67  a view */.  Trig
2d556 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
2d557 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74           /* List
2d558 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20   of triggers on 
2d559 70 54 61 62 2c 20 69 66 20 72 65 71 75 69 72 65  pTab, if require
2d55a 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  d */.#endif..  /
2d55b 2a 20 52 65 67 69 73 74 65 72 20 41 6c 6c 6f 63  * Register Alloc
2d55c 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ations */.  int 
2d55d 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b  regRowCount = 0;
2d55e 20 20 20 2f 2a 20 41 20 63 6f 75 6e 74 20 6f 66     /* A count of
2d55f 20 72 6f 77 73 20 63 68 61 6e 67 65 64 20 2a 2f   rows changed */
2d560 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77  .  int regOldRow
2d561 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  id;       /* The
2d562 20 6f 6c 64 20 72 6f 77 69 64 20 2a 2f 0a 20 20   old rowid */.  
2d563 69 6e 74 20 72 65 67 4e 65 77 52 6f 77 69 64 3b  int regNewRowid;
2d564 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
2d565 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  w rowid */.  int
2d566 20 72 65 67 4e 65 77 3b 0a 20 20 69 6e 74 20 72   regNew;.  int r
2d567 65 67 4f 6c 64 20 3d 20 30 3b 0a 20 20 69 6e 74  egOld = 0;.  int
2d568 20 72 65 67 52 6f 77 53 65 74 20 3d 20 30 3b 20   regRowSet = 0; 
2d569 20 20 20 20 2f 2a 20 52 6f 77 73 65 74 20 6f 66      /* Rowset of
2d56a 20 72 6f 77 73 20 74 6f 20 62 65 20 75 70 64 61   rows to be upda
2d56b 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ted */.  int reg
2d56c 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
2d56d 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64  /* Register used
2d56e 20 66 6f 72 20 6e 65 77 20 74 61 62 6c 65 20 72   for new table r
2d56f 65 63 6f 72 64 20 74 6f 20 69 6e 73 65 72 74 20  ecord to insert 
2d570 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 43  */..  memset(&sC
2d571 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f  ontext, 0, sizeo
2d572 66 28 73 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20  f(sContext));.  
2d573 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2d574 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
2d575 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
2d576 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
2d577 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2d578 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  up;.  }.  assert
2d579 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  ( pTabList->nSrc
2d57a 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  ==1 );..  /* Loc
2d57b 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 77 68  ate the table wh
2d57c 69 63 68 20 77 65 20 77 61 6e 74 20 74 6f 20 75  ich we want to u
2d57d 70 64 61 74 65 2e 20 0a 20 20 2a 2f 0a 20 20 70  pdate. .  */.  p
2d57e 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
2d57f 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
2d580 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  e, pTabList);.  
2d581 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 67 6f  if( pTab==0 ) go
2d582 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
2d583 70 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  p;.  iDb = sqlit
2d584 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2d585 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
2d586 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f  ->pSchema);..  /
2d587 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20  * Figure out if 
2d588 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69 67  we have any trig
2d589 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65 20  gers and if the 
2d58a 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a  table being.  **
2d58b 20 75 70 64 61 74 65 64 20 69 73 20 61 20 76 69   updated is a vi
2d58c 65 77 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ew..  */.#ifndef
2d58d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
2d58e 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72 20  GGER.  pTrigger 
2d58f 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
2d590 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70  sExist(pParse, p
2d591 54 61 62 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20  Tab, TK_UPDATE, 
2d592 70 43 68 61 6e 67 65 73 2c 20 30 29 3b 0a 20 20  pChanges, 0);.  
2d593 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70  isView = pTab->p
2d594 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65  Select!=0;.#else
2d595 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69 67 67  .# define pTrigg
2d596 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20 69 73  er 0.# define is
2d597 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23 69  View 0.#endif.#i
2d598 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
2d599 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73  _VIEW.# undef is
2d59a 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73  View.# define is
2d59b 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a 20  View 0.#endif.. 
2d59c 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77   if( sqlite3View
2d59d 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  GetColumnNames(p
2d59e 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a  Parse, pTab) ){.
2d59f 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
2d5a0 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
2d5a1 66 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64  f( sqlite3IsRead
2d5a2 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61  Only(pParse, pTa
2d5a3 62 2c 20 28 70 54 72 69 67 67 65 72 3f 31 3a 30  b, (pTrigger?1:0
2d5a4 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75  )) ){.    goto u
2d5a5 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
2d5a6 20 7d 0a 20 20 61 58 52 65 66 20 3d 20 73 71 6c   }.  aXRef = sql
2d5a7 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
2d5a8 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 20  db, sizeof(int) 
2d5a9 2a 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a  * pTab->nCol );.
2d5aa 20 20 69 66 28 20 61 58 52 65 66 3d 3d 30 20 29    if( aXRef==0 )
2d5ab 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
2d5ac 61 6e 75 70 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  anup;.  for(i=0;
2d5ad 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
2d5ae 2b 2b 29 20 61 58 52 65 66 5b 69 5d 20 3d 20 2d  ++) aXRef[i] = -
2d5af 31 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  1;..  /* Allocat
2d5b0 65 20 61 20 63 75 72 73 6f 72 73 20 66 6f 72 20  e a cursors for 
2d5b1 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2d5b2 65 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20  e table and for 
2d5b3 61 6c 6c 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a  all indices..  *
2d5b4 2a 20 54 68 65 20 69 6e 64 65 78 20 63 75 72 73  * The index curs
2d5b5 6f 72 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65  ors might not be
2d5b6 20 75 73 65 64 2c 20 62 75 74 20 69 66 20 74 68   used, but if th
2d5b7 65 79 20 61 72 65 20 75 73 65 64 20 74 68 65 79  ey are used they
2d5b8 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6f 63  .  ** need to oc
2d5b9 63 75 72 20 72 69 67 68 74 20 61 66 74 65 72 20  cur right after 
2d5ba 74 68 65 20 64 61 74 61 62 61 73 65 20 63 75 72  the database cur
2d5bb 73 6f 72 2e 20 20 53 6f 20 67 6f 20 61 68 65 61  sor.  So go ahea
2d5bc 64 20 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 6f 63  d and.  ** alloc
2d5bd 61 74 65 20 65 6e 6f 75 67 68 20 73 70 61 63 65  ate enough space
2d5be 2c 20 6a 75 73 74 20 69 6e 20 63 61 73 65 2e 0a  , just in case..
2d5bf 20 20 2a 2f 0a 20 20 70 54 61 62 4c 69 73 74 2d    */.  pTabList-
2d5c0 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20  >a[0].iCursor = 
2d5c1 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iCur = pParse->n
2d5c2 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64  Tab++;.  for(pId
2d5c3 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2d5c4 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2d5c5 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61  >pNext){.    pPa
2d5c6 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d  rse->nTab++;.  }
2d5c7 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
2d5c8 65 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65  e the name-conte
2d5c9 78 74 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  xt */.  memset(&
2d5ca 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
2d5cb 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72  NC));.  sNC.pPar
2d5cc 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73  se = pParse;.  s
2d5cd 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54  NC.pSrcList = pT
2d5ce 61 62 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65  abList;..  /* Re
2d5cf 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e  solve the column
2d5d0 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 74 68   names in all th
2d5d1 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66  e expressions of
2d5d2 20 74 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65   the.  ** of the
2d5d3 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2d5d4 74 2e 20 20 41 6c 73 6f 20 66 69 6e 64 20 74 68  t.  Also find th
2d5d5 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20  e column index. 
2d5d6 20 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c   ** for each col
2d5d7 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61 74 65  umn to be update
2d5d8 64 20 69 6e 20 74 68 65 20 70 43 68 61 6e 67 65  d in the pChange
2d5d9 73 20 61 72 72 61 79 2e 20 20 46 6f 72 20 65 61  s array.  For ea
2d5da 63 68 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 74  ch.  ** column t
2d5db 6f 20 62 65 20 75 70 64 61 74 65 64 2c 20 6d 61  o be updated, ma
2d5dc 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65 20  ke sure we have 
2d5dd 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f  authorization to
2d5de 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 74 68 61   change.  ** tha
2d5df 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a 20  t column..  */. 
2d5e0 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 30 3b 0a   chngRowid = 0;.
2d5e1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68    for(i=0; i<pCh
2d5e2 61 6e 67 65 73 2d 3e 6e 45 78 70 72 3b 20 69 2b  anges->nExpr; i+
2d5e3 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +){.    if( sqli
2d5e4 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2d5e5 6d 65 73 28 26 73 4e 43 2c 20 70 43 68 61 6e 67  mes(&sNC, pChang
2d5e6 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20  es->a[i].pExpr) 
2d5e7 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75 70  ){.      goto up
2d5e8 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
2d5e9 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b    }.    for(j=0;
2d5ea 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
2d5eb 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
2d5ec 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54  qlite3StrICmp(pT
2d5ed 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  ab->aCol[j].zNam
2d5ee 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69  e, pChanges->a[i
2d5ef 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  ].zName)==0 ){. 
2d5f0 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 54         if( j==pT
2d5f1 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
2d5f2 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77 69 64         chngRowid
2d5f3 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
2d5f4 70 52 6f 77 69 64 45 78 70 72 20 3d 20 70 43 68  pRowidExpr = pCh
2d5f5 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70  anges->a[i].pExp
2d5f6 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  r;.        }.   
2d5f7 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20       aXRef[j] = 
2d5f8 69 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  i;.        break
2d5f9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2d5fa 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d      if( j>=pTab-
2d5fb 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69  >nCol ){.      i
2d5fc 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69  f( sqlite3IsRowi
2d5fd 64 28 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d  d(pChanges->a[i]
2d5fe 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20  .zName) ){.     
2d5ff 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 31     chngRowid = 1
2d600 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 77 69 64  ;.        pRowid
2d601 45 78 70 72 20 3d 20 70 43 68 61 6e 67 65 73 2d  Expr = pChanges-
2d602 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
2d603 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2d604 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2d605 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75  g(pParse, "no su
2d606 63 68 20 63 6f 6c 75 6d 6e 3a 20 25 73 22 2c 20  ch column: %s", 
2d607 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a  pChanges->a[i].z
2d608 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67  Name);.        g
2d609 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2d60a 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  up;.      }.    
2d60b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2d60c 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2d60d 49 4f 4e 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  ION.    {.      
2d60e 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63  int rc;.      rc
2d60f 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68   = sqlite3AuthCh
2d610 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
2d611 54 45 5f 55 50 44 41 54 45 2c 20 70 54 61 62 2d  TE_UPDATE, pTab-
2d612 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  >zName,.        
2d613 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d614 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d     pTab->aCol[j]
2d615 2e 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b  .zName, db->aDb[
2d616 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  iDb].zName);.   
2d617 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2d618 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20 20  E_DENY ){.      
2d619 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2d61a 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 65 6c  eanup;.      }el
2d61b 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  se if( rc==SQLIT
2d61c 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20  E_IGNORE ){.    
2d61d 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 2d      aXRef[j] = -
2d61e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
2d61f 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 68  .#endif.  }..  h
2d620 61 73 46 4b 20 3d 20 73 71 6c 69 74 65 33 46 6b  asFK = sqlite3Fk
2d621 52 65 71 75 69 72 65 64 28 70 50 61 72 73 65 2c  Required(pParse,
2d622 20 70 54 61 62 2c 20 61 58 52 65 66 2c 20 63 68   pTab, aXRef, ch
2d623 6e 67 52 6f 77 69 64 29 3b 0a 0a 20 20 2f 2a 20  ngRowid);..  /* 
2d624 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  Allocate memory 
2d625 66 6f 72 20 74 68 65 20 61 72 72 61 79 20 61 52  for the array aR
2d626 65 67 49 64 78 5b 5d 2e 20 20 54 68 65 72 65 20  egIdx[].  There 
2d627 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20  is one entry in 
2d628 74 68 65 0a 20 20 2a 2a 20 61 72 72 61 79 20 66  the.  ** array f
2d629 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 61 73  or each index as
2d62a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 61  sociated with ta
2d62b 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74 65  ble being update
2d62c 64 2e 20 20 46 69 6c 6c 20 69 6e 0a 20 20 2a 2a  d.  Fill in.  **
2d62d 20 74 68 65 20 76 61 6c 75 65 20 77 69 74 68 20   the value with 
2d62e 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  a register numbe
2d62f 72 20 66 6f 72 20 69 6e 64 69 63 65 73 20 74 68  r for indices th
2d630 61 74 20 61 72 65 20 74 6f 20 62 65 20 75 73 65  at are to be use
2d631 64 0a 20 20 2a 2a 20 61 6e 64 20 77 69 74 68 20  d.  ** and with 
2d632 7a 65 72 6f 20 66 6f 72 20 75 6e 75 73 65 64 20  zero for unused 
2d633 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20  indices..  */.  
2d634 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78  for(nIdx=0, pIdx
2d635 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2d636 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2d637 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d  pNext, nIdx++){}
2d638 0a 20 20 69 66 28 20 6e 49 64 78 3e 30 20 29 7b  .  if( nIdx>0 ){
2d639 0a 20 20 20 20 61 52 65 67 49 64 78 20 3d 20 73  .    aRegIdx = s
2d63a 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2d63b 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e 64  w(db, sizeof(Ind
2d63c 65 78 2a 29 20 2a 20 6e 49 64 78 20 29 3b 0a 20  ex*) * nIdx );. 
2d63d 20 20 20 69 66 28 20 61 52 65 67 49 64 78 3d 3d     if( aRegIdx==
2d63e 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
2d63f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 66  cleanup;.  }.  f
2d640 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61  or(j=0, pIdx=pTa
2d641 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
2d642 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
2d643 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  t, j++){.    int
2d644 20 72 65 67 3b 0a 20 20 20 20 69 66 28 20 63 68   reg;.    if( ch
2d645 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  ngRowid ){.     
2d646 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d   reg = ++pParse-
2d647 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65  >nMem;.    }else
2d648 7b 0a 20 20 20 20 20 20 72 65 67 20 3d 20 30 3b  {.      reg = 0;
2d649 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2d64a 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  i<pIdx->nColumn;
2d64b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
2d64c 66 28 20 61 58 52 65 66 5b 70 49 64 78 2d 3e 61  f( aXRef[pIdx->a
2d64d 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29  iColumn[i]]>=0 )
2d64e 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 20  {.          reg 
2d64f 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2d650 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
2d651 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
2d652 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
2d653 52 65 67 49 64 78 5b 6a 5d 20 3d 20 72 65 67 3b  RegIdx[j] = reg;
2d654 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  .  }..  /* Begin
2d655 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65   generating code
2d656 2e 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74  . */.  v = sqlit
2d657 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2d658 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
2d659 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
2d65a 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73  nup;.  if( pPars
2d65b 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73  e->nested==0 ) s
2d65c 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
2d65d 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c  hanges(v);.  sql
2d65e 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
2d65f 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
2d660 31 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65  1, iDb);..#ifnde
2d661 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2d662 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20  RTUALTABLE.  /* 
2d663 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  Virtual tables m
2d664 75 73 74 20 62 65 20 68 61 6e 64 6c 65 64 20 73  ust be handled s
2d665 65 70 61 72 61 74 65 6c 79 20 2a 2f 0a 20 20 69  eparately */.  i
2d666 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2d667 62 29 20 29 7b 0a 20 20 20 20 75 70 64 61 74 65  b) ){.    update
2d668 56 69 72 74 75 61 6c 54 61 62 6c 65 28 70 50 61  VirtualTable(pPa
2d669 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
2d66a 54 61 62 2c 20 70 43 68 61 6e 67 65 73 2c 20 70  Tab, pChanges, p
2d66b 52 6f 77 69 64 45 78 70 72 2c 20 61 58 52 65 66  RowidExpr, aXRef
2d66c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2d66d 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 29           pWhere)
2d66e 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 30  ;.    pWhere = 0
2d66f 3b 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d  ;.    pTabList =
2d670 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64   0;.    goto upd
2d671 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ate_cleanup;.  }
2d672 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6c  .#endif..  /* Al
2d673 6c 6f 63 61 74 65 20 72 65 71 75 69 72 65 64 20  locate required 
2d674 72 65 67 69 73 74 65 72 73 2e 20 2a 2f 0a 20 20  registers. */.  
2d675 72 65 67 4f 6c 64 52 6f 77 69 64 20 3d 20 72 65  regOldRowid = re
2d676 67 4e 65 77 52 6f 77 69 64 20 3d 20 2b 2b 70 50  gNewRowid = ++pP
2d677 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 69 66  arse->nMem;.  if
2d678 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61  ( pTrigger || ha
2d679 73 46 4b 20 29 7b 0a 20 20 20 20 72 65 67 4f 6c  sFK ){.    regOl
2d67a 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  d = pParse->nMem
2d67b 20 2b 20 31 3b 0a 20 20 20 20 70 50 61 72 73 65   + 1;.    pParse
2d67c 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e  ->nMem += pTab->
2d67d 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nCol;.  }.  if( 
2d67e 63 68 6e 67 52 6f 77 69 64 20 7c 7c 20 70 54 72  chngRowid || pTr
2d67f 69 67 67 65 72 20 7c 7c 20 68 61 73 46 4b 20 29  igger || hasFK )
2d680 7b 0a 20 20 20 20 72 65 67 4e 65 77 52 6f 77 69  {.    regNewRowi
2d681 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  d = ++pParse->nM
2d682 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 67 4e 65 77  em;.  }.  regNew
2d683 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20   = pParse->nMem 
2d684 2b 20 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  + 1;.  pParse->n
2d685 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f  Mem += pTab->nCo
2d686 6c 3b 0a 20 20 72 65 67 52 65 63 20 3d 20 2b 2b  l;.  regRec = ++
2d687 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20  pParse->nMem;.. 
2d688 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20 76 69   /* Start the vi
2d689 65 77 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20  ew context. */. 
2d68a 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20   if( isView ){. 
2d68b 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f     sqlite3AuthCo
2d68c 6e 74 65 78 74 50 75 73 68 28 70 50 61 72 73 65  ntextPush(pParse
2d68d 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 54 61  , &sContext, pTa
2d68e 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a  b->zName);.  }..
2d68f 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 74    /* If we are t
2d690 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20  rying to update 
2d691 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65 20  a view, realize 
2d692 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0a 20  that view into. 
2d693 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61 6c 20   ** a ephemeral 
2d694 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 20  table..  */.#if 
2d695 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2d696 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64  OMIT_VIEW) && !d
2d697 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2d698 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 69 66  IT_TRIGGER).  if
2d699 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
2d69a 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69  sqlite3Materiali
2d69b 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c 20 70  zeView(pParse, p
2d69c 54 61 62 2c 20 70 57 68 65 72 65 2c 20 69 43 75  Tab, pWhere, iCu
2d69d 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  r);.  }.#endif..
2d69e 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65    /* Resolve the
2d69f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e   column names in
2d6a0 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73   all the express
2d6a1 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a  ions in the.  **
2d6a2 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20   WHERE clause.. 
2d6a3 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2d6a4 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2d6a5 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20  s(&sNC, pWhere) 
2d6a6 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  ){.    goto upda
2d6a7 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  te_cleanup;.  }.
2d6a8 0a 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 20  .  /* Begin the 
2d6a9 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20  database scan.  
2d6aa 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
2d6ab 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2d6ac 6c 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69  l, 0, regOldRowi
2d6ad 64 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73  d);.  pWInfo = s
2d6ae 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2d6af 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
2d6b0 74 2c 20 70 57 68 65 72 65 2c 30 2c 20 57 48 45  t, pWhere,0, WHE
2d6b1 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52  RE_ONEPASS_DESIR
2d6b2 45 44 29 3b 0a 20 20 69 66 28 20 70 57 49 6e 66  ED);.  if( pWInf
2d6b3 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61  o==0 ) goto upda
2d6b4 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 6f 6b  te_cleanup;.  ok
2d6b5 4f 6e 65 50 61 73 73 20 3d 20 70 57 49 6e 66 6f  OnePass = pWInfo
2d6b6 2d 3e 6f 6b 4f 6e 65 50 61 73 73 3b 0a 0a 20 20  ->okOnePass;..  
2d6b7 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20  /* Remember the 
2d6b8 72 6f 77 69 64 20 6f 66 20 65 76 65 72 79 20 69  rowid of every i
2d6b9 74 65 6d 20 74 6f 20 62 65 20 75 70 64 61 74 65  tem to be update
2d6ba 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  d..  */.  sqlite
2d6bb 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2d6bc 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72  P_Rowid, iCur, r
2d6bd 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 69  egOldRowid);.  i
2d6be 66 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20 29 7b  f( !okOnePass ){
2d6bf 0a 20 20 20 20 72 65 67 52 6f 77 53 65 74 20 3d  .    regRowSet =
2d6c0 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2d6c1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2d6c2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
2d6c3 53 65 74 41 64 64 2c 20 72 65 67 52 6f 77 53 65  SetAdd, regRowSe
2d6c4 74 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b  t, regOldRowid);
2d6c5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 64 20 74  .  }..  /* End t
2d6c6 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e  he database scan
2d6c7 20 6c 6f 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73 71   loop..  */.  sq
2d6c8 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
2d6c9 49 6e 66 6f 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69  Info);..  /* Ini
2d6ca 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e  tialize the coun
2d6cb 74 20 6f 66 20 75 70 64 61 74 65 64 20 72 6f 77  t of updated row
2d6cc 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62  s.  */.  if( (db
2d6cd 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2d6ce 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21  _CountRows) && !
2d6cf 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
2d6d0 54 61 62 20 29 7b 0a 20 20 20 20 72 65 67 52 6f  Tab ){.    regRo
2d6d1 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73  wCount = ++pPars
2d6d2 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
2d6d3 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2d6d4 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2d6d5 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20   regRowCount);. 
2d6d6 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 56 69 65   }..  if( !isVie
2d6d7 77 20 29 7b 0a 20 20 20 20 2f 2a 20 0a 20 20 20  w ){.    /* .   
2d6d8 20 2a 2a 20 4f 70 65 6e 20 65 76 65 72 79 20 69   ** Open every i
2d6d9 6e 64 65 78 20 74 68 61 74 20 6e 65 65 64 73 20  ndex that needs 
2d6da 75 70 64 61 74 69 6e 67 2e 20 20 4e 6f 74 65 20  updating.  Note 
2d6db 74 68 61 74 20 69 66 20 61 6e 79 0a 20 20 20 20  that if any.    
2d6dc 2a 2a 20 69 6e 64 65 78 20 63 6f 75 6c 64 20 70  ** index could p
2d6dd 6f 74 65 6e 74 69 61 6c 6c 79 20 69 6e 76 6f 6b  otentially invok
2d6de 65 20 61 20 52 45 50 4c 41 43 45 20 63 6f 6e 66  e a REPLACE conf
2d6df 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
2d6e0 0a 20 20 20 20 2a 2a 20 61 63 74 69 6f 6e 2c 20  .    ** action, 
2d6e1 74 68 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 20  then we need to 
2d6e2 6f 70 65 6e 20 61 6c 6c 20 69 6e 64 69 63 65 73  open all indices
2d6e3 20 62 65 63 61 75 73 65 20 77 65 20 6d 69 67 68   because we migh
2d6e4 74 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f  t need.    ** to
2d6e5 20 62 65 20 64 65 6c 65 74 69 6e 67 20 73 6f 6d   be deleting som
2d6e6 65 20 72 65 63 6f 72 64 73 2e 0a 20 20 20 20 2a  e records..    *
2d6e7 2f 0a 20 20 20 20 69 66 28 20 21 6f 6b 4f 6e 65  /.    if( !okOne
2d6e8 50 61 73 73 20 29 20 73 71 6c 69 74 65 33 4f 70  Pass ) sqlite3Op
2d6e9 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  enTable(pParse, 
2d6ea 69 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62 2c  iCur, iDb, pTab,
2d6eb 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 20   OP_OpenWrite); 
2d6ec 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72  .    if( onError
2d6ed 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a  ==OE_Replace ){.
2d6ee 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20        openAll = 
2d6ef 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
2d6f0 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b      openAll = 0;
2d6f1 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d  .      for(pIdx=
2d6f2 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2d6f3 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2d6f4 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69  Next){.        i
2d6f5 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  f( pIdx->onError
2d6f6 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a  ==OE_Replace ){.
2d6f7 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e 41 6c            openAl
2d6f8 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  l = 1;.         
2d6f9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2d6fa 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
2d6fb 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 64      for(i=0, pId
2d6fc 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2d6fd 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2d6fe 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20  >pNext, i++){.  
2d6ff 20 20 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20      if( openAll 
2d700 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20  || aRegIdx[i]>0 
2d701 29 7b 0a 20 20 20 20 20 20 20 20 4b 65 79 49 6e  ){.        KeyIn
2d702 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74  fo *pKey = sqlit
2d703 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70  e3IndexKeyinfo(p
2d704 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20  Parse, pIdx);.  
2d705 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2d706 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70  eAddOp4(v, OP_Op
2d707 65 6e 57 72 69 74 65 2c 20 69 43 75 72 2b 69 2b  enWrite, iCur+i+
2d708 31 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69  1, pIdx->tnum, i
2d709 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Db,.            
2d70a 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
2d70b 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
2d70c 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
2d70d 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
2d70e 72 73 65 2d 3e 6e 54 61 62 3e 69 43 75 72 2b 69  rse->nTab>iCur+i
2d70f 2b 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  +1 );.      }.  
2d710 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f    }.  }..  /* To
2d711 70 20 6f 66 20 74 68 65 20 75 70 64 61 74 65 20  p of the update 
2d712 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20 6f 6b  loop */.  if( ok
2d713 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20 20 20 69  OnePass ){.    i
2d714 6e 74 20 61 31 20 3d 20 73 71 6c 69 74 65 33 56  nt a1 = sqlite3V
2d715 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2d716 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 4f 6c 64 52  NotNull, regOldR
2d717 6f 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72 20  owid);.    addr 
2d718 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2d719 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b  Op0(v, OP_Goto);
2d71a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2d71b 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 31 29 3b  JumpHere(v, a1);
2d71c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64  .  }else{.    ad
2d71d 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
2d71e 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 6f 77  AddOp3(v, OP_Row
2d71f 53 65 74 52 65 61 64 2c 20 72 65 67 52 6f 77 53  SetRead, regRowS
2d720 65 74 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77  et, 0, regOldRow
2d721 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  id);.  }..  /* M
2d722 61 6b 65 20 63 75 72 73 6f 72 20 69 43 75 72 20  ake cursor iCur 
2d723 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 65 63  point to the rec
2d724 6f 72 64 20 74 68 61 74 20 69 73 20 62 65 69 6e  ord that is bein
2d725 67 20 75 70 64 61 74 65 64 2e 20 49 66 0a 20 20  g updated. If.  
2d726 2a 2a 20 74 68 69 73 20 72 65 63 6f 72 64 20 64  ** this record d
2d727 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 66 6f  oes not exist fo
2d728 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 20 28 64  r some reason (d
2d729 65 6c 65 74 65 64 20 62 79 20 61 20 74 72 69 67  eleted by a trig
2d72a 67 65 72 2c 0a 20 20 2a 2a 20 66 6f 72 20 65 78  ger,.  ** for ex
2d72b 61 6d 70 6c 65 2c 20 74 68 65 6e 20 6a 75 6d 70  ample, then jump
2d72c 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65   to the next ite
2d72d 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 52 6f  ration of the Ro
2d72e 77 53 65 74 20 6c 6f 6f 70 2e 20 20 2a 2f 0a 20  wSet loop.  */. 
2d72f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d730 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
2d731 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20  ts, iCur, addr, 
2d732 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20  regOldRowid);.. 
2d733 20 2f 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72   /* If the recor
2d734 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20 63 68  d number will ch
2d735 61 6e 67 65 2c 20 73 65 74 20 72 65 67 69 73 74  ange, set regist
2d736 65 72 20 72 65 67 4e 65 77 52 6f 77 69 64 20 74  er regNewRowid t
2d737 6f 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 74  o.  ** contain t
2d738 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 20 49 66  he new value. If
2d739 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
2d73a 65 72 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  er is not being 
2d73b 6d 6f 64 69 66 69 65 64 2c 0a 20 20 2a 2a 20 74  modified,.  ** t
2d73c 68 65 6e 20 72 65 67 4e 65 77 52 6f 77 69 64 20  hen regNewRowid 
2d73d 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 69  is the same regi
2d73e 73 74 65 72 20 61 73 20 72 65 67 4f 6c 64 52 6f  ster as regOldRo
2d73f 77 69 64 2c 20 77 68 69 63 68 20 69 73 0a 20 20  wid, which is.  
2d740 2a 2a 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c  ** already popul
2d741 61 74 65 64 2e 20 20 2a 2f 0a 20 20 61 73 73 65  ated.  */.  asse
2d742 72 74 28 20 63 68 6e 67 52 6f 77 69 64 20 7c 7c  rt( chngRowid ||
2d743 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61 73   pTrigger || has
2d744 46 4b 20 7c 7c 20 72 65 67 4f 6c 64 52 6f 77 69  FK || regOldRowi
2d745 64 3d 3d 72 65 67 4e 65 77 52 6f 77 69 64 20 29  d==regNewRowid )
2d746 3b 0a 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69  ;.  if( chngRowi
2d747 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
2d748 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2d749 20 70 52 6f 77 69 64 45 78 70 72 2c 20 72 65 67   pRowidExpr, reg
2d74a 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  NewRowid);.    s
2d74b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2d74c 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  (v, OP_MustBeInt
2d74d 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a  , regNewRowid);.
2d74e 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
2d74f 72 65 20 61 72 65 20 74 72 69 67 67 65 72 73 20  re are triggers 
2d750 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20 70  on this table, p
2d751 6f 70 75 6c 61 74 65 20 61 6e 20 61 72 72 61 79  opulate an array
2d752 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 0a 20   of registers . 
2d753 20 2a 2a 20 77 69 74 68 20 74 68 65 20 72 65 71   ** with the req
2d754 75 69 72 65 64 20 6f 6c 64 2e 2a 20 63 6f 6c 75  uired old.* colu
2d755 6d 6e 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 69  mn data.  */.  i
2d756 66 28 20 68 61 73 46 4b 20 7c 7c 20 70 54 72 69  f( hasFK || pTri
2d757 67 67 65 72 20 29 7b 0a 20 20 20 20 75 33 32 20  gger ){.    u32 
2d758 6f 6c 64 6d 61 73 6b 20 3d 20 28 68 61 73 46 4b  oldmask = (hasFK
2d759 20 3f 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d   ? sqlite3FkOldm
2d75a 61 73 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62  ask(pParse, pTab
2d75b 29 20 3a 20 30 29 3b 0a 20 20 20 20 6f 6c 64 6d  ) : 0);.    oldm
2d75c 61 73 6b 20 7c 3d 20 73 71 6c 69 74 65 33 54 72  ask |= sqlite3Tr
2d75d 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28 70 50 61  iggerOldmask(pPa
2d75e 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 70  rse, pTrigger, p
2d75f 43 68 61 6e 67 65 73 2c 20 70 54 61 62 2c 20 6f  Changes, pTab, o
2d760 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20 66 6f 72  nError);.    for
2d761 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
2d762 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
2d763 69 66 28 20 61 58 52 65 66 5b 69 5d 3c 30 20 7c  if( aXRef[i]<0 |
2d764 7c 20 6f 6c 64 6d 61 73 6b 3d 3d 30 78 66 66 66  | oldmask==0xfff
2d765 66 66 66 66 66 20 7c 7c 20 28 6f 6c 64 6d 61 73  fffff || (oldmas
2d766 6b 20 26 20 28 31 3c 3c 69 29 29 20 29 7b 0a 20  k & (1<<i)) ){. 
2d767 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2d768 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
2d769 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20  olumn, iCur, i, 
2d76a 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 20 20  regOld+i);.     
2d76b 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e     sqlite3Column
2d76c 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c  Default(v, pTab,
2d76d 20 69 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20   i, regOld+i);. 
2d76e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2d76f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d770 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2d771 2c 20 30 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a  , 0, regOld+i);.
2d772 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2d773 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 3d    if( chngRowid=
2d774 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
2d775 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d776 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4f 6c 64   OP_Copy, regOld
2d777 52 6f 77 69 64 2c 20 72 65 67 4e 65 77 52 6f 77  Rowid, regNewRow
2d778 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  id);.    }.  }..
2d779 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68    /* Populate th
2d77a 65 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73  e array of regis
2d77b 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 61  ters beginning a
2d77c 74 20 72 65 67 4e 65 77 20 77 69 74 68 20 74 68  t regNew with th
2d77d 65 20 6e 65 77 0a 20 20 2a 2a 20 72 6f 77 20 64  e new.  ** row d
2d77e 61 74 61 2e 20 54 68 69 73 20 61 72 72 61 79 20  ata. This array 
2d77f 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b  is used to check
2d780 20 63 6f 6e 73 74 61 69 6e 74 73 2c 20 63 72 65   constaints, cre
2d781 61 74 65 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a  ate the new.  **
2d782 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78   table and index
2d783 20 72 65 63 6f 72 64 73 2c 20 61 6e 64 20 61 73   records, and as
2d784 20 74 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20   the values for 
2d785 61 6e 79 20 6e 65 77 2e 2a 20 72 65 66 65 72 65  any new.* refere
2d786 6e 63 65 73 0a 20 20 2a 2a 20 6d 61 64 65 20 62  nces.  ** made b
2d787 79 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a  y triggers.  */.
2d788 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
2d789 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
2d78a 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e     if( i==pTab->
2d78b 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73  iPKey ){.      s
2d78c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2d78d 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2d78e 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 7d  regNew+i);.    }
2d78f 65 6c 73 65 7b 0a 20 20 20 20 20 20 6a 20 3d 20  else{.      j = 
2d790 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20 20  aXRef[i];.      
2d791 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20 20  if( j<0 ){.     
2d792 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2d793 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
2d794 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67 4e  n, iCur, i, regN
2d795 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73  ew+i);.        s
2d796 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
2d797 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 2c 20  ult(v, pTab, i, 
2d798 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20  regNew+i);.     
2d799 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2d79a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2d79b 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67 65 73  pParse, pChanges
2d79c 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65  ->a[j].pExpr, re
2d79d 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 7d  gNew+i);.      }
2d79e 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2d79f 20 46 69 72 65 20 61 6e 79 20 42 45 46 4f 52 45   Fire any BEFORE
2d7a0 20 55 50 44 41 54 45 20 74 72 69 67 67 65 72 73   UPDATE triggers
2d7a1 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 62  . This happens b
2d7a2 65 66 6f 72 65 20 63 6f 6e 73 74 72 61 69 6e 74  efore constraint
2d7a3 73 20 61 72 65 0a 20 20 2a 2a 20 76 65 72 69 66  s are.  ** verif
2d7a4 69 65 64 2e 20 4f 6e 65 20 63 6f 75 6c 64 20 61  ied. One could a
2d7a5 72 67 75 65 20 74 68 61 74 20 74 68 69 73 20 69  rgue that this i
2d7a6 73 20 77 72 6f 6e 67 2e 20 20 2a 2f 0a 20 20 69  s wrong.  */.  i
2d7a7 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
2d7a8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2d7a9 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66 66 69 6e  dOp2(v, OP_Affin
2d7aa 69 74 79 2c 20 72 65 67 4e 65 77 2c 20 70 54 61  ity, regNew, pTa
2d7ab 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71  b->nCol);.    sq
2d7ac 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69  lite3TableAffini
2d7ad 74 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a  tyStr(v, pTab);.
2d7ae 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52      sqlite3CodeR
2d7af 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
2d7b0 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 55  , pTrigger, TK_U
2d7b1 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c  PDATE, pChanges,
2d7b2 20 0a 20 20 20 20 20 20 20 20 54 52 49 47 47 45   .        TRIGGE
2d7b3 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 20  R_BEFORE, pTab, 
2d7b4 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 6f 6e 45  regOldRowid, onE
2d7b5 72 72 6f 72 2c 20 61 64 64 72 29 3b 0a 0a 20 20  rror, addr);..  
2d7b6 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 74 72 69    /* The row-tri
2d7b7 67 67 65 72 20 6d 61 79 20 68 61 76 65 20 64 65  gger may have de
2d7b8 6c 65 74 65 64 20 74 68 65 20 72 6f 77 20 62 65  leted the row be
2d7b9 69 6e 67 20 75 70 64 61 74 65 64 2e 20 49 6e 20  ing updated. In 
2d7ba 74 68 69 73 0a 20 20 20 20 2a 2a 20 63 61 73 65  this.    ** case
2d7bb 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e 65  , jump to the ne
2d7bc 78 74 20 72 6f 77 2e 20 4e 6f 20 75 70 64 61 74  xt row. No updat
2d7bd 65 73 20 6f 72 20 41 46 54 45 52 20 74 72 69 67  es or AFTER trig
2d7be 67 65 72 73 20 61 72 65 20 0a 20 20 20 20 2a 2a  gers are .    **
2d7bf 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20   required. This 
2d7c0 62 65 68 61 76 69 6f 75 72 20 2d 20 77 68 61 74  behaviour - what
2d7c1 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74 68   happens when th
2d7c2 65 20 72 6f 77 20 62 65 69 6e 67 20 75 70 64 61  e row being upda
2d7c3 74 65 64 0a 20 20 20 20 2a 2a 20 69 73 20 64 65  ted.    ** is de
2d7c4 6c 65 74 65 64 20 6f 72 20 72 65 6e 61 6d 65 64  leted or renamed
2d7c5 20 62 79 20 61 20 42 45 46 4f 52 45 20 74 72 69   by a BEFORE tri
2d7c6 67 67 65 72 20 2d 20 69 73 20 6c 65 66 74 20 75  gger - is left u
2d7c7 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 0a  ndefined in the.
2d7c8 20 20 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 61      ** documenta
2d7c9 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 73 71  tion.  */.    sq
2d7ca 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2d7cb 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
2d7cc 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72 65 67   iCur, addr, reg
2d7cd 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a  OldRowid);.  }..
2d7ce 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
2d7cf 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6e 73  ..    /* Do cons
2d7d0 74 72 61 69 6e 74 20 63 68 65 63 6b 73 2e 20 2a  traint checks. *
2d7d1 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e  /.    sqlite3Gen
2d7d2 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
2d7d3 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20 70 54  hecks(pParse, pT
2d7d4 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e 65 77  ab, iCur, regNew
2d7d5 52 6f 77 69 64 2c 0a 20 20 20 20 20 20 20 20 61  Rowid,.        a
2d7d6 52 65 67 49 64 78 2c 20 28 63 68 6e 67 52 6f 77  RegIdx, (chngRow
2d7d7 69 64 3f 72 65 67 4f 6c 64 52 6f 77 69 64 3a 30  id?regOldRowid:0
2d7d8 29 2c 20 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 61  ), 1, onError, a
2d7d9 64 64 72 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a  ddr, 0);..    /*
2d7da 20 44 6f 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e   Do FK constrain
2d7db 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0a 20 20 20  t checks. */.   
2d7dc 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20 20   if( hasFK ){.  
2d7dd 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65      sqlite3FkChe
2d7de 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ck(pParse, pTab,
2d7df 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 30 29   regOldRowid, 0)
2d7e0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2d7e1 44 65 6c 65 74 65 20 74 68 65 20 69 6e 64 65 78  Delete the index
2d7e2 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61   entries associa
2d7e3 74 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72  ted with the cur
2d7e4 72 65 6e 74 20 72 65 63 6f 72 64 2e 20 20 2a 2f  rent record.  */
2d7e5 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65  .    j1 = sqlite
2d7e6 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2d7e7 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75  P_NotExists, iCu
2d7e8 72 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69  r, 0, regOldRowi
2d7e9 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  d);.    sqlite3G
2d7ea 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44  enerateRowIndexD
2d7eb 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70 54  elete(pParse, pT
2d7ec 61 62 2c 20 69 43 75 72 2c 20 61 52 65 67 49 64  ab, iCur, aRegId
2d7ed 78 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66  x);.  .    /* If
2d7ee 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72 65   changing the re
2d7ef 63 6f 72 64 20 6e 75 6d 62 65 72 2c 20 64 65 6c  cord number, del
2d7f0 65 74 65 20 74 68 65 20 6f 6c 64 20 72 65 63 6f  ete the old reco
2d7f1 72 64 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  rd.  */.    if( 
2d7f2 68 61 73 46 4b 20 7c 7c 20 63 68 6e 67 52 6f 77  hasFK || chngRow
2d7f3 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  id ){.      sqli
2d7f4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d7f5 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75 72   OP_Delete, iCur
2d7f6 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
2d7f7 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2d7f8 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20  ere(v, j1);..   
2d7f9 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20 20   if( hasFK ){.  
2d7fa 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65      sqlite3FkChe
2d7fb 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ck(pParse, pTab,
2d7fc 20 30 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29   0, regNewRowid)
2d7fd 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
2d7fe 2a 20 49 6e 73 65 72 74 20 74 68 65 20 6e 65 77  * Insert the new
2d7ff 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61   index entries a
2d800 6e 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  nd the new recor
2d801 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  d. */.    sqlite
2d802 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69  3CompleteInserti
2d803 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  on(pParse, pTab,
2d804 20 69 43 75 72 2c 20 72 65 67 4e 65 77 52 6f 77   iCur, regNewRow
2d805 69 64 2c 20 61 52 65 67 49 64 78 2c 20 31 2c 20  id, aRegIdx, 1, 
2d806 30 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  0, 0);..    /* D
2d807 6f 20 61 6e 79 20 4f 4e 20 43 41 53 43 41 44 45  o any ON CASCADE
2d808 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45  , SET NULL or SE
2d809 54 20 44 45 46 41 55 4c 54 20 6f 70 65 72 61 74  T DEFAULT operat
2d80a 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 74 6f  ions required to
2d80b 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 72  .    ** handle r
2d80c 6f 77 73 20 28 70 6f 73 73 69 62 6c 79 20 69 6e  ows (possibly in
2d80d 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29 20 74   other tables) t
2d80e 68 61 74 20 72 65 66 65 72 20 76 69 61 20 61 20  hat refer via a 
2d80f 66 6f 72 65 69 67 6e 20 6b 65 79 0a 20 20 20 20  foreign key.    
2d810 2a 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 6a 75  ** to the row ju
2d811 73 74 20 75 70 64 61 74 65 64 2e 20 2a 2f 20 0a  st updated. */ .
2d812 20 20 20 20 69 66 28 20 68 61 73 46 4b 20 29 7b      if( hasFK ){
2d813 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b  .      sqlite3Fk
2d814 41 63 74 69 6f 6e 73 28 70 50 61 72 73 65 2c 20  Actions(pParse, 
2d815 70 54 61 62 2c 20 70 43 68 61 6e 67 65 73 2c 20  pTab, pChanges, 
2d816 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20  regOldRowid);.  
2d817 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e    }.  }..  /* In
2d818 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f 77 20  crement the row 
2d819 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f 0a 20 20  counter .  */.  
2d81a 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  if( (db->flags &
2d81b 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
2d81c 73 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70  s) && !pParse->p
2d81d 54 72 69 67 67 65 72 54 61 62 29 7b 0a 20 20 20  TriggerTab){.   
2d81e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d81f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
2d820 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29   regRowCount, 1)
2d821 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
2d822 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70  CodeRowTrigger(p
2d823 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c  Parse, pTrigger,
2d824 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61   TK_UPDATE, pCha
2d825 6e 67 65 73 2c 20 0a 20 20 20 20 20 20 54 52 49  nges, .      TRI
2d826 47 47 45 52 5f 41 46 54 45 52 2c 20 70 54 61 62  GGER_AFTER, pTab
2d827 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 6f  , regOldRowid, o
2d828 6e 45 72 72 6f 72 2c 20 61 64 64 72 29 3b 0a 0a  nError, addr);..
2d829 20 20 2f 2a 20 52 65 70 65 61 74 20 74 68 65 20    /* Repeat the 
2d82a 61 62 6f 76 65 20 77 69 74 68 20 74 68 65 20 6e  above with the n
2d82b 65 78 74 20 72 65 63 6f 72 64 20 74 6f 20 62 65  ext record to be
2d82c 20 75 70 64 61 74 65 64 2c 20 75 6e 74 69 6c 0a   updated, until.
2d82d 20 20 2a 2a 20 61 6c 6c 20 72 65 63 6f 72 64 20    ** all record 
2d82e 73 65 6c 65 63 74 65 64 20 62 79 20 74 68 65 20  selected by the 
2d82f 57 48 45 52 45 20 63 6c 61 75 73 65 20 68 61 76  WHERE clause hav
2d830 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 2e 0a  e been updated..
2d831 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2d832 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2d833 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20  oto, 0, addr);. 
2d834 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2d835 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a  Here(v, addr);..
2d836 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74    /* Close all t
2d837 61 62 6c 65 73 20 2a 2f 0a 20 20 66 6f 72 28 69  ables */.  for(i
2d838 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
2d839 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
2d83a 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69  x=pIdx->pNext, i
2d83b 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6f 70 65  ++){.    if( ope
2d83c 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b  nAll || aRegIdx[
2d83d 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  i]>0 ){.      sq
2d83e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2d83f 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75  v, OP_Close, iCu
2d840 72 2b 69 2b 31 2c 20 30 29 3b 0a 20 20 20 20 7d  r+i+1, 0);.    }
2d841 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
2d842 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2d843 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a  lose, iCur, 0);.
2d844 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65  .  /* Update the
2d845 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
2d846 20 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69 6e   table by storin
2d847 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  g the content of
2d848 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75   the.  ** maximu
2d849 6d 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 20  m rowid counter 
2d84a 76 61 6c 75 65 73 20 72 65 63 6f 72 64 65 64 20  values recorded 
2d84b 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20  while inserting 
2d84c 69 6e 74 6f 0a 20 20 2a 2a 20 61 75 74 6f 69 6e  into.  ** autoin
2d84d 63 72 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e 0a  crement tables..
2d84e 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73    */.  if( pPars
2d84f 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20  e->nested==0 && 
2d850 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
2d851 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Tab==0 ){.    sq
2d852 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65  lite3Autoincreme
2d853 6e 74 45 6e 64 28 70 50 61 72 73 65 29 3b 0a 20  ntEnd(pParse);. 
2d854 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65   }..  /*.  ** Re
2d855 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
2d856 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 65 72  of rows that wer
2d857 65 20 63 68 61 6e 67 65 64 2e 20 49 66 20 74 68  e changed. If th
2d858 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20  is routine is . 
2d859 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63   ** generating c
2d85a 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20 61  ode because of a
2d85b 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
2d85c 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20 64  NestedParse(), d
2d85d 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b  o not.  ** invok
2d85e 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
2d85f 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  unction..  */.  
2d860 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53  if( (db->flags&S
2d861 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29  QLITE_CountRows)
2d862 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72   && !pParse->pTr
2d863 69 67 67 65 72 54 61 62 20 26 26 20 21 70 50 61  iggerTab && !pPa
2d864 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20  rse->nested ){. 
2d865 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2d866 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
2d867 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75 6e  tRow, regRowCoun
2d868 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  t, 1);.    sqlit
2d869 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
2d86a 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
2d86b 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2d86c 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
2d86d 4e 41 4d 45 2c 20 22 72 6f 77 73 20 75 70 64 61  NAME, "rows upda
2d86e 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ted", SQLITE_STA
2d86f 54 49 43 29 3b 0a 20 20 7d 0a 0a 75 70 64 61 74  TIC);.  }..updat
2d870 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c  e_cleanup:.  sql
2d871 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
2d872 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20  op(&sContext);. 
2d873 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2d874 62 2c 20 61 52 65 67 49 64 78 29 3b 0a 20 20 73  b, aRegIdx);.  s
2d875 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2d876 20 61 58 52 65 66 29 3b 0a 20 20 73 71 6c 69 74   aXRef);.  sqlit
2d877 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
2d878 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20  db, pTabList);. 
2d879 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2d87a 44 65 6c 65 74 65 28 64 62 2c 20 70 43 68 61 6e  Delete(db, pChan
2d87b 67 65 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ges);.  sqlite3E
2d87c 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57  xprDelete(db, pW
2d87d 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b  here);.  return;
2d87e 0a 7d 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .}./* Make sure 
2d87f 22 69 73 56 69 65 77 22 20 61 6e 64 20 6f 74 68  "isView" and oth
2d880 65 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65  er macros define
2d881 64 20 61 62 6f 76 65 20 61 72 65 20 75 6e 64 65  d above are unde
2d882 66 69 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  fined. Otherwise
2d883 0a 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20 69 6e  .** thely may in
2d884 74 65 72 66 65 72 65 20 77 69 74 68 20 63 6f 6d  terfere with com
2d885 70 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68 65  pilation of othe
2d886 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74  r functions in t
2d887 68 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f 72 20  his file.** (or 
2d888 69 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c  in another file,
2d889 20 69 66 20 74 68 69 73 20 66 69 6c 65 20 62 65   if this file be
2d88a 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 20 74 68  comes part of th
2d88b 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e  e amalgamation).
2d88c 20 20 2a 2f 0a 23 69 66 64 65 66 20 69 73 56 69    */.#ifdef isVi
2d88d 65 77 0a 20 23 75 6e 64 65 66 20 69 73 56 69 65  ew. #undef isVie
2d88e 77 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  w.#endif.#ifdef 
2d88f 70 54 72 69 67 67 65 72 0a 20 23 75 6e 64 65 66  pTrigger. #undef
2d890 20 70 54 72 69 67 67 65 72 0a 23 65 6e 64 69 66   pTrigger.#endif
2d891 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2d892 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2d893 4c 45 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  LE./*.** Generat
2d894 65 20 63 6f 64 65 20 66 6f 72 20 61 6e 20 55 50  e code for an UP
2d895 44 41 54 45 20 6f 66 20 61 20 76 69 72 74 75 61  DATE of a virtua
2d896 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  l table..**.** T
2d897 68 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74  he strategy is t
2d898 68 61 74 20 77 65 20 63 72 65 61 74 65 20 61 6e  hat we create an
2d899 20 65 70 68 65 6d 65 72 69 61 6c 20 74 61 62 6c   ephemerial tabl
2d89a 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  e that contains.
2d89b 2a 2a 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20  ** for each row 
2d89c 74 6f 20 62 65 20 63 68 61 6e 67 65 64 3a 0a 2a  to be changed:.*
2d89d 2a 0a 2a 2a 20 20 20 28 41 29 20 20 54 68 65 20  *.**   (A)  The 
2d89e 6f 72 69 67 69 6e 61 6c 20 72 6f 77 69 64 20 6f  original rowid o
2d89f 66 20 74 68 61 74 20 72 6f 77 2e 0a 2a 2a 20 20  f that row..**  
2d8a0 20 28 42 29 20 20 54 68 65 20 72 65 76 69 73 65   (B)  The revise
2d8a1 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  d rowid for the 
2d8a2 72 6f 77 2e 20 28 6e 6f 74 65 31 29 0a 2a 2a 20  row. (note1).** 
2d8a3 20 20 28 43 29 20 20 54 68 65 20 63 6f 6e 74 65    (C)  The conte
2d8a4 6e 74 20 6f 66 20 65 76 65 72 79 20 63 6f 6c 75  nt of every colu
2d8a5 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 2e 0a 2a  mn in the row..*
2d8a6 2a 0a 2a 2a 20 54 68 65 6e 20 77 65 20 6c 6f 6f  *.** Then we loo
2d8a7 70 20 6f 76 65 72 20 74 68 69 73 20 65 70 68 65  p over this ephe
2d8a8 6d 65 72 61 6c 20 74 61 62 6c 65 20 61 6e 64 20  meral table and 
2d8a9 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e 0a  for each row in.
2d8aa 2a 2a 20 74 68 65 20 65 70 68 65 72 6d 65 72 61  ** the ephermera
2d8ab 6c 20 74 61 62 6c 65 20 63 61 6c 6c 20 56 55 70  l table call VUp
2d8ac 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  date..**.** When
2d8ad 20 66 69 6e 69 73 68 65 64 2c 20 64 72 6f 70 20   finished, drop 
2d8ae 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61  the ephemeral ta
2d8af 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 6e 6f 74 65  ble..**.** (note
2d8b0 31 29 20 41 63 74 75 61 6c 6c 79 2c 20 69 66 20  1) Actually, if 
2d8b1 77 65 20 6b 6e 6f 77 20 69 6e 20 61 64 76 61 6e  we know in advan
2d8b2 63 65 20 74 68 61 74 20 28 41 29 20 69 73 20 61  ce that (A) is a
2d8b3 6c 77 61 79 73 20 74 68 65 20 73 61 6d 65 0a 2a  lways the same.*
2d8b4 2a 20 61 73 20 28 42 29 20 77 65 20 6f 6e 6c 79  * as (B) we only
2d8b5 20 73 74 6f 72 65 20 28 41 29 2c 20 74 68 65 6e   store (A), then
2d8b6 20 64 75 70 6c 69 63 61 74 65 20 28 41 29 20 77   duplicate (A) w
2d8b7 68 65 6e 20 70 75 6c 6c 69 6e 67 0a 2a 2a 20 69  hen pulling.** i
2d8b8 74 20 6f 75 74 20 6f 66 20 74 68 65 20 65 70 68  t out of the eph
2d8b9 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 62 65 66  emeral table bef
2d8ba 6f 72 65 20 63 61 6c 6c 69 6e 67 20 56 55 70 64  ore calling VUpd
2d8bb 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ate..*/.static v
2d8bc 6f 69 64 20 75 70 64 61 74 65 56 69 72 74 75 61  oid updateVirtua
2d8bd 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20  lTable(.  Parse 
2d8be 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
2d8bf 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
2d8c0 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
2d8c1 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20  st *pSrc,       
2d8c2 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74  /* The virtual t
2d8c3 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64 69 66  able to be modif
2d8c4 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ied */.  Table *
2d8c5 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a  pTab,         /*
2d8c6 20 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   The virtual tab
2d8c7 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  le */.  ExprList
2d8c8 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20   *pChanges,  /* 
2d8c9 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63  The columns to c
2d8ca 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55 50 44  hange in the UPD
2d8cb 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ATE statement */
2d8cc 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 2c  .  Expr *pRowid,
2d8cd 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
2d8ce 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65  ssion used to re
2d8cf 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f 77 69  compute the rowi
2d8d0 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65  d */.  int *aXRe
2d8d1 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  f,          /* M
2d8d2 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f 6c 75  apping from colu
2d8d3 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f 20 65  mns of pTab to e
2d8d4 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61 6e 67  ntries in pChang
2d8d5 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  es */.  Expr *pW
2d8d6 68 65 72 65 20 20 20 20 20 20 20 20 20 2f 2a 20  here         /* 
2d8d7 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20  WHERE clause of 
2d8d8 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65  the UPDATE state
2d8d9 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  ment */.){.  Vdb
2d8da 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
2d8db 56 64 62 65 3b 20 20 2f 2a 20 56 69 72 74 75 61  Vdbe;  /* Virtua
2d8dc 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72 20  l machine under 
2d8dd 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a  construction */.
2d8de 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2d8df 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54  st = 0;     /* T
2d8e0 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
2d8e1 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2d8e2 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ement */.  Selec
2d8e3 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b 20  t *pSelect = 0; 
2d8e4 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
2d8e5 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  CT statement */.
2d8e6 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20    Expr *pExpr;  
2d8e7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2d8e8 65 6d 70 6f 72 61 72 79 20 65 78 70 72 65 73 73  emporary express
2d8e9 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 65 70 68  ion */.  int eph
2d8ea 65 6d 54 61 62 3b 20 20 20 20 20 20 20 20 20 20  emTab;          
2d8eb 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 6f 6c 64     /* Table hold
2d8ec 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f  ing the result o
2d8ed 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a  f the SELECT */.
2d8ee 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
2d8ef 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2d8f0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
2d8f1 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20   int addr;      
2d8f2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
2d8f3 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f 66 20  dress of top of 
2d8f4 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 52  loop */.  int iR
2d8f5 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  eg;             
2d8f6 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67      /* First reg
2d8f7 69 73 74 65 72 20 69 6e 20 73 65 74 20 70 61 73  ister in set pas
2d8f8 73 65 64 20 74 6f 20 4f 50 5f 56 55 70 64 61 74  sed to OP_VUpdat
2d8f9 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
2d8fa 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2d8fb 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2d8fc 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e  nection */.  con
2d8fd 73 74 20 63 68 61 72 20 2a 70 56 54 61 62 20 3d  st char *pVTab =
2d8fe 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
2d8ff 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64  lite3GetVTable(d
2d900 62 2c 20 70 54 61 62 29 3b 0a 20 20 53 65 6c 65  b, pTab);.  Sele
2d901 63 74 44 65 73 74 20 64 65 73 74 3b 0a 0a 20 20  ctDest dest;..  
2d902 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65  /* Construct the
2d903 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2d904 74 20 74 68 61 74 20 77 69 6c 6c 20 66 69 6e 64  t that will find
2d905 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 20   the new values 
2d906 66 6f 72 0a 20 20 2a 2a 20 61 6c 6c 20 75 70 64  for.  ** all upd
2d907 61 74 65 64 20 72 6f 77 73 2e 20 0a 20 20 2a 2f  ated rows. .  */
2d908 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69  .  pEList = sqli
2d909 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2d90a 64 28 70 50 61 72 73 65 2c 20 30 2c 20 73 71 6c  d(pParse, 0, sql
2d90b 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
2d90c 49 44 2c 20 22 5f 72 6f 77 69 64 5f 22 29 29 3b  ID, "_rowid_"));
2d90d 0a 20 20 69 66 28 20 70 52 6f 77 69 64 20 29 7b  .  if( pRowid ){
2d90e 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71  .    pEList = sq
2d90f 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2d910 65 6e 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69  end(pParse, pELi
2d911 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  st,.            
2d912 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d913 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2d914 70 72 44 75 70 28 64 62 2c 20 70 52 6f 77 69 64  prDup(db, pRowid
2d915 2c 20 30 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73  , 0));.  }.  ass
2d916 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79  ert( pTab->iPKey
2d917 3c 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  <0 );.  for(i=0;
2d918 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
2d919 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 58 52  ++){.    if( aXR
2d91a 65 66 5b 69 5d 3e 3d 30 20 29 7b 0a 20 20 20 20  ef[i]>=0 ){.    
2d91b 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65    pExpr = sqlite
2d91c 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 43 68  3ExprDup(db, pCh
2d91d 61 6e 67 65 73 2d 3e 61 5b 61 58 52 65 66 5b 69  anges->a[aXRef[i
2d91e 5d 5d 2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20  ]].pExpr, 0);.  
2d91f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2d920 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78  Expr = sqlite3Ex
2d921 70 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20 70 54  pr(db, TK_ID, pT
2d922 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d  ab->aCol[i].zNam
2d923 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45  e);.    }.    pE
2d924 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
2d925 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2d926 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 78  rse, pEList, pEx
2d927 70 72 29 3b 0a 20 20 7d 0a 20 20 70 53 65 6c 65  pr);.  }.  pSele
2d928 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ct = sqlite3Sele
2d929 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20 70 45  ctNew(pParse, pE
2d92a 4c 69 73 74 2c 20 70 53 72 63 2c 20 70 57 68 65  List, pSrc, pWhe
2d92b 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  re, 0, 0, 0, 0, 
2d92c 30 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a 20 43  0, 0);.  .  /* C
2d92d 72 65 61 74 65 20 74 68 65 20 65 70 68 65 6d 65  reate the epheme
2d92e 72 61 6c 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  ral table into w
2d92f 68 69 63 68 20 74 68 65 20 75 70 64 61 74 65 20  hich the update 
2d930 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 20 20 2a  results will.  *
2d931 2a 20 62 65 20 73 74 6f 72 65 64 2e 0a 20 20 2a  * be stored..  *
2d932 2f 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b  /.  assert( v );
2d933 0a 20 20 65 70 68 65 6d 54 61 62 20 3d 20 70 50  .  ephemTab = pP
2d934 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
2d935 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d936 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
2d937 6d 65 72 61 6c 2c 20 65 70 68 65 6d 54 61 62 2c  meral, ephemTab,
2d938 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 2b 28 70   pTab->nCol+1+(p
2d939 52 6f 77 69 64 21 3d 30 29 29 3b 0a 0a 20 20 2f  Rowid!=0));..  /
2d93a 2a 20 66 69 6c 6c 20 74 68 65 20 65 70 68 65 6d  * fill the ephem
2d93b 65 72 61 6c 20 74 61 62 6c 65 20 0a 20 20 2a 2f  eral table .  */
2d93c 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
2d93d 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20  DestInit(&dest, 
2d93e 53 52 54 5f 54 61 62 6c 65 2c 20 65 70 68 65 6d  SRT_Table, ephem
2d93f 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  Tab);.  sqlite3S
2d940 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
2d941 65 6c 65 63 74 2c 20 26 64 65 73 74 29 3b 0a 0a  elect, &dest);..
2d942 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
2d943 64 65 20 74 6f 20 73 63 61 6e 20 74 68 65 20 65  de to scan the e
2d944 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 61  phemeral table a
2d945 6e 64 20 63 61 6c 6c 20 56 55 70 64 61 74 65 2e  nd call VUpdate.
2d946 20 2a 2f 0a 20 20 69 52 65 67 20 3d 20 2b 2b 70   */.  iReg = ++p
2d947 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 70  Parse->nMem;.  p
2d948 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
2d949 54 61 62 2d 3e 6e 43 6f 6c 2b 31 3b 0a 20 20 61  Tab->nCol+1;.  a
2d94a 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
2d94b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
2d94c 77 69 6e 64 2c 20 65 70 68 65 6d 54 61 62 2c 20  wind, ephemTab, 
2d94d 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  0);.  sqlite3Vdb
2d94e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2d94f 6c 75 6d 6e 2c 20 20 65 70 68 65 6d 54 61 62 2c  lumn,  ephemTab,
2d950 20 30 2c 20 69 52 65 67 29 3b 0a 20 20 73 71 6c   0, iReg);.  sql
2d951 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2d952 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68  , OP_Column, eph
2d953 65 6d 54 61 62 2c 20 28 70 52 6f 77 69 64 3f 31  emTab, (pRowid?1
2d954 3a 30 29 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20  :0), iReg+1);.  
2d955 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
2d956 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
2d957 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d958 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
2d959 20 65 70 68 65 6d 54 61 62 2c 20 69 2b 31 2b 28   ephemTab, i+1+(
2d95a 70 52 6f 77 69 64 21 3d 30 29 2c 20 69 52 65 67  pRowid!=0), iReg
2d95b 2b 32 2b 69 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  +2+i);.  }.  sql
2d95c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74  ite3VtabMakeWrit
2d95d 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
2d95e 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
2d95f 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55  eAddOp4(v, OP_VU
2d960 70 64 61 74 65 2c 20 30 2c 20 70 54 61 62 2d 3e  pdate, 0, pTab->
2d961 6e 43 6f 6c 2b 32 2c 20 69 52 65 67 2c 20 70 56  nCol+2, iReg, pV
2d962 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  Tab, P4_VTAB);. 
2d963 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74   sqlite3MayAbort
2d964 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
2d965 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d966 20 4f 50 5f 4e 65 78 74 2c 20 65 70 68 65 6d 54   OP_Next, ephemT
2d967 61 62 2c 20 61 64 64 72 2b 31 29 3b 0a 20 20 73  ab, addr+1);.  s
2d968 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2d969 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 73  re(v, addr);.  s
2d96a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2d96b 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 65 70  (v, OP_Close, ep
2d96c 68 65 6d 54 61 62 2c 20 30 29 3b 0a 0a 20 20 2f  hemTab, 0);..  /
2d96d 2a 20 43 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 73  * Cleanup */.  s
2d96e 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2d96f 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
2d970 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53    .}.#endif /* S
2d971 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2d972 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a  ALTABLE */../***
2d973 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2d974 6f 66 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a  of update.c ****
2d975 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d976 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d977 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2d978 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2d979 6e 20 66 69 6c 65 20 76 61 63 75 75 6d 2e 63 20  n file vacuum.c 
2d97a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d97b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d97c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2d97d 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a  * 2003 April 6.*
2d97e 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2d97f 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2d980 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2d981 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2d982 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2d983 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2d984 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2d985 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2d986 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2d987 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2d988 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2d989 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2d98a 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2d98b 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2d98c 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2d98d 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2d98e 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2d98f 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2d990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d991 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d992 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d993 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2d994 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
2d995 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
2d996 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 56   implement the V
2d997 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ACUUM command..*
2d998 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65  *.** Most of the
2d999 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
2d99a 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65  le may be omitte
2d99b 64 20 62 79 20 64 65 66 69 6e 69 6e 67 20 74 68  d by defining th
2d99c 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  e.** SQLITE_OMIT
2d99d 5f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a  _VACUUM macro..*
2d99e 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
2d99f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55  SQLITE_OMIT_VACU
2d9a0 55 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  UM) && !defined(
2d9a1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41  SQLITE_OMIT_ATTA
2d9a2 43 48 29 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  CH)./*.** Execut
2d9a3 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61  e zSql on databa
2d9a4 73 65 20 64 62 2e 20 52 65 74 75 72 6e 20 61 6e  se db. Return an
2d9a5 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
2d9a6 73 74 61 74 69 63 20 69 6e 74 20 65 78 65 63 53  static int execS
2d9a7 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ql(sqlite3 *db, 
2d9a8 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
2d9a9 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
2d9aa 74 20 2a 70 53 74 6d 74 3b 0a 20 20 56 56 41 5f  t *pStmt;.  VVA_
2d9ab 4f 4e 4c 59 28 20 69 6e 74 20 72 63 3b 20 29 0a  ONLY( int rc; ).
2d9ac 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20    if( !zSql ){. 
2d9ad 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2d9ae 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66  _NOMEM;.  }.  if
2d9af 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
2d9b0 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c  ite3_prepare(db,
2d9b1 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
2d9b2 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74  t, 0) ){.    ret
2d9b3 75 72 6e 20 73 71 6c 69 74 65 33 5f 65 72 72 63  urn sqlite3_errc
2d9b4 6f 64 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20 56  ode(db);.  }.  V
2d9b5 56 41 5f 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20  VA_ONLY( rc = ) 
2d9b6 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
2d9b7 6d 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  mt);.  assert( r
2d9b8 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 3b  c!=SQLITE_ROW );
2d9b9 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
2d9ba 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
2d9bb 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  );.}../*.** Exec
2d9bc 75 74 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61  ute zSql on data
2d9bd 62 61 73 65 20 64 62 2e 20 54 68 65 20 73 74 61  base db. The sta
2d9be 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 65  tement returns e
2d9bf 78 61 63 74 6c 79 0a 2a 2a 20 6f 6e 65 20 63 6f  xactly.** one co
2d9c0 6c 75 6d 6e 2e 20 45 78 65 63 75 74 65 20 74 68  lumn. Execute th
2d9c1 69 73 20 61 73 20 53 51 4c 20 6f 6e 20 74 68 65  is as SQL on the
2d9c2 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 0a   same database..
2d9c3 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78  */.static int ex
2d9c4 65 63 45 78 65 63 53 71 6c 28 73 71 6c 69 74 65  ecExecSql(sqlite
2d9c5 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
2d9c6 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69  r *zSql){.  sqli
2d9c7 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
2d9c8 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63  .  int rc;..  rc
2d9c9 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61   = sqlite3_prepa
2d9ca 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
2d9cb 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69   &pStmt, 0);.  i
2d9cc 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2d9cd 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2d9ce 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52   while( SQLITE_R
2d9cf 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70  OW==sqlite3_step
2d9d0 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 72  (pStmt) ){.    r
2d9d1 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20  c = execSql(db, 
2d9d2 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63  (char*)sqlite3_c
2d9d3 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74  olumn_text(pStmt
2d9d4 2c 20 30 29 29 3b 0a 20 20 20 20 69 66 28 20 72  , 0));.    if( r
2d9d5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
2d9d6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69        sqlite3_fi
2d9d7 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20  nalize(pStmt);. 
2d9d8 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
2d9d9 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
2d9da 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
2d9db 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
2d9dc 2f 2a 0a 2a 2a 20 54 68 65 20 6e 6f 6e 2d 73 74  /*.** The non-st
2d9dd 61 6e 64 61 72 64 20 56 41 43 55 55 4d 20 63 6f  andard VACUUM co
2d9de 6d 6d 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f  mmand is used to
2d9df 20 63 6c 65 61 6e 20 75 70 20 74 68 65 20 64 61   clean up the da
2d9e0 74 61 62 61 73 65 2c 0a 2a 2a 20 63 6f 6c 6c 61  tabase,.** colla
2d9e1 70 73 65 20 66 72 65 65 20 73 70 61 63 65 2c 20  pse free space, 
2d9e2 65 74 63 2e 20 20 49 74 20 69 73 20 6d 6f 64 65  etc.  It is mode
2d9e3 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 56  lled after the V
2d9e4 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 0a 2a 2a  ACUUM command.**
2d9e5 20 69 6e 20 50 6f 73 74 67 72 65 53 51 4c 2e 0a   in PostgreSQL..
2d9e6 2a 2a 0a 2a 2a 20 49 6e 20 76 65 72 73 69 6f 6e  **.** In version
2d9e7 20 31 2e 30 2e 78 20 6f 66 20 53 51 4c 69 74 65   1.0.x of SQLite
2d9e8 2c 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d  , the VACUUM com
2d9e9 6d 61 6e 64 20 77 6f 75 6c 64 20 63 61 6c 6c 0a  mand would call.
2d9ea 2a 2a 20 67 64 62 6d 5f 72 65 6f 72 67 61 6e 69  ** gdbm_reorgani
2d9eb 7a 65 28 29 20 6f 6e 20 61 6c 6c 20 74 68 65 20  ze() on all the 
2d9ec 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 73 2e  database tables.
2d9ed 20 20 42 75 74 20 62 65 67 69 6e 6e 69 6e 67 0a    But beginning.
2d9ee 2a 2a 20 77 69 74 68 20 32 2e 30 2e 30 2c 20 53  ** with 2.0.0, S
2d9ef 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20  QLite no longer 
2d9f0 75 73 65 73 20 47 44 42 4d 20 73 6f 20 74 68 69  uses GDBM so thi
2d9f1 73 20 63 6f 6d 6d 61 6e 64 20 68 61 73 0a 2a 2a  s command has.**
2d9f2 20 62 65 63 6f 6d 65 20 61 20 6e 6f 2d 6f 70 2e   become a no-op.
2d9f3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2d9f4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
2d9f5 61 63 75 75 6d 28 50 61 72 73 65 20 2a 70 50 61  acuum(Parse *pPa
2d9f6 72 73 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  rse){.  Vdbe *v 
2d9f7 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2d9f8 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
2d9f9 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
2d9fa 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2d9fb 5f 56 61 63 75 75 6d 2c 20 30 2c 20 30 29 3b 0a  _Vacuum, 0, 0);.
2d9fc 20 20 7d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a    }.  return;.}.
2d9fd 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2d9fe 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ine implements t
2d9ff 68 65 20 4f 50 5f 56 61 63 75 75 6d 20 6f 70 63  he OP_Vacuum opc
2da00 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e  ode of the VDBE.
2da01 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2da02 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75  TE int sqlite3Ru
2da03 6e 56 61 63 75 75 6d 28 63 68 61 72 20 2a 2a 70  nVacuum(char **p
2da04 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33  zErrMsg, sqlite3
2da05 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20   *db){.  int rc 
2da06 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
2da07 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
2da08 66 72 6f 6d 20 73 65 72 76 69 63 65 20 72 6f 75  from service rou
2da09 74 69 6e 65 73 20 2a 2f 0a 20 20 42 74 72 65 65  tines */.  Btree
2da0a 20 2a 70 4d 61 69 6e 3b 20 20 20 20 20 20 20 20   *pMain;        
2da0b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2da0c 73 65 20 62 65 69 6e 67 20 76 61 63 75 75 6d 65  se being vacuume
2da0d 64 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 54  d */.  Btree *pT
2da0e 65 6d 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f  emp;           /
2da0f 2a 20 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20  * The temporary 
2da10 64 61 74 61 62 61 73 65 20 77 65 20 76 61 63 75  database we vacu
2da11 75 6d 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 68 61  um into */.  cha
2da12 72 20 2a 7a 53 71 6c 20 3d 20 30 3b 20 20 20 20  r *zSql = 0;    
2da13 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74       /* SQL stat
2da14 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  ements */.  int 
2da15 73 61 76 65 64 5f 66 6c 61 67 73 3b 20 20 20 20  saved_flags;    
2da16 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c      /* Saved val
2da17 75 65 20 6f 66 20 74 68 65 20 64 62 2d 3e 66 6c  ue of the db->fl
2da18 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  ags */.  int sav
2da19 65 64 5f 6e 43 68 61 6e 67 65 3b 20 20 20 20 20  ed_nChange;     
2da1a 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20   /* Saved value 
2da1b 6f 66 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a  of db->nChange *
2da1c 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 6e 54  /.  int saved_nT
2da1d 6f 74 61 6c 43 68 61 6e 67 65 3b 20 2f 2a 20 53  otalChange; /* S
2da1e 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62  aved value of db
2da1f 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2a  ->nTotalChange *
2da20 2f 0a 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b  /.  Db *pDb = 0;
2da21 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2da22 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 61 63  atabase to detac
2da23 68 20 61 74 20 65 6e 64 20 6f 66 20 76 61 63 75  h at end of vacu
2da24 75 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 65  um */.  int isMe
2da25 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  mDb;            
2da26 2f 2a 20 54 72 75 65 20 69 66 20 76 61 63 75 75  /* True if vacuu
2da27 6d 69 6e 67 20 61 20 3a 6d 65 6d 6f 72 79 3a 20  ming a :memory: 
2da28 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
2da29 74 20 6e 52 65 73 3b 0a 0a 20 20 69 66 28 20 21  t nRes;..  if( !
2da2a 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
2da2b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
2da2c 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c  String(pzErrMsg,
2da2d 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 56 41 43   db, "cannot VAC
2da2e 55 55 4d 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  UUM from within 
2da2f 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b  a transaction");
2da30 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2da31 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
2da32 20 2f 2a 20 53 61 76 65 20 74 68 65 20 63 75 72   /* Save the cur
2da33 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
2da34 65 20 64 61 74 61 62 61 73 65 20 66 6c 61 67 73  e database flags
2da35 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20   so that it can 
2da36 62 65 20 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65  be .  ** restore
2da37 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  d before returni
2da38 6e 67 2e 20 54 68 65 6e 20 73 65 74 20 74 68 65  ng. Then set the
2da39 20 77 72 69 74 61 62 6c 65 2d 73 63 68 65 6d 61   writable-schema
2da3a 20 66 6c 61 67 2c 20 61 6e 64 0a 20 20 2a 2a 20   flag, and.  ** 
2da3b 64 69 73 61 62 6c 65 20 43 48 45 43 4b 20 61 6e  disable CHECK an
2da3c 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  d foreign key co
2da3d 6e 73 74 72 61 69 6e 74 73 2e 20 20 2a 2f 0a 20  nstraints.  */. 
2da3e 20 73 61 76 65 64 5f 66 6c 61 67 73 20 3d 20 64   saved_flags = d
2da3f 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 73 61 76 65  b->flags;.  save
2da40 64 5f 6e 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e  d_nChange = db->
2da41 6e 43 68 61 6e 67 65 3b 0a 20 20 73 61 76 65 64  nChange;.  saved
2da42 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 3d 20  _nTotalChange = 
2da43 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65  db->nTotalChange
2da44 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d  ;.  db->flags |=
2da45 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
2da46 65 6d 61 20 7c 20 53 51 4c 49 54 45 5f 49 67 6e  ema | SQLITE_Ign
2da47 6f 72 65 43 68 65 63 6b 73 3b 0a 20 20 64 62 2d  oreChecks;.  db-
2da48 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54  >flags &= ~SQLIT
2da49 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 3b 0a 0a  E_ForeignKeys;..
2da4a 20 20 70 4d 61 69 6e 20 3d 20 64 62 2d 3e 61 44    pMain = db->aD
2da4b 62 5b 30 5d 2e 70 42 74 3b 0a 20 20 69 73 4d 65  b[0].pBt;.  isMe
2da4c 6d 44 62 20 3d 20 73 71 6c 69 74 65 33 50 61 67  mDb = sqlite3Pag
2da4d 65 72 49 73 4d 65 6d 64 62 28 73 71 6c 69 74 65  erIsMemdb(sqlite
2da4e 33 42 74 72 65 65 50 61 67 65 72 28 70 4d 61 69  3BtreePager(pMai
2da4f 6e 29 29 3b 0a 0a 20 20 2f 2a 20 41 74 74 61 63  n));..  /* Attac
2da50 68 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  h the temporary 
2da51 64 61 74 61 62 61 73 65 20 61 73 20 27 76 61 63  database as 'vac
2da52 75 75 6d 5f 64 62 27 2e 20 54 68 65 20 73 79 6e  uum_db'. The syn
2da53 63 68 72 6f 6e 6f 75 73 20 70 72 61 67 6d 61 0a  chronous pragma.
2da54 20 20 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20    ** can be set 
2da55 74 6f 20 27 6f 66 66 27 20 66 6f 72 20 74 68 69  to 'off' for thi
2da56 73 20 66 69 6c 65 2c 20 61 73 20 69 74 20 69 73  s file, as it is
2da57 20 6e 6f 74 20 72 65 63 6f 76 65 72 65 64 20 69   not recovered i
2da58 66 20 61 20 63 72 61 73 68 0a 20 20 2a 2a 20 6f  f a crash.  ** o
2da59 63 63 75 72 73 20 61 6e 79 77 61 79 2e 20 54 68  ccurs anyway. Th
2da5a 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74  e integrity of t
2da5b 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6d  he database is m
2da5c 61 69 6e 74 61 69 6e 65 64 20 62 79 20 61 0a 20  aintained by a. 
2da5d 20 2a 2a 20 28 70 6f 73 73 69 62 6c 79 20 73 79   ** (possibly sy
2da5e 6e 63 68 72 6f 6e 6f 75 73 29 20 74 72 61 6e 73  nchronous) trans
2da5f 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 6f 6e  action opened on
2da60 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
2da61 73 65 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 73  se before.  ** s
2da62 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46  qlite3BtreeCopyF
2da63 69 6c 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e  ile() is called.
2da64 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 6f 70  .  **.  ** An op
2da65 74 69 6d 69 73 61 74 69 6f 6e 20 77 6f 75 6c 64  timisation would
2da66 20 62 65 20 74 6f 20 75 73 65 20 61 20 6e 6f 6e   be to use a non
2da67 2d 6a 6f 75 72 6e 61 6c 65 64 20 70 61 67 65 72  -journaled pager
2da68 2e 0a 20 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20  ..  ** (Later:) 
2da69 49 20 74 72 69 65 64 20 73 65 74 74 69 6e 67 20  I tried setting 
2da6a 22 50 52 41 47 4d 41 20 76 61 63 75 75 6d 5f 64  "PRAGMA vacuum_d
2da6b 62 2e 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f  b.journal_mode=O
2da6c 46 46 22 20 62 75 74 0a 20 20 2a 2a 20 74 68 61  FF" but.  ** tha
2da6d 74 20 61 63 74 75 61 6c 6c 79 20 6d 61 64 65 20  t actually made 
2da6e 74 68 65 20 56 41 43 55 55 4d 20 72 75 6e 20 73  the VACUUM run s
2da6f 6c 6f 77 65 72 2e 20 20 56 65 72 79 20 6c 69 74  lower.  Very lit
2da70 74 6c 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0a  tle journalling.
2da71 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6f 63    ** actually oc
2da72 63 75 72 73 20 77 68 65 6e 20 64 6f 69 6e 67 20  curs when doing 
2da73 61 20 76 61 63 75 75 6d 20 73 69 6e 63 65 20 74  a vacuum since t
2da74 68 65 20 76 61 63 75 75 6d 5f 64 62 20 69 73 20  he vacuum_db is 
2da75 69 6e 69 74 69 61 6c 6c 79 0a 20 20 2a 2a 20 65  initially.  ** e
2da76 6d 70 74 79 2e 20 20 4f 6e 6c 79 20 74 68 65 20  mpty.  Only the 
2da77 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69  journal header i
2da78 73 20 77 72 69 74 74 65 6e 2e 20 20 41 70 70 61  s written.  Appa
2da79 72 65 6e 74 6c 79 20 69 74 20 74 61 6b 65 73 20  rently it takes 
2da7a 6d 6f 72 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74  more.  ** time t
2da7b 6f 20 70 61 72 73 65 20 61 6e 64 20 72 75 6e 20  o parse and run 
2da7c 74 68 65 20 50 52 41 47 4d 41 20 74 6f 20 74 75  the PRAGMA to tu
2da7d 72 6e 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f  rn journalling o
2da7e 66 66 20 74 68 61 6e 20 69 74 20 64 6f 65 73 0a  ff than it does.
2da7f 20 20 2a 2a 20 74 6f 20 77 72 69 74 65 20 74 68    ** to write th
2da80 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
2da81 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 7a 53   file..  */.  zS
2da82 71 6c 20 3d 20 22 41 54 54 41 43 48 20 27 27 20  ql = "ATTACH '' 
2da83 41 53 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a  AS vacuum_db;";.
2da84 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64    rc = execSql(d
2da85 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20  b, zSql);.  if( 
2da86 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2da87 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
2da88 75 6d 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d  um;.  pDb = &db-
2da89 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b  >aDb[db->nDb-1];
2da8a 0a 20 20 61 73 73 65 72 74 28 20 73 74 72 63 6d  .  assert( strcm
2da8b 70 28 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44  p(db->aDb[db->nD
2da8c 62 2d 31 5d 2e 7a 4e 61 6d 65 2c 22 76 61 63 75  b-1].zName,"vacu
2da8d 75 6d 5f 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20  um_db")==0 );.  
2da8e 70 54 65 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b  pTemp = db->aDb[
2da8f 64 62 2d 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a  db->nDb-1].pBt;.
2da90 0a 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 74  .  /* The call t
2da91 6f 20 65 78 65 63 53 71 6c 28 29 20 74 6f 20 61  o execSql() to a
2da92 74 74 61 63 68 20 74 68 65 20 74 65 6d 70 20 64  ttach the temp d
2da93 61 74 61 62 61 73 65 20 68 61 73 20 6c 65 66 74  atabase has left
2da94 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 6c   the file.  ** l
2da95 6f 63 6b 65 64 20 28 61 73 20 74 68 65 72 65 20  ocked (as there 
2da96 77 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  was more than on
2da97 65 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65  e active stateme
2da98 6e 74 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e  nt when the tran
2da99 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20  saction.  ** to 
2da9a 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  read the schema 
2da9b 77 61 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 55  was concluded. U
2da9c 6e 6c 6f 63 6b 20 69 74 20 68 65 72 65 20 73 6f  nlock it here so
2da9d 20 74 68 61 74 20 74 68 69 73 20 64 6f 65 73 6e   that this doesn
2da9e 27 74 0a 20 20 2a 2a 20 63 61 75 73 65 20 70 72  't.  ** cause pr
2da9f 6f 62 6c 65 6d 73 20 66 6f 72 20 74 68 65 20 63  oblems for the c
2daa0 61 6c 6c 20 74 6f 20 42 74 72 65 65 53 65 74 50  all to BtreeSetP
2daa1 61 67 65 53 69 7a 65 28 29 20 62 65 6c 6f 77 2e  ageSize() below.
2daa2 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74    */.  sqlite3Bt
2daa3 72 65 65 43 6f 6d 6d 69 74 28 70 54 65 6d 70 29  reeCommit(pTemp)
2daa4 3b 0a 0a 20 20 6e 52 65 73 20 3d 20 73 71 6c 69  ;..  nRes = sqli
2daa5 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72  te3BtreeGetReser
2daa6 76 65 28 70 4d 61 69 6e 29 3b 0a 0a 20 20 2f 2a  ve(pMain);..  /*
2daa7 20 41 20 56 41 43 55 55 4d 20 63 61 6e 6e 6f 74   A VACUUM cannot
2daa8 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 67 65   change the page
2daa9 73 69 7a 65 20 6f 66 20 61 6e 20 65 6e 63 72 79  size of an encry
2daaa 70 74 65 64 20 64 61 74 61 62 61 73 65 2e 20 2a  pted database. *
2daab 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
2daac 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 20  HAS_CODEC.  if( 
2daad 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65  db->nextPagesize
2daae 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 76   ){.    extern v
2daaf 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 63  oid sqlite3Codec
2dab0 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33 2a 2c  GetKey(sqlite3*,
2dab1 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e   int, void**, in
2dab2 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b 65  t*);.    int nKe
2dab3 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b 65  y;.    char *zKe
2dab4 79 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  y;.    sqlite3Co
2dab5 64 65 63 47 65 74 4b 65 79 28 64 62 2c 20 30 2c  decGetKey(db, 0,
2dab6 20 28 76 6f 69 64 2a 2a 29 26 7a 4b 65 79 2c 20   (void**)&zKey, 
2dab7 26 6e 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20  &nKey);.    if( 
2dab8 6e 4b 65 79 20 29 20 64 62 2d 3e 6e 65 78 74 50  nKey ) db->nextP
2dab9 61 67 65 73 69 7a 65 20 3d 20 30 3b 0a 20 20 7d  agesize = 0;.  }
2daba 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73  .#endif..  if( s
2dabb 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61  qlite3BtreeSetPa
2dabc 67 65 53 69 7a 65 28 70 54 65 6d 70 2c 20 73 71  geSize(pTemp, sq
2dabd 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
2dabe 65 53 69 7a 65 28 70 4d 61 69 6e 29 2c 20 6e 52  eSize(pMain), nR
2dabf 65 73 2c 20 30 29 0a 20 20 20 7c 7c 20 28 21 69  es, 0).   || (!i
2dac0 73 4d 65 6d 44 62 20 26 26 20 73 71 6c 69 74 65  sMemDb && sqlite
2dac1 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a  3BtreeSetPageSiz
2dac2 65 28 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78  e(pTemp, db->nex
2dac3 74 50 61 67 65 73 69 7a 65 2c 20 6e 52 65 73 2c  tPagesize, nRes,
2dac4 20 30 29 29 0a 20 20 20 7c 7c 20 4e 45 56 45 52   0)).   || NEVER
2dac5 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  (db->mallocFaile
2dac6 64 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d  d).  ){.    rc =
2dac7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2dac8 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76     goto end_of_v
2dac9 61 63 75 75 6d 3b 0a 20 20 7d 0a 20 20 72 63 20  acuum;.  }.  rc 
2daca 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 22 50  = execSql(db, "P
2dacb 52 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e  RAGMA vacuum_db.
2dacc 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 22  synchronous=OFF"
2dacd 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2dace 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f  ITE_OK ){.    go
2dacf 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2dad0 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
2dad1 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
2dad2 41 43 55 55 4d 0a 20 20 73 71 6c 69 74 65 33 42  ACUUM.  sqlite3B
2dad3 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75  treeSetAutoVacuu
2dad4 6d 28 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78  m(pTemp, db->nex
2dad5 74 41 75 74 6f 76 61 63 3e 3d 30 20 3f 20 64 62  tAutovac>=0 ? db
2dad6 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 20 3a 0a  ->nextAutovac :.
2dad7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dad8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dad9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2dada 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61  e3BtreeGetAutoVa
2dadb 63 75 75 6d 28 70 4d 61 69 6e 29 29 3b 0a 23 65  cuum(pMain));.#e
2dadc 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  ndif..  /* Begin
2dadd 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a   a transaction *
2dade 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c  /.  rc = execSql
2dadf 28 64 62 2c 20 22 42 45 47 49 4e 20 45 58 43 4c  (db, "BEGIN EXCL
2dae0 55 53 49 56 45 3b 22 29 3b 0a 20 20 69 66 28 20  USIVE;");.  if( 
2dae1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2dae2 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
2dae3 75 6d 3b 0a 0a 20 20 2f 2a 20 51 75 65 72 79 20  um;..  /* Query 
2dae4 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 74 68  the schema of th
2dae5 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
2dae6 20 43 72 65 61 74 65 20 61 20 6d 69 72 72 6f 72   Create a mirror
2dae7 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 69 6e 20   schema.  ** in 
2dae8 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
2dae9 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72  tabase..  */.  r
2daea 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28  c = execExecSql(
2daeb 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45  db, .      "SELE
2daec 43 54 20 27 43 52 45 41 54 45 20 54 41 42 4c 45  CT 'CREATE TABLE
2daed 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
2daee 73 75 62 73 74 72 28 73 71 6c 2c 31 34 29 20 22  substr(sql,14) "
2daef 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73  .      "  FROM s
2daf0 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
2daf1 52 45 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20  RE type='table' 
2daf2 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74  AND name!='sqlit
2daf3 65 5f 73 65 71 75 65 6e 63 65 27 22 0a 20 20 20  e_sequence'".   
2daf4 20 20 20 22 20 20 20 41 4e 44 20 72 6f 6f 74 70     "   AND rootp
2daf5 61 67 65 3e 30 22 0a 20 20 29 3b 0a 20 20 69 66  age>0".  );.  if
2daf6 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2daf7 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
2daf8 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65  cuum;.  rc = exe
2daf9 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
2dafa 20 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45      "SELECT 'CRE
2dafb 41 54 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d  ATE INDEX vacuum
2dafc 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28  _db.' || substr(
2dafd 73 71 6c 2c 31 34 29 22 0a 20 20 20 20 20 20 22  sql,14)".      "
2dafe 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61    FROM sqlite_ma
2daff 73 74 65 72 20 57 48 45 52 45 20 73 71 6c 20 4c  ster WHERE sql L
2db00 49 4b 45 20 27 43 52 45 41 54 45 20 49 4e 44 45  IKE 'CREATE INDE
2db01 58 20 25 27 20 22 29 3b 0a 20 20 69 66 28 20 72  X %' ");.  if( r
2db02 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
2db03 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2db04 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  m;.  rc = execEx
2db05 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20  ecSql(db, .     
2db06 20 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45   "SELECT 'CREATE
2db07 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 76 61   UNIQUE INDEX va
2db08 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62  cuum_db.' || sub
2db09 73 74 72 28 73 71 6c 2c 32 31 29 20 22 0a 20 20  str(sql,21) ".  
2db0a 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69      "  FROM sqli
2db0b 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20  te_master WHERE 
2db0c 73 71 6c 20 4c 49 4b 45 20 27 43 52 45 41 54 45  sql LIKE 'CREATE
2db0d 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 25 27   UNIQUE INDEX %'
2db0e 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ");.  if( rc!=SQ
2db0f 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2db10 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20  nd_of_vacuum;.. 
2db11 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
2db12 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
2db13 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2db14 2e 20 46 6f 72 20 65 61 63 68 2c 20 64 6f 0a 20  . For each, do. 
2db15 20 2a 2a 20 61 6e 20 22 49 4e 53 45 52 54 20 49   ** an "INSERT I
2db16 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 78 78  NTO vacuum_db.xx
2db17 78 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  x SELECT * FROM 
2db18 6d 61 69 6e 2e 78 78 78 3b 22 20 74 6f 20 63 6f  main.xxx;" to co
2db19 70 79 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74  py.  ** the cont
2db1a 65 6e 74 73 20 74 6f 20 74 68 65 20 74 65 6d 70  ents to the temp
2db1b 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a  orary database..
2db1c 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63    */.  rc = exec
2db1d 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20  ExecSql(db, .   
2db1e 20 20 20 22 53 45 4c 45 43 54 20 27 49 4e 53 45     "SELECT 'INSE
2db1f 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64  RT INTO vacuum_d
2db20 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d  b.' || quote(nam
2db21 65 29 20 22 0a 20 20 20 20 20 20 22 7c 7c 20 27  e) ".      "|| '
2db22 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6d   SELECT * FROM m
2db23 61 69 6e 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e  ain.' || quote(n
2db24 61 6d 65 29 20 7c 7c 20 27 3b 27 22 0a 20 20 20  ame) || ';'".   
2db25 20 20 20 22 46 52 4f 4d 20 6d 61 69 6e 2e 73 71     "FROM main.sq
2db26 6c 69 74 65 5f 6d 61 73 74 65 72 20 22 0a 20 20  lite_master ".  
2db27 20 20 20 20 22 57 48 45 52 45 20 74 79 70 65 20      "WHERE type 
2db28 3d 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61  = 'table' AND na
2db29 6d 65 21 3d 27 73 71 6c 69 74 65 5f 73 65 71 75  me!='sqlite_sequ
2db2a 65 6e 63 65 27 20 22 0a 20 20 20 20 20 20 22 20  ence' ".      " 
2db2b 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3e 30 22   AND rootpage>0"
2db2c 0a 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21  ..  );.  if( rc!
2db2d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
2db2e 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
2db2f 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72  ..  /* Copy over
2db30 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 74 61   the sequence ta
2db31 62 6c 65 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  ble.  */.  rc = 
2db32 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20  execExecSql(db, 
2db33 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27  .      "SELECT '
2db34 44 45 4c 45 54 45 20 46 52 4f 4d 20 76 61 63 75  DELETE FROM vacu
2db35 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65  um_db.' || quote
2db36 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a  (name) || ';' ".
2db37 20 20 20 20 20 20 22 46 52 4f 4d 20 76 61 63 75        "FROM vacu
2db38 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73  um_db.sqlite_mas
2db39 74 65 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 27  ter WHERE name='
2db3a 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27  sqlite_sequence'
2db3b 20 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63   ".  );.  if( rc
2db3c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2db3d 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2db3e 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  ;.  rc = execExe
2db3f 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
2db40 22 53 45 4c 45 43 54 20 27 49 4e 53 45 52 54 20  "SELECT 'INSERT 
2db41 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27  INTO vacuum_db.'
2db42 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20   || quote(name) 
2db43 22 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53 45  ".      "|| ' SE
2db44 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6d 61 69 6e  LECT * FROM main
2db45 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65  .' || quote(name
2db46 29 20 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20  ) || ';' ".     
2db47 20 22 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62   "FROM vacuum_db
2db48 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57  .sqlite_master W
2db49 48 45 52 45 20 6e 61 6d 65 3d 3d 27 73 71 6c 69  HERE name=='sqli
2db4a 74 65 5f 73 65 71 75 65 6e 63 65 27 3b 22 0a 20  te_sequence';". 
2db4b 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
2db4c 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2db4d 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 0a  nd_of_vacuum;...
2db4e 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 74 72    /* Copy the tr
2db4f 69 67 67 65 72 73 2c 20 76 69 65 77 73 2c 20 61  iggers, views, a
2db50 6e 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  nd virtual table
2db51 73 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20  s from the main 
2db52 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 6f 76  database.  ** ov
2db53 65 72 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72  er to the tempor
2db54 61 72 79 20 64 61 74 61 62 61 73 65 2e 20 20 4e  ary database.  N
2db55 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a  one of these obj
2db56 65 63 74 73 20 68 61 73 20 61 6e 79 0a 20 20 2a  ects has any.  *
2db57 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 6f  * associated sto
2db58 72 61 67 65 2c 20 73 6f 20 61 6c 6c 20 77 65 20  rage, so all we 
2db59 68 61 76 65 20 74 6f 20 64 6f 20 69 73 20 63 6f  have to do is co
2db5a 70 79 20 74 68 65 69 72 20 65 6e 74 72 69 65 73  py their entries
2db5b 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53  .  ** from the S
2db5c 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
2db5d 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  le..  */.  rc = 
2db5e 65 78 65 63 53 71 6c 28 64 62 2c 0a 20 20 20 20  execSql(db,.    
2db5f 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 76    "INSERT INTO v
2db60 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f  acuum_db.sqlite_
2db61 6d 61 73 74 65 72 20 22 0a 20 20 20 20 20 20 22  master ".      "
2db62 20 20 53 45 4c 45 43 54 20 74 79 70 65 2c 20 6e    SELECT type, n
2db63 61 6d 65 2c 20 74 62 6c 5f 6e 61 6d 65 2c 20 72  ame, tbl_name, r
2db64 6f 6f 74 70 61 67 65 2c 20 73 71 6c 22 0a 20 20  ootpage, sql".  
2db65 20 20 20 20 22 20 20 20 20 46 52 4f 4d 20 6d 61      "    FROM ma
2db66 69 6e 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  in.sqlite_master
2db67 22 0a 20 20 20 20 20 20 22 20 20 20 57 48 45 52  ".      "   WHER
2db68 45 20 74 79 70 65 3d 27 76 69 65 77 27 20 4f 52  E type='view' OR
2db69 20 74 79 70 65 3d 27 74 72 69 67 67 65 72 27 22   type='trigger'"
2db6a 0a 20 20 20 20 20 20 22 20 20 20 20 20 20 4f 52  .      "      OR
2db6b 20 28 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41   (type='table' A
2db6c 4e 44 20 72 6f 6f 74 70 61 67 65 3d 30 29 22 0a  ND rootpage=0)".
2db6d 20 20 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20    );.  if( rc ) 
2db6e 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
2db6f 75 6d 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69  um;..  /* At thi
2db70 73 20 70 6f 69 6e 74 2c 20 75 6e 6c 65 73 73 20  s point, unless 
2db71 74 68 65 20 6d 61 69 6e 20 64 62 20 77 61 73 20  the main db was 
2db72 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79  completely empty
2db73 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 77 20 61  , there is now a
2db74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
2db75 6e 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61  n open on the va
2db76 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 62  cuum database, b
2db77 75 74 20 6e 6f 74 20 6f 6e 20 74 68 65 20 6d 61  ut not on the ma
2db78 69 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  in database..  *
2db79 2a 20 4f 70 65 6e 20 61 20 62 74 72 65 65 20 6c  * Open a btree l
2db7a 65 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e  evel transaction
2db7b 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   on the main dat
2db7c 61 62 61 73 65 2e 20 54 68 69 73 20 61 6c 6c 6f  abase. This allo
2db7d 77 73 20 61 0a 20 20 2a 2a 20 63 61 6c 6c 20 74  ws a.  ** call t
2db7e 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  o sqlite3BtreeCo
2db7f 70 79 46 69 6c 65 28 29 2e 20 54 68 65 20 6d 61  pyFile(). The ma
2db80 69 6e 20 64 61 74 61 62 61 73 65 20 62 74 72 65  in database btre
2db81 65 20 6c 65 76 65 6c 0a 20 20 2a 2a 20 74 72 61  e level.  ** tra
2db82 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 68 65 6e  nsaction is then
2db83 20 63 6f 6d 6d 69 74 74 65 64 2c 20 73 6f 20 74   committed, so t
2db84 68 65 20 53 51 4c 20 6c 65 76 65 6c 20 6e 65 76  he SQL level nev
2db85 65 72 20 6b 6e 6f 77 73 20 69 74 20 77 61 73 0a  er knows it was.
2db86 20 20 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20    ** opened for 
2db87 77 72 69 74 69 6e 67 2e 20 54 68 69 73 20 77 61  writing. This wa
2db88 79 2c 20 74 68 65 20 53 51 4c 20 74 72 61 6e 73  y, the SQL trans
2db89 61 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63  action used to c
2db8a 72 65 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 74  reate the.  ** t
2db8b 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
2db8c 65 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f  e never needs to
2db8d 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 20   be committed.. 
2db8e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 33 32 20   */.  {.    u32 
2db8f 6d 65 74 61 3b 0a 20 20 20 20 69 6e 74 20 69 3b  meta;.    int i;
2db90 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 61 72  ..    /* This ar
2db91 72 61 79 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ray determines w
2db92 68 69 63 68 20 6d 65 74 61 20 6d 65 74 61 20 76  hich meta meta v
2db93 61 6c 75 65 73 20 61 72 65 20 70 72 65 73 65 72  alues are preser
2db94 76 65 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a  ved in the.    *
2db95 2a 20 76 61 63 75 75 6d 2e 20 20 45 76 65 6e 20  * vacuum.  Even 
2db96 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20  entries are the 
2db97 6d 65 74 61 20 76 61 6c 75 65 20 6e 75 6d 62 65  meta value numbe
2db98 72 20 61 6e 64 20 6f 64 64 20 65 6e 74 72 69 65  r and odd entrie
2db99 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 61 6e 20  s.    ** are an 
2db9a 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 61 70 70  increment to app
2db9b 6c 79 20 74 6f 20 74 68 65 20 6d 65 74 61 20 76  ly to the meta v
2db9c 61 6c 75 65 20 61 66 74 65 72 20 74 68 65 20 76  alue after the v
2db9d 61 63 75 75 6d 2e 0a 20 20 20 20 2a 2a 20 54 68  acuum..    ** Th
2db9e 65 20 69 6e 63 72 65 6d 65 6e 74 20 69 73 20 75  e increment is u
2db9f 73 65 64 20 74 6f 20 69 6e 63 72 65 61 73 65 20  sed to increase 
2dba0 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
2dba1 65 20 73 6f 20 74 68 61 74 20 6f 74 68 65 72 0a  e so that other.
2dba2 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f      ** connectio
2dba3 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64  ns to the same d
2dba4 61 74 61 62 61 73 65 20 77 69 6c 6c 20 6b 6e 6f  atabase will kno
2dba5 77 20 74 6f 20 72 65 72 65 61 64 20 74 68 65 20  w to reread the 
2dba6 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20  schema..    */. 
2dba7 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
2dba8 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 43  unsigned char aC
2dba9 6f 70 79 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  opy[] = {.      
2dbaa 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45   BTREE_SCHEMA_VE
2dbab 52 53 49 4f 4e 2c 20 20 20 20 20 31 2c 20 20 2f  RSION,     1,  /
2dbac 2a 20 41 64 64 20 6f 6e 65 20 74 6f 20 74 68 65  * Add one to the
2dbad 20 6f 6c 64 20 73 63 68 65 6d 61 20 63 6f 6f 6b   old schema cook
2dbae 69 65 20 2a 2f 0a 20 20 20 20 20 20 20 42 54 52  ie */.       BTR
2dbaf 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  EE_DEFAULT_CACHE
2dbb0 5f 53 49 5a 45 2c 20 30 2c 20 20 2f 2a 20 50 72  _SIZE, 0,  /* Pr
2dbb1 65 73 65 72 76 65 20 74 68 65 20 64 65 66 61 75  eserve the defau
2dbb2 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 73 69  lt page cache si
2dbb3 7a 65 20 2a 2f 0a 20 20 20 20 20 20 20 42 54 52  ze */.       BTR
2dbb4 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47  EE_TEXT_ENCODING
2dbb5 2c 20 20 20 20 20 20 30 2c 20 20 2f 2a 20 50 72  ,      0,  /* Pr
2dbb6 65 73 65 72 76 65 20 74 68 65 20 74 65 78 74 20  eserve the text 
2dbb7 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20  encoding */.    
2dbb8 20 20 20 42 54 52 45 45 5f 55 53 45 52 5f 56 45     BTREE_USER_VE
2dbb9 52 53 49 4f 4e 2c 20 20 20 20 20 20 20 30 2c 20  RSION,       0, 
2dbba 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74 68 65   /* Preserve the
2dbbb 20 75 73 65 72 20 76 65 72 73 69 6f 6e 20 2a 2f   user version */
2dbbc 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 61 73 73  .    };..    ass
2dbbd 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42  ert( 1==sqlite3B
2dbbe 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 54  treeIsInTrans(pT
2dbbf 65 6d 70 29 20 29 3b 0a 20 20 20 20 61 73 73 65  emp) );.    asse
2dbc0 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42 74  rt( 1==sqlite3Bt
2dbc1 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 4d 61  reeIsInTrans(pMa
2dbc2 69 6e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  in) );..    /* C
2dbc3 6f 70 79 20 42 74 72 65 65 20 6d 65 74 61 20 76  opy Btree meta v
2dbc4 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 66 6f 72  alues */.    for
2dbc5 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a  (i=0; i<ArraySiz
2dbc6 65 28 61 43 6f 70 79 29 3b 20 69 2b 3d 32 29 7b  e(aCopy); i+=2){
2dbc7 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 4d 65 74  .      /* GetMet
2dbc8 61 28 29 20 61 6e 64 20 55 70 64 61 74 65 4d 65  a() and UpdateMe
2dbc9 74 61 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c  ta() cannot fail
2dbca 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74   in this context
2dbcb 20 62 65 63 61 75 73 65 0a 20 20 20 20 20 20 2a   because.      *
2dbcc 2a 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76  * we already hav
2dbcd 65 20 70 61 67 65 20 31 20 6c 6f 61 64 65 64 20  e page 1 loaded 
2dbce 69 6e 74 6f 20 63 61 63 68 65 20 61 6e 64 20 6d  into cache and m
2dbcf 61 72 6b 65 64 20 64 69 72 74 79 2e 20 2a 2f 0a  arked dirty. */.
2dbd0 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
2dbd1 65 65 47 65 74 4d 65 74 61 28 70 4d 61 69 6e 2c  eeGetMeta(pMain,
2dbd2 20 61 43 6f 70 79 5b 69 5d 2c 20 26 6d 65 74 61   aCopy[i], &meta
2dbd3 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
2dbd4 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
2dbd5 4d 65 74 61 28 70 54 65 6d 70 2c 20 61 43 6f 70  Meta(pTemp, aCop
2dbd6 79 5b 69 5d 2c 20 6d 65 74 61 2b 61 43 6f 70 79  y[i], meta+aCopy
2dbd7 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66  [i+1]);.      if
2dbd8 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49  ( NEVER(rc!=SQLI
2dbd9 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 65 6e  TE_OK) ) goto en
2dbda 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 20  d_of_vacuum;.   
2dbdb 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   }..    rc = sql
2dbdc 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c  ite3BtreeCopyFil
2dbdd 65 28 70 4d 61 69 6e 2c 20 70 54 65 6d 70 29 3b  e(pMain, pTemp);
2dbde 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
2dbdf 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
2dbe0 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 20  d_of_vacuum;.   
2dbe1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
2dbe2 65 65 43 6f 6d 6d 69 74 28 70 54 65 6d 70 29 3b  eeCommit(pTemp);
2dbe3 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
2dbe4 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
2dbe5 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 23 69 66  d_of_vacuum;.#if
2dbe6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2dbe7 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
2dbe8 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41  sqlite3BtreeSetA
2dbe9 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e 2c  utoVacuum(pMain,
2dbea 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
2dbeb 41 75 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70  AutoVacuum(pTemp
2dbec 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a  ));.#endif.  }..
2dbed 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
2dbee 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20  LITE_OK );.  rc 
2dbef 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  = sqlite3BtreeSe
2dbf0 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 2c  tPageSize(pMain,
2dbf1 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
2dbf2 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 29 2c  PageSize(pTemp),
2dbf3 20 6e 52 65 73 2c 31 29 3b 0a 0a 65 6e 64 5f 6f   nRes,1);..end_o
2dbf4 66 5f 76 61 63 75 75 6d 3a 0a 20 20 2f 2a 20 52  f_vacuum:.  /* R
2dbf5 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69  estore the origi
2dbf6 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 64 62 2d  nal value of db-
2dbf7 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 64 62 2d 3e  >flags */.  db->
2dbf8 66 6c 61 67 73 20 3d 20 73 61 76 65 64 5f 66 6c  flags = saved_fl
2dbf9 61 67 73 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e  ags;.  db->nChan
2dbfa 67 65 20 3d 20 73 61 76 65 64 5f 6e 43 68 61 6e  ge = saved_nChan
2dbfb 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c  ge;.  db->nTotal
2dbfc 43 68 61 6e 67 65 20 3d 20 73 61 76 65 64 5f 6e  Change = saved_n
2dbfd 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 0a 20 20  TotalChange;..  
2dbfe 2f 2a 20 43 75 72 72 65 6e 74 6c 79 20 74 68 65  /* Currently the
2dbff 72 65 20 69 73 20 61 6e 20 53 51 4c 20 6c 65 76  re is an SQL lev
2dc00 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  el transaction o
2dc01 70 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75 75  pen on the vacuu
2dc02 6d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e  m.  ** database.
2dc03 20 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65   No locks are he
2dc04 6c 64 20 6f 6e 20 61 6e 79 20 6f 74 68 65 72 20  ld on any other 
2dc05 66 69 6c 65 73 20 28 73 69 6e 63 65 20 74 68 65  files (since the
2dc06 20 6d 61 69 6e 20 66 69 6c 65 0a 20 20 2a 2a 20   main file.  ** 
2dc07 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 20 61 74  was committed at
2dc08 20 74 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c   the btree level
2dc09 29 2e 20 53 6f 20 69 74 20 73 61 66 65 20 74 6f  ). So it safe to
2dc0a 20 65 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63   end the transac
2dc0b 74 69 6f 6e 0a 20 20 2a 2a 20 62 79 20 6d 61 6e  tion.  ** by man
2dc0c 75 61 6c 6c 79 20 73 65 74 74 69 6e 67 20 74 68  ually setting th
2dc0d 65 20 61 75 74 6f 43 6f 6d 6d 69 74 20 66 6c 61  e autoCommit fla
2dc0e 67 20 74 6f 20 74 72 75 65 20 61 6e 64 20 64 65  g to true and de
2dc0f 74 61 63 68 69 6e 67 20 74 68 65 0a 20 20 2a 2a  taching the.  **
2dc10 20 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65   vacuum database
2dc11 2e 20 54 68 65 20 76 61 63 75 75 6d 5f 64 62 20  . The vacuum_db 
2dc12 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
2dc13 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65  deleted when the
2dc14 20 70 61 67 65 72 0a 20 20 2a 2a 20 69 73 20 63   pager.  ** is c
2dc15 6c 6f 73 65 64 20 62 79 20 74 68 65 20 44 45 54  losed by the DET
2dc16 41 43 48 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e  ACH..  */.  db->
2dc17 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a  autoCommit = 1;.
2dc18 0a 20 20 69 66 28 20 70 44 62 20 29 7b 0a 20 20  .  if( pDb ){.  
2dc19 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
2dc1a 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20  ose(pDb->pBt);. 
2dc1b 20 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b     pDb->pBt = 0;
2dc1c 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d  .    pDb->pSchem
2dc1d 61 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71  a = 0;.  }..  sq
2dc1e 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
2dc1f 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
2dc20 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
2dc21 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49  .#endif  /* SQLI
2dc22 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 26  TE_OMIT_VACUUM &
2dc23 26 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54  & SQLITE_OMIT_AT
2dc24 54 41 43 48 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TACH */../******
2dc25 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2dc26 76 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a  vacuum.c *******
2dc27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc29 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2dc2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2dc2b 69 6c 65 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a  ile vtab.c *****
2dc2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc2e 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
2dc2f 30 30 36 20 4a 75 6e 65 20 31 30 0a 2a 2a 0a 2a  006 June 10.**.*
2dc30 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2dc31 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2dc32 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2dc33 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2dc34 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2dc35 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2dc36 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2dc37 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2dc38 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2dc39 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2dc3a 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2dc3b 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2dc3c 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2dc3d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2dc3e 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2dc3f 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2dc40 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2dc41 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2dc42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
2dc46 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
2dc47 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 68 65   code used to he
2dc48 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 76 69 72  lp implement vir
2dc49 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a  tual tables..*/.
2dc4a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2dc4b 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2dc4c 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 63 74 75  ../*.** The actu
2dc4d 61 6c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  al function that
2dc4e 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f   does the work o
2dc4f 66 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77  f creating a new
2dc50 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 54 68 69 73   module..** This
2dc51 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d   function implem
2dc52 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74 65 33  ents the sqlite3
2dc53 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29  _create_module()
2dc54 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   and.** sqlite3_
2dc55 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32  create_module_v2
2dc56 28 29 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a  () interfaces..*
2dc57 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 65  /.static int cre
2dc58 61 74 65 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c  ateModule(.  sql
2dc59 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2dc5a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2dc5b 44 61 74 61 62 61 73 65 20 69 6e 20 77 68 69 63  Database in whic
2dc5c 68 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69  h module is regi
2dc5d 73 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  stered */.  cons
2dc5e 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
2dc5f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2dc60 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20  ame assigned to 
2dc61 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20  this module */. 
2dc62 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
2dc63 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20  odule *pModule, 
2dc64 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69   /* The definiti
2dc65 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  on of the module
2dc66 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78   */.  void *pAux
2dc67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2dc68 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
2dc69 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72   pointer for xCr
2dc6a 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f  eate/xConnect */
2dc6b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72  .  void (*xDestr
2dc6c 6f 79 29 28 76 6f 69 64 20 2a 29 20 20 20 20 20  oy)(void *)     
2dc6d 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73     /* Module des
2dc6e 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e  tructor function
2dc6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 2c   */.){.  int rc,
2dc70 20 6e 4e 61 6d 65 3b 0a 20 20 4d 6f 64 75 6c 65   nName;.  Module
2dc71 20 2a 70 4d 6f 64 3b 0a 0a 20 20 73 71 6c 69 74   *pMod;..  sqlit
2dc72 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
2dc73 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 6e 4e 61  b->mutex);.  nNa
2dc74 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
2dc75 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 70  en30(zName);.  p
2dc76 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 20 2a 29  Mod = (Module *)
2dc77 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
2dc78 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 6f  aw(db, sizeof(Mo
2dc79 64 75 6c 65 29 20 2b 20 6e 4e 61 6d 65 20 2b 20  dule) + nName + 
2dc7a 31 29 3b 0a 20 20 69 66 28 20 70 4d 6f 64 20 29  1);.  if( pMod )
2dc7b 7b 0a 20 20 20 20 4d 6f 64 75 6c 65 20 2a 70 44  {.    Module *pD
2dc7c 65 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43  el;.    char *zC
2dc7d 6f 70 79 20 3d 20 28 63 68 61 72 20 2a 29 28 26  opy = (char *)(&
2dc7e 70 4d 6f 64 5b 31 5d 29 3b 0a 20 20 20 20 6d 65  pMod[1]);.    me
2dc7f 6d 63 70 79 28 7a 43 6f 70 79 2c 20 7a 4e 61 6d  mcpy(zCopy, zNam
2dc80 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20  e, nName+1);.   
2dc81 20 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 20 3d 20 7a   pMod->zName = z
2dc82 43 6f 70 79 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e  Copy;.    pMod->
2dc83 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 75 6c  pModule = pModul
2dc84 65 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 70 41 75  e;.    pMod->pAu
2dc85 78 20 3d 20 70 41 75 78 3b 0a 20 20 20 20 70 4d  x = pAux;.    pM
2dc86 6f 64 2d 3e 78 44 65 73 74 72 6f 79 20 3d 20 78  od->xDestroy = x
2dc87 44 65 73 74 72 6f 79 3b 0a 20 20 20 20 70 44 65  Destroy;.    pDe
2dc88 6c 20 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71  l = (Module *)sq
2dc89 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
2dc8a 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 43  &db->aModule, zC
2dc8b 6f 70 79 2c 20 6e 4e 61 6d 65 2c 20 28 76 6f 69  opy, nName, (voi
2dc8c 64 2a 29 70 4d 6f 64 29 3b 0a 20 20 20 20 69 66  d*)pMod);.    if
2dc8d 28 20 70 44 65 6c 20 26 26 20 70 44 65 6c 2d 3e  ( pDel && pDel->
2dc8e 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20  xDestroy ){.    
2dc8f 20 20 70 44 65 6c 2d 3e 78 44 65 73 74 72 6f 79    pDel->xDestroy
2dc90 28 70 44 65 6c 2d 3e 70 41 75 78 29 3b 0a 20 20  (pDel->pAux);.  
2dc91 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2dc92 62 46 72 65 65 28 64 62 2c 20 70 44 65 6c 29 3b  bFree(db, pDel);
2dc93 0a 20 20 20 20 69 66 28 20 70 44 65 6c 3d 3d 70  .    if( pDel==p
2dc94 4d 6f 64 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  Mod ){.      db-
2dc95 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2dc96 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  1;.    }.    sql
2dc97 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
2dc98 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
2dc99 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65 73    }else if( xDes
2dc9a 74 72 6f 79 20 29 7b 0a 20 20 20 20 78 44 65 73  troy ){.    xDes
2dc9b 74 72 6f 79 28 70 41 75 78 29 3b 0a 20 20 7d 0a  troy(pAux);.  }.
2dc9c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
2dc9d 69 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45  iExit(db, SQLITE
2dc9e 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  _OK);.  sqlite3_
2dc9f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2dca0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2dca1 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45   rc;.}.../*.** E
2dca2 78 74 65 72 6e 61 6c 20 41 50 49 20 66 75 6e 63  xternal API func
2dca3 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65  tion used to cre
2dca4 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61  ate a new virtua
2dca5 6c 2d 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a  l-table module..
2dca6 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2dca7 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
2dca8 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74  _module(.  sqlit
2dca9 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2dcaa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2dcab 74 61 62 61 73 65 20 69 6e 20 77 68 69 63 68 20  tabase in which 
2dcac 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73 74  module is regist
2dcad 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ered */.  const 
2dcae 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
2dcaf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
2dcb0 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  e assigned to th
2dcb1 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63  is module */.  c
2dcb2 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
2dcb3 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f  ule *pModule,  /
2dcb4 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  * The definition
2dcb5 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a   of the module *
2dcb6 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 20 20  /.  void *pAux  
2dcb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dcb8 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70      /* Context p
2dcb9 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61  ointer for xCrea
2dcba 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29  te/xConnect */.)
2dcbb 7b 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74  {.  return creat
2dcbc 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d  eModule(db, zNam
2dcbd 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78  e, pModule, pAux
2dcbe 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  , 0);.}../*.** E
2dcbf 78 74 65 72 6e 61 6c 20 41 50 49 20 66 75 6e 63  xternal API func
2dcc0 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65  tion used to cre
2dcc1 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61  ate a new virtua
2dcc2 6c 2d 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a  l-table module..
2dcc3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2dcc4 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
2dcc5 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20 73 71  _module_v2(.  sq
2dcc6 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2dcc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2dcc8 20 44 61 74 61 62 61 73 65 20 69 6e 20 77 68 69   Database in whi
2dcc9 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67  ch module is reg
2dcca 69 73 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e  istered */.  con
2dccb 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
2dccc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2dccd 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f  Name assigned to
2dcce 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a   this module */.
2dccf 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
2dcd0 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c  module *pModule,
2dcd1 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74    /* The definit
2dcd2 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  ion of the modul
2dcd3 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75  e */.  void *pAu
2dcd4 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
2dcd5 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
2dcd6 74 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43  t pointer for xC
2dcd7 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a  reate/xConnect *
2dcd8 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74  /.  void (*xDest
2dcd9 72 6f 79 29 28 76 6f 69 64 20 2a 29 20 20 20 20  roy)(void *)    
2dcda 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65      /* Module de
2dcdb 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
2dcdc 6e 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e  n */.){.  return
2dcdd 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62   createModule(db
2dcde 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65  , zName, pModule
2dcdf 2c 20 70 41 75 78 2c 20 78 44 65 73 74 72 6f 79  , pAux, xDestroy
2dce0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  );.}../*.** Lock
2dce1 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2dce2 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61  le so that it ca
2dce3 6e 6e 6f 74 20 62 65 20 64 69 73 63 6f 6e 6e 65  nnot be disconne
2dce4 63 74 65 64 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 6e  cted..** Locks n
2dce5 65 73 74 2e 20 20 45 76 65 72 79 20 6c 6f 63 6b  est.  Every lock
2dce6 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 20 63   should have a c
2dce7 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 75 6e 6c  orresponding unl
2dce8 6f 63 6b 2e 0a 2a 2a 20 49 66 20 61 6e 20 75 6e  ock..** If an un
2dce9 6c 6f 63 6b 20 69 73 20 6f 6d 69 74 74 65 64 2c  lock is omitted,
2dcea 20 72 65 73 6f 75 72 63 65 73 20 6c 65 61 6b 73   resources leaks
2dceb 20 77 69 6c 6c 20 6f 63 63 75 72 2e 20 20 0a 2a   will occur.  .*
2dcec 2a 0a 2a 2a 20 49 66 20 61 20 64 69 73 63 6f 6e  *.** If a discon
2dced 6e 65 63 74 20 69 73 20 61 74 74 65 6d 70 74 65  nect is attempte
2dcee 64 20 77 68 69 6c 65 20 61 20 76 69 72 74 75 61  d while a virtua
2dcef 6c 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65  l table is locke
2dcf0 64 2c 0a 2a 2a 20 74 68 65 20 64 69 73 63 6f 6e  d,.** the discon
2dcf1 6e 65 63 74 20 69 73 20 64 65 66 65 72 72 65 64  nect is deferred
2dcf2 20 75 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73   until all locks
2dcf3 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76   have been remov
2dcf4 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
2dcf5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2dcf6 65 33 56 74 61 62 4c 6f 63 6b 28 56 54 61 62 6c  e3VtabLock(VTabl
2dcf7 65 20 2a 70 56 54 61 62 29 7b 0a 20 20 70 56 54  e *pVTab){.  pVT
2dcf8 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 0a  ab->nRef++;.}...
2dcf9 2f 2a 0a 2a 2a 20 70 54 61 62 20 69 73 20 61 20  /*.** pTab is a 
2dcfa 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 61 62  pointer to a Tab
2dcfb 6c 65 20 73 74 72 75 63 74 75 72 65 20 72 65 70  le structure rep
2dcfc 72 65 73 65 6e 74 69 6e 67 20 61 20 76 69 72 74  resenting a virt
2dcfd 75 61 6c 2d 74 61 62 6c 65 2e 0a 2a 2a 20 52 65  ual-table..** Re
2dcfe 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
2dcff 6f 20 74 68 65 20 56 54 61 62 6c 65 20 6f 62 6a  o the VTable obj
2dd00 65 63 74 20 75 73 65 64 20 62 79 20 63 6f 6e 6e  ect used by conn
2dd01 65 63 74 69 6f 6e 20 64 62 20 74 6f 20 61 63 63  ection db to acc
2dd02 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 76 69 72  ess .** this vir
2dd03 74 75 61 6c 2d 74 61 62 6c 65 2c 20 69 66 20 6f  tual-table, if o
2dd04 6e 65 20 68 61 73 20 62 65 65 6e 20 63 72 65 61  ne has been crea
2dd05 74 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 6f 74 68  ted, or NULL oth
2dd06 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
2dd07 45 5f 50 52 49 56 41 54 45 20 56 54 61 62 6c 65  E_PRIVATE VTable
2dd08 20 2a 73 71 6c 69 74 65 33 47 65 74 56 54 61 62   *sqlite3GetVTab
2dd09 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  le(sqlite3 *db, 
2dd0a 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20  Table *pTab){.  
2dd0b 56 54 61 62 6c 65 20 2a 70 56 74 61 62 3b 0a 20  VTable *pVtab;. 
2dd0c 20 61 73 73 65 72 74 28 20 49 73 56 69 72 74 75   assert( IsVirtu
2dd0d 61 6c 28 70 54 61 62 29 20 29 3b 0a 20 20 66 6f  al(pTab) );.  fo
2dd0e 72 28 70 56 74 61 62 3d 70 54 61 62 2d 3e 70 56  r(pVtab=pTab->pV
2dd0f 54 61 62 6c 65 3b 20 70 56 74 61 62 20 26 26 20  Table; pVtab && 
2dd10 70 56 74 61 62 2d 3e 64 62 21 3d 64 62 3b 20 70  pVtab->db!=db; p
2dd11 56 74 61 62 3d 70 56 74 61 62 2d 3e 70 4e 65 78  Vtab=pVtab->pNex
2dd12 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 74  t);.  return pVt
2dd13 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63  ab;.}../*.** Dec
2dd14 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 2d 63  rement the ref-c
2dd15 6f 75 6e 74 20 6f 6e 20 61 20 76 69 72 74 75 61  ount on a virtua
2dd16 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2e 20  l table object. 
2dd17 57 68 65 6e 20 74 68 65 20 72 65 66 2d 63 6f 75  When the ref-cou
2dd18 6e 74 0a 2a 2a 20 72 65 61 63 68 65 73 20 7a 65  nt.** reaches ze
2dd19 72 6f 2c 20 63 61 6c 6c 20 74 68 65 20 78 44 69  ro, call the xDi
2dd1a 73 63 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f  sconnect() metho
2dd1b 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  d to delete the 
2dd1c 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54  object..*/.SQLIT
2dd1d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2dd1e 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
2dd1f 28 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b  (VTable *pVTab){
2dd20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2dd21 20 70 56 54 61 62 2d 3e 64 62 3b 0a 0a 20 20 61   pVTab->db;..  a
2dd22 73 73 65 72 74 28 20 64 62 20 29 3b 0a 20 20 61  ssert( db );.  a
2dd23 73 73 65 72 74 28 20 70 56 54 61 62 2d 3e 6e 52  ssert( pVTab->nR
2dd24 65 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ef>0 );.  assert
2dd25 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  ( sqlite3SafetyC
2dd26 68 65 63 6b 4f 6b 28 64 62 29 20 29 3b 0a 0a 20  heckOk(db) );.. 
2dd27 20 70 56 54 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a   pVTab->nRef--;.
2dd28 20 20 69 66 28 20 70 56 54 61 62 2d 3e 6e 52 65    if( pVTab->nRe
2dd29 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  f==0 ){.    sqli
2dd2a 74 65 33 5f 76 74 61 62 20 2a 70 20 3d 20 70 56  te3_vtab *p = pV
2dd2b 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20  Tab->pVtab;.    
2dd2c 69 66 28 20 70 20 29 7b 0a 23 69 66 64 65 66 20  if( p ){.#ifdef 
2dd2d 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
2dd2e 20 20 20 69 66 28 20 70 56 54 61 62 2d 3e 64 62     if( pVTab->db
2dd2f 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
2dd30 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20  MAGIC_BUSY ){.  
2dd31 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69        (void)sqli
2dd32 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
2dd33 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4d 6f  ;.        p->pMo
2dd34 64 75 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63  dule->xDisconnec
2dd35 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 28 76  t(p);.        (v
2dd36 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
2dd37 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 20 20 7d  yOn(db);.      }
2dd38 20 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20   else.#endif.   
2dd39 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e     {.        p->
2dd3a 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73 63 6f 6e  pModule->xDiscon
2dd3b 6e 65 63 74 28 70 29 3b 0a 20 20 20 20 20 20 7d  nect(p);.      }
2dd3c 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2dd3d 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 54  e3DbFree(db, pVT
2dd3e 61 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ab);.  }.}../*.*
2dd3f 2a 20 54 61 62 6c 65 20 70 20 69 73 20 61 20 76  * Table p is a v
2dd40 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 54 68  irtual table. Th
2dd41 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 76 65  is function move
2dd42 73 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 69  s all elements i
2dd43 6e 20 74 68 65 0a 2a 2a 20 70 2d 3e 70 56 54 61  n the.** p->pVTa
2dd44 62 6c 65 20 6c 69 73 74 20 74 6f 20 74 68 65 20  ble list to the 
2dd45 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e  sqlite3.pDisconn
2dd46 65 63 74 20 6c 69 73 74 73 20 6f 66 20 74 68 65  ect lists of the
2dd47 69 72 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  ir associated.**
2dd48 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
2dd49 74 69 6f 6e 73 20 74 6f 20 62 65 20 64 69 73 63  tions to be disc
2dd4a 6f 6e 6e 65 63 74 65 64 20 61 74 20 74 68 65 20  onnected at the 
2dd4b 6e 65 78 74 20 6f 70 70 6f 72 74 75 6e 69 74 79  next opportunity
2dd4c 2e 20 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66  . .** Except, if
2dd4d 20 61 72 67 75 6d 65 6e 74 20 64 62 20 69 73 20   argument db is 
2dd4e 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  not NULL, then t
2dd4f 68 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61  he entry associa
2dd50 74 65 64 20 77 69 74 68 0a 2a 2a 20 63 6f 6e 6e  ted with.** conn
2dd51 65 63 74 69 6f 6e 20 64 62 20 69 73 20 6c 65 66  ection db is lef
2dd52 74 20 69 6e 20 74 68 65 20 70 2d 3e 70 56 54 61  t in the p->pVTa
2dd53 62 6c 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61  ble list..*/.sta
2dd54 74 69 63 20 56 54 61 62 6c 65 20 2a 76 74 61 62  tic VTable *vtab
2dd55 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28 73 71  DisconnectAll(sq
2dd56 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65  lite3 *db, Table
2dd57 20 2a 70 29 7b 0a 20 20 56 54 61 62 6c 65 20 2a   *p){.  VTable *
2dd58 70 52 65 74 20 3d 20 30 3b 0a 20 20 56 54 61 62  pRet = 0;.  VTab
2dd59 6c 65 20 2a 70 56 54 61 62 6c 65 20 3d 20 70 2d  le *pVTable = p-
2dd5a 3e 70 56 54 61 62 6c 65 3b 0a 20 20 70 2d 3e 70  >pVTable;.  p->p
2dd5b 56 54 61 62 6c 65 20 3d 20 30 3b 0a 0a 20 20 2f  VTable = 0;..  /
2dd5c 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68  * Assert that th
2dd5d 65 20 6d 75 74 65 78 20 28 69 66 20 61 6e 79 29  e mutex (if any)
2dd5e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2dd5f 20 74 68 65 20 42 74 53 68 61 72 65 64 20 64 61   the BtShared da
2dd60 74 61 62 61 73 65 20 0a 20 20 2a 2a 20 74 68 61  tabase .  ** tha
2dd61 74 20 63 6f 6e 74 61 69 6e 73 20 74 61 62 6c 65  t contains table
2dd62 20 70 20 69 73 20 68 65 6c 64 20 62 79 20 74 68   p is held by th
2dd63 65 20 63 61 6c 6c 65 72 2e 20 53 65 65 20 68 65  e caller. See he
2dd64 61 64 65 72 20 63 6f 6d 6d 65 6e 74 73 20 0a 20  ader comments . 
2dd65 20 2a 2a 20 61 62 6f 76 65 20 66 75 6e 63 74 69   ** above functi
2dd66 6f 6e 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e  on sqlite3VtabUn
2dd67 6c 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72 20 61  lockList() for a
2dd68 6e 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66  n explanation of
2dd69 20 77 68 79 0a 20 20 2a 2a 20 74 68 69 73 20 6d   why.  ** this m
2dd6a 61 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f 20  akes it safe to 
2dd6b 61 63 63 65 73 73 20 74 68 65 20 73 71 6c 69 74  access the sqlit
2dd6c 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c  e3.pDisconnect l
2dd6d 69 73 74 20 6f 66 20 61 6e 79 0a 20 20 2a 2a 20  ist of any.  ** 
2dd6e 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2dd6f 69 6f 6e 20 74 68 61 74 20 6d 61 79 20 68 61 76  ion that may hav
2dd70 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68  e an entry in th
2dd71 65 20 70 2d 3e 70 56 54 61 62 6c 65 20 6c 69 73  e p->pVTable lis
2dd72 74 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  t.  */.  assert(
2dd73 20 64 62 3d 3d 30 20 7c 7c 0a 20 20 20 20 73 71   db==0 ||.    sq
2dd74 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
2dd75 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 73 71 6c  utex(db->aDb[sql
2dd76 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2dd77 78 28 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61  x(db, p->pSchema
2dd78 29 5d 2e 70 42 74 29 20 0a 20 20 29 3b 0a 0a 20  )].pBt) .  );.. 
2dd79 20 77 68 69 6c 65 28 20 70 56 54 61 62 6c 65 20   while( pVTable 
2dd7a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
2dd7b 64 62 32 20 3d 20 70 56 54 61 62 6c 65 2d 3e 64  db2 = pVTable->d
2dd7c 62 3b 0a 20 20 20 20 56 54 61 62 6c 65 20 2a 70  b;.    VTable *p
2dd7d 4e 65 78 74 20 3d 20 70 56 54 61 62 6c 65 2d 3e  Next = pVTable->
2dd7e 70 4e 65 78 74 3b 0a 20 20 20 20 61 73 73 65 72  pNext;.    asser
2dd7f 74 28 20 64 62 32 20 29 3b 0a 20 20 20 20 69 66  t( db2 );.    if
2dd80 28 20 64 62 32 3d 3d 64 62 20 29 7b 0a 20 20 20  ( db2==db ){.   
2dd81 20 20 20 70 52 65 74 20 3d 20 70 56 54 61 62 6c     pRet = pVTabl
2dd82 65 3b 0a 20 20 20 20 20 20 70 2d 3e 70 56 54 61  e;.      p->pVTa
2dd83 62 6c 65 20 3d 20 70 52 65 74 3b 0a 20 20 20 20  ble = pRet;.    
2dd84 20 20 70 52 65 74 2d 3e 70 4e 65 78 74 20 3d 20    pRet->pNext = 
2dd85 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
2dd86 20 20 20 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65      pVTable->pNe
2dd87 78 74 20 3d 20 64 62 32 2d 3e 70 44 69 73 63 6f  xt = db2->pDisco
2dd88 6e 6e 65 63 74 3b 0a 20 20 20 20 20 20 64 62 32  nnect;.      db2
2dd89 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74 20 3d 20  ->pDisconnect = 
2dd8a 70 56 54 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20  pVTable;.    }. 
2dd8b 20 20 20 70 56 54 61 62 6c 65 20 3d 20 70 4e 65     pVTable = pNe
2dd8c 78 74 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  xt;.  }..  asser
2dd8d 74 28 20 21 64 62 20 7c 7c 20 70 52 65 74 20 29  t( !db || pRet )
2dd8e 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  ;.  return pRet;
2dd8f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 63 6f  .}.../*.** Disco
2dd90 6e 6e 65 63 74 20 61 6c 6c 20 74 68 65 20 76 69  nnect all the vi
2dd91 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65  rtual table obje
2dd92 63 74 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74  cts in the sqlit
2dd93 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c  e3.pDisconnect l
2dd94 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ist..**.** This 
2dd95 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c  function may onl
2dd96 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  y be called when
2dd97 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 73 73   the mutexes ass
2dd98 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c  ociated with all
2dd99 0a 2a 2a 20 73 68 61 72 65 64 20 62 2d 74 72 65  .** shared b-tre
2dd9a 65 20 64 61 74 61 62 61 73 65 73 20 6f 70 65 6e  e databases open
2dd9b 65 64 20 75 73 69 6e 67 20 63 6f 6e 6e 65 63 74  ed using connect
2dd9c 69 6f 6e 20 64 62 20 61 72 65 20 68 65 6c 64 20  ion db are held 
2dd9d 62 79 20 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65  by the .** calle
2dd9e 72 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20  r. This is done 
2dd9f 74 6f 20 70 72 6f 74 65 63 74 20 74 68 65 20 73  to protect the s
2dda0 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65  qlite3.pDisconne
2dda1 63 74 20 6c 69 73 74 2e 20 54 68 65 0a 2a 2a 20  ct list. The.** 
2dda2 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e  sqlite3.pDisconn
2dda3 65 63 74 20 6c 69 73 74 20 69 73 20 61 63 63 65  ect list is acce
2dda4 73 73 65 64 20 6f 6e 6c 79 20 61 73 20 66 6f 6c  ssed only as fol
2dda5 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29  lows:.**.**   1)
2dda6 20 42 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   By this functio
2dda7 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  n. In this case,
2dda8 20 61 6c 6c 20 42 74 53 68 61 72 65 64 20 6d 75   all BtShared mu
2dda9 74 65 78 65 73 20 61 6e 64 20 74 68 65 20 6d 75  texes and the mu
2ddaa 74 65 78 0a 2a 2a 20 20 20 20 20 20 61 73 73 6f  tex.**      asso
2ddab 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
2ddac 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
2ddad 69 74 73 65 6c 66 20 6d 75 73 74 20 62 65 20 68  itself must be h
2ddae 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20  eld..**.**   2) 
2ddaf 42 79 20 66 75 6e 63 74 69 6f 6e 20 76 74 61 62  By function vtab
2ddb0 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28 29 2c  DisconnectAll(),
2ddb1 20 77 68 65 6e 20 69 74 20 61 64 64 73 20 61 20   when it adds a 
2ddb2 56 54 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 0a  VTable entry to.
2ddb3 2a 2a 20 20 20 20 20 20 74 68 65 20 73 71 6c 69  **      the sqli
2ddb4 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20  te3.pDisconnect 
2ddb5 6c 69 73 74 2e 20 49 6e 20 74 68 69 73 20 63 61  list. In this ca
2ddb6 73 65 20 65 69 74 68 65 72 20 74 68 65 20 42 74  se either the Bt
2ddb7 53 68 61 72 65 64 20 6d 75 74 65 78 0a 2a 2a 20  Shared mutex.** 
2ddb8 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20       associated 
2ddb9 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
2ddba 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  e the virtual ta
2ddbb 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ble is stored in
2ddbc 20 69 73 20 68 65 6c 64 0a 2a 2a 20 20 20 20 20   is held.**     
2ddbd 20 6f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74   or, if the virt
2ddbe 75 61 6c 20 74 61 62 6c 65 20 69 73 20 73 74 6f  ual table is sto
2ddbf 72 65 64 20 69 6e 20 61 20 6e 6f 6e 2d 73 68 61  red in a non-sha
2ddc0 72 61 62 6c 65 20 64 61 74 61 62 61 73 65 2c 20  rable database, 
2ddc1 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 74 68 65  then.**      the
2ddc2 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
2ddc3 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e 0a   mutex is held..
2ddc4 2a 2a 0a 2a 2a 20 41 73 20 61 20 72 65 73 75 6c  **.** As a resul
2ddc5 74 2c 20 61 20 73 71 6c 69 74 65 33 2e 70 44 69  t, a sqlite3.pDi
2ddc6 73 63 6f 6e 6e 65 63 74 20 63 61 6e 6e 6f 74 20  sconnect cannot 
2ddc7 62 65 20 61 63 63 65 73 73 65 64 20 73 69 6d 75  be accessed simu
2ddc8 6c 74 61 6e 65 6f 75 73 6c 79 20 0a 2a 2a 20 62  ltaneously .** b
2ddc9 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61  y multiple threa
2ddca 64 73 2e 20 49 74 20 69 73 20 74 68 72 65 61 64  ds. It is thread
2ddcb 2d 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  -safe..*/.SQLITE
2ddcc 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2ddcd 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c  lite3VtabUnlockL
2ddce 69 73 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ist(sqlite3 *db)
2ddcf 7b 0a 20 20 56 54 61 62 6c 65 20 2a 70 20 3d 20  {.  VTable *p = 
2ddd0 64 62 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74 3b  db->pDisconnect;
2ddd1 0a 20 20 64 62 2d 3e 70 44 69 73 63 6f 6e 6e 65  .  db->pDisconne
2ddd2 63 74 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  ct = 0;..  asser
2ddd3 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
2ddd4 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64  oldsAllMutexes(d
2ddd5 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  b) );.  assert( 
2ddd6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
2ddd7 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
2ddd8 0a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ..  if( p ){.   
2ddd9 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
2ddda 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
2dddb 28 64 62 29 3b 0a 20 20 20 20 64 6f 20 7b 0a 20  (db);.    do {. 
2dddc 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70 4e 65       VTable *pNe
2dddd 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20  xt = p->pNext;. 
2ddde 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
2dddf 55 6e 6c 6f 63 6b 28 70 29 3b 0a 20 20 20 20 20  Unlock(p);.     
2dde0 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20   p = pNext;.    
2dde1 7d 77 68 69 6c 65 28 20 70 20 29 3b 0a 20 20 7d  }while( p );.  }
2dde2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
2dde3 61 6e 79 20 61 6e 64 20 61 6c 6c 20 76 69 72 74  any and all virt
2dde4 75 61 6c 2d 74 61 62 6c 65 20 69 6e 66 6f 72 6d  ual-table inform
2dde5 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 54  ation from the T
2dde6 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2a 20  able record..** 
2dde7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2dde8 63 61 6c 6c 65 64 2c 20 66 6f 72 20 65 78 61 6d  called, for exam
2dde9 70 6c 65 2c 20 6a 75 73 74 20 62 65 66 6f 72 65  ple, just before
2ddea 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54 61   deleting the Ta
2ddeb 62 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a  ble.** record..*
2ddec 2a 0a 2a 2a 20 53 69 6e 63 65 20 69 74 20 69 73  *.** Since it is
2dded 20 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65   a virtual-table
2ddee 2c 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  , the Table stru
2ddef 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
2ddf0 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74   pointer.** to t
2ddf1 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c 69 6e  he head of a lin
2ddf2 6b 65 64 20 6c 69 73 74 20 6f 66 20 56 54 61 62  ked list of VTab
2ddf3 6c 65 20 73 74 72 75 63 74 75 72 65 73 2e 20 45  le structures. E
2ddf4 61 63 68 20 56 54 61 62 6c 65 20 0a 2a 2a 20 73  ach VTable .** s
2ddf5 74 72 75 63 74 75 72 65 20 69 73 20 61 73 73 6f  tructure is asso
2ddf6 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69  ciated with a si
2ddf7 6e 67 6c 65 20 73 71 6c 69 74 65 33 2a 20 75 73  ngle sqlite3* us
2ddf8 65 72 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  er of the schema
2ddf9 2e 0a 2a 2a 20 54 68 65 20 72 65 66 65 72 65 6e  ..** The referen
2ddfa 63 65 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20  ce count of the 
2ddfb 56 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65  VTable structure
2ddfc 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2ddfd 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 63 6f   database .** co
2ddfe 6e 6e 65 63 74 69 6f 6e 20 64 62 20 69 73 20 64  nnection db is d
2ddff 65 63 72 65 6d 65 6e 74 65 64 20 69 6d 6d 65 64  ecremented immed
2de00 69 61 74 65 6c 79 20 28 77 68 69 63 68 20 6d 61  iately (which ma
2de01 79 20 6c 65 61 64 20 74 6f 20 74 68 65 20 0a 2a  y lead to the .*
2de02 2a 20 73 74 72 75 63 74 75 72 65 20 62 65 69 6e  * structure bein
2de03 67 20 78 44 69 73 63 6f 6e 6e 65 63 74 65 64 20  g xDisconnected 
2de04 61 6e 64 20 66 72 65 65 29 2e 20 41 6e 79 20 6f  and free). Any o
2de05 74 68 65 72 20 56 54 61 62 6c 65 20 73 74 72 75  ther VTable stru
2de06 63 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65  ctures.** in the
2de07 20 6c 69 73 74 20 61 72 65 20 6d 6f 76 65 64 20   list are moved 
2de08 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70  to the sqlite3.p
2de09 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20  Disconnect list 
2de0a 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  of the associate
2de0b 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  d .** database c
2de0c 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  onnection..*/.SQ
2de0d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2de0e 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65  d sqlite3VtabCle
2de0f 61 72 28 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20  ar(Table *p){.  
2de10 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 41 6c  vtabDisconnectAl
2de11 6c 28 30 2c 20 70 29 3b 0a 20 20 69 66 28 20 70  l(0, p);.  if( p
2de12 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 29 7b  ->azModuleArg ){
2de13 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
2de14 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d  for(i=0; i<p->nM
2de15 6f 64 75 6c 65 41 72 67 3b 20 69 2b 2b 29 7b 0a  oduleArg; i++){.
2de16 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2de17 72 65 65 28 70 2d 3e 64 62 4d 65 6d 2c 20 70 2d  ree(p->dbMem, p-
2de18 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29  >azModuleArg[i])
2de19 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2de1a 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 4d  te3DbFree(p->dbM
2de1b 65 6d 2c 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41  em, p->azModuleA
2de1c 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  rg);.  }.}../*.*
2de1d 2a 20 41 64 64 20 61 20 6e 65 77 20 6d 6f 64 75  * Add a new modu
2de1e 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70  le argument to p
2de1f 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41  Table->azModuleA
2de20 72 67 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72  rg[]..** The str
2de21 69 6e 67 20 69 73 20 6e 6f 74 20 63 6f 70 69 65  ing is not copie
2de22 64 20 2d 20 74 68 65 20 70 6f 69 6e 74 65 72 20  d - the pointer 
2de23 69 73 20 73 74 6f 72 65 64 2e 20 20 54 68 65 0a  is stored.  The.
2de24 2a 2a 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62  ** string will b
2de25 65 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74 69  e freed automati
2de26 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 74  cally when the t
2de27 61 62 6c 65 20 69 73 0a 2a 2a 20 64 65 6c 65 74  able is.** delet
2de28 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
2de29 69 64 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75  id addModuleArgu
2de2a 6d 65 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62  ment(sqlite3 *db
2de2b 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 2c  , Table *pTable,
2de2c 20 63 68 61 72 20 2a 7a 41 72 67 29 7b 0a 20 20   char *zArg){.  
2de2d 69 6e 74 20 69 20 3d 20 70 54 61 62 6c 65 2d 3e  int i = pTable->
2de2e 6e 4d 6f 64 75 6c 65 41 72 67 2b 2b 3b 0a 20 20  nModuleArg++;.  
2de2f 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a  int nBytes = siz
2de30 65 6f 66 28 63 68 61 72 20 2a 29 2a 28 31 2b 70  eof(char *)*(1+p
2de31 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72  Table->nModuleAr
2de32 67 29 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d  g);.  char **azM
2de33 6f 64 75 6c 65 41 72 67 3b 0a 20 20 61 7a 4d 6f  oduleArg;.  azMo
2de34 64 75 6c 65 41 72 67 20 3d 20 73 71 6c 69 74 65  duleArg = sqlite
2de35 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70  3DbRealloc(db, p
2de36 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41  Table->azModuleA
2de37 72 67 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69  rg, nBytes);.  i
2de38 66 28 20 61 7a 4d 6f 64 75 6c 65 41 72 67 3d 3d  f( azModuleArg==
2de39 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a  0 ){.    int j;.
2de3a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69      for(j=0; j<i
2de3b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; j++){.      sq
2de3c 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2de3d 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
2de3e 41 72 67 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20  Arg[j]);.    }. 
2de3f 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2de40 28 64 62 2c 20 7a 41 72 67 29 3b 0a 20 20 20 20  (db, zArg);.    
2de41 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2de42 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75  , pTable->azModu
2de43 6c 65 41 72 67 29 3b 0a 20 20 20 20 70 54 61 62  leArg);.    pTab
2de44 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d  le->nModuleArg =
2de45 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
2de46 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 20   azModuleArg[i] 
2de47 3d 20 7a 41 72 67 3b 0a 20 20 20 20 61 7a 4d 6f  = zArg;.    azMo
2de48 64 75 6c 65 41 72 67 5b 69 2b 31 5d 20 3d 20 30  duleArg[i+1] = 0
2de49 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e  ;.  }.  pTable->
2de4a 61 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20 61 7a  azModuleArg = az
2de4b 4d 6f 64 75 6c 65 41 72 67 3b 0a 7d 0a 0a 2f 2a  ModuleArg;.}../*
2de4c 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63  .** The parser c
2de4d 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2de4e 65 20 77 68 65 6e 20 69 74 20 66 69 72 73 74 20  e when it first 
2de4f 73 65 65 73 20 61 20 43 52 45 41 54 45 20 56 49  sees a CREATE VI
2de50 52 54 55 41 4c 20 54 41 42 4c 45 0a 2a 2a 20 73  RTUAL TABLE.** s
2de51 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 6d  tatement.  The m
2de52 6f 64 75 6c 65 20 6e 61 6d 65 20 68 61 73 20 62  odule name has b
2de53 65 65 6e 20 70 61 72 73 65 64 2c 20 62 75 74 20  een parsed, but 
2de54 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 6c 69 73  the optional lis
2de55 74 0a 2a 2a 20 6f 66 20 70 61 72 61 6d 65 74 65  t.** of paramete
2de56 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74  rs that follow t
2de57 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 61  he module name a
2de58 72 65 20 73 74 69 6c 6c 20 70 65 6e 64 69 6e 67  re still pending
2de59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2de5a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2de5b 56 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 0a  VtabBeginParse(.
2de5c 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2de5d 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
2de5e 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2de5f 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20  Token *pName1,  
2de60 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
2de61 20 6e 65 77 20 74 61 62 6c 65 2c 20 6f 72 20 64   new table, or d
2de62 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a  atabase name */.
2de63 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c    Token *pName2,
2de64 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2de65 6f 66 20 6e 65 77 20 74 61 62 6c 65 20 6f 72 20  of new table or 
2de66 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  NULL */.  Token 
2de67 2a 70 4d 6f 64 75 6c 65 4e 61 6d 65 20 20 20 20  *pModuleName    
2de68 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d  /* Name of the m
2de69 6f 64 75 6c 65 20 66 6f 72 20 74 68 65 20 76 69  odule for the vi
2de6a 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 29  rtual table */.)
2de6b 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  {.  int iDb;    
2de6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2de6d 20 64 61 74 61 62 61 73 65 20 74 68 65 20 74 61   database the ta
2de6e 62 6c 65 20 69 73 20 62 65 69 6e 67 20 63 72 65  ble is being cre
2de6f 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 54 61 62  ated in */.  Tab
2de70 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 20 20  le *pTable;     
2de71 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 76 69     /* The new vi
2de72 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
2de73 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
2de74 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2de75 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2de76 0a 0a 20 20 73 71 6c 69 74 65 33 53 74 61 72 74  ..  sqlite3Start
2de77 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 4e  Table(pParse, pN
2de78 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 30 2c  ame1, pName2, 0,
2de79 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 70 54 61   0, 1, 0);.  pTa
2de7a 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  ble = pParse->pN
2de7b 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
2de7c 54 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72  Table==0 ) retur
2de7d 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d  n;.  assert( 0==
2de7e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 20 29  pTable->pIndex )
2de7f 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  ;..  db = pParse
2de80 2d 3e 64 62 3b 0a 20 20 69 44 62 20 3d 20 73 71  ->db;.  iDb = sq
2de81 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
2de82 65 78 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70  ex(db, pTable->p
2de83 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72  Schema);.  asser
2de84 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 20 20  t( iDb>=0 );..  
2de85 70 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73  pTable->tabFlags
2de86 20 7c 3d 20 54 46 5f 56 69 72 74 75 61 6c 3b 0a   |= TF_Virtual;.
2de87 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c    pTable->nModul
2de88 65 41 72 67 20 3d 20 30 3b 0a 20 20 61 64 64 4d  eArg = 0;.  addM
2de89 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62  oduleArgument(db
2de8a 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74 65  , pTable, sqlite
2de8b 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
2de8c 62 2c 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 29 29  b, pModuleName))
2de8d 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67  ;.  addModuleArg
2de8e 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c 65  ument(db, pTable
2de8f 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  , sqlite3DbStrDu
2de90 70 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44  p(db, db->aDb[iD
2de91 62 5d 2e 7a 4e 61 6d 65 29 29 3b 0a 20 20 61 64  b].zName));.  ad
2de92 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28  dModuleArgument(
2de93 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69  db, pTable, sqli
2de94 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2de95 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 29 3b  pTable->zName));
2de96 0a 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65  .  pParse->sName
2de97 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e 74 29 28  Token.n = (int)(
2de98 26 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 7a 5b  &pModuleName->z[
2de99 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 6e 5d 20  pModuleName->n] 
2de9a 2d 20 70 4e 61 6d 65 31 2d 3e 7a 29 3b 0a 0a 23  - pName1->z);..#
2de9b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2de9c 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
2de9d 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61  .  /* Creating a
2de9e 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
2de9f 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74 68 6f  nvokes the autho
2dea0 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
2dea1 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20 54 68  k twice..  ** Th
2dea2 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69  e first invocati
2dea3 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70 65  on, to obtain pe
2dea4 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e 53 45  rmission to INSE
2dea5 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20 74 68  RT a row into th
2dea6 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61  e.  ** sqlite_ma
2dea7 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61 73 20  ster table, has 
2dea8 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d 61 64  already been mad
2dea9 65 20 62 79 20 73 71 6c 69 74 65 33 53 74 61 72  e by sqlite3Star
2deaa 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a 20 54  tTable()..  ** T
2deab 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 2c 20  he second call, 
2deac 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d 69 73  to obtain permis
2dead 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 74  sion to create t
2deae 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d 61 64  he table, is mad
2deaf 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  e now..  */.  if
2deb0 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75  ( pTable->azModu
2deb1 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73 71 6c  leArg ){.    sql
2deb2 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2deb3 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43 52 45  arse, SQLITE_CRE
2deb4 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54 61 62  ATE_VTABLE, pTab
2deb5 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20  le->zName, .    
2deb6 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e          pTable->
2deb7 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 2c 20  azModuleArg[0], 
2deb8 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b  pParse->db->aDb[
2deb9 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d  iDb].zName);.  }
2deba 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
2debb 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 61   This routine ta
2debc 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65 20 61  kes the module a
2debd 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68 61 73  rgument that has
2debe 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61 74 69   been accumulati
2debf 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d  ng.** in pParse-
2dec0 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70 70 65  >zArg[] and appe
2dec1 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20 6c 69  nds it to the li
2dec2 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  st of arguments 
2dec3 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61  on the.** virtua
2dec4 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c  l table currentl
2dec5 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
2dec6 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65 2d 3e  tion in pParse->
2dec7 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  pTable..*/.stati
2dec8 63 20 76 6f 69 64 20 61 64 64 41 72 67 75 6d 65  c void addArgume
2dec9 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65 20 2a  ntToVtab(Parse *
2deca 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28 20 70  pParse){.  if( p
2decb 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 26 26  Parse->sArg.z &&
2decc 20 41 4c 57 41 59 53 28 70 50 61 72 73 65 2d 3e   ALWAYS(pParse->
2decd 70 4e 65 77 54 61 62 6c 65 29 20 29 7b 0a 20 20  pNewTable) ){.  
2dece 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
2decf 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  = (const char*)p
2ded0 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 3b 0a 20  Parse->sArg.z;. 
2ded1 20 20 20 69 6e 74 20 6e 20 3d 20 70 50 61 72 73     int n = pPars
2ded2 65 2d 3e 73 41 72 67 2e 6e 3b 0a 20 20 20 20 73  e->sArg.n;.    s
2ded3 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2ded4 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 61 64 64  rse->db;.    add
2ded5 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64  ModuleArgument(d
2ded6 62 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  b, pParse->pNewT
2ded7 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53  able, sqlite3DbS
2ded8 74 72 4e 44 75 70 28 64 62 2c 20 7a 2c 20 6e 29  trNDup(db, z, n)
2ded9 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2deda 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
2dedb 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 66   this routine af
2dedc 74 65 72 20 74 68 65 20 43 52 45 41 54 45 20 56  ter the CREATE V
2dedd 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
2dede 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65  tement.** has be
2dedf 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 20 70 61  en completely pa
2dee0 72 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rsed..*/.SQLITE_
2dee1 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2dee2 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61  ite3VtabFinishPa
2dee3 72 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  rse(Parse *pPars
2dee4 65 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b  e, Token *pEnd){
2dee5 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
2dee6 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2dee7 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 61 62 6c  le;  /* The tabl
2dee8 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63  e being construc
2dee9 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ted */.  sqlite3
2deea 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2deeb 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b;         /* Th
2deec 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2deed 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20  ction */..  if( 
2deee 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e  pTab==0 ) return
2deef 3b 0a 20 20 61 64 64 41 72 67 75 6d 65 6e 74 54  ;.  addArgumentT
2def0 6f 56 74 61 62 28 70 50 61 72 73 65 29 3b 0a 20  oVtab(pParse);. 
2def1 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
2def2 3d 20 30 3b 0a 20 20 69 66 28 20 70 54 61 62 2d  = 0;.  if( pTab-
2def3 3e 6e 4d 6f 64 75 6c 65 41 72 67 3c 31 20 29 20  >nModuleArg<1 ) 
2def4 72 65 74 75 72 6e 3b 0a 20 20 0a 20 20 2f 2a 20  return;.  .  /* 
2def5 49 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49  If the CREATE VI
2def6 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
2def7 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 65  ement is being e
2def8 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 0a 20  ntered for the. 
2def9 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 28   ** first time (
2defa 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69  in other words i
2defb 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2defc 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20  ble is actually 
2defd 62 65 69 6e 67 0a 20 20 2a 2a 20 63 72 65 61 74  being.  ** creat
2defe 65 64 20 6e 6f 77 20 69 6e 73 74 65 61 64 20 6f  ed now instead o
2deff 66 20 6a 75 73 74 20 62 65 69 6e 67 20 72 65 61  f just being rea
2df00 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 5f  d out of sqlite_
2df01 6d 61 73 74 65 72 29 20 74 68 65 6e 0a 20 20 2a  master) then.  *
2df02 2a 20 64 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20  * do additional 
2df03 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 77  initialization w
2df04 6f 72 6b 20 61 6e 64 20 73 74 6f 72 65 20 74 68  ork and store th
2df05 65 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74  e statement text
2df06 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c  .  ** in the sql
2df07 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2df08 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  ..  */.  if( !db
2df09 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2df0a 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a     char *zStmt;.
2df0b 20 20 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65      char *zWhere
2df0c 3b 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20  ;.    int iDb;. 
2df0d 20 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 20     Vdbe *v;..   
2df0e 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
2df0f 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
2df10 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54   the CREATE VIRT
2df11 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d  UAL TABLE statem
2df12 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ent */.    if( p
2df13 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 50 61  End ){.      pPa
2df14 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
2df15 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e  n = (int)(pEnd->
2df16 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  z - pParse->sNam
2df17 65 54 6f 6b 65 6e 2e 7a 29 20 2b 20 70 45 6e 64  eToken.z) + pEnd
2df18 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  ->n;.    }.    z
2df19 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50  Stmt = sqlite3MP
2df1a 72 69 6e 74 66 28 64 62 2c 20 22 43 52 45 41 54  rintf(db, "CREAT
2df1b 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  E VIRTUAL TABLE 
2df1c 25 54 22 2c 20 26 70 50 61 72 73 65 2d 3e 73 4e  %T", &pParse->sN
2df1d 61 6d 65 54 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20  ameToken);..    
2df1e 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 68  /* A slot for th
2df1f 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c 72  e record has alr
2df20 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
2df21 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ted in the .    
2df22 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  ** SQLITE_MASTER
2df23 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73 74   table.  We just
2df24 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65 20   need to update 
2df25 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20 61  that slot with a
2df26 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e  ll.    ** the in
2df27 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76 65 20  formation we've 
2df28 63 6f 6c 6c 65 63 74 65 64 2e 20 20 0a 20 20 20  collected.  .   
2df29 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 56   **.    ** The V
2df2a 4d 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  M register numbe
2df2b 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77  r pParse->regRow
2df2c 69 64 20 68 6f 6c 64 73 20 74 68 65 20 72 6f 77  id holds the row
2df2d 69 64 20 6f 66 20 61 6e 0a 20 20 20 20 2a 2a 20  id of an.    ** 
2df2e 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73 71 6c  entry in the sql
2df2f 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2df30 20 74 68 74 20 77 61 73 20 63 72 65 61 74 65 64   tht was created
2df31 20 66 6f 72 20 74 68 69 73 20 76 74 61 62 0a 20   for this vtab. 
2df32 20 20 20 2a 2a 20 62 79 20 73 71 6c 69 74 65 33     ** by sqlite3
2df33 53 74 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20  StartTable()..  
2df34 20 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73    */.    iDb = s
2df35 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2df36 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
2df37 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69  chema);.    sqli
2df38 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
2df39 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50  Parse,.      "UP
2df3a 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20 20 20  DATE %Q.%s ".   
2df3b 20 20 20 20 20 20 22 53 45 54 20 74 79 70 65 3d        "SET type=
2df3c 27 74 61 62 6c 65 27 2c 20 6e 61 6d 65 3d 25 51  'table', name=%Q
2df3d 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72  , tbl_name=%Q, r
2df3e 6f 6f 74 70 61 67 65 3d 30 2c 20 73 71 6c 3d 25  ootpage=0, sql=%
2df3f 51 20 22 0a 20 20 20 20 20 20 20 22 57 48 45 52  Q ".       "WHER
2df40 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20  E rowid=#%d",.  
2df41 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d      db->aDb[iDb]
2df42 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54  .zName, SCHEMA_T
2df43 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20  ABLE(iDb),.     
2df44 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20   pTab->zName,.  
2df45 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
2df46 0a 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20  .      zStmt,.  
2df47 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52      pParse->regR
2df48 6f 77 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20  owid.    );.    
2df49 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2df4a 2c 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 76 20  , zStmt);.    v 
2df4b 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2df4c 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71  (pParse);.    sq
2df4d 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
2df4e 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
2df4f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2df50 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70  AddOp2(v, OP_Exp
2df51 69 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ire, 0, 0);.    
2df52 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  zWhere = sqlite3
2df53 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d  MPrintf(db, "nam
2df54 65 3d 27 25 71 27 22 2c 20 70 54 61 62 2d 3e 7a  e='%q'", pTab->z
2df55 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Name);.    sqlit
2df56 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2df57 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20  OP_ParseSchema, 
2df58 69 44 62 2c 20 31 2c 20 30 2c 20 7a 57 68 65 72  iDb, 1, 0, zWher
2df59 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a  e, P4_DYNAMIC);.
2df5a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2df5b 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 43 72 65  ddOp4(v, OP_VCre
2df5c 61 74 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  ate, iDb, 0, 0, 
2df5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2df5e 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e            pTab->
2df5f 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74  zName, sqlite3St
2df60 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e 61  rlen30(pTab->zNa
2df61 6d 65 29 20 2b 20 31 29 3b 0a 20 20 7d 0a 0a 20  me) + 1);.  }.. 
2df62 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 72 65   /* If we are re
2df63 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69  reading the sqli
2df64 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
2df65 63 72 65 61 74 65 20 74 68 65 20 69 6e 2d 6d 65  create the in-me
2df66 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 63 6f 72 64  mory.  ** record
2df67 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 54   of the table. T
2df68 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65  he xConnect() me
2df69 74 68 6f 64 20 69 73 20 6e 6f 74 20 63 61 6c 6c  thod is not call
2df6a 65 64 20 75 6e 74 69 6c 0a 20 20 2a 2a 20 74 68  ed until.  ** th
2df6b 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65  e first time the
2df6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
2df6d 73 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c  s used in an SQL
2df6e 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73   statement. This
2df6f 0a 20 20 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 73  .  ** allows a s
2df70 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e 74 61  chema that conta
2df71 69 6e 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ins virtual tabl
2df72 65 73 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 20  es to be loaded 
2df73 62 65 66 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20  before.  ** the 
2df74 72 65 71 75 69 72 65 64 20 76 69 72 74 75 61 6c  required virtual
2df75 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
2df76 61 74 69 6f 6e 73 20 61 72 65 20 72 65 67 69 73  ations are regis
2df77 74 65 72 65 64 2e 20 20 2a 2f 0a 20 20 65 6c 73  tered.  */.  els
2df78 65 20 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  e {.    Table *p
2df79 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d 61 20  Old;.    Schema 
2df7a 2a 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d  *pSchema = pTab-
2df7b 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 63 6f  >pSchema;.    co
2df7c 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
2df7d 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20  = pTab->zName;. 
2df7e 20 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73     int nName = s
2df7f 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2df80 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4f 6c 64 20  Name);.    pOld 
2df81 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
2df82 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62  ert(&pSchema->tb
2df83 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  lHash, zName, nN
2df84 61 6d 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  ame, pTab);.    
2df85 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20  if( pOld ){.    
2df86 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2df87 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 61 73  ed = 1;.      as
2df88 73 65 72 74 28 20 70 54 61 62 3d 3d 70 4f 6c 64  sert( pTab==pOld
2df89 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d   );  /* Malloc m
2df8a 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20  ust have failed 
2df8b 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72  inside HashInser
2df8c 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 72 65 74  t() */.      ret
2df8d 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  urn;.    }.    p
2df8e 53 63 68 65 6d 61 2d 3e 64 62 20 3d 20 70 50 61  Schema->db = pPa
2df8f 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 50 61  rse->db;.    pPa
2df90 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d  rse->pNewTable =
2df91 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
2df92 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
2df93 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  s this routine w
2df94 68 65 6e 20 69 74 20 73 65 65 73 20 74 68 65 20  hen it sees the 
2df95 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 6f  first token.** o
2df96 66 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f  f an argument to
2df97 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   the module name
2df98 20 69 6e 20 61 20 43 52 45 41 54 45 20 56 49 52   in a CREATE VIR
2df99 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65  TUAL TABLE state
2df9a 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
2df9b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2df9c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28  ite3VtabArgInit(
2df9d 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
2df9e 20 20 61 64 64 41 72 67 75 6d 65 6e 74 54 6f 56    addArgumentToV
2df9f 74 61 62 28 70 50 61 72 73 65 29 3b 0a 20 20 70  tab(pParse);.  p
2dfa0 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20  Parse->sArg.z = 
2dfa1 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72  0;.  pParse->sAr
2dfa2 67 2e 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  g.n = 0;.}../*.*
2dfa3 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
2dfa4 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2dfa5 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 61  for each token a
2dfa6 66 74 65 72 20 74 68 65 20 66 69 72 73 74 20 74  fter the first t
2dfa7 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 61 6e 20 61 72  oken.** in an ar
2dfa8 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f  gument to the mo
2dfa9 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43  dule name in a C
2dfaa 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
2dfab 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  BLE statement..*
2dfac 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2dfad 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
2dfae 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 65  bArgExtend(Parse
2dfaf 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
2dfb0 2a 70 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 41  *p){.  Token *pA
2dfb1 72 67 20 3d 20 26 70 50 61 72 73 65 2d 3e 73 41  rg = &pParse->sA
2dfb2 72 67 3b 0a 20 20 69 66 28 20 70 41 72 67 2d 3e  rg;.  if( pArg->
2dfb3 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 41 72 67  z==0 ){.    pArg
2dfb4 2d 3e 7a 20 3d 20 70 2d 3e 7a 3b 0a 20 20 20 20  ->z = p->z;.    
2dfb5 70 41 72 67 2d 3e 6e 20 3d 20 70 2d 3e 6e 3b 0a  pArg->n = p->n;.
2dfb6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
2dfb7 65 72 74 28 70 41 72 67 2d 3e 7a 20 3c 20 70 2d  ert(pArg->z < p-
2dfb8 3e 7a 29 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e  >z);.    pArg->n
2dfb9 20 3d 20 28 69 6e 74 29 28 26 70 2d 3e 7a 5b 70   = (int)(&p->z[p
2dfba 2d 3e 6e 5d 20 2d 20 70 41 72 67 2d 3e 7a 29 3b  ->n] - pArg->z);
2dfbb 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  .  }.}../*.** In
2dfbc 76 6f 6b 65 20 61 20 76 69 72 74 75 61 6c 20 74  voke a virtual t
2dfbd 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72  able constructor
2dfbe 20 28 65 69 74 68 65 72 20 78 43 72 65 61 74 65   (either xCreate
2dfbf 20 6f 72 20 78 43 6f 6e 6e 65 63 74 29 2e 20 54   or xConnect). T
2dfc0 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f  he.** pointer to
2dfc1 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f   the function to
2dfc2 20 69 6e 76 6f 6b 65 20 69 73 20 70 61 73 73 65   invoke is passe
2dfc3 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68 20  d as the fourth 
2dfc4 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20  parameter.** to 
2dfc5 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 0a  this procedure..
2dfc6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 74  */.static int vt
2dfc7 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f  abCallConstructo
2dfc8 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r(.  sqlite3 *db
2dfc9 2c 20 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  , .  Table *pTab
2dfca 2c 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  ,.  Module *pMod
2dfcb 2c 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 73 74  ,.  int (*xConst
2dfcc 72 75 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 76  ruct)(sqlite3*,v
2dfcd 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
2dfce 68 61 72 2a 63 6f 6e 73 74 2a 2c 73 71 6c 69 74  har*const*,sqlit
2dfcf 65 33 5f 76 74 61 62 2a 2a 2c 63 68 61 72 2a 2a  e3_vtab**,char**
2dfd0 29 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  ),.  char **pzEr
2dfd1 72 0a 29 7b 0a 20 20 56 54 61 62 6c 65 20 2a 70  r.){.  VTable *p
2dfd2 56 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 72 63  VTable;.  int rc
2dfd3 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2dfd4 63 6f 6e 73 74 2a 61 7a 41 72 67 20 3d 20 28 63  const*azArg = (c
2dfd5 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
2dfd6 2a 29 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  *)pTab->azModule
2dfd7 41 72 67 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20  Arg;.  int nArg 
2dfd8 3d 20 70 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41  = pTab->nModuleA
2dfd9 72 67 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  rg;.  char *zErr
2dfda 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4d   = 0;.  char *zM
2dfdb 6f 64 75 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69  oduleName = sqli
2dfdc 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2dfdd 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  %s", pTab->zName
2dfde 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4d 6f 64 75  );..  if( !zModu
2dfdf 6c 65 4e 61 6d 65 20 29 7b 0a 20 20 20 20 72 65  leName ){.    re
2dfe0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2dfe1 4d 3b 0a 20 20 7d 0a 0a 20 20 70 56 54 61 62 6c  M;.  }..  pVTabl
2dfe2 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  e = sqlite3DbMal
2dfe3 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2dfe4 6f 66 28 56 54 61 62 6c 65 29 29 3b 0a 20 20 69  of(VTable));.  i
2dfe5 66 28 20 21 70 56 54 61 62 6c 65 20 29 7b 0a 20  f( !pVTable ){. 
2dfe6 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2dfe7 28 64 62 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65  (db, zModuleName
2dfe8 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
2dfe9 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
2dfea 20 20 70 56 54 61 62 6c 65 2d 3e 64 62 20 3d 20    pVTable->db = 
2dfeb 64 62 3b 0a 20 20 70 56 54 61 62 6c 65 2d 3e 70  db;.  pVTable->p
2dfec 4d 6f 64 20 3d 20 70 4d 6f 64 3b 0a 0a 20 20 61  Mod = pMod;..  a
2dfed 73 73 65 72 74 28 20 21 64 62 2d 3e 70 56 54 61  ssert( !db->pVTa
2dfee 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78  b );.  assert( x
2dfef 43 6f 6e 73 74 72 75 63 74 20 29 3b 0a 20 20 64  Construct );.  d
2dff0 62 2d 3e 70 56 54 61 62 20 3d 20 70 54 61 62 3b  b->pVTab = pTab;
2dff1 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ..  /* Invoke th
2dff2 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2dff3 63 6f 6e 73 74 72 75 63 74 6f 72 20 2a 2f 0a 20  constructor */. 
2dff4 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2dff5 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 72  fetyOff(db);.  r
2dff6 63 20 3d 20 78 43 6f 6e 73 74 72 75 63 74 28 64  c = xConstruct(d
2dff7 62 2c 20 70 4d 6f 64 2d 3e 70 41 75 78 2c 20 6e  b, pMod->pAux, n
2dff8 41 72 67 2c 20 61 7a 41 72 67 2c 20 26 70 56 54  Arg, azArg, &pVT
2dff9 61 62 6c 65 2d 3e 70 56 74 61 62 2c 20 26 7a 45  able->pVtab, &zE
2dffa 72 72 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c  rr);.  (void)sql
2dffb 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
2dffc 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
2dffd 54 45 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d  TE_NOMEM ) db->m
2dffe 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2dfff 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
2e000 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 69 66 28  K!=rc ){.    if(
2e001 20 7a 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20   zErr==0 ){.    
2e002 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
2e003 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 76  e3MPrintf(db, "v
2e004 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f  table constructo
2e005 72 20 66 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a  r failed: %s", z
2e006 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20 20 20  ModuleName);.   
2e007 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2a   }else {.      *
2e008 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d  pzErr = sqlite3M
2e009 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c  Printf(db, "%s",
2e00a 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71   zErr);.      sq
2e00b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2e00c 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  zErr);.    }.   
2e00d 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2e00e 62 2c 20 70 56 54 61 62 6c 65 29 3b 0a 20 20 7d  b, pVTable);.  }
2e00f 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28  else if( ALWAYS(
2e010 70 56 54 61 62 6c 65 2d 3e 70 56 74 61 62 29 20  pVTable->pVtab) 
2e011 29 7b 0a 20 20 20 20 2f 2a 20 4a 75 73 74 69 66  ){.    /* Justif
2e012 69 63 61 74 69 6f 6e 20 6f 66 20 41 4c 57 41 59  ication of ALWAY
2e013 53 28 29 3a 20 20 41 20 63 6f 72 72 65 63 74 20  S():  A correct 
2e014 76 74 61 62 20 63 6f 6e 73 74 72 75 63 74 6f 72  vtab constructor
2e015 20 6d 75 73 74 20 61 6c 6c 6f 63 61 74 65 0a 20   must allocate. 
2e016 20 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65     ** the sqlite
2e017 33 5f 76 74 61 62 20 6f 62 6a 65 63 74 20 69 66  3_vtab object if
2e018 20 73 75 63 63 65 73 73 66 75 6c 2e 20 20 2a 2f   successful.  */
2e019 0a 20 20 20 20 70 56 54 61 62 6c 65 2d 3e 70 56  .    pVTable->pV
2e01a 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70  tab->pModule = p
2e01b 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  Mod->pModule;.  
2e01c 20 20 70 56 54 61 62 6c 65 2d 3e 6e 52 65 66 20    pVTable->nRef 
2e01d 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  = 1;.    if( db-
2e01e 3e 70 56 54 61 62 20 29 7b 0a 20 20 20 20 20 20  >pVTab ){.      
2e01f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
2e020 6d 61 74 20 3d 20 22 76 74 61 62 6c 65 20 63 6f  mat = "vtable co
2e021 6e 73 74 72 75 63 74 6f 72 20 64 69 64 20 6e 6f  nstructor did no
2e022 74 20 64 65 63 6c 61 72 65 20 73 63 68 65 6d 61  t declare schema
2e023 3a 20 25 73 22 3b 0a 20 20 20 20 20 20 2a 70 7a  : %s";.      *pz
2e024 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
2e025 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74  intf(db, zFormat
2e026 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2e027 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61        sqlite3Vta
2e028 62 55 6e 6c 6f 63 6b 28 70 56 54 61 62 6c 65 29  bUnlock(pVTable)
2e029 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
2e02a 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d  ITE_ERROR;.    }
2e02b 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
2e02c 69 43 6f 6c 3b 0a 20 20 20 20 20 20 2f 2a 20 49  iCol;.      /* I
2e02d 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 65 6e  f everything wen
2e02e 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 70  t according to p
2e02f 6c 61 6e 2c 20 6c 69 6e 6b 20 74 68 65 20 6e 65  lan, link the ne
2e030 77 20 56 54 61 62 6c 65 20 73 74 72 75 63 74 75  w VTable structu
2e031 72 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f  re.      ** into
2e032 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
2e033 20 68 65 61 64 65 64 20 62 79 20 70 54 61 62 2d   headed by pTab-
2e034 3e 70 56 54 61 62 6c 65 2e 20 54 68 65 6e 20 6c  >pVTable. Then l
2e035 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
2e036 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e  .      ** column
2e037 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  s of the table t
2e038 6f 20 73 65 65 20 69 66 20 61 6e 79 20 6f 66 20  o see if any of 
2e039 74 68 65 6d 20 63 6f 6e 74 61 69 6e 20 74 68 65  them contain the
2e03a 20 74 6f 6b 65 6e 20 22 68 69 64 64 65 6e 22 2e   token "hidden".
2e03b 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 73 6f 2c  .      ** If so,
2e03c 20 73 65 74 20 74 68 65 20 43 6f 6c 75 6d 6e 2e   set the Column.
2e03d 69 73 48 69 64 64 65 6e 20 66 6c 61 67 20 61 6e  isHidden flag an
2e03e 64 20 72 65 6d 6f 76 65 20 74 68 65 20 74 6f 6b  d remove the tok
2e03f 65 6e 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a  en from.      **
2e040 20 74 68 65 20 74 79 70 65 20 73 74 72 69 6e 67   the type string
2e041 2e 20 20 2a 2f 0a 20 20 20 20 20 20 70 56 54 61  .  */.      pVTa
2e042 62 6c 65 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61  ble->pNext = pTa
2e043 62 2d 3e 70 56 54 61 62 6c 65 3b 0a 20 20 20 20  b->pVTable;.    
2e044 20 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 20    pTab->pVTable 
2e045 3d 20 70 56 54 61 62 6c 65 3b 0a 0a 20 20 20 20  = pVTable;..    
2e046 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43    for(iCol=0; iC
2e047 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69  ol<pTab->nCol; i
2e048 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Col++){.        
2e049 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 70 54  char *zType = pT
2e04a 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
2e04b 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69 6e  Type;.        in
2e04c 74 20 6e 54 79 70 65 3b 0a 20 20 20 20 20 20 20  t nType;.       
2e04d 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 20 20   int i = 0;.    
2e04e 20 20 20 20 69 66 28 20 21 7a 54 79 70 65 20 29      if( !zType )
2e04f 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2e050 20 20 20 6e 54 79 70 65 20 3d 20 73 71 6c 69 74     nType = sqlit
2e051 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65  e3Strlen30(zType
2e052 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  );.        if( s
2e053 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 22  qlite3StrNICmp("
2e054 68 69 64 64 65 6e 22 2c 20 7a 54 79 70 65 2c 20  hidden", zType, 
2e055 36 29 7c 7c 28 7a 54 79 70 65 5b 36 5d 20 26 26  6)||(zType[6] &&
2e056 20 7a 54 79 70 65 5b 36 5d 21 3d 27 20 27 29 20   zType[6]!=' ') 
2e057 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  ){.          for
2e058 28 69 3d 30 3b 20 69 3c 6e 54 79 70 65 3b 20 69  (i=0; i<nType; i
2e059 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
2e05a 20 69 66 28 20 28 30 3d 3d 73 71 6c 69 74 65 33   if( (0==sqlite3
2e05b 53 74 72 4e 49 43 6d 70 28 22 20 68 69 64 64 65  StrNICmp(" hidde
2e05c 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d 2c 20 37  n", &zType[i], 7
2e05d 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  )).             
2e05e 26 26 20 28 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d  && (zType[i+7]==
2e05f 27 5c 30 27 20 7c 7c 20 7a 54 79 70 65 5b 69 2b  '\0' || zType[i+
2e060 37 5d 3d 3d 27 20 27 29 0a 20 20 20 20 20 20 20  7]==' ').       
2e061 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
2e062 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
2e063 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e064 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2e065 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2e066 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2e067 69 3c 6e 54 79 70 65 20 29 7b 0a 20 20 20 20 20  i<nType ){.     
2e068 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2e069 20 20 20 20 20 20 69 6e 74 20 6e 44 65 6c 20 3d        int nDel =
2e06a 20 36 20 2b 20 28 7a 54 79 70 65 5b 69 2b 36 5d   6 + (zType[i+6]
2e06b 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20   ? 1 : 0);.     
2e06c 20 20 20 20 20 66 6f 72 28 6a 3d 69 3b 20 28 6a       for(j=i; (j
2e06d 2b 6e 44 65 6c 29 3c 3d 6e 54 79 70 65 3b 20 6a  +nDel)<=nType; j
2e06e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
2e06f 20 7a 54 79 70 65 5b 6a 5d 20 3d 20 7a 54 79 70   zType[j] = zTyp
2e070 65 5b 6a 2b 6e 44 65 6c 5d 3b 0a 20 20 20 20 20  e[j+nDel];.     
2e071 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2e072 20 69 66 28 20 7a 54 79 70 65 5b 69 5d 3d 3d 27   if( zType[i]=='
2e073 5c 30 27 20 26 26 20 69 3e 30 20 29 7b 0a 20 20  \0' && i>0 ){.  
2e074 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2e075 28 7a 54 79 70 65 5b 69 2d 31 5d 3d 3d 27 20 27  (zType[i-1]==' '
2e076 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a  );.            z
2e077 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27 5c 30 27  Type[i-1] = '\0'
2e078 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2e079 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43          pTab->aC
2e07a 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 48 69 64 64 65  ol[iCol].isHidde
2e07b 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  n = 1;.        }
2e07c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2e07d 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46   }..  sqlite3DbF
2e07e 72 65 65 28 64 62 2c 20 7a 4d 6f 64 75 6c 65 4e  ree(db, zModuleN
2e07f 61 6d 65 29 3b 0a 20 20 64 62 2d 3e 70 56 54 61  ame);.  db->pVTa
2e080 62 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  b = 0;.  return 
2e081 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
2e082 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  s function is in
2e083 76 6f 6b 65 64 20 62 79 20 74 68 65 20 70 61 72  voked by the par
2e084 73 65 72 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  ser to call the 
2e085 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f  xConnect() metho
2e086 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74  d.** of the virt
2e087 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62 2e 20  ual table pTab. 
2e088 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
2e089 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  rs, an error cod
2e08a 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 0a 2a  e is returned .*
2e08b 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6c  * and an error l
2e08c 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a  eft in pParse..*
2e08d 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69  *.** This call i
2e08e 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 61 62  s a no-op if tab
2e08f 6c 65 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61  le pTab is not a
2e090 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
2e091 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e092 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
2e093 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72  bCallConnect(Par
2e094 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
2e095 65 20 2a 70 54 61 62 29 7b 0a 20 20 73 71 6c 69  e *pTab){.  sqli
2e096 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2e097 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68  ->db;.  const ch
2e098 61 72 20 2a 7a 4d 6f 64 3b 0a 20 20 4d 6f 64 75  ar *zMod;.  Modu
2e099 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74 20  le *pMod;.  int 
2e09a 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  rc;..  assert( p
2e09b 54 61 62 20 29 3b 0a 20 20 69 66 28 20 28 70 54  Tab );.  if( (pT
2e09c 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
2e09d 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 20 7c 7c  F_Virtual)==0 ||
2e09e 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c   sqlite3GetVTabl
2e09f 65 28 64 62 2c 20 70 54 61 62 29 20 29 7b 0a 20  e(db, pTab) ){. 
2e0a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2e0a1 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c  _OK;.  }..  /* L
2e0a2 6f 63 61 74 65 20 74 68 65 20 72 65 71 75 69 72  ocate the requir
2e0a3 65 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ed virtual table
2e0a4 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 7a 4d 6f   module */.  zMo
2e0a5 64 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75  d = pTab->azModu
2e0a6 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64  leArg[0];.  pMod
2e0a7 20 3d 20 28 4d 6f 64 75 6c 65 2a 29 73 71 6c 69   = (Module*)sqli
2e0a8 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d  te3HashFind(&db-
2e0a9 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64 2c 20  >aModule, zMod, 
2e0aa 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2e0ab 7a 4d 6f 64 29 29 3b 0a 0a 20 20 69 66 28 20 21  zMod));..  if( !
2e0ac 70 4d 6f 64 20 29 7b 0a 20 20 20 20 63 6f 6e 73  pMod ){.    cons
2e0ad 74 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 20  t char *zModule 
2e0ae 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  = pTab->azModule
2e0af 41 72 67 5b 30 5d 3b 0a 20 20 20 20 73 71 6c 69  Arg[0];.    sqli
2e0b0 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2e0b1 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 6d 6f 64  se, "no such mod
2e0b2 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c  ule: %s", zModul
2e0b3 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  e);.    rc = SQL
2e0b4 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c  ITE_ERROR;.  }el
2e0b5 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45  se{.    char *zE
2e0b6 72 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  rr = 0;.    rc =
2e0b7 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75   vtabCallConstru
2e0b8 63 74 6f 72 28 64 62 2c 20 70 54 61 62 2c 20 70  ctor(db, pTab, p
2e0b9 4d 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75  Mod, pMod->pModu
2e0ba 6c 65 2d 3e 78 43 6f 6e 6e 65 63 74 2c 20 26 7a  le->xConnect, &z
2e0bb 45 72 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Err);.    if( rc
2e0bc 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
2e0bd 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2e0be 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73  rMsg(pParse, "%s
2e0bf 22 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a  ", zErr);.    }.
2e0c0 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2e0c1 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 7d  e(db, zErr);.  }
2e0c2 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
2e0c3 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  ../*.** Add the 
2e0c4 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 56  virtual table pV
2e0c5 54 61 62 20 74 6f 20 74 68 65 20 61 72 72 61 79  Tab to the array
2e0c6 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73   sqlite3.aVTrans
2e0c7 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  []..*/.static in
2e0c8 74 20 61 64 64 54 6f 56 54 72 61 6e 73 28 73 71  t addToVTrans(sq
2e0c9 6c 69 74 65 33 20 2a 64 62 2c 20 56 54 61 62 6c  lite3 *db, VTabl
2e0ca 65 20 2a 70 56 54 61 62 29 7b 0a 20 20 63 6f 6e  e *pVTab){.  con
2e0cb 73 74 20 69 6e 74 20 41 52 52 41 59 5f 49 4e 43  st int ARRAY_INC
2e0cc 52 20 3d 20 35 3b 0a 0a 20 20 2f 2a 20 47 72 6f  R = 5;..  /* Gro
2e0cd 77 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56  w the sqlite3.aV
2e0ce 54 72 61 6e 73 20 61 72 72 61 79 20 69 66 20 72  Trans array if r
2e0cf 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 66 28  equired */.  if(
2e0d0 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73 25 41 52   (db->nVTrans%AR
2e0d1 52 41 59 5f 49 4e 43 52 29 3d 3d 30 20 29 7b 0a  RAY_INCR)==0 ){.
2e0d2 20 20 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 54      VTable **aVT
2e0d3 72 61 6e 73 3b 0a 20 20 20 20 69 6e 74 20 6e 42  rans;.    int nB
2e0d4 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 73 71  ytes = sizeof(sq
2e0d5 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 20 2a 20  lite3_vtab *) * 
2e0d6 28 64 62 2d 3e 6e 56 54 72 61 6e 73 20 2b 20 41  (db->nVTrans + A
2e0d7 52 52 41 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20  RRAY_INCR);.    
2e0d8 61 56 54 72 61 6e 73 20 3d 20 73 71 6c 69 74 65  aVTrans = sqlite
2e0d9 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 28  3DbRealloc(db, (
2e0da 76 6f 69 64 20 2a 29 64 62 2d 3e 61 56 54 72 61  void *)db->aVTra
2e0db 6e 73 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20  ns, nBytes);.   
2e0dc 20 69 66 28 20 21 61 56 54 72 61 6e 73 20 29 7b   if( !aVTrans ){
2e0dd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
2e0de 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
2e0df 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61 56  }.    memset(&aV
2e0e0 54 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72 61 6e  Trans[db->nVTran
2e0e1 73 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 71  s], 0, sizeof(sq
2e0e2 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 2a 41 52  lite3_vtab *)*AR
2e0e3 52 41 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20 64  RAY_INCR);.    d
2e0e4 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61 56 54  b->aVTrans = aVT
2e0e5 72 61 6e 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rans;.  }..  /* 
2e0e6 41 64 64 20 70 56 74 61 62 20 74 6f 20 74 68 65  Add pVtab to the
2e0e7 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e   end of sqlite3.
2e0e8 61 56 54 72 61 6e 73 20 2a 2f 0a 20 20 64 62 2d  aVTrans */.  db-
2e0e9 3e 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e 56 54  >aVTrans[db->nVT
2e0ea 72 61 6e 73 2b 2b 5d 20 3d 20 70 56 54 61 62 3b  rans++] = pVTab;
2e0eb 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f  .  sqlite3VtabLo
2e0ec 63 6b 28 70 56 54 61 62 29 3b 0a 20 20 72 65 74  ck(pVTab);.  ret
2e0ed 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2e0ee 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
2e0ef 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64  ction is invoked
2e0f0 20 62 79 20 74 68 65 20 76 64 62 65 20 74 6f 20   by the vdbe to 
2e0f1 63 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65  call the xCreate
2e0f2 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68   method.** of th
2e0f3 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2e0f4 6e 61 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61  named zTab in da
2e0f5 74 61 62 61 73 65 20 69 44 62 2e 20 0a 2a 2a 0a  tabase iDb. .**.
2e0f6 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
2e0f7 63 63 75 72 73 2c 20 2a 70 7a 45 72 72 20 69 73  ccurs, *pzErr is
2e0f8 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 6e   set to point an
2e0f9 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67   an English lang
2e0fa 75 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 70 74  uage.** descript
2e0fb 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ion of the error
2e0fc 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 58   and an SQLITE_X
2e0fd 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  XX error code is
2e0fe 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 6e   returned..** In
2e0ff 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63   this case the c
2e100 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20  aller must call 
2e101 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2e102 2c 20 29 20 6f 6e 20 2a 70 7a 45 72 72 2e 0a 2a  , ) on *pzErr..*
2e103 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2e104 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
2e105 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74  CallCreate(sqlit
2e106 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
2e107 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
2e108 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29  b, char **pzErr)
2e109 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
2e10a 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20  ITE_OK;.  Table 
2e10b 2a 70 54 61 62 3b 0a 20 20 4d 6f 64 75 6c 65 20  *pTab;.  Module 
2e10c 2a 70 4d 6f 64 3b 0a 20 20 63 6f 6e 73 74 20 63  *pMod;.  const c
2e10d 68 61 72 20 2a 7a 4d 6f 64 3b 0a 0a 20 20 70 54  har *zMod;..  pT
2e10e 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
2e10f 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20  Table(db, zTab, 
2e110 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2e111 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  me);.  assert( p
2e112 54 61 62 20 26 26 20 28 70 54 61 62 2d 3e 74 61  Tab && (pTab->ta
2e113 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
2e114 75 61 6c 29 21 3d 30 20 26 26 20 21 70 54 61 62  ual)!=0 && !pTab
2e115 2d 3e 70 56 54 61 62 6c 65 20 29 3b 0a 0a 20 20  ->pVTable );..  
2e116 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 72 65  /* Locate the re
2e117 71 75 69 72 65 64 20 76 69 72 74 75 61 6c 20 74  quired virtual t
2e118 61 62 6c 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20  able module */. 
2e119 20 7a 4d 6f 64 20 3d 20 70 54 61 62 2d 3e 61 7a   zMod = pTab->az
2e11a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 20 20  ModuleArg[0];.  
2e11b 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 2a 29  pMod = (Module*)
2e11c 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
2e11d 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4d  &db->aModule, zM
2e11e 6f 64 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  od, sqlite3Strle
2e11f 6e 33 30 28 7a 4d 6f 64 29 29 3b 0a 0a 20 20 2f  n30(zMod));..  /
2e120 2a 20 49 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  * If the module 
2e121 68 61 73 20 62 65 65 6e 20 72 65 67 69 73 74 65  has been registe
2e122 72 65 64 20 61 6e 64 20 69 6e 63 6c 75 64 65 73  red and includes
2e123 20 61 20 43 72 65 61 74 65 20 6d 65 74 68 6f 64   a Create method
2e124 2c 20 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 69  , .  ** invoke i
2e125 74 20 6e 6f 77 2e 20 49 66 20 74 68 65 20 6d 6f  t now. If the mo
2e126 64 75 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65  dule has not bee
2e127 6e 20 72 65 67 69 73 74 65 72 65 64 2c 20 72 65  n registered, re
2e128 74 75 72 6e 20 61 6e 20 0a 20 20 2a 2a 20 65 72  turn an .  ** er
2e129 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ror. Otherwise, 
2e12a 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 20 20 2a 2f  do nothing..  */
2e12b 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a  .  if( !pMod ){.
2e12c 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c      *pzErr = sql
2e12d 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2e12e 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a  "no such module:
2e12f 20 25 73 22 2c 20 7a 4d 6f 64 29 3b 0a 20 20 20   %s", zMod);.   
2e130 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
2e131 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OR;.  }else{.   
2e132 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c 43 6f   rc = vtabCallCo
2e133 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20 70 54  nstructor(db, pT
2e134 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e  ab, pMod, pMod->
2e135 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61 74 65  pModule->xCreate
2e136 2c 20 70 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20  , pzErr);.  }.. 
2e137 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74 69 6f   /* Justificatio
2e138 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 3a 20 20  n of ALWAYS():  
2e139 54 68 65 20 78 43 6f 6e 73 74 72 75 63 74 6f 72  The xConstructor
2e13a 20 6d 65 74 68 6f 64 20 69 73 20 72 65 71 75 69   method is requi
2e13b 72 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61  red to.  ** crea
2e13c 74 65 20 61 20 76 61 6c 69 64 20 73 71 6c 69 74  te a valid sqlit
2e13d 65 33 5f 76 74 61 62 20 69 66 20 69 74 20 72 65  e3_vtab if it re
2e13e 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e  turns SQLITE_OK.
2e13f 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
2e140 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57 41 59  LITE_OK && ALWAY
2e141 53 28 73 71 6c 69 74 65 33 47 65 74 56 54 61 62  S(sqlite3GetVTab
2e142 6c 65 28 64 62 2c 20 70 54 61 62 29 29 20 29 7b  le(db, pTab)) ){
2e143 0a 20 20 20 20 20 20 72 63 20 3d 20 61 64 64 54  .      rc = addT
2e144 6f 56 54 72 61 6e 73 28 64 62 2c 20 73 71 6c 69  oVTrans(db, sqli
2e145 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
2e146 20 70 54 61 62 29 29 3b 0a 20 20 7d 0a 0a 20 20   pTab));.  }..  
2e147 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2e148 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2e149 6e 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74  n is used to set
2e14a 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 61   the schema of a
2e14b 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
2e14c 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 76   It is only.** v
2e14d 61 6c 69 64 20 74 6f 20 63 61 6c 6c 20 74 68 69  alid to call thi
2e14e 73 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 20  s function from 
2e14f 77 69 74 68 69 6e 20 74 68 65 20 78 43 72 65 61  within the xCrea
2e150 74 65 28 29 20 6f 72 20 78 43 6f 6e 6e 65 63 74  te() or xConnect
2e151 28 29 20 6f 66 20 61 0a 2a 2a 20 76 69 72 74 75  () of a.** virtu
2e152 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e  al table module.
2e153 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2e154 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61  nt sqlite3_decla
2e155 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 20  re_vtab(sqlite3 
2e156 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
2e157 2a 7a 43 72 65 61 74 65 54 61 62 6c 65 29 7b 0a  *zCreateTable){.
2e158 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
2e159 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
2e15a 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20  ITE_OK;.  Table 
2e15b 2a 70 54 61 62 3b 0a 20 20 63 68 61 72 20 2a 7a  *pTab;.  char *z
2e15c 45 72 72 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69  Err = 0;..  sqli
2e15d 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2e15e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 54  db->mutex);.  pT
2e15f 61 62 20 3d 20 64 62 2d 3e 70 56 54 61 62 3b 0a  ab = db->pVTab;.
2e160 20 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20    if( !pTab ){. 
2e161 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
2e162 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  db, SQLITE_MISUS
2e163 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  E, 0);.    sqlit
2e164 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2e165 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72  b->mutex);.    r
2e166 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
2e167 55 53 45 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  USE;.  }.  asser
2e168 74 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61  t( (pTab->tabFla
2e169 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29  gs & TF_Virtual)
2e16a 21 3d 30 20 29 3b 0a 0a 20 20 70 50 61 72 73 65  !=0 );..  pParse
2e16b 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41   = sqlite3StackA
2e16c 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2e16d 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a 20  eof(*pParse));. 
2e16e 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 29   if( pParse==0 )
2e16f 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
2e170 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65  E_NOMEM;.  }else
2e171 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 65  {.    pParse->de
2e172 63 6c 61 72 65 56 74 61 62 20 3d 20 31 3b 0a 20  clareVtab = 1;. 
2e173 20 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20     pParse->db = 
2e174 64 62 3b 0a 20 20 0a 20 20 20 20 69 66 28 20 0a  db;.  .    if( .
2e175 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
2e176 4b 20 3d 3d 20 73 71 6c 69 74 65 33 52 75 6e 50  K == sqlite3RunP
2e177 61 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a 43  arser(pParse, zC
2e178 72 65 61 74 65 54 61 62 6c 65 2c 20 26 7a 45 72  reateTable, &zEr
2e179 72 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 70  r) && .        p
2e17a 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2e17b 20 26 26 20 0a 20 20 20 20 20 20 20 20 21 70 50   && .        !pP
2e17c 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d  arse->pNewTable-
2e17d 3e 70 53 65 6c 65 63 74 20 26 26 20 0a 20 20 20  >pSelect && .   
2e17e 20 20 20 20 20 28 70 50 61 72 73 65 2d 3e 70 4e       (pParse->pN
2e17f 65 77 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61 67  ewTable->tabFlag
2e180 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d  s & TF_Virtual)=
2e181 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  =0.    ){.      
2e182 69 66 28 20 21 70 54 61 62 2d 3e 61 43 6f 6c 20  if( !pTab->aCol 
2e183 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d  ){.        pTab-
2e184 3e 61 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e  >aCol = pParse->
2e185 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b  pNewTable->aCol;
2e186 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 6e  .        pTab->n
2e187 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Col = pParse->pN
2e188 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20  ewTable->nCol;. 
2e189 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 70         pParse->p
2e18a 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d  NewTable->nCol =
2e18b 20 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72   0;.        pPar
2e18c 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 61  se->pNewTable->a
2e18d 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  Col = 0;.      }
2e18e 0a 20 20 20 20 20 20 64 62 2d 3e 70 56 54 61 62  .      db->pVTab
2e18f 20 3d 20 30 3b 0a 20 20 20 20 7d 20 65 6c 73 65   = 0;.    } else
2e190 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
2e191 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
2e192 5f 45 52 52 4f 52 2c 20 7a 45 72 72 29 3b 0a 20  _ERROR, zErr);. 
2e193 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2e194 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  ee(db, zErr);.  
2e195 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2e196 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20  ERROR;.    }.   
2e197 20 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65   pParse->declare
2e198 56 74 61 62 20 3d 20 30 3b 0a 20 20 0a 20 20 20  Vtab = 0;.  .   
2e199 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 56 64   if( pParse->pVd
2e19a 62 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  be ){.      sqli
2e19b 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
2e19c 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a  pParse->pVdbe);.
2e19d 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2e19e 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 50 61  3DeleteTable(pPa
2e19f 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b  rse->pNewTable);
2e1a0 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63  .    sqlite3Stac
2e1a1 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  kFree(db, pParse
2e1a2 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
2e1a3 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72 63 20  ( (rc&0xff)==rc 
2e1a4 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
2e1a5 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
2e1a6 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2e1a7 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2e1a8 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
2e1a9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
2e1aa 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
2e1ab 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20 74  ed by the vdbe t
2e1ac 6f 20 63 61 6c 6c 20 74 68 65 20 78 44 65 73 74  o call the xDest
2e1ad 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66  roy method.** of
2e1ae 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2e1af 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62 20 69 6e  le named zTab in
2e1b0 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 54   database iDb. T
2e1b1 68 69 73 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68  his occurs.** wh
2e1b2 65 6e 20 61 20 44 52 4f 50 20 54 41 42 4c 45 20  en a DROP TABLE 
2e1b3 69 73 20 6d 65 6e 74 69 6f 6e 65 64 2e 0a 2a 2a  is mentioned..**
2e1b4 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73  .** This call is
2e1b5 20 61 20 6e 6f 2d 6f 70 20 69 66 20 7a 54 61 62   a no-op if zTab
2e1b6 20 69 73 20 6e 6f 74 20 61 20 76 69 72 74 75 61   is not a virtua
2e1b7 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  l table..*/.SQLI
2e1b8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2e1b9 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65  qlite3VtabCallDe
2e1ba 73 74 72 6f 79 28 73 71 6c 69 74 65 33 20 2a 64  stroy(sqlite3 *d
2e1bb 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73  b, int iDb, cons
2e1bc 74 20 63 68 61 72 20 2a 7a 54 61 62 29 7b 0a 20  t char *zTab){. 
2e1bd 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2e1be 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  _OK;.  Table *pT
2e1bf 61 62 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71  ab;..  pTab = sq
2e1c0 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
2e1c1 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44 62  b, zTab, db->aDb
2e1c2 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
2e1c3 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62 21  if( ALWAYS(pTab!
2e1c4 3d 30 20 26 26 20 70 54 61 62 2d 3e 70 56 54 61  =0 && pTab->pVTa
2e1c5 62 6c 65 21 3d 30 29 20 29 7b 0a 20 20 20 20 56  ble!=0) ){.    V
2e1c6 54 61 62 6c 65 20 2a 70 20 3d 20 76 74 61 62 44  Table *p = vtabD
2e1c7 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28 64 62 2c  isconnectAll(db,
2e1c8 20 70 54 61 62 29 3b 0a 0a 20 20 20 20 72 63 20   pTab);..    rc 
2e1c9 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  = sqlite3SafetyO
2e1ca 66 66 28 64 62 29 3b 0a 20 20 20 20 61 73 73 65  ff(db);.    asse
2e1cb 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
2e1cc 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 2d  K );.    rc = p-
2e1cd 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e  >pMod->pModule->
2e1ce 78 44 65 73 74 72 6f 79 28 70 2d 3e 70 56 74 61  xDestroy(p->pVta
2e1cf 62 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  b);.    (void)sq
2e1d0 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
2e1d1 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76  );..    /* Remov
2e1d2 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74  e the sqlite3_vt
2e1d3 61 62 2a 20 66 72 6f 6d 20 74 68 65 20 61 56 54  ab* from the aVT
2e1d4 72 61 6e 73 5b 5d 20 61 72 72 61 79 2c 20 69 66  rans[] array, if
2e1d5 20 61 70 70 6c 69 63 61 62 6c 65 20 2a 2f 0a 20   applicable */. 
2e1d6 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2e1d7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73  E_OK ){.      as
2e1d8 73 65 72 74 28 20 70 54 61 62 2d 3e 70 56 54 61  sert( pTab->pVTa
2e1d9 62 6c 65 3d 3d 70 20 26 26 20 70 2d 3e 70 4e 65  ble==p && p->pNe
2e1da 78 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  xt==0 );.      p
2e1db 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20 20 20  ->pVtab = 0;.   
2e1dc 20 20 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65     pTab->pVTable
2e1dd 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 0;.      sqli
2e1de 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70 29  te3VtabUnlock(p)
2e1df 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
2e1e0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
2e1e1 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2e1e2 20 69 6e 76 6f 6b 65 73 20 65 69 74 68 65 72 20   invokes either 
2e1e3 74 68 65 20 78 52 6f 6c 6c 62 61 63 6b 20 6f 72  the xRollback or
2e1e4 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68 6f 64 0a   xCommit method.
2e1e5 2a 2a 20 6f 66 20 65 61 63 68 20 6f 66 20 74 68  ** of each of th
2e1e6 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  e virtual tables
2e1e7 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e   in the sqlite3.
2e1e8 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20 54  aVTrans array. T
2e1e9 68 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 61 6c  he method.** cal
2e1ea 6c 65 64 20 69 73 20 69 64 65 6e 74 69 66 69 65  led is identifie
2e1eb 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20  d by the second 
2e1ec 61 72 67 75 6d 65 6e 74 2c 20 22 6f 66 66 73 65  argument, "offse
2e1ed 74 22 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  t", which is.** 
2e1ee 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68  the offset of th
2e1ef 65 20 6d 65 74 68 6f 64 20 74 6f 20 63 61 6c 6c  e method to call
2e1f0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f   in the sqlite3_
2e1f1 6d 6f 64 75 6c 65 20 73 74 72 75 63 74 75 72 65  module structure
2e1f2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72 61  ..**.** The arra
2e1f3 79 20 69 73 20 63 6c 65 61 72 65 64 20 61 66 74  y is cleared aft
2e1f4 65 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  er invoking the 
2e1f5 63 61 6c 6c 62 61 63 6b 73 2e 20 0a 2a 2f 0a 73  callbacks. .*/.s
2e1f6 74 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c 46  tatic void callF
2e1f7 69 6e 61 6c 69 73 65 72 28 73 71 6c 69 74 65 33  inaliser(sqlite3
2e1f8 20 2a 64 62 2c 20 69 6e 74 20 6f 66 66 73 65 74   *db, int offset
2e1f9 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
2e1fa 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 29 7b  ( db->aVTrans ){
2e1fb 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2e1fc 64 62 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b 2b  db->nVTrans; i++
2e1fd 29 7b 0a 20 20 20 20 20 20 56 54 61 62 6c 65 20  ){.      VTable 
2e1fe 2a 70 56 54 61 62 20 3d 20 64 62 2d 3e 61 56 54  *pVTab = db->aVT
2e1ff 72 61 6e 73 5b 69 5d 3b 0a 20 20 20 20 20 20 73  rans[i];.      s
2e200 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 20 3d  qlite3_vtab *p =
2e201 20 70 56 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20   pVTab->pVtab;. 
2e202 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20       if( p ){.  
2e203 20 20 20 20 20 20 69 6e 74 20 28 2a 78 29 28 73        int (*x)(s
2e204 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a  qlite3_vtab *);.
2e205 20 20 20 20 20 20 20 20 78 20 3d 20 2a 28 69 6e          x = *(in
2e206 74 20 28 2a 2a 29 28 73 71 6c 69 74 65 33 5f 76  t (**)(sqlite3_v
2e207 74 61 62 20 2a 29 29 28 28 63 68 61 72 20 2a 29  tab *))((char *)
2e208 70 2d 3e 70 4d 6f 64 75 6c 65 20 2b 20 6f 66 66  p->pModule + off
2e209 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  set);.        if
2e20a 28 20 78 20 29 20 78 28 70 29 3b 0a 20 20 20 20  ( x ) x(p);.    
2e20b 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2e20c 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70 56 54 61  3VtabUnlock(pVTa
2e20d 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  b);.    }.    sq
2e20e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2e20f 64 62 2d 3e 61 56 54 72 61 6e 73 29 3b 0a 20 20  db->aVTrans);.  
2e210 20 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20 3d 20    db->nVTrans = 
2e211 30 3b 0a 20 20 20 20 64 62 2d 3e 61 56 54 72 61  0;.    db->aVTra
2e212 6e 73 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ns = 0;.  }.}../
2e213 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20  *.** Invoke the 
2e214 78 53 79 6e 63 20 6d 65 74 68 6f 64 20 6f 66 20  xSync method of 
2e215 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c  all virtual tabl
2e216 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  es in the sqlite
2e217 33 2e 61 56 54 72 61 6e 73 0a 2a 2a 20 61 72 72  3.aVTrans.** arr
2e218 61 79 2e 20 52 65 74 75 72 6e 20 74 68 65 20 65  ay. Return the e
2e219 72 72 6f 72 20 63 6f 64 65 20 66 6f 72 20 74 68  rror code for th
2e21a 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 74 68  e first error th
2e21b 61 74 20 6f 63 63 75 72 73 2c 20 6f 72 0a 2a 2a  at occurs, or.**
2e21c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c   SQLITE_OK if al
2e21d 6c 20 78 53 79 6e 63 20 6f 70 65 72 61 74 69 6f  l xSync operatio
2e21e 6e 73 20 61 72 65 20 73 75 63 63 65 73 73 66 75  ns are successfu
2e21f 6c 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 20 2a 70 7a  l..**.** Set *pz
2e220 45 72 72 6d 73 67 20 74 6f 20 70 6f 69 6e 74 20  Errmsg to point 
2e221 74 6f 20 61 20 62 75 66 66 65 72 20 74 68 61 74  to a buffer that
2e222 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61   should be relea
2e223 73 65 64 20 75 73 69 6e 67 20 0a 2a 2a 20 73 71  sed using .** sq
2e224 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 63 6f  lite3DbFree() co
2e225 6e 74 61 69 6e 69 6e 67 20 61 6e 20 65 72 72 6f  ntaining an erro
2e226 72 20 6d 65 73 73 61 67 65 2c 20 69 66 20 6f 6e  r message, if on
2e227 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  e is available..
2e228 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e229 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
2e22a 62 53 79 6e 63 28 73 71 6c 69 74 65 33 20 2a 64  bSync(sqlite3 *d
2e22b 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d  b, char **pzErrm
2e22c 73 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  sg){.  int i;.  
2e22d 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2e22e 4f 4b 3b 0a 20 20 69 6e 74 20 72 63 73 61 66 65  OK;.  int rcsafe
2e22f 74 79 3b 0a 20 20 56 54 61 62 6c 65 20 2a 2a 61  ty;.  VTable **a
2e230 56 54 72 61 6e 73 20 3d 20 64 62 2d 3e 61 56 54  VTrans = db->aVT
2e231 72 61 6e 73 3b 0a 0a 20 20 72 63 20 3d 20 73 71  rans;..  rc = sq
2e232 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
2e233 62 29 3b 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e  b);.  db->aVTran
2e234 73 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  s = 0;.  for(i=0
2e235 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
2e236 26 26 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73  && i<db->nVTrans
2e237 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; i++){.    int 
2e238 28 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61  (*x)(sqlite3_vta
2e239 62 20 2a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  b *);.    sqlite
2e23a 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20  3_vtab *pVtab = 
2e23b 61 56 54 72 61 6e 73 5b 69 5d 2d 3e 70 56 74 61  aVTrans[i]->pVta
2e23c 62 3b 0a 20 20 20 20 69 66 28 20 70 56 74 61 62  b;.    if( pVtab
2e23d 20 26 26 20 28 78 20 3d 20 70 56 74 61 62 2d 3e   && (x = pVtab->
2e23e 70 4d 6f 64 75 6c 65 2d 3e 78 53 79 6e 63 29 21  pModule->xSync)!
2e23f 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  =0 ){.      rc =
2e240 20 78 28 70 56 74 61 62 29 3b 0a 20 20 20 20 20   x(pVtab);.     
2e241 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2e242 62 2c 20 2a 70 7a 45 72 72 6d 73 67 29 3b 0a 20  b, *pzErrmsg);. 
2e243 20 20 20 20 20 2a 70 7a 45 72 72 6d 73 67 20 3d       *pzErrmsg =
2e244 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b   pVtab->zErrMsg;
2e245 0a 20 20 20 20 20 20 70 56 74 61 62 2d 3e 7a 45  .      pVtab->zE
2e246 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 7d  rrMsg = 0;.    }
2e247 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 56 54 72 61  .  }.  db->aVTra
2e248 6e 73 20 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20  ns = aVTrans;.  
2e249 72 63 73 61 66 65 74 79 20 3d 20 73 71 6c 69 74  rcsafety = sqlit
2e24a 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
2e24b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2e24c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
2e24d 20 72 63 73 61 66 65 74 79 3b 0a 20 20 7d 0a 20   rcsafety;.  }. 
2e24e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2e24f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20  *.** Invoke the 
2e250 78 52 6f 6c 6c 62 61 63 6b 20 6d 65 74 68 6f 64  xRollback method
2e251 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20   of all virtual 
2e252 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a  tables in the .*
2e253 2a 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  * sqlite3.aVTran
2e254 73 20 61 72 72 61 79 2e 20 54 68 65 6e 20 63 6c  s array. Then cl
2e255 65 61 72 20 74 68 65 20 61 72 72 61 79 20 69 74  ear the array it
2e256 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  self..*/.SQLITE_
2e257 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2e258 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28  te3VtabRollback(
2e259 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2e25a 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28 64 62  callFinaliser(db
2e25b 2c 20 6f 66 66 73 65 74 6f 66 28 73 71 6c 69 74  , offsetof(sqlit
2e25c 65 33 5f 6d 6f 64 75 6c 65 2c 78 52 6f 6c 6c 62  e3_module,xRollb
2e25d 61 63 6b 29 29 3b 0a 20 20 72 65 74 75 72 6e 20  ack));.  return 
2e25e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2e25f 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78  .** Invoke the x
2e260 43 6f 6d 6d 69 74 20 6d 65 74 68 6f 64 20 6f 66  Commit method of
2e261 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62   all virtual tab
2e262 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73  les in the .** s
2e263 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
2e264 72 72 61 79 2e 20 54 68 65 6e 20 63 6c 65 61 72  rray. Then clear
2e265 20 74 68 65 20 61 72 72 61 79 20 69 74 73 65 6c   the array itsel
2e266 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  f..*/.SQLITE_PRI
2e267 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2e268 56 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74  VtabCommit(sqlit
2e269 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46  e3 *db){.  callF
2e26a 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f 66 66  inaliser(db, off
2e26b 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f  setof(sqlite3_mo
2e26c 64 75 6c 65 2c 78 43 6f 6d 6d 69 74 29 29 3b 0a  dule,xCommit));.
2e26d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2e26e 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  OK;.}../*.** If 
2e26f 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2e270 65 20 70 56 74 61 62 20 73 75 70 70 6f 72 74 73  e pVtab supports
2e271 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
2e272 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 28 78   interface.** (x
2e273 42 65 67 69 6e 2f 78 52 6f 6c 6c 62 61 63 6b 2f  Begin/xRollback/
2e274 78 43 6f 6d 6d 69 74 20 61 6e 64 20 6f 70 74 69  xCommit and opti
2e275 6f 6e 61 6c 6c 79 20 78 53 79 6e 63 29 20 61 6e  onally xSync) an
2e276 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  d a transaction 
2e277 69 73 0a 2a 2a 20 6e 6f 74 20 63 75 72 72 65 6e  is.** not curren
2e278 74 6c 79 20 6f 70 65 6e 2c 20 69 6e 76 6f 6b 65  tly open, invoke
2e279 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65 74 68   the xBegin meth
2e27a 6f 64 20 6e 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66  od now..**.** If
2e27b 20 74 68 65 20 78 42 65 67 69 6e 20 63 61 6c 6c   the xBegin call
2e27c 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
2e27d 70 6c 61 63 65 20 74 68 65 20 73 71 6c 69 74 65  place the sqlite
2e27e 33 5f 76 74 61 62 20 70 6f 69 6e 74 65 72 0a 2a  3_vtab pointer.*
2e27f 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  * in the sqlite3
2e280 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 0a  .aVTrans array..
2e281 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e282 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
2e283 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a  bBegin(sqlite3 *
2e284 64 62 2c 20 56 54 61 62 6c 65 20 2a 70 56 54 61  db, VTable *pVTa
2e285 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  b){.  int rc = S
2e286 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73  QLITE_OK;.  cons
2e287 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
2e288 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a   *pModule;..  /*
2e289 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 49   Special case: I
2e28a 66 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 69 73  f db->aVTrans is
2e28b 20 4e 55 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e 56   NULL and db->nV
2e28c 54 72 61 6e 73 20 69 73 20 67 72 65 61 74 65 72  Trans is greater
2e28d 0a 20 20 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c  .  ** than zero,
2e28e 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
2e28f 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c  ion is being cal
2e290 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  led from within 
2e291 61 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d  a.  ** virtual m
2e292 6f 64 75 6c 65 20 78 53 79 6e 63 28 29 20 63 61  odule xSync() ca
2e293 6c 6c 62 61 63 6b 2e 20 49 74 20 69 73 20 69 6c  llback. It is il
2e294 6c 65 67 61 6c 20 74 6f 20 77 72 69 74 65 20 74  legal to write t
2e295 6f 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20  o .  ** virtual 
2e296 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 69 6e  module tables in
2e297 20 74 68 69 73 20 63 61 73 65 2c 20 73 6f 20 72   this case, so r
2e298 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
2e299 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  KED..  */.  if( 
2e29a 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e  sqlite3VtabInSyn
2e29b 63 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  c(db) ){.    ret
2e29c 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
2e29d 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 56  D;.  }.  if( !pV
2e29e 54 61 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Tab ){.    retur
2e29f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
2e2a0 20 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56   .  pModule = pV
2e2a1 54 61 62 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64  Tab->pVtab->pMod
2e2a2 75 6c 65 3b 0a 0a 20 20 69 66 28 20 70 4d 6f 64  ule;..  if( pMod
2e2a3 75 6c 65 2d 3e 78 42 65 67 69 6e 20 29 7b 0a 20  ule->xBegin ){. 
2e2a4 20 20 20 69 6e 74 20 69 3b 0a 0a 0a 20 20 20 20     int i;...    
2e2a5 2f 2a 20 49 66 20 70 56 74 61 62 20 69 73 20 61  /* If pVtab is a
2e2a6 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 61 56  lready in the aV
2e2a7 54 72 61 6e 73 20 61 72 72 61 79 2c 20 72 65 74  Trans array, ret
2e2a8 75 72 6e 20 65 61 72 6c 79 20 2a 2f 0a 20 20 20  urn early */.   
2e2a9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
2e2aa 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20  nVTrans; i++){. 
2e2ab 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 56 54       if( db->aVT
2e2ac 72 61 6e 73 5b 69 5d 3d 3d 70 56 54 61 62 20 29  rans[i]==pVTab )
2e2ad 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
2e2ae 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
2e2af 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
2e2b0 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 42 65  * Invoke the xBe
2e2b1 67 69 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  gin method */.  
2e2b2 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
2e2b3 78 42 65 67 69 6e 28 70 56 54 61 62 2d 3e 70 56  xBegin(pVTab->pV
2e2b4 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63  tab);.    if( rc
2e2b5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
2e2b6 20 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 56       rc = addToV
2e2b7 54 72 61 6e 73 28 64 62 2c 20 70 56 54 61 62 29  Trans(db, pVTab)
2e2b8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2e2b9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2e2ba 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
2e2bb 6d 65 74 65 72 20 28 70 44 65 66 29 20 69 73 20  meter (pDef) is 
2e2bc 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  a function imple
2e2bd 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a  mentation.  The.
2e2be 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  ** second parame
2e2bf 74 65 72 20 28 70 45 78 70 72 29 20 69 73 20 74  ter (pExpr) is t
2e2c0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
2e2c1 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
2e2c2 6f 6e 2e 0a 2a 2a 20 49 66 20 70 45 78 70 72 20  on..** If pExpr 
2e2c3 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61  is a column in a
2e2c4 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20   virtual table, 
2e2c5 74 68 65 6e 20 6c 65 74 20 74 68 65 20 76 69 72  then let the vir
2e2c6 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 6d  tual.** table im
2e2c7 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 76  plementation hav
2e2c8 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79  e an opportunity
2e2c9 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 65   to overload the
2e2ca 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
2e2cb 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2e2cc 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 77 20 76   used to allow v
2e2cd 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
2e2ce 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f 0a  lementations to.
2e2cf 2a 2a 20 6f 76 65 72 6c 6f 61 64 20 4d 41 54 43  ** overload MATC
2e2d0 48 2c 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 61  H, LIKE, GLOB, a
2e2d1 6e 64 20 52 45 47 45 58 50 20 6f 70 65 72 61 74  nd REGEXP operat
2e2d2 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ors..**.** Retur
2e2d3 6e 20 65 69 74 68 65 72 20 74 68 65 20 70 44 65  n either the pDe
2e2d4 66 20 61 72 67 75 6d 65 6e 74 20 28 69 6e 64 69  f argument (indi
2e2d5 63 61 74 69 6e 67 20 6e 6f 20 63 68 61 6e 67 65  cating no change
2e2d6 29 20 6f 72 20 61 20 0a 2a 2a 20 6e 65 77 20 46  ) or a .** new F
2e2d7 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65  uncDef structure
2e2d8 20 74 68 61 74 20 69 73 20 6d 61 72 6b 65 64 20   that is marked 
2e2d9 61 73 20 65 70 68 65 6d 65 72 61 6c 20 75 73 69  as ephemeral usi
2e2da 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  ng the.** SQLITE
2e2db 5f 46 55 4e 43 5f 45 50 48 45 4d 20 66 6c 61 67  _FUNC_EPHEM flag
2e2dc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2e2dd 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c  ATE FuncDef *sql
2e2de 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64  ite3VtabOverload
2e2df 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69  Function(.  sqli
2e2e0 74 65 33 20 2a 64 62 2c 20 20 20 20 2f 2a 20 44  te3 *db,    /* D
2e2e1 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2e2e2 6f 6e 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67  on for reporting
2e2e3 20 6d 61 6c 6c 6f 63 20 70 72 6f 62 6c 65 6d 73   malloc problems
2e2e4 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70   */.  FuncDef *p
2e2e5 44 65 66 2c 20 20 2f 2a 20 46 75 6e 63 74 69 6f  Def,  /* Functio
2e2e6 6e 20 74 6f 20 70 6f 73 73 69 62 6c 79 20 6f 76  n to possibly ov
2e2e7 65 72 6c 6f 61 64 20 2a 2f 0a 20 20 69 6e 74 20  erload */.  int 
2e2e8 6e 41 72 67 2c 20 20 20 20 20 20 20 2f 2a 20 4e  nArg,       /* N
2e2e9 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
2e2ea 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  ts to the functi
2e2eb 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  on */.  Expr *pE
2e2ec 78 70 72 20 20 20 20 20 2f 2a 20 46 69 72 73 74  xpr     /* First
2e2ed 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
2e2ee 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a   function */.){.
2e2ef 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
2e2f0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
2e2f1 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Vtab;.  sqlite3_
2e2f2 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20  module *pMod;.  
2e2f3 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
2e2f4 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
2e2f5 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
2e2f6 2a 2a 29 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20  **) = 0;.  void 
2e2f7 2a 70 41 72 67 20 3d 20 30 3b 0a 20 20 46 75 6e  *pArg = 0;.  Fun
2e2f8 63 44 65 66 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  cDef *pNew;.  in
2e2f9 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 68 61 72  t rc = 0;.  char
2e2fa 20 2a 7a 4c 6f 77 65 72 4e 61 6d 65 3b 0a 20 20   *zLowerName;.  
2e2fb 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2e2fc 3b 0a 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  ;...  /* Check t
2e2fd 6f 20 73 65 65 20 74 68 65 20 6c 65 66 74 20 6f  o see the left o
2e2fe 70 65 72 61 6e 64 20 69 73 20 61 20 63 6f 6c 75  perand is a colu
2e2ff 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20  mn in a virtual 
2e300 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 4e  table */.  if( N
2e301 45 56 45 52 28 70 45 78 70 72 3d 3d 30 29 20 29  EVER(pExpr==0) )
2e302 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
2e303 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
2e304 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72  K_COLUMN ) retur
2e305 6e 20 70 44 65 66 3b 0a 20 20 70 54 61 62 20 3d  n pDef;.  pTab =
2e306 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20   pExpr->pTab;.  
2e307 69 66 28 20 4e 45 56 45 52 28 70 54 61 62 3d 3d  if( NEVER(pTab==
2e308 30 29 20 29 20 72 65 74 75 72 6e 20 70 44 65 66  0) ) return pDef
2e309 3b 0a 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74  ;.  if( (pTab->t
2e30a 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72  abFlags & TF_Vir
2e30b 74 75 61 6c 29 3d 3d 30 20 29 20 72 65 74 75 72  tual)==0 ) retur
2e30c 6e 20 70 44 65 66 3b 0a 20 20 70 56 74 61 62 20  n pDef;.  pVtab 
2e30d 3d 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 62  = sqlite3GetVTab
2e30e 6c 65 28 64 62 2c 20 70 54 61 62 29 2d 3e 70 56  le(db, pTab)->pV
2e30f 74 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  tab;.  assert( p
2e310 56 74 61 62 21 3d 30 20 29 3b 0a 20 20 61 73 73  Vtab!=0 );.  ass
2e311 65 72 74 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64  ert( pVtab->pMod
2e312 75 6c 65 21 3d 30 20 29 3b 0a 20 20 70 4d 6f 64  ule!=0 );.  pMod
2e313 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75   = (sqlite3_modu
2e314 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64  le *)pVtab->pMod
2e315 75 6c 65 3b 0a 20 20 69 66 28 20 70 4d 6f 64 2d  ule;.  if( pMod-
2e316 3e 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 3d 3d  >xFindFunction==
2e317 30 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b  0 ) return pDef;
2e318 0a 20 0a 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65  . .  /* Call the
2e319 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d   xFindFunction m
2e31a 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 69 72  ethod on the vir
2e31b 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65  tual table imple
2e31c 6d 65 6e 74 61 74 69 6f 6e 0a 20 20 2a 2a 20 74  mentation.  ** t
2e31d 6f 20 73 65 65 20 69 66 20 74 68 65 20 69 6d 70  o see if the imp
2e31e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 61 6e 74  lementation want
2e31f 73 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68  s to overload th
2e320 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a  is function .  *
2e321 2f 0a 20 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 3d  /.  zLowerName =
2e322 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
2e323 28 64 62 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65  (db, pDef->zName
2e324 29 3b 0a 20 20 69 66 28 20 7a 4c 6f 77 65 72 4e  );.  if( zLowerN
2e325 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 28 7a  ame ){.    for(z
2e326 3d 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  =(unsigned char*
2e327 29 7a 4c 6f 77 65 72 4e 61 6d 65 3b 20 2a 7a 3b  )zLowerName; *z;
2e328 20 7a 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 7a 20   z++){.      *z 
2e329 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  = sqlite3UpperTo
2e32a 4c 6f 77 65 72 5b 2a 7a 5d 3b 0a 20 20 20 20 7d  Lower[*z];.    }
2e32b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e  .    rc = pMod->
2e32c 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 56  xFindFunction(pV
2e32d 74 61 62 2c 20 6e 41 72 67 2c 20 7a 4c 6f 77 65  tab, nArg, zLowe
2e32e 72 4e 61 6d 65 2c 20 26 78 46 75 6e 63 2c 20 26  rName, &xFunc, &
2e32f 70 41 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  pArg);.    sqlit
2e330 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4c 6f  e3DbFree(db, zLo
2e331 77 65 72 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20  werName);.  }.  
2e332 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
2e333 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
2e334 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 61  }..  /* Create a
2e335 20 6e 65 77 20 65 70 68 65 6d 65 72 61 6c 20 66   new ephemeral f
2e336 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
2e337 6f 6e 20 66 6f 72 20 74 68 65 20 6f 76 65 72 6c  on for the overl
2e338 6f 61 64 65 64 0a 20 20 2a 2a 20 66 75 6e 63 74  oaded.  ** funct
2e339 69 6f 6e 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20  ion */.  pNew = 
2e33a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2e33b 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  ero(db, sizeof(*
2e33c 70 4e 65 77 29 0a 20 20 20 20 20 20 20 20 20 20  pNew).          
2e33d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e33e 20 20 20 2b 20 73 71 6c 69 74 65 33 53 74 72 6c     + sqlite3Strl
2e33f 65 6e 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65  en30(pDef->zName
2e340 29 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 70 4e  ) + 1);.  if( pN
2e341 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ew==0 ){.    ret
2e342 75 72 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 20 20  urn pDef;.  }.  
2e343 2a 70 4e 65 77 20 3d 20 2a 70 44 65 66 3b 0a 20  *pNew = *pDef;. 
2e344 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28   pNew->zName = (
2e345 63 68 61 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b  char *)&pNew[1];
2e346 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e  .  memcpy(pNew->
2e347 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 7a 4e 61  zName, pDef->zNa
2e348 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  me, sqlite3Strle
2e349 6e 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29  n30(pDef->zName)
2e34a 2b 31 29 3b 0a 20 20 70 4e 65 77 2d 3e 78 46 75  +1);.  pNew->xFu
2e34b 6e 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 4e  nc = xFunc;.  pN
2e34c 65 77 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20  ew->pUserData = 
2e34d 70 41 72 67 3b 0a 20 20 70 4e 65 77 2d 3e 66 6c  pArg;.  pNew->fl
2e34e 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 46 55  ags |= SQLITE_FU
2e34f 4e 43 5f 45 50 48 45 4d 3b 0a 20 20 72 65 74 75  NC_EPHEM;.  retu
2e350 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
2e351 2a 20 4d 61 6b 65 20 73 75 72 65 20 76 69 72 74  * Make sure virt
2e352 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62 20 69  ual table pTab i
2e353 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  s contained in t
2e354 68 65 20 70 50 61 72 73 65 2d 3e 61 70 56 69 72  he pParse->apVir
2e355 74 75 61 6c 4c 6f 63 6b 5b 5d 0a 2a 2a 20 61 72  tualLock[].** ar
2e356 72 61 79 20 73 6f 20 74 68 61 74 20 61 6e 20 4f  ray so that an O
2e357 50 5f 56 42 65 67 69 6e 20 77 69 6c 6c 20 67 65  P_VBegin will ge
2e358 74 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  t generated for 
2e359 69 74 2e 20 20 41 64 64 20 70 54 61 62 20 74 6f  it.  Add pTab to
2e35a 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 69 66   the.** array if
2e35b 20 69 74 20 69 73 20 6d 69 73 73 69 6e 67 2e 20   it is missing. 
2e35c 20 49 66 20 70 54 61 62 20 69 73 20 61 6c 72 65   If pTab is alre
2e35d 61 64 79 20 69 6e 20 74 68 65 20 61 72 72 61 79  ady in the array
2e35e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  , this routine.*
2e35f 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  * is a no-op..*/
2e360 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2e361 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
2e362 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72  MakeWritable(Par
2e363 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
2e364 65 20 2a 70 54 61 62 29 7b 0a 20 20 50 61 72 73  e *pTab){.  Pars
2e365 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73  e *pToplevel = s
2e366 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
2e367 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 69  vel(pParse);.  i
2e368 6e 74 20 69 2c 20 6e 3b 0a 20 20 54 61 62 6c 65  nt i, n;.  Table
2e369 20 2a 2a 61 70 56 74 61 62 4c 6f 63 6b 3b 0a 0a   **apVtabLock;..
2e36a 20 20 61 73 73 65 72 74 28 20 49 73 56 69 72 74    assert( IsVirt
2e36b 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 20 20 66  ual(pTab) );.  f
2e36c 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 70 6c 65  or(i=0; i<pTople
2e36d 76 65 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20  vel->nVtabLock; 
2e36e 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54  i++){.    if( pT
2e36f 61 62 3d 3d 70 54 6f 70 6c 65 76 65 6c 2d 3e 61  ab==pToplevel->a
2e370 70 56 74 61 62 4c 6f 63 6b 5b 69 5d 20 29 20 72  pVtabLock[i] ) r
2e371 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 20 3d  eturn;.  }.  n =
2e372 20 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74   (pToplevel->nVt
2e373 61 62 4c 6f 63 6b 2b 31 29 2a 73 69 7a 65 6f 66  abLock+1)*sizeof
2e374 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74  (pToplevel->apVt
2e375 61 62 4c 6f 63 6b 5b 30 5d 29 3b 0a 20 20 61 70  abLock[0]);.  ap
2e376 56 74 61 62 4c 6f 63 6b 20 3d 20 73 71 6c 69 74  VtabLock = sqlit
2e377 65 33 5f 72 65 61 6c 6c 6f 63 28 70 54 6f 70 6c  e3_realloc(pTopl
2e378 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  evel->apVtabLock
2e379 2c 20 6e 29 3b 0a 20 20 69 66 28 20 61 70 56 74  , n);.  if( apVt
2e37a 61 62 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 54  abLock ){.    pT
2e37b 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c  oplevel->apVtabL
2e37c 6f 63 6b 20 3d 20 61 70 56 74 61 62 4c 6f 63 6b  ock = apVtabLock
2e37d 3b 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d  ;.    pToplevel-
2e37e 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 70 54 6f 70  >apVtabLock[pTop
2e37f 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b  level->nVtabLock
2e380 2b 2b 5d 20 3d 20 70 54 61 62 3b 0a 20 20 7d 65  ++] = pTab;.  }e
2e381 6c 73 65 7b 0a 20 20 20 20 70 54 6f 70 6c 65 76  lse{.    pToplev
2e382 65 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  el->db->mallocFa
2e383 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a  iled = 1;.  }.}.
2e384 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2e385 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2e386 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  BLE */../*******
2e387 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
2e388 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tab.c **********
2e389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e38a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e38b 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2e38c 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2e38d 6c 65 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a  le where.c *****
2e38e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e38f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e390 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2e391 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
2e392 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2e393 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2e394 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2e395 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2e396 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2e397 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2e398 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2e399 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2e39a 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2e39b 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2e39c 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2e39d 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2e39e 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2e39f 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2e3a0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2e3a1 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2e3a2 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2e3a3 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2e3a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e3a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2e3a8 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 63 6f  * This module co
2e3a9 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68  ntains C code th
2e3aa 61 74 20 67 65 6e 65 72 61 74 65 73 20 56 44 42  at generates VDB
2e3ab 45 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 70  E code used to p
2e3ac 72 6f 63 65 73 73 0a 2a 2a 20 74 68 65 20 57 48  rocess.** the WH
2e3ad 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 53 51  ERE clause of SQ
2e3ae 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54  L statements.  T
2e3af 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 72 65  his module is re
2e3b0 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a  sponsible for.**
2e3b1 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20   generating the 
2e3b2 63 6f 64 65 20 74 68 61 74 20 6c 6f 6f 70 73 20  code that loops 
2e3b3 74 68 72 6f 75 67 68 20 61 20 74 61 62 6c 65 20  through a table 
2e3b4 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 70 70 6c  looking for appl
2e3b5 69 63 61 62 6c 65 0a 2a 2a 20 72 6f 77 73 2e 20  icable.** rows. 
2e3b6 20 49 6e 64 69 63 65 73 20 61 72 65 20 73 65 6c   Indices are sel
2e3b7 65 63 74 65 64 20 61 6e 64 20 75 73 65 64 20 74  ected and used t
2e3b8 6f 20 73 70 65 65 64 20 74 68 65 20 73 65 61 72  o speed the sear
2e3b9 63 68 20 77 68 65 6e 20 64 6f 69 6e 67 0a 2a 2a  ch when doing.**
2e3ba 20 73 6f 20 69 73 20 61 70 70 6c 69 63 61 62 6c   so is applicabl
2e3bb 65 2e 20 20 42 65 63 61 75 73 65 20 74 68 69 73  e.  Because this
2e3bc 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 73 70 6f   module is respo
2e3bd 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 6c 65 63  nsible for selec
2e3be 74 69 6e 67 0a 2a 2a 20 69 6e 64 69 63 65 73 2c  ting.** indices,
2e3bf 20 79 6f 75 20 6d 69 67 68 74 20 61 6c 73 6f 20   you might also 
2e3c0 74 68 69 6e 6b 20 6f 66 20 74 68 69 73 20 6d 6f  think of this mo
2e3c1 64 75 6c 65 20 61 73 20 74 68 65 20 22 71 75 65  dule as the "que
2e3c2 72 79 20 6f 70 74 69 6d 69 7a 65 72 22 2e 0a 2a  ry optimizer"..*
2e3c3 2f 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f  /../*.** Trace o
2e3c4 75 74 70 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a  utput macros.*/.
2e3c5 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
2e3c6 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69  TE_TEST) || defi
2e3c7 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
2e3c8 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
2e3c9 20 69 6e 74 20 73 71 6c 69 74 65 33 57 68 65 72   int sqlite3Wher
2e3ca 65 54 72 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64  eTrace = 0;.#end
2e3cb 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  if.#if defined(S
2e3cc 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64  QLITE_TEST) && d
2e3cd 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
2e3ce 42 55 47 29 0a 23 20 64 65 66 69 6e 65 20 57 48  BUG).# define WH
2e3cf 45 52 45 54 52 41 43 45 28 58 29 20 20 69 66 28  ERETRACE(X)  if(
2e3d0 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63  sqlite3WhereTrac
2e3d1 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  e) sqlite3DebugP
2e3d2 72 69 6e 74 66 20 58 0a 23 65 6c 73 65 0a 23 20  rintf X.#else.# 
2e3d3 64 65 66 69 6e 65 20 57 48 45 52 45 54 52 41 43  define WHERETRAC
2e3d4 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  E(X).#endif../* 
2e3d5 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
2e3d6 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  e.*/.typedef str
2e3d7 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 20  uct WhereClause 
2e3d8 57 68 65 72 65 43 6c 61 75 73 65 3b 0a 74 79 70  WhereClause;.typ
2e3d9 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
2e3da 65 4d 61 73 6b 53 65 74 20 57 68 65 72 65 4d 61  eMaskSet WhereMa
2e3db 73 6b 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73  skSet;.typedef s
2e3dc 74 72 75 63 74 20 57 68 65 72 65 4f 72 49 6e 66  truct WhereOrInf
2e3dd 6f 20 57 68 65 72 65 4f 72 49 6e 66 6f 3b 0a 74  o WhereOrInfo;.t
2e3de 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68  ypedef struct Wh
2e3df 65 72 65 41 6e 64 49 6e 66 6f 20 57 68 65 72 65  ereAndInfo Where
2e3e0 41 6e 64 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  AndInfo;.typedef
2e3e1 20 73 74 72 75 63 74 20 57 68 65 72 65 43 6f 73   struct WhereCos
2e3e2 74 20 57 68 65 72 65 43 6f 73 74 3b 0a 0a 2f 2a  t WhereCost;../*
2e3e3 0a 2a 2a 20 54 68 65 20 71 75 65 72 79 20 67 65  .** The query ge
2e3e4 6e 65 72 61 74 6f 72 20 75 73 65 73 20 61 6e 20  nerator uses an 
2e3e5 61 72 72 61 79 20 6f 66 20 69 6e 73 74 61 6e 63  array of instanc
2e3e6 65 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  es of this struc
2e3e7 74 75 72 65 20 74 6f 0a 2a 2a 20 68 65 6c 70 20  ture to.** help 
2e3e8 69 74 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 73  it analyze the s
2e3e9 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66  ubexpressions of
2e3ea 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2e3eb 65 2e 20 20 45 61 63 68 20 57 48 45 52 45 0a 2a  e.  Each WHERE.*
2e3ec 2a 20 63 6c 61 75 73 65 20 73 75 62 65 78 70 72  * clause subexpr
2e3ed 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61  ession is separa
2e3ee 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 74 68  ted from the oth
2e3ef 65 72 73 20 62 79 20 41 4e 44 20 6f 70 65 72 61  ers by AND opera
2e3f0 74 6f 72 73 2c 0a 2a 2a 20 75 73 75 61 6c 6c 79  tors,.** usually
2e3f1 2c 20 6f 72 20 73 6f 6d 65 74 69 6d 65 73 20 73  , or sometimes s
2e3f2 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 73 65  ubexpressions se
2e3f3 70 61 72 61 74 65 64 20 62 79 20 4f 52 2e 0a 2a  parated by OR..*
2e3f4 2a 0a 2a 2a 20 41 6c 6c 20 57 68 65 72 65 54 65  *.** All WhereTe
2e3f5 72 6d 73 20 61 72 65 20 63 6f 6c 6c 65 63 74 65  rms are collecte
2e3f6 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  d into a single 
2e3f7 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
2e3f8 63 74 75 72 65 2e 20 20 0a 2a 2a 20 54 68 65 20  cture.  .** The 
2e3f9 66 6f 6c 6c 6f 77 69 6e 67 20 69 64 65 6e 74 69  following identi
2e3fa 74 79 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20  ty holds:.**.** 
2e3fb 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d         WhereTerm
2e3fc 2e 70 57 43 2d 3e 61 5b 57 68 65 72 65 54 65 72  .pWC->a[WhereTer
2e3fd 6d 2e 69 64 78 5d 20 3d 3d 20 57 68 65 72 65 54  m.idx] == WhereT
2e3fe 65 72 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  erm.**.** When a
2e3ff 20 74 65 72 6d 20 69 73 20 6f 66 20 74 68 65 20   term is of the 
2e400 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  form:.**.**     
2e401 20 20 20 20 20 20 20 20 20 58 20 3c 6f 70 3e 20           X <op> 
2e402 3c 65 78 70 72 3e 0a 2a 2a 0a 2a 2a 20 77 68 65  <expr>.**.** whe
2e403 72 65 20 58 20 69 73 20 61 20 63 6f 6c 75 6d 6e  re X is a column
2e404 20 6e 61 6d 65 20 61 6e 64 20 3c 6f 70 3e 20 69   name and <op> i
2e405 73 20 6f 6e 65 20 6f 66 20 63 65 72 74 61 69 6e  s one of certain
2e406 20 6f 70 65 72 61 74 6f 72 73 2c 0a 2a 2a 20 74   operators,.** t
2e407 68 65 6e 20 57 68 65 72 65 54 65 72 6d 2e 6c 65  hen WhereTerm.le
2e408 66 74 43 75 72 73 6f 72 20 61 6e 64 20 57 68 65  ftCursor and Whe
2e409 72 65 54 65 72 6d 2e 75 2e 6c 65 66 74 43 6f 6c  reTerm.u.leftCol
2e40a 75 6d 6e 20 72 65 63 6f 72 64 20 74 68 65 0a 2a  umn record the.*
2e40b 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * cursor number 
2e40c 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  and column numbe
2e40d 72 20 66 6f 72 20 58 2e 20 20 57 68 65 72 65 54  r for X.  WhereT
2e40e 65 72 6d 2e 65 4f 70 65 72 61 74 6f 72 20 72 65  erm.eOperator re
2e40f 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20 3c 6f 70  cords.** the <op
2e410 3e 20 75 73 69 6e 67 20 61 20 62 69 74 6d 61 73  > using a bitmas
2e411 6b 20 65 6e 63 6f 64 69 6e 67 20 64 65 66 69 6e  k encoding defin
2e412 65 64 20 62 79 20 57 4f 5f 78 78 78 20 62 65 6c  ed by WO_xxx bel
2e413 6f 77 2e 20 20 54 68 65 0a 2a 2a 20 75 73 65 20  ow.  The.** use 
2e414 6f 66 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63  of a bitmask enc
2e415 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 6f 70  oding for the op
2e416 65 72 61 74 6f 72 20 61 6c 6c 6f 77 73 20 75 73  erator allows us
2e417 20 74 6f 20 73 65 61 72 63 68 0a 2a 2a 20 71 75   to search.** qu
2e418 69 63 6b 6c 79 20 66 6f 72 20 74 65 72 6d 73 20  ickly for terms 
2e419 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20 6f  that match any o
2e41a 66 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 72  f several differ
2e41b 65 6e 74 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  ent operators..*
2e41c 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d  *.** A WhereTerm
2e41d 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 74   might also be t
2e41e 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 62 74 65  wo or more subte
2e41f 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  rms connected by
2e420 20 4f 52 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   OR:.**.**      
2e421 20 20 20 28 74 31 2e 58 20 3c 6f 70 3e 20 3c 65     (t1.X <op> <e
2e422 78 70 72 3e 29 20 4f 52 20 28 74 31 2e 59 20 3c  xpr>) OR (t1.Y <
2e423 6f 70 3e 20 3c 65 78 70 72 3e 29 20 4f 52 20 2e  op> <expr>) OR .
2e424 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69  ....**.** In thi
2e425 73 20 73 65 63 6f 6e 64 20 63 61 73 65 2c 20 77  s second case, w
2e426 74 46 6c 61 67 20 61 73 20 74 68 65 20 54 45 52  tFlag as the TER
2e427 4d 5f 4f 52 49 4e 46 4f 20 73 65 74 20 61 6e 64  M_ORINFO set and
2e428 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f   eOperator==WO_O
2e429 52 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65  R.** and the Whe
2e42a 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f  reTerm.u.pOrInfo
2e42b 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f   field points to
2e42c 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72   auxiliary infor
2e42d 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 69  mation that.** i
2e42e 73 20 63 6f 6c 6c 65 63 74 65 64 20 61 62 6f 75  s collected abou
2e42f 74 20 74 68 65 0a 2a 2a 0a 2a 2a 20 49 66 20 61  t the.**.** If a
2e430 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45   term in the WHE
2e431 52 45 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e  RE clause does n
2e432 6f 74 20 6d 61 74 63 68 20 65 69 74 68 65 72 20  ot match either 
2e433 6f 66 20 74 68 65 20 74 77 6f 20 70 72 65 76 69  of the two previ
2e434 6f 75 73 0a 2a 2a 20 63 61 74 65 67 6f 72 69 65  ous.** categorie
2e435 73 2c 20 74 68 65 6e 20 65 4f 70 65 72 61 74 6f  s, then eOperato
2e436 72 3d 3d 30 2e 20 20 54 68 65 20 57 68 65 72 65  r==0.  The Where
2e437 54 65 72 6d 2e 70 45 78 70 72 20 66 69 65 6c 64  Term.pExpr field
2e438 20 69 73 20 73 74 69 6c 6c 20 73 65 74 0a 2a 2a   is still set.**
2e439 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   to the original
2e43a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 63   subexpression c
2e43b 6f 6e 74 65 6e 74 20 61 6e 64 20 77 74 46 6c 61  ontent and wtFla
2e43c 67 73 20 69 73 20 73 65 74 20 75 70 20 61 70 70  gs is set up app
2e43d 72 6f 70 72 69 61 74 65 6c 79 0a 2a 2a 20 62 75  ropriately.** bu
2e43e 74 20 6e 6f 20 6f 74 68 65 72 20 66 69 65 6c 64  t no other field
2e43f 73 20 69 6e 20 74 68 65 20 57 68 65 72 65 54 65  s in the WhereTe
2e440 72 6d 20 6f 62 6a 65 63 74 20 61 72 65 20 6d 65  rm object are me
2e441 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20  aningful..**.** 
2e442 57 68 65 6e 20 65 4f 70 65 72 61 74 6f 72 21 3d  When eOperator!=
2e443 30 2c 20 70 72 65 72 65 71 52 69 67 68 74 20 61  0, prereqRight a
2e444 6e 64 20 70 72 65 72 65 71 41 6c 6c 20 72 65 63  nd prereqAll rec
2e445 6f 72 64 20 73 65 74 73 20 6f 66 20 63 75 72 73  ord sets of curs
2e446 6f 72 20 6e 75 6d 62 65 72 73 2c 0a 2a 2a 20 62  or numbers,.** b
2e447 75 74 20 74 68 65 79 20 64 6f 20 73 6f 20 69 6e  ut they do so in
2e448 64 69 72 65 63 74 6c 79 2e 20 20 41 20 73 69 6e  directly.  A sin
2e449 67 6c 65 20 57 68 65 72 65 4d 61 73 6b 53 65 74  gle WhereMaskSet
2e44a 20 73 74 72 75 63 74 75 72 65 20 74 72 61 6e 73   structure trans
2e44b 6c 61 74 65 73 0a 2a 2a 20 63 75 72 73 6f 72 20  lates.** cursor 
2e44c 6e 75 6d 62 65 72 20 69 6e 74 6f 20 62 69 74 73  number into bits
2e44d 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73 6c 61   and the transla
2e44e 74 65 64 20 62 69 74 20 69 73 20 73 74 6f 72 65  ted bit is store
2e44f 64 20 69 6e 20 74 68 65 20 70 72 65 72 65 71 0a  d in the prereq.
2e450 2a 2a 20 66 69 65 6c 64 73 2e 20 20 54 68 65 20  ** fields.  The 
2e451 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 75  translation is u
2e452 73 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  sed in order to 
2e453 6d 61 78 69 6d 69 7a 65 20 74 68 65 20 6e 75 6d  maximize the num
2e454 62 65 72 20 6f 66 0a 2a 2a 20 62 69 74 73 20 74  ber of.** bits t
2e455 68 61 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20  hat will fit in 
2e456 61 20 42 69 74 6d 61 73 6b 2e 20 20 54 68 65 20  a Bitmask.  The 
2e457 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
2e458 65 72 73 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20  ers might be.** 
2e459 73 70 72 65 61 64 20 6f 75 74 20 6f 76 65 72 20  spread out over 
2e45a 74 68 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65  the non-negative
2e45b 20 69 6e 74 65 67 65 72 73 2e 20 20 46 6f 72 20   integers.  For 
2e45c 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 63 75 72  example, the cur
2e45d 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 6d  sor.** numbers m
2e45e 69 67 68 74 20 62 65 20 33 2c 20 38 2c 20 39 2c  ight be 3, 8, 9,
2e45f 20 31 30 2c 20 32 30 2c 20 32 33 2c 20 34 31 2c   10, 20, 23, 41,
2e460 20 61 6e 64 20 34 35 2e 20 20 54 68 65 20 57 68   and 45.  The Wh
2e461 65 72 65 4d 61 73 6b 53 65 74 0a 2a 2a 20 74 72  ereMaskSet.** tr
2e462 61 6e 73 6c 61 74 65 73 20 74 68 65 73 65 20 73  anslates these s
2e463 70 61 72 73 65 20 63 75 72 73 6f 72 20 6e 75 6d  parse cursor num
2e464 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65 63  bers into consec
2e465 75 74 69 76 65 20 69 6e 74 65 67 65 72 73 0a 2a  utive integers.*
2e466 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  * beginning with
2e467 20 30 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d   0 in order to m
2e468 61 6b 65 20 74 68 65 20 62 65 73 74 20 70 6f 73  ake the best pos
2e469 73 69 62 6c 65 20 75 73 65 20 6f 66 20 74 68 65  sible use of the
2e46a 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 62 69   available.** bi
2e46b 74 73 20 69 6e 20 74 68 65 20 42 69 74 6d 61 73  ts in the Bitmas
2e46c 6b 2e 20 20 53 6f 2c 20 69 6e 20 74 68 65 20 65  k.  So, in the e
2e46d 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74 68  xample above, th
2e46e 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
2e46f 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 61 70  .** would be map
2e470 70 65 64 20 69 6e 74 6f 20 69 6e 74 65 67 65 72  ped into integer
2e471 73 20 30 20 74 68 72 6f 75 67 68 20 37 2e 0a 2a  s 0 through 7..*
2e472 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20  *.** The number 
2e473 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 6a 6f  of terms in a jo
2e474 69 6e 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79  in is limited by
2e475 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
2e476 69 74 73 0a 2a 2a 20 69 6e 20 70 72 65 72 65 71  its.** in prereq
2e477 52 69 67 68 74 20 61 6e 64 20 70 72 65 72 65 71  Right and prereq
2e478 41 6c 6c 2e 20 20 54 68 65 20 64 65 66 61 75 6c  All.  The defaul
2e479 74 20 69 73 20 36 34 20 62 69 74 73 2c 20 68 65  t is 64 bits, he
2e47a 6e 63 65 20 53 51 4c 69 74 65 0a 2a 2a 20 69 73  nce SQLite.** is
2e47b 20 6f 6e 6c 79 20 61 62 6c 65 20 74 6f 20 70 72   only able to pr
2e47c 6f 63 65 73 73 20 6a 6f 69 6e 73 20 77 69 74 68  ocess joins with
2e47d 20 36 34 20 6f 72 20 66 65 77 65 72 20 74 61 62   64 or fewer tab
2e47e 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  les..*/.typedef 
2e47f 73 74 72 75 63 74 20 57 68 65 72 65 54 65 72 6d  struct WhereTerm
2e480 20 57 68 65 72 65 54 65 72 6d 3b 0a 73 74 72 75   WhereTerm;.stru
2e481 63 74 20 57 68 65 72 65 54 65 72 6d 20 7b 0a 20  ct WhereTerm {. 
2e482 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20   Expr *pExpr;   
2e483 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
2e484 74 65 72 20 74 6f 20 74 68 65 20 73 75 62 65 78  ter to the subex
2e485 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73  pression that is
2e486 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20   this term */.  
2e487 69 6e 74 20 69 50 61 72 65 6e 74 3b 20 20 20 20  int iParent;    
2e488 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62          /* Disab
2e489 6c 65 20 70 57 43 2d 3e 61 5b 69 50 61 72 65 6e  le pWC->a[iParen
2e48a 74 5d 20 77 68 65 6e 20 74 68 69 73 20 74 65 72  t] when this ter
2e48b 6d 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20  m disabled */.  
2e48c 69 6e 74 20 6c 65 66 74 43 75 72 73 6f 72 3b 20  int leftCursor; 
2e48d 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
2e48e 72 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 69 6e  r number of X in
2e48f 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22   "X <op> <expr>"
2e490 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20   */.  union {.  
2e491 20 20 69 6e 74 20 6c 65 66 74 43 6f 6c 75 6d 6e    int leftColumn
2e492 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ;         /* Col
2e493 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 20 58 20  umn number of X 
2e494 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72  in "X <op> <expr
2e495 3e 22 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 4f  >" */.    WhereO
2e496 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20  rInfo *pOrInfo; 
2e497 20 20 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f 72    /* Extra infor
2e498 6d 61 74 69 6f 6e 20 69 66 20 65 4f 70 65 72 61  mation if eOpera
2e499 74 6f 72 3d 3d 57 4f 5f 4f 52 20 2a 2f 0a 20 20  tor==WO_OR */.  
2e49a 20 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a    WhereAndInfo *
2e49b 70 41 6e 64 49 6e 66 6f 3b 20 2f 2a 20 45 78 74  pAndInfo; /* Ext
2e49c 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ra information i
2e49d 66 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  f eOperator==WO_
2e49e 41 4e 44 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20  AND */.  } u;.  
2e49f 75 31 36 20 65 4f 70 65 72 61 74 6f 72 3b 20 20  u16 eOperator;  
2e4a0 20 20 20 20 20 20 20 20 2f 2a 20 41 20 57 4f 5f          /* A WO_
2e4a1 78 78 20 76 61 6c 75 65 20 64 65 73 63 72 69 62  xx value describ
2e4a2 69 6e 67 20 3c 6f 70 3e 20 2a 2f 0a 20 20 75 38  ing <op> */.  u8
2e4a3 20 77 74 46 6c 61 67 73 3b 20 20 20 20 20 20 20   wtFlags;       
2e4a4 20 20 20 20 20 20 2f 2a 20 54 45 52 4d 5f 78 78        /* TERM_xx
2e4a5 78 20 62 69 74 20 66 6c 61 67 73 2e 20 20 53 65  x bit flags.  Se
2e4a6 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 38 20  e below */.  u8 
2e4a7 6e 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20  nChild;         
2e4a8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2e4a9 66 20 63 68 69 6c 64 72 65 6e 20 74 68 61 74 20  f children that 
2e4aa 6d 75 73 74 20 64 69 73 61 62 6c 65 20 75 73 20  must disable us 
2e4ab 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
2e4ac 20 2a 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20   *pWC;       /* 
2e4ad 54 68 65 20 63 6c 61 75 73 65 20 74 68 69 73 20  The clause this 
2e4ae 74 65 72 6d 20 69 73 20 70 61 72 74 20 6f 66 20  term is part of 
2e4af 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65  */.  Bitmask pre
2e4b0 72 65 71 52 69 67 68 74 3b 20 20 20 20 2f 2a 20  reqRight;    /* 
2e4b1 42 69 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c 65  Bitmask of table
2e4b2 73 20 75 73 65 64 20 62 79 20 70 45 78 70 72 2d  s used by pExpr-
2e4b3 3e 70 52 69 67 68 74 20 2a 2f 0a 20 20 42 69 74  >pRight */.  Bit
2e4b4 6d 61 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20  mask prereqAll; 
2e4b5 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20       /* Bitmask 
2e4b6 6f 66 20 74 61 62 6c 65 73 20 72 65 66 65 72 65  of tables refere
2e4b7 6e 63 65 64 20 62 79 20 70 45 78 70 72 20 2a 2f  nced by pExpr */
2e4b8 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77  .};../*.** Allow
2e4b9 65 64 20 76 61 6c 75 65 73 20 6f 66 20 57 68 65  ed values of Whe
2e4ba 72 65 54 65 72 6d 2e 77 74 46 6c 61 67 73 0a 2a  reTerm.wtFlags.*
2e4bb 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 44  /.#define TERM_D
2e4bc 59 4e 41 4d 49 43 20 20 20 20 30 78 30 31 20 20  YNAMIC    0x01  
2e4bd 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c   /* Need to call
2e4be 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2e4bf 74 65 28 64 62 2c 20 70 45 78 70 72 29 20 2a 2f  te(db, pExpr) */
2e4c0 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 56 49  .#define TERM_VI
2e4c1 52 54 55 41 4c 20 20 20 20 30 78 30 32 20 20 20  RTUAL    0x02   
2e4c2 2f 2a 20 41 64 64 65 64 20 62 79 20 74 68 65 20  /* Added by the 
2e4c3 6f 70 74 69 6d 69 7a 65 72 2e 20 20 44 6f 20 6e  optimizer.  Do n
2e4c4 6f 74 20 63 6f 64 65 20 2a 2f 0a 23 64 65 66 69  ot code */.#defi
2e4c5 6e 65 20 54 45 52 4d 5f 43 4f 44 45 44 20 20 20  ne TERM_CODED   
2e4c6 20 20 20 30 78 30 34 20 20 20 2f 2a 20 54 68 69     0x04   /* Thi
2e4c7 73 20 74 65 72 6d 20 69 73 20 61 6c 72 65 61 64  s term is alread
2e4c8 79 20 63 6f 64 65 64 20 2a 2f 0a 23 64 65 66 69  y coded */.#defi
2e4c9 6e 65 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 20  ne TERM_COPIED  
2e4ca 20 20 20 30 78 30 38 20 20 20 2f 2a 20 48 61 73     0x08   /* Has
2e4cb 20 61 20 63 68 69 6c 64 20 2a 2f 0a 23 64 65 66   a child */.#def
2e4cc 69 6e 65 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20  ine TERM_ORINFO 
2e4cd 20 20 20 20 30 78 31 30 20 20 20 2f 2a 20 4e 65      0x10   /* Ne
2e4ce 65 64 20 74 6f 20 66 72 65 65 20 74 68 65 20 57  ed to free the W
2e4cf 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e  hereTerm.u.pOrIn
2e4d0 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65  fo object */.#de
2e4d1 66 69 6e 65 20 54 45 52 4d 5f 41 4e 44 49 4e 46  fine TERM_ANDINF
2e4d2 4f 20 20 20 20 30 78 32 30 20 20 20 2f 2a 20 4e  O    0x20   /* N
2e4d3 65 65 64 20 74 6f 20 66 72 65 65 20 74 68 65 20  eed to free the 
2e4d4 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 41 6e 64  WhereTerm.u.pAnd
2e4d5 49 6e 66 6f 20 6f 62 6a 20 2a 2f 0a 23 64 65 66  Info obj */.#def
2e4d6 69 6e 65 20 54 45 52 4d 5f 4f 52 5f 4f 4b 20 20  ine TERM_OR_OK  
2e4d7 20 20 20 20 30 78 34 30 20 20 20 2f 2a 20 55 73      0x40   /* Us
2e4d8 65 64 20 64 75 72 69 6e 67 20 4f 52 2d 63 6c 61  ed during OR-cla
2e4d9 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 2a  use processing *
2e4da 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  /../*.** An inst
2e4db 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
2e4dc 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
2e4dd 68 6f 6c 64 73 20 61 6c 6c 20 69 6e 66 6f 72 6d  holds all inform
2e4de 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a  ation about a.**
2e4df 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
2e4e0 4d 6f 73 74 6c 79 20 74 68 69 73 20 69 73 20 61  Mostly this is a
2e4e1 20 63 6f 6e 74 61 69 6e 65 72 20 66 6f 72 20 6f   container for o
2e4e2 6e 65 20 6f 72 20 6d 6f 72 65 20 57 68 65 72 65  ne or more Where
2e4e3 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  Terms..*/.struct
2e4e4 20 57 68 65 72 65 43 6c 61 75 73 65 20 7b 0a 20   WhereClause {. 
2e4e5 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
2e4e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2e4e7 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
2e4e8 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65  */.  WhereMaskSe
2e4e9 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20 2f 2a  t *pMaskSet;  /*
2e4ea 20 4d 61 70 70 69 6e 67 20 6f 66 20 74 61 62 6c   Mapping of tabl
2e4eb 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
2e4ec 20 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a   to bitmasks */.
2e4ed 20 20 42 69 74 6d 61 73 6b 20 76 6d 61 73 6b 3b    Bitmask vmask;
2e4ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69             /* Bi
2e4ef 74 6d 61 73 6b 20 69 64 65 6e 74 69 66 79 69 6e  tmask identifyin
2e4f0 67 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  g virtual table 
2e4f1 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 75 38 20  cursors */.  u8 
2e4f2 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
2e4f3 20 20 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 6f        /* Split o
2e4f4 70 65 72 61 74 6f 72 2e 20 20 54 4b 5f 41 4e 44  perator.  TK_AND
2e4f5 20 6f 72 20 54 4b 5f 4f 52 20 2a 2f 0a 20 20 69   or TK_OR */.  i
2e4f6 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
2e4f7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2e4f8 72 20 6f 66 20 74 65 72 6d 73 20 2a 2f 0a 20 20  r of terms */.  
2e4f9 69 6e 74 20 6e 53 6c 6f 74 3b 20 20 20 20 20 20  int nSlot;      
2e4fa 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2e4fb 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
2e4fc 20 61 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 65 54   a[] */.  WhereT
2e4fd 65 72 6d 20 2a 61 3b 20 20 20 20 20 20 20 20 20  erm *a;         
2e4fe 20 20 20 2f 2a 20 45 61 63 68 20 61 5b 5d 20 64     /* Each a[] d
2e4ff 65 73 63 72 69 62 65 73 20 61 20 74 65 72 6d 20  escribes a term 
2e500 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 75  of the WHERE clu
2e501 61 73 65 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ase */.#if defin
2e502 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f  ed(SQLITE_SMALL_
2e503 53 54 41 43 4b 29 0a 20 20 57 68 65 72 65 54 65  STACK).  WhereTe
2e504 72 6d 20 61 53 74 61 74 69 63 5b 31 5d 3b 20 20  rm aStatic[1];  
2e505 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 73 74 61    /* Initial sta
2e506 74 69 63 20 73 70 61 63 65 20 66 6f 72 20 61 5b  tic space for a[
2e507 5d 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 57 68 65  ] */.#else.  Whe
2e508 72 65 54 65 72 6d 20 61 53 74 61 74 69 63 5b 38  reTerm aStatic[8
2e509 5d 3b 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c  ];    /* Initial
2e50a 20 73 74 61 74 69 63 20 73 70 61 63 65 20 66 6f   static space fo
2e50b 72 20 61 5b 5d 20 2a 2f 0a 23 65 6e 64 69 66 0a  r a[] */.#endif.
2e50c 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72  };../*.** A Wher
2e50d 65 54 65 72 6d 20 77 69 74 68 20 65 4f 70 65 72  eTerm with eOper
2e50e 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 68 61 73 20  ator==WO_OR has 
2e50f 69 74 73 20 75 2e 70 4f 72 49 6e 66 6f 20 70 6f  its u.pOrInfo po
2e510 69 6e 74 65 72 20 73 65 74 20 74 6f 0a 2a 2a 20  inter set to.** 
2e511 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  a dynamically al
2e512 6c 6f 63 61 74 65 64 20 69 6e 73 74 61 6e 63 65  located instance
2e513 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2e514 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  g structure..*/.
2e515 73 74 72 75 63 74 20 57 68 65 72 65 4f 72 49 6e  struct WhereOrIn
2e516 66 6f 20 7b 0a 20 20 57 68 65 72 65 43 6c 61 75  fo {.  WhereClau
2e517 73 65 20 77 63 3b 20 20 20 20 20 20 20 20 20 20  se wc;          
2e518 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e  /* Decomposition
2e519 20 69 6e 74 6f 20 73 75 62 74 65 72 6d 73 20 2a   into subterms *
2e51a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 69 6e 64 65  /.  Bitmask inde
2e51b 78 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20  xable;       /* 
2e51c 42 69 74 6d 61 73 6b 20 6f 66 20 61 6c 6c 20 69  Bitmask of all i
2e51d 6e 64 65 78 61 62 6c 65 20 74 61 62 6c 65 73 20  ndexable tables 
2e51e 69 6e 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f  in the clause */
2e51f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65  .};../*.** A Whe
2e520 72 65 54 65 72 6d 20 77 69 74 68 20 65 4f 70 65  reTerm with eOpe
2e521 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 68 61  rator==WO_AND ha
2e522 73 20 69 74 73 20 75 2e 70 41 6e 64 49 6e 66 6f  s its u.pAndInfo
2e523 20 70 6f 69 6e 74 65 72 20 73 65 74 20 74 6f 0a   pointer set to.
2e524 2a 2a 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  ** a dynamically
2e525 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 73 74 61   allocated insta
2e526 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
2e527 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
2e528 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 41  */.struct WhereA
2e529 6e 64 49 6e 66 6f 20 7b 0a 20 20 57 68 65 72 65  ndInfo {.  Where
2e52a 43 6c 61 75 73 65 20 77 63 3b 20 20 20 20 20 20  Clause wc;      
2e52b 20 20 20 20 2f 2a 20 54 68 65 20 73 75 62 65 78      /* The subex
2e52c 70 72 65 73 73 69 6f 6e 20 62 72 6f 6b 65 6e 20  pression broken 
2e52d 6f 75 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  out */.};../*.**
2e52e 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
2e52f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2e530 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
2e531 61 63 6b 20 6f 66 20 61 20 6d 61 70 70 69 6e 67  ack of a mapping
2e532 0a 2a 2a 20 62 65 74 77 65 65 6e 20 56 44 42 45  .** between VDBE
2e533 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2e534 61 6e 64 20 62 69 74 73 20 6f 66 20 74 68 65 20  and bits of the 
2e535 62 69 74 6d 61 73 6b 73 20 69 6e 20 57 68 65 72  bitmasks in Wher
2e536 65 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  eTerm..**.** The
2e537 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
2e538 62 65 72 73 20 61 72 65 20 73 6d 61 6c 6c 20 69  bers are small i
2e539 6e 74 65 67 65 72 73 20 63 6f 6e 74 61 69 6e 65  ntegers containe
2e53a 64 20 69 6e 20 0a 2a 2a 20 53 72 63 4c 69 73 74  d in .** SrcList
2e53b 5f 69 74 65 6d 2e 69 43 75 72 73 6f 72 20 61 6e  _item.iCursor an
2e53c 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20 66 69  d Expr.iTable fi
2e53d 65 6c 64 73 2e 20 20 46 6f 72 20 61 6e 79 20 67  elds.  For any g
2e53e 69 76 65 6e 20 57 48 45 52 45 20 0a 2a 2a 20 63  iven WHERE .** c
2e53f 6c 61 75 73 65 2c 20 74 68 65 20 63 75 72 73 6f  lause, the curso
2e540 72 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20  r numbers might 
2e541 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 30  not begin with 0
2e542 20 61 6e 64 20 74 68 65 79 20 6d 69 67 68 74 0a   and they might.
2e543 2a 2a 20 63 6f 6e 74 61 69 6e 20 67 61 70 73 20  ** contain gaps 
2e544 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 69 6e 67  in the numbering
2e545 20 73 65 71 75 65 6e 63 65 2e 20 20 42 75 74 20   sequence.  But 
2e546 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20  we want to make 
2e547 6d 61 78 69 6d 75 6d 0a 2a 2a 20 75 73 65 20 6f  maximum.** use o
2e548 66 20 74 68 65 20 62 69 74 73 20 69 6e 20 6f 75  f the bits in ou
2e549 72 20 62 69 74 6d 61 73 6b 73 2e 20 20 54 68 69  r bitmasks.  Thi
2e54a 73 20 73 74 72 75 63 74 75 72 65 20 70 72 6f 76  s structure prov
2e54b 69 64 65 73 20 61 20 6d 61 70 70 69 6e 67 0a 2a  ides a mapping.*
2e54c 2a 20 66 72 6f 6d 20 74 68 65 20 73 70 61 72 73  * from the spars
2e54d 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
2e54e 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69 76   into consecutiv
2e54f 65 20 69 6e 74 65 67 65 72 73 20 62 65 67 69 6e  e integers begin
2e550 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 30 2e 0a  ning.** with 0..
2e551 2a 2a 0a 2a 2a 20 49 66 20 57 68 65 72 65 4d 61  **.** If WhereMa
2e552 73 6b 53 65 74 2e 69 78 5b 41 5d 3d 3d 42 20 69  skSet.ix[A]==B i
2e553 74 20 6d 65 61 6e 73 20 74 68 61 74 20 54 68 65  t means that The
2e554 20 41 2d 74 68 20 62 69 74 20 6f 66 20 61 20 42   A-th bit of a B
2e555 69 74 6d 61 73 6b 0a 2a 2a 20 63 6f 72 72 65 73  itmask.** corres
2e556 70 6f 6e 64 73 20 56 44 42 45 20 63 75 72 73 6f  ponds VDBE curso
2e557 72 20 6e 75 6d 62 65 72 20 42 2e 20 20 54 68 65  r number B.  The
2e558 20 41 2d 74 68 20 62 69 74 20 6f 66 20 61 20 62   A-th bit of a b
2e559 69 74 6d 61 73 6b 20 69 73 20 31 3c 3c 41 2e 0a  itmask is 1<<A..
2e55a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  **.** For exampl
2e55b 65 2c 20 69 66 20 74 68 65 20 57 48 45 52 45 20  e, if the WHERE 
2e55c 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 69 6f  clause expressio
2e55d 6e 20 75 73 65 64 20 74 68 65 73 65 20 56 44 42  n used these VDB
2e55e 45 0a 2a 2a 20 63 75 72 73 6f 72 73 3a 20 20 34  E.** cursors:  4
2e55f 2c 20 35 2c 20 38 2c 20 32 39 2c 20 35 37 2c 20  , 5, 8, 29, 57, 
2e560 37 33 2e 20 20 54 68 65 6e 20 74 68 65 20 20 57  73.  Then the  W
2e561 68 65 72 65 4d 61 73 6b 53 65 74 20 73 74 72 75  hereMaskSet stru
2e562 63 74 75 72 65 0a 2a 2a 20 77 6f 75 6c 64 20 6d  cture.** would m
2e563 61 70 20 74 68 6f 73 65 20 63 75 72 73 6f 72 20  ap those cursor 
2e564 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 62 69 74  numbers into bit
2e565 73 20 30 20 74 68 72 6f 75 67 68 20 35 2e 0a 2a  s 0 through 5..*
2e566 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
2e567 68 65 20 6d 61 70 70 69 6e 67 20 69 73 20 6e 6f  he mapping is no
2e568 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6f 72  t necessarily or
2e569 64 65 72 65 64 2e 20 20 49 6e 20 74 68 65 20 65  dered.  In the e
2e56a 78 61 6d 70 6c 65 0a 2a 2a 20 61 62 6f 76 65 2c  xample.** above,
2e56b 20 74 68 65 20 6d 61 70 70 69 6e 67 20 6d 69 67   the mapping mig
2e56c 68 74 20 67 6f 20 6c 69 6b 65 20 74 68 69 73 3a  ht go like this:
2e56d 20 20 34 2d 3e 33 2c 20 35 2d 3e 31 2c 20 38 2d    4->3, 5->1, 8-
2e56e 3e 32 2c 20 32 39 2d 3e 30 2c 0a 2a 2a 20 35 37  >2, 29->0,.** 57
2e56f 2d 3e 35 2c 20 37 33 2d 3e 34 2e 20 20 4f 72 20  ->5, 73->4.  Or 
2e570 6f 6e 65 20 6f 66 20 37 31 39 20 6f 74 68 65 72  one of 719 other
2e571 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6d 69   combinations mi
2e572 67 68 74 20 62 65 20 75 73 65 64 2e 20 49 74 0a  ght be used. It.
2e573 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c  ** does not real
2e574 6c 79 20 6d 61 74 74 65 72 2e 20 20 57 68 61 74  ly matter.  What
2e575 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 69 73   is important is
2e576 20 74 68 61 74 20 73 70 61 72 73 65 20 63 75 72   that sparse cur
2e577 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61  sor.** numbers a
2e578 6c 6c 20 67 65 74 20 6d 61 70 70 65 64 20 69 6e  ll get mapped in
2e579 74 6f 20 62 69 74 20 6e 75 6d 62 65 72 73 20 74  to bit numbers t
2e57a 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 30  hat begin with 0
2e57b 20 61 6e 64 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20   and contain.** 
2e57c 6e 6f 20 67 61 70 73 2e 0a 2a 2f 0a 73 74 72 75  no gaps..*/.stru
2e57d 63 74 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20  ct WhereMaskSet 
2e57e 7b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20  {.  int n;      
2e57f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e580 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
2e581 73 73 69 67 6e 65 64 20 63 75 72 73 6f 72 20 76  ssigned cursor v
2e582 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69  alues */.  int i
2e583 78 5b 42 4d 53 5d 3b 20 20 20 20 20 20 20 20 20  x[BMS];         
2e584 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
2e585 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 65  or assigned to e
2e586 61 63 68 20 62 69 74 20 2a 2f 0a 7d 3b 0a 0a 2f  ach bit */.};../
2e587 2a 0a 2a 2a 20 41 20 57 68 65 72 65 43 6f 73 74  *.** A WhereCost
2e588 20 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20   object records 
2e589 61 20 6c 6f 6f 6b 75 70 20 73 74 72 61 74 65 67  a lookup strateg
2e58a 79 20 61 6e 64 20 74 68 65 20 65 73 74 69 6d 61  y and the estima
2e58b 74 65 64 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 70  ted.** cost of p
2e58c 75 72 73 75 69 6e 67 20 74 68 61 74 20 73 74 72  ursuing that str
2e58d 61 74 65 67 79 2e 0a 2a 2f 0a 73 74 72 75 63 74  ategy..*/.struct
2e58e 20 57 68 65 72 65 43 6f 73 74 20 7b 0a 20 20 57   WhereCost {.  W
2e58f 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20  herePlan plan;  
2e590 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20    /* The lookup 
2e591 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 64 6f  strategy */.  do
2e592 75 62 6c 65 20 72 43 6f 73 74 3b 20 20 20 20 20  uble rCost;     
2e593 20 2f 2a 20 4f 76 65 72 61 6c 6c 20 63 6f 73 74   /* Overall cost
2e594 20 6f 66 20 70 75 72 73 75 69 6e 67 20 74 68 69   of pursuing thi
2e595 73 20 73 65 61 72 63 68 20 73 74 72 61 74 65 67  s search strateg
2e596 79 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 52  y */.  double nR
2e597 6f 77 3b 20 20 20 20 20 20 20 2f 2a 20 45 73 74  ow;       /* Est
2e598 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66  imated number of
2e599 20 6f 75 74 70 75 74 20 72 6f 77 73 20 2a 2f 0a   output rows */.
2e59a 20 20 42 69 74 6d 61 73 6b 20 75 73 65 64 3b 20    Bitmask used; 
2e59b 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20       /* Bitmask 
2e59c 6f 66 20 63 75 72 73 6f 72 73 20 75 73 65 64 20  of cursors used 
2e59d 62 79 20 74 68 69 73 20 70 6c 61 6e 20 2a 2f 0a  by this plan */.
2e59e 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 6d 61 73  };../*.** Bitmas
2e59f 6b 73 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61  ks for the opera
2e5a0 74 6f 72 73 20 74 68 61 74 20 69 6e 64 69 63 65  tors that indice
2e5a1 73 20 61 72 65 20 61 62 6c 65 20 74 6f 20 65 78  s are able to ex
2e5a2 70 6c 6f 69 74 2e 20 20 41 6e 0a 2a 2a 20 4f 52  ploit.  An.** OR
2e5a3 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  -ed combination 
2e5a4 6f 66 20 74 68 65 73 65 20 76 61 6c 75 65 73 20  of these values 
2e5a5 63 61 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e  can be used when
2e5a6 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 0a 2a   searching for.*
2e5a7 2a 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 77  * terms in the w
2e5a8 68 65 72 65 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a  here clause..*/.
2e5a9 23 64 65 66 69 6e 65 20 57 4f 5f 49 4e 20 20 20  #define WO_IN   
2e5aa 20 20 30 78 30 30 31 0a 23 64 65 66 69 6e 65 20    0x001.#define 
2e5ab 57 4f 5f 45 51 20 20 20 20 20 30 78 30 30 32 0a  WO_EQ     0x002.
2e5ac 23 64 65 66 69 6e 65 20 57 4f 5f 4c 54 20 20 20  #define WO_LT   
2e5ad 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 54    (WO_EQ<<(TK_LT
2e5ae 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65  -TK_EQ)).#define
2e5af 20 57 4f 5f 4c 45 20 20 20 20 20 28 57 4f 5f 45   WO_LE     (WO_E
2e5b0 51 3c 3c 28 54 4b 5f 4c 45 2d 54 4b 5f 45 51 29  Q<<(TK_LE-TK_EQ)
2e5b1 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47 54 20  ).#define WO_GT 
2e5b2 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f      (WO_EQ<<(TK_
2e5b3 47 54 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69  GT-TK_EQ)).#defi
2e5b4 6e 65 20 57 4f 5f 47 45 20 20 20 20 20 28 57 4f  ne WO_GE     (WO
2e5b5 5f 45 51 3c 3c 28 54 4b 5f 47 45 2d 54 4b 5f 45  _EQ<<(TK_GE-TK_E
2e5b6 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4d  Q)).#define WO_M
2e5b7 41 54 43 48 20 20 30 78 30 34 30 0a 23 64 65 66  ATCH  0x040.#def
2e5b8 69 6e 65 20 57 4f 5f 49 53 4e 55 4c 4c 20 30 78  ine WO_ISNULL 0x
2e5b9 30 38 30 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4f  080.#define WO_O
2e5ba 52 20 20 20 20 20 30 78 31 30 30 20 20 20 20 20  R     0x100     
2e5bb 20 20 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72 65    /* Two or more
2e5bc 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65   OR-connected te
2e5bd 72 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  rms */.#define W
2e5be 4f 5f 41 4e 44 20 20 20 20 30 78 32 30 30 20 20  O_AND    0x200  
2e5bf 20 20 20 20 20 2f 2a 20 54 77 6f 20 6f 72 20 6d       /* Two or m
2e5c0 6f 72 65 20 41 4e 44 2d 63 6f 6e 6e 65 63 74 65  ore AND-connecte
2e5c1 64 20 74 65 72 6d 73 20 2a 2f 0a 0a 23 64 65 66  d terms */..#def
2e5c2 69 6e 65 20 57 4f 5f 41 4c 4c 20 20 20 20 30 78  ine WO_ALL    0x
2e5c3 66 66 66 20 20 20 20 20 20 20 2f 2a 20 4d 61 73  fff       /* Mas
2e5c4 6b 20 6f 66 20 61 6c 6c 20 70 6f 73 73 69 62 6c  k of all possibl
2e5c5 65 20 57 4f 5f 2a 20 76 61 6c 75 65 73 20 2a 2f  e WO_* values */
2e5c6 0a 23 64 65 66 69 6e 65 20 57 4f 5f 53 49 4e 47  .#define WO_SING
2e5c7 4c 45 20 30 78 30 66 66 20 20 20 20 20 20 20 2f  LE 0x0ff       /
2e5c8 2a 20 4d 61 73 6b 20 6f 66 20 61 6c 6c 20 6e 6f  * Mask of all no
2e5c9 6e 2d 63 6f 6d 70 6f 75 6e 64 20 57 4f 5f 2a 20  n-compound WO_* 
2e5ca 76 61 6c 75 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  values */../*.**
2e5cb 20 56 61 6c 75 65 20 66 6f 72 20 77 73 46 6c 61   Value for wsFla
2e5cc 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 62  gs returned by b
2e5cd 65 73 74 49 6e 64 65 78 28 29 20 61 6e 64 20 73  estIndex() and s
2e5ce 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 57 68 65 72  tored in.** Wher
2e5cf 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 2e 20  eLevel.wsFlags. 
2e5d0 20 54 68 65 73 65 20 66 6c 61 67 73 20 64 65 74   These flags det
2e5d1 65 72 6d 69 6e 65 20 77 68 69 63 68 20 73 65 61  ermine which sea
2e5d2 72 63 68 0a 2a 2a 20 73 74 72 61 74 65 67 69 65  rch.** strategie
2e5d3 73 20 61 72 65 20 61 70 70 72 6f 70 72 69 61 74  s are appropriat
2e5d4 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 61  e..**.** The lea
2e5d5 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 31  st significant 1
2e5d6 32 20 62 69 74 73 20 69 73 20 72 65 73 65 72 76  2 bits is reserv
2e5d7 65 64 20 61 73 20 61 20 6d 61 73 6b 20 66 6f 72  ed as a mask for
2e5d8 20 57 4f 5f 20 76 61 6c 75 65 73 20 61 62 6f 76   WO_ values abov
2e5d9 65 2e 0a 2a 2a 20 54 68 65 20 57 68 65 72 65 4c  e..** The WhereL
2e5da 65 76 65 6c 2e 77 73 46 6c 61 67 73 20 66 69 65  evel.wsFlags fie
2e5db 6c 64 20 69 73 20 75 73 75 61 6c 6c 79 20 73 65  ld is usually se
2e5dc 74 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51  t to WO_IN|WO_EQ
2e5dd 7c 57 4f 5f 49 53 4e 55 4c 4c 2e 0a 2a 2a 20 42  |WO_ISNULL..** B
2e5de 75 74 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ut if the table 
2e5df 69 73 20 74 68 65 20 72 69 67 68 74 20 74 61 62  is the right tab
2e5e0 6c 65 20 6f 66 20 61 20 6c 65 66 74 20 6a 6f 69  le of a left joi
2e5e1 6e 2c 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73  n, WhereLevel.ws
2e5e2 46 6c 61 67 73 0a 2a 2a 20 69 73 20 73 65 74 20  Flags.** is set 
2e5e3 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 2e 20  to WO_IN|WO_EQ. 
2e5e4 20 54 68 65 20 57 68 65 72 65 4c 65 76 65 6c 2e   The WhereLevel.
2e5e5 77 73 46 6c 61 67 73 20 66 69 65 6c 64 20 63 61  wsFlags field ca
2e5e6 6e 20 74 68 65 6e 20 62 65 20 75 73 65 64 20 61  n then be used a
2e5e7 73 0a 2a 2a 20 74 68 65 20 22 6f 70 22 20 70 61  s.** the "op" pa
2e5e8 72 61 6d 65 74 65 72 20 74 6f 20 66 69 6e 64 54  rameter to findT
2e5e9 65 72 6d 20 77 68 65 6e 20 77 65 20 61 72 65 20  erm when we are 
2e5ea 72 65 73 6f 6c 76 69 6e 67 20 65 71 75 61 6c 69  resolving equali
2e5eb 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a  ty constraints..
2e5ec 2a 2a 20 49 53 4e 55 4c 4c 20 63 6f 6e 73 74 72  ** ISNULL constr
2e5ed 61 69 6e 74 73 20 77 69 6c 6c 20 74 68 65 6e 20  aints will then 
2e5ee 6e 6f 74 20 62 65 20 75 73 65 64 20 6f 6e 20 74  not be used on t
2e5ef 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
2e5f0 66 20 61 20 6c 65 66 74 0a 2a 2a 20 6a 6f 69 6e  f a left.** join
2e5f1 2e 20 20 54 69 63 6b 65 74 73 20 23 32 31 37 37  .  Tickets #2177
2e5f2 20 61 6e 64 20 23 32 31 38 39 2e 0a 2a 2f 0a 23   and #2189..*/.#
2e5f3 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 4f 57  define WHERE_ROW
2e5f4 49 44 5f 45 51 20 20 20 20 20 30 78 30 30 30 30  ID_EQ     0x0000
2e5f5 31 30 30 30 20 20 2f 2a 20 72 6f 77 69 64 3d 45  1000  /* rowid=E
2e5f6 58 50 52 20 6f 72 20 72 6f 77 69 64 20 49 4e 20  XPR or rowid IN 
2e5f7 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65  (...) */.#define
2e5f8 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e   WHERE_ROWID_RAN
2e5f9 47 45 20 20 30 78 30 30 30 30 32 30 30 30 20 20  GE  0x00002000  
2e5fa 2f 2a 20 72 6f 77 69 64 3c 45 58 50 52 20 61 6e  /* rowid<EXPR an
2e5fb 64 2f 6f 72 20 72 6f 77 69 64 3e 45 58 50 52 20  d/or rowid>EXPR 
2e5fc 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2e5fd 5f 43 4f 4c 55 4d 4e 5f 45 51 20 20 20 20 30 78  _COLUMN_EQ    0x
2e5fe 30 30 30 31 30 30 30 30 20 20 2f 2a 20 78 3d 45  00010000  /* x=E
2e5ff 58 50 52 20 6f 72 20 78 20 49 4e 20 28 2e 2e 2e  XPR or x IN (...
2e600 29 20 6f 72 20 78 20 49 53 20 4e 55 4c 4c 20 2a  ) or x IS NULL *
2e601 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e602 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 20 30 78 30  COLUMN_RANGE 0x0
2e603 30 30 32 30 30 30 30 20 20 2f 2a 20 78 3c 45 58  0020000  /* x<EX
2e604 50 52 20 61 6e 64 2f 6f 72 20 78 3e 45 58 50 52  PR and/or x>EXPR
2e605 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
2e606 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 20 20 20 30  E_COLUMN_IN    0
2e607 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 78 20  x00040000  /* x 
2e608 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66  IN (...) */.#def
2e609 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  ine WHERE_COLUMN
2e60a 5f 4e 55 4c 4c 20 20 30 78 30 30 30 38 30 30 30  _NULL  0x0008000
2e60b 30 20 20 2f 2a 20 78 20 49 53 20 4e 55 4c 4c 20  0  /* x IS NULL 
2e60c 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2e60d 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20 30 78  _INDEXED      0x
2e60e 30 30 30 66 30 30 30 30 20 20 2f 2a 20 41 6e 79  000f0000  /* Any
2e60f 74 68 69 6e 67 20 74 68 61 74 20 75 73 65 73 20  thing that uses 
2e610 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66  an index */.#def
2e611 69 6e 65 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c  ine WHERE_IN_ABL
2e612 45 20 20 20 20 20 20 30 78 30 30 30 66 31 30 30  E      0x000f100
2e613 30 20 20 2f 2a 20 41 62 6c 65 20 74 6f 20 73 75  0  /* Able to su
2e614 70 70 6f 72 74 20 61 6e 20 49 4e 20 6f 70 65 72  pport an IN oper
2e615 61 74 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ator */.#define 
2e616 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20  WHERE_TOP_LIMIT 
2e617 20 20 20 30 78 30 30 31 30 30 30 30 30 20 20 2f     0x00100000  /
2e618 2a 20 78 3c 45 58 50 52 20 6f 72 20 78 3c 3d 45  * x<EXPR or x<=E
2e619 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a  XPR constraint *
2e61a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e61b 42 54 4d 5f 4c 49 4d 49 54 20 20 20 20 30 78 30  BTM_LIMIT    0x0
2e61c 30 32 30 30 30 30 30 20 20 2f 2a 20 78 3e 45 58  0200000  /* x>EX
2e61d 50 52 20 6f 72 20 78 3e 3d 45 58 50 52 20 63 6f  PR or x>=EXPR co
2e61e 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66  nstraint */.#def
2e61f 69 6e 65 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  ine WHERE_IDX_ON
2e620 4c 59 20 20 20 20 20 30 78 30 30 38 30 30 30 30  LY     0x0080000
2e621 30 20 20 2f 2a 20 55 73 65 20 69 6e 64 65 78 20  0  /* Use index 
2e622 6f 6e 6c 79 20 2d 20 6f 6d 69 74 20 74 61 62 6c  only - omit tabl
2e623 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  e */.#define WHE
2e624 52 45 5f 4f 52 44 45 52 42 59 20 20 20 20 20 20  RE_ORDERBY      
2e625 30 78 30 31 30 30 30 30 30 30 20 20 2f 2a 20 4f  0x01000000  /* O
2e626 75 74 70 75 74 20 77 69 6c 6c 20 61 70 70 65 61  utput will appea
2e627 72 20 69 6e 20 63 6f 72 72 65 63 74 20 6f 72 64  r in correct ord
2e628 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  er */.#define WH
2e629 45 52 45 5f 52 45 56 45 52 53 45 20 20 20 20 20  ERE_REVERSE     
2e62a 20 30 78 30 32 30 30 30 30 30 30 20 20 2f 2a 20   0x02000000  /* 
2e62b 53 63 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20  Scan in reverse 
2e62c 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  order */.#define
2e62d 20 57 48 45 52 45 5f 55 4e 49 51 55 45 20 20 20   WHERE_UNIQUE   
2e62e 20 20 20 20 30 78 30 34 30 30 30 30 30 30 20 20      0x04000000  
2e62f 2f 2a 20 53 65 6c 65 63 74 73 20 6e 6f 20 6d 6f  /* Selects no mo
2e630 72 65 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20  re than one row 
2e631 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2e632 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 30 78  _VIRTUALTABLE 0x
2e633 30 38 30 30 30 30 30 30 20 20 2f 2a 20 55 73 65  08000000  /* Use
2e634 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 70   virtual-table p
2e635 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 23 64 65  rocessing */.#de
2e636 66 69 6e 65 20 57 48 45 52 45 5f 4d 55 4c 54 49  fine WHERE_MULTI
2e637 5f 4f 52 20 20 20 20 20 30 78 31 30 30 30 30 30  _OR     0x100000
2e638 30 30 20 20 2f 2a 20 4f 52 20 75 73 69 6e 67 20  00  /* OR using 
2e639 6d 75 6c 74 69 70 6c 65 20 69 6e 64 69 63 65 73  multiple indices
2e63a 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69   */../*.** Initi
2e63b 61 6c 69 7a 65 20 61 20 70 72 65 61 6c 6c 6f 63  alize a prealloc
2e63c 61 74 65 64 20 57 68 65 72 65 43 6c 61 75 73 65  ated WhereClause
2e63d 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
2e63e 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65  tatic void where
2e63f 43 6c 61 75 73 65 49 6e 69 74 28 0a 20 20 57 68  ClauseInit(.  Wh
2e640 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2e641 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 68         /* The Wh
2e642 65 72 65 43 6c 61 75 73 65 20 74 6f 20 62 65 20  ereClause to be 
2e643 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
2e644 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2e645 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2e646 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
2e647 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53   */.  WhereMaskS
2e648 65 74 20 2a 70 4d 61 73 6b 53 65 74 20 20 20 2f  et *pMaskSet   /
2e649 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74  * Mapping from t
2e64a 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  able cursor numb
2e64b 65 72 73 20 74 6f 20 62 69 74 6d 61 73 6b 73 20  ers to bitmasks 
2e64c 2a 2f 0a 29 7b 0a 20 20 70 57 43 2d 3e 70 50 61  */.){.  pWC->pPa
2e64d 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
2e64e 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 20 3d 20  pWC->pMaskSet = 
2e64f 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70 57 43 2d  pMaskSet;.  pWC-
2e650 3e 6e 54 65 72 6d 20 3d 20 30 3b 0a 20 20 70 57  >nTerm = 0;.  pW
2e651 43 2d 3e 6e 53 6c 6f 74 20 3d 20 41 72 72 61 79  C->nSlot = Array
2e652 53 69 7a 65 28 70 57 43 2d 3e 61 53 74 61 74 69  Size(pWC->aStati
2e653 63 29 3b 0a 20 20 70 57 43 2d 3e 61 20 3d 20 70  c);.  pWC->a = p
2e654 57 43 2d 3e 61 53 74 61 74 69 63 3b 0a 20 20 70  WC->aStatic;.  p
2e655 57 43 2d 3e 76 6d 61 73 6b 20 3d 20 30 3b 0a 7d  WC->vmask = 0;.}
2e656 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66  ../* Forward ref
2e657 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63  erence */.static
2e658 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73   void whereClaus
2e659 65 43 6c 65 61 72 28 57 68 65 72 65 43 6c 61 75  eClear(WhereClau
2e65a 73 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 61  se*);../*.** Dea
2e65b 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 6d 65 6d 6f  llocate all memo
2e65c 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
2e65d 74 68 20 61 20 57 68 65 72 65 4f 72 49 6e 66 6f  th a WhereOrInfo
2e65e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74   object..*/.stat
2e65f 69 63 20 76 6f 69 64 20 77 68 65 72 65 4f 72 49  ic void whereOrI
2e660 6e 66 6f 44 65 6c 65 74 65 28 73 71 6c 69 74 65  nfoDelete(sqlite
2e661 33 20 2a 64 62 2c 20 57 68 65 72 65 4f 72 49 6e  3 *db, WhereOrIn
2e662 66 6f 20 2a 70 29 7b 0a 20 20 77 68 65 72 65 43  fo *p){.  whereC
2e663 6c 61 75 73 65 43 6c 65 61 72 28 26 70 2d 3e 77  lauseClear(&p->w
2e664 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  c);.  sqlite3DbF
2e665 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f  ree(db, p);.}../
2e666 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20  *.** Deallocate 
2e667 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63  all memory assoc
2e668 69 61 74 65 64 20 77 69 74 68 20 61 20 57 68 65  iated with a Whe
2e669 72 65 41 6e 64 49 6e 66 6f 20 6f 62 6a 65 63 74  reAndInfo object
2e66a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e66b 20 77 68 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c   whereAndInfoDel
2e66c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ete(sqlite3 *db,
2e66d 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70   WhereAndInfo *p
2e66e 29 7b 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65  ){.  whereClause
2e66f 43 6c 65 61 72 28 26 70 2d 3e 77 63 29 3b 0a 20  Clear(&p->wc);. 
2e670 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2e671 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  b, p);.}../*.** 
2e672 44 65 61 6c 6c 6f 63 61 74 65 20 61 20 57 68 65  Deallocate a Whe
2e673 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75  reClause structu
2e674 72 65 2e 20 20 54 68 65 20 57 68 65 72 65 43 6c  re.  The WhereCl
2e675 61 75 73 65 20 73 74 72 75 63 74 75 72 65 0a 2a  ause structure.*
2e676 2a 20 69 74 73 65 6c 66 20 69 73 20 6e 6f 74 20  * itself is not 
2e677 66 72 65 65 64 2e 20 20 54 68 69 73 20 72 6f 75  freed.  This rou
2e678 74 69 6e 65 20 69 73 20 74 68 65 20 69 6e 76 65  tine is the inve
2e679 72 73 65 20 6f 66 20 77 68 65 72 65 43 6c 61 75  rse of whereClau
2e67a 73 65 49 6e 69 74 28 29 2e 0a 2a 2f 0a 73 74 61  seInit()..*/.sta
2e67b 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c  tic void whereCl
2e67c 61 75 73 65 43 6c 65 61 72 28 57 68 65 72 65 43  auseClear(WhereC
2e67d 6c 61 75 73 65 20 2a 70 57 43 29 7b 0a 20 20 69  lause *pWC){.  i
2e67e 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 54 65 72  nt i;.  WhereTer
2e67f 6d 20 2a 61 3b 0a 20 20 73 71 6c 69 74 65 33 20  m *a;.  sqlite3 
2e680 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50 61 72 73  *db = pWC->pPars
2e681 65 2d 3e 64 62 3b 0a 20 20 66 6f 72 28 69 3d 70  e->db;.  for(i=p
2e682 57 43 2d 3e 6e 54 65 72 6d 2d 31 2c 20 61 3d 70  WC->nTerm-1, a=p
2e683 57 43 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d  WC->a; i>=0; i--
2e684 2c 20 61 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , a++){.    if( 
2e685 61 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  a->wtFlags & TER
2e686 4d 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20  M_DYNAMIC ){.   
2e687 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
2e688 6c 65 74 65 28 64 62 2c 20 61 2d 3e 70 45 78 70  lete(db, a->pExp
2e689 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  r);.    }.    if
2e68a 28 20 61 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  ( a->wtFlags & T
2e68b 45 52 4d 5f 4f 52 49 4e 46 4f 20 29 7b 0a 20 20  ERM_ORINFO ){.  
2e68c 20 20 20 20 77 68 65 72 65 4f 72 49 6e 66 6f 44      whereOrInfoD
2e68d 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70  elete(db, a->u.p
2e68e 4f 72 49 6e 66 6f 29 3b 0a 20 20 20 20 7d 65 6c  OrInfo);.    }el
2e68f 73 65 20 69 66 28 20 61 2d 3e 77 74 46 6c 61 67  se if( a->wtFlag
2e690 73 20 26 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f  s & TERM_ANDINFO
2e691 20 29 7b 0a 20 20 20 20 20 20 77 68 65 72 65 41   ){.      whereA
2e692 6e 64 49 6e 66 6f 44 65 6c 65 74 65 28 64 62 2c  ndInfoDelete(db,
2e693 20 61 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 29 3b   a->u.pAndInfo);
2e694 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
2e695 20 70 57 43 2d 3e 61 21 3d 70 57 43 2d 3e 61 53   pWC->a!=pWC->aS
2e696 74 61 74 69 63 20 29 7b 0a 20 20 20 20 73 71 6c  tatic ){.    sql
2e697 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2e698 57 43 2d 3e 61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  WC->a);.  }.}../
2e699 2a 0a 2a 2a 20 41 64 64 20 61 20 73 69 6e 67 6c  *.** Add a singl
2e69a 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20  e new WhereTerm 
2e69b 65 6e 74 72 79 20 74 6f 20 74 68 65 20 57 68 65  entry to the Whe
2e69c 72 65 43 6c 61 75 73 65 20 6f 62 6a 65 63 74 20  reClause object 
2e69d 70 57 43 2e 0a 2a 2a 20 54 68 65 20 6e 65 77 20  pWC..** The new 
2e69e 57 68 65 72 65 54 65 72 6d 20 6f 62 6a 65 63 74  WhereTerm object
2e69f 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20   is constructed 
2e6a0 66 72 6f 6d 20 45 78 70 72 20 70 20 61 6e 64 20  from Expr p and 
2e6a1 77 69 74 68 20 77 74 46 6c 61 67 73 2e 0a 2a 2a  with wtFlags..**
2e6a2 20 54 68 65 20 69 6e 64 65 78 20 69 6e 20 70 57   The index in pW
2e6a3 43 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 20 6e 65  C->a[] of the ne
2e6a4 77 20 57 68 65 72 65 54 65 72 6d 20 69 73 20 72  w WhereTerm is r
2e6a5 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65  eturned on succe
2e6a6 73 73 2e 0a 2a 2a 20 30 20 69 73 20 72 65 74 75  ss..** 0 is retu
2e6a7 72 6e 65 64 20 69 66 20 74 68 65 20 6e 65 77 20  rned if the new 
2e6a8 57 68 65 72 65 54 65 72 6d 20 63 6f 75 6c 64 20  WhereTerm could 
2e6a9 6e 6f 74 20 62 65 20 61 64 64 65 64 20 64 75 65  not be added due
2e6aa 20 74 6f 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20   to a memory.** 
2e6ab 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
2e6ac 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c  .  The memory al
2e6ad 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72 65  location failure
2e6ae 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 72 64 65   will be recorde
2e6af 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 62 2d 3e  d in.** the db->
2e6b0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
2e6b1 67 20 73 6f 20 74 68 61 74 20 68 69 67 68 65 72  g so that higher
2e6b2 2d 6c 65 76 65 6c 20 66 75 6e 63 74 69 6f 6e 73  -level functions
2e6b3 20 63 61 6e 20 64 65 74 65 63 74 20 69 74 2e 0a   can detect it..
2e6b4 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2e6b5 6e 65 20 77 69 6c 6c 20 69 6e 63 72 65 61 73 65  ne will increase
2e6b6 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
2e6b7 20 70 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79 20   pWC->a[] array 
2e6b8 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a  as necessary..**
2e6b9 0a 2a 2a 20 49 66 20 74 68 65 20 77 74 46 6c 61  .** If the wtFla
2e6ba 67 73 20 61 72 67 75 6d 65 6e 74 20 69 6e 63 6c  gs argument incl
2e6bb 75 64 65 73 20 54 45 52 4d 5f 44 59 4e 41 4d 49  udes TERM_DYNAMI
2e6bc 43 2c 20 74 68 65 6e 20 72 65 73 70 6f 6e 73 69  C, then responsi
2e6bd 62 69 6c 69 74 79 0a 2a 2a 20 66 6f 72 20 66 72  bility.** for fr
2e6be 65 65 69 6e 67 20 74 68 65 20 65 78 70 72 65 73  eeing the expres
2e6bf 73 69 6f 6e 20 70 20 69 73 20 61 73 73 75 6d 65  sion p is assume
2e6c0 64 20 62 79 20 74 68 65 20 57 68 65 72 65 43 6c  d by the WhereCl
2e6c1 61 75 73 65 20 6f 62 6a 65 63 74 20 70 57 43 2e  ause object pWC.
2e6c2 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 72 75 65  .** This is true
2e6c3 20 65 76 65 6e 20 69 66 20 74 68 69 73 20 72 6f   even if this ro
2e6c4 75 74 69 6e 65 20 66 61 69 6c 73 20 74 6f 20 61  utine fails to a
2e6c5 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 57 68  llocate a new Wh
2e6c6 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 57  ereTerm..**.** W
2e6c7 41 52 4e 49 4e 47 3a 20 20 54 68 69 73 20 72 6f  ARNING:  This ro
2e6c8 75 74 69 6e 65 20 6d 69 67 68 74 20 72 65 61 6c  utine might real
2e6c9 6c 6f 63 61 74 65 20 74 68 65 20 73 70 61 63 65  locate the space
2e6ca 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a   used to store.*
2e6cb 2a 20 57 68 65 72 65 54 65 72 6d 73 2e 20 20 41  * WhereTerms.  A
2e6cc 6c 6c 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 57  ll pointers to W
2e6cd 68 65 72 65 54 65 72 6d 73 20 73 68 6f 75 6c 64  hereTerms should
2e6ce 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20   be invalidated 
2e6cf 61 66 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67  after.** calling
2e6d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
2e6d1 53 75 63 68 20 70 6f 69 6e 74 65 72 73 20 6d 61  Such pointers ma
2e6d2 79 20 62 65 20 72 65 69 6e 69 74 69 61 6c 69 7a  y be reinitializ
2e6d3 65 64 20 62 79 20 72 65 66 65 72 65 6e 63 69 6e  ed by referencin
2e6d4 67 0a 2a 2a 20 74 68 65 20 70 57 43 2d 3e 61 5b  g.** the pWC->a[
2e6d5 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a 73 74 61 74  ] array..*/.stat
2e6d6 69 63 20 69 6e 74 20 77 68 65 72 65 43 6c 61 75  ic int whereClau
2e6d7 73 65 49 6e 73 65 72 74 28 57 68 65 72 65 43 6c  seInsert(WhereCl
2e6d8 61 75 73 65 20 2a 70 57 43 2c 20 45 78 70 72 20  ause *pWC, Expr 
2e6d9 2a 70 2c 20 75 38 20 77 74 46 6c 61 67 73 29 7b  *p, u8 wtFlags){
2e6da 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
2e6db 65 72 6d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a  erm;.  int idx;.
2e6dc 20 20 69 66 28 20 70 57 43 2d 3e 6e 54 65 72 6d    if( pWC->nTerm
2e6dd 3e 3d 70 57 43 2d 3e 6e 53 6c 6f 74 20 29 7b 0a  >=pWC->nSlot ){.
2e6de 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2e6df 4f 6c 64 20 3d 20 70 57 43 2d 3e 61 3b 0a 20 20  Old = pWC->a;.  
2e6e0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2e6e1 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 3b  pWC->pParse->db;
2e6e2 0a 20 20 20 20 70 57 43 2d 3e 61 20 3d 20 73 71  .    pWC->a = sq
2e6e3 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
2e6e4 28 64 62 2c 20 73 69 7a 65 6f 66 28 70 57 43 2d  (db, sizeof(pWC-
2e6e5 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 53 6c 6f  >a[0])*pWC->nSlo
2e6e6 74 2a 32 20 29 3b 0a 20 20 20 20 69 66 28 20 70  t*2 );.    if( p
2e6e7 57 43 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20  WC->a==0 ){.    
2e6e8 20 20 69 66 28 20 77 74 46 6c 61 67 73 20 26 20    if( wtFlags & 
2e6e9 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 29 7b 0a  TERM_DYNAMIC ){.
2e6ea 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2e6eb 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 29  xprDelete(db, p)
2e6ec 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2e6ed 70 57 43 2d 3e 61 20 3d 20 70 4f 6c 64 3b 0a 20  pWC->a = pOld;. 
2e6ee 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
2e6ef 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28     }.    memcpy(
2e6f0 70 57 43 2d 3e 61 2c 20 70 4f 6c 64 2c 20 73 69  pWC->a, pOld, si
2e6f1 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a  zeof(pWC->a[0])*
2e6f2 70 57 43 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20 20  pWC->nTerm);.   
2e6f3 20 69 66 28 20 70 4f 6c 64 21 3d 70 57 43 2d 3e   if( pOld!=pWC->
2e6f4 61 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20  aStatic ){.     
2e6f5 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2e6f6 62 2c 20 70 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a  b, pOld);.    }.
2e6f7 20 20 20 20 70 57 43 2d 3e 6e 53 6c 6f 74 20 3d      pWC->nSlot =
2e6f8 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2e6f9 53 69 7a 65 28 64 62 2c 20 70 57 43 2d 3e 61 29  Size(db, pWC->a)
2e6fa 2f 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30  /sizeof(pWC->a[0
2e6fb 5d 29 3b 0a 20 20 7d 0a 20 20 70 54 65 72 6d 20  ]);.  }.  pTerm 
2e6fc 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 20 3d 20  = &pWC->a[idx = 
2e6fd 70 57 43 2d 3e 6e 54 65 72 6d 2b 2b 5d 3b 0a 20  pWC->nTerm++];. 
2e6fe 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 20 3d 20   pTerm->pExpr = 
2e6ff 70 3b 0a 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c  p;.  pTerm->wtFl
2e700 61 67 73 20 3d 20 77 74 46 6c 61 67 73 3b 0a 20  ags = wtFlags;. 
2e701 20 70 54 65 72 6d 2d 3e 70 57 43 20 3d 20 70 57   pTerm->pWC = pW
2e702 43 3b 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61 72  C;.  pTerm->iPar
2e703 65 6e 74 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75  ent = -1;.  retu
2e704 72 6e 20 69 64 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn idx;.}../*.**
2e705 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 64   This routine id
2e706 65 6e 74 69 66 69 65 73 20 73 75 62 65 78 70 72  entifies subexpr
2e707 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57  essions in the W
2e708 48 45 52 45 20 63 6c 61 75 73 65 20 77 68 65 72  HERE clause wher
2e709 65 0a 2a 2a 20 65 61 63 68 20 73 75 62 65 78 70  e.** each subexp
2e70a 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72  ression is separ
2e70b 61 74 65 64 20 62 79 20 74 68 65 20 41 4e 44 20  ated by the AND 
2e70c 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 6f 6d 65  operator or some
2e70d 20 6f 74 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74   other.** operat
2e70e 6f 72 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  or specified in 
2e70f 74 68 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72  the op parameter
2e710 2e 20 20 54 68 65 20 57 68 65 72 65 43 6c 61 75  .  The WhereClau
2e711 73 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  se structure.** 
2e712 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 70  is filled with p
2e713 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 65 78  ointers to subex
2e714 70 72 65 73 73 69 6f 6e 73 2e 20 20 46 6f 72 20  pressions.  For 
2e715 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  example:.**.**  
2e716 20 20 57 48 45 52 45 20 20 61 3d 3d 27 68 65 6c    WHERE  a=='hel
2e717 6c 6f 27 20 41 4e 44 20 63 6f 61 6c 65 73 63 65  lo' AND coalesce
2e718 28 62 2c 31 31 29 3c 31 30 20 41 4e 44 20 28 63  (b,11)<10 AND (c
2e719 2b 31 32 21 3d 64 20 4f 52 20 63 3d 3d 32 32 29  +12!=d OR c==22)
2e71a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5c 5f  .**           \_
2e71b 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f  _______/     \__
2e71c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20  _____________/  
2e71d 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f     \____________
2e71e 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20  ____/.**        
2e71f 20 20 20 20 73 6c 6f 74 5b 30 5d 20 20 20 20 20      slot[0]     
2e720 20 20 20 20 20 20 20 73 6c 6f 74 5b 31 5d 20 20         slot[1]  
2e721 20 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f               slo
2e722 74 5b 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  t[2].**.** The o
2e723 72 69 67 69 6e 61 6c 20 57 48 45 52 45 20 63 6c  riginal WHERE cl
2e724 61 75 73 65 20 69 6e 20 70 45 78 70 72 20 69 73  ause in pExpr is
2e725 20 75 6e 61 6c 74 65 72 65 64 2e 20 20 41 6c 6c   unaltered.  All
2e726 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
2e727 20 64 6f 65 73 20 69 73 20 6d 61 6b 65 20 73 6c   does is make sl
2e728 6f 74 5b 5d 20 65 6e 74 72 69 65 73 20 70 6f 69  ot[] entries poi
2e729 6e 74 20 74 6f 20 73 75 62 73 74 72 75 63 74 75  nt to substructu
2e72a 72 65 20 77 69 74 68 69 6e 20 70 45 78 70 72 2e  re within pExpr.
2e72b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 70 72  .**.** In the pr
2e72c 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 20  evious sentence 
2e72d 61 6e 64 20 69 6e 20 74 68 65 20 64 69 61 67 72  and in the diagr
2e72e 61 6d 2c 20 22 73 6c 6f 74 5b 5d 22 20 72 65 66  am, "slot[]" ref
2e72f 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 57 68  ers to.** the Wh
2e730 65 72 65 43 6c 61 75 73 65 2e 61 5b 5d 20 61 72  ereClause.a[] ar
2e731 72 61 79 2e 20 20 54 68 65 20 73 6c 6f 74 5b 5d  ray.  The slot[]
2e732 20 61 72 72 61 79 20 67 72 6f 77 73 20 61 73 20   array grows as 
2e733 6e 65 65 64 65 64 20 74 6f 20 63 6f 6e 74 61 69  needed to contai
2e734 6e 0a 2a 2a 20 61 6c 6c 20 74 65 72 6d 73 20 6f  n.** all terms o
2e735 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2e736 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  se..*/.static vo
2e737 69 64 20 77 68 65 72 65 53 70 6c 69 74 28 57 68  id whereSplit(Wh
2e738 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2e739 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
2e73a 20 6f 70 29 7b 0a 20 20 70 57 43 2d 3e 6f 70 20   op){.  pWC->op 
2e73b 3d 20 28 75 38 29 6f 70 3b 0a 20 20 69 66 28 20  = (u8)op;.  if( 
2e73c 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
2e73d 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  n;.  if( pExpr->
2e73e 6f 70 21 3d 6f 70 20 29 7b 0a 20 20 20 20 77 68  op!=op ){.    wh
2e73f 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
2e740 70 57 43 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a  pWC, pExpr, 0);.
2e741 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 65    }else{.    whe
2e742 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70 45 78  reSplit(pWC, pEx
2e743 70 72 2d 3e 70 4c 65 66 74 2c 20 6f 70 29 3b 0a  pr->pLeft, op);.
2e744 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70      whereSplit(p
2e745 57 43 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  WC, pExpr->pRigh
2e746 74 2c 20 6f 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  t, op);.  }.}../
2e747 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
2e748 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61  an expression ma
2e749 73 6b 20 73 65 74 20 28 61 20 57 68 65 72 65 4d  sk set (a WhereM
2e74a 61 73 6b 53 65 74 20 6f 62 6a 65 63 74 29 0a 2a  askSet object).*
2e74b 2f 0a 23 64 65 66 69 6e 65 20 69 6e 69 74 4d 61  /.#define initMa
2e74c 73 6b 53 65 74 28 50 29 20 20 6d 65 6d 73 65 74  skSet(P)  memset
2e74d 28 50 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 50  (P, 0, sizeof(*P
2e74e 29 29 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  ))../*.** Return
2e74f 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72   the bitmask for
2e750 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f   the given curso
2e751 72 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72  r number.  Retur
2e752 6e 20 30 20 69 66 0a 2a 2a 20 69 43 75 72 73 6f  n 0 if.** iCurso
2e753 72 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20  r is not in the 
2e754 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42  set..*/.static B
2e755 69 74 6d 61 73 6b 20 67 65 74 4d 61 73 6b 28 57  itmask getMask(W
2e756 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2e757 73 6b 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73  skSet, int iCurs
2e758 6f 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  or){.  int i;.  
2e759 61 73 73 65 72 74 28 20 70 4d 61 73 6b 53 65 74  assert( pMaskSet
2e75a 2d 3e 6e 3c 3d 73 69 7a 65 6f 66 28 42 69 74 6d  ->n<=sizeof(Bitm
2e75b 61 73 6b 29 2a 38 20 29 3b 0a 20 20 66 6f 72 28  ask)*8 );.  for(
2e75c 69 3d 30 3b 20 69 3c 70 4d 61 73 6b 53 65 74 2d  i=0; i<pMaskSet-
2e75d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  >n; i++){.    if
2e75e 28 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 69  ( pMaskSet->ix[i
2e75f 5d 3d 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20  ]==iCursor ){.  
2e760 20 20 20 20 72 65 74 75 72 6e 20 28 28 42 69 74      return ((Bit
2e761 6d 61 73 6b 29 31 29 3c 3c 69 3b 0a 20 20 20 20  mask)1)<<i;.    
2e762 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
2e763 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
2e764 65 20 61 20 6e 65 77 20 6d 61 73 6b 20 66 6f 72  e a new mask for
2e765 20 63 75 72 73 6f 72 20 69 43 75 72 73 6f 72 2e   cursor iCursor.
2e766 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
2e767 6f 6e 65 20 63 75 72 73 6f 72 20 70 65 72 20 74  one cursor per t
2e768 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
2e769 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20 6e 75   clause.  The nu
2e76a 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65  mber of.** table
2e76b 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
2e76c 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65 64 20  ause is limited 
2e76d 62 79 20 61 20 74 65 73 74 20 65 61 72 6c 79 20  by a test early 
2e76e 69 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  in the.** sqlite
2e76f 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 72 6f  3WhereBegin() ro
2e770 75 74 69 6e 65 2e 20 20 53 6f 20 77 65 20 6b 6e  utine.  So we kn
2e771 6f 77 20 74 68 61 74 20 74 68 65 20 70 4d 61 73  ow that the pMas
2e772 6b 53 65 74 2d 3e 69 78 5b 5d 0a 2a 2a 20 61 72  kSet->ix[].** ar
2e773 72 61 79 20 77 69 6c 6c 20 6e 65 76 65 72 20 6f  ray will never o
2e774 76 65 72 66 6c 6f 77 2e 0a 2a 2f 0a 73 74 61 74  verflow..*/.stat
2e775 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 4d 61  ic void createMa
2e776 73 6b 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20  sk(WhereMaskSet 
2e777 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e 74 20 69  *pMaskSet, int i
2e778 43 75 72 73 6f 72 29 7b 0a 20 20 61 73 73 65 72  Cursor){.  asser
2e779 74 28 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 20 3c  t( pMaskSet->n <
2e77a 20 41 72 72 61 79 53 69 7a 65 28 70 4d 61 73 6b   ArraySize(pMask
2e77b 53 65 74 2d 3e 69 78 29 20 29 3b 0a 20 20 70 4d  Set->ix) );.  pM
2e77c 61 73 6b 53 65 74 2d 3e 69 78 5b 70 4d 61 73 6b  askSet->ix[pMask
2e77d 53 65 74 2d 3e 6e 2b 2b 5d 20 3d 20 69 43 75 72  Set->n++] = iCur
2e77e 73 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  sor;.}../*.** Th
2e77f 69 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73  is routine walks
2e780 20 28 72 65 63 75 72 73 69 76 65 6c 79 29 20 61   (recursively) a
2e781 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
2e782 65 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73 0a  e and generates.
2e783 2a 2a 20 61 20 62 69 74 6d 61 73 6b 20 69 6e 64  ** a bitmask ind
2e784 69 63 61 74 69 6e 67 20 77 68 69 63 68 20 74 61  icating which ta
2e785 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 69 6e  bles are used in
2e786 20 74 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e   that expression
2e787 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  .** tree..**.** 
2e788 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69  In order for thi
2e789 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 77 6f 72  s routine to wor
2e78a 6b 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66  k, the calling f
2e78b 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 68 61 76  unction must hav
2e78c 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  e.** previously 
2e78d 69 6e 76 6f 6b 65 64 20 73 71 6c 69 74 65 33 52  invoked sqlite3R
2e78e 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
2e78f 29 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73  ) on the express
2e790 69 6f 6e 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65  ion.  See.** the
2e791 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20   header comment 
2e792 6f 6e 20 74 68 61 74 20 72 6f 75 74 69 6e 65 20  on that routine 
2e793 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
2e794 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54  nformation..** T
2e795 68 65 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  he sqlite3Resolv
2e796 65 45 78 70 72 4e 61 6d 65 73 28 29 20 72 6f 75  eExprNames() rou
2e797 74 69 6e 65 73 20 6c 6f 6f 6b 73 20 66 6f 72 20  tines looks for 
2e798 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64  column names and
2e799 0a 2a 2a 20 73 65 74 73 20 74 68 65 69 72 20 6f  .** sets their o
2e79a 70 63 6f 64 65 73 20 74 6f 20 54 4b 5f 43 4f 4c  pcodes to TK_COL
2e79b 55 4d 4e 20 61 6e 64 20 74 68 65 69 72 20 45 78  UMN and their Ex
2e79c 70 72 2e 69 54 61 62 6c 65 20 66 69 65 6c 64 73  pr.iTable fields
2e79d 20 74 6f 0a 2a 2a 20 74 68 65 20 56 44 42 45 20   to.** the VDBE 
2e79e 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  cursor number of
2e79f 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 69   the table.  Thi
2e7a0 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 68  s routine just h
2e7a1 61 73 20 74 6f 0a 2a 2a 20 74 72 61 6e 73 6c 61  as to.** transla
2e7a2 74 65 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75  te the cursor nu
2e7a3 6d 62 65 72 73 20 69 6e 74 6f 20 62 69 74 6d 61  mbers into bitma
2e7a4 73 6b 20 76 61 6c 75 65 73 20 61 6e 64 20 4f 52  sk values and OR
2e7a5 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 62 69 74 6d   all.** the bitm
2e7a6 61 73 6b 73 20 74 6f 67 65 74 68 65 72 2e 0a 2a  asks together..*
2e7a7 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b  /.static Bitmask
2e7a8 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73   exprListTableUs
2e7a9 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74  age(WhereMaskSet
2e7aa 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73  *, ExprList*);.s
2e7ab 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78  tatic Bitmask ex
2e7ac 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61  prSelectTableUsa
2e7ad 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74 2a  ge(WhereMaskSet*
2e7ae 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 73 74 61 74  , Select*);.stat
2e7af 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 54  ic Bitmask exprT
2e7b0 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d  ableUsage(WhereM
2e7b1 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
2e7b2 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 42 69  , Expr *p){.  Bi
2e7b3 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a  tmask mask = 0;.
2e7b4 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
2e7b5 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e  urn 0;.  if( p->
2e7b6 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b  op==TK_COLUMN ){
2e7b7 0a 20 20 20 20 6d 61 73 6b 20 3d 20 67 65 74 4d  .    mask = getM
2e7b8 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d  ask(pMaskSet, p-
2e7b9 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 72 65  >iTable);.    re
2e7ba 74 75 72 6e 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20  turn mask;.  }. 
2e7bb 20 6d 61 73 6b 20 3d 20 65 78 70 72 54 61 62 6c   mask = exprTabl
2e7bc 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
2e7bd 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 6d   p->pRight);.  m
2e7be 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65  ask |= exprTable
2e7bf 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2e7c0 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66 28  p->pLeft);.  if(
2e7c1 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
2e7c2 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  (p, EP_xIsSelect
2e7c3 29 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d  ) ){.    mask |=
2e7c4 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65   exprSelectTable
2e7c5 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2e7c6 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20  p->x.pSelect);. 
2e7c7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 61 73 6b   }else{.    mask
2e7c8 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c   |= exprListTabl
2e7c9 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
2e7ca 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20   p->x.pList);.  
2e7cb 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b  }.  return mask;
2e7cc 0a 7d 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73  .}.static Bitmas
2e7cd 6b 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  k exprListTableU
2e7ce 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65  sage(WhereMaskSe
2e7cf 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78 70  t *pMaskSet, Exp
2e7d0 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20  rList *pList){. 
2e7d1 20 69 6e 74 20 69 3b 0a 20 20 42 69 74 6d 61 73   int i;.  Bitmas
2e7d2 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66  k mask = 0;.  if
2e7d3 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66  ( pList ){.    f
2e7d4 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
2e7d5 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
2e7d6 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72      mask |= expr
2e7d7 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2e7d8 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  Set, pList->a[i]
2e7d9 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20  .pExpr);.    }. 
2e7da 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b   }.  return mask
2e7db 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69 74 6d 61  ;.}.static Bitma
2e7dc 73 6b 20 65 78 70 72 53 65 6c 65 63 74 54 61 62  sk exprSelectTab
2e7dd 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73  leUsage(WhereMas
2e7de 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
2e7df 53 65 6c 65 63 74 20 2a 70 53 29 7b 0a 20 20 42  Select *pS){.  B
2e7e0 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b  itmask mask = 0;
2e7e1 0a 20 20 77 68 69 6c 65 28 20 70 53 20 29 7b 0a  .  while( pS ){.
2e7e2 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72      mask |= expr
2e7e3 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70  ListTableUsage(p
2e7e4 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 45 4c  MaskSet, pS->pEL
2e7e5 69 73 74 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c  ist);.    mask |
2e7e6 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  = exprListTableU
2e7e7 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2e7e8 53 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20  S->pGroupBy);.  
2e7e9 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69    mask |= exprLi
2e7ea 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  stTableUsage(pMa
2e7eb 73 6b 53 65 74 2c 20 70 53 2d 3e 70 4f 72 64 65  skSet, pS->pOrde
2e7ec 72 42 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c  rBy);.    mask |
2e7ed 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2e7ee 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70  (pMaskSet, pS->p
2e7ef 57 68 65 72 65 29 3b 0a 20 20 20 20 6d 61 73 6b  Where);.    mask
2e7f0 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61   |= exprTableUsa
2e7f1 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d  ge(pMaskSet, pS-
2e7f2 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 70  >pHaving);.    p
2e7f3 53 20 3d 20 70 53 2d 3e 70 50 72 69 6f 72 3b 0a  S = pS->pPrior;.
2e7f4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73    }.  return mas
2e7f5 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  k;.}../*.** Retu
2e7f6 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67  rn TRUE if the g
2e7f7 69 76 65 6e 20 6f 70 65 72 61 74 6f 72 20 69 73  iven operator is
2e7f8 20 6f 6e 65 20 6f 66 20 74 68 65 20 6f 70 65 72   one of the oper
2e7f9 61 74 6f 72 73 20 74 68 61 74 20 69 73 0a 2a 2a  ators that is.**
2e7fa 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 61 6e 20   allowed for an 
2e7fb 69 6e 64 65 78 61 62 6c 65 20 57 48 45 52 45 20  indexable WHERE 
2e7fc 63 6c 61 75 73 65 20 74 65 72 6d 2e 20 20 54 68  clause term.  Th
2e7fd 65 20 61 6c 6c 6f 77 65 64 20 6f 70 65 72 61 74  e allowed operat
2e7fe 6f 72 73 20 61 72 65 0a 2a 2a 20 22 3d 22 2c 20  ors are.** "=", 
2e7ff 22 3c 22 2c 20 22 3e 22 2c 20 22 3c 3d 22 2c 20  "<", ">", "<=", 
2e800 22 3e 3d 22 2c 20 61 6e 64 20 22 49 4e 22 2e 0a  ">=", and "IN"..
2e801 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c  */.static int al
2e802 6c 6f 77 65 64 4f 70 28 69 6e 74 20 6f 70 29 7b  lowedOp(int op){
2e803 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54  .  assert( TK_GT
2e804 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f 47 54 3c  >TK_EQ && TK_GT<
2e805 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72  TK_GE );.  asser
2e806 74 28 20 54 4b 5f 4c 54 3e 54 4b 5f 45 51 20 26  t( TK_LT>TK_EQ &
2e807 26 20 54 4b 5f 4c 54 3c 54 4b 5f 47 45 20 29 3b  & TK_LT<TK_GE );
2e808 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45  .  assert( TK_LE
2e809 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 45 3c  >TK_EQ && TK_LE<
2e80a 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72  TK_GE );.  asser
2e80b 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 45 51 2b  t( TK_GE==TK_EQ+
2e80c 34 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f 70  4 );.  return op
2e80d 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 28 6f 70 3e 3d  ==TK_IN || (op>=
2e80e 54 4b 5f 45 51 20 26 26 20 6f 70 3c 3d 54 4b 5f  TK_EQ && op<=TK_
2e80f 47 45 29 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 53  GE) || op==TK_IS
2e810 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  NULL;.}../*.** S
2e811 77 61 70 20 74 77 6f 20 6f 62 6a 65 63 74 73 20  wap two objects 
2e812 6f 66 20 74 79 70 65 20 54 59 50 45 2e 0a 2a 2f  of type TYPE..*/
2e813 0a 23 64 65 66 69 6e 65 20 53 57 41 50 28 54 59  .#define SWAP(TY
2e814 50 45 2c 41 2c 42 29 20 7b 54 59 50 45 20 74 3d  PE,A,B) {TYPE t=
2e815 41 3b 20 41 3d 42 3b 20 42 3d 74 3b 7d 0a 0a 2f  A; A=B; B=t;}../
2e816 2a 0a 2a 2a 20 43 6f 6d 6d 75 74 65 20 61 20 63  *.** Commute a c
2e817 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74  omparison operat
2e818 6f 72 2e 20 20 45 78 70 72 65 73 73 69 6f 6e 73  or.  Expressions
2e819 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20   of the form "X 
2e81a 6f 70 20 59 22 0a 2a 2a 20 61 72 65 20 63 6f 6e  op Y".** are con
2e81b 76 65 72 74 65 64 20 69 6e 74 6f 20 22 59 20 6f  verted into "Y o
2e81c 70 20 58 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  p X"..**.** If a
2e81d 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2e81e 6e 63 65 20 69 73 20 61 73 73 6f 63 69 61 74 65  nce is associate
2e81f 64 20 77 69 74 68 20 65 69 74 68 65 72 20 74 68  d with either th
2e820 65 20 6c 65 66 74 20 6f 72 20 72 69 67 68 74 0a  e left or right.
2e821 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65 20 63  ** side of the c
2e822 6f 6d 70 61 72 69 73 6f 6e 2c 20 69 74 20 72 65  omparison, it re
2e823 6d 61 69 6e 73 20 61 73 73 6f 63 69 61 74 65 64  mains associated
2e824 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 73   with the same s
2e825 69 64 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 65  ide after.** the
2e826 20 63 6f 6d 6d 75 74 61 74 69 6f 6e 2e 20 53 6f   commutation. So
2e827 20 22 59 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41   "Y collate NOCA
2e828 53 45 20 6f 70 20 58 22 20 62 65 63 6f 6d 65 73  SE op X" becomes
2e829 20 0a 2a 2a 20 22 58 20 63 6f 6c 6c 61 74 65 20   .** "X collate 
2e82a 4e 4f 43 41 53 45 20 6f 70 20 59 22 2e 20 54 68  NOCASE op Y". Th
2e82b 69 73 20 69 73 20 62 65 63 61 75 73 65 20 61 6e  is is because an
2e82c 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  y collation sequ
2e82d 65 6e 63 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 6c  ence on.** the l
2e82e 65 66 74 20 68 61 6e 64 20 73 69 64 65 20 6f 66  eft hand side of
2e82f 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 76   a comparison ov
2e830 65 72 72 69 64 65 73 20 61 6e 79 20 63 6f 6c 6c  errides any coll
2e831 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 0a  ation sequence .
2e832 2a 2a 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  ** attached to t
2e833 68 65 20 72 69 67 68 74 2e 20 46 6f 72 20 74 68  he right. For th
2e834 65 20 73 61 6d 65 20 72 65 61 73 6f 6e 20 74 68  e same reason th
2e835 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  e EP_ExpCollate 
2e836 66 6c 61 67 0a 2a 2a 20 69 73 20 6e 6f 74 20 63  flag.** is not c
2e837 6f 6d 6d 75 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  ommuted..*/.stat
2e838 69 63 20 76 6f 69 64 20 65 78 70 72 43 6f 6d 6d  ic void exprComm
2e839 75 74 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ute(Parse *pPars
2e83a 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  e, Expr *pExpr){
2e83b 0a 20 20 75 31 36 20 65 78 70 52 69 67 68 74 20  .  u16 expRight 
2e83c 3d 20 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74  = (pExpr->pRight
2e83d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
2e83e 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 75 31 36 20  Collate);.  u16 
2e83f 65 78 70 4c 65 66 74 20 3d 20 28 70 45 78 70 72  expLeft = (pExpr
2e840 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26  ->pLeft->flags &
2e841 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 3b   EP_ExpCollate);
2e842 0a 20 20 61 73 73 65 72 74 28 20 61 6c 6c 6f 77  .  assert( allow
2e843 65 64 4f 70 28 70 45 78 70 72 2d 3e 6f 70 29 20  edOp(pExpr->op) 
2e844 26 26 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  && pExpr->op!=TK
2e845 5f 49 4e 20 29 3b 0a 20 20 70 45 78 70 72 2d 3e  _IN );.  pExpr->
2e846 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 3d 20  pRight->pColl = 
2e847 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
2e848 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  eq(pParse, pExpr
2e849 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 70 45 78  ->pRight);.  pEx
2e84a 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c  pr->pLeft->pColl
2e84b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2e84c 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
2e84d 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 53  xpr->pLeft);.  S
2e84e 57 41 50 28 43 6f 6c 6c 53 65 71 2a 2c 70 45 78  WAP(CollSeq*,pEx
2e84f 70 72 2d 3e 70 52 69 67 68 74 2d 3e 70 43 6f 6c  pr->pRight->pCol
2e850 6c 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e  l,pExpr->pLeft->
2e851 70 43 6f 6c 6c 29 3b 0a 20 20 70 45 78 70 72 2d  pColl);.  pExpr-
2e852 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 3d  >pRight->flags =
2e853 20 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d   (pExpr->pRight-
2e854 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f 45 78 70  >flags & ~EP_Exp
2e855 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78 70 4c 65  Collate) | expLe
2e856 66 74 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65  ft;.  pExpr->pLe
2e857 66 74 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45 78  ft->flags = (pEx
2e858 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73  pr->pLeft->flags
2e859 20 26 20 7e 45 50 5f 45 78 70 43 6f 6c 6c 61 74   & ~EP_ExpCollat
2e85a 65 29 20 7c 20 65 78 70 52 69 67 68 74 3b 0a 20  e) | expRight;. 
2e85b 20 53 57 41 50 28 45 78 70 72 2a 2c 70 45 78 70   SWAP(Expr*,pExp
2e85c 72 2d 3e 70 52 69 67 68 74 2c 70 45 78 70 72 2d  r->pRight,pExpr-
2e85d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70  >pLeft);.  if( p
2e85e 45 78 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20  Expr->op>=TK_GT 
2e85f 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 54  ){.    assert( T
2e860 4b 5f 4c 54 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b  K_LT==TK_GT+2 );
2e861 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f  .    assert( TK_
2e862 47 45 3d 3d 54 4b 5f 4c 45 2b 32 20 29 3b 0a 20  GE==TK_LE+2 );. 
2e863 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54     assert( TK_GT
2e864 3e 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 61 73  >TK_EQ );.    as
2e865 73 65 72 74 28 20 54 4b 5f 47 54 3c 54 4b 5f 4c  sert( TK_GT<TK_L
2e866 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  E );.    assert(
2e867 20 70 45 78 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47   pExpr->op>=TK_G
2e868 54 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 3c 3d  T && pExpr->op<=
2e869 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 70 45 78  TK_GE );.    pEx
2e86a 70 72 2d 3e 6f 70 20 3d 20 28 28 70 45 78 70 72  pr->op = ((pExpr
2e86b 2d 3e 6f 70 2d 54 4b 5f 47 54 29 5e 32 29 2b 54  ->op-TK_GT)^2)+T
2e86c 4b 5f 47 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  K_GT;.  }.}../*.
2e86d 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 66 72 6f  ** Translate fro
2e86e 6d 20 54 4b 5f 78 78 20 6f 70 65 72 61 74 6f 72  m TK_xx operator
2e86f 20 74 6f 20 57 4f 5f 78 78 20 62 69 74 6d 61 73   to WO_xx bitmas
2e870 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31 36  k..*/.static u16
2e871 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b 28 69 6e   operatorMask(in
2e872 74 20 6f 70 29 7b 0a 20 20 75 31 36 20 63 3b 0a  t op){.  u16 c;.
2e873 20 20 61 73 73 65 72 74 28 20 61 6c 6c 6f 77 65    assert( allowe
2e874 64 4f 70 28 6f 70 29 20 29 3b 0a 20 20 69 66 28  dOp(op) );.  if(
2e875 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20   op==TK_IN ){.  
2e876 20 20 63 20 3d 20 57 4f 5f 49 4e 3b 0a 20 20 7d    c = WO_IN;.  }
2e877 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  else if( op==TK_
2e878 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 63 20  ISNULL ){.    c 
2e879 3d 20 57 4f 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d  = WO_ISNULL;.  }
2e87a 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
2e87b 28 20 28 57 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b  ( (WO_EQ<<(op-TK
2e87c 5f 45 51 29 29 20 3c 20 30 78 37 66 66 66 20 29  _EQ)) < 0x7fff )
2e87d 3b 0a 20 20 20 20 63 20 3d 20 28 75 31 36 29 28  ;.    c = (u16)(
2e87e 57 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51  WO_EQ<<(op-TK_EQ
2e87f 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ));.  }.  assert
2e880 28 20 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op!=TK_ISNULL 
2e881 7c 7c 20 63 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20  || c==WO_ISNULL 
2e882 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21  );.  assert( op!
2e883 3d 54 4b 5f 49 4e 20 7c 7c 20 63 3d 3d 57 4f 5f  =TK_IN || c==WO_
2e884 49 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  IN );.  assert( 
2e885 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c 20 63 3d 3d  op!=TK_EQ || c==
2e886 57 4f 5f 45 51 20 29 3b 0a 20 20 61 73 73 65 72  WO_EQ );.  asser
2e887 74 28 20 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20  t( op!=TK_LT || 
2e888 63 3d 3d 57 4f 5f 4c 54 20 29 3b 0a 20 20 61 73  c==WO_LT );.  as
2e889 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 4c 45 20  sert( op!=TK_LE 
2e88a 7c 7c 20 63 3d 3d 57 4f 5f 4c 45 20 29 3b 0a 20  || c==WO_LE );. 
2e88b 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2e88c 47 54 20 7c 7c 20 63 3d 3d 57 4f 5f 47 54 20 29  GT || c==WO_GT )
2e88d 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
2e88e 54 4b 5f 47 45 20 7c 7c 20 63 3d 3d 57 4f 5f 47  TK_GE || c==WO_G
2e88f 45 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 3b  E );.  return c;
2e890 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68  .}../*.** Search
2e891 20 66 6f 72 20 61 20 74 65 72 6d 20 69 6e 20 74   for a term in t
2e892 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2e893 74 68 61 74 20 69 73 20 6f 66 20 74 68 65 20 66  that is of the f
2e894 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70  orm "X <op> <exp
2e895 72 3e 22 0a 2a 2a 20 77 68 65 72 65 20 58 20 69  r>".** where X i
2e896 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
2e897 20 74 68 65 20 69 43 6f 6c 75 6d 6e 20 6f 66 20   the iColumn of 
2e898 74 61 62 6c 65 20 69 43 75 72 20 61 6e 64 20 3c  table iCur and <
2e899 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66 0a 2a 2a  op> is one of.**
2e89a 20 74 68 65 20 57 4f 5f 78 78 20 6f 70 65 72 61   the WO_xx opera
2e89b 74 6f 72 20 63 6f 64 65 73 20 73 70 65 63 69 66  tor codes specif
2e89c 69 65 64 20 62 79 20 74 68 65 20 6f 70 20 70 61  ied by the op pa
2e89d 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 52 65 74 75  rameter..** Retu
2e89e 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
2e89f 74 68 65 20 74 65 72 6d 2e 20 20 52 65 74 75 72  the term.  Retur
2e8a0 6e 20 30 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  n 0 if not found
2e8a1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 68 65 72  ..*/.static Wher
2e8a2 65 54 65 72 6d 20 2a 66 69 6e 64 54 65 72 6d 28  eTerm *findTerm(
2e8a3 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2e8a4 70 57 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  pWC,     /* The 
2e8a5 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20  WHERE clause to 
2e8a6 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20  be searched */. 
2e8a7 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20   int iCur,      
2e8a8 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
2e8a9 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48 53 20 2a   number of LHS *
2e8aa 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c  /.  int iColumn,
2e8ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
2e8ac 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48  umn number of LH
2e8ad 53 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  S */.  Bitmask n
2e8ae 6f 74 52 65 61 64 79 2c 20 20 20 20 20 2f 2a 20  otReady,     /* 
2e8af 52 48 53 20 6d 75 73 74 20 6e 6f 74 20 6f 76 65  RHS must not ove
2e8b0 72 6c 61 70 20 77 69 74 68 20 74 68 69 73 20 6d  rlap with this m
2e8b1 61 73 6b 20 2a 2f 0a 20 20 75 33 32 20 6f 70 2c  ask */.  u32 op,
2e8b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e8b3 2a 20 4d 61 73 6b 20 6f 66 20 57 4f 5f 78 78 20  * Mask of WO_xx 
2e8b4 76 61 6c 75 65 73 20 64 65 73 63 72 69 62 69 6e  values describin
2e8b5 67 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  g operator */.  
2e8b6 49 6e 64 65 78 20 2a 70 49 64 78 20 20 20 20 20  Index *pIdx     
2e8b7 20 20 20 20 20 20 2f 2a 20 4d 75 73 74 20 62 65        /* Must be
2e8b8 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68   compatible with
2e8b9 20 74 68 69 73 20 69 6e 64 65 78 2c 20 69 66 20   this index, if 
2e8ba 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20  not NULL */.){. 
2e8bb 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2e8bc 6d 3b 0a 20 20 69 6e 74 20 6b 3b 0a 20 20 61 73  m;.  int k;.  as
2e8bd 73 65 72 74 28 20 69 43 75 72 3e 3d 30 20 29 3b  sert( iCur>=0 );
2e8be 0a 20 20 6f 70 20 26 3d 20 57 4f 5f 41 4c 4c 3b  .  op &= WO_ALL;
2e8bf 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43  .  for(pTerm=pWC
2e8c0 2d 3e 61 2c 20 6b 3d 70 57 43 2d 3e 6e 54 65 72  ->a, k=pWC->nTer
2e8c1 6d 3b 20 6b 3b 20 6b 2d 2d 2c 20 70 54 65 72 6d  m; k; k--, pTerm
2e8c2 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65  ++){.    if( pTe
2e8c3 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d  rm->leftCursor==
2e8c4 69 43 75 72 0a 20 20 20 20 20 20 20 26 26 20 28  iCur.       && (
2e8c5 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
2e8c6 68 74 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d  ht & notReady)==
2e8c7 30 0a 20 20 20 20 20 20 20 26 26 20 70 54 65 72  0.       && pTer
2e8c8 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d  m->u.leftColumn=
2e8c9 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20  =iColumn.       
2e8ca 26 26 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72  && (pTerm->eOper
2e8cb 61 74 6f 72 20 26 20 6f 70 29 21 3d 30 0a 20 20  ator & op)!=0.  
2e8cc 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70    ){.      if( p
2e8cd 49 64 78 20 26 26 20 70 54 65 72 6d 2d 3e 65 4f  Idx && pTerm->eO
2e8ce 70 65 72 61 74 6f 72 21 3d 57 4f 5f 49 53 4e 55  perator!=WO_ISNU
2e8cf 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78  LL ){.        Ex
2e8d0 70 72 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e  pr *pX = pTerm->
2e8d1 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 43  pExpr;.        C
2e8d2 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
2e8d3 20 20 20 20 20 20 20 63 68 61 72 20 69 64 78 61         char idxa
2e8d4 66 66 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ff;.        int 
2e8d5 6a 3b 0a 20 20 20 20 20 20 20 20 50 61 72 73 65  j;.        Parse
2e8d6 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e   *pParse = pWC->
2e8d7 70 50 61 72 73 65 3b 0a 0a 20 20 20 20 20 20 20  pParse;..       
2e8d8 20 69 64 78 61 66 66 20 3d 20 70 49 64 78 2d 3e   idxaff = pIdx->
2e8d9 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b 69 43 6f  pTable->aCol[iCo
2e8da 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3b 0a  lumn].affinity;.
2e8db 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c          if( !sql
2e8dc 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
2e8dd 79 4f 6b 28 70 58 2c 20 69 64 78 61 66 66 29 20  yOk(pX, idxaff) 
2e8de 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20  ) continue;..   
2e8df 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f       /* Figure o
2e8e0 75 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ut the collation
2e8e1 20 73 65 71 75 65 6e 63 65 20 72 65 71 75 69 72   sequence requir
2e8e2 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 64 65 78  ed from an index
2e8e3 20 66 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20   for.        ** 
2e8e4 69 74 20 74 6f 20 62 65 20 75 73 65 66 75 6c 20  it to be useful 
2e8e5 66 6f 72 20 6f 70 74 69 6d 69 73 69 6e 67 20 65  for optimising e
2e8e6 78 70 72 65 73 73 69 6f 6e 20 70 58 2e 20 53 74  xpression pX. St
2e8e7 6f 72 65 20 74 68 69 73 0a 20 20 20 20 20 20 20  ore this.       
2e8e8 20 2a 2a 20 76 61 6c 75 65 20 69 6e 20 76 61 72   ** value in var
2e8e9 69 61 62 6c 65 20 70 43 6f 6c 6c 2e 0a 20 20 20  iable pColl..   
2e8ea 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2e8eb 61 73 73 65 72 74 28 70 58 2d 3e 70 4c 65 66 74  assert(pX->pLeft
2e8ec 29 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  );.        pColl
2e8ed 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79   = sqlite3Binary
2e8ee 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70  CompareCollSeq(p
2e8ef 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74  Parse, pX->pLeft
2e8f0 2c 20 70 58 2d 3e 70 52 69 67 68 74 29 3b 0a 20  , pX->pRight);. 
2e8f1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 43         assert(pC
2e8f2 6f 6c 6c 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e  oll || pParse->n
2e8f3 45 72 72 29 3b 0a 0a 20 20 20 20 20 20 20 20 66  Err);..        f
2e8f4 6f 72 28 6a 3d 30 3b 20 70 49 64 78 2d 3e 61 69  or(j=0; pIdx->ai
2e8f5 43 6f 6c 75 6d 6e 5b 6a 5d 21 3d 69 43 6f 6c 75  Column[j]!=iColu
2e8f6 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; j++){.      
2e8f7 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 6a 3e      if( NEVER(j>
2e8f8 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 20  =pIdx->nColumn) 
2e8f9 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
2e8fa 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
2e8fb 28 20 70 43 6f 6c 6c 20 26 26 20 73 71 6c 69 74  ( pColl && sqlit
2e8fc 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d  e3StrICmp(pColl-
2e8fd 3e 7a 4e 61 6d 65 2c 20 70 49 64 78 2d 3e 61 7a  >zName, pIdx->az
2e8fe 43 6f 6c 6c 5b 6a 5d 29 20 29 20 63 6f 6e 74 69  Coll[j]) ) conti
2e8ff 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nue;.      }.   
2e900 20 20 20 72 65 74 75 72 6e 20 70 54 65 72 6d 3b     return pTerm;
2e901 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2e902 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72  urn 0;.}../* For
2e903 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a  ward reference *
2e904 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  /.static void ex
2e905 70 72 41 6e 61 6c 79 7a 65 28 53 72 63 4c 69 73  prAnalyze(SrcLis
2e906 74 2a 2c 20 57 68 65 72 65 43 6c 61 75 73 65 2a  t*, WhereClause*
2e907 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  , int);../*.** C
2e908 61 6c 6c 20 65 78 70 72 41 6e 61 6c 79 7a 65 20  all exprAnalyze 
2e909 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20  on all terms in 
2e90a 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  a WHERE clause. 
2e90b 20 0a 2a 2a 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69   .**.**.*/.stati
2e90c 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
2e90d 7a 65 41 6c 6c 28 0a 20 20 53 72 63 4c 69 73 74  zeAll(.  SrcList
2e90e 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 20   *pTabList,     
2e90f 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c    /* the FROM cl
2e910 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43  ause */.  WhereC
2e911 6c 61 75 73 65 20 2a 70 57 43 20 20 20 20 20 20  lause *pWC      
2e912 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20     /* the WHERE 
2e913 63 6c 61 75 73 65 20 74 6f 20 62 65 20 61 6e 61  clause to be ana
2e914 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e  lyzed */.){.  in
2e915 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43  t i;.  for(i=pWC
2e916 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b  ->nTerm-1; i>=0;
2e917 20 69 2d 2d 29 7b 0a 20 20 20 20 65 78 70 72 41   i--){.    exprA
2e918 6e 61 6c 79 7a 65 28 70 54 61 62 4c 69 73 74 2c  nalyze(pTabList,
2e919 20 70 57 43 2c 20 69 29 3b 0a 20 20 7d 0a 7d 0a   pWC, i);.  }.}.
2e91a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2e91b 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49  OMIT_LIKE_OPTIMI
2e91c 5a 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 43 68 65  ZATION./*.** Che
2e91d 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
2e91e 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f   given expressio
2e91f 6e 20 69 73 20 61 20 4c 49 4b 45 20 6f 72 20 47  n is a LIKE or G
2e920 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 74 68 61  LOB operator tha
2e921 74 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 70 74 69  t.** can be opti
2e922 6d 69 7a 65 64 20 75 73 69 6e 67 20 69 6e 65 71  mized using ineq
2e923 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2e924 74 73 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45  ts.  Return TRUE
2e925 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 73 6f 20   if it is.** so 
2e926 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e 6f 74  and false if not
2e927 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72  ..**.** In order
2e928 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f   for the operato
2e929 72 20 74 6f 20 62 65 20 6f 70 74 69 6d 69 7a 69  r to be optimizi
2e92a 62 6c 65 2c 20 74 68 65 20 52 48 53 20 6d 75 73  ble, the RHS mus
2e92b 74 20 62 65 20 61 20 73 74 72 69 6e 67 0a 2a 2a  t be a string.**
2e92c 20 6c 69 74 65 72 61 6c 20 74 68 61 74 20 64 6f   literal that do
2e92d 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74  es not begin wit
2e92e 68 20 61 20 77 69 6c 64 63 61 72 64 2e 20 20 0a  h a wildcard.  .
2e92f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
2e930 4c 69 6b 65 4f 72 47 6c 6f 62 28 0a 20 20 50 61  LikeOrGlob(.  Pa
2e931 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2e932 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63  /* Parsing and c
2e933 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63  ode generating c
2e934 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
2e935 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 2f 2a   *pExpr,      /*
2e936 20 54 65 73 74 20 74 68 69 73 20 65 78 70 72 65   Test this expre
2e937 73 73 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20  ssion */.  Expr 
2e938 2a 2a 70 70 50 72 65 66 69 78 2c 20 20 2f 2a 20  **ppPrefix,  /* 
2e939 50 6f 69 6e 74 65 72 20 74 6f 20 54 4b 5f 53 54  Pointer to TK_ST
2e93a 52 49 4e 47 20 65 78 70 72 65 73 73 69 6f 6e 20  RING expression 
2e93b 77 69 74 68 20 70 61 74 74 65 72 6e 20 70 72 65  with pattern pre
2e93c 66 69 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  fix */.  int *pi
2e93d 73 43 6f 6d 70 6c 65 74 65 2c 20 2f 2a 20 54 72  sComplete, /* Tr
2e93e 75 65 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 77  ue if the only w
2e93f 69 6c 64 63 61 72 64 20 69 73 20 25 20 69 6e 20  ildcard is % in 
2e940 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  the last charact
2e941 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f  er */.  int *pno
2e942 43 61 73 65 20 20 20 20 20 20 2f 2a 20 54 72 75  Case      /* Tru
2e943 65 20 69 66 20 75 70 70 65 72 63 61 73 65 20 69  e if uppercase i
2e944 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  s equivalent to 
2e945 6c 6f 77 65 72 63 61 73 65 20 2a 2f 0a 29 7b 0a  lowercase */.){.
2e946 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
2e947 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
2e948 53 74 72 69 6e 67 20 6f 6e 20 52 48 53 20 6f 66  String on RHS of
2e949 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a   LIKE operator *
2e94a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  /.  Expr *pRight
2e94b 2c 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 2f  , *pLeft;      /
2e94c 2a 20 52 69 67 68 74 20 61 6e 64 20 6c 65 66 74  * Right and left
2e94d 20 73 69 7a 65 20 6f 66 20 4c 49 4b 45 20 6f 70   size of LIKE op
2e94e 65 72 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70 72  erator */.  Expr
2e94f 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20  List *pList;    
2e950 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
2e951 66 20 6f 70 65 72 61 6e 64 73 20 74 6f 20 74 68  f operands to th
2e952 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  e LIKE operator 
2e953 2a 2f 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20  */.  int c;     
2e954 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e955 2f 2a 20 4f 6e 65 20 63 68 61 72 61 63 74 65 72  /* One character
2e956 20 69 6e 20 7a 5b 5d 20 2a 2f 0a 20 20 69 6e 74   in z[] */.  int
2e957 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20   cnt;           
2e958 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2e959 72 20 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72  r of non-wildcar
2e95a 64 20 70 72 65 66 69 78 20 63 68 61 72 61 63 74  d prefix charact
2e95b 65 72 73 20 2a 2f 0a 20 20 63 68 61 72 20 77 63  ers */.  char wc
2e95c 5b 33 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  [3];            
2e95d 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20      /* Wildcard 
2e95e 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20  characters */.  
2e95f 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
2e960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
2e961 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2e962 20 66 6f 72 20 4c 48 53 20 2a 2f 0a 20 20 73 71   for LHS */.  sq
2e963 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2e964 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61  se->db;  /* Data
2e965 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2e966 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  */.  sqlite3_val
2e967 75 65 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 20 20  ue *pVal = 0;.  
2e968 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
2e969 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
2e96a 63 6f 64 65 20 6f 66 20 70 52 69 67 68 74 20 2a  code of pRight *
2e96b 2f 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  /..  if( !sqlite
2e96c 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28  3IsLikeFunction(
2e96d 64 62 2c 20 70 45 78 70 72 2c 20 70 6e 6f 43 61  db, pExpr, pnoCa
2e96e 73 65 2c 20 77 63 29 20 29 7b 0a 20 20 20 20 72  se, wc) ){.    r
2e96f 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 69 66  eturn 0;.  }.#if
2e970 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49  def SQLITE_EBCDI
2e971 43 0a 20 20 69 66 28 20 2a 70 6e 6f 43 61 73 65  C.  if( *pnoCase
2e972 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e   ) return 0;.#en
2e973 64 69 66 0a 20 20 70 4c 69 73 74 20 3d 20 70 45  dif.  pList = pE
2e974 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20  xpr->x.pList;.  
2e975 70 4c 65 66 74 20 3d 20 70 4c 69 73 74 2d 3e 61  pLeft = pList->a
2e976 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28  [1].pExpr;.  if(
2e977 20 70 4c 65 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43   pLeft->op!=TK_C
2e978 4f 4c 55 4d 4e 20 7c 7c 20 73 71 6c 69 74 65 33  OLUMN || sqlite3
2e979 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65  ExprAffinity(pLe
2e97a 66 74 29 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ft)!=SQLITE_AFF_
2e97b 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20 49  TEXT ){.    /* I
2e97c 4d 50 3a 20 52 2d 30 32 30 36 35 2d 34 39 34 36  MP: R-02065-4946
2e97d 35 20 54 68 65 20 6c 65 66 74 2d 68 61 6e 64 20  5 The left-hand 
2e97e 73 69 64 65 20 6f 66 20 74 68 65 20 4c 49 4b 45  side of the LIKE
2e97f 20 6f 72 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f   or GLOB operato
2e980 72 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65  r must.    ** be
2e981 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20   the name of an 
2e982 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 77  indexed column w
2e983 69 74 68 20 54 45 58 54 20 61 66 66 69 6e 69 74  ith TEXT affinit
2e984 79 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  y. */.    return
2e985 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
2e986 28 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e  ( pLeft->iColumn
2e987 21 3d 28 2d 31 29 20 29 3b 20 2f 2a 20 42 65 63  !=(-1) ); /* Bec
2e988 61 75 73 65 20 49 50 4b 20 6e 65 76 65 72 20 68  ause IPK never h
2e989 61 73 20 41 46 46 5f 54 45 58 54 20 2a 2f 0a 20  as AFF_TEXT */. 
2e98a 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
2e98b 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
2e98c 73 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 61 73  se, pLeft);.  as
2e98d 73 65 72 74 28 20 70 43 6f 6c 6c 21 3d 30 20 29  sert( pColl!=0 )
2e98e 3b 20 20 2f 2a 20 45 76 65 72 79 20 6e 6f 6e 2d  ;  /* Every non-
2e98f 49 50 4b 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61  IPK column has a
2e990 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2e991 6e 63 65 20 2a 2f 0a 20 20 69 66 28 20 28 70 43  nce */.  if( (pC
2e992 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54  oll->type!=SQLIT
2e993 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 7c 7c  E_COLL_BINARY ||
2e994 20 2a 70 6e 6f 43 61 73 65 29 20 26 26 0a 20 20   *pnoCase) &&.  
2e995 20 20 20 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65      (pColl->type
2e996 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f  !=SQLITE_COLL_NO
2e997 43 41 53 45 20 7c 7c 20 21 2a 70 6e 6f 43 61 73  CASE || !*pnoCas
2e998 65 29 20 29 7b 0a 20 20 20 20 2f 2a 20 49 4d 50  e) ){.    /* IMP
2e999 3a 20 52 2d 30 39 30 30 33 2d 33 32 30 34 36 20  : R-09003-32046 
2e99a 46 6f 72 20 74 68 65 20 47 4c 4f 42 20 6f 70 65  For the GLOB ope
2e99b 72 61 74 6f 72 2c 20 74 68 65 20 63 6f 6c 75 6d  rator, the colum
2e99c 6e 20 6d 75 73 74 20 75 73 65 20 74 68 65 0a 20  n must use the. 
2e99d 20 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 42 49     ** default BI
2e99e 4e 41 52 59 20 63 6f 6c 6c 61 74 69 6e 67 20 73  NARY collating s
2e99f 65 71 75 65 6e 63 65 2e 0a 20 20 20 20 2a 2a 20  equence..    ** 
2e9a0 49 4d 50 3a 20 52 2d 34 31 34 30 38 2d 32 38 33  IMP: R-41408-283
2e9a1 30 36 20 46 6f 72 20 74 68 65 20 4c 49 4b 45 20  06 For the LIKE 
2e9a2 6f 70 65 72 61 74 6f 72 2c 20 69 66 20 63 61 73  operator, if cas
2e9a3 65 5f 73 65 6e 73 69 74 69 76 65 5f 6c 69 6b 65  e_sensitive_like
2e9a4 20 6d 6f 64 65 0a 20 20 20 20 2a 2a 20 69 73 20   mode.    ** is 
2e9a5 65 6e 61 62 6c 65 64 20 74 68 65 6e 20 74 68 65  enabled then the
2e9a6 20 63 6f 6c 75 6d 6e 20 6d 75 73 74 20 75 73 65   column must use
2e9a7 20 74 68 65 20 64 65 66 61 75 6c 74 20 42 49 4e   the default BIN
2e9a8 41 52 59 20 63 6f 6c 6c 61 74 69 6e 67 0a 20 20  ARY collating.  
2e9a9 20 20 2a 2a 20 73 65 71 75 65 6e 63 65 2c 20 6f    ** sequence, o
2e9aa 72 20 69 66 20 63 61 73 65 5f 73 65 6e 73 69 74  r if case_sensit
2e9ab 69 76 65 5f 6c 69 6b 65 20 6d 6f 64 65 20 69 73  ive_like mode is
2e9ac 20 64 69 73 61 62 6c 65 64 20 74 68 65 6e 20 74   disabled then t
2e9ad 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a  he column.    **
2e9ae 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 62 75   must use the bu
2e9af 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f  ilt-in NOCASE co
2e9b0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2e9b1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74  ..    */.    ret
2e9b2 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 52  urn 0;.  }..  pR
2e9b3 69 67 68 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b  ight = pList->a[
2e9b4 30 5d 2e 70 45 78 70 72 3b 0a 20 20 6f 70 20 3d  0].pExpr;.  op =
2e9b5 20 70 52 69 67 68 74 2d 3e 6f 70 3b 0a 20 20 69   pRight->op;.  i
2e9b6 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54  f( op==TK_REGIST
2e9b7 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 70  ER ){.    op = p
2e9b8 52 69 67 68 74 2d 3e 6f 70 32 3b 0a 20 20 7d 0a  Right->op2;.  }.
2e9b9 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 56 41 52    if( op==TK_VAR
2e9ba 49 41 42 4c 45 20 29 7b 0a 20 20 20 20 56 64 62  IABLE ){.    Vdb
2e9bb 65 20 2a 70 52 65 70 72 65 70 61 72 65 20 3d 20  e *pReprepare = 
2e9bc 70 50 61 72 73 65 2d 3e 70 52 65 70 72 65 70 61  pParse->pReprepa
2e9bd 72 65 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73  re;.    pVal = s
2e9be 71 6c 69 74 65 33 56 64 62 65 47 65 74 56 61 6c  qlite3VdbeGetVal
2e9bf 75 65 28 70 52 65 70 72 65 70 61 72 65 2c 20 70  ue(pReprepare, p
2e9c0 52 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 2c 20  Right->iColumn, 
2e9c1 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29  SQLITE_AFF_NONE)
2e9c2 3b 0a 20 20 20 20 69 66 28 20 70 56 61 6c 20 26  ;.    if( pVal &
2e9c3 26 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  & sqlite3_value_
2e9c4 74 79 70 65 28 70 56 61 6c 29 3d 3d 53 51 4c 49  type(pVal)==SQLI
2e9c5 54 45 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 20  TE_TEXT ){.     
2e9c6 20 7a 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c   z = (char *)sql
2e9c7 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2e9c8 70 56 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pVal);.    }.   
2e9c9 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 56   sqlite3VdbeSetV
2e9ca 61 72 6d 61 73 6b 28 70 50 61 72 73 65 2d 3e 70  armask(pParse->p
2e9cb 56 64 62 65 2c 20 70 52 69 67 68 74 2d 3e 69 43  Vdbe, pRight->iC
2e9cc 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 61 73 73 65  olumn);.    asse
2e9cd 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d  rt( pRight->op==
2e9ce 54 4b 5f 56 41 52 49 41 42 4c 45 20 7c 7c 20 70  TK_VARIABLE || p
2e9cf 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 52 45  Right->op==TK_RE
2e9d0 47 49 53 54 45 52 20 29 3b 0a 20 20 7d 65 6c 73  GISTER );.  }els
2e9d1 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52  e if( op==TK_STR
2e9d2 49 4e 47 20 29 7b 0a 20 20 20 20 7a 20 3d 20 70  ING ){.    z = p
2e9d3 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  Right->u.zToken;
2e9d4 0a 20 20 7d 0a 20 20 69 66 28 20 7a 20 29 7b 0a  .  }.  if( z ){.
2e9d5 20 20 20 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20      cnt = 0;.   
2e9d6 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b 63 6e 74   while( (c=z[cnt
2e9d7 5d 29 21 3d 30 20 26 26 20 63 21 3d 77 63 5b 30  ])!=0 && c!=wc[0
2e9d8 5d 20 26 26 20 63 21 3d 77 63 5b 31 5d 20 26 26  ] && c!=wc[1] &&
2e9d9 20 63 21 3d 77 63 5b 32 5d 20 29 7b 0a 20 20 20   c!=wc[2] ){.   
2e9da 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a     cnt++;.    }.
2e9db 20 20 20 20 69 66 28 20 63 6e 74 21 3d 30 20 26      if( cnt!=0 &
2e9dc 26 20 63 21 3d 30 20 26 26 20 32 35 35 21 3d 28  & c!=0 && 255!=(
2e9dd 75 38 29 7a 5b 63 6e 74 2d 31 5d 20 29 7b 0a 20  u8)z[cnt-1] ){. 
2e9de 20 20 20 20 20 45 78 70 72 20 2a 70 50 72 65 66       Expr *pPref
2e9df 69 78 3b 0a 20 20 20 20 20 20 2a 70 69 73 43 6f  ix;.      *pisCo
2e9e0 6d 70 6c 65 74 65 20 3d 20 7a 5b 63 6e 74 5d 3d  mplete = z[cnt]=
2e9e1 3d 77 63 5b 30 5d 20 26 26 20 7a 5b 63 6e 74 2b  =wc[0] && z[cnt+
2e9e2 31 5d 3d 3d 30 3b 0a 20 20 20 20 20 20 70 50 72  1]==0;.      pPr
2e9e3 65 66 69 78 20 3d 20 73 71 6c 69 74 65 33 45 78  efix = sqlite3Ex
2e9e4 70 72 28 64 62 2c 20 54 4b 5f 53 54 52 49 4e 47  pr(db, TK_STRING
2e9e5 2c 20 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , z);.      if( 
2e9e6 70 50 72 65 66 69 78 20 29 20 70 50 72 65 66 69  pPrefix ) pPrefi
2e9e7 78 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 63 6e 74 5d  x->u.zToken[cnt]
2e9e8 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a 70 70 50   = 0;.      *ppP
2e9e9 72 65 66 69 78 20 3d 20 70 50 72 65 66 69 78 3b  refix = pPrefix;
2e9ea 0a 20 20 20 20 20 20 69 66 28 20 6f 70 3d 3d 54  .      if( op==T
2e9eb 4b 5f 56 41 52 49 41 42 4c 45 20 29 7b 0a 20 20  K_VARIABLE ){.  
2e9ec 20 20 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20        Vdbe *v = 
2e9ed 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2e9ee 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2e9ef 62 65 53 65 74 56 61 72 6d 61 73 6b 28 76 2c 20  beSetVarmask(v, 
2e9f0 70 52 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 29  pRight->iColumn)
2e9f1 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 70  ;.        if( *p
2e9f2 69 73 43 6f 6d 70 6c 65 74 65 20 26 26 20 70 52  isComplete && pR
2e9f3 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31  ight->u.zToken[1
2e9f4 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  ] ){.          /
2e9f5 2a 20 49 66 20 74 68 65 20 72 68 73 20 6f 66 20  * If the rhs of 
2e9f6 74 68 65 20 4c 49 4b 45 20 65 78 70 72 65 73 73  the LIKE express
2e9f7 69 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c  ion is a variabl
2e9f8 65 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65  e, and the curre
2e9f9 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  nt.          ** 
2e9fa 76 61 6c 75 65 20 6f 66 20 74 68 65 20 76 61 72  value of the var
2e9fb 69 61 62 6c 65 20 6d 65 61 6e 73 20 74 68 65 72  iable means ther
2e9fc 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20  e is no need to 
2e9fd 69 6e 76 6f 6b 65 20 74 68 65 20 4c 49 4b 45 0a  invoke the LIKE.
2e9fe 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e            ** fun
2e9ff 63 74 69 6f 6e 2c 20 74 68 65 6e 20 6e 6f 20 4f  ction, then no O
2ea00 50 5f 56 61 72 69 61 62 6c 65 20 77 69 6c 6c 20  P_Variable will 
2ea01 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  be added to the 
2ea02 70 72 6f 67 72 61 6d 2e 0a 20 20 20 20 20 20 20  program..       
2ea03 20 20 20 2a 2a 20 54 68 69 73 20 63 61 75 73 65     ** This cause
2ea04 73 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 74  s problems for t
2ea05 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  he sqlite3_bind_
2ea06 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29  parameter_name()
2ea07 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 50  .          ** AP
2ea08 49 2e 20 54 6f 20 77 6f 72 6b 61 72 6f 75 6e 64  I. To workaround
2ea09 20 74 68 65 6d 2c 20 61 64 64 20 61 20 64 75 6d   them, add a dum
2ea0a 6d 79 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 68  my OP_Variable h
2ea0b 65 72 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ere..          *
2ea0c 2f 20 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  / .          int
2ea0d 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2ea0e 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2ea0f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2ea10 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
2ea11 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c  (pParse, pRight,
2ea12 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20   r1);.          
2ea13 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2ea14 65 50 33 28 76 2c 20 73 71 6c 69 74 65 33 56 64  eP3(v, sqlite3Vd
2ea15 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2ea16 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  -1, 0);.        
2ea17 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2ea18 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2ea19 72 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  r1);.        }. 
2ea1a 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2ea1b 7b 0a 20 20 20 20 20 20 7a 20 3d 20 30 3b 0a 20  {.      z = 0;. 
2ea1c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
2ea1d 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61  te3ValueFree(pVa
2ea1e 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 7a 21  l);.  return (z!
2ea1f 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  =0);.}.#endif /*
2ea20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b   SQLITE_OMIT_LIK
2ea21 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a  E_OPTIMIZATION *
2ea22 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  /...#ifndef SQLI
2ea23 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2ea24 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  ABLE./*.** Check
2ea25 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 67   to see if the g
2ea26 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  iven expression 
2ea27 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a  is of the form.*
2ea28 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6c  *.**         col
2ea29 75 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 0a 2a  umn MATCH expr.*
2ea2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 74 68  *.** If it is th
2ea2b 65 6e 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20  en return TRUE. 
2ea2c 20 49 66 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20   If not, return 
2ea2d 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63  FALSE..*/.static
2ea2e 20 69 6e 74 20 69 73 4d 61 74 63 68 4f 66 43 6f   int isMatchOfCo
2ea2f 6c 75 6d 6e 28 0a 20 20 45 78 70 72 20 2a 70 45  lumn(.  Expr *pE
2ea30 78 70 72 20 20 20 20 20 20 2f 2a 20 54 65 73 74  xpr      /* Test
2ea31 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   this expression
2ea32 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73   */.){.  ExprLis
2ea33 74 20 2a 70 4c 69 73 74 3b 0a 0a 20 20 69 66 28  t *pList;..  if(
2ea34 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46   pExpr->op!=TK_F
2ea35 55 4e 43 54 49 4f 4e 20 29 7b 0a 20 20 20 20 72  UNCTION ){.    r
2ea36 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
2ea37 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2ea38 70 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  p(pExpr->u.zToke
2ea39 6e 2c 22 6d 61 74 63 68 22 29 21 3d 30 20 29 7b  n,"match")!=0 ){
2ea3a 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2ea3b 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78   }.  pList = pEx
2ea3c 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 69  pr->x.pList;.  i
2ea3d 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21  f( pList->nExpr!
2ea3e 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =2 ){.    return
2ea3f 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c   0;.  }.  if( pL
2ea40 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2d  ist->a[1].pExpr-
2ea41 3e 6f 70 20 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e  >op != TK_COLUMN
2ea42 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2ea43 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  ;.  }.  return 1
2ea44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
2ea45 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2ea46 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  LTABLE */../*.**
2ea47 20 49 66 20 74 68 65 20 70 42 61 73 65 20 65 78   If the pBase ex
2ea48 70 72 65 73 73 69 6f 6e 20 6f 72 69 67 69 6e 61  pression origina
2ea49 74 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72  ted in the ON or
2ea4a 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66   USING clause of
2ea4b 0a 2a 2a 20 61 20 6a 6f 69 6e 2c 20 74 68 65 6e  .** a join, then
2ea4c 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 61 70   transfer the ap
2ea4d 70 72 6f 70 72 69 61 74 65 20 6d 61 72 6b 69 6e  propriate markin
2ea4e 67 73 20 6f 76 65 72 20 74 6f 20 64 65 72 69 76  gs over to deriv
2ea4f 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
2ea50 69 64 20 74 72 61 6e 73 66 65 72 4a 6f 69 6e 4d  id transferJoinM
2ea51 61 72 6b 69 6e 67 73 28 45 78 70 72 20 2a 70 44  arkings(Expr *pD
2ea52 65 72 69 76 65 64 2c 20 45 78 70 72 20 2a 70 42  erived, Expr *pB
2ea53 61 73 65 29 7b 0a 20 20 70 44 65 72 69 76 65 64  ase){.  pDerived
2ea54 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 42 61 73 65  ->flags |= pBase
2ea55 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 46 72 6f  ->flags & EP_Fro
2ea56 6d 4a 6f 69 6e 3b 0a 20 20 70 44 65 72 69 76 65  mJoin;.  pDerive
2ea57 64 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  d->iRightJoinTab
2ea58 6c 65 20 3d 20 70 42 61 73 65 2d 3e 69 52 69 67  le = pBase->iRig
2ea59 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a 7d 0a 0a  htJoinTable;.}..
2ea5a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2ea5b 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49  ITE_OMIT_OR_OPTI
2ea5c 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20 21 64 65  MIZATION) && !de
2ea5d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2ea5e 54 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a  T_SUBQUERY)./*.*
2ea5f 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74 65 72 6d  * Analyze a term
2ea60 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f   that consists o
2ea61 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52  f two or more OR
2ea62 2d 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 73 75  -connected.** su
2ea63 62 74 65 72 6d 73 2e 20 20 53 6f 20 69 6e 3a 0a  bterms.  So in:.
2ea64 2a 2a 0a 2a 2a 20 20 20 20 20 2e 2e 2e 20 57 48  **.**     ... WH
2ea65 45 52 45 20 20 28 61 3d 35 29 20 41 4e 44 20 28  ERE  (a=5) AND (
2ea66 62 3d 37 20 4f 52 20 63 3d 39 20 4f 52 20 64 3d  b=7 OR c=9 OR d=
2ea67 31 33 29 20 41 4e 44 20 28 64 3d 31 33 29 0a 2a  13) AND (d=13).*
2ea68 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2ea69 20 20 20 20 20 20 20 20 20 20 20 5e 5e 5e 5e 5e             ^^^^^
2ea6a 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 0a  ^^^^^^^^^^^^^^^.
2ea6b 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2ea6c 6e 65 20 61 6e 61 6c 79 7a 65 73 20 74 65 72 6d  ne analyzes term
2ea6d 73 20 73 75 63 68 20 61 73 20 74 68 65 20 6d 69  s such as the mi
2ea6e 64 64 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 65  ddle term in the
2ea6f 20 61 62 6f 76 65 20 65 78 61 6d 70 6c 65 2e 0a   above example..
2ea70 2a 2a 20 41 20 57 68 65 72 65 4f 72 54 65 72 6d  ** A WhereOrTerm
2ea71 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6d 70 75   object is compu
2ea72 74 65 64 20 61 6e 64 20 61 74 74 61 63 68 65 64  ted and attached
2ea73 20 74 6f 20 74 68 65 20 74 65 72 6d 20 75 6e 64   to the term und
2ea74 65 72 0a 2a 2a 20 61 6e 61 6c 79 73 69 73 2c 20  er.** analysis, 
2ea75 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  regardless of th
2ea76 65 20 6f 75 74 63 6f 6d 65 20 6f 66 20 74 68 65  e outcome of the
2ea77 20 61 6e 61 6c 79 73 69 73 2e 20 20 48 65 6e 63   analysis.  Henc
2ea78 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57 68 65  e:.**.**     Whe
2ea79 72 65 54 65 72 6d 2e 77 74 46 6c 61 67 73 20 20  reTerm.wtFlags  
2ea7a 20 7c 3d 20 20 54 45 52 4d 5f 4f 52 49 4e 46 4f   |=  TERM_ORINFO
2ea7b 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65 72  .**     WhereTer
2ea7c 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 20 3d 20 20  m.u.pOrInfo  =  
2ea7d 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  a dynamically al
2ea7e 6c 6f 63 61 74 65 64 20 57 68 65 72 65 4f 72 54  located WhereOrT
2ea7f 65 72 6d 20 6f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a  erm object.**.**
2ea80 20 54 68 65 20 74 65 72 6d 20 62 65 69 6e 67 20   The term being 
2ea81 61 6e 61 6c 79 7a 65 64 20 6d 75 73 74 20 68 61  analyzed must ha
2ea82 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f  ve two or more o
2ea83 66 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20 73  f OR-connected s
2ea84 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 41 20 73 69  ubterms..** A si
2ea85 6e 67 6c 65 20 73 75 62 74 65 72 6d 20 6d 69 67  ngle subterm mig
2ea86 68 74 20 62 65 20 61 20 73 65 74 20 6f 66 20 41  ht be a set of A
2ea87 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62  ND-connected sub
2ea88 2d 73 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 45 78  -subterms..** Ex
2ea89 61 6d 70 6c 65 73 20 6f 66 20 74 65 72 6d 73 20  amples of terms 
2ea8a 75 6e 64 65 72 20 61 6e 61 6c 79 73 69 73 3a 0a  under analysis:.
2ea8b 2a 2a 0a 2a 2a 20 20 20 20 20 28 41 29 20 20 20  **.**     (A)   
2ea8c 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20 74    t1.x=t2.y OR t
2ea8d 31 2e 78 3d 74 32 2e 7a 20 4f 52 20 74 31 2e 79  1.x=t2.z OR t1.y
2ea8e 3d 31 35 20 4f 52 20 74 31 2e 7a 3d 74 33 2e 61  =15 OR t1.z=t3.a
2ea8f 2b 35 0a 2a 2a 20 20 20 20 20 28 42 29 20 20 20  +5.**     (B)   
2ea90 20 20 78 3d 65 78 70 72 31 20 4f 52 20 65 78 70    x=expr1 OR exp
2ea91 72 32 3d 78 20 4f 52 20 78 3d 65 78 70 72 33 0a  r2=x OR x=expr3.
2ea92 2a 2a 20 20 20 20 20 28 43 29 20 20 20 20 20 74  **     (C)     t
2ea93 31 2e 78 3d 74 32 2e 79 20 4f 52 20 28 74 31 2e  1.x=t2.y OR (t1.
2ea94 78 3d 74 32 2e 7a 20 41 4e 44 20 74 31 2e 79 3d  x=t2.z AND t1.y=
2ea95 31 35 29 0a 2a 2a 20 20 20 20 20 28 44 29 20 20  15).**     (D)  
2ea96 20 20 20 78 3d 65 78 70 72 31 20 4f 52 20 28 79     x=expr1 OR (y
2ea97 3e 31 31 20 41 4e 44 20 79 3c 32 32 20 41 4e 44  >11 AND y<22 AND
2ea98 20 7a 20 4c 49 4b 45 20 27 2a 68 65 6c 6c 6f 2a   z LIKE '*hello*
2ea99 27 29 0a 2a 2a 20 20 20 20 20 28 45 29 20 20 20  ').**     (E)   
2ea9a 20 20 28 70 2e 61 3d 31 20 41 4e 44 20 71 2e 62    (p.a=1 AND q.b
2ea9b 3d 32 20 41 4e 44 20 72 2e 63 3d 33 29 20 4f 52  =2 AND r.c=3) OR
2ea9c 20 28 70 2e 78 3d 34 20 41 4e 44 20 71 2e 79 3d   (p.x=4 AND q.y=
2ea9d 35 20 41 4e 44 20 72 2e 7a 3d 36 29 0a 2a 2a 0a  5 AND r.z=6).**.
2ea9e 2a 2a 20 43 41 53 45 20 31 3a 0a 2a 2a 0a 2a 2a  ** CASE 1:.**.**
2ea9f 20 49 66 20 61 6c 6c 20 73 75 62 74 65 72 6d 73   If all subterms
2eaa0 20 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d   are of the form
2eaa1 20 54 2e 43 3d 65 78 70 72 20 66 6f 72 20 73 6f   T.C=expr for so
2eaa2 6d 65 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e  me single column
2eaa3 20 6f 66 20 43 0a 2a 2a 20 61 20 73 69 6e 67 6c   of C.** a singl
2eaa4 65 20 74 61 62 6c 65 20 54 20 28 61 73 20 73 68  e table T (as sh
2eaa5 6f 77 6e 20 69 6e 20 65 78 61 6d 70 6c 65 20 42  own in example B
2eaa6 20 61 62 6f 76 65 29 20 74 68 65 6e 20 63 72 65   above) then cre
2eaa7 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61  ate a new virtua
2eaa8 6c 0a 2a 2a 20 74 65 72 6d 20 74 68 61 74 20 69  l.** term that i
2eaa9 73 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20  s an equivalent 
2eaaa 49 4e 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  IN expression.  
2eaab 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
2eaac 69 66 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20 62  if the term.** b
2eaad 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 69 73  eing analyzed is
2eaae 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 3d  :.**.**      x =
2eaaf 20 65 78 70 72 31 20 20 4f 52 20 20 65 78 70 72   expr1  OR  expr
2eab0 32 20 3d 20 78 20 20 4f 52 20 20 78 20 3d 20 65  2 = x  OR  x = e
2eab1 78 70 72 33 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20  xpr3.**.** then 
2eab2 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72  create a new vir
2eab3 74 75 61 6c 20 74 65 72 6d 20 6c 69 6b 65 20 74  tual term like t
2eab4 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
2eab5 78 20 49 4e 20 28 65 78 70 72 31 2c 65 78 70 72  x IN (expr1,expr
2eab6 32 2c 65 78 70 72 33 29 0a 2a 2a 0a 2a 2a 20 43  2,expr3).**.** C
2eab7 41 53 45 20 32 3a 0a 2a 2a 0a 2a 2a 20 49 66 20  ASE 2:.**.** If 
2eab8 61 6c 6c 20 73 75 62 74 65 72 6d 73 20 61 72 65  all subterms are
2eab9 20 69 6e 64 65 78 61 62 6c 65 20 62 79 20 61 20   indexable by a 
2eaba 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 54 2c 20  single table T, 
2eabb 74 68 65 6e 20 73 65 74 0a 2a 2a 0a 2a 2a 20 20  then set.**.**  
2eabc 20 20 20 57 68 65 72 65 54 65 72 6d 2e 65 4f 70     WhereTerm.eOp
2eabd 65 72 61 74 6f 72 20 20 20 20 20 20 20 20 20 20  erator          
2eabe 20 20 20 20 3d 20 20 57 4f 5f 4f 52 0a 2a 2a 20      =  WO_OR.** 
2eabf 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 75 2e      WhereTerm.u.
2eac0 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62  pOrInfo->indexab
2eac1 6c 65 20 20 7c 3d 20 20 74 68 65 20 63 75 72 73  le  |=  the curs
2eac2 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 61  or number for ta
2eac3 62 6c 65 20 54 0a 2a 2a 0a 2a 2a 20 41 20 73 75  ble T.**.** A su
2eac4 62 74 65 72 6d 20 69 73 20 22 69 6e 64 65 78 61  bterm is "indexa
2eac5 62 6c 65 22 20 69 66 20 69 74 20 69 73 20 6f 66  ble" if it is of
2eac6 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 20 22 54 2e   the form.** "T.
2eac7 43 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 77  C <op> <expr>" w
2eac8 68 65 72 65 20 43 20 69 73 20 61 6e 79 20 63 6f  here C is any co
2eac9 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 54 20  lumn of table T 
2eaca 61 6e 64 20 0a 2a 2a 20 3c 6f 70 3e 20 69 73 20  and .** <op> is 
2eacb 6f 6e 65 20 6f 66 20 22 3d 22 2c 20 22 3c 22 2c  one of "=", "<",
2eacc 20 22 3c 3d 22 2c 20 22 3e 22 2c 20 22 3e 3d 22   "<=", ">", ">="
2eacd 2c 20 22 49 53 20 4e 55 4c 4c 22 2c 20 6f 72 20  , "IS NULL", or 
2eace 22 49 4e 22 2e 0a 2a 2a 20 41 20 73 75 62 74 65  "IN"..** A subte
2eacf 72 6d 20 69 73 20 61 6c 73 6f 20 69 6e 64 65 78  rm is also index
2ead0 61 62 6c 65 20 69 66 20 69 74 20 69 73 20 61 6e  able if it is an
2ead1 20 41 4e 44 20 6f 66 20 74 77 6f 20 6f 72 20 6d   AND of two or m
2ead2 6f 72 65 0a 2a 2a 20 73 75 62 73 75 62 74 65 72  ore.** subsubter
2ead3 6d 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  ms at least one 
2ead4 6f 66 20 77 68 69 63 68 20 69 73 20 69 6e 64 65  of which is inde
2ead5 78 61 62 6c 65 2e 20 20 49 6e 64 65 78 61 62 6c  xable.  Indexabl
2ead6 65 20 41 4e 44 20 0a 2a 2a 20 73 75 62 74 65 72  e AND .** subter
2ead7 6d 73 20 68 61 76 65 20 74 68 65 69 72 20 65 4f  ms have their eO
2ead8 70 65 72 61 74 6f 72 20 73 65 74 20 74 6f 20 57  perator set to W
2ead9 4f 5f 41 4e 44 20 61 6e 64 20 74 68 65 79 20 68  O_AND and they h
2eada 61 76 65 0a 2a 2a 20 75 2e 70 41 6e 64 49 6e 66  ave.** u.pAndInf
2eadb 6f 20 73 65 74 20 74 6f 20 61 20 64 79 6e 61 6d  o set to a dynam
2eadc 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
2eadd 20 57 68 65 72 65 41 6e 64 54 65 72 6d 20 6f 62   WhereAndTerm ob
2eade 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 46 72 6f 6d  ject..**.** From
2eadf 20 61 6e 6f 74 68 65 72 20 70 6f 69 6e 74 20 6f   another point o
2eae0 66 20 76 69 65 77 2c 20 22 69 6e 64 65 78 61 62  f view, "indexab
2eae1 6c 65 22 20 6d 65 61 6e 73 20 74 68 61 74 20 74  le" means that t
2eae2 68 65 20 73 75 62 74 65 72 6d 20 63 6f 75 6c 64  he subterm could
2eae3 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  .** potentially 
2eae4 62 65 20 75 73 65 64 20 77 69 74 68 20 61 6e 20  be used with an 
2eae5 69 6e 64 65 78 20 69 66 20 61 6e 20 61 70 70 72  index if an appr
2eae6 6f 70 72 69 61 74 65 20 69 6e 64 65 78 20 65 78  opriate index ex
2eae7 69 73 74 73 2e 0a 2a 2a 20 54 68 69 73 20 61 6e  ists..** This an
2eae8 61 6c 79 73 69 73 20 64 6f 65 73 20 6e 6f 74 20  alysis does not 
2eae9 63 6f 6e 73 69 64 65 72 20 77 68 65 74 68 65 72  consider whether
2eaea 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 65   or not the inde
2eaeb 78 20 65 78 69 73 74 73 3b 20 74 68 61 74 0a 2a  x exists; that.*
2eaec 2a 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 74  * is something t
2eaed 68 65 20 62 65 73 74 49 6e 64 65 78 28 29 20 72  he bestIndex() r
2eaee 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 65 74 65  outine will dete
2eaef 72 6d 69 6e 65 2e 20 20 54 68 69 73 20 61 6e 61  rmine.  This ana
2eaf0 6c 79 73 69 73 0a 2a 2a 20 6f 6e 6c 79 20 6c 6f  lysis.** only lo
2eaf1 6f 6b 73 20 61 74 20 77 68 65 74 68 65 72 20 73  oks at whether s
2eaf2 75 62 74 65 72 6d 73 20 61 70 70 72 6f 70 72 69  ubterms appropri
2eaf3 61 74 65 20 66 6f 72 20 69 6e 64 65 78 69 6e 67  ate for indexing
2eaf4 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c   exist..**.** Al
2eaf5 6c 20 65 78 61 6d 70 6c 65 73 20 41 20 74 68 72  l examples A thr
2eaf6 6f 75 67 68 20 45 20 61 62 6f 76 65 20 61 6c 6c  ough E above all
2eaf7 20 73 61 74 69 73 66 79 20 63 61 73 65 20 32 2e   satisfy case 2.
2eaf8 20 20 42 75 74 20 69 66 20 61 20 74 65 72 6d 0a    But if a term.
2eaf9 2a 2a 20 61 6c 73 6f 20 73 74 61 74 69 73 66 69  ** also statisfi
2eafa 65 73 20 63 61 73 65 20 31 20 28 73 75 63 68 20  es case 1 (such 
2eafb 61 73 20 42 29 20 77 65 20 6b 6e 6f 77 20 74 68  as B) we know th
2eafc 61 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72  at the optimizer
2eafd 20 77 69 6c 6c 0a 2a 2a 20 61 6c 77 61 79 73 20   will.** always 
2eafe 70 72 65 66 65 72 20 63 61 73 65 20 31 2c 20 73  prefer case 1, s
2eaff 6f 20 69 6e 20 74 68 61 74 20 63 61 73 65 20 77  o in that case w
2eb00 65 20 70 72 65 74 65 6e 64 20 74 68 61 74 20 63  e pretend that c
2eb01 61 73 65 20 32 20 69 73 20 6e 6f 74 0a 2a 2a 20  ase 2 is not.** 
2eb02 73 61 74 69 73 66 69 65 64 2e 0a 2a 2a 0a 2a 2a  satisfied..**.**
2eb03 20 49 74 20 6d 69 67 68 74 20 62 65 20 74 68 65   It might be the
2eb04 20 63 61 73 65 20 74 68 61 74 20 6d 75 6c 74 69   case that multi
2eb05 70 6c 65 20 74 61 62 6c 65 73 20 61 72 65 20 69  ple tables are i
2eb06 6e 64 65 78 61 62 6c 65 2e 20 20 46 6f 72 20 65  ndexable.  For e
2eb07 78 61 6d 70 6c 65 2c 0a 2a 2a 20 28 45 29 20 61  xample,.** (E) a
2eb08 62 6f 76 65 20 69 73 20 69 6e 64 65 78 61 62 6c  bove is indexabl
2eb09 65 20 6f 6e 20 74 61 62 6c 65 73 20 50 2c 20 51  e on tables P, Q
2eb0a 2c 20 61 6e 64 20 52 2e 0a 2a 2a 0a 2a 2a 20 54  , and R..**.** T
2eb0b 65 72 6d 73 20 74 68 61 74 20 73 61 74 69 73 66  erms that satisf
2eb0c 79 20 63 61 73 65 20 32 20 61 72 65 20 63 61 6e  y case 2 are can
2eb0d 64 69 64 61 74 65 73 20 66 6f 72 20 6c 6f 6f 6b  didates for look
2eb0e 75 70 20 62 79 20 75 73 69 6e 67 0a 2a 2a 20 73  up by using.** s
2eb0f 65 70 61 72 61 74 65 20 69 6e 64 69 63 65 73 20  eparate indices 
2eb10 74 6f 20 66 69 6e 64 20 72 6f 77 69 64 73 20 66  to find rowids f
2eb11 6f 72 20 65 61 63 68 20 73 75 62 74 65 72 6d 20  or each subterm 
2eb12 61 6e 64 20 63 6f 6d 70 6f 73 69 6e 67 0a 2a 2a  and composing.**
2eb13 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 61 6c   the union of al
2eb14 6c 20 72 6f 77 69 64 73 20 75 73 69 6e 67 20 61  l rowids using a
2eb15 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e 20   RowSet object. 
2eb16 20 54 68 69 73 20 69 73 20 73 69 6d 69 6c 61 72   This is similar
2eb17 0a 2a 2a 20 74 6f 20 22 62 69 74 6d 61 70 20 69  .** to "bitmap i
2eb18 6e 64 69 63 65 73 22 20 69 6e 20 6f 74 68 65 72  ndices" in other
2eb19 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
2eb1a 73 2e 0a 2a 2a 0a 2a 2a 20 4f 54 48 45 52 57 49  s..**.** OTHERWI
2eb1b 53 45 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69  SE:.**.** If nei
2eb1c 74 68 65 72 20 63 61 73 65 20 31 20 6e 6f 72 20  ther case 1 nor 
2eb1d 63 61 73 65 20 32 20 61 70 70 6c 79 2c 20 74 68  case 2 apply, th
2eb1e 65 6e 20 6c 65 61 76 65 20 74 68 65 20 65 4f 70  en leave the eOp
2eb1f 65 72 61 74 6f 72 20 73 65 74 20 74 6f 0a 2a 2a  erator set to.**
2eb20 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 74 65 72   zero.  This ter
2eb21 6d 20 69 73 20 6e 6f 74 20 75 73 65 66 75 6c 20  m is not useful 
2eb22 66 6f 72 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 73  for search..*/.s
2eb23 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41  tatic void exprA
2eb24 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28 0a 20 20  nalyzeOrTerm(.  
2eb25 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20  SrcList *pSrc,  
2eb26 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65            /* the
2eb27 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
2eb28 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2eb29 57 43 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74  WC,         /* t
2eb2a 68 65 20 63 6f 6d 70 6c 65 74 65 20 57 48 45 52  he complete WHER
2eb2b 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  E clause */.  in
2eb2c 74 20 69 64 78 54 65 72 6d 20 20 20 20 20 20 20  t idxTerm       
2eb2d 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2eb2e 20 6f 66 20 74 68 65 20 4f 52 2d 74 65 72 6d 20   of the OR-term 
2eb2f 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a  to be analyzed *
2eb30 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  /.){.  Parse *pP
2eb31 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72  arse = pWC->pPar
2eb32 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  se;            /
2eb33 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
2eb34 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2eb35 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2eb36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2eb37 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2eb38 74 69 6f 6e 20 2a 2f 0a 20 20 57 68 65 72 65 54  tion */.  WhereT
2eb39 65 72 6d 20 2a 70 54 65 72 6d 20 3d 20 26 70 57  erm *pTerm = &pW
2eb3a 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 20 20  C->a[idxTerm];  
2eb3b 20 20 2f 2a 20 54 68 65 20 74 65 72 6d 20 74 6f    /* The term to
2eb3c 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
2eb3d 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20    Expr *pExpr = 
2eb3e 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 20 20 20  pTerm->pExpr;   
2eb3f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2eb40 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74   expression of t
2eb41 68 65 20 74 65 72 6d 20 2a 2f 0a 20 20 57 68 65  he term */.  Whe
2eb42 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
2eb43 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b  Set = pWC->pMask
2eb44 53 65 74 3b 20 2f 2a 20 54 61 62 6c 65 20 75 73  Set; /* Table us
2eb45 65 20 6d 61 73 6b 73 20 2a 2f 0a 20 20 69 6e 74  e masks */.  int
2eb46 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
2eb47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb48 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2eb49 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 65 72 65  nters */.  Where
2eb4a 43 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20  Clause *pOrWc;  
2eb4b 20 20 20 20 20 2f 2a 20 42 72 65 61 6b 75 70 20       /* Breakup 
2eb4c 6f 66 20 70 54 65 72 6d 20 69 6e 74 6f 20 73 75  of pTerm into su
2eb4d 62 74 65 72 6d 73 20 2a 2f 0a 20 20 57 68 65 72  bterms */.  Wher
2eb4e 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 20  eTerm *pOrTerm; 
2eb4f 20 20 20 20 20 20 2f 2a 20 41 20 53 75 62 2d 74        /* A Sub-t
2eb50 65 72 6d 20 77 69 74 68 69 6e 20 74 68 65 20 70  erm within the p
2eb51 4f 72 57 63 20 2a 2f 0a 20 20 57 68 65 72 65 4f  OrWc */.  WhereO
2eb52 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20  rInfo *pOrInfo; 
2eb53 20 20 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e 61      /* Additiona
2eb54 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73  l information as
2eb55 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54  sociated with pT
2eb56 65 72 6d 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  erm */.  Bitmask
2eb57 20 63 68 6e 67 54 6f 49 4e 3b 20 20 20 20 20 20   chngToIN;      
2eb58 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 74 68 61     /* Tables tha
2eb59 74 20 6d 69 67 68 74 20 73 61 74 69 73 66 79 20  t might satisfy 
2eb5a 63 61 73 65 20 31 20 2a 2f 0a 20 20 42 69 74 6d  case 1 */.  Bitm
2eb5b 61 73 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20 20  ask indexable;  
2eb5c 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 73 20        /* Tables 
2eb5d 74 68 61 74 20 61 72 65 20 69 6e 64 65 78 61 62  that are indexab
2eb5e 6c 65 2c 20 73 61 74 69 73 66 79 69 6e 67 20 63  le, satisfying c
2eb5f 61 73 65 20 32 20 2a 2f 0a 0a 20 20 2f 2a 0a 20  ase 2 */..  /*. 
2eb60 20 2a 2a 20 42 72 65 61 6b 20 74 68 65 20 4f 52   ** Break the OR
2eb61 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 69 74 73   clause into its
2eb62 20 73 65 70 61 72 61 74 65 20 73 75 62 74 65 72   separate subter
2eb63 6d 73 2e 20 20 54 68 65 20 73 75 62 74 65 72 6d  ms.  The subterm
2eb64 73 20 61 72 65 0a 20 20 2a 2a 20 73 74 6f 72 65  s are.  ** store
2eb65 64 20 69 6e 20 61 20 57 68 65 72 65 43 6c 61 75  d in a WhereClau
2eb66 73 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  se structure con
2eb67 74 61 69 6e 69 6e 67 20 77 69 74 68 69 6e 20 74  taining within t
2eb68 68 65 20 57 68 65 72 65 4f 72 49 6e 66 6f 0a 20  he WhereOrInfo. 
2eb69 20 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20   ** object that 
2eb6a 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  is attached to t
2eb6b 68 65 20 6f 72 69 67 69 6e 61 6c 20 4f 52 20 63  he original OR c
2eb6c 6c 61 75 73 65 20 74 65 72 6d 2e 0a 20 20 2a 2f  lause term..  */
2eb6d 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72  .  assert( (pTer
2eb6e 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45  m->wtFlags & (TE
2eb6f 52 4d 5f 44 59 4e 41 4d 49 43 7c 54 45 52 4d 5f  RM_DYNAMIC|TERM_
2eb70 4f 52 49 4e 46 4f 7c 54 45 52 4d 5f 41 4e 44 49  ORINFO|TERM_ANDI
2eb71 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73  NFO))==0 );.  as
2eb72 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d  sert( pExpr->op=
2eb73 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 70 54 65 72  =TK_OR );.  pTer
2eb74 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 20 3d 20 70  m->u.pOrInfo = p
2eb75 4f 72 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  OrInfo = sqlite3
2eb76 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2eb77 20 73 69 7a 65 6f 66 28 2a 70 4f 72 49 6e 66 6f   sizeof(*pOrInfo
2eb78 29 29 3b 0a 20 20 69 66 28 20 70 4f 72 49 6e 66  ));.  if( pOrInf
2eb79 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  o==0 ) return;. 
2eb7a 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2eb7b 7c 3d 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 3b 0a  |= TERM_ORINFO;.
2eb7c 20 20 70 4f 72 57 63 20 3d 20 26 70 4f 72 49 6e    pOrWc = &pOrIn
2eb7d 66 6f 2d 3e 77 63 3b 0a 20 20 77 68 65 72 65 43  fo->wc;.  whereC
2eb7e 6c 61 75 73 65 49 6e 69 74 28 70 4f 72 57 63 2c  lauseInit(pOrWc,
2eb7f 20 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d   pWC->pParse, pM
2eb80 61 73 6b 53 65 74 29 3b 0a 20 20 77 68 65 72 65  askSet);.  where
2eb81 53 70 6c 69 74 28 70 4f 72 57 63 2c 20 70 45 78  Split(pOrWc, pEx
2eb82 70 72 2c 20 54 4b 5f 4f 52 29 3b 0a 20 20 65 78  pr, TK_OR);.  ex
2eb83 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72  prAnalyzeAll(pSr
2eb84 63 2c 20 70 4f 72 57 63 29 3b 0a 20 20 69 66 28  c, pOrWc);.  if(
2eb85 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2eb86 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  d ) return;.  as
2eb87 73 65 72 74 28 20 70 4f 72 57 63 2d 3e 6e 54 65  sert( pOrWc->nTe
2eb88 72 6d 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 0a 20  rm>=2 );..  /*. 
2eb89 20 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   ** Compute the 
2eb8a 73 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68  set of tables th
2eb8b 61 74 20 6d 69 67 68 74 20 73 61 74 69 73 66 79  at might satisfy
2eb8c 20 63 61 73 65 73 20 31 20 6f 72 20 32 2e 0a 20   cases 1 or 2.. 
2eb8d 20 2a 2f 0a 20 20 69 6e 64 65 78 61 62 6c 65 20   */.  indexable 
2eb8e 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20  = ~(Bitmask)0;. 
2eb8f 20 63 68 6e 67 54 6f 49 4e 20 3d 20 7e 28 70 57   chngToIN = ~(pW
2eb90 43 2d 3e 76 6d 61 73 6b 29 3b 0a 20 20 66 6f 72  C->vmask);.  for
2eb91 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d  (i=pOrWc->nTerm-
2eb92 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72 57 63  1, pOrTerm=pOrWc
2eb93 2d 3e 61 3b 20 69 3e 3d 30 20 26 26 20 69 6e 64  ->a; i>=0 && ind
2eb94 65 78 61 62 6c 65 3b 20 69 2d 2d 2c 20 70 4f 72  exable; i--, pOr
2eb95 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Term++){.    if(
2eb96 20 28 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72   (pOrTerm->eOper
2eb97 61 74 6f 72 20 26 20 57 4f 5f 53 49 4e 47 4c 45  ator & WO_SINGLE
2eb98 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68  )==0 ){.      Wh
2eb99 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64  ereAndInfo *pAnd
2eb9a 49 6e 66 6f 3b 0a 20 20 20 20 20 20 61 73 73 65  Info;.      asse
2eb9b 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70  rt( pOrTerm->eOp
2eb9c 65 72 61 74 6f 72 3d 3d 30 20 29 3b 0a 20 20 20  erator==0 );.   
2eb9d 20 20 20 61 73 73 65 72 74 28 20 28 70 4f 72 54     assert( (pOrT
2eb9e 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28  erm->wtFlags & (
2eb9f 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 7c 54 45 52  TERM_ANDINFO|TER
2eba0 4d 5f 4f 52 49 4e 46 4f 29 29 3d 3d 30 20 29 3b  M_ORINFO))==0 );
2eba1 0a 20 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20  .      chngToIN 
2eba2 3d 20 30 3b 0a 20 20 20 20 20 20 70 41 6e 64 49  = 0;.      pAndI
2eba3 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  nfo = sqlite3DbM
2eba4 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
2eba5 65 6f 66 28 2a 70 41 6e 64 49 6e 66 6f 29 29 3b  eof(*pAndInfo));
2eba6 0a 20 20 20 20 20 20 69 66 28 20 70 41 6e 64 49  .      if( pAndI
2eba7 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 57  nfo ){.        W
2eba8 68 65 72 65 43 6c 61 75 73 65 20 2a 70 41 6e 64  hereClause *pAnd
2eba9 57 43 3b 0a 20 20 20 20 20 20 20 20 57 68 65 72  WC;.        Wher
2ebaa 65 54 65 72 6d 20 2a 70 41 6e 64 54 65 72 6d 3b  eTerm *pAndTerm;
2ebab 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  .        int j;.
2ebac 20 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20          Bitmask 
2ebad 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  b = 0;.        p
2ebae 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e  OrTerm->u.pAndIn
2ebaf 66 6f 20 3d 20 70 41 6e 64 49 6e 66 6f 3b 0a 20  fo = pAndInfo;. 
2ebb0 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e         pOrTerm->
2ebb1 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2ebb2 41 4e 44 49 4e 46 4f 3b 0a 20 20 20 20 20 20 20  ANDINFO;.       
2ebb3 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2ebb4 74 6f 72 20 3d 20 57 4f 5f 41 4e 44 3b 0a 20 20  tor = WO_AND;.  
2ebb5 20 20 20 20 20 20 70 41 6e 64 57 43 20 3d 20 26        pAndWC = &
2ebb6 70 41 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20  pAndInfo->wc;.  
2ebb7 20 20 20 20 20 20 77 68 65 72 65 43 6c 61 75 73        whereClaus
2ebb8 65 49 6e 69 74 28 70 41 6e 64 57 43 2c 20 70 57  eInit(pAndWC, pW
2ebb9 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b  C->pParse, pMask
2ebba 53 65 74 29 3b 0a 20 20 20 20 20 20 20 20 77 68  Set);.        wh
2ebbb 65 72 65 53 70 6c 69 74 28 70 41 6e 64 57 43 2c  ereSplit(pAndWC,
2ebbc 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2c   pOrTerm->pExpr,
2ebbd 20 54 4b 5f 41 4e 44 29 3b 0a 20 20 20 20 20 20   TK_AND);.      
2ebbe 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c    exprAnalyzeAll
2ebbf 28 70 53 72 63 2c 20 70 41 6e 64 57 43 29 3b 0a  (pSrc, pAndWC);.
2ebc0 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
2ebc1 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2ebc2 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ed );.        if
2ebc3 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
2ebc4 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  led ){.         
2ebc5 20 66 6f 72 28 6a 3d 30 2c 20 70 41 6e 64 54 65   for(j=0, pAndTe
2ebc6 72 6d 3d 70 41 6e 64 57 43 2d 3e 61 3b 20 6a 3c  rm=pAndWC->a; j<
2ebc7 70 41 6e 64 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a  pAndWC->nTerm; j
2ebc8 2b 2b 2c 20 70 41 6e 64 54 65 72 6d 2b 2b 29 7b  ++, pAndTerm++){
2ebc9 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
2ebca 65 72 74 28 20 70 41 6e 64 54 65 72 6d 2d 3e 70  ert( pAndTerm->p
2ebcb 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 20 20  Expr );.        
2ebcc 20 20 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f      if( allowedO
2ebcd 70 28 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70  p(pAndTerm->pExp
2ebce 72 2d 3e 6f 70 29 20 29 7b 0a 20 20 20 20 20 20  r->op) ){.      
2ebcf 20 20 20 20 20 20 20 20 62 20 7c 3d 20 67 65 74          b |= get
2ebd0 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
2ebd1 41 6e 64 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  AndTerm->leftCur
2ebd2 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  sor);.          
2ebd3 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
2ebd4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2ebd5 20 20 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62    indexable &= b
2ebd6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2ebd7 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d  lse if( pOrTerm-
2ebd8 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2ebd9 43 4f 50 49 45 44 20 29 7b 0a 20 20 20 20 20 20  COPIED ){.      
2ebda 2f 2a 20 53 6b 69 70 20 74 68 69 73 20 74 65 72  /* Skip this ter
2ebdb 6d 20 66 6f 72 20 6e 6f 77 2e 20 20 57 65 20 72  m for now.  We r
2ebdc 65 76 69 73 69 74 20 69 74 20 77 68 65 6e 20 77  evisit it when w
2ebdd 65 20 70 72 6f 63 65 73 73 20 74 68 65 0a 20 20  e process the.  
2ebde 20 20 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e      ** correspon
2ebdf 64 69 6e 67 20 54 45 52 4d 5f 56 49 52 54 55 41  ding TERM_VIRTUA
2ebe0 4c 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 7d 65  L term */.    }e
2ebe1 6c 73 65 7b 0a 20 20 20 20 20 20 42 69 74 6d 61  lse{.      Bitma
2ebe2 73 6b 20 62 3b 0a 20 20 20 20 20 20 62 20 3d 20  sk b;.      b = 
2ebe3 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
2ebe4 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  , pOrTerm->leftC
2ebe5 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66  ursor);.      if
2ebe6 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61  ( pOrTerm->wtFla
2ebe7 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41  gs & TERM_VIRTUA
2ebe8 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65  L ){.        Whe
2ebe9 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d  reTerm *pOther =
2ebea 20 26 70 4f 72 57 63 2d 3e 61 5b 70 4f 72 54 65   &pOrWc->a[pOrTe
2ebeb 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20  rm->iParent];.  
2ebec 20 20 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61        b |= getMa
2ebed 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f 74  sk(pMaskSet, pOt
2ebee 68 65 72 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29  her->leftCursor)
2ebef 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2ebf0 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a  indexable &= b;.
2ebf1 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72        if( pOrTer
2ebf2 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f  m->eOperator!=WO
2ebf3 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20 63  _EQ ){.        c
2ebf4 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20  hngToIN = 0;.   
2ebf5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2ebf6 20 20 63 68 6e 67 54 6f 49 4e 20 26 3d 20 62 3b    chngToIN &= b;
2ebf7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2ebf8 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65   }..  /*.  ** Re
2ebf9 63 6f 72 64 20 74 68 65 20 73 65 74 20 6f 66 20  cord the set of 
2ebfa 74 61 62 6c 65 73 20 74 68 61 74 20 73 61 74 69  tables that sati
2ebfb 73 66 79 20 63 61 73 65 20 32 2e 20 20 54 68 65  sfy case 2.  The
2ebfc 20 73 65 74 20 6d 69 67 68 74 20 62 65 0a 20 20   set might be.  
2ebfd 2a 2a 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20  ** empty..  */. 
2ebfe 20 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61   pOrInfo->indexa
2ebff 62 6c 65 20 3d 20 69 6e 64 65 78 61 62 6c 65 3b  ble = indexable;
2ec00 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  .  pTerm->eOpera
2ec01 74 6f 72 20 3d 20 69 6e 64 65 78 61 62 6c 65 3d  tor = indexable=
2ec02 3d 30 20 3f 20 30 20 3a 20 57 4f 5f 4f 52 3b 0a  =0 ? 0 : WO_OR;.
2ec03 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 68 6e 67 54  .  /*.  ** chngT
2ec04 6f 49 4e 20 68 6f 6c 64 73 20 61 20 73 65 74 20  oIN holds a set 
2ec05 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20 2a  of tables that *
2ec06 6d 69 67 68 74 2a 20 73 61 74 69 73 66 79 20 63  might* satisfy c
2ec07 61 73 65 20 31 2e 20 20 42 75 74 0a 20 20 2a 2a  ase 1.  But.  **
2ec08 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73   we have to do s
2ec09 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63  ome additional c
2ec0a 68 65 63 6b 69 6e 67 20 74 6f 20 73 65 65 20 69  hecking to see i
2ec0b 66 20 63 61 73 65 20 31 20 72 65 61 6c 6c 79 0a  f case 1 really.
2ec0c 20 20 2a 2a 20 69 73 20 73 61 74 69 73 66 69 65    ** is satisfie
2ec0d 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 68 6e  d..  **.  ** chn
2ec0e 67 54 6f 49 4e 20 77 69 6c 6c 20 68 6f 6c 64 20  gToIN will hold 
2ec0f 65 69 74 68 65 72 20 30 2c 20 31 2c 20 6f 72 20  either 0, 1, or 
2ec10 32 20 62 69 74 73 2e 20 20 54 68 65 20 30 2d 62  2 bits.  The 0-b
2ec11 69 74 20 63 61 73 65 20 6d 65 61 6e 73 0a 20 20  it case means.  
2ec12 2a 2a 20 74 68 61 74 20 74 68 65 72 65 20 69 73  ** that there is
2ec13 20 6e 6f 20 70 6f 73 73 69 62 69 6c 69 74 79 20   no possibility 
2ec14 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20  of transforming 
2ec15 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 69 6e  the OR clause in
2ec16 74 6f 20 61 6e 0a 20 20 2a 2a 20 49 4e 20 6f 70  to an.  ** IN op
2ec17 65 72 61 74 6f 72 20 62 65 63 61 75 73 65 20 6f  erator because o
2ec18 6e 65 20 6f 72 20 6d 6f 72 65 20 74 65 72 6d 73  ne or more terms
2ec19 20 69 6e 20 74 68 65 20 4f 52 20 63 6c 61 75 73   in the OR claus
2ec1a 65 20 63 6f 6e 74 61 69 6e 0a 20 20 2a 2a 20 73  e contain.  ** s
2ec1b 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74  omething other t
2ec1c 68 61 6e 20 3d 3d 20 6f 6e 20 61 20 63 6f 6c 75  han == on a colu
2ec1d 6d 6e 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65  mn in the single
2ec1e 20 74 61 62 6c 65 2e 20 20 54 68 65 20 31 2d 62   table.  The 1-b
2ec1f 69 74 0a 20 20 2a 2a 20 63 61 73 65 20 6d 65 61  it.  ** case mea
2ec20 6e 73 20 74 68 61 74 20 65 76 65 72 79 20 74 65  ns that every te
2ec21 72 6d 20 6f 66 20 74 68 65 20 4f 52 20 63 6c 61  rm of the OR cla
2ec22 75 73 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f  use is of the fo
2ec23 72 6d 0a 20 20 2a 2a 20 22 74 61 62 6c 65 2e 63  rm.  ** "table.c
2ec24 6f 6c 75 6d 6e 3d 65 78 70 72 22 20 66 6f 72 20  olumn=expr" for 
2ec25 73 6f 6d 65 20 73 69 6e 67 6c 65 20 74 61 62 6c  some single tabl
2ec26 65 2e 20 20 54 68 65 20 6f 6e 65 20 62 69 74 20  e.  The one bit 
2ec27 74 68 61 74 20 69 73 20 73 65 74 0a 20 20 2a 2a  that is set.  **
2ec28 20 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64   will correspond
2ec29 20 74 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 74   to the common t
2ec2a 61 62 6c 65 2e 20 20 57 65 20 73 74 69 6c 6c 20  able.  We still 
2ec2b 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 74 6f  need to check to
2ec2c 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20   make.  ** sure 
2ec2d 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20  the same column 
2ec2e 69 73 20 75 73 65 64 20 6f 6e 20 61 6c 6c 20 74  is used on all t
2ec2f 65 72 6d 73 2e 20 20 54 68 65 20 32 2d 62 69 74  erms.  The 2-bit
2ec30 20 63 61 73 65 20 69 73 20 77 68 65 6e 0a 20 20   case is when.  
2ec31 2a 2a 20 74 68 65 20 61 6c 6c 20 74 65 72 6d 73  ** the all terms
2ec32 20 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d   are of the form
2ec33 20 22 74 61 62 6c 65 31 2e 63 6f 6c 75 6d 6e 3d   "table1.column=
2ec34 74 61 62 6c 65 32 2e 63 6f 6c 75 6d 6e 22 2e 20  table2.column". 
2ec35 20 49 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62   It.  ** might b
2ec36 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 6f  e possible to fo
2ec37 72 6d 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f  rm an IN operato
2ec38 72 20 77 69 74 68 20 65 69 74 68 65 72 20 74 61  r with either ta
2ec39 62 6c 65 31 2e 63 6f 6c 75 6d 6e 0a 20 20 2a 2a  ble1.column.  **
2ec3a 20 6f 72 20 74 61 62 6c 65 32 2e 63 6f 6c 75 6d   or table2.colum
2ec3b 6e 20 61 73 20 74 68 65 20 4c 48 53 20 69 66 20  n as the LHS if 
2ec3c 65 69 74 68 65 72 20 69 73 20 63 6f 6d 6d 6f 6e  either is common
2ec3d 20 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20 6f   to every term o
2ec3e 66 0a 20 20 2a 2a 20 74 68 65 20 4f 52 20 63 6c  f.  ** the OR cl
2ec3f 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ause..  **.  ** 
2ec40 4e 6f 74 65 20 74 68 61 74 20 74 65 72 6d 73 20  Note that terms 
2ec41 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 74 61 62  of the form "tab
2ec42 6c 65 2e 63 6f 6c 75 6d 6e 31 3d 74 61 62 6c 65  le.column1=table
2ec43 2e 63 6f 6c 75 6d 6e 32 22 20 28 74 68 65 0a 20  .column2" (the. 
2ec44 20 2a 2a 20 73 61 6d 65 20 74 61 62 6c 65 20 6f   ** same table o
2ec45 6e 20 62 6f 74 68 20 73 69 7a 65 73 20 6f 66 20  n both sizes of 
2ec46 74 68 65 20 3d 3d 29 20 63 61 6e 6e 6f 74 20 62  the ==) cannot b
2ec47 65 20 6f 70 74 69 6d 69 7a 65 64 2e 0a 20 20 2a  e optimized..  *
2ec48 2f 0a 20 20 69 66 28 20 63 68 6e 67 54 6f 49 4e  /.  if( chngToIN
2ec49 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 6b 54 6f   ){.    int okTo
2ec4a 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 20 20 20  ChngToIN = 0;   
2ec4b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
2ec4c 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 49   conversion to I
2ec4d 4e 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20  N is valid */.  
2ec4e 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 20 3d 20    int iColumn = 
2ec4f 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  -1;         /* C
2ec50 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 6f 6e 20 6c  olumn index on l
2ec51 68 73 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f  hs of IN operato
2ec52 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75  r */.    int iCu
2ec53 72 73 6f 72 20 3d 20 2d 31 3b 20 20 20 20 20 20  rsor = -1;      
2ec54 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73     /* Table curs
2ec55 6f 72 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  or common to all
2ec56 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 69 6e   terms */.    in
2ec57 74 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20  t j = 0;        
2ec58 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2ec59 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 20 20  counter */..    
2ec5a 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20  /* Search for a 
2ec5b 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
2ec5c 20 74 68 61 74 20 61 70 70 65 61 72 73 20 6f 6e   that appears on
2ec5d 20 6f 6e 65 20 73 69 64 65 20 6f 72 20 74 68 65   one side or the
2ec5e 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 6f 66  .    ** other of
2ec5f 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74 6f 72   the == operator
2ec60 20 69 6e 20 65 76 65 72 79 20 73 75 62 74 65 72   in every subter
2ec61 6d 2e 20 20 54 68 61 74 20 74 61 62 6c 65 20 61  m.  That table a
2ec62 6e 64 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a  nd column.    **
2ec63 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 72 64 65   will be recorde
2ec64 64 20 69 6e 20 69 43 75 72 73 6f 72 20 61 6e 64  d in iCursor and
2ec65 20 69 43 6f 6c 75 6d 6e 2e 20 20 54 68 65 72 65   iColumn.  There
2ec66 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 6e   might not be an
2ec67 79 0a 20 20 20 20 2a 2a 20 73 75 63 68 20 74 61  y.    ** such ta
2ec68 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 20  ble and column. 
2ec69 20 53 65 74 20 6f 6b 54 6f 43 68 6e 67 54 6f 49   Set okToChngToI
2ec6a 4e 20 69 66 20 61 6e 20 61 70 70 72 6f 70 72 69  N if an appropri
2ec6b 61 74 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  ate table.    **
2ec6c 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 69 73 20 66   and column is f
2ec6d 6f 75 6e 64 20 62 75 74 20 6c 65 61 76 65 20 6f  ound but leave o
2ec6e 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 66 61 6c 73  kToChngToIN fals
2ec6f 65 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a  e if not found..
2ec70 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a      */.    for(j
2ec71 3d 30 3b 20 6a 3c 32 20 26 26 20 21 6f 6b 54 6f  =0; j<2 && !okTo
2ec72 43 68 6e 67 54 6f 49 4e 3b 20 6a 2b 2b 29 7b 0a  ChngToIN; j++){.
2ec73 20 20 20 20 20 20 70 4f 72 54 65 72 6d 20 3d 20        pOrTerm = 
2ec74 70 4f 72 57 63 2d 3e 61 3b 0a 20 20 20 20 20 20  pOrWc->a;.      
2ec75 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65  for(i=pOrWc->nTe
2ec76 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c  rm-1; i>=0; i--,
2ec77 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20   pOrTerm++){.   
2ec78 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72       assert( pOr
2ec79 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2ec7a 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 20  =WO_EQ );.      
2ec7b 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61    pOrTerm->wtFla
2ec7c 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f  gs &= ~TERM_OR_O
2ec7d 4b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  K;.        if( p
2ec7e 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73  OrTerm->leftCurs
2ec7f 6f 72 3d 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20  or==iCursor ){. 
2ec80 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
2ec81 20 69 73 20 74 68 65 20 32 2d 62 69 74 20 63 61   is the 2-bit ca
2ec82 73 65 20 61 6e 64 20 77 65 20 61 72 65 20 6f 6e  se and we are on
2ec83 20 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72   the second iter
2ec84 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20  ation and.      
2ec85 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 74      ** current t
2ec86 65 72 6d 20 69 73 20 66 72 6f 6d 20 74 68 65 20  erm is from the 
2ec87 66 69 72 73 74 20 69 74 65 72 61 74 69 6f 6e 2e  first iteration.
2ec88 20 20 53 6f 20 73 6b 69 70 20 74 68 69 73 20 74    So skip this t
2ec89 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  erm. */.        
2ec8a 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 31 20 29    assert( j==1 )
2ec8b 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74  ;.          cont
2ec8c 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  inue;.        }.
2ec8d 20 20 20 20 20 20 20 20 69 66 28 20 28 63 68 6e          if( (chn
2ec8e 67 54 6f 49 4e 20 26 20 67 65 74 4d 61 73 6b 28  gToIN & getMask(
2ec8f 70 4d 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72  pMaskSet, pOrTer
2ec90 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 29 3d  m->leftCursor))=
2ec91 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2ec92 2f 2a 20 54 68 69 73 20 74 65 72 6d 20 6d 75 73  /* This term mus
2ec93 74 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  t be of the form
2ec94 20 74 31 2e 61 3d 3d 74 32 2e 62 20 77 68 65 72   t1.a==t2.b wher
2ec95 65 20 74 32 20 69 73 20 69 6e 20 74 68 65 0a 20  e t2 is in the. 
2ec96 20 20 20 20 20 20 20 20 20 2a 2a 20 63 68 6e 67           ** chng
2ec97 54 6f 49 4e 20 73 65 74 20 62 75 74 20 74 31 20  ToIN set but t1 
2ec98 69 73 20 6e 6f 74 2e 20 20 54 68 69 73 20 74 65  is not.  This te
2ec99 72 6d 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65  rm will be eithe
2ec9a 72 20 70 72 65 63 65 65 64 65 64 0a 20 20 20 20  r preceeded.    
2ec9b 20 20 20 20 20 20 2a 2a 20 6f 72 20 66 6f 6c 6c        ** or foll
2ec9c 77 65 64 20 62 79 20 61 6e 20 69 6e 76 65 72 74  wed by an invert
2ec9d 65 64 20 63 6f 70 79 20 28 74 32 2e 62 3d 3d 74  ed copy (t2.b==t
2ec9e 31 2e 61 29 2e 20 20 53 6b 69 70 20 74 68 69 73  1.a).  Skip this
2ec9f 20 74 65 72 6d 20 0a 20 20 20 20 20 20 20 20 20   term .         
2eca0 20 2a 2a 20 61 6e 64 20 75 73 65 20 69 74 73 20   ** and use its 
2eca1 69 6e 76 65 72 73 69 6f 6e 2e 20 2a 2f 0a 20 20  inversion. */.  
2eca2 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
2eca3 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61  ( pOrTerm->wtFla
2eca4 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49 45 44  gs & TERM_COPIED
2eca5 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65   );.          te
2eca6 73 74 63 61 73 65 28 20 70 4f 72 54 65 72 6d 2d  stcase( pOrTerm-
2eca7 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2eca8 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 20  VIRTUAL );.     
2eca9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72       assert( pOr
2ecaa 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2ecab 28 54 45 52 4d 5f 43 4f 50 49 45 44 7c 54 45 52  (TERM_COPIED|TER
2ecac 4d 5f 56 49 52 54 55 41 4c 29 20 29 3b 0a 20 20  M_VIRTUAL) );.  
2ecad 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
2ecae 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2ecaf 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 4f      iColumn = pO
2ecb0 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  rTerm->u.leftCol
2ecb1 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 69 43 75  umn;.        iCu
2ecb2 72 73 6f 72 20 3d 20 70 4f 72 54 65 72 6d 2d 3e  rsor = pOrTerm->
2ecb3 6c 65 66 74 43 75 72 73 6f 72 3b 0a 20 20 20 20  leftCursor;.    
2ecb4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ecb5 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3c 30   }.      if( i<0
2ecb6 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e   ){.        /* N
2ecb7 6f 20 63 61 6e 64 69 64 61 74 65 20 74 61 62 6c  o candidate tabl
2ecb8 65 2b 63 6f 6c 75 6d 6e 20 77 61 73 20 66 6f 75  e+column was fou
2ecb9 6e 64 2e 20 20 54 68 69 73 20 63 61 6e 20 6f 6e  nd.  This can on
2ecba 6c 79 20 6f 63 63 75 72 0a 20 20 20 20 20 20 20  ly occur.       
2ecbb 20 2a 2a 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e   ** on the secon
2ecbc 64 20 69 74 65 72 61 74 69 6f 6e 20 2a 2f 0a 20  d iteration */. 
2ecbd 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a         assert( j
2ecbe 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ==1 );.        a
2ecbf 73 73 65 72 74 28 20 28 63 68 6e 67 54 6f 49 4e  ssert( (chngToIN
2ecc0 26 28 63 68 6e 67 54 6f 49 4e 2d 31 29 29 3d 3d  &(chngToIN-1))==
2ecc1 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  0 );.        ass
2ecc2 65 72 74 28 20 63 68 6e 67 54 6f 49 4e 3d 3d 67  ert( chngToIN==g
2ecc3 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  etMask(pMaskSet,
2ecc4 20 69 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20   iCursor) );.   
2ecc5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ecc6 20 20 7d 0a 20 20 20 20 20 20 74 65 73 74 63 61    }.      testca
2ecc7 73 65 28 20 6a 3d 3d 31 20 29 3b 0a 0a 20 20 20  se( j==1 );..   
2ecc8 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 66 6f     /* We have fo
2ecc9 75 6e 64 20 61 20 63 61 6e 64 69 64 61 74 65 20  und a candidate 
2ecca 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
2eccb 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  .  Check to see 
2eccc 69 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a  if that.      **
2eccd 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
2ecce 6e 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 65  n is common to e
2eccf 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65  very term in the
2ecd0 20 4f 52 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   OR clause */.  
2ecd1 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e      okToChngToIN
2ecd2 20 3d 20 31 3b 0a 20 20 20 20 20 20 66 6f 72 28   = 1;.      for(
2ecd3 3b 20 69 3e 3d 30 20 26 26 20 6f 6b 54 6f 43 68  ; i>=0 && okToCh
2ecd4 6e 67 54 6f 49 4e 3b 20 69 2d 2d 2c 20 70 4f 72  ngToIN; i--, pOr
2ecd5 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Term++){.       
2ecd6 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2ecd7 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2ecd8 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  EQ );.        if
2ecd9 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  ( pOrTerm->leftC
2ecda 75 72 73 6f 72 21 3d 69 43 75 72 73 6f 72 20 29  ursor!=iCursor )
2ecdb 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 54  {.          pOrT
2ecdc 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 3d 20  erm->wtFlags &= 
2ecdd 7e 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20  ~TERM_OR_OK;.   
2ecde 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2ecdf 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f  OrTerm->u.leftCo
2ece0 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b  lumn!=iColumn ){
2ece1 0a 20 20 20 20 20 20 20 20 20 20 6f 6b 54 6f 43  .          okToC
2ece2 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20  hngToIN = 0;.   
2ece3 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2ece4 20 20 20 20 20 20 69 6e 74 20 61 66 66 4c 65 66        int affLef
2ece5 74 2c 20 61 66 66 52 69 67 68 74 3b 0a 20 20 20  t, affRight;.   
2ece6 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
2ece7 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
2ece8 20 69 73 20 61 6c 73 6f 20 61 20 63 6f 6c 75 6d   is also a colum
2ece9 6e 2c 20 74 68 65 6e 20 74 68 65 20 61 66 66 69  n, then the affi
2ecea 6e 69 74 69 65 73 0a 20 20 20 20 20 20 20 20 20  nities.         
2eceb 20 2a 2a 20 6f 66 20 62 6f 74 68 20 72 69 67 68   ** of both righ
2ecec 74 20 61 6e 64 20 6c 65 66 74 20 73 69 64 65 73  t and left sides
2eced 20 6d 75 73 74 20 62 65 20 73 75 63 68 20 74 68   must be such th
2ecee 61 74 20 6e 6f 20 74 79 70 65 0a 20 20 20 20 20  at no type.     
2ecef 20 20 20 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69       ** conversi
2ecf0 6f 6e 73 20 61 72 65 20 72 65 71 75 69 72 65 64  ons are required
2ecf1 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2e 20 20   on the right.  
2ecf2 28 54 69 63 6b 65 74 20 23 32 32 34 39 29 0a 20  (Ticket #2249). 
2ecf3 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
2ecf4 20 20 20 20 20 20 61 66 66 52 69 67 68 74 20 3d        affRight =
2ecf5 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
2ecf6 6e 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45  nity(pOrTerm->pE
2ecf7 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20  xpr->pRight);.  
2ecf8 20 20 20 20 20 20 20 20 61 66 66 4c 65 66 74 20          affLeft 
2ecf9 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  = sqlite3ExprAff
2ecfa 69 6e 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70  inity(pOrTerm->p
2ecfb 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
2ecfc 20 20 20 20 20 20 20 20 69 66 28 20 61 66 66 52          if( affR
2ecfd 69 67 68 74 21 3d 30 20 26 26 20 61 66 66 52 69  ight!=0 && affRi
2ecfe 67 68 74 21 3d 61 66 66 4c 65 66 74 20 29 7b 0a  ght!=affLeft ){.
2ecff 20 20 20 20 20 20 20 20 20 20 20 20 6f 6b 54 6f              okTo
2ed00 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20  ChngToIN = 0;.  
2ed01 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2ed02 20 20 20 20 20 20 20 20 20 20 20 70 4f 72 54 65             pOrTe
2ed03 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
2ed04 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20  ERM_OR_OK;.     
2ed05 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2ed06 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2ed07 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70      /* At this p
2ed08 6f 69 6e 74 2c 20 6f 6b 54 6f 43 68 6e 67 54 6f  oint, okToChngTo
2ed09 49 4e 20 69 73 20 74 72 75 65 20 69 66 20 6f 72  IN is true if or
2ed0a 69 67 69 6e 61 6c 20 70 54 65 72 6d 20 73 61 74  iginal pTerm sat
2ed0b 69 73 66 69 65 73 0a 20 20 20 20 2a 2a 20 63 61  isfies.    ** ca
2ed0c 73 65 20 31 2e 20 20 49 6e 20 74 68 61 74 20 63  se 1.  In that c
2ed0d 61 73 65 2c 20 63 6f 6e 73 74 72 75 63 74 20 61  ase, construct a
2ed0e 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65 72   new virtual ter
2ed0f 6d 20 74 68 61 74 20 69 73 20 0a 20 20 20 20 2a  m that is .    *
2ed10 2a 20 70 54 65 72 6d 20 63 6f 6e 76 65 72 74 65  * pTerm converte
2ed11 64 20 69 6e 74 6f 20 61 6e 20 49 4e 20 6f 70 65  d into an IN ope
2ed12 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  rator..    */.  
2ed13 20 20 69 66 28 20 6f 6b 54 6f 43 68 6e 67 54 6f    if( okToChngTo
2ed14 49 4e 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  IN ){.      Expr
2ed15 20 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20 20   *pDup;         
2ed16 20 20 20 2f 2a 20 41 20 74 72 61 6e 73 69 65 6e     /* A transien
2ed17 74 20 64 75 70 6c 69 63 61 74 65 20 65 78 70 72  t duplicate expr
2ed18 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  ession */.      
2ed19 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
2ed1a 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 52 48  = 0;   /* The RH
2ed1b 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72  S of the IN oper
2ed1c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 45 78  ator */.      Ex
2ed1d 70 72 20 2a 70 4c 65 66 74 20 3d 20 30 3b 20 20  pr *pLeft = 0;  
2ed1e 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20       /* The LHS 
2ed1f 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74  of the IN operat
2ed20 6f 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  or */.      Expr
2ed21 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20   *pNew;         
2ed22 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65     /* The comple
2ed23 74 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a  te IN operator *
2ed24 2f 0a 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 70  /..      for(i=p
2ed25 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70  OrWc->nTerm-1, p
2ed26 4f 72 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b  OrTerm=pOrWc->a;
2ed27 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72 54   i>=0; i--, pOrT
2ed28 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  erm++){.        
2ed29 69 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e 77 74  if( (pOrTerm->wt
2ed2a 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 5f  Flags & TERM_OR_
2ed2b 4f 4b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  OK)==0 ) continu
2ed2c 65 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e;.        asser
2ed2d 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65  t( pOrTerm->eOpe
2ed2e 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a  rator==WO_EQ );.
2ed2f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2ed30 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2ed31 73 6f 72 3d 3d 69 43 75 72 73 6f 72 20 29 3b 0a  sor==iCursor );.
2ed32 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2ed33 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43  pOrTerm->u.leftC
2ed34 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29  olumn==iColumn )
2ed35 3b 0a 20 20 20 20 20 20 20 20 70 44 75 70 20 3d  ;.        pDup =
2ed36 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2ed37 64 62 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78  db, pOrTerm->pEx
2ed38 70 72 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a  pr->pRight, 0);.
2ed39 20 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20          pList = 
2ed3a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2ed3b 70 70 65 6e 64 28 70 57 43 2d 3e 70 50 61 72 73  ppend(pWC->pPars
2ed3c 65 2c 20 70 4c 69 73 74 2c 20 70 44 75 70 29 3b  e, pList, pDup);
2ed3d 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d  .        pLeft =
2ed3e 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d   pOrTerm->pExpr-
2ed3f 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 7d 0a  >pLeft;.      }.
2ed40 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c        assert( pL
2ed41 65 66 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  eft!=0 );.      
2ed42 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
2ed43 70 72 44 75 70 28 64 62 2c 20 70 4c 65 66 74 2c  prDup(db, pLeft,
2ed44 20 30 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20   0);.      pNew 
2ed45 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2ed46 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 44  Parse, TK_IN, pD
2ed47 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  up, 0, 0);.     
2ed48 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
2ed49 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b       int idxNew;
2ed4a 0a 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65  .        transfe
2ed4b 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 70 4e  rJoinMarkings(pN
2ed4c 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  ew, pExpr);.    
2ed4d 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2ed4e 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65  rHasProperty(pNe
2ed4f 77 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  w, EP_xIsSelect)
2ed50 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77   );.        pNew
2ed51 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73  ->x.pList = pLis
2ed52 74 3b 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65  t;.        idxNe
2ed53 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49  w = whereClauseI
2ed54 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 2c  nsert(pWC, pNew,
2ed55 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45   TERM_VIRTUAL|TE
2ed56 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  RM_DYNAMIC);.   
2ed57 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2ed58 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20  dxNew==0 );.    
2ed59 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28      exprAnalyze(
2ed5a 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65  pSrc, pWC, idxNe
2ed5b 77 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72  w);.        pTer
2ed5c 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
2ed5d 65 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 57  erm];.        pW
2ed5e 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61  C->a[idxNew].iPa
2ed5f 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a  rent = idxTerm;.
2ed60 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e          pTerm->n
2ed61 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20  Child = 1;.     
2ed62 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2ed63 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2ed64 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
2ed65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2ed66 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2ed67 20 3d 20 30 3b 20 20 2f 2a 20 63 61 73 65 20 31   = 0;  /* case 1
2ed68 20 74 72 75 6d 70 73 20 63 61 73 65 20 32 20 2a   trumps case 2 *
2ed69 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65  /.    }.  }.}.#e
2ed6a 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
2ed6b 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2ed6c 54 49 4f 4e 20 26 26 20 21 53 51 4c 49 54 45 5f  TION && !SQLITE_
2ed6d 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
2ed6e 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  .../*.** The inp
2ed6f 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ut to this routi
2ed70 6e 65 20 69 73 20 61 6e 20 57 68 65 72 65 54 65  ne is an WhereTe
2ed71 72 6d 20 73 74 72 75 63 74 75 72 65 20 77 69 74  rm structure wit
2ed72 68 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 22 70  h only the.** "p
2ed73 45 78 70 72 22 20 66 69 65 6c 64 20 66 69 6c 6c  Expr" field fill
2ed74 65 64 20 69 6e 2e 20 20 54 68 65 20 6a 6f 62 20  ed in.  The job 
2ed75 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
2ed76 69 73 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68  is to analyze th
2ed77 65 0a 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69  e.** subexpressi
2ed78 6f 6e 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20  on and populate 
2ed79 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 66 69  all the other fi
2ed7a 65 6c 64 73 20 6f 66 20 74 68 65 20 57 68 65 72  elds of the Wher
2ed7b 65 54 65 72 6d 0a 2a 2a 20 73 74 72 75 63 74 75  eTerm.** structu
2ed7c 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  re..**.** If the
2ed7d 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
2ed7e 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 65 78 70  f the form "<exp
2ed7f 72 3e 20 3c 6f 70 3e 20 58 22 20 69 74 20 67 65  r> <op> X" it ge
2ed80 74 73 20 63 6f 6d 6d 75 74 65 64 0a 2a 2a 20 74  ts commuted.** t
2ed81 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 66  o the standard f
2ed82 6f 72 6d 20 6f 66 20 22 58 20 3c 6f 70 3e 20 3c  orm of "X <op> <
2ed83 65 78 70 72 3e 22 2e 0a 2a 2a 0a 2a 2a 20 49 66  expr>"..**.** If
2ed84 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2ed85 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  is of the form "
2ed86 58 20 3c 6f 70 3e 20 59 22 20 77 68 65 72 65 20  X <op> Y" where 
2ed87 62 6f 74 68 20 58 20 61 6e 64 20 59 20 61 72 65  both X and Y are
2ed88 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65  .** columns, the
2ed89 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 65  n the original e
2ed8a 78 70 72 65 73 73 69 6f 6e 20 69 73 20 75 6e 63  xpression is unc
2ed8b 68 61 6e 67 65 64 20 61 6e 64 20 61 20 6e 65 77  hanged and a new
2ed8c 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 65 72 6d   virtual.** term
2ed8d 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 59 20   of the form "Y 
2ed8e 3c 6f 70 3e 20 58 22 20 69 73 20 61 64 64 65 64  <op> X" is added
2ed8f 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c   to the WHERE cl
2ed90 61 75 73 65 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c  ause and.** anal
2ed91 79 7a 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  yzed separately.
2ed92 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74    The original t
2ed93 65 72 6d 20 69 73 20 6d 61 72 6b 65 64 20 77 69  erm is marked wi
2ed94 74 68 20 54 45 52 4d 5f 43 4f 50 49 45 44 0a 2a  th TERM_COPIED.*
2ed95 2a 20 61 6e 64 20 74 68 65 20 6e 65 77 20 74 65  * and the new te
2ed96 72 6d 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74  rm is marked wit
2ed97 68 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 28  h TERM_DYNAMIC (
2ed98 62 65 63 61 75 73 65 20 69 74 27 73 20 70 45 78  because it's pEx
2ed99 70 72 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62  pr.** needs to b
2ed9a 65 20 66 72 65 65 64 20 77 69 74 68 20 74 68 65  e freed with the
2ed9b 20 57 68 65 72 65 43 6c 61 75 73 65 29 20 61 6e   WhereClause) an
2ed9c 64 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 28  d TERM_VIRTUAL (
2ed9d 62 65 63 61 75 73 65 20 69 74 0a 2a 2a 20 69 73  because it.** is
2ed9e 20 61 20 63 6f 6d 6d 75 74 65 64 20 63 6f 70 79   a commuted copy
2ed9f 20 6f 66 20 61 20 70 72 69 6f 72 20 74 65 72 6d   of a prior term
2eda0 2e 29 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c  .)  The original
2eda1 20 74 65 72 6d 20 68 61 73 20 6e 43 68 69 6c 64   term has nChild
2eda2 3d 31 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f  =1.** and the co
2eda3 70 79 20 68 61 73 20 69 64 78 50 61 72 65 6e 74  py has idxParent
2eda4 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65   set to the inde
2eda5 78 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  x of the origina
2eda6 6c 20 74 65 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69  l term..*/.stati
2eda7 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
2eda8 7a 65 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  ze(.  SrcList *p
2eda9 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
2edaa 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* the FROM clau
2edab 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  se */.  WhereCla
2edac 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20  use *pWC,       
2edad 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63    /* the WHERE c
2edae 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
2edaf 64 78 54 65 72 6d 20 20 20 20 20 20 20 20 20 20  dxTerm          
2edb0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
2edb1 20 74 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   the term to be 
2edb2 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20  analyzed */.){. 
2edb3 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2edb4 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
2edb5 20 20 2f 2a 20 54 68 65 20 74 65 72 6d 20 74 6f    /* The term to
2edb6 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
2edb7 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a    WhereMaskSet *
2edb8 70 4d 61 73 6b 53 65 74 3b 20 20 20 20 20 20 20  pMaskSet;       
2edb9 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 74 61 62     /* Set of tab
2edba 6c 65 20 69 6e 64 65 78 20 6d 61 73 6b 73 20 2a  le index masks *
2edbb 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  /.  Expr *pExpr;
2edbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2edbd 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
2edbe 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61  ession to be ana
2edbf 6c 79 7a 65 64 20 2a 2f 0a 20 20 42 69 74 6d 61  lyzed */.  Bitma
2edc0 73 6b 20 70 72 65 72 65 71 4c 65 66 74 3b 20 20  sk prereqLeft;  
2edc1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2edc2 72 65 72 65 71 75 65 73 69 74 65 73 20 6f 66 20  rerequesites of 
2edc3 74 68 65 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  the pExpr->pLeft
2edc4 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72   */.  Bitmask pr
2edc5 65 72 65 71 41 6c 6c 3b 20 20 20 20 20 20 20 20  ereqAll;        
2edc6 20 20 20 20 20 20 20 2f 2a 20 50 72 65 72 65 71         /* Prereq
2edc7 75 65 73 69 74 65 73 20 6f 66 20 70 45 78 70 72  uesites of pExpr
2edc8 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 65 78   */.  Bitmask ex
2edc9 74 72 61 52 69 67 68 74 20 3d 20 30 3b 20 20 20  traRight = 0;   
2edca 20 20 20 20 20 20 20 2f 2a 20 2a 2f 0a 20 20 45         /* */.  E
2edcb 78 70 72 20 2a 70 53 74 72 31 20 3d 20 30 3b 20  xpr *pStr1 = 0; 
2edcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2edcd 2f 2a 20 52 48 53 20 6f 66 20 4c 49 4b 45 2f 47  /* RHS of LIKE/G
2edce 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a  LOB operator */.
2edcf 20 20 69 6e 74 20 69 73 43 6f 6d 70 6c 65 74 65    int isComplete
2edd0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2edd1 20 20 20 2f 2a 20 52 48 53 20 6f 66 20 4c 49 4b     /* RHS of LIK
2edd2 45 2f 47 4c 4f 42 20 65 6e 64 73 20 77 69 74 68  E/GLOB ends with
2edd3 20 77 69 6c 64 63 61 72 64 20 2a 2f 0a 20 20 69   wildcard */.  i
2edd4 6e 74 20 6e 6f 43 61 73 65 20 3d 20 30 3b 20 20  nt noCase = 0;  
2edd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2edd6 2f 2a 20 4c 49 4b 45 2f 47 4c 4f 42 20 64 69 73  /* LIKE/GLOB dis
2edd7 74 69 6e 67 75 69 73 68 65 73 20 63 61 73 65 20  tinguishes case 
2edd8 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20  */.  int op;    
2edd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2edda 20 20 20 20 20 20 2f 2a 20 54 6f 70 2d 6c 65 76        /* Top-lev
2eddb 65 6c 20 6f 70 65 72 61 74 6f 72 2e 20 20 70 45  el operator.  pE
2eddc 78 70 72 2d 3e 6f 70 20 2a 2f 0a 20 20 50 61 72  xpr->op */.  Par
2eddd 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43  se *pParse = pWC
2edde 2d 3e 70 50 61 72 73 65 3b 20 20 20 20 20 2f 2a  ->pParse;     /*
2eddf 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2ede0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2ede1 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2ede2 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2ede3 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2ede4 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ..  if( db->mall
2ede5 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2ede6 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 54  return;.  }.  pT
2ede7 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
2ede8 78 54 65 72 6d 5d 3b 0a 20 20 70 4d 61 73 6b 53  xTerm];.  pMaskS
2ede9 65 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53  et = pWC->pMaskS
2edea 65 74 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 54  et;.  pExpr = pT
2edeb 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 70 72  erm->pExpr;.  pr
2edec 65 72 65 71 4c 65 66 74 20 3d 20 65 78 70 72 54  ereqLeft = exprT
2eded 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2edee 65 74 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  et, pExpr->pLeft
2edef 29 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d  );.  op = pExpr-
2edf0 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54  >op;.  if( op==T
2edf1 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 61 73 73 65  K_IN ){.    asse
2edf2 72 74 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68  rt( pExpr->pRigh
2edf3 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  t==0 );.    if( 
2edf4 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2edf5 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2edf6 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 70 54  ect) ){.      pT
2edf7 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2edf8 20 3d 20 65 78 70 72 53 65 6c 65 63 74 54 61 62   = exprSelectTab
2edf9 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2edfa 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  , pExpr->x.pSele
2edfb 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ct);.    }else{.
2edfc 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65        pTerm->pre
2edfd 72 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 4c  reqRight = exprL
2edfe 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  istTableUsage(pM
2edff 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 78  askSet, pExpr->x
2ee00 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20  .pList);.    }. 
2ee01 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54   }else if( op==T
2ee02 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  K_ISNULL ){.    
2ee03 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
2ee04 68 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ht = 0;.  }else{
2ee05 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72  .    pTerm->prer
2ee06 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 54 61  eqRight = exprTa
2ee07 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2ee08 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
2ee09 29 3b 0a 20 20 7d 0a 20 20 70 72 65 72 65 71 41  );.  }.  prereqA
2ee0a 6c 6c 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73  ll = exprTableUs
2ee0b 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  age(pMaskSet, pE
2ee0c 78 70 72 29 3b 0a 20 20 69 66 28 20 45 78 70 72  xpr);.  if( Expr
2ee0d 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2ee0e 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20  r, EP_FromJoin) 
2ee0f 29 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 78  ){.    Bitmask x
2ee10 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b   = getMask(pMask
2ee11 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 52 69 67  Set, pExpr->iRig
2ee12 68 74 4a 6f 69 6e 54 61 62 6c 65 29 3b 0a 20 20  htJoinTable);.  
2ee13 20 20 70 72 65 72 65 71 41 6c 6c 20 7c 3d 20 78    prereqAll |= x
2ee14 3b 0a 20 20 20 20 65 78 74 72 61 52 69 67 68 74  ;.    extraRight
2ee15 20 3d 20 78 2d 31 3b 20 20 2f 2a 20 4f 4e 20 63   = x-1;  /* ON c
2ee16 6c 61 75 73 65 20 74 65 72 6d 73 20 6d 61 79 20  lause terms may 
2ee17 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 68  not be used with
2ee18 20 61 6e 20 69 6e 64 65 78 0a 20 20 20 20 20 20   an index.      
2ee19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee1a 20 2a 2a 20 6f 6e 20 6c 65 66 74 20 74 61 62 6c   ** on left tabl
2ee1b 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e  e of a LEFT JOIN
2ee1c 2e 20 20 54 69 63 6b 65 74 20 23 33 30 31 35 20  .  Ticket #3015 
2ee1d 2a 2f 0a 20 20 7d 0a 20 20 70 54 65 72 6d 2d 3e  */.  }.  pTerm->
2ee1e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72  prereqAll = prer
2ee1f 65 71 41 6c 6c 3b 0a 20 20 70 54 65 72 6d 2d 3e  eqAll;.  pTerm->
2ee20 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 2d 31 3b  leftCursor = -1;
2ee21 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e  .  pTerm->iParen
2ee22 74 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d  t = -1;.  pTerm-
2ee23 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 0a  >eOperator = 0;.
2ee24 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28    if( allowedOp(
2ee25 6f 70 29 20 26 26 20 28 70 54 65 72 6d 2d 3e 70  op) && (pTerm->p
2ee26 72 65 72 65 71 52 69 67 68 74 20 26 20 70 72 65  rereqRight & pre
2ee27 72 65 71 4c 65 66 74 29 3d 3d 30 20 29 7b 0a 20  reqLeft)==0 ){. 
2ee28 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
2ee29 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
2ee2a 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20     Expr *pRight 
2ee2b 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  = pExpr->pRight;
2ee2c 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  .    if( pLeft->
2ee2d 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b  op==TK_COLUMN ){
2ee2e 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6c 65  .      pTerm->le
2ee2f 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74  ftCursor = pLeft
2ee30 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
2ee31 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  pTerm->u.leftCol
2ee32 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f  umn = pLeft->iCo
2ee33 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 54 65 72  lumn;.      pTer
2ee34 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f  m->eOperator = o
2ee35 70 65 72 61 74 6f 72 4d 61 73 6b 28 6f 70 29 3b  peratorMask(op);
2ee36 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
2ee37 52 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d  Right && pRight-
2ee38 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29  >op==TK_COLUMN )
2ee39 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  {.      WhereTer
2ee3a 6d 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 45  m *pNew;.      E
2ee3b 78 70 72 20 2a 70 44 75 70 3b 0a 20 20 20 20 20  xpr *pDup;.     
2ee3c 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
2ee3d 43 75 72 73 6f 72 3e 3d 30 20 29 7b 0a 20 20 20  Cursor>=0 ){.   
2ee3e 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b       int idxNew;
2ee3f 0a 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20  .        pDup = 
2ee40 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2ee41 62 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20  b, pExpr, 0);.  
2ee42 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61        if( db->ma
2ee43 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2ee44 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2ee45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44  xprDelete(db, pD
2ee46 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  up);.          r
2ee47 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d  eturn;.        }
2ee48 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65 77 20  .        idxNew 
2ee49 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
2ee4a 65 72 74 28 70 57 43 2c 20 70 44 75 70 2c 20 54  ert(pWC, pDup, T
2ee4b 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
2ee4c 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
2ee4d 20 20 20 69 66 28 20 69 64 78 4e 65 77 3d 3d 30     if( idxNew==0
2ee4e 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20   ) return;.     
2ee4f 20 20 20 70 4e 65 77 20 3d 20 26 70 57 43 2d 3e     pNew = &pWC->
2ee50 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20  a[idxNew];.     
2ee51 20 20 20 70 4e 65 77 2d 3e 69 50 61 72 65 6e 74     pNew->iParent
2ee52 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
2ee53 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
2ee54 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20  ->a[idxTerm];.  
2ee55 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68        pTerm->nCh
2ee56 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ild = 1;.       
2ee57 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2ee58 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a  |= TERM_COPIED;.
2ee59 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2ee5a 20 20 20 20 20 70 44 75 70 20 3d 20 70 45 78 70       pDup = pExp
2ee5b 72 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20  r;.        pNew 
2ee5c 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d  = pTerm;.      }
2ee5d 0a 20 20 20 20 20 20 65 78 70 72 43 6f 6d 6d 75  .      exprCommu
2ee5e 74 65 28 70 50 61 72 73 65 2c 20 70 44 75 70 29  te(pParse, pDup)
2ee5f 3b 0a 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20  ;.      pLeft = 
2ee60 70 44 75 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20  pDup->pLeft;.   
2ee61 20 20 20 70 4e 65 77 2d 3e 6c 65 66 74 43 75 72     pNew->leftCur
2ee62 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61  sor = pLeft->iTa
2ee63 62 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  ble;.      pNew-
2ee64 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20  >u.leftColumn = 
2ee65 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pLeft->iColumn;.
2ee66 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 72        pNew->prer
2ee67 65 71 52 69 67 68 74 20 3d 20 70 72 65 72 65 71  eqRight = prereq
2ee68 4c 65 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77  Left;.      pNew
2ee69 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72  ->prereqAll = pr
2ee6a 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 20 20 70  ereqAll;.      p
2ee6b 4e 65 77 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d  New->eOperator =
2ee6c 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b 28 70 44   operatorMask(pD
2ee6d 75 70 2d 3e 6f 70 29 3b 0a 20 20 20 20 7d 0a 20  up->op);.    }. 
2ee6e 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
2ee6f 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f  TE_OMIT_BETWEEN_
2ee70 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f  OPTIMIZATION.  /
2ee71 2a 20 49 66 20 61 20 74 65 72 6d 20 69 73 20 74  * If a term is t
2ee72 68 65 20 42 45 54 57 45 45 4e 20 6f 70 65 72 61  he BETWEEN opera
2ee73 74 6f 72 2c 20 63 72 65 61 74 65 20 74 77 6f 20  tor, create two 
2ee74 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d  new virtual term
2ee75 73 0a 20 20 2a 2a 20 74 68 61 74 20 64 65 66 69  s.  ** that defi
2ee76 6e 65 20 74 68 65 20 72 61 6e 67 65 20 74 68 61  ne the range tha
2ee77 74 20 74 68 65 20 42 45 54 57 45 45 4e 20 69 6d  t the BETWEEN im
2ee78 70 6c 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65  plements.  For e
2ee79 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a  xample:.  **.  *
2ee7a 2a 20 20 20 20 20 20 61 20 42 45 54 57 45 45 4e  *      a BETWEEN
2ee7b 20 62 20 41 4e 44 20 63 0a 20 20 2a 2a 0a 20 20   b AND c.  **.  
2ee7c 2a 2a 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  ** is converted 
2ee7d 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  into:.  **.  ** 
2ee7e 20 20 20 20 20 28 61 20 42 45 54 57 45 45 4e 20       (a BETWEEN 
2ee7f 62 20 41 4e 44 20 63 29 20 41 4e 44 20 28 61 3e  b AND c) AND (a>
2ee80 3d 62 29 20 41 4e 44 20 28 61 3c 3d 63 29 0a 20  =b) AND (a<=c). 
2ee81 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 74 77 6f   **.  ** The two
2ee82 20 6e 65 77 20 74 65 72 6d 73 20 61 72 65 20 61   new terms are a
2ee83 64 64 65 64 20 6f 6e 74 6f 20 74 68 65 20 65 6e  dded onto the en
2ee84 64 20 6f 66 20 74 68 65 20 57 68 65 72 65 43 6c  d of the WhereCl
2ee85 61 75 73 65 20 6f 62 6a 65 63 74 2e 0a 20 20 2a  ause object..  *
2ee86 2a 20 54 68 65 20 6e 65 77 20 74 65 72 6d 73 20  * The new terms 
2ee87 61 72 65 20 22 64 79 6e 61 6d 69 63 22 20 61 6e  are "dynamic" an
2ee88 64 20 61 72 65 20 63 68 69 6c 64 72 65 6e 20 6f  d are children o
2ee89 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 42  f the original B
2ee8a 45 54 57 45 45 4e 0a 20 20 2a 2a 20 74 65 72 6d  ETWEEN.  ** term
2ee8b 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68  .  That means th
2ee8c 61 74 20 69 66 20 74 68 65 20 42 45 54 57 45 45  at if the BETWEE
2ee8d 4e 20 74 65 72 6d 20 69 73 20 63 6f 64 65 64 2c  N term is coded,
2ee8e 20 74 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72   the children ar
2ee8f 65 0a 20 20 2a 2a 20 73 6b 69 70 70 65 64 2e 20  e.  ** skipped. 
2ee90 20 4f 72 2c 20 69 66 20 74 68 65 20 63 68 69 6c   Or, if the chil
2ee91 64 72 65 6e 20 61 72 65 20 73 61 74 69 73 66 69  dren are satisfi
2ee92 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78 2c 20  ed by an index, 
2ee93 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a  the original.  *
2ee94 2a 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69  * BETWEEN term i
2ee95 73 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a  s skipped..  */.
2ee96 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72    else if( pExpr
2ee97 2d 3e 6f 70 3d 3d 54 4b 5f 42 45 54 57 45 45 4e  ->op==TK_BETWEEN
2ee98 20 26 26 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f   && pWC->op==TK_
2ee99 41 4e 44 20 29 7b 0a 20 20 20 20 45 78 70 72 4c  AND ){.    ExprL
2ee9a 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78  ist *pList = pEx
2ee9b 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20  pr->x.pList;.   
2ee9c 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 74 61 74   int i;.    stat
2ee9d 69 63 20 63 6f 6e 73 74 20 75 38 20 6f 70 73 5b  ic const u8 ops[
2ee9e 5d 20 3d 20 7b 54 4b 5f 47 45 2c 20 54 4b 5f 4c  ] = {TK_GE, TK_L
2ee9f 45 7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  E};.    assert( 
2eea0 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20  pList!=0 );.    
2eea1 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e  assert( pList->n
2eea2 45 78 70 72 3d 3d 32 20 29 3b 0a 20 20 20 20 66  Expr==2 );.    f
2eea3 6f 72 28 69 3d 30 3b 20 69 3c 32 3b 20 69 2b 2b  or(i=0; i<2; i++
2eea4 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
2eea5 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20 69  NewExpr;.      i
2eea6 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20  nt idxNew;.     
2eea7 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69   pNewExpr = sqli
2eea8 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2eea9 20 6f 70 73 5b 69 5d 2c 20 0a 20 20 20 20 20 20   ops[i], .      
2eeaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eeab 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2eeac 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2d  prDup(db, pExpr-
2eead 3e 70 4c 65 66 74 2c 20 30 29 2c 0a 20 20 20 20  >pLeft, 0),.    
2eeae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eeaf 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2eeb0 45 78 70 72 44 75 70 28 64 62 2c 20 70 4c 69 73  ExprDup(db, pLis
2eeb1 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 30  t->a[i].pExpr, 0
2eeb2 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 64 78  ), 0);.      idx
2eeb3 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73  New = whereClaus
2eeb4 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65  eInsert(pWC, pNe
2eeb5 77 45 78 70 72 2c 20 54 45 52 4d 5f 56 49 52 54  wExpr, TERM_VIRT
2eeb6 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43  UAL|TERM_DYNAMIC
2eeb7 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2eeb8 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a  e( idxNew==0 );.
2eeb9 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a        exprAnalyz
2eeba 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78  e(pSrc, pWC, idx
2eebb 4e 65 77 29 3b 0a 20 20 20 20 20 20 70 54 65 72  New);.      pTer
2eebc 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
2eebd 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70 57 43 2d  erm];.      pWC-
2eebe 3e 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65  >a[idxNew].iPare
2eebf 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20  nt = idxTerm;.  
2eec0 20 20 7d 0a 20 20 20 20 70 54 65 72 6d 2d 3e 6e    }.    pTerm->n
2eec1 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20 7d 0a 23  Child = 2;.  }.#
2eec2 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2eec3 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54  OMIT_BETWEEN_OPT
2eec4 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69  IMIZATION */..#i
2eec5 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2eec6 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49  E_OMIT_OR_OPTIMI
2eec7 5a 41 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69  ZATION) && !defi
2eec8 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2eec9 53 55 42 51 55 45 52 59 29 0a 20 20 2f 2a 20 41  SUBQUERY).  /* A
2eeca 6e 61 6c 79 7a 65 20 61 20 74 65 72 6d 20 74 68  nalyze a term th
2eecb 61 74 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f  at is composed o
2eecc 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75  f two or more su
2eecd 62 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64  bterms connected
2eece 20 62 79 0a 20 20 2a 2a 20 61 6e 20 4f 52 20 6f   by.  ** an OR o
2eecf 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20  perator..  */.  
2eed0 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e  else if( pExpr->
2eed1 6f 70 3d 3d 54 4b 5f 4f 52 20 29 7b 0a 20 20 20  op==TK_OR ){.   
2eed2 20 61 73 73 65 72 74 28 20 70 57 43 2d 3e 6f 70   assert( pWC->op
2eed3 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20  ==TK_AND );.    
2eed4 65 78 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65 72  exprAnalyzeOrTer
2eed5 6d 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78  m(pSrc, pWC, idx
2eed6 54 65 72 6d 29 3b 0a 20 20 20 20 70 54 65 72 6d  Term);.    pTerm
2eed7 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2eed8 72 6d 5d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  rm];.  }.#endif 
2eed9 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  /* SQLITE_OMIT_O
2eeda 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a  R_OPTIMIZATION *
2eedb 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
2eedc 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49  E_OMIT_LIKE_OPTI
2eedd 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 41 64  MIZATION.  /* Ad
2eede 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f  d constraints to
2eedf 20 72 65 64 75 63 65 20 74 68 65 20 73 65 61 72   reduce the sear
2eee0 63 68 20 73 70 61 63 65 20 6f 6e 20 61 20 4c 49  ch space on a LI
2eee1 4b 45 20 6f 72 20 47 4c 4f 42 0a 20 20 2a 2a 20  KE or GLOB.  ** 
2eee2 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 0a 20  operator..  **. 
2eee3 20 2a 2a 20 41 20 6c 69 6b 65 20 70 61 74 74 65   ** A like patte
2eee4 72 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  rn of the form "
2eee5 78 20 4c 49 4b 45 20 27 61 62 63 25 27 22 20 69  x LIKE 'abc%'" i
2eee6 73 20 63 68 61 6e 67 65 64 20 69 6e 74 6f 20 63  s changed into c
2eee7 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 0a  onstraints.  **.
2eee8 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 78 3e    **          x>
2eee9 3d 27 61 62 63 27 20 41 4e 44 20 78 3c 27 61 62  ='abc' AND x<'ab
2eeea 64 27 20 41 4e 44 20 78 20 4c 49 4b 45 20 27 61  d' AND x LIKE 'a
2eeeb 62 63 25 27 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  bc%'.  **.  ** T
2eeec 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74 65  he last characte
2eeed 72 20 6f 66 20 74 68 65 20 70 72 65 66 69 78 20  r of the prefix 
2eeee 22 61 62 63 22 20 69 73 20 69 6e 63 72 65 6d 65  "abc" is increme
2eeef 6e 74 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65  nted to form the
2eef0 0a 20 20 2a 2a 20 74 65 72 6d 69 6e 61 74 69 6f  .  ** terminatio
2eef1 6e 20 63 6f 6e 64 69 74 69 6f 6e 20 22 61 62 64  n condition "abd
2eef2 22 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57  "..  */.  if( pW
2eef3 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 0a 20  C->op==TK_AND . 
2eef4 20 20 26 26 20 69 73 4c 69 6b 65 4f 72 47 6c 6f    && isLikeOrGlo
2eef5 62 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  b(pParse, pExpr,
2eef6 20 26 70 53 74 72 31 2c 20 26 69 73 43 6f 6d 70   &pStr1, &isComp
2eef7 6c 65 74 65 2c 20 26 6e 6f 43 61 73 65 29 0a 20  lete, &noCase). 
2eef8 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4c   ){.    Expr *pL
2eef9 65 66 74 3b 20 20 20 20 20 20 20 2f 2a 20 4c 48  eft;       /* LH
2eefa 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f  S of LIKE/GLOB o
2eefb 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45  perator */.    E
2eefc 78 70 72 20 2a 70 53 74 72 32 3b 20 20 20 20 20  xpr *pStr2;     
2eefd 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 53 74    /* Copy of pSt
2eefe 72 31 20 2d 20 52 48 53 20 6f 66 20 4c 49 4b 45  r1 - RHS of LIKE
2eeff 2f 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 2a  /GLOB operator *
2ef00 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77  /.    Expr *pNew
2ef01 45 78 70 72 31 3b 0a 20 20 20 20 45 78 70 72 20  Expr1;.    Expr 
2ef02 2a 70 4e 65 77 45 78 70 72 32 3b 0a 20 20 20 20  *pNewExpr2;.    
2ef03 69 6e 74 20 69 64 78 4e 65 77 31 3b 0a 20 20 20  int idxNew1;.   
2ef04 20 69 6e 74 20 69 64 78 4e 65 77 32 3b 0a 0a 20   int idxNew2;.. 
2ef05 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72     pLeft = pExpr
2ef06 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  ->x.pList->a[1].
2ef07 70 45 78 70 72 3b 0a 20 20 20 20 70 53 74 72 32  pExpr;.    pStr2
2ef08 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2ef09 70 28 64 62 2c 20 70 53 74 72 31 2c 20 30 29 3b  p(db, pStr1, 0);
2ef0a 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  .    if( !db->ma
2ef0b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2ef0c 20 20 20 20 75 38 20 63 2c 20 2a 70 43 3b 20 20      u8 c, *pC;  
2ef0d 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 63 68 61       /* Last cha
2ef0e 72 61 63 74 65 72 20 62 65 66 6f 72 65 20 74 68  racter before th
2ef0f 65 20 66 69 72 73 74 20 77 69 6c 64 63 61 72 64  e first wildcard
2ef10 20 2a 2f 0a 20 20 20 20 20 20 70 43 20 3d 20 28   */.      pC = (
2ef11 75 38 2a 29 26 70 53 74 72 32 2d 3e 75 2e 7a 54  u8*)&pStr2->u.zT
2ef12 6f 6b 65 6e 5b 73 71 6c 69 74 65 33 53 74 72 6c  oken[sqlite3Strl
2ef13 65 6e 33 30 28 70 53 74 72 32 2d 3e 75 2e 7a 54  en30(pStr2->u.zT
2ef14 6f 6b 65 6e 29 2d 31 5d 3b 0a 20 20 20 20 20 20  oken)-1];.      
2ef15 63 20 3d 20 2a 70 43 3b 0a 20 20 20 20 20 20 69  c = *pC;.      i
2ef16 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20  f( noCase ){.   
2ef17 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e       /* The poin
2ef18 74 20 69 73 20 74 6f 20 69 6e 63 72 65 6d 65 6e  t is to incremen
2ef19 74 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61  t the last chara
2ef1a 63 74 65 72 20 62 65 66 6f 72 65 20 74 68 65 20  cter before the 
2ef1b 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a  first.        **
2ef1c 20 77 69 6c 64 63 61 72 64 2e 20 20 42 75 74 20   wildcard.  But 
2ef1d 69 66 20 77 65 20 69 6e 63 72 65 6d 65 6e 74 20  if we increment 
2ef1e 27 40 27 2c 20 74 68 61 74 20 77 69 6c 6c 20 70  '@', that will p
2ef1f 75 73 68 20 69 74 20 69 6e 74 6f 20 74 68 65 0a  ush it into the.
2ef20 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 70 68 61          ** alpha
2ef21 62 65 74 69 63 20 72 61 6e 67 65 20 77 68 65 72  betic range wher
2ef22 65 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f  e case conversio
2ef23 6e 73 20 77 69 6c 6c 20 6d 65 73 73 20 75 70 20  ns will mess up 
2ef24 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
2ef25 69 6e 65 71 75 61 6c 69 74 79 2e 20 20 54 6f 20  inequality.  To 
2ef26 61 76 6f 69 64 20 74 68 69 73 2c 20 6d 61 6b 65  avoid this, make
2ef27 20 73 75 72 65 20 74 6f 20 61 6c 73 6f 20 72 75   sure to also ru
2ef28 6e 20 74 68 65 20 66 75 6c 6c 0a 20 20 20 20 20  n the full.     
2ef29 20 20 20 2a 2a 20 4c 49 4b 45 20 6f 6e 20 61 6c     ** LIKE on al
2ef2a 6c 20 63 61 6e 64 69 64 61 74 65 20 65 78 70 72  l candidate expr
2ef2b 65 73 73 69 6f 6e 73 20 62 79 20 63 6c 65 61 72  essions by clear
2ef2c 69 6e 67 20 74 68 65 20 69 73 43 6f 6d 70 6c 65  ing the isComple
2ef2d 74 65 20 66 6c 61 67 0a 20 20 20 20 20 20 20 20  te flag.        
2ef2e 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  */.        if( c
2ef2f 3d 3d 27 41 27 2d 31 20 29 20 69 73 43 6f 6d 70  =='A'-1 ) isComp
2ef30 6c 65 74 65 20 3d 20 30 3b 0a 0a 20 20 20 20 20  lete = 0;..     
2ef31 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 70     c = sqlite3Up
2ef32 70 65 72 54 6f 4c 6f 77 65 72 5b 63 5d 3b 0a 20  perToLower[c];. 
2ef33 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 43       }.      *pC
2ef34 20 3d 20 63 20 2b 20 31 3b 0a 20 20 20 20 7d 0a   = c + 1;.    }.
2ef35 20 20 20 20 70 4e 65 77 45 78 70 72 31 20 3d 20      pNewExpr1 = 
2ef36 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2ef37 72 73 65 2c 20 54 4b 5f 47 45 2c 20 73 71 6c 69  rse, TK_GE, sqli
2ef38 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c  te3ExprDup(db,pL
2ef39 65 66 74 2c 30 29 2c 70 53 74 72 31 2c 30 29 3b  eft,0),pStr1,0);
2ef3a 0a 20 20 20 20 69 64 78 4e 65 77 31 20 3d 20 77  .    idxNew1 = w
2ef3b 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74  hereClauseInsert
2ef3c 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 31 2c  (pWC, pNewExpr1,
2ef3d 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45   TERM_VIRTUAL|TE
2ef3e 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  RM_DYNAMIC);.   
2ef3f 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65   testcase( idxNe
2ef40 77 31 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78 70  w1==0 );.    exp
2ef41 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70  rAnalyze(pSrc, p
2ef42 57 43 2c 20 69 64 78 4e 65 77 31 29 3b 0a 20 20  WC, idxNew1);.  
2ef43 20 20 70 4e 65 77 45 78 70 72 32 20 3d 20 73 71    pNewExpr2 = sq
2ef44 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ef45 65 2c 20 54 4b 5f 4c 54 2c 20 73 71 6c 69 74 65  e, TK_LT, sqlite
2ef46 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66  3ExprDup(db,pLef
2ef47 74 2c 30 29 2c 70 53 74 72 32 2c 30 29 3b 0a 20  t,0),pStr2,0);. 
2ef48 20 20 20 69 64 78 4e 65 77 32 20 3d 20 77 68 65     idxNew2 = whe
2ef49 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70  reClauseInsert(p
2ef4a 57 43 2c 20 70 4e 65 77 45 78 70 72 32 2c 20 54  WC, pNewExpr2, T
2ef4b 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
2ef4c 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74  _DYNAMIC);.    t
2ef4d 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 32  estcase( idxNew2
2ef4e 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41  ==0 );.    exprA
2ef4f 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43  nalyze(pSrc, pWC
2ef50 2c 20 69 64 78 4e 65 77 32 29 3b 0a 20 20 20 20  , idxNew2);.    
2ef51 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
2ef52 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 69 66  idxTerm];.    if
2ef53 28 20 69 73 43 6f 6d 70 6c 65 74 65 20 29 7b 0a  ( isComplete ){.
2ef54 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78        pWC->a[idx
2ef55 4e 65 77 31 5d 2e 69 50 61 72 65 6e 74 20 3d 20  New1].iParent = 
2ef56 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  idxTerm;.      p
2ef57 57 43 2d 3e 61 5b 69 64 78 4e 65 77 32 5d 2e 69  WC->a[idxNew2].i
2ef58 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d  Parent = idxTerm
2ef59 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e  ;.      pTerm->n
2ef5a 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20 20 20 7d  Child = 2;.    }
2ef5b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
2ef5c 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f  QLITE_OMIT_LIKE_
2ef5d 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
2ef5e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ef5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2ef60 45 0a 20 20 2f 2a 20 41 64 64 20 61 20 57 4f 5f  E.  /* Add a WO_
2ef61 4d 41 54 43 48 20 61 75 78 69 6c 69 61 72 79 20  MATCH auxiliary 
2ef62 74 65 72 6d 20 74 6f 20 74 68 65 20 63 6f 6e 73  term to the cons
2ef63 74 72 61 69 6e 74 20 73 65 74 20 69 66 20 74 68  traint set if th
2ef64 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 65  e.  ** current e
2ef65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20  xpression is of 
2ef66 74 68 65 20 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d  the form:  colum
2ef67 6e 20 4d 41 54 43 48 20 65 78 70 72 2e 0a 20 20  n MATCH expr..  
2ef68 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74  ** This informat
2ef69 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74  ion is used by t
2ef6a 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65  he xBestIndex me
2ef6b 74 68 6f 64 73 20 6f 66 0a 20 20 2a 2a 20 76 69  thods of.  ** vi
2ef6c 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 54  rtual tables.  T
2ef6d 68 65 20 6e 61 74 69 76 65 20 71 75 65 72 79 20  he native query 
2ef6e 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65 73 20 6e  optimizer does n
2ef6f 6f 74 20 61 74 74 65 6d 70 74 0a 20 20 2a 2a 20  ot attempt.  ** 
2ef70 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 77  to do anything w
2ef71 69 74 68 20 4d 41 54 43 48 20 66 75 6e 63 74 69  ith MATCH functi
2ef72 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ons..  */.  if( 
2ef73 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28  isMatchOfColumn(
2ef74 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e  pExpr) ){.    in
2ef75 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 45 78  t idxNew;.    Ex
2ef76 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65  pr *pRight, *pLe
2ef77 66 74 3b 0a 20 20 20 20 57 68 65 72 65 54 65 72  ft;.    WhereTer
2ef78 6d 20 2a 70 4e 65 77 54 65 72 6d 3b 0a 20 20 20  m *pNewTerm;.   
2ef79 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 43   Bitmask prereqC
2ef7a 6f 6c 75 6d 6e 2c 20 70 72 65 72 65 71 45 78 70  olumn, prereqExp
2ef7b 72 3b 0a 0a 20 20 20 20 70 52 69 67 68 74 20 3d  r;..    pRight =
2ef7c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
2ef7d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[0].pExpr;.   
2ef7e 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e   pLeft = pExpr->
2ef7f 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  x.pList->a[1].pE
2ef80 78 70 72 3b 0a 20 20 20 20 70 72 65 72 65 71 45  xpr;.    prereqE
2ef81 78 70 72 20 3d 20 65 78 70 72 54 61 62 6c 65 55  xpr = exprTableU
2ef82 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2ef83 52 69 67 68 74 29 3b 0a 20 20 20 20 70 72 65 72  Right);.    prer
2ef84 65 71 43 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 54  eqColumn = exprT
2ef85 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2ef86 65 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  et, pLeft);.    
2ef87 69 66 28 20 28 70 72 65 72 65 71 45 78 70 72 20  if( (prereqExpr 
2ef88 26 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 29 3d  & prereqColumn)=
2ef89 3d 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  =0 ){.      Expr
2ef8a 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20 20 20   *pNewExpr;.    
2ef8b 20 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c    pNewExpr = sql
2ef8c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2ef8d 2c 20 54 4b 5f 4d 41 54 43 48 2c 20 0a 20 20 20  , TK_MATCH, .   
2ef8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef8f 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 73 71             0, sq
2ef90 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2ef91 20 70 52 69 67 68 74 2c 20 30 29 2c 20 30 29 3b   pRight, 0), 0);
2ef92 0a 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20  .      idxNew = 
2ef93 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
2ef94 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 2c  t(pWC, pNewExpr,
2ef95 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45   TERM_VIRTUAL|TE
2ef96 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  RM_DYNAMIC);.   
2ef97 20 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78     testcase( idx
2ef98 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  New==0 );.      
2ef99 70 4e 65 77 54 65 72 6d 20 3d 20 26 70 57 43 2d  pNewTerm = &pWC-
2ef9a 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20  >a[idxNew];.    
2ef9b 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72    pNewTerm->prer
2ef9c 65 71 52 69 67 68 74 20 3d 20 70 72 65 72 65 71  eqRight = prereq
2ef9d 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4e 65 77  Expr;.      pNew
2ef9e 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
2ef9f 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65   = pLeft->iTable
2efa0 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ;.      pNewTerm
2efa1 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d  ->u.leftColumn =
2efa2 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b   pLeft->iColumn;
2efa3 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
2efa4 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f  >eOperator = WO_
2efa5 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 70 4e 65  MATCH;.      pNe
2efa6 77 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d  wTerm->iParent =
2efa7 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20   idxTerm;.      
2efa8 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
2efa9 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20  idxTerm];.      
2efaa 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20  pTerm->nChild = 
2efab 31 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  1;.      pTerm->
2efac 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2efad 43 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 70 4e  COPIED;.      pN
2efae 65 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c  ewTerm->prereqAl
2efaf 6c 20 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65  l = pTerm->prere
2efb0 71 41 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  qAll;.    }.  }.
2efb1 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2efb2 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2efb3 4c 45 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 76  LE */..  /* Prev
2efb4 65 6e 74 20 4f 4e 20 63 6c 61 75 73 65 20 74 65  ent ON clause te
2efb5 72 6d 73 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f  rms of a LEFT JO
2efb6 49 4e 20 66 72 6f 6d 20 62 65 69 6e 67 20 75 73  IN from being us
2efb7 65 64 20 74 6f 20 64 72 69 76 65 0a 20 20 2a 2a  ed to drive.  **
2efb8 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 20 74 61   an index for ta
2efb9 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74  bles to the left
2efba 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20   of the join..  
2efbb 2a 2f 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65 72  */.  pTerm->prer
2efbc 65 71 52 69 67 68 74 20 7c 3d 20 65 78 74 72 61  eqRight |= extra
2efbd 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Right;.}../*.** 
2efbe 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 61  Return TRUE if a
2efbf 6e 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ny of the expres
2efc0 73 69 6f 6e 73 20 69 6e 20 70 4c 69 73 74 2d 3e  sions in pList->
2efc1 61 5b 69 46 69 72 73 74 2e 2e 2e 5d 20 63 6f 6e  a[iFirst...] con
2efc2 74 61 69 6e 0a 2a 2a 20 61 20 72 65 66 65 72 65  tain.** a refere
2efc3 6e 63 65 20 74 6f 20 61 6e 79 20 74 61 62 6c 65  nce to any table
2efc4 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20   other than the 
2efc5 69 42 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  iBase table..*/.
2efc6 73 74 61 74 69 63 20 69 6e 74 20 72 65 66 65 72  static int refer
2efc7 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73  encesOtherTables
2efc8 28 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  (.  ExprList *pL
2efc9 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ist,          /*
2efca 20 53 65 61 72 63 68 20 65 78 70 72 65 73 73 69   Search expressi
2efcb 6f 6e 73 20 69 6e 20 74 68 73 20 6c 69 73 74 20  ons in ths list 
2efcc 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65  */.  WhereMaskSe
2efcd 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 20 20 2f  t *pMaskSet,   /
2efce 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74  * Mapping from t
2efcf 61 62 6c 65 73 20 74 6f 20 62 69 74 6d 61 70 73  ables to bitmaps
2efd0 20 2a 2f 0a 20 20 69 6e 74 20 69 46 69 72 73 74   */.  int iFirst
2efd1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2efd2 2f 2a 20 42 65 20 73 65 61 72 63 68 69 6e 67 20  /* Be searching 
2efd3 77 69 74 68 20 74 68 65 20 69 46 69 72 73 74 2d  with the iFirst-
2efd4 74 68 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  th expression */
2efd5 0a 20 20 69 6e 74 20 69 42 61 73 65 20 20 20 20  .  int iBase    
2efd6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2efd7 49 67 6e 6f 72 65 20 72 65 66 65 72 65 6e 63 65  Ignore reference
2efd8 73 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20  s to this table 
2efd9 2a 2f 0a 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20  */.){.  Bitmask 
2efda 61 6c 6c 6f 77 65 64 20 3d 20 7e 67 65 74 4d 61  allowed = ~getMa
2efdb 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 69 42 61  sk(pMaskSet, iBa
2efdc 73 65 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 46  se);.  while( iF
2efdd 69 72 73 74 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  irst<pList->nExp
2efde 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28 65 78  r ){.    if( (ex
2efdf 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
2efe0 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b  skSet, pList->a[
2efe1 69 46 69 72 73 74 2b 2b 5d 2e 70 45 78 70 72 29  iFirst++].pExpr)
2efe2 26 61 6c 6c 6f 77 65 64 29 21 3d 30 20 29 7b 0a  &allowed)!=0 ){.
2efe3 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2efe4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2efe5 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn 0;.}.../*.** 
2efe6 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 63  This routine dec
2efe7 69 64 65 73 20 69 66 20 70 49 64 78 20 63 61 6e  ides if pIdx can
2efe8 20 62 65 20 75 73 65 64 20 74 6f 20 73 61 74 69   be used to sati
2efe9 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 42 59  sfy the ORDER BY
2efea 0a 2a 2a 20 63 6c 61 75 73 65 2e 20 20 49 66 20  .** clause.  If 
2efeb 69 74 20 63 61 6e 2c 20 69 74 20 72 65 74 75 72  it can, it retur
2efec 6e 73 20 31 2e 20 20 49 66 20 70 49 64 78 20 63  ns 1.  If pIdx c
2efed 61 6e 6e 6f 74 20 73 61 74 69 73 66 79 20 74 68  annot satisfy th
2efee 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 63 6c  e.** ORDER BY cl
2efef 61 75 73 65 2c 20 74 68 69 73 20 72 6f 75 74 69  ause, this routi
2eff0 6e 65 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a  ne returns 0..**
2eff1 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20  .** pOrderBy is 
2eff2 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
2eff3 73 65 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54  se from a SELECT
2eff4 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 54 61   statement.  pTa
2eff5 62 20 69 73 20 74 68 65 0a 2a 2a 20 6c 65 66 74  b is the.** left
2eff6 2d 6d 6f 73 74 20 74 61 62 6c 65 20 69 6e 20 74  -most table in t
2eff7 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
2eff8 66 20 74 68 61 74 20 73 61 6d 65 20 53 45 4c 45  f that same SELE
2eff9 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  CT statement and
2effa 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61  .** the table ha
2effb 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  s a cursor numbe
2effc 72 20 6f 66 20 22 62 61 73 65 22 2e 20 20 70 49  r of "base".  pI
2effd 64 78 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f  dx is an index o
2effe 6e 20 70 54 61 62 2e 0a 2a 2a 0a 2a 2a 20 6e 45  n pTab..**.** nE
2efff 71 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d 62  qCol is the numb
2f000 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66  er of columns of
2f001 20 70 49 64 78 20 74 68 61 74 20 61 72 65 20 75   pIdx that are u
2f002 73 65 64 20 61 73 20 65 71 75 61 6c 69 74 79 0a  sed as equality.
2f003 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ** constraints. 
2f004 20 41 6e 79 20 6f 66 20 74 68 65 73 65 20 63 6f   Any of these co
2f005 6c 75 6d 6e 73 20 6d 61 79 20 62 65 20 6d 69 73  lumns may be mis
2f006 73 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 4f 52  sing from the OR
2f007 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65  DER BY.** clause
2f008 20 61 6e 64 20 74 68 65 20 6d 61 74 63 68 20 63   and the match c
2f009 61 6e 20 73 74 69 6c 6c 20 62 65 20 61 20 73 75  an still be a su
2f00a 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c  ccess..**.** All
2f00b 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52   terms of the OR
2f00c 44 45 52 20 42 59 20 74 68 61 74 20 6d 61 74 63  DER BY that matc
2f00d 68 20 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e  h against the in
2f00e 64 65 78 20 6d 75 73 74 20 62 65 20 65 69 74 68  dex must be eith
2f00f 65 72 0a 2a 2a 20 41 53 43 20 6f 72 20 44 45 53  er.** ASC or DES
2f010 43 2e 20 20 28 54 65 72 6d 73 20 6f 66 20 74 68  C.  (Terms of th
2f011 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2f012 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
2f013 66 20 61 20 55 4e 49 51 55 45 0a 2a 2a 20 69 6e  f a UNIQUE.** in
2f014 64 65 78 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20  dex do not need 
2f015 74 6f 20 73 61 74 69 73 66 79 20 74 68 69 73 20  to satisfy this 
2f016 63 6f 6e 73 74 72 61 69 6e 74 2e 29 20 20 54 68  constraint.)  Th
2f017 65 20 2a 70 62 52 65 76 20 76 61 6c 75 65 20 69  e *pbRev value i
2f018 73 0a 2a 2a 20 73 65 74 20 74 6f 20 31 20 69 66  s.** set to 1 if
2f019 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2f01a 61 75 73 65 20 69 73 20 61 6c 6c 20 44 45 53 43  ause is all DESC
2f01b 20 61 6e 64 20 69 74 20 69 73 20 73 65 74 20 74   and it is set t
2f01c 6f 20 30 20 69 66 0a 2a 2a 20 74 68 65 20 4f 52  o 0 if.** the OR
2f01d 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73  DER BY clause is
2f01e 20 61 6c 6c 20 41 53 43 2e 0a 2a 2f 0a 73 74 61   all ASC..*/.sta
2f01f 74 69 63 20 69 6e 74 20 69 73 53 6f 72 74 69 6e  tic int isSortin
2f020 67 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  gIndex(.  Parse 
2f021 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2f022 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2f023 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d  text */.  WhereM
2f024 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
2f025 2c 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f  , /* Mapping fro
2f026 6d 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6e  m table cursor n
2f027 75 6d 62 65 72 73 20 74 6f 20 62 69 74 6d 61 70  umbers to bitmap
2f028 73 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  s */.  Index *pI
2f029 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  dx,            /
2f02a 2a 20 54 68 65 20 69 6e 64 65 78 20 77 65 20 61  * The index we a
2f02b 72 65 20 74 65 73 74 69 6e 67 20 2a 2f 0a 20 20  re testing */.  
2f02c 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20 20 20  int base,       
2f02d 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
2f02e 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  r number for the
2f02f 20 74 61 62 6c 65 20 74 6f 20 62 65 20 73 6f 72   table to be sor
2f030 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ted */.  ExprLis
2f031 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20  t *pOrderBy,    
2f032 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2f033 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
2f034 20 6e 45 71 43 6f 6c 2c 20 20 20 20 20 20 20 20   nEqCol,        
2f035 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2f036 66 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20  f index columns 
2f037 77 69 74 68 20 3d 3d 20 63 6f 6e 73 74 72 61 69  with == constrai
2f038 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62  nts */.  int *pb
2f039 52 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20  Rev             
2f03a 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 69 66 20   /* Set to 1 if 
2f03b 4f 52 44 45 52 20 42 59 20 69 73 20 44 45 53 43  ORDER BY is DESC
2f03c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
2f03d 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
2f03e 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2f03f 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
2f040 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 30  nt sortOrder = 0
2f041 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2f042 2a 20 58 4f 52 20 6f 66 20 69 6e 64 65 78 20 61  * XOR of index a
2f043 6e 64 20 4f 52 44 45 52 20 42 59 20 73 6f 72 74  nd ORDER BY sort
2f044 20 64 69 72 65 63 74 69 6f 6e 20 2a 2f 0a 20 20   direction */.  
2f045 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20  int nTerm;      
2f046 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f047 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4f 52 44  /* Number of ORD
2f048 45 52 20 42 59 20 74 65 72 6d 73 20 2a 2f 0a 20  ER BY terms */. 
2f049 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2f04a 5f 69 74 65 6d 20 2a 70 54 65 72 6d 3b 20 20 20  _item *pTerm;   
2f04b 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68   /* A term of th
2f04c 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2f04d 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
2f04e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2f04f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 64  ..  assert( pOrd
2f050 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 6e 54 65  erBy!=0 );.  nTe
2f051 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e  rm = pOrderBy->n
2f052 45 78 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20  Expr;.  assert( 
2f053 6e 54 65 72 6d 3e 30 20 29 3b 0a 0a 20 20 2f 2a  nTerm>0 );..  /*
2f054 20 41 72 67 75 6d 65 6e 74 20 70 49 64 78 20 6d   Argument pIdx m
2f055 75 73 74 20 65 69 74 68 65 72 20 70 6f 69 6e 74  ust either point
2f056 20 74 6f 20 61 20 27 72 65 61 6c 27 20 6e 61 6d   to a 'real' nam
2f057 65 64 20 69 6e 64 65 78 20 73 74 72 75 63 74 75  ed index structu
2f058 72 65 2c 20 0a 20 20 2a 2a 20 6f 72 20 61 6e 20  re, .  ** or an 
2f059 69 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20  index structure 
2f05a 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65  allocated on the
2f05b 20 73 74 61 63 6b 20 62 79 20 62 65 73 74 42 74   stack by bestBt
2f05c 72 65 65 49 6e 64 65 78 28 29 20 74 6f 0a 20 20  reeIndex() to.  
2f05d 2a 2a 20 72 65 70 72 65 73 65 6e 74 20 74 68 65  ** represent the
2f05e 20 72 6f 77 69 64 20 69 6e 64 65 78 20 74 68 61   rowid index tha
2f05f 74 20 69 73 20 70 61 72 74 20 6f 66 20 65 76 65  t is part of eve
2f060 72 79 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20  ry table.  */.  
2f061 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 7a 4e  assert( pIdx->zN
2f062 61 6d 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43  ame || (pIdx->nC
2f063 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78  olumn==1 && pIdx
2f064 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 2d  ->aiColumn[0]==-
2f065 31 29 20 29 3b 0a 0a 20 20 2f 2a 20 4d 61 74 63  1) );..  /* Matc
2f066 68 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f  h terms of the O
2f067 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61  RDER BY clause a
2f068 67 61 69 6e 73 74 20 63 6f 6c 75 6d 6e 73 20 6f  gainst columns o
2f069 66 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78  f.  ** the index
2f06a 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ..  **.  ** Note
2f06b 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 68 61   that indices ha
2f06c 76 65 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  ve pIdx->nColumn
2f06d 20 72 65 67 75 6c 61 72 20 63 6f 6c 75 6d 6e 73   regular columns
2f06e 20 70 6c 75 73 0a 20 20 2a 2a 20 6f 6e 65 20 61   plus.  ** one a
2f06f 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e  dditional column
2f070 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2f071 72 6f 77 69 64 2e 20 20 54 68 65 20 72 6f 77 69  rowid.  The rowi
2f072 64 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 66  d column.  ** of
2f073 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 61 6c   the index is al
2f074 73 6f 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61  so allowed to ma
2f075 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 65 20  tch against the 
2f076 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a 20 63 6c  ORDER BY.  ** cl
2f077 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ause..  */.  for
2f078 28 69 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d 70 4f  (i=j=0, pTerm=pO
2f079 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e 54 65  rderBy->a; j<nTe
2f07a 72 6d 20 26 26 20 69 3c 3d 70 49 64 78 2d 3e 6e  rm && i<=pIdx->n
2f07b 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2f07c 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20    Expr *pExpr;  
2f07d 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
2f07e 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 4f 52  ession of the OR
2f07f 44 45 52 20 42 59 20 70 54 65 72 6d 20 2a 2f 0a  DER BY pTerm */.
2f080 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
2f081 6c 6c 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 6f  ll;    /* The co
2f082 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2f083 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a 20 20 20   of pExpr */.   
2f084 20 69 6e 74 20 74 65 72 6d 53 6f 72 74 4f 72 64   int termSortOrd
2f085 65 72 3b 20 2f 2a 20 53 6f 72 74 20 6f 72 64 65  er; /* Sort orde
2f086 72 20 66 6f 72 20 74 68 69 73 20 74 65 72 6d 20  r for this term 
2f087 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75  */.    int iColu
2f088 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  mn;       /* The
2f089 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   i-th column of 
2f08a 74 68 65 20 69 6e 64 65 78 2e 20 20 2d 31 20 66  the index.  -1 f
2f08b 6f 72 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20  or rowid */.    
2f08c 69 6e 74 20 69 53 6f 72 74 4f 72 64 65 72 3b 20  int iSortOrder; 
2f08d 20 20 20 2f 2a 20 31 20 66 6f 72 20 44 45 53 43     /* 1 for DESC
2f08e 2c 20 30 20 66 6f 72 20 41 53 43 20 6f 6e 20 74  , 0 for ASC on t
2f08f 68 65 20 69 2d 74 68 20 69 6e 64 65 78 20 74 65  he i-th index te
2f090 72 6d 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20  rm */.    const 
2f091 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 2f 2a 20  char *zColl; /* 
2f092 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c  Name of the coll
2f093 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
2f094 6f 72 20 69 2d 74 68 20 69 6e 64 65 78 20 74 65  or i-th index te
2f095 72 6d 20 2a 2f 0a 0a 20 20 20 20 70 45 78 70 72  rm */..    pExpr
2f096 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b   = pTerm->pExpr;
2f097 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e  .    if( pExpr->
2f098 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op!=TK_COLUMN ||
2f099 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d   pExpr->iTable!=
2f09a 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  base ){.      /*
2f09b 20 43 61 6e 20 6e 6f 74 20 75 73 65 20 61 6e 20   Can not use an 
2f09c 69 6e 64 65 78 20 73 6f 72 74 20 6f 6e 20 61 6e  index sort on an
2f09d 79 74 68 69 6e 67 20 74 68 61 74 20 69 73 20 6e  ything that is n
2f09e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  ot a column in t
2f09f 68 65 0a 20 20 20 20 20 20 2a 2a 20 6c 65 66 74  he.      ** left
2f0a0 2d 6d 6f 73 74 20 74 61 62 6c 65 20 6f 66 20 74  -most table of t
2f0a1 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
2f0a2 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
2f0a3 20 20 20 7d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d     }.    pColl =
2f0a4 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
2f0a5 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70  Seq(pParse, pExp
2f0a6 72 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f  r);.    if( !pCo
2f0a7 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c  ll ){.      pCol
2f0a8 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  l = db->pDfltCol
2f0a9 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  l;.    }.    if(
2f0aa 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 26 26 20   pIdx->zName && 
2f0ab 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20  i<pIdx->nColumn 
2f0ac 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e  ){.      iColumn
2f0ad 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   = pIdx->aiColum
2f0ae 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  n[i];.      if( 
2f0af 69 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 70  iColumn==pIdx->p
2f0b0 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20 29 7b 0a  Table->iPKey ){.
2f0b1 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20          iColumn 
2f0b2 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = -1;.      }.  
2f0b3 20 20 20 20 69 53 6f 72 74 4f 72 64 65 72 20 3d      iSortOrder =
2f0b4 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65   pIdx->aSortOrde
2f0b5 72 5b 69 5d 3b 0a 20 20 20 20 20 20 7a 43 6f 6c  r[i];.      zCol
2f0b6 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  l = pIdx->azColl
2f0b7 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  [i];.    }else{.
2f0b8 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20        iColumn = 
2f0b9 2d 31 3b 0a 20 20 20 20 20 20 69 53 6f 72 74 4f  -1;.      iSortO
2f0ba 72 64 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  rder = 0;.      
2f0bb 7a 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 2d 3e 7a  zColl = pColl->z
2f0bc 4e 61 6d 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Name;.    }.    
2f0bd 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  if( pExpr->iColu
2f0be 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 7c 7c 20 73  mn!=iColumn || s
2f0bf 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
2f0c0 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  oll->zName, zCol
2f0c1 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  l) ){.      /* T
2f0c2 65 72 6d 20 6a 20 6f 66 20 74 68 65 20 4f 52 44  erm j of the ORD
2f0c3 45 52 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65  ER BY clause doe
2f0c4 73 20 6e 6f 74 20 6d 61 74 63 68 20 63 6f 6c 75  s not match colu
2f0c5 6d 6e 20 69 20 6f 66 20 74 68 65 20 69 6e 64 65  mn i of the inde
2f0c6 78 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69  x */.      if( i
2f0c7 3c 6e 45 71 43 6f 6c 20 29 7b 0a 20 20 20 20 20  <nEqCol ){.     
2f0c8 20 20 20 2f 2a 20 49 66 20 61 6e 20 69 6e 64 65     /* If an inde
2f0c9 78 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73  x column that is
2f0ca 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20   constrained by 
2f0cb 3d 3d 20 66 61 69 6c 73 20 74 6f 20 6d 61 74 63  == fails to matc
2f0cc 68 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  h an.        ** 
2f0cd 4f 52 44 45 52 20 42 59 20 74 65 72 6d 2c 20 74  ORDER BY term, t
2f0ce 68 61 74 20 69 73 20 4f 4b 2e 20 20 4a 75 73 74  hat is OK.  Just
2f0cf 20 69 67 6e 6f 72 65 20 74 68 61 74 20 63 6f 6c   ignore that col
2f0d0 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
2f0d1 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
2f0d2 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
2f0d3 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3d      }else if( i=
2f0d4 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  =pIdx->nColumn )
2f0d5 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  {.        /* Ind
2f0d6 65 78 20 63 6f 6c 75 6d 6e 20 69 20 69 73 20 74  ex column i is t
2f0d7 68 65 20 72 6f 77 69 64 2e 20 20 41 6c 6c 20 6f  he rowid.  All o
2f0d8 74 68 65 72 20 74 65 72 6d 73 20 6d 61 74 63 68  ther terms match
2f0d9 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65  . */.        bre
2f0da 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ak;.      }else{
2f0db 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61  .        /* If a
2f0dc 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 66  n index column f
2f0dd 61 69 6c 73 20 74 6f 20 6d 61 74 63 68 20 61 6e  ails to match an
2f0de 64 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 72 61  d is not constra
2f0df 69 6e 65 64 20 62 79 20 3d 3d 0a 20 20 20 20 20  ined by ==.     
2f0e0 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 69     ** then the i
2f0e1 6e 64 65 78 20 63 61 6e 6e 6f 74 20 73 61 74 69  ndex cannot sati
2f0e2 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 42 59  sfy the ORDER BY
2f0e3 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20   constraint..   
2f0e4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2f0e5 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2f0e6 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
2f0e7 72 74 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f  rt( pIdx->aSortO
2f0e8 72 64 65 72 21 3d 30 20 7c 7c 20 69 43 6f 6c 75  rder!=0 || iColu
2f0e9 6d 6e 3d 3d 2d 31 20 29 3b 0a 20 20 20 20 61 73  mn==-1 );.    as
2f0ea 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 73 6f 72  sert( pTerm->sor
2f0eb 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 70 54 65  tOrder==0 || pTe
2f0ec 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 31  rm->sortOrder==1
2f0ed 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2f0ee 69 53 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c  iSortOrder==0 ||
2f0ef 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29   iSortOrder==1 )
2f0f0 3b 0a 20 20 20 20 74 65 72 6d 53 6f 72 74 4f 72  ;.    termSortOr
2f0f1 64 65 72 20 3d 20 69 53 6f 72 74 4f 72 64 65 72  der = iSortOrder
2f0f2 20 5e 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72   ^ pTerm->sortOr
2f0f3 64 65 72 3b 0a 20 20 20 20 69 66 28 20 69 3e 6e  der;.    if( i>n
2f0f4 45 71 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69  EqCol ){.      i
2f0f5 66 28 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72  f( termSortOrder
2f0f6 21 3d 73 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20  !=sortOrder ){. 
2f0f7 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 65         /* Indice
2f0f8 73 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73  s can only be us
2f0f9 65 64 20 69 66 20 61 6c 6c 20 4f 52 44 45 52 20  ed if all ORDER 
2f0fa 42 59 20 74 65 72 6d 73 20 70 61 73 74 20 74 68  BY terms past th
2f0fb 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 71 75  e.        ** equ
2f0fc 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2f0fd 73 20 61 72 65 20 61 6c 6c 20 65 69 74 68 65 72  s are all either
2f0fe 20 44 45 53 43 20 6f 72 20 41 53 43 2e 20 2a 2f   DESC or ASC. */
2f0ff 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2f100 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
2f101 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 6f 72 74  else{.      sort
2f102 4f 72 64 65 72 20 3d 20 74 65 72 6d 53 6f 72 74  Order = termSort
2f103 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20  Order;.    }.   
2f104 20 6a 2b 2b 3b 0a 20 20 20 20 70 54 65 72 6d 2b   j++;.    pTerm+
2f105 2b 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 75  +;.    if( iColu
2f106 6d 6e 3c 30 20 26 26 20 21 72 65 66 65 72 65 6e  mn<0 && !referen
2f107 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28 70  cesOtherTables(p
2f108 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b 53 65  OrderBy, pMaskSe
2f109 74 2c 20 6a 2c 20 62 61 73 65 29 20 29 7b 0a 20  t, j, base) ){. 
2f10a 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69       /* If the i
2f10b 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 69 73  ndexed column is
2f10c 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
2f10d 20 61 6e 64 20 65 76 65 72 79 74 68 69 6e 67 20   and everything 
2f10e 6d 61 74 63 68 65 73 0a 20 20 20 20 20 20 2a 2a  matches.      **
2f10f 20 73 6f 20 66 61 72 20 61 6e 64 20 6e 6f 6e 65   so far and none
2f110 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2f111 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20 72 69   terms to the ri
2f112 67 68 74 20 72 65 66 65 72 65 6e 63 65 20 6f 74  ght reference ot
2f113 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 61 62  her.      ** tab
2f114 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e 2c  les in the join,
2f115 20 74 68 65 6e 20 77 65 20 61 72 65 20 61 73 73   then we are ass
2f116 75 72 65 64 20 74 68 61 74 20 74 68 65 20 69 6e  ured that the in
2f117 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64 20  dex can be used 
2f118 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 6f 72  .      ** to sor
2f119 74 20 62 65 63 61 75 73 65 20 74 68 65 20 70 72  t because the pr
2f11a 69 6d 61 72 79 20 6b 65 79 20 69 73 20 75 6e 69  imary key is uni
2f11b 71 75 65 20 61 6e 64 20 73 6f 20 6e 6f 6e 65 20  que and so none 
2f11c 6f 66 20 74 68 65 20 6f 74 68 65 72 0a 20 20 20  of the other.   
2f11d 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 77 69     ** columns wi
2f11e 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 64 69 66 66  ll make any diff
2f11f 65 72 65 6e 63 65 0a 20 20 20 20 20 20 2a 2f 0a  erence.      */.
2f120 20 20 20 20 20 20 6a 20 3d 20 6e 54 65 72 6d 3b        j = nTerm;
2f121 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70  .    }.  }..  *p
2f122 62 52 65 76 20 3d 20 73 6f 72 74 4f 72 64 65 72  bRev = sortOrder
2f123 21 3d 30 3b 0a 20 20 69 66 28 20 6a 3e 3d 6e 54  !=0;.  if( j>=nT
2f124 65 72 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c  erm ){.    /* Al
2f125 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f  l terms of the O
2f126 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61  RDER BY clause a
2f127 72 65 20 63 6f 76 65 72 65 64 20 62 79 20 74 68  re covered by th
2f128 69 73 20 69 6e 64 65 78 20 73 6f 0a 20 20 20 20  is index so.    
2f129 2a 2a 20 74 68 69 73 20 69 6e 64 65 78 20 63 61  ** this index ca
2f12a 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 73 6f  n be used for so
2f12b 72 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 65  rting. */.    re
2f12c 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66  turn 1;.  }.  if
2f12d 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21  ( pIdx->onError!
2f12e 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 69 3d 3d 70  =OE_None && i==p
2f12f 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 0a 20 20 20  Idx->nColumn.   
2f130 20 20 20 26 26 20 21 72 65 66 65 72 65 6e 63 65     && !reference
2f131 73 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72  sOtherTables(pOr
2f132 64 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c  derBy, pMaskSet,
2f133 20 6a 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20   j, base) ){.   
2f134 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66   /* All terms of
2f135 20 74 68 69 73 20 69 6e 64 65 78 20 6d 61 74 63   this index matc
2f136 68 20 73 6f 6d 65 20 70 72 65 66 69 78 20 6f 66  h some prefix of
2f137 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2f138 61 75 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20  ause.    ** and 
2f139 74 68 65 20 69 6e 64 65 78 20 69 73 20 55 4e 49  the index is UNI
2f13a 51 55 45 20 61 6e 64 20 6e 6f 20 74 65 72 6d 73  QUE and no terms
2f13b 20 6f 6e 20 74 68 65 20 74 61 69 6c 20 6f 66 20   on the tail of 
2f13c 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 20  the ORDER BY.   
2f13d 20 2a 2a 20 63 6c 61 75 73 65 20 72 65 66 65 72   ** clause refer
2f13e 65 6e 63 65 20 6f 74 68 65 72 20 74 61 62 6c 65  ence other table
2f13f 73 20 69 6e 20 61 20 6a 6f 69 6e 2e 20 20 49 66  s in a join.  If
2f140 20 74 68 69 73 20 69 73 20 61 6c 6c 20 74 72 75   this is all tru
2f141 65 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68  e then.    ** th
2f142 65 20 6f 72 64 65 72 20 62 79 20 63 6c 61 75 73  e order by claus
2f143 65 20 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73  e is superfluous
2f144 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
2f145 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
2f146 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70  0;.}../*.** Prep
2f147 61 72 65 20 61 20 63 72 75 64 65 20 65 73 74 69  are a crude esti
2f148 6d 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 67 61  mate of the loga
2f149 72 69 74 68 6d 20 6f 66 20 74 68 65 20 69 6e 70  rithm of the inp
2f14a 75 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 54 68 65  ut value..** The
2f14b 20 72 65 73 75 6c 74 73 20 6e 65 65 64 20 6e 6f   results need no
2f14c 74 20 62 65 20 65 78 61 63 74 2e 20 20 54 68 69  t be exact.  Thi
2f14d 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66  s is only used f
2f14e 6f 72 20 65 73 74 69 6d 61 74 69 6e 67 0a 2a 2a  or estimating.**
2f14f 20 74 68 65 20 74 6f 74 61 6c 20 63 6f 73 74 20   the total cost 
2f150 6f 66 20 70 65 72 66 6f 72 6d 69 6e 67 20 6f 70  of performing op
2f151 65 72 61 74 69 6f 6e 73 20 77 69 74 68 20 4f 28  erations with O(
2f152 6c 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c 6f 67 4e  logN) or O(NlogN
2f153 29 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69 74 79 2e  ).** complexity.
2f154 20 20 42 65 63 61 75 73 65 20 4e 20 69 73 20 6a    Because N is j
2f155 75 73 74 20 61 20 67 75 65 73 73 2c 20 69 74 20  ust a guess, it 
2f156 69 73 20 6e 6f 20 67 72 65 61 74 20 74 72 61 67  is no great trag
2f157 65 64 79 20 69 66 0a 2a 2a 20 6c 6f 67 4e 20 69  edy if.** logN i
2f158 73 20 61 20 6c 69 74 74 6c 65 20 6f 66 66 2e 0a  s a little off..
2f159 2a 2f 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65  */.static double
2f15a 20 65 73 74 4c 6f 67 28 64 6f 75 62 6c 65 20 4e   estLog(double N
2f15b 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6c 6f 67 4e  ){.  double logN
2f15c 20 3d 20 31 3b 0a 20 20 64 6f 75 62 6c 65 20 78   = 1;.  double x
2f15d 20 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65 28 20   = 10;.  while( 
2f15e 4e 3e 78 20 29 7b 0a 20 20 20 20 6c 6f 67 4e 20  N>x ){.    logN 
2f15f 2b 3d 20 31 3b 0a 20 20 20 20 78 20 2a 3d 20 31  += 1;.    x *= 1
2f160 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
2f161 6c 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  logN;.}../*.** T
2f162 77 6f 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20  wo routines for 
2f163 70 72 69 6e 74 69 6e 67 20 74 68 65 20 63 6f 6e  printing the con
2f164 74 65 6e 74 20 6f 66 20 61 6e 20 73 71 6c 69 74  tent of an sqlit
2f165 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a  e3_index_info.**
2f166 20 73 74 72 75 63 74 75 72 65 2e 20 20 55 73 65   structure.  Use
2f167 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e  d for testing an
2f168 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79  d debugging only
2f169 2e 20 20 49 66 20 6e 65 69 74 68 65 72 0a 2a 2a  .  If neither.**
2f16a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6f 72 20   SQLITE_TEST or 
2f16b 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 72 65  SQLITE_DEBUG are
2f16c 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74   defined, then t
2f16d 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  hese routines.**
2f16e 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a   are no-ops..*/.
2f16f 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2f170 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2f171 54 41 42 4c 45 29 20 26 26 20 64 65 66 69 6e 65  TABLE) && define
2f172 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
2f173 73 74 61 74 69 63 20 76 6f 69 64 20 54 52 41 43  static void TRAC
2f174 45 5f 49 44 58 5f 49 4e 50 55 54 53 28 73 71 6c  E_IDX_INPUTS(sql
2f175 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2f176 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
2f177 69 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72  if( !sqlite3Wher
2f178 65 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b  eTrace ) return;
2f179 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
2f17a 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b  >nConstraint; i+
2f17b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  +){.    sqlite3D
2f17c 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 63 6f  ebugPrintf("  co
2f17d 6e 73 74 72 61 69 6e 74 5b 25 64 5d 3a 20 63 6f  nstraint[%d]: co
2f17e 6c 3d 25 64 20 74 65 72 6d 69 64 3d 25 64 20 6f  l=%d termid=%d o
2f17f 70 3d 25 64 20 75 73 61 62 6c 65 64 3d 25 64 5c  p=%d usabled=%d\
2f180 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20  n",.       i,.  
2f181 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61       p->aConstra
2f182 69 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a  int[i].iColumn,.
2f183 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74         p->aConst
2f184 72 61 69 6e 74 5b 69 5d 2e 69 54 65 72 6d 4f 66  raint[i].iTermOf
2f185 66 73 65 74 2c 0a 20 20 20 20 20 20 20 70 2d 3e  fset,.       p->
2f186 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 6f  aConstraint[i].o
2f187 70 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f  p,.       p->aCo
2f188 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62  nstraint[i].usab
2f189 6c 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  le);.  }.  for(i
2f18a 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 72 64 65 72 42  =0; i<p->nOrderB
2f18b 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  y; i++){.    sql
2f18c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2f18d 22 20 20 6f 72 64 65 72 62 79 5b 25 64 5d 3a 20  "  orderby[%d]: 
2f18e 63 6f 6c 3d 25 64 20 64 65 73 63 3d 25 64 5c 6e  col=%d desc=%d\n
2f18f 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20  ",.       i,.   
2f190 20 20 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b      p->aOrderBy[
2f191 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20  i].iColumn,.    
2f192 20 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69     p->aOrderBy[i
2f193 5d 2e 64 65 73 63 29 3b 0a 20 20 7d 0a 7d 0a 73  ].desc);.  }.}.s
2f194 74 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45  tatic void TRACE
2f195 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 73 71 6c  _IDX_OUTPUTS(sql
2f196 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2f197 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
2f198 69 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72  if( !sqlite3Wher
2f199 65 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b  eTrace ) return;
2f19a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
2f19b 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b  >nConstraint; i+
2f19c 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  +){.    sqlite3D
2f19d 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 75 73  ebugPrintf("  us
2f19e 61 67 65 5b 25 64 5d 3a 20 61 72 67 76 49 64 78  age[%d]: argvIdx
2f19f 3d 25 64 20 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0a  =%d omit=%d\n",.
2f1a0 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20         i,.      
2f1a1 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55   p->aConstraintU
2f1a2 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65  sage[i].argvInde
2f1a3 78 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f  x,.       p->aCo
2f1a4 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d  nstraintUsage[i]
2f1a5 2e 6f 6d 69 74 29 3b 0a 20 20 7d 0a 20 20 73 71  .omit);.  }.  sq
2f1a6 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2f1a7 28 22 20 20 69 64 78 4e 75 6d 3d 25 64 5c 6e 22  ("  idxNum=%d\n"
2f1a8 2c 20 70 2d 3e 69 64 78 4e 75 6d 29 3b 0a 20 20  , p->idxNum);.  
2f1a9 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2f1aa 74 66 28 22 20 20 69 64 78 53 74 72 3d 25 73 5c  tf("  idxStr=%s\
2f1ab 6e 22 2c 20 70 2d 3e 69 64 78 53 74 72 29 3b 0a  n", p->idxStr);.
2f1ac 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2f1ad 69 6e 74 66 28 22 20 20 6f 72 64 65 72 42 79 43  intf("  orderByC
2f1ae 6f 6e 73 75 6d 65 64 3d 25 64 5c 6e 22 2c 20 70  onsumed=%d\n", p
2f1af 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65  ->orderByConsume
2f1b0 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62  d);.  sqlite3Deb
2f1b1 75 67 50 72 69 6e 74 66 28 22 20 20 65 73 74 69  ugPrintf("  esti
2f1b2 6d 61 74 65 64 43 6f 73 74 3d 25 67 5c 6e 22 2c  matedCost=%g\n",
2f1b3 20 70 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73   p->estimatedCos
2f1b4 74 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66  t);.}.#else.#def
2f1b5 69 6e 65 20 54 52 41 43 45 5f 49 44 58 5f 49 4e  ine TRACE_IDX_IN
2f1b6 50 55 54 53 28 41 29 0a 23 64 65 66 69 6e 65 20  PUTS(A).#define 
2f1b7 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54  TRACE_IDX_OUTPUT
2f1b8 53 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  S(A).#endif../* 
2f1b9 0a 2a 2a 20 52 65 71 75 69 72 65 64 20 62 65 63  .** Required bec
2f1ba 61 75 73 65 20 62 65 73 74 49 6e 64 65 78 28 29  ause bestIndex()
2f1bb 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 62 65   is called by be
2f1bc 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28  stOrClauseIndex(
2f1bd 29 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ) .*/.static voi
2f1be 64 20 62 65 73 74 49 6e 64 65 78 28 0a 20 20 20  d bestIndex(.   
2f1bf 20 50 61 72 73 65 2a 2c 20 57 68 65 72 65 43 6c   Parse*, WhereCl
2f1c0 61 75 73 65 2a 2c 20 73 74 72 75 63 74 20 53 72  ause*, struct Sr
2f1c1 63 4c 69 73 74 5f 69 74 65 6d 2a 2c 20 42 69 74  cList_item*, Bit
2f1c2 6d 61 73 6b 2c 20 45 78 70 72 4c 69 73 74 2a 2c  mask, ExprList*,
2f1c3 20 57 68 65 72 65 43 6f 73 74 2a 29 3b 0a 0a 2f   WhereCost*);../
2f1c4 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2f1c5 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 69  e attempts to fi
2f1c6 6e 64 20 61 6e 20 73 63 61 6e 6e 69 6e 67 20 73  nd an scanning s
2f1c7 74 72 61 74 65 67 79 20 74 68 61 74 20 63 61 6e  trategy that can
2f1c8 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 74 6f 20   be used .** to 
2f1c9 6f 70 74 69 6d 69 7a 65 20 61 6e 20 27 4f 52 27  optimize an 'OR'
2f1ca 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
2f1cb 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 57 48   is part of a WH
2f1cc 45 52 45 20 63 6c 61 75 73 65 2e 20 0a 2a 2a 0a  ERE clause. .**.
2f1cd 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61 73 73  ** The table ass
2f1ce 6f 63 69 61 74 65 64 20 77 69 74 68 20 46 52 4f  ociated with FRO
2f1cf 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 70 53  M clause term pS
2f1d0 72 63 20 6d 61 79 20 62 65 20 65 69 74 68 65 72  rc may be either
2f1d1 20 61 0a 2a 2a 20 72 65 67 75 6c 61 72 20 42 2d   a.** regular B-
2f1d2 54 72 65 65 20 74 61 62 6c 65 20 6f 72 20 61 20  Tree table or a 
2f1d3 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
2f1d4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65  /.static void be
2f1d5 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28  stOrClauseIndex(
2f1d6 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2f1d7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2f1d8 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
2f1d9 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
2f1da 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
2f1db 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
2f1dc 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2f1dd 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2f1de 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54  tem *pSrc,  /* T
2f1df 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74  he FROM clause t
2f1e0 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f  erm to search */
2f1e1 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65  .  Bitmask notRe
2f1e2 61 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ady,           /
2f1e3 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72  * Mask of cursor
2f1e4 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61  s that are not a
2f1e5 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78  vailable */.  Ex
2f1e6 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
2f1e7 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2f1e8 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f1e9 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73 74 20   */.  WhereCost 
2f1ea 2a 70 43 6f 73 74 20 20 20 20 20 20 20 20 20 20  *pCost          
2f1eb 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74    /* Lowest cost
2f1ec 20 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 29   query plan */.)
2f1ed 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
2f1ee 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
2f1ef 41 54 49 4f 4e 0a 20 20 63 6f 6e 73 74 20 69 6e  ATION.  const in
2f1f0 74 20 69 43 75 72 20 3d 20 70 53 72 63 2d 3e 69  t iCur = pSrc->i
2f1f1 43 75 72 73 6f 72 3b 20 20 20 2f 2a 20 54 68 65  Cursor;   /* The
2f1f2 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74   cursor of the t
2f1f3 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73  able to be acces
2f1f4 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 42  sed */.  const B
2f1f5 69 74 6d 61 73 6b 20 6d 61 73 6b 53 72 63 20 3d  itmask maskSrc =
2f1f6 20 67 65 74 4d 61 73 6b 28 70 57 43 2d 3e 70 4d   getMask(pWC->pM
2f1f7 61 73 6b 53 65 74 2c 20 69 43 75 72 29 3b 20 20  askSet, iCur);  
2f1f8 2f 2a 20 42 69 74 6d 61 73 6b 20 66 6f 72 20 70  /* Bitmask for p
2f1f9 53 72 63 20 2a 2f 0a 20 20 57 68 65 72 65 54 65  Src */.  WhereTe
2f1fa 72 6d 20 2a 20 63 6f 6e 73 74 20 70 57 43 45 6e  rm * const pWCEn
2f1fb 64 20 3d 20 26 70 57 43 2d 3e 61 5b 70 57 43 2d  d = &pWC->a[pWC-
2f1fc 3e 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20 20 20  >nTerm];        
2f1fd 2f 2a 20 45 6e 64 20 6f 66 20 70 57 43 2d 3e 61  /* End of pWC->a
2f1fe 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  [] */.  WhereTer
2f1ff 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20  m *pTerm;       
2f200 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
2f201 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66 20 74 68  ingle term of th
2f202 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2f203 2f 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 74  /..  /* Search t
2f204 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2f205 74 65 72 6d 73 20 66 6f 72 20 61 20 75 73 61 62  terms for a usab
2f206 6c 65 20 57 4f 5f 4f 52 20 74 65 72 6d 2e 20 2a  le WO_OR term. *
2f207 2f 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57  /.  for(pTerm=pW
2f208 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70 57 43 45  C->a; pTerm<pWCE
2f209 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20  nd; pTerm++){.  
2f20a 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70    if( pTerm->eOp
2f20b 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 0a 20  erator==WO_OR . 
2f20c 20 20 20 20 26 26 20 28 28 70 54 65 72 6d 2d 3e      && ((pTerm->
2f20d 70 72 65 72 65 71 41 6c 6c 20 26 20 7e 6d 61 73  prereqAll & ~mas
2f20e 6b 53 72 63 29 20 26 20 6e 6f 74 52 65 61 64 79  kSrc) & notReady
2f20f 29 3d 3d 30 0a 20 20 20 20 20 26 26 20 28 70 54  )==0.     && (pT
2f210 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e  erm->u.pOrInfo->
2f211 69 6e 64 65 78 61 62 6c 65 20 26 20 6d 61 73 6b  indexable & mask
2f212 53 72 63 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a  Src)!=0 .    ){.
2f213 20 20 20 20 20 20 57 68 65 72 65 43 6c 61 75 73        WhereClaus
2f214 65 20 2a 20 63 6f 6e 73 74 20 70 4f 72 57 43 20  e * const pOrWC 
2f215 3d 20 26 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49  = &pTerm->u.pOrI
2f216 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 57  nfo->wc;.      W
2f217 68 65 72 65 54 65 72 6d 20 2a 20 63 6f 6e 73 74  hereTerm * const
2f218 20 70 4f 72 57 43 45 6e 64 20 3d 20 26 70 4f 72   pOrWCEnd = &pOr
2f219 57 43 2d 3e 61 5b 70 4f 72 57 43 2d 3e 6e 54 65  WC->a[pOrWC->nTe
2f21a 72 6d 5d 3b 0a 20 20 20 20 20 20 57 68 65 72 65  rm];.      Where
2f21b 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 0a 20  Term *pOrTerm;. 
2f21c 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d       int flags =
2f21d 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b   WHERE_MULTI_OR;
2f21e 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 54  .      double rT
2f21f 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  otal = 0;.      
2f220 64 6f 75 62 6c 65 20 6e 52 6f 77 20 3d 20 30 3b  double nRow = 0;
2f221 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 75  .      Bitmask u
2f222 73 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20  sed = 0;..      
2f223 66 6f 72 28 70 4f 72 54 65 72 6d 3d 70 4f 72 57  for(pOrTerm=pOrW
2f224 43 2d 3e 61 3b 20 70 4f 72 54 65 72 6d 3c 70 4f  C->a; pOrTerm<pO
2f225 72 57 43 45 6e 64 3b 20 70 4f 72 54 65 72 6d 2b  rWCEnd; pOrTerm+
2f226 2b 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72  +){.        Wher
2f227 65 43 6f 73 74 20 73 54 65 72 6d 43 6f 73 74 3b  eCost sTermCost;
2f228 0a 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52  .        WHERETR
2f229 41 43 45 28 28 22 2e 2e 2e 20 4d 75 6c 74 69 2d  ACE(("... Multi-
2f22a 69 6e 64 65 78 20 4f 52 20 74 65 73 74 69 6e 67  index OR testing
2f22b 20 66 6f 72 20 74 65 72 6d 20 25 64 20 6f 66 20   for term %d of 
2f22c 25 64 2e 2e 2e 2e 5c 6e 22 2c 20 0a 20 20 20 20  %d....\n", .    
2f22d 20 20 20 20 20 20 28 70 4f 72 54 65 72 6d 20 2d        (pOrTerm -
2f22e 20 70 4f 72 57 43 2d 3e 61 29 2c 20 28 70 54 65   pOrWC->a), (pTe
2f22f 72 6d 20 2d 20 70 57 43 2d 3e 61 29 0a 20 20 20  rm - pWC->a).   
2f230 20 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20       ));.       
2f231 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f   if( pOrTerm->eO
2f232 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20  perator==WO_AND 
2f233 29 7b 0a 20 20 20 20 20 20 20 20 20 20 57 68 65  ){.          Whe
2f234 72 65 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43  reClause *pAndWC
2f235 20 3d 20 26 70 4f 72 54 65 72 6d 2d 3e 75 2e 70   = &pOrTerm->u.p
2f236 41 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20  AndInfo->wc;.   
2f237 20 20 20 20 20 20 20 62 65 73 74 49 6e 64 65 78         bestIndex
2f238 28 70 50 61 72 73 65 2c 20 70 41 6e 64 57 43 2c  (pParse, pAndWC,
2f239 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c   pSrc, notReady,
2f23a 20 30 2c 20 26 73 54 65 72 6d 43 6f 73 74 29 3b   0, &sTermCost);
2f23b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
2f23c 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74  f( pOrTerm->left
2f23d 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 7b 0a  Cursor==iCur ){.
2f23e 20 20 20 20 20 20 20 20 20 20 57 68 65 72 65 43            WhereC
2f23f 6c 61 75 73 65 20 74 65 6d 70 57 43 3b 0a 20 20  lause tempWC;.  
2f240 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 70          tempWC.p
2f241 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61  Parse = pWC->pPa
2f242 72 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 74  rse;.          t
2f243 65 6d 70 57 43 2e 70 4d 61 73 6b 53 65 74 20 3d  empWC.pMaskSet =
2f244 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a   pWC->pMaskSet;.
2f245 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 43            tempWC
2f246 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20  .op = TK_AND;.  
2f247 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 61          tempWC.a
2f248 20 3d 20 70 4f 72 54 65 72 6d 3b 0a 20 20 20 20   = pOrTerm;.    
2f249 20 20 20 20 20 20 74 65 6d 70 57 43 2e 6e 54 65        tempWC.nTe
2f24a 72 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  rm = 1;.        
2f24b 20 20 62 65 73 74 49 6e 64 65 78 28 70 50 61 72    bestIndex(pPar
2f24c 73 65 2c 20 26 74 65 6d 70 57 43 2c 20 70 53 72  se, &tempWC, pSr
2f24d 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 30 2c 20  c, notReady, 0, 
2f24e 26 73 54 65 72 6d 43 6f 73 74 29 3b 0a 20 20 20  &sTermCost);.   
2f24f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2f250 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
2f251 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f252 20 20 72 54 6f 74 61 6c 20 2b 3d 20 73 54 65 72    rTotal += sTer
2f253 6d 43 6f 73 74 2e 72 43 6f 73 74 3b 0a 20 20 20  mCost.rCost;.   
2f254 20 20 20 20 20 6e 52 6f 77 20 2b 3d 20 73 54 65       nRow += sTe
2f255 72 6d 43 6f 73 74 2e 6e 52 6f 77 3b 0a 20 20 20  rmCost.nRow;.   
2f256 20 20 20 20 20 75 73 65 64 20 7c 3d 20 73 54 65       used |= sTe
2f257 72 6d 43 6f 73 74 2e 75 73 65 64 3b 0a 20 20 20  rmCost.used;.   
2f258 20 20 20 20 20 69 66 28 20 72 54 6f 74 61 6c 3e       if( rTotal>
2f259 3d 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29 20  =pCost->rCost ) 
2f25a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a  break;.      }..
2f25b 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72        /* If ther
2f25c 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  e is an ORDER BY
2f25d 20 63 6c 61 75 73 65 2c 20 69 6e 63 72 65 61 73   clause, increas
2f25e 65 20 74 68 65 20 73 63 61 6e 20 63 6f 73 74 20  e the scan cost 
2f25f 74 6f 20 61 63 63 6f 75 6e 74 20 0a 20 20 20 20  to account .    
2f260 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 73    ** for the cos
2f261 74 20 6f 66 20 74 68 65 20 73 6f 72 74 2e 20 2a  t of the sort. *
2f262 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64  /.      if( pOrd
2f263 65 72 42 79 21 3d 30 20 29 7b 0a 20 20 20 20 20  erBy!=0 ){.     
2f264 20 20 20 72 54 6f 74 61 6c 20 2b 3d 20 6e 52 6f     rTotal += nRo
2f265 77 2a 65 73 74 4c 6f 67 28 6e 52 6f 77 29 3b 0a  w*estLog(nRow);.
2f266 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41          WHERETRA
2f267 43 45 28 28 22 2e 2e 2e 20 73 6f 72 74 69 6e 67  CE(("... sorting
2f268 20 69 6e 63 72 65 61 73 65 73 20 4f 52 20 63 6f   increases OR co
2f269 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 72  st to %.9g\n", r
2f26a 54 6f 74 61 6c 29 29 3b 0a 20 20 20 20 20 20 7d  Total));.      }
2f26b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  ..      /* If th
2f26c 65 20 63 6f 73 74 20 6f 66 20 73 63 61 6e 6e 69  e cost of scanni
2f26d 6e 67 20 75 73 69 6e 67 20 74 68 69 73 20 4f 52  ng using this OR
2f26e 20 74 65 72 6d 20 66 6f 72 20 6f 70 74 69 6d 69   term for optimi
2f26f 7a 61 74 69 6f 6e 20 69 73 0a 20 20 20 20 20 20  zation is.      
2f270 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65  ** less than the
2f271 20 63 75 72 72 65 6e 74 20 63 6f 73 74 20 73 74   current cost st
2f272 6f 72 65 64 20 69 6e 20 70 43 6f 73 74 2c 20 72  ored in pCost, r
2f273 65 70 6c 61 63 65 20 74 68 65 20 63 6f 6e 74 65  eplace the conte
2f274 6e 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  nts.      ** of 
2f275 70 43 6f 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20  pCost. */.      
2f276 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e  WHERETRACE(("...
2f277 20 6d 75 6c 74 69 2d 69 6e 64 65 78 20 4f 52 20   multi-index OR 
2f278 63 6f 73 74 3d 25 2e 39 67 20 6e 72 6f 77 3d 25  cost=%.9g nrow=%
2f279 2e 39 67 5c 6e 22 2c 20 72 54 6f 74 61 6c 2c 20  .9g\n", rTotal, 
2f27a 6e 52 6f 77 29 29 3b 0a 20 20 20 20 20 20 69 66  nRow));.      if
2f27b 28 20 72 54 6f 74 61 6c 3c 70 43 6f 73 74 2d 3e  ( rTotal<pCost->
2f27c 72 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 20  rCost ){.       
2f27d 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20   pCost->rCost = 
2f27e 72 54 6f 74 61 6c 3b 0a 20 20 20 20 20 20 20 20  rTotal;.        
2f27f 70 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52  pCost->nRow = nR
2f280 6f 77 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73  ow;.        pCos
2f281 74 2d 3e 75 73 65 64 20 3d 20 75 73 65 64 3b 0a  t->used = used;.
2f282 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70          pCost->p
2f283 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 66 6c  lan.wsFlags = fl
2f284 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 43 6f  ags;.        pCo
2f285 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d  st->plan.u.pTerm
2f286 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20   = pTerm;.      
2f287 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  }.    }.  }.#end
2f288 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2f289 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  T_OR_OPTIMIZATIO
2f28a 4e 20 2a 2f 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  N */.}..#ifndef 
2f28b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2f28c 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 41  UALTABLE./*.** A
2f28d 6c 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70 75  llocate and popu
2f28e 6c 61 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f  late an sqlite3_
2f28f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
2f290 74 75 72 65 2e 20 49 74 20 69 73 20 74 68 65 20  ture. It is the 
2f291 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  .** responsibili
2f292 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72  ty of the caller
2f293 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 72   to eventually r
2f294 65 6c 65 61 73 65 20 74 68 65 20 73 74 72 75 63  elease the struc
2f295 74 75 72 65 0a 2a 2a 20 62 79 20 70 61 73 73 69  ture.** by passi
2f296 6e 67 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72  ng the pointer r
2f297 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20  eturned by this 
2f298 66 75 6e 63 74 69 6f 6e 20 74 6f 20 73 71 6c 69  function to sqli
2f299 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 73  te3_free()..*/.s
2f29a 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 69 6e  tatic sqlite3_in
2f29b 64 65 78 5f 69 6e 66 6f 20 2a 61 6c 6c 6f 63 61  dex_info *alloca
2f29c 74 65 49 6e 64 65 78 49 6e 66 6f 28 0a 20 20 50  teIndexInfo(.  P
2f29d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20  arse *pParse, . 
2f29e 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2f29f 43 2c 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  C,.  struct SrcL
2f2a0 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 0a  ist_item *pSrc,.
2f2a1 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
2f2a2 65 72 42 79 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  erBy.){.  int i,
2f2a3 20 6a 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   j;.  int nTerm;
2f2a4 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
2f2a5 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2f2a6 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b 0a 20 20  nt *pIdxCons;.  
2f2a7 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2f2a8 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 2a 70 49  ndex_orderby *pI
2f2a9 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 73 74 72  dxOrderBy;.  str
2f2aa 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2f2ab 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
2f2ac 67 65 20 2a 70 55 73 61 67 65 3b 0a 20 20 57 68  ge *pUsage;.  Wh
2f2ad 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a  ereTerm *pTerm;.
2f2ae 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a    int nOrderBy;.
2f2af 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
2f2b0 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 0a  info *pIdxInfo;.
2f2b1 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22  .  WHERETRACE(("
2f2b2 52 65 63 6f 6d 70 75 74 69 6e 67 20 69 6e 64 65  Recomputing inde
2f2b3 78 20 69 6e 66 6f 20 66 6f 72 20 25 73 2e 2e 2e  x info for %s...
2f2b4 5c 6e 22 2c 20 70 53 72 63 2d 3e 70 54 61 62 2d  \n", pSrc->pTab-
2f2b5 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 2f 2a 20  >zName));..  /* 
2f2b6 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72  Count the number
2f2b7 20 6f 66 20 70 6f 73 73 69 62 6c 65 20 57 48 45   of possible WHE
2f2b8 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72  RE clause constr
2f2b9 61 69 6e 74 73 20 72 65 66 65 72 72 69 6e 67 0a  aints referring.
2f2ba 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 76 69 72    ** to this vir
2f2bb 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
2f2bc 66 6f 72 28 69 3d 6e 54 65 72 6d 3d 30 2c 20 70  for(i=nTerm=0, p
2f2bd 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70  Term=pWC->a; i<p
2f2be 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20  WC->nTerm; i++, 
2f2bf 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66  pTerm++){.    if
2f2c0 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  ( pTerm->leftCur
2f2c1 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e 69 43 75  sor != pSrc->iCu
2f2c2 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b  rsor ) continue;
2f2c3 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54  .    assert( (pT
2f2c4 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28  erm->eOperator&(
2f2c5 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2f2c6 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 74  -1))==0 );.    t
2f2c7 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
2f2c8 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e  eOperator==WO_IN
2f2c9 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2f2ca 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2f2cb 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b  or==WO_ISNULL );
2f2cc 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  .    if( pTerm->
2f2cd 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f  eOperator & (WO_
2f2ce 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20  IN|WO_ISNULL) ) 
2f2cf 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 6e 54  continue;.    nT
2f2d0 65 72 6d 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  erm++;.  }..  /*
2f2d1 20 49 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   If the ORDER BY
2f2d2 20 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73   clause contains
2f2d3 20 6f 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 69 6e   only columns in
2f2d4 20 74 68 65 20 63 75 72 72 65 6e 74 20 0a 20 20   the current .  
2f2d5 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
2f2d6 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 73   then allocate s
2f2d7 70 61 63 65 20 66 6f 72 20 74 68 65 20 61 4f 72  pace for the aOr
2f2d8 64 65 72 42 79 20 70 61 72 74 20 6f 66 0a 20 20  derBy part of.  
2f2d9 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ** the sqlite3_i
2f2da 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2f2db 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 72 64  ure..  */.  nOrd
2f2dc 65 72 42 79 20 3d 20 30 3b 0a 20 20 69 66 28 20  erBy = 0;.  if( 
2f2dd 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2f2de 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65  for(i=0; i<pOrde
2f2df 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  rBy->nExpr; i++)
2f2e0 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45  {.      Expr *pE
2f2e1 78 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  xpr = pOrderBy->
2f2e2 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
2f2e3 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21    if( pExpr->op!
2f2e4 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45  =TK_COLUMN || pE
2f2e5 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d 70 53 72  xpr->iTable!=pSr
2f2e6 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 62 72 65  c->iCursor ) bre
2f2e7 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
2f2e8 28 20 69 3d 3d 70 4f 72 64 65 72 42 79 2d 3e 6e  ( i==pOrderBy->n
2f2e9 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 6e 4f  Expr ){.      nO
2f2ea 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42  rderBy = pOrderB
2f2eb 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 7d 0a  y->nExpr;.    }.
2f2ec 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61    }..  /* Alloca
2f2ed 74 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  te the sqlite3_i
2f2ee 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2f2ef 75 72 65 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49  ure.  */.  pIdxI
2f2f0 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  nfo = sqlite3DbM
2f2f1 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65  allocZero(pParse
2f2f2 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49  ->db, sizeof(*pI
2f2f3 64 78 49 6e 66 6f 29 0a 20 20 20 20 20 20 20 20  dxInfo).        
2f2f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2f5 20 20 20 2b 20 28 73 69 7a 65 6f 66 28 2a 70 49     + (sizeof(*pI
2f2f6 64 78 43 6f 6e 73 29 20 2b 20 73 69 7a 65 6f 66  dxCons) + sizeof
2f2f7 28 2a 70 55 73 61 67 65 29 29 2a 6e 54 65 72 6d  (*pUsage))*nTerm
2f2f8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f2f9 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 73 69              + si
2f2fa 7a 65 6f 66 28 2a 70 49 64 78 4f 72 64 65 72 42  zeof(*pIdxOrderB
2f2fb 79 29 2a 6e 4f 72 64 65 72 42 79 20 29 3b 0a 20  y)*nOrderBy );. 
2f2fc 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30   if( pIdxInfo==0
2f2fd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2f2fe 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2f2ff 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29  "out of memory")
2f300 3b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65  ;.    /* (double
2f301 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51  )0 In case of SQ
2f302 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
2f303 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20  NG_POINT... */. 
2f304 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2f305 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
2f306 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65 2e  e the structure.
2f307 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e    The sqlite3_in
2f308 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2f309 72 65 20 63 6f 6e 74 61 69 6e 73 0a 20 20 2a 2a  re contains.  **
2f30a 20 6d 61 6e 79 20 66 69 65 6c 64 73 20 74 68 61   many fields tha
2f30b 74 20 61 72 65 20 64 65 63 6c 61 72 65 64 20 22  t are declared "
2f30c 63 6f 6e 73 74 22 20 74 6f 20 70 72 65 76 65 6e  const" to preven
2f30d 74 20 78 42 65 73 74 49 6e 64 65 78 20 66 72 6f  t xBestIndex fro
2f30e 6d 0a 20 20 2a 2a 20 63 68 61 6e 67 69 6e 67 20  m.  ** changing 
2f30f 74 68 65 6d 2e 20 20 57 65 20 68 61 76 65 20 74  them.  We have t
2f310 6f 20 64 6f 20 73 6f 6d 65 20 66 75 6e 6b 79 20  o do some funky 
2f311 63 61 73 74 69 6e 67 20 69 6e 20 6f 72 64 65 72  casting in order
2f312 20 74 6f 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c   to.  ** initial
2f313 69 7a 65 20 74 68 6f 73 65 20 66 69 65 6c 64 73  ize those fields
2f314 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78 43 6f 6e  ..  */.  pIdxCon
2f315 73 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69  s = (struct sqli
2f316 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
2f317 61 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 5b  aint*)&pIdxInfo[
2f318 31 5d 3b 0a 20 20 70 49 64 78 4f 72 64 65 72 42  1];.  pIdxOrderB
2f319 79 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69  y = (struct sqli
2f31a 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62  te3_index_orderb
2f31b 79 2a 29 26 70 49 64 78 43 6f 6e 73 5b 6e 54 65  y*)&pIdxCons[nTe
2f31c 72 6d 5d 3b 0a 20 20 70 55 73 61 67 65 20 3d 20  rm];.  pUsage = 
2f31d 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  (struct sqlite3_
2f31e 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2f31f 5f 75 73 61 67 65 2a 29 26 70 49 64 78 4f 72 64  _usage*)&pIdxOrd
2f320 65 72 42 79 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a  erBy[nOrderBy];.
2f321 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e    *(int*)&pIdxIn
2f322 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20  fo->nConstraint 
2f323 3d 20 6e 54 65 72 6d 3b 0a 20 20 2a 28 69 6e 74  = nTerm;.  *(int
2f324 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72  *)&pIdxInfo->nOr
2f325 64 65 72 42 79 20 3d 20 6e 4f 72 64 65 72 42 79  derBy = nOrderBy
2f326 3b 0a 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c  ;.  *(struct sql
2f327 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
2f328 72 61 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66  raint**)&pIdxInf
2f329 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d  o->aConstraint =
2f32a 20 70 49 64 78 43 6f 6e 73 3b 0a 20 20 2a 28 73   pIdxCons;.  *(s
2f32b 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2f32c 64 65 78 5f 6f 72 64 65 72 62 79 2a 2a 29 26 70  dex_orderby**)&p
2f32d 49 64 78 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42  IdxInfo->aOrderB
2f32e 79 20 3d 20 70 49 64 78 4f 72 64 65 72 42 79 3b  y = pIdxOrderBy;
2f32f 0a 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69  .  *(struct sqli
2f330 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
2f331 61 69 6e 74 5f 75 73 61 67 65 2a 2a 29 26 70 49  aint_usage**)&pI
2f332 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61  dxInfo->aConstra
2f333 69 6e 74 55 73 61 67 65 20 3d 0a 20 20 20 20 20  intUsage =.     
2f334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f335 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f336 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f337 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 55                pU
2f338 73 61 67 65 3b 0a 0a 20 20 66 6f 72 28 69 3d 6a  sage;..  for(i=j
2f339 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e 61  =0, pTerm=pWC->a
2f33a 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20  ; i<pWC->nTerm; 
2f33b 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20  i++, pTerm++){. 
2f33c 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65     if( pTerm->le
2f33d 66 74 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63  ftCursor != pSrc
2f33e 2d 3e 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74  ->iCursor ) cont
2f33f 69 6e 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74  inue;.    assert
2f340 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  ( (pTerm->eOpera
2f341 74 6f 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65  tor&(pTerm->eOpe
2f342 72 61 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a  rator-1))==0 );.
2f343 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
2f344 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
2f345 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 74 65 73  WO_IN );.    tes
2f346 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
2f347 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55  perator==WO_ISNU
2f348 4c 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54  LL );.    if( pT
2f349 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
2f34a 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c   (WO_IN|WO_ISNUL
2f34b 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  L) ) continue;. 
2f34c 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69     pIdxCons[j].i
2f34d 43 6f 6c 75 6d 6e 20 3d 20 70 54 65 72 6d 2d 3e  Column = pTerm->
2f34e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20  u.leftColumn;.  
2f34f 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 54    pIdxCons[j].iT
2f350 65 72 6d 4f 66 66 73 65 74 20 3d 20 69 3b 0a 20  ermOffset = i;. 
2f351 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 6f     pIdxCons[j].o
2f352 70 20 3d 20 28 75 38 29 70 54 65 72 6d 2d 3e 65  p = (u8)pTerm->e
2f353 4f 70 65 72 61 74 6f 72 3b 0a 20 20 20 20 2f 2a  Operator;.    /*
2f354 20 54 68 65 20 64 69 72 65 63 74 20 61 73 73 69   The direct assi
2f355 67 6e 6d 65 6e 74 20 69 6e 20 74 68 65 20 70 72  gnment in the pr
2f356 65 76 69 6f 75 73 20 6c 69 6e 65 20 69 73 20 70  evious line is p
2f357 6f 73 73 69 62 6c 65 20 6f 6e 6c 79 20 62 65 63  ossible only bec
2f358 61 75 73 65 0a 20 20 20 20 2a 2a 20 74 68 65 20  ause.    ** the 
2f359 57 4f 5f 20 61 6e 64 20 53 51 4c 49 54 45 5f 49  WO_ and SQLITE_I
2f35a 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
2f35b 20 63 6f 64 65 73 20 61 72 65 20 69 64 65 6e 74   codes are ident
2f35c 69 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 2a  ical.  The.    *
2f35d 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  * following asse
2f35e 72 74 73 20 76 65 72 69 66 79 20 74 68 69 73 20  rts verify this 
2f35f 66 61 63 74 2e 20 2a 2f 0a 20 20 20 20 61 73 73  fact. */.    ass
2f360 65 72 74 28 20 57 4f 5f 45 51 3d 3d 53 51 4c 49  ert( WO_EQ==SQLI
2f361 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
2f362 49 4e 54 5f 45 51 20 29 3b 0a 20 20 20 20 61 73  INT_EQ );.    as
2f363 73 65 72 74 28 20 57 4f 5f 4c 54 3d 3d 53 51 4c  sert( WO_LT==SQL
2f364 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
2f365 41 49 4e 54 5f 4c 54 20 29 3b 0a 20 20 20 20 61  AINT_LT );.    a
2f366 73 73 65 72 74 28 20 57 4f 5f 4c 45 3d 3d 53 51  ssert( WO_LE==SQ
2f367 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
2f368 52 41 49 4e 54 5f 4c 45 20 29 3b 0a 20 20 20 20  RAINT_LE );.    
2f369 61 73 73 65 72 74 28 20 57 4f 5f 47 54 3d 3d 53  assert( WO_GT==S
2f36a 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
2f36b 54 52 41 49 4e 54 5f 47 54 20 29 3b 0a 20 20 20  TRAINT_GT );.   
2f36c 20 61 73 73 65 72 74 28 20 57 4f 5f 47 45 3d 3d   assert( WO_GE==
2f36d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
2f36e 53 54 52 41 49 4e 54 5f 47 45 20 29 3b 0a 20 20  STRAINT_GE );.  
2f36f 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4d 41 54    assert( WO_MAT
2f370 43 48 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  CH==SQLITE_INDEX
2f371 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43  _CONSTRAINT_MATC
2f372 48 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  H );.    assert(
2f373 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2f374 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54  r & (WO_EQ|WO_LT
2f375 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f  |WO_LE|WO_GT|WO_
2f376 47 45 7c 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a  GE|WO_MATCH) );.
2f377 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 66      j++;.  }.  f
2f378 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72  or(i=0; i<nOrder
2f379 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78  By; i++){.    Ex
2f37a 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64  pr *pExpr = pOrd
2f37b 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  erBy->a[i].pExpr
2f37c 3b 0a 20 20 20 20 70 49 64 78 4f 72 64 65 72 42  ;.    pIdxOrderB
2f37d 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70  y[i].iColumn = p
2f37e 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  Expr->iColumn;. 
2f37f 20 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69     pIdxOrderBy[i
2f380 5d 2e 64 65 73 63 20 3d 20 70 4f 72 64 65 72 42  ].desc = pOrderB
2f381 79 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65  y->a[i].sortOrde
2f382 72 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  r;.  }..  return
2f383 20 70 49 64 78 49 6e 66 6f 3b 0a 7d 0a 0a 2f 2a   pIdxInfo;.}../*
2f384 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 62  .** The table ob
2f385 6a 65 63 74 20 72 65 66 65 72 65 6e 63 65 20 70  ject reference p
2f386 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
2f387 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
2f388 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  this function.**
2f389 20 6d 75 73 74 20 72 65 70 72 65 73 65 6e 74 20   must represent 
2f38a 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
2f38b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2f38c 6e 76 6f 6b 65 73 20 74 68 65 20 78 42 65 73 74  nvokes the xBest
2f38d 49 6e 64 65 78 28 29 0a 2a 2a 20 6d 65 74 68 6f  Index().** metho
2f38e 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
2f38f 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20   table with the 
2f390 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f391 66 6f 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65  fo pointer passe
2f392 64 0a 2a 2a 20 61 73 20 74 68 65 20 61 72 67 75  d.** as the argu
2f393 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ment..**.** If a
2f394 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
2f395 70 50 61 72 73 65 20 69 73 20 70 6f 70 75 6c 61  pParse is popula
2f396 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ted with an erro
2f397 72 20 6d 65 73 73 61 67 65 20 61 6e 64 20 61 0a  r message and a.
2f398 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75  ** non-zero valu
2f399 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  e is returned. O
2f39a 74 68 65 72 77 69 73 65 2c 20 30 20 69 73 20 72  therwise, 0 is r
2f39b 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20  eturned and the 
2f39c 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 74 20 6f  output.** part o
2f39d 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  f the sqlite3_in
2f39e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2f39f 72 65 20 69 73 20 6c 65 66 74 20 70 6f 70 75 6c  re is left popul
2f3a0 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74  ated..**.** Whet
2f3a1 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72  her or not an er
2f3a2 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2c  ror is returned,
2f3a3 20 69 74 20 69 73 20 74 68 65 20 72 65 73 70 6f   it is the respo
2f3a4 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65  nsibility of the
2f3a5 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20 65 76  .** caller to ev
2f3a6 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 20 70 2d  entually free p-
2f3a7 3e 69 64 78 53 74 72 20 69 66 20 70 2d 3e 6e 65  >idxStr if p->ne
2f3a8 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 69  edToFreeIdxStr i
2f3a9 6e 64 69 63 61 74 65 73 0a 2a 2a 20 74 68 61 74  ndicates.** that
2f3aa 20 74 68 69 73 20 69 73 20 72 65 71 75 69 72 65   this is require
2f3ab 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
2f3ac 20 76 74 61 62 42 65 73 74 49 6e 64 65 78 28 50   vtabBestIndex(P
2f3ad 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
2f3ae 62 6c 65 20 2a 70 54 61 62 2c 20 73 71 6c 69 74  ble *pTab, sqlit
2f3af 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
2f3b0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  ){.  sqlite3_vta
2f3b1 62 20 2a 70 56 74 61 62 20 3d 20 73 71 6c 69 74  b *pVtab = sqlit
2f3b2 65 33 47 65 74 56 54 61 62 6c 65 28 70 50 61 72  e3GetVTable(pPar
2f3b3 73 65 2d 3e 64 62 2c 20 70 54 61 62 29 2d 3e 70  se->db, pTab)->p
2f3b4 56 74 61 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Vtab;.  int i;. 
2f3b5 20 69 6e 74 20 72 63 3b 0a 0a 20 20 28 76 6f 69   int rc;..  (voi
2f3b6 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2f3b7 66 66 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a  ff(pParse->db);.
2f3b8 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 78    WHERETRACE(("x
2f3b9 42 65 73 74 49 6e 64 65 78 20 66 6f 72 20 25 73  BestIndex for %s
2f3ba 5c 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  \n", pTab->zName
2f3bb 29 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f  ));.  TRACE_IDX_
2f3bc 49 4e 50 55 54 53 28 70 29 3b 0a 20 20 72 63 20  INPUTS(p);.  rc 
2f3bd 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  = pVtab->pModule
2f3be 2d 3e 78 42 65 73 74 49 6e 64 65 78 28 70 56 74  ->xBestIndex(pVt
2f3bf 61 62 2c 20 70 29 3b 0a 20 20 54 52 41 43 45 5f  ab, p);.  TRACE_
2f3c0 49 44 58 5f 4f 55 54 50 55 54 53 28 70 29 3b 0a  IDX_OUTPUTS(p);.
2f3c1 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2f3c2 61 66 65 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e  afetyOn(pParse->
2f3c3 64 62 29 3b 0a 0a 20 20 69 66 28 20 72 63 21 3d  db);..  if( rc!=
2f3c4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2f3c5 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2f3c6 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 70  NOMEM ){.      p
2f3c7 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
2f3c8 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
2f3c9 20 7d 65 6c 73 65 20 69 66 28 20 21 70 56 74 61   }else if( !pVta
2f3ca 62 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  b->zErrMsg ){.  
2f3cb 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2f3cc 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22  Msg(pParse, "%s"
2f3cd 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  , sqlite3ErrStr(
2f3ce 72 63 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rc));.    }else{
2f3cf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2f3d0 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2f3d1 25 73 22 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72  %s", pVtab->zErr
2f3d2 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Msg);.    }.  }.
2f3d3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2f3d4 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 56 74 61  pParse->db, pVta
2f3d5 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  b->zErrMsg);.  p
2f3d6 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
2f3d7 30 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  0;..  for(i=0; i
2f3d8 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  <p->nConstraint;
2f3d9 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21   i++){.    if( !
2f3da 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
2f3db 5d 2e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 61  ].usable && p->a
2f3dc 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
2f3dd 69 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29  i].argvIndex>0 )
2f3de 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2f3df 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2f3e0 0a 20 20 20 20 20 20 20 20 20 20 22 74 61 62 6c  .          "tabl
2f3e1 65 20 25 73 3a 20 78 42 65 73 74 49 6e 64 65 78  e %s: xBestIndex
2f3e2 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6e 76   returned an inv
2f3e3 61 6c 69 64 20 70 6c 61 6e 22 2c 20 70 54 61 62  alid plan", pTab
2f3e4 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ->zName);.    }.
2f3e5 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 50    }..  return pP
2f3e6 61 72 73 65 2d 3e 6e 45 72 72 3b 0a 7d 0a 0a 0a  arse->nErr;.}...
2f3e7 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
2f3e8 65 20 62 65 73 74 20 69 6e 64 65 78 20 66 6f 72  e best index for
2f3e9 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2f3ea 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 65 73 74  ..**.** The best
2f3eb 20 69 6e 64 65 78 20 69 73 20 63 6f 6d 70 75 74   index is comput
2f3ec 65 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49  ed by the xBestI
2f3ed 6e 64 65 78 20 6d 65 74 68 6f 64 20 6f 66 20 74  ndex method of t
2f3ee 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61  he virtual.** ta
2f3ef 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 69  ble module.  Thi
2f3f0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 61  s routine is rea
2f3f1 6c 6c 79 20 6a 75 73 74 20 61 20 77 72 61 70 70  lly just a wrapp
2f3f2 65 72 20 74 68 61 74 20 73 65 74 73 20 75 70 0a  er that sets up.
2f3f3 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ** the sqlite3_i
2f3f4 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2f3f5 75 72 65 20 74 68 61 74 20 69 73 20 75 73 65 64  ure that is used
2f3f6 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20   to communicate 
2f3f7 77 69 74 68 0a 2a 2a 20 78 42 65 73 74 49 6e 64  with.** xBestInd
2f3f8 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a  ex..**.** In a j
2f3f9 6f 69 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  oin, this routin
2f3fa 65 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65  e might be calle
2f3fb 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
2f3fc 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65   for the.** same
2f3fd 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
2f3fe 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   The sqlite3_ind
2f3ff 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2f400 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20  e is created.** 
2f401 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  and initialized 
2f402 6f 6e 20 74 68 65 20 66 69 72 73 74 20 69 6e 76  on the first inv
2f403 6f 63 61 74 69 6f 6e 20 61 6e 64 20 72 65 75 73  ocation and reus
2f404 65 64 20 6f 6e 20 61 6c 6c 20 73 75 62 73 65 71  ed on all subseq
2f405 75 65 6e 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  uent.** invocati
2f406 6f 6e 73 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ons.  The sqlite
2f407 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2f408 75 63 74 75 72 65 20 69 73 20 61 6c 73 6f 20 75  ucture is also u
2f409 73 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 64 65  sed when.** code
2f40a 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f   is generated to
2f40b 20 61 63 63 65 73 73 20 74 68 65 20 76 69 72 74   access the virt
2f40c 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20  ual table.  The 
2f40d 77 68 65 72 65 49 6e 66 6f 44 65 6c 65 74 65 28  whereInfoDelete(
2f40e 29 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61  ) .** routine ta
2f40f 6b 65 73 20 63 61 72 65 20 6f 66 20 66 72 65 65  kes care of free
2f410 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
2f411 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
2f412 74 75 72 65 20 61 66 74 65 72 0a 2a 2a 20 65 76  ture after.** ev
2f413 65 72 79 62 6f 64 79 20 68 61 73 20 66 69 6e 69  erybody has fini
2f414 73 68 65 64 20 77 69 74 68 20 69 74 2e 0a 2a 2f  shed with it..*/
2f415 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73  .static void bes
2f416 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28 0a 20  tVirtualIndex(. 
2f417 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2f418 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f419 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2f41a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65  context */.  Whe
2f41b 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
2f41c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f41d 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2f41e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2f41f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c  List_item *pSrc,
2f420 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f        /* The FRO
2f421 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f  M clause term to
2f422 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74   search */.  Bit
2f423 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20  mask notReady,  
2f424 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f425 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
2f426 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61  that are not ava
2f427 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72  ilable */.  Expr
2f428 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2f429 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2f42a 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c 61 75  he order by clau
2f42b 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73  se */.  WhereCos
2f42c 74 20 2a 70 43 6f 73 74 2c 20 20 20 20 20 20 20  t *pCost,       
2f42d 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73          /* Lowes
2f42e 74 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61  t cost query pla
2f42f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  n */.  sqlite3_i
2f430 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64  ndex_info **ppId
2f431 78 49 6e 66 6f 20 20 2f 2a 20 49 6e 64 65 78 20  xInfo  /* Index 
2f432 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73  information pass
2f433 65 64 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78  ed to xBestIndex
2f434 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
2f435 70 54 61 62 20 3d 20 70 53 72 63 2d 3e 70 54 61  pTab = pSrc->pTa
2f436 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64  b;.  sqlite3_ind
2f437 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66  ex_info *pIdxInf
2f438 6f 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69  o;.  struct sqli
2f439 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
2f43a 61 69 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b 0a  aint *pIdxCons;.
2f43b 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
2f43c 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2f43d 74 5f 75 73 61 67 65 20 2a 70 55 73 61 67 65 3b  t_usage *pUsage;
2f43e 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
2f43f 65 72 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  erm;.  int i, j;
2f440 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b  .  int nOrderBy;
2f441 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
2f442 20 77 73 46 6c 61 67 73 20 69 73 20 69 6e 69 74   wsFlags is init
2f443 69 61 6c 69 7a 65 64 20 74 6f 20 73 6f 6d 65 20  ialized to some 
2f444 73 61 6e 65 20 76 61 6c 75 65 2e 20 4f 74 68 65  sane value. Othe
2f445 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 0a 20  rwise, if the . 
2f446 20 2a 2a 20 6d 61 6c 6c 6f 63 20 69 6e 20 61 6c   ** malloc in al
2f447 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28  locateIndexInfo(
2f448 29 20 66 61 69 6c 73 20 61 6e 64 20 74 68 69 73  ) fails and this
2f449 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2f44a 73 20 6c 65 61 76 69 6e 67 0a 20 20 2a 2a 20 77  s leaving.  ** w
2f44b 73 46 6c 61 67 73 20 69 6e 20 61 6e 20 75 6e 69  sFlags in an uni
2f44c 6e 69 74 69 61 6c 69 7a 65 64 20 73 74 61 74 65  nitialized state
2f44d 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79  , the caller may
2f44e 20 62 65 68 61 76 65 20 75 6e 70 72 65 64 69 63   behave unpredic
2f44f 74 61 62 6c 79 2e 0a 20 20 2a 2f 0a 20 20 6d 65  tably..  */.  me
2f450 6d 73 65 74 28 70 43 6f 73 74 2c 20 30 2c 20 73  mset(pCost, 0, s
2f451 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b 0a  izeof(*pCost));.
2f452 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
2f453 46 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 56 49  Flags = WHERE_VI
2f454 52 54 55 41 4c 54 41 42 4c 45 3b 0a 0a 20 20 2f  RTUALTABLE;..  /
2f455 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33  * If the sqlite3
2f456 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2f457 63 74 75 72 65 20 68 61 73 20 6e 6f 74 20 62 65  cture has not be
2f458 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 20  en previously.  
2f459 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  ** allocated and
2f45a 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68   initialized, th
2f45b 65 6e 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20  en allocate and 
2f45c 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 20 6e 6f  initialize it no
2f45d 77 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e  w..  */.  pIdxIn
2f45e 66 6f 20 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b  fo = *ppIdxInfo;
2f45f 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d  .  if( pIdxInfo=
2f460 3d 30 20 29 7b 0a 20 20 20 20 2a 70 70 49 64 78  =0 ){.    *ppIdx
2f461 49 6e 66 6f 20 3d 20 70 49 64 78 49 6e 66 6f 20  Info = pIdxInfo 
2f462 3d 20 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78 49  = allocateIndexI
2f463 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 57 43 2c  nfo(pParse, pWC,
2f464 20 70 53 72 63 2c 20 70 4f 72 64 65 72 42 79 29   pSrc, pOrderBy)
2f465 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78  ;.  }.  if( pIdx
2f466 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Info==0 ){.    r
2f467 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eturn;.  }..  /*
2f468 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
2f469 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
2f46a 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2f46b 20 74 68 61 74 20 70 49 64 78 49 6e 66 6f 20 70   that pIdxInfo p
2f46c 6f 69 6e 74 73 0a 20 20 2a 2a 20 74 6f 20 77 69  oints.  ** to wi
2f46d 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69  ll have been ini
2f46e 74 69 61 6c 69 7a 65 64 2c 20 65 69 74 68 65 72  tialized, either
2f46f 20 64 75 72 69 6e 67 20 74 68 65 20 63 75 72 72   during the curr
2f470 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ent invocation o
2f471 72 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 73 6f  r.  ** during so
2f472 6d 65 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 74  me prior invocat
2f473 69 6f 6e 2e 20 20 4e 6f 77 20 77 65 20 6a 75 73  ion.  Now we jus
2f474 74 20 68 61 76 65 20 74 6f 20 63 75 73 74 6f 6d  t have to custom
2f475 69 7a 65 20 74 68 65 0a 20 20 2a 2a 20 64 65 74  ize the.  ** det
2f476 61 69 6c 73 20 6f 66 20 70 49 64 78 49 6e 66 6f  ails of pIdxInfo
2f477 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
2f478 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20   invocation and 
2f479 70 61 73 73 20 69 74 20 74 6f 0a 20 20 2a 2a 20  pass it to.  ** 
2f47a 78 42 65 73 74 49 6e 64 65 78 2e 0a 20 20 2a 2f  xBestIndex..  */
2f47b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  ..  /* The modul
2f47c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 64  e name must be d
2f47d 65 66 69 6e 65 64 2e 20 41 6c 73 6f 2c 20 62 79  efined. Also, by
2f47e 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72   this point ther
2f47f 65 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 61  e must.  ** be a
2f480 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
2f481 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
2f482 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65  cture. Otherwise
2f483 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 69 65  .  ** sqlite3Vie
2f484 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
2f485 29 20 77 6f 75 6c 64 20 68 61 76 65 20 70 69 63  ) would have pic
2f486 6b 65 64 20 75 70 20 74 68 65 20 65 72 72 6f 72  ked up the error
2f487 2e 20 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  . .  */.  assert
2f488 28 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  ( pTab->azModule
2f489 41 72 67 20 26 26 20 70 54 61 62 2d 3e 61 7a 4d  Arg && pTab->azM
2f48a 6f 64 75 6c 65 41 72 67 5b 30 5d 20 29 3b 0a 20  oduleArg[0] );. 
2f48b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2f48c 47 65 74 56 54 61 62 6c 65 28 70 50 61 72 73 65  GetVTable(pParse
2f48d 2d 3e 64 62 2c 20 70 54 61 62 29 20 29 3b 0a 0a  ->db, pTab) );..
2f48e 20 20 2f 2a 20 53 65 74 20 74 68 65 20 61 43 6f    /* Set the aCo
2f48f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c  nstraint[].usabl
2f490 65 20 66 69 65 6c 64 73 20 61 6e 64 20 69 6e 69  e fields and ini
2f491 74 69 61 6c 69 7a 65 20 61 6c 6c 20 0a 20 20 2a  tialize all .  *
2f492 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  * output variabl
2f493 65 73 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a  es to zero..  **
2f494 0a 20 20 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e  .  ** aConstrain
2f495 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20 74 72  t[].usable is tr
2f496 75 65 20 66 6f 72 20 63 6f 6e 73 74 72 61 69 6e  ue for constrain
2f497 74 73 20 77 68 65 72 65 20 74 68 65 20 72 69 67  ts where the rig
2f498 68 74 2d 68 61 6e 64 0a 20 20 2a 2a 20 73 69 64  ht-hand.  ** sid
2f499 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20  e contains only 
2f49a 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 61  references to ta
2f49b 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74  bles to the left
2f49c 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a   of the current.
2f49d 20 20 2a 2a 20 74 61 62 6c 65 2e 20 20 49 6e 20    ** table.  In 
2f49e 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
2f49f 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  the constraint i
2f4a0 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20  s of the form:. 
2f4a1 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
2f4a2 20 20 20 63 6f 6c 75 6d 6e 20 3d 20 65 78 70 72     column = expr
2f4a3 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20 77  .  **.  ** and w
2f4a4 65 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67  e are evaluating
2f4a5 20 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 68   a join, then th
2f4a6 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20  e constraint on 
2f4a7 63 6f 6c 75 6d 6e 20 69 73 20 0a 20 20 2a 2a 20  column is .  ** 
2f4a8 6f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 61 6c  only valid if al
2f4a9 6c 20 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e  l tables referen
2f4aa 63 65 64 20 69 6e 20 65 78 70 72 20 6f 63 63 75  ced in expr occu
2f4ab 72 20 74 6f 20 74 68 65 20 6c 65 66 74 0a 20 20  r to the left.  
2f4ac 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ** of the table 
2f4ad 63 6f 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d  containing colum
2f4ae 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  n..  **.  ** The
2f4af 20 61 43 6f 6e 73 74 72 61 69 6e 74 73 5b 5d 20   aConstraints[] 
2f4b0 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 65  array contains e
2f4b1 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63  ntries for all c
2f4b2 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20  onstraints.  ** 
2f4b3 6f 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  on the current t
2f4b4 61 62 6c 65 2e 20 20 54 68 61 74 20 77 61 79 20  able.  That way 
2f4b5 77 65 20 6f 6e 6c 79 20 68 61 76 65 20 74 6f 20  we only have to 
2f4b6 63 6f 6d 70 75 74 65 20 69 74 20 6f 6e 63 65 0a  compute it once.
2f4b7 20 20 2a 2a 20 65 76 65 6e 20 74 68 6f 75 67 68    ** even though
2f4b8 20 77 65 20 6d 69 67 68 74 20 74 72 79 20 74 6f   we might try to
2f4b9 20 70 69 63 6b 20 74 68 65 20 62 65 73 74 20 69   pick the best i
2f4ba 6e 64 65 78 20 6d 75 6c 74 69 70 6c 65 20 74 69  ndex multiple ti
2f4bb 6d 65 73 2e 0a 20 20 2a 2a 20 46 6f 72 20 65 61  mes..  ** For ea
2f4bc 63 68 20 61 74 74 65 6d 70 74 20 61 74 20 70 69  ch attempt at pi
2f4bd 63 6b 69 6e 67 20 61 6e 20 69 6e 64 65 78 2c 20  cking an index, 
2f4be 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 61 62  the order of tab
2f4bf 6c 65 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  les in the.  ** 
2f4c0 6a 6f 69 6e 20 6d 69 67 68 74 20 62 65 20 64 69  join might be di
2f4c1 66 66 65 72 65 6e 74 20 73 6f 20 77 65 20 68 61  fferent so we ha
2f4c2 76 65 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20  ve to recompute 
2f4c3 74 68 65 20 75 73 61 62 6c 65 20 66 6c 61 67 0a  the usable flag.
2f4c4 20 20 2a 2a 20 65 61 63 68 20 74 69 6d 65 2e 0a    ** each time..
2f4c5 20 20 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20    */.  pIdxCons 
2f4c6 3d 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74  = *(struct sqlit
2f4c7 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2f4c8 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d  int**)&pIdxInfo-
2f4c9 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20  >aConstraint;.  
2f4ca 70 55 73 61 67 65 20 3d 20 70 49 64 78 49 6e 66  pUsage = pIdxInf
2f4cb 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
2f4cc 61 67 65 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  age;.  for(i=0; 
2f4cd 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e  i<pIdxInfo->nCon
2f4ce 73 74 72 61 69 6e 74 3b 20 69 2b 2b 2c 20 70 49  straint; i++, pI
2f4cf 64 78 43 6f 6e 73 2b 2b 29 7b 0a 20 20 20 20 6a  dxCons++){.    j
2f4d0 20 3d 20 70 49 64 78 43 6f 6e 73 2d 3e 69 54 65   = pIdxCons->iTe
2f4d1 72 6d 4f 66 66 73 65 74 3b 0a 20 20 20 20 70 54  rmOffset;.    pT
2f4d2 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 6a 5d  erm = &pWC->a[j]
2f4d3 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 2d 3e  ;.    pIdxCons->
2f4d4 75 73 61 62 6c 65 20 3d 20 28 70 54 65 72 6d 2d  usable = (pTerm-
2f4d5 3e 70 72 65 72 65 71 52 69 67 68 74 26 6e 6f 74  >prereqRight&not
2f4d6 52 65 61 64 79 29 20 3f 20 30 20 3a 20 31 3b 0a  Ready) ? 0 : 1;.
2f4d7 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 55 73    }.  memset(pUs
2f4d8 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70  age, 0, sizeof(p
2f4d9 55 73 61 67 65 5b 30 5d 29 2a 70 49 64 78 49 6e  Usage[0])*pIdxIn
2f4da 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 29  fo->nConstraint)
2f4db 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f  ;.  if( pIdxInfo
2f4dc 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
2f4dd 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  tr ){.    sqlite
2f4de 33 5f 66 72 65 65 28 70 49 64 78 49 6e 66 6f 2d  3_free(pIdxInfo-
2f4df 3e 69 64 78 53 74 72 29 3b 0a 20 20 7d 0a 20 20  >idxStr);.  }.  
2f4e0 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72  pIdxInfo->idxStr
2f4e1 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f   = 0;.  pIdxInfo
2f4e2 2d 3e 69 64 78 4e 75 6d 20 3d 20 30 3b 0a 20 20  ->idxNum = 0;.  
2f4e3 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f  pIdxInfo->needTo
2f4e4 46 72 65 65 49 64 78 53 74 72 20 3d 20 30 3b 0a  FreeIdxStr = 0;.
2f4e5 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64 65    pIdxInfo->orde
2f4e6 72 42 79 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b  rByConsumed = 0;
2f4e7 0a 20 20 2f 2a 20 28 28 64 6f 75 62 6c 65 29 32  .  /* ((double)2
2f4e8 29 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c  ) In case of SQL
2f4e9 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
2f4ea 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20  G_POINT... */.  
2f4eb 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
2f4ec 74 65 64 43 6f 73 74 20 3d 20 53 51 4c 49 54 45  tedCost = SQLITE
2f4ed 5f 42 49 47 5f 44 42 4c 20 2f 20 28 28 64 6f 75  _BIG_DBL / ((dou
2f4ee 62 6c 65 29 32 29 3b 0a 20 20 6e 4f 72 64 65 72  ble)2);.  nOrder
2f4ef 42 79 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 6e  By = pIdxInfo->n
2f4f0 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20 21  OrderBy;.  if( !
2f4f1 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2f4f2 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72  pIdxInfo->nOrder
2f4f3 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69  By = 0;.  }..  i
2f4f4 66 28 20 76 74 61 62 42 65 73 74 49 6e 64 65 78  f( vtabBestIndex
2f4f5 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
2f4f6 49 64 78 49 6e 66 6f 29 20 29 7b 0a 20 20 20 20  IdxInfo) ){.    
2f4f7 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70  return;.  }..  p
2f4f8 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75  IdxCons = *(stru
2f4f9 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
2f4fa 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70  _constraint**)&p
2f4fb 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
2f4fc 61 69 6e 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  aint;.  for(i=0;
2f4fd 20 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f   i<pIdxInfo->nCo
2f4fe 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a  nstraint; i++){.
2f4ff 20 20 20 20 69 66 28 20 70 55 73 61 67 65 5b 69      if( pUsage[i
2f500 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29 7b  ].argvIndex>0 ){
2f501 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73  .      pCost->us
2f502 65 64 20 7c 3d 20 70 57 43 2d 3e 61 5b 70 49 64  ed |= pWC->a[pId
2f503 78 43 6f 6e 73 5b 69 5d 2e 69 54 65 72 6d 4f 66  xCons[i].iTermOf
2f504 66 73 65 74 5d 2e 70 72 65 72 65 71 52 69 67 68  fset].prereqRigh
2f505 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  t;.    }.  }..  
2f506 2f 2a 20 54 68 65 20 63 6f 73 74 20 69 73 20 6e  /* The cost is n
2f507 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  ot allowed to be
2f508 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 51 4c   larger than SQL
2f509 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 74 68 65  ITE_BIG_DBL (the
2f50a 0a 20 20 2a 2a 20 69 6e 69 74 61 6c 20 76 61 6c  .  ** inital val
2f50b 75 65 20 6f 66 20 6c 6f 77 65 73 74 43 6f 73 74  ue of lowestCost
2f50c 20 69 6e 20 74 68 69 73 20 6c 6f 6f 70 2e 20 49   in this loop. I
2f50d 66 20 69 74 20 69 73 2c 20 74 68 65 6e 20 74 68  f it is, then th
2f50e 65 0a 20 20 2a 2a 20 28 63 6f 73 74 3c 6c 6f 77  e.  ** (cost<low
2f50f 65 73 74 43 6f 73 74 29 20 74 65 73 74 20 62 65  estCost) test be
2f510 6c 6f 77 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  low will never b
2f511 65 20 74 72 75 65 2e 0a 20 20 2a 2a 20 0a 20 20  e true..  ** .  
2f512 2a 2a 20 55 73 65 20 22 28 64 6f 75 62 6c 65 29  ** Use "(double)
2f513 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22 32  2" instead of "2
2f514 2e 30 22 20 69 6e 20 63 61 73 65 20 4f 4d 49 54  .0" in case OMIT
2f515 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20  _FLOATING_POINT 
2f516 0a 20 20 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  .  ** is defined
2f517 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 53 51  ..  */.  if( (SQ
2f518 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f 28 28 64  LITE_BIG_DBL/((d
2f519 6f 75 62 6c 65 29 32 29 29 3c 70 49 64 78 49 6e  ouble)2))<pIdxIn
2f51a 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
2f51b 74 20 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e  t ){.    pCost->
2f51c 72 43 6f 73 74 20 3d 20 28 53 51 4c 49 54 45 5f  rCost = (SQLITE_
2f51d 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c 65  BIG_DBL/((double
2f51e 29 32 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )2));.  }else{. 
2f51f 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20     pCost->rCost 
2f520 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69  = pIdxInfo->esti
2f521 6d 61 74 65 64 43 6f 73 74 3b 0a 20 20 7d 0a 20  matedCost;.  }. 
2f522 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70   pCost->plan.u.p
2f523 56 74 61 62 49 64 78 20 3d 20 70 49 64 78 49 6e  VtabIdx = pIdxIn
2f524 66 6f 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e  fo;.  if( pIdxIn
2f525 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75  fo->orderByConsu
2f526 6d 65 64 20 29 7b 0a 20 20 20 20 70 43 6f 73 74  med ){.    pCost
2f527 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c  ->plan.wsFlags |
2f528 3d 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b  = WHERE_ORDERBY;
2f529 0a 20 20 7d 0a 20 20 70 43 6f 73 74 2d 3e 70 6c  .  }.  pCost->pl
2f52a 61 6e 2e 6e 45 71 20 3d 20 30 3b 0a 20 20 70 49  an.nEq = 0;.  pI
2f52b 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79  dxInfo->nOrderBy
2f52c 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 0a 20 20   = nOrderBy;..  
2f52d 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 61  /* Try to find a
2f52e 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20   more efficient 
2f52f 61 63 63 65 73 73 20 70 61 74 74 65 72 6e 20 62  access pattern b
2f530 79 20 75 73 69 6e 67 20 6d 75 6c 74 69 70 6c 65  y using multiple
2f531 20 69 6e 64 65 78 65 73 0a 20 20 2a 2a 20 74 6f   indexes.  ** to
2f532 20 6f 70 74 69 6d 69 7a 65 20 61 6e 20 4f 52 20   optimize an OR 
2f533 65 78 70 72 65 73 73 69 6f 6e 20 77 69 74 68 69  expression withi
2f534 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
2f535 73 65 2e 20 0a 20 20 2a 2f 0a 20 20 62 65 73 74  se. .  */.  best
2f536 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28 70 50  OrClauseIndex(pP
2f537 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c  arse, pWC, pSrc,
2f538 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65   notReady, pOrde
2f539 72 42 79 2c 20 70 43 6f 73 74 29 3b 0a 7d 0a 23  rBy, pCost);.}.#
2f53a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2f53b 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2f53c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75  E */../*.** Argu
2f53d 6d 65 6e 74 20 70 49 64 78 20 69 73 20 61 20 70  ment pIdx is a p
2f53e 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 64  ointer to an ind
2f53f 65 78 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ex structure tha
2f540 74 20 68 61 73 20 61 6e 20 61 72 72 61 79 20 6f  t has an array o
2f541 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4e 44 45  f.** SQLITE_INDE
2f542 58 5f 53 41 4d 50 4c 45 53 20 65 76 65 6e 6c 79  X_SAMPLES evenly
2f543 20 73 70 61 63 65 64 20 73 61 6d 70 6c 65 73 20   spaced samples 
2f544 6f 66 20 74 68 65 20 66 69 72 73 74 20 69 6e 64  of the first ind
2f545 65 78 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 73  exed column.** s
2f546 74 6f 72 65 64 20 69 6e 20 49 6e 64 65 78 2e 61  tored in Index.a
2f547 53 61 6d 70 6c 65 2e 20 54 68 65 20 64 6f 6d 61  Sample. The doma
2f548 69 6e 20 6f 66 20 76 61 6c 75 65 73 20 73 74 6f  in of values sto
2f549 72 65 64 20 69 6e 20 73 61 69 64 20 63 6f 6c 75  red in said colu
2f54a 6d 6e 0a 2a 2a 20 6d 61 79 20 62 65 20 74 68 6f  mn.** may be tho
2f54b 75 67 68 74 20 6f 66 20 61 73 20 64 69 76 69 64  ught of as divid
2f54c 65 64 20 69 6e 74 6f 20 28 53 51 4c 49 54 45 5f  ed into (SQLITE_
2f54d 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2b 31 29  INDEX_SAMPLES+1)
2f54e 20 72 65 67 69 6f 6e 73 2e 0a 2a 2a 20 52 65 67   regions..** Reg
2f54f 69 6f 6e 20 30 20 63 6f 6e 74 61 69 6e 73 20 61  ion 0 contains a
2f550 6c 6c 20 76 61 6c 75 65 73 20 73 6d 61 6c 6c 65  ll values smalle
2f551 72 20 74 68 61 6e 20 74 68 65 20 66 69 72 73 74  r than the first
2f552 20 73 61 6d 70 6c 65 20 76 61 6c 75 65 2e 20 52   sample value. R
2f553 65 67 69 6f 6e 0a 2a 2a 20 31 20 63 6f 6e 74 61  egion.** 1 conta
2f554 69 6e 73 20 76 61 6c 75 65 73 20 6c 61 72 67 65  ins values large
2f555 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
2f556 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  to the value of 
2f557 74 68 65 20 66 69 72 73 74 20 73 61 6d 70 6c 65  the first sample
2f558 2c 0a 2a 2a 20 62 75 74 20 73 6d 61 6c 6c 65 72  ,.** but smaller
2f559 20 74 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20   than the value 
2f55a 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 2e 20 41  of the second. A
2f55b 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nd so on..**.** 
2f55c 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74  If successful, t
2f55d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 74  his function det
2f55e 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20 6f 66  ermines which of
2f55f 20 74 68 65 20 72 65 67 69 6f 6e 73 20 76 61 6c   the regions val
2f560 75 65 20 0a 2a 2a 20 70 56 61 6c 20 6c 69 65 73  ue .** pVal lies
2f561 20 69 6e 2c 20 73 65 74 73 20 2a 70 69 52 65 67   in, sets *piReg
2f562 69 6f 6e 20 74 6f 20 74 68 65 20 72 65 67 69 6f  ion to the regio
2f563 6e 20 69 6e 64 65 78 20 28 61 20 76 61 6c 75 65  n index (a value
2f564 20 62 65 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e   between 0.** an
2f565 64 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53  d SQLITE_INDEX_S
2f566 41 4d 50 4c 45 53 2b 31 2c 20 69 6e 63 6c 75 73  AMPLES+1, inclus
2f567 69 76 65 29 20 61 6e 64 20 72 65 74 75 72 6e 73  ive) and returns
2f568 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 4f   SQLITE_OK..** O
2f569 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 6f 63 63  r, if an OOM occ
2f56a 75 72 73 20 77 68 69 6c 65 20 63 6f 6e 76 65 72  urs while conver
2f56b 74 69 6e 67 20 74 65 78 74 20 76 61 6c 75 65 73  ting text values
2f56c 20 62 65 74 77 65 65 6e 20 65 6e 63 6f 64 69 6e   between encodin
2f56d 67 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f  gs,.** SQLITE_NO
2f56e 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20  MEM is returned 
2f56f 61 6e 64 20 2a 70 69 52 65 67 69 6f 6e 20 69 73  and *piRegion is
2f570 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23   undefined..*/.#
2f571 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
2f572 42 4c 45 5f 53 54 41 54 32 0a 73 74 61 74 69 63  BLE_STAT2.static
2f573 20 69 6e 74 20 77 68 65 72 65 52 61 6e 67 65 52   int whereRangeR
2f574 65 67 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a  egion(.  Parse *
2f575 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2f576 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2f577 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2f578 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20   Index *pIdx,   
2f579 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f57a 49 6e 64 65 78 20 74 6f 20 63 6f 6e 73 69 64 65  Index to conside
2f57b 72 20 64 6f 6d 61 69 6e 20 6f 66 20 2a 2f 0a 20  r domain of */. 
2f57c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2f57d 70 56 61 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20  pVal,        /* 
2f57e 56 61 6c 75 65 20 74 6f 20 63 6f 6e 73 69 64 65  Value to conside
2f57f 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 52 65  r */.  int *piRe
2f580 67 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20  gion            
2f581 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 67 69 6f     /* OUT: Regio
2f582 6e 20 6f 66 20 64 6f 6d 61 69 6e 20 69 6e 20 77  n of domain in w
2f583 68 69 63 68 20 76 61 6c 75 65 20 6c 69 65 73 20  hich value lies 
2f584 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 41 4c 57 41  */.){.  if( ALWA
2f585 59 53 28 70 56 61 6c 29 20 29 7b 0a 20 20 20 20  YS(pVal) ){.    
2f586 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 61 53 61  IndexSample *aSa
2f587 6d 70 6c 65 20 3d 20 70 49 64 78 2d 3e 61 53 61  mple = pIdx->aSa
2f588 6d 70 6c 65 3b 0a 20 20 20 20 69 6e 74 20 69 20  mple;.    int i 
2f589 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 65 54 79  = 0;.    int eTy
2f58a 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pe = sqlite3_val
2f58b 75 65 5f 74 79 70 65 28 70 56 61 6c 29 3b 0a 0a  ue_type(pVal);..
2f58c 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 53      if( eType==S
2f58d 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c  QLITE_INTEGER ||
2f58e 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 46   eType==SQLITE_F
2f58f 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 64 6f  LOAT ){.      do
2f590 75 62 6c 65 20 72 20 3d 20 73 71 6c 69 74 65 33  uble r = sqlite3
2f591 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 70 56  _value_double(pV
2f592 61 6c 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  al);.      for(i
2f593 3d 30 3b 20 69 3c 53 51 4c 49 54 45 5f 49 4e 44  =0; i<SQLITE_IND
2f594 45 58 5f 53 41 4d 50 4c 45 53 3b 20 69 2b 2b 29  EX_SAMPLES; i++)
2f595 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 53  {.        if( aS
2f596 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3d 3d  ample[i].eType==
2f597 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 63 6f  SQLITE_NULL ) co
2f598 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
2f599 69 66 28 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65  if( aSample[i].e
2f59a 54 79 70 65 3e 3d 53 51 4c 49 54 45 5f 54 45 58  Type>=SQLITE_TEX
2f59b 54 20 7c 7c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e  T || aSample[i].
2f59c 75 2e 72 3e 72 20 29 20 62 72 65 61 6b 3b 0a 20  u.r>r ) break;. 
2f59d 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2f59e 7b 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  { .      sqlite3
2f59f 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2f5a0 62 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  b;.      CollSeq
2f5a1 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 63   *pColl;.      c
2f5a2 6f 6e 73 74 20 75 38 20 2a 7a 3b 0a 20 20 20 20  onst u8 *z;.    
2f5a3 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 20 20 20 20    int n;..      
2f5a4 2f 2a 20 70 56 61 6c 20 63 6f 6d 65 73 20 66 72  /* pVal comes fr
2f5a5 6f 6d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  om sqlite3ValueF
2f5a6 72 6f 6d 45 78 70 72 28 29 20 73 6f 20 74 68 65  romExpr() so the
2f5a7 20 74 79 70 65 20 63 61 6e 6e 6f 74 20 62 65 20   type cannot be 
2f5a8 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 61 73  NULL */.      as
2f5a9 73 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c  sert( eType==SQL
2f5aa 49 54 45 5f 54 45 58 54 20 7c 7c 20 65 54 79 70  ITE_TEXT || eTyp
2f5ab 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  e==SQLITE_BLOB )
2f5ac 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 65 54 79  ;..      if( eTy
2f5ad 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20  pe==SQLITE_BLOB 
2f5ae 29 7b 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 28  ){.        z = (
2f5af 63 6f 6e 73 74 20 75 38 20 2a 29 73 71 6c 69 74  const u8 *)sqlit
2f5b0 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 70 56  e3_value_blob(pV
2f5b1 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 70 43 6f  al);.        pCo
2f5b2 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
2f5b3 6c 6c 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ll;.        asse
2f5b4 72 74 28 20 70 43 6f 6c 6c 2d 3e 65 6e 63 3d 3d  rt( pColl->enc==
2f5b5 53 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20  SQLITE_UTF8 );. 
2f5b6 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2f5b7 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
2f5b8 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 64 62  te3GetCollSeq(db
2f5b9 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
2f5ba 2c 20 2a 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 29  , *pIdx->azColl)
2f5bb 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43  ;.        if( pC
2f5bc 6f 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  oll==0 ){.      
2f5bd 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2f5be 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
2f5bf 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  such collation s
2f5c0 65 71 75 65 6e 63 65 3a 20 25 73 22 2c 0a 20 20  equence: %s",.  
2f5c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f5c2 20 20 20 20 20 20 20 20 2a 70 49 64 78 2d 3e 61          *pIdx->a
2f5c3 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20  zColl);.        
2f5c4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2f5c5 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 7d  ERROR;.        }
2f5c6 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 28 63 6f  .        z = (co
2f5c7 6e 73 74 20 75 38 20 2a 29 73 71 6c 69 74 65 33  nst u8 *)sqlite3
2f5c8 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
2f5c9 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20  pColl->enc);.   
2f5ca 20 20 20 20 20 69 66 28 20 21 7a 20 29 7b 0a 20       if( !z ){. 
2f5cb 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
2f5cc 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2f5cd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2f5ce 61 73 73 65 72 74 28 20 7a 20 26 26 20 70 43 6f  assert( z && pCo
2f5cf 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43 6d  ll && pColl->xCm
2f5d0 70 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  p );.      }.   
2f5d1 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 56 61     n = sqlite3Va
2f5d2 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 70  lueBytes(pVal, p
2f5d3 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 0a 20 20 20  Coll->enc);..   
2f5d4 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53 51     for(i=0; i<SQ
2f5d5 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
2f5d6 45 53 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ES; i++){.      
2f5d7 20 20 69 6e 74 20 72 3b 0a 20 20 20 20 20 20 20    int r;.       
2f5d8 20 69 6e 74 20 65 53 61 6d 70 6c 65 74 79 70 65   int eSampletype
2f5d9 20 3d 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54   = aSample[i].eT
2f5da 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ype;.        if(
2f5db 20 65 53 61 6d 70 6c 65 74 79 70 65 3d 3d 53 51   eSampletype==SQ
2f5dc 4c 49 54 45 5f 4e 55 4c 4c 20 7c 7c 20 65 53 61  LITE_NULL || eSa
2f5dd 6d 70 6c 65 74 79 70 65 3c 65 54 79 70 65 20 29  mpletype<eType )
2f5de 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2f5df 20 20 20 69 66 28 20 28 65 53 61 6d 70 6c 65 74     if( (eSamplet
2f5e0 79 70 65 21 3d 65 54 79 70 65 29 20 29 20 62 72  ype!=eType) ) br
2f5e1 65 61 6b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  eak;.#ifndef SQL
2f5e2 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20  ITE_OMIT_UTF16. 
2f5e3 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c         if( pColl
2f5e4 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54  ->enc!=SQLITE_UT
2f5e5 46 38 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  F8 ){.          
2f5e6 69 6e 74 20 6e 53 61 6d 70 6c 65 3b 0a 20 20 20  int nSample;.   
2f5e7 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53 61         char *zSa
2f5e8 6d 70 6c 65 20 3d 20 73 71 6c 69 74 65 33 55 74  mple = sqlite3Ut
2f5e9 66 38 74 6f 31 36 28 0a 20 20 20 20 20 20 20 20  f8to16(.        
2f5ea 20 20 20 20 20 20 64 62 2c 20 70 43 6f 6c 6c 2d        db, pColl-
2f5eb 3e 65 6e 63 2c 20 61 53 61 6d 70 6c 65 5b 69 5d  >enc, aSample[i]
2f5ec 2e 75 2e 7a 2c 20 61 53 61 6d 70 6c 65 5b 69 5d  .u.z, aSample[i]
2f5ed 2e 6e 42 79 74 65 2c 20 26 6e 53 61 6d 70 6c 65  .nByte, &nSample
2f5ee 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20  .          );.  
2f5ef 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 53 61          if( !zSa
2f5f0 6d 70 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  mple ){.        
2f5f1 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
2f5f2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
2f5f3 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
2f5f4 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2f5f5 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2f5f6 20 20 20 20 20 20 20 72 20 3d 20 70 43 6f 6c 6c         r = pColl
2f5f7 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55  ->xCmp(pColl->pU
2f5f8 73 65 72 2c 20 6e 53 61 6d 70 6c 65 2c 20 7a 53  ser, nSample, zS
2f5f9 61 6d 70 6c 65 2c 20 6e 2c 20 7a 29 3b 0a 20 20  ample, n, z);.  
2f5fa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2f5fb 62 46 72 65 65 28 64 62 2c 20 7a 53 61 6d 70 6c  bFree(db, zSampl
2f5fc 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  e);.        }els
2f5fd 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  e.#endif.       
2f5fe 20 7b 0a 20 20 20 20 20 20 20 20 20 20 72 20 3d   {.          r =
2f5ff 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f   pColl->xCmp(pCo
2f600 6c 6c 2d 3e 70 55 73 65 72 2c 20 61 53 61 6d 70  ll->pUser, aSamp
2f601 6c 65 5b 69 5d 2e 6e 42 79 74 65 2c 20 61 53 61  le[i].nByte, aSa
2f602 6d 70 6c 65 5b 69 5d 2e 75 2e 7a 2c 20 6e 2c 20  mple[i].u.z, n, 
2f603 7a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  z);.        }.  
2f604 20 20 20 20 20 20 69 66 28 20 72 3e 30 20 29 20        if( r>0 ) 
2f605 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2f606 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74     }..    assert
2f607 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 53 51 4c  ( i>=0 && i<=SQL
2f608 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
2f609 53 20 29 3b 0a 20 20 20 20 2a 70 69 52 65 67 69  S );.    *piRegi
2f60a 6f 6e 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 72 65  on = i;.  }.  re
2f60b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2f60c 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69  }.#endif   /* #i
2f60d 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
2f60e 4c 45 5f 53 54 41 54 32 20 2a 2f 0a 0a 2f 2a 0a  LE_STAT2 */../*.
2f60f 2a 2a 20 49 66 20 65 78 70 72 65 73 73 69 6f 6e  ** If expression
2f610 20 70 45 78 70 72 20 72 65 70 72 65 73 65 6e 74   pExpr represent
2f611 73 20 61 20 6c 69 74 65 72 61 6c 20 76 61 6c 75  s a literal valu
2f612 65 2c 20 73 65 74 20 2a 70 70 20 74 6f 20 70 6f  e, set *pp to po
2f613 69 6e 74 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c  int to.** an sql
2f614 69 74 65 33 5f 76 61 6c 75 65 20 73 74 72 75 63  ite3_value struc
2f615 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ture containing 
2f616 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 2c 20  the same value, 
2f617 77 69 74 68 20 61 66 66 69 6e 69 74 79 0a 2a 2a  with affinity.**
2f618 20 61 66 66 20 61 70 70 6c 69 65 64 20 74 6f 20   aff applied to 
2f619 69 74 2c 20 62 65 66 6f 72 65 20 72 65 74 75 72  it, before retur
2f61a 6e 69 6e 67 2e 20 49 74 20 69 73 20 74 68 65 20  ning. It is the 
2f61b 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f  responsibility o
2f61c 66 20 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72  f the .** caller
2f61d 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 72   to eventually r
2f61e 65 6c 65 61 73 65 20 74 68 69 73 20 73 74 72 75  elease this stru
2f61f 63 74 75 72 65 20 62 79 20 70 61 73 73 69 6e 67  cture by passing
2f620 20 69 74 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74   it to .** sqlit
2f621 65 33 56 61 6c 75 65 46 72 65 65 28 29 2e 0a 2a  e3ValueFree()..*
2f622 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72  *.** If the curr
2f623 65 6e 74 20 70 61 72 73 65 20 69 73 20 61 20 72  ent parse is a r
2f624 65 63 6f 6d 70 69 6c 65 20 28 73 71 6c 69 74 65  ecompile (sqlite
2f625 33 52 65 70 72 65 70 61 72 65 28 29 29 20 61 6e  3Reprepare()) an
2f626 64 20 70 45 78 70 72 0a 2a 2a 20 69 73 20 61 6e  d pExpr.** is an
2f627 20 53 51 4c 20 76 61 72 69 61 62 6c 65 20 74 68   SQL variable th
2f628 61 74 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73  at currently has
2f629 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75   a non-NULL valu
2f62a 65 20 62 6f 75 6e 64 20 74 6f 20 69 74 2c 0a 2a  e bound to it,.*
2f62b 2a 20 63 72 65 61 74 65 20 61 6e 20 73 71 6c 69  * create an sqli
2f62c 74 65 33 5f 76 61 6c 75 65 20 73 74 72 75 63 74  te3_value struct
2f62d 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ure containing t
2f62e 68 69 73 20 76 61 6c 75 65 2c 20 61 67 61 69 6e  his value, again
2f62f 20 77 69 74 68 0a 2a 2a 20 61 66 66 69 6e 69 74   with.** affinit
2f630 79 20 61 66 66 20 61 70 70 6c 69 65 64 20 74 6f  y aff applied to
2f631 20 69 74 2c 20 69 6e 73 74 65 61 64 2e 0a 2a 2a   it, instead..**
2f632 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f  .** If neither o
2f633 66 20 74 68 65 20 61 62 6f 76 65 20 61 70 70 6c  f the above appl
2f634 79 2c 20 73 65 74 20 2a 70 70 20 74 6f 20 4e 55  y, set *pp to NU
2f635 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  LL..**.** If an 
2f636 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65  error occurs, re
2f637 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  turn an error co
2f638 64 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53  de. Otherwise, S
2f639 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 23 69 66  QLITE_OK..*/.#if
2f63a 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
2f63b 45 5f 53 54 41 54 32 0a 73 74 61 74 69 63 20 69  E_STAT2.static i
2f63c 6e 74 20 76 61 6c 75 65 46 72 6f 6d 45 78 70 72  nt valueFromExpr
2f63d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2f63e 65 2c 20 0a 20 20 45 78 70 72 20 2a 70 45 78 70  e, .  Expr *pExp
2f63f 72 2c 20 0a 20 20 75 38 20 61 66 66 2c 20 0a 20  r, .  u8 aff, . 
2f640 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2f641 2a 70 70 0a 29 7b 0a 20 20 2f 2a 20 54 68 65 20  *pp.){.  /* The 
2f642 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 28 29 20  evalConstExpr() 
2f643 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 68 61  function will ha
2f644 76 65 20 61 6c 72 65 61 64 79 20 63 6f 6e 76 65  ve already conve
2f645 72 74 65 64 20 61 6e 79 20 54 4b 5f 56 41 52 49  rted any TK_VARI
2f646 41 42 4c 45 0a 20 20 2a 2a 20 65 78 70 72 65 73  ABLE.  ** expres
2f647 73 69 6f 6e 20 69 6e 76 6f 6c 76 65 64 20 69 6e  sion involved in
2f648 20 61 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69   an comparison i
2f649 6e 74 6f 20 61 20 54 4b 5f 52 45 47 49 53 54 45  nto a TK_REGISTE
2f64a 52 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  R. */.  assert( 
2f64b 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 56 41  pExpr->op!=TK_VA
2f64c 52 49 41 42 4c 45 20 29 3b 0a 20 20 69 66 28 20  RIABLE );.  if( 
2f64d 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45  pExpr->op==TK_RE
2f64e 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 2d  GISTER && pExpr-
2f64f 3e 6f 70 32 3d 3d 54 4b 5f 56 41 52 49 41 42 4c  >op2==TK_VARIABL
2f650 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 56 61  E ){.    int iVa
2f651 72 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  r = pExpr->iColu
2f652 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  mn;.    sqlite3V
2f653 64 62 65 53 65 74 56 61 72 6d 61 73 6b 28 70 50  dbeSetVarmask(pP
2f654 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 69 56 61  arse->pVdbe, iVa
2f655 72 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20 73 71  r);.    *pp = sq
2f656 6c 69 74 65 33 56 64 62 65 47 65 74 56 61 6c 75  lite3VdbeGetValu
2f657 65 28 70 50 61 72 73 65 2d 3e 70 52 65 70 72 65  e(pParse->pRepre
2f658 70 61 72 65 2c 20 69 56 61 72 2c 20 61 66 66 29  pare, iVar, aff)
2f659 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
2f65a 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65  ITE_OK;.  }.  re
2f65b 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75  turn sqlite3Valu
2f65c 65 46 72 6f 6d 45 78 70 72 28 70 50 61 72 73 65  eFromExpr(pParse
2f65d 2d 3e 64 62 2c 20 70 45 78 70 72 2c 20 53 51 4c  ->db, pExpr, SQL
2f65e 49 54 45 5f 55 54 46 38 2c 20 61 66 66 2c 20 70  ITE_UTF8, aff, p
2f65f 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  p);.}.#endif../*
2f660 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2f661 6e 20 69 73 20 75 73 65 64 20 74 6f 20 65 73 74  n is used to est
2f662 69 6d 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72  imate the number
2f663 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69   of rows that wi
2f664 6c 6c 20 62 65 20 76 69 73 69 74 65 64 0a 2a 2a  ll be visited.**
2f665 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 61 6e 20   by scanning an 
2f666 69 6e 64 65 78 20 66 6f 72 20 61 20 72 61 6e 67  index for a rang
2f667 65 20 6f 66 20 76 61 6c 75 65 73 2e 20 54 68 65  e of values. The
2f668 20 72 61 6e 67 65 20 6d 61 79 20 68 61 76 65 20   range may have 
2f669 61 6e 20 75 70 70 65 72 0a 2a 2a 20 62 6f 75 6e  an upper.** boun
2f66a 64 2c 20 61 20 6c 6f 77 65 72 20 62 6f 75 6e 64  d, a lower bound
2f66b 2c 20 6f 72 20 62 6f 74 68 2e 20 54 68 65 20 57  , or both. The W
2f66c 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
2f66d 73 20 74 68 61 74 20 73 65 74 20 74 68 65 20 75  s that set the u
2f66e 70 70 65 72 0a 2a 2a 20 61 6e 64 20 6c 6f 77 65  pper.** and lowe
2f66f 72 20 62 6f 75 6e 64 73 20 61 72 65 20 72 65 70  r bounds are rep
2f670 72 65 73 65 6e 74 65 64 20 62 79 20 70 4c 6f 77  resented by pLow
2f671 65 72 20 61 6e 64 20 70 55 70 70 65 72 20 72 65  er and pUpper re
2f672 73 70 65 63 74 69 76 65 6c 79 2e 20 46 6f 72 0a  spectively. For.
2f673 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75  ** example, assu
2f674 6d 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20  ming that index 
2f675 70 20 69 73 20 6f 6e 20 74 31 28 61 29 3a 0a 2a  p is on t1(a):.*
2f676 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d 20  *.**   ... FROM 
2f677 74 31 20 57 48 45 52 45 20 61 20 3e 20 3f 20 41  t1 WHERE a > ? A
2f678 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 20  ND a < ? ....** 
2f679 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f67a 20 20 20 7c 5f 5f 5f 5f 5f 7c 20 20 20 7c 5f 5f     |_____|   |__
2f67b 5f 5f 5f 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ___|.**         
2f67c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
2f67d 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20          |.**    
2f67e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f67f 20 70 4c 6f 77 65 72 20 20 20 20 70 55 70 70 65   pLower    pUppe
2f680 72 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65  r.**.** If eithe
2f681 72 20 6f 66 20 74 68 65 20 75 70 70 65 72 20 6f  r of the upper o
2f682 72 20 6c 6f 77 65 72 20 62 6f 75 6e 64 20 69 73  r lower bound is
2f683 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20 74 68   not present, th
2f684 65 6e 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65  en NULL is passe
2f685 64 20 69 6e 0a 2a 2a 20 70 6c 61 63 65 20 6f 66  d in.** place of
2f686 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
2f687 6e 67 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a  ng WhereTerm..**
2f688 0a 2a 2a 20 54 68 65 20 6e 45 71 20 70 61 72 61  .** The nEq para
2f689 6d 65 74 65 72 20 69 73 20 70 61 73 73 65 64 20  meter is passed 
2f68a 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
2f68b 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 73 75   index column su
2f68c 62 6a 65 63 74 20 74 6f 20 74 68 65 0a 2a 2a 20  bject to the.** 
2f68d 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74  range constraint
2f68e 2e 20 4f 72 2c 20 65 71 75 69 76 61 6c 65 6e 74  . Or, equivalent
2f68f 6c 79 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ly, the number o
2f690 66 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  f equality const
2f691 72 61 69 6e 74 73 0a 2a 2a 20 6f 70 74 69 6d 69  raints.** optimi
2f692 7a 65 64 20 62 79 20 74 68 65 20 70 72 6f 70 6f  zed by the propo
2f693 73 65 64 20 69 6e 64 65 78 20 73 63 61 6e 2e 20  sed index scan. 
2f694 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 73 73  For example, ass
2f695 75 6d 69 6e 67 20 69 6e 64 65 78 20 70 20 69 73  uming index p is
2f696 0a 2a 2a 20 6f 6e 20 74 31 28 61 2c 20 62 29 2c  .** on t1(a, b),
2f697 20 61 6e 64 20 74 68 65 20 53 51 4c 20 71 75 65   and the SQL que
2f698 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e  ry is:.**.**   .
2f699 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45  .. FROM t1 WHERE
2f69a 20 61 20 3d 20 3f 20 41 4e 44 20 62 20 3e 20 3f   a = ? AND b > ?
2f69b 20 41 4e 44 20 62 20 3c 20 3f 20 2e 2e 2e 0a 2a   AND b < ? ....*
2f69c 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45 71 20 73 68  *.** then nEq sh
2f69d 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 74  ould be passed t
2f69e 68 65 20 76 61 6c 75 65 20 31 20 28 61 73 20 74  he value 1 (as t
2f69f 68 65 20 72 61 6e 67 65 20 72 65 73 74 72 69 63  he range restric
2f6a0 74 65 64 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 62  ted column,.** b
2f6a1 2c 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  , is the second 
2f6a2 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
2f6a3 20 6f 66 20 74 68 65 20 69 6e 64 65 78 29 2e 20   of the index). 
2f6a4 4f 72 2c 20 69 66 20 74 68 65 20 71 75 65 72 79  Or, if the query
2f6a5 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e   is:.**.**   ...
2f6a6 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
2f6a7 20 3e 20 3f 20 41 4e 44 20 61 20 3c 20 3f 20 2e   > ? AND a < ? .
2f6a8 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45  ...**.** then nE
2f6a9 71 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73  q should be pass
2f6aa 65 64 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ed 0..**.** The 
2f6ab 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
2f6ac 73 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74  s an integer bet
2f6ad 77 65 65 6e 20 31 20 61 6e 64 20 31 30 30 2c 20  ween 1 and 100, 
2f6ae 69 6e 63 6c 75 73 69 76 65 2e 20 41 20 72 65 74  inclusive. A ret
2f6af 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20  urn.** value of 
2f6b0 31 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  1 indicates that
2f6b1 20 74 68 65 20 70 72 6f 70 6f 73 65 64 20 72 61   the proposed ra
2f6b2 6e 67 65 20 73 63 61 6e 20 69 73 20 65 78 70 65  nge scan is expe
2f6b3 63 74 65 64 20 74 6f 20 76 69 73 69 74 0a 2a 2a  cted to visit.**
2f6b4 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 31   approximately 1
2f6b5 2f 31 30 30 74 68 20 28 31 25 29 20 6f 66 20 74  /100th (1%) of t
2f6b6 68 65 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64  he rows selected
2f6b7 20 62 79 20 74 68 65 20 6e 45 71 20 65 71 75 61   by the nEq equa
2f6b8 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73 74 72 61 69  lity.** constrai
2f6b9 6e 74 73 20 28 69 66 20 61 6e 79 29 2e 20 41 20  nts (if any). A 
2f6ba 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
2f6bb 31 30 30 20 69 6e 64 69 63 61 74 65 73 20 74 68  100 indicates th
2f6bc 61 74 20 69 74 20 69 73 20 65 78 70 65 63 74 65  at it is expecte
2f6bd 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 61  d.** that the ra
2f6be 6e 67 65 20 73 63 61 6e 20 77 69 6c 6c 20 76 69  nge scan will vi
2f6bf 73 69 74 20 65 76 65 72 79 20 72 6f 77 20 28 31  sit every row (1
2f6c0 30 30 25 29 20 73 65 6c 65 63 74 65 64 20 62 79  00%) selected by
2f6c1 20 74 68 65 20 65 71 75 61 6c 69 74 79 0a 2a 2a   the equality.**
2f6c2 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a   constraints..**
2f6c3 0a 2a 2a 20 49 6e 20 74 68 65 20 61 62 73 65 6e  .** In the absen
2f6c4 63 65 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61  ce of sqlite_sta
2f6c5 74 32 20 41 4e 41 4c 59 5a 45 20 64 61 74 61 2c  t2 ANALYZE data,
2f6c6 20 65 61 63 68 20 72 61 6e 67 65 20 69 6e 65 71   each range ineq
2f6c7 75 61 6c 69 74 79 0a 2a 2a 20 72 65 64 75 63 65  uality.** reduce
2f6c8 73 20 74 68 65 20 73 65 61 72 63 68 20 73 70 61  s the search spa
2f6c9 63 65 20 62 79 20 32 2f 33 72 64 73 2e 20 20 48  ce by 2/3rds.  H
2f6ca 65 6e 63 65 20 61 20 73 69 6e 67 6c 65 20 63 6f  ence a single co
2f6cb 6e 73 74 72 61 69 6e 74 20 28 78 3e 3f 29 0a 2a  nstraint (x>?).*
2f6cc 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 72  * results in a r
2f6cd 65 74 75 72 6e 20 6f 66 20 33 33 20 61 6e 64 20  eturn of 33 and 
2f6ce 61 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69  a range constrai
2f6cf 6e 74 20 28 78 3e 3f 20 41 4e 44 20 78 3c 3f 29  nt (x>? AND x<?)
2f6d0 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 20 61   results.** in a
2f6d1 20 72 65 74 75 72 6e 20 6f 66 20 31 31 2e 0a 2a   return of 11..*
2f6d2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65  /.static int whe
2f6d3 72 65 52 61 6e 67 65 53 63 61 6e 45 73 74 28 0a  reRangeScanEst(.
2f6d4 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2f6d5 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2f6d6 67 20 26 20 63 6f 64 65 20 67 65 6e 65 72 61 74  g & code generat
2f6d7 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2f6d8 20 49 6e 64 65 78 20 2a 70 2c 20 20 20 20 20 20   Index *p,      
2f6d9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
2f6da 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ex containing th
2f6db 65 20 72 61 6e 67 65 2d 63 6f 6d 70 61 72 65 64  e range-compared
2f6dc 20 63 6f 6c 75 6d 6e 3b 20 22 78 22 20 2a 2f 0a   column; "x" */.
2f6dd 20 20 69 6e 74 20 6e 45 71 2c 20 20 20 20 20 20    int nEq,      
2f6de 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65 78 20         /* index 
2f6df 69 6e 74 6f 20 70 2d 3e 61 43 6f 6c 5b 5d 20 6f  into p->aCol[] o
2f6e0 66 20 74 68 65 20 72 61 6e 67 65 2d 63 6f 6d 70  f the range-comp
2f6e1 61 72 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  ared column */. 
2f6e2 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4c 6f 77   WhereTerm *pLow
2f6e3 65 72 2c 20 20 20 2f 2a 20 4c 6f 77 65 72 20 62  er,   /* Lower b
2f6e4 6f 75 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e 67  ound on the rang
2f6e5 65 2e 20 65 78 3a 20 22 78 3e 31 32 33 22 20 4d  e. ex: "x>123" M
2f6e6 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  ight be NULL */.
2f6e7 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 55 70    WhereTerm *pUp
2f6e8 70 65 72 2c 20 20 20 2f 2a 20 55 70 70 65 72 20  per,   /* Upper 
2f6e9 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e  bound on the ran
2f6ea 67 65 2e 20 65 78 3a 20 22 78 3c 34 35 35 22 20  ge. ex: "x<455" 
2f6eb 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  Might be NULL */
2f6ec 0a 20 20 69 6e 74 20 2a 70 69 45 73 74 20 20 20  .  int *piEst   
2f6ed 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
2f6ee 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a  Return value */.
2f6ef 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
2f6f0 4c 49 54 45 5f 4f 4b 3b 0a 0a 23 69 66 64 65 66  LITE_OK;..#ifdef
2f6f1 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
2f6f2 54 41 54 32 0a 0a 20 20 69 66 28 20 6e 45 71 3d  TAT2..  if( nEq=
2f6f3 3d 30 20 26 26 20 70 2d 3e 61 53 61 6d 70 6c 65  =0 && p->aSample
2f6f4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2f6f5 76 61 6c 75 65 20 2a 70 4c 6f 77 65 72 56 61 6c  value *pLowerVal
2f6f6 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
2f6f7 33 5f 76 61 6c 75 65 20 2a 70 55 70 70 65 72 56  3_value *pUpperV
2f6f8 61 6c 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  al = 0;.    int 
2f6f9 69 45 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 4c  iEst;.    int iL
2f6fa 6f 77 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e  ower = 0;.    in
2f6fb 74 20 69 55 70 70 65 72 20 3d 20 53 51 4c 49 54  t iUpper = SQLIT
2f6fc 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b  E_INDEX_SAMPLES;
2f6fd 0a 20 20 20 20 75 38 20 61 66 66 20 3d 20 70 2d  .    u8 aff = p-
2f6fe 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b 70 2d  >pTable->aCol[p-
2f6ff 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 5d 2e 61 66  >aiColumn[0]].af
2f700 66 69 6e 69 74 79 3b 0a 0a 20 20 20 20 69 66 28  finity;..    if(
2f701 20 70 4c 6f 77 65 72 20 29 7b 0a 20 20 20 20 20   pLower ){.     
2f702 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70   Expr *pExpr = p
2f703 4c 6f 77 65 72 2d 3e 70 45 78 70 72 2d 3e 70 52  Lower->pExpr->pR
2f704 69 67 68 74 3b 0a 20 20 20 20 20 20 72 63 20 3d  ight;.      rc =
2f705 20 76 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 70   valueFromExpr(p
2f706 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 61 66  Parse, pExpr, af
2f707 66 2c 20 26 70 4c 6f 77 65 72 56 61 6c 29 3b 0a  f, &pLowerVal);.
2f708 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
2f709 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
2f70a 55 70 70 65 72 20 29 7b 0a 20 20 20 20 20 20 45  Upper ){.      E
2f70b 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 55 70  xpr *pExpr = pUp
2f70c 70 65 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67  per->pExpr->pRig
2f70d 68 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 76  ht;.      rc = v
2f70e 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 70 50 61  alueFromExpr(pPa
2f70f 72 73 65 2c 20 70 45 78 70 72 2c 20 61 66 66 2c  rse, pExpr, aff,
2f710 20 26 70 55 70 70 65 72 56 61 6c 29 3b 0a 20 20   &pUpperVal);.  
2f711 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21    }..    if( rc!
2f712 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 70  =SQLITE_OK || (p
2f713 4c 6f 77 65 72 56 61 6c 3d 3d 30 20 26 26 20 70  LowerVal==0 && p
2f714 55 70 70 65 72 56 61 6c 3d 3d 30 29 20 29 7b 0a  UpperVal==0) ){.
2f715 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
2f716 75 65 46 72 65 65 28 70 4c 6f 77 65 72 56 61 6c  ueFree(pLowerVal
2f717 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2f718 56 61 6c 75 65 46 72 65 65 28 70 55 70 70 65 72  ValueFree(pUpper
2f719 56 61 6c 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  Val);.      goto
2f71a 20 72 61 6e 67 65 5f 65 73 74 5f 66 61 6c 6c 62   range_est_fallb
2f71b 61 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ack;.    }else i
2f71c 66 28 20 70 4c 6f 77 65 72 56 61 6c 3d 3d 30 20  f( pLowerVal==0 
2f71d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 68  ){.      rc = wh
2f71e 65 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70  ereRangeRegion(p
2f71f 50 61 72 73 65 2c 20 70 2c 20 70 55 70 70 65 72  Parse, p, pUpper
2f720 56 61 6c 2c 20 26 69 55 70 70 65 72 29 3b 0a 20  Val, &iUpper);. 
2f721 20 20 20 20 20 69 66 28 20 70 4c 6f 77 65 72 20       if( pLower 
2f722 29 20 69 4c 6f 77 65 72 20 3d 20 69 55 70 70 65  ) iLower = iUppe
2f723 72 2f 32 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  r/2;.    }else i
2f724 66 28 20 70 55 70 70 65 72 56 61 6c 3d 3d 30 20  f( pUpperVal==0 
2f725 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 68  ){.      rc = wh
2f726 65 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70  ereRangeRegion(p
2f727 50 61 72 73 65 2c 20 70 2c 20 70 4c 6f 77 65 72  Parse, p, pLower
2f728 56 61 6c 2c 20 26 69 4c 6f 77 65 72 29 3b 0a 20  Val, &iLower);. 
2f729 20 20 20 20 20 69 66 28 20 70 55 70 70 65 72 20       if( pUpper 
2f72a 29 20 69 55 70 70 65 72 20 3d 20 28 69 4c 6f 77  ) iUpper = (iLow
2f72b 65 72 20 2b 20 53 51 4c 49 54 45 5f 49 4e 44 45  er + SQLITE_INDE
2f72c 58 5f 53 41 4d 50 4c 45 53 20 2b 20 31 29 2f 32  X_SAMPLES + 1)/2
2f72d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2f72e 20 20 20 72 63 20 3d 20 77 68 65 72 65 52 61 6e     rc = whereRan
2f72f 67 65 52 65 67 69 6f 6e 28 70 50 61 72 73 65 2c  geRegion(pParse,
2f730 20 70 2c 20 70 55 70 70 65 72 56 61 6c 2c 20 26   p, pUpperVal, &
2f731 69 55 70 70 65 72 29 3b 0a 20 20 20 20 20 20 69  iUpper);.      i
2f732 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2f733 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
2f734 20 77 68 65 72 65 52 61 6e 67 65 52 65 67 69 6f   whereRangeRegio
2f735 6e 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4c 6f  n(pParse, p, pLo
2f736 77 65 72 56 61 6c 2c 20 26 69 4c 6f 77 65 72 29  werVal, &iLower)
2f737 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2f738 0a 20 20 20 20 69 45 73 74 20 3d 20 69 55 70 70  .    iEst = iUpp
2f739 65 72 20 2d 20 69 4c 6f 77 65 72 3b 0a 20 20 20  er - iLower;.   
2f73a 20 74 65 73 74 63 61 73 65 28 20 69 45 73 74 3d   testcase( iEst=
2f73b 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41  =SQLITE_INDEX_SA
2f73c 4d 50 4c 45 53 20 29 3b 0a 20 20 20 20 61 73 73  MPLES );.    ass
2f73d 65 72 74 28 20 69 45 73 74 3c 3d 53 51 4c 49 54  ert( iEst<=SQLIT
2f73e 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20  E_INDEX_SAMPLES 
2f73f 29 3b 0a 20 20 20 20 69 66 28 20 69 45 73 74 3c  );.    if( iEst<
2f740 31 20 29 7b 0a 20 20 20 20 20 20 69 45 73 74 20  1 ){.      iEst 
2f741 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 1;.    }..    
2f742 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
2f743 28 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20 20  (pLowerVal);.   
2f744 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
2f745 65 28 70 55 70 70 65 72 56 61 6c 29 3b 0a 20 20  e(pUpperVal);.  
2f746 20 20 2a 70 69 45 73 74 20 3d 20 28 69 45 73 74    *piEst = (iEst
2f747 20 2a 20 31 30 30 29 2f 53 51 4c 49 54 45 5f 49   * 100)/SQLITE_I
2f748 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20  NDEX_SAMPLES;.  
2f749 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
2f74a 0a 72 61 6e 67 65 5f 65 73 74 5f 66 61 6c 6c 62  .range_est_fallb
2f74b 61 63 6b 3a 0a 23 65 6c 73 65 0a 20 20 55 4e 55  ack:.#else.  UNU
2f74c 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50  SED_PARAMETER(pP
2f74d 61 72 73 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f  arse);.  UNUSED_
2f74e 50 41 52 41 4d 45 54 45 52 28 70 29 3b 0a 20 20  PARAMETER(p);.  
2f74f 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
2f750 28 6e 45 71 29 3b 0a 23 65 6e 64 69 66 0a 20 20  (nEq);.#endif.  
2f751 61 73 73 65 72 74 28 20 70 4c 6f 77 65 72 20 7c  assert( pLower |
2f752 7c 20 70 55 70 70 65 72 20 29 3b 0a 20 20 69 66  | pUpper );.  if
2f753 28 20 70 4c 6f 77 65 72 20 26 26 20 70 55 70 70  ( pLower && pUpp
2f754 65 72 20 29 7b 0a 20 20 20 20 2a 70 69 45 73 74  er ){.    *piEst
2f755 20 3d 20 31 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a   = 11;.  }else{.
2f756 20 20 20 20 2a 70 69 45 73 74 20 3d 20 33 33 3b      *piEst = 33;
2f757 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2f758 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ;.}.../*.** Find
2f759 20 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20   the query plan 
2f75a 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 61 20  for accessing a 
2f75b 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65  particular table
2f75c 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20  .  Write the.** 
2f75d 62 65 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20  best query plan 
2f75e 61 6e 64 20 69 74 73 20 63 6f 73 74 20 69 6e 74  and its cost int
2f75f 6f 20 74 68 65 20 57 68 65 72 65 43 6f 73 74 20  o the WhereCost 
2f760 6f 62 6a 65 63 74 20 73 75 70 70 6c 69 65 64 20  object supplied 
2f761 61 73 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70  as the.** last p
2f762 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  arameter..**.** 
2f763 54 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20  The lowest cost 
2f764 70 6c 61 6e 20 77 69 6e 73 2e 20 20 54 68 65 20  plan wins.  The 
2f765 63 6f 73 74 20 69 73 20 61 6e 20 65 73 74 69 6d  cost is an estim
2f766 61 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e  ate of the amoun
2f767 74 20 6f 66 0a 2a 2a 20 43 50 55 20 61 6e 64 20  t of.** CPU and 
2f768 64 69 73 6b 20 49 2f 4f 20 6e 65 65 64 20 74 6f  disk I/O need to
2f769 20 70 72 6f 63 65 73 73 20 74 68 65 20 72 65 71   process the req
2f76a 75 65 73 74 20 75 73 69 6e 67 20 74 68 65 20 73  uest using the s
2f76b 65 6c 65 63 74 65 64 20 70 6c 61 6e 2e 0a 2a 2a  elected plan..**
2f76c 20 46 61 63 74 6f 72 73 20 74 68 61 74 20 69 6e   Factors that in
2f76d 66 6c 75 65 6e 63 65 20 63 6f 73 74 20 69 6e 63  fluence cost inc
2f76e 6c 75 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  lude:.**.**    *
2f76f 20 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 20    The estimated 
2f770 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74  number of rows t
2f771 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 72  hat will be retr
2f772 69 65 76 65 64 2e 20 20 28 54 68 65 0a 2a 2a 20  ieved.  (The.** 
2f773 20 20 20 20 20 20 66 65 77 65 72 20 74 68 65 20        fewer the 
2f774 62 65 74 74 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 20  better.).**.**  
2f775 20 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20    *  Whether or 
2f776 6e 6f 74 20 73 6f 72 74 69 6e 67 20 6d 75 73 74  not sorting must
2f777 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20   occur..**.**   
2f778 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e   *  Whether or n
2f779 6f 74 20 74 68 65 72 65 20 6d 75 73 74 20 62 65  ot there must be
2f77a 20 73 65 70 61 72 61 74 65 20 6c 6f 6f 6b 75 70   separate lookup
2f77b 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20  s in the.**     
2f77c 20 20 69 6e 64 65 78 20 61 6e 64 20 69 6e 20 74    index and in t
2f77d 68 65 20 6d 61 69 6e 20 74 61 62 6c 65 2e 0a 2a  he main table..*
2f77e 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 77 61  *.** If there wa
2f77f 73 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20  s an INDEXED BY 
2f780 63 6c 61 75 73 65 20 28 70 53 72 63 2d 3e 70 49  clause (pSrc->pI
2f781 6e 64 65 78 29 20 61 74 74 61 63 68 65 64 20 74  ndex) attached t
2f782 6f 20 74 68 65 20 74 61 62 6c 65 20 69 6e 0a 2a  o the table in.*
2f783 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  * the SQL statem
2f784 65 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66  ent, then this f
2f785 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 63 6f 6e  unction only con
2f786 73 69 64 65 72 73 20 70 6c 61 6e 73 20 75 73 69  siders plans usi
2f787 6e 67 20 74 68 65 20 0a 2a 2a 20 6e 61 6d 65 64  ng the .** named
2f788 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 73 75   index. If no su
2f789 63 68 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64  ch plan is found
2f78a 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72  , then the retur
2f78b 6e 65 64 20 63 6f 73 74 20 69 73 0a 2a 2a 20 53  ned cost is.** S
2f78c 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2e 20 49  QLITE_BIG_DBL. I
2f78d 66 20 61 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e  f a plan is foun
2f78e 64 20 74 68 61 74 20 75 73 65 73 20 74 68 65 20  d that uses the 
2f78f 6e 61 6d 65 64 20 69 6e 64 65 78 2c 20 0a 2a 2a  named index, .**
2f790 20 74 68 65 6e 20 74 68 65 20 63 6f 73 74 20 69   then the cost i
2f791 73 20 63 61 6c 63 75 6c 61 74 65 64 20 69 6e 20  s calculated in 
2f792 74 68 65 20 75 73 75 61 6c 20 77 61 79 2e 0a 2a  the usual way..*
2f793 2a 0a 2a 2a 20 49 66 20 61 20 4e 4f 54 20 49 4e  *.** If a NOT IN
2f794 44 45 58 45 44 20 63 6c 61 75 73 65 20 28 70 53  DEXED clause (pS
2f795 72 63 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 21 3d  rc->notIndexed!=
2f796 30 29 20 77 61 73 20 61 74 74 61 63 68 65 64 20  0) was attached 
2f797 74 6f 20 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a  to the table .**
2f798 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73   in the SELECT s
2f799 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 6e  tatement, then n
2f79a 6f 20 69 6e 64 65 78 65 73 20 61 72 65 20 63 6f  o indexes are co
2f79b 6e 73 69 64 65 72 65 64 2e 20 48 6f 77 65 76 65  nsidered. Howeve
2f79c 72 2c 20 74 68 65 20 0a 2a 2a 20 73 65 6c 65 63  r, the .** selec
2f79d 74 65 64 20 70 6c 61 6e 20 6d 61 79 20 73 74 69  ted plan may sti
2f79e 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  ll take advantag
2f79f 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20  e of the tables 
2f7a0 62 75 69 6c 74 2d 69 6e 20 72 6f 77 69 64 0a 2a  built-in rowid.*
2f7a1 2a 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74  * index..*/.stat
2f7a2 69 63 20 76 6f 69 64 20 62 65 73 74 42 74 72 65  ic void bestBtre
2f7a3 65 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  eIndex(.  Parse 
2f7a4 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2f7a5 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2f7a6 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2f7a7 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2f7a8 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  WC,           /*
2f7a9 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2f7aa 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  e */.  struct Sr
2f7ab 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63  cList_item *pSrc
2f7ac 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63  ,  /* The FROM c
2f7ad 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65  lause term to se
2f7ae 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73  arch */.  Bitmas
2f7af 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20  k notReady,     
2f7b0 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2f7b1 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 61 72   cursors that ar
2f7b2 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  e not available 
2f7b3 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2f7b4 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20  OrderBy,        
2f7b5 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2f7b6 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
2f7b7 72 65 43 6f 73 74 20 2a 70 43 6f 73 74 20 20 20  reCost *pCost   
2f7b8 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65           /* Lowe
2f7b9 73 74 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c  st cost query pl
2f7ba 61 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  an */.){.  int i
2f7bb 43 75 72 20 3d 20 70 53 72 63 2d 3e 69 43 75 72  Cur = pSrc->iCur
2f7bc 73 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75  sor;   /* The cu
2f7bd 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c  rsor of the tabl
2f7be 65 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64  e to be accessed
2f7bf 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50 72   */.  Index *pPr
2f7c0 6f 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  obe;            
2f7c1 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 77 65    /* An index we
2f7c2 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20   are evaluating 
2f7c3 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
2f7c4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f7c5 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50 72 6f   /* Copy of pPro
2f7c6 62 65 2c 20 6f 72 20 7a 65 72 6f 20 66 6f 72 20  be, or zero for 
2f7c7 49 50 4b 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  IPK index */.  i
2f7c8 6e 74 20 65 71 54 65 72 6d 4d 61 73 6b 3b 20 20  nt eqTermMask;  
2f7c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
2f7ca 72 72 65 6e 74 20 6d 61 73 6b 20 6f 66 20 76 61  rrent mask of va
2f7cb 6c 69 64 20 65 71 75 61 6c 69 74 79 20 6f 70 65  lid equality ope
2f7cc 72 61 74 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20  rators */.  int 
2f7cd 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 3b 20 20  idxEqTermMask;  
2f7ce 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2f7cf 20 6d 61 73 6b 20 6f 66 20 76 61 6c 69 64 20 65   mask of valid e
2f7d0 71 75 61 6c 69 74 79 20 6f 70 65 72 61 74 6f 72  quality operator
2f7d1 73 20 2a 2f 0a 20 20 49 6e 64 65 78 20 73 50 6b  s */.  Index sPk
2f7d2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f7d3 20 20 20 2f 2a 20 41 20 66 61 6b 65 20 69 6e 64     /* A fake ind
2f7d4 65 78 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68  ex object for th
2f7d5 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f  e primary key */
2f7d6 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
2f7d7 61 69 52 6f 77 45 73 74 50 6b 5b 32 5d 3b 20 2f  aiRowEstPk[2]; /
2f7d8 2a 20 54 68 65 20 61 69 52 6f 77 45 73 74 5b 5d  * The aiRowEst[]
2f7d9 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73   value for the s
2f7da 50 6b 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  Pk index */.  in
2f7db 74 20 61 69 43 6f 6c 75 6d 6e 50 6b 20 3d 20 2d  t aiColumnPk = -
2f7dc 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  1;        /* The
2f7dd 20 61 43 6f 6c 75 6d 6e 5b 5d 20 76 61 6c 75 65   aColumn[] value
2f7de 20 66 6f 72 20 74 68 65 20 73 50 6b 20 69 6e 64   for the sPk ind
2f7df 65 78 20 2a 2f 0a 20 20 69 6e 74 20 77 73 46 6c  ex */.  int wsFl
2f7e0 61 67 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  agMask;         
2f7e1 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66      /* Allowed f
2f7e2 6c 61 67 73 20 69 6e 20 70 43 6f 73 74 2d 3e 70  lags in pCost->p
2f7e3 6c 61 6e 2e 77 73 46 6c 61 67 20 2a 2f 0a 0a 20  lan.wsFlag */.. 
2f7e4 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
2f7e5 68 65 20 63 6f 73 74 20 74 6f 20 61 20 77 6f 72  he cost to a wor
2f7e6 73 74 2d 63 61 73 65 20 76 61 6c 75 65 20 2a 2f  st-case value */
2f7e7 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c  .  memset(pCost,
2f7e8 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73   0, sizeof(*pCos
2f7e9 74 29 29 3b 0a 20 20 70 43 6f 73 74 2d 3e 72 43  t));.  pCost->rC
2f7ea 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47  ost = SQLITE_BIG
2f7eb 5f 44 42 4c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  _DBL;..  /* If t
2f7ec 68 65 20 70 53 72 63 20 74 61 62 6c 65 20 69 73  he pSrc table is
2f7ed 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65   the right table
2f7ee 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20   of a LEFT JOIN 
2f7ef 74 68 65 6e 20 77 65 20 6d 61 79 20 6e 6f 74 0a  then we may not.
2f7f0 20 20 2a 2a 20 75 73 65 20 61 6e 20 69 6e 64 65    ** use an inde
2f7f1 78 20 74 6f 20 73 61 74 69 73 66 79 20 49 53 20  x to satisfy IS 
2f7f2 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73  NULL constraints
2f7f3 20 6f 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 20   on that table. 
2f7f4 20 54 68 69 73 20 69 73 0a 20 20 2a 2a 20 62 65   This is.  ** be
2f7f5 63 61 75 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 69  cause columns mi
2f7f6 67 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67  ght end up being
2f7f7 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 74 61 62   NULL if the tab
2f7f8 6c 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63  le does not matc
2f7f9 68 20 2d 0a 20 20 2a 2a 20 61 20 63 69 72 63 75  h -.  ** a circu
2f7fa 6d 73 74 61 6e 63 65 20 77 68 69 63 68 20 74 68  mstance which th
2f7fb 65 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 68  e index cannot h
2f7fc 65 6c 70 20 75 73 20 64 69 73 63 6f 76 65 72 2e  elp us discover.
2f7fd 20 20 54 69 63 6b 65 74 20 23 32 31 37 37 2e 0a    Ticket #2177..
2f7fe 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 2d    */.  if( pSrc-
2f7ff 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c  >jointype & JT_L
2f800 45 46 54 20 29 7b 0a 20 20 20 20 69 64 78 45 71  EFT ){.    idxEq
2f801 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51  TermMask = WO_EQ
2f802 7c 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b  |WO_IN;.  }else{
2f803 0a 20 20 20 20 69 64 78 45 71 54 65 72 6d 4d 61  .    idxEqTermMa
2f804 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e  sk = WO_EQ|WO_IN
2f805 7c 57 4f 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 0a  |WO_ISNULL;.  }.
2f806 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70 49 6e  .  if( pSrc->pIn
2f807 64 65 78 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e  dex ){.    /* An
2f808 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
2f809 73 65 20 73 70 65 63 69 66 69 65 73 20 61 20 70  se specifies a p
2f80a 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20  articular index 
2f80b 74 6f 20 75 73 65 20 2a 2f 0a 20 20 20 20 70 49  to use */.    pI
2f80c 64 78 20 3d 20 70 50 72 6f 62 65 20 3d 20 70 53  dx = pProbe = pS
2f80d 72 63 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20  rc->pIndex;.    
2f80e 77 73 46 6c 61 67 4d 61 73 6b 20 3d 20 7e 28 57  wsFlagMask = ~(W
2f80f 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48  HERE_ROWID_EQ|WH
2f810 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29  ERE_ROWID_RANGE)
2f811 3b 0a 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b  ;.    eqTermMask
2f812 20 3d 20 69 64 78 45 71 54 65 72 6d 4d 61 73 6b   = idxEqTermMask
2f813 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
2f814 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 49 4e  * There is no IN
2f815 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 2e  DEXED BY clause.
2f816 20 20 43 72 65 61 74 65 20 61 20 66 61 6b 65 20    Create a fake 
2f817 49 6e 64 65 78 20 6f 62 6a 65 63 74 20 74 6f 0a  Index object to.
2f818 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74      ** represent
2f819 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
2f81a 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   */.    Index *p
2f81b 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20  First;          
2f81c 20 20 20 20 20 20 2f 2a 20 41 6e 79 20 6f 74 68        /* Any oth
2f81d 65 72 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20  er index on the 
2f81e 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 6d  table */.    mem
2f81f 73 65 74 28 26 73 50 6b 2c 20 30 2c 20 73 69 7a  set(&sPk, 0, siz
2f820 65 6f 66 28 49 6e 64 65 78 29 29 3b 0a 20 20 20  eof(Index));.   
2f821 20 73 50 6b 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 31   sPk.nColumn = 1
2f822 3b 0a 20 20 20 20 73 50 6b 2e 61 69 43 6f 6c 75  ;.    sPk.aiColu
2f823 6d 6e 20 3d 20 26 61 69 43 6f 6c 75 6d 6e 50 6b  mn = &aiColumnPk
2f824 3b 0a 20 20 20 20 73 50 6b 2e 61 69 52 6f 77 45  ;.    sPk.aiRowE
2f825 73 74 20 3d 20 61 69 52 6f 77 45 73 74 50 6b 3b  st = aiRowEstPk;
2f826 0a 20 20 20 20 61 69 52 6f 77 45 73 74 50 6b 5b  .    aiRowEstPk[
2f827 31 5d 20 3d 20 31 3b 0a 20 20 20 20 73 50 6b 2e  1] = 1;.    sPk.
2f828 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70  onError = OE_Rep
2f829 6c 61 63 65 3b 0a 20 20 20 20 73 50 6b 2e 70 54  lace;.    sPk.pT
2f82a 61 62 6c 65 20 3d 20 70 53 72 63 2d 3e 70 54 61  able = pSrc->pTa
2f82b 62 3b 0a 20 20 20 20 70 46 69 72 73 74 20 3d 20  b;.    pFirst = 
2f82c 70 53 72 63 2d 3e 70 54 61 62 2d 3e 70 49 6e 64  pSrc->pTab->pInd
2f82d 65 78 3b 0a 20 20 20 20 69 66 28 20 70 53 72 63  ex;.    if( pSrc
2f82e 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20  ->notIndexed==0 
2f82f 29 7b 0a 20 20 20 20 20 20 73 50 6b 2e 70 4e 65  ){.      sPk.pNe
2f830 78 74 20 3d 20 70 46 69 72 73 74 3b 0a 20 20 20  xt = pFirst;.   
2f831 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 69   }.    /* The ai
2f832 52 6f 77 45 73 74 50 6b 5b 30 5d 20 69 73 20 61  RowEstPk[0] is a
2f833 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68  n estimate of th
2f834 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
2f835 66 20 72 6f 77 73 20 69 6e 20 74 68 65 0a 20 20  f rows in the.  
2f836 20 20 2a 2a 20 74 61 62 6c 65 2e 20 20 47 65 74    ** table.  Get
2f837 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   this informatio
2f838 6e 20 66 72 6f 6d 20 74 68 65 20 41 4e 41 4c 59  n from the ANALY
2f839 5a 45 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ZE information i
2f83a 66 20 69 74 20 69 73 0a 20 20 20 20 2a 2a 20 61  f it is.    ** a
2f83b 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 6e 6f  vailable.  If no
2f83c 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 61 73 73  t available, ass
2f83d 75 6d 65 20 74 68 65 20 74 61 62 6c 65 20 31 20  ume the table 1 
2f83e 6d 69 6c 6c 69 6f 6e 20 72 6f 77 73 20 69 6e 20  million rows in 
2f83f 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  size..    */.   
2f840 20 69 66 28 20 70 46 69 72 73 74 20 29 7b 0a 20   if( pFirst ){. 
2f841 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69       assert( pFi
2f842 72 73 74 2d 3e 61 69 52 6f 77 45 73 74 21 3d 30  rst->aiRowEst!=0
2f843 20 29 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64   ); /* Allocated
2f844 20 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 70   together with p
2f845 46 69 72 73 74 20 2a 2f 0a 20 20 20 20 20 20 61  First */.      a
2f846 69 52 6f 77 45 73 74 50 6b 5b 30 5d 20 3d 20 70  iRowEstPk[0] = p
2f847 46 69 72 73 74 2d 3e 61 69 52 6f 77 45 73 74 5b  First->aiRowEst[
2f848 30 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  0];.    }else{. 
2f849 20 20 20 20 20 61 69 52 6f 77 45 73 74 50 6b 5b       aiRowEstPk[
2f84a 30 5d 20 3d 20 31 30 30 30 30 30 30 3b 0a 20 20  0] = 1000000;.  
2f84b 20 20 7d 0a 20 20 20 20 70 50 72 6f 62 65 20 3d    }.    pProbe =
2f84c 20 26 73 50 6b 3b 0a 20 20 20 20 77 73 46 6c 61   &sPk;.    wsFla
2f84d 67 4d 61 73 6b 20 3d 20 7e 28 0a 20 20 20 20 20  gMask = ~(.     
2f84e 20 20 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f     WHERE_COLUMN_
2f84f 49 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  IN|WHERE_COLUMN_
2f850 45 51 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  EQ|WHERE_COLUMN_
2f851 4e 55 4c 4c 7c 57 48 45 52 45 5f 43 4f 4c 55 4d  NULL|WHERE_COLUM
2f852 4e 5f 52 41 4e 47 45 0a 20 20 20 20 29 3b 0a 20  N_RANGE.    );. 
2f853 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20     eqTermMask = 
2f854 57 4f 5f 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20 20  WO_EQ|WO_IN;.   
2f855 20 70 49 64 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a   pIdx = 0;.  }..
2f856 20 20 2f 2a 20 4c 6f 6f 70 20 6f 76 65 72 20 61    /* Loop over a
2f857 6c 6c 20 69 6e 64 69 63 65 73 20 6c 6f 6f 6b 69  ll indices looki
2f858 6e 67 20 66 6f 72 20 74 68 65 20 62 65 73 74 20  ng for the best 
2f859 6f 6e 65 20 74 6f 20 75 73 65 0a 20 20 2a 2f 0a  one to use.  */.
2f85a 20 20 66 6f 72 28 3b 20 70 50 72 6f 62 65 3b 20    for(; pProbe; 
2f85b 70 49 64 78 3d 70 50 72 6f 62 65 3d 70 50 72 6f  pIdx=pProbe=pPro
2f85c 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  be->pNext){.    
2f85d 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 69  const unsigned i
2f85e 6e 74 20 2a 20 63 6f 6e 73 74 20 61 69 52 6f 77  nt * const aiRow
2f85f 45 73 74 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69  Est = pProbe->ai
2f860 52 6f 77 45 73 74 3b 0a 20 20 20 20 64 6f 75 62  RowEst;.    doub
2f861 6c 65 20 63 6f 73 74 3b 20 20 20 20 20 20 20 20  le cost;        
2f862 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 73 74 20          /* Cost 
2f863 6f 66 20 75 73 69 6e 67 20 70 50 72 6f 62 65 20  of using pProbe 
2f864 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e 52  */.    double nR
2f865 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ow;             
2f866 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20     /* Estimated 
2f867 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
2f868 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  n result set */.
2f869 20 20 20 20 69 6e 74 20 72 65 76 3b 20 20 20 20      int rev;    
2f86a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f86b 2f 2a 20 54 72 75 65 20 74 6f 20 73 63 61 6e 20  /* True to scan 
2f86c 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
2f86d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 77 73 46 6c   */.    int wsFl
2f86e 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 42 69 74  ags = 0;.    Bit
2f86f 6d 61 73 6b 20 75 73 65 64 20 3d 20 30 3b 0a 0a  mask used = 0;..
2f870 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
2f871 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61  wing variables a
2f872 72 65 20 70 6f 70 75 6c 61 74 65 64 20 62 61 73  re populated bas
2f873 65 64 20 6f 6e 20 74 68 65 20 70 72 6f 70 65 72  ed on the proper
2f874 74 69 65 73 20 6f 66 0a 20 20 20 20 2a 2a 20 73  ties of.    ** s
2f875 63 61 6e 20 62 65 69 6e 67 20 65 76 61 6c 75 61  can being evalua
2f876 74 65 64 2e 20 54 68 65 79 20 61 72 65 20 74 68  ted. They are th
2f877 65 6e 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  en used to deter
2f878 6d 69 6e 65 20 74 68 65 20 65 78 70 65 63 74 65  mine the expecte
2f879 64 0a 20 20 20 20 2a 2a 20 63 6f 73 74 20 61 6e  d.    ** cost an
2f87a 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  d number of rows
2f87b 20 72 65 74 75 72 6e 65 64 2e 0a 20 20 20 20 2a   returned..    *
2f87c 2a 0a 20 20 20 20 2a 2a 20 20 6e 45 71 3a 20 0a  *.    **  nEq: .
2f87d 20 20 20 20 2a 2a 20 20 20 20 4e 75 6d 62 65 72      **    Number
2f87e 20 6f 66 20 65 71 75 61 6c 69 74 79 20 74 65 72   of equality ter
2f87f 6d 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 69  ms that can be i
2f880 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67  mplemented using
2f881 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20   the index..    
2f882 2a 2a 0a 20 20 20 20 2a 2a 20 20 6e 49 6e 4d 75  **.    **  nInMu
2f883 6c 3a 20 20 0a 20 20 20 20 2a 2a 20 20 20 20 54  l:  .    **    T
2f884 68 65 20 22 69 6e 2d 6d 75 6c 74 69 70 6c 69 65  he "in-multiplie
2f885 72 22 2e 20 54 68 69 73 20 69 73 20 61 6e 20 65  r". This is an e
2f886 73 74 69 6d 61 74 65 20 6f 66 20 68 6f 77 20 6d  stimate of how m
2f887 61 6e 79 20 73 65 65 6b 20 6f 70 65 72 61 74 69  any seek operati
2f888 6f 6e 73 20 0a 20 20 20 20 2a 2a 20 20 20 20 53  ons .    **    S
2f889 51 4c 69 74 65 20 6d 75 73 74 20 70 65 72 66 6f  QLite must perfo
2f88a 72 6d 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 20  rm on the index 
2f88b 69 6e 20 71 75 65 73 74 69 6f 6e 2e 20 46 6f 72  in question. For
2f88c 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
2f88d 20 0a 20 20 20 20 2a 2a 20 20 20 20 57 48 45 52   .    **    WHER
2f88e 45 20 63 6c 61 75 73 65 20 69 73 3a 0a 20 20 20  E clause is:.   
2f88f 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
2f890 57 48 45 52 45 20 61 20 49 4e 20 28 31 2c 20 32  WHERE a IN (1, 2
2f891 2c 20 33 29 20 41 4e 44 20 62 20 49 4e 20 28 34  , 3) AND b IN (4
2f892 2c 20 35 2c 20 36 29 0a 20 20 20 20 2a 2a 0a 20  , 5, 6).    **. 
2f893 20 20 20 2a 2a 20 20 20 20 53 51 4c 69 74 65 20     **    SQLite 
2f894 6d 75 73 74 20 70 65 72 66 6f 72 6d 20 39 20 6c  must perform 9 l
2f895 6f 6f 6b 75 70 73 20 6f 6e 20 61 6e 20 69 6e 64  ookups on an ind
2f896 65 78 20 6f 6e 20 28 61 2c 20 62 29 2c 20 73 6f  ex on (a, b), so
2f897 20 6e 49 6e 4d 75 6c 20 69 73 20 0a 20 20 20 20   nInMul is .    
2f898 2a 2a 20 20 20 20 73 65 74 20 74 6f 20 39 2e 20  **    set to 9. 
2f899 47 69 76 65 6e 20 74 68 65 20 73 61 6d 65 20 73  Given the same s
2f89a 63 68 65 6d 61 20 61 6e 64 20 65 69 74 68 65 72  chema and either
2f89b 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2f89c 67 20 57 48 45 52 45 20 0a 20 20 20 20 2a 2a 20  g WHERE .    ** 
2f89d 20 20 20 63 6c 61 75 73 65 73 3a 0a 20 20 20 20     clauses:.    
2f89e 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 57  **.    **      W
2f89f 48 45 52 45 20 61 20 3d 20 20 31 0a 20 20 20 20  HERE a =  1.    
2f8a0 2a 2a 20 20 20 20 20 20 57 48 45 52 45 20 61 20  **      WHERE a 
2f8a1 3e 3d 20 32 0a 20 20 20 20 2a 2a 0a 20 20 20 20  >= 2.    **.    
2f8a2 2a 2a 20 20 20 20 6e 49 6e 4d 75 6c 20 69 73 20  **    nInMul is 
2f8a3 73 65 74 20 74 6f 20 31 2e 0a 20 20 20 20 2a 2a  set to 1..    **
2f8a4 0a 20 20 20 20 2a 2a 20 20 20 20 49 66 20 74 68  .    **    If th
2f8a5 65 72 65 20 65 78 69 73 74 73 20 61 20 57 48 45  ere exists a WHE
2f8a6 52 45 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66  RE term of the f
2f8a7 6f 72 6d 20 22 78 20 49 4e 20 28 53 45 4c 45 43  orm "x IN (SELEC
2f8a8 54 20 2e 2e 2e 29 22 2c 20 74 68 65 6e 20 0a 20  T ...)", then . 
2f8a9 20 20 20 2a 2a 20 20 20 20 74 68 65 20 73 75 62     **    the sub
2f8aa 2d 73 65 6c 65 63 74 20 69 73 20 61 73 73 75 6d  -select is assum
2f8ab 65 64 20 74 6f 20 72 65 74 75 72 6e 20 32 35 20  ed to return 25 
2f8ac 72 6f 77 73 20 66 6f 72 20 74 68 65 20 70 75 72  rows for the pur
2f8ad 70 6f 73 65 73 20 6f 66 20 0a 20 20 20 20 2a 2a  poses of .    **
2f8ae 20 20 20 20 64 65 74 65 72 6d 69 6e 69 6e 67 20      determining 
2f8af 6e 49 6e 4d 75 6c 2e 0a 20 20 20 20 2a 2a 0a 20  nInMul..    **. 
2f8b0 20 20 20 2a 2a 20 20 62 49 6e 45 73 74 3a 20 20     **  bInEst:  
2f8b1 0a 20 20 20 20 2a 2a 20 20 20 20 53 65 74 20 74  .    **    Set t
2f8b2 6f 20 74 72 75 65 20 69 66 20 74 68 65 72 65 20  o true if there 
2f8b3 77 61 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  was at least one
2f8b4 20 22 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e   "x IN (SELECT .
2f8b5 2e 2e 29 22 20 74 65 72 6d 20 75 73 65 64 20 0a  ..)" term used .
2f8b6 20 20 20 20 2a 2a 20 20 20 20 69 6e 20 64 65 74      **    in det
2f8b7 65 72 6d 69 6e 69 6e 67 20 74 68 65 20 76 61 6c  ermining the val
2f8b8 75 65 20 6f 66 20 6e 49 6e 4d 75 6c 2e 0a 20 20  ue of nInMul..  
2f8b9 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 6e 42 6f    **.    **  nBo
2f8ba 75 6e 64 3a 0a 20 20 20 20 2a 2a 20 20 20 20 41  und:.    **    A
2f8bb 6e 20 65 73 74 69 6d 61 74 65 20 6f 6e 20 74 68  n estimate on th
2f8bc 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20  e amount of the 
2f8bd 74 61 62 6c 65 20 74 68 61 74 20 6d 75 73 74 20  table that must 
2f8be 62 65 20 73 65 61 72 63 68 65 64 2e 20 20 41 0a  be searched.  A.
2f8bf 20 20 20 20 2a 2a 20 20 20 20 76 61 6c 75 65 20      **    value 
2f8c0 6f 66 20 31 30 30 20 6d 65 61 6e 73 20 74 68 65  of 100 means the
2f8c1 20 65 6e 74 69 72 65 20 74 61 62 6c 65 20 69 73   entire table is
2f8c2 20 73 65 61 72 63 68 65 64 2e 20 20 52 61 6e 67   searched.  Rang
2f8c3 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  e constraints.  
2f8c4 20 20 2a 2a 20 20 20 20 6d 69 67 68 74 20 72 65    **    might re
2f8c5 64 75 63 65 20 74 68 69 73 20 74 6f 20 61 20 76  duce this to a v
2f8c6 61 6c 75 65 20 6c 65 73 73 20 74 68 61 6e 20 31  alue less than 1
2f8c7 30 30 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  00 to indicate t
2f8c8 68 61 74 20 6f 6e 6c 79 0a 20 20 20 20 2a 2a 20  hat only.    ** 
2f8c9 20 20 20 61 20 66 72 61 63 74 69 6f 6e 20 6f 66     a fraction of
2f8ca 20 74 68 65 20 74 61 62 6c 65 20 6e 65 65 64 73   the table needs
2f8cb 20 73 65 61 72 63 68 69 6e 67 2e 20 20 49 6e 20   searching.  In 
2f8cc 74 68 65 20 61 62 73 65 6e 63 65 20 6f 66 0a 20  the absence of. 
2f8cd 20 20 20 2a 2a 20 20 20 20 73 71 6c 69 74 65 5f     **    sqlite_
2f8ce 73 74 61 74 32 20 41 4e 41 4c 59 5a 45 20 64 61  stat2 ANALYZE da
2f8cf 74 61 2c 20 61 20 73 69 6e 67 6c 65 20 69 6e 65  ta, a single ine
2f8d0 71 75 61 6c 69 74 79 20 72 65 64 75 63 65 73 20  quality reduces 
2f8d1 74 68 65 20 73 65 61 72 63 68 0a 20 20 20 20 2a  the search.    *
2f8d2 2a 20 20 20 20 73 70 61 63 65 20 74 6f 20 31 2f  *    space to 1/
2f8d3 33 72 64 20 69 74 73 20 6f 72 69 67 69 6e 61 6c  3rd its original
2f8d4 20 73 69 7a 65 2e 20 20 53 6f 20 61 6e 20 78 3e   size.  So an x>
2f8d5 3f 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 64  ? constraint red
2f8d6 75 63 65 73 0a 20 20 20 20 2a 2a 20 20 20 20 6e  uces.    **    n
2f8d7 42 6f 75 6e 64 20 74 6f 20 33 33 2e 20 20 54 77  Bound to 33.  Tw
2f8d8 6f 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 78  o constraints (x
2f8d9 3e 3f 20 41 4e 44 20 78 3c 3f 29 20 72 65 64 75  >? AND x<?) redu
2f8da 63 65 20 6e 42 6f 75 6e 64 20 74 6f 20 31 31 2e  ce nBound to 11.
2f8db 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2f8dc 62 53 6f 72 74 3a 20 20 20 0a 20 20 20 20 2a 2a  bSort:   .    **
2f8dd 20 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75      Boolean. Tru
2f8de 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  e if there is an
2f8df 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f8e0 20 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69   that will requi
2f8e1 72 65 20 61 6e 20 0a 20 20 20 20 2a 2a 20 20 20  re an .    **   
2f8e2 20 65 78 74 65 72 6e 61 6c 20 73 6f 72 74 20 28   external sort (
2f8e3 69 2e 65 2e 20 73 63 61 6e 6e 69 6e 67 20 74 68  i.e. scanning th
2f8e4 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 65 76  e index being ev
2f8e5 61 6c 75 61 74 65 64 20 77 69 6c 6c 20 6e 6f 74  aluated will not
2f8e6 20 0a 20 20 20 20 2a 2a 20 20 20 20 63 6f 72 72   .    **    corr
2f8e7 65 63 74 6c 79 20 6f 72 64 65 72 20 72 65 63 6f  ectly order reco
2f8e8 72 64 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  rds)..    **.   
2f8e9 20 2a 2a 20 20 62 4c 6f 6f 6b 75 70 3a 20 0a 20   **  bLookup: . 
2f8ea 20 20 20 2a 2a 20 20 20 20 42 6f 6f 6c 65 61 6e     **    Boolean
2f8eb 2e 20 54 72 75 65 20 69 66 20 66 6f 72 20 65 61  . True if for ea
2f8ec 63 68 20 69 6e 64 65 78 20 65 6e 74 72 79 20 76  ch index entry v
2f8ed 69 73 69 74 65 64 20 61 20 6c 6f 6f 6b 75 70 20  isited a lookup 
2f8ee 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20  on the .    **  
2f8ef 20 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20    corresponding 
2f8f0 74 61 62 6c 65 20 62 2d 74 72 65 65 20 69 73 20  table b-tree is 
2f8f1 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 69  required. This i
2f8f2 73 20 61 6c 77 61 79 73 20 66 61 6c 73 65 20 0a  s always false .
2f8f3 20 20 20 20 2a 2a 20 20 20 20 66 6f 72 20 74 68      **    for th
2f8f4 65 20 72 6f 77 69 64 20 69 6e 64 65 78 2e 20 46  e rowid index. F
2f8f5 6f 72 20 6f 74 68 65 72 20 69 6e 64 65 78 65 73  or other indexes
2f8f6 2c 20 69 74 20 69 73 20 74 72 75 65 20 75 6e 6c  , it is true unl
2f8f7 65 73 73 20 61 6c 6c 20 74 68 65 20 0a 20 20 20  ess all the .   
2f8f8 20 2a 2a 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f   **    columns o
2f8f9 66 20 74 68 65 20 74 61 62 6c 65 20 75 73 65 64  f the table used
2f8fa 20 62 79 20 74 68 65 20 53 45 4c 45 43 54 20 73   by the SELECT s
2f8fb 74 61 74 65 6d 65 6e 74 20 61 72 65 20 70 72 65  tatement are pre
2f8fc 73 65 6e 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20  sent in .    ** 
2f8fd 20 20 20 74 68 65 20 69 6e 64 65 78 20 28 73 75     the index (su
2f8fe 63 68 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73  ch an index is s
2f8ff 6f 6d 65 74 69 6d 65 73 20 64 65 73 63 72 69 62  ometimes describ
2f900 65 64 20 61 73 20 61 20 63 6f 76 65 72 69 6e 67  ed as a covering
2f901 20 69 6e 64 65 78 29 2e 0a 20 20 20 20 2a 2a 20   index)..    ** 
2f902 20 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20     For example, 
2f903 67 69 76 65 6e 20 74 68 65 20 69 6e 64 65 78 20  given the index 
2f904 6f 6e 20 28 61 2c 20 62 29 2c 20 74 68 65 20 73  on (a, b), the s
2f905 65 63 6f 6e 64 20 6f 66 20 74 68 65 20 66 6f 6c  econd of the fol
2f906 6c 6f 77 69 6e 67 20 0a 20 20 20 20 2a 2a 20 20  lowing .    **  
2f907 20 20 74 77 6f 20 71 75 65 72 69 65 73 20 72 65    two queries re
2f908 71 75 69 72 65 73 20 74 61 62 6c 65 20 62 2d 74  quires table b-t
2f909 72 65 65 20 6c 6f 6f 6b 75 70 73 2c 20 62 75 74  ree lookups, but
2f90a 20 74 68 65 20 66 69 72 73 74 20 64 6f 65 73 20   the first does 
2f90b 6e 6f 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  not..    **.    
2f90c 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 53  **             S
2f90d 45 4c 45 43 54 20 61 2c 20 62 20 20 20 20 46 52  ELECT a, b    FR
2f90e 4f 4d 20 74 62 6c 20 57 48 45 52 45 20 61 20 3d  OM tbl WHERE a =
2f90f 20 31 3b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   1;.    **      
2f910 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2c         SELECT a,
2f911 20 62 2c 20 63 20 46 52 4f 4d 20 74 62 6c 20 57   b, c FROM tbl W
2f912 48 45 52 45 20 61 20 3d 20 31 3b 0a 20 20 20 20  HERE a = 1;.    
2f913 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 71 3b 0a  */.    int nEq;.
2f914 20 20 20 20 69 6e 74 20 62 49 6e 45 73 74 20 3d      int bInEst =
2f915 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 49 6e 4d   0;.    int nInM
2f916 75 6c 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20  ul = 1;.    int 
2f917 6e 42 6f 75 6e 64 20 3d 20 31 30 30 3b 0a 20 20  nBound = 100;.  
2f918 20 20 69 6e 74 20 62 53 6f 72 74 20 3d 20 30 3b    int bSort = 0;
2f919 0a 20 20 20 20 69 6e 74 20 62 4c 6f 6f 6b 75 70  .    int bLookup
2f91a 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 44 65   = 0;..    /* De
2f91b 74 65 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75  termine the valu
2f91c 65 73 20 6f 66 20 6e 45 71 20 61 6e 64 20 6e 49  es of nEq and nI
2f91d 6e 4d 75 6c 20 2a 2f 0a 20 20 20 20 66 6f 72 28  nMul */.    for(
2f91e 6e 45 71 3d 30 3b 20 6e 45 71 3c 70 50 72 6f 62  nEq=0; nEq<pProb
2f91f 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 45 71 2b  e->nColumn; nEq+
2f920 2b 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54  +){.      WhereT
2f921 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20  erm *pTerm;     
2f922 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c        /* A singl
2f923 65 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48  e term of the WH
2f924 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2f925 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f      int j = pPro
2f926 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71  be->aiColumn[nEq
2f927 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d  ];.      pTerm =
2f928 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2f929 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79  Cur, j, notReady
2f92a 2c 20 65 71 54 65 72 6d 4d 61 73 6b 2c 20 70 49  , eqTermMask, pI
2f92b 64 78 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  dx);.      if( p
2f92c 54 65 72 6d 3d 3d 30 20 29 20 62 72 65 61 6b 3b  Term==0 ) break;
2f92d 0a 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c  .      wsFlags |
2f92e 3d 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  = (WHERE_COLUMN_
2f92f 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 45  EQ|WHERE_ROWID_E
2f930 51 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  Q);.      if( pT
2f931 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
2f932 20 57 4f 5f 49 4e 20 29 7b 0a 20 20 20 20 20 20   WO_IN ){.      
2f933 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20    Expr *pExpr = 
2f934 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
2f935 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2f936 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e   WHERE_COLUMN_IN
2f937 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78  ;.        if( Ex
2f938 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2f939 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2f93a 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  t) ){.          
2f93b 6e 49 6e 4d 75 6c 20 2a 3d 20 32 35 3b 0a 20 20  nInMul *= 25;.  
2f93c 20 20 20 20 20 20 20 20 62 49 6e 45 73 74 20 3d          bInEst =
2f93d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   1;.        }els
2f93e 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70  e if( pExpr->x.p
2f93f 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  List ){.        
2f940 20 20 6e 49 6e 4d 75 6c 20 2a 3d 20 70 45 78 70    nInMul *= pExp
2f941 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70  r->x.pList->nExp
2f942 72 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  r + 1;.        }
2f943 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
2f944 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2f945 72 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b  r & WO_ISNULL ){
2f946 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73  .        wsFlags
2f947 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e   |= WHERE_COLUMN
2f948 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20  _NULL;.      }. 
2f949 20 20 20 20 20 75 73 65 64 20 7c 3d 20 70 54 65       used |= pTe
2f94a 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 3b  rm->prereqRight;
2f94b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
2f94c 65 74 65 72 6d 69 6e 65 20 74 68 65 20 76 61 6c  etermine the val
2f94d 75 65 20 6f 66 20 6e 42 6f 75 6e 64 2e 20 2a 2f  ue of nBound. */
2f94e 0a 20 20 20 20 69 66 28 20 6e 45 71 3c 70 50 72  .    if( nEq<pPr
2f94f 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  obe->nColumn ){.
2f950 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 50        int j = pP
2f951 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e  robe->aiColumn[n
2f952 45 71 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 66  Eq];.      if( f
2f953 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2f954 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, j, notReady, 
2f955 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47  WO_LT|WO_LE|WO_G
2f956 54 7c 57 4f 5f 47 45 2c 20 70 49 64 78 29 20 29  T|WO_GE, pIdx) )
2f957 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 54  {.        WhereT
2f958 65 72 6d 20 2a 70 54 6f 70 20 3d 20 66 69 6e 64  erm *pTop = find
2f959 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
2f95a 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f  j, notReady, WO_
2f95b 4c 54 7c 57 4f 5f 4c 45 2c 20 70 49 64 78 29 3b  LT|WO_LE, pIdx);
2f95c 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65  .        WhereTe
2f95d 72 6d 20 2a 70 42 74 6d 20 3d 20 66 69 6e 64 54  rm *pBtm = findT
2f95e 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a  erm(pWC, iCur, j
2f95f 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47  , notReady, WO_G
2f960 54 7c 57 4f 5f 47 45 2c 20 70 49 64 78 29 3b 0a  T|WO_GE, pIdx);.
2f961 20 20 20 20 20 20 20 20 77 68 65 72 65 52 61 6e          whereRan
2f962 67 65 53 63 61 6e 45 73 74 28 70 50 61 72 73 65  geScanEst(pParse
2f963 2c 20 70 50 72 6f 62 65 2c 20 6e 45 71 2c 20 70  , pProbe, nEq, p
2f964 42 74 6d 2c 20 70 54 6f 70 2c 20 26 6e 42 6f 75  Btm, pTop, &nBou
2f965 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  nd);.        if(
2f966 20 70 54 6f 70 20 29 7b 0a 20 20 20 20 20 20 20   pTop ){.       
2f967 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48     wsFlags |= WH
2f968 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20  ERE_TOP_LIMIT;. 
2f969 20 20 20 20 20 20 20 20 20 75 73 65 64 20 7c 3d           used |=
2f96a 20 70 54 6f 70 2d 3e 70 72 65 72 65 71 52 69 67   pTop->prereqRig
2f96b 68 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ht;.        }.  
2f96c 20 20 20 20 20 20 69 66 28 20 70 42 74 6d 20 29        if( pBtm )
2f96d 7b 0a 20 20 20 20 20 20 20 20 20 20 77 73 46 6c  {.          wsFl
2f96e 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 42 54 4d  ags |= WHERE_BTM
2f96f 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 20 20  _LIMIT;.        
2f970 20 20 75 73 65 64 20 7c 3d 20 70 42 74 6d 2d 3e    used |= pBtm->
2f971 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20  prereqRight;.   
2f972 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77       }.        w
2f973 73 46 6c 61 67 73 20 7c 3d 20 28 57 48 45 52 45  sFlags |= (WHERE
2f974 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57 48  _COLUMN_RANGE|WH
2f975 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29  ERE_ROWID_RANGE)
2f976 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2f977 6c 73 65 20 69 66 28 20 70 50 72 6f 62 65 2d 3e  lse if( pProbe->
2f978 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
2f979 20 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61   ){.      testca
2f97a 73 65 28 20 77 73 46 6c 61 67 73 20 26 20 57 48  se( wsFlags & WH
2f97b 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 29 3b  ERE_COLUMN_IN );
2f97c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f97d 20 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45   wsFlags & WHERE
2f97e 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 29 3b 0a  _COLUMN_NULL );.
2f97f 20 20 20 20 20 20 69 66 28 20 28 77 73 46 6c 61        if( (wsFla
2f980 67 73 20 26 20 28 57 48 45 52 45 5f 43 4f 4c 55  gs & (WHERE_COLU
2f981 4d 4e 5f 49 4e 7c 57 48 45 52 45 5f 43 4f 4c 55  MN_IN|WHERE_COLU
2f982 4d 4e 5f 4e 55 4c 4c 29 29 3d 3d 30 20 29 7b 0a  MN_NULL))==0 ){.
2f983 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2f984 7c 3d 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b  |= WHERE_UNIQUE;
2f985 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2f986 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
2f987 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
2f988 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20 69 6e  lause and the in
2f989 64 65 78 20 62 65 69 6e 67 20 63 6f 6e 73 69 64  dex being consid
2f98a 65 72 65 64 20 77 69 6c 6c 0a 20 20 20 20 2a 2a  ered will.    **
2f98b 20 6e 61 74 75 72 61 6c 6c 79 20 73 63 61 6e 20   naturally scan 
2f98c 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65 71 75  rows in the requ
2f98d 69 72 65 64 20 6f 72 64 65 72 2c 20 73 65 74 20  ired order, set 
2f98e 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
2f98f 66 6c 61 67 73 0a 20 20 20 20 2a 2a 20 69 6e 20  flags.    ** in 
2f990 77 73 46 6c 61 67 73 2e 20 4f 74 68 65 72 77 69  wsFlags. Otherwi
2f991 73 65 2c 20 69 66 20 74 68 65 72 65 20 69 73 20  se, if there is 
2f992 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
2f993 73 65 20 62 75 74 20 74 68 65 20 69 6e 64 65 78  se but the index
2f994 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61  .    ** will sca
2f995 6e 20 72 6f 77 73 20 69 6e 20 61 20 64 69 66 66  n rows in a diff
2f996 65 72 65 6e 74 20 6f 72 64 65 72 2c 20 73 65 74  erent order, set
2f997 20 74 68 65 20 62 53 6f 72 74 20 76 61 72 69 61   the bSort varia
2f998 62 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  ble.  */.    if(
2f999 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20   pOrderBy ){.   
2f99a 20 20 20 69 66 28 20 28 77 73 46 6c 61 67 73 20     if( (wsFlags 
2f99b 26 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  & (WHERE_COLUMN_
2f99c 49 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  IN|WHERE_COLUMN_
2f99d 4e 55 4c 4c 29 29 3d 3d 30 0a 20 20 20 20 20 20  NULL))==0.      
2f99e 20 20 26 26 20 69 73 53 6f 72 74 69 6e 67 49 6e    && isSortingIn
2f99f 64 65 78 28 70 50 61 72 73 65 2c 70 57 43 2d 3e  dex(pParse,pWC->
2f9a0 70 4d 61 73 6b 53 65 74 2c 70 50 72 6f 62 65 2c  pMaskSet,pProbe,
2f9a1 69 43 75 72 2c 70 4f 72 64 65 72 42 79 2c 6e 45  iCur,pOrderBy,nE
2f9a2 71 2c 26 72 65 76 29 0a 20 20 20 20 20 20 29 7b  q,&rev).      ){
2f9a3 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73  .        wsFlags
2f9a4 20 7c 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f   |= WHERE_ROWID_
2f9a5 52 41 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55  RANGE|WHERE_COLU
2f9a6 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f 4f  MN_RANGE|WHERE_O
2f9a7 52 44 45 52 42 59 3b 0a 20 20 20 20 20 20 20 20  RDERBY;.        
2f9a8 77 73 46 6c 61 67 73 20 7c 3d 20 28 72 65 76 20  wsFlags |= (rev 
2f9a9 3f 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 20  ? WHERE_REVERSE 
2f9aa 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  : 0);.      }els
2f9ab 65 7b 0a 20 20 20 20 20 20 20 20 62 53 6f 72 74  e{.        bSort
2f9ac 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
2f9ad 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63    }..    /* If c
2f9ae 75 72 72 65 6e 74 6c 79 20 63 61 6c 63 75 6c 61  urrently calcula
2f9af 74 69 6e 67 20 74 68 65 20 63 6f 73 74 20 6f 66  ting the cost of
2f9b0 20 75 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 20   using an index 
2f9b1 28 6e 6f 74 20 74 68 65 20 49 50 4b 0a 20 20 20  (not the IPK.   
2f9b2 20 2a 2a 20 69 6e 64 65 78 29 2c 20 64 65 74 65   ** index), dete
2f9b3 72 6d 69 6e 65 20 69 66 20 61 6c 6c 20 72 65 71  rmine if all req
2f9b4 75 69 72 65 64 20 63 6f 6c 75 6d 6e 20 64 61 74  uired column dat
2f9b5 61 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65  a may be obtaine
2f9b6 64 20 77 69 74 68 6f 75 74 20 0a 20 20 20 20 2a  d without .    *
2f9b7 2a 20 73 65 65 6b 69 6e 67 20 74 6f 20 65 6e 74  * seeking to ent
2f9b8 72 69 65 73 20 69 6e 20 74 68 65 20 6d 61 69 6e  ries in the main
2f9b9 20 74 61 62 6c 65 20 28 69 2e 65 2e 20 69 66 20   table (i.e. if 
2f9ba 74 68 65 20 69 6e 64 65 78 20 69 73 20 61 20 63  the index is a c
2f9bb 6f 76 65 72 69 6e 67 0a 20 20 20 20 2a 2a 20 69  overing.    ** i
2f9bc 6e 64 65 78 20 66 6f 72 20 74 68 69 73 20 71 75  ndex for this qu
2f9bd 65 72 79 29 2e 20 49 66 20 69 74 20 69 73 2c 20  ery). If it is, 
2f9be 73 65 74 20 74 68 65 20 57 48 45 52 45 5f 49 44  set the WHERE_ID
2f9bf 58 5f 4f 4e 4c 59 20 66 6c 61 67 20 69 6e 0a 20  X_ONLY flag in. 
2f9c0 20 20 20 2a 2a 20 77 73 46 6c 61 67 73 2e 20 4f     ** wsFlags. O
2f9c1 74 68 65 72 77 69 73 65 2c 20 73 65 74 20 74 68  therwise, set th
2f9c2 65 20 62 4c 6f 6f 6b 75 70 20 76 61 72 69 61 62  e bLookup variab
2f9c3 6c 65 20 74 6f 20 74 72 75 65 2e 20 20 2a 2f 0a  le to true.  */.
2f9c4 20 20 20 20 69 66 28 20 70 49 64 78 20 26 26 20      if( pIdx && 
2f9c5 77 73 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 20  wsFlags ){.     
2f9c6 20 42 69 74 6d 61 73 6b 20 6d 20 3d 20 70 53 72   Bitmask m = pSr
2f9c7 63 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20  c->colUsed;.    
2f9c8 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66    int j;.      f
2f9c9 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e  or(j=0; j<pIdx->
2f9ca 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20  nColumn; j++){. 
2f9cb 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 70         int x = p
2f9cc 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
2f9cd 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 3c  ;.        if( x<
2f9ce 42 4d 53 2d 31 20 29 7b 0a 20 20 20 20 20 20 20  BMS-1 ){.       
2f9cf 20 20 20 6d 20 26 3d 20 7e 28 28 28 42 69 74 6d     m &= ~(((Bitm
2f9d0 61 73 6b 29 31 29 3c 3c 78 29 3b 0a 20 20 20 20  ask)1)<<x);.    
2f9d1 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2f9d2 20 20 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0a      if( m==0 ){.
2f9d3 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2f9d4 7c 3d 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c  |= WHERE_IDX_ONL
2f9d5 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  Y;.      }else{.
2f9d6 20 20 20 20 20 20 20 20 62 4c 6f 6f 6b 75 70 20          bLookup 
2f9d7 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
2f9d8 20 7d 0a 0a 20 20 20 20 2f 2a 2a 2a 2a 20 42 65   }..    /**** Be
2f9d9 67 69 6e 20 61 64 64 69 6e 67 20 75 70 20 74 68  gin adding up th
2f9da 65 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20  e cost of using 
2f9db 74 68 69 73 20 69 6e 64 65 78 20 28 4e 65 65 64  this index (Need
2f9dc 73 20 69 6d 70 72 6f 76 65 6d 65 6e 74 73 29 0a  s improvements).
2f9dd 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 73      **.    ** Es
2f9de 74 69 6d 61 74 65 20 74 68 65 20 6e 75 6d 62 65  timate the numbe
2f9df 72 20 6f 66 20 72 6f 77 73 20 6f 66 20 6f 75 74  r of rows of out
2f9e0 70 75 74 2e 20 20 46 6f 72 20 61 6e 20 49 4e 20  put.  For an IN 
2f9e1 6f 70 65 72 61 74 6f 72 2c 0a 20 20 20 20 2a 2a  operator,.    **
2f9e2 20 64 6f 20 6e 6f 74 20 6c 65 74 20 74 68 65 20   do not let the 
2f9e3 65 73 74 69 6d 61 74 65 20 65 78 63 65 65 64 20  estimate exceed 
2f9e4 68 61 6c 66 20 74 68 65 20 72 6f 77 73 20 69 6e  half the rows in
2f9e5 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20   the table..    
2f9e6 2a 2f 0a 20 20 20 20 6e 52 6f 77 20 3d 20 28 64  */.    nRow = (d
2f9e7 6f 75 62 6c 65 29 28 61 69 52 6f 77 45 73 74 5b  ouble)(aiRowEst[
2f9e8 6e 45 71 5d 20 2a 20 6e 49 6e 4d 75 6c 29 3b 0a  nEq] * nInMul);.
2f9e9 20 20 20 20 69 66 28 20 62 49 6e 45 73 74 20 26      if( bInEst &
2f9ea 26 20 6e 52 6f 77 2a 32 3e 61 69 52 6f 77 45 73  & nRow*2>aiRowEs
2f9eb 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 6e 52  t[0] ){.      nR
2f9ec 6f 77 20 3d 20 61 69 52 6f 77 45 73 74 5b 30 5d  ow = aiRowEst[0]
2f9ed 2f 32 3b 0a 20 20 20 20 20 20 6e 49 6e 4d 75 6c  /2;.      nInMul
2f9ee 20 3d 20 28 69 6e 74 29 28 6e 52 6f 77 20 2f 20   = (int)(nRow / 
2f9ef 61 69 52 6f 77 45 73 74 5b 6e 45 71 5d 29 3b 0a  aiRowEst[nEq]);.
2f9f0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 73      }..    /* As
2f9f1 73 75 6d 65 20 63 6f 6e 73 74 61 6e 74 20 63 6f  sume constant co
2f9f2 73 74 20 74 6f 20 61 63 63 65 73 73 20 61 20 72  st to access a r
2f9f3 6f 77 20 61 6e 64 20 6c 6f 67 61 72 69 74 68 6d  ow and logarithm
2f9f4 69 63 20 63 6f 73 74 20 74 6f 0a 20 20 20 20 2a  ic cost to.    *
2f9f5 2a 20 64 6f 20 61 20 62 69 6e 61 72 79 20 73 65  * do a binary se
2f9f6 61 72 63 68 2e 20 20 48 65 6e 63 65 2c 20 74 68  arch.  Hence, th
2f9f7 65 20 69 6e 69 74 69 61 6c 20 63 6f 73 74 20 69  e initial cost i
2f9f8 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
2f9f9 6f 75 74 70 75 74 0a 20 20 20 20 2a 2a 20 72 6f  output.    ** ro
2f9fa 77 73 20 70 6c 75 73 20 6c 6f 67 32 28 74 61 62  ws plus log2(tab
2f9fb 6c 65 2d 73 69 7a 65 29 20 74 69 6d 65 73 20 74  le-size) times t
2f9fc 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e  he number of bin
2f9fd 61 72 79 20 73 65 61 72 63 68 65 73 2e 0a 20 20  ary searches..  
2f9fe 20 20 2a 2f 0a 20 20 20 20 63 6f 73 74 20 3d 20    */.    cost = 
2f9ff 6e 52 6f 77 20 2b 20 6e 49 6e 4d 75 6c 2a 65 73  nRow + nInMul*es
2fa00 74 4c 6f 67 28 61 69 52 6f 77 45 73 74 5b 30 5d  tLog(aiRowEst[0]
2fa01 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 6a 75 73  );..    /* Adjus
2fa02 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
2fa03 72 6f 77 73 20 61 6e 64 20 74 68 65 20 63 6f 73  rows and the cos
2fa04 74 20 64 6f 77 6e 77 61 72 64 20 74 6f 20 72 65  t downward to re
2fa05 66 6c 65 63 74 20 72 6f 77 73 0a 20 20 20 20 2a  flect rows.    *
2fa06 2a 20 74 68 61 74 20 61 72 65 20 65 78 63 6c 75  * that are exclu
2fa07 64 65 64 20 62 79 20 72 61 6e 67 65 20 63 6f 6e  ded by range con
2fa08 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2f  straints..    */
2fa09 0a 20 20 20 20 6e 52 6f 77 20 3d 20 28 6e 52 6f  .    nRow = (nRo
2fa0a 77 20 2a 20 28 64 6f 75 62 6c 65 29 6e 42 6f 75  w * (double)nBou
2fa0b 6e 64 29 20 2f 20 28 64 6f 75 62 6c 65 29 31 30  nd) / (double)10
2fa0c 30 3b 0a 20 20 20 20 63 6f 73 74 20 3d 20 28 63  0;.    cost = (c
2fa0d 6f 73 74 20 2a 20 28 64 6f 75 62 6c 65 29 6e 42  ost * (double)nB
2fa0e 6f 75 6e 64 29 20 2f 20 28 64 6f 75 62 6c 65 29  ound) / (double)
2fa0f 31 30 30 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 64  100;..    /* Add
2fa10 20 69 6e 20 74 68 65 20 65 73 74 69 6d 61 74 65   in the estimate
2fa11 64 20 63 6f 73 74 20 6f 66 20 73 6f 72 74 69 6e  d cost of sortin
2fa12 67 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20  g the result.   
2fa13 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 53 6f 72   */.    if( bSor
2fa14 74 20 29 7b 0a 20 20 20 20 20 20 63 6f 73 74 20  t ){.      cost 
2fa15 2b 3d 20 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63  += cost*estLog(c
2fa16 6f 73 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ost);.    }..   
2fa17 20 2f 2a 20 49 66 20 61 6c 6c 20 69 6e 66 6f 72   /* If all infor
2fa18 6d 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 74 61  mation can be ta
2fa19 6b 65 6e 20 64 69 72 65 63 74 6c 79 20 66 72 6f  ken directly fro
2fa1a 6d 20 74 68 65 20 69 6e 64 65 78 2c 20 77 65 20  m the index, we 
2fa1b 61 76 6f 69 64 0a 20 20 20 20 2a 2a 20 64 6f 69  avoid.    ** doi
2fa1c 6e 67 20 74 61 62 6c 65 20 6c 6f 6f 6b 75 70 73  ng table lookups
2fa1d 2e 20 20 54 68 69 73 20 72 65 64 75 63 65 73 20  .  This reduces 
2fa1e 74 68 65 20 63 6f 73 74 20 62 79 20 68 61 6c 66  the cost by half
2fa1f 2e 20 20 28 4e 6f 74 20 72 65 61 6c 6c 79 20 2d  .  (Not really -
2fa20 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 6e 65 65  .    ** this nee
2fa21 64 73 20 74 6f 20 62 65 20 66 69 78 65 64 2e 29  ds to be fixed.)
2fa22 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2fa23 70 49 64 78 20 26 26 20 62 4c 6f 6f 6b 75 70 3d  pIdx && bLookup=
2fa24 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 73 74  =0 ){.      cost
2fa25 20 2f 3d 20 28 64 6f 75 62 6c 65 29 32 3b 0a 20   /= (double)2;. 
2fa26 20 20 20 7d 0a 20 20 20 20 2f 2a 2a 2a 2a 20 43     }.    /**** C
2fa27 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69  ost of using thi
2fa28 73 20 69 6e 64 65 78 20 68 61 73 20 6e 6f 77 20  s index has now 
2fa29 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 2a 2a  been computed **
2fa2a 2a 2a 2f 0a 0a 20 20 20 20 57 48 45 52 45 54 52  **/..    WHERETR
2fa2b 41 43 45 28 28 0a 20 20 20 20 20 20 22 74 62 6c  ACE((.      "tbl
2fa2c 3d 25 73 20 69 64 78 3d 25 73 20 6e 45 71 3d 25  =%s idx=%s nEq=%
2fa2d 64 20 6e 49 6e 4d 75 6c 3d 25 64 20 6e 42 6f 75  d nInMul=%d nBou
2fa2e 6e 64 3d 25 64 20 62 53 6f 72 74 3d 25 64 20 62  nd=%d bSort=%d b
2fa2f 4c 6f 6f 6b 75 70 3d 25 64 22 0a 20 20 20 20 20  Lookup=%d".     
2fa30 20 22 20 77 73 46 6c 61 67 73 3d 25 64 20 20 20   " wsFlags=%d   
2fa31 28 6e 52 6f 77 3d 25 2e 32 66 20 63 6f 73 74 3d  (nRow=%.2f cost=
2fa32 25 2e 32 66 29 5c 6e 22 2c 0a 20 20 20 20 20 20  %.2f)\n",.      
2fa33 70 53 72 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  pSrc->pTab->zNam
2fa34 65 2c 20 28 70 49 64 78 20 3f 20 70 49 64 78 2d  e, (pIdx ? pIdx-
2fa35 3e 7a 4e 61 6d 65 20 3a 20 22 69 70 6b 22 29 2c  >zName : "ipk"),
2fa36 20 0a 20 20 20 20 20 20 6e 45 71 2c 20 6e 49 6e   .      nEq, nIn
2fa37 4d 75 6c 2c 20 6e 42 6f 75 6e 64 2c 20 62 53 6f  Mul, nBound, bSo
2fa38 72 74 2c 20 62 4c 6f 6f 6b 75 70 2c 20 77 73 46  rt, bLookup, wsF
2fa39 6c 61 67 73 2c 20 6e 52 6f 77 2c 20 63 6f 73 74  lags, nRow, cost
2fa3a 0a 20 20 20 20 29 29 3b 0a 0a 20 20 20 20 2f 2a  .    ));..    /*
2fa3b 20 49 66 20 74 68 69 73 20 69 6e 64 65 78 20 69   If this index i
2fa3c 73 20 74 68 65 20 62 65 73 74 20 77 65 20 68 61  s the best we ha
2fa3d 76 65 20 73 65 65 6e 20 73 6f 20 66 61 72 2c 20  ve seen so far, 
2fa3e 74 68 65 6e 20 72 65 63 6f 72 64 20 74 68 69 73  then record this
2fa3f 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e  .    ** index an
2fa40 64 20 69 74 73 20 63 6f 73 74 20 69 6e 20 74 68  d its cost in th
2fa41 65 20 70 43 6f 73 74 20 73 74 72 75 63 74 75 72  e pCost structur
2fa42 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
2fa43 28 20 28 21 70 49 64 78 20 7c 7c 20 77 73 46 6c  ( (!pIdx || wsFl
2fa44 61 67 73 29 20 26 26 20 63 6f 73 74 3c 70 43 6f  ags) && cost<pCo
2fa45 73 74 2d 3e 72 43 6f 73 74 20 29 7b 0a 20 20 20  st->rCost ){.   
2fa46 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20     pCost->rCost 
2fa47 3d 20 63 6f 73 74 3b 0a 20 20 20 20 20 20 70 43  = cost;.      pC
2fa48 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77  ost->nRow = nRow
2fa49 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75  ;.      pCost->u
2fa4a 73 65 64 20 3d 20 75 73 65 64 3b 0a 20 20 20 20  sed = used;.    
2fa4b 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
2fa4c 46 6c 61 67 73 20 3d 20 28 77 73 46 6c 61 67 73  Flags = (wsFlags
2fa4d 26 77 73 46 6c 61 67 4d 61 73 6b 29 3b 0a 20 20  &wsFlagMask);.  
2fa4e 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e      pCost->plan.
2fa4f 6e 45 71 20 3d 20 6e 45 71 3b 0a 20 20 20 20 20  nEq = nEq;.     
2fa50 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70   pCost->plan.u.p
2fa51 49 64 78 20 3d 20 70 49 64 78 3b 0a 20 20 20 20  Idx = pIdx;.    
2fa52 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
2fa53 72 65 20 77 61 73 20 61 6e 20 49 4e 44 45 58 45  re was an INDEXE
2fa54 44 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65  D BY clause, the
2fa55 6e 20 6f 6e 6c 79 20 74 68 61 74 20 6f 6e 65 20  n only that one 
2fa56 69 6e 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20  index is.    ** 
2fa57 63 6f 6e 73 69 64 65 72 65 64 2e 20 2a 2f 0a 20  considered. */. 
2fa58 20 20 20 69 66 28 20 70 53 72 63 2d 3e 70 49 6e     if( pSrc->pIn
2fa59 64 65 78 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20  dex ) break;..  
2fa5a 20 20 2f 2a 20 52 65 73 65 74 20 6d 61 73 6b 73    /* Reset masks
2fa5b 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 69 6e   for the next in
2fa5c 64 65 78 20 69 6e 20 74 68 65 20 6c 6f 6f 70 20  dex in the loop 
2fa5d 2a 2f 0a 20 20 20 20 77 73 46 6c 61 67 4d 61 73  */.    wsFlagMas
2fa5e 6b 20 3d 20 7e 28 57 48 45 52 45 5f 52 4f 57 49  k = ~(WHERE_ROWI
2fa5f 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44  D_EQ|WHERE_ROWID
2fa60 5f 52 41 4e 47 45 29 3b 0a 20 20 20 20 65 71 54  _RANGE);.    eqT
2fa61 65 72 6d 4d 61 73 6b 20 3d 20 69 64 78 45 71 54  ermMask = idxEqT
2fa62 65 72 6d 4d 61 73 6b 3b 0a 20 20 7d 0a 0a 20 20  ermMask;.  }..  
2fa63 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
2fa64 6f 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  o ORDER BY claus
2fa65 65 20 61 6e 64 20 74 68 65 20 53 51 4c 49 54 45  e and the SQLITE
2fa66 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20 66 6c  _ReverseOrder fl
2fa67 61 67 0a 20 20 2a 2a 20 69 73 20 73 65 74 2c 20  ag.  ** is set, 
2fa68 74 68 65 6e 20 72 65 76 65 72 73 65 20 74 68 65  then reverse the
2fa69 20 6f 72 64 65 72 20 74 68 61 74 20 74 68 65 20   order that the 
2fa6a 69 6e 64 65 78 20 77 69 6c 6c 20 62 65 20 73 63  index will be sc
2fa6b 61 6e 6e 65 64 0a 20 20 2a 2a 20 69 6e 2e 20 54  anned.  ** in. T
2fa6c 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
2fa6d 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 65 73 74  application test
2fa6e 69 6e 67 2c 20 74 6f 20 68 65 6c 70 20 66 69 6e  ing, to help fin
2fa6f 64 20 63 61 73 65 73 0a 20 20 2a 2a 20 77 68 65  d cases.  ** whe
2fa70 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 62  re application b
2fa71 65 68 61 76 69 6f 75 72 20 64 65 70 65 6e 64 73  ehaviour depends
2fa72 20 6f 6e 20 74 68 65 20 28 75 6e 64 65 66 69 6e   on the (undefin
2fa73 65 64 29 20 6f 72 64 65 72 20 74 68 61 74 0a 20  ed) order that. 
2fa74 20 2a 2a 20 53 51 4c 69 74 65 20 6f 75 74 70 75   ** SQLite outpu
2fa75 74 73 20 72 6f 77 73 20 69 6e 20 69 6e 20 74 68  ts rows in in th
2fa76 65 20 61 62 73 65 6e 63 65 20 6f 66 20 61 6e 20  e absence of an 
2fa77 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e  ORDER BY clause.
2fa78 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4f 72 64    */.  if( !pOrd
2fa79 65 72 42 79 20 26 26 20 70 50 61 72 73 65 2d 3e  erBy && pParse->
2fa7a 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
2fa7b 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20  TE_ReverseOrder 
2fa7c 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c  ){.    pCost->pl
2fa7d 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48  an.wsFlags |= WH
2fa7e 45 52 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 7d  ERE_REVERSE;.  }
2fa7f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 64  ..  assert( pOrd
2fa80 65 72 42 79 20 7c 7c 20 28 70 43 6f 73 74 2d 3e  erBy || (pCost->
2fa81 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45  plan.wsFlags&WHE
2fa82 52 45 5f 4f 52 44 45 52 42 59 29 3d 3d 30 20 29  RE_ORDERBY)==0 )
2fa83 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 73  ;.  assert( pCos
2fa84 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d  t->plan.u.pIdx==
2fa85 30 20 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c 61  0 || (pCost->pla
2fa86 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f  n.wsFlags&WHERE_
2fa87 52 4f 57 49 44 5f 45 51 29 3d 3d 30 20 29 3b 0a  ROWID_EQ)==0 );.
2fa88 20 20 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e    assert( pSrc->
2fa89 70 49 6e 64 65 78 3d 3d 30 20 0a 20 20 20 20 20  pIndex==0 .     
2fa8a 20 20 7c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e    || pCost->plan
2fa8b 2e 75 2e 70 49 64 78 3d 3d 30 20 0a 20 20 20 20  .u.pIdx==0 .    
2fa8c 20 20 20 7c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61     || pCost->pla
2fa8d 6e 2e 75 2e 70 49 64 78 3d 3d 70 53 72 63 2d 3e  n.u.pIdx==pSrc->
2fa8e 70 49 6e 64 65 78 20 0a 20 20 29 3b 0a 0a 20 20  pIndex .  );..  
2fa8f 57 48 45 52 45 54 52 41 43 45 28 28 22 62 65 73  WHERETRACE(("bes
2fa90 74 20 69 6e 64 65 78 20 69 73 3a 20 25 73 5c 6e  t index is: %s\n
2fa91 22 2c 20 0a 20 20 20 20 28 70 43 6f 73 74 2d 3e  ", .    (pCost->
2fa92 70 6c 61 6e 2e 75 2e 70 49 64 78 20 3f 20 70 43  plan.u.pIdx ? pC
2fa93 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78  ost->plan.u.pIdx
2fa94 2d 3e 7a 4e 61 6d 65 20 3a 20 22 69 70 6b 22 29  ->zName : "ipk")
2fa95 0a 20 20 29 29 3b 0a 20 20 0a 20 20 62 65 73 74  .  ));.  .  best
2fa96 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28 70 50  OrClauseIndex(pP
2fa97 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c  arse, pWC, pSrc,
2fa98 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65   notReady, pOrde
2fa99 72 42 79 2c 20 70 43 6f 73 74 29 3b 0a 20 20 70  rBy, pCost);.  p
2fa9a 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  Cost->plan.wsFla
2fa9b 67 73 20 7c 3d 20 65 71 54 65 72 6d 4d 61 73 6b  gs |= eqTermMask
2fa9c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  ;.}../*.** Find 
2fa9d 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66  the query plan f
2fa9e 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 61 62  or accessing tab
2fa9f 6c 65 20 70 53 72 63 2d 3e 70 54 61 62 2e 20 57  le pSrc->pTab. W
2faa0 72 69 74 65 20 74 68 65 0a 2a 2a 20 62 65 73 74  rite the.** best
2faa1 20 71 75 65 72 79 20 70 6c 61 6e 20 61 6e 64 20   query plan and 
2faa2 69 74 73 20 63 6f 73 74 20 69 6e 74 6f 20 74 68  its cost into th
2faa3 65 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65  e WhereCost obje
2faa4 63 74 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20  ct supplied .** 
2faa5 61 73 20 74 68 65 20 6c 61 73 74 20 70 61 72 61  as the last para
2faa6 6d 65 74 65 72 2e 20 54 68 69 73 20 66 75 6e 63  meter. This func
2faa7 74 69 6f 6e 20 6d 61 79 20 63 61 6c 63 75 6c 61  tion may calcula
2faa8 74 65 20 74 68 65 20 63 6f 73 74 20 6f 66 0a 2a  te the cost of.*
2faa9 2a 20 62 6f 74 68 20 72 65 61 6c 20 61 6e 64 20  * both real and 
2faaa 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 63  virtual table sc
2faab 61 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ans..*/.static v
2faac 6f 69 64 20 62 65 73 74 49 6e 64 65 78 28 0a 20  oid bestIndex(. 
2faad 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2faae 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2faaf 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
2fab0 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ext */.  WhereCl
2fab1 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20  ause *pWC,      
2fab2 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2fab3 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74  E clause */.  st
2fab4 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2fab5 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65  m *pSrc,  /* The
2fab6 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
2fab7 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20  m to search */. 
2fab8 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
2fab9 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
2faba 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
2fabb 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61  that are not ava
2fabc 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72  ilable */.  Expr
2fabd 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2fabe 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
2fabf 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
2fac0 2f 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70  /.  WhereCost *p
2fac1 43 6f 73 74 20 20 20 20 20 20 20 20 20 20 20 20  Cost            
2fac2 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71  /* Lowest cost q
2fac3 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a  uery plan */.){.
2fac4 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2fac5 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2fac6 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
2fac7 28 70 53 72 63 2d 3e 70 54 61 62 29 20 29 7b 0a  (pSrc->pTab) ){.
2fac8 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65      sqlite3_inde
2fac9 78 5f 69 6e 66 6f 20 2a 70 20 3d 20 30 3b 0a 20  x_info *p = 0;. 
2faca 20 20 20 62 65 73 74 56 69 72 74 75 61 6c 49 6e     bestVirtualIn
2facb 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c  dex(pParse, pWC,
2facc 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c   pSrc, notReady,
2facd 20 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74   pOrderBy, pCost
2face 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 28 20 70  , &p);.    if( p
2facf 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
2fad0 74 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  tr ){.      sqli
2fad1 74 65 33 5f 66 72 65 65 28 70 2d 3e 69 64 78 53  te3_free(p->idxS
2fad2 74 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  tr);.    }.    s
2fad3 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61  qlite3DbFree(pPa
2fad4 72 73 65 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 7d  rse->db, p);.  }
2fad5 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a  else.#endif.  {.
2fad6 20 20 20 20 62 65 73 74 42 74 72 65 65 49 6e 64      bestBtreeInd
2fad7 65 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20  ex(pParse, pWC, 
2fad8 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20  pSrc, notReady, 
2fad9 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74 29  pOrderBy, pCost)
2fada 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
2fadb 69 73 61 62 6c 65 20 61 20 74 65 72 6d 20 69 6e  isable a term in
2fadc 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2fadd 65 2e 20 20 45 78 63 65 70 74 2c 20 64 6f 20 6e  e.  Except, do n
2fade 6f 74 20 64 69 73 61 62 6c 65 20 74 68 65 20 74  ot disable the t
2fadf 65 72 6d 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e  erm.** if it con
2fae0 74 72 6f 6c 73 20 61 20 4c 45 46 54 20 4f 55 54  trols a LEFT OUT
2fae1 45 52 20 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64  ER JOIN and it d
2fae2 69 64 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65  id not originate
2fae3 20 69 6e 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72   in the ON.** or
2fae4 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66   USING clause of
2fae5 20 74 68 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a   that join..**.*
2fae6 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 74  * Consider the t
2fae7 65 72 6d 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e  erm t2.z='ok' in
2fae8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71   the following q
2fae9 75 65 72 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ueries:.**.**   
2faea 28 31 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52  (1)  SELECT * FR
2faeb 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20  OM t1 LEFT JOIN 
2faec 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20  t2 ON t1.a=t2.x 
2faed 57 48 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a  WHERE t2.z='ok'.
2faee 2a 2a 20 20 20 28 32 29 20 20 53 45 4c 45 43 54  **   (2)  SELECT
2faef 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20   * FROM t1 LEFT 
2faf0 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d  JOIN t2 ON t1.a=
2faf1 74 32 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f  t2.x AND t2.z='o
2faf2 6b 27 0a 2a 2a 20 20 20 28 33 29 20 20 53 45 4c  k'.**   (3)  SEL
2faf3 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74  ECT * FROM t1, t
2faf4 32 20 57 48 45 52 45 20 74 31 2e 61 3d 74 32 2e  2 WHERE t1.a=t2.
2faf5 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a  x AND t2.z='ok'.
2faf6 2a 2a 0a 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27  **.** The t2.z='
2faf7 6f 6b 27 20 69 73 20 64 69 73 61 62 6c 65 64 20  ok' is disabled 
2faf8 69 6e 20 74 68 65 20 69 6e 20 28 32 29 20 62 65  in the in (2) be
2faf9 63 61 75 73 65 20 69 74 20 6f 72 69 67 69 6e 61  cause it origina
2fafa 74 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e  tes.** in the ON
2fafb 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20 74 65   clause.  The te
2fafc 72 6d 20 69 73 20 64 69 73 61 62 6c 65 64 20 69  rm is disabled i
2fafd 6e 20 28 33 29 20 62 65 63 61 75 73 65 20 69 74  n (3) because it
2fafe 20 69 73 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20   is not part.** 
2faff 6f 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20  of a LEFT OUTER 
2fb00 4a 4f 49 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74  JOIN.  In (1), t
2fb01 68 65 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 64  he term is not d
2fb02 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44  isabled..**.** D
2fb03 69 73 61 62 6c 69 6e 67 20 61 20 74 65 72 6d 20  isabling a term 
2fb04 63 61 75 73 65 73 20 74 68 61 74 20 74 65 72 6d  causes that term
2fb05 20 74 6f 20 6e 6f 74 20 62 65 20 74 65 73 74 65   to not be teste
2fb06 64 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c  d in the inner l
2fb07 6f 6f 70 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f  oop.** of the jo
2fb08 69 6e 2e 20 20 44 69 73 61 62 6c 69 6e 67 20 69  in.  Disabling i
2fb09 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
2fb0a 6e 2e 20 20 57 68 65 6e 20 74 65 72 6d 73 20 61  n.  When terms a
2fb0b 72 65 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20  re satisfied.** 
2fb0c 62 79 20 69 6e 64 69 63 65 73 2c 20 77 65 20 64  by indices, we d
2fb0d 69 73 61 62 6c 65 20 74 68 65 6d 20 74 6f 20 70  isable them to p
2fb0e 72 65 76 65 6e 74 20 72 65 64 75 6e 64 61 6e 74  revent redundant
2fb0f 20 74 65 73 74 73 20 69 6e 20 74 68 65 20 69 6e   tests in the in
2fb10 6e 65 72 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65  ner.** loop.  We
2fb11 20 77 6f 75 6c 64 20 67 65 74 20 74 68 65 20 63   would get the c
2fb12 6f 72 72 65 63 74 20 72 65 73 75 6c 74 73 20 69  orrect results i
2fb13 66 20 6e 6f 74 68 69 6e 67 20 77 65 72 65 20 65  f nothing were e
2fb14 76 65 72 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a  ver disabled,.**
2fb15 20 62 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74   but joins might
2fb16 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c   run a little sl
2fb17 6f 77 65 72 2e 20 20 54 68 65 20 74 72 69 63 6b  ower.  The trick
2fb18 20 69 73 20 74 6f 20 64 69 73 61 62 6c 65 20 61   is to disable a
2fb19 73 20 6d 75 63 68 0a 2a 2a 20 61 73 20 77 65 20  s much.** as we 
2fb1a 63 61 6e 20 77 69 74 68 6f 75 74 20 64 69 73 61  can without disa
2fb1b 62 6c 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20  bling too much. 
2fb1c 20 49 66 20 77 65 20 64 69 73 61 62 6c 65 64 20   If we disabled 
2fb1d 69 6e 20 28 31 29 2c 20 77 65 27 64 20 67 65 74  in (1), we'd get
2fb1e 0a 2a 2a 20 74 68 65 20 77 72 6f 6e 67 20 61 6e  .** the wrong an
2fb1f 73 77 65 72 2e 20 20 53 65 65 20 74 69 63 6b 65  swer.  See ticke
2fb20 74 20 23 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69  t #813..*/.stati
2fb21 63 20 76 6f 69 64 20 64 69 73 61 62 6c 65 54 65  c void disableTe
2fb22 72 6d 28 57 68 65 72 65 4c 65 76 65 6c 20 2a 70  rm(WhereLevel *p
2fb23 4c 65 76 65 6c 2c 20 57 68 65 72 65 54 65 72 6d  Level, WhereTerm
2fb24 20 2a 70 54 65 72 6d 29 7b 0a 20 20 69 66 28 20   *pTerm){.  if( 
2fb25 70 54 65 72 6d 0a 20 20 20 20 20 20 26 26 20 41  pTerm.      && A
2fb26 4c 57 41 59 53 28 28 70 54 65 72 6d 2d 3e 77 74  LWAYS((pTerm->wt
2fb27 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44  Flags & TERM_COD
2fb28 45 44 29 3d 3d 30 29 0a 20 20 20 20 20 20 26 26  ED)==0).      &&
2fb29 20 28 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a   (pLevel->iLeftJ
2fb2a 6f 69 6e 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61  oin==0 || ExprHa
2fb2b 73 50 72 6f 70 65 72 74 79 28 70 54 65 72 6d 2d  sProperty(pTerm-
2fb2c 3e 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a  >pExpr, EP_FromJ
2fb2d 6f 69 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 70  oin)).  ){.    p
2fb2e 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d  Term->wtFlags |=
2fb2f 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20   TERM_CODED;.   
2fb30 20 69 66 28 20 70 54 65 72 6d 2d 3e 69 50 61 72   if( pTerm->iPar
2fb31 65 6e 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  ent>=0 ){.      
2fb32 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 74 68 65  WhereTerm *pOthe
2fb33 72 20 3d 20 26 70 54 65 72 6d 2d 3e 70 57 43 2d  r = &pTerm->pWC-
2fb34 3e 61 5b 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e  >a[pTerm->iParen
2fb35 74 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 28 2d  t];.      if( (-
2fb36 2d 70 4f 74 68 65 72 2d 3e 6e 43 68 69 6c 64 29  -pOther->nChild)
2fb37 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ==0 ){.        d
2fb38 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65  isableTerm(pLeve
2fb39 6c 2c 20 70 4f 74 68 65 72 29 3b 0a 20 20 20 20  l, pOther);.    
2fb3a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
2fb3b 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f  ./*.** Code an O
2fb3c 50 5f 41 66 66 69 6e 69 74 79 20 6f 70 63 6f 64  P_Affinity opcod
2fb3d 65 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20 63  e to apply the c
2fb3e 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
2fb3f 74 72 69 6e 67 20 7a 41 66 66 0a 2a 2a 20 74 6f  tring zAff.** to
2fb40 20 74 68 65 20 6e 20 72 65 67 69 73 74 65 72 73   the n registers
2fb41 20 73 74 61 72 74 69 6e 67 20 61 74 20 62 61 73   starting at bas
2fb42 65 2e 20 0a 2a 2a 0a 2a 2a 20 41 73 20 61 6e 20  e. .**.** As an 
2fb43 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 53 51  optimization, SQ
2fb44 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 65 6e  LITE_AFF_NONE en
2fb45 74 72 69 65 73 20 28 77 68 69 63 68 20 61 72 65  tries (which are
2fb46 20 6e 6f 2d 6f 70 73 29 20 61 74 20 74 68 65 0a   no-ops) at the.
2fb47 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64  ** beginning and
2fb48 20 65 6e 64 20 6f 66 20 7a 41 66 66 20 61 72 65   end of zAff are
2fb49 20 69 67 6e 6f 72 65 64 2e 20 20 49 66 20 61 6c   ignored.  If al
2fb4a 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 7a 41 66  l entries in zAf
2fb4b 66 20 61 72 65 0a 2a 2a 20 53 51 4c 49 54 45 5f  f are.** SQLITE_
2fb4c 41 46 46 5f 4e 4f 4e 45 2c 20 74 68 65 6e 20 6e  AFF_NONE, then n
2fb4d 6f 20 63 6f 64 65 20 67 65 74 73 20 67 65 6e 65  o code gets gene
2fb4e 72 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  rated..**.** Thi
2fb4f 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73 20  s routine makes 
2fb50 69 74 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66 20  its own copy of 
2fb51 7a 41 66 66 20 73 6f 20 74 68 61 74 20 74 68 65  zAff so that the
2fb52 20 63 61 6c 6c 65 72 20 69 73 20 66 72 65 65 0a   caller is free.
2fb53 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 7a 41 66  ** to modify zAf
2fb54 66 20 61 66 74 65 72 20 74 68 69 73 20 72 6f 75  f after this rou
2fb55 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f  tine returns..*/
2fb56 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
2fb57 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 50  eApplyAffinity(P
2fb58 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
2fb59 74 20 62 61 73 65 2c 20 69 6e 74 20 6e 2c 20 63  t base, int n, c
2fb5a 68 61 72 20 2a 7a 41 66 66 29 7b 0a 20 20 56 64  har *zAff){.  Vd
2fb5b 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2fb5c 70 56 64 62 65 3b 0a 20 20 69 66 28 20 7a 41 66  pVdbe;.  if( zAf
2fb5d 66 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  f==0 ){.    asse
2fb5e 72 74 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  rt( pParse->db->
2fb5f 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
2fb60 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2fb61 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
2fb62 3b 0a 0a 20 20 2f 2a 20 41 64 6a 75 73 74 20 62  ;..  /* Adjust b
2fb63 61 73 65 20 61 6e 64 20 6e 20 74 6f 20 73 6b 69  ase and n to ski
2fb64 70 20 6f 76 65 72 20 53 51 4c 49 54 45 5f 41 46  p over SQLITE_AF
2fb65 46 5f 4e 4f 4e 45 20 65 6e 74 72 69 65 73 20 61  F_NONE entries a
2fb66 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  t the beginning.
2fb67 20 20 2a 2a 20 61 6e 64 20 65 6e 64 20 6f 66 20    ** and end of 
2fb68 74 68 65 20 61 66 66 69 6e 69 74 79 20 73 74 72  the affinity str
2fb69 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c  ing..  */.  whil
2fb6a 65 28 20 6e 3e 30 20 26 26 20 7a 41 66 66 5b 30  e( n>0 && zAff[0
2fb6b 5d 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  ]==SQLITE_AFF_NO
2fb6c 4e 45 20 29 7b 0a 20 20 20 20 6e 2d 2d 3b 0a 20  NE ){.    n--;. 
2fb6d 20 20 20 62 61 73 65 2b 2b 3b 0a 20 20 20 20 7a     base++;.    z
2fb6e 41 66 66 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69  Aff++;.  }.  whi
2fb6f 6c 65 28 20 6e 3e 31 20 26 26 20 7a 41 66 66 5b  le( n>1 && zAff[
2fb70 6e 2d 31 5d 3d 3d 53 51 4c 49 54 45 5f 41 46 46  n-1]==SQLITE_AFF
2fb71 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 6e 2d 2d  _NONE ){.    n--
2fb72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 64 65  ;.  }..  /* Code
2fb73 20 74 68 65 20 4f 50 5f 41 66 66 69 6e 69 74 79   the OP_Affinity
2fb74 20 6f 70 63 6f 64 65 20 69 66 20 74 68 65 72 65   opcode if there
2fb75 20 69 73 20 61 6e 79 74 68 69 6e 67 20 6c 65 66   is anything lef
2fb76 74 20 74 6f 20 64 6f 2e 20 2a 2f 0a 20 20 69 66  t to do. */.  if
2fb77 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  ( n>0 ){.    sql
2fb78 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2fb79 2c 20 4f 50 5f 41 66 66 69 6e 69 74 79 2c 20 62  , OP_Affinity, b
2fb7a 61 73 65 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c  ase, n);.    sql
2fb7b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2fb7c 28 76 2c 20 2d 31 2c 20 7a 41 66 66 2c 20 6e 29  (v, -1, zAff, n)
2fb7d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2fb7e 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
2fb7f 61 6e 67 65 28 70 50 61 72 73 65 2c 20 62 61 73  ange(pParse, bas
2fb80 65 2c 20 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f  e, n);.  }.}.../
2fb81 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2fb82 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  de for a single 
2fb83 65 71 75 61 6c 69 74 79 20 74 65 72 6d 20 6f 66  equality term of
2fb84 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2fb85 65 2e 20 20 41 6e 20 65 71 75 61 6c 69 74 79 0a  e.  An equality.
2fb86 2a 2a 20 74 65 72 6d 20 63 61 6e 20 62 65 20 65  ** term can be e
2fb87 69 74 68 65 72 20 58 3d 65 78 70 72 20 6f 72 20  ither X=expr or 
2fb88 58 20 49 4e 20 28 2e 2e 2e 29 2e 20 20 20 70 54  X IN (...).   pT
2fb89 65 72 6d 20 69 73 20 74 68 65 20 74 65 72 6d 20  erm is the term 
2fb8a 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 64 65 64 2e  to be .** coded.
2fb8b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65  .**.** The curre
2fb8c 6e 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  nt value for the
2fb8d 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6c   constraint is l
2fb8e 65 66 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  eft in register 
2fb8f 69 52 65 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  iReg..**.** For 
2fb90 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20  a constraint of 
2fb91 74 68 65 20 66 6f 72 6d 20 58 3d 65 78 70 72 2c  the form X=expr,
2fb92 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2fb93 69 73 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64  is evaluated and
2fb94 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69   its.** result i
2fb95 73 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 73 74  s left on the st
2fb96 61 63 6b 2e 20 20 46 6f 72 20 63 6f 6e 73 74 72  ack.  For constr
2fb97 61 69 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72  aints of the for
2fb98 6d 20 58 20 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20  m X IN (...).** 
2fb99 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  this routine set
2fb9a 73 20 75 70 20 61 20 6c 6f 6f 70 20 74 68 61 74  s up a loop that
2fb9b 20 77 69 6c 6c 20 69 74 65 72 61 74 65 20 6f 76   will iterate ov
2fb9c 65 72 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66  er all values of
2fb9d 20 58 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   X..*/.static in
2fb9e 74 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65  t codeEqualityTe
2fb9f 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  rm(.  Parse *pPa
2fba0 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  rse,      /* The
2fba1 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
2fba2 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
2fba3 2a 70 54 65 72 6d 2c 20 20 20 2f 2a 20 54 68 65  *pTerm,   /* The
2fba4 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45   term of the WHE
2fba5 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20  RE clause to be 
2fba6 63 6f 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65  coded */.  Where
2fba7 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f  Level *pLevel, /
2fba8 2a 20 57 68 65 6e 20 6c 65 76 65 6c 20 6f 66 20  * When level of 
2fba9 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2fbaa 77 65 20 61 72 65 20 77 6f 72 6b 69 6e 67 20 6f  we are working o
2fbab 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 72 67  n */.  int iTarg
2fbac 65 74 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74  et         /* At
2fbad 74 65 6d 70 74 20 74 6f 20 6c 65 61 76 65 20 72  tempt to leave r
2fbae 65 73 75 6c 74 73 20 69 6e 20 74 68 69 73 20 72  esults in this r
2fbaf 65 67 69 73 74 65 72 20 2a 2f 0a 29 7b 0a 20 20  egister */.){.  
2fbb0 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65 72 6d  Expr *pX = pTerm
2fbb1 2d 3e 70 45 78 70 72 3b 0a 20 20 56 64 62 65 20  ->pExpr;.  Vdbe 
2fbb2 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2fbb3 62 65 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b 20  be;.  int iReg; 
2fbb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fbb5 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
2fbb6 64 69 6e 67 20 72 65 73 75 6c 74 73 20 2a 2f 0a  ding results */.
2fbb7 0a 20 20 61 73 73 65 72 74 28 20 69 54 61 72 67  .  assert( iTarg
2fbb8 65 74 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 58  et>0 );.  if( pX
2fbb9 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20  ->op==TK_EQ ){. 
2fbba 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65     iReg = sqlite
2fbbb 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
2fbbc 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67  pParse, pX->pRig
2fbbd 68 74 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20  ht, iTarget);.  
2fbbe 7d 65 6c 73 65 20 69 66 28 20 70 58 2d 3e 6f 70  }else if( pX->op
2fbbf 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20  ==TK_ISNULL ){. 
2fbc0 20 20 20 69 52 65 67 20 3d 20 69 54 61 72 67 65     iReg = iTarge
2fbc1 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
2fbc2 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2fbc3 75 6c 6c 2c 20 30 2c 20 69 52 65 67 29 3b 0a 23  ull, 0, iReg);.#
2fbc4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2fbc5 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 7d 65  IT_SUBQUERY.  }e
2fbc6 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 65 54 79  lse{.    int eTy
2fbc7 70 65 3b 0a 20 20 20 20 69 6e 74 20 69 54 61 62  pe;.    int iTab
2fbc8 3b 0a 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c  ;.    struct InL
2fbc9 6f 6f 70 20 2a 70 49 6e 3b 0a 0a 20 20 20 20 61  oop *pIn;..    a
2fbca 73 73 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54  ssert( pX->op==T
2fbcb 4b 5f 49 4e 20 29 3b 0a 20 20 20 20 69 52 65 67  K_IN );.    iReg
2fbcc 20 3d 20 69 54 61 72 67 65 74 3b 0a 20 20 20 20   = iTarget;.    
2fbcd 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46  eType = sqlite3F
2fbce 69 6e 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73  indInIndex(pPars
2fbcf 65 2c 20 70 58 2c 20 30 29 3b 0a 20 20 20 20 69  e, pX, 0);.    i
2fbd0 54 61 62 20 3d 20 70 58 2d 3e 69 54 61 62 6c 65  Tab = pX->iTable
2fbd1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2fbd2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
2fbd3 77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a  wind, iTab, 0);.
2fbd4 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 76      assert( pLev
2fbd5 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2fbd6 20 26 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45   & WHERE_IN_ABLE
2fbd7 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76   );.    if( pLev
2fbd8 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 3d 3d 30 20  el->u.in.nIn==0 
2fbd9 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  ){.      pLevel-
2fbda 3e 61 64 64 72 4e 78 74 20 3d 20 73 71 6c 69 74  >addrNxt = sqlit
2fbdb 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2fbdc 76 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c  v);.    }.    pL
2fbdd 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2b 2b  evel->u.in.nIn++
2fbde 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e  ;.    pLevel->u.
2fbdf 69 6e 2e 61 49 6e 4c 6f 6f 70 20 3d 0a 20 20 20  in.aInLoop =.   
2fbe0 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61      sqlite3DbRea
2fbe1 6c 6c 6f 63 4f 72 46 72 65 65 28 70 50 61 72 73  llocOrFree(pPars
2fbe2 65 2d 3e 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75  e->db, pLevel->u
2fbe3 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20  .in.aInLoop,.   
2fbe4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fbe5 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
2fbe6 66 28 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61  f(pLevel->u.in.a
2fbe7 49 6e 4c 6f 6f 70 5b 30 5d 29 2a 70 4c 65 76 65  InLoop[0])*pLeve
2fbe8 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 29 3b 0a 20 20  l->u.in.nIn);.  
2fbe9 20 20 70 49 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e    pIn = pLevel->
2fbea 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 3b 0a 20 20  u.in.aInLoop;.  
2fbeb 20 20 69 66 28 20 70 49 6e 20 29 7b 0a 20 20 20    if( pIn ){.   
2fbec 20 20 20 70 49 6e 20 2b 3d 20 70 4c 65 76 65 6c     pIn += pLevel
2fbed 2d 3e 75 2e 69 6e 2e 6e 49 6e 20 2d 20 31 3b 0a  ->u.in.nIn - 1;.
2fbee 20 20 20 20 20 20 70 49 6e 2d 3e 69 43 75 72 20        pIn->iCur 
2fbef 3d 20 69 54 61 62 3b 0a 20 20 20 20 20 20 69 66  = iTab;.      if
2fbf0 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45  ( eType==IN_INDE
2fbf1 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20  X_ROWID ){.     
2fbf2 20 20 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f     pIn->addrInTo
2fbf3 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  p = sqlite3VdbeA
2fbf4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
2fbf5 64 2c 20 69 54 61 62 2c 20 69 52 65 67 29 3b 0a  d, iTab, iReg);.
2fbf6 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2fbf7 20 20 20 20 20 70 49 6e 2d 3e 61 64 64 72 49 6e       pIn->addrIn
2fbf8 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Top = sqlite3Vdb
2fbf9 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2fbfa 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 30 2c 20 69  lumn, iTab, 0, i
2fbfb 52 65 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Reg);.      }.  
2fbfc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2fbfd 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp1(v, OP_IsNu
2fbfe 6c 6c 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d  ll, iReg);.    }
2fbff 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 65 76  else{.      pLev
2fc00 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 20 3d 20 30  el->u.in.nIn = 0
2fc01 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
2fc02 20 7d 0a 20 20 64 69 73 61 62 6c 65 54 65 72 6d   }.  disableTerm
2fc03 28 70 4c 65 76 65 6c 2c 20 70 54 65 72 6d 29 3b  (pLevel, pTerm);
2fc04 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a  .  return iReg;.
2fc05 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
2fc06 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
2fc07 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20 3d 3d   evaluate all ==
2fc08 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69   and IN constrai
2fc09 6e 74 73 20 66 6f 72 20 61 6e 0a 2a 2a 20 69 6e  nts for an.** in
2fc0a 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65  dex..**.** For e
2fc0b 78 61 6d 70 6c 65 2c 20 63 6f 6e 73 69 64 65 72  xample, consider
2fc0c 20 74 61 62 6c 65 20 74 31 28 61 2c 62 2c 63 2c   table t1(a,b,c,
2fc0d 64 2c 65 2c 66 29 20 77 69 74 68 20 69 6e 64 65  d,e,f) with inde
2fc0e 78 20 69 31 28 61 2c 62 2c 63 29 2e 0a 2a 2a 20  x i1(a,b,c)..** 
2fc0f 53 75 70 70 6f 73 65 20 74 68 65 20 57 48 45 52  Suppose the WHER
2fc10 45 20 63 6c 61 75 73 65 20 69 73 20 74 68 69 73  E clause is this
2fc11 3a 20 20 61 3d 3d 35 20 41 4e 44 20 62 20 49 4e  :  a==5 AND b IN
2fc12 20 28 31 2c 32 2c 33 29 20 41 4e 44 20 63 3e 35   (1,2,3) AND c>5
2fc13 20 41 4e 44 20 63 3c 31 30 0a 2a 2a 20 54 68 65   AND c<10.** The
2fc14 20 69 6e 64 65 78 20 68 61 73 20 61 73 20 6d 61   index has as ma
2fc15 6e 79 20 61 73 20 74 68 72 65 65 20 65 71 75 61  ny as three equa
2fc16 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2fc17 2c 20 62 75 74 20 69 6e 20 74 68 69 73 0a 2a 2a  , but in this.**
2fc18 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 74 68   example, the th
2fc19 69 72 64 20 22 63 22 20 76 61 6c 75 65 20 69 73  ird "c" value is
2fc1a 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 2e 20   an inequality. 
2fc1b 20 53 6f 20 6f 6e 6c 79 20 74 77 6f 20 0a 2a 2a   So only two .**
2fc1c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65   constraints are
2fc1d 20 63 6f 64 65 64 2e 20 20 54 68 69 73 20 72 6f   coded.  This ro
2fc1e 75 74 69 6e 65 20 77 69 6c 6c 20 67 65 6e 65 72  utine will gener
2fc1f 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61 6c  ate code to eval
2fc20 75 61 74 65 0a 2a 2a 20 61 3d 3d 35 20 61 6e 64  uate.** a==5 and
2fc21 20 62 20 49 4e 20 28 31 2c 32 2c 33 29 2e 20 20   b IN (1,2,3).  
2fc22 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  The current valu
2fc23 65 73 20 66 6f 72 20 61 20 61 6e 64 20 62 20 77  es for a and b w
2fc24 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 0a 2a 2a  ill be stored.**
2fc25 20 69 6e 20 63 6f 6e 73 65 63 75 74 69 76 65 20   in consecutive 
2fc26 72 65 67 69 73 74 65 72 73 20 61 6e 64 20 74 68  registers and th
2fc27 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 66  e index of the f
2fc28 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69 73  irst register is
2fc29 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
2fc2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20   In the example 
2fc2b 61 62 6f 76 65 20 6e 45 71 3d 3d 32 2e 20 20 42  above nEq==2.  B
2fc2c 75 74 20 74 68 69 73 20 73 75 62 72 6f 75 74 69  ut this subrouti
2fc2d 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20 61 6e 79  ne works for any
2fc2e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 6e 45 71   value.** of nEq
2fc2f 20 69 6e 63 6c 75 64 69 6e 67 20 30 2e 20 20 49   including 0.  I
2fc30 66 20 6e 45 71 3d 3d 30 2c 20 74 68 69 73 20 72  f nEq==0, this r
2fc31 6f 75 74 69 6e 65 20 69 73 20 6e 65 61 72 6c 79  outine is nearly
2fc32 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 54 68 65   a no-op..** The
2fc33 20 6f 6e 6c 79 20 74 68 69 6e 67 20 69 74 20 64   only thing it d
2fc34 6f 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 20  oes is allocate 
2fc35 74 68 65 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d  the pLevel->iMem
2fc36 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64   memory cell and
2fc37 0a 2a 2a 20 63 6f 6d 70 75 74 65 20 74 68 65 20  .** compute the 
2fc38 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 2e  affinity string.
2fc39 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2fc3a 69 6e 65 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63  ine always alloc
2fc3b 61 74 65 73 20 61 74 20 6c 65 61 73 74 20 6f 6e  ates at least on
2fc3c 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e  e memory cell an
2fc3d 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65  d returns.** the
2fc3e 20 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20 6d   index of that m
2fc3f 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 54 68 65 20  emory cell. The 
2fc40 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 63 61 6c  code that.** cal
2fc41 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2fc42 77 69 6c 6c 20 75 73 65 20 74 68 61 74 20 6d 65  will use that me
2fc43 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 74 6f  mory cell to sto
2fc44 72 65 20 74 68 65 20 74 65 72 6d 69 6e 61 74 69  re the terminati
2fc45 6f 6e 0a 2a 2a 20 6b 65 79 20 76 61 6c 75 65 20  on.** key value 
2fc46 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 49 66  of the loop.  If
2fc47 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 49 4e 20   one or more IN 
2fc48 6f 70 65 72 61 74 6f 72 73 20 61 70 70 65 61 72  operators appear
2fc49 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72  , then.** this r
2fc4a 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
2fc4b 20 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e   an additional n
2fc4c 45 71 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  Eq memory cells 
2fc4d 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  for internal.** 
2fc4e 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72  use..**.** Befor
2fc4f 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a  e returning, *pz
2fc50 41 66 66 20 69 73 20 73 65 74 20 74 6f 20 70 6f  Aff is set to po
2fc51 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
2fc52 63 6f 6e 74 61 69 6e 69 6e 67 20 61 0a 2a 2a 20  containing a.** 
2fc53 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75  copy of the colu
2fc54 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
2fc55 6e 67 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ng of the index 
2fc56 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 0a  allocated using.
2fc57 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  ** sqlite3DbMall
2fc58 6f 63 28 29 2e 20 45 78 63 65 70 74 2c 20 65 6e  oc(). Except, en
2fc59 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 70  tries in the cop
2fc5a 79 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20  y of the string 
2fc5b 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
2fc5c 74 68 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73  th equality cons
2fc5d 74 72 61 69 6e 74 73 20 74 68 61 74 20 75 73 65  traints that use
2fc5e 20 4e 4f 4e 45 20 61 66 66 69 6e 69 74 79 20 61   NONE affinity a
2fc5f 72 65 20 73 65 74 20 74 6f 0a 2a 2a 20 53 51 4c  re set to.** SQL
2fc60 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 54 68  ITE_AFF_NONE. Th
2fc61 69 73 20 69 73 20 74 6f 20 64 65 61 6c 20 77 69  is is to deal wi
2fc62 74 68 20 53 51 4c 20 73 75 63 68 20 61 73 20 74  th SQL such as t
2fc63 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
2fc64 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42  .**   CREATE TAB
2fc65 4c 45 20 74 31 28 61 20 54 45 58 54 20 50 52 49  LE t1(a TEXT PRI
2fc66 4d 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a 2a 2a  MARY KEY, b);.**
2fc67 20 20 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52     SELECT ... FR
2fc68 4f 4d 20 74 31 20 41 53 20 74 32 2c 20 74 31 20  OM t1 AS t2, t1 
2fc69 57 48 45 52 45 20 74 31 2e 61 20 3d 20 74 32 2e  WHERE t1.a = t2.
2fc6a 62 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  b;.**.** In the 
2fc6b 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74  example above, t
2fc6c 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 31 28 61  he index on t1(a
2fc6d 29 20 68 61 73 20 54 45 58 54 20 61 66 66 69 6e  ) has TEXT affin
2fc6e 69 74 79 2e 20 42 75 74 20 73 69 6e 63 65 0a 2a  ity. But since.*
2fc6f 2a 20 74 68 65 20 72 69 67 68 74 20 68 61 6e 64  * the right hand
2fc70 20 73 69 64 65 20 6f 66 20 74 68 65 20 65 71 75   side of the equ
2fc71 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2fc72 20 28 74 32 2e 62 29 20 68 61 73 20 4e 4f 4e 45   (t2.b) has NONE
2fc73 20 61 66 66 69 6e 69 74 79 2c 0a 2a 2a 20 6e 6f   affinity,.** no
2fc74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 73 68 6f 75   conversion shou
2fc75 6c 64 20 62 65 20 61 74 74 65 6d 70 74 65 64 20  ld be attempted 
2fc76 62 65 66 6f 72 65 20 75 73 69 6e 67 20 61 20 74  before using a t
2fc77 32 2e 62 20 76 61 6c 75 65 20 61 73 20 70 61 72  2.b value as par
2fc78 74 20 6f 66 0a 2a 2a 20 61 20 6b 65 79 20 74 6f  t of.** a key to
2fc79 20 73 65 61 72 63 68 20 74 68 65 20 69 6e 64 65   search the inde
2fc7a 78 2e 20 48 65 6e 63 65 20 74 68 65 20 66 69 72  x. Hence the fir
2fc7b 73 74 20 62 79 74 65 20 69 6e 20 74 68 65 20 72  st byte in the r
2fc7c 65 74 75 72 6e 65 64 20 61 66 66 69 6e 69 74 79  eturned affinity
2fc7d 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 20 74 68  .** string in th
2fc7e 69 73 20 65 78 61 6d 70 6c 65 20 77 6f 75 6c 64  is example would
2fc7f 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54   be set to SQLIT
2fc80 45 5f 41 46 46 5f 4e 4f 4e 45 2e 0a 2a 2f 0a 73  E_AFF_NONE..*/.s
2fc81 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c  tatic int codeAl
2fc82 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 0a  lEqualityTerms(.
2fc83 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2fc84 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
2fc85 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2fc86 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2fc87 65 6c 2c 20 20 20 2f 2a 20 57 68 69 63 68 20 6e  el,   /* Which n
2fc88 65 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68  ested loop of th
2fc89 65 20 46 52 4f 4d 20 77 65 20 61 72 65 20 63 6f  e FROM we are co
2fc8a 64 69 6e 67 20 2a 2f 0a 20 20 57 68 65 72 65 43  ding */.  WhereC
2fc8b 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20  lause *pWC,     
2fc8c 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2fc8d 75 73 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  use */.  Bitmask
2fc8e 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 2f   notReady,     /
2fc8f 2a 20 57 68 69 63 68 20 70 61 72 74 73 20 6f 66  * Which parts of
2fc90 20 46 52 4f 4d 20 68 61 76 65 20 6e 6f 74 20 79   FROM have not y
2fc91 65 74 20 62 65 65 6e 20 63 6f 64 65 64 20 2a 2f  et been coded */
2fc92 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 52 65 67  .  int nExtraReg
2fc93 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ,        /* Numb
2fc94 65 72 20 6f 66 20 65 78 74 72 61 20 72 65 67 69  er of extra regi
2fc95 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74  sters to allocat
2fc96 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
2fc97 41 66 66 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Aff          /* 
2fc98 4f 55 54 3a 20 53 65 74 20 74 6f 20 70 6f 69 6e  OUT: Set to poin
2fc99 74 20 74 6f 20 61 66 66 69 6e 69 74 79 20 73 74  t to affinity st
2fc9a 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ring */.){.  int
2fc9b 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70   nEq = pLevel->p
2fc9c 6c 61 6e 2e 6e 45 71 3b 20 20 20 2f 2a 20 54 68  lan.nEq;   /* Th
2fc9d 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f  e number of == o
2fc9e 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73  r IN constraints
2fc9f 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 56 64   to code */.  Vd
2fca0 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2fca1 70 56 64 62 65 3b 20 20 20 20 20 20 2f 2a 20 54  pVdbe;      /* T
2fca2 68 65 20 76 6d 20 75 6e 64 65 72 20 63 6f 6e 73  he vm under cons
2fca3 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e  truction */.  In
2fca4 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
2fca5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2fca6 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 75  he index being u
2fca7 73 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 6f  sed for this loo
2fca8 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20  p */.  int iCur 
2fca9 3d 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75  = pLevel->iTabCu
2fcaa 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  r;   /* The curs
2fcab 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  or of the table 
2fcac 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
2fcad 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  pTerm;          
2fcae 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63     /* A single c
2fcaf 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 20 2a  onstraint term *
2fcb0 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20  /.  int j;      
2fcb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fcb2 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2fcb3 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 42 61  r */.  int regBa
2fcb4 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
2fcb5 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67       /* Base reg
2fcb6 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  ister */.  int n
2fcb7 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Reg;            
2fcb8 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2fcb9 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  er of registers 
2fcba 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20  to allocate */. 
2fcbb 20 63 68 61 72 20 2a 7a 41 66 66 3b 20 20 20 20   char *zAff;    
2fcbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2fcbd 2a 20 41 66 66 69 6e 69 74 79 20 73 74 72 69 6e  * Affinity strin
2fcbe 67 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a  g to return */..
2fcbf 20 20 2f 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65    /* This module
2fcc0 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20   is only called 
2fcc1 6f 6e 20 71 75 65 72 79 20 70 6c 61 6e 73 20 74  on query plans t
2fcc2 68 61 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78  hat use an index
2fcc3 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
2fcc4 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2fcc5 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45  ags & WHERE_INDE
2fcc6 58 45 44 20 29 3b 0a 20 20 70 49 64 78 20 3d 20  XED );.  pIdx = 
2fcc7 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
2fcc8 49 64 78 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  Idx;..  /* Figur
2fcc9 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6d  e out how many m
2fcca 65 6d 6f 72 79 20 63 65 6c 6c 73 20 77 65 20 77  emory cells we w
2fccb 69 6c 6c 20 6e 65 65 64 20 74 68 65 6e 20 61 6c  ill need then al
2fccc 6c 6f 63 61 74 65 20 74 68 65 6d 2e 0a 20 20 2a  locate them..  *
2fccd 2f 0a 20 20 72 65 67 42 61 73 65 20 3d 20 70 50  /.  regBase = pP
2fcce 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a  arse->nMem + 1;.
2fccf 20 20 6e 52 65 67 20 3d 20 70 4c 65 76 65 6c 2d    nReg = pLevel-
2fcd0 3e 70 6c 61 6e 2e 6e 45 71 20 2b 20 6e 45 78 74  >plan.nEq + nExt
2fcd1 72 61 52 65 67 3b 0a 20 20 70 50 61 72 73 65 2d  raReg;.  pParse-
2fcd2 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a 0a  >nMem += nReg;..
2fcd3 20 20 7a 41 66 66 20 3d 20 73 71 6c 69 74 65 33    zAff = sqlite3
2fcd4 44 62 53 74 72 44 75 70 28 70 50 61 72 73 65 2d  DbStrDup(pParse-
2fcd5 3e 64 62 2c 20 73 71 6c 69 74 65 33 49 6e 64 65  >db, sqlite3Inde
2fcd6 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20  xAffinityStr(v, 
2fcd7 70 49 64 78 29 29 3b 0a 20 20 69 66 28 20 21 7a  pIdx));.  if( !z
2fcd8 41 66 66 20 29 7b 0a 20 20 20 20 70 50 61 72 73  Aff ){.    pPars
2fcd9 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
2fcda 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  led = 1;.  }..  
2fcdb 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20  /* Evaluate the 
2fcdc 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2fcdd 69 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ints.  */.  asse
2fcde 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  rt( pIdx->nColum
2fcdf 6e 3e 3d 6e 45 71 20 29 3b 0a 20 20 66 6f 72 28  n>=nEq );.  for(
2fce0 6a 3d 30 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29  j=0; j<nEq; j++)
2fce1 7b 0a 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20  {.    int r1;.  
2fce2 20 20 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e    int k = pIdx->
2fce3 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20  aiColumn[j];.   
2fce4 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72   pTerm = findTer
2fce5 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20  m(pWC, iCur, k, 
2fce6 6e 6f 74 52 65 61 64 79 2c 20 70 4c 65 76 65 6c  notReady, pLevel
2fce7 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2c 20  ->plan.wsFlags, 
2fce8 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20 4e  pIdx);.    if( N
2fce9 45 56 45 52 28 70 54 65 72 6d 3d 3d 30 29 20 29  EVER(pTerm==0) )
2fcea 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73 73 65   break;.    asse
2fceb 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c  rt( (pTerm->wtFl
2fcec 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44  ags & TERM_CODED
2fced 29 3d 3d 30 20 29 3b 0a 20 20 20 20 72 31 20 3d  )==0 );.    r1 =
2fcee 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72   codeEqualityTer
2fcef 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c  m(pParse, pTerm,
2fcf0 20 70 4c 65 76 65 6c 2c 20 72 65 67 42 61 73 65   pLevel, regBase
2fcf1 2b 6a 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21  +j);.    if( r1!
2fcf2 3d 72 65 67 42 61 73 65 2b 6a 20 29 7b 0a 20 20  =regBase+j ){.  
2fcf3 20 20 20 20 69 66 28 20 6e 52 65 67 3d 3d 31 20      if( nReg==1 
2fcf4 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2fcf5 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2fcf6 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65  (pParse, regBase
2fcf7 29 3b 0a 20 20 20 20 20 20 20 20 72 65 67 42 61  );.        regBa
2fcf8 73 65 20 3d 20 72 31 3b 0a 20 20 20 20 20 20 7d  se = r1;.      }
2fcf9 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
2fcfa 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2fcfb 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 31 2c  v, OP_SCopy, r1,
2fcfc 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20   regBase+j);.   
2fcfd 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74     }.    }.    t
2fcfe 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
2fcff 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49  eOperator & WO_I
2fd00 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 74 65 73  SNULL );.    tes
2fd01 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
2fd02 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20  perator & WO_IN 
2fd03 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 65 72  );.    if( (pTer
2fd04 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
2fd05 57 4f 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e 29  WO_ISNULL|WO_IN)
2fd06 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 45 78  )==0 ){.      Ex
2fd07 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 54 65  pr *pRight = pTe
2fd08 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  rm->pExpr->pRigh
2fd09 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
2fd0a 45 78 70 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75  ExprCodeIsNullJu
2fd0b 6d 70 28 76 2c 20 70 52 69 67 68 74 2c 20 72 65  mp(v, pRight, re
2fd0c 67 42 61 73 65 2b 6a 2c 20 70 4c 65 76 65 6c 2d  gBase+j, pLevel-
2fd0d 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 20  >addrBrk);.     
2fd0e 20 69 66 28 20 7a 41 66 66 20 29 7b 0a 20 20 20   if( zAff ){.   
2fd0f 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2fd10 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
2fd11 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6a 5d 29  pRight, zAff[j])
2fd12 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  ==SQLITE_AFF_NON
2fd13 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  E ){.          z
2fd14 41 66 66 5b 6a 5d 20 3d 20 53 51 4c 49 54 45 5f  Aff[j] = SQLITE_
2fd15 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20  AFF_NONE;.      
2fd16 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2fd17 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65 64 73  sqlite3ExprNeeds
2fd18 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  NoAffinityChange
2fd19 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6a 5d  (pRight, zAff[j]
2fd1a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ) ){.          z
2fd1b 41 66 66 5b 6a 5d 20 3d 20 53 51 4c 49 54 45 5f  Aff[j] = SQLITE_
2fd1c 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20  AFF_NONE;.      
2fd1d 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2fd1e 7d 0a 20 20 7d 0a 20 20 2a 70 7a 41 66 66 20 3d  }.  }.  *pzAff =
2fd1f 20 7a 41 66 66 3b 0a 20 20 72 65 74 75 72 6e 20   zAff;.  return 
2fd20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  regBase;.}../*.*
2fd21 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2fd22 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f 66  for the start of
2fd23 20 74 68 65 20 69 4c 65 76 65 6c 2d 74 68 20 6c   the iLevel-th l
2fd24 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45 52 45  oop in the WHERE
2fd25 20 63 6c 61 75 73 65 0a 2a 2a 20 69 6d 70 6c 65   clause.** imple
2fd26 6d 65 6e 74 61 74 69 6f 6e 20 64 65 73 63 72 69  mentation descri
2fd27 62 65 64 20 62 79 20 70 57 49 6e 66 6f 2e 0a 2a  bed by pWInfo..*
2fd28 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b  /.static Bitmask
2fd29 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72   codeOneLoopStar
2fd2a 74 28 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  t(.  WhereInfo *
2fd2b 70 57 49 6e 66 6f 2c 20 20 20 2f 2a 20 43 6f 6d  pWInfo,   /* Com
2fd2c 70 6c 65 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  plete informatio
2fd2d 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 52  n about the WHER
2fd2e 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  E clause */.  in
2fd2f 74 20 69 4c 65 76 65 6c 2c 20 20 20 20 20 20 20  t iLevel,       
2fd30 20 20 20 2f 2a 20 57 68 69 63 68 20 6c 65 76 65     /* Which leve
2fd31 6c 20 6f 66 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  l of pWInfo->a[]
2fd32 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 64 65 64   should be coded
2fd33 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c 46   */.  u16 wctrlF
2fd34 6c 61 67 73 2c 20 20 20 20 20 20 2f 2a 20 4f 6e  lags,      /* On
2fd35 65 20 6f 66 20 74 68 65 20 57 48 45 52 45 5f 2a  e of the WHERE_*
2fd36 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20 69   flags defined i
2fd37 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f  n sqliteInt.h */
2fd38 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65  .  Bitmask notRe
2fd39 61 64 79 20 20 20 20 20 2f 2a 20 57 68 69 63 68  ady     /* Which
2fd3a 20 74 61 62 6c 65 73 20 61 72 65 20 63 75 72 72   tables are curr
2fd3b 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 20  ently available 
2fd3c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6a 2c 20 6b  */.){.  int j, k
2fd3d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2fd3e 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
2fd3f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20  .  int iCur;    
2fd40 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
2fd41 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
2fd42 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
2fd43 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20  t addrNxt;      
2fd44 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 6a     /* Where to j
2fd45 75 6d 70 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  ump to continue 
2fd46 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e  with the next IN
2fd47 20 63 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f   case */.  int o
2fd48 6d 69 74 54 61 62 6c 65 3b 20 20 20 20 20 20 20  mitTable;       
2fd49 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 75 73  /* True if we us
2fd4a 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 6c 79  e the index only
2fd4b 20 2a 2f 0a 20 20 69 6e 74 20 62 52 65 76 3b 20   */.  int bRev; 
2fd4c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
2fd4d 75 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f  ue if we need to
2fd4e 20 73 63 61 6e 20 69 6e 20 72 65 76 65 72 73 65   scan in reverse
2fd4f 20 6f 72 64 65 72 20 2a 2f 0a 20 20 57 68 65 72   order */.  Wher
2fd50 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20  eLevel *pLevel; 
2fd51 20 2f 2a 20 54 68 65 20 77 68 65 72 65 20 6c 65   /* The where le
2fd52 76 65 6c 20 74 6f 20 62 65 20 63 6f 64 65 64 20  vel to be coded 
2fd53 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
2fd54 20 2a 70 57 43 3b 20 20 20 20 2f 2a 20 44 65 63   *pWC;    /* Dec
2fd55 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68  omposition of th
2fd56 65 20 65 6e 74 69 72 65 20 57 48 45 52 45 20 63  e entire WHERE c
2fd57 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65  lause */.  Where
2fd58 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20  Term *pTerm;    
2fd59 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
2fd5a 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
2fd5b 6d 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50  m */.  Parse *pP
2fd5c 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  arse;           
2fd5d 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2fd5e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 56  g context */.  V
2fd5f 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
2fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2fd61 2a 20 54 68 65 20 70 72 65 70 61 72 65 64 20 73  * The prepared s
2fd62 74 6d 74 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tmt under constr
2fd63 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72  uctions */.  str
2fd64 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2fd65 20 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20   *pTabItem;  /* 
2fd66 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
2fd67 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a   being coded */.
2fd68 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b 20 20    int addrBrk;  
2fd69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fd6a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2fd6b 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74  o break out of t
2fd6c 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
2fd6d 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 20   addrCont;      
2fd6e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2fd6f 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e  Jump here to con
2fd70 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20  tinue with next 
2fd71 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  cycle */.  int i
2fd72 52 6f 77 69 64 52 65 67 20 3d 20 30 3b 20 20 20  RowidReg = 0;   
2fd73 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 69 73       /* Rowid is
2fd74 20 73 74 6f 72 65 64 20 69 6e 20 74 68 69 73 20   stored in this 
2fd75 72 65 67 69 73 74 65 72 2c 20 69 66 20 6e 6f 74  register, if not
2fd76 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 69   zero */.  int i
2fd77 52 65 6c 65 61 73 65 52 65 67 20 3d 20 30 3b 20  ReleaseReg = 0; 
2fd78 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67       /* Temp reg
2fd79 69 73 74 65 72 20 74 6f 20 66 72 65 65 20 62 65  ister to free be
2fd7a 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 2a  fore returning *
2fd7b 2f 0a 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57  /..  pParse = pW
2fd7c 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20  Info->pParse;.  
2fd7d 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2fd7e 65 3b 0a 20 20 70 57 43 20 3d 20 70 57 49 6e 66  e;.  pWC = pWInf
2fd7f 6f 2d 3e 70 57 43 3b 0a 20 20 70 4c 65 76 65 6c  o->pWC;.  pLevel
2fd80 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 4c   = &pWInfo->a[iL
2fd81 65 76 65 6c 5d 3b 0a 20 20 70 54 61 62 49 74 65  evel];.  pTabIte
2fd82 6d 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 70 54 61  m = &pWInfo->pTa
2fd83 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d  bList->a[pLevel-
2fd84 3e 69 46 72 6f 6d 5d 3b 0a 20 20 69 43 75 72 20  >iFrom];.  iCur 
2fd85 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72  = pTabItem->iCur
2fd86 73 6f 72 3b 0a 20 20 62 52 65 76 20 3d 20 28 70  sor;.  bRev = (p
2fd87 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2fd88 61 67 73 20 26 20 57 48 45 52 45 5f 52 45 56 45  ags & WHERE_REVE
2fd89 52 53 45 29 21 3d 30 3b 0a 20 20 6f 6d 69 74 54  RSE)!=0;.  omitT
2fd8a 61 62 6c 65 20 3d 20 28 70 4c 65 76 65 6c 2d 3e  able = (pLevel->
2fd8b 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2fd8c 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 21 3d  HERE_IDX_ONLY)!=
2fd8d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20 26 26  0 .           &&
2fd8e 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57   (wctrlFlags & W
2fd8f 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45  HERE_FORCE_TABLE
2fd90 29 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 43 72 65 61  )==0;..  /* Crea
2fd91 74 65 20 6c 61 62 65 6c 73 20 66 6f 72 20 74 68  te labels for th
2fd92 65 20 22 62 72 65 61 6b 22 20 61 6e 64 20 22 63  e "break" and "c
2fd93 6f 6e 74 69 6e 75 65 22 20 69 6e 73 74 72 75 63  ontinue" instruc
2fd94 74 69 6f 6e 73 0a 20 20 2a 2a 20 66 6f 72 20 74  tions.  ** for t
2fd95 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 6f 70 2e  he current loop.
2fd96 20 20 4a 75 6d 70 20 74 6f 20 61 64 64 72 42 72    Jump to addrBr
2fd97 6b 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f  k to break out o
2fd98 66 20 61 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 4a  f a loop..  ** J
2fd99 75 6d 70 20 74 6f 20 63 6f 6e 74 20 74 6f 20 67  ump to cont to g
2fd9a 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  o immediately to
2fd9b 20 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74   the next iterat
2fd9c 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  ion of the.  ** 
2fd9d 6c 6f 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  loop..  **.  ** 
2fd9e 57 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6e  When there is an
2fd9f 20 49 4e 20 6f 70 65 72 61 74 6f 72 2c 20 77 65   IN operator, we
2fda0 20 61 6c 73 6f 20 68 61 76 65 20 61 20 22 61 64   also have a "ad
2fda1 64 72 4e 78 74 22 20 6c 61 62 65 6c 20 74 68 61  drNxt" label tha
2fda2 74 0a 20 20 2a 2a 20 6d 65 61 6e 73 20 74 6f 20  t.  ** means to 
2fda3 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68  continue with th
2fda4 65 20 6e 65 78 74 20 49 4e 20 76 61 6c 75 65 20  e next IN value 
2fda5 63 6f 6d 62 69 6e 61 74 69 6f 6e 2e 20 20 57 68  combination.  Wh
2fda6 65 6e 0a 20 20 2a 2a 20 74 68 65 72 65 20 61 72  en.  ** there ar
2fda7 65 20 6e 6f 20 49 4e 20 6f 70 65 72 61 74 6f 72  e no IN operator
2fda8 73 20 69 6e 20 74 68 65 20 63 6f 6e 73 74 72 61  s in the constra
2fda9 69 6e 74 73 2c 20 74 68 65 20 22 61 64 64 72 4e  ints, the "addrN
2fdaa 78 74 22 20 6c 61 62 65 6c 0a 20 20 2a 2a 20 69  xt" label.  ** i
2fdab 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 22 61  s the same as "a
2fdac 64 64 72 42 72 6b 22 2e 0a 20 20 2a 2f 0a 20 20  ddrBrk"..  */.  
2fdad 61 64 64 72 42 72 6b 20 3d 20 70 4c 65 76 65 6c  addrBrk = pLevel
2fdae 2d 3e 61 64 64 72 42 72 6b 20 3d 20 70 4c 65 76  ->addrBrk = pLev
2fdaf 65 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20 73 71  el->addrNxt = sq
2fdb0 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2fdb1 65 6c 28 76 29 3b 0a 20 20 61 64 64 72 43 6f 6e  el(v);.  addrCon
2fdb2 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72  t = pLevel->addr
2fdb3 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Cont = sqlite3Vd
2fdb4 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2fdb5 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
2fdb6 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65   the right table
2fdb7 20 6f 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52   of a LEFT OUTER
2fdb8 20 4a 4f 49 4e 2c 20 61 6c 6c 6f 63 61 74 65 20   JOIN, allocate 
2fdb9 61 6e 64 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c  and.  ** initial
2fdba 69 7a 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c  ize a memory cel
2fdbb 6c 20 74 68 61 74 20 72 65 63 6f 72 64 73 20 69  l that records i
2fdbc 66 20 74 68 69 73 20 74 61 62 6c 65 20 6d 61 74  f this table mat
2fdbd 63 68 65 73 20 61 6e 79 0a 20 20 2a 2a 20 72 6f  ches any.  ** ro
2fdbe 77 20 6f 66 20 74 68 65 20 6c 65 66 74 20 74 61  w of the left ta
2fdbf 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e  ble of the join.
2fdc0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 65 76  .  */.  if( pLev
2fdc1 65 6c 2d 3e 69 46 72 6f 6d 3e 30 20 26 26 20 28  el->iFrom>0 && (
2fdc2 70 54 61 62 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e  pTabItem[0].join
2fdc3 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21  type & JT_LEFT)!
2fdc4 3d 30 20 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c  =0 ){.    pLevel
2fdc5 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 3d 20 2b 2b  ->iLeftJoin = ++
2fdc6 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2fdc7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2fdc8 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2fdc9 72 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c  r, 0, pLevel->iL
2fdca 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 56 64  eftJoin);.    Vd
2fdcb 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69  beComment((v, "i
2fdcc 6e 69 74 20 4c 45 46 54 20 4a 4f 49 4e 20 6e 6f  nit LEFT JOIN no
2fdcd 2d 6d 61 74 63 68 20 66 6c 61 67 22 29 29 3b 0a  -match flag"));.
2fdce 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
2fdcf 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2fdd0 54 41 42 4c 45 0a 20 20 69 66 28 20 20 28 70 4c  TABLE.  if(  (pL
2fdd1 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2fdd2 67 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55  gs & WHERE_VIRTU
2fdd3 41 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20  ALTABLE)!=0 ){. 
2fdd4 20 20 20 2f 2a 20 43 61 73 65 20 30 3a 20 20 54     /* Case 0:  T
2fdd5 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69  he table is a vi
2fdd6 72 74 75 61 6c 2d 74 61 62 6c 65 2e 20 20 55 73  rtual-table.  Us
2fdd7 65 20 74 68 65 20 56 46 69 6c 74 65 72 20 61 6e  e the VFilter an
2fdd8 64 20 56 4e 65 78 74 0a 20 20 20 20 2a 2a 20 20  d VNext.    **  
2fdd9 20 20 20 20 20 20 20 20 74 6f 20 61 63 63 65 73          to acces
2fdda 73 20 74 68 65 20 64 61 74 61 2e 0a 20 20 20 20  s the data..    
2fddb 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b  */.    int iReg;
2fddc 20 20 20 2f 2a 20 50 33 20 56 61 6c 75 65 20 66     /* P3 Value f
2fddd 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20 2a 2f  or OP_VFilter */
2fdde 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64  .    sqlite3_ind
2fddf 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64  ex_info *pVtabId
2fde0 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  x = pLevel->plan
2fde1 2e 75 2e 70 56 74 61 62 49 64 78 3b 0a 20 20 20  .u.pVtabIdx;.   
2fde2 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74   int nConstraint
2fde3 20 3d 20 70 56 74 61 62 49 64 78 2d 3e 6e 43 6f   = pVtabIdx->nCo
2fde4 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 73 74  nstraint;.    st
2fde5 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2fde6 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73  ex_constraint_us
2fde7 61 67 65 20 2a 61 55 73 61 67 65 20 3d 0a 20 20  age *aUsage =.  
2fde8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fde9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fdea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56                pV
2fdeb 74 61 62 49 64 78 2d 3e 61 43 6f 6e 73 74 72 61  tabIdx->aConstra
2fdec 69 6e 74 55 73 61 67 65 3b 0a 20 20 20 20 63 6f  intUsage;.    co
2fded 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74  nst struct sqlit
2fdee 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2fdef 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74  int *aConstraint
2fdf0 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   =.             
2fdf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fdf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fdf3 20 20 20 70 56 74 61 62 49 64 78 2d 3e 61 43 6f     pVtabIdx->aCo
2fdf4 6e 73 74 72 61 69 6e 74 3b 0a 0a 20 20 20 20 69  nstraint;..    i
2fdf5 52 65 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74  Reg = sqlite3Get
2fdf6 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
2fdf7 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29  , nConstraint+2)
2fdf8 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a  ;.    for(j=1; j
2fdf9 3c 3d 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a  <=nConstraint; j
2fdfa 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6b  ++){.      for(k
2fdfb 3d 30 3b 20 6b 3c 6e 43 6f 6e 73 74 72 61 69 6e  =0; k<nConstrain
2fdfc 74 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20  t; k++){.       
2fdfd 20 69 66 28 20 61 55 73 61 67 65 5b 6b 5d 2e 61   if( aUsage[k].a
2fdfe 72 67 76 49 6e 64 65 78 3d 3d 6a 20 29 7b 0a 20  rgvIndex==j ){. 
2fdff 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 54 65           int iTe
2fe00 72 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74  rm = aConstraint
2fe01 5b 6b 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b  [k].iTermOffset;
2fe02 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2fe03 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2fe04 65 2c 20 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d  e, pWC->a[iTerm]
2fe05 2e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20  .pExpr->pRight, 
2fe06 69 52 65 67 2b 6a 2b 31 29 3b 0a 20 20 20 20 20  iReg+j+1);.     
2fe07 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2fe08 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2fe09 20 20 20 20 69 66 28 20 6b 3d 3d 6e 43 6f 6e 73      if( k==nCons
2fe0a 74 72 61 69 6e 74 20 29 20 62 72 65 61 6b 3b 0a  traint ) break;.
2fe0b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2fe0c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2fe0d 50 5f 49 6e 74 65 67 65 72 2c 20 70 56 74 61 62  P_Integer, pVtab
2fe0e 49 64 78 2d 3e 69 64 78 4e 75 6d 2c 20 69 52 65  Idx->idxNum, iRe
2fe0f 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  g);.    sqlite3V
2fe10 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2fe11 49 6e 74 65 67 65 72 2c 20 6a 2d 31 2c 20 69 52  Integer, j-1, iR
2fe12 65 67 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  eg+1);.    sqlit
2fe13 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2fe14 4f 50 5f 56 46 69 6c 74 65 72 2c 20 69 43 75 72  OP_VFilter, iCur
2fe15 2c 20 61 64 64 72 42 72 6b 2c 20 69 52 65 67 2c  , addrBrk, iReg,
2fe16 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78 53 74   pVtabIdx->idxSt
2fe17 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
2fe18 20 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64           pVtabId
2fe19 78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  x->needToFreeIdx
2fe1a 53 74 72 20 3f 20 50 34 5f 4d 50 52 49 4e 54 46  Str ? P4_MPRINTF
2fe1b 20 3a 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20   : P4_STATIC);. 
2fe1c 20 20 20 70 56 74 61 62 49 64 78 2d 3e 6e 65 65     pVtabIdx->nee
2fe1d 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d 20  dToFreeIdxStr = 
2fe1e 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  0;.    for(j=0; 
2fe1f 6a 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a  j<nConstraint; j
2fe20 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61  ++){.      if( a
2fe21 55 73 61 67 65 5b 6a 5d 2e 6f 6d 69 74 20 29 7b  Usage[j].omit ){
2fe22 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 54 65  .        int iTe
2fe23 72 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74  rm = aConstraint
2fe24 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b  [j].iTermOffset;
2fe25 0a 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65  .        disable
2fe26 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 26 70 57  Term(pLevel, &pW
2fe27 43 2d 3e 61 5b 69 54 65 72 6d 5d 29 3b 0a 20 20  C->a[iTerm]);.  
2fe28 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2fe29 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f  pLevel->op = OP_
2fe2a 56 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76 65  VNext;.    pLeve
2fe2b 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20  l->p1 = iCur;.  
2fe2c 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73    pLevel->p2 = s
2fe2d 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2fe2e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71  tAddr(v);.    sq
2fe2f 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2fe30 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 52  Range(pParse, iR
2fe31 65 67 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b  eg, nConstraint+
2fe32 32 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  2);.  }else.#end
2fe33 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2fe34 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
2fe35 2f 0a 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  /..  if( pLevel-
2fe36 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2fe37 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29  WHERE_ROWID_EQ )
2fe38 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a  {.    /* Case 1:
2fe39 20 20 57 65 20 63 61 6e 20 64 69 72 65 63 74 6c    We can directl
2fe3a 79 20 72 65 66 65 72 65 6e 63 65 20 61 20 73 69  y reference a si
2fe3b 6e 67 6c 65 20 72 6f 77 20 75 73 69 6e 67 20 61  ngle row using a
2fe3c 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  n.    **        
2fe3d 20 20 65 71 75 61 6c 69 74 79 20 63 6f 6d 70 61    equality compa
2fe3e 72 69 73 6f 6e 20 61 67 61 69 6e 73 74 20 74 68  rison against th
2fe3f 65 20 52 4f 57 49 44 20 66 69 65 6c 64 2e 20 20  e ROWID field.  
2fe40 4f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  Or.    **       
2fe41 20 20 20 77 65 20 72 65 66 65 72 65 6e 63 65 20     we reference 
2fe42 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 75 73  multiple rows us
2fe43 69 6e 67 20 61 20 22 72 6f 77 69 64 20 49 4e 20  ing a "rowid IN 
2fe44 28 2e 2e 2e 29 22 0a 20 20 20 20 2a 2a 20 20 20  (...)".    **   
2fe45 20 20 20 20 20 20 20 63 6f 6e 73 74 72 75 63 74         construct
2fe46 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 52 65  ..    */.    iRe
2fe47 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74  leaseReg = sqlit
2fe48 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2fe49 72 73 65 29 3b 0a 20 20 20 20 70 54 65 72 6d 20  rse);.    pTerm 
2fe4a 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
2fe4b 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61  iCur, -1, notRea
2fe4c 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c  dy, WO_EQ|WO_IN,
2fe4d 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   0);.    assert(
2fe4e 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20   pTerm!=0 );.   
2fe4f 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e   assert( pTerm->
2fe50 70 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20  pExpr!=0 );.    
2fe51 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 6c  assert( pTerm->l
2fe52 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20  eftCursor==iCur 
2fe53 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f  );.    assert( o
2fe54 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  mitTable==0 );. 
2fe55 20 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20 63     iRowidReg = c
2fe56 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28  odeEqualityTerm(
2fe57 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70  pParse, pTerm, p
2fe58 4c 65 76 65 6c 2c 20 69 52 65 6c 65 61 73 65 52  Level, iReleaseR
2fe59 65 67 29 3b 0a 20 20 20 20 61 64 64 72 4e 78 74  eg);.    addrNxt
2fe5a 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e   = pLevel->addrN
2fe5b 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  xt;.    sqlite3V
2fe5c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2fe5d 4d 75 73 74 42 65 49 6e 74 2c 20 69 52 6f 77 69  MustBeInt, iRowi
2fe5e 64 52 65 67 2c 20 61 64 64 72 4e 78 74 29 3b 0a  dReg, addrNxt);.
2fe5f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2fe60 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
2fe61 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64  xists, iCur, add
2fe62 72 4e 78 74 2c 20 69 52 6f 77 69 64 52 65 67 29  rNxt, iRowidReg)
2fe63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2fe64 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 72  rCacheStore(pPar
2fe65 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52  se, iCur, -1, iR
2fe66 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 56 64  owidReg);.    Vd
2fe67 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 70  beComment((v, "p
2fe68 6b 22 29 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c  k"));.    pLevel
2fe69 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a  ->op = OP_Noop;.
2fe6a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76    }else if( pLev
2fe6b 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2fe6c 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52   & WHERE_ROWID_R
2fe6d 41 4e 47 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43  ANGE ){.    /* C
2fe6e 61 73 65 20 32 3a 20 20 57 65 20 68 61 76 65 20  ase 2:  We have 
2fe6f 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  an inequality co
2fe70 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73 74  mparison against
2fe71 20 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c 64   the ROWID field
2fe72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
2fe73 20 74 65 73 74 4f 70 20 3d 20 4f 50 5f 4e 6f 6f   testOp = OP_Noo
2fe74 70 3b 0a 20 20 20 20 69 6e 74 20 73 74 61 72 74  p;.    int start
2fe75 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 45 6e 64  ;.    int memEnd
2fe76 56 61 6c 75 65 20 3d 20 30 3b 0a 20 20 20 20 57  Value = 0;.    W
2fe77 68 65 72 65 54 65 72 6d 20 2a 70 53 74 61 72 74  hereTerm *pStart
2fe78 2c 20 2a 70 45 6e 64 3b 0a 0a 20 20 20 20 61 73  , *pEnd;..    as
2fe79 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d  sert( omitTable=
2fe7a 3d 30 20 29 3b 0a 20 20 20 20 70 53 74 61 72 74  =0 );.    pStart
2fe7b 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c   = findTerm(pWC,
2fe7c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65   iCur, -1, notRe
2fe7d 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45  ady, WO_GT|WO_GE
2fe7e 2c 20 30 29 3b 0a 20 20 20 20 70 45 6e 64 20 3d  , 0);.    pEnd =
2fe7f 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2fe80 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64  Cur, -1, notRead
2fe81 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20  y, WO_LT|WO_LE, 
2fe82 30 29 3b 0a 20 20 20 20 69 66 28 20 62 52 65 76  0);.    if( bRev
2fe83 20 29 7b 0a 20 20 20 20 20 20 70 54 65 72 6d 20   ){.      pTerm 
2fe84 3d 20 70 53 74 61 72 74 3b 0a 20 20 20 20 20 20  = pStart;.      
2fe85 70 53 74 61 72 74 20 3d 20 70 45 6e 64 3b 0a 20  pStart = pEnd;. 
2fe86 20 20 20 20 20 70 45 6e 64 20 3d 20 70 54 65 72       pEnd = pTer
2fe87 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  m;.    }.    if(
2fe88 20 70 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20   pStart ){.     
2fe89 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20   Expr *pX;      
2fe8a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
2fe8b 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 64 65  pression that de
2fe8c 66 69 6e 65 73 20 74 68 65 20 73 74 61 72 74 20  fines the start 
2fe8d 62 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69  bound */.      i
2fe8e 6e 74 20 72 31 2c 20 72 54 65 6d 70 3b 20 20 20  nt r1, rTemp;   
2fe8f 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
2fe90 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68  s for holding th
2fe91 65 20 73 74 61 72 74 20 62 6f 75 6e 64 61 72 79  e start boundary
2fe92 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68   */..      /* Th
2fe93 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73  e following cons
2fe94 74 61 6e 74 20 6d 61 70 73 20 54 4b 5f 78 78 20  tant maps TK_xx 
2fe95 63 6f 64 65 73 20 69 6e 74 6f 20 63 6f 72 72 65  codes into corre
2fe96 73 70 6f 6e 64 69 6e 67 20 0a 20 20 20 20 20 20  sponding .      
2fe97 2a 2a 20 73 65 65 6b 20 6f 70 63 6f 64 65 73 2e  ** seek opcodes.
2fe98 20 20 49 74 20 64 65 70 65 6e 64 73 20 6f 6e 20    It depends on 
2fe99 61 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64  a particular ord
2fe9a 65 72 69 6e 67 20 6f 66 20 54 4b 5f 78 78 0a 20  ering of TK_xx. 
2fe9b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f       */.      co
2fe9c 6e 73 74 20 75 38 20 61 4d 6f 76 65 4f 70 5b 5d  nst u8 aMoveOp[]
2fe9d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20   = {.           
2fe9e 2f 2a 20 54 4b 5f 47 54 20 2a 2f 20 20 4f 50 5f  /* TK_GT */  OP_
2fe9f 53 65 65 6b 47 74 2c 0a 20 20 20 20 20 20 20 20  SeekGt,.        
2fea0 20 20 20 2f 2a 20 54 4b 5f 4c 45 20 2a 2f 20 20     /* TK_LE */  
2fea1 4f 50 5f 53 65 65 6b 4c 65 2c 0a 20 20 20 20 20  OP_SeekLe,.     
2fea2 20 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 54 20 2a        /* TK_LT *
2fea3 2f 20 20 4f 50 5f 53 65 65 6b 4c 74 2c 0a 20 20  /  OP_SeekLt,.  
2fea4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 47           /* TK_G
2fea5 45 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 47 65 0a  E */  OP_SeekGe.
2fea6 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 61        };.      a
2fea7 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 54 4b  ssert( TK_LE==TK
2fea8 5f 47 54 2b 31 20 29 3b 20 20 20 20 20 20 2f 2a  _GT+1 );      /*
2fea9 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f   Make sure the o
2feaa 72 64 65 72 69 6e 67 2e 2e 20 2a 2f 0a 20 20 20  rdering.. */.   
2feab 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54     assert( TK_LT
2feac 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 20 20 20 20  ==TK_GT+2 );    
2fead 20 20 2f 2a 20 20 2e 2e 2e 20 6f 66 20 74 68 65    /*  ... of the
2feae 20 54 4b 5f 78 78 20 76 61 6c 75 65 73 2e 2e 2e   TK_xx values...
2feaf 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
2feb0 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 47 54 2b 33  ( TK_GE==TK_GT+3
2feb1 20 29 3b 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e   );      /*  ...
2feb2 20 69 73 20 63 6f 72 72 65 63 63 74 2e 20 2a 2f   is correcct. */
2feb3 0a 0a 20 20 20 20 20 20 70 58 20 3d 20 70 53 74  ..      pX = pSt
2feb4 61 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  art->pExpr;.    
2feb5 20 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 20    assert( pX!=0 
2feb6 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2feb7 20 70 53 74 61 72 74 2d 3e 6c 65 66 74 43 75 72   pStart->leftCur
2feb8 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20 20 20  sor==iCur );.   
2feb9 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
2feba 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
2febb 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20  se, pX->pRight, 
2febc 26 72 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73  &rTemp);.      s
2febd 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2febe 28 76 2c 20 61 4d 6f 76 65 4f 70 5b 70 58 2d 3e  (v, aMoveOp[pX->
2febf 6f 70 2d 54 4b 5f 47 54 5d 2c 20 69 43 75 72 2c  op-TK_GT], iCur,
2fec0 20 61 64 64 72 42 72 6b 2c 20 72 31 29 3b 0a 20   addrBrk, r1);. 
2fec1 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2fec2 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20  ((v, "pk"));.   
2fec3 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2fec4 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
2fec5 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 31 29  e(pParse, r1, 1)
2fec6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
2fec7 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2fec8 61 72 73 65 2c 20 72 54 65 6d 70 29 3b 0a 20 20  arse, rTemp);.  
2fec9 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28      disableTerm(
2feca 70 4c 65 76 65 6c 2c 20 70 53 74 61 72 74 29 3b  pLevel, pStart);
2fecb 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2fecc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2fecd 4f 70 32 28 76 2c 20 62 52 65 76 20 3f 20 4f 50  Op2(v, bRev ? OP
2fece 5f 4c 61 73 74 20 3a 20 4f 50 5f 52 65 77 69 6e  _Last : OP_Rewin
2fecf 64 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b  d, iCur, addrBrk
2fed0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2fed1 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 45   pEnd ){.      E
2fed2 78 70 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 70  xpr *pX;.      p
2fed3 58 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70 72 3b  X = pEnd->pExpr;
2fed4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2fed5 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73  X!=0 );.      as
2fed6 73 65 72 74 28 20 70 45 6e 64 2d 3e 6c 65 66 74  sert( pEnd->left
2fed7 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a  Cursor==iCur );.
2fed8 20 20 20 20 20 20 6d 65 6d 45 6e 64 56 61 6c 75        memEndValu
2fed9 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  e = ++pParse->nM
2feda 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  em;.      sqlite
2fedb 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
2fedc 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 6d 65  , pX->pRight, me
2fedd 6d 45 6e 64 56 61 6c 75 65 29 3b 0a 20 20 20 20  mEndValue);.    
2fede 20 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b    if( pX->op==TK
2fedf 5f 4c 54 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54  _LT || pX->op==T
2fee0 4b 5f 47 54 20 29 7b 0a 20 20 20 20 20 20 20 20  K_GT ){.        
2fee1 74 65 73 74 4f 70 20 3d 20 62 52 65 76 20 3f 20  testOp = bRev ? 
2fee2 4f 50 5f 4c 65 20 3a 20 4f 50 5f 47 65 3b 0a 20  OP_Le : OP_Ge;. 
2fee3 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2fee4 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65      testOp = bRe
2fee5 76 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47  v ? OP_Lt : OP_G
2fee6 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
2fee7 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2fee8 76 65 6c 2c 20 70 45 6e 64 29 3b 0a 20 20 20 20  vel, pEnd);.    
2fee9 7d 0a 20 20 20 20 73 74 61 72 74 20 3d 20 73 71  }.    start = sq
2feea 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2feeb 41 64 64 72 28 76 29 3b 0a 20 20 20 20 70 4c 65  Addr(v);.    pLe
2feec 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f  vel->op = bRev ?
2feed 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65   OP_Prev : OP_Ne
2feee 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  xt;.    pLevel->
2feef 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70  p1 = iCur;.    p
2fef0 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72  Level->p2 = star
2fef1 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  t;.    pLevel->p
2fef2 35 20 3d 20 28 70 53 74 61 72 74 3d 3d 30 20 26  5 = (pStart==0 &
2fef3 26 20 70 45 6e 64 3d 3d 30 29 20 3f 31 3a 30 3b  & pEnd==0) ?1:0;
2fef4 0a 20 20 20 20 69 66 28 20 74 65 73 74 4f 70 21  .    if( testOp!
2fef5 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20  =OP_Noop ){.    
2fef6 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20 69 52    iRowidReg = iR
2fef7 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69  eleaseReg = sqli
2fef8 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2fef9 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  arse);.      sql
2fefa 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2fefb 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72  , OP_Rowid, iCur
2fefc 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20  , iRowidReg);.  
2fefd 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2fefe 61 63 68 65 53 74 6f 72 65 28 70 50 61 72 73 65  acheStore(pParse
2feff 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77  , iCur, -1, iRow
2ff00 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71  idReg);.      sq
2ff01 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2ff02 76 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d 45 6e  v, testOp, memEn
2ff03 64 56 61 6c 75 65 2c 20 61 64 64 72 42 72 6b 2c  dValue, addrBrk,
2ff04 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20   iRowidReg);.   
2ff05 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2ff06 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45  angeP5(v, SQLITE
2ff07 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 20 53  _AFF_NUMERIC | S
2ff08 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
2ff09 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
2ff0a 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61   if( pLevel->pla
2ff0b 6e 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45  n.wsFlags & (WHE
2ff0c 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c  RE_COLUMN_RANGE|
2ff0d 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 29  WHERE_COLUMN_EQ)
2ff0e 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20   ){.    /* Case 
2ff0f 33 3a 20 41 20 73 63 61 6e 20 75 73 69 6e 67 20  3: A scan using 
2ff10 61 6e 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a  an index..    **
2ff11 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2ff12 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2ff13 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 7a 65 72   may contain zer
2ff14 6f 20 6f 72 20 6d 6f 72 65 20 65 71 75 61 6c 69  o or more equali
2ff15 74 79 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ty .    **      
2ff16 20 20 20 74 65 72 6d 73 20 28 22 3d 3d 22 20 6f     terms ("==" o
2ff17 72 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73  r "IN" operators
2ff18 29 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20  ) that refer to 
2ff19 74 68 65 20 4e 0a 20 20 20 20 2a 2a 20 20 20 20  the N.    **    
2ff1a 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20 63       left-most c
2ff1b 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 69 6e  olumns of the in
2ff1c 64 65 78 2e 20 49 74 20 6d 61 79 20 61 6c 73 6f  dex. It may also
2ff1d 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20   contain.    ** 
2ff1e 20 20 20 20 20 20 20 20 69 6e 65 71 75 61 6c 69          inequali
2ff1f 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 28  ty constraints (
2ff20 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d 29 20  >, <, >= or <=) 
2ff21 6f 6e 20 74 68 65 20 69 6e 64 65 78 65 64 0a 20  on the indexed. 
2ff22 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6f     **         co
2ff23 6c 75 6d 6e 20 74 68 61 74 20 69 6d 6d 65 64 69  lumn that immedi
2ff24 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68  ately follows th
2ff25 65 20 4e 20 65 71 75 61 6c 69 74 69 65 73 2e 20  e N equalities. 
2ff26 4f 6e 6c 79 20 0a 20 20 20 20 2a 2a 20 20 20 20  Only .    **    
2ff27 20 20 20 20 20 74 68 65 20 72 69 67 68 74 2d 6d       the right-m
2ff28 6f 73 74 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62  ost column can b
2ff29 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 20  e an inequality 
2ff2a 2d 20 74 68 65 20 72 65 73 74 20 6d 75 73 74 0a  - the rest must.
2ff2b 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 75      **         u
2ff2c 73 65 20 74 68 65 20 22 3d 3d 22 20 61 6e 64 20  se the "==" and 
2ff2d 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 2e 20  "IN" operators. 
2ff2e 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
2ff2f 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20  the .    **     
2ff30 20 20 20 20 69 6e 64 65 78 20 69 73 20 6f 6e 20      index is on 
2ff31 28 78 2c 79 2c 7a 29 2c 20 74 68 65 6e 20 74 68  (x,y,z), then th
2ff32 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6c 61 75  e following clau
2ff33 73 65 73 20 61 72 65 20 61 6c 6c 20 0a 20 20 20  ses are all .   
2ff34 20 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 74 69   **         opti
2ff35 6d 69 7a 65 64 3a 0a 20 20 20 20 2a 2a 0a 20 20  mized:.    **.  
2ff36 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
2ff37 78 3d 35 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  x=5.    **      
2ff38 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3d        x=5 AND y=
2ff39 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  10.    **       
2ff3a 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3c 31       x=5 AND y<1
2ff3b 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  0.    **        
2ff3c 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3e 35 20      x=5 AND y>5 
2ff3d 41 4e 44 20 79 3c 31 30 0a 20 20 20 20 2a 2a 20  AND y<10.    ** 
2ff3e 20 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41             x=5 A
2ff3f 4e 44 20 79 3d 35 20 41 4e 44 20 7a 3c 3d 31 30  ND y=5 AND z<=10
2ff40 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2ff41 20 20 20 20 20 20 20 54 68 65 20 7a 3c 31 30 20         The z<10 
2ff42 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 6c 6c  term of the foll
2ff43 6f 77 69 6e 67 20 63 61 6e 6e 6f 74 20 62 65 20  owing cannot be 
2ff44 75 73 65 64 2c 20 6f 6e 6c 79 0a 20 20 20 20 2a  used, only.    *
2ff45 2a 20 20 20 20 20 20 20 20 20 74 68 65 20 78 3d  *         the x=
2ff46 35 20 74 65 72 6d 3a 0a 20 20 20 20 2a 2a 0a 20  5 term:.    **. 
2ff47 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
2ff48 20 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a 20 20   x=5 AND z<10.  
2ff49 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2ff4a 20 20 20 20 4e 20 6d 61 79 20 62 65 20 7a 65 72      N may be zer
2ff4b 6f 20 69 66 20 74 68 65 72 65 20 61 72 65 20 69  o if there are i
2ff4c 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
2ff4d 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 20 20  aints..    **   
2ff4e 20 20 20 20 20 20 49 66 20 74 68 65 72 65 20 61        If there a
2ff4f 72 65 20 6e 6f 20 69 6e 65 71 75 61 6c 69 74 79  re no inequality
2ff50 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68   constraints, th
2ff51 65 6e 20 4e 20 69 73 20 61 74 0a 20 20 20 20 2a  en N is at.    *
2ff52 2a 20 20 20 20 20 20 20 20 20 6c 65 61 73 74 20  *         least 
2ff53 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  one..    **.    
2ff54 2a 2a 20 20 20 20 20 20 20 20 20 54 68 69 73 20  **         This 
2ff55 63 61 73 65 20 69 73 20 61 6c 73 6f 20 75 73 65  case is also use
2ff56 64 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65  d when there are
2ff57 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65   no WHERE clause
2ff58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2ff59 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 75 74 20  constraints but 
2ff5a 61 6e 20 69 6e 64 65 78 20 69 73 20 73 65 6c 65  an index is sele
2ff5b 63 74 65 64 20 61 6e 79 77 61 79 2c 20 69 6e 20  cted anyway, in 
2ff5c 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 20 20 20  order.    **    
2ff5d 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20 74 68       to force th
2ff5e 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72 20 74  e output order t
2ff5f 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61 6e 20  o conform to an 
2ff60 4f 52 44 45 52 20 42 59 2e 0a 20 20 20 20 2a 2f  ORDER BY..    */
2ff61 20 20 0a 20 20 20 20 69 6e 74 20 61 53 74 61 72    .    int aStar
2ff62 74 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  tOp[] = {.      
2ff63 30 2c 0a 20 20 20 20 20 20 30 2c 0a 20 20 20 20  0,.      0,.    
2ff64 20 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20    OP_Rewind,    
2ff65 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 28 21 73         /* 2: (!s
2ff66 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
2ff67 20 26 26 20 73 74 61 72 74 45 71 20 26 26 20 20   && startEq &&  
2ff68 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20  !bRev) */.      
2ff69 4f 50 5f 4c 61 73 74 2c 20 20 20 20 20 20 20 20  OP_Last,        
2ff6a 20 20 20 20 20 2f 2a 20 33 3a 20 28 21 73 74 61       /* 3: (!sta
2ff6b 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26  rt_constraints &
2ff6c 26 20 73 74 61 72 74 45 71 20 26 26 20 20 20 62  & startEq &&   b
2ff6d 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  Rev) */.      OP
2ff6e 5f 53 65 65 6b 47 74 2c 20 20 20 20 20 20 20 20  _SeekGt,        
2ff6f 20 20 20 2f 2a 20 34 3a 20 28 73 74 61 72 74 5f     /* 4: (start_
2ff70 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20  constraints  && 
2ff71 21 73 74 61 72 74 45 71 20 26 26 20 21 62 52 65  !startEq && !bRe
2ff72 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53  v) */.      OP_S
2ff73 65 65 6b 4c 74 2c 20 20 20 20 20 20 20 20 20 20  eekLt,          
2ff74 20 2f 2a 20 35 3a 20 28 73 74 61 72 74 5f 63 6f   /* 5: (start_co
2ff75 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 21 73  nstraints  && !s
2ff76 74 61 72 74 45 71 20 26 26 20 20 62 52 65 76 29  tartEq &&  bRev)
2ff77 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65   */.      OP_See
2ff78 6b 47 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  kGe,           /
2ff79 2a 20 36 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73  * 6: (start_cons
2ff7a 74 72 61 69 6e 74 73 20 20 26 26 20 20 73 74 61  traints  &&  sta
2ff7b 72 74 45 71 20 26 26 20 21 62 52 65 76 29 20 2a  rtEq && !bRev) *
2ff7c 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 4c  /.      OP_SeekL
2ff7d 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e            /* 
2ff7e 37 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72  7: (start_constr
2ff7f 61 69 6e 74 73 20 20 26 26 20 20 73 74 61 72 74  aints  &&  start
2ff80 45 71 20 26 26 20 20 62 52 65 76 29 20 2a 2f 0a  Eq &&  bRev) */.
2ff81 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 61      };.    int a
2ff82 45 6e 64 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20  EndOp[] = {.    
2ff83 20 20 4f 50 5f 4e 6f 6f 70 2c 20 20 20 20 20 20    OP_Noop,      
2ff84 20 20 20 20 20 20 20 2f 2a 20 30 3a 20 28 21 65         /* 0: (!e
2ff85 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 29 20  nd_constraints) 
2ff86 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64 78 47  */.      OP_IdxG
2ff87 45 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  E,            /*
2ff88 20 31 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61   1: (end_constra
2ff89 69 6e 74 73 20 26 26 20 21 62 52 65 76 29 20 2a  ints && !bRev) *
2ff8a 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64 78 4c 54  /.      OP_IdxLT
2ff8b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ff8c 32 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69  2: (end_constrai
2ff8d 6e 74 73 20 26 26 20 62 52 65 76 29 20 2a 2f 0a  nts && bRev) */.
2ff8e 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6e      };.    int n
2ff8f 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  Eq = pLevel->pla
2ff90 6e 2e 6e 45 71 3b 0a 20 20 20 20 69 6e 74 20 69  n.nEq;.    int i
2ff91 73 4d 69 6e 51 75 65 72 79 20 3d 20 30 3b 20 20  sMinQuery = 0;  
2ff92 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
2ff93 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a  is is an optimiz
2ff94 65 64 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29  ed SELECT min(x)
2ff95 2e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65  .. */.    int re
2ff96 67 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20  gBase;          
2ff97 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
2ff98 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
2ff99 63 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c 75 65  constraint value
2ff9a 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 31 3b  s */.    int r1;
2ff9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff9c 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65        /* Temp re
2ff9d 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 57 68  gister */.    Wh
2ff9e 65 72 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 53  ereTerm *pRangeS
2ff9f 74 61 72 74 20 3d 20 30 3b 20 20 2f 2a 20 49 6e  tart = 0;  /* In
2ffa0 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2ffa1 69 6e 74 20 61 74 20 72 61 6e 67 65 20 73 74 61  int at range sta
2ffa2 72 74 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 54  rt */.    WhereT
2ffa3 65 72 6d 20 2a 70 52 61 6e 67 65 45 6e 64 20 3d  erm *pRangeEnd =
2ffa4 20 30 3b 20 20 20 20 2f 2a 20 49 6e 65 71 75 61   0;    /* Inequa
2ffa5 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20  lity constraint 
2ffa6 61 74 20 72 61 6e 67 65 20 65 6e 64 20 2a 2f 0a  at range end */.
2ffa7 20 20 20 20 69 6e 74 20 73 74 61 72 74 45 71 3b      int startEq;
2ffa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ffa9 20 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e 67   /* True if rang
2ffaa 65 20 73 74 61 72 74 20 75 73 65 73 20 3d 3d 2c  e start uses ==,
2ffab 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20   >= or <= */.   
2ffac 20 69 6e 74 20 65 6e 64 45 71 3b 20 20 20 20 20   int endEq;     
2ffad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ffae 20 54 72 75 65 20 69 66 20 72 61 6e 67 65 20 65   True if range e
2ffaf 6e 64 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f  nd uses ==, >= o
2ffb0 72 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e 74 20  r <= */.    int 
2ffb1 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74  start_constraint
2ffb2 73 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72  s;       /* Star
2ffb3 74 20 6f 66 20 72 61 6e 67 65 20 69 73 20 63 6f  t of range is co
2ffb4 6e 73 74 72 61 69 6e 65 64 20 2a 2f 0a 20 20 20  nstrained */.   
2ffb5 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74   int nConstraint
2ffb6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2ffb7 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6e 73 74   Number of const
2ffb8 72 61 69 6e 74 20 74 65 72 6d 73 20 2a 2f 0a 20  raint terms */. 
2ffb9 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20     Index *pIdx; 
2ffba 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
2ffbb 6e 64 65 78 20 77 65 20 77 69 6c 6c 20 62 65 20  ndex we will be 
2ffbc 75 73 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74  using */.    int
2ffbd 20 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20   iIdxCur;       
2ffbe 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75    /* The VDBE cu
2ffbf 72 73 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64  rsor for the ind
2ffc0 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45  ex */.    int nE
2ffc1 78 74 72 61 52 65 67 20 3d 20 30 3b 20 20 20 2f  xtraReg = 0;   /
2ffc2 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72  * Number of extr
2ffc3 61 20 72 65 67 69 73 74 65 72 73 20 6e 65 65 64  a registers need
2ffc4 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70  ed */.    int op
2ffc5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2ffc6 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 6f 70  * Instruction op
2ffc7 63 6f 64 65 20 2a 2f 0a 20 20 20 20 63 68 61 72  code */.    char
2ffc8 20 2a 7a 41 66 66 3b 0a 0a 20 20 20 20 70 49 64   *zAff;..    pId
2ffc9 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  x = pLevel->plan
2ffca 2e 75 2e 70 49 64 78 3b 0a 20 20 20 20 69 49 64  .u.pIdx;.    iId
2ffcb 78 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69  xCur = pLevel->i
2ffcc 49 64 78 43 75 72 3b 0a 20 20 20 20 6b 20 3d 20  IdxCur;.    k = 
2ffcd 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e  pIdx->aiColumn[n
2ffce 45 71 5d 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 75  Eq];     /* Colu
2ffcf 6d 6e 20 66 6f 72 20 69 6e 65 71 75 61 6c 69 74  mn for inequalit
2ffd0 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  y constraints */
2ffd1 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
2ffd2 20 6c 6f 6f 70 20 73 61 74 69 73 66 69 65 73 20   loop satisfies 
2ffd3 61 20 73 6f 72 74 20 6f 72 64 65 72 20 28 70 4f  a sort order (pO
2ffd4 72 64 65 72 42 79 29 20 72 65 71 75 65 73 74 20  rderBy) request 
2ffd5 74 68 61 74 20 0a 20 20 20 20 2a 2a 20 77 61 73  that .    ** was
2ffd6 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
2ffd7 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6d 70 6c  function to impl
2ffd8 65 6d 65 6e 74 20 61 20 22 53 45 4c 45 43 54 20  ement a "SELECT 
2ffd9 6d 69 6e 28 78 29 20 2e 2e 2e 22 20 0a 20 20 20  min(x) ..." .   
2ffda 20 2a 2a 20 71 75 65 72 79 2c 20 74 68 65 6e 20   ** query, then 
2ffdb 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20  the caller will 
2ffdc 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74 68 65 20 6c  only allow the l
2ffdd 6f 6f 70 20 74 6f 20 72 75 6e 20 66 6f 72 0a 20  oop to run for. 
2ffde 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69     ** a single i
2ffdf 74 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 6d  teration. This m
2ffe0 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 69  eans that the fi
2ffe1 72 73 74 20 72 6f 77 20 72 65 74 75 72 6e 65 64  rst row returned
2ffe2 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 6e  .    ** should n
2ffe3 6f 74 20 68 61 76 65 20 61 20 4e 55 4c 4c 20 76  ot have a NULL v
2ffe4 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 27  alue stored in '
2ffe5 78 27 2e 20 49 66 20 63 6f 6c 75 6d 6e 20 27 78  x'. If column 'x
2ffe6 27 20 69 73 0a 20 20 20 20 2a 2a 20 74 68 65 20  ' is.    ** the 
2ffe7 66 69 72 73 74 20 6f 6e 65 20 61 66 74 65 72 20  first one after 
2ffe8 74 68 65 20 6e 45 71 20 65 71 75 61 6c 69 74 79  the nEq equality
2ffe9 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20   constraints in 
2ffea 74 68 65 20 69 6e 64 65 78 2c 0a 20 20 20 20 2a  the index,.    *
2ffeb 2a 20 74 68 69 73 20 72 65 71 75 69 72 65 73 20  * this requires 
2ffec 73 6f 6d 65 20 73 70 65 63 69 61 6c 20 68 61 6e  some special han
2ffed 64 6c 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20  dling..    */.  
2ffee 20 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67    if( (wctrlFlag
2ffef 73 26 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  s&WHERE_ORDERBY_
2fff0 4d 49 4e 29 21 3d 30 0a 20 20 20 20 20 26 26 20  MIN)!=0.     && 
2fff1 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
2fff2 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45  Flags&WHERE_ORDE
2fff3 52 42 59 29 0a 20 20 20 20 20 26 26 20 28 70 49  RBY).     && (pI
2fff4 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 6e 45 71 29  dx->nColumn>nEq)
2fff5 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a  .    ){.      /*
2fff6 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42   assert( pOrderB
2fff7 79 2d 3e 6e 45 78 70 72 3d 3d 31 20 29 3b 20 2a  y->nExpr==1 ); *
2fff8 2f 0a 20 20 20 20 20 20 2f 2a 20 61 73 73 65 72  /.      /* asser
2fff9 74 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30  t( pOrderBy->a[0
2fffa 5d 2e 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ].pExpr->iColumn
2fffb 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ==pIdx->aiColumn
2fffc 5b 6e 45 71 5d 20 29 3b 20 2a 2f 0a 20 20 20 20  [nEq] ); */.    
2fffd 20 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20 31    isMinQuery = 1
2fffe 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61 52 65  ;.      nExtraRe
2ffff 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20  g = 1;.    }..  
30000 20 20 2f 2a 20 46 69 6e 64 20 61 6e 79 20 69 6e    /* Find any in
30001 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
30002 69 6e 74 20 74 65 72 6d 73 20 66 6f 72 20 74 68  int terms for th
30003 65 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64 20  e start and end 
30004 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72  .    ** of the r
30005 61 6e 67 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  ange. .    */.  
30006 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c    if( pLevel->pl
30007 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
30008 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 7b 0a  RE_TOP_LIMIT ){.
30009 20 20 20 20 20 20 70 52 61 6e 67 65 45 6e 64 20        pRangeEnd 
3000a 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
3000b 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64  iCur, k, notRead
3000c 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 29  y, (WO_LT|WO_LE)
3000d 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 6e  , pIdx);.      n
3000e 45 78 74 72 61 52 65 67 20 3d 20 31 3b 0a 20 20  ExtraReg = 1;.  
3000f 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 76    }.    if( pLev
30010 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
30011 20 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d   & WHERE_BTM_LIM
30012 49 54 20 29 7b 0a 20 20 20 20 20 20 70 52 61 6e  IT ){.      pRan
30013 67 65 53 74 61 72 74 20 3d 20 66 69 6e 64 54 65  geStart = findTe
30014 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c  rm(pWC, iCur, k,
30015 20 6e 6f 74 52 65 61 64 79 2c 20 28 57 4f 5f 47   notReady, (WO_G
30016 54 7c 57 4f 5f 47 45 29 2c 20 70 49 64 78 29 3b  T|WO_GE), pIdx);
30017 0a 20 20 20 20 20 20 6e 45 78 74 72 61 52 65 67  .      nExtraReg
30018 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 1;.    }..   
30019 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
3001a 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6c  e to evaluate al
3001b 6c 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72  l constraint ter
3001c 6d 73 20 75 73 69 6e 67 20 3d 3d 20 6f 72 20 49  ms using == or I
3001d 4e 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f  N.    ** and sto
3001e 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  re the values of
3001f 20 74 68 6f 73 65 20 74 65 72 6d 73 20 69 6e 20   those terms in 
30020 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67 69  an array of regi
30021 73 74 65 72 73 0a 20 20 20 20 2a 2a 20 73 74 61  sters.    ** sta
30022 72 74 69 6e 67 20 61 74 20 72 65 67 42 61 73 65  rting at regBase
30023 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67  ..    */.    reg
30024 42 61 73 65 20 3d 20 63 6f 64 65 41 6c 6c 45 71  Base = codeAllEq
30025 75 61 6c 69 74 79 54 65 72 6d 73 28 0a 20 20 20  ualityTerms(.   
30026 20 20 20 20 20 70 50 61 72 73 65 2c 20 70 4c 65       pParse, pLe
30027 76 65 6c 2c 20 70 57 43 2c 20 6e 6f 74 52 65 61  vel, pWC, notRea
30028 64 79 2c 20 6e 45 78 74 72 61 52 65 67 2c 20 26  dy, nExtraReg, &
30029 7a 41 66 66 0a 20 20 20 20 29 3b 0a 20 20 20 20  zAff.    );.    
3002a 61 64 64 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c  addrNxt = pLevel
3002b 2d 3e 61 64 64 72 4e 78 74 3b 0a 0a 20 20 20 20  ->addrNxt;..    
3002c 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69  /* If we are doi
3002d 6e 67 20 61 20 72 65 76 65 72 73 65 20 6f 72 64  ng a reverse ord
3002e 65 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20 61 73  er scan on an as
3002f 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20 6f  cending index, o
30030 72 0a 20 20 20 20 2a 2a 20 61 20 66 6f 72 77 61  r.    ** a forwa
30031 72 64 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e  rd order scan on
30032 20 61 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e   a descending in
30033 64 65 78 2c 20 69 6e 74 65 72 63 68 61 6e 67 65  dex, interchange
30034 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 74 61   the .    ** sta
30035 72 74 20 61 6e 64 20 65 6e 64 20 74 65 72 6d 73  rt and end terms
30036 20 28 70 52 61 6e 67 65 53 74 61 72 74 20 61 6e   (pRangeStart an
30037 64 20 70 52 61 6e 67 65 45 6e 64 29 2e 0a 20 20  d pRangeEnd)..  
30038 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 52 65    */.    if( bRe
30039 76 3d 3d 28 70 49 64 78 2d 3e 61 53 6f 72 74 4f  v==(pIdx->aSortO
3003a 72 64 65 72 5b 6e 45 71 5d 3d 3d 53 51 4c 49 54  rder[nEq]==SQLIT
3003b 45 5f 53 4f 5f 41 53 43 29 20 29 7b 0a 20 20 20  E_SO_ASC) ){.   
3003c 20 20 20 53 57 41 50 28 57 68 65 72 65 54 65 72     SWAP(WhereTer
3003d 6d 20 2a 2c 20 70 52 61 6e 67 65 45 6e 64 2c 20  m *, pRangeEnd, 
3003e 70 52 61 6e 67 65 53 74 61 72 74 29 3b 0a 20 20  pRangeStart);.  
3003f 20 20 7d 0a 0a 20 20 20 20 74 65 73 74 63 61 73    }..    testcas
30040 65 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26  e( pRangeStart &
30041 26 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65  & pRangeStart->e
30042 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45  Operator & WO_LE
30043 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
30044 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26  ( pRangeStart &&
30045 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f   pRangeStart->eO
30046 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20  perator & WO_GE 
30047 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
30048 20 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52   pRangeEnd && pR
30049 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74  angeEnd->eOperat
3004a 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20  or & WO_LE );.  
3004b 20 20 74 65 73 74 63 61 73 65 28 20 70 52 61 6e    testcase( pRan
3004c 67 65 45 6e 64 20 26 26 20 70 52 61 6e 67 65 45  geEnd && pRangeE
3004d 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  nd->eOperator & 
3004e 57 4f 5f 47 45 20 29 3b 0a 20 20 20 20 73 74 61  WO_GE );.    sta
3004f 72 74 45 71 20 3d 20 21 70 52 61 6e 67 65 53 74  rtEq = !pRangeSt
30050 61 72 74 20 7c 7c 20 70 52 61 6e 67 65 53 74 61  art || pRangeSta
30051 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rt->eOperator & 
30052 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20  (WO_LE|WO_GE);. 
30053 20 20 20 65 6e 64 45 71 20 3d 20 20 20 21 70 52     endEq =   !pR
30054 61 6e 67 65 45 6e 64 20 7c 7c 20 70 52 61 6e 67  angeEnd || pRang
30055 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20  eEnd->eOperator 
30056 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b  & (WO_LE|WO_GE);
30057 0a 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74  .    start_const
30058 72 61 69 6e 74 73 20 3d 20 70 52 61 6e 67 65 53  raints = pRangeS
30059 74 61 72 74 20 7c 7c 20 6e 45 71 3e 30 3b 0a 0a  tart || nEq>0;..
3005a 20 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20      /* Seek the 
3005b 69 6e 64 65 78 20 63 75 72 73 6f 72 20 74 6f 20  index cursor to 
3005c 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
3005d 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6e   range. */.    n
3005e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71  Constraint = nEq
3005f 3b 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67 65  ;.    if( pRange
30060 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 45  Start ){.      E
30061 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 52  xpr *pRight = pR
30062 61 6e 67 65 53 74 61 72 74 2d 3e 70 45 78 70 72  angeStart->pExpr
30063 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ->pRight;.      
30064 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
30065 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20  pParse, pRight, 
30066 72 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20  regBase+nEq);.  
30067 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
30068 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c  odeIsNullJump(v,
30069 20 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65   pRight, regBase
3006a 2b 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a  +nEq, addrNxt);.
3006b 20 20 20 20 20 20 69 66 28 20 7a 41 66 66 20 29        if( zAff )
3006c 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
3006d 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
3006e 6e 69 74 79 28 70 52 69 67 68 74 2c 20 7a 41 66  nity(pRight, zAf
3006f 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29 3d  f[nConstraint])=
30070 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
30071 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
30072 53 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72  Since the compar
30073 69 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65  ison is to be pe
30074 72 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20  rformed with no 
30075 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 20 20 20 20  conversions.    
30076 20 20 20 20 20 20 2a 2a 20 61 70 70 6c 69 65 64        ** applied
30077 20 74 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73   to the operands
30078 2c 20 73 65 74 20 74 68 65 20 61 66 66 69 6e 69  , set the affini
30079 74 79 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70  ty to apply to p
3007a 52 69 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20  Right to .      
3007b 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46      ** SQLITE_AF
3007c 46 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20  F_NONE.  */.    
3007d 20 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f 6e 73        zAff[nCons
3007e 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49 54 45  traint] = SQLITE
3007f 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20  _AFF_NONE;.     
30080 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
30081 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65 64   sqlite3ExprNeed
30082 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e 67  sNoAffinityChang
30083 65 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6e  e(pRight, zAff[n
30084 43 6f 6e 73 74 72 61 69 6e 74 5d 29 20 29 7b 0a  Constraint]) ){.
30085 20 20 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6e            zAff[n
30086 43 6f 6e 73 74 72 61 69 6e 74 5d 20 3d 20 53 51  Constraint] = SQ
30087 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20  LITE_AFF_NONE;. 
30088 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
30089 20 20 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72    .      nConstr
3008a 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73  aint++;.    }els
3008b 65 20 69 66 28 20 69 73 4d 69 6e 51 75 65 72 79  e if( isMinQuery
3008c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
3008d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
3008e 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 42 61  P_Null, 0, regBa
3008f 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 6e  se+nEq);.      n
30090 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20  Constraint++;.  
30091 20 20 20 20 73 74 61 72 74 45 71 20 3d 20 30 3b      startEq = 0;
30092 0a 20 20 20 20 20 20 73 74 61 72 74 5f 63 6f 6e  .      start_con
30093 73 74 72 61 69 6e 74 73 20 3d 20 31 3b 0a 20 20  straints = 1;.  
30094 20 20 7d 0a 20 20 20 20 63 6f 64 65 41 70 70 6c    }.    codeAppl
30095 79 41 66 66 69 6e 69 74 79 28 70 50 61 72 73 65  yAffinity(pParse
30096 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73  , regBase, nCons
30097 74 72 61 69 6e 74 2c 20 7a 41 66 66 29 3b 0a 20  traint, zAff);. 
30098 20 20 20 6f 70 20 3d 20 61 53 74 61 72 74 4f 70     op = aStartOp
30099 5b 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69  [(start_constrai
3009a 6e 74 73 3c 3c 32 29 20 2b 20 28 73 74 61 72 74  nts<<2) + (start
3009b 45 71 3c 3c 31 29 20 2b 20 62 52 65 76 5d 3b 0a  Eq<<1) + bRev];.
3009c 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d      assert( op!=
3009d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  0 );.    testcas
3009e 65 28 20 6f 70 3d 3d 4f 50 5f 52 65 77 69 6e 64  e( op==OP_Rewind
3009f 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
300a0 28 20 6f 70 3d 3d 4f 50 5f 4c 61 73 74 20 29 3b  ( op==OP_Last );
300a1 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f  .    testcase( o
300a2 70 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a  p==OP_SeekGt );.
300a3 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
300a4 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 3b 0a 20  ==OP_SeekGe );. 
300a5 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
300a6 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20  =OP_SeekLe );.  
300a7 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
300a8 4f 50 5f 53 65 65 6b 4c 74 20 29 3b 0a 20 20 20  OP_SeekLt );.   
300a9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
300aa 70 34 49 6e 74 28 76 2c 20 6f 70 2c 20 69 49 64  p4Int(v, op, iId
300ab 78 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72  xCur, addrNxt, r
300ac 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61  egBase, nConstra
300ad 69 6e 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f  int);..    /* Lo
300ae 61 64 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72  ad the value for
300af 20 74 68 65 20 69 6e 65 71 75 61 6c 69 74 79 20   the inequality 
300b0 63 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 74 68  constraint at th
300b1 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 20  e end of the.   
300b2 20 2a 2a 20 72 61 6e 67 65 20 28 69 66 20 61 6e   ** range (if an
300b3 79 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  y)..    */.    n
300b4 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71  Constraint = nEq
300b5 3b 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67 65  ;.    if( pRange
300b6 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 45 78 70  End ){.      Exp
300b7 72 20 2a 70 52 69 67 68 74 20 3d 20 70 52 61 6e  r *pRight = pRan
300b8 67 65 45 6e 64 2d 3e 70 45 78 70 72 2d 3e 70 52  geEnd->pExpr->pR
300b9 69 67 68 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  ight;.      sqli
300ba 74 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f  te3ExprCacheRemo
300bb 76 65 28 70 50 61 72 73 65 2c 20 72 65 67 42 61  ve(pParse, regBa
300bc 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 73  se+nEq);.      s
300bd 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
300be 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72  Parse, pRight, r
300bf 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20  egBase+nEq);.   
300c0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
300c1 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c 20  deIsNullJump(v, 
300c2 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b  pRight, regBase+
300c3 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20  nEq, addrNxt);. 
300c4 20 20 20 20 20 69 66 28 20 7a 41 66 66 20 29 7b       if( zAff ){
300c5 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
300c6 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e  ite3CompareAffin
300c7 69 74 79 28 70 52 69 67 68 74 2c 20 7a 41 66 66  ity(pRight, zAff
300c8 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29 3d 3d  [nConstraint])==
300c9 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29  SQLITE_AFF_NONE)
300ca 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  {.          /* S
300cb 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69  ince the compari
300cc 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65 72  son is to be per
300cd 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20 63  formed with no c
300ce 6f 6e 76 65 72 73 69 6f 6e 73 0a 20 20 20 20 20  onversions.     
300cf 20 20 20 20 20 2a 2a 20 61 70 70 6c 69 65 64 20       ** applied 
300d0 74 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c  to the operands,
300d1 20 73 65 74 20 74 68 65 20 61 66 66 69 6e 69 74   set the affinit
300d2 79 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52  y to apply to pR
300d3 69 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20  ight to .       
300d4 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46     ** SQLITE_AFF
300d5 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20  _NONE.  */.     
300d6 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f 6e 73 74       zAff[nConst
300d7 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49 54 45 5f  raint] = SQLITE_
300d8 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20  AFF_NONE;.      
300d9 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
300da 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65 64 73  sqlite3ExprNeeds
300db 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  NoAffinityChange
300dc 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6e 43  (pRight, zAff[nC
300dd 6f 6e 73 74 72 61 69 6e 74 5d 29 20 29 7b 0a 20  onstraint]) ){. 
300de 20 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6e 43           zAff[nC
300df 6f 6e 73 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c  onstraint] = SQL
300e0 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
300e1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20        }.      } 
300e2 20 0a 20 20 20 20 20 20 63 6f 64 65 41 70 70 6c   .      codeAppl
300e3 79 41 66 66 69 6e 69 74 79 28 70 50 61 72 73 65  yAffinity(pParse
300e4 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 71 2b 31  , regBase, nEq+1
300e5 2c 20 7a 41 66 66 29 3b 0a 20 20 20 20 20 20 6e  , zAff);.      n
300e6 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20  Constraint++;.  
300e7 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
300e8 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62  bFree(pParse->db
300e9 2c 20 7a 41 66 66 29 3b 0a 0a 20 20 20 20 2f 2a  , zAff);..    /*
300ea 20 54 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   Top of the loop
300eb 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 70 4c 65   body */.    pLe
300ec 76 65 6c 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65  vel->p2 = sqlite
300ed 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
300ee 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65  (v);..    /* Che
300ef 63 6b 20 69 66 20 74 68 65 20 69 6e 64 65 78 20  ck if the index 
300f0 63 75 72 73 6f 72 20 69 73 20 70 61 73 74 20 74  cursor is past t
300f1 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 61  he end of the ra
300f2 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6f 70 20 3d  nge. */.    op =
300f3 20 61 45 6e 64 4f 70 5b 28 70 52 61 6e 67 65 45   aEndOp[(pRangeE
300f4 6e 64 20 7c 7c 20 6e 45 71 29 20 2a 20 28 31 20  nd || nEq) * (1 
300f5 2b 20 62 52 65 76 29 5d 3b 0a 20 20 20 20 74 65  + bRev)];.    te
300f6 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e  stcase( op==OP_N
300f7 6f 6f 70 20 29 3b 0a 20 20 20 20 74 65 73 74 63  oop );.    testc
300f8 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 47  ase( op==OP_IdxG
300f9 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  E );.    testcas
300fa 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 4c 54 20  e( op==OP_IdxLT 
300fb 29 3b 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 4f  );.    if( op!=O
300fc 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20  P_Noop ){.      
300fd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
300fe 34 49 6e 74 28 76 2c 20 6f 70 2c 20 69 49 64 78  4Int(v, op, iIdx
300ff 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72 65  Cur, addrNxt, re
30100 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69  gBase, nConstrai
30101 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  nt);.      sqlit
30102 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
30103 2c 20 65 6e 64 45 71 21 3d 62 52 65 76 20 3f 31  , endEq!=bRev ?1
30104 3a 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  :0);.    }..    
30105 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
30106 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  inequality const
30107 72 61 69 6e 74 73 2c 20 63 68 65 63 6b 20 74 68  raints, check th
30108 61 74 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20  at the value.   
30109 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65   ** of the table
3010a 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65   column that the
3010b 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 74   inequality cont
3010c 72 61 69 6e 73 20 69 73 20 6e 6f 74 20 4e 55 4c  rains is not NUL
3010d 4c 2e 0a 20 20 20 20 2a 2a 20 49 66 20 69 74 20  L..    ** If it 
3010e 69 73 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20  is, jump to the 
3010f 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
30110 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 20 20  f the loop..    
30111 2a 2f 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69  */.    r1 = sqli
30112 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
30113 61 72 73 65 29 3b 0a 20 20 20 20 74 65 73 74 63  arse);.    testc
30114 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  ase( pLevel->pla
30115 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
30116 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20  E_BTM_LIMIT );. 
30117 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65     testcase( pLe
30118 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
30119 73 20 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49  s & WHERE_TOP_LI
3011a 4d 49 54 20 29 3b 0a 20 20 20 20 69 66 28 20 70  MIT );.    if( p
3011b 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
3011c 61 67 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d  ags & (WHERE_BTM
3011d 5f 4c 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50  _LIMIT|WHERE_TOP
3011e 5f 4c 49 4d 49 54 29 20 29 7b 0a 20 20 20 20 20  _LIMIT) ){.     
3011f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30120 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
30121 20 69 49 64 78 43 75 72 2c 20 6e 45 71 2c 20 72   iIdxCur, nEq, r
30122 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
30123 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
30124 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20 61 64  P_IsNull, r1, ad
30125 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 7d 0a 20  drCont);.    }. 
30126 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
30127 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
30128 20 72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65   r1);..    /* Se
30129 65 6b 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  ek the table cur
3012a 73 6f 72 2c 20 69 66 20 72 65 71 75 69 72 65 64  sor, if required
3012b 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 54   */.    disableT
3012c 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e  erm(pLevel, pRan
3012d 67 65 53 74 61 72 74 29 3b 0a 20 20 20 20 64 69  geStart);.    di
3012e 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
3012f 2c 20 70 52 61 6e 67 65 45 6e 64 29 3b 0a 20 20  , pRangeEnd);.  
30130 20 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65    if( !omitTable
30131 20 29 7b 0a 20 20 20 20 20 20 69 52 6f 77 69 64   ){.      iRowid
30132 52 65 67 20 3d 20 69 52 65 6c 65 61 73 65 52 65  Reg = iReleaseRe
30133 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  g = sqlite3GetTe
30134 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
30135 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
30136 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
30137 52 6f 77 69 64 2c 20 69 49 64 78 43 75 72 2c 20  Rowid, iIdxCur, 
30138 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
30139 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
3013a 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20  heStore(pParse, 
3013b 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64  iCur, -1, iRowid
3013c 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Reg);.      sqli
3013d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
3013e 20 4f 50 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20   OP_Seek, iCur, 
3013f 69 52 6f 77 69 64 52 65 67 29 3b 20 20 2f 2a 20  iRowidReg);  /* 
30140 44 65 66 65 72 72 65 64 20 73 65 65 6b 20 2a 2f  Deferred seek */
30141 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52  .    }..    /* R
30142 65 63 6f 72 64 20 74 68 65 20 69 6e 73 74 72 75  ecord the instru
30143 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65  ction used to te
30144 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70  rminate the loop
30145 2e 20 44 69 73 61 62 6c 65 20 0a 20 20 20 20 2a  . Disable .    *
30146 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  * WHERE clause t
30147 65 72 6d 73 20 6d 61 64 65 20 72 65 64 75 6e 64  erms made redund
30148 61 6e 74 20 62 79 20 74 68 65 20 69 6e 64 65 78  ant by the index
30149 20 72 61 6e 67 65 20 73 63 61 6e 2e 0a 20 20 20   range scan..   
3014a 20 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e   */.    pLevel->
3014b 6f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50  op = bRev ? OP_P
3014c 72 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20  rev : OP_Next;. 
3014d 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20     pLevel->p1 = 
3014e 69 49 64 78 43 75 72 3b 0a 20 20 7d 65 6c 73 65  iIdxCur;.  }else
3014f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
30150 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
30151 41 54 49 4f 4e 0a 20 20 69 66 28 20 70 4c 65 76  ATION.  if( pLev
30152 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
30153 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f   & WHERE_MULTI_O
30154 52 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65  R ){.    /* Case
30155 20 34 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65   4:  Two or more
30156 20 73 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65   separately inde
30157 78 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63  xed terms connec
30158 74 65 64 20 62 79 20 4f 52 0a 20 20 20 20 2a 2a  ted by OR.    **
30159 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a  .    ** Example:
3015a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
3015b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
3015c 28 61 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 2a  (a,b,c,d);.    *
3015d 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58  *   CREATE INDEX
3015e 20 69 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 20   i1 ON t1(a);.  
3015f 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e    **   CREATE IN
30160 44 45 58 20 69 32 20 4f 4e 20 74 31 28 62 29 3b  DEX i2 ON t1(b);
30161 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45  .    **   CREATE
30162 20 49 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 28   INDEX i3 ON t1(
30163 63 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  c);.    **.    *
30164 2a 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  *   SELECT * FRO
30165 4d 20 74 31 20 57 48 45 52 45 20 61 3d 35 20 4f  M t1 WHERE a=5 O
30166 52 20 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 41  R b=7 OR (c=11 A
30167 4e 44 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a 0a  ND d=13).    **.
30168 20 20 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 78      ** In the ex
30169 61 6d 70 6c 65 2c 20 74 68 65 72 65 20 61 72 65  ample, there are
3016a 20 74 68 72 65 65 20 69 6e 64 65 78 65 64 20 74   three indexed t
3016b 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62  erms connected b
3016c 79 20 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 65  y OR..    ** The
3016d 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
3016e 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73   looks like this
3016f 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
30170 20 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20           Null   
30171 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
30172 20 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20       # Zero the 
30173 72 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 0a  rowset in reg 1.
30174 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
30175 65 6e 2c 20 66 6f 72 20 65 61 63 68 20 69 6e 64  en, for each ind
30176 65 78 65 64 20 74 65 72 6d 2c 20 74 68 65 20 66  exed term, the f
30177 6f 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 72  ollowing. The ar
30178 67 75 6d 65 6e 74 73 20 74 6f 0a 20 20 20 20 2a  guments to.    *
30179 2a 20 52 6f 77 53 65 74 54 65 73 74 20 61 72 65  * RowSetTest are
3017a 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 72   such that the r
3017b 6f 77 69 64 20 6f 66 20 74 68 65 20 63 75 72 72  owid of the curr
3017c 65 6e 74 20 72 6f 77 20 69 73 20 69 6e 73 65 72  ent row is inser
3017d 74 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20  ted.    ** into 
3017e 74 68 65 20 52 6f 77 53 65 74 2e 20 49 66 20 69  the RowSet. If i
3017f 74 20 69 73 20 61 6c 72 65 61 64 79 20 70 72 65  t is already pre
30180 73 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73 6b  sent, control sk
30181 69 70 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 47  ips the.    ** G
30182 6f 73 75 62 20 6f 70 63 6f 64 65 20 61 6e 64 20  osub opcode and 
30183 6a 75 6d 70 73 20 73 74 72 61 69 67 68 74 20 74  jumps straight t
30184 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  o the code gener
30185 61 74 65 64 20 62 79 20 57 68 65 72 65 45 6e 64  ated by WhereEnd
30186 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ()..    **.    *
30187 2a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  *        sqlite3
30188 57 68 65 72 65 42 65 67 69 6e 28 3c 74 65 72 6d  WhereBegin(<term
30189 3e 29 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  >).    **       
3018a 20 20 20 52 6f 77 53 65 74 54 65 73 74 20 20 20     RowSetTest   
3018b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23                 #
3018c 20 49 6e 73 65 72 74 20 72 6f 77 69 64 20 69 6e   Insert rowid in
3018d 74 6f 20 72 6f 77 73 65 74 0a 20 20 20 20 2a 2a  to rowset.    **
3018e 20 20 20 20 20 20 20 20 20 20 47 6f 73 75 62 20            Gosub 
3018f 20 20 20 20 20 32 20 41 0a 20 20 20 20 2a 2a 20       2 A.    ** 
30190 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
30191 65 72 65 45 6e 64 28 29 0a 20 20 20 20 2a 2a 0a  ereEnd().    **.
30192 20 20 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67      ** Following
30193 20 74 68 65 20 61 62 6f 76 65 2c 20 63 6f 64 65   the above, code
30194 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68   to terminate th
30195 65 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 41 2c  e loop. Label A,
30196 20 74 68 65 20 74 61 72 67 65 74 0a 20 20 20 20   the target.    
30197 2a 2a 20 6f 66 20 74 68 65 20 47 6f 73 75 62 20  ** of the Gosub 
30198 61 62 6f 76 65 2c 20 6a 75 6d 70 73 20 74 6f 20  above, jumps to 
30199 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
3019a 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20  right after the 
3019b 47 6f 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  Goto..    **.   
3019c 20 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c   **          Nul
3019d 6c 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20  l       1       
3019e 20 20 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20           # Zero 
3019f 74 68 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65  the rowset in re
301a0 67 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  g 1.    **      
301a1 20 20 20 20 47 6f 74 6f 20 20 20 20 20 20 20 42      Goto       B
301a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
301a3 23 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 66 69  # The loop is fi
301a4 6e 69 73 68 65 64 2e 0a 20 20 20 20 2a 2a 0a 20  nished..    **. 
301a5 20 20 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 3c     **       A: <
301a6 6c 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 20  loop body>      
301a7 20 20 20 20 20 20 20 20 20 20 20 23 20 52 65 74             # Ret
301a8 75 72 6e 20 64 61 74 61 2c 20 77 68 61 74 65 76  urn data, whatev
301a9 65 72 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  er..    **.    *
301aa 2a 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72  *          Retur
301ab 6e 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20  n     2         
301ac 20 20 20 20 20 20 20 23 20 4a 75 6d 70 20 62 61         # Jump ba
301ad 63 6b 20 74 6f 20 74 68 65 20 47 6f 73 75 62 0a  ck to the Gosub.
301ae 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
301af 20 20 20 20 42 3a 20 3c 61 66 74 65 72 20 74 68      B: <after th
301b0 65 20 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a 0a 20  e loop>.    **. 
301b1 20 20 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 43     */.    WhereC
301b2 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20  lause *pOrWc;   
301b3 20 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75 73   /* The OR-claus
301b4 65 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e 74  e broken out int
301b5 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20  o subterms */.  
301b6 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 46 69    WhereTerm *pFi
301b7 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 46 69 6e 61  nal;     /* Fina
301b8 6c 20 73 75 62 74 65 72 6d 20 77 69 74 68 69 6e  l subterm within
301b9 20 74 68 65 20 4f 52 2d 63 6c 61 75 73 65 2e 20   the OR-clause. 
301ba 2a 2f 0a 20 20 20 20 53 72 63 4c 69 73 74 20 6f  */.    SrcList o
301bb 6e 65 54 61 62 3b 20 20 20 20 20 20 20 20 2f 2a  neTab;        /*
301bc 20 53 68 6f 72 74 65 6e 65 64 20 74 61 62 6c 65   Shortened table
301bd 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 69 6e   list */..    in
301be 74 20 72 65 67 52 65 74 75 72 6e 20 3d 20 2b 2b  t regReturn = ++
301bf 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20  pParse->nMem;   
301c0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
301c1 74 65 72 20 75 73 65 64 20 77 69 74 68 20 4f 50  ter used with OP
301c2 5f 47 6f 73 75 62 20 2a 2f 0a 20 20 20 20 69 6e  _Gosub */.    in
301c3 74 20 72 65 67 52 6f 77 73 65 74 20 3d 20 30 3b  t regRowset = 0;
301c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
301c5 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
301c6 74 65 72 20 66 6f 72 20 52 6f 77 53 65 74 20 6f  ter for RowSet o
301c7 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 69 6e 74  bject */.    int
301c8 20 72 65 67 52 6f 77 69 64 20 3d 20 30 3b 20 20   regRowid = 0;  
301c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
301ca 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
301cb 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64  er holding rowid
301cc 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c 6f 6f   */.    int iLoo
301cd 70 42 6f 64 79 20 3d 20 73 71 6c 69 74 65 33 56  pBody = sqlite3V
301ce 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
301cf 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6c 6f    /* Start of lo
301d0 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 69  op body */.    i
301d1 6e 74 20 69 52 65 74 49 6e 69 74 3b 20 20 20 20  nt iRetInit;    
301d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
301d3 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
301d4 65 73 73 20 6f 66 20 72 65 67 52 65 74 75 72 6e  ess of regReturn
301d5 20 69 6e 69 74 20 2a 2f 0a 20 20 20 20 69 6e 74   init */.    int
301d6 20 69 69 3b 0a 20 20 20 0a 20 20 20 20 70 54 65   ii;.   .    pTe
301d7 72 6d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  rm = pLevel->pla
301d8 6e 2e 75 2e 70 54 65 72 6d 3b 0a 20 20 20 20 61  n.u.pTerm;.    a
301d9 73 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20  ssert( pTerm!=0 
301da 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
301db 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
301dc 3d 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61 73  =WO_OR );.    as
301dd 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74  sert( (pTerm->wt
301de 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49  Flags & TERM_ORI
301df 4e 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20 70  NFO)!=0 );.    p
301e0 4f 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75  OrWc = &pTerm->u
301e1 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20  .pOrInfo->wc;.  
301e2 20 20 70 46 69 6e 61 6c 20 3d 20 26 70 4f 72 57    pFinal = &pOrW
301e3 63 2d 3e 61 5b 70 4f 72 57 63 2d 3e 6e 54 65 72  c->a[pOrWc->nTer
301e4 6d 2d 31 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 65  m-1];..    /* Se
301e5 74 20 75 70 20 61 20 53 72 63 4c 69 73 74 20 63  t up a SrcList c
301e6 6f 6e 74 61 69 6e 69 6e 67 20 6a 75 73 74 20 74  ontaining just t
301e7 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 73  he table being s
301e8 63 61 6e 6e 65 64 20 62 79 20 74 68 69 73 20 6c  canned by this l
301e9 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 6f 6e 65 54  oop. */.    oneT
301ea 61 62 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20  ab.nSrc = 1;.   
301eb 20 6f 6e 65 54 61 62 2e 6e 41 6c 6c 6f 63 20 3d   oneTab.nAlloc =
301ec 20 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62 2e 61   1;.    oneTab.a
301ed 5b 30 5d 20 3d 20 2a 70 54 61 62 49 74 65 6d 3b  [0] = *pTabItem;
301ee 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c  ..    /* Initial
301ef 69 7a 65 20 74 68 65 20 72 6f 77 73 65 74 20 72  ize the rowset r
301f0 65 67 69 73 74 65 72 20 74 6f 20 63 6f 6e 74 61  egister to conta
301f1 69 6e 20 4e 55 4c 4c 2e 20 41 6e 20 53 51 4c 20  in NULL. An SQL 
301f2 4e 55 4c 4c 20 69 73 20 0a 20 20 20 20 2a 2a 20  NULL is .    ** 
301f3 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 6e  equivalent to an
301f4 20 65 6d 70 74 79 20 72 6f 77 73 65 74 2e 0a 20   empty rowset.. 
301f5 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73     **.    ** Als
301f6 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 72 65 67  o initialize reg
301f7 52 65 74 75 72 6e 20 74 6f 20 63 6f 6e 74 61 69  Return to contai
301f8 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  n the address of
301f9 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e   the instruction
301fa 20 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61   .    ** immedia
301fb 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  tely following t
301fc 68 65 20 4f 50 5f 52 65 74 75 72 6e 20 61 74 20  he OP_Return at 
301fd 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68  the bottom of th
301fe 65 20 6c 6f 6f 70 2e 20 54 68 69 73 0a 20 20 20  e loop. This.   
301ff 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20   ** is required 
30200 69 6e 20 61 20 66 65 77 20 6f 62 73 63 75 72 65  in a few obscure
30201 20 4c 45 46 54 20 4a 4f 49 4e 20 63 61 73 65 73   LEFT JOIN cases
30202 20 77 68 65 72 65 20 63 6f 6e 74 72 6f 6c 20 6a   where control j
30203 75 6d 70 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72  umps.    ** over
30204 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
30205 6c 6f 6f 70 20 69 6e 74 6f 20 74 68 65 20 62 6f  loop into the bo
30206 64 79 20 6f 66 20 69 74 2e 20 49 6e 20 74 68 69  dy of it. In thi
30207 73 20 63 61 73 65 20 74 68 65 20 0a 20 20 20 20  s case the .    
30208 2a 2a 20 63 6f 72 72 65 63 74 20 72 65 73 70 6f  ** correct respo
30209 6e 73 65 20 66 6f 72 20 74 68 65 20 65 6e 64 2d  nse for the end-
3020a 6f 66 2d 6c 6f 6f 70 20 63 6f 64 65 20 28 74 68  of-loop code (th
3020b 65 20 4f 50 5f 52 65 74 75 72 6e 29 20 69 73 20  e OP_Return) is 
3020c 74 6f 20 0a 20 20 20 20 2a 2a 20 66 61 6c 6c 20  to .    ** fall 
3020d 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
3020e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c  ext instruction,
3020f 20 6a 75 73 74 20 61 73 20 61 6e 20 4f 50 5f 4e   just as an OP_N
30210 65 78 74 20 64 6f 65 73 20 69 66 0a 20 20 20 20  ext does if.    
30211 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20  ** called on an 
30212 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 75  uninitialized cu
30213 72 73 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rsor..    */.   
30214 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73   if( (wctrlFlags
30215 20 26 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41   & WHERE_DUPLICA
30216 54 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20  TES_OK)==0 ){.  
30217 20 20 20 20 72 65 67 52 6f 77 73 65 74 20 3d 20      regRowset = 
30218 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
30219 20 20 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d        regRowid =
3021a 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
3021b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3021c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
3021d 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 73 65  ull, 0, regRowse
3021e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 52  t);.    }.    iR
3021f 65 74 49 6e 69 74 20 3d 20 73 71 6c 69 74 65 33  etInit = sqlite3
30220 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
30221 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67  _Integer, 0, reg
30222 52 65 74 75 72 6e 29 3b 0a 0a 20 20 20 20 66 6f  Return);..    fo
30223 72 28 69 69 3d 30 3b 20 69 69 3c 70 4f 72 57 63  r(ii=0; ii<pOrWc
30224 2d 3e 6e 54 65 72 6d 3b 20 69 69 2b 2b 29 7b 0a  ->nTerm; ii++){.
30225 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
30226 2a 70 4f 72 54 65 72 6d 20 3d 20 26 70 4f 72 57  *pOrTerm = &pOrW
30227 63 2d 3e 61 5b 69 69 5d 3b 0a 20 20 20 20 20 20  c->a[ii];.      
30228 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66  if( pOrTerm->lef
30229 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 7c 7c  tCursor==iCur ||
3022a 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
3022b 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20  tor==WO_AND ){. 
3022c 20 20 20 20 20 20 20 57 68 65 72 65 49 6e 66 6f         WhereInfo
3022d 20 2a 70 53 75 62 57 49 6e 66 6f 3b 20 20 20 20   *pSubWInfo;    
3022e 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 66 6f        /* Info fo
3022f 72 20 73 69 6e 67 6c 65 20 4f 52 2d 74 65 72 6d  r single OR-term
30230 20 73 63 61 6e 20 2a 2f 0a 20 20 20 20 20 20 20   scan */.       
30231 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
30232 20 74 61 62 6c 65 20 65 6e 74 72 69 65 73 20 74   table entries t
30233 68 61 74 20 6d 61 74 63 68 20 74 65 72 6d 20 70  hat match term p
30234 4f 72 54 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20  OrTerm. */.     
30235 20 20 20 70 53 75 62 57 49 6e 66 6f 20 3d 20 73     pSubWInfo = s
30236 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
30237 28 70 50 61 72 73 65 2c 20 26 6f 6e 65 54 61 62  (pParse, &oneTab
30238 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  , pOrTerm->pExpr
30239 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
3023a 20 20 20 20 20 20 20 20 20 20 20 20 20 57 48 45               WHE
3023b 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 7c 20 57  RE_OMIT_OPEN | W
3023c 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20  HERE_OMIT_CLOSE 
3023d 7c 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41  | WHERE_FORCE_TA
3023e 42 4c 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66  BLE);.        if
3023f 28 20 70 53 75 62 57 49 6e 66 6f 20 29 7b 0a 20  ( pSubWInfo ){. 
30240 20 20 20 20 20 20 20 20 20 69 66 28 20 28 77 63           if( (wc
30241 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
30242 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3d  _DUPLICATES_OK)=
30243 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
30244 20 20 69 6e 74 20 69 53 65 74 20 3d 20 28 28 69    int iSet = ((i
30245 69 3d 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d  i==pOrWc->nTerm-
30246 31 29 3f 2d 31 3a 69 69 29 3b 0a 20 20 20 20 20  1)?-1:ii);.     
30247 20 20 20 20 20 20 20 69 6e 74 20 72 3b 0a 20 20         int r;.  
30248 20 20 20 20 20 20 20 20 20 20 72 20 3d 20 73 71            r = sq
30249 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
3024a 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
3024b 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2c 20 2d  TabItem->pTab, -
3024c 31 2c 20 69 43 75 72 2c 20 0a 20 20 20 20 20 20  1, iCur, .      
3024d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3024e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3024f 20 20 20 72 65 67 52 6f 77 69 64 2c 20 30 29 3b     regRowid, 0);
30250 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
30251 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e  ite3VdbeAddOp4In
30252 74 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 54 65  t(v, OP_RowSetTe
30253 73 74 2c 20 72 65 67 52 6f 77 73 65 74 2c 0a 20  st, regRowset,. 
30254 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30255 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30256 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
30257 6e 74 41 64 64 72 28 76 29 2b 32 2c 20 72 2c 20  ntAddr(v)+2, r, 
30258 69 53 65 74 29 3b 0a 20 20 20 20 20 20 20 20 20  iSet);.         
30259 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c   }.          sql
3025a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3025b 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 52  , OP_Gosub, regR
3025c 65 74 75 72 6e 2c 20 69 4c 6f 6f 70 42 6f 64 79  eturn, iLoopBody
3025d 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  );..          /*
3025e 20 46 69 6e 69 73 68 20 74 68 65 20 6c 6f 6f 70   Finish the loop
3025f 20 74 68 72 6f 75 67 68 20 74 61 62 6c 65 20 65   through table e
30260 6e 74 72 69 65 73 20 74 68 61 74 20 6d 61 74 63  ntries that matc
30261 68 20 74 65 72 6d 20 70 4f 72 54 65 72 6d 2e 20  h term pOrTerm. 
30262 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  */.          sql
30263 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 53 75  ite3WhereEnd(pSu
30264 62 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  bWInfo);.       
30265 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
30266 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
30267 43 68 61 6e 67 65 50 31 28 76 2c 20 69 52 65 74  ChangeP1(v, iRet
30268 49 6e 69 74 2c 20 73 71 6c 69 74 65 33 56 64 62  Init, sqlite3Vdb
30269 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 29  eCurrentAddr(v))
3026a 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33  ;.    /* sqlite3
3026b 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
3026c 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77  _Null, 0, regRow
3026d 73 65 74 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c  set); */.    sql
3026e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3026f 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 4c  , OP_Goto, 0, pL
30270 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29 3b 0a  evel->addrBrk);.
30271 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
30272 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
30273 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a 20 20 20 20  LoopBody);..    
30274 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f  pLevel->op = OP_
30275 52 65 74 75 72 6e 3b 0a 20 20 20 20 70 4c 65 76  Return;.    pLev
30276 65 6c 2d 3e 70 31 20 3d 20 72 65 67 52 65 74 75  el->p1 = regRetu
30277 72 6e 3b 0a 20 20 20 20 64 69 73 61 62 6c 65 54  rn;.    disableT
30278 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54 65 72  erm(pLevel, pTer
30279 6d 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  m);.  }else.#end
3027a 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
3027b 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  T_OR_OPTIMIZATIO
3027c 4e 20 2a 2f 0a 0a 20 20 7b 0a 20 20 20 20 2f 2a  N */..  {.    /*
3027d 20 43 61 73 65 20 35 3a 20 20 54 68 65 72 65 20   Case 5:  There 
3027e 69 73 20 6e 6f 20 75 73 61 62 6c 65 20 69 6e 64  is no usable ind
3027f 65 78 2e 20 20 57 65 20 6d 75 73 74 20 64 6f 20  ex.  We must do 
30280 61 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a  a complete.    *
30281 2a 20 20 20 20 20 20 20 20 20 20 73 63 61 6e 20  *          scan 
30282 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 61  of the entire ta
30283 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ble..    */.    
30284 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
30285 61 53 74 65 70 5b 5d 20 3d 20 7b 20 4f 50 5f 4e  aStep[] = { OP_N
30286 65 78 74 2c 20 4f 50 5f 50 72 65 76 20 7d 3b 0a  ext, OP_Prev };.
30287 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
30288 20 75 38 20 61 53 74 61 72 74 5b 5d 20 3d 20 7b   u8 aStart[] = {
30289 20 4f 50 5f 52 65 77 69 6e 64 2c 20 4f 50 5f 4c   OP_Rewind, OP_L
3028a 61 73 74 20 7d 3b 0a 20 20 20 20 61 73 73 65 72  ast };.    asser
3028b 74 28 20 62 52 65 76 3d 3d 30 20 7c 7c 20 62 52  t( bRev==0 || bR
3028c 65 76 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73  ev==1 );.    ass
3028d 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d  ert( omitTable==
3028e 30 20 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  0 );.    pLevel-
3028f 3e 6f 70 20 3d 20 61 53 74 65 70 5b 62 52 65 76  >op = aStep[bRev
30290 5d 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  ];.    pLevel->p
30291 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70 4c  1 = iCur;.    pL
30292 65 76 65 6c 2d 3e 70 32 20 3d 20 31 20 2b 20 73  evel->p2 = 1 + s
30293 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
30294 28 76 2c 20 61 53 74 61 72 74 5b 62 52 65 76 5d  (v, aStart[bRev]
30295 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b 29  , iCur, addrBrk)
30296 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 35  ;.    pLevel->p5
30297 20 3d 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 54   = SQLITE_STMTST
30298 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54  ATUS_FULLSCAN_ST
30299 45 50 3b 0a 20 20 7d 0a 20 20 6e 6f 74 52 65 61  EP;.  }.  notRea
3029a 64 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70  dy &= ~getMask(p
3029b 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 69 43  WC->pMaskSet, iC
3029c 75 72 29 3b 0a 0a 20 20 2f 2a 20 49 6e 73 65 72  ur);..  /* Inser
3029d 74 20 63 6f 64 65 20 74 6f 20 74 65 73 74 20 65  t code to test e
3029e 76 65 72 79 20 73 75 62 65 78 70 72 65 73 73 69  very subexpressi
3029f 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 63  on that can be c
302a0 6f 6d 70 6c 65 74 65 6c 79 0a 20 20 2a 2a 20 63  ompletely.  ** c
302a1 6f 6d 70 75 74 65 64 20 75 73 69 6e 67 20 74 68  omputed using th
302a2 65 20 63 75 72 72 65 6e 74 20 73 65 74 20 6f 66  e current set of
302a3 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
302a4 6b 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 54 65  k = 0;.  for(pTe
302a5 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 70 57 43  rm=pWC->a, j=pWC
302a6 2d 3e 6e 54 65 72 6d 3b 20 6a 3e 30 3b 20 6a 2d  ->nTerm; j>0; j-
302a7 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  -, pTerm++){.   
302a8 20 45 78 70 72 20 2a 70 45 3b 0a 20 20 20 20 74   Expr *pE;.    t
302a9 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
302aa 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56  wtFlags & TERM_V
302ab 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 74 65  IRTUAL );.    te
302ac 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 77  stcase( pTerm->w
302ad 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f  tFlags & TERM_CO
302ae 44 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20 70  DED );.    if( p
302af 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
302b0 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45  (TERM_VIRTUAL|TE
302b1 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e 74  RM_CODED) ) cont
302b2 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 28 70  inue;.    if( (p
302b3 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20  Term->prereqAll 
302b4 26 20 6e 6f 74 52 65 61 64 79 29 21 3d 30 20 29  & notReady)!=0 )
302b5 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70   continue;.    p
302b6 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  E = pTerm->pExpr
302b7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ;.    assert( pE
302b8 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  !=0 );.    if( p
302b9 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
302ba 20 26 26 20 21 45 78 70 72 48 61 73 50 72 6f 70   && !ExprHasProp
302bb 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72 6f 6d  erty(pE, EP_From
302bc 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20 20 63  Join) ){.      c
302bd 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20  ontinue;.    }. 
302be 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
302bf 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45  False(pParse, pE
302c0 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49  , addrCont, SQLI
302c1 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
302c2 20 20 20 20 6b 20 3d 20 31 3b 0a 20 20 20 20 70      k = 1;.    p
302c3 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d  Term->wtFlags |=
302c4 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 7d   TERM_CODED;.  }
302c5 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 4c 45 46  ..  /* For a LEF
302c6 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 67 65  T OUTER JOIN, ge
302c7 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
302c8 20 77 69 6c 6c 20 72 65 63 6f 72 64 20 74 68 65   will record the
302c9 20 66 61 63 74 20 74 68 61 74 0a 20 20 2a 2a 20   fact that.  ** 
302ca 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f 77  at least one row
302cb 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61   of the right ta
302cc 62 6c 65 20 68 61 73 20 6d 61 74 63 68 65 64 20  ble has matched 
302cd 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 2e 20  the left table. 
302ce 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 65   .  */.  if( pLe
302cf 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29  vel->iLeftJoin )
302d0 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64  {.    pLevel->ad
302d1 64 72 46 69 72 73 74 20 3d 20 73 71 6c 69 74 65  drFirst = sqlite
302d2 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
302d3 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  (v);.    sqlite3
302d4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
302d5 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 70 4c 65  _Integer, 1, pLe
302d6 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b  vel->iLeftJoin);
302d7 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
302d8 28 28 76 2c 20 22 72 65 63 6f 72 64 20 4c 45 46  ((v, "record LEF
302d9 54 20 4a 4f 49 4e 20 68 69 74 22 29 29 3b 0a 20  T JOIN hit"));. 
302da 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
302db 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29  cheClear(pParse)
302dc 3b 0a 20 20 20 20 66 6f 72 28 70 54 65 72 6d 3d  ;.    for(pTerm=
302dd 70 57 43 2d 3e 61 2c 20 6a 3d 30 3b 20 6a 3c 70  pWC->a, j=0; j<p
302de 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20  WC->nTerm; j++, 
302df 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pTerm++){.      
302e0 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
302e1 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
302e2 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 20  VIRTUAL );.     
302e3 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
302e4 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
302e5 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 20 20  _CODED );.      
302e6 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61  if( pTerm->wtFla
302e7 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54 55  gs & (TERM_VIRTU
302e8 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20 29  AL|TERM_CODED) )
302e9 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
302ea 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65   if( (pTerm->pre
302eb 72 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64  reqAll & notRead
302ec 79 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  y)!=0 ) continue
302ed 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
302ee 70 54 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b 0a  pTerm->pExpr );.
302ef 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
302f0 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
302f1 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 61   pTerm->pExpr, a
302f2 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54 45 5f  ddrCont, SQLITE_
302f3 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
302f4 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67     pTerm->wtFlag
302f5 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b  s |= TERM_CODED;
302f6 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
302f7 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
302f8 65 67 28 70 50 61 72 73 65 2c 20 69 52 65 6c 65  eg(pParse, iRele
302f9 61 73 65 52 65 67 29 3b 0a 0a 20 20 72 65 74 75  aseReg);..  retu
302fa 72 6e 20 6e 6f 74 52 65 61 64 79 3b 0a 7d 0a 0a  rn notReady;.}..
302fb 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
302fc 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54  TE_TEST)./*.** T
302fd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72  he following var
302fe 69 61 62 6c 65 20 68 6f 6c 64 73 20 61 20 74 65  iable holds a te
302ff 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f  xt description o
30300 66 20 71 75 65 72 79 20 70 6c 61 6e 20 67 65 6e  f query plan gen
30301 65 72 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65  erated.** by the
30302 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c   most recent cal
30303 6c 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72  l to sqlite3Wher
30304 65 42 65 67 69 6e 28 29 2e 20 20 45 61 63 68 20  eBegin().  Each 
30305 63 61 6c 6c 20 74 6f 20 57 68 65 72 65 42 65 67  call to WhereBeg
30306 69 6e 0a 2a 2a 20 6f 76 65 72 77 72 69 74 65 73  in.** overwrites
30307 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e 20 20   the previous.  
30308 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
30309 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
3030a 74 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c  ting and.** anal
3030b 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51  ysis only..*/.SQ
3030c 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 73 71  LITE_API char sq
3030d 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
3030e 5b 42 4d 53 2a 32 2a 34 30 5d 3b 20 20 2f 2a 20  [BMS*2*40];  /* 
3030f 54 65 78 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e  Text of the join
30310 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e   */.static int n
30311 51 50 6c 61 6e 20 3d 20 30 3b 20 20 20 20 20 20  QPlan = 0;      
30312 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
30313 66 72 65 65 20 73 6c 6f 77 20 69 6e 20 5f 71 75  free slow in _qu
30314 65 72 79 5f 70 6c 61 6e 5b 5d 20 2a 2f 0a 0a 23  ery_plan[] */..#
30315 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
30316 54 45 53 54 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  TEST */.../*.** 
30317 46 72 65 65 20 61 20 57 68 65 72 65 49 6e 66 6f  Free a WhereInfo
30318 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 73 74   structure.*/.st
30319 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 49  atic void whereI
3031a 6e 66 6f 46 72 65 65 28 73 71 6c 69 74 65 33 20  nfoFree(sqlite3 
3031b 2a 64 62 2c 20 57 68 65 72 65 49 6e 66 6f 20 2a  *db, WhereInfo *
3031c 70 57 49 6e 66 6f 29 7b 0a 20 20 69 66 28 20 70  pWInfo){.  if( p
3031d 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20 69 6e 74  WInfo ){.    int
3031e 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   i;.    for(i=0;
3031f 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65   i<pWInfo->nLeve
30320 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  l; i++){.      s
30321 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
30322 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 57 49 6e 66  o *pInfo = pWInf
30323 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f  o->a[i].pIdxInfo
30324 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e 66  ;.      if( pInf
30325 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  o ){.        /* 
30326 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e 6e  assert( pInfo->n
30327 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 3d  eedToFreeIdxStr=
30328 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  =0 || db->malloc
30329 46 61 69 6c 65 64 20 29 3b 20 2a 2f 0a 20 20 20  Failed ); */.   
3032a 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e       if( pInfo->
3032b 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
3032c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
3032d 6c 69 74 65 33 5f 66 72 65 65 28 70 49 6e 66 6f  lite3_free(pInfo
3032e 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20 20  ->idxStr);.     
3032f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
30330 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
30331 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Info);.      }. 
30332 20 20 20 7d 0a 20 20 20 20 77 68 65 72 65 43 6c     }.    whereCl
30333 61 75 73 65 43 6c 65 61 72 28 70 57 49 6e 66 6f  auseClear(pWInfo
30334 2d 3e 70 57 43 29 3b 0a 20 20 20 20 73 71 6c 69  ->pWC);.    sqli
30335 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 57  te3DbFree(db, pW
30336 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f  Info);.  }.}.../
30337 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68  *.** Generate th
30338 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
30339 68 65 20 6c 6f 6f 70 20 75 73 65 64 20 66 6f 72  he loop used for
3033a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72   WHERE clause pr
3033b 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 20 54 68 65  ocessing..** The
3033c 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
3033d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
3033e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72   opaque structur
3033f 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  e that contains.
30340 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e  ** information n
30341 65 65 64 65 64 20 74 6f 20 74 65 72 6d 69 6e 61  eeded to termina
30342 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20 20 4c 61  te the loop.  La
30343 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67  ter, the calling
30344 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 68 6f 75   routine.** shou
30345 6c 64 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ld invoke sqlite
30346 33 57 68 65 72 65 45 6e 64 28 29 20 77 69 74 68  3WhereEnd() with
30347 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
30348 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  e of this functi
30349 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74  on.** in order t
3034a 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 57  o complete the W
3034b 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63  HERE clause proc
3034c 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  essing..**.** If
3034d 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
3034e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  , this routine r
3034f 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  eturns NULL..**.
30350 2a 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 65  ** The basic ide
30351 61 20 69 73 20 74 6f 20 64 6f 20 61 20 6e 65 73  a is to do a nes
30352 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65 20 6c 6f  ted loop, one lo
30353 6f 70 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c  op for each tabl
30354 65 20 69 6e 0a 2a 2a 20 74 68 65 20 46 52 4f 4d  e in.** the FROM
30355 20 63 6c 61 75 73 65 20 6f 66 20 61 20 73 65 6c   clause of a sel
30356 65 63 74 2e 20 20 28 49 4e 53 45 52 54 20 61 6e  ect.  (INSERT an
30357 64 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  d UPDATE stateme
30358 6e 74 73 20 61 72 65 20 74 68 65 0a 2a 2a 20 73  nts are the.** s
30359 61 6d 65 20 61 73 20 61 20 53 45 4c 45 43 54 20  ame as a SELECT 
3035a 77 69 74 68 20 6f 6e 6c 79 20 61 20 73 69 6e 67  with only a sing
3035b 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  le table in the 
3035c 46 52 4f 4d 20 63 6c 61 75 73 65 2e 29 20 20 46  FROM clause.)  F
3035d 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69  or.** example, i
3035e 66 20 74 68 65 20 53 51 4c 20 69 73 20 74 68 69  f the SQL is thi
3035f 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53  s:.**.**       S
30360 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c  ELECT * FROM t1,
30361 20 74 32 2c 20 74 33 20 57 48 45 52 45 20 2e 2e   t2, t3 WHERE ..
30362 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68  .;.**.** Then th
30363 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
30364 20 69 73 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79   is conceptually
30365 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77   like the follow
30366 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ing:.**.**      
30367 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e 20  foreach row1 in 
30368 74 31 20 64 6f 20 20 20 20 20 20 20 5c 20 20 20  t1 do       \   
30369 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a   Code generated.
3036a 2a 2a 20 20 20 20 20 20 20 20 66 6f 72 65 61 63  **        foreac
3036b 68 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 20  h row2 in t2 do 
3036c 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69       |-- by sqli
3036d 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 0a  te3WhereBegin().
3036e 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 65  **          fore
3036f 61 63 68 20 72 6f 77 33 20 69 6e 20 74 33 20 64  ach row3 in t3 d
30370 6f 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20 20 20  o   /.**        
30371 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20      ....**      
30372 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20 20      end         
30373 20 20 20 20 20 20 20 20 20 20 20 20 5c 20 20 20              \   
30374 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a   Code generated.
30375 2a 2a 20 20 20 20 20 20 20 20 65 6e 64 20 20 20  **        end   
30376 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30377 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69       |-- by sqli
30378 74 65 33 57 68 65 72 65 45 6e 64 28 29 0a 2a 2a  te3WhereEnd().**
30379 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20        end       
3037a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3037b 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74    /.**.** Note t
3037c 68 61 74 20 74 68 65 20 6c 6f 6f 70 73 20 6d 69  hat the loops mi
3037d 67 68 74 20 6e 6f 74 20 62 65 20 6e 65 73 74 65  ght not be neste
3037e 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 69  d in the order i
3037f 6e 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20  n which they.** 
30380 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 46 52  appear in the FR
30381 4f 4d 20 63 6c 61 75 73 65 20 69 66 20 61 20 64  OM clause if a d
30382 69 66 66 65 72 65 6e 74 20 6f 72 64 65 72 20 69  ifferent order i
30383 73 20 62 65 74 74 65 72 20 61 62 6c 65 20 74 6f  s better able to
30384 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20   make.** use of 
30385 69 6e 64 69 63 65 73 2e 20 20 4e 6f 74 65 20 61  indices.  Note a
30386 6c 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68  lso that when th
30387 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 61 70  e IN operator ap
30388 70 65 61 72 73 20 69 6e 0a 2a 2a 20 74 68 65 20  pears in.** the 
30389 57 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69 74  WHERE clause, it
3038a 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e   might result in
3038b 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e 65 73 74   additional nest
3038c 65 64 20 6c 6f 6f 70 73 20 66 6f 72 0a 2a 2a 20  ed loops for.** 
3038d 73 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68  scanning through
3038e 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 6e 20 74   all values on t
3038f 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
30390 64 65 20 6f 66 20 74 68 65 20 49 4e 2e 0a 2a 2a  de of the IN..**
30391 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 42 74  .** There are Bt
30392 72 65 65 20 63 75 72 73 6f 72 73 20 61 73 73 6f  ree cursors asso
30393 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68  ciated with each
30394 20 74 61 62 6c 65 2e 20 20 74 31 20 75 73 65 73   table.  t1 uses
30395 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65   cursor.** numbe
30396 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d  r pTabList->a[0]
30397 2e 69 43 75 72 73 6f 72 2e 20 20 74 32 20 75 73  .iCursor.  t2 us
30398 65 73 20 74 68 65 20 63 75 72 73 6f 72 20 70 54  es the cursor pT
30399 61 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69 43 75  abList->a[1].iCu
3039a 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20  rsor..** And so 
3039b 66 6f 72 74 68 2e 20 20 54 68 69 73 20 72 6f 75  forth.  This rou
3039c 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63  tine generates c
3039d 6f 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 6f 73  ode to open thos
3039e 65 20 56 44 42 45 20 63 75 72 73 6f 72 73 0a 2a  e VDBE cursors.*
3039f 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 57 68 65  * and sqlite3Whe
303a0 72 65 45 6e 64 28 29 20 67 65 6e 65 72 61 74 65  reEnd() generate
303a1 73 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 6c  s the code to cl
303a2 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20  ose them..**.** 
303a3 54 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 71  The code that sq
303a4 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
303a5 29 20 67 65 6e 65 72 61 74 65 73 20 6c 65 61 76  ) generates leav
303a6 65 73 20 74 68 65 20 63 75 72 73 6f 72 73 20 6e  es the cursors n
303a7 61 6d 65 64 0a 2a 2a 20 69 6e 20 70 54 61 62 4c  amed.** in pTabL
303a8 69 73 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  ist pointing at 
303a9 74 68 65 69 72 20 61 70 70 72 6f 70 72 69 61 74  their appropriat
303aa 65 20 65 6e 74 72 69 65 73 2e 20 20 54 68 65 20  e entries.  The 
303ab 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a 20 63 61  [...] code.** ca
303ac 6e 20 75 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  n use OP_Column 
303ad 61 6e 64 20 4f 50 5f 52 6f 77 69 64 20 6f 70 63  and OP_Rowid opc
303ae 6f 64 65 73 20 6f 6e 20 74 68 65 73 65 20 63 75  odes on these cu
303af 72 73 6f 72 73 20 74 6f 20 65 78 74 72 61 63 74  rsors to extract
303b0 0a 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68  .** data from th
303b1 65 20 76 61 72 69 6f 75 73 20 74 61 62 6c 65 73  e various tables
303b2 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a   of the loop..**
303b3 0a 2a 2a 20 49 66 20 74 68 65 20 57 48 45 52 45  .** If the WHERE
303b4 20 63 6c 61 75 73 65 20 69 73 20 65 6d 70 74 79   clause is empty
303b5 2c 20 74 68 65 20 66 6f 72 65 61 63 68 20 6c 6f  , the foreach lo
303b6 6f 70 73 20 6d 75 73 74 20 65 61 63 68 20 73 63  ops must each sc
303b7 61 6e 20 74 68 65 69 72 0a 2a 2a 20 65 6e 74 69  an their.** enti
303b8 72 65 20 74 61 62 6c 65 73 2e 20 20 54 68 75 73  re tables.  Thus
303b9 20 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f 69   a three-way joi
303ba 6e 20 69 73 20 61 6e 20 4f 28 4e 5e 33 29 20 6f  n is an O(N^3) o
303bb 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69  peration.  But i
303bc 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 73 20  f.** the tables 
303bd 68 61 76 65 20 69 6e 64 69 63 65 73 20 61 6e 64  have indices and
303be 20 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73   there are terms
303bf 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
303c0 61 75 73 65 20 74 68 61 74 0a 2a 2a 20 72 65 66  ause that.** ref
303c1 65 72 20 74 6f 20 74 68 6f 73 65 20 69 6e 64 69  er to those indi
303c2 63 65 73 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20  ces, a complete 
303c3 74 61 62 6c 65 20 73 63 61 6e 20 63 61 6e 20 62  table scan can b
303c4 65 20 61 76 6f 69 64 65 64 20 61 6e 64 20 74 68  e avoided and th
303c5 65 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 72  e.** code will r
303c6 75 6e 20 6d 75 63 68 20 66 61 73 74 65 72 2e 20  un much faster. 
303c7 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72   Most of the wor
303c8 6b 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  k of this routin
303c9 65 20 69 73 20 63 68 65 63 6b 69 6e 67 0a 2a 2a  e is checking.**
303ca 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65   to see if there
303cb 20 61 72 65 20 69 6e 64 69 63 65 73 20 74 68 61   are indices tha
303cc 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  t can be used to
303cd 20 73 70 65 65 64 20 75 70 20 74 68 65 20 6c 6f   speed up the lo
303ce 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20  op..**.** Terms 
303cf 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
303d0 75 73 65 20 61 72 65 20 61 6c 73 6f 20 75 73 65  use are also use
303d1 64 20 74 6f 20 6c 69 6d 69 74 20 77 68 69 63 68  d to limit which
303d2 20 72 6f 77 73 20 61 63 74 75 61 6c 6c 79 0a 2a   rows actually.*
303d3 2a 20 6d 61 6b 65 20 69 74 20 74 6f 20 74 68 65  * make it to the
303d4 20 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20 6d 69   "..." in the mi
303d5 64 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70  ddle of the loop
303d6 2e 20 20 41 66 74 65 72 20 65 61 63 68 20 22 66  .  After each "f
303d7 6f 72 65 61 63 68 22 2c 0a 2a 2a 20 74 65 72 6d  oreach",.** term
303d8 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  s of the WHERE c
303d9 6c 61 75 73 65 20 74 68 61 74 20 75 73 65 20 6f  lause that use o
303da 6e 6c 79 20 74 65 72 6d 73 20 69 6e 20 74 68 61  nly terms in tha
303db 74 20 6c 6f 6f 70 20 61 6e 64 20 6f 75 74 65 72  t loop and outer
303dc 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72 65 20 65 76  .** loops are ev
303dd 61 6c 75 61 74 65 64 20 61 6e 64 20 69 66 20 66  aluated and if f
303de 61 6c 73 65 20 61 20 6a 75 6d 70 20 69 73 20 6d  alse a jump is m
303df 61 64 65 20 61 72 6f 75 6e 64 20 61 6c 6c 20 73  ade around all s
303e0 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 6e  ubsequent.** inn
303e1 65 72 20 6c 6f 6f 70 73 20 28 6f 72 20 61 72 6f  er loops (or aro
303e2 75 6e 64 20 74 68 65 20 22 2e 2e 2e 22 20 69 66  und the "..." if
303e3 20 74 68 65 20 74 65 73 74 20 6f 63 63 75 72 73   the test occurs
303e4 20 77 69 74 68 69 6e 20 74 68 65 20 69 6e 6e 65   within the inne
303e5 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f 6f 70 29  r-.** most loop)
303e6 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20 4a 4f 49  .**.** OUTER JOI
303e7 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f 75 74 65  NS.**.** An oute
303e8 72 20 6a 6f 69 6e 20 6f 66 20 74 61 62 6c 65 73  r join of tables
303e9 20 74 31 20 61 6e 64 20 74 32 20 69 73 20 63 6f   t1 and t2 is co
303ea 6e 63 65 70 74 61 6c 6c 79 20 63 6f 64 65 64 20  nceptally coded 
303eb 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
303ec 2a 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77  *    foreach row
303ed 31 20 69 6e 20 74 31 20 64 6f 0a 2a 2a 20 20 20  1 in t1 do.**   
303ee 20 20 20 66 6c 61 67 20 3d 20 30 0a 2a 2a 20 20     flag = 0.**  
303ef 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 32      foreach row2
303f0 20 69 6e 20 74 32 20 64 6f 0a 2a 2a 20 20 20 20   in t2 do.**    
303f1 20 20 20 20 73 74 61 72 74 3a 0a 2a 2a 20 20 20      start:.**   
303f2 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20         ....**   
303f3 20 20 20 20 20 20 20 66 6c 61 67 20 3d 20 31 0a         flag = 1.
303f4 2a 2a 20 20 20 20 20 20 65 6e 64 0a 2a 2a 20 20  **      end.**  
303f5 20 20 20 20 69 66 20 66 6c 61 67 3d 3d 30 20 74      if flag==0 t
303f6 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 6d 6f  hen.**        mo
303f7 76 65 20 74 68 65 20 72 6f 77 32 20 63 75 72 73  ve the row2 curs
303f8 6f 72 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77  or to a null row
303f9 0a 2a 2a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .**        goto 
303fa 73 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 66 69  start.**      fi
303fb 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a 0a 2a 2a  .**    end.**.**
303fc 20 4f 52 44 45 52 20 42 59 20 43 4c 41 55 53 45   ORDER BY CLAUSE
303fd 20 50 52 4f 43 45 53 53 49 4e 47 0a 2a 2a 0a 2a   PROCESSING.**.*
303fe 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20  * *ppOrderBy is 
303ff 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
30400 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
30401 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
30402 74 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66 20 74 68  tement,.** if th
30403 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 49 66 20  ere is one.  If 
30404 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45  there is no ORDE
30405 52 20 42 59 20 63 6c 61 75 73 65 20 6f 72 20 69  R BY clause or i
30406 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  f this routine.*
30407 2a 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d  * is called from
30408 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   an UPDATE or DE
30409 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 2c 20  LETE statement, 
3040a 74 68 65 6e 20 70 70 4f 72 64 65 72 42 79 20 69  then ppOrderBy i
3040b 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66  s NULL..**.** If
3040c 20 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 62 65   an index can be
3040d 20 75 73 65 64 20 73 6f 20 74 68 61 74 20 74 68   used so that th
3040e 65 20 6e 61 74 75 72 61 6c 20 6f 75 74 70 75 74  e natural output
3040f 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 74 61   order of the ta
30410 62 6c 65 0a 2a 2a 20 73 63 61 6e 20 69 73 20 63  ble.** scan is c
30411 6f 72 72 65 63 74 20 66 6f 72 20 74 68 65 20 4f  orrect for the O
30412 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20  RDER BY clause, 
30413 74 68 65 6e 20 74 68 61 74 20 69 6e 64 65 78 20  then that index 
30414 69 73 20 75 73 65 64 20 61 6e 64 0a 2a 2a 20 2a  is used and.** *
30415 70 70 4f 72 64 65 72 42 79 20 69 73 20 73 65 74  ppOrderBy is set
30416 20 74 6f 20 4e 55 4c 4c 2e 20 20 54 68 69 73 20   to NULL.  This 
30417 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  is an optimizati
30418 6f 6e 20 74 68 61 74 20 70 72 65 76 65 6e 74 73  on that prevents
30419 20 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65 73 73 61   an.** unnecessa
3041a 72 79 20 73 6f 72 74 20 6f 66 20 74 68 65 20 72  ry sort of the r
3041b 65 73 75 6c 74 20 73 65 74 20 69 66 20 61 6e 20  esult set if an 
3041c 69 6e 64 65 78 20 61 70 70 72 6f 70 72 69 61 74  index appropriat
3041d 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 4f 52 44  e for the.** ORD
3041e 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 6c 72  ER BY clause alr
3041f 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a 0a  eady exists..**.
30420 2a 2a 20 49 66 20 74 68 65 20 77 68 65 72 65 20  ** If the where 
30421 63 6c 61 75 73 65 20 6c 6f 6f 70 73 20 63 61 6e  clause loops can
30422 6e 6f 74 20 62 65 20 61 72 72 61 6e 67 65 64 20  not be arranged 
30423 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 63  to provide the c
30424 6f 72 72 65 63 74 0a 2a 2a 20 6f 75 74 70 75 74  orrect.** output
30425 20 6f 72 64 65 72 2c 20 74 68 65 6e 20 74 68 65   order, then the
30426 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 75   *ppOrderBy is u
30427 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c  nchanged..*/.SQL
30428 49 54 45 5f 50 52 49 56 41 54 45 20 57 68 65 72  ITE_PRIVATE Wher
30429 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68  eInfo *sqlite3Wh
3042a 65 72 65 42 65 67 69 6e 28 0a 20 20 50 61 72 73  ereBegin(.  Pars
3042b 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
3042c 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
3042d 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
3042e 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20  List *pTabList, 
3042f 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20     /* A list of 
30430 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 62 65  all tables to be
30431 20 73 63 61 6e 6e 65 64 20 2a 2f 0a 20 20 45 78   scanned */.  Ex
30432 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20  pr *pWhere,     
30433 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
30434 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
30435 72 4c 69 73 74 20 2a 2a 70 70 4f 72 64 65 72 42  rList **ppOrderB
30436 79 2c 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42  y, /* An ORDER B
30437 59 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e 55 4c  Y clause, or NUL
30438 4c 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c  L */.  u16 wctrl
30439 46 6c 61 67 73 20 20 20 20 20 20 20 20 2f 2a 20  Flags        /* 
3043a 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45 52 45  One of the WHERE
3043b 5f 2a 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64  _* flags defined
3043c 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
3043d 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  */.){.  int i;  
3043e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3043f 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
30440 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  er */.  int nByt
30441 65 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20  eWInfo;         
30442 20 20 20 2f 2a 20 4e 75 6d 2e 20 62 79 74 65 73     /* Num. bytes
30443 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 57   allocated for W
30444 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74 20  hereInfo struct 
30445 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
30446 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20  pWInfo;         
30447 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65 20 74  /* Will become t
30448 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
30449 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
3044a 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20   */.  Vdbe *v = 
3044b 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
3044c 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
3044d 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20  database engine 
3044e 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
3044f 52 65 61 64 79 3b 20 20 20 20 20 20 20 20 20 20  Ready;          
30450 2f 2a 20 43 75 72 73 6f 72 73 20 74 68 61 74 20  /* Cursors that 
30451 61 72 65 20 6e 6f 74 20 79 65 74 20 70 6f 73 69  are not yet posi
30452 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 57 68 65 72  tioned */.  Wher
30453 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
30454 65 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 78  et;    /* The ex
30455 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65  pression mask se
30456 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  t */.  WhereClau
30457 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20 20  se *pWC;        
30458 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70         /* Decomp
30459 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57  osition of the W
3045a 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
3045b 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
3045c 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 3b 20  item *pTabItem; 
3045d 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 65 6e 74   /* A single ent
3045e 72 79 20 66 72 6f 6d 20 70 54 61 62 4c 69 73 74  ry from pTabList
3045f 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c   */.  WhereLevel
30460 20 2a 70 4c 65 76 65 6c 3b 20 20 20 20 20 20 20   *pLevel;       
30461 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c        /* A singl
30462 65 20 6c 65 76 65 6c 20 69 6e 20 74 68 65 20 70  e level in the p
30463 57 49 6e 66 6f 20 6c 69 73 74 20 2a 2f 0a 20 20  WInfo list */.  
30464 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20  int iFrom;      
30465 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30466 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20  /* First unused 
30467 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6c 65 6d  FROM clause elem
30468 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 61 6e 64  ent */.  int and
30469 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
3046a 20 20 20 20 2f 2a 20 41 4e 44 2d 65 64 20 63 6f      /* AND-ed co
3046b 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 61 6c 6c  mbination of all
3046c 20 70 57 43 2d 3e 61 5b 5d 2e 77 74 46 6c 61 67   pWC->a[].wtFlag
3046d 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  s */.  sqlite3 *
3046e 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
3046f 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
30470 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f  nnection */..  /
30471 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
30472 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52  tables in the FR
30473 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c 69 6d  OM clause is lim
30474 69 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62  ited by the numb
30475 65 72 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20  er of.  ** bits 
30476 69 6e 20 61 20 42 69 74 6d 61 73 6b 20 0a 20 20  in a Bitmask .  
30477 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 4c 69 73  */.  if( pTabLis
30478 74 2d 3e 6e 53 72 63 3e 42 4d 53 20 29 7b 0a 20  t->nSrc>BMS ){. 
30479 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
3047a 73 67 28 70 50 61 72 73 65 2c 20 22 61 74 20 6d  sg(pParse, "at m
3047b 6f 73 74 20 25 64 20 74 61 62 6c 65 73 20 69 6e  ost %d tables in
3047c 20 61 20 6a 6f 69 6e 22 2c 20 42 4d 53 29 3b 0a   a join", BMS);.
3047d 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
3047e 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
3047f 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20   and initialize 
30480 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74  the WhereInfo st
30481 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69 6c  ructure that wil
30482 6c 20 62 65 63 6f 6d 65 20 74 68 65 0a 20 20 2a  l become the.  *
30483 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 20  * return value. 
30484 41 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74  A single allocat
30485 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 73  ion is used to s
30486 74 6f 72 65 20 74 68 65 20 57 68 65 72 65 49 6e  tore the WhereIn
30487 66 6f 0a 20 20 2a 2a 20 73 74 72 75 63 74 2c 20  fo.  ** struct, 
30488 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
30489 57 68 65 72 65 49 6e 66 6f 2e 61 5b 5d 2c 20 74  WhereInfo.a[], t
3048a 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73  he WhereClause s
3048b 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 61 6e  tructure.  ** an
3048c 64 20 74 68 65 20 57 68 65 72 65 4d 61 73 6b 53  d the WhereMaskS
3048d 65 74 20 73 74 72 75 63 74 75 72 65 2e 20 53 69  et structure. Si
3048e 6e 63 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  nce WhereClause 
3048f 63 6f 6e 74 61 69 6e 73 20 61 6e 20 38 2d 62 79  contains an 8-by
30490 74 65 0a 20 20 2a 2a 20 66 69 65 6c 64 20 28 74  te.  ** field (t
30491 79 70 65 20 42 69 74 6d 61 73 6b 29 20 69 74 20  ype Bitmask) it 
30492 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20  must be aligned 
30493 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20 62 6f 75  on an 8-byte bou
30494 6e 64 61 72 79 20 6f 6e 0a 20 20 2a 2a 20 73 6f  ndary on.  ** so
30495 6d 65 20 61 72 63 68 69 74 65 63 74 75 72 65 73  me architectures
30496 2e 20 48 65 6e 63 65 20 74 68 65 20 52 4f 55 4e  . Hence the ROUN
30497 44 38 28 29 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f  D8() below..  */
30498 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
30499 64 62 3b 0a 20 20 6e 42 79 74 65 57 49 6e 66 6f  db;.  nByteWInfo
3049a 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66   = ROUND8(sizeof
3049b 28 57 68 65 72 65 49 6e 66 6f 29 2b 28 70 54 61  (WhereInfo)+(pTa
3049c 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 29 2a 73  bList->nSrc-1)*s
3049d 69 7a 65 6f 66 28 57 68 65 72 65 4c 65 76 65 6c  izeof(WhereLevel
3049e 29 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73  ));.  pWInfo = s
3049f 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
304a0 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 6e 42  ro(db, .      nB
304a1 79 74 65 57 49 6e 66 6f 20 2b 20 0a 20 20 20 20  yteWInfo + .    
304a2 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65 43 6c    sizeof(WhereCl
304a3 61 75 73 65 29 20 2b 0a 20 20 20 20 20 20 73 69  ause) +.      si
304a4 7a 65 6f 66 28 57 68 65 72 65 4d 61 73 6b 53 65  zeof(WhereMaskSe
304a5 74 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 64 62  t).  );.  if( db
304a6 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
304a7 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65  {.    goto where
304a8 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a  BeginError;.  }.
304a9 20 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c    pWInfo->nLevel
304aa 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   = pTabList->nSr
304ab 63 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 50 61  c;.  pWInfo->pPa
304ac 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
304ad 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74  pWInfo->pTabList
304ae 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 70   = pTabList;.  p
304af 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 20 3d 20  WInfo->iBreak = 
304b0 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
304b1 61 62 65 6c 28 76 29 3b 0a 20 20 70 57 49 6e 66  abel(v);.  pWInf
304b2 6f 2d 3e 70 57 43 20 3d 20 70 57 43 20 3d 20 28  o->pWC = pWC = (
304b3 57 68 65 72 65 43 6c 61 75 73 65 20 2a 29 26 28  WhereClause *)&(
304b4 28 75 38 20 2a 29 70 57 49 6e 66 6f 29 5b 6e 42  (u8 *)pWInfo)[nB
304b5 79 74 65 57 49 6e 66 6f 5d 3b 0a 20 20 70 57 49  yteWInfo];.  pWI
304b6 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20  nfo->wctrlFlags 
304b7 3d 20 77 63 74 72 6c 46 6c 61 67 73 3b 0a 20 20  = wctrlFlags;.  
304b8 70 4d 61 73 6b 53 65 74 20 3d 20 28 57 68 65 72  pMaskSet = (Wher
304b9 65 4d 61 73 6b 53 65 74 2a 29 26 70 57 43 5b 31  eMaskSet*)&pWC[1
304ba 5d 3b 0a 0a 20 20 2f 2a 20 53 70 6c 69 74 20 74  ];..  /* Split t
304bb 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
304bc 69 6e 74 6f 20 73 65 70 61 72 61 74 65 20 73 75  into separate su
304bd 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68 65  bexpressions whe
304be 72 65 20 65 61 63 68 0a 20 20 2a 2a 20 73 75 62  re each.  ** sub
304bf 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65  expression is se
304c0 70 61 72 61 74 65 64 20 62 79 20 61 6e 20 41 4e  parated by an AN
304c1 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2f  D operator..  */
304c2 0a 20 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 70  .  initMaskSet(p
304c3 4d 61 73 6b 53 65 74 29 3b 0a 20 20 77 68 65 72  MaskSet);.  wher
304c4 65 43 6c 61 75 73 65 49 6e 69 74 28 70 57 43 2c  eClauseInit(pWC,
304c5 20 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65   pParse, pMaskSe
304c6 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  t);.  sqlite3Exp
304c7 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 70  rCodeConstants(p
304c8 50 61 72 73 65 2c 20 70 57 68 65 72 65 29 3b 0a  Parse, pWhere);.
304c9 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 57 43    whereSplit(pWC
304ca 2c 20 70 57 68 65 72 65 2c 20 54 4b 5f 41 4e 44  , pWhere, TK_AND
304cb 29 3b 0a 20 20 20 20 0a 20 20 2f 2a 20 53 70 65  );.    .  /* Spe
304cc 63 69 61 6c 20 63 61 73 65 3a 20 61 20 57 48 45  cial case: a WHE
304cd 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 69  RE clause that i
304ce 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20 45 76 61  s constant.  Eva
304cf 6c 75 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 65  luate the.  ** e
304d0 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 65 69  xpression and ei
304d1 74 68 65 72 20 6a 75 6d 70 20 6f 76 65 72 20 61  ther jump over a
304d2 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20 6f  ll of the code o
304d3 72 20 66 61 6c 6c 20 74 68 72 75 2e 0a 20 20 2a  r fall thru..  *
304d4 2f 0a 20 20 69 66 28 20 70 57 68 65 72 65 20 26  /.  if( pWhere &
304d5 26 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  & (pTabList->nSr
304d6 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 45  c==0 || sqlite3E
304d7 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74  xprIsConstantNot
304d8 4a 6f 69 6e 28 70 57 68 65 72 65 29 29 20 29 7b  Join(pWhere)) ){
304d9 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
304da 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
304db 70 57 68 65 72 65 2c 20 70 57 49 6e 66 6f 2d 3e  pWhere, pWInfo->
304dc 69 42 72 65 61 6b 2c 20 53 51 4c 49 54 45 5f 4a  iBreak, SQLITE_J
304dd 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
304de 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 7d 0a  pWhere = 0;.  }.
304df 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 61 20 62  .  /* Assign a b
304e0 69 74 20 66 72 6f 6d 20 74 68 65 20 62 69 74 6d  it from the bitm
304e1 61 73 6b 20 74 6f 20 65 76 65 72 79 20 74 65 72  ask to every ter
304e2 6d 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  m in the FROM cl
304e3 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ause..  **.  ** 
304e4 57 68 65 6e 20 61 73 73 69 67 6e 69 6e 67 20 62  When assigning b
304e5 69 74 6d 61 73 6b 20 76 61 6c 75 65 73 20 74 6f  itmask values to
304e6 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 63 75 72   FROM clause cur
304e7 73 6f 72 73 2c 20 69 74 20 6d 75 73 74 20 62 65  sors, it must be
304e8 0a 20 20 2a 2a 20 74 68 65 20 63 61 73 65 20 74  .  ** the case t
304e9 68 61 74 20 69 66 20 58 20 69 73 20 74 68 65 20  hat if X is the 
304ea 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68 65 20  bitmask for the 
304eb 4e 2d 74 68 20 46 52 4f 4d 20 63 6c 61 75 73 65  N-th FROM clause
304ec 20 74 65 72 6d 20 74 68 65 6e 0a 20 20 2a 2a 20   term then.  ** 
304ed 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20  the bitmask for 
304ee 61 6c 6c 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  all FROM clause 
304ef 74 65 72 6d 73 20 74 6f 20 74 68 65 20 6c 65 66  terms to the lef
304f0 74 20 6f 66 20 74 68 65 20 4e 2d 74 68 20 74 65  t of the N-th te
304f1 72 6d 0a 20 20 2a 2a 20 69 73 20 28 58 2d 31 29  rm.  ** is (X-1)
304f2 2e 20 20 20 41 6e 20 65 78 70 72 65 73 73 69 6f  .   An expressio
304f3 6e 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 63 6c  n from the ON cl
304f4 61 75 73 65 20 6f 66 20 61 20 4c 45 46 54 20 4a  ause of a LEFT J
304f5 4f 49 4e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a  OIN can use.  **
304f6 20 69 74 73 20 45 78 70 72 2e 69 52 69 67 68 74   its Expr.iRight
304f7 4a 6f 69 6e 54 61 62 6c 65 20 76 61 6c 75 65 20  JoinTable value 
304f8 74 6f 20 66 69 6e 64 20 74 68 65 20 62 69 74 6d  to find the bitm
304f9 61 73 6b 20 6f 66 20 74 68 65 20 72 69 67 68 74  ask of the right
304fa 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 66 20 74   table.  ** of t
304fb 68 65 20 6a 6f 69 6e 2e 20 20 53 75 62 74 72 61  he join.  Subtra
304fc 63 74 69 6e 67 20 6f 6e 65 20 66 72 6f 6d 20 74  cting one from t
304fd 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 62  he right table b
304fe 69 74 6d 61 73 6b 20 67 69 76 65 73 20 61 0a 20  itmask gives a. 
304ff 20 2a 2a 20 62 69 74 6d 61 73 6b 20 66 6f 72 20   ** bitmask for 
30500 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 74 68  all tables to th
30501 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 6a 6f  e left of the jo
30502 69 6e 2e 20 20 4b 6e 6f 77 69 6e 67 20 74 68 65  in.  Knowing the
30503 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20 66 6f   bitmask.  ** fo
30504 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20  r all tables to 
30505 74 68 65 20 6c 65 66 74 20 6f 66 20 61 20 6c 65  the left of a le
30506 66 74 20 6a 6f 69 6e 20 69 73 20 69 6d 70 6f 72  ft join is impor
30507 74 61 6e 74 2e 20 20 54 69 63 6b 65 74 20 23 33  tant.  Ticket #3
30508 30 31 35 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43  015..  **.  ** C
30509 6f 6e 66 69 67 75 72 65 20 74 68 65 20 57 68 65  onfigure the Whe
3050a 72 65 43 6c 61 75 73 65 2e 76 6d 61 73 6b 20 76  reClause.vmask v
3050b 61 72 69 61 62 6c 65 20 73 6f 20 74 68 61 74 20  ariable so that 
3050c 62 69 74 73 20 74 68 61 74 20 63 6f 72 72 65 73  bits that corres
3050d 70 6f 6e 64 0a 20 20 2a 2a 20 74 6f 20 76 69 72  pond.  ** to vir
3050e 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73 6f  tual table curso
3050f 72 73 20 61 72 65 20 73 65 74 2e 20 54 68 69 73  rs are set. This
30510 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 6c 65   is used to sele
30511 63 74 69 76 65 6c 79 20 64 69 73 61 62 6c 65 20  ctively disable 
30512 0a 20 20 2a 2a 20 74 68 65 20 4f 52 2d 74 6f 2d  .  ** the OR-to-
30513 49 4e 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f  IN transformatio
30514 6e 20 69 6e 20 65 78 70 72 41 6e 61 6c 79 7a 65  n in exprAnalyze
30515 4f 72 54 65 72 6d 28 29 2e 20 49 74 20 69 73 20  OrTerm(). It is 
30516 6e 6f 74 20 68 65 6c 70 66 75 6c 20 0a 20 20 2a  not helpful .  *
30517 2a 20 77 69 74 68 20 76 69 72 74 75 61 6c 20 74  * with virtual t
30518 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73  ables..  */.  as
30519 73 65 72 74 28 20 70 57 43 2d 3e 76 6d 61 73 6b  sert( pWC->vmask
3051a 3d 3d 30 20 26 26 20 70 4d 61 73 6b 53 65 74 2d  ==0 && pMaskSet-
3051b 3e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69  >n==0 );.  for(i
3051c 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  =0; i<pTabList->
3051d 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSrc; i++){.    
3051e 63 72 65 61 74 65 4d 61 73 6b 28 70 4d 61 73 6b  createMask(pMask
3051f 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61  Set, pTabList->a
30520 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 23 69  [i].iCursor);.#i
30521 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
30522 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
30523 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 54     if( ALWAYS(pT
30524 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54 61  abList->a[i].pTa
30525 62 29 20 26 26 20 49 73 56 69 72 74 75 61 6c 28  b) && IsVirtual(
30526 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  pTabList->a[i].p
30527 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 70 57  Tab) ){.      pW
30528 43 2d 3e 76 6d 61 73 6b 20 7c 3d 20 28 28 42 69  C->vmask |= ((Bi
30529 74 6d 61 73 6b 29 31 20 3c 3c 20 69 29 3b 0a 20  tmask)1 << i);. 
3052a 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
3052b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
3052c 20 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 74   {.    Bitmask t
3052d 6f 54 68 65 4c 65 66 74 20 3d 20 30 3b 0a 20 20  oTheLeft = 0;.  
3052e 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
3052f 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
30530 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b  ){.      Bitmask
30531 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61   m = getMask(pMa
30532 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d  skSet, pTabList-
30533 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a  >a[i].iCursor);.
30534 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 6d        assert( (m
30535 2d 31 29 3d 3d 74 6f 54 68 65 4c 65 66 74 20 29  -1)==toTheLeft )
30536 3b 0a 20 20 20 20 20 20 74 6f 54 68 65 4c 65 66  ;.      toTheLef
30537 74 20 7c 3d 20 6d 3b 0a 20 20 20 20 7d 0a 20 20  t |= m;.    }.  
30538 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41  }.#endif..  /* A
30539 6e 61 6c 79 7a 65 20 61 6c 6c 20 6f 66 20 74 68  nalyze all of th
3053a 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  e subexpressions
3053b 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 65 78 70  .  Note that exp
3053c 72 41 6e 61 6c 79 7a 65 28 29 20 6d 69 67 68 74  rAnalyze() might
3053d 0a 20 20 2a 2a 20 61 64 64 20 6e 65 77 20 76 69  .  ** add new vi
3053e 72 74 75 61 6c 20 74 65 72 6d 73 20 6f 6e 74 6f  rtual terms onto
3053f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
30540 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 57  WHERE clause.  W
30541 65 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 77 61  e do not.  ** wa
30542 6e 74 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68  nt to analyze th
30543 65 73 65 20 76 69 72 74 75 61 6c 20 74 65 72 6d  ese virtual term
30544 73 2c 20 73 6f 20 73 74 61 72 74 20 61 6e 61 6c  s, so start anal
30545 79 7a 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64  yzing at the end
30546 0a 20 20 2a 2a 20 61 6e 64 20 77 6f 72 6b 20 66  .  ** and work f
30547 6f 72 77 61 72 64 20 73 6f 20 74 68 61 74 20 74  orward so that t
30548 68 65 20 61 64 64 65 64 20 76 69 72 74 75 61 6c  he added virtual
30549 20 74 65 72 6d 73 20 61 72 65 20 6e 65 76 65 72   terms are never
3054a 20 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2f   processed..  */
3054b 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c  .  exprAnalyzeAl
3054c 6c 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43 29  l(pTabList, pWC)
3054d 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
3054e 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
3054f 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45  goto whereBeginE
30550 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rror;.  }..  /* 
30551 43 68 6f 73 65 20 74 68 65 20 62 65 73 74 20 69  Chose the best i
30552 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20  ndex to use for 
30553 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74 68  each table in th
30554 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20  e FROM clause.. 
30555 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f   **.  ** This lo
30556 6f 70 20 66 69 6c 6c 73 20 69 6e 20 74 68 65 20  op fills in the 
30557 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73  following fields
30558 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 70 57  :.  **.  **   pW
30559 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78 20 20  Info->a[].pIdx  
3055a 20 20 20 20 54 68 65 20 69 6e 64 65 78 20 74 6f      The index to
3055b 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 6c 65   use for this le
3055c 76 65 6c 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  vel of the loop.
3055d 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
3055e 61 5b 5d 2e 77 73 46 6c 61 67 73 20 20 20 57 48  a[].wsFlags   WH
3055f 45 52 45 5f 78 78 78 20 66 6c 61 67 73 20 61 73  ERE_xxx flags as
30560 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 49  sociated with pI
30561 64 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  dx.  **   pWInfo
30562 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20 20 20 20 20  ->a[].nEq       
30563 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d  The number of ==
30564 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69   and IN constrai
30565 6e 74 73 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66  nts.  **   pWInf
30566 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20 20 20  o->a[].iFrom    
30567 20 57 68 69 63 68 20 74 65 72 6d 20 6f 66 20 74   Which term of t
30568 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69  he FROM clause i
30569 73 20 62 65 69 6e 67 20 63 6f 64 65 64 0a 20 20  s being coded.  
3056a 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  **   pWInfo->a[]
3056b 2e 69 54 61 62 43 75 72 20 20 20 54 68 65 20 56  .iTabCur   The V
3056c 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
3056d 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
3056e 65 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  e.  **   pWInfo-
3056f 3e 61 5b 5d 2e 69 49 64 78 43 75 72 20 20 20 54  >a[].iIdxCur   T
30570 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66  he VDBE cursor f
30571 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a  or the index.  *
30572 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e  *   pWInfo->a[].
30573 70 54 65 72 6d 20 20 20 20 20 57 68 65 6e 20 77  pTerm     When w
30574 73 46 6c 61 67 73 3d 3d 57 4f 5f 4f 52 2c 20 74  sFlags==WO_OR, t
30575 68 65 20 4f 52 2d 63 6c 61 75 73 65 20 74 65 72  he OR-clause ter
30576 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  m.  **.  ** This
30577 20 6c 6f 6f 70 20 61 6c 73 6f 20 66 69 67 75 72   loop also figur
30578 65 73 20 6f 75 74 20 74 68 65 20 6e 65 73 74 69  es out the nesti
30579 6e 67 20 6f 72 64 65 72 20 6f 66 20 74 61 62 6c  ng order of tabl
3057a 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20  es in the FROM. 
3057b 20 2a 2a 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f   ** clause..  */
3057c 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28  .  notReady = ~(
3057d 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 70 54 61  Bitmask)0;.  pTa
3057e 62 49 74 65 6d 20 3d 20 70 54 61 62 4c 69 73 74  bItem = pTabList
3057f 2d 3e 61 3b 0a 20 20 70 4c 65 76 65 6c 20 3d 20  ->a;.  pLevel = 
30580 70 57 49 6e 66 6f 2d 3e 61 3b 0a 20 20 61 6e 64  pWInfo->a;.  and
30581 46 6c 61 67 73 20 3d 20 7e 30 3b 0a 20 20 57 48  Flags = ~0;.  WH
30582 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f  ERETRACE(("*** O
30583 70 74 69 6d 69 7a 65 72 20 53 74 61 72 74 20 2a  ptimizer Start *
30584 2a 2a 5c 6e 22 29 29 3b 0a 20 20 66 6f 72 28 69  **\n"));.  for(i
30585 3d 69 46 72 6f 6d 3d 30 2c 20 70 4c 65 76 65 6c  =iFrom=0, pLevel
30586 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54  =pWInfo->a; i<pT
30587 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
30588 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20  +, pLevel++){.  
30589 20 20 57 68 65 72 65 43 6f 73 74 20 62 65 73 74    WhereCost best
3058a 50 6c 61 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  Plan;         /*
3058b 20 4d 6f 73 74 20 65 66 66 69 63 69 65 6e 74 20   Most efficient 
3058c 70 6c 61 6e 20 73 65 65 6e 20 73 6f 20 66 61 72  plan seen so far
3058d 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   */.    Index *p
3058e 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
3058f 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 66 6f 72      /* Index for
30590 20 46 52 4f 4d 20 74 61 62 6c 65 20 61 74 20 70   FROM table at p
30591 54 61 62 49 74 65 6d 20 2a 2f 0a 20 20 20 20 69  TabItem */.    i
30592 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  nt j;           
30593 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
30594 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 46  r looping over F
30595 52 4f 4d 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20  ROM tables */.  
30596 20 20 69 6e 74 20 62 65 73 74 4a 20 3d 20 2d 31    int bestJ = -1
30597 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
30598 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6a 20   The value of j 
30599 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 6d  */.    Bitmask m
3059a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3059b 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 76 61     /* Bitmask va
3059c 6c 75 65 20 66 6f 72 20 6a 20 6f 72 20 62 65 73  lue for j or bes
3059d 74 4a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 73  tJ */.    int is
3059e 4f 70 74 69 6d 61 6c 3b 20 20 20 20 20 20 20 20  Optimal;        
3059f 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
305a0 72 20 66 6f 72 20 6f 70 74 69 6d 61 6c 2f 6e 6f  r for optimal/no
305a1 6e 2d 6f 70 74 69 6d 61 6c 20 73 65 61 72 63 68  n-optimal search
305a2 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28   */..    memset(
305a3 26 62 65 73 74 50 6c 61 6e 2c 20 30 2c 20 73 69  &bestPlan, 0, si
305a4 7a 65 6f 66 28 62 65 73 74 50 6c 61 6e 29 29 3b  zeof(bestPlan));
305a5 0a 20 20 20 20 62 65 73 74 50 6c 61 6e 2e 72 43  .    bestPlan.rC
305a6 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47  ost = SQLITE_BIG
305a7 5f 44 42 4c 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f  _DBL;..    /* Lo
305a8 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 72  op through the r
305a9 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 73  emaining entries
305aa 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
305ab 75 73 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0a  use to find the.
305ac 20 20 20 20 2a 2a 20 6e 65 78 74 20 6e 65 73 74      ** next nest
305ad 65 64 20 6c 6f 6f 70 2e 20 54 68 65 20 46 52 4f  ed loop. The FRO
305ae 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 69 65 73  M clause entries
305af 20 6d 61 79 20 62 65 20 69 74 65 72 61 74 65 64   may be iterated
305b0 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20   through.    ** 
305b1 65 69 74 68 65 72 20 6f 6e 63 65 20 6f 72 20 74  either once or t
305b2 77 69 63 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20  wice. .    **.  
305b3 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 69    ** The first i
305b4 74 65 72 61 74 69 6f 6e 2c 20 77 68 69 63 68 20  teration, which 
305b5 69 73 20 61 6c 77 61 79 73 20 70 65 72 66 6f 72  is always perfor
305b6 6d 65 64 2c 20 73 65 61 72 63 68 65 73 20 66 6f  med, searches fo
305b7 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 46 52 4f  r the.    ** FRO
305b8 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20 74  M clause entry t
305b9 68 61 74 20 70 65 72 6d 69 74 73 20 74 68 65 20  hat permits the 
305ba 6c 6f 77 65 73 74 2d 63 6f 73 74 2c 20 22 6f 70  lowest-cost, "op
305bb 74 69 6d 61 6c 22 20 73 63 61 6e 2e 20 49 6e 0a  timal" scan. In.
305bc 20 20 20 20 2a 2a 20 74 68 69 73 20 63 6f 6e 74      ** this cont
305bd 65 78 74 20 61 6e 20 6f 70 74 69 6d 61 6c 20 73  ext an optimal s
305be 63 61 6e 20 69 73 20 6f 6e 65 20 74 68 61 74 20  can is one that 
305bf 75 73 65 73 20 74 68 65 20 73 61 6d 65 20 73 74  uses the same st
305c0 72 61 74 65 67 79 0a 20 20 20 20 2a 2a 20 66 6f  rategy.    ** fo
305c1 72 20 74 68 65 20 67 69 76 65 6e 20 46 52 4f 4d  r the given FROM
305c2 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20 61 73   clause entry as
305c3 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74   would be select
305c4 65 64 20 69 66 20 74 68 65 20 65 6e 74 72 79 0a  ed if the entry.
305c5 20 20 20 20 2a 2a 20 77 65 72 65 20 75 73 65 64      ** were used
305c6 20 61 73 20 74 68 65 20 69 6e 6e 65 72 6d 6f 73   as the innermos
305c7 74 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2e 20 20  t nested loop.  
305c8 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
305c9 61 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69  a table.    ** i
305ca 73 20 63 68 6f 73 65 6e 20 73 75 63 68 20 74 68  s chosen such th
305cb 61 74 20 74 68 65 20 63 6f 73 74 20 6f 66 20 72  at the cost of r
305cc 75 6e 6e 69 6e 67 20 74 68 61 74 20 74 61 62 6c  unning that tabl
305cd 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 64 75  e cannot be redu
305ce 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 77 61  ced.    ** by wa
305cf 69 74 69 6e 67 20 66 6f 72 20 6f 74 68 65 72 20  iting for other 
305d0 74 61 62 6c 65 73 20 74 6f 20 72 75 6e 20 66 69  tables to run fi
305d1 72 73 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  rst..    **.    
305d2 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 69 74  ** The second it
305d3 65 72 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  eration is only 
305d4 70 65 72 66 6f 72 6d 65 64 20 69 66 20 6e 6f 20  performed if no 
305d5 6f 70 74 69 6d 61 6c 20 73 63 61 6e 20 73 74 72  optimal scan str
305d6 61 74 65 67 69 65 73 0a 20 20 20 20 2a 2a 20 77  ategies.    ** w
305d7 65 72 65 20 66 6f 75 6e 64 20 62 79 20 74 68 65  ere found by the
305d8 20 66 69 72 73 74 2e 20 54 68 69 73 20 69 74 65   first. This ite
305d9 72 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  ration is used t
305da 6f 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65  o search for the
305db 0a 20 20 20 20 2a 2a 20 6c 6f 77 65 73 74 20 63  .    ** lowest c
305dc 6f 73 74 20 73 63 61 6e 20 6f 76 65 72 61 6c 6c  ost scan overall
305dd 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
305de 50 72 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e  Previous version
305df 73 20 6f 66 20 53 51 4c 69 74 65 20 70 65 72 66  s of SQLite perf
305e0 6f 72 6d 65 64 20 6f 6e 6c 79 20 74 68 65 20 73  ormed only the s
305e1 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 20  econd iteration 
305e2 2d 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78  -.    ** the nex
305e3 74 20 6f 75 74 65 72 6d 6f 73 74 20 6c 6f 6f 70  t outermost loop
305e4 20 77 61 73 20 61 6c 77 61 79 73 20 74 68 61 74   was always that
305e5 20 77 69 74 68 20 74 68 65 20 6c 6f 77 65 73 74   with the lowest
305e6 20 6f 76 65 72 61 6c 6c 0a 20 20 20 20 2a 2a 20   overall.    ** 
305e7 63 6f 73 74 2e 20 48 6f 77 65 76 65 72 2c 20 74  cost. However, t
305e8 68 69 73 20 6d 65 61 6e 74 20 74 68 61 74 20 53  his meant that S
305e9 51 4c 69 74 65 20 63 6f 75 6c 64 20 73 65 6c 65  QLite could sele
305ea 63 74 20 74 68 65 20 77 72 6f 6e 67 20 70 6c 61  ct the wrong pla
305eb 6e 0a 20 20 20 20 2a 2a 20 66 6f 72 20 73 63 72  n.    ** for scr
305ec 69 70 74 73 20 73 75 63 68 20 61 73 20 74 68 65  ipts such as the
305ed 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20   following:.    
305ee 2a 2a 20 20 20 0a 20 20 20 20 2a 2a 20 20 20 43  **   .    **   C
305ef 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
305f0 2c 20 62 29 3b 20 0a 20 20 20 20 2a 2a 20 20 20  , b); .    **   
305f1 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28  CREATE TABLE t2(
305f2 63 2c 20 64 29 3b 0a 20 20 20 20 2a 2a 20 20 20  c, d);.    **   
305f3 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 32  SELECT * FROM t2
305f4 2c 20 74 31 20 57 48 45 52 45 20 74 32 2e 72 6f  , t1 WHERE t2.ro
305f5 77 69 64 20 3d 20 74 31 2e 61 3b 0a 20 20 20 20  wid = t1.a;.    
305f6 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 62 65  **.    ** The be
305f7 73 74 20 73 74 72 61 74 65 67 79 20 69 73 20 74  st strategy is t
305f8 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
305f9 68 20 74 61 62 6c 65 20 74 31 20 66 69 72 73 74  h table t1 first
305fa 2e 20 48 6f 77 65 76 65 72 20 69 74 0a 20 20 20  . However it.   
305fb 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   ** is not possi
305fc 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ble to determine
305fd 20 74 68 69 73 20 77 69 74 68 20 61 20 73 69 6d   this with a sim
305fe 70 6c 65 20 67 72 65 65 64 79 20 61 6c 67 6f 72  ple greedy algor
305ff 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 20 48 6f 77  ithm..    ** How
30600 65 76 65 72 2c 20 73 69 6e 63 65 20 74 68 65 20  ever, since the 
30601 63 6f 73 74 20 6f 66 20 61 20 6c 69 6e 65 61 72  cost of a linear
30602 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 61   scan through ta
30603 62 6c 65 20 74 32 20 69 73 20 74 68 65 20 73 61  ble t2 is the sa
30604 6d 65 20 0a 20 20 20 20 2a 2a 20 61 73 20 74 68  me .    ** as th
30605 65 20 63 6f 73 74 20 6f 66 20 61 20 6c 69 6e 65  e cost of a line
30606 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20  ar scan through 
30607 74 61 62 6c 65 20 74 31 2c 20 61 20 73 69 6d 70  table t1, a simp
30608 6c 65 20 67 72 65 65 64 79 20 0a 20 20 20 20 2a  le greedy .    *
30609 2a 20 61 6c 67 6f 72 69 74 68 6d 20 6d 61 79 20  * algorithm may 
3060a 63 68 6f 6f 73 65 20 74 6f 20 75 73 65 20 74 32  choose to use t2
3060b 20 66 6f 72 20 74 68 65 20 6f 75 74 65 72 20 6c   for the outer l
3060c 6f 6f 70 2c 20 77 68 69 63 68 20 69 73 20 61 20  oop, which is a 
3060d 6d 75 63 68 0a 20 20 20 20 2a 2a 20 63 6f 73 74  much.    ** cost
3060e 6c 69 65 72 20 61 70 70 72 6f 61 63 68 2e 0a 20  lier approach.. 
3060f 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 73     */.    for(is
30610 4f 70 74 69 6d 61 6c 3d 31 3b 20 69 73 4f 70 74  Optimal=1; isOpt
30611 69 6d 61 6c 3e 3d 30 20 26 26 20 62 65 73 74 4a  imal>=0 && bestJ
30612 3c 30 3b 20 69 73 4f 70 74 69 6d 61 6c 2d 2d 29  <0; isOptimal--)
30613 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20  {.      Bitmask 
30614 6d 61 73 6b 20 3d 20 28 69 73 4f 70 74 69 6d 61  mask = (isOptima
30615 6c 20 3f 20 30 20 3a 20 6e 6f 74 52 65 61 64 79  l ? 0 : notReady
30616 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
30617 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63   (pTabList->nSrc
30618 2d 69 46 72 6f 6d 29 3e 31 20 7c 7c 20 69 73 4f  -iFrom)>1 || isO
30619 70 74 69 6d 61 6c 20 29 3b 0a 20 20 20 20 20 20  ptimal );.      
3061a 66 6f 72 28 6a 3d 69 46 72 6f 6d 2c 20 70 54 61  for(j=iFrom, pTa
3061b 62 49 74 65 6d 3d 26 70 54 61 62 4c 69 73 74 2d  bItem=&pTabList-
3061c 3e 61 5b 6a 5d 3b 20 6a 3c 70 54 61 62 4c 69 73  >a[j]; j<pTabLis
3061d 74 2d 3e 6e 53 72 63 3b 20 6a 2b 2b 2c 20 70 54  t->nSrc; j++, pT
3061e 61 62 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20  abItem++){.     
3061f 20 20 20 69 6e 74 20 64 6f 4e 6f 74 52 65 6f 72     int doNotReor
30620 64 65 72 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  der;    /* True 
30621 69 66 20 74 68 69 73 20 74 61 62 6c 65 20 73 68  if this table sh
30622 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 6f 72  ould not be reor
30623 64 65 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20  dered */.       
30624 20 57 68 65 72 65 43 6f 73 74 20 73 43 6f 73 74   WhereCost sCost
30625 3b 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 69 6e  ;     /* Cost in
30626 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 62  formation from b
30627 65 73 74 5b 56 69 72 74 75 61 6c 5d 49 6e 64 65  est[Virtual]Inde
30628 78 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 45  x() */.        E
30629 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
3062a 79 3b 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 20  y;  /* ORDER BY 
3062b 63 6c 61 75 73 65 20 66 6f 72 20 69 6e 64 65 78  clause for index
3062c 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 2a 2f 0a   to optimize */.
3062d 20 20 0a 20 20 20 20 20 20 20 20 64 6f 4e 6f 74    .        doNot
3062e 52 65 6f 72 64 65 72 20 3d 20 20 28 70 54 61 62  Reorder =  (pTab
3062f 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26  Item->jointype &
30630 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 43 52 4f   (JT_LEFT|JT_CRO
30631 53 53 29 29 21 3d 30 3b 0a 20 20 20 20 20 20 20  SS))!=0;.       
30632 20 69 66 28 20 6a 21 3d 69 46 72 6f 6d 20 26 26   if( j!=iFrom &&
30633 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29 20   doNotReorder ) 
30634 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 6d  break;.        m
30635 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b   = getMask(pMask
30636 53 65 74 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69  Set, pTabItem->i
30637 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20  Cursor);.       
30638 20 69 66 28 20 28 6d 20 26 20 6e 6f 74 52 65 61   if( (m & notRea
30639 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  dy)==0 ){.      
3063a 20 20 20 20 69 66 28 20 6a 3d 3d 69 46 72 6f 6d      if( j==iFrom
3063b 20 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20 20 20 20   ) iFrom++;.    
3063c 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
3063d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3063e 20 20 70 4f 72 64 65 72 42 79 20 3d 20 28 28 69    pOrderBy = ((i
3063f 3d 3d 30 20 26 26 20 70 70 4f 72 64 65 72 42 79  ==0 && ppOrderBy
30640 20 29 3f 2a 70 70 4f 72 64 65 72 42 79 3a 30 29   )?*ppOrderBy:0)
30641 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 61 73 73  ;.  .        ass
30642 65 72 74 28 20 70 54 61 62 49 74 65 6d 2d 3e 70  ert( pTabItem->p
30643 54 61 62 20 29 3b 0a 23 69 66 6e 64 65 66 20 53  Tab );.#ifndef S
30644 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
30645 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 20 20  ALTABLE.        
30646 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
30647 61 62 49 74 65 6d 2d 3e 70 54 61 62 29 20 29 7b  abItem->pTab) ){
30648 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
30649 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a  e3_index_info **
3064a 70 70 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b  pp = &pWInfo->a[
3064b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20 20  j].pIdxInfo;.   
3064c 20 20 20 20 20 20 20 62 65 73 74 56 69 72 74 75         bestVirtu
3064d 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  alIndex(pParse, 
3064e 70 57 43 2c 20 70 54 61 62 49 74 65 6d 2c 20 6d  pWC, pTabItem, m
3064f 61 73 6b 2c 20 70 4f 72 64 65 72 42 79 2c 20 26  ask, pOrderBy, &
30650 73 43 6f 73 74 2c 20 70 70 29 3b 0a 20 20 20 20  sCost, pp);.    
30651 20 20 20 20 7d 65 6c 73 65 20 0a 23 65 6e 64 69      }else .#endi
30652 66 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20  f.        {.    
30653 20 20 20 20 20 20 62 65 73 74 42 74 72 65 65 49        bestBtreeI
30654 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43  ndex(pParse, pWC
30655 2c 20 70 54 61 62 49 74 65 6d 2c 20 6d 61 73 6b  , pTabItem, mask
30656 2c 20 70 4f 72 64 65 72 42 79 2c 20 26 73 43 6f  , pOrderBy, &sCo
30657 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  st);.        }. 
30658 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69         assert( i
30659 73 4f 70 74 69 6d 61 6c 20 7c 7c 20 28 73 43 6f  sOptimal || (sCo
3065a 73 74 2e 75 73 65 64 26 6e 6f 74 52 65 61 64 79  st.used&notReady
3065b 29 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20  )==0 );..       
3065c 20 69 66 28 20 28 73 43 6f 73 74 2e 75 73 65 64   if( (sCost.used
3065d 26 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20 20  &notReady)==0.  
3065e 20 20 20 20 20 20 20 26 26 20 28 6a 3d 3d 69 46         && (j==iF
3065f 72 6f 6d 20 7c 7c 20 73 43 6f 73 74 2e 72 43 6f  rom || sCost.rCo
30660 73 74 3c 62 65 73 74 50 6c 61 6e 2e 72 43 6f 73  st<bestPlan.rCos
30661 74 29 20 0a 20 20 20 20 20 20 20 20 29 7b 0a 20  t) .        ){. 
30662 20 20 20 20 20 20 20 20 20 62 65 73 74 50 6c 61           bestPla
30663 6e 20 3d 20 73 43 6f 73 74 3b 0a 20 20 20 20 20  n = sCost;.     
30664 20 20 20 20 20 62 65 73 74 4a 20 3d 20 6a 3b 0a       bestJ = j;.
30665 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
30666 20 20 69 66 28 20 64 6f 4e 6f 74 52 65 6f 72 64    if( doNotReord
30667 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  er ) break;.    
30668 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
30669 73 65 72 74 28 20 62 65 73 74 4a 3e 3d 30 20 29  sert( bestJ>=0 )
3066a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 6f  ;.    assert( no
3066b 74 52 65 61 64 79 20 26 20 67 65 74 4d 61 73 6b  tReady & getMask
3066c 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c  (pMaskSet, pTabL
3066d 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69 43  ist->a[bestJ].iC
3066e 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 57 48  ursor) );.    WH
3066f 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f  ERETRACE(("*** O
30670 70 74 69 6d 69 7a 65 72 20 73 65 6c 65 63 74 73  ptimizer selects
30671 20 74 61 62 6c 65 20 25 64 20 66 6f 72 20 6c 6f   table %d for lo
30672 6f 70 20 25 64 5c 6e 22 2c 20 62 65 73 74 4a 2c  op %d\n", bestJ,
30673 0a 20 20 20 20 20 20 20 20 20 20 20 70 4c 65 76  .           pLev
30674 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61 29 29 3b 0a  el-pWInfo->a));.
30675 20 20 20 20 69 66 28 20 28 62 65 73 74 50 6c 61      if( (bestPla
30676 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  n.plan.wsFlags &
30677 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 29 21   WHERE_ORDERBY)!
30678 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 4f  =0 ){.      *ppO
30679 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20  rderBy = 0;.    
3067a 7d 0a 20 20 20 20 61 6e 64 46 6c 61 67 73 20 26  }.    andFlags &
3067b 3d 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e  = bestPlan.plan.
3067c 77 73 46 6c 61 67 73 3b 0a 20 20 20 20 70 4c 65  wsFlags;.    pLe
3067d 76 65 6c 2d 3e 70 6c 61 6e 20 3d 20 62 65 73 74  vel->plan = best
3067e 50 6c 61 6e 2e 70 6c 61 6e 3b 0a 20 20 20 20 69  Plan.plan;.    i
3067f 66 28 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e  f( bestPlan.plan
30680 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30681 5f 49 4e 44 45 58 45 44 20 29 7b 0a 20 20 20 20  _INDEXED ){.    
30682 20 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75    pLevel->iIdxCu
30683 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
30684 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ++;.    }else{. 
30685 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49 64       pLevel->iId
30686 78 43 75 72 20 3d 20 2d 31 3b 0a 20 20 20 20 7d  xCur = -1;.    }
30687 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20 26 3d  .    notReady &=
30688 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53   ~getMask(pMaskS
30689 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  et, pTabList->a[
3068a 62 65 73 74 4a 5d 2e 69 43 75 72 73 6f 72 29 3b  bestJ].iCursor);
3068b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 46 72  .    pLevel->iFr
3068c 6f 6d 20 3d 20 28 75 38 29 62 65 73 74 4a 3b 0a  om = (u8)bestJ;.
3068d 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
3068e 61 74 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  at if the table 
3068f 73 63 61 6e 6e 65 64 20 62 79 20 74 68 69 73 20  scanned by this 
30690 6c 6f 6f 70 20 69 74 65 72 61 74 69 6f 6e 20 68  loop iteration h
30691 61 64 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4e 44  ad an.    ** IND
30692 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 61  EXED BY clause a
30693 74 74 61 63 68 65 64 20 74 6f 20 69 74 2c 20 74  ttached to it, t
30694 68 61 74 20 74 68 65 20 6e 61 6d 65 64 20 69 6e  hat the named in
30695 64 65 78 20 69 73 20 62 65 69 6e 67 0a 20 20 20  dex is being.   
30696 20 2a 2a 20 75 73 65 64 20 66 6f 72 20 74 68 65   ** used for the
30697 20 73 63 61 6e 2e 20 49 66 20 6e 6f 74 2c 20 74   scan. If not, t
30698 68 65 6e 20 71 75 65 72 79 20 63 6f 6d 70 69 6c  hen query compil
30699 61 74 69 6f 6e 20 68 61 73 20 66 61 69 6c 65 64  ation has failed
3069a 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75 72 6e 20  ..    ** Return 
3069b 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f  an error..    */
3069c 0a 20 20 20 20 70 49 64 78 20 3d 20 70 54 61 62  .    pIdx = pTab
3069d 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 70  List->a[bestJ].p
3069e 49 6e 64 65 78 3b 0a 20 20 20 20 69 66 28 20 70  Index;.    if( p
3069f 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Idx ){.      if(
306a0 20 28 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e   (bestPlan.plan.
306a1 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
306a2 49 4e 44 45 58 45 44 29 3d 3d 30 20 29 7b 0a 20  INDEXED)==0 ){. 
306a3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
306a4 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
306a5 63 61 6e 6e 6f 74 20 75 73 65 20 69 6e 64 65 78  cannot use index
306a6 3a 20 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61  : %s", pIdx->zNa
306a7 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  me);.        got
306a8 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f  o whereBeginErro
306a9 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  r;.      }else{.
306aa 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e          /* If an
306ab 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
306ac 73 65 20 69 73 20 75 73 65 64 2c 20 74 68 65 20  se is used, the 
306ad 62 65 73 74 49 6e 64 65 78 28 29 20 66 75 6e 63  bestIndex() func
306ae 74 69 6f 6e 20 69 73 0a 20 20 20 20 20 20 20 20  tion is.        
306af 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  ** guaranteed to
306b0 20 66 69 6e 64 20 74 68 65 20 69 6e 64 65 78 20   find the index 
306b1 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
306b2 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
306b3 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66  se.        ** if
306b4 20 69 74 20 66 69 6e 64 20 61 6e 20 69 6e 64 65   it find an inde
306b5 78 20 61 74 20 61 6c 6c 2e 20 2a 2f 0a 20 20 20  x at all. */.   
306b6 20 20 20 20 20 61 73 73 65 72 74 28 20 62 65 73       assert( bes
306b7 74 50 6c 61 6e 2e 70 6c 61 6e 2e 75 2e 70 49 64  tPlan.plan.u.pId
306b8 78 3d 3d 70 49 64 78 20 29 3b 0a 20 20 20 20 20  x==pIdx );.     
306b9 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 57   }.    }.  }.  W
306ba 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20  HERETRACE(("*** 
306bb 4f 70 74 69 6d 69 7a 65 72 20 46 69 6e 69 73 68  Optimizer Finish
306bc 65 64 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20 69  ed ***\n"));.  i
306bd 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
306be 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
306bf 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
306c0 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b  whereBeginError;
306c1 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
306c2 65 20 74 6f 74 61 6c 20 71 75 65 72 79 20 6f 6e  e total query on
306c3 6c 79 20 73 65 6c 65 63 74 73 20 61 20 73 69 6e  ly selects a sin
306c4 67 6c 65 20 72 6f 77 2c 20 74 68 65 6e 20 74 68  gle row, then th
306c5 65 20 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a 20  e ORDER BY.  ** 
306c6 63 6c 61 75 73 65 20 69 73 20 69 72 72 65 6c 65  clause is irrele
306c7 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  vant..  */.  if(
306c8 20 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48 45   (andFlags & WHE
306c9 52 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 26 26  RE_UNIQUE)!=0 &&
306ca 20 70 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20   ppOrderBy ){.  
306cb 20 20 2a 70 70 4f 72 64 65 72 42 79 20 3d 20 30    *ppOrderBy = 0
306cc 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
306cd 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 6e 20  he caller is an 
306ce 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
306cf 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
306d0 69 73 20 72 65 71 75 65 73 74 69 6e 67 0a 20 20  is requesting.  
306d1 2a 2a 20 74 6f 20 75 73 65 20 61 20 6f 6e 65 2d  ** to use a one-
306d2 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 2c 20  pass algorithm, 
306d3 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 69  determine if thi
306d4 73 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  s is appropriate
306d5 2e 0a 20 20 2a 2a 20 54 68 65 20 6f 6e 65 2d 70  ..  ** The one-p
306d6 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 6f 6e  ass algorithm on
306d7 6c 79 20 77 6f 72 6b 73 20 69 66 20 74 68 65 20  ly works if the 
306d8 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e  WHERE clause con
306d9 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 74 68  straints.  ** th
306da 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75  e statement to u
306db 70 64 61 74 65 20 61 20 73 69 6e 67 6c 65 20 72  pdate a single r
306dc 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ow..  */.  asser
306dd 74 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26  t( (wctrlFlags &
306de 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44   WHERE_ONEPASS_D
306df 45 53 49 52 45 44 29 3d 3d 30 20 7c 7c 20 70 57  ESIRED)==0 || pW
306e0 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d 31 20  Info->nLevel==1 
306e1 29 3b 0a 20 20 69 66 28 20 28 77 63 74 72 6c 46  );.  if( (wctrlF
306e2 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4e 45  lags & WHERE_ONE
306e3 50 41 53 53 5f 44 45 53 49 52 45 44 29 21 3d 30  PASS_DESIRED)!=0
306e4 20 26 26 20 28 61 6e 64 46 6c 61 67 73 20 26 20   && (andFlags & 
306e5 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30  WHERE_UNIQUE)!=0
306e6 20 29 7b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e   ){.    pWInfo->
306e7 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 31 3b 0a 20  okOnePass = 1;. 
306e8 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e     pWInfo->a[0].
306e9 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 3d 20  plan.wsFlags &= 
306ea 7e 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b  ~WHERE_IDX_ONLY;
306eb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  .  }..  /* Open 
306ec 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68  all tables in th
306ed 65 20 70 54 61 62 4c 69 73 74 20 61 6e 64 20 61  e pTabList and a
306ee 6e 79 20 69 6e 64 69 63 65 73 20 73 65 6c 65 63  ny indices selec
306ef 74 65 64 20 66 6f 72 0a 20 20 2a 2a 20 73 65 61  ted for.  ** sea
306f0 72 63 68 69 6e 67 20 74 68 6f 73 65 20 74 61 62  rching those tab
306f1 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  les..  */.  sqli
306f2 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
306f3 65 6d 61 28 70 50 61 72 73 65 2c 20 2d 31 29 3b  ema(pParse, -1);
306f4 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63   /* Insert the c
306f5 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 47  ookie verifier G
306f6 6f 74 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  oto */.  for(i=0
306f7 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d  , pLevel=pWInfo-
306f8 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  >a; i<pTabList->
306f9 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65  nSrc; i++, pLeve
306fa 6c 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20  l++){.    Table 
306fb 2a 70 54 61 62 3b 20 20 20 20 20 2f 2a 20 54 61  *pTab;     /* Ta
306fc 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20  ble to open */. 
306fd 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20     int iDb;     
306fe 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
306ff 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
30700 69 6e 67 20 74 61 62 6c 65 2f 69 6e 64 65 78 20  ing table/index 
30701 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
30702 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a  TE_OMIT_EXPLAIN.
30703 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
30704 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20  explain==2 ){.  
30705 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a      char *zMsg;.
30706 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63        struct Src
30707 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
30708 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b   = &pTabList->a[
30709 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a  pLevel->iFrom];.
3070a 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c        zMsg = sql
3070b 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
3070c 22 54 41 42 4c 45 20 25 73 22 2c 20 70 49 74 65  "TABLE %s", pIte
3070d 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  m->zName);.     
3070e 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69   if( pItem->zAli
3070f 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d  as ){.        zM
30710 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70  sg = sqlite3MApp
30711 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22  endf(db, zMsg, "
30712 25 73 20 41 53 20 25 73 22 2c 20 7a 4d 73 67 2c  %s AS %s", zMsg,
30713 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b   pItem->zAlias);
30714 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
30715 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
30716 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30717 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a  _INDEXED)!=0 ){.
30718 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
30719 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
3071a 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 57 49 54  b, zMsg, "%s WIT
3071b 48 20 49 4e 44 45 58 20 25 73 22 2c 0a 20 20 20  H INDEX %s",.   
3071c 20 20 20 20 20 20 20 20 7a 4d 73 67 2c 20 70 4c          zMsg, pL
3071d 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64  evel->plan.u.pId
3071e 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  x->zName);.     
3071f 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65   }else if( pLeve
30720 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
30721 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52  & WHERE_MULTI_OR
30722 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67   ){.        zMsg
30723 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
30724 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
30725 20 56 49 41 20 4d 55 4c 54 49 2d 49 4e 44 45 58   VIA MULTI-INDEX
30726 20 55 4e 49 4f 4e 22 2c 20 7a 4d 73 67 29 3b 0a   UNION", zMsg);.
30727 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
30728 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
30729 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 52 4f  lags & (WHERE_RO
3072a 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57  WID_EQ|WHERE_ROW
3072b 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20 20 20  ID_RANGE) ){.   
3072c 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
3072d 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
3072e 7a 4d 73 67 2c 20 22 25 73 20 55 53 49 4e 47 20  zMsg, "%s USING 
3072f 50 52 49 4d 41 52 59 20 4b 45 59 22 2c 20 7a 4d  PRIMARY KEY", zM
30730 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66  sg);.      }.#if
30731 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
30732 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
30733 20 20 20 20 65 6c 73 65 20 69 66 28 20 28 70 4c      else if( (pL
30734 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
30735 67 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55  gs & WHERE_VIRTU
30736 41 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20  ALTABLE)!=0 ){. 
30737 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
30738 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62  ndex_info *pVtab
30739 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  Idx = pLevel->pl
3073a 61 6e 2e 75 2e 70 56 74 61 62 49 64 78 3b 0a 20  an.u.pVtabIdx;. 
3073b 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
3073c 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62  lite3MAppendf(db
3073d 2c 20 7a 4d 73 67 2c 20 22 25 73 20 56 49 52 54  , zMsg, "%s VIRT
3073e 55 41 4c 20 54 41 42 4c 45 20 49 4e 44 45 58 20  UAL TABLE INDEX 
3073f 25 64 3a 25 73 22 2c 20 7a 4d 73 67 2c 0a 20 20  %d:%s", zMsg,.  
30740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30741 20 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78 4e    pVtabIdx->idxN
30742 75 6d 2c 20 70 56 74 61 62 49 64 78 2d 3e 69 64  um, pVtabIdx->id
30743 78 53 74 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23  xStr);.      }.#
30744 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20  endif.      if( 
30745 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
30746 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44  lags & WHERE_ORD
30747 45 52 42 59 20 29 7b 0a 20 20 20 20 20 20 20 20  ERBY ){.        
30748 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41  zMsg = sqlite3MA
30749 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c  ppendf(db, zMsg,
3074a 20 22 25 73 20 4f 52 44 45 52 20 42 59 22 2c 20   "%s ORDER BY", 
3074b 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20  zMsg);.      }. 
3074c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3074d 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 45 78 70  AddOp4(v, OP_Exp
3074e 6c 61 69 6e 2c 20 69 2c 20 70 4c 65 76 65 6c 2d  lain, i, pLevel-
3074f 3e 69 46 72 6f 6d 2c 20 30 2c 20 7a 4d 73 67 2c  >iFrom, 0, zMsg,
30750 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20   P4_DYNAMIC);.  
30751 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
30752 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
30753 4e 20 2a 2f 0a 20 20 20 20 70 54 61 62 49 74 65  N */.    pTabIte
30754 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61  m = &pTabList->a
30755 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b  [pLevel->iFrom];
30756 0a 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62  .    pTab = pTab
30757 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Item->pTab;.    
30758 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
30759 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
3075a 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
3075b 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74 61     if( (pTab->ta
3075c 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65  bFlags & TF_Ephe
3075d 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70 54 61  meral)!=0 || pTa
3075e 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63 6f 6e  b->pSelect ) con
3075f 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53  tinue;.#ifndef S
30760 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
30761 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20  ALTABLE.    if( 
30762 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
30763 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56 49  Flags & WHERE_VI
30764 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20 29  RTUALTABLE)!=0 )
30765 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
30766 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e  ar *pVTab = (con
30767 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
30768 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70  3GetVTable(db, p
30769 54 61 62 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  Tab);.      int 
3076a 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d  iCur = pTabItem-
3076b 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  >iCursor;.      
3076c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3076d 34 28 76 2c 20 4f 50 5f 56 4f 70 65 6e 2c 20 69  4(v, OP_VOpen, i
3076e 43 75 72 2c 20 30 2c 20 30 2c 20 70 56 54 61 62  Cur, 0, 0, pVTab
3076f 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20  , P4_VTAB);.    
30770 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
30771 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c   if( (pLevel->pl
30772 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
30773 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 0a  RE_IDX_ONLY)==0.
30774 20 20 20 20 20 20 20 20 20 26 26 20 28 77 63 74           && (wct
30775 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  rlFlags & WHERE_
30776 4f 4d 49 54 5f 4f 50 45 4e 29 3d 3d 30 20 29 7b  OMIT_OPEN)==0 ){
30777 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20  .      int op = 
30778 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73  pWInfo->okOnePas
30779 73 20 3f 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  s ? OP_OpenWrite
3077a 20 3a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3b 0a   : OP_OpenRead;.
3077b 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65        sqlite3Ope
3077c 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  nTable(pParse, p
3077d 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
3077e 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29  , iDb, pTab, op)
3077f 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 57 49  ;.      if( !pWI
30780 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 26  nfo->okOnePass &
30781 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3c 42 4d 53  & pTab->nCol<BMS
30782 20 29 7b 0a 20 20 20 20 20 20 20 20 42 69 74 6d   ){.        Bitm
30783 61 73 6b 20 62 20 3d 20 70 54 61 62 49 74 65 6d  ask b = pTabItem
30784 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20  ->colUsed;.     
30785 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20     int n = 0;.  
30786 20 20 20 20 20 20 66 6f 72 28 3b 20 62 3b 20 62        for(; b; b
30787 3d 62 3e 3e 31 2c 20 6e 2b 2b 29 7b 7d 0a 20 20  =b>>1, n++){}.  
30788 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
30789 65 43 68 61 6e 67 65 50 34 28 76 2c 20 73 71 6c  eChangeP4(v, sql
3078a 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
3078b 64 64 72 28 76 29 2d 31 2c 20 0a 20 20 20 20 20  ddr(v)-1, .     
3078c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3078d 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e         SQLITE_IN
3078e 54 5f 54 4f 5f 50 54 52 28 6e 29 2c 20 50 34 5f  T_TO_PTR(n), P4_
3078f 49 4e 54 33 32 29 3b 0a 20 20 20 20 20 20 20 20  INT32);.        
30790 61 73 73 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d  assert( n<=pTab-
30791 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d  >nCol );.      }
30792 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
30793 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
30794 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  ck(pParse, iDb, 
30795 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70  pTab->tnum, 0, p
30796 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
30797 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69   }.    pLevel->i
30798 54 61 62 43 75 72 20 3d 20 70 54 61 62 49 74 65  TabCur = pTabIte
30799 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20  m->iCursor;.    
3079a 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
3079b 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
3079c 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b  E_INDEXED)!=0 ){
3079d 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49  .      Index *pI
3079e 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  x = pLevel->plan
3079f 2e 75 2e 70 49 64 78 3b 0a 20 20 20 20 20 20 4b  .u.pIdx;.      K
307a0 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73  eyInfo *pKey = s
307a1 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
307a2 66 6f 28 70 50 61 72 73 65 2c 20 70 49 78 29 3b  fo(pParse, pIx);
307a3 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 43  .      int iIdxC
307a4 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64  ur = pLevel->iId
307a5 78 43 75 72 3b 0a 20 20 20 20 20 20 61 73 73 65  xCur;.      asse
307a6 72 74 28 20 70 49 78 2d 3e 70 53 63 68 65 6d 61  rt( pIx->pSchema
307a7 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20  ==pTab->pSchema 
307a8 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
307a9 20 69 49 64 78 43 75 72 3e 3d 30 20 29 3b 0a 20   iIdxCur>=0 );. 
307aa 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
307ab 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65  AddOp4(v, OP_Ope
307ac 6e 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20  nRead, iIdxCur, 
307ad 70 49 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  pIx->tnum, iDb,.
307ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307af 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
307b0 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Key, P4_KEYINFO_
307b1 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20  HANDOFF);.      
307b2 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
307b3 22 25 73 22 2c 20 70 49 78 2d 3e 7a 4e 61 6d 65  "%s", pIx->zName
307b4 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ));.    }.    sq
307b5 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
307b6 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44  chema(pParse, iD
307b7 62 29 3b 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f  b);.  }.  pWInfo
307b8 2d 3e 69 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  ->iTop = sqlite3
307b9 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
307ba 76 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  v);..  /* Genera
307bb 74 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64  te the code to d
307bc 6f 20 74 68 65 20 73 65 61 72 63 68 2e 20 20 45  o the search.  E
307bd 61 63 68 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ach iteration of
307be 20 74 68 65 20 66 6f 72 0a 20 20 2a 2a 20 6c 6f   the for.  ** lo
307bf 6f 70 20 62 65 6c 6f 77 20 67 65 6e 65 72 61 74  op below generat
307c0 65 73 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69  es code for a si
307c1 6e 67 6c 65 20 6e 65 73 74 65 64 20 6c 6f 6f 70  ngle nested loop
307c2 20 6f 66 20 74 68 65 20 56 4d 0a 20 20 2a 2a 20   of the VM.  ** 
307c3 70 72 6f 67 72 61 6d 2e 0a 20 20 2a 2f 0a 20 20  program..  */.  
307c4 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74  notReady = ~(Bit
307c5 6d 61 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69 3d  mask)0;.  for(i=
307c6 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  0; i<pTabList->n
307c7 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e  Src; i++){.    n
307c8 6f 74 52 65 61 64 79 20 3d 20 63 6f 64 65 4f 6e  otReady = codeOn
307c9 65 4c 6f 6f 70 53 74 61 72 74 28 70 57 49 6e 66  eLoopStart(pWInf
307ca 6f 2c 20 69 2c 20 77 63 74 72 6c 46 6c 61 67 73  o, i, wctrlFlags
307cb 2c 20 6e 6f 74 52 65 61 64 79 29 3b 0a 20 20 20  , notReady);.   
307cc 20 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e   pWInfo->iContin
307cd 75 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b 69  ue = pWInfo->a[i
307ce 5d 2e 61 64 64 72 43 6f 6e 74 3b 0a 20 20 7d 0a  ].addrCont;.  }.
307cf 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
307d0 45 53 54 20 20 2f 2a 20 46 6f 72 20 74 65 73 74  EST  /* For test
307d1 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
307d2 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20  g use only */.  
307d3 2f 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65  /* Record in the
307d4 20 71 75 65 72 79 20 70 6c 61 6e 20 69 6e 66 6f   query plan info
307d5 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
307d6 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 0a  e current table.
307d7 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64    ** and the ind
307d8 65 78 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  ex used to acces
307d9 73 20 69 74 20 28 69 66 20 61 6e 79 29 2e 20 20  s it (if any).  
307da 49 66 20 74 68 65 20 74 61 62 6c 65 20 69 74 73  If the table its
307db 65 6c 66 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20  elf.  ** is not 
307dc 75 73 65 64 2c 20 69 74 73 20 6e 61 6d 65 20 69  used, its name i
307dd 73 20 6a 75 73 74 20 27 7b 7d 27 2e 20 20 49 66  s just '{}'.  If
307de 20 6e 6f 20 69 6e 64 65 78 20 69 73 20 75 73 65   no index is use
307df 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78  d.  ** the index
307e0 20 69 73 20 6c 69 73 74 65 64 20 61 73 20 22 7b   is listed as "{
307e1 7d 22 2e 20 20 49 66 20 74 68 65 20 70 72 69 6d  }".  If the prim
307e2 61 72 79 20 6b 65 79 20 69 73 20 75 73 65 64 20  ary key is used 
307e3 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6e  the.  ** index n
307e4 61 6d 65 20 69 73 20 27 2a 27 2e 0a 20 20 2a 2f  ame is '*'..  */
307e5 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
307e6 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
307e7 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b  +){.    char *z;
307e8 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20  .    int n;.    
307e9 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f  pLevel = &pWInfo
307ea 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 54 61 62  ->a[i];.    pTab
307eb 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74  Item = &pTabList
307ec 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f  ->a[pLevel->iFro
307ed 6d 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54 61 62  m];.    z = pTab
307ee 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20  Item->zAlias;.  
307ef 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a 20 3d    if( z==0 ) z =
307f0 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d   pTabItem->pTab-
307f1 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20 3d 20  >zName;.    n = 
307f2 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
307f3 7a 29 3b 0a 20 20 20 20 69 66 28 20 6e 2b 6e 51  z);.    if( n+nQ
307f4 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71  Plan < sizeof(sq
307f5 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
307f6 29 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 69 66  )-10 ){.      if
307f7 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
307f8 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
307f9 44 58 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20  DX_ONLY ){.     
307fa 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
307fb 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
307fc 50 6c 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b  Plan], "{}", 2);
307fd 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  .        nQPlan 
307fe 2b 3d 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  += 2;.      }els
307ff 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  e{.        memcp
30800 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79  y(&sqlite3_query
30801 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 7a  _plan[nQPlan], z
30802 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51  , n);.        nQ
30803 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20  Plan += n;.     
30804 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
30805 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
30806 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20  an++] = ' ';.   
30807 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28   }.    testcase(
30808 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
30809 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f  Flags & WHERE_RO
3080a 57 49 44 5f 45 51 20 29 3b 0a 20 20 20 20 74 65  WID_EQ );.    te
3080b 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e  stcase( pLevel->
3080c 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
3080d 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45  HERE_ROWID_RANGE
3080e 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76   );.    if( pLev
3080f 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
30810 20 26 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f   & (WHERE_ROWID_
30811 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52  EQ|WHERE_ROWID_R
30812 41 4e 47 45 29 20 29 7b 0a 20 20 20 20 20 20 6d  ANGE) ){.      m
30813 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71  emcpy(&sqlite3_q
30814 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
30815 5d 2c 20 22 2a 20 22 2c 20 32 29 3b 0a 20 20 20  ], "* ", 2);.   
30816 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a     nQPlan += 2;.
30817 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
30818 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
30819 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45  ags & WHERE_INDE
3081a 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  XED)!=0 ){.     
3081b 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
3081c 65 6e 33 30 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  en30(pLevel->pla
3081d 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29  n.u.pIdx->zName)
3081e 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 2b 6e 51  ;.      if( n+nQ
3081f 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71  Plan < sizeof(sq
30820 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
30821 29 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  )-2 ){.        m
30822 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71  emcpy(&sqlite3_q
30823 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
30824 5d 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ], pLevel->plan.
30825 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e  u.pIdx->zName, n
30826 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61  );.        nQPla
30827 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20  n += n;.        
30828 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
30829 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27  an[nQPlan++] = '
3082a 20 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   ';.      }.    
3082b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
3082c 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65  cpy(&sqlite3_que
3082d 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c  ry_plan[nQPlan],
3082e 20 22 7b 7d 20 22 2c 20 33 29 3b 0a 20 20 20 20   "{} ", 3);.    
3082f 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b 0a 20    nQPlan += 3;. 
30830 20 20 20 7d 0a 20 20 7d 0a 20 20 77 68 69 6c 65     }.  }.  while
30831 28 20 6e 51 50 6c 61 6e 3e 30 20 26 26 20 73 71  ( nQPlan>0 && sq
30832 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
30833 5b 6e 51 50 6c 61 6e 2d 31 5d 3d 3d 27 20 27 20  [nQPlan-1]==' ' 
30834 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 71  ){.    sqlite3_q
30835 75 65 72 79 5f 70 6c 61 6e 5b 2d 2d 6e 51 50 6c  uery_plan[--nQPl
30836 61 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73  an] = 0;.  }.  s
30837 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
30838 6e 5b 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20  n[nQPlan] = 0;. 
30839 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 0a 23 65 6e   nQPlan = 0;.#en
3083a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45  dif /* SQLITE_TE
3083b 53 54 20 2f 2f 20 54 65 73 74 69 6e 67 20 61 6e  ST // Testing an
3083c 64 20 64 65 62 75 67 67 69 6e 67 20 75 73 65 20  d debugging use 
3083d 6f 6e 6c 79 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65  only */..  /* Re
3083e 63 6f 72 64 20 74 68 65 20 63 6f 6e 74 69 6e 75  cord the continu
3083f 61 74 69 6f 6e 20 61 64 64 72 65 73 73 20 69 6e  ation address in
30840 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73   the WhereInfo s
30841 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 6e 0a  tructure.  Then.
30842 20 20 2a 2a 20 63 6c 65 61 6e 20 75 70 20 61 6e    ** clean up an
30843 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20  d return..  */. 
30844 20 72 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b 0a   return pWInfo;.
30845 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20  .  /* Jump here 
30846 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20  if malloc fails 
30847 2a 2f 0a 77 68 65 72 65 42 65 67 69 6e 45 72 72  */.whereBeginErr
30848 6f 72 3a 0a 20 20 77 68 65 72 65 49 6e 66 6f 46  or:.  whereInfoF
30849 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b  ree(db, pWInfo);
3084a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
3084b 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74  /*.** Generate t
3084c 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48  he end of the WH
3084d 45 52 45 20 6c 6f 6f 70 2e 20 20 53 65 65 20 63  ERE loop.  See c
3084e 6f 6d 6d 65 6e 74 73 20 6f 6e 20 0a 2a 2a 20 73  omments on .** s
3084f 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
30850 28 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  () for additiona
30851 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
30852 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
30853 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65   void sqlite3Whe
30854 72 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 20  reEnd(WhereInfo 
30855 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 50 61 72 73  *pWInfo){.  Pars
30856 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 49 6e  e *pParse = pWIn
30857 66 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 56 64  fo->pParse;.  Vd
30858 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
30859 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  pVdbe;.  int i;.
3085a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c    WhereLevel *pL
3085b 65 76 65 6c 3b 0a 20 20 53 72 63 4c 69 73 74 20  evel;.  SrcList 
3085c 2a 70 54 61 62 4c 69 73 74 20 3d 20 70 57 49 6e  *pTabList = pWIn
3085d 66 6f 2d 3e 70 54 61 62 4c 69 73 74 3b 0a 20 20  fo->pTabList;.  
3085e 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
3085f 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20  arse->db;..  /* 
30860 47 65 6e 65 72 61 74 65 20 6c 6f 6f 70 20 74 65  Generate loop te
30861 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 2e 0a  rmination code..
30862 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78    */.  sqlite3Ex
30863 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61  prCacheClear(pPa
30864 72 73 65 29 3b 0a 20 20 66 6f 72 28 69 3d 70 54  rse);.  for(i=pT
30865 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 3b 20  abList->nSrc-1; 
30866 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  i>=0; i--){.    
30867 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f  pLevel = &pWInfo
30868 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 71 6c 69  ->a[i];.    sqli
30869 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
3086a 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61  bel(v, pLevel->a
3086b 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 69 66  ddrCont);.    if
3086c 28 20 70 4c 65 76 65 6c 2d 3e 6f 70 21 3d 4f 50  ( pLevel->op!=OP
3086d 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73  _Noop ){.      s
3086e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3086f 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 6f 70 2c 20  (v, pLevel->op, 
30870 70 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76  pLevel->p1, pLev
30871 65 6c 2d 3e 70 32 29 3b 0a 20 20 20 20 20 20 73  el->p2);.      s
30872 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
30873 50 35 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 70 35  P5(v, pLevel->p5
30874 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
30875 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
30876 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
30877 5f 41 42 4c 45 20 26 26 20 70 4c 65 76 65 6c 2d  _ABLE && pLevel-
30878 3e 75 2e 69 6e 2e 6e 49 6e 3e 30 20 29 7b 0a 20  >u.in.nIn>0 ){. 
30879 20 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f       struct InLo
3087a 6f 70 20 2a 70 49 6e 3b 0a 20 20 20 20 20 20 69  op *pIn;.      i
3087b 6e 74 20 6a 3b 0a 20 20 20 20 20 20 73 71 6c 69  nt j;.      sqli
3087c 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
3087d 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61  bel(v, pLevel->a
3087e 64 64 72 4e 78 74 29 3b 0a 20 20 20 20 20 20 66  ddrNxt);.      f
3087f 6f 72 28 6a 3d 70 4c 65 76 65 6c 2d 3e 75 2e 69  or(j=pLevel->u.i
30880 6e 2e 6e 49 6e 2c 20 70 49 6e 3d 26 70 4c 65 76  n.nIn, pIn=&pLev
30881 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70  el->u.in.aInLoop
30882 5b 6a 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c  [j-1]; j>0; j--,
30883 20 70 49 6e 2d 2d 29 7b 0a 20 20 20 20 20 20 20   pIn--){.       
30884 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
30885 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e 61 64 64  Here(v, pIn->add
30886 72 49 6e 54 6f 70 2b 31 29 3b 0a 20 20 20 20 20  rInTop+1);.     
30887 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
30888 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
30889 20 70 49 6e 2d 3e 69 43 75 72 2c 20 70 49 6e 2d   pIn->iCur, pIn-
3088a 3e 61 64 64 72 49 6e 54 6f 70 29 3b 0a 20 20 20  >addrInTop);.   
3088b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3088c 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 49 6e 2d  JumpHere(v, pIn-
3088d 3e 61 64 64 72 49 6e 54 6f 70 2d 31 29 3b 0a 20  >addrInTop-1);. 
3088e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
3088f 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
30890 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c  Level->u.in.aInL
30891 6f 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oop);.    }.    
30892 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
30893 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65  veLabel(v, pLeve
30894 6c 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20  l->addrBrk);.   
30895 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65   if( pLevel->iLe
30896 66 74 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 20 20  ftJoin ){.      
30897 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20  int addr;.      
30898 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
30899 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
3089a 66 50 6f 73 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c  fPos, pLevel->iL
3089b 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 20 20  eftJoin);.      
3089c 61 73 73 65 72 74 28 20 28 70 4c 65 76 65 6c 2d  assert( (pLevel-
3089d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
3089e 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d  WHERE_IDX_ONLY)=
3089f 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  =0.           ||
308a0 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
308a1 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
308a2 4e 44 45 58 45 44 29 21 3d 30 20 29 3b 0a 20 20  NDEXED)!=0 );.  
308a3 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d      if( (pLevel-
308a4 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
308a5 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d  WHERE_IDX_ONLY)=
308a6 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
308a7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
308a8 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70  v, OP_NullRow, p
308a9 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43  TabList->a[i].iC
308aa 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a  ursor);.      }.
308ab 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c        if( pLevel
308ac 2d 3e 69 49 64 78 43 75 72 3e 3d 30 20 29 7b 0a  ->iIdxCur>=0 ){.
308ad 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
308ae 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
308af 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65 6c 2d  NullRow, pLevel-
308b0 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20  >iIdxCur);.     
308b1 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65   }.      if( pLe
308b2 76 65 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65 74 75  vel->op==OP_Retu
308b3 72 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  rn ){.        sq
308b4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
308b5 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 70 4c 65  v, OP_Gosub, pLe
308b6 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c 2d  vel->p1, pLevel-
308b7 3e 61 64 64 72 46 69 72 73 74 29 3b 0a 20 20 20  >addrFirst);.   
308b8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
308b9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
308ba 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
308bb 30 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46  0, pLevel->addrF
308bc 69 72 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  irst);.      }. 
308bd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
308be 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
308bf 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
308c0 2f 2a 20 54 68 65 20 22 62 72 65 61 6b 22 20 70  /* The "break" p
308c1 6f 69 6e 74 20 69 73 20 68 65 72 65 2c 20 6a 75  oint is here, ju
308c2 73 74 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  st past the end 
308c3 6f 66 20 74 68 65 20 6f 75 74 65 72 20 6c 6f 6f  of the outer loo
308c4 70 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74 2e 0a  p..  ** Set it..
308c5 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
308c6 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
308c7 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b  , pWInfo->iBreak
308c8 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61  );..  /* Close a
308c9 6c 6c 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72  ll of the cursor
308ca 73 20 74 68 61 74 20 77 65 72 65 20 6f 70 65 6e  s that were open
308cb 65 64 20 62 79 20 73 71 6c 69 74 65 33 57 68 65  ed by sqlite3Whe
308cc 72 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a 20 20  reBegin..  */.  
308cd 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d  for(i=0, pLevel=
308ce 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61  pWInfo->a; i<pTa
308cf 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
308d0 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20  , pLevel++){.   
308d1 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
308d2 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 20 3d  item *pTabItem =
308d3 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c   &pTabList->a[pL
308d4 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20  evel->iFrom];.  
308d5 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
308d6 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a  pTabItem->pTab;.
308d7 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
308d8 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28  !=0 );.    if( (
308d9 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
308da 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d   TF_Ephemeral)!=
308db 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65  0 || pTab->pSele
308dc 63 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ct ) continue;. 
308dd 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e     if( (pWInfo->
308de 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
308df 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29 3d 3d  RE_OMIT_CLOSE)==
308e0 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  0 ){.      if( !
308e1 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73  pWInfo->okOnePas
308e2 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c  s && (pLevel->pl
308e3 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
308e4 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 20  RE_IDX_ONLY)==0 
308e5 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
308e6 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
308e7 4f 50 5f 43 6c 6f 73 65 2c 20 70 54 61 62 49 74  OP_Close, pTabIt
308e8 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  em->iCursor);.  
308e9 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
308ea 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
308eb 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
308ec 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20  DEXED)!=0 ){.   
308ed 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
308ee 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
308ef 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  se, pLevel->iIdx
308f0 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Cur);.      }.  
308f1 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
308f2 68 69 73 20 73 63 61 6e 20 75 73 65 73 20 61 6e  his scan uses an
308f3 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f 64   index, make cod
308f4 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20  e substitutions 
308f5 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20 20  to read data.   
308f6 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 64   ** from the ind
308f7 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63 65  ex in preference
308f8 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20 53   to the table. S
308f9 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20 6d  ometimes, this m
308fa 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20  eans.    ** the 
308fb 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65 72  table need never
308fc 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20 54   be read from. T
308fd 68 69 73 20 69 73 20 61 20 70 65 72 66 6f 72 6d  his is a perform
308fe 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20 20  ance boost,.    
308ff 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65 20 6c  ** as the vdbe l
30900 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69 6c  evel waits until
30901 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 72 65   the table is re
30902 61 64 20 62 65 66 6f 72 65 20 61 63 74 75 61 6c  ad before actual
30903 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69 6e  ly.    ** seekin
30904 67 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73  g the table curs
30905 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64  or to the record
30906 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
30907 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20  o the current.  
30908 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e    ** position in
30909 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20   the index..    
3090a 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 73  ** .    ** Calls
3090b 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e   to the code gen
3090c 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65 65  erator in betwee
3090d 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  n sqlite3WhereBe
3090e 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20 73  gin and.    ** s
3090f 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20 77  qlite3WhereEnd w
30910 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65 64  ill have created
30911 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65 72   code that refer
30912 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65 0a  ences the table.
30913 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 2e      ** directly.
30914 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61 6e    This loop scan
30915 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65 20  s all that code 
30916 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63 6f  looking for opco
30917 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  des.    ** that 
30918 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 74 61  reference the ta
30919 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74 73  ble and converts
3091a 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f 64   them into opcod
3091b 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 72  es that.    ** r
3091c 65 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e 64  eference the ind
3091d 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ex..    */.    i
3091e 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
3091f 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30920 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 26 26 20  _INDEXED)!=0 && 
30921 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
30922 64 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 2c  d){.      int k,
30923 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20 20   j, last;.      
30924 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20  VdbeOp *pOp;.   
30925 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d     Index *pIdx =
30926 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
30927 70 49 64 78 3b 0a 0a 20 20 20 20 20 20 61 73 73  pIdx;..      ass
30928 65 72 74 28 20 70 49 64 78 21 3d 30 20 29 3b 0a  ert( pIdx!=0 );.
30929 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69        pOp = sqli
3092a 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20  te3VdbeGetOp(v, 
3092b 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20  pWInfo->iTop);. 
3092c 20 20 20 20 20 6c 61 73 74 20 3d 20 73 71 6c 69       last = sqli
3092d 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
3092e 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66 6f 72  dr(v);.      for
3092f 28 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b  (k=pWInfo->iTop;
30930 20 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f   k<last; k++, pO
30931 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  p++){.        if
30932 28 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76 65  ( pOp->p1!=pLeve
30933 6c 2d 3e 69 54 61 62 43 75 72 20 29 20 63 6f 6e  l->iTabCur ) con
30934 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69  tinue;.        i
30935 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
30936 4f 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  OP_Column ){.   
30937 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
30938 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  j<pIdx->nColumn;
30939 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
3093a 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d     if( pOp->p2==
3093b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a  pIdx->aiColumn[j
3093c 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ] ){.           
3093d 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b 0a     pOp->p2 = j;.
3093e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f                pO
3093f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e  p->p1 = pLevel->
30940 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20  iIdxCur;.       
30941 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30942 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
30943 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
30944 20 20 61 73 73 65 72 74 28 20 28 70 4c 65 76 65    assert( (pLeve
30945 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
30946 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
30947 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20  )==0.           
30948 20 20 20 20 7c 7c 20 6a 3c 70 49 64 78 2d 3e 6e      || j<pIdx->n
30949 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20  Column );.      
3094a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d    }else if( pOp-
3094b 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 69  >opcode==OP_Rowi
3094c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  d ){.          p
3094d 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d  Op->p1 = pLevel-
3094e 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20  >iIdxCur;.      
3094f 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20      pOp->opcode 
30950 3d 20 4f 50 5f 49 64 78 52 6f 77 69 64 3b 0a 20  = OP_IdxRowid;. 
30951 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
30952 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
30953 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0a 20   Final cleanup. 
30954 20 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66 6f 46   */.  whereInfoF
30955 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b  ree(db, pWInfo);
30956 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
30957 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
30958 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a  d of where.c ***
30959 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3095a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3095b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
3095c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
3095d 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 63  gin file parse.c
3095e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3095f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
30961 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74 65   Driver template
30962 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70   for the LEMON p
30963 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 2e  arser generator.
30964 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
30965 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
30966 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
30967 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e code..**.** Th
30968 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 22 6c  is version of "l
30969 65 6d 70 61 72 2e 63 22 20 69 73 20 6d 6f 64 69  empar.c" is modi
3096a 66 69 65 64 2c 20 73 6c 69 67 68 74 6c 79 2c 20  fied, slightly, 
3096b 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69 74  for use by SQLit
3096c 65 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 6d  e..** The only m
3096d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65  odifications are
3096e 20 74 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66   the addition of
3096f 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 4e 45 56   a couple of NEV
30970 45 52 28 29 0a 2a 2a 20 6d 61 63 72 6f 73 20 74  ER().** macros t
30971 6f 20 64 69 73 61 62 6c 65 20 74 65 73 74 73 20  o disable tests 
30972 74 68 61 74 20 61 72 65 20 6e 65 65 64 65 64 20  that are needed 
30973 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 61  in the case of a
30974 20 67 65 6e 65 72 61 6c 0a 2a 2a 20 4c 41 4c 52   general.** LALR
30975 28 31 29 20 67 72 61 6d 6d 61 72 20 62 75 74 20  (1) grammar but 
30976 77 68 69 63 68 20 61 72 65 20 61 6c 77 61 79 73  which are always
30977 20 66 61 6c 73 65 20 69 6e 20 74 68 65 0a 2a 2a   false in the.**
30978 20 73 70 65 63 69 66 69 63 20 67 72 61 6d 6d 61   specific gramma
30979 72 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  r used by SQLite
3097a 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74 20 6f 66  ..*/./* First of
3097b 66 2c 20 63 6f 64 65 20 69 73 20 69 6e 63 6c 75  f, code is inclu
3097c 64 65 64 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ded that follows
3097d 20 74 68 65 20 22 69 6e 63 6c 75 64 65 22 20 64   the "include" d
3097e 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e  eclaration.** in
3097f 20 74 68 65 20 69 6e 70 75 74 20 67 72 61 6d 6d   the input gramm
30980 61 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a 2f 2a  ar file. */.../*
30981 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61 6c 6c 20  .** Disable all 
30982 65 72 72 6f 72 20 72 65 63 6f 76 65 72 79 20 70  error recovery p
30983 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 74 68 65  rocessing in the
30984 20 70 61 72 73 65 72 20 70 75 73 68 2d 64 6f 77   parser push-dow
30985 6e 0a 2a 2a 20 61 75 74 6f 6d 61 74 6f 6e 2e 0a  n.** automaton..
30986 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f 45  */.#define YYNOE
30987 52 52 4f 52 52 45 43 4f 56 45 52 59 20 31 0a 0a  RRORRECOVERY 1..
30988 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 79 79 74 65 73  /*.** Make yytes
30989 74 63 61 73 65 28 29 20 74 68 65 20 73 61 6d 65  tcase() the same
3098a 20 61 73 20 74 65 73 74 63 61 73 65 28 29 0a 2a   as testcase().*
3098b 2f 0a 23 64 65 66 69 6e 65 20 79 79 74 65 73 74  /.#define yytest
3098c 63 61 73 65 28 58 29 20 74 65 73 74 63 61 73 65  case(X) testcase
3098d 28 58 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  (X)../*.** An in
3098e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
3098f 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69  tructure holds i
30990 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
30991 20 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c   the.** LIMIT cl
30992 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
30993 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73   statement..*/.s
30994 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 7b  truct LimitVal {
30995 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b  .  Expr *pLimit;
30996 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54      /* The LIMIT
30997 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55   expression.  NU
30998 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  LL if there is n
30999 6f 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45 78 70  o limit */.  Exp
3099a 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 2f 2a  r *pOffset;   /*
3099b 20 54 68 65 20 4f 46 46 53 45 54 20 65 78 70 72   The OFFSET expr
3099c 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66  ession.  NULL if
3099d 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 20 2a   there is none *
3099e 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
3099f 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
309a0 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
309a1 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 4c  d to store the L
309a2 49 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f  IKE,.** GLOB, NO
309a3 54 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f 54 20  T LIKE, and NOT 
309a4 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e 0a  GLOB operators..
309a5 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65 4f 70  */.struct LikeOp
309a6 20 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70 65 72   {.  Token eOper
309a7 61 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b 65 22  ator;  /* "like"
309a8 20 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20 22 72   or "glob" or "r
309a9 65 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e 74 20  egexp" */.  int 
309aa 6e 6f 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  not;         /* 
309ab 54 72 75 65 20 69 66 20 74 68 65 20 4e 4f 54 20  True if the NOT 
309ac 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
309ad 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
309ae 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
309af 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
309b0 75 63 74 75 72 65 20 64 65 73 63 72 69 62 65 73  ucture describes
309b1 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 0a   the event of a.
309b2 2a 2a 20 54 52 49 47 47 45 52 2e 20 20 22 61 22  ** TRIGGER.  "a"
309b3 20 69 73 20 74 68 65 20 65 76 65 6e 74 20 74 79   is the event ty
309b4 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 50  pe, one of TK_UP
309b5 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  DATE, TK_INSERT,
309b6 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20 6f  .** TK_DELETE, o
309b7 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20 49  r TK_INSTEAD.  I
309b8 66 20 74 68 65 20 65 76 65 6e 74 20 69 73 20 6f  f the event is o
309b9 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
309ba 20 20 20 20 20 20 55 50 44 41 54 45 20 4f 4e 20        UPDATE ON 
309bb 28 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68  (a,b,c).**.** Th
309bc 65 6e 20 74 68 65 20 22 62 22 20 49 64 4c 69 73  en the "b" IdLis
309bd 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 69  t records the li
309be 73 74 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73  st "a,b,c"..*/.s
309bf 74 72 75 63 74 20 54 72 69 67 45 76 65 6e 74 20  truct TrigEvent 
309c0 7b 20 69 6e 74 20 61 3b 20 49 64 4c 69 73 74 20  { int a; IdList 
309c1 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  * b; };../*.** A
309c2 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
309c3 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  is structure hol
309c4 64 73 20 74 68 65 20 41 54 54 41 43 48 20 6b 65  ds the ATTACH ke
309c5 79 20 61 6e 64 20 74 68 65 20 6b 65 79 20 74 79  y and the key ty
309c6 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 74  pe..*/.struct At
309c7 74 61 63 68 4b 65 79 20 7b 20 69 6e 74 20 74 79  tachKey { int ty
309c8 70 65 3b 20 20 54 6f 6b 65 6e 20 6b 65 79 3b 20  pe;  Token key; 
309c9 7d 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69  };...  /* This i
309ca 73 20 61 20 75 74 69 6c 69 74 79 20 72 6f 75 74  s a utility rout
309cb 69 6e 65 20 75 73 65 64 20 74 6f 20 73 65 74 20  ine used to set 
309cc 74 68 65 20 45 78 70 72 53 70 61 6e 2e 7a 53 74  the ExprSpan.zSt
309cd 61 72 74 20 61 6e 64 0a 20 20 2a 2a 20 45 78 70  art and.  ** Exp
309ce 72 53 70 61 6e 2e 7a 45 6e 64 20 76 61 6c 75 65  rSpan.zEnd value
309cf 73 20 6f 66 20 70 4f 75 74 20 73 6f 20 74 68 61  s of pOut so tha
309d0 74 20 74 68 65 20 73 70 61 6e 20 63 6f 76 65 72  t the span cover
309d1 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 20  s the complete. 
309d2 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 74 65 78   ** range of tex
309d3 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  t beginning with
309d4 20 70 53 74 61 72 74 20 61 6e 64 20 67 6f 69 6e   pStart and goin
309d5 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  g to the end of 
309d6 70 45 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 74 61  pEnd..  */.  sta
309d7 74 69 63 20 76 6f 69 64 20 73 70 61 6e 53 65 74  tic void spanSet
309d8 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c  (ExprSpan *pOut,
309d9 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20   Token *pStart, 
309da 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20  Token *pEnd){.  
309db 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d    pOut->zStart =
309dc 20 70 53 74 61 72 74 2d 3e 7a 3b 0a 20 20 20 20   pStart->z;.    
309dd 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 26 70 45  pOut->zEnd = &pE
309de 6e 64 2d 3e 7a 5b 70 45 6e 64 2d 3e 6e 5d 3b 0a  nd->z[pEnd->n];.
309df 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72    }..  /* Constr
309e0 75 63 74 20 61 20 6e 65 77 20 45 78 70 72 20 6f  uct a new Expr o
309e1 62 6a 65 63 74 20 66 72 6f 6d 20 61 20 73 69 6e  bject from a sin
309e2 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2e 20  gle identifier. 
309e3 20 55 73 65 20 74 68 65 0a 20 20 2a 2a 20 6e 65   Use the.  ** ne
309e4 77 20 45 78 70 72 20 74 6f 20 70 6f 70 75 6c 61  w Expr to popula
309e5 74 65 20 70 4f 75 74 2e 20 20 53 65 74 20 74 68  te pOut.  Set th
309e6 65 20 73 70 61 6e 20 6f 66 20 70 4f 75 74 20 74  e span of pOut t
309e7 6f 20 62 65 20 74 68 65 20 69 64 65 6e 74 69 66  o be the identif
309e8 69 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 63 72  ier.  ** that cr
309e9 65 61 74 65 64 20 74 68 65 20 65 78 70 72 65 73  eated the expres
309ea 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 61  sion..  */.  sta
309eb 74 69 63 20 76 6f 69 64 20 73 70 61 6e 45 78 70  tic void spanExp
309ec 72 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74  r(ExprSpan *pOut
309ed 2c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  , Parse *pParse,
309ee 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a   int op, Token *
309ef 70 56 61 6c 75 65 29 7b 0a 20 20 20 20 70 4f 75  pValue){.    pOu
309f0 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  t->pExpr = sqlit
309f1 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
309f2 6f 70 2c 20 30 2c 20 30 2c 20 70 56 61 6c 75 65  op, 0, 0, pValue
309f3 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74  );.    pOut->zSt
309f4 61 72 74 20 3d 20 70 56 61 6c 75 65 2d 3e 7a 3b  art = pValue->z;
309f5 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20  .    pOut->zEnd 
309f6 3d 20 26 70 56 61 6c 75 65 2d 3e 7a 5b 70 56 61  = &pValue->z[pVa
309f7 6c 75 65 2d 3e 6e 5d 3b 0a 20 20 7d 0a 0a 20 20  lue->n];.  }..  
309f8 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  /* This routine 
309f9 63 6f 6e 73 74 72 75 63 74 73 20 61 20 62 69 6e  constructs a bin
309fa 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  ary expression n
309fb 6f 64 65 20 6f 75 74 20 6f 66 20 74 77 6f 20 45  ode out of two E
309fc 78 70 72 53 70 61 6e 0a 20 20 2a 2a 20 6f 62 6a  xprSpan.  ** obj
309fd 65 63 74 73 20 61 6e 64 20 75 73 65 73 20 74 68  ects and uses th
309fe 65 20 72 65 73 75 6c 74 20 74 6f 20 70 6f 70 75  e result to popu
309ff 6c 61 74 65 20 61 20 6e 65 77 20 45 78 70 72 53  late a new ExprS
30a00 70 61 6e 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f  pan object..  */
30a01 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 73  .  static void s
30a02 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28 0a 20  panBinaryExpr(. 
30a03 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75     ExprSpan *pOu
30a04 74 2c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  t,     /* Write 
30a05 74 68 65 20 72 65 73 75 6c 74 20 68 65 72 65 20  the result here 
30a06 2a 2f 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50  */.    Parse *pP
30a07 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
30a08 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
30a09 74 2e 20 20 45 72 72 6f 72 73 20 61 63 63 75 6d  t.  Errors accum
30a0a 75 6c 61 74 65 20 68 65 72 65 20 2a 2f 0a 20 20  ulate here */.  
30a0b 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
30a0c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e        /* The bin
30a0d 61 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  ary operation */
30a0e 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
30a0f 4c 65 66 74 2c 20 20 20 20 2f 2a 20 54 68 65 20  Left,    /* The 
30a10 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  left operand */.
30a11 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 52      ExprSpan *pR
30a12 69 67 68 74 20 20 20 20 2f 2a 20 54 68 65 20 72  ight    /* The r
30a13 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
30a14 20 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70    ){.    pOut->p
30a15 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
30a16 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20  xpr(pParse, op, 
30a17 70 4c 65 66 74 2d 3e 70 45 78 70 72 2c 20 70 52  pLeft->pExpr, pR
30a18 69 67 68 74 2d 3e 70 45 78 70 72 2c 20 30 29 3b  ight->pExpr, 0);
30a19 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72  .    pOut->zStar
30a1a 74 20 3d 20 70 4c 65 66 74 2d 3e 7a 53 74 61 72  t = pLeft->zStar
30a1b 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e  t;.    pOut->zEn
30a1c 64 20 3d 20 70 52 69 67 68 74 2d 3e 7a 45 6e 64  d = pRight->zEnd
30a1d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73  ;.  }..  /* Cons
30a1e 74 72 75 63 74 20 61 6e 20 65 78 70 72 65 73 73  truct an express
30a1f 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 75  ion node for a u
30a20 6e 61 72 79 20 70 6f 73 74 66 69 78 20 6f 70 65  nary postfix ope
30a21 72 61 74 6f 72 0a 20 20 2a 2f 0a 20 20 73 74 61  rator.  */.  sta
30a22 74 69 63 20 76 6f 69 64 20 73 70 61 6e 55 6e 61  tic void spanUna
30a23 72 79 50 6f 73 74 66 69 78 28 0a 20 20 20 20 45  ryPostfix(.    E
30a24 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20 20  xprSpan *pOut,  
30a25 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
30a26 68 65 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f  he new expressio
30a27 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a 2f 0a 20  n node here */. 
30a28 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65     Parse *pParse
30a29 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ,         /* Par
30a2a 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20  sing context to 
30a2b 72 65 63 6f 72 64 20 65 72 72 6f 72 73 20 2a 2f  record errors */
30a2c 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  .    int op,    
30a2d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
30a2e 68 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  he operator */. 
30a2f 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 70     ExprSpan *pOp
30a30 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20 54 68 65  erand,    /* The
30a31 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20   operand */.    
30a32 54 6f 6b 65 6e 20 2a 70 50 6f 73 74 4f 70 20 20  Token *pPostOp  
30a33 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70         /* The op
30a34 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f 72 20  erand token for 
30a35 73 65 74 74 69 6e 67 20 74 68 65 20 73 70 61 6e  setting the span
30a36 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75   */.  ){.    pOu
30a37 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  t->pExpr = sqlit
30a38 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
30a39 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e 70 45  op, pOperand->pE
30a3a 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
30a3b 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70  pOut->zStart = p
30a3c 4f 70 65 72 61 6e 64 2d 3e 7a 53 74 61 72 74 3b  Operand->zStart;
30a3d 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20  .    pOut->zEnd 
30a3e 3d 20 26 70 50 6f 73 74 4f 70 2d 3e 7a 5b 70 50  = &pPostOp->z[pP
30a3f 6f 73 74 4f 70 2d 3e 6e 5d 3b 0a 20 20 7d 20 20  ostOp->n];.  }  
30a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30a41 20 20 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a 20           ..  /* 
30a42 41 20 72 6f 75 74 69 6e 65 20 74 6f 20 63 6f 6e  A routine to con
30a43 76 65 72 74 20 61 20 62 69 6e 61 72 79 20 54 4b  vert a binary TK
30a44 5f 49 53 20 6f 72 20 54 4b 5f 49 53 4e 4f 54 20  _IS or TK_ISNOT 
30a45 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20  expression into 
30a46 61 0a 20 20 2a 2a 20 75 6e 61 72 79 20 54 4b 5f  a.  ** unary TK_
30a47 49 53 4e 55 4c 4c 20 6f 72 20 54 4b 5f 4e 4f 54  ISNULL or TK_NOT
30a48 4e 55 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 2e  NULL expression.
30a49 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69   */.  static voi
30a4a 64 20 62 69 6e 61 72 79 54 6f 55 6e 61 72 79 49  d binaryToUnaryI
30a4b 66 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 50 61  fNull(Parse *pPa
30a4c 72 73 65 2c 20 45 78 70 72 20 2a 70 59 2c 20 45  rse, Expr *pY, E
30a4d 78 70 72 20 2a 70 41 2c 20 69 6e 74 20 6f 70 29  xpr *pA, int op)
30a4e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  {.    sqlite3 *d
30a4f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
30a50 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
30a51 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26 26 20 70  ocFailed==0 && p
30a52 59 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29  Y->op==TK_NULL )
30a53 7b 0a 20 20 20 20 20 20 70 41 2d 3e 6f 70 20 3d  {.      pA->op =
30a54 20 6f 70 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   op;.      sqlit
30a55 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
30a56 20 70 41 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20   pA->pRight);.  
30a57 20 20 20 20 70 41 2d 3e 70 52 69 67 68 74 20 3d      pA->pRight =
30a58 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
30a59 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 6e   /* Construct an
30a5a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65   expression node
30a5b 20 66 6f 72 20 61 20 75 6e 61 72 79 20 70 72 65   for a unary pre
30a5c 66 69 78 20 6f 70 65 72 61 74 6f 72 0a 20 20 2a  fix operator.  *
30a5d 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20  /.  static void 
30a5e 73 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28  spanUnaryPrefix(
30a5f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
30a60 4f 75 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57  Out,        /* W
30a61 72 69 74 65 20 74 68 65 20 6e 65 77 20 65 78 70  rite the new exp
30a62 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 68 65 72  ression node her
30a63 65 20 2a 2f 0a 20 20 20 20 50 61 72 73 65 20 2a  e */.    Parse *
30a64 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
30a65 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
30a66 78 74 20 74 6f 20 72 65 63 6f 72 64 20 65 72 72  xt to record err
30a67 6f 72 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f  ors */.    int o
30a68 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
30a69 20 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 74 6f    /* The operato
30a6a 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 53 70 61  r */.    ExprSpa
30a6b 6e 20 2a 70 4f 70 65 72 61 6e 64 2c 20 20 20 20  n *pOperand,    
30a6c 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e 64 20 2a  /* The operand *
30a6d 2f 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70 50 72  /.    Token *pPr
30a6e 65 4f 70 20 20 20 20 20 20 20 20 20 2f 2a 20 54  eOp         /* T
30a6f 68 65 20 6f 70 65 72 61 6e 64 20 74 6f 6b 65 6e  he operand token
30a70 20 66 6f 72 20 73 65 74 74 69 6e 67 20 74 68 65   for setting the
30a71 20 73 70 61 6e 20 2a 2f 0a 20 20 29 7b 0a 20 20   span */.  ){.  
30a72 20 20 70 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20    pOut->pExpr = 
30a73 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
30a74 72 73 65 2c 20 6f 70 2c 20 70 4f 70 65 72 61 6e  rse, op, pOperan
30a75 64 2d 3e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  d->pExpr, 0, 0);
30a76 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72  .    pOut->zStar
30a77 74 20 3d 20 70 50 72 65 4f 70 2d 3e 7a 3b 0a 20  t = pPreOp->z;. 
30a78 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20     pOut->zEnd = 
30a79 70 4f 70 65 72 61 6e 64 2d 3e 7a 45 6e 64 3b 0a  pOperand->zEnd;.
30a7a 20 20 7d 0a 2f 2a 20 4e 65 78 74 20 69 73 20 61    }./* Next is a
30a7b 6c 6c 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 2c  ll token values,
30a7c 20 69 6e 20 61 20 66 6f 72 6d 20 73 75 69 74 61   in a form suita
30a7d 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 20 6d  ble for use by m
30a7e 61 6b 65 68 65 61 64 65 72 73 2e 0a 2a 2a 20 54  akeheaders..** T
30a7f 68 69 73 20 73 65 63 74 69 6f 6e 20 77 69 6c 6c  his section will
30a80 20 62 65 20 6e 75 6c 6c 20 75 6e 6c 65 73 73 20   be null unless 
30a81 6c 65 6d 6f 6e 20 69 73 20 72 75 6e 20 77 69 74  lemon is run wit
30a82 68 20 74 68 65 20 2d 6d 20 73 77 69 74 63 68 2e  h the -m switch.
30a83 0a 2a 2f 0a 2f 2a 20 0a 2a 2a 20 54 68 65 73 65  .*/./* .** These
30a84 20 63 6f 6e 73 74 61 6e 74 73 20 28 61 6c 6c 20   constants (all 
30a85 67 65 6e 65 72 61 74 65 64 20 61 75 74 6f 6d 61  generated automa
30a86 74 69 63 61 6c 6c 79 20 62 79 20 74 68 65 20 70  tically by the p
30a87 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 29  arser generator)
30a88 0a 2a 2a 20 73 70 65 63 69 66 79 20 74 68 65 20  .** specify the 
30a89 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66  various kinds of
30a8a 20 74 6f 6b 65 6e 73 20 28 74 65 72 6d 69 6e 61   tokens (termina
30a8b 6c 73 29 20 74 68 61 74 20 74 68 65 20 70 61 72  ls) that the par
30a8c 73 65 72 0a 2a 2a 20 75 6e 64 65 72 73 74 61 6e  ser.** understan
30a8d 64 73 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  ds. .**.** Each 
30a8e 73 79 6d 62 6f 6c 20 68 65 72 65 20 69 73 20 61  symbol here is a
30a8f 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c   terminal symbol
30a90 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e   in the grammar.
30a91 0a 2a 2f 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65  .*/./* Make sure
30a92 20 74 68 65 20 49 4e 54 45 52 46 41 43 45 20 6d   the INTERFACE m
30a93 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e  acro is defined.
30a94 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 54 45  .*/.#ifndef INTE
30a95 52 46 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49  RFACE.# define I
30a96 4e 54 45 52 46 41 43 45 20 31 0a 23 65 6e 64 69  NTERFACE 1.#endi
30a97 66 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 68  f./* The next th
30a98 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 69 73 20  ing included is 
30a99 73 65 72 69 65 73 20 6f 66 20 64 65 66 69 6e 65  series of define
30a9a 73 20 77 68 69 63 68 20 63 6f 6e 74 72 6f 6c 0a  s which control.
30a9b 2a 2a 20 76 61 72 69 6f 75 73 20 61 73 70 65 63  ** various aspec
30a9c 74 73 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61  ts of the genera
30a9d 74 65 64 20 70 61 72 73 65 72 2e 0a 2a 2a 20 20  ted parser..**  
30a9e 20 20 59 59 43 4f 44 45 54 59 50 45 20 20 20 20    YYCODETYPE    
30a9f 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61       is the data
30aa0 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 73   type used for s
30aa1 74 6f 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a  toring terminal.
30aa2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30aa3 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e           and non
30aa4 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73  terminal numbers
30aa5 2e 20 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61  .  "unsigned cha
30aa6 72 22 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  r" is.**        
30aa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
30aa8 73 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65  sed if there are
30aa9 20 66 65 77 65 72 20 74 68 61 6e 20 32 35 30 20   fewer than 250 
30aaa 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 20 20 20  terminals.**    
30aab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30aac 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e     and nontermin
30aad 61 6c 73 2e 20 20 22 69 6e 74 22 20 69 73 20 75  als.  "int" is u
30aae 73 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  sed otherwise..*
30aaf 2a 20 20 20 20 59 59 4e 4f 43 4f 44 45 20 20 20  *    YYNOCODE   
30ab0 20 20 20 20 20 20 20 20 69 73 20 61 20 6e 75 6d          is a num
30ab1 62 65 72 20 6f 66 20 74 79 70 65 20 59 59 43 4f  ber of type YYCO
30ab2 44 45 54 59 50 45 20 77 68 69 63 68 20 63 6f 72  DETYPE which cor
30ab3 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 20 20 20 20  responds.**     
30ab4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ab5 20 20 74 6f 20 6e 6f 20 6c 65 67 61 6c 20 74 65    to no legal te
30ab6 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72  rminal or nonter
30ab7 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 2e 20 20 54  minal number.  T
30ab8 68 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  his.**          
30ab9 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 6d               num
30aba 62 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 66  ber is used to f
30abb 69 6c 6c 20 69 6e 20 65 6d 70 74 79 20 73 6c 6f  ill in empty slo
30abc 74 73 20 6f 66 20 74 68 65 20 68 61 73 68 20 0a  ts of the hash .
30abd 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30abe 20 20 20 20 20 20 20 20 20 74 61 62 6c 65 2e 0a           table..
30abf 2a 2a 20 20 20 20 59 59 46 41 4c 4c 42 41 43 4b  **    YYFALLBACK
30ac0 20 20 20 20 20 20 20 20 20 49 66 20 64 65 66 69           If defi
30ac1 6e 65 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61  ned, this indica
30ac2 74 65 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20  tes that one or 
30ac3 6d 6f 72 65 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20  more tokens.**  
30ac4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ac5 20 20 20 20 20 68 61 76 65 20 66 61 6c 6c 2d 62       have fall-b
30ac6 61 63 6b 20 76 61 6c 75 65 73 20 77 68 69 63 68  ack values which
30ac7 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
30ac8 69 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  if the.**       
30ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30aca 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f  original value o
30acb 66 20 74 68 65 20 74 6f 6b 65 6e 20 77 69 6c 6c  f the token will
30acc 20 6e 6f 74 20 70 61 72 73 65 2e 0a 2a 2a 20 20   not parse..**  
30acd 20 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 20    YYACTIONTYPE  
30ace 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61       is the data
30acf 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 73   type used for s
30ad0 74 6f 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a  toring terminal.
30ad1 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30ad2 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e           and non
30ad3 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73  terminal numbers
30ad4 2e 20 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61  .  "unsigned cha
30ad5 72 22 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  r" is.**        
30ad6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
30ad7 73 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65  sed if there are
30ad8 20 66 65 77 65 72 20 74 68 61 6e 20 32 35 30 20   fewer than 250 
30ad9 72 75 6c 65 73 20 61 6e 64 0a 2a 2a 20 20 20 20  rules and.**    
30ada 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30adb 20 20 20 73 74 61 74 65 73 20 63 6f 6d 62 69 6e     states combin
30adc 65 64 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73  ed.  "int" is us
30add 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a  ed otherwise..**
30ade 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
30adf 72 54 4f 4b 45 4e 54 59 50 45 20 20 20 20 20 69  rTOKENTYPE     i
30ae0 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65 20  s the data type 
30ae1 75 73 65 64 20 66 6f 72 20 6d 69 6e 6f 72 20 74  used for minor t
30ae2 6f 6b 65 6e 73 20 67 69 76 65 6e 20 0a 2a 2a 20  okens given .** 
30ae3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ae4 20 20 20 20 20 20 64 69 72 65 63 74 6c 79 20 74        directly t
30ae5 6f 20 74 68 65 20 70 61 72 73 65 72 20 66 72 6f  o the parser fro
30ae6 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e  m the tokenizer.
30ae7 0a 2a 2a 20 20 20 20 59 59 4d 49 4e 4f 52 54 59  .**    YYMINORTY
30ae8 50 45 20 20 20 20 20 20 20 20 69 73 20 74 68 65  PE        is the
30ae9 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
30aea 66 6f 72 20 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f  for all minor to
30aeb 6b 65 6e 73 2e 0a 2a 2a 20 20 20 20 20 20 20 20  kens..**        
30aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
30aed 68 69 73 20 69 73 20 74 79 70 69 63 61 6c 6c 79  his is typically
30aee 20 61 20 75 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79   a union of many
30aef 20 74 79 70 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a   types, one of.*
30af0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
30af1 20 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73          which is
30af2 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f   sqlite3ParserTO
30af3 4b 45 4e 54 59 50 45 2e 20 20 54 68 65 20 65 6e  KENTYPE.  The en
30af4 74 72 79 20 69 6e 20 74 68 65 20 75 6e 69 6f 6e  try in the union
30af5 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
30af6 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 62 61            for ba
30af7 73 65 20 74 6f 6b 65 6e 73 20 69 73 20 63 61 6c  se tokens is cal
30af8 6c 65 64 20 22 79 79 30 22 2e 0a 2a 2a 20 20 20  led "yy0"..**   
30af9 20 59 59 53 54 41 43 4b 44 45 50 54 48 20 20 20   YYSTACKDEPTH   
30afa 20 20 20 20 69 73 20 74 68 65 20 6d 61 78 69 6d      is the maxim
30afb 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  um depth of the 
30afc 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 2e 20  parser's stack. 
30afd 20 49 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   If.**          
30afe 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 65 72               zer
30aff 6f 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 64  o the stack is d
30b00 79 6e 61 6d 69 63 61 6c 6c 79 20 73 69 7a 65 64  ynamically sized
30b01 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29   using realloc()
30b02 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61  .**    sqlite3Pa
30b03 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20  rserARG_SDECL   
30b04 20 20 41 20 73 74 61 74 69 63 20 76 61 72 69 61    A static varia
30b05 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ble declaration 
30b06 66 6f 72 20 74 68 65 20 25 65 78 74 72 61 5f 61  for the %extra_a
30b07 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71  rgument.**    sq
30b08 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50  lite3ParserARG_P
30b09 44 45 43 4c 20 20 20 20 20 41 20 70 61 72 61 6d  DECL     A param
30b0a 65 74 65 72 20 64 65 63 6c 61 72 61 74 69 6f 6e  eter declaration
30b0b 20 66 6f 72 20 74 68 65 20 25 65 78 74 72 61 5f   for the %extra_
30b0c 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73  argument.**    s
30b0d 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
30b0e 53 54 4f 52 45 20 20 20 20 20 43 6f 64 65 20 74  STORE     Code t
30b0f 6f 20 73 74 6f 72 65 20 25 65 78 74 72 61 5f 61  o store %extra_a
30b10 72 67 75 6d 65 6e 74 20 69 6e 74 6f 20 79 79 70  rgument into yyp
30b11 50 61 72 73 65 72 0a 2a 2a 20 20 20 20 73 71 6c  Parser.**    sql
30b12 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
30b13 54 43 48 20 20 20 20 20 43 6f 64 65 20 74 6f 20  TCH     Code to 
30b14 65 78 74 72 61 63 74 20 25 65 78 74 72 61 5f 61  extract %extra_a
30b15 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 79 79 70  rgument from yyp
30b16 50 61 72 73 65 72 0a 2a 2a 20 20 20 20 59 59 4e  Parser.**    YYN
30b17 53 54 41 54 45 20 20 20 20 20 20 20 20 20 20 20  STATE           
30b18 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 6e 75 6d  the combined num
30b19 62 65 72 20 6f 66 20 73 74 61 74 65 73 2e 0a 2a  ber of states..*
30b1a 2a 20 20 20 20 59 59 4e 52 55 4c 45 20 20 20 20  *    YYNRULE    
30b1b 20 20 20 20 20 20 20 20 74 68 65 20 6e 75 6d 62          the numb
30b1c 65 72 20 6f 66 20 72 75 6c 65 73 20 69 6e 20 74  er of rules in t
30b1d 68 65 20 67 72 61 6d 6d 61 72 0a 2a 2a 20 20 20  he grammar.**   
30b1e 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 20   YYERRORSYMBOL  
30b1f 20 20 20 20 69 73 20 74 68 65 20 63 6f 64 65 20      is the code 
30b20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 65 72  number of the er
30b21 72 6f 72 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20  ror symbol.  If 
30b22 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  not.**          
30b23 20 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66               def
30b24 69 6e 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  ined, then do no
30b25 20 65 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e   error processin
30b26 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59  g..*/.#define YY
30b27 43 4f 44 45 54 59 50 45 20 75 6e 73 69 67 6e 65  CODETYPE unsigne
30b28 64 20 63 68 61 72 0a 23 64 65 66 69 6e 65 20 59  d char.#define Y
30b29 59 4e 4f 43 4f 44 45 20 32 35 34 0a 23 64 65 66  YNOCODE 254.#def
30b2a 69 6e 65 20 59 59 41 43 54 49 4f 4e 54 59 50 45  ine YYACTIONTYPE
30b2b 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20   unsigned short 
30b2c 69 6e 74 0a 23 64 65 66 69 6e 65 20 59 59 57 49  int.#define YYWI
30b2d 4c 44 43 41 52 44 20 36 37 0a 23 64 65 66 69 6e  LDCARD 67.#defin
30b2e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54  e sqlite3ParserT
30b2f 4f 4b 45 4e 54 59 50 45 20 54 6f 6b 65 6e 0a 74  OKENTYPE Token.t
30b30 79 70 65 64 65 66 20 75 6e 69 6f 6e 20 7b 0a 20  ypedef union {. 
30b31 20 69 6e 74 20 79 79 69 6e 69 74 3b 0a 20 20 73   int yyinit;.  s
30b32 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
30b33 4e 54 59 50 45 20 79 79 30 3b 0a 20 20 53 65 6c  NTYPE yy0;.  Sel
30b34 65 63 74 2a 20 79 79 33 3b 0a 20 20 45 78 70 72  ect* yy3;.  Expr
30b35 4c 69 73 74 2a 20 79 79 31 34 3b 0a 20 20 53 72  List* yy14;.  Sr
30b36 63 4c 69 73 74 2a 20 79 79 36 35 3b 0a 20 20 73  cList* yy65;.  s
30b37 74 72 75 63 74 20 4c 69 6b 65 4f 70 20 79 79 39  truct LikeOp yy9
30b38 36 3b 0a 20 20 45 78 70 72 2a 20 79 79 31 33 32  6;.  Expr* yy132
30b39 3b 0a 20 20 75 38 20 79 79 31 38 36 3b 0a 20 20  ;.  u8 yy186;.  
30b3a 69 6e 74 20 79 79 33 32 38 3b 0a 20 20 45 78 70  int yy328;.  Exp
30b3b 72 53 70 61 6e 20 79 79 33 34 36 3b 0a 20 20 73  rSpan yy346;.  s
30b3c 74 72 75 63 74 20 54 72 69 67 45 76 65 6e 74 20  truct TrigEvent 
30b3d 79 79 33 37 38 3b 0a 20 20 49 64 4c 69 73 74 2a  yy378;.  IdList*
30b3e 20 79 79 34 30 38 3b 0a 20 20 73 74 72 75 63 74   yy408;.  struct
30b3f 20 7b 69 6e 74 20 76 61 6c 75 65 3b 20 69 6e 74   {int value; int
30b40 20 6d 61 73 6b 3b 7d 20 79 79 34 32 39 3b 0a 20   mask;} yy429;. 
30b41 20 54 72 69 67 67 65 72 53 74 65 70 2a 20 79 79   TriggerStep* yy
30b42 34 37 33 3b 0a 20 20 73 74 72 75 63 74 20 4c 69  473;.  struct Li
30b43 6d 69 74 56 61 6c 20 79 79 34 37 36 3b 0a 7d 20  mitVal yy476;.} 
30b44 59 59 4d 49 4e 4f 52 54 59 50 45 3b 0a 23 69 66  YYMINORTYPE;.#if
30b45 6e 64 65 66 20 59 59 53 54 41 43 4b 44 45 50 54  ndef YYSTACKDEPT
30b46 48 0a 23 64 65 66 69 6e 65 20 59 59 53 54 41 43  H.#define YYSTAC
30b47 4b 44 45 50 54 48 20 31 30 30 0a 23 65 6e 64 69  KDEPTH 100.#endi
30b48 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
30b49 33 50 61 72 73 65 72 41 52 47 5f 53 44 45 43 4c  3ParserARG_SDECL
30b4a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a   Parse *pParse;.
30b4b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50  #define sqlite3P
30b4c 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20 2c  arserARG_PDECL ,
30b4d 50 61 72 73 65 20 2a 70 50 61 72 73 65 0a 23 64  Parse *pParse.#d
30b4e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72  efine sqlite3Par
30b4f 73 65 72 41 52 47 5f 46 45 54 43 48 20 50 61 72  serARG_FETCH Par
30b50 73 65 20 2a 70 50 61 72 73 65 20 3d 20 79 79 70  se *pParse = yyp
30b51 50 61 72 73 65 72 2d 3e 70 50 61 72 73 65 0a 23  Parser->pParse.#
30b52 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61  define sqlite3Pa
30b53 72 73 65 72 41 52 47 5f 53 54 4f 52 45 20 79 79  rserARG_STORE yy
30b54 70 50 61 72 73 65 72 2d 3e 70 50 61 72 73 65 20  pParser->pParse 
30b55 3d 20 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65  = pParse.#define
30b56 20 59 59 4e 53 54 41 54 45 20 36 32 39 0a 23 64   YYNSTATE 629.#d
30b57 65 66 69 6e 65 20 59 59 4e 52 55 4c 45 20 33 32  efine YYNRULE 32
30b58 39 0a 23 64 65 66 69 6e 65 20 59 59 46 41 4c 4c  9.#define YYFALL
30b59 42 41 43 4b 20 31 0a 23 64 65 66 69 6e 65 20 59  BACK 1.#define Y
30b5a 59 5f 4e 4f 5f 41 43 54 49 4f 4e 20 20 20 20 20  Y_NO_ACTION     
30b5b 20 28 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55   (YYNSTATE+YYNRU
30b5c 4c 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 59 59  LE+2).#define YY
30b5d 5f 41 43 43 45 50 54 5f 41 43 54 49 4f 4e 20 20  _ACCEPT_ACTION  
30b5e 28 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c  (YYNSTATE+YYNRUL
30b5f 45 2b 31 29 0a 23 64 65 66 69 6e 65 20 59 59 5f  E+1).#define YY_
30b60 45 52 52 4f 52 5f 41 43 54 49 4f 4e 20 20 20 28  ERROR_ACTION   (
30b61 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45  YYNSTATE+YYNRULE
30b62 29 0a 0a 2f 2a 20 54 68 65 20 79 79 7a 65 72 6f  )../* The yyzero
30b63 6d 69 6e 6f 72 20 63 6f 6e 73 74 61 6e 74 20 69  minor constant i
30b64 73 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61  s used to initia
30b65 6c 69 7a 65 20 69 6e 73 74 61 6e 63 65 73 20 6f  lize instances o
30b66 66 0a 2a 2a 20 59 59 4d 49 4e 4f 52 54 59 50 45  f.** YYMINORTYPE
30b67 20 6f 62 6a 65 63 74 73 20 74 6f 20 7a 65 72 6f   objects to zero
30b68 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  . */.static cons
30b69 74 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79  t YYMINORTYPE yy
30b6a 7a 65 72 6f 6d 69 6e 6f 72 20 3d 20 7b 20 30 20  zerominor = { 0 
30b6b 7d 3b 0a 0a 2f 2a 20 44 65 66 69 6e 65 20 74 68  };../* Define th
30b6c 65 20 79 79 74 65 73 74 63 61 73 65 28 29 20 6d  e yytestcase() m
30b6d 61 63 72 6f 20 74 6f 20 62 65 20 61 20 6e 6f 2d  acro to be a no-
30b6e 6f 70 20 69 66 20 69 73 20 6e 6f 74 20 61 6c 72  op if is not alr
30b6f 65 61 64 79 20 64 65 66 69 6e 65 64 0a 2a 2a 20  eady defined.** 
30b70 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a  otherwise..**.**
30b71 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61   Applications ca
30b72 6e 20 63 68 6f 6f 73 65 20 74 6f 20 64 65 66 69  n choose to defi
30b73 6e 65 20 79 79 74 65 73 74 63 61 73 65 28 29 20  ne yytestcase() 
30b74 69 6e 20 74 68 65 20 25 69 6e 63 6c 75 64 65 20  in the %include 
30b75 73 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 61 20  section.** to a 
30b76 6d 61 63 72 6f 20 74 68 61 74 20 63 61 6e 20 61  macro that can a
30b77 73 73 69 73 74 20 69 6e 20 76 65 72 69 66 79 69  ssist in verifyi
30b78 6e 67 20 63 6f 64 65 20 63 6f 76 65 72 61 67 65  ng code coverage
30b79 2e 20 20 46 6f 72 20 70 72 6f 64 75 63 74 69 6f  .  For productio
30b7a 6e 0a 2a 2a 20 63 6f 64 65 20 74 68 65 20 79 79  n.** code the yy
30b7b 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f  testcase() macro
30b7c 20 73 68 6f 75 6c 64 20 62 65 20 74 75 72 6e 65   should be turne
30b7d 64 20 6f 66 66 2e 20 20 42 75 74 20 69 74 20 69  d off.  But it i
30b7e 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20  s useful.** for 
30b7f 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 6e  testing..*/.#ifn
30b80 64 65 66 20 79 79 74 65 73 74 63 61 73 65 0a 23  def yytestcase.#
30b81 20 64 65 66 69 6e 65 20 79 79 74 65 73 74 63 61   define yytestca
30b82 73 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f  se(X).#endif.../
30b83 2a 20 4e 65 78 74 20 61 72 65 20 74 68 65 20 74  * Next are the t
30b84 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 64 65  ables used to de
30b85 74 65 72 6d 69 6e 65 20 77 68 61 74 20 61 63 74  termine what act
30b86 69 6f 6e 20 74 6f 20 74 61 6b 65 20 62 61 73 65  ion to take base
30b87 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72  d on the.** curr
30b88 65 6e 74 20 73 74 61 74 65 20 61 6e 64 20 6c 6f  ent state and lo
30b89 6f 6b 61 68 65 61 64 20 74 6f 6b 65 6e 2e 20 20  okahead token.  
30b8a 54 68 65 73 65 20 74 61 62 6c 65 73 20 61 72 65  These tables are
30b8b 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
30b8c 6e 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  nt.** functions 
30b8d 74 68 61 74 20 74 61 6b 65 20 61 20 73 74 61 74  that take a stat
30b8e 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 6c 6f 6f  e number and loo
30b8f 6b 61 68 65 61 64 20 76 61 6c 75 65 20 61 6e 64  kahead value and
30b90 20 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20 61 63   return an.** ac
30b91 74 69 6f 6e 20 69 6e 74 65 67 65 72 2e 20 20 0a  tion integer.  .
30b92 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68  **.** Suppose th
30b93 65 20 61 63 74 69 6f 6e 20 69 6e 74 65 67 65 72  e action integer
30b94 20 69 73 20 4e 2e 20 20 54 68 65 6e 20 74 68 65   is N.  Then the
30b95 20 61 63 74 69 6f 6e 20 69 73 20 64 65 74 65 72   action is deter
30b96 6d 69 6e 65 64 20 61 73 0a 2a 2a 20 66 6f 6c 6c  mined as.** foll
30b97 6f 77 73 0a 2a 2a 0a 2a 2a 20 20 20 30 20 3c 3d  ows.**.**   0 <=
30b98 20 4e 20 3c 20 59 59 4e 53 54 41 54 45 20 20 20   N < YYNSTATE   
30b99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
30b9a 68 69 66 74 20 4e 2e 20 20 54 68 61 74 20 69 73  hift N.  That is
30b9b 2c 20 70 75 73 68 20 74 68 65 20 6c 6f 6f 6b 61  , push the looka
30b9c 68 65 61 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  head.**         
30b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30b9e 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
30b9f 65 6e 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63  en onto the stac
30ba0 6b 20 61 6e 64 20 67 6f 74 6f 20 73 74 61 74 65  k and goto state
30ba1 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20 20 59 59 4e 53   N..**.**   YYNS
30ba2 54 41 54 45 20 3c 3d 20 4e 20 3c 20 59 59 4e 53  TATE <= N < YYNS
30ba3 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20 20 52  TATE+YYNRULE   R
30ba4 65 64 75 63 65 20 62 79 20 72 75 6c 65 20 4e 2d  educe by rule N-
30ba5 59 59 4e 53 54 41 54 45 2e 0a 2a 2a 0a 2a 2a 20  YYNSTATE..**.** 
30ba6 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b    N == YYNSTATE+
30ba7 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20 20  YYNRULE         
30ba8 20 20 20 20 20 41 20 73 79 6e 74 61 78 20 65 72       A syntax er
30ba9 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
30baa 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59  ..**.**   N == Y
30bab 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b  YNSTATE+YYNRULE+
30bac 31 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65  1            The
30bad 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73 20   parser accepts 
30bae 69 74 73 20 69 6e 70 75 74 2e 0a 2a 2a 0a 2a 2a  its input..**.**
30baf 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45     N == YYNSTATE
30bb0 2b 59 59 4e 52 55 4c 45 2b 32 20 20 20 20 20 20  +YYNRULE+2      
30bb1 20 20 20 20 20 20 4e 6f 20 73 75 63 68 20 61 63        No such ac
30bb2 74 69 6f 6e 2e 20 20 44 65 6e 6f 74 65 73 20 75  tion.  Denotes u
30bb3 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  nused.**        
30bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30bb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 6c                sl
30bb6 6f 74 73 20 69 6e 20 74 68 65 20 79 79 5f 61 63  ots in the yy_ac
30bb7 74 69 6f 6e 5b 5d 20 74 61 62 6c 65 2e 0a 2a 2a  tion[] table..**
30bb8 0a 2a 2a 20 54 68 65 20 61 63 74 69 6f 6e 20 74  .** The action t
30bb9 61 62 6c 65 20 69 73 20 63 6f 6e 73 74 72 75 63  able is construc
30bba 74 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20  ted as a single 
30bbb 6c 61 72 67 65 20 74 61 62 6c 65 20 6e 61 6d 65  large table name
30bbc 64 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 2e 0a 2a  d yy_action[]..*
30bbd 2a 20 47 69 76 65 6e 20 73 74 61 74 65 20 53 20  * Given state S 
30bbe 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20 58 2c  and lookahead X,
30bbf 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 63   the action is c
30bc0 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 0a 2a 2a  omputed as.**.**
30bc1 20 20 20 20 20 20 79 79 5f 61 63 74 69 6f 6e 5b        yy_action[
30bc2 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53   yy_shift_ofst[S
30bc3 5d 20 2b 20 58 20 5d 0a 2a 2a 0a 2a 2a 20 49 66  ] + X ].**.** If
30bc4 20 74 68 65 20 69 6e 64 65 78 20 76 61 6c 75 65   the index value
30bc5 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53   yy_shift_ofst[S
30bc6 5d 2b 58 20 69 73 20 6f 75 74 20 6f 66 20 72 61  ]+X is out of ra
30bc7 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 76 61  nge or if the va
30bc8 6c 75 65 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b 61 68  lue.** yy_lookah
30bc9 65 61 64 5b 79 79 5f 73 68 69 66 74 5f 6f 66 73  ead[yy_shift_ofs
30bca 74 5b 53 5d 2b 58 5d 20 69 73 20 6e 6f 74 20 65  t[S]+X] is not e
30bcb 71 75 61 6c 20 74 6f 20 58 20 6f 72 20 69 66 20  qual to X or if 
30bcc 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d  yy_shift_ofst[S]
30bcd 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74 6f 20  .** is equal to 
30bce 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c  YY_SHIFT_USE_DFL
30bcf 54 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  T, it means that
30bd0 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 6e   the action is n
30bd1 6f 74 20 69 6e 20 74 68 65 20 74 61 62 6c 65 0a  ot in the table.
30bd2 2a 2a 20 61 6e 64 20 74 68 61 74 20 79 79 5f 64  ** and that yy_d
30bd3 65 66 61 75 6c 74 5b 53 5d 20 73 68 6f 75 6c 64  efault[S] should
30bd4 20 62 65 20 75 73 65 64 20 69 6e 73 74 65 61 64   be used instead
30bd5 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  .  .**.** The fo
30bd6 72 6d 75 6c 61 20 61 62 6f 76 65 20 69 73 20 66  rmula above is f
30bd7 6f 72 20 63 6f 6d 70 75 74 69 6e 67 20 74 68 65  or computing the
30bd8 20 61 63 74 69 6f 6e 20 77 68 65 6e 20 74 68 65   action when the
30bd9 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 0a 2a 2a   lookahead is.**
30bda 20 61 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d 62   a terminal symb
30bdb 6f 6c 2e 20 20 49 66 20 74 68 65 20 6c 6f 6f 6b  ol.  If the look
30bdc 61 68 65 61 64 20 69 73 20 61 20 6e 6f 6e 2d 74  ahead is a non-t
30bdd 65 72 6d 69 6e 61 6c 20 28 61 73 20 6f 63 63 75  erminal (as occu
30bde 72 73 20 61 66 74 65 72 0a 2a 2a 20 61 20 72 65  rs after.** a re
30bdf 64 75 63 65 20 61 63 74 69 6f 6e 29 20 74 68 65  duce action) the
30be0 6e 20 74 68 65 20 79 79 5f 72 65 64 75 63 65 5f  n the yy_reduce_
30be1 6f 66 73 74 5b 5d 20 61 72 72 61 79 20 69 73 20  ofst[] array is 
30be2 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66  used in place of
30be3 0a 2a 2a 20 74 68 65 20 79 79 5f 73 68 69 66 74  .** the yy_shift
30be4 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20 61 6e  _ofst[] array an
30be5 64 20 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f  d YY_REDUCE_USE_
30be6 44 46 4c 54 20 69 73 20 75 73 65 64 20 69 6e 20  DFLT is used in 
30be7 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 59 59 5f 53  place of.** YY_S
30be8 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2e 0a 2a  HIFT_USE_DFLT..*
30be9 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
30bea 6e 67 20 61 72 65 20 74 68 65 20 74 61 62 6c 65  ng are the table
30beb 73 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20 74  s generated in t
30bec 68 69 73 20 73 65 63 74 69 6f 6e 3a 0a 2a 2a 0a  his section:.**.
30bed 2a 2a 20 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20  **  yy_action[] 
30bee 20 20 20 20 20 20 20 41 20 73 69 6e 67 6c 65 20         A single 
30bef 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
30bf0 20 61 6c 6c 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a   all actions..**
30bf1 20 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d    yy_lookahead[]
30bf2 20 20 20 20 20 41 20 74 61 62 6c 65 20 63 6f 6e       A table con
30bf3 74 61 69 6e 69 6e 67 20 74 68 65 20 6c 6f 6f 6b  taining the look
30bf4 61 68 65 61 64 20 66 6f 72 20 65 61 63 68 20 65  ahead for each e
30bf5 6e 74 72 79 20 69 6e 0a 2a 2a 20 20 20 20 20 20  ntry in.**      
30bf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79                 y
30bf7 79 5f 61 63 74 69 6f 6e 2e 20 20 55 73 65 64 20  y_action.  Used 
30bf8 74 6f 20 64 65 74 65 63 74 20 68 61 73 68 20 63  to detect hash c
30bf9 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 2a 2a 20 20 79  ollisions..**  y
30bfa 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20 20  y_shift_ofst[]  
30bfb 20 20 46 6f 72 20 65 61 63 68 20 73 74 61 74 65    For each state
30bfc 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e 74  , the offset int
30bfd 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0a  o yy_action for.
30bfe 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30bff 20 20 20 20 20 20 20 73 68 69 66 74 69 6e 67 20         shifting 
30c00 74 65 72 6d 69 6e 61 6c 73 2e 0a 2a 2a 20 20 79  terminals..**  y
30c01 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20  y_reduce_ofst[] 
30c02 20 20 46 6f 72 20 65 61 63 68 20 73 74 61 74 65    For each state
30c03 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e 74  , the offset int
30c04 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0a  o yy_action for.
30c05 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30c06 20 20 20 20 20 20 20 73 68 69 66 74 69 6e 67 20         shifting 
30c07 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 73 20 61 66  non-terminals af
30c08 74 65 72 20 61 20 72 65 64 75 63 65 2e 0a 2a 2a  ter a reduce..**
30c09 20 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d 20 20    yy_default[]  
30c0a 20 20 20 20 20 44 65 66 61 75 6c 74 20 61 63 74       Default act
30c0b 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 73 74 61  ion for each sta
30c0c 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59  te..*/.#define Y
30c0d 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e 54 20 28  Y_ACTTAB_COUNT (
30c0e 31 35 34 33 29 0a 73 74 61 74 69 63 20 63 6f 6e  1543).static con
30c0f 73 74 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20  st YYACTIONTYPE 
30c10 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 3d 20 7b 0a  yy_action[] = {.
30c11 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 33   /*     0 */   3
30c12 31 32 2c 20 20 20 34 39 2c 20 20 35 35 34 2c 20  12,   49,  554, 
30c13 20 20 34 36 2c 20 20 31 34 37 2c 20 20 31 37 32    46,  147,  172
30c14 2c 20 20 36 32 36 2c 20 20 35 39 36 2c 20 20 20  ,  626,  596,   
30c15 35 35 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20  55,   55,. /*   
30c16 20 31 30 20 2a 2f 20 20 20 20 35 35 2c 20 20 20   10 */    55,   
30c17 35 35 2c 20 20 33 30 31 2c 20 20 20 35 33 2c 20  55,  301,   53, 
30c18 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
30c19 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20  ,   52,   52,   
30c1a 35 31 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f  51,. /*    20 */
30c1b 20 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20      51,   51,   
30c1c 35 30 2c 20 20 32 33 37 2c 20 20 36 31 37 2c 20  50,  237,  617, 
30c1d 20 36 31 36 2c 20 20 36 31 35 2c 20 20 36 32 32   616,  615,  622
30c1e 2c 20 20 36 32 31 2c 20 20 36 30 37 2c 0a 20 2f  ,  621,  607,. /
30c1f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 35 38 39  *    30 */   589
30c20 2c 20 20 35 38 33 2c 20 20 20 34 38 2c 20 20 20  ,  583,   48,   
30c21 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  53,   53,   53, 
30c22 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32    53,   52,   52
30c23 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20 20 34  ,   51,. /*    4
30c24 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 20 35 31  0 */    51,   51
30c25 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 20  ,   50,  237,   
30c26 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  51,   51,   51, 
30c27 20 20 35 30 2c 20 20 32 33 37 2c 20 20 20 35 36    50,  237,   56
30c28 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20  ,. /*    50 */  
30c29 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35 38 31    57,   47,  581
30c2a 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35  ,  580,  582,  5
30c2b 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20  82,   54,   54, 
30c2c 20 20 35 35 2c 20 20 20 35 35 2c 0a 20 2f 2a 20    55,   55,. /* 
30c2d 20 20 20 36 30 20 2a 2f 20 20 20 20 35 35 2c 20     60 */    55, 
30c2e 20 20 35 35 2c 20 20 32 31 36 2c 20 20 20 35 33    55,  216,   53
30c2f 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30c30 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20  53,   52,   52, 
30c31 20 20 35 31 2c 0a 20 2f 2a 20 20 20 20 37 30 20    51,. /*    70 
30c32 2a 2f 20 20 20 20 35 31 2c 20 20 20 35 31 2c 20  */    51,   51, 
30c33 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32    50,  237,  312
30c34 2c 20 20 35 39 36 2c 20 20 20 34 39 2c 20 20 33  ,  596,   49,  3
30c35 32 39 2c 20 20 20 34 36 2c 20 20 31 34 37 2c 0a  29,   46,  147,.
30c36 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20   /*    80 */    
30c37 33 32 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  32,   53,   53, 
30c38 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32    53,   53,   52
30c39 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20  ,   52,   51,   
30c3a 35 31 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20  51,   51,. /*   
30c3b 20 39 30 20 2a 2f 20 20 20 20 35 30 2c 20 20 32   90 */    50,  2
30c3c 33 37 2c 20 20 33 32 39 2c 20 20 36 32 32 2c 20  37,  329,  622, 
30c3d 20 36 32 31 2c 20 20 36 31 39 2c 20 20 36 31 38   621,  619,  618
30c3e 2c 20 20 31 36 36 2c 20 20 34 33 33 2c 20 20 35  ,  166,  433,  5
30c3f 34 37 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  47,. /*   100 */
30c40 20 20 20 33 38 31 2c 20 20 33 37 38 2c 20 20 33     381,  378,  3
30c41 37 37 2c 20 20 35 34 39 2c 20 20 35 38 39 2c 20  77,  549,  589, 
30c42 20 35 38 33 2c 20 20 33 38 39 2c 20 20 34 39 30   583,  389,  490
30c43 2c 20 20 31 36 36 2c 20 20 20 35 38 2c 0a 20 2f  ,  166,   58,. /
30c44 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 33 37 36  *   110 */   376
30c45 2c 20 20 33 38 31 2c 20 20 33 37 38 2c 20 20 33  ,  381,  378,  3
30c46 37 37 2c 20 20 33 39 30 2c 20 20 32 39 39 2c 20  77,  390,  299, 
30c47 20 36 32 32 2c 20 20 36 32 31 2c 20 20 34 38 30   622,  621,  480
30c48 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20 20 31 32  ,   67,. /*   12
30c49 30 20 2a 2f 20 20 20 36 37 30 2c 20 20 33 37 36  0 */   670,  376
30c4a 2c 20 20 36 32 30 2c 20 20 20 35 36 2c 20 20 20  ,  620,   56,   
30c4b 35 37 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20  57,   47,  581, 
30c4c 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38 32   580,  582,  582
30c4d 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20  ,. /*   130 */  
30c4e 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35    54,   54,   55
30c4f 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
30c50 35 35 2c 20 20 32 35 33 2c 20 20 20 35 33 2c 20  55,  253,   53, 
30c51 20 20 35 33 2c 20 20 20 35 33 2c 0a 20 2f 2a 20    53,   53,. /* 
30c52 20 20 31 34 30 20 2a 2f 20 20 20 20 35 33 2c 20    140 */    53, 
30c53 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31    52,   52,   51
30c54 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20  ,   51,   51,   
30c55 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c 20  50,  237,  312, 
30c56 20 34 30 38 2c 0a 20 2f 2a 20 20 20 31 35 30 20   408,. /*   150 
30c57 2a 2f 20 20 20 32 32 35 2c 20 20 35 37 38 2c 20  */   225,  578, 
30c58 20 35 37 38 2c 20 20 31 33 33 2c 20 20 31 37 37   578,  133,  177
30c59 2c 20 20 31 33 39 2c 20 20 32 38 33 2c 20 20 33  ,  139,  283,  3
30c5a 38 34 2c 20 20 32 37 38 2c 20 20 33 38 33 2c 0a  84,  278,  383,.
30c5b 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 31   /*   160 */   1
30c5c 36 39 2c 20 20 36 31 39 2c 20 20 36 31 38 2c 20  69,  619,  618, 
30c5d 20 36 30 31 2c 20 20 31 39 37 2c 20 20 32 32 35   601,  197,  225
30c5e 2c 20 20 32 37 34 2c 20 20 36 30 32 2c 20 20 34  ,  274,  602,  4
30c5f 33 39 2c 20 20 31 34 36 2c 0a 20 2f 2a 20 20 20  39,  146,. /*   
30c60 31 37 30 20 2a 2f 20 20 20 31 33 39 2c 20 20 32  170 */   139,  2
30c61 38 33 2c 20 20 33 38 34 2c 20 20 32 37 38 2c 20  83,  384,  278, 
30c62 20 33 38 33 2c 20 20 31 36 39 2c 20 20 35 36 39   383,  169,  569
30c63 2c 20 20 32 33 35 2c 20 20 35 38 39 2c 20 20 35  ,  235,  589,  5
30c64 38 33 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f  83,. /*   180 */
30c65 20 20 20 32 35 30 2c 20 20 32 37 34 2c 20 20 32     250,  274,  2
30c66 35 32 2c 20 20 36 32 30 2c 20 20 36 31 39 2c 20  52,  620,  619, 
30c67 20 36 31 38 2c 20 20 35 34 36 2c 20 20 34 33 36   618,  546,  436
30c68 2c 20 20 34 34 30 2c 20 20 34 34 31 2c 0a 20 2f  ,  440,  441,. /
30c69 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 31 36 38  *   190 */   168
30c6a 2c 20 20 36 32 32 2c 20 20 36 32 31 2c 20 20 35  ,  622,  621,  5
30c6b 34 37 2c 20 20 34 33 38 2c 20 20 34 33 37 2c 20  47,  438,  437, 
30c6c 20 31 39 32 2c 20 20 20 35 36 2c 20 20 20 35 37   192,   56,   57
30c6d 2c 20 20 20 34 37 2c 0a 20 2f 2a 20 20 20 32 30  ,   47,. /*   20
30c6e 30 20 2a 2f 20 20 20 35 38 31 2c 20 20 35 38 30  0 */   581,  580
30c6f 2c 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20  ,  582,  582,   
30c70 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20  54,   54,   55, 
30c71 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35    55,   55,   55
30c72 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20  ,. /*   210 */  
30c73 20 20 20 36 2c 20 20 20 35 33 2c 20 20 20 35 33     6,   53,   53
30c74 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30c75 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20  52,   52,   51, 
30c76 20 20 35 31 2c 20 20 20 35 31 2c 0a 20 2f 2a 20    51,   51,. /* 
30c77 20 20 32 32 30 20 2a 2f 20 20 20 20 35 30 2c 20    220 */    50, 
30c78 20 32 33 37 2c 20 20 33 31 32 2c 20 20 32 38 32   237,  312,  282
30c79 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20  ,   52,   52,   
30c7a 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  51,   51,   51, 
30c7b 20 20 35 30 2c 0a 20 2f 2a 20 20 20 32 33 30 20    50,. /*   230 
30c7c 2a 2f 20 20 20 32 33 37 2c 20 20 34 39 30 2c 20  */   237,  490, 
30c7d 20 31 38 33 2c 20 20 32 38 31 2c 20 20 35 34 37   183,  281,  547
30c7e 2c 20 20 31 36 36 2c 20 20 34 33 39 2c 20 20 35  ,  166,  439,  5
30c7f 36 35 2c 20 20 33 38 31 2c 20 20 33 37 38 2c 0a  65,  381,  378,.
30c80 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 33   /*   240 */   3
30c81 37 37 2c 20 20 35 39 36 2c 20 20 36 30 36 2c 20  77,  596,  606, 
30c82 20 20 36 37 2c 20 20 33 32 37 2c 20 20 31 37 32    67,  327,  172
30c83 2c 20 20 36 32 30 2c 20 20 35 39 36 2c 20 20 33  ,  620,  596,  3
30c84 37 36 2c 20 20 34 34 32 2c 0a 20 2f 2a 20 20 20  76,  442,. /*   
30c85 32 35 30 20 2a 2f 20 20 20 32 33 36 2c 20 20 36  250 */   236,  6
30c86 32 30 2c 20 20 35 38 39 2c 20 20 35 38 33 2c 20  20,  589,  583, 
30c87 20 33 30 36 2c 20 20 34 32 33 2c 20 20 34 34 30   306,  423,  440
30c88 2c 20 20 33 33 39 2c 20 20 32 35 31 2c 20 20 36  ,  339,  251,  6
30c89 31 39 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  19,. /*   260 */
30c8a 20 20 20 36 31 38 2c 20 20 33 33 31 2c 20 20 35     618,  331,  5
30c8b 37 34 2c 20 20 35 37 33 2c 20 20 20 20 37 2c 20  74,  573,    7, 
30c8c 20 35 32 34 2c 20 20 31 39 34 2c 20 20 34 38 31   524,  194,  481
30c8d 2c 20 20 20 31 36 2c 20 20 35 39 34 2c 0a 20 2f  ,   16,  594,. /
30c8e 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 31 38 39  *   270 */   189
30c8f 2c 20 20 20 35 36 2c 20 20 20 35 37 2c 20 20 20  ,   56,   57,   
30c90 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 20  47,  581,  580, 
30c91 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35 34   582,  582,   54
30c92 2c 20 20 20 35 34 2c 0a 20 2f 2a 20 20 20 32 38  ,   54,. /*   28
30c93 30 20 2a 2f 20 20 20 20 35 35 2c 20 20 20 35 35  0 */    55,   55
30c94 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 35  ,   55,   55,  5
30c95 34 35 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  45,   53,   53, 
30c96 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32    53,   53,   52
30c97 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20  ,. /*   290 */  
30c98 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31    52,   51,   51
30c99 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32  ,   51,   50,  2
30c9a 33 37 2c 20 20 33 31 32 2c 20 20 34 31 30 2c 20  37,  312,  410, 
30c9b 20 34 36 34 2c 20 20 34 32 31 2c 0a 20 2f 2a 20   464,  421,. /* 
30c9c 20 20 33 30 30 20 2a 2f 20 20 20 35 39 32 2c 20    300 */   592, 
30c9d 20 35 39 32 2c 20 20 35 39 32 2c 20 20 36 37 31   592,  592,  671
30c9e 2c 20 20 31 34 36 2c 20 20 34 31 30 2c 20 20 20  ,  146,  410,   
30c9f 20 31 2c 20 20 32 30 35 2c 20 20 34 31 30 2c 20   1,  205,  410, 
30ca0 20 35 39 36 2c 0a 20 2f 2a 20 20 20 33 31 30 20   596,. /*   310 
30ca1 2a 2f 20 20 20 36 32 32 2c 20 20 36 32 31 2c 20  */   622,  621, 
30ca2 20 34 31 33 2c 20 20 34 32 30 2c 20 20 39 34 39   413,  420,  949
30ca3 2c 20 20 35 39 36 2c 20 20 39 34 39 2c 20 20 33  ,  596,  949,  3
30ca4 34 30 2c 20 20 32 33 36 2c 20 20 35 33 30 2c 0a  40,  236,  530,.
30ca5 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 34   /*   320 */   4
30ca6 31 33 2c 20 20 36 30 30 2c 20 20 20 37 34 2c 20  13,  600,   74, 
30ca7 20 34 31 33 2c 20 20 32 33 36 2c 20 20 35 35 32   413,  236,  552
30ca8 2c 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20 35  ,  589,  583,  5
30ca9 34 37 2c 20 20 36 30 30 2c 0a 20 2f 2a 20 20 20  47,  600,. /*   
30caa 33 33 30 20 2a 2f 20 20 20 20 39 35 2c 20 20 20  330 */    95,   
30cab 36 38 2c 20 20 36 30 30 2c 20 20 20 38 38 2c 20  68,  600,   88, 
30cac 20 35 35 31 2c 20 20 36 32 32 2c 20 20 36 32 31   551,  622,  621
30cad 2c 20 20 34 36 35 2c 20 20 35 34 32 2c 20 20 20  ,  465,  542,   
30cae 33 38 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f  38,. /*   340 */
30caf 20 20 20 20 34 39 2c 20 20 35 39 39 2c 20 20 20      49,  599,   
30cb0 34 36 2c 20 20 31 34 37 2c 20 20 34 36 35 2c 20  46,  147,  465, 
30cb1 20 20 35 36 2c 20 20 20 35 37 2c 20 20 20 34 37    56,   57,   47
30cb2 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 0a 20 2f  ,  581,  580,. /
30cb3 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 35 38 32  *   350 */   582
30cb4 2c 20 20 35 38 32 2c 20 20 20 35 34 2c 20 20 20  ,  582,   54,   
30cb5 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  54,   55,   55, 
30cb6 20 20 35 35 2c 20 20 20 35 35 2c 20 20 34 32 34    55,   55,  424
30cb7 2c 20 20 20 35 33 2c 0a 20 2f 2a 20 20 20 33 36  ,   53,. /*   36
30cb8 30 20 2a 2f 20 20 20 20 35 33 2c 20 20 20 35 33  0 */    53,   53
30cb9 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20  ,   53,   52,   
30cba 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  52,   51,   51, 
30cbb 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37    51,   50,  237
30cbc 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20  ,. /*   370 */  
30cbd 20 33 31 32 2c 20 20 33 39 37 2c 20 20 33 39 35   312,  397,  395
30cbe 2c 20 20 32 33 32 2c 20 20 35 32 39 2c 20 20 35  ,  232,  529,  5
30cbf 37 37 2c 20 20 33 38 37 2c 20 20 35 33 33 2c 20  77,  387,  533, 
30cc0 20 36 31 39 2c 20 20 36 31 38 2c 0a 20 2f 2a 20   619,  618,. /* 
30cc1 20 20 33 38 30 20 2a 2f 20 20 20 36 30 35 2c 20    380 */   605, 
30cc2 20 34 39 32 2c 20 20 35 36 30 2c 20 20 35 38 38   492,  560,  588
30cc3 2c 20 20 35 38 37 2c 20 20 33 35 30 2c 20 20 32  ,  587,  350,  2
30cc4 35 37 2c 20 20 36 32 32 2c 20 20 36 32 31 2c 20  57,  622,  621, 
30cc5 20 34 39 35 2c 0a 20 2f 2a 20 20 20 33 39 30 20   495,. /*   390 
30cc6 2a 2f 20 20 20 35 36 34 2c 20 20 33 35 36 2c 20  */   564,  356, 
30cc7 20 33 35 30 2c 20 20 32 35 37 2c 20 20 20 34 39   350,  257,   49
30cc8 2c 20 20 32 33 39 2c 20 20 20 34 36 2c 20 20 31  ,  239,   46,  1
30cc9 34 37 2c 20 20 35 35 39 2c 20 20 33 35 37 2c 0a  47,  559,  357,.
30cca 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 35   /*   400 */   5
30ccb 38 39 2c 20 20 35 38 33 2c 20 20 32 33 39 2c 20  89,  583,  239, 
30ccc 20 36 31 39 2c 20 20 36 31 38 2c 20 20 35 38 35   619,  618,  585
30ccd 2c 20 20 35 38 34 2c 20 20 34 30 38 2c 20 20 32  ,  584,  408,  2
30cce 35 38 2c 20 20 35 37 38 2c 0a 20 2f 2a 20 20 20  58,  578,. /*   
30ccf 34 31 30 20 2a 2f 20 20 20 35 37 38 2c 20 20 36  410 */   578,  6
30cd0 37 32 2c 20 20 32 30 39 2c 20 20 20 33 35 2c 20  72,  209,   35, 
30cd1 20 35 35 38 2c 20 20 32 35 38 2c 20 20 34 30 31   558,  258,  401
30cd2 2c 20 20 36 32 32 2c 20 20 36 32 31 2c 20 20 20  ,  622,  621,   
30cd3 35 36 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f  56,. /*   420 */
30cd4 20 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35      57,   47,  5
30cd5 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20  81,  580,  582, 
30cd6 20 35 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34   582,   54,   54
30cd7 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 0a 20 2f  ,   55,   55,. /
30cd8 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 20 35 35  *   430 */    55
30cd9 2c 20 20 20 35 35 2c 20 20 35 38 36 2c 20 20 20  ,   55,  586,   
30cda 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  53,   53,   53, 
30cdb 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32    53,   52,   52
30cdc 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20 34 34  ,   51,. /*   44
30cdd 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 20 35 31  0 */    51,   51
30cde 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33  ,   50,  237,  3
30cdf 31 32 2c 20 20 35 36 30 2c 20 20 35 39 39 2c 20  12,  560,  599, 
30ce0 20 34 31 30 2c 20 20 35 32 36 2c 20 20 35 33 31   410,  526,  531
30ce1 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20  ,. /*   450 */  
30ce2 20 31 38 34 2c 20 20 35 31 34 2c 20 20 35 31 33   184,  514,  513
30ce3 2c 20 20 34 37 34 2c 20 20 33 36 36 2c 20 20 36  ,  474,  366,  6
30ce4 31 39 2c 20 20 36 31 38 2c 20 20 35 37 36 2c 20  19,  618,  576, 
30ce5 20 34 31 30 2c 20 20 20 36 35 2c 0a 20 2f 2a 20   410,   65,. /* 
30ce6 20 20 34 36 30 20 2a 2f 20 20 20 31 37 36 2c 20    460 */   176, 
30ce7 20 35 35 39 2c 20 20 34 31 33 2c 20 20 34 30 38   559,  413,  408
30ce8 2c 20 20 33 31 31 2c 20 20 35 37 38 2c 20 20 35  ,  311,  578,  5
30ce9 37 38 2c 20 20 35 36 37 2c 20 20 34 39 31 2c 20  78,  567,  491, 
30cea 20 32 31 35 2c 0a 20 2f 2a 20 20 20 34 37 30 20   215,. /*   470 
30ceb 2a 2f 20 20 20 33 35 32 2c 20 20 36 30 30 2c 20  */   352,  600, 
30cec 20 20 39 34 2c 20 20 34 31 33 2c 20 20 35 38 39    94,  413,  589
30ced 2c 20 20 35 38 33 2c 20 20 34 37 34 2c 20 20 35  ,  583,  474,  5
30cee 35 38 2c 20 20 34 30 38 2c 20 20 35 31 38 2c 0a  58,  408,  518,.
30cef 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 35   /*   480 */   5
30cf0 37 38 2c 20 20 35 37 38 2c 20 20 36 30 30 2c 20  78,  578,  600, 
30cf1 20 20 39 35 2c 20 20 34 37 30 2c 20 20 36 31 39    95,  470,  619
30cf2 2c 20 20 36 31 38 2c 20 20 20 36 32 2c 20 20 34  ,  618,   62,  4
30cf3 32 30 2c 20 20 39 34 38 2c 0a 20 2f 2a 20 20 20  20,  948,. /*   
30cf4 34 39 30 20 2a 2f 20 20 20 35 31 37 2c 20 20 39  490 */   517,  9
30cf5 34 38 2c 20 20 33 34 39 2c 20 20 20 35 36 2c 20  48,  349,   56, 
30cf6 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35 38 31    57,   47,  581
30cf7 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35  ,  580,  582,  5
30cf8 38 32 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f  82,. /*   500 */
30cf9 20 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20      54,   54,   
30cfa 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  55,   55,   55, 
30cfb 20 20 35 35 2c 20 20 31 37 35 2c 20 20 20 35 33    55,  175,   53
30cfc 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 0a 20 2f  ,   53,   53,. /
30cfd 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 20 35 33  *   510 */    53
30cfe 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20  ,   52,   52,   
30cff 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  51,   51,   51, 
30d00 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32    50,  237,  312
30d01 2c 20 20 34 39 30 2c 0a 20 2f 2a 20 20 20 35 32  ,  490,. /*   52
30d02 30 20 2a 2f 20 20 20 31 35 37 2c 20 20 34 31 30  0 */   157,  410
30d03 2c 20 20 35 30 39 2c 20 20 32 39 32 2c 20 20 33  ,  509,  292,  3
30d04 39 33 2c 20 20 33 37 33 2c 20 20 33 34 38 2c 20  93,  373,  348, 
30d05 20 34 31 30 2c 20 20 36 32 33 2c 20 20 34 31 30   410,  623,  410
30d06 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20  ,. /*   530 */  
30d07 20 34 32 38 2c 20 20 20 36 37 2c 20 20 36 31 31   428,   67,  611
30d08 2c 20 20 34 32 34 2c 20 20 36 32 30 2c 20 20 34  ,  424,  620,  4
30d09 31 30 2c 20 20 34 31 33 2c 20 20 35 34 30 2c 20  10,  413,  540, 
30d0a 20 34 30 38 2c 20 20 31 37 31 2c 0a 20 2f 2a 20   408,  171,. /* 
30d0b 20 20 35 34 30 20 2a 2f 20 20 20 35 37 38 2c 20    540 */   578, 
30d0c 20 35 37 38 2c 20 20 34 31 33 2c 20 20 36 32 30   578,  413,  620
30d0d 2c 20 20 34 31 33 2c 20 20 36 30 30 2c 20 20 20  ,  413,  600,   
30d0e 37 33 2c 20 20 36 32 30 2c 20 20 35 38 39 2c 20  73,  620,  589, 
30d0f 20 35 38 33 2c 0a 20 2f 2a 20 20 20 35 35 30 20   583,. /*   550 
30d10 2a 2f 20 20 20 34 31 33 2c 20 20 36 30 30 2c 20  */   413,  600, 
30d11 20 20 38 30 2c 20 20 36 30 30 2c 20 20 20 38 38    80,  600,   88
30d12 2c 20 20 32 33 38 2c 20 20 31 36 38 2c 20 20 33  ,  238,  168,  3
30d13 30 36 2c 20 20 34 32 32 2c 20 20 36 30 30 2c 0a  06,  422,  600,.
30d14 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 20   /*   560 */    
30d15 38 30 2c 20 20 32 30 31 2c 20 20 20 31 38 2c 20  80,  201,   18, 
30d16 20 34 36 38 2c 20 20 34 30 36 2c 20 20 35 37 34   468,  406,  574
30d17 2c 20 20 35 37 33 2c 20 20 20 35 36 2c 20 20 20  ,  573,   56,   
30d18 35 37 2c 20 20 20 34 37 2c 0a 20 2f 2a 20 20 20  57,   47,. /*   
30d19 35 37 30 20 2a 2f 20 20 20 35 38 31 2c 20 20 35  570 */   581,  5
30d1a 38 30 2c 20 20 35 38 32 2c 20 20 35 38 32 2c 20  80,  582,  582, 
30d1b 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35    54,   54,   55
30d1c 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
30d1d 35 35 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f  55,. /*   580 */
30d1e 20 20 20 35 37 39 2c 20 20 20 35 33 2c 20 20 20     579,   53,   
30d1f 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  53,   53,   53, 
30d20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31    52,   52,   51
30d21 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 0a 20 2f  ,   51,   51,. /
30d22 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 20 35 30  *   590 */    50
30d23 2c 20 20 32 33 37 2c 20 20 33 31 32 2c 20 20 20  ,  237,  312,   
30d24 34 34 2c 20 20 32 33 33 2c 20 20 35 39 39 2c 20  44,  233,  599, 
30d25 20 32 37 31 2c 20 20 33 32 30 2c 20 20 33 34 31   271,  320,  341
30d26 2c 20 20 34 37 32 2c 0a 20 2f 2a 20 20 20 36 30  ,  472,. /*   60
30d27 30 20 2a 2f 20 20 20 34 31 30 2c 20 20 38 37 34  0 */   410,  874
30d28 2c 20 20 34 32 31 2c 20 20 34 37 33 2c 20 20 35  ,  421,  473,  5
30d29 30 33 2c 20 20 33 31 39 2c 20 20 34 31 30 2c 20  03,  319,  410, 
30d2a 20 32 30 30 2c 20 20 31 34 34 2c 20 20 20 36 36   200,  144,   66
30d2b 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20  ,. /*   610 */  
30d2c 20 33 32 37 2c 20 20 34 38 33 2c 20 20 35 30 38   327,  483,  508
30d2d 2c 20 20 35 39 36 2c 20 20 32 37 34 2c 20 20 34  ,  596,  274,  4
30d2e 31 33 2c 20 20 32 33 39 2c 20 20 33 36 34 2c 20  13,  239,  364, 
30d2f 20 34 38 34 2c 20 20 33 38 32 2c 0a 20 2f 2a 20   484,  382,. /* 
30d30 20 20 36 32 30 20 2a 2f 20 20 20 20 33 30 2c 20    620 */    30, 
30d31 20 34 31 33 2c 20 20 35 38 39 2c 20 20 35 38 33   413,  589,  583
30d32 2c 20 20 36 30 30 2c 20 20 20 36 39 2c 20 20 35  ,  600,   69,  5
30d33 30 32 2c 20 20 32 33 36 2c 20 20 33 34 32 2c 20  02,  236,  342, 
30d34 20 35 37 35 2c 0a 20 2f 2a 20 20 20 36 33 30 20   575,. /*   630 
30d35 2a 2f 20 20 20 36 30 30 2c 20 20 20 39 37 2c 20  */   600,   97, 
30d36 20 31 39 39 2c 20 20 31 39 38 2c 20 20 32 30 39   199,  198,  209
30d37 2c 20 20 39 35 39 2c 20 20 31 38 36 2c 20 20 34  ,  959,  186,  4
30d38 31 38 2c 20 20 20 20 32 2c 20 20 35 36 36 2c 0a  18,    2,  566,.
30d39 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20 20 32   /*   640 */   2
30d3a 36 39 2c 20 20 20 35 36 2c 20 20 20 35 37 2c 20  69,   56,   57, 
30d3b 20 20 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30    47,  581,  580
30d3c 2c 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20  ,  582,  582,   
30d3d 35 34 2c 20 20 20 35 34 2c 0a 20 2f 2a 20 20 20  54,   54,. /*   
30d3e 36 35 30 20 2a 2f 20 20 20 20 35 35 2c 20 20 20  650 */    55,   
30d3f 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  55,   55,   55, 
30d40 20 34 31 30 2c 20 20 20 35 33 2c 20 20 20 35 33   410,   53,   53
30d41 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30d42 35 32 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f  52,. /*   660 */
30d43 20 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20      52,   51,   
30d44 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20  51,   51,   50, 
30d45 20 32 33 37 2c 20 20 33 31 32 2c 20 20 32 36 33   237,  312,  263
30d46 2c 20 20 35 39 39 2c 20 20 34 31 33 2c 0a 20 2f  ,  599,  413,. /
30d47 2a 20 20 20 36 37 30 20 2a 2f 20 20 20 34 31 30  *   670 */   410
30d48 2c 20 20 20 32 31 2c 20 20 31 39 30 2c 20 20 33  ,   21,  190,  3
30d49 35 38 2c 20 20 34 31 30 2c 20 20 33 32 36 2c 20  58,  410,  326, 
30d4a 20 34 31 30 2c 20 20 32 30 32 2c 20 20 36 30 30   410,  202,  600
30d4b 2c 20 20 31 30 30 2c 0a 20 2f 2a 20 20 20 36 38  ,  100,. /*   68
30d4c 30 20 2a 2f 20 20 20 33 38 36 2c 20 20 35 39 36  0 */   386,  596
30d4d 2c 20 20 36 32 30 2c 20 20 35 36 32 2c 20 20 32  ,  620,  562,  2
30d4e 36 35 2c 20 20 34 31 33 2c 20 20 32 36 37 2c 20  65,  413,  267, 
30d4f 20 34 31 30 2c 20 20 36 32 30 2c 20 20 34 31 33   410,  620,  413
30d50 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20  ,. /*   690 */  
30d51 20 35 36 33 2c 20 20 34 31 33 2c 20 20 33 35 32   563,  413,  352
30d52 2c 20 20 20 20 34 2c 20 20 36 30 30 2c 20 20 20  ,    4,  600,   
30d53 39 38 2c 20 20 35 38 39 2c 20 20 35 38 33 2c 20  98,  589,  583, 
30d54 20 36 30 30 2c 20 20 31 30 36 2c 0a 20 2f 2a 20   600,  106,. /* 
30d55 20 20 37 30 30 20 2a 2f 20 20 20 36 30 30 2c 20    700 */   600, 
30d56 20 31 30 34 2c 20 20 34 31 33 2c 20 20 31 37 34   104,  413,  174
30d57 2c 20 20 36 30 31 2c 20 20 36 32 39 2c 20 20 36  ,  601,  629,  6
30d58 32 37 2c 20 20 33 33 33 2c 20 20 36 30 32 2c 20  27,  333,  602, 
30d59 20 20 33 34 2c 0a 20 2f 2a 20 20 20 37 31 30 20    34,. /*   710 
30d5a 2a 2f 20 20 20 33 33 37 2c 20 20 36 30 30 2c 20  */   337,  600, 
30d5b 20 31 30 38 2c 20 20 35 36 31 2c 20 20 33 35 39   108,  561,  359
30d5c 2c 20 20 20 35 36 2c 20 20 20 35 37 2c 20 20 20  ,   56,   57,   
30d5d 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 0a  47,  581,  580,.
30d5e 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 35   /*   720 */   5
30d5f 38 32 2c 20 20 35 38 32 2c 20 20 20 35 34 2c 20  82,  582,   54, 
30d60 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35    54,   55,   55
30d61 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 34  ,   55,   55,  4
30d62 31 30 2c 20 20 20 35 33 2c 0a 20 2f 2a 20 20 20  10,   53,. /*   
30d63 37 33 30 20 2a 2f 20 20 20 20 35 33 2c 20 20 20  730 */    53,   
30d64 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20  53,   53,   52, 
30d65 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31    52,   51,   51
30d66 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32  ,   51,   50,  2
30d67 33 37 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f  37,. /*   740 */
30d68 20 20 20 33 31 32 2c 20 20 34 31 30 2c 20 20 34     312,  410,  4
30d69 39 39 2c 20 20 34 31 33 2c 20 20 31 36 37 2c 20  99,  413,  167, 
30d6a 20 35 36 37 2c 20 20 34 30 35 2c 20 20 32 31 35   567,  405,  215
30d6b 2c 20 20 35 30 34 2c 20 20 35 30 35 2c 0a 20 2f  ,  504,  505,. /
30d6c 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 33 31 36  *   750 */   316
30d6d 2c 20 20 35 35 37 2c 20 20 36 30 30 2c 20 20 31  ,  557,  600,  1
30d6e 30 39 2c 20 20 33 35 33 2c 20 20 20 31 33 2c 20  09,  353,   13, 
30d6f 20 34 31 33 2c 20 20 34 31 30 2c 20 20 20 31 32   413,  410,   12
30d70 2c 20 20 34 31 30 2c 0a 20 2f 2a 20 20 20 37 36  ,  410,. /*   76
30d71 30 20 2a 2f 20 20 20 35 33 38 2c 20 20 34 31 30  0 */   538,  410
30d72 2c 20 20 33 33 35 2c 20 20 33 35 38 2c 20 20 32  ,  335,  358,  2
30d73 32 33 2c 20 20 36 30 30 2c 20 20 31 33 34 2c 20  23,  600,  134, 
30d74 20 35 37 31 2c 20 20 35 37 31 2c 20 20 36 32 30   571,  571,  620
30d75 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20  ,. /*   770 */  
30d76 20 35 38 39 2c 20 20 35 38 33 2c 20 20 34 31 33   589,  583,  413
30d77 2c 20 20 20 32 30 2c 20 20 34 31 33 2c 20 20 36  ,   20,  413,  6
30d78 32 30 2c 20 20 34 31 33 2c 20 20 32 37 32 2c 20  20,  413,  272, 
30d79 20 36 32 30 2c 20 20 31 36 37 2c 0a 20 2f 2a 20   620,  167,. /* 
30d7a 20 20 37 38 30 20 2a 2f 20 20 20 31 36 37 2c 20    780 */   167, 
30d7b 20 36 30 30 2c 20 20 31 33 35 2c 20 20 36 30 30   600,  135,  600
30d7c 2c 20 20 20 36 31 2c 20 20 36 30 30 2c 20 20 31  ,   61,  600,  1
30d7d 30 35 2c 20 20 33 31 37 2c 20 20 31 34 38 2c 20  05,  317,  148, 
30d7e 20 20 35 36 2c 0a 20 2f 2a 20 20 20 37 39 30 20    56,. /*   790 
30d7f 2a 2f 20 20 20 20 35 37 2c 20 20 20 34 37 2c 20  */    57,   47, 
30d80 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32   581,  580,  582
30d81 2c 20 20 35 38 32 2c 20 20 20 35 34 2c 20 20 20  ,  582,   54,   
30d82 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 0a  54,   55,   55,.
30d83 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20 20 20   /*   800 */    
30d84 35 35 2c 20 20 20 35 35 2c 20 20 34 31 30 2c 20  55,   55,  410, 
30d85 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
30d86 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20  ,   53,   52,   
30d87 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20  52,   51,. /*   
30d88 38 31 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 20  810 */    51,   
30d89 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20  51,   50,  237, 
30d8a 20 33 31 32 2c 20 20 34 31 30 2c 20 20 32 37 35   312,  410,  275
30d8b 2c 20 20 34 31 33 2c 20 20 34 31 30 2c 20 20 32  ,  413,  410,  2
30d8c 37 35 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f  75,. /*   820 */
30d8d 20 20 20 32 37 35 2c 20 20 32 32 32 2c 20 20 34     275,  222,  4
30d8e 31 30 2c 20 20 33 33 30 2c 20 20 33 36 33 2c 20  10,  330,  363, 
30d8f 20 35 34 34 2c 20 20 36 30 30 2c 20 20 31 30 33   544,  600,  103
30d90 2c 20 20 31 33 32 2c 20 20 33 36 30 2c 0a 20 2f  ,  132,  360,. /
30d91 2a 20 20 20 38 33 30 20 2a 2f 20 20 20 34 31 33  *   830 */   413
30d92 2c 20 20 36 32 30 2c 20 20 35 32 32 2c 20 20 34  ,  620,  522,  4
30d93 31 33 2c 20 20 36 32 30 2c 20 20 36 32 30 2c 20  13,  620,  620, 
30d94 20 34 31 30 2c 20 20 34 31 33 2c 20 20 31 37 30   410,  413,  170
30d95 2c 20 20 36 30 30 2c 0a 20 2f 2a 20 20 20 38 34  ,  600,. /*   84
30d96 30 20 2a 2f 20 20 20 20 39 36 2c 20 20 36 30 33  0 */    96,  603
30d97 2c 20 20 36 30 30 2c 20 20 31 30 32 2c 20 20 35  ,  600,  102,  5
30d98 38 39 2c 20 20 35 38 33 2c 20 20 36 30 30 2c 20  89,  583,  600, 
30d99 20 20 37 37 2c 20 20 33 37 34 2c 20 20 35 33 36    77,  374,  536
30d9a 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20  ,. /*   850 */  
30d9b 20 31 36 37 2c 20 20 34 31 33 2c 20 20 31 34 33   167,  413,  143
30d9c 2c 20 20 33 32 35 2c 20 20 32 35 36 2c 20 20 20  ,  325,  256,   
30d9d 32 38 2c 20 20 32 32 34 2c 20 20 33 32 34 2c 20  28,  224,  324, 
30d9e 20 35 31 31 2c 20 20 35 32 38 2c 0a 20 2f 2a 20   511,  528,. /* 
30d9f 20 20 38 36 30 20 2a 2f 20 20 20 36 30 30 2c 20    860 */   600, 
30da0 20 20 39 39 2c 20 20 35 32 37 2c 20 20 20 35 36    99,  527,   56
30da1 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35  ,   57,   47,  5
30da2 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20  81,  580,  582, 
30da3 20 35 38 32 2c 0a 20 2f 2a 20 20 20 38 37 30 20   582,. /*   870 
30da4 2a 2f 20 20 20 20 35 34 2c 20 20 20 35 34 2c 20  */    54,   54, 
30da5 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35    55,   55,   55
30da6 2c 20 20 20 35 35 2c 20 20 34 31 30 2c 20 20 20  ,   55,  410,   
30da7 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 0a  53,   53,   53,.
30da8 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 20   /*   880 */    
30da9 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20  53,   52,   52, 
30daa 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31    51,   51,   51
30dab 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33  ,   50,  237,  3
30dac 31 32 2c 20 20 34 31 30 2c 0a 20 2f 2a 20 20 20  12,  410,. /*   
30dad 38 39 30 20 2a 2f 20 20 20 32 37 35 2c 20 20 34  890 */   275,  4
30dae 31 33 2c 20 20 34 31 30 2c 20 20 34 36 39 2c 20  13,  410,  469, 
30daf 20 32 37 35 2c 20 20 31 36 37 2c 20 20 34 35 38   275,  167,  458
30db0 2c 20 20 20 33 39 2c 20 20 31 37 31 2c 20 20 20  ,   39,  171,   
30db1 33 37 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f  37,. /*   900 */
30db2 20 20 20 36 30 30 2c 20 20 31 33 38 2c 20 20 32     600,  138,  2
30db3 31 34 2c 20 20 31 34 34 2c 20 20 34 31 33 2c 20  14,  144,  413, 
30db4 20 36 32 30 2c 20 20 31 34 32 2c 20 20 34 31 33   620,  142,  413
30db5 2c 20 20 34 31 30 2c 20 20 36 32 30 2c 0a 20 2f  ,  410,  620,. /
30db6 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 34 31 30  *   910 */   410
30db7 2c 20 20 33 35 38 2c 20 20 32 33 39 2c 20 20 36  ,  358,  239,  6
30db8 30 30 2c 20 20 31 33 37 2c 20 20 32 33 30 2c 20  00,  137,  230, 
30db9 20 36 30 30 2c 20 20 31 33 36 2c 20 20 35 38 39   600,  136,  589
30dba 2c 20 20 35 38 33 2c 0a 20 2f 2a 20 20 20 39 32  ,  583,. /*   92
30dbb 30 20 2a 2f 20 20 20 34 35 37 2c 20 20 32 36 33  0 */   457,  263
30dbc 2c 20 20 20 32 33 2c 20 20 34 31 33 2c 20 20 33  ,   23,  413,  3
30dbd 35 31 2c 20 20 34 31 33 2c 20 20 36 32 30 2c 20  51,  413,  620, 
30dbe 20 33 32 33 2c 20 20 34 34 35 2c 20 20 35 30 31   323,  445,  501
30dbf 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20  ,. /*   930 */  
30dc0 20 20 32 33 2c 20 20 33 32 32 2c 20 20 36 30 30    23,  322,  600
30dc1 2c 20 20 20 37 36 2c 20 20 36 30 30 2c 20 20 20  ,   76,  600,   
30dc2 39 33 2c 20 20 36 32 30 2c 20 20 20 35 36 2c 20  93,  620,   56, 
30dc3 20 20 34 35 2c 20 20 20 34 37 2c 0a 20 2f 2a 20    45,   47,. /* 
30dc4 20 20 39 34 30 20 2a 2f 20 20 20 35 38 31 2c 20    940 */   581, 
30dc5 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38 32   580,  582,  582
30dc6 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20  ,   54,   54,   
30dc7 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  55,   55,   55, 
30dc8 20 20 35 35 2c 0a 20 2f 2a 20 20 20 39 35 30 20    55,. /*   950 
30dc9 2a 2f 20 20 20 34 31 30 2c 20 20 20 35 33 2c 20  */   410,   53, 
30dca 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
30dcb 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20  ,   52,   52,   
30dcc 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 0a  51,   51,   51,.
30dcd 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20 20 20   /*   960 */    
30dce 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c 20  50,  237,  312, 
30dcf 20 34 31 30 2c 20 20 32 36 32 2c 20 20 34 31 33   410,  262,  413
30dd0 2c 20 20 34 31 30 2c 20 20 34 32 36 2c 20 20 32  ,  410,  426,  2
30dd1 36 33 2c 20 20 33 30 38 2c 0a 20 2f 2a 20 20 20  63,  308,. /*   
30dd2 39 37 30 20 2a 2f 20 20 20 32 30 33 2c 20 20 32  970 */   203,  2
30dd3 31 33 2c 20 20 32 31 32 2c 20 20 33 38 30 2c 20  13,  212,  380, 
30dd4 20 36 30 30 2c 20 20 20 39 32 2c 20 20 35 32 30   600,   92,  520
30dd5 2c 20 20 35 31 39 2c 20 20 34 31 33 2c 20 20 31  ,  519,  413,  1
30dd6 33 30 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f  30,. /*   980 */
30dd7 20 20 20 35 33 38 2c 20 20 34 31 33 2c 20 20 35     538,  413,  5
30dd8 33 38 2c 20 20 36 32 30 2c 20 20 34 31 30 2c 20  38,  620,  410, 
30dd9 20 36 32 38 2c 20 20 20 20 32 2c 20 20 36 30 30   628,    2,  600
30dda 2c 20 20 20 37 35 2c 20 20 32 37 33 2c 0a 20 2f  ,   75,  273,. /
30ddb 2a 20 20 20 39 39 30 20 2a 2f 20 20 20 36 30 30  *   990 */   600
30ddc 2c 20 20 20 39 31 2c 20 20 35 38 39 2c 20 20 35  ,   91,  589,  5
30ddd 38 33 2c 20 20 33 37 35 2c 20 20 36 32 30 2c 20  83,  375,  620, 
30dde 20 31 32 39 2c 20 20 36 32 30 2c 20 20 20 32 37   129,  620,   27
30ddf 2c 20 20 34 31 33 2c 0a 20 2f 2a 20 20 31 30 30  ,  413,. /*  100
30de0 30 20 2a 2f 20 20 20 34 32 35 2c 20 20 33 30 37  0 */   425,  307
30de1 2c 20 20 32 32 31 2c 20 20 31 32 38 2c 20 20 35  ,  221,  128,  5
30de2 39 39 2c 20 20 35 39 39 2c 20 20 35 39 39 2c 20  99,  599,  599, 
30de3 20 32 38 31 2c 20 20 36 30 30 2c 20 20 20 39 30   281,  600,   90
30de4 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20  ,. /*  1010 */  
30de5 20 33 37 31 2c 20 20 34 35 32 2c 20 20 20 35 37   371,  452,   57
30de6 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20 35  ,   47,  581,  5
30de7 38 30 2c 20 20 35 38 32 2c 20 20 35 38 32 2c 20  80,  582,  582, 
30de8 20 20 35 34 2c 20 20 20 35 34 2c 0a 20 2f 2a 20    54,   54,. /* 
30de9 20 31 30 32 30 20 2a 2f 20 20 20 20 35 35 2c 20   1020 */    55, 
30dea 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35    55,   55,   55
30deb 2c 20 20 34 31 30 2c 20 20 20 35 33 2c 20 20 20  ,  410,   53,   
30dec 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  53,   53,   53, 
30ded 20 20 35 32 2c 0a 20 2f 2a 20 20 31 30 33 30 20    52,. /*  1030 
30dee 2a 2f 20 20 20 20 35 32 2c 20 20 20 35 31 2c 20  */    52,   51, 
30def 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30    51,   51,   50
30df0 2c 20 20 32 33 37 2c 20 20 33 31 32 2c 20 20 34  ,  237,  312,  4
30df1 31 30 2c 20 20 32 36 33 2c 20 20 34 31 33 2c 0a  10,  263,  413,.
30df2 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 34   /*  1040 */   4
30df3 31 30 2c 20 20 32 36 33 2c 20 20 32 36 33 2c 20  10,  263,  263, 
30df4 20 33 36 35 2c 20 20 32 30 38 2c 20 20 33 32 31   365,  208,  321
30df5 2c 20 20 32 30 36 2c 20 20 35 34 32 2c 20 20 36  ,  206,  542,  6
30df6 30 30 2c 20 20 31 30 31 2c 0a 20 2f 2a 20 20 31  00,  101,. /*  1
30df7 30 35 30 20 2a 2f 20 20 20 20 35 30 2c 20 20 32  050 */    50,  2
30df8 33 37 2c 20 20 34 31 33 2c 20 20 36 32 30 2c 20  37,  413,  620, 
30df9 20 36 31 30 2c 20 20 34 31 33 2c 20 20 36 32 30   610,  413,  620
30dfa 2c 20 20 36 32 30 2c 20 20 34 31 30 2c 20 20 35  ,  620,  410,  5
30dfb 34 32 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f  42,. /*  1060 */
30dfc 20 20 20 31 36 35 2c 20 20 36 30 30 2c 20 20 20     165,  600,   
30dfd 38 39 2c 20 20 31 38 38 2c 20 20 36 30 30 2c 20  89,  188,  600, 
30dfe 20 20 38 37 2c 20 20 35 38 39 2c 20 20 35 38 33    87,  589,  583
30dff 2c 20 20 34 37 38 2c 20 20 36 32 30 2c 0a 20 2f  ,  478,  620,. /
30e00 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 34 36 37  *  1070 */   467
30e01 2c 20 20 35 31 39 2c 20 20 31 32 35 2c 20 20 34  ,  519,  125,  4
30e02 31 33 2c 20 20 35 36 39 2c 20 20 32 33 35 2c 20  13,  569,  235, 
30e03 20 35 34 32 2c 20 20 33 36 37 2c 20 20 35 39 39   542,  367,  599
30e04 2c 20 20 34 37 35 2c 0a 20 2f 2a 20 20 31 30 38  ,  475,. /*  108
30e05 30 20 2a 2f 20 20 20 35 39 39 2c 20 20 34 35 30  0 */   599,  450
30e06 2c 20 20 36 30 30 2c 20 20 20 38 36 2c 20 20 34  ,  600,   86,  4
30e07 34 39 2c 20 20 34 34 38 2c 20 20 32 33 31 2c 20  49,  448,  231, 
30e08 20 20 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30    47,  581,  580
30e09 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20  ,. /*  1090 */  
30e0a 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35 34   582,  582,   54
30e0b 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20  ,   54,   55,   
30e0c 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  55,   55,   55, 
30e0d 20 32 38 37 2c 20 20 20 35 33 2c 0a 20 2f 2a 20   287,   53,. /* 
30e0e 20 31 31 30 30 20 2a 2f 20 20 20 20 35 33 2c 20   1100 */    53, 
30e0f 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32    53,   53,   52
30e10 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20  ,   52,   51,   
30e11 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20  51,   51,   50, 
30e12 20 32 33 37 2c 0a 20 2f 2a 20 20 31 31 31 30 20   237,. /*  1110 
30e13 2a 2f 20 20 20 20 34 33 2c 20 20 34 30 34 2c 20  */    43,  404, 
30e14 20 34 31 30 2c 20 20 20 20 33 2c 20 20 34 31 30   410,    3,  410
30e15 2c 20 20 32 38 35 2c 20 20 32 36 30 2c 20 20 34  ,  285,  260,  4
30e16 31 34 2c 20 20 36 32 31 2c 20 20 32 36 33 2c 0a  14,  621,  263,.
30e17 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20 20 36   /*  1120 */   6
30e18 30 39 2c 20 20 36 32 37 2c 20 20 33 33 33 2c 20  09,  627,  333, 
30e19 20 20 34 33 2c 20 20 34 30 34 2c 20 20 34 31 30    43,  404,  410
30e1a 2c 20 20 20 20 33 2c 20 20 34 31 33 2c 20 20 34  ,    3,  413,  4
30e1b 30 37 2c 20 20 34 31 33 2c 0a 20 2f 2a 20 20 31  07,  413,. /*  1
30e1c 31 33 30 20 2a 2f 20 20 20 34 31 34 2c 20 20 36  130 */   414,  6
30e1d 32 31 2c 20 20 31 37 31 2c 20 20 32 36 33 2c 20  21,  171,  263, 
30e1e 20 36 32 30 2c 20 20 36 32 30 2c 20 20 36 30 30   620,  620,  600
30e1f 2c 20 20 20 38 35 2c 20 20 36 30 30 2c 20 20 20  ,   85,  600,   
30e20 37 32 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f  72,. /*  1140 */
30e21 20 20 20 34 31 33 2c 20 20 34 30 37 2c 20 20 31     413,  407,  1
30e22 32 34 2c 20 20 31 34 30 2c 20 20 33 35 33 2c 20  24,  140,  353, 
30e23 20 36 30 34 2c 20 20 34 30 39 2c 20 20 34 30 32   604,  409,  402
30e24 2c 20 20 36 32 30 2c 20 20 36 30 30 2c 0a 20 2f  ,  620,  600,. /
30e25 2a 20 20 31 31 35 30 20 2a 2f 20 20 20 20 37 31  *  1150 */    71
30e26 2c 20 20 32 39 31 2c 20 20 34 37 31 2c 20 20 34  ,  291,  471,  4
30e27 39 35 2c 20 20 31 36 30 2c 20 20 31 32 33 2c 20  95,  160,  123, 
30e28 20 35 39 33 2c 20 20 35 36 35 2c 20 20 36 32 30   593,  565,  620
30e29 2c 20 20 36 32 30 2c 0a 20 2f 2a 20 20 31 31 36  ,  620,. /*  116
30e2a 30 20 2a 2f 20 20 20 34 30 32 2c 20 20 36 32 30  0 */   402,  620
30e2b 2c 20 20 32 32 30 2c 20 20 20 31 35 2c 20 20 34  ,  220,   15,  4
30e2c 36 33 2c 20 20 34 36 30 2c 20 20 36 32 30 2c 20  63,  460,  620, 
30e2d 20 34 31 37 2c 20 20 36 32 35 2c 20 20 31 35 39   417,  625,  159
30e2e 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20  ,. /*  1170 */  
30e2f 20 35 36 35 2c 20 20 36 32 30 2c 20 20 33 39 39   565,  620,  399
30e30 2c 20 20 32 34 30 2c 20 20 31 35 38 2c 20 20 31  ,  240,  158,  1
30e31 32 36 2c 20 20 32 31 39 2c 20 20 20 34 30 2c 20  26,  219,   40, 
30e32 20 20 34 31 2c 20 20 35 33 32 2c 0a 20 2f 2a 20    41,  532,. /* 
30e33 20 31 31 38 30 20 2a 2f 20 20 20 34 31 30 2c 20   1180 */   410, 
30e34 20 32 30 37 2c 20 20 31 32 31 2c 20 20 31 32 30   207,  121,  120
30e35 2c 20 20 20 34 32 2c 20 20 34 31 32 2c 20 20 34  ,   42,  412,  4
30e36 31 31 2c 20 20 36 32 30 2c 20 20 32 36 33 2c 20  11,  620,  263, 
30e37 20 35 39 34 2c 0a 20 2f 2a 20 20 31 31 39 30 20   594,. /*  1190 
30e38 2a 2f 20 20 20 20 34 30 2c 20 20 20 34 31 2c 20  */    40,   41, 
30e39 20 35 35 36 2c 20 20 35 34 33 2c 20 20 20 32 35   556,  543,   25
30e3a 2c 20 20 34 31 33 2c 20 20 20 31 31 2c 20 20 20  ,  413,   11,   
30e3b 34 32 2c 20 20 34 31 32 2c 20 20 34 31 31 2c 0a  42,  412,  411,.
30e3c 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 20   /*  1200 */    
30e3d 32 34 2c 20 20 31 31 38 2c 20 20 35 39 34 2c 20  24,  118,  594, 
30e3e 20 36 32 30 2c 20 20 36 30 30 2c 20 20 20 38 34   620,  600,   84
30e3f 2c 20 20 34 35 35 2c 20 20 36 32 30 2c 20 20 36  ,  455,  620,  6
30e40 32 30 2c 20 20 20 34 33 2c 0a 20 2f 2a 20 20 31  20,   43,. /*  1
30e41 32 31 30 20 2a 2f 20 20 20 34 30 34 2c 20 20 32  210 */   404,  2
30e42 31 38 2c 20 20 20 20 33 2c 20 20 35 33 39 2c 20  18,    3,  539, 
30e43 20 31 35 36 2c 20 20 35 39 39 2c 20 20 34 31 34   156,  599,  414
30e44 2c 20 20 36 32 31 2c 20 20 31 31 33 2c 20 20 32  ,  621,  113,  2
30e45 33 39 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f  39,. /*  1220 */
30e46 20 20 20 35 39 32 2c 20 20 35 39 32 2c 20 20 35     592,  592,  5
30e47 39 32 2c 20 20 35 39 31 2c 20 20 35 39 30 2c 20  92,  591,  590, 
30e48 20 20 31 34 2c 20 20 31 35 35 2c 20 20 34 30 37    14,  155,  407
30e49 2c 20 20 36 32 30 2c 20 20 35 33 37 2c 0a 20 2f  ,  620,  537,. /
30e4a 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 34 35 31  *  1230 */   451
30e4b 2c 20 20 32 34 37 2c 20 20 34 34 34 2c 20 20 35  ,  247,  444,  5
30e4c 39 32 2c 20 20 35 39 32 2c 20 20 35 39 32 2c 20  92,  592,  592, 
30e4d 20 35 39 31 2c 20 20 35 39 30 2c 20 20 20 31 34   591,  590,   14
30e4e 2c 20 20 33 34 33 2c 0a 20 2f 2a 20 20 31 32 34  ,  343,. /*  124
30e4f 30 20 2a 2f 20 20 20 34 31 30 2c 20 20 31 31 31  0 */   410,  111
30e50 2c 20 20 34 31 30 2c 20 20 32 37 37 2c 20 20 36  ,  410,  277,  6
30e51 32 30 2c 20 20 34 31 30 2c 20 20 34 30 32 2c 20  20,  410,  402, 
30e52 20 34 31 30 2c 20 20 35 30 37 2c 20 20 31 31 30   410,  507,  110
30e53 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20  ,. /*  1250 */  
30e54 20 20 31 30 2c 20 20 20 36 34 2c 20 20 32 30 34    10,   64,  204
30e55 2c 20 20 33 33 36 2c 20 20 34 33 35 2c 20 20 34  ,  336,  435,  4
30e56 31 33 2c 20 20 35 36 35 2c 20 20 34 31 33 2c 20  13,  565,  413, 
30e57 20 36 32 30 2c 20 20 32 37 36 2c 0a 20 2f 2a 20   620,  276,. /* 
30e58 20 31 32 36 30 20 2a 2f 20 20 20 34 31 33 2c 20   1260 */   413, 
30e59 20 34 33 34 2c 20 20 34 31 33 2c 20 20 36 32 30   434,  413,  620
30e5a 2c 20 20 36 30 30 2c 20 20 20 38 33 2c 20 20 36  ,  600,   83,  6
30e5b 30 30 2c 20 20 20 39 35 2c 20 20 33 33 34 2c 20  00,   95,  334, 
30e5c 20 36 30 30 2c 0a 20 2f 2a 20 20 31 32 37 30 20   600,. /*  1270 
30e5d 2a 2f 20 20 20 20 38 32 2c 20 20 36 30 30 2c 20  */    82,  600, 
30e5e 20 20 38 31 2c 20 20 31 35 30 2c 20 20 36 32 30    81,  150,  620
30e5f 2c 20 20 34 38 38 2c 20 20 20 34 30 2c 20 20 20  ,  488,   40,   
30e60 34 31 2c 20 20 32 37 30 2c 20 20 32 36 38 2c 0a  41,  270,  268,.
30e61 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20 20 32   /*  1280 */   2
30e62 36 36 2c 20 20 31 39 31 2c 20 20 33 33 32 2c 20  66,  191,  332, 
30e63 20 20 34 32 2c 20 20 34 31 32 2c 20 20 34 31 31    42,  412,  411
30e64 2c 20 20 35 39 39 2c 20 20 34 31 30 2c 20 20 35  ,  599,  410,  5
30e65 39 34 2c 20 20 32 34 31 2c 0a 20 2f 2a 20 20 31  94,  241,. /*  1
30e66 32 39 30 20 2a 2f 20 20 20 36 32 30 2c 20 20 34  290 */   620,  4
30e67 31 30 2c 20 20 32 36 34 2c 20 20 36 32 30 2c 20  10,  264,  620, 
30e68 20 36 32 30 2c 20 20 36 32 30 2c 20 20 20 33 33   620,  620,   33
30e69 2c 20 20 34 30 34 2c 20 20 34 31 39 2c 20 20 20  ,  404,  419,   
30e6a 20 33 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f   3,. /*  1300 */
30e6b 20 20 20 31 30 37 2c 20 20 32 32 39 2c 20 20 34     107,  229,  4
30e6c 31 33 2c 20 20 34 31 34 2c 20 20 36 32 31 2c 20  13,  414,  621, 
30e6d 20 31 34 39 2c 20 20 34 31 33 2c 20 20 36 32 30   149,  413,  620
30e6e 2c 20 20 33 39 37 2c 20 20 31 38 31 2c 0a 20 2f  ,  397,  181,. /
30e6f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20 32 35 39  *  1310 */   259
30e70 2c 20 20 36 30 30 2c 20 20 20 37 30 2c 20 20 33  ,  600,   70,  3
30e71 39 38 2c 20 20 34 30 37 2c 20 20 36 30 30 2c 20  98,  407,  600, 
30e72 20 20 31 37 2c 20 20 33 31 35 2c 20 20 33 31 34    17,  315,  314
30e73 2c 20 20 35 39 32 2c 0a 20 2f 2a 20 20 31 33 32  ,  592,. /*  132
30e74 30 20 2a 2f 20 20 20 35 39 32 2c 20 20 35 39 32  0 */   592,  592
30e75 2c 20 20 35 39 31 2c 20 20 35 39 30 2c 20 20 20  ,  591,  590,   
30e76 31 34 2c 20 20 36 32 30 2c 20 20 31 32 37 2c 20  14,  620,  127, 
30e77 20 33 36 31 2c 20 20 36 32 34 2c 20 20 32 31 37   361,  624,  217
30e78 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20  ,. /*  1330 */  
30e79 20 34 36 32 2c 20 20 34 36 31 2c 20 20 33 35 34   462,  461,  354
30e7a 2c 20 20 34 30 32 2c 20 20 33 30 34 2c 20 20 33  ,  402,  304,  3
30e7b 30 33 2c 20 20 33 30 32 2c 20 20 31 37 39 2c 20  03,  302,  179, 
30e7c 20 33 30 30 2c 20 20 32 35 34 2c 0a 20 2f 2a 20   300,  254,. /* 
30e7d 20 31 33 34 30 20 2a 2f 20 20 20 36 31 34 2c 20   1340 */   614, 
30e7e 20 34 35 33 2c 20 20 36 32 30 2c 20 20 35 36 35   453,  620,  565
30e7f 2c 20 20 34 35 34 2c 20 20 36 32 30 2c 20 20 36  ,  454,  620,  6
30e80 32 30 2c 20 20 36 32 30 2c 20 20 36 31 33 2c 20  20,  620,  613, 
30e81 20 36 31 32 2c 0a 20 2f 2a 20 20 31 33 35 30 20   612,. /*  1350 
30e82 2a 2f 20 20 20 34 34 33 2c 20 20 34 31 36 2c 20  */   443,  416, 
30e83 20 31 38 30 2c 20 20 32 34 36 2c 20 20 36 32 30   180,  246,  620
30e84 2c 20 20 31 35 31 2c 20 20 34 31 35 2c 20 20 32  ,  151,  415,  2
30e85 34 35 2c 20 20 32 34 33 2c 20 20 36 32 30 2c 0a  45,  243,  620,.
30e86 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20 20 20 31   /*  1360 */   1
30e87 37 38 2c 20 20 35 39 38 2c 20 20 32 34 32 2c 20  78,  598,  242, 
30e88 20 20 34 30 2c 20 20 20 34 31 2c 20 20 36 32 30    40,   41,  620
30e89 2c 20 20 32 34 34 2c 20 20 20 20 38 2c 20 20 36  ,  244,    8,  6
30e8a 32 30 2c 20 20 32 33 39 2c 0a 20 2f 2a 20 20 31  20,  239,. /*  1
30e8b 33 37 30 20 2a 2f 20 20 20 20 34 32 2c 20 20 34  370 */    42,  4
30e8c 31 32 2c 20 20 34 31 31 2c 20 20 36 32 30 2c 20  12,  411,  620, 
30e8d 20 34 31 30 2c 20 20 35 39 34 2c 20 20 34 31 30   410,  594,  410
30e8e 2c 20 20 36 32 30 2c 20 20 20 36 30 2c 20 20 31  ,  620,   60,  1
30e8f 35 33 2c 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f  53,. /*  1380 */
30e90 20 20 20 34 32 39 2c 20 20 34 36 35 2c 20 20 36     429,  465,  6
30e91 32 32 2c 20 20 36 32 31 2c 20 20 32 39 36 2c 20  22,  621,  296, 
30e92 20 31 35 34 2c 20 20 20 33 30 2c 20 20 31 34 35   154,   30,  145
30e93 2c 20 20 31 35 32 2c 20 20 34 31 33 2c 0a 20 2f  ,  152,  413,. /
30e94 2a 20 20 31 33 39 30 20 2a 2f 20 20 20 33 38 38  *  1390 */   388
30e95 2c 20 20 34 31 33 2c 20 20 32 39 35 2c 20 20 33  ,  413,  295,  3
30e96 39 34 2c 20 20 32 39 34 2c 20 20 36 32 30 2c 20  94,  294,  620, 
30e97 20 20 33 31 2c 20 20 33 39 32 2c 20 20 36 30 30    31,  392,  600
30e98 2c 20 20 20 37 39 2c 0a 20 2f 2a 20 20 31 34 30  ,   79,. /*  140
30e99 30 20 2a 2f 20 20 20 36 30 30 2c 20 20 20 37 38  0 */   600,   78
30e9a 2c 20 20 36 32 30 2c 20 20 32 39 30 2c 20 20 33  ,  620,  290,  3
30e9b 39 36 2c 20 20 34 31 33 2c 20 20 35 39 32 2c 20  96,  413,  592, 
30e9c 20 35 39 32 2c 20 20 35 39 32 2c 20 20 35 39 31   592,  592,  591
30e9d 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a 2f 20 20  ,. /*  1410 */  
30e9e 20 35 39 30 2c 20 20 20 31 34 2c 20 20 36 32 30   590,   14,  620
30e9f 2c 20 20 32 39 33 2c 20 20 36 30 30 2c 20 20 20  ,  293,  600,   
30ea0 20 39 2c 20 20 35 39 37 2c 20 20 20 35 39 2c 20   9,  597,   59, 
30ea1 20 36 32 30 2c 20 20 20 33 36 2c 0a 20 2f 2a 20   620,   36,. /* 
30ea2 20 31 34 32 30 20 2a 2f 20 20 20 35 35 35 2c 20   1420 */   555, 
30ea3 20 31 37 33 2c 20 20 35 36 35 2c 20 20 32 33 34   173,  565,  234
30ea4 2c 20 20 31 38 35 2c 20 20 32 38 38 2c 20 20 20  ,  185,  288,   
30ea5 32 39 2c 20 20 35 34 31 2c 20 20 33 39 31 2c 20  29,  541,  391, 
30ea6 20 33 34 35 2c 0a 20 2f 2a 20 20 31 34 33 30 20   345,. /*  1430 
30ea7 2a 2f 20 20 20 32 34 38 2c 20 20 32 38 36 2c 20  */   248,  286, 
30ea8 20 35 32 31 2c 20 20 35 33 35 2c 20 20 33 31 33   521,  535,  313
30ea9 2c 20 20 32 38 34 2c 20 20 33 38 35 2c 20 20 33  ,  284,  385,  3
30eaa 32 38 2c 20 20 35 33 34 2c 20 20 32 33 39 2c 0a  28,  534,  239,.
30eab 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20 20 20 35   /*  1440 */   5
30eac 31 36 2c 20 20 35 31 35 2c 20 20 31 39 36 2c 20  16,  515,  196, 
30ead 20 31 39 35 2c 20 20 32 37 39 2c 20 20 33 31 30   195,  279,  310
30eae 2c 20 20 35 31 31 2c 20 20 35 31 32 2c 20 20 35  ,  511,  512,  5
30eaf 31 30 2c 20 20 31 33 31 2c 0a 20 2f 2a 20 20 31  10,  131,. /*  1
30eb0 34 35 30 20 2a 2f 20 20 20 35 32 34 2c 20 20 32  450 */   524,  2
30eb1 32 37 2c 20 20 32 35 38 2c 20 20 32 32 38 2c 20  27,  258,  228, 
30eb2 20 35 39 34 2c 20 20 33 30 39 2c 20 20 34 38 37   594,  309,  487
30eb3 2c 20 20 34 38 36 2c 20 20 34 39 33 2c 20 20 32  ,  486,  493,  2
30eb4 32 36 2c 0a 20 2f 2a 20 20 31 34 36 30 20 2a 2f  26,. /*  1460 */
30eb5 20 20 20 33 37 32 2c 20 20 34 38 35 2c 20 20 31     372,  485,  1
30eb6 36 34 2c 20 20 33 33 38 2c 20 20 34 37 39 2c 20  64,  338,  479, 
30eb7 20 31 36 33 2c 20 20 33 36 38 2c 20 20 33 37 30   163,  368,  370
30eb8 2c 20 20 31 36 32 2c 20 20 20 32 36 2c 0a 20 2f  ,  162,   26,. /
30eb9 2a 20 20 31 34 37 30 20 2a 2f 20 20 20 32 31 31  *  1470 */   211
30eba 2c 20 20 34 37 37 2c 20 20 32 36 31 2c 20 20 31  ,  477,  261,  1
30ebb 36 31 2c 20 20 31 34 31 2c 20 20 34 37 36 2c 20  61,  141,  476, 
30ebc 20 33 36 32 2c 20 20 34 36 36 2c 20 20 31 32 32   362,  466,  122
30ebd 2c 20 20 31 38 37 2c 0a 20 2f 2a 20 20 31 34 38  ,  187,. /*  148
30ebe 30 20 2a 2f 20 20 20 31 31 39 2c 20 20 34 35 36  0 */   119,  456
30ebf 2c 20 20 33 34 37 2c 20 20 31 31 37 2c 20 20 33  ,  347,  117,  3
30ec0 34 36 2c 20 20 35 39 32 2c 20 20 35 39 32 2c 20  46,  592,  592, 
30ec1 20 35 39 32 2c 20 20 31 31 36 2c 20 20 31 31 35   592,  116,  115
30ec2 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a 2f 20 20  ,. /*  1490 */  
30ec3 20 31 31 34 2c 20 20 34 34 37 2c 20 20 31 31 32   114,  447,  112
30ec4 2c 20 20 31 38 32 2c 20 20 33 31 38 2c 20 20 20  ,  182,  318,   
30ec5 32 32 2c 20 20 34 33 32 2c 20 20 20 31 39 2c 20  22,  432,   19, 
30ec6 20 34 33 31 2c 20 20 34 33 30 2c 0a 20 2f 2a 20   431,  430,. /* 
30ec7 20 31 35 30 30 20 2a 2f 20 20 20 20 36 33 2c 20   1500 */    63, 
30ec8 20 34 32 37 2c 20 20 36 30 38 2c 20 20 31 39 33   427,  608,  193
30ec9 2c 20 20 32 39 37 2c 20 20 35 39 35 2c 20 20 35  ,  297,  595,  5
30eca 37 32 2c 20 20 35 37 30 2c 20 20 34 30 33 2c 20  72,  570,  403, 
30ecb 20 35 35 33 2c 0a 20 2f 2a 20 20 31 35 31 30 20   553,. /*  1510 
30ecc 2a 2f 20 20 20 35 35 30 2c 20 20 32 38 39 2c 20  */   550,  289, 
30ecd 20 32 38 30 2c 20 20 35 30 38 2c 20 20 34 39 38   280,  508,  498
30ece 2c 20 20 34 39 37 2c 20 20 34 39 36 2c 20 20 34  ,  497,  496,  4
30ecf 39 34 2c 20 20 33 37 39 2c 20 20 33 35 35 2c 0a  94,  379,  355,.
30ed0 20 2f 2a 20 20 31 35 32 30 20 2a 2f 20 20 20 34   /*  1520 */   4
30ed1 35 39 2c 20 20 32 35 35 2c 20 20 32 34 39 2c 20  59,  255,  249, 
30ed2 20 33 34 34 2c 20 20 34 34 36 2c 20 20 33 30 35   344,  446,  305
30ed3 2c 20 20 20 20 35 2c 20 20 35 36 38 2c 20 20 35  ,    5,  568,  5
30ed4 34 38 2c 20 20 32 39 38 2c 0a 20 2f 2a 20 20 31  48,  298,. /*  1
30ed5 35 33 30 20 2a 2f 20 20 20 32 39 38 2c 20 20 32  530 */   298,  2
30ed6 31 30 2c 20 20 33 36 39 2c 20 20 32 39 38 2c 20  10,  369,  298, 
30ed7 20 34 30 30 2c 20 20 35 30 36 2c 20 20 35 30 30   400,  506,  500
30ed8 2c 20 20 34 38 39 2c 20 20 35 32 35 2c 20 20 35  ,  489,  525,  5
30ed9 32 33 2c 0a 20 2f 2a 20 20 31 35 34 30 20 2a 2f  23,. /*  1540 */
30eda 20 20 20 34 38 32 2c 20 20 32 33 39 2c 20 20 32     482,  239,  2
30edb 33 37 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63 6f  37,.};.static co
30edc 6e 73 74 20 59 59 43 4f 44 45 54 59 50 45 20 79  nst YYCODETYPE y
30edd 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 3d 20  y_lookahead[] = 
30ede 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20  {. /*     0 */  
30edf 20 20 31 39 2c 20 20 32 32 32 2c 20 20 32 32 33    19,  222,  223
30ee0 2c 20 20 32 32 34 2c 20 20 32 32 35 2c 20 20 20  ,  224,  225,   
30ee1 32 34 2c 20 20 20 20 31 2c 20 20 20 32 36 2c 20  24,    1,   26, 
30ee2 20 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20    77,   78,. /* 
30ee3 20 20 20 31 30 20 2a 2f 20 20 20 20 37 39 2c 20     10 */    79, 
30ee4 20 20 38 30 2c 20 20 20 31 35 2c 20 20 20 38 32    80,   15,   82
30ee5 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
30ee6 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
30ee7 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20 32 30 20    88,. /*    20 
30ee8 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30 2c 20  */    89,   90, 
30ee9 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 20 37    91,   92,    7
30eea 2c 20 20 20 20 38 2c 20 20 20 20 39 2c 20 20 20  ,    8,    9,   
30eeb 32 36 2c 20 20 20 32 37 2c 20 20 20 32 33 2c 0a  26,   27,   23,.
30eec 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 20   /*    30 */    
30eed 34 39 2c 20 20 20 35 30 2c 20 20 20 38 31 2c 20  49,   50,   81, 
30eee 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
30eef 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
30ef0 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20  87,   88,. /*   
30ef1 20 34 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20   40 */    89,   
30ef2 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20  90,   91,   92, 
30ef3 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
30ef4 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
30ef5 36 38 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f  68,. /*    50 */
30ef6 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20      69,   70,   
30ef7 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
30ef8 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
30ef9 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f  ,   77,   78,. /
30efa 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 20 37 39  *    60 */    79
30efb 2c 20 20 20 38 30 2c 20 20 20 32 32 2c 20 20 20  ,   80,   22,   
30efc 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
30efd 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
30efe 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20 37  ,   88,. /*    7
30eff 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30  0 */    89,   90
30f00 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
30f01 31 39 2c 20 20 20 39 34 2c 20 20 32 32 32 2c 20  19,   94,  222, 
30f02 20 20 31 39 2c 20 20 32 32 34 2c 20 20 32 32 35    19,  224,  225
30f03 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20  ,. /*    80 */  
30f04 20 20 32 35 2c 20 20 20 38 32 2c 20 20 20 38 33    25,   82,   83
30f05 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
30f06 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20  86,   87,   88, 
30f07 20 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20    89,   90,. /* 
30f08 20 20 20 39 30 20 2a 2f 20 20 20 20 39 31 2c 20     90 */    91, 
30f09 20 20 39 32 2c 20 20 20 31 39 2c 20 20 20 32 36    92,   19,   26
30f0a 2c 20 20 20 32 37 2c 20 20 20 39 34 2c 20 20 20  ,   27,   94,   
30f0b 39 35 2c 20 20 20 39 36 2c 20 20 32 34 34 2c 20  95,   96,  244, 
30f0c 20 20 32 35 2c 0a 20 2f 2a 20 20 20 31 30 30 20    25,. /*   100 
30f0d 2a 2f 20 20 20 20 39 39 2c 20 20 31 30 30 2c 20  */    99,  100, 
30f0e 20 31 30 31 2c 20 20 20 32 35 2c 20 20 20 34 39   101,   25,   49
30f0f 2c 20 20 20 35 30 2c 20 20 20 31 39 2c 20 20 31  ,   50,   19,  1
30f10 35 30 2c 20 20 20 39 36 2c 20 20 20 35 34 2c 0a  50,   96,   54,.
30f11 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 31   /*   110 */   1
30f12 30 39 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20  09,   99,  100, 
30f13 20 31 30 31 2c 20 20 20 32 37 2c 20 20 31 35 38   101,   27,  158
30f14 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31  ,   26,   27,  1
30f15 36 31 2c 20 20 31 36 32 2c 0a 20 2f 2a 20 20 20  61,  162,. /*   
30f16 31 32 30 20 2a 2f 20 20 20 31 31 37 2c 20 20 31  120 */   117,  1
30f17 30 39 2c 20 20 31 36 35 2c 20 20 20 36 38 2c 20  09,  165,   68, 
30f18 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
30f19 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
30f1a 37 34 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f  74,. /*   130 */
30f1b 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20      75,   76,   
30f1c 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
30f1d 20 20 38 30 2c 20 20 20 31 36 2c 20 20 20 38 32    80,   16,   82
30f1e 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f  ,   83,   84,. /
30f1f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 38 35  *   140 */    85
30f20 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
30f21 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
30f22 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39    91,   92,   19
30f23 2c 20 20 31 31 31 2c 0a 20 2f 2a 20 20 20 31 35  ,  111,. /*   15
30f24 30 20 2a 2f 20 20 20 20 39 32 2c 20 20 31 31 33  0 */    92,  113
30f25 2c 20 20 31 31 34 2c 20 20 20 32 34 2c 20 20 20  ,  114,   24,   
30f26 39 36 2c 20 20 20 39 37 2c 20 20 20 39 38 2c 20  96,   97,   98, 
30f27 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31    99,  100,  101
30f28 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20  ,. /*   160 */  
30f29 20 31 30 32 2c 20 20 20 39 34 2c 20 20 20 39 35   102,   94,   95
30f2a 2c 20 20 31 31 32 2c 20 20 20 32 35 2c 20 20 20  ,  112,   25,   
30f2b 39 32 2c 20 20 31 30 38 2c 20 20 31 31 36 2c 20  92,  108,  116, 
30f2c 20 31 35 30 2c 20 20 20 39 35 2c 0a 20 2f 2a 20   150,   95,. /* 
30f2d 20 20 31 37 30 20 2a 2f 20 20 20 20 39 37 2c 20    170 */    97, 
30f2e 20 20 39 38 2c 20 20 20 39 39 2c 20 20 31 30 30    98,   99,  100
30f2f 2c 20 20 31 30 31 2c 20 20 31 30 32 2c 20 20 20  ,  101,  102,   
30f30 38 36 2c 20 20 20 38 37 2c 20 20 20 34 39 2c 20  86,   87,   49, 
30f31 20 20 35 30 2c 0a 20 2f 2a 20 20 20 31 38 30 20    50,. /*   180 
30f32 2a 2f 20 20 20 20 36 30 2c 20 20 31 30 38 2c 20  */    60,  108, 
30f33 20 20 36 32 2c 20 20 31 36 35 2c 20 20 20 39 34    62,  165,   94
30f34 2c 20 20 20 39 35 2c 20 20 31 31 39 2c 20 20 20  ,   95,  119,   
30f35 39 37 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 0a  97,  170,  171,.
30f36 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 20   /*   190 */    
30f37 35 30 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20  50,   26,   27, 
30f38 20 31 31 39 2c 20 20 31 30 34 2c 20 20 31 30 35   119,  104,  105
30f39 2c 20 20 31 31 38 2c 20 20 20 36 38 2c 20 20 20  ,  118,   68,   
30f3a 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20  69,   70,. /*   
30f3b 32 30 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20  200 */    71,   
30f3c 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20  72,   73,   74, 
30f3d 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
30f3e 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
30f3f 38 30 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f  80,. /*   210 */
30f40 20 20 20 20 32 32 2c 20 20 20 38 32 2c 20 20 20      22,   82,   
30f41 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
30f42 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
30f43 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f  ,   89,   90,. /
30f44 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 20 39 31  *   220 */    91
30f45 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 20  ,   92,   19,   
30f46 39 38 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  98,   86,   87, 
30f47 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
30f48 2c 20 20 20 39 31 2c 0a 20 2f 2a 20 20 20 32 33  ,   91,. /*   23
30f49 30 20 2a 2f 20 20 20 20 39 32 2c 20 20 31 35 30  0 */    92,  150
30f4a 2c 20 20 20 32 33 2c 20 20 31 30 38 2c 20 20 20  ,   23,  108,   
30f4b 32 35 2c 20 20 20 39 36 2c 20 20 31 35 30 2c 20  25,   96,  150, 
30f4c 20 20 36 36 2c 20 20 20 39 39 2c 20 20 31 30 30    66,   99,  100
30f4d 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20  ,. /*   240 */  
30f4e 20 31 30 31 2c 20 20 20 32 36 2c 20 20 31 36 31   101,   26,  161
30f4f 2c 20 20 31 36 32 2c 20 20 31 30 34 2c 20 20 20  ,  162,  104,   
30f50 32 34 2c 20 20 31 36 35 2c 20 20 20 32 36 2c 20  24,  165,   26, 
30f51 20 31 30 39 2c 20 20 32 33 31 2c 0a 20 2f 2a 20   109,  231,. /* 
30f52 20 20 32 35 30 20 2a 2f 20 20 20 32 33 32 2c 20    250 */   232, 
30f53 20 31 36 35 2c 20 20 20 34 39 2c 20 20 20 35 30   165,   49,   50
30f54 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 20 20 31  ,   22,   23,  1
30f55 37 30 2c 20 20 31 37 31 2c 20 20 31 33 38 2c 20  70,  171,  138, 
30f56 20 20 39 34 2c 0a 20 2f 2a 20 20 20 32 36 30 20    94,. /*   260 
30f57 2a 2f 20 20 20 20 39 35 2c 20 20 31 36 39 2c 20  */    95,  169, 
30f58 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20 37 36   170,  171,   76
30f59 2c 20 20 20 39 34 2c 20 20 31 38 35 2c 20 20 31  ,   94,  185,  1
30f5a 38 36 2c 20 20 20 32 32 2c 20 20 20 39 38 2c 0a  86,   22,   98,.
30f5b 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 20   /*   270 */    
30f5c 32 34 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20  24,   68,   69, 
30f5d 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
30f5e 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20  ,   73,   74,   
30f5f 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20  75,   76,. /*   
30f60 32 38 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20  280 */    77,   
30f61 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
30f62 20 31 31 39 2c 20 20 20 38 32 2c 20 20 20 38 33   119,   82,   83
30f63 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
30f64 38 36 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f  86,. /*   290 */
30f65 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20      87,   88,   
30f66 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20  89,   90,   91, 
30f67 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 35 30    92,   19,  150
30f68 2c 20 20 20 31 31 2c 20 20 20 36 37 2c 0a 20 2f  ,   11,   67,. /
30f69 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 31 32 39  *   300 */   129
30f6a 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31  ,  130,  131,  1
30f6b 31 37 2c 20 20 20 39 35 2c 20 20 31 35 30 2c 20  17,   95,  150, 
30f6c 20 20 32 32 2c 20 20 31 36 30 2c 20 20 31 35 30    22,  160,  150
30f6d 2c 20 20 20 39 34 2c 0a 20 2f 2a 20 20 20 33 31  ,   94,. /*   31
30f6e 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 37  0 */    26,   27
30f6f 2c 20 20 31 36 35 2c 20 20 20 32 32 2c 20 20 20  ,  165,   22,   
30f70 32 33 2c 20 20 20 39 34 2c 20 20 20 32 35 2c 20  23,   94,   25, 
30f71 20 32 33 31 2c 20 20 32 33 32 2c 20 20 20 32 33   231,  232,   23
30f72 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20  ,. /*   320 */  
30f73 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35   165,  174,  175
30f74 2c 20 20 31 36 35 2c 20 20 32 33 32 2c 20 20 20  ,  165,  232,   
30f75 33 32 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20  32,   49,   50, 
30f76 20 31 31 39 2c 20 20 31 37 34 2c 0a 20 2f 2a 20   119,  174,. /* 
30f77 20 20 33 33 30 20 2a 2f 20 20 20 31 37 35 2c 20    330 */   175, 
30f78 20 20 32 32 2c 20 20 31 37 34 2c 20 20 31 37 35    22,  174,  175
30f79 2c 20 20 20 34 31 2c 20 20 20 32 36 2c 20 20 20  ,   41,   26,   
30f7a 32 37 2c 20 20 20 35 37 2c 20 20 31 36 36 2c 20  27,   57,  166, 
30f7b 20 31 33 36 2c 0a 20 2f 2a 20 20 20 33 34 30 20   136,. /*   340 
30f7c 2a 2f 20 20 20 32 32 32 2c 20 20 31 39 34 2c 20  */   222,  194, 
30f7d 20 32 32 34 2c 20 20 32 32 35 2c 20 20 20 35 37   224,  225,   57
30f7e 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
30f7f 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 0a  70,   71,   72,.
30f80 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 20   /*   350 */    
30f81 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20  73,   74,   75, 
30f82 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
30f83 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20  ,   79,   80,   
30f84 36 37 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20 20  67,   82,. /*   
30f85 33 36 30 20 2a 2f 20 20 20 20 38 33 2c 20 20 20  360 */    83,   
30f86 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20  84,   85,   86, 
30f87 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
30f88 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20  ,   90,   91,   
30f89 39 32 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f  92,. /*   370 */
30f8a 20 20 20 20 31 39 2c 20 20 32 31 36 2c 20 20 32      19,  216,  2
30f8b 31 34 2c 20 20 32 31 35 2c 20 20 20 32 33 2c 20  14,  215,   23, 
30f8c 20 20 32 33 2c 20 20 32 32 31 2c 20 20 32 30 35    23,  221,  205
30f8d 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 0a 20 2f  ,   94,   95,. /
30f8e 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 31 37 32  *   380 */   172
30f8f 2c 20 20 31 37 33 2c 20 20 20 31 32 2c 20 20 20  ,  173,   12,   
30f90 34 39 2c 20 20 20 35 30 2c 20 20 31 30 35 2c 20  49,   50,  105, 
30f91 20 31 30 36 2c 20 20 20 32 36 2c 20 20 20 32 37   106,   26,   27
30f92 2c 20 20 31 38 31 2c 0a 20 2f 2a 20 20 20 33 39  ,  181,. /*   39
30f93 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 20 31 39  0 */    23,   19
30f94 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 20 20 32  ,  105,  106,  2
30f95 32 32 2c 20 20 31 31 35 2c 20 20 32 32 34 2c 20  22,  115,  224, 
30f96 20 32 32 35 2c 20 20 20 32 38 2c 20 20 20 32 37   225,   28,   27
30f97 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20  ,. /*   400 */  
30f98 20 20 34 39 2c 20 20 20 35 30 2c 20 20 31 31 35    49,   50,  115
30f99 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20  ,   94,   95,   
30f9a 37 31 2c 20 20 20 37 32 2c 20 20 31 31 31 2c 20  71,   72,  111, 
30f9b 20 31 32 38 2c 20 20 31 31 33 2c 0a 20 2f 2a 20   128,  113,. /* 
30f9c 20 20 34 31 30 20 2a 2f 20 20 20 31 31 34 2c 20    410 */   114, 
30f9d 20 31 31 37 2c 20 20 31 36 30 2c 20 20 31 33 36   117,  160,  136
30f9e 2c 20 20 20 34 34 2c 20 20 31 32 38 2c 20 20 20  ,   44,  128,   
30f9f 34 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20  46,   26,   27, 
30fa0 20 20 36 38 2c 0a 20 2f 2a 20 20 20 34 32 30 20    68,. /*   420 
30fa1 2a 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20  */    69,   70, 
30fa2 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
30fa3 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
30fa4 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a  76,   77,   78,.
30fa5 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 20   /*   430 */    
30fa6 37 39 2c 20 20 20 38 30 2c 20 20 20 39 38 2c 20  79,   80,   98, 
30fa7 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
30fa8 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
30fa9 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20  87,   88,. /*   
30faa 34 34 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20  440 */    89,   
30fab 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20  90,   91,   92, 
30fac 20 20 31 39 2c 20 20 20 31 32 2c 20 20 31 39 34    19,   12,  194
30fad 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20 20  ,  150,   23,   
30fae 38 38 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f  88,. /*   450 */
30faf 20 20 20 20 32 33 2c 20 20 20 20 37 2c 20 20 20      23,    7,   
30fb0 20 38 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 20   8,  105,  106, 
30fb1 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20 32 33    94,   95,   23
30fb2 2c 20 20 31 35 30 2c 20 20 20 32 35 2c 0a 20 2f  ,  150,   25,. /
30fb3 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 31 31 37  *   460 */   117
30fb4 2c 20 20 20 32 38 2c 20 20 31 36 35 2c 20 20 31  ,   28,  165,  1
30fb5 31 31 2c 20 20 31 36 33 2c 20 20 31 31 33 2c 20  11,  163,  113, 
30fb6 20 31 31 34 2c 20 20 31 36 36 2c 20 20 31 36 37   114,  166,  167
30fb7 2c 20 20 31 36 38 2c 0a 20 2f 2a 20 20 20 34 37  ,  168,. /*   47
30fb8 30 20 2a 2f 20 20 20 32 31 38 2c 20 20 31 37 34  0 */   218,  174
30fb9 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 20  ,  175,  165,   
30fba 34 39 2c 20 20 20 35 30 2c 20 20 31 32 38 2c 20  49,   50,  128, 
30fbb 20 20 34 34 2c 20 20 31 31 31 2c 20 20 20 34 36    44,  111,   46
30fbc 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20  ,. /*   480 */  
30fbd 20 31 31 33 2c 20 20 31 31 34 2c 20 20 31 37 34   113,  114,  174
30fbe 2c 20 20 31 37 35 2c 20 20 20 32 31 2c 20 20 20  ,  175,   21,   
30fbf 39 34 2c 20 20 20 39 35 2c 20 20 32 33 35 2c 20  94,   95,  235, 
30fc0 20 20 32 32 2c 20 20 20 32 33 2c 0a 20 2f 2a 20    22,   23,. /* 
30fc1 20 20 34 39 30 20 2a 2f 20 20 20 20 35 37 2c 20    490 */    57, 
30fc2 20 20 32 35 2c 20 20 32 34 30 2c 20 20 20 36 38    25,  240,   68
30fc3 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
30fc4 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
30fc5 20 20 37 34 2c 0a 20 2f 2a 20 20 20 35 30 30 20    74,. /*   500 
30fc6 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20  */    75,   76, 
30fc7 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
30fc8 2c 20 20 20 38 30 2c 20 20 31 31 37 2c 20 20 20  ,   80,  117,   
30fc9 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a  82,   83,   84,.
30fca 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 20   /*   510 */    
30fcb 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
30fcc 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
30fcd 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
30fce 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20  19,  150,. /*   
30fcf 35 32 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 31  520 */    25,  1
30fd0 35 30 2c 20 20 20 32 33 2c 20 20 32 32 36 2c 20  50,   23,  226, 
30fd1 20 32 31 36 2c 20 20 20 31 39 2c 20 20 20 36 33   216,   19,   63
30fd2 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31  ,  150,  150,  1
30fd3 35 30 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f  50,. /*   530 */
30fd4 20 20 20 31 36 31 2c 20 20 31 36 32 2c 20 20 31     161,  162,  1
30fd5 35 30 2c 20 20 20 36 37 2c 20 20 31 36 35 2c 20  50,   67,  165, 
30fd6 20 31 35 30 2c 20 20 31 36 35 2c 20 20 20 32 33   150,  165,   23
30fd7 2c 20 20 31 31 31 2c 20 20 20 32 35 2c 0a 20 2f  ,  111,   25,. /
30fd8 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 31 31 33  *   540 */   113
30fd9 2c 20 20 31 31 34 2c 20 20 31 36 35 2c 20 20 31  ,  114,  165,  1
30fda 36 35 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20  65,  165,  174, 
30fdb 20 31 37 35 2c 20 20 31 36 35 2c 20 20 20 34 39   175,  165,   49
30fdc 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 35 35  ,   50,. /*   55
30fdd 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 37 34  0 */   165,  174
30fde 2c 20 20 31 37 35 2c 20 20 31 37 34 2c 20 20 31  ,  175,  174,  1
30fdf 37 35 2c 20 20 31 39 37 2c 20 20 20 35 30 2c 20  75,  197,   50, 
30fe0 20 20 32 32 2c 20 20 20 32 33 2c 20 20 31 37 34    22,   23,  174
30fe1 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20  ,. /*   560 */  
30fe2 20 31 37 35 2c 20 20 31 36 30 2c 20 20 32 30 34   175,  160,  204
30fe3 2c 20 20 31 30 30 2c 20 20 31 36 39 2c 20 20 31  ,  100,  169,  1
30fe4 37 30 2c 20 20 31 37 31 2c 20 20 20 36 38 2c 20  70,  171,   68, 
30fe5 20 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20    69,   70,. /* 
30fe6 20 20 35 37 30 20 2a 2f 20 20 20 20 37 31 2c 20    570 */    71, 
30fe7 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34    72,   73,   74
30fe8 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20  ,   75,   76,   
30fe9 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
30fea 20 20 38 30 2c 0a 20 2f 2a 20 20 20 35 38 30 20    80,. /*   580 
30feb 2a 2f 20 20 20 31 31 32 2c 20 20 20 38 32 2c 20  */   112,   82, 
30fec 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
30fed 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
30fee 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a  88,   89,   90,.
30fef 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 20   /*   590 */    
30ff0 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20  91,   92,   19, 
30ff1 20 20 32 32 2c 20 20 32 31 35 2c 20 20 31 39 34    22,  215,  194
30ff2 2c 20 20 20 32 33 2c 20 20 32 32 30 2c 20 20 20  ,   23,  220,   
30ff3 39 37 2c 20 20 20 33 30 2c 0a 20 2f 2a 20 20 20  97,   30,. /*   
30ff4 36 30 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  600 */   150,  1
30ff5 33 38 2c 20 20 20 36 37 2c 20 20 20 33 34 2c 20  38,   67,   34, 
30ff6 20 20 33 36 2c 20 20 32 32 30 2c 20 20 31 35 30    36,  220,  150
30ff7 2c 20 20 32 30 36 2c 20 20 32 30 37 2c 20 20 20  ,  206,  207,   
30ff8 32 32 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f  22,. /*   610 */
30ff9 20 20 20 31 30 34 2c 20 20 31 38 31 2c 20 20 31     104,  181,  1
30ffa 38 32 2c 20 20 20 32 36 2c 20 20 31 30 38 2c 20  82,   26,  108, 
30ffb 20 31 36 35 2c 20 20 31 31 35 2c 20 20 20 34 38   165,  115,   48
30ffc 2c 20 20 31 38 38 2c 20 20 20 35 31 2c 0a 20 2f  ,  188,   51,. /
30ffd 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 31 32 35  *   620 */   125
30ffe 2c 20 20 31 36 35 2c 20 20 20 34 39 2c 20 20 20  ,  165,   49,   
30fff 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  50,  174,  175, 
31000 20 20 35 38 2c 20 20 32 33 32 2c 20 20 31 32 37    58,  232,  127
31001 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 36 33  ,   23,. /*   63
31002 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35  0 */   174,  175
31003 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 20 20 31  ,  105,  106,  1
31004 36 30 2c 20 20 31 34 32 2c 20 20 31 34 33 2c 20  60,  142,  143, 
31005 20 31 34 34 2c 20 20 31 34 35 2c 20 20 20 32 33   144,  145,   23
31006 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20  ,. /*   640 */  
31007 20 20 31 36 2c 20 20 20 36 38 2c 20 20 20 36 39    16,   68,   69
31008 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
31009 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20  72,   73,   74, 
3100a 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20    75,   76,. /* 
3100b 20 20 36 35 30 20 2a 2f 20 20 20 20 37 37 2c 20    650 */    77, 
3100c 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
3100d 2c 20 20 31 35 30 2c 20 20 20 38 32 2c 20 20 20  ,  150,   82,   
3100e 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
3100f 20 20 38 36 2c 0a 20 2f 2a 20 20 20 36 36 30 20    86,. /*   660 
31010 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20  */    87,   88, 
31011 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31    89,   90,   91
31012 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31  ,   92,   19,  1
31013 35 30 2c 20 20 31 39 34 2c 20 20 31 36 35 2c 0a  50,  194,  165,.
31014 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20 31   /*   670 */   1
31015 35 30 2c 20 20 20 32 34 2c 20 20 20 32 32 2c 20  50,   24,   22, 
31016 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 30 37   150,  150,  107
31017 2c 20 20 31 35 30 2c 20 20 20 32 32 2c 20 20 31  ,  150,   22,  1
31018 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 20  74,  175,. /*   
31019 36 38 30 20 2a 2f 20 20 20 20 38 38 2c 20 20 20  680 */    88,   
3101a 39 34 2c 20 20 31 36 35 2c 20 20 20 32 33 2c 20  94,  165,   23, 
3101b 20 20 36 30 2c 20 20 31 36 35 2c 20 20 20 36 32    60,  165,   62
3101c 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31  ,  150,  165,  1
3101d 36 35 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f  65,. /*   690 */
3101e 20 20 20 20 31 31 2c 20 20 31 36 35 2c 20 20 32      11,  165,  2
3101f 31 38 2c 20 20 20 33 35 2c 20 20 31 37 34 2c 20  18,   35,  174, 
31020 20 31 37 35 2c 20 20 20 34 39 2c 20 20 20 35 30   175,   49,   50
31021 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f  ,  174,  175,. /
31022 2a 20 20 20 37 30 30 20 2a 2f 20 20 20 31 37 34  *   700 */   174
31023 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 20  ,  175,  165,   
31024 32 35 2c 20 20 31 31 32 2c 20 20 20 20 30 2c 20  25,  112,    0, 
31025 20 20 20 31 2c 20 20 20 20 32 2c 20 20 31 31 36     1,    2,  116
31026 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20 37 31  ,   25,. /*   71
31027 30 20 2a 2f 20 20 20 31 39 33 2c 20 20 31 37 34  0 */   193,  174
31028 2c 20 20 31 37 35 2c 20 20 20 32 33 2c 20 20 32  ,  175,   23,  2
31029 34 30 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20  40,   68,   69, 
3102a 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
3102b 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20  ,. /*   720 */  
3102c 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35    73,   74,   75
3102d 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
3102e 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
3102f 20 31 35 30 2c 20 20 20 38 32 2c 0a 20 2f 2a 20   150,   82,. /* 
31030 20 20 37 33 30 20 2a 2f 20 20 20 20 38 33 2c 20    730 */    83, 
31031 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
31032 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20  ,   87,   88,   
31033 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20  89,   90,   91, 
31034 20 20 39 32 2c 0a 20 2f 2a 20 20 20 37 34 30 20    92,. /*   740 
31035 2a 2f 20 20 20 20 31 39 2c 20 20 31 35 30 2c 20  */    19,  150, 
31036 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32 35    23,  165,   25
31037 2c 20 20 31 36 36 2c 20 20 31 36 37 2c 20 20 31  ,  166,  167,  1
31038 36 38 2c 20 20 20 39 37 2c 20 20 20 39 38 2c 0a  68,   97,   98,.
31039 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 31   /*   750 */   1
3103a 35 35 2c 20 20 20 32 33 2c 20 20 31 37 34 2c 20  55,   23,  174, 
3103b 20 31 37 35 2c 20 20 31 35 30 2c 20 20 20 32 35   175,  150,   25
3103c 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20  ,  165,  150,   
3103d 33 35 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20  35,  150,. /*   
3103e 37 36 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  760 */   150,  1
3103f 35 30 2c 20 20 32 34 35 2c 20 20 31 35 30 2c 20  50,  245,  150, 
31040 20 32 34 31 2c 20 20 31 37 34 2c 20 20 31 37 35   241,  174,  175
31041 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31  ,  129,  130,  1
31042 36 35 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f  65,. /*   770 */
31043 20 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 31      49,   50,  1
31044 36 35 2c 20 20 20 35 32 2c 20 20 31 36 35 2c 20  65,   52,  165, 
31045 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20 32 33   165,  165,   23
31046 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 0a 20 2f  ,  165,   25,. /
31047 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 20 32 35  *   780 */    25
31048 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
31049 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c 20  74,  175,  174, 
3104a 20 31 37 35 2c 20 20 32 34 38 2c 20 20 32 34 39   175,  248,  249
3104b 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 37 39  ,   68,. /*   79
3104c 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37 30  0 */    69,   70
3104d 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20  ,   71,   72,   
3104e 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20  73,   74,   75, 
3104f 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
31050 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20  ,. /*   800 */  
31051 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31 35 30    79,   80,  150
31052 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
31053 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20  84,   85,   86, 
31054 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20    87,   88,. /* 
31055 20 20 38 31 30 20 2a 2f 20 20 20 20 38 39 2c 20    810 */    89, 
31056 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32    90,   91,   92
31057 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31  ,   19,  150,  1
31058 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20  50,  165,  150, 
31059 20 31 35 30 2c 0a 20 2f 2a 20 20 20 38 32 30 20   150,. /*   820 
3105a 2a 2f 20 20 20 31 35 30 2c 20 20 32 31 37 2c 20  */   150,  217, 
3105b 20 31 35 30 2c 20 20 32 31 33 2c 20 20 32 32 39   150,  213,  229
3105c 2c 20 20 31 31 39 2c 20 20 31 37 34 2c 20 20 31  ,  119,  174,  1
3105d 37 35 2c 20 20 20 32 32 2c 20 20 32 33 34 2c 0a  75,   22,  234,.
3105e 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20 31   /*   830 */   1
3105f 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  65,  165,  165, 
31060 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
31061 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 20  ,  150,  165,   
31062 33 35 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20  35,  174,. /*   
31063 38 34 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31  840 */   175,  1
31064 37 34 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  74,  174,  175, 
31065 20 20 34 39 2c 20 20 20 35 30 2c 20 20 31 37 34    49,   50,  174
31066 2c 20 20 31 37 35 2c 20 20 20 32 33 2c 20 20 20  ,  175,   23,   
31067 32 37 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f  27,. /*   850 */
31068 20 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20 31      25,  165,  1
31069 31 37 2c 20 20 31 38 37 2c 20 20 32 34 31 2c 20  17,  187,  241, 
3106a 20 20 32 32 2c 20 20 31 38 37 2c 20 20 31 38 37    22,  187,  187
3106b 2c 20 20 31 30 33 2c 20 20 20 32 33 2c 0a 20 2f  ,  103,   23,. /
3106c 2a 20 20 20 38 36 30 20 2a 2f 20 20 20 31 37 34  *   860 */   174
3106d 2c 20 20 31 37 35 2c 20 20 20 32 33 2c 20 20 20  ,  175,   23,   
3106e 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  68,   69,   70, 
3106f 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
31070 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 38 37  ,   74,. /*   87
31071 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36  0 */    75,   76
31072 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20  ,   77,   78,   
31073 37 39 2c 20 20 20 38 30 2c 20 20 31 35 30 2c 20  79,   80,  150, 
31074 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
31075 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20  ,. /*   880 */  
31076 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
31077 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20  ,   88,   89,   
31078 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20  90,   91,   92, 
31079 20 20 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a 20    19,  150,. /* 
3107a 20 20 38 39 30 20 2a 2f 20 20 20 31 35 30 2c 20    890 */   150, 
3107b 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 32 33   165,  150,   23
3107c 2c 20 20 31 35 30 2c 20 20 20 32 35 2c 20 20 20  ,  150,   25,   
3107d 32 33 2c 20 20 31 33 35 2c 20 20 20 32 35 2c 20  23,  135,   25, 
3107e 20 31 33 37 2c 0a 20 2f 2a 20 20 20 39 30 30 20   137,. /*   900 
3107f 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20  */   174,  175, 
31080 20 32 30 36 2c 20 20 32 30 37 2c 20 20 31 36 35   206,  207,  165
31081 2c 20 20 31 36 35 2c 20 20 20 33 39 2c 20 20 31  ,  165,   39,  1
31082 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 0a  65,  150,  165,.
31083 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 31   /*   910 */   1
31084 35 30 2c 20 20 31 35 30 2c 20 20 31 31 35 2c 20  50,  150,  115, 
31085 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 35 32   174,  175,   52
31086 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
31087 34 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20  49,   50,. /*   
31088 39 32 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 31  920 */    23,  1
31089 35 30 2c 20 20 20 32 35 2c 20 20 31 36 35 2c 20  50,   25,  165, 
3108a 20 31 32 37 2c 20 20 31 36 35 2c 20 20 31 36 35   127,  165,  165
3108b 2c 20 20 31 38 37 2c 20 20 20 32 33 2c 20 20 20  ,  187,   23,   
3108c 32 39 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f  29,. /*   930 */
3108d 20 20 20 20 32 35 2c 20 20 31 38 37 2c 20 20 31      25,  187,  1
3108e 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c 20  74,  175,  174, 
3108f 20 31 37 35 2c 20 20 31 36 35 2c 20 20 20 36 38   175,  165,   68
31090 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f  ,   69,   70,. /
31091 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 20 37 31  *   940 */    71
31092 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
31093 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
31094 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
31095 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 39 35  ,   80,. /*   95
31096 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 38 32  0 */   150,   82
31097 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
31098 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
31099 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
3109a 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20  ,. /*   960 */  
3109b 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39    91,   92,   19
3109c 2c 20 20 31 35 30 2c 20 20 31 39 33 2c 20 20 31  ,  150,  193,  1
3109d 36 35 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20  65,  150,   23, 
3109e 20 31 35 30 2c 20 20 20 32 35 2c 0a 20 2f 2a 20   150,   25,. /* 
3109f 20 20 39 37 30 20 2a 2f 20 20 20 31 36 30 2c 20    970 */   160, 
310a0 20 31 36 30 2c 20 20 31 36 30 2c 20 20 20 35 32   160,  160,   52
310a1 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
310a2 39 30 2c 20 20 31 39 31 2c 20 20 31 36 35 2c 20  90,  191,  165, 
310a3 20 20 32 32 2c 0a 20 2f 2a 20 20 20 39 38 30 20    22,. /*   980 
310a4 2a 2f 20 20 20 31 35 30 2c 20 20 31 36 35 2c 20  */   150,  165, 
310a5 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30   150,  165,  150
310a6 2c 20 20 31 34 34 2c 20 20 31 34 35 2c 20 20 31  ,  144,  145,  1
310a7 37 34 2c 20 20 31 37 35 2c 20 20 20 32 33 2c 0a  74,  175,   23,.
310a8 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20 31   /*   990 */   1
310a9 37 34 2c 20 20 31 37 35 2c 20 20 20 34 39 2c 20  74,  175,   49, 
310aa 20 20 35 30 2c 20 20 20 35 32 2c 20 20 31 36 35    50,   52,  165
310ab 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 20 20 20  ,   22,  165,   
310ac 32 32 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31  22,  165,. /*  1
310ad 30 30 30 20 2a 2f 20 20 20 32 35 30 2c 20 20 32  000 */   250,  2
310ae 35 31 2c 20 20 32 34 31 2c 20 20 20 32 32 2c 20  51,  241,   22, 
310af 20 31 39 34 2c 20 20 31 39 34 2c 20 20 31 39 34   194,  194,  194
310b0 2c 20 20 31 30 38 2c 20 20 31 37 34 2c 20 20 31  ,  108,  174,  1
310b1 37 35 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f  75,. /*  1010 */
310b2 20 20 20 20 31 39 2c 20 20 31 39 33 2c 20 20 20      19,  193,   
310b3 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
310b4 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34    72,   73,   74
310b5 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f  ,   75,   76,. /
310b6 2a 20 20 31 30 32 30 20 2a 2f 20 20 20 20 37 37  *  1020 */    77
310b7 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
310b8 38 30 2c 20 20 31 35 30 2c 20 20 20 38 32 2c 20  80,  150,   82, 
310b9 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
310ba 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20 31 30 33  ,   86,. /*  103
310bb 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38  0 */    87,   88
310bc 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20  ,   89,   90,   
310bd 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20  91,   92,   19, 
310be 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36 35   150,  150,  165
310bf 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20  ,. /*  1040 */  
310c0 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30   150,  150,  150
310c1 2c 20 20 32 31 33 2c 20 20 31 36 30 2c 20 20 32  ,  213,  160,  2
310c2 31 33 2c 20 20 31 36 30 2c 20 20 31 36 36 2c 20  13,  160,  166, 
310c3 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20   174,  175,. /* 
310c4 20 31 30 35 30 20 2a 2f 20 20 20 20 39 31 2c 20   1050 */    91, 
310c5 20 20 39 32 2c 20 20 31 36 35 2c 20 20 31 36 35    92,  165,  165
310c6 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31  ,  150,  165,  1
310c7 36 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20  65,  165,  150, 
310c8 20 31 36 36 2c 0a 20 2f 2a 20 20 31 30 36 30 20   166,. /*  1060 
310c9 2a 2f 20 20 20 31 30 32 2c 20 20 31 37 34 2c 20  */   102,  174, 
310ca 20 31 37 35 2c 20 20 20 32 34 2c 20 20 31 37 34   175,   24,  174
310cb 2c 20 20 31 37 35 2c 20 20 20 34 39 2c 20 20 20  ,  175,   49,   
310cc 35 30 2c 20 20 20 32 30 2c 20 20 31 36 35 2c 0a  50,   20,  165,.
310cd 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 31   /*  1070 */   1
310ce 39 30 2c 20 20 31 39 31 2c 20 20 31 30 34 2c 20  90,  191,  104, 
310cf 20 31 36 35 2c 20 20 20 38 36 2c 20 20 20 38 37   165,   86,   87
310d0 2c 20 20 31 36 36 2c 20 20 20 34 33 2c 20 20 31  ,  166,   43,  1
310d1 39 34 2c 20 20 20 35 39 2c 0a 20 2f 2a 20 20 31  94,   59,. /*  1
310d2 30 38 30 20 2a 2f 20 20 20 31 39 34 2c 20 20 31  080 */   194,  1
310d3 39 33 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  93,  174,  175, 
310d4 20 31 39 33 2c 20 20 31 39 33 2c 20 20 32 30 35   193,  193,  205
310d5 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
310d6 37 32 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f  72,. /*  1090 */
310d7 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20      73,   74,   
310d8 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
310d9 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
310da 2c 20 20 32 30 35 2c 20 20 20 38 32 2c 0a 20 2f  ,  205,   82,. /
310db 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 20 38 33  *  1100 */    83
310dc 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
310dd 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20  86,   87,   88, 
310de 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31    89,   90,   91
310df 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20 31 31 31  ,   92,. /*  111
310e0 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 32 30  0 */    19,   20
310e1 2c 20 20 31 35 30 2c 20 20 20 32 32 2c 20 20 31  ,  150,   22,  1
310e2 35 30 2c 20 20 32 30 35 2c 20 20 31 33 38 2c 20  50,  205,  138, 
310e3 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 35 30    26,   27,  150
310e4 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20  ,. /*  1120 */  
310e5 20 31 35 30 2c 20 20 20 20 31 2c 20 20 20 20 32   150,    1,    2
310e6 2c 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 31  ,   19,   20,  1
310e7 35 30 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 20  50,   22,  165, 
310e8 20 20 33 37 2c 20 20 31 36 35 2c 0a 20 2f 2a 20    37,  165,. /* 
310e9 20 31 31 33 30 20 2a 2f 20 20 20 20 32 36 2c 20   1130 */    26, 
310ea 20 20 32 37 2c 20 20 20 32 35 2c 20 20 31 35 30    27,   25,  150
310eb 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31  ,  165,  165,  1
310ec 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c 20  74,  175,  174, 
310ed 20 31 37 35 2c 0a 20 2f 2a 20 20 31 31 34 30 20   175,. /*  1140 
310ee 2a 2f 20 20 20 31 36 35 2c 20 20 20 33 37 2c 20  */   165,   37, 
310ef 20 20 35 33 2c 20 20 31 35 30 2c 20 20 31 35 30    53,  150,  150
310f0 2c 20 20 31 37 33 2c 20 20 31 35 30 2c 20 20 20  ,  173,  150,   
310f1 35 36 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 0a  56,  165,  174,.
310f2 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20 31   /*  1150 */   1
310f3 37 35 2c 20 20 31 35 30 2c 20 20 20 35 33 2c 20  75,  150,   53, 
310f4 20 31 38 31 2c 20 20 31 30 34 2c 20 20 20 32 32   181,  104,   22
310f5 2c 20 20 31 35 30 2c 20 20 20 36 36 2c 20 20 31  ,  150,   66,  1
310f6 36 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31  65,  165,. /*  1
310f7 31 36 30 20 2a 2f 20 20 20 20 35 36 2c 20 20 31  160 */    56,  1
310f8 36 35 2c 20 20 31 39 33 2c 20 20 20 20 35 2c 20  65,  193,    5, 
310f9 20 20 20 31 2c 20 20 20 32 37 2c 20 20 31 36 35     1,   27,  165
310fa 2c 20 20 31 34 36 2c 20 20 31 34 37 2c 20 20 31  ,  146,  147,  1
310fb 31 37 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f  17,. /*  1170 */
310fc 20 20 20 20 36 36 2c 20 20 31 36 35 2c 20 20 31      66,  165,  1
310fd 35 30 2c 20 20 31 35 32 2c 20 20 20 33 35 2c 20  50,  152,   35, 
310fe 20 31 35 34 2c 20 20 31 39 33 2c 20 20 20 38 36   154,  193,   86
310ff 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f  ,   87,   88,. /
31100 2a 20 20 31 31 38 30 20 2a 2f 20 20 20 31 35 30  *  1180 */   150
31101 2c 20 20 31 36 30 2c 20 20 31 30 37 2c 20 20 31  ,  160,  107,  1
31102 32 36 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20  26,   93,   94, 
31103 20 20 39 35 2c 20 20 31 36 35 2c 20 20 31 35 30    95,  165,  150
31104 2c 20 20 20 39 38 2c 0a 20 2f 2a 20 20 31 31 39  ,   98,. /*  119
31105 30 20 2a 2f 20 20 20 20 38 36 2c 20 20 20 38 37  0 */    86,   87
31106 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20  ,  150,  150,   
31107 37 36 2c 20 20 31 36 35 2c 20 20 20 32 32 2c 20  76,  165,   22, 
31108 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35    93,   94,   95
31109 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20  ,. /*  1200 */  
3110a 20 20 37 36 2c 20 20 31 31 38 2c 20 20 20 39 38    76,  118,   98
3110b 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31  ,  165,  174,  1
3110c 37 35 2c 20 20 20 20 31 2c 20 20 31 36 35 2c 20  75,    1,  165, 
3110d 20 31 36 35 2c 20 20 20 31 39 2c 0a 20 2f 2a 20   165,   19,. /* 
3110e 20 31 32 31 30 20 2a 2f 20 20 20 20 32 30 2c 20   1210 */    20, 
3110f 20 32 31 37 2c 20 20 20 32 32 2c 20 20 31 35 30   217,   22,  150
31110 2c 20 20 20 31 36 2c 20 20 31 39 34 2c 20 20 20  ,   16,  194,   
31111 32 36 2c 20 20 20 32 37 2c 20 20 31 31 38 2c 20  26,   27,  118, 
31112 20 31 31 35 2c 0a 20 2f 2a 20 20 31 32 32 30 20   115,. /*  1220 
31113 2a 2f 20 20 20 31 32 39 2c 20 20 31 33 30 2c 20  */   129,  130, 
31114 20 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33   131,  132,  133
31115 2c 20 20 31 33 34 2c 20 20 31 32 30 2c 20 20 20  ,  134,  120,   
31116 33 37 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 0a  37,  165,  150,.
31117 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 20   /*  1230 */    
31118 32 30 2c 20 20 31 39 33 2c 20 20 31 32 37 2c 20  20,  193,  127, 
31119 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31   129,  130,  131
3111a 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20 20 31  ,  132,  133,  1
3111b 33 34 2c 20 20 32 31 38 2c 0a 20 2f 2a 20 20 31  34,  218,. /*  1
3111c 32 34 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  240 */   150,  1
3111d 30 37 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  07,  150,  150, 
3111e 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 35 36   165,  150,   56
3111f 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31  ,  150,  150,  1
31120 32 36 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f  26,. /*  1250 */
31121 20 20 20 20 32 32 2c 20 20 20 31 36 2c 20 20 31      22,   16,  1
31122 36 30 2c 20 20 20 36 35 2c 20 20 20 32 33 2c 20  60,   65,   23, 
31123 20 31 36 35 2c 20 20 20 36 36 2c 20 20 31 36 35   165,   66,  165
31124 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 0a 20 2f  ,  165,  150,. /
31125 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 31 36 35  *  1260 */   165
31126 2c 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 31  ,   23,  165,  1
31127 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  65,  174,  175, 
31128 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 34 37   174,  175,  247
31129 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 31 32 37  ,  174,. /*  127
3112a 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 37 34  0 */   175,  174
3112b 2c 20 20 31 37 35 2c 20 20 20 31 35 2c 20 20 31  ,  175,   15,  1
3112c 36 35 2c 20 20 31 35 30 2c 20 20 20 38 36 2c 20  65,  150,   86, 
3112d 20 20 38 37 2c 20 20 31 35 30 2c 20 20 31 35 30    87,  150,  150
3112e 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20  ,. /*  1280 */  
3112f 20 31 35 30 2c 20 20 20 32 32 2c 20 20 20 20 33   150,   22,    3
31130 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20  ,   93,   94,   
31131 39 35 2c 20 20 31 39 34 2c 20 20 31 35 30 2c 20  95,  194,  150, 
31132 20 20 39 38 2c 20 20 31 34 30 2c 0a 20 2f 2a 20    98,  140,. /* 
31133 20 31 32 39 30 20 2a 2f 20 20 20 31 36 35 2c 20   1290 */   165, 
31134 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36 35   150,  150,  165
31135 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20  ,  165,  165,   
31136 31 39 2c 20 20 20 32 30 2c 20 20 20 20 34 2c 20  19,   20,    4, 
31137 20 20 32 32 2c 0a 20 2f 2a 20 20 31 33 30 30 20    22,. /*  1300 
31138 2a 2f 20 20 20 31 36 34 2c 20 20 31 38 30 2c 20  */   164,  180, 
31139 20 31 36 35 2c 20 20 20 32 36 2c 20 20 20 32 37   165,   26,   27
3113a 2c 20 20 32 34 39 2c 20 20 31 36 35 2c 20 20 31  ,  249,  165,  1
3113b 36 35 2c 20 20 32 31 36 2c 20 20 20 20 36 2c 0a  65,  216,    6,.
3113c 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20 31   /*  1310 */   1
3113d 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  50,  174,  175, 
3113e 20 32 32 31 2c 20 20 20 33 37 2c 20 20 31 37 34   221,   37,  174
3113f 2c 20 20 31 37 35 2c 20 20 32 35 32 2c 20 20 32  ,  175,  252,  2
31140 35 32 2c 20 20 31 32 39 2c 0a 20 2f 2a 20 20 31  52,  129,. /*  1
31141 33 32 30 20 2a 2f 20 20 20 31 33 30 2c 20 20 31  320 */   130,  1
31142 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20  31,  132,  133, 
31143 20 31 33 34 2c 20 20 31 36 35 2c 20 20 31 38 30   134,  165,  180
31144 2c 20 20 31 35 30 2c 20 20 31 34 39 2c 20 20 20  ,  150,  149,   
31145 20 35 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f   5,. /*  1330 */
31146 20 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31     150,  150,  1
31147 35 30 2c 20 20 20 35 36 2c 20 20 20 31 30 2c 20  50,   56,   10, 
31148 20 20 31 31 2c 20 20 20 31 32 2c 20 20 20 31 33    11,   12,   13
31149 2c 20 20 20 31 34 2c 20 20 31 35 30 2c 0a 20 2f  ,   14,  150,. /
3114a 2a 20 20 31 33 34 30 20 2a 2f 20 20 20 31 34 39  *  1340 */   149
3114b 2c 20 20 20 31 37 2c 20 20 31 36 35 2c 20 20 20  ,   17,  165,   
3114c 36 36 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20  66,  150,  165, 
3114d 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 34 39   165,  165,  149
3114e 2c 20 20 20 31 33 2c 0a 20 2f 2a 20 20 31 33 35  ,   13,. /*  135
3114f 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 34 39  0 */   150,  149
31150 2c 20 20 31 35 31 2c 20 20 31 35 30 2c 20 20 31  ,  151,  150,  1
31151 36 35 2c 20 20 20 33 31 2c 20 20 31 35 39 2c 20  65,   31,  159, 
31152 20 20 33 33 2c 20 20 31 35 30 2c 20 20 31 36 35    33,  150,  165
31153 2c 0a 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20 20  ,. /*  1360 */  
31154 20 31 35 31 2c 20 20 31 39 34 2c 20 20 31 35 30   151,  194,  150
31155 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 31  ,   86,   87,  1
31156 36 35 2c 20 20 20 34 32 2c 20 20 20 32 35 2c 20  65,   42,   25, 
31157 20 31 36 35 2c 20 20 31 31 35 2c 0a 20 2f 2a 20   165,  115,. /* 
31158 20 31 33 37 30 20 2a 2f 20 20 20 20 39 33 2c 20   1370 */    93, 
31159 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 36 35    94,   95,  165
3115a 2c 20 20 31 35 30 2c 20 20 20 39 38 2c 20 20 31  ,  150,   98,  1
3115b 35 30 2c 20 20 31 36 35 2c 20 20 20 32 32 2c 20  50,  165,   22, 
3115c 20 20 35 35 2c 0a 20 2f 2a 20 20 31 33 38 30 20    55,. /*  1380 
3115d 2a 2f 20 20 20 31 35 30 2c 20 20 20 35 37 2c 20  */   150,   57, 
3115e 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 39 39    26,   27,  199
3115f 2c 20 20 20 36 31 2c 20 20 31 32 35 2c 20 20 31  ,   61,  125,  1
31160 35 30 2c 20 20 20 36 34 2c 20 20 31 36 35 2c 0a  50,   64,  165,.
31161 20 2f 2a 20 20 31 33 39 30 20 2a 2f 20 20 20 31   /*  1390 */   1
31162 35 30 2c 20 20 31 36 35 2c 20 20 32 30 30 2c 20  50,  165,  200, 
31163 20 31 32 32 2c 20 20 32 30 31 2c 20 20 31 36 35   122,  201,  165
31164 2c 20 20 31 32 33 2c 20 20 31 35 30 2c 20 20 31  ,  123,  150,  1
31165 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 31  74,  175,. /*  1
31166 34 30 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31  400 */   174,  1
31167 37 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20  75,  165,  150, 
31168 20 31 32 31 2c 20 20 31 36 35 2c 20 20 31 32 39   121,  165,  129
31169 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31  ,  130,  131,  1
3116a 33 32 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a 2f  32,. /*  1410 */
3116b 20 20 20 31 33 33 2c 20 20 31 33 34 2c 20 20 31     133,  134,  1
3116c 36 35 2c 20 20 32 30 32 2c 20 20 31 37 34 2c 20  65,  202,  174, 
3116d 20 31 37 35 2c 20 20 32 30 33 2c 20 20 31 32 34   175,  203,  124
3116e 2c 20 20 31 36 35 2c 20 20 31 33 35 2c 0a 20 2f  ,  165,  135,. /
3116f 2a 20 20 31 34 32 30 20 2a 2f 20 20 20 31 35 37  *  1420 */   157
31170 2c 20 20 31 31 37 2c 20 20 20 36 36 2c 20 20 32  ,  117,   66,  2
31171 32 37 2c 20 20 31 35 37 2c 20 20 32 31 30 2c 20  27,  157,  210, 
31172 20 31 30 34 2c 20 20 32 31 31 2c 20 20 31 32 30   104,  211,  120
31173 2c 20 20 31 30 35 2c 0a 20 2f 2a 20 20 31 34 33  ,  105,. /*  143
31174 30 20 2a 2f 20 20 20 31 30 36 2c 20 20 32 31 30  0 */   106,  210
31175 2c 20 20 31 37 36 2c 20 20 32 31 31 2c 20 20 31  ,  176,  211,  1
31176 31 30 2c 20 20 32 31 30 2c 20 20 31 30 34 2c 20  10,  210,  104, 
31177 20 20 34 37 2c 20 20 32 31 31 2c 20 20 31 31 35    47,  211,  115
31178 2c 0a 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20 20  ,. /*  1440 */  
31179 20 31 37 36 2c 20 20 31 38 34 2c 20 20 20 38 36   176,  184,   86
3117a 2c 20 20 20 38 37 2c 20 20 31 37 36 2c 20 20 31  ,   87,  176,  1
3117b 37 39 2c 20 20 31 30 33 2c 20 20 31 37 38 2c 20  79,  103,  178, 
3117c 20 31 37 36 2c 20 20 20 32 32 2c 0a 20 2f 2a 20   176,   22,. /* 
3117d 20 31 34 35 30 20 2a 2f 20 20 20 20 39 34 2c 20   1450 */    94, 
3117e 20 20 39 32 2c 20 20 31 32 38 2c 20 20 32 33 30    92,  128,  230
3117f 2c 20 20 20 39 38 2c 20 20 31 37 39 2c 20 20 31  ,   98,  179,  1
31180 37 36 2c 20 20 31 37 36 2c 20 20 31 38 34 2c 20  76,  176,  184, 
31181 20 32 33 30 2c 0a 20 2f 2a 20 20 31 34 36 30 20   230,. /*  1460 
31182 2a 2f 20 20 20 20 31 38 2c 20 20 31 37 36 2c 20  */    18,  176, 
31183 20 31 35 36 2c 20 20 31 33 39 2c 20 20 31 35 37   156,  139,  157
31184 2c 20 20 31 35 36 2c 20 20 20 34 35 2c 20 20 31  ,  156,   45,  1
31185 35 37 2c 20 20 31 35 36 2c 20 20 31 33 35 2c 0a  57,  156,  135,.
31186 20 2f 2a 20 20 31 34 37 30 20 2a 2f 20 20 20 31   /*  1470 */   1
31187 35 37 2c 20 20 31 35 37 2c 20 20 32 33 38 2c 20  57,  157,  238, 
31188 20 31 35 36 2c 20 20 20 36 38 2c 20 20 32 33 39   156,   68,  239
31189 2c 20 20 31 35 37 2c 20 20 31 38 39 2c 20 20 31  ,  157,  189,  1
3118a 38 39 2c 20 20 32 31 39 2c 0a 20 2f 2a 20 20 31  89,  219,. /*  1
3118b 34 38 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 31  480 */    22,  1
3118c 39 39 2c 20 20 31 35 37 2c 20 20 31 39 32 2c 20  99,  157,  192, 
3118d 20 20 31 38 2c 20 20 31 32 39 2c 20 20 31 33 30    18,  129,  130
3118e 2c 20 20 31 33 31 2c 20 20 31 39 32 2c 20 20 31  ,  131,  192,  1
3118f 39 32 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a 2f  92,. /*  1490 */
31190 20 20 20 31 39 32 2c 20 20 31 39 39 2c 20 20 31     192,  199,  1
31191 38 39 2c 20 20 32 31 39 2c 20 20 31 35 37 2c 20  89,  219,  157, 
31192 20 32 34 33 2c 20 20 20 34 30 2c 20 20 32 34 33   243,   40,  243
31193 2c 20 20 31 35 37 2c 20 20 31 35 37 2c 0a 20 2f  ,  157,  157,. /
31194 2a 20 20 31 35 30 30 20 2a 2f 20 20 20 32 34 36  *  1500 */   246
31195 2c 20 20 20 33 38 2c 20 20 31 35 33 2c 20 20 31  ,   38,  153,  1
31196 39 36 2c 20 20 31 39 38 2c 20 20 31 36 36 2c 20  96,  198,  166, 
31197 20 32 33 33 2c 20 20 32 33 33 2c 20 20 32 32 38   233,  233,  228
31198 2c 20 20 31 37 37 2c 0a 20 2f 2a 20 20 31 35 31  ,  177,. /*  151
31199 30 20 2a 2f 20 20 20 31 37 37 2c 20 20 32 30 39  0 */   177,  209
3119a 2c 20 20 31 37 37 2c 20 20 31 38 32 2c 20 20 31  ,  177,  182,  1
3119b 37 37 2c 20 20 31 36 36 2c 20 20 31 37 37 2c 20  77,  166,  177, 
3119c 20 31 36 36 2c 20 20 31 37 38 2c 20 20 32 34 32   166,  178,  242
3119d 2c 0a 20 2f 2a 20 20 31 35 32 30 20 2a 2f 20 20  ,. /*  1520 */  
3119e 20 31 39 39 2c 20 20 32 34 32 2c 20 20 32 30 39   199,  242,  209
3119f 2c 20 20 32 30 39 2c 20 20 31 39 39 2c 20 20 31  ,  209,  199,  1
311a0 34 38 2c 20 20 31 39 36 2c 20 20 31 36 36 2c 20  48,  196,  166, 
311a1 20 32 30 38 2c 20 20 31 39 35 2c 0a 20 2f 2a 20   208,  195,. /* 
311a2 20 31 35 33 30 20 2a 2f 20 20 20 31 39 35 2c 20   1530 */   195, 
311a3 20 32 33 36 2c 20 20 32 33 37 2c 20 20 31 39 35   236,  237,  195
311a4 2c 20 20 31 39 31 2c 20 20 31 38 33 2c 20 20 31  ,  191,  183,  1
311a5 38 33 2c 20 20 31 38 36 2c 20 20 31 37 34 2c 20  83,  186,  174, 
311a6 20 31 37 34 2c 0a 20 2f 2a 20 20 31 35 34 30 20   174,. /*  1540 
311a7 2a 2f 20 20 20 31 38 36 2c 20 20 31 31 35 2c 20  */   186,  115, 
311a8 20 20 39 32 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65    92,.};.#define
311a9 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46   YY_SHIFT_USE_DF
311aa 4c 54 20 28 2d 37 30 29 0a 23 64 65 66 69 6e 65  LT (-70).#define
311ab 20 59 59 5f 53 48 49 46 54 5f 43 4f 55 4e 54 20   YY_SHIFT_COUNT 
311ac 28 34 31 37 29 0a 23 64 65 66 69 6e 65 20 59 59  (417).#define YY
311ad 5f 53 48 49 46 54 5f 4d 49 4e 20 20 20 28 2d 36  _SHIFT_MIN   (-6
311ae 39 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 53 48  9).#define YY_SH
311af 49 46 54 5f 4d 41 58 20 20 20 28 31 34 36 36 29  IFT_MAX   (1466)
311b0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 68  .static const sh
311b1 6f 72 74 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  ort yy_shift_ofs
311b2 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20  t[] = {. /*     
311b3 30 20 2a 2f 20 20 31 31 32 30 2c 20 31 31 30 34  0 */  1120, 1104
311b4 2c 20 31 33 32 34 2c 20 31 31 30 34 2c 20 31 31  , 1324, 1104, 11
311b5 39 30 2c 20 31 31 39 30 2c 20 20 20 39 30 2c 20  90, 1190,   90, 
311b6 20 20 39 30 2c 20 20 20 20 31 2c 20 20 2d 31 39    90,    1,  -19
311b7 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20  ,. /*    10 */  
311b8 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
311b9 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 20 32  , 1190, 1190,  2
311ba 38 30 2c 20 20 33 39 31 2c 20 20 37 32 31 2c 20  80,  391,  721, 
311bb 31 30 39 31 2c 20 31 31 39 30 2c 0a 20 2f 2a 20  1091, 1190,. /* 
311bc 20 20 20 32 30 20 2a 2f 20 20 31 31 39 30 2c 20     20 */  1190, 
311bd 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
311be 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31  , 1190, 1190, 11
311bf 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20  90, 1190, 1190, 
311c0 31 31 39 30 2c 0a 20 2f 2a 20 20 20 20 33 30 20  1190,. /*    30 
311c1 2a 2f 20 20 31 31 39 30 2c 20 31 31 39 30 2c 20  */  1190, 1190, 
311c2 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
311c3 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31  , 1190, 1190, 11
311c4 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 0a  90, 1190, 1190,.
311c5 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 31 31   /*    40 */  11
311c6 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20  90, 1190, 1190, 
311c7 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
311c8 2c 20 31 31 39 30 2c 20 31 32 37 37 2c 20 31 31  , 1190, 1277, 11
311c9 39 30 2c 20 31 31 39 30 2c 0a 20 2f 2a 20 20 20  90, 1190,. /*   
311ca 20 35 30 20 2a 2f 20 20 31 31 39 30 2c 20 31 31   50 */  1190, 11
311cb 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20  90, 1190, 1190, 
311cc 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
311cd 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31  , 1190, 1190, 11
311ce 39 30 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f  90,. /*    60 */
311cf 20 20 31 31 39 30 2c 20 20 2d 34 39 2c 20 20 32    1190,  -49,  2
311d0 38 37 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  87,  391,  391, 
311d1 20 39 38 38 2c 20 20 39 38 38 2c 20 20 32 31 35   988,  988,  215
311d2 2c 20 31 34 32 36 2c 20 20 20 35 35 2c 0a 20 2f  , 1426,   55,. /
311d3 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 36 34 37  *    70 */   647
311d4 2c 20 20 35 37 33 2c 20 20 34 39 39 2c 20 20 34  ,  573,  499,  4
311d5 32 35 2c 20 20 33 35 31 2c 20 20 32 37 37 2c 20  25,  351,  277, 
311d6 20 32 30 33 2c 20 20 31 32 39 2c 20 20 37 39 35   203,  129,  795
311d7 2c 20 20 37 39 35 2c 0a 20 2f 2a 20 20 20 20 38  ,  795,. /*    8
311d8 30 20 2a 2f 20 20 20 37 39 35 2c 20 20 37 39 35  0 */   795,  795
311d9 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37  ,  795,  795,  7
311da 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20  95,  795,  795, 
311db 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35   795,  795,  795
311dc 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20  ,. /*    90 */  
311dd 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35   795,  795,  795
311de 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37  ,  795,  795,  7
311df 39 35 2c 20 20 38 36 39 2c 20 20 37 39 35 2c 20  95,  869,  795, 
311e0 20 39 34 33 2c 20 31 30 31 37 2c 0a 20 2f 2a 20   943, 1017,. /* 
311e1 20 20 31 30 30 20 2a 2f 20 20 31 30 31 37 2c 20    100 */  1017, 
311e2 20 2d 36 39 2c 20 20 2d 36 39 2c 20 20 2d 36 39   -69,  -69,  -69
311e3 2c 20 20 2d 36 39 2c 20 20 20 2d 31 2c 20 20 20  ,  -69,   -1,   
311e4 2d 31 2c 20 20 20 35 38 2c 20 20 31 33 38 2c 20  -1,   58,  138, 
311e5 20 2d 34 34 2c 0a 20 2f 2a 20 20 20 31 31 30 20   -44,. /*   110 
311e6 2a 2f 20 20 20 33 39 31 2c 20 20 33 39 31 2c 20  */   391,  391, 
311e7 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
311e8 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33  ,  391,  391,  3
311e9 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 0a  91,  391,  391,.
311ea 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 33   /*   120 */   3
311eb 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
311ec 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
311ed 2c 20 20 34 36 33 2c 20 20 35 30 36 2c 20 20 33  ,  463,  506,  3
311ee 39 31 2c 20 20 33 39 31 2c 0a 20 2f 2a 20 20 20  91,  391,. /*   
311ef 31 33 30 20 2a 2f 20 20 20 33 39 31 2c 20 20 33  130 */   391,  3
311f0 39 31 2c 20 20 33 39 31 2c 20 20 32 31 35 2c 20  91,  391,  215, 
311f1 20 39 35 39 2c 20 31 34 35 30 2c 20 20 2d 37 30   959, 1450,  -70
311f2 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 31 33  ,  -70,  -70, 13
311f3 35 36 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f  56,. /*   140 */
311f4 20 20 20 20 37 33 2c 20 20 34 33 33 2c 20 20 34      73,  433,  4
311f5 33 33 2c 20 20 33 36 31 2c 20 20 33 30 39 2c 20  33,  361,  309, 
311f6 20 31 36 35 2c 20 20 20 36 37 2c 20 20 32 38 34   165,   67,  284
311f7 2c 20 20 34 36 36 2c 20 20 32 39 31 2c 0a 20 2f  ,  466,  291,. /
311f8 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 33 39 31  *   150 */   391
311f9 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33  ,  391,  391,  3
311fa 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
311fb 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
311fc 2c 20 20 33 39 31 2c 0a 20 2f 2a 20 20 20 31 36  ,  391,. /*   16
311fd 30 20 2a 2f 20 20 20 33 39 31 2c 20 20 33 39 31  0 */   391,  391
311fe 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33  ,  391,  391,  3
311ff 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
31200 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
31201 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20  ,. /*   170 */  
31202 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
31203 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33  ,  391,  391,  3
31204 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
31205 20 33 39 31 2c 20 20 33 39 31 2c 0a 20 2f 2a 20   391,  391,. /* 
31206 20 20 31 38 30 20 2a 2f 20 20 20 33 39 31 2c 20    180 */   391, 
31207 20 33 39 31 2c 20 20 35 30 31 2c 20 20 32 32 31   391,  501,  221
31208 2c 20 20 32 32 31 2c 20 20 32 32 31 2c 20 20 37  ,  221,  221,  7
31209 30 35 2c 20 20 37 39 37 2c 20 31 34 32 36 2c 20  05,  797, 1426, 
3120a 31 34 32 36 2c 0a 20 2f 2a 20 20 20 31 39 30 20  1426,. /*   190 
3120b 2a 2f 20 20 31 34 32 36 2c 20 20 2d 37 30 2c 20  */  1426,  -70, 
3120c 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20 31 33 39   -70,  -70,  139
3120d 2c 20 20 31 37 31 2c 20 20 31 37 31 2c 20 20 20  ,  171,  171,   
3120e 31 32 2c 20 20 35 36 38 2c 20 20 35 36 38 2c 0a  12,  568,  568,.
3120f 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 32   /*   200 */   2
31210 30 39 2c 20 20 34 32 37 2c 20 20 33 37 30 2c 20  09,  427,  370, 
31211 20 33 36 37 2c 20 20 33 35 32 2c 20 20 32 39 36   367,  352,  296
31212 2c 20 20 20 33 38 2c 20 20 20 33 38 2c 20 20 20  ,   38,   38,   
31213 33 38 2c 20 20 20 33 38 2c 0a 20 2f 2a 20 20 20  38,   38,. /*   
31214 32 31 30 20 2a 2f 20 20 20 33 34 38 2c 20 20 35  210 */   348,  5
31215 36 39 2c 20 20 20 33 38 2c 20 20 20 33 38 2c 20  69,   38,   38, 
31216 20 20 37 34 2c 20 20 35 38 37 2c 20 20 35 39 32    74,  587,  592
31217 2c 20 20 20 31 37 2c 20 20 34 39 35 2c 20 20 20  ,   17,  495,   
31218 38 37 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f  87,. /*   220 */
31219 20 20 20 20 38 37 2c 20 20 33 37 32 2c 20 20 34      87,  372,  4
3121a 39 35 2c 20 20 33 37 32 2c 20 20 37 35 35 2c 20  95,  372,  755, 
3121b 20 32 31 35 2c 20 20 32 39 33 2c 20 20 32 31 35   215,  293,  215
3121c 2c 20 20 32 39 33 2c 20 20 31 34 30 2c 0a 20 2f  ,  293,  140,. /
3121d 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 32 39 33  *   230 */   293
3121e 2c 20 20 20 38 37 2c 20 20 32 39 33 2c 20 20 32  ,   87,  293,  2
3121f 39 33 2c 20 20 37 36 32 2c 20 20 36 33 38 2c 20  93,  762,  638, 
31220 20 36 33 38 2c 20 20 32 31 35 2c 20 20 20 37 38   638,  215,   78
31221 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20 32 34  ,   51,. /*   24
31222 30 20 2a 2f 20 20 20 32 34 36 2c 20 31 34 36 33  0 */   246, 1463
31223 2c 20 31 33 30 34 2c 20 31 33 30 34 2c 20 31 34  , 1304, 1304, 14
31224 35 36 2c 20 31 34 35 36 2c 20 31 33 30 34 2c 20  56, 1456, 1304, 
31225 31 34 35 38 2c 20 31 34 30 36 2c 20 31 32 36 31  1458, 1406, 1261
31226 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20  ,. /*   250 */  
31227 31 34 36 36 2c 20 31 34 36 36 2c 20 31 34 36 36  1466, 1466, 1466
31228 2c 20 31 34 36 36 2c 20 31 33 30 34 2c 20 31 32  , 1466, 1304, 12
31229 36 31 2c 20 31 34 35 38 2c 20 31 34 30 36 2c 20  61, 1458, 1406, 
3122a 31 34 30 36 2c 20 31 33 30 34 2c 0a 20 2f 2a 20  1406, 1304,. /* 
3122b 20 20 32 36 30 20 2a 2f 20 20 31 34 34 32 2c 20    260 */  1442, 
3122c 31 33 33 34 2c 20 31 34 32 31 2c 20 31 33 30 34  1334, 1421, 1304
3122d 2c 20 31 33 30 34 2c 20 31 34 34 32 2c 20 31 33  , 1304, 1442, 13
3122e 30 34 2c 20 31 34 34 32 2c 20 31 33 30 34 2c 20  04, 1442, 1304, 
3122f 31 34 34 32 2c 0a 20 2f 2a 20 20 20 32 37 30 20  1442,. /*   270 
31230 2a 2f 20 20 31 34 32 37 2c 20 31 33 33 32 2c 20  */  1427, 1332, 
31231 31 33 33 32 2c 20 31 33 33 32 2c 20 31 33 39 30  1332, 1332, 1390
31232 2c 20 31 33 35 39 2c 20 31 33 35 39 2c 20 31 34  , 1359, 1359, 14
31233 32 37 2c 20 31 33 33 32 2c 20 31 33 34 33 2c 0a  27, 1332, 1343,.
31234 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 31 33   /*   280 */  13
31235 33 32 2c 20 31 33 39 30 2c 20 31 33 33 32 2c 20  32, 1390, 1332, 
31236 31 33 33 32 2c 20 31 33 30 38 2c 20 31 33 32 32  1332, 1308, 1322
31237 2c 20 31 33 30 38 2c 20 31 33 32 32 2c 20 31 33  , 1308, 1322, 13
31238 30 38 2c 20 31 33 32 32 2c 0a 20 2f 2a 20 20 20  08, 1322,. /*   
31239 32 39 30 20 2a 2f 20 20 31 33 30 34 2c 20 31 33  290 */  1304, 13
3123a 30 34 2c 20 31 32 38 34 2c 20 31 32 39 33 2c 20  04, 1284, 1293, 
3123b 31 32 38 33 2c 20 31 32 37 33 2c 20 31 32 37 31  1283, 1273, 1271
3123c 2c 20 31 32 36 31 2c 20 31 32 35 34 2c 20 31 33  , 1261, 1254, 13
3123d 34 32 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f  42,. /*   300 */
3123e 20 20 31 33 33 36 2c 20 31 33 33 36 2c 20 31 33    1336, 1336, 13
3123f 30 33 2c 20 31 33 30 33 2c 20 31 33 30 33 2c 20  03, 1303, 1303, 
31240 31 33 30 33 2c 20 20 2d 37 30 2c 20 20 2d 37 30  1303,  -70,  -70
31241 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c 0a 20 2f  ,  -70,  -70,. /
31242 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 2d 37 30  *   310 */   -70
31243 2c 20 20 2d 37 30 2c 20 20 33 33 34 2c 20 20 31  ,  -70,  334,  1
31244 32 30 2c 20 20 35 33 35 2c 20 20 32 33 32 2c 20  20,  535,  232, 
31245 20 36 32 34 2c 20 20 39 34 34 2c 20 20 31 38 38   624,  944,  188
31246 2c 20 20 39 30 35 2c 0a 20 2f 2a 20 20 20 33 32  ,  905,. /*   32
31247 30 20 2a 2f 20 20 20 38 39 37 2c 20 20 38 37 33  0 */   897,  873
31248 2c 20 20 38 37 30 2c 20 20 38 32 35 2c 20 20 37  ,  870,  825,  7
31249 35 34 2c 20 20 37 31 39 2c 20 20 36 35 31 2c 20  54,  719,  651, 
3124a 20 35 32 37 2c 20 20 34 34 34 2c 20 20 31 32 35   527,  444,  125
3124b 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20  ,. /*   330 */  
3124c 20 35 31 34 2c 20 20 34 33 34 2c 20 31 32 39 34   514,  434, 1294
3124d 2c 20 31 32 37 39 2c 20 31 32 35 39 2c 20 31 31  , 1279, 1259, 11
3124e 34 39 2c 20 31 32 35 38 2c 20 31 31 38 38 2c 20  49, 1258, 1188, 
3124f 31 32 33 35 2c 20 31 32 33 38 2c 0a 20 2f 2a 20  1235, 1238,. /* 
31250 20 20 33 34 30 20 2a 2f 20 20 31 32 33 31 2c 20    340 */  1231, 
31251 31 31 30 35 2c 20 31 32 32 38 2c 20 31 31 32 33  1105, 1228, 1123
31252 2c 20 31 31 33 34 2c 20 31 31 30 30 2c 20 31 32  , 1134, 1100, 12
31253 31 30 2c 20 31 31 30 36 2c 20 31 31 39 38 2c 20  10, 1106, 1198, 
31254 31 32 30 35 2c 0a 20 2f 2a 20 20 20 33 35 30 20  1205,. /*   350 
31255 2a 2f 20 20 31 30 38 33 2c 20 31 31 37 34 2c 20  */  1083, 1174, 
31256 31 30 35 37 2c 20 31 31 32 34 2c 20 31 31 31 38  1057, 1124, 1118
31257 2c 20 31 30 37 35 2c 20 31 31 33 38 2c 20 31 31  , 1075, 1138, 11
31258 33 39 2c 20 31 30 35 32 2c 20 31 31 36 33 2c 0a  39, 1052, 1163,.
31259 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 31 31   /*   360 */  11
3125a 35 38 2c 20 31 31 33 33 2c 20 31 30 35 30 2c 20  58, 1133, 1050, 
3125b 20 39 37 38 2c 20 31 30 39 39 2c 20 31 31 30 37   978, 1099, 1107
3125c 2c 20 31 30 38 39 2c 20 31 30 32 30 2c 20 31 30  , 1089, 1020, 10
3125d 33 34 2c 20 20 39 36 38 2c 0a 20 2f 2a 20 20 20  34,  968,. /*   
3125e 33 37 30 20 2a 2f 20 20 31 30 33 39 2c 20 31 30  370 */  1039, 10
3125f 34 38 2c 20 20 39 39 31 2c 20 20 38 39 39 2c 20  48,  991,  899, 
31260 20 39 35 38 2c 20 20 39 38 31 2c 20 20 39 34 32   958,  981,  942
31261 2c 20 20 39 37 36 2c 20 20 39 37 34 2c 20 20 39  ,  976,  974,  9
31262 36 36 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f  66,. /*   380 */
31263 20 20 20 39 35 37 2c 20 20 39 32 31 2c 20 20 39     957,  921,  9
31264 30 30 2c 20 20 38 33 33 2c 20 20 38 36 33 2c 20  00,  833,  863, 
31265 20 38 36 37 2c 20 20 38 33 39 2c 20 20 38 33 36   867,  839,  836
31266 2c 20 20 37 33 35 2c 20 20 38 32 32 2c 0a 20 2f  ,  735,  822,. /
31267 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 38 30 33  *   390 */   803
31268 2c 20 20 38 30 36 2c 20 20 37 30 36 2c 20 20 36  ,  806,  706,  6
31269 38 34 2c 20 20 37 32 33 2c 20 20 37 33 30 2c 20  84,  723,  730, 
3126a 20 36 35 38 2c 20 20 36 38 34 2c 20 20 37 32 38   658,  684,  728
3126b 2c 20 20 36 39 30 2c 0a 20 2f 2a 20 20 20 34 30  ,  690,. /*   40
3126c 30 20 2a 2f 20 20 20 36 37 38 2c 20 20 36 36 30  0 */   678,  660
3126d 2c 20 20 36 35 35 2c 20 20 36 37 39 2c 20 20 36  ,  655,  679,  6
3126e 35 30 2c 20 20 36 31 36 2c 20 20 36 30 36 2c 20  50,  616,  606, 
3126f 20 35 37 31 2c 20 20 34 36 38 2c 20 20 33 38 39   571,  468,  389
31270 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20  ,. /*   410 */  
31271 20 33 34 33 2c 20 20 32 39 34 2c 20 20 31 38 36   343,  294,  186
31272 2c 20 20 20 20 33 2c 20 20 20 34 30 2c 20 20 20  ,    3,   40,   
31273 20 36 2c 20 20 20 2d 33 2c 20 20 20 20 35 2c 0a   6,   -3,    5,.
31274 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45  };.#define YY_RE
31275 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 28 2d  DUCE_USE_DFLT (-
31276 32 32 32 29 0a 23 64 65 66 69 6e 65 20 59 59 5f  222).#define YY_
31277 52 45 44 55 43 45 5f 43 4f 55 4e 54 20 28 33 31  REDUCE_COUNT (31
31278 31 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45  1).#define YY_RE
31279 44 55 43 45 5f 4d 49 4e 20 20 20 28 2d 32 32 31  DUCE_MIN   (-221
3127a 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44  ).#define YY_RED
3127b 55 43 45 5f 4d 41 58 20 20 20 28 31 33 37 37 29  UCE_MAX   (1377)
3127c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 68  .static const sh
3127d 6f 72 74 20 79 79 5f 72 65 64 75 63 65 5f 6f 66  ort yy_reduce_of
3127e 73 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20  st[] = {. /*    
3127f 20 30 20 2a 2f 20 20 20 34 39 33 2c 20 31 30 39   0 */   493, 109
31280 32 2c 20 31 30 32 31 2c 20 20 31 34 37 2c 20 20  2, 1021,  147,  
31281 31 35 38 2c 20 20 31 35 35 2c 20 20 20 38 36 2c  158,  155,   86,
31282 20 20 20 31 38 2c 20 20 20 38 31 2c 20 20 31 37     18,   81,  17
31283 32 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20  2,. /*    10 */ 
31284 20 20 33 38 35 2c 20 20 33 37 37 2c 20 20 33 30    385,  377,  30
31285 38 2c 20 20 33 37 39 2c 20 20 32 39 37 2c 20 20  8,  379,  297,  
31286 32 35 32 2c 20 20 2d 34 33 2c 20 2d 31 34 36 2c  252,  -43, -146,
31287 20 31 32 34 30 2c 20 31 32 32 36 2c 0a 20 2f 2a   1240, 1226,. /*
31288 20 20 20 20 32 30 20 2a 2f 20 20 31 32 32 34 2c      20 */  1224,
31289 20 31 31 34 31 2c 20 31 31 33 37 2c 20 31 30 39   1141, 1137, 109
3128a 37 2c 20 31 30 39 35 2c 20 31 30 39 30 2c 20 31  7, 1095, 1090, 1
3128b 30 33 30 2c 20 20 39 37 35 2c 20 20 39 36 34 2c  030,  975,  964,
3128c 20 20 39 36 32 2c 0a 20 2f 2a 20 20 20 20 33 30    962,. /*    30
3128d 20 2a 2f 20 20 20 39 30 38 2c 20 20 38 39 30 2c   */   908,  890,
3128e 20 20 38 38 37 2c 20 20 38 37 34 2c 20 20 38 33    887,  874,  83
3128f 34 2c 20 20 38 31 36 2c 20 20 38 31 33 2c 20 20  4,  816,  813,  
31290 38 30 30 2c 20 20 37 36 30 2c 20 20 37 35 38 2c  800,  760,  758,
31291 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20  . /*    40 */   
31292 37 34 32 2c 20 20 37 33 39 2c 20 20 37 32 36 2c  742,  739,  726,
31293 20 20 36 38 36 2c 20 20 36 37 32 2c 20 20 36 36    686,  672,  66
31294 38 2c 20 20 36 36 35 2c 20 20 36 35 32 2c 20 20  8,  665,  652,  
31295 36 31 31 2c 20 20 36 30 39 2c 0a 20 2f 2a 20 20  611,  609,. /*  
31296 20 20 35 30 20 2a 2f 20 20 20 36 30 37 2c 20 20    50 */   607,  
31297 35 39 31 2c 20 20 35 37 38 2c 20 20 35 33 37 2c  591,  578,  537,
31298 20 20 35 32 36 2c 20 20 35 32 34 2c 20 20 35 32    526,  524,  52
31299 30 2c 20 20 35 30 34 2c 20 20 34 35 36 2c 20 20  0,  504,  456,  
3129a 34 35 30 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a  450,. /*    60 *
3129b 2f 20 20 20 33 37 31 2c 20 2d 32 32 31 2c 20 20  /   371, -221,  
3129c 34 37 34 2c 20 20 33 36 39 2c 20 20 35 31 37 2c  474,  369,  517,
3129d 20 20 33 39 35 2c 20 20 20 39 32 2c 20 20 33 30    395,   92,  30
3129e 31 2c 20 20 34 30 31 2c 20 20 31 31 38 2c 0a 20  1,  401,  118,. 
3129f 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 31 31  /*    70 */   11
312a0 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
312a1 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c  118,  118,  118,
312a2 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
312a3 38 2c 20 20 31 31 38 2c 0a 20 2f 2a 20 20 20 20  8,  118,. /*    
312a4 38 30 20 2a 2f 20 20 20 31 31 38 2c 20 20 31 31  80 */   118,  11
312a5 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
312a6 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c  118,  118,  118,
312a7 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
312a8 38 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20  8,. /*    90 */ 
312a9 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
312aa 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
312ab 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c  118,  118,  118,
312ac 20 20 31 31 38 2c 20 20 31 31 38 2c 0a 20 2f 2a    118,  118,. /*
312ad 20 20 20 31 30 30 20 2a 2f 20 20 20 31 31 38 2c     100 */   118,
312ae 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
312af 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
312b0 31 31 38 2c 20 20 32 30 38 2c 20 20 31 31 38 2c  118,  208,  118,
312b1 20 20 31 31 38 2c 0a 20 2f 2a 20 20 20 31 31 30    118,. /*   110
312b2 20 2a 2f 20 20 31 30 33 38 2c 20 20 39 39 34 2c   */  1038,  994,
312b3 20 20 39 38 33 2c 20 20 39 36 39 2c 20 20 38 39    983,  969,  89
312b4 32 2c 20 20 38 39 31 2c 20 20 38 38 38 2c 20 20  2,  891,  888,  
312b5 38 31 38 2c 20 20 37 36 31 2c 20 20 38 33 32 2c  818,  761,  832,
312b6 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20  . /*   120 */   
312b7 36 31 33 2c 20 20 36 30 34 2c 20 20 35 32 33 2c  613,  604,  523,
312b8 20 20 37 34 34 2c 20 20 38 33 30 2c 20 20 37 37    744,  830,  77
312b9 31 2c 20 20 35 39 35 2c 20 20 34 33 30 2c 20 20  1,  595,  430,  
312ba 37 34 30 2c 20 20 36 37 30 2c 0a 20 2f 2a 20 20  740,  670,. /*  
312bb 20 31 33 30 20 2a 2f 20 20 20 36 36 39 2c 20 20   130 */   669,  
312bc 36 36 36 2c 20 20 36 31 30 2c 20 20 35 37 39 2c  666,  610,  579,
312bd 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
312be 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
312bf 36 36 37 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a  667,. /*   140 *
312c0 2f 20 20 20 39 37 32 2c 20 20 38 38 30 2c 20 20  /   972,  880,  
312c1 37 38 36 2c 20 20 39 39 36 2c 20 31 32 35 33 2c  786,  996, 1253,
312c2 20 31 32 34 37 2c 20 31 32 33 37 2c 20 31 30 30   1247, 1237, 100
312c3 31 2c 20 20 37 35 30 2c 20 20 37 35 30 2c 0a 20  1,  750,  750,. 
312c4 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 31 32 33  /*   150 */  123
312c5 30 2c 20 31 32 31 32 2c 20 31 32 30 38 2c 20 31  0, 1212, 1208, 1
312c6 32 30 33 2c 20 31 32 30 30 2c 20 31 31 39 34 2c  203, 1200, 1194,
312c7 20 31 31 38 39 2c 20 31 31 38 32 2c 20 31 31 38   1189, 1182, 118
312c8 31 2c 20 31 31 38 30 2c 0a 20 2f 2a 20 20 20 31  1, 1180,. /*   1
312c9 36 30 20 2a 2f 20 20 31 31 37 37 2c 20 31 31 36  60 */  1177, 116
312ca 30 2c 20 31 31 34 32 2c 20 31 31 33 30 2c 20 31  0, 1142, 1130, 1
312cb 31 32 39 2c 20 31 31 32 38 2c 20 31 31 32 35 2c  129, 1128, 1125,
312cc 20 31 31 30 39 2c 20 31 30 39 38 2c 20 31 30 39   1109, 1098, 109
312cd 33 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20  3,. /*   170 */ 
312ce 20 31 30 37 39 2c 20 31 30 36 33 2c 20 31 30 34   1079, 1063, 104
312cf 33 2c 20 31 30 34 32 2c 20 31 30 32 32 2c 20 31  3, 1042, 1022, 1
312d0 30 30 36 2c 20 20 39 39 36 2c 20 20 39 39 33 2c  006,  996,  993,
312d1 20 20 39 37 30 2c 20 20 39 30 34 2c 0a 20 2f 2a    970,  904,. /*
312d2 20 20 20 31 38 30 20 2a 2f 20 20 20 33 38 32 2c     180 */   382,
312d3 20 20 33 37 38 2c 20 20 38 38 36 2c 20 20 39 31    378,  886,  91
312d4 30 2c 20 20 38 39 33 2c 20 20 38 38 31 2c 20 20  0,  893,  881,  
312d5 38 34 31 2c 20 20 38 38 34 2c 20 20 38 31 32 2c  841,  884,  812,
312d6 20 20 38 31 31 2c 0a 20 2f 2a 20 20 20 31 39 30    811,. /*   190
312d7 20 2a 2f 20 20 20 38 31 30 2c 20 20 35 33 39 2c   */   810,  539,
312d8 20 20 36 39 36 2c 20 20 33 35 38 2c 20 31 33 35    696,  358, 135
312d9 34 2c 20 31 33 36 35 2c 20 31 33 36 34 2c 20 31  4, 1365, 1364, 1
312da 33 35 31 2c 20 31 33 35 33 2c 20 31 33 35 32 2c  351, 1353, 1352,
312db 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 31  . /*   200 */  1
312dc 33 32 30 2c 20 31 33 33 38 2c 20 31 33 34 33 2c  320, 1338, 1343,
312dd 20 31 33 33 38 2c 20 31 33 33 38 2c 20 31 33 33   1338, 1338, 133
312de 38 2c 20 31 33 33 38 2c 20 31 33 33 38 2c 20 31  8, 1338, 1338, 1
312df 33 33 38 2c 20 31 33 33 38 2c 0a 20 2f 2a 20 20  338, 1338,. /*  
312e0 20 32 31 30 20 2a 2f 20 20 31 32 39 35 2c 20 31   210 */  1295, 1
312e1 32 39 35 2c 20 31 33 33 35 2c 20 31 33 33 34 2c  295, 1335, 1334,
312e2 20 31 33 32 30 2c 20 31 33 36 31 2c 20 31 33 33   1320, 1361, 133
312e3 30 2c 20 31 33 37 37 2c 20 31 33 32 35 2c 20 31  0, 1377, 1325, 1
312e4 33 31 34 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a  314,. /*   220 *
312e5 2f 20 20 31 33 31 33 2c 20 31 32 37 39 2c 20 31  /  1313, 1279, 1
312e6 33 32 31 2c 20 31 32 37 37 2c 20 31 33 34 30 2c  321, 1277, 1340,
312e7 20 31 33 35 31 2c 20 31 33 33 39 2c 20 31 33 34   1351, 1339, 134
312e8 39 2c 20 31 33 33 37 2c 20 31 33 33 31 2c 0a 20  9, 1337, 1331,. 
312e9 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 31 33 33  /*   230 */  133
312ea 35 2c 20 31 33 30 32 2c 20 31 33 33 33 2c 20 31  5, 1302, 1333, 1
312eb 33 33 32 2c 20 31 32 38 30 2c 20 31 32 37 34 2c  332, 1280, 1274,
312ec 20 31 32 37 33 2c 20 31 33 33 39 2c 20 31 33 30   1273, 1339, 130
312ed 36 2c 20 31 33 30 37 2c 0a 20 2f 2a 20 20 20 32  6, 1307,. /*   2
312ee 34 30 20 2a 2f 20 20 31 33 34 39 2c 20 31 32 35  40 */  1349, 125
312ef 34 2c 20 31 33 34 32 2c 20 31 33 34 31 2c 20 31  4, 1342, 1341, 1
312f0 32 35 34 2c 20 31 32 35 32 2c 20 31 33 33 37 2c  254, 1252, 1337,
312f1 20 31 32 37 34 2c 20 31 33 30 33 2c 20 31 32 39   1274, 1303, 129
312f2 32 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20  2,. /*   250 */ 
312f3 20 31 32 39 38 2c 20 31 32 39 37 2c 20 31 32 39   1298, 1297, 129
312f4 36 2c 20 31 32 39 31 2c 20 31 33 32 35 2c 20 31  6, 1291, 1325, 1
312f5 32 38 32 2c 20 31 32 36 30 2c 20 31 32 38 39 2c  282, 1260, 1289,
312f6 20 31 32 38 38 2c 20 31 33 31 39 2c 0a 20 2f 2a   1288, 1319,. /*
312f7 20 20 20 32 36 30 20 2a 2f 20 20 31 33 31 37 2c     260 */  1317,
312f8 20 31 32 33 36 2c 20 31 32 33 34 2c 20 31 33 31   1236, 1234, 131
312f9 34 2c 20 31 33 31 33 2c 20 31 33 31 32 2c 20 31  4, 1313, 1312, 1
312fa 33 31 30 2c 20 31 33 30 39 2c 20 31 33 30 37 2c  310, 1309, 1307,
312fb 20 31 33 30 36 2c 0a 20 2f 2a 20 20 20 32 37 30   1306,. /*   270
312fc 20 2a 2f 20 20 31 32 37 36 2c 20 31 32 38 35 2c   */  1276, 1285,
312fd 20 31 32 38 31 2c 20 31 32 38 30 2c 20 31 32 37   1281, 1280, 127
312fe 34 2c 20 31 32 32 39 2c 20 31 32 32 33 2c 20 31  4, 1229, 1223, 1
312ff 32 36 36 2c 20 31 32 37 32 2c 20 31 32 36 39 2c  266, 1272, 1269,
31300 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 31  . /*   280 */  1
31301 32 36 38 2c 20 31 32 35 37 2c 20 31 32 36 34 2c  268, 1257, 1264,
31302 20 31 32 35 36 2c 20 31 32 32 37 2c 20 31 32 32   1256, 1227, 122
31303 35 2c 20 31 32 32 32 2c 20 31 32 32 31 2c 20 31  5, 1222, 1221, 1
31304 32 31 36 2c 20 31 32 31 35 2c 0a 20 2f 2a 20 20  216, 1215,. /*  
31305 20 32 39 30 20 2a 2f 20 20 31 32 36 37 2c 20 31   290 */  1267, 1
31306 32 36 33 2c 20 31 31 39 36 2c 20 31 32 31 33 2c  263, 1196, 1213,
31307 20 31 32 31 31 2c 20 31 31 39 33 2c 20 31 31 39   1211, 1193, 119
31308 32 2c 20 31 31 38 35 2c 20 31 31 36 37 2c 20 31  2, 1185, 1167, 1
31309 31 39 37 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a  197,. /*   300 *
3130a 2f 20 20 31 32 30 39 2c 20 31 32 30 31 2c 20 31  /  1209, 1201, 1
3130b 32 30 32 2c 20 31 31 39 39 2c 20 31 31 39 31 2c  202, 1199, 1191,
3130c 20 31 31 37 39 2c 20 31 30 36 36 2c 20 31 30 36   1179, 1066, 106
3130d 35 2c 20 31 30 35 36 2c 20 31 31 34 36 2c 0a 20  5, 1056, 1146,. 
3130e 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 31 31 32  /*   310 */  112
3130f 31 2c 20 31 31 33 36 2c 0a 7d 3b 0a 73 74 61 74  1, 1136,.};.stat
31310 69 63 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f  ic const YYACTIO
31311 4e 54 59 50 45 20 79 79 5f 64 65 66 61 75 6c 74  NTYPE yy_default
31312 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30  [] = {. /*     0
31313 20 2a 2f 20 20 20 36 33 34 2c 20 20 38 36 39 2c   */   634,  869,
31314 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31315 38 2c 20 20 38 36 39 2c 20 20 38 39 38 2c 20 20  8,  869,  898,  
31316 38 39 38 2c 20 20 39 35 38 2c 20 20 37 35 37 2c  898,  958,  757,
31317 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20  . /*    10 */   
31318 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31319 20 20 39 35 38 2c 20 20 38 36 37 2c 20 20 39 35    958,  867,  95
3131a 38 2c 20 20 39 35 38 2c 20 20 39 33 32 2c 20 20  8,  958,  932,  
3131b 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20  958,  958,. /*  
3131c 20 20 32 30 20 2a 2f 20 20 20 39 35 38 2c 20 20    20 */   958,  
3131d 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3131e 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3131f 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31320 39 35 38 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a  958,. /*    30 *
31321 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
31322 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31323 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31324 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  8,  958,  958,. 
31325 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 39 35  /*    40 */   95
31326 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31327 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31328 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31329 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20  8,  958,. /*    
3132a 35 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35  50 */   958,  95
3132b 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3132c 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3132d 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3132e 38 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20  8,. /*    60 */ 
3132f 20 20 39 35 38 2c 20 20 38 34 31 2c 20 20 39 35    958,  841,  95
31330 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31331 38 39 38 2c 20 20 38 39 38 2c 20 20 36 37 33 2c  898,  898,  673,
31332 20 20 37 36 31 2c 20 20 37 39 32 2c 0a 20 2f 2a    761,  792,. /*
31333 20 20 20 20 37 30 20 2a 2f 20 20 20 39 35 38 2c      70 */   958,
31334 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31335 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31336 39 35 38 2c 20 20 39 35 38 2c 20 20 39 33 31 2c  958,  958,  931,
31337 20 20 39 33 33 2c 0a 20 2f 2a 20 20 20 20 38 30    933,. /*    80
31338 20 2a 2f 20 20 20 38 30 37 2c 20 20 38 30 36 2c   */   807,  806,
31339 20 20 38 30 30 2c 20 20 37 39 39 2c 20 20 39 31    800,  799,  91
3133a 31 2c 20 20 37 37 32 2c 20 20 37 39 37 2c 20 20  1,  772,  797,  
3133b 37 39 30 2c 20 20 37 38 33 2c 20 20 37 39 34 2c  790,  783,  794,
3133c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20  . /*    90 */   
3133d 38 37 30 2c 20 20 38 36 33 2c 20 20 38 36 34 2c  870,  863,  864,
3133e 20 20 38 36 32 2c 20 20 38 36 36 2c 20 20 38 37    862,  866,  87
3133f 31 2c 20 20 39 35 38 2c 20 20 37 39 33 2c 20 20  1,  958,  793,  
31340 38 32 39 2c 20 20 38 34 37 2c 0a 20 2f 2a 20 20  829,  847,. /*  
31341 20 31 30 30 20 2a 2f 20 20 20 38 32 38 2c 20 20   100 */   828,  
31342 38 34 36 2c 20 20 38 35 33 2c 20 20 38 34 35 2c  846,  853,  845,
31343 20 20 38 33 31 2c 20 20 38 34 30 2c 20 20 38 33    831,  840,  83
31344 30 2c 20 20 36 36 35 2c 20 20 38 33 32 2c 20 20  0,  665,  832,  
31345 38 33 33 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a  833,. /*   110 *
31346 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
31347 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31348 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31349 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  8,  958,  958,. 
3134a 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 39 35  /*   120 */   95
3134b 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3134c 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3134d 20 20 36 36 30 2c 20 20 37 32 36 2c 20 20 39 35    660,  726,  95
3134e 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31  8,  958,. /*   1
3134f 33 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35  30 */   958,  95
31350 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31351 38 33 34 2c 20 20 38 33 35 2c 20 20 38 35 30 2c  834,  835,  850,
31352 20 20 38 34 39 2c 20 20 38 34 38 2c 20 20 39 35    849,  848,  95
31353 38 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20  8,. /*   140 */ 
31354 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31355 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31356 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31357 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a    958,  958,. /*
31358 20 20 20 31 35 30 20 2a 2f 20 20 20 39 35 38 2c     150 */   958,
31359 20 20 39 33 38 2c 20 20 39 33 36 2c 20 20 39 35    938,  936,  95
3135a 38 2c 20 20 38 38 32 2c 20 20 39 35 38 2c 20 20  8,  882,  958,  
3135b 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3135c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 36 30    958,. /*   160
3135d 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
3135e 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3135f 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31360 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31361 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20  . /*   170 */   
31362 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31363 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31364 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31365 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20  958,  958,. /*  
31366 20 31 38 30 20 2a 2f 20 20 20 39 35 38 2c 20 20   180 */   958,  
31367 36 34 30 2c 20 20 39 35 38 2c 20 20 37 35 37 2c  640,  958,  757,
31368 20 20 37 35 37 2c 20 20 37 35 37 2c 20 20 36 33    757,  757,  63
31369 34 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  4,  958,  958,  
3136a 39 35 38 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a  958,. /*   190 *
3136b 2f 20 20 20 39 35 38 2c 20 20 39 35 30 2c 20 20  /   958,  950,  
3136c 37 36 31 2c 20 20 37 35 31 2c 20 20 37 31 37 2c  761,  751,  717,
3136d 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3136e 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  8,  958,  958,. 
3136f 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 39 35  /*   200 */   95
31370 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31371 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31372 20 20 38 30 32 2c 20 20 37 34 30 2c 20 20 39 32    802,  740,  92
31373 31 2c 20 20 39 32 33 2c 0a 20 2f 2a 20 20 20 32  1,  923,. /*   2
31374 31 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 30  10 */   958,  90
31375 34 2c 20 20 37 33 38 2c 20 20 36 36 32 2c 20 20  4,  738,  662,  
31376 37 35 39 2c 20 20 36 37 35 2c 20 20 37 34 39 2c  759,  675,  749,
31377 20 20 36 34 32 2c 20 20 37 39 36 2c 20 20 37 37    642,  796,  77
31378 34 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20  4,. /*   220 */ 
31379 20 20 37 37 34 2c 20 20 39 31 36 2c 20 20 37 39    774,  916,  79
3137a 36 2c 20 20 39 31 36 2c 20 20 36 39 39 2c 20 20  6,  916,  699,  
3137b 39 35 38 2c 20 20 37 38 36 2c 20 20 39 35 38 2c  958,  786,  958,
3137c 20 20 37 38 36 2c 20 20 36 39 36 2c 0a 20 2f 2a    786,  696,. /*
3137d 20 20 20 32 33 30 20 2a 2f 20 20 20 37 38 36 2c     230 */   786,
3137e 20 20 37 37 34 2c 20 20 37 38 36 2c 20 20 37 38    774,  786,  78
3137f 36 2c 20 20 38 36 35 2c 20 20 39 35 38 2c 20 20  6,  865,  958,  
31380 39 35 38 2c 20 20 39 35 38 2c 20 20 37 35 38 2c  958,  958,  758,
31381 20 20 37 34 39 2c 0a 20 2f 2a 20 20 20 32 34 30    749,. /*   240
31382 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 34 33 2c   */   958,  943,
31383 20 20 37 36 35 2c 20 20 37 36 35 2c 20 20 39 33    765,  765,  93
31384 35 2c 20 20 39 33 35 2c 20 20 37 36 35 2c 20 20  5,  935,  765,  
31385 38 30 38 2c 20 20 37 33 30 2c 20 20 37 39 36 2c  808,  730,  796,
31386 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20  . /*   250 */   
31387 37 33 37 2c 20 20 37 33 37 2c 20 20 37 33 37 2c  737,  737,  737,
31388 20 20 37 33 37 2c 20 20 37 36 35 2c 20 20 37 39    737,  765,  79
31389 36 2c 20 20 38 30 38 2c 20 20 37 33 30 2c 20 20  6,  808,  730,  
3138a 37 33 30 2c 20 20 37 36 35 2c 0a 20 2f 2a 20 20  730,  765,. /*  
3138b 20 32 36 30 20 2a 2f 20 20 20 36 35 37 2c 20 20   260 */   657,  
3138c 39 31 30 2c 20 20 39 30 38 2c 20 20 37 36 35 2c  910,  908,  765,
3138d 20 20 37 36 35 2c 20 20 36 35 37 2c 20 20 37 36    765,  657,  76
3138e 35 2c 20 20 36 35 37 2c 20 20 37 36 35 2c 20 20  5,  657,  765,  
3138f 36 35 37 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a  657,. /*   270 *
31390 2f 20 20 20 38 37 35 2c 20 20 37 32 38 2c 20 20  /   875,  728,  
31391 37 32 38 2c 20 20 37 32 38 2c 20 20 37 31 33 2c  728,  728,  713,
31392 20 20 38 37 39 2c 20 20 38 37 39 2c 20 20 38 37    879,  879,  87
31393 35 2c 20 20 37 32 38 2c 20 20 36 39 39 2c 0a 20  5,  728,  699,. 
31394 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20 37 32  /*   280 */   72
31395 38 2c 20 20 37 31 33 2c 20 20 37 32 38 2c 20 20  8,  713,  728,  
31396 37 32 38 2c 20 20 37 37 38 2c 20 20 37 37 33 2c  728,  778,  773,
31397 20 20 37 37 38 2c 20 20 37 37 33 2c 20 20 37 37    778,  773,  77
31398 38 2c 20 20 37 37 33 2c 0a 20 2f 2a 20 20 20 32  8,  773,. /*   2
31399 39 30 20 2a 2f 20 20 20 37 36 35 2c 20 20 37 36  90 */   765,  76
3139a 35 2c 20 20 39 35 38 2c 20 20 37 39 31 2c 20 20  5,  958,  791,  
3139b 37 37 39 2c 20 20 37 38 39 2c 20 20 37 38 37 2c  779,  789,  787,
3139c 20 20 37 39 36 2c 20 20 39 35 38 2c 20 20 37 31    796,  958,  71
3139d 36 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20  6,. /*   300 */ 
3139e 20 20 36 35 30 2c 20 20 36 35 30 2c 20 20 36 33    650,  650,  63
3139f 39 2c 20 20 36 33 39 2c 20 20 36 33 39 2c 20 20  9,  639,  639,  
313a0 36 33 39 2c 20 20 39 35 35 2c 20 20 39 35 35 2c  639,  955,  955,
313a1 20 20 39 35 30 2c 20 20 37 30 31 2c 0a 20 2f 2a    950,  701,. /*
313a2 20 20 20 33 31 30 20 2a 2f 20 20 20 37 30 31 2c     310 */   701,
313a3 20 20 36 38 33 2c 20 20 39 35 38 2c 20 20 39 35    683,  958,  95
313a4 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
313a5 39 35 38 2c 20 20 39 35 38 2c 20 20 38 38 34 2c  958,  958,  884,
313a6 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 32 30    958,. /*   320
313a7 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
313a8 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
313a9 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
313aa 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
313ab 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20  . /*   330 */   
313ac 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
313ad 20 20 36 33 35 2c 20 20 39 34 35 2c 20 20 39 35    635,  945,  95
313ae 38 2c 20 20 39 35 38 2c 20 20 39 34 32 2c 20 20  8,  958,  942,  
313af 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20  958,  958,. /*  
313b0 20 33 34 30 20 2a 2f 20 20 20 39 35 38 2c 20 20   340 */   958,  
313b1 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
313b2 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
313b3 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
313b4 39 35 38 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a  958,. /*   350 *
313b5 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
313b6 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
313b7 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
313b8 38 2c 20 20 39 31 34 2c 20 20 39 35 38 2c 0a 20  8,  914,  958,. 
313b9 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 39 35  /*   360 */   95
313ba 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
313bb 39 35 38 2c 20 20 39 35 38 2c 20 20 39 30 37 2c  958,  958,  907,
313bc 20 20 39 30 36 2c 20 20 39 35 38 2c 20 20 39 35    906,  958,  95
313bd 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33  8,  958,. /*   3
313be 37 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35  70 */   958,  95
313bf 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
313c0 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
313c1 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
313c2 38 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20  8,. /*   380 */ 
313c3 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
313c4 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
313c5 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
313c6 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a    958,  958,. /*
313c7 20 20 20 33 39 30 20 2a 2f 20 20 20 39 35 38 2c     390 */   958,
313c8 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 37 38    958,  958,  78
313c9 38 2c 20 20 39 35 38 2c 20 20 37 38 30 2c 20 20  8,  958,  780,  
313ca 39 35 38 2c 20 20 38 36 38 2c 20 20 39 35 38 2c  958,  868,  958,
313cb 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 34 30 30    958,. /*   400
313cc 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
313cd 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
313ce 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
313cf 39 35 38 2c 20 20 37 34 33 2c 20 20 38 31 37 2c  958,  743,  817,
313d0 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20  . /*   410 */   
313d1 39 35 38 2c 20 20 38 31 36 2c 20 20 38 32 30 2c  958,  816,  820,
313d2 20 20 38 31 35 2c 20 20 36 36 37 2c 20 20 39 35    815,  667,  95
313d3 38 2c 20 20 36 34 38 2c 20 20 39 35 38 2c 20 20  8,  648,  958,  
313d4 36 33 31 2c 20 20 36 33 36 2c 0a 20 2f 2a 20 20  631,  636,. /*  
313d5 20 34 32 30 20 2a 2f 20 20 20 39 35 34 2c 20 20   420 */   954,  
313d6 39 35 37 2c 20 20 39 35 36 2c 20 20 39 35 33 2c  957,  956,  953,
313d7 20 20 39 35 32 2c 20 20 39 35 31 2c 20 20 39 34    952,  951,  94
313d8 36 2c 20 20 39 34 34 2c 20 20 39 34 31 2c 20 20  6,  944,  941,  
313d9 39 34 30 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a  940,. /*   430 *
313da 2f 20 20 20 39 33 39 2c 20 20 39 33 37 2c 20 20  /   939,  937,  
313db 39 33 34 2c 20 20 39 33 30 2c 20 20 38 38 38 2c  934,  930,  888,
313dc 20 20 38 38 36 2c 20 20 38 39 33 2c 20 20 38 39    886,  893,  89
313dd 32 2c 20 20 38 39 31 2c 20 20 38 39 30 2c 0a 20  2,  891,  890,. 
313de 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20 38 38  /*   440 */   88
313df 39 2c 20 20 38 38 37 2c 20 20 38 38 35 2c 20 20  9,  887,  885,  
313e0 38 38 33 2c 20 20 38 30 33 2c 20 20 38 30 31 2c  883,  803,  801,
313e1 20 20 37 39 38 2c 20 20 37 39 35 2c 20 20 39 32    798,  795,  92
313e2 39 2c 20 20 38 38 31 2c 0a 20 2f 2a 20 20 20 34  9,  881,. /*   4
313e3 35 30 20 2a 2f 20 20 20 37 33 39 2c 20 20 37 33  50 */   739,  73
313e4 36 2c 20 20 37 33 35 2c 20 20 36 35 36 2c 20 20  6,  735,  656,  
313e5 39 34 37 2c 20 20 39 31 33 2c 20 20 39 32 32 2c  947,  913,  922,
313e6 20 20 39 32 30 2c 20 20 38 30 39 2c 20 20 39 31    920,  809,  91
313e7 39 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20  9,. /*   460 */ 
313e8 20 20 39 31 38 2c 20 20 39 31 37 2c 20 20 39 31    918,  917,  91
313e9 35 2c 20 20 39 31 32 2c 20 20 38 39 39 2c 20 20  5,  912,  899,  
313ea 38 30 35 2c 20 20 38 30 34 2c 20 20 37 33 31 2c  805,  804,  731,
313eb 20 20 38 37 33 2c 20 20 38 37 32 2c 0a 20 2f 2a    873,  872,. /*
313ec 20 20 20 34 37 30 20 2a 2f 20 20 20 36 35 39 2c     470 */   659,
313ed 20 20 39 30 33 2c 20 20 39 30 32 2c 20 20 39 30    903,  902,  90
313ee 31 2c 20 20 39 30 35 2c 20 20 39 30 39 2c 20 20  1,  905,  909,  
313ef 39 30 30 2c 20 20 37 36 37 2c 20 20 36 35 38 2c  900,  767,  658,
313f0 20 20 36 35 35 2c 0a 20 2f 2a 20 20 20 34 38 30    655,. /*   480
313f1 20 2a 2f 20 20 20 36 36 34 2c 20 20 37 32 30 2c   */   664,  720,
313f2 20 20 37 31 39 2c 20 20 37 32 37 2c 20 20 37 32    719,  727,  72
313f3 35 2c 20 20 37 32 34 2c 20 20 37 32 33 2c 20 20  5,  724,  723,  
313f4 37 32 32 2c 20 20 37 32 31 2c 20 20 37 31 38 2c  722,  721,  718,
313f5 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20  . /*   490 */   
313f6 36 36 36 2c 20 20 36 37 34 2c 20 20 36 38 35 2c  666,  674,  685,
313f7 20 20 37 31 32 2c 20 20 36 39 38 2c 20 20 36 39    712,  698,  69
313f8 37 2c 20 20 38 37 38 2c 20 20 38 38 30 2c 20 20  7,  878,  880,  
313f9 38 37 37 2c 20 20 38 37 36 2c 0a 20 2f 2a 20 20  877,  876,. /*  
313fa 20 35 30 30 20 2a 2f 20 20 20 37 30 35 2c 20 20   500 */   705,  
313fb 37 31 30 2c 20 20 37 30 39 2c 20 20 37 30 38 2c  710,  709,  708,
313fc 20 20 37 30 37 2c 20 20 37 30 36 2c 20 20 37 30    707,  706,  70
313fd 34 2c 20 20 37 30 33 2c 20 20 37 30 32 2c 20 20  4,  703,  702,  
313fe 36 39 35 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a  695,. /*   510 *
313ff 2f 20 20 20 36 39 34 2c 20 20 37 30 30 2c 20 20  /   694,  700,  
31400 36 39 33 2c 20 20 37 31 35 2c 20 20 37 31 34 2c  693,  715,  714,
31401 20 20 37 31 31 2c 20 20 36 39 32 2c 20 20 37 33    711,  692,  73
31402 34 2c 20 20 37 33 33 2c 20 20 37 33 32 2c 0a 20  4,  733,  732,. 
31403 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 37 32  /*   520 */   72
31404 39 2c 20 20 36 39 31 2c 20 20 36 39 30 2c 20 20  9,  691,  690,  
31405 36 38 39 2c 20 20 38 32 30 2c 20 20 36 38 38 2c  689,  820,  688,
31406 20 20 36 38 37 2c 20 20 38 32 36 2c 20 20 38 32    687,  826,  82
31407 35 2c 20 20 38 31 33 2c 0a 20 2f 2a 20 20 20 35  5,  813,. /*   5
31408 33 30 20 2a 2f 20 20 20 38 35 37 2c 20 20 37 35  30 */   857,  75
31409 34 2c 20 20 37 35 33 2c 20 20 37 35 32 2c 20 20  4,  753,  752,  
3140a 37 36 34 2c 20 20 37 36 33 2c 20 20 37 37 36 2c  764,  763,  776,
3140b 20 20 37 37 35 2c 20 20 38 31 31 2c 20 20 38 31    775,  811,  81
3140c 30 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20  0,. /*   540 */ 
3140d 20 20 37 37 37 2c 20 20 37 36 32 2c 20 20 37 35    777,  762,  75
3140e 36 2c 20 20 37 35 35 2c 20 20 37 37 31 2c 20 20  6,  755,  771,  
3140f 37 37 30 2c 20 20 37 36 39 2c 20 20 37 36 38 2c  770,  769,  768,
31410 20 20 37 36 30 2c 20 20 37 35 30 2c 0a 20 2f 2a    760,  750,. /*
31411 20 20 20 35 35 30 20 2a 2f 20 20 20 37 38 32 2c     550 */   782,
31412 20 20 37 38 35 2c 20 20 37 38 34 2c 20 20 37 38    785,  784,  78
31413 31 2c 20 20 38 34 32 2c 20 20 38 35 39 2c 20 20  1,  842,  859,  
31414 37 36 36 2c 20 20 38 35 36 2c 20 20 39 32 38 2c  766,  856,  928,
31415 20 20 39 32 37 2c 0a 20 2f 2a 20 20 20 35 36 30    927,. /*   560
31416 20 2a 2f 20 20 20 39 32 36 2c 20 20 39 32 35 2c   */   926,  925,
31417 20 20 39 32 34 2c 20 20 38 36 31 2c 20 20 38 36    924,  861,  86
31418 30 2c 20 20 38 32 37 2c 20 20 38 32 34 2c 20 20  0,  827,  824,  
31419 36 37 38 2c 20 20 36 37 39 2c 20 20 38 39 37 2c  678,  679,  897,
3141a 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20  . /*   570 */   
3141b 38 39 35 2c 20 20 38 39 36 2c 20 20 38 39 34 2c  895,  896,  894,
3141c 20 20 36 38 31 2c 20 20 36 38 30 2c 20 20 36 37    681,  680,  67
3141d 37 2c 20 20 36 37 36 2c 20 20 38 35 38 2c 20 20  7,  676,  858,  
3141e 37 34 35 2c 20 20 37 34 34 2c 0a 20 2f 2a 20 20  745,  744,. /*  
3141f 20 35 38 30 20 2a 2f 20 20 20 38 35 34 2c 20 20   580 */   854,  
31420 38 35 31 2c 20 20 38 34 33 2c 20 20 38 33 38 2c  851,  843,  838,
31421 20 20 38 35 35 2c 20 20 38 35 32 2c 20 20 38 34    855,  852,  84
31422 34 2c 20 20 38 33 39 2c 20 20 38 33 37 2c 20 20  4,  839,  837,  
31423 38 33 36 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a  836,. /*   590 *
31424 2f 20 20 20 38 32 32 2c 20 20 38 32 31 2c 20 20  /   822,  821,  
31425 38 31 39 2c 20 20 38 31 38 2c 20 20 38 31 34 2c  819,  818,  814,
31426 20 20 38 32 33 2c 20 20 36 36 39 2c 20 20 37 34    823,  669,  74
31427 36 2c 20 20 37 34 32 2c 20 20 37 34 31 2c 0a 20  6,  742,  741,. 
31428 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20 38 31  /*   600 */   81
31429 32 2c 20 20 37 34 38 2c 20 20 37 34 37 2c 20 20  2,  748,  747,  
3142a 36 38 36 2c 20 20 36 38 34 2c 20 20 36 38 32 2c  686,  684,  682,
3142b 20 20 36 36 33 2c 20 20 36 36 31 2c 20 20 36 35    663,  661,  65
3142c 34 2c 20 20 36 35 32 2c 0a 20 2f 2a 20 20 20 36  4,  652,. /*   6
3142d 31 30 20 2a 2f 20 20 20 36 35 31 2c 20 20 36 35  10 */   651,  65
3142e 33 2c 20 20 36 34 39 2c 20 20 36 34 37 2c 20 20  3,  649,  647,  
3142f 36 34 36 2c 20 20 36 34 35 2c 20 20 36 34 34 2c  646,  645,  644,
31430 20 20 36 34 33 2c 20 20 36 37 32 2c 20 20 36 37    643,  672,  67
31431 31 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20  1,. /*   620 */ 
31432 20 20 36 37 30 2c 20 20 36 36 38 2c 20 20 36 36    670,  668,  66
31433 37 2c 20 20 36 34 31 2c 20 20 36 33 38 2c 20 20  7,  641,  638,  
31434 36 33 37 2c 20 20 36 33 33 2c 20 20 36 33 32 2c  637,  633,  632,
31435 20 20 36 33 30 2c 0a 7d 3b 0a 0a 2f 2a 20 54 68    630,.};../* Th
31436 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6d 61 70  e next table map
31437 73 20 74 6f 6b 65 6e 73 20 69 6e 74 6f 20 66 61  s tokens into fa
31438 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 73 2e 20 20  llback tokens.  
31439 49 66 20 61 20 63 6f 6e 73 74 72 75 63 74 0a 2a  If a construct.*
3143a 2a 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f  * like the follo
3143b 77 69 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20  wing:.** .**    
3143c 20 20 25 66 61 6c 6c 62 61 63 6b 20 49 44 20 58    %fallback ID X
3143d 20 59 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61 70 70 65   Y Z..**.** appe
3143e 61 72 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d  ars in the gramm
3143f 61 72 2c 20 74 68 65 6e 20 49 44 20 62 65 63 6f  ar, then ID beco
31440 6d 65 73 20 61 20 66 61 6c 6c 62 61 63 6b 20 74  mes a fallback t
31441 6f 6b 65 6e 20 66 6f 72 20 58 2c 20 59 2c 0a 2a  oken for X, Y,.*
31442 2a 20 61 6e 64 20 5a 2e 20 20 57 68 65 6e 65 76  * and Z.  Whenev
31443 65 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 6f  er one of the to
31444 6b 65 6e 73 20 58 2c 20 59 2c 20 6f 72 20 5a 20  kens X, Y, or Z 
31445 69 73 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20  is input to the 
31446 70 61 72 73 65 72 0a 2a 2a 20 62 75 74 20 69 74  parser.** but it
31447 20 64 6f 65 73 20 6e 6f 74 20 70 61 72 73 65 2c   does not parse,
31448 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65   the type of the
31449 20 74 6f 6b 65 6e 20 69 73 20 63 68 61 6e 67 65   token is change
3144a 64 20 74 6f 20 49 44 20 61 6e 64 0a 2a 2a 20 74  d to ID and.** t
3144b 68 65 20 70 61 72 73 65 20 69 73 20 72 65 74 72  he parse is retr
3144c 69 65 64 20 62 65 66 6f 72 65 20 61 6e 20 65 72  ied before an er
3144d 72 6f 72 20 69 73 20 74 68 72 6f 77 6e 2e 0a 2a  ror is thrown..*
3144e 2f 0a 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42  /.#ifdef YYFALLB
3144f 41 43 4b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ACK.static const
31450 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 46 61   YYCODETYPE yyFa
31451 6c 6c 62 61 63 6b 5b 5d 20 3d 20 7b 0a 20 20 20  llback[] = {.   
31452 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20   0,  /*         
31453 20 24 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f   $ => nothing */
31454 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
31455 20 20 53 45 4d 49 20 3d 3e 20 6e 6f 74 68 69 6e    SEMI => nothin
31456 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  g */.   26,  /* 
31457 20 20 20 45 58 50 4c 41 49 4e 20 3d 3e 20 49 44     EXPLAIN => ID
31458 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
31459 20 20 20 20 51 55 45 52 59 20 3d 3e 20 49 44 20      QUERY => ID 
3145a 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
3145b 20 20 20 20 50 4c 41 4e 20 3d 3e 20 49 44 20 2a      PLAN => ID *
3145c 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
3145d 20 20 42 45 47 49 4e 20 3d 3e 20 49 44 20 2a 2f    BEGIN => ID */
3145e 0a 20 20 20 20 30 2c 20 20 2f 2a 20 54 52 41 4e  .    0,  /* TRAN
3145f 53 41 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69  SACTION => nothi
31460 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ng */.   26,  /*
31461 20 20 20 44 45 46 45 52 52 45 44 20 3d 3e 20 49     DEFERRED => I
31462 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
31463 20 49 4d 4d 45 44 49 41 54 45 20 3d 3e 20 49 44   IMMEDIATE => ID
31464 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
31465 45 58 43 4c 55 53 49 56 45 20 3d 3e 20 49 44 20  EXCLUSIVE => ID 
31466 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
31467 20 20 43 4f 4d 4d 49 54 20 3d 3e 20 6e 6f 74 68    COMMIT => noth
31468 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f  ing */.   26,  /
31469 2a 20 20 20 20 20 20 20 20 45 4e 44 20 3d 3e 20  *        END => 
3146a 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
3146b 20 20 20 52 4f 4c 4c 42 41 43 4b 20 3d 3e 20 49     ROLLBACK => I
3146c 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
3146d 20 53 41 56 45 50 4f 49 4e 54 20 3d 3e 20 49 44   SAVEPOINT => ID
3146e 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
3146f 20 20 52 45 4c 45 41 53 45 20 3d 3e 20 49 44 20    RELEASE => ID 
31470 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
31471 20 20 20 20 20 20 54 4f 20 3d 3e 20 6e 6f 74 68        TO => noth
31472 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
31473 2a 20 20 20 20 20 20 54 41 42 4c 45 20 3d 3e 20  *      TABLE => 
31474 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
31475 2c 20 20 2f 2a 20 20 20 20 20 43 52 45 41 54 45  ,  /*     CREATE
31476 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
31477 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
31478 20 20 49 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20    IF => ID */.  
31479 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
3147a 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  NOT => nothing *
3147b 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
3147c 20 45 58 49 53 54 53 20 3d 3e 20 6e 6f 74 68 69   EXISTS => nothi
3147d 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ng */.   26,  /*
3147e 20 20 20 20 20 20 20 54 45 4d 50 20 3d 3e 20 49         TEMP => I
3147f 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  D */.    0,  /* 
31480 20 20 20 20 20 20 20 20 4c 50 20 3d 3e 20 6e 6f          LP => no
31481 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
31482 20 2f 2a 20 20 20 20 20 20 20 20 20 52 50 20 3d   /*         RP =
31483 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
31484 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20   0,  /*         
31485 41 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  AS => nothing */
31486 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
31487 20 43 4f 4d 4d 41 20 3d 3e 20 6e 6f 74 68 69 6e   COMMA => nothin
31488 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  g */.    0,  /* 
31489 20 20 20 20 20 20 20 20 49 44 20 3d 3e 20 6e 6f          ID => no
3148a 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
3148b 20 2f 2a 20 20 20 20 49 4e 44 45 58 45 44 20 3d   /*    INDEXED =
3148c 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
3148d 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 41 42 4f  26,  /*      ABO
3148e 52 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  RT => ID */.   2
3148f 36 2c 20 20 2f 2a 20 20 20 20 20 41 43 54 49 4f  6,  /*     ACTIO
31490 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  N => ID */.   26
31491 2c 20 20 2f 2a 20 20 20 20 20 20 41 46 54 45 52  ,  /*      AFTER
31492 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
31493 20 20 2f 2a 20 20 20 20 41 4e 41 4c 59 5a 45 20    /*    ANALYZE 
31494 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
31495 20 2f 2a 20 20 20 20 20 20 20 20 41 53 43 20 3d   /*        ASC =
31496 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
31497 2f 2a 20 20 20 20 20 41 54 54 41 43 48 20 3d 3e  /*     ATTACH =>
31498 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
31499 2a 20 20 20 20 20 42 45 46 4f 52 45 20 3d 3e 20  *     BEFORE => 
3149a 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
3149b 20 20 20 20 20 20 20 20 20 42 59 20 3d 3e 20 49           BY => I
3149c 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
3149d 20 20 20 43 41 53 43 41 44 45 20 3d 3e 20 49 44     CASCADE => ID
3149e 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
3149f 20 20 20 20 20 43 41 53 54 20 3d 3e 20 49 44 20       CAST => ID 
314a0 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
314a1 43 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 49 44 20 2a  COLUMNKW => ID *
314a2 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43  /.   26,  /*   C
314a3 4f 4e 46 4c 49 43 54 20 3d 3e 20 49 44 20 2a 2f  ONFLICT => ID */
314a4 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 44 41  .   26,  /*   DA
314a5 54 41 42 41 53 45 20 3d 3e 20 49 44 20 2a 2f 0a  TABASE => ID */.
314a6 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
314a7 20 44 45 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20   DESC => ID */. 
314a8 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 44 45    26,  /*     DE
314a9 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TACH => ID */.  
314aa 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 45   26,  /*       E
314ab 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ACH => ID */.   
314ac 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 46 41  26,  /*       FA
314ad 49 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  IL => ID */.   2
314ae 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 46 4f  6,  /*        FO
314af 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  R => ID */.   26
314b0 2c 20 20 2f 2a 20 20 20 20 20 49 47 4e 4f 52 45  ,  /*     IGNORE
314b1 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
314b2 20 20 2f 2a 20 20 49 4e 49 54 49 41 4c 4c 59 20    /*  INITIALLY 
314b3 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
314b4 20 2f 2a 20 20 20 20 49 4e 53 54 45 41 44 20 3d   /*    INSTEAD =
314b5 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
314b6 2f 2a 20 20 20 20 4c 49 4b 45 5f 4b 57 20 3d 3e  /*    LIKE_KW =>
314b7 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
314b8 2a 20 20 20 20 20 20 4d 41 54 43 48 20 3d 3e 20  *      MATCH => 
314b9 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
314ba 20 20 20 20 20 20 20 20 20 4e 4f 20 3d 3e 20 49           NO => I
314bb 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
314bc 20 20 20 20 20 20 20 4b 45 59 20 3d 3e 20 49 44         KEY => ID
314bd 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
314be 20 20 20 20 20 20 20 4f 46 20 3d 3e 20 49 44 20         OF => ID 
314bf 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
314c0 20 20 4f 46 46 53 45 54 20 3d 3e 20 49 44 20 2a    OFFSET => ID *
314c1 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
314c2 20 50 52 41 47 4d 41 20 3d 3e 20 49 44 20 2a 2f   PRAGMA => ID */
314c3 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
314c4 20 52 41 49 53 45 20 3d 3e 20 49 44 20 2a 2f 0a   RAISE => ID */.
314c5 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 52 45     26,  /*    RE
314c6 50 4c 41 43 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  PLACE => ID */. 
314c7 20 20 32 36 2c 20 20 2f 2a 20 20 20 52 45 53 54    26,  /*   REST
314c8 52 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  RICT => ID */.  
314c9 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
314ca 52 4f 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ROW => ID */.   
314cb 32 36 2c 20 20 2f 2a 20 20 20 20 54 52 49 47 47  26,  /*    TRIGG
314cc 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ER => ID */.   2
314cd 36 2c 20 20 2f 2a 20 20 20 20 20 56 41 43 55 55  6,  /*     VACUU
314ce 4d 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  M => ID */.   26
314cf 2c 20 20 2f 2a 20 20 20 20 20 20 20 56 49 45 57  ,  /*       VIEW
314d0 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
314d1 20 20 2f 2a 20 20 20 20 56 49 52 54 55 41 4c 20    /*    VIRTUAL 
314d2 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
314d3 20 2f 2a 20 20 20 20 52 45 49 4e 44 45 58 20 3d   /*    REINDEX =
314d4 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
314d5 2f 2a 20 20 20 20 20 52 45 4e 41 4d 45 20 3d 3e  /*     RENAME =>
314d6 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
314d7 2a 20 20 20 43 54 49 4d 45 5f 4b 57 20 3d 3e 20  *   CTIME_KW => 
314d8 49 44 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 20  ID */.};.#endif 
314d9 2f 2a 20 59 59 46 41 4c 4c 42 41 43 4b 20 2a 2f  /* YYFALLBACK */
314da 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
314db 6e 67 20 73 74 72 75 63 74 75 72 65 20 72 65 70  ng structure rep
314dc 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65  resents a single
314dd 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a   element of the.
314de 2a 2a 20 70 61 72 73 65 72 27 73 20 73 74 61 63  ** parser's stac
314df 6b 2e 20 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20  k.  Information 
314e0 73 74 6f 72 65 64 20 69 6e 63 6c 75 64 65 73 3a  stored includes:
314e1 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20  .**.**   +  The 
314e2 73 74 61 74 65 20 6e 75 6d 62 65 72 20 66 6f 72  state number for
314e3 20 74 68 65 20 70 61 72 73 65 72 20 61 74 20 74   the parser at t
314e4 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  his level of the
314e5 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20   stack..**.**   
314e6 2b 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  +  The value of 
314e7 74 68 65 20 74 6f 6b 65 6e 20 73 74 6f 72 65 64  the token stored
314e8 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f   at this level o
314e9 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20  f the stack..** 
314ea 20 20 20 20 20 28 49 6e 20 6f 74 68 65 72 20 77       (In other w
314eb 6f 72 64 73 2c 20 74 68 65 20 22 6d 61 6a 6f 72  ords, the "major
314ec 22 20 74 6f 6b 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20  " token.).**.** 
314ed 20 20 2b 20 20 54 68 65 20 73 65 6d 61 6e 74 69    +  The semanti
314ee 63 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 61  c value stored a
314ef 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20  t this level of 
314f0 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 69 73  the stack.  This
314f1 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 68 65 20   is.**      the 
314f2 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64  information used
314f3 20 62 79 20 74 68 65 20 61 63 74 69 6f 6e 20 72   by the action r
314f4 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 67  outines in the g
314f5 72 61 6d 6d 61 72 2e 0a 2a 2a 20 20 20 20 20 20  rammar..**      
314f6 49 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 20  It is sometimes 
314f7 63 61 6c 6c 65 64 20 74 68 65 20 22 6d 69 6e 6f  called the "mino
314f8 72 22 20 74 6f 6b 65 6e 2e 0a 2a 2f 0a 73 74 72  r" token..*/.str
314f9 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72 79  uct yyStackEntry
314fa 20 7b 0a 20 20 59 59 41 43 54 49 4f 4e 54 59 50   {.  YYACTIONTYP
314fb 45 20 73 74 61 74 65 6e 6f 3b 20 20 2f 2a 20 54  E stateno;  /* T
314fc 68 65 20 73 74 61 74 65 2d 6e 75 6d 62 65 72 20  he state-number 
314fd 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  */.  YYCODETYPE 
314fe 6d 61 6a 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54  major;      /* T
314ff 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 76  he major token v
31500 61 6c 75 65 2e 20 20 54 68 69 73 20 69 73 20 74  alue.  This is t
31501 68 65 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20  he code.        
31502 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31503 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74   ** number for t
31504 68 65 20 74 6f 6b 65 6e 20 61 74 20 74 68 69 73  he token at this
31505 20 73 74 61 63 6b 20 6c 65 76 65 6c 20 2a 2f 0a   stack level */.
31506 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6d 69    YYMINORTYPE mi
31507 6e 6f 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  nor;     /* The 
31508 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20 6d 69  user-supplied mi
31509 6e 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e  nor token value.
3150a 20 20 54 68 69 73 0a 20 20 20 20 20 20 20 20 20    This.         
3150b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3150c 2a 2a 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ** is the value 
3150d 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 20 2a 2f  of the token  */
3150e 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  .};.typedef stru
3150f 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  ct yyStackEntry 
31510 79 79 53 74 61 63 6b 45 6e 74 72 79 3b 0a 0a 2f  yyStackEntry;../
31511 2a 20 54 68 65 20 73 74 61 74 65 20 6f 66 20 74  * The state of t
31512 68 65 20 70 61 72 73 65 72 20 69 73 20 63 6f 6d  he parser is com
31513 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61 69 6e 65  pletely containe
31514 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65  d in an instance
31515 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f   of.** the follo
31516 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 2a  wing structure *
31517 2f 0a 73 74 72 75 63 74 20 79 79 50 61 72 73 65  /.struct yyParse
31518 72 20 7b 0a 20 20 69 6e 74 20 79 79 69 64 78 3b  r {.  int yyidx;
31519 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3151a 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
3151b 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e 20 73  top element in s
3151c 74 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66 20 59  tack */.#ifdef Y
3151d 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45  YTRACKMAXSTACKDE
3151e 50 54 48 0a 20 20 69 6e 74 20 79 79 69 64 78 4d  PTH.  int yyidxM
3151f 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ax;             
31520 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76      /* Maximum v
31521 61 6c 75 65 20 6f 66 20 79 79 69 64 78 20 2a 2f  alue of yyidx */
31522 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 79 79  .#endif.  int yy
31523 65 72 72 63 6e 74 3b 20 20 20 20 20 20 20 20 20  errcnt;         
31524 20 20 20 20 20 20 20 20 2f 2a 20 53 68 69 66 74          /* Shift
31525 73 20 6c 65 66 74 20 62 65 66 6f 72 65 20 6f 75  s left before ou
31526 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 2a  t of the error *
31527 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
31528 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20 20  rARG_SDECL      
31529 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70            /* A p
3152a 6c 61 63 65 20 74 6f 20 68 6f 6c 64 20 25 65 78  lace to hold %ex
3152b 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  tra_argument */.
3152c 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48  #if YYSTACKDEPTH
3152d 3c 3d 30 0a 20 20 69 6e 74 20 79 79 73 74 6b 73  <=0.  int yystks
3152e 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
3152f 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73      /* Current s
31530 69 64 65 20 6f 66 20 74 68 65 20 73 74 61 63 6b  ide of the stack
31531 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e 74   */.  yyStackEnt
31532 72 79 20 2a 79 79 73 74 61 63 6b 3b 20 20 20 20  ry *yystack;    
31533 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
31534 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6c  r's stack */.#el
31535 73 65 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72  se.  yyStackEntr
31536 79 20 79 79 73 74 61 63 6b 5b 59 59 53 54 41 43  y yystack[YYSTAC
31537 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20 54 68 65  KDEPTH];  /* The
31538 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20   parser's stack 
31539 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70  */.#endif.};.typ
3153a 65 64 65 66 20 73 74 72 75 63 74 20 79 79 50 61  edef struct yyPa
3153b 72 73 65 72 20 79 79 50 61 72 73 65 72 3b 0a 0a  rser yyParser;..
3153c 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73  #ifndef NDEBUG.s
3153d 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79 54 72  tatic FILE *yyTr
3153e 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 73 74 61  aceFILE = 0;.sta
3153f 74 69 63 20 63 68 61 72 20 2a 79 79 54 72 61 63  tic char *yyTrac
31540 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 23 65 6e  ePrompt = 0;.#en
31541 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
31542 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
31543 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20 70 61 72  ./* .** Turn par
31544 73 65 72 20 74 72 61 63 69 6e 67 20 6f 6e 20 62  ser tracing on b
31545 79 20 67 69 76 69 6e 67 20 61 20 73 74 72 65 61  y giving a strea
31546 6d 20 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  m to which to wr
31547 69 74 65 20 74 68 65 20 74 72 61 63 65 0a 2a 2a  ite the trace.**
31548 20 61 6e 64 20 61 20 70 72 6f 6d 70 74 20 74 6f   and a prompt to
31549 20 70 72 65 66 61 63 65 20 65 61 63 68 20 74 72   preface each tr
3154a 61 63 65 20 6d 65 73 73 61 67 65 2e 20 20 54 72  ace message.  Tr
3154b 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20  acing is turned 
3154c 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67  off.** by making
3154d 20 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e 74   either argument
3154e 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49 6e 70   NULL .**.** Inp
3154f 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  uts:.** <ul>.** 
31550 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20 74 6f 20  <li> A FILE* to 
31551 77 68 69 63 68 20 74 72 61 63 65 20 6f 75 74 70  which trace outp
31552 75 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69  ut should be wri
31553 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 49 66  tten..**      If
31554 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63   NULL, then trac
31555 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 66  ing is turned of
31556 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 72 65  f..** <li> A pre
31557 66 69 78 20 73 74 72 69 6e 67 20 77 72 69 74 74  fix string writt
31558 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  en at the beginn
31559 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20  ing of every.** 
3155a 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74 72 61       line of tra
3155b 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66 20 4e  ce output.  If N
3155c 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69 6e  ULL, then tracin
3155d 67 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 75 72  g is.**      tur
3155e 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 2f 75 6c  ned off..** </ul
3155f 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  >.**.** Outputs:
31560 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  .** None..*/.SQL
31561 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
31562 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
31563 61 63 65 28 46 49 4c 45 20 2a 54 72 61 63 65 46  ace(FILE *TraceF
31564 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54 72 61 63  ILE, char *zTrac
31565 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79 79 54 72  ePrompt){.  yyTr
31566 61 63 65 46 49 4c 45 20 3d 20 54 72 61 63 65 46  aceFILE = TraceF
31567 49 4c 45 3b 0a 20 20 79 79 54 72 61 63 65 50 72  ILE;.  yyTracePr
31568 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65 50 72 6f  ompt = zTracePro
31569 6d 70 74 3b 0a 20 20 69 66 28 20 79 79 54 72 61  mpt;.  if( yyTra
3156a 63 65 46 49 4c 45 3d 3d 30 20 29 20 79 79 54 72  ceFILE==0 ) yyTr
3156b 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 20  acePrompt = 0;. 
3156c 20 65 6c 73 65 20 69 66 28 20 79 79 54 72 61 63   else if( yyTrac
3156d 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20 79 79 54  ePrompt==0 ) yyT
3156e 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 7d 0a  raceFILE = 0;.}.
3156f 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
31570 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45   */..#ifndef NDE
31571 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69  BUG./* For traci
31572 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65 20 6e  ng shifts, the n
31573 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65 72 6d  ames of all term
31574 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74 65 72  inals and nonter
31575 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65 20 72 65  minals.** are re
31576 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 6f 6c  quired.  The fol
31577 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73 75 70  lowing table sup
31578 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61 6d 65  plies these name
31579 73 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  s */.static cons
3157a 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79 79  t char *const yy
3157b 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20 7b 20  TokenName[] = { 
3157c 0a 20 20 22 24 22 2c 20 20 20 20 20 20 20 20 20  .  "$",         
3157d 20 20 20 20 22 53 45 4d 49 22 2c 20 20 20 20 20      "SEMI",     
3157e 20 20 20 20 20 22 45 58 50 4c 41 49 4e 22 2c 20       "EXPLAIN", 
3157f 20 20 20 20 20 20 22 51 55 45 52 59 22 2c 20 20        "QUERY",  
31580 20 20 20 20 20 0a 20 20 22 50 4c 41 4e 22 2c 20       .  "PLAN", 
31581 20 20 20 20 20 20 20 20 20 22 42 45 47 49 4e 22           "BEGIN"
31582 2c 20 20 20 20 20 20 20 20 20 22 54 52 41 4e 53  ,         "TRANS
31583 41 43 54 49 4f 4e 22 2c 20 20 20 22 44 45 46 45  ACTION",   "DEFE
31584 52 52 45 44 22 2c 20 20 20 20 0a 20 20 22 49 4d  RRED",    .  "IM
31585 4d 45 44 49 41 54 45 22 2c 20 20 20 20 20 22 45  MEDIATE",     "E
31586 58 43 4c 55 53 49 56 45 22 2c 20 20 20 20 20 22  XCLUSIVE",     "
31587 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20 20 20 20  COMMIT",        
31588 22 45 4e 44 22 2c 20 20 20 20 20 20 20 20 20 0a  "END",         .
31589 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 20 20    "ROLLBACK",   
3158a 20 20 20 22 53 41 56 45 50 4f 49 4e 54 22 2c 20     "SAVEPOINT", 
3158b 20 20 20 20 22 52 45 4c 45 41 53 45 22 2c 20 20      "RELEASE",  
3158c 20 20 20 20 20 22 54 4f 22 2c 20 20 20 20 20 20       "TO",      
3158d 20 20 20 20 0a 20 20 22 54 41 42 4c 45 22 2c 20      .  "TABLE", 
3158e 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 22          "CREATE"
3158f 2c 20 20 20 20 20 20 20 20 22 49 46 22 2c 20 20  ,        "IF",  
31590 20 20 20 20 20 20 20 20 20 20 22 4e 4f 54 22 2c            "NOT",
31591 20 20 20 20 20 20 20 20 20 0a 20 20 22 45 58 49           .  "EXI
31592 53 54 53 22 2c 20 20 20 20 20 20 20 20 22 54 45  STS",        "TE
31593 4d 50 22 2c 20 20 20 20 20 20 20 20 20 20 22 4c  MP",          "L
31594 50 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  P",            "
31595 52 50 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20  RP",          . 
31596 20 22 41 53 22 2c 20 20 20 20 20 20 20 20 20 20   "AS",          
31597 20 20 22 43 4f 4d 4d 41 22 2c 20 20 20 20 20 20    "COMMA",      
31598 20 20 20 22 49 44 22 2c 20 20 20 20 20 20 20 20     "ID",        
31599 20 20 20 20 22 49 4e 44 45 58 45 44 22 2c 20 20      "INDEXED",  
3159a 20 20 20 0a 20 20 22 41 42 4f 52 54 22 2c 20 20     .  "ABORT",  
3159b 20 20 20 20 20 20 20 22 41 43 54 49 4f 4e 22 2c         "ACTION",
3159c 20 20 20 20 20 20 20 20 22 41 46 54 45 52 22 2c          "AFTER",
3159d 20 20 20 20 20 20 20 20 20 22 41 4e 41 4c 59 5a           "ANALYZ
3159e 45 22 2c 20 20 20 20 20 0a 20 20 22 41 53 43 22  E",     .  "ASC"
3159f 2c 20 20 20 20 20 20 20 20 20 20 20 22 41 54 54  ,           "ATT
315a0 41 43 48 22 2c 20 20 20 20 20 20 20 20 22 42 45  ACH",        "BE
315a1 46 4f 52 45 22 2c 20 20 20 20 20 20 20 20 22 42  FORE",        "B
315a2 59 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20  Y",          .  
315a3 22 43 41 53 43 41 44 45 22 2c 20 20 20 20 20 20  "CASCADE",      
315a4 20 22 43 41 53 54 22 2c 20 20 20 20 20 20 20 20   "CAST",        
315a5 20 20 22 43 4f 4c 55 4d 4e 4b 57 22 2c 20 20 20    "COLUMNKW",   
315a6 20 20 20 22 43 4f 4e 46 4c 49 43 54 22 2c 20 20     "CONFLICT",  
315a7 20 20 0a 20 20 22 44 41 54 41 42 41 53 45 22 2c    .  "DATABASE",
315a8 20 20 20 20 20 20 22 44 45 53 43 22 2c 20 20 20        "DESC",   
315a9 20 20 20 20 20 20 20 22 44 45 54 41 43 48 22 2c         "DETACH",
315aa 20 20 20 20 20 20 20 20 22 45 41 43 48 22 2c 20          "EACH", 
315ab 20 20 20 20 20 20 20 0a 20 20 22 46 41 49 4c 22         .  "FAIL"
315ac 2c 20 20 20 20 20 20 20 20 20 20 22 46 4f 52 22  ,          "FOR"
315ad 2c 20 20 20 20 20 20 20 20 20 20 20 22 49 47 4e  ,           "IGN
315ae 4f 52 45 22 2c 20 20 20 20 20 20 20 20 22 49 4e  ORE",        "IN
315af 49 54 49 41 4c 4c 59 22 2c 20 20 20 0a 20 20 22  ITIALLY",   .  "
315b0 49 4e 53 54 45 41 44 22 2c 20 20 20 20 20 20 20  INSTEAD",       
315b1 22 4c 49 4b 45 5f 4b 57 22 2c 20 20 20 20 20 20  "LIKE_KW",      
315b2 20 22 4d 41 54 43 48 22 2c 20 20 20 20 20 20 20   "MATCH",       
315b3 20 20 22 4e 4f 22 2c 20 20 20 20 20 20 20 20 20    "NO",         
315b4 20 0a 20 20 22 4b 45 59 22 2c 20 20 20 20 20 20   .  "KEY",      
315b5 20 20 20 20 20 22 4f 46 22 2c 20 20 20 20 20 20       "OF",      
315b6 20 20 20 20 20 20 22 4f 46 46 53 45 54 22 2c 20        "OFFSET", 
315b7 20 20 20 20 20 20 20 22 50 52 41 47 4d 41 22 2c         "PRAGMA",
315b8 20 20 20 20 20 20 0a 20 20 22 52 41 49 53 45 22        .  "RAISE"
315b9 2c 20 20 20 20 20 20 20 20 20 22 52 45 50 4c 41  ,         "REPLA
315ba 43 45 22 2c 20 20 20 20 20 20 20 22 52 45 53 54  CE",       "REST
315bb 52 49 43 54 22 2c 20 20 20 20 20 20 22 52 4f 57  RICT",      "ROW
315bc 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 54  ",         .  "T
315bd 52 49 47 47 45 52 22 2c 20 20 20 20 20 20 20 22  RIGGER",       "
315be 56 41 43 55 55 4d 22 2c 20 20 20 20 20 20 20 20  VACUUM",        
315bf 22 56 49 45 57 22 2c 20 20 20 20 20 20 20 20 20  "VIEW",         
315c0 20 22 56 49 52 54 55 41 4c 22 2c 20 20 20 20 20   "VIRTUAL",     
315c1 0a 20 20 22 52 45 49 4e 44 45 58 22 2c 20 20 20  .  "REINDEX",   
315c2 20 20 20 20 22 52 45 4e 41 4d 45 22 2c 20 20 20      "RENAME",   
315c3 20 20 20 20 20 22 43 54 49 4d 45 5f 4b 57 22 2c       "CTIME_KW",
315c4 20 20 20 20 20 20 22 41 4e 59 22 2c 20 20 20 20        "ANY",    
315c5 20 20 20 20 20 0a 20 20 22 4f 52 22 2c 20 20 20       .  "OR",   
315c6 20 20 20 20 20 20 20 20 20 22 41 4e 44 22 2c 20           "AND", 
315c7 20 20 20 20 20 20 20 20 20 20 22 49 53 22 2c 20            "IS", 
315c8 20 20 20 20 20 20 20 20 20 20 20 22 42 45 54 57             "BETW
315c9 45 45 4e 22 2c 20 20 20 20 20 0a 20 20 22 49 4e  EEN",     .  "IN
315ca 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 49  ",            "I
315cb 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20 20 22  SNULL",        "
315cc 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20  NOTNULL",       
315cd 22 4e 45 22 2c 20 20 20 20 20 20 20 20 20 20 0a  "NE",          .
315ce 20 20 22 45 51 22 2c 20 20 20 20 20 20 20 20 20    "EQ",         
315cf 20 20 20 22 47 54 22 2c 20 20 20 20 20 20 20 20     "GT",        
315d0 20 20 20 20 22 4c 45 22 2c 20 20 20 20 20 20 20      "LE",       
315d1 20 20 20 20 20 22 4c 54 22 2c 20 20 20 20 20 20       "LT",      
315d2 20 20 20 20 0a 20 20 22 47 45 22 2c 20 20 20 20      .  "GE",    
315d3 20 20 20 20 20 20 20 20 22 45 53 43 41 50 45 22          "ESCAPE"
315d4 2c 20 20 20 20 20 20 20 20 22 42 49 54 41 4e 44  ,        "BITAND
315d5 22 2c 20 20 20 20 20 20 20 20 22 42 49 54 4f 52  ",        "BITOR
315d6 22 2c 20 20 20 20 20 20 20 0a 20 20 22 4c 53 48  ",       .  "LSH
315d7 49 46 54 22 2c 20 20 20 20 20 20 20 20 22 52 53  IFT",        "RS
315d8 48 49 46 54 22 2c 20 20 20 20 20 20 20 20 22 50  HIFT",        "P
315d9 4c 55 53 22 2c 20 20 20 20 20 20 20 20 20 20 22  LUS",          "
315da 4d 49 4e 55 53 22 2c 20 20 20 20 20 20 20 0a 20  MINUS",       . 
315db 20 22 53 54 41 52 22 2c 20 20 20 20 20 20 20 20   "STAR",        
315dc 20 20 22 53 4c 41 53 48 22 2c 20 20 20 20 20 20    "SLASH",      
315dd 20 20 20 22 52 45 4d 22 2c 20 20 20 20 20 20 20     "REM",       
315de 20 20 20 20 22 43 4f 4e 43 41 54 22 2c 20 20 20      "CONCAT",   
315df 20 20 20 0a 20 20 22 43 4f 4c 4c 41 54 45 22 2c     .  "COLLATE",
315e0 20 20 20 20 20 20 20 22 42 49 54 4e 4f 54 22 2c         "BITNOT",
315e1 20 20 20 20 20 20 20 20 22 53 54 52 49 4e 47 22          "STRING"
315e2 2c 20 20 20 20 20 20 20 20 22 4a 4f 49 4e 5f 4b  ,        "JOIN_K
315e3 57 22 2c 20 20 20 20 20 0a 20 20 22 43 4f 4e 53  W",     .  "CONS
315e4 54 52 41 49 4e 54 22 2c 20 20 20 20 22 44 45 46  TRAINT",    "DEF
315e5 41 55 4c 54 22 2c 20 20 20 20 20 20 20 22 4e 55  AULT",       "NU
315e6 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22 50  LL",          "P
315e7 52 49 4d 41 52 59 22 2c 20 20 20 20 20 0a 20 20  RIMARY",     .  
315e8 22 55 4e 49 51 55 45 22 2c 20 20 20 20 20 20 20  "UNIQUE",       
315e9 20 22 43 48 45 43 4b 22 2c 20 20 20 20 20 20 20   "CHECK",       
315ea 20 20 22 52 45 46 45 52 45 4e 43 45 53 22 2c 20    "REFERENCES", 
315eb 20 20 20 22 41 55 54 4f 49 4e 43 52 22 2c 20 20     "AUTOINCR",  
315ec 20 20 0a 20 20 22 4f 4e 22 2c 20 20 20 20 20 20    .  "ON",      
315ed 20 20 20 20 20 20 22 44 45 4c 45 54 45 22 2c 20        "DELETE", 
315ee 20 20 20 20 20 20 20 22 55 50 44 41 54 45 22 2c         "UPDATE",
315ef 20 20 20 20 20 20 20 20 22 53 45 54 22 2c 20 20          "SET",  
315f0 20 20 20 20 20 20 20 0a 20 20 22 44 45 46 45 52         .  "DEFER
315f1 52 41 42 4c 45 22 2c 20 20 20 20 22 46 4f 52 45  RABLE",    "FORE
315f2 49 47 4e 22 2c 20 20 20 20 20 20 20 22 44 52 4f  IGN",       "DRO
315f3 50 22 2c 20 20 20 20 20 20 20 20 20 20 22 55 4e  P",          "UN
315f4 49 4f 4e 22 2c 20 20 20 20 20 20 20 0a 20 20 22  ION",       .  "
315f5 41 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20 20  ALL",           
315f6 22 45 58 43 45 50 54 22 2c 20 20 20 20 20 20 20  "EXCEPT",       
315f7 20 22 49 4e 54 45 52 53 45 43 54 22 2c 20 20 20   "INTERSECT",   
315f8 20 20 22 53 45 4c 45 43 54 22 2c 20 20 20 20 20    "SELECT",     
315f9 20 0a 20 20 22 44 49 53 54 49 4e 43 54 22 2c 20   .  "DISTINCT", 
315fa 20 20 20 20 20 22 44 4f 54 22 2c 20 20 20 20 20       "DOT",     
315fb 20 20 20 20 20 20 22 46 52 4f 4d 22 2c 20 20 20        "FROM",   
315fc 20 20 20 20 20 20 20 22 4a 4f 49 4e 22 2c 20 20         "JOIN",  
315fd 20 20 20 20 20 20 0a 20 20 22 55 53 49 4e 47 22        .  "USING"
315fe 2c 20 20 20 20 20 20 20 20 20 22 4f 52 44 45 52  ,         "ORDER
315ff 22 2c 20 20 20 20 20 20 20 20 20 22 47 52 4f 55  ",         "GROU
31600 50 22 2c 20 20 20 20 20 20 20 20 20 22 48 41 56  P",         "HAV
31601 49 4e 47 22 2c 20 20 20 20 20 20 0a 20 20 22 4c  ING",      .  "L
31602 49 4d 49 54 22 2c 20 20 20 20 20 20 20 20 20 22  IMIT",         "
31603 57 48 45 52 45 22 2c 20 20 20 20 20 20 20 20 20  WHERE",         
31604 22 49 4e 54 4f 22 2c 20 20 20 20 20 20 20 20 20  "INTO",         
31605 20 22 56 41 4c 55 45 53 22 2c 20 20 20 20 20 20   "VALUES",      
31606 0a 20 20 22 49 4e 53 45 52 54 22 2c 20 20 20 20  .  "INSERT",    
31607 20 20 20 20 22 49 4e 54 45 47 45 52 22 2c 20 20      "INTEGER",  
31608 20 20 20 20 20 22 46 4c 4f 41 54 22 2c 20 20 20       "FLOAT",   
31609 20 20 20 20 20 20 22 42 4c 4f 42 22 2c 20 20 20        "BLOB",   
3160a 20 20 20 20 20 0a 20 20 22 52 45 47 49 53 54 45       .  "REGISTE
3160b 52 22 2c 20 20 20 20 20 20 22 56 41 52 49 41 42  R",      "VARIAB
3160c 4c 45 22 2c 20 20 20 20 20 20 22 43 41 53 45 22  LE",      "CASE"
3160d 2c 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e  ,          "WHEN
3160e 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 54 48  ",        .  "TH
3160f 45 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 45  EN",          "E
31610 4c 53 45 22 2c 20 20 20 20 20 20 20 20 20 20 22  LSE",          "
31611 49 4e 44 45 58 22 2c 20 20 20 20 20 20 20 20 20  INDEX",         
31612 22 41 4c 54 45 52 22 2c 20 20 20 20 20 20 20 0a  "ALTER",       .
31613 20 20 22 41 44 44 22 2c 20 20 20 20 20 20 20 20    "ADD",        
31614 20 20 20 22 65 72 72 6f 72 22 2c 20 20 20 20 20     "error",     
31615 20 20 20 20 22 69 6e 70 75 74 22 2c 20 20 20 20      "input",    
31616 20 20 20 20 20 22 63 6d 64 6c 69 73 74 22 2c 20       "cmdlist", 
31617 20 20 20 20 0a 20 20 22 65 63 6d 64 22 2c 20 20      .  "ecmd",  
31618 20 20 20 20 20 20 20 20 22 65 78 70 6c 61 69 6e          "explain
31619 22 2c 20 20 20 20 20 20 20 22 63 6d 64 78 22 2c  ",       "cmdx",
3161a 20 20 20 20 20 20 20 20 20 20 22 63 6d 64 22 2c            "cmd",
3161b 20 20 20 20 20 20 20 20 20 0a 20 20 22 74 72 61           .  "tra
3161c 6e 73 74 79 70 65 22 2c 20 20 20 20 20 22 74 72  nstype",     "tr
3161d 61 6e 73 5f 6f 70 74 22 2c 20 20 20 20 20 22 6e  ans_opt",     "n
3161e 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  m",            "
3161f 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 22 2c 0a  savepoint_opt",.
31620 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 22    "create_table"
31621 2c 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65  ,  "create_table
31622 5f 61 72 67 73 22 2c 20 20 22 63 72 65 61 74 65  _args",  "create
31623 6b 77 22 2c 20 20 20 20 20 20 22 74 65 6d 70 22  kw",      "temp"
31624 2c 20 20 20 20 20 20 20 20 0a 20 20 22 69 66 6e  ,        .  "ifn
31625 6f 74 65 78 69 73 74 73 22 2c 20 20 20 22 64 62  otexists",   "db
31626 6e 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 63  nm",          "c
31627 6f 6c 75 6d 6e 6c 69 73 74 22 2c 20 20 20 20 22  olumnlist",    "
31628 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 22 2c 0a 20  conslist_opt",. 
31629 20 22 73 65 6c 65 63 74 22 2c 20 20 20 20 20 20   "select",      
3162a 20 20 22 63 6f 6c 75 6d 6e 22 2c 20 20 20 20 20    "column",     
3162b 20 20 20 22 63 6f 6c 75 6d 6e 69 64 22 2c 20 20     "columnid",  
3162c 20 20 20 20 22 74 79 70 65 22 2c 20 20 20 20 20      "type",     
3162d 20 20 20 0a 20 20 22 63 61 72 67 6c 69 73 74 22     .  "carglist"
3162e 2c 20 20 20 20 20 20 22 69 64 22 2c 20 20 20 20  ,      "id",    
3162f 20 20 20 20 20 20 20 20 22 69 64 73 22 2c 20 20          "ids",  
31630 20 20 20 20 20 20 20 20 20 22 74 79 70 65 74 6f           "typeto
31631 6b 65 6e 22 2c 20 20 20 0a 20 20 22 74 79 70 65  ken",   .  "type
31632 6e 61 6d 65 22 2c 20 20 20 20 20 20 22 73 69 67  name",      "sig
31633 6e 65 64 22 2c 20 20 20 20 20 20 20 20 22 70 6c  ned",        "pl
31634 75 73 5f 6e 75 6d 22 2c 20 20 20 20 20 20 22 6d  us_num",      "m
31635 69 6e 75 73 5f 6e 75 6d 22 2c 20 20 20 0a 20 20  inus_num",   .  
31636 22 63 61 72 67 22 2c 20 20 20 20 20 20 20 20 20  "carg",         
31637 20 22 63 63 6f 6e 73 22 2c 20 20 20 20 20 20 20   "ccons",       
31638 20 20 22 74 65 72 6d 22 2c 20 20 20 20 20 20 20    "term",       
31639 20 20 20 22 65 78 70 72 22 2c 20 20 20 20 20 20     "expr",      
3163a 20 20 0a 20 20 22 6f 6e 63 6f 6e 66 22 2c 20 20    .  "onconf",  
3163b 20 20 20 20 20 20 22 73 6f 72 74 6f 72 64 65 72        "sortorder
3163c 22 2c 20 20 20 20 20 22 61 75 74 6f 69 6e 63 22  ",     "autoinc"
3163d 2c 20 20 20 20 20 20 20 22 69 64 78 6c 69 73 74  ,       "idxlist
3163e 5f 6f 70 74 22 2c 20 0a 20 20 22 72 65 66 61 72  _opt", .  "refar
3163f 67 73 22 2c 20 20 20 20 20 20 20 22 64 65 66 65  gs",       "defe
31640 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 20 20 22  r_subclause",  "
31641 72 65 66 61 72 67 22 2c 20 20 20 20 20 20 20 20  refarg",        
31642 22 72 65 66 61 63 74 22 2c 20 20 20 20 20 20 0a  "refact",      .
31643 20 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64    "init_deferred
31644 5f 70 72 65 64 5f 6f 70 74 22 2c 20 20 22 63 6f  _pred_opt",  "co
31645 6e 73 6c 69 73 74 22 2c 20 20 20 20 20 20 22 74  nslist",      "t
31646 63 6f 6e 73 22 2c 20 20 20 20 20 20 20 20 20 22  cons",         "
31647 69 64 78 6c 69 73 74 22 2c 20 20 20 20 20 0a 20  idxlist",     . 
31648 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
31649 65 5f 6f 70 74 22 2c 20 20 22 6f 72 63 6f 6e 66  e_opt",  "orconf
3164a 22 2c 20 20 20 20 20 20 20 20 22 72 65 73 6f 6c  ",        "resol
3164b 76 65 74 79 70 65 22 2c 20 20 20 22 72 61 69 73  vetype",   "rais
3164c 65 74 79 70 65 22 2c 20 20 20 0a 20 20 22 69 66  etype",   .  "if
3164d 65 78 69 73 74 73 22 2c 20 20 20 20 20 20 22 66  exists",      "f
3164e 75 6c 6c 6e 61 6d 65 22 2c 20 20 20 20 20 20 22  ullname",      "
3164f 6f 6e 65 73 65 6c 65 63 74 22 2c 20 20 20 20 20  oneselect",     
31650 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 22  "multiselect_op"
31651 2c 0a 20 20 22 64 69 73 74 69 6e 63 74 22 2c 20  ,.  "distinct", 
31652 20 20 20 20 20 22 73 65 6c 63 6f 6c 6c 69 73 74       "selcollist
31653 22 2c 20 20 20 20 22 66 72 6f 6d 22 2c 20 20 20  ",    "from",   
31654 20 20 20 20 20 20 20 22 77 68 65 72 65 5f 6f 70         "where_op
31655 74 22 2c 20 20 20 0a 20 20 22 67 72 6f 75 70 62  t",   .  "groupb
31656 79 5f 6f 70 74 22 2c 20 20 20 22 68 61 76 69 6e  y_opt",   "havin
31657 67 5f 6f 70 74 22 2c 20 20 20 20 22 6f 72 64 65  g_opt",    "orde
31658 72 62 79 5f 6f 70 74 22 2c 20 20 20 22 6c 69 6d  rby_opt",   "lim
31659 69 74 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 73  it_opt",   .  "s
3165a 63 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20 22  clp",          "
3165b 61 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  as",            
3165c 22 73 65 6c 74 61 62 6c 69 73 74 22 2c 20 20 20  "seltablist",   
3165d 20 22 73 74 6c 5f 70 72 65 66 69 78 22 2c 20 20   "stl_prefix",  
3165e 0a 20 20 22 6a 6f 69 6e 6f 70 22 2c 20 20 20 20  .  "joinop",    
3165f 20 20 20 20 22 69 6e 64 65 78 65 64 5f 6f 70 74      "indexed_opt
31660 22 2c 20 20 20 22 6f 6e 5f 6f 70 74 22 2c 20 20  ",   "on_opt",  
31661 20 20 20 20 20 20 22 75 73 69 6e 67 5f 6f 70 74        "using_opt
31662 22 2c 20 20 20 0a 20 20 22 6a 6f 69 6e 6f 70 32  ",   .  "joinop2
31663 22 2c 20 20 20 20 20 20 20 22 69 6e 73 63 6f 6c  ",       "inscol
31664 6c 69 73 74 22 2c 20 20 20 20 22 73 6f 72 74 6c  list",    "sortl
31665 69 73 74 22 2c 20 20 20 20 20 20 22 73 6f 72 74  ist",      "sort
31666 69 74 65 6d 22 2c 20 20 20 20 0a 20 20 22 6e 65  item",    .  "ne
31667 78 70 72 6c 69 73 74 22 2c 20 20 20 20 20 22 73  xprlist",     "s
31668 65 74 6c 69 73 74 22 2c 20 20 20 20 20 20 20 22  etlist",       "
31669 69 6e 73 65 72 74 5f 63 6d 64 22 2c 20 20 20 20  insert_cmd",    
3166a 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 22  "inscollist_opt"
3166b 2c 0a 20 20 22 69 74 65 6d 6c 69 73 74 22 2c 20  ,.  "itemlist", 
3166c 20 20 20 20 20 22 65 78 70 72 6c 69 73 74 22 2c       "exprlist",
3166d 20 20 20 20 20 20 22 6c 69 6b 65 6f 70 22 2c 20        "likeop", 
3166e 20 20 20 20 20 20 20 22 65 73 63 61 70 65 22 2c         "escape",
3166f 20 20 20 20 20 20 0a 20 20 22 62 65 74 77 65 65        .  "betwee
31670 6e 5f 6f 70 22 2c 20 20 20 20 22 69 6e 5f 6f 70  n_op",    "in_op
31671 22 2c 20 20 20 20 20 20 20 20 20 22 63 61 73 65  ",         "case
31672 5f 6f 70 65 72 61 6e 64 22 2c 20 20 22 63 61 73  _operand",  "cas
31673 65 5f 65 78 70 72 6c 69 73 74 22 2c 0a 20 20 22  e_exprlist",.  "
31674 63 61 73 65 5f 65 6c 73 65 22 2c 20 20 20 20 20  case_else",     
31675 22 75 6e 69 71 75 65 66 6c 61 67 22 2c 20 20 20  "uniqueflag",   
31676 20 22 63 6f 6c 6c 61 74 65 22 2c 20 20 20 20 20   "collate",     
31677 20 20 22 6e 6d 6e 75 6d 22 2c 20 20 20 20 20 20    "nmnum",      
31678 20 0a 20 20 22 70 6c 75 73 5f 6f 70 74 22 2c 20   .  "plus_opt", 
31679 20 20 20 20 20 22 6e 75 6d 62 65 72 22 2c 20 20       "number",  
3167a 20 20 20 20 20 20 22 74 72 69 67 67 65 72 5f 64        "trigger_d
3167b 65 63 6c 22 2c 20 20 22 74 72 69 67 67 65 72 5f  ecl",  "trigger_
3167c 63 6d 64 5f 6c 69 73 74 22 2c 0a 20 20 22 74 72  cmd_list",.  "tr
3167d 69 67 67 65 72 5f 74 69 6d 65 22 2c 20 20 22 74  igger_time",  "t
3167e 72 69 67 67 65 72 5f 65 76 65 6e 74 22 2c 20 20  rigger_event",  
3167f 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 22  "foreach_clause"
31680 2c 20 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 22  ,  "when_clause"
31681 2c 20 0a 20 20 22 74 72 69 67 67 65 72 5f 63 6d  , .  "trigger_cm
31682 64 22 2c 20 20 20 22 74 72 6e 6d 22 2c 20 20 20  d",   "trnm",   
31683 20 20 20 20 20 20 20 22 74 72 69 64 78 62 79 22         "tridxby"
31684 2c 20 20 20 20 20 20 20 22 64 61 74 61 62 61 73  ,       "databas
31685 65 5f 6b 77 5f 6f 70 74 22 2c 0a 20 20 22 6b 65  e_kw_opt",.  "ke
31686 79 5f 6f 70 74 22 2c 20 20 20 20 20 20 20 22 61  y_opt",       "a
31687 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
31688 6d 65 22 2c 20 20 22 6b 77 63 6f 6c 75 6d 6e 5f  me",  "kwcolumn_
31689 6f 70 74 22 2c 20 20 22 63 72 65 61 74 65 5f 76  opt",  "create_v
3168a 74 61 62 22 2c 20 0a 20 20 22 76 74 61 62 61 72  tab", .  "vtabar
3168b 67 6c 69 73 74 22 2c 20 20 20 22 76 74 61 62 61  glist",   "vtaba
3168c 72 67 22 2c 20 20 20 20 20 20 20 22 76 74 61 62  rg",       "vtab
3168d 61 72 67 74 6f 6b 65 6e 22 2c 20 20 22 6c 70 22  argtoken",  "lp"
3168e 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 61  ,          .  "a
3168f 6e 79 6c 69 73 74 22 2c 20 20 20 20 20 0a 7d 3b  nylist",     .};
31690 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55  .#endif /* NDEBU
31691 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44  G */..#ifndef ND
31692 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63  EBUG./* For trac
31693 69 6e 67 20 72 65 64 75 63 65 20 61 63 74 69 6f  ing reduce actio
31694 6e 73 2c 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ns, the names of
31695 20 61 6c 6c 20 72 75 6c 65 73 20 61 72 65 20 72   all rules are r
31696 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  equired..*/.stat
31697 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
31698 6f 6e 73 74 20 79 79 52 75 6c 65 4e 61 6d 65 5b  onst yyRuleName[
31699 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 30 20 2a 2f  ] = {. /*   0 */
3169a 20 22 69 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c   "input ::= cmdl
3169b 69 73 74 22 2c 0a 20 2f 2a 20 20 20 31 20 2a 2f  ist",. /*   1 */
3169c 20 22 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d   "cmdlist ::= cm
3169d 64 6c 69 73 74 20 65 63 6d 64 22 2c 0a 20 2f 2a  dlist ecmd",. /*
3169e 20 20 20 32 20 2a 2f 20 22 63 6d 64 6c 69 73 74     2 */ "cmdlist
3169f 20 3a 3a 3d 20 65 63 6d 64 22 2c 0a 20 2f 2a 20   ::= ecmd",. /* 
316a0 20 20 33 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d    3 */ "ecmd ::=
316a1 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 34 20   SEMI",. /*   4 
316a2 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20 65 78 70  */ "ecmd ::= exp
316a3 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d 49 22 2c  lain cmdx SEMI",
316a4 0a 20 2f 2a 20 20 20 35 20 2a 2f 20 22 65 78 70  . /*   5 */ "exp
316a5 6c 61 69 6e 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  lain ::=",. /*  
316a6 20 36 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a   6 */ "explain :
316a7 3a 3d 20 45 58 50 4c 41 49 4e 22 2c 0a 20 2f 2a  := EXPLAIN",. /*
316a8 20 20 20 37 20 2a 2f 20 22 65 78 70 6c 61 69 6e     7 */ "explain
316a9 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 51 55 45   ::= EXPLAIN QUE
316aa 52 59 20 50 4c 41 4e 22 2c 0a 20 2f 2a 20 20 20  RY PLAN",. /*   
316ab 38 20 2a 2f 20 22 63 6d 64 78 20 3a 3a 3d 20 63  8 */ "cmdx ::= c
316ac 6d 64 22 2c 0a 20 2f 2a 20 20 20 39 20 2a 2f 20  md",. /*   9 */ 
316ad 22 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74  "cmd ::= BEGIN t
316ae 72 61 6e 73 74 79 70 65 20 74 72 61 6e 73 5f 6f  ranstype trans_o
316af 70 74 22 2c 0a 20 2f 2a 20 20 31 30 20 2a 2f 20  pt",. /*  10 */ 
316b0 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 22 2c  "trans_opt ::=",
316b1 0a 20 2f 2a 20 20 31 31 20 2a 2f 20 22 74 72 61  . /*  11 */ "tra
316b2 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53  ns_opt ::= TRANS
316b3 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20 20 31 32  ACTION",. /*  12
316b4 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a   */ "trans_opt :
316b5 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e  := TRANSACTION n
316b6 6d 22 2c 0a 20 2f 2a 20 20 31 33 20 2a 2f 20 22  m",. /*  13 */ "
316b7 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 22 2c 0a  transtype ::=",.
316b8 20 2f 2a 20 20 31 34 20 2a 2f 20 22 74 72 61 6e   /*  14 */ "tran
316b9 73 74 79 70 65 20 3a 3a 3d 20 44 45 46 45 52 52  stype ::= DEFERR
316ba 45 44 22 2c 0a 20 2f 2a 20 20 31 35 20 2a 2f 20  ED",. /*  15 */ 
316bb 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49  "transtype ::= I
316bc 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20  MMEDIATE",. /*  
316bd 31 36 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65  16 */ "transtype
316be 20 3a 3a 3d 20 45 58 43 4c 55 53 49 56 45 22 2c   ::= EXCLUSIVE",
316bf 0a 20 2f 2a 20 20 31 37 20 2a 2f 20 22 63 6d 64  . /*  17 */ "cmd
316c0 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e   ::= COMMIT tran
316c1 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 38 20  s_opt",. /*  18 
316c2 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 45 4e 44 20  */ "cmd ::= END 
316c3 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20  trans_opt",. /* 
316c4 20 31 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20   19 */ "cmd ::= 
316c5 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f  ROLLBACK trans_o
316c6 70 74 22 2c 0a 20 2f 2a 20 20 32 30 20 2a 2f 20  pt",. /*  20 */ 
316c7 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a  "savepoint_opt :
316c8 3a 3d 20 53 41 56 45 50 4f 49 4e 54 22 2c 0a 20  := SAVEPOINT",. 
316c9 2f 2a 20 20 32 31 20 2a 2f 20 22 73 61 76 65 70  /*  21 */ "savep
316ca 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  oint_opt ::=",. 
316cb 2f 2a 20 20 32 32 20 2a 2f 20 22 63 6d 64 20 3a  /*  22 */ "cmd :
316cc 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 6e 6d 22  := SAVEPOINT nm"
316cd 2c 0a 20 2f 2a 20 20 32 33 20 2a 2f 20 22 63 6d  ,. /*  23 */ "cm
316ce 64 20 3a 3a 3d 20 52 45 4c 45 41 53 45 20 73 61  d ::= RELEASE sa
316cf 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c  vepoint_opt nm",
316d0 0a 20 2f 2a 20 20 32 34 20 2a 2f 20 22 63 6d 64  . /*  24 */ "cmd
316d1 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72   ::= ROLLBACK tr
316d2 61 6e 73 5f 6f 70 74 20 54 4f 20 73 61 76 65 70  ans_opt TO savep
316d3 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c 0a 20 2f  oint_opt nm",. /
316d4 2a 20 20 32 35 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  25 */ "cmd ::
316d5 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 63  = create_table c
316d6 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73  reate_table_args
316d7 22 2c 0a 20 2f 2a 20 20 32 36 20 2a 2f 20 22 63  ",. /*  26 */ "c
316d8 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20  reate_table ::= 
316d9 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 54 41  createkw temp TA
316da 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74 73 20  BLE ifnotexists 
316db 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 20 32  nm dbnm",. /*  2
316dc 37 20 2a 2f 20 22 63 72 65 61 74 65 6b 77 20 3a  7 */ "createkw :
316dd 3a 3d 20 43 52 45 41 54 45 22 2c 0a 20 2f 2a 20  := CREATE",. /* 
316de 20 32 38 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69   28 */ "ifnotexi
316df 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32  sts ::=",. /*  2
316e0 39 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73 74  9 */ "ifnotexist
316e1 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58 49  s ::= IF NOT EXI
316e2 53 54 53 22 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f  STS",. /*  30 */
316e3 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 22   "temp ::= TEMP"
316e4 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f 20 22 74 65  ,. /*  31 */ "te
316e5 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 33 32  mp ::=",. /*  32
316e6 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c   */ "create_tabl
316e7 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f  e_args ::= LP co
316e8 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73  lumnlist conslis
316e9 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f 2a 20 20  t_opt RP",. /*  
316ea 33 33 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61  33 */ "create_ta
316eb 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20  ble_args ::= AS 
316ec 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 20 33 34  select",. /*  34
316ed 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20   */ "columnlist 
316ee 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43  ::= columnlist C
316ef 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f  OMMA column",. /
316f0 2a 20 20 33 35 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  35 */ "column
316f1 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 22  list ::= column"
316f2 2c 0a 20 2f 2a 20 20 33 36 20 2a 2f 20 22 63 6f  ,. /*  36 */ "co
316f3 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69  lumn ::= columni
316f4 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74 22  d type carglist"
316f5 2c 0a 20 2f 2a 20 20 33 37 20 2a 2f 20 22 63 6f  ,. /*  37 */ "co
316f6 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0a  lumnid ::= nm",.
316f7 20 2f 2a 20 20 33 38 20 2a 2f 20 22 69 64 20 3a   /*  38 */ "id :
316f8 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20 33 39 20  := ID",. /*  39 
316f9 2a 2f 20 22 69 64 20 3a 3a 3d 20 49 4e 44 45 58  */ "id ::= INDEX
316fa 45 44 22 2c 0a 20 2f 2a 20 20 34 30 20 2a 2f 20  ED",. /*  40 */ 
316fb 22 69 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49  "ids ::= ID|STRI
316fc 4e 47 22 2c 0a 20 2f 2a 20 20 34 31 20 2a 2f 20  NG",. /*  41 */ 
316fd 22 6e 6d 20 3a 3a 3d 20 69 64 22 2c 0a 20 2f 2a  "nm ::= id",. /*
316fe 20 20 34 32 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20    42 */ "nm ::= 
316ff 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 34 33  STRING",. /*  43
31700 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e   */ "nm ::= JOIN
31701 5f 4b 57 22 2c 0a 20 2f 2a 20 20 34 34 20 2a 2f  _KW",. /*  44 */
31702 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a   "type ::=",. /*
31703 20 20 34 35 20 2a 2f 20 22 74 79 70 65 20 3a 3a    45 */ "type ::
31704 3d 20 74 79 70 65 74 6f 6b 65 6e 22 2c 0a 20 2f  = typetoken",. /
31705 2a 20 20 34 36 20 2a 2f 20 22 74 79 70 65 74 6f  *  46 */ "typeto
31706 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65  ken ::= typename
31707 22 2c 0a 20 2f 2a 20 20 34 37 20 2a 2f 20 22 74  ",. /*  47 */ "t
31708 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
31709 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20  ename LP signed 
3170a 52 50 22 2c 0a 20 2f 2a 20 20 34 38 20 2a 2f 20  RP",. /*  48 */ 
3170b 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74  "typetoken ::= t
3170c 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65  ypename LP signe
3170d 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52  d COMMA signed R
3170e 50 22 2c 0a 20 2f 2a 20 20 34 39 20 2a 2f 20 22  P",. /*  49 */ "
3170f 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73  typename ::= ids
31710 22 2c 0a 20 2f 2a 20 20 35 30 20 2a 2f 20 22 74  ",. /*  50 */ "t
31711 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65  ypename ::= type
31712 6e 61 6d 65 20 69 64 73 22 2c 0a 20 2f 2a 20 20  name ids",. /*  
31713 35 31 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a  51 */ "signed ::
31714 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a  = plus_num",. /*
31715 20 20 35 32 20 2a 2f 20 22 73 69 67 6e 65 64 20    52 */ "signed 
31716 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a  ::= minus_num",.
31717 20 2f 2a 20 20 35 33 20 2a 2f 20 22 63 61 72 67   /*  53 */ "carg
31718 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73  list ::= carglis
31719 74 20 63 61 72 67 22 2c 0a 20 2f 2a 20 20 35 34  t carg",. /*  54
3171a 20 2a 2f 20 22 63 61 72 67 6c 69 73 74 20 3a 3a   */ "carglist ::
3171b 3d 22 2c 0a 20 2f 2a 20 20 35 35 20 2a 2f 20 22  =",. /*  55 */ "
3171c 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41  carg ::= CONSTRA
3171d 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 22 2c 0a 20  INT nm ccons",. 
3171e 2f 2a 20 20 35 36 20 2a 2f 20 22 63 61 72 67 20  /*  56 */ "carg 
3171f 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20 2f 2a 20  ::= ccons",. /* 
31720 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   57 */ "ccons ::
31721 3d 20 44 45 46 41 55 4c 54 20 74 65 72 6d 22 2c  = DEFAULT term",
31722 0a 20 2f 2a 20 20 35 38 20 2a 2f 20 22 63 63 6f  . /*  58 */ "cco
31723 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4c  ns ::= DEFAULT L
31724 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20  P expr RP",. /* 
31725 20 35 39 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   59 */ "ccons ::
31726 3d 20 44 45 46 41 55 4c 54 20 50 4c 55 53 20 74  = DEFAULT PLUS t
31727 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 30 20 2a 2f  erm",. /*  60 */
31728 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41   "ccons ::= DEFA
31729 55 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d 22 2c  ULT MINUS term",
3172a 0a 20 2f 2a 20 20 36 31 20 2a 2f 20 22 63 63 6f  . /*  61 */ "cco
3172b 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69  ns ::= DEFAULT i
3172c 64 22 2c 0a 20 2f 2a 20 20 36 32 20 2a 2f 20 22  d",. /*  62 */ "
3172d 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20 6f  ccons ::= NULL o
3172e 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 33 20  nconf",. /*  63 
3172f 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f  */ "ccons ::= NO
31730 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a  T NULL onconf",.
31731 20 2f 2a 20 20 36 34 20 2a 2f 20 22 63 63 6f 6e   /*  64 */ "ccon
31732 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45  s ::= PRIMARY KE
31733 59 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f  Y sortorder onco
31734 6e 66 20 61 75 74 6f 69 6e 63 22 2c 0a 20 2f 2a  nf autoinc",. /*
31735 20 20 36 35 20 2a 2f 20 22 63 63 6f 6e 73 20 3a    65 */ "ccons :
31736 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63 6f 6e 66  := UNIQUE onconf
31737 22 2c 0a 20 2f 2a 20 20 36 36 20 2a 2f 20 22 63  ",. /*  66 */ "c
31738 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c  cons ::= CHECK L
31739 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20  P expr RP",. /* 
3173a 20 36 37 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   67 */ "ccons ::
3173b 3d 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20  = REFERENCES nm 
3173c 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61  idxlist_opt refa
3173d 72 67 73 22 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f  rgs",. /*  68 */
3173e 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65   "ccons ::= defe
3173f 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a 20 2f  r_subclause",. /
31740 2a 20 20 36 39 20 2a 2f 20 22 63 63 6f 6e 73 20  *  69 */ "ccons 
31741 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 22  ::= COLLATE ids"
31742 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f 20 22 61 75  ,. /*  70 */ "au
31743 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  toinc ::=",. /* 
31744 20 37 31 20 2a 2f 20 22 61 75 74 6f 69 6e 63 20   71 */ "autoinc 
31745 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 22 2c 0a 20  ::= AUTOINCR",. 
31746 2f 2a 20 20 37 32 20 2a 2f 20 22 72 65 66 61 72  /*  72 */ "refar
31747 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 37 33  gs ::=",. /*  73
31748 20 2a 2f 20 22 72 65 66 61 72 67 73 20 3a 3a 3d   */ "refargs ::=
31749 20 72 65 66 61 72 67 73 20 72 65 66 61 72 67 22   refargs refarg"
3174a 2c 0a 20 2f 2a 20 20 37 34 20 2a 2f 20 22 72 65  ,. /*  74 */ "re
3174b 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e  farg ::= MATCH n
3174c 6d 22 2c 0a 20 2f 2a 20 20 37 35 20 2a 2f 20 22  m",. /*  75 */ "
3174d 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45  refarg ::= ON DE
3174e 4c 45 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f  LETE refact",. /
3174f 2a 20 20 37 36 20 2a 2f 20 22 72 65 66 61 72 67  *  76 */ "refarg
31750 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72   ::= ON UPDATE r
31751 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37 37 20  efact",. /*  77 
31752 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53  */ "refact ::= S
31753 45 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 20 37  ET NULL",. /*  7
31754 38 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d  8 */ "refact ::=
31755 20 53 45 54 20 44 45 46 41 55 4c 54 22 2c 0a 20   SET DEFAULT",. 
31756 2f 2a 20 20 37 39 20 2a 2f 20 22 72 65 66 61 63  /*  79 */ "refac
31757 74 20 3a 3a 3d 20 43 41 53 43 41 44 45 22 2c 0a  t ::= CASCADE",.
31758 20 2f 2a 20 20 38 30 20 2a 2f 20 22 72 65 66 61   /*  80 */ "refa
31759 63 74 20 3a 3a 3d 20 52 45 53 54 52 49 43 54 22  ct ::= RESTRICT"
3175a 2c 0a 20 2f 2a 20 20 38 31 20 2a 2f 20 22 72 65  ,. /*  81 */ "re
3175b 66 61 63 74 20 3a 3a 3d 20 4e 4f 20 41 43 54 49  fact ::= NO ACTI
3175c 4f 4e 22 2c 0a 20 2f 2a 20 20 38 32 20 2a 2f 20  ON",. /*  82 */ 
3175d 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65  "defer_subclause
3175e 20 3a 3a 3d 20 4e 4f 54 20 44 45 46 45 52 52 41   ::= NOT DEFERRA
3175f 42 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65  BLE init_deferre
31760 64 5f 70 72 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a  d_pred_opt",. /*
31761 20 20 38 33 20 2a 2f 20 22 64 65 66 65 72 5f 73    83 */ "defer_s
31762 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 44 45 46  ubclause ::= DEF
31763 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66  ERRABLE init_def
31764 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c  erred_pred_opt",
31765 0a 20 2f 2a 20 20 38 34 20 2a 2f 20 22 69 6e 69  . /*  84 */ "ini
31766 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
31767 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38  opt ::=",. /*  8
31768 35 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65 72  5 */ "init_defer
31769 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d  red_pred_opt ::=
3176a 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
3176b 52 45 44 22 2c 0a 20 2f 2a 20 20 38 36 20 2a 2f  RED",. /*  86 */
3176c 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f   "init_deferred_
3176d 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49  pred_opt ::= INI
3176e 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45  TIALLY IMMEDIATE
3176f 22 2c 0a 20 2f 2a 20 20 38 37 20 2a 2f 20 22 63  ",. /*  87 */ "c
31770 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 22  onslist_opt ::="
31771 2c 0a 20 2f 2a 20 20 38 38 20 2a 2f 20 22 63 6f  ,. /*  88 */ "co
31772 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 43  nslist_opt ::= C
31773 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73 74 22 2c 0a  OMMA conslist",.
31774 20 2f 2a 20 20 38 39 20 2a 2f 20 22 63 6f 6e 73   /*  89 */ "cons
31775 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73  list ::= conslis
31776 74 20 43 4f 4d 4d 41 20 74 63 6f 6e 73 22 2c 0a  t COMMA tcons",.
31777 20 2f 2a 20 20 39 30 20 2a 2f 20 22 63 6f 6e 73   /*  90 */ "cons
31778 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73  list ::= conslis
31779 74 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39  t tcons",. /*  9
3177a 31 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a  1 */ "conslist :
3177b 3a 3d 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20  := tcons",. /*  
3177c 39 32 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  92 */ "tcons ::=
3177d 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 22 2c   CONSTRAINT nm",
3177e 0a 20 2f 2a 20 20 39 33 20 2a 2f 20 22 74 63 6f  . /*  93 */ "tco
3177f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b  ns ::= PRIMARY K
31780 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 61 75  EY LP idxlist au
31781 74 6f 69 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 22  toinc RP onconf"
31782 2c 0a 20 2f 2a 20 20 39 34 20 2a 2f 20 22 74 63  ,. /*  94 */ "tc
31783 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 4c  ons ::= UNIQUE L
31784 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f 6e 63  P idxlist RP onc
31785 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 35 20 2a 2f  onf",. /*  95 */
31786 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43   "tcons ::= CHEC
31787 4b 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e 63  K LP expr RP onc
31788 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 36 20 2a 2f  onf",. /*  96 */
31789 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 46 4f 52 45   "tcons ::= FORE
3178a 49 47 4e 20 4b 45 59 20 4c 50 20 69 64 78 6c 69  IGN KEY LP idxli
3178b 73 74 20 52 50 20 52 45 46 45 52 45 4e 43 45 53  st RP REFERENCES
3178c 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
3178d 72 65 66 61 72 67 73 20 64 65 66 65 72 5f 73 75  refargs defer_su
3178e 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c 0a 20 2f  bclause_opt",. /
3178f 2a 20 20 39 37 20 2a 2f 20 22 64 65 66 65 72 5f  *  97 */ "defer_
31790 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a  subclause_opt ::
31791 3d 22 2c 0a 20 2f 2a 20 20 39 38 20 2a 2f 20 22  =",. /*  98 */ "
31792 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
31793 6f 70 74 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75  opt ::= defer_su
31794 62 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 20 39  bclause",. /*  9
31795 39 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d  9 */ "onconf ::=
31796 22 2c 0a 20 2f 2a 20 31 30 30 20 2a 2f 20 22 6f  ",. /* 100 */ "o
31797 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e  nconf ::= ON CON
31798 46 4c 49 43 54 20 72 65 73 6f 6c 76 65 74 79 70  FLICT resolvetyp
31799 65 22 2c 0a 20 2f 2a 20 31 30 31 20 2a 2f 20 22  e",. /* 101 */ "
3179a 6f 72 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a  orconf ::=",. /*
3179b 20 31 30 32 20 2a 2f 20 22 6f 72 63 6f 6e 66 20   102 */ "orconf 
3179c 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74 79  ::= OR resolvety
3179d 70 65 22 2c 0a 20 2f 2a 20 31 30 33 20 2a 2f 20  pe",. /* 103 */ 
3179e 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d  "resolvetype ::=
3179f 20 72 61 69 73 65 74 79 70 65 22 2c 0a 20 2f 2a   raisetype",. /*
317a0 20 31 30 34 20 2a 2f 20 22 72 65 73 6f 6c 76 65   104 */ "resolve
317a1 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 22  type ::= IGNORE"
317a2 2c 0a 20 2f 2a 20 31 30 35 20 2a 2f 20 22 72 65  ,. /* 105 */ "re
317a3 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 52 45  solvetype ::= RE
317a4 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 30 36 20  PLACE",. /* 106 
317a5 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50  */ "cmd ::= DROP
317a6 20 54 41 42 4c 45 20 69 66 65 78 69 73 74 73 20   TABLE ifexists 
317a7 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31  fullname",. /* 1
317a8 30 37 20 2a 2f 20 22 69 66 65 78 69 73 74 73 20  07 */ "ifexists 
317a9 3a 3a 3d 20 49 46 20 45 58 49 53 54 53 22 2c 0a  ::= IF EXISTS",.
317aa 20 2f 2a 20 31 30 38 20 2a 2f 20 22 69 66 65 78   /* 108 */ "ifex
317ab 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  ists ::=",. /* 1
317ac 30 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63  09 */ "cmd ::= c
317ad 72 65 61 74 65 6b 77 20 74 65 6d 70 20 56 49 45  reatekw temp VIE
317ae 57 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  W ifnotexists nm
317af 20 64 62 6e 6d 20 41 53 20 73 65 6c 65 63 74 22   dbnm AS select"
317b0 2c 0a 20 2f 2a 20 31 31 30 20 2a 2f 20 22 63 6d  ,. /* 110 */ "cm
317b1 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57 20  d ::= DROP VIEW 
317b2 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
317b3 65 22 2c 0a 20 2f 2a 20 31 31 31 20 2a 2f 20 22  e",. /* 111 */ "
317b4 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c  cmd ::= select",
317b5 0a 20 2f 2a 20 31 31 32 20 2a 2f 20 22 73 65 6c  . /* 112 */ "sel
317b6 65 63 74 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63  ect ::= oneselec
317b7 74 22 2c 0a 20 2f 2a 20 31 31 33 20 2a 2f 20 22  t",. /* 113 */ "
317b8 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65 6c 65 63  select ::= selec
317b9 74 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70  t multiselect_op
317ba 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a   oneselect",. /*
317bb 20 31 31 34 20 2a 2f 20 22 6d 75 6c 74 69 73 65   114 */ "multise
317bc 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f  lect_op ::= UNIO
317bd 4e 22 2c 0a 20 2f 2a 20 31 31 35 20 2a 2f 20 22  N",. /* 115 */ "
317be 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
317bf 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c 22 2c 0a 20  := UNION ALL",. 
317c0 2f 2a 20 31 31 36 20 2a 2f 20 22 6d 75 6c 74 69  /* 116 */ "multi
317c1 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 45 58  select_op ::= EX
317c2 43 45 50 54 7c 49 4e 54 45 52 53 45 43 54 22 2c  CEPT|INTERSECT",
317c3 0a 20 2f 2a 20 31 31 37 20 2a 2f 20 22 6f 6e 65  . /* 117 */ "one
317c4 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45 4c 45 43  select ::= SELEC
317c5 54 20 64 69 73 74 69 6e 63 74 20 73 65 6c 63 6f  T distinct selco
317c6 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68 65 72 65  llist from where
317c7 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f 6f 70 74  _opt groupby_opt
317c8 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f 72 64 65   having_opt orde
317c9 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70  rby_opt limit_op
317ca 74 22 2c 0a 20 2f 2a 20 31 31 38 20 2a 2f 20 22  t",. /* 118 */ "
317cb 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 44 49 53  distinct ::= DIS
317cc 54 49 4e 43 54 22 2c 0a 20 2f 2a 20 31 31 39 20  TINCT",. /* 119 
317cd 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d  */ "distinct ::=
317ce 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 32 30 20 2a   ALL",. /* 120 *
317cf 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d 22  / "distinct ::="
317d0 2c 0a 20 2f 2a 20 31 32 31 20 2a 2f 20 22 73 63  ,. /* 121 */ "sc
317d1 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73  lp ::= selcollis
317d2 74 20 43 4f 4d 4d 41 22 2c 0a 20 2f 2a 20 31 32  t COMMA",. /* 12
317d3 32 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 22 2c  2 */ "sclp ::=",
317d4 0a 20 2f 2a 20 31 32 33 20 2a 2f 20 22 73 65 6c  . /* 123 */ "sel
317d5 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70  collist ::= sclp
317d6 20 65 78 70 72 20 61 73 22 2c 0a 20 2f 2a 20 31   expr as",. /* 1
317d7 32 34 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73  24 */ "selcollis
317d8 74 20 3a 3a 3d 20 73 63 6c 70 20 53 54 41 52 22  t ::= sclp STAR"
317d9 2c 0a 20 2f 2a 20 31 32 35 20 2a 2f 20 22 73 65  ,. /* 125 */ "se
317da 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
317db 70 20 6e 6d 20 44 4f 54 20 53 54 41 52 22 2c 0a  p nm DOT STAR",.
317dc 20 2f 2a 20 31 32 36 20 2a 2f 20 22 61 73 20 3a   /* 126 */ "as :
317dd 3a 3d 20 41 53 20 6e 6d 22 2c 0a 20 2f 2a 20 31  := AS nm",. /* 1
317de 32 37 20 2a 2f 20 22 61 73 20 3a 3a 3d 20 69 64  27 */ "as ::= id
317df 73 22 2c 0a 20 2f 2a 20 31 32 38 20 2a 2f 20 22  s",. /* 128 */ "
317e0 61 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 39  as ::=",. /* 129
317e1 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d 22 2c 0a   */ "from ::=",.
317e2 20 2f 2a 20 31 33 30 20 2a 2f 20 22 66 72 6f 6d   /* 130 */ "from
317e3 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c 74 61 62   ::= FROM seltab
317e4 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 33 31 20 2a  list",. /* 131 *
317e5 2f 20 22 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a  / "stl_prefix ::
317e6 3d 20 73 65 6c 74 61 62 6c 69 73 74 20 6a 6f 69  = seltablist joi
317e7 6e 6f 70 22 2c 0a 20 2f 2a 20 31 33 32 20 2a 2f  nop",. /* 132 */
317e8 20 22 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d   "stl_prefix ::=
317e9 22 2c 0a 20 2f 2a 20 31 33 33 20 2a 2f 20 22 73  ",. /* 133 */ "s
317ea 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74  eltablist ::= st
317eb 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64 62 6e 6d  l_prefix nm dbnm
317ec 20 61 73 20 69 6e 64 65 78 65 64 5f 6f 70 74 20   as indexed_opt 
317ed 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74  on_opt using_opt
317ee 22 2c 0a 20 2f 2a 20 31 33 34 20 2a 2f 20 22 73  ",. /* 134 */ "s
317ef 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74  eltablist ::= st
317f0 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c 65  l_prefix LP sele
317f1 63 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20  ct RP as on_opt 
317f2 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20  using_opt",. /* 
317f3 31 33 35 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69  135 */ "seltabli
317f4 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69  st ::= stl_prefi
317f5 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73 74 20  x LP seltablist 
317f6 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69  RP as on_opt usi
317f7 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 36  ng_opt",. /* 136
317f8 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 22 2c 0a   */ "dbnm ::=",.
317f9 20 2f 2a 20 31 33 37 20 2a 2f 20 22 64 62 6e 6d   /* 137 */ "dbnm
317fa 20 3a 3a 3d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f   ::= DOT nm",. /
317fb 2a 20 31 33 38 20 2a 2f 20 22 66 75 6c 6c 6e 61  * 138 */ "fullna
317fc 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 22 2c  me ::= nm dbnm",
317fd 0a 20 2f 2a 20 31 33 39 20 2a 2f 20 22 6a 6f 69  . /* 139 */ "joi
317fe 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f  nop ::= COMMA|JO
317ff 49 4e 22 2c 0a 20 2f 2a 20 31 34 30 20 2a 2f 20  IN",. /* 140 */ 
31800 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e  "joinop ::= JOIN
31801 5f 4b 57 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31  _KW JOIN",. /* 1
31802 34 31 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a  41 */ "joinop ::
31803 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49  = JOIN_KW nm JOI
31804 4e 22 2c 0a 20 2f 2a 20 31 34 32 20 2a 2f 20 22  N",. /* 142 */ "
31805 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
31806 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 22 2c 0a  KW nm nm JOIN",.
31807 20 2f 2a 20 31 34 33 20 2a 2f 20 22 6f 6e 5f 6f   /* 143 */ "on_o
31808 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 22 2c  pt ::= ON expr",
31809 0a 20 2f 2a 20 31 34 34 20 2a 2f 20 22 6f 6e 5f  . /* 144 */ "on_
3180a 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34  opt ::=",. /* 14
3180b 35 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70  5 */ "indexed_op
3180c 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 36 20  t ::=",. /* 146 
3180d 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 20  */ "indexed_opt 
3180e 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e  ::= INDEXED BY n
3180f 6d 22 2c 0a 20 2f 2a 20 31 34 37 20 2a 2f 20 22  m",. /* 147 */ "
31810 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20  indexed_opt ::= 
31811 4e 4f 54 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f  NOT INDEXED",. /
31812 2a 20 31 34 38 20 2a 2f 20 22 75 73 69 6e 67 5f  * 148 */ "using_
31813 6f 70 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50  opt ::= USING LP
31814 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 22 2c   inscollist RP",
31815 0a 20 2f 2a 20 31 34 39 20 2a 2f 20 22 75 73 69  . /* 149 */ "usi
31816 6e 67 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  ng_opt ::=",. /*
31817 20 31 35 30 20 2a 2f 20 22 6f 72 64 65 72 62 79   150 */ "orderby
31818 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  _opt ::=",. /* 1
31819 35 31 20 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f  51 */ "orderby_o
3181a 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20  pt ::= ORDER BY 
3181b 73 6f 72 74 6c 69 73 74 22 2c 0a 20 2f 2a 20 31  sortlist",. /* 1
3181c 35 32 20 2a 2f 20 22 73 6f 72 74 6c 69 73 74 20  52 */ "sortlist 
3181d 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d  ::= sortlist COM
3181e 4d 41 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74  MA sortitem sort
3181f 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 31 35 33 20  order",. /* 153 
31820 2a 2f 20 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d  */ "sortlist ::=
31821 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72   sortitem sortor
31822 64 65 72 22 2c 0a 20 2f 2a 20 31 35 34 20 2a 2f  der",. /* 154 */
31823 20 22 73 6f 72 74 69 74 65 6d 20 3a 3a 3d 20 65   "sortitem ::= e
31824 78 70 72 22 2c 0a 20 2f 2a 20 31 35 35 20 2a 2f  xpr",. /* 155 */
31825 20 22 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20   "sortorder ::= 
31826 41 53 43 22 2c 0a 20 2f 2a 20 31 35 36 20 2a 2f  ASC",. /* 156 */
31827 20 22 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20   "sortorder ::= 
31828 44 45 53 43 22 2c 0a 20 2f 2a 20 31 35 37 20 2a  DESC",. /* 157 *
31829 2f 20 22 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d  / "sortorder ::=
3182a 22 2c 0a 20 2f 2a 20 31 35 38 20 2a 2f 20 22 67  ",. /* 158 */ "g
3182b 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c  roupby_opt ::=",
3182c 0a 20 2f 2a 20 31 35 39 20 2a 2f 20 22 67 72 6f  . /* 159 */ "gro
3182d 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f  upby_opt ::= GRO
3182e 55 50 20 42 59 20 6e 65 78 70 72 6c 69 73 74 22  UP BY nexprlist"
3182f 2c 0a 20 2f 2a 20 31 36 30 20 2a 2f 20 22 68 61  ,. /* 160 */ "ha
31830 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  ving_opt ::=",. 
31831 2f 2a 20 31 36 31 20 2a 2f 20 22 68 61 76 69 6e  /* 161 */ "havin
31832 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56 49 4e 47  g_opt ::= HAVING
31833 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 32 20   expr",. /* 162 
31834 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a  */ "limit_opt ::
31835 3d 22 2c 0a 20 2f 2a 20 31 36 33 20 2a 2f 20 22  =",. /* 163 */ "
31836 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49  limit_opt ::= LI
31837 4d 49 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  MIT expr",. /* 1
31838 36 34 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74  64 */ "limit_opt
31839 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20   ::= LIMIT expr 
3183a 4f 46 46 53 45 54 20 65 78 70 72 22 2c 0a 20 2f  OFFSET expr",. /
3183b 2a 20 31 36 35 20 2a 2f 20 22 6c 69 6d 69 74 5f  * 165 */ "limit_
3183c 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78  opt ::= LIMIT ex
3183d 70 72 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a  pr COMMA expr",.
3183e 20 2f 2a 20 31 36 36 20 2a 2f 20 22 63 6d 64 20   /* 166 */ "cmd 
3183f 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
31840 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64  fullname indexed
31841 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70 74 22 2c  _opt where_opt",
31842 0a 20 2f 2a 20 31 36 37 20 2a 2f 20 22 77 68 65  . /* 167 */ "whe
31843 72 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  re_opt ::=",. /*
31844 20 31 36 38 20 2a 2f 20 22 77 68 65 72 65 5f 6f   168 */ "where_o
31845 70 74 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70  pt ::= WHERE exp
31846 72 22 2c 0a 20 2f 2a 20 31 36 39 20 2a 2f 20 22  r",. /* 169 */ "
31847 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f  cmd ::= UPDATE o
31848 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20 69  rconf fullname i
31849 6e 64 65 78 65 64 5f 6f 70 74 20 53 45 54 20 73  ndexed_opt SET s
3184a 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74  etlist where_opt
3184b 22 2c 0a 20 2f 2a 20 31 37 30 20 2a 2f 20 22 73  ",. /* 170 */ "s
3184c 65 74 6c 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69  etlist ::= setli
3184d 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65  st COMMA nm EQ e
3184e 78 70 72 22 2c 0a 20 2f 2a 20 31 37 31 20 2a 2f  xpr",. /* 171 */
3184f 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d   "setlist ::= nm
31850 20 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31   EQ expr",. /* 1
31851 37 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69  72 */ "cmd ::= i
31852 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66  nsert_cmd INTO f
31853 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69  ullname inscolli
31854 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50  st_opt VALUES LP
31855 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20   itemlist RP",. 
31856 2f 2a 20 31 37 33 20 2a 2f 20 22 63 6d 64 20 3a  /* 173 */ "cmd :
31857 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
31858 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
31859 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63  ollist_opt selec
3185a 74 22 2c 0a 20 2f 2a 20 31 37 34 20 2a 2f 20 22  t",. /* 174 */ "
3185b 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
3185c 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
3185d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
3185e 44 45 46 41 55 4c 54 20 56 41 4c 55 45 53 22 2c  DEFAULT VALUES",
3185f 0a 20 2f 2a 20 31 37 35 20 2a 2f 20 22 69 6e 73  . /* 175 */ "ins
31860 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53 45  ert_cmd ::= INSE
31861 52 54 20 6f 72 63 6f 6e 66 22 2c 0a 20 2f 2a 20  RT orconf",. /* 
31862 31 37 36 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63  176 */ "insert_c
31863 6d 64 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c  md ::= REPLACE",
31864 0a 20 2f 2a 20 31 37 37 20 2a 2f 20 22 69 74 65  . /* 177 */ "ite
31865 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69  mlist ::= itemli
31866 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a  st COMMA expr",.
31867 20 2f 2a 20 31 37 38 20 2a 2f 20 22 69 74 65 6d   /* 178 */ "item
31868 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a  list ::= expr",.
31869 20 2f 2a 20 31 37 39 20 2a 2f 20 22 69 6e 73 63   /* 179 */ "insc
3186a 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c  ollist_opt ::=",
3186b 0a 20 2f 2a 20 31 38 30 20 2a 2f 20 22 69 6e 73  . /* 180 */ "ins
3186c 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  collist_opt ::= 
3186d 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50  LP inscollist RP
3186e 22 2c 0a 20 2f 2a 20 31 38 31 20 2a 2f 20 22 69  ",. /* 181 */ "i
3186f 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e  nscollist ::= in
31870 73 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 6e  scollist COMMA n
31871 6d 22 2c 0a 20 2f 2a 20 31 38 32 20 2a 2f 20 22  m",. /* 182 */ "
31872 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e  inscollist ::= n
31873 6d 22 2c 0a 20 2f 2a 20 31 38 33 20 2a 2f 20 22  m",. /* 183 */ "
31874 65 78 70 72 20 3a 3a 3d 20 74 65 72 6d 22 2c 0a  expr ::= term",.
31875 20 2f 2a 20 31 38 34 20 2a 2f 20 22 65 78 70 72   /* 184 */ "expr
31876 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52 50 22   ::= LP expr RP"
31877 2c 0a 20 2f 2a 20 31 38 35 20 2a 2f 20 22 74 65  ,. /* 185 */ "te
31878 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 22 2c 0a 20 2f  rm ::= NULL",. /
31879 2a 20 31 38 36 20 2a 2f 20 22 65 78 70 72 20 3a  * 186 */ "expr :
3187a 3a 3d 20 69 64 22 2c 0a 20 2f 2a 20 31 38 37 20  := id",. /* 187 
3187b 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4a 4f 49  */ "expr ::= JOI
3187c 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 31 38 38 20 2a  N_KW",. /* 188 *
3187d 2f 20 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44  / "expr ::= nm D
3187e 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 39 20  OT nm",. /* 189 
3187f 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20  */ "expr ::= nm 
31880 44 4f 54 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a  DOT nm DOT nm",.
31881 20 2f 2a 20 31 39 30 20 2a 2f 20 22 74 65 72 6d   /* 190 */ "term
31882 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f   ::= INTEGER|FLO
31883 41 54 7c 42 4c 4f 42 22 2c 0a 20 2f 2a 20 31 39  AT|BLOB",. /* 19
31884 31 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 53  1 */ "term ::= S
31885 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 31 39 32 20  TRING",. /* 192 
31886 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52 45 47  */ "expr ::= REG
31887 49 53 54 45 52 22 2c 0a 20 2f 2a 20 31 39 33 20  ISTER",. /* 193 
31888 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 56 41 52  */ "expr ::= VAR
31889 49 41 42 4c 45 22 2c 0a 20 2f 2a 20 31 39 34 20  IABLE",. /* 194 
3188a 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
3188b 72 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a  r COLLATE ids",.
3188c 20 2f 2a 20 31 39 35 20 2a 2f 20 22 65 78 70 72   /* 195 */ "expr
3188d 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20 65 78 70   ::= CAST LP exp
3188e 72 20 41 53 20 74 79 70 65 74 6f 6b 65 6e 20 52  r AS typetoken R
3188f 50 22 2c 0a 20 2f 2a 20 31 39 36 20 2a 2f 20 22  P",. /* 196 */ "
31890 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64  expr ::= ID LP d
31891 69 73 74 69 6e 63 74 20 65 78 70 72 6c 69 73 74  istinct exprlist
31892 20 52 50 22 2c 0a 20 2f 2a 20 31 39 37 20 2a 2f   RP",. /* 197 */
31893 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50   "expr ::= ID LP
31894 20 53 54 41 52 20 52 50 22 2c 0a 20 2f 2a 20 31   STAR RP",. /* 1
31895 39 38 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  98 */ "term ::= 
31896 43 54 49 4d 45 5f 4b 57 22 2c 0a 20 2f 2a 20 31  CTIME_KW",. /* 1
31897 39 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  99 */ "expr ::= 
31898 65 78 70 72 20 41 4e 44 20 65 78 70 72 22 2c 0a  expr AND expr",.
31899 20 2f 2a 20 32 30 30 20 2a 2f 20 22 65 78 70 72   /* 200 */ "expr
3189a 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70   ::= expr OR exp
3189b 72 22 2c 0a 20 2f 2a 20 32 30 31 20 2a 2f 20 22  r",. /* 201 */ "
3189c 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4c 54  expr ::= expr LT
3189d 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70 72 22 2c  |GT|GE|LE expr",
3189e 0a 20 2f 2a 20 32 30 32 20 2a 2f 20 22 65 78 70  . /* 202 */ "exp
3189f 72 20 3a 3a 3d 20 65 78 70 72 20 45 51 7c 4e 45  r ::= expr EQ|NE
318a0 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 33 20   expr",. /* 203 
318a1 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
318a2 72 20 42 49 54 41 4e 44 7c 42 49 54 4f 52 7c 4c  r BITAND|BITOR|L
318a3 53 48 49 46 54 7c 52 53 48 49 46 54 20 65 78 70  SHIFT|RSHIFT exp
318a4 72 22 2c 0a 20 2f 2a 20 32 30 34 20 2a 2f 20 22  r",. /* 204 */ "
318a5 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c  expr ::= expr PL
318a6 55 53 7c 4d 49 4e 55 53 20 65 78 70 72 22 2c 0a  US|MINUS expr",.
318a7 20 2f 2a 20 32 30 35 20 2a 2f 20 22 65 78 70 72   /* 205 */ "expr
318a8 20 3a 3a 3d 20 65 78 70 72 20 53 54 41 52 7c 53   ::= expr STAR|S
318a9 4c 41 53 48 7c 52 45 4d 20 65 78 70 72 22 2c 0a  LASH|REM expr",.
318aa 20 2f 2a 20 32 30 36 20 2a 2f 20 22 65 78 70 72   /* 206 */ "expr
318ab 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54   ::= expr CONCAT
318ac 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 37 20   expr",. /* 207 
318ad 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c  */ "likeop ::= L
318ae 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 38  IKE_KW",. /* 208
318af 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20   */ "likeop ::= 
318b0 4e 4f 54 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f  NOT LIKE_KW",. /
318b1 2a 20 32 30 39 20 2a 2f 20 22 6c 69 6b 65 6f 70  * 209 */ "likeop
318b2 20 3a 3a 3d 20 4d 41 54 43 48 22 2c 0a 20 2f 2a   ::= MATCH",. /*
318b3 20 32 31 30 20 2a 2f 20 22 6c 69 6b 65 6f 70 20   210 */ "likeop 
318b4 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43 48 22 2c 0a  ::= NOT MATCH",.
318b5 20 2f 2a 20 32 31 31 20 2a 2f 20 22 65 73 63 61   /* 211 */ "esca
318b6 70 65 20 3a 3a 3d 20 45 53 43 41 50 45 20 65 78  pe ::= ESCAPE ex
318b7 70 72 22 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f 20  pr",. /* 212 */ 
318b8 22 65 73 63 61 70 65 20 3a 3a 3d 22 2c 0a 20 2f  "escape ::=",. /
318b9 2a 20 32 31 33 20 2a 2f 20 22 65 78 70 72 20 3a  * 213 */ "expr :
318ba 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f 70 20 65  := expr likeop e
318bb 78 70 72 20 65 73 63 61 70 65 22 2c 0a 20 2f 2a  xpr escape",. /*
318bc 20 32 31 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   214 */ "expr ::
318bd 3d 20 65 78 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f  = expr ISNULL|NO
318be 54 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 35 20  TNULL",. /* 215 
318bf 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
318c0 72 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a  r NOT NULL",. /*
318c1 20 32 31 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a   216 */ "expr ::
318c2 3d 20 65 78 70 72 20 49 53 20 65 78 70 72 22 2c  = expr IS expr",
318c3 0a 20 2f 2a 20 32 31 37 20 2a 2f 20 22 65 78 70  . /* 217 */ "exp
318c4 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f  r ::= expr IS NO
318c5 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 38  T expr",. /* 218
318c6 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4e 4f   */ "expr ::= NO
318c7 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 39  T expr",. /* 219
318c8 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 42 49   */ "expr ::= BI
318c9 54 4e 4f 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20  TNOT expr",. /* 
318ca 32 32 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  220 */ "expr ::=
318cb 20 4d 49 4e 55 53 20 65 78 70 72 22 2c 0a 20 2f   MINUS expr",. /
318cc 2a 20 32 32 31 20 2a 2f 20 22 65 78 70 72 20 3a  * 221 */ "expr :
318cd 3a 3d 20 50 4c 55 53 20 65 78 70 72 22 2c 0a 20  := PLUS expr",. 
318ce 2f 2a 20 32 32 32 20 2a 2f 20 22 62 65 74 77 65  /* 222 */ "betwe
318cf 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45 54 57 45 45  en_op ::= BETWEE
318d0 4e 22 2c 0a 20 2f 2a 20 32 32 33 20 2a 2f 20 22  N",. /* 223 */ "
318d1 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e  between_op ::= N
318d2 4f 54 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a  OT BETWEEN",. /*
318d3 20 32 32 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   224 */ "expr ::
318d4 3d 20 65 78 70 72 20 62 65 74 77 65 65 6e 5f 6f  = expr between_o
318d5 70 20 65 78 70 72 20 41 4e 44 20 65 78 70 72 22  p expr AND expr"
318d6 2c 0a 20 2f 2a 20 32 32 35 20 2a 2f 20 22 69 6e  ,. /* 225 */ "in
318d7 5f 6f 70 20 3a 3a 3d 20 49 4e 22 2c 0a 20 2f 2a  _op ::= IN",. /*
318d8 20 32 32 36 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a   226 */ "in_op :
318d9 3a 3d 20 4e 4f 54 20 49 4e 22 2c 0a 20 2f 2a 20  := NOT IN",. /* 
318da 32 32 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  227 */ "expr ::=
318db 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 65   expr in_op LP e
318dc 78 70 72 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  xprlist RP",. /*
318dd 20 32 32 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a   228 */ "expr ::
318de 3d 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22 2c  = LP select RP",
318df 0a 20 2f 2a 20 32 32 39 20 2a 2f 20 22 65 78 70  . /* 229 */ "exp
318e0 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
318e1 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a   LP select RP",.
318e2 20 2f 2a 20 32 33 30 20 2a 2f 20 22 65 78 70 72   /* 230 */ "expr
318e3 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
318e4 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32 33  nm dbnm",. /* 23
318e5 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 45  1 */ "expr ::= E
318e6 58 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74 20  XISTS LP select 
318e7 52 50 22 2c 0a 20 2f 2a 20 32 33 32 20 2a 2f 20  RP",. /* 232 */ 
318e8 22 65 78 70 72 20 3a 3a 3d 20 43 41 53 45 20 63  "expr ::= CASE c
318e9 61 73 65 5f 6f 70 65 72 61 6e 64 20 63 61 73 65  ase_operand case
318ea 5f 65 78 70 72 6c 69 73 74 20 63 61 73 65 5f 65  _exprlist case_e
318eb 6c 73 65 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 33  lse END",. /* 23
318ec 33 20 2a 2f 20 22 63 61 73 65 5f 65 78 70 72 6c  3 */ "case_exprl
318ed 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f 65 78 70  ist ::= case_exp
318ee 72 6c 69 73 74 20 57 48 45 4e 20 65 78 70 72 20  rlist WHEN expr 
318ef 54 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20  THEN expr",. /* 
318f0 32 33 34 20 2a 2f 20 22 63 61 73 65 5f 65 78 70  234 */ "case_exp
318f1 72 6c 69 73 74 20 3a 3a 3d 20 57 48 45 4e 20 65  rlist ::= WHEN e
318f2 78 70 72 20 54 48 45 4e 20 65 78 70 72 22 2c 0a  xpr THEN expr",.
318f3 20 2f 2a 20 32 33 35 20 2a 2f 20 22 63 61 73 65   /* 235 */ "case
318f4 5f 65 6c 73 65 20 3a 3a 3d 20 45 4c 53 45 20 65  _else ::= ELSE e
318f5 78 70 72 22 2c 0a 20 2f 2a 20 32 33 36 20 2a 2f  xpr",. /* 236 */
318f6 20 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 22   "case_else ::="
318f7 2c 0a 20 2f 2a 20 32 33 37 20 2a 2f 20 22 63 61  ,. /* 237 */ "ca
318f8 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65  se_operand ::= e
318f9 78 70 72 22 2c 0a 20 2f 2a 20 32 33 38 20 2a 2f  xpr",. /* 238 */
318fa 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a   "case_operand :
318fb 3a 3d 22 2c 0a 20 2f 2a 20 32 33 39 20 2a 2f 20  :=",. /* 239 */ 
318fc 22 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65  "exprlist ::= ne
318fd 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 34  xprlist",. /* 24
318fe 30 20 2a 2f 20 22 65 78 70 72 6c 69 73 74 20 3a  0 */ "exprlist :
318ff 3a 3d 22 2c 0a 20 2f 2a 20 32 34 31 20 2a 2f 20  :=",. /* 241 */ 
31900 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e  "nexprlist ::= n
31901 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d 41 20 65  exprlist COMMA e
31902 78 70 72 22 2c 0a 20 2f 2a 20 32 34 32 20 2a 2f  xpr",. /* 242 */
31903 20 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20   "nexprlist ::= 
31904 65 78 70 72 22 2c 0a 20 2f 2a 20 32 34 33 20 2a  expr",. /* 243 *
31905 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74  / "cmd ::= creat
31906 65 6b 77 20 75 6e 69 71 75 65 66 6c 61 67 20 49  ekw uniqueflag I
31907 4e 44 45 58 20 69 66 6e 6f 74 65 78 69 73 74 73  NDEX ifnotexists
31908 20 6e 6d 20 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c   nm dbnm ON nm L
31909 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c 0a 20  P idxlist RP",. 
3190a 2f 2a 20 32 34 34 20 2a 2f 20 22 75 6e 69 71 75  /* 244 */ "uniqu
3190b 65 66 6c 61 67 20 3a 3a 3d 20 55 4e 49 51 55 45  eflag ::= UNIQUE
3190c 22 2c 0a 20 2f 2a 20 32 34 35 20 2a 2f 20 22 75  ",. /* 245 */ "u
3190d 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 22 2c 0a  niqueflag ::=",.
3190e 20 2f 2a 20 32 34 36 20 2a 2f 20 22 69 64 78 6c   /* 246 */ "idxl
3190f 69 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ist_opt ::=",. /
31910 2a 20 32 34 37 20 2a 2f 20 22 69 64 78 6c 69 73  * 247 */ "idxlis
31911 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 64 78  t_opt ::= LP idx
31912 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 34  list RP",. /* 24
31913 38 20 2a 2f 20 22 69 64 78 6c 69 73 74 20 3a 3a  8 */ "idxlist ::
31914 3d 20 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20  = idxlist COMMA 
31915 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f  nm collate sorto
31916 72 64 65 72 22 2c 0a 20 2f 2a 20 32 34 39 20 2a  rder",. /* 249 *
31917 2f 20 22 69 64 78 6c 69 73 74 20 3a 3a 3d 20 6e  / "idxlist ::= n
31918 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72  m collate sortor
31919 64 65 72 22 2c 0a 20 2f 2a 20 32 35 30 20 2a 2f  der",. /* 250 */
3191a 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 22 2c 0a   "collate ::=",.
3191b 20 2f 2a 20 32 35 31 20 2a 2f 20 22 63 6f 6c 6c   /* 251 */ "coll
3191c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20  ate ::= COLLATE 
3191d 69 64 73 22 2c 0a 20 2f 2a 20 32 35 32 20 2a 2f  ids",. /* 252 */
3191e 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 49   "cmd ::= DROP I
3191f 4e 44 45 58 20 69 66 65 78 69 73 74 73 20 66 75  NDEX ifexists fu
31920 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 32 35 33  llname",. /* 253
31921 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 56 41 43   */ "cmd ::= VAC
31922 55 55 4d 22 2c 0a 20 2f 2a 20 32 35 34 20 2a 2f  UUM",. /* 254 */
31923 20 22 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d   "cmd ::= VACUUM
31924 20 6e 6d 22 2c 0a 20 2f 2a 20 32 35 35 20 2a 2f   nm",. /* 255 */
31925 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41   "cmd ::= PRAGMA
31926 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32   nm dbnm",. /* 2
31927 35 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  56 */ "cmd ::= P
31928 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
31929 20 6e 6d 6e 75 6d 22 2c 0a 20 2f 2a 20 32 35 37   nmnum",. /* 257
3192a 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41   */ "cmd ::= PRA
3192b 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e  GMA nm dbnm LP n
3192c 6d 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 35  mnum RP",. /* 25
3192d 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52  8 */ "cmd ::= PR
3192e 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
3192f 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20  minus_num",. /* 
31930 32 35 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  259 */ "cmd ::= 
31931 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c  PRAGMA nm dbnm L
31932 50 20 6d 69 6e 75 73 5f 6e 75 6d 20 52 50 22 2c  P minus_num RP",
31933 0a 20 2f 2a 20 32 36 30 20 2a 2f 20 22 6e 6d 6e  . /* 260 */ "nmn
31934 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22  um ::= plus_num"
31935 2c 0a 20 2f 2a 20 32 36 31 20 2a 2f 20 22 6e 6d  ,. /* 261 */ "nm
31936 6e 75 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a  num ::= nm",. /*
31937 20 32 36 32 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a   262 */ "nmnum :
31938 3a 3d 20 4f 4e 22 2c 0a 20 2f 2a 20 32 36 33 20  := ON",. /* 263 
31939 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 44 45  */ "nmnum ::= DE
3193a 4c 45 54 45 22 2c 0a 20 2f 2a 20 32 36 34 20 2a  LETE",. /* 264 *
3193b 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 44 45 46  / "nmnum ::= DEF
3193c 41 55 4c 54 22 2c 0a 20 2f 2a 20 32 36 35 20 2a  AULT",. /* 265 *
3193d 2f 20 22 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d 20  / "plus_num ::= 
3193e 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72 22  plus_opt number"
3193f 2c 0a 20 2f 2a 20 32 36 36 20 2a 2f 20 22 6d 69  ,. /* 266 */ "mi
31940 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49 4e 55  nus_num ::= MINU
31941 53 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32  S number",. /* 2
31942 36 37 20 2a 2f 20 22 6e 75 6d 62 65 72 20 3a 3a  67 */ "number ::
31943 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 22  = INTEGER|FLOAT"
31944 2c 0a 20 2f 2a 20 32 36 38 20 2a 2f 20 22 70 6c  ,. /* 268 */ "pl
31945 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55 53 22  us_opt ::= PLUS"
31946 2c 0a 20 2f 2a 20 32 36 39 20 2a 2f 20 22 70 6c  ,. /* 269 */ "pl
31947 75 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  us_opt ::=",. /*
31948 20 32 37 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   270 */ "cmd ::=
31949 20 63 72 65 61 74 65 6b 77 20 74 72 69 67 67 65   createkw trigge
3194a 72 5f 64 65 63 6c 20 42 45 47 49 4e 20 74 72 69  r_decl BEGIN tri
3194b 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e  gger_cmd_list EN
3194c 44 22 2c 0a 20 2f 2a 20 32 37 31 20 2a 2f 20 22  D",. /* 271 */ "
3194d 74 72 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d  trigger_decl ::=
3194e 20 74 65 6d 70 20 54 52 49 47 47 45 52 20 69 66   temp TRIGGER if
3194f 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e  notexists nm dbn
31950 6d 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74  m trigger_time t
31951 72 69 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20  rigger_event ON 
31952 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68  fullname foreach
31953 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61  _clause when_cla
31954 75 73 65 22 2c 0a 20 2f 2a 20 32 37 32 20 2a 2f  use",. /* 272 */
31955 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a   "trigger_time :
31956 3a 3d 20 42 45 46 4f 52 45 22 2c 0a 20 2f 2a 20  := BEFORE",. /* 
31957 32 37 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  273 */ "trigger_
31958 74 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 22 2c  time ::= AFTER",
31959 0a 20 2f 2a 20 32 37 34 20 2a 2f 20 22 74 72 69  . /* 274 */ "tri
3195a 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e  gger_time ::= IN
3195b 53 54 45 41 44 20 4f 46 22 2c 0a 20 2f 2a 20 32  STEAD OF",. /* 2
3195c 37 35 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74  75 */ "trigger_t
3195d 69 6d 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37  ime ::=",. /* 27
3195e 36 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76  6 */ "trigger_ev
3195f 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c 49  ent ::= DELETE|I
31960 4e 53 45 52 54 22 2c 0a 20 2f 2a 20 32 37 37 20  NSERT",. /* 277 
31961 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e  */ "trigger_even
31962 74 20 3a 3a 3d 20 55 50 44 41 54 45 22 2c 0a 20  t ::= UPDATE",. 
31963 2f 2a 20 32 37 38 20 2a 2f 20 22 74 72 69 67 67  /* 278 */ "trigg
31964 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44  er_event ::= UPD
31965 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c 6c 69 73  ATE OF inscollis
31966 74 22 2c 0a 20 2f 2a 20 32 37 39 20 2a 2f 20 22  t",. /* 279 */ "
31967 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a  foreach_clause :
31968 3a 3d 22 2c 0a 20 2f 2a 20 32 38 30 20 2a 2f 20  :=",. /* 280 */ 
31969 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20  "foreach_clause 
3196a 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20 52 4f 57  ::= FOR EACH ROW
3196b 22 2c 0a 20 2f 2a 20 32 38 31 20 2a 2f 20 22 77  ",. /* 281 */ "w
3196c 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 22 2c  hen_clause ::=",
3196d 0a 20 2f 2a 20 32 38 32 20 2a 2f 20 22 77 68 65  . /* 282 */ "whe
3196e 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45  n_clause ::= WHE
3196f 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 38 33  N expr",. /* 283
31970 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64   */ "trigger_cmd
31971 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65  _list ::= trigge
31972 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72 69 67 67  r_cmd_list trigg
31973 65 72 5f 63 6d 64 20 53 45 4d 49 22 2c 0a 20 2f  er_cmd SEMI",. /
31974 2a 20 32 38 34 20 2a 2f 20 22 74 72 69 67 67 65  * 284 */ "trigge
31975 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74  r_cmd_list ::= t
31976 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 22  rigger_cmd SEMI"
31977 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f 20 22 74 72  ,. /* 285 */ "tr
31978 6e 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20  nm ::= nm",. /* 
31979 32 38 36 20 2a 2f 20 22 74 72 6e 6d 20 3a 3a 3d  286 */ "trnm ::=
3197a 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a   nm DOT nm",. /*
3197b 20 32 38 37 20 2a 2f 20 22 74 72 69 64 78 62 79   287 */ "tridxby
3197c 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 38 20 2a   ::=",. /* 288 *
3197d 2f 20 22 74 72 69 64 78 62 79 20 3a 3a 3d 20 49  / "tridxby ::= I
3197e 4e 44 45 58 45 44 20 42 59 20 6e 6d 22 2c 0a 20  NDEXED BY nm",. 
3197f 2f 2a 20 32 38 39 20 2a 2f 20 22 74 72 69 64 78  /* 289 */ "tridx
31980 62 79 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58  by ::= NOT INDEX
31981 45 44 22 2c 0a 20 2f 2a 20 32 39 30 20 2a 2f 20  ED",. /* 290 */ 
31982 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d  "trigger_cmd ::=
31983 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 74   UPDATE orconf t
31984 72 6e 6d 20 74 72 69 64 78 62 79 20 53 45 54 20  rnm tridxby SET 
31985 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70  setlist where_op
31986 74 22 2c 0a 20 2f 2a 20 32 39 31 20 2a 2f 20 22  t",. /* 291 */ "
31987 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
31988 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
31989 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  trnm inscollist_
3198a 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74  opt VALUES LP it
3198b 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  emlist RP",. /* 
3198c 32 39 32 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  292 */ "trigger_
3198d 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
3198e 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73  md INTO trnm ins
3198f 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65  collist_opt sele
31990 63 74 22 2c 0a 20 2f 2a 20 32 39 33 20 2a 2f 20  ct",. /* 293 */ 
31991 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d  "trigger_cmd ::=
31992 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 72 6e   DELETE FROM trn
31993 6d 20 74 72 69 64 78 62 79 20 77 68 65 72 65 5f  m tridxby where_
31994 6f 70 74 22 2c 0a 20 2f 2a 20 32 39 34 20 2a 2f  opt",. /* 294 */
31995 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
31996 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32  = select",. /* 2
31997 39 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  95 */ "expr ::= 
31998 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52 45 20  RAISE LP IGNORE 
31999 52 50 22 2c 0a 20 2f 2a 20 32 39 36 20 2a 2f 20  RP",. /* 296 */ 
3199a 22 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45 20  "expr ::= RAISE 
3199b 4c 50 20 72 61 69 73 65 74 79 70 65 20 43 4f 4d  LP raisetype COM
3199c 4d 41 20 6e 6d 20 52 50 22 2c 0a 20 2f 2a 20 32  MA nm RP",. /* 2
3199d 39 37 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65  97 */ "raisetype
3199e 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 22 2c 0a   ::= ROLLBACK",.
3199f 20 2f 2a 20 32 39 38 20 2a 2f 20 22 72 61 69 73   /* 298 */ "rais
319a0 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54 22  etype ::= ABORT"
319a1 2c 0a 20 2f 2a 20 32 39 39 20 2a 2f 20 22 72 61  ,. /* 299 */ "ra
319a2 69 73 65 74 79 70 65 20 3a 3a 3d 20 46 41 49 4c  isetype ::= FAIL
319a3 22 2c 0a 20 2f 2a 20 33 30 30 20 2a 2f 20 22 63  ",. /* 300 */ "c
319a4 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47  md ::= DROP TRIG
319a5 47 45 52 20 69 66 65 78 69 73 74 73 20 66 75 6c  GER ifexists ful
319a6 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33 30 31 20  lname",. /* 301 
319a7 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 54 54 41  */ "cmd ::= ATTA
319a8 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  CH database_kw_o
319a9 70 74 20 65 78 70 72 20 41 53 20 65 78 70 72 20  pt expr AS expr 
319aa 6b 65 79 5f 6f 70 74 22 2c 0a 20 2f 2a 20 33 30  key_opt",. /* 30
319ab 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45  2 */ "cmd ::= DE
319ac 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77  TACH database_kw
319ad 5f 6f 70 74 20 65 78 70 72 22 2c 0a 20 2f 2a 20  _opt expr",. /* 
319ae 33 30 33 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20  303 */ "key_opt 
319af 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 34 20 2a 2f  ::=",. /* 304 */
319b0 20 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45   "key_opt ::= KE
319b1 59 20 65 78 70 72 22 2c 0a 20 2f 2a 20 33 30 35  Y expr",. /* 305
319b2 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f 6b 77   */ "database_kw
319b3 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42 41 53  _opt ::= DATABAS
319b4 45 22 2c 0a 20 2f 2a 20 33 30 36 20 2a 2f 20 22  E",. /* 306 */ "
319b5 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
319b6 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 37 20 2a 2f  ::=",. /* 307 */
319b7 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45   "cmd ::= REINDE
319b8 58 22 2c 0a 20 2f 2a 20 33 30 38 20 2a 2f 20 22  X",. /* 308 */ "
319b9 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20  cmd ::= REINDEX 
319ba 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 33 30  nm dbnm",. /* 30
319bb 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4e  9 */ "cmd ::= AN
319bc 41 4c 59 5a 45 22 2c 0a 20 2f 2a 20 33 31 30 20  ALYZE",. /* 310 
319bd 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c  */ "cmd ::= ANAL
319be 59 5a 45 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f  YZE nm dbnm",. /
319bf 2a 20 33 31 31 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 311 */ "cmd ::
319c0 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 66 75  = ALTER TABLE fu
319c1 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45 20 54 4f  llname RENAME TO
319c2 20 6e 6d 22 2c 0a 20 2f 2a 20 33 31 32 20 2a 2f   nm",. /* 312 */
319c3 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20   "cmd ::= ALTER 
319c4 54 41 42 4c 45 20 61 64 64 5f 63 6f 6c 75 6d 6e  TABLE add_column
319c5 5f 66 75 6c 6c 6e 61 6d 65 20 41 44 44 20 6b 77  _fullname ADD kw
319c6 63 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f 6c 75 6d  column_opt colum
319c7 6e 22 2c 0a 20 2f 2a 20 33 31 33 20 2a 2f 20 22  n",. /* 313 */ "
319c8 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e  add_column_fulln
319c9 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65  ame ::= fullname
319ca 22 2c 0a 20 2f 2a 20 33 31 34 20 2a 2f 20 22 6b  ",. /* 314 */ "k
319cb 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 22  wcolumn_opt ::="
319cc 2c 0a 20 2f 2a 20 33 31 35 20 2a 2f 20 22 6b 77  ,. /* 315 */ "kw
319cd 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 43  column_opt ::= C
319ce 4f 4c 55 4d 4e 4b 57 22 2c 0a 20 2f 2a 20 33 31  OLUMNKW",. /* 31
319cf 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72  6 */ "cmd ::= cr
319d0 65 61 74 65 5f 76 74 61 62 22 2c 0a 20 2f 2a 20  eate_vtab",. /* 
319d1 33 31 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  317 */ "cmd ::= 
319d2 63 72 65 61 74 65 5f 76 74 61 62 20 4c 50 20 76  create_vtab LP v
319d3 74 61 62 61 72 67 6c 69 73 74 20 52 50 22 2c 0a  tabarglist RP",.
319d4 20 2f 2a 20 33 31 38 20 2a 2f 20 22 63 72 65 61   /* 318 */ "crea
319d5 74 65 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65 61  te_vtab ::= crea
319d6 74 65 6b 77 20 56 49 52 54 55 41 4c 20 54 41 42  tekw VIRTUAL TAB
319d7 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47  LE nm dbnm USING
319d8 20 6e 6d 22 2c 0a 20 2f 2a 20 33 31 39 20 2a 2f   nm",. /* 319 */
319d9 20 22 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a   "vtabarglist ::
319da 3d 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20  = vtabarg",. /* 
319db 33 32 30 20 2a 2f 20 22 76 74 61 62 61 72 67 6c  320 */ "vtabargl
319dc 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c  ist ::= vtabargl
319dd 69 73 74 20 43 4f 4d 4d 41 20 76 74 61 62 61 72  ist COMMA vtabar
319de 67 22 2c 0a 20 2f 2a 20 33 32 31 20 2a 2f 20 22  g",. /* 321 */ "
319df 76 74 61 62 61 72 67 20 3a 3a 3d 22 2c 0a 20 2f  vtabarg ::=",. /
319e0 2a 20 33 32 32 20 2a 2f 20 22 76 74 61 62 61 72  * 322 */ "vtabar
319e1 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20 76 74  g ::= vtabarg vt
319e2 61 62 61 72 67 74 6f 6b 65 6e 22 2c 0a 20 2f 2a  abargtoken",. /*
319e3 20 33 32 33 20 2a 2f 20 22 76 74 61 62 61 72 67   323 */ "vtabarg
319e4 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 22 2c 0a  token ::= ANY",.
319e5 20 2f 2a 20 33 32 34 20 2a 2f 20 22 76 74 61 62   /* 324 */ "vtab
319e6 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20  argtoken ::= lp 
319e7 61 6e 79 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  anylist RP",. /*
319e8 20 33 32 35 20 2a 2f 20 22 6c 70 20 3a 3a 3d 20   325 */ "lp ::= 
319e9 4c 50 22 2c 0a 20 2f 2a 20 33 32 36 20 2a 2f 20  LP",. /* 326 */ 
319ea 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20  "anylist ::=",. 
319eb 2f 2a 20 33 32 37 20 2a 2f 20 22 61 6e 79 6c 69  /* 327 */ "anyli
319ec 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 4c  st ::= anylist L
319ed 50 20 61 6e 79 6c 69 73 74 20 52 50 22 2c 0a 20  P anylist RP",. 
319ee 2f 2a 20 33 32 38 20 2a 2f 20 22 61 6e 79 6c 69  /* 328 */ "anyli
319ef 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41  st ::= anylist A
319f0 4e 59 22 2c 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f  NY",.};.#endif /
319f1 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69  * NDEBUG */...#i
319f2 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d  f YYSTACKDEPTH<=
319f3 30 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 69  0./*.** Try to i
319f4 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65  ncrease the size
319f5 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 20 73   of the parser s
319f6 74 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tack..*/.static 
319f7 76 6f 69 64 20 79 79 47 72 6f 77 53 74 61 63 6b  void yyGrowStack
319f8 28 79 79 50 61 72 73 65 72 20 2a 70 29 7b 0a 20  (yyParser *p){. 
319f9 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20   int newSize;.  
319fa 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 70 4e  yyStackEntry *pN
319fb 65 77 3b 0a 0a 20 20 6e 65 77 53 69 7a 65 20 3d  ew;..  newSize =
319fc 20 70 2d 3e 79 79 73 74 6b 73 7a 2a 32 20 2b 20   p->yystksz*2 + 
319fd 31 30 30 3b 0a 20 20 70 4e 65 77 20 3d 20 72 65  100;.  pNew = re
319fe 61 6c 6c 6f 63 28 70 2d 3e 79 79 73 74 61 63 6b  alloc(p->yystack
319ff 2c 20 6e 65 77 53 69 7a 65 2a 73 69 7a 65 6f 66  , newSize*sizeof
31a00 28 70 4e 65 77 5b 30 5d 29 29 3b 0a 20 20 69 66  (pNew[0]));.  if
31a01 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d  ( pNew ){.    p-
31a02 3e 79 79 73 74 61 63 6b 20 3d 20 70 4e 65 77 3b  >yystack = pNew;
31a03 0a 20 20 20 20 70 2d 3e 79 79 73 74 6b 73 7a 20  .    p->yystksz 
31a04 3d 20 6e 65 77 53 69 7a 65 3b 0a 23 69 66 6e 64  = newSize;.#ifnd
31a05 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 69 66  ef NDEBUG.    if
31a06 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
31a07 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79  .      fprintf(y
31a08 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 74  yTraceFILE,"%sSt
31a09 61 63 6b 20 67 72 6f 77 73 20 74 6f 20 25 64 20  ack grows to %d 
31a0a 65 6e 74 72 69 65 73 21 5c 6e 22 2c 0a 20 20 20  entries!\n",.   
31a0b 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72 61             yyTra
31a0c 63 65 50 72 6f 6d 70 74 2c 20 70 2d 3e 79 79 73  cePrompt, p->yys
31a0d 74 6b 73 7a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  tksz);.    }.#en
31a0e 64 69 66 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  dif.  }.}.#endif
31a0f 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75  ../* .** This fu
31a10 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73  nction allocates
31a11 20 61 20 6e 65 77 20 70 61 72 73 65 72 2e 0a 2a   a new parser..*
31a12 2a 20 54 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d  * The only argum
31a13 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ent is a pointer
31a14 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 77   to a function w
31a15 68 69 63 68 20 77 6f 72 6b 73 20 6c 69 6b 65 0a  hich works like.
31a16 2a 2a 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a  ** malloc..**.**
31a17 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 41 20 70 6f   Inputs:.** A po
31a18 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e  inter to the fun
31a19 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c  ction used to al
31a1a 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 0a 2a  locate memory..*
31a1b 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a  *.** Outputs:.**
31a1c 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   A pointer to a 
31a1d 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 70 6f  parser.  This po
31a1e 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 69 6e  inter is used in
31a1f 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
31a20 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50  s.** to sqlite3P
31a21 61 72 73 65 72 20 61 6e 64 20 73 71 6c 69 74 65  arser and sqlite
31a22 33 50 61 72 73 65 72 46 72 65 65 2e 0a 2a 2f 0a  3ParserFree..*/.
31a23 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
31a24 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73  oid *sqlite3Pars
31a25 65 72 41 6c 6c 6f 63 28 76 6f 69 64 20 2a 28 2a  erAlloc(void *(*
31a26 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 73 69 7a 65  mallocProc)(size
31a27 5f 74 29 29 7b 0a 20 20 79 79 50 61 72 73 65 72  _t)){.  yyParser
31a28 20 2a 70 50 61 72 73 65 72 3b 0a 20 20 70 50 61   *pParser;.  pPa
31a29 72 73 65 72 20 3d 20 28 79 79 50 61 72 73 65 72  rser = (yyParser
31a2a 2a 29 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28  *)(*mallocProc)(
31a2b 20 28 73 69 7a 65 5f 74 29 73 69 7a 65 6f 66 28   (size_t)sizeof(
31a2c 79 79 50 61 72 73 65 72 29 20 29 3b 0a 20 20 69  yyParser) );.  i
31a2d 66 28 20 70 50 61 72 73 65 72 20 29 7b 0a 20 20  f( pParser ){.  
31a2e 20 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78    pParser->yyidx
31a2f 20 3d 20 2d 31 3b 0a 23 69 66 64 65 66 20 59 59   = -1;.#ifdef YY
31a30 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50  TRACKMAXSTACKDEP
31a31 54 48 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e  TH.    pParser->
31a32 79 79 69 64 78 4d 61 78 20 3d 20 30 3b 0a 23 65  yyidxMax = 0;.#e
31a33 6e 64 69 66 0a 23 69 66 20 59 59 53 54 41 43 4b  ndif.#if YYSTACK
31a34 44 45 50 54 48 3c 3d 30 0a 20 20 20 20 70 50 61  DEPTH<=0.    pPa
31a35 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 20 3d 20  rser->yystack = 
31a36 4e 55 4c 4c 3b 0a 20 20 20 20 70 50 61 72 73 65  NULL;.    pParse
31a37 72 2d 3e 79 79 73 74 6b 73 7a 20 3d 20 30 3b 0a  r->yystksz = 0;.
31a38 20 20 20 20 79 79 47 72 6f 77 53 74 61 63 6b 28      yyGrowStack(
31a39 70 50 61 72 73 65 72 29 3b 0a 23 65 6e 64 69 66  pParser);.#endif
31a3a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50  .  }.  return pP
31a3b 61 72 73 65 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 65  arser;.}../* The
31a3c 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74   following funct
31a3d 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 65 20  ion deletes the 
31a3e 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64  value associated
31a3f 20 77 69 74 68 20 61 0a 2a 2a 20 73 79 6d 62 6f   with a.** symbo
31a40 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 63  l.  The symbol c
31a41 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 74  an be either a t
31a42 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65  erminal or nonte
31a43 72 6d 69 6e 61 6c 2e 0a 2a 2a 20 22 79 79 6d 61  rminal..** "yyma
31a44 6a 6f 72 22 20 69 73 20 74 68 65 20 73 79 6d 62  jor" is the symb
31a45 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20 22 79 79  ol code, and "yy
31a46 70 6d 69 6e 6f 72 22 20 69 73 20 61 20 70 6f 69  pminor" is a poi
31a47 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 76  nter to.** the v
31a48 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  alue..*/.static 
31a49 76 6f 69 64 20 79 79 5f 64 65 73 74 72 75 63 74  void yy_destruct
31a4a 6f 72 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  or(.  yyParser *
31a4b 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 2f 2a  yypParser,    /*
31a4c 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
31a4d 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 6d 61   YYCODETYPE yyma
31a4e 6a 6f 72 2c 20 20 20 20 20 2f 2a 20 54 79 70 65  jor,     /* Type
31a4f 20 63 6f 64 65 20 66 6f 72 20 6f 62 6a 65 63 74   code for object
31a50 20 74 6f 20 64 65 73 74 72 6f 79 20 2a 2f 0a 20   to destroy */. 
31a51 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79   YYMINORTYPE *yy
31a52 70 6d 69 6e 6f 72 20 20 20 2f 2a 20 54 68 65 20  pminor   /* The 
31a53 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 64 65 73  object to be des
31a54 74 72 6f 79 65 64 20 2a 2f 0a 29 7b 0a 20 20 73  troyed */.){.  s
31a55 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
31a56 46 45 54 43 48 3b 0a 20 20 73 77 69 74 63 68 28  FETCH;.  switch(
31a57 20 79 79 6d 61 6a 6f 72 20 29 7b 0a 20 20 20 20   yymajor ){.    
31a58 2f 2a 20 48 65 72 65 20 69 73 20 69 6e 73 65 72  /* Here is inser
31a59 74 65 64 20 74 68 65 20 61 63 74 69 6f 6e 73 20  ted the actions 
31a5a 77 68 69 63 68 20 74 61 6b 65 20 70 6c 61 63 65  which take place
31a5b 20 77 68 65 6e 20 61 0a 20 20 20 20 2a 2a 20 74   when a.    ** t
31a5c 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 2d 74  erminal or non-t
31a5d 65 72 6d 69 6e 61 6c 20 69 73 20 64 65 73 74 72  erminal is destr
31a5e 6f 79 65 64 2e 20 20 54 68 69 73 20 63 61 6e 20  oyed.  This can 
31a5f 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a 20 77 68  happen.    ** wh
31a60 65 6e 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 73  en the symbol is
31a61 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65   popped from the
31a62 20 73 74 61 63 6b 20 64 75 72 69 6e 67 20 61 0a   stack during a.
31a63 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20 6f 72      ** reduce or
31a64 20 64 75 72 69 6e 67 20 65 72 72 6f 72 20 70 72   during error pr
31a65 6f 63 65 73 73 69 6e 67 20 6f 72 20 77 68 65 6e  ocessing or when
31a66 20 61 20 70 61 72 73 65 72 20 69 73 20 0a 20 20   a parser is .  
31a67 20 20 2a 2a 20 62 65 69 6e 67 20 64 65 73 74 72    ** being destr
31a68 6f 79 65 64 20 62 65 66 6f 72 65 20 69 74 20 69  oyed before it i
31a69 73 20 66 69 6e 69 73 68 65 64 20 70 61 72 73 69  s finished parsi
31a6a 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ng..    **.    *
31a6b 2a 20 4e 6f 74 65 3a 20 64 75 72 69 6e 67 20 61  * Note: during a
31a6c 20 72 65 64 75 63 65 2c 20 74 68 65 20 6f 6e 6c   reduce, the onl
31a6d 79 20 73 79 6d 62 6f 6c 73 20 64 65 73 74 72 6f  y symbols destro
31a6e 79 65 64 20 61 72 65 20 74 68 6f 73 65 0a 20 20  yed are those.  
31a6f 20 20 2a 2a 20 77 68 69 63 68 20 61 70 70 65 61    ** which appea
31a70 72 20 6f 6e 20 74 68 65 20 52 48 53 20 6f 66 20  r on the RHS of 
31a71 74 68 65 20 72 75 6c 65 2c 20 62 75 74 20 77 68  the rule, but wh
31a72 69 63 68 20 61 72 65 20 6e 6f 74 20 75 73 65 64  ich are not used
31a73 0a 20 20 20 20 2a 2a 20 69 6e 73 69 64 65 20 74  .    ** inside t
31a74 68 65 20 43 20 63 6f 64 65 2e 0a 20 20 20 20 2a  he C code..    *
31a75 2f 0a 20 20 20 20 63 61 73 65 20 31 36 30 3a 20  /.    case 160: 
31a76 2f 2a 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20 20  /* select */.   
31a77 20 63 61 73 65 20 31 39 34 3a 20 2f 2a 20 6f 6e   case 194: /* on
31a78 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 73 71 6c  eselect */.{.sql
31a79 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
31a7a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79  (pParse->db, (yy
31a7b 70 6d 69 6e 6f 72 2d 3e 79 79 33 29 29 3b 0a 7d  pminor->yy3));.}
31a7c 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
31a7d 20 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 74    case 174: /* t
31a7e 65 72 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  erm */.    case 
31a7f 31 37 35 3a 20 2f 2a 20 65 78 70 72 20 2a 2f 0a  175: /* expr */.
31a80 20 20 20 20 63 61 73 65 20 32 32 33 3a 20 2f 2a      case 223: /*
31a81 20 65 73 63 61 70 65 20 2a 2f 0a 7b 0a 73 71 6c   escape */.{.sql
31a82 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
31a83 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d  Parse->db, (yypm
31a84 69 6e 6f 72 2d 3e 79 79 33 34 36 29 2e 70 45 78  inor->yy346).pEx
31a85 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  pr);.}.      bre
31a86 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37 39  ak;.    case 179
31a87 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74  : /* idxlist_opt
31a88 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 38 37   */.    case 187
31a89 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 2a 2f 0a  : /* idxlist */.
31a8a 20 20 20 20 63 61 73 65 20 31 39 37 3a 20 2f 2a      case 197: /*
31a8b 20 73 65 6c 63 6f 6c 6c 69 73 74 20 2a 2f 0a 20   selcollist */. 
31a8c 20 20 20 63 61 73 65 20 32 30 30 3a 20 2f 2a 20     case 200: /* 
31a8d 67 72 6f 75 70 62 79 5f 6f 70 74 20 2a 2f 0a 20  groupby_opt */. 
31a8e 20 20 20 63 61 73 65 20 32 30 32 3a 20 2f 2a 20     case 202: /* 
31a8f 6f 72 64 65 72 62 79 5f 6f 70 74 20 2a 2f 0a 20  orderby_opt */. 
31a90 20 20 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20     case 204: /* 
31a91 73 63 6c 70 20 2a 2f 0a 20 20 20 20 63 61 73 65  sclp */.    case
31a92 20 32 31 34 3a 20 2f 2a 20 73 6f 72 74 6c 69 73   214: /* sortlis
31a93 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31  t */.    case 21
31a94 36 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20  6: /* nexprlist 
31a95 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 37 3a  */.    case 217:
31a96 20 2f 2a 20 73 65 74 6c 69 73 74 20 2a 2f 0a 20   /* setlist */. 
31a97 20 20 20 63 61 73 65 20 32 32 30 3a 20 2f 2a 20     case 220: /* 
31a98 69 74 65 6d 6c 69 73 74 20 2a 2f 0a 20 20 20 20  itemlist */.    
31a99 63 61 73 65 20 32 32 31 3a 20 2f 2a 20 65 78 70  case 221: /* exp
31a9a 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  rlist */.    cas
31a9b 65 20 32 32 37 3a 20 2f 2a 20 63 61 73 65 5f 65  e 227: /* case_e
31a9c 78 70 72 6c 69 73 74 20 2a 2f 0a 7b 0a 73 71 6c  xprlist */.{.sql
31a9d 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
31a9e 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28  te(pParse->db, (
31a9f 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 34 29 29  yypminor->yy14))
31aa0 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
31aa1 0a 20 20 20 20 63 61 73 65 20 31 39 33 3a 20 2f  .    case 193: /
31aa2 2a 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 20 20  * fullname */.  
31aa3 20 20 63 61 73 65 20 31 39 38 3a 20 2f 2a 20 66    case 198: /* f
31aa4 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  rom */.    case 
31aa5 32 30 36 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69  206: /* seltabli
31aa6 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
31aa7 30 37 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69  07: /* stl_prefi
31aa8 78 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53 72  x */.{.sqlite3Sr
31aa9 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  cListDelete(pPar
31aaa 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f  se->db, (yypmino
31aab 72 2d 3e 79 79 36 35 29 29 3b 0a 7d 0a 20 20 20  r->yy65));.}.   
31aac 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
31aad 73 65 20 31 39 39 3a 20 2f 2a 20 77 68 65 72 65  se 199: /* where
31aae 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
31aaf 20 32 30 31 3a 20 2f 2a 20 68 61 76 69 6e 67 5f   201: /* having_
31ab0 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
31ab1 32 31 30 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 2a  210: /* on_opt *
31ab2 2f 0a 20 20 20 20 63 61 73 65 20 32 31 35 3a 20  /.    case 215: 
31ab3 2f 2a 20 73 6f 72 74 69 74 65 6d 20 2a 2f 0a 20  /* sortitem */. 
31ab4 20 20 20 63 61 73 65 20 32 32 36 3a 20 2f 2a 20     case 226: /* 
31ab5 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 2a 2f 0a  case_operand */.
31ab6 20 20 20 20 63 61 73 65 20 32 32 38 3a 20 2f 2a      case 228: /*
31ab7 20 63 61 73 65 5f 65 6c 73 65 20 2a 2f 0a 20 20   case_else */.  
31ab8 20 20 63 61 73 65 20 32 33 39 3a 20 2f 2a 20 77    case 239: /* w
31ab9 68 65 6e 5f 63 6c 61 75 73 65 20 2a 2f 0a 20 20  hen_clause */.  
31aba 20 20 63 61 73 65 20 32 34 34 3a 20 2f 2a 20 6b    case 244: /* k
31abb 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c 69  ey_opt */.{.sqli
31abc 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50  te3ExprDelete(pP
31abd 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69  arse->db, (yypmi
31abe 6e 6f 72 2d 3e 79 79 31 33 32 29 29 3b 0a 7d 0a  nor->yy132));.}.
31abf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31ac0 20 63 61 73 65 20 32 31 31 3a 20 2f 2a 20 75 73   case 211: /* us
31ac1 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63  ing_opt */.    c
31ac2 61 73 65 20 32 31 33 3a 20 2f 2a 20 69 6e 73 63  ase 213: /* insc
31ac3 6f 6c 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  ollist */.    ca
31ac4 73 65 20 32 31 39 3a 20 2f 2a 20 69 6e 73 63 6f  se 219: /* insco
31ac5 6c 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 73  llist_opt */.{.s
31ac6 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
31ac7 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28  te(pParse->db, (
31ac8 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 30 38 29  yypminor->yy408)
31ac9 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
31aca 3b 0a 20 20 20 20 63 61 73 65 20 32 33 35 3a 20  ;.    case 235: 
31acb 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  /* trigger_cmd_l
31acc 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
31acd 32 34 30 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  240: /* trigger_
31ace 63 6d 64 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33  cmd */.{.sqlite3
31acf 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65  DeleteTriggerSte
31ad0 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79  p(pParse->db, (y
31ad1 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 37 33 29 29  ypminor->yy473))
31ad2 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
31ad3 0a 20 20 20 20 63 61 73 65 20 32 33 37 3a 20 2f  .    case 237: /
31ad4 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  * trigger_event 
31ad5 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c 69  */.{.sqlite3IdLi
31ad6 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
31ad7 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e  >db, (yypminor->
31ad8 79 79 33 37 38 29 2e 62 29 3b 0a 7d 0a 20 20 20  yy378).b);.}.   
31ad9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
31ada 66 61 75 6c 74 3a 20 20 62 72 65 61 6b 3b 20 20  fault:  break;  
31adb 20 2f 2a 20 49 66 20 6e 6f 20 64 65 73 74 72 75   /* If no destru
31adc 63 74 6f 72 20 61 63 74 69 6f 6e 20 73 70 65 63  ctor action spec
31add 69 66 69 65 64 3a 20 64 6f 20 6e 6f 74 68 69 6e  ified: do nothin
31ade 67 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  g */.  }.}../*.*
31adf 2a 20 50 6f 70 20 74 68 65 20 70 61 72 73 65 72  * Pop the parser
31ae0 27 73 20 73 74 61 63 6b 20 6f 6e 63 65 2e 0a 2a  's stack once..*
31ae1 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  *.** If there is
31ae2 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 72 6f   a destructor ro
31ae3 75 74 69 6e 65 20 61 73 73 6f 63 69 61 74 65 64  utine associated
31ae4 20 77 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 20   with the token 
31ae5 77 68 69 63 68 0a 2a 2a 20 69 73 20 70 6f 70 70  which.** is popp
31ae6 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ed from the stac
31ae7 6b 2c 20 74 68 65 6e 20 63 61 6c 6c 20 69 74 2e  k, then call it.
31ae8 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
31ae9 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75  e major token nu
31aea 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73 79 6d  mber for the sym
31aeb 62 6f 6c 20 70 6f 70 70 65 64 2e 0a 2a 2f 0a 73  bol popped..*/.s
31aec 74 61 74 69 63 20 69 6e 74 20 79 79 5f 70 6f 70  tatic int yy_pop
31aed 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79  _parser_stack(yy
31aee 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 29  Parser *pParser)
31aef 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 79  {.  YYCODETYPE y
31af0 79 6d 61 6a 6f 72 3b 0a 20 20 79 79 53 74 61 63  ymajor;.  yyStac
31af1 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73 20 3d 20  kEntry *yytos = 
31af2 26 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  &pParser->yystac
31af3 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  k[pParser->yyidx
31af4 5d 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 69  ];..  /* There i
31af5 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20 62  s no mechanism b
31af6 79 20 77 68 69 63 68 20 74 68 65 20 70 61 72 73  y which the pars
31af7 65 72 20 73 74 61 63 6b 20 63 61 6e 20 62 65 20  er stack can be 
31af8 70 6f 70 70 65 64 20 62 65 6c 6f 77 0a 20 20 2a  popped below.  *
31af9 2a 20 65 6d 70 74 79 20 69 6e 20 53 51 4c 69 74  * empty in SQLit
31afa 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56  e.  */.  if( NEV
31afb 45 52 28 70 50 61 72 73 65 72 2d 3e 79 79 69 64  ER(pParser->yyid
31afc 78 3c 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b  x<0) ) return 0;
31afd 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
31afe 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
31aff 45 20 26 26 20 70 50 61 72 73 65 72 2d 3e 79 79  E && pParser->yy
31b00 69 64 78 3e 3d 30 20 29 7b 0a 20 20 20 20 66 70  idx>=0 ){.    fp
31b01 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
31b02 45 2c 22 25 73 50 6f 70 70 69 6e 67 20 25 73 5c  E,"%sPopping %s\
31b03 6e 22 2c 0a 20 20 20 20 20 20 79 79 54 72 61 63  n",.      yyTrac
31b04 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79  ePrompt,.      y
31b05 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 74 6f 73  yTokenName[yytos
31b06 2d 3e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23  ->major]);.  }.#
31b07 65 6e 64 69 66 0a 20 20 79 79 6d 61 6a 6f 72 20  endif.  yymajor 
31b08 3d 20 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 3b 0a  = yytos->major;.
31b09 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28    yy_destructor(
31b0a 70 50 61 72 73 65 72 2c 20 79 79 6d 61 6a 6f 72  pParser, yymajor
31b0b 2c 20 26 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72 29  , &yytos->minor)
31b0c 3b 0a 20 20 70 50 61 72 73 65 72 2d 3e 79 79 69  ;.  pParser->yyi
31b0d 64 78 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20 79  dx--;.  return y
31b0e 79 6d 61 6a 6f 72 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  ymajor;.}../* .*
31b0f 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6e 64  * Deallocate and
31b10 20 64 65 73 74 72 6f 79 20 61 20 70 61 72 73 65   destroy a parse
31b11 72 2e 20 20 44 65 73 74 72 75 63 74 6f 72 73 20  r.  Destructors 
31b12 61 72 65 20 61 6c 6c 20 63 61 6c 6c 65 64 20 66  are all called f
31b13 6f 72 0a 2a 2a 20 61 6c 6c 20 73 74 61 63 6b 20  or.** all stack 
31b14 65 6c 65 6d 65 6e 74 73 20 62 65 66 6f 72 65 20  elements before 
31b15 73 68 75 74 74 69 6e 67 20 74 68 65 20 70 61 72  shutting the par
31b16 73 65 72 20 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20  ser down..**.** 
31b17 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a  Inputs:.** <ul>.
31b18 2a 2a 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74  ** <li>  A point
31b19 65 72 20 74 6f 20 74 68 65 20 70 61 72 73 65 72  er to the parser
31b1a 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  .  This should b
31b1b 65 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20  e a pointer.**  
31b1c 20 20 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72       obtained fr
31b1d 6f 6d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  om sqlite3Parser
31b1e 41 6c 6c 6f 63 2e 0a 2a 2a 20 3c 6c 69 3e 20 20  Alloc..** <li>  
31b1f 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  A pointer to a f
31b20 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20  unction used to 
31b21 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 6f  reclaim memory o
31b22 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20  btained.**      
31b23 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a   from malloc..**
31b24 20 3c 2f 75 6c 3e 0a 2a 2f 0a 53 51 4c 49 54 45   </ul>.*/.SQLITE
31b25 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
31b26 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28  lite3ParserFree(
31b27 0a 20 20 76 6f 69 64 20 2a 70 2c 20 20 20 20 20  .  void *p,     
31b28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
31b29 2a 20 54 68 65 20 70 61 72 73 65 72 20 74 6f 20  * The parser to 
31b2a 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  be deleted */.  
31b2b 76 6f 69 64 20 28 2a 66 72 65 65 50 72 6f 63 29  void (*freeProc)
31b2c 28 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a 20 46  (void*)     /* F
31b2d 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20  unction used to 
31b2e 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 2a  reclaim memory *
31b2f 2f 0a 29 7b 0a 20 20 79 79 50 61 72 73 65 72 20  /.){.  yyParser 
31b30 2a 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61  *pParser = (yyPa
31b31 72 73 65 72 2a 29 70 3b 0a 20 20 2f 2a 20 49 6e  rser*)p;.  /* In
31b32 20 53 51 4c 69 74 65 2c 20 77 65 20 6e 65 76 65   SQLite, we neve
31b33 72 20 74 72 79 20 74 6f 20 64 65 73 74 72 6f 79  r try to destroy
31b34 20 61 20 70 61 72 73 65 72 20 74 68 61 74 20 77   a parser that w
31b35 61 73 20 6e 6f 74 20 73 75 63 63 65 73 73 66 75  as not successfu
31b36 6c 6c 79 0a 20 20 2a 2a 20 63 72 65 61 74 65 64  lly.  ** created
31b37 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c   in the first pl
31b38 61 63 65 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45  ace. */.  if( NE
31b39 56 45 52 28 70 50 61 72 73 65 72 3d 3d 30 29 20  VER(pParser==0) 
31b3a 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c  ) return;.  whil
31b3b 65 28 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64  e( pParser->yyid
31b3c 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61  x>=0 ) yy_pop_pa
31b3d 72 73 65 72 5f 73 74 61 63 6b 28 70 50 61 72 73  rser_stack(pPars
31b3e 65 72 29 3b 0a 23 69 66 20 59 59 53 54 41 43 4b  er);.#if YYSTACK
31b3f 44 45 50 54 48 3c 3d 30 0a 20 20 66 72 65 65 28  DEPTH<=0.  free(
31b40 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
31b41 29 3b 0a 23 65 6e 64 69 66 0a 20 20 28 2a 66 72  );.#endif.  (*fr
31b42 65 65 50 72 6f 63 29 28 28 76 6f 69 64 2a 29 70  eeProc)((void*)p
31b43 50 61 72 73 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Parser);.}../*.*
31b44 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 65 61  * Return the pea
31b45 6b 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 73  k depth of the s
31b46 74 61 63 6b 20 66 6f 72 20 61 20 70 61 72 73 65  tack for a parse
31b47 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59 54  r..*/.#ifdef YYT
31b48 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54  RACKMAXSTACKDEPT
31b49 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  H.SQLITE_PRIVATE
31b4a 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73   int sqlite3Pars
31b4b 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64  erStackPeak(void
31b4c 20 2a 70 29 7b 0a 20 20 79 79 50 61 72 73 65 72   *p){.  yyParser
31b4d 20 2a 70 50 61 72 73 65 72 20 3d 20 28 79 79 50   *pParser = (yyP
31b4e 61 72 73 65 72 2a 29 70 3b 0a 20 20 72 65 74 75  arser*)p;.  retu
31b4f 72 6e 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64  rn pParser->yyid
31b50 78 4d 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  xMax;.}.#endif..
31b51 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 61  /*.** Find the a
31b52 70 70 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f  ppropriate actio
31b53 6e 20 66 6f 72 20 61 20 70 61 72 73 65 72 20 67  n for a parser g
31b54 69 76 65 6e 20 74 68 65 20 74 65 72 6d 69 6e 61  iven the termina
31b55 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20  l.** look-ahead 
31b56 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64  token iLookAhead
31b57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
31b58 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
31b59 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65  is YYNOCODE, the
31b5a 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  n check to see i
31b5b 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a  f the action is.
31b5c 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f  ** independent o
31b5d 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  f the look-ahead
31b5e 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72 65 74  .  If it is, ret
31b5f 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20  urn the action, 
31b60 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74  otherwise.** ret
31b61 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e  urn YY_NO_ACTION
31b62 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31b63 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61 63  yy_find_shift_ac
31b64 74 69 6f 6e 28 0a 20 20 79 79 50 61 72 73 65 72  tion(.  yyParser
31b65 20 2a 70 50 61 72 73 65 72 2c 20 20 20 20 20 20   *pParser,      
31b66 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
31b67 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  */.  YYCODETYPE 
31b68 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20 20 20 2f  iLookAhead     /
31b69 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  * The look-ahead
31b6a 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69   token */.){.  i
31b6b 6e 74 20 69 3b 0a 20 20 69 6e 74 20 73 74 61 74  nt i;.  int stat
31b6c 65 6e 6f 20 3d 20 70 50 61 72 73 65 72 2d 3e 79  eno = pParser->y
31b6d 79 73 74 61 63 6b 5b 70 50 61 72 73 65 72 2d 3e  ystack[pParser->
31b6e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e 6f 3b 0a  yyidx].stateno;.
31b6f 20 0a 20 20 69 66 28 20 73 74 61 74 65 6e 6f 3e   .  if( stateno>
31b70 59 59 5f 53 48 49 46 54 5f 43 4f 55 4e 54 0a 20  YY_SHIFT_COUNT. 
31b71 20 20 7c 7c 20 28 69 20 3d 20 79 79 5f 73 68 69    || (i = yy_shi
31b72 66 74 5f 6f 66 73 74 5b 73 74 61 74 65 6e 6f 5d  ft_ofst[stateno]
31b73 29 3d 3d 59 59 5f 53 48 49 46 54 5f 55 53 45 5f  )==YY_SHIFT_USE_
31b74 44 46 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75  DFLT ){.    retu
31b75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b 73 74  rn yy_default[st
31b76 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 20 20 61 73  ateno];.  }.  as
31b77 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68 65 61 64  sert( iLookAhead
31b78 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b 0a 20 20  !=YYNOCODE );.  
31b79 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b  i += iLookAhead;
31b7a 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e  .  if( i<0 || i>
31b7b 3d 59 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e 54  =YY_ACTTAB_COUNT
31b7c 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64   || yy_lookahead
31b7d 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20  [i]!=iLookAhead 
31b7e 29 7b 0a 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b  ){.    if( iLook
31b7f 41 68 65 61 64 3e 30 20 29 7b 0a 23 69 66 64 65  Ahead>0 ){.#ifde
31b80 66 20 59 59 46 41 4c 4c 42 41 43 4b 0a 20 20 20  f YYFALLBACK.   
31b81 20 20 20 59 59 43 4f 44 45 54 59 50 45 20 69 46     YYCODETYPE iF
31b82 61 6c 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20  allback;        
31b83 20 20 20 20 2f 2a 20 46 61 6c 6c 62 61 63 6b 20      /* Fallback 
31b84 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69  token */.      i
31b85 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3c 73 69  f( iLookAhead<si
31b86 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61 63 6b 29  zeof(yyFallback)
31b87 2f 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61  /sizeof(yyFallba
31b88 63 6b 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 20  ck[0]).         
31b89 20 20 20 20 26 26 20 28 69 46 61 6c 6c 62 61 63      && (iFallbac
31b8a 6b 20 3d 20 79 79 46 61 6c 6c 62 61 63 6b 5b 69  k = yyFallback[i
31b8b 4c 6f 6f 6b 41 68 65 61 64 5d 29 21 3d 30 20 29  LookAhead])!=0 )
31b8c 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  {.#ifndef NDEBUG
31b8d 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 79 54  .        if( yyT
31b8e 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
31b8f 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79        fprintf(yy
31b90 54 72 61 63 65 46 49 4c 45 2c 20 22 25 73 46 41  TraceFILE, "%sFA
31b91 4c 4c 42 41 43 4b 20 25 73 20 3d 3e 20 25 73 5c  LLBACK %s => %s\
31b92 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
31b93 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20   yyTracePrompt, 
31b94 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f  yyTokenName[iLoo
31b95 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e  kAhead], yyToken
31b96 4e 61 6d 65 5b 69 46 61 6c 6c 62 61 63 6b 5d 29  Name[iFallback])
31b97 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64  ;.        }.#end
31b98 69 66 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  if.        retur
31b99 6e 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f  n yy_find_shift_
31b9a 61 63 74 69 6f 6e 28 70 50 61 72 73 65 72 2c 20  action(pParser, 
31b9b 69 46 61 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20  iFallback);.    
31b9c 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65    }.#endif.#ifde
31b9d 66 20 59 59 57 49 4c 44 43 41 52 44 0a 20 20 20  f YYWILDCARD.   
31b9e 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74     {.        int
31b9f 20 6a 20 3d 20 69 20 2d 20 69 4c 6f 6f 6b 41 68   j = i - iLookAh
31ba0 65 61 64 20 2b 20 59 59 57 49 4c 44 43 41 52 44  ead + YYWILDCARD
31ba1 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0a 23  ;.        if( .#
31ba2 69 66 20 59 59 5f 53 48 49 46 54 5f 4d 49 4e 2b  if YY_SHIFT_MIN+
31ba3 59 59 57 49 4c 44 43 41 52 44 3c 30 0a 20 20 20  YYWILDCARD<0.   
31ba4 20 20 20 20 20 20 20 6a 3e 3d 30 20 26 26 0a 23         j>=0 &&.#
31ba5 65 6e 64 69 66 0a 23 69 66 20 59 59 5f 53 48 49  endif.#if YY_SHI
31ba6 46 54 5f 4d 41 58 2b 59 59 57 49 4c 44 43 41 52  FT_MAX+YYWILDCAR
31ba7 44 3e 3d 59 59 5f 41 43 54 54 41 42 5f 43 4f 55  D>=YY_ACTTAB_COU
31ba8 4e 54 0a 20 20 20 20 20 20 20 20 20 20 6a 3c 59  NT.          j<Y
31ba9 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e 54 20 26  Y_ACTTAB_COUNT &
31baa 26 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  &.#endif.       
31bab 20 20 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b     yy_lookahead[
31bac 6a 5d 3d 3d 59 59 57 49 4c 44 43 41 52 44 0a 20  j]==YYWILDCARD. 
31bad 20 20 20 20 20 20 20 29 7b 0a 23 69 66 6e 64 65         ){.#ifnde
31bae 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20  f NDEBUG.       
31baf 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49     if( yyTraceFI
31bb0 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  LE ){.          
31bb1 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
31bb2 65 46 49 4c 45 2c 20 22 25 73 57 49 4c 44 43 41  eFILE, "%sWILDCA
31bb3 52 44 20 25 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a  RD %s => %s\n",.
31bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79                 y
31bb5 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 79 79  yTracePrompt, yy
31bb6 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b 41  TokenName[iLookA
31bb7 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e 61  head], yyTokenNa
31bb8 6d 65 5b 59 59 57 49 4c 44 43 41 52 44 5d 29 3b  me[YYWILDCARD]);
31bb9 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  .          }.#en
31bba 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
31bbb 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
31bbc 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 6a 5d 3b 0a  n yy_action[j];.
31bbd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
31bbe 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 57 49  }.#endif /* YYWI
31bbf 4c 44 43 41 52 44 20 2a 2f 0a 20 20 20 20 7d 0a  LDCARD */.    }.
31bc0 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65      return yy_de
31bc1 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a  fault[stateno];.
31bc2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
31bc3 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d  urn yy_action[i]
31bc4 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ;.  }.}../*.** F
31bc5 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69  ind the appropri
31bc6 61 74 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61  ate action for a
31bc7 20 70 61 72 73 65 72 20 67 69 76 65 6e 20 74 68   parser given th
31bc8 65 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 0a 2a  e non-terminal.*
31bc9 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b  * look-ahead tok
31bca 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a  en iLookAhead..*
31bcb 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f 6b  *.** If the look
31bcc 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 73 20  -ahead token is 
31bcd 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20 63  YYNOCODE, then c
31bce 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
31bcf 68 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  he action is.** 
31bd0 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66 20 74  independent of t
31bd1 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20  he look-ahead.  
31bd2 49 66 20 69 74 20 69 73 2c 20 72 65 74 75 72 6e  If it is, return
31bd3 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74 68   the action, oth
31bd4 65 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e  erwise.** return
31bd5 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a   YY_NO_ACTION..*
31bd6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f  /.static int yy_
31bd7 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69  find_reduce_acti
31bd8 6f 6e 28 0a 20 20 69 6e 74 20 73 74 61 74 65 6e  on(.  int staten
31bd9 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o,              
31bda 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65  /* Current state
31bdb 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43   number */.  YYC
31bdc 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65  ODETYPE iLookAhe
31bdd 61 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f  ad     /* The lo
31bde 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a  ok-ahead token *
31bdf 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 23 69  /.){.  int i;.#i
31be0 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42  fdef YYERRORSYMB
31be1 4f 4c 0a 20 20 69 66 28 20 73 74 61 74 65 6e 6f  OL.  if( stateno
31be2 3e 59 59 5f 52 45 44 55 43 45 5f 43 4f 55 4e 54  >YY_REDUCE_COUNT
31be3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79   ){.    return y
31be4 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e  y_default[staten
31be5 6f 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  o];.  }.#else.  
31be6 61 73 73 65 72 74 28 20 73 74 61 74 65 6e 6f 3c  assert( stateno<
31be7 3d 59 59 5f 52 45 44 55 43 45 5f 43 4f 55 4e 54  =YY_REDUCE_COUNT
31be8 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 20 3d   );.#endif.  i =
31be9 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b   yy_reduce_ofst[
31bea 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 61 73 73 65  stateno];.  asse
31beb 72 74 28 20 69 21 3d 59 59 5f 52 45 44 55 43 45  rt( i!=YY_REDUCE
31bec 5f 55 53 45 5f 44 46 4c 54 20 29 3b 0a 20 20 61  _USE_DFLT );.  a
31bed 73 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68 65 61  ssert( iLookAhea
31bee 64 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b 0a 20  d!=YYNOCODE );. 
31bef 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64   i += iLookAhead
31bf0 3b 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52  ;.#ifdef YYERROR
31bf1 53 59 4d 42 4f 4c 0a 20 20 69 66 28 20 69 3c 30  SYMBOL.  if( i<0
31bf2 20 7c 7c 20 69 3e 3d 59 59 5f 41 43 54 54 41 42   || i>=YY_ACTTAB
31bf3 5f 43 4f 55 4e 54 20 7c 7c 20 79 79 5f 6c 6f 6f  _COUNT || yy_loo
31bf4 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b  kahead[i]!=iLook
31bf5 41 68 65 61 64 20 29 7b 0a 20 20 20 20 72 65 74  Ahead ){.    ret
31bf6 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b 73  urn yy_default[s
31bf7 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 23 65 6c  tateno];.  }.#el
31bf8 73 65 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  se.  assert( i>=
31bf9 30 20 26 26 20 69 3c 59 59 5f 41 43 54 54 41 42  0 && i<YY_ACTTAB
31bfa 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 61 73 73 65  _COUNT );.  asse
31bfb 72 74 28 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64  rt( yy_lookahead
31bfc 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41 68 65 61 64 20  [i]==iLookAhead 
31bfd 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  );.#endif.  retu
31bfe 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b  rn yy_action[i];
31bff 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
31c00 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
31c01 69 73 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65  is called if the
31c02 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 73   stack overflows
31c03 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
31c04 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77   yyStackOverflow
31c05 28 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61  (yyParser *yypPa
31c06 72 73 65 72 2c 20 59 59 4d 49 4e 4f 52 54 59 50  rser, YYMINORTYP
31c07 45 20 2a 79 79 70 4d 69 6e 6f 72 29 7b 0a 20 20  E *yypMinor){.  
31c08 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
31c09 47 5f 46 45 54 43 48 3b 0a 20 20 20 79 79 70 50  G_FETCH;.   yypP
31c0a 61 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a  arser->yyidx--;.
31c0b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
31c0c 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
31c0d 45 20 29 7b 0a 20 20 20 20 20 66 70 72 69 6e 74  E ){.     fprint
31c0e 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
31c0f 73 53 74 61 63 6b 20 4f 76 65 72 66 6c 6f 77 21  sStack Overflow!
31c10 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70  \n",yyTracePromp
31c11 74 29 3b 0a 20 20 20 7d 0a 23 65 6e 64 69 66 0a  t);.   }.#endif.
31c12 20 20 20 77 68 69 6c 65 28 20 79 79 70 50 61 72     while( yypPar
31c13 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20  ser->yyidx>=0 ) 
31c14 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74  yy_pop_parser_st
31c15 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a  ack(yypParser);.
31c16 20 20 20 2f 2a 20 48 65 72 65 20 63 6f 64 65 20     /* Here code 
31c17 69 73 20 69 6e 73 65 72 74 65 64 20 77 68 69 63  is inserted whic
31c18 68 20 77 69 6c 6c 20 65 78 65 63 75 74 65 20 69  h will execute i
31c19 66 20 74 68 65 20 70 61 72 73 65 72 0a 20 20 20  f the parser.   
31c1a 2a 2a 20 73 74 61 63 6b 20 65 76 65 72 79 20 6f  ** stack every o
31c1b 76 65 72 66 6c 6f 77 73 20 2a 2f 0a 0a 20 20 55  verflows */..  U
31c1c 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
31c1d 79 79 70 4d 69 6e 6f 72 29 3b 20 2f 2a 20 53 69  yypMinor); /* Si
31c1e 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69  lence some compi
31c1f 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0a  ler warnings */.
31c20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
31c21 67 28 70 50 61 72 73 65 2c 20 22 70 61 72 73 65  g(pParse, "parse
31c22 72 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  r stack overflow
31c23 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61  ");.  pParse->pa
31c24 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20  rseError = 1;.  
31c25 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
31c26 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70  G_STORE; /* Supp
31c27 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f  ress warning abo
31c28 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61  ut unused %extra
31c29 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 20 2a 2f  _argument var */
31c2a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  .}../*.** Perfor
31c2b 6d 20 61 20 73 68 69 66 74 20 61 63 74 69 6f 6e  m a shift action
31c2c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
31c2d 20 79 79 5f 73 68 69 66 74 28 0a 20 20 79 79 50   yy_shift(.  yyP
31c2e 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
31c2f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
31c30 65 20 70 61 72 73 65 72 20 74 6f 20 62 65 20 73  e parser to be s
31c31 68 69 66 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  hifted */.  int 
31c32 79 79 4e 65 77 53 74 61 74 65 2c 20 20 20 20 20  yyNewState,     
31c33 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
31c34 20 6e 65 77 20 73 74 61 74 65 20 74 6f 20 73 68   new state to sh
31c35 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  ift in */.  int 
31c36 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20  yyMajor,        
31c37 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
31c38 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 74 6f 20   major token to 
31c39 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 59 59  shift in */.  YY
31c3a 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69  MINORTYPE *yypMi
31c3b 6e 6f 72 20 20 20 20 20 20 20 20 20 2f 2a 20 50  nor         /* P
31c3c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 69  ointer to the mi
31c3d 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68 69  nor token to shi
31c3e 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20 79 79  ft in */.){.  yy
31c3f 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f  StackEntry *yyto
31c40 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e  s;.  yypParser->
31c41 79 79 69 64 78 2b 2b 3b 0a 23 69 66 64 65 66 20  yyidx++;.#ifdef 
31c42 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
31c43 45 50 54 48 0a 20 20 69 66 28 20 79 79 70 50 61  EPTH.  if( yypPa
31c44 72 73 65 72 2d 3e 79 79 69 64 78 3e 79 79 70 50  rser->yyidx>yypP
31c45 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20  arser->yyidxMax 
31c46 29 7b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72  ){.    yypParser
31c47 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20 79 79 70  ->yyidxMax = yyp
31c48 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 0a 20  Parser->yyidx;. 
31c49 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59   }.#endif.#if YY
31c4a 53 54 41 43 4b 44 45 50 54 48 3e 30 20 0a 20 20  STACKDEPTH>0 .  
31c4b 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
31c4c 79 69 64 78 3e 3d 59 59 53 54 41 43 4b 44 45 50  yidx>=YYSTACKDEP
31c4d 54 48 20 29 7b 0a 20 20 20 20 79 79 53 74 61 63  TH ){.    yyStac
31c4e 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72  kOverflow(yypPar
31c4f 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a  ser, yypMinor);.
31c50 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
31c51 23 65 6c 73 65 0a 20 20 69 66 28 20 79 79 70 50  #else.  if( yypP
31c52 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79  arser->yyidx>=yy
31c53 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a  pParser->yystksz
31c54 20 29 7b 0a 20 20 20 20 79 79 47 72 6f 77 53 74   ){.    yyGrowSt
31c55 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a  ack(yypParser);.
31c56 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65      if( yypParse
31c57 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50 61 72  r->yyidx>=yypPar
31c58 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0a  ser->yystksz ){.
31c59 20 20 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65        yyStackOve
31c5a 72 66 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c  rflow(yypParser,
31c5b 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20 20 20   yypMinor);.    
31c5c 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
31c5d 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 74    }.#endif.  yyt
31c5e 6f 73 20 3d 20 26 79 79 70 50 61 72 73 65 72 2d  os = &yypParser-
31c5f 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73  >yystack[yypPars
31c60 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20 79 79  er->yyidx];.  yy
31c61 74 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 28  tos->stateno = (
31c62 59 59 41 43 54 49 4f 4e 54 59 50 45 29 79 79 4e  YYACTIONTYPE)yyN
31c63 65 77 53 74 61 74 65 3b 0a 20 20 79 79 74 6f 73  ewState;.  yytos
31c64 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59 59 43 4f 44  ->major = (YYCOD
31c65 45 54 59 50 45 29 79 79 4d 61 6a 6f 72 3b 0a 20  ETYPE)yyMajor;. 
31c66 20 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72 20 3d 20   yytos->minor = 
31c67 2a 79 79 70 4d 69 6e 6f 72 3b 0a 23 69 66 6e 64  *yypMinor;.#ifnd
31c68 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
31c69 79 79 54 72 61 63 65 46 49 4c 45 20 26 26 20 79  yyTraceFILE && y
31c6a 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
31c6b 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
31c6c 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
31c6d 61 63 65 46 49 4c 45 2c 22 25 73 53 68 69 66 74  aceFILE,"%sShift
31c6e 20 25 64 5c 6e 22 2c 79 79 54 72 61 63 65 50 72   %d\n",yyTracePr
31c6f 6f 6d 70 74 2c 79 79 4e 65 77 53 74 61 74 65 29  ompt,yyNewState)
31c70 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79  ;.    fprintf(yy
31c71 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 74 61  TraceFILE,"%sSta
31c72 63 6b 3a 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  ck:",yyTraceProm
31c73 70 74 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31  pt);.    for(i=1
31c74 3b 20 69 3c 3d 79 79 70 50 61 72 73 65 72 2d 3e  ; i<=yypParser->
31c75 79 79 69 64 78 3b 20 69 2b 2b 29 0a 20 20 20 20  yyidx; i++).    
31c76 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
31c77 65 46 49 4c 45 2c 22 20 25 73 22 2c 79 79 54 6f  eFILE," %s",yyTo
31c78 6b 65 6e 4e 61 6d 65 5b 79 79 70 50 61 72 73 65  kenName[yypParse
31c79 72 2d 3e 79 79 73 74 61 63 6b 5b 69 5d 2e 6d 61  r->yystack[i].ma
31c7a 6a 6f 72 5d 29 3b 0a 20 20 20 20 66 70 72 69 6e  jor]);.    fprin
31c7b 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
31c7c 5c 6e 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  \n");.  }.#endif
31c7d 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  .}../* The follo
31c7e 77 69 6e 67 20 74 61 62 6c 65 20 63 6f 6e 74 61  wing table conta
31c7f 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ins information 
31c80 61 62 6f 75 74 20 65 76 65 72 79 20 72 75 6c 65  about every rule
31c81 20 74 68 61 74 0a 2a 2a 20 69 73 20 75 73 65 64   that.** is used
31c82 20 64 75 72 69 6e 67 20 74 68 65 20 72 65 64 75   during the redu
31c83 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  ce..*/.static co
31c84 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 59  nst struct {.  Y
31c85 59 43 4f 44 45 54 59 50 45 20 6c 68 73 3b 20 20  YCODETYPE lhs;  
31c86 20 20 20 20 20 20 20 2f 2a 20 53 79 6d 62 6f 6c         /* Symbol
31c87 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   on the left-han
31c88 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 75  d side of the ru
31c89 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  le */.  unsigned
31c8a 20 63 68 61 72 20 6e 72 68 73 3b 20 20 20 20 20   char nrhs;     
31c8b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 69 67  /* Number of rig
31c8c 68 74 2d 68 61 6e 64 20 73 69 64 65 20 73 79 6d  ht-hand side sym
31c8d 62 6f 6c 73 20 69 6e 20 74 68 65 20 72 75 6c 65  bols in the rule
31c8e 20 2a 2f 0a 7d 20 79 79 52 75 6c 65 49 6e 66 6f   */.} yyRuleInfo
31c8f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 31 34 32 2c 20  [] = {.  { 142, 
31c90 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32 20  1 },.  { 143, 2 
31c91 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31 20 7d 2c  },.  { 143, 1 },
31c92 0a 20 20 7b 20 31 34 34 2c 20 31 20 7d 2c 0a 20  .  { 144, 1 },. 
31c93 20 7b 20 31 34 34 2c 20 33 20 7d 2c 0a 20 20 7b   { 144, 3 },.  {
31c94 20 31 34 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   145, 0 },.  { 1
31c95 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 35  45, 1 },.  { 145
31c96 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20  , 3 },.  { 146, 
31c97 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20  1 },.  { 147, 3 
31c98 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 30 20 7d 2c  },.  { 149, 0 },
31c99 0a 20 20 7b 20 31 34 39 2c 20 31 20 7d 2c 0a 20  .  { 149, 1 },. 
31c9a 20 7b 20 31 34 39 2c 20 32 20 7d 2c 0a 20 20 7b   { 149, 2 },.  {
31c9b 20 31 34 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   148, 0 },.  { 1
31c9c 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38  48, 1 },.  { 148
31c9d 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20  , 1 },.  { 148, 
31c9e 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20  1 },.  { 147, 2 
31c9f 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c  },.  { 147, 2 },
31ca0 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20  .  { 147, 2 },. 
31ca1 20 7b 20 31 35 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 151, 1 },.  {
31ca2 20 31 35 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   151, 0 },.  { 1
31ca3 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 2 },.  { 147
31ca4 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 3 },.  { 147, 
31ca5 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20  5 },.  { 147, 2 
31ca6 7d 2c 0a 20 20 7b 20 31 35 32 2c 20 36 20 7d 2c  },.  { 152, 6 },
31ca7 0a 20 20 7b 20 31 35 34 2c 20 31 20 7d 2c 0a 20  .  { 154, 1 },. 
31ca8 20 7b 20 31 35 36 2c 20 30 20 7d 2c 0a 20 20 7b   { 156, 0 },.  {
31ca9 20 31 35 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   156, 3 },.  { 1
31caa 35 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 35  55, 1 },.  { 155
31cab 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20  , 0 },.  { 153, 
31cac 34 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 32 20  4 },.  { 153, 2 
31cad 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 33 20 7d 2c  },.  { 158, 3 },
31cae 0a 20 20 7b 20 31 35 38 2c 20 31 20 7d 2c 0a 20  .  { 158, 1 },. 
31caf 20 7b 20 31 36 31 2c 20 33 20 7d 2c 0a 20 20 7b   { 161, 3 },.  {
31cb0 20 31 36 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   162, 1 },.  { 1
31cb1 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 35  65, 1 },.  { 165
31cb2 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 36 2c 20  , 1 },.  { 166, 
31cb3 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20  1 },.  { 150, 1 
31cb4 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c  },.  { 150, 1 },
31cb5 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20  .  { 150, 1 },. 
31cb6 20 7b 20 31 36 33 2c 20 30 20 7d 2c 0a 20 20 7b   { 163, 0 },.  {
31cb7 20 31 36 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   163, 1 },.  { 1
31cb8 36 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 37  67, 1 },.  { 167
31cb9 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36 37 2c 20  , 4 },.  { 167, 
31cba 36 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 31 20  6 },.  { 168, 1 
31cbb 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c  },.  { 168, 2 },
31cbc 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20  .  { 169, 1 },. 
31cbd 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20 7b   { 169, 1 },.  {
31cbe 20 31 36 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   164, 2 },.  { 1
31cbf 36 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 32  64, 0 },.  { 172
31cc0 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20  , 3 },.  { 172, 
31cc1 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20  1 },.  { 173, 2 
31cc2 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d 2c  },.  { 173, 4 },
31cc3 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20  .  { 173, 3 },. 
31cc4 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20 20 7b   { 173, 3 },.  {
31cc5 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   173, 2 },.  { 1
31cc6 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33  73, 2 },.  { 173
31cc7 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 3 },.  { 173, 
31cc8 35 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20  5 },.  { 173, 2 
31cc9 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d 2c  },.  { 173, 4 },
31cca 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d 2c 0a 20  .  { 173, 4 },. 
31ccb 20 7b 20 31 37 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 173, 1 },.  {
31ccc 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   173, 2 },.  { 1
31ccd 37 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 38  78, 0 },.  { 178
31cce 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20  , 1 },.  { 180, 
31ccf 30 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 32 20  0 },.  { 180, 2 
31cd0 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 32 20 7d 2c  },.  { 182, 2 },
31cd1 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20  .  { 182, 3 },. 
31cd2 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20 20 7b   { 182, 3 },.  {
31cd3 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   183, 2 },.  { 1
31cd4 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 33  83, 2 },.  { 183
31cd5 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20  , 1 },.  { 183, 
31cd6 31 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 32 20  1 },.  { 183, 2 
31cd7 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 33 20 7d 2c  },.  { 181, 3 },
31cd8 0a 20 20 7b 20 31 38 31 2c 20 32 20 7d 2c 0a 20  .  { 181, 2 },. 
31cd9 20 7b 20 31 38 34 2c 20 30 20 7d 2c 0a 20 20 7b   { 184, 0 },.  {
31cda 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   184, 2 },.  { 1
31cdb 38 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 39  84, 2 },.  { 159
31cdc 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 39 2c 20  , 0 },.  { 159, 
31cdd 32 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 33 20  2 },.  { 185, 3 
31cde 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 32 20 7d 2c  },.  { 185, 2 },
31cdf 0a 20 20 7b 20 31 38 35 2c 20 31 20 7d 2c 0a 20  .  { 185, 1 },. 
31ce0 20 7b 20 31 38 36 2c 20 32 20 7d 2c 0a 20 20 7b   { 186, 2 },.  {
31ce1 20 31 38 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 31   186, 7 },.  { 1
31ce2 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 36  86, 5 },.  { 186
31ce3 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20  , 5 },.  { 186, 
31ce4 31 30 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 30  10 },.  { 188, 0
31ce5 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 31 20 7d   },.  { 188, 1 }
31ce6 2c 0a 20 20 7b 20 31 37 36 2c 20 30 20 7d 2c 0a  ,.  { 176, 0 },.
31ce7 20 20 7b 20 31 37 36 2c 20 33 20 7d 2c 0a 20 20    { 176, 3 },.  
31ce8 7b 20 31 38 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 189, 0 },.  { 
31ce9 31 38 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39  189, 2 },.  { 19
31cea 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c  0, 1 },.  { 190,
31ceb 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31   1 },.  { 190, 1
31cec 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d   },.  { 147, 4 }
31ced 2c 0a 20 20 7b 20 31 39 32 2c 20 32 20 7d 2c 0a  ,.  { 192, 2 },.
31cee 20 20 7b 20 31 39 32 2c 20 30 20 7d 2c 0a 20 20    { 192, 0 },.  
31cef 7b 20 31 34 37 2c 20 38 20 7d 2c 0a 20 20 7b 20  { 147, 8 },.  { 
31cf0 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34  147, 4 },.  { 14
31cf1 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 30 2c  7, 1 },.  { 160,
31cf2 20 31 20 7d 2c 0a 20 20 7b 20 31 36 30 2c 20 33   1 },.  { 160, 3
31cf3 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 31 20 7d   },.  { 195, 1 }
31cf4 2c 0a 20 20 7b 20 31 39 35 2c 20 32 20 7d 2c 0a  ,.  { 195, 2 },.
31cf5 20 20 7b 20 31 39 35 2c 20 31 20 7d 2c 0a 20 20    { 195, 1 },.  
31cf6 7b 20 31 39 34 2c 20 39 20 7d 2c 0a 20 20 7b 20  { 194, 9 },.  { 
31cf7 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39  196, 1 },.  { 19
31cf8 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 36 2c  6, 1 },.  { 196,
31cf9 20 30 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 32   0 },.  { 204, 2
31cfa 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 30 20 7d   },.  { 204, 0 }
31cfb 2c 0a 20 20 7b 20 31 39 37 2c 20 33 20 7d 2c 0a  ,.  { 197, 3 },.
31cfc 20 20 7b 20 31 39 37 2c 20 32 20 7d 2c 0a 20 20    { 197, 2 },.  
31cfd 7b 20 31 39 37 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 197, 4 },.  { 
31cfe 32 30 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30  205, 2 },.  { 20
31cff 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 30 35 2c  5, 1 },.  { 205,
31d00 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 30   0 },.  { 198, 0
31d01 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 32 20 7d   },.  { 198, 2 }
31d02 2c 0a 20 20 7b 20 32 30 37 2c 20 32 20 7d 2c 0a  ,.  { 207, 2 },.
31d03 20 20 7b 20 32 30 37 2c 20 30 20 7d 2c 0a 20 20    { 207, 0 },.  
31d04 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20  { 206, 7 },.  { 
31d05 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 30  206, 7 },.  { 20
31d06 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 31 35 37 2c  6, 7 },.  { 157,
31d07 20 30 20 7d 2c 0a 20 20 7b 20 31 35 37 2c 20 32   0 },.  { 157, 2
31d08 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20 32 20 7d   },.  { 193, 2 }
31d09 2c 0a 20 20 7b 20 32 30 38 2c 20 31 20 7d 2c 0a  ,.  { 208, 1 },.
31d0a 20 20 7b 20 32 30 38 2c 20 32 20 7d 2c 0a 20 20    { 208, 2 },.  
31d0b 7b 20 32 30 38 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 208, 3 },.  { 
31d0c 32 30 38 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31  208, 4 },.  { 21
31d0d 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 30 2c  0, 2 },.  { 210,
31d0e 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 30   0 },.  { 209, 0
31d0f 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 33 20 7d   },.  { 209, 3 }
31d10 2c 0a 20 20 7b 20 32 30 39 2c 20 32 20 7d 2c 0a  ,.  { 209, 2 },.
31d11 20 20 7b 20 32 31 31 2c 20 34 20 7d 2c 0a 20 20    { 211, 4 },.  
31d12 7b 20 32 31 31 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 211, 0 },.  { 
31d13 32 30 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30  202, 0 },.  { 20
31d14 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 34 2c  2, 3 },.  { 214,
31d15 20 34 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 32   4 },.  { 214, 2
31d16 20 7d 2c 0a 20 20 7b 20 32 31 35 2c 20 31 20 7d   },.  { 215, 1 }
31d17 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20 7d 2c 0a  ,.  { 177, 1 },.
31d18 20 20 7b 20 31 37 37 2c 20 31 20 7d 2c 0a 20 20    { 177, 1 },.  
31d19 7b 20 31 37 37 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 177, 0 },.  { 
31d1a 32 30 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30  200, 0 },.  { 20
31d1b 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 31 2c  0, 3 },.  { 201,
31d1c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 31 2c 20 32   0 },.  { 201, 2
31d1d 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 30 20 7d   },.  { 203, 0 }
31d1e 2c 0a 20 20 7b 20 32 30 33 2c 20 32 20 7d 2c 0a  ,.  { 203, 2 },.
31d1f 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20    { 203, 4 },.  
31d20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 203, 4 },.  { 
31d21 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 39  147, 5 },.  { 19
31d22 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39 2c  9, 0 },.  { 199,
31d23 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 37   2 },.  { 147, 7
31d24 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 35 20 7d   },.  { 217, 5 }
31d25 2c 0a 20 20 7b 20 32 31 37 2c 20 33 20 7d 2c 0a  ,.  { 217, 3 },.
31d26 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c 0a 20 20    { 147, 8 },.  
31d27 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 147, 5 },.  { 
31d28 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 31  147, 6 },.  { 21
31d29 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 38 2c  8, 2 },.  { 218,
31d2a 20 31 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 33   1 },.  { 220, 3
31d2b 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 31 20 7d   },.  { 220, 1 }
31d2c 2c 0a 20 20 7b 20 32 31 39 2c 20 30 20 7d 2c 0a  ,.  { 219, 0 },.
31d2d 20 20 7b 20 32 31 39 2c 20 33 20 7d 2c 0a 20 20    { 219, 3 },.  
31d2e 7b 20 32 31 33 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 213, 3 },.  { 
31d2f 32 31 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  213, 1 },.  { 17
31d30 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 1 },.  { 175,
31d31 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31   3 },.  { 174, 1
31d32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d   },.  { 175, 1 }
31d33 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a  ,.  { 175, 1 },.
31d34 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
31d35 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 175, 5 },.  { 
31d36 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  174, 1 },.  { 17
31d37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  4, 1 },.  { 175,
31d38 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31   1 },.  { 175, 1
31d39 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d   },.  { 175, 3 }
31d3a 2c 0a 20 20 7b 20 31 37 35 2c 20 36 20 7d 2c 0a  ,.  { 175, 6 },.
31d3b 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20    { 175, 5 },.  
31d3c 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 175, 4 },.  { 
31d3d 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  174, 1 },.  { 17
31d3e 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 3 },.  { 175,
31d3f 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   3 },.  { 175, 3
31d40 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d   },.  { 175, 3 }
31d41 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
31d42 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
31d43 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
31d44 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32  175, 3 },.  { 22
31d45 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 32 2c  2, 1 },.  { 222,
31d46 20 32 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 31   2 },.  { 222, 1
31d47 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 32 20 7d   },.  { 222, 2 }
31d48 2c 0a 20 20 7b 20 32 32 33 2c 20 32 20 7d 2c 0a  ,.  { 223, 2 },.
31d49 20 20 7b 20 32 32 33 2c 20 30 20 7d 2c 0a 20 20    { 223, 0 },.  
31d4a 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 175, 4 },.  { 
31d4b 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  175, 2 },.  { 17
31d4c 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 3 },.  { 175,
31d4d 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34   3 },.  { 175, 4
31d4e 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d   },.  { 175, 2 }
31d4f 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a  ,.  { 175, 2 },.
31d50 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20    { 175, 2 },.  
31d51 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 175, 2 },.  { 
31d52 32 32 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32  224, 1 },.  { 22
31d53 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  4, 2 },.  { 175,
31d54 20 35 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 31   5 },.  { 225, 1
31d55 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 32 20 7d   },.  { 225, 2 }
31d56 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a  ,.  { 175, 5 },.
31d57 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
31d58 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 175, 5 },.  { 
31d59 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37  175, 4 },.  { 17
31d5a 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 4 },.  { 175,
31d5b 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 35   5 },.  { 227, 5
31d5c 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 34 20 7d   },.  { 227, 4 }
31d5d 2c 0a 20 20 7b 20 32 32 38 2c 20 32 20 7d 2c 0a  ,.  { 228, 2 },.
31d5e 20 20 7b 20 32 32 38 2c 20 30 20 7d 2c 0a 20 20    { 228, 0 },.  
31d5f 7b 20 32 32 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 226, 1 },.  { 
31d60 32 32 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32  226, 0 },.  { 22
31d61 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 31 2c  1, 1 },.  { 221,
31d62 20 30 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 33   0 },.  { 216, 3
31d63 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 31 20 7d   },.  { 216, 1 }
31d64 2c 0a 20 20 7b 20 31 34 37 2c 20 31 31 20 7d 2c  ,.  { 147, 11 },
31d65 0a 20 20 7b 20 32 32 39 2c 20 31 20 7d 2c 0a 20  .  { 229, 1 },. 
31d66 20 7b 20 32 32 39 2c 20 30 20 7d 2c 0a 20 20 7b   { 229, 0 },.  {
31d67 20 31 37 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   179, 0 },.  { 1
31d68 37 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 37  79, 3 },.  { 187
31d69 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20  , 5 },.  { 187, 
31d6a 33 20 7d 2c 0a 20 20 7b 20 32 33 30 2c 20 30 20  3 },.  { 230, 0 
31d6b 7d 2c 0a 20 20 7b 20 32 33 30 2c 20 32 20 7d 2c  },.  { 230, 2 },
31d6c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20  .  { 147, 4 },. 
31d6d 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 147, 1 },.  {
31d6e 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   147, 2 },.  { 1
31d6f 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 3 },.  { 147
31d70 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 5 },.  { 147, 
31d71 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20  6 },.  { 147, 5 
31d72 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c  },.  { 147, 6 },
31d73 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20  .  { 231, 1 },. 
31d74 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 231, 1 },.  {
31d75 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   231, 1 },.  { 2
31d76 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 31  31, 1 },.  { 231
31d77 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 1 },.  { 170, 
31d78 32 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 32 20  2 },.  { 171, 2 
31d79 7d 2c 0a 20 20 7b 20 32 33 33 2c 20 31 20 7d 2c  },.  { 233, 1 },
31d7a 0a 20 20 7b 20 32 33 32 2c 20 31 20 7d 2c 0a 20  .  { 232, 1 },. 
31d7b 20 7b 20 32 33 32 2c 20 30 20 7d 2c 0a 20 20 7b   { 232, 0 },.  {
31d7c 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 32   147, 5 },.  { 2
31d7d 33 34 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32 33  34, 11 },.  { 23
31d7e 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 36 2c  6, 1 },.  { 236,
31d7f 20 31 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 32   1 },.  { 236, 2
31d80 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 30 20 7d   },.  { 236, 0 }
31d81 2c 0a 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c 0a  ,.  { 237, 1 },.
31d82 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20    { 237, 1 },.  
31d83 7b 20 32 33 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 237, 3 },.  { 
31d84 32 33 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33  238, 0 },.  { 23
31d85 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 39 2c  8, 3 },.  { 239,
31d86 20 30 20 7d 2c 0a 20 20 7b 20 32 33 39 2c 20 32   0 },.  { 239, 2
31d87 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 33 20 7d   },.  { 235, 3 }
31d88 2c 0a 20 20 7b 20 32 33 35 2c 20 32 20 7d 2c 0a  ,.  { 235, 2 },.
31d89 20 20 7b 20 32 34 31 2c 20 31 20 7d 2c 0a 20 20    { 241, 1 },.  
31d8a 7b 20 32 34 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 241, 3 },.  { 
31d8b 32 34 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34  242, 0 },.  { 24
31d8c 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 32 2c  2, 3 },.  { 242,
31d8d 20 32 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 37   2 },.  { 240, 7
31d8e 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 38 20 7d   },.  { 240, 8 }
31d8f 2c 0a 20 20 7b 20 32 34 30 2c 20 35 20 7d 2c 0a  ,.  { 240, 5 },.
31d90 20 20 7b 20 32 34 30 2c 20 35 20 7d 2c 0a 20 20    { 240, 5 },.  
31d91 7b 20 32 34 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 240, 1 },.  { 
31d92 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37  175, 4 },.  { 17
31d93 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 39 31 2c  5, 6 },.  { 191,
31d94 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31   1 },.  { 191, 1
31d95 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31 20 7d   },.  { 191, 1 }
31d96 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a  ,.  { 147, 4 },.
31d97 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20    { 147, 6 },.  
31d98 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
31d99 32 34 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34  244, 0 },.  { 24
31d9a 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34 33 2c  4, 2 },.  { 243,
31d9b 20 31 20 7d 2c 0a 20 20 7b 20 32 34 33 2c 20 30   1 },.  { 243, 0
31d9c 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d   },.  { 147, 1 }
31d9d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a  ,.  { 147, 3 },.
31d9e 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20    { 147, 1 },.  
31d9f 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
31da0 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34  147, 6 },.  { 14
31da1 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 34 35 2c  7, 6 },.  { 245,
31da2 20 31 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 30   1 },.  { 246, 0
31da3 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 31 20 7d   },.  { 246, 1 }
31da4 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a  ,.  { 147, 1 },.
31da5 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20    { 147, 4 },.  
31da6 7b 20 32 34 37 2c 20 37 20 7d 2c 0a 20 20 7b 20  { 247, 7 },.  { 
31da7 32 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34  248, 1 },.  { 24
31da8 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 39 2c  8, 3 },.  { 249,
31da9 20 30 20 7d 2c 0a 20 20 7b 20 32 34 39 2c 20 32   0 },.  { 249, 2
31daa 20 7d 2c 0a 20 20 7b 20 32 35 30 2c 20 31 20 7d   },.  { 250, 1 }
31dab 2c 0a 20 20 7b 20 32 35 30 2c 20 33 20 7d 2c 0a  ,.  { 250, 3 },.
31dac 20 20 7b 20 32 35 31 2c 20 31 20 7d 2c 0a 20 20    { 251, 1 },.  
31dad 7b 20 32 35 32 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 252, 0 },.  { 
31dae 32 35 32 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 35  252, 4 },.  { 25
31daf 32 2c 20 32 20 7d 2c 0a 7d 3b 0a 0a 73 74 61 74  2, 2 },.};..stat
31db0 69 63 20 76 6f 69 64 20 79 79 5f 61 63 63 65 70  ic void yy_accep
31db1 74 28 79 79 50 61 72 73 65 72 2a 29 3b 20 20 2f  t(yyParser*);  /
31db2 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61 72  * Forward Declar
31db3 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ation */../*.** 
31db4 50 65 72 66 6f 72 6d 20 61 20 72 65 64 75 63 65  Perform a reduce
31db5 20 61 63 74 69 6f 6e 20 61 6e 64 20 74 68 65 20   action and the 
31db6 73 68 69 66 74 20 74 68 61 74 20 6d 75 73 74 20  shift that must 
31db7 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 66  immediately.** f
31db8 6f 6c 6c 6f 77 20 74 68 65 20 72 65 64 75 63 65  ollow the reduce
31db9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
31dba 20 79 79 5f 72 65 64 75 63 65 28 0a 20 20 79 79   yy_reduce(.  yy
31dbb 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65  Parser *yypParse
31dbc 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  r,         /* Th
31dbd 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e  e parser */.  in
31dbe 74 20 79 79 72 75 6c 65 6e 6f 20 20 20 20 20 20  t yyruleno      
31dbf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
31dc0 6d 62 65 72 20 6f 66 20 74 68 65 20 72 75 6c 65  mber of the rule
31dc1 20 62 79 20 77 68 69 63 68 20 74 6f 20 72 65 64   by which to red
31dc2 75 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  uce */.){.  int 
31dc3 79 79 67 6f 74 6f 3b 20 20 20 20 20 20 20 20 20  yygoto;         
31dc4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
31dc5 68 65 20 6e 65 78 74 20 73 74 61 74 65 20 2a 2f  he next state */
31dc6 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20 20  .  int yyact;   
31dc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31dc8 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 61     /* The next a
31dc9 63 74 69 6f 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e  ction */.  YYMIN
31dca 4f 52 54 59 50 45 20 79 79 67 6f 74 6f 6d 69 6e  ORTYPE yygotomin
31dcb 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  or;        /* Th
31dcc 65 20 4c 48 53 20 6f 66 20 74 68 65 20 72 75 6c  e LHS of the rul
31dcd 65 20 72 65 64 75 63 65 64 20 2a 2f 0a 20 20 79  e reduced */.  y
31dce 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 6d  yStackEntry *yym
31dcf 73 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  sp;            /
31dd0 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68 65  * The top of the
31dd1 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20   parser's stack 
31dd2 2a 2f 0a 20 20 69 6e 74 20 79 79 73 69 7a 65 3b  */.  int yysize;
31dd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31dd4 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 74       /* Amount t
31dd5 6f 20 70 6f 70 20 74 68 65 20 73 74 61 63 6b 20  o pop the stack 
31dd6 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
31dd7 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 79  erARG_FETCH;.  y
31dd8 79 6d 73 70 20 3d 20 26 79 79 70 50 61 72 73 65  ymsp = &yypParse
31dd9 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61  r->yystack[yypPa
31dda 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 23 69  rser->yyidx];.#i
31ddb 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
31ddc 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 26  f( yyTraceFILE &
31ddd 26 20 79 79 72 75 6c 65 6e 6f 3e 3d 30 20 0a 20  & yyruleno>=0 . 
31dde 20 20 20 20 20 20 20 26 26 20 79 79 72 75 6c 65         && yyrule
31ddf 6e 6f 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28  no<(int)(sizeof(
31de0 79 79 52 75 6c 65 4e 61 6d 65 29 2f 73 69 7a 65  yyRuleName)/size
31de1 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65 5b 30 5d  of(yyRuleName[0]
31de2 29 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  )) ){.    fprint
31de3 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22  f(yyTraceFILE, "
31de4 25 73 52 65 64 75 63 65 20 5b 25 73 5d 2e 5c 6e  %sReduce [%s].\n
31de5 22 2c 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74  ", yyTracePrompt
31de6 2c 0a 20 20 20 20 20 20 79 79 52 75 6c 65 4e 61  ,.      yyRuleNa
31de7 6d 65 5b 79 79 72 75 6c 65 6e 6f 5d 29 3b 0a 20  me[yyruleno]);. 
31de8 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45   }.#endif /* NDE
31de9 42 55 47 20 2a 2f 0a 0a 20 20 2f 2a 20 53 69 6c  BUG */..  /* Sil
31dea 65 6e 63 65 20 63 6f 6d 70 6c 61 69 6e 74 73 20  ence complaints 
31deb 66 72 6f 6d 20 70 75 72 69 66 79 20 61 62 6f 75  from purify abou
31dec 74 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 62 65  t yygotominor be
31ded 69 6e 67 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65  ing uninitialize
31dee 64 0a 20 20 2a 2a 20 69 6e 20 73 6f 6d 65 20 63  d.  ** in some c
31def 61 73 65 73 20 77 68 65 6e 20 69 74 20 69 73 20  ases when it is 
31df0 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20  copied into the 
31df1 73 74 61 63 6b 20 61 66 74 65 72 20 74 68 65 20  stack after the 
31df2 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73  following.  ** s
31df3 77 69 74 63 68 2e 20 20 79 79 67 6f 74 6f 6d 69  witch.  yygotomi
31df4 6e 6f 72 20 69 73 20 75 6e 69 6e 69 74 69 61 6c  nor is uninitial
31df5 69 7a 65 64 20 77 68 65 6e 20 61 20 72 75 6c 65  ized when a rule
31df6 20 72 65 64 75 63 65 73 20 74 68 61 74 20 64 6f   reduces that do
31df7 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 74 20  es.  ** not set 
31df8 74 68 65 20 76 61 6c 75 65 20 6f 66 20 69 74 73  the value of its
31df9 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
31dfa 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 20 20 4c 65  nonterminal.  Le
31dfb 61 76 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76  aving the.  ** v
31dfc 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 6f 6e 74  alue of the nont
31dfd 65 72 6d 69 6e 61 6c 20 75 6e 69 6e 69 74 69 61  erminal uninitia
31dfe 6c 69 7a 65 64 20 69 73 20 75 74 74 65 72 6c 79  lized is utterly
31dff 20 68 61 72 6d 6c 65 73 73 20 61 73 20 6c 6f 6e   harmless as lon
31e00 67 0a 20 20 2a 2a 20 61 73 20 74 68 65 20 76 61  g.  ** as the va
31e01 6c 75 65 20 69 73 20 6e 65 76 65 72 20 75 73 65  lue is never use
31e02 64 2e 20 20 53 6f 20 72 65 61 6c 6c 79 20 74 68  d.  So really th
31e03 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 69  e only thing thi
31e04 73 20 63 6f 64 65 0a 20 20 2a 2a 20 61 63 63 6f  s code.  ** acco
31e05 6d 70 6c 69 73 68 65 73 20 69 73 20 74 6f 20 71  mplishes is to q
31e06 75 69 65 74 65 6e 20 70 75 72 69 66 79 2e 20 20  uieten purify.  
31e07 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 37 2d  .  **.  ** 2007-
31e08 30 31 2d 31 36 3a 20 20 54 68 65 20 77 69 72 65  01-16:  The wire
31e09 73 68 61 72 6b 20 70 72 6f 6a 65 63 74 20 28 77  shark project (w
31e0a 77 77 2e 77 69 72 65 73 68 61 72 6b 2e 6f 72 67  ww.wireshark.org
31e0b 29 20 72 65 70 6f 72 74 73 20 74 68 61 74 0a 20  ) reports that. 
31e0c 20 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73   ** without this
31e0d 20 63 6f 64 65 2c 20 74 68 65 69 72 20 70 61 72   code, their par
31e0e 73 65 72 20 73 65 67 66 61 75 6c 74 73 2e 20 20  ser segfaults.  
31e0f 49 27 6d 20 6e 6f 74 20 73 75 72 65 20 77 68 61  I'm not sure wha
31e10 74 20 74 68 65 72 65 0a 20 20 2a 2a 20 70 61 72  t there.  ** par
31e11 73 65 72 20 69 73 20 64 6f 69 6e 67 20 74 6f 20  ser is doing to 
31e12 6d 61 6b 65 20 74 68 69 73 20 68 61 70 70 65 6e  make this happen
31e13 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 73  .  This is the s
31e14 65 63 6f 6e 64 20 62 75 67 20 72 65 70 6f 72 74  econd bug report
31e15 0a 20 20 2a 2a 20 66 72 6f 6d 20 77 69 72 65 73  .  ** from wires
31e16 68 61 72 6b 20 74 68 69 73 20 77 65 65 6b 2e 20  hark this week. 
31e17 20 43 6c 65 61 72 6c 79 20 74 68 65 79 20 61 72   Clearly they ar
31e18 65 20 73 74 72 65 73 73 69 6e 67 20 4c 65 6d 6f  e stressing Lemo
31e19 6e 20 69 6e 20 77 61 79 73 0a 20 20 2a 2a 20 74  n in ways.  ** t
31e1a 68 61 74 20 69 74 20 68 61 73 20 6e 6f 74 20 62  hat it has not b
31e1b 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 73  een previously s
31e1c 74 72 65 73 73 65 64 2e 2e 2e 20 20 28 53 51 4c  tressed...  (SQL
31e1d 69 74 65 20 74 69 63 6b 65 74 20 23 32 31 37 32  ite ticket #2172
31e1e 29 0a 20 20 2a 2f 0a 20 20 2f 2a 6d 65 6d 73 65  ).  */.  /*memse
31e1f 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2c 20  t(&yygotominor, 
31e20 30 2c 20 73 69 7a 65 6f 66 28 79 79 67 6f 74 6f  0, sizeof(yygoto
31e21 6d 69 6e 6f 72 29 29 3b 2a 2f 0a 20 20 79 79 67  minor));*/.  yyg
31e22 6f 74 6f 6d 69 6e 6f 72 20 3d 20 79 79 7a 65 72  otominor = yyzer
31e23 6f 6d 69 6e 6f 72 3b 0a 0a 0a 20 20 73 77 69 74  ominor;...  swit
31e24 63 68 28 20 79 79 72 75 6c 65 6e 6f 20 29 7b 0a  ch( yyruleno ){.
31e25 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 68    /* Beginning h
31e26 65 72 65 20 61 72 65 20 74 68 65 20 72 65 64 75  ere are the redu
31e27 63 74 69 6f 6e 20 63 61 73 65 73 2e 20 20 41 20  ction cases.  A 
31e28 74 79 70 69 63 61 6c 20 65 78 61 6d 70 6c 65 0a  typical example.
31e29 20 20 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20    ** follows:.  
31e2a 2a 2a 20 20 20 63 61 73 65 20 30 3a 0a 20 20 2a  **   case 0:.  *
31e2b 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f  *  #line <lineno
31e2c 3e 20 3c 67 72 61 6d 6d 61 72 66 69 6c 65 3e 0a  > <grammarfile>.
31e2d 20 20 2a 2a 20 20 20 20 20 7b 20 2e 2e 2e 20 7d    **     { ... }
31e2e 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55 73             // Us
31e2f 65 72 20 73 75 70 70 6c 69 65 64 20 63 6f 64 65  er supplied code
31e30 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69  .  **  #line <li
31e31 6e 65 6e 6f 3e 20 3c 74 68 69 73 66 69 6c 65 3e  neno> <thisfile>
31e32 0a 20 20 2a 2a 20 20 20 20 20 62 72 65 61 6b 3b  .  **     break;
31e33 0a 20 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  .  */.      case
31e34 20 35 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a   5: /* explain :
31e35 3a 3d 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42  := */.{ sqlite3B
31e36 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65  eginParse(pParse
31e37 2c 20 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  , 0); }.        
31e38 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31e39 65 20 36 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20  e 6: /* explain 
31e3a 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 2a 2f 0a 7b  ::= EXPLAIN */.{
31e3b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72   sqlite3BeginPar
31e3c 73 65 28 70 50 61 72 73 65 2c 20 31 29 3b 20 7d  se(pParse, 1); }
31e3d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31e3e 20 20 20 20 20 20 63 61 73 65 20 37 3a 20 2f 2a        case 7: /*
31e3f 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50   explain ::= EXP
31e40 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20  LAIN QUERY PLAN 
31e41 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69  */.{ sqlite3Begi
31e42 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 32  nParse(pParse, 2
31e43 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
31e44 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  ak;.      case 8
31e45 3a 20 2f 2a 20 63 6d 64 78 20 3a 3a 3d 20 63 6d  : /* cmdx ::= cm
31e46 64 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 46 69  d */.{ sqlite3Fi
31e47 6e 69 73 68 43 6f 64 69 6e 67 28 70 50 61 72 73  nishCoding(pPars
31e48 65 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  e); }.        br
31e49 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31e4a 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 42 45  9: /* cmd ::= BE
31e4b 47 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74 72  GIN transtype tr
31e4c 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69  ans_opt */.{sqli
31e4d 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74  te3BeginTransact
31e4e 69 6f 6e 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ion(pParse, yyms
31e4f 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
31e50 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  8);}.        bre
31e51 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
31e52 33 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20  3: /* transtype 
31e53 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
31e54 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f 44  nor.yy328 = TK_D
31e55 45 46 45 52 52 45 44 3b 7d 0a 20 20 20 20 20 20  EFERRED;}.      
31e56 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31e57 61 73 65 20 31 34 3a 20 2f 2a 20 74 72 61 6e 73  ase 14: /* trans
31e58 74 79 70 65 20 3a 3a 3d 20 44 45 46 45 52 52 45  type ::= DEFERRE
31e59 44 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  D */.      case 
31e5a 31 35 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65  15: /* transtype
31e5b 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 20 2a   ::= IMMEDIATE *
31e5c 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31e5d 75 6c 65 6e 6f 3d 3d 31 35 29 3b 0a 20 20 20 20  uleno==15);.    
31e5e 20 20 63 61 73 65 20 31 36 3a 20 2f 2a 20 74 72    case 16: /* tr
31e5f 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43 4c  anstype ::= EXCL
31e60 55 53 49 56 45 20 2a 2f 20 79 79 74 65 73 74 63  USIVE */ yytestc
31e61 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36  ase(yyruleno==16
31e62 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  );.      case 11
31e63 34 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63  4: /* multiselec
31e64 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 2a  t_op ::= UNION *
31e65 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31e66 75 6c 65 6e 6f 3d 3d 31 31 34 29 3b 0a 20 20 20  uleno==114);.   
31e67 20 20 20 63 61 73 65 20 31 31 36 3a 20 2f 2a 20     case 116: /* 
31e68 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
31e69 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53  := EXCEPT|INTERS
31e6a 45 43 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ECT */ yytestcas
31e6b 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 36 29  e(yyruleno==116)
31e6c 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
31e6d 79 33 32 38 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y328 = yymsp[0].
31e6e 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20 20 20 20  major;}.        
31e6f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31e70 65 20 31 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  e 17: /* cmd ::=
31e71 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f 70   COMMIT trans_op
31e72 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  t */.      case 
31e73 31 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 45  18: /* cmd ::= E
31e74 4e 44 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 20  ND trans_opt */ 
31e75 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31e76 65 6e 6f 3d 3d 31 38 29 3b 0a 7b 73 71 6c 69 74  eno==18);.{sqlit
31e77 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74  e3CommitTransact
31e78 69 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20 20  ion(pParse);}.  
31e79 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31e7a 20 20 20 63 61 73 65 20 31 39 3a 20 2f 2a 20 63     case 19: /* c
31e7b 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20  md ::= ROLLBACK 
31e7c 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71  trans_opt */.{sq
31e7d 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61  lite3RollbackTra
31e7e 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65 29  nsaction(pParse)
31e7f 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31e80 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 3a  ;.      case 22:
31e81 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 53 41 56 45   /* cmd ::= SAVE
31e82 50 4f 49 4e 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20  POINT nm */.{.  
31e83 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74  sqlite3Savepoint
31e84 28 70 50 61 72 73 65 2c 20 53 41 56 45 50 4f 49  (pParse, SAVEPOI
31e85 4e 54 5f 42 45 47 49 4e 2c 20 26 79 79 6d 73 70  NT_BEGIN, &yymsp
31e86 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
31e87 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31e88 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 3a 20  .      case 23: 
31e89 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 4c 45 41  /* cmd ::= RELEA
31e8a 53 45 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74  SE savepoint_opt
31e8b 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74   nm */.{.  sqlit
31e8c 65 33 53 61 76 65 70 6f 69 6e 74 28 70 50 61 72  e3Savepoint(pPar
31e8d 73 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45  se, SAVEPOINT_RE
31e8e 4c 45 41 53 45 2c 20 26 79 79 6d 73 70 5b 30 5d  LEASE, &yymsp[0]
31e8f 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
31e90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31e91 20 20 20 20 63 61 73 65 20 32 34 3a 20 2f 2a 20      case 24: /* 
31e92 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b  cmd ::= ROLLBACK
31e93 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f 20 73 61   trans_opt TO sa
31e94 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a  vepoint_opt nm *
31e95 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61 76  /.{.  sqlite3Sav
31e96 65 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53  epoint(pParse, S
31e97 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
31e98 4b 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  K, &yymsp[0].min
31e99 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
31e9a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31e9b 63 61 73 65 20 32 36 3a 20 2f 2a 20 63 72 65 61  case 26: /* crea
31e9c 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 63 72 65  te_table ::= cre
31e9d 61 74 65 6b 77 20 74 65 6d 70 20 54 41 42 4c 45  atekw temp TABLE
31e9e 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
31e9f 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 73 71 6c  dbnm */.{.   sql
31ea0 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70  ite3StartTable(p
31ea1 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d  Parse,&yymsp[-1]
31ea2 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
31ea3 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79  p[0].minor.yy0,y
31ea4 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
31ea5 79 33 32 38 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d  y328,0,0,yymsp[-
31ea6 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  2].minor.yy328);
31ea7 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31ea8 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 3a  ;.      case 27:
31ea9 20 2f 2a 20 63 72 65 61 74 65 6b 77 20 3a 3a 3d   /* createkw ::=
31eaa 20 43 52 45 41 54 45 20 2a 2f 0a 7b 0a 20 20 70   CREATE */.{.  p
31eab 50 61 72 73 65 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61  Parse->db->looka
31eac 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20  side.bEnabled = 
31ead 30 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  0;.  yygotominor
31eae 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  .yy0 = yymsp[0].
31eaf 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20 20 20  minor.yy0;.}.   
31eb0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31eb1 20 20 63 61 73 65 20 32 38 3a 20 2f 2a 20 69 66    case 28: /* if
31eb2 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 2a 2f  notexists ::= */
31eb3 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 3a 20  .      case 31: 
31eb4 2f 2a 20 74 65 6d 70 20 3a 3a 3d 20 2a 2f 20 79  /* temp ::= */ y
31eb5 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31eb6 6e 6f 3d 3d 33 31 29 3b 0a 20 20 20 20 20 20 63  no==31);.      c
31eb7 61 73 65 20 37 30 3a 20 2f 2a 20 61 75 74 6f 69  ase 70: /* autoi
31eb8 6e 63 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  nc ::= */ yytest
31eb9 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 37  case(yyruleno==7
31eba 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  0);.      case 8
31ebb 32 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63  2: /* defer_subc
31ebc 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45  lause ::= NOT DE
31ebd 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65  FERRABLE init_de
31ebe 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20  ferred_pred_opt 
31ebf 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31ec0 72 75 6c 65 6e 6f 3d 3d 38 32 29 3b 0a 20 20 20  ruleno==82);.   
31ec1 20 20 20 63 61 73 65 20 38 34 3a 20 2f 2a 20 69     case 84: /* i
31ec2 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
31ec3 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  d_opt ::= */ yyt
31ec4 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31ec5 3d 3d 38 34 29 3b 0a 20 20 20 20 20 20 63 61 73  ==84);.      cas
31ec6 65 20 38 36 3a 20 2f 2a 20 69 6e 69 74 5f 64 65  e 86: /* init_de
31ec7 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20  ferred_pred_opt 
31ec8 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d  ::= INITIALLY IM
31ec9 4d 45 44 49 41 54 45 20 2a 2f 20 79 79 74 65 73  MEDIATE */ yytes
31eca 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31ecb 38 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  86);.      case 
31ecc 39 37 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62  97: /* defer_sub
31ecd 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 2a  clause_opt ::= *
31ece 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31ecf 75 6c 65 6e 6f 3d 3d 39 37 29 3b 0a 20 20 20 20  uleno==97);.    
31ed0 20 20 63 61 73 65 20 31 30 38 3a 20 2f 2a 20 69    case 108: /* i
31ed1 66 65 78 69 73 74 73 20 3a 3a 3d 20 2a 2f 20 79  fexists ::= */ y
31ed2 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31ed3 6e 6f 3d 3d 31 30 38 29 3b 0a 20 20 20 20 20 20  no==108);.      
31ed4 63 61 73 65 20 31 31 39 3a 20 2f 2a 20 64 69 73  case 119: /* dis
31ed5 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 20 2a 2f  tinct ::= ALL */
31ed6 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31ed7 6c 65 6e 6f 3d 3d 31 31 39 29 3b 0a 20 20 20 20  leno==119);.    
31ed8 20 20 63 61 73 65 20 31 32 30 3a 20 2f 2a 20 64    case 120: /* d
31ed9 69 73 74 69 6e 63 74 20 3a 3a 3d 20 2a 2f 20 79  istinct ::= */ y
31eda 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31edb 6e 6f 3d 3d 31 32 30 29 3b 0a 20 20 20 20 20 20  no==120);.      
31edc 63 61 73 65 20 32 32 32 3a 20 2f 2a 20 62 65 74  case 222: /* bet
31edd 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45 54 57  ween_op ::= BETW
31ede 45 45 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73  EEN */ yytestcas
31edf 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 32 29  e(yyruleno==222)
31ee0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 35  ;.      case 225
31ee1 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 49  : /* in_op ::= I
31ee2 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  N */ yytestcase(
31ee3 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 35 29 3b 0a  yyruleno==225);.
31ee4 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
31ee5 32 38 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  28 = 0;}.       
31ee6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31ee7 73 65 20 32 39 3a 20 2f 2a 20 69 66 6e 6f 74 65  se 29: /* ifnote
31ee8 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54  xists ::= IF NOT
31ee9 20 45 58 49 53 54 53 20 2a 2f 0a 20 20 20 20 20   EXISTS */.     
31eea 20 63 61 73 65 20 33 30 3a 20 2f 2a 20 74 65 6d   case 30: /* tem
31eeb 70 20 3a 3a 3d 20 54 45 4d 50 20 2a 2f 20 79 79  p ::= TEMP */ yy
31eec 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31eed 6f 3d 3d 33 30 29 3b 0a 20 20 20 20 20 20 63 61  o==30);.      ca
31eee 73 65 20 37 31 3a 20 2f 2a 20 61 75 74 6f 69 6e  se 71: /* autoin
31eef 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 20 2a  c ::= AUTOINCR *
31ef0 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31ef1 75 6c 65 6e 6f 3d 3d 37 31 29 3b 0a 20 20 20 20  uleno==71);.    
31ef2 20 20 63 61 73 65 20 38 35 3a 20 2f 2a 20 69 6e    case 85: /* in
31ef3 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
31ef4 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c  _opt ::= INITIAL
31ef5 4c 59 20 44 45 46 45 52 52 45 44 20 2a 2f 20 79  LY DEFERRED */ y
31ef6 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31ef7 6e 6f 3d 3d 38 35 29 3b 0a 20 20 20 20 20 20 63  no==85);.      c
31ef8 61 73 65 20 31 30 37 3a 20 2f 2a 20 69 66 65 78  ase 107: /* ifex
31ef9 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58 49 53  ists ::= IF EXIS
31efa 54 53 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  TS */ yytestcase
31efb 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 37 29 3b  (yyruleno==107);
31efc 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 38 3a  .      case 118:
31efd 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d   /* distinct ::=
31efe 20 44 49 53 54 49 4e 43 54 20 2a 2f 20 79 79 74   DISTINCT */ yyt
31eff 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31f00 3d 3d 31 31 38 29 3b 0a 20 20 20 20 20 20 63 61  ==118);.      ca
31f01 73 65 20 32 32 33 3a 20 2f 2a 20 62 65 74 77 65  se 223: /* betwe
31f02 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45  en_op ::= NOT BE
31f03 54 57 45 45 4e 20 2a 2f 20 79 79 74 65 73 74 63  TWEEN */ yytestc
31f04 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32  ase(yyruleno==22
31f05 33 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  3);.      case 2
31f06 32 36 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d  26: /* in_op ::=
31f07 20 4e 4f 54 20 49 4e 20 2a 2f 20 79 79 74 65 73   NOT IN */ yytes
31f08 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31f09 32 32 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  226);.{yygotomin
31f0a 6f 72 2e 79 79 33 32 38 20 3d 20 31 3b 7d 0a 20  or.yy328 = 1;}. 
31f0b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31f0c 20 20 20 20 63 61 73 65 20 33 32 3a 20 2f 2a 20      case 32: /* 
31f0d 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
31f0e 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d 6e 6c  s ::= LP columnl
31f0f 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74  ist conslist_opt
31f10 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74   RP */.{.  sqlit
31f11 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73  e3EndTable(pPars
31f12 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e,&yymsp[-1].min
31f13 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
31f14 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 0a 7d  .minor.yy0,0);.}
31f15 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31f16 20 20 20 20 20 20 63 61 73 65 20 33 33 3a 20 2f        case 33: /
31f17 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61  * create_table_a
31f18 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c 65 63  rgs ::= AS selec
31f19 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  t */.{.  sqlite3
31f1a 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c  EndTable(pParse,
31f1b 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0,0,yymsp[0].min
31f1c 6f 72 2e 79 79 33 29 3b 0a 20 20 73 71 6c 69 74  or.yy3);.  sqlit
31f1d 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
31f1e 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70  Parse->db, yymsp
31f1f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a  [0].minor.yy3);.
31f20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31f21 0a 20 20 20 20 20 20 63 61 73 65 20 33 36 3a 20  .      case 36: 
31f22 2f 2a 20 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f  /* column ::= co
31f23 6c 75 6d 6e 69 64 20 74 79 70 65 20 63 61 72 67  lumnid type carg
31f24 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  list */.{.  yygo
31f25 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20  tominor.yy0.z = 
31f26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
31f27 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d  yy0.z;.  yygotom
31f28 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e  inor.yy0.n = (in
31f29 74 29 28 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  t)(pParse->sLast
31f2a 54 6f 6b 65 6e 2e 7a 2d 79 79 6d 73 70 5b 2d 32  Token.z-yymsp[-2
31f2b 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 20 2b  ].minor.yy0.z) +
31f2c 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
31f2d 6b 65 6e 2e 6e 3b 0a 7d 0a 20 20 20 20 20 20 20  ken.n;.}.       
31f2e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31f2f 73 65 20 33 37 3a 20 2f 2a 20 63 6f 6c 75 6d 6e  se 37: /* column
31f30 69 64 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 0a 20  id ::= nm */.{. 
31f31 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d   sqlite3AddColum
31f32 6e 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  n(pParse,&yymsp[
31f33 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
31f34 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
31f35 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
31f36 72 2e 79 79 30 3b 0a 7d 0a 20 20 20 20 20 20 20  r.yy0;.}.       
31f37 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31f38 73 65 20 33 38 3a 20 2f 2a 20 69 64 20 3a 3a 3d  se 38: /* id ::=
31f39 20 49 44 20 2a 2f 0a 20 20 20 20 20 20 63 61 73   ID */.      cas
31f3a 65 20 33 39 3a 20 2f 2a 20 69 64 20 3a 3a 3d 20  e 39: /* id ::= 
31f3b 49 4e 44 45 58 45 44 20 2a 2f 20 79 79 74 65 73  INDEXED */ yytes
31f3c 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31f3d 33 39 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  39);.      case 
31f3e 34 30 3a 20 2f 2a 20 69 64 73 20 3a 3a 3d 20 49  40: /* ids ::= I
31f3f 44 7c 53 54 52 49 4e 47 20 2a 2f 20 79 79 74 65  D|STRING */ yyte
31f40 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31f41 3d 34 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =40);.      case
31f42 20 34 31 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 69   41: /* nm ::= i
31f43 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  d */ yytestcase(
31f44 79 79 72 75 6c 65 6e 6f 3d 3d 34 31 29 3b 0a 20  yyruleno==41);. 
31f45 20 20 20 20 20 63 61 73 65 20 34 32 3a 20 2f 2a       case 42: /*
31f46 20 6e 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 20 2a   nm ::= STRING *
31f47 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31f48 75 6c 65 6e 6f 3d 3d 34 32 29 3b 0a 20 20 20 20  uleno==42);.    
31f49 20 20 63 61 73 65 20 34 33 3a 20 2f 2a 20 6e 6d    case 43: /* nm
31f4a 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 20   ::= JOIN_KW */ 
31f4b 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31f4c 65 6e 6f 3d 3d 34 33 29 3b 0a 20 20 20 20 20 20  eno==43);.      
31f4d 63 61 73 65 20 34 36 3a 20 2f 2a 20 74 79 70 65  case 46: /* type
31f4e 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61  token ::= typena
31f4f 6d 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  me */ yytestcase
31f50 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 36 29 3b 0a  (yyruleno==46);.
31f51 20 20 20 20 20 20 63 61 73 65 20 34 39 3a 20 2f        case 49: /
31f52 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 69  * typename ::= i
31f53 64 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  ds */ yytestcase
31f54 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 39 29 3b 0a  (yyruleno==49);.
31f55 20 20 20 20 20 20 63 61 73 65 20 31 32 36 3a 20        case 126: 
31f56 2f 2a 20 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 20  /* as ::= AS nm 
31f57 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31f58 72 75 6c 65 6e 6f 3d 3d 31 32 36 29 3b 0a 20 20  ruleno==126);.  
31f59 20 20 20 20 63 61 73 65 20 31 32 37 3a 20 2f 2a      case 127: /*
31f5a 20 61 73 20 3a 3a 3d 20 69 64 73 20 2a 2f 20 79   as ::= ids */ y
31f5b 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31f5c 6e 6f 3d 3d 31 32 37 29 3b 0a 20 20 20 20 20 20  no==127);.      
31f5d 63 61 73 65 20 31 33 37 3a 20 2f 2a 20 64 62 6e  case 137: /* dbn
31f5e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 20 2a 2f 20  m ::= DOT nm */ 
31f5f 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31f60 65 6e 6f 3d 3d 31 33 37 29 3b 0a 20 20 20 20 20  eno==137);.     
31f61 20 63 61 73 65 20 31 34 36 3a 20 2f 2a 20 69 6e   case 146: /* in
31f62 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e  dexed_opt ::= IN
31f63 44 45 58 45 44 20 42 59 20 6e 6d 20 2a 2f 20 79  DEXED BY nm */ y
31f64 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31f65 6e 6f 3d 3d 31 34 36 29 3b 0a 20 20 20 20 20 20  no==146);.      
31f66 63 61 73 65 20 32 35 31 3a 20 2f 2a 20 63 6f 6c  case 251: /* col
31f67 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45  late ::= COLLATE
31f68 20 69 64 73 20 2a 2f 20 79 79 74 65 73 74 63 61   ids */ yytestca
31f69 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35 31  se(yyruleno==251
31f6a 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  );.      case 26
31f6b 30 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20  0: /* nmnum ::= 
31f6c 70 6c 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65  plus_num */ yyte
31f6d 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31f6e 3d 32 36 30 29 3b 0a 20 20 20 20 20 20 63 61 73  =260);.      cas
31f6f 65 20 32 36 31 3a 20 2f 2a 20 6e 6d 6e 75 6d 20  e 261: /* nmnum 
31f70 3a 3a 3d 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74  ::= nm */ yytest
31f71 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
31f72 36 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  61);.      case 
31f73 32 36 32 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a  262: /* nmnum ::
31f74 3d 20 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63 61  = ON */ yytestca
31f75 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 32  se(yyruleno==262
31f76 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  );.      case 26
31f77 33 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20  3: /* nmnum ::= 
31f78 44 45 4c 45 54 45 20 2a 2f 20 79 79 74 65 73 74  DELETE */ yytest
31f79 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
31f7a 36 33 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  63);.      case 
31f7b 32 36 34 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a  264: /* nmnum ::
31f7c 3d 20 44 45 46 41 55 4c 54 20 2a 2f 20 79 79 74  = DEFAULT */ yyt
31f7d 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31f7e 3d 3d 32 36 34 29 3b 0a 20 20 20 20 20 20 63 61  ==264);.      ca
31f7f 73 65 20 32 36 35 3a 20 2f 2a 20 70 6c 75 73 5f  se 265: /* plus_
31f80 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74  num ::= plus_opt
31f81 20 6e 75 6d 62 65 72 20 2a 2f 20 79 79 74 65 73   number */ yytes
31f82 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31f83 32 36 35 29 3b 0a 20 20 20 20 20 20 63 61 73 65  265);.      case
31f84 20 32 36 36 3a 20 2f 2a 20 6d 69 6e 75 73 5f 6e   266: /* minus_n
31f85 75 6d 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d  um ::= MINUS num
31f86 62 65 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ber */ yytestcas
31f87 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 36 29  e(yyruleno==266)
31f88 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 37  ;.      case 267
31f89 3a 20 2f 2a 20 6e 75 6d 62 65 72 20 3a 3a 3d 20  : /* number ::= 
31f8a 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 20 2a 2f  INTEGER|FLOAT */
31f8b 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31f8c 6c 65 6e 6f 3d 3d 32 36 37 29 3b 0a 20 20 20 20  leno==267);.    
31f8d 20 20 63 61 73 65 20 32 38 35 3a 20 2f 2a 20 74    case 285: /* t
31f8e 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20 79 79  rnm ::= nm */ yy
31f8f 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31f90 6f 3d 3d 32 38 35 29 3b 0a 7b 79 79 67 6f 74 6f  o==285);.{yygoto
31f91 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73  minor.yy0 = yyms
31f92 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d  p[0].minor.yy0;}
31f93 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31f94 20 20 20 20 20 20 63 61 73 65 20 34 35 3a 20 2f        case 45: /
31f95 2a 20 74 79 70 65 20 3a 3a 3d 20 74 79 70 65 74  * type ::= typet
31f96 6f 6b 65 6e 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  oken */.{sqlite3
31f97 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 70 50  AddColumnType(pP
31f98 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  arse,&yymsp[0].m
31f99 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
31f9a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31f9b 20 63 61 73 65 20 34 37 3a 20 2f 2a 20 74 79 70   case 47: /* typ
31f9c 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
31f9d 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 52 50  ame LP signed RP
31f9e 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
31f9f 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73  nor.yy0.z = yyms
31fa0 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-3].minor.yy0.
31fa1 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  z;.  yygotominor
31fa2 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74 29 28 26  .yy0.n = (int)(&
31fa3 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31fa4 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0.z[yymsp[0].mi
31fa5 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d  nor.yy0.n] - yym
31fa6 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
31fa7 2e 7a 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  .z);.}.        b
31fa8 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31fa9 20 34 38 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65   48: /* typetoke
31faa 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c  n ::= typename L
31fab 50 20 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73  P signed COMMA s
31fac 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20  igned RP */.{.  
31fad 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
31fae 7a 20 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  z = yymsp[-5].mi
31faf 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67  nor.yy0.z;.  yyg
31fb0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
31fb1 20 28 69 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d   (int)(&yymsp[0]
31fb2 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
31fb3 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
31fb4 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  n] - yymsp[-5].m
31fb5 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a 20  inor.yy0.z);.}. 
31fb6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31fb7 20 20 20 20 63 61 73 65 20 35 30 3a 20 2f 2a 20      case 50: /* 
31fb8 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70  typename ::= typ
31fb9 65 6e 61 6d 65 20 69 64 73 20 2a 2f 0a 7b 79 79  ename ids */.{yy
31fba 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d  gotominor.yy0.z=
31fbb 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
31fbc 79 79 30 2e 7a 3b 20 79 79 67 6f 74 6f 6d 69 6e  yy0.z; yygotomin
31fbd 6f 72 2e 79 79 30 2e 6e 3d 79 79 6d 73 70 5b 30  or.yy0.n=yymsp[0
31fbe 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 2b 28 69  ].minor.yy0.n+(i
31fbf 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  nt)(yymsp[0].min
31fc0 6f 72 2e 79 79 30 2e 7a 2d 79 79 6d 73 70 5b 2d  or.yy0.z-yymsp[-
31fc1 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b  1].minor.yy0.z);
31fc2 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31fc3 0a 20 20 20 20 20 20 63 61 73 65 20 35 37 3a 20  .      case 57: 
31fc4 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  /* ccons ::= DEF
31fc5 41 55 4c 54 20 74 65 72 6d 20 2a 2f 0a 20 20 20  AULT term */.   
31fc6 20 20 20 63 61 73 65 20 35 39 3a 20 2f 2a 20 63     case 59: /* c
31fc7 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54  cons ::= DEFAULT
31fc8 20 50 4c 55 53 20 74 65 72 6d 20 2a 2f 20 79 79   PLUS term */ yy
31fc9 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31fca 6f 3d 3d 35 39 29 3b 0a 7b 73 71 6c 69 74 65 33  o==59);.{sqlite3
31fcb 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28  AddDefaultValue(
31fcc 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d  pParse,&yymsp[0]
31fcd 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a  .minor.yy346);}.
31fce 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31fcf 20 20 20 20 20 63 61 73 65 20 35 38 3a 20 2f 2a       case 58: /*
31fd0 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
31fd1 4c 54 20 4c 50 20 65 78 70 72 20 52 50 20 2a 2f  LT LP expr RP */
31fd2 0a 7b 73 71 6c 69 74 65 33 41 64 64 44 65 66 61  .{sqlite3AddDefa
31fd3 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c  ultValue(pParse,
31fd4 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
31fd5 2e 79 79 33 34 36 29 3b 7d 0a 20 20 20 20 20 20  .yy346);}.      
31fd6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31fd7 61 73 65 20 36 30 3a 20 2f 2a 20 63 63 6f 6e 73  ase 60: /* ccons
31fd8 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4d 49 4e   ::= DEFAULT MIN
31fd9 55 53 20 74 65 72 6d 20 2a 2f 0a 7b 0a 20 20 45  US term */.{.  E
31fda 78 70 72 53 70 61 6e 20 76 3b 0a 20 20 76 2e 70  xprSpan v;.  v.p
31fdb 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
31fdc 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 55  xpr(pParse, TK_U
31fdd 4d 49 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d 2e  MINUS, yymsp[0].
31fde 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
31fdf 72 2c 20 30 2c 20 30 29 3b 0a 20 20 76 2e 7a 53  r, 0, 0);.  v.zS
31fe0 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  tart = yymsp[-1]
31fe1 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
31fe2 76 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30  v.zEnd = yymsp[0
31fe3 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45  ].minor.yy346.zE
31fe4 6e 64 3b 0a 20 20 73 71 6c 69 74 65 33 41 64 64  nd;.  sqlite3Add
31fe5 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61  DefaultValue(pPa
31fe6 72 73 65 2c 26 76 29 3b 0a 7d 0a 20 20 20 20 20  rse,&v);.}.     
31fe7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31fe8 63 61 73 65 20 36 31 3a 20 2f 2a 20 63 63 6f 6e  case 61: /* ccon
31fe9 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69 64  s ::= DEFAULT id
31fea 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 53 70 61 6e   */.{.  ExprSpan
31feb 20 76 3b 0a 20 20 73 70 61 6e 45 78 70 72 28 26   v;.  spanExpr(&
31fec 76 2c 20 70 50 61 72 73 65 2c 20 54 4b 5f 53 54  v, pParse, TK_ST
31fed 52 49 4e 47 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  RING, &yymsp[0].
31fee 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71  minor.yy0);.  sq
31fef 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56  lite3AddDefaultV
31ff0 61 6c 75 65 28 70 50 61 72 73 65 2c 26 76 29 3b  alue(pParse,&v);
31ff1 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31ff2 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 33 3a  ;.      case 63:
31ff3 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f   /* ccons ::= NO
31ff4 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f  T NULL onconf */
31ff5 0a 7b 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e  .{sqlite3AddNotN
31ff6 75 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ull(pParse, yyms
31ff7 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
31ff8 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31ff9 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 34  k;.      case 64
31ffa 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 50  : /* ccons ::= P
31ffb 52 49 4d 41 52 59 20 4b 45 59 20 73 6f 72 74 6f  RIMARY KEY sorto
31ffc 72 64 65 72 20 6f 6e 63 6f 6e 66 20 61 75 74 6f  rder onconf auto
31ffd 69 6e 63 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  inc */.{sqlite3A
31ffe 64 64 50 72 69 6d 61 72 79 4b 65 79 28 70 50 61  ddPrimaryKey(pPa
31fff 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e  rse,0,yymsp[-1].
32000 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 79 79 6d 73  minor.yy328,yyms
32001 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
32002 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
32003 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20  .yy328);}.      
32004 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32005 61 73 65 20 36 35 3a 20 2f 2a 20 63 63 6f 6e 73  ase 65: /* ccons
32006 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63 6f   ::= UNIQUE onco
32007 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72  nf */.{sqlite3Cr
32008 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65  eateIndex(pParse
32009 2c 30 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b 30  ,0,0,0,0,yymsp[0
3200a 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30 2c  ].minor.yy328,0,
3200b 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  0,0,0);}.       
3200c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3200d 73 65 20 36 36 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 66: /* ccons 
3200e 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70  ::= CHECK LP exp
3200f 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  r RP */.{sqlite3
32010 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69  AddCheckConstrai
32011 6e 74 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nt(pParse,yymsp[
32012 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -1].minor.yy346.
32013 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20  pExpr);}.       
32014 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32015 73 65 20 36 37 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 67: /* ccons 
32016 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20 6e  ::= REFERENCES n
32017 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65  m idxlist_opt re
32018 66 61 72 67 73 20 2a 2f 0a 7b 73 71 6c 69 74 65  fargs */.{sqlite
32019 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65  3CreateForeignKe
3201a 79 28 70 50 61 72 73 65 2c 30 2c 26 79 79 6d 73  y(pParse,0,&yyms
3201b 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
3201c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3201d 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy14,yymsp[0].mi
3201e 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20  nor.yy328);}.   
3201f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32020 20 20 63 61 73 65 20 36 38 3a 20 2f 2a 20 63 63    case 68: /* cc
32021 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75  ons ::= defer_su
32022 62 63 6c 61 75 73 65 20 2a 2f 0a 7b 73 71 6c 69  bclause */.{sqli
32023 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b  te3DeferForeignK
32024 65 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  ey(pParse,yymsp[
32025 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  0].minor.yy328);
32026 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32027 0a 20 20 20 20 20 20 63 61 73 65 20 36 39 3a 20  .      case 69: 
32028 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c  /* ccons ::= COL
32029 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b 73 71 6c  LATE ids */.{sql
3202a 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79  ite3AddCollateTy
3202b 70 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  pe(pParse, &yyms
3202c 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
3202d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3202e 0a 20 20 20 20 20 20 63 61 73 65 20 37 32 3a 20  .      case 72: 
3202f 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 2a  /* refargs ::= *
32030 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
32031 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65 2a  yy328 = OE_None*
32032 30 78 30 31 30 31 3b 20 2f 2a 20 45 56 3a 20 52  0x0101; /* EV: R
32033 2d 31 39 38 30 33 2d 34 35 38 38 34 20 2a 2f 7d  -19803-45884 */}
32034 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32035 20 20 20 20 20 20 63 61 73 65 20 37 33 3a 20 2f        case 73: /
32036 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 72 65  * refargs ::= re
32037 66 61 72 67 73 20 72 65 66 61 72 67 20 2a 2f 0a  fargs refarg */.
32038 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
32039 33 32 38 20 3d 20 28 79 79 6d 73 70 5b 2d 31 5d  328 = (yymsp[-1]
3203a 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 26 20 7e  .minor.yy328 & ~
3203b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3203c 79 34 32 39 2e 6d 61 73 6b 29 20 7c 20 79 79 6d  y429.mask) | yym
3203d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 32  sp[0].minor.yy42
3203e 39 2e 76 61 6c 75 65 3b 20 7d 0a 20 20 20 20 20  9.value; }.     
3203f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32040 63 61 73 65 20 37 34 3a 20 2f 2a 20 72 65 66 61  case 74: /* refa
32041 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d 20  rg ::= MATCH nm 
32042 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
32043 2e 79 79 34 32 39 2e 76 61 6c 75 65 20 3d 20 30  .yy429.value = 0
32044 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ;     yygotomino
32045 72 2e 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20 30  r.yy429.mask = 0
32046 78 30 30 30 30 30 30 3b 20 7d 0a 20 20 20 20 20  x000000; }.     
32047 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32048 63 61 73 65 20 37 35 3a 20 2f 2a 20 72 65 66 61  case 75: /* refa
32049 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45 54 45  rg ::= ON DELETE
3204a 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67   refact */.{ yyg
3204b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76  otominor.yy429.v
3204c 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  alue = yymsp[0].
3204d 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 20 20 20 20  minor.yy328;    
3204e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
3204f 32 39 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30 30  29.mask = 0x0000
32050 66 66 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ff; }.        br
32051 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32052 37 36 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a  76: /* refarg ::
32053 3d 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66 61  = ON UPDATE refa
32054 63 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  ct */.{ yygotomi
32055 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c 75 65 20  nor.yy429.value 
32056 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
32057 2e 79 79 33 32 38 3c 3c 38 3b 20 20 79 79 67 6f  .yy328<<8;  yygo
32058 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61  tominor.yy429.ma
32059 73 6b 20 3d 20 30 78 30 30 66 66 30 30 3b 20 7d  sk = 0x00ff00; }
3205a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3205b 20 20 20 20 20 20 63 61 73 65 20 37 37 3a 20 2f        case 77: /
3205c 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54  * refact ::= SET
3205d 20 4e 55 4c 4c 20 2a 2f 0a 7b 20 79 79 67 6f 74   NULL */.{ yygot
3205e 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f  ominor.yy328 = O
3205f 45 5f 53 65 74 4e 75 6c 6c 3b 20 20 2f 2a 20 45  E_SetNull;  /* E
32060 56 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35 32  V: R-33326-45252
32061 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72 65   */}.        bre
32062 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37  ak;.      case 7
32063 38 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d  8: /* refact ::=
32064 20 53 45 54 20 44 45 46 41 55 4c 54 20 2a 2f 0a   SET DEFAULT */.
32065 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
32066 33 32 38 20 3d 20 4f 45 5f 53 65 74 44 66 6c 74  328 = OE_SetDflt
32067 3b 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32  ;  /* EV: R-3332
32068 36 2d 34 35 32 35 32 20 2a 2f 7d 0a 20 20 20 20  6-45252 */}.    
32069 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3206a 20 63 61 73 65 20 37 39 3a 20 2f 2a 20 72 65 66   case 79: /* ref
3206b 61 63 74 20 3a 3a 3d 20 43 41 53 43 41 44 45 20  act ::= CASCADE 
3206c 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
3206d 2e 79 79 33 32 38 20 3d 20 4f 45 5f 43 61 73 63  .yy328 = OE_Casc
3206e 61 64 65 3b 20 20 2f 2a 20 45 56 3a 20 52 2d 33  ade;  /* EV: R-3
3206f 33 33 32 36 2d 34 35 32 35 32 20 2a 2f 7d 0a 20  3326-45252 */}. 
32070 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32071 20 20 20 20 63 61 73 65 20 38 30 3a 20 2f 2a 20      case 80: /* 
32072 72 65 66 61 63 74 20 3a 3a 3d 20 52 45 53 54 52  refact ::= RESTR
32073 49 43 54 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  ICT */.{ yygotom
32074 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f  inor.yy328 = OE_
32075 52 65 73 74 72 69 63 74 3b 20 2f 2a 20 45 56 3a  Restrict; /* EV:
32076 20 52 2d 33 33 33 32 36 2d 34 35 32 35 32 20 2a   R-33326-45252 *
32077 2f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  /}.        break
32078 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 31 3a  ;.      case 81:
32079 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 4e   /* refact ::= N
3207a 4f 20 41 43 54 49 4f 4e 20 2a 2f 0a 7b 20 79 79  O ACTION */.{ yy
3207b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
3207c 3d 20 4f 45 5f 4e 6f 6e 65 3b 20 20 20 20 20 2f  = OE_None;     /
3207d 2a 20 45 56 3a 20 52 2d 33 33 33 32 36 2d 34 35  * EV: R-33326-45
3207e 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20  252 */}.        
3207f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32080 65 20 38 33 3a 20 2f 2a 20 64 65 66 65 72 5f 73  e 83: /* defer_s
32081 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 44 45 46  ubclause ::= DEF
32082 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66  ERRABLE init_def
32083 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 2a  erred_pred_opt *
32084 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39 38 3a  /.      case 98:
32085 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61   /* defer_subcla
32086 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65  use_opt ::= defe
32087 72 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f 20 79  r_subclause */ y
32088 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32089 6e 6f 3d 3d 39 38 29 3b 0a 20 20 20 20 20 20 63  no==98);.      c
3208a 61 73 65 20 31 30 30 3a 20 2f 2a 20 6f 6e 63 6f  ase 100: /* onco
3208b 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49  nf ::= ON CONFLI
3208c 43 54 20 72 65 73 6f 6c 76 65 74 79 70 65 20 2a  CT resolvetype *
3208d 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3208e 75 6c 65 6e 6f 3d 3d 31 30 30 29 3b 0a 20 20 20  uleno==100);.   
3208f 20 20 20 63 61 73 65 20 31 30 33 3a 20 2f 2a 20     case 103: /* 
32090 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20  resolvetype ::= 
32091 72 61 69 73 65 74 79 70 65 20 2a 2f 20 79 79 74  raisetype */ yyt
32092 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32093 3d 3d 31 30 33 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==103);.{yygotom
32094 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 79 79 6d  inor.yy328 = yym
32095 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
32096 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  8;}.        brea
32097 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 37  k;.      case 87
32098 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f 70  : /* conslist_op
32099 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  t ::= */.{yygoto
3209a 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b  minor.yy0.n = 0;
3209b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
3209c 2e 7a 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  .z = 0;}.       
3209d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3209e 73 65 20 38 38 3a 20 2f 2a 20 63 6f 6e 73 6c 69  se 88: /* consli
3209f 73 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41  st_opt ::= COMMA
320a0 20 63 6f 6e 73 6c 69 73 74 20 2a 2f 0a 7b 79 79   conslist */.{yy
320a1 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20  gotominor.yy0 = 
320a2 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
320a3 79 79 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  yy0;}.        br
320a4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
320a5 39 33 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d  93: /* tcons ::=
320a6 20 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20   PRIMARY KEY LP 
320a7 69 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20  idxlist autoinc 
320a8 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71  RP onconf */.{sq
320a9 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b  lite3AddPrimaryK
320aa 65 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  ey(pParse,yymsp[
320ab 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79  -3].minor.yy14,y
320ac 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
320ad 33 32 38 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  328,yymsp[-2].mi
320ae 6e 6f 72 2e 79 79 33 32 38 2c 30 29 3b 7d 0a 20  nor.yy328,0);}. 
320af 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
320b0 20 20 20 20 63 61 73 65 20 39 34 3a 20 2f 2a 20      case 94: /* 
320b1 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45  tcons ::= UNIQUE
320b2 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f   LP idxlist RP o
320b3 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65  nconf */.{sqlite
320b4 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61  3CreateIndex(pPa
320b5 72 73 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b  rse,0,0,0,yymsp[
320b6 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79  -2].minor.yy14,y
320b7 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
320b8 33 32 38 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20  328,0,0,0,0);}. 
320b9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
320ba 20 20 20 20 63 61 73 65 20 39 35 3a 20 2f 2a 20      case 95: /* 
320bb 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20  tcons ::= CHECK 
320bc 4c 50 20 65 78 70 72 20 52 50 20 6f 6e 63 6f 6e  LP expr RP oncon
320bd 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64  f */.{sqlite3Add
320be 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28  CheckConstraint(
320bf 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d  pParse,yymsp[-2]
320c0 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
320c1 70 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  pr);}.        br
320c2 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
320c3 39 36 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d  96: /* tcons ::=
320c4 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50 20   FOREIGN KEY LP 
320c5 69 64 78 6c 69 73 74 20 52 50 20 52 45 46 45 52  idxlist RP REFER
320c6 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74  ENCES nm idxlist
320c7 5f 6f 70 74 20 72 65 66 61 72 67 73 20 64 65 66  _opt refargs def
320c8 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
320c9 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65   */.{.    sqlite
320ca 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65  3CreateForeignKe
320cb 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  y(pParse, yymsp[
320cc 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  -6].minor.yy14, 
320cd 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
320ce 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  .yy0, yymsp[-2].
320cf 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73  minor.yy14, yyms
320d0 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
320d1 38 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  8);.    sqlite3D
320d2 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 70  eferForeignKey(p
320d3 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
320d4 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a  minor.yy328);.}.
320d5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
320d6 20 20 20 20 20 63 61 73 65 20 39 39 3a 20 2f 2a       case 99: /*
320d7 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a 7b   onconf ::= */.{
320d8 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
320d9 38 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 7d  8 = OE_Default;}
320da 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
320db 20 20 20 20 20 20 63 61 73 65 20 31 30 31 3a 20        case 101: 
320dc 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20 2a 2f  /* orconf ::= */
320dd 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
320de 31 38 36 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74  186 = OE_Default
320df 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
320e0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 32  ;.      case 102
320e1 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20  : /* orconf ::= 
320e2 4f 52 20 72 65 73 6f 6c 76 65 74 79 70 65 20 2a  OR resolvetype *
320e3 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
320e4 79 31 38 36 20 3d 20 28 75 38 29 79 79 6d 73 70  y186 = (u8)yymsp
320e5 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b  [0].minor.yy328;
320e6 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
320e7 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 34 3a  .      case 104:
320e8 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20   /* resolvetype 
320e9 3a 3a 3d 20 49 47 4e 4f 52 45 20 2a 2f 0a 7b 79  ::= IGNORE */.{y
320ea 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
320eb 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 7d 0a 20   = OE_Ignore;}. 
320ec 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
320ed 20 20 20 20 63 61 73 65 20 31 30 35 3a 20 2f 2a      case 105: /*
320ee 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d   resolvetype ::=
320ef 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79 67   REPLACE */.{yyg
320f0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d  otominor.yy328 =
320f1 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20   OE_Replace;}.  
320f2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
320f3 20 20 20 63 61 73 65 20 31 30 36 3a 20 2f 2a 20     case 106: /* 
320f4 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42  cmd ::= DROP TAB
320f5 4c 45 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c  LE ifexists full
320f6 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  name */.{.  sqli
320f7 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50 61  te3DropTable(pPa
320f8 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
320f9 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20 79 79 6d  nor.yy65, 0, yym
320fa 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
320fb 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  28);.}.        b
320fc 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
320fd 20 31 30 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   109: /* cmd ::=
320fe 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 56   createkw temp V
320ff 49 45 57 20 69 66 6e 6f 74 65 78 69 73 74 73 20  IEW ifnotexists 
32100 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c 65 63  nm dbnm AS selec
32101 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  t */.{.  sqlite3
32102 43 72 65 61 74 65 56 69 65 77 28 70 50 61 72 73  CreateView(pPars
32103 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69  e, &yymsp[-7].mi
32104 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
32105 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  -3].minor.yy0, &
32106 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
32107 79 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0, yymsp[0].mi
32108 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73 70 5b 2d  nor.yy3, yymsp[-
32109 36 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 20  6].minor.yy328, 
3210a 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3210b 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20  yy328);.}.      
3210c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3210d 61 73 65 20 31 31 30 3a 20 2f 2a 20 63 6d 64 20  ase 110: /* cmd 
3210e 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57 20 69 66  ::= DROP VIEW if
3210f 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20  exists fullname 
32110 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72  */.{.  sqlite3Dr
32111 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  opTable(pParse, 
32112 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32113 79 36 35 2c 20 31 2c 20 79 79 6d 73 70 5b 2d 31  y65, 1, yymsp[-1
32114 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a  ].minor.yy328);.
32115 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32116 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 31 3a  .      case 111:
32117 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73 65 6c 65   /* cmd ::= sele
32118 63 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c 65 63 74  ct */.{.  Select
32119 44 65 73 74 20 64 65 73 74 20 3d 20 7b 53 52 54  Dest dest = {SRT
3211a 5f 4f 75 74 70 75 74 2c 20 30 2c 20 30 2c 20 30  _Output, 0, 0, 0
3211b 2c 20 30 7d 3b 0a 20 20 73 71 6c 69 74 65 33 53  , 0};.  sqlite3S
3211c 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 79 79  elect(pParse, yy
3211d 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
3211e 2c 20 26 64 65 73 74 29 3b 0a 20 20 73 71 6c 69  , &dest);.  sqli
3211f 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
32120 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
32121 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b  p[0].minor.yy3);
32122 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32123 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 32  ;.      case 112
32124 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a 3d 20  : /* select ::= 
32125 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79  oneselect */.{yy
32126 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d 20  gotominor.yy3 = 
32127 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32128 79 33 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  y3;}.        bre
32129 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3212a 31 33 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a  13: /* select ::
3212b 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73 65  = select multise
3212c 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63  lect_op oneselec
3212d 74 20 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d  t */.{.  if( yym
3212e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 20  sp[0].minor.yy3 
3212f 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e  ){.    yymsp[0].
32130 6d 69 6e 6f 72 2e 79 79 33 2d 3e 6f 70 20 3d 20  minor.yy3->op = 
32131 28 75 38 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  (u8)yymsp[-1].mi
32132 6e 6f 72 2e 79 79 33 32 38 3b 0a 20 20 20 20 79  nor.yy328;.    y
32133 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32134 33 2d 3e 70 50 72 69 6f 72 20 3d 20 79 79 6d 73  3->pPrior = yyms
32135 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b  p[-2].minor.yy3;
32136 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
32137 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
32138 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79  e(pParse->db, yy
32139 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3213a 33 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  3);.  }.  yygoto
3213b 6d 69 6e 6f 72 2e 79 79 33 20 3d 20 79 79 6d 73  minor.yy3 = yyms
3213c 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a  p[0].minor.yy3;.
3213d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3213e 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 35 3a  .      case 115:
3213f 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f   /* multiselect_
32140 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c  op ::= UNION ALL
32141 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
32142 2e 79 79 33 32 38 20 3d 20 54 4b 5f 41 4c 4c 3b  .yy328 = TK_ALL;
32143 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32144 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 37 3a  .      case 117:
32145 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 3a 3a   /* oneselect ::
32146 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e 63  = SELECT distinc
32147 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f  t selcollist fro
32148 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f 75  m where_opt grou
32149 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f  pby_opt having_o
3214a 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c  pt orderby_opt l
3214b 69 6d 69 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20  imit_opt */.{.  
3214c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20  yygotominor.yy3 
3214d 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e  = sqlite3SelectN
3214e 65 77 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  ew(pParse,yymsp[
3214f 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79  -6].minor.yy14,y
32150 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
32151 79 36 35 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  y65,yymsp[-4].mi
32152 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b  nor.yy132,yymsp[
32153 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79  -3].minor.yy14,y
32154 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32155 79 31 33 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y132,yymsp[-1].m
32156 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b  inor.yy14,yymsp[
32157 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c  -7].minor.yy328,
32158 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32159 79 34 37 36 2e 70 4c 69 6d 69 74 2c 79 79 6d 73  y476.pLimit,yyms
3215a 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 36  p[0].minor.yy476
3215b 2e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a 20 20 20  .pOffset);.}.   
3215c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3215d 20 20 63 61 73 65 20 31 32 31 3a 20 2f 2a 20 73    case 121: /* s
3215e 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69  clp ::= selcolli
3215f 73 74 20 43 4f 4d 4d 41 20 2a 2f 0a 20 20 20 20  st COMMA */.    
32160 20 20 63 61 73 65 20 32 34 37 3a 20 2f 2a 20 69    case 247: /* i
32161 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c  dxlist_opt ::= L
32162 50 20 69 64 78 6c 69 73 74 20 52 50 20 2a 2f 20  P idxlist RP */ 
32163 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32164 65 6e 6f 3d 3d 32 34 37 29 3b 0a 7b 79 79 67 6f  eno==247);.{yygo
32165 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 79  tominor.yy14 = y
32166 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32167 79 31 34 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y14;}.        br
32168 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32169 31 32 32 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d  122: /* sclp ::=
3216a 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
3216b 35 30 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f  50: /* orderby_o
3216c 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
3216d 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
3216e 35 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  50);.      case 
3216f 31 35 38 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f  158: /* groupby_
32170 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
32171 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32172 31 35 38 29 3b 0a 20 20 20 20 20 20 63 61 73 65  158);.      case
32173 20 32 34 30 3a 20 2f 2a 20 65 78 70 72 6c 69 73   240: /* exprlis
32174 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
32175 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 34  ase(yyruleno==24
32176 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  0);.      case 2
32177 34 36 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f  46: /* idxlist_o
32178 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
32179 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3217a 34 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  46);.{yygotomino
3217b 72 2e 79 79 31 34 20 3d 20 30 3b 7d 0a 20 20 20  r.yy14 = 0;}.   
3217c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3217d 20 20 63 61 73 65 20 31 32 33 3a 20 2f 2a 20 73    case 123: /* s
3217e 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63  elcollist ::= sc
3217f 6c 70 20 65 78 70 72 20 61 73 20 2a 2f 0a 7b 0a  lp expr as */.{.
32180 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
32181 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  y14 = sqlite3Exp
32182 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
32183 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se, yymsp[-2].mi
32184 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b  nor.yy14, yymsp[
32185 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -1].minor.yy346.
32186 70 45 78 70 72 29 3b 0a 20 20 20 69 66 28 20 79  pExpr);.   if( y
32187 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32188 30 2e 6e 3e 30 20 29 20 73 71 6c 69 74 65 33 45  0.n>0 ) sqlite3E
32189 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70  xprListSetName(p
3218a 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
3218b 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d 73 70 5b  or.yy14, &yymsp[
3218c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 31 29  0].minor.yy0, 1)
3218d 3b 0a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  ;.   sqlite3Expr
3218e 4c 69 73 74 53 65 74 53 70 61 6e 28 70 50 61 72  ListSetSpan(pPar
3218f 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  se,yygotominor.y
32190 79 31 34 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y14,&yymsp[-1].m
32191 69 6e 6f 72 2e 79 79 33 34 36 29 3b 0a 7d 0a 20  inor.yy346);.}. 
32192 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32193 20 20 20 20 63 61 73 65 20 31 32 34 3a 20 2f 2a      case 124: /*
32194 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20   selcollist ::= 
32195 73 63 6c 70 20 53 54 41 52 20 2a 2f 0a 7b 0a 20  sclp STAR */.{. 
32196 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74   Expr *p = sqlit
32197 65 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64  e3Expr(pParse->d
32198 62 2c 20 54 4b 5f 41 4c 4c 2c 20 30 29 3b 0a 20  b, TK_ALL, 0);. 
32199 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3219a 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  4 = sqlite3ExprL
3219b 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
3219c 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
3219d 72 2e 79 79 31 34 2c 20 70 29 3b 0a 7d 0a 20 20  r.yy14, p);.}.  
3219e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3219f 20 20 20 63 61 73 65 20 31 32 35 3a 20 2f 2a 20     case 125: /* 
321a0 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73  selcollist ::= s
321a1 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41 52 20  clp nm DOT STAR 
321a2 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 52 69  */.{.  Expr *pRi
321a3 67 68 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78  ght = sqlite3PEx
321a4 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 4c  pr(pParse, TK_AL
321a5 4c 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  L, 0, 0, &yymsp[
321a6 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
321a7 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 73   Expr *pLeft = s
321a8 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
321a9 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
321aa 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
321ab 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a  r.yy0);.  Expr *
321ac 70 44 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 45  pDot = sqlite3PE
321ad 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
321ae 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  OT, pLeft, pRigh
321af 74 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  t, 0);.  yygotom
321b0 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
321b1 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
321b2 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
321b3 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 70  3].minor.yy14, p
321b4 44 6f 74 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  Dot);.}.        
321b5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
321b6 65 20 31 32 38 3a 20 2f 2a 20 61 73 20 3a 3a 3d  e 128: /* as ::=
321b7 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
321b8 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20  .yy0.n = 0;}.   
321b9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
321ba 20 20 63 61 73 65 20 31 32 39 3a 20 2f 2a 20 66    case 129: /* f
321bb 72 6f 6d 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f  rom ::= */.{yygo
321bc 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73  tominor.yy65 = s
321bd 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
321be 72 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73  ro(pParse->db, s
321bf 69 7a 65 6f 66 28 2a 79 79 67 6f 74 6f 6d 69 6e  izeof(*yygotomin
321c0 6f 72 2e 79 79 36 35 29 29 3b 7d 0a 20 20 20 20  or.yy65));}.    
321c1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
321c2 20 63 61 73 65 20 31 33 30 3a 20 2f 2a 20 66 72   case 130: /* fr
321c3 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c 74  om ::= FROM selt
321c4 61 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79 79  ablist */.{.  yy
321c5 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d  gotominor.yy65 =
321c6 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
321c7 79 79 36 35 3b 0a 20 20 73 71 6c 69 74 65 33 53  yy65;.  sqlite3S
321c8 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54  rcListShiftJoinT
321c9 79 70 65 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ype(yygotominor.
321ca 79 79 36 35 29 3b 0a 7d 0a 20 20 20 20 20 20 20  yy65);.}.       
321cb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
321cc 73 65 20 31 33 31 3a 20 2f 2a 20 73 74 6c 5f 70  se 131: /* stl_p
321cd 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62  refix ::= seltab
321ce 6c 69 73 74 20 6a 6f 69 6e 6f 70 20 2a 2f 0a 7b  list joinop */.{
321cf 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  .   yygotominor.
321d0 79 79 36 35 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  yy65 = yymsp[-1]
321d1 2e 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a 20 20 20  .minor.yy65;.   
321d2 69 66 28 20 41 4c 57 41 59 53 28 79 79 67 6f 74  if( ALWAYS(yygot
321d3 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 26 26 20 79  ominor.yy65 && y
321d4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 2d  ygotominor.yy65-
321d5 3e 6e 53 72 63 3e 30 29 20 29 20 79 79 67 6f 74  >nSrc>0) ) yygot
321d6 6f 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e 61 5b 79  ominor.yy65->a[y
321d7 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 2d  ygotominor.yy65-
321d8 3e 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e 74 79 70  >nSrc-1].jointyp
321d9 65 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d  e = (u8)yymsp[0]
321da 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a  .minor.yy328;.}.
321db 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
321dc 20 20 20 20 20 63 61 73 65 20 31 33 32 3a 20 2f       case 132: /
321dd 2a 20 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d  * stl_prefix ::=
321de 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
321df 2e 79 79 36 35 20 3d 20 30 3b 7d 0a 20 20 20 20  .yy65 = 0;}.    
321e0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
321e1 20 63 61 73 65 20 31 33 33 3a 20 2f 2a 20 73 65   case 133: /* se
321e2 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c  ltablist ::= stl
321e3 5f 70 72 65 66 69 78 20 6e 6d 20 64 62 6e 6d 20  _prefix nm dbnm 
321e4 61 73 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 6f  as indexed_opt o
321e5 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20  n_opt using_opt 
321e6 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
321e7 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69 74 65  or.yy65 = sqlite
321e8 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72  3SrcListAppendFr
321e9 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79 79  omTerm(pParse,yy
321ea 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
321eb 36 35 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  65,&yymsp[-5].mi
321ec 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
321ed 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  4].minor.yy0,&yy
321ee 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
321ef 30 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0,0,yymsp[-1].mi
321f0 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b  nor.yy132,yymsp[
321f1 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 29 3b  0].minor.yy408);
321f2 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
321f3 74 49 6e 64 65 78 65 64 42 79 28 70 50 61 72 73  tIndexedBy(pPars
321f4 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  e, yygotominor.y
321f5 79 36 35 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  y65, &yymsp[-2].
321f6 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
321f7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
321f8 20 20 20 63 61 73 65 20 31 33 34 3a 20 2f 2a 20     case 134: /* 
321f9 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73  seltablist ::= s
321fa 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c  tl_prefix LP sel
321fb 65 63 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74  ect RP as on_opt
321fc 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a   using_opt */.{.
321fd 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
321fe 79 79 36 35 20 3d 20 73 71 6c 69 74 65 33 53 72  yy65 = sqlite3Sr
321ff 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54  cListAppendFromT
32200 65 72 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70  erm(pParse,yymsp
32201 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c  [-6].minor.yy65,
32202 30 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  0,0,&yymsp[-2].m
32203 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d  inor.yy0,yymsp[-
32204 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 79 79 6d  4].minor.yy3,yym
32205 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
32206 33 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  32,yymsp[0].mino
32207 72 2e 79 79 34 30 38 29 3b 0a 20 20 7d 0a 20 20  r.yy408);.  }.  
32208 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32209 20 20 20 63 61 73 65 20 31 33 35 3a 20 2f 2a 20     case 135: /* 
3220a 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73  seltablist ::= s
3220b 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c  tl_prefix LP sel
3220c 74 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f 6e  tablist RP as on
3220d 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a  _opt using_opt *
3220e 2f 0a 7b 0a 20 20 20 20 69 66 28 20 79 79 6d 73  /.{.    if( yyms
3220f 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35  p[-6].minor.yy65
32210 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b 2d 32 5d  ==0 && yymsp[-2]
32211 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d 30 20  .minor.yy0.n==0 
32212 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  && yymsp[-1].min
32213 6f 72 2e 79 79 31 33 32 3d 3d 30 20 26 26 20 79  or.yy132==0 && y
32214 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32215 34 30 38 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  408==0 ){.      
32216 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35  yygotominor.yy65
32217 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e   = yymsp[-4].min
32218 6f 72 2e 79 79 36 35 3b 0a 20 20 20 20 7d 65 6c  or.yy65;.    }el
32219 73 65 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74  se{.      Select
3221a 20 2a 70 53 75 62 71 75 65 72 79 3b 0a 20 20 20   *pSubquery;.   
3221b 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
3221c 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 79  tShiftJoinType(y
3221d 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3221e 79 36 35 29 3b 0a 20 20 20 20 20 20 70 53 75 62  y65);.      pSub
3221f 71 75 65 72 79 20 3d 20 73 71 6c 69 74 65 33 53  query = sqlite3S
32220 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
32221 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  0,yymsp[-4].mino
32222 72 2e 79 79 36 35 2c 30 2c 30 2c 30 2c 30 2c 30  r.yy65,0,0,0,0,0
32223 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 79 79 67  ,0,0);.      yyg
32224 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20  otominor.yy65 = 
32225 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
32226 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61  pendFromTerm(pPa
32227 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  rse,yymsp[-6].mi
32228 6e 6f 72 2e 79 79 36 35 2c 30 2c 30 2c 26 79 79  nor.yy65,0,0,&yy
32229 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3222a 30 2c 70 53 75 62 71 75 65 72 79 2c 79 79 6d 73  0,pSubquery,yyms
3222b 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33  p[-1].minor.yy13
3222c 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  2,yymsp[0].minor
3222d 2e 79 79 34 30 38 29 3b 0a 20 20 20 20 7d 0a 20  .yy408);.    }. 
3222e 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
3222f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 36  ;.      case 136
32230 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 2a 2f  : /* dbnm ::= */
32231 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 35 3a  .      case 145:
32232 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20   /* indexed_opt 
32233 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
32234 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 34 35 29  e(yyruleno==145)
32235 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
32236 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d 69  y0.z=0; yygotomi
32237 6e 6f 72 2e 79 79 30 2e 6e 3d 30 3b 7d 0a 20 20  nor.yy0.n=0;}.  
32238 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32239 20 20 20 63 61 73 65 20 31 33 38 3a 20 2f 2a 20     case 138: /* 
3223a 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d 20  fullname ::= nm 
3223b 64 62 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  dbnm */.{yygotom
3223c 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69  inor.yy65 = sqli
3223d 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
3223e 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79  (pParse->db,0,&y
3223f 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32240 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
32241 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
32242 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32243 61 73 65 20 31 33 39 3a 20 2f 2a 20 6a 6f 69 6e  ase 139: /* join
32244 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49  op ::= COMMA|JOI
32245 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  N */.{ yygotomin
32246 6f 72 2e 79 79 33 32 38 20 3d 20 4a 54 5f 49 4e  or.yy328 = JT_IN
32247 4e 45 52 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  NER; }.        b
32248 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32249 20 31 34 30 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20   140: /* joinop 
3224a 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e  ::= JOIN_KW JOIN
3224b 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
3224c 72 2e 79 79 33 32 38 20 3d 20 73 71 6c 69 74 65  r.yy328 = sqlite
3224d 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65  3JoinType(pParse
3224e 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
3224f 72 2e 79 79 30 2c 30 2c 30 29 3b 20 7d 0a 20 20  r.yy0,0,0); }.  
32250 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32251 20 20 20 63 61 73 65 20 31 34 31 3a 20 2f 2a 20     case 141: /* 
32252 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
32253 4b 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b 20  KW nm JOIN */.{ 
32254 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
32255 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54  8 = sqlite3JoinT
32256 79 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  ype(pParse,&yyms
32257 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
32258 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
32259 2e 79 79 30 2c 30 29 3b 20 7d 0a 20 20 20 20 20  .yy0,0); }.     
3225a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3225b 63 61 73 65 20 31 34 32 3a 20 2f 2a 20 6a 6f 69  case 142: /* joi
3225c 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20  nop ::= JOIN_KW 
3225d 6e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b 20  nm nm JOIN */.{ 
3225e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
3225f 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54  8 = sqlite3JoinT
32260 79 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  ype(pParse,&yyms
32261 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
32262 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
32263 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  .yy0,&yymsp[-1].
32264 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a 20 20  minor.yy0); }.  
32265 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32266 20 20 20 63 61 73 65 20 31 34 33 3a 20 2f 2a 20     case 143: /* 
32267 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65 78  on_opt ::= ON ex
32268 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
32269 20 31 35 34 3a 20 2f 2a 20 73 6f 72 74 69 74 65   154: /* sortite
3226a 6d 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20 79 79  m ::= expr */ yy
3226b 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3226c 6f 3d 3d 31 35 34 29 3b 0a 20 20 20 20 20 20 63  o==154);.      c
3226d 61 73 65 20 31 36 31 3a 20 2f 2a 20 68 61 76 69  ase 161: /* havi
3226e 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56 49 4e  ng_opt ::= HAVIN
3226f 47 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  G expr */ yytest
32270 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
32271 36 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  61);.      case 
32272 31 36 38 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70  168: /* where_op
32273 74 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70 72  t ::= WHERE expr
32274 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32275 79 72 75 6c 65 6e 6f 3d 3d 31 36 38 29 3b 0a 20  yruleno==168);. 
32276 20 20 20 20 20 63 61 73 65 20 32 33 35 3a 20 2f       case 235: /
32277 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20  * case_else ::= 
32278 45 4c 53 45 20 65 78 70 72 20 2a 2f 20 79 79 74  ELSE expr */ yyt
32279 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3227a 3d 3d 32 33 35 29 3b 0a 20 20 20 20 20 20 63 61  ==235);.      ca
3227b 73 65 20 32 33 37 3a 20 2f 2a 20 63 61 73 65 5f  se 237: /* case_
3227c 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70 72  operand ::= expr
3227d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3227e 79 72 75 6c 65 6e 6f 3d 3d 32 33 37 29 3b 0a 7b  yruleno==237);.{
3227f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 33  yygotominor.yy13
32280 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  2 = yymsp[0].min
32281 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 7d  or.yy346.pExpr;}
32282 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32283 20 20 20 20 20 20 63 61 73 65 20 31 34 34 3a 20        case 144: 
32284 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  /* on_opt ::= */
32285 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 30 3a  .      case 160:
32286 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a   /* having_opt :
32287 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
32288 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 30 29 3b  (yyruleno==160);
32289 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 37 3a  .      case 167:
3228a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a   /* where_opt ::
3228b 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3228c 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 37 29 3b 0a  yyruleno==167);.
3228d 20 20 20 20 20 20 63 61 73 65 20 32 33 36 3a 20        case 236: 
3228e 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d  /* case_else ::=
3228f 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32290 79 72 75 6c 65 6e 6f 3d 3d 32 33 36 29 3b 0a 20  yruleno==236);. 
32291 20 20 20 20 20 63 61 73 65 20 32 33 38 3a 20 2f       case 238: /
32292 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a  * case_operand :
32293 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
32294 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 38 29 3b  (yyruleno==238);
32295 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
32296 31 33 32 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  132 = 0;}.      
32297 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32298 61 73 65 20 31 34 37 3a 20 2f 2a 20 69 6e 64 65  ase 147: /* inde
32299 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20  xed_opt ::= NOT 
3229a 49 4e 44 45 58 45 44 20 2a 2f 0a 7b 79 79 67 6f  INDEXED */.{yygo
3229b 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b  tominor.yy0.z=0;
3229c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
3229d 2e 6e 3d 31 3b 7d 0a 20 20 20 20 20 20 20 20 62  .n=1;}.        b
3229e 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3229f 20 31 34 38 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f   148: /* using_o
322a0 70 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20  pt ::= USING LP 
322a1 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f  inscollist RP */
322a2 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 30 3a  .      case 180:
322a3 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f   /* inscollist_o
322a4 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c  pt ::= LP inscol
322a5 6c 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65 73  list RP */ yytes
322a6 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
322a7 31 38 30 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  180);.{yygotomin
322a8 6f 72 2e 79 79 34 30 38 20 3d 20 79 79 6d 73 70  or.yy408 = yymsp
322a9 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38  [-1].minor.yy408
322aa 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
322ab 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 39  ;.      case 149
322ac 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a  : /* using_opt :
322ad 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
322ae 20 31 37 39 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c   179: /* inscoll
322af 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79  ist_opt ::= */ y
322b0 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
322b1 6e 6f 3d 3d 31 37 39 29 3b 0a 7b 79 79 67 6f 74  no==179);.{yygot
322b2 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20 30  ominor.yy408 = 0
322b3 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
322b4 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 31  ;.      case 151
322b5 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74  : /* orderby_opt
322b6 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20 73 6f   ::= ORDER BY so
322b7 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20  rtlist */.      
322b8 63 61 73 65 20 31 35 39 3a 20 2f 2a 20 67 72 6f  case 159: /* gro
322b9 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f  upby_opt ::= GRO
322ba 55 50 20 42 59 20 6e 65 78 70 72 6c 69 73 74 20  UP BY nexprlist 
322bb 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
322bc 72 75 6c 65 6e 6f 3d 3d 31 35 39 29 3b 0a 20 20  ruleno==159);.  
322bd 20 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f 2a      case 239: /*
322be 20 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65   exprlist ::= ne
322bf 78 70 72 6c 69 73 74 20 2a 2f 20 79 79 74 65 73  xprlist */ yytes
322c0 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
322c1 32 33 39 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  239);.{yygotomin
322c2 6f 72 2e 79 79 31 34 20 3d 20 79 79 6d 73 70 5b  or.yy14 = yymsp[
322c3 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 3b 7d 0a  0].minor.yy14;}.
322c4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
322c5 20 20 20 20 20 63 61 73 65 20 31 35 32 3a 20 2f       case 152: /
322c6 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73  * sortlist ::= s
322c7 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f  ortlist COMMA so
322c8 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72  rtitem sortorder
322c9 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
322ca 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
322cb 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
322cc 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33  (pParse,yymsp[-3
322cd 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d  ].minor.yy14,yym
322ce 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
322cf 33 32 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74  32);.  if( yygot
322d0 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 29 20 79 79  ominor.yy14 ) yy
322d1 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e  gotominor.yy14->
322d2 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  a[yygotominor.yy
322d3 31 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72  14->nExpr-1].sor
322d4 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d  tOrder = (u8)yym
322d5 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
322d6 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  8;.}.        bre
322d7 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
322d8 35 33 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20  53: /* sortlist 
322d9 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f 72  ::= sortitem sor
322da 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79  torder */.{.  yy
322db 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d  gotominor.yy14 =
322dc 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
322dd 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
322de 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
322df 79 79 31 33 32 29 3b 0a 20 20 69 66 28 20 79 79  yy132);.  if( yy
322e0 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 26  gotominor.yy14 &
322e1 26 20 41 4c 57 41 59 53 28 79 79 67 6f 74 6f 6d  & ALWAYS(yygotom
322e2 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 29 20 29 20  inor.yy14->a) ) 
322e3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
322e4 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72  ->a[0].sortOrder
322e5 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e   = (u8)yymsp[0].
322e6 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a 20  minor.yy328;.}. 
322e7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
322e8 20 20 20 20 63 61 73 65 20 31 35 35 3a 20 2f 2a      case 155: /*
322e9 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41   sortorder ::= A
322ea 53 43 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  SC */.      case
322eb 20 31 35 37 3a 20 2f 2a 20 73 6f 72 74 6f 72 64   157: /* sortord
322ec 65 72 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  er ::= */ yytest
322ed 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
322ee 35 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  57);.{yygotomino
322ef 72 2e 79 79 33 32 38 20 3d 20 53 51 4c 49 54 45  r.yy328 = SQLITE
322f0 5f 53 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20 20  _SO_ASC;}.      
322f1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
322f2 61 73 65 20 31 35 36 3a 20 2f 2a 20 73 6f 72 74  ase 156: /* sort
322f3 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 20 2a  order ::= DESC *
322f4 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
322f5 79 33 32 38 20 3d 20 53 51 4c 49 54 45 5f 53 4f  y328 = SQLITE_SO
322f6 5f 44 45 53 43 3b 7d 0a 20 20 20 20 20 20 20 20  _DESC;}.        
322f7 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
322f8 65 20 31 36 32 3a 20 2f 2a 20 6c 69 6d 69 74 5f  e 162: /* limit_
322f9 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f  opt ::= */.{yygo
322fa 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c  tominor.yy476.pL
322fb 69 6d 69 74 20 3d 20 30 3b 20 79 79 67 6f 74 6f  imit = 0; yygoto
322fc 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66 66  minor.yy476.pOff
322fd 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  set = 0;}.      
322fe 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
322ff 61 73 65 20 31 36 33 3a 20 2f 2a 20 6c 69 6d 69  ase 163: /* limi
32300 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20  t_opt ::= LIMIT 
32301 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  expr */.{yygotom
32302 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69 6d 69  inor.yy476.pLimi
32303 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  t = yymsp[0].min
32304 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20  or.yy346.pExpr; 
32305 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37  yygotominor.yy47
32306 36 2e 70 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0a  6.pOffset = 0;}.
32307 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32308 20 20 20 20 20 63 61 73 65 20 31 36 34 3a 20 2f       case 164: /
32309 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20  * limit_opt ::= 
3230a 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46 53 45  LIMIT expr OFFSE
3230b 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74  T expr */.{yygot
3230c 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69  ominor.yy476.pLi
3230d 6d 69 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e  mit = yymsp[-2].
3230e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
3230f 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r; yygotominor.y
32310 79 34 37 36 2e 70 4f 66 66 73 65 74 20 3d 20 79  y476.pOffset = y
32311 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32312 33 34 36 2e 70 45 78 70 72 3b 7d 0a 20 20 20 20  346.pExpr;}.    
32313 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32314 20 63 61 73 65 20 31 36 35 3a 20 2f 2a 20 6c 69   case 165: /* li
32315 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49  mit_opt ::= LIMI
32316 54 20 65 78 70 72 20 43 4f 4d 4d 41 20 65 78 70  T expr COMMA exp
32317 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  r */.{yygotomino
32318 72 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 20  r.yy476.pOffset 
32319 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  = yymsp[-2].mino
3231a 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20 79  r.yy346.pExpr; y
3231b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36  ygotominor.yy476
3231c 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b  .pLimit = yymsp[
3231d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
3231e 45 78 70 72 3b 7d 0a 20 20 20 20 20 20 20 20 62  Expr;}.        b
3231f 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32320 20 31 36 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   166: /* cmd ::=
32321 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c   DELETE FROM ful
32322 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70  lname indexed_op
32323 74 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b  t where_opt */.{
32324 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
32325 74 49 6e 64 65 78 65 64 42 79 28 70 50 61 72 73  tIndexedBy(pPars
32326 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  e, yymsp[-2].min
32327 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70 5b  or.yy65, &yymsp[
32328 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -1].minor.yy0);.
32329 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46    sqlite3DeleteF
3232a 72 6f 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70  rom(pParse,yymsp
3232b 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c  [-2].minor.yy65,
3232c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3232d 79 31 33 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y132);.}.       
3232e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3232f 73 65 20 31 36 39 3a 20 2f 2a 20 63 6d 64 20 3a  se 169: /* cmd :
32330 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66  := UPDATE orconf
32331 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65   fullname indexe
32332 64 5f 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73  d_opt SET setlis
32333 74 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b  t where_opt */.{
32334 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
32335 74 49 6e 64 65 78 65 64 42 79 28 70 50 61 72 73  tIndexedBy(pPars
32336 65 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  e, yymsp[-4].min
32337 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70 5b  or.yy65, &yymsp[
32338 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -3].minor.yy0);.
32339 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
3233a 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61  tCheckLength(pPa
3233b 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  rse,yymsp[-1].mi
3233c 6e 6f 72 2e 79 79 31 34 2c 22 73 65 74 20 6c 69  nor.yy14,"set li
3233d 73 74 22 29 3b 20 0a 20 20 73 71 6c 69 74 65 33  st"); .  sqlite3
3233e 55 70 64 61 74 65 28 70 50 61 72 73 65 2c 79 79  Update(pParse,yy
3233f 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
32340 36 35 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  65,yymsp[-1].min
32341 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d  or.yy14,yymsp[0]
32342 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d  .minor.yy132,yym
32343 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-5].minor.yy1
32344 38 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  86);.}.        b
32345 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32346 20 31 37 30 3a 20 2f 2a 20 73 65 74 6c 69 73 74   170: /* setlist
32347 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d   ::= setlist COM
32348 4d 41 20 6e 6d 20 45 51 20 65 78 70 72 20 2a 2f  MA nm EQ expr */
32349 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
3234a 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45  .yy14 = sqlite3E
3234b 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
3234c 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  arse, yymsp[-4].
3234d 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73  minor.yy14, yyms
3234e 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
3234f 2e 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  .pExpr);.  sqlit
32350 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
32351 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  e(pParse, yygoto
32352 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d  minor.yy14, &yym
32353 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
32354 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  , 1);.}.        
32355 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32356 65 20 31 37 31 3a 20 2f 2a 20 73 65 74 6c 69 73  e 171: /* setlis
32357 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72  t ::= nm EQ expr
32358 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
32359 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
3235a 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
3235b 28 70 50 61 72 73 65 2c 20 30 2c 20 79 79 6d 73  (pParse, 0, yyms
3235c 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
3235d 2e 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  .pExpr);.  sqlit
3235e 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
3235f 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  e(pParse, yygoto
32360 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d  minor.yy14, &yym
32361 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
32362 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  , 1);.}.        
32363 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32364 65 20 31 37 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 172: /* cmd ::
32365 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
32366 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
32367 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53  llist_opt VALUES
32368 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 20   LP itemlist RP 
32369 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72  */.{sqlite3Inser
3236a 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  t(pParse, yymsp[
3236b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20  -5].minor.yy65, 
3236c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3236d 79 79 31 34 2c 20 30 2c 20 79 79 6d 73 70 5b 2d  yy14, 0, yymsp[-
3236e 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20  4].minor.yy408, 
3236f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e  yymsp[-7].minor.
32370 79 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20 20  yy186);}.       
32371 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32372 73 65 20 31 37 33 3a 20 2f 2a 20 63 6d 64 20 3a  se 173: /* cmd :
32373 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
32374 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
32375 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63  ollist_opt selec
32376 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73  t */.{sqlite3Ins
32377 65 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ert(pParse, yyms
32378 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35  p[-2].minor.yy65
32379 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  , 0, yymsp[0].mi
3237a 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73 70 5b 2d  nor.yy3, yymsp[-
3237b 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20  1].minor.yy408, 
3237c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3237d 79 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20 20  yy186);}.       
3237e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3237f 73 65 20 31 37 34 3a 20 2f 2a 20 63 6d 64 20 3a  se 174: /* cmd :
32380 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
32381 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
32382 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55  ollist_opt DEFAU
32383 4c 54 20 56 41 4c 55 45 53 20 2a 2f 0a 7b 73 71  LT VALUES */.{sq
32384 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72  lite3Insert(pPar
32385 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  se, yymsp[-3].mi
32386 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20 30 2c 20  nor.yy65, 0, 0, 
32387 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
32388 79 79 34 30 38 2c 20 79 79 6d 73 70 5b 2d 35 5d  yy408, yymsp[-5]
32389 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a  .minor.yy186);}.
3238a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3238b 20 20 20 20 20 63 61 73 65 20 31 37 35 3a 20 2f       case 175: /
3238c 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d  * insert_cmd ::=
3238d 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 20 2a   INSERT orconf *
3238e 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
3238f 79 31 38 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y186 = yymsp[0].
32390 6d 69 6e 6f 72 2e 79 79 31 38 36 3b 7d 0a 20 20  minor.yy186;}.  
32391 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32392 20 20 20 63 61 73 65 20 31 37 36 3a 20 2f 2a 20     case 176: /* 
32393 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 52  insert_cmd ::= R
32394 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79 67 6f 74  EPLACE */.{yygot
32395 6f 6d 69 6e 6f 72 2e 79 79 31 38 36 20 3d 20 4f  ominor.yy186 = O
32396 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20 20 20  E_Replace;}.    
32397 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32398 20 63 61 73 65 20 31 37 37 3a 20 2f 2a 20 69 74   case 177: /* it
32399 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d 6c  emlist ::= iteml
3239a 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a  ist COMMA expr *
3239b 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 31  /.      case 241
3239c 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a  : /* nexprlist :
3239d 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d  := nexprlist COM
3239e 4d 41 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  MA expr */ yytes
3239f 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
323a0 32 34 31 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  241);.{yygotomin
323a1 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65  or.yy14 = sqlite
323a2 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
323a3 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d  pParse,yymsp[-2]
323a4 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73  .minor.yy14,yyms
323a5 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
323a6 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20  .pExpr);}.      
323a7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
323a8 61 73 65 20 31 37 38 3a 20 2f 2a 20 69 74 65 6d  ase 178: /* item
323a9 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f  list ::= expr */
323aa 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 32 3a  .      case 242:
323ab 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a   /* nexprlist ::
323ac 3d 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  = expr */ yytest
323ad 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
323ae 34 32 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  42);.{yygotomino
323af 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
323b0 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
323b1 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d  Parse,0,yymsp[0]
323b2 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
323b3 70 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  pr);}.        br
323b4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
323b5 31 38 31 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69  181: /* inscolli
323b6 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73  st ::= inscollis
323b7 74 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f 0a 7b 79  t COMMA nm */.{y
323b8 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38  ygotominor.yy408
323b9 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74   = sqlite3IdList
323ba 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
323bb 62 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  b,yymsp[-2].mino
323bc 72 2e 79 79 34 30 38 2c 26 79 79 6d 73 70 5b 30  r.yy408,&yymsp[0
323bd 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
323be 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
323bf 20 20 20 20 63 61 73 65 20 31 38 32 3a 20 2f 2a      case 182: /*
323c0 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20   inscollist ::= 
323c1 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  nm */.{yygotomin
323c2 6f 72 2e 79 79 34 30 38 20 3d 20 73 71 6c 69 74  or.yy408 = sqlit
323c3 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 70  e3IdListAppend(p
323c4 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d  Parse->db,0,&yym
323c5 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
323c6 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
323c7 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 33  ;.      case 183
323c8 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 74 65  : /* expr ::= te
323c9 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  rm */.      case
323ca 20 32 31 31 3a 20 2f 2a 20 65 73 63 61 70 65 20   211: /* escape 
323cb 3a 3a 3d 20 45 53 43 41 50 45 20 65 78 70 72 20  ::= ESCAPE expr 
323cc 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
323cd 72 75 6c 65 6e 6f 3d 3d 32 31 31 29 3b 0a 7b 79  ruleno==211);.{y
323ce 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
323cf 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
323d0 72 2e 79 79 33 34 36 3b 7d 0a 20 20 20 20 20 20  r.yy346;}.      
323d1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
323d2 61 73 65 20 31 38 34 3a 20 2f 2a 20 65 78 70 72  ase 184: /* expr
323d3 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52 50 20   ::= LP expr RP 
323d4 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
323d5 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 79 79  yy346.pExpr = yy
323d6 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
323d7 33 34 36 2e 70 45 78 70 72 3b 20 73 70 61 6e 53  346.pExpr; spanS
323d8 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  et(&yygotominor.
323d9 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 32 5d  yy346,&yymsp[-2]
323da 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
323db 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
323dc 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
323dd 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 35 3a  .      case 185:
323de 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c   /* term ::= NUL
323df 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  L */.      case 
323e0 31 39 30 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d  190: /* term ::=
323e1 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42   INTEGER|FLOAT|B
323e2 4c 4f 42 20 2a 2f 20 79 79 74 65 73 74 63 61 73  LOB */ yytestcas
323e3 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 39 30 29  e(yyruleno==190)
323e4 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 31  ;.      case 191
323e5 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 53 54  : /* term ::= ST
323e6 52 49 4e 47 20 2a 2f 20 79 79 74 65 73 74 63 61  RING */ yytestca
323e7 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 39 31  se(yyruleno==191
323e8 29 3b 0a 7b 73 70 61 6e 45 78 70 72 28 26 79 79  );.{spanExpr(&yy
323e9 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
323ea 20 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30   pParse, yymsp[0
323eb 5d 2e 6d 61 6a 6f 72 2c 20 26 79 79 6d 73 70 5b  ].major, &yymsp[
323ec 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
323ed 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
323ee 20 20 20 20 20 63 61 73 65 20 31 38 36 3a 20 2f       case 186: /
323ef 2a 20 65 78 70 72 20 3a 3a 3d 20 69 64 20 2a 2f  * expr ::= id */
323f0 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 37 3a  .      case 187:
323f1 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4a 4f 49   /* expr ::= JOI
323f2 4e 5f 4b 57 20 2a 2f 20 79 79 74 65 73 74 63 61  N_KW */ yytestca
323f3 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 38 37  se(yyruleno==187
323f4 29 3b 0a 7b 73 70 61 6e 45 78 70 72 28 26 79 79  );.{spanExpr(&yy
323f5 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
323f6 20 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20   pParse, TK_ID, 
323f7 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
323f8 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
323f9 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
323fa 20 31 38 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   188: /* expr ::
323fb 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b  = nm DOT nm */.{
323fc 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20 3d  .  Expr *temp1 =
323fd 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
323fe 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20  arse, TK_ID, 0, 
323ff 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  0, &yymsp[-2].mi
32400 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72  nor.yy0);.  Expr
32401 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69 74 65   *temp2 = sqlite
32402 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
32403 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d  K_ID, 0, 0, &yym
32404 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
32405 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
32406 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
32407 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
32408 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31  e, TK_DOT, temp1
32409 2c 20 74 65 6d 70 32 2c 20 30 29 3b 0a 20 20 73  , temp2, 0);.  s
3240a 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69  panSet(&yygotomi
3240b 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70  nor.yy346,&yymsp
3240c 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-2].minor.yy0,&
3240d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3240e 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
3240f 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32410 20 31 38 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   189: /* expr ::
32411 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54 20  = nm DOT nm DOT 
32412 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a  nm */.{.  Expr *
32413 74 65 6d 70 31 20 3d 20 73 71 6c 69 74 65 33 50  temp1 = sqlite3P
32414 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
32415 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70  ID, 0, 0, &yymsp
32416 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-4].minor.yy0);
32417 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 32 20 3d  .  Expr *temp2 =
32418 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
32419 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20  arse, TK_ID, 0, 
3241a 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  0, &yymsp[-2].mi
3241b 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72  nor.yy0);.  Expr
3241c 20 2a 74 65 6d 70 33 20 3d 20 73 71 6c 69 74 65   *temp3 = sqlite
3241d 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3241e 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d  K_ID, 0, 0, &yym
3241f 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
32420 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 34 20  ;.  Expr *temp4 
32421 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
32422 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74  Parse, TK_DOT, t
32423 65 6d 70 32 2c 20 74 65 6d 70 33 2c 20 30 29 3b  emp2, temp3, 0);
32424 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
32425 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
32426 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32427 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c  , TK_DOT, temp1,
32428 20 74 65 6d 70 34 2c 20 30 29 3b 0a 20 20 73 70   temp4, 0);.  sp
32429 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
3242a 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
3242b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -4].minor.yy0,&y
3242c 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3242d 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
3242e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3242f 31 39 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  192: /* expr ::=
32430 20 52 45 47 49 53 54 45 52 20 2a 2f 0a 7b 0a 20   REGISTER */.{. 
32431 20 2f 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 61   /* When doing a
32432 20 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20 6f   nested parse, o
32433 6e 65 20 63 61 6e 20 69 6e 63 6c 75 64 65 20 74  ne can include t
32434 65 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72 65  erms in an expre
32435 73 73 69 6f 6e 0a 20 20 2a 2a 20 74 68 61 74 20  ssion.  ** that 
32436 6c 6f 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a 20  look like this: 
32437 20 20 23 31 20 23 32 20 2e 2e 2e 20 20 54 68 65    #1 #2 ...  The
32438 73 65 20 74 65 72 6d 73 20 72 65 66 65 72 20 74  se terms refer t
32439 6f 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a 2a  o registers.  **
3243a 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20   in the virtual 
3243b 6d 61 63 68 69 6e 65 2e 20 20 23 4e 20 69 73 20  machine.  #N is 
3243c 74 68 65 20 4e 2d 74 68 20 72 65 67 69 73 74 65  the N-th registe
3243d 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72  r. */.  if( pPar
3243e 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b  se->nested==0 ){
3243f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
32440 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65  rMsg(pParse, "ne
32441 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61  ar \"%T\": synta
32442 78 20 65 72 72 6f 72 22 2c 20 26 79 79 6d 73 70  x error", &yymsp
32443 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
32444 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
32445 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 30 3b  yy346.pExpr = 0;
32446 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 79 79  .  }else{.    yy
32447 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32448 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
32449 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
3244a 52 45 47 49 53 54 45 52 2c 20 30 2c 20 30 2c 20  REGISTER, 0, 0, 
3244b 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3244c 79 79 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79  yy0);.    if( yy
3244d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3244e 70 45 78 70 72 20 29 20 73 71 6c 69 74 65 33 47  pExpr ) sqlite3G
3244f 65 74 49 6e 74 33 32 28 26 79 79 6d 73 70 5b 30  etInt32(&yymsp[0
32450 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 31 5d  ].minor.yy0.z[1]
32451 2c 20 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  , &yygotominor.y
32452 79 33 34 36 2e 70 45 78 70 72 2d 3e 69 54 61 62  y346.pExpr->iTab
32453 6c 65 29 3b 0a 20 20 7d 0a 20 20 73 70 61 6e 53  le);.  }.  spanS
32454 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  et(&yygotominor.
32455 79 79 33 34 36 2c 20 26 79 79 6d 73 70 5b 30 5d  yy346, &yymsp[0]
32456 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
32457 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
32458 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
32459 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
3245a 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 56  3: /* expr ::= V
3245b 41 52 49 41 42 4c 45 20 2a 2f 0a 7b 0a 20 20 73  ARIABLE */.{.  s
3245c 70 61 6e 45 78 70 72 28 26 79 79 67 6f 74 6f 6d  panExpr(&yygotom
3245d 69 6e 6f 72 2e 79 79 33 34 36 2c 20 70 50 61 72  inor.yy346, pPar
3245e 73 65 2c 20 54 4b 5f 56 41 52 49 41 42 4c 45 2c  se, TK_VARIABLE,
3245f 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32460 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  .yy0);.  sqlite3
32461 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d  ExprAssignVarNum
32462 62 65 72 28 70 50 61 72 73 65 2c 20 79 79 67 6f  ber(pParse, yygo
32463 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32464 78 70 72 29 3b 0a 20 20 73 70 61 6e 53 65 74 28  xpr);.  spanSet(
32465 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
32466 34 36 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  46, &yymsp[0].mi
32467 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
32468 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
32469 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3246a 20 20 20 20 20 20 63 61 73 65 20 31 39 34 3a 20        case 194: 
3246b 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
3246c 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a   COLLATE ids */.
3246d 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
3246e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
3246f 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c  lite3ExprSetColl
32470 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
32471 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  2].minor.yy346.p
32472 45 78 70 72 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  Expr, &yymsp[0].
32473 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79 79  minor.yy0);.  yy
32474 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32475 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d  zStart = yymsp[-
32476 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  2].minor.yy346.z
32477 53 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d  Start;.  yygotom
32478 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20  inor.yy346.zEnd 
32479 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  = &yymsp[0].mino
3247a 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d  r.yy0.z[yymsp[0]
3247b 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 7d  .minor.yy0.n];.}
3247c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3247d 20 20 20 20 20 20 63 61 73 65 20 31 39 35 3a 20        case 195: 
3247e 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53 54  /* expr ::= CAST
3247f 20 4c 50 20 65 78 70 72 20 41 53 20 74 79 70 65   LP expr AS type
32480 74 6f 6b 65 6e 20 52 50 20 2a 2f 0a 7b 0a 20 20  token RP */.{.  
32481 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32482 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
32483 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
32484 4b 5f 43 41 53 54 2c 20 79 79 6d 73 70 5b 2d 33  K_CAST, yymsp[-3
32485 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
32486 78 70 72 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d  xpr, 0, &yymsp[-
32487 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  1].minor.yy0);. 
32488 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f   spanSet(&yygoto
32489 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d  minor.yy346,&yym
3248a 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-5].minor.yy0
3248b 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
3248c 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
3248d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3248e 73 65 20 31 39 36 3a 20 2f 2a 20 65 78 70 72 20  se 196: /* expr 
3248f 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73 74 69 6e  ::= ID LP distin
32490 63 74 20 65 78 70 72 6c 69 73 74 20 52 50 20 2a  ct exprlist RP *
32491 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b  /.{.  if( yymsp[
32492 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 20 26  -1].minor.yy14 &
32493 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  & yymsp[-1].mino
32494 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 3e 70 50  r.yy14->nExpr>pP
32495 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74  arse->db->aLimit
32496 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55  [SQLITE_LIMIT_FU
32497 4e 43 54 49 4f 4e 5f 41 52 47 5d 20 29 7b 0a 20  NCTION_ARG] ){. 
32498 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
32499 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
3249a 6d 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20 6f  many arguments o
3249b 6e 20 66 75 6e 63 74 69 6f 6e 20 25 54 22 2c 20  n function %T", 
3249c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
3249d 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67  .yy0);.  }.  yyg
3249e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
3249f 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78  Expr = sqlite3Ex
324a0 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  prFunction(pPars
324a1 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e, yymsp[-1].min
324a2 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d 73 70 5b  or.yy14, &yymsp[
324a3 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -4].minor.yy0);.
324a4 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74    spanSet(&yygot
324a5 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79  ominor.yy346,&yy
324a6 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
324a7 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
324a8 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79 79  r.yy0);.  if( yy
324a9 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
324aa 33 32 38 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e  328 && yygotomin
324ab 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29  or.yy346.pExpr )
324ac 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
324ad 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 66  r.yy346.pExpr->f
324ae 6c 61 67 73 20 7c 3d 20 45 50 5f 44 69 73 74 69  lags |= EP_Disti
324af 6e 63 74 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20  nct;.  }.}.     
324b0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
324b1 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 65 78 70  case 197: /* exp
324b2 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52  r ::= ID LP STAR
324b3 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
324b4 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
324b5 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  pr = sqlite3Expr
324b6 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c  Function(pParse,
324b7 20 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d   0, &yymsp[-3].m
324b8 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 70 61  inor.yy0);.  spa
324b9 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  nSet(&yygotomino
324ba 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d  r.yy346,&yymsp[-
324bb 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  3].minor.yy0,&yy
324bc 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
324bd 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
324be 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
324bf 39 38 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20  98: /* term ::= 
324c0 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 7b 0a 20 20  CTIME_KW */.{.  
324c1 2f 2a 20 54 68 65 20 43 55 52 52 45 4e 54 5f 54  /* The CURRENT_T
324c2 49 4d 45 2c 20 43 55 52 52 45 4e 54 5f 44 41 54  IME, CURRENT_DAT
324c3 45 2c 20 61 6e 64 20 43 55 52 52 45 4e 54 5f 54  E, and CURRENT_T
324c4 49 4d 45 53 54 41 4d 50 20 76 61 6c 75 65 73 20  IMESTAMP values 
324c5 61 72 65 0a 20 20 2a 2a 20 74 72 65 61 74 65 64  are.  ** treated
324c6 20 61 73 20 66 75 6e 63 74 69 6f 6e 73 20 74 68   as functions th
324c7 61 74 20 72 65 74 75 72 6e 20 63 6f 6e 73 74 61  at return consta
324c8 6e 74 73 20 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d  nts */.  yygotom
324c9 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
324ca 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75   = sqlite3ExprFu
324cb 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  nction(pParse, 0
324cc 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
324cd 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79 79 67  .yy0);.  if( yyg
324ce 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
324cf 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f  Expr ){.    yygo
324d0 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
324d1 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4e  xpr->op = TK_CON
324d2 53 54 5f 46 55 4e 43 3b 20 20 0a 20 20 7d 0a 20  ST_FUNC;  .  }. 
324d3 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f   spanSet(&yygoto
324d4 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20 26 79 79  minor.yy346, &yy
324d5 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
324d6 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
324d7 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
324d8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
324d9 61 73 65 20 31 39 39 3a 20 2f 2a 20 65 78 70 72  ase 199: /* expr
324da 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20 65 78   ::= expr AND ex
324db 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
324dc 20 32 30 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   200: /* expr ::
324dd 3d 20 65 78 70 72 20 4f 52 20 65 78 70 72 20 2a  = expr OR expr *
324de 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
324df 75 6c 65 6e 6f 3d 3d 32 30 30 29 3b 0a 20 20 20  uleno==200);.   
324e0 20 20 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20     case 201: /* 
324e1 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4c 54  expr ::= expr LT
324e2 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70 72 20 2a  |GT|GE|LE expr *
324e3 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
324e4 75 6c 65 6e 6f 3d 3d 32 30 31 29 3b 0a 20 20 20  uleno==201);.   
324e5 20 20 20 63 61 73 65 20 32 30 32 3a 20 2f 2a 20     case 202: /* 
324e6 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 45 51  expr ::= expr EQ
324e7 7c 4e 45 20 65 78 70 72 20 2a 2f 20 79 79 74 65  |NE expr */ yyte
324e8 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
324e9 3d 32 30 32 29 3b 0a 20 20 20 20 20 20 63 61 73  =202);.      cas
324ea 65 20 32 30 33 3a 20 2f 2a 20 65 78 70 72 20 3a  e 203: /* expr :
324eb 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44 7c 42  := expr BITAND|B
324ec 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53 48 49  ITOR|LSHIFT|RSHI
324ed 46 54 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  FT expr */ yytes
324ee 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
324ef 32 30 33 29 3b 0a 20 20 20 20 20 20 63 61 73 65  203);.      case
324f0 20 32 30 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   204: /* expr ::
324f1 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55  = expr PLUS|MINU
324f2 53 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  S expr */ yytest
324f3 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
324f4 30 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  04);.      case 
324f5 32 30 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  205: /* expr ::=
324f6 20 65 78 70 72 20 53 54 41 52 7c 53 4c 41 53 48   expr STAR|SLASH
324f7 7c 52 45 4d 20 65 78 70 72 20 2a 2f 20 79 79 74  |REM expr */ yyt
324f8 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
324f9 3d 3d 32 30 35 29 3b 0a 20 20 20 20 20 20 63 61  ==205);.      ca
324fa 73 65 20 32 30 36 3a 20 2f 2a 20 65 78 70 72 20  se 206: /* expr 
324fb 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54 20  ::= expr CONCAT 
324fc 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61  expr */ yytestca
324fd 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 36  se(yyruleno==206
324fe 29 3b 0a 7b 73 70 61 6e 42 69 6e 61 72 79 45 78  );.{spanBinaryEx
324ff 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr(&yygotominor.
32500 79 79 33 34 36 2c 70 50 61 72 73 65 2c 79 79 6d  yy346,pParse,yym
32501 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 26 79 79  sp[-1].major,&yy
32502 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32503 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  346,&yymsp[0].mi
32504 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a 20 20 20  nor.yy346);}.   
32505 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32506 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 6c    case 207: /* l
32507 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b  ikeop ::= LIKE_K
32508 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  W */.      case 
32509 32 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a  209: /* likeop :
3250a 3a 3d 20 4d 41 54 43 48 20 2a 2f 20 79 79 74 65  := MATCH */ yyte
3250b 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3250c 3d 32 30 39 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =209);.{yygotomi
3250d 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72 61 74  nor.yy96.eOperat
3250e 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  or = yymsp[0].mi
3250f 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d  nor.yy0; yygotom
32510 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74 20 3d 20  inor.yy96.not = 
32511 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
32512 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30  k;.      case 20
32513 38 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d  8: /* likeop ::=
32514 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0a   NOT LIKE_KW */.
32515 20 20 20 20 20 20 63 61 73 65 20 32 31 30 3a 20        case 210: 
32516 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f  /* likeop ::= NO
32517 54 20 4d 41 54 43 48 20 2a 2f 20 79 79 74 65 73  T MATCH */ yytes
32518 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32519 32 31 30 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  210);.{yygotomin
3251a 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72 61 74 6f  or.yy96.eOperato
3251b 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  r = yymsp[0].min
3251c 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69  or.yy0; yygotomi
3251d 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74 20 3d 20 31  nor.yy96.not = 1
3251e 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3251f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 32  ;.      case 212
32520 3a 20 2f 2a 20 65 73 63 61 70 65 20 3a 3a 3d 20  : /* escape ::= 
32521 2a 2f 0a 7b 6d 65 6d 73 65 74 28 26 79 79 67 6f  */.{memset(&yygo
32522 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 30 2c  tominor.yy346,0,
32523 73 69 7a 65 6f 66 28 79 79 67 6f 74 6f 6d 69 6e  sizeof(yygotomin
32524 6f 72 2e 79 79 33 34 36 29 29 3b 7d 0a 20 20 20  or.yy346));}.   
32525 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32526 20 20 63 61 73 65 20 32 31 33 3a 20 2f 2a 20 65    case 213: /* e
32527 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b  xpr ::= expr lik
32528 65 6f 70 20 65 78 70 72 20 65 73 63 61 70 65 20  eop expr escape 
32529 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  */.{.  ExprList 
3252a 2a 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74 20  *pList;.  pList 
3252b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
3252c 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
3252d 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
3252e 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
3252f 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
32530 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
32531 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79  pParse,pList, yy
32532 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32533 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 69 66  346.pExpr);.  if
32534 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ( yymsp[0].minor
32535 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a  .yy346.pExpr ){.
32536 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
32537 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
32538 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20  d(pParse,pList, 
32539 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3253a 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 7d  y346.pExpr);.  }
3253b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3253c 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
3253d 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
3253e 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20  (pParse, pList, 
3253f 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
32540 2e 79 79 39 36 2e 65 4f 70 65 72 61 74 6f 72 29  .yy96.eOperator)
32541 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32  ;.  if( yymsp[-2
32542 5d 2e 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74  ].minor.yy96.not
32543 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
32544 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
32545 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32546 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f  , TK_NOT, yygoto
32547 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32548 72 2c 20 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f  r, 0, 0);.  yygo
32549 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  tominor.yy346.zS
3254a 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d  tart = yymsp[-3]
3254b 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  .minor.yy346.zSt
3254c 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  art;.  yygotomin
3254d 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20  or.yy346.zEnd = 
3254e 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3254f 79 79 33 34 36 2e 7a 45 6e 64 3b 0a 20 20 69 66  yy346.zEnd;.  if
32550 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
32551 33 34 36 2e 70 45 78 70 72 20 29 20 79 79 67 6f  346.pExpr ) yygo
32552 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32553 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  xpr->flags |= EP
32554 5f 49 6e 66 69 78 46 75 6e 63 3b 0a 7d 0a 20 20  _InfixFunc;.}.  
32555 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32556 20 20 20 63 61 73 65 20 32 31 34 3a 20 2f 2a 20     case 214: /* 
32557 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53  expr ::= expr IS
32558 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a  NULL|NOTNULL */.
32559 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73 74 66 69  {spanUnaryPostfi
3255a 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  x(&yygotominor.y
3255b 79 33 34 36 2c 70 50 61 72 73 65 2c 79 79 6d 73  y346,pParse,yyms
3255c 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d 73  p[0].major,&yyms
3255d 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-1].minor.yy34
3255e 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  6,&yymsp[0].mino
3255f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
32560 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32561 73 65 20 32 31 35 3a 20 2f 2a 20 65 78 70 72 20  se 215: /* expr 
32562 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c  ::= expr NOT NUL
32563 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50  L */.{spanUnaryP
32564 6f 73 74 66 69 78 28 26 79 79 67 6f 74 6f 6d 69  ostfix(&yygotomi
32565 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65  nor.yy346,pParse
32566 2c 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 26 79 79 6d  ,TK_NOTNULL,&yym
32567 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
32568 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  46,&yymsp[0].min
32569 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
3256a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3256b 61 73 65 20 32 31 36 3a 20 2f 2a 20 65 78 70 72  ase 216: /* expr
3256c 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 65 78 70   ::= expr IS exp
3256d 72 20 2a 2f 0a 7b 0a 20 20 73 70 61 6e 42 69 6e  r */.{.  spanBin
3256e 61 72 79 45 78 70 72 28 26 79 79 67 6f 74 6f 6d  aryExpr(&yygotom
3256f 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73  inor.yy346,pPars
32570 65 2c 54 4b 5f 49 53 2c 26 79 79 6d 73 70 5b 2d  e,TK_IS,&yymsp[-
32571 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26  2].minor.yy346,&
32572 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32573 79 33 34 36 29 3b 0a 20 20 62 69 6e 61 72 79 54  y346);.  binaryT
32574 6f 55 6e 61 72 79 49 66 4e 75 6c 6c 28 70 50 61  oUnaryIfNull(pPa
32575 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
32576 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
32577 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32578 34 36 2e 70 45 78 70 72 2c 20 54 4b 5f 49 53 4e  46.pExpr, TK_ISN
32579 55 4c 4c 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  ULL);.}.        
3257a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3257b 65 20 32 31 37 3a 20 2f 2a 20 65 78 70 72 20 3a  e 217: /* expr :
3257c 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f 54 20 65  := expr IS NOT e
3257d 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 70 61 6e 42  xpr */.{.  spanB
3257e 69 6e 61 72 79 45 78 70 72 28 26 79 79 67 6f 74  inaryExpr(&yygot
3257f 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61  ominor.yy346,pPa
32580 72 73 65 2c 54 4b 5f 49 53 4e 4f 54 2c 26 79 79  rse,TK_ISNOT,&yy
32581 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32582 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  346,&yymsp[0].mi
32583 6e 6f 72 2e 79 79 33 34 36 29 3b 0a 20 20 62 69  nor.yy346);.  bi
32584 6e 61 72 79 54 6f 55 6e 61 72 79 49 66 4e 75 6c  naryToUnaryIfNul
32585 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  l(pParse, yymsp[
32586 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
32587 45 78 70 72 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  Expr, yygotomino
32588 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 54  r.yy346.pExpr, T
32589 4b 5f 4e 4f 54 4e 55 4c 4c 29 3b 0a 7d 0a 20 20  K_NOTNULL);.}.  
3258a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3258b 20 20 20 63 61 73 65 20 32 31 38 3a 20 2f 2a 20     case 218: /* 
3258c 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70  expr ::= NOT exp
3258d 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
3258e 32 31 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  219: /* expr ::=
3258f 20 42 49 54 4e 4f 54 20 65 78 70 72 20 2a 2f 20   BITNOT expr */ 
32590 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32591 65 6e 6f 3d 3d 32 31 39 29 3b 0a 7b 73 70 61 6e  eno==219);.{span
32592 55 6e 61 72 79 50 72 65 66 69 78 28 26 79 79 67  UnaryPrefix(&yyg
32593 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70  otominor.yy346,p
32594 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e  Parse,yymsp[-1].
32595 6d 61 6a 6f 72 2c 26 79 79 6d 73 70 5b 30 5d 2e  major,&yymsp[0].
32596 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d  minor.yy346,&yym
32597 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
32598 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
32599 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
3259a 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4d  0: /* expr ::= M
3259b 49 4e 55 53 20 65 78 70 72 20 2a 2f 0a 7b 73 70  INUS expr */.{sp
3259c 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28 26 79  anUnaryPrefix(&y
3259d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3259e 2c 70 50 61 72 73 65 2c 54 4b 5f 55 4d 49 4e 55  ,pParse,TK_UMINU
3259f 53 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  S,&yymsp[0].mino
325a0 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d  r.yy346,&yymsp[-
325a1 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  1].minor.yy0);}.
325a2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
325a3 20 20 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f       case 221: /
325a4 2a 20 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20  * expr ::= PLUS 
325a5 65 78 70 72 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61  expr */.{spanUna
325a6 72 79 50 72 65 66 69 78 28 26 79 79 67 6f 74 6f  ryPrefix(&yygoto
325a7 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72  minor.yy346,pPar
325a8 73 65 2c 54 4b 5f 55 50 4c 55 53 2c 26 79 79 6d  se,TK_UPLUS,&yym
325a9 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
325aa 36 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  6,&yymsp[-1].min
325ab 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
325ac 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
325ad 61 73 65 20 32 32 34 3a 20 2f 2a 20 65 78 70 72  ase 224: /* expr
325ae 20 3a 3a 3d 20 65 78 70 72 20 62 65 74 77 65 65   ::= expr betwee
325af 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20 65 78  n_op expr AND ex
325b0 70 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c 69  pr */.{.  ExprLi
325b1 73 74 20 2a 70 4c 69 73 74 20 3d 20 73 71 6c 69  st *pList = sqli
325b2 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
325b3 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73  d(pParse,0, yyms
325b4 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
325b5 36 2e 70 45 78 70 72 29 3b 0a 20 20 70 4c 69 73  6.pExpr);.  pLis
325b6 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
325b7 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
325b8 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30 5d  ,pList, yymsp[0]
325b9 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
325ba 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  pr);.  yygotomin
325bb 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
325bc 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
325bd 61 72 73 65 2c 20 54 4b 5f 42 45 54 57 45 45 4e  arse, TK_BETWEEN
325be 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
325bf 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30  r.yy346.pExpr, 0
325c0 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  , 0);.  if( yygo
325c1 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
325c2 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74  xpr ){.    yygot
325c3 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
325c4 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c  pr->x.pList = pL
325c5 69 73 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ist;.  }else{.  
325c6 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
325c7 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
325c8 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 7d 20  db, pList);.  } 
325c9 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d  .  if( yymsp[-3]
325ca 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 29 20 79  .minor.yy328 ) y
325cb 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
325cc 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
325cd 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
325ce 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  _NOT, yygotomino
325cf 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30  r.yy346.pExpr, 0
325d0 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  , 0);.  yygotomi
325d1 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
325d2 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e   = yymsp[-4].min
325d3 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 3b  or.yy346.zStart;
325d4 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
325d5 79 33 34 36 2e 7a 45 6e 64 20 3d 20 79 79 6d 73  y346.zEnd = yyms
325d6 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
325d7 2e 7a 45 6e 64 3b 0a 7d 0a 20 20 20 20 20 20 20  .zEnd;.}.       
325d8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
325d9 73 65 20 32 32 37 3a 20 2f 2a 20 65 78 70 72 20  se 227: /* expr 
325da 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c  ::= expr in_op L
325db 50 20 65 78 70 72 6c 69 73 74 20 52 50 20 2a 2f  P exprlist RP */
325dc 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .{.    yygotomin
325dd 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
325de 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
325df 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d  arse, TK_IN, yym
325e0 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
325e1 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  46.pExpr, 0, 0);
325e2 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  .    if( yygotom
325e3 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
325e4 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f   ){.      yygoto
325e5 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
325e6 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79 79 6d  r->x.pList = yym
325e7 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
325e8 34 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  4;.      sqlite3
325e9 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
325ea 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
325eb 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
325ec 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
325ed 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
325ee 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
325ef 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  b, yymsp[-1].min
325f0 6f 72 2e 79 79 31 34 29 3b 0a 20 20 20 20 7d 0a  or.yy14);.    }.
325f1 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33      if( yymsp[-3
325f2 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 29 20  ].minor.yy328 ) 
325f3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
325f4 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
325f5 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
325f6 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e  K_NOT, yygotomin
325f7 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
325f8 30 2c 20 30 29 3b 0a 20 20 20 20 79 79 67 6f 74  0, 0);.    yygot
325f9 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  ominor.yy346.zSt
325fa 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e  art = yymsp[-4].
325fb 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
325fc 72 74 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  rt;.    yygotomi
325fd 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d  nor.yy346.zEnd =
325fe 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
325ff 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e  .yy0.z[yymsp[0].
32600 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20  minor.yy0.n];.  
32601 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32602 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 38 3a  .      case 228:
32603 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20   /* expr ::= LP 
32604 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20  select RP */.{. 
32605 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
32606 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
32607 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32608 2c 20 54 4b 5f 53 45 4c 45 43 54 2c 20 30 2c 20  , TK_SELECT, 0, 
32609 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79  0, 0);.    if( y
3260a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3260b 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  .pExpr ){.      
3260c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3260d 36 2e 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  6.pExpr->x.pSele
3260e 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ct = yymsp[-1].m
3260f 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20 20 20 20  inor.yy3;.      
32610 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28  ExprSetProperty(
32611 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32612 36 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  6.pExpr, EP_xIsS
32613 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71  elect);.      sq
32614 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
32615 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  ht(pParse, yygot
32616 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32617 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  pr);.    }else{.
32618 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
32619 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  ectDelete(pParse
3261a 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  ->db, yymsp[-1].
3261b 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 20 20  minor.yy3);.    
3261c 7d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  }.    yygotomino
3261d 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d  r.yy346.zStart =
3261e 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
3261f 2e 79 79 30 2e 7a 3b 0a 20 20 20 20 79 79 67 6f  .yy0.z;.    yygo
32620 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45  tominor.yy346.zE
32621 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  nd = &yymsp[0].m
32622 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
32623 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
32624 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ;.  }.        br
32625 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32626 32 32 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  229: /* expr ::=
32627 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73   expr in_op LP s
32628 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  elect RP */.{.  
32629 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3262a 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
3262b 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3262c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34   TK_IN, yymsp[-4
3262d 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
3262e 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
3262f 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
32630 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20  yy346.pExpr ){. 
32631 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
32632 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e  .yy346.pExpr->x.
32633 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b  pSelect = yymsp[
32634 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20  -1].minor.yy3;. 
32635 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70       ExprSetProp
32636 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  erty(yygotominor
32637 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 45 50  .yy346.pExpr, EP
32638 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20 20  _xIsSelect);.   
32639 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65     sqlite3ExprSe
3263a 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
3263b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3263c 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 65  6.pExpr);.    }e
3263d 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
3263e 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
3263f 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70  Parse->db, yymsp
32640 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b  [-1].minor.yy3);
32641 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79  .    }.    if( y
32642 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32643 79 33 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e  y328 ) yygotomin
32644 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
32645 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
32646 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79  arse, TK_NOT, yy
32647 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32648 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
32649 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3264a 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d  346.zStart = yym
3264b 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
3264c 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 20 20 79  46.zStart;.    y
3264d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3264e 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30  .zEnd = &yymsp[0
3264f 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
32650 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32651 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20  .n];.  }.       
32652 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32653 73 65 20 32 33 30 3a 20 2f 2a 20 65 78 70 72 20  se 230: /* expr 
32654 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 6e  ::= expr in_op n
32655 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20  m dbnm */.{.    
32656 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20  SrcList *pSrc = 
32657 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
32658 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  pend(pParse->db,
32659 20 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69   0,&yymsp[-1].mi
3265a 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
3265b 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
3265c 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3265d 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
3265e 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3265f 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 33   TK_IN, yymsp[-3
32660 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
32661 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
32662 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
32663 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20  yy346.pExpr ){. 
32664 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
32665 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e  .yy346.pExpr->x.
32666 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
32667 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73  3SelectNew(pPars
32668 65 2c 20 30 2c 70 53 72 63 2c 30 2c 30 2c 30 2c  e, 0,pSrc,0,0,0,
32669 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  0,0,0,0);.      
3266a 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28  ExprSetProperty(
3266b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3266c 36 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  6.pExpr, EP_xIsS
3266d 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71  elect);.      sq
3266e 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
3266f 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  ht(pParse, yygot
32670 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32671 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  pr);.    }else{.
32672 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
32673 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
32674 65 2d 3e 64 62 2c 20 70 53 72 63 29 3b 0a 20 20  e->db, pSrc);.  
32675 20 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73    }.    if( yyms
32676 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-2].minor.yy32
32677 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  8 ) yygotominor.
32678 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
32679 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
3267a 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74  e, TK_NOT, yygot
3267b 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
3267c 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 79  pr, 0, 0);.    y
3267d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3267e 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
3267f 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -3].minor.yy346.
32680 7a 53 74 61 72 74 3b 0a 20 20 20 20 79 79 67 6f  zStart;.    yygo
32681 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45  tominor.yy346.zE
32682 6e 64 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  nd = yymsp[0].mi
32683 6e 6f 72 2e 79 79 30 2e 7a 20 3f 20 26 79 79 6d  nor.yy0.z ? &yym
32684 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
32685 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
32686 2e 79 79 30 2e 6e 5d 20 3a 20 26 79 79 6d 73 70  .yy0.n] : &yymsp
32687 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-1].minor.yy0.z
32688 5b 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  [yymsp[-1].minor
32689 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20  .yy0.n];.  }.   
3268a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3268b 20 20 63 61 73 65 20 32 33 31 3a 20 2f 2a 20 65    case 231: /* e
3268c 78 70 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c  xpr ::= EXISTS L
3268d 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b  P select RP */.{
3268e 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 79  .    Expr *p = y
3268f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32690 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
32691 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
32692 5f 45 58 49 53 54 53 2c 20 30 2c 20 30 2c 20 30  _EXISTS, 0, 0, 0
32693 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  );.    if( p ){.
32694 20 20 20 20 20 20 70 2d 3e 78 2e 70 53 65 6c 65        p->x.pSele
32695 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ct = yymsp[-1].m
32696 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20 20 20 20  inor.yy3;.      
32697 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28  ExprSetProperty(
32698 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  p, EP_xIsSelect)
32699 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
3269a 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61  xprSetHeight(pPa
3269b 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 7d 65 6c  rse, p);.    }el
3269c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
3269d 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50  3SelectDelete(pP
3269e 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b  arse->db, yymsp[
3269f 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a  -1].minor.yy3);.
326a0 20 20 20 20 7d 0a 20 20 20 20 79 79 67 6f 74 6f      }.    yygoto
326a1 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
326a2 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  rt = yymsp[-3].m
326a3 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 20 20  inor.yy0.z;.    
326a4 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
326a5 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b  6.zEnd = &yymsp[
326a6 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79  0].minor.yy0.z[y
326a7 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
326a8 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20  0.n];.  }.      
326a9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
326aa 61 73 65 20 32 33 32 3a 20 2f 2a 20 65 78 70 72  ase 232: /* expr
326ab 20 3a 3a 3d 20 43 41 53 45 20 63 61 73 65 5f 6f   ::= CASE case_o
326ac 70 65 72 61 6e 64 20 63 61 73 65 5f 65 78 70 72  perand case_expr
326ad 6c 69 73 74 20 63 61 73 65 5f 65 6c 73 65 20 45  list case_else E
326ae 4e 44 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  ND */.{.  yygoto
326af 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
326b0 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
326b1 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 41 53 45  (pParse, TK_CASE
326b2 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  , yymsp[-3].mino
326b3 72 2e 79 79 31 33 32 2c 20 79 79 6d 73 70 5b 2d  r.yy132, yymsp[-
326b4 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 20  1].minor.yy132, 
326b5 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  0);.  if( yygoto
326b6 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
326b7 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  r ){.    yygotom
326b8 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
326b9 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79 79 6d 73  ->x.pList = yyms
326ba 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-2].minor.yy14
326bb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
326bc 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73  rSetHeight(pPars
326bd 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  e, yygotominor.y
326be 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 7d  y346.pExpr);.  }
326bf 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
326c0 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
326c1 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
326c2 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-2].minor.yy14
326c3 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
326c4 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
326c5 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  t = yymsp[-4].mi
326c6 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67  nor.yy0.z;.  yyg
326c7 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
326c8 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e  End = &yymsp[0].
326c9 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73  minor.yy0.z[yyms
326ca 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
326cb 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ];.}.        bre
326cc 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
326cd 33 33 3a 20 2f 2a 20 63 61 73 65 5f 65 78 70 72  33: /* case_expr
326ce 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f 65 78  list ::= case_ex
326cf 70 72 6c 69 73 74 20 57 48 45 4e 20 65 78 70 72  prlist WHEN expr
326d0 20 54 48 45 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a   THEN expr */.{.
326d1 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
326d2 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  14 = sqlite3Expr
326d3 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
326d4 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  e,yymsp[-4].mino
326d5 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 2d 32  r.yy14, yymsp[-2
326d6 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
326d7 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  xpr);.  yygotomi
326d8 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
326d9 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
326da 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69  (pParse,yygotomi
326db 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b  nor.yy14, yymsp[
326dc 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
326dd 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20 20  Expr);.}.       
326de 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
326df 73 65 20 32 33 34 3a 20 2f 2a 20 63 61 73 65 5f  se 234: /* case_
326e0 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57 48 45  exprlist ::= WHE
326e1 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72  N expr THEN expr
326e2 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
326e3 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
326e4 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
326e5 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70  (pParse,0, yymsp
326e6 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-2].minor.yy346
326e7 2e 70 45 78 70 72 29 3b 0a 20 20 79 79 67 6f 74  .pExpr);.  yygot
326e8 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
326e9 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
326ea 65 6e 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74  end(pParse,yygot
326eb 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d  ominor.yy14, yym
326ec 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
326ed 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20  6.pExpr);.}.    
326ee 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
326ef 20 63 61 73 65 20 32 34 33 3a 20 2f 2a 20 63 6d   case 243: /* cm
326f0 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 75  d ::= createkw u
326f1 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45 58 20  niqueflag INDEX 
326f2 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64  ifnotexists nm d
326f3 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78  bnm ON nm LP idx
326f4 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73  list RP */.{.  s
326f5 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65  qlite3CreateInde
326f6 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70  x(pParse, &yymsp
326f7 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-6].minor.yy0, 
326f8 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72  &yymsp[-5].minor
326f9 2e 79 79 30 2c 20 0a 20 20 20 20 20 20 20 20 20  .yy0, .         
326fa 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
326fb 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
326fc 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79  (pParse->db,0,&y
326fd 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
326fe 79 30 2c 30 29 2c 20 79 79 6d 73 70 5b 2d 31 5d  y0,0), yymsp[-1]
326ff 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d  .minor.yy14, yym
32700 73 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-9].minor.yy3
32701 32 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  28,.            
32702 20 20 20 20 20 20 20 20 20 20 26 79 79 6d 73 70            &yymsp
32703 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  [-10].minor.yy0,
32704 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32705 2e 79 79 30 2c 20 53 51 4c 49 54 45 5f 53 4f 5f  .yy0, SQLITE_SO_
32706 41 53 43 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d  ASC, yymsp[-7].m
32707 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20  inor.yy328);.}. 
32708 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32709 20 20 20 20 63 61 73 65 20 32 34 34 3a 20 2f 2a      case 244: /*
3270a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20   uniqueflag ::= 
3270b 55 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20 20 20  UNIQUE */.      
3270c 63 61 73 65 20 32 39 38 3a 20 2f 2a 20 72 61 69  case 298: /* rai
3270d 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54  setype ::= ABORT
3270e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3270f 79 72 75 6c 65 6e 6f 3d 3d 32 39 38 29 3b 0a 7b  yruleno==298);.{
32710 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
32711 38 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d 0a 20  8 = OE_Abort;}. 
32712 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32713 20 20 20 20 63 61 73 65 20 32 34 35 3a 20 2f 2a      case 245: /*
32714 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20   uniqueflag ::= 
32715 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
32716 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65 3b  yy328 = OE_None;
32717 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32718 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 38 3a  .      case 248:
32719 20 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20   /* idxlist ::= 
3271a 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d  idxlist COMMA nm
3271b 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64   collate sortord
3271c 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a  er */.{.  Expr *
3271d 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d  p = 0;.  if( yym
3271e 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
3271f 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20  .n>0 ){.    p = 
32720 73 71 6c 69 74 65 33 45 78 70 72 28 70 50 61 72  sqlite3Expr(pPar
32721 73 65 2d 3e 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d  se->db, TK_COLUM
32722 4e 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  N, 0);.    sqlit
32723 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 70 50  e3ExprSetColl(pP
32724 61 72 73 65 2c 20 70 2c 20 26 79 79 6d 73 70 5b  arse, p, &yymsp[
32725 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -1].minor.yy0);.
32726 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f    }.  yygotomino
32727 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
32728 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
32729 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e  Parse,yymsp[-4].
3272a 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 70 29 3b 0a  minor.yy14, p);.
3272b 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
3272c 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  tSetName(pParse,
3272d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
3272e 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
3272f 72 2e 79 79 30 2c 31 29 3b 0a 20 20 73 71 6c 69  r.yy0,1);.  sqli
32730 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
32731 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20 79  Length(pParse, y
32732 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c  ygotominor.yy14,
32733 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69 66 28   "index");.  if(
32734 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
32735 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  4 ) yygotominor.
32736 79 79 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69  yy14->a[yygotomi
32737 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d  nor.yy14->nExpr-
32738 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28  1].sortOrder = (
32739 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  u8)yymsp[0].mino
3273a 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20  r.yy328;.}.     
3273b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3273c 63 61 73 65 20 32 34 39 3a 20 2f 2a 20 69 64 78  case 249: /* idx
3273d 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c  list ::= nm coll
3273e 61 74 65 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f  ate sortorder */
3273f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 30  .{.  Expr *p = 0
32740 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 31  ;.  if( yymsp[-1
32741 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20  ].minor.yy0.n>0 
32742 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  ){.    p = sqlit
32743 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
32744 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c  TK_COLUMN, 0, 0,
32745 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
32746 45 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72  ExprSetColl(pPar
32747 73 65 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 31  se, p, &yymsp[-1
32748 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
32749 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
3274a 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
3274b 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
3274c 72 73 65 2c 30 2c 20 70 29 3b 0a 20 20 73 71 6c  rse,0, p);.  sql
3274d 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e  ite3ExprListSetN
3274e 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f  ame(pParse, yygo
3274f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79  tominor.yy14, &y
32750 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32751 79 30 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65  y0, 1);.  sqlite
32752 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65  3ExprListCheckLe
32753 6e 67 74 68 28 70 50 61 72 73 65 2c 20 79 79 67  ngth(pParse, yyg
32754 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 22  otominor.yy14, "
32755 69 6e 64 65 78 22 29 3b 0a 20 20 69 66 28 20 79  index");.  if( y
32756 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
32757 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
32758 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f  14->a[yygotomino
32759 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d 31 5d  r.yy14->nExpr-1]
3275a 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  .sortOrder = (u8
3275b 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  )yymsp[0].minor.
3275c 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20 20 20  yy328;.}.       
3275d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3275e 73 65 20 32 35 30 3a 20 2f 2a 20 63 6f 6c 6c 61  se 250: /* colla
3275f 74 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  te ::= */.{yygot
32760 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 30  ominor.yy0.z = 0
32761 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
32762 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  0.n = 0;}.      
32763 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32764 61 73 65 20 32 35 32 3a 20 2f 2a 20 63 6d 64 20  ase 252: /* cmd 
32765 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20 69  ::= DROP INDEX i
32766 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65  fexists fullname
32767 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 72 6f 70   */.{sqlite3Drop
32768 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 79 79  Index(pParse, yy
32769 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36  msp[0].minor.yy6
3276a 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  5, yymsp[-1].min
3276b 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20  or.yy328);}.    
3276c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3276d 20 63 61 73 65 20 32 35 33 3a 20 2f 2a 20 63 6d   case 253: /* cm
3276e 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 2a 2f 0a  d ::= VACUUM */.
3276f 20 20 20 20 20 20 63 61 73 65 20 32 35 34 3a 20        case 254: 
32770 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55  /* cmd ::= VACUU
32771 4d 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61  M nm */ yytestca
32772 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35 34  se(yyruleno==254
32773 29 3b 0a 7b 73 71 6c 69 74 65 33 56 61 63 75 75  );.{sqlite3Vacuu
32774 6d 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20  m(pParse);}.    
32775 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32776 20 63 61 73 65 20 32 35 35 3a 20 2f 2a 20 63 6d   case 255: /* cm
32777 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
32778 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  dbnm */.{sqlite3
32779 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79  Pragma(pParse,&y
3277a 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3277b 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
3277c 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 7d 0a 20 20  or.yy0,0,0);}.  
3277d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3277e 20 20 20 63 61 73 65 20 32 35 36 3a 20 2f 2a 20     case 256: /* 
3277f 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
32780 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 20  m dbnm EQ nmnum 
32781 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d  */.{sqlite3Pragm
32782 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  a(pParse,&yymsp[
32783 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
32784 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32785 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
32786 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20 20 20  or.yy0,0);}.    
32787 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32788 20 63 61 73 65 20 32 35 37 3a 20 2f 2a 20 63 6d   case 257: /* cm
32789 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
3278a 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52 50  dbnm LP nmnum RP
3278b 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67   */.{sqlite3Prag
3278c 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  ma(pParse,&yymsp
3278d 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-4].minor.yy0,&
3278e 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
3278f 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  yy0,&yymsp[-1].m
32790 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20  inor.yy0,0);}.  
32791 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32792 20 20 20 63 61 73 65 20 32 35 38 3a 20 2f 2a 20     case 258: /* 
32793 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
32794 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f  m dbnm EQ minus_
32795 6e 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50  num */.{sqlite3P
32796 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79  ragma(pParse,&yy
32797 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32798 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0,&yymsp[-2].min
32799 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
3279a 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 7d 0a  .minor.yy0,1);}.
3279b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3279c 20 20 20 20 20 63 61 73 65 20 32 35 39 3a 20 2f       case 259: /
3279d 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41  * cmd ::= PRAGMA
3279e 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6d 69 6e 75   nm dbnm LP minu
3279f 73 5f 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71 6c  s_num RP */.{sql
327a0 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73  ite3Pragma(pPars
327a1 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  e,&yymsp[-4].min
327a2 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33  or.yy0,&yymsp[-3
327a3 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
327a4 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
327a5 2c 31 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,1);}.        br
327a6 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
327a7 32 37 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  270: /* cmd ::= 
327a8 63 72 65 61 74 65 6b 77 20 74 72 69 67 67 65 72  createkw trigger
327a9 5f 64 65 63 6c 20 42 45 47 49 4e 20 74 72 69 67  _decl BEGIN trig
327aa 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e 44  ger_cmd_list END
327ab 20 2a 2f 0a 7b 0a 20 20 54 6f 6b 65 6e 20 61 6c   */.{.  Token al
327ac 6c 3b 0a 20 20 61 6c 6c 2e 7a 20 3d 20 79 79 6d  l;.  all.z = yym
327ad 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
327ae 2e 7a 3b 0a 20 20 61 6c 6c 2e 6e 20 3d 20 28 69  .z;.  all.n = (i
327af 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  nt)(yymsp[0].min
327b0 6f 72 2e 79 79 30 2e 7a 20 2d 20 79 79 6d 73 70  or.yy0.z - yymsp
327b1 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-3].minor.yy0.z
327b2 29 20 2b 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  ) + yymsp[0].min
327b3 6f 72 2e 79 79 30 2e 6e 3b 0a 20 20 73 71 6c 69  or.yy0.n;.  sqli
327b4 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72  te3FinishTrigger
327b5 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
327b6 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2c 20  1].minor.yy473, 
327b7 26 61 6c 6c 29 3b 0a 7d 0a 20 20 20 20 20 20 20  &all);.}.       
327b8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
327b9 73 65 20 32 37 31 3a 20 2f 2a 20 74 72 69 67 67  se 271: /* trigg
327ba 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74 65 6d 70  er_decl ::= temp
327bb 20 54 52 49 47 47 45 52 20 69 66 6e 6f 74 65 78   TRIGGER ifnotex
327bc 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 74 72 69  ists nm dbnm tri
327bd 67 67 65 72 5f 74 69 6d 65 20 74 72 69 67 67 65  gger_time trigge
327be 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75 6c 6c 6e  r_event ON fulln
327bf 61 6d 65 20 66 6f 72 65 61 63 68 5f 63 6c 61 75  ame foreach_clau
327c0 73 65 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 2a  se when_clause *
327c1 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 42 65 67  /.{.  sqlite3Beg
327c2 69 6e 54 72 69 67 67 65 72 28 70 50 61 72 73 65  inTrigger(pParse
327c3 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  , &yymsp[-7].min
327c4 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d  or.yy0, &yymsp[-
327c5 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79  6].minor.yy0, yy
327c6 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
327c7 33 32 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  328, yymsp[-4].m
327c8 69 6e 6f 72 2e 79 79 33 37 38 2e 61 2c 20 79 79  inor.yy378.a, yy
327c9 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
327ca 33 37 38 2e 62 2c 20 79 79 6d 73 70 5b 2d 32 5d  378.b, yymsp[-2]
327cb 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 79 79 6d  .minor.yy65, yym
327cc 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33  sp[0].minor.yy13
327cd 32 2c 20 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69  2, yymsp[-10].mi
327ce 6e 6f 72 2e 79 79 33 32 38 2c 20 79 79 6d 73 70  nor.yy328, yymsp
327cf 5b 2d 38 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-8].minor.yy328
327d0 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
327d1 2e 79 79 30 20 3d 20 28 79 79 6d 73 70 5b 2d 36  .yy0 = (yymsp[-6
327d2 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d 30  ].minor.yy0.n==0
327d3 3f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72  ?yymsp[-7].minor
327d4 2e 79 79 30 3a 79 79 6d 73 70 5b 2d 36 5d 2e 6d  .yy0:yymsp[-6].m
327d5 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
327d6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
327d7 20 20 63 61 73 65 20 32 37 32 3a 20 2f 2a 20 74    case 272: /* t
327d8 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
327d9 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20  BEFORE */.      
327da 63 61 73 65 20 32 37 35 3a 20 2f 2a 20 74 72 69  case 275: /* tri
327db 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 2a 2f  gger_time ::= */
327dc 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
327dd 6c 65 6e 6f 3d 3d 32 37 35 29 3b 0a 7b 20 79 79  leno==275);.{ yy
327de 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
327df 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 20 7d 0a 20  = TK_BEFORE; }. 
327e0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
327e1 20 20 20 20 63 61 73 65 20 32 37 33 3a 20 2f 2a      case 273: /*
327e2 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a   trigger_time ::
327e3 3d 20 41 46 54 45 52 20 2a 2f 0a 7b 20 79 79 67  = AFTER */.{ yyg
327e4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d  otominor.yy328 =
327e5 20 54 4b 5f 41 46 54 45 52 3b 20 20 7d 0a 20 20   TK_AFTER;  }.  
327e6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
327e7 20 20 20 63 61 73 65 20 32 37 34 3a 20 2f 2a 20     case 274: /* 
327e8 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
327e9 20 49 4e 53 54 45 41 44 20 4f 46 20 2a 2f 0a 7b   INSTEAD OF */.{
327ea 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
327eb 32 38 20 3d 20 54 4b 5f 49 4e 53 54 45 41 44 3b  28 = TK_INSTEAD;
327ec 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
327ed 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 36 3a  .      case 276:
327ee 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e   /* trigger_even
327ef 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c 49 4e 53  t ::= DELETE|INS
327f0 45 52 54 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ERT */.      cas
327f1 65 20 32 37 37 3a 20 2f 2a 20 74 72 69 67 67 65  e 277: /* trigge
327f2 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41  r_event ::= UPDA
327f3 54 45 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  TE */ yytestcase
327f4 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 37 37 29 3b  (yyruleno==277);
327f5 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
327f6 33 37 38 2e 61 20 3d 20 79 79 6d 73 70 5b 30 5d  378.a = yymsp[0]
327f7 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f 74 6f 6d 69  .major; yygotomi
327f8 6e 6f 72 2e 79 79 33 37 38 2e 62 20 3d 20 30 3b  nor.yy378.b = 0;
327f9 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
327fa 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 38 3a  .      case 278:
327fb 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e   /* trigger_even
327fc 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f 46 20  t ::= UPDATE OF 
327fd 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a 7b 79  inscollist */.{y
327fe 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 38  ygotominor.yy378
327ff 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b 20  .a = TK_UPDATE; 
32800 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37  yygotominor.yy37
32801 38 2e 62 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  8.b = yymsp[0].m
32802 69 6e 6f 72 2e 79 79 34 30 38 3b 7d 0a 20 20 20  inor.yy408;}.   
32803 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32804 20 20 63 61 73 65 20 32 38 31 3a 20 2f 2a 20 77    case 281: /* w
32805 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a  hen_clause ::= *
32806 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 33  /.      case 303
32807 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d  : /* key_opt ::=
32808 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32809 79 72 75 6c 65 6e 6f 3d 3d 33 30 33 29 3b 0a 7b  yruleno==303);.{
3280a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3280b 33 32 20 3d 20 30 3b 20 7d 0a 20 20 20 20 20 20  32 = 0; }.      
3280c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3280d 61 73 65 20 32 38 32 3a 20 2f 2a 20 77 68 65 6e  ase 282: /* when
3280e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45 4e  _clause ::= WHEN
3280f 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
32810 61 73 65 20 33 30 34 3a 20 2f 2a 20 6b 65 79 5f  ase 304: /* key_
32811 6f 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72  opt ::= KEY expr
32812 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32813 79 72 75 6c 65 6e 6f 3d 3d 33 30 34 29 3b 0a 7b  yruleno==304);.{
32814 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
32815 33 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  32 = yymsp[0].mi
32816 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b  nor.yy346.pExpr;
32817 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
32818 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 33  ;.      case 283
32819 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
3281a 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65  _list ::= trigge
3281b 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72 69 67 67  r_cmd_list trigg
3281c 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b  er_cmd SEMI */.{
3281d 0a 20 20 61 73 73 65 72 74 28 20 79 79 6d 73 70  .  assert( yymsp
3281e 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33  [-2].minor.yy473
3281f 21 3d 30 20 29 3b 0a 20 20 79 79 6d 73 70 5b 2d  !=0 );.  yymsp[-
32820 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2d 3e  2].minor.yy473->
32821 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 79  pLast->pNext = y
32822 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32823 79 34 37 33 3b 0a 20 20 79 79 6d 73 70 5b 2d 32  y473;.  yymsp[-2
32824 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2d 3e 70  ].minor.yy473->p
32825 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  Last = yymsp[-1]
32826 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 20 20  .minor.yy473;.  
32827 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37  yygotominor.yy47
32828 33 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  3 = yymsp[-2].mi
32829 6e 6f 72 2e 79 79 34 37 33 3b 0a 7d 0a 20 20 20  nor.yy473;.}.   
3282a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3282b 20 20 63 61 73 65 20 32 38 34 3a 20 2f 2a 20 74    case 284: /* t
3282c 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
3282d 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 20  ::= trigger_cmd 
3282e 53 45 4d 49 20 2a 2f 0a 7b 20 0a 20 20 61 73 73  SEMI */.{ .  ass
3282f 65 72 74 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ert( yymsp[-1].m
32830 69 6e 6f 72 2e 79 79 34 37 33 21 3d 30 20 29 3b  inor.yy473!=0 );
32831 0a 20 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  .  yymsp[-1].min
32832 6f 72 2e 79 79 34 37 33 2d 3e 70 4c 61 73 74 20  or.yy473->pLast 
32833 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
32834 72 2e 79 79 34 37 33 3b 0a 20 20 79 79 67 6f 74  r.yy473;.  yygot
32835 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 79  ominor.yy473 = y
32836 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32837 79 34 37 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y473;.}.        
32838 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32839 65 20 32 38 36 3a 20 2f 2a 20 74 72 6e 6d 20 3a  e 286: /* trnm :
3283a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a  := nm DOT nm */.
3283b 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
3283c 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  yy0 = yymsp[0].m
3283d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 73 71 6c 69  inor.yy0;.  sqli
3283e 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
3283f 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 71 75  se, .        "qu
32840 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61  alified table na
32841 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  mes are not allo
32842 77 65 64 20 6f 6e 20 49 4e 53 45 52 54 2c 20 55  wed on INSERT, U
32843 50 44 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54  PDATE, and DELET
32844 45 20 22 0a 20 20 20 20 20 20 20 20 22 73 74 61  E ".        "sta
32845 74 65 6d 65 6e 74 73 20 77 69 74 68 69 6e 20 74  tements within t
32846 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a 20 20 20  riggers");.}.   
32847 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32848 20 20 63 61 73 65 20 32 38 38 3a 20 2f 2a 20 74    case 288: /* t
32849 72 69 64 78 62 79 20 3a 3a 3d 20 49 4e 44 45 58  ridxby ::= INDEX
3284a 45 44 20 42 59 20 6e 6d 20 2a 2f 0a 7b 0a 20 20  ED BY nm */.{.  
3284b 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
3284c 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
3284d 22 74 68 65 20 49 4e 44 45 58 45 44 20 42 59 20  "the INDEXED BY 
3284e 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61 6c  clause is not al
3284f 6c 6f 77 65 64 20 6f 6e 20 55 50 44 41 54 45 20  lowed on UPDATE 
32850 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d  or DELETE statem
32851 65 6e 74 73 20 22 0a 20 20 20 20 20 20 20 20 22  ents ".        "
32852 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 22  within triggers"
32853 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
32854 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32855 38 39 3a 20 2f 2a 20 74 72 69 64 78 62 79 20 3a  89: /* tridxby :
32856 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 2a  := NOT INDEXED *
32857 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72  /.{.  sqlite3Err
32858 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20  orMsg(pParse,.  
32859 20 20 20 20 20 20 22 74 68 65 20 4e 4f 54 20 49        "the NOT I
3285a 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 69 73  NDEXED clause is
3285b 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20   not allowed on 
3285c 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
3285d 20 73 74 61 74 65 6d 65 6e 74 73 20 22 0a 20 20   statements ".  
3285e 20 20 20 20 20 20 22 77 69 74 68 69 6e 20 74 72        "within tr
3285f 69 67 67 65 72 73 22 29 3b 0a 7d 0a 20 20 20 20  iggers");.}.    
32860 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32861 20 63 61 73 65 20 32 39 30 3a 20 2f 2a 20 74 72   case 290: /* tr
32862 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 55 50  igger_cmd ::= UP
32863 44 41 54 45 20 6f 72 63 6f 6e 66 20 74 72 6e 6d  DATE orconf trnm
32864 20 74 72 69 64 78 62 79 20 53 45 54 20 73 65 74   tridxby SET set
32865 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74 20 2a  list where_opt *
32866 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
32867 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54  yy473 = sqlite3T
32868 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70  riggerUpdateStep
32869 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79  (pParse->db, &yy
3286a 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
3286b 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0, yymsp[-1].min
3286c 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 30  or.yy14, yymsp[0
3286d 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 20 79  ].minor.yy132, y
3286e 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
3286f 79 31 38 36 29 3b 20 7d 0a 20 20 20 20 20 20 20  y186); }.       
32870 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32871 73 65 20 32 39 31 3a 20 2f 2a 20 74 72 69 67 67  se 291: /* trigg
32872 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72  er_cmd ::= inser
32873 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20  t_cmd INTO trnm 
32874 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56  inscollist_opt V
32875 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73  ALUES LP itemlis
32876 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  t RP */.{yygotom
32877 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c  inor.yy473 = sql
32878 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72  ite3TriggerInser
32879 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62  tStep(pParse->db
3287a 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  , &yymsp[-5].min
3287b 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 34  or.yy0, yymsp[-4
3287c 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20 79  ].minor.yy408, y
3287d 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3287e 79 31 34 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 37  y14, 0, yymsp[-7
3287f 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d  ].minor.yy186);}
32880 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32881 20 20 20 20 20 20 63 61 73 65 20 32 39 32 3a 20        case 292: 
32882 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  /* trigger_cmd :
32883 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
32884 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69  TO trnm inscolli
32885 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a 2f  st_opt select */
32886 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
32887 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69  473 = sqlite3Tri
32888 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 70  ggerInsertStep(p
32889 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73  Parse->db, &yyms
3288a 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
3288b 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3288c 2e 79 79 34 30 38 2c 20 30 2c 20 79 79 6d 73 70  .yy408, 0, yymsp
3288d 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79  [0].minor.yy3, y
3288e 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3288f 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  y186);}.        
32890 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32891 65 20 32 39 33 3a 20 2f 2a 20 74 72 69 67 67 65  e 293: /* trigge
32892 72 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45  r_cmd ::= DELETE
32893 20 46 52 4f 4d 20 74 72 6e 6d 20 74 72 69 64 78   FROM trnm tridx
32894 62 79 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a  by where_opt */.
32895 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
32896 37 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  73 = sqlite3Trig
32897 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 70 50  gerDeleteStep(pP
32898 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70  arse->db, &yymsp
32899 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-2].minor.yy0, 
3289a 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3289b 79 31 33 32 29 3b 7d 0a 20 20 20 20 20 20 20 20  y132);}.        
3289c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3289d 65 20 32 39 34 3a 20 2f 2a 20 74 72 69 67 67 65  e 294: /* trigge
3289e 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74  r_cmd ::= select
3289f 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
328a0 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33  .yy473 = sqlite3
328a1 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74 65  TriggerSelectSte
328a2 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79  p(pParse->db, yy
328a3 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
328a4 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
328a5 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
328a6 39 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  95: /* expr ::= 
328a7 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52 45 20  RAISE LP IGNORE 
328a8 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  RP */.{.  yygoto
328a9 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
328aa 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
328ab 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41 49 53  (pParse, TK_RAIS
328ac 45 2c 20 30 2c 20 30 2c 20 30 29 3b 20 0a 20 20  E, 0, 0, 0); .  
328ad 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
328ae 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20  yy346.pExpr ){. 
328af 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
328b0 79 33 34 36 2e 70 45 78 70 72 2d 3e 61 66 66 69  y346.pExpr->affi
328b1 6e 69 74 79 20 3d 20 4f 45 5f 49 67 6e 6f 72 65  nity = OE_Ignore
328b2 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69  ;.  }.  yygotomi
328b3 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
328b4 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   = yymsp[-3].min
328b5 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f  or.yy0.z;.  yygo
328b6 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45  tominor.yy346.zE
328b7 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  nd = &yymsp[0].m
328b8 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
328b9 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
328ba 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
328bb 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39  k;.      case 29
328bc 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 52  6: /* expr ::= R
328bd 41 49 53 45 20 4c 50 20 72 61 69 73 65 74 79 70  AISE LP raisetyp
328be 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 20 2a 2f  e COMMA nm RP */
328bf 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
328c0 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
328c1 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
328c2 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c  se, TK_RAISE, 0,
328c3 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d   0, &yymsp[-1].m
328c4 69 6e 6f 72 2e 79 79 30 29 3b 20 0a 20 20 69 66  inor.yy0); .  if
328c5 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
328c6 33 34 36 2e 70 45 78 70 72 20 29 20 7b 0a 20 20  346.pExpr ) {.  
328c7 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
328c8 33 34 36 2e 70 45 78 70 72 2d 3e 61 66 66 69 6e  346.pExpr->affin
328c9 69 74 79 20 3d 20 28 63 68 61 72 29 79 79 6d 73  ity = (char)yyms
328ca 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-3].minor.yy32
328cb 38 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  8;.  }.  yygotom
328cc 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
328cd 74 20 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  t = yymsp[-5].mi
328ce 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67  nor.yy0.z;.  yyg
328cf 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
328d0 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e  End = &yymsp[0].
328d1 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73  minor.yy0.z[yyms
328d2 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
328d3 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ];.}.        bre
328d4 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
328d5 39 37 3a 20 2f 2a 20 72 61 69 73 65 74 79 70 65  97: /* raisetype
328d6 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 2a 2f   ::= ROLLBACK */
328d7 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
328d8 33 32 38 20 3d 20 4f 45 5f 52 6f 6c 6c 62 61 63  328 = OE_Rollbac
328d9 6b 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  k;}.        brea
328da 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39  k;.      case 29
328db 39 3a 20 2f 2a 20 72 61 69 73 65 74 79 70 65 20  9: /* raisetype 
328dc 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a 7b 79 79 67  ::= FAIL */.{yyg
328dd 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d  otominor.yy328 =
328de 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20 20 20 20 20   OE_Fail;}.     
328df 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
328e0 63 61 73 65 20 33 30 30 3a 20 2f 2a 20 63 6d 64  case 300: /* cmd
328e1 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47 47 45   ::= DROP TRIGGE
328e2 52 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  R ifexists fulln
328e3 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  ame */.{.  sqlit
328e4 65 33 44 72 6f 70 54 72 69 67 67 65 72 28 70 50  e3DropTrigger(pP
328e5 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  arse,yymsp[0].mi
328e6 6e 6f 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d  nor.yy65,yymsp[-
328e7 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  1].minor.yy328);
328e8 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
328e9 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 31  ;.      case 301
328ea 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 54 54  : /* cmd ::= ATT
328eb 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f  ACH database_kw_
328ec 6f 70 74 20 65 78 70 72 20 41 53 20 65 78 70 72  opt expr AS expr
328ed 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20   key_opt */.{.  
328ee 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 70 50  sqlite3Attach(pP
328ef 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e  arse, yymsp[-3].
328f0 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
328f1 72 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  r, yymsp[-1].min
328f2 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
328f3 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
328f4 79 31 33 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y132);.}.       
328f5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
328f6 73 65 20 33 30 32 3a 20 2f 2a 20 63 6d 64 20 3a  se 302: /* cmd :
328f7 3a 3d 20 44 45 54 41 43 48 20 64 61 74 61 62 61  := DETACH databa
328f8 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 2a  se_kw_opt expr *
328f9 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 65 74  /.{.  sqlite3Det
328fa 61 63 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ach(pParse, yyms
328fb 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
328fc 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20  .pExpr);.}.     
328fd 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
328fe 63 61 73 65 20 33 30 37 3a 20 2f 2a 20 63 6d 64  case 307: /* cmd
328ff 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 2a 2f 0a   ::= REINDEX */.
32900 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28  {sqlite3Reindex(
32901 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a  pParse, 0, 0);}.
32902 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32903 20 20 20 20 20 63 61 73 65 20 33 30 38 3a 20 2f       case 308: /
32904 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45  * cmd ::= REINDE
32905 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71  X nm dbnm */.{sq
32906 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70 50 61  lite3Reindex(pPa
32907 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e  rse, &yymsp[-1].
32908 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
32909 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
3290a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3290b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 39 3a  .      case 309:
3290c 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c   /* cmd ::= ANAL
3290d 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  YZE */.{sqlite3A
3290e 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 30  nalyze(pParse, 0
3290f 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  , 0);}.        b
32910 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32911 20 33 31 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   310: /* cmd ::=
32912 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d   ANALYZE nm dbnm
32913 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 6e 61 6c   */.{sqlite3Anal
32914 79 7a 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d  yze(pParse, &yym
32915 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
32916 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
32917 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
32918 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32919 73 65 20 33 31 31 3a 20 2f 2a 20 63 6d 64 20 3a  se 311: /* cmd :
3291a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 66  := ALTER TABLE f
3291b 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45 20 54  ullname RENAME T
3291c 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  O nm */.{.  sqli
3291d 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61  te3AlterRenameTa
3291e 62 6c 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70  ble(pParse,yymsp
3291f 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c  [-3].minor.yy65,
32920 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
32921 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
32922 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32923 65 20 33 31 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 312: /* cmd ::
32924 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64  = ALTER TABLE ad
32925 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d  d_column_fullnam
32926 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f  e ADD kwcolumn_o
32927 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 7b 0a 20  pt column */.{. 
32928 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e   sqlite3AlterFin
32929 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61  ishAddColumn(pPa
3292a 72 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  rse, &yymsp[0].m
3292b 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
3292c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3292d 20 20 63 61 73 65 20 33 31 33 3a 20 2f 2a 20 61    case 313: /* a
3292e 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
3292f 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65 20  me ::= fullname 
32930 2a 2f 0a 7b 0a 20 20 70 50 61 72 73 65 2d 3e 64  */.{.  pParse->d
32931 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
32932 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 73 71 6c  abled = 0;.  sql
32933 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64  ite3AlterBeginAd
32934 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20  dColumn(pParse, 
32935 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32936 79 36 35 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y65);.}.        
32937 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32938 65 20 33 31 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 316: /* cmd ::
32939 3d 20 63 72 65 61 74 65 5f 76 74 61 62 20 2a 2f  = create_vtab */
3293a 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e  .{sqlite3VtabFin
3293b 69 73 68 50 61 72 73 65 28 70 50 61 72 73 65 2c  ishParse(pParse,
3293c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
3293d 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
3293e 31 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63  17: /* cmd ::= c
3293f 72 65 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74  reate_vtab LP vt
32940 61 62 61 72 67 6c 69 73 74 20 52 50 20 2a 2f 0a  abarglist RP */.
32941 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69  {sqlite3VtabFini
32942 73 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 26  shParse(pParse,&
32943 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32944 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
32945 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32946 33 31 38 3a 20 2f 2a 20 63 72 65 61 74 65 5f 76  318: /* create_v
32947 74 61 62 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  tab ::= createkw
32948 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 6e   VIRTUAL TABLE n
32949 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e 6d 20  m dbnm USING nm 
3294a 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  */.{.    sqlite3
3294b 56 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 70  VtabBeginParse(p
3294c 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 33  Parse, &yymsp[-3
3294d 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
3294e 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3294f 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
32950 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
32951 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32952 63 61 73 65 20 33 32 31 3a 20 2f 2a 20 76 74 61  case 321: /* vta
32953 62 61 72 67 20 3a 3a 3d 20 2a 2f 0a 7b 73 71 6c  barg ::= */.{sql
32954 69 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28  ite3VtabArgInit(
32955 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20  pParse);}.      
32956 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32957 61 73 65 20 33 32 33 3a 20 2f 2a 20 76 74 61 62  ase 323: /* vtab
32958 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59  argtoken ::= ANY
32959 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
3295a 32 34 3a 20 2f 2a 20 76 74 61 62 61 72 67 74 6f  24: /* vtabargto
3295b 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69  ken ::= lp anyli
3295c 73 74 20 52 50 20 2a 2f 20 79 79 74 65 73 74 63  st RP */ yytestc
3295d 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32  ase(yyruleno==32
3295e 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  4);.      case 3
3295f 32 35 3a 20 2f 2a 20 6c 70 20 3a 3a 3d 20 4c 50  25: /* lp ::= LP
32960 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32961 79 72 75 6c 65 6e 6f 3d 3d 33 32 35 29 3b 0a 7b  yruleno==325);.{
32962 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78  sqlite3VtabArgEx
32963 74 65 6e 64 28 70 50 61 72 73 65 2c 26 79 79 6d  tend(pParse,&yym
32964 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
32965 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32966 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  ;.      default:
32967 0a 20 20 20 20 20 20 2f 2a 20 28 30 29 20 69 6e  .      /* (0) in
32968 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20  put ::= cmdlist 
32969 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3296a 72 75 6c 65 6e 6f 3d 3d 30 29 3b 0a 20 20 20 20  ruleno==0);.    
3296b 20 20 2f 2a 20 28 31 29 20 63 6d 64 6c 69 73 74    /* (1) cmdlist
3296c 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d   ::= cmdlist ecm
3296d 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  d */ yytestcase(
3296e 79 79 72 75 6c 65 6e 6f 3d 3d 31 29 3b 0a 20 20  yyruleno==1);.  
3296f 20 20 20 20 2f 2a 20 28 32 29 20 63 6d 64 6c 69      /* (2) cmdli
32970 73 74 20 3a 3a 3d 20 65 63 6d 64 20 2a 2f 20 79  st ::= ecmd */ y
32971 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32972 6e 6f 3d 3d 32 29 3b 0a 20 20 20 20 20 20 2f 2a  no==2);.      /*
32973 20 28 33 29 20 65 63 6d 64 20 3a 3a 3d 20 53 45   (3) ecmd ::= SE
32974 4d 49 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  MI */ yytestcase
32975 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 29 3b 0a 20  (yyruleno==3);. 
32976 20 20 20 20 20 2f 2a 20 28 34 29 20 65 63 6d 64       /* (4) ecmd
32977 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64   ::= explain cmd
32978 78 20 53 45 4d 49 20 2a 2f 20 79 79 74 65 73 74  x SEMI */ yytest
32979 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34  case(yyruleno==4
3297a 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 30 29  );.      /* (10)
3297b 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 2a   trans_opt ::= *
3297c 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3297d 75 6c 65 6e 6f 3d 3d 31 30 29 3b 0a 20 20 20 20  uleno==10);.    
3297e 20 20 2f 2a 20 28 31 31 29 20 74 72 61 6e 73 5f    /* (11) trans_
3297f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
32980 49 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ION */ yytestcas
32981 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 29 3b  e(yyruleno==11);
32982 0a 20 20 20 20 20 20 2f 2a 20 28 31 32 29 20 74  .      /* (12) t
32983 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41  rans_opt ::= TRA
32984 4e 53 41 43 54 49 4f 4e 20 6e 6d 20 2a 2f 20 79  NSACTION nm */ y
32985 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32986 6e 6f 3d 3d 31 32 29 3b 0a 20 20 20 20 20 20 2f  no==12);.      /
32987 2a 20 28 32 30 29 20 73 61 76 65 70 6f 69 6e 74  * (20) savepoint
32988 5f 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49  _opt ::= SAVEPOI
32989 4e 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  NT */ yytestcase
3298a 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 29 3b 0a  (yyruleno==20);.
3298b 20 20 20 20 20 20 2f 2a 20 28 32 31 29 20 73 61        /* (21) sa
3298c 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20  vepoint_opt ::= 
3298d 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3298e 72 75 6c 65 6e 6f 3d 3d 32 31 29 3b 0a 20 20 20  ruleno==21);.   
3298f 20 20 20 2f 2a 20 28 32 35 29 20 63 6d 64 20 3a     /* (25) cmd :
32990 3a 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20  := create_table 
32991 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
32992 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  s */ yytestcase(
32993 79 79 72 75 6c 65 6e 6f 3d 3d 32 35 29 3b 0a 20  yyruleno==25);. 
32994 20 20 20 20 20 2f 2a 20 28 33 34 29 20 63 6f 6c       /* (34) col
32995 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75  umnlist ::= colu
32996 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41 20 63 6f 6c  mnlist COMMA col
32997 75 6d 6e 20 2a 2f 20 79 79 74 65 73 74 63 61 73  umn */ yytestcas
32998 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 34 29 3b  e(yyruleno==34);
32999 0a 20 20 20 20 20 20 2f 2a 20 28 33 35 29 20 63  .      /* (35) c
3299a 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f  olumnlist ::= co
3299b 6c 75 6d 6e 20 2a 2f 20 79 79 74 65 73 74 63 61  lumn */ yytestca
3299c 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 35 29  se(yyruleno==35)
3299d 3b 0a 20 20 20 20 20 20 2f 2a 20 28 34 34 29 20  ;.      /* (44) 
3299e 74 79 70 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  type ::= */ yyte
3299f 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
329a0 3d 34 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =44);.      /* (
329a1 35 31 29 20 73 69 67 6e 65 64 20 3a 3a 3d 20 70  51) signed ::= p
329a2 6c 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65 73  lus_num */ yytes
329a3 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
329a4 35 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35  51);.      /* (5
329a5 32 29 20 73 69 67 6e 65 64 20 3a 3a 3d 20 6d 69  2) signed ::= mi
329a6 6e 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65 73  nus_num */ yytes
329a7 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
329a8 35 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35  52);.      /* (5
329a9 33 29 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20  3) carglist ::= 
329aa 63 61 72 67 6c 69 73 74 20 63 61 72 67 20 2a 2f  carglist carg */
329ab 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
329ac 6c 65 6e 6f 3d 3d 35 33 29 3b 0a 20 20 20 20 20  leno==53);.     
329ad 20 2f 2a 20 28 35 34 29 20 63 61 72 67 6c 69 73   /* (54) carglis
329ae 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
329af 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 34  ase(yyruleno==54
329b0 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35 35 29  );.      /* (55)
329b1 20 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52   carg ::= CONSTR
329b2 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 20 2a 2f  AINT nm ccons */
329b3 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
329b4 6c 65 6e 6f 3d 3d 35 35 29 3b 0a 20 20 20 20 20  leno==55);.     
329b5 20 2f 2a 20 28 35 36 29 20 63 61 72 67 20 3a 3a   /* (56) carg ::
329b6 3d 20 63 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73  = ccons */ yytes
329b7 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
329b8 35 36 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 36  56);.      /* (6
329b9 32 29 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c  2) ccons ::= NUL
329ba 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 20 79 79 74 65  L onconf */ yyte
329bb 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
329bc 3d 36 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =62);.      /* (
329bd 38 39 29 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d  89) conslist ::=
329be 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20   conslist COMMA 
329bf 74 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74 63  tcons */ yytestc
329c0 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 39  ase(yyruleno==89
329c1 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 39 30 29  );.      /* (90)
329c2 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f   conslist ::= co
329c3 6e 73 6c 69 73 74 20 74 63 6f 6e 73 20 2a 2f 20  nslist tcons */ 
329c4 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
329c5 65 6e 6f 3d 3d 39 30 29 3b 0a 20 20 20 20 20 20  eno==90);.      
329c6 2f 2a 20 28 39 31 29 20 63 6f 6e 73 6c 69 73 74  /* (91) conslist
329c7 20 3a 3a 3d 20 74 63 6f 6e 73 20 2a 2f 20 79 79   ::= tcons */ yy
329c8 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
329c9 6f 3d 3d 39 31 29 3b 0a 20 20 20 20 20 20 2f 2a  o==91);.      /*
329ca 20 28 39 32 29 20 74 63 6f 6e 73 20 3a 3a 3d 20   (92) tcons ::= 
329cb 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20 2a 2f  CONSTRAINT nm */
329cc 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
329cd 6c 65 6e 6f 3d 3d 39 32 29 3b 0a 20 20 20 20 20  leno==92);.     
329ce 20 2f 2a 20 28 32 36 38 29 20 70 6c 75 73 5f 6f   /* (268) plus_o
329cf 70 74 20 3a 3a 3d 20 50 4c 55 53 20 2a 2f 20 79  pt ::= PLUS */ y
329d0 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
329d1 6e 6f 3d 3d 32 36 38 29 3b 0a 20 20 20 20 20 20  no==268);.      
329d2 2f 2a 20 28 32 36 39 29 20 70 6c 75 73 5f 6f 70  /* (269) plus_op
329d3 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
329d4 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36  ase(yyruleno==26
329d5 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 37  9);.      /* (27
329d6 39 29 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73  9) foreach_claus
329d7 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  e ::= */ yytestc
329d8 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 37  ase(yyruleno==27
329d9 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 38  9);.      /* (28
329da 30 29 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73  0) foreach_claus
329db 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20 52  e ::= FOR EACH R
329dc 4f 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  OW */ yytestcase
329dd 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 38 30 29 3b  (yyruleno==280);
329de 0a 20 20 20 20 20 20 2f 2a 20 28 32 38 37 29 20  .      /* (287) 
329df 74 72 69 64 78 62 79 20 3a 3a 3d 20 2a 2f 20 79  tridxby ::= */ y
329e0 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
329e1 6e 6f 3d 3d 32 38 37 29 3b 0a 20 20 20 20 20 20  no==287);.      
329e2 2f 2a 20 28 33 30 35 29 20 64 61 74 61 62 61 73  /* (305) databas
329e3 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54  e_kw_opt ::= DAT
329e4 41 42 41 53 45 20 2a 2f 20 79 79 74 65 73 74 63  ABASE */ yytestc
329e5 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30  ase(yyruleno==30
329e6 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 30  5);.      /* (30
329e7 36 29 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  6) database_kw_o
329e8 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
329e9 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
329ea 30 36 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  06);.      /* (3
329eb 31 34 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  14) kwcolumn_opt
329ec 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
329ed 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 34  se(yyruleno==314
329ee 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 31 35  );.      /* (315
329ef 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a  ) kwcolumn_opt :
329f0 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 20 2a 2f 20 79  := COLUMNKW */ y
329f1 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
329f2 6e 6f 3d 3d 33 31 35 29 3b 0a 20 20 20 20 20 20  no==315);.      
329f3 2f 2a 20 28 33 31 39 29 20 76 74 61 62 61 72 67  /* (319) vtabarg
329f4 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67  list ::= vtabarg
329f5 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
329f6 79 72 75 6c 65 6e 6f 3d 3d 33 31 39 29 3b 0a 20  yruleno==319);. 
329f7 20 20 20 20 20 2f 2a 20 28 33 32 30 29 20 76 74       /* (320) vt
329f8 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74  abarglist ::= vt
329f9 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20  abarglist COMMA 
329fa 76 74 61 62 61 72 67 20 2a 2f 20 79 79 74 65 73  vtabarg */ yytes
329fb 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
329fc 33 32 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  320);.      /* (
329fd 33 32 32 29 20 76 74 61 62 61 72 67 20 3a 3a 3d  322) vtabarg ::=
329fe 20 76 74 61 62 61 72 67 20 76 74 61 62 61 72 67   vtabarg vtabarg
329ff 74 6f 6b 65 6e 20 2a 2f 20 79 79 74 65 73 74 63  token */ yytestc
32a00 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32  ase(yyruleno==32
32a01 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32  2);.      /* (32
32a02 36 29 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 2a  6) anylist ::= *
32a03 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32a04 75 6c 65 6e 6f 3d 3d 33 32 36 29 3b 0a 20 20 20  uleno==326);.   
32a05 20 20 20 2f 2a 20 28 33 32 37 29 20 61 6e 79 6c     /* (327) anyl
32a06 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20  ist ::= anylist 
32a07 4c 50 20 61 6e 79 6c 69 73 74 20 52 50 20 2a 2f  LP anylist RP */
32a08 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32a09 6c 65 6e 6f 3d 3d 33 32 37 29 3b 0a 20 20 20 20  leno==327);.    
32a0a 20 20 2f 2a 20 28 33 32 38 29 20 61 6e 79 6c 69    /* (328) anyli
32a0b 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41  st ::= anylist A
32a0c 4e 59 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  NY */ yytestcase
32a0d 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 38 29 3b  (yyruleno==328);
32a0e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32a0f 20 20 7d 3b 0a 20 20 79 79 67 6f 74 6f 20 3d 20    };.  yygoto = 
32a10 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75 6c  yyRuleInfo[yyrul
32a11 65 6e 6f 5d 2e 6c 68 73 3b 0a 20 20 79 79 73 69  eno].lhs;.  yysi
32a12 7a 65 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b  ze = yyRuleInfo[
32a13 79 79 72 75 6c 65 6e 6f 5d 2e 6e 72 68 73 3b 0a  yyruleno].nrhs;.
32a14 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69    yypParser->yyi
32a15 64 78 20 2d 3d 20 79 79 73 69 7a 65 3b 0a 20 20  dx -= yysize;.  
32a16 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f  yyact = yy_find_
32a17 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 79 79  reduce_action(yy
32a18 6d 73 70 5b 2d 79 79 73 69 7a 65 5d 2e 73 74 61  msp[-yysize].sta
32a19 74 65 6e 6f 2c 28 59 59 43 4f 44 45 54 59 50 45  teno,(YYCODETYPE
32a1a 29 79 79 67 6f 74 6f 29 3b 0a 20 20 69 66 28 20  )yygoto);.  if( 
32a1b 79 79 61 63 74 20 3c 20 59 59 4e 53 54 41 54 45  yyact < YYNSTATE
32a1c 20 29 7b 0a 23 69 66 64 65 66 20 4e 44 45 42 55   ){.#ifdef NDEBU
32a1d 47 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61  G.    /* If we a
32a1e 72 65 20 6e 6f 74 20 64 65 62 75 67 67 69 6e 67  re not debugging
32a1f 20 61 6e 64 20 74 68 65 20 72 65 64 75 63 65 20   and the reduce 
32a20 61 63 74 69 6f 6e 20 70 6f 70 70 65 64 20 61 74  action popped at
32a21 20 6c 65 61 73 74 0a 20 20 20 20 2a 2a 20 6f 6e   least.    ** on
32a22 65 20 65 6c 65 6d 65 6e 74 20 6f 66 66 20 74 68  e element off th
32a23 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20 77 65  e stack, then we
32a24 20 63 61 6e 20 70 75 73 68 20 74 68 65 20 6e 65   can push the ne
32a25 77 20 65 6c 65 6d 65 6e 74 20 62 61 63 6b 0a 20  w element back. 
32a26 20 20 20 2a 2a 20 6f 6e 74 6f 20 74 68 65 20 73     ** onto the s
32a27 74 61 63 6b 20 68 65 72 65 2c 20 61 6e 64 20 73  tack here, and s
32a28 6b 69 70 20 74 68 65 20 73 74 61 63 6b 20 6f 76  kip the stack ov
32a29 65 72 66 6c 6f 77 20 74 65 73 74 20 69 6e 20 79  erflow test in y
32a2a 79 5f 73 68 69 66 74 28 29 2e 0a 20 20 20 20 2a  y_shift()..    *
32a2b 2a 20 54 68 61 74 20 67 69 76 65 73 20 61 20 73  * That gives a s
32a2c 69 67 6e 69 66 69 63 61 6e 74 20 73 70 65 65 64  ignificant speed
32a2d 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 20 2a 2f   improvement. */
32a2e 0a 20 20 20 20 69 66 28 20 79 79 73 69 7a 65 20  .    if( yysize 
32a2f 29 7b 0a 20 20 20 20 20 20 79 79 70 50 61 72 73  ){.      yypPars
32a30 65 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 20 20 20  er->yyidx++;.   
32a31 20 20 20 79 79 6d 73 70 20 2d 3d 20 79 79 73 69     yymsp -= yysi
32a32 7a 65 2d 31 3b 0a 20 20 20 20 20 20 79 79 6d 73  ze-1;.      yyms
32a33 70 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 28 59 59  p->stateno = (YY
32a34 41 43 54 49 4f 4e 54 59 50 45 29 79 79 61 63 74  ACTIONTYPE)yyact
32a35 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 6d  ;.      yymsp->m
32a36 61 6a 6f 72 20 3d 20 28 59 59 43 4f 44 45 54 59  ajor = (YYCODETY
32a37 50 45 29 79 79 67 6f 74 6f 3b 0a 20 20 20 20 20  PE)yygoto;.     
32a38 20 79 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d 20   yymsp->minor = 
32a39 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20 20  yygotominor;.   
32a3a 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
32a3b 20 20 7b 0a 20 20 20 20 20 20 79 79 5f 73 68 69    {.      yy_shi
32a3c 66 74 28 79 79 70 50 61 72 73 65 72 2c 79 79 61  ft(yypParser,yya
32a3d 63 74 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f 74  ct,yygoto,&yygot
32a3e 6f 6d 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a 20  ominor);.    }. 
32a3f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
32a40 72 74 28 20 79 79 61 63 74 20 3d 3d 20 59 59 4e  rt( yyact == YYN
32a41 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20  STATE + YYNRULE 
32a42 2b 20 31 20 29 3b 0a 20 20 20 20 79 79 5f 61 63  + 1 );.    yy_ac
32a43 63 65 70 74 28 79 79 70 50 61 72 73 65 72 29 3b  cept(yypParser);
32a44 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
32a45 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65  e following code
32a46 20 65 78 65 63 75 74 65 73 20 77 68 65 6e 20 74   executes when t
32a47 68 65 20 70 61 72 73 65 20 66 61 69 6c 73 0a 2a  he parse fails.*
32a48 2f 0a 23 69 66 6e 64 65 66 20 59 59 4e 4f 45 52  /.#ifndef YYNOER
32a49 52 4f 52 52 45 43 4f 56 45 52 59 0a 73 74 61 74  RORRECOVERY.stat
32a4a 69 63 20 76 6f 69 64 20 79 79 5f 70 61 72 73 65  ic void yy_parse
32a4b 5f 66 61 69 6c 65 64 28 0a 20 20 79 79 50 61 72  _failed(.  yyPar
32a4c 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 20 20  ser *yypParser  
32a4d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
32a4e 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73  parser */.){.  s
32a4f 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
32a50 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e  FETCH;.#ifndef N
32a51 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72  DEBUG.  if( yyTr
32a52 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66  aceFILE ){.    f
32a53 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
32a54 4c 45 2c 22 25 73 46 61 69 6c 21 5c 6e 22 2c 79  LE,"%sFail!\n",y
32a55 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20  yTracePrompt);. 
32a56 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68 69 6c   }.#endif.  whil
32a57 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  e( yypParser->yy
32a58 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f  idx>=0 ) yy_pop_
32a59 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70  parser_stack(yyp
32a5a 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65  Parser);.  /* He
32a5b 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72  re code is inser
32a5c 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20 62  ted which will b
32a5d 65 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 65  e executed whene
32a5e 76 65 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 72  ver the.  ** par
32a5f 73 65 72 20 66 61 69 6c 73 20 2a 2f 0a 20 20 73  ser fails */.  s
32a60 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
32a61 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65  STORE; /* Suppre
32a62 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74  ss warning about
32a63 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61   unused %extra_a
32a64 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65  rgument variable
32a65 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   */.}.#endif /* 
32a66 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52  YYNOERRORRECOVER
32a67 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  Y */../*.** The 
32a68 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65  following code e
32a69 78 65 63 75 74 65 73 20 77 68 65 6e 20 61 20 73  xecutes when a s
32a6a 79 6e 74 61 78 20 65 72 72 6f 72 20 66 69 72 73  yntax error firs
32a6b 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61  t occurs..*/.sta
32a6c 74 69 63 20 76 6f 69 64 20 79 79 5f 73 79 6e 74  tic void yy_synt
32a6d 61 78 5f 65 72 72 6f 72 28 0a 20 20 79 79 50 61  ax_error(.  yyPa
32a6e 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c  rser *yypParser,
32a6f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
32a70 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e  e parser */.  in
32a71 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20  t yymajor,      
32a72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32a73 54 68 65 20 6d 61 6a 6f 72 20 74 79 70 65 20 6f  The major type o
32a74 66 20 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65  f the error toke
32a75 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59  n */.  YYMINORTY
32a76 50 45 20 79 79 6d 69 6e 6f 72 20 20 20 20 20 20  PE yyminor      
32a77 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 69 6e        /* The min
32a78 6f 72 20 74 79 70 65 20 6f 66 20 74 68 65 20 65  or type of the e
32a79 72 72 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b  rror token */.){
32a7a 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
32a7b 41 52 47 5f 46 45 54 43 48 3b 0a 23 64 65 66 69  ARG_FETCH;.#defi
32a7c 6e 65 20 54 4f 4b 45 4e 20 28 79 79 6d 69 6e 6f  ne TOKEN (yymino
32a7d 72 2e 79 79 30 29 0a 0a 20 20 55 4e 55 53 45 44  r.yy0)..  UNUSED
32a7e 5f 50 41 52 41 4d 45 54 45 52 28 79 79 6d 61 6a  _PARAMETER(yymaj
32a7f 6f 72 29 3b 20 20 2f 2a 20 53 69 6c 65 6e 63 65  or);  /* Silence
32a80 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 20 77   some compiler w
32a81 61 72 6e 69 6e 67 73 20 2a 2f 0a 20 20 61 73 73  arnings */.  ass
32a82 65 72 74 28 20 54 4f 4b 45 4e 2e 7a 5b 30 5d 20  ert( TOKEN.z[0] 
32a83 29 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e  );  /* The token
32a84 69 7a 65 72 20 61 6c 77 61 79 73 20 67 69 76 65  izer always give
32a85 73 20 75 73 20 61 20 74 6f 6b 65 6e 20 2a 2f 0a  s us a token */.
32a86 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
32a87 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61 72 20  g(pParse, "near 
32a88 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78 20 65  \"%T\": syntax e
32a89 72 72 6f 72 22 2c 20 26 54 4f 4b 45 4e 29 3b 0a  rror", &TOKEN);.
32a8a 20 20 70 50 61 72 73 65 2d 3e 70 61 72 73 65 45    pParse->parseE
32a8b 72 72 6f 72 20 3d 20 31 3b 0a 20 20 73 71 6c 69  rror = 1;.  sqli
32a8c 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
32a8d 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20  RE; /* Suppress 
32a8e 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e  warning about un
32a8f 75 73 65 64 20 25 65 78 74 72 61 5f 61 72 67 75  used %extra_argu
32a90 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20 2a 2f  ment variable */
32a91 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
32a92 6c 6c 6f 77 69 6e 67 20 69 73 20 65 78 65 63 75  llowing is execu
32a93 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 72  ted when the par
32a94 73 65 72 20 61 63 63 65 70 74 73 0a 2a 2f 0a 73  ser accepts.*/.s
32a95 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63  tatic void yy_ac
32a96 63 65 70 74 28 0a 20 20 79 79 50 61 72 73 65 72  cept(.  yyParser
32a97 20 2a 79 79 70 50 61 72 73 65 72 20 20 20 20 20   *yypParser     
32a98 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
32a99 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ser */.){.  sqli
32a9a 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
32a9b 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  CH;.#ifndef NDEB
32a9c 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  UG.  if( yyTrace
32a9d 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69  FILE ){.    fpri
32a9e 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
32a9f 22 25 73 41 63 63 65 70 74 21 5c 6e 22 2c 79 79  "%sAccept!\n",yy
32aa0 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20  TracePrompt);.  
32aa1 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68 69 6c 65  }.#endif.  while
32aa2 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
32aa3 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70  dx>=0 ) yy_pop_p
32aa4 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50  arser_stack(yypP
32aa5 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65 72  arser);.  /* Her
32aa6 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74  e code is insert
32aa7 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65  ed which will be
32aa8 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 65 76   executed whenev
32aa9 65 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 72 73  er the.  ** pars
32aaa 65 72 20 61 63 63 65 70 74 73 20 2a 2f 0a 20 20  er accepts */.  
32aab 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
32aac 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72  _STORE; /* Suppr
32aad 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75  ess warning abou
32aae 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f  t unused %extra_
32aaf 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c  argument variabl
32ab0 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54 68 65 20 6d  e */.}../* The m
32ab1 61 69 6e 20 70 61 72 73 65 72 20 70 72 6f 67 72  ain parser progr
32ab2 61 6d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  am..** The first
32ab3 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
32ab4 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75  ointer to a stru
32ab5 63 74 75 72 65 20 6f 62 74 61 69 6e 65 64 20 66  cture obtained f
32ab6 72 6f 6d 0a 2a 2a 20 22 73 71 6c 69 74 65 33 50  rom.** "sqlite3P
32ab7 61 72 73 65 72 41 6c 6c 6f 63 22 20 77 68 69 63  arserAlloc" whic
32ab8 68 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  h describes the 
32ab9 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
32aba 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20   the parser..** 
32abb 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
32abc 65 6e 74 20 69 73 20 74 68 65 20 6d 61 6a 6f 72  ent is the major
32abd 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 20 20   token number.  
32abe 54 68 65 20 74 68 69 72 64 20 69 73 0a 2a 2a 20  The third is.** 
32abf 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 2e  the minor token.
32ac0 20 20 54 68 65 20 66 6f 75 72 74 68 20 6f 70 74    The fourth opt
32ac1 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 69  ional argument i
32ac2 73 20 77 68 61 74 65 76 65 72 20 74 68 65 0a 2a  s whatever the.*
32ac3 2a 20 75 73 65 72 20 77 61 6e 74 73 20 28 61 6e  * user wants (an
32ac4 64 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  d specified in t
32ac5 68 65 20 67 72 61 6d 6d 61 72 29 20 61 6e 64 20  he grammar) and 
32ac6 69 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  is available for
32ac7 0a 2a 2a 20 75 73 65 20 62 79 20 74 68 65 20 61  .** use by the a
32ac8 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a  ction routines..
32ac9 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a  **.** Inputs:.**
32aca 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20   <ul>.** <li> A 
32acb 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
32acc 61 72 73 65 72 20 28 61 6e 20 6f 70 61 71 75 65  arser (an opaque
32acd 20 73 74 72 75 63 74 75 72 65 2e 29 0a 2a 2a 20   structure.).** 
32ace 3c 6c 69 3e 20 54 68 65 20 6d 61 6a 6f 72 20 74  <li> The major t
32acf 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  oken number..** 
32ad0 3c 6c 69 3e 20 54 68 65 20 6d 69 6e 6f 72 20 74  <li> The minor t
32ad1 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  oken number..** 
32ad2 3c 6c 69 3e 20 41 6e 20 6f 70 74 69 6f 6e 20 61  <li> An option a
32ad3 72 67 75 6d 65 6e 74 20 6f 66 20 61 20 67 72 61  rgument of a gra
32ad4 6d 6d 61 72 2d 73 70 65 63 69 66 69 65 64 20 74  mmar-specified t
32ad5 79 70 65 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  ype..** </ul>.**
32ad6 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20  .** Outputs:.** 
32ad7 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  None..*/.SQLITE_
32ad8 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
32ad9 69 74 65 33 50 61 72 73 65 72 28 0a 20 20 76 6f  ite3Parser(.  vo
32ada 69 64 20 2a 79 79 70 2c 20 20 20 20 20 20 20 20  id *yyp,        
32adb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
32adc 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e  e parser */.  in
32add 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20  t yymajor,      
32ade 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
32adf 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 63 6f  e major token co
32ae0 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73  de number */.  s
32ae1 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
32ae2 4e 54 59 50 45 20 79 79 6d 69 6e 6f 72 20 20 20  NTYPE yyminor   
32ae3 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65      /* The value
32ae4 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 2a   for the token *
32ae5 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
32ae6 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20 20  rARG_PDECL      
32ae7 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69           /* Opti
32ae8 6f 6e 61 6c 20 25 65 78 74 72 61 5f 61 72 67 75  onal %extra_argu
32ae9 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 2a  ment parameter *
32aea 2f 0a 29 7b 0a 20 20 59 59 4d 49 4e 4f 52 54 59  /.){.  YYMINORTY
32aeb 50 45 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 3b  PE yyminorunion;
32aec 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20 20  .  int yyact;   
32aed 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
32aee 70 61 72 73 65 72 20 61 63 74 69 6f 6e 2e 20 2a  parser action. *
32aef 2f 0a 20 20 69 6e 74 20 79 79 65 6e 64 6f 66 69  /.  int yyendofi
32af0 6e 70 75 74 3b 20 20 20 20 20 2f 2a 20 54 72 75  nput;     /* Tru
32af1 65 20 69 66 20 77 65 20 61 72 65 20 61 74 20 74  e if we are at t
32af2 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20  he end of input 
32af3 2a 2f 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f  */.#ifdef YYERRO
32af4 52 53 59 4d 42 4f 4c 0a 20 20 69 6e 74 20 79 79  RSYMBOL.  int yy
32af5 65 72 72 6f 72 68 69 74 20 3d 20 30 3b 20 20 20  errorhit = 0;   
32af6 2f 2a 20 54 72 75 65 20 69 66 20 79 79 6d 61 6a  /* True if yymaj
32af7 6f 72 20 68 61 73 20 69 6e 76 6f 6b 65 64 20 61  or has invoked a
32af8 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 65 6e 64 69  n error */.#endi
32af9 66 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79  f.  yyParser *yy
32afa 70 50 61 72 73 65 72 3b 20 20 2f 2a 20 54 68 65  pParser;  /* The
32afb 20 70 61 72 73 65 72 20 2a 2f 0a 0a 20 20 2f 2a   parser */..  /*
32afc 20 28 72 65 29 69 6e 69 74 69 61 6c 69 7a 65 20   (re)initialize 
32afd 74 68 65 20 70 61 72 73 65 72 2c 20 69 66 20 6e  the parser, if n
32afe 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20 79 79  ecessary */.  yy
32aff 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72  pParser = (yyPar
32b00 73 65 72 2a 29 79 79 70 3b 0a 20 20 69 66 28 20  ser*)yyp;.  if( 
32b01 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
32b02 3c 30 20 29 7b 0a 23 69 66 20 59 59 53 54 41 43  <0 ){.#if YYSTAC
32b03 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20 69 66  KDEPTH<=0.    if
32b04 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73  ( yypParser->yys
32b05 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a 20 20 20 20  tksz <=0 ){.    
32b06 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79 6d 69    /*memset(&yymi
32b07 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c 20 73 69 7a  norunion, 0, siz
32b08 65 6f 66 28 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  eof(yyminorunion
32b09 29 29 3b 2a 2f 0a 20 20 20 20 20 20 79 79 6d 69  ));*/.      yymi
32b0a 6e 6f 72 75 6e 69 6f 6e 20 3d 20 79 79 7a 65 72  norunion = yyzer
32b0b 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 20 20 79 79  ominor;.      yy
32b0c 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79  StackOverflow(yy
32b0d 70 50 61 72 73 65 72 2c 20 26 79 79 6d 69 6e 6f  pParser, &yymino
32b0e 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 72  runion);.      r
32b0f 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 23 65 6e  eturn;.    }.#en
32b10 64 69 66 0a 20 20 20 20 79 79 70 50 61 72 73 65  dif.    yypParse
32b11 72 2d 3e 79 79 69 64 78 20 3d 20 30 3b 0a 20 20  r->yyidx = 0;.  
32b12 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65    yypParser->yye
32b13 72 72 63 6e 74 20 3d 20 2d 31 3b 0a 20 20 20 20  rrcnt = -1;.    
32b14 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
32b15 63 6b 5b 30 5d 2e 73 74 61 74 65 6e 6f 20 3d 20  ck[0].stateno = 
32b16 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72  0;.    yypParser
32b17 2d 3e 79 79 73 74 61 63 6b 5b 30 5d 2e 6d 61 6a  ->yystack[0].maj
32b18 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 79 79  or = 0;.  }.  yy
32b19 6d 69 6e 6f 72 75 6e 69 6f 6e 2e 79 79 30 20 3d  minorunion.yy0 =
32b1a 20 79 79 6d 69 6e 6f 72 3b 0a 20 20 79 79 65 6e   yyminor;.  yyen
32b1b 64 6f 66 69 6e 70 75 74 20 3d 20 28 79 79 6d 61  dofinput = (yyma
32b1c 6a 6f 72 3d 3d 30 29 3b 0a 20 20 73 71 6c 69 74  jor==0);.  sqlit
32b1d 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52  e3ParserARG_STOR
32b1e 45 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  E;..#ifndef NDEB
32b1f 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  UG.  if( yyTrace
32b20 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69  FILE ){.    fpri
32b21 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
32b22 22 25 73 49 6e 70 75 74 20 25 73 5c 6e 22 2c 79  "%sInput %s\n",y
32b23 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54  yTracePrompt,yyT
32b24 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72  okenName[yymajor
32b25 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ]);.  }.#endif..
32b26 20 20 64 6f 7b 0a 20 20 20 20 79 79 61 63 74 20    do{.    yyact 
32b27 3d 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f  = yy_find_shift_
32b28 61 63 74 69 6f 6e 28 79 79 70 50 61 72 73 65 72  action(yypParser
32b29 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d  ,(YYCODETYPE)yym
32b2a 61 6a 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 79  ajor);.    if( y
32b2b 79 61 63 74 3c 59 59 4e 53 54 41 54 45 20 29 7b  yact<YYNSTATE ){
32b2c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
32b2d 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29 3b 20  yyendofinput ); 
32b2e 20 2f 2a 20 49 6d 70 6f 73 73 69 62 6c 65 20 74   /* Impossible t
32b2f 6f 20 73 68 69 66 74 20 74 68 65 20 24 20 74 6f  o shift the $ to
32b30 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 79 79 5f  ken */.      yy_
32b31 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72 2c  shift(yypParser,
32b32 79 79 61 63 74 2c 79 79 6d 61 6a 6f 72 2c 26 79  yyact,yymajor,&y
32b33 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
32b34 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
32b35 79 65 72 72 63 6e 74 2d 2d 3b 0a 20 20 20 20 20  yerrcnt--;.     
32b36 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
32b37 4f 44 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ODE;.    }else i
32b38 66 28 20 79 79 61 63 74 20 3c 20 59 59 4e 53 54  f( yyact < YYNST
32b39 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 29 7b  ATE + YYNRULE ){
32b3a 0a 20 20 20 20 20 20 79 79 5f 72 65 64 75 63 65  .      yy_reduce
32b3b 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
32b3c 2d 59 59 4e 53 54 41 54 45 29 3b 0a 20 20 20 20  -YYNSTATE);.    
32b3d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
32b3e 65 72 74 28 20 79 79 61 63 74 20 3d 3d 20 59 59  ert( yyact == YY
32b3f 5f 45 52 52 4f 52 5f 41 43 54 49 4f 4e 20 29 3b  _ERROR_ACTION );
32b40 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53  .#ifdef YYERRORS
32b41 59 4d 42 4f 4c 0a 20 20 20 20 20 20 69 6e 74 20  YMBOL.      int 
32b42 79 79 6d 78 3b 0a 23 65 6e 64 69 66 0a 23 69 66  yymx;.#endif.#if
32b43 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
32b44 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
32b45 45 20 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72  E ){.        fpr
32b46 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
32b47 2c 22 25 73 53 79 6e 74 61 78 20 45 72 72 6f 72  ,"%sSyntax Error
32b48 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  !\n",yyTraceProm
32b49 70 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  pt);.      }.#en
32b4a 64 69 66 0a 23 69 66 64 65 66 20 59 59 45 52 52  dif.#ifdef YYERR
32b4b 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20 2f  ORSYMBOL.      /
32b4c 2a 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f 72  * A syntax error
32b4d 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 20   has occurred.. 
32b4e 20 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 70       ** The resp
32b4f 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f 72  onse to an error
32b50 20 64 65 70 65 6e 64 73 20 75 70 6f 6e 20 77 68   depends upon wh
32b51 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
32b52 0a 20 20 20 20 20 20 2a 2a 20 67 72 61 6d 6d 61  .      ** gramma
32b53 72 20 64 65 66 69 6e 65 73 20 61 6e 20 65 72 72  r defines an err
32b54 6f 72 20 74 6f 6b 65 6e 20 22 45 52 52 4f 52 22  or token "ERROR"
32b55 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  .  .      **.   
32b56 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 77 68     ** This is wh
32b57 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65 20  at we do if the 
32b58 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 64 65 66  grammar does def
32b59 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20 20  ine ERROR:.     
32b5a 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20   **.      **  * 
32b5b 43 61 6c 6c 20 74 68 65 20 25 73 79 6e 74 61 78  Call the %syntax
32b5c 5f 65 72 72 6f 72 20 66 75 6e 63 74 69 6f 6e 2e  _error function.
32b5d 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
32b5e 2a 2a 20 20 2a 20 42 65 67 69 6e 20 70 6f 70 70  **  * Begin popp
32b5f 69 6e 67 20 74 68 65 20 73 74 61 63 6b 20 75 6e  ing the stack un
32b60 74 69 6c 20 77 65 20 65 6e 74 65 72 20 61 20 73  til we enter a s
32b61 74 61 74 65 20 77 68 65 72 65 0a 20 20 20 20 20  tate where.     
32b62 20 2a 2a 20 20 20 20 69 74 20 69 73 20 6c 65 67   **    it is leg
32b63 61 6c 20 74 6f 20 73 68 69 66 74 20 74 68 65 20  al to shift the 
32b64 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2c 20 74 68  error symbol, th
32b65 65 6e 20 73 68 69 66 74 0a 20 20 20 20 20 20 2a  en shift.      *
32b66 2a 20 20 20 20 74 68 65 20 65 72 72 6f 72 20 73  *    the error s
32b67 79 6d 62 6f 6c 2e 0a 20 20 20 20 20 20 2a 2a 0a  ymbol..      **.
32b68 20 20 20 20 20 20 2a 2a 20 20 2a 20 53 65 74 20        **  * Set 
32b69 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20  the error count 
32b6a 74 6f 20 74 68 72 65 65 2e 0a 20 20 20 20 20 20  to three..      
32b6b 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42  **.      **  * B
32b6c 65 67 69 6e 20 61 63 63 65 70 74 69 6e 67 20 61  egin accepting a
32b6d 6e 64 20 73 68 69 66 74 69 6e 67 20 6e 65 77 20  nd shifting new 
32b6e 74 6f 6b 65 6e 73 2e 20 20 4e 6f 20 6e 65 77 20  tokens.  No new 
32b6f 65 72 72 6f 72 0a 20 20 20 20 20 20 2a 2a 20 20  error.      **  
32b70 20 20 70 72 6f 63 65 73 73 69 6e 67 20 77 69 6c    processing wil
32b71 6c 20 6f 63 63 75 72 20 75 6e 74 69 6c 20 74 68  l occur until th
32b72 72 65 65 20 74 6f 6b 65 6e 73 20 68 61 76 65 20  ree tokens have 
32b73 62 65 65 6e 0a 20 20 20 20 20 20 2a 2a 20 20 20  been.      **   
32b74 20 73 68 69 66 74 65 64 20 73 75 63 63 65 73 73   shifted success
32b75 66 75 6c 6c 79 2e 0a 20 20 20 20 20 20 2a 2a 0a  fully..      **.
32b76 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
32b77 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
32b78 65 72 72 63 6e 74 3c 30 20 29 7b 0a 20 20 20 20  errcnt<0 ){.    
32b79 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f 65 72      yy_syntax_er
32b7a 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79 79  ror(yypParser,yy
32b7b 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e 69  major,yyminoruni
32b7c 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  on);.      }.   
32b7d 20 20 20 79 79 6d 78 20 3d 20 79 79 70 50 61 72     yymx = yypPar
32b7e 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70  ser->yystack[yyp
32b7f 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 6d  Parser->yyidx].m
32b80 61 6a 6f 72 3b 0a 20 20 20 20 20 20 69 66 28 20  ajor;.      if( 
32b81 79 79 6d 78 3d 3d 59 59 45 52 52 4f 52 53 59 4d  yymx==YYERRORSYM
32b82 42 4f 4c 20 7c 7c 20 79 79 65 72 72 6f 72 68 69  BOL || yyerrorhi
32b83 74 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45  t ){.#ifndef NDE
32b84 42 55 47 0a 20 20 20 20 20 20 20 20 69 66 28 20  BUG.        if( 
32b85 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
32b86 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66           fprintf
32b87 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
32b88 44 69 73 63 61 72 64 20 69 6e 70 75 74 20 74 6f  Discard input to
32b89 6b 65 6e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20  ken %s\n",.     
32b8a 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50          yyTraceP
32b8b 72 6f 6d 70 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d  rompt,yyTokenNam
32b8c 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20  e[yymajor]);.   
32b8d 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
32b8e 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75 63        yy_destruc
32b8f 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 20 28  tor(yypParser, (
32b90 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
32b91 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
32b92 29 3b 0a 20 20 20 20 20 20 20 20 79 79 6d 61 6a  );.        yymaj
32b93 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20  or = YYNOCODE;. 
32b94 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
32b95 20 20 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20       while(.    
32b96 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d        yypParser-
32b97 3e 79 79 69 64 78 20 3e 3d 20 30 20 26 26 0a 20  >yyidx >= 0 &&. 
32b98 20 20 20 20 20 20 20 20 20 79 79 6d 78 20 21 3d           yymx !=
32b99 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 26   YYERRORSYMBOL &
32b9a 26 0a 20 20 20 20 20 20 20 20 20 20 28 79 79 61  &.          (yya
32b9b 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65 64  ct = yy_find_red
32b9c 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20 20 20 20  uce_action(.    
32b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b9e 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
32b9f 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72  ystack[yypParser
32ba0 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e 6f  ->yyidx].stateno
32ba1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32ba2 20 20 20 20 20 20 20 20 20 20 59 59 45 52 52 4f            YYERRO
32ba3 52 53 59 4d 42 4f 4c 29 29 20 3e 3d 20 59 59 4e  RSYMBOL)) >= YYN
32ba4 53 54 41 54 45 0a 20 20 20 20 20 20 20 20 29 7b  STATE.        ){
32ba5 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 70 6f  .          yy_po
32ba6 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79  p_parser_stack(y
32ba7 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20 20 20  ypParser);.     
32ba8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
32ba9 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
32baa 78 20 3c 20 30 20 7c 7c 20 79 79 6d 61 6a 6f 72  x < 0 || yymajor
32bab 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
32bac 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79   yy_destructor(y
32bad 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45  ypParser,(YYCODE
32bae 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79  TYPE)yymajor,&yy
32baf 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
32bb0 20 20 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f         yy_parse_
32bb1 66 61 69 6c 65 64 28 79 79 70 50 61 72 73 65 72  failed(yypParser
32bb2 29 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d  );.          yym
32bb3 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b  ajor = YYNOCODE;
32bb4 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
32bb5 66 28 20 79 79 6d 78 21 3d 59 59 45 52 52 4f 52  f( yymx!=YYERROR
32bb6 53 59 4d 42 4f 4c 20 29 7b 0a 20 20 20 20 20 20  SYMBOL ){.      
32bb7 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20      YYMINORTYPE 
32bb8 75 32 3b 0a 20 20 20 20 20 20 20 20 20 20 75 32  u2;.          u2
32bb9 2e 59 59 45 52 52 53 59 4d 44 54 20 3d 20 30 3b  .YYERRSYMDT = 0;
32bba 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 73 68  .          yy_sh
32bbb 69 66 74 28 79 79 70 50 61 72 73 65 72 2c 79 79  ift(yypParser,yy
32bbc 61 63 74 2c 59 59 45 52 52 4f 52 53 59 4d 42 4f  act,YYERRORSYMBO
32bbd 4c 2c 26 75 32 29 3b 0a 20 20 20 20 20 20 20 20  L,&u2);.        
32bbe 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
32bbf 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72  yypParser->yyerr
32bc0 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 20 20 79  cnt = 3;.      y
32bc1 79 65 72 72 6f 72 68 69 74 20 3d 20 31 3b 0a 23  yerrorhit = 1;.#
32bc2 65 6c 69 66 20 64 65 66 69 6e 65 64 28 59 59 4e  elif defined(YYN
32bc3 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 29 0a  OERRORRECOVERY).
32bc4 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
32bc5 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52  YYNOERRORRECOVER
32bc6 59 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e  Y macro is defin
32bc7 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  ed, then do not 
32bc8 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 20 20 20  attempt to.     
32bc9 20 2a 2a 20 64 6f 20 61 6e 79 20 6b 69 6e 64 20   ** do any kind 
32bca 6f 66 20 65 72 72 6f 72 20 72 65 63 6f 76 65 72  of error recover
32bcb 79 2e 20 20 49 6e 73 74 65 61 64 2c 20 73 69 6d  y.  Instead, sim
32bcc 70 6c 79 20 69 6e 76 6f 6b 65 20 74 68 65 20 73  ply invoke the s
32bcd 79 6e 74 61 78 0a 20 20 20 20 20 20 2a 2a 20 65  yntax.      ** e
32bce 72 72 6f 72 20 72 6f 75 74 69 6e 65 20 61 6e 64  rror routine and
32bcf 20 63 6f 6e 74 69 6e 75 65 20 67 6f 69 6e 67 20   continue going 
32bd0 61 73 20 69 66 20 6e 6f 74 68 69 6e 67 20 68 61  as if nothing ha
32bd1 64 20 68 61 70 70 65 6e 65 64 2e 0a 20 20 20 20  d happened..    
32bd2 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 70    **.      ** Ap
32bd3 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73  plications can s
32bd4 65 74 20 74 68 69 73 20 6d 61 63 72 6f 20 28 66  et this macro (f
32bd5 6f 72 20 65 78 61 6d 70 6c 65 20 69 6e 73 69 64  or example insid
32bd6 65 20 25 69 6e 63 6c 75 64 65 29 20 69 66 0a 20  e %include) if. 
32bd7 20 20 20 20 20 2a 2a 20 74 68 65 79 20 69 6e 74       ** they int
32bd8 65 6e 64 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74  end to abandon t
32bd9 68 65 20 70 61 72 73 65 20 75 70 6f 6e 20 74 68  he parse upon th
32bda 65 20 66 69 72 73 74 20 73 79 6e 74 61 78 20 65  e first syntax e
32bdb 72 72 6f 72 20 73 65 65 6e 2e 0a 20 20 20 20 20  rror seen..     
32bdc 20 2a 2f 0a 20 20 20 20 20 20 79 79 5f 73 79 6e   */.      yy_syn
32bdd 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72  tax_error(yypPar
32bde 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69  ser,yymajor,yymi
32bdf 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
32be0 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79   yy_destructor(y
32be1 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45  ypParser,(YYCODE
32be2 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79  TYPE)yymajor,&yy
32be3 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
32be4 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e     yymajor = YYN
32be5 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 0a 23 65  OCODE;.      .#e
32be6 6c 73 65 20 20 2f 2a 20 59 59 45 52 52 4f 52 53  lse  /* YYERRORS
32be7 59 4d 42 4f 4c 20 69 73 20 6e 6f 74 20 64 65 66  YMBOL is not def
32be8 69 6e 65 64 20 2a 2f 0a 20 20 20 20 20 20 2f 2a  ined */.      /*
32be9 20 54 68 69 73 20 69 73 20 77 68 61 74 20 77 65   This is what we
32bea 20 64 6f 20 69 66 20 74 68 65 20 67 72 61 6d 6d   do if the gramm
32beb 61 72 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69  ar does not defi
32bec 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20 20 20  ne ERROR:.      
32bed 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 52  **.      **  * R
32bee 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 20 6d  eport an error m
32bef 65 73 73 61 67 65 2c 20 61 6e 64 20 74 68 72 6f  essage, and thro
32bf0 77 20 61 77 61 79 20 74 68 65 20 69 6e 70 75 74  w away the input
32bf1 20 74 6f 6b 65 6e 2e 0a 20 20 20 20 20 20 2a 2a   token..      **
32bf2 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 49 66 20  .      **  * If 
32bf3 74 68 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e 20  the input token 
32bf4 69 73 20 24 2c 20 74 68 65 6e 20 66 61 69 6c 20  is $, then fail 
32bf5 74 68 65 20 70 61 72 73 65 2e 0a 20 20 20 20 20  the parse..     
32bf6 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73 20   **.      ** As 
32bf7 62 65 66 6f 72 65 2c 20 73 75 62 73 65 71 75 65  before, subseque
32bf8 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  nt error message
32bf9 73 20 61 72 65 20 73 75 70 70 72 65 73 73 65 64  s are suppressed
32bfa 20 75 6e 74 69 6c 0a 20 20 20 20 20 20 2a 2a 20   until.      ** 
32bfb 74 68 72 65 65 20 69 6e 70 75 74 20 74 6f 6b 65  three input toke
32bfc 6e 73 20 68 61 76 65 20 62 65 65 6e 20 73 75 63  ns have been suc
32bfd 63 65 73 73 66 75 6c 6c 79 20 73 68 69 66 74 65  cessfully shifte
32bfe 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
32bff 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
32c00 3e 79 79 65 72 72 63 6e 74 3c 3d 30 20 29 7b 0a  >yyerrcnt<=0 ){.
32c01 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74 61          yy_synta
32c02 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73 65  x_error(yypParse
32c03 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f  r,yymajor,yymino
32c04 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d  runion);.      }
32c05 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72  .      yypParser
32c06 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a  ->yyerrcnt = 3;.
32c07 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75 63        yy_destruc
32c08 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28 59  tor(yypParser,(Y
32c09 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f  YCODETYPE)yymajo
32c0a 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29  r,&yyminorunion)
32c0b 3b 0a 20 20 20 20 20 20 69 66 28 20 79 79 65 6e  ;.      if( yyen
32c0c 64 6f 66 69 6e 70 75 74 20 29 7b 0a 20 20 20 20  dofinput ){.    
32c0d 20 20 20 20 79 79 5f 70 61 72 73 65 5f 66 61 69      yy_parse_fai
32c0e 6c 65 64 28 79 79 70 50 61 72 73 65 72 29 3b 0a  led(yypParser);.
32c0f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79        }.      yy
32c10 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45  major = YYNOCODE
32c11 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20  ;.#endif.    }. 
32c12 20 7d 77 68 69 6c 65 28 20 79 79 6d 61 6a 6f 72   }while( yymajor
32c13 21 3d 59 59 4e 4f 43 4f 44 45 20 26 26 20 79 79  !=YYNOCODE && yy
32c14 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d  pParser->yyidx>=
32c15 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  0 );.  return;.}
32c16 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
32c17 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 63  * End of parse.c
32c18 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
32c19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c1b 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
32c1c 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 6f 6b  * Begin file tok
32c1d 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  enize.c ********
32c1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c20 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
32c21 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
32c22 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
32c23 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
32c24 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
32c25 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
32c26 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
32c27 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
32c28 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
32c29 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
32c2a 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
32c2b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
32c2c 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
32c2d 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
32c2e 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
32c2f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
32c30 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
32c31 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
32c32 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
32c33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c37 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74  ********.** An t
32c38 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53 51 4c  okenizer for SQL
32c39 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
32c3a 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
32c3b 20 74 68 61 74 20 73 70 6c 69 74 73 20 61 6e 20   that splits an 
32c3c 53 51 4c 20 69 6e 70 75 74 20 73 74 72 69 6e 67  SQL input string
32c3d 20 75 70 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69   up into.** indi
32c3e 76 69 64 75 61 6c 20 74 6f 6b 65 6e 73 20 61 6e  vidual tokens an
32c3f 64 20 73 65 6e 64 73 20 74 68 6f 73 65 20 74 6f  d sends those to
32c40 6b 65 6e 73 20 6f 6e 65 2d 62 79 2d 6f 6e 65 20  kens one-by-one 
32c41 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 70  over to the.** p
32c42 61 72 73 65 72 20 66 6f 72 20 61 6e 61 6c 79 73  arser for analys
32c43 69 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  is..*/../*.** Th
32c44 65 20 63 68 61 72 4d 61 70 28 29 20 6d 61 63 72  e charMap() macr
32c45 6f 20 6d 61 70 73 20 61 6c 70 68 61 62 65 74 69  o maps alphabeti
32c46 63 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 74  c characters int
32c47 6f 20 74 68 65 69 72 0a 2a 2a 20 6c 6f 77 65 72  o their.** lower
32c48 2d 63 61 73 65 20 41 53 43 49 49 20 65 71 75 69  -case ASCII equi
32c49 76 61 6c 65 6e 74 2e 20 20 4f 6e 20 41 53 43 49  valent.  On ASCI
32c4a 49 20 6d 61 63 68 69 6e 65 73 2c 20 74 68 69 73  I machines, this
32c4b 20 69 73 20 6a 75 73 74 0a 2a 2a 20 61 6e 20 75   is just.** an u
32c4c 70 70 65 72 2d 74 6f 2d 6c 6f 77 65 72 20 63 61  pper-to-lower ca
32c4d 73 65 20 6d 61 70 2e 20 20 4f 6e 20 45 42 43 44  se map.  On EBCD
32c4e 49 43 20 6d 61 63 68 69 6e 65 73 20 77 65 20 61  IC machines we a
32c4f 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 61  lso need.** to a
32c50 64 6a 75 73 74 20 74 68 65 20 65 6e 63 6f 64 69  djust the encodi
32c51 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c 70 68 61 62  ng.  Only alphab
32c52 65 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20  etic characters 
32c53 61 6e 64 20 75 6e 64 65 72 73 63 6f 72 65 73 0a  and underscores.
32c54 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72  ** need to be tr
32c55 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a 23 69 66  anslated..*/.#if
32c56 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49  def SQLITE_ASCII
32c57 0a 23 20 64 65 66 69 6e 65 20 63 68 61 72 4d 61  .# define charMa
32c58 70 28 58 29 20 73 71 6c 69 74 65 33 55 70 70 65  p(X) sqlite3Uppe
32c59 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e  rToLower[(unsign
32c5a 65 64 20 63 68 61 72 29 58 5d 0a 23 65 6e 64 69  ed char)X].#endi
32c5b 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
32c5c 45 42 43 44 49 43 0a 23 20 64 65 66 69 6e 65 20  EBCDIC.# define 
32c5d 63 68 61 72 4d 61 70 28 58 29 20 65 62 63 64 69  charMap(X) ebcdi
32c5e 63 54 6f 41 73 63 69 69 5b 28 75 6e 73 69 67 6e  cToAscii[(unsign
32c5f 65 64 20 63 68 61 72 29 58 5d 0a 63 6f 6e 73 74  ed char)X].const
32c60 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65   unsigned char e
32c61 62 63 64 69 63 54 6f 41 73 63 69 69 5b 5d 20 3d  bcdicToAscii[] =
32c62 20 7b 0a 2f 2a 20 30 20 20 20 31 20 20 20 32 20   {./* 0   1   2 
32c63 20 20 33 20 20 20 34 20 20 20 35 20 20 20 36 20    3   4   5   6 
32c64 20 20 37 20 20 20 38 20 20 20 39 20 20 20 41 20    7   8   9   A 
32c65 20 20 42 20 20 20 43 20 20 20 44 20 20 20 45 20    B   C   D   E 
32c66 20 20 46 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c    F */.   0,  0,
32c67 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c68 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c69 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c6a 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 30 78 20    0,  0,  /* 0x 
32c6b 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c  */.   0,  0,  0,
32c6c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c6d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c6e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c6f 20 20 30 2c 20 20 2f 2a 20 31 78 20 2a 2f 0a 20    0,  /* 1x */. 
32c70 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c71 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c72 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c73 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c74 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 30 2c    /* 2x */.   0,
32c75 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c76 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c77 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c78 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
32c79 20 33 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   3x */.   0,  0,
32c7a 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c7b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c7c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c7d 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 34 78 20    0,  0,  /* 4x 
32c7e 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c  */.   0,  0,  0,
32c7f 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c80 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c81 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c82 20 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20    0,  /* 5x */. 
32c83 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c84 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c85 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c86 20 20 30 2c 20 39 35 2c 20 20 30 2c 20 20 30 2c    0, 95,  0,  0,
32c87 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 30 2c    /* 6x */.   0,
32c88 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c89 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c8a 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c8b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
32c8c 20 37 78 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c   7x */.   0, 97,
32c8d 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c   98, 99,100,101,
32c8e 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c  102,103,104,105,
32c8f 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c90 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 38 78 20    0,  0,  /* 8x 
32c91 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30 37 2c  */.   0,106,107,
32c92 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c  108,109,110,111,
32c93 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c  112,113,114,  0,
32c94 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c95 20 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a 20    0,  /* 9x */. 
32c96 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c    0,  0,115,116,
32c97 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c  117,118,119,120,
32c98 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c  121,122,  0,  0,
32c99 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c9a 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 30 2c    /* Ax */.   0,
32c9b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c9c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c9d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32c9e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
32c9f 20 42 78 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c   Bx */.   0, 97,
32ca0 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c   98, 99,100,101,
32ca1 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c  102,103,104,105,
32ca2 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32ca3 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 43 78 20    0,  0,  /* Cx 
32ca4 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30 37 2c  */.   0,106,107,
32ca5 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c  108,109,110,111,
32ca6 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c  112,113,114,  0,
32ca7 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32ca8 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20    0,  /* Dx */. 
32ca9 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c    0,  0,115,116,
32caa 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c  117,118,119,120,
32cab 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c  121,122,  0,  0,
32cac 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32cad 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 30 2c    /* Ex */.   0,
32cae 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32caf 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32cb0 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
32cb1 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
32cb2 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66   Fx */.};.#endif
32cb3 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
32cb4 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 20 66  te3KeywordCode f
32cb5 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73 20 75 70  unction looks up
32cb6 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74   an identifier t
32cb7 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 0a 2a  o determine if.*
32cb8 2a 20 69 74 20 69 73 20 61 20 6b 65 79 77 6f 72  * it is a keywor
32cb9 64 2e 20 20 49 66 20 69 74 20 69 73 20 61 20 6b  d.  If it is a k
32cba 65 79 77 6f 72 64 2c 20 74 68 65 20 74 6f 6b 65  eyword, the toke
32cbb 6e 20 63 6f 64 65 20 6f 66 20 74 68 61 74 20 6b  n code of that k
32cbc 65 79 77 6f 72 64 20 69 73 20 0a 2a 2a 20 72 65  eyword is .** re
32cbd 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 20  turned.  If the 
32cbe 69 6e 70 75 74 20 69 73 20 6e 6f 74 20 61 20 6b  input is not a k
32cbf 65 79 77 6f 72 64 2c 20 54 4b 5f 49 44 20 69 73  eyword, TK_ID is
32cc0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
32cc1 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
32cc2 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74  ion of this rout
32cc3 69 6e 65 20 77 61 73 20 67 65 6e 65 72 61 74 65  ine was generate
32cc4 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d 2c 0a  d by a program,.
32cc5 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68  ** mkkeywordhash
32cc6 2e 68 2c 20 6c 6f 63 61 74 65 64 20 69 6e 20 74  .h, located in t
32cc7 68 65 20 74 6f 6f 6c 20 73 75 62 64 69 72 65 63  he tool subdirec
32cc8 74 6f 72 79 20 6f 66 20 74 68 65 20 64 69 73 74  tory of the dist
32cc9 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65  ribution..** The
32cca 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20 6d   output of the m
32ccb 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63 20 70  kkeywordhash.c p
32ccc 72 6f 67 72 61 6d 20 69 73 20 77 72 69 74 74 65  rogram is writte
32ccd 6e 20 69 6e 74 6f 20 61 20 66 69 6c 65 0a 2a 2a  n into a file.**
32cce 20 6e 61 6d 65 64 20 6b 65 79 77 6f 72 64 68 61   named keywordha
32ccf 73 68 2e 68 20 61 6e 64 20 74 68 65 6e 20 69 6e  sh.h and then in
32cd0 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 69 73  cluded into this
32cd1 20 73 6f 75 72 63 65 20 66 69 6c 65 20 62 79 0a   source file by.
32cd2 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20  ** the #include 
32cd3 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  below..*/./*****
32cd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
32cd5 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20  e keywordhash.h 
32cd6 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
32cd7 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a   tokenize.c ****
32cd8 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
32cd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
32cda 66 69 6c 65 20 6b 65 79 77 6f 72 64 68 61 73 68  file keywordhash
32cdb 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
32cdc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32cdd 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
32cde 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
32cdf 69 6e 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ins automaticall
32ce0 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65  y generated code
32ce1 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68   ******.**.** Th
32ce2 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
32ce3 69 6c 65 20 68 61 73 20 62 65 65 6e 20 61 75 74  ile has been aut
32ce4 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72  omatically gener
32ce5 61 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a 20 20 20  ated by.**.**   
32ce6 73 71 6c 69 74 65 2f 74 6f 6f 6c 2f 6d 6b 6b 65  sqlite/tool/mkke
32ce7 79 77 6f 72 64 68 61 73 68 2e 63 0a 2a 2a 0a 2a  ywordhash.c.**.*
32ce8 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
32ce9 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
32cea 74 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68  ts a function th
32ceb 61 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  at determines wh
32cec 65 74 68 65 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20  ether.** or not 
32ced 61 20 67 69 76 65 6e 20 69 64 65 6e 74 69 66 69  a given identifi
32cee 65 72 20 69 73 20 72 65 61 6c 6c 79 20 61 6e 20  er is really an 
32cef 53 51 4c 20 6b 65 79 77 6f 72 64 2e 20 20 54 68  SQL keyword.  Th
32cf0 65 20 73 61 6d 65 20 74 68 69 6e 67 0a 2a 2a 20  e same thing.** 
32cf1 6d 69 67 68 74 20 62 65 20 69 6d 70 6c 65 6d 65  might be impleme
32cf2 6e 74 65 64 20 6d 6f 72 65 20 64 69 72 65 63 74  nted more direct
32cf3 6c 79 20 75 73 69 6e 67 20 61 20 68 61 6e 64 2d  ly using a hand-
32cf4 77 72 69 74 74 65 6e 20 68 61 73 68 20 74 61 62  written hash tab
32cf5 6c 65 2e 0a 2a 2a 20 42 75 74 20 62 79 20 75 73  le..** But by us
32cf6 69 6e 67 20 74 68 69 73 20 61 75 74 6f 6d 61 74  ing this automat
32cf7 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
32cf8 20 63 6f 64 65 2c 20 74 68 65 20 73 69 7a 65 20   code, the size 
32cf9 6f 66 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 69  of the code.** i
32cfa 73 20 73 75 62 73 74 61 6e 74 69 61 6c 6c 79 20  s substantially 
32cfb 72 65 64 75 63 65 64 2e 20 20 54 68 69 73 20 69  reduced.  This i
32cfc 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20  s important for 
32cfd 65 6d 62 65 64 64 65 64 20 61 70 70 6c 69 63 61  embedded applica
32cfe 74 69 6f 6e 73 0a 2a 2a 20 6f 6e 20 70 6c 61 74  tions.** on plat
32cff 66 6f 72 6d 73 20 77 69 74 68 20 6c 69 6d 69 74  forms with limit
32d00 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 2f 2a  ed memory..*/./*
32d01 20 48 61 73 68 20 73 63 6f 72 65 3a 20 31 37 35   Hash score: 175
32d02 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6b   */.static int k
32d03 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74  eywordCode(const
32d04 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
32d05 7b 0a 20 20 2f 2a 20 7a 54 65 78 74 5b 5d 20 65  {.  /* zText[] e
32d06 6e 63 6f 64 65 73 20 38 31 31 20 62 79 74 65 73  ncodes 811 bytes
32d07 20 6f 66 20 6b 65 79 77 6f 72 64 73 20 69 6e 20   of keywords in 
32d08 35 34 31 20 62 79 74 65 73 20 2a 2f 0a 20 20 2f  541 bytes */.  /
32d09 2a 20 20 20 52 45 49 4e 44 45 58 45 44 45 53 43  *   REINDEXEDESC
32d0a 41 50 45 41 43 48 45 43 4b 45 59 42 45 46 4f 52  APEACHECKEYBEFOR
32d0b 45 49 47 4e 4f 52 45 47 45 58 50 4c 41 49 4e 53  EIGNOREGEXPLAINS
32d0c 54 45 41 44 44 41 54 41 42 41 53 45 4c 45 43 54  TEADDATABASELECT
32d0d 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20         */.  /*  
32d0e 20 41 42 4c 45 46 54 48 45 4e 44 45 46 45 52 52   ABLEFTHENDEFERR
32d0f 41 42 4c 45 4c 53 45 58 43 45 50 54 52 41 4e 53  ABLELSEXCEPTRANS
32d10 41 43 54 49 4f 4e 41 54 55 52 41 4c 54 45 52 41  ACTIONATURALTERA
32d11 49 53 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  ISEXCLUSIVE     
32d12 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 58 49      */.  /*   XI
32d13 53 54 53 41 56 45 50 4f 49 4e 54 45 52 53 45 43  STSAVEPOINTERSEC
32d14 54 52 49 47 47 45 52 45 46 45 52 45 4e 43 45 53  TRIGGEREFERENCES
32d15 43 4f 4e 53 54 52 41 49 4e 54 4f 46 46 53 45 54  CONSTRAINTOFFSET
32d16 45 4d 50 4f 52 41 52 59 20 20 20 20 20 20 20 20  EMPORARY        
32d17 20 2a 2f 0a 20 20 2f 2a 20 20 20 55 4e 49 51 55   */.  /*   UNIQU
32d18 45 52 59 41 54 54 41 43 48 41 56 49 4e 47 52 4f  ERYATTACHAVINGRO
32d19 55 50 44 41 54 45 42 45 47 49 4e 4e 45 52 45 4c  UPDATEBEGINNEREL
32d1a 45 41 53 45 42 45 54 57 45 45 4e 4f 54 4e 55 4c  EASEBETWEENOTNUL
32d1b 4c 49 4b 45 20 20 20 20 20 20 20 20 20 20 2a 2f  LIKE          */
32d1c 0a 20 20 2f 2a 20 20 20 43 41 53 43 41 44 45 4c  .  /*   CASCADEL
32d1d 45 54 45 43 41 53 45 43 4f 4c 4c 41 54 45 43 52  ETECASECOLLATECR
32d1e 45 41 54 45 43 55 52 52 45 4e 54 5f 44 41 54 45  EATECURRENT_DATE
32d1f 44 45 54 41 43 48 49 4d 4d 45 44 49 41 54 45 4a  DETACHIMMEDIATEJ
32d20 4f 49 4e 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  OIN        */.  
32d21 2f 2a 20 20 20 53 45 52 54 4d 41 54 43 48 50 4c  /*   SERTMATCHPL
32d22 41 4e 41 4c 59 5a 45 50 52 41 47 4d 41 42 4f 52  ANALYZEPRAGMABOR
32d23 54 56 41 4c 55 45 53 56 49 52 54 55 41 4c 49 4d  TVALUESVIRTUALIM
32d24 49 54 57 48 45 4e 57 48 45 52 45 4e 41 4d 45 20  ITWHENWHERENAME 
32d25 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20          */.  /* 
32d26 20 20 41 46 54 45 52 45 50 4c 41 43 45 41 4e 44    AFTEREPLACEAND
32d27 45 46 41 55 4c 54 41 55 54 4f 49 4e 43 52 45 4d  EFAULTAUTOINCREM
32d28 45 4e 54 43 41 53 54 43 4f 4c 55 4d 4e 43 4f 4d  ENTCASTCOLUMNCOM
32d29 4d 49 54 43 4f 4e 46 4c 49 43 54 43 52 4f 53 53  MITCONFLICTCROSS
32d2a 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 43       */.  /*   C
32d2b 55 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50  URRENT_TIMESTAMP
32d2c 52 49 4d 41 52 59 44 45 46 45 52 52 45 44 49 53  RIMARYDEFERREDIS
32d2d 54 49 4e 43 54 44 52 4f 50 46 41 49 4c 46 52 4f  TINCTDROPFAILFRO
32d2e 4d 46 55 4c 4c 47 4c 4f 42 59 49 46 20 20 20 20  MFULLGLOBYIF    
32d2f 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 49 53 4e 55    */.  /*   ISNU
32d30 4c 4c 4f 52 44 45 52 45 53 54 52 49 43 54 4f 55  LLORDERESTRICTOU
32d31 54 45 52 49 47 48 54 52 4f 4c 4c 42 41 43 4b 52  TERIGHTROLLBACKR
32d32 4f 57 55 4e 49 4f 4e 55 53 49 4e 47 56 41 43 55  OWUNIONUSINGVACU
32d33 55 4d 56 49 45 57 20 20 20 20 20 20 20 20 20 2a  UMVIEW         *
32d34 2f 0a 20 20 2f 2a 20 20 20 49 4e 49 54 49 41 4c  /.  /*   INITIAL
32d35 4c 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LY              
32d36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d38 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
32d39 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
32d3a 61 72 20 7a 54 65 78 74 5b 35 34 30 5d 20 3d 20  ar zText[540] = 
32d3b 7b 0a 20 20 20 20 27 52 27 2c 27 45 27 2c 27 49  {.    'R','E','I
32d3c 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 58  ','N','D','E','X
32d3d 27 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c 27 53  ','E','D','E','S
32d3e 27 2c 27 43 27 2c 27 41 27 2c 27 50 27 2c 27 45  ','C','A','P','E
32d3f 27 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c 0a 20  ','A','C','H',. 
32d40 20 20 20 27 45 27 2c 27 43 27 2c 27 4b 27 2c 27     'E','C','K','
32d41 45 27 2c 27 59 27 2c 27 42 27 2c 27 45 27 2c 27  E','Y','B','E','
32d42 46 27 2c 27 4f 27 2c 27 52 27 2c 27 45 27 2c 27  F','O','R','E','
32d43 49 27 2c 27 47 27 2c 27 4e 27 2c 27 4f 27 2c 27  I','G','N','O','
32d44 52 27 2c 27 45 27 2c 27 47 27 2c 0a 20 20 20 20  R','E','G',.    
32d45 27 45 27 2c 27 58 27 2c 27 50 27 2c 27 4c 27 2c  'E','X','P','L',
32d46 27 41 27 2c 27 49 27 2c 27 4e 27 2c 27 53 27 2c  'A','I','N','S',
32d47 27 54 27 2c 27 45 27 2c 27 41 27 2c 27 44 27 2c  'T','E','A','D',
32d48 27 44 27 2c 27 41 27 2c 27 54 27 2c 27 41 27 2c  'D','A','T','A',
32d49 27 42 27 2c 27 41 27 2c 0a 20 20 20 20 27 53 27  'B','A',.    'S'
32d4a 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27 43 27  ,'E','L','E','C'
32d4b 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c 27 4c 27  ,'T','A','B','L'
32d4c 2c 27 45 27 2c 27 46 27 2c 27 54 27 2c 27 48 27  ,'E','F','T','H'
32d4d 2c 27 45 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27  ,'E','N','D','E'
32d4e 2c 27 46 27 2c 0a 20 20 20 20 27 45 27 2c 27 52  ,'F',.    'E','R
32d4f 27 2c 27 52 27 2c 27 41 27 2c 27 42 27 2c 27 4c  ','R','A','B','L
32d50 27 2c 27 45 27 2c 27 4c 27 2c 27 53 27 2c 27 45  ','E','L','S','E
32d51 27 2c 27 58 27 2c 27 43 27 2c 27 45 27 2c 27 50  ','X','C','E','P
32d52 27 2c 27 54 27 2c 27 52 27 2c 27 41 27 2c 27 4e  ','T','R','A','N
32d53 27 2c 0a 20 20 20 20 27 53 27 2c 27 41 27 2c 27  ',.    'S','A','
32d54 43 27 2c 27 54 27 2c 27 49 27 2c 27 4f 27 2c 27  C','T','I','O','
32d55 4e 27 2c 27 41 27 2c 27 54 27 2c 27 55 27 2c 27  N','A','T','U','
32d56 52 27 2c 27 41 27 2c 27 4c 27 2c 27 54 27 2c 27  R','A','L','T','
32d57 45 27 2c 27 52 27 2c 27 41 27 2c 27 49 27 2c 0a  E','R','A','I',.
32d58 20 20 20 20 27 53 27 2c 27 45 27 2c 27 58 27 2c      'S','E','X',
32d59 27 43 27 2c 27 4c 27 2c 27 55 27 2c 27 53 27 2c  'C','L','U','S',
32d5a 27 49 27 2c 27 56 27 2c 27 45 27 2c 27 58 27 2c  'I','V','E','X',
32d5b 27 49 27 2c 27 53 27 2c 27 54 27 2c 27 53 27 2c  'I','S','T','S',
32d5c 27 41 27 2c 27 56 27 2c 27 45 27 2c 0a 20 20 20  'A','V','E',.   
32d5d 20 27 50 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27   'P','O','I','N'
32d5e 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 53 27  ,'T','E','R','S'
32d5f 2c 27 45 27 2c 27 43 27 2c 27 54 27 2c 27 52 27  ,'E','C','T','R'
32d60 2c 27 49 27 2c 27 47 27 2c 27 47 27 2c 27 45 27  ,'I','G','G','E'
32d61 2c 27 52 27 2c 27 45 27 2c 0a 20 20 20 20 27 46  ,'R','E',.    'F
32d62 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4e  ','E','R','E','N
32d63 27 2c 27 43 27 2c 27 45 27 2c 27 53 27 2c 27 43  ','C','E','S','C
32d64 27 2c 27 4f 27 2c 27 4e 27 2c 27 53 27 2c 27 54  ','O','N','S','T
32d65 27 2c 27 52 27 2c 27 41 27 2c 27 49 27 2c 27 4e  ','R','A','I','N
32d66 27 2c 27 54 27 2c 0a 20 20 20 20 27 4f 27 2c 27  ','T',.    'O','
32d67 46 27 2c 27 46 27 2c 27 53 27 2c 27 45 27 2c 27  F','F','S','E','
32d68 54 27 2c 27 45 27 2c 27 4d 27 2c 27 50 27 2c 27  T','E','M','P','
32d69 4f 27 2c 27 52 27 2c 27 41 27 2c 27 52 27 2c 27  O','R','A','R','
32d6a 59 27 2c 27 55 27 2c 27 4e 27 2c 27 49 27 2c 27  Y','U','N','I','
32d6b 51 27 2c 0a 20 20 20 20 27 55 27 2c 27 45 27 2c  Q',.    'U','E',
32d6c 27 52 27 2c 27 59 27 2c 27 41 27 2c 27 54 27 2c  'R','Y','A','T',
32d6d 27 54 27 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c  'T','A','C','H',
32d6e 27 41 27 2c 27 56 27 2c 27 49 27 2c 27 4e 27 2c  'A','V','I','N',
32d6f 27 47 27 2c 27 52 27 2c 27 4f 27 2c 27 55 27 2c  'G','R','O','U',
32d70 0a 20 20 20 20 27 50 27 2c 27 44 27 2c 27 41 27  .    'P','D','A'
32d71 2c 27 54 27 2c 27 45 27 2c 27 42 27 2c 27 45 27  ,'T','E','B','E'
32d72 2c 27 47 27 2c 27 49 27 2c 27 4e 27 2c 27 4e 27  ,'G','I','N','N'
32d73 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4c 27  ,'E','R','E','L'
32d74 2c 27 45 27 2c 27 41 27 2c 27 53 27 2c 0a 20 20  ,'E','A','S',.  
32d75 20 20 27 45 27 2c 27 42 27 2c 27 45 27 2c 27 54    'E','B','E','T
32d76 27 2c 27 57 27 2c 27 45 27 2c 27 45 27 2c 27 4e  ','W','E','E','N
32d77 27 2c 27 4f 27 2c 27 54 27 2c 27 4e 27 2c 27 55  ','O','T','N','U
32d78 27 2c 27 4c 27 2c 27 4c 27 2c 27 49 27 2c 27 4b  ','L','L','I','K
32d79 27 2c 27 45 27 2c 27 43 27 2c 0a 20 20 20 20 27  ','E','C',.    '
32d7a 41 27 2c 27 53 27 2c 27 43 27 2c 27 41 27 2c 27  A','S','C','A','
32d7b 44 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27  D','E','L','E','
32d7c 54 27 2c 27 45 27 2c 27 43 27 2c 27 41 27 2c 27  T','E','C','A','
32d7d 53 27 2c 27 45 27 2c 27 43 27 2c 27 4f 27 2c 27  S','E','C','O','
32d7e 4c 27 2c 27 4c 27 2c 0a 20 20 20 20 27 41 27 2c  L','L',.    'A',
32d7f 27 54 27 2c 27 45 27 2c 27 43 27 2c 27 52 27 2c  'T','E','C','R',
32d80 27 45 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c  'E','A','T','E',
32d81 27 43 27 2c 27 55 27 2c 27 52 27 2c 27 52 27 2c  'C','U','R','R',
32d82 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c  'E','N','T','_',
32d83 27 44 27 2c 0a 20 20 20 20 27 41 27 2c 27 54 27  'D',.    'A','T'
32d84 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c 27 54 27  ,'E','D','E','T'
32d85 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c 27 49 27  ,'A','C','H','I'
32d86 2c 27 4d 27 2c 27 4d 27 2c 27 45 27 2c 27 44 27  ,'M','M','E','D'
32d87 2c 27 49 27 2c 27 41 27 2c 27 54 27 2c 27 45 27  ,'I','A','T','E'
32d88 2c 0a 20 20 20 20 27 4a 27 2c 27 4f 27 2c 27 49  ,.    'J','O','I
32d89 27 2c 27 4e 27 2c 27 53 27 2c 27 45 27 2c 27 52  ','N','S','E','R
32d8a 27 2c 27 54 27 2c 27 4d 27 2c 27 41 27 2c 27 54  ','T','M','A','T
32d8b 27 2c 27 43 27 2c 27 48 27 2c 27 50 27 2c 27 4c  ','C','H','P','L
32d8c 27 2c 27 41 27 2c 27 4e 27 2c 27 41 27 2c 0a 20  ','A','N','A',. 
32d8d 20 20 20 27 4c 27 2c 27 59 27 2c 27 5a 27 2c 27     'L','Y','Z','
32d8e 45 27 2c 27 50 27 2c 27 52 27 2c 27 41 27 2c 27  E','P','R','A','
32d8f 47 27 2c 27 4d 27 2c 27 41 27 2c 27 42 27 2c 27  G','M','A','B','
32d90 4f 27 2c 27 52 27 2c 27 54 27 2c 27 56 27 2c 27  O','R','T','V','
32d91 41 27 2c 27 4c 27 2c 27 55 27 2c 0a 20 20 20 20  A','L','U',.    
32d92 27 45 27 2c 27 53 27 2c 27 56 27 2c 27 49 27 2c  'E','S','V','I',
32d93 27 52 27 2c 27 54 27 2c 27 55 27 2c 27 41 27 2c  'R','T','U','A',
32d94 27 4c 27 2c 27 49 27 2c 27 4d 27 2c 27 49 27 2c  'L','I','M','I',
32d95 27 54 27 2c 27 57 27 2c 27 48 27 2c 27 45 27 2c  'T','W','H','E',
32d96 27 4e 27 2c 27 57 27 2c 0a 20 20 20 20 27 48 27  'N','W',.    'H'
32d97 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27  ,'E','R','E','N'
32d98 2c 27 41 27 2c 27 4d 27 2c 27 45 27 2c 27 41 27  ,'A','M','E','A'
32d99 2c 27 46 27 2c 27 54 27 2c 27 45 27 2c 27 52 27  ,'F','T','E','R'
32d9a 2c 27 45 27 2c 27 50 27 2c 27 4c 27 2c 27 41 27  ,'E','P','L','A'
32d9b 2c 27 43 27 2c 0a 20 20 20 20 27 45 27 2c 27 41  ,'C',.    'E','A
32d9c 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 46  ','N','D','E','F
32d9d 27 2c 27 41 27 2c 27 55 27 2c 27 4c 27 2c 27 54  ','A','U','L','T
32d9e 27 2c 27 41 27 2c 27 55 27 2c 27 54 27 2c 27 4f  ','A','U','T','O
32d9f 27 2c 27 49 27 2c 27 4e 27 2c 27 43 27 2c 27 52  ','I','N','C','R
32da0 27 2c 0a 20 20 20 20 27 45 27 2c 27 4d 27 2c 27  ',.    'E','M','
32da1 45 27 2c 27 4e 27 2c 27 54 27 2c 27 43 27 2c 27  E','N','T','C','
32da2 41 27 2c 27 53 27 2c 27 54 27 2c 27 43 27 2c 27  A','S','T','C','
32da3 4f 27 2c 27 4c 27 2c 27 55 27 2c 27 4d 27 2c 27  O','L','U','M','
32da4 4e 27 2c 27 43 27 2c 27 4f 27 2c 27 4d 27 2c 0a  N','C','O','M',.
32da5 20 20 20 20 27 4d 27 2c 27 49 27 2c 27 54 27 2c      'M','I','T',
32da6 27 43 27 2c 27 4f 27 2c 27 4e 27 2c 27 46 27 2c  'C','O','N','F',
32da7 27 4c 27 2c 27 49 27 2c 27 43 27 2c 27 54 27 2c  'L','I','C','T',
32da8 27 43 27 2c 27 52 27 2c 27 4f 27 2c 27 53 27 2c  'C','R','O','S',
32da9 27 53 27 2c 27 43 27 2c 27 55 27 2c 0a 20 20 20  'S','C','U',.   
32daa 20 27 52 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27   'R','R','E','N'
32dab 2c 27 54 27 2c 27 5f 27 2c 27 54 27 2c 27 49 27  ,'T','_','T','I'
32dac 2c 27 4d 27 2c 27 45 27 2c 27 53 27 2c 27 54 27  ,'M','E','S','T'
32dad 2c 27 41 27 2c 27 4d 27 2c 27 50 27 2c 27 52 27  ,'A','M','P','R'
32dae 2c 27 49 27 2c 27 4d 27 2c 0a 20 20 20 20 27 41  ,'I','M',.    'A
32daf 27 2c 27 52 27 2c 27 59 27 2c 27 44 27 2c 27 45  ','R','Y','D','E
32db0 27 2c 27 46 27 2c 27 45 27 2c 27 52 27 2c 27 52  ','F','E','R','R
32db1 27 2c 27 45 27 2c 27 44 27 2c 27 49 27 2c 27 53  ','E','D','I','S
32db2 27 2c 27 54 27 2c 27 49 27 2c 27 4e 27 2c 27 43  ','T','I','N','C
32db3 27 2c 27 54 27 2c 0a 20 20 20 20 27 44 27 2c 27  ','T',.    'D','
32db4 52 27 2c 27 4f 27 2c 27 50 27 2c 27 46 27 2c 27  R','O','P','F','
32db5 41 27 2c 27 49 27 2c 27 4c 27 2c 27 46 27 2c 27  A','I','L','F','
32db6 52 27 2c 27 4f 27 2c 27 4d 27 2c 27 46 27 2c 27  R','O','M','F','
32db7 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 47 27 2c 27  U','L','L','G','
32db8 4c 27 2c 0a 20 20 20 20 27 4f 27 2c 27 42 27 2c  L',.    'O','B',
32db9 27 59 27 2c 27 49 27 2c 27 46 27 2c 27 49 27 2c  'Y','I','F','I',
32dba 27 53 27 2c 27 4e 27 2c 27 55 27 2c 27 4c 27 2c  'S','N','U','L',
32dbb 27 4c 27 2c 27 4f 27 2c 27 52 27 2c 27 44 27 2c  'L','O','R','D',
32dbc 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 53 27 2c  'E','R','E','S',
32dbd 0a 20 20 20 20 27 54 27 2c 27 52 27 2c 27 49 27  .    'T','R','I'
32dbe 2c 27 43 27 2c 27 54 27 2c 27 4f 27 2c 27 55 27  ,'C','T','O','U'
32dbf 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 49 27  ,'T','E','R','I'
32dc0 2c 27 47 27 2c 27 48 27 2c 27 54 27 2c 27 52 27  ,'G','H','T','R'
32dc1 2c 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20  ,'O','L','L',.  
32dc2 20 20 27 42 27 2c 27 41 27 2c 27 43 27 2c 27 4b    'B','A','C','K
32dc3 27 2c 27 52 27 2c 27 4f 27 2c 27 57 27 2c 27 55  ','R','O','W','U
32dc4 27 2c 27 4e 27 2c 27 49 27 2c 27 4f 27 2c 27 4e  ','N','I','O','N
32dc5 27 2c 27 55 27 2c 27 53 27 2c 27 49 27 2c 27 4e  ','U','S','I','N
32dc6 27 2c 27 47 27 2c 27 56 27 2c 0a 20 20 20 20 27  ','G','V',.    '
32dc7 41 27 2c 27 43 27 2c 27 55 27 2c 27 55 27 2c 27  A','C','U','U','
32dc8 4d 27 2c 27 56 27 2c 27 49 27 2c 27 45 27 2c 27  M','V','I','E','
32dc9 57 27 2c 27 49 27 2c 27 4e 27 2c 27 49 27 2c 27  W','I','N','I','
32dca 54 27 2c 27 49 27 2c 27 41 27 2c 27 4c 27 2c 27  T','I','A','L','
32dcb 4c 27 2c 27 59 27 2c 0a 20 20 7d 3b 0a 20 20 73  L','Y',.  };.  s
32dcc 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
32dcd 67 6e 65 64 20 63 68 61 72 20 61 48 61 73 68 5b  gned char aHash[
32dce 31 32 37 5d 20 3d 20 7b 0a 20 20 20 20 20 20 37  127] = {.      7
32dcf 32 2c 20 31 30 31 2c 20 31 31 34 2c 20 20 37 30  2, 101, 114,  70
32dd0 2c 20 20 20 30 2c 20 20 34 35 2c 20 20 20 30 2c  ,   0,  45,   0,
32dd1 20 20 20 30 2c 20 20 37 38 2c 20 20 20 30 2c 20     0,  78,   0, 
32dd2 20 37 33 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20   73,   0,   0,. 
32dd3 20 20 20 20 20 34 32 2c 20 20 31 32 2c 20 20 37       42,  12,  7
32dd4 34 2c 20 20 31 35 2c 20 20 20 30 2c 20 31 31 33  4,  15,   0, 113
32dd5 2c 20 20 38 31 2c 20 20 35 30 2c 20 31 30 38 2c  ,  81,  50, 108,
32dd6 20 20 20 30 2c 20 20 31 39 2c 20 20 20 30 2c 20     0,  19,   0, 
32dd7 20 20 30 2c 0a 20 20 20 20 20 31 31 38 2c 20 20    0,.     118,  
32dd8 20 30 2c 20 31 31 36 2c 20 31 31 31 2c 20 20 20   0, 116, 111,   
32dd9 30 2c 20 20 32 32 2c 20 20 38 39 2c 20 20 20 30  0,  22,  89,   0
32dda 2c 20 20 20 39 2c 20 20 20 30 2c 20 20 20 30 2c  ,   9,   0,   0,
32ddb 20 20 36 36 2c 20 20 36 37 2c 0a 20 20 20 20 20    66,  67,.     
32ddc 20 20 30 2c 20 20 36 35 2c 20 20 20 36 2c 20 20    0,  65,   6,  
32ddd 20 30 2c 20 20 34 38 2c 20 20 38 36 2c 20 20 39   0,  48,  86,  9
32dde 38 2c 20 20 20 30 2c 20 31 31 35 2c 20 20 39 37  8,   0, 115,  97
32ddf 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 34 34 2c  ,   0,   0,  44,
32de0 0a 20 20 20 20 20 20 20 30 2c 20 20 39 39 2c 20  .       0,  99, 
32de1 20 32 34 2c 20 20 20 30 2c 20 20 31 37 2c 20 20   24,   0,  17,  
32de2 20 30 2c 20 31 31 39 2c 20 20 34 39 2c 20 20 32   0, 119,  49,  2
32de3 33 2c 20 20 20 30 2c 20 20 20 35 2c 20 31 30 36  3,   0,   5, 106
32de4 2c 20 20 32 35 2c 0a 20 20 20 20 20 20 39 32 2c  ,  25,.      92,
32de5 20 20 20 30 2c 20 20 20 30 2c 20 31 32 31 2c 20     0,   0, 121, 
32de6 31 30 32 2c 20 20 35 36 2c 20 31 32 30 2c 20 20  102,  56, 120,  
32de7 35 33 2c 20 20 32 38 2c 20 20 35 31 2c 20 20 20  53,  28,  51,   
32de8 30 2c 20 20 38 37 2c 20 20 20 30 2c 0a 20 20 20  0,  87,   0,.   
32de9 20 20 20 39 36 2c 20 20 32 36 2c 20 20 20 30 2c     96,  26,   0,
32dea 20 20 39 35 2c 20 20 20 30 2c 20 20 20 30 2c 20    95,   0,   0, 
32deb 20 20 30 2c 20 20 39 31 2c 20 20 38 38 2c 20 20    0,  91,  88,  
32dec 39 33 2c 20 20 38 34 2c 20 31 30 35 2c 20 20 31  93,  84, 105,  1
32ded 34 2c 0a 20 20 20 20 20 20 33 39 2c 20 31 30 34  4,.      39, 104
32dee 2c 20 20 20 30 2c 20 20 37 37 2c 20 20 20 30 2c  ,   0,  77,   0,
32def 20 20 31 38 2c 20 20 38 35 2c 20 31 30 37 2c 20    18,  85, 107, 
32df0 20 33 32 2c 20 20 20 30 2c 20 31 31 37 2c 20 20   32,   0, 117,  
32df1 37 36 2c 20 31 30 39 2c 0a 20 20 20 20 20 20 35  76, 109,.      5
32df2 38 2c 20 20 34 36 2c 20 20 38 30 2c 20 20 20 30  8,  46,  80,   0
32df3 2c 20 20 20 30 2c 20 20 39 30 2c 20 20 34 30 2c  ,   0,  90,  40,
32df4 20 20 20 30 2c 20 31 31 32 2c 20 20 20 30 2c 20     0, 112,   0, 
32df5 20 33 36 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20   36,   0,   0,. 
32df6 20 20 20 20 20 32 39 2c 20 20 20 30 2c 20 20 38       29,   0,  8
32df7 32 2c 20 20 35 39 2c 20 20 36 30 2c 20 20 20 30  2,  59,  60,   0
32df8 2c 20 20 32 30 2c 20 20 35 37 2c 20 20 20 30 2c  ,  20,  57,   0,
32df9 20 20 35 32 2c 0a 20 20 7d 3b 0a 20 20 73 74 61    52,.  };.  sta
32dfa 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
32dfb 65 64 20 63 68 61 72 20 61 4e 65 78 74 5b 31 32  ed char aNext[12
32dfc 31 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 30 2c  1] = {.       0,
32dfd 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32dfe 20 20 34 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    4,   0,   0,  
32dff 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
32e00 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  0,   0,   0,.   
32e01 20 20 20 20 30 2c 20 20 20 32 2c 20 20 20 30 2c      0,   2,   0,
32e02 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32e03 20 20 30 2c 20 20 20 30 2c 20 20 31 33 2c 20 20    0,   0,  13,  
32e04 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
32e05 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 37  0,.       0,   7
32e06 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
32e07 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32e08 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32e09 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20   0,   0,.       
32e0a 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
32e0b 2c 20 20 33 33 2c 20 20 20 30 2c 20 20 32 31 2c  ,  33,   0,  21,
32e0c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32e0d 20 34 33 2c 20 20 20 33 2c 20 20 34 37 2c 0a 20   43,   3,  47,. 
32e0e 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20        0,   0,   
32e0f 30 2c 20 20 20 30 2c 20 20 33 30 2c 20 20 20 30  0,   0,  30,   0
32e10 2c 20 20 35 34 2c 20 20 20 30 2c 20 20 33 38 2c  ,  54,   0,  38,
32e11 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32e12 20 20 31 2c 0a 20 20 20 20 20 20 36 32 2c 20 20    1,.      62,  
32e13 20 30 2c 20 20 20 30 2c 20 20 36 33 2c 20 20 20   0,   0,  63,   
32e14 30 2c 20 20 34 31 2c 20 20 20 30 2c 20 20 20 30  0,  41,   0,   0
32e15 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
32e16 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
32e17 20 36 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   61,   0,   0,  
32e18 20 30 2c 20 20 20 30 2c 20 20 33 31 2c 20 20 35   0,   0,  31,  5
32e19 35 2c 20 20 31 36 2c 20 20 33 34 2c 20 20 31 30  5,  16,  34,  10
32e1a 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
32e1b 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20  .       0,   0, 
32e1c 20 20 30 2c 20 20 20 30 2c 20 20 31 31 2c 20 20    0,   0,  11,  
32e1d 36 38 2c 20 20 37 35 2c 20 20 20 30 2c 20 20 20  68,  75,   0,   
32e1e 38 2c 20 20 20 30 2c 20 31 30 30 2c 20 20 39 34  8,   0, 100,  94
32e1f 2c 20 20 20 30 2c 0a 20 20 20 20 20 31 30 33 2c  ,   0,.     103,
32e20 20 20 20 30 2c 20 20 38 33 2c 20 20 20 30 2c 20     0,  83,   0, 
32e21 20 37 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 31   71,   0,   0, 1
32e22 31 30 2c 20 20 32 37 2c 20 20 33 37 2c 20 20 36  10,  27,  37,  6
32e23 39 2c 20 20 37 39 2c 20 20 20 30 2c 0a 20 20 20  9,  79,   0,.   
32e24 20 20 20 33 35 2c 20 20 36 34 2c 20 20 20 30 2c     35,  64,   0,
32e25 20 20 20 30 2c 0a 20 20 7d 3b 0a 20 20 73 74 61     0,.  };.  sta
32e26 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
32e27 65 64 20 63 68 61 72 20 61 4c 65 6e 5b 31 32 31  ed char aLen[121
32e28 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 37 2c 20  ] = {.       7, 
32e29 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c 20 20    7,   5,   4,  
32e2a 20 36 2c 20 20 20 34 2c 20 20 20 35 2c 20 20 20   6,   4,   5,   
32e2b 33 2c 20 20 20 36 2c 20 20 20 37 2c 20 20 20 33  3,   6,   7,   3
32e2c 2c 20 20 20 36 2c 20 20 20 36 2c 0a 20 20 20 20  ,   6,   6,.    
32e2d 20 20 20 37 2c 20 20 20 37 2c 20 20 20 33 2c 20     7,   7,   3, 
32e2e 20 20 38 2c 20 20 20 32 2c 20 20 20 36 2c 20 20    8,   2,   6,  
32e2f 20 35 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20   5,   4,   4,   
32e30 33 2c 20 20 31 30 2c 20 20 20 34 2c 20 20 20 36  3,  10,   4,   6
32e31 2c 0a 20 20 20 20 20 20 31 31 2c 20 20 20 36 2c  ,.      11,   6,
32e32 20 20 20 32 2c 20 20 20 37 2c 20 20 20 35 2c 20     2,   7,   5, 
32e33 20 20 35 2c 20 20 20 39 2c 20 20 20 36 2c 20 20    5,   9,   6,  
32e34 20 39 2c 20 20 20 39 2c 20 20 20 37 2c 20 20 31   9,   9,   7,  1
32e35 30 2c 20 20 31 30 2c 0a 20 20 20 20 20 20 20 34  0,  10,.       4
32e36 2c 20 20 20 36 2c 20 20 20 32 2c 20 20 20 33 2c  ,   6,   2,   3,
32e37 20 20 20 39 2c 20 20 20 34 2c 20 20 20 32 2c 20     9,   4,   2, 
32e38 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 20 20    6,   5,   6,  
32e39 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 0a 20 20   6,   5,   6,.  
32e3a 20 20 20 20 20 35 2c 20 20 20 35 2c 20 20 20 37       5,   5,   7
32e3b 2c 20 20 20 37 2c 20 20 20 37 2c 20 20 20 33 2c  ,   7,   7,   3,
32e3c 20 20 20 32 2c 20 20 20 34 2c 20 20 20 34 2c 20     2,   4,   4, 
32e3d 20 20 37 2c 20 20 20 33 2c 20 20 20 36 2c 20 20    7,   3,   6,  
32e3e 20 34 2c 0a 20 20 20 20 20 20 20 37 2c 20 20 20   4,.       7,   
32e3f 36 2c 20 20 31 32 2c 20 20 20 36 2c 20 20 20 39  6,  12,   6,   9
32e40 2c 20 20 20 34 2c 20 20 20 36 2c 20 20 20 35 2c  ,   4,   6,   5,
32e41 20 20 20 34 2c 20 20 20 37 2c 20 20 20 36 2c 20     4,   7,   6, 
32e42 20 20 35 2c 20 20 20 36 2c 0a 20 20 20 20 20 20    5,   6,.      
32e43 20 37 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20   7,   5,   4,   
32e44 35 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 37  5,   6,   5,   7
32e45 2c 20 20 20 33 2c 20 20 20 37 2c 20 20 31 33 2c  ,   3,   7,  13,
32e46 20 20 20 32 2c 20 20 20 32 2c 20 20 20 34 2c 0a     2,   2,   4,.
32e47 20 20 20 20 20 20 20 36 2c 20 20 20 36 2c 20 20         6,   6,  
32e48 20 38 2c 20 20 20 35 2c 20 20 31 37 2c 20 20 31   8,   5,  17,  1
32e49 32 2c 20 20 20 37 2c 20 20 20 38 2c 20 20 20 38  2,   7,   8,   8
32e4a 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20 34 2c  ,   2,   4,   4,
32e4b 20 20 20 34 2c 0a 20 20 20 20 20 20 20 34 2c 20     4,.       4, 
32e4c 20 20 34 2c 20 20 20 32 2c 20 20 20 32 2c 20 20    4,   2,   2,  
32e4d 20 36 2c 20 20 20 35 2c 20 20 20 38 2c 20 20 20   6,   5,   8,   
32e4e 35 2c 20 20 20 35 2c 20 20 20 38 2c 20 20 20 33  5,   5,   8,   3
32e4f 2c 20 20 20 35 2c 20 20 20 35 2c 0a 20 20 20 20  ,   5,   5,.    
32e50 20 20 20 36 2c 20 20 20 34 2c 20 20 20 39 2c 20     6,   4,   9, 
32e51 20 20 33 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74    3,.  };.  stat
32e52 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
32e53 64 20 73 68 6f 72 74 20 69 6e 74 20 61 4f 66 66  d short int aOff
32e54 73 65 74 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20  set[121] = {.   
32e55 20 20 20 20 30 2c 20 20 20 32 2c 20 20 20 32 2c      0,   2,   2,
32e56 20 20 20 38 2c 20 20 20 39 2c 20 20 31 34 2c 20     8,   9,  14, 
32e57 20 31 36 2c 20 20 32 30 2c 20 20 32 33 2c 20 20   16,  20,  23,  
32e58 32 35 2c 20 20 32 35 2c 20 20 32 39 2c 20 20 33  25,  25,  29,  3
32e59 33 2c 0a 20 20 20 20 20 20 33 36 2c 20 20 34 31  3,.      36,  41
32e5a 2c 20 20 34 36 2c 20 20 34 38 2c 20 20 35 33 2c  ,  46,  48,  53,
32e5b 20 20 35 34 2c 20 20 35 39 2c 20 20 36 32 2c 20    54,  59,  62, 
32e5c 20 36 35 2c 20 20 36 37 2c 20 20 36 39 2c 20 20   65,  67,  69,  
32e5d 37 38 2c 20 20 38 31 2c 0a 20 20 20 20 20 20 38  78,  81,.      8
32e5e 36 2c 20 20 39 31 2c 20 20 39 35 2c 20 20 39 36  6,  91,  95,  96
32e5f 2c 20 31 30 31 2c 20 31 30 35 2c 20 31 30 39 2c  , 101, 105, 109,
32e60 20 31 31 37 2c 20 31 32 32 2c 20 31 32 38 2c 20   117, 122, 128, 
32e61 31 33 36 2c 20 31 34 32 2c 20 31 35 32 2c 0a 20  136, 142, 152,. 
32e62 20 20 20 20 31 35 39 2c 20 31 36 32 2c 20 31 36      159, 162, 16
32e63 32 2c 20 31 36 35 2c 20 31 36 37 2c 20 31 36 37  2, 165, 167, 167
32e64 2c 20 31 37 31 2c 20 31 37 36 2c 20 31 37 39 2c  , 171, 176, 179,
32e65 20 31 38 34 2c 20 31 38 39 2c 20 31 39 34 2c 20   184, 189, 194, 
32e66 31 39 37 2c 0a 20 20 20 20 20 32 30 33 2c 20 32  197,.     203, 2
32e67 30 36 2c 20 32 31 30 2c 20 32 31 37 2c 20 32 32  06, 210, 217, 22
32e68 33 2c 20 32 32 33 2c 20 32 32 33 2c 20 32 32 36  3, 223, 223, 226
32e69 2c 20 32 32 39 2c 20 32 33 33 2c 20 32 33 34 2c  , 229, 233, 234,
32e6a 20 32 33 38 2c 20 32 34 34 2c 0a 20 20 20 20 20   238, 244,.     
32e6b 32 34 38 2c 20 32 35 35 2c 20 32 36 31 2c 20 32  248, 255, 261, 2
32e6c 37 33 2c 20 32 37 39 2c 20 32 38 38 2c 20 32 39  73, 279, 288, 29
32e6d 30 2c 20 32 39 36 2c 20 33 30 31 2c 20 33 30 33  0, 296, 301, 303
32e6e 2c 20 33 31 30 2c 20 33 31 35 2c 20 33 32 30 2c  , 310, 315, 320,
32e6f 0a 20 20 20 20 20 33 32 36 2c 20 33 33 32 2c 20  .     326, 332, 
32e70 33 33 37 2c 20 33 34 31 2c 20 33 34 34 2c 20 33  337, 341, 344, 3
32e71 35 30 2c 20 33 35 34 2c 20 33 36 31 2c 20 33 36  50, 354, 361, 36
32e72 33 2c 20 33 37 30 2c 20 33 37 32 2c 20 33 37 34  3, 370, 372, 374
32e73 2c 20 33 38 33 2c 0a 20 20 20 20 20 33 38 37 2c  , 383,.     387,
32e74 20 33 39 33 2c 20 33 39 39 2c 20 34 30 37 2c 20   393, 399, 407, 
32e75 34 31 32 2c 20 34 31 32 2c 20 34 32 38 2c 20 34  412, 412, 428, 4
32e76 33 35 2c 20 34 34 32 2c 20 34 34 33 2c 20 34 35  35, 442, 443, 45
32e77 30 2c 20 34 35 34 2c 20 34 35 38 2c 0a 20 20 20  0, 454, 458,.   
32e78 20 20 34 36 32 2c 20 34 36 36 2c 20 34 36 39 2c    462, 466, 469,
32e79 20 34 37 31 2c 20 34 37 33 2c 20 34 37 39 2c 20   471, 473, 479, 
32e7a 34 38 33 2c 20 34 39 31 2c 20 34 39 35 2c 20 35  483, 491, 495, 5
32e7b 30 30 2c 20 35 30 38 2c 20 35 31 31 2c 20 35 31  00, 508, 511, 51
32e7c 36 2c 0a 20 20 20 20 20 35 32 31 2c 20 35 32 37  6,.     521, 527
32e7d 2c 20 35 33 31 2c 20 35 33 36 2c 0a 20 20 7d 3b  , 531, 536,.  };
32e7e 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
32e7f 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 43  unsigned char aC
32e80 6f 64 65 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20  ode[121] = {.   
32e81 20 54 4b 5f 52 45 49 4e 44 45 58 2c 20 20 20 20   TK_REINDEX,    
32e82 54 4b 5f 49 4e 44 45 58 45 44 2c 20 20 20 20 54  TK_INDEXED,    T
32e83 4b 5f 49 4e 44 45 58 2c 20 20 20 20 20 20 54 4b  K_INDEX,      TK
32e84 5f 44 45 53 43 2c 20 20 20 20 20 20 20 54 4b 5f  _DESC,       TK_
32e85 45 53 43 41 50 45 2c 20 20 20 20 20 0a 20 20 20  ESCAPE,     .   
32e86 20 54 4b 5f 45 41 43 48 2c 20 20 20 20 20 20 20   TK_EACH,       
32e87 54 4b 5f 43 48 45 43 4b 2c 20 20 20 20 20 20 54  TK_CHECK,      T
32e88 4b 5f 4b 45 59 2c 20 20 20 20 20 20 20 20 54 4b  K_KEY,        TK
32e89 5f 42 45 46 4f 52 45 2c 20 20 20 20 20 54 4b 5f  _BEFORE,     TK_
32e8a 46 4f 52 45 49 47 4e 2c 20 20 20 20 0a 20 20 20  FOREIGN,    .   
32e8b 20 54 4b 5f 46 4f 52 2c 20 20 20 20 20 20 20 20   TK_FOR,        
32e8c 54 4b 5f 49 47 4e 4f 52 45 2c 20 20 20 20 20 54  TK_IGNORE,     T
32e8d 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b  K_LIKE_KW,    TK
32e8e 5f 45 58 50 4c 41 49 4e 2c 20 20 20 20 54 4b 5f  _EXPLAIN,    TK_
32e8f 49 4e 53 54 45 41 44 2c 20 20 20 20 0a 20 20 20  INSTEAD,    .   
32e90 20 54 4b 5f 41 44 44 2c 20 20 20 20 20 20 20 20   TK_ADD,        
32e91 54 4b 5f 44 41 54 41 42 41 53 45 2c 20 20 20 54  TK_DATABASE,   T
32e92 4b 5f 41 53 2c 20 20 20 20 20 20 20 20 20 54 4b  K_AS,         TK
32e93 5f 53 45 4c 45 43 54 2c 20 20 20 20 20 54 4b 5f  _SELECT,     TK_
32e94 54 41 42 4c 45 2c 20 20 20 20 20 20 0a 20 20 20  TABLE,      .   
32e95 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
32e96 54 4b 5f 54 48 45 4e 2c 20 20 20 20 20 20 20 54  TK_THEN,       T
32e97 4b 5f 45 4e 44 2c 20 20 20 20 20 20 20 20 54 4b  K_END,        TK
32e98 5f 44 45 46 45 52 52 41 42 4c 45 2c 20 54 4b 5f  _DEFERRABLE, TK_
32e99 45 4c 53 45 2c 20 20 20 20 20 20 20 0a 20 20 20  ELSE,       .   
32e9a 20 54 4b 5f 45 58 43 45 50 54 2c 20 20 20 20 20   TK_EXCEPT,     
32e9b 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 54  TK_TRANSACTION,T
32e9c 4b 5f 41 43 54 49 4f 4e 2c 20 20 20 20 20 54 4b  K_ACTION,     TK
32e9d 5f 4f 4e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _ON,         TK_
32e9e 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20  JOIN_KW,    .   
32e9f 20 54 4b 5f 41 4c 54 45 52 2c 20 20 20 20 20 20   TK_ALTER,      
32ea0 54 4b 5f 52 41 49 53 45 2c 20 20 20 20 20 20 54  TK_RAISE,      T
32ea1 4b 5f 45 58 43 4c 55 53 49 56 45 2c 20 20 54 4b  K_EXCLUSIVE,  TK
32ea2 5f 45 58 49 53 54 53 2c 20 20 20 20 20 54 4b 5f  _EXISTS,     TK_
32ea3 53 41 56 45 50 4f 49 4e 54 2c 20 20 0a 20 20 20  SAVEPOINT,  .   
32ea4 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 2c 20 20   TK_INTERSECT,  
32ea5 54 4b 5f 54 52 49 47 47 45 52 2c 20 20 20 20 54  TK_TRIGGER,    T
32ea6 4b 5f 52 45 46 45 52 45 4e 43 45 53 2c 20 54 4b  K_REFERENCES, TK
32ea7 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 54 4b 5f  _CONSTRAINT, TK_
32ea8 49 4e 54 4f 2c 20 20 20 20 20 20 20 0a 20 20 20  INTO,       .   
32ea9 20 54 4b 5f 4f 46 46 53 45 54 2c 20 20 20 20 20   TK_OFFSET,     
32eaa 54 4b 5f 4f 46 2c 20 20 20 20 20 20 20 20 20 54  TK_OF,         T
32eab 4b 5f 53 45 54 2c 20 20 20 20 20 20 20 20 54 4b  K_SET,        TK
32eac 5f 54 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f  _TEMP,       TK_
32ead 54 45 4d 50 2c 20 20 20 20 20 20 20 0a 20 20 20  TEMP,       .   
32eae 20 54 4b 5f 4f 52 2c 20 20 20 20 20 20 20 20 20   TK_OR,         
32eaf 54 4b 5f 55 4e 49 51 55 45 2c 20 20 20 20 20 54  TK_UNIQUE,     T
32eb0 4b 5f 51 55 45 52 59 2c 20 20 20 20 20 20 54 4b  K_QUERY,      TK
32eb1 5f 41 54 54 41 43 48 2c 20 20 20 20 20 54 4b 5f  _ATTACH,     TK_
32eb2 48 41 56 49 4e 47 2c 20 20 20 20 20 0a 20 20 20  HAVING,     .   
32eb3 20 54 4b 5f 47 52 4f 55 50 2c 20 20 20 20 20 20   TK_GROUP,      
32eb4 54 4b 5f 55 50 44 41 54 45 2c 20 20 20 20 20 54  TK_UPDATE,     T
32eb5 4b 5f 42 45 47 49 4e 2c 20 20 20 20 20 20 54 4b  K_BEGIN,      TK
32eb6 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f  _JOIN_KW,    TK_
32eb7 52 45 4c 45 41 53 45 2c 20 20 20 20 0a 20 20 20  RELEASE,    .   
32eb8 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20 20 20 20   TK_BETWEEN,    
32eb9 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 20 20 20 54  TK_NOTNULL,    T
32eba 4b 5f 4e 4f 54 2c 20 20 20 20 20 20 20 20 54 4b  K_NOT,        TK
32ebb 5f 4e 4f 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _NO,         TK_
32ebc 4e 55 4c 4c 2c 20 20 20 20 20 20 20 0a 20 20 20  NULL,       .   
32ebd 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20   TK_LIKE_KW,    
32ebe 54 4b 5f 43 41 53 43 41 44 45 2c 20 20 20 20 54  TK_CASCADE,    T
32ebf 4b 5f 41 53 43 2c 20 20 20 20 20 20 20 20 54 4b  K_ASC,        TK
32ec0 5f 44 45 4c 45 54 45 2c 20 20 20 20 20 54 4b 5f  _DELETE,     TK_
32ec1 43 41 53 45 2c 20 20 20 20 20 20 20 0a 20 20 20  CASE,       .   
32ec2 20 54 4b 5f 43 4f 4c 4c 41 54 45 2c 20 20 20 20   TK_COLLATE,    
32ec3 54 4b 5f 43 52 45 41 54 45 2c 20 20 20 20 20 54  TK_CREATE,     T
32ec4 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b  K_CTIME_KW,   TK
32ec5 5f 44 45 54 41 43 48 2c 20 20 20 20 20 54 4b 5f  _DETACH,     TK_
32ec6 49 4d 4d 45 44 49 41 54 45 2c 20 20 0a 20 20 20  IMMEDIATE,  .   
32ec7 20 54 4b 5f 4a 4f 49 4e 2c 20 20 20 20 20 20 20   TK_JOIN,       
32ec8 54 4b 5f 49 4e 53 45 52 54 2c 20 20 20 20 20 54  TK_INSERT,     T
32ec9 4b 5f 4d 41 54 43 48 2c 20 20 20 20 20 20 54 4b  K_MATCH,      TK
32eca 5f 50 4c 41 4e 2c 20 20 20 20 20 20 20 54 4b 5f  _PLAN,       TK_
32ecb 41 4e 41 4c 59 5a 45 2c 20 20 20 20 0a 20 20 20  ANALYZE,    .   
32ecc 20 54 4b 5f 50 52 41 47 4d 41 2c 20 20 20 20 20   TK_PRAGMA,     
32ecd 54 4b 5f 41 42 4f 52 54 2c 20 20 20 20 20 20 54  TK_ABORT,      T
32ece 4b 5f 56 41 4c 55 45 53 2c 20 20 20 20 20 54 4b  K_VALUES,     TK
32ecf 5f 56 49 52 54 55 41 4c 2c 20 20 20 20 54 4b 5f  _VIRTUAL,    TK_
32ed0 4c 49 4d 49 54 2c 20 20 20 20 20 20 0a 20 20 20  LIMIT,      .   
32ed1 20 54 4b 5f 57 48 45 4e 2c 20 20 20 20 20 20 20   TK_WHEN,       
32ed2 54 4b 5f 57 48 45 52 45 2c 20 20 20 20 20 20 54  TK_WHERE,      T
32ed3 4b 5f 52 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b  K_RENAME,     TK
32ed4 5f 41 46 54 45 52 2c 20 20 20 20 20 20 54 4b 5f  _AFTER,      TK_
32ed5 52 45 50 4c 41 43 45 2c 20 20 20 20 0a 20 20 20  REPLACE,    .   
32ed6 20 54 4b 5f 41 4e 44 2c 20 20 20 20 20 20 20 20   TK_AND,        
32ed7 54 4b 5f 44 45 46 41 55 4c 54 2c 20 20 20 20 54  TK_DEFAULT,    T
32ed8 4b 5f 41 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b  K_AUTOINCR,   TK
32ed9 5f 54 4f 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _TO,         TK_
32eda 49 4e 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20  IN,         .   
32edb 20 54 4b 5f 43 41 53 54 2c 20 20 20 20 20 20 20   TK_CAST,       
32edc 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c 20 20 20 54  TK_COLUMNKW,   T
32edd 4b 5f 43 4f 4d 4d 49 54 2c 20 20 20 20 20 54 4b  K_COMMIT,     TK
32ede 5f 43 4f 4e 46 4c 49 43 54 2c 20 20 20 54 4b 5f  _CONFLICT,   TK_
32edf 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20  JOIN_KW,    .   
32ee0 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20   TK_CTIME_KW,   
32ee1 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54  TK_CTIME_KW,   T
32ee2 4b 5f 50 52 49 4d 41 52 59 2c 20 20 20 20 54 4b  K_PRIMARY,    TK
32ee3 5f 44 45 46 45 52 52 45 44 2c 20 20 20 54 4b 5f  _DEFERRED,   TK_
32ee4 44 49 53 54 49 4e 43 54 2c 20 20 20 0a 20 20 20  DISTINCT,   .   
32ee5 20 54 4b 5f 49 53 2c 20 20 20 20 20 20 20 20 20   TK_IS,         
32ee6 54 4b 5f 44 52 4f 50 2c 20 20 20 20 20 20 20 54  TK_DROP,       T
32ee7 4b 5f 46 41 49 4c 2c 20 20 20 20 20 20 20 54 4b  K_FAIL,       TK
32ee8 5f 46 52 4f 4d 2c 20 20 20 20 20 20 20 54 4b 5f  _FROM,       TK_
32ee9 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20  JOIN_KW,    .   
32eea 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20   TK_LIKE_KW,    
32eeb 54 4b 5f 42 59 2c 20 20 20 20 20 20 20 20 20 54  TK_BY,         T
32eec 4b 5f 49 46 2c 20 20 20 20 20 20 20 20 20 54 4b  K_IF,         TK
32eed 5f 49 53 4e 55 4c 4c 2c 20 20 20 20 20 54 4b 5f  _ISNULL,     TK_
32eee 4f 52 44 45 52 2c 20 20 20 20 20 20 0a 20 20 20  ORDER,      .   
32eef 20 54 4b 5f 52 45 53 54 52 49 43 54 2c 20 20 20   TK_RESTRICT,   
32ef0 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54  TK_JOIN_KW,    T
32ef1 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
32ef2 5f 52 4f 4c 4c 42 41 43 4b 2c 20 20 20 54 4b 5f  _ROLLBACK,   TK_
32ef3 52 4f 57 2c 20 20 20 20 20 20 20 20 0a 20 20 20  ROW,        .   
32ef4 20 54 4b 5f 55 4e 49 4f 4e 2c 20 20 20 20 20 20   TK_UNION,      
32ef5 54 4b 5f 55 53 49 4e 47 2c 20 20 20 20 20 20 54  TK_USING,      T
32ef6 4b 5f 56 41 43 55 55 4d 2c 20 20 20 20 20 54 4b  K_VACUUM,     TK
32ef7 5f 56 49 45 57 2c 20 20 20 20 20 20 20 54 4b 5f  _VIEW,       TK_
32ef8 49 4e 49 54 49 41 4c 4c 59 2c 20 20 0a 20 20 20  INITIALLY,  .   
32ef9 20 54 4b 5f 41 4c 4c 2c 20 20 20 20 20 20 20 20   TK_ALL,        
32efa 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 68 2c 20 69  .  };.  int h, i
32efb 3b 0a 20 20 69 66 28 20 6e 3c 32 20 29 20 72 65  ;.  if( n<2 ) re
32efc 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 20 20 68 20  turn TK_ID;.  h 
32efd 3d 20 28 28 63 68 61 72 4d 61 70 28 7a 5b 30 5d  = ((charMap(z[0]
32efe 29 2a 34 29 20 5e 0a 20 20 20 20 20 20 28 63 68  )*4) ^.      (ch
32eff 61 72 4d 61 70 28 7a 5b 6e 2d 31 5d 29 2a 33 29  arMap(z[n-1])*3)
32f00 20 5e 0a 20 20 20 20 20 20 6e 29 20 25 20 31 32   ^.      n) % 12
32f01 37 3b 0a 20 20 66 6f 72 28 69 3d 28 28 69 6e 74  7;.  for(i=((int
32f02 29 61 48 61 73 68 5b 68 5d 29 2d 31 3b 20 69 3e  )aHash[h])-1; i>
32f03 3d 30 3b 20 69 3d 28 28 69 6e 74 29 61 4e 65 78  =0; i=((int)aNex
32f04 74 5b 69 5d 29 2d 31 29 7b 0a 20 20 20 20 69 66  t[i])-1){.    if
32f05 28 20 61 4c 65 6e 5b 69 5d 3d 3d 6e 20 26 26 20  ( aLen[i]==n && 
32f06 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
32f07 26 7a 54 65 78 74 5b 61 4f 66 66 73 65 74 5b 69  &zText[aOffset[i
32f08 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20  ]],z,n)==0 ){.  
32f09 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32f0a 3d 30 20 29 3b 20 2f 2a 20 52 45 49 4e 44 45 58  =0 ); /* REINDEX
32f0b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32f0c 73 65 28 20 69 3d 3d 31 20 29 3b 20 2f 2a 20 49  se( i==1 ); /* I
32f0d 4e 44 45 58 45 44 20 2a 2f 0a 20 20 20 20 20 20  NDEXED */.      
32f0e 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 20 29  testcase( i==2 )
32f0f 3b 20 2f 2a 20 49 4e 44 45 58 20 2a 2f 0a 20 20  ; /* INDEX */.  
32f10 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32f11 3d 33 20 29 3b 20 2f 2a 20 44 45 53 43 20 2a 2f  =3 ); /* DESC */
32f12 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32f13 20 69 3d 3d 34 20 29 3b 20 2f 2a 20 45 53 43 41   i==4 ); /* ESCA
32f14 50 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  PE */.      test
32f15 63 61 73 65 28 20 69 3d 3d 35 20 29 3b 20 2f 2a  case( i==5 ); /*
32f16 20 45 41 43 48 20 2a 2f 0a 20 20 20 20 20 20 74   EACH */.      t
32f17 65 73 74 63 61 73 65 28 20 69 3d 3d 36 20 29 3b  estcase( i==6 );
32f18 20 2f 2a 20 43 48 45 43 4b 20 2a 2f 0a 20 20 20   /* CHECK */.   
32f19 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32f1a 37 20 29 3b 20 2f 2a 20 4b 45 59 20 2a 2f 0a 20  7 ); /* KEY */. 
32f1b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32f1c 3d 3d 38 20 29 3b 20 2f 2a 20 42 45 46 4f 52 45  ==8 ); /* BEFORE
32f1d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32f1e 73 65 28 20 69 3d 3d 39 20 29 3b 20 2f 2a 20 46  se( i==9 ); /* F
32f1f 4f 52 45 49 47 4e 20 2a 2f 0a 20 20 20 20 20 20  OREIGN */.      
32f20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 20  testcase( i==10 
32f21 29 3b 20 2f 2a 20 46 4f 52 20 2a 2f 0a 20 20 20  ); /* FOR */.   
32f22 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32f23 31 31 20 29 3b 20 2f 2a 20 49 47 4e 4f 52 45 20  11 ); /* IGNORE 
32f24 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32f25 65 28 20 69 3d 3d 31 32 20 29 3b 20 2f 2a 20 52  e( i==12 ); /* R
32f26 45 47 45 58 50 20 2a 2f 0a 20 20 20 20 20 20 74  EGEXP */.      t
32f27 65 73 74 63 61 73 65 28 20 69 3d 3d 31 33 20 29  estcase( i==13 )
32f28 3b 20 2f 2a 20 45 58 50 4c 41 49 4e 20 2a 2f 0a  ; /* EXPLAIN */.
32f29 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32f2a 69 3d 3d 31 34 20 29 3b 20 2f 2a 20 49 4e 53 54  i==14 ); /* INST
32f2b 45 41 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  EAD */.      tes
32f2c 74 63 61 73 65 28 20 69 3d 3d 31 35 20 29 3b 20  tcase( i==15 ); 
32f2d 2f 2a 20 41 44 44 20 2a 2f 0a 20 20 20 20 20 20  /* ADD */.      
32f2e 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 36 20  testcase( i==16 
32f2f 29 3b 20 2f 2a 20 44 41 54 41 42 41 53 45 20 2a  ); /* DATABASE *
32f30 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32f31 28 20 69 3d 3d 31 37 20 29 3b 20 2f 2a 20 41 53  ( i==17 ); /* AS
32f32 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32f33 73 65 28 20 69 3d 3d 31 38 20 29 3b 20 2f 2a 20  se( i==18 ); /* 
32f34 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20  SELECT */.      
32f35 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 39 20  testcase( i==19 
32f36 29 3b 20 2f 2a 20 54 41 42 4c 45 20 2a 2f 0a 20  ); /* TABLE */. 
32f37 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32f38 3d 3d 32 30 20 29 3b 20 2f 2a 20 4c 45 46 54 20  ==20 ); /* LEFT 
32f39 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32f3a 65 28 20 69 3d 3d 32 31 20 29 3b 20 2f 2a 20 54  e( i==21 ); /* T
32f3b 48 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  HEN */.      tes
32f3c 74 63 61 73 65 28 20 69 3d 3d 32 32 20 29 3b 20  tcase( i==22 ); 
32f3d 2f 2a 20 45 4e 44 20 2a 2f 0a 20 20 20 20 20 20  /* END */.      
32f3e 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 33 20  testcase( i==23 
32f3f 29 3b 20 2f 2a 20 44 45 46 45 52 52 41 42 4c 45  ); /* DEFERRABLE
32f40 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32f41 73 65 28 20 69 3d 3d 32 34 20 29 3b 20 2f 2a 20  se( i==24 ); /* 
32f42 45 4c 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  ELSE */.      te
32f43 73 74 63 61 73 65 28 20 69 3d 3d 32 35 20 29 3b  stcase( i==25 );
32f44 20 2f 2a 20 45 58 43 45 50 54 20 2a 2f 0a 20 20   /* EXCEPT */.  
32f45 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32f46 3d 32 36 20 29 3b 20 2f 2a 20 54 52 41 4e 53 41  =26 ); /* TRANSA
32f47 43 54 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74  CTION */.      t
32f48 65 73 74 63 61 73 65 28 20 69 3d 3d 32 37 20 29  estcase( i==27 )
32f49 3b 20 2f 2a 20 41 43 54 49 4f 4e 20 2a 2f 0a 20  ; /* ACTION */. 
32f4a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32f4b 3d 3d 32 38 20 29 3b 20 2f 2a 20 4f 4e 20 2a 2f  ==28 ); /* ON */
32f4c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32f4d 20 69 3d 3d 32 39 20 29 3b 20 2f 2a 20 4e 41 54   i==29 ); /* NAT
32f4e 55 52 41 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65  URAL */.      te
32f4f 73 74 63 61 73 65 28 20 69 3d 3d 33 30 20 29 3b  stcase( i==30 );
32f50 20 2f 2a 20 41 4c 54 45 52 20 2a 2f 0a 20 20 20   /* ALTER */.   
32f51 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32f52 33 31 20 29 3b 20 2f 2a 20 52 41 49 53 45 20 2a  31 ); /* RAISE *
32f53 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32f54 28 20 69 3d 3d 33 32 20 29 3b 20 2f 2a 20 45 58  ( i==32 ); /* EX
32f55 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 20 20 20  CLUSIVE */.     
32f56 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 33   testcase( i==33
32f57 20 29 3b 20 2f 2a 20 45 58 49 53 54 53 20 2a 2f   ); /* EXISTS */
32f58 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32f59 20 69 3d 3d 33 34 20 29 3b 20 2f 2a 20 53 41 56   i==34 ); /* SAV
32f5a 45 50 4f 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20  EPOINT */.      
32f5b 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 35 20  testcase( i==35 
32f5c 29 3b 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20  ); /* INTERSECT 
32f5d 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32f5e 65 28 20 69 3d 3d 33 36 20 29 3b 20 2f 2a 20 54  e( i==36 ); /* T
32f5f 52 49 47 47 45 52 20 2a 2f 0a 20 20 20 20 20 20  RIGGER */.      
32f60 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 37 20  testcase( i==37 
32f61 29 3b 20 2f 2a 20 52 45 46 45 52 45 4e 43 45 53  ); /* REFERENCES
32f62 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32f63 73 65 28 20 69 3d 3d 33 38 20 29 3b 20 2f 2a 20  se( i==38 ); /* 
32f64 43 4f 4e 53 54 52 41 49 4e 54 20 2a 2f 0a 20 20  CONSTRAINT */.  
32f65 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32f66 3d 33 39 20 29 3b 20 2f 2a 20 49 4e 54 4f 20 2a  =39 ); /* INTO *
32f67 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32f68 28 20 69 3d 3d 34 30 20 29 3b 20 2f 2a 20 4f 46  ( i==40 ); /* OF
32f69 46 53 45 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  FSET */.      te
32f6a 73 74 63 61 73 65 28 20 69 3d 3d 34 31 20 29 3b  stcase( i==41 );
32f6b 20 2f 2a 20 4f 46 20 2a 2f 0a 20 20 20 20 20 20   /* OF */.      
32f6c 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 32 20  testcase( i==42 
32f6d 29 3b 20 2f 2a 20 53 45 54 20 2a 2f 0a 20 20 20  ); /* SET */.   
32f6e 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32f6f 34 33 20 29 3b 20 2f 2a 20 54 45 4d 50 4f 52 41  43 ); /* TEMPORA
32f70 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RY */.      test
32f71 63 61 73 65 28 20 69 3d 3d 34 34 20 29 3b 20 2f  case( i==44 ); /
32f72 2a 20 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20 20  * TEMP */.      
32f73 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 35 20  testcase( i==45 
32f74 29 3b 20 2f 2a 20 4f 52 20 2a 2f 0a 20 20 20 20  ); /* OR */.    
32f75 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
32f76 36 20 29 3b 20 2f 2a 20 55 4e 49 51 55 45 20 2a  6 ); /* UNIQUE *
32f77 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32f78 28 20 69 3d 3d 34 37 20 29 3b 20 2f 2a 20 51 55  ( i==47 ); /* QU
32f79 45 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ERY */.      tes
32f7a 74 63 61 73 65 28 20 69 3d 3d 34 38 20 29 3b 20  tcase( i==48 ); 
32f7b 2f 2a 20 41 54 54 41 43 48 20 2a 2f 0a 20 20 20  /* ATTACH */.   
32f7c 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32f7d 34 39 20 29 3b 20 2f 2a 20 48 41 56 49 4e 47 20  49 ); /* HAVING 
32f7e 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32f7f 65 28 20 69 3d 3d 35 30 20 29 3b 20 2f 2a 20 47  e( i==50 ); /* G
32f80 52 4f 55 50 20 2a 2f 0a 20 20 20 20 20 20 74 65  ROUP */.      te
32f81 73 74 63 61 73 65 28 20 69 3d 3d 35 31 20 29 3b  stcase( i==51 );
32f82 20 2f 2a 20 55 50 44 41 54 45 20 2a 2f 0a 20 20   /* UPDATE */.  
32f83 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32f84 3d 35 32 20 29 3b 20 2f 2a 20 42 45 47 49 4e 20  =52 ); /* BEGIN 
32f85 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32f86 65 28 20 69 3d 3d 35 33 20 29 3b 20 2f 2a 20 49  e( i==53 ); /* I
32f87 4e 4e 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  NNER */.      te
32f88 73 74 63 61 73 65 28 20 69 3d 3d 35 34 20 29 3b  stcase( i==54 );
32f89 20 2f 2a 20 52 45 4c 45 41 53 45 20 2a 2f 0a 20   /* RELEASE */. 
32f8a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32f8b 3d 3d 35 35 20 29 3b 20 2f 2a 20 42 45 54 57 45  ==55 ); /* BETWE
32f8c 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  EN */.      test
32f8d 63 61 73 65 28 20 69 3d 3d 35 36 20 29 3b 20 2f  case( i==56 ); /
32f8e 2a 20 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 20 20 20  * NOTNULL */.   
32f8f 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32f90 35 37 20 29 3b 20 2f 2a 20 4e 4f 54 20 2a 2f 0a  57 ); /* NOT */.
32f91 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32f92 69 3d 3d 35 38 20 29 3b 20 2f 2a 20 4e 4f 20 2a  i==58 ); /* NO *
32f93 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32f94 28 20 69 3d 3d 35 39 20 29 3b 20 2f 2a 20 4e 55  ( i==59 ); /* NU
32f95 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  LL */.      test
32f96 63 61 73 65 28 20 69 3d 3d 36 30 20 29 3b 20 2f  case( i==60 ); /
32f97 2a 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20  * LIKE */.      
32f98 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 31 20  testcase( i==61 
32f99 29 3b 20 2f 2a 20 43 41 53 43 41 44 45 20 2a 2f  ); /* CASCADE */
32f9a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32f9b 20 69 3d 3d 36 32 20 29 3b 20 2f 2a 20 41 53 43   i==62 ); /* ASC
32f9c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32f9d 73 65 28 20 69 3d 3d 36 33 20 29 3b 20 2f 2a 20  se( i==63 ); /* 
32f9e 44 45 4c 45 54 45 20 2a 2f 0a 20 20 20 20 20 20  DELETE */.      
32f9f 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 34 20  testcase( i==64 
32fa0 29 3b 20 2f 2a 20 43 41 53 45 20 2a 2f 0a 20 20  ); /* CASE */.  
32fa1 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32fa2 3d 36 35 20 29 3b 20 2f 2a 20 43 4f 4c 4c 41 54  =65 ); /* COLLAT
32fa3 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
32fa4 61 73 65 28 20 69 3d 3d 36 36 20 29 3b 20 2f 2a  ase( i==66 ); /*
32fa5 20 43 52 45 41 54 45 20 2a 2f 0a 20 20 20 20 20   CREATE */.     
32fa6 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 37   testcase( i==67
32fa7 20 29 3b 20 2f 2a 20 43 55 52 52 45 4e 54 5f 44   ); /* CURRENT_D
32fa8 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ATE */.      tes
32fa9 74 63 61 73 65 28 20 69 3d 3d 36 38 20 29 3b 20  tcase( i==68 ); 
32faa 2f 2a 20 44 45 54 41 43 48 20 2a 2f 0a 20 20 20  /* DETACH */.   
32fab 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32fac 36 39 20 29 3b 20 2f 2a 20 49 4d 4d 45 44 49 41  69 ); /* IMMEDIA
32fad 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  TE */.      test
32fae 63 61 73 65 28 20 69 3d 3d 37 30 20 29 3b 20 2f  case( i==70 ); /
32faf 2a 20 4a 4f 49 4e 20 2a 2f 0a 20 20 20 20 20 20  * JOIN */.      
32fb0 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 31 20  testcase( i==71 
32fb1 29 3b 20 2f 2a 20 49 4e 53 45 52 54 20 2a 2f 0a  ); /* INSERT */.
32fb2 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32fb3 69 3d 3d 37 32 20 29 3b 20 2f 2a 20 4d 41 54 43  i==72 ); /* MATC
32fb4 48 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  H */.      testc
32fb5 61 73 65 28 20 69 3d 3d 37 33 20 29 3b 20 2f 2a  ase( i==73 ); /*
32fb6 20 50 4c 41 4e 20 2a 2f 0a 20 20 20 20 20 20 74   PLAN */.      t
32fb7 65 73 74 63 61 73 65 28 20 69 3d 3d 37 34 20 29  estcase( i==74 )
32fb8 3b 20 2f 2a 20 41 4e 41 4c 59 5a 45 20 2a 2f 0a  ; /* ANALYZE */.
32fb9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32fba 69 3d 3d 37 35 20 29 3b 20 2f 2a 20 50 52 41 47  i==75 ); /* PRAG
32fbb 4d 41 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  MA */.      test
32fbc 63 61 73 65 28 20 69 3d 3d 37 36 20 29 3b 20 2f  case( i==76 ); /
32fbd 2a 20 41 42 4f 52 54 20 2a 2f 0a 20 20 20 20 20  * ABORT */.     
32fbe 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 37   testcase( i==77
32fbf 20 29 3b 20 2f 2a 20 56 41 4c 55 45 53 20 2a 2f   ); /* VALUES */
32fc0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32fc1 20 69 3d 3d 37 38 20 29 3b 20 2f 2a 20 56 49 52   i==78 ); /* VIR
32fc2 54 55 41 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65  TUAL */.      te
32fc3 73 74 63 61 73 65 28 20 69 3d 3d 37 39 20 29 3b  stcase( i==79 );
32fc4 20 2f 2a 20 4c 49 4d 49 54 20 2a 2f 0a 20 20 20   /* LIMIT */.   
32fc5 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32fc6 38 30 20 29 3b 20 2f 2a 20 57 48 45 4e 20 2a 2f  80 ); /* WHEN */
32fc7 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32fc8 20 69 3d 3d 38 31 20 29 3b 20 2f 2a 20 57 48 45   i==81 ); /* WHE
32fc9 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RE */.      test
32fca 63 61 73 65 28 20 69 3d 3d 38 32 20 29 3b 20 2f  case( i==82 ); /
32fcb 2a 20 52 45 4e 41 4d 45 20 2a 2f 0a 20 20 20 20  * RENAME */.    
32fcc 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38    testcase( i==8
32fcd 33 20 29 3b 20 2f 2a 20 41 46 54 45 52 20 2a 2f  3 ); /* AFTER */
32fce 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32fcf 20 69 3d 3d 38 34 20 29 3b 20 2f 2a 20 52 45 50   i==84 ); /* REP
32fd0 4c 41 43 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  LACE */.      te
32fd1 73 74 63 61 73 65 28 20 69 3d 3d 38 35 20 29 3b  stcase( i==85 );
32fd2 20 2f 2a 20 41 4e 44 20 2a 2f 0a 20 20 20 20 20   /* AND */.     
32fd3 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 36   testcase( i==86
32fd4 20 29 3b 20 2f 2a 20 44 45 46 41 55 4c 54 20 2a   ); /* DEFAULT *
32fd5 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32fd6 28 20 69 3d 3d 38 37 20 29 3b 20 2f 2a 20 41 55  ( i==87 ); /* AU
32fd7 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20  TOINCREMENT */. 
32fd8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32fd9 3d 3d 38 38 20 29 3b 20 2f 2a 20 54 4f 20 2a 2f  ==88 ); /* TO */
32fda 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32fdb 20 69 3d 3d 38 39 20 29 3b 20 2f 2a 20 49 4e 20   i==89 ); /* IN 
32fdc 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32fdd 65 28 20 69 3d 3d 39 30 20 29 3b 20 2f 2a 20 43  e( i==90 ); /* C
32fde 41 53 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  AST */.      tes
32fdf 74 63 61 73 65 28 20 69 3d 3d 39 31 20 29 3b 20  tcase( i==91 ); 
32fe0 2f 2a 20 43 4f 4c 55 4d 4e 20 2a 2f 0a 20 20 20  /* COLUMN */.   
32fe1 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32fe2 39 32 20 29 3b 20 2f 2a 20 43 4f 4d 4d 49 54 20  92 ); /* COMMIT 
32fe3 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32fe4 65 28 20 69 3d 3d 39 33 20 29 3b 20 2f 2a 20 43  e( i==93 ); /* C
32fe5 4f 4e 46 4c 49 43 54 20 2a 2f 0a 20 20 20 20 20  ONFLICT */.     
32fe6 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 34   testcase( i==94
32fe7 20 29 3b 20 2f 2a 20 43 52 4f 53 53 20 2a 2f 0a   ); /* CROSS */.
32fe8 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32fe9 69 3d 3d 39 35 20 29 3b 20 2f 2a 20 43 55 52 52  i==95 ); /* CURR
32fea 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 20 2a 2f  ENT_TIMESTAMP */
32feb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32fec 20 69 3d 3d 39 36 20 29 3b 20 2f 2a 20 43 55 52   i==96 ); /* CUR
32fed 52 45 4e 54 5f 54 49 4d 45 20 2a 2f 0a 20 20 20  RENT_TIME */.   
32fee 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32fef 39 37 20 29 3b 20 2f 2a 20 50 52 49 4d 41 52 59  97 ); /* PRIMARY
32ff0 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32ff1 73 65 28 20 69 3d 3d 39 38 20 29 3b 20 2f 2a 20  se( i==98 ); /* 
32ff2 44 45 46 45 52 52 45 44 20 2a 2f 0a 20 20 20 20  DEFERRED */.    
32ff3 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39    testcase( i==9
32ff4 39 20 29 3b 20 2f 2a 20 44 49 53 54 49 4e 43 54  9 ); /* DISTINCT
32ff5 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32ff6 73 65 28 20 69 3d 3d 31 30 30 20 29 3b 20 2f 2a  se( i==100 ); /*
32ff7 20 49 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73   IS */.      tes
32ff8 74 63 61 73 65 28 20 69 3d 3d 31 30 31 20 29 3b  tcase( i==101 );
32ff9 20 2f 2a 20 44 52 4f 50 20 2a 2f 0a 20 20 20 20   /* DROP */.    
32ffa 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
32ffb 30 32 20 29 3b 20 2f 2a 20 46 41 49 4c 20 2a 2f  02 ); /* FAIL */
32ffc 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32ffd 20 69 3d 3d 31 30 33 20 29 3b 20 2f 2a 20 46 52   i==103 ); /* FR
32ffe 4f 4d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  OM */.      test
32fff 63 61 73 65 28 20 69 3d 3d 31 30 34 20 29 3b 20  case( i==104 ); 
33000 2f 2a 20 46 55 4c 4c 20 2a 2f 0a 20 20 20 20 20  /* FULL */.     
33001 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
33002 35 20 29 3b 20 2f 2a 20 47 4c 4f 42 20 2a 2f 0a  5 ); /* GLOB */.
33003 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33004 69 3d 3d 31 30 36 20 29 3b 20 2f 2a 20 42 59 20  i==106 ); /* BY 
33005 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33006 65 28 20 69 3d 3d 31 30 37 20 29 3b 20 2f 2a 20  e( i==107 ); /* 
33007 49 46 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IF */.      test
33008 63 61 73 65 28 20 69 3d 3d 31 30 38 20 29 3b 20  case( i==108 ); 
33009 2f 2a 20 49 53 4e 55 4c 4c 20 2a 2f 0a 20 20 20  /* ISNULL */.   
3300a 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3300b 31 30 39 20 29 3b 20 2f 2a 20 4f 52 44 45 52 20  109 ); /* ORDER 
3300c 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3300d 65 28 20 69 3d 3d 31 31 30 20 29 3b 20 2f 2a 20  e( i==110 ); /* 
3300e 52 45 53 54 52 49 43 54 20 2a 2f 0a 20 20 20 20  RESTRICT */.    
3300f 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
33010 31 31 20 29 3b 20 2f 2a 20 4f 55 54 45 52 20 2a  11 ); /* OUTER *
33011 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
33012 28 20 69 3d 3d 31 31 32 20 29 3b 20 2f 2a 20 52  ( i==112 ); /* R
33013 49 47 48 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  IGHT */.      te
33014 73 74 63 61 73 65 28 20 69 3d 3d 31 31 33 20 29  stcase( i==113 )
33015 3b 20 2f 2a 20 52 4f 4c 4c 42 41 43 4b 20 2a 2f  ; /* ROLLBACK */
33016 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33017 20 69 3d 3d 31 31 34 20 29 3b 20 2f 2a 20 52 4f   i==114 ); /* RO
33018 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  W */.      testc
33019 61 73 65 28 20 69 3d 3d 31 31 35 20 29 3b 20 2f  ase( i==115 ); /
3301a 2a 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20 20  * UNION */.     
3301b 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
3301c 36 20 29 3b 20 2f 2a 20 55 53 49 4e 47 20 2a 2f  6 ); /* USING */
3301d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3301e 20 69 3d 3d 31 31 37 20 29 3b 20 2f 2a 20 56 41   i==117 ); /* VA
3301f 43 55 55 4d 20 2a 2f 0a 20 20 20 20 20 20 74 65  CUUM */.      te
33020 73 74 63 61 73 65 28 20 69 3d 3d 31 31 38 20 29  stcase( i==118 )
33021 3b 20 2f 2a 20 56 49 45 57 20 2a 2f 0a 20 20 20  ; /* VIEW */.   
33022 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
33023 31 31 39 20 29 3b 20 2f 2a 20 49 4e 49 54 49 41  119 ); /* INITIA
33024 4c 4c 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  LLY */.      tes
33025 74 63 61 73 65 28 20 69 3d 3d 31 32 30 20 29 3b  tcase( i==120 );
33026 20 2f 2a 20 41 4c 4c 20 2a 2f 0a 20 20 20 20 20   /* ALL */.     
33027 20 72 65 74 75 72 6e 20 61 43 6f 64 65 5b 69 5d   return aCode[i]
33028 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
33029 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d 0a 53 51  turn TK_ID;.}.SQ
3302a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
3302b 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43   sqlite3KeywordC
3302c 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ode(const unsign
3302d 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  ed char *z, int 
3302e 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 6b 65 79  n){.  return key
3302f 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a 29  wordCode((char*)
33030 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  z, n);.}../*****
33031 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
33032 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 2a   keywordhash.h *
33033 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33034 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33035 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
33036 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
33037 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
33038 66 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65 6e 69  ft off in tokeni
33039 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ze.c ***********
3303a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a  ********/.../*.*
3303b 2a 20 49 66 20 58 20 69 73 20 61 20 63 68 61 72  * If X is a char
3303c 61 63 74 65 72 20 74 68 61 74 20 63 61 6e 20 62  acter that can b
3303d 65 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64 65  e used in an ide
3303e 6e 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20  ntifier then.** 
3303f 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62  IdChar(X) will b
33040 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  e true.  Otherwi
33041 73 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a  se it is false..
33042 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c  **.** For ASCII,
33043 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77   any character w
33044 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64  ith the high-ord
33045 65 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a  er bit set is.**
33046 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69   allowed in an i
33047 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20  dentifier.  For 
33048 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 73  7-bit characters
33049 2c 20 0a 2a 2a 20 73 71 6c 69 74 65 33 49 73 49  , .** sqlite3IsI
3304a 64 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62 65  dChar[X] must be
3304b 20 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 45 42   1..**.** For EB
3304c 43 44 49 43 2c 20 74 68 65 20 72 75 6c 65 73 20  CDIC, the rules 
3304d 61 72 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78  are more complex
3304e 20 62 75 74 20 68 61 76 65 20 74 68 65 20 73 61   but have the sa
3304f 6d 65 0a 2a 2a 20 65 6e 64 20 72 65 73 75 6c 74  me.** end result
33050 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23  ..**.** Ticket #
33051 31 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73  1066.  the SQL s
33052 74 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74  tandard does not
33053 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68   allow '$' in th
33054 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69  e.** middle of i
33055 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20  dentfiers.  But 
33056 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65  many SQL impleme
33057 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a  ntations do. .**
33058 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c   SQLite will all
33059 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69  ow '$' in identi
3305a 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74  fiers for compat
3305b 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20  ibility..** But 
3305c 74 68 65 20 66 65 61 74 75 72 65 20 69 73 20 75  the feature is u
3305d 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a  ndocumented..*/.
3305e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
3305f 43 49 49 0a 23 64 65 66 69 6e 65 20 49 64 43 68  CII.#define IdCh
33060 61 72 28 43 29 20 20 28 28 73 71 6c 69 74 65 33  ar(C)  ((sqlite3
33061 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
33062 65 64 20 63 68 61 72 29 43 5d 26 30 78 34 36 29  ed char)C]&0x46)
33063 21 3d 30 29 0a 23 65 6e 64 69 66 0a 23 69 66 64  !=0).#endif.#ifd
33064 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ef SQLITE_EBCDIC
33065 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
33066 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
33067 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72  e3IsEbcdicIdChar
33068 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20  [] = {./* x0 x1 
33069 78 32 20 78 33 20 78 34 20 78 35 20 78 36 20 78  x2 x3 x4 x5 x6 x
3306a 37 20 78 38 20 78 39 20 78 41 20 78 42 20 78 43  7 x8 x9 xA xB xC
3306b 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20 20   xD xE xF */.   
3306c 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 0, 1, 1, 1, 
3306d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
3306e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
3306f 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 30    /* 4x */.    0
33070 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
33071 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
33072 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20  1, 0, 0, 0, 0,  
33073 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 5x */.    0, 
33074 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
33075 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
33076 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 1, 0, 0,  /*
33077 20 36 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c   6x */.    0, 1,
33078 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33079 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 1, 0, 0, 0, 0
3307a 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37  , 0, 0, 0,  /* 7
3307b 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31  x */.    0, 1, 1
3307c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3307d 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20   1, 1, 0, 0, 1, 
3307e 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 38 78 20  1, 1, 0,  /* 8x 
3307f 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20  */.    0, 1, 1, 
33080 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
33081 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c  , 1, 0, 0, 1, 0,
33082 20 31 2c 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f   1, 0,  /* 9x */
33083 0a 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 31 2c  .    1, 0, 1, 1,
33084 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33085 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
33086 2c 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20  , 0,  /* Ax */. 
33087 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30     0, 0, 0, 0, 0
33088 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
33089 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
3308a 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20  0,  /* Bx */.   
3308b 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
3308c 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
3308d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3308e 20 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 30    /* Cx */.    0
3308f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
33090 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
33091 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20  1, 1, 1, 1, 1,  
33092 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* Dx */.    0, 
33093 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
33094 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c  , 1, 1, 1, 0, 1,
33095 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
33096 20 45 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c   Ex */.    1, 1,
33097 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33098 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
33099 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 46  , 1, 1, 0,  /* F
3309a 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  x */.};.#define 
3309b 49 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d  IdChar(C)  (((c=
3309c 43 29 3e 3d 30 78 34 32 20 26 26 20 73 71 6c 69  C)>=0x42 && sqli
3309d 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61  te3IsEbcdicIdCha
3309e 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e 64  r[c-0x40])).#end
3309f 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  if.../*.** Retur
330a0 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  n the length of 
330a1 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 62  the token that b
330a2 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e 20 0a  egins at z[0]. .
330a3 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b  ** Store the tok
330a4 65 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f 6b 65  en type in *toke
330a5 6e 54 79 70 65 20 62 65 66 6f 72 65 20 72 65 74  nType before ret
330a6 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  urning..*/.SQLIT
330a7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
330a8 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f  lite3GetToken(co
330a9 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
330aa 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e  r *z, int *token
330ab 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20  Type){.  int i, 
330ac 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a 7a 20  c;.  switch( *z 
330ad 29 7b 0a 20 20 20 20 63 61 73 65 20 27 20 27 3a  ){.    case ' ':
330ae 20 63 61 73 65 20 27 5c 74 27 3a 20 63 61 73 65   case '\t': case
330af 20 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c 66 27   '\n': case '\f'
330b0 3a 20 63 61 73 65 20 27 5c 72 27 3a 20 7b 0a 20  : case '\r': {. 
330b1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
330b2 5b 30 5d 3d 3d 27 20 27 20 29 3b 0a 20 20 20 20  [0]==' ' );.    
330b3 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
330b4 3d 3d 27 5c 74 27 20 29 3b 0a 20 20 20 20 20 20  =='\t' );.      
330b5 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
330b6 27 5c 6e 27 20 29 3b 0a 20 20 20 20 20 20 74 65  '\n' );.      te
330b7 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 5c  stcase( z[0]=='\
330b8 66 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  f' );.      test
330b9 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 5c 72 27  case( z[0]=='\r'
330ba 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   );.      for(i=
330bb 31 3b 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  1; sqlite3Isspac
330bc 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a  e(z[i]); i++){}.
330bd 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
330be 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20   = TK_SPACE;.   
330bf 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
330c0 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2d 27 3a   }.    case '-':
330c1 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31   {.      if( z[1
330c2 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20  ]=='-' ){.      
330c3 20 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b    for(i=2; (c=z[
330c4 69 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 6e  i])!=0 && c!='\n
330c5 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20  '; i++){}.      
330c6 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
330c7 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20 20  K_SPACE;.       
330c8 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20   return i;.     
330c9 20 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   }.      *tokenT
330ca 79 70 65 20 3d 20 54 4b 5f 4d 49 4e 55 53 3b 0a  ype = TK_MINUS;.
330cb 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
330cc 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
330cd 28 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  (': {.      *tok
330ce 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 50 3b 0a  enType = TK_LP;.
330cf 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
330d0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
330d1 29 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  )': {.      *tok
330d2 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 50 3b 0a  enType = TK_RP;.
330d3 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
330d4 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
330d5 3b 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  ;': {.      *tok
330d6 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 45 4d 49  enType = TK_SEMI
330d7 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
330d8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
330d9 20 27 2b 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74   '+': {.      *t
330da 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 50 4c  okenType = TK_PL
330db 55 53 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  US;.      return
330dc 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
330dd 73 65 20 27 2a 27 3a 20 7b 0a 20 20 20 20 20 20  se '*': {.      
330de 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
330df 53 54 41 52 3b 0a 20 20 20 20 20 20 72 65 74 75  STAR;.      retu
330e0 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
330e1 63 61 73 65 20 27 2f 27 3a 20 7b 0a 20 20 20 20  case '/': {.    
330e2 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 2a 27 20    if( z[1]!='*' 
330e3 7c 7c 20 7a 5b 32 5d 3d 3d 30 20 29 7b 0a 20 20  || z[2]==0 ){.  
330e4 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
330e5 20 3d 20 54 4b 5f 53 4c 41 53 48 3b 0a 20 20 20   = TK_SLASH;.   
330e6 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
330e7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
330e8 28 69 3d 33 2c 20 63 3d 7a 5b 32 5d 3b 20 28 63  (i=3, c=z[2]; (c
330e9 21 3d 27 2a 27 20 7c 7c 20 7a 5b 69 5d 21 3d 27  !='*' || z[i]!='
330ea 2f 27 29 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21  /') && (c=z[i])!
330eb 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  =0; i++){}.     
330ec 20 69 66 28 20 63 20 29 20 69 2b 2b 3b 0a 20 20   if( c ) i++;.  
330ed 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
330ee 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20 20   TK_SPACE;.     
330ef 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d   return i;.    }
330f0 0a 20 20 20 20 63 61 73 65 20 27 25 27 3a 20 7b  .    case '%': {
330f1 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
330f2 65 20 3d 20 54 4b 5f 52 45 4d 3b 0a 20 20 20 20  e = TK_REM;.    
330f3 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
330f4 7d 0a 20 20 20 20 63 61 73 65 20 27 3d 27 3a 20  }.    case '=': 
330f5 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
330f6 70 65 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 20  pe = TK_EQ;.    
330f7 20 20 72 65 74 75 72 6e 20 31 20 2b 20 28 7a 5b    return 1 + (z[
330f8 31 5d 3d 3d 27 3d 27 29 3b 0a 20 20 20 20 7d 0a  1]=='=');.    }.
330f9 20 20 20 20 63 61 73 65 20 27 3c 27 3a 20 7b 0a      case '<': {.
330fa 20 20 20 20 20 20 69 66 28 20 28 63 3d 7a 5b 31        if( (c=z[1
330fb 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20  ])=='=' ){.     
330fc 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
330fd 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 20 20 72  TK_LE;.        r
330fe 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d  eturn 2;.      }
330ff 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3e 27 20  else if( c=='>' 
33100 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  ){.        *toke
33101 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20  nType = TK_NE;. 
33102 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
33103 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
33104 20 63 3d 3d 27 3c 27 20 29 7b 0a 20 20 20 20 20   c=='<' ){.     
33105 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
33106 54 4b 5f 4c 53 48 49 46 54 3b 0a 20 20 20 20 20  TK_LSHIFT;.     
33107 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20     return 2;.   
33108 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
33109 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
3310a 4b 5f 4c 54 3b 0a 20 20 20 20 20 20 20 20 72 65  K_LT;.        re
3310b 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
3310c 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
3310d 3e 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  >': {.      if( 
3310e 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b  (c=z[1])=='=' ){
3310f 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
33110 79 70 65 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20  ype = TK_GE;.   
33111 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
33112 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
33113 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20 20  =='>' ){.       
33114 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
33115 5f 52 53 48 49 46 54 3b 0a 20 20 20 20 20 20 20  _RSHIFT;.       
33116 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20   return 2;.     
33117 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
33118 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
33119 47 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  GT;.        retu
3311a 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
3311b 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 21 27    }.    case '!'
3311c 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  : {.      if( z[
3311d 31 5d 21 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20  1]!='=' ){.     
3311e 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3311f 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20  TK_ILLEGAL;.    
33120 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
33121 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
33122 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
33123 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20 72  TK_NE;.        r
33124 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d  eturn 2;.      }
33125 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
33126 27 7c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  '|': {.      if(
33127 20 7a 5b 31 5d 21 3d 27 7c 27 20 29 7b 0a 20 20   z[1]!='|' ){.  
33128 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
33129 20 3d 20 54 4b 5f 42 49 54 4f 52 3b 0a 20 20 20   = TK_BITOR;.   
3312a 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
3312b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3312c 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
3312d 20 54 4b 5f 43 4f 4e 43 41 54 3b 0a 20 20 20 20   TK_CONCAT;.    
3312e 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
3312f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
33130 63 61 73 65 20 27 2c 27 3a 20 7b 0a 20 20 20 20  case ',': {.    
33131 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
33132 4b 5f 43 4f 4d 4d 41 3b 0a 20 20 20 20 20 20 72  K_COMMA;.      r
33133 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
33134 20 20 20 63 61 73 65 20 27 26 27 3a 20 7b 0a 20     case '&': {. 
33135 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
33136 3d 20 54 4b 5f 42 49 54 41 4e 44 3b 0a 20 20 20  = TK_BITAND;.   
33137 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
33138 20 7d 0a 20 20 20 20 63 61 73 65 20 27 7e 27 3a   }.    case '~':
33139 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
3313a 79 70 65 20 3d 20 54 4b 5f 42 49 54 4e 4f 54 3b  ype = TK_BITNOT;
3313b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
3313c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
3313d 27 60 27 3a 0a 20 20 20 20 63 61 73 65 20 27 5c  '`':.    case '\
3313e 27 27 3a 0a 20 20 20 20 63 61 73 65 20 27 22 27  '':.    case '"'
3313f 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65  : {.      int de
33140 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20  lim = z[0];.    
33141 20 20 74 65 73 74 63 61 73 65 28 20 64 65 6c 69    testcase( deli
33142 6d 3d 3d 27 60 27 20 29 3b 0a 20 20 20 20 20 20  m=='`' );.      
33143 74 65 73 74 63 61 73 65 28 20 64 65 6c 69 6d 3d  testcase( delim=
33144 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 74  ='\'' );.      t
33145 65 73 74 63 61 73 65 28 20 64 65 6c 69 6d 3d 3d  estcase( delim==
33146 27 22 27 20 29 3b 0a 20 20 20 20 20 20 66 6f 72  '"' );.      for
33147 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d  (i=1; (c=z[i])!=
33148 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  0; i++){.       
33149 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29 7b   if( c==delim ){
3314a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
3314b 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b 0a  [i+1]==delim ){.
3314c 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
3314d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
3314e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  {.            br
3314f 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
33150 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
33151 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d   }.      if( c==
33152 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20 20  '\'' ){.        
33153 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
33154 53 54 52 49 4e 47 3b 0a 20 20 20 20 20 20 20 20  STRING;.        
33155 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20  return i+1;.    
33156 20 20 7d 65 6c 73 65 20 69 66 28 20 63 21 3d 30    }else if( c!=0
33157 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
33158 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 44 3b 0a  enType = TK_ID;.
33159 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69          return i
3315a 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  +1;.      }else{
3315b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
3315c 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c  ype = TK_ILLEGAL
3315d 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
3315e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   i;.      }.    
3315f 7d 0a 20 20 20 20 63 61 73 65 20 27 2e 27 3a 20  }.    case '.': 
33160 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
33161 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
33162 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28 20 21  OINT.      if( !
33163 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a  sqlite3Isdigit(z
33164 5b 31 5d 29 20 29 0a 23 65 6e 64 69 66 0a 20 20  [1]) ).#endif.  
33165 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 2a 74      {.        *t
33166 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 44 4f  okenType = TK_DO
33167 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  T;.        retur
33168 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
33169 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 65 78     /* If the nex
3316a 74 20 63 68 61 72 61 63 74 65 72 20 69 73 20 61  t character is a
3316b 20 64 69 67 69 74 2c 20 74 68 69 73 20 69 73 20   digit, this is 
3316c 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  a floating point
3316d 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72  .      ** number
3316e 20 74 68 61 74 20 62 65 67 69 6e 73 20 77 69 74   that begins wit
3316f 68 20 22 2e 22 2e 20 20 46 61 6c 6c 20 74 68 72  h ".".  Fall thr
33170 75 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20  u into the next 
33171 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  case */.    }.  
33172 20 20 63 61 73 65 20 27 30 27 3a 20 63 61 73 65    case '0': case
33173 20 27 31 27 3a 20 63 61 73 65 20 27 32 27 3a 20   '1': case '2': 
33174 63 61 73 65 20 27 33 27 3a 20 63 61 73 65 20 27  case '3': case '
33175 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27  4':.    case '5'
33176 3a 20 63 61 73 65 20 27 36 27 3a 20 63 61 73 65  : case '6': case
33177 20 27 37 27 3a 20 63 61 73 65 20 27 38 27 3a 20   '7': case '8': 
33178 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20  case '9': {.    
33179 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
3317a 3d 3d 27 30 27 20 29 3b 20 20 74 65 73 74 63 61  =='0' );  testca
3317b 73 65 28 20 7a 5b 30 5d 3d 3d 27 31 27 20 29 3b  se( z[0]=='1' );
3317c 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
3317d 3d 3d 27 32 27 20 29 3b 0a 20 20 20 20 20 20 74  =='2' );.      t
3317e 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
3317f 33 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28  3' );  testcase(
33180 20 7a 5b 30 5d 3d 3d 27 34 27 20 29 3b 20 20 74   z[0]=='4' );  t
33181 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
33182 35 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  5' );.      test
33183 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 36 27 20  case( z[0]=='6' 
33184 29 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b  );  testcase( z[
33185 30 5d 3d 3d 27 37 27 20 29 3b 20 20 74 65 73 74  0]=='7' );  test
33186 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 38 27 20  case( z[0]=='8' 
33187 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
33188 65 28 20 7a 5b 30 5d 3d 3d 27 39 27 20 29 3b 0a  e( z[0]=='9' );.
33189 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3318a 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20   = TK_INTEGER;. 
3318b 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71       for(i=0; sq
3318c 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69  lite3Isdigit(z[i
3318d 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69 66 6e 64  ]); i++){}.#ifnd
3318e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
3318f 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
33190 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2e      if( z[i]=='.
33191 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 2b 2b  ' ){.        i++
33192 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
33193 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
33194 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d 0a  z[i]) ){ i++; }.
33195 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
33196 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a 20  pe = TK_FLOAT;. 
33197 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
33198 20 28 7a 5b 69 5d 3d 3d 27 65 27 20 7c 7c 20 7a   (z[i]=='e' || z
33199 5b 69 5d 3d 3d 27 45 27 29 20 26 26 0a 20 20 20  [i]=='E') &&.   
3319a 20 20 20 20 20 20 20 20 28 20 73 71 6c 69 74 65          ( sqlite
3319b 33 49 73 64 69 67 69 74 28 7a 5b 69 2b 31 5d 29  3Isdigit(z[i+1])
3319c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c   .            ||
3319d 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b 27 20 7c   ((z[i+1]=='+' |
3319e 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27 29 20 26  | z[i+1]=='-') &
3319f 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  & sqlite3Isdigit
331a0 28 7a 5b 69 2b 32 5d 29 29 0a 20 20 20 20 20 20  (z[i+2])).      
331a1 20 20 20 20 20 29 0a 20 20 20 20 20 20 29 7b 0a       ).      ){.
331a2 20 20 20 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a          i += 2;.
331a3 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 73          while( s
331a4 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b  qlite3Isdigit(z[
331a5 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20  i]) ){ i++; }.  
331a6 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
331a7 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a 20 20 20   = TK_FLOAT;.   
331a8 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
331a9 20 20 77 68 69 6c 65 28 20 49 64 43 68 61 72 28    while( IdChar(
331aa 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20  z[i]) ){.       
331ab 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
331ac 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20  _ILLEGAL;.      
331ad 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20    i++;.      }. 
331ae 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
331af 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 5b     }.    case '[
331b0 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ': {.      for(i
331b1 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27  =1, c=z[0]; c!='
331b2 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d  ]' && (c=z[i])!=
331b3 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20  0; i++){}.      
331b4 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 63 3d 3d  *tokenType = c==
331b5 27 5d 27 20 3f 20 54 4b 5f 49 44 20 3a 20 54 4b  ']' ? TK_ID : TK
331b6 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20  _ILLEGAL;.      
331b7 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
331b8 20 20 20 20 63 61 73 65 20 27 3f 27 3a 20 7b 0a      case '?': {.
331b9 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
331ba 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a   = TK_VARIABLE;.
331bb 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 73        for(i=1; s
331bc 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b  qlite3Isdigit(z[
331bd 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  i]); i++){}.    
331be 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
331bf 7d 0a 20 20 20 20 63 61 73 65 20 27 23 27 3a 20  }.    case '#': 
331c0 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  {.      for(i=1;
331c1 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
331c2 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20  z[i]); i++){}.  
331c3 20 20 20 20 69 66 28 20 69 3e 31 20 29 7b 0a 20      if( i>1 ){. 
331c4 20 20 20 20 20 20 20 2f 2a 20 50 61 72 61 6d 65         /* Parame
331c5 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ters of the form
331c6 20 23 4e 4e 4e 20 28 77 68 65 72 65 20 4e 4e 4e   #NNN (where NNN
331c7 20 69 73 20 61 20 6e 75 6d 62 65 72 29 20 61 72   is a number) ar
331c8 65 20 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a  e used.        *
331c9 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20  * internally by 
331ca 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
331cb 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  se.  */.        
331cc 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
331cd 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20  REGISTER;.      
331ce 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
331cf 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c    }.      /* Fal
331d0 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74  l through into t
331d1 68 65 20 6e 65 78 74 20 63 61 73 65 20 69 66 20  he next case if 
331d2 74 68 65 20 27 23 27 20 69 73 20 6e 6f 74 20 66  the '#' is not f
331d3 6f 6c 6c 6f 77 65 64 20 62 79 0a 20 20 20 20 20  ollowed by.     
331d4 20 2a 2a 20 61 20 64 69 67 69 74 2e 20 54 72 79   ** a digit. Try
331d5 20 74 6f 20 6d 61 74 63 68 20 23 41 41 41 41 20   to match #AAAA 
331d6 77 68 65 72 65 20 41 41 41 41 20 69 73 20 61 20  where AAAA is a 
331d7 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 2e 20  parameter name. 
331d8 2a 2f 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  */.    }.#ifndef
331d9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c   SQLITE_OMIT_TCL
331da 5f 56 41 52 49 41 42 4c 45 0a 20 20 20 20 63 61  _VARIABLE.    ca
331db 73 65 20 27 24 27 3a 0a 23 65 6e 64 69 66 0a 20  se '$':.#endif. 
331dc 20 20 20 63 61 73 65 20 27 40 27 3a 20 20 2f 2a     case '@':  /*
331dd 20 46 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69   For compatibili
331de 74 79 20 77 69 74 68 20 4d 53 20 53 51 4c 20 53  ty with MS SQL S
331df 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 63 61 73  erver */.    cas
331e0 65 20 27 3a 27 3a 20 7b 0a 20 20 20 20 20 20 69  e ':': {.      i
331e1 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  nt n = 0;.      
331e2 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
331e3 27 24 27 20 29 3b 20 20 74 65 73 74 63 61 73 65  '$' );  testcase
331e4 28 20 7a 5b 30 5d 3d 3d 27 40 27 20 29 3b 20 20  ( z[0]=='@' );  
331e5 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
331e6 27 3a 27 20 29 3b 0a 20 20 20 20 20 20 2a 74 6f  ':' );.      *to
331e7 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41 52  kenType = TK_VAR
331e8 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f 72  IABLE;.      for
331e9 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d  (i=1; (c=z[i])!=
331ea 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  0; i++){.       
331eb 20 69 66 28 20 49 64 43 68 61 72 28 63 29 20 29   if( IdChar(c) )
331ec 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b  {.          n++;
331ed 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
331ee 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c  OMIT_TCL_VARIABL
331ef 45 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  E.        }else 
331f0 69 66 28 20 63 3d 3d 27 28 27 20 26 26 20 6e 3e  if( c=='(' && n>
331f1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64  0 ){.          d
331f2 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  o{.            i
331f3 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 77  ++;.          }w
331f4 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d 29 21 3d  hile( (c=z[i])!=
331f5 30 20 26 26 20 21 73 71 6c 69 74 65 33 49 73 73  0 && !sqlite3Iss
331f6 70 61 63 65 28 63 29 20 26 26 20 63 21 3d 27 29  pace(c) && c!=')
331f7 27 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ' );.          i
331f8 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a 20 20 20  f( c==')' ){.   
331f9 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20           i++;.  
331fa 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
331fb 20 20 20 20 20 20 20 20 20 20 20 2a 74 6f 6b 65             *toke
331fc 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47  nType = TK_ILLEG
331fd 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  AL;.          }.
331fe 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
331ff 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
33200 66 28 20 63 3d 3d 27 3a 27 20 26 26 20 7a 5b 69  f( c==':' && z[i
33201 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20  +1]==':' ){.    
33202 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65 6e 64 69        i++;.#endi
33203 66 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  f.        }else{
33204 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
33205 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
33206 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 3d    }.      if( n=
33207 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65 20  =0 ) *tokenType 
33208 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20  = TK_ILLEGAL;.  
33209 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
3320a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
3320b 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
3320c 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 27 78  ERAL.    case 'x
3320d 27 3a 20 63 61 73 65 20 27 58 27 3a 20 7b 0a 20  ': case 'X': {. 
3320e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
3320f 5b 30 5d 3d 3d 27 78 27 20 29 3b 20 74 65 73 74  [0]=='x' ); test
33210 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 58 27 20  case( z[0]=='X' 
33211 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31  );.      if( z[1
33212 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20  ]=='\'' ){.     
33213 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
33214 54 4b 5f 42 4c 4f 42 3b 0a 20 20 20 20 20 20 20  TK_BLOB;.       
33215 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69   for(i=2; (c=z[i
33216 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 27 27  ])!=0 && c!='\''
33217 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
33218 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
33219 78 64 69 67 69 74 28 63 29 20 29 7b 0a 20 20 20  xdigit(c) ){.   
3321a 20 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54           *tokenT
3321b 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c  ype = TK_ILLEGAL
3321c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
3321d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3321e 69 66 28 20 69 25 32 20 7c 7c 20 21 63 20 29 20  if( i%2 || !c ) 
3321f 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
33220 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20  ILLEGAL;.       
33221 20 69 66 28 20 63 20 29 20 69 2b 2b 3b 0a 20 20   if( c ) i++;.  
33222 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
33223 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a        }.      /*
33224 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20   Otherwise fall 
33225 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
33226 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20  ext case */.    
33227 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 65 66  }.#endif.    def
33228 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 66  ault: {.      if
33229 28 20 21 49 64 43 68 61 72 28 2a 7a 29 20 29 7b  ( !IdChar(*z) ){
3322a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3322b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
3322c 72 28 69 3d 31 3b 20 49 64 43 68 61 72 28 7a 5b  r(i=1; IdChar(z[
3322d 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  i]); i++){}.    
3322e 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 6b    *tokenType = k
3322f 65 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72  eywordCode((char
33230 2a 29 7a 2c 20 69 29 3b 0a 20 20 20 20 20 20 72  *)z, i);.      r
33231 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20  eturn i;.    }. 
33232 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20   }.  *tokenType 
33233 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20  = TK_ILLEGAL;.  
33234 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
33235 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65  ** Run the parse
33236 72 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 53  r on the given S
33237 51 4c 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20  QL string.  The 
33238 70 61 72 73 65 72 20 73 74 72 75 63 74 75 72 65  parser structure
33239 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e   is.** passed in
3323a 2e 20 20 41 6e 20 53 51 4c 49 54 45 5f 20 73 74  .  An SQLITE_ st
3323b 61 74 75 73 20 63 6f 64 65 20 69 73 20 72 65 74  atus code is ret
3323c 75 72 6e 65 64 2e 20 20 49 66 20 61 6e 20 65 72  urned.  If an er
3323d 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68  ror occurs.** th
3323e 65 6e 20 61 6e 20 61 6e 64 20 61 74 74 65 6d 70  en an and attemp
3323f 74 20 69 73 20 6d 61 64 65 20 74 6f 20 77 72 69  t is made to wri
33240 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  te an error mess
33241 61 67 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d  age into .** mem
33242 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
33243 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
33244 28 29 20 61 6e 64 20 74 6f 20 6d 61 6b 65 20 2a  () and to make *
33245 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20 74  pzErrMsg point t
33246 6f 20 74 68 61 74 0a 2a 2a 20 65 72 72 6f 72 20  o that.** error 
33247 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49  message..*/.SQLI
33248 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
33249 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28  qlite3RunParser(
3324a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
3324b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
3324c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
3324d 29 7b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20  ){.  int nErr = 
3324e 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
3324f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
33250 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74  f errors encount
33251 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  ered */.  int i;
33252 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33253 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
33254 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 76  p counter */.  v
33255 6f 69 64 20 2a 70 45 6e 67 69 6e 65 3b 20 20 20  oid *pEngine;   
33256 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33257 2a 20 54 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65  * The LEMON-gene
33258 72 61 74 65 64 20 4c 41 4c 52 28 31 29 20 70 61  rated LALR(1) pa
33259 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f  rser */.  int to
3325a 6b 65 6e 54 79 70 65 3b 20 20 20 20 20 20 20 20  kenType;        
3325b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 79 70            /* typ
3325c 65 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74 6f  e of the next to
3325d 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73  ken */.  int las
3325e 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20 2d  tTokenParsed = -
3325f 31 3b 20 20 20 20 20 20 20 2f 2a 20 74 79 70 65  1;       /* type
33260 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   of the previous
33261 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 75 38 20 65   token */.  u8 e
33262 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 20  nableLookaside; 
33263 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
33264 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62  aved value of db
33265 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
33266 62 6c 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  bled */.  sqlite
33267 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
33268 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  db;       /* The
33269 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3326a 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  tion */.  int mx
3326b 53 71 6c 4c 65 6e 3b 20 20 20 20 20 20 20 20 20  SqlLen;         
3326c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
3326d 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e 20 53 51   length of an SQ
3326e 4c 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 0a 20 20  L string */...  
3326f 6d 78 53 71 6c 4c 65 6e 20 3d 20 64 62 2d 3e 61  mxSqlLen = db->a
33270 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
33271 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a  IT_SQL_LENGTH];.
33272 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65    if( db->active
33273 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20  VdbeCnt==0 ){.  
33274 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72    db->u1.isInter
33275 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 7d 0a  rupted = 0;.  }.
33276 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
33277 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 72  QLITE_OK;.  pPar
33278 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 7a 53 71 6c  se->zTail = zSql
33279 3b 0a 20 20 69 20 3d 20 30 3b 0a 20 20 61 73 73  ;.  i = 0;.  ass
3327a 65 72 74 28 20 70 7a 45 72 72 4d 73 67 21 3d 30  ert( pzErrMsg!=0
3327b 20 29 3b 0a 20 20 70 45 6e 67 69 6e 65 20 3d 20   );.  pEngine = 
3327c 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c  sqlite3ParserAll
3327d 6f 63 28 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a  oc((void*(*)(siz
3327e 65 5f 74 29 29 73 71 6c 69 74 65 33 4d 61 6c 6c  e_t))sqlite3Mall
3327f 6f 63 29 3b 0a 20 20 69 66 28 20 70 45 6e 67 69  oc);.  if( pEngi
33280 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d  ne==0 ){.    db-
33281 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
33282 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  1;.    return SQ
33283 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
33284 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
33285 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29  ->pNewTable==0 )
33286 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
33287 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3d  se->pNewTrigger=
33288 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
33289 70 50 61 72 73 65 2d 3e 6e 56 61 72 3d 3d 30 20  pParse->nVar==0 
3328a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
3328b 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3d 3d 30  rse->nVarExpr==0
3328c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
3328d 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c  arse->nVarExprAl
3328e 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  loc==0 );.  asse
3328f 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70 56 61  rt( pParse->apVa
33290 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20 20 65 6e  rExpr==0 );.  en
33291 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 20 3d 20  ableLookaside = 
33292 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
33293 6e 61 62 6c 65 64 3b 0a 20 20 69 66 28 20 64 62  nabled;.  if( db
33294 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61  ->lookaside.pSta
33295 72 74 20 29 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  rt ) db->lookasi
33296 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 31 3b  de.bEnabled = 1;
33297 0a 20 20 77 68 69 6c 65 28 20 21 64 62 2d 3e 6d  .  while( !db->m
33298 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 7a  allocFailed && z
33299 53 71 6c 5b 69 5d 21 3d 30 20 29 7b 0a 20 20 20  Sql[i]!=0 ){.   
3329a 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b   assert( i>=0 );
3329b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4c 61  .    pParse->sLa
3329c 73 74 54 6f 6b 65 6e 2e 7a 20 3d 20 26 7a 53 71  stToken.z = &zSq
3329d 6c 5b 69 5d 3b 0a 20 20 20 20 70 50 61 72 73 65  l[i];.    pParse
3329e 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 20 3d  ->sLastToken.n =
3329f 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
332a0 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  ((unsigned char*
332a1 29 26 7a 53 71 6c 5b 69 5d 2c 26 74 6f 6b 65 6e  )&zSql[i],&token
332a2 54 79 70 65 29 3b 0a 20 20 20 20 69 20 2b 3d 20  Type);.    i += 
332a3 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b  pParse->sLastTok
332a4 65 6e 2e 6e 3b 0a 20 20 20 20 69 66 28 20 69 3e  en.n;.    if( i>
332a5 6d 78 53 71 6c 4c 65 6e 20 29 7b 0a 20 20 20 20  mxSqlLen ){.    
332a6 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
332a7 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20  QLITE_TOOBIG;.  
332a8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
332a9 0a 20 20 20 20 73 77 69 74 63 68 28 20 74 6f 6b  .    switch( tok
332aa 65 6e 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20  enType ){.      
332ab 63 61 73 65 20 54 4b 5f 53 50 41 43 45 3a 20 7b  case TK_SPACE: {
332ac 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
332ad 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
332ae 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  d ){.          s
332af 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
332b0 50 61 72 73 65 2c 20 22 69 6e 74 65 72 72 75 70  Parse, "interrup
332b1 74 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  t");.          p
332b2 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
332b3 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  TE_INTERRUPT;.  
332b4 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
332b5 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20 20  rt_parse;.      
332b6 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
332b7 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
332b8 20 63 61 73 65 20 54 4b 5f 49 4c 4c 45 47 41 4c   case TK_ILLEGAL
332b9 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  : {.        sqli
332ba 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70  te3DbFree(db, *p
332bb 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
332bc 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
332bd 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
332be 20 22 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 74   "unrecognized t
332bf 6f 6b 65 6e 3a 20 5c 22 25 54 5c 22 22 2c 0a 20  oken: \"%T\"",. 
332c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332c1 20 20 20 20 20 20 20 26 70 50 61 72 73 65 2d 3e         &pParse->
332c2 73 4c 61 73 74 54 6f 6b 65 6e 29 3b 0a 20 20 20  sLastToken);.   
332c3 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20       nErr++;.   
332c4 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
332c5 70 61 72 73 65 3b 0a 20 20 20 20 20 20 7d 0a 20  parse;.      }. 
332c6 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4d       case TK_SEM
332c7 49 3a 20 7b 0a 20 20 20 20 20 20 20 20 70 50 61  I: {.        pPa
332c8 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53  rse->zTail = &zS
332c9 71 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 2f  ql[i];.        /
332ca 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f  * Fall thru into
332cb 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 61 73   the default cas
332cc 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  e */.      }.   
332cd 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
332ce 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72        sqlite3Par
332cf 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 74 6f 6b  ser(pEngine, tok
332d0 65 6e 54 79 70 65 2c 20 70 50 61 72 73 65 2d 3e  enType, pParse->
332d1 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72  sLastToken, pPar
332d2 73 65 29 3b 0a 20 20 20 20 20 20 20 20 6c 61 73  se);.        las
332d3 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20 74  tTokenParsed = t
332d4 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 20 20 20 20  okenType;.      
332d5 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63    if( pParse->rc
332d6 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
332d7 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62           goto ab
332d8 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20  ort_parse;.     
332d9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65     }.        bre
332da 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
332db 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f 70 61 72 73  }.  }.abort_pars
332dc 65 3a 0a 20 20 69 66 28 20 7a 53 71 6c 5b 69 5d  e:.  if( zSql[i]
332dd 3d 3d 30 20 26 26 20 6e 45 72 72 3d 3d 30 20 26  ==0 && nErr==0 &
332de 26 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51  & pParse->rc==SQ
332df 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
332e0 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73  f( lastTokenPars
332e1 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29 7b 0a 20  ed!=TK_SEMI ){. 
332e2 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73       sqlite3Pars
332e3 65 72 28 70 45 6e 67 69 6e 65 2c 20 54 4b 5f 53  er(pEngine, TK_S
332e4 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e 73 4c 61  EMI, pParse->sLa
332e5 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65 29  stToken, pParse)
332e6 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
332e7 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d  zTail = &zSql[i]
332e8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
332e9 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e  te3Parser(pEngin
332ea 65 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 73 4c  e, 0, pParse->sL
332eb 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65  astToken, pParse
332ec 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20 59 59  );.  }.#ifdef YY
332ed 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50  TRACKMAXSTACKDEP
332ee 54 48 0a 20 20 73 71 6c 69 74 65 33 53 74 61 74  TH.  sqlite3Stat
332ef 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41  usSet(SQLITE_STA
332f0 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b  TUS_PARSER_STACK
332f1 2c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  ,.      sqlite3P
332f2 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 70  arserStackPeak(p
332f3 45 6e 67 69 6e 65 29 0a 20 20 29 3b 0a 23 65 6e  Engine).  );.#en
332f4 64 69 66 20 2f 2a 20 59 59 44 45 42 55 47 20 2a  dif /* YYDEBUG *
332f5 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
332f6 72 46 72 65 65 28 70 45 6e 67 69 6e 65 2c 20 73  rFree(pEngine, s
332f7 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
332f8 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
332f9 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c  nabled = enableL
332fa 6f 6f 6b 61 73 69 64 65 3b 0a 20 20 69 66 28 20  ookaside;.  if( 
332fb 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
332fc 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
332fd 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
332fe 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  M;.  }.  if( pPa
332ff 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  rse->rc!=SQLITE_
33300 4f 4b 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63  OK && pParse->rc
33301 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26  !=SQLITE_DONE &&
33302 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
33303 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
33304 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 50 61  e3SetString(&pPa
33305 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  rse->zErrMsg, db
33306 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45  , "%s", sqlite3E
33307 72 72 53 74 72 28 70 50 61 72 73 65 2d 3e 72 63  rrStr(pParse->rc
33308 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ));.  }.  assert
33309 28 20 70 7a 45 72 72 4d 73 67 21 3d 30 20 29 3b  ( pzErrMsg!=0 );
3330a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 7a  .  if( pParse->z
3330b 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 2a 70  ErrMsg ){.    *p
3330c 7a 45 72 72 4d 73 67 20 3d 20 70 50 61 72 73 65  zErrMsg = pParse
3330d 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70  ->zErrMsg;.    p
3330e 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d  Parse->zErrMsg =
3330f 20 30 3b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b 0a   0;.    nErr++;.
33310 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65    }.  if( pParse
33311 2d 3e 70 56 64 62 65 20 26 26 20 70 50 61 72 73  ->pVdbe && pPars
33312 65 2d 3e 6e 45 72 72 3e 30 20 26 26 20 70 50 61  e->nErr>0 && pPa
33313 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
33314 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
33315 65 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  eDelete(pParse->
33316 70 56 64 62 65 29 3b 0a 20 20 20 20 70 50 61 72  pVdbe);.    pPar
33317 73 65 2d 3e 70 56 64 62 65 20 3d 20 30 3b 0a 20  se->pVdbe = 0;. 
33318 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
33319 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
3331a 43 48 45 0a 20 20 69 66 28 20 70 50 61 72 73 65  CHE.  if( pParse
3331b 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20  ->nested==0 ){. 
3331c 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
3331d 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 54 61  (db, pParse->aTa
3331e 62 6c 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 70 50  bleLock);.    pP
3331f 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
33320 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65   = 0;.    pParse
33321 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30  ->nTableLock = 0
33322 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66  ;.  }.#endif.#if
33323 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33324 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
33325 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
33326 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62  , pParse->apVtab
33327 4c 6f 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  Lock);.#endif.. 
33328 20 69 66 28 20 21 49 4e 5f 44 45 43 4c 41 52 45   if( !IN_DECLARE
33329 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 2f 2a 20  _VTAB ){.    /* 
3332a 49 66 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64  If the pParse->d
3332b 65 63 6c 61 72 65 56 74 61 62 20 66 6c 61 67 20  eclareVtab flag 
3332c 69 73 20 73 65 74 2c 20 64 6f 20 6e 6f 74 20 64  is set, do not d
3332d 65 6c 65 74 65 20 61 6e 79 20 74 61 62 6c 65 20  elete any table 
3332e 0a 20 20 20 20 2a 2a 20 73 74 72 75 63 74 75 72  .    ** structur
3332f 65 20 62 75 69 6c 74 20 75 70 20 69 6e 20 70 50  e built up in pP
33330 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e  arse->pNewTable.
33331 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64   The calling cod
33332 65 20 28 73 65 65 20 76 74 61 62 2e 63 29 0a 20  e (see vtab.c). 
33333 20 20 20 2a 2a 20 77 69 6c 6c 20 74 61 6b 65 20     ** will take 
33334 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 66  responsibility f
33335 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 54  or freeing the T
33336 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a  able structure..
33337 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
33338 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 50  e3DeleteTable(pP
33339 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29  arse->pNewTable)
3333a 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
3333b 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62  DeleteTrigger(db
3333c 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72  , pParse->pNewTr
3333d 69 67 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65  igger);.  sqlite
3333e 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72  3DbFree(db, pPar
3333f 73 65 2d 3e 61 70 56 61 72 45 78 70 72 29 3b 0a  se->apVarExpr);.
33340 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
33341 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c 69  db, pParse->aAli
33342 61 73 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 50  as);.  while( pP
33343 61 72 73 65 2d 3e 70 41 69 6e 63 20 29 7b 0a 20  arse->pAinc ){. 
33344 20 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a     AutoincInfo *
33345 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 41 69 6e  p = pParse->pAin
33346 63 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70  c;.    pParse->p
33347 41 69 6e 63 20 3d 20 70 2d 3e 70 4e 65 78 74 3b  Ainc = p->pNext;
33348 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
33349 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20  ee(db, p);.  }. 
3334a 20 77 68 69 6c 65 28 20 70 50 61 72 73 65 2d 3e   while( pParse->
3334b 70 5a 6f 6d 62 69 65 54 61 62 20 29 7b 0a 20 20  pZombieTab ){.  
3334c 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61    Table *p = pPa
3334d 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b  rse->pZombieTab;
3334e 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 5a 6f  .    pParse->pZo
3334f 6d 62 69 65 54 61 62 20 3d 20 70 2d 3e 70 4e 65  mbieTab = p->pNe
33350 78 74 5a 6f 6d 62 69 65 3b 0a 20 20 20 20 73 71  xtZombie;.    sq
33351 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
33352 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  (p);.  }.  if( n
33353 45 72 72 3e 30 20 26 26 20 70 50 61 72 73 65 2d  Err>0 && pParse-
33354 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc==SQLITE_OK )
33355 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63  {.    pParse->rc
33356 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
33357 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 45  .  }.  return nE
33358 72 72 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  rr;.}../********
33359 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 6f  ****** End of to
3335a 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  kenize.c *******
3335b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3335c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3335d 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
3335e 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
3335f 65 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a  e complete.c ***
33360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33362 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
33363 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
33364 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
33365 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
33366 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
33367 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
33368 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
33369 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
3336a 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
3336b 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
3336c 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
3336d 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
3336e 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
3336f 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
33370 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
33371 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
33372 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
33373 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
33374 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
33375 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
33376 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33377 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33378 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33379 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
3337a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f   An tokenizer fo
3337b 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  r SQL.**.** This
3337c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43   file contains C
3337d 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65   code that imple
3337e 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74 65  ments the sqlite
3337f 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 41 50 49  3_complete() API
33380 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 75  ..** This code u
33381 73 65 64 20 74 6f 20 62 65 20 70 61 72 74 20 6f  sed to be part o
33382 66 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e  f the tokenizer.
33383 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 20 20  c source file.  
33384 42 75 74 20 62 79 0a 2a 2a 20 73 65 70 61 72 61  But by.** separa
33385 74 69 6e 67 20 69 74 20 6f 75 74 2c 20 74 68 65  ting it out, the
33386 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 20 61 75   code will be au
33387 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6f 6d 69 74  tomatically omit
33388 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 74 61 74  ted from.** stat
33389 69 63 20 6c 69 6e 6b 73 20 74 68 61 74 20 64 6f  ic links that do
3338a 20 6e 6f 74 20 75 73 65 20 69 74 2e 0a 2a 2f 0a   not use it..*/.
3338b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3338c 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 0a 2f 2a  MIT_COMPLETE../*
3338d 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 65 66 69  .** This is defi
3338e 6e 65 64 20 69 6e 20 74 6f 6b 65 6e 69 7a 65 2e  ned in tokenize.
3338f 63 2e 20 20 57 65 20 6a 75 73 74 20 68 61 76 65  c.  We just have
33390 20 74 6f 20 69 6d 70 6f 72 74 20 74 68 65 20 64   to import the d
33391 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69  efinition..*/.#i
33392 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41  fndef SQLITE_AMA
33393 4c 47 41 4d 41 54 49 4f 4e 0a 23 69 66 64 65 66  LGAMATION.#ifdef
33394 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 64   SQLITE_ASCII.#d
33395 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29 20  efine IdChar(C) 
33396 20 28 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d   ((sqlite3CtypeM
33397 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61  ap[(unsigned cha
33398 72 29 43 5d 26 30 78 34 36 29 21 3d 30 29 0a 23  r)C]&0x46)!=0).#
33399 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
3339a 49 54 45 5f 45 42 43 44 49 43 0a 53 51 4c 49 54  ITE_EBCDIC.SQLIT
3339b 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
3339c 63 68 61 72 20 73 71 6c 69 74 65 33 49 73 45 62  char sqlite3IsEb
3339d 63 64 69 63 49 64 43 68 61 72 5b 5d 3b 0a 23 64  cdicIdChar[];.#d
3339e 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29 20  efine IdChar(C) 
3339f 20 28 28 28 63 3d 43 29 3e 3d 30 78 34 32 20 26   (((c=C)>=0x42 &
333a0 26 20 73 71 6c 69 74 65 33 49 73 45 62 63 64 69  & sqlite3IsEbcdi
333a1 63 49 64 43 68 61 72 5b 63 2d 30 78 34 30 5d 29  cIdChar[c-0x40])
333a2 29 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20  ).#endif.#endif 
333a3 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41  /* SQLITE_AMALGA
333a4 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  MATION */.../*.*
333a5 2a 20 54 6f 6b 65 6e 20 74 79 70 65 73 20 75 73  * Token types us
333a6 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ed by the sqlite
333a7 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75  3_complete() rou
333a8 74 69 6e 65 2e 20 20 53 65 65 20 74 68 65 20 68  tine.  See the h
333a9 65 61 64 65 72 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  eader.** comment
333aa 73 20 6f 6e 20 74 68 61 74 20 70 72 6f 63 65 64  s on that proced
333ab 75 72 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ure for addition
333ac 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
333ad 2a 2f 0a 23 64 65 66 69 6e 65 20 74 6b 53 45 4d  */.#define tkSEM
333ae 49 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 74  I    0.#define t
333af 6b 57 53 20 20 20 20 20 20 31 0a 23 64 65 66 69  kWS      1.#defi
333b0 6e 65 20 74 6b 4f 54 48 45 52 20 20 20 32 0a 23  ne tkOTHER   2.#
333b1 64 65 66 69 6e 65 20 74 6b 45 58 50 4c 41 49 4e  define tkEXPLAIN
333b2 20 33 0a 23 64 65 66 69 6e 65 20 74 6b 43 52 45   3.#define tkCRE
333b3 41 54 45 20 20 34 0a 23 64 65 66 69 6e 65 20 74  ATE  4.#define t
333b4 6b 54 45 4d 50 20 20 20 20 35 0a 23 64 65 66 69  kTEMP    5.#defi
333b5 6e 65 20 74 6b 54 52 49 47 47 45 52 20 36 0a 23  ne tkTRIGGER 6.#
333b6 64 65 66 69 6e 65 20 74 6b 45 4e 44 20 20 20 20  define tkEND    
333b7 20 37 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e   7../*.** Return
333b8 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76   TRUE if the giv
333b9 65 6e 20 53 51 4c 20 73 74 72 69 6e 67 20 65 6e  en SQL string en
333ba 64 73 20 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f  ds in a semicolo
333bb 6e 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c  n..**.** Special
333bc 20 68 61 6e 64 6c 69 6e 67 20 69 73 20 72 65 71   handling is req
333bd 75 69 72 65 20 66 6f 72 20 43 52 45 41 54 45 20  uire for CREATE 
333be 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e  TRIGGER statemen
333bf 74 73 2e 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20  ts..** Whenever 
333c0 74 68 65 20 43 52 45 41 54 45 20 54 52 49 47 47  the CREATE TRIGG
333c1 45 52 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20  ER keywords are 
333c2 73 65 65 6e 2c 20 74 68 65 20 73 74 61 74 65 6d  seen, the statem
333c3 65 6e 74 0a 2a 2a 20 6d 75 73 74 20 65 6e 64 20  ent.** must end 
333c4 77 69 74 68 20 22 3b 45 4e 44 3b 22 2e 0a 2a 2a  with ";END;"..**
333c5 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  .** This impleme
333c6 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73  ntation uses a s
333c7 74 61 74 65 20 6d 61 63 68 69 6e 65 20 77 69 74  tate machine wit
333c8 68 20 37 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a  h 7 states:.**.*
333c9 2a 20 20 20 28 30 29 20 53 54 41 52 54 20 20 20  *   (0) START   
333ca 20 20 41 74 20 74 68 65 20 62 65 67 69 6e 6e 69    At the beginni
333cb 6e 67 20 6f 72 20 65 6e 64 20 6f 66 20 61 6e 20  ng or end of an 
333cc 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  SQL statement.  
333cd 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
333ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333cf 72 65 74 75 72 6e 73 20 31 20 69 66 20 69 74 20  returns 1 if it 
333d0 65 6e 64 73 20 69 6e 20 74 68 65 20 53 54 41 52  ends in the STAR
333d1 54 20 73 74 61 74 65 20 61 6e 64 20 30 20 69 66  T state and 0 if
333d2 20 69 74 20 65 6e 64 73 0a 2a 2a 20 20 20 20 20   it ends.**     
333d3 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 20 61              in a
333d4 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 2e 0a  ny other state..
333d5 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 4e 4f 52 4d  **.**   (1) NORM
333d6 41 4c 20 20 20 20 57 65 20 61 72 65 20 69 6e 20  AL    We are in 
333d7 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 74  the middle of st
333d8 61 74 65 6d 65 6e 74 20 77 68 69 63 68 20 65 6e  atement which en
333d9 64 73 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65  ds with a single
333da 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
333db 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a      semicolon..*
333dc 2a 0a 2a 2a 20 20 20 28 32 29 20 45 58 50 4c 41  *.**   (2) EXPLA
333dd 49 4e 20 20 20 54 68 65 20 6b 65 79 77 6f 72 64  IN   The keyword
333de 20 45 58 50 4c 41 49 4e 20 68 61 73 20 62 65 65   EXPLAIN has bee
333df 6e 20 73 65 65 6e 20 61 74 20 74 68 65 20 62 65  n seen at the be
333e0 67 69 6e 6e 69 6e 67 20 6f 66 20 0a 2a 2a 20 20  ginning of .**  
333e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
333e2 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
333e3 2a 20 20 20 28 33 29 20 43 52 45 41 54 45 20 20  *   (3) CREATE  
333e4 20 20 54 68 65 20 6b 65 79 77 6f 72 64 20 43 52    The keyword CR
333e5 45 41 54 45 20 68 61 73 20 62 65 65 6e 20 73 65  EATE has been se
333e6 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  en at the beginn
333e7 69 6e 67 20 6f 66 20 61 0a 2a 2a 20 20 20 20 20  ing of a.**     
333e8 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74              stat
333e9 65 6d 65 6e 74 2c 20 70 6f 73 73 69 62 6c 79 20  ement, possibly 
333ea 70 72 65 63 65 65 64 65 64 20 62 79 20 45 58 50  preceeded by EXP
333eb 4c 41 49 4e 20 61 6e 64 2f 6f 72 20 66 6f 6c 6c  LAIN and/or foll
333ec 6f 77 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20  owed by.**      
333ed 20 20 20 20 20 20 20 20 20 20 20 54 45 4d 50 20             TEMP 
333ee 6f 72 20 54 45 4d 50 4f 52 41 52 59 0a 2a 2a 0a  or TEMPORARY.**.
333ef 2a 2a 20 20 20 28 34 29 20 54 52 49 47 47 45 52  **   (4) TRIGGER
333f0 20 20 20 57 65 20 61 72 65 20 69 6e 20 74 68 65     We are in the
333f1 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 74 72 69   middle of a tri
333f2 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e 20  gger definition 
333f3 74 68 61 74 20 6d 75 73 74 20 62 65 0a 2a 2a 20  that must be.** 
333f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333f5 65 6e 64 65 64 20 62 79 20 61 20 73 65 6d 69 63  ended by a semic
333f6 6f 6c 6f 6e 2c 20 74 68 65 20 6b 65 79 77 6f 72  olon, the keywor
333f7 64 20 45 4e 44 2c 20 61 6e 64 20 61 6e 6f 74 68  d END, and anoth
333f8 65 72 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a  er semicolon..**
333f9 0a 2a 2a 20 20 20 28 35 29 20 53 45 4d 49 20 20  .**   (5) SEMI  
333fa 20 20 20 20 57 65 27 76 65 20 73 65 65 6e 20 74      We've seen t
333fb 68 65 20 66 69 72 73 74 20 73 65 6d 69 63 6f 6c  he first semicol
333fc 6f 6e 20 69 6e 20 74 68 65 20 22 3b 45 4e 44 3b  on in the ";END;
333fd 22 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 74  " that occurs at
333fe 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
333ff 20 20 20 20 74 68 65 20 65 6e 64 20 6f 66 20 61      the end of a
33400 20 74 72 69 67 67 65 72 20 64 65 66 69 6e 69 74   trigger definit
33401 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29  ion..**.**   (6)
33402 20 45 4e 44 20 20 20 20 20 20 20 57 65 27 76 65   END       We've
33403 20 73 65 65 6e 20 74 68 65 20 22 3b 45 4e 44 22   seen the ";END"
33404 20 6f 66 20 74 68 65 20 22 3b 45 4e 44 3b 22 20   of the ";END;" 
33405 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 20 74  that occurs at t
33406 68 65 20 65 6e 64 0a 2a 2a 20 20 20 20 20 20 20  he end.**       
33407 20 20 20 20 20 20 20 20 20 20 6f 66 20 61 20 74            of a t
33408 72 69 67 67 65 72 20 64 69 66 69 6e 69 74 69 6f  rigger difinitio
33409 6e 2e 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 69 74  n..**.** Transit
3340a 69 6f 6e 73 20 62 65 74 77 65 65 6e 20 73 74 61  ions between sta
3340b 74 65 73 20 61 62 6f 76 65 20 61 72 65 20 64 65  tes above are de
3340c 74 65 72 6d 69 6e 65 64 20 62 79 20 74 6f 6b 65  termined by toke
3340d 6e 73 20 65 78 74 72 61 63 74 65 64 0a 2a 2a 20  ns extracted.** 
3340e 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 2e 20  from the input. 
3340f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
33410 6f 6b 65 6e 73 20 61 72 65 20 73 69 67 6e 69 66  okens are signif
33411 69 63 61 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 28  icant:.**.**   (
33412 30 29 20 74 6b 53 45 4d 49 20 20 20 20 20 20 41  0) tkSEMI      A
33413 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 20 20   semicolon..**  
33414 20 28 31 29 20 74 6b 57 53 20 20 20 20 20 20 20   (1) tkWS       
33415 20 57 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 20   Whitespace.**  
33416 20 28 32 29 20 74 6b 4f 54 48 45 52 20 20 20 20   (2) tkOTHER    
33417 20 41 6e 79 20 6f 74 68 65 72 20 53 51 4c 20 74   Any other SQL t
33418 6f 6b 65 6e 2e 0a 2a 2a 20 20 20 28 33 29 20 74  oken..**   (3) t
33419 6b 45 58 50 4c 41 49 4e 20 20 20 54 68 65 20 22  kEXPLAIN   The "
3341a 65 78 70 6c 61 69 6e 22 20 6b 65 79 77 6f 72 64  explain" keyword
3341b 2e 0a 2a 2a 20 20 20 28 34 29 20 74 6b 43 52 45  ..**   (4) tkCRE
3341c 41 54 45 20 20 20 20 54 68 65 20 22 63 72 65 61  ATE    The "crea
3341d 74 65 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20  te" keyword..** 
3341e 20 20 28 35 29 20 74 6b 54 45 4d 50 20 20 20 20    (5) tkTEMP    
3341f 20 20 54 68 65 20 22 74 65 6d 70 22 20 6f 72 20    The "temp" or 
33420 22 74 65 6d 70 6f 72 61 72 79 22 20 6b 65 79 77  "temporary" keyw
33421 6f 72 64 2e 0a 2a 2a 20 20 20 28 36 29 20 74 6b  ord..**   (6) tk
33422 54 52 49 47 47 45 52 20 20 20 54 68 65 20 22 74  TRIGGER   The "t
33423 72 69 67 67 65 72 22 20 6b 65 79 77 6f 72 64 2e  rigger" keyword.
33424 0a 2a 2a 20 20 20 28 37 29 20 74 6b 45 4e 44 20  .**   (7) tkEND 
33425 20 20 20 20 20 20 54 68 65 20 22 65 6e 64 22 20        The "end" 
33426 6b 65 79 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 57  keyword..**.** W
33427 68 69 74 65 73 70 61 63 65 20 6e 65 76 65 72 20  hitespace never 
33428 63 61 75 73 65 73 20 61 20 73 74 61 74 65 20 74  causes a state t
33429 72 61 6e 73 69 74 69 6f 6e 20 61 6e 64 20 69 73  ransition and is
3342a 20 61 6c 77 61 79 73 20 69 67 6e 6f 72 65 64 2e   always ignored.
3342b 0a 2a 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d  .**.** If we com
3342c 70 69 6c 65 20 77 69 74 68 20 53 51 4c 49 54 45  pile with SQLITE
3342d 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 2c 20 61  _OMIT_TRIGGER, a
3342e 6c 6c 20 6f 66 20 74 68 65 20 63 6f 6d 70 75 74  ll of the comput
3342f 61 74 69 6f 6e 20 6e 65 65 64 65 64 0a 2a 2a 20  ation needed.** 
33430 74 6f 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 65  to recognize the
33431 20 65 6e 64 20 6f 66 20 61 20 74 72 69 67 67 65   end of a trigge
33432 72 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64  r can be omitted
33433 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 65 20 74  .  All we have t
33434 6f 20 64 6f 0a 2a 2a 20 69 73 20 6c 6f 6f 6b 20  o do.** is look 
33435 66 6f 72 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20  for a semicolon 
33436 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74  that is not part
33437 20 6f 66 20 61 6e 20 73 74 72 69 6e 67 20 6f 72   of an string or
33438 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c   comment..*/.SQL
33439 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3343a 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e  te3_complete(con
3343b 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a  st char *zSql){.
3343c 20 20 75 38 20 73 74 61 74 65 20 3d 20 30 3b 20    u8 state = 0; 
3343d 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61    /* Current sta
3343e 74 65 2c 20 75 73 69 6e 67 20 6e 75 6d 62 65 72  te, using number
3343f 73 20 64 65 66 69 6e 65 64 20 69 6e 20 68 65 61  s defined in hea
33440 64 65 72 20 63 6f 6d 6d 65 6e 74 20 2a 2f 0a 20  der comment */. 
33441 20 75 38 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20   u8 token;      
33442 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 74 68 65   /* Value of the
33443 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 0a   next token */..
33444 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
33445 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a  MIT_TRIGGER.  /*
33446 20 41 20 63 6f 6d 70 6c 65 78 20 73 74 61 74 65   A complex state
33447 6d 65 6e 74 20 6d 61 63 68 69 6e 65 20 75 73 65  ment machine use
33448 64 20 74 6f 20 64 65 74 65 63 74 20 74 68 65 20  d to detect the 
33449 65 6e 64 20 6f 66 20 61 20 43 52 45 41 54 45 20  end of a CREATE 
3344a 54 52 49 47 47 45 52 0a 20 20 2a 2a 20 73 74 61  TRIGGER.  ** sta
3344b 74 65 6d 65 6e 74 2e 20 20 54 68 69 73 20 69 73  tement.  This is
3344c 20 74 68 65 20 6e 6f 72 6d 61 6c 20 63 61 73 65   the normal case
3344d 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  ..  */.  static 
3344e 63 6f 6e 73 74 20 75 38 20 74 72 61 6e 73 5b 37  const u8 trans[7
3344f 5d 5b 38 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  ][8] = {.       
33450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33451 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20 20 20 20   Token:         
33452 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33454 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f         */.     /
33455 2a 20 53 74 61 74 65 3a 20 20 20 20 20 20 20 2a  * State:       *
33456 2a 20 20 53 45 4d 49 20 20 57 53 20 20 4f 54 48  *  SEMI  WS  OTH
33457 45 52 20 45 58 50 4c 41 49 4e 20 20 43 52 45 41  ER EXPLAIN  CREA
33458 54 45 20 20 54 45 4d 50 20 20 54 52 49 47 47 45  TE  TEMP  TRIGGE
33459 52 20 20 45 4e 44 20 20 2a 2f 0a 20 20 20 20 20  R  END  */.     
3345a 2f 2a 20 30 20 20 20 53 54 41 52 54 3a 20 2a 2f  /* 0   START: */
3345b 20 7b 20 20 20 20 30 2c 20 20 30 2c 20 20 20 20   {    0,  0,    
3345c 20 31 2c 20 20 20 20 20 20 32 2c 20 20 20 20 20   1,      2,     
3345d 20 33 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20   3,    1,       
3345e 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20  1,   1,  },.    
3345f 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a   /* 1  NORMAL: *
33460 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c 20 20 20  / {    0,  1,   
33461 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20    1,      1,    
33462 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 20 20    1,    1,      
33463 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20   1,   1,  },.   
33464 20 20 2f 2a 20 32 20 45 58 50 4c 41 49 4e 3a 20    /* 2 EXPLAIN: 
33465 2a 2f 20 7b 20 20 20 20 30 2c 20 20 32 2c 20 20  */ {    0,  2,  
33466 20 20 20 32 2c 20 20 20 20 20 20 31 2c 20 20 20     2,      1,   
33467 20 20 20 33 2c 20 20 20 20 31 2c 20 20 20 20 20     3,    1,     
33468 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20    1,   1,  },.  
33469 20 20 20 2f 2a 20 33 20 20 43 52 45 41 54 45 3a     /* 3  CREATE:
3346a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 33 2c 20   */ {    0,  3, 
3346b 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20      1,      1,  
3346c 20 20 20 20 31 2c 20 20 20 20 33 2c 20 20 20 20      1,    3,    
3346d 20 20 20 34 2c 20 20 20 31 2c 20 20 7d 2c 0a 20     4,   1,  },. 
3346e 20 20 20 20 2f 2a 20 34 20 54 52 49 47 47 45 52      /* 4 TRIGGER
3346f 3a 20 2a 2f 20 7b 20 20 20 20 35 2c 20 20 34 2c  : */ {    5,  4,
33470 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20       4,      4, 
33471 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20       4,    4,   
33472 20 20 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a      4,   4,  },.
33473 20 20 20 20 20 2f 2a 20 35 20 20 20 20 53 45 4d       /* 5    SEM
33474 49 3a 20 2a 2f 20 7b 20 20 20 20 35 2c 20 20 35  I: */ {    5,  5
33475 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c  ,     4,      4,
33476 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20        4,    4,  
33477 20 20 20 20 20 34 2c 20 20 20 36 2c 20 20 7d 2c       4,   6,  },
33478 0a 20 20 20 20 20 2f 2a 20 36 20 20 20 20 20 45  .     /* 6     E
33479 4e 44 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20  ND: */ {    0,  
3347a 36 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20 34  6,     4,      4
3347b 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20  ,      4,    4, 
3347c 20 20 20 20 20 20 34 2c 20 20 20 34 2c 20 20 7d        4,   4,  }
3347d 2c 0a 20 20 7d 3b 0a 23 65 6c 73 65 0a 20 20 2f  ,.  };.#else.  /
3347e 2a 20 49 66 20 74 72 69 67 67 65 72 73 20 61 72  * If triggers ar
3347f 65 20 6e 6f 74 20 73 75 70 70 6f 72 65 64 20 62  e not suppored b
33480 79 20 74 68 69 73 20 63 6f 6d 70 69 6c 65 20 74  y this compile t
33481 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e  hen the statemen
33482 74 20 6d 61 63 68 69 6e 65 0a 20 20 2a 2a 20 75  t machine.  ** u
33483 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 74 68  sed to detect th
33484 65 20 65 6e 64 20 6f 66 20 61 20 73 74 61 74 65  e end of a state
33485 6d 65 6e 74 20 69 73 20 6d 75 63 68 20 73 69 6d  ment is much sim
33486 70 6c 69 65 72 0a 20 20 2a 2f 0a 20 20 73 74 61  plier.  */.  sta
33487 74 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61  tic const u8 tra
33488 6e 73 5b 32 5d 5b 33 5d 20 3d 20 7b 0a 20 20 20  ns[2][3] = {.   
33489 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3348a 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20    /* Token:     
3348b 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a        */.     /*
3348c 20 53 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a   State:       **
3348d 20 20 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45    SEMI  WS  OTHE
3348e 52 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20  R */.     /* 0  
3348f 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20   START: */ {    
33490 30 2c 20 20 30 2c 20 20 20 20 20 31 2c 20 7d 2c  0,  0,     1, },
33491 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d  .     /* 1  NORM
33492 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20  AL: */ {    0,  
33493 31 2c 20 20 20 20 20 31 2c 20 7d 2c 0a 20 20 7d  1,     1, },.  }
33494 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
33495 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20  TE_OMIT_TRIGGER 
33496 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 2a 7a 53  */..  while( *zS
33497 71 6c 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68  ql ){.    switch
33498 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20  ( *zSql ){.     
33499 20 63 61 73 65 20 27 3b 27 3a 20 7b 20 20 2f 2a   case ';': {  /*
3349a 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f 0a   A semicolon */.
3349b 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
3349c 74 6b 53 45 4d 49 3b 0a 20 20 20 20 20 20 20 20  tkSEMI;.        
3349d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
3349e 20 20 20 20 20 63 61 73 65 20 27 20 27 3a 0a 20       case ' ':. 
3349f 20 20 20 20 20 63 61 73 65 20 27 5c 72 27 3a 0a       case '\r':.
334a0 20 20 20 20 20 20 63 61 73 65 20 27 5c 74 27 3a        case '\t':
334a1 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 6e 27  .      case '\n'
334a2 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 66  :.      case '\f
334a3 27 3a 20 7b 20 20 2f 2a 20 57 68 69 74 65 20 73  ': {  /* White s
334a4 70 61 63 65 20 69 73 20 69 67 6e 6f 72 65 64 20  pace is ignored 
334a5 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  */.        token
334a6 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20 20   = tkWS;.       
334a7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
334a8 20 20 20 20 20 20 63 61 73 65 20 27 2f 27 3a 20        case '/': 
334a9 7b 20 20 20 2f 2a 20 43 2d 73 74 79 6c 65 20 63  {   /* C-style c
334aa 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  omments */.     
334ab 20 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d     if( zSql[1]!=
334ac 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '*' ){.         
334ad 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
334ae 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
334af 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
334b0 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20 32 3b 0a       zSql += 2;.
334b1 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a          while( z
334b2 53 71 6c 5b 30 5d 20 26 26 20 28 7a 53 71 6c 5b  Sql[0] && (zSql[
334b3 30 5d 21 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c 5b  0]!='*' || zSql[
334b4 31 5d 21 3d 27 2f 27 29 20 29 7b 20 7a 53 71 6c  1]!='/') ){ zSql
334b5 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66  ++; }.        if
334b6 28 20 7a 53 71 6c 5b 30 5d 3d 3d 30 20 29 20 72  ( zSql[0]==0 ) r
334b7 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20  eturn 0;.       
334b8 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20   zSql++;.       
334b9 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20   token = tkWS;. 
334ba 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
334bb 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
334bc 20 27 2d 27 3a 20 7b 20 20 20 2f 2a 20 53 51 4c   '-': {   /* SQL
334bd 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20  -style comments 
334be 66 72 6f 6d 20 22 2d 2d 22 20 74 6f 20 65 6e 64  from "--" to end
334bf 20 6f 66 20 6c 69 6e 65 20 2a 2f 0a 20 20 20 20   of line */.    
334c0 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21      if( zSql[1]!
334c1 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='-' ){.        
334c2 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
334c3 52 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  R;.          bre
334c4 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
334c5 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53        while( *zS
334c6 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 27 5c 6e  ql && *zSql!='\n
334c7 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20  ' ){ zSql++; }. 
334c8 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c         if( *zSql
334c9 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 73 74 61  ==0 ) return sta
334ca 74 65 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20 74  te==0;.        t
334cb 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20  oken = tkWS;.   
334cc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
334cd 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27    }.      case '
334ce 5b 27 3a 20 7b 20 20 20 2f 2a 20 4d 69 63 72 6f  [': {   /* Micro
334cf 73 6f 66 74 2d 73 74 79 6c 65 20 69 64 65 6e 74  soft-style ident
334d0 69 66 69 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d 20  ifiers in [...] 
334d1 2a 2f 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b  */.        zSql+
334d2 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  +;.        while
334d3 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c  ( *zSql && *zSql
334d4 21 3d 27 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b  !=']' ){ zSql++;
334d5 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a   }.        if( *
334d6 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  zSql==0 ) return
334d7 20 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65   0;.        toke
334d8 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
334d9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
334da 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27    }.      case '
334db 60 27 3a 20 20 20 20 20 2f 2a 20 47 72 61 76 65  `':     /* Grave
334dc 2d 61 63 63 65 6e 74 20 71 75 6f 74 65 64 20 73  -accent quoted s
334dd 79 6d 62 6f 6c 73 20 75 73 65 64 20 62 79 20 4d  ymbols used by M
334de 79 53 51 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61  ySQL */.      ca
334df 73 65 20 27 22 27 3a 20 20 20 20 20 2f 2a 20 73  se '"':     /* s
334e0 69 6e 67 6c 65 2d 20 61 6e 64 20 64 6f 75 62 6c  ingle- and doubl
334e1 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 73  e-quoted strings
334e2 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 27   */.      case '
334e3 5c 27 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  \'': {.        i
334e4 6e 74 20 63 20 3d 20 2a 7a 53 71 6c 3b 0a 20 20  nt c = *zSql;.  
334e5 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20        zSql++;.  
334e6 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53        while( *zS
334e7 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 63 20 29  ql && *zSql!=c )
334e8 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20  { zSql++; }.    
334e9 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30      if( *zSql==0
334ea 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
334eb 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
334ec 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 62 72  THER;.        br
334ed 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
334ee 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 23 69     default: {.#i
334ef 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
334f0 49 43 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67  IC.        unsig
334f1 6e 65 64 20 63 68 61 72 20 63 3b 0a 23 65 6e 64  ned char c;.#end
334f2 69 66 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  if.        if( I
334f3 64 43 68 61 72 28 28 75 38 29 2a 7a 53 71 6c 29  dChar((u8)*zSql)
334f4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
334f5 20 4b 65 79 77 6f 72 64 73 20 61 6e 64 20 75 6e   Keywords and un
334f6 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65  quoted identifie
334f7 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  rs */.          
334f8 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20 20 20  int nId;.       
334f9 20 20 20 66 6f 72 28 6e 49 64 3d 31 3b 20 49 64     for(nId=1; Id
334fa 43 68 61 72 28 7a 53 71 6c 5b 6e 49 64 5d 29 3b  Char(zSql[nId]);
334fb 20 6e 49 64 2b 2b 29 7b 7d 0a 23 69 66 64 65 66   nId++){}.#ifdef
334fc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
334fd 47 47 45 52 0a 20 20 20 20 20 20 20 20 20 20 74  GGER.          t
334fe 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
334ff 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20  #else.          
33500 73 77 69 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b  switch( *zSql ){
33501 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73  .            cas
33502 65 20 27 63 27 3a 20 63 61 73 65 20 27 43 27 3a  e 'c': case 'C':
33503 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
33504 20 69 66 28 20 6e 49 64 3d 3d 36 20 26 26 20 73   if( nId==6 && s
33505 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
33506 53 71 6c 2c 20 22 63 72 65 61 74 65 22 2c 20 36  Sql, "create", 6
33507 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
33508 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
33509 74 6b 43 52 45 41 54 45 3b 0a 20 20 20 20 20 20  tkCREATE;.      
3350a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
3350b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
3350c 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
3350d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
3350e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
3350f 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
33510 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 63   }.            c
33511 61 73 65 20 27 74 27 3a 20 63 61 73 65 20 27 54  ase 't': case 'T
33512 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ': {.           
33513 20 20 20 69 66 28 20 6e 49 64 3d 3d 37 20 26 26     if( nId==7 &&
33514 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
33515 28 7a 53 71 6c 2c 20 22 74 72 69 67 67 65 72 22  (zSql, "trigger"
33516 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 7)==0 ){.     
33517 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
33518 20 3d 20 74 6b 54 52 49 47 47 45 52 3b 0a 20 20   = tkTRIGGER;.  
33519 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
3351a 65 20 69 66 28 20 6e 49 64 3d 3d 34 20 26 26 20  e if( nId==4 && 
3351b 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
3351c 7a 53 71 6c 2c 20 22 74 65 6d 70 22 2c 20 34 29  zSql, "temp", 4)
3351d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
3351e 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
3351f 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20 20  kTEMP;.         
33520 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
33521 49 64 3d 3d 39 20 26 26 20 73 71 6c 69 74 65 33  Id==9 && sqlite3
33522 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22  StrNICmp(zSql, "
33523 74 65 6d 70 6f 72 61 72 79 22 2c 20 39 29 3d 3d  temporary", 9)==
33524 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
33525 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54       token = tkT
33526 45 4d 50 3b 0a 20 20 20 20 20 20 20 20 20 20 20  EMP;.           
33527 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
33528 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
33529 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
3352a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3352b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
3352c 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
3352d 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 27            case '
3352e 65 27 3a 20 20 63 61 73 65 20 27 45 27 3a 20 7b  e':  case 'E': {
3352f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
33530 66 28 20 6e 49 64 3d 3d 33 20 26 26 20 73 71 6c  f( nId==3 && sql
33531 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71  ite3StrNICmp(zSq
33532 6c 2c 20 22 65 6e 64 22 2c 20 33 29 3d 3d 30 20  l, "end", 3)==0 
33533 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
33534 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45 4e 44     token = tkEND
33535 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
33536 7d 65 6c 73 65 0a 23 69 66 6e 64 65 66 20 53 51  }else.#ifndef SQ
33537 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
33538 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N.              
33539 69 66 28 20 6e 49 64 3d 3d 37 20 26 26 20 73 71  if( nId==7 && sq
3353a 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53  lite3StrNICmp(zS
3353b 71 6c 2c 20 22 65 78 70 6c 61 69 6e 22 2c 20 37  ql, "explain", 7
3353c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
3353d 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
3353e 74 6b 45 58 50 4c 41 49 4e 3b 0a 20 20 20 20 20  tkEXPLAIN;.     
3353f 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23           }else.#
33540 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20  endif.          
33541 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20      {.          
33542 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
33543 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20  OTHER;.         
33544 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
33545 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33546 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
33547 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
33548 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74  .              t
33549 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
3354a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
3354b 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
3354c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23   }.          }.#
3354d 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
3354e 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
3354f 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20 2b            zSql +
33550 3d 20 6e 49 64 2d 31 3b 0a 20 20 20 20 20 20 20  = nId-1;.       
33551 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
33552 20 20 2f 2a 20 4f 70 65 72 61 74 6f 72 73 20 61    /* Operators a
33553 6e 64 20 73 70 65 63 69 61 6c 20 73 79 6d 62 6f  nd special symbo
33554 6c 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ls */.          
33555 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
33556 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
33557 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
33558 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 74 61 74  }.    }.    stat
33559 65 20 3d 20 74 72 61 6e 73 5b 73 74 61 74 65 5d  e = trans[state]
3355a 5b 74 6f 6b 65 6e 5d 3b 0a 20 20 20 20 7a 53 71  [token];.    zSq
3355b 6c 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  l++;.  }.  retur
3355c 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 7d 0a 0a 23  n state==0;.}..#
3355d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3355e 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54  IT_UTF16./*.** T
3355f 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
33560 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73  he same as the s
33561 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
33562 29 20 72 6f 75 74 69 6e 65 20 64 65 73 63 72 69  ) routine descri
33563 62 65 64 0a 2a 2a 20 61 62 6f 76 65 2c 20 65 78  bed.** above, ex
33564 63 65 70 74 20 74 68 61 74 20 74 68 65 20 70 61  cept that the pa
33565 72 61 6d 65 74 65 72 20 69 73 20 72 65 71 75 69  rameter is requi
33566 72 65 64 20 74 6f 20 62 65 20 55 54 46 2d 31 36  red to be UTF-16
33567 20 65 6e 63 6f 64 65 64 2c 20 6e 6f 74 0a 2a 2a   encoded, not.**
33568 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54   UTF-8..*/.SQLIT
33569 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3356a 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e  3_complete16(con
3356b 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 29 7b 0a  st void *zSql){.
3356c 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
3356d 2a 70 56 61 6c 3b 0a 20 20 63 68 61 72 20 63 6f  *pVal;.  char co
3356e 6e 73 74 20 2a 7a 53 71 6c 38 3b 0a 20 20 69 6e  nst *zSql8;.  in
3356f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  t rc = SQLITE_NO
33570 4d 45 4d 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  MEM;..#ifndef SQ
33571 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
33572 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  IT.  rc = sqlite
33573 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a  3_initialize();.
33574 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
33575 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70  n rc;.#endif.  p
33576 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Val = sqlite3Val
33577 75 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c 69  ueNew(0);.  sqli
33578 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70  te3ValueSetStr(p
33579 56 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c 2c 20 53  Val, -1, zSql, S
3357a 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
3357b 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  E, SQLITE_STATIC
3357c 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c  );.  zSql8 = sql
3357d 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56  ite3ValueText(pV
3357e 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  al, SQLITE_UTF8)
3357f 3b 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b  ;.  if( zSql8 ){
33580 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
33581 33 5f 63 6f 6d 70 6c 65 74 65 28 7a 53 71 6c 38  3_complete(zSql8
33582 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
33583 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
33584 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  M;.  }.  sqlite3
33585 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b  ValueFree(pVal);
33586 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
33587 33 41 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b  3ApiExit(0, rc);
33588 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
33589 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
3358a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  /.#endif /* SQLI
3358b 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45  TE_OMIT_COMPLETE
3358c 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
3358d 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 6f 6d 70  **** End of comp
3358e 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lete.c *********
3358f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33591 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
33592 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
33593 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  main.c *********
33594 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33595 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33596 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
33597 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
33598 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
33599 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
3359a 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
3359b 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
3359c 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
3359d 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
3359e 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
3359f 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
335a0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
335a1 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
335a2 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
335a3 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
335a4 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
335a5 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
335a6 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
335a7 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
335a8 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
335a9 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
335aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d  ***********.** M
335ae 61 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65  ain file for the
335af 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
335b0 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69    The routines i
335b1 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69  n this file.** i
335b2 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f  mplement the pro
335b3 67 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63  grammer interfac
335b4 65 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79  e to the library
335b5 2e 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a  .  Routines in.*
335b6 2a 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72  * other files ar
335b7 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75  e for internal u
335b8 73 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64  se by SQLite and
335b9 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a   should not be.*
335ba 2a 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73  * accessed by us
335bb 65 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ers of the libra
335bc 72 79 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53  ry..*/..#ifdef S
335bd 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
335be 33 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3./*************
335bf 2a 20 49 6e 63 6c 75 64 65 20 66 74 73 33 2e 68  * Include fts3.h
335c0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
335c1 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f main.c *******
335c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335c3 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
335c4 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
335c5 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.h ************
335c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335c8 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74  /./*.** 2006 Oct
335c9 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   10.**.** The au
335ca 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
335cb 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
335cc 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
335cd 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
335ce 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
335cf 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
335d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
335d1 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
335d2 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
335d3 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
335d4 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
335d5 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
335d6 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
335d7 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
335d8 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
335d9 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
335da 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
335db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335df 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
335e0 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69  is header file i
335e1 73 20 75 73 65 64 20 62 79 20 70 72 6f 67 72 61  s used by progra
335e2 6d 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20  ms that want to 
335e3 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65  link against the
335e4 0a 2a 2a 20 46 54 53 33 20 6c 69 62 72 61 72 79  .** FTS3 library
335e5 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69  .  All it does i
335e6 73 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 71  s declare the sq
335e7 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 29 20  lite3Fts3Init() 
335e8 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23  interface..*/..#
335e9 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20  if 0.extern "C" 
335ea 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63  {.#endif  /* __c
335eb 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c  plusplus */..SQL
335ec 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
335ed 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28  sqlite3Fts3Init(
335ee 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23  sqlite3 *db);..#
335ef 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72  if 0.}  /* exter
335f0 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20  n "C" */.#endif 
335f1 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20   /* __cplusplus 
335f2 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
335f3 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 2e  *** End of fts3.
335f4 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
335f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335f7 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
335f8 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
335f9 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
335fa 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   in main.c *****
335fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335fc 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  **/.#endif.#ifde
335fd 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
335fe 52 54 52 45 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  RTREE./*********
335ff 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 72 74  ***** Include rt
33600 72 65 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ree.h in the mid
33601 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a  dle of main.c **
33602 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33603 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
33604 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
33605 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   rtree.h *******
33606 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33607 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33608 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
33609 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68   May 26.**.** Th
3360a 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
3360b 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
3360c 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
3360d 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
3360e 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
3360f 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
33610 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
33611 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
33612 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
33613 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
33614 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
33615 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
33616 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
33617 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
33618 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
33619 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
3361a 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
3361b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3361c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3361d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3361e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3361f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
33620 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
33621 6c 65 20 69 73 20 75 73 65 64 20 62 79 20 70 72  le is used by pr
33622 6f 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74  ograms that want
33623 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74   to link against
33624 20 74 68 65 0a 2a 2a 20 52 54 52 45 45 20 6c 69   the.** RTREE li
33625 62 72 61 72 79 2e 20 20 41 6c 6c 20 69 74 20 64  brary.  All it d
33626 6f 65 73 20 69 73 20 64 65 63 6c 61 72 65 20 74  oes is declare t
33627 68 65 20 73 71 6c 69 74 65 33 52 74 72 65 65 49  he sqlite3RtreeI
33628 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e  nit() interface.
33629 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65 72  .*/..#if 0.exter
3362a 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20 20  n "C" {.#endif  
3362b 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a  /* __cplusplus *
3362c 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
3362d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74 72  E int sqlite3Rtr
3362e 65 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a  eeInit(sqlite3 *
3362f 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f  db);..#if 0.}  /
33630 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a  * extern "C" */.
33631 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c  #endif  /* __cpl
33632 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  usplus */../****
33633 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
33634 66 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a  f rtree.h ******
33635 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33636 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33637 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
33638 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
33639 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
3363a 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e  eft off in main.
3363b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3363c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69  *********/.#endi
3363d 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
3363e 45 4e 41 42 4c 45 5f 49 43 55 0a 2f 2a 2a 2a 2a  ENABLE_ICU./****
3363f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
33640 64 65 20 73 71 6c 69 74 65 69 63 75 2e 68 20 69  de sqliteicu.h i
33641 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
33642 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  main.c *********
33643 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
33644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
33645 20 66 69 6c 65 20 73 71 6c 69 74 65 69 63 75 2e   file sqliteicu.
33646 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
33647 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33648 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
33649 20 32 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a   2008 May 26.**.
3364a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
3364b 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
3364c 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
3364d 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
3364e 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
3364f 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
33650 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
33651 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
33652 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
33653 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
33654 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
33655 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
33656 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
33657 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
33658 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
33659 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
3365a 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
3365b 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
3365c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3365d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3365e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3365f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33660 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  .**.** This head
33661 65 72 20 66 69 6c 65 20 69 73 20 75 73 65 64 20  er file is used 
33662 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74  by programs that
33663 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67   want to link ag
33664 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 49 43 55  ainst the.** ICU
33665 20 65 78 74 65 6e 73 69 6f 6e 2e 20 20 41 6c 6c   extension.  All
33666 20 69 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c   it does is decl
33667 61 72 65 20 74 68 65 20 73 71 6c 69 74 65 33 49  are the sqlite3I
33668 63 75 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61  cuInit() interfa
33669 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78  ce..*/..#if 0.ex
3366a 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69  tern "C" {.#endi
3366b 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75  f  /* __cplusplu
3366c 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49  s */..SQLITE_PRI
3366d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
3366e 49 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33 20  IcuInit(sqlite3 
3366f 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20  *db);..#if 0.}  
33670 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f  /* extern "C" */
33671 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70  .#endif  /* __cp
33672 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 0a 2f 2a 2a  lusplus */.../**
33673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
33674 20 6f 66 20 73 71 6c 69 74 65 69 63 75 2e 68 20   of sqliteicu.h 
33675 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33676 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33677 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
33678 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
33679 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
3367a 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69   left off in mai
3367b 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
3367c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e  ***********/.#en
3367d 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76  dif../*.** The v
3367e 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  ersion of the li
3367f 62 72 61 72 79 0a 2a 2f 0a 23 69 66 6e 64 65 66  brary.*/.#ifndef
33680 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
33681 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 41 50 49 20  TION.SQLITE_API 
33682 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
33683 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53  e3_version[] = S
33684 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 23  QLITE_VERSION;.#
33685 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49  endif.SQLITE_API
33686 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
33687 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28  ite3_libversion(
33688 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 71  void){ return sq
33689 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 3b 20 7d  lite3_version; }
3368a 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
3368b 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
3368c 73 6f 75 72 63 65 69 64 28 76 6f 69 64 29 7b 20  sourceid(void){ 
3368d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 53 4f  return SQLITE_SO
3368e 55 52 43 45 5f 49 44 3b 20 7d 0a 53 51 4c 49 54  URCE_ID; }.SQLIT
3368f 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
33690 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
33691 62 65 72 28 76 6f 69 64 29 7b 20 72 65 74 75 72  ber(void){ retur
33692 6e 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  n SQLITE_VERSION
33693 5f 4e 55 4d 42 45 52 3b 20 7d 0a 53 51 4c 49 54  _NUMBER; }.SQLIT
33694 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
33695 33 5f 74 68 72 65 61 64 73 61 66 65 28 76 6f 69  3_threadsafe(voi
33696 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
33697 45 5f 54 48 52 45 41 44 53 41 46 45 3b 20 7d 0a  E_THREADSAFE; }.
33698 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
33699 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29  LITE_OMIT_TRACE)
3369a 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
3369b 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43  TE_ENABLE_IOTRAC
3369c 45 29 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  E)./*.** If the 
3369d 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69  following functi
3369e 6f 6e 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f  on pointer is no
3369f 74 20 4e 55 4c 4c 20 61 6e 64 20 69 66 0a 2a 2a  t NULL and if.**
336a0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49   SQLITE_ENABLE_I
336a1 4f 54 52 41 43 45 20 69 73 20 65 6e 61 62 6c 65  OTRACE is enable
336a2 64 2c 20 74 68 65 6e 20 6d 65 73 73 61 67 65 73  d, then messages
336a3 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 49   describing.** I
336a4 2f 4f 20 61 63 74 69 76 65 20 61 72 65 20 77 72  /O active are wr
336a5 69 74 74 65 6e 20 75 73 69 6e 67 20 74 68 69 73  itten using this
336a6 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73   function.  Thes
336a7 65 20 6d 65 73 73 61 67 65 73 0a 2a 2a 20 61 72  e messages.** ar
336a8 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 64  e intended for d
336a9 65 62 75 67 67 69 6e 67 20 61 63 74 69 76 69 74  ebugging activit
336aa 79 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  y only..*/.SQLIT
336ab 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28  E_PRIVATE void (
336ac 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29  *sqlite3IoTrace)
336ad 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e  (const char*, ..
336ae 2e 29 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  .) = 0;.#endif..
336af 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c  /*.** If the fol
336b0 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61  lowing global va
336b1 72 69 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f  riable points to
336b2 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20   a string which 
336b3 69 73 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f  is the.** name o
336b4 66 20 61 20 64 69 72 65 63 74 6f 72 79 2c 20 74  f a directory, t
336b5 68 65 6e 20 74 68 61 74 20 64 69 72 65 63 74 6f  hen that directo
336b6 72 79 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  ry will be used 
336b7 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70  to store.** temp
336b8 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 0a  orary files..**.
336b9 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
336ba 22 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f  "PRAGMA temp_sto
336bb 72 65 5f 64 69 72 65 63 74 6f 72 79 22 20 53 51  re_directory" SQ
336bc 4c 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51  L command..*/.SQ
336bd 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
336be 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
336bf 63 74 6f 72 79 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a  ctory = 0;../*.*
336c0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 53 51 4c  * Initialize SQL
336c1 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69  ite.  .**.** Thi
336c2 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62  s routine must b
336c3 65 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e 69 74  e called to init
336c4 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
336c5 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a  y allocation,.**
336c6 20 56 46 53 2c 20 61 6e 64 20 6d 75 74 65 78 20   VFS, and mutex 
336c7 73 75 62 73 79 73 74 65 6d 73 20 70 72 69 6f 72  subsystems prior
336c8 20 74 6f 20 64 6f 69 6e 67 20 61 6e 79 20 73 65   to doing any se
336c9 72 69 6f 75 73 20 77 6f 72 6b 20 77 69 74 68 0a  rious work with.
336ca 2a 2a 20 53 51 4c 69 74 65 2e 20 20 42 75 74 20  ** SQLite.  But 
336cb 61 73 20 6c 6f 6e 67 20 61 73 20 79 6f 75 20 64  as long as you d
336cc 6f 20 6e 6f 74 20 63 6f 6d 70 69 6c 65 20 77 69  o not compile wi
336cd 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  th SQLITE_OMIT_A
336ce 55 54 4f 49 4e 49 54 0a 2a 2a 20 74 68 69 73 20  UTOINIT.** this 
336cf 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62 65 20  routine will be 
336d0 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 63  called automatic
336d1 61 6c 6c 79 20 62 79 20 6b 65 79 20 72 6f 75 74  ally by key rout
336d2 69 6e 65 73 20 73 75 63 68 20 61 73 0a 2a 2a 20  ines such as.** 
336d3 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 20  sqlite3_open(). 
336d4 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75   .**.** This rou
336d5 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20  tine is a no-op 
336d6 65 78 63 65 70 74 20 6f 6e 20 69 74 73 20 76 65  except on its ve
336d7 72 79 20 66 69 72 73 74 20 63 61 6c 6c 20 66 6f  ry first call fo
336d8 72 20 74 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a  r the process,.*
336d9 2a 20 6f 72 20 66 6f 72 20 74 68 65 20 66 69 72  * or for the fir
336da 73 74 20 63 61 6c 6c 20 61 66 74 65 72 20 61 20  st call after a 
336db 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
336dc 73 68 75 74 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20  shutdown..**.** 
336dd 54 68 65 20 66 69 72 73 74 20 74 68 72 65 61 64  The first thread
336de 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f   to call this ro
336df 75 74 69 6e 65 20 72 75 6e 73 20 74 68 65 20 69  utine runs the i
336e0 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 74 6f  nitialization to
336e1 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20  .** completion. 
336e2 20 49 66 20 73 75 62 73 65 71 75 65 6e 74 20 74   If subsequent t
336e3 68 72 65 61 64 73 20 63 61 6c 6c 20 74 68 69 73  hreads call this
336e4 20 72 6f 75 74 69 6e 65 20 62 65 66 6f 72 65 20   routine before 
336e5 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 68 72  the first.** thr
336e6 65 61 64 20 68 61 73 20 66 69 6e 69 73 68 65 64  ead has finished
336e7 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74   the initializat
336e8 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20 74 68 65  ion process, the
336e9 6e 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74  n the subsequent
336ea 0a 2a 2a 20 74 68 72 65 61 64 73 20 6d 75 73 74  .** threads must
336eb 20 62 6c 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65   block until the
336ec 20 66 69 72 73 74 20 74 68 72 65 61 64 20 66 69   first thread fi
336ed 6e 69 73 68 65 73 20 77 69 74 68 20 74 68 65 20  nishes with the 
336ee 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a  initialization..
336ef 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
336f0 74 68 72 65 61 64 20 6d 69 67 68 74 20 63 61 6c  thread might cal
336f1 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  l this routine r
336f2 65 63 75 72 73 69 76 65 6c 79 2e 20 20 52 65 63  ecursively.  Rec
336f3 75 72 73 69 76 65 0a 2a 2a 20 63 61 6c 6c 73 20  ursive.** calls 
336f4 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
336f5 73 68 6f 75 6c 64 20 6e 6f 74 20 62 6c 6f 63 6b  should not block
336f6 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20 4f 74  , of course.  Ot
336f7 68 65 72 77 69 73 65 20 74 68 65 0a 2a 2a 20 69  herwise the.** i
336f8 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72  nitialization pr
336f9 6f 63 65 73 73 20 77 6f 75 6c 64 20 6e 65 76 65  ocess would neve
336fa 72 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a  r complete..**.*
336fb 2a 20 4c 65 74 20 58 20 62 65 20 74 68 65 20 66  * Let X be the f
336fc 69 72 73 74 20 74 68 72 65 61 64 20 74 6f 20 65  irst thread to e
336fd 6e 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  nter this routin
336fe 65 2e 20 20 4c 65 74 20 59 20 62 65 20 73 6f 6d  e.  Let Y be som
336ff 65 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61  e other.** threa
33700 64 2e 20 20 54 68 65 6e 20 77 68 69 6c 65 20 74  d.  Then while t
33701 68 65 20 69 6e 69 74 69 61 6c 20 69 6e 76 6f 63  he initial invoc
33702 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f  ation of this ro
33703 75 74 69 6e 65 20 62 79 20 58 20 69 73 0a 2a 2a  utine by X is.**
33704 20 69 6e 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20   incomplete, it 
33705 69 73 20 72 65 71 75 69 72 65 64 20 74 68 61 74  is required that
33706 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 43 61  :.**.**    *  Ca
33707 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74  lls to this rout
33708 69 6e 65 20 66 72 6f 6d 20 59 20 6d 75 73 74 20  ine from Y must 
33709 62 6c 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65 20  block until the 
3370a 6f 75 74 65 72 2d 6d 6f 73 74 0a 2a 2a 20 20 20  outer-most.**   
3370b 20 20 20 20 63 61 6c 6c 20 62 79 20 58 20 63 6f      call by X co
3370c 6d 70 6c 65 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  mpletes..**.**  
3370d 20 20 2a 20 20 52 65 63 75 72 73 69 76 65 20 63    *  Recursive c
3370e 61 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75  alls to this rou
3370f 74 69 6e 65 20 66 72 6f 6d 20 74 68 72 65 61 64  tine from thread
33710 20 58 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69   X return immedi
33711 61 74 65 6c 79 0a 2a 2a 20 20 20 20 20 20 20 77  ately.**       w
33712 69 74 68 6f 75 74 20 62 6c 6f 63 6b 69 6e 67 2e  ithout blocking.
33713 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
33714 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  nt sqlite3_initi
33715 61 6c 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 73  alize(void){.  s
33716 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d  qlite3_mutex *pM
33717 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20  aster;          
33718 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
33719 68 65 20 6d 61 69 6e 20 73 74 61 74 69 63 20 6d  he main static m
3371a 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 63  utex */.  int rc
3371b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3371c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3371d 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
3371e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66   code */..#ifdef
3371f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
33720 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
33721 77 73 64 5f 69 6e 69 74 28 34 30 39 36 2c 20 32  wsd_init(4096, 2
33722 34 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  4);.  if( rc!=SQ
33723 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
33724 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65  eturn rc;.  }.#e
33725 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 53 51  ndif..  /* If SQ
33726 4c 69 74 65 20 69 73 20 61 6c 72 65 61 64 79 20  Lite is already 
33727 63 6f 6d 70 6c 65 74 65 6c 79 20 69 6e 69 74 69  completely initi
33728 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20 74 68 69  alized, then thi
33729 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 73  s call.  ** to s
3372a 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
3372b 65 28 29 20 73 68 6f 75 6c 64 20 62 65 20 61 20  e() should be a 
3372c 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 74 68 65 20  no-op.  But the 
3372d 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20  initialization. 
3372e 20 2a 2a 20 6d 75 73 74 20 62 65 20 63 6f 6d 70   ** must be comp
3372f 6c 65 74 65 2e 20 20 53 6f 20 69 73 49 6e 69 74  lete.  So isInit
33730 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 73 65 74   must not be set
33731 20 75 6e 74 69 6c 20 74 68 65 20 76 65 72 79 20   until the very 
33732 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73  end.  ** of this
33733 20 72 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20   routine..  */. 
33734 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
33735 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20  alConfig.isInit 
33736 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
33737 4f 4b 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  OK;..  /* Make s
33738 75 72 65 20 74 68 65 20 6d 75 74 65 78 20 73 75  ure the mutex su
33739 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69  bsystem is initi
3373a 61 6c 69 7a 65 64 2e 20 20 49 66 20 75 6e 61 62  alized.  If unab
3373b 6c 65 20 74 6f 20 0a 20 20 2a 2a 20 69 6e 69 74  le to .  ** init
3373c 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78  ialize the mutex
3373d 20 73 75 62 73 79 73 74 65 6d 2c 20 72 65 74 75   subsystem, retu
3373e 72 6e 20 65 61 72 6c 79 20 77 69 74 68 20 74 68  rn early with th
3373f 65 20 65 72 72 6f 72 2e 0a 20 20 2a 2a 20 49 66  e error..  ** If
33740 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 73   the system is s
33741 6f 20 73 69 63 6b 20 74 68 61 74 20 77 65 20 61  o sick that we a
33742 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c  re unable to all
33743 6f 63 61 74 65 20 61 20 6d 75 74 65 78 2c 0a 20  ocate a mutex,. 
33744 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74   ** there is not
33745 20 6d 75 63 68 20 53 51 4c 69 74 65 20 69 73 20   much SQLite is 
33746 67 6f 69 6e 67 20 74 6f 20 62 65 20 61 62 6c 65  going to be able
33747 20 74 6f 20 64 6f 2e 0a 20 20 2a 2a 0a 20 20 2a   to do..  **.  *
33748 2a 20 54 68 65 20 6d 75 74 65 78 20 73 75 62 73  * The mutex subs
33749 79 73 74 65 6d 20 6d 75 73 74 20 74 61 6b 65 20  ystem must take 
3374a 63 61 72 65 20 6f 66 20 73 65 72 69 61 6c 69 7a  care of serializ
3374b 69 6e 67 20 69 74 73 20 6f 77 6e 0a 20 20 2a 2a  ing its own.  **
3374c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e   initialization.
3374d 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
3374e 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 3b  ite3MutexInit();
3374f 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
33750 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 6e 69  rn rc;..  /* Ini
33751 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c  tialize the mall
33752 6f 63 28 29 20 73 79 73 74 65 6d 20 61 6e 64 20  oc() system and 
33753 74 68 65 20 72 65 63 75 72 73 69 76 65 20 70 49  the recursive pI
33754 6e 69 74 4d 75 74 65 78 20 6d 75 74 65 78 2e 0a  nitMutex mutex..
33755 20 20 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74    ** This operat
33756 69 6f 6e 20 69 73 20 70 72 6f 74 65 63 74 65 64  ion is protected
33757 20 62 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d   by the STATIC_M
33758 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 4e 6f  ASTER mutex.  No
33759 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 4d 75 74  te that.  ** Mut
3375a 65 78 41 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c  exAlloc() is cal
3375b 6c 65 64 20 66 6f 72 20 61 20 73 74 61 74 69 63  led for a static
3375c 20 6d 75 74 65 78 20 70 72 69 6f 72 20 74 6f 20   mutex prior to 
3375d 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
3375e 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 73 75 62  .  ** malloc sub
3375f 73 79 73 74 65 6d 20 2d 20 74 68 69 73 20 69 6d  system - this im
33760 70 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 61  plies that the a
33761 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 73  llocation of a s
33762 74 61 74 69 63 0a 20 20 2a 2a 20 6d 75 74 65 78  tatic.  ** mutex
33763 20 6d 75 73 74 20 6e 6f 74 20 72 65 71 75 69 72   must not requir
33764 65 20 73 75 70 70 6f 72 74 20 66 72 6f 6d 20 74  e support from t
33765 68 65 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73  he malloc subsys
33766 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73  tem..  */.  pMas
33767 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  ter = sqlite3Mut
33768 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
33769 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
3376a 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ER);.  sqlite3_m
3376b 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74  utex_enter(pMast
3376c 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 6c  er);.  sqlite3Gl
3376d 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74  obalConfig.isMut
3376e 65 78 49 6e 69 74 20 3d 20 31 3b 0a 20 20 69 66  exInit = 1;.  if
3376f 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ( !sqlite3Global
33770 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49  Config.isMallocI
33771 6e 69 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  nit ){.    rc = 
33772 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69  sqlite3MallocIni
33773 74 28 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  t();.  }.  if( r
33774 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
33775 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
33776 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63  lConfig.isMalloc
33777 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 69 66  Init = 1;.    if
33778 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ( !sqlite3Global
33779 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65  Config.pInitMute
3377a 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  x ){.      sqlit
3377b 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
3377c 49 6e 69 74 4d 75 74 65 78 20 3d 0a 20 20 20 20  InitMutex =.    
3377d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 75         sqlite3Mu
3377e 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
3377f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29  MUTEX_RECURSIVE)
33780 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
33781 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33782 62 43 6f 72 65 4d 75 74 65 78 20 26 26 20 21 73  bCoreMutex && !s
33783 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33784 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 29 7b  ig.pInitMutex ){
33785 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
33786 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
33787 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
33788 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
33789 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
3378a 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65  GlobalConfig.nRe
3378b 66 49 6e 69 74 4d 75 74 65 78 2b 2b 3b 0a 20 20  fInitMutex++;.  
3378c 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
3378d 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29  x_leave(pMaster)
3378e 3b 0a 0a 20 20 2f 2a 20 49 66 20 72 63 20 69 73  ;..  /* If rc is
3378f 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4f 4b 20 61   not SQLITE_OK a
33790 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68  t this point, th
33791 65 6e 20 65 69 74 68 65 72 20 74 68 65 20 6d 61  en either the ma
33792 6c 6c 6f 63 0a 20 20 2a 2a 20 73 75 62 73 79 73  lloc.  ** subsys
33793 74 65 6d 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  tem could not be
33794 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6f 72 20   initialized or 
33795 74 68 65 20 73 79 73 74 65 6d 20 66 61 69 6c 65  the system faile
33796 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a 20 20  d to allocate.  
33797 2a 2a 20 74 68 65 20 70 49 6e 69 74 4d 75 74 65  ** the pInitMute
33798 78 20 6d 75 74 65 78 2e 20 52 65 74 75 72 6e 20  x mutex. Return 
33799 61 6e 20 65 72 72 6f 72 20 69 6e 20 65 69 74 68  an error in eith
3379a 65 72 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 69  er case.  */.  i
3379b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3379c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
3379d 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20  c;.  }..  /* Do 
3379e 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20  the rest of the 
3379f 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75  initialization u
337a0 6e 64 65 72 20 74 68 65 20 72 65 63 75 72 73 69  nder the recursi
337a1 76 65 20 6d 75 74 65 78 20 73 6f 0a 20 20 2a 2a  ve mutex so.  **
337a2 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65   that we will be
337a3 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20   able to handle 
337a4 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20  recursive calls 
337a5 69 6e 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65  into.  ** sqlite
337a6 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20  3_initialize(). 
337a7 20 54 68 65 20 72 65 63 75 72 73 69 76 65 20 63   The recursive c
337a8 61 6c 6c 73 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f  alls normally co
337a9 6d 65 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20  me through.  ** 
337aa 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
337ab 29 20 77 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65  ) when it invoke
337ac 73 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65  s sqlite3_vfs_re
337ad 67 69 73 74 65 72 28 29 2c 20 62 75 74 20 6f 74  gister(), but ot
337ae 68 65 72 0a 20 20 2a 2a 20 72 65 63 75 72 73 69  her.  ** recursi
337af 76 65 20 63 61 6c 6c 73 20 6d 69 67 68 74 20 61  ve calls might a
337b0 6c 73 6f 20 62 65 20 70 6f 73 73 69 62 6c 65 2e  lso be possible.
337b1 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
337b2 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69  mutex_enter(sqli
337b3 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
337b4 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a 20 20 69  pInitMutex);.  i
337b5 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
337b6 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 3d 3d 30  Config.isInit==0
337b7 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
337b8 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65  lConfig.inProgre
337b9 73 73 3d 3d 30 20 29 7b 0a 20 20 20 20 46 75 6e  ss==0 ){.    Fun
337ba 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20  cDefHash *pHash 
337bb 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  = &GLOBAL(FuncDe
337bc 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c  fHash, sqlite3Gl
337bd 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a  obalFunctions);.
337be 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
337bf 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65  lConfig.inProgre
337c0 73 73 20 3d 20 31 3b 0a 20 20 20 20 6d 65 6d 73  ss = 1;.    mems
337c1 65 74 28 70 48 61 73 68 2c 20 30 2c 20 73 69 7a  et(pHash, 0, siz
337c2 65 6f 66 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  eof(sqlite3Globa
337c3 6c 46 75 6e 63 74 69 6f 6e 73 29 29 3b 0a 20 20  lFunctions));.  
337c4 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65    sqlite3Registe
337c5 72 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73  rGlobalFunctions
337c6 28 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ();.    if( sqli
337c7 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
337c8 69 73 50 43 61 63 68 65 49 6e 69 74 3d 3d 30 20  isPCacheInit==0 
337c9 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
337ca 6c 69 74 65 33 50 63 61 63 68 65 49 6e 69 74 69  lite3PcacheIniti
337cb 61 6c 69 7a 65 28 29 3b 0a 20 20 20 20 7d 0a 20  alize();.    }. 
337cc 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
337cd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
337ce 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
337cf 67 2e 69 73 50 43 61 63 68 65 49 6e 69 74 20 3d  g.isPCacheInit =
337d0 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   1;.      rc = s
337d1 71 6c 69 74 65 33 4f 73 49 6e 69 74 28 29 3b 0a  qlite3OsInit();.
337d2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
337d3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
337d4 20 20 20 20 20 73 71 6c 69 74 65 33 50 43 61 63       sqlite3PCac
337d5 68 65 42 75 66 66 65 72 53 65 74 75 70 28 20 73  heBufferSetup( s
337d6 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
337d7 69 67 2e 70 50 61 67 65 2c 20 0a 20 20 20 20 20  ig.pPage, .     
337d8 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
337d9 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2c  alConfig.szPage,
337da 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
337db 6e 66 69 67 2e 6e 50 61 67 65 29 3b 0a 20 20 20  nfig.nPage);.   
337dc 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
337dd 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 3d 20  Config.isInit = 
337de 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  1;.    }.    sql
337df 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
337e0 2e 69 6e 50 72 6f 67 72 65 73 73 20 3d 20 30 3b  .inProgress = 0;
337e1 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
337e2 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74  utex_leave(sqlit
337e3 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
337e4 49 6e 69 74 4d 75 74 65 78 29 3b 0a 0a 20 20 2f  InitMutex);..  /
337e5 2a 20 47 6f 20 62 61 63 6b 20 75 6e 64 65 72 20  * Go back under 
337e6 74 68 65 20 73 74 61 74 69 63 20 6d 75 74 65 78  the static mutex
337e7 20 61 6e 64 20 63 6c 65 61 6e 20 75 70 20 74 68   and clean up th
337e8 65 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a  e recursive.  **
337e9 20 6d 75 74 65 78 20 74 6f 20 70 72 65 76 65 6e   mutex to preven
337ea 74 20 61 20 72 65 73 6f 75 72 63 65 20 6c 65 61  t a resource lea
337eb 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  k..  */.  sqlite
337ec 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d  3_mutex_enter(pM
337ed 61 73 74 65 72 29 3b 0a 20 20 73 71 6c 69 74 65  aster);.  sqlite
337ee 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52  3GlobalConfig.nR
337ef 65 66 49 6e 69 74 4d 75 74 65 78 2d 2d 3b 0a 20  efInitMutex--;. 
337f0 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
337f1 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69  alConfig.nRefIni
337f2 74 4d 75 74 65 78 3c 3d 30 20 29 7b 0a 20 20 20  tMutex<=0 ){.   
337f3 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
337f4 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65  GlobalConfig.nRe
337f5 66 49 6e 69 74 4d 75 74 65 78 3d 3d 30 20 29 3b  fInitMutex==0 );
337f6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
337f7 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 47  ex_free(sqlite3G
337f8 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69  lobalConfig.pIni
337f9 74 4d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c  tMutex);.    sql
337fa 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
337fb 2e 70 49 6e 69 74 4d 75 74 65 78 20 3d 20 30 3b  .pInitMutex = 0;
337fc 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
337fd 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74  utex_leave(pMast
337fe 65 72 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66  er);..  /* The f
337ff 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 6a 75 73 74  ollowing is just
33800 20 61 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20   a sanity check 
33801 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 53 51 4c  to make sure SQL
33802 69 74 65 20 68 61 73 0a 20 20 2a 2a 20 62 65 65  ite has.  ** bee
33803 6e 20 63 6f 6d 70 69 6c 65 64 20 63 6f 72 72 65  n compiled corre
33804 63 74 6c 79 2e 20 20 49 74 20 69 73 20 69 6d 70  ctly.  It is imp
33805 6f 72 74 61 6e 74 20 74 6f 20 72 75 6e 20 74 68  ortant to run th
33806 69 73 20 63 6f 64 65 2c 20 62 75 74 0a 20 20 2a  is code, but.  *
33807 2a 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20  * we don't want 
33808 74 6f 20 72 75 6e 20 69 74 20 74 6f 6f 20 6f 66  to run it too of
33809 74 65 6e 20 61 6e 64 20 73 6f 61 6b 20 75 70 20  ten and soak up 
3380a 43 50 55 20 63 79 63 6c 65 73 20 66 6f 72 20 6e  CPU cycles for n
3380b 6f 0a 20 20 2a 2a 20 72 65 61 73 6f 6e 2e 20 20  o.  ** reason.  
3380c 53 6f 20 77 65 20 72 75 6e 20 69 74 20 6f 6e 63  So we run it onc
3380d 65 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c  e during initial
3380e 69 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69  ization..  */.#i
3380f 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 69 66  fndef NDEBUG.#if
33810 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33811 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
33812 20 20 2f 2a 20 54 68 69 73 20 73 65 63 74 69 6f    /* This sectio
33813 6e 20 6f 66 20 63 6f 64 65 27 73 20 6f 6e 6c 79  n of code's only
33814 20 22 6f 75 74 70 75 74 22 20 69 73 20 76 69 61   "output" is via
33815 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
33816 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 20 28 20  ents. */.  if ( 
33817 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
33818 0a 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28  .    u64 x = (((
33819 75 36 34 29 31 29 3c 3c 36 33 29 2d 31 3b 0a 20  u64)1)<<63)-1;. 
3381a 20 20 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20     double y;.   
3381b 20 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78   assert(sizeof(x
3381c 29 3d 3d 38 29 3b 0a 20 20 20 20 61 73 73 65 72  )==8);.    asser
3381d 74 28 73 69 7a 65 6f 66 28 78 29 3d 3d 73 69 7a  t(sizeof(x)==siz
3381e 65 6f 66 28 79 29 29 3b 0a 20 20 20 20 6d 65 6d  eof(y));.    mem
3381f 63 70 79 28 26 79 2c 20 26 78 2c 20 38 29 3b 0a  cpy(&y, &x, 8);.
33820 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
33821 74 65 33 49 73 4e 61 4e 28 79 29 20 29 3b 0a 20  te3IsNaN(y) );. 
33822 20 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66   }.#endif.#endif
33823 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
33824 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65  ../*.** Undo the
33825 20 65 66 66 65 63 74 73 20 6f 66 20 73 71 6c 69   effects of sqli
33826 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
33827 2e 20 20 4d 75 73 74 20 6e 6f 74 20 62 65 20 63  .  Must not be c
33828 61 6c 6c 65 64 20 77 68 69 6c 65 0a 2a 2a 20 74  alled while.** t
33829 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e  here are outstan
3382a 64 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f  ding database co
3382b 6e 6e 65 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d  nnections or mem
3382c 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ory allocations 
3382d 6f 72 0a 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20  or.** while any 
3382e 70 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 69  part of SQLite i
3382f 73 20 6f 74 68 65 72 77 69 73 65 20 69 6e 20 75  s otherwise in u
33830 73 65 20 69 6e 20 61 6e 79 20 74 68 72 65 61 64  se in any thread
33831 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69  .  This.** routi
33832 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64  ne is not thread
33833 73 61 66 65 2e 20 20 42 75 74 20 69 74 20 69 73  safe.  But it is
33834 20 73 61 66 65 20 74 6f 20 69 6e 76 6f 6b 65 20   safe to invoke 
33835 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
33836 6f 6e 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69  on when SQLite i
33837 73 20 61 6c 72 65 61 64 79 20 73 68 75 74 20 64  s already shut d
33838 6f 77 6e 2e 20 20 49 66 20 53 51 4c 69 74 65 20  own.  If SQLite 
33839 69 73 20 61 6c 72 65 61 64 79 20 73 68 75 74 20  is already shut 
3383a 64 6f 77 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69  down.** when thi
3383b 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 76  s routine is inv
3383c 6f 6b 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20  oked, then this 
3383d 72 6f 75 74 69 6e 65 20 69 73 20 61 20 68 61 72  routine is a har
3383e 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a  mless no-op..*/.
3383f 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33840 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
33841 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c  void){.  if( sql
33842 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33843 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 73  .isInit ){.    s
33844 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 3b  qlite3_os_end();
33845 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
33846 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f  et_auto_extensio
33847 6e 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  n();.    sqlite3
33848 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
33849 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  nit = 0;.  }.  i
3384a 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
3384b 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68 65 49  Config.isPCacheI
3384c 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nit ){.    sqlit
3384d 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e  e3PcacheShutdown
3384e 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  ();.    sqlite3G
3384f 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43  lobalConfig.isPC
33850 61 63 68 65 49 6e 69 74 20 3d 20 30 3b 0a 20 20  acheInit = 0;.  
33851 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  }.  if( sqlite3G
33852 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61  lobalConfig.isMa
33853 6c 6c 6f 63 49 6e 69 74 20 29 7b 0a 20 20 20 20  llocInit ){.    
33854 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64  sqlite3MallocEnd
33855 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  ();.    sqlite3G
33856 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61  lobalConfig.isMa
33857 6c 6c 6f 63 49 6e 69 74 20 3d 20 30 3b 0a 20 20  llocInit = 0;.  
33858 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  }.  if( sqlite3G
33859 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75  lobalConfig.isMu
3385a 74 65 78 49 6e 69 74 20 29 7b 0a 20 20 20 20 73  texInit ){.    s
3385b 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 29  qlite3MutexEnd()
3385c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
3385d 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65  balConfig.isMute
3385e 78 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a  xInit = 0;.  }..
3385f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
33860 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
33861 73 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70  s API allows app
33862 6c 69 63 61 74 69 6f 6e 73 20 74 6f 20 6d 6f 64  lications to mod
33863 69 66 79 20 74 68 65 20 67 6c 6f 62 61 6c 20 63  ify the global c
33864 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 66 0a  onfiguration of.
33865 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  ** the SQLite li
33866 62 72 61 72 79 20 61 74 20 72 75 6e 2d 74 69 6d  brary at run-tim
33867 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
33868 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c  utine should onl
33869 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  y be called when
3386a 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75   there are no ou
3386b 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 64 61 74  tstanding.** dat
3386c 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3386d 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  s or memory allo
3386e 63 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 72  cations.  This r
3386f 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 0a 2a 2a  outine is not.**
33870 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 46 61   threadsafe.  Fa
33871 69 6c 75 72 65 20 74 6f 20 68 65 65 64 20 74 68  ilure to heed th
33872 65 73 65 20 77 61 72 6e 69 6e 67 73 20 63 61 6e  ese warnings can
33873 20 6c 65 61 64 20 74 6f 20 75 6e 70 72 65 64 69   lead to unpredi
33874 63 74 61 62 6c 65 0a 2a 2a 20 62 65 68 61 76 69  ctable.** behavi
33875 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or..*/.SQLITE_AP
33876 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
33877 6e 66 69 67 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e  nfig(int op, ...
33878 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
33879 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
3387a 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 73 71 6c  TE_OK;..  /* sql
3387b 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 73 68  ite3_config() sh
3387c 61 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  all return SQLIT
3387d 45 5f 4d 49 53 55 53 45 20 69 66 20 69 74 20 69  E_MISUSE if it i
3387e 73 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 0a  s invoked while.
3387f 20 20 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20    ** the SQLite 
33880 6c 69 62 72 61 72 79 20 69 73 20 69 6e 20 75 73  library is in us
33881 65 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  e. */.  if( sqli
33882 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33883 69 73 49 6e 69 74 20 29 20 72 65 74 75 72 6e 20  isInit ) return 
33884 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 0a  SQLITE_MISUSE;..
33885 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f    va_start(ap, o
33886 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  p);.  switch( op
33887 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 4d 75 74 65   ){..    /* Mute
33888 78 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  x configuration 
33889 6f 70 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79  options are only
3388a 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 20   available in a 
3388b 74 68 72 65 61 64 73 61 66 65 0a 20 20 20 20 2a  threadsafe.    *
3388c 2a 20 63 6f 6d 70 69 6c 65 2e 20 0a 20 20 20 20  * compile. .    
3388d 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
3388e 52 45 41 44 53 41 46 45 0a 20 20 20 20 63 61 73  READSAFE.    cas
3388f 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
33890 53 49 4e 47 4c 45 54 48 52 45 41 44 3a 20 7b 0a  SINGLETHREAD: {.
33891 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65        /* Disable
33892 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f   all mutexing */
33893 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
33894 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
33895 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20  Mutex = 0;.     
33896 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
33897 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20  nfig.bFullMutex 
33898 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  = 0;.      break
33899 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
3389a 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
3389b 55 4c 54 49 54 48 52 45 41 44 3a 20 7b 0a 20 20  ULTITHREAD: {.  
3389c 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 6d      /* Disable m
3389d 75 74 65 78 69 6e 67 20 6f 66 20 64 61 74 61 62  utexing of datab
3389e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
3389f 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62  */.      /* Enab
338a0 6c 65 20 6d 75 74 65 78 69 6e 67 20 6f 66 20 63  le mutexing of c
338a1 6f 72 65 20 64 61 74 61 20 73 74 72 75 63 74 75  ore data structu
338a2 72 65 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  res */.      sql
338a3 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
338a4 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b  .bCoreMutex = 1;
338a5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
338a6 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c  obalConfig.bFull
338a7 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20  Mutex = 0;.     
338a8 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
338a9 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
338aa 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 3a  NFIG_SERIALIZED:
338ab 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62   {.      /* Enab
338ac 6c 65 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20  le all mutexing 
338ad 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
338ae 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
338af 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20 20  reMutex = 1;.   
338b0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
338b1 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65  Config.bFullMute
338b2 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65  x = 1;.      bre
338b3 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
338b4 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
338b5 5f 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20 20 20  _MUTEX: {.      
338b6 2f 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c  /* Specify an al
338b7 74 65 72 6e 61 74 69 76 65 20 6d 75 74 65 78 20  ternative mutex 
338b8 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
338b9 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
338ba 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
338bb 78 20 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20  x = *va_arg(ap, 
338bc 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
338bd 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62  thods*);.      b
338be 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
338bf 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
338c0 49 47 5f 47 45 54 4d 55 54 45 58 3a 20 7b 0a 20  IG_GETMUTEX: {. 
338c1 20 20 20 20 20 2f 2a 20 52 65 74 72 69 65 76 65       /* Retrieve
338c2 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74   the current mut
338c3 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
338c4 6e 20 2a 2f 0a 20 20 20 20 20 20 2a 76 61 5f 61  n */.      *va_a
338c5 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d  rg(ap, sqlite3_m
338c6 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29 20 3d  utex_methods*) =
338c7 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
338c8 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 20 20 20 20  nfig.mutex;.    
338c9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
338ca 65 6e 64 69 66 0a 0a 0a 20 20 20 20 63 61 73 65  endif...    case
338cb 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
338cc 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f  ALLOC: {.      /
338cd 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74  * Specify an alt
338ce 65 72 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 20  ernative malloc 
338cf 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
338d0 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
338d1 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20  lobalConfig.m = 
338d2 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69  *va_arg(ap, sqli
338d3 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a  te3_mem_methods*
338d4 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
338d5 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
338d6 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
338d7 4d 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20  MALLOC: {.      
338d8 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20  /* Retrieve the 
338d9 63 75 72 72 65 6e 74 20 6d 61 6c 6c 6f 63 28 29  current malloc()
338da 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
338db 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  */.      if( sql
338dc 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
338dd 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 20  .m.xMalloc==0 ) 
338de 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66  sqlite3MemSetDef
338df 61 75 6c 74 28 29 3b 0a 20 20 20 20 20 20 2a 76  ault();.      *v
338e0 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65  a_arg(ap, sqlite
338e1 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 20  3_mem_methods*) 
338e2 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
338e3 6f 6e 66 69 67 2e 6d 3b 0a 20 20 20 20 20 20 62  onfig.m;.      b
338e4 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
338e5 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
338e6 49 47 5f 4d 45 4d 53 54 41 54 55 53 3a 20 7b 0a  IG_MEMSTATUS: {.
338e7 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
338e8 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 6d  or disable the m
338e9 61 6c 6c 6f 63 20 73 74 61 74 75 73 20 63 6f 6c  alloc status col
338ea 6c 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  lection */.     
338eb 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
338ec 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 3d 20  nfig.bMemstat = 
338ed 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
338ee 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
338ef 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
338f0 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
338f1 43 48 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44  CH: {.      /* D
338f2 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65  esignate a buffe
338f3 72 20 66 6f 72 20 73 63 72 61 74 63 68 20 6d 65  r for scratch me
338f4 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20  mory space */.  
338f5 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
338f6 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
338f7 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
338f8 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  id*);.      sqli
338f9 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
338fa 73 7a 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61  szScratch = va_a
338fb 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
338fc 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
338fd 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20  Config.nScratch 
338fe 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
338ff 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
33900 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
33901 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
33902 45 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20  ECACHE: {.      
33903 2f 2a 20 44 65 73 69 67 6e 61 74 65 20 61 20 62  /* Designate a b
33904 75 66 66 65 72 20 66 6f 72 20 70 61 67 65 20 63  uffer for page c
33905 61 63 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63  ache memory spac
33906 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  e */.      sqlit
33907 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
33908 50 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70  Page = va_arg(ap
33909 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20  , void*);.      
3390a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3390b 66 69 67 2e 73 7a 50 61 67 65 20 3d 20 76 61 5f  fig.szPage = va_
3390c 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
3390d 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3390e 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 3d 20  lConfig.nPage = 
3390f 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
33910 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
33911 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 51    }..    case SQ
33912 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43  LITE_CONFIG_PCAC
33913 48 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53  HE: {.      /* S
33914 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
33915 61 74 69 76 65 20 70 61 67 65 20 63 61 63 68 65  ative page cache
33916 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
33917 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
33918 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
33919 63 68 65 20 3d 20 2a 76 61 5f 61 72 67 28 61 70  che = *va_arg(ap
3391a 2c 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  , sqlite3_pcache
3391b 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20  _methods*);.    
3391c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
3391d 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
3391e 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45  CONFIG_GETPCACHE
3391f 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  : {.      if( sq
33920 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33921 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 3d 3d  g.pcache.xInit==
33922 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
33923 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66  ite3PCacheSetDef
33924 61 75 6c 74 28 29 3b 0a 20 20 20 20 20 20 7d 0a  ault();.      }.
33925 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70        *va_arg(ap
33926 2c 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  , sqlite3_pcache
33927 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c  _methods*) = sql
33928 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33929 2e 70 63 61 63 68 65 3b 0a 20 20 20 20 20 20 62  .pcache;.      b
3392a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
3392b 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3392c 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 29 20  ENABLE_MEMSYS3) 
3392d 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
3392e 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  E_ENABLE_MEMSYS5
3392f 29 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ).    case SQLIT
33930 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3a 20 7b  E_CONFIG_HEAP: {
33931 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67 6e  .      /* Design
33932 61 74 65 20 61 20 62 75 66 66 65 72 20 66 6f 72  ate a buffer for
33933 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 73 70 61   heap memory spa
33934 63 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ce */.      sqli
33935 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33936 70 48 65 61 70 20 3d 20 76 61 5f 61 72 67 28 61  pHeap = va_arg(a
33937 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20  p, void*);.     
33938 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
33939 6e 66 69 67 2e 6e 48 65 61 70 20 3d 20 76 61 5f  nfig.nHeap = va_
3393a 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
3393b 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3393c 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 20 3d 20  lConfig.mnReq = 
3393d 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
3393e 0a 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ..      if( sqli
3393f 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33940 70 48 65 61 70 3d 3d 30 20 29 7b 0a 20 20 20 20  pHeap==0 ){.    
33941 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 68 65      /* If the he
33942 61 70 20 70 6f 69 6e 74 65 72 20 69 73 20 4e 55  ap pointer is NU
33943 4c 4c 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65  LL, then restore
33944 20 74 68 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c   the malloc impl
33945 65 6d 65 6e 74 61 74 69 6f 6e 0a 20 20 20 20 20  ementation.     
33946 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 4e 55     ** back to NU
33947 4c 4c 20 70 6f 69 6e 74 65 72 73 20 74 6f 6f 2e  LL pointers too.
33948 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73    This will caus
33949 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 74 6f 20  e the malloc to 
3394a 67 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 61  go.        ** ba
3394b 63 6b 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c  ck to its defaul
3394c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
3394d 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e   when sqlite3_in
3394e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 0a 20 20  itialize() is.  
3394f 20 20 20 20 20 20 2a 2a 20 72 75 6e 2e 0a 20 20        ** run..  
33950 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
33951 20 6d 65 6d 73 65 74 28 26 73 71 6c 69 74 65 33   memset(&sqlite3
33952 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2c 20  GlobalConfig.m, 
33953 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65  0, sizeof(sqlite
33954 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 29  3GlobalConfig.m)
33955 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
33956 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68          /* The h
33957 65 61 70 20 70 6f 69 6e 74 65 72 20 69 73 20 6e  eap pointer is n
33958 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 6e  ot NULL, then in
33959 73 74 61 6c 6c 20 6f 6e 65 20 6f 66 20 74 68 65  stall one of the
3395a 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 65 6d 35  .        ** mem5
3395b 2e 63 2f 6d 65 6d 33 2e 63 20 6d 65 74 68 6f 64  .c/mem3.c method
3395c 73 2e 20 49 66 20 6e 65 69 74 68 65 72 20 45 4e  s. If neither EN
3395d 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 6e 6f 72  ABLE_MEMSYS3 nor
3395e 0a 20 20 20 20 20 20 20 20 2a 2a 20 45 4e 41 42  .        ** ENAB
3395f 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65  LE_MEMSYS5 is de
33960 66 69 6e 65 64 2c 20 72 65 74 75 72 6e 20 61 6e  fined, return an
33961 20 65 72 72 6f 72 2e 0a 20 20 20 20 20 20 20 20   error..        
33962 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
33963 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 0a  _ENABLE_MEMSYS3.
33964 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
33965 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20  lobalConfig.m = 
33966 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65  *sqlite3MemGetMe
33967 6d 73 79 73 33 28 29 3b 0a 23 65 6e 64 69 66 0a  msys3();.#endif.
33968 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
33969 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 20 20 20  ABLE_MEMSYS5.   
3396a 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3396b 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71  alConfig.m = *sq
3396c 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79  lite3MemGetMemsy
3396d 73 35 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  s5();.#endif.   
3396e 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
3396f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
33970 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
33971 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
33972 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
33973 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
33974 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 76 61 5f 61  Lookaside = va_a
33975 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
33976 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
33977 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61 73 69 64  Config.nLookasid
33978 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  e = va_arg(ap, i
33979 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  nt);.      break
3397a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 65 66  ;.    }..    def
3397b 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63  ault: {.      rc
3397c 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
3397d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3397e 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64    }.  }.  va_end
3397f 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  (ap);.  return r
33980 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  c;.}../*.** Set 
33981 75 70 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65  up the lookaside
33982 20 62 75 66 66 65 72 73 20 66 6f 72 20 61 20 64   buffers for a d
33983 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
33984 6f 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  on..** Return SQ
33985 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
33986 73 73 2e 20 20 0a 2a 2a 20 49 66 20 6c 6f 6f 6b  ss.  .** If look
33987 61 73 69 64 65 20 69 73 20 61 6c 72 65 61 64 79  aside is already
33988 20 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20   active, return 
33989 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a  SQLITE_BUSY..**.
3398a 2a 2a 20 54 68 65 20 73 7a 20 70 61 72 61 6d 65  ** The sz parame
3398b 74 65 72 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ter is the numbe
3398c 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 65 61  r of bytes in ea
3398d 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f  ch lookaside slo
3398e 74 2e 0a 2a 2a 20 54 68 65 20 63 6e 74 20 70 61  t..** The cnt pa
3398f 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e  rameter is the n
33990 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 2e 20  umber of slots. 
33991 20 49 66 20 70 53 74 61 72 74 20 69 73 20 4e 55   If pStart is NU
33992 4c 4c 20 74 68 65 0a 2a 2a 20 73 70 61 63 65 20  LL the.** space 
33993 66 6f 72 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64  for the lookasid
33994 65 20 6d 65 6d 6f 72 79 20 69 73 20 6f 62 74 61  e memory is obta
33995 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
33996 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49  3_malloc()..** I
33997 66 20 70 53 74 61 72 74 20 69 73 20 6e 6f 74 20  f pStart is not 
33998 4e 55 4c 4c 20 74 68 65 6e 20 69 74 20 69 73 20  NULL then it is 
33999 73 7a 2a 63 6e 74 20 62 79 74 65 73 20 6f 66 20  sz*cnt bytes of 
3399a 6d 65 6d 6f 72 79 20 74 6f 20 75 73 65 20 66 6f  memory to use fo
3399b 72 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69  r.** the lookasi
3399c 64 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  de memory..*/.st
3399d 61 74 69 63 20 69 6e 74 20 73 65 74 75 70 4c 6f  atic int setupLo
3399e 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 65 33 20  okaside(sqlite3 
3399f 2a 64 62 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c  *db, void *pBuf,
339a0 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 63 6e 74   int sz, int cnt
339a1 29 7b 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72  ){.  void *pStar
339a2 74 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f  t;.  if( db->loo
339a3 6b 61 73 69 64 65 2e 6e 4f 75 74 20 29 7b 0a 20  kaside.nOut ){. 
339a4 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
339a5 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20  _BUSY;.  }.  /* 
339a6 46 72 65 65 20 61 6e 79 20 65 78 69 73 74 69 6e  Free any existin
339a7 67 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66  g lookaside buff
339a8 65 72 20 66 6f 72 20 74 68 69 73 20 68 61 6e 64  er for this hand
339a9 6c 65 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61  le before.  ** a
339aa 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20  llocating a new 
339ab 6f 6e 65 20 73 6f 20 77 65 20 64 6f 6e 27 74 20  one so we don't 
339ac 68 61 76 65 20 74 6f 20 68 61 76 65 20 73 70 61  have to have spa
339ad 63 65 20 66 6f 72 20 0a 20 20 2a 2a 20 62 6f 74  ce for .  ** bot
339ae 68 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  h at the same ti
339af 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  me..  */.  if( d
339b0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61  b->lookaside.bMa
339b1 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20 20 73 71  lloced ){.    sq
339b2 6c 69 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c  lite3_free(db->l
339b3 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29  ookaside.pStart)
339b4 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 65 20 73  ;.  }.  /* The s
339b5 69 7a 65 20 6f 66 20 61 20 6c 6f 6f 6b 61 73 69  ize of a lookasi
339b6 64 65 20 73 6c 6f 74 20 6e 65 65 64 73 20 74 6f  de slot needs to
339b7 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20   be larger than 
339b8 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74  a pointer.  ** t
339b9 6f 20 62 65 20 75 73 65 66 75 6c 2e 0a 20 20 2a  o be useful..  *
339ba 2f 0a 20 20 69 66 28 20 73 7a 3c 3d 28 69 6e 74  /.  if( sz<=(int
339bb 29 73 69 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64  )sizeof(Lookasid
339bc 65 53 6c 6f 74 2a 29 20 29 20 73 7a 20 3d 20 30  eSlot*) ) sz = 0
339bd 3b 0a 20 20 69 66 28 20 63 6e 74 3c 30 20 29 20  ;.  if( cnt<0 ) 
339be 63 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  cnt = 0;.  if( s
339bf 7a 3d 3d 30 20 7c 7c 20 63 6e 74 3d 3d 30 20 29  z==0 || cnt==0 )
339c0 7b 0a 20 20 20 20 73 7a 20 3d 20 30 3b 0a 20 20  {.    sz = 0;.  
339c1 20 20 70 53 74 61 72 74 20 3d 20 30 3b 0a 20 20    pStart = 0;.  
339c2 7d 65 6c 73 65 20 69 66 28 20 70 42 75 66 3d 3d  }else if( pBuf==
339c3 30 20 29 7b 0a 20 20 20 20 73 7a 20 3d 20 52 4f  0 ){.    sz = RO
339c4 55 4e 44 38 28 73 7a 29 3b 0a 20 20 20 20 73 71  UND8(sz);.    sq
339c5 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
339c6 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 70 53  Malloc();.    pS
339c7 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 4d 61  tart = sqlite3Ma
339c8 6c 6c 6f 63 28 20 73 7a 2a 63 6e 74 20 29 3b 0a  lloc( sz*cnt );.
339c9 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65      sqlite3EndBe
339ca 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
339cb 7d 65 6c 73 65 7b 0a 20 20 20 20 73 7a 20 3d 20  }else{.    sz = 
339cc 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a  ROUNDDOWN8(sz);.
339cd 20 20 20 20 70 53 74 61 72 74 20 3d 20 70 42 75      pStart = pBu
339ce 66 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f  f;.  }.  db->loo
339cf 6b 61 73 69 64 65 2e 70 53 74 61 72 74 20 3d 20  kaside.pStart = 
339d0 70 53 74 61 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f  pStart;.  db->lo
339d1 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20  okaside.pFree = 
339d2 30 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  0;.  db->lookasi
339d3 64 65 2e 73 7a 20 3d 20 28 75 31 36 29 73 7a 3b  de.sz = (u16)sz;
339d4 0a 20 20 69 66 28 20 70 53 74 61 72 74 20 29 7b  .  if( pStart ){
339d5 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
339d6 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70  LookasideSlot *p
339d7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a  ;.    assert( sz
339d8 20 3e 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 4c   > (int)sizeof(L
339d9 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 20 29  ookasideSlot*) )
339da 3b 0a 20 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61  ;.    p = (Looka
339db 73 69 64 65 53 6c 6f 74 2a 29 70 53 74 61 72 74  sideSlot*)pStart
339dc 3b 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 2d  ;.    for(i=cnt-
339dd 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20  1; i>=0; i--){. 
339de 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20       p->pNext = 
339df 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46  db->lookaside.pF
339e0 72 65 65 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c  ree;.      db->l
339e1 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d  ookaside.pFree =
339e2 20 70 3b 0a 20 20 20 20 20 20 70 20 3d 20 28 4c   p;.      p = (L
339e3 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 26 28  ookasideSlot*)&(
339e4 28 75 38 2a 29 70 29 5b 73 7a 5d 3b 0a 20 20 20  (u8*)p)[sz];.   
339e5 20 7d 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61   }.    db->looka
339e6 73 69 64 65 2e 70 45 6e 64 20 3d 20 70 3b 0a 20  side.pEnd = p;. 
339e7 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
339e8 2e 62 45 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 20  .bEnabled = 1;. 
339e9 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
339ea 2e 62 4d 61 6c 6c 6f 63 65 64 20 3d 20 70 42 75  .bMalloced = pBu
339eb 66 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 7d 65 6c  f==0 ?1:0;.  }el
339ec 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  se{.    db->look
339ed 61 73 69 64 65 2e 70 45 6e 64 20 3d 20 30 3b 0a  aside.pEnd = 0;.
339ee 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
339ef 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a  e.bEnabled = 0;.
339f0 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
339f1 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 3d 20 30 3b  e.bMalloced = 0;
339f2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
339f3 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
339f4 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 75 74  * Return the mut
339f5 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
339f6 74 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  th a database co
339f7 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
339f8 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
339f9 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64  mutex *sqlite3_d
339fa 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 20  b_mutex(sqlite3 
339fb 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64  *db){.  return d
339fc 62 2d 3e 6d 75 74 65 78 3b 0a 7d 0a 0a 2f 2a 0a  b->mutex;.}../*.
339fd 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ** Configuration
339fe 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 61 6e   settings for an
339ff 20 69 6e 64 69 76 69 64 75 61 6c 20 64 61 74 61   individual data
33a00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
33a01 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33a02 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e  t sqlite3_db_con
33a03 66 69 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  fig(sqlite3 *db,
33a04 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20   int op, ...){. 
33a05 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69   va_list ap;.  i
33a06 6e 74 20 72 63 3b 0a 20 20 76 61 5f 73 74 61 72  nt rc;.  va_star
33a07 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69  t(ap, op);.  swi
33a08 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
33a09 61 73 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e  ase SQLITE_DBCON
33a0a 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b  FIG_LOOKASIDE: {
33a0b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 42 75  .      void *pBu
33a0c 66 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76  f = va_arg(ap, v
33a0d 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74  oid*);.      int
33a0e 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c   sz = va_arg(ap,
33a0f 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e 74   int);.      int
33a10 20 63 6e 74 20 3d 20 76 61 5f 61 72 67 28 61 70   cnt = va_arg(ap
33a11 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63  , int);.      rc
33a12 20 3d 20 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64   = setupLookasid
33a13 65 28 64 62 2c 20 70 42 75 66 2c 20 73 7a 2c 20  e(db, pBuf, sz, 
33a14 63 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  cnt);.      brea
33a15 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
33a16 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63  ault: {.      rc
33a17 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
33a18 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
33a19 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64    }.  }.  va_end
33a1a 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  (ap);.  return r
33a1b 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  c;.}.../*.** Ret
33a1c 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
33a1d 62 75 66 66 65 72 20 7a 5b 30 2e 2e 6e 2d 31 5d  buffer z[0..n-1]
33a1e 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 73 70   contains all sp
33a1f 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  aces..*/.static 
33a20 69 6e 74 20 61 6c 6c 53 70 61 63 65 73 28 63 6f  int allSpaces(co
33a21 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
33a22 20 6e 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e   n){.  while( n>
33a23 30 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 20 27  0 && z[n-1]==' '
33a24 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 72 65 74   ){ n--; }.  ret
33a25 75 72 6e 20 6e 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  urn n==0;.}../*.
33a26 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64  ** This is the d
33a27 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67  efault collating
33a28 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20   function named 
33a29 22 42 49 4e 41 52 59 22 20 77 68 69 63 68 20 69  "BINARY" which i
33a2a 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 76 61 69  s always.** avai
33a2b 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  lable..**.** If 
33a2c 74 68 65 20 70 61 64 46 6c 61 67 20 61 72 67 75  the padFlag argu
33a2d 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ment is not NULL
33a2e 20 74 68 65 6e 20 73 70 61 63 65 20 70 61 64 64   then space padd
33a2f 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64 0a 2a  ing at the end.*
33a30 2a 20 6f 66 20 73 74 72 69 6e 67 73 20 69 73 20  * of strings is 
33a31 69 67 6e 6f 72 65 64 2e 20 20 54 68 69 73 20 69  ignored.  This i
33a32 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 52 54  mplements the RT
33a33 52 49 4d 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 2a  RIM collation..*
33a34 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e  /.static int bin
33a35 43 6f 6c 6c 46 75 6e 63 28 0a 20 20 76 6f 69 64  CollFunc(.  void
33a36 20 2a 70 61 64 46 6c 61 67 2c 0a 20 20 69 6e 74   *padFlag,.  int
33a37 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f   nKey1, const vo
33a38 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74  id *pKey1,.  int
33a39 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f   nKey2, const vo
33a3a 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69  id *pKey2.){.  i
33a3b 6e 74 20 72 63 2c 20 6e 3b 0a 20 20 6e 20 3d 20  nt rc, n;.  n = 
33a3c 6e 4b 65 79 31 3c 6e 4b 65 79 32 20 3f 20 6e 4b  nKey1<nKey2 ? nK
33a3d 65 79 31 20 3a 20 6e 4b 65 79 32 3b 0a 20 20 72  ey1 : nKey2;.  r
33a3e 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31  c = memcmp(pKey1
33a3f 2c 20 70 4b 65 79 32 2c 20 6e 29 3b 0a 20 20 69  , pKey2, n);.  i
33a40 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20  f( rc==0 ){.    
33a41 69 66 28 20 70 61 64 46 6c 61 67 0a 20 20 20 20  if( padFlag.    
33a42 20 26 26 20 61 6c 6c 53 70 61 63 65 73 28 28 28   && allSpaces(((
33a43 63 68 61 72 2a 29 70 4b 65 79 31 29 2b 6e 2c 20  char*)pKey1)+n, 
33a44 6e 4b 65 79 31 2d 6e 29 0a 20 20 20 20 20 26 26  nKey1-n).     &&
33a45 20 61 6c 6c 53 70 61 63 65 73 28 28 28 63 68 61   allSpaces(((cha
33a46 72 2a 29 70 4b 65 79 32 29 2b 6e 2c 20 6e 4b 65  r*)pKey2)+n, nKe
33a47 79 32 2d 6e 29 0a 20 20 20 20 29 7b 0a 20 20 20  y2-n).    ){.   
33a48 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 20 75     /* Leave rc u
33a49 6e 63 68 61 6e 67 65 64 20 61 74 20 30 20 2a 2f  nchanged at 0 */
33a4a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
33a4b 20 20 72 63 20 3d 20 6e 4b 65 79 31 20 2d 20 6e    rc = nKey1 - n
33a4c 4b 65 79 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Key2;.    }.  }.
33a4d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
33a4e 2f 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 62 75  /*.** Another bu
33a4f 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67  ilt-in collating
33a50 20 73 65 71 75 65 6e 63 65 3a 20 4e 4f 43 41 53   sequence: NOCAS
33a51 45 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63  E. .**.** This c
33a52 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
33a53 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  e is intended to
33a54 20 62 65 20 75 73 65 64 20 66 6f 72 20 22 63 61   be used for "ca
33a55 73 65 20 69 6e 64 65 70 65 6e 64 61 6e 74 0a 2a  se independant.*
33a56 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 22 2e 20 53  * comparison". S
33a57 51 4c 69 74 65 27 73 20 6b 6e 6f 77 6c 65 64 67  QLite's knowledg
33a58 65 20 6f 66 20 75 70 70 65 72 20 61 6e 64 20 6c  e of upper and l
33a59 6f 77 65 72 20 63 61 73 65 20 65 71 75 69 76 61  ower case equiva
33a5a 6c 65 6e 74 73 0a 2a 2a 20 65 78 74 65 6e 64 73  lents.** extends
33a5b 20 6f 6e 6c 79 20 74 6f 20 74 68 65 20 32 36 20   only to the 26 
33a5c 63 68 61 72 61 63 74 65 72 73 20 75 73 65 64 20  characters used 
33a5d 69 6e 20 74 68 65 20 45 6e 67 6c 69 73 68 20 6c  in the English l
33a5e 61 6e 67 75 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41  anguage..**.** A
33a5f 74 20 74 68 65 20 6d 6f 6d 65 6e 74 20 74 68 65  t the moment the
33a60 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 55 54 46  re is only a UTF
33a61 2d 38 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  -8 implementatio
33a62 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
33a63 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67   nocaseCollating
33a64 46 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a 4e 6f  Func(.  void *No
33a65 74 55 73 65 64 2c 0a 20 20 69 6e 74 20 6e 4b 65  tUsed,.  int nKe
33a66 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  y1, const void *
33a67 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65  pKey1,.  int nKe
33a68 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  y2, const void *
33a69 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72  pKey2.){.  int r
33a6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43   = sqlite3StrNIC
33a6b 6d 70 28 0a 20 20 20 20 20 20 28 63 6f 6e 73 74  mp(.      (const
33a6c 20 63 68 61 72 20 2a 29 70 4b 65 79 31 2c 20 28   char *)pKey1, (
33a6d 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65  const char *)pKe
33a6e 79 32 2c 20 28 6e 4b 65 79 31 3c 6e 4b 65 79 32  y2, (nKey1<nKey2
33a6f 29 3f 6e 4b 65 79 31 3a 6e 4b 65 79 32 29 3b 0a  )?nKey1:nKey2);.
33a70 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
33a71 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69  ER(NotUsed);.  i
33a72 66 28 20 30 3d 3d 72 20 29 7b 0a 20 20 20 20 72  f( 0==r ){.    r
33a73 20 3d 20 6e 4b 65 79 31 2d 6e 4b 65 79 32 3b 0a   = nKey1-nKey2;.
33a74 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a    }.  return r;.
33a75 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
33a76 74 68 65 20 52 4f 57 49 44 20 6f 66 20 74 68 65  the ROWID of the
33a77 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73   most recent ins
33a78 65 72 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ert.*/.SQLITE_AP
33a79 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73  I sqlite_int64 s
33a7a 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
33a7b 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33  rt_rowid(sqlite3
33a7c 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20   *db){.  return 
33a7d 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 7d  db->lastRowid;.}
33a7e 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
33a7f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
33a80 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74  nges in the most
33a81 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20   recent call to 
33a82 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a  sqlite3_exec()..
33a83 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33a84 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65  t sqlite3_change
33a85 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
33a86 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e 43 68    return db->nCh
33a87 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ange;.}../*.** R
33a88 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
33a89 20 6f 66 20 63 68 61 6e 67 65 73 20 73 69 6e 63   of changes sinc
33a8a 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  e the database h
33a8b 61 6e 64 6c 65 20 77 61 73 20 6f 70 65 6e 65 64  andle was opened
33a8c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33a8d 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  int sqlite3_tota
33a8e 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65  l_changes(sqlite
33a8f 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e  3 *db){.  return
33a90 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67   db->nTotalChang
33a91 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  e;.}../*.** Clos
33a92 65 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70  e all open savep
33a93 6f 69 6e 74 73 2e 20 54 68 69 73 20 66 75 6e 63  oints. This func
33a94 74 69 6f 6e 20 6f 6e 6c 79 20 6d 61 6e 69 70 75  tion only manipu
33a95 6c 61 74 65 73 20 66 69 65 6c 64 73 20 6f 66 20  lates fields of 
33a96 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
33a97 68 61 6e 64 6c 65 20 6f 62 6a 65 63 74 2c 20 69  handle object, i
33a98 74 20 64 6f 65 73 20 6e 6f 74 20 63 6c 6f 73 65  t does not close
33a99 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20   any savepoints 
33a9a 74 68 61 74 20 6d 61 79 20 62 65 20 6f 70 65 6e  that may be open
33a9b 0a 2a 2a 20 61 74 20 74 68 65 20 62 2d 74 72 65  .** at the b-tre
33a9c 65 2f 70 61 67 65 72 20 6c 65 76 65 6c 2e 0a 2a  e/pager level..*
33a9d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
33a9e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f   void sqlite3Clo
33a9f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 73 71 6c  seSavepoints(sql
33aa0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 77 68 69  ite3 *db){.  whi
33aa1 6c 65 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69  le( db->pSavepoi
33aa2 6e 74 20 29 7b 0a 20 20 20 20 53 61 76 65 70 6f  nt ){.    Savepo
33aa3 69 6e 74 20 2a 70 54 6d 70 20 3d 20 64 62 2d 3e  int *pTmp = db->
33aa4 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20  pSavepoint;.    
33aa5 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d  db->pSavepoint =
33aa6 20 70 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20   pTmp->pNext;.  
33aa7 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
33aa8 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20 7d 0a 20  db, pTmp);.  }. 
33aa9 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20   db->nSavepoint 
33aaa 3d 20 30 3b 0a 20 20 64 62 2d 3e 6e 53 74 61 74  = 0;.  db->nStat
33aab 65 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 64 62 2d  ement = 0;.  db-
33aac 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61  >isTransactionSa
33aad 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 7d 0a 0a  vepoint = 0;.}..
33aae 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 65  /*.** Close an e
33aaf 78 69 73 74 69 6e 67 20 53 51 4c 69 74 65 20 64  xisting SQLite d
33ab0 61 74 61 62 61 73 65 0a 2a 2f 0a 53 51 4c 49 54  atabase.*/.SQLIT
33ab1 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
33ab2 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 20  3_close(sqlite3 
33ab3 2a 64 62 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  *db){.  HashElem
33ab4 20 2a 69 3b 0a 20 20 69 6e 74 20 6a 3b 0a 0a 20   *i;.  int j;.. 
33ab5 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20   if( !db ){.    
33ab6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
33ab7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c  ;.  }.  if( !sql
33ab8 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
33ab9 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20  ickOrOk(db) ){. 
33aba 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
33abb 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73  _MISUSE;.  }.  s
33abc 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
33abd 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a  er(db->mutex);..
33abe 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
33abf 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
33ac0 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20   0);..  /* If a 
33ac1 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
33ac2 70 65 6e 2c 20 74 68 65 20 52 65 73 65 74 49 6e  pen, the ResetIn
33ac3 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 29 20 63  ternalSchema() c
33ac4 61 6c 6c 20 61 62 6f 76 65 0a 20 20 2a 2a 20 77  all above.  ** w
33ac5 69 6c 6c 20 6e 6f 74 20 68 61 76 65 20 63 61 6c  ill not have cal
33ac6 6c 65 64 20 74 68 65 20 78 44 69 73 63 6f 6e 6e  led the xDisconn
33ac7 65 63 74 28 29 20 6d 65 74 68 6f 64 20 6f 6e 20  ect() method on 
33ac8 61 6e 79 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a  any virtual.  **
33ac9 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64   tables in the d
33aca 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61 72 72  b->aVTrans[] arr
33acb 61 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ay. The followin
33acc 67 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c  g sqlite3VtabRol
33acd 6c 62 61 63 6b 28 29 0a 20 20 2a 2a 20 63 61 6c  lback().  ** cal
33ace 6c 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 20 57 65  l will do so. We
33acf 20 6e 65 65 64 20 74 6f 20 64 6f 20 74 68 69 73   need to do this
33ad0 20 62 65 66 6f 72 65 20 74 68 65 20 63 68 65 63   before the chec
33ad1 6b 20 66 6f 72 20 61 63 74 69 76 65 0a 20 20 2a  k for active.  *
33ad2 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  * SQL statements
33ad3 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 76   below, as the v
33ad4 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  -table implement
33ad5 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 73 74 6f  ation may be sto
33ad6 72 69 6e 67 0a 20 20 2a 2a 20 73 6f 6d 65 20 70  ring.  ** some p
33ad7 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
33ad8 74 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20  ts internally.. 
33ad9 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 74 61   */.  sqlite3Vta
33ada 62 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 0a  bRollback(db);..
33adb 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
33adc 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e  e any outstandin
33add 67 20 56 4d 73 2c 20 72 65 74 75 72 6e 20 53 51  g VMs, return SQ
33ade 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20  LITE_BUSY. */.  
33adf 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b  if( db->pVdbe ){
33ae0 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
33ae1 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53  r(db, SQLITE_BUS
33ae2 59 2c 20 0a 20 20 20 20 20 20 20 20 22 75 6e 61  Y, .        "una
33ae3 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64 75 65  ble to close due
33ae4 20 74 6f 20 75 6e 66 69 6e 61 6c 69 73 65 64 20   to unfinalised 
33ae5 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20  statements");.  
33ae6 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
33ae7 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
33ae8 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
33ae9 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
33aea 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
33aeb 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72  afetyCheckSickOr
33aec 4f 6b 28 64 62 29 20 29 3b 0a 0a 20 20 66 6f 72  Ok(db) );..  for
33aed 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b  (j=0; j<db->nDb;
33aee 20 6a 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65   j++){.    Btree
33aef 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
33af0 6a 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20  j].pBt;.    if( 
33af1 70 42 74 20 26 26 20 73 71 6c 69 74 65 33 42 74  pBt && sqlite3Bt
33af2 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 70 42  reeIsInBackup(pB
33af3 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  t) ){.      sqli
33af4 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
33af5 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20  ITE_BUSY, .     
33af6 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
33af7 63 6c 6f 73 65 20 64 75 65 20 74 6f 20 75 6e 66  close due to unf
33af8 69 6e 69 73 68 65 64 20 62 61 63 6b 75 70 20 6f  inished backup o
33af9 70 65 72 61 74 69 6f 6e 22 29 3b 0a 20 20 20 20  peration");.    
33afa 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
33afb 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
33afc 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
33afd 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
33afe 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65  }.  }..  /* Free
33aff 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67   any outstanding
33b00 20 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63   Savepoint struc
33b01 74 75 72 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  tures. */.  sqli
33b02 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e  te3CloseSavepoin
33b03 74 73 28 64 62 29 3b 0a 0a 20 20 66 6f 72 28 6a  ts(db);..  for(j
33b04 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a  =0; j<db->nDb; j
33b05 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  ++){.    struct 
33b06 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61  Db *pDb = &db->a
33b07 44 62 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20 70  Db[j];.    if( p
33b08 44 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20  Db->pBt ){.     
33b09 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
33b0a 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  se(pDb->pBt);.  
33b0b 20 20 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30      pDb->pBt = 0
33b0c 3b 0a 20 20 20 20 20 20 69 66 28 20 6a 21 3d 31  ;.      if( j!=1
33b0d 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 62 2d   ){.        pDb-
33b0e 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20  >pSchema = 0;.  
33b0f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
33b10 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
33b11 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
33b12 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 65 6c 6c 20   0);..  /* Tell 
33b13 74 68 65 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69  the code in noti
33b14 66 79 2e 63 20 74 68 61 74 20 74 68 65 20 63 6f  fy.c that the co
33b15 6e 6e 65 63 74 69 6f 6e 20 6e 6f 20 6c 6f 6e 67  nnection no long
33b16 65 72 20 68 6f 6c 64 73 20 61 6e 79 0a 20 20 2a  er holds any.  *
33b17 2a 20 6c 6f 63 6b 73 20 61 6e 64 20 64 6f 65 73  * locks and does
33b18 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 6e 79   not require any
33b19 20 66 75 72 74 68 65 72 20 75 6e 6c 6f 63 6b 2d   further unlock-
33b1a 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 73  notify callbacks
33b1b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
33b1c 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64  ConnectionClosed
33b1d 28 64 62 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  (db);..  assert(
33b1e 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b 0a 20   db->nDb<=2 );. 
33b1f 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
33b20 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20  ==db->aDbStatic 
33b21 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  );.  for(j=0; j<
33b22 41 72 72 61 79 53 69 7a 65 28 64 62 2d 3e 61 46  ArraySize(db->aF
33b23 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29 7b 0a 20 20  unc.a); j++){.  
33b24 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 78 74    FuncDef *pNext
33b25 2c 20 2a 70 48 61 73 68 2c 20 2a 70 3b 0a 20 20  , *pHash, *p;.  
33b26 20 20 66 6f 72 28 70 3d 64 62 2d 3e 61 46 75 6e    for(p=db->aFun
33b27 63 2e 61 5b 6a 5d 3b 20 70 3b 20 70 3d 70 48 61  c.a[j]; p; p=pHa
33b28 73 68 29 7b 0a 20 20 20 20 20 20 70 48 61 73 68  sh){.      pHash
33b29 20 3d 20 70 2d 3e 70 48 61 73 68 3b 0a 20 20 20   = p->pHash;.   
33b2a 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20     while( p ){. 
33b2b 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70         pNext = p
33b2c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
33b2d 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
33b2e 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 70  b, p);.        p
33b2f 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20 20 20   = pNext;.      
33b30 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f  }.    }.  }.  fo
33b31 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(i=sqliteHashFi
33b32 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  rst(&db->aCollSe
33b33 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65 48  q); i; i=sqliteH
33b34 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20  ashNext(i)){.   
33b35 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
33b36 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c  = (CollSeq *)sql
33b37 69 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a  iteHashData(i);.
33b38 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 61 6e      /* Invoke an
33b39 79 20 64 65 73 74 72 75 63 74 6f 72 73 20 72 65  y destructors re
33b3a 67 69 73 74 65 72 65 64 20 66 6f 72 20 63 6f 6c  gistered for col
33b3b 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
33b3c 75 73 65 72 20 64 61 74 61 2e 20 2a 2f 0a 20 20  user data. */.  
33b3d 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20    for(j=0; j<3; 
33b3e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
33b3f 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 20 29 7b  pColl[j].xDel ){
33b40 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 5b 6a  .        pColl[j
33b41 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b 6a 5d 2e  ].xDel(pColl[j].
33b42 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  pUser);.      }.
33b43 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
33b44 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c  3DbFree(db, pCol
33b45 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  l);.  }.  sqlite
33b46 33 48 61 73 68 43 6c 65 61 72 28 26 64 62 2d 3e  3HashClear(&db->
33b47 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66 6e 64  aCollSeq);.#ifnd
33b48 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
33b49 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 66 6f  IRTUALTABLE.  fo
33b4a 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(i=sqliteHashFi
33b4b 72 73 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  rst(&db->aModule
33b4c 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65 48 61  ); i; i=sqliteHa
33b4d 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20  shNext(i)){.    
33b4e 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28  Module *pMod = (
33b4f 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 48  Module *)sqliteH
33b50 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20  ashData(i);.    
33b51 69 66 28 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72  if( pMod->xDestr
33b52 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 4d 6f 64  oy ){.      pMod
33b53 2d 3e 78 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d  ->xDestroy(pMod-
33b54 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20  >pAux);.    }.  
33b55 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
33b56 64 62 2c 20 70 4d 6f 64 29 3b 0a 20 20 7d 0a 20  db, pMod);.  }. 
33b57 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
33b58 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b  r(&db->aModule);
33b59 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74  .#endif..  sqlit
33b5a 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
33b5b 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a 20 44 65  TE_OK, 0); /* De
33b5c 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 63 61  allocates any ca
33b5d 63 68 65 64 20 65 72 72 6f 72 20 73 74 72 69 6e  ched error strin
33b5e 67 73 2e 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  gs. */.  if( db-
33b5f 3e 70 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c  >pErr ){.    sql
33b60 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 64 62  ite3ValueFree(db
33b61 2d 3e 70 45 72 72 29 3b 0a 20 20 7d 0a 20 20 73  ->pErr);.  }.  s
33b62 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e  qlite3CloseExten
33b63 73 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 64 62  sions(db);..  db
33b64 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
33b65 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 0a 20  _MAGIC_ERROR;.. 
33b66 20 2f 2a 20 54 68 65 20 74 65 6d 70 2d 64 61 74   /* The temp-dat
33b67 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20  abase schema is 
33b68 61 6c 6c 6f 63 61 74 65 64 20 64 69 66 66 65 72  allocated differ
33b69 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f  ently from the o
33b6a 74 68 65 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a  ther schema.  **
33b6b 20 6f 62 6a 65 63 74 73 20 28 75 73 69 6e 67 20   objects (using 
33b6c 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 64  sqliteMalloc() d
33b6d 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65 61 64  irectly, instead
33b6e 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72 65 65   of sqlite3Btree
33b6f 53 63 68 65 6d 61 28 29 29 2e 0a 20 20 2a 2a 20  Schema())..  ** 
33b70 53 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62  So it needs to b
33b71 65 20 66 72 65 65 64 20 68 65 72 65 2e 20 54 6f  e freed here. To
33b72 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72 6f 6c 6c  do: Why not roll
33b73 20 74 68 65 20 74 65 6d 70 20 73 63 68 65 6d 61   the temp schema
33b74 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73   into.  ** the s
33b75 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  ame sqliteMalloc
33b76 28 29 20 61 73 20 74 68 65 20 6f 6e 65 20 74 68  () as the one th
33b77 61 74 20 61 6c 6c 6f 63 61 74 65 73 20 74 68 65  at allocates the
33b78 20 64 61 74 61 62 61 73 65 20 0a 20 20 2a 2a 20   database .  ** 
33b79 73 74 72 75 63 74 75 72 65 3f 0a 20 20 2a 2f 0a  structure?.  */.
33b7a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
33b7b 64 62 2c 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  db, db->aDb[1].p
33b7c 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74  Schema);.  sqlit
33b7d 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
33b7e 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d  b->mutex);.  db-
33b7f 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
33b80 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b 0a 20 20  MAGIC_CLOSED;.  
33b81 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72  sqlite3_mutex_fr
33b82 65 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ee(db->mutex);. 
33b83 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6c 6f 6f   assert( db->loo
33b84 6b 61 73 69 64 65 2e 6e 4f 75 74 3d 3d 30 20 29  kaside.nOut==0 )
33b85 3b 20 20 2f 2a 20 46 61 69 6c 73 20 6f 6e 20 61  ;  /* Fails on a
33b86 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72   lookaside memor
33b87 79 20 6c 65 61 6b 20 2a 2f 0a 20 20 69 66 28 20  y leak */.  if( 
33b88 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d  db->lookaside.bM
33b89 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20 20 73  alloced ){.    s
33b8a 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62 2d 3e  qlite3_free(db->
33b8b 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74  lookaside.pStart
33b8c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
33b8d 5f 66 72 65 65 28 64 62 29 3b 0a 20 20 72 65 74  _free(db);.  ret
33b8e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
33b8f 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b  ../*.** Rollback
33b90 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69   all database fi
33b91 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  les..*/.SQLITE_P
33b92 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
33b93 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73  te3RollbackAll(s
33b94 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
33b95 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 6e 54 72  nt i;.  int inTr
33b96 61 6e 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  ans = 0;.  asser
33b97 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
33b98 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
33b99 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67   );.  sqlite3Beg
33b9a 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
33b9b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64  ;.  for(i=0; i<d
33b9c 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
33b9d 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d    if( db->aDb[i]
33b9e 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 69 66  .pBt ){.      if
33b9f 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  ( sqlite3BtreeIs
33ba0 49 6e 54 72 61 6e 73 28 64 62 2d 3e 61 44 62 5b  InTrans(db->aDb[
33ba1 69 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20 20 20  i].pBt) ){.     
33ba2 20 20 20 69 6e 54 72 61 6e 73 20 3d 20 31 3b 0a     inTrans = 1;.
33ba3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
33ba4 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
33ba5 63 6b 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  ck(db->aDb[i].pB
33ba6 74 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44  t);.      db->aD
33ba7 62 5b 69 5d 2e 69 6e 54 72 61 6e 73 20 3d 20 30  b[i].inTrans = 0
33ba8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
33ba9 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
33baa 6b 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  k(db);.  sqlite3
33bab 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
33bac 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c  );..  if( db->fl
33bad 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72  ags&SQLITE_Inter
33bae 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20  nChanges ){.    
33baf 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
33bb0 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
33bb1 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  db);.    sqlite3
33bb2 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
33bb3 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a  ema(db, 0);.  }.
33bb4 0a 20 20 2f 2a 20 41 6e 79 20 64 65 66 65 72 72  .  /* Any deferr
33bb5 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69  ed constraint vi
33bb6 6f 6c 61 74 69 6f 6e 73 20 68 61 76 65 20 6e 6f  olations have no
33bb7 77 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e  w been resolved.
33bb8 20 2a 2f 0a 20 20 64 62 2d 3e 6e 44 65 66 65 72   */.  db->nDefer
33bb9 72 65 64 43 6f 6e 73 20 3d 20 30 3b 0a 0a 20 20  redCons = 0;..  
33bba 2f 2a 20 49 66 20 6f 6e 65 20 68 61 73 20 62 65  /* If one has be
33bbb 65 6e 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 69  en configured, i
33bbc 6e 76 6f 6b 65 20 74 68 65 20 72 6f 6c 6c 62 61  nvoke the rollba
33bbd 63 6b 2d 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b  ck-hook callback
33bbe 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 52   */.  if( db->xR
33bbf 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 20  ollbackCallback 
33bc0 26 26 20 28 69 6e 54 72 61 6e 73 20 7c 7c 20 21  && (inTrans || !
33bc1 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 29 20  db->autoCommit) 
33bc2 29 7b 0a 20 20 20 20 64 62 2d 3e 78 52 6f 6c 6c  ){.    db->xRoll
33bc3 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 64 62 2d  backCallback(db-
33bc4 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 29 3b 0a  >pRollbackArg);.
33bc5 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
33bc6 75 72 6e 20 61 20 73 74 61 74 69 63 20 73 74 72  urn a static str
33bc7 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62  ing that describ
33bc8 65 73 20 74 68 65 20 6b 69 6e 64 20 6f 66 20 65  es the kind of e
33bc9 72 72 6f 72 20 73 70 65 63 69 66 69 65 64 20 69  rror specified i
33bca 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  n the.** argumen
33bcb 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
33bcc 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
33bcd 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 69  *sqlite3ErrStr(i
33bce 6e 74 20 72 63 29 7b 0a 20 20 73 74 61 74 69 63  nt rc){.  static
33bcf 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 63 6f 6e   const char* con
33bd0 73 74 20 61 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20  st aMsg[] = {.  
33bd1 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 20    /* SQLITE_OK  
33bd2 20 20 20 20 20 20 20 20 2a 2f 20 22 6e 6f 74 20          */ "not 
33bd3 61 6e 20 65 72 72 6f 72 22 2c 0a 20 20 20 20 2f  an error",.    /
33bd4 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20  * SQLITE_ERROR  
33bd5 20 20 20 20 20 2a 2f 20 22 53 51 4c 20 6c 6f 67       */ "SQL log
33bd6 69 63 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73  ic error or miss
33bd7 69 6e 67 20 64 61 74 61 62 61 73 65 22 2c 0a 20  ing database",. 
33bd8 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 49 4e 54     /* SQLITE_INT
33bd9 45 52 4e 41 4c 20 20 20 20 2a 2f 20 30 2c 0a 20  ERNAL    */ 0,. 
33bda 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 50 45 52     /* SQLITE_PER
33bdb 4d 20 20 20 20 20 20 20 20 2a 2f 20 22 61 63 63  M        */ "acc
33bdc 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 64  ess permission d
33bdd 65 6e 69 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  enied",.    /* S
33bde 51 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20  QLITE_ABORT     
33bdf 20 20 2a 2f 20 22 63 61 6c 6c 62 61 63 6b 20 72    */ "callback r
33be0 65 71 75 65 73 74 65 64 20 71 75 65 72 79 20 61  equested query a
33be1 62 6f 72 74 22 2c 0a 20 20 20 20 2f 2a 20 53 51  bort",.    /* SQ
33be2 4c 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20  LITE_BUSY       
33be3 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 69 73   */ "database is
33be4 20 6c 6f 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a   locked",.    /*
33be5 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20   SQLITE_LOCKED  
33be6 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65      */ "database
33be7 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64   table is locked
33be8 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
33be9 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 2a 2f 20  _NOMEM       */ 
33bea 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c  "out of memory",
33beb 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52  .    /* SQLITE_R
33bec 45 41 44 4f 4e 4c 59 20 20 20 20 2a 2f 20 22 61  EADONLY    */ "a
33bed 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
33bee 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62  a readonly datab
33bef 61 73 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ase",.    /* SQL
33bf0 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 20 20  ITE_INTERRUPT   
33bf1 2a 2f 20 22 69 6e 74 65 72 72 75 70 74 65 64 22  */ "interrupted"
33bf2 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33bf3 49 4f 45 52 52 20 20 20 20 20 20 20 2a 2f 20 22  IOERR       */ "
33bf4 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22 2c  disk I/O error",
33bf5 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43  .    /* SQLITE_C
33bf6 4f 52 52 55 50 54 20 20 20 20 20 2a 2f 20 22 64  ORRUPT     */ "d
33bf7 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61  atabase disk ima
33bf8 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 22  ge is malformed"
33bf9 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33bfa 4e 4f 54 46 4f 55 4e 44 20 20 20 20 2a 2f 20 30  NOTFOUND    */ 0
33bfb 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33bfc 46 55 4c 4c 20 20 20 20 20 20 20 20 2a 2f 20 22  FULL        */ "
33bfd 64 61 74 61 62 61 73 65 20 6f 72 20 64 69 73 6b  database or disk
33bfe 20 69 73 20 66 75 6c 6c 22 2c 0a 20 20 20 20 2f   is full",.    /
33bff 2a 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  * SQLITE_CANTOPE
33c00 4e 20 20 20 20 2a 2f 20 22 75 6e 61 62 6c 65 20  N    */ "unable 
33c01 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  to open database
33c02 20 66 69 6c 65 22 2c 0a 20 20 20 20 2f 2a 20 53   file",.    /* S
33c03 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20  QLITE_PROTOCOL  
33c04 20 20 2a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53    */ 0,.    /* S
33c05 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20  QLITE_EMPTY     
33c06 20 20 2a 2f 20 22 74 61 62 6c 65 20 63 6f 6e 74    */ "table cont
33c07 61 69 6e 73 20 6e 6f 20 64 61 74 61 22 2c 0a 20  ains no data",. 
33c08 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 43 48     /* SQLITE_SCH
33c09 45 4d 41 20 20 20 20 20 20 2a 2f 20 22 64 61 74  EMA      */ "dat
33c0a 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73  abase schema has
33c0b 20 63 68 61 6e 67 65 64 22 2c 0a 20 20 20 20 2f   changed",.    /
33c0c 2a 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20  * SQLITE_TOOBIG 
33c0d 20 20 20 20 20 2a 2f 20 22 73 74 72 69 6e 67 20       */ "string 
33c0e 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22  or blob too big"
33c0f 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33c10 43 4f 4e 53 54 52 41 49 4e 54 20 20 2a 2f 20 22  CONSTRAINT  */ "
33c11 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
33c12 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  d",.    /* SQLIT
33c13 45 5f 4d 49 53 4d 41 54 43 48 20 20 20 20 2a 2f  E_MISMATCH    */
33c14 20 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d 61   "datatype misma
33c15 74 63 68 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  tch",.    /* SQL
33c16 49 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20  ITE_MISUSE      
33c17 2a 2f 20 22 6c 69 62 72 61 72 79 20 72 6f 75 74  */ "library rout
33c18 69 6e 65 20 63 61 6c 6c 65 64 20 6f 75 74 20 6f  ine called out o
33c19 66 20 73 65 71 75 65 6e 63 65 22 2c 0a 20 20 20  f sequence",.   
33c1a 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53   /* SQLITE_NOLFS
33c1b 20 20 20 20 20 20 20 2a 2f 20 22 6c 61 72 67 65         */ "large
33c1c 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 69 73   file support is
33c1d 20 64 69 73 61 62 6c 65 64 22 2c 0a 20 20 20 20   disabled",.    
33c1e 2f 2a 20 53 51 4c 49 54 45 5f 41 55 54 48 20 20  /* SQLITE_AUTH  
33c1f 20 20 20 20 20 20 2a 2f 20 22 61 75 74 68 6f 72        */ "author
33c20 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 22 2c  ization denied",
33c21 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46  .    /* SQLITE_F
33c22 4f 52 4d 41 54 20 20 20 20 20 20 2a 2f 20 22 61  ORMAT      */ "a
33c23 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
33c24 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 22 2c  e format error",
33c25 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52  .    /* SQLITE_R
33c26 41 4e 47 45 20 20 20 20 20 20 20 2a 2f 20 22 62  ANGE       */ "b
33c27 69 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20 69 6e  ind or column in
33c28 64 65 78 20 6f 75 74 20 6f 66 20 72 61 6e 67 65  dex out of range
33c29 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
33c2a 5f 4e 4f 54 41 44 42 20 20 20 20 20 20 2a 2f 20  _NOTADB      */ 
33c2b 22 66 69 6c 65 20 69 73 20 65 6e 63 72 79 70 74  "file is encrypt
33c2c 65 64 20 6f 72 20 69 73 20 6e 6f 74 20 61 20 64  ed or is not a d
33c2d 61 74 61 62 61 73 65 22 2c 0a 20 20 7d 3b 0a 20  atabase",.  };. 
33c2e 20 72 63 20 26 3d 20 30 78 66 66 3b 0a 20 20 69   rc &= 0xff;.  i
33c2f 66 28 20 41 4c 57 41 59 53 28 72 63 3e 3d 30 29  f( ALWAYS(rc>=0)
33c30 20 26 26 20 72 63 3c 28 69 6e 74 29 28 73 69 7a   && rc<(int)(siz
33c31 65 6f 66 28 61 4d 73 67 29 2f 73 69 7a 65 6f 66  eof(aMsg)/sizeof
33c32 28 61 4d 73 67 5b 30 5d 29 29 20 26 26 20 61 4d  (aMsg[0])) && aM
33c33 73 67 5b 72 63 5d 21 3d 30 20 29 7b 0a 20 20 20  sg[rc]!=0 ){.   
33c34 20 72 65 74 75 72 6e 20 61 4d 73 67 5b 72 63 5d   return aMsg[rc]
33c35 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
33c36 65 74 75 72 6e 20 22 75 6e 6b 6e 6f 77 6e 20 65  eturn "unknown e
33c37 72 72 6f 72 22 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rror";.  }.}../*
33c38 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
33c39 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 62 75   implements a bu
33c3a 73 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74  sy callback that
33c3b 20 73 6c 65 65 70 73 20 61 6e 64 20 74 72 69 65   sleeps and trie
33c3c 73 0a 2a 2a 20 61 67 61 69 6e 20 75 6e 74 69 6c  s.** again until
33c3d 20 61 20 74 69 6d 65 6f 75 74 20 76 61 6c 75 65   a timeout value
33c3e 20 69 73 20 72 65 61 63 68 65 64 2e 20 20 54 68   is reached.  Th
33c3f 65 20 74 69 6d 65 6f 75 74 20 76 61 6c 75 65 20  e timeout value 
33c40 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72  is.** an integer
33c41 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69   number of milli
33c42 73 65 63 6f 6e 64 73 20 70 61 73 73 65 64 20 69  seconds passed i
33c43 6e 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a  n as the first.*
33c44 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  * argument..*/.s
33c45 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
33c46 44 65 66 61 75 6c 74 42 75 73 79 43 61 6c 6c 62  DefaultBusyCallb
33c47 61 63 6b 28 0a 20 76 6f 69 64 20 2a 70 74 72 2c  ack(. void *ptr,
33c48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33c49 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
33c4a 63 74 69 6f 6e 20 2a 2f 0a 20 69 6e 74 20 63 6f  ction */. int co
33c4b 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  unt             
33c4c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
33c4d 74 69 6d 65 73 20 74 61 62 6c 65 20 68 61 73 20  times table has 
33c4e 62 65 65 6e 20 62 75 73 79 20 2a 2f 0a 29 7b 0a  been busy */.){.
33c4f 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
33c50 4e 20 7c 7c 20 28 64 65 66 69 6e 65 64 28 48 41  N || (defined(HA
33c51 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41  VE_USLEEP) && HA
33c52 56 45 5f 55 53 4c 45 45 50 29 0a 20 20 73 74 61  VE_USLEEP).  sta
33c53 74 69 63 20 63 6f 6e 73 74 20 75 38 20 64 65 6c  tic const u8 del
33c54 61 79 73 5b 5d 20 3d 0a 20 20 20 20 20 7b 20 31  ays[] =.     { 1
33c55 2c 20 32 2c 20 35 2c 20 31 30 2c 20 31 35 2c 20  , 2, 5, 10, 15, 
33c56 32 30 2c 20 32 35 2c 20 32 35 2c 20 20 32 35 2c  20, 25, 25,  25,
33c57 20 20 35 30 2c 20 20 35 30 2c 20 31 30 30 20 7d    50,  50, 100 }
33c58 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
33c59 20 75 38 20 74 6f 74 61 6c 73 5b 5d 20 3d 0a 20   u8 totals[] =. 
33c5a 20 20 20 20 7b 20 30 2c 20 31 2c 20 33 2c 20 20      { 0, 1, 3,  
33c5b 38 2c 20 31 38 2c 20 33 33 2c 20 35 33 2c 20 37  8, 18, 33, 53, 7
33c5c 38 2c 20 31 30 33 2c 20 31 32 38 2c 20 31 37 38  8, 103, 128, 178
33c5d 2c 20 32 32 38 20 7d 3b 0a 23 20 64 65 66 69 6e  , 228 };.# defin
33c5e 65 20 4e 44 45 4c 41 59 20 28 73 69 7a 65 6f 66  e NDELAY (sizeof
33c5f 28 64 65 6c 61 79 73 29 2f 73 69 7a 65 6f 66 28  (delays)/sizeof(
33c60 64 65 6c 61 79 73 5b 30 5d 29 29 0a 20 20 73 71  delays[0])).  sq
33c61 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c  lite3 *db = (sql
33c62 69 74 65 33 20 2a 29 70 74 72 3b 0a 20 20 69 6e  ite3 *)ptr;.  in
33c63 74 20 74 69 6d 65 6f 75 74 20 3d 20 64 62 2d 3e  t timeout = db->
33c64 62 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20 20 69  busyTimeout;.  i
33c65 6e 74 20 64 65 6c 61 79 2c 20 70 72 69 6f 72 3b  nt delay, prior;
33c66 0a 0a 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e  ..  assert( coun
33c67 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 63 6f  t>=0 );.  if( co
33c68 75 6e 74 20 3c 20 4e 44 45 4c 41 59 20 29 7b 0a  unt < NDELAY ){.
33c69 20 20 20 20 64 65 6c 61 79 20 3d 20 64 65 6c 61      delay = dela
33c6a 79 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 20 20 70  ys[count];.    p
33c6b 72 69 6f 72 20 3d 20 74 6f 74 61 6c 73 5b 63 6f  rior = totals[co
33c6c 75 6e 74 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  unt];.  }else{. 
33c6d 20 20 20 64 65 6c 61 79 20 3d 20 64 65 6c 61 79     delay = delay
33c6e 73 5b 4e 44 45 4c 41 59 2d 31 5d 3b 0a 20 20 20  s[NDELAY-1];.   
33c6f 20 70 72 69 6f 72 20 3d 20 74 6f 74 61 6c 73 5b   prior = totals[
33c70 4e 44 45 4c 41 59 2d 31 5d 20 2b 20 64 65 6c 61  NDELAY-1] + dela
33c71 79 2a 28 63 6f 75 6e 74 2d 28 4e 44 45 4c 41 59  y*(count-(NDELAY
33c72 2d 31 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  -1));.  }.  if( 
33c73 70 72 69 6f 72 20 2b 20 64 65 6c 61 79 20 3e 20  prior + delay > 
33c74 74 69 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 64  timeout ){.    d
33c75 65 6c 61 79 20 3d 20 74 69 6d 65 6f 75 74 20 2d  elay = timeout -
33c76 20 70 72 69 6f 72 3b 0a 20 20 20 20 69 66 28 20   prior;.    if( 
33c77 64 65 6c 61 79 3c 3d 30 20 29 20 72 65 74 75 72  delay<=0 ) retur
33c78 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  n 0;.  }.  sqlit
33c79 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70 56  e3OsSleep(db->pV
33c7a 66 73 2c 20 64 65 6c 61 79 2a 31 30 30 30 29 3b  fs, delay*1000);
33c7b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6c  .  return 1;.#el
33c7c 73 65 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  se.  sqlite3 *db
33c7d 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74   = (sqlite3 *)pt
33c7e 72 3b 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74  r;.  int timeout
33c7f 20 3d 20 28 28 73 71 6c 69 74 65 33 20 2a 29 70   = ((sqlite3 *)p
33c80 74 72 29 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74  tr)->busyTimeout
33c81 3b 0a 20 20 69 66 28 20 28 63 6f 75 6e 74 2b 31  ;.  if( (count+1
33c82 29 2a 31 30 30 30 20 3e 20 74 69 6d 65 6f 75 74  )*1000 > timeout
33c83 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
33c84 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f  ;.  }.  sqlite3O
33c85 73 53 6c 65 65 70 28 64 62 2d 3e 70 56 66 73 2c  sSleep(db->pVfs,
33c86 20 31 30 30 30 30 30 30 29 3b 0a 20 20 72 65 74   1000000);.  ret
33c87 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a  urn 1;.#endif.}.
33c88 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
33c89 65 20 67 69 76 65 6e 20 62 75 73 79 20 68 61 6e  e given busy han
33c8a 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  dler..**.** This
33c8b 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
33c8c 65 64 20 77 68 65 6e 20 61 6e 20 6f 70 65 72 61  ed when an opera
33c8d 74 69 6f 6e 20 66 61 69 6c 65 64 20 77 69 74 68  tion failed with
33c8e 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20 74   a lock..** If t
33c8f 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
33c90 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
33c91 65 20 6c 6f 63 6b 20 69 73 20 72 65 74 72 69 65  e lock is retrie
33c92 64 2e 20 20 49 66 20 69 74 0a 2a 2a 20 72 65 74  d.  If it.** ret
33c93 75 72 6e 73 20 30 2c 20 74 68 65 20 6f 70 65 72  urns 0, the oper
33c94 61 74 69 6f 6e 20 61 62 6f 72 74 73 20 77 69 74  ation aborts wit
33c95 68 20 61 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  h an SQLITE_BUSY
33c96 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54   error..*/.SQLIT
33c97 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
33c98 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48  lite3InvokeBusyH
33c99 61 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c  andler(BusyHandl
33c9a 65 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  er *p){.  int rc
33c9b 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d  ;.  if( NEVER(p=
33c9c 3d 30 29 20 7c 7c 20 70 2d 3e 78 46 75 6e 63 3d  =0) || p->xFunc=
33c9d 3d 30 20 7c 7c 20 70 2d 3e 6e 42 75 73 79 3c 30  =0 || p->nBusy<0
33c9e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72   ) return 0;.  r
33c9f 63 20 3d 20 70 2d 3e 78 46 75 6e 63 28 70 2d 3e  c = p->xFunc(p->
33ca0 70 41 72 67 2c 20 70 2d 3e 6e 42 75 73 79 29 3b  pArg, p->nBusy);
33ca1 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a  .  if( rc==0 ){.
33ca2 20 20 20 20 70 2d 3e 6e 42 75 73 79 20 3d 20 2d      p->nBusy = -
33ca3 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
33ca4 70 2d 3e 6e 42 75 73 79 2b 2b 3b 0a 20 20 7d 0a  p->nBusy++;.  }.
33ca5 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a    return rc; .}.
33ca6 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
33ca7 69 6e 65 20 73 65 74 73 20 74 68 65 20 62 75 73  ine sets the bus
33ca8 79 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  y callback for a
33ca9 6e 20 53 71 6c 69 74 65 20 64 61 74 61 62 61 73  n Sqlite databas
33caa 65 20 74 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65  e to the.** give
33cab 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  n callback funct
33cac 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76  ion with the giv
33cad 65 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  en argument..*/.
33cae 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33caf 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
33cb0 6c 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ler(.  sqlite3 *
33cb1 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 42 75 73  db,.  int (*xBus
33cb2 79 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 0a 20  y)(void*,int),. 
33cb3 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20   void *pArg.){. 
33cb4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
33cb5 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
33cb6 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  .  db->busyHandl
33cb7 65 72 2e 78 46 75 6e 63 20 3d 20 78 42 75 73 79  er.xFunc = xBusy
33cb8 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64  ;.  db->busyHand
33cb9 6c 65 72 2e 70 41 72 67 20 3d 20 70 41 72 67 3b  ler.pArg = pArg;
33cba 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  .  db->busyHandl
33cbb 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20  er.nBusy = 0;.  
33cbc 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
33cbd 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
33cbe 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
33cbf 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  OK;.}..#ifndef S
33cc0 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
33cc1 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 2f 2a 0a  ESS_CALLBACK./*.
33cc2 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
33cc3 73 65 74 73 20 74 68 65 20 70 72 6f 67 72 65 73  sets the progres
33cc4 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  s callback for a
33cc5 6e 20 53 71 6c 69 74 65 20 64 61 74 61 62 61 73  n Sqlite databas
33cc6 65 20 74 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65  e to the.** give
33cc7 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  n callback funct
33cc8 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76  ion with the giv
33cc9 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65  en argument. The
33cca 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
33ccb 63 6b 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 69 6e  ck will.** be in
33ccc 76 6f 6b 65 64 20 65 76 65 72 79 20 6e 4f 70 73  voked every nOps
33ccd 20 6f 70 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c   opcodes..*/.SQL
33cce 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
33ccf 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61  ite3_progress_ha
33cd0 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69 74 65 33  ndler(.  sqlite3
33cd1 20 2a 64 62 2c 20 0a 20 20 69 6e 74 20 6e 4f 70   *db, .  int nOp
33cd2 73 2c 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67  s,.  int (*xProg
33cd3 72 65 73 73 29 28 76 6f 69 64 2a 29 2c 20 0a 20  ress)(void*), . 
33cd4 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20   void *pArg.){. 
33cd5 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
33cd6 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
33cd7 0a 20 20 69 66 28 20 6e 4f 70 73 3e 30 20 29 7b  .  if( nOps>0 ){
33cd8 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 67 72 65  .    db->xProgre
33cd9 73 73 20 3d 20 78 50 72 6f 67 72 65 73 73 3b 0a  ss = xProgress;.
33cda 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73      db->nProgres
33cdb 73 4f 70 73 20 3d 20 6e 4f 70 73 3b 0a 20 20 20  sOps = nOps;.   
33cdc 20 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72   db->pProgressAr
33cdd 67 20 3d 20 70 41 72 67 3b 0a 20 20 7d 65 6c 73  g = pArg;.  }els
33cde 65 7b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 67  e{.    db->xProg
33cdf 72 65 73 73 20 3d 20 30 3b 0a 20 20 20 20 64 62  ress = 0;.    db
33ce0 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d  ->nProgressOps =
33ce1 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f   0;.    db->pPro
33ce2 67 72 65 73 73 41 72 67 20 3d 20 30 3b 0a 20 20  gressArg = 0;.  
33ce3 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
33ce4 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
33ce5 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f  x);.}.#endif.../
33ce6 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
33ce7 65 20 69 6e 73 74 61 6c 6c 73 20 61 20 64 65 66  e installs a def
33ce8 61 75 6c 74 20 62 75 73 79 20 68 61 6e 64 6c 65  ault busy handle
33ce9 72 20 74 68 61 74 20 77 61 69 74 73 20 66 6f 72  r that waits for
33cea 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 65   the.** specifie
33ceb 64 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c  d number of mill
33cec 69 73 65 63 6f 6e 64 73 20 62 65 66 6f 72 65 20  iseconds before 
33ced 72 65 74 75 72 6e 69 6e 67 20 30 2e 0a 2a 2f 0a  returning 0..*/.
33cee 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33cef 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
33cf0 6f 75 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  out(sqlite3 *db,
33cf1 20 69 6e 74 20 6d 73 29 7b 0a 20 20 69 66 28 20   int ms){.  if( 
33cf2 6d 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  ms>0 ){.    db->
33cf3 62 75 73 79 54 69 6d 65 6f 75 74 20 3d 20 6d 73  busyTimeout = ms
33cf4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 75  ;.    sqlite3_bu
33cf5 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20 73  sy_handler(db, s
33cf6 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79  qliteDefaultBusy
33cf7 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64 2a  Callback, (void*
33cf8 29 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )db);.  }else{. 
33cf9 20 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f     sqlite3_busy_
33cfa 68 61 6e 64 6c 65 72 28 64 62 2c 20 30 2c 20 30  handler(db, 0, 0
33cfb 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
33cfc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
33cfd 0a 2a 2a 20 43 61 75 73 65 20 61 6e 79 20 70 65  .** Cause any pe
33cfe 6e 64 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20  nding operation 
33cff 74 6f 20 73 74 6f 70 20 61 74 20 69 74 73 20 65  to stop at its e
33d00 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e  arliest opportun
33d01 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ity..*/.SQLITE_A
33d02 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
33d03 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65  interrupt(sqlite
33d04 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 75 31  3 *db){.  db->u1
33d05 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d  .isInterrupted =
33d06 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68   1;.}.../*.** Th
33d07 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65  is function is e
33d08 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20  xactly the same 
33d09 61 73 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  as sqlite3_creat
33d0a 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 65 78  e_function(), ex
33d0b 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 74 20  cept.** that it 
33d0c 69 73 20 64 65 73 69 67 6e 65 64 20 74 6f 20 62  is designed to b
33d0d 65 20 63 61 6c 6c 65 64 20 62 79 20 69 6e 74 65  e called by inte
33d0e 72 6e 61 6c 20 63 6f 64 65 2e 20 54 68 65 20 64  rnal code. The d
33d0f 69 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20  ifference is.** 
33d10 74 68 61 74 20 69 66 20 61 20 6d 61 6c 6c 6f 63  that if a malloc
33d11 28 29 20 66 61 69 6c 73 20 69 6e 20 73 71 6c 69  () fails in sqli
33d12 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
33d13 69 6f 6e 28 29 2c 20 61 6e 20 65 72 72 6f 72 20  ion(), an error 
33d14 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72  code.** is retur
33d15 6e 65 64 20 61 6e 64 20 74 68 65 20 6d 61 6c 6c  ned and the mall
33d16 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 63 6c  ocFailed flag cl
33d17 65 61 72 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54  eared. .*/.SQLIT
33d18 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
33d19 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
33d1a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
33d1b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
33d1c 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69  unctionName,.  i
33d1d 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65  nt nArg,.  int e
33d1e 6e 63 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65  nc,.  void *pUse
33d1f 72 44 61 74 61 2c 0a 20 20 76 6f 69 64 20 28 2a  rData,.  void (*
33d20 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
33d21 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
33d22 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20  te3_value **),. 
33d23 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
33d24 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
33d25 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
33d26 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  e **),.  void (*
33d27 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f  xFinal)(sqlite3_
33d28 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 46  context*).){.  F
33d29 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 69 6e 74  uncDef *p;.  int
33d2a 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72   nName;..  asser
33d2b 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
33d2c 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
33d2d 20 29 3b 0a 20 20 69 66 28 20 7a 46 75 6e 63 74   );.  if( zFunct
33d2e 69 6f 6e 4e 61 6d 65 3d 3d 30 20 7c 7c 0a 20 20  ionName==0 ||.  
33d2f 20 20 20 20 28 78 46 75 6e 63 20 26 26 20 28 78      (xFunc && (x
33d30 46 69 6e 61 6c 20 7c 7c 20 78 53 74 65 70 29 29  Final || xStep))
33d31 20 7c 7c 20 0a 20 20 20 20 20 20 28 21 78 46 75   || .      (!xFu
33d32 6e 63 20 26 26 20 28 78 46 69 6e 61 6c 20 26 26  nc && (xFinal &&
33d33 20 21 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20   !xStep)) ||.   
33d34 20 20 20 28 21 78 46 75 6e 63 20 26 26 20 28 21     (!xFunc && (!
33d35 78 46 69 6e 61 6c 20 26 26 20 78 53 74 65 70 29  xFinal && xStep)
33d36 29 20 7c 7c 0a 20 20 20 20 20 20 28 6e 41 72 67  ) ||.      (nArg
33d37 3c 2d 31 20 7c 7c 20 6e 41 72 67 3e 53 51 4c 49  <-1 || nArg>SQLI
33d38 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
33d39 41 52 47 29 20 7c 7c 0a 20 20 20 20 20 20 28 32  ARG) ||.      (2
33d3a 35 35 3c 28 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  55<(nName = sqli
33d3b 74 65 33 53 74 72 6c 65 6e 33 30 28 20 7a 46 75  te3Strlen30( zFu
33d3c 6e 63 74 69 6f 6e 4e 61 6d 65 29 29 29 20 29 7b  nctionName))) ){
33d3d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
33d3e 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
33d3f 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45   .#ifndef SQLITE
33d40 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a  _OMIT_UTF16.  /*
33d41 20 49 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36   If SQLITE_UTF16
33d42 20 69 73 20 73 70 65 63 69 66 69 65 64 20 61 73   is specified as
33d43 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 74 79   the encoding ty
33d44 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20 74 68  pe, transform th
33d45 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f  is.  ** to one o
33d46 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  f SQLITE_UTF16LE
33d47 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36   or SQLITE_UTF16
33d48 42 45 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a  BE using the.  *
33d49 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  * SQLITE_UTF16NA
33d4a 54 49 56 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49  TIVE macro. SQLI
33d4b 54 45 5f 55 54 46 31 36 20 69 73 20 6e 6f 74 20  TE_UTF16 is not 
33d4c 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e  used internally.
33d4d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 53 51  .  **.  ** If SQ
33d4e 4c 49 54 45 5f 41 4e 59 20 69 73 20 73 70 65 63  LITE_ANY is spec
33d4f 69 66 69 65 64 2c 20 61 64 64 20 74 68 72 65 65  ified, add three
33d50 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
33d51 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 74   function.  ** t
33d52 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
33d53 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 6e 63  ..  */.  if( enc
33d54 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 29  ==SQLITE_UTF16 )
33d55 7b 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c 49  {.    enc = SQLI
33d56 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a  TE_UTF16NATIVE;.
33d57 20 20 7d 65 6c 73 65 20 69 66 28 20 65 6e 63 3d    }else if( enc=
33d58 3d 53 51 4c 49 54 45 5f 41 4e 59 20 29 7b 0a 20  =SQLITE_ANY ){. 
33d59 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72     int rc;.    r
33d5a 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  c = sqlite3Creat
33d5b 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74  eFunc(db, zFunct
33d5c 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53  ionName, nArg, S
33d5d 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20 20  QLITE_UTF8,.    
33d5e 20 20 20 20 20 70 55 73 65 72 44 61 74 61 2c 20       pUserData, 
33d5f 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46  xFunc, xStep, xF
33d60 69 6e 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  inal);.    if( r
33d61 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
33d62 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
33d63 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
33d64 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20   zFunctionName, 
33d65 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
33d66 31 36 4c 45 2c 0a 20 20 20 20 20 20 20 20 20 20  16LE,.          
33d67 70 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e 63  pUserData, xFunc
33d68 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
33d69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
33d6a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
33d6b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
33d6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6e 63 20  ;.    }.    enc 
33d6d 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  = SQLITE_UTF16BE
33d6e 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 65 6e  ;.  }.#else.  en
33d6f 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
33d70 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 2f 2a 20  .#endif.  .  /* 
33d71 43 68 65 63 6b 20 69 66 20 61 6e 20 65 78 69 73  Check if an exis
33d72 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73  ting function is
33d73 20 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64 65   being overridde
33d74 6e 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 49 66  n or deleted. If
33d75 20 73 6f 2c 0a 20 20 2a 2a 20 61 6e 64 20 74 68   so,.  ** and th
33d76 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20 56  ere are active V
33d77 4d 73 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  Ms, then return 
33d78 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 49 66 20  SQLITE_BUSY. If 
33d79 61 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20  a function.  ** 
33d7a 69 73 20 62 65 69 6e 67 20 6f 76 65 72 72 69 64  is being overrid
33d7b 64 65 6e 2f 64 65 6c 65 74 65 64 20 62 75 74 20  den/deleted but 
33d7c 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 63 74  there are no act
33d7d 69 76 65 20 56 4d 73 2c 20 61 6c 6c 6f 77 20 74  ive VMs, allow t
33d7e 68 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f  he.  ** operatio
33d7f 6e 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 62 75  n to continue bu
33d80 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  t invalidate all
33d81 20 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61   precompiled sta
33d82 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20  tements..  */.  
33d83 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46  p = sqlite3FindF
33d84 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e  unction(db, zFun
33d85 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65  ctionName, nName
33d86 2c 20 6e 41 72 67 2c 20 28 75 38 29 65 6e 63 2c  , nArg, (u8)enc,
33d87 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20   0);.  if( p && 
33d88 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 65 6e 63  p->iPrefEnc==enc
33d89 20 26 26 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72   && p->nArg==nAr
33d8a 67 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d  g ){.    if( db-
33d8b 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29  >activeVdbeCnt )
33d8c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
33d8d 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
33d8e 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22  BUSY, .        "
33d8f 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65  unable to delete
33d90 2f 6d 6f 64 69 66 79 20 75 73 65 72 2d 66 75 6e  /modify user-fun
33d91 63 74 69 6f 6e 20 64 75 65 20 74 6f 20 61 63 74  ction due to act
33d92 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 22 29  ive statements")
33d93 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
33d94 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
33d95 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  d );.      retur
33d96 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
33d97 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
33d98 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
33d99 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
33d9a 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  db);.    }.  }..
33d9b 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    p = sqlite3Fin
33d9c 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46  dFunction(db, zF
33d9d 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61  unctionName, nNa
33d9e 6d 65 2c 20 6e 41 72 67 2c 20 28 75 38 29 65 6e  me, nArg, (u8)en
33d9f 63 2c 20 31 29 3b 0a 20 20 61 73 73 65 72 74 28  c, 1);.  assert(
33da0 70 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  p || db->mallocF
33da1 61 69 6c 65 64 29 3b 0a 20 20 69 66 28 20 21 70  ailed);.  if( !p
33da2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
33da3 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
33da4 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 30 3b  .  p->flags = 0;
33da5 0a 20 20 70 2d 3e 78 46 75 6e 63 20 3d 20 78 46  .  p->xFunc = xF
33da6 75 6e 63 3b 0a 20 20 70 2d 3e 78 53 74 65 70 20  unc;.  p->xStep 
33da7 3d 20 78 53 74 65 70 3b 0a 20 20 70 2d 3e 78 46  = xStep;.  p->xF
33da8 69 6e 61 6c 69 7a 65 20 3d 20 78 46 69 6e 61 6c  inalize = xFinal
33da9 3b 0a 20 20 70 2d 3e 70 55 73 65 72 44 61 74 61  ;.  p->pUserData
33daa 20 3d 20 70 55 73 65 72 44 61 74 61 3b 0a 20 20   = pUserData;.  
33dab 70 2d 3e 6e 41 72 67 20 3d 20 28 75 31 36 29 6e  p->nArg = (u16)n
33dac 41 72 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Arg;.  return SQ
33dad 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
33dae 2a 20 43 72 65 61 74 65 20 6e 65 77 20 75 73 65  * Create new use
33daf 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  r functions..*/.
33db0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33db1 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
33db2 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
33db3 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3 *db,.  const c
33db4 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  har *zFunctionNa
33db5 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
33db6 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69    int enc,.  voi
33db7 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78  d *p,.  void (*x
33db8 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
33db9 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
33dba 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20  e3_value **),.  
33dbb 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
33dbc 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
33dbd 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
33dbe 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78   **),.  void (*x
33dbf 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
33dc0 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e  ontext*).){.  in
33dc1 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
33dc2 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
33dc3 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73  mutex);.  rc = s
33dc4 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
33dc5 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
33dc6 6d 65 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70  me, nArg, enc, p
33dc7 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20  , xFunc, xStep, 
33dc8 78 46 69 6e 61 6c 29 3b 0a 20 20 72 63 20 3d 20  xFinal);.  rc = 
33dc9 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
33dca 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
33dcb 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
33dcc 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
33dcd 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65  rn rc;.}..#ifnde
33dce 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
33dcf 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  F16.SQLITE_API i
33dd0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
33dd1 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20  e_function16(.  
33dd2 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
33dd3 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63  onst void *zFunc
33dd4 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
33dd5 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78  nArg,.  int eTex
33dd6 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 2c  tRep,.  void *p,
33dd7 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  .  void (*xFunc)
33dd8 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
33dd9 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
33dda 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  lue**),.  void (
33ddb 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f  *xStep)(sqlite3_
33ddc 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
33ddd 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20  ite3_value**),. 
33dde 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
33ddf 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
33de0 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ).){.  int rc;. 
33de1 20 63 68 61 72 20 2a 7a 46 75 6e 63 38 3b 0a 20   char *zFunc8;. 
33de2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
33de3 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
33de4 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e  .  assert( !db->
33de5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
33de6 20 20 7a 46 75 6e 63 38 20 3d 20 73 71 6c 69 74    zFunc8 = sqlit
33de7 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a  e3Utf16to8(db, z
33de8 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 2d 31  FunctionName, -1
33de9 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
33dea 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
33deb 7a 46 75 6e 63 38 2c 20 6e 41 72 67 2c 20 65 54  zFunc8, nArg, eT
33dec 65 78 74 52 65 70 2c 20 70 2c 20 78 46 75 6e 63  extRep, p, xFunc
33ded 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
33dee 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
33def 65 28 64 62 2c 20 7a 46 75 6e 63 38 29 3b 0a 20  e(db, zFunc8);. 
33df0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
33df1 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
33df2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
33df3 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
33df4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
33df5 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65  endif.../*.** De
33df6 63 6c 61 72 65 20 74 68 61 74 20 61 20 66 75 6e  clare that a fun
33df7 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f  ction has been o
33df8 76 65 72 6c 6f 61 64 65 64 20 62 79 20 61 20 76  verloaded by a v
33df9 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
33dfa 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74  .** If the funct
33dfb 69 6f 6e 20 61 6c 72 65 61 64 79 20 65 78 69 73  ion already exis
33dfc 74 73 20 61 73 20 61 20 72 65 67 75 6c 61 72 20  ts as a regular 
33dfd 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c  global function,
33dfe 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f   then.** this ro
33dff 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
33e00 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  .  If the functi
33e01 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  on does not exis
33e02 74 2c 20 74 68 65 6e 20 63 72 65 61 74 65 0a 2a  t, then create.*
33e03 2a 20 61 20 6e 65 77 20 6f 6e 65 20 74 68 61 74  * a new one that
33e04 20 61 6c 77 61 79 73 20 74 68 72 6f 77 73 20 61   always throws a
33e05 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e   run-time error.
33e06 20 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69    .**.** When vi
33e07 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 74  rtual tables int
33e08 65 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20 61  end to provide a
33e09 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20 66 75 6e  n overloaded fun
33e0a 63 74 69 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73  ction, they.** s
33e0b 68 6f 75 6c 64 20 63 61 6c 6c 20 74 68 69 73 20  hould call this 
33e0c 72 6f 75 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20  routine to make 
33e0d 73 75 72 65 20 74 68 65 20 67 6c 6f 62 61 6c 20  sure the global 
33e0e 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 2e  function exists.
33e0f 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e  .** A global fun
33e10 63 74 69 6f 6e 20 6d 75 73 74 20 65 78 69 73 74  ction must exist
33e11 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 6e 61   in order for na
33e12 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f  me resolution to
33e13 20 77 6f 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c   work.** properl
33e14 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
33e15 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65   int sqlite3_ove
33e16 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0a  rload_function(.
33e17 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
33e18 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
33e19 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 0a 29  me,.  int nArg.)
33e1a 7b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  {.  int nName = 
33e1b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
33e1c 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63  zName);.  int rc
33e1d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
33e1e 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
33e1f 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  x);.  if( sqlite
33e20 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
33e21 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  , zName, nName, 
33e22 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
33e23 38 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20  8, 0)==0 ){.    
33e24 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
33e25 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72  c(db, zName, nAr
33e26 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a  g, SQLITE_UTF8,.
33e27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e28 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65 33        0, sqlite3
33e29 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c  InvalidFunction,
33e2a 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 63   0, 0);.  }.  rc
33e2b 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
33e2c 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29  t(db, SQLITE_OK)
33e2d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
33e2e 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
33e2f 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
33e30 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
33e31 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a  TE_OMIT_TRACE./*
33e32 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 74  .** Register a t
33e33 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20  race function.  
33e34 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74 68  The pArg from th
33e35 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  e previously reg
33e36 69 73 74 65 72 65 64 20 74 72 61 63 65 0a 2a 2a  istered trace.**
33e37 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a   is returned.  .
33e38 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 74 72 61  **.** A NULL tra
33e39 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e  ce function mean
33e3a 73 20 74 68 61 74 20 6e 6f 20 74 72 61 63 69 6e  s that no tracin
33e3b 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20 20  g is executes.  
33e3c 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74 72  A non-NULL.** tr
33e3d 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ace is a pointer
33e3e 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74   to a function t
33e3f 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 61  hat is invoked a
33e40 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65  t the start of e
33e41 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ach.** SQL state
33e42 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
33e43 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
33e44 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33 20  3_trace(sqlite3 
33e45 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78 54 72 61  *db, void (*xTra
33e46 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  ce)(void*,const 
33e47 63 68 61 72 2a 29 2c 20 76 6f 69 64 20 2a 70 41  char*), void *pA
33e48 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c  rg){.  void *pOl
33e49 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
33e4a 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
33e4b 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62  ex);.  pOld = db
33e4c 2d 3e 70 54 72 61 63 65 41 72 67 3b 0a 20 20 64  ->pTraceArg;.  d
33e4d 62 2d 3e 78 54 72 61 63 65 20 3d 20 78 54 72 61  b->xTrace = xTra
33e4e 63 65 3b 0a 20 20 64 62 2d 3e 70 54 72 61 63 65  ce;.  db->pTrace
33e4f 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71  Arg = pArg;.  sq
33e50 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
33e51 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
33e52 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 2f  return pOld;.}./
33e53 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
33e54 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e  profile function
33e55 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d  .  The pArg from
33e56 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20   the previously 
33e57 72 65 67 69 73 74 65 72 65 64 20 0a 2a 2a 20 70  registered .** p
33e58 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20  rofile function 
33e59 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a  is returned.  .*
33e5a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70 72 6f 66  *.** A NULL prof
33e5b 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61  ile function mea
33e5c 6e 73 20 74 68 61 74 20 6e 6f 20 70 72 6f 66 69  ns that no profi
33e5d 6c 69 6e 67 20 69 73 20 65 78 65 63 75 74 65 73  ling is executes
33e5e 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a  .  A non-NULL.**
33e5f 20 70 72 6f 66 69 6c 65 20 69 73 20 61 20 70 6f   profile is a po
33e60 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
33e61 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76 6f  ion that is invo
33e62 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c  ked at the concl
33e63 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68  usion of.** each
33e64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
33e65 68 61 74 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a 53  hat is run..*/.S
33e66 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
33e67 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28  sqlite3_profile(
33e68 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
33e69 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c    void (*xProfil
33e6a 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
33e6b 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74  har*,sqlite_uint
33e6c 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72  64),.  void *pAr
33e6d 67 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c  g.){.  void *pOl
33e6e 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
33e6f 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
33e70 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62  ex);.  pOld = db
33e71 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 3b 0a 20  ->pProfileArg;. 
33e72 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 3d 20   db->xProfile = 
33e73 78 50 72 6f 66 69 6c 65 3b 0a 20 20 64 62 2d 3e  xProfile;.  db->
33e74 70 50 72 6f 66 69 6c 65 41 72 67 20 3d 20 70 41  pProfileArg = pA
33e75 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rg;.  sqlite3_mu
33e76 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
33e77 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  tex);.  return p
33e78 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Old;.}.#endif /*
33e79 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
33e7a 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50 45  CE */../*** EXPE
33e7b 52 49 4d 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a  RIMENTAL ***.**.
33e7c 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 66 75  ** Register a fu
33e7d 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76  nction to be inv
33e7e 6f 6b 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e  oked when a tran
33e7f 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73  saction comments
33e80 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 76 6f  ..** If the invo
33e81 6b 65 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  ked function ret
33e82 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
33e83 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 62  hen the commit b
33e84 65 63 6f 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c 6c  ecomes a.** roll
33e85 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  back..*/.SQLITE_
33e86 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
33e87 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20  3_commit_hook(. 
33e88 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
33e89 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74             /* At
33e8a 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f  tach the hook to
33e8b 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
33e8c 2f 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 62  /.  int (*xCallb
33e8d 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 20 2f 2a  ack)(void*),  /*
33e8e 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76   Function to inv
33e8f 6f 6b 65 20 6f 6e 20 65 61 63 68 20 63 6f 6d 6d  oke on each comm
33e90 69 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  it */.  void *pA
33e91 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
33e92 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
33e93 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
33e94 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64  .){.  void *pOld
33e95 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
33e96 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
33e97 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d  x);.  pOld = db-
33e98 3e 70 43 6f 6d 6d 69 74 41 72 67 3b 0a 20 20 64  >pCommitArg;.  d
33e99 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61  b->xCommitCallba
33e9a 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a  ck = xCallback;.
33e9b 20 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67    db->pCommitArg
33e9c 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
33e9d 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
33e9e 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
33e9f 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a 0a  urn pOld;.}../*.
33ea0 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 61  ** Register a ca
33ea1 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76  llback to be inv
33ea2 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20 61  oked each time a
33ea3 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c   row is updated,
33ea4 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 6f 72 20  .** inserted or 
33ea5 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74 68  deleted using th
33ea6 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
33ea7 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
33ea8 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
33ea9 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
33eaa 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
33eab 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33eac 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20  Attach the hook 
33ead 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65  to this database
33eae 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 61   */.  void (*xCa
33eaf 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
33eb0 74 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63  t,char const *,c
33eb1 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69  har const *,sqli
33eb2 74 65 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69  te_int64),.  voi
33eb3 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20 20  d *pArg         
33eb4 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
33eb5 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
33eb6 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20  on */.){.  void 
33eb7 2a 70 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33  *pRet;.  sqlite3
33eb8 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
33eb9 3e 6d 75 74 65 78 29 3b 0a 20 20 70 52 65 74 20  >mutex);.  pRet 
33eba 3d 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67  = db->pUpdateArg
33ebb 3b 0a 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43  ;.  db->xUpdateC
33ebc 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62  allback = xCallb
33ebd 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 55 70 64 61  ack;.  db->pUpda
33ebe 74 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  teArg = pArg;.  
33ebf 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
33ec0 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
33ec1 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d    return pRet;.}
33ec2 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
33ec3 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62   a callback to b
33ec4 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20 74  e invoked each t
33ec5 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ime a transactio
33ec6 6e 20 69 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62  n is rolled.** b
33ec7 61 63 6b 20 62 79 20 74 68 69 73 20 64 61 74 61  ack by this data
33ec8 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
33ec9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
33eca 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c  oid *sqlite3_rol
33ecb 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73 71  lback_hook(.  sq
33ecc 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
33ecd 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63          /* Attac
33ece 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68  h the hook to th
33ecf 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  is database */. 
33ed0 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63   void (*xCallbac
33ed1 6b 29 28 76 6f 69 64 2a 29 2c 20 2f 2a 20 43 61  k)(void*), /* Ca
33ed2 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
33ed3 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20  */.  void *pArg 
33ed4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33ed5 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
33ed6 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  e function */.){
33ed7 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20  .  void *pRet;. 
33ed8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
33ed9 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
33eda 0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e 70 52  .  pRet = db->pR
33edb 6f 6c 6c 62 61 63 6b 41 72 67 3b 0a 20 20 64 62  ollbackArg;.  db
33edc 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  ->xRollbackCallb
33edd 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b  ack = xCallback;
33ede 0a 20 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b  .  db->pRollback
33edf 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71  Arg = pArg;.  sq
33ee0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
33ee1 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
33ee2 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a  return pRet;.}..
33ee3 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
33ee4 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 72 75 65  ion returns true
33ee5 20 69 66 20 6d 61 69 6e 2d 6d 65 6d 6f 72 79 20   if main-memory 
33ee6 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69  should be used i
33ee7 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 61 20 74  nstead of.** a t
33ee8 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 66 6f  emporary file fo
33ee9 72 20 74 72 61 6e 73 69 65 6e 74 20 70 61 67 65  r transient page
33eea 72 20 66 69 6c 65 73 20 61 6e 64 20 73 74 61 74  r files and stat
33eeb 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2e 0a  ement journals..
33eec 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  ** The value ret
33eed 75 72 6e 65 64 20 64 65 70 65 6e 64 73 20 6f 6e  urned depends on
33eee 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 64 62   the value of db
33eef 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 28 72 75  ->temp_store (ru
33ef0 6e 74 69 6d 65 0a 2a 2a 20 70 61 72 61 6d 65 74  ntime.** paramet
33ef1 65 72 29 20 61 6e 64 20 74 68 65 20 63 6f 6d 70  er) and the comp
33ef2 69 6c 65 20 74 69 6d 65 20 76 61 6c 75 65 20 6f  ile time value o
33ef3 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  f SQLITE_TEMP_ST
33ef4 4f 52 45 2e 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c  ORE. The.** foll
33ef5 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 73 63  owing table desc
33ef6 72 69 62 65 73 20 74 68 65 20 72 65 6c 61 74 69  ribes the relati
33ef7 6f 6e 73 68 69 70 20 62 65 74 77 65 65 6e 20 74  onship between t
33ef8 68 65 73 65 20 74 77 6f 20 76 61 6c 75 65 73 0a  hese two values.
33ef9 2a 2a 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63  ** and this func
33efa 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 76 61 6c  tions return val
33efb 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  ue..**.**   SQLI
33efc 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 20 20  TE_TEMP_STORE   
33efd 20 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65    db->temp_store
33efe 20 20 20 20 20 4c 6f 63 61 74 69 6f 6e 20 6f 66       Location of
33eff 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62   temporary datab
33f00 61 73 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ase.**   -------
33f01 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d  ----------     -
33f02 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20  -------------   
33f03 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
33f04 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33f05 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20  .**   0         
33f06 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20              any 
33f07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
33f08 69 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e  ile      (return
33f09 20 30 29 0a 2a 2a 20 20 20 31 20 20 20 20 20 20   0).**   1      
33f0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
33f0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f0c 20 20 66 69 6c 65 20 20 20 20 20 20 28 72 65 74    file      (ret
33f0d 75 72 6e 20 30 29 0a 2a 2a 20 20 20 31 20 20 20  urn 0).**   1   
33f0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f0f 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
33f10 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28       memory    (
33f11 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20 31  return 1).**   1
33f12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f13 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
33f14 20 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20          file    
33f15 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20    (return 0).** 
33f16 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
33f17 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20          1       
33f18 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20             file 
33f19 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29 0a       (return 0).
33f1a 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20 20  **   2          
33f1b 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20             2    
33f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65                me
33f1d 6d 6f 72 79 20 20 20 20 28 72 65 74 75 72 6e 20  mory    (return 
33f1e 31 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  1).**   2       
33f1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
33f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f21 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75   memory    (retu
33f22 72 6e 20 31 29 0a 2a 2a 20 20 20 33 20 20 20 20  rn 1).**   3    
33f23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f24 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
33f25 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72      memory    (r
33f26 65 74 75 72 6e 20 31 29 0a 2a 2f 0a 53 51 4c 49  eturn 1).*/.SQLI
33f27 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
33f28 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f  qlite3TempInMemo
33f29 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ry(const sqlite3
33f2a 20 2a 64 62 29 7b 0a 23 69 66 20 53 51 4c 49 54   *db){.#if SQLIT
33f2b 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a  E_TEMP_STORE==1.
33f2c 20 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e 74    return ( db->t
33f2d 65 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 3b 0a  emp_store==2 );.
33f2e 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
33f2f 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 0a  E_TEMP_STORE==2.
33f30 20 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e 74    return ( db->t
33f31 65 6d 70 5f 73 74 6f 72 65 21 3d 31 20 29 3b 0a  emp_store!=1 );.
33f32 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
33f33 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a  E_TEMP_STORE==3.
33f34 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64    return 1;.#end
33f35 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45  if.#if SQLITE_TE
33f36 4d 50 5f 53 54 4f 52 45 3c 31 20 7c 7c 20 53 51  MP_STORE<1 || SQ
33f37 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3e  LITE_TEMP_STORE>
33f38 33 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  3.  return 0;.#e
33f39 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ndif.}../*.** Th
33f3a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
33f3b 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  lled to create a
33f3c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61   connection to a
33f3d 20 64 61 74 61 62 61 73 65 20 42 54 72 65 65 0a   database BTree.
33f3e 2a 2a 20 64 72 69 76 65 72 2e 20 20 49 66 20 7a  ** driver.  If z
33f3f 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20  Filename is the 
33f40 6e 61 6d 65 20 6f 66 20 61 20 66 69 6c 65 2c 20  name of a file, 
33f41 74 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 69  then that file i
33f42 73 0a 2a 2a 20 6f 70 65 6e 65 64 20 61 6e 64 20  s.** opened and 
33f43 75 73 65 64 2e 20 20 49 66 20 7a 46 69 6c 65 6e  used.  If zFilen
33f44 61 6d 65 20 69 73 20 74 68 65 20 6d 61 67 69 63  ame is the magic
33f45 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22   name ":memory:"
33f46 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74   then.** the dat
33f47 61 62 61 73 65 20 69 73 20 73 74 6f 72 65 64 20  abase is stored 
33f48 69 6e 20 6d 65 6d 6f 72 79 20 28 61 6e 64 20 69  in memory (and i
33f49 73 20 74 68 75 73 20 66 6f 72 67 6f 74 74 65 6e  s thus forgotten
33f4a 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74   as soon as.** t
33f4b 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  he connection is
33f4c 20 63 6c 6f 73 65 64 2e 29 20 20 49 66 20 7a 46   closed.)  If zF
33f4d 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20  ilename is NULL 
33f4e 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  then the databas
33f4f 65 0a 2a 2a 20 69 73 20 61 20 22 76 69 72 74 75  e.** is a "virtu
33f50 61 6c 22 20 64 61 74 61 62 61 73 65 20 66 6f 72  al" database for
33f51 20 74 72 61 6e 73 69 65 6e 74 20 75 73 65 20 6f   transient use o
33f52 6e 6c 79 20 61 6e 64 20 69 73 20 64 65 6c 65 74  nly and is delet
33f53 65 64 20 61 73 0a 2a 2a 20 73 6f 6f 6e 20 61 73  ed as.** soon as
33f54 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
33f55 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a  is closed..**.**
33f56 20 41 20 76 69 72 74 75 61 6c 20 64 61 74 61 62   A virtual datab
33f57 61 73 65 20 63 61 6e 20 62 65 20 65 69 74 68 65  ase can be eithe
33f58 72 20 61 20 64 69 73 6b 20 66 69 6c 65 20 28 74  r a disk file (t
33f59 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63  hat is automatic
33f5a 61 6c 6c 79 0a 2a 2a 20 64 65 6c 65 74 65 64 20  ally.** deleted 
33f5b 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 69 73  when the file is
33f5c 20 63 6c 6f 73 65 64 29 20 6f 72 20 69 74 20 61   closed) or it a
33f5d 6e 20 62 65 20 68 65 6c 64 20 65 6e 74 69 72 65  n be held entire
33f5e 6c 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  ly in memory..**
33f5f 20 54 68 65 20 73 71 6c 69 74 65 33 54 65 6d 70   The sqlite3Temp
33f60 49 6e 4d 65 6d 6f 72 79 28 29 20 66 75 6e 63 74  InMemory() funct
33f61 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64  ion is used to d
33f62 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 0a  etermine which..
33f63 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
33f64 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
33f65 65 65 46 61 63 74 6f 72 79 28 0a 20 20 73 71 6c  eeFactory(.  sql
33f66 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
33f67 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64         /* Main d
33f68 61 74 61 62 61 73 65 20 77 68 65 6e 20 6f 70 65  atabase when ope
33f69 6e 69 6e 67 20 61 75 78 20 6f 74 68 65 72 77 69  ning aux otherwi
33f6a 73 65 20 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  se 0 */.  const 
33f6b 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
33f6c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
33f6d 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69  he file containi
33f6e 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74  ng the BTree dat
33f6f 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  abase */.  int o
33f70 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 20 20 20 20  mitJournal,     
33f71 20 20 20 20 20 2f 2a 20 69 66 20 54 52 55 45 20       /* if TRUE 
33f72 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75 72  then do not jour
33f73 6e 61 6c 20 74 68 69 73 20 66 69 6c 65 20 2a 2f  nal this file */
33f74 0a 20 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 20  .  int nCache,  
33f75 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33f76 48 6f 77 20 6d 61 6e 79 20 70 61 67 65 73 20 69  How many pages i
33f77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
33f78 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61   */.  int vfsFla
33f79 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
33f7a 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20  /* Flags passed 
33f7b 74 68 72 6f 75 67 68 20 74 6f 20 76 66 73 4f 70  through to vfsOp
33f7c 65 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a  en */.  Btree **
33f7d 70 70 42 74 72 65 65 20 20 20 20 20 20 20 20 20  ppBtree         
33f7e 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
33f7f 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74  new Btree object
33f80 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
33f81 0a 29 7b 0a 20 20 69 6e 74 20 62 74 46 6c 61 67  .){.  int btFlag
33f82 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  s = 0;.  int rc;
33f83 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71  .  .  assert( sq
33f84 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
33f85 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
33f86 20 61 73 73 65 72 74 28 20 70 70 42 74 72 65 65   assert( ppBtree
33f87 20 21 3d 20 30 29 3b 0a 20 20 69 66 28 20 6f 6d   != 0);.  if( om
33f88 69 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  itJournal ){.   
33f89 20 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45   btFlags |= BTRE
33f8a 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a  E_OMIT_JOURNAL;.
33f8b 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c    }.  if( db->fl
33f8c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4e 6f 52  ags & SQLITE_NoR
33f8d 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 62  eadlock ){.    b
33f8e 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f  tFlags |= BTREE_
33f8f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d  NO_READLOCK;.  }
33f90 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
33f91 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20  OMIT_MEMORYDB.  
33f92 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  if( zFilename==0
33f93 20 26 26 20 73 71 6c 69 74 65 33 54 65 6d 70 49   && sqlite3TempI
33f94 6e 4d 65 6d 6f 72 79 28 64 62 29 20 29 7b 0a 20  nMemory(db) ){. 
33f95 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22     zFilename = "
33f96 3a 6d 65 6d 6f 72 79 3a 22 3b 0a 20 20 7d 0a 23  :memory:";.  }.#
33f97 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 28 76 66  endif..  if( (vf
33f98 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  sFlags & SQLITE_
33f99 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30  OPEN_MAIN_DB)!=0
33f9a 20 26 26 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d   && (zFilename==
33f9b 30 20 7c 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d  0 || *zFilename=
33f9c 3d 30 29 20 29 7b 0a 20 20 20 20 76 66 73 46 6c  =0) ){.    vfsFl
33f9d 61 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73 20  ags = (vfsFlags 
33f9e 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  & ~SQLITE_OPEN_M
33f9f 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54 45  AIN_DB) | SQLITE
33fa0 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20  _OPEN_TEMP_DB;. 
33fa1 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
33fa2 33 42 74 72 65 65 4f 70 65 6e 28 7a 46 69 6c 65  3BtreeOpen(zFile
33fa3 6e 61 6d 65 2c 20 28 73 71 6c 69 74 65 33 20 2a  name, (sqlite3 *
33fa4 29 64 62 2c 20 70 70 42 74 72 65 65 2c 20 62 74  )db, ppBtree, bt
33fa5 46 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73 29  Flags, vfsFlags)
33fa6 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 42  ;..  /* If the B
33fa7 2d 54 72 65 65 20 77 61 73 20 73 75 63 63 65 73  -Tree was succes
33fa8 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73  sfully opened, s
33fa9 65 74 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  et the pager-cac
33faa 68 65 20 73 69 7a 65 20 74 6f 20 74 68 65 0a 20  he size to the. 
33fab 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75   ** default valu
33fac 65 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68  e. Except, if th
33fad 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65 4f  e call to BtreeO
33fae 70 65 6e 28 29 20 72 65 74 75 72 6e 65 64 20 61  pen() returned a
33faf 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6f 70 65   handle.  ** ope
33fb0 6e 20 6f 6e 20 61 6e 20 65 78 69 73 74 69 6e 67  n on an existing
33fb1 20 73 68 61 72 65 64 20 70 61 67 65 72 2d 63 61   shared pager-ca
33fb2 63 68 65 2c 20 64 6f 20 6e 6f 74 20 63 68 61 6e  che, do not chan
33fb3 67 65 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  ge the pager-cac
33fb4 68 65 20 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20  he .  ** size.. 
33fb5 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
33fb6 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71  LITE_OK && 0==sq
33fb7 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
33fb8 28 2a 70 70 42 74 72 65 65 2c 20 30 2c 20 30 29  (*ppBtree, 0, 0)
33fb9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
33fba 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
33fbb 28 2a 70 70 42 74 72 65 65 2c 20 6e 43 61 63 68  (*ppBtree, nCach
33fbc 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
33fbd 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
33fbe 74 75 72 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64  turn UTF-8 encod
33fbf 65 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75  ed English langu
33fc0 61 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20  age explanation 
33fc1 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
33fc2 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a  nt.** error..*/.
33fc3 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
33fc4 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 65   char *sqlite3_e
33fc5 72 72 6d 73 67 28 73 71 6c 69 74 65 33 20 2a 64  rrmsg(sqlite3 *d
33fc6 62 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b){.  const char
33fc7 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62 20 29   *z;.  if( !db )
33fc8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
33fc9 69 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54  ite3ErrStr(SQLIT
33fca 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20  E_NOMEM);.  }.  
33fcb 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65  if( !sqlite3Safe
33fcc 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
33fcd 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  db) ){.    retur
33fce 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  n sqlite3ErrStr(
33fcf 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 29 3b 0a  SQLITE_MISUSE);.
33fd0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
33fd1 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
33fd2 74 65 78 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  tex);.  if( db->
33fd3 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
33fd4 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 45      z = sqlite3E
33fd5 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f 4d  rrStr(SQLITE_NOM
33fd6 45 4d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  EM);.  }else{.  
33fd7 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c    z = (char*)sql
33fd8 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
33fd9 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 61  db->pErr);.    a
33fda 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
33fdb 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
33fdc 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  if( z==0 ){.    
33fdd 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 45 72 72    z = sqlite3Err
33fde 53 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29  Str(db->errCode)
33fdf 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
33fe0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
33fe1 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
33fe2 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66  return z;.}..#if
33fe3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33fe4 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 74  _UTF16./*.** Ret
33fe5 75 72 6e 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  urn UTF-16 encod
33fe6 65 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75  ed English langu
33fe7 61 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20  age explanation 
33fe8 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
33fe9 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a  nt.** error..*/.
33fea 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
33feb 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 65   void *sqlite3_e
33fec 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65 33 20  rrmsg16(sqlite3 
33fed 2a 64 62 29 7b 0a 20 20 73 74 61 74 69 63 20 63  *db){.  static c
33fee 6f 6e 73 74 20 75 31 36 20 6f 75 74 4f 66 4d 65  onst u16 outOfMe
33fef 6d 5b 5d 20 3d 20 7b 0a 20 20 20 20 27 6f 27 2c  m[] = {.    'o',
33ff0 20 27 75 27 2c 20 27 74 27 2c 20 27 20 27 2c 20   'u', 't', ' ', 
33ff1 27 6f 27 2c 20 27 66 27 2c 20 27 20 27 2c 20 27  'o', 'f', ' ', '
33ff2 6d 27 2c 20 27 65 27 2c 20 27 6d 27 2c 20 27 6f  m', 'e', 'm', 'o
33ff3 27 2c 20 27 72 27 2c 20 27 79 27 2c 20 30 0a 20  ', 'r', 'y', 0. 
33ff4 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   };.  static con
33ff5 73 74 20 75 31 36 20 6d 69 73 75 73 65 5b 5d 20  st u16 misuse[] 
33ff6 3d 20 7b 0a 20 20 20 20 27 6c 27 2c 20 27 69 27  = {.    'l', 'i'
33ff7 2c 20 27 62 27 2c 20 27 72 27 2c 20 27 61 27 2c  , 'b', 'r', 'a',
33ff8 20 27 72 27 2c 20 27 79 27 2c 20 27 20 27 2c 20   'r', 'y', ' ', 
33ff9 0a 20 20 20 20 27 72 27 2c 20 27 6f 27 2c 20 27  .    'r', 'o', '
33ffa 75 27 2c 20 27 74 27 2c 20 27 69 27 2c 20 27 6e  u', 't', 'i', 'n
33ffb 27 2c 20 27 65 27 2c 20 27 20 27 2c 20 0a 20 20  ', 'e', ' ', .  
33ffc 20 20 27 63 27 2c 20 27 61 27 2c 20 27 6c 27 2c    'c', 'a', 'l',
33ffd 20 27 6c 27 2c 20 27 65 27 2c 20 27 64 27 2c 20   'l', 'e', 'd', 
33ffe 27 20 27 2c 20 0a 20 20 20 20 27 6f 27 2c 20 27  ' ', .    'o', '
33fff 75 27 2c 20 27 74 27 2c 20 27 20 27 2c 20 0a 20  u', 't', ' ', . 
34000 20 20 20 27 6f 27 2c 20 27 66 27 2c 20 27 20 27     'o', 'f', ' '
34001 2c 20 0a 20 20 20 20 27 73 27 2c 20 27 65 27 2c  , .    's', 'e',
34002 20 27 71 27 2c 20 27 75 27 2c 20 27 65 27 2c 20   'q', 'u', 'e', 
34003 27 6e 27 2c 20 27 63 27 2c 20 27 65 27 2c 20 30  'n', 'c', 'e', 0
34004 0a 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73 74 20 76  .  };..  const v
34005 6f 69 64 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64  oid *z;.  if( !d
34006 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  b ){.    return 
34007 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d 65 6d  (void *)outOfMem
34008 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c  ;.  }.  if( !sql
34009 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
3400a 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20  ickOrOk(db) ){. 
3400b 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 20     return (void 
3400c 2a 29 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20  *)misuse;.  }.  
3400d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
3400e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
3400f 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
34010 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 7a 20  Failed ){.    z 
34011 3d 20 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d  = (void *)outOfM
34012 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  em;.  }else{.   
34013 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   z = sqlite3_val
34014 75 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70 45  ue_text16(db->pE
34015 72 72 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  rr);.    if( z==
34016 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
34017 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62  e3ValueSetStr(db
34018 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 73 71 6c 69  ->pErr, -1, sqli
34019 74 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65 72  te3ErrStr(db->er
3401a 72 43 6f 64 65 29 2c 0a 20 20 20 20 20 20 20 20  rCode),.        
3401b 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20     SQLITE_UTF8, 
3401c 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
3401d 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
3401e 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 64  3_value_text16(d
3401f 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 7d 0a  b->pErr);.    }.
34020 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28      /* A malloc(
34021 29 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65  ) may have faile
34022 64 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c  d within the cal
34023 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c  l to sqlite3_val
34024 75 65 5f 74 65 78 74 31 36 28 29 0a 20 20 20 20  ue_text16().    
34025 2a 2a 20 61 62 6f 76 65 2e 20 49 66 20 74 68 69  ** above. If thi
34026 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  s is the case, t
34027 68 65 6e 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c  hen the db->mall
34028 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6e 65  ocFailed flag ne
34029 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65  eds to.    ** be
3402a 20 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20   cleared before 
3402b 72 65 74 75 72 6e 69 6e 67 2e 20 44 6f 20 74 68  returning. Do th
3402c 69 73 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73  is directly, ins
3402d 74 65 61 64 20 6f 66 20 76 69 61 0a 20 20 20 20  tead of via.    
3402e 2a 2a 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  ** sqlite3ApiExi
3402f 74 28 29 2c 20 74 6f 20 61 76 6f 69 64 20 73 65  t(), to avoid se
34030 74 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  tting the databa
34031 73 65 20 68 61 6e 64 6c 65 20 65 72 72 6f 72 20  se handle error 
34032 6d 65 73 73 61 67 65 2e 0a 20 20 20 20 2a 2f 0a  message..    */.
34033 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
34034 69 6c 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  iled = 0;.  }.  
34035 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
34036 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
34037 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65    return z;.}.#e
34038 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
34039 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
3403a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d  .** Return the m
3403b 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
3403c 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
3403d 62 79 20 61 6e 20 53 51 4c 69 74 65 20 72 6f 75  by an SQLite rou
3403e 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c 20 69 73  tine. If NULL is
3403f 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 74 68  .** passed to th
34040 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 77 65 20  is function, we 
34041 61 73 73 75 6d 65 20 61 20 6d 61 6c 6c 6f 63 28  assume a malloc(
34042 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20  ) failed during 
34043 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a  sqlite3_open()..
34044 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
34045 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  t sqlite3_errcod
34046 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  e(sqlite3 *db){.
34047 20 20 69 66 28 20 64 62 20 26 26 20 21 73 71 6c    if( db && !sql
34048 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
34049 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20  ickOrOk(db) ){. 
3404a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3404b 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69  _MISUSE;.  }.  i
3404c 66 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61  f( !db || db->ma
3404d 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
3404e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3404f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74  NOMEM;.  }.  ret
34050 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65 20  urn db->errCode 
34051 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d  & db->errMask;.}
34052 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
34053 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
34054 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33  _errcode(sqlite3
34055 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 20   *db){.  if( db 
34056 26 26 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  && !sqlite3Safet
34057 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
34058 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
34059 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
3405a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c 7c    }.  if( !db ||
3405b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
3405c 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
3405d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
3405e 7d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 65  }.  return db->e
3405f 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rrCode;.}../*.**
34060 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 6f   Create a new co
34061 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
34062 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 22 64   for database "d
34063 62 22 2e 20 20 54 68 65 20 6e 61 6d 65 20 69 73  b".  The name is
34064 20 7a 4e 61 6d 65 0a 2a 2a 20 61 6e 64 20 74 68   zName.** and th
34065 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 65 6e  e encoding is en
34066 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  c..*/.static int
34067 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e   createCollation
34068 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c  (.  sqlite3* db,
34069 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3406a 4e 61 6d 65 2c 20 0a 20 20 75 38 20 65 6e 63 2c  Name, .  u8 enc,
3406b 0a 20 20 75 38 20 63 6f 6c 6c 54 79 70 65 2c 0a  .  u8 collType,.
3406c 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20    void* pCtx,.  
3406d 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76  int(*xCompare)(v
3406e 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
3406f 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
34070 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78  oid*),.  void(*x
34071 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20  Del)(void*).){. 
34072 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
34073 0a 20 20 69 6e 74 20 65 6e 63 32 3b 0a 20 20 69  .  int enc2;.  i
34074 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
34075 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
34076 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20  );.  .  assert( 
34077 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
34078 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
34079 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45  ..  /* If SQLITE
3407a 5f 55 54 46 31 36 20 69 73 20 73 70 65 63 69 66  _UTF16 is specif
3407b 69 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64  ied as the encod
3407c 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66  ing type, transf
3407d 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f  orm this.  ** to
3407e 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55   one of SQLITE_U
3407f 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45  TF16LE or SQLITE
34080 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67 20 74  _UTF16BE using t
34081 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55  he.  ** SQLITE_U
34082 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f  TF16NATIVE macro
34083 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69  . SQLITE_UTF16 i
34084 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72  s not used inter
34085 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e  nally..  */.  en
34086 63 32 20 3d 20 65 6e 63 3b 0a 20 20 74 65 73 74  c2 = enc;.  test
34087 63 61 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49  case( enc2==SQLI
34088 54 45 5f 55 54 46 31 36 20 29 3b 0a 20 20 74 65  TE_UTF16 );.  te
34089 73 74 63 61 73 65 28 20 65 6e 63 32 3d 3d 53 51  stcase( enc2==SQ
3408a 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
3408b 45 44 20 29 3b 0a 20 20 69 66 28 20 65 6e 63 32  ED );.  if( enc2
3408c 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 7c  ==SQLITE_UTF16 |
3408d 7c 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55  | enc2==SQLITE_U
3408e 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 29 7b 0a  TF16_ALIGNED ){.
3408f 20 20 20 20 65 6e 63 32 20 3d 20 53 51 4c 49 54      enc2 = SQLIT
34090 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20  E_UTF16NATIVE;. 
34091 20 7d 0a 20 20 69 66 28 20 65 6e 63 32 3c 53 51   }.  if( enc2<SQ
34092 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63  LITE_UTF8 || enc
34093 32 3e 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  2>SQLITE_UTF16BE
34094 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
34095 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
34096 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  }..  /* Check if
34097 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 72 65   this call is re
34098 6d 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c 61 63  moving or replac
34099 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20  ing an existing 
3409a 63 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a 20  collation .  ** 
3409b 73 65 71 75 65 6e 63 65 2e 20 49 66 20 73 6f 2c  sequence. If so,
3409c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61   and there are a
3409d 63 74 69 76 65 20 56 4d 73 2c 20 72 65 74 75 72  ctive VMs, retur
3409e 6e 20 62 75 73 79 2e 20 49 66 20 74 68 65 72 65  n busy. If there
3409f 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74  .  ** are no act
340a0 69 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c 69 64  ive VMs, invalid
340a1 61 74 65 20 61 6e 79 20 70 72 65 2d 63 6f 6d 70  ate any pre-comp
340a2 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e  iled statements.
340a3 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 6c 20 3d 20  .  */.  pColl = 
340a4 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
340a5 65 71 28 64 62 2c 20 28 75 38 29 65 6e 63 32 2c  eq(db, (u8)enc2,
340a6 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66   zName, 0);.  if
340a7 28 20 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c  ( pColl && pColl
340a8 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 69 66  ->xCmp ){.    if
340a9 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ( db->activeVdbe
340aa 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Cnt ){.      sql
340ab 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
340ac 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20  LITE_BUSY, .    
340ad 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 64      "unable to d
340ae 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 63 6f 6c  elete/modify col
340af 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
340b0 64 75 65 20 74 6f 20 61 63 74 69 76 65 20 73 74  due to active st
340b1 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20  atements");.    
340b2 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
340b3 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BUSY;.    }.    
340b4 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
340b5 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
340b6 64 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  db);..    /* If 
340b7 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
340b8 63 65 20 70 43 6f 6c 6c 20 77 61 73 20 63 72 65  ce pColl was cre
340b9 61 74 65 64 20 64 69 72 65 63 74 6c 79 20 62 79  ated directly by
340ba 20 61 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a   a call to.    *
340bb 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  * sqlite3_create
340bc 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 20 61 6e 64 20  _collation, and 
340bd 6e 6f 74 20 67 65 6e 65 72 61 74 65 64 20 62 79  not generated by
340be 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 2c   synthCollSeq(),
340bf 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 79  .    ** then any
340c0 20 63 6f 70 69 65 73 20 6d 61 64 65 20 62 79 20   copies made by 
340c1 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 20 6e  synthCollSeq() n
340c2 65 65 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69  eed to be invali
340c3 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 20 41 6c  dated..    ** Al
340c4 73 6f 2c 20 63 6f 6c 6c 61 74 69 6f 6e 20 64 65  so, collation de
340c5 73 74 72 75 63 74 6f 72 20 2d 20 43 6f 6c 6c 53  structor - CollS
340c6 65 71 2e 78 44 65 6c 28 29 20 2d 20 66 75 6e 63  eq.xDel() - func
340c7 74 69 6f 6e 20 6d 61 79 20 6e 65 65 64 0a 20 20  tion may need.  
340c8 20 20 2a 2a 20 74 6f 20 62 65 20 63 61 6c 6c 65    ** to be calle
340c9 64 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 69  d..    */ .    i
340ca 66 28 20 28 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26  f( (pColl->enc &
340cb 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41   ~SQLITE_UTF16_A
340cc 4c 49 47 4e 45 44 29 3d 3d 65 6e 63 32 20 29 7b  LIGNED)==enc2 ){
340cd 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
340ce 61 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48  aColl = sqlite3H
340cf 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f  ashFind(&db->aCo
340d0 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  llSeq, zName, nN
340d1 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  ame);.      int 
340d2 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  j;.      for(j=0
340d3 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ; j<3; j++){.   
340d4 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20       CollSeq *p 
340d5 3d 20 26 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20  = &aColl[j];.   
340d6 20 20 20 20 20 69 66 28 20 70 2d 3e 65 6e 63 3d       if( p->enc=
340d7 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20  =pColl->enc ){. 
340d8 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e           if( p->
340d9 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  xDel ){.        
340da 20 20 20 20 70 2d 3e 78 44 65 6c 28 70 2d 3e 70      p->xDel(p->p
340db 55 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  User);.         
340dc 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e   }.          p->
340dd 78 43 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  xCmp = 0;.      
340de 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
340df 7d 0a 20 20 7d 0a 0a 20 20 70 43 6f 6c 6c 20 3d  }.  }..  pColl =
340e0 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
340e1 53 65 71 28 64 62 2c 20 28 75 38 29 65 6e 63 32  Seq(db, (u8)enc2
340e2 2c 20 7a 4e 61 6d 65 2c 20 31 29 3b 0a 20 20 69  , zName, 1);.  i
340e3 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
340e4 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d 20 78 43  pColl->xCmp = xC
340e5 6f 6d 70 61 72 65 3b 0a 20 20 20 20 70 43 6f 6c  ompare;.    pCol
340e6 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43 74 78 3b  l->pUser = pCtx;
340e7 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c  .    pColl->xDel
340e8 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 70 43 6f   = xDel;.    pCo
340e9 6c 6c 2d 3e 65 6e 63 20 3d 20 28 75 38 29 28 65  ll->enc = (u8)(e
340ea 6e 63 32 20 7c 20 28 65 6e 63 20 26 20 53 51 4c  nc2 | (enc & SQL
340eb 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45  ITE_UTF16_ALIGNE
340ec 44 29 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e  D));.    pColl->
340ed 74 79 70 65 20 3d 20 63 6f 6c 6c 54 79 70 65 3b  type = collType;
340ee 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72  .  }.  sqlite3Er
340ef 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
340f0 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  K, 0);.  return 
340f1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
340f2 2a 0a 2a 2a 20 54 68 69 73 20 61 72 72 61 79 20  *.** This array 
340f3 64 65 66 69 6e 65 73 20 68 61 72 64 20 75 70 70  defines hard upp
340f4 65 72 20 62 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d  er bounds on lim
340f5 69 74 20 76 61 6c 75 65 73 2e 20 20 54 68 65 0a  it values.  The.
340f6 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 6d  ** initializer m
340f7 75 73 74 20 62 65 20 6b 65 70 74 20 69 6e 20 73  ust be kept in s
340f8 79 6e 63 20 77 69 74 68 20 74 68 65 20 53 51 4c  ync with the SQL
340f9 49 54 45 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23  ITE_LIMIT_*.** #
340fa 64 65 66 69 6e 65 73 20 69 6e 20 73 71 6c 69 74  defines in sqlit
340fb 65 33 2e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  e3.h..*/.static 
340fc 63 6f 6e 73 74 20 69 6e 74 20 61 48 61 72 64 4c  const int aHardL
340fd 69 6d 69 74 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c  imit[] = {.  SQL
340fe 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a  ITE_MAX_LENGTH,.
340ff 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c    SQLITE_MAX_SQL
34100 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54  _LENGTH,.  SQLIT
34101 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20  E_MAX_COLUMN,.  
34102 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
34103 44 45 50 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f  DEPTH,.  SQLITE_
34104 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
34105 45 43 54 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  ECT,.  SQLITE_MA
34106 58 5f 56 44 42 45 5f 4f 50 2c 0a 20 20 53 51 4c  X_VDBE_OP,.  SQL
34107 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
34108 5f 41 52 47 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  _ARG,.  SQLITE_M
34109 41 58 5f 41 54 54 41 43 48 45 44 2c 0a 20 20 53  AX_ATTACHED,.  S
3410a 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
3410b 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20  ATTERN_LENGTH,. 
3410c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49   SQLITE_MAX_VARI
3410d 41 42 4c 45 5f 4e 55 4d 42 45 52 2c 0a 20 20 53  ABLE_NUMBER,.  S
3410e 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
3410f 52 5f 44 45 50 54 48 2c 0a 7d 3b 0a 0a 2f 2a 0a  R_DEPTH,.};../*.
34110 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  ** Make sure the
34111 20 68 61 72 64 20 6c 69 6d 69 74 73 20 61 72 65   hard limits are
34112 20 73 65 74 20 74 6f 20 72 65 61 73 6f 6e 61 62   set to reasonab
34113 6c 65 20 76 61 6c 75 65 73 0a 2a 2f 0a 23 69 66  le values.*/.#if
34114 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
34115 54 48 3c 31 30 30 0a 23 20 65 72 72 6f 72 20 53  TH<100.# error S
34116 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
34117 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
34118 74 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 69 66  t 100.#endif.#if
34119 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
3411a 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20 65 72 72  LENGTH<100.# err
3411b 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  or SQLITE_MAX_SQ
3411c 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65  L_LENGTH must be
3411d 20 61 74 20 6c 65 61 73 74 20 31 30 30 0a 23 65   at least 100.#e
3411e 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
3411f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3e 53  MAX_SQL_LENGTH>S
34120 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
34121 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
34122 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d  MAX_SQL_LENGTH m
34123 75 73 74 20 6e 6f 74 20 62 65 20 67 72 65 61 74  ust not be great
34124 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d  er than SQLITE_M
34125 41 58 5f 4c 45 4e 47 54 48 0a 23 65 6e 64 69 66  AX_LENGTH.#endif
34126 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
34127 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c  COMPOUND_SELECT<
34128 32 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  2.# error SQLITE
34129 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  _MAX_COMPOUND_SE
3412a 4c 45 43 54 20 6d 75 73 74 20 62 65 20 61 74 20  LECT must be at 
3412b 6c 65 61 73 74 20 32 0a 23 65 6e 64 69 66 0a 23  least 2.#endif.#
3412c 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44  if SQLITE_MAX_VD
3412d 42 45 5f 4f 50 3c 34 30 0a 23 20 65 72 72 6f 72  BE_OP<40.# error
3412e 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45   SQLITE_MAX_VDBE
3412f 5f 4f 50 20 6d 75 73 74 20 62 65 20 61 74 20 6c  _OP must be at l
34130 65 61 73 74 20 34 30 0a 23 65 6e 64 69 66 0a 23  east 40.#endif.#
34131 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  if SQLITE_MAX_FU
34132 4e 43 54 49 4f 4e 5f 41 52 47 3c 30 20 7c 7c 20  NCTION_ARG<0 || 
34133 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
34134 49 4f 4e 5f 41 52 47 3e 31 30 30 30 0a 23 20 65  ION_ARG>1000.# e
34135 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
34136 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 6d 75 73  FUNCTION_ARG mus
34137 74 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 61  t be between 0 a
34138 6e 64 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 23  nd 1000.#endif.#
34139 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  if SQLITE_MAX_AT
3413a 54 41 43 48 45 44 3c 30 20 7c 7c 20 53 51 4c 49  TACHED<0 || SQLI
3413b 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 3e  TE_MAX_ATTACHED>
3413c 33 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  30.# error SQLIT
3413d 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 20 6d  E_MAX_ATTACHED m
3413e 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 30  ust be between 0
3413f 20 61 6e 64 20 33 30 0a 23 65 6e 64 69 66 0a 23   and 30.#endif.#
34140 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  if SQLITE_MAX_LI
34141 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
34142 48 3c 31 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  H<1.# error SQLI
34143 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
34144 45 52 4e 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20  ERN_LENGTH must 
34145 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65  be at least 1.#e
34146 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
34147 4d 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32 37 36 37  MAX_COLUMN>32767
34148 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
34149 4d 41 58 5f 43 4f 4c 55 4d 4e 20 6d 75 73 74 20  MAX_COLUMN must 
3414a 6e 6f 74 20 65 78 63 65 65 64 20 33 32 37 36 37  not exceed 32767
3414b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
3414c 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44  TE_MAX_TRIGGER_D
3414d 45 50 54 48 3c 31 0a 23 20 65 72 72 6f 72 20 53  EPTH<1.# error S
3414e 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
3414f 52 5f 44 45 50 54 48 20 6d 75 73 74 20 62 65 20  R_DEPTH must be 
34150 61 74 20 6c 65 61 73 74 20 31 0a 23 65 6e 64 69  at least 1.#endi
34151 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  f.../*.** Change
34152 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
34153 6c 69 6d 69 74 2e 20 20 52 65 70 6f 72 74 20 74  limit.  Report t
34154 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 2a 2a  he old value..**
34155 20 49 66 20 61 6e 20 69 6e 76 61 6c 69 64 20 6c   If an invalid l
34156 69 6d 69 74 20 69 6e 64 65 78 20 69 73 20 73 75  imit index is su
34157 70 70 6c 69 65 64 2c 20 72 65 70 6f 72 74 20 2d  pplied, report -
34158 31 2e 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68  1..** Make no ch
34159 61 6e 67 65 73 20 62 75 74 20 73 74 69 6c 6c 20  anges but still 
3415a 72 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76  report the old v
3415b 61 6c 75 65 20 69 66 20 74 68 65 0a 2a 2a 20 6e  alue if the.** n
3415c 65 77 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61  ew limit is nega
3415d 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65  tive..**.** A ne
3415e 77 20 6c 6f 77 65 72 20 6c 69 6d 69 74 20 64 6f  w lower limit do
3415f 65 73 20 6e 6f 74 20 73 68 72 69 6e 6b 20 65 78  es not shrink ex
34160 69 73 74 69 6e 67 20 63 6f 6e 73 74 72 75 63 74  isting construct
34161 73 2e 0a 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20  s..** It merely 
34162 70 72 65 76 65 6e 74 73 20 6e 65 77 20 63 6f 6e  prevents new con
34163 73 74 72 75 63 74 73 20 74 68 61 74 20 65 78 63  structs that exc
34164 65 65 64 20 74 68 65 20 6c 69 6d 69 74 0a 2a 2a  eed the limit.**
34165 20 66 72 6f 6d 20 66 6f 72 6d 69 6e 67 2e 0a 2a   from forming..*
34166 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
34167 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73   sqlite3_limit(s
34168 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
34169 6c 69 6d 69 74 49 64 2c 20 69 6e 74 20 6e 65 77  limitId, int new
3416a 4c 69 6d 69 74 29 7b 0a 20 20 69 6e 74 20 6f 6c  Limit){.  int ol
3416b 64 4c 69 6d 69 74 3b 0a 20 20 69 66 28 20 6c 69  dLimit;.  if( li
3416c 6d 69 74 49 64 3c 30 20 7c 7c 20 6c 69 6d 69 74  mitId<0 || limit
3416d 49 64 3e 3d 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d  Id>=SQLITE_N_LIM
3416e 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  IT ){.    return
3416f 20 2d 31 3b 0a 20 20 7d 0a 20 20 6f 6c 64 4c 69   -1;.  }.  oldLi
34170 6d 69 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  mit = db->aLimit
34171 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20 69 66 28  [limitId];.  if(
34172 20 6e 65 77 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a   newLimit>=0 ){.
34173 20 20 20 20 69 66 28 20 6e 65 77 4c 69 6d 69 74      if( newLimit
34174 3e 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69  >aHardLimit[limi
34175 74 49 64 5d 20 29 7b 0a 20 20 20 20 20 20 6e 65  tId] ){.      ne
34176 77 4c 69 6d 69 74 20 3d 20 61 48 61 72 64 4c 69  wLimit = aHardLi
34177 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20  mit[limitId];.  
34178 20 20 7d 0a 20 20 20 20 64 62 2d 3e 61 4c 69 6d    }.    db->aLim
34179 69 74 5b 6c 69 6d 69 74 49 64 5d 20 3d 20 6e 65  it[limitId] = ne
3417a 77 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65  wLimit;.  }.  re
3417b 74 75 72 6e 20 6f 6c 64 4c 69 6d 69 74 3b 0a 7d  turn oldLimit;.}
3417c 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
3417d 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 77 6f  tine does the wo
3417e 72 6b 20 6f 66 20 6f 70 65 6e 69 6e 67 20 61 20  rk of opening a 
3417f 64 61 74 61 62 61 73 65 20 6f 6e 20 62 65 68 61  database on beha
34180 6c 66 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33  lf of.** sqlite3
34181 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69  _open() and sqli
34182 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20 54 68  te3_open16(). Th
34183 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e  e database filen
34184 61 6d 65 20 22 7a 46 69 6c 65 6e 61 6d 65 22 20  ame "zFilename" 
34185 20 0a 2a 2a 20 69 73 20 55 54 46 2d 38 20 65 6e   .** is UTF-8 en
34186 63 6f 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  coded..*/.static
34187 20 69 6e 74 20 6f 70 65 6e 44 61 74 61 62 61 73   int openDatabas
34188 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e(.  const char 
34189 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 2f 2a 20 44  *zFilename, /* D
3418a 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
3418b 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a   UTF-8 encoded *
3418c 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
3418d 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  Db,        /* OU
3418e 54 3a 20 52 65 74 75 72 6e 65 64 20 64 61 74 61  T: Returned data
3418f 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
34190 20 75 6e 73 69 67 6e 65 64 20 66 6c 61 67 73 2c   unsigned flags,
34191 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61          /* Opera
34192 74 69 6f 6e 61 6c 20 66 6c 61 67 73 20 2a 2f 0a  tional flags */.
34193 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56    const char *zV
34194 66 73 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  fs       /* Name
34195 20 6f 66 20 74 68 65 20 56 46 53 20 74 6f 20 75   of the VFS to u
34196 73 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  se */.){.  sqlit
34197 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63  e3 *db;.  int rc
34198 3b 0a 20 20 69 6e 74 20 69 73 54 68 72 65 61 64  ;.  int isThread
34199 73 61 66 65 3b 0a 0a 20 20 2a 70 70 44 62 20 3d  safe;..  *ppDb =
3419a 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   0;.#ifndef SQLI
3419b 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
3419c 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3419d 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20  initialize();.  
3419e 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
3419f 72 63 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66  rc;.#endif..  if
341a0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
341a1 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78  onfig.bCoreMutex
341a2 3d 3d 30 20 29 7b 0a 20 20 20 20 69 73 54 68 72  ==0 ){.    isThr
341a3 65 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d  eadsafe = 0;.  }
341a4 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
341a5 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d   SQLITE_OPEN_NOM
341a6 55 54 45 58 20 29 7b 0a 20 20 20 20 69 73 54 68  UTEX ){.    isTh
341a7 72 65 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20  readsafe = 0;.  
341a8 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20  }else if( flags 
341a9 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55  & SQLITE_OPEN_FU
341aa 4c 4c 4d 55 54 45 58 20 29 7b 0a 20 20 20 20 69  LLMUTEX ){.    i
341ab 73 54 68 72 65 61 64 73 61 66 65 20 3d 20 31 3b  sThreadsafe = 1;
341ac 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 73  .  }else{.    is
341ad 54 68 72 65 61 64 73 61 66 65 20 3d 20 73 71 6c  Threadsafe = sql
341ae 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
341af 2e 62 46 75 6c 6c 4d 75 74 65 78 3b 0a 20 20 7d  .bFullMutex;.  }
341b0 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  .  if( flags & S
341b1 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41  QLITE_OPEN_PRIVA
341b2 54 45 43 41 43 48 45 20 29 7b 0a 20 20 20 20 66  TECACHE ){.    f
341b3 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f  lags &= ~SQLITE_
341b4 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45  OPEN_SHAREDCACHE
341b5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
341b6 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
341b7 67 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e 61  g.sharedCacheEna
341b8 62 6c 65 64 20 29 7b 0a 20 20 20 20 66 6c 61 67  bled ){.    flag
341b9 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e  s |= SQLITE_OPEN
341ba 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20  _SHAREDCACHE;.  
341bb 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 68  }..  /* Remove h
341bc 61 72 6d 66 75 6c 20 62 69 74 73 20 66 72 6f 6d  armful bits from
341bd 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   the flags param
341be 65 74 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  eter.  **.  ** T
341bf 68 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e  he SQLITE_OPEN_N
341c0 4f 4d 55 54 45 58 20 61 6e 64 20 53 51 4c 49 54  OMUTEX and SQLIT
341c1 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58  E_OPEN_FULLMUTEX
341c2 20 66 6c 61 67 73 20 77 65 72 65 0a 20 20 2a 2a   flags were.  **
341c3 20 64 65 61 6c 74 20 77 69 74 68 20 69 6e 20 74   dealt with in t
341c4 68 65 20 70 72 65 76 69 6f 75 73 20 63 6f 64 65  he previous code
341c5 20 62 6c 6f 63 6b 2e 20 20 42 65 73 69 64 65 73   block.  Besides
341c6 20 74 68 65 73 65 2c 20 74 68 65 20 6f 6e 6c 79   these, the only
341c7 0a 20 20 2a 2a 20 76 61 6c 69 64 20 69 6e 70 75  .  ** valid inpu
341c8 74 20 66 6c 61 67 73 20 66 6f 72 20 73 71 6c 69  t flags for sqli
341c9 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 61 72  te3_open_v2() ar
341ca 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  e SQLITE_OPEN_RE
341cb 41 44 4f 4e 4c 59 2c 0a 20 20 2a 2a 20 53 51 4c  ADONLY,.  ** SQL
341cc 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
341cd 54 45 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f  TE, and SQLITE_O
341ce 50 45 4e 5f 43 52 45 41 54 45 2e 20 20 53 69 6c  PEN_CREATE.  Sil
341cf 65 6e 74 6c 79 20 6d 61 73 6b 0a 20 20 2a 2a 20  ently mask.  ** 
341d0 6f 66 66 20 61 6c 6c 20 6f 74 68 65 72 20 66 6c  off all other fl
341d1 61 67 73 2e 0a 20 20 2a 2f 0a 20 20 66 6c 61 67  ags..  */.  flag
341d2 73 20 26 3d 20 20 7e 28 20 53 51 4c 49 54 45 5f  s &=  ~( SQLITE_
341d3 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
341d4 53 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20  SE |.           
341d5 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
341d6 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20  EXCLUSIVE |.    
341d7 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
341d8 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c  E_OPEN_MAIN_DB |
341d9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
341da 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
341db 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20 20 20  _DB | .         
341dc 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
341dd 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 7c  N_TRANSIENT_DB |
341de 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
341df 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49   SQLITE_OPEN_MAI
341e0 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20  N_JOURNAL | .   
341e1 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
341e2 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55  TE_OPEN_TEMP_JOU
341e3 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20  RNAL | .        
341e4 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
341e5 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 7c 20  EN_SUBJOURNAL | 
341e6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
341e7 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
341e8 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 0a 20 20 20  ER_JOURNAL |.   
341e9 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
341ea 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20  TE_OPEN_NOMUTEX 
341eb 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
341ec 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c   SQLITE_OPEN_FUL
341ed 4c 4d 55 54 45 58 0a 20 20 20 20 20 20 20 20 20  LMUTEX.         
341ee 20 20 20 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c      );..  /* All
341ef 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  ocate the sqlite
341f0 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
341f1 2a 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  */.  db = sqlite
341f2 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a  3MallocZero( siz
341f3 65 6f 66 28 73 71 6c 69 74 65 33 29 20 29 3b 0a  eof(sqlite3) );.
341f4 20 20 69 66 28 20 64 62 3d 3d 30 20 29 20 67 6f    if( db==0 ) go
341f5 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20  to opendb_out;. 
341f6 20 69 66 28 20 69 73 54 68 72 65 61 64 73 61 66   if( isThreadsaf
341f7 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 75 74  e ){.    db->mut
341f8 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
341f9 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
341fa 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a  TEX_RECURSIVE);.
341fb 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 75 74 65      if( db->mute
341fc 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  x==0 ){.      sq
341fd 6c 69 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a  lite3_free(db);.
341fe 20 20 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20        db = 0;.  
341ff 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
34200 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  out;.    }.  }. 
34201 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
34202 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
34203 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d  .  db->errMask =
34204 20 30 78 66 66 3b 0a 20 20 64 62 2d 3e 6e 44 62   0xff;.  db->nDb
34205 20 3d 20 32 3b 0a 20 20 64 62 2d 3e 6d 61 67 69   = 2;.  db->magi
34206 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
34207 5f 42 55 53 59 3b 0a 20 20 64 62 2d 3e 61 44 62  _BUSY;.  db->aDb
34208 20 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63   = db->aDbStatic
34209 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  ;..  assert( siz
3420a 65 6f 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29 3d  eof(db->aLimit)=
3420b 3d 73 69 7a 65 6f 66 28 61 48 61 72 64 4c 69 6d  =sizeof(aHardLim
3420c 69 74 29 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28  it) );.  memcpy(
3420d 64 62 2d 3e 61 4c 69 6d 69 74 2c 20 61 48 61 72  db->aLimit, aHar
3420e 64 4c 69 6d 69 74 2c 20 73 69 7a 65 6f 66 28 64  dLimit, sizeof(d
3420f 62 2d 3e 61 4c 69 6d 69 74 29 29 3b 0a 20 20 64  b->aLimit));.  d
34210 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
34211 31 3b 0a 20 20 64 62 2d 3e 6e 65 78 74 41 75 74  1;.  db->nextAut
34212 6f 76 61 63 20 3d 20 2d 31 3b 0a 20 20 64 62 2d  ovac = -1;.  db-
34213 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20  >nextPagesize = 
34214 30 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  0;.  db->flags |
34215 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f  = SQLITE_ShortCo
34216 6c 4e 61 6d 65 73 0a 23 69 66 20 53 51 4c 49 54  lNames.#if SQLIT
34217 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46  E_DEFAULT_FILE_F
34218 4f 52 4d 41 54 3c 34 0a 20 20 20 20 20 20 20 20  ORMAT<4.        
34219 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54           | SQLIT
3421a 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 0a  E_LegacyFileFmt.
3421b 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
3421c 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44  LITE_ENABLE_LOAD
3421d 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 20  _EXTENSION.     
3421e 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 53 51              | SQ
3421f 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69  LITE_LoadExtensi
34220 6f 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  on.#endif.#if SQ
34221 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43  LITE_DEFAULT_REC
34222 55 52 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a  URSIVE_TRIGGERS.
34223 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34224 20 7c 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69   | SQLITE_RecTri
34225 67 67 65 72 73 0a 23 65 6e 64 69 66 0a 20 20 20  ggers.#endif.   
34226 20 20 20 3b 0a 20 20 73 71 6c 69 74 65 33 48 61     ;.  sqlite3Ha
34227 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 43 6f 6c  shInit(&db->aCol
34228 6c 53 65 71 29 3b 0a 23 69 66 6e 64 65 66 20 53  lSeq);.#ifndef S
34229 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
3422a 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65  ALTABLE.  sqlite
3422b 33 48 61 73 68 49 6e 69 74 28 26 64 62 2d 3e 61  3HashInit(&db->a
3422c 4d 6f 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66 0a  Module);.#endif.
3422d 0a 20 20 64 62 2d 3e 70 56 66 73 20 3d 20 73 71  .  db->pVfs = sq
3422e 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 7a  lite3_vfs_find(z
3422f 56 66 73 29 3b 0a 20 20 69 66 28 20 21 64 62 2d  Vfs);.  if( !db-
34230 3e 70 56 66 73 20 29 7b 0a 20 20 20 20 72 63 20  >pVfs ){.    rc 
34231 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
34232 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
34233 28 64 62 2c 20 72 63 2c 20 22 6e 6f 20 73 75 63  (db, rc, "no suc
34234 68 20 76 66 73 3a 20 25 73 22 2c 20 7a 56 66 73  h vfs: %s", zVfs
34235 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  );.    goto open
34236 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  db_out;.  }..  /
34237 2a 20 41 64 64 20 74 68 65 20 64 65 66 61 75 6c  * Add the defaul
34238 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  t collation sequ
34239 65 6e 63 65 20 42 49 4e 41 52 59 2e 20 42 49 4e  ence BINARY. BIN
3423a 41 52 59 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f  ARY works for bo
3423b 74 68 20 55 54 46 2d 38 0a 20 20 2a 2a 20 61 6e  th UTF-8.  ** an
3423c 64 20 55 54 46 2d 31 36 2c 20 73 6f 20 61 64 64  d UTF-16, so add
3423d 20 61 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 65   a version for e
3423e 61 63 68 20 74 6f 20 61 76 6f 69 64 20 61 6e 79  ach to avoid any
3423f 20 75 6e 6e 65 63 65 73 73 61 72 79 0a 20 20 2a   unnecessary.  *
34240 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 54  * conversions. T
34241 68 65 20 6f 6e 6c 79 20 65 72 72 6f 72 20 74 68  he only error th
34242 61 74 20 63 61 6e 20 6f 63 63 75 72 20 68 65 72  at can occur her
34243 65 20 69 73 20 61 20 6d 61 6c 6c 6f 63 28 29 20  e is a malloc() 
34244 66 61 69 6c 75 72 65 2e 0a 20 20 2a 2f 0a 20 20  failure..  */.  
34245 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
34246 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51  db, "BINARY", SQ
34247 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
34248 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 2c 20 30  E_COLL_BINARY, 0
34249 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3424a 20 20 20 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c      binCollFunc,
3424b 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c   0);.  createCol
3424c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41  lation(db, "BINA
3424d 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  RY", SQLITE_UTF1
3424e 36 42 45 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  6BE, SQLITE_COLL
3424f 5f 42 49 4e 41 52 59 2c 20 30 2c 0a 20 20 20 20  _BINARY, 0,.    
34250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 69                bi
34251 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20  nCollFunc, 0);. 
34252 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e   createCollation
34253 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53  (db, "BINARY", S
34254 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53  QLITE_UTF16LE, S
34255 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52  QLITE_COLL_BINAR
34256 59 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  Y, 0,.          
34257 20 20 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c 46          binCollF
34258 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74  unc, 0);.  creat
34259 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22  eCollation(db, "
3425a 52 54 52 49 4d 22 2c 20 53 51 4c 49 54 45 5f 55  RTRIM", SQLITE_U
3425b 54 46 38 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  TF8, SQLITE_COLL
3425c 5f 55 53 45 52 2c 20 28 76 6f 69 64 2a 29 31 2c  _USER, (void*)1,
3425d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3425e 20 20 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20     binCollFunc, 
3425f 30 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  0);.  if( db->ma
34260 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
34261 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
34262 74 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 44 66  t;.  }.  db->pDf
34263 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33  ltColl = sqlite3
34264 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
34265 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 42 49  SQLITE_UTF8, "BI
34266 4e 41 52 59 22 2c 20 30 29 3b 0a 20 20 61 73 73  NARY", 0);.  ass
34267 65 72 74 28 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ert( db->pDfltCo
34268 6c 6c 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41  ll!=0 );..  /* A
34269 6c 73 6f 20 61 64 64 20 61 20 55 54 46 2d 38 20  lso add a UTF-8 
3426a 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 65  case-insensitive
3426b 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3426c 6e 63 65 2e 20 2a 2f 0a 20 20 63 72 65 61 74 65  nce. */.  create
3426d 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 4e  Collation(db, "N
3426e 4f 43 41 53 45 22 2c 20 53 51 4c 49 54 45 5f 55  OCASE", SQLITE_U
3426f 54 46 38 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  TF8, SQLITE_COLL
34270 5f 4e 4f 43 41 53 45 2c 20 30 2c 0a 20 20 20 20  _NOCASE, 0,.    
34271 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f                no
34272 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e  caseCollatingFun
34273 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65  c, 0);..  /* Ope
34274 6e 20 74 68 65 20 62 61 63 6b 65 6e 64 20 64 61  n the backend da
34275 74 61 62 61 73 65 20 64 72 69 76 65 72 20 2a 2f  tabase driver */
34276 0a 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73  .  db->openFlags
34277 20 3d 20 66 6c 61 67 73 3b 0a 20 20 72 63 20 3d   = flags;.  rc =
34278 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63   sqlite3BtreeFac
34279 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65 6e 61  tory(db, zFilena
3427a 6d 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45  me, 0, SQLITE_DE
3427b 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
3427c 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
3427d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6c                fl
3427e 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  ags | SQLITE_OPE
3427f 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20  N_MAIN_DB,.     
34280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34281 20 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 30        &db->aDb[0
34282 5d 2e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  ].pBt);.  if( rc
34283 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
34284 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
34285 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
34286 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
34287 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
34288 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
34289 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20  (db, rc, 0);.   
3428a 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74   goto opendb_out
3428b 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 5b  ;.  }.  db->aDb[
3428c 30 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c  0].pSchema = sql
3428d 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 64 62  ite3SchemaGet(db
3428e 2c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  , db->aDb[0].pBt
3428f 29 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e  );.  db->aDb[1].
34290 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65  pSchema = sqlite
34291 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 30  3SchemaGet(db, 0
34292 29 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 64 65  );...  /* The de
34293 66 61 75 6c 74 20 73 61 66 65 74 79 5f 6c 65 76  fault safety_lev
34294 65 6c 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20  el for the main 
34295 64 61 74 61 62 61 73 65 20 69 73 20 27 66 75 6c  database is 'ful
34296 6c 27 3b 20 66 6f 72 20 74 68 65 20 74 65 6d 70  l'; for the temp
34297 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69  .  ** database i
34298 74 20 69 73 20 27 4e 4f 4e 45 27 2e 20 54 68 69  t is 'NONE'. Thi
34299 73 20 6d 61 74 63 68 65 73 20 74 68 65 20 70 61  s matches the pa
3429a 67 65 72 20 6c 61 79 65 72 20 64 65 66 61 75 6c  ger layer defaul
3429b 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20 64 62 2d  ts.  .  */.  db-
3429c 3e 61 44 62 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20  >aDb[0].zName = 
3429d 22 6d 61 69 6e 22 3b 0a 20 20 64 62 2d 3e 61 44  "main";.  db->aD
3429e 62 5b 30 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65  b[0].safety_leve
3429f 6c 20 3d 20 33 3b 0a 20 20 64 62 2d 3e 61 44 62  l = 3;.  db->aDb
342a0 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 22 74 65 6d  [1].zName = "tem
342a1 70 22 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d  p";.  db->aDb[1]
342a2 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20  .safety_level = 
342a3 31 3b 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20  1;..  db->magic 
342a4 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  = SQLITE_MAGIC_O
342a5 50 45 4e 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  PEN;.  if( db->m
342a6 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
342a7 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f     goto opendb_o
342a8 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  ut;.  }..  /* Re
342a9 67 69 73 74 65 72 20 61 6c 6c 20 62 75 69 6c 74  gister all built
342aa 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62  -in functions, b
342ab 75 74 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70  ut do not attemp
342ac 74 20 74 6f 20 72 65 61 64 20 74 68 65 0a 20 20  t to read the.  
342ad 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ** database sche
342ae 6d 61 20 79 65 74 2e 20 54 68 69 73 20 69 73 20  ma yet. This is 
342af 64 65 6c 61 79 65 64 20 75 6e 74 69 6c 20 74 68  delayed until th
342b0 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65  e first time the
342b1 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 69   database.  ** i
342b2 73 20 61 63 63 65 73 73 65 64 2e 0a 20 20 2a 2f  s accessed..  */
342b3 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  .  sqlite3Error(
342b4 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30  db, SQLITE_OK, 0
342b5 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69  );.  sqlite3Regi
342b6 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74  sterBuiltinFunct
342b7 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 2f 2a 20  ions(db);..  /* 
342b8 4c 6f 61 64 20 61 75 74 6f 6d 61 74 69 63 20 65  Load automatic e
342b9 78 74 65 6e 73 69 6f 6e 73 20 2d 20 65 78 74 65  xtensions - exte
342ba 6e 73 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65  nsions that have
342bb 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64   been registered
342bc 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  .  ** using the 
342bd 73 71 6c 69 74 65 33 5f 61 75 74 6f 6d 61 74 69  sqlite3_automati
342be 63 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 41 50  c_extension() AP
342bf 49 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  I..  */.  sqlite
342c0 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69  3AutoLoadExtensi
342c1 6f 6e 73 28 64 62 29 3b 0a 20 20 72 63 20 3d 20  ons(db);.  rc = 
342c2 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
342c3 64 62 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  db);.  if( rc!=S
342c4 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
342c5 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b  goto opendb_out;
342c6 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  .  }..#ifdef SQL
342c7 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 31 0a  ITE_ENABLE_FTS1.
342c8 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
342c9 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 65  cFailed ){.    e
342ca 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65  xtern int sqlite
342cb 33 46 74 73 31 49 6e 69 74 28 73 71 6c 69 74 65  3Fts1Init(sqlite
342cc 33 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  3*);.    rc = sq
342cd 6c 69 74 65 33 46 74 73 31 49 6e 69 74 28 64 62  lite3Fts1Init(db
342ce 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23  );.  }.#endif..#
342cf 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
342d0 42 4c 45 5f 46 54 53 32 0a 20 20 69 66 28 20 21  BLE_FTS2.  if( !
342d1 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
342d2 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
342d3 4b 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20  K ){.    extern 
342d4 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 32 49  int sqlite3Fts2I
342d5 6e 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  nit(sqlite3*);. 
342d6 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46     rc = sqlite3F
342d7 74 73 32 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d  ts2Init(db);.  }
342d8 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
342d9 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
342da 53 33 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  S3.  if( !db->ma
342db 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63  llocFailed && rc
342dc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
342dd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46     rc = sqlite3F
342de 74 73 33 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d  ts3Init(db);.  }
342df 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
342e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43  SQLITE_ENABLE_IC
342e1 55 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c  U.  if( !db->mal
342e2 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d  locFailed && rc=
342e3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
342e4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 63    rc = sqlite3Ic
342e5 75 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23  uInit(db);.  }.#
342e6 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
342e7 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45  LITE_ENABLE_RTRE
342e8 45 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c  E.  if( !db->mal
342e9 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d  locFailed && rc=
342ea 3d 53 51 4c 49 54 45 5f 4f 4b 29 7b 0a 20 20 20  =SQLITE_OK){.   
342eb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 74 72   rc = sqlite3Rtr
342ec 65 65 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a  eeInit(db);.  }.
342ed 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
342ee 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30  3Error(db, rc, 0
342ef 29 3b 0a 0a 20 20 2f 2a 20 2d 44 53 51 4c 49 54  );..  /* -DSQLIT
342f0 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e  E_DEFAULT_LOCKIN
342f1 47 5f 4d 4f 44 45 3d 31 20 6d 61 6b 65 73 20 45  G_MODE=1 makes E
342f2 58 43 4c 55 53 49 56 45 20 74 68 65 20 64 65 66  XCLUSIVE the def
342f3 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a  ault locking.  *
342f4 2a 20 6d 6f 64 65 2e 20 20 2d 44 53 51 4c 49 54  * mode.  -DSQLIT
342f5 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e  E_DEFAULT_LOCKIN
342f6 47 5f 4d 4f 44 45 3d 30 20 6d 61 6b 65 20 4e 4f  G_MODE=0 make NO
342f7 52 4d 41 4c 20 74 68 65 20 64 65 66 61 75 6c 74  RMAL the default
342f8 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f   locking.  ** mo
342f9 64 65 2e 20 20 44 6f 69 6e 67 20 6e 6f 74 68 69  de.  Doing nothi
342fa 6e 67 20 61 74 20 61 6c 6c 20 61 6c 73 6f 20 6d  ng at all also m
342fb 61 6b 65 73 20 4e 4f 52 4d 41 4c 20 74 68 65 20  akes NORMAL the 
342fc 64 65 66 61 75 6c 74 2e 0a 20 20 2a 2f 0a 23 69  default..  */.#i
342fd 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  fdef SQLITE_DEFA
342fe 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
342ff 0a 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d  .  db->dfltLockM
34300 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46  ode = SQLITE_DEF
34301 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44  AULT_LOCKING_MOD
34302 45 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  E;.  sqlite3Page
34303 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 73 71 6c  rLockingMode(sql
34304 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64  ite3BtreePager(d
34305 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 2c 0a  b->aDb[0].pBt),.
34306 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34307 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
34308 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47  _DEFAULT_LOCKING
34309 5f 4d 4f 44 45 29 3b 0a 23 65 6e 64 69 66 0a 0a  _MODE);.#endif..
3430a 20 20 2f 2a 20 45 6e 61 62 6c 65 20 74 68 65 20    /* Enable the 
3430b 6c 6f 6f 6b 61 73 69 64 65 2d 6d 61 6c 6c 6f 63  lookaside-malloc
3430c 20 73 75 62 73 79 73 74 65 6d 20 2a 2f 0a 20 20   subsystem */.  
3430d 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 64  setupLookaside(d
3430e 62 2c 20 30 2c 20 73 71 6c 69 74 65 33 47 6c 6f  b, 0, sqlite3Glo
3430f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b  balConfig.szLook
34310 61 73 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20  aside,.         
34311 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
34312 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
34313 69 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65 29 3b 0a  ig.nLookaside);.
34314 0a 6f 70 65 6e 64 62 5f 6f 75 74 3a 0a 20 20 69  .opendb_out:.  i
34315 66 28 20 64 62 20 29 7b 0a 20 20 20 20 61 73 73  f( db ){.    ass
34316 65 72 74 28 20 64 62 2d 3e 6d 75 74 65 78 21 3d  ert( db->mutex!=
34317 30 20 7c 7c 20 69 73 54 68 72 65 61 64 73 61 66  0 || isThreadsaf
34318 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47  e==0 || sqlite3G
34319 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c  lobalConfig.bFul
3431a 6c 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 20 20 20  lMutex==0 );.   
3431b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
3431c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
3431d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
3431e 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b  te3_errcode(db);
3431f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
34320 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73  E_NOMEM ){.    s
34321 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29  qlite3_close(db)
34322 3b 0a 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20  ;.    db = 0;.  
34323 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
34324 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 64  LITE_OK ){.    d
34325 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
34326 45 5f 4d 41 47 49 43 5f 53 49 43 4b 3b 0a 20 20  E_MAGIC_SICK;.  
34327 7d 0a 20 20 2a 70 70 44 62 20 3d 20 64 62 3b 0a  }.  *ppDb = db;.
34328 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
34329 41 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a  ApiExit(0, rc);.
3432a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  }../*.** Open a 
3432b 6e 65 77 20 64 61 74 61 62 61 73 65 20 68 61 6e  new database han
3432c 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
3432d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
3432e 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  pen(.  const cha
3432f 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20  r *zFilename, . 
34330 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20   sqlite3 **ppDb 
34331 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70 65  .){.  return ope
34332 6e 44 61 74 61 62 61 73 65 28 7a 46 69 6c 65 6e  nDatabase(zFilen
34333 61 6d 65 2c 20 70 70 44 62 2c 0a 20 20 20 20 20  ame, ppDb,.     
34334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34335 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
34336 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f  DWRITE | SQLITE_
34337 4f 50 45 4e 5f 43 52 45 41 54 45 2c 20 30 29 3b  OPEN_CREATE, 0);
34338 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
34339 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  t sqlite3_open_v
3433a 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  2(.  const char 
3433b 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20  *filename,   /* 
3433c 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  Database filenam
3433d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73  e (UTF-8) */.  s
3433e 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20  qlite3 **ppDb,  
3433f 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53         /* OUT: S
34340 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20  QLite db handle 
34341 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
34342 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34343 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  Flags */.  const
34344 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20   char *zVfs     
34345 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46     /* Name of VF
34346 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20  S module to use 
34347 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f  */.){.  return o
34348 70 65 6e 44 61 74 61 62 61 73 65 28 66 69 6c 65  penDatabase(file
34349 6e 61 6d 65 2c 20 70 70 44 62 2c 20 66 6c 61 67  name, ppDb, flag
3434a 73 2c 20 7a 56 66 73 29 3b 0a 7d 0a 0a 23 69 66  s, zVfs);.}..#if
3434b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3434c 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 4f 70 65  _UTF16./*.** Ope
3434d 6e 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65  n a new database
3434e 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   handle..*/.SQLI
3434f 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
34350 65 33 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e  e3_open16(.  con
34351 73 74 20 76 6f 69 64 20 2a 7a 46 69 6c 65 6e 61  st void *zFilena
34352 6d 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a  me, .  sqlite3 *
34353 2a 70 70 44 62 0a 29 7b 0a 20 20 63 68 61 72 20  *ppDb.){.  char 
34354 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 6e 61 6d 65  const *zFilename
34355 38 3b 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d  8;   /* zFilenam
34356 65 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46  e encoded in UTF
34357 2d 38 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54  -8 instead of UT
34358 46 2d 31 36 20 2a 2f 0a 20 20 73 71 6c 69 74 65  F-16 */.  sqlite
34359 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20  3_value *pVal;. 
3435a 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
3435b 72 74 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b  rt( zFilename );
3435c 0a 20 20 61 73 73 65 72 74 28 20 70 70 44 62 20  .  assert( ppDb 
3435d 29 3b 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a  );.  *ppDb = 0;.
3435e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3435f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72  MIT_AUTOINIT.  r
34360 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  c = sqlite3_init
34361 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20  ialize();.  if( 
34362 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
34363 23 65 6e 64 69 66 0a 20 20 70 56 61 6c 20 3d 20  #endif.  pVal = 
34364 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
34365 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c  0);.  sqlite3Val
34366 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d  ueSetStr(pVal, -
34367 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 53 51  1, zFilename, SQ
34368 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
34369 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
3436a 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 38 20 3d  ;.  zFilename8 =
3436b 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78   sqlite3ValueTex
3436c 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55  t(pVal, SQLITE_U
3436d 54 46 38 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c  TF8);.  if( zFil
3436e 65 6e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72 63  ename8 ){.    rc
3436f 20 3d 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28   = openDatabase(
34370 7a 46 69 6c 65 6e 61 6d 65 38 2c 20 70 70 44 62  zFilename8, ppDb
34371 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
34372 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
34373 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20  PEN_READWRITE | 
34374 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
34375 54 45 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65  TE, 0);.    asse
34376 72 74 28 20 2a 70 70 44 62 20 7c 7c 20 72 63 3d  rt( *ppDb || rc=
34377 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
34378 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
34379 49 54 45 5f 4f 4b 20 26 26 20 21 44 62 48 61 73  ITE_OK && !DbHas
3437a 50 72 6f 70 65 72 74 79 28 2a 70 70 44 62 2c 20  Property(*ppDb, 
3437b 30 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  0, DB_SchemaLoad
3437c 65 64 29 20 29 7b 0a 20 20 20 20 20 20 45 4e 43  ed) ){.      ENC
3437d 28 2a 70 70 44 62 29 20 3d 20 53 51 4c 49 54 45  (*ppDb) = SQLITE
3437e 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20  _UTF16NATIVE;.  
3437f 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
34380 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
34381 45 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  EM;.  }.  sqlite
34382 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29  3ValueFree(pVal)
34383 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ;..  return sqli
34384 74 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63  te3ApiExit(0, rc
34385 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
34386 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
34387 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73   */../*.** Regis
34388 74 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74  ter a new collat
34389 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74  ion sequence wit
3438a 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  h the database h
3438b 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c  andle db..*/.SQL
3438c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3438d 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
3438e 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a  tion(.  sqlite3*
3438f 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68   db, .  const ch
34390 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e  ar *zName, .  in
34391 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20  t enc, .  void* 
34392 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f  pCtx,.  int(*xCo
34393 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74  mpare)(void*,int
34394 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
34395 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b  ,const void*).){
34396 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
34397 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
34398 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61  (db->mutex);.  a
34399 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
3439a 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63  ocFailed );.  rc
3439b 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69   = createCollati
3439c 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 28 75  on(db, zName, (u
3439d 38 29 65 6e 63 2c 20 53 51 4c 49 54 45 5f 43 4f  8)enc, SQLITE_CO
3439e 4c 4c 5f 55 53 45 52 2c 20 70 43 74 78 2c 20 78  LL_USER, pCtx, x
3439f 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 72  Compare, 0);.  r
343a0 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
343a1 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
343a2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
343a3 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
343a4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
343a5 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e  .** Register a n
343a6 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ew collation seq
343a7 75 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64  uence with the d
343a8 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
343a9 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  b..*/.SQLITE_API
343aa 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
343ab 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
343ac 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c  (.  sqlite3* db,
343ad 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
343ae 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e  zName, .  int en
343af 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78  c, .  void* pCtx
343b0 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72  ,.  int(*xCompar
343b1 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  e)(void*,int,con
343b2 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
343b3 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69  st void*),.  voi
343b4 64 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a  d(*xDel)(void*).
343b5 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
343b6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
343b7 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
343b8 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
343b9 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
343ba 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61  rc = createColla
343bb 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  tion(db, zName, 
343bc 28 75 38 29 65 6e 63 2c 20 53 51 4c 49 54 45 5f  (u8)enc, SQLITE_
343bd 43 4f 4c 4c 5f 55 53 45 52 2c 20 70 43 74 78 2c  COLL_USER, pCtx,
343be 20 78 43 6f 6d 70 61 72 65 2c 20 78 44 65 6c 29   xCompare, xDel)
343bf 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
343c0 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
343c1 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
343c2 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
343c3 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
343c4 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
343c5 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
343c6 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65  ** Register a ne
343c7 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  w collation sequ
343c8 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61  ence with the da
343c9 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
343ca 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
343cb 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
343cc 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a  te_collation16(.
343cd 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a    sqlite3* db, .
343ce 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e    const void *zN
343cf 61 6d 65 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 20  ame,.  int enc, 
343d0 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20  .  void* pCtx,. 
343d1 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28   int(*xCompare)(
343d2 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
343d3 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
343d4 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20  void*).){.  int 
343d5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
343d6 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 38 3b 0a    char *zName8;.
343d7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
343d8 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
343d9 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
343da 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
343db 0a 20 20 7a 4e 61 6d 65 38 20 3d 20 73 71 6c 69  .  zName8 = sqli
343dc 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20  te3Utf16to8(db, 
343dd 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69 66  zName, -1);.  if
343de 28 20 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20 20  ( zName8 ){.    
343df 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61  rc = createColla
343e0 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38 2c  tion(db, zName8,
343e1 20 28 75 38 29 65 6e 63 2c 20 53 51 4c 49 54 45   (u8)enc, SQLITE
343e2 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 70 43 74 78  _COLL_USER, pCtx
343e3 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a  , xCompare, 0);.
343e4 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
343e5 65 28 64 62 2c 20 7a 4e 61 6d 65 38 29 3b 0a 20  e(db, zName8);. 
343e6 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
343e7 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
343e8 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
343e9 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
343ea 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
343eb 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
343ec 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
343ed 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  /../*.** Registe
343ee 72 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  r a collation se
343ef 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79 20 63  quence factory c
343f0 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65  allback with the
343f1 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
343f2 0a 2a 2a 20 64 62 2e 20 52 65 70 6c 61 63 65 20  .** db. Replace 
343f3 61 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69  any previously i
343f4 6e 73 74 61 6c 6c 65 64 20 63 6f 6c 6c 61 74 69  nstalled collati
343f5 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63 74  on sequence fact
343f6 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ory..*/.SQLITE_A
343f7 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
343f8 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28  ollation_needed(
343f9 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
343fa 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65  .  void *pCollNe
343fb 65 64 65 64 41 72 67 2c 20 0a 20 20 76 6f 69 64  ededArg, .  void
343fc 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76  (*xCollNeeded)(v
343fd 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
343fe 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
343ff 20 63 68 61 72 2a 29 0a 29 7b 0a 20 20 73 71 6c   char*).){.  sql
34400 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
34401 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
34402 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d  b->xCollNeeded =
34403 20 78 43 6f 6c 6c 4e 65 65 64 65 64 3b 0a 20 20   xCollNeeded;.  
34404 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31  db->xCollNeeded1
34405 36 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 43 6f  6 = 0;.  db->pCo
34406 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d 20 70 43  llNeededArg = pC
34407 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20  ollNeededArg;.  
34408 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
34409 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
3440a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3440b 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  OK;.}..#ifndef S
3440c 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
3440d 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
3440e 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
3440f 65 6e 63 65 20 66 61 63 74 6f 72 79 20 63 61 6c  ence factory cal
34410 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 64  lback with the d
34411 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a  atabase handle.*
34412 2a 20 64 62 2e 20 52 65 70 6c 61 63 65 20 61 6e  * db. Replace an
34413 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73  y previously ins
34414 74 61 6c 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  talled collation
34415 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72   sequence factor
34416 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
34417 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
34418 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28  lation_needed16(
34419 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
3441a 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65  .  void *pCollNe
3441b 65 64 65 64 41 72 67 2c 20 0a 20 20 76 6f 69 64  ededArg, .  void
3441c 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29  (*xCollNeeded16)
3441d 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c  (void*,sqlite3*,
3441e 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e  int eTextRep,con
3441f 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73  st void*).){.  s
34420 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
34421 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
34422 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
34423 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c   = 0;.  db->xCol
34424 6c 4e 65 65 64 65 64 31 36 20 3d 20 78 43 6f 6c  lNeeded16 = xCol
34425 6c 4e 65 65 64 65 64 31 36 3b 0a 20 20 64 62 2d  lNeeded16;.  db-
34426 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20  >pCollNeededArg 
34427 3d 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67  = pCollNeededArg
34428 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
34429 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
3442a 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
3442b 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
3442c 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
3442d 55 54 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65  UTF16 */..#ifnde
3442e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 4c  f SQLITE_OMIT_GL
3442f 4f 42 41 4c 52 45 43 4f 56 45 52 0a 23 69 66 6e  OBALRECOVER.#ifn
34430 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
34431 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a  DEPRECATED./*.**
34432 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
34433 73 20 6e 6f 77 20 61 6e 20 61 6e 61 63 68 72 6f  s now an anachro
34434 6e 69 73 6d 2e 20 49 74 20 75 73 65 64 20 74 6f  nism. It used to
34435 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f   be used to reco
34436 76 65 72 20 66 72 6f 6d 20 61 0a 2a 2a 20 6d 61  ver from a.** ma
34437 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 2c 20  lloc() failure, 
34438 62 75 74 20 53 51 4c 69 74 65 20 6e 6f 77 20 64  but SQLite now d
34439 6f 65 73 20 74 68 69 73 20 61 75 74 6f 6d 61 74  oes this automat
3443a 69 63 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  ically..*/.SQLIT
3443b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3443c 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  3_global_recover
3443d 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  (void){.  return
3443e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
3443f 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ndif.#endif../*.
34440 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 77  ** Test to see w
34441 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
34442 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
34443 63 74 69 6f 6e 20 69 73 20 69 6e 20 61 75 74 6f  ction is in auto
34444 63 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f 64 65 2e 20  commit.** mode. 
34445 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
34446 69 74 20 69 73 20 61 6e 64 20 46 41 4c 53 45 20  it is and FALSE 
34447 69 66 20 6e 6f 74 2e 20 20 41 75 74 6f 63 6f 6d  if not.  Autocom
34448 6d 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 0a 2a  mit mode is on.*
34449 2a 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20 41  * by default.  A
3444a 75 74 6f 63 6f 6d 6d 69 74 20 69 73 20 64 69 73  utocommit is dis
3444b 61 62 6c 65 64 20 62 79 20 61 20 42 45 47 49 4e  abled by a BEGIN
3444c 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72   statement and r
3444d 65 65 6e 61 62 6c 65 64 0a 2a 2a 20 62 79 20 74  eenabled.** by t
3444e 68 65 20 6e 65 78 74 20 43 4f 4d 4d 49 54 20 6f  he next COMMIT o
3444f 72 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a  r ROLLBACK..**.*
34450 2a 2a 2a 2a 2a 2a 20 54 48 49 53 20 49 53 20 41  ****** THIS IS A
34451 4e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 41  N EXPERIMENTAL A
34452 50 49 20 41 4e 44 20 49 53 20 53 55 42 4a 45 43  PI AND IS SUBJEC
34453 54 20 54 4f 20 43 48 41 4e 47 45 20 2a 2a 2a 2a  T TO CHANGE ****
34454 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  **.*/.SQLITE_API
34455 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74   int sqlite3_get
34456 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73 71 6c 69  _autocommit(sqli
34457 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75  te3 *db){.  retu
34458 72 6e 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  rn db->autoCommi
34459 74 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  t;.}..#ifdef SQL
3445a 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
3445b 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
3445c 75 74 69 6e 65 20 69 73 20 73 75 62 74 69 74 75  utine is subtitu
3445d 74 65 64 20 66 6f 72 20 63 6f 6e 73 74 61 6e 74  ted for constant
3445e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
3445f 69 6e 0a 2a 2a 20 64 65 62 75 67 67 69 6e 67 20  in.** debugging 
34460 62 75 69 6c 64 73 2e 20 20 54 68 69 73 20 70 72  builds.  This pr
34461 6f 76 69 64 65 73 20 61 20 77 61 79 20 74 6f 20  ovides a way to 
34462 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74  set a breakpoint
34463 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 63 6f 72   for when.** cor
34464 72 75 70 74 69 6f 6e 20 69 73 20 66 69 72 73 74  ruption is first
34465 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f 0a 53 51   detected..*/.SQ
34466 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
34467 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28   sqlite3Corrupt(
34468 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20  void){.  return 
34469 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a  SQLITE_CORRUPT;.
3446a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
3446b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
3446c 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54  PRECATED./*.** T
3446d 68 69 73 20 69 73 20 61 20 63 6f 6e 76 65 6e 69  his is a conveni
3446e 65 6e 63 65 20 72 6f 75 74 69 6e 65 20 74 68 61  ence routine tha
3446f 74 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61  t makes sure tha
34470 74 20 61 6c 6c 20 74 68 72 65 61 64 2d 73 70 65  t all thread-spe
34471 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 20 66 6f  cific.** data fo
34472 72 20 74 68 69 73 20 74 68 72 65 61 64 20 68 61  r this thread ha
34473 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74  s been deallocat
34474 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  ed..**.** SQLite
34475 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20   no longer uses 
34476 74 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 20  thread-specific 
34477 64 61 74 61 20 73 6f 20 74 68 69 73 20 72 6f 75  data so this rou
34478 74 69 6e 65 20 69 73 20 6e 6f 77 20 61 0a 2a 2a  tine is now a.**
34479 20 6e 6f 2d 6f 70 2e 20 20 49 74 20 69 73 20 72   no-op.  It is r
3447a 65 74 61 69 6e 65 64 20 66 6f 72 20 68 69 73 74  etained for hist
3447b 6f 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69  orical compatibi
3447c 6c 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lity..*/.SQLITE_
3447d 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
3447e 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28  _thread_cleanup(
3447f 76 6f 69 64 29 7b 0a 7d 0a 23 65 6e 64 69 66 0a  void){.}.#endif.
34480 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6d 65  ./*.** Return me
34481 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ta information a
34482 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 20  bout a specific 
34483 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 64 61 74 61  column of a data
34484 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 53  base table..** S
34485 65 65 20 63 6f 6d 6d 65 6e 74 20 69 6e 20 73 71  ee comment in sq
34486 6c 69 74 65 33 2e 68 20 28 73 71 6c 69 74 65 2e  lite3.h (sqlite.
34487 68 2e 69 6e 29 20 66 6f 72 20 64 65 74 61 69 6c  h.in) for detail
34488 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  s..*/.#ifdef SQL
34489 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
3448a 4e 5f 4d 45 54 41 44 41 54 41 0a 53 51 4c 49 54  N_METADATA.SQLIT
3448b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3448c 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  3_table_column_m
3448d 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c 69 74  etadata(.  sqlit
3448e 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
3448f 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63         /* Connec
34490 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  tion handle */. 
34491 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
34492 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  Name,        /* 
34493 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72  Database name or
34494 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74   NULL */.  const
34495 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d   char *zTableNam
34496 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  e,     /* Table 
34497 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
34498 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d  char *zColumnNam
34499 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20  e,    /* Column 
3449a 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 63  name */.  char c
3449b 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70  onst **pzDataTyp
3449c 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a  e,    /* OUTPUT:
3449d 20 44 65 63 6c 61 72 65 64 20 64 61 74 61 20 74   Declared data t
3449e 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f  ype */.  char co
3449f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c  nst **pzCollSeq,
344a0 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20       /* OUTPUT: 
344a1 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  Collation sequen
344a2 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74  ce name */.  int
344a3 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20   *pNotNull,     
344a4 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50           /* OUTP
344a5 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f 54 20  UT: True if NOT 
344a6 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20  NULL constraint 
344a7 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20  exists */.  int 
344a8 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20 20 20  *pPrimaryKey,   
344a9 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
344aa 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  T: True if colum
344ab 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a  n part of PK */.
344ac 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20    int *pAutoinc 
344ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
344ae 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66   OUTPUT: True if
344af 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74 6f 2d   column is auto-
344b0 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a  increment */.){.
344b1 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72    int rc;.  char
344b2 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20   *zErrMsg = 0;. 
344b3 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30   Table *pTab = 0
344b4 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c  ;.  Column *pCol
344b5 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c   = 0;.  int iCol
344b6 3b 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  ;..  char const 
344b7 2a 7a 44 61 74 61 54 79 70 65 20 3d 20 30 3b 0a  *zDataType = 0;.
344b8 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43    char const *zC
344b9 6f 6c 6c 53 65 71 20 3d 20 30 3b 0a 20 20 69 6e  ollSeq = 0;.  in
344ba 74 20 6e 6f 74 6e 75 6c 6c 20 3d 20 30 3b 0a 20  t notnull = 0;. 
344bb 20 69 6e 74 20 70 72 69 6d 61 72 79 6b 65 79 20   int primarykey 
344bc 3d 20 30 3b 0a 20 20 69 6e 74 20 61 75 74 6f 69  = 0;.  int autoi
344bd 6e 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 45 6e  nc = 0;..  /* En
344be 73 75 72 65 20 74 68 65 20 64 61 74 61 62 61 73  sure the databas
344bf 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65 65  e schema has bee
344c0 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 20 20 73 71  n loaded */.  sq
344c1 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
344c2 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
344c3 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
344c4 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c  etyOn(db);.  sql
344c5 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
344c6 6c 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71  l(db);.  rc = sq
344c7 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26 7a  lite3Init(db, &z
344c8 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 53  ErrMsg);.  if( S
344c9 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a  QLITE_OK!=rc ){.
344ca 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
344cb 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f  ut;.  }..  /* Lo
344cc 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 69  cate the table i
344cd 6e 20 71 75 65 73 74 69 6f 6e 20 2a 2f 0a 20 20  n question */.  
344ce 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
344cf 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
344d0 6c 65 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29  leName, zDbName)
344d1 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c  ;.  if( !pTab ||
344d2 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
344d3 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a  {.    pTab = 0;.
344d4 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
344d5 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69  ut;.  }..  /* Fi
344d6 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f  nd the column fo
344d7 72 20 77 68 69 63 68 20 69 6e 66 6f 20 69 73 20  r which info is 
344d8 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 69  requested */.  i
344d9 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69  f( sqlite3IsRowi
344da 64 28 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29  d(zColumnName) )
344db 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61  {.    iCol = pTa
344dc 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 69 66  b->iPKey;.    if
344dd 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20  ( iCol>=0 ){.   
344de 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d     pCol = &pTab-
344df 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20  >aCol[iCol];.   
344e0 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
344e1 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c  for(iCol=0; iCol
344e2 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f  <pTab->nCol; iCo
344e3 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c  l++){.      pCol
344e4 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   = &pTab->aCol[i
344e5 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  Col];.      if( 
344e6 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
344e7 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a  p(pCol->zName, z
344e8 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29 7b 0a 20  ColumnName) ){. 
344e9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
344ea 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
344eb 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e  if( iCol==pTab->
344ec 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 70 54  nCol ){.      pT
344ed 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f  ab = 0;.      go
344ee 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  to error_out;.  
344ef 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68    }.  }..  /* Th
344f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
344f1 6b 20 73 74 6f 72 65 73 20 74 68 65 20 6d 65 74  k stores the met
344f2 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  a information th
344f3 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  at will be retur
344f4 6e 65 64 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20  ned.  ** to the 
344f5 63 61 6c 6c 65 72 20 69 6e 20 6c 6f 63 61 6c 20  caller in local 
344f6 76 61 72 69 61 62 6c 65 73 20 7a 44 61 74 61 54  variables zDataT
344f7 79 70 65 2c 20 7a 43 6f 6c 6c 53 65 71 2c 20 6e  ype, zCollSeq, n
344f8 6f 74 6e 75 6c 6c 2c 20 70 72 69 6d 61 72 79 6b  otnull, primaryk
344f9 65 79 0a 20 20 2a 2a 20 61 6e 64 20 61 75 74 6f  ey.  ** and auto
344fa 69 6e 63 2e 20 41 74 20 74 68 69 73 20 70 6f 69  inc. At this poi
344fb 6e 74 20 74 68 65 72 65 20 61 72 65 20 74 77 6f  nt there are two
344fc 20 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a   possibilities:.
344fd 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20 31    ** .  **     1
344fe 2e 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20  . The specified 
344ff 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77 61 73 20  column name was 
34500 72 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f 72  rowid", "oid" or
34501 20 22 5f 72 6f 77 69 64 5f 22 20 0a 20 20 2a 2a   "_rowid_" .  **
34502 20 20 20 20 20 20 20 20 61 6e 64 20 74 68 65 72          and ther
34503 65 20 69 73 20 6e 6f 20 65 78 70 6c 69 63 69 74  e is no explicit
34504 6c 79 20 64 65 63 6c 61 72 65 64 20 49 50 4b 20  ly declared IPK 
34505 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2a 0a 20 20  column. .  **.  
34506 2a 2a 20 20 20 20 20 32 2e 20 54 68 65 20 74 61  **     2. The ta
34507 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65  ble is not a vie
34508 77 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e  w and the column
34509 20 6e 61 6d 65 20 69 64 65 6e 74 69 66 69 65 64   name identified
3450a 20 61 6e 20 0a 20 20 2a 2a 20 20 20 20 20 20 20   an .  **       
3450b 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c   explicitly decl
3450c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 43 6f 70  ared column. Cop
3450d 79 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69  y meta informati
3450e 6f 6e 20 66 72 6f 6d 20 2a 70 43 6f 6c 2e 0a 20  on from *pCol.. 
3450f 20 2a 2f 20 0a 20 20 69 66 28 20 70 43 6f 6c 20   */ .  if( pCol 
34510 29 7b 0a 20 20 20 20 7a 44 61 74 61 54 79 70 65  ){.    zDataType
34511 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a   = pCol->zType;.
34512 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d 20 70      zCollSeq = p
34513 43 6f 6c 2d 3e 7a 43 6f 6c 6c 3b 0a 20 20 20 20  Col->zColl;.    
34514 6e 6f 74 6e 75 6c 6c 20 3d 20 70 43 6f 6c 2d 3e  notnull = pCol->
34515 6e 6f 74 4e 75 6c 6c 21 3d 30 3b 0a 20 20 20 20  notNull!=0;.    
34516 70 72 69 6d 61 72 79 6b 65 79 20 20 3d 20 70 43  primarykey  = pC
34517 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 21 3d 30  ol->isPrimKey!=0
34518 3b 0a 20 20 20 20 61 75 74 6f 69 6e 63 20 3d 20  ;.    autoinc = 
34519 70 54 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f  pTab->iPKey==iCo
3451a 6c 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62 46  l && (pTab->tabF
3451b 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e  lags & TF_Autoin
3451c 63 72 65 6d 65 6e 74 29 21 3d 30 3b 0a 20 20 7d  crement)!=0;.  }
3451d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 54  else{.    zDataT
3451e 79 70 65 20 3d 20 22 49 4e 54 45 47 45 52 22 3b  ype = "INTEGER";
3451f 0a 20 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20  .    primarykey 
34520 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  = 1;.  }.  if( !
34521 7a 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20  zCollSeq ){.    
34522 7a 43 6f 6c 6c 53 65 71 20 3d 20 22 42 49 4e 41  zCollSeq = "BINA
34523 52 59 22 3b 0a 20 20 7d 0a 0a 65 72 72 6f 72 5f  RY";.  }..error_
34524 6f 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 42 74  out:.  sqlite3Bt
34525 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
34526 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
34527 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a  SafetyOff(db);..
34528 20 20 2f 2a 20 57 68 65 74 68 65 72 20 74 68 65    /* Whether the
34529 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 73   function call s
3452a 75 63 63 65 65 64 65 64 20 6f 72 20 66 61 69 6c  ucceeded or fail
3452b 65 64 2c 20 73 65 74 20 74 68 65 20 6f 75 74 70  ed, set the outp
3452c 75 74 20 70 61 72 61 6d 65 74 65 72 73 0a 20 20  ut parameters.  
3452d 2a 2a 20 74 6f 20 77 68 61 74 65 76 65 72 20 74  ** to whatever t
3452e 68 65 69 72 20 6c 6f 63 61 6c 20 63 6f 75 6e 74  heir local count
3452f 65 72 70 61 72 74 73 20 63 6f 6e 74 61 69 6e 2e  erparts contain.
34530 20 49 66 20 61 6e 20 65 72 72 6f 72 20 64 69 64   If an error did
34531 20 6f 63 63 75 72 2c 0a 20 20 2a 2a 20 74 68 69   occur,.  ** thi
34532 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74  s has the effect
34533 20 6f 66 20 7a 65 72 6f 69 6e 67 20 61 6c 6c 20   of zeroing all 
34534 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72  output parameter
34535 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 7a  s..  */.  if( pz
34536 44 61 74 61 54 79 70 65 20 29 20 2a 70 7a 44 61  DataType ) *pzDa
34537 74 61 54 79 70 65 20 3d 20 7a 44 61 74 61 54 79  taType = zDataTy
34538 70 65 3b 0a 20 20 69 66 28 20 70 7a 43 6f 6c 6c  pe;.  if( pzColl
34539 53 65 71 20 29 20 2a 70 7a 43 6f 6c 6c 53 65 71  Seq ) *pzCollSeq
3453a 20 3d 20 7a 43 6f 6c 6c 53 65 71 3b 0a 20 20 69   = zCollSeq;.  i
3453b 66 28 20 70 4e 6f 74 4e 75 6c 6c 20 29 20 2a 70  f( pNotNull ) *p
3453c 4e 6f 74 4e 75 6c 6c 20 3d 20 6e 6f 74 6e 75 6c  NotNull = notnul
3453d 6c 3b 0a 20 20 69 66 28 20 70 50 72 69 6d 61 72  l;.  if( pPrimar
3453e 79 4b 65 79 20 29 20 2a 70 50 72 69 6d 61 72 79  yKey ) *pPrimary
3453f 4b 65 79 20 3d 20 70 72 69 6d 61 72 79 6b 65 79  Key = primarykey
34540 3b 0a 20 20 69 66 28 20 70 41 75 74 6f 69 6e 63  ;.  if( pAutoinc
34541 20 29 20 2a 70 41 75 74 6f 69 6e 63 20 3d 20 61   ) *pAutoinc = a
34542 75 74 6f 69 6e 63 3b 0a 0a 20 20 69 66 28 20 53  utoinc;..  if( S
34543 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20  QLITE_OK==rc && 
34544 21 70 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c  !pTab ){.    sql
34545 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
34546 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7a 45 72  ErrMsg);.    zEr
34547 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50  rMsg = sqlite3MP
34548 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75  rintf(db, "no su
34549 63 68 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 3a  ch table column:
3454a 20 25 73 2e 25 73 22 2c 20 7a 54 61 62 6c 65 4e   %s.%s", zTableN
3454b 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 7a 43 6f  ame,.        zCo
3454c 6c 75 6d 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 72  lumnName);.    r
3454d 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
3454e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
3454f 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 7a 45  rror(db, rc, (zE
34550 72 72 4d 73 67 3f 22 25 73 22 3a 30 29 2c 20 7a  rrMsg?"%s":0), z
34551 45 72 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74  ErrMsg);.  sqlit
34552 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
34553 72 4d 73 67 29 3b 0a 20 20 72 63 20 3d 20 73 71  rMsg);.  rc = sq
34554 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
34555 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   rc);.  sqlite3_
34556 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
34557 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
34558 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f   rc;.}.#endif../
34559 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61  *.** Sleep for a
3455a 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20   little while.  
3455b 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e  Return the amoun
3455c 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e  t of time slept.
3455d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3455e 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70  nt sqlite3_sleep
3455f 28 69 6e 74 20 6d 73 29 7b 0a 20 20 73 71 6c 69  (int ms){.  sqli
34560 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 0a 20  te3_vfs *pVfs;. 
34561 20 69 6e 74 20 72 63 3b 0a 20 20 70 56 66 73 20   int rc;.  pVfs 
34562 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69  = sqlite3_vfs_fi
34563 6e 64 28 30 29 3b 0a 20 20 69 66 28 20 70 56 66  nd(0);.  if( pVf
34564 73 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  s==0 ) return 0;
34565 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63  ..  /* This func
34566 74 69 6f 6e 20 77 6f 72 6b 73 20 69 6e 20 6d 69  tion works in mi
34567 6c 6c 69 73 65 63 6f 6e 64 73 2c 20 62 75 74 20  lliseconds, but 
34568 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 4f  the underlying O
34569 73 53 6c 65 65 70 28 29 20 0a 20 20 2a 2a 20 41  sSleep() .  ** A
3456a 50 49 20 75 73 65 73 20 6d 69 63 72 6f 73 65 63  PI uses microsec
3456b 6f 6e 64 73 2e 20 48 65 6e 63 65 20 74 68 65 20  onds. Hence the 
3456c 31 30 30 30 27 73 2e 0a 20 20 2a 2f 0a 20 20 72  1000's..  */.  r
3456d 63 20 3d 20 28 73 71 6c 69 74 65 33 4f 73 53 6c  c = (sqlite3OsSl
3456e 65 65 70 28 70 56 66 73 2c 20 31 30 30 30 2a 6d  eep(pVfs, 1000*m
3456f 73 29 2f 31 30 30 30 29 3b 0a 20 20 72 65 74 75  s)/1000);.  retu
34570 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
34571 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c  Enable or disabl
34572 65 20 74 68 65 20 65 78 74 65 6e 64 65 64 20 72  e the extended r
34573 65 73 75 6c 74 20 63 6f 64 65 73 2e 0a 2a 2f 0a  esult codes..*/.
34574 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
34575 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
34576 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c  result_codes(sql
34577 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e  ite3 *db, int on
34578 6f 66 66 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  off){.  sqlite3_
34579 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
3457a 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72  mutex);.  db->er
3457b 72 4d 61 73 6b 20 3d 20 6f 6e 6f 66 66 20 3f 20  rMask = onoff ? 
3457c 30 78 66 66 66 66 66 66 66 66 20 3a 20 30 78 66  0xffffffff : 0xf
3457d 66 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  f;.  sqlite3_mut
3457e 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
3457f 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
34580 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
34581 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69  * Invoke the xFi
34582 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64  leControl method
34583 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   on a particular
34584 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51   database..*/.SQ
34585 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
34586 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
34587 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  l(sqlite3 *db, c
34588 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61  onst char *zDbNa
34589 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  me, int op, void
3458a 20 2a 70 41 72 67 29 7b 0a 20 20 69 6e 74 20 72   *pArg){.  int r
3458b 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
3458c 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73  ;.  int iDb;.  s
3458d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3458e 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
3458f 20 69 66 28 20 7a 44 62 4e 61 6d 65 3d 3d 30 20   if( zDbName==0 
34590 29 7b 0a 20 20 20 20 69 44 62 20 3d 20 30 3b 0a  ){.    iDb = 0;.
34591 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72    }else{.    for
34592 28 69 44 62 3d 30 3b 20 69 44 62 3c 64 62 2d 3e  (iDb=0; iDb<db->
34593 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20 20 20  nDb; iDb++){.   
34594 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 64 62     if( strcmp(db
34595 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
34596 2c 20 7a 44 62 4e 61 6d 65 29 3d 3d 30 20 29 20  , zDbName)==0 ) 
34597 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
34598 0a 20 20 69 66 28 20 69 44 62 3c 64 62 2d 3e 6e  .  if( iDb<db->n
34599 44 62 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20  Db ){.    Btree 
3459a 2a 70 42 74 72 65 65 20 3d 20 64 62 2d 3e 61 44  *pBtree = db->aD
3459b 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20  b[iDb].pBt;.    
3459c 69 66 28 20 70 42 74 72 65 65 20 29 7b 0a 20 20  if( pBtree ){.  
3459d 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
3459e 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
3459f 5f 66 69 6c 65 20 2a 66 64 3b 0a 20 20 20 20 20  _file *fd;.     
345a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
345a1 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20  er(pBtree);.    
345a2 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74    pPager = sqlit
345a3 65 33 42 74 72 65 65 50 61 67 65 72 28 70 42 74  e3BtreePager(pBt
345a4 72 65 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ree);.      asse
345a5 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b  rt( pPager!=0 );
345a6 0a 20 20 20 20 20 20 66 64 20 3d 20 73 71 6c 69  .      fd = sqli
345a7 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 50 61  te3PagerFile(pPa
345a8 67 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ger);.      asse
345a9 72 74 28 20 66 64 21 3d 30 20 29 3b 0a 20 20 20  rt( fd!=0 );.   
345aa 20 20 20 69 66 28 20 66 64 2d 3e 70 4d 65 74 68     if( fd->pMeth
345ab 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ods ){.        r
345ac 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
345ad 65 43 6f 6e 74 72 6f 6c 28 66 64 2c 20 6f 70 2c  eControl(fd, op,
345ae 20 70 41 72 67 29 3b 0a 20 20 20 20 20 20 7d 0a   pArg);.      }.
345af 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
345b0 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b  eeLeave(pBtree);
345b1 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
345b2 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
345b3 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
345b4 65 74 75 72 6e 20 72 63 3b 20 20 20 0a 7d 0a 0a  eturn rc;   .}..
345b5 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 20  /*.** Interface 
345b6 74 6f 20 74 68 65 20 74 65 73 74 69 6e 67 20 6c  to the testing l
345b7 6f 67 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ogic..*/.SQLITE_
345b8 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
345b9 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74  test_control(int
345ba 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 69 6e 74   op, ...){.  int
345bb 20 72 63 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66   rc = 0;.#ifndef
345bc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
345bd 4c 54 49 4e 5f 54 45 53 54 0a 20 20 76 61 5f 6c  LTIN_TEST.  va_l
345be 69 73 74 20 61 70 3b 0a 20 20 76 61 5f 73 74 61  ist ap;.  va_sta
345bf 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77  rt(ap, op);.  sw
345c0 69 74 63 68 28 20 6f 70 20 29 7b 0a 0a 20 20 20  itch( op ){..   
345c1 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 61 76 65 20   /*.    ** Save 
345c2 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
345c3 65 20 6f 66 20 74 68 65 20 50 52 4e 47 2e 0a 20  e of the PRNG.. 
345c4 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
345c5 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50  QLITE_TESTCTRL_P
345c6 52 4e 47 5f 53 41 56 45 3a 20 7b 0a 20 20 20 20  RNG_SAVE: {.    
345c7 20 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76    sqlite3PrngSav
345c8 65 53 74 61 74 65 28 29 3b 0a 20 20 20 20 20 20  eState();.      
345c9 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
345ca 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52 65 73 74    /*.    ** Rest
345cb 6f 72 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  ore the state of
345cc 20 74 68 65 20 50 52 4e 47 20 74 6f 20 74 68 65   the PRNG to the
345cd 20 6c 61 73 74 20 73 74 61 74 65 20 73 61 76 65   last state save
345ce 64 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 50  d using.    ** P
345cf 52 4e 47 5f 53 41 56 45 2e 20 20 49 66 20 50 52  RNG_SAVE.  If PR
345d0 4e 47 5f 53 41 56 45 20 68 61 73 20 6e 65 76 65  NG_SAVE has neve
345d1 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 63 61  r before been ca
345d2 6c 6c 65 64 2c 20 74 68 65 6e 0a 20 20 20 20 2a  lled, then.    *
345d3 2a 20 74 68 69 73 20 76 65 72 62 20 61 63 74 73  * this verb acts
345d4 20 6c 69 6b 65 20 50 52 4e 47 5f 52 45 53 45 54   like PRNG_RESET
345d5 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
345d6 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
345d7 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45 3a 20  L_PRNG_RESTORE: 
345d8 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  {.      sqlite3P
345d9 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28  rngRestoreState(
345da 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
345db 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20      }..    /*.  
345dc 20 20 2a 2a 20 52 65 73 65 74 20 74 68 65 20 50    ** Reset the P
345dd 52 4e 47 20 62 61 63 6b 20 74 6f 20 69 74 73 20  RNG back to its 
345de 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 74  uninitialized st
345df 61 74 65 2e 20 20 54 68 65 20 6e 65 78 74 20 63  ate.  The next c
345e0 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71  all.    ** to sq
345e1 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
345e2 28 29 20 77 69 6c 6c 20 72 65 73 65 65 64 20 74  () will reseed t
345e3 68 65 20 50 52 4e 47 20 75 73 69 6e 67 20 61 20  he PRNG using a 
345e4 73 69 6e 67 6c 65 20 63 61 6c 6c 0a 20 20 20 20  single call.    
345e5 2a 2a 20 74 6f 20 74 68 65 20 78 52 61 6e 64 6f  ** to the xRando
345e6 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20  mness method of 
345e7 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 2e  the default VFS.
345e8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
345e9 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
345ea 5f 50 52 4e 47 5f 52 45 53 45 54 3a 20 7b 0a 20  _PRNG_RESET: {. 
345eb 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67       sqlite3Prng
345ec 52 65 73 65 74 53 74 61 74 65 28 29 3b 0a 20 20  ResetState();.  
345ed 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
345ee 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
345ef 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
345f0 6e 74 72 6f 6c 28 42 49 54 56 45 43 5f 54 45 53  ntrol(BITVEC_TES
345f1 54 2c 20 73 69 7a 65 2c 20 70 72 6f 67 72 61 6d  T, size, program
345f2 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ).    **.    ** 
345f3 52 75 6e 20 61 20 74 65 73 74 20 61 67 61 69 6e  Run a test again
345f4 73 74 20 61 20 42 69 74 76 65 63 20 6f 62 6a 65  st a Bitvec obje
345f5 63 74 20 6f 66 20 73 69 7a 65 2e 20 20 54 68 65  ct of size.  The
345f6 20 70 72 6f 67 72 61 6d 20 61 72 67 75 6d 65 6e   program argumen
345f7 74 0a 20 20 20 20 2a 2a 20 69 73 20 61 6e 20 61  t.    ** is an a
345f8 72 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73  rray of integers
345f9 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
345fa 65 20 74 65 73 74 2e 20 20 52 65 74 75 72 6e 20  e test.  Return 
345fb 2d 31 20 6f 6e 20 61 0a 20 20 20 20 2a 2a 20 6d  -1 on a.    ** m
345fc 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
345fd 20 65 72 72 6f 72 2c 20 30 20 6f 6e 20 73 75 63   error, 0 on suc
345fe 63 65 73 73 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72  cess, or non-zer
345ff 6f 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 2e 0a  o for an error..
34600 20 20 20 20 2a 2a 20 53 65 65 20 74 68 65 20 73      ** See the s
34601 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c  qlite3BitvecBuil
34602 74 69 6e 54 65 73 74 28 29 20 66 6f 72 20 61 64  tinTest() for ad
34603 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
34604 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
34605 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
34606 54 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53  TCTRL_BITVEC_TES
34607 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  T: {.      int s
34608 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  z = va_arg(ap, i
34609 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 2a  nt);.      int *
3460a 61 50 72 6f 67 20 3d 20 76 61 5f 61 72 67 28 61  aProg = va_arg(a
3460b 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 20 20  p, int*);.      
3460c 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  rc = sqlite3Bitv
3460d 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28 73 7a  ecBuiltinTest(sz
3460e 2c 20 61 50 72 6f 67 29 3b 0a 20 20 20 20 20 20  , aProg);.      
3460f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
34610 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c    /*.    **  sql
34611 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
34612 6c 28 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f  l(BENIGN_MALLOC_
34613 48 4f 4f 4b 53 2c 20 78 42 65 67 69 6e 2c 20 78  HOOKS, xBegin, x
34614 45 6e 64 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  End).    **.    
34615 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b  ** Register hook
34616 73 20 74 6f 20 63 61 6c 6c 20 74 6f 20 69 6e 64  s to call to ind
34617 69 63 61 74 65 20 77 68 69 63 68 20 6d 61 6c 6c  icate which mall
34618 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 0a 20  oc() failures . 
34619 20 20 20 2a 2a 20 61 72 65 20 62 65 6e 69 67 6e     ** are benign
3461a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
3461b 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
3461c 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f  L_BENIGN_MALLOC_
3461d 48 4f 4f 4b 53 3a 20 7b 0a 20 20 20 20 20 20 74  HOOKS: {.      t
3461e 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a 76 6f  ypedef void (*vo
3461f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 28 76 6f 69  id_function)(voi
34620 64 29 3b 0a 20 20 20 20 20 20 76 6f 69 64 5f 66  d);.      void_f
34621 75 6e 63 74 69 6f 6e 20 78 42 65 6e 69 67 6e 42  unction xBenignB
34622 65 67 69 6e 3b 0a 20 20 20 20 20 20 76 6f 69 64  egin;.      void
34623 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e 69 67  _function xBenig
34624 6e 45 6e 64 3b 0a 20 20 20 20 20 20 78 42 65 6e  nEnd;.      xBen
34625 69 67 6e 42 65 67 69 6e 20 3d 20 76 61 5f 61 72  ignBegin = va_ar
34626 67 28 61 70 2c 20 76 6f 69 64 5f 66 75 6e 63 74  g(ap, void_funct
34627 69 6f 6e 29 3b 0a 20 20 20 20 20 20 78 42 65 6e  ion);.      xBen
34628 69 67 6e 45 6e 64 20 3d 20 76 61 5f 61 72 67 28  ignEnd = va_arg(
34629 61 70 2c 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f  ap, void_functio
3462a 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
3462b 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f  3BenignMallocHoo
3462c 6b 73 28 78 42 65 6e 69 67 6e 42 65 67 69 6e 2c  ks(xBenignBegin,
3462d 20 78 42 65 6e 69 67 6e 45 6e 64 29 3b 0a 20 20   xBenignEnd);.  
3462e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
3462f 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
34630 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
34631 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53  ntrol(SQLITE_TES
34632 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59  TCTRL_PENDING_BY
34633 54 45 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  TE, unsigned int
34634 20 58 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a   X).    **.    *
34635 2a 20 53 65 74 20 74 68 65 20 50 45 4e 44 49 4e  * Set the PENDIN
34636 47 20 62 79 74 65 20 74 6f 20 74 68 65 20 76 61  G byte to the va
34637 6c 75 65 20 69 6e 20 74 68 65 20 61 72 67 75 6d  lue in the argum
34638 65 6e 74 2c 20 69 66 20 58 3e 30 2e 0a 20 20 20  ent, if X>0..   
34639 20 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e   ** Make no chan
3463a 67 65 73 20 69 66 20 58 3d 3d 30 2e 20 20 52 65  ges if X==0.  Re
3463b 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  turn the value o
3463c 66 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  f the pending by
3463d 74 65 0a 20 20 20 20 2a 2a 20 61 73 20 69 74 20  te.    ** as it 
3463e 65 78 69 73 74 69 6e 67 20 62 65 66 6f 72 65 20  existing before 
3463f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73  this routine was
34640 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 0a   called..    **.
34641 20 20 20 20 2a 2a 20 49 4d 50 4f 52 54 41 4e 54      ** IMPORTANT
34642 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20  :  Changing the 
34643 50 45 4e 44 49 4e 47 20 62 79 74 65 20 66 72 6f  PENDING byte fro
34644 6d 20 30 78 34 30 30 30 30 30 30 30 20 72 65 73  m 0x40000000 res
34645 75 6c 74 73 20 69 6e 0a 20 20 20 20 2a 2a 20 61  ults in.    ** a
34646 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 64  n incompatible d
34647 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72  atabase file for
34648 6d 61 74 2e 20 20 43 68 61 6e 67 69 6e 67 20 74  mat.  Changing t
34649 68 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65 0a  he PENDING byte.
3464a 20 20 20 20 2a 2a 20 77 68 69 6c 65 20 61 6e 79      ** while any
3464b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3464c 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 72 65 73  tion is open res
3464d 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65  ults in undefine
3464e 64 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 69 6c  d and.    ** dil
3464f 65 74 65 72 69 6f 75 73 20 62 65 68 61 76 69 6f  eterious behavio
34650 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  r..    */.    ca
34651 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  se SQLITE_TESTCT
34652 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45 3a  RL_PENDING_BYTE:
34653 20 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65   {.      unsigne
34654 64 20 69 6e 74 20 6e 65 77 56 61 6c 20 3d 20 76  d int newVal = v
34655 61 5f 61 72 67 28 61 70 2c 20 75 6e 73 69 67 6e  a_arg(ap, unsign
34656 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72  ed int);.      r
34657 63 20 3d 20 73 71 6c 69 74 65 33 50 65 6e 64 69  c = sqlite3Pendi
34658 6e 67 42 79 74 65 3b 0a 20 20 20 20 20 20 69 66  ngByte;.      if
34659 28 20 6e 65 77 56 61 6c 20 29 20 73 71 6c 69 74  ( newVal ) sqlit
3465a 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d 20  e3PendingByte = 
3465b 6e 65 77 56 61 6c 3b 0a 20 20 20 20 20 20 62 72  newVal;.      br
3465c 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
3465d 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74  /*.    **  sqlit
3465e 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
3465f 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
34660 41 53 53 45 52 54 2c 20 69 6e 74 20 58 29 0a 20  ASSERT, int X). 
34661 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69     **.    ** Thi
34662 73 20 61 63 74 69 6f 6e 20 70 72 6f 76 69 64 65  s action provide
34663 73 20 61 20 72 75 6e 2d 74 69 6d 65 20 74 65 73  s a run-time tes
34664 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72  t to see whether
34665 20 6f 72 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 61   or not.    ** a
34666 73 73 65 72 74 28 29 20 77 61 73 20 65 6e 61 62  ssert() was enab
34667 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  led at compile-t
34668 69 6d 65 2e 20 20 49 66 20 58 20 69 73 20 74 72  ime.  If X is tr
34669 75 65 20 61 6e 64 20 61 73 73 65 72 74 28 29 0a  ue and assert().
3466a 20 20 20 20 2a 2a 20 69 73 20 65 6e 61 62 6c 65      ** is enable
3466b 64 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74 75  d, then the retu
3466c 72 6e 20 76 61 6c 75 65 20 69 73 20 74 72 75 65  rn value is true
3466d 2e 20 20 49 66 20 58 20 69 73 20 74 72 75 65 20  .  If X is true 
3466e 61 6e 64 0a 20 20 20 20 2a 2a 20 61 73 73 65 72  and.    ** asser
3466f 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 64 2c  t() is disabled,
34670 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
34671 20 76 61 6c 75 65 20 69 73 20 7a 65 72 6f 2e 20   value is zero. 
34672 20 49 66 20 58 20 69 73 0a 20 20 20 20 2a 2a 20   If X is.    ** 
34673 66 61 6c 73 65 20 61 6e 64 20 61 73 73 65 72 74  false and assert
34674 28 29 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74  () is enabled, t
34675 68 65 6e 20 74 68 65 20 61 73 73 65 72 74 69 6f  hen the assertio
34676 6e 20 66 69 72 65 73 20 61 6e 64 20 74 68 65 0a  n fires and the.
34677 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61      ** process a
34678 62 6f 72 74 73 2e 20 20 49 66 20 58 20 69 73 20  borts.  If X is 
34679 66 61 6c 73 65 20 61 6e 64 20 61 73 73 65 72 74  false and assert
3467a 28 29 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20  () is disabled, 
3467b 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  then the.    ** 
3467c 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
3467d 7a 65 72 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  zero..    */.   
3467e 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
3467f 54 43 54 52 4c 5f 41 53 53 45 52 54 3a 20 7b 0a  TCTRL_ASSERT: {.
34680 20 20 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69        volatile i
34681 6e 74 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20  nt x = 0;.      
34682 61 73 73 65 72 74 28 20 28 78 20 3d 20 76 61 5f  assert( (x = va_
34683 61 72 67 28 61 70 2c 69 6e 74 29 29 21 3d 30 20  arg(ap,int))!=0 
34684 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 78 3b  );.      rc = x;
34685 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
34686 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20    }...    /*.   
34687 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73   **  sqlite3_tes
34688 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45  t_control(SQLITE
34689 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53  _TESTCTRL_ALWAYS
3468a 2c 20 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a  , int X).    **.
3468b 20 20 20 20 2a 2a 20 54 68 69 73 20 61 63 74 69      ** This acti
3468c 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 72 75  on provides a ru
3468d 6e 2d 74 69 6d 65 20 74 65 73 74 20 74 6f 20 73  n-time test to s
3468e 65 65 20 68 6f 77 20 74 68 65 20 41 4c 57 41 59  ee how the ALWAY
3468f 53 20 61 6e 64 0a 20 20 20 20 2a 2a 20 4e 45 56  S and.    ** NEV
34690 45 52 20 6d 61 63 72 6f 73 20 77 65 72 65 20 64  ER macros were d
34691 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c  efined at compil
34692 65 2d 74 69 6d 65 2e 0a 20 20 20 20 2a 2a 0a 20  e-time..    **. 
34693 20 20 20 2a 2a 20 54 68 65 20 72 65 74 75 72 6e     ** The return
34694 20 76 61 6c 75 65 20 69 73 20 41 4c 57 41 59 53   value is ALWAYS
34695 28 58 29 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20  (X).  .    **.  
34696 20 20 2a 2a 20 54 68 65 20 72 65 63 6f 6d 6d 65    ** The recomme
34697 6e 64 65 64 20 74 65 73 74 20 69 73 20 58 3d 3d  nded test is X==
34698 32 2e 20 20 49 66 20 74 68 65 20 72 65 74 75 72  2.  If the retur
34699 6e 20 76 61 6c 75 65 20 69 73 20 32 2c 20 74 68  n value is 2, th
3469a 61 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20  at means.    ** 
3469b 41 4c 57 41 59 53 28 29 20 61 6e 64 20 4e 45 56  ALWAYS() and NEV
3469c 45 52 28 29 20 61 72 65 20 62 6f 74 68 20 6e 6f  ER() are both no
3469d 2d 6f 70 20 70 61 73 73 2d 74 68 72 6f 75 67 68  -op pass-through
3469e 20 6d 61 63 72 6f 73 2c 20 77 68 69 63 68 20 69   macros, which i
3469f 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 65 66  s the.    ** def
346a0 61 75 6c 74 20 73 65 74 74 69 6e 67 2e 20 20 49  ault setting.  I
346a1 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  f the return val
346a2 75 65 20 69 73 20 31 2c 20 74 68 65 6e 20 41 4c  ue is 1, then AL
346a3 57 41 59 53 28 29 20 69 73 20 65 69 74 68 65 72  WAYS() is either
346a4 0a 20 20 20 20 2a 2a 20 68 61 72 64 2d 63 6f 64  .    ** hard-cod
346a5 65 64 20 74 6f 20 74 72 75 65 20 6f 72 20 65 6c  ed to true or el
346a6 73 65 20 69 74 20 61 73 73 65 72 74 73 20 69 66  se it asserts if
346a7 20 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69 73   its argument is
346a8 20 66 61 6c 73 65 2e 0a 20 20 20 20 2a 2a 20 54   false..    ** T
346a9 68 65 20 66 69 72 73 74 20 62 65 68 61 76 69 6f  he first behavio
346aa 72 20 28 68 61 72 64 2d 63 6f 64 65 64 20 74 6f  r (hard-coded to
346ab 20 74 72 75 65 29 20 69 73 20 74 68 65 20 63 61   true) is the ca
346ac 73 65 20 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c  se if.    ** SQL
346ad 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53  ITE_TESTCTRL_ASS
346ae 45 52 54 20 73 68 6f 77 73 20 74 68 61 74 20 61  ERT shows that a
346af 73 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62  ssert() is disab
346b0 6c 65 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f  led and the seco
346b1 6e 64 0a 20 20 20 20 2a 2a 20 62 65 68 61 76 69  nd.    ** behavi
346b2 6f 72 20 28 61 73 73 65 72 74 20 69 66 20 74 68  or (assert if th
346b3 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c  e argument to AL
346b4 57 41 59 53 28 29 20 69 73 20 66 61 6c 73 65 29  WAYS() is false)
346b5 20 69 73 20 74 68 65 20 63 61 73 65 20 69 66 0a   is the case if.
346b6 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 54 45      ** SQLITE_TE
346b7 53 54 43 54 52 4c 5f 41 53 53 45 52 54 20 73 68  STCTRL_ASSERT sh
346b8 6f 77 73 20 74 68 61 74 20 61 73 73 65 72 74 28  ows that assert(
346b9 29 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20  ) is enabled..  
346ba 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
346bb 72 75 6e 2d 74 69 6d 65 20 74 65 73 74 20 70 72  run-time test pr
346bc 6f 63 65 64 75 72 65 20 6d 69 67 68 74 20 6c 6f  ocedure might lo
346bd 6f 6b 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b  ok something lik
346be 65 20 74 68 69 73 3a 0a 20 20 20 20 2a 2a 0a 20  e this:.    **. 
346bf 20 20 20 2a 2a 20 20 20 20 69 66 28 20 73 71 6c     **    if( sql
346c0 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
346c1 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  l(SQLITE_TESTCTR
346c2 4c 5f 41 4c 57 41 59 53 2c 20 32 29 3d 3d 32 20  L_ALWAYS, 2)==2 
346c3 29 7b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f  ){.    **      /
346c4 2f 20 41 4c 57 41 59 53 28 29 20 61 6e 64 20 4e  / ALWAYS() and N
346c5 45 56 45 52 28 29 20 61 72 65 20 6e 6f 2d 6f 70  EVER() are no-op
346c6 20 70 61 73 73 2d 74 68 72 6f 75 67 68 20 6d 61   pass-through ma
346c7 63 72 6f 73 0a 20 20 20 20 2a 2a 20 20 20 20 7d  cros.    **    }
346c8 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
346c9 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51  _test_control(SQ
346ca 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53  LITE_TESTCTRL_AS
346cb 53 45 52 54 2c 20 31 29 20 29 7b 0a 20 20 20 20  SERT, 1) ){.    
346cc 2a 2a 20 20 20 20 20 20 2f 2f 20 41 4c 57 41 59  **      // ALWAY
346cd 53 28 78 29 20 61 73 73 65 72 74 73 20 74 68 61  S(x) asserts tha
346ce 74 20 78 20 69 73 20 74 72 75 65 2e 20 4e 45 56  t x is true. NEV
346cf 45 52 28 78 29 20 61 73 73 65 72 74 73 20 78 20  ER(x) asserts x 
346d0 69 73 20 66 61 6c 73 65 2e 0a 20 20 20 20 2a 2a  is false..    **
346d1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a      }else{.    *
346d2 2a 20 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53  *      // ALWAYS
346d3 28 78 29 20 69 73 20 61 20 63 6f 6e 73 74 61 6e  (x) is a constan
346d4 74 20 31 2e 20 20 4e 45 56 45 52 28 78 29 20 69  t 1.  NEVER(x) i
346d5 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 30 2e 0a  s a constant 0..
346d6 20 20 20 20 2a 2a 20 20 20 20 7d 0a 20 20 20 20      **    }.    
346d7 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
346d8 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41  TE_TESTCTRL_ALWA
346d9 59 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  YS: {.      int 
346da 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e  x = va_arg(ap,in
346db 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 41  t);.      rc = A
346dc 4c 57 41 59 53 28 78 29 3b 0a 20 20 20 20 20 20  LWAYS(x);.      
346dd 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
346de 20 20 2f 2a 20 20 20 73 71 6c 69 74 65 33 5f 74    /*   sqlite3_t
346df 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49  est_control(SQLI
346e0 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53 45  TE_TESTCTRL_RESE
346e1 52 56 45 2c 20 73 71 6c 69 74 65 33 20 2a 64 62  RVE, sqlite3 *db
346e2 2c 20 69 6e 74 20 4e 29 0a 20 20 20 20 2a 2a 0a  , int N).    **.
346e3 20 20 20 20 2a 2a 20 53 65 74 20 74 68 65 20 6e      ** Set the n
346e4 52 65 73 65 72 76 65 20 73 69 7a 65 20 74 6f 20  Reserve size to 
346e5 4e 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  N for the main d
346e6 61 74 61 62 61 73 65 20 6f 6e 20 74 68 65 20 64  atabase on the d
346e7 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 63  atabase.    ** c
346e8 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 20 20  onnection db..  
346e9 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51    */.    case SQ
346ea 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45  LITE_TESTCTRL_RE
346eb 53 45 52 56 45 3a 20 7b 0a 20 20 20 20 20 20 73  SERVE: {.      s
346ec 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 61 5f  qlite3 *db = va_
346ed 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 2a  arg(ap, sqlite3*
346ee 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 78 20 3d  );.      int x =
346ef 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b   va_arg(ap,int);
346f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
346f1 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
346f2 75 74 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c  utex);.      sql
346f3 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65  ite3BtreeSetPage
346f4 53 69 7a 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e  Size(db->aDb[0].
346f5 70 42 74 2c 20 30 2c 20 78 2c 20 30 29 3b 0a 20  pBt, 0, x, 0);. 
346f6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
346f7 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
346f8 65 78 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ex);.      break
346f9 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 0a 20 20 76  ;.    }..  }.  v
346fa 61 5f 65 6e 64 28 61 70 29 3b 0a 23 65 6e 64 69  a_end(ap);.#endi
346fb 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
346fc 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f  _BUILTIN_TEST */
346fd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
346fe 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
346ff 20 45 6e 64 20 6f 66 20 6d 61 69 6e 2e 63 20 2a   End of main.c *
34700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34701 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34702 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
34703 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
34704 20 42 65 67 69 6e 20 66 69 6c 65 20 6e 6f 74 69   Begin file noti
34705 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  fy.c ***********
34706 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34707 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
34708 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4d 61 72 63  ./*.** 2009 Marc
34709 68 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  h 3.**.** The au
3470a 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
3470b 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
3470c 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
3470d 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
3470e 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
3470f 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
34710 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
34711 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
34712 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
34713 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
34714 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
34715 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
34716 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
34717 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
34718 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
34719 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
3471a 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
3471b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3471c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3471d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3471e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3471f 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
34720 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
34721 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
34722 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 75 6e  f the sqlite3_un
34723 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2a  lock_notify().**
34724 20 41 50 49 20 6d 65 74 68 6f 64 20 61 6e 64 20   API method and 
34725 69 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 66  its associated f
34726 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2f  unctionality..*/
34727 0a 0a 2f 2a 20 4f 6d 69 74 20 74 68 69 73 20 65  ../* Omit this e
34728 6e 74 69 72 65 20 66 69 6c 65 20 69 66 20 53 51  ntire file if SQ
34729 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f  LITE_ENABLE_UNLO
3472a 43 4b 5f 4e 4f 54 49 46 59 20 69 73 20 6e 6f 74  CK_NOTIFY is not
3472b 20 64 65 66 69 6e 65 64 2e 20 2a 2f 0a 23 69 66   defined. */.#if
3472c 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
3472d 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a  E_UNLOCK_NOTIFY.
3472e 0a 2f 2a 0a 2a 2a 20 50 75 62 6c 69 63 20 69 6e  ./*.** Public in
3472f 74 65 72 66 61 63 65 73 3a 0a 2a 2a 0a 2a 2a 20  terfaces:.**.** 
34730 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74    sqlite3Connect
34731 69 6f 6e 42 6c 6f 63 6b 65 64 28 29 0a 2a 2a 20  ionBlocked().** 
34732 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74    sqlite3Connect
34733 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 0a 2a 2a  ionUnlocked().**
34734 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63     sqlite3Connec
34735 74 69 6f 6e 43 6c 6f 73 65 64 28 29 0a 2a 2a 20  tionClosed().** 
34736 20 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b    sqlite3_unlock
34737 5f 6e 6f 74 69 66 79 28 29 0a 2a 2f 0a 0a 23 64  _notify().*/..#d
34738 65 66 69 6e 65 20 61 73 73 65 72 74 4d 75 74 65  efine assertMute
34739 78 48 65 6c 64 28 29 20 5c 0a 20 20 61 73 73 65  xHeld() \.  asse
3473a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
3473b 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75  x_held(sqlite3Mu
3473c 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
3473d 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
3473e 54 45 52 29 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 48  TER)) )../*.** H
3473f 65 61 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20  ead of a linked 
34740 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 71 6c 69  list of all sqli
34741 74 65 33 20 6f 62 6a 65 63 74 73 20 63 72 65 61  te3 objects crea
34742 74 65 64 20 62 79 20 74 68 69 73 20 70 72 6f 63  ted by this proc
34743 65 73 73 0a 2a 2a 20 66 6f 72 20 77 68 69 63 68  ess.** for which
34744 20 65 69 74 68 65 72 20 73 71 6c 69 74 65 33 2e   either sqlite3.
34745 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
34746 69 6f 6e 20 6f 72 20 73 71 6c 69 74 65 33 2e 70  ion or sqlite3.p
34747 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
34748 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e  .** is not NULL.
34749 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6d   This variable m
3474a 61 79 20 6f 6e 6c 79 20 61 63 63 65 73 73 65 64  ay only accessed
3474b 20 77 68 69 6c 65 20 74 68 65 20 53 54 41 54 49   while the STATI
3474c 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75 74 65  C_MASTER.** mute
3474d 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74  x is held..*/.st
3474e 61 74 69 63 20 73 71 6c 69 74 65 33 20 2a 53 51  atic sqlite3 *SQ
3474f 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33  LITE_WSD sqlite3
34750 42 6c 6f 63 6b 65 64 4c 69 73 74 20 3d 20 30 3b  BlockedList = 0;
34751 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
34752 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
34753 74 69 6f 6e 20 69 73 20 61 20 63 6f 6d 70 6c 65  tion is a comple
34754 78 20 61 73 73 65 72 74 28 29 20 74 68 61 74 20  x assert() that 
34755 76 65 72 69 66 69 65 73 20 74 68 65 20 66 6f 6c  verifies the fol
34756 6c 6f 77 69 6e 67 20 0a 2a 2a 20 70 72 6f 70 65  lowing .** prope
34757 72 74 69 65 73 20 6f 66 20 74 68 65 20 62 6c 6f  rties of the blo
34758 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
34759 20 6c 69 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 31   list:.**.**   1
3475a 29 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20  ) Each entry in 
3475b 74 68 65 20 6c 69 73 74 20 68 61 73 20 61 20 6e  the list has a n
3475c 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 20 66 6f  on-NULL value fo
3475d 72 20 65 69 74 68 65 72 20 0a 2a 2a 20 20 20 20  r either .**    
3475e 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74    pUnlockConnect
3475f 69 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b 69 6e 67  ion or pBlocking
34760 43 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 72 20 62  Connection, or b
34761 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20  oth..**.**   2) 
34762 41 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74  All entries in t
34763 68 65 20 6c 69 73 74 20 74 68 61 74 20 73 68 61  he list that sha
34764 72 65 20 61 20 63 6f 6d 6d 6f 6e 20 76 61 6c 75  re a common valu
34765 65 20 66 6f 72 20 0a 2a 2a 20 20 20 20 20 20 78  e for .**      x
34766 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 61 72 65  UnlockNotify are
34767 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65   grouped togethe
34768 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66  r..**.**   3) If
34769 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 64 62   the argument db
3476a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
3476b 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65  en none of the e
3476c 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a  ntries in the.**
3476d 20 20 20 20 20 20 62 6c 6f 63 6b 65 64 20 63 6f        blocked co
3476e 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 20 68  nnections list h
3476f 61 76 65 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65  ave pUnlockConne
34770 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b 69  ction or pBlocki
34771 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  ngConnection.** 
34772 20 20 20 20 20 73 65 74 20 74 6f 20 64 62 2e 20       set to db. 
34773 54 68 69 73 20 69 73 20 75 73 65 64 20 77 68 65  This is used whe
34774 6e 20 63 6c 6f 73 69 6e 67 20 63 6f 6e 6e 65 63  n closing connec
34775 74 69 6f 6e 20 64 62 2e 0a 2a 2f 0a 73 74 61 74  tion db..*/.stat
34776 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73  ic void checkLis
34777 74 50 72 6f 70 65 72 74 69 65 73 28 73 71 6c 69  tProperties(sqli
34778 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69  te3 *db){.  sqli
34779 74 65 33 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d  te3 *p;.  for(p=
3477a 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69  sqlite3BlockedLi
3477b 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  st; p; p=p->pNex
3477c 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69  tBlocked){.    i
3477d 6e 74 20 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20  nt seen = 0;.   
3477e 20 73 71 6c 69 74 65 33 20 2a 70 32 3b 0a 0a 20   sqlite3 *p2;.. 
3477f 20 20 20 2f 2a 20 56 65 72 69 66 79 20 70 72 6f     /* Verify pro
34780 70 65 72 74 79 20 28 31 29 20 2a 2f 0a 20 20 20  perty (1) */.   
34781 20 61 73 73 65 72 74 28 20 70 2d 3e 70 55 6e 6c   assert( p->pUnl
34782 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 7c 7c  ockConnection ||
34783 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e   p->pBlockingCon
34784 6e 65 63 74 69 6f 6e 20 29 3b 0a 0a 20 20 20 20  nection );..    
34785 2f 2a 20 56 65 72 69 66 79 20 70 72 6f 70 65 72  /* Verify proper
34786 74 79 20 28 32 29 20 2a 2f 0a 20 20 20 20 66 6f  ty (2) */.    fo
34787 72 28 70 32 3d 73 71 6c 69 74 65 33 42 6c 6f 63  r(p2=sqlite3Bloc
34788 6b 65 64 4c 69 73 74 3b 20 70 32 21 3d 70 3b 20  kedList; p2!=p; 
34789 70 32 3d 70 32 2d 3e 70 4e 65 78 74 42 6c 6f 63  p2=p2->pNextBloc
3478a 6b 65 64 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ked){.      if( 
3478b 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  p2->xUnlockNotif
3478c 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  y==p->xUnlockNot
3478d 69 66 79 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a  ify ) seen = 1;.
3478e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 32        assert( p2
3478f 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3d  ->xUnlockNotify=
34790 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  =p->xUnlockNotif
34791 79 20 7c 7c 20 21 73 65 65 6e 20 29 3b 0a 20 20  y || !seen );.  
34792 20 20 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d      assert( db==
34793 30 20 7c 7c 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43  0 || p->pUnlockC
34794 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64 62 20 29 3b  onnection!=db );
34795 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
34796 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63  b==0 || p->pBloc
34797 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d  kingConnection!=
34798 64 62 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  db );.    }.  }.
34799 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
3479a 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72   checkListProper
3479b 74 69 65 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a  ties(x).#endif..
3479c 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 63 6f 6e  /*.** Remove con
3479d 6e 65 63 74 69 6f 6e 20 64 62 20 66 72 6f 6d 20  nection db from 
3479e 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  the blocked conn
3479f 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 49 66  ections list. If
347a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 64   connection.** d
347a1 62 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  b is not current
347a2 6c 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65  ly a part of the
347a3 20 6c 69 73 74 2c 20 74 68 69 73 20 66 75 6e 63   list, this func
347a4 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
347a5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
347a6 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65  removeFromBlocke
347a7 64 4c 69 73 74 28 73 71 6c 69 74 65 33 20 2a 64  dList(sqlite3 *d
347a8 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  b){.  sqlite3 **
347a9 70 70 3b 0a 20 20 61 73 73 65 72 74 4d 75 74 65  pp;.  assertMute
347aa 78 48 65 6c 64 28 29 3b 0a 20 20 66 6f 72 28 70  xHeld();.  for(p
347ab 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65  p=&sqlite3Blocke
347ac 64 4c 69 73 74 3b 20 2a 70 70 3b 20 70 70 20 3d  dList; *pp; pp =
347ad 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c   &(*pp)->pNextBl
347ae 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69 66 28 20  ocked){.    if( 
347af 2a 70 70 3d 3d 64 62 20 29 7b 0a 20 20 20 20 20  *pp==db ){.     
347b0 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e   *pp = (*pp)->pN
347b1 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20  extBlocked;.    
347b2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
347b3 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
347b4 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 74 6f  connection db to
347b5 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e   the blocked con
347b6 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 49  nections list. I
347b7 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20  t is assumed.** 
347b8 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 61  that it is not a
347b9 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f 66  lready a part of
347ba 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74   the list..*/.st
347bb 61 74 69 63 20 76 6f 69 64 20 61 64 64 54 6f 42  atic void addToB
347bc 6c 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c 69 74  lockedList(sqlit
347bd 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74  e3 *db){.  sqlit
347be 65 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72  e3 **pp;.  asser
347bf 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20 20  tMutexHeld();.  
347c0 66 6f 72 28 0a 20 20 20 20 70 70 3d 26 73 71 6c  for(.    pp=&sql
347c1 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b  ite3BlockedList;
347c2 20 0a 20 20 20 20 2a 70 70 20 26 26 20 28 2a 70   .    *pp && (*p
347c3 70 29 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  p)->xUnlockNotif
347c4 79 21 3d 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f  y!=db->xUnlockNo
347c5 74 69 66 79 3b 20 0a 20 20 20 20 70 70 3d 26 28  tify; .    pp=&(
347c6 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b  *pp)->pNextBlock
347c7 65 64 0a 20 20 29 3b 0a 20 20 64 62 2d 3e 70 4e  ed.  );.  db->pN
347c8 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 2a 70 70  extBlocked = *pp
347c9 3b 0a 20 20 2a 70 70 20 3d 20 64 62 3b 0a 7d 0a  ;.  *pp = db;.}.
347ca 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68  ./*.** Obtain th
347cb 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  e STATIC_MASTER 
347cc 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63  mutex..*/.static
347cd 20 76 6f 69 64 20 65 6e 74 65 72 4d 75 74 65 78   void enterMutex
347ce 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  (void){.  sqlite
347cf 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
347d0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
347d1 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
347d2 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20  TIC_MASTER));.  
347d3 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74  checkListPropert
347d4 69 65 73 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ies(0);.}../*.**
347d5 20 52 65 6c 65 61 73 65 20 74 68 65 20 53 54 41   Release the STA
347d6 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
347d7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
347d8 20 6c 65 61 76 65 4d 75 74 65 78 28 76 6f 69 64   leaveMutex(void
347d9 29 7b 0a 20 20 61 73 73 65 72 74 4d 75 74 65 78  ){.  assertMutex
347da 48 65 6c 64 28 29 3b 0a 20 20 63 68 65 63 6b 4c  Held();.  checkL
347db 69 73 74 50 72 6f 70 65 72 74 69 65 73 28 30 29  istProperties(0)
347dc 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
347dd 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d  x_leave(sqlite3M
347de 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
347df 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
347e0 53 54 45 52 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  STER));.}../*.**
347e1 20 52 65 67 69 73 74 65 72 20 61 6e 20 75 6e 6c   Register an unl
347e2 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
347e3 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ack..**.** This 
347e4 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
347e5 63 6f 6e 6e 65 63 74 69 6f 6e 20 22 64 62 22 20  connection "db" 
347e6 68 61 73 20 61 74 74 65 6d 70 74 65 64 20 73 6f  has attempted so
347e7 6d 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20  me operation.** 
347e8 62 75 74 20 68 61 73 20 72 65 63 65 69 76 65 64  but has received
347e9 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   an SQLITE_LOCKE
347ea 44 20 65 72 72 6f 72 20 62 65 63 61 75 73 65 20  D error because 
347eb 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  another connecti
347ec 6f 6e 0a 2a 2a 20 28 63 61 6c 6c 20 69 74 20 70  on.** (call it p
347ed 4f 74 68 65 72 29 20 69 6e 20 74 68 65 20 73 61  Other) in the sa
347ee 6d 65 20 70 72 6f 63 65 73 73 20 77 61 73 20 62  me process was b
347ef 75 73 79 20 75 73 69 6e 67 20 74 68 65 20 73 61  usy using the sa
347f0 6d 65 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63  me shared.** cac
347f1 68 65 2e 20 20 70 4f 74 68 65 72 20 69 73 20 66  he.  pOther is f
347f2 6f 75 6e 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20  ound by looking 
347f3 61 74 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67  at db->pBlocking
347f4 43 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  Connection..**.*
347f5 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
347f6 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63   blocking connec
347f7 74 69 6f 6e 2c 20 74 68 65 20 63 61 6c 6c 62 61  tion, the callba
347f8 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d  ck is invoked im
347f9 6d 65 64 69 61 74 65 6c 79 2c 0a 2a 2a 20 62 65  mediately,.** be
347fa 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e  fore this routin
347fb 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a  e returns..**.**
347fc 20 49 66 20 70 4f 74 68 65 72 20 69 73 20 61 6c   If pOther is al
347fd 72 65 61 64 79 20 62 6c 6f 63 6b 65 64 20 6f 6e  ready blocked on
347fe 20 64 62 2c 20 74 68 65 6e 20 72 65 70 6f 72 74   db, then report
347ff 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20   SQLITE_LOCKED, 
34800 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 61  to indicate.** a
34801 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a   deadlock..**.**
34802 20 4f 74 68 65 72 77 69 73 65 2c 20 6d 61 6b 65   Otherwise, make
34803 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 74 6f   arrangements to
34804 20 69 6e 76 6f 6b 65 20 78 4e 6f 74 69 66 79 20   invoke xNotify 
34805 77 68 65 6e 20 70 4f 74 68 65 72 20 64 72 6f 70  when pOther drop
34806 73 0a 2a 2a 20 69 74 73 20 6c 6f 63 6b 73 2e 0a  s.** its locks..
34807 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20  **.** Each call 
34808 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
34809 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 72  overrides any pr
3480a 69 6f 72 20 63 61 6c 6c 62 61 63 6b 73 20 72 65  ior callbacks re
3480b 67 69 73 74 65 72 65 64 0a 2a 2a 20 6f 6e 20 74  gistered.** on t
3480c 68 65 20 73 61 6d 65 20 22 64 62 22 2e 20 20 49  he same "db".  I
3480d 66 20 78 4e 6f 74 69 66 79 3d 3d 30 20 74 68 65  f xNotify==0 the
3480e 6e 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c  n any prior call
3480f 62 61 63 6b 73 20 61 72 65 20 69 6d 6d 65 64 69  backs are immedi
34810 61 74 65 6c 79 0a 2a 2a 20 63 61 6e 63 65 6c 6c  ately.** cancell
34811 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
34812 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e  I int sqlite3_un
34813 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73  lock_notify(.  s
34814 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f  qlite3 *db,.  vo
34815 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f  id (*xNotify)(vo
34816 69 64 20 2a 2a 2c 20 69 6e 74 29 2c 0a 20 20 76  id **, int),.  v
34817 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 69  oid *pArg.){.  i
34818 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
34819 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  K;..  sqlite3_mu
3481a 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
3481b 74 65 78 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74  tex);.  enterMut
3481c 65 78 28 29 3b 0a 0a 20 20 69 66 28 20 78 4e 6f  ex();..  if( xNo
3481d 74 69 66 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72  tify==0 ){.    r
3481e 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64  emoveFromBlocked
3481f 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20 64 62  List(db);.    db
34820 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
34821 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  ion = 0;.    db-
34822 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d  >xUnlockNotify =
34823 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 55 6e 6c   0;.    db->pUnl
34824 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65  ockArg = 0;.  }e
34825 6c 73 65 20 69 66 28 20 30 3d 3d 64 62 2d 3e 70  lse if( 0==db->p
34826 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
34827 6f 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  on ){.    /* The
34828 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61 6e 73 61   blocking transa
34829 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 63  ction has been c
3482a 6f 6e 63 6c 75 64 65 64 2e 20 4f 72 20 74 68 65  oncluded. Or the
3482b 72 65 20 6e 65 76 65 72 20 77 61 73 20 61 20 0a  re never was a .
3482c 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 69 6e 67 20      ** blocking 
3482d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 6e 20  transaction. In 
3482e 65 69 74 68 65 72 20 63 61 73 65 2c 20 69 6e 76  either case, inv
3482f 6f 6b 65 20 74 68 65 20 6e 6f 74 69 66 79 20 63  oke the notify c
34830 61 6c 6c 62 61 63 6b 0a 20 20 20 20 2a 2a 20 69  allback.    ** i
34831 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20 20  mmediately. .   
34832 20 2a 2f 0a 20 20 20 20 78 4e 6f 74 69 66 79 28   */.    xNotify(
34833 26 70 41 72 67 2c 20 31 29 3b 0a 20 20 7d 65 6c  &pArg, 1);.  }el
34834 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  se{.    sqlite3 
34835 2a 70 3b 0a 0a 20 20 20 20 66 6f 72 28 70 3d 64  *p;..    for(p=d
34836 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e  b->pBlockingConn
34837 65 63 74 69 6f 6e 3b 20 70 20 26 26 20 70 21 3d  ection; p && p!=
34838 64 62 3b 20 70 3d 70 2d 3e 70 55 6e 6c 6f 63 6b  db; p=p->pUnlock
34839 43 6f 6e 6e 65 63 74 69 6f 6e 29 7b 7d 0a 20 20  Connection){}.  
3483a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
3483b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43   rc = SQLITE_LOC
3483c 4b 45 44 3b 20 20 20 20 20 20 20 20 20 20 20 20  KED;            
3483d 20 20 2f 2a 20 44 65 61 64 6c 6f 63 6b 20 64 65    /* Deadlock de
3483e 74 65 63 74 65 64 2e 20 2a 2f 0a 20 20 20 20 7d  tected. */.    }
3483f 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e  else{.      db->
34840 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
34841 6e 20 3d 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e  n = db->pBlockin
34842 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 0a 20 20 20  gConnection;.   
34843 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f     db->xUnlockNo
34844 74 69 66 79 20 3d 20 78 4e 6f 74 69 66 79 3b 0a  tify = xNotify;.
34845 20 20 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63        db->pUnloc
34846 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 20  kArg = pArg;.   
34847 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f     removeFromBlo
34848 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20  ckedList(db);.  
34849 20 20 20 20 61 64 64 54 6f 42 6c 6f 63 6b 65 64      addToBlocked
3484a 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a  List(db);.    }.
3484b 20 20 7d 0a 0a 20 20 6c 65 61 76 65 4d 75 74 65    }..  leaveMute
3484c 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  x();.  assert( !
3484d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
3484e 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72   );.  sqlite3Err
3484f 6f 72 28 64 62 2c 20 72 63 2c 20 28 72 63 3f 22  or(db, rc, (rc?"
34850 64 61 74 61 62 61 73 65 20 69 73 20 64 65 61 64  database is dead
34851 6c 6f 63 6b 65 64 22 3a 30 29 29 3b 0a 20 20 73  locked":0));.  s
34852 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
34853 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
34854 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
34855 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
34856 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 69  on is called whi
34857 6c 65 20 73 74 65 70 70 69 6e 67 20 6f 72 20 70  le stepping or p
34858 72 65 70 61 72 69 6e 67 20 61 20 73 74 61 74 65  reparing a state
34859 6d 65 6e 74 20 0a 2a 2a 20 61 73 73 6f 63 69 61  ment .** associa
3485a 74 65 64 20 77 69 74 68 20 63 6f 6e 6e 65 63 74  ted with connect
3485b 69 6f 6e 20 64 62 2e 20 54 68 65 20 6f 70 65 72  ion db. The oper
3485c 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 74 75 72  ation will retur
3485d 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 0a  n SQLITE_LOCKED.
3485e 2a 2a 20 74 6f 20 74 68 65 20 75 73 65 72 20 62  ** to the user b
3485f 65 63 61 75 73 65 20 69 74 20 72 65 71 75 69 72  ecause it requir
34860 65 73 20 61 20 6c 6f 63 6b 20 74 68 61 74 20 77  es a lock that w
34861 69 6c 6c 20 6e 6f 74 20 62 65 20 61 76 61 69 6c  ill not be avail
34862 61 62 6c 65 0a 2a 2a 20 75 6e 74 69 6c 20 63 6f  able.** until co
34863 6e 6e 65 63 74 69 6f 6e 20 70 42 6c 6f 63 6b 65  nnection pBlocke
34864 72 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20  r concludes its 
34865 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
34866 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
34867 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
34868 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
34869 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62  cked(sqlite3 *db
3486a 2c 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63  , sqlite3 *pBloc
3486b 6b 65 72 29 7b 0a 20 20 65 6e 74 65 72 4d 75 74  ker){.  enterMut
3486c 65 78 28 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ex();.  if( db->
3486d 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
3486e 69 6f 6e 3d 3d 30 20 26 26 20 64 62 2d 3e 70 55  ion==0 && db->pU
3486f 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d  nlockConnection=
34870 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 54 6f 42  =0 ){.    addToB
34871 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
34872 20 20 7d 0a 20 20 64 62 2d 3e 70 42 6c 6f 63 6b    }.  db->pBlock
34873 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20  ingConnection = 
34874 70 42 6c 6f 63 6b 65 72 3b 0a 20 20 6c 65 61 76  pBlocker;.  leav
34875 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a  eMutex();.}../*.
34876 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
34877 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 0a   is called when.
34878 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
34879 6f 6e 20 6f 70 65 6e 65 64 20 62 79 20 64 61 74  on opened by dat
3487a 61 62 61 73 65 20 64 62 20 68 61 73 20 6a 75 73  abase db has jus
3487b 74 20 66 69 6e 69 73 68 65 64 2e 20 4c 6f 63 6b  t finished. Lock
3487c 73 20 68 65 6c 64 20 0a 2a 2a 20 62 79 20 64 61  s held .** by da
3487d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3487e 6e 20 64 62 20 68 61 76 65 20 62 65 65 6e 20 72  n db have been r
3487f 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eleased..**.** T
34880 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f  his function loo
34881 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 68 20  ps through each 
34882 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62 6c 6f  entry in the blo
34883 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
34884 0a 2a 2a 20 6c 69 73 74 20 61 6e 64 20 64 6f 65  .** list and doe
34885 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
34886 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66 20 74  .**.**   1) If t
34887 68 65 20 73 71 6c 69 74 65 33 2e 70 42 6c 6f 63  he sqlite3.pBloc
34888 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d  kingConnection m
34889 65 6d 62 65 72 20 6f 66 20 61 20 6c 69 73 74 20  ember of a list 
3488a 65 6e 74 72 79 20 69 73 0a 2a 2a 20 20 20 20 20  entry is.**     
3488b 20 73 65 74 20 74 6f 20 64 62 2c 20 74 68 65 6e   set to db, then
3488c 20 73 65 74 20 70 42 6c 6f 63 6b 69 6e 67 43 6f   set pBlockingCo
3488d 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a 2a  nnection=0..**.*
3488e 2a 20 20 20 32 29 20 49 66 20 74 68 65 20 73 71  *   2) If the sq
3488f 6c 69 74 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e  lite3.pUnlockCon
34890 6e 65 63 74 69 6f 6e 20 6d 65 6d 62 65 72 20 6f  nection member o
34891 66 20 61 20 6c 69 73 74 20 65 6e 74 72 79 20 69  f a list entry i
34892 73 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 74 6f  s.**      set to
34893 20 64 62 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65   db, then invoke
34894 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20   the configured 
34895 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
34896 6c 6c 62 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20  llback and.**   
34897 20 20 20 73 65 74 20 70 55 6e 6c 6f 63 6b 43 6f     set pUnlockCo
34898 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a 2a  nnection=0..**.*
34899 2a 20 20 20 33 29 20 49 66 20 74 68 65 20 74 77  *   3) If the tw
3489a 6f 20 73 74 65 70 73 20 61 62 6f 76 65 20 6d 65  o steps above me
3489b 61 6e 20 74 68 61 74 20 70 42 6c 6f 63 6b 69 6e  an that pBlockin
3489c 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 61  gConnection==0 a
3489d 6e 64 0a 2a 2a 20 20 20 20 20 20 70 55 6e 6c 6f  nd.**      pUnlo
3489e 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 2c  ckConnection==0,
3489f 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72   remove the entr
348a0 79 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b  y from the block
348a1 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a  ed connections.*
348a2 2a 20 20 20 20 20 20 6c 69 73 74 2e 0a 2a 2f 0a  *      list..*/.
348a3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
348a4 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  oid sqlite3Conne
348a5 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71  ctionUnlocked(sq
348a6 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 76 6f  lite3 *db){.  vo
348a7 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69  id (*xUnlockNoti
348a8 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74  fy)(void **, int
348a9 29 20 3d 20 30 3b 20 2f 2a 20 55 6e 6c 6f 63 6b  ) = 0; /* Unlock
348aa 2d 6e 6f 74 69 66 79 20 63 62 20 74 6f 20 69 6e  -notify cb to in
348ab 76 6f 6b 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  voke */.  int nA
348ac 72 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  rg = 0;         
348ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348ae 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
348af 65 6e 74 72 69 65 73 20 69 6e 20 61 41 72 67 5b  entries in aArg[
348b0 5d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  ] */.  sqlite3 *
348b1 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  *pp;            
348b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348b3 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
348b4 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  able */.  void *
348b5 2a 61 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  *aArg;          
348b6 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
348b7 73 20 74 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 20  s to the unlock 
348b8 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f  callback */.  vo
348b9 69 64 20 2a 2a 61 44 79 6e 20 3d 20 30 3b 20 20  id **aDyn = 0;  
348ba 20 20 20 20 20 20 20 20 20 2f 2a 20 44 79 6e 61           /* Dyna
348bb 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
348bc 64 20 73 70 61 63 65 20 66 6f 72 20 61 41 72 67  d space for aArg
348bd 5b 5d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 61 53  [] */.  void *aS
348be 74 61 74 69 63 5b 31 36 5d 3b 20 20 20 20 20 20  tatic[16];      
348bf 20 20 20 2f 2a 20 53 74 61 72 74 65 72 20 73 70     /* Starter sp
348c0 61 63 65 20 66 6f 72 20 61 41 72 67 5b 5d 2e 20  ace for aArg[]. 
348c1 20 4e 6f 20 6d 61 6c 6c 6f 63 20 72 65 71 75 69   No malloc requi
348c2 72 65 64 20 2a 2f 0a 0a 20 20 61 41 72 67 20 3d  red */..  aArg =
348c3 20 61 53 74 61 74 69 63 3b 0a 20 20 65 6e 74 65   aStatic;.  ente
348c4 72 4d 75 74 65 78 28 29 3b 20 20 20 20 20 20 20  rMutex();       
348c5 20 20 2f 2a 20 45 6e 74 65 72 20 53 54 41 54 49    /* Enter STATI
348c6 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 20 2a  C_MASTER mutex *
348c7 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f  /..  /* This loo
348c8 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20  p runs once for 
348c9 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68  each entry in th
348ca 65 20 62 6c 6f 63 6b 65 64 2d 63 6f 6e 6e 65 63  e blocked-connec
348cb 74 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f 0a 20  tions list. */. 
348cc 20 66 6f 72 28 70 70 3d 26 73 71 6c 69 74 65 33   for(pp=&sqlite3
348cd 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70  BlockedList; *pp
348ce 3b 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 20 29 7b  ; /* no-op */ ){
348cf 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 70 20  .    sqlite3 *p 
348d0 3d 20 2a 70 70 3b 0a 0a 20 20 20 20 2f 2a 20 53  = *pp;..    /* S
348d1 74 65 70 20 31 2e 20 2a 2f 0a 20 20 20 20 69 66  tep 1. */.    if
348d2 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f  ( p->pBlockingCo
348d3 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29 7b 0a  nnection==db ){.
348d4 20 20 20 20 20 20 70 2d 3e 70 42 6c 6f 63 6b 69        p->pBlocki
348d5 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30  ngConnection = 0
348d6 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
348d7 53 74 65 70 20 32 2e 20 2a 2f 0a 20 20 20 20 69  Step 2. */.    i
348d8 66 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e  f( p->pUnlockCon
348d9 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29 7b 0a 20  nection==db ){. 
348da 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
348db 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 29 3b  xUnlockNotify );
348dc 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 78 55  .      if( p->xU
348dd 6e 6c 6f 63 6b 4e 6f 74 69 66 79 21 3d 78 55 6e  nlockNotify!=xUn
348de 6c 6f 63 6b 4e 6f 74 69 66 79 20 26 26 20 6e 41  lockNotify && nA
348df 72 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rg!=0 ){.       
348e0 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61   xUnlockNotify(a
348e1 41 72 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20  Arg, nArg);.    
348e2 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20      nArg = 0;.  
348e3 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73 71 6c      }..      sql
348e4 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
348e5 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 61  alloc();.      a
348e6 73 73 65 72 74 28 20 61 41 72 67 3d 3d 61 44 79  ssert( aArg==aDy
348e7 6e 20 7c 7c 20 28 61 44 79 6e 3d 3d 30 20 26 26  n || (aDyn==0 &&
348e8 20 61 41 72 67 3d 3d 61 53 74 61 74 69 63 29 20   aArg==aStatic) 
348e9 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
348ea 20 6e 41 72 67 3c 3d 28 69 6e 74 29 41 72 72 61   nArg<=(int)Arra
348eb 79 53 69 7a 65 28 61 53 74 61 74 69 63 29 20 7c  ySize(aStatic) |
348ec 7c 20 61 41 72 67 3d 3d 61 44 79 6e 20 29 3b 0a  | aArg==aDyn );.
348ed 20 20 20 20 20 20 69 66 28 20 28 21 61 44 79 6e        if( (!aDyn
348ee 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74 29 41   && nArg==(int)A
348ef 72 72 61 79 53 69 7a 65 28 61 53 74 61 74 69 63  rraySize(aStatic
348f0 29 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 61 44  )).       || (aD
348f1 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74  yn && nArg==(int
348f2 29 28 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  )(sqlite3DbMallo
348f3 63 53 69 7a 65 28 64 62 2c 20 61 44 79 6e 29 2f  cSize(db, aDyn)/
348f4 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 29 0a  sizeof(void*))).
348f5 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
348f6 20 2f 2a 20 54 68 65 20 61 41 72 67 5b 5d 20 61   /* The aArg[] a
348f7 72 72 61 79 20 6e 65 65 64 73 20 74 6f 20 67 72  rray needs to gr
348f8 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 76  ow. */.        v
348f9 6f 69 64 20 2a 2a 70 4e 65 77 20 3d 20 28 76 6f  oid **pNew = (vo
348fa 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 4d 61 6c  id **)sqlite3Mal
348fb 6c 6f 63 28 6e 41 72 67 2a 73 69 7a 65 6f 66 28  loc(nArg*sizeof(
348fc 76 6f 69 64 20 2a 29 2a 32 29 3b 0a 20 20 20 20  void *)*2);.    
348fd 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a      if( pNew ){.
348fe 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
348ff 28 70 4e 65 77 2c 20 61 41 72 67 2c 20 6e 41 72  (pNew, aArg, nAr
34900 67 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29  g*sizeof(void *)
34901 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
34902 69 74 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b  ite3_free(aDyn);
34903 0a 20 20 20 20 20 20 20 20 20 20 61 44 79 6e 20  .          aDyn 
34904 3d 20 61 41 72 67 20 3d 20 70 4e 65 77 3b 0a 20  = aArg = pNew;. 
34905 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
34906 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
34907 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68 65 20  occurs when the 
34908 61 72 72 61 79 20 6f 66 20 63 6f 6e 74 65 78 74  array of context
34909 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20 6e   pointers that n
3490a 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20  eed to.         
3490b 20 2a 2a 20 62 65 20 70 61 73 73 65 64 20 74 6f   ** be passed to
3490c 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69   the unlock-noti
3490d 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6c  fy callback is l
3490e 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 20  arger than the. 
3490f 20 20 20 20 20 20 20 20 20 2a 2a 20 61 53 74 61           ** aSta
34910 74 69 63 5b 5d 20 61 72 72 61 79 20 61 6c 6c 6f  tic[] array allo
34911 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73 74 61  cated on the sta
34912 63 6b 20 61 6e 64 20 74 68 65 20 61 74 74 65 6d  ck and the attem
34913 70 74 20 74 6f 20 0a 20 20 20 20 20 20 20 20 20  pt to .         
34914 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 6c   ** allocate a l
34915 61 72 67 65 72 20 61 72 72 61 79 20 66 72 6f 6d  arger array from
34916 20 74 68 65 20 68 65 61 70 20 68 61 73 20 66 61   the heap has fa
34917 69 6c 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20  iled..          
34918 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  **.          ** 
34919 54 68 69 73 20 69 73 20 61 20 64 69 66 66 69 63  This is a diffic
3491a 75 6c 74 20 73 69 74 75 61 74 69 6f 6e 20 74 6f  ult situation to
3491b 20 68 61 6e 64 6c 65 2e 20 52 65 74 75 72 6e 69   handle. Returni
3491c 6e 67 20 61 6e 20 65 72 72 6f 72 0a 20 20 20 20  ng an error.    
3491d 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f        ** code to
3491e 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 69   the caller is i
3491f 6e 73 75 66 66 69 63 69 65 6e 74 2c 20 61 73 20  nsufficient, as 
34920 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72  even if an error
34921 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20 20 20   code.          
34922 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  ** is returned t
34923 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
34924 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20  n connection db 
34925 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 0a 20 20  will still be.  
34926 20 20 20 20 20 20 20 20 2a 2a 20 63 6c 6f 73 65          ** close
34927 64 20 61 6e 64 20 74 68 65 20 75 6e 6c 6f 63 6b  d and the unlock
34928 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
34929 73 20 6f 6e 20 62 6c 6f 63 6b 65 64 20 63 6f 6e  s on blocked con
3492a 6e 65 63 74 69 6f 6e 73 0a 20 20 20 20 20 20 20  nections.       
3492b 20 20 20 2a 2a 20 77 69 6c 6c 20 67 6f 20 75 6e     ** will go un
3492c 69 73 73 75 65 64 2e 20 54 68 69 73 20 6d 69 67  issued. This mig
3492d 68 74 20 63 61 75 73 65 20 74 68 65 20 61 70 70  ht cause the app
3492e 6c 69 63 61 74 69 6f 6e 20 74 6f 20 77 61 69 74  lication to wait
3492f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  .          ** in
34930 64 65 66 69 6e 69 74 65 6c 79 20 66 6f 72 20 61  definitely for a
34931 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  n unlock-notify 
34932 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69  callback that wi
34933 6c 6c 20 6e 65 76 65 72 20 0a 20 20 20 20 20 20  ll never .      
34934 20 20 20 20 2a 2a 20 61 72 72 69 76 65 2e 0a 20      ** arrive.. 
34935 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20           **.    
34936 20 20 20 20 20 20 2a 2a 20 49 6e 73 74 65 61 64        ** Instead
34937 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 75 6e 6c  , invoke the unl
34938 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
34939 61 63 6b 20 77 69 74 68 20 74 68 65 20 63 6f 6e  ack with the con
3493a 74 65 78 74 0a 20 20 20 20 20 20 20 20 20 20 2a  text.          *
3493b 2a 20 61 72 72 61 79 20 61 6c 72 65 61 64 79 20  * array already 
3493c 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 57 65 20  accumulated. We 
3493d 63 61 6e 20 74 68 65 6e 20 63 6c 65 61 72 20 74  can then clear t
3493e 68 65 20 61 72 72 61 79 20 61 6e 64 0a 20 20 20  he array and.   
3493f 20 20 20 20 20 20 20 2a 2a 20 62 65 67 69 6e 20         ** begin 
34940 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 61 6e 79  accumulating any
34941 20 66 75 72 74 68 65 72 20 63 6f 6e 74 65 78 74   further context
34942 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 6f 75   pointers withou
34943 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  t .          ** 
34944 72 65 71 75 69 72 69 6e 67 20 61 6e 79 20 64 79  requiring any dy
34945 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 6f 6e  namic allocation
34946 2e 20 54 68 69 73 20 69 73 20 73 75 62 2d 6f 70  . This is sub-op
34947 74 69 6d 61 6c 20 62 65 63 61 75 73 65 0a 20 20  timal because.  
34948 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 65          ** it me
34949 61 6e 73 20 74 68 61 74 20 69 6e 73 74 65 61 64  ans that instead
3494a 20 6f 66 20 6f 6e 65 20 63 61 6c 6c 62 61 63 6b   of one callback
3494b 20 77 69 74 68 20 61 20 6c 61 72 67 65 20 61 72   with a large ar
3494c 72 61 79 20 6f 66 0a 20 20 20 20 20 20 20 20 20  ray of.         
3494d 20 2a 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e   ** context poin
3494e 74 65 72 73 20 74 68 65 20 61 70 70 6c 69 63 61  ters the applica
3494f 74 69 6f 6e 20 77 69 6c 6c 20 72 65 63 65 69 76  tion will receiv
34950 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 20 20  e two or more.  
34951 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 62          ** callb
34952 61 63 6b 73 20 77 69 74 68 20 73 6d 61 6c 6c 65  acks with smalle
34953 72 20 61 72 72 61 79 73 20 6f 66 20 63 6f 6e 74  r arrays of cont
34954 65 78 74 20 70 6f 69 6e 74 65 72 73 2c 20 77 68  ext pointers, wh
34955 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 20 20 20  ich will.       
34956 20 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65     ** reduce the
34957 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 62   applications ab
34958 69 6c 69 74 79 20 74 6f 20 70 72 69 6f 72 69 74  ility to priorit
34959 69 7a 65 20 6d 75 6c 74 69 70 6c 65 20 0a 20 20  ize multiple .  
3495a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 6e 65          ** conne
3495b 63 74 69 6f 6e 73 2e 20 42 75 74 20 69 74 20 69  ctions. But it i
3495c 73 20 74 68 65 20 62 65 73 74 20 74 68 61 74 20  s the best that 
3495d 63 61 6e 20 62 65 20 64 6f 6e 65 20 75 6e 64 65  can be done unde
3495e 72 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  r the.          
3495f 2a 2a 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73  ** circumstances
34960 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
34961 20 20 20 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b           xUnlock
34962 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72  Notify(aArg, nAr
34963 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 41  g);.          nA
34964 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  rg = 0;.        
34965 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
34966 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
34967 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 20 20 20  Malloc();..     
34968 20 61 41 72 67 5b 6e 41 72 67 2b 2b 5d 20 3d 20   aArg[nArg++] = 
34969 70 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 3b 0a 20  p->pUnlockArg;. 
3496a 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69       xUnlockNoti
3496b 66 79 20 3d 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e  fy = p->xUnlockN
3496c 6f 74 69 66 79 3b 0a 20 20 20 20 20 20 70 2d 3e  otify;.      p->
3496d 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
3496e 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  n = 0;.      p->
3496f 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20  xUnlockNotify = 
34970 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 55 6e 6c  0;.      p->pUnl
34971 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20 20 20  ockArg = 0;.    
34972 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20 33  }..    /* Step 3
34973 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e  . */.    if( p->
34974 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
34975 69 6f 6e 3d 3d 30 20 26 26 20 70 2d 3e 70 55 6e  ion==0 && p->pUn
34976 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  lockConnection==
34977 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65  0 ){.      /* Re
34978 6d 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  move connection 
34979 70 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b  p from the block
3497a 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c  ed connections l
3497b 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70  ist. */.      *p
3497c 70 20 3d 20 70 2d 3e 70 4e 65 78 74 42 6c 6f 63  p = p->pNextBloc
3497d 6b 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e  ked;.      p->pN
3497e 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a  extBlocked = 0;.
3497f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
34980 20 70 70 20 3d 20 26 70 2d 3e 70 4e 65 78 74 42   pp = &p->pNextB
34981 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 7d 0a 20 20  locked;.    }.  
34982 7d 0a 0a 20 20 69 66 28 20 6e 41 72 67 21 3d 30  }..  if( nArg!=0
34983 20 29 7b 0a 20 20 20 20 78 55 6e 6c 6f 63 6b 4e   ){.    xUnlockN
34984 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67  otify(aArg, nArg
34985 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
34986 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a 20 20 6c  _free(aDyn);.  l
34987 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20  eaveMutex();    
34988 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 53 54       /* Leave ST
34989 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65  ATIC_MASTER mute
3498a 78 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  x */.}../*.** Th
3498b 69 73 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  is is called whe
3498c 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  n the database c
3498d 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73 65 64  onnection passed
3498e 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
3498f 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 63 6c 6f  is .** being clo
34990 73 65 64 2e 20 54 68 65 20 63 6f 6e 6e 65 63 74  sed. The connect
34991 69 6f 6e 20 69 73 20 72 65 6d 6f 76 65 64 20 66  ion is removed f
34992 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  rom the blocked 
34993 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  list..*/.SQLITE_
34994 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
34995 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c  ite3ConnectionCl
34996 6f 73 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62  osed(sqlite3 *db
34997 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  ){.  sqlite3Conn
34998 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64  ectionUnlocked(d
34999 62 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78  b);.  enterMutex
3499a 28 29 3b 0a 20 20 72 65 6d 6f 76 65 46 72 6f 6d  ();.  removeFrom
3499b 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b  BlockedList(db);
3499c 0a 20 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70  .  checkListProp
3499d 65 72 74 69 65 73 28 64 62 29 3b 0a 20 20 6c 65  erties(db);.  le
3499e 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65  aveMutex();.}.#e
3499f 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
349a0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6e 6f 74  ***** End of not
349a1 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ify.c **********
349a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349a4 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
349a5 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
349a6 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.c ********
349a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349a9 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
349aa 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68   Oct 10.**.** Th
349ab 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
349ac 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
349ad 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
349ae 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
349af 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
349b0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
349b1 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
349b2 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
349b3 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
349b4 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
349b5 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
349b6 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
349b7 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
349b8 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
349b9 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
349ba 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
349bb 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
349bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
349c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
349c1 2a 20 54 68 69 73 20 69 73 20 61 6e 20 53 51 4c  * This is an SQL
349c2 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  ite module imple
349c3 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78  menting full-tex
349c4 74 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a 2f 2a  t search..*/../*
349c5 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
349c6 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
349c7 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a  y compiled if:.*
349c8 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
349c9 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
349ca 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20  ing built as an 
349cb 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  extension.**    
349cc 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73     (in which cas
349cd 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73  e SQLITE_CORE is
349ce 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f   not defined), o
349cf 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  r.**.**     * Th
349d0 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
349d1 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74   being built int
349d2 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a  o the core of.**
349d3 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
349d4 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
349d5 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
349d6 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a  is defined)..*/.
349d7 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
349d8 43 6f 6e 73 69 64 65 72 20 65 78 70 6f 72 74 69  Consider exporti
349d9 6e 67 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 20  ng this comment 
349da 74 6f 20 61 6e 20 48 54 4d 4c 20 66 69 6c 65 20  to an HTML file 
349db 6f 72 20 74 68 65 0a 2a 2a 20 77 69 6b 69 2e 0a  or the.** wiki..
349dc 2a 2f 0a 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74  */./* The full-t
349dd 65 78 74 20 69 6e 64 65 78 20 69 73 20 73 74 6f  ext index is sto
349de 72 65 64 20 69 6e 20 61 20 73 65 72 69 65 73 20  red in a series 
349df 6f 66 20 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65  of b+tree (-like
349e0 29 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  ).** structures 
349e1 63 61 6c 6c 65 64 20 73 65 67 6d 65 6e 74 73 20  called segments 
349e2 77 68 69 63 68 20 6d 61 70 20 74 65 72 6d 73 20  which map terms 
349e3 74 6f 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68  to doclists.  Th
349e4 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  e.** structures 
349e5 61 72 65 20 6c 69 6b 65 20 62 2b 74 72 65 65 73  are like b+trees
349e6 20 69 6e 20 6c 61 79 6f 75 74 2c 20 62 75 74 20   in layout, but 
349e7 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  are constructed 
349e8 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74  from the.** bott
349e9 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c  om up in optimal
349ea 20 66 61 73 68 69 6f 6e 20 61 6e 64 20 61 72 65   fashion and are
349eb 20 6e 6f 74 20 75 70 64 61 74 61 62 6c 65 2e 20   not updatable. 
349ec 20 53 69 6e 63 65 20 74 72 65 65 73 0a 2a 2a 20   Since trees.** 
349ed 61 72 65 20 62 75 69 6c 74 20 66 72 6f 6d 20 74  are built from t
349ee 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c 20 74 68  he bottom up, th
349ef 69 6e 67 73 20 77 69 6c 6c 20 62 65 20 64 65 73  ings will be des
349f0 63 72 69 62 65 64 20 66 72 6f 6d 20 74 68 65 0a  cribed from the.
349f1 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e 0a 2a 2a  ** bottom up..**
349f2 0a 2a 2a 0a 2a 2a 2a 2a 20 56 61 72 69 6e 74 73  .**.**** Varints
349f3 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73   ****.** The bas
349f4 69 63 20 75 6e 69 74 20 6f 66 20 65 6e 63 6f 64  ic unit of encod
349f5 69 6e 67 20 69 73 20 61 20 76 61 72 69 61 62 6c  ing is a variabl
349f6 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
349f7 20 63 61 6c 6c 65 64 20 61 0a 2a 2a 20 76 61 72   called a.** var
349f8 69 6e 74 2e 20 20 57 65 20 65 6e 63 6f 64 65 20  int.  We encode 
349f9 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
349fa 69 6e 74 65 67 65 72 73 20 69 6e 20 6c 69 74 74  integers in litt
349fb 6c 65 2d 65 6e 64 69 61 6e 20 6f 72 64 65 72 0a  le-endian order.
349fc 2a 2a 20 75 73 69 6e 67 20 73 65 76 65 6e 20 62  ** using seven b
349fd 69 74 73 20 2a 20 70 65 72 20 62 79 74 65 20 61  its * per byte a
349fe 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
349ff 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20   KEY:.**        
34a00 20 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20   A = 0xxxxxxx   
34a01 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20   7 bits of data 
34a02 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74  and one flag bit
34a03 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20  .**         B = 
34a04 31 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69  1xxxxxxx    7 bi
34a05 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f  ts of data and o
34a06 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 0a 2a  ne flag bit.**.*
34a07 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a  *  7 bits - A.**
34a08 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a   14 bits - BA.**
34a09 20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a   21 bits - BBA.*
34a0a 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  * and so on..**.
34a0b 2a 2a 20 54 68 69 73 20 69 73 20 69 64 65 6e 74  ** This is ident
34a0c 69 63 61 6c 20 74 6f 20 68 6f 77 20 73 71 6c 69  ical to how sqli
34a0d 74 65 20 65 6e 63 6f 64 65 73 20 76 61 72 69 6e  te encodes varin
34a0e 74 73 20 28 73 65 65 20 75 74 69 6c 2e 63 29 2e  ts (see util.c).
34a0f 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f 63 75  .**.**.**** Docu
34a10 6d 65 6e 74 20 6c 69 73 74 73 20 2a 2a 2a 2a 0a  ment lists ****.
34a11 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20 28 64 6f  ** A doclist (do
34a12 63 75 6d 65 6e 74 20 6c 69 73 74 29 20 68 6f 6c  cument list) hol
34a13 64 73 20 61 20 64 6f 63 69 64 2d 73 6f 72 74 65  ds a docid-sorte
34a14 64 20 6c 69 73 74 20 6f 66 20 68 69 74 73 20 66  d list of hits f
34a15 6f 72 20 61 0a 2a 2a 20 67 69 76 65 6e 20 74 65  or a.** given te
34a16 72 6d 2e 20 20 44 6f 63 6c 69 73 74 73 20 68 6f  rm.  Doclists ho
34a17 6c 64 20 64 6f 63 69 64 73 2c 20 61 6e 64 20 63  ld docids, and c
34a18 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 61 73  an optionally as
34a19 73 6f 63 69 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e  sociate.** token
34a1a 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f   positions and o
34a1b 66 66 73 65 74 73 20 77 69 74 68 20 64 6f 63 69  ffsets with doci
34a1c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50  ds..**.** A DL_P
34a1d 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
34a1e 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74 6f 72   doclist is stor
34a1f 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ed like this:.**
34a20 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20  .** array {.**  
34a21 20 76 61 72 69 6e 74 20 64 6f 63 69 64 3b 0a 2a   varint docid;.*
34a22 2a 20 20 20 61 72 72 61 79 20 7b 20 20 20 20 20  *   array {     
34a23 20 20 20 20 20 20 20 20 20 20 20 28 70 6f 73 69             (posi
34a24 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 63 6f  tion list for co
34a25 6c 75 6d 6e 20 30 29 0a 2a 2a 20 20 20 20 20 76  lumn 0).**     v
34a26 61 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20  arint position; 
34a27 20 20 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20      (delta from 
34a28 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74 69 6f  previous positio
34a29 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53 45 29  n plus POS_BASE)
34a2a 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 73  .**     varint s
34a2b 74 61 72 74 4f 66 66 73 65 74 3b 20 20 28 64 65  tartOffset;  (de
34a2c 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75  lta from previou
34a2d 73 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a  s startOffset).*
34a2e 2a 20 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64  *     varint end
34a2f 4f 66 66 73 65 74 3b 20 20 20 20 28 64 65 6c 74  Offset;    (delt
34a30 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66 66 73  a from startOffs
34a31 65 74 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20  et).**   }.**   
34a32 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 76  array {.**     v
34a33 61 72 69 6e 74 20 50 4f 53 5f 43 4f 4c 55 4d 4e  arint POS_COLUMN
34a34 3b 20 20 20 28 6d 61 72 6b 73 20 73 74 61 72 74  ;   (marks start
34a35 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73   of position lis
34a36 74 20 66 6f 72 20 6e 65 77 20 63 6f 6c 75 6d 6e  t for new column
34a37 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20  ).**     varint 
34a38 63 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 28 69  column;       (i
34a39 6e 64 65 78 20 6f 66 20 6e 65 77 20 63 6f 6c 75  ndex of new colu
34a3a 6d 6e 29 0a 2a 2a 20 20 20 20 20 61 72 72 61 79  mn).**     array
34a3b 20 7b 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69   {.**       vari
34a3c 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20 28  nt position;   (
34a3d 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
34a3e 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70 6c 75  ous position plu
34a3f 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a 20 20  s POS_BASE).**  
34a40 20 20 20 20 20 76 61 72 69 6e 74 20 73 74 61 72       varint star
34a41 74 4f 66 66 73 65 74 3b 28 64 65 6c 74 61 20 66  tOffset;(delta f
34a42 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73 74 61  rom previous sta
34a43 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20 20  rtOffset).**    
34a44 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f 66 66     varint endOff
34a45 73 65 74 3b 20 20 28 64 65 6c 74 61 20 66 72 6f  set;  (delta fro
34a46 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a  m startOffset).*
34a47 2a 20 20 20 20 20 7d 0a 2a 2a 20 20 20 7d 0a 2a  *     }.**   }.*
34a48 2a 20 20 20 76 61 72 69 6e 74 20 50 4f 53 5f 45  *   varint POS_E
34a49 4e 44 3b 20 20 20 20 20 20 20 20 28 6d 61 72 6b  ND;        (mark
34a4a 73 20 65 6e 64 20 6f 66 20 70 6f 73 69 74 69 6f  s end of positio
34a4b 6e 73 20 66 6f 72 20 74 68 69 73 20 64 6f 63 75  ns for this docu
34a4c 6d 65 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  ment..** }.**.**
34a4d 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58   Here, array { X
34a4e 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72   } means zero or
34a4f 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
34a50 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
34a51 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 20 20   in.** memory.  
34a52 41 20 22 70 6f 73 69 74 69 6f 6e 22 20 69 73 20  A "position" is 
34a53 61 6e 20 69 6e 64 65 78 20 6f 66 20 61 20 74 6f  an index of a to
34a54 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f 6b 65 6e  ken in the token
34a55 20 73 74 72 65 61 6d 0a 2a 2a 20 67 65 6e 65 72   stream.** gener
34a56 61 74 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65  ated by the toke
34a57 6e 69 7a 65 72 2c 20 77 68 69 6c 65 20 61 6e 20  nizer, while an 
34a58 22 6f 66 66 73 65 74 22 20 69 73 20 61 20 62 79  "offset" is a by
34a59 74 65 20 6f 66 66 73 65 74 2c 0a 2a 2a 20 62 6f  te offset,.** bo
34a5a 74 68 20 62 61 73 65 64 20 61 74 20 30 2e 20 20  th based at 0.  
34a5b 4e 6f 74 65 20 74 68 61 74 20 50 4f 53 5f 45 4e  Note that POS_EN
34a5c 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c 55 4d 4e  D and POS_COLUMN
34a5d 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a   occur in the.**
34a5e 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20 70 6c   same logical pl
34a5f 61 63 65 20 61 73 20 74 68 65 20 70 6f 73 69 74  ace as the posit
34a60 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20 61 6e 64  ion element, and
34a61 20 61 63 74 20 61 73 20 73 65 6e 74 69 6e 61 6c   act as sentinal
34a62 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61 20 70 6f  s.** ending a po
34a63 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72 72 61  sition list arra
34a64 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f  y..**.** A DL_PO
34a65 53 49 54 49 4f 4e 53 20 64 6f 63 6c 69 73 74 20  SITIONS doclist 
34a66 6f 6d 69 74 73 20 74 68 65 20 73 74 61 72 74 4f  omits the startO
34a67 66 66 73 65 74 20 61 6e 64 20 65 6e 64 4f 66 66  ffset and endOff
34a68 73 65 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  set.** informati
34a69 6f 6e 2e 20 20 41 20 44 4c 5f 44 4f 43 49 44 53  on.  A DL_DOCIDS
34a6a 20 64 6f 63 6c 69 73 74 20 6f 6d 69 74 73 20 62   doclist omits b
34a6b 6f 74 68 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  oth the position
34a6c 20 61 6e 64 0a 2a 2a 20 6f 66 66 73 65 74 20 69   and.** offset i
34a6d 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62 65 63 6f  nformation, beco
34a6e 6d 69 6e 67 20 61 6e 20 61 72 72 61 79 20 6f 66  ming an array of
34a6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65 64 20   varint-encoded 
34a70 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  docids..**.** On
34a71 2d 64 69 73 6b 20 64 61 74 61 20 69 73 20 73 74  -disk data is st
34a72 6f 72 65 64 20 61 73 20 74 79 70 65 20 44 4c 5f  ored as type DL_
34a73 44 45 46 41 55 4c 54 2c 20 73 6f 20 77 65 20 64  DEFAULT, so we d
34a74 6f 6e 27 74 20 73 65 72 69 61 6c 69 7a 65 0a 2a  on't serialize.*
34a75 2a 20 74 68 65 20 74 79 70 65 2e 20 20 44 75 65  * the type.  Due
34a76 20 74 6f 20 68 6f 77 20 64 65 6c 65 74 69 6f 6e   to how deletion
34a77 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
34a78 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 61 74  in the segmentat
34a79 69 6f 6e 0a 2a 2a 20 73 79 73 74 65 6d 2c 20 6f  ion.** system, o
34a7a 6e 2d 64 69 73 6b 20 64 6f 63 6c 69 73 74 73 20  n-disk doclists 
34a7b 4d 55 53 54 20 73 74 6f 72 65 20 61 74 20 6c 65  MUST store at le
34a7c 61 73 74 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a  ast positions..*
34a7d 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e  *.**.**** Segmen
34a7e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 2a 2a 2a  t leaf nodes ***
34a7f 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c 65 61  *.** Segment lea
34a80 66 20 6e 6f 64 65 73 20 73 74 6f 72 65 20 74 65  f nodes store te
34a81 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73 74 73  rms and doclists
34a82 2c 20 6f 72 64 65 72 65 64 20 62 79 20 74 65 72  , ordered by ter
34a83 6d 2e 20 20 4c 65 61 66 0a 2a 2a 20 6e 6f 64 65  m.  Leaf.** node
34a84 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73  s are written us
34a85 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72 2c 20  ing LeafWriter, 
34a86 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67 20 4c  and read using L
34a87 65 61 66 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a  eafReader (to.**
34a88 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
34a89 20 61 20 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e   a single leaf n
34a8a 6f 64 65 27 73 20 64 61 74 61 29 20 61 6e 64 20  ode's data) and 
34a8b 4c 65 61 76 65 73 52 65 61 64 65 72 20 28 74 6f  LeavesReader (to
34a8c 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f  .** iterate thro
34a8d 75 67 68 20 61 20 73 65 67 6d 65 6e 74 27 73 20  ugh a segment's 
34a8e 65 6e 74 69 72 65 20 6c 65 61 66 20 6c 61 79 65  entire leaf laye
34a8f 72 29 2e 20 20 4c 65 61 66 20 6e 6f 64 65 73 20  r).  Leaf nodes 
34a90 68 61 76 65 0a 2a 2a 20 74 68 65 20 66 6f 72 6d  have.** the form
34a91 61 74 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74  at:.**.** varint
34a92 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20   iHeight;       
34a93 20 20 20 20 20 20 28 68 65 69 67 68 74 20 66 72        (height fr
34a94 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61  om leaf level, a
34a95 6c 77 61 79 73 20 30 29 0a 2a 2a 20 76 61 72 69  lways 0).** vari
34a96 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
34a97 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
34a98 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a  of first term).*
34a99 2a 20 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65  * char pTerm[nTe
34a9a 72 6d 5d 3b 20 20 20 20 20 20 20 20 20 20 28 63  rm];          (c
34a9b 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20  ontent of first 
34a9c 74 65 72 6d 29 0a 2a 2a 20 76 61 72 69 6e 74 20  term).** varint 
34a9d 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20  nDoclist;       
34a9e 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
34a9f 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61 74 65  term's associate
34aa0 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 63 68  d doclist).** ch
34aa1 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63  ar pDoclist[nDoc
34aa2 6c 69 73 74 5d 3b 20 20 20 20 28 63 6f 6e 74 65  list];    (conte
34aa3 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a  nt of doclist).*
34aa4 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20  * array {.**    
34aa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34aa6 20 20 20 20 20 20 20 20 20 28 66 75 72 74 68 65           (furthe
34aa7 72 20 74 65 72 6d 73 20 61 72 65 20 64 65 6c 74  r terms are delt
34aa8 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20  a-encoded).**   
34aa9 76 61 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20  varint nPrefix; 
34aaa 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
34aab 68 20 6f 66 20 70 72 65 66 69 78 20 73 68 61 72  h of prefix shar
34aac 65 64 20 77 69 74 68 20 70 72 65 76 69 6f 75 73  ed with previous
34aad 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69   term).**   vari
34aae 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20 20 20  nt nSuffix;     
34aaf 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
34ab0 20 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78   unshared suffix
34ab1 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72  ).**   char pTer
34ab2 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d  mSuffix[nSuffix]
34ab3 3b 28 75 6e 73 68 61 72 65 64 20 73 75 66 66 69  ;(unshared suffi
34ab4 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a  x of next term).
34ab5 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 44 6f 63  **   varint nDoc
34ab6 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 28  list;          (
34ab7 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d 27 73  length of term's
34ab8 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c   associated docl
34ab9 69 73 74 29 0a 2a 2a 20 20 20 63 68 61 72 20 70  ist).**   char p
34aba 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74  Doclist[nDoclist
34abb 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20  ];  (content of 
34abc 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 7d 0a 2a 2a  doclist).** }.**
34abd 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20  .** Here, array 
34abe 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f  { X } means zero
34abf 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
34ac0 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
34ac1 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79  ent in.** memory
34ac2 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 20 6e 6f 64  ..**.** Leaf nod
34ac3 65 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 69 6e  es are broken in
34ac4 74 6f 20 62 6c 6f 63 6b 73 20 77 68 69 63 68 20  to blocks which 
34ac5 61 72 65 20 73 74 6f 72 65 64 20 63 6f 6e 74 69  are stored conti
34ac6 67 75 6f 75 73 6c 79 20 69 6e 0a 2a 2a 20 74 68  guously in.** th
34ac7 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62  e %_segments tab
34ac8 6c 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64  le in sorted ord
34ac9 65 72 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 20  er.  This means 
34aca 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 65 6e  that when the en
34acb 64 0a 2a 2a 20 6f 66 20 61 20 6e 6f 64 65 20 69  d.** of a node i
34acc 73 20 72 65 61 63 68 65 64 2c 20 74 68 65 20 6e  s reached, the n
34acd 65 78 74 20 74 65 72 6d 20 69 73 20 69 6e 20 74  ext term is in t
34ace 68 65 20 6e 6f 64 65 20 77 69 74 68 20 74 68 65  he node with the
34acf 20 6e 65 78 74 0a 2a 2a 20 67 72 65 61 74 65 72   next.** greater
34ad0 20 6e 6f 64 65 20 69 64 2e 0a 2a 2a 0a 2a 2a 20   node id..**.** 
34ad1 4e 65 77 20 64 61 74 61 20 69 73 20 73 70 69 6c  New data is spil
34ad2 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 6c 65 61  led to a new lea
34ad3 66 20 6e 6f 64 65 20 77 68 65 6e 20 74 68 65 20  f node when the 
34ad4 63 75 72 72 65 6e 74 20 6e 6f 64 65 0a 2a 2a 20  current node.** 
34ad5 65 78 63 65 65 64 73 20 4c 45 41 46 5f 4d 41 58  exceeds LEAF_MAX
34ad6 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
34ad7 32 30 34 38 29 2e 20 20 4e 65 77 20 64 61 74 61  2048).  New data
34ad8 20 77 68 69 63 68 20 69 74 73 65 6c 66 20 69 73   which itself is
34ad9 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20  .** larger than 
34ada 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28  STANDALONE_MIN (
34adb 64 65 66 61 75 6c 74 20 31 30 32 34 29 20 69 73  default 1024) is
34adc 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73 74 61   placed in a sta
34add 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64 65 20  ndalone.** node 
34ade 28 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 69 74  (a leaf node wit
34adf 68 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  h a single term 
34ae0 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e 20 20 54  and doclist).  T
34ae1 68 65 20 67 6f 61 6c 20 6f 66 0a 2a 2a 20 74 68  he goal of.** th
34ae2 65 73 65 20 73 65 74 74 69 6e 67 73 20 69 73 20  ese settings is 
34ae3 74 6f 20 70 61 63 6b 20 74 6f 67 65 74 68 65 72  to pack together
34ae4 20 67 72 6f 75 70 73 20 6f 66 20 73 6d 61 6c 6c   groups of small
34ae5 20 64 6f 63 6c 69 73 74 73 20 77 68 69 6c 65 0a   doclists while.
34ae6 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20 65 66 66  ** making it eff
34ae7 69 63 69 65 6e 74 20 74 6f 20 64 69 72 65 63 74  icient to direct
34ae8 6c 79 20 61 63 63 65 73 73 20 6c 61 72 67 65 20  ly access large 
34ae9 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a  doclists.  The.*
34aea 2a 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20  * assumption is 
34aeb 74 68 61 74 20 6c 61 72 67 65 20 64 6f 63 6c 69  that large docli
34aec 73 74 73 20 72 65 70 72 65 73 65 6e 74 20 74 65  sts represent te
34aed 72 6d 73 20 77 68 69 63 68 20 61 72 65 20 6d 6f  rms which are mo
34aee 72 65 0a 2a 2a 20 6c 69 6b 65 6c 79 20 74 6f 20  re.** likely to 
34aef 62 65 20 71 75 65 72 79 20 74 61 72 67 65 74 73  be query targets
34af0 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65  ..**.** TODO(she
34af1 73 73 29 20 49 74 20 6d 61 79 20 62 65 20 75 73  ss) It may be us
34af2 65 66 75 6c 20 66 6f 72 20 62 6c 6f 63 6b 69 6e  eful for blockin
34af3 67 20 64 65 63 69 73 69 6f 6e 73 20 74 6f 20 62  g decisions to b
34af4 65 20 6d 6f 72 65 0a 2a 2a 20 64 79 6e 61 6d 69  e more.** dynami
34af5 63 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65  c.  For instance
34af6 2c 20 69 74 20 6d 61 79 20 6d 61 6b 65 20 6d 6f  , it may make mo
34af7 72 65 20 73 65 6e 73 65 20 74 6f 20 68 61 76 65  re sense to have
34af8 20 61 20 32 2e 35 6b 20 6c 65 61 66 0a 2a 2a 20   a 2.5k leaf.** 
34af9 6e 6f 64 65 20 72 61 74 68 65 72 20 74 68 61 6e  node rather than
34afa 20 73 70 6c 69 74 74 69 6e 67 20 69 6e 74 6f 20   splitting into 
34afb 32 6b 20 61 6e 64 20 2e 35 6b 20 6e 6f 64 65 73  2k and .5k nodes
34afc 2e 20 20 4d 79 20 69 6e 74 75 69 74 69 6f 6e 20  .  My intuition 
34afd 69 73 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20  is.** that this 
34afe 6d 69 67 68 74 20 65 78 74 65 6e 64 20 74 68 72  might extend thr
34aff 6f 75 67 68 20 32 78 20 6f 72 20 34 78 20 74 68  ough 2x or 4x th
34b00 65 20 70 61 67 65 73 69 7a 65 2e 0a 2a 2a 0a 2a  e pagesize..**.*
34b01 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69  *.**** Segment i
34b02 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a  nterior nodes **
34b03 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e  **.** Segment in
34b04 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f  terior nodes sto
34b05 72 65 20 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20  re blockids for 
34b06 73 75 62 74 72 65 65 20 6e 6f 64 65 73 20 61 6e  subtree nodes an
34b07 64 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 64 65  d terms.** to de
34b08 73 63 72 69 62 65 20 77 68 61 74 20 64 61 74 61  scribe what data
34b09 20 69 73 20 73 74 6f 72 65 64 20 62 79 20 74 68   is stored by th
34b0a 65 20 65 61 63 68 20 73 75 62 74 72 65 65 2e 20  e each subtree. 
34b0b 20 49 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64   Interior.** nod
34b0c 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75  es are written u
34b0d 73 69 6e 67 20 49 6e 74 65 72 69 6f 72 57 72 69  sing InteriorWri
34b0e 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20 75 73  ter, and read us
34b0f 69 6e 67 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 52  ing.** InteriorR
34b10 65 61 64 65 72 2e 20 20 49 6e 74 65 72 69 6f 72  eader.  Interior
34b11 57 72 69 74 65 72 73 20 61 72 65 20 63 72 65 61  Writers are crea
34b12 74 65 64 20 61 73 20 6e 65 65 64 65 64 20 77 68  ted as needed wh
34b13 65 6e 0a 2a 2a 20 53 65 67 6d 65 6e 74 57 72 69  en.** SegmentWri
34b14 74 65 72 20 63 72 65 61 74 65 73 20 6e 65 77 20  ter creates new 
34b15 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f 72 20 77  leaf nodes, or w
34b16 68 65 6e 20 61 6e 20 69 6e 74 65 72 69 6f 72 20  hen an interior 
34b17 6e 6f 64 65 0a 2a 2a 20 69 74 73 65 6c 66 20 67  node.** itself g
34b18 72 6f 77 73 20 74 6f 6f 20 62 69 67 20 61 6e 64  rows too big and
34b19 20 6d 75 73 74 20 62 65 20 73 70 6c 69 74 2e 20   must be split. 
34b1a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 69   The format of i
34b1b 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73  nterior.** nodes
34b1c 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69  :.**.** varint i
34b1d 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  Height;         
34b1e 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d 20 6c    (height from l
34b1f 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77 61 79  eaf level, alway
34b20 73 20 3e 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20  s >0).** varint 
34b21 69 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20 20  iBlockid;       
34b22 20 20 20 28 62 6c 6f 63 6b 20 69 64 20 6f 66 20     (block id of 
34b23 6e 6f 64 65 27 73 20 6c 65 66 74 6d 6f 73 74 20  node's leftmost 
34b24 73 75 62 74 72 65 65 29 0a 2a 2a 20 6f 70 74 69  subtree).** opti
34b25 6f 6e 61 6c 20 7b 0a 2a 2a 20 20 20 76 61 72 69  onal {.**   vari
34b26 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
34b27 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 66      (length of f
34b28 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 20 20  irst term).**   
34b29 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
34b2a 5d 3b 20 20 20 20 20 20 28 63 6f 6e 74 65 6e 74  ];      (content
34b2b 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a   of first term).
34b2c 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20  **   array {.** 
34b2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
34b2f 66 75 72 74 68 65 72 20 74 65 72 6d 73 20 61 72  further terms ar
34b30 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29  e delta-encoded)
34b31 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e  .**     varint n
34b32 50 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20  Prefix;         
34b33 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 73 68     (length of sh
34b34 61 72 65 64 20 70 72 65 66 69 78 20 77 69 74 68  ared prefix with
34b35 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d 29 0a   previous term).
34b36 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e 53  **     varint nS
34b37 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  uffix;          
34b38 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73    (length of uns
34b39 68 61 72 65 64 20 73 75 66 66 69 78 29 0a 2a 2a  hared suffix).**
34b3a 20 20 20 20 20 63 68 61 72 20 70 54 65 72 6d 53       char pTermS
34b3b 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20  uffix[nSuffix]; 
34b3c 28 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78  (unshared suffix
34b3d 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a   of next term).*
34b3e 2a 20 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  *   }.** }.**.**
34b3f 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e 61 6c 20   Here, optional 
34b40 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61 6e 20 6f  { X } means an o
34b41 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65 6e 74 2c  ptional element,
34b42 20 77 68 69 6c 65 20 61 72 72 61 79 20 7b 20 58   while array { X
34b43 20 7d 0a 2a 2a 20 6d 65 61 6e 73 20 7a 65 72 6f   }.** means zero
34b44 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
34b45 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
34b46 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a  ent in memory..*
34b47 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 72 69 6f 72  *.** An interior
34b48 20 6e 6f 64 65 20 65 6e 63 6f 64 65 73 20 6e 20   node encodes n 
34b49 74 65 72 6d 73 20 73 65 70 61 72 61 74 69 6e 67  terms separating
34b4a 20 6e 2b 31 20 73 75 62 74 72 65 65 73 2e 20 20   n+1 subtrees.  
34b4b 54 68 65 0a 2a 2a 20 73 75 62 74 72 65 65 20 62  The.** subtree b
34b4c 6c 6f 63 6b 73 20 61 72 65 20 63 6f 6e 74 69 67  locks are contig
34b4d 75 6f 75 73 2c 20 73 6f 20 6f 6e 6c 79 20 74 68  uous, so only th
34b4e 65 20 66 69 72 73 74 20 73 75 62 74 72 65 65 27  e first subtree'
34b4f 73 20 62 6c 6f 63 6b 69 64 0a 2a 2a 20 69 73 20  s blockid.** is 
34b50 65 6e 63 6f 64 65 64 2e 20 20 54 68 65 20 73 75  encoded.  The su
34b51 62 74 72 65 65 20 61 74 20 69 42 6c 6f 63 6b 69  btree at iBlocki
34b52 64 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61  d will contain a
34b53 6c 6c 20 74 65 72 6d 73 20 6c 65 73 73 0a 2a 2a  ll terms less.**
34b54 20 74 68 61 6e 20 74 68 65 20 66 69 72 73 74 20   than the first 
34b55 74 65 72 6d 20 65 6e 63 6f 64 65 64 20 28 6f 72  term encoded (or
34b56 20 61 6c 6c 20 74 65 72 6d 73 20 69 66 20 6e 6f   all terms if no
34b57 20 74 65 72 6d 20 69 73 20 65 6e 63 6f 64 65 64   term is encoded
34b58 29 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  )..** Otherwise,
34b59 20 66 6f 72 20 74 65 72 6d 73 20 67 72 65 61 74   for terms great
34b5a 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
34b5b 20 74 6f 20 70 54 65 72 6d 5b 69 5d 20 62 75 74   to pTerm[i] but
34b5c 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 70 54   less.** than pT
34b5d 65 72 6d 5b 69 2b 31 5d 2c 20 74 68 65 20 73 75  erm[i+1], the su
34b5e 62 74 72 65 65 20 66 6f 72 20 74 68 61 74 20 74  btree for that t
34b5f 65 72 6d 20 77 69 6c 6c 20 62 65 20 72 6f 6f 74  erm will be root
34b60 65 64 20 61 74 0a 2a 2a 20 69 42 6c 6f 63 6b 69  ed at.** iBlocki
34b61 64 2b 69 2e 20 20 49 6e 74 65 72 69 6f 72 20 6e  d+i.  Interior n
34b62 6f 64 65 73 20 6f 6e 6c 79 20 73 74 6f 72 65 20  odes only store 
34b63 65 6e 6f 75 67 68 20 74 65 72 6d 20 64 61 74 61  enough term data
34b64 20 74 6f 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69   to.** distingui
34b65 73 68 20 61 64 6a 61 63 65 6e 74 20 63 68 69 6c  sh adjacent chil
34b66 64 72 65 6e 20 28 69 66 20 74 68 65 20 72 69 67  dren (if the rig
34b67 68 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74  htmost term of t
34b68 68 65 20 6c 65 66 74 0a 2a 2a 20 63 68 69 6c 64  he left.** child
34b69 20 69 73 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c   is "something",
34b6a 20 61 6e 64 20 74 68 65 20 6c 65 66 74 6d 6f 73   and the leftmos
34b6b 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 72 69  t term of the ri
34b6c 67 68 74 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20  ght child is.** 
34b6d 22 77 69 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22  "wicked", only "
34b6e 77 22 20 69 73 20 73 74 6f 72 65 64 29 2e 0a 2a  w" is stored)..*
34b6f 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20 69 73  *.** New data is
34b70 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65   spilled to a ne
34b71 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  w interior node 
34b72 61 74 20 74 68 65 20 73 61 6d 65 20 68 65 69 67  at the same heig
34b73 68 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63  ht when.** the c
34b74 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 78 63 65  urrent node exce
34b75 65 64 73 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58  eds INTERIOR_MAX
34b76 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
34b77 32 30 34 38 29 2e 0a 2a 2a 20 49 4e 54 45 52 49  2048)..** INTERI
34b78 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 28 64 65  OR_MIN_TERMS (de
34b79 66 61 75 6c 74 20 37 29 20 6b 65 65 70 73 20 6c  fault 7) keeps l
34b7a 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f 6d 20  arge terms from 
34b7b 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0a 2a 2a 20  monopolizing.** 
34b7c 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61  interior nodes a
34b7d 6e 64 20 6d 61 6b 69 6e 67 20 74 68 65 20 74 72  nd making the tr
34b7e 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79 2e 20 20  ee too skinny.  
34b7f 54 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  The interior nod
34b80 65 73 0a 2a 2a 20 61 74 20 61 20 67 69 76 65 6e  es.** at a given
34b81 20 68 65 69 67 68 74 20 61 72 65 20 6e 61 74 75   height are natu
34b82 72 61 6c 6c 79 20 74 72 61 63 6b 65 64 20 62 79  rally tracked by
34b83 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
34b84 61 74 0a 2a 2a 20 68 65 69 67 68 74 2b 31 2c 20  at.** height+1, 
34b85 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
34b86 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 64 69  .**** Segment di
34b87 72 65 63 74 6f 72 79 20 2a 2a 2a 2a 0a 2a 2a 20  rectory ****.** 
34b88 54 68 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65  The segment dire
34b89 63 74 6f 72 79 20 69 6e 20 74 61 62 6c 65 20 25  ctory in table %
34b8a 5f 73 65 67 64 69 72 20 73 74 6f 72 65 73 20 6d  _segdir stores m
34b8b 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
34b8c 66 6f 72 0a 2a 2a 20 6d 65 72 67 69 6e 67 20 61  for.** merging a
34b8d 6e 64 20 64 65 6c 65 74 69 6e 67 20 73 65 67 6d  nd deleting segm
34b8e 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f 20 74  ents, and also t
34b8f 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  he root node of 
34b90 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 27 73  the.** segment's
34b91 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   tree..**.** The
34b92 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20 74 68   root node is th
34b93 65 20 74 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68  e top node of th
34b94 65 20 73 65 67 6d 65 6e 74 27 73 20 74 72 65 65  e segment's tree
34b95 20 61 66 74 65 72 20 65 6e 63 6f 64 69 6e 67 0a   after encoding.
34b96 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 73 65  ** the entire se
34b97 67 6d 65 6e 74 2c 20 72 65 73 74 72 69 63 74 65  gment, restricte
34b98 64 20 74 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79  d to ROOT_MAX by
34b99 74 65 73 20 28 64 65 66 61 75 6c 74 20 31 30 32  tes (default 102
34b9a 34 29 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c  4)..** This coul
34b9b 64 20 62 65 20 65 69 74 68 65 72 20 61 20 6c 65  d be either a le
34b9c 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e  af node or an in
34b9d 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66  terior node.  If
34b9e 20 74 68 65 20 74 6f 70 0a 2a 2a 20 6e 6f 64 65   the top.** node
34b9f 20 72 65 71 75 69 72 65 73 20 6d 6f 72 65 20 74   requires more t
34ba0 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74  han ROOT_MAX byt
34ba1 65 73 2c 20 69 74 20 69 73 20 66 6c 75 73 68 65  es, it is flushe
34ba2 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 0a  d to %_segments.
34ba3 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20 72 6f 6f  ** and a new roo
34ba4 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  t interior node 
34ba5 69 73 20 67 65 6e 65 72 61 74 65 64 20 28 77 68  is generated (wh
34ba6 69 63 68 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ich should alway
34ba7 73 20 66 69 74 0a 2a 2a 20 77 69 74 68 69 6e 20  s fit.** within 
34ba8 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61 75 73 65  ROOT_MAX because
34ba9 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64 73 20 73   it only needs s
34baa 70 61 63 65 20 66 6f 72 20 32 20 76 61 72 69 6e  pace for 2 varin
34bab 74 73 2c 20 74 68 65 0a 2a 2a 20 68 65 69 67 68  ts, the.** heigh
34bac 74 20 61 6e 64 20 74 68 65 20 62 6c 6f 63 6b 69  t and the blocki
34bad 64 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  d of the previou
34bae 73 20 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 54  s root)..**.** T
34baf 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74  he meta-informat
34bb0 69 6f 6e 20 69 6e 20 74 68 65 20 73 65 67 6d 65  ion in the segme
34bb1 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 73 3a  nt directory is:
34bb2 0a 2a 2a 20 20 20 6c 65 76 65 6c 20 20 20 20 20  .**   level     
34bb3 20 20 20 20 20 20 20 20 20 20 2d 20 73 65 67 6d            - segm
34bb4 65 6e 74 20 6c 65 76 65 6c 20 28 73 65 65 20 62  ent level (see b
34bb5 65 6c 6f 77 29 0a 2a 2a 20 20 20 69 64 78 20 20  elow).**   idx  
34bb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
34bb7 20 69 6e 64 65 78 20 77 69 74 68 69 6e 20 6c 65   index within le
34bb8 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  vel.**          
34bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 28               - (
34bba 6c 65 76 65 6c 2c 69 64 78 20 75 6e 69 71 75 65  level,idx unique
34bbb 6c 79 20 69 64 65 6e 74 69 66 79 20 61 20 73 65  ly identify a se
34bbc 67 6d 65 6e 74 29 0a 2a 2a 20 20 20 73 74 61 72  gment).**   star
34bbd 74 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20  t_block         
34bbe 2d 20 66 69 72 73 74 20 6c 65 61 66 20 6e 6f 64  - first leaf nod
34bbf 65 0a 2a 2a 20 20 20 6c 65 61 76 65 73 5f 65 6e  e.**   leaves_en
34bc0 64 5f 62 6c 6f 63 6b 20 20 20 20 2d 20 6c 61 73  d_block    - las
34bc1 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20  t leaf node.**  
34bc2 20 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 20 20   end_block      
34bc3 20 20 20 20 20 2d 20 6c 61 73 74 20 62 6c 6f 63       - last bloc
34bc4 6b 20 28 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74  k (including int
34bc5 65 72 69 6f 72 20 6e 6f 64 65 73 29 0a 2a 2a 20  erior nodes).** 
34bc6 20 20 72 6f 6f 74 20 20 20 20 20 20 20 20 20 20    root          
34bc7 20 20 20 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73        - contents
34bc8 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65 0a 2a 2a   of root node.**
34bc9 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 20  .** If the root 
34bca 6e 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e  node is a leaf n
34bcb 6f 64 65 2c 20 74 68 65 6e 20 73 74 61 72 74 5f  ode, then start_
34bcc 62 6c 6f 63 6b 2c 0a 2a 2a 20 6c 65 61 76 65 73  block,.** leaves
34bcd 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20  _end_block, and 
34bce 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c  end_block are al
34bcf 6c 20 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20  l 0..**.**.**** 
34bd0 53 65 67 6d 65 6e 74 20 6d 65 72 67 69 6e 67 20  Segment merging 
34bd1 2a 2a 2a 2a 0a 2a 2a 20 54 6f 20 61 6d 6f 72 74  ****.** To amort
34bd2 69 7a 65 20 75 70 64 61 74 65 20 63 6f 73 74 73  ize update costs
34bd3 2c 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 67  , segments are g
34bd4 72 6f 75 70 65 64 20 69 6e 74 6f 20 6c 65 76 65  rouped into leve
34bd5 6c 73 20 61 6e 64 0a 2a 2a 20 6d 65 72 67 65 64  ls and.** merged
34bd6 20 69 6e 20 62 61 74 63 68 65 73 2e 20 20 45 61   in batches.  Ea
34bd7 63 68 20 69 6e 63 72 65 61 73 65 20 69 6e 20 6c  ch increase in l
34bd8 65 76 65 6c 20 72 65 70 72 65 73 65 6e 74 73 20  evel represents 
34bd9 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79 0a 2a 2a  exponentially.**
34bda 20 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74 73 2e   more documents.
34bdb 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 6f 63 75 6d  .**.** New docum
34bdc 65 6e 74 73 20 28 61 63 74 75 61 6c 6c 79 2c 20  ents (actually, 
34bdd 64 6f 63 75 6d 65 6e 74 20 75 70 64 61 74 65 73  document updates
34bde 29 20 61 72 65 20 74 6f 6b 65 6e 69 7a 65 64 20  ) are tokenized 
34bdf 61 6e 64 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69  and.** written i
34be0 6e 64 69 76 69 64 75 61 6c 6c 79 20 28 75 73 69  ndividually (usi
34be1 6e 67 20 4c 65 61 66 57 72 69 74 65 72 29 20 74  ng LeafWriter) t
34be2 6f 20 61 20 6c 65 76 65 6c 20 30 20 73 65 67 6d  o a level 0 segm
34be3 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 69 6e 63  ent, with.** inc
34be4 72 65 6d 65 6e 74 69 6e 67 20 69 64 78 2e 20 20  rementing idx.  
34be5 57 68 65 6e 20 69 64 78 20 72 65 61 63 68 65 73  When idx reaches
34be6 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 28 64 65   MERGE_COUNT (de
34be7 66 61 75 6c 74 20 31 36 29 2c 20 61 6c 6c 0a 2a  fault 16), all.*
34be8 2a 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e  * level 0 segmen
34be9 74 73 20 61 72 65 20 6d 65 72 67 65 64 20 69 6e  ts are merged in
34bea 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65  to a single leve
34beb 6c 20 31 20 73 65 67 6d 65 6e 74 2e 20 20 4c 65  l 1 segment.  Le
34bec 76 65 6c 20 31 0a 2a 2a 20 69 73 20 70 6f 70 75  vel 1.** is popu
34bed 6c 61 74 65 64 20 6c 69 6b 65 20 6c 65 76 65 6c  lated like level
34bee 20 30 2c 20 61 6e 64 20 65 76 65 6e 74 75 61 6c   0, and eventual
34bef 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6c  ly MERGE_COUNT l
34bf0 65 76 65 6c 20 31 0a 2a 2a 20 73 65 67 6d 65 6e  evel 1.** segmen
34bf1 74 73 20 61 72 65 20 6d 65 72 67 65 64 20 74 6f  ts are merged to
34bf2 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20   a single level 
34bf3 32 20 73 65 67 6d 65 6e 74 20 28 72 65 70 72 65  2 segment (repre
34bf4 73 65 6e 74 69 6e 67 0a 2a 2a 20 4d 45 52 47 45  senting.** MERGE
34bf5 5f 43 4f 55 4e 54 5e 32 20 75 70 64 61 74 65 73  _COUNT^2 updates
34bf6 29 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a  ), and so on..**
34bf7 0a 2a 2a 20 41 20 73 65 67 6d 65 6e 74 20 6d 65  .** A segment me
34bf8 72 67 65 20 74 72 61 76 65 72 73 65 73 20 61 6c  rge traverses al
34bf9 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 61 20  l segments at a 
34bfa 67 69 76 65 6e 20 6c 65 76 65 6c 20 69 6e 0a 2a  given level in.*
34bfb 2a 20 70 61 72 61 6c 6c 65 6c 2c 20 70 65 72 66  * parallel, perf
34bfc 6f 72 6d 69 6e 67 20 61 20 73 74 72 61 69 67 68  orming a straigh
34bfd 74 66 6f 72 77 61 72 64 20 73 6f 72 74 65 64 20  tforward sorted 
34bfe 6d 65 72 67 65 2e 20 20 53 69 6e 63 65 20 73 65  merge.  Since se
34bff 67 6d 65 6e 74 0a 2a 2a 20 6c 65 61 66 20 6e 6f  gment.** leaf no
34c00 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20  des are written 
34c01 69 6e 20 74 6f 20 74 68 65 20 25 5f 73 65 67 6d  in to the %_segm
34c02 65 6e 74 73 20 74 61 62 6c 65 20 69 6e 20 6f 72  ents table in or
34c03 64 65 72 2c 20 74 68 69 73 0a 2a 2a 20 6d 65 72  der, this.** mer
34c04 67 65 20 74 72 61 76 65 72 73 65 73 20 74 68 65  ge traverses the
34c05 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 71 6c 69   underlying sqli
34c06 74 65 20 64 69 73 6b 20 73 74 72 75 63 74 75 72  te disk structur
34c07 65 73 20 65 66 66 69 63 69 65 6e 74 6c 79 2e 0a  es efficiently..
34c08 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6d 65 72  ** After the mer
34c09 67 65 2c 20 61 6c 6c 20 73 65 67 6d 65 6e 74 20  ge, all segment 
34c0a 62 6c 6f 63 6b 73 20 66 72 6f 6d 20 74 68 65 20  blocks from the 
34c0b 6d 65 72 67 65 64 20 6c 65 76 65 6c 20 61 72 65  merged level are
34c0c 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a  .** deleted..**.
34c0d 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63  ** MERGE_COUNT c
34c0e 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65  ontrols how ofte
34c0f 6e 20 77 65 20 6d 65 72 67 65 20 73 65 67 6d 65  n we merge segme
34c10 6e 74 73 2e 20 20 31 36 20 73 65 65 6d 73 20 74  nts.  16 seems t
34c11 6f 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74  o be.** somewhat
34c12 20 6f 66 20 61 20 73 77 65 65 74 20 73 70 6f 74   of a sweet spot
34c13 20 66 6f 72 20 69 6e 73 65 72 74 69 6f 6e 20 70   for insertion p
34c14 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 33 32 20  erformance.  32 
34c15 61 6e 64 20 36 34 20 73 68 6f 77 0a 2a 2a 20 76  and 64 show.** v
34c16 65 72 79 20 73 69 6d 69 6c 61 72 20 70 65 72 66  ery similar perf
34c17 6f 72 6d 61 6e 63 65 20 6e 75 6d 62 65 72 73 20  ormance numbers 
34c18 74 6f 20 31 36 20 6f 6e 20 69 6e 73 65 72 74 69  to 16 on inserti
34c19 6f 6e 2c 20 74 68 6f 75 67 68 20 74 68 65 79 27  on, though they'
34c1a 72 65 0a 2a 2a 20 61 20 74 69 6e 79 20 62 69 74  re.** a tiny bit
34c1b 20 73 6c 6f 77 65 72 20 28 70 65 72 68 61 70 73   slower (perhaps
34c1c 20 64 75 65 20 74 6f 20 6d 6f 72 65 20 6f 76 65   due to more ove
34c1d 72 68 65 61 64 20 69 6e 20 6d 65 72 67 65 2d 74  rhead in merge-t
34c1e 69 6d 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 29 2e  ime.** sorting).
34c1f 20 20 38 20 69 73 20 61 62 6f 75 74 20 32 30 25    8 is about 20%
34c20 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31 36 2c   slower than 16,
34c21 20 34 20 61 62 6f 75 74 20 35 30 25 20 73 6c 6f   4 about 50% slo
34c22 77 65 72 20 74 68 61 6e 0a 2a 2a 20 31 36 2c 20  wer than.** 16, 
34c23 32 20 61 62 6f 75 74 20 36 36 25 20 73 6c 6f 77  2 about 66% slow
34c24 65 72 20 74 68 61 6e 20 31 36 2e 0a 2a 2a 0a 2a  er than 16..**.*
34c25 2a 20 41 74 20 71 75 65 72 79 20 74 69 6d 65 2c  * At query time,
34c26 20 68 69 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e   high MERGE_COUN
34c27 54 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20  T increases the 
34c28 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e  number of segmen
34c29 74 73 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64  ts.** which need
34c2a 20 74 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 61   to be scanned a
34c2b 6e 64 20 6d 65 72 67 65 64 2e 20 20 46 6f 72 20  nd merged.  For 
34c2c 69 6e 73 74 61 6e 63 65 2c 20 77 69 74 68 20 31  instance, with 1
34c2d 30 30 6b 20 64 6f 63 73 0a 2a 2a 20 69 6e 73 65  00k docs.** inse
34c2e 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  rted:.**.**    M
34c2f 45 52 47 45 5f 43 4f 55 4e 54 20 20 20 73 65 67  ERGE_COUNT   seg
34c30 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 31  ments.**       1
34c31 36 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 2a  6           25.*
34c32 2a 20 20 20 20 20 20 20 20 38 20 20 20 20 20 20  *        8      
34c33 20 20 20 20 20 31 32 0a 2a 2a 20 20 20 20 20 20       12.**      
34c34 20 20 34 20 20 20 20 20 20 20 20 20 20 20 31 30    4           10
34c35 0a 2a 2a 20 20 20 20 20 20 20 20 32 20 20 20 20  .**        2    
34c36 20 20 20 20 20 20 20 20 36 0a 2a 2a 0a 2a 2a 20          6.**.** 
34c37 54 68 69 73 20 61 70 70 65 61 72 73 20 74 6f 20  This appears to 
34c38 68 61 76 65 20 6f 6e 6c 79 20 61 20 6d 6f 64 65  have only a mode
34c39 72 61 74 65 20 69 6d 70 61 63 74 20 6f 6e 20 71  rate impact on q
34c3a 75 65 72 69 65 73 20 66 6f 72 20 76 65 72 79 0a  ueries for very.
34c3b 2a 2a 20 66 72 65 71 75 65 6e 74 20 74 65 72 6d  ** frequent term
34c3c 73 20 28 77 68 69 63 68 20 61 72 65 20 73 6f 6d  s (which are som
34c3d 65 77 68 61 74 20 64 6f 6d 69 6e 61 74 65 64 20  ewhat dominated 
34c3e 62 79 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65  by segment merge
34c3f 0a 2a 2a 20 63 6f 73 74 73 29 2c 20 61 6e 64 20  .** costs), and 
34c40 69 6e 66 72 65 71 75 65 6e 74 20 61 6e 64 20 6e  infrequent and n
34c41 6f 6e 2d 65 78 69 73 74 65 6e 74 20 74 65 72 6d  on-existent term
34c42 73 20 73 74 69 6c 6c 20 73 65 65 6d 20 74 6f 20  s still seem to 
34c43 62 65 20 66 61 73 74 0a 2a 2a 20 65 76 65 6e 20  be fast.** even 
34c44 77 69 74 68 20 6d 61 6e 79 20 73 65 67 6d 65 6e  with many segmen
34c45 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73  ts..**.** TODO(s
34c46 68 65 73 73 29 20 54 68 61 74 20 73 61 69 64 2c  hess) That said,
34c47 20 69 74 20 77 6f 75 6c 64 20 62 65 20 6e 69 63   it would be nic
34c48 65 20 74 6f 20 68 61 76 65 20 61 20 62 65 74 74  e to have a bett
34c49 65 72 20 71 75 65 72 79 2d 73 69 64 65 0a 2a 2a  er query-side.**
34c4a 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45   argument for ME
34c4b 52 47 45 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e  RGE_COUNT of 16.
34c4c 20 20 41 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f    Also, it is po
34c4d 73 73 69 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68  ssible/likely th
34c4e 61 74 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69  at.** optimizati
34c4f 6f 6e 73 20 74 6f 20 74 68 69 6e 67 73 20 6c 69  ons to things li
34c50 6b 65 20 64 6f 63 6c 69 73 74 20 6d 65 72 67 69  ke doclist mergi
34c51 6e 67 20 77 69 6c 6c 20 73 77 69 6e 67 20 74 68  ng will swing th
34c52 65 20 73 77 65 65 74 0a 2a 2a 20 73 70 6f 74 20  e sweet.** spot 
34c53 61 72 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a  around..**.**.**
34c54 0a 2a 2a 2a 2a 20 48 61 6e 64 6c 69 6e 67 20 6f  .**** Handling o
34c55 66 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20  f deletions and 
34c56 75 70 64 61 74 65 73 20 2a 2a 2a 2a 0a 2a 2a 20  updates ****.** 
34c57 53 69 6e 63 65 20 77 65 27 72 65 20 75 73 69 6e  Since we're usin
34c58 67 20 61 20 73 65 67 6d 65 6e 74 65 64 20 73 74  g a segmented st
34c59 72 75 63 74 75 72 65 2c 20 77 69 74 68 20 6e 6f  ructure, with no
34c5a 20 64 6f 63 69 64 2d 6f 72 69 65 6e 74 65 64 0a   docid-oriented.
34c5b 2a 2a 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68  ** index into th
34c5c 65 20 74 65 72 6d 20 69 6e 64 65 78 2c 20 77 65  e term index, we
34c5d 20 63 6c 65 61 72 6c 79 20 63 61 6e 6e 6f 74 20   clearly cannot 
34c5e 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20 74 68  simply update th
34c5f 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 64 65 78 20  e term.** index 
34c60 77 68 65 6e 20 61 20 64 6f 63 75 6d 65 6e 74 20  when a document 
34c61 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20 75 70  is deleted or up
34c62 64 61 74 65 64 2e 20 20 46 6f 72 20 64 65 6c 65  dated.  For dele
34c63 74 69 6f 6e 73 2c 20 77 65 0a 2a 2a 20 77 72 69  tions, we.** wri
34c64 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c  te an empty docl
34c65 69 73 74 20 28 76 61 72 69 6e 74 28 64 6f 63 69  ist (varint(doci
34c66 64 29 20 76 61 72 69 6e 74 28 50 4f 53 5f 45 4e  d) varint(POS_EN
34c67 44 29 29 2c 20 66 6f 72 20 75 70 64 61 74 65 73  D)), for updates
34c68 0a 2a 2a 20 77 65 20 73 69 6d 70 6c 79 20 77 72  .** we simply wr
34c69 69 74 65 20 74 68 65 20 6e 65 77 20 64 6f 63 6c  ite the new docl
34c6a 69 73 74 2e 20 20 53 65 67 6d 65 6e 74 20 6d 65  ist.  Segment me
34c6b 72 67 65 73 20 6f 76 65 72 77 72 69 74 65 20 6f  rges overwrite o
34c6c 6c 64 65 72 0a 2a 2a 20 64 61 74 61 20 66 6f 72  lder.** data for
34c6d 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 6f   a particular do
34c6e 63 69 64 20 77 69 74 68 20 6e 65 77 65 72 20 64  cid with newer d
34c6f 61 74 61 2c 20 73 6f 20 64 65 6c 65 74 65 73 20  ata, so deletes 
34c70 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a 20 77 69  or updates.** wi
34c71 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 6f 76  ll eventually ov
34c72 65 72 74 61 6b 65 20 74 68 65 20 65 61 72 6c 69  ertake the earli
34c73 65 72 20 64 61 74 61 20 61 6e 64 20 6b 6e 6f 63  er data and knoc
34c74 6b 20 69 74 20 6f 75 74 2e 20 20 54 68 65 0a 2a  k it out.  The.*
34c75 2a 20 71 75 65 72 79 20 6c 6f 67 69 63 20 6c 69  * query logic li
34c76 6b 65 77 69 73 65 20 6d 65 72 67 65 73 20 64 6f  kewise merges do
34c77 63 6c 69 73 74 73 20 73 6f 20 74 68 61 74 20 6e  clists so that n
34c78 65 77 65 72 20 64 61 74 61 20 6b 6e 6f 63 6b 73  ewer data knocks
34c79 20 6f 75 74 0a 2a 2a 20 6f 6c 64 65 72 20 64 61   out.** older da
34c7a 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73  ta..**.** TODO(s
34c7b 68 65 73 73 29 20 50 72 6f 76 69 64 65 20 61 20  hess) Provide a 
34c7c 56 41 43 55 55 4d 20 74 79 70 65 20 6f 70 65 72  VACUUM type oper
34c7d 61 74 69 6f 6e 20 74 6f 20 63 6c 65 61 72 20 6f  ation to clear o
34c7e 75 74 20 61 6c 6c 0a 2a 2a 20 64 65 6c 65 74 69  ut all.** deleti
34c7f 6f 6e 73 20 61 6e 64 20 64 75 70 6c 69 63 61 74  ons and duplicat
34c80 69 6f 6e 73 2e 20 20 54 68 69 73 20 77 6f 75 6c  ions.  This woul
34c81 64 20 62 61 73 69 63 61 6c 6c 79 20 62 65 20 61  d basically be a
34c82 20 66 6f 72 63 65 64 20 6d 65 72 67 65 0a 2a 2a   forced merge.**
34c83 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
34c84 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 0a 23 69 66 20  egment..*/..#if 
34c85 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
34c86 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
34c87 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
34c88 54 53 33 29 0a 0a 23 69 66 20 64 65 66 69 6e 65  TS3)..#if define
34c89 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
34c8a 46 54 53 33 29 20 26 26 20 21 64 65 66 69 6e 65  FTS3) && !define
34c8b 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 0a 23  d(SQLITE_CORE).#
34c8c 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43   define SQLITE_C
34c8d 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 23 69  ORE 1.#endif..#i
34c8e 6e 63 6c 75 64 65 20 22 66 74 73 33 49 6e 74 2e  nclude "fts3Int.
34c8f 68 22 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  h"...#ifndef SQL
34c90 49 54 45 5f 43 4f 52 45 20 0a 20 20 53 51 4c 49  ITE_CORE .  SQLI
34c91 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
34c92 54 31 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 20  T1.#endif..../* 
34c93 54 4f 44 4f 28 73 68 65 73 73 29 20 4d 41 4e 2c  TODO(shess) MAN,
34c94 20 74 68 69 73 20 74 68 69 6e 67 20 6e 65 65 64   this thing need
34c95 73 20 73 6f 6d 65 20 72 65 66 61 63 74 6f 72 69  s some refactori
34c96 6e 67 2e 20 20 41 74 20 6d 69 6e 69 6d 75 6d 2c  ng.  At minimum,
34c97 20 69 74 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20   it.** would be 
34c98 6e 69 63 65 20 74 6f 20 6f 72 64 65 72 20 74 68  nice to order th
34c99 65 20 66 69 6c 65 20 62 65 74 74 65 72 2c 20 70  e file better, p
34c9a 65 72 68 61 70 73 20 73 6f 6d 65 74 68 69 6e 67  erhaps something
34c9b 20 61 6c 6f 6e 67 20 74 68 65 0a 2a 2a 20 6c 69   along the.** li
34c9c 6e 65 73 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 2d  nes of:.**.**  -
34c9d 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f   utility functio
34c9e 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20 73  ns.**  - table s
34c9f 65 74 75 70 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  etup functions.*
34ca0 2a 20 20 2d 20 74 61 62 6c 65 20 75 70 64 61 74  *  - table updat
34ca1 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20  e functions.**  
34ca2 2d 20 74 61 62 6c 65 20 71 75 65 72 79 20 66 75  - table query fu
34ca3 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 50 75  nctions.**.** Pu
34ca4 74 20 74 68 65 20 71 75 65 72 79 20 66 75 6e 63  t the query func
34ca5 74 69 6f 6e 73 20 6c 61 73 74 20 62 65 63 61 75  tions last becau
34ca6 73 65 20 74 68 65 79 27 72 65 20 6c 69 6b 65 6c  se they're likel
34ca7 79 20 74 6f 20 72 65 66 65 72 65 6e 63 65 0a 2a  y to reference.*
34ca8 2a 20 74 79 70 65 64 65 66 73 20 6f 72 20 66 75  * typedefs or fu
34ca9 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65  nctions from the
34caa 20 74 61 62 6c 65 20 75 70 64 61 74 65 20 73 65   table update se
34cab 63 74 69 6f 6e 2e 0a 2a 2f 0a 0a 23 69 66 20 30  ction..*/..#if 0
34cac 0a 23 20 64 65 66 69 6e 65 20 46 54 53 54 52 41  .# define FTSTRA
34cad 43 45 28 41 29 20 20 70 72 69 6e 74 66 20 41 3b  CE(A)  printf A;
34cae 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 0a   fflush(stdout).
34caf 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 46  #else.# define F
34cb0 54 53 54 52 41 43 45 28 41 29 0a 23 65 6e 64 69  TSTRACE(A).#endi
34cb1 66 0a 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20  f..typedef enum 
34cb2 44 6f 63 4c 69 73 74 54 79 70 65 20 7b 0a 20 20  DocListType {.  
34cb3 44 4c 5f 44 4f 43 49 44 53 2c 20 20 20 20 20 20  DL_DOCIDS,      
34cb4 20 20 20 20 20 20 20 20 2f 2a 20 64 6f 63 69 64          /* docid
34cb5 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50  s only */.  DL_P
34cb6 4f 53 49 54 49 4f 4e 53 2c 20 20 20 20 20 20 20  OSITIONS,       
34cb7 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20      /* docids + 
34cb8 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 44  positions */.  D
34cb9 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
34cba 45 54 53 20 20 20 20 2f 2a 20 64 6f 63 69 64 73  ETS    /* docids
34cbb 20 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2b 20 6f   + positions + o
34cbc 66 66 73 65 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c  ffsets */.} DocL
34cbd 69 73 74 54 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20  istType;../*.** 
34cbe 42 79 20 64 65 66 61 75 6c 74 2c 20 6f 6e 6c 79  By default, only
34cbf 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6e   positions and n
34cc0 6f 74 20 6f 66 66 73 65 74 73 20 61 72 65 20 73  ot offsets are s
34cc1 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 6f 63  tored in the doc
34cc2 6c 69 73 74 73 2e 0a 2a 2a 20 54 6f 20 63 68 61  lists..** To cha
34cc3 6e 67 65 20 74 68 69 73 20 73 6f 20 74 68 61 74  nge this so that
34cc4 20 6f 66 66 73 65 74 73 20 61 72 65 20 73 74 6f   offsets are sto
34cc5 72 65 64 20 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65  red too, compile
34cc6 20 77 69 74 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20   with.**.**     
34cc7 20 20 20 20 20 2d 44 44 4c 5f 44 45 46 41 55 4c       -DDL_DEFAUL
34cc8 54 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  T=DL_POSITIONS_O
34cc9 46 46 53 45 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20  FFSETS.**.** If 
34cca 44 4c 5f 44 45 46 41 55 4c 54 20 69 73 20 73 65  DL_DEFAULT is se
34ccb 74 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53 2c 20  t to DL_DOCIDS, 
34ccc 79 6f 75 72 20 74 61 62 6c 65 20 63 61 6e 20 6f  your table can o
34ccd 6e 6c 79 20 62 65 20 69 6e 73 65 72 74 65 64 0a  nly be inserted.
34cce 2a 2a 20 69 6e 74 6f 20 28 6e 6f 20 64 65 6c 65  ** into (no dele
34ccf 74 65 73 20 6f 72 20 75 70 64 61 74 65 73 29 2e  tes or updates).
34cd0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 44 4c 5f 44  .*/.#ifndef DL_D
34cd1 45 46 41 55 4c 54 0a 23 20 64 65 66 69 6e 65 20  EFAULT.# define 
34cd2 44 4c 5f 44 45 46 41 55 4c 54 20 44 4c 5f 50 4f  DL_DEFAULT DL_PO
34cd3 53 49 54 49 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a  SITIONS.#endif..
34cd4 65 6e 75 6d 20 7b 0a 20 20 50 4f 53 5f 45 4e 44  enum {.  POS_END
34cd5 20 3d 20 30 2c 20 20 20 20 20 20 20 20 2f 2a 20   = 0,        /* 
34cd6 65 6e 64 20 6f 66 20 74 68 69 73 20 70 6f 73 69  end of this posi
34cd7 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 50  tion list */.  P
34cd8 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 20 20 20 20 20  OS_COLUMN,      
34cd9 20 20 20 2f 2a 20 66 6f 6c 6c 6f 77 65 64 20 62     /* followed b
34cda 79 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d  y new column num
34cdb 62 65 72 20 2a 2f 0a 20 20 50 4f 53 5f 42 41 53  ber */.  POS_BAS
34cdc 45 0a 7d 3b 0a 0a 2f 2a 20 75 74 69 6c 69 74 79  E.};../* utility
34cdd 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f   functions */../
34cde 2a 20 43 4c 45 41 52 28 29 20 61 6e 64 20 53 43  * CLEAR() and SC
34cdf 52 41 4d 42 4c 45 28 29 20 61 62 73 74 72 61 63  RAMBLE() abstrac
34ce0 74 20 6d 65 6d 73 65 74 28 29 20 6f 6e 20 61 20  t memset() on a 
34ce1 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 69 6e  pointer to a sin
34ce2 67 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20 74 6f  gle.** record to
34ce3 20 70 72 65 76 65 6e 74 20 65 72 72 6f 72 73 20   prevent errors 
34ce4 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
34ce5 2a 2a 20 6d 79 5f 66 75 6e 63 74 69 6f 6e 28 53  ** my_function(S
34ce6 6f 6d 65 54 79 70 65 20 2a 62 29 7b 0a 2a 2a 20  omeType *b){.** 
34ce7 20 20 6d 65 6d 73 65 74 28 62 2c 20 27 5c 30 27    memset(b, '\0'
34ce8 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 20 20 2f  , sizeof(b));  /
34ce9 2f 20 73 69 7a 65 6f 66 28 62 29 21 3d 73 69 7a  / sizeof(b)!=siz
34cea 65 6f 66 28 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a  eof(*b).** }.*/.
34ceb 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 4f  /* TODO(shess) O
34cec 62 76 69 6f 75 73 20 63 61 6e 64 69 64 61 74 65  bvious candidate
34ced 73 20 66 6f 72 20 61 20 68 65 61 64 65 72 20 66  s for a header f
34cee 69 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ile. */.#define 
34cef 43 4c 45 41 52 28 62 29 20 6d 65 6d 73 65 74 28  CLEAR(b) memset(
34cf0 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28  b, '\0', sizeof(
34cf1 2a 28 62 29 29 29 0a 0a 23 69 66 6e 64 65 66 20  *(b)))..#ifndef 
34cf2 4e 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e 65  NDEBUG.#  define
34cf3 20 53 43 52 41 4d 42 4c 45 28 62 29 20 6d 65 6d   SCRAMBLE(b) mem
34cf4 73 65 74 28 62 2c 20 30 78 35 35 2c 20 73 69 7a  set(b, 0x55, siz
34cf5 65 6f 66 28 2a 28 62 29 29 29 0a 23 65 6c 73 65  eof(*(b))).#else
34cf6 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52 41 4d  .#  define SCRAM
34cf7 42 4c 45 28 62 29 0a 23 65 6e 64 69 66 0a 0a 2f  BLE(b).#endif../
34cf8 2a 20 0a 2a 2a 20 57 72 69 74 65 20 61 20 36 34  * .** Write a 64
34cf9 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65  -bit variable-le
34cfa 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20  ngth integer to 
34cfb 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20  memory starting 
34cfc 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20  at p[0]..** The 
34cfd 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77  length of data w
34cfe 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 62  ritten will be b
34cff 65 74 77 65 65 6e 20 31 20 61 6e 64 20 46 54 53  etween 1 and FTS
34d00 33 5f 56 41 52 49 4e 54 5f 4d 41 58 20 62 79 74  3_VARINT_MAX byt
34d01 65 73 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  es..** The numbe
34d02 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74  r of bytes writt
34d03 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  en is returned..
34d04 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
34d05 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
34d06 33 50 75 74 56 61 72 69 6e 74 28 63 68 61 72 20  3PutVarint(char 
34d07 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *p, sqlite_int64
34d08 20 76 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20   v){.  unsigned 
34d09 63 68 61 72 20 2a 71 20 3d 20 28 75 6e 73 69 67  char *q = (unsig
34d0a 6e 65 64 20 63 68 61 72 20 2a 29 20 70 3b 0a 20  ned char *) p;. 
34d0b 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 76   sqlite_uint64 v
34d0c 75 20 3d 20 76 3b 0a 20 20 64 6f 7b 0a 20 20 20  u = v;.  do{.   
34d0d 20 2a 71 2b 2b 20 3d 20 28 75 6e 73 69 67 6e 65   *q++ = (unsigne
34d0e 64 20 63 68 61 72 29 20 28 28 76 75 20 26 20 30  d char) ((vu & 0
34d0f 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20  x7f) | 0x80);.  
34d10 20 20 76 75 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77    vu >>= 7;.  }w
34d11 68 69 6c 65 28 20 76 75 21 3d 30 20 29 3b 0a 20  hile( vu!=0 );. 
34d12 20 71 5b 2d 31 5d 20 26 3d 20 30 78 37 66 3b 20   q[-1] &= 0x7f; 
34d13 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 68 69 67   /* turn off hig
34d14 68 20 62 69 74 20 69 6e 20 66 69 6e 61 6c 20 62  h bit in final b
34d15 79 74 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  yte */.  assert(
34d16 20 71 20 2d 20 28 75 6e 73 69 67 6e 65 64 20 63   q - (unsigned c
34d17 68 61 72 20 2a 29 70 20 3c 3d 20 46 54 53 33 5f  har *)p <= FTS3_
34d18 56 41 52 49 4e 54 5f 4d 41 58 20 29 3b 0a 20 20  VARINT_MAX );.  
34d19 72 65 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20  return (int) (q 
34d1a 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  - (unsigned char
34d1b 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a   *)p);.}../* .**
34d1c 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76   Read a 64-bit v
34d1d 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
34d1e 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f  nteger from memo
34d1f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ry starting at p
34d20 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74  [0]..** Return t
34d21 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
34d22 65 73 20 72 65 61 64 2c 20 6f 72 20 30 20 6f 6e  es read, or 0 on
34d23 20 65 72 72 6f 72 2e 0a 2a 2a 20 54 68 65 20 76   error..** The v
34d24 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69  alue is stored i
34d25 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  n *v..*/.SQLITE_
34d26 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
34d27 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74  te3Fts3GetVarint
34d28 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c 20  (const char *p, 
34d29 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 76 29  sqlite_int64 *v)
34d2a 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
34d2b 65 64 20 63 68 61 72 20 2a 71 20 3d 20 28 63 6f  ed char *q = (co
34d2c 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
34d2d 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74 65  r *) p;.  sqlite
34d2e 5f 75 69 6e 74 36 34 20 78 20 3d 20 30 2c 20 79  _uint64 x = 0, y
34d2f 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 28   = 1;.  while( (
34d30 2a 71 20 26 20 30 78 38 30 29 20 3d 3d 20 30 78  *q & 0x80) == 0x
34d31 38 30 20 29 7b 0a 20 20 20 20 78 20 2b 3d 20 79  80 ){.    x += y
34d32 20 2a 20 28 2a 71 2b 2b 20 26 20 30 78 37 66 29   * (*q++ & 0x7f)
34d33 3b 0a 20 20 20 20 79 20 3c 3c 3d 20 37 3b 0a 20  ;.    y <<= 7;. 
34d34 20 20 20 69 66 28 20 71 20 2d 20 28 75 6e 73 69     if( q - (unsi
34d35 67 6e 65 64 20 63 68 61 72 20 2a 29 70 20 3e 3d  gned char *)p >=
34d36 20 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58   FTS3_VARINT_MAX
34d37 20 29 7b 20 20 2f 2a 20 62 61 64 20 64 61 74 61   ){  /* bad data
34d38 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
34d39 28 20 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74  ( 0 );.      ret
34d3a 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
34d3b 0a 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b  .  x += y * (*q+
34d3c 2b 29 3b 0a 20 20 2a 76 20 3d 20 28 73 71 6c 69  +);.  *v = (sqli
34d3d 74 65 5f 69 6e 74 36 34 29 20 78 3b 0a 20 20 72  te_int64) x;.  r
34d3e 65 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d  eturn (int) (q -
34d3f 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
34d40 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  *)p);.}../*.** S
34d41 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65  imilar to sqlite
34d42 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 29  3Fts3GetVarint()
34d43 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  , except that th
34d44 65 20 6f 75 74 70 75 74 20 69 73 20 74 72 75 6e  e output is trun
34d45 63 61 74 65 64 20 74 6f 20 61 0a 2a 2a 20 33 32  cated to a.** 32
34d46 2d 62 69 74 20 69 6e 74 65 67 65 72 20 62 65 66  -bit integer bef
34d47 6f 72 65 20 69 74 20 69 73 20 72 65 74 75 72 6e  ore it is return
34d48 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
34d49 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
34d4a 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
34d4b 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c 20  (const char *p, 
34d4c 69 6e 74 20 2a 70 69 29 7b 0a 20 73 71 6c 69 74  int *pi){. sqlit
34d4d 65 5f 69 6e 74 36 34 20 69 3b 0a 20 69 6e 74 20  e_int64 i;. int 
34d4e 72 65 74 20 3d 20 73 71 6c 69 74 65 33 46 74 73  ret = sqlite3Fts
34d4f 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 69  3GetVarint(p, &i
34d50 29 3b 0a 20 2a 70 69 20 3d 20 28 69 6e 74 29 20  );. *pi = (int) 
34d51 69 3b 0a 20 61 73 73 65 72 74 28 20 2a 70 69 3d  i;. assert( *pi=
34d52 3d 69 20 29 3b 0a 20 72 65 74 75 72 6e 20 72 65  =i );. return re
34d53 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  t;.}../*.** Retu
34d54 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
34d55 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20   bytes required 
34d56 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c  to store the val
34d57 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ue passed as the
34d58 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75 6d 65  .** first argume
34d59 6e 74 20 69 6e 20 76 61 72 69 6e 74 20 66 6f 72  nt in varint for
34d5a 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  m..*/.SQLITE_PRI
34d5b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
34d5c 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 73 71  Fts3VarintLen(sq
34d5d 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 76 29 7b  lite3_uint64 v){
34d5e 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20  .  int i = 0;.  
34d5f 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20  do{.    i++;.   
34d60 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69   v >>= 7;.  }whi
34d61 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 72 65  le( v!=0 );.  re
34d62 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn i;.}../*.**
34d63 20 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d   Convert an SQL-
34d64 73 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72  style quoted str
34d65 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61  ing into a norma
34d66 6c 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f  l string by remo
34d67 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74  ving.** the quot
34d68 65 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54  e characters.  T
34d69 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  he conversion is
34d6a 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20   done in-place. 
34d6b 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74   If the.** input
34d6c 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20   does not begin 
34d6d 77 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61  with a quote cha
34d6e 72 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69  racter, then thi
34d6f 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20  s routine.** is 
34d70 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45  a no-op..**.** E
34d71 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  xamples:.**.**  
34d72 20 20 20 22 61 62 63 22 20 20 20 62 65 63 6f 6d     "abc"   becom
34d73 65 73 20 20 20 61 62 63 0a 2a 2a 20 20 20 20 20  es   abc.**     
34d74 27 78 79 7a 27 20 20 20 62 65 63 6f 6d 65 73 20  'xyz'   becomes 
34d75 20 20 78 79 7a 0a 2a 2a 20 20 20 20 20 5b 70 71    xyz.**     [pq
34d76 72 5d 20 20 20 62 65 63 6f 6d 65 73 20 20 20 70  r]   becomes   p
34d77 71 72 0a 2a 2a 20 20 20 20 20 60 6d 6e 6f 60 20  qr.**     `mno` 
34d78 20 20 62 65 63 6f 6d 65 73 20 20 20 6d 6e 6f 0a    becomes   mno.
34d79 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
34d7a 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
34d7b 73 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a  s3Dequote(char *
34d7c 7a 29 7b 0a 20 20 69 6e 74 20 71 75 6f 74 65 3b  z){.  int quote;
34d7d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 0a 20 20  .  int i, j;..  
34d7e 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20  quote = z[0];.  
34d7f 73 77 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b  switch( quote ){
34d80 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20  .    case '\'': 
34d81 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
34d82 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20   '"':   break;. 
34d83 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 62     case '`':   b
34d84 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20  reak;           
34d85 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51       /* For MySQ
34d86 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  L compatibility 
34d87 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a  */.    case '[':
34d88 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20     quote = ']'; 
34d89 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20   break;  /* For 
34d8a 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d  MS SqlServer com
34d8b 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20  patibility */.  
34d8c 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65    default:    re
34d8d 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
34d8e 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20  i=1, j=0; z[i]; 
34d8f 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b  i++){.    if( z[
34d90 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20  i]==quote ){.   
34d91 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71     if( z[i+1]==q
34d92 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  uote ){.        
34d93 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a  z[j++] = quote;.
34d94 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
34d95 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
34d96 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20    z[j++] = 0;.  
34d97 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
34d98 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
34d99 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
34d9a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  [i];.    }.  }.}
34d9b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  ..static void ft
34d9c 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
34d9d 28 63 68 61 72 20 2a 2a 70 70 2c 20 73 71 6c 69  (char **pp, sqli
34d9e 74 65 33 5f 69 6e 74 36 34 20 2a 70 56 61 6c 29  te3_int64 *pVal)
34d9f 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
34da0 34 20 69 56 61 6c 3b 0a 20 20 2a 70 70 20 2b 3d  4 iVal;.  *pp +=
34da1 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
34da2 61 72 69 6e 74 28 2a 70 70 2c 20 26 69 56 61 6c  arint(*pp, &iVal
34da3 29 3b 0a 20 20 2a 70 56 61 6c 20 2b 3d 20 69 56  );.  *pVal += iV
34da4 61 6c 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  al;.}..static vo
34da5 69 64 20 66 74 73 33 47 65 74 44 65 6c 74 61 56  id fts3GetDeltaV
34da6 61 72 69 6e 74 32 28 63 68 61 72 20 2a 2a 70 70  arint2(char **pp
34da7 2c 20 63 68 61 72 20 2a 70 45 6e 64 2c 20 73 71  , char *pEnd, sq
34da8 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 56 61  lite3_int64 *pVa
34da9 6c 29 7b 0a 20 20 69 66 28 20 2a 70 70 3e 3d 70  l){.  if( *pp>=p
34daa 45 6e 64 20 29 7b 0a 20 20 20 20 2a 70 70 20 3d  End ){.    *pp =
34dab 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
34dac 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
34dad 69 6e 74 28 70 70 2c 20 70 56 61 6c 29 3b 0a 20  int(pp, pVal);. 
34dae 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   }.}.../*.** The
34daf 20 46 74 73 33 43 75 72 73 6f 72 2e 65 54 79 70   Fts3Cursor.eTyp
34db0 65 20 6d 65 6d 62 65 72 20 69 73 20 61 6c 77 61  e member is alwa
34db1 79 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6f 66  ys set to one of
34db2 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 2e 0a   the following..
34db3 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f  */.#define FTS3_
34db4 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48 20  FULLSCAN_SEARCH 
34db5 30 20 20 20 20 2f 2a 20 4c 69 6e 65 61 72 20 73  0    /* Linear s
34db6 63 61 6e 20 6f 66 20 25 5f 63 6f 6e 74 65 6e 74  can of %_content
34db7 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e   table */.#defin
34db8 65 20 46 54 53 33 5f 44 4f 43 49 44 5f 53 45 41  e FTS3_DOCID_SEA
34db9 52 43 48 20 20 20 20 31 20 20 20 20 2f 2a 20 4c  RCH    1    /* L
34dba 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 20 6f  ookup by rowid o
34dbb 6e 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c  n %_content tabl
34dbc 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53  e */.#define FTS
34dbd 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45 41 52 43  3_FULLTEXT_SEARC
34dbe 48 20 32 20 20 20 20 2f 2a 20 46 75 6c 6c 2d 74  H 2    /* Full-t
34dbf 65 78 74 20 69 6e 64 65 78 20 73 65 61 72 63 68  ext index search
34dc0 20 2a 2f 0a 0a 73 74 61 74 69 63 20 46 74 73 33   */..static Fts3
34dc1 54 61 62 6c 65 20 2a 63 75 72 73 6f 72 5f 76 74  Table *cursor_vt
34dc2 61 62 28 46 74 73 33 43 75 72 73 6f 72 20 2a 63  ab(Fts3Cursor *c
34dc3 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 46 74 73  ){.  return (Fts
34dc4 33 54 61 62 6c 65 20 2a 29 20 63 2d 3e 62 61 73  3Table *) c->bas
34dc5 65 2e 70 56 74 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  e.pVtab;.}../*.*
34dc6 2a 20 54 68 65 20 78 44 69 73 63 6f 6e 6e 65 63  * The xDisconnec
34dc7 74 28 29 20 76 69 72 74 75 61 6c 20 74 61 62 6c  t() virtual tabl
34dc8 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  e method..*/.sta
34dc9 74 69 63 20 69 6e 74 20 66 74 73 33 44 69 73 63  tic int fts3Disc
34dca 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 28 73 71 6c  onnectMethod(sql
34dcb 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
34dcc 29 7b 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  ){.  Fts3Table *
34dcd 70 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a  p = (Fts3Table *
34dce 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 69 3b  )pVtab;.  int i;
34dcf 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ..  assert( p->n
34dd0 50 65 6e 64 69 6e 67 44 61 74 61 3d 3d 30 20 29  PendingData==0 )
34dd1 3b 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79  ;..  /* Free any
34dd2 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
34dd3 65 6e 74 73 20 68 65 6c 64 20 2a 2f 0a 20 20 66  ents held */.  f
34dd4 6f 72 28 69 3d 30 3b 20 69 3c 53 69 7a 65 6f 66  or(i=0; i<Sizeof
34dd5 41 72 72 61 79 28 70 2d 3e 61 53 74 6d 74 29 3b  Array(p->aStmt);
34dd6 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
34dd7 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 61  e3_finalize(p->a
34dd8 53 74 6d 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20  Stmt[i]);.  }.  
34dd9 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4c  for(i=0; i<p->nL
34dda 65 61 76 65 73 53 74 6d 74 3b 20 69 2b 2b 29 7b  eavesStmt; i++){
34ddb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  .    sqlite3_fin
34ddc 61 6c 69 7a 65 28 70 2d 3e 61 4c 65 61 76 65 73  alize(p->aLeaves
34ddd 53 74 6d 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20  Stmt[i]);.  }.  
34dde 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
34ddf 7a 53 65 6c 65 63 74 4c 65 61 76 65 73 29 3b 0a  zSelectLeaves);.
34de0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
34de1 2d 3e 61 4c 65 61 76 65 73 53 74 6d 74 29 3b 0a  ->aLeavesStmt);.
34de2 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
34de3 20 74 6f 6b 65 6e 69 7a 65 72 20 64 65 73 74 72   tokenizer destr
34de4 75 63 74 6f 72 20 74 6f 20 66 72 65 65 20 74 68  uctor to free th
34de5 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 2a 2f 0a  e tokenizer. */.
34de6 20 20 70 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d    p->pTokenizer-
34de7 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72  >pModule->xDestr
34de8 6f 79 28 70 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  oy(p->pTokenizer
34de9 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
34dea 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(p);.  return 
34deb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
34dec 0a 2a 2a 20 54 68 65 20 78 44 65 73 74 72 6f 79  .** The xDestroy
34ded 28 29 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  () virtual table
34dee 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
34def 69 63 20 69 6e 74 20 66 74 73 33 44 65 73 74 72  ic int fts3Destr
34df0 6f 79 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33  oyMethod(sqlite3
34df1 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20  _vtab *pVtab){. 
34df2 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
34df3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34df4 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
34df5 2a 2f 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  */.  Fts3Table *
34df6 70 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a  p = (Fts3Table *
34df7 29 70 56 74 61 62 3b 0a 0a 20 20 2f 2a 20 43 72  )pVtab;..  /* Cr
34df8 65 61 74 65 20 61 20 73 63 72 69 70 74 20 74 6f  eate a script to
34df9 20 64 72 6f 70 20 74 68 65 20 75 6e 64 65 72 6c   drop the underl
34dfa 79 69 6e 67 20 74 68 72 65 65 20 73 74 6f 72 61  ying three stora
34dfb 67 65 20 74 61 62 6c 65 73 2e 20 2a 2f 0a 20 20  ge tables. */.  
34dfc 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c  char *zSql = sql
34dfd 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
34dfe 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20      "DROP TABLE 
34dff 49 46 20 45 58 49 53 54 53 20 25 51 2e 27 25 71  IF EXISTS %Q.'%q
34e00 5f 63 6f 6e 74 65 6e 74 27 3b 22 0a 20 20 20 20  _content';".    
34e01 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20 49 46    "DROP TABLE IF
34e02 20 45 58 49 53 54 53 20 25 51 2e 27 25 71 5f 73   EXISTS %Q.'%q_s
34e03 65 67 6d 65 6e 74 73 27 3b 22 0a 20 20 20 20 20  egments';".     
34e04 20 22 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20   "DROP TABLE IF 
34e05 45 58 49 53 54 53 20 25 51 2e 27 25 71 5f 73 65  EXISTS %Q.'%q_se
34e06 67 64 69 72 27 3b 22 2c 20 0a 20 20 20 20 20 20  gdir';", .      
34e07 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65  p->zDb, p->zName
34e08 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61  , p->zDb, p->zNa
34e09 6d 65 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a  me, p->zDb, p->z
34e0a 4e 61 6d 65 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20  Name.  );..  /* 
34e0b 49 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61  If malloc has fa
34e0c 69 6c 65 64 2c 20 73 65 74 20 72 63 20 74 6f 20  iled, set rc to 
34e0d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 4f 74  SQLITE_NOMEM. Ot
34e0e 68 65 72 77 69 73 65 2c 20 74 72 79 20 74 6f 0a  herwise, try to.
34e0f 20 20 2a 2a 20 65 78 65 63 75 74 65 20 74 68 65    ** execute the
34e10 20 53 51 4c 20 73 63 72 69 70 74 20 63 72 65 61   SQL script crea
34e11 74 65 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a  ted above..  */.
34e12 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
34e13 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
34e14 78 65 63 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c  xec(p->db, zSql,
34e15 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73   0, 0, 0);.    s
34e16 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c  qlite3_free(zSql
34e17 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
34e18 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
34e19 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  M;.  }..  /* If 
34e1a 65 76 65 72 79 74 68 69 6e 67 20 68 61 73 20 77  everything has w
34e1b 6f 72 6b 65 64 2c 20 69 6e 76 6f 6b 65 20 66 74  orked, invoke ft
34e1c 73 33 44 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68  s3DisconnectMeth
34e1d 6f 64 28 29 20 74 6f 20 66 72 65 65 20 74 68 65  od() to free the
34e1e 0a 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 73 73  .  ** memory ass
34e1f 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
34e20 20 46 74 73 33 54 61 62 6c 65 20 73 74 72 75 63   Fts3Table struc
34e21 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20  ture and return 
34e22 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2a 20  SQLITE_OK..  ** 
34e23 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
34e24 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  n an SQLite erro
34e25 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 72  r code..  */.  r
34e26 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54  eturn (rc==SQLIT
34e27 45 5f 4f 4b 20 3f 20 66 74 73 33 44 69 73 63 6f  E_OK ? fts3Disco
34e28 6e 6e 65 63 74 4d 65 74 68 6f 64 28 70 56 74 61  nnectMethod(pVta
34e29 62 29 20 3a 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a  b) : rc);.}.../*
34e2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74  .** Invoke sqlit
34e2b 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28  e3_declare_vtab(
34e2c 29 20 74 6f 20 64 65 63 6c 61 72 65 20 74 68 65  ) to declare the
34e2d 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65 20   schema for the 
34e2e 46 54 53 33 20 74 61 62 6c 65 0a 2a 2a 20 70 61  FTS3 table.** pa
34e2f 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
34e30 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73  t argument. This
34e31 20 69 73 20 64 6f 6e 65 20 61 73 20 70 61 72 74   is done as part
34e32 20 6f 66 20 74 68 65 20 78 43 6f 6e 6e 65 63 74   of the xConnect
34e33 28 29 0a 2a 2a 20 61 6e 64 20 78 43 72 65 61 74  ().** and xCreat
34e34 65 28 29 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a  e() methods..*/.
34e35 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 44  static int fts3D
34e36 65 63 6c 61 72 65 56 74 61 62 28 46 74 73 33 54  eclareVtab(Fts3T
34e37 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  able *p){.  int 
34e38 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
34e39 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
34e3a 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
34e3b 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
34e3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e3d 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
34e3e 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  code */.  char *
34e3f 7a 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20  zSql;           
34e40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c            /* SQL
34e41 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65   statement passe
34e42 64 20 74 6f 20 64 65 63 6c 61 72 65 5f 76 74 61  d to declare_vta
34e43 62 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  b() */.  char *z
34e44 43 6f 6c 73 3b 20 20 20 20 20 20 20 20 20 20 20  Cols;           
34e45 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74           /* List
34e46 20 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65 64   of user defined
34e47 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 2f   columns */..  /
34e48 2a 20 43 72 65 61 74 65 20 61 20 6c 69 73 74 20  * Create a list 
34e49 6f 66 20 75 73 65 72 20 63 6f 6c 75 6d 6e 73 20  of user columns 
34e4a 66 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20  for the virtual 
34e4b 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 43 6f 6c 73  table */.  zCols
34e4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
34e4d 74 66 28 22 25 51 2c 20 22 2c 20 70 2d 3e 61 7a  tf("%Q, ", p->az
34e4e 43 6f 6c 75 6d 6e 5b 30 5d 29 3b 0a 20 20 66 6f  Column[0]);.  fo
34e4f 72 28 69 3d 31 3b 20 7a 43 6f 6c 73 20 26 26 20  r(i=1; zCols && 
34e50 69 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  i<p->nColumn; i+
34e51 2b 29 7b 0a 20 20 20 20 7a 43 6f 6c 73 20 3d 20  +){.    zCols = 
34e52 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
34e53 22 25 7a 25 51 2c 20 22 2c 20 7a 43 6f 6c 73 2c  "%z%Q, ", zCols,
34e54 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 5d 29   p->azColumn[i])
34e55 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61  ;.  }..  /* Crea
34e56 74 65 20 74 68 65 20 77 68 6f 6c 65 20 22 43 52  te the whole "CR
34e57 45 41 54 45 20 54 41 42 4c 45 22 20 73 74 61 74  EATE TABLE" stat
34e58 65 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f  ement to pass to
34e59 20 53 51 4c 69 74 65 20 2a 2f 0a 20 20 7a 53 71   SQLite */.  zSq
34e5a 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  l = sqlite3_mpri
34e5b 6e 74 66 28 0a 20 20 20 20 20 20 22 43 52 45 41  ntf(.      "CREA
34e5c 54 45 20 54 41 42 4c 45 20 78 28 25 73 20 25 51  TE TABLE x(%s %Q
34e5d 20 48 49 44 44 45 4e 2c 20 64 6f 63 69 64 20 48   HIDDEN, docid H
34e5e 49 44 44 45 4e 29 22 2c 20 7a 43 6f 6c 73 2c 20  IDDEN)", zCols, 
34e5f 70 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b 0a 0a 20  p->zName.  );.. 
34e60 20 69 66 28 20 21 7a 43 6f 6c 73 20 7c 7c 20 21   if( !zCols || !
34e61 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  zSql ){.    rc =
34e62 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
34e63 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
34e64 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
34e65 5f 76 74 61 62 28 70 2d 3e 64 62 2c 20 7a 53 71  _vtab(p->db, zSq
34e66 6c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  l);.  }..  sqlit
34e67 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
34e68 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
34e69 6f 6c 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ols);.  return r
34e6a 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  c;.}../*.** Crea
34e6b 74 65 20 74 68 65 20 62 61 63 6b 69 6e 67 20 73  te the backing s
34e6c 74 6f 72 65 20 74 61 62 6c 65 73 20 28 25 5f 63  tore tables (%_c
34e6d 6f 6e 74 65 6e 74 2c 20 25 5f 73 65 67 6d 65 6e  ontent, %_segmen
34e6e 74 73 20 61 6e 64 20 25 5f 73 65 67 64 69 72 29  ts and %_segdir)
34e6f 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 62 79 20  .** required by 
34e70 74 68 65 20 46 54 53 33 20 74 61 62 6c 65 20 70  the FTS3 table p
34e71 61 73 73 65 64 20 61 73 20 74 68 65 20 6f 6e 6c  assed as the onl
34e72 79 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73  y argument. This
34e73 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 61 73 20 70   is done.** as p
34e74 61 72 74 20 6f 66 20 74 68 65 20 76 74 61 62 20  art of the vtab 
34e75 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64  xCreate() method
34e76 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
34e77 66 74 73 33 43 72 65 61 74 65 54 61 62 6c 65 73  fts3CreateTables
34e78 28 46 74 73 33 54 61 62 6c 65 20 2a 70 29 7b 0a  (Fts3Table *p){.
34e79 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
34e7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e7b 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
34e7c 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
34e7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e7e 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
34e7f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
34e80 63 68 61 72 20 2a 7a 43 6f 6e 74 65 6e 74 43 6f  char *zContentCo
34e81 6c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ls;             
34e82 2f 2a 20 43 6f 6c 75 6d 6e 73 20 6f 66 20 25 5f  /* Columns of %_
34e83 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 2a 2f  content table */
34e84 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20  .  char *zSql;  
34e85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e86 20 20 20 2f 2a 20 53 51 4c 20 73 63 72 69 70 74     /* SQL script
34e87 20 74 6f 20 63 72 65 61 74 65 20 72 65 71 75 69   to create requi
34e88 72 65 64 20 74 61 62 6c 65 73 20 2a 2f 0a 0a 20  red tables */.. 
34e89 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6c 69 73   /* Create a lis
34e8a 74 20 6f 66 20 75 73 65 72 20 63 6f 6c 75 6d 6e  t of user column
34e8b 73 20 66 6f 72 20 74 68 65 20 63 6f 6e 74 65 6e  s for the conten
34e8c 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 43 6f  t table */.  zCo
34e8d 6e 74 65 6e 74 43 6f 6c 73 20 3d 20 73 71 6c 69  ntentCols = sqli
34e8e 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 64 6f 63  te3_mprintf("doc
34e8f 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
34e90 52 59 20 4b 45 59 22 29 3b 0a 20 20 66 6f 72 28  RY KEY");.  for(
34e91 69 3d 30 3b 20 7a 43 6f 6e 74 65 6e 74 43 6f 6c  i=0; zContentCol
34e92 73 20 26 26 20 69 3c 70 2d 3e 6e 43 6f 6c 75 6d  s && i<p->nColum
34e93 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61  n; i++){.    cha
34e94 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 43 6f 6c 75  r *z = p->azColu
34e95 6d 6e 5b 69 5d 3b 0a 20 20 20 20 7a 43 6f 6e 74  mn[i];.    zCont
34e96 65 6e 74 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65  entCols = sqlite
34e97 33 5f 6d 70 72 69 6e 74 66 28 22 25 7a 2c 20 27  3_mprintf("%z, '
34e98 63 25 64 25 71 27 22 2c 20 7a 43 6f 6e 74 65 6e  c%d%q'", zConten
34e99 74 43 6f 6c 73 2c 20 69 2c 20 7a 29 3b 0a 20 20  tCols, i, z);.  
34e9a 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74  }..  /* Create t
34e9b 68 65 20 77 68 6f 6c 65 20 53 51 4c 20 73 63 72  he whole SQL scr
34e9c 69 70 74 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20  ipt */.  zSql = 
34e9d 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
34e9e 0a 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54  .      "CREATE T
34e9f 41 42 4c 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74  ABLE %Q.'%q_cont
34ea0 65 6e 74 27 28 25 73 29 3b 22 0a 20 20 20 20 20  ent'(%s);".     
34ea1 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25   "CREATE TABLE %
34ea2 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 28  Q.'%q_segments'(
34ea3 62 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45 52 20  blockid INTEGER 
34ea4 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 6c 6f  PRIMARY KEY, blo
34ea5 63 6b 20 42 4c 4f 42 29 3b 22 0a 20 20 20 20 20  ck BLOB);".     
34ea6 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25   "CREATE TABLE %
34ea7 51 2e 27 25 71 5f 73 65 67 64 69 72 27 28 22 0a  Q.'%q_segdir'(".
34ea8 20 20 20 20 20 20 20 20 22 6c 65 76 65 6c 20 49          "level I
34ea9 4e 54 45 47 45 52 2c 22 0a 20 20 20 20 20 20 20  NTEGER,".       
34eaa 20 22 69 64 78 20 49 4e 54 45 47 45 52 2c 22 0a   "idx INTEGER,".
34eab 20 20 20 20 20 20 20 20 22 73 74 61 72 74 5f 62          "start_b
34eac 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 22 0a 20  lock INTEGER,". 
34ead 20 20 20 20 20 20 20 22 6c 65 61 76 65 73 5f 65         "leaves_e
34eae 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52  nd_block INTEGER
34eaf 2c 22 0a 20 20 20 20 20 20 20 20 22 65 6e 64 5f  ,".        "end_
34eb0 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 22 0a  block INTEGER,".
34eb1 20 20 20 20 20 20 20 20 22 72 6f 6f 74 20 42 4c          "root BL
34eb2 4f 42 2c 22 0a 20 20 20 20 20 20 20 20 22 50 52  OB,".        "PR
34eb3 49 4d 41 52 59 20 4b 45 59 28 6c 65 76 65 6c 2c  IMARY KEY(level,
34eb4 20 69 64 78 29 22 0a 20 20 20 20 20 20 22 29 3b   idx)".      ");
34eb5 22 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 44 62 2c  ",.      p->zDb,
34eb6 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6e 74   p->zName, zCont
34eb7 65 6e 74 43 6f 6c 73 2c 20 70 2d 3e 7a 44 62 2c  entCols, p->zDb,
34eb8 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d 3e 7a 44   p->zName, p->zD
34eb9 62 2c 20 70 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b  b, p->zName.  );
34eba 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 61 20  ..  /* Unless a 
34ebb 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
34ebc 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20 65   has occurred, e
34ebd 78 65 63 75 74 65 20 74 68 65 20 53 51 4c 20 73  xecute the SQL s
34ebe 63 72 69 70 74 20 74 6f 20 0a 20 20 2a 2a 20 63  cript to .  ** c
34ebf 72 65 61 74 65 20 74 68 65 20 74 61 62 6c 65 73  reate the tables
34ec0 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 64   used to store d
34ec1 61 74 61 20 66 6f 72 20 74 68 69 73 20 46 54 53  ata for this FTS
34ec2 33 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  3 virtual table.
34ec3 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 43 6f 6e  .  */.  if( zCon
34ec4 74 65 6e 74 43 6f 6c 73 3d 3d 30 20 7c 7c 20 7a  tentCols==0 || z
34ec5 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  Sql==0 ){.    rc
34ec6 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
34ec7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
34ec8 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
34ec9 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20  p->db, zSql, 0, 
34eca 30 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 73 71  0, 0);.  }..  sq
34ecb 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29  lite3_free(zSql)
34ecc 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
34ecd 28 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 29 3b 0a  (zContentCols);.
34ece 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
34ecf 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
34ed0 69 6f 6e 20 69 73 20 74 68 65 20 69 6d 70 6c 65  ion is the imple
34ed1 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 62 6f 74  mentation of bot
34ed2 68 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 20 61  h the xConnect a
34ed3 6e 64 20 78 43 72 65 61 74 65 0a 2a 2a 20 6d 65  nd xCreate.** me
34ed4 74 68 6f 64 73 20 6f 66 20 74 68 65 20 46 54 53  thods of the FTS
34ed5 33 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  3 virtual table.
34ed6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b  .**.** The argv[
34ed7 5d 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73  ] array contains
34ed8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
34ed9 2a 2a 0a 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20  **.**   argv[0] 
34eda 20 20 2d 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65    -> module name
34edb 0a 2a 2a 20 20 20 61 72 67 76 5b 31 5d 20 20 20  .**   argv[1]   
34edc 2d 3e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  -> database name
34edd 0a 2a 2a 20 20 20 61 72 67 76 5b 32 5d 20 20 20  .**   argv[2]   
34ede 2d 3e 20 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a  -> table name.**
34edf 20 20 20 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20     argv[...] -> 
34ee0 22 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22 20 66 69  "column name" fi
34ee1 65 6c 64 73 2e 2e 2e 0a 2a 2f 0a 69 6e 74 20 66  elds....*/.int f
34ee2 74 73 33 49 6e 69 74 56 74 61 62 28 0a 20 20 69  ts3InitVtab(.  i
34ee3 6e 74 20 69 73 43 72 65 61 74 65 2c 20 20 20 20  nt isCreate,    
34ee4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34ee5 2a 20 54 72 75 65 20 66 6f 72 20 78 43 72 65 61  * True for xCrea
34ee6 74 65 2c 20 66 61 6c 73 65 20 66 6f 72 20 78 43  te, false for xC
34ee7 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69  onnect */.  sqli
34ee8 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
34ee9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
34eea 68 65 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  he SQLite databa
34eeb 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
34eec 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20  .  void *pAux,  
34eed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34eee 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65     /* Hash table
34eef 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f 6b 65   containing toke
34ef0 6e 69 7a 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  nizers */.  int 
34ef1 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20  argc,           
34ef2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
34ef3 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
34ef4 73 20 69 6e 20 61 72 67 76 20 61 72 72 61 79 20  s in argv array 
34ef5 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
34ef6 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20 20  * const *argv,  
34ef7 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 2f       /* xCreate/
34ef8 78 43 6f 6e 6e 65 63 74 20 61 72 67 75 6d 65 6e  xConnect argumen
34ef9 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 71 6c  t array */.  sql
34efa 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54  ite3_vtab **ppVT
34efb 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ab,          /* 
34efc 57 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74  Write the result
34efd 69 6e 67 20 76 74 61 62 20 73 74 72 75 63 74 75  ing vtab structu
34efe 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61  re here */.  cha
34eff 72 20 2a 2a 70 7a 45 72 72 20 20 20 20 20 20 20  r **pzErr       
34f00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34f01 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20  Write any error 
34f02 6d 65 73 73 61 67 65 20 68 65 72 65 20 2a 2f 0a  message here */.
34f03 29 7b 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70  ){.  Fts3Hash *p
34f04 48 61 73 68 20 3d 20 28 46 74 73 33 48 61 73 68  Hash = (Fts3Hash
34f05 20 2a 29 70 41 75 78 3b 0a 20 20 46 74 73 33 54   *)pAux;.  Fts3T
34f06 61 62 6c 65 20 2a 70 3b 20 20 20 20 20 20 20 20  able *p;        
34f07 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
34f08 72 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20 76  r to allocated v
34f09 74 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  tab */.  int rc;
34f0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f0b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
34f0c 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn code */.  int
34f0d 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   i;.  int nByte;
34f0e 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69  .  int iCol;.  i
34f0f 6e 74 20 6e 53 74 72 69 6e 67 20 3d 20 30 3b 0a  nt nString = 0;.
34f10 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 30 3b 0a    int nCol = 0;.
34f11 20 20 63 68 61 72 20 2a 7a 43 73 72 3b 0a 20 20    char *zCsr;.  
34f12 69 6e 74 20 6e 44 62 3b 0a 20 20 69 6e 74 20 6e  int nDb;.  int n
34f13 4e 61 6d 65 3b 0a 0a 20 20 63 6f 6e 73 74 20 63  Name;..  const c
34f14 68 61 72 20 2a 7a 54 6f 6b 65 6e 69 7a 65 72 20  har *zTokenizer 
34f15 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
34f16 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
34f17 69 7a 65 72 3b 20 20 2f 2a 20 54 6f 6b 65 6e 69  izer;  /* Tokeni
34f18 7a 65 72 20 66 6f 72 20 74 68 69 73 20 74 61 62  zer for this tab
34f19 6c 65 20 2a 2f 0a 0a 20 20 6e 44 62 20 3d 20 73  le */..  nDb = s
34f1a 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 20 2b  trlen(argv[1]) +
34f1b 20 31 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74   1;.  nName = st
34f1c 72 6c 65 6e 28 61 72 67 76 5b 32 5d 29 20 2b 20  rlen(argv[2]) + 
34f1d 31 3b 0a 20 20 66 6f 72 28 69 3d 33 3b 20 69 3c  1;.  for(i=3; i<
34f1e 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  argc; i++){.    
34f1f 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 3d 20  char const *z = 
34f20 61 72 67 76 5b 69 5d 3b 0a 20 20 20 20 72 63 20  argv[i];.    rc 
34f21 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
34f22 74 54 6f 6b 65 6e 69 7a 65 72 28 70 48 61 73 68  tTokenizer(pHash
34f23 2c 20 7a 2c 20 26 70 54 6f 6b 65 6e 69 7a 65 72  , z, &pTokenizer
34f24 2c 20 26 7a 54 6f 6b 65 6e 69 7a 65 72 2c 20 70  , &zTokenizer, p
34f25 7a 45 72 72 29 3b 0a 20 20 20 20 69 66 28 20 72  zErr);.    if( r
34f26 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
34f27 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
34f28 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a  .    }.    if( z
34f29 21 3d 7a 54 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a  !=zTokenizer ){.
34f2a 20 20 20 20 20 20 6e 53 74 72 69 6e 67 20 2b 3d        nString +=
34f2b 20 73 74 72 6c 65 6e 28 7a 29 20 2b 20 31 3b 0a   strlen(z) + 1;.
34f2c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6e 43 6f 6c      }.  }.  nCol
34f2d 20 3d 20 61 72 67 63 20 2d 20 33 20 2d 20 28 7a   = argc - 3 - (z
34f2e 54 6f 6b 65 6e 69 7a 65 72 21 3d 30 29 3b 0a 20  Tokenizer!=0);. 
34f2f 20 69 66 28 20 7a 54 6f 6b 65 6e 69 7a 65 72 3d   if( zTokenizer=
34f30 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  =0 ){.    rc = s
34f31 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 54 6f  qlite3Fts3InitTo
34f32 6b 65 6e 69 7a 65 72 28 70 48 61 73 68 2c 20 30  kenizer(pHash, 0
34f33 2c 20 26 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 30  , &pTokenizer, 0
34f34 2c 20 70 7a 45 72 72 29 3b 0a 20 20 20 20 69 66  , pzErr);.    if
34f35 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34f36 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
34f37 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  rc;.    }.    as
34f38 73 65 72 74 28 20 70 54 6f 6b 65 6e 69 7a 65 72  sert( pTokenizer
34f39 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c   );.  }..  /* Al
34f3a 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c  locate and popul
34f3b 61 74 65 20 74 68 65 20 46 74 73 33 54 61 62 6c  ate the Fts3Tabl
34f3c 65 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a  e structure. */.
34f3d 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66    nByte = sizeof
34f3e 28 46 74 73 33 54 61 62 6c 65 29 20 2b 20 20 20  (Fts3Table) +   
34f3f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 74             /* Ft
34f40 73 33 54 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20  s3Table */.     
34f41 20 20 20 20 20 6e 43 6f 6c 20 2a 20 73 69 7a 65       nCol * size
34f42 6f 66 28 63 68 61 72 20 2a 29 20 2b 20 20 20 20  of(char *) +    
34f43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 7a 43            /* azC
34f44 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 20  olumn */.       
34f45 20 20 20 6e 4e 61 6d 65 20 2b 20 20 20 20 20 20     nName +      
34f46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f47 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65          /* zName
34f48 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6e 44   */.          nD
34f49 62 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  b +             
34f4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f4b 20 20 20 2f 2a 20 7a 44 62 20 2a 2f 0a 20 20 20     /* zDb */.   
34f4c 20 20 20 20 20 20 20 6e 53 74 72 69 6e 67 3b 20         nString; 
34f4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f4e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
34f4f 70 61 63 65 20 66 6f 72 20 61 7a 43 6f 6c 75 6d  pace for azColum
34f50 6e 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 70  n strings */.  p
34f51 20 3d 20 28 46 74 73 33 54 61 62 6c 65 2a 29 73   = (Fts3Table*)s
34f52 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
34f53 79 74 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30  yte);.  if( p==0
34f54 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
34f55 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
34f56 6f 74 6f 20 66 74 73 33 5f 69 6e 69 74 5f 6f 75  oto fts3_init_ou
34f57 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  t;.  }.  memset(
34f58 70 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 0a 20  p, 0, nByte);.. 
34f59 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70   p->db = db;.  p
34f5a 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c  ->nColumn = nCol
34f5b 3b 0a 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44  ;.  p->nPendingD
34f5c 61 74 61 20 3d 20 30 3b 0a 20 20 70 2d 3e 61 7a  ata = 0;.  p->az
34f5d 43 6f 6c 75 6d 6e 20 3d 20 28 63 68 61 72 20 2a  Column = (char *
34f5e 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e 70 54  *)&p[1];.  p->pT
34f5f 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65  okenizer = pToke
34f60 6e 69 7a 65 72 3b 0a 20 20 7a 43 73 72 20 3d 20  nizer;.  zCsr = 
34f61 28 63 68 61 72 20 2a 29 26 70 2d 3e 61 7a 43 6f  (char *)&p->azCo
34f62 6c 75 6d 6e 5b 6e 43 6f 6c 5d 3b 0a 0a 20 20 66  lumn[nCol];..  f
34f63 74 73 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e  ts3HashInit(&p->
34f64 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 46 54  pendingTerms, FT
34f65 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20  S3_HASH_STRING, 
34f66 31 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69  1);..  /* Fill i
34f67 6e 20 74 68 65 20 7a 4e 61 6d 65 20 61 6e 64 20  n the zName and 
34f68 7a 44 62 20 66 69 65 6c 64 73 20 6f 66 20 74 68  zDb fields of th
34f69 65 20 76 74 61 62 20 73 74 72 75 63 74 75 72 65  e vtab structure
34f6a 2e 20 2a 2f 0a 20 20 70 2d 3e 7a 4e 61 6d 65 20  . */.  p->zName 
34f6b 3d 20 7a 43 73 72 3b 0a 20 20 6d 65 6d 63 70 79  = zCsr;.  memcpy
34f6c 28 7a 43 73 72 2c 20 61 72 67 76 5b 32 5d 2c 20  (zCsr, argv[2], 
34f6d 6e 4e 61 6d 65 29 3b 0a 20 20 7a 43 73 72 20 2b  nName);.  zCsr +
34f6e 3d 20 6e 4e 61 6d 65 3b 0a 20 20 70 2d 3e 7a 44  = nName;.  p->zD
34f6f 62 20 3d 20 7a 43 73 72 3b 0a 20 20 6d 65 6d 63  b = zCsr;.  memc
34f70 70 79 28 7a 43 73 72 2c 20 61 72 67 76 5b 31 5d  py(zCsr, argv[1]
34f71 2c 20 6e 44 62 29 3b 0a 20 20 7a 43 73 72 20 2b  , nDb);.  zCsr +
34f72 3d 20 6e 44 62 3b 0a 0a 20 20 2f 2a 20 46 69 6c  = nDb;..  /* Fil
34f73 6c 20 69 6e 20 74 68 65 20 61 7a 43 6f 6c 75 6d  l in the azColum
34f74 6e 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 43 6f  n array */.  iCo
34f75 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 33  l = 0;.  for(i=3
34f76 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a  ; i<argc; i++){.
34f77 20 20 20 20 69 66 28 20 61 72 67 76 5b 69 5d 21      if( argv[i]!
34f78 3d 7a 54 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a 20  =zTokenizer ){. 
34f79 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 0a 20       char *z; . 
34f7a 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
34f7b 20 20 7a 20 3d 20 28 63 68 61 72 20 2a 29 73 71    z = (char *)sq
34f7c 6c 69 74 65 33 46 74 73 33 4e 65 78 74 54 6f 6b  lite3Fts3NextTok
34f7d 65 6e 28 61 72 67 76 5b 69 5d 2c 20 26 6e 29 3b  en(argv[i], &n);
34f7e 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 43  .      memcpy(zC
34f7f 73 72 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20  sr, z, n);.     
34f80 20 7a 43 73 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b   zCsr[n] = '\0';
34f81 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74  .      sqlite3Ft
34f82 73 33 44 65 71 75 6f 74 65 28 7a 43 73 72 29 3b  s3Dequote(zCsr);
34f83 0a 20 20 20 20 20 20 70 2d 3e 61 7a 43 6f 6c 75  .      p->azColu
34f84 6d 6e 5b 69 43 6f 6c 2b 2b 5d 20 3d 20 7a 43 73  mn[iCol++] = zCs
34f85 72 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d  r;.      zCsr +=
34f86 20 6e 2b 31 3b 0a 20 20 20 20 20 20 61 73 73 65   n+1;.      asse
34f87 72 74 28 20 7a 43 73 72 20 3c 3d 20 26 28 28 63  rt( zCsr <= &((c
34f88 68 61 72 20 2a 29 70 29 5b 6e 42 79 74 65 5d 20  har *)p)[nByte] 
34f89 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
34f8a 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
34f8b 20 78 43 72 65 61 74 65 20 63 61 6c 6c 2c 20 63   xCreate call, c
34f8c 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c  reate the underl
34f8d 79 69 6e 67 20 74 61 62 6c 65 73 20 69 6e 20 74  ying tables in t
34f8e 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  he .  ** databas
34f8f 65 2e 20 54 4f 44 4f 3a 20 46 6f 72 20 78 43 6f  e. TODO: For xCo
34f90 6e 6e 65 63 74 28 29 2c 20 69 74 20 63 6f 75 6c  nnect(), it coul
34f91 64 20 76 65 72 69 66 79 20 74 68 61 74 20 73 61  d verify that sa
34f92 69 64 20 74 61 62 6c 65 73 20 65 78 69 73 74 2e  id tables exist.
34f93 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 72  .  */.  if( isCr
34f94 65 61 74 65 20 29 7b 0a 20 20 20 20 72 63 20 3d  eate ){.    rc =
34f95 20 66 74 73 33 43 72 65 61 74 65 54 61 62 6c 65   fts3CreateTable
34f96 73 28 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63  s(p);.    if( rc
34f97 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
34f98 74 6f 20 66 74 73 33 5f 69 6e 69 74 5f 6f 75 74  to fts3_init_out
34f99 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 66 74  ;.  }..  rc = ft
34f9a 73 33 44 65 63 6c 61 72 65 56 74 61 62 28 70 29  s3DeclareVtab(p)
34f9b 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
34f9c 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 74 73  TE_OK ) goto fts
34f9d 33 5f 69 6e 69 74 5f 6f 75 74 3b 0a 0a 20 20 2a  3_init_out;..  *
34f9e 70 70 56 54 61 62 20 3d 20 26 70 2d 3e 62 61 73  ppVTab = &p->bas
34f9f 65 3b 0a 0a 66 74 73 33 5f 69 6e 69 74 5f 6f 75  e;..fts3_init_ou
34fa0 74 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  t:.  if( rc!=SQL
34fa1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
34fa2 28 20 70 20 29 20 66 74 73 33 44 69 73 63 6f 6e  ( p ) fts3Discon
34fa3 6e 65 63 74 4d 65 74 68 6f 64 28 28 73 71 6c 69  nectMethod((sqli
34fa4 74 65 33 5f 76 74 61 62 20 2a 29 70 29 3b 0a 20  te3_vtab *)p);. 
34fa5 20 20 20 65 6c 73 65 20 69 66 28 20 70 54 6f 6b     else if( pTok
34fa6 65 6e 69 7a 65 72 20 29 20 70 54 6f 6b 65 6e 69  enizer ) pTokeni
34fa7 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44  zer->pModule->xD
34fa8 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65  estroy(pTokenize
34fa9 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  r);.  }.  return
34faa 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
34fab 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 61 6e 64  e xConnect() and
34fac 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f   xCreate() metho
34fad 64 73 20 66 6f 72 20 74 68 65 20 76 69 72 74 75  ds for the virtu
34fae 61 6c 20 74 61 62 6c 65 2e 20 41 6c 6c 20 74 68  al table. All th
34faf 65 0a 2a 2a 20 77 6f 72 6b 20 69 73 20 64 6f 6e  e.** work is don
34fb0 65 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 66 74  e in function ft
34fb1 73 33 49 6e 69 74 56 74 61 62 28 29 2e 0a 2a 2f  s3InitVtab()..*/
34fb2 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
34fb3 43 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 28 0a 20  ConnectMethod(. 
34fb4 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
34fb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fb6 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
34fb7 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  nection */.  voi
34fb8 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20  d *pAux,        
34fb9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34fba 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e  Pointer to token
34fbb 69 7a 65 72 20 68 61 73 68 20 74 61 62 6c 65 20  izer hash table 
34fbc 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20  */.  int argc,  
34fbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fbe 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
34fbf 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 72  f elements in ar
34fc0 67 76 20 61 72 72 61 79 20 2a 2f 0a 20 20 63 6f  gv array */.  co
34fc1 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
34fc2 20 2a 61 72 67 76 2c 20 20 20 20 20 20 20 2f 2a   *argv,       /*
34fc3 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
34fc4 74 20 61 72 67 75 6d 65 6e 74 20 61 72 72 61 79  t argument array
34fc5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   */.  sqlite3_vt
34fc6 61 62 20 2a 2a 70 70 56 74 61 62 2c 20 20 20 20  ab **ppVtab,    
34fc7 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65        /* OUT: Ne
34fc8 77 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 6f  w sqlite3_vtab o
34fc9 62 6a 65 63 74 20 2a 2f 0a 20 20 63 68 61 72 20  bject */.  char 
34fca 2a 2a 70 7a 45 72 72 20 20 20 20 20 20 20 20 20  **pzErr         
34fcb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
34fcc 54 3a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  T: sqlite3_mallo
34fcd 63 27 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67  c'd error messag
34fce 65 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e  e */.){.  return
34fcf 20 66 74 73 33 49 6e 69 74 56 74 61 62 28 30 2c   fts3InitVtab(0,
34fd0 20 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c   db, pAux, argc,
34fd1 20 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70   argv, ppVtab, p
34fd2 7a 45 72 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  zErr);.}.static 
34fd3 69 6e 74 20 66 74 73 33 43 72 65 61 74 65 4d 65  int fts3CreateMe
34fd4 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 20  thod(.  sqlite3 
34fd5 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
34fd6 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
34fd7 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
34fd8 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20  /.  void *pAux, 
34fd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fda 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
34fdb 6f 20 74 6f 6b 65 6e 69 7a 65 72 20 68 61 73 68  o tokenizer hash
34fdc 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
34fdd 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20  argc,           
34fde 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
34fdf 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
34fe0 73 20 69 6e 20 61 72 67 76 20 61 72 72 61 79 20  s in argv array 
34fe1 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
34fe2 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20 20  * const *argv,  
34fe3 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 2f       /* xCreate/
34fe4 78 43 6f 6e 6e 65 63 74 20 61 72 67 75 6d 65 6e  xConnect argumen
34fe5 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 71 6c  t array */.  sql
34fe6 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 74  ite3_vtab **ppVt
34fe7 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ab,          /* 
34fe8 4f 55 54 3a 20 4e 65 77 20 73 71 6c 69 74 65 33  OUT: New sqlite3
34fe9 5f 76 74 61 62 20 6f 62 6a 65 63 74 20 2a 2f 0a  _vtab object */.
34fea 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 20 20    char **pzErr  
34feb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fec 20 20 2f 2a 20 4f 55 54 3a 20 73 71 6c 69 74 65    /* OUT: sqlite
34fed 33 5f 6d 61 6c 6c 6f 63 27 64 20 65 72 72 6f 72  3_malloc'd error
34fee 20 6d 65 73 73 61 67 65 20 2a 2f 0a 29 7b 0a 20   message */.){. 
34fef 20 72 65 74 75 72 6e 20 66 74 73 33 49 6e 69 74   return fts3Init
34ff0 56 74 61 62 28 31 2c 20 64 62 2c 20 70 41 75 78  Vtab(1, db, pAux
34ff1 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70  , argc, argv, pp
34ff2 56 74 61 62 2c 20 70 7a 45 72 72 29 3b 0a 7d 0a  Vtab, pzErr);.}.
34ff3 0a 2f 2a 20 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ./* .** Implemen
34ff4 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 78 42  tation of the xB
34ff5 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 20  estIndex method 
34ff6 66 6f 72 20 46 54 53 33 20 74 61 62 6c 65 73 2e  for FTS3 tables.
34ff7 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 68   There.** are th
34ff8 72 65 65 20 70 6f 73 73 69 62 6c 65 20 73 74 72  ree possible str
34ff9 61 74 65 67 69 65 73 2c 20 69 6e 20 6f 72 64 65  ategies, in orde
34ffa 72 20 6f 66 20 70 72 65 66 65 72 65 6e 63 65 3a  r of preference:
34ffb 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 44 69 72 65  .**.**   1. Dire
34ffc 63 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77  ct lookup by row
34ffd 69 64 20 6f 72 20 64 6f 63 69 64 2e 20 0a 2a 2a  id or docid. .**
34ffe 20 20 20 32 2e 20 46 75 6c 6c 2d 74 65 78 74 20     2. Full-text 
34fff 73 65 61 72 63 68 20 75 73 69 6e 67 20 61 20 4d  search using a M
35000 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20 6f 6e  ATCH operator on
35001 20 61 20 6e 6f 6e 2d 64 6f 63 69 64 20 63 6f 6c   a non-docid col
35002 75 6d 6e 2e 0a 2a 2a 20 20 20 33 2e 20 4c 69 6e  umn..**   3. Lin
35003 65 61 72 20 73 63 61 6e 20 6f 66 20 25 5f 63 6f  ear scan of %_co
35004 6e 74 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a  ntent table..*/.
35005 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 42  static int fts3B
35006 65 73 74 49 6e 64 65 78 4d 65 74 68 6f 64 28 73  estIndexMethod(s
35007 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
35008 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  ab, sqlite3_inde
35009 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a  x_info *pInfo){.
3500a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d    Fts3Table *p =
3500b 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 56   (Fts3Table *)pV
3500c 54 61 62 3b 0a 20 20 69 6e 74 20 69 3b 20 20 20  Tab;.  int i;   
3500d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3500e 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
3500f 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
35010 20 69 6e 74 20 69 43 6f 6e 73 20 3d 20 2d 31 3b   int iCons = -1;
35011 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35012 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6e   /* Index of con
35013 73 74 72 61 69 6e 74 20 74 6f 20 75 73 65 20 2a  straint to use *
35014 2f 0a 0a 20 20 2f 2a 20 42 79 20 64 65 66 61 75  /..  /* By defau
35015 6c 74 20 75 73 65 20 61 20 66 75 6c 6c 20 74 61  lt use a full ta
35016 62 6c 65 20 73 63 61 6e 2e 20 54 68 69 73 20 69  ble scan. This i
35017 73 20 61 6e 20 65 78 70 65 6e 73 69 76 65 20 6f  s an expensive o
35018 70 74 69 6f 6e 2c 0a 20 20 2a 2a 20 73 6f 20 73  ption,.  ** so s
35019 65 61 72 63 68 20 74 68 72 6f 75 67 68 20 74 68  earch through th
3501a 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f  e constraints to
3501b 20 73 65 65 20 69 66 20 61 20 6d 6f 72 65 20 65   see if a more e
3501c 66 66 69 63 69 65 6e 74 20 0a 20 20 2a 2a 20 73  fficient .  ** s
3501d 74 72 61 74 65 67 79 20 69 73 20 70 6f 73 73 69  trategy is possi
3501e 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 49 6e 66  ble..  */.  pInf
3501f 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 46 54 53 33  o->idxNum = FTS3
35020 5f 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48  _FULLSCAN_SEARCH
35021 3b 0a 20 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d  ;.  pInfo->estim
35022 61 74 65 64 43 6f 73 74 20 3d 20 35 30 30 30 30  atedCost = 50000
35023 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
35024 70 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  pInfo->nConstrai
35025 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74  nt; i++){.    st
35026 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
35027 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70  ex_constraint *p
35028 43 6f 6e 73 20 3d 20 26 70 49 6e 66 6f 2d 3e 61  Cons = &pInfo->a
35029 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 3b 0a 20  Constraint[i];. 
3502a 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 75 73     if( pCons->us
3502b 61 62 6c 65 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  able==0 ) contin
3502c 75 65 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 64 69  ue;..    /* A di
3502d 72 65 63 74 20 6c 6f 6f 6b 75 70 20 6f 6e 20 74  rect lookup on t
3502e 68 65 20 72 6f 77 69 64 20 6f 72 20 64 6f 63 69  he rowid or doci
3502f 64 20 63 6f 6c 75 6d 6e 2e 20 54 68 69 73 20 69  d column. This i
35030 73 20 74 68 65 20 62 65 73 74 0a 20 20 20 20 2a  s the best.    *
35031 2a 20 73 74 72 61 74 65 67 79 20 69 6e 20 61 6c  * strategy in al
35032 6c 20 63 61 73 65 73 2e 20 41 73 73 69 67 6e 20  l cases. Assign 
35033 61 20 63 6f 73 74 20 6f 66 20 31 2e 30 20 61 6e  a cost of 1.0 an
35034 64 20 72 65 74 75 72 6e 20 65 61 72 6c 79 2e 0a  d return early..
35035 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
35036 43 6f 6e 73 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45  Cons->op==SQLITE
35037 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
35038 54 5f 45 51 20 0a 20 20 20 20 20 26 26 20 28 70  T_EQ .     && (p
35039 43 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20  Cons->iColumn<0 
3503a 7c 7c 20 70 43 6f 6e 73 2d 3e 69 43 6f 6c 75 6d  || pCons->iColum
3503b 6e 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20  n==p->nColumn+1 
3503c 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70  ).    ){.      p
3503d 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 46  Info->idxNum = F
3503e 54 53 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48  TS3_DOCID_SEARCH
3503f 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 65  ;.      pInfo->e
35040 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 31  stimatedCost = 1
35041 2e 30 3b 0a 20 20 20 20 20 20 69 43 6f 6e 73 20  .0;.      iCons 
35042 3d 20 69 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  = i;.      break
35043 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
35044 41 20 4d 41 54 43 48 20 63 6f 6e 73 74 72 61 69  A MATCH constrai
35045 6e 74 2e 20 55 73 65 20 61 20 66 75 6c 6c 2d 74  nt. Use a full-t
35046 65 78 74 20 73 65 61 72 63 68 2e 0a 20 20 20 20  ext search..    
35047 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65  **.    ** If the
35048 72 65 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20  re is more than 
35049 6f 6e 65 20 4d 41 54 43 48 20 63 6f 6e 73 74 72  one MATCH constr
3504a 61 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 2c 20  aint available, 
3504b 75 73 65 20 74 68 65 20 66 69 72 73 74 0a 20 20  use the first.  
3504c 20 20 2a 2a 20 6f 6e 65 20 65 6e 63 6f 75 6e 74    ** one encount
3504d 65 72 65 64 2e 20 49 66 20 74 68 65 72 65 20 69  ered. If there i
3504e 73 20 62 6f 74 68 20 61 20 4d 41 54 43 48 20 63  s both a MATCH c
3504f 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 61 20  onstraint and a 
35050 64 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 72 6f  direct.    ** ro
35051 77 69 64 2f 64 6f 63 69 64 20 6c 6f 6f 6b 75 70  wid/docid lookup
35052 2c 20 70 72 65 66 65 72 20 74 68 65 20 72 6f 77  , prefer the row
35053 69 64 2f 64 6f 63 69 64 20 73 74 72 61 74 65 67  id/docid strateg
35054 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  y..    */.    if
35055 28 20 69 43 6f 6e 73 3c 30 20 0a 20 20 20 20 20  ( iCons<0 .     
35056 26 26 20 70 43 6f 6e 73 2d 3e 6f 70 3d 3d 53 51  && pCons->op==SQ
35057 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
35058 52 41 49 4e 54 5f 4d 41 54 43 48 20 0a 20 20 20  RAINT_MATCH .   
35059 20 20 26 26 20 70 43 6f 6e 73 2d 3e 69 43 6f 6c    && pCons->iCol
3505a 75 6d 6e 3e 3d 30 20 26 26 20 70 43 6f 6e 73 2d  umn>=0 && pCons-
3505b 3e 69 43 6f 6c 75 6d 6e 3c 3d 70 2d 3e 6e 43 6f  >iColumn<=p->nCo
3505c 6c 75 6d 6e 0a 20 20 20 20 29 7b 0a 20 20 20 20  lumn.    ){.    
3505d 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20    pInfo->idxNum 
3505e 3d 20 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f  = FTS3_FULLTEXT_
3505f 53 45 41 52 43 48 20 2b 20 70 43 6f 6e 73 2d 3e  SEARCH + pCons->
35060 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70  iColumn;.      p
35061 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43  Info->estimatedC
35062 6f 73 74 20 3d 20 32 2e 30 3b 0a 20 20 20 20 20  ost = 2.0;.     
35063 20 69 43 6f 6e 73 20 3d 20 69 3b 0a 20 20 20 20   iCons = i;.    
35064 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 43 6f  }.  }..  if( iCo
35065 6e 73 3e 3d 30 20 29 7b 0a 20 20 20 20 70 49 6e  ns>=0 ){.    pIn
35066 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55  fo->aConstraintU
35067 73 61 67 65 5b 69 43 6f 6e 73 5d 2e 61 72 67 76  sage[iCons].argv
35068 49 6e 64 65 78 20 3d 20 31 3b 0a 20 20 20 20 70  Index = 1;.    p
35069 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
3506a 74 55 73 61 67 65 5b 69 43 6f 6e 73 5d 2e 6f 6d  tUsage[iCons].om
3506b 69 74 20 3d 20 31 3b 0a 20 20 7d 20 0a 20 20 72  it = 1;.  } .  r
3506c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3506d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
3506e 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 4f 70 65  entation of xOpe
3506f 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  n method..*/.sta
35070 74 69 63 20 69 6e 74 20 66 74 73 33 4f 70 65 6e  tic int fts3Open
35071 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76  Method(sqlite3_v
35072 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
35073 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
35074 2a 2a 70 70 43 73 72 29 7b 0a 20 20 73 71 6c 69  **ppCsr){.  sqli
35075 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
35076 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 20  *pCsr;          
35077 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65       /* Allocate
35078 64 20 63 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f  d cursor */..  /
35079 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 75 66  * Allocate a buf
3507a 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68  fer large enough
3507b 20 66 6f 72 20 61 6e 20 46 74 73 33 43 75 72 73   for an Fts3Curs
3507c 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20 49 66  or structure. If
3507d 20 74 68 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61   the.  ** alloca
3507e 74 69 6f 6e 20 73 75 63 63 65 65 64 73 2c 20 7a  tion succeeds, z
3507f 65 72 6f 20 69 74 20 61 6e 64 20 72 65 74 75 72  ero it and retur
35080 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68  n SQLITE_OK. Oth
35081 65 72 77 69 73 65 2c 20 0a 20 20 2a 2a 20 69 66  erwise, .  ** if
35082 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
35083 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 53 51  fails, return SQ
35084 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f  LITE_NOMEM..  */
35085 0a 20 20 2a 70 70 43 73 72 20 3d 20 70 43 73 72  .  *ppCsr = pCsr
35086 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62   = (sqlite3_vtab
35087 5f 63 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65  _cursor *)sqlite
35088 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
35089 46 74 73 33 43 75 72 73 6f 72 29 29 3b 0a 20 20  Fts3Cursor));.  
3508a 69 66 28 20 21 70 43 73 72 20 29 7b 0a 20 20 20  if( !pCsr ){.   
3508b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3508c 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
3508d 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65  et(pCsr, 0, size
3508e 6f 66 28 46 74 73 33 43 75 72 73 6f 72 29 29 3b  of(Fts3Cursor));
3508f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
35090 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _OK;.}../*******
35091 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35092 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
35095 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35096 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35097 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35098 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
35099 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3509a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3509b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3509c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3509d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3509e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3509f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350a1 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  **/.../*.** Clos
350a2 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 46  e the cursor.  F
350a3 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
350a4 66 6f 72 6d 61 74 69 6f 6e 20 73 65 65 20 74 68  formation see th
350a5 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a  e documentation.
350a6 2a 2a 20 6f 6e 20 74 68 65 20 78 43 6c 6f 73 65  ** on the xClose
350a7 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76   method of the v
350a8 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74  irtual table int
350a9 65 72 66 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69  erface..*/.stati
350aa 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 6c  c int fulltextCl
350ab 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ose(sqlite3_vtab
350ac 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
350ad 29 7b 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  ){.  Fts3Cursor 
350ae 2a 70 43 73 72 20 3d 20 28 46 74 73 33 43 75 72  *pCsr = (Fts3Cur
350af 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a 20  sor *)pCursor;. 
350b0 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
350b1 65 28 70 43 73 72 2d 3e 70 53 74 6d 74 29 3b 0a  e(pCsr->pStmt);.
350b2 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
350b3 72 46 72 65 65 28 70 43 73 72 2d 3e 70 45 78 70  rFree(pCsr->pExp
350b4 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  r);.  sqlite3_fr
350b5 65 65 28 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73  ee(pCsr->aDoclis
350b6 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  t);.  sqlite3_fr
350b7 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65 74 75  ee(pCsr);.  retu
350b8 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
350b9 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
350ba 43 75 72 73 6f 72 53 65 65 6b 28 46 74 73 33 43  CursorSeek(Fts3C
350bb 75 72 73 6f 72 20 2a 70 43 73 72 29 7b 0a 20 20  ursor *pCsr){.  
350bc 69 66 28 20 70 43 73 72 2d 3e 69 73 52 65 71 75  if( pCsr->isRequ
350bd 69 72 65 53 65 65 6b 20 29 7b 0a 20 20 20 20 70  ireSeek ){.    p
350be 43 73 72 2d 3e 69 73 52 65 71 75 69 72 65 53 65  Csr->isRequireSe
350bf 65 6b 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  ek = 0;.    sqli
350c0 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
350c1 43 73 72 2d 3e 70 53 74 6d 74 2c 20 31 2c 20 70  Csr->pStmt, 1, p
350c2 43 73 72 2d 3e 69 50 72 65 76 49 64 29 3b 0a 20  Csr->iPrevId);. 
350c3 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f     if( SQLITE_RO
350c4 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
350c5 70 43 73 72 2d 3e 70 53 74 6d 74 29 20 29 7b 0a  pCsr->pStmt) ){.
350c6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
350c7 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73  ITE_OK;.    }els
350c8 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b  e{.      int rc;
350c9 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69 73 45  .      pCsr->isE
350ca 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66  of = 1;.      if
350cb 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
350cc 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
350cd 28 70 43 73 72 2d 3e 70 53 74 6d 74 29 29 20 29  (pCsr->pStmt)) )
350ce 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
350cf 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
350d0 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
350d1 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  n rc;.    }.  }e
350d2 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
350d3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d  SQLITE_OK;.  }.}
350d4 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
350d5 33 4e 65 78 74 4d 65 74 68 6f 64 28 73 71 6c 69  3NextMethod(sqli
350d6 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
350d7 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74  *pCursor){.  int
350d8 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
350d9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
350da 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
350db 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
350dc 72 20 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20  r = (Fts3Cursor 
350dd 2a 29 70 43 75 72 73 6f 72 3b 0a 0a 20 20 69 66  *)pCursor;..  if
350de 28 20 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74  ( pCsr->aDoclist
350df 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 53  ==0 ){.    if( S
350e0 51 4c 49 54 45 5f 52 4f 57 21 3d 73 71 6c 69 74  QLITE_ROW!=sqlit
350e1 65 33 5f 73 74 65 70 28 70 43 73 72 2d 3e 70 53  e3_step(pCsr->pS
350e2 74 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 70 43  tmt) ){.      pC
350e3 73 72 2d 3e 69 73 45 6f 66 20 3d 20 31 3b 0a 20  sr->isEof = 1;. 
350e4 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
350e5 33 5f 72 65 73 65 74 28 70 43 73 72 2d 3e 70 53  3_reset(pCsr->pS
350e6 74 6d 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  tmt);.    }.  }e
350e7 6c 73 65 20 69 66 28 20 70 43 73 72 2d 3e 70 4e  lse if( pCsr->pN
350e8 65 78 74 49 64 3e 3d 26 70 43 73 72 2d 3e 61 44  extId>=&pCsr->aD
350e9 6f 63 6c 69 73 74 5b 70 43 73 72 2d 3e 6e 44 6f  oclist[pCsr->nDo
350ea 63 6c 69 73 74 5d 20 29 7b 0a 20 20 20 20 70 43  clist] ){.    pC
350eb 73 72 2d 3e 69 73 45 6f 66 20 3d 20 31 3b 0a 20  sr->isEof = 1;. 
350ec 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
350ed 74 65 33 5f 72 65 73 65 74 28 70 43 73 72 2d 3e  te3_reset(pCsr->
350ee 70 53 74 6d 74 29 3b 0a 20 20 20 20 66 74 73 33  pStmt);.    fts3
350ef 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26  GetDeltaVarint(&
350f0 70 43 73 72 2d 3e 70 4e 65 78 74 49 64 2c 20 26  pCsr->pNextId, &
350f1 70 43 73 72 2d 3e 69 50 72 65 76 49 64 29 3b 0a  pCsr->iPrevId);.
350f2 20 20 20 20 70 43 73 72 2d 3e 69 73 52 65 71 75      pCsr->isRequ
350f3 69 72 65 53 65 65 6b 20 3d 20 31 3b 0a 20 20 7d  ireSeek = 1;.  }
350f4 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
350f5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 75 66 66  ../*.** The buff
350f6 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  er pointed to by
350f7 20 61 72 67 75 6d 65 6e 74 20 7a 4e 6f 64 65 20   argument zNode 
350f8 28 73 69 7a 65 20 6e 4e 6f 64 65 20 62 79 74 65  (size nNode byte
350f9 73 29 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a  s) contains the.
350fa 2a 2a 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  ** root node of 
350fb 61 20 62 2d 74 72 65 65 20 73 65 67 6d 65 6e 74  a b-tree segment
350fc 2e 20 54 68 65 20 73 65 67 6d 65 6e 74 20 69 73  . The segment is
350fd 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62   guaranteed to b
350fe 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 6f 6e  e at least.** on
350ff 65 20 6c 65 76 65 6c 20 68 69 67 68 20 28 69 2e  e level high (i.
35100 65 2e 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65  e. the root node
35101 20 69 73 20 6e 6f 74 20 61 6c 73 6f 20 61 20 6c   is not also a l
35102 65 61 66 29 2e 20 49 66 20 73 75 63 63 65 73 73  eaf). If success
35103 66 75 6c 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e  ful,.** this fun
35104 63 74 69 6f 6e 20 6c 6f 63 61 74 65 73 20 74 68  ction locates th
35105 65 20 6c 65 61 66 20 6e 6f 64 65 20 6f 66 20 74  e leaf node of t
35106 68 65 20 73 65 67 6d 65 6e 74 20 74 68 61 74 20  he segment that 
35107 6d 61 79 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  may contain the 
35108 0a 2a 2a 20 74 65 72 6d 20 73 70 65 63 69 66 69  .** term specifi
35109 65 64 20 62 79 20 61 72 67 75 6d 65 6e 74 73 20  ed by arguments 
3510a 7a 54 65 72 6d 20 61 6e 64 20 6e 54 65 72 6d 20  zTerm and nTerm 
3510b 61 6e 64 20 77 72 69 74 65 73 20 69 74 73 20 62  and writes its b
3510c 6c 6f 63 6b 20 6e 75 6d 62 65 72 20 0a 2a 2a 20  lock number .** 
3510d 74 6f 20 2a 70 69 4c 65 61 66 2e 0a 2a 2a 0a 2a  to *piLeaf..**.*
3510e 2a 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65  * It is possible
3510f 20 74 68 61 74 20 74 68 65 20 72 65 74 75 72 6e   that the return
35110 65 64 20 6c 65 61 66 20 6e 6f 64 65 20 64 6f 65  ed leaf node doe
35111 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 74 68  s not contain th
35112 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 74  e specified.** t
35113 65 72 6d 2e 20 48 6f 77 65 76 65 72 2c 20 69 66  erm. However, if
35114 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64 6f 65   the segment doe
35115 73 20 63 6f 6e 74 61 69 6e 20 73 61 69 64 20 74  s contain said t
35116 65 72 6d 2c 20 69 74 20 69 73 20 73 74 6f 72 65  erm, it is store
35117 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 69 64 65 6e  d on.** the iden
35118 74 69 66 69 65 64 20 6c 65 61 66 20 6e 6f 64 65  tified leaf node
35119 2e 20 42 65 63 61 75 73 65 20 74 68 69 73 20 66  . Because this f
3511a 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 69 6e 73  unction only ins
3511b 70 65 63 74 73 20 69 6e 74 65 72 69 6f 72 0a 2a  pects interior.*
3511c 2a 20 73 65 67 6d 65 6e 74 20 6e 6f 64 65 73 20  * segment nodes 
3511d 28 61 6e 64 20 6e 65 76 65 72 20 6c 6f 61 64 73  (and never loads
3511e 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 74 6f   leaf nodes into
3511f 20 6d 65 6d 6f 72 79 29 2c 20 69 74 20 69 73 20   memory), it is 
35120 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20  not possible.** 
35121 74 6f 20 62 65 20 73 75 72 65 2e 0a 2a 2a 0a 2a  to be sure..**.*
35122 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
35123 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 63  curs, an error c
35124 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53  ode other than S
35125 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
35126 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  rned..*/ .static
35127 20 69 6e 74 20 66 74 73 33 53 65 6c 65 63 74 4c   int fts3SelectL
35128 65 61 66 28 0a 20 20 46 74 73 33 54 61 62 6c 65  eaf(.  Fts3Table
35129 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
3512a 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
3512b 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
3512c 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
3512d 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20  zTerm,          
3512e 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 73      /* Term to s
3512f 65 6c 65 63 74 20 6c 65 61 76 65 73 20 66 6f 72  elect leaves for
35130 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c   */.  int nTerm,
35131 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35132 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
35133 20 74 65 72 6d 20 7a 54 65 72 6d 20 69 6e 20 62   term zTerm in b
35134 79 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ytes */.  const 
35135 63 68 61 72 20 2a 7a 4e 6f 64 65 2c 20 20 20 20  char *zNode,    
35136 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66            /* Buf
35137 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73  fer containing s
35138 65 67 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72 20  egment interior 
35139 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e  node */.  int nN
3513a 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ode,            
3513b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
3513c 65 20 6f 66 20 62 75 66 66 65 72 20 61 74 20 7a  e of buffer at z
3513d 4e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  Node */.  sqlite
3513e 33 5f 69 6e 74 36 34 20 2a 70 69 4c 65 61 66 20  3_int64 *piLeaf 
3513f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c            /* Sel
35140 65 63 74 65 64 20 6c 65 61 66 20 6e 6f 64 65 20  ected leaf node 
35141 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
35142 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
35143 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
35144 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73  n code */.  cons
35145 74 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a  t char *zCsr = z
35146 4e 6f 64 65 3b 20 20 20 20 20 20 20 2f 2a 20 43  Node;       /* C
35147 75 72 73 6f 72 20 74 6f 20 69 74 65 72 61 74 65  ursor to iterate
35148 20 74 68 72 6f 75 67 68 20 6e 6f 64 65 20 2a 2f   through node */
35149 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3514a 45 6e 64 20 3d 20 26 7a 43 73 72 5b 6e 4e 6f 64  End = &zCsr[nNod
3514b 65 5d 3b 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 74  e];/* End of int
3514c 65 72 69 6f 72 20 6e 6f 64 65 20 62 75 66 66 65  erior node buffe
3514d 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 75  r */.  char *zBu
3514e 66 66 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20  ffer = 0;       
3514f 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72         /* Buffer
35150 20 74 6f 20 6c 6f 61 64 20 74 65 72 6d 73 20 69   to load terms i
35151 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  nto */.  int nAl
35152 6c 6f 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20  loc = 0;        
35153 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
35154 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 62 75   of allocated bu
35155 66 66 65 72 20 2a 2f 0a 0a 20 20 77 68 69 6c 65  ffer */..  while
35156 28 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ( 1 ){.    int i
35157 73 46 69 72 73 74 54 65 72 6d 20 3d 20 31 3b 20  sFirstTerm = 1; 
35158 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
35159 20 77 68 65 6e 20 70 72 6f 63 65 73 73 69 6e 67   when processing
3515a 20 66 69 72 73 74 20 74 65 72 6d 20 6f 6e 20 70   first term on p
3515b 61 67 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  age */.    int i
3515c 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  Height;         
3515d 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67           /* Heig
3515e 68 74 20 6f 66 20 74 68 69 73 20 6e 6f 64 65 20  ht of this node 
3515f 69 6e 20 74 72 65 65 20 2a 2f 0a 20 20 20 20 73  in tree */.    s
35160 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 43 68  qlite3_int64 iCh
35161 69 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ild;         /* 
35162 42 6c 6f 63 6b 20 69 64 20 6f 66 20 63 68 69 6c  Block id of chil
35163 64 20 6e 6f 64 65 20 74 6f 20 64 65 73 63 65 6e  d node to descen
35164 64 20 74 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20  d to */.    int 
35165 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20  nBlock;         
35166 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
35167 65 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20  e of child node 
35168 69 6e 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20 20  in bytes */..   
35169 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33   zCsr += sqlite3
3516a 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
3516b 7a 43 73 72 2c 20 26 69 48 65 69 67 68 74 29 3b  zCsr, &iHeight);
3516c 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c  .    zCsr += sql
3516d 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
3516e 74 28 7a 43 73 72 2c 20 26 69 43 68 69 6c 64 29  t(zCsr, &iChild)
3516f 3b 0a 20 20 0a 20 20 20 20 77 68 69 6c 65 28 20  ;.  .    while( 
35170 7a 43 73 72 3c 7a 45 6e 64 20 29 7b 0a 20 20 20  zCsr<zEnd ){.   
35171 20 20 20 69 6e 74 20 63 6d 70 3b 20 20 20 20 20     int cmp;     
35172 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35173 2a 20 6d 65 6d 63 6d 70 28 29 20 72 65 73 75 6c  * memcmp() resul
35174 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  t */.      int n
35175 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20  Suffix;         
35176 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
35177 66 20 74 65 72 6d 20 73 75 66 66 69 78 20 2a 2f  f term suffix */
35178 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 72 65 66  .      int nPref
35179 69 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ix = 0;         
3517a 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 65     /* Size of te
3517b 72 6d 20 70 72 65 66 69 78 20 2a 2f 0a 20 20 20  rm prefix */.   
3517c 20 20 20 69 6e 74 20 6e 42 75 66 66 65 72 3b 20     int nBuffer; 
3517d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3517e 2a 20 54 6f 74 61 6c 20 74 65 72 6d 20 73 69 7a  * Total term siz
3517f 65 20 2a 2f 0a 20 20 0a 20 20 20 20 20 20 2f 2a  e */.  .      /*
35180 20 4c 6f 61 64 20 74 68 65 20 6e 65 78 74 20 74   Load the next t
35181 65 72 6d 20 6f 6e 20 74 68 65 20 6e 6f 64 65 20  erm on the node 
35182 69 6e 74 6f 20 7a 42 75 66 66 65 72 20 2a 2f 0a  into zBuffer */.
35183 20 20 20 20 20 20 69 66 28 20 21 69 73 46 69 72        if( !isFir
35184 73 74 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  stTerm ){.      
35185 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65    zCsr += sqlite
35186 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
35187 28 7a 43 73 72 2c 20 26 6e 50 72 65 66 69 78 29  (zCsr, &nPrefix)
35188 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
35189 69 73 46 69 72 73 74 54 65 72 6d 20 3d 20 30 3b  isFirstTerm = 0;
3518a 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73  .      zCsr += s
3518b 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72  qlite3Fts3GetVar
3518c 69 6e 74 33 32 28 7a 43 73 72 2c 20 26 6e 53 75  int32(zCsr, &nSu
3518d 66 66 69 78 29 3b 0a 20 20 20 20 20 20 69 66 28  ffix);.      if(
3518e 20 6e 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78   nPrefix+nSuffix
3518f 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  >nAlloc ){.     
35190 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20     char *zNew;. 
35191 20 20 20 20 20 20 20 6e 41 6c 6c 6f 63 20 3d 20         nAlloc = 
35192 28 6e 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78  (nPrefix+nSuffix
35193 29 20 2a 20 32 3b 0a 20 20 20 20 20 20 20 20 7a  ) * 2;.        z
35194 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73 71  New = (char *)sq
35195 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 42  lite3_realloc(zB
35196 75 66 66 65 72 2c 20 6e 41 6c 6c 6f 63 29 3b 0a  uffer, nAlloc);.
35197 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 4e 65          if( !zNe
35198 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  w ){.          s
35199 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 42 75 66  qlite3_free(zBuf
3519a 66 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  fer);.          
3519b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
3519c 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  MEM;.        }. 
3519d 20 20 20 20 20 20 20 7a 42 75 66 66 65 72 20 3d         zBuffer =
3519e 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20   zNew;.      }. 
3519f 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75       memcpy(&zBu
351a0 66 66 65 72 5b 6e 50 72 65 66 69 78 5d 2c 20 7a  ffer[nPrefix], z
351a1 43 73 72 2c 20 6e 53 75 66 66 69 78 29 3b 0a 20  Csr, nSuffix);. 
351a2 20 20 20 20 20 6e 42 75 66 66 65 72 20 3d 20 6e       nBuffer = n
351a3 50 72 65 66 69 78 20 2b 20 6e 53 75 66 66 69 78  Prefix + nSuffix
351a4 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20  ;.      zCsr += 
351a5 6e 53 75 66 66 69 78 3b 0a 20 20 0a 20 20 20 20  nSuffix;.  .    
351a6 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20 74 68 65    /* Compare the
351a7 20 74 65 72 6d 20 77 65 20 61 72 65 20 73 65 61   term we are sea
351a8 72 63 68 69 6e 67 20 66 6f 72 20 77 69 74 68 20  rching for with 
351a9 74 68 65 20 74 65 72 6d 20 6a 75 73 74 20 6c 6f  the term just lo
351aa 61 64 65 64 20 66 72 6f 6d 0a 20 20 20 20 20 20  aded from.      
351ab 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20  ** the interior 
351ac 6e 6f 64 65 2e 20 49 66 20 74 68 65 20 73 70 65  node. If the spe
351ad 63 69 66 69 65 64 20 74 65 72 6d 20 69 73 20 67  cified term is g
351ae 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
351af 71 75 61 6c 0a 20 20 20 20 20 20 2a 2a 20 74 6f  qual.      ** to
351b0 20 74 68 65 20 74 65 72 6d 20 66 72 6f 6d 20 74   the term from t
351b1 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  he interior node
351b2 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73  , then all terms
351b3 20 6f 6e 20 74 68 65 20 73 75 62 2d 74 72 65 65   on the sub-tree
351b4 20 0a 20 20 20 20 20 20 2a 2a 20 68 65 61 64 65   .      ** heade
351b5 64 20 62 79 20 6e 6f 64 65 20 69 43 68 69 6c 64  d by node iChild
351b6 20 61 72 65 20 73 6d 61 6c 6c 65 72 20 74 68 61   are smaller tha
351b7 6e 20 7a 54 65 72 6d 2e 20 4e 6f 20 6e 65 65 64  n zTerm. No need
351b8 20 74 6f 20 73 65 61 72 63 68 20 0a 20 20 20 20   to search .    
351b9 20 20 2a 2a 20 69 43 68 69 6c 64 2e 0a 20 20 20    ** iChild..   
351ba 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49     **.      ** I
351bb 66 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e  f the interior n
351bc 6f 64 65 20 74 65 72 6d 20 69 73 20 6c 61 72 67  ode term is larg
351bd 65 72 20 74 68 61 6e 20 74 68 65 20 73 70 65 63  er than the spec
351be 69 66 69 65 64 20 74 65 72 6d 2c 20 74 68 65 6e  ified term, then
351bf 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 74 72  .      ** the tr
351c0 65 65 20 68 65 61 64 65 64 20 62 79 20 69 43 68  ee headed by iCh
351c1 69 6c 64 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  ild may contain 
351c2 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74 65  the specified te
351c3 72 6d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  rm..      */.   
351c4 20 20 20 63 6d 70 20 3d 20 6d 65 6d 63 6d 70 28     cmp = memcmp(
351c5 7a 54 65 72 6d 2c 20 7a 42 75 66 66 65 72 2c 20  zTerm, zBuffer, 
351c6 28 6e 42 75 66 66 65 72 3e 6e 54 65 72 6d 20 3f  (nBuffer>nTerm ?
351c7 20 6e 54 65 72 6d 20 3a 20 6e 42 75 66 66 65 72   nTerm : nBuffer
351c8 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 6d  ));.      if( cm
351c9 70 3c 30 20 7c 7c 20 28 63 6d 70 3d 3d 30 20 26  p<0 || (cmp==0 &
351ca 26 20 6e 42 75 66 66 65 72 3e 6e 54 65 72 6d 29  & nBuffer>nTerm)
351cb 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
351cc 69 43 68 69 6c 64 2b 2b 3b 0a 20 20 20 20 7d 3b  iChild++;.    };
351cd 0a 0a 20 20 20 20 2f 2a 20 49 66 20 28 69 48 65  ..    /* If (iHe
351ce 69 67 68 74 3d 3d 31 29 2c 20 74 68 65 20 63 68  ight==1), the ch
351cf 69 6c 64 72 65 6e 20 6f 66 20 74 68 69 73 20 69  ildren of this i
351d0 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 61 72 65  nterior node are
351d1 20 6c 65 61 76 65 73 2e 20 54 68 65 0a 20 20 20   leaves. The.   
351d2 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 74 65   ** specified te
351d3 72 6d 20 6d 61 79 20 62 65 20 70 72 65 73 65 6e  rm may be presen
351d4 74 20 6f 6e 20 6c 65 61 66 20 6e 6f 64 65 20 69  t on leaf node i
351d5 43 68 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  Child..    */.  
351d6 20 20 69 66 28 20 69 48 65 69 67 68 74 3d 3d 31    if( iHeight==1
351d7 20 29 7b 0a 20 20 20 20 20 20 2a 70 69 4c 65 61   ){.      *piLea
351d8 66 20 3d 20 69 43 68 69 6c 64 3b 0a 20 20 20 20  f = iChild;.    
351d9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
351da 20 20 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 74      /* Descend t
351db 6f 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  o interior node 
351dc 69 43 68 69 6c 64 2e 20 2a 2f 0a 20 20 20 20 72  iChild. */.    r
351dd 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 52  c = sqlite3Fts3R
351de 65 61 64 42 6c 6f 63 6b 28 70 2c 20 69 43 68 69  eadBlock(p, iChi
351df 6c 64 2c 20 26 7a 43 73 72 2c 20 26 6e 42 6c 6f  ld, &zCsr, &nBlo
351e0 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ck);.    if( rc!
351e1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65  =SQLITE_OK ) bre
351e2 61 6b 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20 26  ak;.    zEnd = &
351e3 7a 43 73 72 5b 6e 42 6c 6f 63 6b 5d 3b 0a 20 20  zCsr[nBlock];.  
351e4 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
351e5 28 7a 42 75 66 66 65 72 29 3b 0a 20 20 72 65 74  (zBuffer);.  ret
351e6 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69  urn rc;.}..stati
351e7 63 20 76 6f 69 64 20 66 74 73 33 50 75 74 44 65  c void fts3PutDe
351e8 6c 74 61 56 61 72 69 6e 74 28 0a 20 20 63 68 61  ltaVarint(.  cha
351e9 72 20 2a 2a 70 70 2c 20 0a 20 20 73 71 6c 69 74  r **pp, .  sqlit
351ea 65 33 5f 69 6e 74 36 34 20 2a 70 69 50 72 65 76  e3_int64 *piPrev
351eb 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  , .  sqlite3_int
351ec 36 34 20 69 56 61 6c 0a 29 7b 0a 20 20 61 73 73  64 iVal.){.  ass
351ed 65 72 74 28 20 69 56 61 6c 2d 2a 70 69 50 72 65  ert( iVal-*piPre
351ee 76 20 3e 20 30 20 29 3b 0a 20 20 2a 70 70 20 2b  v > 0 );.  *pp +
351ef 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  = sqlite3Fts3Put
351f0 56 61 72 69 6e 74 28 2a 70 70 2c 20 69 56 61 6c  Varint(*pp, iVal
351f1 2d 2a 70 69 50 72 65 76 29 3b 0a 20 20 2a 70 69  -*piPrev);.  *pi
351f2 50 72 65 76 20 3d 20 69 56 61 6c 3b 0a 7d 0a 0a  Prev = iVal;.}..
351f3 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
351f4 50 6f 73 6c 69 73 74 43 6f 70 79 28 63 68 61 72  PoslistCopy(char
351f5 20 2a 2a 70 70 2c 20 63 68 61 72 20 2a 2a 70 70   **pp, char **pp
351f6 50 6f 73 6c 69 73 74 29 7b 0a 20 20 63 68 61 72  Poslist){.  char
351f7 20 2a 70 45 6e 64 20 3d 20 2a 70 70 50 6f 73 6c   *pEnd = *ppPosl
351f8 69 73 74 3b 0a 20 20 63 68 61 72 20 63 20 3d 20  ist;.  char c = 
351f9 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a 70 45 6e  0;.  while( *pEn
351fa 64 20 7c 20 63 20 29 20 63 20 3d 20 2a 70 45 6e  d | c ) c = *pEn
351fb 64 2b 2b 20 26 20 30 78 38 30 3b 0a 20 20 70 45  d++ & 0x80;.  pE
351fc 6e 64 2b 2b 3b 0a 20 20 69 66 28 20 70 70 20 29  nd++;.  if( pp )
351fd 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45  {.    int n = pE
351fe 6e 64 20 2d 20 2a 70 70 50 6f 73 6c 69 73 74 3b  nd - *ppPoslist;
351ff 0a 20 20 20 20 63 68 61 72 20 2a 70 20 3d 20 2a  .    char *p = *
35200 70 70 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  pp;.    memcpy(p
35201 2c 20 2a 70 70 50 6f 73 6c 69 73 74 2c 20 6e 29  , *ppPoslist, n)
35202 3b 0a 20 20 20 20 70 20 2b 3d 20 6e 3b 0a 20 20  ;.    p += n;.  
35203 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 7d 0a 20    *pp = p;.  }. 
35204 20 2a 70 70 50 6f 73 6c 69 73 74 20 3d 20 70 45   *ppPoslist = pE
35205 6e 64 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  nd;.}..static vo
35206 69 64 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69 73  id fts3Columnlis
35207 74 43 6f 70 79 28 63 68 61 72 20 2a 2a 70 70 2c  tCopy(char **pp,
35208 20 63 68 61 72 20 2a 2a 70 70 50 6f 73 6c 69 73   char **ppPoslis
35209 74 29 7b 0a 20 20 63 68 61 72 20 2a 70 45 6e 64  t){.  char *pEnd
3520a 20 3d 20 2a 70 70 50 6f 73 6c 69 73 74 3b 0a 20   = *ppPoslist;. 
3520b 20 63 68 61 72 20 63 20 3d 20 30 3b 0a 20 20 77   char c = 0;.  w
3520c 68 69 6c 65 28 20 30 78 46 45 20 26 20 28 2a 70  hile( 0xFE & (*p
3520d 45 6e 64 20 7c 20 63 29 20 29 20 63 20 3d 20 2a  End | c) ) c = *
3520e 70 45 6e 64 2b 2b 20 26 20 30 78 38 30 3b 0a 20  pEnd++ & 0x80;. 
3520f 20 69 66 28 20 70 70 20 29 7b 0a 20 20 20 20 69   if( pp ){.    i
35210 6e 74 20 6e 20 3d 20 70 45 6e 64 20 2d 20 2a 70  nt n = pEnd - *p
35211 70 50 6f 73 6c 69 73 74 3b 0a 20 20 20 20 63 68  pPoslist;.    ch
35212 61 72 20 2a 70 20 3d 20 2a 70 70 3b 0a 20 20 20  ar *p = *pp;.   
35213 20 6d 65 6d 63 70 79 28 70 2c 20 2a 70 70 50 6f   memcpy(p, *ppPo
35214 73 6c 69 73 74 2c 20 6e 29 3b 0a 20 20 20 20 70  slist, n);.    p
35215 20 2b 3d 20 6e 3b 0a 20 20 20 20 2a 70 70 20 3d   += n;.    *pp =
35216 20 70 3b 0a 20 20 7d 0a 20 20 2a 70 70 50 6f 73   p;.  }.  *ppPos
35217 6c 69 73 74 20 3d 20 70 45 6e 64 3b 0a 7d 0a 0a  list = pEnd;.}..
35218 2f 2a 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20  /*.**.*/.static 
35219 76 6f 69 64 20 66 74 73 33 50 6f 73 6c 69 73 74  void fts3Poslist
3521a 4d 65 72 67 65 28 0a 20 20 63 68 61 72 20 2a 2a  Merge(.  char **
3521b 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pp,             
3521c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
3521d 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63  ut buffer */.  c
3521e 68 61 72 20 2a 2a 70 70 31 2c 20 20 20 20 20 20  har **pp1,      
3521f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35220 2a 20 4c 65 66 74 20 69 6e 70 75 74 20 6c 69 73  * Left input lis
35221 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70  t */.  char **pp
35222 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
35223 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20         /* Right 
35224 69 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 29 7b  input list */.){
35225 0a 20 20 63 68 61 72 20 2a 70 20 3d 20 2a 70 70  .  char *p = *pp
35226 3b 0a 20 20 63 68 61 72 20 2a 70 31 20 3d 20 2a  ;.  char *p1 = *
35227 70 70 31 3b 0a 20 20 63 68 61 72 20 2a 70 32 20  pp1;.  char *p2 
35228 3d 20 2a 70 70 32 3b 0a 0a 20 20 77 68 69 6c 65  = *pp2;..  while
35229 28 20 2a 70 31 20 26 26 20 2a 70 32 20 29 7b 0a  ( *p1 && *p2 ){.
3522a 20 20 20 20 69 6e 74 20 69 43 6f 6c 31 20 3d 20      int iCol1 = 
3522b 30 3b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 32  0;.    int iCol2
3522c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 2a 70   = 0;.    if( *p
3522d 31 3d 3d 30 78 30 31 20 29 20 73 71 6c 69 74 65  1==0x01 ) sqlite
3522e 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
3522f 28 26 70 31 5b 31 5d 2c 20 26 69 43 6f 6c 31 29  (&p1[1], &iCol1)
35230 3b 0a 20 20 20 20 69 66 28 20 2a 70 32 3d 3d 30  ;.    if( *p2==0
35231 78 30 31 20 29 20 73 71 6c 69 74 65 33 46 74 73  x01 ) sqlite3Fts
35232 33 47 65 74 56 61 72 69 6e 74 33 32 28 26 70 32  3GetVarint32(&p2
35233 5b 31 5d 2c 20 26 69 43 6f 6c 32 29 3b 0a 0a 20  [1], &iCol2);.. 
35234 20 20 20 69 66 28 20 69 43 6f 6c 31 3d 3d 69 43     if( iCol1==iC
35235 6f 6c 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ol2 ){.      sql
35236 69 74 65 33 5f 69 6e 74 36 34 20 69 31 20 3d 20  ite3_int64 i1 = 
35237 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
35238 5f 69 6e 74 36 34 20 69 32 20 3d 20 30 3b 0a 20  _int64 i2 = 0;. 
35239 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
3523a 36 34 20 69 50 72 65 76 20 3d 20 30 3b 0a 20 20  64 iPrev = 0;.  
3523b 20 20 20 20 69 66 28 20 69 43 6f 6c 31 21 3d 30      if( iCol1!=0
3523c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
3523d 6e 3b 0a 20 20 20 20 20 20 20 20 2a 70 2b 2b 20  n;.        *p++ 
3523e 3d 20 30 78 30 31 3b 0a 20 20 20 20 20 20 20 20  = 0x01;.        
3523f 6e 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50  n = sqlite3Fts3P
35240 75 74 56 61 72 69 6e 74 28 70 2c 20 69 43 6f 6c  utVarint(p, iCol
35241 31 29 3b 0a 20 20 20 20 20 20 20 20 70 20 2b 3d  1);.        p +=
35242 20 6e 3b 0a 20 20 20 20 20 20 20 20 70 31 20 2b   n;.        p1 +
35243 3d 20 31 20 2b 20 6e 3b 0a 20 20 20 20 20 20 20  = 1 + n;.       
35244 20 70 32 20 2b 3d 20 31 20 2b 20 6e 3b 0a 20 20   p2 += 1 + n;.  
35245 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c      }.      whil
35246 65 28 20 28 2a 70 31 26 30 78 46 45 29 20 7c 7c  e( (*p1&0xFE) ||
35247 20 28 2a 70 32 26 30 78 46 45 29 20 29 7b 0a 20   (*p2&0xFE) ){. 
35248 20 20 20 20 20 20 20 69 66 28 20 69 31 3d 3d 69         if( i1==i
35249 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  2 ){.          f
3524a 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e  ts3GetDeltaVarin
3524b 74 28 26 70 31 2c 20 26 69 31 29 3b 20 69 31 20  t(&p1, &i1); i1 
3524c 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20  -= 2;.          
3524d 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
3524e 6e 74 28 26 70 32 2c 20 26 69 32 29 3b 20 69 32  nt(&p2, &i2); i2
3524f 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 7d   -= 2;.        }
35250 65 6c 73 65 20 69 66 28 20 69 31 3c 69 32 20 29  else if( i1<i2 )
35251 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  {.          fts3
35252 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26  GetDeltaVarint(&
35253 70 31 2c 20 26 69 31 29 3b 20 69 31 20 2d 3d 20  p1, &i1); i1 -= 
35254 32 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  2;.        }else
35255 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  {.          fts3
35256 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26  GetDeltaVarint(&
35257 70 32 2c 20 26 69 32 29 3b 20 69 32 20 2d 3d 20  p2, &i2); i2 -= 
35258 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  2;.        }.   
35259 20 20 20 20 20 66 74 73 33 50 75 74 44 65 6c 74       fts3PutDelt
3525a 61 56 61 72 69 6e 74 28 26 70 2c 20 26 69 50 72  aVarint(&p, &iPr
3525b 65 76 2c 20 28 69 31 3c 69 32 20 3f 20 69 31 20  ev, (i1<i2 ? i1 
3525c 3a 20 69 32 29 20 2b 20 32 29 3b 20 69 50 72 65  : i2) + 2); iPre
3525d 76 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20 20  v -= 2;.        
3525e 69 66 28 20 30 3d 3d 28 2a 70 31 26 30 78 46 45  if( 0==(*p1&0xFE
3525f 29 20 29 20 69 31 20 3d 20 30 78 37 46 46 46 46  ) ) i1 = 0x7FFFF
35260 46 46 46 3b 0a 20 20 20 20 20 20 20 20 69 66 28  FFF;.        if(
35261 20 30 3d 3d 28 2a 70 32 26 30 78 46 45 29 20 29   0==(*p2&0xFE) )
35262 20 69 32 20 3d 20 30 78 37 46 46 46 46 46 46 46   i2 = 0x7FFFFFFF
35263 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
35264 6c 73 65 20 69 66 28 20 69 43 6f 6c 31 3c 69 43  lse if( iCol1<iC
35265 6f 6c 32 20 29 7b 0a 20 20 20 20 20 20 66 74 73  ol2 ){.      fts
35266 33 43 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28  3ColumnlistCopy(
35267 26 70 2c 20 26 70 31 29 3b 0a 20 20 20 20 7d 65  &p, &p1);.    }e
35268 6c 73 65 7b 0a 20 20 20 20 20 20 66 74 73 33 43  lse{.      fts3C
35269 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 26 70  olumnlistCopy(&p
3526a 2c 20 26 70 32 29 3b 0a 20 20 20 20 7d 0a 20 20  , &p2);.    }.  
3526b 7d 0a 0a 20 20 2a 70 2b 2b 20 3d 20 27 5c 30 27  }..  *p++ = '\0'
3526c 3b 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 2a  ;.  *pp = p;.  *
3526d 70 70 31 20 3d 20 70 31 20 2b 20 31 3b 0a 20 20  pp1 = p1 + 1;.  
3526e 2a 70 70 32 20 3d 20 70 32 20 2b 20 31 3b 0a 7d  *pp2 = p2 + 1;.}
3526f 0a 0a 2f 2a 0a 2a 2a 20 6e 54 6f 6b 65 6e 3d 3d  ../*.** nToken==
35270 31 20 73 65 61 72 63 68 65 73 20 66 6f 72 20 61  1 searches for a
35271 64 6a 61 63 65 6e 74 20 70 6f 73 69 74 69 6f 6e  djacent position
35272 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
35273 20 66 74 73 33 50 6f 73 6c 69 73 74 50 68 72 61   fts3PoslistPhra
35274 73 65 4d 65 72 67 65 28 0a 20 20 63 68 61 72 20  seMerge(.  char 
35275 2a 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20  **pp,           
35276 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
35277 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20  tput buffer */. 
35278 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 20 20 20   int nToken,    
35279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3527a 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 64 69 66 66   /* Maximum diff
3527b 65 72 65 6e 63 65 20 69 6e 20 74 6f 6b 65 6e 20  erence in token 
3527c 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  positions */.  i
3527d 6e 74 20 69 73 53 61 76 65 4c 65 66 74 2c 20 20  nt isSaveLeft,  
3527e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3527f 2a 20 53 61 76 65 20 74 68 65 20 6c 65 66 74 20  * Save the left 
35280 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 63 68  position */.  ch
35281 61 72 20 2a 2a 70 70 31 2c 20 20 20 20 20 20 20  ar **pp1,       
35282 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35283 20 4c 65 66 74 20 69 6e 70 75 74 20 6c 69 73 74   Left input list
35284 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70 32   */.  char **pp2
35285 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35286 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 69        /* Right i
35287 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 29 7b 0a  nput list */.){.
35288 20 20 63 68 61 72 20 2a 70 20 3d 20 28 70 70 20    char *p = (pp 
35289 3f 20 2a 70 70 20 3a 20 30 29 3b 0a 20 20 63 68  ? *pp : 0);.  ch
3528a 61 72 20 2a 70 31 20 3d 20 2a 70 70 31 3b 0a 20  ar *p1 = *pp1;. 
3528b 20 63 68 61 72 20 2a 70 32 20 3d 20 2a 70 70 32   char *p2 = *pp2
3528c 3b 0a 0a 20 20 69 6e 74 20 69 43 6f 6c 31 20 3d  ;..  int iCol1 =
3528d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 32 20   0;.  int iCol2 
3528e 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 2a  = 0;.  assert( *
3528f 70 31 21 3d 30 20 26 26 20 2a 70 32 21 3d 30 20  p1!=0 && *p2!=0 
35290 29 3b 0a 20 20 69 66 28 20 2a 70 31 3d 3d 30 78  );.  if( *p1==0x
35291 30 31 20 29 7b 20 0a 20 20 20 20 70 31 2b 2b 3b  01 ){ .    p1++;
35292 0a 20 20 20 20 70 31 20 2b 3d 20 73 71 6c 69 74  .    p1 += sqlit
35293 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33  e3Fts3GetVarint3
35294 32 28 70 31 2c 20 26 69 43 6f 6c 31 29 3b 0a 20  2(p1, &iCol1);. 
35295 20 7d 0a 20 20 69 66 28 20 2a 70 32 3d 3d 30 78   }.  if( *p2==0x
35296 30 31 20 29 7b 20 0a 20 20 20 20 70 32 2b 2b 3b  01 ){ .    p2++;
35297 0a 20 20 20 20 70 32 20 2b 3d 20 73 71 6c 69 74  .    p2 += sqlit
35298 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33  e3Fts3GetVarint3
35299 32 28 70 32 2c 20 26 69 43 6f 6c 32 29 3b 0a 20  2(p2, &iCol2);. 
3529a 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 31 20 29   }..  while( 1 )
3529b 7b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 31 3d  {.    if( iCol1=
3529c 3d 69 43 6f 6c 32 20 29 7b 0a 20 20 20 20 20 20  =iCol2 ){.      
3529d 63 68 61 72 20 2a 70 53 61 76 65 20 3d 20 70 3b  char *pSave = p;
3529e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69  .      sqlite3_i
3529f 6e 74 36 34 20 69 50 72 65 76 20 3d 20 30 3b 0a  nt64 iPrev = 0;.
352a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
352a1 74 36 34 20 69 50 6f 73 31 20 3d 20 30 3b 0a 20  t64 iPos1 = 0;. 
352a2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
352a3 36 34 20 69 50 6f 73 32 20 3d 20 30 3b 0a 0a 20  64 iPos2 = 0;.. 
352a4 20 20 20 20 20 69 66 28 20 70 70 20 26 26 20 69       if( pp && i
352a5 43 6f 6c 31 20 29 7b 0a 20 20 20 20 20 20 20 20  Col1 ){.        
352a6 2a 70 2b 2b 20 3d 20 30 78 30 31 3b 0a 20 20 20  *p++ = 0x01;.   
352a7 20 20 20 20 20 70 20 2b 3d 20 73 71 6c 69 74 65       p += sqlite
352a8 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 70  3Fts3PutVarint(p
352a9 2c 20 69 43 6f 6c 31 29 3b 0a 20 20 20 20 20 20  , iCol1);.      
352aa 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  }..      assert(
352ab 20 2a 70 31 21 3d 30 78 30 30 20 26 26 20 2a 70   *p1!=0x00 && *p
352ac 32 21 3d 30 78 30 30 20 26 26 20 2a 70 31 21 3d  2!=0x00 && *p1!=
352ad 30 78 30 31 20 26 26 20 2a 70 32 21 3d 30 78 30  0x01 && *p2!=0x0
352ae 31 20 29 3b 0a 20 20 20 20 20 20 66 74 73 33 47  1 );.      fts3G
352af 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70  etDeltaVarint(&p
352b0 31 2c 20 26 69 50 6f 73 31 29 3b 20 69 50 6f 73  1, &iPos1); iPos
352b1 31 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 66 74  1 -= 2;.      ft
352b2 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
352b3 28 26 70 32 2c 20 26 69 50 6f 73 32 29 3b 20 69  (&p2, &iPos2); i
352b4 50 6f 73 32 20 2d 3d 20 32 3b 0a 0a 20 20 20 20  Pos2 -= 2;..    
352b5 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20    while( 1 ){.  
352b6 20 20 20 20 20 20 69 66 28 20 69 50 6f 73 32 3e        if( iPos2>
352b7 69 50 6f 73 31 20 26 26 20 69 50 6f 73 32 3c 3d  iPos1 && iPos2<=
352b8 69 50 6f 73 31 2b 6e 54 6f 6b 65 6e 20 29 7b 0a  iPos1+nToken ){.
352b9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
352ba 33 5f 69 6e 74 36 34 20 69 53 61 76 65 3b 0a 20  3_int64 iSave;. 
352bb 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 70           if( !pp
352bc 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
352bd 66 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28  fts3PoslistCopy(
352be 30 2c 20 26 70 32 29 3b 0a 20 20 20 20 20 20 20  0, &p2);.       
352bf 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74       fts3Poslist
352c0 43 6f 70 79 28 30 2c 20 26 70 31 29 3b 0a 20 20  Copy(0, &p1);.  
352c1 20 20 20 20 20 20 20 20 20 20 2a 70 70 31 20 3d            *pp1 =
352c2 20 70 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20   p1;.           
352c3 20 2a 70 70 32 20 3d 20 70 32 3b 0a 20 20 20 20   *pp2 = p2;.    
352c4 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
352c5 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
352c6 20 20 20 20 20 20 20 20 69 53 61 76 65 20 3d 20          iSave = 
352c7 69 73 53 61 76 65 4c 65 66 74 20 3f 20 69 50 6f  isSaveLeft ? iPo
352c8 73 31 20 3a 20 69 50 6f 73 32 3b 0a 20 20 20 20  s1 : iPos2;.    
352c9 20 20 20 20 20 20 66 74 73 33 50 75 74 44 65 6c        fts3PutDel
352ca 74 61 56 61 72 69 6e 74 28 26 70 2c 20 26 69 50  taVarint(&p, &iP
352cb 72 65 76 2c 20 69 53 61 76 65 2b 32 29 3b 20 69  rev, iSave+2); i
352cc 50 72 65 76 20 2d 3d 20 32 3b 0a 20 20 20 20 20  Prev -= 2;.     
352cd 20 20 20 20 20 70 53 61 76 65 20 3d 20 30 3b 0a       pSave = 0;.
352ce 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
352cf 20 20 69 66 28 20 69 50 6f 73 32 3c 3d 69 50 6f    if( iPos2<=iPo
352d0 73 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  s1 ){.          
352d1 69 66 28 20 28 2a 70 32 26 30 78 46 45 29 3d 3d  if( (*p2&0xFE)==
352d2 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
352d3 20 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74       fts3GetDelt
352d4 61 56 61 72 69 6e 74 28 26 70 32 2c 20 26 69 50  aVarint(&p2, &iP
352d5 6f 73 32 29 3b 20 69 50 6f 73 32 20 2d 3d 20 32  os2); iPos2 -= 2
352d6 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
352d7 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28  .          if( (
352d8 2a 70 31 26 30 78 46 45 29 3d 3d 30 20 29 20 62  *p1&0xFE)==0 ) b
352d9 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
352da 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
352db 6e 74 28 26 70 31 2c 20 26 69 50 6f 73 31 29 3b  nt(&p1, &iPos1);
352dc 20 69 50 6f 73 31 20 2d 3d 20 32 3b 0a 20 20 20   iPos1 -= 2;.   
352dd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
352de 20 20 20 20 20 69 66 28 20 70 53 61 76 65 20 26       if( pSave &
352df 26 20 70 70 20 29 7b 0a 20 20 20 20 20 20 20 20  & pp ){.        
352e0 70 20 3d 20 70 53 61 76 65 3b 0a 20 20 20 20 20  p = pSave;.     
352e1 20 7d 0a 0a 20 20 20 20 20 20 66 74 73 33 43 6f   }..      fts3Co
352e2 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30 2c 20  lumnlistCopy(0, 
352e3 26 70 31 29 3b 0a 20 20 20 20 20 20 66 74 73 33  &p1);.      fts3
352e4 43 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30  ColumnlistCopy(0
352e5 2c 20 26 70 32 29 3b 0a 20 20 20 20 20 20 61 73  , &p2);.      as
352e6 73 65 72 74 28 20 28 2a 70 31 26 30 78 46 45 29  sert( (*p1&0xFE)
352e7 3d 3d 30 20 26 26 20 28 2a 70 32 26 30 78 46 45  ==0 && (*p2&0xFE
352e8 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  )==0 );.      if
352e9 28 20 30 3d 3d 2a 70 31 20 7c 7c 20 30 3d 3d 2a  ( 0==*p1 || 0==*
352ea 70 32 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20  p2 ) break;..   
352eb 20 20 20 70 31 2b 2b 3b 0a 20 20 20 20 20 20 70     p1++;.      p
352ec 31 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33  1 += sqlite3Fts3
352ed 47 65 74 56 61 72 69 6e 74 33 32 28 70 31 2c 20  GetVarint32(p1, 
352ee 26 69 43 6f 6c 31 29 3b 0a 20 20 20 20 20 20 70  &iCol1);.      p
352ef 32 2b 2b 3b 0a 20 20 20 20 20 20 70 32 20 2b 3d  2++;.      p2 +=
352f0 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
352f1 61 72 69 6e 74 33 32 28 70 32 2c 20 26 69 43 6f  arint32(p2, &iCo
352f2 6c 32 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  l2);.    }..    
352f3 2f 2a 20 41 64 76 61 6e 63 65 20 70 6f 69 6e 74  /* Advance point
352f4 65 72 20 70 31 20 6f 72 20 70 32 20 28 77 68 69  er p1 or p2 (whi
352f5 63 68 65 76 65 72 20 63 6f 72 72 65 73 70 6f 6e  chever correspon
352f6 64 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65  ds to the smalle
352f7 72 20 6f 66 0a 20 20 20 20 2a 2a 20 69 43 6f 6c  r of.    ** iCol
352f8 31 20 61 6e 64 20 69 43 6f 6c 32 29 20 73 6f 20  1 and iCol2) so 
352f9 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
352fa 6f 20 65 69 74 68 65 72 20 74 68 65 20 30 78 30  o either the 0x0
352fb 30 20 74 68 61 74 20 6d 61 72 6b 73 20 74 68 65  0 that marks the
352fc 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74  .    ** end of t
352fd 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74  he position list
352fe 2c 20 6f 72 20 74 68 65 20 30 78 30 31 20 74 68  , or the 0x01 th
352ff 61 74 20 70 72 65 63 65 64 65 73 20 74 68 65 20  at precedes the 
35300 6e 65 78 74 20 0a 20 20 20 20 2a 2a 20 63 6f 6c  next .    ** col
35301 75 6d 6e 2d 6e 75 6d 62 65 72 20 69 6e 20 74 68  umn-number in th
35302 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2e  e position list.
35303 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 6c 73   .    */.    els
35304 65 20 69 66 28 20 69 43 6f 6c 31 3c 69 43 6f 6c  e if( iCol1<iCol
35305 32 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 43  2 ){.      fts3C
35306 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30 2c  olumnlistCopy(0,
35307 20 26 70 31 29 3b 0a 20 20 20 20 20 20 69 66 28   &p1);.      if(
35308 20 30 3d 3d 2a 70 31 20 29 20 62 72 65 61 6b 3b   0==*p1 ) break;
35309 0a 20 20 20 20 20 20 70 31 2b 2b 3b 0a 20 20 20  .      p1++;.   
3530a 20 20 20 70 31 20 2b 3d 20 73 71 6c 69 74 65 33     p1 += sqlite3
3530b 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
3530c 70 31 2c 20 26 69 43 6f 6c 31 29 3b 0a 20 20 20  p1, &iCol1);.   
3530d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 74   }else{.      ft
3530e 73 33 43 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79  s3ColumnlistCopy
3530f 28 30 2c 20 26 70 32 29 3b 0a 20 20 20 20 20 20  (0, &p2);.      
35310 69 66 28 20 30 3d 3d 2a 70 32 20 29 20 62 72 65  if( 0==*p2 ) bre
35311 61 6b 3b 0a 20 20 20 20 20 20 70 32 2b 2b 3b 0a  ak;.      p2++;.
35312 20 20 20 20 20 20 70 32 20 2b 3d 20 73 71 6c 69        p2 += sqli
35313 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74  te3Fts3GetVarint
35314 33 32 28 70 32 2c 20 26 69 43 6f 6c 32 29 3b 0a  32(p2, &iCol2);.
35315 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 74 73      }.  }..  fts
35316 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c 20  3PoslistCopy(0, 
35317 26 70 32 29 3b 0a 20 20 66 74 73 33 50 6f 73 6c  &p2);.  fts3Posl
35318 69 73 74 43 6f 70 79 28 30 2c 20 26 70 31 29 3b  istCopy(0, &p1);
35319 0a 20 20 2a 70 70 31 20 3d 20 70 31 3b 0a 20 20  .  *pp1 = p1;.  
3531a 2a 70 70 32 20 3d 20 70 32 3b 0a 20 20 69 66 28  *pp2 = p2;.  if(
3531b 20 21 70 70 20 7c 7c 20 2a 70 70 3d 3d 70 20 29   !pp || *pp==p )
3531c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
3531d 20 20 7d 0a 20 20 2a 70 2b 2b 20 3d 20 30 78 30    }.  *p++ = 0x0
3531e 30 3b 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20  0;.  *pp = p;.  
3531f 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61  return 1;.}..sta
35320 74 69 63 20 69 6e 74 20 66 74 73 33 50 6f 73 6c  tic int fts3Posl
35321 69 73 74 4e 65 61 72 4d 65 72 67 65 28 0a 20 20  istNearMerge(.  
35322 63 68 61 72 20 2a 2a 70 70 2c 20 20 20 20 20 20  char **pp,      
35323 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35324 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72  /* Output buffer
35325 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 54 6d 70   */.  char *aTmp
35326 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35327 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61        /* Tempora
35328 72 79 20 62 75 66 66 65 72 20 73 70 61 63 65 20  ry buffer space 
35329 2a 2f 0a 20 20 69 6e 74 20 6e 52 69 67 68 74 2c  */.  int nRight,
3532a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3532b 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
3532c 64 69 66 66 65 72 65 6e 63 65 20 69 6e 20 74 6f  difference in to
3532d 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 73 20 2a 2f  ken positions */
3532e 0a 20 20 69 6e 74 20 6e 4c 65 66 74 2c 20 20 20  .  int nLeft,   
3532f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35330 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 64 69     /* Maximum di
35331 66 66 65 72 65 6e 63 65 20 69 6e 20 74 6f 6b 65  fference in toke
35332 6e 20 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20  n positions */. 
35333 20 63 68 61 72 20 2a 2a 70 70 31 2c 20 20 20 20   char **pp1,    
35334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35335 20 2f 2a 20 4c 65 66 74 20 69 6e 70 75 74 20 6c   /* Left input l
35336 69 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ist */.  char **
35337 70 70 32 20 20 20 20 20 20 20 20 20 20 20 20 20  pp2             
35338 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68           /* Righ
35339 74 20 69 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a  t input list */.
3533a 29 7b 0a 20 20 63 68 61 72 20 2a 70 31 20 3d 20  ){.  char *p1 = 
3533b 2a 70 70 31 3b 0a 20 20 63 68 61 72 20 2a 70 32  *pp1;.  char *p2
3533c 20 3d 20 2a 70 70 32 3b 0a 0a 20 20 69 66 28 20   = *pp2;..  if( 
3533d 21 70 70 20 29 7b 0a 20 20 20 20 69 66 28 20 66  !pp ){.    if( f
3533e 74 73 33 50 6f 73 6c 69 73 74 50 68 72 61 73 65  ts3PoslistPhrase
3533f 4d 65 72 67 65 28 30 2c 20 6e 52 69 67 68 74 2c  Merge(0, nRight,
35340 20 30 2c 20 70 70 31 2c 20 70 70 32 29 20 29 20   0, pp1, pp2) ) 
35341 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 2a 70  return 1;.    *p
35342 70 31 20 3d 20 70 31 3b 0a 20 20 20 20 2a 70 70  p1 = p1;.    *pp
35343 32 20 3d 20 70 32 3b 0a 20 20 20 20 72 65 74 75  2 = p2;.    retu
35344 72 6e 20 66 74 73 33 50 6f 73 6c 69 73 74 50 68  rn fts3PoslistPh
35345 72 61 73 65 4d 65 72 67 65 28 30 2c 20 6e 4c 65  raseMerge(0, nLe
35346 66 74 2c 20 30 2c 20 70 70 32 2c 20 70 70 31 29  ft, 0, pp2, pp1)
35347 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
35348 68 61 72 20 2a 70 54 6d 70 31 20 3d 20 61 54 6d  har *pTmp1 = aTm
35349 70 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 54 6d  p;.    char *pTm
3534a 70 32 3b 0a 20 20 20 20 63 68 61 72 20 2a 61 54  p2;.    char *aT
3534b 6d 70 32 3b 0a 20 20 20 20 69 6e 74 20 72 65 73  mp2;.    int res
3534c 20 3d 20 31 3b 0a 0a 20 20 20 20 66 74 73 33 50   = 1;..    fts3P
3534d 6f 73 6c 69 73 74 50 68 72 61 73 65 4d 65 72 67  oslistPhraseMerg
3534e 65 28 26 70 54 6d 70 31 2c 20 6e 52 69 67 68 74  e(&pTmp1, nRight
3534f 2c 20 30 2c 20 70 70 31 2c 20 70 70 32 29 3b 0a  , 0, pp1, pp2);.
35350 20 20 20 20 61 54 6d 70 32 20 3d 20 70 54 6d 70      aTmp2 = pTmp
35351 32 20 3d 20 70 54 6d 70 31 3b 0a 20 20 20 20 2a  2 = pTmp1;.    *
35352 70 70 31 20 3d 20 70 31 3b 0a 20 20 20 20 2a 70  pp1 = p1;.    *p
35353 70 32 20 3d 20 70 32 3b 0a 20 20 20 20 66 74 73  p2 = p2;.    fts
35354 33 50 6f 73 6c 69 73 74 50 68 72 61 73 65 4d 65  3PoslistPhraseMe
35355 72 67 65 28 26 70 54 6d 70 32 2c 20 6e 4c 65 66  rge(&pTmp2, nLef
35356 74 2c 20 31 2c 20 70 70 32 2c 20 70 70 31 29 3b  t, 1, pp2, pp1);
35357 0a 20 20 20 20 69 66 28 20 70 54 6d 70 31 21 3d  .    if( pTmp1!=
35358 61 54 6d 70 20 26 26 20 70 54 6d 70 32 21 3d 61  aTmp && pTmp2!=a
35359 54 6d 70 32 20 29 7b 0a 20 20 20 20 20 20 66 74  Tmp2 ){.      ft
3535a 73 33 50 6f 73 6c 69 73 74 4d 65 72 67 65 28 70  s3PoslistMerge(p
3535b 70 2c 20 26 61 54 6d 70 2c 20 26 61 54 6d 70 32  p, &aTmp, &aTmp2
3535c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
3535d 20 70 54 6d 70 31 21 3d 61 54 6d 70 20 29 7b 0a   pTmp1!=aTmp ){.
3535e 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73        fts3Poslis
3535f 74 43 6f 70 79 28 70 70 2c 20 26 61 54 6d 70 29  tCopy(pp, &aTmp)
35360 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
35361 70 54 6d 70 32 21 3d 61 54 6d 70 32 20 29 7b 0a  pTmp2!=aTmp2 ){.
35362 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73        fts3Poslis
35363 74 43 6f 70 79 28 70 70 2c 20 26 61 54 6d 70 32  tCopy(pp, &aTmp2
35364 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
35365 20 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20      res = 0;.   
35366 20 7d 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 72   }..    return r
35367 65 73 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  es;.  }.}../*.**
35368 20 56 61 6c 75 65 73 20 74 68 61 74 20 6d 61 79   Values that may
35369 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65 20   be used as the 
3536a 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
3536b 74 6f 20 66 74 73 33 44 6f 63 6c 69 73 74 4d 65  to fts3DoclistMe
3536c 72 67 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  rge()..*/.#defin
3536d 65 20 4d 45 52 47 45 5f 4e 4f 54 20 20 20 20 20  e MERGE_NOT     
3536e 20 20 20 32 20 20 20 20 20 20 20 20 2f 2a 20 44     2        /* D
3536f 20 2b 20 44 20 2d 3e 20 44 20 2a 2f 0a 23 64 65   + D -> D */.#de
35370 66 69 6e 65 20 4d 45 52 47 45 5f 41 4e 44 20 20  fine MERGE_AND  
35371 20 20 20 20 20 20 33 20 20 20 20 20 20 20 20 2f        3        /
35372 2a 20 44 20 2b 20 44 20 2d 3e 20 44 20 2a 2f 0a  * D + D -> D */.
35373 23 64 65 66 69 6e 65 20 4d 45 52 47 45 5f 4f 52  #define MERGE_OR
35374 20 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20           4      
35375 20 20 2f 2a 20 44 20 2b 20 44 20 2d 3e 20 44 20    /* D + D -> D 
35376 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 52 47 45  */.#define MERGE
35377 5f 50 4f 53 5f 4f 52 20 20 20 20 20 35 20 20 20  _POS_OR     5   
35378 20 20 20 20 20 2f 2a 20 50 20 2b 20 50 20 2d 3e       /* P + P ->
35379 20 50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45   P */.#define ME
3537a 52 47 45 5f 50 48 52 41 53 45 20 20 20 20 20 36  RGE_PHRASE     6
3537b 20 20 20 20 20 20 20 20 2f 2a 20 50 20 2b 20 50          /* P + P
3537c 20 2d 3e 20 44 20 2a 2f 0a 23 64 65 66 69 6e 65   -> D */.#define
3537d 20 4d 45 52 47 45 5f 50 4f 53 5f 50 48 52 41 53   MERGE_POS_PHRAS
3537e 45 20 37 20 20 20 20 20 20 20 20 2f 2a 20 50 20  E 7        /* P 
3537f 2b 20 50 20 2d 3e 20 50 20 2a 2f 0a 23 64 65 66  + P -> P */.#def
35380 69 6e 65 20 4d 45 52 47 45 5f 4e 45 41 52 20 20  ine MERGE_NEAR  
35381 20 20 20 20 20 38 20 20 20 20 20 20 20 20 2f 2a       8        /*
35382 20 50 20 2b 20 50 20 2d 3e 20 44 20 2a 2f 0a 23   P + P -> D */.#
35383 64 65 66 69 6e 65 20 4d 45 52 47 45 5f 50 4f 53  define MERGE_POS
35384 5f 4e 45 41 52 20 20 20 39 20 20 20 20 20 20 20  _NEAR   9       
35385 20 2f 2a 20 50 20 2b 20 50 20 2d 3e 20 50 20 2a   /* P + P -> P *
35386 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74  /../*.** Merge t
35387 68 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73 20  he two doclists 
35388 70 61 73 73 65 64 20 69 6e 20 62 75 66 66 65 72  passed in buffer
35389 20 61 31 20 28 73 69 7a 65 20 6e 31 20 62 79 74   a1 (size n1 byt
3538a 65 73 29 20 61 6e 64 20 61 32 0a 2a 2a 20 28 73  es) and a2.** (s
3538b 69 7a 65 20 6e 32 20 62 79 74 65 73 29 2e 20 54  ize n2 bytes). T
3538c 68 65 20 6f 75 74 70 75 74 20 69 73 20 77 72 69  he output is wri
3538d 74 74 65 6e 20 74 6f 20 70 72 65 2d 61 6c 6c 6f  tten to pre-allo
3538e 63 61 74 65 64 20 62 75 66 66 65 72 20 61 42 75  cated buffer aBu
3538f 66 66 65 72 2c 0a 2a 2a 20 77 68 69 63 68 20 69  ffer,.** which i
35390 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  s guaranteed to 
35391 62 65 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20  be large enough 
35392 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75  to hold the resu
35393 6c 74 73 2e 20 54 68 65 20 6e 75 6d 62 65 72 0a  lts. The number.
35394 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 72 69 74  ** of bytes writ
35395 74 65 6e 20 74 6f 20 61 42 75 66 66 65 72 20 69  ten to aBuffer i
35396 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 6e 42  s stored in *pnB
35397 75 66 66 65 72 20 62 65 66 6f 72 65 20 72 65 74  uffer before ret
35398 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  urning..**.** If
35399 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
3539a 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
3539b 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ed. Otherwise, i
3539c 66 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72  f a malloc error
3539d 0a 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 6c 65  .** occurs while
3539e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 74 65   allocating a te
3539f 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 61  mporary buffer a
353a0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 6d 65  s part of the me
353a1 72 67 65 20 6f 70 65 72 61 74 69 6f 6e 2c 0a 2a  rge operation,.*
353a2 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  * SQLITE_NOMEM i
353a3 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  s returned..*/.s
353a4 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 44 6f  tatic int fts3Do
353a5 63 6c 69 73 74 4d 65 72 67 65 28 0a 20 20 69 6e  clistMerge(.  in
353a6 74 20 6d 65 72 67 65 74 79 70 65 2c 20 20 20 20  t mergetype,    
353a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
353a8 20 4f 6e 65 20 6f 66 20 74 68 65 20 4d 45 52 47   One of the MERG
353a9 45 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20  E_XXX constants 
353aa 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 72 61 6d 31  */.  int nParam1
353ab 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
353ac 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20       /* Used by 
353ad 4d 45 52 47 45 5f 4e 45 41 52 20 61 6e 64 20 4d  MERGE_NEAR and M
353ae 45 52 47 45 5f 50 4f 53 5f 4e 45 41 52 20 2a 2f  ERGE_POS_NEAR */
353af 0a 20 20 69 6e 74 20 6e 50 61 72 61 6d 32 2c 20  .  int nParam2, 
353b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
353b1 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 4d 45     /* Used by ME
353b2 52 47 45 5f 4e 45 41 52 20 61 6e 64 20 4d 45 52  RGE_NEAR and MER
353b3 47 45 5f 50 4f 53 5f 4e 45 41 52 20 2a 2f 0a 20  GE_POS_NEAR */. 
353b4 20 63 68 61 72 20 2a 61 42 75 66 66 65 72 2c 20   char *aBuffer, 
353b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
353b6 20 2f 2a 20 50 72 65 2d 61 6c 6c 6f 63 61 74 65   /* Pre-allocate
353b7 64 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  d output buffer 
353b8 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 75 66 66  */.  int *pnBuff
353b9 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
353ba 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 42 79 74       /* OUT: Byt
353bb 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 61 42  es written to aB
353bc 75 66 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  uffer */.  char 
353bd 2a 61 31 2c 20 20 20 20 20 20 20 20 20 20 20 20  *a1,            
353be 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75             /* Bu
353bf 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
353c0 66 69 72 73 74 20 64 6f 63 6c 69 73 74 20 2a 2f  first doclist */
353c1 0a 20 20 69 6e 74 20 6e 31 2c 20 20 20 20 20 20  .  int n1,      
353c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
353c3 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75     /* Size of bu
353c4 66 66 65 72 20 61 31 20 2a 2f 0a 20 20 63 68 61  ffer a1 */.  cha
353c5 72 20 2a 61 32 2c 20 20 20 20 20 20 20 20 20 20  r *a2,          
353c6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
353c7 42 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  Buffer containin
353c8 67 20 73 65 63 6f 6e 64 20 64 6f 63 6c 69 73 74  g second doclist
353c9 20 2a 2f 0a 20 20 69 6e 74 20 6e 32 20 20 20 20   */.  int n2    
353ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
353cb 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
353cc 20 62 75 66 66 65 72 20 61 32 20 2a 2f 0a 29 7b   buffer a2 */.){
353cd 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
353ce 20 69 31 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74   i1 = 0;.  sqlit
353cf 65 33 5f 69 6e 74 36 34 20 69 32 20 3d 20 30 3b  e3_int64 i2 = 0;
353d0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
353d1 20 69 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 63   iPrev = 0;..  c
353d2 68 61 72 20 2a 70 20 3d 20 61 42 75 66 66 65 72  har *p = aBuffer
353d3 3b 0a 20 20 63 68 61 72 20 2a 70 31 20 3d 20 61  ;.  char *p1 = a
353d4 31 3b 0a 20 20 63 68 61 72 20 2a 70 32 20 3d 20  1;.  char *p2 = 
353d5 61 32 3b 0a 20 20 63 68 61 72 20 2a 70 45 6e 64  a2;.  char *pEnd
353d6 31 20 3d 20 26 61 31 5b 6e 31 5d 3b 0a 20 20 63  1 = &a1[n1];.  c
353d7 68 61 72 20 2a 70 45 6e 64 32 20 3d 20 26 61 32  har *pEnd2 = &a2
353d8 5b 6e 32 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  [n2];..  assert(
353d9 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d 45 52 47   mergetype==MERG
353da 45 5f 4f 52 20 20 20 20 20 7c 7c 20 6d 65 72 67  E_OR     || merg
353db 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50 4f 53  etype==MERGE_POS
353dc 5f 4f 52 20 0a 20 20 20 20 20 20 20 7c 7c 20 6d  _OR .       || m
353dd 65 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f  ergetype==MERGE_
353de 41 4e 44 20 20 20 20 7c 7c 20 6d 65 72 67 65 74  AND    || merget
353df 79 70 65 3d 3d 4d 45 52 47 45 5f 4e 4f 54 0a 20  ype==MERGE_NOT. 
353e0 20 20 20 20 20 20 7c 7c 20 6d 65 72 67 65 74 79        || mergety
353e1 70 65 3d 3d 4d 45 52 47 45 5f 50 48 52 41 53 45  pe==MERGE_PHRASE
353e2 20 7c 7c 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d   || mergetype==M
353e3 45 52 47 45 5f 50 4f 53 5f 50 48 52 41 53 45 0a  ERGE_POS_PHRASE.
353e4 20 20 20 20 20 20 20 7c 7c 20 6d 65 72 67 65 74         || merget
353e5 79 70 65 3d 3d 4d 45 52 47 45 5f 4e 45 41 52 20  ype==MERGE_NEAR 
353e6 20 20 7c 7c 20 6d 65 72 67 65 74 79 70 65 3d 3d    || mergetype==
353e7 4d 45 52 47 45 5f 50 4f 53 5f 4e 45 41 52 0a 20  MERGE_POS_NEAR. 
353e8 20 29 3b 0a 0a 20 20 69 66 28 20 21 61 42 75 66   );..  if( !aBuf
353e9 66 65 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72  fer ){.    retur
353ea 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
353eb 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74    }..  /* Read t
353ec 68 65 20 66 69 72 73 74 20 64 6f 63 69 64 20 66  he first docid f
353ed 72 6f 6d 20 65 61 63 68 20 64 6f 63 6c 69 73 74  rom each doclist
353ee 20 2a 2f 0a 20 20 66 74 73 33 47 65 74 44 65 6c   */.  fts3GetDel
353ef 74 61 56 61 72 69 6e 74 32 28 26 70 31 2c 20 70  taVarint2(&p1, p
353f0 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20 20 66 74  End1, &i1);.  ft
353f1 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
353f2 32 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 26 69  2(&p2, pEnd2, &i
353f3 32 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 6d  2);..  switch( m
353f4 65 72 67 65 74 79 70 65 20 29 7b 0a 20 20 20 20  ergetype ){.    
353f5 63 61 73 65 20 4d 45 52 47 45 5f 4f 52 3a 0a 20  case MERGE_OR:. 
353f6 20 20 20 63 61 73 65 20 4d 45 52 47 45 5f 50 4f     case MERGE_PO
353f7 53 5f 4f 52 3a 0a 20 20 20 20 20 20 77 68 69 6c  S_OR:.      whil
353f8 65 28 20 70 31 20 7c 7c 20 70 32 20 29 7b 0a 20  e( p1 || p2 ){. 
353f9 20 20 20 20 20 20 20 69 66 28 20 70 32 20 26 26         if( p2 &&
353fa 20 70 31 20 26 26 20 69 31 3d 3d 69 32 20 29 7b   p1 && i1==i2 ){
353fb 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 50  .          fts3P
353fc 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70  utDeltaVarint(&p
353fd 2c 20 26 69 50 72 65 76 2c 20 69 31 29 3b 0a 20  , &iPrev, i1);. 
353fe 20 20 20 20 20 20 20 20 20 69 66 28 20 6d 65 72           if( mer
353ff 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50 4f  getype==MERGE_PO
35400 53 5f 4f 52 20 29 20 66 74 73 33 50 6f 73 6c 69  S_OR ) fts3Posli
35401 73 74 4d 65 72 67 65 28 26 70 2c 20 26 70 31 2c  stMerge(&p, &p1,
35402 20 26 70 32 29 3b 0a 20 20 20 20 20 20 20 20 20   &p2);.         
35403 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
35404 69 6e 74 32 28 26 70 31 2c 20 70 45 6e 64 31 2c  int2(&p1, pEnd1,
35405 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20 20 20   &i1);.         
35406 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
35407 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64 32 2c  int2(&p2, pEnd2,
35408 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20 20 7d   &i2);.        }
35409 65 6c 73 65 20 69 66 28 20 21 70 32 20 7c 7c 20  else if( !p2 || 
3540a 28 70 31 20 26 26 20 69 31 3c 69 32 29 20 29 7b  (p1 && i1<i2) ){
3540b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 50  .          fts3P
3540c 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70  utDeltaVarint(&p
3540d 2c 20 26 69 50 72 65 76 2c 20 69 31 29 3b 0a 20  , &iPrev, i1);. 
3540e 20 20 20 20 20 20 20 20 20 69 66 28 20 6d 65 72           if( mer
3540f 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50 4f  getype==MERGE_PO
35410 53 5f 4f 52 20 29 20 66 74 73 33 50 6f 73 6c 69  S_OR ) fts3Posli
35411 73 74 43 6f 70 79 28 26 70 2c 20 26 70 31 29 3b  stCopy(&p, &p1);
35412 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47  .          fts3G
35413 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26  etDeltaVarint2(&
35414 70 31 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b  p1, pEnd1, &i1);
35415 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
35416 20 20 20 20 20 20 20 20 20 20 66 74 73 33 50 75            fts3Pu
35417 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70 2c  tDeltaVarint(&p,
35418 20 26 69 50 72 65 76 2c 20 69 32 29 3b 0a 20 20   &iPrev, i2);.  
35419 20 20 20 20 20 20 20 20 69 66 28 20 6d 65 72 67          if( merg
3541a 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50 4f 53  etype==MERGE_POS
3541b 5f 4f 52 20 29 20 66 74 73 33 50 6f 73 6c 69 73  _OR ) fts3Poslis
3541c 74 43 6f 70 79 28 26 70 2c 20 26 70 32 29 3b 0a  tCopy(&p, &p2);.
3541d 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65            fts3Ge
3541e 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70  tDeltaVarint2(&p
3541f 32 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a  2, pEnd2, &i2);.
35420 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35421 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  }.      break;..
35422 20 20 20 20 63 61 73 65 20 4d 45 52 47 45 5f 41      case MERGE_A
35423 4e 44 3a 0a 20 20 20 20 20 20 77 68 69 6c 65 28  ND:.      while(
35424 20 70 31 20 26 26 20 70 32 20 29 7b 0a 20 20 20   p1 && p2 ){.   
35425 20 20 20 20 20 69 66 28 20 69 31 3d 3d 69 32 20       if( i1==i2 
35426 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  ){.          fts
35427 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28  3PutDeltaVarint(
35428 26 70 2c 20 26 69 50 72 65 76 2c 20 69 31 29 3b  &p, &iPrev, i1);
35429 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47  .          fts3G
3542a 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26  etDeltaVarint2(&
3542b 70 31 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b  p1, pEnd1, &i1);
3542c 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47  .          fts3G
3542d 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26  etDeltaVarint2(&
3542e 70 32 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b  p2, pEnd2, &i2);
3542f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
35430 66 28 20 69 31 3c 69 32 20 29 7b 0a 20 20 20 20  f( i1<i2 ){.    
35431 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65 6c        fts3GetDel
35432 74 61 56 61 72 69 6e 74 32 28 26 70 31 2c 20 70  taVarint2(&p1, p
35433 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20 20 20 20  End1, &i1);.    
35434 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
35435 20 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74       fts3GetDelt
35436 61 56 61 72 69 6e 74 32 28 26 70 32 2c 20 70 45  aVarint2(&p2, pE
35437 6e 64 32 2c 20 26 69 32 29 3b 0a 20 20 20 20 20  nd2, &i2);.     
35438 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
35439 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63     break;..    c
3543a 61 73 65 20 4d 45 52 47 45 5f 4e 4f 54 3a 0a 20  ase MERGE_NOT:. 
3543b 20 20 20 20 20 77 68 69 6c 65 28 20 70 31 20 29       while( p1 )
3543c 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 32  {.        if( p2
3543d 20 26 26 20 69 31 3d 3d 69 32 20 29 7b 0a 20 20   && i1==i2 ){.  
3543e 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
3543f 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 31 2c  eltaVarint2(&p1,
35440 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20 20   pEnd1, &i1);.  
35441 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
35442 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 32 2c  eltaVarint2(&p2,
35443 20 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a 20 20   pEnd2, &i2);.  
35444 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
35445 21 70 32 20 7c 7c 20 69 31 3c 69 32 20 29 7b 0a  !p2 || i1<i2 ){.
35446 20 20 20 20 20 20 20 20 20 20 66 74 73 33 50 75            fts3Pu
35447 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70 2c  tDeltaVarint(&p,
35448 20 26 69 50 72 65 76 2c 20 69 31 29 3b 0a 20 20   &iPrev, i1);.  
35449 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
3544a 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 31 2c  eltaVarint2(&p1,
3544b 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20 20   pEnd1, &i1);.  
3544c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3544d 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65         fts3GetDe
3544e 6c 74 61 56 61 72 69 6e 74 32 28 26 70 32 2c 20  ltaVarint2(&p2, 
3544f 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a 20 20 20  pEnd2, &i2);.   
35450 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
35451 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
35452 20 63 61 73 65 20 4d 45 52 47 45 5f 50 4f 53 5f   case MERGE_POS_
35453 50 48 52 41 53 45 3a 0a 20 20 20 20 63 61 73 65  PHRASE:.    case
35454 20 4d 45 52 47 45 5f 50 48 52 41 53 45 3a 20 7b   MERGE_PHRASE: {
35455 0a 20 20 20 20 20 20 63 68 61 72 20 2a 2a 70 70  .      char **pp
35456 50 6f 73 20 3d 20 28 6d 65 72 67 65 74 79 70 65  Pos = (mergetype
35457 3d 3d 4d 45 52 47 45 5f 50 48 52 41 53 45 20 3f  ==MERGE_PHRASE ?
35458 20 30 20 3a 20 26 70 29 3b 0a 20 20 20 20 20 20   0 : &p);.      
35459 77 68 69 6c 65 28 20 70 31 20 26 26 20 70 32 20  while( p1 && p2 
3545a 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  ){.        if( i
3545b 31 3d 3d 69 32 20 29 7b 0a 20 20 20 20 20 20 20  1==i2 ){.       
3545c 20 20 20 63 68 61 72 20 2a 70 53 61 76 65 20 3d     char *pSave =
3545d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71   p;.          sq
3545e 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72 65  lite3_int64 iPre
3545f 76 53 61 76 65 20 3d 20 69 50 72 65 76 3b 0a 20  vSave = iPrev;. 
35460 20 20 20 20 20 20 20 20 20 66 74 73 33 50 75 74           fts3Put
35461 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70 2c 20  DeltaVarint(&p, 
35462 26 69 50 72 65 76 2c 20 69 31 29 3b 0a 20 20 20  &iPrev, i1);.   
35463 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 66 74         if( 0==ft
35464 73 33 50 6f 73 6c 69 73 74 50 68 72 61 73 65 4d  s3PoslistPhraseM
35465 65 72 67 65 28 70 70 50 6f 73 2c 20 31 2c 20 30  erge(ppPos, 1, 0
35466 2c 20 26 70 31 2c 20 26 70 32 29 20 29 7b 0a 20  , &p1, &p2) ){. 
35467 20 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 70             p = p
35468 53 61 76 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Save;.          
35469 20 20 69 50 72 65 76 20 3d 20 69 50 72 65 76 53    iPrev = iPrevS
3546a 61 76 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ave;.          }
3546b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47  .          fts3G
3546c 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26  etDeltaVarint2(&
3546d 70 31 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b  p1, pEnd1, &i1);
3546e 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47  .          fts3G
3546f 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26  etDeltaVarint2(&
35470 70 32 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b  p2, pEnd2, &i2);
35471 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
35472 66 28 20 69 31 3c 69 32 20 29 7b 0a 20 20 20 20  f( i1<i2 ){.    
35473 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73        fts3Poslis
35474 74 43 6f 70 79 28 30 2c 20 26 70 31 29 3b 0a 20  tCopy(0, &p1);. 
35475 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74           fts3Get
35476 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 31  DeltaVarint2(&p1
35477 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20  , pEnd1, &i1);. 
35478 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
35479 20 20 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c          fts3Posl
3547a 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32 29 3b  istCopy(0, &p2);
3547b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47  .          fts3G
3547c 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26  etDeltaVarint2(&
3547d 70 32 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b  p2, pEnd2, &i2);
3547e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3547f 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
35480 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20      }..    case 
35481 4d 45 52 47 45 5f 50 4f 53 5f 4e 45 41 52 3a 0a  MERGE_POS_NEAR:.
35482 20 20 20 20 63 61 73 65 20 4d 45 52 47 45 5f 4e      case MERGE_N
35483 45 41 52 3a 20 7b 0a 20 20 20 20 20 20 63 68 61  EAR: {.      cha
35484 72 20 2a 61 54 6d 70 20 3d 20 30 3b 0a 20 20 20  r *aTmp = 0;.   
35485 20 20 20 63 68 61 72 20 2a 2a 70 70 50 6f 73 20     char **ppPos 
35486 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6d  = 0;.      if( m
35487 65 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f  ergetype==MERGE_
35488 50 4f 53 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20  POS_NEAR ){.    
35489 20 20 20 20 70 70 50 6f 73 20 3d 20 26 70 3b 0a      ppPos = &p;.
3548a 20 20 20 20 20 20 20 20 61 54 6d 70 20 3d 20 73          aTmp = s
3548b 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 32 2a  qlite3_malloc(2*
3548c 28 6e 31 2b 6e 32 29 29 3b 0a 20 20 20 20 20 20  (n1+n2));.      
3548d 20 20 69 66 28 20 21 61 54 6d 70 20 29 7b 0a 20    if( !aTmp ){. 
3548e 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
3548f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
35490 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
35491 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 31  .      while( p1
35492 20 26 26 20 70 32 20 29 7b 0a 20 20 20 20 20 20   && p2 ){.      
35493 20 20 69 66 28 20 69 31 3d 3d 69 32 20 29 7b 0a    if( i1==i2 ){.
35494 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
35495 70 53 61 76 65 20 3d 20 70 3b 0a 20 20 20 20 20  pSave = p;.     
35496 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
35497 36 34 20 69 50 72 65 76 53 61 76 65 20 3d 20 69  64 iPrevSave = i
35498 50 72 65 76 3b 0a 20 20 20 20 20 20 20 20 20 20  Prev;.          
35499 66 74 73 33 50 75 74 44 65 6c 74 61 56 61 72 69  fts3PutDeltaVari
3549a 6e 74 28 26 70 2c 20 26 69 50 72 65 76 2c 20 69  nt(&p, &iPrev, i
3549b 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69  1);..          i
3549c 66 28 20 21 66 74 73 33 50 6f 73 6c 69 73 74 4e  f( !fts3PoslistN
3549d 65 61 72 4d 65 72 67 65 28 70 70 50 6f 73 2c 20  earMerge(ppPos, 
3549e 61 54 6d 70 2c 20 6e 50 61 72 61 6d 31 2c 20 6e  aTmp, nParam1, n
3549f 50 61 72 61 6d 32 2c 20 26 70 31 2c 20 26 70 32  Param2, &p1, &p2
354a0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
354a1 20 69 50 72 65 76 20 3d 20 69 50 72 65 76 53 61   iPrev = iPrevSa
354a2 76 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ve;.            
354a3 70 20 3d 20 70 53 61 76 65 3b 0a 20 20 20 20 20  p = pSave;.     
354a4 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
354a5 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
354a6 72 69 6e 74 32 28 26 70 31 2c 20 70 45 6e 64 31  rint2(&p1, pEnd1
354a7 2c 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20 20  , &i1);.        
354a8 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
354a9 72 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64 32  rint2(&p2, pEnd2
354aa 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20 20  , &i2);.        
354ab 7d 65 6c 73 65 20 69 66 28 20 69 31 3c 69 32 20  }else if( i1<i2 
354ac 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  ){.          fts
354ad 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c 20  3PoslistCopy(0, 
354ae 26 70 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  &p1);.          
354af 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
354b0 6e 74 32 28 26 70 31 2c 20 70 45 6e 64 31 2c 20  nt2(&p1, pEnd1, 
354b1 26 69 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  &i1);.        }e
354b2 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66  lse{.          f
354b3 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30  ts3PoslistCopy(0
354b4 2c 20 26 70 32 29 3b 0a 20 20 20 20 20 20 20 20  , &p2);.        
354b5 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
354b6 72 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64 32  rint2(&p2, pEnd2
354b7 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20 20  , &i2);.        
354b8 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
354b9 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 54 6d  sqlite3_free(aTm
354ba 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  p);.      break;
354bb 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 65 66 61  .    }..    defa
354bc 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72  ult:.      asser
354bd 74 28 21 22 49 6e 76 61 6c 69 64 20 6d 65 72 67  t(!"Invalid merg
354be 65 74 79 70 65 20 76 61 6c 75 65 20 70 61 73 73  etype value pass
354bf 65 64 20 74 6f 20 66 74 73 33 44 6f 63 6c 69 73  ed to fts3Doclis
354c0 74 4d 65 72 67 65 28 29 22 29 3b 0a 20 20 7d 0a  tMerge()");.  }.
354c1 0a 20 20 2a 70 6e 42 75 66 66 65 72 20 3d 20 28  .  *pnBuffer = (
354c2 70 2d 61 42 75 66 66 65 72 29 3b 0a 20 20 72 65  p-aBuffer);.  re
354c3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
354c4 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 20 70 6f 69 6e  }../* .** A poin
354c5 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
354c6 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
354c7 74 75 72 65 20 69 73 20 75 73 65 64 20 61 73 20  ture is used as 
354c8 74 68 65 20 63 6f 6e 74 65 78 74 20 0a 2a 2a 20  the context .** 
354c9 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
354ca 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
354cb 49 74 65 72 61 74 65 28 29 0a 2a 2f 0a 74 79 70  Iterate().*/.typ
354cc 65 64 65 66 20 73 74 72 75 63 74 20 54 65 72 6d  edef struct Term
354cd 53 65 6c 65 63 74 20 54 65 72 6d 53 65 6c 65 63  Select TermSelec
354ce 74 3b 0a 73 74 72 75 63 74 20 54 65 72 6d 53 65  t;.struct TermSe
354cf 6c 65 63 74 20 7b 0a 20 20 69 6e 74 20 69 73 52  lect {.  int isR
354d0 65 71 50 6f 73 3b 0a 20 20 63 68 61 72 20 2a 61  eqPos;.  char *a
354d1 4f 75 74 70 75 74 3b 20 20 20 20 20 20 20 20 20  Output;         
354d2 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c           /* Mall
354d3 6f 63 27 64 20 6f 75 74 70 75 74 20 62 75 66 66  oc'd output buff
354d4 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74  er */.  int nOut
354d5 70 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  put;            
354d6 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
354d7 6f 66 20 6f 75 74 70 75 74 20 69 6e 20 62 79 74  of output in byt
354d8 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  es */.};../*.** 
354d9 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
354da 20 75 73 65 64 20 61 73 20 74 68 65 20 73 71 6c   used as the sql
354db 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
354dc 72 49 74 65 72 61 74 65 28 29 20 63 61 6c 6c 62  rIterate() callb
354dd 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 71 75 65 72  ack when.** quer
354de 79 69 6e 67 20 74 68 65 20 66 75 6c 6c 2d 74 65  ying the full-te
354df 78 74 20 69 6e 64 65 78 20 66 6f 72 20 61 20 64  xt index for a d
354e0 6f 63 6c 69 73 74 20 61 73 73 6f 63 69 61 74 65  oclist associate
354e1 64 20 77 69 74 68 20 61 20 74 65 72 6d 20 6f 72  d with a term or
354e2 0a 2a 2a 20 74 65 72 6d 2d 70 72 65 66 69 78 2e  .** term-prefix.
354e3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
354e4 74 73 33 54 65 72 6d 53 65 6c 65 63 74 43 62 28  ts3TermSelectCb(
354e5 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
354e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
354e7 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
354e8 62 6c 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ble object */.  
354e9 76 6f 69 64 20 2a 70 43 6f 6e 74 65 78 74 2c 20  void *pContext, 
354ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
354eb 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 54 65  /* Pointer to Te
354ec 72 6d 53 65 6c 65 63 74 20 73 74 72 75 63 74 75  rmSelect structu
354ed 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54  re */.  char *zT
354ee 65 72 6d 2c 0a 20 20 69 6e 74 20 6e 54 65 72 6d  erm,.  int nTerm
354ef 2c 0a 20 20 63 68 61 72 20 2a 61 44 6f 63 6c 69  ,.  char *aDocli
354f0 73 74 2c 0a 20 20 69 6e 74 20 6e 44 6f 63 6c 69  st,.  int nDocli
354f1 73 74 0a 29 7b 0a 20 20 54 65 72 6d 53 65 6c 65  st.){.  TermSele
354f2 63 74 20 2a 70 54 53 20 3d 20 28 54 65 72 6d 53  ct *pTS = (TermS
354f3 65 6c 65 63 74 20 2a 29 70 43 6f 6e 74 65 78 74  elect *)pContext
354f4 3b 0a 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 70  ;.  int nNew = p
354f5 54 53 2d 3e 6e 4f 75 74 70 75 74 20 2b 20 6e 44  TS->nOutput + nD
354f6 6f 63 6c 69 73 74 3b 0a 20 20 63 68 61 72 20 2a  oclist;.  char *
354f7 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d  aNew = sqlite3_m
354f8 61 6c 6c 6f 63 28 6e 4e 65 77 29 3b 0a 0a 20 20  alloc(nNew);..  
354f9 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 20 20  if( !aNew ){.   
354fa 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
354fb 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  OMEM;.  }..  if(
354fc 20 70 54 53 2d 3e 6e 4f 75 74 70 75 74 3d 3d 30   pTS->nOutput==0
354fd 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
354fe 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
354ff 74 65 72 6d 20 73 65 6c 65 63 74 65 64 2c 20 63  term selected, c
35500 6f 70 79 20 74 68 65 20 64 6f 63 6c 69 73 74 20  opy the doclist 
35501 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20  to the output.  
35502 20 20 2a 2a 20 62 75 66 66 65 72 20 75 73 69 6e    ** buffer usin
35503 67 20 6d 65 6d 63 70 79 28 29 2e 20 54 4f 44 4f  g memcpy(). TODO
35504 3a 20 41 64 64 20 61 20 77 61 79 20 74 6f 20 74  : Add a way to t
35505 72 61 6e 73 66 65 72 20 63 6f 6e 74 72 6f 6c 20  ransfer control 
35506 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 44  of the.    ** aD
35507 6f 63 6c 69 73 74 20 62 75 66 66 65 72 20 66 72  oclist buffer fr
35508 6f 6d 20 74 68 65 20 63 61 6c 6c 65 72 20 73 6f  om the caller so
35509 20 61 73 20 74 6f 20 61 76 6f 69 64 20 74 68 65   as to avoid the
3550a 20 6d 65 6d 63 70 79 28 29 2e 0a 20 20 20 20 2a   memcpy()..    *
3550b 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 4e 65  /.    memcpy(aNe
3550c 77 2c 20 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f  w, aDoclist, nDo
3550d 63 6c 69 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  clist);.  }else{
3550e 0a 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74 70  .    /* The outp
3550f 75 74 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74  ut buffer is not
35510 20 65 6d 70 74 79 2e 20 4d 65 72 67 65 20 64 6f   empty. Merge do
35511 63 6c 69 73 74 20 61 44 6f 63 6c 69 73 74 20 77  clist aDoclist w
35512 69 74 68 20 74 68 65 0a 20 20 20 20 2a 2a 20 65  ith the.    ** e
35513 78 69 73 74 69 6e 67 20 6f 75 74 70 75 74 2e 20  xisting output. 
35514 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61  This can only ha
35515 70 70 65 6e 20 77 69 74 68 20 70 72 65 66 69 78  ppen with prefix
35516 2d 73 65 61 72 63 68 65 73 20 28 61 73 0a 20 20  -searches (as.  
35517 20 20 2a 2a 20 73 65 61 72 63 68 65 73 20 66 6f    ** searches fo
35518 72 20 65 78 61 63 74 20 74 65 72 6d 73 20 72 65  r exact terms re
35519 74 75 72 6e 20 65 78 61 63 74 6c 79 20 6f 6e 65  turn exactly one
3551a 20 64 6f 63 6c 69 73 74 29 2e 0a 20 20 20 20 2a   doclist)..    *
3551b 2f 0a 20 20 20 20 69 6e 74 20 6d 65 72 67 65 74  /.    int merget
3551c 79 70 65 20 3d 20 28 70 54 53 2d 3e 69 73 52 65  ype = (pTS->isRe
3551d 71 50 6f 73 20 3f 20 4d 45 52 47 45 5f 50 4f 53  qPos ? MERGE_POS
3551e 5f 4f 52 20 3a 20 4d 45 52 47 45 5f 4f 52 29 3b  _OR : MERGE_OR);
3551f 0a 20 20 20 20 66 74 73 33 44 6f 63 6c 69 73 74  .    fts3Doclist
35520 4d 65 72 67 65 28 6d 65 72 67 65 74 79 70 65 2c  Merge(mergetype,
35521 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 61   0, 0,.        a
35522 4e 65 77 2c 20 26 6e 4e 65 77 2c 20 70 54 53 2d  New, &nNew, pTS-
35523 3e 61 4f 75 74 70 75 74 2c 20 70 54 53 2d 3e 6e  >aOutput, pTS->n
35524 4f 75 74 70 75 74 2c 20 61 44 6f 63 6c 69 73 74  Output, aDoclist
35525 2c 20 6e 44 6f 63 6c 69 73 74 0a 20 20 20 20 29  , nDoclist.    )
35526 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
35527 5f 66 72 65 65 28 70 54 53 2d 3e 61 4f 75 74 70  _free(pTS->aOutp
35528 75 74 29 3b 0a 20 20 70 54 53 2d 3e 61 4f 75 74  ut);.  pTS->aOut
35529 70 75 74 20 3d 20 61 4e 65 77 3b 0a 20 20 70 54  put = aNew;.  pT
3552a 53 2d 3e 6e 4f 75 74 70 75 74 20 3d 20 6e 4e 65  S->nOutput = nNe
3552b 77 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  w;..  return SQL
3552c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3552d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
3552e 65 74 72 65 69 76 65 73 20 74 68 65 20 64 6f 63  etreives the doc
3552f 6c 69 73 74 20 66 6f 72 20 74 68 65 20 73 70 65  list for the spe
35530 63 69 66 69 65 64 20 74 65 72 6d 20 28 6f 72 20  cified term (or 
35531 74 65 72 6d 0a 2a 2a 20 70 72 65 66 69 78 29 20  term.** prefix) 
35532 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
35533 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  e. .**.** The re
35534 74 75 72 6e 65 64 20 64 6f 63 6c 69 73 74 20 6d  turned doclist m
35535 61 79 20 62 65 20 69 6e 20 6f 6e 65 20 6f 66 20  ay be in one of 
35536 74 77 6f 20 66 6f 72 6d 61 74 73 2c 20 64 65 70  two formats, dep
35537 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 0a 2a  ending on the .*
35538 2a 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61 6d  * value of param
35539 65 74 65 72 20 69 73 52 65 71 50 6f 73 2e 20 49  eter isReqPos. I
3553a 66 20 69 73 52 65 71 50 6f 73 20 69 73 20 7a 65  f isReqPos is ze
3553b 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 64 6f 63  ro, then the doc
3553c 6c 69 73 74 20 69 73 0a 2a 2a 20 61 20 73 6f 72  list is.** a sor
3553d 74 65 64 20 6c 69 73 74 20 6f 66 20 64 65 6c 74  ted list of delt
3553e 61 2d 63 6f 6d 70 72 65 73 73 65 64 20 64 6f 63  a-compressed doc
3553f 69 64 73 2e 20 49 66 20 69 73 52 65 71 50 6f 73  ids. If isReqPos
35540 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 0a 2a   is non-zero, .*
35541 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72  * then the retur
35542 6e 65 64 20 6c 69 73 74 20 69 73 20 69 6e 20 74  ned list is in t
35543 68 65 20 73 61 6d 65 20 66 6f 72 6d 61 74 20 61  he same format a
35544 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  s is stored in t
35545 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77  he.** database w
35546 69 74 68 6f 75 74 20 74 68 65 20 66 6f 75 6e 64  ithout the found
35547 20 6c 65 6e 67 74 68 20 73 70 65 63 69 66 69 65   length specifie
35548 72 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  r at the start o
35549 66 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 64 6f 63  f on-disk.** doc
3554a 6c 69 73 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  lists..*/.static
3554b 20 69 6e 74 20 66 74 73 33 54 65 72 6d 53 65 6c   int fts3TermSel
3554c 65 63 74 28 0a 20 20 46 74 73 33 54 61 62 6c 65  ect(.  Fts3Table
3554d 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
3554e 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
3554f 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
35550 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c  /.  int iColumn,
35551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35552 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 6f      /* Column to
35553 20 71 75 65 72 79 20 28 6f 72 20 2d 76 65 20 66   query (or -ve f
35554 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 29 20  or all columns) 
35555 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
35556 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20  *zTerm,         
35557 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20       /* Term to 
35558 71 75 65 72 79 20 66 6f 72 20 2a 2f 0a 20 20 69  query for */.  i
35559 6e 74 20 6e 54 65 72 6d 2c 20 20 20 20 20 20 20  nt nTerm,       
3555a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3555b 2a 20 53 69 7a 65 20 6f 66 20 7a 54 65 72 6d 20  * Size of zTerm 
3555c 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e  in bytes */.  in
3555d 74 20 69 73 50 72 65 66 69 78 2c 20 20 20 20 20  t isPrefix,     
3555e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3555f 20 54 72 75 65 20 66 6f 72 20 61 20 70 72 65 66   True for a pref
35560 69 78 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 69  ix search */.  i
35561 6e 74 20 69 73 52 65 71 50 6f 73 2c 20 20 20 20  nt isReqPos,    
35562 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35563 2a 20 54 72 75 65 20 74 6f 20 69 6e 63 6c 75 64  * True to includ
35564 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73  e position lists
35565 20 69 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20   in output */.  
35566 69 6e 74 20 2a 70 6e 4f 75 74 2c 20 20 20 20 20  int *pnOut,     
35567 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35568 2f 2a 20 4f 55 54 3a 20 53 69 7a 65 20 6f 66 20  /* OUT: Size of 
35569 62 75 66 66 65 72 20 61 74 20 2a 70 70 4f 75 74  buffer at *ppOut
3556a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70 4f   */.  char **ppO
3556b 75 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ut              
3556c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61        /* OUT: Ma
3556d 6c 6c 6f 63 65 64 20 72 65 73 75 6c 74 20 62 75  lloced result bu
3556e 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ffer */.){.  int
3556f 20 69 3b 0a 20 20 54 65 72 6d 53 65 6c 65 63 74   i;.  TermSelect
35570 20 74 73 63 3b 0a 20 20 46 74 73 33 53 65 67 46   tsc;.  Fts3SegF
35571 69 6c 74 65 72 20 66 69 6c 74 65 72 3b 20 20 20  ilter filter;   
35572 20 20 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65          /* Segme
35573 6e 74 20 74 65 72 6d 20 66 69 6c 74 65 72 20 63  nt term filter c
35574 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 2a 2f 0a  onfiguration */.
35575 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
35576 2a 2a 61 70 53 65 67 6d 65 6e 74 20 3d 20 30 3b  **apSegment = 0;
35577 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 65    /* Array of se
35578 67 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 20 64  gments to read d
35579 61 74 61 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e  ata from */.  in
3557a 74 20 6e 53 65 67 6d 65 6e 74 20 3d 20 30 3b 20  t nSegment = 0; 
3557b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3557c 20 53 69 7a 65 20 6f 66 20 61 70 53 65 67 6d 65   Size of apSegme
3557d 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e  nt array */.  in
3557e 74 20 6e 41 6c 6c 6f 63 20 3d 20 30 3b 20 20 20  t nAlloc = 0;   
3557f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35580 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   Allocated size 
35581 6f 66 20 73 65 67 6d 65 6e 74 20 61 72 72 61 79  of segment array
35582 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
35583 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35584 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
35585 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  code */.  sqlite
35586 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20  3_stmt *pStmt;  
35587 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c            /* SQL
35588 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 73 63   statement to sc
35589 61 6e 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c  an %_segdir tabl
3558a 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 65 20  e */.  int iAge 
3558b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
3558c 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
3558d 6f 20 61 73 73 69 67 6e 20 61 67 65 73 20 74 6f  o assign ages to
3558e 20 73 65 67 6d 65 6e 74 73 20 2a 2f 0a 0a 20 20   segments */..  
3558f 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
35590 74 68 65 20 65 6e 74 69 72 65 20 25 5f 73 65 67  the entire %_seg
35591 64 69 72 20 74 61 62 6c 65 2e 20 46 6f 72 20 65  dir table. For e
35592 61 63 68 20 73 65 67 6d 65 6e 74 2c 20 63 72 65  ach segment, cre
35593 61 74 65 20 61 0a 20 20 2a 2a 20 46 74 73 33 53  ate a.  ** Fts3S
35594 65 67 52 65 61 64 65 72 20 74 6f 20 69 74 65 72  egReader to iter
35595 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 20  ate through the 
35596 73 75 62 73 65 74 20 6f 66 20 74 68 65 20 73 65  subset of the se
35597 67 6d 65 6e 74 20 6c 65 61 76 65 73 0a 20 20 2a  gment leaves.  *
35598 2a 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61  * that may conta
35599 69 6e 20 61 20 74 65 72 6d 20 74 68 61 74 20 6d  in a term that m
3559a 61 74 63 68 65 73 20 7a 54 65 72 6d 2f 6e 54 65  atches zTerm/nTe
3559b 72 6d 2e 20 46 6f 72 20 6e 6f 6e 2d 70 72 65 66  rm. For non-pref
3559c 69 78 0a 20 20 2a 2a 20 73 65 61 72 63 68 65 73  ix.  ** searches
3559d 2c 20 74 68 69 73 20 69 73 20 61 6c 77 61 79 73  , this is always
3559e 20 61 20 73 69 6e 67 6c 65 20 6c 65 61 66 2e 20   a single leaf. 
3559f 46 6f 72 20 70 72 65 66 69 78 20 73 65 61 72 63  For prefix searc
355a0 68 65 73 2c 20 74 68 69 73 0a 20 20 2a 2a 20 6d  hes, this.  ** m
355a1 61 79 20 62 65 20 61 20 63 6f 6e 74 69 67 75 6f  ay be a contiguo
355a2 75 73 20 62 6c 6f 63 6b 20 6f 66 20 6c 65 61 76  us block of leav
355a3 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  es..  **.  ** Th
355a4 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 6c  e code in this l
355a5 6f 6f 70 20 64 6f 65 73 20 6e 6f 74 20 61 63 74  oop does not act
355a6 75 61 6c 6c 79 20 6c 6f 61 64 20 61 6e 79 20 6c  ually load any l
355a7 65 61 76 65 73 20 69 6e 74 6f 20 6d 65 6d 6f 72  eaves into memor
355a8 79 0a 20 20 2a 2a 20 28 75 6e 6c 65 73 73 20 74  y.  ** (unless t
355a9 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 68 61 70  he root node hap
355aa 70 65 6e 73 20 74 6f 20 62 65 20 61 20 6c 65 61  pens to be a lea
355ab 66 29 2e 20 49 74 20 73 69 6d 70 6c 79 20 65 78  f). It simply ex
355ac 61 6d 69 6e 65 73 20 74 68 65 0a 20 20 2a 2a 20  amines the.  ** 
355ad 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65  b-tree structure
355ae 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68   to determine wh
355af 69 63 68 20 6c 65 61 76 65 73 20 6e 65 65 64 20  ich leaves need 
355b0 74 6f 20 62 65 20 69 6e 73 70 65 63 74 65 64 2e  to be inspected.
355b1 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
355b2 69 74 65 33 46 74 73 33 41 6c 6c 53 65 67 64 69  ite3Fts3AllSegdi
355b3 72 73 28 70 2c 20 26 70 53 74 6d 74 29 3b 0a 20  rs(p, &pStmt);. 
355b4 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49   while( rc==SQLI
355b5 54 45 5f 4f 4b 20 26 26 20 53 51 4c 49 54 45 5f  TE_OK && SQLITE_
355b6 52 4f 57 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  ROW==(rc = sqlit
355b7 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 29 20  e3_step(pStmt)) 
355b8 29 7b 0a 20 20 20 20 46 74 73 33 53 65 67 52 65  ){.    Fts3SegRe
355b9 61 64 65 72 20 2a 70 4e 65 77 20 3d 20 30 3b 0a  ader *pNew = 0;.
355ba 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 20 3d 20      int nRoot = 
355bb 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
355bc 79 74 65 73 28 70 53 74 6d 74 2c 20 34 29 3b 0a  ytes(pStmt, 4);.
355bd 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
355be 7a 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f  zRoot = sqlite3_
355bf 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d  column_blob(pStm
355c0 74 2c 20 34 29 3b 0a 20 20 20 20 69 66 28 20 73  t, 4);.    if( s
355c1 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
355c2 74 36 34 28 70 53 74 6d 74 2c 20 31 29 3d 3d 30  t64(pStmt, 1)==0
355c3 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   ){.      /* The
355c4 20 65 6e 74 69 72 65 20 73 65 67 6d 65 6e 74 20   entire segment 
355c5 69 73 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65  is stored on the
355c6 20 72 6f 6f 74 20 6e 6f 64 65 20 28 77 68 69 63   root node (whic
355c7 68 20 6d 75 73 74 20 62 65 20 61 0a 20 20 20 20  h must be a.    
355c8 20 20 2a 2a 20 6c 65 61 66 29 2e 20 44 6f 20 6e    ** leaf). Do n
355c9 6f 74 20 62 6f 74 68 65 72 20 69 6e 73 70 65 63  ot bother inspec
355ca 74 69 6e 67 20 61 6e 79 20 64 61 74 61 20 69 6e  ting any data in
355cb 20 74 68 69 73 20 63 61 73 65 2c 20 6a 75 73 74   this case, just
355cc 0a 20 20 20 20 20 20 2a 2a 20 63 72 65 61 74 65  .      ** create
355cd 20 61 20 46 74 73 33 53 65 67 52 65 61 64 65 72   a Fts3SegReader
355ce 20 74 6f 20 73 63 61 6e 20 74 68 65 20 73 69 6e   to scan the sin
355cf 67 6c 65 20 6c 65 61 66 2e 20 0a 20 20 20 20 20  gle leaf. .     
355d0 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73   */.      rc = s
355d1 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
355d2 64 65 72 4e 65 77 28 70 2c 20 69 41 67 65 2c 20  derNew(p, iAge, 
355d3 30 2c 20 30 2c 20 30 2c 20 7a 52 6f 6f 74 2c 20  0, 0, 0, zRoot, 
355d4 6e 52 6f 6f 74 2c 20 26 70 4e 65 77 29 3b 0a 20  nRoot, &pNew);. 
355d5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
355d6 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 31  sqlite3_int64 i1
355d7 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  ;.      rc = fts
355d8 33 53 65 6c 65 63 74 4c 65 61 66 28 70 2c 20 7a  3SelectLeaf(p, z
355d9 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 7a 52 6f  Term, nTerm, zRo
355da 6f 74 2c 20 6e 52 6f 6f 74 2c 20 26 69 31 29 3b  ot, nRoot, &i1);
355db 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
355dc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
355dd 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
355de 34 20 69 32 20 3d 20 73 71 6c 69 74 65 33 5f 63  4 i2 = sqlite3_c
355df 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74 6d  olumn_int64(pStm
355e0 74 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 72  t, 2);.        r
355e1 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53  c = sqlite3Fts3S
355e2 65 67 52 65 61 64 65 72 4e 65 77 28 70 2c 20 69  egReaderNew(p, i
355e3 41 67 65 2c 20 69 31 2c 20 69 32 2c 20 30 2c 20  Age, i1, i2, 0, 
355e4 30 2c 20 30 2c 20 26 70 4e 65 77 29 3b 0a 20 20  0, 0, &pNew);.  
355e5 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
355e6 69 41 67 65 2b 2b 3b 0a 0a 20 20 20 20 2f 2a 20  iAge++;..    /* 
355e7 49 66 20 61 20 6e 65 77 20 46 74 73 33 53 65 67  If a new Fts3Seg
355e8 52 65 61 64 65 72 20 77 61 73 20 61 6c 6c 6f 63  Reader was alloc
355e9 61 74 65 64 2c 20 61 64 64 20 69 74 20 74 6f 20  ated, add it to 
355ea 74 68 65 20 61 70 53 65 67 6d 65 6e 74 20 61 72  the apSegment ar
355eb 72 61 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65  ray. */.    asse
355ec 72 74 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  rt( (rc==SQLITE_
355ed 4f 4b 29 3d 3d 28 70 4e 65 77 21 3d 30 29 20 29  OK)==(pNew!=0) )
355ee 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 20 29  ;.    if( pNew )
355ef 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 53 65 67  {.      if( nSeg
355f0 6d 65 6e 74 3d 3d 6e 41 6c 6c 6f 63 20 29 7b 0a  ment==nAlloc ){.
355f1 20 20 20 20 20 20 20 20 6e 41 6c 6c 6f 63 20 2b          nAlloc +
355f2 3d 20 31 36 3b 0a 20 20 20 20 20 20 20 20 46 74  = 16;.        Ft
355f3 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 70 41  s3SegReader **pA
355f4 72 72 61 79 20 3d 20 28 46 74 73 33 53 65 67 52  rray = (Fts3SegR
355f5 65 61 64 65 72 20 2a 2a 29 73 71 6c 69 74 65 33  eader **)sqlite3
355f6 5f 72 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20  _realloc(.      
355f7 20 20 20 20 20 20 61 70 53 65 67 6d 65 6e 74 2c        apSegment,
355f8 20 6e 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 46   nAlloc*sizeof(F
355f9 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 29 0a  ts3SegReader *).
355fa 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
355fb 20 20 20 69 66 28 20 21 70 41 72 72 61 79 20 29     if( !pArray )
355fc 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
355fd 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
355fe 46 72 65 65 28 70 2c 20 70 4e 65 77 29 3b 0a 20  Free(p, pNew);. 
355ff 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
35600 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
35601 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73        goto finis
35602 68 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  hed;.        }. 
35603 20 20 20 20 20 20 20 61 70 53 65 67 6d 65 6e 74         apSegment
35604 20 3d 20 70 41 72 72 61 79 3b 0a 20 20 20 20 20   = pArray;.     
35605 20 7d 0a 20 20 20 20 20 20 61 70 53 65 67 6d 65   }.      apSegme
35606 6e 74 5b 6e 53 65 67 6d 65 6e 74 2b 2b 5d 20 3d  nt[nSegment++] =
35607 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 20 20 7d   pNew;.    }.  }
35608 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35609 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73  E_DONE ){.    as
3560a 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
3560b 5f 4f 4b 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20  _OK );.    goto 
3560c 66 69 6e 69 73 68 65 64 3b 0a 20 20 7d 0a 0a 20  finished;.  }.. 
3560d 20 6d 65 6d 73 65 74 28 26 74 73 63 2c 20 30 2c   memset(&tsc, 0,
3560e 20 73 69 7a 65 6f 66 28 54 65 72 6d 53 65 6c 65   sizeof(TermSele
3560f 63 74 29 29 3b 0a 20 20 74 73 63 2e 69 73 52 65  ct));.  tsc.isRe
35610 71 50 6f 73 20 3d 20 69 73 52 65 71 50 6f 73 3b  qPos = isReqPos;
35611 0a 0a 20 20 66 69 6c 74 65 72 2e 66 6c 61 67 73  ..  filter.flags
35612 20 3d 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f   = FTS3_SEGMENT_
35613 49 47 4e 4f 52 45 5f 45 4d 50 54 59 20 0a 20 20  IGNORE_EMPTY .  
35614 20 20 20 20 20 20 7c 20 28 69 73 50 72 65 66 69        | (isPrefi
35615 78 20 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  x ? FTS3_SEGMENT
35616 5f 50 52 45 46 49 58 20 3a 20 30 29 0a 20 20 20  _PREFIX : 0).   
35617 20 20 20 20 20 7c 20 28 69 73 52 65 71 50 6f 73       | (isReqPos
35618 20 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f   ? FTS3_SEGMENT_
35619 52 45 51 55 49 52 45 5f 50 4f 53 20 3a 20 30 29  REQUIRE_POS : 0)
3561a 0a 20 20 20 20 20 20 20 20 7c 20 28 69 43 6f 6c  .        | (iCol
3561b 75 6d 6e 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3f  umn<p->nColumn ?
3561c 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 43 4f   FTS3_SEGMENT_CO
3561d 4c 55 4d 4e 5f 46 49 4c 54 45 52 20 3a 20 30 29  LUMN_FILTER : 0)
3561e 3b 0a 20 20 66 69 6c 74 65 72 2e 69 43 6f 6c 20  ;.  filter.iCol 
3561f 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 66 69 6c  = iColumn;.  fil
35620 74 65 72 2e 7a 54 65 72 6d 20 3d 20 7a 54 65 72  ter.zTerm = zTer
35621 6d 3b 0a 20 20 66 69 6c 74 65 72 2e 6e 54 65 72  m;.  filter.nTer
35622 6d 20 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20 72 63  m = nTerm;..  rc
35623 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53 65   = sqlite3Fts3Se
35624 67 52 65 61 64 65 72 49 74 65 72 61 74 65 28 70  gReaderIterate(p
35625 2c 20 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65  , apSegment, nSe
35626 67 6d 65 6e 74 2c 20 26 66 69 6c 74 65 72 2c 0a  gment, &filter,.
35627 20 20 20 20 20 20 66 74 73 33 54 65 72 6d 53 65        fts3TermSe
35628 6c 65 63 74 43 62 2c 20 28 76 6f 69 64 20 2a 29  lectCb, (void *)
35629 26 74 73 63 0a 20 20 29 3b 0a 0a 20 20 69 66 28  &tsc.  );..  if(
3562a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3562b 7b 0a 20 20 20 20 2a 70 70 4f 75 74 20 3d 20 74  {.    *ppOut = t
3562c 73 63 2e 61 4f 75 74 70 75 74 3b 0a 20 20 20 20  sc.aOutput;.    
3562d 2a 70 6e 4f 75 74 20 3d 20 74 73 63 2e 6e 4f 75  *pnOut = tsc.nOu
3562e 74 70 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tput;.  }else{. 
3562f 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
35630 74 73 63 2e 61 4f 75 74 70 75 74 29 3b 0a 20 20  tsc.aOutput);.  
35631 7d 0a 0a 66 69 6e 69 73 68 65 64 3a 0a 20 20 73  }..finished:.  s
35632 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
35633 6d 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  mt);.  for(i=0; 
35634 69 3c 6e 53 65 67 6d 65 6e 74 3b 20 69 2b 2b 29  i<nSegment; i++)
35635 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
35636 33 53 65 67 52 65 61 64 65 72 46 72 65 65 28 70  3SegReaderFree(p
35637 2c 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d 29 3b  , apSegment[i]);
35638 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
35639 72 65 65 28 61 70 53 65 67 6d 65 6e 74 29 3b 0a  ree(apSegment);.
3563a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3563b 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ./* .** Return a
3563c 20 44 6f 63 4c 69 73 74 20 63 6f 72 72 65 73 70   DocList corresp
3563d 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 68  onding to the ph
3563e 72 61 73 65 20 2a 70 50 68 72 61 73 65 2e 0a 2a  rase *pPhrase..*
3563f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
35640 33 50 68 72 61 73 65 53 65 6c 65 63 74 28 0a 20  3PhraseSelect(. 
35641 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20   Fts3Table *p,  
35642 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35643 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
35644 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74  e handle */.  Ft
35645 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61 73  s3Phrase *pPhras
35646 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
35647 20 50 68 72 61 73 65 20 74 6f 20 72 65 74 75 72   Phrase to retur
35648 6e 20 61 20 64 6f 63 6c 69 73 74 20 66 6f 72 20  n a doclist for 
35649 2a 2f 0a 20 20 69 6e 74 20 69 73 52 65 71 50 6f  */.  int isReqPo
3564a 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
3564b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3564c 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 63 6f  output should co
3564d 6e 74 61 69 6e 20 70 6f 73 69 74 69 6f 6e 73 20  ntain positions 
3564e 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 61 4f 75  */.  char **paOu
3564f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
35650 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69       /* OUT: Poi
35651 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 64  nter to malloc'd
35652 20 72 65 73 75 6c 74 20 62 75 66 66 65 72 20 2a   result buffer *
35653 2f 0a 20 20 69 6e 74 20 2a 70 6e 4f 75 74 20 20  /.  int *pnOut  
35654 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35655 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 69 7a 65      /* OUT: Size
35656 20 6f 66 20 62 75 66 66 65 72 20 61 74 20 2a 70   of buffer at *p
35657 61 4f 75 74 20 2a 2f 0a 29 7b 0a 20 20 63 68 61  aOut */.){.  cha
35658 72 20 2a 70 4f 75 74 20 3d 20 30 3b 0a 20 20 69  r *pOut = 0;.  i
35659 6e 74 20 6e 4f 75 74 20 3d 20 30 3b 0a 20 20 69  nt nOut = 0;.  i
3565a 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3565b 4b 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69  K;.  int ii;.  i
3565c 6e 74 20 69 43 6f 6c 20 3d 20 70 50 68 72 61 73  nt iCol = pPhras
3565d 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e  e->iColumn;.  in
3565e 74 20 69 73 54 65 72 6d 50 6f 73 20 3d 20 28 70  t isTermPos = (p
3565f 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3e 31  Phrase->nToken>1
35660 20 7c 7c 20 69 73 52 65 71 50 6f 73 29 3b 0a 0a   || isReqPos);..
35661 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 50 65    assert( p->nPe
35662 6e 64 69 6e 67 44 61 74 61 3d 3d 30 20 29 3b 0a  ndingData==0 );.
35663 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
35664 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b  pPhrase->nToken;
35665 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75   ii++){.    stru
35666 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a  ct PhraseToken *
35667 70 54 6f 6b 20 3d 20 26 70 50 68 72 61 73 65 2d  pTok = &pPhrase-
35668 3e 61 54 6f 6b 65 6e 5b 69 69 5d 3b 0a 20 20 20  >aToken[ii];.   
35669 20 63 68 61 72 20 2a 7a 20 3d 20 70 54 6f 6b 2d   char *z = pTok-
3566a 3e 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  >z;            /
3566b 2a 20 4e 65 78 74 20 74 6f 6b 65 6e 20 6f 66 20  * Next token of 
3566c 74 68 65 20 70 68 72 61 73 65 20 2a 2f 0a 20 20  the phrase */.  
3566d 20 20 69 6e 74 20 6e 20 3d 20 70 54 6f 6b 2d 3e    int n = pTok->
3566e 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
3566f 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 20 69 6e 20  /* Size of z in 
35670 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74  bytes */.    int
35671 20 69 73 50 72 65 66 69 78 20 3d 20 70 54 6f 6b   isPrefix = pTok
35672 2d 3e 69 73 50 72 65 66 69 78 3b 2f 2a 20 54 72  ->isPrefix;/* Tr
35673 75 65 20 69 66 20 74 6f 6b 65 6e 20 69 73 20 61  ue if token is a
35674 20 70 72 65 66 69 78 20 2a 2f 0a 20 20 20 20 63   prefix */.    c
35675 68 61 72 20 2a 70 4c 69 73 74 3b 20 20 20 20 20  har *pList;     
35676 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35677 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e  Pointer to token
35678 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 20 20   doclist */.    
35679 69 6e 74 20 6e 4c 69 73 74 3b 20 20 20 20 20 20  int nList;      
3567a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3567b 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20   Size of buffer 
3567c 61 74 20 70 4c 69 73 74 20 2a 2f 0a 0a 20 20 20  at pList */..   
3567d 20 72 63 20 3d 20 66 74 73 33 54 65 72 6d 53 65   rc = fts3TermSe
3567e 6c 65 63 74 28 70 2c 20 69 43 6f 6c 2c 20 7a 2c  lect(p, iCol, z,
3567f 20 6e 2c 20 69 73 50 72 65 66 69 78 2c 20 69 73   n, isPrefix, is
35680 54 65 72 6d 50 6f 73 2c 20 26 6e 4c 69 73 74 2c  TermPos, &nList,
35681 20 26 70 4c 69 73 74 29 3b 0a 20 20 20 20 69 66   &pList);.    if
35682 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35683 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66  ) break;..    if
35684 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ( ii==0 ){.     
35685 20 70 4f 75 74 20 3d 20 70 4c 69 73 74 3b 0a 20   pOut = pList;. 
35686 20 20 20 20 20 6e 4f 75 74 20 3d 20 6e 4c 69 73       nOut = nLis
35687 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  t;.    }else{.  
35688 20 20 20 20 2f 2a 20 4d 65 72 67 65 20 74 68 65      /* Merge the
35689 20 6e 65 77 20 74 65 72 6d 20 6c 69 73 74 20 61   new term list a
3568a 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f  nd the current o
3568b 75 74 70 75 74 2e 20 49 66 20 74 68 69 73 20 69  utput. If this i
3568c 73 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6c  s the.      ** l
3568d 61 73 74 20 74 65 72 6d 20 69 6e 20 74 68 65 20  ast term in the 
3568e 70 68 72 61 73 65 2c 20 61 6e 64 20 70 6f 73 69  phrase, and posi
3568f 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 72 65  tions are not re
35690 71 75 69 72 65 64 20 69 6e 20 74 68 65 0a 20 20  quired in the.  
35691 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 6f 66      ** output of
35692 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
35693 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 63 61  the positions ca
35694 6e 20 62 65 20 64 72 6f 70 70 65 64 20 61 73 20  n be dropped as 
35695 70 61 72 74 0a 20 20 20 20 20 20 2a 2a 20 6f 66  part.      ** of
35696 20 74 68 69 73 20 6d 65 72 67 65 2e 20 45 69 74   this merge. Eit
35697 68 65 72 20 77 61 79 2c 20 74 68 65 20 72 65 73  her way, the res
35698 75 6c 74 20 6f 66 20 74 68 69 73 20 6d 65 72 67  ult of this merg
35699 65 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20  e will be.      
3569a 2a 2a 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  ** smaller than 
3569b 6e 4c 69 73 74 20 62 79 74 65 73 2e 20 54 68 65  nList bytes. The
3569c 20 63 6f 64 65 20 69 6e 20 66 74 73 33 44 6f 63   code in fts3Doc
3569d 6c 69 73 74 4d 65 72 67 65 28 29 20 69 73 20 77  listMerge() is w
3569e 72 69 74 74 65 6e 0a 20 20 20 20 20 20 2a 2a 20  ritten.      ** 
3569f 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 73 61  so that it is sa
356a0 66 65 20 74 6f 20 75 73 65 20 70 4c 69 73 74 20  fe to use pList 
356a1 61 73 20 74 68 65 20 6f 75 74 70 75 74 20 61 73  as the output as
356a2 20 77 65 6c 6c 20 61 73 20 61 6e 20 69 6e 70 75   well as an inpu
356a3 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68  t.      ** in th
356a4 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 2a  is case..      *
356a5 2f 0a 20 20 20 20 20 20 69 6e 74 20 6d 65 72 67  /.      int merg
356a6 65 74 79 70 65 20 3d 20 4d 45 52 47 45 5f 50 4f  etype = MERGE_PO
356a7 53 5f 50 48 52 41 53 45 3b 0a 20 20 20 20 20 20  S_PHRASE;.      
356a8 69 66 28 20 69 69 3d 3d 70 50 68 72 61 73 65 2d  if( ii==pPhrase-
356a9 3e 6e 54 6f 6b 65 6e 2d 31 20 26 26 20 21 69 73  >nToken-1 && !is
356aa 52 65 71 50 6f 73 20 29 7b 0a 20 20 20 20 20 20  ReqPos ){.      
356ab 20 20 6d 65 72 67 65 74 79 70 65 20 3d 20 4d 45    mergetype = ME
356ac 52 47 45 5f 50 48 52 41 53 45 3b 0a 20 20 20 20  RGE_PHRASE;.    
356ad 20 20 7d 0a 20 20 20 20 20 20 66 74 73 33 44 6f    }.      fts3Do
356ae 63 6c 69 73 74 4d 65 72 67 65 28 6d 65 72 67 65  clistMerge(merge
356af 74 79 70 65 2c 20 30 2c 20 30 2c 20 70 4c 69 73  type, 0, 0, pLis
356b0 74 2c 20 26 6e 4f 75 74 2c 20 70 4f 75 74 2c 20  t, &nOut, pOut, 
356b1 6e 4f 75 74 2c 20 70 4c 69 73 74 2c 20 6e 4c 69  nOut, pList, nLi
356b2 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  st);.      sqlit
356b3 65 33 5f 66 72 65 65 28 70 4f 75 74 29 3b 0a 20  e3_free(pOut);. 
356b4 20 20 20 20 20 70 4f 75 74 20 3d 20 70 4c 69 73       pOut = pLis
356b5 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  t;.    }.  }..  
356b6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
356b7 4b 20 29 7b 0a 20 20 20 20 2a 70 61 4f 75 74 20  K ){.    *paOut 
356b8 3d 20 70 4f 75 74 3b 0a 20 20 20 20 2a 70 6e 4f  = pOut;.    *pnO
356b9 75 74 20 3d 20 6e 4f 75 74 3b 0a 20 20 7d 65 6c  ut = nOut;.  }el
356ba 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
356bb 66 72 65 65 28 70 4f 75 74 29 3b 0a 20 20 7d 0a  free(pOut);.  }.
356bc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
356bd 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65 20 74  /*.** Evaluate t
356be 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 65 78 70  he full-text exp
356bf 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61 67  ression pExpr ag
356c0 61 69 6e 73 74 20 66 74 73 33 20 74 61 62 6c 65  ainst fts3 table
356c1 20 70 54 61 62 2e 20 53 74 6f 72 65 0a 2a 2a 20   pTab. Store.** 
356c2 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 64 6f  the resulting do
356c3 63 6c 69 73 74 20 69 6e 20 2a 70 61 4f 75 74 20  clist in *paOut 
356c4 61 6e 64 20 2a 70 6e 4f 75 74 2e 0a 2a 2f 0a 73  and *pnOut..*/.s
356c5 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 46 74  tatic int evalFt
356c6 73 33 45 78 70 72 28 0a 20 20 46 74 73 33 54 61  s3Expr(.  Fts3Ta
356c7 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ble *p,         
356c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72            /* Vir
356c9 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c  tual table handl
356ca 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20  e */.  Fts3Expr 
356cb 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  *pExpr,         
356cc 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 64         /* Parsed
356cd 20 66 74 73 33 20 65 78 70 72 65 73 73 69 6f 6e   fts3 expression
356ce 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 61 4f   */.  char **paO
356cf 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ut,             
356d0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f        /* OUT: Po
356d1 69 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63 27  inter to malloc'
356d2 64 20 72 65 73 75 6c 74 20 62 75 66 66 65 72 20  d result buffer 
356d3 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4f 75 74 20  */.  int *pnOut 
356d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356d5 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 69 7a       /* OUT: Siz
356d6 65 20 6f 66 20 62 75 66 66 65 72 20 61 74 20 2a  e of buffer at *
356d7 70 61 4f 75 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e  paOut */.){.  in
356d8 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
356d9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
356da 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
356db 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 6f  .  /* Zero the o
356dc 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73  utput parameters
356dd 2e 20 2a 2f 0a 20 20 2a 70 61 4f 75 74 20 3d 20  . */.  *paOut = 
356de 30 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 30 3b  0;.  *pnOut = 0;
356df 0a 0a 20 20 69 66 28 20 70 45 78 70 72 20 29 7b  ..  if( pExpr ){
356e0 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e  .    if( pExpr->
356e1 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
356e2 50 48 52 41 53 45 20 29 7b 0a 20 20 20 20 20 20  PHRASE ){.      
356e3 69 6e 74 20 69 73 52 65 71 50 6f 73 20 3d 20 28  int isReqPos = (
356e4 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 20 26  pExpr->pParent &
356e5 26 20 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74  & pExpr->pParent
356e6 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
356e7 59 5f 4e 45 41 52 29 3b 0a 20 20 20 20 20 20 72  Y_NEAR);.      r
356e8 63 20 3d 20 66 74 73 33 50 68 72 61 73 65 53 65  c = fts3PhraseSe
356e9 6c 65 63 74 28 70 2c 20 70 45 78 70 72 2d 3e 70  lect(p, pExpr->p
356ea 50 68 72 61 73 65 2c 20 69 73 52 65 71 50 6f 73  Phrase, isReqPos
356eb 2c 20 70 61 4f 75 74 2c 20 70 6e 4f 75 74 29 3b  , paOut, pnOut);
356ec 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
356ed 20 20 63 68 61 72 20 2a 61 4c 65 66 74 3b 0a 20    char *aLeft;. 
356ee 20 20 20 20 20 63 68 61 72 20 2a 61 52 69 67 68       char *aRigh
356ef 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4c 65  t;.      int nLe
356f0 66 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 52  ft;.      int nR
356f1 69 67 68 74 3b 0a 0a 20 20 20 20 20 20 69 66 28  ight;..      if(
356f2 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
356f3 3d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 70  = evalFts3Expr(p
356f4 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
356f5 20 26 61 52 69 67 68 74 2c 20 26 6e 52 69 67 68   &aRight, &nRigh
356f6 74 29 29 0a 20 20 20 20 20 20 20 26 26 20 53 51  t)).       && SQ
356f7 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 65  LITE_OK==(rc = e
356f8 76 61 6c 46 74 73 33 45 78 70 72 28 70 2c 20 70  valFts3Expr(p, p
356f9 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 61 4c  Expr->pLeft, &aL
356fa 65 66 74 2c 20 26 6e 4c 65 66 74 29 29 0a 20 20  eft, &nLeft)).  
356fb 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 73      ){.        s
356fc 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65 54  witch( pExpr->eT
356fd 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ype ){.         
356fe 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e   case FTSQUERY_N
356ff 45 41 52 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  EAR: {.         
35700 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c 65     Fts3Expr *pLe
35701 66 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ft;.            
35702 46 74 73 33 45 78 70 72 20 2a 70 52 69 67 68 74  Fts3Expr *pRight
35703 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  ;.            in
35704 74 20 6d 65 72 67 65 74 79 70 65 20 3d 20 4d 45  t mergetype = ME
35705 52 47 45 5f 4e 45 41 52 3b 0a 20 20 20 20 20 20  RGE_NEAR;.      
35706 20 20 20 20 20 20 69 6e 74 20 6e 50 61 72 61 6d        int nParam
35707 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  1;.            i
35708 6e 74 20 6e 50 61 72 61 6d 32 3b 0a 20 20 20 20  nt nParam2;.    
35709 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 61 42          char *aB
3570a 75 66 66 65 72 3b 0a 20 20 20 20 20 20 20 20 20  uffer;.         
3570b 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 69    .            i
3570c 66 28 20 70 45 78 70 72 2d 3e 70 50 61 72 65 6e  f( pExpr->pParen
3570d 74 20 26 26 20 70 45 78 70 72 2d 3e 70 50 61 72  t && pExpr->pPar
3570e 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51  ent->eType==FTSQ
3570f 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20  UERY_NEAR ){.   
35710 20 20 20 20 20 20 20 20 20 20 20 6d 65 72 67 65             merge
35711 74 79 70 65 20 3d 20 4d 45 52 47 45 5f 50 4f 53  type = MERGE_POS
35712 5f 4e 45 41 52 3b 0a 20 20 20 20 20 20 20 20 20  _NEAR;.         
35713 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
35714 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e   pLeft = pExpr->
35715 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 20  pLeft;.         
35716 20 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d     while( pLeft-
35717 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
35718 5f 4e 45 41 52 20 29 7b 20 0a 20 20 20 20 20 20  _NEAR ){ .      
35719 20 20 20 20 20 20 20 20 70 4c 65 66 74 3d 70 4c          pLeft=pL
3571a 65 66 74 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  eft->pRight;.   
3571b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3571c 20 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20         pRight = 
3571d 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
3571e 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
3571f 74 28 20 70 52 69 67 68 74 2d 3e 65 54 79 70 65  t( pRight->eType
35720 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  ==FTSQUERY_PHRAS
35721 45 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  E );.           
35722 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e   assert( pLeft->
35723 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
35724 50 48 52 41 53 45 20 29 3b 0a 0a 20 20 20 20 20  PHRASE );..     
35725 20 20 20 20 20 20 20 6e 50 61 72 61 6d 31 20 3d         nParam1 =
35726 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72 2b 31 3b   pExpr->nNear+1;
35727 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 50 61  .            nPa
35728 72 61 6d 32 20 3d 20 6e 50 61 72 61 6d 31 2b 70  ram2 = nParam1+p
35729 4c 65 66 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  Left->pPhrase->n
3572a 54 6f 6b 65 6e 2b 70 52 69 67 68 74 2d 3e 70 50  Token+pRight->pP
3572b 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 32 3b  hrase->nToken-2;
3572c 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 42 75  .            aBu
3572d 66 66 65 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  ffer = sqlite3_m
3572e 61 6c 6c 6f 63 28 6e 4c 65 66 74 2b 6e 52 69 67  alloc(nLeft+nRig
3572f 68 74 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  ht+1);.         
35730 20 20 20 72 63 20 3d 20 66 74 73 33 44 6f 63 6c     rc = fts3Docl
35731 69 73 74 4d 65 72 67 65 28 6d 65 72 67 65 74 79  istMerge(mergety
35732 70 65 2c 20 6e 50 61 72 61 6d 31 2c 20 6e 50 61  pe, nParam1, nPa
35733 72 61 6d 32 2c 20 61 42 75 66 66 65 72 2c 0a 20  ram2, aBuffer,. 
35734 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
35735 6e 4f 75 74 2c 20 61 4c 65 66 74 2c 20 6e 4c 65  nOut, aLeft, nLe
35736 66 74 2c 20 61 52 69 67 68 74 2c 20 6e 52 69 67  ft, aRight, nRig
35737 68 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 29  ht.            )
35738 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
35739 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3573a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
3573b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 42   sqlite3_free(aB
3573c 75 66 66 65 72 29 3b 0a 20 20 20 20 20 20 20 20  uffer);.        
3573d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3573e 20 20 20 20 20 20 20 20 20 2a 70 61 4f 75 74 20           *paOut 
3573f 3d 20 61 42 75 66 66 65 72 3b 0a 20 20 20 20 20  = aBuffer;.     
35740 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
35741 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
35742 65 28 61 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  e(aLeft);.      
35743 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
35744 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
35745 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
35746 59 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 20 20  Y_OR: {.        
35747 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20      /* Allocate 
35748 61 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65  a buffer for the
35749 20 6f 75 74 70 75 74 2e 20 54 68 65 20 6d 61 78   output. The max
3574a 69 6d 75 6d 20 73 69 7a 65 20 69 73 20 74 68 65  imum size is the
3574b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
3574c 73 75 6d 20 6f 66 20 74 68 65 20 73 69 7a 65 73  sum of the sizes
3574d 20 6f 66 20 74 68 65 20 74 77 6f 20 69 6e 70 75   of the two inpu
3574e 74 20 62 75 66 66 65 72 73 2e 20 54 68 65 20 2b  t buffers. The +
3574f 31 20 74 65 72 6d 20 69 73 0a 20 20 20 20 20 20  1 term is.      
35750 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74        ** so that
35751 20 61 20 62 75 66 66 65 72 20 6f 66 20 7a 65 72   a buffer of zer
35752 6f 20 62 79 74 65 73 20 69 73 20 6e 65 76 65 72  o bytes is never
35753 20 61 6c 6c 6f 63 61 74 65 64 20 2d 20 74 68 69   allocated - thi
35754 73 20 63 61 6e 0a 20 20 20 20 20 20 20 20 20 20  s can.          
35755 20 20 2a 2a 20 63 61 75 73 65 20 66 74 73 33 44    ** cause fts3D
35756 6f 63 6c 69 73 74 4d 65 72 67 65 28 29 20 74 6f  oclistMerge() to
35757 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 72 65 74   incorrectly ret
35758 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
35759 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  ..            */
3575a 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61  .            cha
3575b 72 20 2a 61 42 75 66 66 65 72 20 3d 20 73 71 6c  r *aBuffer = sql
3575c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 69 67  ite3_malloc(nRig
3575d 68 74 2b 6e 4c 65 66 74 2b 31 29 3b 0a 20 20 20  ht+nLeft+1);.   
3575e 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74           rc = ft
3575f 73 33 44 6f 63 6c 69 73 74 4d 65 72 67 65 28 4d  s3DoclistMerge(M
35760 45 52 47 45 5f 4f 52 2c 20 30 2c 20 30 2c 20 61  ERGE_OR, 0, 0, a
35761 42 75 66 66 65 72 2c 20 70 6e 4f 75 74 2c 0a 20  Buffer, pnOut,. 
35762 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
35763 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 61 52 69  Left, nLeft, aRi
35764 67 68 74 2c 20 6e 52 69 67 68 74 0a 20 20 20 20  ght, nRight.    
35765 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
35766 20 20 20 20 20 20 20 2a 70 61 4f 75 74 20 3d 20         *paOut = 
35767 61 42 75 66 66 65 72 3b 0a 20 20 20 20 20 20 20  aBuffer;.       
35768 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
35769 65 28 61 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  e(aLeft);.      
3576a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3576b 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
3576c 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
3576d 59 5f 41 4e 44 3a 0a 20 20 20 20 20 20 20 20 20  Y_AND:.         
3576e 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e   case FTSQUERY_N
3576f 4f 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  OT: {.          
35770 20 20 61 73 73 65 72 74 28 20 46 54 53 51 55 45    assert( FTSQUE
35771 52 59 5f 4e 4f 54 3d 3d 4d 45 52 47 45 5f 4e 4f  RY_NOT==MERGE_NO
35772 54 20 26 26 20 46 54 53 51 55 45 52 59 5f 41 4e  T && FTSQUERY_AN
35773 44 3d 3d 4d 45 52 47 45 5f 41 4e 44 20 29 3b 0a  D==MERGE_AND );.
35774 20 20 20 20 20 20 20 20 20 20 20 20 66 74 73 33              fts3
35775 44 6f 63 6c 69 73 74 4d 65 72 67 65 28 70 45 78  DoclistMerge(pEx
35776 70 72 2d 3e 65 54 79 70 65 2c 20 30 2c 20 30 2c  pr->eType, 0, 0,
35777 20 61 4c 65 66 74 2c 20 70 6e 4f 75 74 2c 0a 20   aLeft, pnOut,. 
35778 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
35779 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 61 52 69  Left, nLeft, aRi
3577a 67 68 74 2c 20 6e 52 69 67 68 74 0a 20 20 20 20  ght, nRight.    
3577b 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
3577c 20 20 20 20 20 20 20 2a 70 61 4f 75 74 20 3d 20         *paOut = 
3577d 61 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 20  aLeft;.         
3577e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3577f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
35780 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
35781 6c 69 74 65 33 5f 66 72 65 65 28 61 52 69 67 68  lite3_free(aRigh
35782 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
35783 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
35784 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
35785 20 78 46 69 6c 74 65 72 20 69 6e 74 65 72 66 61   xFilter interfa
35786 63 65 20 66 6f 72 20 74 68 65 20 76 69 72 74 75  ce for the virtu
35787 61 6c 20 74 61 62 6c 65 2e 20 20 53 65 65 0a 2a  al table.  See.*
35788 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  * the virtual ta
35789 62 6c 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68  ble xFilter meth
3578a 6f 64 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  od documentation
3578b 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a   for additional.
3578c 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  ** information..
3578d 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d  **.** If idxNum=
3578e 3d 46 54 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53  =FTS3_FULLSCAN_S
3578f 45 41 52 43 48 20 74 68 65 6e 20 64 6f 20 61 20  EARCH then do a 
35790 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 20  full table scan 
35791 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 25  against.** the %
35792 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e 0a  _content table..
35793 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d  **.** If idxNum=
35794 3d 46 54 53 33 5f 44 4f 43 49 44 5f 53 45 41 52  =FTS3_DOCID_SEAR
35795 43 48 20 74 68 65 6e 20 64 6f 20 61 20 64 6f 63  CH then do a doc
35796 69 64 20 6c 6f 6f 6b 75 70 20 66 6f 72 20 61 20  id lookup for a 
35797 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20  single entry.** 
35798 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  in the %_content
35799 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
3579a 20 69 64 78 4e 75 6d 3e 3d 46 54 53 33 5f 46 55   idxNum>=FTS3_FU
3579b 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 20 74 68  LLTEXT_SEARCH th
3579c 65 6e 20 75 73 65 20 74 68 65 20 66 75 6c 6c 20  en use the full 
3579d 74 65 78 74 20 69 6e 64 65 78 2e 20 20 54 68 65  text index.  The
3579e 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68  .** column on th
3579f 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  e left-hand side
357a0 20 6f 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70   of the MATCH op
357a1 65 72 61 74 6f 72 20 69 73 20 63 6f 6c 75 6d 6e  erator is column
357a2 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 64 78 4e 75  .** number idxNu
357a3 6d 2d 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f  m-FTS3_FULLTEXT_
357a4 53 45 41 52 43 48 2c 20 30 20 69 6e 64 65 78 65  SEARCH, 0 indexe
357a5 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74  d.  argv[0] is t
357a6 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a  he right-hand.**
357a7 20 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54   side of the MAT
357a8 43 48 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a  CH operator..*/.
357a9 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55  /* TODO(shess) U
357aa 70 67 72 61 64 65 20 74 68 65 20 63 75 72 73 6f  pgrade the curso
357ab 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  r initialization
357ac 20 61 6e 64 20 64 65 73 74 72 75 63 74 69 6f 6e   and destruction
357ad 20 74 6f 0a 2a 2a 20 61 63 63 6f 75 6e 74 20 66   to.** account f
357ae 6f 72 20 66 74 73 33 46 69 6c 74 65 72 4d 65 74  or fts3FilterMet
357af 68 6f 64 28 29 20 62 65 69 6e 67 20 63 61 6c 6c  hod() being call
357b0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
357b1 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65  s on the.** same
357b2 20 63 75 72 73 6f 72 2e 20 54 68 65 20 63 75 72   cursor. The cur
357b3 72 65 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 69 73  rent solution is
357b4 20 76 65 72 79 20 66 72 61 67 69 6c 65 2e 20 41   very fragile. A
357b5 70 70 6c 79 20 66 69 78 20 74 6f 0a 2a 2a 20 66  pply fix to.** f
357b6 74 73 33 20 61 73 20 61 70 70 72 6f 70 72 69 61  ts3 as appropria
357b7 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  te..*/.static in
357b8 74 20 66 74 73 33 46 69 6c 74 65 72 4d 65 74 68  t fts3FilterMeth
357b9 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  od(.  sqlite3_vt
357ba 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  ab_cursor *pCurs
357bb 6f 72 2c 20 20 20 2f 2a 20 54 68 65 20 63 75 72  or,   /* The cur
357bc 73 6f 72 20 75 73 65 64 20 66 6f 72 20 74 68 69  sor used for thi
357bd 73 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74  s query */.  int
357be 20 69 64 78 4e 75 6d 2c 20 20 20 20 20 20 20 20   idxNum,        
357bf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
357c0 53 74 72 61 74 65 67 79 20 69 6e 64 65 78 20 2a  Strategy index *
357c1 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
357c2 69 64 78 53 74 72 2c 20 20 20 20 20 20 20 20 20  idxStr,         
357c3 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 20 2a 2f      /* Unused */
357c4 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20  .  int nVal,    
357c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357c6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
357c7 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 70 56 61  elements in apVa
357c8 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  l */.  sqlite3_v
357c9 61 6c 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20  alue **apVal    
357ca 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
357cb 6e 74 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65  nts for the inde
357cc 78 69 6e 67 20 73 63 68 65 6d 65 20 2a 2f 0a 29  xing scheme */.)
357cd 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
357ce 61 7a 53 71 6c 5b 5d 20 3d 20 7b 0a 20 20 20 20  azSql[] = {.    
357cf 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 25  "SELECT * FROM %
357d0 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 57  Q.'%q_content' W
357d1 48 45 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 2c  HERE docid = ?",
357d2 20 2f 2a 20 6e 6f 6e 2d 66 75 6c 6c 2d 74 61 62   /* non-full-tab
357d3 6c 65 2d 73 63 61 6e 20 2a 2f 0a 20 20 20 20 22  le-scan */.    "
357d4 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 25 51  SELECT * FROM %Q
357d5 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 22 2c 20  .'%q_content'", 
357d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357d7 2f 2a 20 66 75 6c 6c 2d 74 61 62 6c 65 2d 73 63  /* full-table-sc
357d8 61 6e 20 2a 2f 0a 20 20 7d 3b 0a 20 20 69 6e 74  an */.  };.  int
357d9 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
357da 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
357db 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
357dc 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20   char *zSql;    
357dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357de 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
357df 74 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  t used to access
357e0 20 25 5f 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20   %_content */.  
357e1 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28  Fts3Table *p = (
357e2 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 75 72  Fts3Table *)pCur
357e3 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 46 74  sor->pVtab;.  Ft
357e4 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  s3Cursor *pCsr =
357e5 20 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 70   (Fts3Cursor *)p
357e6 43 75 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65 72  Cursor;..  asser
357e7 74 28 20 69 64 78 4e 75 6d 3e 3d 30 20 26 26 20  t( idxNum>=0 && 
357e8 69 64 78 4e 75 6d 3c 3d 28 46 54 53 33 5f 46 55  idxNum<=(FTS3_FU
357e9 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 2b 70 2d  LLTEXT_SEARCH+p-
357ea 3e 6e 43 6f 6c 75 6d 6e 29 20 29 3b 0a 20 20 61  >nColumn) );.  a
357eb 73 73 65 72 74 28 20 6e 56 61 6c 3d 3d 30 20 7c  ssert( nVal==0 |
357ec 7c 20 6e 56 61 6c 3d 3d 31 20 29 3b 0a 20 20 61  | nVal==1 );.  a
357ed 73 73 65 72 74 28 20 28 6e 56 61 6c 3d 3d 30 29  ssert( (nVal==0)
357ee 3d 3d 28 69 64 78 4e 75 6d 3d 3d 46 54 53 33 5f  ==(idxNum==FTS3_
357ef 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48 29  FULLSCAN_SEARCH)
357f0 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 63 61 73   );..  /* In cas
357f1 65 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73  e the cursor has
357f2 20 62 65 65 6e 20 75 73 65 64 20 62 65 66 6f 72   been used befor
357f3 65 2c 20 63 6c 65 61 72 20 69 74 20 6e 6f 77 2e  e, clear it now.
357f4 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
357f5 6e 61 6c 69 7a 65 28 70 43 73 72 2d 3e 70 53 74  nalize(pCsr->pSt
357f6 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  mt);.  sqlite3_f
357f7 72 65 65 28 70 43 73 72 2d 3e 61 44 6f 63 6c 69  ree(pCsr->aDocli
357f8 73 74 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70  st);.  memset(&p
357f9 43 75 72 73 6f 72 5b 31 5d 2c 20 30 2c 20 73 69  Cursor[1], 0, si
357fa 7a 65 6f 66 28 46 74 73 33 43 75 72 73 6f 72 29  zeof(Fts3Cursor)
357fb 2d 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f  -sizeof(sqlite3_
357fc 76 74 61 62 5f 63 75 72 73 6f 72 29 29 3b 0a 0a  vtab_cursor));..
357fd 20 20 2f 2a 20 43 6f 6d 70 69 6c 65 20 61 20 53    /* Compile a S
357fe 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
357ff 66 6f 72 20 74 68 69 73 20 63 75 72 73 6f 72 2e  for this cursor.
35800 20 46 6f 72 20 61 20 66 75 6c 6c 2d 74 61 62 6c   For a full-tabl
35801 65 2d 73 63 61 6e 2c 20 74 68 65 0a 20 20 2a 2a  e-scan, the.  **
35802 20 73 74 61 74 65 6d 65 6e 74 20 6c 6f 6f 70 73   statement loops
35803 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 72 6f 77   through all row
35804 73 20 6f 66 20 74 68 65 20 25 5f 63 6f 6e 74 65  s of the %_conte
35805 6e 74 20 74 61 62 6c 65 2e 20 46 6f 72 20 61 0a  nt table. For a.
35806 20 20 2a 2a 20 66 75 6c 6c 2d 74 65 78 74 20 71    ** full-text q
35807 75 65 72 79 20 6f 72 20 64 6f 63 69 64 20 6c 6f  uery or docid lo
35808 6f 6b 75 70 2c 20 74 68 65 20 73 74 61 74 65 6d  okup, the statem
35809 65 6e 74 20 72 65 74 72 69 65 76 65 73 20 61 20  ent retrieves a 
3580a 73 69 6e 67 6c 65 0a 20 20 2a 2a 20 72 6f 77 20  single.  ** row 
3580b 62 79 20 64 6f 63 69 64 2e 0a 20 20 2a 2f 0a 20  by docid..  */. 
3580c 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f   zSql = sqlite3_
3580d 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b 69 64  mprintf(azSql[id
3580e 78 4e 75 6d 3d 3d 46 54 53 33 5f 46 55 4c 4c 53  xNum==FTS3_FULLS
3580f 43 41 4e 5f 53 45 41 52 43 48 5d 2c 20 70 2d 3e  CAN_SEARCH], p->
35810 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a  zDb, p->zName);.
35811 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20    if( !zSql ){. 
35812 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
35813 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OMEM;.  }else{. 
35814 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
35815 70 72 65 70 61 72 65 5f 76 32 28 70 2d 3e 64 62  prepare_v2(p->db
35816 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 43 73  , zSql, -1, &pCs
35817 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  r->pStmt, 0);.  
35818 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
35819 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Sql);.  }.  if( 
3581a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3581b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 43 73  return rc;.  pCs
3581c 72 2d 3e 65 54 79 70 65 20 3d 20 69 64 78 4e 75  r->eType = idxNu
3581d 6d 3b 0a 0a 20 20 69 66 28 20 69 64 78 4e 75 6d  m;..  if( idxNum
3581e 3d 3d 46 54 53 33 5f 44 4f 43 49 44 5f 53 45 41  ==FTS3_DOCID_SEA
3581f 52 43 48 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  RCH ){.    rc = 
35820 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c  sqlite3_bind_val
35821 75 65 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20  ue(pCsr->pStmt, 
35822 31 2c 20 61 70 56 61 6c 5b 30 5d 29 3b 0a 20 20  1, apVal[0]);.  
35823 7d 65 6c 73 65 20 69 66 28 20 69 64 78 4e 75 6d  }else if( idxNum
35824 21 3d 46 54 53 33 5f 46 55 4c 4c 53 43 41 4e 5f  !=FTS3_FULLSCAN_
35825 53 45 41 52 43 48 20 29 7b 0a 20 20 20 20 69 6e  SEARCH ){.    in
35826 74 20 69 43 6f 6c 20 3d 20 69 64 78 4e 75 6d 2d  t iCol = idxNum-
35827 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45  FTS3_FULLTEXT_SE
35828 41 52 43 48 3b 0a 20 20 20 20 63 6f 6e 73 74 20  ARCH;.    const 
35829 63 68 61 72 20 2a 7a 51 75 65 72 79 20 3d 20 28  char *zQuery = (
3582a 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
3582b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
3582c 61 70 56 61 6c 5b 30 5d 29 3b 0a 0a 20 20 20 20  apVal[0]);..    
3582d 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
3582e 50 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73  PendingTermsFlus
3582f 68 28 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63  h(p);.    if( rc
35830 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35831 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63  turn rc;..    rc
35832 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 45 78   = sqlite3Fts3Ex
35833 70 72 50 61 72 73 65 28 70 2d 3e 70 54 6f 6b 65  prParse(p->pToke
35834 6e 69 7a 65 72 2c 20 70 2d 3e 61 7a 43 6f 6c 75  nizer, p->azColu
35835 6d 6e 2c 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20  mn, p->nColumn, 
35836 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 2c 20 7a  .        iCol, z
35837 51 75 65 72 79 2c 20 2d 31 2c 20 26 70 43 73 72  Query, -1, &pCsr
35838 2d 3e 70 45 78 70 72 0a 20 20 20 20 29 3b 0a 20  ->pExpr.    );. 
35839 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
3583a 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
3583b 3b 0a 0a 20 20 20 20 72 63 20 3d 20 65 76 61 6c  ;..    rc = eval
3583c 46 74 73 33 45 78 70 72 28 70 2c 20 70 43 73 72  Fts3Expr(p, pCsr
3583d 2d 3e 70 45 78 70 72 2c 20 26 70 43 73 72 2d 3e  ->pExpr, &pCsr->
3583e 61 44 6f 63 6c 69 73 74 2c 20 26 70 43 73 72 2d  aDoclist, &pCsr-
3583f 3e 6e 44 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20  >nDoclist);.    
35840 70 43 73 72 2d 3e 70 4e 65 78 74 49 64 20 3d 20  pCsr->pNextId = 
35841 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74 3b 0a  pCsr->aDoclist;.
35842 20 20 20 20 70 43 73 72 2d 3e 69 50 72 65 76 49      pCsr->iPrevI
35843 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66  d = 0;.  }..  if
35844 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35845 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
35846 65 74 75 72 6e 20 66 74 73 33 4e 65 78 74 4d 65  eturn fts3NextMe
35847 74 68 6f 64 28 70 43 75 72 73 6f 72 29 3b 0a 7d  thod(pCursor);.}
35848 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69 73  ../* .** This is
35849 20 74 68 65 20 78 45 6f 66 20 6d 65 74 68 6f 64   the xEof method
3584a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
3584b 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 63 61  table. SQLite ca
3584c 6c 6c 73 20 74 68 69 73 20 0a 2a 2a 20 72 6f 75  lls this .** rou
3584d 74 69 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74  tine to find out
3584e 20 69 66 20 69 74 20 68 61 73 20 72 65 61 63 68   if it has reach
3584f 65 64 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  ed the end of a 
35850 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73  result set..*/.s
35851 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 6f  tatic int fts3Eo
35852 66 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f  fMethod(sqlite3_
35853 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
35854 72 73 6f 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  rsor){.  return 
35855 28 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 70  ((Fts3Cursor *)p
35856 43 75 72 73 6f 72 29 2d 3e 69 73 45 6f 66 3b 0a  Cursor)->isEof;.
35857 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69  }../* .** This i
35858 73 20 74 68 65 20 78 43 6f 6c 75 6d 6e 20 6d 65  s the xColumn me
35859 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74  thod of the virt
3585a 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20  ual table.  The 
3585b 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 63  SQLite.** core c
3585c 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64  alls this method
3585d 20 64 75 72 69 6e 67 20 61 20 71 75 65 72 79 20   during a query 
3585e 77 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 68  when it needs th
3585f 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 61 20  e value.** of a 
35860 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20  column from the 
35861 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
35862 54 68 69 73 20 6d 65 74 68 6f 64 20 6e 65 65 64  This method need
35863 73 20 74 6f 20 75 73 65 0a 2a 2a 20 6f 6e 65 20  s to use.** one 
35864 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  of the sqlite3_r
35865 65 73 75 6c 74 5f 2a 28 29 20 72 6f 75 74 69 6e  esult_*() routin
35866 65 73 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  es to store the 
35867 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 76 61 6c  requested.** val
35868 75 65 20 62 61 63 6b 20 69 6e 20 74 68 65 20 70  ue back in the p
35869 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74  Context..*/.stat
3586a 69 63 20 69 6e 74 20 66 74 73 33 43 6f 6c 75 6d  ic int fts3Colum
3586b 6e 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f  nMethod(sqlite3_
3586c 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
3586d 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  rsor,.          
3586e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3586f 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
35870 2a 70 43 6f 6e 74 65 78 74 2c 20 69 6e 74 20 69  *pContext, int i
35871 64 78 43 6f 6c 29 7b 0a 20 20 46 74 73 33 43 75  dxCol){.  Fts3Cu
35872 72 73 6f 72 20 2a 63 20 3d 20 28 46 74 73 33 43  rsor *c = (Fts3C
35873 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
35874 3b 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 76  ;.  Fts3Table *v
35875 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28 63   = cursor_vtab(c
35876 29 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 74  );.  int rc = ft
35877 73 33 43 75 72 73 6f 72 53 65 65 6b 28 63 29 3b  s3CursorSeek(c);
35878 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35879 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
3587a 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66  rn rc;.  }..  if
3587b 28 20 69 64 78 43 6f 6c 3c 76 2d 3e 6e 43 6f 6c  ( idxCol<v->nCol
3587c 75 6d 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  umn ){.    sqlit
3587d 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d  e3_value *pVal =
3587e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
3587f 76 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c 20  value(c->pStmt, 
35880 69 64 78 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 73  idxCol+1);.    s
35881 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
35882 6c 75 65 28 70 43 6f 6e 74 65 78 74 2c 20 70 56  lue(pContext, pV
35883 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  al);.  }else if(
35884 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f 6c   idxCol==v->nCol
35885 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  umn ){.    /* Th
35886 65 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 20 77  e extra column w
35887 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 74 68 65  hose name is the
35888 20 73 61 6d 65 20 61 73 20 74 68 65 20 74 61 62   same as the tab
35889 6c 65 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75 72  le..    ** Retur
3588a 6e 20 61 20 62 6c 6f 62 20 77 68 69 63 68 20 69  n a blob which i
3588b 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
3588c 68 65 20 63 75 72 73 6f 72 0a 20 20 20 20 2a 2f  he cursor.    */
3588d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3588e 75 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e 74 65 78  ult_blob(pContex
3588f 74 2c 20 26 63 2c 20 73 69 7a 65 6f 66 28 63 29  t, &c, sizeof(c)
35890 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
35891 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  NT);.  }else if(
35892 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f 6c   idxCol==v->nCol
35893 75 6d 6e 2b 31 20 29 7b 0a 20 20 20 20 2f 2a 20  umn+1 ){.    /* 
35894 54 68 65 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e  The docid column
35895 2c 20 77 68 69 63 68 20 69 73 20 61 6e 20 61 6c  , which is an al
35896 69 61 73 20 66 6f 72 20 72 6f 77 69 64 2e 20 2a  ias for rowid. *
35897 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  /.    sqlite3_va
35898 6c 75 65 20 2a 70 56 61 6c 20 3d 20 73 71 6c 69  lue *pVal = sqli
35899 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65  te3_column_value
3589a 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20  (c->pStmt, 0);. 
3589b 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3589c 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65 78 74  t_value(pContext
3589d 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20 72  , pVal);.  }.  r
3589e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3589f 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20  .}../* .** This 
358a0 69 73 20 74 68 65 20 78 52 6f 77 69 64 20 6d 65  is the xRowid me
358a1 74 68 6f 64 2e 20 54 68 65 20 53 51 4c 69 74 65  thod. The SQLite
358a2 20 63 6f 72 65 20 63 61 6c 6c 73 20 74 68 69 73   core calls this
358a3 20 72 6f 75 74 69 6e 65 20 74 6f 0a 2a 2a 20 72   routine to.** r
358a4 65 74 72 69 65 76 65 20 74 68 65 20 72 6f 77 69  etrieve the rowi
358a5 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  d for the curren
358a6 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73  t row of the res
358a7 75 6c 74 20 73 65 74 2e 20 66 74 73 33 0a 2a 2a  ult set. fts3.**
358a8 20 65 78 70 6f 73 65 73 20 25 5f 63 6f 6e 74 65   exposes %_conte
358a9 6e 74 2e 64 6f 63 69 64 20 61 73 20 74 68 65 20  nt.docid as the 
358aa 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 76 69  rowid for the vi
358ab 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 65  rtual table. The
358ac 0a 2a 2a 20 72 6f 77 69 64 20 73 68 6f 75 6c 64  .** rowid should
358ad 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 2a   be written to *
358ae 70 52 6f 77 69 64 2e 0a 2a 2f 0a 73 74 61 74 69  pRowid..*/.stati
358af 63 20 69 6e 74 20 66 74 73 33 52 6f 77 69 64 4d  c int fts3RowidM
358b0 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74  ethod(sqlite3_vt
358b1 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  ab_cursor *pCurs
358b2 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  or, sqlite_int64
358b3 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20 46 74 73   *pRowid){.  Fts
358b4 33 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20  3Cursor *pCsr = 
358b5 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 20 70  (Fts3Cursor *) p
358b6 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43  Cursor;.  if( pC
358b7 73 72 2d 3e 61 44 6f 63 6c 69 73 74 20 29 7b 0a  sr->aDoclist ){.
358b8 20 20 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 43      *pRowid = pC
358b9 73 72 2d 3e 69 50 72 65 76 49 64 3b 0a 20 20 7d  sr->iPrevId;.  }
358ba 65 6c 73 65 7b 0a 20 20 20 20 2a 70 52 6f 77 69  else{.    *pRowi
358bb 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
358bc 6d 6e 5f 69 6e 74 36 34 28 70 43 73 72 2d 3e 70  mn_int64(pCsr->p
358bd 53 74 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  Stmt, 0);.  }.  
358be 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
358bf 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73  ;.}../* .** This
358c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65   function is the
358c1 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
358c2 6f 66 20 74 68 65 20 78 55 70 64 61 74 65 20 63  of the xUpdate c
358c3 61 6c 6c 62 61 63 6b 20 75 73 65 64 20 62 79 20  allback used by 
358c4 0a 2a 2a 20 46 54 53 33 20 76 69 72 74 75 61 6c  .** FTS3 virtual
358c5 20 74 61 62 6c 65 73 2e 20 49 74 20 69 73 20 69   tables. It is i
358c6 6e 76 6f 6b 65 64 20 62 79 20 53 51 4c 69 74 65  nvoked by SQLite
358c7 20 65 61 63 68 20 74 69 6d 65 20 61 20 72 6f 77   each time a row
358c8 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 69 6e 73   is to be.** ins
358c9 65 72 74 65 64 2c 20 75 70 64 61 74 65 64 20 6f  erted, updated o
358ca 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74  r deleted..*/.st
358cb 61 74 69 63 20 69 6e 74 20 66 74 73 33 55 70 64  atic int fts3Upd
358cc 61 74 65 4d 65 74 68 6f 64 28 0a 20 20 73 71 6c  ateMethod(.  sql
358cd 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
358ce 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
358cf 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
358d0 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  ndle */.  int nA
358d1 72 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rg,             
358d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
358d3 65 20 6f 66 20 61 72 67 75 6d 65 6e 74 20 61 72  e of argument ar
358d4 72 61 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ray */.  sqlite3
358d5 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 2c 20  _value **apVal, 
358d6 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
358d7 79 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a  y of arguments *
358d8 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  /.  sqlite_int64
358d9 20 2a 70 52 6f 77 69 64 20 20 20 20 20 20 20 20   *pRowid        
358da 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 68 65 20      /* OUT: The 
358db 61 66 66 65 63 74 65 64 20 28 6f 72 20 65 66 66  affected (or eff
358dc 65 63 74 65 64 29 20 72 6f 77 69 64 20 2a 2f 0a  ected) rowid */.
358dd 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
358de 74 65 33 46 74 73 33 55 70 64 61 74 65 4d 65 74  te3Fts3UpdateMet
358df 68 6f 64 28 70 56 74 61 62 2c 20 6e 41 72 67 2c  hod(pVtab, nArg,
358e0 20 61 70 56 61 6c 2c 20 70 52 6f 77 69 64 29 3b   apVal, pRowid);
358e1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
358e2 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 79 6e  entation of xSyn
358e3 63 28 29 20 6d 65 74 68 6f 64 2e 20 46 6c 75 73  c() method. Flus
358e4 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  h the contents o
358e5 66 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65  f the pending-te
358e6 72 6d 73 0a 2a 2a 20 68 61 73 68 2d 74 61 62 6c  rms.** hash-tabl
358e7 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
358e8 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
358e9 20 66 74 73 33 53 79 6e 63 4d 65 74 68 6f 64 28   fts3SyncMethod(
358ea 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
358eb 74 61 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  tab){.  return s
358ec 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e  qlite3Fts3Pendin
358ed 67 54 65 72 6d 73 46 6c 75 73 68 28 28 46 74 73  gTermsFlush((Fts
358ee 33 54 61 62 6c 65 20 2a 29 70 56 74 61 62 29 3b  3Table *)pVtab);
358ef 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
358f0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 42 65 67  entation of xBeg
358f1 69 6e 28 29 20 6d 65 74 68 6f 64 2e 20 54 68 69  in() method. Thi
358f2 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  s is a no-op..*/
358f3 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
358f4 42 65 67 69 6e 4d 65 74 68 6f 64 28 73 71 6c 69  BeginMethod(sqli
358f5 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
358f6 7b 0a 20 20 61 73 73 65 72 74 28 20 28 28 46 74  {.  assert( ((Ft
358f7 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61 62 29  s3Table *)pVtab)
358f8 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3d 3d  ->nPendingData==
358f9 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  0 );.  return SQ
358fa 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
358fb 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
358fc 20 6f 66 20 78 43 6f 6d 6d 69 74 28 29 20 6d 65   of xCommit() me
358fd 74 68 6f 64 2e 20 54 68 69 73 20 69 73 20 61 20  thod. This is a 
358fe 6e 6f 2d 6f 70 2e 20 54 68 65 20 63 6f 6e 74 65  no-op. The conte
358ff 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20 70 65  nts of.** the pe
35900 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68 61 73 68  nding-terms hash
35901 2d 74 61 62 6c 65 20 68 61 76 65 20 61 6c 72 65  -table have alre
35902 61 64 79 20 62 65 65 6e 20 66 6c 75 73 68 65 64  ady been flushed
35903 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
35904 73 65 0a 2a 2a 20 62 79 20 66 74 73 33 53 79 6e  se.** by fts3Syn
35905 63 4d 65 74 68 6f 64 28 29 2e 0a 2a 2f 0a 73 74  cMethod()..*/.st
35906 61 74 69 63 20 69 6e 74 20 66 74 73 33 43 6f 6d  atic int fts3Com
35907 6d 69 74 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  mitMethod(sqlite
35908 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
35909 20 20 61 73 73 65 72 74 28 20 28 28 46 74 73 33    assert( ((Fts3
3590a 54 61 62 6c 65 20 2a 29 70 56 74 61 62 29 2d 3e  Table *)pVtab)->
3590b 6e 50 65 6e 64 69 6e 67 44 61 74 61 3d 3d 30 20  nPendingData==0 
3590c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
3590d 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
3590e 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
3590f 66 20 78 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 44  f xRollback(). D
35910 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65  iscard the conte
35911 6e 74 73 20 6f 66 20 74 68 65 20 70 65 6e 64 69  nts of the pendi
35912 6e 67 2d 74 65 72 6d 73 0a 2a 2a 20 68 61 73 68  ng-terms.** hash
35913 2d 74 61 62 6c 65 2e 20 41 6e 79 20 63 68 61 6e  -table. Any chan
35914 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20  ges made to the 
35915 64 61 74 61 62 61 73 65 20 61 72 65 20 72 65 76  database are rev
35916 65 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e  erted by SQLite.
35917 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
35918 74 73 33 52 6f 6c 6c 62 61 63 6b 4d 65 74 68 6f  ts3RollbackMetho
35919 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  d(sqlite3_vtab *
3591a 70 56 74 61 62 29 7b 0a 20 20 73 71 6c 69 74 65  pVtab){.  sqlite
3591b 33 46 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d  3Fts3PendingTerm
3591c 73 43 6c 65 61 72 28 28 46 74 73 33 54 61 62 6c  sClear((Fts3Tabl
3591d 65 20 2a 29 70 56 74 61 62 29 3b 0a 20 20 72 65  e *)pVtab);.  re
3591e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3591f 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  }../*.** Helper 
35920 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79  function used by
35921 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
35922 69 6f 6e 20 6f 66 20 74 68 65 20 6f 76 65 72 6c  ion of the overl
35923 6f 61 64 65 64 20 73 6e 69 70 70 65 74 28 29 2c  oaded snippet(),
35924 0a 2a 2a 20 6f 66 66 73 65 74 73 28 29 20 61 6e  .** offsets() an
35925 64 20 6f 70 74 69 6d 69 7a 65 28 29 20 53 51 4c  d optimize() SQL
35926 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a   functions..**.*
35927 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 70  * If the value p
35928 61 73 73 65 64 20 61 73 20 74 68 65 20 74 68 69  assed as the thi
35929 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  rd argument is a
3592a 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 0a 2a 2a   blob of size.**
3592b 20 73 69 7a 65 6f 66 28 46 74 73 33 43 75 72 73   sizeof(Fts3Curs
3592c 6f 72 2a 29 2c 20 74 68 65 6e 20 74 68 65 20 62  or*), then the b
3592d 6c 6f 62 20 63 6f 6e 74 65 6e 74 73 20 61 72 65  lob contents are
3592e 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 0a   copied to the .
3592f 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62  ** output variab
35930 6c 65 20 2a 70 70 43 73 72 20 61 6e 64 20 53 51  le *ppCsr and SQ
35931 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
35932 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
35933 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73  an error.** mess
35934 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74  age is written t
35935 6f 20 63 6f 6e 74 65 78 74 20 70 43 6f 6e 74 65  o context pConte
35936 78 74 20 61 6e 64 20 53 51 4c 49 54 45 5f 45 52  xt and SQLITE_ER
35937 52 4f 52 20 72 65 74 75 72 6e 65 64 2e 20 54 68  ROR returned. Th
35938 65 0a 2a 2a 20 73 74 72 69 6e 67 20 70 61 73 73  e.** string pass
35939 65 64 20 76 69 61 20 7a 46 75 6e 63 20 69 73 20  ed via zFunc is 
3593a 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
3593b 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
3593c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
3593d 20 66 74 73 33 46 75 6e 63 74 69 6f 6e 41 72 67   fts3FunctionArg
3593e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
3593f 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20  ext *pContext,  
35940 20 20 20 20 2f 2a 20 53 51 4c 20 66 75 6e 63 74      /* SQL funct
35941 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78 74  ion call context
35942 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
35943 20 2a 7a 46 75 6e 63 2c 20 20 20 20 20 20 20 20   *zFunc,        
35944 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f        /* Functio
35945 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71 6c 69  n name */.  sqli
35946 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c  te3_value *pVal,
35947 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61              /* a
35948 72 67 76 5b 30 5d 20 70 61 73 73 65 64 20 74 6f  rgv[0] passed to
35949 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 46   function */.  F
3594a 74 73 33 43 75 72 73 6f 72 20 2a 2a 70 70 43 73  ts3Cursor **ppCs
3594b 72 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  r         /* OUT
3594c 3a 20 53 74 6f 72 65 20 63 75 72 73 6f 72 20 68  : Store cursor h
3594d 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 29 7b  andle here */.){
3594e 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70  .  Fts3Cursor *p
3594f 52 65 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  Ret;.  if( sqlit
35950 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 56  e3_value_type(pV
35951 61 6c 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42  al)!=SQLITE_BLOB
35952 20 0a 20 20 20 26 26 20 73 71 6c 69 74 65 33 5f   .   && sqlite3_
35953 76 61 6c 75 65 5f 62 79 74 65 73 28 70 56 61 6c  value_bytes(pVal
35954 29 21 3d 73 69 7a 65 6f 66 28 46 74 73 33 43 75  )!=sizeof(Fts3Cu
35955 72 73 6f 72 20 2a 29 0a 20 20 29 7b 0a 20 20 20  rsor *).  ){.   
35956 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 73 71   char *zErr = sq
35957 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 69  lite3_mprintf("i
35958 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72 67  llegal first arg
35959 75 6d 65 6e 74 20 74 6f 20 25 73 22 2c 20 7a 46  ument to %s", zF
3595a 75 6e 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  unc);.    sqlite
3595b 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
3595c 43 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  Context, zErr, -
3595d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  1);.    sqlite3_
3595e 66 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20  free(zErr);.    
3595f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
35960 52 4f 52 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70  ROR;.  }.  memcp
35961 79 28 26 70 52 65 74 2c 20 73 71 6c 69 74 65 33  y(&pRet, sqlite3
35962 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 70 56 61 6c  _value_blob(pVal
35963 29 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 43 75  ), sizeof(Fts3Cu
35964 72 73 6f 72 20 2a 29 29 3b 0a 20 20 2a 70 70 43  rsor *));.  *ppC
35965 73 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65 74  sr = pRet;.  ret
35966 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
35967 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
35968 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 6e  tation of the sn
35969 69 70 70 65 74 28 29 20 66 75 6e 63 74 69 6f 6e  ippet() function
3596a 20 66 6f 72 20 46 54 53 33 0a 2a 2f 0a 73 74 61   for FTS3.*/.sta
3596b 74 69 63 20 76 6f 69 64 20 66 74 73 33 53 6e 69  tic void fts3Sni
3596c 70 70 65 74 46 75 6e 63 28 0a 20 20 73 71 6c 69  ppetFunc(.  sqli
3596d 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
3596e 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
3596f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
35970 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 46  ue **argv.){.  F
35971 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 3b  ts3Cursor *pCsr;
35972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35973 2a 20 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20  * Cursor handle 
35974 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 61  passed through a
35975 70 56 61 6c 5b 30 5d 20 2a 2f 0a 20 20 63 6f 6e  pVal[0] */.  con
35976 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20  st char *zStart 
35977 3d 20 22 3c 62 3e 22 3b 0a 20 20 63 6f 6e 73 74  = "<b>";.  const
35978 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 22 3c   char *zEnd = "<
35979 2f 62 3e 22 3b 0a 20 20 63 6f 6e 73 74 20 63 68  /b>";.  const ch
3597a 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20 3d 20  ar *zEllipsis = 
3597b 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b 0a 0a 20  "<b>...</b>";.. 
3597c 20 69 66 28 20 61 72 67 63 3c 31 20 7c 7c 20 61   if( argc<1 || a
3597d 72 67 63 3e 34 20 29 20 72 65 74 75 72 6e 3b 0a  rgc>4 ) return;.
3597e 20 20 69 66 28 20 66 74 73 33 46 75 6e 63 74 69    if( fts3Functi
3597f 6f 6e 41 72 67 28 70 43 6f 6e 74 65 78 74 2c 20  onArg(pContext, 
35980 22 73 6e 69 70 70 65 74 22 2c 20 61 72 67 76 5b  "snippet", argv[
35981 30 5d 2c 20 26 70 43 73 72 29 20 29 20 72 65 74  0], &pCsr) ) ret
35982 75 72 6e 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  urn;..  switch( 
35983 61 72 67 63 20 29 7b 0a 20 20 20 20 63 61 73 65  argc ){.    case
35984 20 34 3a 20 7a 45 6c 6c 69 70 73 69 73 20 3d 20   4: zEllipsis = 
35985 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c  (const char*)sql
35986 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
35987 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 63 61  argv[3]);.    ca
35988 73 65 20 33 3a 20 7a 45 6e 64 20 3d 20 28 63 6f  se 3: zEnd = (co
35989 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
3598a 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
3598b 76 5b 32 5d 29 3b 0a 20 20 20 20 63 61 73 65 20  v[2]);.    case 
3598c 32 3a 20 7a 53 74 61 72 74 20 3d 20 28 63 6f 6e  2: zStart = (con
3598d 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33  st char*)sqlite3
3598e 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
3598f 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  [1]);.  }..  sql
35990 69 74 65 33 46 74 73 33 53 6e 69 70 70 65 74 28  ite3Fts3Snippet(
35991 70 43 6f 6e 74 65 78 74 2c 20 70 43 73 72 2c 20  pContext, pCsr, 
35992 7a 53 74 61 72 74 2c 20 7a 45 6e 64 2c 20 7a 45  zStart, zEnd, zE
35993 6c 6c 69 70 73 69 73 29 3b 0a 7d 0a 0a 2f 2a 0a  llipsis);.}../*.
35994 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
35995 6e 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 73  n of the offsets
35996 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  () function for 
35997 46 54 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76  FTS3.*/.static v
35998 6f 69 64 20 66 74 73 33 4f 66 66 73 65 74 73 46  oid fts3OffsetsF
35999 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
3599a 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74  ontext *pContext
3599b 2c 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65  ,      /* SQLite
3599c 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 63   function call c
3599d 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
3599e 6e 56 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20  nVal,           
3599f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
359a0 69 7a 65 20 6f 66 20 61 72 67 75 6d 65 6e 74 20  ize of argument 
359a1 61 72 72 61 79 20 2a 2f 0a 20 20 73 71 6c 69 74  array */.  sqlit
359a2 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
359a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
359a4 72 61 79 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ray of arguments
359a5 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 43 75 72   */.){.  Fts3Cur
359a6 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20 20  sor *pCsr;      
359a7 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
359a8 6f 72 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64  or handle passed
359a9 20 74 68 72 6f 75 67 68 20 61 70 56 61 6c 5b 30   through apVal[0
359aa 5d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  ] */..  assert( 
359ab 6e 56 61 6c 3d 3d 31 20 29 3b 0a 20 20 69 66 28  nVal==1 );.  if(
359ac 20 66 74 73 33 46 75 6e 63 74 69 6f 6e 41 72 67   fts3FunctionArg
359ad 28 70 43 6f 6e 74 65 78 74 2c 20 22 6f 66 66 73  (pContext, "offs
359ae 65 74 73 22 2c 20 61 70 56 61 6c 5b 30 5d 2c 20  ets", apVal[0], 
359af 26 70 43 73 72 29 20 29 20 72 65 74 75 72 6e 3b  &pCsr) ) return;
359b0 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 20  .  assert( pCsr 
359b1 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33  );.  sqlite3Fts3
359b2 4f 66 66 73 65 74 73 28 70 43 6f 6e 74 65 78 74  Offsets(pContext
359b3 2c 20 70 43 73 72 29 3b 0a 7d 0a 0a 2f 2a 20 0a  , pCsr);.}../* .
359b4 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
359b5 6e 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c  n of the special
359b6 20 6f 70 74 69 6d 69 7a 65 28 29 20 66 75 6e 63   optimize() func
359b7 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 2e 20 54  tion for FTS3. T
359b8 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his .** function
359b9 20 6d 65 72 67 65 73 20 61 6c 6c 20 73 65 67 6d   merges all segm
359ba 65 6e 74 73 20 69 6e 20 74 68 65 20 64 61 74 61  ents in the data
359bb 62 61 73 65 20 74 6f 20 61 20 73 69 6e 67 6c 65  base to a single
359bc 20 73 65 67 6d 65 6e 74 2e 0a 2a 2a 20 45 78 61   segment..** Exa
359bd 6d 70 6c 65 20 75 73 61 67 65 20 69 73 3a 0a 2a  mple usage is:.*
359be 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 6f 70  *.**   SELECT op
359bf 74 69 6d 69 7a 65 28 74 29 20 46 52 4f 4d 20 74  timize(t) FROM t
359c0 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a 0a 2a 2a 20   LIMIT 1;.**.** 
359c1 77 68 65 72 65 20 27 74 27 20 69 73 20 74 68 65  where 't' is the
359c2 20 6e 61 6d 65 20 6f 66 20 61 6e 20 46 54 53 33   name of an FTS3
359c3 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
359c4 63 20 76 6f 69 64 20 66 74 73 33 4f 70 74 69 6d  c void fts3Optim
359c5 69 7a 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  izeFunc(.  sqlit
359c6 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  e3_context *pCon
359c7 74 65 78 74 2c 20 20 20 20 20 20 2f 2a 20 53 51  text,      /* SQ
359c8 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 20 63 61  Lite function ca
359c9 6c 6c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ll context */.  
359ca 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20 20 20  int nVal,       
359cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359cc 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 67 75 6d  /* Size of argum
359cd 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  ent array */.  s
359ce 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
359cf 70 56 61 6c 20 20 20 20 20 20 20 20 20 20 20 2f  pVal           /
359d0 2a 20 41 72 72 61 79 20 6f 66 20 61 72 67 75 6d  * Array of argum
359d1 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ents */.){.  int
359d2 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
359d3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
359d4 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
359d5 20 46 74 73 33 54 61 62 6c 65 20 2a 70 3b 20 20   Fts3Table *p;  
359d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359d7 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
359d8 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74  e handle */.  Ft
359d9 73 33 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  s3Cursor *pCurso
359da 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
359db 20 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 70   Cursor handle p
359dc 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 61 70  assed through ap
359dd 56 61 6c 5b 30 5d 20 2a 2f 0a 0a 20 20 61 73 73  Val[0] */..  ass
359de 65 72 74 28 20 6e 56 61 6c 3d 3d 31 20 29 3b 0a  ert( nVal==1 );.
359df 20 20 69 66 28 20 66 74 73 33 46 75 6e 63 74 69    if( fts3Functi
359e0 6f 6e 41 72 67 28 70 43 6f 6e 74 65 78 74 2c 20  onArg(pContext, 
359e1 22 6f 70 74 69 6d 69 7a 65 22 2c 20 61 70 56 61  "optimize", apVa
359e2 6c 5b 30 5d 2c 20 26 70 43 75 72 73 6f 72 29 20  l[0], &pCursor) 
359e3 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 20 3d 20  ) return;.  p = 
359e4 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 75  (Fts3Table *)pCu
359e5 72 73 6f 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  rsor->base.pVtab
359e6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b  ;.  assert( p );
359e7 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
359e8 46 74 73 33 4f 70 74 69 6d 69 7a 65 28 70 29 3b  Fts3Optimize(p);
359e9 0a 0a 20 20 73 77 69 74 63 68 28 20 72 63 20 29  ..  switch( rc )
359ea 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
359eb 45 5f 4f 4b 3a 0a 20 20 20 20 20 20 73 71 6c 69  E_OK:.      sqli
359ec 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
359ed 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65 78  pContext, "Index
359ee 20 6f 70 74 69 6d 69 7a 65 64 22 2c 20 2d 31 2c   optimized", -1,
359ef 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
359f0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
359f1 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 4f    case SQLITE_DO
359f2 4e 45 3a 0a 20 20 20 20 20 20 73 71 6c 69 74 65  NE:.      sqlite
359f3 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
359f4 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65 78 20 61  ontext, "Index a
359f5 6c 72 65 61 64 79 20 6f 70 74 69 6d 61 6c 22 2c  lready optimal",
359f6 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54   -1, SQLITE_STAT
359f7 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  IC);.      break
359f8 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20  ;.    default:. 
359f9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
359fa 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 70  ult_error_code(p
359fb 43 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a 20 20  Context, rc);.  
359fc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 7d      break;.  }.}
359fd 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
359fe 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  tine implements 
359ff 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f  the xFindFunctio
35a00 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65  n method for the
35a01 20 46 54 53 33 0a 2a 2a 20 76 69 72 74 75 61 6c   FTS3.** virtual
35a02 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
35a03 63 20 69 6e 74 20 66 74 73 33 46 69 6e 64 46 75  c int fts3FindFu
35a04 6e 63 74 69 6f 6e 4d 65 74 68 6f 64 28 0a 20 20  nctionMethod(.  
35a05 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
35a06 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  tab,            
35a07 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
35a08 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74   handle */.  int
35a09 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20 20   nArg,          
35a0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35a0b 4e 75 6d 62 65 72 20 6f 66 20 53 51 4c 20 66 75  Number of SQL fu
35a0c 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73  nction arguments
35a0d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
35a0e 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
35a0f 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
35a10 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   SQL function */
35a11 0a 20 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e  .  void (**pxFun
35a12 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
35a13 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
35a14 76 61 6c 75 65 2a 2a 29 2c 20 2f 2a 20 4f 55 54  value**), /* OUT
35a15 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 20 20 76 6f  : Result */.  vo
35a16 69 64 20 2a 2a 70 70 41 72 67 20 20 20 20 20 20  id **ppArg      
35a17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35a18 20 55 6e 75 73 65 64 20 2a 2f 0a 29 7b 0a 20 20   Unused */.){.  
35a19 73 74 72 75 63 74 20 4f 76 65 72 6c 6f 61 64 65  struct Overloade
35a1a 64 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  d {.    const ch
35a1b 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 76  ar *zName;.    v
35a1c 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
35a1d 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
35a1e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
35a1f 2a 29 3b 0a 20 20 7d 20 61 4f 76 65 72 6c 6f 61  *);.  } aOverloa
35a20 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 73  d[] = {.    { "s
35a21 6e 69 70 70 65 74 22 2c 20 66 74 73 33 53 6e 69  nippet", fts3Sni
35a22 70 70 65 74 46 75 6e 63 20 7d 2c 0a 20 20 20 20  ppetFunc },.    
35a23 7b 20 22 6f 66 66 73 65 74 73 22 2c 20 66 74 73  { "offsets", fts
35a24 33 4f 66 66 73 65 74 73 46 75 6e 63 20 7d 2c 0a  3OffsetsFunc },.
35a25 20 20 20 20 7b 20 22 6f 70 74 69 6d 69 7a 65 22      { "optimize"
35a26 2c 20 66 74 73 33 4f 70 74 69 6d 69 7a 65 46 75  , fts3OptimizeFu
35a27 6e 63 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74  nc },.  };.  int
35a28 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
35a29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35a2a 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
35a2b 65 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  e */.  for(i=0; 
35a2c 69 3c 53 69 7a 65 6f 66 41 72 72 61 79 28 61 4f  i<SizeofArray(aO
35a2d 76 65 72 6c 6f 61 64 29 3b 20 69 2b 2b 29 7b 0a  verload); i++){.
35a2e 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
35a2f 4e 61 6d 65 2c 20 61 4f 76 65 72 6c 6f 61 64 5b  Name, aOverload[
35a30 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  i].zName)==0 ){.
35a31 20 20 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20        *pxFunc = 
35a32 61 4f 76 65 72 6c 6f 61 64 5b 69 5d 2e 78 46 75  aOverload[i].xFu
35a33 6e 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  nc;.      return
35a34 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   1;.    }.  }.. 
35a35 20 2f 2a 20 4e 6f 20 66 75 6e 63 74 69 6f 6e 20   /* No function 
35a36 6f 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64  of the specified
35a37 20 6e 61 6d 65 20 77 61 73 20 66 6f 75 6e 64 2e   name was found.
35a38 20 52 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20   Return 0. */.  
35a39 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
35a3a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
35a3b 6e 20 6f 66 20 46 54 53 33 20 78 52 65 6e 61 6d  n of FTS3 xRenam
35a3c 65 20 6d 65 74 68 6f 64 2e 20 52 65 6e 61 6d 65  e method. Rename
35a3d 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 2e 0a   an fts3 table..
35a3e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
35a3f 73 33 52 65 6e 61 6d 65 4d 65 74 68 6f 64 28 0a  s3RenameMethod(.
35a40 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
35a41 70 56 74 61 62 2c 20 20 20 20 20 20 20 20 20 20  pVtab,          
35a42 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
35a43 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  le handle */.  c
35a44 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
35a45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35a46 2a 20 4e 65 77 20 6e 61 6d 65 20 6f 66 20 74 61  * New name of ta
35a47 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33  ble */.){.  Fts3
35a48 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33  Table *p = (Fts3
35a49 54 61 62 6c 65 20 2a 29 70 56 74 61 62 3b 20 20  Table *)pVtab;  
35a4a 20 20 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 53     .  int rc = S
35a4b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 20 20 20 20  QLITE_NOMEM;    
35a4c 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
35a4d 43 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  Code */.  char *
35a4e 7a 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20  zSql;           
35a4f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c            /* SQL
35a50 20 73 63 72 69 70 74 20 74 6f 20 72 75 6e 20 74   script to run t
35a51 6f 20 72 65 6e 61 6d 65 20 74 61 62 6c 65 73 20  o rename tables 
35a52 2a 2f 0a 20 0a 20 20 7a 53 71 6c 20 3d 20 73 71  */. .  zSql = sq
35a53 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20  lite3_mprintf(. 
35a54 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20     "ALTER TABLE 
35a55 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20  %Q.'%q_content' 
35a56 20 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 63   RENAME TO '%q_c
35a57 6f 6e 74 65 6e 74 27 3b 22 0a 20 20 20 20 22 41  ontent';".    "A
35a58 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
35a59 71 5f 73 65 67 6d 65 6e 74 73 27 20 52 45 4e 41  q_segments' RENA
35a5a 4d 45 20 54 4f 20 27 25 71 5f 73 65 67 6d 65 6e  ME TO '%q_segmen
35a5b 74 73 27 3b 22 0a 20 20 20 20 22 41 4c 54 45 52  ts';".    "ALTER
35a5c 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73 65   TABLE %Q.'%q_se
35a5d 67 64 69 72 27 20 20 20 52 45 4e 41 4d 45 20 54  gdir'   RENAME T
35a5e 4f 20 27 25 71 5f 73 65 67 64 69 72 27 3b 22 0a  O '%q_segdir';".
35a5f 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d      , p->zDb, p-
35a60 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20  >zName, zName . 
35a61 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e     , p->zDb, p->
35a62 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20 20  zName, zName .  
35a63 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a    , p->zDb, p->z
35a64 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a 20 20 29 3b  Name, zName.  );
35a65 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20  .  if( zSql ){. 
35a66 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
35a67 65 78 65 63 28 70 2d 3e 64 62 2c 20 7a 53 71 6c  exec(p->db, zSql
35a68 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
35a69 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
35a6a 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
35a6b 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63   rc;.}..static c
35a6c 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
35a6d 75 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65 20 3d  ule fts3Module =
35a6e 20 7b 0a 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e   {.  /* iVersion
35a6f 20 20 20 20 20 20 2a 2f 20 30 2c 0a 20 20 2f 2a        */ 0,.  /*
35a70 20 78 43 72 65 61 74 65 20 20 20 20 20 20 20 2a   xCreate       *
35a71 2f 20 66 74 73 33 43 72 65 61 74 65 4d 65 74 68  / fts3CreateMeth
35a72 6f 64 2c 0a 20 20 2f 2a 20 78 43 6f 6e 6e 65 63  od,.  /* xConnec
35a73 74 20 20 20 20 20 20 2a 2f 20 66 74 73 33 43 6f  t      */ fts3Co
35a74 6e 6e 65 63 74 4d 65 74 68 6f 64 2c 0a 20 20 2f  nnectMethod,.  /
35a75 2a 20 78 42 65 73 74 49 6e 64 65 78 20 20 20 20  * xBestIndex    
35a76 2a 2f 20 66 74 73 33 42 65 73 74 49 6e 64 65 78  */ fts3BestIndex
35a77 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 44 69  Method,.  /* xDi
35a78 73 63 6f 6e 6e 65 63 74 20 20 20 2a 2f 20 66 74  sconnect   */ ft
35a79 73 33 44 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68  s3DisconnectMeth
35a7a 6f 64 2c 0a 20 20 2f 2a 20 78 44 65 73 74 72 6f  od,.  /* xDestro
35a7b 79 20 20 20 20 20 20 2a 2f 20 66 74 73 33 44 65  y      */ fts3De
35a7c 73 74 72 6f 79 4d 65 74 68 6f 64 2c 0a 20 20 2f  stroyMethod,.  /
35a7d 2a 20 78 4f 70 65 6e 20 20 20 20 20 20 20 20 20  * xOpen         
35a7e 2a 2f 20 66 74 73 33 4f 70 65 6e 4d 65 74 68 6f  */ fts3OpenMetho
35a7f 64 2c 0a 20 20 2f 2a 20 78 43 6c 6f 73 65 20 20  d,.  /* xClose  
35a80 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
35a81 74 43 6c 6f 73 65 2c 0a 20 20 2f 2a 20 78 46 69  tClose,.  /* xFi
35a82 6c 74 65 72 20 20 20 20 20 20 20 2a 2f 20 66 74  lter       */ ft
35a83 73 33 46 69 6c 74 65 72 4d 65 74 68 6f 64 2c 0a  s3FilterMethod,.
35a84 20 20 2f 2a 20 78 4e 65 78 74 20 20 20 20 20 20    /* xNext      
35a85 20 20 20 2a 2f 20 66 74 73 33 4e 65 78 74 4d 65     */ fts3NextMe
35a86 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 45 6f 66 20  thod,.  /* xEof 
35a87 20 20 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33           */ fts3
35a88 45 6f 66 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20  EofMethod,.  /* 
35a89 78 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 2a 2f  xColumn       */
35a8a 20 66 74 73 33 43 6f 6c 75 6d 6e 4d 65 74 68 6f   fts3ColumnMetho
35a8b 64 2c 0a 20 20 2f 2a 20 78 52 6f 77 69 64 20 20  d,.  /* xRowid  
35a8c 20 20 20 20 20 20 2a 2f 20 66 74 73 33 52 6f 77        */ fts3Row
35a8d 69 64 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78  idMethod,.  /* x
35a8e 55 70 64 61 74 65 20 20 20 20 20 20 20 2a 2f 20  Update       */ 
35a8f 66 74 73 33 55 70 64 61 74 65 4d 65 74 68 6f 64  fts3UpdateMethod
35a90 2c 0a 20 20 2f 2a 20 78 42 65 67 69 6e 20 20 20  ,.  /* xBegin   
35a91 20 20 20 20 20 2a 2f 20 66 74 73 33 42 65 67 69       */ fts3Begi
35a92 6e 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 53  nMethod,.  /* xS
35a93 79 6e 63 20 20 20 20 20 20 20 20 20 2a 2f 20 66  ync         */ f
35a94 74 73 33 53 79 6e 63 4d 65 74 68 6f 64 2c 0a 20  ts3SyncMethod,. 
35a95 20 2f 2a 20 78 43 6f 6d 6d 69 74 20 20 20 20 20   /* xCommit     
35a96 20 20 2a 2f 20 66 74 73 33 43 6f 6d 6d 69 74 4d    */ fts3CommitM
35a97 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 52 6f 6c  ethod,.  /* xRol
35a98 6c 62 61 63 6b 20 20 20 20 20 2a 2f 20 66 74 73  lback     */ fts
35a99 33 52 6f 6c 6c 62 61 63 6b 4d 65 74 68 6f 64 2c  3RollbackMethod,
35a9a 0a 20 20 2f 2a 20 78 46 69 6e 64 46 75 6e 63 74  .  /* xFindFunct
35a9b 69 6f 6e 20 2a 2f 20 66 74 73 33 46 69 6e 64 46  ion */ fts3FindF
35a9c 75 6e 63 74 69 6f 6e 4d 65 74 68 6f 64 2c 0a 20  unctionMethod,. 
35a9d 20 2f 2a 20 78 52 65 6e 61 6d 65 20 2a 2f 20 20   /* xRename */  
35a9e 20 20 20 20 20 66 74 73 33 52 65 6e 61 6d 65 4d       fts3RenameM
35a9f 65 74 68 6f 64 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ethod,.};../*.**
35aa0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
35aa1 73 20 72 65 67 69 73 74 65 72 65 64 20 61 73 20  s registered as 
35aa2 74 68 65 20 6d 6f 64 75 6c 65 20 64 65 73 74 72  the module destr
35aa3 75 63 74 6f 72 20 28 63 61 6c 6c 65 64 20 77 68  uctor (called wh
35aa4 65 6e 20 61 6e 0a 2a 2a 20 46 54 53 33 20 65 6e  en an.** FTS3 en
35aa5 61 62 6c 65 64 20 64 61 74 61 62 61 73 65 20 63  abled database c
35aa6 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
35aa7 73 65 64 29 2e 20 49 74 20 66 72 65 65 73 20 74  sed). It frees t
35aa8 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c  he memory.** all
35aa9 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 74  ocated for the t
35aaa 6f 6b 65 6e 69 7a 65 72 20 68 61 73 68 20 74 61  okenizer hash ta
35aab 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
35aac 6f 69 64 20 68 61 73 68 44 65 73 74 72 6f 79 28  oid hashDestroy(
35aad 76 6f 69 64 20 2a 70 29 7b 0a 20 20 46 74 73 33  void *p){.  Fts3
35aae 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 28 46  Hash *pHash = (F
35aaf 74 73 33 48 61 73 68 20 2a 29 70 3b 0a 20 20 73  ts3Hash *)p;.  s
35ab0 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
35ab1 65 61 72 28 70 48 61 73 68 29 3b 0a 20 20 73 71  ear(pHash);.  sq
35ab2 6c 69 74 65 33 5f 66 72 65 65 28 70 48 61 73 68  lite3_free(pHash
35ab3 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
35ab4 66 74 73 33 20 62 75 69 6c 74 2d 69 6e 20 74 6f  fts3 built-in to
35ab5 6b 65 6e 69 7a 65 72 73 20 2d 20 22 73 69 6d 70  kenizers - "simp
35ab6 6c 65 22 20 61 6e 64 20 22 70 6f 72 74 65 72 22  le" and "porter"
35ab7 20 2d 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74   - are implement
35ab8 65 64 0a 2a 2a 20 69 6e 20 66 69 6c 65 73 20 66  ed.** in files f
35ab9 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63  ts3_tokenizer1.c
35aba 20 61 6e 64 20 66 74 73 33 5f 70 6f 72 74 65 72   and fts3_porter
35abb 2e 63 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  .c respectively.
35abc 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   The following.*
35abd 2a 20 74 77 6f 20 66 6f 72 77 61 72 64 20 64 65  * two forward de
35abe 63 6c 61 72 61 74 69 6f 6e 73 20 61 72 65 20 66  clarations are f
35abf 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 64 65 63  or functions dec
35ac0 6c 61 72 65 64 20 69 6e 20 74 68 65 73 65 20 66  lared in these f
35ac1 69 6c 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  iles.** used to 
35ac2 72 65 74 72 69 65 76 65 20 74 68 65 20 72 65 73  retrieve the res
35ac3 70 65 63 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e  pective implemen
35ac4 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43  tations..**.** C
35ac5 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 46 74  alling sqlite3Ft
35ac6 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65  s3SimpleTokenize
35ac7 72 4d 6f 64 75 6c 65 28 29 20 73 65 74 73 20 74  rModule() sets t
35ac8 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  he value pointed
35ac9 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 61 72  .** to by the ar
35aca 67 75 6d 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20  gument to point 
35acb 61 20 74 68 65 20 22 73 69 6d 70 6c 65 22 20 74  a the "simple" t
35acc 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65  okenizer impleme
35acd 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 46 75 6e 63  ntation..** Func
35ace 74 69 6f 6e 20 2e 2e 2e 50 6f 72 74 65 72 54 6f  tion ...PorterTo
35acf 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20  kenizerModule() 
35ad0 73 65 74 73 20 2a 70 4d 6f 64 75 6c 65 20 74 6f  sets *pModule to
35ad1 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a   point to the.**
35ad2 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65   porter tokenize
35ad3 72 2f 73 74 65 6d 6d 65 72 20 69 6d 70 6c 65 6d  r/stemmer implem
35ad4 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  entation..*/.SQL
35ad5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
35ad6 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70   sqlite3Fts3Simp
35ad7 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
35ad8 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
35ad9 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
35ada 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c  **ppModule);.SQL
35adb 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
35adc 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74   sqlite3Fts3Port
35add 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  erTokenizerModul
35ade 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
35adf 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
35ae0 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c  **ppModule);.SQL
35ae1 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
35ae2 20 73 71 6c 69 74 65 33 46 74 73 33 49 63 75 54   sqlite3Fts3IcuT
35ae3 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73  okenizerModule(s
35ae4 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
35ae5 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
35ae6 70 4d 6f 64 75 6c 65 29 3b 0a 0a 2f 2a 0a 2a 2a  pModule);../*.**
35ae7 20 49 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20   Initialise the 
35ae8 66 74 73 33 20 65 78 74 65 6e 73 69 6f 6e 2e 20  fts3 extension. 
35ae9 49 66 20 74 68 69 73 20 65 78 74 65 6e 73 69 6f  If this extensio
35aea 6e 20 69 73 20 62 75 69 6c 74 20 61 73 20 70 61  n is built as pa
35aeb 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c  rt.** of the sql
35aec 69 74 65 20 6c 69 62 72 61 72 79 2c 20 74 68 65  ite library, the
35aed 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
35aee 69 73 20 63 61 6c 6c 65 64 20 64 69 72 65 63 74  is called direct
35aef 6c 79 20 62 79 0a 2a 2a 20 53 51 4c 69 74 65 2e  ly by.** SQLite.
35af0 20 49 66 20 66 74 73 33 20 69 73 20 62 75 69 6c   If fts3 is buil
35af1 74 20 61 73 20 61 20 64 79 6e 61 6d 69 63 61 6c  t as a dynamical
35af2 6c 79 20 6c 6f 61 64 61 62 6c 65 20 65 78 74 65  ly loadable exte
35af3 6e 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 66  nsion, this.** f
35af4 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
35af5 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  d by the sqlite3
35af6 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28  _extension_init(
35af7 29 20 65 6e 74 72 79 20 70 6f 69 6e 74 2e 0a 2a  ) entry point..*
35af8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
35af9 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
35afa 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Init(sqlite3 *db
35afb 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
35afc 4c 49 54 45 5f 4f 4b 3b 0a 20 20 46 74 73 33 48  LITE_OK;.  Fts3H
35afd 61 73 68 20 2a 70 48 61 73 68 20 3d 20 30 3b 0a  ash *pHash = 0;.
35afe 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
35aff 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
35b00 20 2a 70 53 69 6d 70 6c 65 20 3d 20 30 3b 0a 20   *pSimple = 0;. 
35b01 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
35b02 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
35b03 2a 70 50 6f 72 74 65 72 20 3d 20 30 3b 0a 20 20  *pPorter = 0;.  
35b04 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
35b05 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
35b06 70 49 63 75 20 3d 20 30 3b 0a 0a 20 20 73 71 6c  pIcu = 0;..  sql
35b07 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f  ite3Fts3SimpleTo
35b08 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70  kenizerModule(&p
35b09 53 69 6d 70 6c 65 29 3b 0a 20 20 73 71 6c 69 74  Simple);.  sqlit
35b0a 65 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65  e3Fts3PorterToke
35b0b 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 50 6f  nizerModule(&pPo
35b0c 72 74 65 72 29 3b 0a 23 69 66 64 65 66 20 53 51  rter);.#ifdef SQ
35b0d 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a  LITE_ENABLE_ICU.
35b0e 20 20 73 71 6c 69 74 65 33 46 74 73 33 49 63 75    sqlite3Fts3Icu
35b0f 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
35b10 26 70 49 63 75 29 3b 0a 23 65 6e 64 69 66 0a 0a  &pIcu);.#endif..
35b11 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e    /* Allocate an
35b12 64 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65  d initialise the
35b13 20 68 61 73 68 2d 74 61 62 6c 65 20 75 73 65 64   hash-table used
35b14 20 74 6f 20 73 74 6f 72 65 20 74 6f 6b 65 6e 69   to store tokeni
35b15 7a 65 72 73 2e 20 2a 2f 0a 20 20 70 48 61 73 68  zers. */.  pHash
35b16 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
35b17 63 28 73 69 7a 65 6f 66 28 46 74 73 33 48 61 73  c(sizeof(Fts3Has
35b18 68 29 29 3b 0a 20 20 69 66 28 20 21 70 48 61 73  h));.  if( !pHas
35b19 68 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  h ){.    rc = SQ
35b1a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
35b1b 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
35b1c 46 74 73 33 48 61 73 68 49 6e 69 74 28 70 48 61  Fts3HashInit(pHa
35b1d 73 68 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54  sh, FTS3_HASH_ST
35b1e 52 49 4e 47 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20  RING, 1);.  }.. 
35b1f 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 62 75 69   /* Load the bui
35b20 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a 65 72 73  lt-in tokenizers
35b21 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74   into the hash t
35b22 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 72 63  able */.  if( rc
35b23 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
35b24 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 74     if( sqlite3Ft
35b25 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  s3HashInsert(pHa
35b26 73 68 2c 20 22 73 69 6d 70 6c 65 22 2c 20 37 2c  sh, "simple", 7,
35b27 20 28 76 6f 69 64 20 2a 29 70 53 69 6d 70 6c 65   (void *)pSimple
35b28 29 0a 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65  ).     || sqlite
35b29 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28  3Fts3HashInsert(
35b2a 70 48 61 73 68 2c 20 22 70 6f 72 74 65 72 22 2c  pHash, "porter",
35b2b 20 37 2c 20 28 76 6f 69 64 20 2a 29 70 50 6f 72   7, (void *)pPor
35b2c 74 65 72 29 20 0a 20 20 20 20 20 7c 7c 20 28 70  ter) .     || (p
35b2d 49 63 75 20 26 26 20 73 71 6c 69 74 65 33 46 74  Icu && sqlite3Ft
35b2e 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  s3HashInsert(pHa
35b2f 73 68 2c 20 22 69 63 75 22 2c 20 34 2c 20 28 76  sh, "icu", 4, (v
35b30 6f 69 64 20 2a 29 70 49 63 75 29 29 0a 20 20 20  oid *)pIcu)).   
35b31 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
35b32 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
35b33 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53   }.  }..#ifdef S
35b34 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c  QLITE_TEST.  sql
35b35 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69 74  ite3Fts3ExprInit
35b36 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 64 62  TestInterface(db
35b37 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
35b38 43 72 65 61 74 65 20 74 68 65 20 76 69 72 74 75  Create the virtu
35b39 61 6c 20 74 61 62 6c 65 20 77 72 61 70 70 65 72  al table wrapper
35b3a 20 61 72 6f 75 6e 64 20 74 68 65 20 68 61 73 68   around the hash
35b3b 2d 74 61 62 6c 65 20 61 6e 64 20 6f 76 65 72 6c  -table and overl
35b3c 6f 61 64 20 0a 20 20 2a 2a 20 74 68 65 20 74 77  oad .  ** the tw
35b3d 6f 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  o scalar functio
35b3e 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 73  ns. If this is s
35b3f 75 63 63 65 73 73 66 75 6c 2c 20 72 65 67 69 73  uccessful, regis
35b40 74 65 72 20 74 68 65 0a 20 20 2a 2a 20 6d 6f 64  ter the.  ** mod
35b41 75 6c 65 20 77 69 74 68 20 73 71 6c 69 74 65 2e  ule with sqlite.
35b42 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49  .  */.  if( SQLI
35b43 54 45 5f 4f 4b 3d 3d 72 63 20 0a 20 20 20 26 26  TE_OK==rc .   &&
35b44 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
35b45 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
35b46 74 48 61 73 68 54 61 62 6c 65 28 64 62 2c 20 70  tHashTable(db, p
35b47 48 61 73 68 2c 20 22 66 74 73 33 5f 74 6f 6b 65  Hash, "fts3_toke
35b48 6e 69 7a 65 72 22 29 29 0a 20 20 20 26 26 20 53  nizer")).   && S
35b49 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
35b4a 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
35b4b 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 73  _function(db, "s
35b4c 6e 69 70 70 65 74 22 2c 20 2d 31 29 29 0a 20 20  nippet", -1)).  
35b4d 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
35b4e 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65  rc = sqlite3_ove
35b4f 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64  rload_function(d
35b50 62 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 31 29  b, "offsets", 1)
35b51 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f  ).   && SQLITE_O
35b52 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
35b53 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
35b54 6f 6e 28 64 62 2c 20 22 6f 70 74 69 6d 69 7a 65  on(db, "optimize
35b55 22 2c 20 31 29 29 0a 20 20 29 7b 0a 20 20 20 20  ", 1)).  ){.    
35b56 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63  return sqlite3_c
35b57 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28  reate_module_v2(
35b58 0a 20 20 20 20 20 20 20 20 64 62 2c 20 22 66 74  .        db, "ft
35b59 73 33 22 2c 20 26 66 74 73 33 4d 6f 64 75 6c 65  s3", &fts3Module
35b5a 2c 20 28 76 6f 69 64 20 2a 29 70 48 61 73 68 2c  , (void *)pHash,
35b5b 20 68 61 73 68 44 65 73 74 72 6f 79 0a 20 20 20   hashDestroy.   
35b5c 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e   );.  }..  /* An
35b5d 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
35b5e 72 65 64 2e 20 44 65 6c 65 74 65 20 74 68 65 20  red. Delete the 
35b5f 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 72  hash table and r
35b60 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20  eturn the error 
35b61 63 6f 64 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72  code. */.  asser
35b62 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
35b63 20 29 3b 0a 20 20 69 66 28 20 70 48 61 73 68 20   );.  if( pHash 
35b64 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  ){.    sqlite3Ft
35b65 73 33 48 61 73 68 43 6c 65 61 72 28 70 48 61 73  s3HashClear(pHas
35b66 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  h);.    sqlite3_
35b67 66 72 65 65 28 70 48 61 73 68 29 3b 0a 20 20 7d  free(pHash);.  }
35b68 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
35b69 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52  .#if !SQLITE_COR
35b6a 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  E.SQLITE_API int
35b6b 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69   sqlite3_extensi
35b6c 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74  on_init(.  sqlit
35b6d 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20  e3 *db, .  char 
35b6e 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f  **pzErrMsg,.  co
35b6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
35b70 72 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29  routines *pApi.)
35b71 7b 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  {.  SQLITE_EXTEN
35b72 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29  SION_INIT2(pApi)
35b73 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
35b74 33 46 74 73 33 49 6e 69 74 28 64 62 29 3b 0a 7d  3Fts3Init(db);.}
35b75 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a  .#endif..#endif.
35b76 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
35b77 20 45 6e 64 20 6f 66 20 66 74 73 33 2e 63 20 2a   End of fts3.c *
35b78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
35b7b 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
35b7c 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
35b7d 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.c ********
35b7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
35b80 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20  ./*.** 2008 Nov 
35b81 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  28.**.** The aut
35b82 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
35b83 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
35b84 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
35b85 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
35b86 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
35b87 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
35b88 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
35b89 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
35b8a 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
35b8b 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
35b8c 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
35b8d 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
35b8e 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
35b8f 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
35b90 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
35b91 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
35b92 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
35b93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b97 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
35b98 73 20 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e  s module contain
35b99 73 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c  s code that impl
35b9a 65 6d 65 6e 74 73 20 61 20 70 61 72 73 65 72 20  ements a parser 
35b9b 66 6f 72 20 66 74 73 33 20 71 75 65 72 79 20 73  for fts3 query s
35b9c 74 72 69 6e 67 73 0a 2a 2a 20 28 74 68 65 20 72  trings.** (the r
35b9d 69 67 68 74 2d 68 61 6e 64 20 61 72 67 75 6d 65  ight-hand argume
35b9e 6e 74 20 74 6f 20 74 68 65 20 4d 41 54 43 48 20  nt to the MATCH 
35b9f 6f 70 65 72 61 74 6f 72 29 2e 20 42 65 63 61 75  operator). Becau
35ba0 73 65 20 74 68 65 20 73 75 70 70 6f 72 74 65 64  se the supported
35ba1 20 0a 2a 2a 20 73 79 6e 74 61 78 20 69 73 20 72   .** syntax is r
35ba2 65 6c 61 74 69 76 65 6c 79 20 73 69 6d 70 6c 65  elatively simple
35ba3 2c 20 74 68 65 20 77 68 6f 6c 65 20 74 6f 6b 65  , the whole toke
35ba4 6e 69 7a 65 72 2f 70 61 72 73 65 72 20 73 79 73  nizer/parser sys
35ba5 74 65 6d 20 69 73 0a 2a 2a 20 68 61 6e 64 2d 63  tem is.** hand-c
35ba6 6f 64 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 21 64  oded. .*/.#if !d
35ba7 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
35ba8 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
35ba9 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
35baa 33 29 0a 0a 2f 2a 0a 2a 2a 20 42 79 20 64 65 66  3)../*.** By def
35bab 61 75 6c 74 2c 20 74 68 69 73 20 6d 6f 64 75 6c  ault, this modul
35bac 65 20 70 61 72 73 65 73 20 74 68 65 20 6c 65 67  e parses the leg
35bad 61 63 79 20 73 79 6e 74 61 78 20 74 68 61 74 20  acy syntax that 
35bae 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 74 72 61  has been .** tra
35baf 64 69 74 69 6f 6e 61 6c 6c 79 20 75 73 65 64 20  ditionally used 
35bb0 62 79 20 66 74 73 33 2e 20 4f 72 2c 20 69 66 20  by fts3. Or, if 
35bb1 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
35bb2 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53 0a 2a  S3_PARENTHESIS.*
35bb3 2a 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68  * is defined, th
35bb4 65 6e 20 69 74 20 75 73 65 73 20 74 68 65 20 6e  en it uses the n
35bb5 65 77 20 73 79 6e 74 61 78 2e 20 54 68 65 20 64  ew syntax. The d
35bb6 69 66 66 65 72 65 6e 63 65 73 20 62 65 74 77 65  ifferences betwe
35bb7 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 61 6e  en.** the new an
35bb8 64 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78  d the old syntax
35bb9 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 61  es are:.**.**  a
35bba 29 20 54 68 65 20 6e 65 77 20 73 79 6e 74 61 78  ) The new syntax
35bbb 20 73 75 70 70 6f 72 74 73 20 70 61 72 65 6e 74   supports parent
35bbc 68 65 73 69 73 2e 20 54 68 65 20 6f 6c 64 20 64  hesis. The old d
35bbd 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 20  oes not..**.**  
35bbe 62 29 20 54 68 65 20 6e 65 77 20 73 79 6e 74 61  b) The new synta
35bbf 78 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 41  x supports the A
35bc0 4e 44 20 61 6e 64 20 4e 4f 54 20 6f 70 65 72 61  ND and NOT opera
35bc1 74 6f 72 73 2e 20 54 68 65 20 6f 6c 64 20 64 6f  tors. The old do
35bc2 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 20 63  es not..**.**  c
35bc3 29 20 54 68 65 20 6f 6c 64 20 73 79 6e 74 61 78  ) The old syntax
35bc4 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 22 2d   supports the "-
35bc5 22 20 74 6f 6b 65 6e 20 71 75 61 6c 69 66 69 65  " token qualifie
35bc6 72 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 0a  r. This is not .
35bc7 2a 2a 20 20 20 20 20 73 75 70 70 6f 72 74 65 64  **     supported
35bc8 20 62 79 20 74 68 65 20 6e 65 77 20 73 79 6e 74   by the new synt
35bc9 61 78 20 28 69 74 20 69 73 20 72 65 70 6c 61 63  ax (it is replac
35bca 65 64 20 62 79 20 74 68 65 20 4e 4f 54 20 6f 70  ed by the NOT op
35bcb 65 72 61 74 6f 72 29 2e 0a 2a 2a 0a 2a 2a 20 20  erator)..**.**  
35bcc 64 29 20 57 68 65 6e 20 75 73 69 6e 67 20 74 68  d) When using th
35bcd 65 20 6f 6c 64 20 73 79 6e 74 61 78 2c 20 74 68  e old syntax, th
35bce 65 20 4f 52 20 6f 70 65 72 61 74 6f 72 20 68 61  e OR operator ha
35bcf 73 20 61 20 67 72 65 61 74 65 72 20 70 72 65 63  s a greater prec
35bd0 65 64 65 6e 63 65 0a 2a 2a 20 20 20 20 20 74 68  edence.**     th
35bd1 61 6e 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41  an an implicit A
35bd2 4e 44 2e 20 57 68 65 6e 20 75 73 69 6e 67 20 74  ND. When using t
35bd3 68 65 20 6e 65 77 2c 20 62 6f 74 68 20 69 6d 70  he new, both imp
35bd4 6c 69 63 69 74 79 20 61 6e 64 20 65 78 70 6c 69  licity and expli
35bd5 63 69 74 0a 2a 2a 20 20 20 20 20 41 4e 44 20 6f  cit.**     AND o
35bd6 70 65 72 61 74 6f 72 73 20 68 61 76 65 20 61 20  perators have a 
35bd7 68 69 67 68 65 72 20 70 72 65 63 65 64 65 6e 63  higher precedenc
35bd8 65 20 74 68 61 6e 20 4f 52 2e 0a 2a 2a 0a 2a 2a  e than OR..**.**
35bd9 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   If compiled wit
35bda 68 20 53 51 4c 49 54 45 5f 54 45 53 54 20 64 65  h SQLITE_TEST de
35bdb 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 69 73  fined, then this
35bdc 20 6d 6f 64 75 6c 65 20 65 78 70 6f 72 74 73 20   module exports 
35bdd 74 68 65 0a 2a 2a 20 73 79 6d 62 6f 6c 20 22 69  the.** symbol "i
35bde 6e 74 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f  nt sqlite3_fts3_
35bdf 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73  enable_parenthes
35be0 65 73 22 2e 20 53 65 74 74 69 6e 67 20 74 68 69  es". Setting thi
35be1 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 74 6f  s variable.** to
35be2 20 7a 65 72 6f 20 63 61 75 73 65 73 20 74 68 65   zero causes the
35be3 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 74   module to use t
35be4 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 2e 20 49  he old syntax. I
35be5 66 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 0a  f it is set to .
35be6 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 20  ** non-zero the 
35be7 6e 65 77 20 73 79 6e 74 61 78 20 69 73 20 61 63  new syntax is ac
35be8 74 69 76 61 74 65 64 2e 20 54 68 69 73 20 69 73  tivated. This is
35be9 20 73 6f 20 62 6f 74 68 20 73 79 6e 74 61 78 65   so both syntaxe
35bea 73 20 63 61 6e 0a 2a 2a 20 62 65 20 74 65 73 74  s can.** be test
35beb 65 64 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c  ed using a singl
35bec 65 20 62 75 69 6c 64 20 6f 66 20 74 65 73 74 66  e build of testf
35bed 69 78 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ixture..**.** Th
35bee 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63  e following desc
35bef 72 69 62 65 73 20 74 68 65 20 73 79 6e 74 61 78  ribes the syntax
35bf0 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 74 68   supported by th
35bf1 65 20 66 74 73 33 20 4d 41 54 43 48 0a 2a 2a 20  e fts3 MATCH.** 
35bf2 6f 70 65 72 61 74 6f 72 20 69 6e 20 61 20 73 69  operator in a si
35bf3 6d 69 6c 61 72 20 66 6f 72 6d 61 74 20 74 6f 20  milar format to 
35bf4 74 68 61 74 20 75 73 65 64 20 62 79 20 74 68 65  that used by the
35bf5 20 6c 65 6d 6f 6e 20 70 61 72 73 65 72 0a 2a 2a   lemon parser.**
35bf6 20 67 65 6e 65 72 61 74 6f 72 2e 20 54 68 69 73   generator. This
35bf7 20 6d 6f 64 75 6c 65 20 64 6f 65 73 20 6e 6f 74   module does not
35bf8 20 75 73 65 20 61 63 74 75 61 6c 6c 79 20 6c 65   use actually le
35bf9 6d 6f 6e 2c 20 69 74 20 75 73 65 73 20 61 0a 2a  mon, it uses a.*
35bfa 2a 20 63 75 73 74 6f 6d 20 70 61 72 73 65 72 2e  * custom parser.
35bfb 0a 2a 2a 0a 2a 2a 20 20 20 71 75 65 72 79 20 3a  .**.**   query :
35bfc 3a 3d 20 61 6e 64 65 78 70 72 20 28 4f 52 20 61  := andexpr (OR a
35bfd 6e 64 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20  ndexpr)*..**.** 
35bfe 20 20 61 6e 64 65 78 70 72 20 3a 3a 3d 20 6e 6f    andexpr ::= no
35bff 74 65 78 70 72 20 28 41 4e 44 3f 20 6e 6f 74 65  texpr (AND? note
35c00 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e  xpr)*..**.**   n
35c01 6f 74 65 78 70 72 20 3a 3a 3d 20 6e 65 61 72 65  otexpr ::= neare
35c02 78 70 72 20 28 4e 4f 54 20 6e 65 61 72 65 78 70  xpr (NOT nearexp
35c03 72 7c 2d 54 4f 4b 45 4e 29 2a 2e 0a 2a 2a 20 20  r|-TOKEN)*..**  
35c04 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 4c 50 20   notexpr ::= LP 
35c05 71 75 65 72 79 20 52 50 2e 0a 2a 2a 0a 2a 2a 20  query RP..**.** 
35c06 20 20 6e 65 61 72 65 78 70 72 20 3a 3a 3d 20 70    nearexpr ::= p
35c07 68 72 61 73 65 20 28 4e 45 41 52 20 64 69 73 74  hrase (NEAR dist
35c08 61 6e 63 65 5f 6f 70 74 20 6e 65 61 72 65 78 70  ance_opt nearexp
35c09 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 64 69 73  r)*..**.**   dis
35c0a 74 61 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20 2e 0a  tance_opt ::= ..
35c0b 2a 2a 20 20 20 64 69 73 74 61 6e 63 65 5f 6f 70  **   distance_op
35c0c 74 20 3a 3a 3d 20 2f 20 49 4e 54 45 47 45 52 2e  t ::= / INTEGER.
35c0d 0a 2a 2a 0a 2a 2a 20 20 20 70 68 72 61 73 65 20  .**.**   phrase 
35c0e 3a 3a 3d 20 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20  ::= TOKEN..**   
35c0f 70 68 72 61 73 65 20 3a 3a 3d 20 43 4f 4c 55 4d  phrase ::= COLUM
35c10 4e 3a 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68  N:TOKEN..**   ph
35c11 72 61 73 65 20 3a 3a 3d 20 22 54 4f 4b 45 4e 20  rase ::= "TOKEN 
35c12 54 4f 4b 45 4e 20 54 4f 4b 45 4e 2e 2e 2e 22 2e  TOKEN TOKEN...".
35c13 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
35c14 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
35c15 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
35c16 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
35c17 74 68 65 73 65 73 20 3d 20 30 3b 0a 23 65 6c 73  theses = 0;.#els
35c18 65 0a 23 20 69 66 64 65 66 20 53 51 4c 49 54 45  e.# ifdef SQLITE
35c19 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41 52  _ENABLE_FTS3_PAR
35c1a 45 4e 54 48 45 53 49 53 20 0a 23 20 20 64 65 66  ENTHESIS .#  def
35c1b 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 74 73 33  ine sqlite3_fts3
35c1c 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65  _enable_parenthe
35c1d 73 65 73 20 31 0a 23 20 65 6c 73 65 0a 23 20 20  ses 1.# else.#  
35c1e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66  define sqlite3_f
35c1f 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
35c20 74 68 65 73 65 73 20 30 0a 23 20 65 6e 64 69 66  theses 0.# endif
35c21 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44  .#endif../*.** D
35c22 65 66 61 75 6c 74 20 73 70 61 6e 20 66 6f 72 20  efault span for 
35c23 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 73 2e 0a  NEAR operators..
35c24 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
35c25 45 5f 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e  E_FTS3_DEFAULT_N
35c26 45 41 52 5f 50 41 52 41 4d 20 31 30 0a 0a 0a 74  EAR_PARAM 10...t
35c27 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
35c28 72 73 65 43 6f 6e 74 65 78 74 20 50 61 72 73 65  rseContext Parse
35c29 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20  Context;.struct 
35c2a 50 61 72 73 65 43 6f 6e 74 65 78 74 20 7b 0a 20  ParseContext {. 
35c2b 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
35c2c 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20  er *pTokenizer; 
35c2d 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65       /* Tokenize
35c2e 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f  r module */.  co
35c2f 6e 73 74 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c  nst char **azCol
35c30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35c31 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f    /* Array of co
35c32 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 66  lumn names for f
35c33 74 73 33 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ts3 table */.  i
35c34 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt nCol;        
35c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c36 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
35c37 65 6e 74 72 69 65 73 20 69 6e 20 61 7a 43 6f 6c  entries in azCol
35c38 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65 66  [] */.  int iDef
35c39 61 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20 20  aultCol;        
35c3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
35c3b 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20 74 6f  efault column to
35c3c 20 71 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69   query */.  sqli
35c3d 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
35c3e 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
35c3f 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72 20 6d  /* Write error m
35c40 65 73 73 61 67 65 20 68 65 72 65 20 2a 2f 0a 20  essage here */. 
35c41 20 69 6e 74 20 6e 4e 65 73 74 3b 20 20 20 20 20   int nNest;     
35c42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c43 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
35c44 66 20 6e 65 73 74 65 64 20 62 72 61 63 6b 65 74  f nested bracket
35c45 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  s */.};../*.** T
35c46 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
35c47 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68  equivalent to th
35c48 65 20 73 74 61 6e 64 61 72 64 20 69 73 73 70 61  e standard isspa
35c49 63 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 0a  ce() function. .
35c4a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61 6e 64 61  **.** The standa
35c4b 72 64 20 69 73 73 70 61 63 65 28 29 20 63 61 6e  rd isspace() can
35c4c 20 62 65 20 61 77 6b 77 61 72 64 20 74 6f 20 75   be awkward to u
35c4d 73 65 20 73 61 66 65 6c 79 2c 20 62 65 63 61 75  se safely, becau
35c4e 73 65 20 61 6c 74 68 6f 75 67 68 20 69 74 0a 2a  se although it.*
35c4f 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20  * is defined to 
35c50 61 63 63 65 70 74 20 61 6e 20 61 72 67 75 6d 65  accept an argume
35c51 6e 74 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20  nt of type int, 
35c52 69 74 73 20 62 65 68 61 76 69 6f 75 72 20 77 68  its behaviour wh
35c53 65 6e 20 70 61 73 73 65 64 0a 2a 2a 20 61 6e 20  en passed.** an 
35c54 69 6e 74 65 67 65 72 20 74 68 61 74 20 66 61 6c  integer that fal
35c55 6c 73 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68  ls outside of th
35c56 65 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 75  e range of the u
35c57 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 79 70  nsigned char typ
35c58 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  e.** is undefine
35c59 64 20 28 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73  d (and sometimes
35c5a 2c 20 22 75 6e 64 65 66 69 6e 65 64 22 20 6d 65  , "undefined" me
35c5b 61 6e 73 20 73 65 67 66 61 75 6c 74 29 2e 20 54  ans segfault). T
35c5c 68 69 73 20 77 72 61 70 70 65 72 0a 2a 2a 20 69  his wrapper.** i
35c5d 73 20 64 65 66 69 6e 65 64 20 74 6f 20 61 63 63  s defined to acc
35c5e 65 70 74 20 61 6e 20 61 72 67 75 6d 65 6e 74 20  ept an argument 
35c5f 6f 66 20 74 79 70 65 20 63 68 61 72 2c 20 61 6e  of type char, an
35c60 64 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  d always returns
35c61 20 30 20 66 6f 72 0a 2a 2a 20 61 6e 79 20 76 61   0 for.** any va
35c62 6c 75 65 73 20 74 68 61 74 20 66 61 6c 6c 20 6f  lues that fall o
35c63 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 72 61  utside of the ra
35c64 6e 67 65 20 6f 66 20 74 68 65 20 75 6e 73 69 67  nge of the unsig
35c65 6e 65 64 20 63 68 61 72 20 74 79 70 65 20 28 69  ned char type (i
35c66 2e 65 2e 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20  .e..** negative 
35c67 76 61 6c 75 65 73 29 2e 0a 2a 2f 0a 73 74 61 74  values)..*/.stat
35c68 69 63 20 69 6e 74 20 66 74 73 33 69 73 73 70 61  ic int fts3isspa
35c69 63 65 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65  ce(char c){.  re
35c6a 74 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30  turn (c&0x80)==0
35c6b 20 3f 20 69 73 73 70 61 63 65 28 63 29 20 3a 20   ? isspace(c) : 
35c6c 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  0;.}../*.** Extr
35c6d 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b  act the next tok
35c6e 65 6e 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a  en from buffer z
35c6f 20 28 6c 65 6e 67 74 68 20 6e 29 20 75 73 69 6e   (length n) usin
35c70 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a  g the tokenizer.
35c71 2a 2a 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 66  ** and other inf
35c72 6f 72 6d 61 74 69 6f 6e 20 28 63 6f 6c 75 6d 6e  ormation (column
35c73 20 6e 61 6d 65 73 20 65 74 63 2e 29 20 69 6e 20   names etc.) in 
35c74 70 50 61 72 73 65 2e 20 43 72 65 61 74 65 20 61  pParse. Create a
35c75 6e 20 46 74 73 33 45 78 70 72 0a 2a 2a 20 73 74  n Fts3Expr.** st
35c76 72 75 63 74 75 72 65 20 6f 66 20 74 79 70 65 20  ructure of type 
35c77 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
35c78 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 68 72  containing a phr
35c79 61 73 65 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f  ase consisting o
35c7a 66 20 74 68 69 73 0a 2a 2a 20 73 69 6e 67 6c 65  f this.** single
35c7b 20 74 6f 6b 65 6e 20 61 6e 64 20 73 65 74 20 2a   token and set *
35c7c 70 70 45 78 70 72 20 74 6f 20 70 6f 69 6e 74 20  ppExpr to point 
35c7d 74 6f 20 69 74 2e 20 49 66 20 74 68 65 20 65 6e  to it. If the en
35c7e 64 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  d of the buffer 
35c7f 69 73 0a 2a 2a 20 72 65 61 63 68 65 64 20 62 65  is.** reached be
35c80 66 6f 72 65 20 61 20 74 6f 6b 65 6e 20 69 73 20  fore a token is 
35c81 66 6f 75 6e 64 2c 20 73 65 74 20 2a 70 70 45 78  found, set *ppEx
35c82 70 72 20 74 6f 20 7a 65 72 6f 2e 20 49 74 20 69  pr to zero. It i
35c83 73 20 74 68 65 0a 2a 2a 20 72 65 73 70 6f 6e 73  s the.** respons
35c84 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63  ibility of the c
35c85 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61  aller to eventua
35c86 6c 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 20 74  lly deallocate t
35c87 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a  he allocated .**
35c88 20 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74   Fts3Expr struct
35c89 75 72 65 20 28 69 66 20 61 6e 79 29 20 62 79 20  ure (if any) by 
35c8a 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71  passing it to sq
35c8b 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2a  lite3_free()..**
35c8c 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
35c8d 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66  E_OK if successf
35c8e 75 6c 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f  ul, or SQLITE_NO
35c8f 4d 45 4d 20 69 66 20 61 20 6d 65 6d 6f 72 79 20  MEM if a memory 
35c90 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61  allocation.** fa
35c91 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ils..*/.static i
35c92 6e 74 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e 28  nt getNextToken(
35c93 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20  .  ParseContext 
35c94 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
35c95 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74             /* ft
35c96 73 33 20 71 75 65 72 79 20 70 61 72 73 65 20 63  s3 query parse c
35c97 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
35c98 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  iCol,           
35c99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c9a 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72      /* Value for
35c9b 20 46 74 73 33 50 68 72 61 73 65 2e 69 43 6f 6c   Fts3Phrase.iCol
35c9c 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  umn */.  const c
35c9d 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20  har *z, int n,  
35c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c9f 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
35ca0 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
35ca1 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20 20 20  *ppExpr,        
35ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35ca3 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e   OUT: expression
35ca4 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e   */.  int *pnCon
35ca5 73 75 6d 65 64 20 20 20 20 20 20 20 20 20 20 20  sumed           
35ca6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35ca7 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
35ca8 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a  bytes consumed *
35ca9 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  /.){.  sqlite3_t
35caa 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
35cab 69 7a 65 72 20 3d 20 70 50 61 72 73 65 2d 3e 70  izer = pParse->p
35cac 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c  Tokenizer;.  sql
35cad 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
35cae 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f  odule const *pMo
35caf 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65  dule = pTokenize
35cb0 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e  r->pModule;.  in
35cb1 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
35cb2 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
35cb3 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 46 74 73   *pCursor;.  Fts
35cb4 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b  3Expr *pRet = 0;
35cb5 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 75 6d 65 64  .  int nConsumed
35cb6 20 3d 20 30 3b 0a 0a 20 20 72 63 20 3d 20 70 4d   = 0;..  rc = pM
35cb7 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f  odule->xOpen(pTo
35cb8 6b 65 6e 69 7a 65 72 2c 20 7a 2c 20 6e 2c 20 26  kenizer, z, n, &
35cb9 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20  pCursor);.  if( 
35cba 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
35cbb 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
35cbc 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 6e 74  *zToken;.    int
35cbd 20 6e 54 6f 6b 65 6e 2c 20 69 53 74 61 72 74 2c   nToken, iStart,
35cbe 20 69 45 6e 64 2c 20 69 50 6f 73 69 74 69 6f 6e   iEnd, iPosition
35cbf 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b  ;.    int nByte;
35cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35cc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35cc2 2a 20 74 6f 74 61 6c 20 73 70 61 63 65 20 74 6f  * total space to
35cc3 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 0a 20 20   allocate */..  
35cc4 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65    pCursor->pToke
35cc5 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
35cc6 65 72 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f  er;.    rc = pMo
35cc7 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72  dule->xNext(pCur
35cc8 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e  sor, &zToken, &n
35cc9 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20  Token, &iStart, 
35cca 26 69 45 6e 64 2c 20 26 69 50 6f 73 69 74 69 6f  &iEnd, &iPositio
35ccb 6e 29 3b 0a 0a 20 20 20 20 69 66 28 20 72 63 3d  n);..    if( rc=
35ccc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
35ccd 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65      nByte = size
35cce 6f 66 28 46 74 73 33 45 78 70 72 29 20 2b 20 73  of(Fts3Expr) + s
35ccf 69 7a 65 6f 66 28 46 74 73 33 50 68 72 61 73 65  izeof(Fts3Phrase
35cd0 29 20 2b 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20  ) + nToken;.    
35cd1 20 20 70 52 65 74 20 3d 20 28 46 74 73 33 45 78    pRet = (Fts3Ex
35cd2 70 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  pr *)sqlite3_mal
35cd3 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 20 20  loc(nByte);.    
35cd4 20 20 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20    if( !pRet ){. 
35cd5 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
35cd6 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
35cd7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d  }else{.        m
35cd8 65 6d 73 65 74 28 70 52 65 74 2c 20 30 2c 20 6e  emset(pRet, 0, n
35cd9 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 70  Byte);.        p
35cda 52 65 74 2d 3e 65 54 79 70 65 20 3d 20 46 54 53  Ret->eType = FTS
35cdb 51 55 45 52 59 5f 50 48 52 41 53 45 3b 0a 20 20  QUERY_PHRASE;.  
35cdc 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72        pRet->pPhr
35cdd 61 73 65 20 3d 20 28 46 74 73 33 50 68 72 61 73  ase = (Fts3Phras
35cde 65 20 2a 29 26 70 52 65 74 5b 31 5d 3b 0a 20 20  e *)&pRet[1];.  
35cdf 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72        pRet->pPhr
35ce0 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 31 3b  ase->nToken = 1;
35ce1 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70  .        pRet->p
35ce2 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 20  Phrase->iColumn 
35ce3 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  = iCol;.        
35ce4 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61  pRet->pPhrase->a
35ce5 54 6f 6b 65 6e 5b 30 5d 2e 6e 20 3d 20 6e 54 6f  Token[0].n = nTo
35ce6 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 52 65  ken;.        pRe
35ce7 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  t->pPhrase->aTok
35ce8 65 6e 5b 30 5d 2e 7a 20 3d 20 28 63 68 61 72 20  en[0].z = (char 
35ce9 2a 29 26 70 52 65 74 2d 3e 70 50 68 72 61 73 65  *)&pRet->pPhrase
35cea 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  [1];.        mem
35ceb 63 70 79 28 70 52 65 74 2d 3e 70 50 68 72 61 73  cpy(pRet->pPhras
35cec 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a 2c 20  e->aToken[0].z, 
35ced 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b  zToken, nToken);
35cee 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 45  ..        if( iE
35cef 6e 64 3c 6e 20 26 26 20 7a 5b 69 45 6e 64 5d 3d  nd<n && z[iEnd]=
35cf0 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
35cf1 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d    pRet->pPhrase-
35cf2 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 69 73 50 72 65  >aToken[0].isPre
35cf3 66 69 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  fix = 1;.       
35cf4 20 20 20 69 45 6e 64 2b 2b 3b 0a 20 20 20 20 20     iEnd++;.     
35cf5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
35cf6 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65   !sqlite3_fts3_e
35cf7 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
35cf8 73 20 26 26 20 69 53 74 61 72 74 3e 30 20 26 26  s && iStart>0 &&
35cf9 20 7a 5b 69 53 74 61 72 74 2d 31 5d 3d 3d 27 2d   z[iStart-1]=='-
35cfa 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ' ){.          p
35cfb 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73  Ret->pPhrase->is
35cfc 4e 6f 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Not = 1;.       
35cfd 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
35cfe 20 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 69 45 6e   nConsumed = iEn
35cff 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d  d;.    }..    pM
35d00 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43  odule->xClose(pC
35d01 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 0a 20  ursor);.  }.  . 
35d02 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 6e   *pnConsumed = n
35d03 43 6f 6e 73 75 6d 65 64 3b 0a 20 20 2a 70 70 45  Consumed;.  *ppE
35d04 78 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65  xpr = pRet;.  re
35d05 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
35d06 2a 2a 20 45 6e 6c 61 72 67 65 20 61 20 6d 65 6d  ** Enlarge a mem
35d07 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  ory allocation. 
35d08 20 49 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65   If an out-of-me
35d09 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
35d0a 6f 63 63 75 72 73 2c 0a 2a 2a 20 74 68 65 6e 20  occurs,.** then 
35d0b 66 72 65 65 20 74 68 65 20 6f 6c 64 20 61 6c 6c  free the old all
35d0c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64  ocation..*/.void
35d0d 20 2a 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46   *fts3ReallocOrF
35d0e 72 65 65 28 76 6f 69 64 20 2a 70 4f 72 69 67 2c  ree(void *pOrig,
35d0f 20 69 6e 74 20 6e 4e 65 77 29 7b 0a 20 20 76 6f   int nNew){.  vo
35d10 69 64 20 2a 70 52 65 74 20 3d 20 73 71 6c 69 74  id *pRet = sqlit
35d11 65 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 72 69 67  e3_realloc(pOrig
35d12 2c 20 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20 21  , nNew);.  if( !
35d13 70 52 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  pRet ){.    sqli
35d14 74 65 33 5f 66 72 65 65 28 70 4f 72 69 67 29 3b  te3_free(pOrig);
35d15 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 52  .  }.  return pR
35d16 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 66  et;.}../*.** Buf
35d17 66 65 72 20 7a 49 6e 70 75 74 2c 20 6c 65 6e 67  fer zInput, leng
35d18 74 68 20 6e 49 6e 70 75 74 2c 20 63 6f 6e 74 61  th nInput, conta
35d19 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ins the contents
35d1a 20 6f 66 20 61 20 71 75 6f 74 65 64 20 73 74 72   of a quoted str
35d1b 69 6e 67 0a 2a 2a 20 74 68 61 74 20 61 70 70 65  ing.** that appe
35d1c 61 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  ared as part of 
35d1d 61 6e 20 66 74 73 33 20 71 75 65 72 79 20 65 78  an fts3 query ex
35d1e 70 72 65 73 73 69 6f 6e 2e 20 4e 65 69 74 68 65  pression. Neithe
35d1f 72 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65  r quote characte
35d20 72 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64  r.** is included
35d21 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 2e 20   in the buffer. 
35d22 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74  This function at
35d23 74 65 6d 70 74 73 20 74 6f 20 74 6f 6b 65 6e 69  tempts to tokeni
35d24 7a 65 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a  ze the entire.**
35d25 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 61 6e   input buffer an
35d26 64 20 63 72 65 61 74 65 20 61 6e 20 46 74 73 33  d create an Fts3
35d27 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20 6f  Expr structure o
35d28 66 20 74 79 70 65 20 46 54 53 51 55 45 52 59 5f  f type FTSQUERY_
35d29 50 48 52 41 53 45 20 0a 2a 2a 20 63 6f 6e 74 61  PHRASE .** conta
35d2a 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  ining the result
35d2b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63  s..**.** If succ
35d2c 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
35d2d 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  K is returned an
35d2e 64 20 2a 70 70 45 78 70 72 20 73 65 74 20 74 6f  d *ppExpr set to
35d2f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 0a 2a 2a   point at the.**
35d30 20 61 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 45   allocated Fts3E
35d31 78 70 72 20 73 74 72 75 63 74 75 72 65 2e 20 4f  xpr structure. O
35d32 74 68 65 72 77 69 73 65 2c 20 65 69 74 68 65 72  therwise, either
35d33 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f   SQLITE_NOMEM (o
35d34 75 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ut of memory.** 
35d35 65 72 72 6f 72 29 20 6f 72 20 53 51 4c 49 54 45  error) or SQLITE
35d36 5f 45 52 52 4f 52 20 28 74 6f 6b 65 6e 69 7a 61  _ERROR (tokeniza
35d37 74 69 6f 6e 20 65 72 72 6f 72 29 20 69 73 20 72  tion error) is r
35d38 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45  eturned and *ppE
35d39 78 70 72 20 73 65 74 0a 2a 2a 20 74 6f 20 30 2e  xpr set.** to 0.
35d3a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
35d3b 65 74 4e 65 78 74 53 74 72 69 6e 67 28 0a 20 20  etNextString(.  
35d3c 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50  ParseContext *pP
35d3d 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
35d3e 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20          /* fts3 
35d3f 71 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74  query parse cont
35d40 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
35d41 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74  har *zInput, int
35d42 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20   nInput,        
35d43 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
35d44 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
35d45 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20 20  *ppExpr         
35d46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35d47 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e   OUT: expression
35d48 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
35d49 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
35d4a 65 6e 69 7a 65 72 20 3d 20 70 50 61 72 73 65 2d  enizer = pParse-
35d4b 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73  >pTokenizer;.  s
35d4c 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
35d4d 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70  _module const *p
35d4e 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69  Module = pTokeni
35d4f 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  zer->pModule;.  
35d50 69 6e 74 20 72 63 3b 0a 20 20 46 74 73 33 45 78  int rc;.  Fts3Ex
35d51 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20 73 71 6c  pr *p = 0;.  sql
35d52 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
35d53 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 3d  ursor *pCursor =
35d54 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 6d   0;.  char *zTem
35d55 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 54 65  p = 0;.  int nTe
35d56 6d 70 20 3d 20 30 3b 0a 0a 20 20 72 63 20 3d 20  mp = 0;..  rc = 
35d57 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70  pModule->xOpen(p
35d58 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75  Tokenizer, zInpu
35d59 74 2c 20 6e 49 6e 70 75 74 2c 20 26 70 43 75 72  t, nInput, &pCur
35d5a 73 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  sor);.  if( rc==
35d5b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
35d5c 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 70 43 75   int ii;.    pCu
35d5d 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  rsor->pTokenizer
35d5e 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20   = pTokenizer;. 
35d5f 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d     for(ii=0; rc=
35d60 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b  =SQLITE_OK; ii++
35d61 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
35d62 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 20  har *zToken;.   
35d63 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69     int nToken, i
35d64 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f  Begin, iEnd, iPo
35d65 73 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 4d  s;.      rc = pM
35d66 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75  odule->xNext(pCu
35d67 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26  rsor, &zToken, &
35d68 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c  nToken, &iBegin,
35d69 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a   &iEnd, &iPos);.
35d6a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
35d6b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
35d6c 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73     int nByte = s
35d6d 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 20  izeof(Fts3Expr) 
35d6e 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72  + sizeof(Fts3Phr
35d6f 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 70 20  ase);.        p 
35d70 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46  = fts3ReallocOrF
35d71 72 65 65 28 70 2c 20 6e 42 79 74 65 2b 69 69 2a  ree(p, nByte+ii*
35d72 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 50 68  sizeof(struct Ph
35d73 72 61 73 65 54 6f 6b 65 6e 29 29 3b 0a 20 20 20  raseToken));.   
35d74 20 20 20 20 20 7a 54 65 6d 70 20 3d 20 66 74 73       zTemp = fts
35d75 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 7a  3ReallocOrFree(z
35d76 54 65 6d 70 2c 20 6e 54 65 6d 70 20 2b 20 6e 54  Temp, nTemp + nT
35d77 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 69  oken);.        i
35d78 66 28 20 21 70 20 7c 7c 20 21 7a 54 65 6d 70 20  f( !p || !zTemp 
35d79 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
35d7a 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20  o no_mem;.      
35d7b 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
35d7c 69 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ii==0 ){.       
35d7d 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20     memset(p, 0, 
35d7e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20  nByte);.        
35d7f 20 20 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28    p->pPhrase = (
35d80 46 74 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b  Fts3Phrase *)&p[
35d81 31 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1];.        }.  
35d82 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65        p->pPhrase
35d83 20 3d 20 28 46 74 73 33 50 68 72 61 73 65 20 2a   = (Fts3Phrase *
35d84 29 26 70 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  )&p[1];.        
35d85 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  p->pPhrase->nTok
35d86 65 6e 20 3d 20 69 69 2b 31 3b 0a 20 20 20 20 20  en = ii+1;.     
35d87 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61     p->pPhrase->a
35d88 54 6f 6b 65 6e 5b 69 69 5d 2e 6e 20 3d 20 6e 54  Token[ii].n = nT
35d89 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 6d 65  oken;.        me
35d8a 6d 63 70 79 28 26 7a 54 65 6d 70 5b 6e 54 65 6d  mcpy(&zTemp[nTem
35d8b 70 5d 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  p], zToken, nTok
35d8c 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 54 65  en);.        nTe
35d8d 6d 70 20 2b 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20  mp += nToken;.  
35d8e 20 20 20 20 20 20 69 66 28 20 69 45 6e 64 3c 6e        if( iEnd<n
35d8f 49 6e 70 75 74 20 26 26 20 7a 49 6e 70 75 74 5b  Input && zInput[
35d90 69 45 6e 64 5d 3d 3d 27 2a 27 20 29 7b 0a 20 20  iEnd]=='*' ){.  
35d91 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61          p->pPhra
35d92 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 2e 69  se->aToken[ii].i
35d93 73 50 72 65 66 69 78 20 3d 20 31 3b 0a 20 20 20  sPrefix = 1;.   
35d94 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
35d95 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65        p->pPhrase
35d96 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50  ->aToken[ii].isP
35d97 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20  refix = 0;.     
35d98 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
35d99 20 7d 0a 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d   }..    pModule-
35d9a 3e 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29  >xClose(pCursor)
35d9b 3b 0a 20 20 20 20 70 43 75 72 73 6f 72 20 3d 20  ;.    pCursor = 
35d9c 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  0;.  }..  if( rc
35d9d 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
35d9e 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20  .    int jj;.   
35d9f 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20   char *zNew;.   
35da0 20 69 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 0a 20   int nNew = 0;. 
35da1 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73     int nByte = s
35da2 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 20  izeof(Fts3Expr) 
35da3 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72  + sizeof(Fts3Phr
35da4 61 73 65 29 3b 0a 20 20 20 20 6e 42 79 74 65 20  ase);.    nByte 
35da5 2b 3d 20 28 70 3f 28 70 2d 3e 70 50 68 72 61 73  += (p?(p->pPhras
35da6 65 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 3a 30 29 20  e->nToken-1):0) 
35da7 2a 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  * sizeof(struct 
35da8 50 68 72 61 73 65 54 6f 6b 65 6e 29 3b 0a 20 20  PhraseToken);.  
35da9 20 20 70 20 3d 20 66 74 73 33 52 65 61 6c 6c 6f    p = fts3Reallo
35daa 63 4f 72 46 72 65 65 28 70 2c 20 6e 42 79 74 65  cOrFree(p, nByte
35dab 20 2b 20 6e 54 65 6d 70 29 3b 0a 20 20 20 20 69   + nTemp);.    i
35dac 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 67  f( !p ){.      g
35dad 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20  oto no_mem;.    
35dae 7d 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 20  }.    if( zTemp 
35daf 29 7b 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d 20  ){.      zNew = 
35db0 26 28 28 28 63 68 61 72 20 2a 29 70 29 5b 6e 42  &(((char *)p)[nB
35db1 79 74 65 5d 29 3b 0a 20 20 20 20 20 20 6d 65 6d  yte]);.      mem
35db2 63 70 79 28 7a 4e 65 77 2c 20 7a 54 65 6d 70 2c  cpy(zNew, zTemp,
35db3 20 6e 54 65 6d 70 29 3b 0a 20 20 20 20 7d 65 6c   nTemp);.    }el
35db4 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  se{.      memset
35db5 28 70 2c 20 30 2c 20 6e 42 79 74 65 2b 6e 54 65  (p, 0, nByte+nTe
35db6 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  mp);.    }.    p
35db7 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74 73  ->pPhrase = (Fts
35db8 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b  3Phrase *)&p[1];
35db9 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a  .    for(jj=0; j
35dba 6a 3c 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  j<p->pPhrase->nT
35dbb 6f 6b 65 6e 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  oken; jj++){.   
35dbc 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61     p->pPhrase->a
35dbd 54 6f 6b 65 6e 5b 6a 6a 5d 2e 7a 20 3d 20 26 7a  Token[jj].z = &z
35dbe 4e 65 77 5b 6e 4e 65 77 5d 3b 0a 20 20 20 20 20  New[nNew];.     
35dbf 20 6e 4e 65 77 20 2b 3d 20 70 2d 3e 70 50 68 72   nNew += p->pPhr
35dc0 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e  ase->aToken[jj].
35dc1 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  n;.    }.    sql
35dc2 69 74 65 33 5f 66 72 65 65 28 7a 54 65 6d 70 29  ite3_free(zTemp)
35dc3 3b 0a 20 20 20 20 70 2d 3e 65 54 79 70 65 20 3d  ;.    p->eType =
35dc4 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
35dc5 3b 0a 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65  ;.    p->pPhrase
35dc6 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 50 61 72  ->iColumn = pPar
35dc7 73 65 2d 3e 69 44 65 66 61 75 6c 74 43 6f 6c 3b  se->iDefaultCol;
35dc8 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
35dc9 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 45  _OK;.  }..  *ppE
35dca 78 70 72 20 3d 20 70 3b 0a 20 20 72 65 74 75 72  xpr = p;.  retur
35dcb 6e 20 72 63 3b 0a 6e 6f 5f 6d 65 6d 3a 0a 0a 20  n rc;.no_mem:.. 
35dcc 20 69 66 28 20 70 43 75 72 73 6f 72 20 29 7b 0a   if( pCursor ){.
35dcd 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c      pModule->xCl
35dce 6f 73 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20  ose(pCursor);.  
35dcf 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
35dd0 28 7a 54 65 6d 70 29 3b 0a 20 20 73 71 6c 69 74  (zTemp);.  sqlit
35dd1 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 2a 70  e3_free(p);.  *p
35dd2 70 45 78 70 72 20 3d 20 30 3b 0a 20 20 72 65 74  pExpr = 0;.  ret
35dd3 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
35dd4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74  ;.}../*.** Funct
35dd5 69 6f 6e 20 67 65 74 4e 65 78 74 4e 6f 64 65 28  ion getNextNode(
35dd6 29 2c 20 77 68 69 63 68 20 69 73 20 63 61 6c 6c  ), which is call
35dd7 65 64 20 62 79 20 66 74 73 33 45 78 70 72 50 61  ed by fts3ExprPa
35dd8 72 73 65 28 29 2c 20 6d 61 79 20 69 74 73 65 6c  rse(), may itsel
35dd9 66 0a 2a 2a 20 63 61 6c 6c 20 66 74 73 33 45 78  f.** call fts3Ex
35dda 70 72 50 61 72 73 65 28 29 2e 20 53 6f 20 74 68  prParse(). So th
35ddb 69 73 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61  is forward decla
35ddc 72 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72  ration is requir
35ddd 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
35dde 74 20 66 74 73 33 45 78 70 72 50 61 72 73 65 28  t fts3ExprParse(
35ddf 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 2c 20  ParseContext *, 
35de0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e  const char *, in
35de1 74 2c 20 46 74 73 33 45 78 70 72 20 2a 2a 2c 20  t, Fts3Expr **, 
35de2 69 6e 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54  int *);../*.** T
35de3 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 62  he output variab
35de4 6c 65 20 2a 70 70 45 78 70 72 20 69 73 20 70 6f  le *ppExpr is po
35de5 70 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e 20  pulated with an 
35de6 61 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 45 78  allocated Fts3Ex
35de7 70 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  pr .** structure
35de8 2c 20 6f 72 20 73 65 74 20 74 6f 20 30 20 69 66  , or set to 0 if
35de9 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
35dea 69 6e 70 75 74 20 62 75 66 66 65 72 20 69 73 20  input buffer is 
35deb 72 65 61 63 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52  reached..**.** R
35dec 65 74 75 72 6e 73 20 61 6e 20 53 51 4c 69 74 65  eturns an SQLite
35ded 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 53 51 4c   error code. SQL
35dee 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74  ITE_OK if everyt
35def 68 69 6e 67 20 77 6f 72 6b 73 2c 20 53 51 4c 49  hing works, SQLI
35df0 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 66 20 61  TE_NOMEM.** if a
35df1 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
35df2 6f 63 63 75 72 73 2c 20 6f 72 20 53 51 4c 49 54  occurs, or SQLIT
35df3 45 5f 45 52 52 4f 52 20 69 66 20 61 20 70 61 72  E_ERROR if a par
35df4 73 65 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  se error is enco
35df5 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 49 66 20 53  untered..** If S
35df6 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72  QLITE_ERROR is r
35df7 65 74 75 72 6e 65 64 2c 20 70 43 6f 6e 74 65 78  eturned, pContex
35df8 74 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77  t is populated w
35df9 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ith an error mes
35dfa 73 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sage..*/.static 
35dfb 69 6e 74 20 67 65 74 4e 65 78 74 4e 6f 64 65 28  int getNextNode(
35dfc 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20  .  ParseContext 
35dfd 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
35dfe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74             /* ft
35dff 73 33 20 71 75 65 72 79 20 70 61 72 73 65 20 63  s3 query parse c
35e00 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73  ontext */.  cons
35e01 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  t char *z, int n
35e02 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35e03 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72      /* Input str
35e04 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  ing */.  Fts3Exp
35e05 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20  r **ppExpr,     
35e06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e07 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65 73 73   /* OUT: express
35e08 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ion */.  int *pn
35e09 43 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20  Consumed        
35e0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e0b 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
35e0c 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65  of bytes consume
35e0d 64 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63  d */.){.  static
35e0e 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 46 74   const struct Ft
35e0f 73 33 4b 65 79 77 6f 72 64 20 7b 0a 20 20 20 20  s3Keyword {.    
35e10 63 68 61 72 20 7a 5b 34 5d 3b 20 20 20 20 20 20  char z[4];      
35e11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e12 20 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64        /* Keyword
35e13 20 74 65 78 74 20 2a 2f 0a 20 20 20 20 75 6e 73   text */.    uns
35e14 69 67 6e 65 64 20 63 68 61 72 20 6e 3b 20 20 20  igned char n;   
35e15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e16 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
35e17 74 68 65 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 20  the keyword */. 
35e18 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
35e19 20 70 61 72 65 6e 4f 6e 6c 79 3b 20 20 20 20 20   parenOnly;     
35e1a 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79           /* Only
35e1b 20 76 61 6c 69 64 20 69 6e 20 70 61 72 65 6e 20   valid in paren 
35e1c 6d 6f 64 65 20 2a 2f 0a 20 20 20 20 75 6e 73 69  mode */.    unsi
35e1d 67 6e 65 64 20 63 68 61 72 20 65 54 79 70 65 3b  gned char eType;
35e1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e1f 20 20 2f 2a 20 4b 65 79 77 6f 72 64 20 63 6f 64    /* Keyword cod
35e20 65 20 2a 2f 0a 20 20 7d 20 61 4b 65 79 77 6f 72  e */.  } aKeywor
35e21 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 4f  d[] = {.    { "O
35e22 52 22 20 2c 20 20 32 2c 20 30 2c 20 46 54 53 51  R" ,  2, 0, FTSQ
35e23 55 45 52 59 5f 4f 52 20 20 20 7d 2c 0a 20 20 20  UERY_OR   },.   
35e24 20 7b 20 22 41 4e 44 22 2c 20 20 33 2c 20 31 2c   { "AND",  3, 1,
35e25 20 46 54 53 51 55 45 52 59 5f 41 4e 44 20 20 7d   FTSQUERY_AND  }
35e26 2c 0a 20 20 20 20 7b 20 22 4e 4f 54 22 2c 20 20  ,.    { "NOT",  
35e27 33 2c 20 31 2c 20 46 54 53 51 55 45 52 59 5f 4e  3, 1, FTSQUERY_N
35e28 4f 54 20 20 7d 2c 0a 20 20 20 20 7b 20 22 4e 45  OT  },.    { "NE
35e29 41 52 22 2c 20 34 2c 20 30 2c 20 46 54 53 51 55  AR", 4, 0, FTSQU
35e2a 45 52 59 5f 4e 45 41 52 20 7d 0a 20 20 7d 3b 0a  ERY_NEAR }.  };.
35e2b 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
35e2c 69 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  iCol;.  int iCol
35e2d 4c 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  Len;.  int rc;. 
35e2e 20 46 74 73 33 45 78 70 72 20 2a 70 52 65 74 20   Fts3Expr *pRet 
35e2f 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68  = 0;..  const ch
35e30 61 72 20 2a 7a 49 6e 70 75 74 20 3d 20 7a 3b 0a  ar *zInput = z;.
35e31 20 20 69 6e 74 20 6e 49 6e 70 75 74 20 3d 20 6e    int nInput = n
35e32 3b 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 6f 76 65  ;..  /* Skip ove
35e33 72 20 61 6e 79 20 77 68 69 74 65 73 70 61 63 65  r any whitespace
35e34 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69 6e 67   before checking
35e35 20 66 6f 72 20 61 20 6b 65 79 77 6f 72 64 2c 20   for a keyword, 
35e36 61 6e 20 6f 70 65 6e 20 6f 72 0a 20 20 2a 2a 20  an open or.  ** 
35e37 63 6c 6f 73 65 20 62 72 61 63 6b 65 74 2c 20 6f  close bracket, o
35e38 72 20 61 20 71 75 6f 74 65 64 20 73 74 72 69 6e  r a quoted strin
35e39 67 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65  g. .  */.  while
35e3a 28 20 6e 49 6e 70 75 74 3e 30 20 26 26 20 66 74  ( nInput>0 && ft
35e3b 73 33 69 73 73 70 61 63 65 28 2a 7a 49 6e 70 75  s3isspace(*zInpu
35e3c 74 29 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75 74  t) ){.    nInput
35e3d 2d 2d 3b 0a 20 20 20 20 7a 49 6e 70 75 74 2b 2b  --;.    zInput++
35e3e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 49 6e 70  ;.  }.  if( nInp
35e3f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ut==0 ){.    ret
35e40 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
35e41 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69  .  }..  /* See i
35e42 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  f we are dealing
35e43 20 77 69 74 68 20 61 20 6b 65 79 77 6f 72 64 2e   with a keyword.
35e44 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   */.  for(ii=0; 
35e45 69 69 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28  ii<(int)(sizeof(
35e46 61 4b 65 79 77 6f 72 64 29 2f 73 69 7a 65 6f 66  aKeyword)/sizeof
35e47 28 73 74 72 75 63 74 20 46 74 73 33 4b 65 79 77  (struct Fts3Keyw
35e48 6f 72 64 29 29 3b 20 69 69 2b 2b 29 7b 0a 20 20  ord)); ii++){.  
35e49 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 46    const struct F
35e4a 74 73 33 4b 65 79 77 6f 72 64 20 2a 70 4b 65 79  ts3Keyword *pKey
35e4b 20 3d 20 26 61 4b 65 79 77 6f 72 64 5b 69 69 5d   = &aKeyword[ii]
35e4c 3b 0a 0a 20 20 20 20 69 66 28 20 28 70 4b 65 79  ;..    if( (pKey
35e4d 2d 3e 70 61 72 65 6e 4f 6e 6c 79 20 26 20 7e 73  ->parenOnly & ~s
35e4e 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62  qlite3_fts3_enab
35e4f 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 29 21  le_parentheses)!
35e50 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74  =0 ){.      cont
35e51 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  inue;.    }..   
35e52 20 69 66 28 20 6e 49 6e 70 75 74 3e 3d 70 4b 65   if( nInput>=pKe
35e53 79 2d 3e 6e 20 26 26 20 30 3d 3d 6d 65 6d 63 6d  y->n && 0==memcm
35e54 70 28 7a 49 6e 70 75 74 2c 20 70 4b 65 79 2d 3e  p(zInput, pKey->
35e55 7a 2c 20 70 4b 65 79 2d 3e 6e 29 20 29 7b 0a 20  z, pKey->n) ){. 
35e56 20 20 20 20 20 69 6e 74 20 6e 4e 65 61 72 20 3d       int nNear =
35e57 20 53 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46   SQLITE_FTS3_DEF
35e58 41 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 3b  AULT_NEAR_PARAM;
35e59 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20  .      int nKey 
35e5a 3d 20 70 4b 65 79 2d 3e 6e 3b 0a 20 20 20 20 20  = pKey->n;.     
35e5b 20 63 68 61 72 20 63 4e 65 78 74 3b 0a 0a 20 20   char cNext;..  
35e5c 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
35e5d 73 20 61 20 22 4e 45 41 52 22 20 6b 65 79 77 6f  s a "NEAR" keywo
35e5e 72 64 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e  rd, check for an
35e5f 20 65 78 70 6c 69 63 69 74 20 6e 65 61 72 6e 65   explicit nearne
35e60 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ss. */.      if(
35e61 20 70 4b 65 79 2d 3e 65 54 79 70 65 3d 3d 46 54   pKey->eType==FT
35e62 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20  SQUERY_NEAR ){. 
35e63 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e         assert( n
35e64 4b 65 79 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20  Key==4 );.      
35e65 20 20 69 66 28 20 7a 49 6e 70 75 74 5b 34 5d 3d    if( zInput[4]=
35e66 3d 27 2f 27 20 26 26 20 7a 49 6e 70 75 74 5b 35  ='/' && zInput[5
35e67 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70 75 74  ]>='0' && zInput
35e68 5b 35 5d 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20  [5]<='9' ){.    
35e69 20 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 30 3b        nNear = 0;
35e6a 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e  .          for(n
35e6b 4b 65 79 3d 35 3b 20 7a 49 6e 70 75 74 5b 6e 4b  Key=5; zInput[nK
35e6c 65 79 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70  ey]>='0' && zInp
35e6d 75 74 5b 6e 4b 65 79 5d 3c 3d 27 39 27 3b 20 6e  ut[nKey]<='9'; n
35e6e 4b 65 79 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Key++){.        
35e6f 20 20 20 20 6e 4e 65 61 72 20 3d 20 6e 4e 65 61      nNear = nNea
35e70 72 20 2a 20 31 30 20 2b 20 28 7a 49 6e 70 75 74  r * 10 + (zInput
35e71 5b 6e 4b 65 79 5d 20 2d 20 27 30 27 29 3b 0a 20  [nKey] - '0');. 
35e72 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
35e73 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
35e74 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70      /* At this p
35e75 6f 69 6e 74 20 74 68 69 73 20 69 73 20 70 72 6f  oint this is pro
35e76 62 61 62 6c 79 20 61 20 6b 65 79 77 6f 72 64 2e  bably a keyword.
35e77 20 42 75 74 20 66 6f 72 20 74 68 61 74 20 74 6f   But for that to
35e78 20 62 65 20 74 72 75 65 2c 0a 20 20 20 20 20 20   be true,.      
35e79 2a 2a 20 74 68 65 20 6e 65 78 74 20 62 79 74 65  ** the next byte
35e7a 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 65 69   must contain ei
35e7b 74 68 65 72 20 77 68 69 74 65 73 70 61 63 65 2c  ther whitespace,
35e7c 20 61 6e 20 6f 70 65 6e 20 6f 72 20 63 6c 6f 73   an open or clos
35e7d 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 65 6e  e.      ** paren
35e7e 74 68 65 73 69 73 2c 20 61 20 71 75 6f 74 65 20  thesis, a quote 
35e7f 63 68 61 72 61 63 74 65 72 2c 20 6f 72 20 45 4f  character, or EO
35e80 46 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  F. .      */.   
35e81 20 20 20 63 4e 65 78 74 20 3d 20 7a 49 6e 70 75     cNext = zInpu
35e82 74 5b 6e 4b 65 79 5d 3b 0a 20 20 20 20 20 20 69  t[nKey];.      i
35e83 66 28 20 66 74 73 33 69 73 73 70 61 63 65 28 63  f( fts3isspace(c
35e84 4e 65 78 74 29 20 0a 20 20 20 20 20 20 20 7c 7c  Next) .       ||
35e85 20 63 4e 65 78 74 3d 3d 27 22 27 20 7c 7c 20 63   cNext=='"' || c
35e86 4e 65 78 74 3d 3d 27 28 27 20 7c 7c 20 63 4e 65  Next=='(' || cNe
35e87 78 74 3d 3d 27 29 27 20 7c 7c 20 63 4e 65 78 74  xt==')' || cNext
35e88 3d 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  ==0.      ){.   
35e89 20 20 20 20 20 70 52 65 74 20 3d 20 28 46 74 73       pRet = (Fts
35e8a 33 45 78 70 72 20 2a 29 73 71 6c 69 74 65 33 5f  3Expr *)sqlite3_
35e8b 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74  malloc(sizeof(Ft
35e8c 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20  s3Expr));.      
35e8d 20 20 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20    if( !pRet ){. 
35e8e 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
35e8f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
35e90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
35e91 6d 65 6d 73 65 74 28 70 52 65 74 2c 20 30 2c 20  memset(pRet, 0, 
35e92 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29  sizeof(Fts3Expr)
35e93 29 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  );.        pRet-
35e94 3e 65 54 79 70 65 20 3d 20 70 4b 65 79 2d 3e 65  >eType = pKey->e
35e95 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 70 52  Type;.        pR
35e96 65 74 2d 3e 6e 4e 65 61 72 20 3d 20 6e 4e 65 61  et->nNear = nNea
35e97 72 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 45 78  r;.        *ppEx
35e98 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 20 20 20  pr = pRet;.     
35e99 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d     *pnConsumed =
35e9a 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20   (zInput - z) + 
35e9b 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 72 65  nKey;.        re
35e9c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
35e9d 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
35e9e 2a 20 54 75 72 6e 73 20 6f 75 74 20 74 68 61 74  * Turns out that
35e9f 20 77 61 73 6e 27 74 20 61 20 6b 65 79 77 6f 72   wasn't a keywor
35ea0 64 20 61 66 74 65 72 20 61 6c 6c 2e 20 54 68 69  d after all. Thi
35ea1 73 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 65  s happens if the
35ea2 0a 20 20 20 20 20 20 2a 2a 20 75 73 65 72 20 68  .      ** user h
35ea3 61 73 20 73 75 70 70 6c 69 65 64 20 61 20 74 6f  as supplied a to
35ea4 6b 65 6e 20 73 75 63 68 20 61 73 20 22 4f 52 61  ken such as "ORa
35ea5 63 6c 65 22 2e 20 43 6f 6e 74 69 6e 75 65 2e 0a  cle". Continue..
35ea6 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 7d 0a 20        */.    }. 
35ea7 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66   }..  /* Check f
35ea8 6f 72 20 61 6e 20 6f 70 65 6e 20 62 72 61 63 6b  or an open brack
35ea9 65 74 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  et. */.  if( sql
35eaa 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65  ite3_fts3_enable
35eab 5f 70 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0a  _parentheses ){.
35eac 20 20 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d      if( *zInput=
35ead 3d 27 28 27 20 29 7b 0a 20 20 20 20 20 20 69 6e  ='(' ){.      in
35eae 74 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a 20 20 20  t nConsumed;.   
35eaf 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20     int rc;.     
35eb0 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2b 2b   pParse->nNest++
35eb1 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  ;.      rc = fts
35eb2 33 45 78 70 72 50 61 72 73 65 28 70 50 61 72 73  3ExprParse(pPars
35eb3 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c 20 6e  e, &zInput[1], n
35eb4 49 6e 70 75 74 2d 31 2c 20 70 70 45 78 70 72 2c  Input-1, ppExpr,
35eb5 20 26 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20   &nConsumed);.  
35eb6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
35eb7 54 45 5f 4f 4b 20 26 26 20 21 2a 70 70 45 78 70  TE_OK && !*ppExp
35eb8 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  r ){.        rc 
35eb9 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
35eba 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 6e       }.      *pn
35ebb 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e 70  Consumed = (zInp
35ebc 75 74 20 2d 20 7a 29 20 2b 20 31 20 2b 20 6e 43  ut - z) + 1 + nC
35ebd 6f 6e 73 75 6d 65 64 3b 0a 20 20 20 20 20 20 72  onsumed;.      r
35ebe 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
35ebf 20 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20    .    /* Check 
35ec0 66 6f 72 20 61 20 63 6c 6f 73 65 20 62 72 61 63  for a close brac
35ec1 6b 65 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ket. */.    if( 
35ec2 2a 7a 49 6e 70 75 74 3d 3d 27 29 27 20 29 7b 0a  *zInput==')' ){.
35ec3 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4e        pParse->nN
35ec4 65 73 74 2d 2d 3b 0a 20 20 20 20 20 20 2a 70 6e  est--;.      *pn
35ec5 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e 70  Consumed = (zInp
35ec6 75 74 20 2d 20 7a 29 20 2b 20 31 3b 0a 20 20 20  ut - z) + 1;.   
35ec7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
35ec8 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _DONE;.    }.  }
35ec9 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65  ..  /* See if we
35eca 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
35ecb 68 20 61 20 71 75 6f 74 65 64 20 70 68 72 61 73  h a quoted phras
35ecc 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68  e. If this is th
35ecd 65 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a  e case, then.  *
35ece 2a 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65  * search for the
35ecf 20 63 6c 6f 73 69 6e 67 20 71 75 6f 74 65 20 61   closing quote a
35ed0 6e 64 20 70 61 73 73 20 74 68 65 20 77 68 6f 6c  nd pass the whol
35ed1 65 20 73 74 72 69 6e 67 20 74 6f 20 67 65 74 4e  e string to getN
35ed2 65 78 74 53 74 72 69 6e 67 28 29 0a 20 20 2a 2a  extString().  **
35ed3 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e   for processing.
35ed4 20 54 68 69 73 20 69 73 20 65 61 73 79 20 74 6f   This is easy to
35ed5 20 64 6f 2c 20 61 73 20 66 74 73 33 20 68 61 73   do, as fts3 has
35ed6 20 6e 6f 20 73 79 6e 74 61 78 20 66 6f 72 20 65   no syntax for e
35ed7 73 63 61 70 69 6e 67 0a 20 20 2a 2a 20 61 20 71  scaping.  ** a q
35ed8 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 20 65  uote character e
35ed9 6d 62 65 64 64 65 64 20 69 6e 20 61 20 73 74 72  mbedded in a str
35eda 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ing..  */.  if( 
35edb 2a 7a 49 6e 70 75 74 3d 3d 27 22 27 20 29 7b 0a  *zInput=='"' ){.
35edc 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69      for(ii=1; ii
35edd 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49 6e 70 75  <nInput && zInpu
35ede 74 5b 69 69 5d 21 3d 27 22 27 3b 20 69 69 2b 2b  t[ii]!='"'; ii++
35edf 29 3b 0a 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d  );.    *pnConsum
35ee0 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a  ed = (zInput - z
35ee1 29 20 2b 20 69 69 20 2b 20 31 3b 0a 20 20 20 20  ) + ii + 1;.    
35ee2 69 66 28 20 69 69 3d 3d 6e 49 6e 70 75 74 20 29  if( ii==nInput )
35ee3 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
35ee4 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
35ee5 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 67 65   }.    return ge
35ee6 74 4e 65 78 74 53 74 72 69 6e 67 28 70 50 61 72  tNextString(pPar
35ee7 73 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c 20  se, &zInput[1], 
35ee8 69 69 2d 31 2c 20 70 70 45 78 70 72 29 3b 0a 20  ii-1, ppExpr);. 
35ee9 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e   }...  /* If con
35eea 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68  trol flows to th
35eeb 69 73 20 70 6f 69 6e 74 2c 20 74 68 69 73 20 6d  is point, this m
35eec 75 73 74 20 62 65 20 61 20 72 65 67 75 6c 61 72  ust be a regular
35eed 20 74 6f 6b 65 6e 2c 20 6f 72 20 0a 20 20 2a 2a   token, or .  **
35eee 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
35eef 69 6e 70 75 74 2e 20 52 65 61 64 20 61 20 72 65  input. Read a re
35ef0 67 75 6c 61 72 20 74 6f 6b 65 6e 20 75 73 69 6e  gular token usin
35ef1 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f  g the sqlite3_to
35ef2 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6e 74  kenizer.  ** int
35ef3 65 72 66 61 63 65 2e 20 42 65 66 6f 72 65 20 64  erface. Before d
35ef4 6f 69 6e 67 20 73 6f 2c 20 66 69 67 75 72 65 20  oing so, figure 
35ef5 6f 75 74 20 69 66 20 74 68 65 72 65 20 69 73 20  out if there is 
35ef6 61 6e 20 65 78 70 6c 69 63 69 74 0a 20 20 2a 2a  an explicit.  **
35ef7 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69 65   column specifie
35ef8 72 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 2e  r for the token.
35ef9 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f   .  **.  ** TODO
35efa 3a 20 53 74 72 61 6e 67 65 6c 79 2c 20 69 74 20  : Strangely, it 
35efb 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
35efc 74 6f 20 61 73 73 6f 63 69 61 74 65 20 61 20 63  to associate a c
35efd 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69 65 72 0a  olumn specifier.
35efe 20 20 2a 2a 20 77 69 74 68 20 61 20 71 75 6f 74    ** with a quot
35eff 65 64 20 70 68 72 61 73 65 2c 20 6f 6e 6c 79 20  ed phrase, only 
35f00 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 6f  with a single to
35f01 6b 65 6e 2e 20 4e 6f 74 20 73 75 72 65 20 69 66  ken. Not sure if
35f02 20 74 68 69 73 20 77 61 73 0a 20 20 2a 2a 20 61   this was.  ** a
35f03 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
35f04 20 61 72 74 69 66 61 63 74 20 6f 72 20 61 6e 20   artifact or an 
35f05 69 6e 74 65 6e 74 69 6f 6e 61 6c 20 64 65 63 69  intentional deci
35f06 73 69 6f 6e 20 77 68 65 6e 20 66 74 73 33 20 77  sion when fts3 w
35f07 61 73 0a 20 20 2a 2a 20 66 69 72 73 74 20 69 6d  as.  ** first im
35f08 70 6c 65 6d 65 6e 74 65 64 2e 20 57 68 69 63 68  plemented. Which
35f09 65 76 65 72 20 69 74 20 77 61 73 2c 20 74 68 69  ever it was, thi
35f0a 73 20 6d 6f 64 75 6c 65 20 64 75 70 6c 69 63 61  s module duplica
35f0b 74 65 73 20 74 68 65 20 0a 20 20 2a 2a 20 6c 69  tes the .  ** li
35f0c 6d 69 74 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  mitation..  */. 
35f0d 20 69 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e   iCol = pParse->
35f0e 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 69  iDefaultCol;.  i
35f0f 43 6f 6c 4c 65 6e 20 3d 20 30 3b 0a 20 20 66 6f  ColLen = 0;.  fo
35f10 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 72 73  r(ii=0; ii<pPars
35f11 65 2d 3e 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a  e->nCol; ii++){.
35f12 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
35f13 7a 53 74 72 20 3d 20 70 50 61 72 73 65 2d 3e 61  zStr = pParse->a
35f14 7a 43 6f 6c 5b 69 69 5d 3b 0a 20 20 20 20 69 6e  zCol[ii];.    in
35f15 74 20 6e 53 74 72 20 3d 20 73 74 72 6c 65 6e 28  t nStr = strlen(
35f16 7a 53 74 72 29 3b 0a 20 20 20 20 69 66 28 20 6e  zStr);.    if( n
35f17 49 6e 70 75 74 3e 6e 53 74 72 20 26 26 20 7a 49  Input>nStr && zI
35f18 6e 70 75 74 5b 6e 53 74 72 5d 3d 3d 27 3a 27 20  nput[nStr]==':' 
35f19 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33  .     && sqlite3
35f1a 5f 73 74 72 6e 69 63 6d 70 28 7a 53 74 72 2c 20  _strnicmp(zStr, 
35f1b 7a 49 6e 70 75 74 2c 20 6e 53 74 72 29 3d 3d 30  zInput, nStr)==0
35f1c 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69   .    ){.      i
35f1d 43 6f 6c 20 3d 20 69 69 3b 0a 20 20 20 20 20 20  Col = ii;.      
35f1e 69 43 6f 6c 4c 65 6e 20 3d 20 28 28 7a 49 6e 70  iColLen = ((zInp
35f1f 75 74 20 2d 20 7a 29 20 2b 20 6e 53 74 72 20 2b  ut - z) + nStr +
35f20 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
35f21 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63  ;.    }.  }.  rc
35f22 20 3d 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e 28   = getNextToken(
35f23 70 50 61 72 73 65 2c 20 69 43 6f 6c 2c 20 26 7a  pParse, iCol, &z
35f24 5b 69 43 6f 6c 4c 65 6e 5d 2c 20 6e 2d 69 43 6f  [iColLen], n-iCo
35f25 6c 4c 65 6e 2c 20 70 70 45 78 70 72 2c 20 70 6e  lLen, ppExpr, pn
35f26 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 2a 70 6e  Consumed);.  *pn
35f27 43 6f 6e 73 75 6d 65 64 20 2b 3d 20 69 43 6f 6c  Consumed += iCol
35f28 4c 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  Len;.  return rc
35f29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  ;.}../*.** The a
35f2a 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 46 74  rgument is an Ft
35f2b 73 33 45 78 70 72 20 73 74 72 75 63 74 75 72 65  s3Expr structure
35f2c 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 6f 70   for a binary op
35f2d 65 72 61 74 6f 72 20 28 61 6e 79 20 74 79 70 65  erator (any type
35f2e 0a 2a 2a 20 65 78 63 65 70 74 20 61 6e 20 46 54  .** except an FT
35f2f 53 51 55 45 52 59 5f 50 48 52 41 53 45 29 2e 20  SQUERY_PHRASE). 
35f30 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65  Return an intege
35f31 72 20 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e  r value represen
35f32 74 69 6e 67 20 74 68 65 0a 2a 2a 20 70 72 65 63  ting the.** prec
35f33 65 64 65 6e 63 65 20 6f 66 20 74 68 65 20 6f 70  edence of the op
35f34 65 72 61 74 6f 72 2e 20 4c 6f 77 65 72 20 76 61  erator. Lower va
35f35 6c 75 65 73 20 68 61 76 65 20 61 20 68 69 67 68  lues have a high
35f36 65 72 20 70 72 65 63 65 64 65 6e 63 65 20 28 69  er precedence (i
35f37 2e 65 2e 0a 2a 2a 20 67 72 6f 75 70 20 6d 6f 72  .e..** group mor
35f38 65 20 74 69 67 68 74 6c 79 29 2e 20 46 6f 72 20  e tightly). For 
35f39 65 78 61 6d 70 6c 65 2c 20 69 6e 20 74 68 65 20  example, in the 
35f3a 43 20 6c 61 6e 67 75 61 67 65 2c 20 74 68 65 20  C language, the 
35f3b 3d 3d 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 67  == operator.** g
35f3c 72 6f 75 70 73 20 6d 6f 72 65 20 74 69 67 68 74  roups more tight
35f3d 6c 79 20 74 68 61 6e 20 7c 7c 2c 20 61 6e 64 20  ly than ||, and 
35f3e 77 6f 75 6c 64 20 74 68 65 72 65 66 6f 72 65 20  would therefore 
35f3f 68 61 76 65 20 61 20 68 69 67 68 65 72 20 70 72  have a higher pr
35f40 65 63 65 64 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  ecedence..**.** 
35f41 57 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20 6e  When using the n
35f42 65 77 20 66 74 73 33 20 71 75 65 72 79 20 73 79  ew fts3 query sy
35f43 6e 74 61 78 20 28 77 68 65 6e 20 53 51 4c 49 54  ntax (when SQLIT
35f44 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41  E_ENABLE_FTS3_PA
35f45 52 45 4e 54 48 45 53 49 53 0a 2a 2a 20 69 73 20  RENTHESIS.** is 
35f46 64 65 66 69 6e 65 64 29 2c 20 74 68 65 20 6f 72  defined), the or
35f47 64 65 72 20 6f 66 20 74 68 65 20 6f 70 65 72 61  der of the opera
35f48 74 6f 72 73 20 69 6e 20 70 72 65 63 65 64 65 6e  tors in preceden
35f49 63 65 20 66 72 6f 6d 20 68 69 67 68 65 73 74 20  ce from highest 
35f4a 74 6f 0a 2a 2a 20 6c 6f 77 65 73 74 20 69 73 3a  to.** lowest is:
35f4b 0a 2a 2a 0a 2a 2a 20 20 20 4e 45 41 52 0a 2a 2a  .**.**   NEAR.**
35f4c 20 20 20 4e 4f 54 0a 2a 2a 20 20 20 41 4e 44 20     NOT.**   AND 
35f4d 28 69 6e 63 6c 75 64 69 6e 67 20 69 6d 70 6c 69  (including impli
35f4e 63 69 74 20 41 4e 44 73 29 0a 2a 2a 20 20 20 4f  cit ANDs).**   O
35f4f 52 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  R.**.** Note tha
35f50 74 20 77 68 65 6e 20 75 73 69 6e 67 20 74 68 65  t when using the
35f51 20 6f 6c 64 20 71 75 65 72 79 20 73 79 6e 74 61   old query synta
35f52 78 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61 74  x, the OR operat
35f53 6f 72 20 68 61 73 20 61 20 68 69 67 68 65 72 0a  or has a higher.
35f54 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20 74 68  ** precedence th
35f55 61 6e 20 74 68 65 20 41 4e 44 20 6f 70 65 72 61  an the AND opera
35f56 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tor..*/.static i
35f57 6e 74 20 6f 70 50 72 65 63 65 64 65 6e 63 65 28  nt opPrecedence(
35f58 46 74 73 33 45 78 70 72 20 2a 70 29 7b 0a 20 20  Fts3Expr *p){.  
35f59 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79 70 65  assert( p->eType
35f5a 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  !=FTSQUERY_PHRAS
35f5b 45 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  E );.  if( sqlit
35f5c 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
35f5d 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0a 20 20  arentheses ){.  
35f5e 20 20 72 65 74 75 72 6e 20 70 2d 3e 65 54 79 70    return p->eTyp
35f5f 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  e;.  }else if( p
35f60 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
35f61 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 72 65  Y_NEAR ){.    re
35f62 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20  turn 1;.  }else 
35f63 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54  if( p->eType==FT
35f64 53 51 55 45 52 59 5f 4f 52 20 29 7b 0a 20 20 20  SQUERY_OR ){.   
35f65 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20   return 2;.  }. 
35f66 20 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79 70   assert( p->eTyp
35f67 65 3d 3d 46 54 53 51 55 45 52 59 5f 41 4e 44 20  e==FTSQUERY_AND 
35f68 29 3b 0a 20 20 72 65 74 75 72 6e 20 33 3b 0a 7d  );.  return 3;.}
35f69 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
35f6a 20 70 70 48 65 61 64 20 63 6f 6e 74 61 69 6e 73   ppHead contains
35f6b 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
35f6c 65 20 63 75 72 72 65 6e 74 20 68 65 61 64 20 6f  e current head o
35f6d 66 20 61 20 71 75 65 72 79 20 0a 2a 2a 20 65 78  f a query .** ex
35f6e 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 62 65  pression tree be
35f6f 69 6e 67 20 70 61 72 73 65 64 2e 20 70 50 72 65  ing parsed. pPre
35f70 76 20 69 73 20 74 68 65 20 65 78 70 72 65 73 73  v is the express
35f71 69 6f 6e 20 6e 6f 64 65 20 6d 6f 73 74 20 72 65  ion node most re
35f72 63 65 6e 74 6c 79 0a 2a 2a 20 69 6e 73 65 72 74  cently.** insert
35f73 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72 65 65  ed into the tree
35f74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
35f75 61 64 64 73 20 70 4e 65 77 2c 20 77 68 69 63 68  adds pNew, which
35f76 20 69 73 20 61 6c 77 61 79 73 20 61 20 62 69 6e   is always a bin
35f77 61 72 79 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  ary.** operator 
35f78 6e 6f 64 65 2c 20 69 6e 74 6f 20 74 68 65 20 65  node, into the e
35f79 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 62  xpression tree b
35f7a 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6c 61  ased on the rela
35f7b 74 69 76 65 20 70 72 65 63 65 64 65 6e 63 65 0a  tive precedence.
35f7c 2a 2a 20 6f 66 20 70 4e 65 77 20 61 6e 64 20 74  ** of pNew and t
35f7d 68 65 20 65 78 69 73 74 69 6e 67 20 6e 6f 64 65  he existing node
35f7e 73 20 6f 66 20 74 68 65 20 74 72 65 65 2e 20 54  s of the tree. T
35f7f 68 69 73 20 6d 61 79 20 72 65 73 75 6c 74 20 69  his may result i
35f80 6e 20 74 68 65 20 68 65 61 64 0a 2a 2a 20 6f 66  n the head.** of
35f81 20 74 68 65 20 74 72 65 65 20 63 68 61 6e 67 69   the tree changi
35f82 6e 67 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73  ng, in which cas
35f83 65 20 2a 70 70 48 65 61 64 20 69 73 20 73 65 74  e *ppHead is set
35f84 20 74 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74   to the new root
35f85 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   node..*/.static
35f86 20 76 6f 69 64 20 69 6e 73 65 72 74 42 69 6e 61   void insertBina
35f87 72 79 4f 70 65 72 61 74 6f 72 28 0a 20 20 46 74  ryOperator(.  Ft
35f88 73 33 45 78 70 72 20 2a 2a 70 70 48 65 61 64 2c  s3Expr **ppHead,
35f89 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
35f8a 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f  r to the root no
35f8b 64 65 20 6f 66 20 61 20 74 72 65 65 20 2a 2f 0a  de of a tree */.
35f8c 20 20 46 74 73 33 45 78 70 72 20 2a 70 50 72 65    Fts3Expr *pPre
35f8d 76 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  v,         /* No
35f8e 64 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  de most recently
35f8f 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
35f90 68 65 20 74 72 65 65 20 2a 2f 0a 20 20 46 74 73  he tree */.  Fts
35f91 33 45 78 70 72 20 2a 70 4e 65 77 20 20 20 20 20  3Expr *pNew     
35f92 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 62 69 6e        /* New bin
35f93 61 72 79 20 6e 6f 64 65 20 74 6f 20 69 6e 73 65  ary node to inse
35f94 72 74 20 69 6e 74 6f 20 65 78 70 72 65 73 73 69  rt into expressi
35f95 6f 6e 20 74 72 65 65 20 2a 2f 0a 29 7b 0a 20 20  on tree */.){.  
35f96 46 74 73 33 45 78 70 72 20 2a 70 53 70 6c 69 74  Fts3Expr *pSplit
35f97 20 3d 20 70 50 72 65 76 3b 0a 20 20 77 68 69 6c   = pPrev;.  whil
35f98 65 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65  e( pSplit->pPare
35f99 6e 74 20 26 26 20 6f 70 50 72 65 63 65 64 65 6e  nt && opPreceden
35f9a 63 65 28 70 53 70 6c 69 74 2d 3e 70 50 61 72 65  ce(pSplit->pPare
35f9b 6e 74 29 3c 3d 6f 70 50 72 65 63 65 64 65 6e 63  nt)<=opPrecedenc
35f9c 65 28 70 4e 65 77 29 20 29 7b 0a 20 20 20 20 70  e(pNew) ){.    p
35f9d 53 70 6c 69 74 20 3d 20 70 53 70 6c 69 74 2d 3e  Split = pSplit->
35f9e 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 0a 20 20  pParent;.  }..  
35f9f 69 66 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72  if( pSplit->pPar
35fa0 65 6e 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ent ){.    asser
35fa1 74 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65  t( pSplit->pPare
35fa2 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70 53 70 6c  nt->pRight==pSpl
35fa3 69 74 20 29 3b 0a 20 20 20 20 70 53 70 6c 69 74  it );.    pSplit
35fa4 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
35fa5 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 4e  t = pNew;.    pN
35fa6 65 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 53  ew->pParent = pS
35fa7 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  plit->pParent;. 
35fa8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 48   }else{.    *ppH
35fa9 65 61 64 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ead = pNew;.  }.
35faa 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20    pNew->pLeft = 
35fab 70 53 70 6c 69 74 3b 0a 20 20 70 53 70 6c 69 74  pSplit;.  pSplit
35fac 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 65 77  ->pParent = pNew
35fad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65  ;.}../*.** Parse
35fae 20 74 68 65 20 66 74 73 33 20 71 75 65 72 79 20   the fts3 query 
35faf 65 78 70 72 65 73 73 69 6f 6e 20 66 6f 75 6e 64  expression found
35fb0 20 69 6e 20 62 75 66 66 65 72 20 7a 2c 20 6c 65   in buffer z, le
35fb1 6e 67 74 68 20 6e 2e 20 54 68 69 73 20 66 75 6e  ngth n. This fun
35fb2 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e 73  ction.** returns
35fb3 20 65 69 74 68 65 72 20 77 68 65 6e 20 74 68 65   either when the
35fb4 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66   end of the buff
35fb5 65 72 20 69 73 20 72 65 61 63 68 65 64 20 6f 72  er is reached or
35fb6 20 61 6e 20 75 6e 6d 61 74 63 68 65 64 20 0a 2a   an unmatched .*
35fb7 2a 20 63 6c 6f 73 69 6e 67 20 62 72 61 63 6b 65  * closing bracke
35fb8 74 20 2d 20 27 29 27 20 2d 20 69 73 20 65 6e 63  t - ')' - is enc
35fb9 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  ountered..**.** 
35fba 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53  If successful, S
35fbb 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
35fbc 72 6e 65 64 2c 20 2a 70 70 45 78 70 72 20 69 73  rned, *ppExpr is
35fbd 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
35fbe 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 64 20 66   the.** parsed f
35fbf 6f 72 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65  orm of the expre
35fc0 73 73 69 6f 6e 20 61 6e 64 20 2a 70 6e 43 6f 6e  ssion and *pnCon
35fc1 73 75 6d 65 64 20 69 73 20 73 65 74 20 74 6f 20  sumed is set to 
35fc2 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
35fc3 20 62 79 74 65 73 20 72 65 61 64 20 66 72 6f 6d   bytes read from
35fc4 20 62 75 66 66 65 72 20 7a 2e 20 4f 74 68 65 72   buffer z. Other
35fc5 77 69 73 65 2c 20 2a 70 70 45 78 70 72 20 69 73  wise, *ppExpr is
35fc6 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51   set to 0 and SQ
35fc7 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 6f  LITE_NOMEM.** (o
35fc8 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65 72 72  ut of memory err
35fc9 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52  or) or SQLITE_ER
35fca 52 4f 52 20 28 70 61 72 73 65 20 65 72 72 6f 72  ROR (parse error
35fcb 29 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  ) is returned..*
35fcc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
35fcd 33 45 78 70 72 50 61 72 73 65 28 0a 20 20 50 61  3ExprParse(.  Pa
35fce 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72  rseContext *pPar
35fcf 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
35fd0 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75        /* fts3 qu
35fd1 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78  ery parse contex
35fd2 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
35fd3 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20  r *z, int n,    
35fd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35fd5 2a 20 54 65 78 74 20 6f 66 20 4d 41 54 43 48 20  * Text of MATCH 
35fd6 71 75 65 72 79 20 2a 2f 0a 20 20 46 74 73 33 45  query */.  Fts3E
35fd7 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20  xpr **ppExpr,   
35fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35fd9 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73 65     /* OUT: Parse
35fda 64 20 71 75 65 72 79 20 73 74 72 75 63 74 75 72  d query structur
35fdb 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  e */.  int *pnCo
35fdc 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20 20 20  nsumed          
35fdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35fde 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
35fdf 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20   bytes consumed 
35fe0 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78 70 72  */.){.  Fts3Expr
35fe1 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 46 74   *pRet = 0;.  Ft
35fe2 73 33 45 78 70 72 20 2a 70 50 72 65 76 20 3d 20  s3Expr *pPrev = 
35fe3 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  0;.  Fts3Expr *p
35fe4 4e 6f 74 42 72 61 6e 63 68 20 3d 20 30 3b 20 20  NotBranch = 0;  
35fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35fe6 4f 6e 6c 79 20 75 73 65 64 20 69 6e 20 6c 65 67  Only used in leg
35fe7 61 63 79 20 70 61 72 73 65 20 6d 6f 64 65 20 2a  acy parse mode *
35fe8 2f 0a 20 20 69 6e 74 20 6e 49 6e 20 3d 20 6e 3b  /.  int nIn = n;
35fe9 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
35fea 49 6e 20 3d 20 7a 3b 0a 20 20 69 6e 74 20 72 63  In = z;.  int rc
35feb 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
35fec 69 6e 74 20 69 73 52 65 71 75 69 72 65 50 68 72  int isRequirePhr
35fed 61 73 65 20 3d 20 31 3b 0a 0a 20 20 77 68 69 6c  ase = 1;..  whil
35fee 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
35fef 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78 70 72   ){.    Fts3Expr
35ff0 20 2a 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74   *p = 0;.    int
35ff1 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20 20 20   nByte = 0;.    
35ff2 72 63 20 3d 20 67 65 74 4e 65 78 74 4e 6f 64 65  rc = getNextNode
35ff3 28 70 50 61 72 73 65 2c 20 7a 49 6e 2c 20 6e 49  (pParse, zIn, nI
35ff4 6e 2c 20 26 70 2c 20 26 6e 42 79 74 65 29 3b 0a  n, &p, &nByte);.
35ff5 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
35ff6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
35ff7 6e 74 20 69 73 50 68 72 61 73 65 3b 0a 0a 20 20  nt isPhrase;..  
35ff8 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
35ff9 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
35ffa 65 6e 74 68 65 73 65 73 20 0a 20 20 20 20 20 20  entheses .      
35ffb 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54   && p->eType==FT
35ffc 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 26 26  SQUERY_PHRASE &&
35ffd 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73 4e   p->pPhrase->isN
35ffe 6f 74 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  ot .      ){.   
35fff 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61       /* Create a
36000 6e 20 69 6d 70 6c 69 63 69 74 20 4e 4f 54 20 6f  n implicit NOT o
36001 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20 20 20  perator. */.    
36002 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 4e      Fts3Expr *pN
36003 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ot = sqlite3_mal
36004 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33 45  loc(sizeof(Fts3E
36005 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20 69  xpr));.        i
36006 66 28 20 21 70 4e 6f 74 20 29 7b 0a 20 20 20 20  f( !pNot ){.    
36007 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73        sqlite3Fts
36008 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20 20  3ExprFree(p);.  
36009 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
3600a 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
3600b 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61       goto exprpa
3600c 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  rse_out;.       
3600d 20 7d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65   }.        memse
3600e 74 28 70 4e 6f 74 2c 20 30 2c 20 73 69 7a 65 6f  t(pNot, 0, sizeo
3600f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
36010 20 20 20 20 20 20 70 4e 6f 74 2d 3e 65 54 79 70        pNot->eTyp
36011 65 20 3d 20 46 54 53 51 55 45 52 59 5f 4e 4f 54  e = FTSQUERY_NOT
36012 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e  ;.        pNot->
36013 70 52 69 67 68 74 20 3d 20 70 3b 0a 20 20 20 20  pRight = p;.    
36014 20 20 20 20 69 66 28 20 70 4e 6f 74 42 72 61 6e      if( pNotBran
36015 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ch ){.          
36016 70 4e 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70 4e  pNot->pLeft = pN
36017 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20 20 20  otBranch;.      
36018 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4e 6f 74    }.        pNot
36019 42 72 61 6e 63 68 20 3d 20 70 4e 6f 74 3b 0a 20  Branch = pNot;. 
3601a 20 20 20 20 20 20 20 70 20 3d 20 70 50 72 65 76         p = pPrev
3601b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
3601c 20 20 20 20 20 20 20 69 6e 74 20 65 54 79 70 65         int eType
3601d 20 3d 20 70 2d 3e 65 54 79 70 65 3b 0a 20 20 20   = p->eType;.   
3601e 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79       assert( eTy
3601f 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe!=FTSQUERY_PHR
36020 41 53 45 20 7c 7c 20 21 70 2d 3e 70 50 68 72 61  ASE || !p->pPhra
36021 73 65 2d 3e 69 73 4e 6f 74 20 29 3b 0a 20 20 20  se->isNot );.   
36022 20 20 20 20 20 69 73 50 68 72 61 73 65 20 3d 20       isPhrase = 
36023 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  (eType==FTSQUERY
36024 5f 50 48 52 41 53 45 20 7c 7c 20 70 2d 3e 70 4c  _PHRASE || p->pL
36025 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f  eft);..        /
36026 2a 20 54 68 65 20 69 73 52 65 71 75 69 72 65 50  * The isRequireP
36027 68 72 61 73 65 20 76 61 72 69 61 62 6c 65 20 69  hrase variable i
36028 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69 66  s set to true if
36029 20 61 20 70 68 72 61 73 65 20 6f 72 0a 20 20 20   a phrase or.   
3602a 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65       ** an expre
3602b 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 65 64 20  ssion contained 
3602c 69 6e 20 70 61 72 65 6e 74 68 65 73 69 73 20 69  in parenthesis i
3602d 73 20 72 65 71 75 69 72 65 64 2e 20 49 66 20 61  s required. If a
3602e 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 69 6e 61  .        ** bina
3602f 72 79 20 6f 70 65 72 61 74 6f 72 20 28 41 4e 44  ry operator (AND
36030 2c 20 4f 52 2c 20 4e 4f 54 20 6f 72 20 4e 45 41  , OR, NOT or NEA
36031 52 29 20 69 73 20 65 6e 63 6f 75 6e 74 65 64 20  R) is encounted 
36032 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  when.        ** 
36033 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
36034 69 73 20 73 65 74 2c 20 74 68 69 73 20 69 73 20  is set, this is 
36035 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 0a  a syntax error..
36036 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
36037 20 20 20 69 66 28 20 21 69 73 50 68 72 61 73 65     if( !isPhrase
36038 20 26 26 20 69 73 52 65 71 75 69 72 65 50 68 72   && isRequirePhr
36039 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
3603a 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
3603b 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Free(p);.       
3603c 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
3603d 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20  RROR;.          
3603e 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f  goto exprparse_o
3603f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ut;.        }.  
36040 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50  .        if( isP
36041 68 72 61 73 65 20 26 26 20 21 69 73 52 65 71 75  hrase && !isRequ
36042 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20 20  irePhrase ){.   
36043 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74         /* Insert
36044 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e 44   an implicit AND
36045 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20   operator. */.  
36046 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
36047 20 2a 70 41 6e 64 3b 0a 20 20 20 20 20 20 20 20   *pAnd;.        
36048 20 20 61 73 73 65 72 74 28 20 70 52 65 74 20 26    assert( pRet &
36049 26 20 70 50 72 65 76 20 29 3b 0a 20 20 20 20 20  & pPrev );.     
3604a 20 20 20 20 20 70 41 6e 64 20 3d 20 73 71 6c 69       pAnd = sqli
3604b 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
3604c 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
3604d 20 20 20 20 20 20 20 20 69 66 28 20 21 70 41 6e          if( !pAn
3604e 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  d ){.           
3604f 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
36050 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Free(p);.       
36051 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
36052 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
36053 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61 72      goto exprpar
36054 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  se_out;.        
36055 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65    }.          me
36056 6d 73 65 74 28 70 41 6e 64 2c 20 30 2c 20 73 69  mset(pAnd, 0, si
36057 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b  zeof(Fts3Expr));
36058 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64 2d  .          pAnd-
36059 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55 45 52  >eType = FTSQUER
3605a 59 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20 20 20  Y_AND;.         
3605b 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65   insertBinaryOpe
3605c 72 61 74 6f 72 28 26 70 52 65 74 2c 20 70 50 72  rator(&pRet, pPr
3605d 65 76 2c 20 70 41 6e 64 29 3b 0a 20 20 20 20 20  ev, pAnd);.     
3605e 20 20 20 20 20 70 50 72 65 76 20 3d 20 70 41 6e       pPrev = pAn
3605f 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  d;.        }..  
36060 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 74 65        /* This te
36061 73 74 20 63 61 74 63 68 65 73 20 61 74 74 65 6d  st catches attem
36062 70 74 73 20 74 6f 20 6d 61 6b 65 20 65 69 74 68  pts to make eith
36063 65 72 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20  er operand of a 
36064 4e 45 41 52 0a 20 20 20 20 20 20 20 20 2a 2a 20  NEAR.        ** 
36065 6f 70 65 72 61 74 6f 72 20 73 6f 6d 65 74 68 69  operator somethi
36066 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20  ng other than a 
36067 70 68 72 61 73 65 2e 20 46 6f 72 20 65 78 61 6d  phrase. For exam
36068 70 6c 65 2c 20 65 69 74 68 65 72 20 6f 66 0a 20  ple, either of. 
36069 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f         ** the fo
3606a 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 20 20 20  llowing:.       
3606b 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   **.        **  
3606c 20 20 28 62 72 61 63 6b 65 74 65 64 20 65 78 70    (bracketed exp
3606d 72 65 73 73 69 6f 6e 29 20 4e 45 41 52 20 70 68  ression) NEAR ph
3606e 72 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  rase.        ** 
3606f 20 20 20 70 68 72 61 73 65 20 4e 45 41 52 20 28     phrase NEAR (
36070 62 72 61 63 6b 65 74 65 64 20 65 78 70 72 65 73  bracketed expres
36071 73 69 6f 6e 29 0a 20 20 20 20 20 20 20 20 2a 2a  sion).        **
36072 0a 20 20 20 20 20 20 20 20 2a 2a 20 52 65 74 75  .        ** Retu
36073 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 65  rn an error in e
36074 69 74 68 65 72 20 63 61 73 65 2e 0a 20 20 20 20  ither case..    
36075 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
36076 66 28 20 70 50 72 65 76 20 26 26 20 28 0a 20 20  f( pPrev && (.  
36077 20 20 20 20 20 20 20 20 20 20 28 65 54 79 70 65            (eType
36078 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20  ==FTSQUERY_NEAR 
36079 26 26 20 21 69 73 50 68 72 61 73 65 20 26 26 20  && !isPhrase && 
3607a 70 50 72 65 76 2d 3e 65 54 79 70 65 21 3d 46 54  pPrev->eType!=FT
3607b 53 51 55 45 52 59 5f 50 48 52 41 53 45 29 0a 20  SQUERY_PHRASE). 
3607c 20 20 20 20 20 20 20 20 7c 7c 20 28 65 54 79 70          || (eTyp
3607d 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e!=FTSQUERY_PHRA
3607e 53 45 20 26 26 20 69 73 50 68 72 61 73 65 20 26  SE && isPhrase &
3607f 26 20 70 50 72 65 76 2d 3e 65 54 79 70 65 3d 3d  & pPrev->eType==
36080 46 54 53 51 55 45 52 59 5f 4e 45 41 52 29 0a 20  FTSQUERY_NEAR). 
36081 20 20 20 20 20 20 20 29 29 7b 0a 20 20 20 20 20         )){.     
36082 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33       sqlite3Fts3
36083 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20 20 20  ExprFree(p);.   
36084 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
36085 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
36086 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61 72      goto exprpar
36087 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  se_out;.        
36088 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 69 66 28  }.  .        if(
36089 20 69 73 50 68 72 61 73 65 20 29 7b 0a 20 20 20   isPhrase ){.   
3608a 20 20 20 20 20 20 20 69 66 28 20 70 52 65 74 20         if( pRet 
3608b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
3608c 73 73 65 72 74 28 20 70 50 72 65 76 20 26 26 20  ssert( pPrev && 
3608d 70 50 72 65 76 2d 3e 70 4c 65 66 74 20 26 26 20  pPrev->pLeft && 
3608e 70 50 72 65 76 2d 3e 70 52 69 67 68 74 3d 3d 30  pPrev->pRight==0
3608f 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
36090 70 50 72 65 76 2d 3e 70 52 69 67 68 74 20 3d 20  pPrev->pRight = 
36091 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  p;.            p
36092 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 72 65  ->pParent = pPre
36093 76 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  v;.          }el
36094 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
36095 70 52 65 74 20 3d 20 70 3b 0a 20 20 20 20 20 20  pRet = p;.      
36096 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
36097 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69  lse{.          i
36098 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61  nsertBinaryOpera
36099 74 6f 72 28 26 70 52 65 74 2c 20 70 50 72 65 76  tor(&pRet, pPrev
3609a 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  , p);.        }.
3609b 20 20 20 20 20 20 20 20 69 73 52 65 71 75 69 72          isRequir
3609c 65 50 68 72 61 73 65 20 3d 20 21 69 73 50 68 72  ePhrase = !isPhr
3609d 61 73 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ase;.      }.   
3609e 20 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65     assert( nByte
3609f 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  >0 );.    }.    
360a0 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
360a1 54 45 5f 4f 4b 20 7c 7c 20 28 6e 42 79 74 65 3e  TE_OK || (nByte>
360a2 30 20 26 26 20 6e 42 79 74 65 3c 3d 6e 49 6e 29  0 && nByte<=nIn)
360a3 20 29 3b 0a 20 20 20 20 6e 49 6e 20 2d 3d 20 6e   );.    nIn -= n
360a4 42 79 74 65 3b 0a 20 20 20 20 7a 49 6e 20 2b 3d  Byte;.    zIn +=
360a5 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 50 72 65   nByte;.    pPre
360a6 76 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 69 66  v = p;.  }..  if
360a7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
360a8 45 20 26 26 20 70 52 65 74 20 26 26 20 69 73 52  E && pRet && isR
360a9 65 71 75 69 72 65 50 68 72 61 73 65 20 29 7b 0a  equirePhrase ){.
360aa 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
360ab 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69 66  ERROR;.  }..  if
360ac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
360ad 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  E ){.    rc = SQ
360ae 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 66 28  LITE_OK;.    if(
360af 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65   !sqlite3_fts3_e
360b0 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
360b1 73 20 26 26 20 70 4e 6f 74 42 72 61 6e 63 68 20  s && pNotBranch 
360b2 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70 52  ){.      if( !pR
360b3 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  et ){.        rc
360b4 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
360b5 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
360b6 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
360b7 70 49 74 65 72 20 3d 20 70 4e 6f 74 42 72 61 6e  pIter = pNotBran
360b8 63 68 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ch;.        whil
360b9 65 28 20 70 49 74 65 72 2d 3e 70 4c 65 66 74 20  e( pIter->pLeft 
360ba 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 49 74  ){.          pIt
360bb 65 72 20 3d 20 70 49 74 65 72 2d 3e 70 4c 65 66  er = pIter->pLef
360bc 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
360bd 20 20 20 20 20 70 49 74 65 72 2d 3e 70 4c 65 66       pIter->pLef
360be 74 20 3d 20 70 52 65 74 3b 0a 20 20 20 20 20 20  t = pRet;.      
360bf 20 20 70 52 65 74 20 3d 20 70 4e 6f 74 42 72 61    pRet = pNotBra
360c0 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nch;.      }.   
360c1 20 7d 0a 20 20 7d 0a 20 20 2a 70 6e 43 6f 6e 73   }.  }.  *pnCons
360c2 75 6d 65 64 20 3d 20 6e 20 2d 20 6e 49 6e 3b 0a  umed = n - nIn;.
360c3 0a 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3a 0a  .exprparse_out:.
360c4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
360c5 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
360c6 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
360c7 52 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Ret);.    sqlite
360c8 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70 4e  3Fts3ExprFree(pN
360c9 6f 74 42 72 61 6e 63 68 29 3b 0a 20 20 20 20 70  otBranch);.    p
360ca 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 2a  Ret = 0;.  }.  *
360cb 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b 0a 20  ppExpr = pRet;. 
360cc 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
360cd 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 20  *.** Parameters 
360ce 7a 20 61 6e 64 20 6e 20 63 6f 6e 74 61 69 6e 20  z and n contain 
360cf 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 64  a pointer to and
360d0 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 62 75 66   length of a buf
360d1 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a  fer containing.*
360d2 2a 20 61 6e 20 66 74 73 33 20 71 75 65 72 79 20  * an fts3 query 
360d3 65 78 70 72 65 73 73 69 6f 6e 2c 20 72 65 73 70  expression, resp
360d4 65 63 74 69 76 65 6c 79 2e 20 54 68 69 73 20 66  ectively. This f
360d5 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
360d6 20 74 6f 20 70 61 72 73 65 20 74 68 65 0a 2a 2a   to parse the.**
360d7 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
360d8 6e 20 61 6e 64 20 63 72 65 61 74 65 20 61 20 74  n and create a t
360d9 72 65 65 20 6f 66 20 46 74 73 33 45 78 70 72 20  ree of Fts3Expr 
360da 73 74 72 75 63 74 75 72 65 73 20 72 65 70 72 65  structures repre
360db 73 65 6e 74 69 6e 67 20 74 68 65 0a 2a 2a 20 70  senting the.** p
360dc 61 72 73 65 64 20 65 78 70 72 65 73 73 69 6f 6e  arsed expression
360dd 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
360de 20 2a 70 70 45 78 70 72 20 69 73 20 73 65 74 20   *ppExpr is set 
360df 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
360e0 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 70  head.** of the p
360e1 61 72 73 65 64 20 65 78 70 72 65 73 73 69 6f 6e  arsed expression
360e2 20 74 72 65 65 20 61 6e 64 20 53 51 4c 49 54 45   tree and SQLITE
360e3 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
360e4 20 49 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20   If an error.** 
360e5 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 53  occurs, either S
360e6 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74  QLITE_NOMEM (out
360e7 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72  -of-memory error
360e8 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f  ) or SQLITE_ERRO
360e9 52 20 28 70 61 72 73 65 0a 2a 2a 20 65 72 72 6f  R (parse.** erro
360ea 72 29 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  r) is returned a
360eb 6e 64 20 2a 70 70 45 78 70 72 20 69 73 20 73 65  nd *ppExpr is se
360ec 74 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66  t to 0..**.** If
360ed 20 70 61 72 61 6d 65 74 65 72 20 6e 20 69 73 20   parameter n is 
360ee 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
360ef 72 2c 20 74 68 65 6e 20 7a 20 69 73 20 61 73 73  r, then z is ass
360f0 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f  umed to point to
360f1 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e   a.** nul-termin
360f2 61 74 65 64 20 73 74 72 69 6e 67 20 61 6e 64 20  ated string and 
360f3 74 68 65 20 6c 65 6e 67 74 68 20 69 73 20 64 65  the length is de
360f4 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 73  termined using s
360f5 74 72 6c 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  trlen()..**.** T
360f6 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
360f7 65 72 2c 20 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er, pTokenizer, 
360f8 69 73 20 70 61 73 73 65 64 20 74 68 65 20 66 74  is passed the ft
360f9 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64  s3 tokenizer mod
360fa 75 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20 74 6f  ule to.** use to
360fb 20 6e 6f 72 6d 61 6c 69 7a 65 20 71 75 65 72 79   normalize query
360fc 20 74 6f 6b 65 6e 73 20 77 68 69 6c 65 20 70 61   tokens while pa
360fd 72 73 69 6e 67 20 74 68 65 20 65 78 70 72 65 73  rsing the expres
360fe 73 69 6f 6e 2e 20 54 68 65 20 61 7a 43 6f 6c 5b  sion. The azCol[
360ff 5d 0a 2a 2a 20 61 72 72 61 79 2c 20 77 68 69 63  ].** array, whic
36100 68 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  h is assumed to 
36101 63 6f 6e 74 61 69 6e 20 6e 43 6f 6c 20 65 6e 74  contain nCol ent
36102 72 69 65 73 2c 20 73 68 6f 75 6c 64 20 63 6f 6e  ries, should con
36103 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 0a 2a  tain the names.*
36104 2a 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e  * of each column
36105 20 69 6e 20 74 68 65 20 74 61 72 67 65 74 20 66   in the target f
36106 74 73 33 20 74 61 62 6c 65 2c 20 69 6e 20 6f 72  ts3 table, in or
36107 64 65 72 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f  der from left to
36108 20 72 69 67 68 74 2e 20 0a 2a 2a 20 43 6f 6c 75   right. .** Colu
36109 6d 6e 20 6e 61 6d 65 73 20 6d 75 73 74 20 62 65  mn names must be
3610a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
3610b 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 54  strings..**.** T
3610c 68 65 20 69 44 65 66 61 75 6c 74 43 6f 6c 20 70  he iDefaultCol p
3610d 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
3610e 62 65 20 70 61 73 73 65 64 20 74 68 65 20 69 6e  be passed the in
3610f 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65  dex of the table
36110 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 61 74 20   column.** that 
36111 61 70 70 65 61 72 73 20 6f 6e 20 74 68 65 20 6c  appears on the l
36112 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66  eft-hand-side of
36113 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61   the MATCH opera
36114 74 6f 72 20 28 74 68 65 20 64 65 66 61 75 6c 74  tor (the default
36115 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 6d 61  .** column to ma
36116 74 63 68 20 61 67 61 69 6e 73 74 20 66 6f 72 20  tch against for 
36117 74 6f 6b 65 6e 73 20 66 6f 72 20 77 68 69 63 68  tokens for which
36118 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69   a column name i
36119 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c 79  s not explicitly
3611a 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61 73  .** specified as
3611b 20 70 61 72 74 20 6f 66 20 74 68 65 20 71 75 65   part of the que
3611c 72 79 20 73 74 72 69 6e 67 29 2c 20 6f 72 20 2d  ry string), or -
3611d 31 20 69 66 20 74 6f 6b 65 6e 73 20 6d 61 79 20  1 if tokens may 
3611e 62 79 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d 61  by default.** ma
3611f 74 63 68 20 61 6e 79 20 74 61 62 6c 65 20 63 6f  tch any table co
36120 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lumn..*/.SQLITE_
36121 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
36122 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73 65  te3Fts3ExprParse
36123 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
36124 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
36125 72 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e  r,      /* Token
36126 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20  izer module */. 
36127 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 2c 20 20   char **azCol,  
36128 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36129 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
3612a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f   column names fo
3612b 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a 2f 0a  r fts3 table */.
3612c 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 20 20 20 20    int nCol,     
3612d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3612e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
3612f 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 7a  of entries in az
36130 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  Col[] */.  int i
36131 44 65 66 61 75 6c 74 43 6f 6c 2c 20 20 20 20 20  DefaultCol,     
36132 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36133 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e  * Default column
36134 20 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20 20 63   to query */.  c
36135 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
36136 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  t n,            
36137 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 4d 41     /* Text of MA
36138 54 43 48 20 71 75 65 72 79 20 2a 2f 0a 20 20 46  TCH query */.  F
36139 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72  ts3Expr **ppExpr
3613a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3613b 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73 65     /* OUT: Parse
3613c 64 20 71 75 65 72 79 20 73 74 72 75 63 74 75 72  d query structur
3613d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50  e */.){.  int nP
3613e 61 72 73 65 64 3b 0a 20 20 69 6e 74 20 72 63 3b  arsed;.  int rc;
3613f 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20  .  ParseContext 
36140 73 50 61 72 73 65 3b 0a 20 20 73 50 61 72 73 65  sParse;.  sParse
36141 2e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54  .pTokenizer = pT
36142 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 50 61 72  okenizer;.  sPar
36143 73 65 2e 61 7a 43 6f 6c 20 3d 20 28 63 6f 6e 73  se.azCol = (cons
36144 74 20 63 68 61 72 20 2a 2a 29 61 7a 43 6f 6c 3b  t char **)azCol;
36145 0a 20 20 73 50 61 72 73 65 2e 6e 43 6f 6c 20 3d  .  sParse.nCol =
36146 20 6e 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65 2e   nCol;.  sParse.
36147 69 44 65 66 61 75 6c 74 43 6f 6c 20 3d 20 69 44  iDefaultCol = iD
36148 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 73 50 61  efaultCol;.  sPa
36149 72 73 65 2e 6e 4e 65 73 74 20 3d 20 30 3b 0a 20  rse.nNest = 0;. 
3614a 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20   if( z==0 ){.   
3614b 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20 20   *ppExpr = 0;.  
3614c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3614d 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3c  OK;.  }.  if( n<
3614e 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74 72  0 ){.    n = str
3614f 6c 65 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 72 63  len(z);.  }.  rc
36150 20 3d 20 66 74 73 33 45 78 70 72 50 61 72 73 65   = fts3ExprParse
36151 28 26 73 50 61 72 73 65 2c 20 7a 2c 20 6e 2c 20  (&sParse, z, n, 
36152 70 70 45 78 70 72 2c 20 26 6e 50 61 72 73 65 64  ppExpr, &nParsed
36153 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66  );..  /* Check f
36154 6f 72 20 6d 69 73 6d 61 74 63 68 65 64 20 70 61  or mismatched pa
36155 72 65 6e 74 68 65 73 69 73 20 2a 2f 0a 20 20 69  renthesis */.  i
36156 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36157 20 26 26 20 73 50 61 72 73 65 2e 6e 4e 65 73 74   && sParse.nNest
36158 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
36159 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73  ITE_ERROR;.    s
3615a 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72  qlite3Fts3ExprFr
3615b 65 65 28 2a 70 70 45 78 70 72 29 3b 0a 20 20 20  ee(*ppExpr);.   
3615c 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20 20   *ppExpr = 0;.  
3615d 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
3615e 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20  }../*.** Free a 
3615f 70 61 72 73 65 64 20 66 74 73 33 20 71 75 65 72  parsed fts3 quer
36160 79 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6c 6c  y expression all
36161 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
36162 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28 29  3Fts3ExprParse()
36163 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
36164 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
36165 46 74 73 33 45 78 70 72 46 72 65 65 28 46 74 73  Fts3ExprFree(Fts
36166 33 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28  3Expr *p){.  if(
36167 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   p ){.    sqlite
36168 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70 2d  3Fts3ExprFree(p-
36169 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c  >pLeft);.    sql
3616a 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
3616b 28 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20  (p->pRight);.   
3616c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
3616d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  ;.  }.}../******
3616e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3616f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36171 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36172 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
36173 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36176 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36177 2a 2a 2a 2a 0a 2a 2a 20 45 76 65 72 79 74 68 69  ****.** Everythi
36178 6e 67 20 61 66 74 65 72 20 74 68 69 73 20 70 6f  ng after this po
36179 69 6e 74 20 69 73 20 6a 75 73 74 20 74 65 73 74  int is just test
3617a 20 63 6f 64 65 2e 0a 2a 2f 0a 0a 23 69 66 64 65   code..*/..#ifde
3617b 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 0a 0a  f SQLITE_TEST...
3617c 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74  /*.** Function t
3617d 6f 20 71 75 65 72 79 20 74 68 65 20 68 61 73 68  o query the hash
3617e 2d 74 61 62 6c 65 20 6f 66 20 74 6f 6b 65 6e 69  -table of tokeni
3617f 7a 65 72 73 20 28 73 65 65 20 52 45 41 44 4d 45  zers (see README
36180 2e 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 0a 2a 2f  .tokenizers)..*/
36181 0a 73 74 61 74 69 63 20 69 6e 74 20 71 75 65 72  .static int quer
36182 79 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72 28 0a  yTestTokenizer(.
36183 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
36184 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
36185 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73 74 20 73  ame,  .  const s
36186 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
36187 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a  _module **pp.){.
36188 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
36189 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
3618a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53  .  const char zS
3618b 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66  ql[] = "SELECT f
3618c 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 29  ts3_tokenizer(?)
3618d 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30 3b 0a 20  ";..  *pp = 0;. 
3618e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
3618f 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71  epare_v2(db, zSq
36190 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30  l, -1, &pStmt, 0
36191 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36192 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
36193 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
36194 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
36195 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d  t(pStmt, 1, zNam
36196 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54  e, -1, SQLITE_ST
36197 41 54 49 43 29 3b 0a 20 20 69 66 28 20 53 51 4c  ATIC);.  if( SQL
36198 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
36199 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
3619a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
3619b 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74 6d  column_type(pStm
3619c 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42 4c  t, 0)==SQLITE_BL
3619d 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  OB ){.      memc
3619e 70 79 28 70 70 2c 20 73 71 6c 69 74 65 33 5f 63  py(pp, sqlite3_c
3619f 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74  olumn_blob(pStmt
361a0 2c 20 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70 70  , 0), sizeof(*pp
361a1 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ));.    }.  }.. 
361a2 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
361a3 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
361a4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
361a5 75 6e 63 74 69 6f 6e 20 69 73 20 70 61 72 74 20  unction is part 
361a6 6f 66 20 74 68 65 20 74 65 73 74 20 69 6e 74 65  of the test inte
361a7 72 66 61 63 65 20 66 6f 72 20 74 68 65 20 71 75  rface for the qu
361a8 65 72 79 20 70 61 72 73 65 72 2e 20 49 74 0a 2a  ery parser. It.*
361a9 2a 20 77 72 69 74 65 73 20 61 20 74 65 78 74 20  * writes a text 
361aa 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
361ab 66 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72  f the query expr
361ac 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 6e 74  ession pExpr int
361ad 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20  o the.** buffer 
361ae 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72  pointed to by ar
361af 67 75 6d 65 6e 74 20 7a 42 75 66 2e 20 49 74 20  gument zBuf. It 
361b0 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
361b1 7a 42 75 66 20 69 73 20 6c 61 72 67 65 20 0a 2a  zBuf is large .*
361b2 2a 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72  * enough to stor
361b3 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74  e the required t
361b4 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  ext representati
361b5 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
361b6 69 64 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28  id exprToString(
361b7 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c  Fts3Expr *pExpr,
361b8 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20   char *zBuf){.  
361b9 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65  switch( pExpr->e
361ba 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65  Type ){.    case
361bb 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
361bc 3a 20 7b 0a 20 20 20 20 20 20 46 74 73 33 50 68  : {.      Fts3Ph
361bd 72 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20  rase *pPhrase = 
361be 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 3b 0a  pExpr->pPhrase;.
361bf 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
361c0 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e     zBuf += sprin
361c1 74 66 28 7a 42 75 66 2c 20 22 50 48 52 41 53 45  tf(zBuf, "PHRASE
361c2 20 25 64 20 25 64 22 2c 20 70 50 68 72 61 73 65   %d %d", pPhrase
361c3 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 50 68 72 61  ->iColumn, pPhra
361c4 73 65 2d 3e 69 73 4e 6f 74 29 3b 0a 20 20 20 20  se->isNot);.    
361c5 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 68    for(i=0; i<pPh
361c6 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 2b  rase->nToken; i+
361c7 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66  +){.        zBuf
361c8 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
361c9 2c 22 20 25 2e 2a 73 22 2c 70 50 68 72 61 73 65  ," %.*s",pPhrase
361ca 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 2c 70 50  ->aToken[i].n,pP
361cb 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d  hrase->aToken[i]
361cc 2e 7a 29 3b 0a 20 20 20 20 20 20 20 20 7a 42 75  .z);.        zBu
361cd 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
361ce 66 2c 22 25 73 22 2c 20 28 70 50 68 72 61 73 65  f,"%s", (pPhrase
361cf 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 69 73 50 72  ->aToken[i].isPr
361d0 65 66 69 78 3f 22 2b 22 3a 22 22 29 29 3b 0a 20  efix?"+":""));. 
361d1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
361d2 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  urn;.    }..    
361d3 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 45  case FTSQUERY_NE
361d4 41 52 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b  AR:.      zBuf +
361d5 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20  = sprintf(zBuf, 
361d6 22 4e 45 41 52 2f 25 64 20 22 2c 20 70 45 78 70  "NEAR/%d ", pExp
361d7 72 2d 3e 6e 4e 65 61 72 29 3b 0a 20 20 20 20 20  r->nNear);.     
361d8 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
361d9 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 3a 0a 20   FTSQUERY_NOT:. 
361da 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72       zBuf += spr
361db 69 6e 74 66 28 7a 42 75 66 2c 20 22 4e 4f 54 20  intf(zBuf, "NOT 
361dc 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ");.      break;
361dd 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45  .    case FTSQUE
361de 52 59 5f 41 4e 44 3a 0a 20 20 20 20 20 20 7a 42  RY_AND:.      zB
361df 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
361e0 75 66 2c 20 22 41 4e 44 20 22 29 3b 0a 20 20 20  uf, "AND ");.   
361e1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
361e2 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52 3a 0a  se FTSQUERY_OR:.
361e3 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70        zBuf += sp
361e4 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4f 52 20  rintf(zBuf, "OR 
361e5 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ");.      break;
361e6 0a 20 20 7d 0a 0a 20 20 7a 42 75 66 20 2b 3d 20  .  }..  zBuf += 
361e7 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 7b  sprintf(zBuf, "{
361e8 22 29 3b 0a 20 20 65 78 70 72 54 6f 53 74 72 69  ");.  exprToStri
361e9 6e 67 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  ng(pExpr->pLeft,
361ea 20 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b   zBuf);.  zBuf +
361eb 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a  = strlen(zBuf);.
361ec 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
361ed 66 28 7a 42 75 66 2c 20 22 7d 20 22 29 3b 0a 0a  f(zBuf, "} ");..
361ee 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
361ef 66 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a 20 20  f(zBuf, "{");.  
361f0 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45 78  exprToString(pEx
361f1 70 72 2d 3e 70 52 69 67 68 74 2c 20 7a 42 75 66  pr->pRight, zBuf
361f2 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74 72  );.  zBuf += str
361f3 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42 75  len(zBuf);.  zBu
361f4 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
361f5 66 2c 20 22 7d 22 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  f, "}");.}../*.*
361f6 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d  * This is the im
361f7 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
361f8 61 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75 6e  a scalar SQL fun
361f9 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65  ction used to te
361fa 73 74 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 65  st the .** expre
361fb 73 73 69 6f 6e 20 70 61 72 73 65 72 2e 20 49 74  ssion parser. It
361fc 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65   should be calle
361fd 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  d as follows:.**
361fe 0a 2a 2a 20 20 20 66 74 73 33 5f 65 78 70 72 74  .**   fts3_exprt
361ff 65 73 74 28 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c  est(<tokenizer>,
36200 20 3c 65 78 70 72 3e 2c 20 3c 63 6f 6c 75 6d 6e   <expr>, <column
36201 20 31 3e 2c 20 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a   1>, ...);.**.**
36202 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
36203 65 6e 74 2c 20 3c 74 6f 6b 65 6e 69 7a 65 72 3e  ent, <tokenizer>
36204 2c 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  , is the name of
36205 20 74 68 65 20 66 74 73 33 20 74 6f 6b 65 6e 69   the fts3 tokeni
36206 7a 65 72 20 75 73 65 64 0a 2a 2a 20 74 6f 20 70  zer used.** to p
36207 61 72 73 65 20 74 68 65 20 71 75 65 72 79 20 65  arse the query e
36208 78 70 72 65 73 73 69 6f 6e 20 28 73 65 65 20 52  xpression (see R
36209 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72 73  EADME.tokenizers
3620a 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ). The second ar
3620b 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65  gument.** is the
3620c 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
3620d 6e 20 74 6f 20 70 61 72 73 65 2e 20 45 61 63 68  n to parse. Each
3620e 20 73 75 62 73 65 71 75 65 6e 74 20 61 72 67 75   subsequent argu
3620f 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d 65  ment is the name
36210 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20  .** of a column 
36211 6f 66 20 74 68 65 20 66 74 73 33 20 74 61 62 6c  of the fts3 tabl
36212 65 20 74 68 61 74 20 74 68 65 20 71 75 65 72 79  e that the query
36213 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 20   expression may 
36214 72 65 66 65 72 20 74 6f 2e 0a 2a 2a 20 46 6f 72  refer to..** For
36215 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
36216 20 20 53 45 4c 45 43 54 20 66 74 73 33 5f 65 78    SELECT fts3_ex
36217 70 72 74 65 73 74 28 27 73 69 6d 70 6c 65 27 2c  prtest('simple',
36218 20 27 42 69 6c 6c 20 63 6f 6c 32 3a 42 6c 6f 67   'Bill col2:Blog
36219 67 73 27 2c 20 27 63 6f 6c 31 27 2c 20 27 63 6f  gs', 'col1', 'co
3621a 6c 32 27 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20  l2');.*/.static 
3621b 76 6f 69 64 20 66 74 73 33 45 78 70 72 54 65 73  void fts3ExprTes
3621c 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  t(.  sqlite3_con
3621d 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
3621e 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
3621f 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
36220 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  v.){.  sqlite3_t
36221 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
36222 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d  const *pModule =
36223 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f   0;.  sqlite3_to
36224 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
36225 7a 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  zer = 0;.  int r
36226 63 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  c;.  char **azCo
36227 6c 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63  l = 0;.  const c
36228 68 61 72 20 2a 7a 45 78 70 72 3b 0a 20 20 69 6e  har *zExpr;.  in
36229 74 20 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20 6e  t nExpr;.  int n
3622a 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  Col;.  int ii;. 
3622b 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72   Fts3Expr *pExpr
3622c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
3622d 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
3622e 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
3622f 65 78 74 29 3b 0a 0a 20 20 69 66 28 20 61 72 67  ext);..  if( arg
36230 63 3c 33 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  c<3 ){.    sqlit
36231 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
36232 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  context, .      
36233 20 20 22 55 73 61 67 65 3a 20 66 74 73 33 5f 65    "Usage: fts3_e
36234 78 70 72 74 65 73 74 28 74 6f 6b 65 6e 69 7a 65  xprtest(tokenize
36235 72 2c 20 65 78 70 72 2c 20 63 6f 6c 31 2c 20 2e  r, expr, col1, .
36236 2e 2e 22 2c 20 2d 31 0a 20 20 20 20 29 3b 0a 20  ..", -1.    );. 
36237 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
36238 20 20 72 63 20 3d 20 71 75 65 72 79 54 65 73 74    rc = queryTest
36239 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 0a 20 20  Tokenizer(db,.  
3623a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3623b 20 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63          (const c
3623c 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
3623d 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
3623e 29 2c 20 26 70 4d 6f 64 75 6c 65 29 3b 0a 20 20  ), &pModule);.  
3623f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
36240 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69  OMEM ){.    sqli
36241 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
36242 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
36243 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65  .    goto exprte
36244 73 74 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20  st_out;.  }else 
36245 69 66 28 20 21 70 4d 6f 64 75 6c 65 20 29 7b 0a  if( !pModule ){.
36246 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
36247 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
36248 2c 20 22 4e 6f 20 73 75 63 68 20 74 6f 6b 65 6e  , "No such token
36249 69 7a 65 72 20 6d 6f 64 75 6c 65 22 2c 20 2d 31  izer module", -1
3624a 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72  );.    goto expr
3624b 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  test_out;.  }.. 
3624c 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78   rc = pModule->x
3624d 43 72 65 61 74 65 28 30 2c 20 30 2c 20 26 70 54  Create(0, 0, &pT
3624e 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 61 73 73  okenizer);.  ass
3624f 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
36250 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c  NOMEM || rc==SQL
36251 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20  ITE_OK );.  if( 
36252 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
36253 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
36254 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
36255 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
36256 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
36257 75 74 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65 6e  ut;.  }.  pToken
36258 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20  izer->pModule = 
36259 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 7a 45 78 70  pModule;..  zExp
3625a 72 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  r = (const char 
3625b 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
3625c 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
3625d 20 6e 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33   nExpr = sqlite3
3625e 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
3625f 76 5b 31 5d 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20  v[1]);.  nCol = 
36260 61 72 67 63 2d 32 3b 0a 20 20 61 7a 43 6f 6c 20  argc-2;.  azCol 
36261 3d 20 28 63 68 61 72 20 2a 2a 29 73 71 6c 69 74  = (char **)sqlit
36262 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 6f 6c 2a 73  e3_malloc(nCol*s
36263 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 29 3b 0a  izeof(char *));.
36264 20 20 69 66 28 20 21 61 7a 43 6f 6c 20 29 7b 0a    if( !azCol ){.
36265 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
36266 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63  lt_error_nomem(c
36267 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74  ontext);.    got
36268 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a  o exprtest_out;.
36269 20 20 7d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20    }.  for(ii=0; 
3626a 69 69 3c 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a  ii<nCol; ii++){.
3626b 20 20 20 20 61 7a 43 6f 6c 5b 69 69 5d 20 3d 20      azCol[ii] = 
3626c 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
3626d 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
3626e 69 69 2b 32 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72  ii+2]);.  }..  r
3626f 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 45  c = sqlite3Fts3E
36270 78 70 72 50 61 72 73 65 28 0a 20 20 20 20 20 20  xprParse(.      
36271 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 61 7a 43 6f  pTokenizer, azCo
36272 6c 2c 20 6e 43 6f 6c 2c 20 6e 43 6f 6c 2c 20 7a  l, nCol, nCol, z
36273 45 78 70 72 2c 20 6e 45 78 70 72 2c 20 26 70 45  Expr, nExpr, &pE
36274 78 70 72 0a 20 20 29 3b 0a 20 20 69 66 28 20 72  xpr.  );.  if( r
36275 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
36276 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
36277 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
36278 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  m(context);.    
36279 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75  goto exprtest_ou
3627a 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  t;.  }else if( r
3627b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3627c 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 34 30      char zBuf[40
3627d 39 36 5d 3b 0a 20 20 20 20 65 78 70 72 54 6f 53  96];.    exprToS
3627e 74 72 69 6e 67 28 70 45 78 70 72 2c 20 7a 42 75  tring(pExpr, zBu
3627f 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  f);.    sqlite3_
36280 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
36281 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53  ext, zBuf, -1, S
36282 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
36283 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ;.    sqlite3Fts
36284 33 45 78 70 72 46 72 65 65 28 70 45 78 70 72 29  3ExprFree(pExpr)
36285 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
36286 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
36287 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 45 72  ror(context, "Er
36288 72 6f 72 20 70 61 72 73 69 6e 67 20 65 78 70 72  ror parsing expr
36289 65 73 73 69 6f 6e 22 2c 20 2d 31 29 3b 0a 20 20  ession", -1);.  
3628a 7d 0a 0a 65 78 70 72 74 65 73 74 5f 6f 75 74 3a  }..exprtest_out:
3628b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 20 26  .  if( pModule &
3628c 26 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a  & pTokenizer ){.
3628d 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65      rc = pModule
3628e 2d 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b 65  ->xDestroy(pToke
3628f 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71  nizer);.  }.  sq
36290 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 6f 6c  lite3_free(azCol
36291 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  );.}../*.** Regi
36292 73 74 65 72 20 74 68 65 20 71 75 65 72 79 20 65  ster the query e
36293 78 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65 72  xpression parser
36294 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 66   test function f
36295 74 73 33 5f 65 78 70 72 74 65 73 74 28 29 20 0a  ts3_exprtest() .
36296 2a 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65  ** with database
36297 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 20   connection db. 
36298 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
36299 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
3629a 74 73 33 45 78 70 72 49 6e 69 74 54 65 73 74 49  ts3ExprInitTestI
3629b 6e 74 65 72 66 61 63 65 28 73 71 6c 69 74 65 33  nterface(sqlite3
3629c 2a 20 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33  * db){.  sqlite3
3629d 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
3629e 28 0a 20 20 20 20 20 20 64 62 2c 20 22 66 74 73  (.      db, "fts
3629f 33 5f 65 78 70 72 74 65 73 74 22 2c 20 2d 31 2c  3_exprtest", -1,
362a0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
362a1 20 66 74 73 33 45 78 70 72 54 65 73 74 2c 20 30   fts3ExprTest, 0
362a2 2c 20 30 0a 20 20 29 3b 0a 7d 0a 0a 23 65 6e 64  , 0.  );.}..#end
362a3 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  if.#endif /* !de
362a4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
362a5 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
362a6 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
362a7 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
362a8 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
362a9 33 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  3_expr.c *******
362aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
362ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
362ac 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
362ad 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
362ae 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a 2a   fts3_hash.c ***
362af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
362b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
362b1 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
362b2 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a   September 22.**
362b3 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
362b4 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
362b5 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
362b6 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
362b7 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
362b8 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
362b9 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
362ba 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
362bb 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
362bc 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
362bd 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
362be 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
362bf 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
362c0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
362c1 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
362c2 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
362c3 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
362c4 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
362c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
362c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
362c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
362c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
362c9 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c  This is the impl
362ca 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67 65  ementation of ge
362cb 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65  neric hash-table
362cc 73 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65  s used in SQLite
362cd 2e 0a 2a 2a 20 57 65 27 76 65 20 6d 6f 64 69 66  ..** We've modif
362ce 69 65 64 20 69 74 20 73 6c 69 67 68 74 6c 79 20  ied it slightly 
362cf 74 6f 20 73 65 72 76 65 20 61 73 20 61 20 73 74  to serve as a st
362d0 61 6e 64 61 6c 6f 6e 65 20 68 61 73 68 20 74 61  andalone hash ta
362d1 62 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ble.** implement
362d2 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75  ation for the fu
362d3 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 69 6e 67  ll-text indexing
362d4 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a   module..*/../*.
362d5 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
362d6 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
362d7 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a   compiled if:.**
362d8 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54  .**     * The FT
362d9 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69  S3 module is bei
362da 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65  ng built as an e
362db 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20  xtension.**     
362dc 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65    (in which case
362dd 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20   SQLITE_CORE is 
362de 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72  not defined), or
362df 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
362e0 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
362e1 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f  being built into
362e2 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20   the core of.** 
362e3 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e        SQLite (in
362e4 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
362e5 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69  TE_ENABLE_FTS3 i
362e6 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23  s defined)..*/.#
362e7 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
362e8 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
362e9 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
362ea 45 5f 46 54 53 33 29 0a 0a 0a 2f 2a 2a 2a 2a 2a  E_FTS3).../*****
362eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
362ec 65 20 66 74 73 33 5f 68 61 73 68 2e 68 20 69 6e  e fts3_hash.h in
362ed 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 66   the middle of f
362ee 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a  ts3_hash.c *****
362ef 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
362f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
362f1 66 69 6c 65 20 66 74 73 33 5f 68 61 73 68 2e 68  file fts3_hash.h
362f2 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
362f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
362f4 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
362f5 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32  2001 September 2
362f6 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
362f7 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
362f8 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
362f9 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
362fa 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
362fb 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
362fc 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
362fd 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
362fe 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
362ff 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
36300 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
36301 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
36302 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
36303 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
36304 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
36305 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
36306 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
36307 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
36308 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36309 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3630a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3630b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3630c 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
3630d 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20  header file for 
3630e 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 73 68  the generic hash
3630f 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 61  -table implemena
36310 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e 20  tion.** used in 
36311 53 51 4c 69 74 65 2e 20 20 57 65 27 76 65 20 6d  SQLite.  We've m
36312 6f 64 69 66 69 65 64 20 69 74 20 73 6c 69 67 68  odified it sligh
36313 74 6c 79 20 74 6f 20 73 65 72 76 65 20 61 73 20  tly to serve as 
36314 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a 20  a standalone.** 
36315 68 61 73 68 20 74 61 62 6c 65 20 69 6d 70 6c 65  hash table imple
36316 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68  mentation for th
36317 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65  e full-text inde
36318 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a  xing module..**.
36319 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 46 54 53 33  */.#ifndef _FTS3
3631a 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65  _HASH_H_.#define
3631b 20 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 0a   _FTS3_HASH_H_..
3631c 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
3631d 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63  rations of struc
3631e 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65  tures. */.typede
3631f 66 20 73 74 72 75 63 74 20 46 74 73 33 48 61 73  f struct Fts3Has
36320 68 20 46 74 73 33 48 61 73 68 3b 0a 74 79 70 65  h Fts3Hash;.type
36321 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33 48  def struct Fts3H
36322 61 73 68 45 6c 65 6d 20 46 74 73 33 48 61 73 68  ashElem Fts3Hash
36323 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70  Elem;../* A comp
36324 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20  lete hash table 
36325 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
36326 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
36327 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
36328 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74  e internals of t
36329 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72  his structure ar
3632a 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  e intended to be
3632b 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e   opaque -- clien
3632c 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64  t.** code should
3632d 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
3632e 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79  access or modify
3632f 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74   the fields of t
36330 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  his structure.**
36331 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e   directly.  Chan
36332 67 65 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ge this structur
36333 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20  e only by using 
36334 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c  the routines bel
36335 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  ow..** However, 
36336 6d 61 6e 79 20 6f 66 20 74 68 65 20 22 70 72 6f  many of the "pro
36337 63 65 64 75 72 65 73 22 20 61 6e 64 20 22 66 75  cedures" and "fu
36338 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64  nctions" for mod
36339 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63  ifying and.** ac
3633a 63 65 73 73 69 6e 67 20 74 68 69 73 20 73 74 72  cessing this str
3633b 75 63 74 75 72 65 20 61 72 65 20 72 65 61 6c 6c  ucture are reall
3633c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77 65 20  y macros, so we 
3633d 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b  can't really mak
3633e 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74  e.** this struct
3633f 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2f 0a 73  ure opaque..*/.s
36340 74 72 75 63 74 20 46 74 73 33 48 61 73 68 20 7b  truct Fts3Hash {
36341 0a 20 20 63 68 61 72 20 6b 65 79 43 6c 61 73 73  .  char keyClass
36342 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 41  ;          /* HA
36343 53 48 5f 49 4e 54 2c 20 5f 50 4f 49 4e 54 45 52  SH_INT, _POINTER
36344 2c 20 5f 53 54 52 49 4e 47 2c 20 5f 42 49 4e 41  , _STRING, _BINA
36345 52 59 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 70  RY */.  char cop
36346 79 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  yKey;           
36347 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 70 79 20  /* True if copy 
36348 6f 66 20 6b 65 79 20 6d 61 64 65 20 6f 6e 20 69  of key made on i
36349 6e 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20 63  nsert */.  int c
3634a 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
3634b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
3634c 65 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73 20  entries in this 
3634d 74 61 62 6c 65 20 2a 2f 0a 20 20 46 74 73 33 48  table */.  Fts3H
3634e 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20  ashElem *first; 
3634f 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20     /* The first 
36350 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  element of the a
36351 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 74  rray */.  int ht
36352 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  size;           
36353 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
36354 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61  uckets in the ha
36355 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74  sh table */.  st
36356 72 75 63 74 20 5f 66 74 73 33 68 74 20 7b 20 20  ruct _fts3ht {  
36357 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73        /* the has
36358 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69  h table */.    i
36359 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  nt count;       
3635a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3635b 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74  r of entries wit
3635c 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20  h this hash */. 
3635d 20 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20     Fts3HashElem 
3635e 2a 63 68 61 69 6e 3b 20 20 20 20 20 2f 2a 20 50  *chain;     /* P
3635f 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20  ointer to first 
36360 65 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20  entry with this 
36361 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b  hash */.  } *ht;
36362 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65  .};../* Each ele
36363 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68  ment in the hash
36364 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73   table is an ins
36365 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
36366 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63  lowing .** struc
36367 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65  ture.  All eleme
36368 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f  nts are stored o
36369 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c  n a single doubl
3636a 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a  y-linked list..*
3636b 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73  *.** Again, this
3636c 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e   structure is in
3636d 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61  tended to be opa
3636e 71 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27  que, but it can'
3636f 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f  t really.** be o
36370 70 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74  paque because it
36371 20 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72   is used by macr
36372 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 74  os..*/.struct Ft
36373 73 33 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 46  s3HashElem {.  F
36374 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78  ts3HashElem *nex
36375 74 2c 20 2a 70 72 65 76 3b 20 2f 2a 20 4e 65 78  t, *prev; /* Nex
36376 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 65  t and previous e
36377 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 74  lements in the t
36378 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  able */.  void *
36379 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  data;           
3637a 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73 73       /* Data ass
3637b 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69  ociated with thi
3637c 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 76  s element */.  v
3637d 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e  oid *pKey; int n
3637e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 4b 65 79  Key;      /* Key
3637f 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
36380 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f   this element */
36381 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65  .};../*.** There
36382 20 61 72 65 20 32 20 64 69 66 66 65 72 65 6e 74   are 2 different
36383 20 6d 6f 64 65 73 20 6f 66 20 6f 70 65 72 61 74   modes of operat
36384 69 6f 6e 20 66 6f 72 20 61 20 68 61 73 68 20 74  ion for a hash t
36385 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 54  able:.**.**   FT
36386 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 20  S3_HASH_STRING  
36387 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e 74        pKey point
36388 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68  s to a string th
36389 61 74 20 69 73 20 6e 4b 65 79 20 62 79 74 65 73  at is nKey bytes
3638a 20 6c 6f 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20   long.**        
3638b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3638c 20 20 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 68     (including th
3638d 65 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 6f  e null-terminato
3638e 72 2c 20 69 66 20 61 6e 79 29 2e 20 20 43 61 73  r, if any).  Cas
3638f 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
36390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
36391 73 20 72 65 73 70 65 63 74 65 64 20 69 6e 20 63  s respected in c
36392 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a  omparisons..**.*
36393 2a 20 20 20 46 54 53 33 5f 48 41 53 48 5f 42 49  *   FTS3_HASH_BI
36394 4e 41 52 59 20 20 20 20 20 20 20 20 70 4b 65 79  NARY        pKey
36395 20 70 6f 69 6e 74 73 20 74 6f 20 62 69 6e 61 72   points to binar
36396 79 20 64 61 74 61 20 6e 4b 65 79 20 62 79 74 65  y data nKey byte
36397 73 20 6c 6f 6e 67 2e 20 0a 2a 2a 20 20 20 20 20  s long. .**     
36398 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36399 20 20 20 20 20 20 6d 65 6d 63 6d 70 28 29 20 69        memcmp() i
3639a 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72  s used to compar
3639b 65 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 41 20  e keys..**.** A 
3639c 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20  copy of the key 
3639d 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20 63  is made if the c
3639e 6f 70 79 4b 65 79 20 70 61 72 61 6d 65 74 65 72  opyKey parameter
3639f 20 74 6f 20 66 74 73 33 48 61 73 68 49 6e 69 74   to fts3HashInit
363a0 20 69 73 20 31 2e 20 20 0a 2a 2f 0a 23 64 65 66   is 1.  .*/.#def
363a1 69 6e 65 20 46 54 53 33 5f 48 41 53 48 5f 53 54  ine FTS3_HASH_ST
363a2 52 49 4e 47 20 20 20 20 31 0a 23 64 65 66 69 6e  RING    1.#defin
363a3 65 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41  e FTS3_HASH_BINA
363a4 52 59 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 41  RY    2../*.** A
363a5 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e 20  ccess routines. 
363a6 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 65   To delete, inse
363a7 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  rt a NULL pointe
363a8 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
363a9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
363aa 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 46 74  3Fts3HashInit(Ft
363ab 73 33 48 61 73 68 2a 2c 20 69 6e 74 20 6b 65 79  s3Hash*, int key
363ac 74 79 70 65 2c 20 69 6e 74 20 63 6f 70 79 4b 65  type, int copyKe
363ad 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
363ae 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
363af 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 46  Fts3HashInsert(F
363b0 74 73 33 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20  ts3Hash*, const 
363b1 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
363b2 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 74  nKey, void *pDat
363b3 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  a);.SQLITE_PRIVA
363b4 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
363b5 46 74 73 33 48 61 73 68 46 69 6e 64 28 63 6f 6e  Fts3HashFind(con
363b6 73 74 20 46 74 73 33 48 61 73 68 2a 2c 20 63 6f  st Fts3Hash*, co
363b7 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
363b8 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54  int nKey);.SQLIT
363b9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
363ba 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
363bb 65 61 72 28 46 74 73 33 48 61 73 68 2a 29 3b 0a  ear(Fts3Hash*);.
363bc 0a 2f 2a 0a 2a 2a 20 53 68 6f 72 74 68 61 6e 64  ./*.** Shorthand
363bd 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f   for the functio
363be 6e 73 20 61 62 6f 76 65 0a 2a 2f 0a 23 64 65 66  ns above.*/.#def
363bf 69 6e 65 20 66 74 73 33 48 61 73 68 49 6e 69 74  ine fts3HashInit
363c0 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61     sqlite3Fts3Ha
363c1 73 68 49 6e 69 74 0a 23 64 65 66 69 6e 65 20 66  shInit.#define f
363c2 74 73 33 48 61 73 68 49 6e 73 65 72 74 20 73 71  ts3HashInsert sq
363c3 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73  lite3Fts3HashIns
363c4 65 72 74 0a 23 64 65 66 69 6e 65 20 66 74 73 33  ert.#define fts3
363c5 48 61 73 68 46 69 6e 64 20 20 20 73 71 6c 69 74  HashFind   sqlit
363c6 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 0a 23  e3Fts3HashFind.#
363c7 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 43  define fts3HashC
363c8 6c 65 61 72 20 20 73 71 6c 69 74 65 33 46 74 73  lear  sqlite3Fts
363c9 33 48 61 73 68 43 6c 65 61 72 0a 0a 2f 2a 0a 2a  3HashClear../*.*
363ca 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f  * Macros for loo
363cb 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c  ping over all el
363cc 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68  ements of a hash
363cd 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69   table.  The idi
363ce 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68  om is.** like th
363cf 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 74 73 33  is:.**.**   Fts3
363d0 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 46 74 73  Hash h;.**   Fts
363d1 33 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a  3HashElem *p;.**
363d2 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28     ....**   for(
363d3 70 3d 66 74 73 33 48 61 73 68 46 69 72 73 74 28  p=fts3HashFirst(
363d4 26 68 29 3b 20 70 3b 20 70 3d 66 74 73 33 48 61  &h); p; p=fts3Ha
363d5 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20  shNext(p)){.**  
363d6 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75 72 65     SomeStructure
363d7 20 2a 70 44 61 74 61 20 3d 20 66 74 73 33 48 61   *pData = fts3Ha
363d8 73 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20  shData(p);.**   
363d9 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e    // do somethin
363da 67 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20  g with pData.** 
363db 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66    }.*/.#define f
363dc 74 73 33 48 61 73 68 46 69 72 73 74 28 48 29 20  ts3HashFirst(H) 
363dd 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64   ((H)->first).#d
363de 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 4e 65  efine fts3HashNe
363df 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65  xt(E)   ((E)->ne
363e0 78 74 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33  xt).#define fts3
363e1 48 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28  HashData(E)   ((
363e2 45 29 2d 3e 64 61 74 61 29 0a 23 64 65 66 69 6e  E)->data).#defin
363e3 65 20 66 74 73 33 48 61 73 68 4b 65 79 28 45 29  e fts3HashKey(E)
363e4 20 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 0a      ((E)->pKey).
363e5 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
363e6 4b 65 79 73 69 7a 65 28 45 29 20 28 28 45 29 2d  Keysize(E) ((E)-
363e7 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a 2a 20 4e 75  >nKey)../*.** Nu
363e8 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
363e9 69 6e 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a  in a hash table.
363ea 2a 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  */.#define fts3H
363eb 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48  ashCount(H)  ((H
363ec 29 2d 3e 63 6f 75 6e 74 29 0a 0a 23 65 6e 64 69  )->count)..#endi
363ed 66 20 2f 2a 20 5f 46 54 53 33 5f 48 41 53 48 5f  f /* _FTS3_HASH_
363ee 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
363ef 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
363f0 73 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  s3_hash.h ******
363f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
363f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
363f3 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
363f4 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
363f5 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
363f6 6f 66 66 20 69 6e 20 66 74 73 33 5f 68 61 73 68  off in fts3_hash
363f7 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
363f8 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61  *****/../*.** Ma
363f9 6c 6c 6f 63 20 61 6e 64 20 46 72 65 65 20 66 75  lloc and Free fu
363fa 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69  nctions.*/.stati
363fb 63 20 76 6f 69 64 20 2a 66 74 73 33 48 61 73 68  c void *fts3Hash
363fc 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20  Malloc(int n){. 
363fd 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74   void *p = sqlit
363fe 65 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20  e3_malloc(n);.  
363ff 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d  if( p ){.    mem
36400 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20  set(p, 0, n);.  
36401 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
36402 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
36403 48 61 73 68 46 72 65 65 28 76 6f 69 64 20 2a 70  HashFree(void *p
36404 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
36405 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 75 72 6e  e(p);.}../* Turn
36406 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74   bulk memory int
36407 6f 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f  o a hash table o
36408 62 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c  bject by initial
36409 69 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65  izing the.** fie
3640a 6c 64 73 20 6f 66 20 74 68 65 20 48 61 73 68 20  lds of the Hash 
3640b 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
3640c 20 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69   "pNew" is a poi
3640d 6e 74 65 72 20 74 6f 20 74 68 65 20 68 61 73 68  nter to the hash
3640e 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74   table that is t
3640f 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  o be initialized
36410 2e 0a 2a 2a 20 6b 65 79 43 6c 61 73 73 20 69 73  ..** keyClass is
36411 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e 73   one of the cons
36412 74 61 6e 74 73 20 0a 2a 2a 20 46 54 53 33 5f 48  tants .** FTS3_H
36413 41 53 48 5f 42 49 4e 41 52 59 20 6f 72 20 46 54  ASH_BINARY or FT
36414 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2e 20  S3_HASH_STRING. 
36415 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6b 65   The value of ke
36416 79 43 6c 61 73 73 20 0a 2a 2a 20 64 65 74 65 72  yClass .** deter
36417 6d 69 6e 65 73 20 77 68 61 74 20 6b 69 6e 64 20  mines what kind 
36418 6f 66 20 6b 65 79 20 74 68 65 20 68 61 73 68 20  of key the hash 
36419 74 61 62 6c 65 20 77 69 6c 6c 20 75 73 65 2e 20  table will use. 
3641a 20 22 63 6f 70 79 4b 65 79 22 20 69 73 0a 2a 2a   "copyKey" is.**
3641b 20 74 72 75 65 20 69 66 20 74 68 65 20 68 61 73   true if the has
3641c 68 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 6d  h table should m
3641d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76  ake its own priv
3641e 61 74 65 20 63 6f 70 79 20 6f 66 20 6b 65 79 73  ate copy of keys
3641f 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66   and.** false if
36420 20 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 74 20   it should just 
36421 75 73 65 20 74 68 65 20 73 75 70 70 6c 69 65 64  use the supplied
36422 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c   pointer..*/.SQL
36423 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
36424 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
36425 49 6e 69 74 28 46 74 73 33 48 61 73 68 20 2a 70  Init(Fts3Hash *p
36426 4e 65 77 2c 20 69 6e 74 20 6b 65 79 43 6c 61 73  New, int keyClas
36427 73 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 7b  s, int copyKey){
36428 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21  .  assert( pNew!
36429 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
3642a 6b 65 79 43 6c 61 73 73 3e 3d 46 54 53 33 5f 48  keyClass>=FTS3_H
3642b 41 53 48 5f 53 54 52 49 4e 47 20 26 26 20 6b 65  ASH_STRING && ke
3642c 79 43 6c 61 73 73 3c 3d 46 54 53 33 5f 48 41 53  yClass<=FTS3_HAS
3642d 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20 70 4e  H_BINARY );.  pN
3642e 65 77 2d 3e 6b 65 79 43 6c 61 73 73 20 3d 20 6b  ew->keyClass = k
3642f 65 79 43 6c 61 73 73 3b 0a 20 20 70 4e 65 77 2d  eyClass;.  pNew-
36430 3e 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b  >copyKey = copyK
36431 65 79 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73  ey;.  pNew->firs
36432 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63  t = 0;.  pNew->c
36433 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77  ount = 0;.  pNew
36434 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20  ->htsize = 0;.  
36435 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a  pNew->ht = 0;.}.
36436 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65  ./* Remove all e
36437 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61  ntries from a ha
36438 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61  sh table.  Recla
36439 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a  im all memory..*
3643a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  * Call this rout
3643b 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20  ine to delete a 
3643c 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f  hash table or to
3643d 20 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61   reset a hash ta
3643e 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d  ble.** to the em
3643f 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51  pty state..*/.SQ
36440 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
36441 64 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  d sqlite3Fts3Has
36442 68 43 6c 65 61 72 28 46 74 73 33 48 61 73 68 20  hClear(Fts3Hash 
36443 2a 70 48 29 7b 0a 20 20 46 74 73 33 48 61 73 68  *pH){.  Fts3Hash
36444 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20  Elem *elem;     
36445 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
36446 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d  ng over all elem
36447 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ents of the tabl
36448 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
36449 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20  pH!=0 );.  elem 
3644a 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70  = pH->first;.  p
3644b 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20  H->first = 0;.  
3644c 66 74 73 33 48 61 73 68 46 72 65 65 28 70 48 2d  fts3HashFree(pH-
3644d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d  >ht);.  pH->ht =
3644e 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65   0;.  pH->htsize
3644f 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65   = 0;.  while( e
36450 6c 65 6d 20 29 7b 0a 20 20 20 20 46 74 73 33 48  lem ){.    Fts3H
36451 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c  ashElem *next_el
36452 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  em = elem->next;
36453 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70  .    if( pH->cop
36454 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b  yKey && elem->pK
36455 65 79 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33  ey ){.      fts3
36456 48 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70  HashFree(elem->p
36457 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Key);.    }.    
36458 66 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65  fts3HashFree(ele
36459 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e  m);.    elem = n
3645a 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20  ext_elem;.  }.  
3645b 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d  pH->count = 0;.}
3645c 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64  ../*.** Hash and
3645d 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
3645e 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d  tions when the m
3645f 6f 64 65 20 69 73 20 46 54 53 33 5f 48 41 53 48  ode is FTS3_HASH
36460 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69  _STRING.*/.stati
36461 63 20 69 6e 74 20 66 74 73 33 53 74 72 48 61 73  c int fts3StrHas
36462 68 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  h(const void *pK
36463 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20  ey, int nKey){. 
36464 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
36465 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
36466 4b 65 79 3b 0a 20 20 69 6e 74 20 68 20 3d 20 30  Key;.  int h = 0
36467 3b 0a 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30 20  ;.  if( nKey<=0 
36468 29 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 20 73  ) nKey = (int) s
36469 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 77 68 69 6c  trlen(z);.  whil
3646a 65 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a  e( nKey > 0  ){.
3646b 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e      h = (h<<3) ^
3646c 20 68 20 5e 20 2a 7a 2b 2b 3b 0a 20 20 20 20 6e   h ^ *z++;.    n
3646d 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74  Key--;.  }.  ret
3646e 75 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66  urn h & 0x7fffff
3646f 66 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ff;.}.static int
36470 20 66 74 73 33 53 74 72 43 6f 6d 70 61 72 65 28   fts3StrCompare(
36471 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
36472 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74  1, int n1, const
36473 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e   void *pKey2, in
36474 74 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21  t n2){.  if( n1!
36475 3d 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  =n2 ) return 1;.
36476 20 20 72 65 74 75 72 6e 20 73 74 72 6e 63 6d 70    return strncmp
36477 28 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b  ((const char*)pK
36478 65 79 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a  ey1,(const char*
36479 29 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f  )pKey2,n1);.}../
3647a 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f  *.** Hash and co
3647b 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
3647c 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65  ns when the mode
3647d 20 69 73 20 46 54 53 33 5f 48 41 53 48 5f 42 49   is FTS3_HASH_BI
3647e 4e 41 52 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69  NARY.*/.static i
3647f 6e 74 20 66 74 73 33 42 69 6e 48 61 73 68 28 63  nt fts3BinHash(c
36480 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
36481 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e   int nKey){.  in
36482 74 20 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74  t h = 0;.  const
36483 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73   char *z = (cons
36484 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20  t char *)pKey;. 
36485 20 77 68 69 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e   while( nKey-- >
36486 20 30 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68   0 ){.    h = (h
36487 3c 3c 33 29 20 5e 20 68 20 5e 20 2a 28 7a 2b 2b  <<3) ^ h ^ *(z++
36488 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
36489 68 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a  h & 0x7fffffff;.
3648a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  }.static int fts
3648b 33 42 69 6e 43 6f 6d 70 61 72 65 28 63 6f 6e 73  3BinCompare(cons
3648c 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69  t void *pKey1, i
3648d 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69  nt n1, const voi
3648e 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32  d *pKey2, int n2
3648f 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20  ){.  if( n1!=n2 
36490 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65  ) return 1;.  re
36491 74 75 72 6e 20 6d 65 6d 63 6d 70 28 70 4b 65 79  turn memcmp(pKey
36492 31 2c 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a  1,pKey2,n1);.}..
36493 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
36494 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70  ointer to the ap
36495 70 72 6f 70 72 69 61 74 65 20 68 61 73 68 20 66  propriate hash f
36496 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68  unction given th
36497 65 20 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a  e key class..**.
36498 2a 2a 20 54 68 65 20 43 20 73 79 6e 74 61 78 20  ** The C syntax 
36499 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  in this function
3649a 20 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 79 20   definition may 
3649b 62 65 20 75 6e 66 61 6d 69 6c 61 72 20 74 6f 20  be unfamilar to 
3649c 73 6f 6d 65 20 0a 2a 2a 20 70 72 6f 67 72 61 6d  some .** program
3649d 6d 65 72 73 2c 20 73 6f 20 77 65 20 70 72 6f 76  mers, so we prov
3649e 69 64 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ide the followin
3649f 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 65 78 70  g additional exp
364a0 6c 61 6e 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  lanation:.**.** 
364a1 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
364a2 66 75 6e 63 74 69 6f 6e 20 69 73 20 22 66 74 73  function is "fts
364a3 48 61 73 68 46 75 6e 63 74 69 6f 6e 22 2e 20 20  HashFunction".  
364a4 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  The function tak
364a5 65 73 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 70  es a.** single p
364a6 61 72 61 6d 65 74 65 72 20 22 6b 65 79 43 6c 61  arameter "keyCla
364a7 73 73 22 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ss".  The return
364a8 20 76 61 6c 75 65 20 6f 66 20 66 74 73 48 61 73   value of ftsHas
364a9 68 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20 69  hFunction().** i
364aa 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
364ab 6e 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 2e  nother function.
364ac 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20    Specifically, 
364ad 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
364ae 0a 2a 2a 20 6f 66 20 66 74 73 48 61 73 68 46 75  .** of ftsHashFu
364af 6e 63 74 69 6f 6e 28 29 20 69 73 20 61 20 70 6f  nction() is a po
364b0 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
364b1 69 6f 6e 20 74 68 61 74 20 74 61 6b 65 73 20 74  ion that takes t
364b2 77 6f 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a  wo parameters.**
364b3 20 77 69 74 68 20 74 79 70 65 73 20 22 63 6f 6e   with types "con
364b4 73 74 20 76 6f 69 64 2a 22 20 61 6e 64 20 22 69  st void*" and "i
364b5 6e 74 22 20 61 6e 64 20 72 65 74 75 72 6e 73 20  nt" and returns 
364b6 61 6e 20 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61  an "int"..*/.sta
364b7 74 69 63 20 69 6e 74 20 28 2a 66 74 73 48 61 73  tic int (*ftsHas
364b8 68 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65  hFunction(int ke
364b9 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76  yClass))(const v
364ba 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28  oid*,int){.  if(
364bb 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
364bc 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20  HASH_STRING ){. 
364bd 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 53     return &fts3S
364be 74 72 48 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b  trHash;.  }else{
364bf 0a 20 20 20 20 61 73 73 65 72 74 28 20 6b 65 79  .    assert( key
364c0 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48  Class==FTS3_HASH
364c1 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72  _BINARY );.    r
364c2 65 74 75 72 6e 20 26 66 74 73 33 42 69 6e 48 61  eturn &fts3BinHa
364c3 73 68 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  sh;.  }.}../*.**
364c4 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
364c5 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72  r to the appropr
364c6 69 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69  iate hash functi
364c7 6f 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79  on given the key
364c8 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f   class..**.** Fo
364c9 72 20 68 65 6c 70 20 69 6e 20 69 6e 74 65 72 70  r help in interp
364ca 72 65 74 65 64 20 74 68 65 20 6f 62 73 63 75 72  reted the obscur
364cb 65 20 43 20 63 6f 64 65 20 69 6e 20 74 68 65 20  e C code in the 
364cc 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
364cd 69 6f 6e 2c 0a 2a 2a 20 73 65 65 20 74 68 65 20  ion,.** see the 
364ce 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f  header comment o
364cf 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 66  n the previous f
364d0 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
364d1 69 63 20 69 6e 74 20 28 2a 66 74 73 43 6f 6d 70  ic int (*ftsComp
364d2 61 72 65 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20  areFunction(int 
364d3 6b 65 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74  keyClass))(const
364d4 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
364d5 20 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69   void*,int){.  i
364d6 66 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53  f( keyClass==FTS
364d7 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b  3_HASH_STRING ){
364d8 0a 20 20 20 20 72 65 74 75 72 6e 20 26 66 74 73  .    return &fts
364d9 33 53 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 7d  3StrCompare;.  }
364da 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
364db 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33  ( keyClass==FTS3
364dc 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a  _HASH_BINARY );.
364dd 20 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33      return &fts3
364de 42 69 6e 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 0a  BinCompare;.  }.
364df 7d 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c  }../* Link an el
364e0 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68  ement into the h
364e1 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61  ash table.*/.sta
364e2 74 69 63 20 76 6f 69 64 20 66 74 73 33 48 61 73  tic void fts3Has
364e3 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a  hInsertElement(.
364e4 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 2c 20    Fts3Hash *pH, 
364e5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
364e6 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20  e complete hash 
364e7 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63  table */.  struc
364e8 74 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72  t _fts3ht *pEntr
364e9 79 2c 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79  y,  /* The entry
364ea 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77   into which pNew
364eb 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   is inserted */.
364ec 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    Fts3HashElem *
364ed 70 4e 65 77 20 20 20 20 20 20 20 2f 2a 20 54 68  pNew       /* Th
364ee 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20  e element to be 
364ef 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20  inserted */.){. 
364f0 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70   Fts3HashElem *p
364f1 48 65 61 64 3b 20 20 20 20 20 2f 2a 20 46 69 72  Head;     /* Fir
364f2 73 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61  st element alrea
364f3 64 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a  dy in pEntry */.
364f4 20 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79    pHead = pEntry
364f5 2d 3e 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70  ->chain;.  if( p
364f6 48 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77  Head ){.    pNew
364f7 2d 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a  ->next = pHead;.
364f8 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d      pNew->prev =
364f9 20 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20   pHead->prev;.  
364fa 20 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65    if( pHead->pre
364fb 76 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76  v ){ pHead->prev
364fc 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d  ->next = pNew; }
364fd 0a 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20  .    else       
364fe 20 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73        { pH->firs
364ff 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  t = pNew; }.    
36500 70 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e  pHead->prev = pN
36501 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ew;.  }else{.   
36502 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48   pNew->next = pH
36503 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28  ->first;.    if(
36504 20 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48   pH->first ){ pH
36505 2d 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20  ->first->prev = 
36506 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77  pNew; }.    pNew
36507 2d 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  ->prev = 0;.    
36508 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77  pH->first = pNew
36509 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e  ;.  }.  pEntry->
3650a 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72  count++;.  pEntr
3650b 79 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b  y->chain = pNew;
3650c 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74  .}.../* Resize t
3650d 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f  he hash table so
3650e 20 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e   that it cantain
3650f 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63  s "new_size" buc
36510 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69  kets..** "new_si
36511 7a 65 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f  ze" must be a po
36512 77 65 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68  wer of 2.  The h
36513 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20  ash table might 
36514 66 61 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69  fail .** to resi
36515 7a 65 20 69 66 20 73 71 6c 69 74 65 4d 61 6c 6c  ze if sqliteMall
36516 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73  oc() fails..*/.s
36517 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 52  tatic void fts3R
36518 65 68 61 73 68 28 46 74 73 33 48 61 73 68 20 2a  ehash(Fts3Hash *
36519 70 48 2c 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65  pH, int new_size
3651a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73  ){.  struct _fts
3651b 33 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20  3ht *new_ht;    
3651c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77        /* The new
3651d 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
3651e 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65   Fts3HashElem *e
3651f 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b  lem, *next_elem;
36520 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
36521 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 65   over existing e
36522 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  lements */.  int
36523 20 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20   (*xHash)(const 
36524 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 20 2f 2a  void*,int);   /*
36525 20 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69   The hash functi
36526 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  on */..  assert(
36527 20 28 6e 65 77 5f 73 69 7a 65 20 26 20 28 6e 65   (new_size & (ne
36528 77 5f 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b  w_size-1))==0 );
36529 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72  .  new_ht = (str
3652a 75 63 74 20 5f 66 74 73 33 68 74 20 2a 29 66 74  uct _fts3ht *)ft
3652b 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 65  s3HashMalloc( ne
3652c 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74  w_size*sizeof(st
3652d 72 75 63 74 20 5f 66 74 73 33 68 74 29 20 29 3b  ruct _fts3ht) );
3652e 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30  .  if( new_ht==0
3652f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 74 73   ) return;.  fts
36530 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e 68 74  3HashFree(pH->ht
36531 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65  );.  pH->ht = ne
36532 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69  w_ht;.  pH->htsi
36533 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20  ze = new_size;. 
36534 20 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68   xHash = ftsHash
36535 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79  Function(pH->key
36536 43 6c 61 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c  Class);.  for(el
36537 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48  em=pH->first, pH
36538 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b  ->first=0; elem;
36539 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65   elem = next_ele
3653a 6d 29 7b 0a 20 20 20 20 69 6e 74 20 68 20 3d 20  m){.    int h = 
3653b 28 2a 78 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70  (*xHash)(elem->p
3653c 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29  Key, elem->nKey)
3653d 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b   & (new_size-1);
3653e 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d  .    next_elem =
3653f 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20   elem->next;.   
36540 20 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 45   fts3HashInsertE
36541 6c 65 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f  lement(pH, &new_
36542 68 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20  ht[h], elem);.  
36543 7d 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e  }.}../* This fun
36544 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72  ction (for inter
36545 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f  nal use only) lo
36546 63 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74  cates an element
36547 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74   in an.** hash t
36548 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65  able that matche
36549 73 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e  s the given key.
3654a 20 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74    The hash for t
3654b 68 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61  his key has.** a
3654c 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70  lready been comp
3654d 75 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73  uted and is pass
3654e 65 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61  ed as the 4th pa
3654f 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  rameter..*/.stat
36550 69 63 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20  ic Fts3HashElem 
36551 2a 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74  *fts3FindElement
36552 42 79 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20  ByHash(.  const 
36553 46 74 73 33 48 61 73 68 20 2a 70 48 2c 20 2f 2a  Fts3Hash *pH, /*
36554 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65   The pH to be se
36555 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  arched */.  cons
36556 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20  t void *pKey,   
36557 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72  /* The key we ar
36558 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20  e searching for 
36559 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20  */.  int nKey,. 
3655a 20 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 20   int h          
3655b 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68       /* The hash
3655c 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a   for this key. *
3655d 2f 0a 29 7b 0a 20 20 46 74 73 33 48 61 73 68 45  /.){.  Fts3HashE
3655e 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20  lem *elem;      
3655f 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
36560 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65   loop thru the e
36561 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20  lement list */. 
36562 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20   int count;     
36563 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36564 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
36565 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65  ments left to te
36566 73 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43  st */.  int (*xC
36567 6f 6d 70 61 72 65 29 28 63 6f 6e 73 74 20 76 6f  ompare)(const vo
36568 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
36569 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 63 6f  id*,int);  /* co
3656a 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
3656b 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e  n */..  if( pH->
3656c 68 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  ht ){.    struct
3656d 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79   _fts3ht *pEntry
3656e 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20   = &pH->ht[h];. 
3656f 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79     elem = pEntry
36570 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75  ->chain;.    cou
36571 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75  nt = pEntry->cou
36572 6e 74 3b 0a 20 20 20 20 78 43 6f 6d 70 61 72 65  nt;.    xCompare
36573 20 3d 20 66 74 73 43 6f 6d 70 61 72 65 46 75 6e   = ftsCompareFun
36574 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61  ction(pH->keyCla
36575 73 73 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ss);.    while( 
36576 63 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65 6d 20  count-- && elem 
36577 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 2a 78  ){.      if( (*x
36578 43 6f 6d 70 61 72 65 29 28 65 6c 65 6d 2d 3e 70  Compare)(elem->p
36579 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70  Key,elem->nKey,p
3657a 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20  Key,nKey)==0 ){ 
3657b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
3657c 65 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  elem;.      }.  
3657d 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d      elem = elem-
3657e 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  >next;.    }.  }
3657f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
36580 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e 67  /* Remove a sing
36581 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  le entry from th
36582 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76  e hash table giv
36583 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  en a pointer to 
36584 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20  that.** element 
36585 61 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74 68  and a hash on th
36586 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e  e element's key.
36587 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
36588 66 74 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e  fts3RemoveElemen
36589 74 42 79 48 61 73 68 28 0a 20 20 46 74 73 33 48  tByHash(.  Fts3H
3658a 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20  ash *pH,        
3658b 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61   /* The pH conta
3658c 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a  ining "elem" */.
3658d 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 2a 20    Fts3HashElem* 
3658e 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65  elem,   /* The e
3658f 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d  lement to be rem
36590 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48  oved from the pH
36591 20 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20 20   */.  int h     
36592 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
36593 61 73 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ash value for th
36594 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a  e element */.){.
36595 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74    struct _fts3ht
36596 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20   *pEntry;.  if( 
36597 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20  elem->prev ){.  
36598 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65    elem->prev->ne
36599 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  xt = elem->next;
3659a 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70   .  }else{.    p
3659b 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d  H->first = elem-
3659c 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28  >next;.  }.  if(
3659d 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20   elem->next ){. 
3659e 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70     elem->next->p
3659f 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76  rev = elem->prev
365a0 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d  ;.  }.  pEntry =
365a1 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69   &pH->ht[h];.  i
365a2 66 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e  f( pEntry->chain
365a3 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45  ==elem ){.    pE
365a4 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c  ntry->chain = el
365a5 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20  em->next;.  }.  
365a6 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b  pEntry->count--;
365a7 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63  .  if( pEntry->c
365a8 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70  ount<=0 ){.    p
365a9 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30  Entry->chain = 0
365aa 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e  ;.  }.  if( pH->
365ab 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d  copyKey && elem-
365ac 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 66 74 73  >pKey ){.    fts
365ad 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e  3HashFree(elem->
365ae 70 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 66 74 73  pKey);.  }.  fts
365af 33 48 61 73 68 46 72 65 65 28 20 65 6c 65 6d 20  3HashFree( elem 
365b0 29 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d  );.  pH->count--
365b1 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e  ;.  if( pH->coun
365b2 74 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  t<=0 ){.    asse
365b3 72 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30  rt( pH->first==0
365b4 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
365b5 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a  pH->count==0 );.
365b6 20 20 20 20 66 74 73 33 48 61 73 68 43 6c 65 61      fts3HashClea
365b7 72 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  r(pH);.  }.}../*
365b8 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61   Attempt to loca
365b9 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66  te an element of
365ba 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
365bb 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a  pH with a key.**
365bc 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b   that matches pK
365bd 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e  ey,nKey.  Return
365be 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68   the data for th
365bf 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74  is element if it
365c0 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72   is.** found, or
365c1 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69   NULL if there i
365c2 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53  s no match..*/.S
365c3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
365c4 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
365c5 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 46 74  ashFind(const Ft
365c6 73 33 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73  s3Hash *pH, cons
365c7 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
365c8 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68  t nKey){.  int h
365c9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
365ca 20 20 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b    /* A hash on k
365cb 65 79 20 2a 2f 0a 20 20 46 74 73 33 48 61 73 68  ey */.  Fts3Hash
365cc 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f  Elem *elem;    /
365cd 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68  * The element th
365ce 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a  at matches key *
365cf 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29  /.  int (*xHash)
365d0 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  (const void*,int
365d1 29 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20  );  /* The hash 
365d2 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69  function */..  i
365d3 66 28 20 70 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e  f( pH==0 || pH->
365d4 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ht==0 ) return 0
365d5 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48  ;.  xHash = ftsH
365d6 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e  ashFunction(pH->
365d7 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73  keyClass);.  ass
365d8 65 72 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b  ert( xHash!=0 );
365d9 0a 20 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28  .  h = (*xHash)(
365da 70 4b 65 79 2c 6e 4b 65 79 29 3b 0a 20 20 61 73  pKey,nKey);.  as
365db 73 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a  sert( (pH->htsiz
365dc 65 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d  e & (pH->htsize-
365dd 31 29 29 3d 3d 30 20 29 3b 0a 20 20 65 6c 65 6d  1))==0 );.  elem
365de 20 3d 20 66 74 73 33 46 69 6e 64 45 6c 65 6d 65   = fts3FindEleme
365df 6e 74 42 79 48 61 73 68 28 70 48 2c 70 4b 65 79  ntByHash(pH,pKey
365e0 2c 6e 4b 65 79 2c 20 68 20 26 20 28 70 48 2d 3e  ,nKey, h & (pH->
365e1 68 74 73 69 7a 65 2d 31 29 29 3b 0a 20 20 72 65  htsize-1));.  re
365e2 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d  turn elem ? elem
365e3 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f  ->data : 0;.}../
365e4 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d  * Insert an elem
365e5 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ent into the has
365e6 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65  h table pH.  The
365e7 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65   key is pKey,nKe
365e8 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74  y.** and the dat
365e9 61 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a  a is "data"..**.
365ea 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74  ** If no element
365eb 20 65 78 69 73 74 73 20 77 69 74 68 20 61 20 6d   exists with a m
365ec 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65  atching key, the
365ed 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65  n a new.** eleme
365ee 6e 74 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  nt is created.  
365ef 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65  A copy of the ke
365f0 79 20 69 73 20 6d 61 64 65 20 69 66 20 74 68 65  y is made if the
365f1 20 63 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67   copyKey.** flag
365f2 20 69 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69   is set.  NULL i
365f3 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
365f4 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65  * If another ele
365f5 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69  ment already exi
365f6 73 74 73 20 77 69 74 68 20 74 68 65 20 73 61 6d  sts with the sam
365f7 65 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a  e key, then the.
365f8 2a 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c  ** new data repl
365f9 61 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74  aces the old dat
365fa 61 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61  a and the old da
365fb 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  ta is returned..
365fc 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f  ** The key is no
365fd 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73  t copied in this
365fe 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61   instance.  If a
365ff 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74   malloc fails, t
36600 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64  hen.** the new d
36601 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 20  ata is returned 
36602 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 62  and the hash tab
36603 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  le is unchanged.
36604 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64  .**.** If the "d
36605 61 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 74  ata" parameter t
36606 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  o this function 
36607 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68  is NULL, then th
36608 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72  e.** element cor
36609 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b  responding to "k
3660a 65 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66  ey" is removed f
3660b 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
3660c 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
3660d 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
3660e 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72  te3Fts3HashInser
3660f 74 28 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70  t(.  Fts3Hash *p
36610 48 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  H,        /* The
36611 20 68 61 73 68 20 74 61 62 6c 65 20 74 6f 20 69   hash table to i
36612 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20  nsert into */.  
36613 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
36614 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20  ,    /* The key 
36615 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20  */.  int nKey,  
36616 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
36617 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
36618 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 76 6f 69  the key */.  voi
36619 64 20 2a 64 61 74 61 20 20 20 20 20 20 20 20 20  d *data         
3661a 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f    /* The data */
3661b 0a 29 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20  .){.  int hraw; 
3661c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3661d 2f 2a 20 52 61 77 20 68 61 73 68 20 76 61 6c 75  /* Raw hash valu
3661e 65 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a  e of the key */.
3661f 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20    int h;        
36620 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74              /* t
36621 68 65 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b  he hash of the k
36622 65 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74  ey modulo hash t
36623 61 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 46  able size */.  F
36624 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  ts3HashElem *ele
36625 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64  m;       /* Used
36626 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68   to loop thru th
36627 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a  e element list *
36628 2f 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d  /.  Fts3HashElem
36629 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a   *new_elem;   /*
3662a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64   New element add
3662b 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a  ed to the pH */.
3662c 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63    int (*xHash)(c
3662d 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b  onst void*,int);
3662e 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75    /* The hash fu
3662f 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73  nction */..  ass
36630 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20  ert( pH!=0 );.  
36631 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46  xHash = ftsHashF
36632 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43  unction(pH->keyC
36633 6c 61 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28  lass);.  assert(
36634 20 78 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68   xHash!=0 );.  h
36635 72 61 77 20 3d 20 28 2a 78 48 61 73 68 29 28 70  raw = (*xHash)(p
36636 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 61 73  Key, nKey);.  as
36637 73 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a  sert( (pH->htsiz
36638 65 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d  e & (pH->htsize-
36639 31 29 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20  1))==0 );.  h = 
3663a 68 72 61 77 20 26 20 28 70 48 2d 3e 68 74 73 69  hraw & (pH->htsi
3663b 7a 65 2d 31 29 3b 0a 20 20 65 6c 65 6d 20 3d 20  ze-1);.  elem = 
3663c 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42  fts3FindElementB
3663d 79 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b  yHash(pH,pKey,nK
3663e 65 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65  ey,h);.  if( ele
3663f 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f  m ){.    void *o
36640 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e  ld_data = elem->
36641 64 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61  data;.    if( da
36642 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66  ta==0 ){.      f
36643 74 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74  ts3RemoveElement
36644 42 79 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68  ByHash(pH,elem,h
36645 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
36646 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d      elem->data =
36647 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20   data;.    }.   
36648 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61   return old_data
36649 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61  ;.  }.  if( data
3664a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
3664b 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65    if( pH->htsize
3664c 3d 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 52  ==0 ){.    fts3R
3664d 65 68 61 73 68 28 70 48 2c 38 29 3b 0a 20 20 20  ehash(pH,8);.   
3664e 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d   if( pH->htsize=
3664f 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 48 2d 3e  =0 ){.      pH->
36650 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  count = 0;.     
36651 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20   return data;.  
36652 20 20 7d 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c    }.  }.  new_el
36653 65 6d 20 3d 20 28 46 74 73 33 48 61 73 68 45 6c  em = (Fts3HashEl
36654 65 6d 2a 29 66 74 73 33 48 61 73 68 4d 61 6c 6c  em*)fts3HashMall
36655 6f 63 28 20 73 69 7a 65 6f 66 28 46 74 73 33 48  oc( sizeof(Fts3H
36656 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66  ashElem) );.  if
36657 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20  ( new_elem==0 ) 
36658 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 69  return data;.  i
36659 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26  f( pH->copyKey &
3665a 26 20 70 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20  & pKey!=0 ){.   
3665b 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20   new_elem->pKey 
3665c 3d 20 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63  = fts3HashMalloc
3665d 28 20 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66  ( nKey );.    if
3665e 28 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79  ( new_elem->pKey
3665f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73  ==0 ){.      fts
36660 33 48 61 73 68 46 72 65 65 28 6e 65 77 5f 65 6c  3HashFree(new_el
36661 65 6d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  em);.      retur
36662 6e 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20  n data;.    }.  
36663 20 20 6d 65 6d 63 70 79 28 28 76 6f 69 64 2a 29    memcpy((void*)
36664 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20  new_elem->pKey, 
36665 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d  pKey, nKey);.  }
36666 65 6c 73 65 7b 0a 20 20 20 20 6e 65 77 5f 65 6c  else{.    new_el
36667 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64  em->pKey = (void
36668 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a 20 20 6e 65  *)pKey;.  }.  ne
36669 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e  w_elem->nKey = n
3666a 4b 65 79 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74  Key;.  pH->count
3666b 2b 2b 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f  ++;.  if( pH->co
3666c 75 6e 74 20 3e 20 70 48 2d 3e 68 74 73 69 7a 65  unt > pH->htsize
3666d 20 29 7b 0a 20 20 20 20 66 74 73 33 52 65 68 61   ){.    fts3Reha
3666e 73 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65  sh(pH,pH->htsize
3666f 2a 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  *2);.  }.  asser
36670 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20  t( pH->htsize>0 
36671 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 48  );.  assert( (pH
36672 2d 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e  ->htsize & (pH->
36673 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b  htsize-1))==0 );
36674 0a 20 20 68 20 3d 20 68 72 61 77 20 26 20 28 70  .  h = hraw & (p
36675 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20  H->htsize-1);.  
36676 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c  fts3HashInsertEl
36677 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68  ement(pH, &pH->h
36678 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b  t[h], new_elem);
36679 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74  .  new_elem->dat
3667a 61 20 3d 20 64 61 74 61 3b 0a 20 20 72 65 74 75  a = data;.  retu
3667b 72 6e 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  rn 0;.}..#endif 
3667c 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
3667d 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
3667e 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
3667f 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a  E_FTS3) */../***
36680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
36681 6f 66 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a  of fts3_hash.c *
36682 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36683 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
36685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
36686 6e 20 66 69 6c 65 20 66 74 73 33 5f 70 6f 72 74  n file fts3_port
36687 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
36688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36689 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
3668a 2a 20 32 30 30 36 20 53 65 70 74 65 6d 62 65 72  * 2006 September
3668b 20 33 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   30.**.** The au
3668c 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
3668d 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
3668e 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
3668f 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
36690 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
36691 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
36692 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
36693 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
36694 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
36695 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
36696 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
36697 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
36698 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
36699 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
3669a 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
3669b 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
3669c 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
3669d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3669e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3669f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
366a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
366a1 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  **.** Implementa
366a2 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6c 6c  tion of the full
366a3 2d 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b  -text-search tok
366a4 65 6e 69 7a 65 72 20 74 68 61 74 20 69 6d 70 6c  enizer that impl
366a5 65 6d 65 6e 74 73 0a 2a 2a 20 61 20 50 6f 72 74  ements.** a Port
366a6 65 72 20 73 74 65 6d 6d 65 72 2e 0a 2a 2f 0a 0a  er stemmer..*/..
366a7 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
366a8 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
366a9 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
366aa 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
366ab 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
366ac 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
366ad 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
366ae 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
366af 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
366b0 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
366b1 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
366b2 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
366b3 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
366b4 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
366b5 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
366b6 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
366b7 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
366b8 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
366b9 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
366ba 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
366bb 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
366bc 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 2f 2a  ABLE_FTS3)..../*
366bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
366be 63 6c 75 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e  clude fts3_token
366bf 69 7a 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69  izer.h in the mi
366c0 64 64 6c 65 20 6f 66 20 66 74 73 33 5f 70 6f 72  ddle of fts3_por
366c1 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ter.c ******/./*
366c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
366c3 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f  gin file fts3_to
366c4 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a  kenizer.h ******
366c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
366c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
366c7 0a 2a 2a 20 32 30 30 36 20 4a 75 6c 79 20 31 30  .** 2006 July 10
366c8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
366c9 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
366ca 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
366cb 75 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  urce code..**.**
366cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
366cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
366ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
366cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
366d0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e  *******.** Defin
366d1 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  es the interface
366d2 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75   to tokenizers u
366d3 73 65 64 20 62 79 20 66 75 6c 6c 74 65 78 74 2d  sed by fulltext-
366d4 73 65 61 72 63 68 2e 20 20 54 68 65 72 65 0a 2a  search.  There.*
366d5 2a 20 61 72 65 20 74 68 72 65 65 20 62 61 73 69  * are three basi
366d6 63 20 63 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a  c components:.**
366d7 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  .** sqlite3_toke
366d8 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20  nizer_module is 
366d9 61 20 73 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69  a singleton defi
366da 6e 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a  ning the tokeniz
366db 65 72 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  er.** interface 
366dc 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  functions.  This
366dd 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 20   is essentially 
366de 74 68 65 20 63 6c 61 73 73 20 73 74 72 75 63 74  the class struct
366df 75 72 65 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e  ure for.** token
366e0 69 7a 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c  izers..**.** sql
366e1 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69  ite3_tokenizer i
366e2 73 20 75 73 65 64 20 74 6f 20 64 65 66 69 6e 65  s used to define
366e3 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 6f   a particular to
366e4 6b 65 6e 69 7a 65 72 2c 20 70 65 72 68 61 70 73  kenizer, perhaps
366e5 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 75  .** including cu
366e6 73 74 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f  stomization info
366e7 72 6d 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  rmation defined 
366e8 61 74 20 63 72 65 61 74 69 6f 6e 20 74 69 6d 65  at creation time
366e9 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ..**.** sqlite3_
366ea 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
366eb 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79   is generated by
366ec 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20   a tokenizer to 
366ed 67 65 6e 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65  generate.** toke
366ee 6e 73 20 66 72 6f 6d 20 61 20 70 61 72 74 69 63  ns from a partic
366ef 75 6c 61 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23  ular input..*/.#
366f0 69 66 6e 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b  ifndef _FTS3_TOK
366f1 45 4e 49 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e  ENIZER_H_.#defin
366f2 65 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  e _FTS3_TOKENIZE
366f3 52 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  R_H_../* TODO(sh
366f4 65 73 73 29 20 4f 6e 6c 79 20 75 73 65 64 20 66  ess) Only used f
366f5 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64  or SQLITE_OK and
366f6 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20   SQLITE_DONE at 
366f7 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66  this time..** If
366f8 20 74 6f 6b 65 6e 69 7a 65 72 73 20 61 72 65 20   tokenizers are 
366f9 74 6f 20 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f  to be allowed to
366fa 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28   call sqlite3_*(
366fb 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65  ) functions, the
366fc 6e 0a 2a 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65  n.** we will nee
366fd 64 20 61 20 77 61 79 20 74 6f 20 72 65 67 69 73  d a way to regis
366fe 74 65 72 20 74 68 65 20 41 50 49 20 63 6f 6e 73  ter the API cons
366ff 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a  istently..*/../*
36700 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 73 20 75  .** Structures u
36701 73 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e  sed by the token
36702 69 7a 65 72 20 69 6e 74 65 72 66 61 63 65 2e 20  izer interface. 
36703 57 68 65 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e  When a new token
36704 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  izer.** implemen
36705 74 61 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74  tation is regist
36706 65 72 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72  ered, the caller
36707 20 70 72 6f 76 69 64 65 73 20 61 20 70 6f 69 6e   provides a poin
36708 74 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c  ter to.** an sql
36709 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
3670a 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  odule containing
3670b 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
3670c 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e   callback.** fun
3670d 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65  ctions that make
3670e 20 75 70 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74   up an implement
3670f 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  ation..**.** Whe
36710 6e 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 20  n an fts3 table 
36711 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 70  is created, it p
36712 61 73 73 65 73 20 61 6e 79 20 61 72 67 75 6d 65  asses any argume
36713 6e 74 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a  nts passed to.**
36714 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63   the tokenizer c
36715 6c 61 75 73 65 20 6f 66 20 74 68 65 20 43 52 45  lause of the CRE
36716 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
36717 45 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74  E statement to t
36718 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f  he.** sqlite3_to
36719 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78  kenizer_module.x
3671a 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f  Create() functio
3671b 6e 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  n of the request
3671c 65 64 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20  ed tokenizer.** 
3671d 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
3671e 54 68 65 20 78 43 72 65 61 74 65 28 29 20 66 75  The xCreate() fu
3671f 6e 63 74 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72  nction in turn r
36720 65 74 75 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71  eturns an .** sq
36721 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
36722 73 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73  structure repres
36723 65 6e 74 69 6e 67 20 74 68 65 20 73 70 65 63 69  enting the speci
36724 66 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f  fic tokenizer to
36725 0a 2a 2a 20 62 65 20 75 73 65 64 20 66 6f 72 20  .** be used for 
36726 74 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 28  the fts3 table (
36727 63 75 73 74 6f 6d 69 7a 65 64 20 62 79 20 74 68  customized by th
36728 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75  e tokenizer clau
36729 73 65 20 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a  se arguments)..*
3672a 2a 0a 2a 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65  *.** To tokenize
3672b 20 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72   an input buffer
3672c 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f  , the sqlite3_to
3672d 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78  kenizer_module.x
3672e 4f 70 65 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  Open().** method
3672f 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 72   is called. It r
36730 65 74 75 72 6e 73 20 61 6e 20 73 71 6c 69 74 65  eturns an sqlite
36731 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
36732 6f 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61  or object.** tha
36733 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  t may be used to
36734 20 74 6f 6b 65 6e 69 7a 65 20 61 20 73 70 65 63   tokenize a spec
36735 69 66 69 63 20 69 6e 70 75 74 20 62 75 66 66 65  ific input buffe
36736 72 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68  r based on.** th
36737 65 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72  e tokenization r
36738 75 6c 65 73 20 73 75 70 70 6c 69 65 64 20 62 79  ules supplied by
36739 20 61 20 73 70 65 63 69 66 69 63 20 73 71 6c 69   a specific sqli
3673a 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  te3_tokenizer.**
3673b 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65   object..*/.type
3673c 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
3673d 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3673e 75 6c 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule sqlite3_toke
3673f 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79  nizer_module;.ty
36740 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
36741 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73  ite3_tokenizer s
36742 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
36743 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
36744 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
36745 65 72 5f 63 75 72 73 6f 72 20 73 71 6c 69 74 65  er_cursor sqlite
36746 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
36747 6f 72 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69  or;..struct sqli
36748 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
36749 64 75 6c 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a  dule {..  /*.  *
3674a 2a 20 53 74 72 75 63 74 75 72 65 20 76 65 72 73  * Structure vers
3674b 69 6f 6e 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61  ion. Should alwa
3674c 79 73 20 62 65 20 73 65 74 20 74 6f 20 30 2e 0a  ys be set to 0..
3674d 20 20 2a 2f 0a 20 20 69 6e 74 20 69 56 65 72 73    */.  int iVers
3674e 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ion;..  /*.  ** 
3674f 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b  Create a new tok
36750 65 6e 69 7a 65 72 2e 20 54 68 65 20 76 61 6c 75  enizer. The valu
36751 65 73 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d  es in the argv[]
36752 20 61 72 72 61 79 20 61 72 65 20 74 68 65 0a 20   array are the. 
36753 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 61   ** arguments pa
36754 73 73 65 64 20 74 6f 20 74 68 65 20 22 74 6f 6b  ssed to the "tok
36755 65 6e 69 7a 65 72 22 20 63 6c 61 75 73 65 20 6f  enizer" clause o
36756 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52  f the CREATE VIR
36757 54 55 41 4c 0a 20 20 2a 2a 20 54 41 42 4c 45 20  TUAL.  ** TABLE 
36758 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63  statement that c
36759 72 65 61 74 65 64 20 74 68 65 20 66 74 73 33 20  reated the fts3 
3675a 74 61 62 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70  table. For examp
3675b 6c 65 2c 20 69 66 0a 20 20 2a 2a 20 74 68 65 20  le, if.  ** the 
3675c 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73  following SQL is
3675d 20 65 78 65 63 75 74 65 64 3a 0a 20 20 2a 2a 0a   executed:.  **.
3675e 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 2e 2e    **   CREATE ..
3675f 20 55 53 49 4e 47 20 66 74 73 33 28 20 2e 2e 2e   USING fts3( ...
36760 20 2c 20 74 6f 6b 65 6e 69 7a 65 72 20 3c 74 6f   , tokenizer <to
36761 6b 65 6e 69 7a 65 72 2d 6e 61 6d 65 3e 20 61 72  kenizer-name> ar
36762 67 31 20 61 72 67 32 29 0a 20 20 2a 2a 0a 20 20  g1 arg2).  **.  
36763 2a 2a 20 74 68 65 6e 20 61 72 67 63 20 69 73 20  ** then argc is 
36764 73 65 74 20 74 6f 20 32 2c 20 61 6e 64 20 74 68  set to 2, and th
36765 65 20 61 72 67 76 5b 5d 20 61 72 72 61 79 20 63  e argv[] array c
36766 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73  ontains pointers
36767 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 73 74 72  .  ** to the str
36768 69 6e 67 73 20 22 61 72 67 31 22 20 61 6e 64 20  ings "arg1" and 
36769 22 61 72 67 32 22 2e 0a 20 20 2a 2a 0a 20 20 2a  "arg2"..  **.  *
3676a 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20 73 68  * This method sh
3676b 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68  ould return eith
3676c 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29  er SQLITE_OK (0)
3676d 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65  , or an SQLite e
3676e 72 72 6f 72 20 0a 20 20 2a 2a 20 63 6f 64 65 2e  rror .  ** code.
3676f 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73   If SQLITE_OK is
36770 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20   returned, then 
36771 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 73 68 6f  *ppTokenizer sho
36772 75 6c 64 20 62 65 20 73 65 74 0a 20 20 2a 2a 20  uld be set.  ** 
36773 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
36774 6e 65 77 6c 79 20 63 72 65 61 74 65 64 20 74 6f  newly created to
36775 6b 65 6e 69 7a 65 72 20 73 74 72 75 63 74 75 72  kenizer structur
36776 65 2e 20 54 68 65 20 67 65 6e 65 72 69 63 0a 20  e. The generic. 
36777 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65   ** sqlite3_toke
36778 6e 69 7a 65 72 2e 70 4d 6f 64 75 6c 65 20 76 61  nizer.pModule va
36779 72 69 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f  riable should no
3677a 74 20 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64  t be initialised
3677b 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 63 61   by.  ** this ca
3677c 6c 6c 62 61 63 6b 2e 20 54 68 65 20 63 61 6c 6c  llback. The call
3677d 65 72 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 0a 20  er will do so.. 
3677e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 72 65   */.  int (*xCre
3677f 61 74 65 29 28 0a 20 20 20 20 69 6e 74 20 61 72  ate)(.    int ar
36780 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gc,             
36781 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36782 20 53 69 7a 65 20 6f 66 20 61 72 67 76 20 61 72   Size of argv ar
36783 72 61 79 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  ray */.    const
36784 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
36785 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  v,             /
36786 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75  * Tokenizer argu
36787 6d 65 6e 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a  ment strings */.
36788 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65      sqlite3_toke
36789 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69  nizer **ppTokeni
3678a 7a 65 72 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  zer     /* OUT: 
3678b 43 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65  Created tokenize
3678c 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a  r */.  );..  /*.
3678d 20 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20    ** Destroy an 
3678e 65 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a  existing tokeniz
3678f 65 72 2e 20 54 68 65 20 66 74 73 33 20 6d 6f 64  er. The fts3 mod
36790 75 6c 65 20 63 61 6c 6c 73 20 74 68 69 73 20 6d  ule calls this m
36791 65 74 68 6f 64 0a 20 20 2a 2a 20 65 78 61 63 74  ethod.  ** exact
36792 6c 79 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ly once for each
36793 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
36794 20 74 6f 20 78 43 72 65 61 74 65 28 29 2e 0a 20   to xCreate().. 
36795 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 44 65 73   */.  int (*xDes
36796 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 74 6f  troy)(sqlite3_to
36797 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
36798 7a 65 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  zer);..  /*.  **
36799 20 43 72 65 61 74 65 20 61 20 74 6f 6b 65 6e 69   Create a tokeni
3679a 7a 65 72 20 63 75 72 73 6f 72 20 74 6f 20 74 6f  zer cursor to to
3679b 6b 65 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20  kenize an input 
3679c 62 75 66 66 65 72 2e 20 54 68 65 20 63 61 6c 6c  buffer. The call
3679d 65 72 0a 20 20 2a 2a 20 69 73 20 72 65 73 70 6f  er.  ** is respo
3679e 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72  nsible for ensur
3679f 69 6e 67 20 74 68 61 74 20 74 68 65 20 69 6e 70  ing that the inp
367a0 75 74 20 62 75 66 66 65 72 20 72 65 6d 61 69 6e  ut buffer remain
367a1 73 20 76 61 6c 69 64 0a 20 20 2a 2a 20 75 6e 74  s valid.  ** unt
367a2 69 6c 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  il the cursor is
367a3 20 63 6c 6f 73 65 64 20 28 75 73 69 6e 67 20 74   closed (using t
367a4 68 65 20 78 43 6c 6f 73 65 28 29 20 6d 65 74 68  he xClose() meth
367a5 6f 64 29 2e 20 0a 20 20 2a 2f 0a 20 20 69 6e 74  od). .  */.  int
367a6 20 28 2a 78 4f 70 65 6e 29 28 0a 20 20 20 20 73   (*xOpen)(.    s
367a7 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
367a8 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20   *pTokenizer,   
367a9 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
367aa 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 63   object */.    c
367ab 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75  onst char *pInpu
367ac 74 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  t, int nBytes,  
367ad 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 62 75 66      /* Input buf
367ae 66 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  fer */.    sqlit
367af 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
367b0 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20  sor **ppCursor  
367b1 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20  /* OUT: Created 
367b2 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  tokenizer cursor
367b3 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20   */.  );..  /*. 
367b4 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65   ** Destroy an e
367b5 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65  xisting tokenize
367b6 72 20 63 75 72 73 6f 72 2e 20 54 68 65 20 66 74  r cursor. The ft
367b7 73 33 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20  s3 module calls 
367b8 74 68 69 73 20 0a 20 20 2a 2a 20 6d 65 74 68 6f  this .  ** metho
367b9 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66  d exactly once f
367ba 6f 72 20 65 61 63 68 20 73 75 63 63 65 73 73 66  or each successf
367bb 75 6c 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e  ul call to xOpen
367bc 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  ()..  */.  int (
367bd 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33  *xClose)(sqlite3
367be 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
367bf 72 20 2a 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20  r *pCursor);..  
367c0 2f 2a 0a 20 20 2a 2a 20 52 65 74 72 69 65 76 65  /*.  ** Retrieve
367c1 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20   the next token 
367c2 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a  from the tokeniz
367c3 65 72 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f  er cursor pCurso
367c4 72 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 65 74  r. This.  ** met
367c5 68 6f 64 20 73 68 6f 75 6c 64 20 65 69 74 68 65  hod should eithe
367c6 72 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r return SQLITE_
367c7 4f 4b 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  OK and set the v
367c8 61 6c 75 65 73 20 6f 66 20 74 68 65 0a 20 20 2a  alues of the.  *
367c9 2a 20 22 4f 55 54 22 20 76 61 72 69 61 62 6c 65  * "OUT" variable
367ca 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 65 6c  s identified bel
367cb 6f 77 2c 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f  ow, or SQLITE_DO
367cc 4e 45 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  NE to indicate t
367cd 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64  hat.  ** the end
367ce 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 68   of the buffer h
367cf 61 73 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c  as been reached,
367d0 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72   or an SQLite er
367d1 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20  ror code..  **. 
367d2 20 2a 2a 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f   ** *ppToken sho
367d3 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 70 6f  uld be set to po
367d4 69 6e 74 20 61 74 20 61 20 62 75 66 66 65 72 20  int at a buffer 
367d5 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 0a  containing the .
367d6 20 20 2a 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20    ** normalized 
367d7 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 74  version of the t
367d8 6f 6b 65 6e 20 28 69 2e 65 2e 20 61 66 74 65 72  oken (i.e. after
367d9 20 61 6e 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e   any case-foldin
367da 67 20 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 74  g and/or.  ** st
367db 65 6d 6d 69 6e 67 20 68 61 73 20 62 65 65 6e 20  emming has been 
367dc 70 65 72 66 6f 72 6d 65 64 29 2e 20 2a 70 6e 42  performed). *pnB
367dd 79 74 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73  ytes should be s
367de 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68  et to the length
367df 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 62 75  .  ** of this bu
367e0 66 66 65 72 20 69 6e 20 62 79 74 65 73 2e 20 54  ffer in bytes. T
367e1 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 74 68  he input text th
367e2 61 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 65  at generated the
367e3 20 74 6f 6b 65 6e 20 69 73 0a 20 20 2a 2a 20 69   token is.  ** i
367e4 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
367e5 20 62 79 74 65 20 6f 66 66 73 65 74 73 20 72 65   byte offsets re
367e6 74 75 72 6e 65 64 20 69 6e 20 2a 70 69 53 74 61  turned in *piSta
367e7 72 74 4f 66 66 73 65 74 20 61 6e 64 0a 20 20 2a  rtOffset and.  *
367e8 2a 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2e 20  * *piEndOffset. 
367e9 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 73  *piStartOffset s
367ea 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
367eb 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
367ec 20 66 69 72 73 74 0a 20 20 2a 2a 20 62 79 74 65   first.  ** byte
367ed 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e   of the token in
367ee 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65   the input buffe
367ef 72 2e 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20  r. *piEndOffset 
367f0 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a 20 20  should be set.  
367f1 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20  ** to the index 
367f2 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 79 74  of the first byt
367f3 65 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20  e just past the 
367f4 65 6e 64 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  end of the token
367f5 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 69 6e 70   in.  ** the inp
367f6 75 74 20 62 75 66 66 65 72 2e 0a 20 20 2a 2a 0a  ut buffer..  **.
367f7 20 20 2a 2a 20 54 68 65 20 62 75 66 66 65 72 20    ** The buffer 
367f8 2a 70 70 54 6f 6b 65 6e 20 69 73 20 73 65 74 20  *ppToken is set 
367f9 74 6f 20 70 6f 69 6e 74 20 61 74 20 69 73 20 6d  to point at is m
367fa 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 74 6f  anaged by the to
367fb 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6d 70  kenizer.  ** imp
367fc 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 74 20  lementation. It 
367fd 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
367fe 20 74 6f 20 62 65 20 76 61 6c 69 64 20 75 6e 74   to be valid unt
367ff 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  il the next call
36800 0a 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74 28 29  .  ** to xNext()
36801 20 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20 0a 20   or xClose(). . 
36802 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
36803 65 73 73 29 20 63 75 72 72 65 6e 74 20 69 6d 70  ess) current imp
36804 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75  lementation requ
36805 69 72 65 73 20 70 49 6e 70 75 74 20 74 6f 20 62  ires pInput to b
36806 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  e.  ** nul-termi
36807 6e 61 74 65 64 2e 20 20 54 68 69 73 20 73 68 6f  nated.  This sho
36808 75 6c 64 20 65 69 74 68 65 72 20 62 65 20 66 69  uld either be fi
36809 78 65 64 2c 20 6f 72 20 70 49 6e 70 75 74 2f 6e  xed, or pInput/n
3680a 42 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f 75 6c  Bytes.  ** shoul
3680b 64 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74  d be converted t
3680c 6f 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20  o zInput..  */. 
3680d 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 0a 20   int (*xNext)(. 
3680e 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
3680f 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
36810 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b 65 6e  rsor,   /* Token
36811 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
36812 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a     const char **
36813 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a 70 6e  ppToken, int *pn
36814 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54 3a 20  Bytes,  /* OUT: 
36815 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78 74 20  Normalized text 
36816 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  for token */.   
36817 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
36818 73 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 42 79  set,  /* OUT: By
36819 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  te offset of tok
3681a 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
3681b 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
3681c 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 2f  iEndOffset,    /
3681d 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66 66 73  * OUT: Byte offs
3681e 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 74 6f 6b  et of end of tok
3681f 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
36820 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
36821 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 2f  iPosition      /
36822 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
36823 20 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e 65 64   tokens returned
36824 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 6e 65   before this one
36825 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73 74 72   */.  );.};..str
36826 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  uct sqlite3_toke
36827 6e 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20  nizer {.  const 
36828 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
36829 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  r_module *pModul
3682a 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  e;  /* The modul
3682b 65 20 66 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e  e for this token
3682c 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b  izer */.  /* Tok
3682d 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
3682e 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
3682f 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
36830 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
36831 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  ;..struct sqlite
36832 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
36833 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
36834 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
36835 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54  izer;       /* T
36836 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69  okenizer for thi
36837 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 2f  s cursor. */.  /
36838 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  * Tokenizer impl
36839 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c  ementations will
3683a 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61   typically add a
3683b 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
3683c 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66 20 2f   */.};..#endif /
3683d 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  * _FTS3_TOKENIZE
3683e 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  R_H_ */../******
3683f 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
36840 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68  fts3_tokenizer.h
36841 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
36842 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36843 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
36844 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
36845 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
36846 74 20 6f 66 66 20 69 6e 20 66 74 73 33 5f 70 6f  t off in fts3_po
36847 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  rter.c *********
36848 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
36849 43 6c 61 73 73 20 64 65 72 69 76 65 64 20 66 72  Class derived fr
3684a 6f 6d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  om sqlite3_token
3684b 69 7a 65 72 0a 2a 2f 0a 74 79 70 65 64 65 66 20  izer.*/.typedef 
3684c 73 74 72 75 63 74 20 70 6f 72 74 65 72 5f 74 6f  struct porter_to
3684d 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69  kenizer {.  sqli
3684e 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61  te3_tokenizer ba
3684f 73 65 3b 20 20 20 20 20 20 2f 2a 20 42 61 73 65  se;      /* Base
36850 20 63 6c 61 73 73 20 2a 2f 0a 7d 20 70 6f 72 74   class */.} port
36851 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 2f  er_tokenizer;../
36852 2a 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72 69 76  *.** Class deriv
36853 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 33 5f 74  ed from sqlit3_t
36854 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 0a  okenizer_cursor.
36855 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
36856 74 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a  t porter_tokeniz
36857 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71  er_cursor {.  sq
36858 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
36859 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 63  cursor base;.  c
3685a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75  onst char *zInpu
3685b 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69  t;          /* i
3685c 6e 70 75 74 20 77 65 20 61 72 65 20 74 6f 6b 65  nput we are toke
3685d 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  nizing */.  int 
3685e 6e 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20  nInput;         
3685f 20 20 20 20 20 20 20 20 20 2f 2a 20 73 69 7a 65           /* size
36860 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 2a 2f   of the input */
36861 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20  .  int iOffset; 
36862 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36863 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74  /* current posit
36864 69 6f 6e 20 69 6e 20 7a 49 6e 70 75 74 20 2a 2f  ion in zInput */
36865 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20  .  int iToken;  
36866 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36867 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e 65 78 74  /* index of next
36868 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20 72 65 74   token to be ret
36869 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  urned */.  char 
3686a 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20  *zToken;        
3686b 20 20 20 20 20 20 20 20 2f 2a 20 73 74 6f 72 61          /* stora
3686c 67 65 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74  ge for current t
3686d 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  oken */.  int nA
3686e 6c 6c 6f 63 61 74 65 64 3b 20 20 20 20 20 20 20  llocated;       
3686f 20 20 20 20 20 20 20 2f 2a 20 73 70 61 63 65 20         /* space 
36870 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f  allocated to zTo
36871 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20  ken buffer */.} 
36872 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
36873 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f  _cursor;.../* Fo
36874 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
36875 6e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  n */.static cons
36876 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
36877 7a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72 74 65  zer_module porte
36878 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
36879 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  ;.../*.** Create
3687a 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72   a new tokenizer
3687b 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74   instance..*/.st
3687c 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 43  atic int porterC
3687d 72 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67  reate(.  int arg
3687e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  c, const char * 
3687f 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 73  const *argv,.  s
36880 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
36881 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29   **ppTokenizer.)
36882 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  {.  porter_token
36883 69 7a 65 72 20 2a 74 3b 0a 20 20 74 20 3d 20 28  izer *t;.  t = (
36884 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
36885 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   *) sqlite3_mall
36886 6f 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a  oc(sizeof(*t));.
36887 20 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29 20    if( t==NULL ) 
36888 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
36889 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74 2c  MEM;.  memset(t,
3688a 20 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29 3b   0, sizeof(*t));
3688b 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20  .  *ppTokenizer 
3688c 3d 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20 72 65  = &t->base;.  re
3688d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3688e 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79  }../*.** Destroy
3688f 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a   a tokenizer.*/.
36890 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65  static int porte
36891 72 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  rDestroy(sqlite3
36892 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
36893 65 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c 69 74  enizer){.  sqlit
36894 65 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e 69 7a  e3_free(pTokeniz
36895 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  er);.  return SQ
36896 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
36897 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67  * Prepare to beg
36898 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20  in tokenizing a 
36899 70 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e  particular strin
3689a 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a  g.  The input.**
3689b 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f   string to be to
3689c 6b 65 6e 69 7a 65 64 20 69 73 20 7a 49 6e 70 75  kenized is zInpu
3689d 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d 2e 20  t[0..nInput-1]. 
3689e 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65   A cursor.** use
3689f 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c  d to incremental
368a0 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73  ly tokenize this
368a1 20 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72   string is retur
368a2 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75  ned in .** *ppCu
368a3 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rsor..*/.static 
368a4 69 6e 74 20 70 6f 72 74 65 72 4f 70 65 6e 28 0a  int porterOpen(.
368a5 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
368a6 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c  zer *pTokenizer,
368a7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
368a8 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63  tokenizer */.  c
368a9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75  onst char *zInpu
368aa 74 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20  t, int nInput,  
368ab 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
368ac 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
368ad 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
368ae 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a  enizer_cursor **
368af 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f  ppCursor    /* O
368b0 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e  UT: Tokenization
368b1 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20   cursor */.){.  
368b2 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
368b3 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20 63  _cursor *c;..  c
368b4 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e   = (porter_token
368b5 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 73  izer_cursor *) s
368b6 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
368b7 7a 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69 66 28  zeof(*c));.  if(
368b8 20 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72   c==NULL ) retur
368b9 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
368ba 0a 20 20 63 2d 3e 7a 49 6e 70 75 74 20 3d 20 7a  .  c->zInput = z
368bb 49 6e 70 75 74 3b 0a 20 20 69 66 28 20 7a 49 6e  Input;.  if( zIn
368bc 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 63 2d  put==0 ){.    c-
368bd 3e 6e 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20 7d  >nInput = 0;.  }
368be 65 6c 73 65 20 69 66 28 20 6e 49 6e 70 75 74 3c  else if( nInput<
368bf 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70  0 ){.    c->nInp
368c0 75 74 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e  ut = (int)strlen
368c1 28 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 65 6c 73  (zInput);.  }els
368c2 65 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74  e{.    c->nInput
368c3 20 3d 20 6e 49 6e 70 75 74 3b 0a 20 20 7d 0a 20   = nInput;.  }. 
368c4 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b   c->iOffset = 0;
368c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368c6 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65 6e 69   /* start tokeni
368c7 7a 69 6e 67 20 61 74 20 74 68 65 20 62 65 67 69  zing at the begi
368c8 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e 69 54  nning */.  c->iT
368c9 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d 3e 7a  oken = 0;.  c->z
368ca 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20 20 20  Token = NULL;   
368cb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
368cc 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  o space allocate
368cd 64 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63 2d 3e  d, yet. */.  c->
368ce 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b 0a  nAllocated = 0;.
368cf 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26  .  *ppCursor = &
368d0 63 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72  c->base;.  retur
368d1 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
368d2 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f  /*.** Close a to
368d3 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
368d4 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65  r previously ope
368d5 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  ned by a call to
368d6 0a 2a 2a 20 70 6f 72 74 65 72 4f 70 65 6e 28 29  .** porterOpen()
368d7 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69   above..*/.stati
368d8 63 20 69 6e 74 20 70 6f 72 74 65 72 43 6c 6f 73  c int porterClos
368d9 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
368da 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
368db 73 6f 72 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74  sor){.  porter_t
368dc 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
368dd 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b  *c = (porter_tok
368de 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29  enizer_cursor *)
368df 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69   pCursor;.  sqli
368e0 74 65 33 5f 66 72 65 65 28 63 2d 3e 7a 54 6f 6b  te3_free(c->zTok
368e1 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  en);.  sqlite3_f
368e2 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e  ree(c);.  return
368e3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a   SQLITE_OK;.}./*
368e4 0a 2a 2a 20 56 6f 77 65 6c 20 6f 72 20 63 6f 6e  .** Vowel or con
368e5 73 6f 6e 61 6e 74 0a 2a 2f 0a 73 74 61 74 69 63  sonant.*/.static
368e6 20 63 6f 6e 73 74 20 63 68 61 72 20 63 54 79 70   const char cTyp
368e7 65 5b 5d 20 3d 20 7b 0a 20 20 20 30 2c 20 31 2c  e[] = {.   0, 1,
368e8 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 1, 0, 1, 1, 
368e9 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 0, 1, 1, 1, 1
368ea 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 0, 1, 1, 1,
368eb 20 31 2c 20 31 2c 20 30 2c 0a 20 20 20 31 2c 20   1, 1, 0,.   1, 
368ec 31 2c 20 31 2c 20 32 2c 20 31 0a 7d 3b 0a 0a 2f  1, 1, 2, 1.};../
368ed 2a 0a 2a 2a 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  *.** isConsonant
368ee 28 29 20 61 6e 64 20 69 73 56 6f 77 65 6c 28 29  () and isVowel()
368ef 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68   determine if th
368f0 65 69 72 20 66 69 72 73 74 20 63 68 61 72 61 63  eir first charac
368f1 74 65 72 20 69 6e 0a 2a 2a 20 74 68 65 20 73 74  ter in.** the st
368f2 72 69 6e 67 20 74 68 65 79 20 70 6f 69 6e 74 20  ring they point 
368f3 74 6f 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e  to is a consonan
368f4 74 20 6f 72 20 61 20 76 6f 77 65 6c 2c 20 61 63  t or a vowel, ac
368f5 63 6f 72 64 69 6e 67 0a 2a 2a 20 74 6f 20 50 6f  cording.** to Po
368f6 72 74 65 72 20 72 75 6c 73 2e 20 20 0a 2a 2a 0a  rter ruls.  .**.
368f7 2a 2a 20 41 20 63 6f 6e 73 6f 6e 61 74 65 20 69  ** A consonate i
368f8 73 20 61 6e 79 20 6c 65 74 74 65 72 20 6f 74 68  s any letter oth
368f9 65 72 20 74 68 61 6e 20 27 61 27 2c 20 27 65 27  er than 'a', 'e'
368fa 2c 20 27 69 27 2c 20 27 6f 27 2c 20 6f 72 20 27  , 'i', 'o', or '
368fb 75 27 2e 0a 2a 2a 20 27 59 27 20 69 73 20 61 20  u'..** 'Y' is a 
368fc 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73  consonant unless
368fd 20 69 74 20 66 6f 6c 6c 6f 77 73 20 61 6e 6f 74   it follows anot
368fe 68 65 72 20 63 6f 6e 73 6f 6e 61 6e 74 2c 0a 2a  her consonant,.*
368ff 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  * in which case 
36900 69 74 20 69 73 20 61 20 76 6f 77 65 6c 2e 0a 2a  it is a vowel..*
36901 2a 0a 2a 2a 20 49 6e 20 74 68 65 73 65 20 72 6f  *.** In these ro
36902 75 74 69 6e 65 2c 20 74 68 65 20 6c 65 74 74 65  utine, the lette
36903 72 73 20 61 72 65 20 69 6e 20 72 65 76 65 72 73  rs are in revers
36904 65 20 6f 72 64 65 72 2e 20 20 53 6f 20 74 68 65  e order.  So the
36905 20 27 79 27 20 72 75 6c 65 0a 2a 2a 20 69 73 20   'y' rule.** is 
36906 74 68 61 74 20 27 79 27 20 69 73 20 61 20 63 6f  that 'y' is a co
36907 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73 20 69  nsonant unless i
36908 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79  t is followed by
36909 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 73   another.** cons
3690a 6f 6e 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  onent..*/.static
3690b 20 69 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e   int isVowel(con
3690c 73 74 20 63 68 61 72 2a 29 3b 0a 73 74 61 74 69  st char*);.stati
3690d 63 20 69 6e 74 20 69 73 43 6f 6e 73 6f 6e 61 6e  c int isConsonan
3690e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  t(const char *z)
3690f 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68 61  {.  int j;.  cha
36910 72 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28 20  r x = *z;.  if( 
36911 78 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  x==0 ) return 0;
36912 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27 61  .  assert( x>='a
36913 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a 20  ' && x<='z' );. 
36914 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61 27   j = cType[x-'a'
36915 5d 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20 72  ];.  if( j<2 ) r
36916 65 74 75 72 6e 20 6a 3b 0a 20 20 72 65 74 75 72  eturn j;.  retur
36917 6e 20 7a 5b 31 5d 3d 3d 30 20 7c 7c 20 69 73 56  n z[1]==0 || isV
36918 6f 77 65 6c 28 7a 20 2b 20 31 29 3b 0a 7d 0a 73  owel(z + 1);.}.s
36919 74 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77 65  tatic int isVowe
3691a 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  l(const char *z)
3691b 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68 61  {.  int j;.  cha
3691c 72 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28 20  r x = *z;.  if( 
3691d 78 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  x==0 ) return 0;
3691e 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27 61  .  assert( x>='a
3691f 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a 20  ' && x<='z' );. 
36920 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61 27   j = cType[x-'a'
36921 5d 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20 72  ];.  if( j<2 ) r
36922 65 74 75 72 6e 20 31 2d 6a 3b 0a 20 20 72 65 74  eturn 1-j;.  ret
36923 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  urn isConsonant(
36924 7a 20 2b 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  z + 1);.}../*.**
36925 20 4c 65 74 20 61 6e 79 20 73 65 71 75 65 6e 63   Let any sequenc
36926 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  e of one or more
36927 20 76 6f 77 65 6c 73 20 62 65 20 72 65 70 72 65   vowels be repre
36928 73 65 6e 74 65 64 20 62 79 20 56 20 61 6e 64 20  sented by V and 
36929 6c 65 74 0a 2a 2a 20 43 20 62 65 20 73 65 71 75  let.** C be sequ
3692a 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d  ence of one or m
3692b 6f 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 73 2e 20  ore consonants. 
3692c 20 54 68 65 6e 20 65 76 65 72 79 20 77 6f 72 64   Then every word
3692d 20 63 61 6e 20 62 65 0a 2a 2a 20 72 65 70 72 65   can be.** repre
3692e 73 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a 2a  sented as:.**.**
3692f 20 20 20 20 20 20 20 20 20 20 20 5b 43 5d 20 28             [C] (
36930 56 43 29 7b 6d 7d 20 5b 56 5d 0a 2a 2a 0a 2a 2a  VC){m} [V].**.**
36931 20 49 6e 20 70 72 6f 73 65 3a 20 20 41 20 77 6f   In prose:  A wo
36932 72 64 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61  rd is an optiona
36933 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c  l consonant foll
36934 6f 77 65 64 20 62 79 20 7a 65 72 6f 20 6f 72 0a  owed by zero or.
36935 2a 2a 20 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 61  ** vowel-consona
36936 6e 74 20 70 61 69 72 73 20 66 6f 6c 6c 6f 77 65  nt pairs followe
36937 64 20 62 79 20 61 6e 20 6f 70 74 69 6f 6e 61 6c  d by an optional
36938 20 76 6f 77 65 6c 2e 20 20 22 6d 22 20 69 73 20   vowel.  "m" is 
36939 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  the.** number of
3693a 20 76 6f 77 65 6c 20 63 6f 6e 73 6f 6e 61 6e 74   vowel consonant
3693b 20 70 61 69 72 73 2e 20 20 54 68 69 73 20 72 6f   pairs.  This ro
3693c 75 74 69 6e 65 20 63 6f 6d 70 75 74 65 73 20 74  utine computes t
3693d 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 6d  he value.** of m
3693e 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 69   for the first i
3693f 20 62 79 74 65 73 20 6f 66 20 61 20 77 6f 72 64   bytes of a word
36940 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ..**.** Return t
36941 72 75 65 20 69 66 20 74 68 65 20 6d 2d 76 61 6c  rue if the m-val
36942 75 65 20 66 6f 72 20 7a 20 69 73 20 31 20 6f 72  ue for z is 1 or
36943 20 6d 6f 72 65 2e 20 20 49 6e 20 6f 74 68 65 72   more.  In other
36944 20 77 6f 72 64 73 2c 0a 2a 2a 20 72 65 74 75 72   words,.** retur
36945 6e 20 74 72 75 65 20 69 66 20 7a 20 63 6f 6e 74  n true if z cont
36946 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f 6e  ains at least on
36947 65 20 76 6f 77 65 6c 20 74 68 61 74 20 69 73 20  e vowel that is 
36948 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 61  followed.** by a
36949 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a   consonant..**.*
3694a 2a 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  * In this routin
3694b 65 20 7a 5b 5d 20 69 73 20 69 6e 20 72 65 76 65  e z[] is in reve
3694c 72 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20 77  rse order.  So w
3694d 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f 6f  e are really loo
3694e 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 6e 20 69  king.** for an i
3694f 6e 73 74 61 6e 63 65 20 6f 66 20 6f 66 20 61 20  nstance of of a 
36950 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f 77  consonant follow
36951 65 64 20 62 79 20 61 20 76 6f 77 65 6c 2e 0a 2a  ed by a vowel..*
36952 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 67  /.static int m_g
36953 74 5f 30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  t_0(const char *
36954 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56  z){.  while( isV
36955 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  owel(z) ){ z++; 
36956 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20  }.  if( *z==0 ) 
36957 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c  return 0;.  whil
36958 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  e( isConsonant(z
36959 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65  ) ){ z++; }.  re
3695a 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f  turn *z!=0;.}../
3695b 2a 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f 76  * Like mgt0 abov
3695c 65 20 65 78 63 65 70 74 20 77 65 20 61 72 65 20  e except we are 
3695d 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76 61  looking for a va
3695e 6c 75 65 20 6f 66 20 6d 20 77 68 69 63 68 20 69  lue of m which i
3695f 73 0a 2a 2a 20 65 78 61 63 74 6c 79 20 31 0a 2a  s.** exactly 1.*
36960 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 65  /.static int m_e
36961 71 5f 31 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  q_1(const char *
36962 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56  z){.  while( isV
36963 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  owel(z) ){ z++; 
36964 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20  }.  if( *z==0 ) 
36965 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c  return 0;.  whil
36966 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  e( isConsonant(z
36967 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66  ) ){ z++; }.  if
36968 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( *z==0 ) return
36969 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 56   0;.  while( isV
3696a 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  owel(z) ){ z++; 
3696b 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20  }.  if( *z==0 ) 
3696c 72 65 74 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c  return 1;.  whil
3696d 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  e( isConsonant(z
3696e 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65  ) ){ z++; }.  re
3696f 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f  turn *z==0;.}../
36970 2a 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f 76  * Like mgt0 abov
36971 65 20 65 78 63 65 70 74 20 77 65 20 61 72 65 20  e except we are 
36972 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76 61  looking for a va
36973 6c 75 65 20 6f 66 20 6d 3e 31 20 69 6e 73 74 65  lue of m>1 inste
36974 61 64 0a 2a 2a 20 6f 72 20 6d 3e 30 0a 2a 2f 0a  ad.** or m>0.*/.
36975 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 67 74 5f  static int m_gt_
36976 31 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  1(const char *z)
36977 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f 77  {.  while( isVow
36978 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  el(z) ){ z++; }.
36979 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65    if( *z==0 ) re
3697a 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28  turn 0;.  while(
3697b 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20   isConsonant(z) 
3697c 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  ){ z++; }.  if( 
3697d 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  *z==0 ) return 0
3697e 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f 77  ;.  while( isVow
3697f 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  el(z) ){ z++; }.
36980 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65    if( *z==0 ) re
36981 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28  turn 0;.  while(
36982 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20   isConsonant(z) 
36983 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75  ){ z++; }.  retu
36984 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  rn *z!=0;.}../*.
36985 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
36986 66 20 74 68 65 72 65 20 69 73 20 61 20 76 6f 77  f there is a vow
36987 65 6c 20 61 6e 79 77 68 65 72 65 20 77 69 74 68  el anywhere with
36988 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 0a 2a 2f 0a  in z[0..n-1].*/.
36989 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 56 6f  static int hasVo
3698a 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  wel(const char *
3698b 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 43  z){.  while( isC
3698c 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a  onsonant(z) ){ z
3698d 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a  ++; }.  return *
3698e 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  z!=0;.}../*.** R
3698f 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
36990 65 20 77 6f 72 64 20 65 6e 64 73 20 69 6e 20 61  e word ends in a
36991 20 64 6f 75 62 6c 65 20 63 6f 6e 73 6f 6e 61 6e   double consonan
36992 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 78  t..**.** The tex
36993 74 20 69 73 20 72 65 76 65 72 73 65 64 20 68 65  t is reversed he
36994 72 65 2e 20 53 6f 20 77 65 20 61 72 65 20 72 65  re. So we are re
36995 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 0a  ally looking at.
36996 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77 6f  ** the first two
36997 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 7a   characters of z
36998 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  []..*/.static in
36999 74 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e 61 6e  t doubleConsonan
3699a 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  t(const char *z)
3699b 7b 0a 20 20 72 65 74 75 72 6e 20 69 73 43 6f 6e  {.  return isCon
3699c 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20 7a 5b 30  sonant(z) && z[0
3699d 5d 3d 3d 7a 5b 31 5d 20 26 26 20 69 73 43 6f 6e  ]==z[1] && isCon
3699e 73 6f 6e 61 6e 74 28 7a 2b 31 29 3b 0a 7d 0a 0a  sonant(z+1);.}..
3699f 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
369a0 45 20 69 66 20 74 68 65 20 77 6f 72 64 20 65 6e  E if the word en
369a1 64 73 20 77 69 74 68 20 74 68 72 65 65 20 6c 65  ds with three le
369a2 74 74 65 72 73 20 77 68 69 63 68 0a 2a 2a 20 61  tters which.** a
369a3 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 2d 76 6f 77  re consonant-vow
369a4 65 6c 2d 63 6f 6e 73 6f 6e 65 6e 74 20 61 6e 64  el-consonent and
369a5 20 77 68 65 72 65 20 74 68 65 20 66 69 6e 61 6c   where the final
369a6 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2a 20 69 73   consonant.** is
369a7 20 6e 6f 74 20 27 77 27 2c 20 27 78 27 2c 20 6f   not 'w', 'x', o
369a8 72 20 27 79 27 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  r 'y'..**.** The
369a9 20 77 6f 72 64 20 69 73 20 72 65 76 65 72 73 65   word is reverse
369aa 64 20 68 65 72 65 2e 20 20 53 6f 20 77 65 20 61  d here.  So we a
369ab 72 65 20 72 65 61 6c 6c 79 20 63 68 65 63 6b 69  re really checki
369ac 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  ng the.** first 
369ad 74 68 72 65 65 20 6c 65 74 74 65 72 73 20 61 6e  three letters an
369ae 64 20 74 68 65 20 66 69 72 73 74 20 6f 6e 65 20  d the first one 
369af 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 5b 77 78  cannot be in [wx
369b0 79 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  y]..*/.static in
369b1 74 20 73 74 61 72 5f 6f 68 28 63 6f 6e 73 74 20  t star_oh(const 
369b2 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75  char *z){.  retu
369b3 72 6e 0a 20 20 20 20 7a 5b 30 5d 21 3d 30 20 26  rn.    z[0]!=0 &
369b4 26 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  & isConsonant(z)
369b5 20 26 26 0a 20 20 20 20 7a 5b 30 5d 21 3d 27 77   &&.    z[0]!='w
369b6 27 20 26 26 20 7a 5b 30 5d 21 3d 27 78 27 20 26  ' && z[0]!='x' &
369b7 26 20 7a 5b 30 5d 21 3d 27 79 27 20 26 26 0a 20  & z[0]!='y' &&. 
369b8 20 20 20 7a 5b 31 5d 21 3d 30 20 26 26 20 69 73     z[1]!=0 && is
369b9 56 6f 77 65 6c 28 7a 2b 31 29 20 26 26 0a 20 20  Vowel(z+1) &&.  
369ba 20 20 7a 5b 32 5d 21 3d 30 20 26 26 20 69 73 43    z[2]!=0 && isC
369bb 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 32 29 3b 0a 7d  onsonant(z+2);.}
369bc 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77  ../*.** If the w
369bd 6f 72 64 20 65 6e 64 73 20 77 69 74 68 20 7a 46  ord ends with zF
369be 72 6f 6d 20 61 6e 64 20 78 43 6f 6e 64 28 29 20  rom and xCond() 
369bf 69 73 20 74 72 75 65 20 66 6f 72 20 74 68 65 20  is true for the 
369c0 73 74 65 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 77  stem.** of the w
369c1 6f 72 64 20 74 68 61 74 20 70 72 65 63 65 65 64  ord that preceed
369c2 73 20 74 68 65 20 7a 46 72 6f 6d 20 65 6e 64 69  s the zFrom endi
369c3 6e 67 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20  ng, then change 
369c4 74 68 65 20 0a 2a 2a 20 65 6e 64 69 6e 67 20 74  the .** ending t
369c5 6f 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  o zTo..**.** The
369c6 20 69 6e 70 75 74 20 77 6f 72 64 20 2a 70 7a 20   input word *pz 
369c7 61 6e 64 20 7a 46 72 6f 6d 20 61 72 65 20 62 6f  and zFrom are bo
369c8 74 68 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72  th in reverse or
369c9 64 65 72 2e 20 20 7a 54 6f 0a 2a 2a 20 69 73 20  der.  zTo.** is 
369ca 69 6e 20 6e 6f 72 6d 61 6c 20 6f 72 64 65 72 2e  in normal order.
369cb 20 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54   .**.** Return T
369cc 52 55 45 20 69 66 20 7a 46 72 6f 6d 20 6d 61 74  RUE if zFrom mat
369cd 63 68 65 73 2e 20 20 52 65 74 75 72 6e 20 46 41  ches.  Return FA
369ce 4c 53 45 20 69 66 20 7a 46 72 6f 6d 20 64 6f 65  LSE if zFrom doe
369cf 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 2e 20  s not.** match. 
369d0 20 4e 6f 74 20 74 68 61 74 20 54 52 55 45 20 69   Not that TRUE i
369d1 73 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e 20  s returned even 
369d2 69 66 20 78 43 6f 6e 64 28 29 20 66 61 69 6c 73  if xCond() fails
369d3 20 61 6e 64 0a 2a 2a 20 6e 6f 20 73 75 62 73 74   and.** no subst
369d4 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 2e 0a  itution occurs..
369d5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74  */.static int st
369d6 65 6d 28 0a 20 20 63 68 61 72 20 2a 2a 70 7a 2c  em(.  char **pz,
369d7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
369d8 54 68 65 20 77 6f 72 64 20 62 65 69 6e 67 20 73  The word being s
369d9 74 65 6d 6d 65 64 20 28 52 65 76 65 72 73 65 64  temmed (Reversed
369da 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  ) */.  const cha
369db 72 20 2a 7a 46 72 6f 6d 2c 20 20 20 20 20 2f 2a  r *zFrom,     /*
369dc 20 49 66 20 74 68 65 20 65 6e 64 69 6e 67 20 6d   If the ending m
369dd 61 74 63 68 65 73 20 74 68 69 73 2e 2e 2e 20 28  atches this... (
369de 52 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20 63  Reversed) */.  c
369df 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 2c 20  onst char *zTo, 
369e0 20 20 20 20 20 20 2f 2a 20 2e 2e 2e 20 63 68 61        /* ... cha
369e1 6e 67 65 20 74 68 65 20 65 6e 64 69 6e 67 20 74  nge the ending t
369e2 6f 20 74 68 69 73 20 28 6e 6f 74 20 72 65 76 65  o this (not reve
369e3 72 73 65 64 29 20 2a 2f 0a 20 20 69 6e 74 20 28  rsed) */.  int (
369e4 2a 78 43 6f 6e 64 29 28 63 6f 6e 73 74 20 63 68  *xCond)(const ch
369e5 61 72 2a 29 20 20 20 2f 2a 20 43 6f 6e 64 69 74  ar*)   /* Condit
369e6 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62 65  ion that must be
369e7 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 68   true */.){.  ch
369e8 61 72 20 2a 7a 20 3d 20 2a 70 7a 3b 0a 20 20 77  ar *z = *pz;.  w
369e9 68 69 6c 65 28 20 2a 7a 46 72 6f 6d 20 26 26 20  hile( *zFrom && 
369ea 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20 29 7b 20 7a 2b  *zFrom==*z ){ z+
369eb 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b 20 7d 0a 20 20  +; zFrom++; }.  
369ec 69 66 28 20 2a 7a 46 72 6f 6d 21 3d 30 20 29 20  if( *zFrom!=0 ) 
369ed 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
369ee 78 43 6f 6e 64 20 26 26 20 21 78 43 6f 6e 64 28  xCond && !xCond(
369ef 7a 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  z) ) return 1;. 
369f0 20 77 68 69 6c 65 28 20 2a 7a 54 6f 20 29 7b 0a   while( *zTo ){.
369f1 20 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 2a 28 7a      *(--z) = *(z
369f2 54 6f 2b 2b 29 3b 0a 20 20 7d 0a 20 20 2a 70 7a  To++);.  }.  *pz
369f3 20 3d 20 7a 3b 0a 20 20 72 65 74 75 72 6e 20 31   = z;.  return 1
369f4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
369f5 69 73 20 74 68 65 20 66 61 6c 6c 62 61 63 6b 20  is the fallback 
369f6 73 74 65 6d 6d 65 72 20 75 73 65 64 20 77 68 65  stemmer used whe
369f7 6e 20 74 68 65 20 70 6f 72 74 65 72 20 73 74 65  n the porter ste
369f8 6d 6d 65 72 20 69 73 0a 2a 2a 20 69 6e 61 70 70  mmer is.** inapp
369f9 72 6f 70 72 69 61 74 65 2e 20 20 54 68 65 20 69  ropriate.  The i
369fa 6e 70 75 74 20 77 6f 72 64 20 69 73 20 63 6f 70  nput word is cop
369fb 69 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 75 74  ied into the out
369fc 70 75 74 20 77 69 74 68 0a 2a 2a 20 55 53 2d 41  put with.** US-A
369fd 53 43 49 49 20 63 61 73 65 20 66 6f 6c 64 69 6e  SCII case foldin
369fe 67 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74  g.  If the input
369ff 20 77 6f 72 64 20 69 73 20 74 6f 6f 20 6c 6f 6e   word is too lon
36a00 67 20 28 6d 6f 72 65 0a 2a 2a 20 74 68 61 6e 20  g (more.** than 
36a01 32 30 20 62 79 74 65 73 20 69 66 20 69 74 20 63  20 bytes if it c
36a02 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 69 67 69 74  ontains no digit
36a03 73 20 6f 72 20 6d 6f 72 65 20 74 68 61 6e 20 36  s or more than 6
36a04 20 62 79 74 65 73 20 69 66 0a 2a 2a 20 69 74 20   bytes if.** it 
36a05 63 6f 6e 74 61 69 6e 73 20 64 69 67 69 74 73 29  contains digits)
36a06 20 74 68 65 6e 20 77 6f 72 64 20 69 73 20 74 72   then word is tr
36a07 75 6e 63 61 74 65 64 20 74 6f 20 32 30 20 6f 72  uncated to 20 or
36a08 20 36 20 62 79 74 65 73 0a 2a 2a 20 62 79 20 74   6 bytes.** by t
36a09 61 6b 69 6e 67 20 31 30 20 6f 72 20 33 20 62 79  aking 10 or 3 by
36a0a 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67  tes from the beg
36a0b 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 2e 0a  inning and end..
36a0c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
36a0d 6f 70 79 5f 73 74 65 6d 6d 65 72 28 63 6f 6e 73  opy_stemmer(cons
36a0e 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74  t char *zIn, int
36a0f 20 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f 75 74   nIn, char *zOut
36a10 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20  , int *pnOut){. 
36a11 20 69 6e 74 20 69 2c 20 6d 78 2c 20 6a 3b 0a 20   int i, mx, j;. 
36a12 20 69 6e 74 20 68 61 73 44 69 67 69 74 20 3d 20   int hasDigit = 
36a13 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
36a14 6e 49 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nIn; i++){.    i
36a15 6e 74 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20  nt c = zIn[i];. 
36a16 20 20 20 69 66 28 20 63 3e 3d 27 41 27 20 26 26     if( c>='A' &&
36a17 20 63 3c 3d 27 5a 27 20 29 7b 0a 20 20 20 20 20   c<='Z' ){.     
36a18 20 7a 4f 75 74 5b 69 5d 20 3d 20 63 20 2d 20 27   zOut[i] = c - '
36a19 41 27 20 2b 20 27 61 27 3b 0a 20 20 20 20 7d 65  A' + 'a';.    }e
36a1a 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 63  lse{.      if( c
36a1b 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20  >='0' && c<='9' 
36a1c 29 20 68 61 73 44 69 67 69 74 20 3d 20 31 3b 0a  ) hasDigit = 1;.
36a1d 20 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20        zOut[i] = 
36a1e 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d  c;.    }.  }.  m
36a1f 78 20 3d 20 68 61 73 44 69 67 69 74 20 3f 20 33  x = hasDigit ? 3
36a20 20 3a 20 31 30 3b 0a 20 20 69 66 28 20 6e 49 6e   : 10;.  if( nIn
36a21 3e 6d 78 2a 32 20 29 7b 0a 20 20 20 20 66 6f 72  >mx*2 ){.    for
36a22 28 6a 3d 6d 78 2c 20 69 3d 6e 49 6e 2d 6d 78 3b  (j=mx, i=nIn-mx;
36a23 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2b 2b   i<nIn; i++, j++
36a24 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 5d  ){.      zOut[j]
36a25 20 3d 20 7a 4f 75 74 5b 69 5d 3b 0a 20 20 20 20   = zOut[i];.    
36a26 7d 0a 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 7d  }.    i = j;.  }
36a27 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a  .  zOut[i] = 0;.
36a28 20 20 2a 70 6e 4f 75 74 20 3d 20 69 3b 0a 7d 0a    *pnOut = i;.}.
36a29 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 6d 20 74 68 65  ../*.** Stem the
36a2a 20 69 6e 70 75 74 20 77 6f 72 64 20 7a 49 6e 5b   input word zIn[
36a2b 30 2e 2e 6e 49 6e 2d 31 5d 2e 20 20 53 74 6f 72  0..nIn-1].  Stor
36a2c 65 20 74 68 65 20 6f 75 74 70 75 74 20 69 6e 20  e the output in 
36a2d 7a 4f 75 74 2e 0a 2a 2a 20 7a 4f 75 74 20 69 73  zOut..** zOut is
36a2e 20 61 74 20 6c 65 61 73 74 20 62 69 67 20 65 6e   at least big en
36a2f 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6e 49 6e  ough to hold nIn
36a30 20 62 79 74 65 73 2e 20 20 57 72 69 74 65 20 74   bytes.  Write t
36a31 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 73 69 7a  he actual.** siz
36a32 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20  e of the output 
36a33 77 6f 72 64 20 28 65 78 63 6c 75 73 69 76 65 20  word (exclusive 
36a34 6f 66 20 74 68 65 20 27 5c 30 27 20 74 65 72 6d  of the '\0' term
36a35 69 6e 61 74 6f 72 29 20 69 6e 74 6f 20 2a 70 6e  inator) into *pn
36a36 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 75  Out..**.** Any u
36a37 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 63  pper-case charac
36a38 74 65 72 73 20 69 6e 20 74 68 65 20 55 53 2d 41  ters in the US-A
36a39 53 43 49 49 20 63 68 61 72 61 63 74 65 72 20 73  SCII character s
36a3a 65 74 20 28 5b 41 2d 5a 5d 29 0a 2a 2a 20 61 72  et ([A-Z]).** ar
36a3b 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 6c  e converted to l
36a3c 6f 77 65 72 20 63 61 73 65 2e 20 20 55 70 70 65  ower case.  Uppe
36a3d 72 2d 63 61 73 65 20 55 54 46 20 63 68 61 72 61  r-case UTF chara
36a3e 63 74 65 72 73 20 61 72 65 0a 2a 2a 20 75 6e 63  cters are.** unc
36a3f 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 6f  hanged..**.** Wo
36a40 72 64 73 20 74 68 61 74 20 61 72 65 20 6c 6f 6e  rds that are lon
36a41 67 65 72 20 74 68 61 6e 20 61 62 6f 75 74 20 32  ger than about 2
36a42 30 20 62 79 74 65 73 20 61 72 65 20 73 74 65 6d  0 bytes are stem
36a43 6d 65 64 20 62 79 20 72 65 74 61 69 6e 69 6e 67  med by retaining
36a44 0a 2a 2a 20 61 20 66 65 77 20 62 79 74 65 73 20  .** a few bytes 
36a45 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
36a46 6e 67 20 61 6e 64 20 74 68 65 20 65 6e 64 20 6f  ng and the end o
36a47 66 20 74 68 65 20 77 6f 72 64 2e 20 20 49 66 20  f the word.  If 
36a48 74 68 65 0a 2a 2a 20 77 6f 72 64 20 63 6f 6e 74  the.** word cont
36a49 61 69 6e 73 20 64 69 67 69 74 73 2c 20 33 20 62  ains digits, 3 b
36a4a 79 74 65 73 20 61 72 65 20 74 61 6b 65 6e 20 66  ytes are taken f
36a4b 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
36a4c 67 20 61 6e 64 0a 2a 2a 20 33 20 62 79 74 65 73  g and.** 3 bytes
36a4d 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 2e 20 20   from the end.  
36a4e 46 6f 72 20 6c 6f 6e 67 20 77 6f 72 64 73 20 77  For long words w
36a4f 69 74 68 6f 75 74 20 64 69 67 69 74 73 2c 20 31  ithout digits, 1
36a50 30 20 62 79 74 65 73 0a 2a 2a 20 61 72 65 20 74  0 bytes.** are t
36a51 61 6b 65 6e 20 66 72 6f 6d 20 65 61 63 68 20 65  aken from each e
36a52 6e 64 2e 20 20 55 53 2d 41 53 43 49 49 20 63 61  nd.  US-ASCII ca
36a53 73 65 20 66 6f 6c 64 69 6e 67 20 73 74 69 6c 6c  se folding still
36a54 20 61 70 70 6c 69 65 73 2e 0a 2a 2a 20 0a 2a 2a   applies..** .**
36a55 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 77 6f   If the input wo
36a56 72 64 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 20  rd contains not 
36a57 64 69 67 69 74 73 20 62 75 74 20 64 6f 65 73 20  digits but does 
36a58 63 68 61 72 61 63 74 65 72 73 20 6e 6f 74 20 0a  characters not .
36a59 2a 2a 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 74  ** in [a-zA-Z] t
36a5a 68 65 6e 20 6e 6f 20 73 74 65 6d 6d 69 6e 67 20  hen no stemming 
36a5b 69 73 20 61 74 74 65 6d 70 74 65 64 20 61 6e 64  is attempted and
36a5c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75   this routine ju
36a5d 73 74 20 0a 2a 2a 20 63 6f 70 69 65 73 20 74 68  st .** copies th
36a5e 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68 65  e input into the
36a5f 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68 65 20   input into the 
36a60 6f 75 74 70 75 74 20 77 69 74 68 20 55 53 2d 41  output with US-A
36a61 53 43 49 49 0a 2a 2a 20 63 61 73 65 20 66 6f 6c  SCII.** case fol
36a62 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 74 65 6d  ding..**.** Stem
36a63 6d 69 6e 67 20 6e 65 76 65 72 20 69 6e 63 72 65  ming never incre
36a64 61 73 65 73 20 74 68 65 20 6c 65 6e 67 74 68 20  ases the length 
36a65 6f 66 20 74 68 65 20 77 6f 72 64 2e 20 20 53 6f  of the word.  So
36a66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20   there is.** no 
36a67 63 68 61 6e 63 65 20 6f 66 20 6f 76 65 72 66 6c  chance of overfl
36a68 6f 77 69 6e 67 20 74 68 65 20 7a 4f 75 74 20 62  owing the zOut b
36a69 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  uffer..*/.static
36a6a 20 76 6f 69 64 20 70 6f 72 74 65 72 5f 73 74 65   void porter_ste
36a6b 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20  mmer(const char 
36a6c 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20 63  *zIn, int nIn, c
36a6d 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a  har *zOut, int *
36a6e 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20 69 2c  pnOut){.  int i,
36a6f 20 6a 2c 20 63 3b 0a 20 20 63 68 61 72 20 7a 52   j, c;.  char zR
36a70 65 76 65 72 73 65 5b 32 38 5d 3b 0a 20 20 63 68  everse[28];.  ch
36a71 61 72 20 2a 7a 2c 20 2a 7a 32 3b 0a 20 20 69 66  ar *z, *z2;.  if
36a72 28 20 6e 49 6e 3c 33 20 7c 7c 20 6e 49 6e 3e 3d  ( nIn<3 || nIn>=
36a73 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73 65 29  sizeof(zReverse)
36a74 2d 37 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  -7 ){.    /* The
36a75 20 77 6f 72 64 20 69 73 20 74 6f 6f 20 62 69 67   word is too big
36a76 20 6f 72 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f   or too small fo
36a77 72 20 74 68 65 20 70 6f 72 74 65 72 20 73 74 65  r the porter ste
36a78 6d 6d 65 72 2e 0a 20 20 20 20 2a 2a 20 46 61 6c  mmer..    ** Fal
36a79 6c 62 61 63 6b 20 74 6f 20 74 68 65 20 63 6f 70  lback to the cop
36a7a 79 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20 20  y stemmer */.   
36a7b 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a 49   copy_stemmer(zI
36a7c 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e  n, nIn, zOut, pn
36a7d 4f 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Out);.    return
36a7e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 2c  ;.  }.  for(i=0,
36a7f 20 6a 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65 72   j=sizeof(zRever
36a80 73 65 29 2d 36 3b 20 69 3c 6e 49 6e 3b 20 69 2b  se)-6; i<nIn; i+
36a81 2b 2c 20 6a 2d 2d 29 7b 0a 20 20 20 20 63 20 3d  +, j--){.    c =
36a82 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66 28   zIn[i];.    if(
36a83 20 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27 5a   c>='A' && c<='Z
36a84 27 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 76 65  ' ){.      zReve
36a85 72 73 65 5b 6a 5d 20 3d 20 63 20 2b 20 27 61 27  rse[j] = c + 'a'
36a86 20 2d 20 27 41 27 3b 0a 20 20 20 20 7d 65 6c 73   - 'A';.    }els
36a87 65 20 69 66 28 20 63 3e 3d 27 61 27 20 26 26 20  e if( c>='a' && 
36a88 63 3c 3d 27 7a 27 20 29 7b 0a 20 20 20 20 20 20  c<='z' ){.      
36a89 7a 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63 3b  zReverse[j] = c;
36a8a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
36a8b 20 20 2f 2a 20 54 68 65 20 75 73 65 20 6f 66 20    /* The use of 
36a8c 61 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20  a character not 
36a8d 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 6d 65 61 6e  in [a-zA-Z] mean
36a8e 73 20 74 68 61 74 20 77 65 20 66 61 6c 6c 62 61  s that we fallba
36a8f 63 6b 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  ck.      ** to t
36a90 68 65 20 63 6f 70 79 20 73 74 65 6d 6d 65 72 20  he copy stemmer 
36a91 2a 2f 0a 20 20 20 20 20 20 63 6f 70 79 5f 73 74  */.      copy_st
36a92 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20  emmer(zIn, nIn, 
36a93 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20 20  zOut, pnOut);.  
36a94 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
36a95 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26  }.  }.  memset(&
36a96 7a 52 65 76 65 72 73 65 5b 73 69 7a 65 6f 66 28  zReverse[sizeof(
36a97 7a 52 65 76 65 72 73 65 29 2d 35 5d 2c 20 30 2c  zReverse)-5], 0,
36a98 20 35 29 3b 0a 20 20 7a 20 3d 20 26 7a 52 65 76   5);.  z = &zRev
36a99 65 72 73 65 5b 6a 2b 31 5d 3b 0a 0a 0a 20 20 2f  erse[j+1];...  /
36a9a 2a 20 53 74 65 70 20 31 61 20 2a 2f 0a 20 20 69  * Step 1a */.  i
36a9b 66 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 29 7b 0a  f( z[0]=='s' ){.
36a9c 20 20 20 20 69 66 28 0a 20 20 20 20 20 21 73 74      if(.     !st
36a9d 65 6d 28 26 7a 2c 20 22 73 65 73 73 22 2c 20 22  em(&z, "sess", "
36a9e 73 73 22 2c 20 30 29 20 26 26 0a 20 20 20 20 20  ss", 0) &&.     
36a9f 21 73 74 65 6d 28 26 7a 2c 20 22 73 65 69 22 2c  !stem(&z, "sei",
36aa0 20 22 69 22 2c 20 30 29 20 20 26 26 0a 20 20 20   "i", 0)  &&.   
36aa1 20 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 73 22    !stem(&z, "ss"
36aa2 2c 20 22 73 73 22 2c 20 30 29 0a 20 20 20 20 29  , "ss", 0).    )
36aa3 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  {.      z++;.   
36aa4 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65   }.  }..  /* Ste
36aa5 70 20 31 62 20 2a 2f 20 20 0a 20 20 7a 32 20 3d  p 1b */  .  z2 =
36aa6 20 7a 3b 0a 20 20 69 66 28 20 73 74 65 6d 28 26   z;.  if( stem(&
36aa7 7a 2c 20 22 64 65 65 22 2c 20 22 65 65 22 2c 20  z, "dee", "ee", 
36aa8 6d 5f 67 74 5f 30 29 20 29 7b 0a 20 20 20 20 2f  m_gt_0) ){.    /
36aa9 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54  * Do nothing.  T
36aaa 68 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20  he work was all 
36aab 69 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20  in the test */. 
36aac 20 7d 65 6c 73 65 20 69 66 28 20 0a 20 20 20 20   }else if( .    
36aad 20 28 73 74 65 6d 28 26 7a 2c 20 22 67 6e 69 22   (stem(&z, "gni"
36aae 2c 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29 20  , "", hasVowel) 
36aaf 7c 7c 20 73 74 65 6d 28 26 7a 2c 20 22 64 65 22  || stem(&z, "de"
36ab0 2c 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29 29  , "", hasVowel))
36ab1 0a 20 20 20 20 20 20 26 26 20 7a 21 3d 7a 32 0a  .      && z!=z2.
36ab2 20 20 29 7b 0a 20 20 20 20 20 69 66 28 20 73 74    ){.     if( st
36ab3 65 6d 28 26 7a 2c 20 22 74 61 22 2c 20 22 61 74  em(&z, "ta", "at
36ab4 65 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20  e", 0) ||.      
36ab5 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 62 22     stem(&z, "lb"
36ab6 2c 20 22 62 6c 65 22 2c 20 30 29 20 7c 7c 0a 20  , "ble", 0) ||. 
36ab7 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c          stem(&z,
36ab8 20 22 7a 69 22 2c 20 22 69 7a 65 22 2c 20 30 29   "zi", "ize", 0)
36ab9 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a 20 44 6f   ){.       /* Do
36aba 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20 77   nothing.  The w
36abb 6f 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20 74  ork was all in t
36abc 68 65 20 74 65 73 74 20 2a 2f 0a 20 20 20 20 20  he test */.     
36abd 7d 65 6c 73 65 20 69 66 28 20 64 6f 75 62 6c 65  }else if( double
36abe 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20  Consonant(z) && 
36abf 28 2a 7a 21 3d 27 6c 27 20 26 26 20 2a 7a 21 3d  (*z!='l' && *z!=
36ac0 27 73 27 20 26 26 20 2a 7a 21 3d 27 7a 27 29 20  's' && *z!='z') 
36ac1 29 7b 0a 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20  ){.       z++;. 
36ac2 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f      }else if( m_
36ac3 65 71 5f 31 28 7a 29 20 26 26 20 73 74 61 72 5f  eq_1(z) && star_
36ac4 6f 68 28 7a 29 20 29 7b 0a 20 20 20 20 20 20 20  oh(z) ){.       
36ac5 2a 28 2d 2d 7a 29 20 3d 20 27 65 27 3b 0a 20 20  *(--z) = 'e';.  
36ac6 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53     }.  }..  /* S
36ac7 74 65 70 20 31 63 20 2a 2f 0a 20 20 69 66 28 20  tep 1c */.  if( 
36ac8 7a 5b 30 5d 3d 3d 27 79 27 20 26 26 20 68 61 73  z[0]=='y' && has
36ac9 56 6f 77 65 6c 28 7a 2b 31 29 20 29 7b 0a 20 20  Vowel(z+1) ){.  
36aca 20 20 7a 5b 30 5d 20 3d 20 27 69 27 3b 0a 20 20    z[0] = 'i';.  
36acb 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 32 20 2a  }..  /* Step 2 *
36acc 2f 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31 5d  /.  switch( z[1]
36acd 20 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27 3a   ){.   case 'a':
36ace 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
36acf 6c 61 6e 6f 69 74 61 22 2c 20 22 61 74 65 22 2c  lanoita", "ate",
36ad0 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
36ad1 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69   stem(&z, "lanoi
36ad2 74 22 2c 20 22 74 69 6f 6e 22 2c 20 6d 5f 67 74  t", "tion", m_gt
36ad3 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
36ad4 0a 20 20 20 63 61 73 65 20 27 63 27 3a 0a 20 20  .   case 'c':.  
36ad5 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e     stem(&z, "icn
36ad6 65 22 2c 20 22 65 6e 63 65 22 2c 20 6d 5f 67 74  e", "ence", m_gt
36ad7 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
36ad8 28 26 7a 2c 20 22 69 63 6e 61 22 2c 20 22 61 6e  (&z, "icna", "an
36ad9 63 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20  ce", m_gt_0);.  
36ada 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
36adb 65 20 27 65 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 'e':.     stem
36adc 28 26 7a 2c 20 22 72 65 7a 69 22 2c 20 22 69 7a  (&z, "rezi", "iz
36add 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  e", m_gt_0);.   
36ade 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
36adf 20 27 67 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   'g':.     stem(
36ae0 26 7a 2c 20 22 69 67 6f 6c 22 2c 20 22 6c 6f 67  &z, "igol", "log
36ae1 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
36ae2 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
36ae3 27 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'l':.     stem(&
36ae4 7a 2c 20 22 69 6c 62 22 2c 20 22 62 6c 65 22 2c  z, "ilb", "ble",
36ae5 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
36ae6 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 6c 61 22   stem(&z, "illa"
36ae7 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20  , "al", m_gt_0) 
36ae8 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
36ae9 20 22 69 6c 74 6e 65 22 2c 20 22 65 6e 74 22 2c   "iltne", "ent",
36aea 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
36aeb 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 65 22 2c   stem(&z, "ile",
36aec 20 22 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c   "e", m_gt_0) ||
36aed 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
36aee 69 6c 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d  ilsuo", "ous", m
36aef 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
36af0 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27 3a  ak;.   case 'o':
36af1 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
36af2 6e 6f 69 74 61 7a 69 22 2c 20 22 69 7a 65 22 2c  noitazi", "ize",
36af3 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
36af4 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61   stem(&z, "noita
36af5 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30  ", "ate", m_gt_0
36af6 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
36af7 7a 2c 20 22 72 6f 74 61 22 2c 20 22 61 74 65 22  z, "rota", "ate"
36af8 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
36af9 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
36afa 73 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  s':.     stem(&z
36afb 2c 20 22 6d 73 69 6c 61 22 2c 20 22 61 6c 22 2c  , "msila", "al",
36afc 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
36afd 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 65   stem(&z, "ssene
36afe 76 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74  vi", "ive", m_gt
36aff 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
36b00 28 26 7a 2c 20 22 73 73 65 6e 6c 75 66 22 2c 20  (&z, "ssenluf", 
36b01 22 66 75 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c  "ful", m_gt_0) |
36b02 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
36b03 22 73 73 65 6e 73 75 6f 22 2c 20 22 6f 75 73 22  "ssensuo", "ous"
36b04 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
36b05 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
36b06 74 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  t':.     stem(&z
36b07 2c 20 22 69 74 69 6c 61 22 2c 20 22 61 6c 22 2c  , "itila", "al",
36b08 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
36b09 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 76 69   stem(&z, "itivi
36b0a 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30  ", "ive", m_gt_0
36b0b 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
36b0c 7a 2c 20 22 69 74 69 6c 69 62 22 2c 20 22 62 6c  z, "itilib", "bl
36b0d 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  e", m_gt_0);.   
36b0e 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20    break;.  }..  
36b0f 2f 2a 20 53 74 65 70 20 33 20 2a 2f 0a 20 20 73  /* Step 3 */.  s
36b10 77 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 20  witch( z[0] ){. 
36b11 20 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20    case 'e':.    
36b12 20 73 74 65 6d 28 26 7a 2c 20 22 65 74 61 63 69   stem(&z, "etaci
36b13 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29  ", "ic", m_gt_0)
36b14 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
36b15 2c 20 22 65 76 69 74 61 22 2c 20 22 22 2c 20 6d  , "evita", "", m
36b16 5f 67 74 5f 30 29 20 20 20 7c 7c 0a 20 20 20 20  _gt_0)   ||.    
36b17 20 73 74 65 6d 28 26 7a 2c 20 22 65 7a 69 6c 61   stem(&z, "ezila
36b18 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29  ", "al", m_gt_0)
36b19 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
36b1a 20 63 61 73 65 20 27 69 27 3a 0a 20 20 20 20 20   case 'i':.     
36b1b 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 63 69 22  stem(&z, "itici"
36b1c 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 3b  , "ic", m_gt_0);
36b1d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
36b1e 63 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 73  case 'l':.     s
36b1f 74 65 6d 28 26 7a 2c 20 22 6c 61 63 69 22 2c 20  tem(&z, "laci", 
36b20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c  "ic", m_gt_0) ||
36b21 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
36b22 6c 75 66 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30  luf", "", m_gt_0
36b23 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
36b24 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20 20    case 's':.    
36b25 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 22   stem(&z, "ssen"
36b26 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  , "", m_gt_0);. 
36b27 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
36b28 20 20 2f 2a 20 53 74 65 70 20 34 20 2a 2f 0a 20    /* Step 4 */. 
36b29 20 73 77 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b   switch( z[1] ){
36b2a 0a 20 20 20 63 61 73 65 20 27 61 27 3a 0a 20 20  .   case 'a':.  
36b2b 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6c 27     if( z[0]=='l'
36b2c 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20   && m_gt_1(z+2) 
36b2d 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32  ){.       z += 2
36b2e 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  ;.     }.     br
36b2f 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27  eak;.   case 'c'
36b30 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
36b31 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6e  ='e' && z[2]=='n
36b32 27 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20  ' && (z[3]=='a' 
36b33 7c 7c 20 7a 5b 33 5d 3d 3d 27 65 27 29 20 20 26  || z[3]=='e')  &
36b34 26 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 20 29  & m_gt_1(z+4)  )
36b35 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b  {.       z += 4;
36b36 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
36b37 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27 3a  ak;.   case 'e':
36b38 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d  .     if( z[0]==
36b39 27 72 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  'r' && m_gt_1(z+
36b3a 32 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  2) ){.       z +
36b3b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 2;.     }.    
36b3c 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
36b3d 27 69 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'i':.     if( z[
36b3e 30 5d 3d 3d 27 63 27 20 26 26 20 6d 5f 67 74 5f  0]=='c' && m_gt_
36b3f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  1(z+2) ){.      
36b40 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a   z += 2;.     }.
36b41 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
36b42 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 69 66  ase 'l':.     if
36b43 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a  ( z[0]=='e' && z
36b44 5b 32 5d 3d 3d 27 62 27 20 26 26 20 28 7a 5b 33  [2]=='b' && (z[3
36b45 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d  ]=='a' || z[3]==
36b46 27 69 27 29 20 26 26 20 6d 5f 67 74 5f 31 28 7a  'i') && m_gt_1(z
36b47 2b 34 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +4) ){.       z 
36b48 2b 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 4;.     }.   
36b49 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
36b4a 20 27 6e 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'n':.     if( z
36b4b 5b 30 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20  [0]=='t' ){.    
36b4c 20 20 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 61 27     if( z[2]=='a'
36b4d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 69 66 28   ){.         if(
36b4e 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a   m_gt_1(z+3) ){.
36b4f 20 20 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20             z += 
36b50 33 3b 0a 20 20 20 20 20 20 20 20 20 7d 0a 20 20  3;.         }.  
36b51 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a       }else if( z
36b52 5b 32 5d 3d 3d 27 65 27 20 29 7b 0a 20 20 20 20  [2]=='e' ){.    
36b53 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74       stem(&z, "t
36b54 6e 65 6d 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  neme", "", m_gt_
36b55 31 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 73  1) ||.         s
36b56 74 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 22 2c 20  tem(&z, "tnem", 
36b57 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20  "", m_gt_1) ||. 
36b58 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c          stem(&z,
36b59 20 22 74 6e 65 22 2c 20 22 22 2c 20 6d 5f 67 74   "tne", "", m_gt
36b5a 5f 31 29 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20  _1);.       }.  
36b5b 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
36b5c 0a 20 20 20 63 61 73 65 20 27 6f 27 3a 0a 20 20  .   case 'o':.  
36b5d 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 75 27     if( z[0]=='u'
36b5e 20 29 7b 0a 20 20 20 20 20 20 20 69 66 28 20 6d   ){.       if( m
36b5f 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20  _gt_1(z+2) ){.  
36b60 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20         z += 2;. 
36b61 20 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 65 6c        }.     }el
36b62 73 65 20 69 66 28 20 7a 5b 33 5d 3d 3d 27 73 27  se if( z[3]=='s'
36b63 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 74 27 20 29 7b   || z[3]=='t' ){
36b64 0a 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  .       stem(&z,
36b65 20 22 6e 6f 69 22 2c 20 22 22 2c 20 6d 5f 67 74   "noi", "", m_gt
36b66 5f 31 29 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  _1);.     }.    
36b67 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
36b68 27 73 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  's':.     if( z[
36b69 30 5d 3d 3d 27 6d 27 20 26 26 20 7a 5b 32 5d 3d  0]=='m' && z[2]=
36b6a 3d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='i' && m_gt_1(z
36b6b 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +3) ){.       z 
36b6c 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 3;.     }.   
36b6d 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
36b6e 20 27 74 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   't':.     stem(
36b6f 26 7a 2c 20 22 65 74 61 22 2c 20 22 22 2c 20 6d  &z, "eta", "", m
36b70 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 73  _gt_1) ||.     s
36b71 74 65 6d 28 26 7a 2c 20 22 69 74 69 22 2c 20 22  tem(&z, "iti", "
36b72 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20  ", m_gt_1);.    
36b73 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
36b74 27 75 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'u':.     if( z[
36b75 30 5d 3d 3d 27 73 27 20 26 26 20 7a 5b 32 5d 3d  0]=='s' && z[2]=
36b76 3d 27 6f 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='o' && m_gt_1(z
36b77 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +3) ){.       z 
36b78 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 3;.     }.   
36b79 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
36b7a 20 27 76 27 3a 0a 20 20 20 63 61 73 65 20 27 7a   'v':.   case 'z
36b7b 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d  ':.     if( z[0]
36b7c 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27  =='e' && z[2]=='
36b7d 69 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33  i' && m_gt_1(z+3
36b7e 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d  ) ){.       z +=
36b7f 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20   3;.     }.     
36b80 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  break;.  }..  /*
36b81 20 53 74 65 70 20 35 61 20 2a 2f 0a 20 20 69 66   Step 5a */.  if
36b82 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 29 7b 0a 20  ( z[0]=='e' ){. 
36b83 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b     if( m_gt_1(z+
36b84 31 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b  1) ){.      z++;
36b85 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d  .    }else if( m
36b86 5f 65 71 5f 31 28 7a 2b 31 29 20 26 26 20 21 73  _eq_1(z+1) && !s
36b87 74 61 72 5f 6f 68 28 7a 2b 31 29 20 29 7b 0a 20  tar_oh(z+1) ){. 
36b88 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a       z++;.    }.
36b89 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35    }..  /* Step 5
36b8a 62 20 2a 2f 0a 20 20 69 66 28 20 6d 5f 67 74 5f  b */.  if( m_gt_
36b8b 31 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 27 6c  1(z) && z[0]=='l
36b8c 27 20 26 26 20 7a 5b 31 5d 3d 3d 27 6c 27 20 29  ' && z[1]=='l' )
36b8d 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 0a  {.    z++;.  }..
36b8e 20 20 2f 2a 20 7a 5b 5d 20 69 73 20 6e 6f 77 20    /* z[] is now 
36b8f 74 68 65 20 73 74 65 6d 6d 65 64 20 77 6f 72 64  the stemmed word
36b90 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65   in reverse orde
36b91 72 2e 20 20 46 6c 69 70 20 69 74 20 62 61 63 6b  r.  Flip it back
36b92 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 69 6e 74  .  ** around int
36b93 6f 20 66 6f 72 77 61 72 64 20 6f 72 64 65 72 20  o forward order 
36b94 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f  and return..  */
36b95 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69 20 3d 20  .  *pnOut = i = 
36b96 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 7a 4f 75  strlen(z);.  zOu
36b97 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 77 68 69 6c  t[i] = 0;.  whil
36b98 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 7a 4f 75  e( *z ){.    zOu
36b99 74 5b 2d 2d 69 5d 20 3d 20 2a 28 7a 2b 2b 29 3b  t[--i] = *(z++);
36b9a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  .  }.}../*.** Ch
36b9b 61 72 61 63 74 65 72 73 20 74 68 61 74 20 63 61  aracters that ca
36b9c 6e 20 62 65 20 70 61 72 74 20 6f 66 20 61 20 74  n be part of a t
36b9d 6f 6b 65 6e 2e 20 20 57 65 20 61 73 73 75 6d 65  oken.  We assume
36b9e 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 0a 2a   any character.*
36b9f 2a 20 77 68 6f 73 65 20 76 61 6c 75 65 20 69 73  * whose value is
36ba0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 78   greater than 0x
36ba1 38 30 20 28 61 6e 79 20 55 54 46 20 63 68 61 72  80 (any UTF char
36ba2 61 63 74 65 72 29 20 63 61 6e 20 62 65 0a 2a 2a  acter) can be.**
36ba3 20 70 61 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e   part of a token
36ba4 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
36ba5 73 2c 20 64 65 6c 69 6d 69 74 65 72 73 20 61 6c  s, delimiters al
36ba6 6c 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a 20 76  l must have.** v
36ba7 61 6c 75 65 73 20 6f 66 20 30 78 37 66 20 6f 72  alues of 0x7f or
36ba8 20 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 69   lower..*/.stati
36ba9 63 20 63 6f 6e 73 74 20 63 68 61 72 20 70 6f 72  c const char por
36baa 74 65 72 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a  terIdChar[] = {.
36bab 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78  /* x0 x1 x2 x3 x
36bac 34 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39  4 x5 x6 x7 x8 x9
36bad 20 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20   xA xB xC xD xE 
36bae 78 46 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  xF */.    1, 1, 
36baf 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
36bb0 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
36bb1 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78   0, 0, 0,  /* 3x
36bb2 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
36bb3 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
36bb4 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
36bb5 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a  , 1, 1,  /* 4x *
36bb6 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
36bb7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
36bb8 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
36bb9 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  0, 1,  /* 5x */.
36bba 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
36bbb 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
36bbc 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
36bbd 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   1,  /* 6x */.  
36bbe 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
36bbf 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
36bc0 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
36bc1 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23  ,  /* 7x */.};.#
36bc2 64 65 66 69 6e 65 20 69 73 44 65 6c 69 6d 28 43  define isDelim(C
36bc3 29 20 28 28 28 63 68 3d 43 29 26 30 78 38 30 29  ) (((ch=C)&0x80)
36bc4 3d 3d 30 20 26 26 20 28 63 68 3c 30 78 33 30 20  ==0 && (ch<0x30 
36bc5 7c 7c 20 21 70 6f 72 74 65 72 49 64 43 68 61 72  || !porterIdChar
36bc6 5b 63 68 2d 30 78 33 30 5d 29 29 0a 0a 2f 2a 0a  [ch-0x30]))../*.
36bc7 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e  ** Extract the n
36bc8 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61  ext token from a
36bc9 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75   tokenization cu
36bca 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f  rsor.  The curso
36bcb 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 62  r must.** have b
36bcc 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20  een opened by a 
36bcd 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 70 6f  prior call to po
36bce 72 74 65 72 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73  rterOpen()..*/.s
36bcf 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72  tatic int porter
36bd0 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f  Next(.  sqlite3_
36bd1 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
36bd2 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43   *pCursor,  /* C
36bd3 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20 62  ursor returned b
36bd4 79 20 70 6f 72 74 65 72 4f 70 65 6e 20 2a 2f 0a  y porterOpen */.
36bd5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
36bd6 7a 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20  zToken,         
36bd7 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70        /* OUT: *p
36bd8 7a 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74 6f  zToken is the to
36bd9 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  ken text */.  in
36bda 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20 20  t *pnBytes,     
36bdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bdc 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72    /* OUT: Number
36bdd 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f 6b   of bytes in tok
36bde 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53  en */.  int *piS
36bdf 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20 20  tartOffset,     
36be0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
36be1 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66 66  UT: Starting off
36be2 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  set of token */.
36be3 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73    int *piEndOffs
36be4 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  et,             
36be5 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
36be6 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74  ding offset of t
36be7 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
36be8 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 20  iPosition       
36be9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36bea 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69   OUT: Position i
36beb 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20  nteger of token 
36bec 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74  */.){.  porter_t
36bed 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
36bee 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b  *c = (porter_tok
36bef 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29  enizer_cursor *)
36bf0 20 70 43 75 72 73 6f 72 3b 0a 20 20 63 6f 6e 73   pCursor;.  cons
36bf1 74 20 63 68 61 72 20 2a 7a 20 3d 20 63 2d 3e 7a  t char *z = c->z
36bf2 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c 65 28  Input;..  while(
36bf3 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e   c->iOffset<c->n
36bf4 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 69 6e 74  Input ){.    int
36bf5 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 63   iStartOffset, c
36bf6 68 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20  h;..    /* Scan 
36bf7 70 61 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63  past delimiter c
36bf8 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 20  haracters */.   
36bf9 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73   while( c->iOffs
36bfa 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26 26 20  et<c->nInput && 
36bfb 69 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f 66  isDelim(z[c->iOf
36bfc 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20  fset]) ){.      
36bfd 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20  c->iOffset++;.  
36bfe 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 75 6e    }..    /* Coun
36bff 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65 72 20  t non-delimiter 
36c00 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f 0a 20  characters. */. 
36c01 20 20 20 69 53 74 61 72 74 4f 66 66 73 65 74 20     iStartOffset 
36c02 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20  = c->iOffset;.  
36c03 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66    while( c->iOff
36c04 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26 26  set<c->nInput &&
36c05 20 21 69 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69   !isDelim(z[c->i
36c06 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20  Offset]) ){.    
36c07 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a    c->iOffset++;.
36c08 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 63      }..    if( c
36c09 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74 61 72 74  ->iOffset>iStart
36c0a 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20  Offset ){.      
36c0b 69 6e 74 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73  int n = c->iOffs
36c0c 65 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74 3b  et-iStartOffset;
36c0d 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e  .      if( n>c->
36c0e 6e 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a 20 20  nAllocated ){.  
36c0f 20 20 20 20 20 20 63 2d 3e 6e 41 6c 6c 6f 63 61        c->nAlloca
36c10 74 65 64 20 3d 20 6e 2b 32 30 3b 0a 20 20 20 20  ted = n+20;.    
36c11 20 20 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20      c->zToken = 
36c12 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
36c13 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 41  c->zToken, c->nA
36c14 6c 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20 20 20  llocated);.     
36c15 20 20 20 69 66 28 20 63 2d 3e 7a 54 6f 6b 65 6e     if( c->zToken
36c16 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20  ==NULL ) return 
36c17 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
36c18 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6f 72 74      }.      port
36c19 65 72 5f 73 74 65 6d 6d 65 72 28 26 7a 5b 69 53  er_stemmer(&z[iS
36c1a 74 61 72 74 4f 66 66 73 65 74 5d 2c 20 6e 2c 20  tartOffset], n, 
36c1b 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70 6e 42 79 74  c->zToken, pnByt
36c1c 65 73 29 3b 0a 20 20 20 20 20 20 2a 70 7a 54 6f  es);.      *pzTo
36c1d 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f 6b 65 6e 3b  ken = c->zToken;
36c1e 0a 20 20 20 20 20 20 2a 70 69 53 74 61 72 74 4f  .      *piStartO
36c1f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f 66  ffset = iStartOf
36c20 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69 45  fset;.      *piE
36c21 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f  ndOffset = c->iO
36c22 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69  ffset;.      *pi
36c23 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e 69 54  Position = c->iT
36c24 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 72 65  oken++;.      re
36c25 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
36c26 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
36c27 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
36c28 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74  }../*.** The set
36c29 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68 61   of routines tha
36c2a 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  t implement the 
36c2b 70 6f 72 74 65 72 2d 73 74 65 6d 6d 65 72 20 74  porter-stemmer t
36c2c 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74  okenizer.*/.stat
36c2d 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
36c2e 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
36c2f 65 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65  e porterTokenize
36c30 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c  rModule = {.  0,
36c31 0a 20 20 70 6f 72 74 65 72 43 72 65 61 74 65 2c  .  porterCreate,
36c32 0a 20 20 70 6f 72 74 65 72 44 65 73 74 72 6f 79  .  porterDestroy
36c33 2c 0a 20 20 70 6f 72 74 65 72 4f 70 65 6e 2c 0a  ,.  porterOpen,.
36c34 20 20 70 6f 72 74 65 72 43 6c 6f 73 65 2c 0a 20    porterClose,. 
36c35 20 70 6f 72 74 65 72 4e 65 78 74 2c 0a 7d 3b 0a   porterNext,.};.
36c36 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
36c37 61 20 6e 65 77 20 70 6f 72 74 65 72 20 74 6f 6b  a new porter tok
36c38 65 6e 69 7a 65 72 2e 20 20 52 65 74 75 72 6e 20  enizer.  Return 
36c39 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
36c3a 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65   new.** tokenize
36c3b 72 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a  r in *ppModule.*
36c3c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
36c3d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
36c3e 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72  3PorterTokenizer
36c3f 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  Module(.  sqlite
36c40 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
36c41 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75  le const**ppModu
36c42 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c  le.){.  *ppModul
36c43 65 20 3d 20 26 70 6f 72 74 65 72 54 6f 6b 65 6e  e = &porterToken
36c44 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23  izerModule;.}..#
36c45 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
36c46 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
36c47 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
36c48 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
36c49 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
36c4a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 70 6f  * End of fts3_po
36c4b 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  rter.c *********
36c4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c4e 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
36c4f 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
36c50 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a  3_tokenizer.c **
36c51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c53 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a 75 6e  /./*.** 2007 Jun
36c54 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  e 22.**.** The a
36c55 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
36c56 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
36c57 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
36c58 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
36c59 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
36c5a 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
36c5b 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
36c5c 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
36c5d 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
36c5e 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
36c5f 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
36c60 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
36c61 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
36c62 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
36c63 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
36c64 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
36c65 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
36c66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c6a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
36c6b 68 69 73 20 69 73 20 70 61 72 74 20 6f 66 20 61  his is part of a
36c6c 6e 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20  n SQLite module 
36c6d 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66 75 6c  implementing ful
36c6e 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e 0a 2a  l-text search..*
36c6f 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c 61  * This particula
36c70 72 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  r file implement
36c71 73 20 74 68 65 20 67 65 6e 65 72 69 63 20 74 6f  s the generic to
36c72 6b 65 6e 69 7a 65 72 20 69 6e 74 65 72 66 61 63  kenizer interfac
36c73 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  e..*/../*.** The
36c74 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
36c75 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69  le is only compi
36c76 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20  led if:.**.**   
36c77 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
36c78 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
36c79 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69  lt as an extensi
36c7a 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20  on.**       (in 
36c7b 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
36c7c 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65  E_CORE is not de
36c7d 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a  fined), or.**.**
36c7e 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20       * The FTS3 
36c7f 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20  module is being 
36c80 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63  built into the c
36c81 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20  ore of.**       
36c82 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68  SQLite (in which
36c83 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41   case SQLITE_ENA
36c84 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69  BLE_FTS3 is defi
36c85 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  ned)..*/.#if !de
36c86 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
36c87 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
36c88 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
36c89 29 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  )..#ifndef SQLIT
36c8a 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f  E_CORE.  SQLITE_
36c8b 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
36c8c 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49  #endif.../*.** I
36c8d 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
36c8e 20 74 68 65 20 53 51 4c 20 73 63 61 6c 61 72 20   the SQL scalar 
36c8f 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 63 63  function for acc
36c90 65 73 73 69 6e 67 20 74 68 65 20 75 6e 64 65 72  essing the under
36c91 6c 79 69 6e 67 20 0a 2a 2a 20 68 61 73 68 20 74  lying .** hash t
36c92 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74  able. This funct
36c93 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  ion may be calle
36c94 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  d as follows:.**
36c95 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75  .**   SELECT <fu
36c96 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65  nction-name>(<ke
36c97 79 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a 20 20 20 53  y-name>);.**   S
36c98 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d  ELECT <function-
36c99 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e  name>(<key-name>
36c9a 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a 2a  , <pointer>);.**
36c9b 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63 74  .** where <funct
36c9c 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65  ion-name> is the
36c9d 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73 20   name passed as 
36c9e 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
36c9f 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71  ent.** to the sq
36ca0 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73  lite3Fts3InitHas
36ca1 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69 6f  hTable() functio
36ca2 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f  n (e.g. 'fts3_to
36ca3 6b 65 6e 69 7a 65 72 27 29 2e 0a 2a 2a 0a 2a 2a  kenizer')..**.**
36ca4 20 49 66 20 74 68 65 20 3c 70 6f 69 6e 74 65 72   If the <pointer
36ca5 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 73 70  > argument is sp
36ca6 65 63 69 66 69 65 64 2c 20 69 74 20 6d 75 73 74  ecified, it must
36ca7 20 62 65 20 61 20 62 6c 6f 62 20 76 61 6c 75 65   be a blob value
36ca8 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  .** containing a
36ca9 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 73   pointer to be s
36caa 74 6f 72 65 64 20 61 73 20 74 68 65 20 68 61 73  tored as the has
36cab 68 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e  h data correspon
36cac 64 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 73  ding.** to the s
36cad 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e  tring <key-name>
36cae 2e 20 49 66 20 3c 70 6f 69 6e 74 65 72 3e 20 69  . If <pointer> i
36caf 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 2c  s not specified,
36cb0 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 73 74 72   then.** the str
36cb1 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20 6d  ing <key-name> m
36cb2 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ust already exis
36cb3 74 20 69 6e 20 74 68 65 20 68 61 73 20 74 61 62  t in the has tab
36cb4 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a  le. Otherwise,.*
36cb5 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  * an error is re
36cb6 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68  turned..**.** Wh
36cb7 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
36cb8 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67 75 6d   <pointer> argum
36cb9 65 6e 74 20 69 73 20 73 70 65 63 69 66 69 65 64  ent is specified
36cba 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  , the value retu
36cbb 72 6e 65 64 0a 2a 2a 20 69 73 20 61 20 62 6c 6f  rned.** is a blo
36cbc 62 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  b containing the
36cbd 20 70 6f 69 6e 74 65 72 20 73 74 6f 72 65 64 20   pointer stored 
36cbe 61 73 20 74 68 65 20 68 61 73 68 20 64 61 74 61  as the hash data
36cbf 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a   corresponding.*
36cc0 2a 20 74 6f 20 73 74 72 69 6e 67 20 3c 6b 65 79  * to string <key
36cc1 2d 6e 61 6d 65 3e 20 28 61 66 74 65 72 20 74 68  -name> (after th
36cc2 65 20 68 61 73 68 2d 74 61 62 6c 65 20 69 73 20  e hash-table is 
36cc3 75 70 64 61 74 65 64 2c 20 69 66 20 61 70 70 6c  updated, if appl
36cc4 69 63 61 62 6c 65 29 2e 0a 2a 2f 0a 73 74 61 74  icable)..*/.stat
36cc5 69 63 20 76 6f 69 64 20 73 63 61 6c 61 72 46 75  ic void scalarFu
36cc6 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
36cc7 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
36cc8 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
36cc9 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
36cca 67 76 0a 29 7b 0a 20 20 46 74 73 33 48 61 73 68  gv.){.  Fts3Hash
36ccb 20 2a 70 48 61 73 68 3b 0a 20 20 76 6f 69 64 20   *pHash;.  void 
36ccc 2a 70 50 74 72 20 3d 20 30 3b 0a 20 20 63 6f 6e  *pPtr = 0;.  con
36ccd 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
36cce 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e   *zName;.  int n
36ccf 4e 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Name;..  assert(
36cd0 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63   argc==1 || argc
36cd1 3d 3d 32 20 29 3b 0a 0a 20 20 70 48 61 73 68 20  ==2 );..  pHash 
36cd2 3d 20 28 46 74 73 33 48 61 73 68 20 2a 29 73 71  = (Fts3Hash *)sq
36cd3 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
36cd4 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 4e 61  context);..  zNa
36cd5 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  me = sqlite3_val
36cd6 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
36cd7 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  ;.  nName = sqli
36cd8 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
36cd9 61 72 67 76 5b 30 5d 29 2b 31 3b 0a 0a 20 20 69  argv[0])+1;..  i
36cda 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20  f( argc==2 ){.  
36cdb 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20 20    void *pOld;.  
36cdc 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
36cdd 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
36cde 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20  gv[1]);.    if( 
36cdf 6e 21 3d 73 69 7a 65 6f 66 28 70 50 74 72 29 20  n!=sizeof(pPtr) 
36ce0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
36ce1 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
36ce2 6e 74 65 78 74 2c 20 22 61 72 67 75 6d 65 6e 74  ntext, "argument
36ce3 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 22 2c   type mismatch",
36ce4 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75   -1);.      retu
36ce5 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rn;.    }.    pP
36ce6 74 72 20 3d 20 2a 28 76 6f 69 64 20 2a 2a 29 73  tr = *(void **)s
36ce7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
36ce8 62 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  b(argv[1]);.    
36ce9 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 46 74  pOld = sqlite3Ft
36cea 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  s3HashInsert(pHa
36ceb 73 68 2c 20 28 76 6f 69 64 20 2a 29 7a 4e 61 6d  sh, (void *)zNam
36cec 65 2c 20 6e 4e 61 6d 65 2c 20 70 50 74 72 29 3b  e, nName, pPtr);
36ced 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 3d 3d 70  .    if( pOld==p
36cee 50 74 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Ptr ){.      sql
36cef 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
36cf0 72 28 63 6f 6e 74 65 78 74 2c 20 22 6f 75 74 20  r(context, "out 
36cf1 6f 66 20 6d 65 6d 6f 72 79 22 2c 20 2d 31 29 3b  of memory", -1);
36cf2 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
36cf3 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
36cf4 20 20 70 50 74 72 20 3d 20 73 71 6c 69 74 65 33    pPtr = sqlite3
36cf5 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48 61  Fts3HashFind(pHa
36cf6 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  sh, zName, nName
36cf7 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 74 72  );.    if( !pPtr
36cf8 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a   ){.      char *
36cf9 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
36cfa 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20  printf("unknown 
36cfb 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20  tokenizer: %s", 
36cfc 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71  zName);.      sq
36cfd 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
36cfe 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72  or(context, zErr
36cff 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , -1);.      sql
36d00 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b  ite3_free(zErr);
36d01 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
36d02 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
36d03 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28  te3_result_blob(
36d04 63 6f 6e 74 65 78 74 2c 20 28 76 6f 69 64 20 2a  context, (void *
36d05 29 26 70 50 74 72 2c 20 73 69 7a 65 6f 66 28 70  )&pPtr, sizeof(p
36d06 50 74 72 29 2c 20 53 51 4c 49 54 45 5f 54 52 41  Ptr), SQLITE_TRA
36d07 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a 73 74 61 74  NSIENT);.}..stat
36d08 69 63 20 69 6e 74 20 66 74 73 33 49 73 49 64 43  ic int fts3IsIdC
36d09 68 61 72 28 63 68 61 72 20 63 29 7b 0a 20 20 73  har(char c){.  s
36d0a 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
36d0b 20 69 73 46 74 73 49 64 43 68 61 72 5b 5d 20 3d   isFtsIdChar[] =
36d0c 20 7b 0a 20 20 20 20 20 20 30 2c 20 30 2c 20 30   {.      0, 0, 0
36d0d 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
36d0e 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
36d0f 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 30 78 20  0, 0, 0,  /* 0x 
36d10 2a 2f 0a 20 20 20 20 20 20 30 2c 20 30 2c 20 30  */.      0, 0, 0
36d11 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
36d12 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
36d13 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 31 78 20  0, 0, 0,  /* 1x 
36d14 2a 2f 0a 20 20 20 20 20 20 30 2c 20 30 2c 20 30  */.      0, 0, 0
36d15 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 1, 0, 0, 0,
36d16 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
36d17 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 32 78 20  0, 0, 0,  /* 2x 
36d18 2a 2f 0a 20 20 20 20 20 20 31 2c 20 31 2c 20 31  */.      1, 1, 1
36d19 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
36d1a 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
36d1b 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78 20  0, 0, 0,  /* 3x 
36d1c 2a 2f 0a 20 20 20 20 20 20 30 2c 20 31 2c 20 31  */.      0, 1, 1
36d1d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
36d1e 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
36d1f 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20  1, 1, 1,  /* 4x 
36d20 2a 2f 0a 20 20 20 20 20 20 31 2c 20 31 2c 20 31  */.      1, 1, 1
36d21 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
36d22 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
36d23 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20  0, 0, 1,  /* 5x 
36d24 2a 2f 0a 20 20 20 20 20 20 30 2c 20 31 2c 20 31  */.      0, 1, 1
36d25 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
36d26 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
36d27 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20  1, 1, 1,  /* 6x 
36d28 2a 2f 0a 20 20 20 20 20 20 31 2c 20 31 2c 20 31  */.      1, 1, 1
36d29 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
36d2a 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
36d2b 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20  0, 0, 0,  /* 7x 
36d2c 2a 2f 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e  */.  };.  return
36d2d 20 28 63 26 30 78 38 30 20 7c 7c 20 69 73 46 74   (c&0x80 || isFt
36d2e 73 49 64 43 68 61 72 5b 28 69 6e 74 29 28 63 29  sIdChar[(int)(c)
36d2f 5d 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  ]);.}..SQLITE_PR
36d30 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
36d31 20 2a 73 71 6c 69 74 65 33 46 74 73 33 4e 65 78   *sqlite3Fts3Nex
36d32 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 63 68 61  tToken(const cha
36d33 72 20 2a 7a 53 74 72 2c 20 69 6e 74 20 2a 70 6e  r *zStr, int *pn
36d34 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
36d35 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  *z1;.  const cha
36d36 72 20 2a 7a 32 20 3d 20 30 3b 0a 0a 20 20 2f 2a  r *z2 = 0;..  /*
36d37 20 46 69 6e 64 20 74 68 65 20 73 74 61 72 74 20   Find the start 
36d38 6f 66 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65  of the next toke
36d39 6e 2e 20 2a 2f 0a 20 20 7a 31 20 3d 20 7a 53 74  n. */.  z1 = zSt
36d3a 72 3b 0a 20 20 77 68 69 6c 65 28 20 7a 32 3d 3d  r;.  while( z2==
36d3b 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28  0 ){.    switch(
36d3c 20 2a 7a 31 20 29 7b 0a 20 20 20 20 20 20 63 61   *z1 ){.      ca
36d3d 73 65 20 27 5c 30 27 3a 20 72 65 74 75 72 6e 20  se '\0': return 
36d3e 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20  0;        /* No 
36d3f 6d 6f 72 65 20 74 6f 6b 65 6e 73 20 68 65 72 65  more tokens here
36d40 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 27   */.      case '
36d41 5c 27 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20  \'':.      case 
36d42 27 22 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20  '"':.      case 
36d43 27 60 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 7a  '`': {.        z
36d44 32 20 3d 20 26 7a 31 5b 31 5d 3b 0a 20 20 20 20  2 = &z1[1];.    
36d45 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 26      while( *z2 &
36d46 26 20 28 7a 32 5b 30 5d 21 3d 2a 7a 31 20 7c 7c  & (z2[0]!=*z1 ||
36d47 20 7a 32 5b 31 5d 3d 3d 2a 7a 31 29 20 29 20 7a   z2[1]==*z1) ) z
36d48 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 66 28  2++;.        if(
36d49 20 2a 7a 32 20 29 20 7a 32 2b 2b 3b 0a 20 20 20   *z2 ) z2++;.   
36d4a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
36d4b 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27    }.      case '
36d4c 5b 27 3a 0a 20 20 20 20 20 20 20 20 7a 32 20 3d  [':.        z2 =
36d4d 20 26 7a 31 5b 31 5d 3b 0a 20 20 20 20 20 20 20   &z1[1];.       
36d4e 20 77 68 69 6c 65 28 20 2a 7a 32 20 26 26 20 7a   while( *z2 && z
36d4f 32 5b 30 5d 21 3d 27 5d 27 20 29 20 7a 32 2b 2b  2[0]!=']' ) z2++
36d50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a  ;.        if( *z
36d51 32 20 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20  2 ) z2++;.      
36d52 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20    break;..      
36d53 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20  default:.       
36d54 20 69 66 28 20 66 74 73 33 49 73 49 64 43 68 61   if( fts3IsIdCha
36d55 72 28 2a 7a 31 29 20 29 7b 0a 20 20 20 20 20 20  r(*z1) ){.      
36d56 20 20 20 20 7a 32 20 3d 20 26 7a 31 5b 31 5d 3b      z2 = &z1[1];
36d57 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
36d58 28 20 66 74 73 33 49 73 49 64 43 68 61 72 28 2a  ( fts3IsIdChar(*
36d59 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a 20 20 20 20  z2) ) z2++;.    
36d5a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36d5b 20 20 20 20 20 7a 31 2b 2b 3b 0a 20 20 20 20 20       z1++;.     
36d5c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
36d5d 20 20 2a 70 6e 20 3d 20 28 7a 32 2d 7a 31 29 3b    *pn = (z2-z1);
36d5e 0a 20 20 72 65 74 75 72 6e 20 7a 31 3b 0a 7d 0a  .  return z1;.}.
36d5f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
36d60 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49  int sqlite3Fts3I
36d61 6e 69 74 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20  nitTokenizer(.  
36d62 46 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c  Fts3Hash *pHash,
36d63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d64 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 68 61 73  /* Tokenizer has
36d65 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e  h table */.  con
36d66 73 74 20 63 68 61 72 20 2a 7a 41 72 67 2c 20 20  st char *zArg,  
36d67 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36d68 50 6f 73 73 69 62 6c 65 20 74 6f 6b 65 6e 69 7a  Possible tokeniz
36d69 65 72 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e  er specification
36d6a 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f   */.  sqlite3_to
36d6b 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 2c  kenizer **ppTok,
36d6c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f        /* OUT: To
36d6d 6b 65 6e 69 7a 65 72 20 28 69 66 20 61 70 70 6c  kenizer (if appl
36d6e 69 63 61 62 6c 65 29 20 2a 2f 0a 20 20 63 6f 6e  icable) */.  con
36d6f 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 6f 6b 65  st char **pzToke
36d70 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 2f 2a 20  nizer,       /* 
36d71 4f 55 54 3a 20 53 65 74 20 74 6f 20 7a 41 72 67  OUT: Set to zArg
36d72 20 69 66 20 69 73 20 74 6f 6b 65 6e 69 7a 65 72   if is tokenizer
36d73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45   */.  char **pzE
36d74 72 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rr              
36d75 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65        /* OUT: Se
36d76 74 20 74 6f 20 6d 61 6c 6c 6f 63 65 64 20 65 72  t to malloced er
36d77 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 29  ror message */.)
36d78 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68  {.  int rc;.  ch
36d79 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 20 2a 29  ar *z = (char *)
36d7a 7a 41 72 67 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20  zArg;.  int n;. 
36d7b 20 63 68 61 72 20 2a 7a 43 6f 70 79 3b 0a 20 20   char *zCopy;.  
36d7c 63 68 61 72 20 2a 7a 45 6e 64 3b 20 20 20 20 20  char *zEnd;     
36d7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d7e 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 75  /* Pointer to nu
36d7f 6c 2d 74 65 72 6d 20 6f 66 20 7a 43 6f 70 79 20  l-term of zCopy 
36d80 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
36d81 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 6d  enizer_module *m
36d82 3b 0a 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20  ;..  if( !z ){. 
36d83 20 20 20 7a 43 6f 70 79 20 3d 20 73 71 6c 69 74     zCopy = sqlit
36d84 65 33 5f 6d 70 72 69 6e 74 66 28 22 73 69 6d 70  e3_mprintf("simp
36d85 6c 65 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  le");.  }else{. 
36d86 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 26 30 78     while( (*z&0x
36d87 38 30 29 20 26 26 20 69 73 73 70 61 63 65 28 2a  80) && isspace(*
36d88 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 69 66  z) ) z++;.    if
36d89 28 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63  ( sqlite3_strnic
36d8a 6d 70 28 7a 2c 20 22 74 6f 6b 65 6e 69 7a 65 22  mp(z, "tokenize"
36d8b 2c 20 38 29 20 7c 7c 20 66 74 73 33 49 73 49 64  , 8) || fts3IsId
36d8c 43 68 61 72 28 7a 5b 38 5d 29 29 7b 0a 20 20 20  Char(z[8])){.   
36d8d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
36d8e 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  _OK;.    }.    z
36d8f 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Copy = sqlite3_m
36d90 70 72 69 6e 74 66 28 22 25 73 22 2c 20 26 7a 5b  printf("%s", &z[
36d91 38 5d 29 3b 0a 20 20 20 20 2a 70 7a 54 6f 6b 65  8]);.    *pzToke
36d92 6e 69 7a 65 72 20 3d 20 7a 41 72 67 3b 0a 20 20  nizer = zArg;.  
36d93 7d 0a 20 20 69 66 28 20 21 7a 43 6f 70 79 20 29  }.  if( !zCopy )
36d94 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
36d95 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a  ITE_NOMEM;.  }..
36d96 20 20 7a 45 6e 64 20 3d 20 26 7a 43 6f 70 79 5b    zEnd = &zCopy[
36d97 73 74 72 6c 65 6e 28 7a 43 6f 70 79 29 5d 3b 0a  strlen(zCopy)];.
36d98 0a 20 20 7a 20 3d 20 28 63 68 61 72 20 2a 29 73  .  z = (char *)s
36d99 71 6c 69 74 65 33 46 74 73 33 4e 65 78 74 54 6f  qlite3Fts3NextTo
36d9a 6b 65 6e 28 7a 43 6f 70 79 2c 20 26 6e 29 3b 0a  ken(zCopy, &n);.
36d9b 20 20 7a 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20    z[n] = '\0';. 
36d9c 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 71 75   sqlite3Fts3Dequ
36d9d 6f 74 65 28 7a 29 3b 0a 0a 20 20 6d 20 3d 20 28  ote(z);..  m = (
36d9e 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
36d9f 72 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74  r_module *)sqlit
36da0 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 70  e3Fts3HashFind(p
36da1 48 61 73 68 2c 20 7a 2c 20 73 74 72 6c 65 6e 28  Hash, z, strlen(
36da2 7a 29 2b 31 29 3b 0a 20 20 69 66 28 20 21 6d 20  z)+1);.  if( !m 
36da3 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  ){.    *pzErr = 
36da4 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
36da5 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a  "unknown tokeniz
36da6 65 72 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20  er: %s", z);.   
36da7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
36da8 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OR;.  }else{.   
36da9 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 61 41   char const **aA
36daa 72 67 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  rg = 0;.    int 
36dab 69 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 7a 20  iArg = 0;.    z 
36dac 3d 20 26 7a 5b 6e 2b 31 5d 3b 0a 20 20 20 20 77  = &z[n+1];.    w
36dad 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26 20  hile( z<zEnd && 
36dae 28 7a 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  (z = (char *)sql
36daf 69 74 65 33 46 74 73 33 4e 65 78 74 54 6f 6b 65  ite3Fts3NextToke
36db0 6e 28 7a 2c 20 26 6e 29 29 20 29 7b 0a 20 20 20  n(z, &n)) ){.   
36db1 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 73 69     int nNew = si
36db2 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 28 69 41  zeof(char *)*(iA
36db3 72 67 2b 31 29 3b 0a 20 20 20 20 20 20 63 68 61  rg+1);.      cha
36db4 72 20 63 6f 6e 73 74 20 2a 2a 61 4e 65 77 20 3d  r const **aNew =
36db5 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29   (const char **)
36db6 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
36db7 61 41 72 67 2c 20 6e 4e 65 77 29 3b 0a 20 20 20  aArg, nNew);.   
36db8 20 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b 0a     if( !aNew ){.
36db9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
36dba 66 72 65 65 28 7a 43 6f 70 79 29 3b 0a 20 20 20  free(zCopy);.   
36dbb 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
36dbc 65 28 61 41 72 67 29 3b 0a 20 20 20 20 20 20 20  e(aArg);.       
36dbd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
36dbe 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
36dbf 20 20 20 20 61 41 72 67 20 3d 20 61 4e 65 77 3b      aArg = aNew;
36dc0 0a 20 20 20 20 20 20 61 41 72 67 5b 69 41 72 67  .      aArg[iArg
36dc1 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 20 20 7a  ++] = z;.      z
36dc2 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  [n] = '\0';.    
36dc3 20 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 71    sqlite3Fts3Deq
36dc4 75 6f 74 65 28 7a 29 3b 0a 20 20 20 20 20 20 7a  uote(z);.      z
36dc5 20 3d 20 26 7a 5b 6e 2b 31 5d 3b 0a 20 20 20 20   = &z[n+1];.    
36dc6 7d 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e 78 43  }.    rc = m->xC
36dc7 72 65 61 74 65 28 69 41 72 67 2c 20 61 41 72 67  reate(iArg, aArg
36dc8 2c 20 70 70 54 6f 6b 29 3b 0a 20 20 20 20 61 73  , ppTok);.    as
36dc9 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
36dca 5f 4f 4b 20 7c 7c 20 2a 70 70 54 6f 6b 20 29 3b  _OK || *ppTok );
36dcb 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
36dcc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
36dcd 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33  *pzErr = sqlite3
36dce 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77  _mprintf("unknow
36dcf 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22  n tokenizer: %s"
36dd0 2c 20 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , z);.    }else{
36dd1 0a 20 20 20 20 20 20 28 2a 70 70 54 6f 6b 29 2d  .      (*ppTok)-
36dd2 3e 70 4d 6f 64 75 6c 65 20 3d 20 6d 3b 20 0a 20  >pModule = m; . 
36dd3 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
36dd4 5f 66 72 65 65 28 61 41 72 67 29 3b 0a 20 20 7d  _free(aArg);.  }
36dd5 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
36dd6 28 7a 43 6f 70 79 29 3b 0a 20 20 72 65 74 75 72  (zCopy);.  retur
36dd7 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66  n rc;.}...#ifdef
36dd8 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 0a 0a 2f   SQLITE_TEST.../
36dd9 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
36dda 69 6f 6e 20 6f 66 20 61 20 73 70 65 63 69 61 6c  ion of a special
36ddb 20 53 51 4c 20 73 63 61 6c 61 72 20 66 75 6e 63   SQL scalar func
36ddc 74 69 6f 6e 20 66 6f 72 20 74 65 73 74 69 6e 67  tion for testing
36ddd 20 74 6f 6b 65 6e 69 7a 65 72 73 20 0a 2a 2a 20   tokenizers .** 
36dde 64 65 73 69 67 6e 65 64 20 74 6f 20 62 65 20 75  designed to be u
36ddf 73 65 64 20 69 6e 20 63 6f 6e 63 65 72 74 20 77  sed in concert w
36de0 69 74 68 20 74 68 65 20 54 63 6c 20 74 65 73 74  ith the Tcl test
36de1 69 6e 67 20 66 72 61 6d 65 77 6f 72 6b 2e 20 54  ing framework. T
36de2 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
36de3 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 77  must be called w
36de4 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74  ith two argument
36de5 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43  s:.**.**   SELEC
36de6 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65  T <function-name
36de7 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 69  >(<key-name>, <i
36de8 6e 70 75 74 2d 73 74 72 69 6e 67 3e 29 3b 0a 2a  nput-string>);.*
36de9 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e 63  *   SELECT <func
36dea 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d  tion-name>(<key-
36deb 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e  name>, <pointer>
36dec 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c  );.**.** where <
36ded 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69  function-name> i
36dee 73 20 74 68 65 20 6e 61 6d 65 20 70 61 73 73 65  s the name passe
36def 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
36df0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74  argument.** to t
36df1 68 65 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e  he sqlite3Fts3In
36df2 69 74 48 61 73 68 54 61 62 6c 65 28 29 20 66 75  itHashTable() fu
36df3 6e 63 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74  nction (e.g. 'ft
36df4 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 0a 2a  s3_tokenizer').*
36df5 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 77  * concatenated w
36df6 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 20 27  ith the string '
36df7 5f 74 65 73 74 27 20 28 65 2e 67 2e 20 27 66 74  _test' (e.g. 'ft
36df8 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65 73  s3_tokenizer_tes
36df9 74 27 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  t')..**.** The r
36dfa 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 61  eturn value is a
36dfb 20 73 74 72 69 6e 67 20 74 68 61 74 20 6d 61 79   string that may
36dfc 20 62 65 20 69 6e 74 65 72 70 72 65 74 65 64 20   be interpreted 
36dfd 61 73 20 61 20 54 63 6c 0a 2a 2a 20 6c 69 73 74  as a Tcl.** list
36dfe 2e 20 46 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e  . For each token
36dff 20 69 6e 20 74 68 65 20 3c 69 6e 70 75 74 2d 73   in the <input-s
36e00 74 72 69 6e 67 3e 2c 20 74 68 72 65 65 20 65 6c  tring>, three el
36e01 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 61 64  ements are.** ad
36e02 64 65 64 20 74 6f 20 74 68 65 20 72 65 74 75 72  ded to the retur
36e03 6e 65 64 20 6c 69 73 74 2e 20 54 68 65 20 66 69  ned list. The fi
36e04 72 73 74 20 69 73 20 74 68 65 20 74 6f 6b 65 6e  rst is the token
36e05 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20 0a   position, the .
36e06 2a 2a 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65  ** second is the
36e07 20 74 6f 6b 65 6e 20 74 65 78 74 20 28 66 6f 6c   token text (fol
36e08 64 65 64 2c 20 73 74 65 6d 6d 65 64 2c 20 65 74  ded, stemmed, et
36e09 63 2e 29 20 61 6e 64 20 74 68 65 20 74 68 69 72  c.) and the thir
36e0a 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 75 62 73  d is the.** subs
36e0b 74 72 69 6e 67 20 6f 66 20 3c 69 6e 70 75 74 2d  tring of <input-
36e0c 73 74 72 69 6e 67 3e 20 61 73 73 6f 63 69 61 74  string> associat
36e0d 65 64 20 77 69 74 68 20 74 68 65 20 74 6f 6b 65  ed with the toke
36e0e 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  n. For example, 
36e0f 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 62 75  .** using the bu
36e10 69 6c 74 2d 69 6e 20 22 73 69 6d 70 6c 65 22 20  ilt-in "simple" 
36e11 74 6f 6b 65 6e 69 7a 65 72 3a 0a 2a 2a 0a 2a 2a  tokenizer:.**.**
36e12 20 20 20 53 45 4c 45 43 54 20 66 74 73 5f 74 6f     SELECT fts_to
36e13 6b 65 6e 69 7a 65 72 5f 74 65 73 74 28 27 73 69  kenizer_test('si
36e14 6d 70 6c 65 27 2c 20 27 49 20 64 6f 6e 27 74 20  mple', 'I don't 
36e15 73 65 65 20 68 6f 77 27 29 3b 0a 2a 2a 0a 2a 2a  see how');.**.**
36e16 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74 68 65   will return the
36e17 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20   string:.**.**  
36e18 20 22 7b 30 20 69 20 49 20 31 20 64 6f 6e 74 20   "{0 i I 1 dont 
36e19 64 6f 6e 27 74 20 32 20 73 65 65 20 73 65 65 20  don't 2 see see 
36e1a 33 20 68 6f 77 20 68 6f 77 7d 22 0a 2a 2a 20 20  3 how how}".**  
36e1b 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64   .*/.static void
36e1c 20 74 65 73 74 46 75 6e 63 28 0a 20 20 73 71 6c   testFunc(.  sql
36e1d 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
36e1e 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
36e1f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
36e20 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 46  ue **argv.){.  F
36e21 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 3b 0a  ts3Hash *pHash;.
36e22 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
36e23 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 3b 0a 20  zer_module *p;. 
36e24 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
36e25 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  er *pTokenizer =
36e26 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f   0;.  sqlite3_to
36e27 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
36e28 70 43 73 72 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e  pCsr = 0;..  con
36e29 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20  st char *zErr = 
36e2a 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  0;..  const char
36e2b 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e   *zName;.  int n
36e2c 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Name;.  const ch
36e2d 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 69 6e  ar *zInput;.  in
36e2e 74 20 6e 49 6e 70 75 74 3b 0a 0a 20 20 63 6f 6e  t nInput;..  con
36e2f 73 74 20 63 68 61 72 20 2a 7a 41 72 67 20 3d 20  st char *zArg = 
36e30 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  0;..  const char
36e31 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20   *zToken;.  int 
36e32 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 69 53  nToken;.  int iS
36e33 74 61 72 74 3b 0a 20 20 69 6e 74 20 69 45 6e 64  tart;.  int iEnd
36e34 3b 0a 20 20 69 6e 74 20 69 50 6f 73 3b 0a 0a 20  ;.  int iPos;.. 
36e35 20 54 63 6c 5f 4f 62 6a 20 2a 70 52 65 74 3b 0a   Tcl_Obj *pRet;.
36e36 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
36e37 3d 32 20 7c 7c 20 61 72 67 63 3d 3d 33 20 29 3b  =2 || argc==3 );
36e38 0a 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  ..  nName = sqli
36e39 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
36e3a 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d  argv[0]);.  zNam
36e3b 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
36e3c 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
36e3d 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
36e3e 20 6e 49 6e 70 75 74 20 3d 20 73 71 6c 69 74 65   nInput = sqlite
36e3f 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
36e40 67 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 20 20 7a  gv[argc-1]);.  z
36e41 49 6e 70 75 74 20 3d 20 28 63 6f 6e 73 74 20 63  Input = (const c
36e42 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
36e43 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 61 72  lue_text(argv[ar
36e44 67 63 2d 31 5d 29 3b 0a 0a 20 20 69 66 28 20 61  gc-1]);..  if( a
36e45 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 7a 41  rgc==3 ){.    zA
36e46 72 67 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  rg = (const char
36e47 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
36e48 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
36e49 20 20 7d 0a 0a 20 20 70 48 61 73 68 20 3d 20 28    }..  pHash = (
36e4a 46 74 73 33 48 61 73 68 20 2a 29 73 71 6c 69 74  Fts3Hash *)sqlit
36e4b 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e  e3_user_data(con
36e4c 74 65 78 74 29 3b 0a 20 20 70 20 3d 20 28 73 71  text);.  p = (sq
36e4d 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
36e4e 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  module *)sqlite3
36e4f 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48 61  Fts3HashFind(pHa
36e50 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  sh, zName, nName
36e51 2b 31 29 3b 0a 0a 20 20 69 66 28 20 21 70 20 29  +1);..  if( !p )
36e52 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72  {.    char *zErr
36e53 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
36e54 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65  tf("unknown toke
36e55 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d  nizer: %s", zNam
36e56 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  e);.    sqlite3_
36e57 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
36e58 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b  text, zErr, -1);
36e59 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
36e5a 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 72 65 74  e(zErr);.    ret
36e5b 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 52 65 74  urn;.  }..  pRet
36e5c 20 3d 20 54 63 6c 5f 4e 65 77 4f 62 6a 28 29 3b   = Tcl_NewObj();
36e5d 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f  .  Tcl_IncrRefCo
36e5e 75 6e 74 28 70 52 65 74 29 3b 0a 0a 20 20 69 66  unt(pRet);..  if
36e5f 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e  ( SQLITE_OK!=p->
36e60 78 43 72 65 61 74 65 28 7a 41 72 67 20 3f 20 31  xCreate(zArg ? 1
36e61 20 3a 20 30 2c 20 26 7a 41 72 67 2c 20 26 70 54   : 0, &zArg, &pT
36e62 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20 20 20  okenizer) ){.   
36e63 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69   zErr = "error i
36e64 6e 20 78 43 72 65 61 74 65 28 29 22 3b 0a 20 20  n xCreate()";.  
36e65 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20    goto finish;. 
36e66 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d   }.  pTokenizer-
36e67 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 3b 0a 20 20  >pModule = p;.  
36e68 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70  if( SQLITE_OK!=p
36e69 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a  ->xOpen(pTokeniz
36e6a 65 72 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70  er, zInput, nInp
36e6b 75 74 2c 20 26 70 43 73 72 29 20 29 7b 0a 20 20  ut, &pCsr) ){.  
36e6c 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20    zErr = "error 
36e6d 69 6e 20 78 4f 70 65 6e 28 29 22 3b 0a 20 20 20  in xOpen()";.   
36e6e 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20   goto finish;.  
36e6f 7d 0a 20 20 70 43 73 72 2d 3e 70 54 6f 6b 65 6e  }.  pCsr->pToken
36e70 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65  izer = pTokenize
36e71 72 3b 0a 0a 20 20 77 68 69 6c 65 28 20 53 51 4c  r;..  while( SQL
36e72 49 54 45 5f 4f 4b 3d 3d 70 2d 3e 78 4e 65 78 74  ITE_OK==p->xNext
36e73 28 70 43 73 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20  (pCsr, &zToken, 
36e74 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72 74  &nToken, &iStart
36e75 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 20  , &iEnd, &iPos) 
36e76 29 7b 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f  ){.    Tcl_ListO
36e77 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28  bjAppendElement(
36e78 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77  0, pRet, Tcl_New
36e79 49 6e 74 4f 62 6a 28 69 50 6f 73 29 29 3b 0a 20  IntObj(iPos));. 
36e7a 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70     Tcl_ListObjAp
36e7b 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70  pendElement(0, p
36e7c 52 65 74 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69  Ret, Tcl_NewStri
36e7d 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54  ngObj(zToken, nT
36e7e 6f 6b 65 6e 29 29 3b 0a 20 20 20 20 7a 54 6f 6b  oken));.    zTok
36e7f 65 6e 20 3d 20 26 7a 49 6e 70 75 74 5b 69 53 74  en = &zInput[iSt
36e80 61 72 74 5d 3b 0a 20 20 20 20 6e 54 6f 6b 65 6e  art];.    nToken
36e81 20 3d 20 69 45 6e 64 2d 69 53 74 61 72 74 3b 0a   = iEnd-iStart;.
36e82 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41      Tcl_ListObjA
36e83 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20  ppendElement(0, 
36e84 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 53 74 72  pRet, Tcl_NewStr
36e85 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e  ingObj(zToken, n
36e86 54 6f 6b 65 6e 29 29 3b 0a 20 20 7d 0a 0a 20 20  Token));.  }..  
36e87 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70  if( SQLITE_OK!=p
36e88 2d 3e 78 43 6c 6f 73 65 28 70 43 73 72 29 20 29  ->xClose(pCsr) )
36e89 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72  {.    zErr = "er
36e8a 72 6f 72 20 69 6e 20 78 43 6c 6f 73 65 28 29 22  ror in xClose()"
36e8b 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73  ;.    goto finis
36e8c 68 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c  h;.  }.  if( SQL
36e8d 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 44 65 73 74  ITE_OK!=p->xDest
36e8e 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65 72 29 20  roy(pTokenizer) 
36e8f 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65  ){.    zErr = "e
36e90 72 72 6f 72 20 69 6e 20 78 44 65 73 74 72 6f 79  rror in xDestroy
36e91 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69  ()";.    goto fi
36e92 6e 69 73 68 3b 0a 20 20 7d 0a 0a 66 69 6e 69 73  nish;.  }..finis
36e93 68 3a 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b  h:.  if( zErr ){
36e94 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
36e95 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
36e96 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20  t, zErr, -1);.  
36e97 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
36e98 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
36e99 6f 6e 74 65 78 74 2c 20 54 63 6c 5f 47 65 74 53  ontext, Tcl_GetS
36e9a 74 72 69 6e 67 28 70 52 65 74 29 2c 20 2d 31 2c  tring(pRet), -1,
36e9b 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
36e9c 54 29 3b 0a 20 20 7d 0a 20 20 54 63 6c 5f 44 65  T);.  }.  Tcl_De
36e9d 63 72 52 65 66 43 6f 75 6e 74 28 70 52 65 74 29  crRefCount(pRet)
36e9e 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a 69 6e 74 20  ;.}..static.int 
36e9f 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a 65  registerTokenize
36ea0 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r(.  sqlite3 *db
36ea1 2c 20 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  , .  char *zName
36ea2 2c 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  , .  const sqlit
36ea3 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
36ea4 75 6c 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20  ule *p.){.  int 
36ea5 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  rc;.  sqlite3_st
36ea6 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e  mt *pStmt;.  con
36ea7 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d  st char zSql[] =
36ea8 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f   "SELECT fts3_to
36ea9 6b 65 6e 69 7a 65 72 28 3f 2c 20 3f 29 22 3b 0a  kenizer(?, ?)";.
36eaa 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
36eab 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a  prepare_v2(db, z
36eac 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
36ead 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   0);.  if( rc!=S
36eae 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
36eaf 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
36eb0 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74    sqlite3_bind_t
36eb1 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e  ext(pStmt, 1, zN
36eb2 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ame, -1, SQLITE_
36eb3 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74  STATIC);.  sqlit
36eb4 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53 74  e3_bind_blob(pSt
36eb5 6d 74 2c 20 32 2c 20 26 70 2c 20 73 69 7a 65 6f  mt, 2, &p, sizeo
36eb6 66 28 70 29 2c 20 53 51 4c 49 54 45 5f 53 54 41  f(p), SQLITE_STA
36eb7 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  TIC);.  sqlite3_
36eb8 73 74 65 70 28 70 53 74 6d 74 29 3b 0a 0a 20 20  step(pStmt);..  
36eb9 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66  return sqlite3_f
36eba 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a  inalize(pStmt);.
36ebb 7d 0a 0a 73 74 61 74 69 63 0a 69 6e 74 20 71 75  }..static.int qu
36ebc 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20  eryTokenizer(.  
36ebd 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
36ebe 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 0a 20  char *zName,  . 
36ebf 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
36ec0 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
36ec1 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63  **pp.){.  int rc
36ec2 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
36ec3 20 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74   *pStmt;.  const
36ec4 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22   char zSql[] = "
36ec5 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65  SELECT fts3_toke
36ec6 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a 20 20 2a 70  nizer(?)";..  *p
36ec7 70 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71  p = 0;.  rc = sq
36ec8 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
36ec9 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
36eca 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28  pStmt, 0);.  if(
36ecb 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
36ecc 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
36ecd 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
36ece 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c  bind_text(pStmt,
36ecf 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53   1, zName, -1, S
36ed0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
36ed1 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
36ed2 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53  =sqlite3_step(pS
36ed3 74 6d 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20  tmt) ){.    if( 
36ed4 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
36ed5 79 70 65 28 70 53 74 6d 74 2c 20 30 29 3d 3d 53  ype(pStmt, 0)==S
36ed6 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
36ed7 20 20 20 20 6d 65 6d 63 70 79 28 70 70 2c 20 73      memcpy(pp, s
36ed8 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
36ed9 6f 62 28 70 53 74 6d 74 2c 20 30 29 2c 20 73 69  ob(pStmt, 0), si
36eda 7a 65 6f 66 28 2a 70 70 29 29 3b 0a 20 20 20 20  zeof(*pp));.    
36edb 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
36edc 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
36edd 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 53 51 4c 49  (pStmt);.}..SQLI
36ede 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
36edf 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c  sqlite3Fts3Simpl
36ee0 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  eTokenizerModule
36ee1 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
36ee2 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a  er_module const*
36ee3 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 2f 2a 0a  *ppModule);../*.
36ee4 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
36ee5 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61 72 20  n of the scalar 
36ee6 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f 74 6f  function fts3_to
36ee7 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c  kenizer_internal
36ee8 5f 74 65 73 74 28 29 2e 0a 2a 2a 20 54 68 69 73  _test()..** This
36ee9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
36eea 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 6f 6e  d for testing on
36eeb 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 69 6e  ly, it is not in
36eec 63 6c 75 64 65 64 20 69 6e 20 74 68 65 0a 2a 2a  cluded in the.**
36eed 20 62 75 69 6c 64 20 75 6e 6c 65 73 73 20 53 51   build unless SQ
36eee 4c 49 54 45 5f 54 45 53 54 20 69 73 20 64 65 66  LITE_TEST is def
36eef 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ined..**.** The 
36ef0 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
36ef1 69 73 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  is to test that 
36ef2 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  the fts3_tokeniz
36ef3 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  er() function.**
36ef4 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20   can be used as 
36ef5 64 65 73 69 67 6e 65 64 20 62 79 20 74 68 65 20  designed by the 
36ef6 43 2d 63 6f 64 65 20 69 6e 20 74 68 65 20 71 75  C-code in the qu
36ef7 65 72 79 54 6f 6b 65 6e 69 7a 65 72 20 61 6e 64  eryTokenizer and
36ef8 0a 2a 2a 20 72 65 67 69 73 74 65 72 54 6f 6b 65  .** registerToke
36ef9 6e 69 7a 65 72 28 29 20 66 75 6e 63 74 69 6f 6e  nizer() function
36efa 73 20 61 62 6f 76 65 2e 20 54 68 65 73 65 20 74  s above. These t
36efb 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  wo functions are
36efc 20 72 65 70 65 61 74 65 64 0a 2a 2a 20 69 6e 20   repeated.** in 
36efd 74 68 65 20 52 45 41 44 4d 45 2e 74 6f 6b 65 6e  the README.token
36efe 69 7a 65 72 20 66 69 6c 65 20 61 73 20 61 6e 20  izer file as an 
36eff 65 78 61 6d 70 6c 65 2c 20 73 6f 20 69 74 20 69  example, so it i
36f00 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 0a 2a  s important to.*
36f01 2a 20 74 65 73 74 20 74 68 65 6d 2e 0a 2a 2a 0a  * test them..**.
36f02 2a 2a 20 54 6f 20 72 75 6e 20 74 68 65 20 74 65  ** To run the te
36f03 73 74 73 2c 20 65 76 61 6c 75 61 74 65 20 74 68  sts, evaluate th
36f04 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  e fts3_tokenizer
36f05 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28 29  _internal_test()
36f06 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74   scalar.** funct
36f07 69 6f 6e 20 77 69 74 68 20 6e 6f 20 61 72 67 75  ion with no argu
36f08 6d 65 6e 74 73 2e 20 41 6e 20 61 73 73 65 72 74  ments. An assert
36f09 28 29 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 20  () will fail if 
36f0a 61 20 70 72 6f 62 6c 65 6d 20 69 73 0a 2a 2a 20  a problem is.** 
36f0b 64 65 74 65 63 74 65 64 2e 20 69 2e 65 2e 3a 0a  detected. i.e.:.
36f0c 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
36f0d 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f   fts3_tokenizer_
36f0e 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28 29 3b  internal_test();
36f0f 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  .**.*/.static vo
36f10 69 64 20 69 6e 74 54 65 73 74 46 75 6e 63 28 0a  id intTestFunc(.
36f11 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
36f12 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
36f13 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
36f14 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
36f15 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 6f  {.  int rc;.  co
36f16 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
36f17 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 31  nizer_module *p1
36f18 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
36f19 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
36f1a 6c 65 20 2a 70 32 3b 0a 20 20 73 71 6c 69 74 65  le *p2;.  sqlite
36f1b 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33  3 *db = (sqlite3
36f1c 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f   *)sqlite3_user_
36f1d 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a  data(context);..
36f1e 20 20 2f 2a 20 54 65 73 74 20 74 68 65 20 71 75    /* Test the qu
36f1f 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ery function */.
36f20 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d    sqlite3Fts3Sim
36f21 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  pleTokenizerModu
36f22 6c 65 28 26 70 31 29 3b 0a 20 20 72 63 20 3d 20  le(&p1);.  rc = 
36f23 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64  queryTokenizer(d
36f24 62 2c 20 22 73 69 6d 70 6c 65 22 2c 20 26 70 32  b, "simple", &p2
36f25 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
36f26 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
36f27 61 73 73 65 72 74 28 20 70 31 3d 3d 70 32 20 29  assert( p1==p2 )
36f28 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f  ;.  rc = queryTo
36f29 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73  kenizer(db, "nos
36f2a 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 26  uchtokenizer", &
36f2b 70 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  p2);.  assert( r
36f2c 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  c==SQLITE_ERROR 
36f2d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 32 3d  );.  assert( p2=
36f2e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
36f2f 30 3d 3d 73 74 72 63 6d 70 28 73 71 6c 69 74 65  0==strcmp(sqlite
36f30 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 22 75  3_errmsg(db), "u
36f31 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72  nknown tokenizer
36f32 3a 20 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65  : nosuchtokenize
36f33 72 22 29 20 29 3b 0a 0a 20 20 2f 2a 20 54 65 73  r") );..  /* Tes
36f34 74 20 74 68 65 20 73 74 6f 72 61 67 65 20 66 75  t the storage fu
36f35 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d  nction */.  rc =
36f36 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a   registerTokeniz
36f37 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f  er(db, "nosuchto
36f38 6b 65 6e 69 7a 65 72 22 2c 20 70 31 29 3b 0a 20  kenizer", p1);. 
36f39 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
36f3a 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d  ITE_OK );.  rc =
36f3b 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28   queryTokenizer(
36f3c 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e  db, "nosuchtoken
36f3d 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a 20 20 61  izer", &p2);.  a
36f3e 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
36f3f 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  E_OK );.  assert
36f40 28 20 70 32 3d 3d 70 31 20 29 3b 0a 0a 20 20 73  ( p2==p1 );..  s
36f41 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
36f42 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 6f 6b 22  xt(context, "ok"
36f43 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
36f44 54 49 43 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a  TIC);.}..#endif.
36f45 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 53 51  ./*.** Set up SQ
36f46 4c 20 6f 62 6a 65 63 74 73 20 69 6e 20 64 61 74  L objects in dat
36f47 61 62 61 73 65 20 64 62 20 75 73 65 64 20 74 6f  abase db used to
36f48 20 61 63 63 65 73 73 20 74 68 65 20 63 6f 6e 74   access the cont
36f49 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20 68  ents of.** the h
36f4a 61 73 68 20 74 61 62 6c 65 20 70 6f 69 6e 74 65  ash table pointe
36f4b 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e 74  d to by argument
36f4c 20 70 48 61 73 68 2e 20 54 68 65 20 68 61 73 68   pHash. The hash
36f4d 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a 20 62   table must.** b
36f4e 65 65 6e 20 69 6e 69 74 69 61 6c 69 73 65 64 20  een initialised 
36f4f 74 6f 20 75 73 65 20 73 74 72 69 6e 67 20 6b 65  to use string ke
36f50 79 73 2c 20 61 6e 64 20 74 6f 20 74 61 6b 65 20  ys, and to take 
36f51 61 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 0a  a private copy .
36f52 2a 2a 20 6f 66 20 74 68 65 20 6b 65 79 20 77 68  ** of the key wh
36f53 65 6e 20 61 20 76 61 6c 75 65 20 69 73 20 69 6e  en a value is in
36f54 73 65 72 74 65 64 2e 20 69 2e 65 2e 20 62 79 20  serted. i.e. by 
36f55 61 20 63 61 6c 6c 20 73 69 6d 69 6c 61 72 20 74  a call similar t
36f56 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69  o:.**.**    sqli
36f57 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 28  te3Fts3HashInit(
36f58 70 48 61 73 68 2c 20 46 54 53 33 5f 48 41 53 48  pHash, FTS3_HASH
36f59 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 2a 2a 0a  _STRING, 1);.**.
36f5a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
36f5b 20 61 64 64 73 20 61 20 73 63 61 6c 61 72 20 66   adds a scalar f
36f5c 75 6e 63 74 69 6f 6e 20 28 73 65 65 20 68 65 61  unction (see hea
36f5d 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76  der comment abov
36f5e 65 0a 2a 2a 20 73 63 61 6c 61 72 46 75 6e 63 28  e.** scalarFunc(
36f5f 29 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 66  ) in this file f
36f60 6f 72 20 64 65 74 61 69 6c 73 29 20 61 6e 64 2c  or details) and,
36f61 20 69 66 20 45 4e 41 42 4c 45 5f 54 41 42 4c 45   if ENABLE_TABLE
36f62 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61   is.** defined a
36f63 74 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 74 69  t compilation ti
36f64 6d 65 2c 20 61 20 74 65 6d 70 6f 72 61 72 79 20  me, a temporary 
36f65 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 28 73  virtual table (s
36f66 65 65 20 68 65 61 64 65 72 20 0a 2a 2a 20 63 6f  ee header .** co
36f67 6d 6d 65 6e 74 20 61 62 6f 76 65 20 73 74 72 75  mment above stru
36f68 63 74 20 48 61 73 68 54 61 62 6c 65 56 74 61 62  ct HashTableVtab
36f69 29 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  ) to the databas
36f6a 65 20 73 63 68 65 6d 61 2e 20 42 6f 74 68 20 0a  e schema. Both .
36f6b 2a 2a 20 70 72 6f 76 69 64 65 20 72 65 61 64 2f  ** provide read/
36f6c 77 72 69 74 65 20 61 63 63 65 73 73 20 74 6f 20  write access to 
36f6d 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
36f6e 2a 70 48 61 73 68 2e 0a 2a 2a 0a 2a 2a 20 54 68  *pHash..**.** Th
36f6f 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
36f70 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
36f71 6e 2c 20 7a 4e 61 6d 65 2c 20 69 73 20 75 73 65  n, zName, is use
36f72 64 20 61 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a  d as the name.**
36f73 20 6f 66 20 62 6f 74 68 20 74 68 65 20 73 63 61   of both the sca
36f74 6c 61 72 20 61 6e 64 2c 20 69 66 20 63 72 65 61  lar and, if crea
36f75 74 65 64 2c 20 74 68 65 20 76 69 72 74 75 61 6c  ted, the virtual
36f76 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
36f77 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
36f78 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73  lite3Fts3InitHas
36f79 68 54 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65  hTable(.  sqlite
36f7a 33 20 2a 64 62 2c 20 0a 20 20 46 74 73 33 48 61  3 *db, .  Fts3Ha
36f7b 73 68 20 2a 70 48 61 73 68 2c 20 0a 20 20 63 6f  sh *pHash, .  co
36f7c 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 0a  nst char *zName.
36f7d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
36f7e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 76 6f 69 64 20  LITE_OK;.  void 
36f7f 2a 70 20 3d 20 28 76 6f 69 64 20 2a 29 70 48 61  *p = (void *)pHa
36f80 73 68 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  sh;.  const int 
36f81 61 6e 79 20 3d 20 53 51 4c 49 54 45 5f 41 4e 59  any = SQLITE_ANY
36f82 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 73 74 20  ;.  char *zTest 
36f83 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65  = 0;.  char *zTe
36f84 73 74 32 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66  st2 = 0;..#ifdef
36f85 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 76   SQLITE_TEST.  v
36f86 6f 69 64 20 2a 70 64 62 20 3d 20 28 76 6f 69 64  oid *pdb = (void
36f87 20 2a 29 64 62 3b 0a 20 20 7a 54 65 73 74 20 3d   *)db;.  zTest =
36f88 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
36f89 28 22 25 73 5f 74 65 73 74 22 2c 20 7a 4e 61 6d  ("%s_test", zNam
36f8a 65 29 3b 0a 20 20 7a 54 65 73 74 32 20 3d 20 73  e);.  zTest2 = s
36f8b 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
36f8c 25 73 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74  %s_internal_test
36f8d 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  ", zName);.  if(
36f8e 20 21 7a 54 65 73 74 20 7c 7c 20 21 7a 54 65 73   !zTest || !zTes
36f8f 74 32 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  t2 ){.    rc = S
36f90 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
36f91 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 72  .#endif..  if( r
36f92 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20  c!=SQLITE_OK.   
36f93 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  || (rc = sqlite3
36f94 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
36f95 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 31 2c 20 61  (db, zName, 1, a
36f96 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72 46 75 6e  ny, p, scalarFun
36f97 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c 20  c, 0, 0)).   || 
36f98 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72  (rc = sqlite3_cr
36f99 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62  eate_function(db
36f9a 2c 20 7a 4e 61 6d 65 2c 20 32 2c 20 61 6e 79 2c  , zName, 2, any,
36f9b 20 70 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c 20   p, scalarFunc, 
36f9c 30 2c 20 30 29 29 0a 23 69 66 64 65 66 20 53 51  0, 0)).#ifdef SQ
36f9d 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 7c 7c 20  LITE_TEST.   || 
36f9e 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72  (rc = sqlite3_cr
36f9f 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62  eate_function(db
36fa0 2c 20 7a 54 65 73 74 2c 20 32 2c 20 61 6e 79 2c  , zTest, 2, any,
36fa1 20 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30 2c   p, testFunc, 0,
36fa2 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20 3d   0)).   || (rc =
36fa3 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
36fa4 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65  function(db, zTe
36fa5 73 74 2c 20 33 2c 20 61 6e 79 2c 20 70 2c 20 74  st, 3, any, p, t
36fa6 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a  estFunc, 0, 0)).
36fa7 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
36fa8 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
36fa9 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 32 2c 20  ion(db, zTest2, 
36faa 30 2c 20 61 6e 79 2c 20 70 64 62 2c 20 69 6e 74  0, any, pdb, int
36fab 54 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29  TestFunc, 0, 0))
36fac 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 0a 20 20  .#endif.  );..  
36fad 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65  sqlite3_free(zTe
36fae 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  st);.  sqlite3_f
36faf 72 65 65 28 7a 54 65 73 74 32 29 3b 0a 20 20 72  ree(zTest2);.  r
36fb0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e  eturn rc;.}..#en
36fb1 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
36fb2 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
36fb3 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
36fb4 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
36fb5 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
36fb6 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65  End of fts3_toke
36fb7 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  nizer.c ********
36fb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36fb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
36fba 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
36fbb 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
36fbc 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a  tokenizer1.c ***
36fbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36fbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
36fbf 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20 31  /*.** 2006 Oct 1
36fc0 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  0.**.** The auth
36fc1 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
36fc2 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
36fc3 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
36fc4 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
36fc5 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
36fc6 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
36fc7 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
36fc8 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
36fc9 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
36fca 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
36fcb 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
36fcc 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
36fcd 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
36fce 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
36fcf 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
36fd0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
36fd1 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
36fd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36fd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36fd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36fd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36fd6 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c  *****.**.** Impl
36fd7 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
36fd8 65 20 22 73 69 6d 70 6c 65 22 20 66 75 6c 6c 2d  e "simple" full-
36fd9 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65  text-search toke
36fda 6e 69 7a 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  nizer..*/../*.**
36fdb 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
36fdc 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
36fdd 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
36fde 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
36fdf 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
36fe0 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
36fe1 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
36fe2 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
36fe3 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
36fe4 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
36fe5 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
36fe6 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
36fe7 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
36fe8 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
36fe9 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
36fea 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
36feb 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
36fec 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66  defined)..*/.#if
36fed 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
36fee 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
36fef 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
36ff0 46 54 53 33 29 0a 0a 0a 0a 0a 74 79 70 65 64 65  FTS3).....typede
36ff1 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65 5f  f struct simple_
36ff2 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71  tokenizer {.  sq
36ff3 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
36ff4 62 61 73 65 3b 0a 20 20 63 68 61 72 20 64 65 6c  base;.  char del
36ff5 69 6d 5b 31 32 38 5d 3b 20 20 20 20 20 20 20 20  im[128];        
36ff6 20 20 20 20 20 2f 2a 20 66 6c 61 67 20 41 53 43       /* flag ASC
36ff7 49 49 20 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f  II delimiters */
36ff8 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .} simple_tokeni
36ff9 7a 65 72 3b 0a 0a 74 79 70 65 64 65 66 20 73 74  zer;..typedef st
36ffa 72 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ruct simple_toke
36ffb 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20  nizer_cursor {. 
36ffc 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
36ffd 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 0a  er_cursor base;.
36ffe 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 49    const char *pI
36fff 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20 2f  nput;          /
37000 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20 74  * input we are t
37001 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69  okenizing */.  i
37002 6e 74 20 6e 42 79 74 65 73 3b 20 20 20 20 20 20  nt nBytes;      
37003 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
37004 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74  ize of the input
37005 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65   */.  int iOffse
37006 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
37007 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f     /* current po
37008 73 69 74 69 6f 6e 20 69 6e 20 70 49 6e 70 75 74  sition in pInput
37009 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e   */.  int iToken
3700a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3700b 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e     /* index of n
3700c 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20  ext token to be 
3700d 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68  returned */.  ch
3700e 61 72 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20  ar *pToken;     
3700f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
37010 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65 6e  orage for curren
37011 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  t token */.  int
37012 20 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64   nTokenAllocated
37013 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70 61  ;         /* spa
37014 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  ce allocated to 
37015 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f  zToken buffer */
37016 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .} simple_tokeni
37017 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a  zer_cursor;.../*
37018 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
37019 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 63  tion */.static c
3701a 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
3701b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69  enizer_module si
3701c 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
3701d 75 6c 65 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  ule;..static int
3701e 20 73 69 6d 70 6c 65 44 65 6c 69 6d 28 73 69 6d   simpleDelim(sim
3701f 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74  ple_tokenizer *t
37020 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  , unsigned char 
37021 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 3c 30  c){.  return c<0
37022 78 38 30 20 26 26 20 74 2d 3e 64 65 6c 69 6d 5b  x80 && t->delim[
37023 63 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  c];.}../*.** Cre
37024 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69  ate a new tokeni
37025 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f  zer instance..*/
37026 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70  .static int simp
37027 6c 65 43 72 65 61 74 65 28 0a 20 20 69 6e 74 20  leCreate(.  int 
37028 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
37029 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a   * const *argv,.
3702a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3702b 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65  zer **ppTokenize
3702c 72 0a 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f  r.){.  simple_to
3702d 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a 0a 20 20 74  kenizer *t;..  t
3702e 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e   = (simple_token
3702f 69 7a 65 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  izer *) sqlite3_
37030 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74  malloc(sizeof(*t
37031 29 29 3b 0a 20 20 69 66 28 20 74 3d 3d 4e 55 4c  ));.  if( t==NUL
37032 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
37033 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65  E_NOMEM;.  memse
37034 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  t(t, 0, sizeof(*
37035 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28  t));..  /* TODO(
37036 73 68 65 73 73 29 20 44 65 6c 69 6d 69 74 65 72  shess) Delimiter
37037 73 20 6e 65 65 64 20 74 6f 20 72 65 6d 61 69 6e  s need to remain
37038 20 74 68 65 20 73 61 6d 65 20 66 72 6f 6d 20 72   the same from r
37039 75 6e 20 74 6f 20 72 75 6e 2c 0a 20 20 2a 2a 20  un to run,.  ** 
3703a 65 6c 73 65 20 77 65 20 6e 65 65 64 20 74 6f 20  else we need to 
3703b 72 65 69 6e 64 65 78 2e 20 20 4f 6e 65 20 73 6f  reindex.  One so
3703c 6c 75 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20  lution would be 
3703d 61 20 6d 65 74 61 2d 74 61 62 6c 65 20 74 6f 0a  a meta-table to.
3703e 20 20 2a 2a 20 74 72 61 63 6b 20 73 75 63 68 20    ** track such 
3703f 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74  information in t
37040 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  he database, the
37041 6e 20 77 65 27 64 20 6f 6e 6c 79 20 77 61 6e 74  n we'd only want
37042 20 74 68 69 73 0a 20 20 2a 2a 20 69 6e 66 6f 72   this.  ** infor
37043 6d 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e  mation on the in
37044 69 74 69 61 6c 20 63 72 65 61 74 65 2e 0a 20 20  itial create..  
37045 2a 2f 0a 20 20 69 66 28 20 61 72 67 63 3e 31 20  */.  if( argc>1 
37046 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e 20  ){.    int i, n 
37047 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d  = strlen(argv[1]
37048 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
37049 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<n; i++){.     
3704a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
3704b 68 20 3d 20 61 72 67 76 5b 31 5d 5b 69 5d 3b 0a  h = argv[1][i];.
3704c 20 20 20 20 20 20 2f 2a 20 57 65 20 65 78 70 6c        /* We expl
3704d 69 63 69 74 6c 79 20 64 6f 6e 27 74 20 73 75 70  icitly don't sup
3704e 70 6f 72 74 20 55 54 46 2d 38 20 64 65 6c 69 6d  port UTF-8 delim
3704f 69 74 65 72 73 20 66 6f 72 20 6e 6f 77 2e 20 2a  iters for now. *
37050 2f 0a 20 20 20 20 20 20 69 66 28 20 63 68 3e 3d  /.      if( ch>=
37051 30 78 38 30 20 29 7b 0a 20 20 20 20 20 20 20 20  0x80 ){.        
37052 73 71 6c 69 74 65 33 5f 66 72 65 65 28 74 29 3b  sqlite3_free(t);
37053 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
37054 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
37055 20 20 20 20 7d 0a 20 20 20 20 20 20 74 2d 3e 64      }.      t->d
37056 65 6c 69 6d 5b 63 68 5d 20 3d 20 31 3b 0a 20 20  elim[ch] = 1;.  
37057 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20    }.  } else {. 
37058 20 20 20 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d 61     /* Mark non-a
37059 6c 70 68 61 6e 75 6d 65 72 69 63 20 41 53 43 49  lphanumeric ASCI
3705a 49 20 63 68 61 72 61 63 74 65 72 73 20 61 73 20  I characters as 
3705b 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f 0a 20 20  delimiters */.  
3705c 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
3705d 28 69 3d 31 3b 20 69 3c 30 78 38 30 3b 20 69 2b  (i=1; i<0x80; i+
3705e 2b 29 7b 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c  +){.      t->del
3705f 69 6d 5b 69 5d 20 3d 20 21 69 73 61 6c 6e 75 6d  im[i] = !isalnum
37060 28 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  (i);.    }.  }..
37061 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d    *ppTokenizer =
37062 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74   &t->base;.  ret
37063 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
37064 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20  ../*.** Destroy 
37065 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73  a tokenizer.*/.s
37066 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65  tatic int simple
37067 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f  Destroy(sqlite3_
37068 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
37069 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c 69 74 65  nizer){.  sqlite
3706a 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e 69 7a 65  3_free(pTokenize
3706b 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  r);.  return SQL
3706c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3706d 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67 69   Prepare to begi
3706e 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70  n tokenizing a p
3706f 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e 67  articular string
37070 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20  .  The input.** 
37071 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b  string to be tok
37072 65 6e 69 7a 65 64 20 69 73 20 70 49 6e 70 75 74  enized is pInput
37073 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e 20 20  [0..nBytes-1].  
37074 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64  A cursor.** used
37075 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c   to incrementall
37076 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20  y tokenize this 
37077 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e  string is return
37078 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72  ed in .** *ppCur
37079 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  sor..*/.static i
3707a 6e 74 20 73 69 6d 70 6c 65 4f 70 65 6e 28 0a 20  nt simpleOpen(. 
3707b 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3707c 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er *pTokenizer, 
3707d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
3707e 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f  okenizer */.  co
3707f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74  nst char *pInput
37080 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20  , int nBytes,   
37081 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 74       /* String t
37082 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 2a  o be tokenized *
37083 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
37084 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70  nizer_cursor **p
37085 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f 55  pCursor    /* OU
37086 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20  T: Tokenization 
37087 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 73  cursor */.){.  s
37088 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
37089 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20 63 20  cursor *c;..  c 
3708a 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  = (simple_tokeni
3708b 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 73 71  zer_cursor *) sq
3708c 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
3708d 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69 66 28 20  eof(*c));.  if( 
3708e 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  c==NULL ) return
3708f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a   SQLITE_NOMEM;..
37090 20 20 63 2d 3e 70 49 6e 70 75 74 20 3d 20 70 49    c->pInput = pI
37091 6e 70 75 74 3b 0a 20 20 69 66 28 20 70 49 6e 70  nput;.  if( pInp
37092 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 63 2d 3e  ut==0 ){.    c->
37093 6e 42 79 74 65 73 20 3d 20 30 3b 0a 20 20 7d 65  nBytes = 0;.  }e
37094 6c 73 65 20 69 66 28 20 6e 42 79 74 65 73 3c 30  lse if( nBytes<0
37095 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65   ){.    c->nByte
37096 73 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  s = (int)strlen(
37097 70 49 6e 70 75 74 29 3b 0a 20 20 7d 65 6c 73 65  pInput);.  }else
37098 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65 73 20  {.    c->nBytes 
37099 3d 20 6e 42 79 74 65 73 3b 0a 20 20 7d 0a 20 20  = nBytes;.  }.  
3709a 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 20  c->iOffset = 0; 
3709b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3709c 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65 6e 69 7a  /* start tokeniz
3709d 69 6e 67 20 61 74 20 74 68 65 20 62 65 67 69 6e  ing at the begin
3709e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e 69 54 6f  ning */.  c->iTo
3709f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d 3e 70 54  ken = 0;.  c->pT
370a0 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  oken = NULL;    
370a1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f             /* no
370a2 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
370a3 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63 2d 3e 6e  , yet. */.  c->n
370a4 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 3d  TokenAllocated =
370a5 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72   0;..  *ppCursor
370a6 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72   = &c->base;.  r
370a7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
370a8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
370a9 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
370aa 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79  ursor previously
370ab 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61 6c   opened by a cal
370ac 6c 20 74 6f 0a 2a 2a 20 73 69 6d 70 6c 65 4f 70  l to.** simpleOp
370ad 65 6e 28 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73  en() above..*/.s
370ae 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65  tatic int simple
370af 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 74 6f  Close(sqlite3_to
370b0 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
370b1 70 43 75 72 73 6f 72 29 7b 0a 20 20 73 69 6d 70  pCursor){.  simp
370b2 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  le_tokenizer_cur
370b3 73 6f 72 20 2a 63 20 3d 20 28 73 69 6d 70 6c 65  sor *c = (simple
370b4 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
370b5 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
370b6 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 2d 3e  sqlite3_free(c->
370b7 70 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74  pToken);.  sqlit
370b8 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20 72 65  e3_free(c);.  re
370b9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
370ba 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
370bb 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20   the next token 
370bc 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74  from a tokenizat
370bd 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20 54 68 65  ion cursor.  The
370be 20 63 75 72 73 6f 72 20 6d 75 73 74 0a 2a 2a 20   cursor must.** 
370bf 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64  have been opened
370c0 20 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c   by a prior call
370c1 20 74 6f 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29   to simpleOpen()
370c2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
370c3 73 69 6d 70 6c 65 4e 65 78 74 28 0a 20 20 73 71  simpleNext(.  sq
370c4 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
370c5 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
370c6 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74 75    /* Cursor retu
370c7 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f 70  rned by simpleOp
370c8 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  en */.  const ch
370c9 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20 20  ar **ppToken,   
370ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
370cb 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20  UT: *ppToken is 
370cc 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a  the token text *
370cd 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 73  /.  int *pnBytes
370ce 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
370cf 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
370d0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
370d1 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  in token */.  in
370d2 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74  t *piStartOffset
370d3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
370d4 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74 69    /* OUT: Starti
370d5 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  ng offset of tok
370d6 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45  en */.  int *piE
370d7 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20  ndOffset,       
370d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
370d9 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73 65  UT: Ending offse
370da 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  t of token */.  
370db 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20  int *piPosition 
370dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370dd 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69      /* OUT: Posi
370de 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66 20  tion integer of 
370df 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 73 69  token */.){.  si
370e0 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  mple_tokenizer_c
370e1 75 72 73 6f 72 20 2a 63 20 3d 20 28 73 69 6d 70  ursor *c = (simp
370e2 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  le_tokenizer_cur
370e3 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
370e4 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a    simple_tokeniz
370e5 65 72 20 2a 74 20 3d 20 28 73 69 6d 70 6c 65 5f  er *t = (simple_
370e6 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 70 43 75  tokenizer *) pCu
370e7 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  rsor->pTokenizer
370e8 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
370e9 72 20 2a 70 20 3d 20 28 75 6e 73 69 67 6e 65 64  r *p = (unsigned
370ea 20 63 68 61 72 20 2a 29 63 2d 3e 70 49 6e 70 75   char *)c->pInpu
370eb 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e  t;..  while( c->
370ec 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65  iOffset<c->nByte
370ed 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 53 74  s ){.    int iSt
370ee 61 72 74 4f 66 66 73 65 74 3b 0a 0a 20 20 20 20  artOffset;..    
370ef 2f 2a 20 53 63 61 6e 20 70 61 73 74 20 64 65 6c  /* Scan past del
370f0 69 6d 69 74 65 72 20 63 68 61 72 61 63 74 65 72  imiter character
370f1 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  s */.    while( 
370f2 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42  c->iOffset<c->nB
370f3 79 74 65 73 20 26 26 20 73 69 6d 70 6c 65 44 65  ytes && simpleDe
370f4 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66  lim(t, p[c->iOff
370f5 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63  set]) ){.      c
370f6 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20  ->iOffset++;.   
370f7 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 75 6e 74   }..    /* Count
370f8 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65 72 20 63   non-delimiter c
370f9 68 61 72 61 63 74 65 72 73 2e 20 2a 2f 0a 20 20  haracters. */.  
370fa 20 20 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d    iStartOffset =
370fb 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20   c->iOffset;.   
370fc 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73   while( c->iOffs
370fd 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 26 26 20  et<c->nBytes && 
370fe 21 73 69 6d 70 6c 65 44 65 6c 69 6d 28 74 2c 20  !simpleDelim(t, 
370ff 70 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29  p[c->iOffset]) )
37100 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73  {.      c->iOffs
37101 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  et++;.    }..   
37102 20 69 66 28 20 63 2d 3e 69 4f 66 66 73 65 74 3e   if( c->iOffset>
37103 69 53 74 61 72 74 4f 66 66 73 65 74 20 29 7b 0a  iStartOffset ){.
37104 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6e 20 3d        int i, n =
37105 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53 74 61   c->iOffset-iSta
37106 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  rtOffset;.      
37107 69 66 28 20 6e 3e 63 2d 3e 6e 54 6f 6b 65 6e 41  if( n>c->nTokenA
37108 6c 6c 6f 63 61 74 65 64 20 29 7b 0a 20 20 20 20  llocated ){.    
37109 20 20 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c      c->nTokenAll
3710a 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a 20  ocated = n+20;. 
3710b 20 20 20 20 20 20 20 63 2d 3e 70 54 6f 6b 65 6e         c->pToken
3710c 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
3710d 6f 63 28 63 2d 3e 70 54 6f 6b 65 6e 2c 20 63 2d  oc(c->pToken, c-
3710e 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64  >nTokenAllocated
3710f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  );.        if( c
37110 2d 3e 70 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20 29  ->pToken==NULL )
37111 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
37112 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
37113 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
37114 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
37115 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54  /* TODO(shess) T
37116 68 69 73 20 6e 65 65 64 73 20 65 78 70 61 6e 73  his needs expans
37117 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65 20 55 54  ion to handle UT
37118 46 2d 38 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  F-8.        ** c
37119 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 69 74  ase-insensitivit
3711a 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  y..        */.  
3711b 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63        unsigned c
3711c 68 61 72 20 63 68 20 3d 20 70 5b 69 53 74 61 72  har ch = p[iStar
3711d 74 4f 66 66 73 65 74 2b 69 5d 3b 0a 20 20 20 20  tOffset+i];.    
3711e 20 20 20 20 63 2d 3e 70 54 6f 6b 65 6e 5b 69 5d      c->pToken[i]
3711f 20 3d 20 63 68 3c 30 78 38 30 20 3f 20 74 6f 6c   = ch<0x80 ? tol
37120 6f 77 65 72 28 63 68 29 20 3a 20 63 68 3b 0a 20  ower(ch) : ch;. 
37121 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 70       }.      *pp
37122 54 6f 6b 65 6e 20 3d 20 63 2d 3e 70 54 6f 6b 65  Token = c->pToke
37123 6e 3b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 65  n;.      *pnByte
37124 73 20 3d 20 6e 3b 0a 20 20 20 20 20 20 2a 70 69  s = n;.      *pi
37125 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53  StartOffset = iS
37126 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20  tartOffset;.    
37127 20 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d    *piEndOffset =
37128 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20   c->iOffset;.   
37129 20 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d     *piPosition =
3712a 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20   c->iToken++;.. 
3712b 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
3712c 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
3712d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3712e 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  _DONE;.}../*.** 
3712f 54 68 65 20 73 65 74 20 6f 66 20 72 6f 75 74 69  The set of routi
37130 6e 65 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  nes that impleme
37131 6e 74 20 74 68 65 20 73 69 6d 70 6c 65 20 74 6f  nt the simple to
37132 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69  kenizer.*/.stati
37133 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
37134 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
37135 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72   simpleTokenizer
37136 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a  Module = {.  0,.
37137 20 20 73 69 6d 70 6c 65 43 72 65 61 74 65 2c 0a    simpleCreate,.
37138 20 20 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 2c    simpleDestroy,
37139 0a 20 20 73 69 6d 70 6c 65 4f 70 65 6e 2c 0a 20  .  simpleOpen,. 
3713a 20 73 69 6d 70 6c 65 43 6c 6f 73 65 2c 0a 20 20   simpleClose,.  
3713b 73 69 6d 70 6c 65 4e 65 78 74 2c 0a 7d 3b 0a 0a  simpleNext,.};..
3713c 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
3713d 20 6e 65 77 20 73 69 6d 70 6c 65 20 74 6f 6b 65   new simple toke
3713e 6e 69 7a 65 72 2e 20 20 52 65 74 75 72 6e 20 61  nizer.  Return a
3713f 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
37140 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72  new.** tokenizer
37141 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f   in *ppModule.*/
37142 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
37143 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
37144 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d  SimpleTokenizerM
37145 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  odule(.  sqlite3
37146 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
37147 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c  e const**ppModul
37148 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65  e.){.  *ppModule
37149 20 3d 20 26 73 69 6d 70 6c 65 54 6f 6b 65 6e 69   = &simpleTokeni
3714a 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65  zerModule;.}..#e
3714b 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
3714c 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
3714d 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3714e 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
3714f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
37150 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b   End of fts3_tok
37151 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a  enizer1.c ******
37152 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37153 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
37154 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
37155 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
37156 5f 77 72 69 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  _write.c *******
37157 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
37159 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4f 63 74 20  ./*.** 2009 Oct 
3715a 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  23.**.** The aut
3715b 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
3715c 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
3715d 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
3715e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
3715f 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
37160 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
37161 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
37162 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
37163 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
37164 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
37165 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
37166 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
37167 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
37168 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
37169 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
3716a 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
3716b 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
3716c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3716d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3716e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3716f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37170 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
37171 73 20 66 69 6c 65 20 69 73 20 70 61 72 74 20 6f  s file is part o
37172 66 20 74 68 65 20 53 51 4c 69 74 65 20 46 54 53  f the SQLite FTS
37173 33 20 65 78 74 65 6e 73 69 6f 6e 20 6d 6f 64 75  3 extension modu
37174 6c 65 2e 20 53 70 65 63 69 66 69 63 61 6c 6c 79  le. Specifically
37175 2c 0a 2a 2a 20 74 68 69 73 20 66 69 6c 65 20 63  ,.** this file c
37176 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20  ontains code to 
37177 69 6e 73 65 72 74 2c 20 75 70 64 61 74 65 20 61  insert, update a
37178 6e 64 20 64 65 6c 65 74 65 20 72 6f 77 73 20 66  nd delete rows f
37179 72 6f 6d 20 46 54 53 33 0a 2a 2a 20 74 61 62 6c  rom FTS3.** tabl
3717a 65 73 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74  es. It also cont
3717b 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 6d 65 72  ains code to mer
3717c 67 65 20 46 54 53 33 20 62 2d 74 72 65 65 20 73  ge FTS3 b-tree s
3717d 65 67 6d 65 6e 74 73 2e 20 53 6f 6d 65 0a 2a 2a  egments. Some.**
3717e 20 6f 66 20 74 68 65 20 73 75 62 2d 72 6f 75 74   of the sub-rout
3717f 69 6e 65 73 20 75 73 65 64 20 74 6f 20 6d 65 72  ines used to mer
37180 67 65 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20  ge segments are 
37181 61 6c 73 6f 20 75 73 65 64 20 62 79 20 74 68 65  also used by the
37182 20 71 75 65 72 79 20 0a 2a 2a 20 63 6f 64 65 20   query .** code 
37183 69 6e 20 66 74 73 33 2e 63 2e 0a 2a 2f 0a 0a 23  in fts3.c..*/..#
37184 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
37185 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
37186 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
37187 45 5f 46 54 53 33 29 0a 0a 0a 23 64 65 66 69 6e  E_FTS3)...#defin
37188 65 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 32  e INTERIOR_MAX 2
37189 30 34 38 20 20 20 20 20 20 20 20 20 2f 2a 20 53  048         /* S
3718a 6f 66 74 20 6c 69 6d 69 74 20 66 6f 72 20 73 65  oft limit for se
3718b 67 6d 65 6e 74 20 6e 6f 64 65 20 73 69 7a 65 20  gment node size 
3718c 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 45 41 46 5f  */.#define LEAF_
3718d 4d 41 58 20 32 30 34 38 20 20 20 20 20 20 20 20  MAX 2048        
3718e 20 20 20 20 20 2f 2a 20 53 6f 66 74 20 6c 69 6d       /* Soft lim
3718f 69 74 20 66 6f 72 20 73 65 67 6d 65 6e 74 20 6c  it for segment l
37190 65 61 66 20 73 69 7a 65 20 2a 2f 0a 0a 74 79 70  eaf size */..typ
37191 65 64 65 66 20 73 74 72 75 63 74 20 50 65 6e 64  edef struct Pend
37192 69 6e 67 4c 69 73 74 20 50 65 6e 64 69 6e 67 4c  ingList PendingL
37193 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ist;.typedef str
37194 75 63 74 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20  uct SegmentNode 
37195 53 65 67 6d 65 6e 74 4e 6f 64 65 3b 0a 74 79 70  SegmentNode;.typ
37196 65 64 65 66 20 73 74 72 75 63 74 20 53 65 67 6d  edef struct Segm
37197 65 6e 74 57 72 69 74 65 72 20 53 65 67 6d 65 6e  entWriter Segmen
37198 74 57 72 69 74 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20  tWriter;../*.** 
37199 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20 75  Data structure u
3719a 73 65 64 20 77 68 69 6c 65 20 61 63 63 75 6d 75  sed while accumu
3719b 6c 61 74 69 6e 67 20 74 65 72 6d 73 20 69 6e 20  lating terms in 
3719c 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d  the pending-term
3719d 73 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 2e  s hash.** table.
3719e 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
3719f 65 6e 74 72 79 20 6d 61 70 73 20 66 72 6f 6d 20  entry maps from 
371a0 74 65 72 6d 20 28 61 20 73 74 72 69 6e 67 29 20  term (a string) 
371a1 74 6f 20 61 20 6d 61 6c 6c 6f 63 65 64 0a 2a 2a  to a malloced.**
371a2 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
371a3 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  s structure..*/.
371a4 73 74 72 75 63 74 20 50 65 6e 64 69 6e 67 4c 69  struct PendingLi
371a5 73 74 20 7b 0a 20 20 69 6e 74 20 6e 44 61 74 61  st {.  int nData
371a6 3b 0a 20 20 63 68 61 72 20 2a 61 44 61 74 61 3b  ;.  char *aData;
371a7 0a 20 20 69 6e 74 20 6e 53 70 61 63 65 3b 0a 20  .  int nSpace;. 
371a8 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
371a9 4c 61 73 74 44 6f 63 69 64 3b 0a 20 20 73 71 6c  LastDocid;.  sql
371aa 69 74 65 33 5f 69 6e 74 36 34 20 69 4c 61 73 74  ite3_int64 iLast
371ab 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  Col;.  sqlite3_i
371ac 6e 74 36 34 20 69 4c 61 73 74 50 6f 73 3b 0a 7d  nt64 iLastPos;.}
371ad 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
371ae 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
371af 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74  ucture is used t
371b0 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
371b1 68 20 74 68 65 20 74 65 72 6d 73 20 6f 6e 0a 2a  h the terms on.*
371b2 2a 20 61 20 63 6f 6e 74 69 67 75 6f 75 73 20 73  * a contiguous s
371b3 65 74 20 6f 66 20 73 65 67 6d 65 6e 74 20 62 2d  et of segment b-
371b4 74 72 65 65 20 6c 65 61 66 20 6e 6f 64 65 73 2e  tree leaf nodes.
371b5 20 41 6c 74 68 6f 75 67 68 20 74 68 65 20 64 65   Although the de
371b6 74 61 69 6c 73 20 6f 66 0a 2a 2a 20 74 68 69 73  tails of.** this
371b7 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 6f   structure are o
371b8 6e 6c 79 20 6d 61 6e 69 70 75 6c 61 74 65 64 20  nly manipulated 
371b9 62 79 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  by code in this 
371ba 66 69 6c 65 2c 20 6f 70 61 71 75 65 20 68 61 6e  file, opaque han
371bb 64 6c 65 73 0a 2a 2a 20 6f 66 20 74 79 70 65 20  dles.** of type 
371bc 46 74 73 33 53 65 67 52 65 61 64 65 72 2a 20 61  Fts3SegReader* a
371bd 72 65 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20  re also used by 
371be 63 6f 64 65 20 69 6e 20 66 74 73 33 2e 63 20 74  code in fts3.c t
371bf 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
371c0 68 0a 2a 2a 20 74 65 72 6d 73 20 77 68 65 6e 20  h.** terms when 
371c1 71 75 65 72 79 69 6e 67 20 74 68 65 20 66 75 6c  querying the ful
371c2 6c 2d 74 65 78 74 20 69 6e 64 65 78 2e 20 53 65  l-text index. Se
371c3 65 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a  e functions:.**.
371c4 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  **   sqlite3Fts3
371c5 53 65 67 52 65 61 64 65 72 4e 65 77 28 29 0a 2a  SegReaderNew().*
371c6 2a 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 53  *   sqlite3Fts3S
371c7 65 67 52 65 61 64 65 72 46 72 65 65 28 29 0a 2a  egReaderFree().*
371c8 2a 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 53  *   sqlite3Fts3S
371c9 65 67 52 65 61 64 65 72 49 74 65 72 61 74 65 28  egReaderIterate(
371ca 29 0a 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33  ).*/.struct Fts3
371cb 53 65 67 52 65 61 64 65 72 20 7b 0a 20 20 69 6e  SegReader {.  in
371cc 74 20 69 49 64 78 3b 20 20 20 20 20 20 20 20 20  t iIdx;         
371cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
371ce 20 49 6e 64 65 78 20 77 69 74 68 69 6e 20 6c 65   Index within le
371cf 76 65 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  vel */.  sqlite3
371d0 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
371d1 63 6b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  ck;.  sqlite3_in
371d2 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 3b 0a 20  t64 iEndBlock;. 
371d3 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
371d4 53 74 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20  Stmt;           
371d5 20 2f 2a 20 53 51 4c 20 53 74 61 74 65 6d 65 6e   /* SQL Statemen
371d6 74 20 74 6f 20 61 63 63 65 73 73 20 6c 65 61 66  t to access leaf
371d7 20 6e 6f 64 65 73 20 2a 2f 0a 20 20 63 68 61 72   nodes */.  char
371d8 20 2a 61 4e 6f 64 65 3b 20 20 20 20 20 20 20 20   *aNode;        
371d9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
371da 6f 69 6e 74 65 72 20 74 6f 20 6e 6f 64 65 20 64  ointer to node d
371db 61 74 61 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f  ata (or NULL) */
371dc 0a 20 20 69 6e 74 20 6e 4e 6f 64 65 3b 20 20 20  .  int nNode;   
371dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371de 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75     /* Size of bu
371df 66 66 65 72 20 61 74 20 61 4e 6f 64 65 20 28 6f  ffer at aNode (o
371e0 72 20 30 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  r 0) */.  int nT
371e1 65 72 6d 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20  ermAlloc;       
371e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
371e3 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 7a  ocated size of z
371e4 54 65 72 6d 20 62 75 66 66 65 72 20 2a 2f 0a 0a  Term buffer */..
371e5 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20 73    /* Variables s
371e6 65 74 20 62 79 20 66 74 73 33 53 65 67 52 65 61  et by fts3SegRea
371e7 64 65 72 4e 65 78 74 28 29 2e 20 54 68 65 73 65  derNext(). These
371e8 20 6d 61 79 20 62 65 20 72 65 61 64 20 64 69 72   may be read dir
371e9 65 63 74 6c 79 0a 20 20 2a 2a 20 62 79 20 74 68  ectly.  ** by th
371ea 65 20 63 61 6c 6c 65 72 2e 20 54 68 65 79 20 61  e caller. They a
371eb 72 65 20 76 61 6c 69 64 20 66 72 6f 6d 20 74 68  re valid from th
371ec 65 20 74 69 6d 65 20 53 65 67 6d 65 6e 74 52 65  e time SegmentRe
371ed 61 64 65 72 4e 65 77 28 29 20 72 65 74 75 72 6e  aderNew() return
371ee 73 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 53 65 67  s.  ** until Seg
371ef 6d 65 6e 74 52 65 61 64 65 72 4e 65 78 74 28 29  mentReaderNext()
371f0 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 68 69   returns somethi
371f1 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51  ng other than SQ
371f2 4c 49 54 45 5f 4f 4b 0a 20 20 2a 2a 20 28 69 2e  LITE_OK.  ** (i.
371f3 65 2e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 29 2e  e. SQLITE_DONE).
371f4 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  .  */.  int nTer
371f5 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
371f6 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
371f7 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 63 75  r of bytes in cu
371f8 72 72 65 6e 74 20 74 65 72 6d 20 2a 2f 0a 20 20  rrent term */.  
371f9 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20  char *zTerm;    
371fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371fb 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 75  /* Pointer to cu
371fc 72 72 65 6e 74 20 74 65 72 6d 20 2a 2f 0a 20 20  rrent term */.  
371fd 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74 3b 20  char *aDoclist; 
371fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371ff 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 6f  /* Pointer to do
37200 63 6c 69 73 74 20 6f 66 20 63 75 72 72 65 6e 74  clist of current
37201 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20   entry */.  int 
37202 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20  nDoclist;       
37203 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
37204 69 7a 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 69  ize of doclist i
37205 6e 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20  n current entry 
37206 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c  */..  /* The fol
37207 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73  lowing variables
37208 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 74 65   are used to ite
37209 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65  rate through the
3720a 20 63 75 72 72 65 6e 74 20 64 6f 63 6c 69 73 74   current doclist
3720b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 4f 66 66   */.  char *pOff
3720c 73 65 74 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74  setList;.  sqlit
3720d 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 3b  e3_int64 iDocid;
3720e 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
3720f 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
37210 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
37211 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 65 67   to create a seg
37212 6d 65 6e 74 20 62 2d 74 72 65 65 20 69 6e 20 74  ment b-tree in t
37213 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  he.** database. 
37214 54 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74  The internal det
37215 61 69 6c 73 20 6f 66 20 74 68 69 73 20 74 79 70  ails of this typ
37216 65 20 61 72 65 20 6f 6e 6c 79 20 61 63 63 65 73  e are only acces
37217 73 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 66 6f  sed by the.** fo
37218 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llowing function
37219 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73 33 53  s:.**.**   fts3S
3721a 65 67 57 72 69 74 65 72 41 64 64 28 29 0a 2a 2a  egWriterAdd().**
3721b 20 20 20 66 74 73 33 53 65 67 57 72 69 74 65 72     fts3SegWriter
3721c 46 6c 75 73 68 28 29 0a 2a 2a 20 20 20 66 74 73  Flush().**   fts
3721d 33 53 65 67 57 72 69 74 65 72 46 72 65 65 28 29  3SegWriterFree()
3721e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 65 67 6d 65  .*/.struct Segme
3721f 6e 74 57 72 69 74 65 72 20 7b 0a 20 20 53 65 67  ntWriter {.  Seg
37220 6d 65 6e 74 4e 6f 64 65 20 2a 70 54 72 65 65 3b  mentNode *pTree;
37221 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37222 50 6f 69 6e 74 65 72 20 74 6f 20 69 6e 74 65 72  Pointer to inter
37223 69 6f 72 20 74 72 65 65 20 73 74 72 75 63 74 75  ior tree structu
37224 72 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  re */.  sqlite3_
37225 69 6e 74 36 34 20 69 46 69 72 73 74 3b 20 20 20  int64 iFirst;   
37226 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
37227 20 73 6c 6f 74 20 69 6e 20 25 5f 73 65 67 6d 65   slot in %_segme
37228 6e 74 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20  nts written */. 
37229 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
3722a 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  Free;           
3722b 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 73 6c   /* Next free sl
3722c 6f 74 20 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73  ot in %_segments
3722d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 65 72   */.  char *zTer
3722e 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
3722f 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
37230 20 74 6f 20 70 72 65 76 69 6f 75 73 20 74 65 72   to previous ter
37231 6d 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  m buffer */.  in
37232 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20  t nTerm;        
37233 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37234 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
37235 20 69 6e 20 7a 54 65 72 6d 20 2a 2f 0a 20 20 69   in zTerm */.  i
37236 6e 74 20 6e 4d 61 6c 6c 6f 63 3b 20 20 20 20 20  nt nMalloc;     
37237 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37238 2a 20 53 69 7a 65 20 6f 66 20 6d 61 6c 6c 6f 63  * Size of malloc
37239 27 64 20 62 75 66 66 65 72 20 61 74 20 7a 4d 61  'd buffer at zMa
3723a 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a  lloc */.  char *
3723b 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20  zMalloc;        
3723c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c            /* Mal
3723d 6c 6f 63 27 64 20 73 70 61 63 65 20 28 70 6f 73  loc'd space (pos
3723e 73 69 62 6c 79 29 20 75 73 65 64 20 66 6f 72 20  sibly) used for 
3723f 7a 54 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  zTerm */.  int n
37240 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
37241 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
37242 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  ze of allocation
37243 20 61 74 20 61 44 61 74 61 20 2a 2f 0a 20 20 69   at aData */.  i
37244 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20  nt nData;       
37245 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37246 2a 20 42 79 74 65 73 20 6f 66 20 64 61 74 61 20  * Bytes of data 
37247 69 6e 20 61 44 61 74 61 20 2a 2f 0a 20 20 63 68  in aData */.  ch
37248 61 72 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20  ar *aData;      
37249 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3724a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 6c 6f 63   Pointer to bloc
3724b 6b 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 20  k from malloc() 
3724c 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 79 70  */.};../*.** Typ
3724d 65 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 69 73  e SegmentNode is
3724e 20 75 73 65 64 20 62 79 20 74 68 65 20 66 6f 6c   used by the fol
3724f 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66 75 6e  lowing three fun
37250 63 74 69 6f 6e 73 20 74 6f 20 63 72 65 61 74 65  ctions to create
37251 0a 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72  .** the interior
37252 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 65 67   part of the seg
37253 6d 65 6e 74 20 62 2b 2d 74 72 65 65 20 73 74 72  ment b+-tree str
37254 75 63 74 75 72 65 73 20 28 65 76 65 72 79 74 68  uctures (everyth
37255 69 6e 67 20 65 78 63 65 70 74 0a 2a 2a 20 74 68  ing except.** th
37256 65 20 6c 65 61 66 20 6e 6f 64 65 73 29 2e 20 54  e leaf nodes). T
37257 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  hese functions a
37258 6e 64 20 74 79 70 65 20 61 72 65 20 6f 6e 6c 79  nd type are only
37259 20 65 76 65 72 20 75 73 65 64 20 62 79 20 63 6f   ever used by co
3725a 64 65 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  de.** within the
3725b 20 66 74 73 33 53 65 67 57 72 69 74 65 72 58 58   fts3SegWriterXX
3725c 58 28 29 20 66 61 6d 69 6c 79 20 6f 66 20 66 75  X() family of fu
3725d 6e 63 74 69 6f 6e 73 20 64 65 73 63 72 69 62 65  nctions describe
3725e 64 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 20  d above..**.**  
3725f 20 66 74 73 33 4e 6f 64 65 41 64 64 54 65 72 6d   fts3NodeAddTerm
37260 28 29 0a 2a 2a 20 20 20 66 74 73 33 4e 6f 64 65  ().**   fts3Node
37261 57 72 69 74 65 28 29 0a 2a 2a 20 20 20 66 74 73  Write().**   fts
37262 33 4e 6f 64 65 46 72 65 65 28 29 0a 2a 2f 0a 73  3NodeFree().*/.s
37263 74 72 75 63 74 20 53 65 67 6d 65 6e 74 4e 6f 64  truct SegmentNod
37264 65 20 7b 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64  e {.  SegmentNod
37265 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20  e *pParent;     
37266 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20        /* Parent 
37267 6e 6f 64 65 20 28 6f 72 20 4e 55 4c 4c 20 66 6f  node (or NULL fo
37268 72 20 72 6f 6f 74 20 6e 6f 64 65 29 20 2a 2f 0a  r root node) */.
37269 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70    SegmentNode *p
3726a 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  Right;          
3726b 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
3726c 72 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 2a 2f  right-sibling */
3726d 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a  .  SegmentNode *
3726e 70 4c 65 66 74 6d 6f 73 74 3b 20 20 20 20 20 20  pLeftmost;      
3726f 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
37270 20 6c 65 66 74 2d 6d 6f 73 74 20 6e 6f 64 65 20   left-most node 
37271 6f 66 20 74 68 69 73 20 64 65 70 74 68 20 2a 2f  of this depth */
37272 0a 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20  .  int nEntry;  
37273 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37274 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
37275 74 65 72 6d 73 20 77 72 69 74 74 65 6e 20 74 6f  terms written to
37276 20 6e 6f 64 65 20 73 6f 20 66 61 72 20 2a 2f 0a   node so far */.
37277 20 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20    char *zTerm;  
37278 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37279 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
3727a 70 72 65 76 69 6f 75 73 20 74 65 72 6d 20 62 75  previous term bu
3727b 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  ffer */.  int nT
3727c 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
3727d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3727e 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
3727f 7a 54 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  zTerm */.  int n
37280 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  Malloc;         
37281 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
37282 7a 65 20 6f 66 20 6d 61 6c 6c 6f 63 27 64 20 62  ze of malloc'd b
37283 75 66 66 65 72 20 61 74 20 7a 4d 61 6c 6c 6f 63  uffer at zMalloc
37284 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c   */.  char *zMal
37285 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  loc;            
37286 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27        /* Malloc'
37287 64 20 73 70 61 63 65 20 28 70 6f 73 73 69 62 6c  d space (possibl
37288 79 29 20 75 73 65 64 20 66 6f 72 20 7a 54 65 72  y) used for zTer
37289 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61  m */.  int nData
3728a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3728b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
3728c 6f 66 20 76 61 6c 69 64 20 64 61 74 61 20 73 6f  of valid data so
3728d 20 66 61 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a   far */.  char *
3728e 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  aData;          
3728f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64            /* Nod
37290 65 20 64 61 74 61 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  e data */.};../*
37291 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 73  .** Valid values
37292 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20   for the second 
37293 61 72 67 75 6d 65 6e 74 20 74 6f 20 66 74 73 33  argument to fts3
37294 53 71 6c 53 74 6d 74 28 29 2e 0a 2a 2f 0a 23 64  SqlStmt()..*/.#d
37295 65 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45  efine SQL_DELETE
37296 5f 43 4f 4e 54 45 4e 54 20 20 20 20 20 20 20 20  _CONTENT        
37297 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
37298 51 4c 5f 49 53 5f 45 4d 50 54 59 20 20 20 20 20  QL_IS_EMPTY     
37299 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
3729a 23 64 65 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45  #define SQL_DELE
3729b 54 45 5f 41 4c 4c 5f 43 4f 4e 54 45 4e 54 20 20  TE_ALL_CONTENT  
3729c 20 20 20 20 20 20 20 32 20 0a 23 64 65 66 69 6e         2 .#defin
3729d 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c  e SQL_DELETE_ALL
3729e 5f 53 45 47 4d 45 4e 54 53 20 20 20 20 20 20 20  _SEGMENTS       
3729f 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 44   3.#define SQL_D
372a0 45 4c 45 54 45 5f 41 4c 4c 5f 53 45 47 44 49 52  ELETE_ALL_SEGDIR
372a1 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66            4.#def
372a2 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54 5f 43  ine SQL_SELECT_C
372a3 4f 4e 54 45 4e 54 5f 42 59 5f 52 4f 57 49 44 20  ONTENT_BY_ROWID 
372a4 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c     5.#define SQL
372a5 5f 4e 45 58 54 5f 53 45 47 4d 45 4e 54 5f 49 4e  _NEXT_SEGMENT_IN
372a6 44 45 58 20 20 20 20 20 20 20 20 20 36 0a 23 64  DEX         6.#d
372a7 65 66 69 6e 65 20 53 51 4c 5f 49 4e 53 45 52 54  efine SQL_INSERT
372a8 5f 53 45 47 4d 45 4e 54 53 20 20 20 20 20 20 20  _SEGMENTS       
372a9 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53       7.#define S
372aa 51 4c 5f 4e 45 58 54 5f 53 45 47 4d 45 4e 54 53  QL_NEXT_SEGMENTS
372ab 5f 49 44 20 20 20 20 20 20 20 20 20 20 20 38 0a  _ID           8.
372ac 23 64 65 66 69 6e 65 20 53 51 4c 5f 49 4e 53 45  #define SQL_INSE
372ad 52 54 5f 53 45 47 44 49 52 20 20 20 20 20 20 20  RT_SEGDIR       
372ae 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65         9.#define
372af 20 53 51 4c 5f 53 45 4c 45 43 54 5f 4c 45 56 45   SQL_SELECT_LEVE
372b0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  L              1
372b1 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 53 45  0.#define SQL_SE
372b2 4c 45 43 54 5f 41 4c 4c 5f 4c 45 56 45 4c 20 20  LECT_ALL_LEVEL  
372b3 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69          11.#defi
372b4 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54 5f 4c 45  ne SQL_SELECT_LE
372b5 56 45 4c 5f 43 4f 55 4e 54 20 20 20 20 20 20 20  VEL_COUNT       
372b6 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f   12.#define SQL_
372b7 53 45 4c 45 43 54 5f 53 45 47 44 49 52 5f 43 4f  SELECT_SEGDIR_CO
372b8 55 4e 54 5f 4d 41 58 20 20 20 31 33 0a 23 64 65  UNT_MAX   13.#de
372b9 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f  fine SQL_DELETE_
372ba 53 45 47 44 49 52 5f 42 59 5f 4c 45 56 45 4c 20  SEGDIR_BY_LEVEL 
372bb 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 53 51     14.#define SQ
372bc 4c 5f 44 45 4c 45 54 45 5f 53 45 47 4d 45 4e 54  L_DELETE_SEGMENT
372bd 53 5f 52 41 4e 47 45 20 20 20 20 20 31 35 0a 23  S_RANGE     15.#
372be 64 65 66 69 6e 65 20 53 51 4c 5f 43 4f 4e 54 45  define SQL_CONTE
372bf 4e 54 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20  NT_INSERT       
372c0 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20       16.#define 
372c1 53 51 4c 5f 47 45 54 5f 42 4c 4f 43 4b 20 20 20  SQL_GET_BLOCK   
372c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37                17
372c3 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
372c4 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
372c5 20 6f 62 74 61 69 6e 20 61 6e 20 53 51 4c 69 74   obtain an SQLit
372c6 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
372c7 6d 65 6e 74 20 68 61 6e 64 6c 65 0a 2a 2a 20 66  ment handle.** f
372c8 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  or the statement
372c9 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
372ca 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
372cb 6e 74 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  nt. If successfu
372cc 6c 2c 0a 2a 2a 20 2a 70 70 20 69 73 20 73 65 74  l,.** *pp is set
372cd 20 74 6f 20 74 68 65 20 72 65 71 75 65 73 74 65   to the requeste
372ce 64 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  d statement hand
372cf 6c 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b  le and SQLITE_OK
372d0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74   returned..** Ot
372d1 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51 4c 69  herwise, an SQLi
372d2 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  te error code is
372d3 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70   returned and *p
372d4 70 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a  p is set to 0..*
372d5 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74  *.** If argument
372d6 20 61 70 56 61 6c 20 69 73 20 6e 6f 74 20 4e 55   apVal is not NU
372d7 4c 4c 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74  LL, then it must
372d8 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 72 72   point to an arr
372d9 61 79 20 77 69 74 68 0a 2a 2a 20 61 74 20 6c 65  ay with.** at le
372da 61 73 74 20 61 73 20 6d 61 6e 79 20 65 6e 74 72  ast as many entr
372db 69 65 73 20 61 73 20 74 68 65 20 72 65 71 75 65  ies as the reque
372dc 73 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 68  sted statement h
372dd 61 73 20 62 6f 75 6e 64 20 0a 2a 2a 20 70 61 72  as bound .** par
372de 61 6d 65 74 65 72 73 2e 20 54 68 65 20 76 61 6c  ameters. The val
372df 75 65 73 20 61 72 65 20 62 6f 75 6e 64 20 74 6f  ues are bound to
372e0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 73 20   the statements 
372e1 70 61 72 61 6d 65 74 65 72 73 20 62 65 66 6f 72  parameters befor
372e2 65 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 2e 0a  e.** returning..
372e3 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
372e4 73 33 53 71 6c 53 74 6d 74 28 0a 20 20 46 74 73  s3SqlStmt(.  Fts
372e5 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
372e6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
372e7 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
372e8 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 65 53  ndle */.  int eS
372e9 74 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  tmt,            
372ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
372eb 20 6f 66 20 74 68 65 20 53 51 4c 5f 58 58 58 20   of the SQL_XXX 
372ec 63 6f 6e 73 74 61 6e 74 73 20 61 62 6f 76 65 20  constants above 
372ed 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
372ee 74 20 2a 2a 70 70 2c 20 20 20 20 20 20 20 20 20  t **pp,         
372ef 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61       /* OUT: Sta
372f0 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f  tement handle */
372f1 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
372f2 20 2a 2a 61 70 56 61 6c 20 20 20 20 20 20 20 20   **apVal        
372f3 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 74 6f 20     /* Values to 
372f4 62 69 6e 64 20 74 6f 20 73 74 61 74 65 6d 65 6e  bind to statemen
372f5 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  t */.){.  const 
372f6 63 68 61 72 20 2a 61 7a 53 71 6c 5b 5d 20 3d 20  char *azSql[] = 
372f7 7b 0a 2f 2a 20 30 20 20 2a 2f 20 20 22 44 45 4c  {./* 0  */  "DEL
372f8 45 54 45 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  ETE FROM %Q.'%q_
372f9 63 6f 6e 74 65 6e 74 27 20 57 48 45 52 45 20 72  content' WHERE r
372fa 6f 77 69 64 20 3d 20 3f 22 2c 0a 2f 2a 20 31 20  owid = ?",./* 1 
372fb 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 4e 4f 54   */  "SELECT NOT
372fc 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 64   EXISTS(SELECT d
372fd 6f 63 69 64 20 46 52 4f 4d 20 25 51 2e 27 25 71  ocid FROM %Q.'%q
372fe 5f 63 6f 6e 74 65 6e 74 27 20 57 48 45 52 45 20  _content' WHERE 
372ff 72 6f 77 69 64 21 3d 3f 29 22 2c 0a 2f 2a 20 32  rowid!=?)",./* 2
37300 20 20 2a 2f 20 20 22 44 45 4c 45 54 45 20 46 52    */  "DELETE FR
37301 4f 4d 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e  OM %Q.'%q_conten
37302 74 27 22 2c 0a 2f 2a 20 33 20 20 2a 2f 20 20 22  t'",./* 3  */  "
37303 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 27  DELETE FROM %Q.'
37304 25 71 5f 73 65 67 6d 65 6e 74 73 27 22 2c 0a 2f  %q_segments'",./
37305 2a 20 34 20 20 2a 2f 20 20 22 44 45 4c 45 54 45  * 4  */  "DELETE
37306 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67   FROM %Q.'%q_seg
37307 64 69 72 27 22 2c 0a 2f 2a 20 35 20 20 2a 2f 20  dir'",./* 5  */ 
37308 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20   "SELECT * FROM 
37309 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20  %Q.'%q_content' 
3730a 57 48 45 52 45 20 72 6f 77 69 64 3d 3f 22 2c 0a  WHERE rowid=?",.
3730b 2f 2a 20 36 20 20 2a 2f 20 20 22 53 45 4c 45 43  /* 6  */  "SELEC
3730c 54 20 63 6f 61 6c 65 73 63 65 28 6d 61 78 28 69  T coalesce(max(i
3730d 64 78 29 2b 31 2c 20 30 29 20 46 52 4f 4d 20 25  dx)+1, 0) FROM %
3730e 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 57 48  Q.'%q_segdir' WH
3730f 45 52 45 20 6c 65 76 65 6c 3d 3f 22 2c 0a 2f 2a  ERE level=?",./*
37310 20 37 20 20 2a 2f 20 20 22 49 4e 53 45 52 54 20   7  */  "INSERT 
37311 49 4e 54 4f 20 25 51 2e 27 25 71 5f 73 65 67 6d  INTO %Q.'%q_segm
37312 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 2c 20 62  ents'(blockid, b
37313 6c 6f 63 6b 29 20 56 41 4c 55 45 53 28 3f 2c 20  lock) VALUES(?, 
37314 3f 29 22 2c 0a 2f 2a 20 38 20 20 2a 2f 20 20 22  ?)",./* 8  */  "
37315 53 45 4c 45 43 54 20 63 6f 61 6c 65 73 63 65 28  SELECT coalesce(
37316 6d 61 78 28 62 6c 6f 63 6b 69 64 29 2b 31 2c 20  max(blockid)+1, 
37317 31 29 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73  1) FROM %Q.'%q_s
37318 65 67 6d 65 6e 74 73 27 22 2c 0a 2f 2a 20 39 20  egments'",./* 9 
37319 20 2a 2f 20 20 22 49 4e 53 45 52 54 20 49 4e 54   */  "INSERT INT
3731a 4f 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27  O %Q.'%q_segdir'
3731b 20 56 41 4c 55 45 53 28 3f 2c 3f 2c 3f 2c 3f 2c   VALUES(?,?,?,?,
3731c 3f 2c 3f 29 22 2c 0a 0a 20 20 20 20 20 20 20 20  ?,?)",..        
3731d 20 20 2f 2a 20 52 65 74 75 72 6e 20 73 65 67 6d    /* Return segm
3731e 65 6e 74 73 20 69 6e 20 6f 72 64 65 72 20 66 72  ents in order fr
3731f 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77  om oldest to new
37320 65 73 74 2e 2a 2f 20 0a 2f 2a 20 31 30 20 2a 2f  est.*/ ./* 10 */
37321 20 20 22 53 45 4c 45 43 54 20 69 64 78 2c 20 73    "SELECT idx, s
37322 74 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76  tart_block, leav
37323 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 65 6e  es_end_block, en
37324 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 22 0a  d_block, root ".
37325 20 20 20 20 20 20 20 20 20 20 20 20 22 46 52 4f              "FRO
37326 4d 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27  M %Q.'%q_segdir'
37327 20 57 48 45 52 45 20 6c 65 76 65 6c 20 3d 20 3f   WHERE level = ?
37328 20 4f 52 44 45 52 20 42 59 20 69 64 78 20 41 53   ORDER BY idx AS
37329 43 22 2c 0a 2f 2a 20 31 31 20 2a 2f 20 20 22 53  C",./* 11 */  "S
3732a 45 4c 45 43 54 20 69 64 78 2c 20 73 74 61 72 74  ELECT idx, start
3732b 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65  _block, leaves_e
3732c 6e 64 5f 62 6c 6f 63 6b 2c 20 65 6e 64 5f 62 6c  nd_block, end_bl
3732d 6f 63 6b 2c 20 72 6f 6f 74 20 22 0a 20 20 20 20  ock, root ".    
3732e 20 20 20 20 20 20 20 20 22 46 52 4f 4d 20 25 51          "FROM %Q
3732f 2e 27 25 71 5f 73 65 67 64 69 72 27 20 4f 52 44  .'%q_segdir' ORD
37330 45 52 20 42 59 20 6c 65 76 65 6c 20 44 45 53 43  ER BY level DESC
37331 2c 20 69 64 78 20 41 53 43 22 2c 0a 0a 2f 2a 20  , idx ASC",../* 
37332 31 32 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 63  12 */  "SELECT c
37333 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 25 51 2e  ount(*) FROM %Q.
37334 27 25 71 5f 73 65 67 64 69 72 27 20 57 48 45 52  '%q_segdir' WHER
37335 45 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a 2f 2a  E level = ?",./*
37336 20 31 33 20 2a 2f 20 20 22 53 45 4c 45 43 54 20   13 */  "SELECT 
37337 63 6f 75 6e 74 28 2a 29 2c 20 6d 61 78 28 6c 65  count(*), max(le
37338 76 65 6c 29 20 46 52 4f 4d 20 25 51 2e 27 25 71  vel) FROM %Q.'%q
37339 5f 73 65 67 64 69 72 27 22 2c 0a 0a 2f 2a 20 31  _segdir'",../* 1
3733a 34 20 2a 2f 20 20 22 44 45 4c 45 54 45 20 46 52  4 */  "DELETE FR
3733b 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72  OM %Q.'%q_segdir
3733c 27 20 57 48 45 52 45 20 6c 65 76 65 6c 20 3d 20  ' WHERE level = 
3733d 3f 22 2c 0a 2f 2a 20 31 35 20 2a 2f 20 20 22 44  ?",./* 15 */  "D
3733e 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 27 25  ELETE FROM %Q.'%
3733f 71 5f 73 65 67 6d 65 6e 74 73 27 20 57 48 45 52  q_segments' WHER
37340 45 20 62 6c 6f 63 6b 69 64 20 42 45 54 57 45 45  E blockid BETWEE
37341 4e 20 3f 20 41 4e 44 20 3f 22 2c 0a 2f 2a 20 31  N ? AND ?",./* 1
37342 36 20 2a 2f 20 20 22 49 4e 53 45 52 54 20 49 4e  6 */  "INSERT IN
37343 54 4f 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e  TO %Q.'%q_conten
37344 74 27 20 56 41 4c 55 45 53 28 25 7a 29 22 2c 0a  t' VALUES(%z)",.
37345 2f 2a 20 31 37 20 2a 2f 20 20 22 53 45 4c 45 43  /* 17 */  "SELEC
37346 54 20 62 6c 6f 63 6b 20 46 52 4f 4d 20 25 51 2e  T block FROM %Q.
37347 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 20 57 48  '%q_segments' WH
37348 45 52 45 20 62 6c 6f 63 6b 69 64 20 3d 20 3f 22  ERE blockid = ?"
37349 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 72 63 20  ,.  };.  int rc 
3734a 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73  = SQLITE_OK;.  s
3734b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
3734c 6d 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 53  mt;..  assert( S
3734d 69 7a 65 6f 66 41 72 72 61 79 28 61 7a 53 71 6c  izeofArray(azSql
3734e 29 3d 3d 53 69 7a 65 6f 66 41 72 72 61 79 28 70  )==SizeofArray(p
3734f 2d 3e 61 53 74 6d 74 29 20 29 3b 0a 20 20 61 73  ->aStmt) );.  as
37350 73 65 72 74 28 20 65 53 74 6d 74 3c 53 69 7a 65  sert( eStmt<Size
37351 6f 66 41 72 72 61 79 28 61 7a 53 71 6c 29 20 26  ofArray(azSql) &
37352 26 20 65 53 74 6d 74 3e 3d 30 20 29 3b 0a 20 20  & eStmt>=0 );.  
37353 0a 20 20 70 53 74 6d 74 20 3d 20 70 2d 3e 61 53  .  pStmt = p->aS
37354 74 6d 74 5b 65 53 74 6d 74 5d 3b 0a 20 20 69 66  tmt[eStmt];.  if
37355 28 20 21 70 53 74 6d 74 20 29 7b 0a 20 20 20 20  ( !pStmt ){.    
37356 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20  char *zSql;.    
37357 69 66 28 20 65 53 74 6d 74 3d 3d 53 51 4c 5f 43  if( eStmt==SQL_C
37358 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 20 29 7b  ONTENT_INSERT ){
37359 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20  .      int i;   
3735a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3735b 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
3735c 61 72 69 61 62 6c 65 20 2a 2f 20 20 0a 20 20 20  ariable */  .   
3735d 20 20 20 63 68 61 72 20 2a 7a 56 61 72 6c 69 73     char *zVarlis
3735e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t;             /
3735f 2a 20 54 68 65 20 22 3f 2c 20 3f 2c 20 2e 2e 2e  * The "?, ?, ...
37360 22 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 20 20  " string */.    
37361 20 20 7a 56 61 72 6c 69 73 74 20 3d 20 28 63 68    zVarlist = (ch
37362 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar *)sqlite3_mal
37363 6c 6f 63 28 32 2a 70 2d 3e 6e 43 6f 6c 75 6d 6e  loc(2*p->nColumn
37364 2b 32 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  +2);.      if( !
37365 7a 56 61 72 6c 69 73 74 20 29 7b 0a 20 20 20 20  zVarlist ){.    
37366 20 20 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 20      *pp = 0;.   
37367 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
37368 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
37369 7d 0a 20 20 20 20 20 20 7a 56 61 72 6c 69 73 74  }.      zVarlist
3736a 5b 30 5d 20 3d 20 27 3f 27 3b 0a 20 20 20 20 20  [0] = '?';.     
3736b 20 7a 56 61 72 6c 69 73 74 5b 70 2d 3e 6e 43 6f   zVarlist[p->nCo
3736c 6c 75 6d 6e 2a 32 2b 31 5d 20 3d 20 27 5c 30 27  lumn*2+1] = '\0'
3736d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  ;.      for(i=1;
3736e 20 69 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20   i<=p->nColumn; 
3736f 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 56  i++){.        zV
37370 61 72 6c 69 73 74 5b 69 2a 32 2d 31 5d 20 3d 20  arlist[i*2-1] = 
37371 27 2c 27 3b 0a 20 20 20 20 20 20 20 20 7a 56 61  ',';.        zVa
37372 72 6c 69 73 74 5b 69 2a 32 5d 20 3d 20 27 3f 27  rlist[i*2] = '?'
37373 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
37374 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSql = sqlite3_m
37375 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b 65 53 74  printf(azSql[eSt
37376 6d 74 5d 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e  mt], p->zDb, p->
37377 7a 4e 61 6d 65 2c 20 7a 56 61 72 6c 69 73 74 29  zName, zVarlist)
37378 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
37379 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65     zSql = sqlite
3737a 33 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b  3_mprintf(azSql[
3737b 65 53 74 6d 74 5d 2c 20 70 2d 3e 7a 44 62 2c 20  eStmt], p->zDb, 
3737c 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  p->zName);.    }
3737d 0a 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 29  .    if( !zSql )
3737e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
3737f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
37380 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
37381 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
37382 5f 76 32 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c  _v2(p->db, zSql,
37383 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 4e 55 4c   -1, &pStmt, NUL
37384 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  L);.      sqlite
37385 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
37386 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
37387 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 53 74  SQLITE_OK || pSt
37388 6d 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  mt==0 );.      p
37389 2d 3e 61 53 74 6d 74 5b 65 53 74 6d 74 5d 20 3d  ->aStmt[eStmt] =
3738a 20 70 53 74 6d 74 3b 0a 20 20 20 20 7d 0a 20 20   pStmt;.    }.  
3738b 7d 0a 20 20 69 66 28 20 61 70 56 61 6c 20 29 7b  }.  if( apVal ){
3738c 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
3738d 69 6e 74 20 6e 50 61 72 61 6d 20 3d 20 73 71 6c  int nParam = sql
3738e 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
3738f 74 65 72 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29  ter_count(pStmt)
37390 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72  ;.    for(i=0; r
37391 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
37392 69 3c 6e 50 61 72 61 6d 3b 20 69 2b 2b 29 7b 0a  i<nParam; i++){.
37393 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
37394 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 70 53  e3_bind_value(pS
37395 74 6d 74 2c 20 69 2b 31 2c 20 61 70 56 61 6c 5b  tmt, i+1, apVal[
37396 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  i]);.    }.  }. 
37397 20 2a 70 70 20 3d 20 70 53 74 6d 74 3b 0a 20 20   *pp = pStmt;.  
37398 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
37399 0a 2a 2a 20 53 69 6d 69 6c 61 72 20 74 6f 20 66  .** Similar to f
3739a 74 73 33 53 71 6c 53 74 6d 74 28 29 2e 20 45 78  ts3SqlStmt(). Ex
3739b 63 65 70 74 2c 20 61 66 74 65 72 20 62 69 6e 64  cept, after bind
3739c 69 6e 67 20 74 68 65 20 70 61 72 61 6d 65 74 65  ing the paramete
3739d 72 73 20 69 6e 0a 2a 2a 20 61 72 72 61 79 20 61  rs in.** array a
3739e 70 56 61 6c 5b 5d 20 74 6f 20 74 68 65 20 53 51  pVal[] to the SQ
3739f 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 64 65 6e  L statement iden
373a0 74 69 66 69 65 64 20 62 79 20 65 53 74 6d 74 2c  tified by eStmt,
373a1 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a   the statement.*
373a2 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a  * is executed..*
373a3 2a 0a 2a 2a 20 52 65 74 75 72 6e 73 20 53 51 4c  *.** Returns SQL
373a4 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 73 74  ITE_OK if the st
373a5 61 74 65 6d 65 6e 74 20 69 73 20 73 75 63 63 65  atement is succe
373a6 73 73 66 75 6c 6c 79 20 65 78 65 63 75 74 65 64  ssfully executed
373a7 2c 20 6f 72 20 61 6e 0a 2a 2a 20 53 51 4c 69 74  , or an.** SQLit
373a8 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68  e error code oth
373a9 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  erwise..*/.stati
373aa 63 20 69 6e 74 20 66 74 73 33 53 71 6c 45 78 65  c int fts3SqlExe
373ab 63 28 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20  c(Fts3Table *p, 
373ac 69 6e 74 20 65 53 74 6d 74 2c 20 73 71 6c 69 74  int eStmt, sqlit
373ad 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
373ae 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
373af 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20  t *pStmt;.  int 
373b0 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
373b1 28 70 2c 20 65 53 74 6d 74 2c 20 26 70 53 74 6d  (p, eStmt, &pStm
373b2 74 2c 20 61 70 56 61 6c 29 3b 20 0a 20 20 69 66  t, apVal); .  if
373b3 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
373b4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
373b5 74 65 70 28 70 53 74 6d 74 29 3b 0a 20 20 20 20  tep(pStmt);.    
373b6 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
373b7 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20  et(pStmt);.  }. 
373b8 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
373b9 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e  /*.** Read a sin
373ba 67 6c 65 20 62 6c 6f 63 6b 20 66 72 6f 6d 20 74  gle block from t
373bb 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61  he %_segments ta
373bc 62 6c 65 2e 20 49 66 20 74 68 65 20 73 70 65 63  ble. If the spec
373bd 69 66 69 65 64 20 62 6c 6f 63 6b 0a 2a 2a 20 64  ified block.** d
373be 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 72  oes not exist, r
373bf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
373c0 52 55 50 54 2e 20 49 66 20 73 6f 6d 65 20 6f 74  RUPT. If some ot
373c1 68 65 72 20 65 72 72 6f 72 20 28 6d 61 6c 6c 6f  her error (mallo
373c2 63 2c 20 49 4f 20 0a 2a 2a 20 65 74 63 2e 29 20  c, IO .** etc.) 
373c3 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74  occurs, return t
373c4 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 53  he appropriate S
373c5 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
373c6 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
373c7 65 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  e, if successful
373c8 2c 20 73 65 74 20 2a 70 7a 42 6c 6f 63 6b 20 74  , set *pzBlock t
373c9 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66  o point to a buf
373ca 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a  fer containing.*
373cb 2a 20 74 68 65 20 62 6c 6f 63 6b 20 72 65 61 64  * the block read
373cc 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
373cd 73 65 2c 20 61 6e 64 20 2a 70 6e 42 6c 6f 63 6b  se, and *pnBlock
373ce 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20   to the size of 
373cf 74 68 65 20 72 65 61 64 0a 2a 2a 20 62 6c 6f 63  the read.** bloc
373d0 6b 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  k in bytes..**.*
373d1 2a 20 57 41 52 4e 49 4e 47 3a 20 54 68 65 20 72  * WARNING: The r
373d2 65 74 75 72 6e 65 64 20 62 75 66 66 65 72 20 69  eturned buffer i
373d3 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 75 6e 74  s only valid unt
373d4 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  il the next call
373d5 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 46   to .** sqlite3F
373d6 74 73 33 52 65 61 64 42 6c 6f 63 6b 28 29 2e 0a  ts3ReadBlock()..
373d7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
373d8 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
373d9 33 52 65 61 64 42 6c 6f 63 6b 28 0a 20 20 46 74  3ReadBlock(.  Ft
373da 73 33 54 61 62 6c 65 20 2a 70 2c 0a 20 20 73 71  s3Table *p,.  sq
373db 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 42 6c 6f  lite3_int64 iBlo
373dc 63 6b 2c 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  ck,.  char const
373dd 20 2a 2a 70 7a 42 6c 6f 63 6b 2c 0a 20 20 69 6e   **pzBlock,.  in
373de 74 20 2a 70 6e 42 6c 6f 63 6b 0a 29 7b 0a 20 20  t *pnBlock.){.  
373df 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
373e0 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  tmt;.  int rc = 
373e1 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53  fts3SqlStmt(p, S
373e2 51 4c 5f 47 45 54 5f 42 4c 4f 43 4b 2c 20 26 70  QL_GET_BLOCK, &p
373e3 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  Stmt, 0);.  if( 
373e4 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
373e5 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 73 71 6c  return rc;.  sql
373e6 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74  ite3_reset(pStmt
373e7 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  );..  sqlite3_bi
373e8 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20  nd_int64(pStmt, 
373e9 31 2c 20 69 42 6c 6f 63 6b 29 3b 0a 20 20 72 63  1, iBlock);.  rc
373ea 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
373eb 70 53 74 6d 74 29 3b 20 0a 20 20 69 66 28 20 72  pStmt); .  if( r
373ec 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c!=SQLITE_ROW ){
373ed 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
373ee 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 7d 0a  TE_CORRUPT;.  }.
373ef 0a 20 20 2a 70 6e 42 6c 6f 63 6b 20 3d 20 73 71  .  *pnBlock = sq
373f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
373f1 65 73 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  es(pStmt, 0);.  
373f2 2a 70 7a 42 6c 6f 63 6b 20 3d 20 28 63 68 61 72  *pzBlock = (char
373f3 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   *)sqlite3_colum
373f4 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29  n_blob(pStmt, 0)
373f5 3b 0a 20 20 69 66 28 20 21 2a 70 7a 42 6c 6f 63  ;.  if( !*pzBloc
373f6 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  k ){.    return 
373f7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
373f8 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
373f9 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  E_OK;.}../*.** S
373fa 65 74 20 2a 70 70 53 74 6d 74 20 74 6f 20 61 20  et *ppStmt to a 
373fb 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
373fc 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65   that may be use
373fd 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72  d to iterate thr
373fe 6f 75 67 68 0a 2a 2a 20 61 6c 6c 20 72 6f 77 73  ough.** all rows
373ff 20 69 6e 20 74 68 65 20 25 5f 73 65 67 64 69 72   in the %_segdir
37400 20 74 61 62 6c 65 2c 20 66 72 6f 6d 20 6f 6c 64   table, from old
37401 65 73 74 20 74 6f 20 6e 65 77 65 73 74 2e 20 49  est to newest. I
37402 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a  f successful,.**
37403 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
37404 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  K. If an error o
37405 63 63 75 72 73 20 77 68 69 6c 65 20 70 72 65 70  ccurs while prep
37406 61 72 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d  aring the statem
37407 65 6e 74 2c 20 0a 2a 2a 20 72 65 74 75 72 6e 20  ent, .** return 
37408 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
37409 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  code..**.** Ther
3740a 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f  e is only ever o
3740b 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ne instance of t
3740c 68 69 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  his SQL statemen
3740d 74 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 0a 2a  t compiled for.*
3740e 2a 20 65 61 63 68 20 46 54 53 33 20 74 61 62 6c  * each FTS3 tabl
3740f 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61  e..**.** The sta
37410 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 74  tement returns t
37411 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6c  he following col
37412 75 6d 6e 73 20 66 72 6f 6d 20 74 68 65 20 25 5f  umns from the %_
37413 73 65 67 64 69 72 20 74 61 62 6c 65 3a 0a 2a 2a  segdir table:.**
37414 0a 2a 2a 20 20 20 30 3a 20 69 64 78 0a 2a 2a 20  .**   0: idx.** 
37415 20 20 31 3a 20 73 74 61 72 74 5f 62 6c 6f 63 6b    1: start_block
37416 0a 2a 2a 20 20 20 32 3a 20 6c 65 61 76 65 73 5f  .**   2: leaves_
37417 65 6e 64 5f 62 6c 6f 63 6b 0a 2a 2a 20 20 20 33  end_block.**   3
37418 3a 20 65 6e 64 5f 62 6c 6f 63 6b 0a 2a 2a 20 20  : end_block.**  
37419 20 34 3a 20 72 6f 6f 74 0a 2a 2f 0a 53 51 4c 49   4: root.*/.SQLI
3741a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3741b 71 6c 69 74 65 33 46 74 73 33 41 6c 6c 53 65 67  qlite3Fts3AllSeg
3741c 64 69 72 73 28 46 74 73 33 54 61 62 6c 65 20 2a  dirs(Fts3Table *
3741d 70 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  p, sqlite3_stmt 
3741e 2a 2a 70 70 53 74 6d 74 29 7b 0a 20 20 72 65 74  **ppStmt){.  ret
3741f 75 72 6e 20 66 74 73 33 53 71 6c 53 74 6d 74 28  urn fts3SqlStmt(
37420 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 41 4c  p, SQL_SELECT_AL
37421 4c 5f 4c 45 56 45 4c 2c 20 70 70 53 74 6d 74 2c  L_LEVEL, ppStmt,
37422 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41   0);.}.../*.** A
37423 70 70 65 6e 64 20 61 20 73 69 6e 67 6c 65 20 76  ppend a single v
37424 61 72 69 6e 74 20 74 6f 20 61 20 50 65 6e 64 69  arint to a Pendi
37425 6e 67 4c 69 73 74 20 62 75 66 66 65 72 2e 20 53  ngList buffer. S
37426 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
37427 72 6e 65 64 0a 2a 2a 20 69 66 20 73 75 63 63 65  rned.** if succe
37428 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c  ssful, or an SQL
37429 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f  ite error code o
3742a 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20  therwise..**.** 
3742b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  This function al
3742c 73 6f 20 73 65 72 76 65 73 20 74 6f 20 61 6c 6c  so serves to all
3742d 6f 63 61 74 65 20 74 68 65 20 50 65 6e 64 69 6e  ocate the Pendin
3742e 67 4c 69 73 74 20 73 74 72 75 63 74 75 72 65 20  gList structure 
3742f 69 74 73 65 6c 66 2e 0a 2a 2a 20 46 6f 72 20 65  itself..** For e
37430 78 61 6d 70 6c 65 2c 20 74 6f 20 63 72 65 61 74  xample, to creat
37431 65 20 61 20 6e 65 77 20 50 65 6e 64 69 6e 67 4c  e a new PendingL
37432 69 73 74 20 73 74 72 75 63 74 75 72 65 20 63 6f  ist structure co
37433 6e 74 61 69 6e 69 6e 67 20 74 77 6f 0a 2a 2a 20  ntaining two.** 
37434 76 61 72 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  varints:.**.**  
37435 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 70 20   PendingList *p 
37436 3d 20 30 3b 0a 2a 2a 20 20 20 66 74 73 33 50 65  = 0;.**   fts3Pe
37437 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 56  ndingListAppendV
37438 61 72 69 6e 74 28 26 70 2c 20 31 29 3b 0a 2a 2a  arint(&p, 1);.**
37439 20 20 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69     fts3PendingLi
3743a 73 74 41 70 70 65 6e 64 56 61 72 69 6e 74 28 26  stAppendVarint(&
3743b 70 2c 20 32 29 3b 0a 2a 2f 0a 73 74 61 74 69 63  p, 2);.*/.static
3743c 20 69 6e 74 20 66 74 73 33 50 65 6e 64 69 6e 67   int fts3Pending
3743d 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69 6e 74  ListAppendVarint
3743e 28 0a 20 20 50 65 6e 64 69 6e 67 4c 69 73 74 20  (.  PendingList 
3743f 2a 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20  **pp,           
37440 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50      /* IN/OUT: P
37441 6f 69 6e 74 65 72 20 74 6f 20 50 65 6e 64 69 6e  ointer to Pendin
37442 67 4c 69 73 74 20 73 74 72 75 63 74 20 2a 2f 0a  gList struct */.
37443 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
37444 69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i               
37445 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 61 70    /* Value to ap
37446 70 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a  pend to data */.
37447 29 7b 0a 20 20 50 65 6e 64 69 6e 67 4c 69 73 74  ){.  PendingList
37448 20 2a 70 20 3d 20 2a 70 70 3b 0a 0a 20 20 2f 2a   *p = *pp;..  /*
37449 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 67 72 6f   Allocate or gro
3744a 77 20 74 68 65 20 50 65 6e 64 69 6e 67 4c 69 73  w the PendingLis
3744b 74 20 61 73 20 72 65 71 75 69 72 65 64 2e 20 2a  t as required. *
3744c 2f 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  /.  if( !p ){.  
3744d 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61    p = sqlite3_ma
3744e 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 29 20  lloc(sizeof(*p) 
3744f 2b 20 31 30 30 29 3b 0a 20 20 20 20 69 66 28 20  + 100);.    if( 
37450 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  !p ){.      retu
37451 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
37452 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 53  .    }.    p->nS
37453 70 61 63 65 20 3d 20 31 30 30 3b 0a 20 20 20 20  pace = 100;.    
37454 70 2d 3e 61 44 61 74 61 20 3d 20 28 63 68 61 72  p->aData = (char
37455 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 70 2d   *)&p[1];.    p-
37456 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 0a  >nData = 0;.  }.
37457 20 20 65 6c 73 65 20 69 66 28 20 70 2d 3e 6e 44    else if( p->nD
37458 61 74 61 2b 46 54 53 33 5f 56 41 52 49 4e 54 5f  ata+FTS3_VARINT_
37459 4d 41 58 2b 31 3e 70 2d 3e 6e 53 70 61 63 65 20  MAX+1>p->nSpace 
3745a 29 7b 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 20  ){.    int nNew 
3745b 3d 20 70 2d 3e 6e 53 70 61 63 65 20 2a 20 32 3b  = p->nSpace * 2;
3745c 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
3745d 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 73 69 7a 65  _realloc(p, size
3745e 6f 66 28 2a 70 29 20 2b 20 6e 4e 65 77 29 3b 0a  of(*p) + nNew);.
3745f 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20      if( !p ){.  
37460 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
37461 28 2a 70 70 29 3b 0a 20 20 20 20 20 20 2a 70 70  (*pp);.      *pp
37462 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   = 0;.      retu
37463 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
37464 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 53  .    }.    p->nS
37465 70 61 63 65 20 3d 20 6e 4e 65 77 3b 0a 20 20 20  pace = nNew;.   
37466 20 70 2d 3e 61 44 61 74 61 20 3d 20 28 63 68 61   p->aData = (cha
37467 72 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 7d 0a 0a  r *)&p[1];.  }..
37468 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20    /* Append the 
37469 6e 65 77 20 73 65 72 69 61 6c 69 7a 65 64 20 76  new serialized v
3746a 61 72 69 6e 74 20 74 6f 20 74 68 65 20 65 6e 64  arint to the end
3746b 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f   of the list. */
3746c 0a 20 20 70 2d 3e 6e 44 61 74 61 20 2b 3d 20 73  .  p->nData += s
3746d 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72  qlite3Fts3PutVar
3746e 69 6e 74 28 26 70 2d 3e 61 44 61 74 61 5b 70 2d  int(&p->aData[p-
3746f 3e 6e 44 61 74 61 5d 2c 20 69 29 3b 0a 20 20 70  >nData], i);.  p
37470 2d 3e 61 44 61 74 61 5b 70 2d 3e 6e 44 61 74 61  ->aData[p->nData
37471 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 2a 70 70 20  ] = '\0';.  *pp 
37472 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  = p;.  return SQ
37473 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
37474 2a 20 41 64 64 20 61 20 64 6f 63 69 64 2f 63 6f  * Add a docid/co
37475 6c 75 6d 6e 2f 70 6f 73 69 74 69 6f 6e 20 65 6e  lumn/position en
37476 74 72 79 20 74 6f 20 61 20 50 65 6e 64 69 6e 67  try to a Pending
37477 4c 69 73 74 20 73 74 72 75 63 74 75 72 65 2e 20  List structure. 
37478 4e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 69 73 20 72  Non-zero.** is r
37479 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 73  eturned if the s
3747a 74 72 75 63 74 75 72 65 20 69 73 20 73 71 6c 69  tructure is sqli
3747b 74 65 33 5f 72 65 61 6c 6c 6f 63 65 64 20 61 73  te3_realloced as
3747c 20 70 61 72 74 20 6f 66 20 61 64 64 69 6e 67 0a   part of adding.
3747d 2a 2a 20 74 68 65 20 65 6e 74 72 79 2e 20 4f 74  ** the entry. Ot
3747e 68 65 72 77 69 73 65 2c 20 7a 65 72 6f 2e 0a 2a  herwise, zero..*
3747f 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f 4d 20 65  *.** If an OOM e
37480 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 52  rror occurs, *pR
37481 63 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  c is set to SQLI
37482 54 45 5f 4e 4f 4d 45 4d 20 62 65 66 6f 72 65 20  TE_NOMEM before 
37483 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 20 5a 65  returning..** Ze
37484 72 6f 20 69 73 20 61 6c 77 61 79 73 20 72 65 74  ro is always ret
37485 75 72 6e 65 64 20 69 6e 20 74 68 69 73 20 63 61  urned in this ca
37486 73 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  se. Otherwise, i
37487 66 20 6e 6f 20 4f 4f 4d 20 65 72 72 6f 72 20 6f  f no OOM error o
37488 63 63 75 72 73 2c 0a 2a 2a 20 69 74 20 69 73 20  ccurs,.** it is 
37489 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b  set to SQLITE_OK
3748a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3748b 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 41  fts3PendingListA
3748c 70 70 65 6e 64 28 0a 20 20 50 65 6e 64 69 6e 67  ppend(.  Pending
3748d 4c 69 73 74 20 2a 2a 70 70 2c 20 20 20 20 20 20  List **pp,      
3748e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
3748f 55 54 3a 20 50 65 6e 64 69 6e 67 4c 69 73 74 20  UT: PendingList 
37490 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73  structure */.  s
37491 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44 6f  qlite3_int64 iDo
37492 63 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f  cid,           /
37493 2a 20 44 6f 63 69 64 20 66 6f 72 20 65 6e 74 72  * Docid for entr
37494 79 20 74 6f 20 61 64 64 20 2a 2f 0a 20 20 73 71  y to add */.  sq
37495 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 43 6f 6c  lite3_int64 iCol
37496 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
37497 20 43 6f 6c 75 6d 6e 20 66 6f 72 20 65 6e 74 72   Column for entr
37498 79 20 74 6f 20 61 64 64 20 2a 2f 0a 20 20 73 71  y to add */.  sq
37499 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 6f 73  lite3_int64 iPos
3749a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
3749b 20 50 6f 73 69 74 69 6f 6e 20 6f 66 20 74 65 72   Position of ter
3749c 6d 20 66 6f 72 20 65 6e 74 72 79 20 74 6f 20 61  m for entry to a
3749d 64 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 63  dd */.  int *pRc
3749e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3749f 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
374a0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 29  Return code */.)
374a1 7b 0a 20 20 50 65 6e 64 69 6e 67 4c 69 73 74 20  {.  PendingList 
374a2 2a 70 20 3d 20 2a 70 70 3b 0a 20 20 69 6e 74 20  *p = *pp;.  int 
374a3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
374a4 0a 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c  .  assert( !p ||
374a5 20 70 2d 3e 69 4c 61 73 74 44 6f 63 69 64 3c 3d   p->iLastDocid<=
374a6 69 44 6f 63 69 64 20 29 3b 0a 0a 20 20 69 66 28  iDocid );..  if(
374a7 20 21 70 20 7c 7c 20 70 2d 3e 69 4c 61 73 74 44   !p || p->iLastD
374a8 6f 63 69 64 21 3d 69 44 6f 63 69 64 20 29 7b 0a  ocid!=iDocid ){.
374a9 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
374aa 34 20 69 44 65 6c 74 61 20 3d 20 69 44 6f 63 69  4 iDelta = iDoci
374ab 64 20 2d 20 28 70 20 3f 20 70 2d 3e 69 4c 61 73  d - (p ? p->iLas
374ac 74 44 6f 63 69 64 20 3a 20 30 29 3b 0a 20 20 20  tDocid : 0);.   
374ad 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
374ae 61 73 73 65 72 74 28 20 70 2d 3e 6e 44 61 74 61  assert( p->nData
374af 3c 70 2d 3e 6e 53 70 61 63 65 20 29 3b 0a 20 20  <p->nSpace );.  
374b0 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
374b1 44 61 74 61 5b 70 2d 3e 6e 44 61 74 61 5d 3d 3d  Data[p->nData]==
374b2 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 44  0 );.      p->nD
374b3 61 74 61 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  ata++;.    }.   
374b4 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
374b5 28 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e  (rc = fts3Pendin
374b6 67 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69 6e  gListAppendVarin
374b7 74 28 26 70 2c 20 69 44 65 6c 74 61 29 29 20 29  t(&p, iDelta)) )
374b8 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 65 6e  {.      goto pen
374b9 64 69 6e 67 6c 69 73 74 61 70 70 65 6e 64 5f 6f  dinglistappend_o
374ba 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  ut;.    }.    p-
374bb 3e 69 4c 61 73 74 43 6f 6c 20 3d 20 2d 31 3b 0a  >iLastCol = -1;.
374bc 20 20 20 20 70 2d 3e 69 4c 61 73 74 50 6f 73 20      p->iLastPos 
374bd 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 69 4c 61 73  = 0;.    p->iLas
374be 74 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b  tDocid = iDocid;
374bf 0a 20 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e  .  }.  if( iCol>
374c0 30 20 26 26 20 70 2d 3e 69 4c 61 73 74 43 6f 6c  0 && p->iLastCol
374c1 21 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 69 66  !=iCol ){.    if
374c2 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
374c3 20 3d 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69   = fts3PendingLi
374c4 73 74 41 70 70 65 6e 64 56 61 72 69 6e 74 28 26  stAppendVarint(&
374c5 70 2c 20 31 29 29 0a 20 20 20 20 20 7c 7c 20 53  p, 1)).     || S
374c6 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
374c7 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 41  fts3PendingListA
374c8 70 70 65 6e 64 56 61 72 69 6e 74 28 26 70 2c 20  ppendVarint(&p, 
374c9 69 43 6f 6c 29 29 0a 20 20 20 20 29 7b 0a 20 20  iCol)).    ){.  
374ca 20 20 20 20 67 6f 74 6f 20 70 65 6e 64 69 6e 67      goto pending
374cb 6c 69 73 74 61 70 70 65 6e 64 5f 6f 75 74 3b 0a  listappend_out;.
374cc 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 4c 61      }.    p->iLa
374cd 73 74 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20  stCol = iCol;.  
374ce 20 20 70 2d 3e 69 4c 61 73 74 50 6f 73 20 3d 20    p->iLastPos = 
374cf 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 43 6f  0;.  }.  if( iCo
374d0 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  l>=0 ){.    asse
374d1 72 74 28 20 69 50 6f 73 3e 70 2d 3e 69 4c 61 73  rt( iPos>p->iLas
374d2 74 50 6f 73 20 7c 7c 20 28 69 50 6f 73 3d 3d 30  tPos || (iPos==0
374d3 20 26 26 20 70 2d 3e 69 4c 61 73 74 50 6f 73 3d   && p->iLastPos=
374d4 3d 30 29 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  =0) );.    rc = 
374d5 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 41  fts3PendingListA
374d6 70 70 65 6e 64 56 61 72 69 6e 74 28 26 70 2c 20  ppendVarint(&p, 
374d7 32 2b 69 50 6f 73 2d 70 2d 3e 69 4c 61 73 74 50  2+iPos-p->iLastP
374d8 6f 73 29 3b 0a 20 20 20 20 70 2d 3e 69 4c 61 73  os);.    p->iLas
374d9 74 50 6f 73 20 3d 20 69 50 6f 73 3b 0a 20 20 7d  tPos = iPos;.  }
374da 0a 0a 20 70 65 6e 64 69 6e 67 6c 69 73 74 61 70  .. pendinglistap
374db 70 65 6e 64 5f 6f 75 74 3a 0a 20 20 2a 70 52 63  pend_out:.  *pRc
374dc 20 3d 20 72 63 3b 0a 20 20 69 66 28 20 70 21 3d   = rc;.  if( p!=
374dd 2a 70 70 20 29 7b 0a 20 20 20 20 2a 70 70 20 3d  *pp ){.    *pp =
374de 20 70 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   p;.    return 1
374df 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
374e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e  ;.}../*.** Token
374e1 69 7a 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d  ize the nul-term
374e2 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 7a 54  inated string zT
374e3 65 78 74 20 61 6e 64 20 61 64 64 20 61 6c 6c 20  ext and add all 
374e4 74 6f 6b 65 6e 73 20 74 6f 20 74 68 65 0a 2a 2a  tokens to the.**
374e5 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68   pending-terms h
374e6 61 73 68 2d 74 61 62 6c 65 2e 20 54 68 65 20 64  ash-table. The d
374e7 6f 63 69 64 20 75 73 65 64 20 69 73 20 74 68 61  ocid used is tha
374e8 74 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72  t currently stor
374e9 65 64 20 69 6e 0a 2a 2a 20 70 2d 3e 69 50 72 65  ed in.** p->iPre
374ea 76 44 6f 63 69 64 2c 20 61 6e 64 20 74 68 65 20  vDocid, and the 
374eb 63 6f 6c 75 6d 6e 20 69 73 20 73 70 65 63 69 66  column is specif
374ec 69 65 64 20 62 79 20 61 72 67 75 6d 65 6e 74 20  ied by argument 
374ed 69 43 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  iCol..**.** If s
374ee 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54  uccessful, SQLIT
374ef 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
374f0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20  . Otherwise, an 
374f1 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
374f2 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
374f3 20 66 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d   fts3PendingTerm
374f4 73 41 64 64 28 46 74 73 33 54 61 62 6c 65 20 2a  sAdd(Fts3Table *
374f5 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
374f6 54 65 78 74 2c 20 69 6e 74 20 69 43 6f 6c 29 7b  Text, int iCol){
374f7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
374f8 20 69 53 74 61 72 74 3b 0a 20 20 69 6e 74 20 69   iStart;.  int i
374f9 45 6e 64 3b 0a 20 20 69 6e 74 20 69 50 6f 73 3b  End;.  int iPos;
374fa 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ..  char const *
374fb 7a 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54  zToken;.  int nT
374fc 6f 6b 65 6e 3b 0a 0a 20 20 73 71 6c 69 74 65 33  oken;..  sqlite3
374fd 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
374fe 65 6e 69 7a 65 72 20 3d 20 70 2d 3e 70 54 6f 6b  enizer = p->pTok
374ff 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65  enizer;.  sqlite
37500 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
37501 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c  le const *pModul
37502 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e  e = pTokenizer->
37503 70 4d 6f 64 75 6c 65 3b 0a 20 20 73 71 6c 69 74  pModule;.  sqlit
37504 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
37505 73 6f 72 20 2a 70 43 73 72 3b 0a 20 20 69 6e 74  sor *pCsr;.  int
37506 20 28 2a 78 4e 65 78 74 29 28 73 71 6c 69 74 65   (*xNext)(sqlite
37507 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
37508 6f 72 20 2a 70 43 75 72 73 6f 72 2c 0a 20 20 20  or *pCursor,.   
37509 20 20 20 63 6f 6e 73 74 20 63 68 61 72 2a 2a 2c     const char**,
3750a 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  int*,int*,int*,i
3750b 6e 74 2a 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  nt*);..  assert(
3750c 20 70 54 6f 6b 65 6e 69 7a 65 72 20 26 26 20 70   pTokenizer && p
3750d 4d 6f 64 75 6c 65 20 29 3b 0a 0a 20 20 72 63 20  Module );..  rc 
3750e 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e  = pModule->xOpen
3750f 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 54 65  (pTokenizer, zTe
37510 78 74 2c 20 2d 31 2c 20 26 70 43 73 72 29 3b 0a  xt, -1, &pCsr);.
37511 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
37512 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
37513 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 73 72  n rc;.  }.  pCsr
37514 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70  ->pTokenizer = p
37515 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20 78 4e  Tokenizer;..  xN
37516 65 78 74 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78  ext = pModule->x
37517 4e 65 78 74 3b 0a 20 20 77 68 69 6c 65 28 20 53  Next;.  while( S
37518 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20  QLITE_OK==rc.   
37519 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
3751a 3d 28 72 63 20 3d 20 78 4e 65 78 74 28 70 43 73  =(rc = xNext(pCs
3751b 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f  r, &zToken, &nTo
3751c 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26 69  ken, &iStart, &i
3751d 45 6e 64 2c 20 26 69 50 6f 73 29 29 0a 20 20 29  End, &iPos)).  )
3751e 7b 0a 20 20 20 20 50 65 6e 64 69 6e 67 4c 69 73  {.    PendingLis
3751f 74 20 2a 70 4c 69 73 74 3b 0a 0a 20 20 20 20 2f  t *pList;..    /
37520 2a 20 50 6f 73 69 74 69 6f 6e 73 20 63 61 6e 6e  * Positions cann
37521 6f 74 20 62 65 20 6e 65 67 61 74 69 76 65 3b 20  ot be negative; 
37522 77 65 20 75 73 65 20 2d 31 20 61 73 20 61 20 74  we use -1 as a t
37523 65 72 6d 69 6e 61 74 6f 72 20 69 6e 74 65 72 6e  erminator intern
37524 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a 20 54 6f 6b  ally..    ** Tok
37525 65 6e 73 20 6d 75 73 74 20 68 61 76 65 20 61 20  ens must have a 
37526 6e 6f 6e 2d 7a 65 72 6f 20 6c 65 6e 67 74 68 2e  non-zero length.
37527 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
37528 69 50 6f 73 3c 30 20 7c 7c 20 21 7a 54 6f 6b 65  iPos<0 || !zToke
37529 6e 20 7c 7c 20 6e 54 6f 6b 65 6e 3c 3d 30 20 29  n || nToken<=0 )
3752a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
3752b 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
3752c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
3752d 20 20 20 70 4c 69 73 74 20 3d 20 28 50 65 6e 64     pList = (Pend
3752e 69 6e 67 4c 69 73 74 20 2a 29 66 74 73 33 48 61  ingList *)fts3Ha
3752f 73 68 46 69 6e 64 28 26 70 2d 3e 70 65 6e 64 69  shFind(&p->pendi
37530 6e 67 54 65 72 6d 73 2c 20 7a 54 6f 6b 65 6e 2c  ngTerms, zToken,
37531 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 69 66   nToken);.    if
37532 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  ( pList ){.     
37533 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   p->nPendingData
37534 20 2d 3d 20 28 70 4c 69 73 74 2d 3e 6e 44 61 74   -= (pList->nDat
37535 61 20 2b 20 6e 54 6f 6b 65 6e 20 2b 20 73 69 7a  a + nToken + siz
37536 65 6f 66 28 46 74 73 33 48 61 73 68 45 6c 65 6d  eof(Fts3HashElem
37537 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ));.    }.    if
37538 28 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73  ( fts3PendingLis
37539 74 41 70 70 65 6e 64 28 26 70 4c 69 73 74 2c 20  tAppend(&pList, 
3753a 70 2d 3e 69 50 72 65 76 44 6f 63 69 64 2c 20 69  p->iPrevDocid, i
3753b 43 6f 6c 2c 20 69 50 6f 73 2c 20 26 72 63 29 20  Col, iPos, &rc) 
3753c 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69  ){.      if( pLi
3753d 73 74 3d 3d 66 74 73 33 48 61 73 68 49 6e 73 65  st==fts3HashInse
3753e 72 74 28 26 70 2d 3e 70 65 6e 64 69 6e 67 54 65  rt(&p->pendingTe
3753f 72 6d 73 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f  rms, zToken, nTo
37540 6b 65 6e 2c 20 70 4c 69 73 74 29 20 29 7b 0a 20  ken, pList) ){. 
37541 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63         /* Malloc
37542 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20 69 6e   failed while in
37543 73 65 72 74 69 6e 67 20 74 68 65 20 6e 65 77 20  serting the new 
37544 65 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20  entry. This can 
37545 6f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20 2a 2a  only .        **
37546 20 68 61 70 70 65 6e 20 69 66 20 74 68 65 72 65   happen if there
37547 20 77 61 73 20 6e 6f 20 70 72 65 76 69 6f 75 73   was no previous
37548 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20   entry for this 
37549 74 6f 6b 65 6e 2e 0a 20 20 20 20 20 20 20 20 2a  token..        *
3754a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
3754b 28 20 30 3d 3d 66 74 73 33 48 61 73 68 46 69 6e  ( 0==fts3HashFin
3754c 64 28 26 70 2d 3e 70 65 6e 64 69 6e 67 54 65 72  d(&p->pendingTer
3754d 6d 73 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  ms, zToken, nTok
3754e 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 20 20 73  en) );.        s
3754f 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 69 73  qlite3_free(pLis
37550 74 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  t);.        rc =
37551 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
37552 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
37553 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
37554 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e  OK ){.      p->n
37555 50 65 6e 64 69 6e 67 44 61 74 61 20 2b 3d 20 28  PendingData += (
37556 70 4c 69 73 74 2d 3e 6e 44 61 74 61 20 2b 20 6e  pList->nData + n
37557 54 6f 6b 65 6e 20 2b 20 73 69 7a 65 6f 66 28 46  Token + sizeof(F
37558 74 73 33 48 61 73 68 45 6c 65 6d 29 29 3b 0a 20  ts3HashElem));. 
37559 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 4d 6f 64     }.  }..  pMod
3755a 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 73 72  ule->xClose(pCsr
3755b 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d  );.  return (rc=
3755c 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53  =SQLITE_DONE ? S
3755d 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a  QLITE_OK : rc);.
3755e 7d 0a 0a 2f 2a 20 0a 2a 2a 20 43 61 6c 6c 69 6e  }../* .** Callin
3755f 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  g this function 
37560 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 73  indicates that s
37561 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
37562 74 6f 20 0a 2a 2a 20 66 74 73 33 50 65 6e 64 69  to .** fts3Pendi
37563 6e 67 54 65 72 6d 73 41 64 64 28 29 20 61 72 65  ngTermsAdd() are
37564 20 74 6f 20 61 64 64 20 74 65 72 6d 2f 70 6f 73   to add term/pos
37565 69 74 69 6f 6e 2d 6c 69 73 74 20 70 61 69 72 73  ition-list pairs
37566 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 6e 74   for the.** cont
37567 65 6e 74 73 20 6f 66 20 74 68 65 20 64 6f 63 75  ents of the docu
37568 6d 65 6e 74 20 77 69 74 68 20 64 6f 63 69 64 20  ment with docid 
37569 69 44 6f 63 69 64 2e 0a 2a 2f 0a 73 74 61 74 69  iDocid..*/.stati
3756a 63 20 69 6e 74 20 66 74 73 33 50 65 6e 64 69 6e  c int fts3Pendin
3756b 67 54 65 72 6d 73 44 6f 63 69 64 28 46 74 73 33  gTermsDocid(Fts3
3756c 54 61 62 6c 65 20 2a 70 2c 20 73 71 6c 69 74 65  Table *p, sqlite
3756d 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a  _int64 iDocid){.
3756e 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
3756f 20 45 78 70 6c 6f 72 65 20 77 68 65 74 68 65 72   Explore whether
37570 20 70 61 72 74 69 61 6c 6c 79 20 66 6c 75 73 68   partially flush
37571 69 6e 67 20 74 68 65 20 62 75 66 66 65 72 20 6f  ing the buffer o
37572 6e 0a 20 20 2a 2a 20 66 6f 72 63 65 64 2d 66 6c  n.  ** forced-fl
37573 75 73 68 20 77 6f 75 6c 64 20 70 72 6f 76 69 64  ush would provid
37574 65 20 62 65 74 74 65 72 20 70 65 72 66 6f 72 6d  e better perform
37575 61 6e 63 65 2e 20 20 49 20 73 75 73 70 65 63 74  ance.  I suspect
37576 20 74 68 61 74 20 69 66 0a 20 20 2a 2a 20 77 65   that if.  ** we
37577 20 6f 72 64 65 72 65 64 20 74 68 65 20 64 6f 63   ordered the doc
37578 6c 69 73 74 73 20 62 79 20 73 69 7a 65 20 61 6e  lists by size an
37579 64 20 66 6c 75 73 68 65 64 20 74 68 65 20 6c 61  d flushed the la
3757a 72 67 65 73 74 20 75 6e 74 69 6c 20 74 68 65 0a  rgest until the.
3757b 20 20 2a 2a 20 62 75 66 66 65 72 20 77 61 73 20    ** buffer was 
3757c 68 61 6c 66 20 65 6d 70 74 79 2c 20 74 68 61 74  half empty, that
3757d 20 77 6f 75 6c 64 20 6c 65 74 20 74 68 65 20 6c   would let the l
3757e 65 73 73 20 66 72 65 71 75 65 6e 74 20 74 65 72  ess frequent ter
3757f 6d 73 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 65  ms.  ** generate
37580 20 6c 6f 6e 67 65 72 20 64 6f 63 6c 69 73 74 73   longer doclists
37581 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44 6f  ..  */.  if( iDo
37582 63 69 64 3c 3d 70 2d 3e 69 50 72 65 76 44 6f 63  cid<=p->iPrevDoc
37583 69 64 20 7c 7c 20 70 2d 3e 6e 50 65 6e 64 69 6e  id || p->nPendin
37584 67 44 61 74 61 3e 46 54 53 33 5f 4d 41 58 5f 50  gData>FTS3_MAX_P
37585 45 4e 44 49 4e 47 5f 44 41 54 41 20 29 7b 0a 20  ENDING_DATA ){. 
37586 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
37587 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67 54 65  te3Fts3PendingTe
37588 72 6d 73 46 6c 75 73 68 28 70 29 3b 0a 20 20 20  rmsFlush(p);.   
37589 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3758a 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
3758b 20 20 7d 0a 20 20 70 2d 3e 69 50 72 65 76 44 6f    }.  p->iPrevDo
3758c 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20  cid = iDocid;.  
3758d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3758e 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
3758f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
37590 46 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73  Fts3PendingTerms
37591 43 6c 65 61 72 28 46 74 73 33 54 61 62 6c 65 20  Clear(Fts3Table 
37592 2a 70 29 7b 0a 20 20 46 74 73 33 48 61 73 68 45  *p){.  Fts3HashE
37593 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 66 6f  lem *pElem;.  fo
37594 72 28 70 45 6c 65 6d 3d 66 74 73 33 48 61 73 68  r(pElem=fts3Hash
37595 46 69 72 73 74 28 26 70 2d 3e 70 65 6e 64 69 6e  First(&p->pendin
37596 67 54 65 72 6d 73 29 3b 20 70 45 6c 65 6d 3b 20  gTerms); pElem; 
37597 70 45 6c 65 6d 3d 66 74 73 33 48 61 73 68 4e 65  pElem=fts3HashNe
37598 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20  xt(pElem)){.    
37599 73 71 6c 69 74 65 33 5f 66 72 65 65 28 66 74 73  sqlite3_free(fts
3759a 33 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29  3HashData(pElem)
3759b 29 3b 0a 20 20 7d 0a 20 20 66 74 73 33 48 61 73  );.  }.  fts3Has
3759c 68 43 6c 65 61 72 28 26 70 2d 3e 70 65 6e 64 69  hClear(&p->pendi
3759d 6e 67 54 65 72 6d 73 29 3b 0a 20 20 70 2d 3e 6e  ngTerms);.  p->n
3759e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 30 3b  PendingData = 0;
3759f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
375a0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
375a1 64 20 62 79 20 74 68 65 20 78 55 70 64 61 74 65  d by the xUpdate
375a2 28 29 20 6d 65 74 68 6f 64 20 61 73 20 70 61 72  () method as par
375a3 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 0a 2a  t of an INSERT.*
375a4 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 74 20  * operation. It 
375a5 61 64 64 73 20 65 6e 74 72 69 65 73 20 66 6f 72  adds entries for
375a6 20 65 61 63 68 20 74 65 72 6d 20 69 6e 20 74 68   each term in th
375a7 65 20 6e 65 77 20 72 65 63 6f 72 64 20 74 6f 20  e new record to 
375a8 74 68 65 0a 2a 2a 20 70 65 6e 64 69 6e 67 54 65  the.** pendingTe
375a9 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  rms hash table..
375aa 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 61  **.** Argument a
375ab 70 56 61 6c 20 69 73 20 74 68 65 20 73 61 6d 65  pVal is the same
375ac 20 61 73 20 74 68 65 20 73 69 6d 69 6c 61 72 6c   as the similarl
375ad 79 20 6e 61 6d 65 64 20 61 72 67 75 6d 65 6e 74  y named argument
375ae 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 66 74   passed to.** ft
375af 73 33 49 6e 73 65 72 74 44 61 74 61 28 29 2e 20  s3InsertData(). 
375b0 50 61 72 61 6d 65 74 65 72 20 69 44 6f 63 69 64  Parameter iDocid
375b1 20 69 73 20 74 68 65 20 64 6f 63 69 64 20 6f 66   is the docid of
375b2 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2f   the new row..*/
375b3 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
375b4 49 6e 73 65 72 74 54 65 72 6d 73 28 46 74 73 33  InsertTerms(Fts3
375b5 54 61 62 6c 65 20 2a 70 2c 20 73 71 6c 69 74 65  Table *p, sqlite
375b6 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 29  3_value **apVal)
375b7 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
375b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
375b9 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
375ba 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f  variable */.  fo
375bb 72 28 69 3d 32 3b 20 69 3c 70 2d 3e 6e 43 6f 6c  r(i=2; i<p->nCol
375bc 75 6d 6e 2b 32 3b 20 69 2b 2b 29 7b 0a 20 20 20  umn+2; i++){.   
375bd 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65   const char *zTe
375be 78 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  xt = (const char
375bf 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
375c0 5f 74 65 78 74 28 61 70 56 61 6c 5b 69 5d 29 3b  _text(apVal[i]);
375c1 0a 20 20 20 20 69 66 28 20 7a 54 65 78 74 20 29  .    if( zText )
375c2 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d  {.      int rc =
375c3 20 66 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d   fts3PendingTerm
375c4 73 41 64 64 28 70 2c 20 7a 54 65 78 74 2c 20 69  sAdd(p, zText, i
375c5 2d 32 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  -2);.      if( r
375c6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
375c7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
375c8 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
375c9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
375ca 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
375cb 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
375cc 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
375cd 20 78 55 70 64 61 74 65 28 29 20 6d 65 74 68 6f   xUpdate() metho
375ce 64 20 66 6f 72 20 61 6e 20 49 4e 53 45 52 54 20  d for an INSERT 
375cf 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68  operation..** Th
375d0 65 20 61 70 56 61 6c 20 70 61 72 61 6d 65 74 65  e apVal paramete
375d1 72 20 69 73 20 70 61 73 73 65 64 20 61 20 63 6f  r is passed a co
375d2 70 79 20 6f 66 20 74 68 65 20 61 70 56 61 6c 20  py of the apVal 
375d3 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
375d4 62 79 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f 20  by.** SQLite to 
375d5 74 68 65 20 78 55 70 64 61 74 65 28 29 20 6d 65  the xUpdate() me
375d6 74 68 6f 64 2e 20 69 2e 65 3a 0a 2a 2a 0a 2a 2a  thod. i.e:.**.**
375d7 20 20 20 61 70 56 61 6c 5b 30 5d 20 20 20 20 20     apVal[0]     
375d8 20 20 20 20 20 20 20 20 20 20 20 4e 6f 74 20 75             Not u
375d9 73 65 64 20 66 6f 72 20 49 4e 53 45 52 54 2e 0a  sed for INSERT..
375da 2a 2a 20 20 20 61 70 56 61 6c 5b 31 5d 20 20 20  **   apVal[1]   
375db 20 20 20 20 20 20 20 20 20 20 20 20 20 72 6f 77               row
375dc 69 64 0a 2a 2a 20 20 20 61 70 56 61 6c 5b 32 5d  id.**   apVal[2]
375dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
375de 4c 65 66 74 2d 6d 6f 73 74 20 75 73 65 72 2d 64  Left-most user-d
375df 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a  efined column.**
375e0 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 61 70 56 61     ....**   apVa
375e1 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 20  l[p->nColumn+1] 
375e2 20 20 20 20 52 69 67 68 74 2d 6d 6f 73 74 20 75      Right-most u
375e3 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 75  ser-defined colu
375e4 6d 6e 0a 2a 2a 20 20 20 61 70 56 61 6c 5b 70 2d  mn.**   apVal[p-
375e5 3e 6e 43 6f 6c 75 6d 6e 2b 32 5d 20 20 20 20 20  >nColumn+2]     
375e6 48 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 20 77 69  Hidden column wi
375e7 74 68 20 73 61 6d 65 20 6e 61 6d 65 20 61 73 20  th same name as 
375e8 74 61 62 6c 65 0a 2a 2a 20 20 20 61 70 56 61 6c  table.**   apVal
375e9 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 33 5d 20 20  [p->nColumn+3]  
375ea 20 20 20 48 69 64 64 65 6e 20 22 64 6f 63 69 64     Hidden "docid
375eb 22 20 63 6f 6c 75 6d 6e 20 28 61 6c 69 61 73 20  " column (alias 
375ec 66 6f 72 20 72 6f 77 69 64 29 0a 2a 2f 0a 73 74  for rowid).*/.st
375ed 61 74 69 63 20 69 6e 74 20 66 74 73 33 49 6e 73  atic int fts3Ins
375ee 65 72 74 44 61 74 61 28 0a 20 20 46 74 73 33 54  ertData(.  Fts3T
375ef 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20  able *p,        
375f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75             /* Fu
375f1 6c 6c 2d 74 65 78 74 20 74 61 62 6c 65 20 2a 2f  ll-text table */
375f2 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
375f3 20 2a 2a 61 70 56 61 6c 2c 20 20 20 20 20 20 20   **apVal,       
375f4 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 76     /* Array of v
375f5 61 6c 75 65 73 20 74 6f 20 69 6e 73 65 72 74 20  alues to insert 
375f6 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
375f7 36 34 20 2a 70 69 44 6f 63 69 64 20 20 20 20 20  64 *piDocid     
375f8 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 44 6f 63       /* OUT: Doc
375f9 69 64 20 66 6f 72 20 72 6f 77 20 6a 75 73 74 20  id for row just 
375fa 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20  inserted */.){. 
375fb 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
375fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
375fd 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
375fe 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
375ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37600 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
37601 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 73   variable */.  s
37602 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 43 6f  qlite3_stmt *pCo
37603 6e 74 65 6e 74 49 6e 73 65 72 74 3b 20 20 20 2f  ntentInsert;   /
37604 2a 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 5f  * INSERT INTO %_
37605 63 6f 6e 74 65 6e 74 20 56 41 4c 55 45 53 28 2e  content VALUES(.
37606 2e 2e 29 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63  ..) */..  /* Loc
37607 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ate the statemen
37608 74 20 68 61 6e 64 6c 65 20 75 73 65 64 20 74 6f  t handle used to
37609 20 69 6e 73 65 72 74 20 64 61 74 61 20 69 6e 74   insert data int
3760a 6f 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 0a  o the %_content.
3760b 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65 20    ** table. The 
3760c 53 51 4c 20 66 6f 72 20 74 68 69 73 20 73 74 61  SQL for this sta
3760d 74 65 6d 65 6e 74 20 69 73 3a 0a 20 20 2a 2a 0a  tement is:.  **.
3760e 20 20 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e    **   INSERT IN
3760f 54 4f 20 25 5f 63 6f 6e 74 65 6e 74 20 56 41 4c  TO %_content VAL
37610 55 45 53 28 3f 2c 20 3f 2c 20 3f 2c 20 2e 2e 2e  UES(?, ?, ?, ...
37611 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ).  **.  ** The 
37612 73 74 61 74 65 6d 65 6e 74 20 66 65 61 74 75 72  statement featur
37613 65 73 20 4e 20 27 3f 27 20 76 61 72 69 61 62 6c  es N '?' variabl
37614 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74  es, where N is t
37615 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 65  he number of use
37616 72 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 63  r.  ** defined c
37617 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 46 54  olumns in the FT
37618 53 33 20 74 61 62 6c 65 2c 20 70 6c 75 73 20 6f  S3 table, plus o
37619 6e 65 20 66 6f 72 20 74 68 65 20 64 6f 63 69 64  ne for the docid
3761a 20 66 69 65 6c 64 2e 0a 20 20 2a 2f 0a 20 20 72   field..  */.  r
3761b 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28  c = fts3SqlStmt(
3761c 70 2c 20 53 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49  p, SQL_CONTENT_I
3761d 4e 53 45 52 54 2c 20 26 70 43 6f 6e 74 65 6e 74  NSERT, &pContent
3761e 49 6e 73 65 72 74 2c 20 26 61 70 56 61 6c 5b 31  Insert, &apVal[1
3761f 5d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ]);.  if( rc!=SQ
37620 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
37621 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
37622 20 2f 2a 20 54 68 65 72 65 20 69 73 20 61 20 71   /* There is a q
37623 75 69 72 6b 20 68 65 72 65 2e 20 54 68 65 20 75  uirk here. The u
37624 73 65 72 73 20 49 4e 53 45 52 54 20 73 74 61 74  sers INSERT stat
37625 65 6d 65 6e 74 20 6d 61 79 20 68 61 76 65 20 73  ement may have s
37626 70 65 63 69 66 69 65 64 0a 20 20 2a 2a 20 61 20  pecified.  ** a 
37627 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 22 72  value for the "r
37628 6f 77 69 64 22 20 66 69 65 6c 64 2c 20 66 6f 72  owid" field, for
37629 20 74 68 65 20 22 64 6f 63 69 64 22 20 66 69 65   the "docid" fie
3762a 6c 64 2c 20 6f 72 20 66 6f 72 20 62 6f 74 68 2e  ld, or for both.
3762b 0a 20 20 2a 2a 20 57 68 69 63 68 20 69 73 20 61  .  ** Which is a
3762c 20 70 72 6f 62 6c 65 6d 2c 20 73 69 6e 63 65 20   problem, since 
3762d 22 72 6f 77 69 64 22 20 61 6e 64 20 22 64 6f 63  "rowid" and "doc
3762e 69 64 22 20 61 72 65 20 61 6c 69 61 73 65 73 20  id" are aliases 
3762f 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d  for the.  ** sam
37630 65 20 76 61 6c 75 65 2e 20 46 6f 72 20 65 78 61  e value. For exa
37631 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  mple:.  **.  ** 
37632 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 66 74    INSERT INTO ft
37633 73 33 74 62 6c 28 72 6f 77 69 64 2c 20 64 6f 63  s3tbl(rowid, doc
37634 69 64 29 20 56 41 4c 55 45 53 28 31 2c 20 32 29  id) VALUES(1, 2)
37635 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 46  ;.  **.  ** In F
37636 54 53 33 2c 20 69 66 20 61 20 6e 6f 6e 2d 4e 55  TS3, if a non-NU
37637 4c 4c 20 64 6f 63 69 64 20 76 61 6c 75 65 20 69  LL docid value i
37638 73 20 73 70 65 63 69 66 69 65 64 2c 20 69 74 20  s specified, it 
37639 69 73 20 74 68 65 20 76 61 6c 75 65 0a 20 20 2a  is the value.  *
3763a 2a 20 69 6e 73 65 72 74 65 64 2e 20 4f 74 68 65  * inserted. Othe
3763b 72 77 69 73 65 2c 20 74 68 65 20 72 6f 77 69 64  rwise, the rowid
3763c 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 2e 0a   value is used..
3763d 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54    */.  if( SQLIT
3763e 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f  E_NULL!=sqlite3_
3763f 76 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c  value_type(apVal
37640 5b 33 2b 70 2d 3e 6e 43 6f 6c 75 6d 6e 5d 29 20  [3+p->nColumn]) 
37641 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
37642 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 70  te3_bind_value(p
37643 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 2c 20 31  ContentInsert, 1
37644 2c 20 61 70 56 61 6c 5b 33 2b 70 2d 3e 6e 43 6f  , apVal[3+p->nCo
37645 6c 75 6d 6e 5d 29 3b 0a 20 20 20 20 69 66 28 20  lumn]);.    if( 
37646 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
37647 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
37648 20 20 2f 2a 20 45 78 65 63 75 74 65 20 74 68 65    /* Execute the
37649 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e   statement to in
3764a 73 65 72 74 20 74 68 65 20 72 65 63 6f 72 64 2e  sert the record.
3764b 20 53 65 74 20 2a 70 69 44 6f 63 69 64 20 74 6f   Set *piDocid to
3764c 20 74 68 65 20 0a 20 20 2a 2a 20 6e 65 77 20 64   the .  ** new d
3764d 6f 63 69 64 20 76 61 6c 75 65 2e 20 0a 20 20 2a  ocid value. .  *
3764e 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  /.  sqlite3_step
3764f 28 70 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 29  (pContentInsert)
37650 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
37651 5f 72 65 73 65 74 28 70 43 6f 6e 74 65 6e 74 49  _reset(pContentI
37652 6e 73 65 72 74 29 3b 0a 0a 20 20 2a 70 69 44 6f  nsert);..  *piDo
37653 63 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61  cid = sqlite3_la
37654 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
37655 70 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e  p->db);.  return
37656 20 72 63 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20   rc;.}..../*.** 
37657 52 65 6d 6f 76 65 20 61 6c 6c 20 64 61 74 61 20  Remove all data 
37658 66 72 6f 6d 20 74 68 65 20 46 54 53 33 20 74 61  from the FTS3 ta
37659 62 6c 65 2e 20 43 6c 65 61 72 20 74 68 65 20 68  ble. Clear the h
3765a 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  ash table contai
3765b 6e 69 6e 67 0a 2a 2a 20 70 65 6e 64 69 6e 67 20  ning.** pending 
3765c 74 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  terms..*/.static
3765d 20 69 6e 74 20 66 74 73 33 44 65 6c 65 74 65 41   int fts3DeleteA
3765e 6c 6c 28 46 74 73 33 54 61 62 6c 65 20 2a 70 29  ll(Fts3Table *p)
3765f 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
37660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37661 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
37662 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 44 69 73 63  de */..  /* Disc
37663 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ard the contents
37664 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67 2d   of the pending-
37665 74 65 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65  terms hash table
37666 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46 74  . */.  sqlite3Ft
37667 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 43 6c  s3PendingTermsCl
37668 65 61 72 28 70 29 3b 0a 0a 20 20 2f 2a 20 44 65  ear(p);..  /* De
37669 6c 65 74 65 20 65 76 65 72 79 74 68 69 6e 67 20  lete everything 
3766a 66 72 6f 6d 20 74 68 65 20 25 5f 63 6f 6e 74 65  from the %_conte
3766b 6e 74 2c 20 25 5f 73 65 67 6d 65 6e 74 73 20 61  nt, %_segments a
3766c 6e 64 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c  nd %_segdir tabl
3766d 65 73 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 74  es. */.  rc = ft
3766e 73 33 53 71 6c 45 78 65 63 28 70 2c 20 53 51 4c  s3SqlExec(p, SQL
3766f 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 43 4f 4e 54  _DELETE_ALL_CONT
37670 45 4e 54 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  ENT, 0);.  if( r
37671 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
37672 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
37673 45 78 65 63 28 70 2c 20 53 51 4c 5f 44 45 4c 45  Exec(p, SQL_DELE
37674 54 45 5f 41 4c 4c 5f 53 45 47 4d 45 4e 54 53 2c  TE_ALL_SEGMENTS,
37675 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72   0);.  }.  if( r
37676 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
37677 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
37678 45 78 65 63 28 70 2c 20 53 51 4c 5f 44 45 4c 45  Exec(p, SQL_DELE
37679 54 45 5f 41 4c 4c 5f 53 45 47 44 49 52 2c 20 30  TE_ALL_SEGDIR, 0
3767a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
3767b 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
3767c 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 69   first element i
3767d 6e 20 74 68 65 20 61 70 56 61 6c 5b 5d 20 61 72  n the apVal[] ar
3767e 72 61 79 20 69 73 20 61 73 73 75 6d 65 64 20 74  ray is assumed t
3767f 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 64 6f  o contain the do
37680 63 69 64 0a 2a 2a 20 28 61 6e 20 69 6e 74 65 67  cid.** (an integ
37681 65 72 29 20 6f 66 20 61 20 72 6f 77 20 61 62 6f  er) of a row abo
37682 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  ut to be deleted
37683 2e 20 52 65 6d 6f 76 65 20 61 6c 6c 20 74 65 72  . Remove all ter
37684 6d 73 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 66  ms from the.** f
37685 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 2e 0a  ull-text index..
37686 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
37687 73 33 44 65 6c 65 74 65 54 65 72 6d 73 28 46 74  s3DeleteTerms(Ft
37688 73 33 54 61 62 6c 65 20 2a 70 2c 20 73 71 6c 69  s3Table *p, sqli
37689 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61  te3_value **apVa
3768a 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  l){.  int rc;.  
3768b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
3768c 65 6c 65 63 74 3b 0a 0a 20 20 72 63 20 3d 20 66  elect;..  rc = f
3768d 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51  ts3SqlStmt(p, SQ
3768e 4c 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45 4e 54  L_SELECT_CONTENT
3768f 5f 42 59 5f 52 4f 57 49 44 2c 20 26 70 53 65 6c  _BY_ROWID, &pSel
37690 65 63 74 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69  ect, apVal);.  i
37691 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
37692 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49   ){.    if( SQLI
37693 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f  TE_ROW==sqlite3_
37694 73 74 65 70 28 70 53 65 6c 65 63 74 29 20 29 7b  step(pSelect) ){
37695 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
37696 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d      for(i=1; i<=
37697 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  p->nColumn; i++)
37698 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  {.        const 
37699 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 28 63  char *zText = (c
3769a 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
3769b 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
3769c 70 53 65 6c 65 63 74 2c 20 69 29 3b 0a 20 20 20  pSelect, i);.   
3769d 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 50 65       rc = fts3Pe
3769e 6e 64 69 6e 67 54 65 72 6d 73 41 64 64 28 70 2c  ndingTermsAdd(p,
3769f 20 7a 54 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20   zText, -1);.   
376a0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
376a1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
376a2 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65      sqlite3_rese
376a3 74 28 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  t(pSelect);.    
376a4 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
376a5 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
376a6 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
376a7 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72  return sqlite3_r
376a8 65 73 65 74 28 70 53 65 6c 65 63 74 29 3b 0a 7d  eset(pSelect);.}
376a9 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
376aa 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 6f 20 61  declaration to a
376ab 63 63 6f 75 6e 74 20 66 6f 72 20 74 68 65 20 63  ccount for the c
376ac 69 72 63 75 6c 61 72 20 64 65 70 65 6e 64 65 6e  ircular dependen
376ad 63 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 66 75  cy between.** fu
376ae 6e 63 74 69 6f 6e 73 20 66 74 73 33 53 65 67 6d  nctions fts3Segm
376af 65 6e 74 4d 65 72 67 65 28 29 20 61 6e 64 20 66  entMerge() and f
376b0 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67 64 69  ts3AllocateSegdi
376b1 72 49 64 78 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  rIdx()..*/.stati
376b2 63 20 69 6e 74 20 66 74 73 33 53 65 67 6d 65 6e  c int fts3Segmen
376b3 74 4d 65 72 67 65 28 46 74 73 33 54 61 62 6c 65  tMerge(Fts3Table
376b4 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 0a 2a   *, int);../* .*
376b5 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
376b6 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20  allocates a new 
376b7 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20 69 6e 64  level iLevel ind
376b8 65 78 20 69 6e 20 74 68 65 20 73 65 67 64 69 72  ex in the segdir
376b9 20 74 61 62 6c 65 2e 0a 2a 2a 20 55 73 75 61 6c   table..** Usual
376ba 6c 79 2c 20 69 6e 64 65 78 65 73 20 61 72 65 20  ly, indexes are 
376bb 61 6c 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e  allocated within
376bc 20 61 20 6c 65 76 65 6c 20 73 65 71 75 65 6e 74   a level sequent
376bd 69 61 6c 6c 79 20 73 74 61 72 74 69 6e 67 0a 2a  ially starting.*
376be 2a 20 77 69 74 68 20 30 2c 20 73 6f 20 74 68 65  * with 0, so the
376bf 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 64 65 78   allocated index
376c0 20 69 73 20 6f 6e 65 20 67 72 65 61 74 65 72 20   is one greater 
376c1 74 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 72  than the value r
376c2 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 3a 0a 2a  eturned.** by:.*
376c3 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 61  *.**   SELECT ma
376c4 78 28 69 64 78 29 20 46 52 4f 4d 20 25 5f 73 65  x(idx) FROM %_se
376c5 67 64 69 72 20 57 48 45 52 45 20 6c 65 76 65 6c  gdir WHERE level
376c6 20 3d 20 3a 69 4c 65 76 65 6c 0a 2a 2a 0a 2a 2a   = :iLevel.**.**
376c7 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65   However, if the
376c8 72 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 46  re are already F
376c9 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20  TS3_MERGE_COUNT 
376ca 69 6e 64 65 78 65 73 20 61 74 20 74 68 65 20 72  indexes at the r
376cb 65 71 75 65 73 74 65 64 0a 2a 2a 20 6c 65 76 65  equested.** leve
376cc 6c 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72 67  l, they are merg
376cd 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ed into a single
376ce 20 6c 65 76 65 6c 20 28 69 4c 65 76 65 6c 2b 31   level (iLevel+1
376cf 29 20 73 65 67 6d 65 6e 74 20 61 6e 64 20 74 68  ) segment and th
376d0 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  e .** allocated 
376d1 69 6e 64 65 78 20 69 73 20 30 2e 0a 2a 2a 0a 2a  index is 0..**.*
376d2 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
376d3 20 2a 70 69 49 64 78 20 69 73 20 73 65 74 20 74   *piIdx is set t
376d4 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  o the allocated 
376d5 69 6e 64 65 78 20 73 6c 6f 74 20 61 6e 64 20 53  index slot and S
376d6 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 72 65 74 75  QLITE_OK.** retu
376d7 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
376d8 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
376d9 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
376da 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
376db 20 66 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67   fts3AllocateSeg
376dc 64 69 72 49 64 78 28 46 74 73 33 54 61 62 6c 65  dirIdx(Fts3Table
376dd 20 2a 70 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *p, int iLevel,
376de 20 69 6e 74 20 2a 70 69 49 64 78 29 7b 0a 20 20   int *piIdx){.  
376df 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
376e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
376e1 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a  /* Return Code *
376e2 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
376e3 20 2a 70 4e 65 78 74 49 64 78 3b 20 20 20 20 20   *pNextIdx;     
376e4 20 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6f 72      /* Query for
376e5 20 6e 65 78 74 20 69 64 78 20 61 74 20 6c 65 76   next idx at lev
376e6 65 6c 20 69 4c 65 76 65 6c 20 2a 2f 0a 20 20 69  el iLevel */.  i
376e7 6e 74 20 69 4e 65 78 74 3b 20 20 20 20 20 20 20  nt iNext;       
376e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
376e9 2a 20 52 65 73 75 6c 74 20 6f 66 20 71 75 65 72  * Result of quer
376ea 79 20 70 4e 65 78 74 49 64 78 20 2a 2f 0a 0a 20  y pNextIdx */.. 
376eb 20 2f 2a 20 53 65 74 20 76 61 72 69 61 62 6c 65   /* Set variable
376ec 20 69 4e 65 78 74 20 74 6f 20 74 68 65 20 6e 65   iNext to the ne
376ed 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 67  xt available seg
376ee 64 69 72 20 69 6e 64 65 78 20 61 74 20 6c 65 76  dir index at lev
376ef 65 6c 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20 20  el iLevel. */.  
376f0 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
376f1 28 70 2c 20 53 51 4c 5f 4e 45 58 54 5f 53 45 47  (p, SQL_NEXT_SEG
376f2 4d 45 4e 54 5f 49 4e 44 45 58 2c 20 26 70 4e 65  MENT_INDEX, &pNe
376f3 78 74 49 64 78 2c 20 30 29 3b 0a 20 20 69 66 28  xtIdx, 0);.  if(
376f4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
376f5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69  {.    sqlite3_bi
376f6 6e 64 5f 69 6e 74 28 70 4e 65 78 74 49 64 78 2c  nd_int(pNextIdx,
376f7 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 20   1, iLevel);.   
376f8 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
376f9 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 4e  =sqlite3_step(pN
376fa 65 78 74 49 64 78 29 20 29 7b 0a 20 20 20 20 20  extIdx) ){.     
376fb 20 69 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33   iNext = sqlite3
376fc 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 4e  _column_int64(pN
376fd 65 78 74 49 64 78 2c 20 30 29 3b 0a 20 20 20 20  extIdx, 0);.    
376fe 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
376ff 65 33 5f 72 65 73 65 74 28 70 4e 65 78 74 49 64  e3_reset(pNextId
37700 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  x);.  }..  if( r
37701 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
37702 20 20 20 20 2f 2a 20 49 66 20 69 4e 65 78 74 20      /* If iNext 
37703 69 73 20 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f  is FTS3_MERGE_CO
37704 55 4e 54 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  UNT, indicating 
37705 74 68 61 74 20 6c 65 76 65 6c 20 69 4c 65 76 65  that level iLeve
37706 6c 20 69 73 20 61 6c 72 65 61 64 79 0a 20 20 20  l is already.   
37707 20 2a 2a 20 66 75 6c 6c 2c 20 6d 65 72 67 65 20   ** full, merge 
37708 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 69 6e 20  all segments in 
37709 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20 69 6e 74  level iLevel int
3770a 6f 20 61 20 73 69 6e 67 6c 65 20 69 4c 65 76 65  o a single iLeve
3770b 6c 2b 31 0a 20 20 20 20 2a 2a 20 73 65 67 6d 65  l+1.    ** segme
3770c 6e 74 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20  nt and allocate 
3770d 28 6e 65 77 6c 79 20 66 72 65 65 64 29 20 69 6e  (newly freed) in
3770e 64 65 78 20 30 20 61 74 20 6c 65 76 65 6c 20 69  dex 0 at level i
3770f 4c 65 76 65 6c 2e 20 4f 74 68 65 72 77 69 73 65  Level. Otherwise
37710 2c 0a 20 20 20 20 2a 2a 20 69 66 20 69 4e 65 78  ,.    ** if iNex
37711 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 46  t is less than F
37712 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 2c  TS3_MERGE_COUNT,
37713 20 61 6c 6c 6f 63 61 74 65 20 69 6e 64 65 78 20   allocate index 
37714 69 4e 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  iNext..    */.  
37715 20 20 69 66 28 20 69 4e 65 78 74 3e 3d 46 54 53    if( iNext>=FTS
37716 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 7b  3_MERGE_COUNT ){
37717 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
37718 53 65 67 6d 65 6e 74 4d 65 72 67 65 28 70 2c 20  SegmentMerge(p, 
37719 69 4c 65 76 65 6c 29 3b 0a 20 20 20 20 20 20 2a  iLevel);.      *
3771a 70 69 49 64 78 20 3d 20 30 3b 0a 20 20 20 20 7d  piIdx = 0;.    }
3771b 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 69 49  else{.      *piI
3771c 64 78 20 3d 20 69 4e 65 78 74 3b 0a 20 20 20 20  dx = iNext;.    
3771d 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
3771e 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76  rc;.}../*.** Mov
3771f 65 20 74 68 65 20 69 74 65 72 61 74 6f 72 20 70  e the iterator p
37720 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
37721 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
37722 68 65 20 6e 65 78 74 20 74 65 72 6d 20 69 6e 20  he next term in 
37723 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 2e 20  the.** segment. 
37724 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53  If successful, S
37725 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
37726 72 6e 65 64 2e 20 49 66 20 74 68 65 72 65 20 69  rned. If there i
37727 73 20 6e 6f 20 6e 65 78 74 20 74 65 72 6d 2c 0a  s no next term,.
37728 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20  ** SQLITE_DONE. 
37729 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51  Otherwise, an SQ
3772a 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
3772b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
3772c 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 78 74  ts3SegReaderNext
3772d 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a  (Fts3SegReader *
3772e 70 52 65 61 64 65 72 29 7b 0a 20 20 63 68 61 72  pReader){.  char
3772f 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20   *pNext;        
37730 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
37731 75 72 73 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  ursor variable *
37732 2f 0a 20 20 69 6e 74 20 6e 50 72 65 66 69 78 3b  /.  int nPrefix;
37733 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37734 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
37735 20 62 79 74 65 73 20 69 6e 20 74 65 72 6d 20 70   bytes in term p
37736 72 65 66 69 78 20 2a 2f 0a 20 20 69 6e 74 20 6e  refix */.  int n
37737 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20  Suffix;         
37738 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
37739 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
3773a 20 74 65 72 6d 20 73 75 66 66 69 78 20 2a 2f 0a   term suffix */.
3773b 0a 20 20 69 66 28 20 21 70 52 65 61 64 65 72 2d  .  if( !pReader-
3773c 3e 61 44 6f 63 6c 69 73 74 20 29 7b 0a 20 20 20  >aDoclist ){.   
3773d 20 70 4e 65 78 74 20 3d 20 70 52 65 61 64 65 72   pNext = pReader
3773e 2d 3e 61 4e 6f 64 65 3b 0a 20 20 7d 65 6c 73 65  ->aNode;.  }else
3773f 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 26 70  {.    pNext = &p
37740 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74  Reader->aDoclist
37741 5b 70 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c 69  [pReader->nDocli
37742 73 74 5d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  st];.  }..  if( 
37743 21 70 4e 65 78 74 20 7c 7c 20 70 4e 65 78 74 3e  !pNext || pNext>
37744 3d 26 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  =&pReader->aNode
37745 5b 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 5d  [pReader->nNode]
37746 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a   ){.    int rc;.
37747 20 20 20 20 69 66 28 20 21 70 52 65 61 64 65 72      if( !pReader
37748 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 20  ->pStmt ){.     
37749 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 20   pReader->aNode 
3774a 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 0;.      retur
3774b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
3774c 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
3774d 74 65 33 5f 73 74 65 70 28 70 52 65 61 64 65 72  te3_step(pReader
3774e 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66  ->pStmt);.    if
3774f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
37750 20 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64 65   ){.      pReade
37751 72 2d 3e 61 4e 6f 64 65 20 3d 20 30 3b 0a 20 20  r->aNode = 0;.  
37752 20 20 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d      return (rc==
37753 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51  SQLITE_DONE ? SQ
37754 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 20  LITE_OK : rc);. 
37755 20 20 20 7d 0a 20 20 20 20 70 52 65 61 64 65 72     }.    pReader
37756 2d 3e 6e 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65  ->nNode = sqlite
37757 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70  3_column_bytes(p
37758 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30  Reader->pStmt, 0
37759 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
3775a 61 4e 6f 64 65 20 3d 20 28 63 68 61 72 20 2a 29  aNode = (char *)
3775b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
3775c 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70 53 74  lob(pReader->pSt
3775d 6d 74 2c 20 30 29 3b 0a 20 20 20 20 70 4e 65 78  mt, 0);.    pNex
3775e 74 20 3d 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f  t = pReader->aNo
3775f 64 65 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 4e 65  de;.  }.  .  pNe
37760 78 74 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  xt += sqlite3Fts
37761 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 4e 65  3GetVarint32(pNe
37762 78 74 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20  xt, &nPrefix);. 
37763 20 70 4e 65 78 74 20 2b 3d 20 73 71 6c 69 74 65   pNext += sqlite
37764 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
37765 28 70 4e 65 78 74 2c 20 26 6e 53 75 66 66 69 78  (pNext, &nSuffix
37766 29 3b 0a 0a 20 20 69 66 28 20 6e 50 72 65 66 69  );..  if( nPrefi
37767 78 2b 6e 53 75 66 66 69 78 3e 70 52 65 61 64 65  x+nSuffix>pReade
37768 72 2d 3e 6e 54 65 72 6d 41 6c 6c 6f 63 20 29 7b  r->nTermAlloc ){
37769 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20  .    int nNew = 
3776a 28 6e 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78  (nPrefix+nSuffix
3776b 29 2a 32 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  )*2;.    char *z
3776c 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  New = sqlite3_re
3776d 61 6c 6c 6f 63 28 70 52 65 61 64 65 72 2d 3e 7a  alloc(pReader->z
3776e 54 65 72 6d 2c 20 6e 4e 65 77 29 3b 0a 20 20 20  Term, nNew);.   
3776f 20 69 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20   if( !zNew ){.  
37770 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
37771 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
37772 20 20 20 70 52 65 61 64 65 72 2d 3e 7a 54 65 72     pReader->zTer
37773 6d 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 70 52  m = zNew;.    pR
37774 65 61 64 65 72 2d 3e 6e 54 65 72 6d 41 6c 6c 6f  eader->nTermAllo
37775 63 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 20 20  c = nNew;.  }.  
37776 6d 65 6d 63 70 79 28 26 70 52 65 61 64 65 72 2d  memcpy(&pReader-
37777 3e 7a 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 2c  >zTerm[nPrefix],
37778 20 70 4e 65 78 74 2c 20 6e 53 75 66 66 69 78 29   pNext, nSuffix)
37779 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 54 65  ;.  pReader->nTe
3777a 72 6d 20 3d 20 6e 50 72 65 66 69 78 2b 6e 53 75  rm = nPrefix+nSu
3777b 66 66 69 78 3b 0a 20 20 70 4e 65 78 74 20 2b 3d  ffix;.  pNext +=
3777c 20 6e 53 75 66 66 69 78 3b 0a 20 20 70 4e 65 78   nSuffix;.  pNex
3777d 74 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33  t += sqlite3Fts3
3777e 47 65 74 56 61 72 69 6e 74 33 32 28 70 4e 65 78  GetVarint32(pNex
3777f 74 2c 20 26 70 52 65 61 64 65 72 2d 3e 6e 44 6f  t, &pReader->nDo
37780 63 6c 69 73 74 29 3b 0a 20 20 61 73 73 65 72 74  clist);.  assert
37781 28 20 70 4e 65 78 74 3c 26 70 52 65 61 64 65 72  ( pNext<&pReader
37782 2d 3e 61 4e 6f 64 65 5b 70 52 65 61 64 65 72 2d  ->aNode[pReader-
37783 3e 6e 4e 6f 64 65 5d 20 29 3b 0a 20 20 70 52 65  >nNode] );.  pRe
37784 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 20 3d  ader->aDoclist =
37785 20 70 4e 65 78 74 3b 0a 20 20 70 52 65 61 64 65   pNext;.  pReade
37786 72 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 20 3d  r->pOffsetList =
37787 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   0;.  return SQL
37788 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
37789 20 53 65 74 20 74 68 65 20 53 65 67 52 65 61 64   Set the SegRead
3778a 65 72 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  er to point to t
3778b 68 65 20 66 69 72 73 74 20 64 6f 63 69 64 20 69  he first docid i
3778c 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 61 73  n the doclist as
3778d 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
3778e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72   the current ter
3778f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  m..*/.static voi
37790 64 20 66 74 73 33 53 65 67 52 65 61 64 65 72 46  d fts3SegReaderF
37791 69 72 73 74 44 6f 63 69 64 28 46 74 73 33 53 65  irstDocid(Fts3Se
37792 67 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  gReader *pReader
37793 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 61 73  ){.  int n;.  as
37794 73 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 61  sert( pReader->a
37795 44 6f 63 6c 69 73 74 20 29 3b 0a 20 20 61 73 73  Doclist );.  ass
37796 65 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e 70  ert( !pReader->p
37797 4f 66 66 73 65 74 4c 69 73 74 20 29 3b 0a 20 20  OffsetList );.  
37798 6e 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47  n = sqlite3Fts3G
37799 65 74 56 61 72 69 6e 74 28 70 52 65 61 64 65 72  etVarint(pReader
3779a 2d 3e 61 44 6f 63 6c 69 73 74 2c 20 26 70 52 65  ->aDoclist, &pRe
3779b 61 64 65 72 2d 3e 69 44 6f 63 69 64 29 3b 0a 20  ader->iDocid);. 
3779c 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65   pReader->pOffse
3779d 74 4c 69 73 74 20 3d 20 26 70 52 65 61 64 65 72  tList = &pReader
3779e 2d 3e 61 44 6f 63 6c 69 73 74 5b 6e 5d 3b 0a 7d  ->aDoclist[n];.}
3779f 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20  ../*.** Advance 
377a0 74 68 65 20 53 65 67 52 65 61 64 65 72 20 74 6f  the SegReader to
377a1 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65   point to the ne
377a2 78 74 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20  xt docid in the 
377a3 64 6f 63 6c 69 73 74 0a 2a 2a 20 61 73 73 6f 63  doclist.** assoc
377a4 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 63  iated with the c
377a5 75 72 72 65 6e 74 20 74 65 72 6d 2e 0a 2a 2a 20  urrent term..** 
377a6 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 73  .** If arguments
377a7 20 70 70 4f 66 66 73 65 74 4c 69 73 74 20 61 6e   ppOffsetList an
377a8 64 20 70 6e 4f 66 66 73 65 74 4c 69 73 74 20 61  d pnOffsetList a
377a9 72 65 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  re not NULL, the
377aa 6e 20 0a 2a 2a 20 2a 70 70 4f 66 66 73 65 74 4c  n .** *ppOffsetL
377ab 69 73 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f  ist is set to po
377ac 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74  int to the first
377ad 20 63 6f 6c 75 6d 6e 2d 6f 66 66 73 65 74 20 6c   column-offset l
377ae 69 73 74 0a 2a 2a 20 69 6e 20 74 68 65 20 64 6f  ist.** in the do
377af 63 6c 69 73 74 20 65 6e 74 72 79 20 28 69 2e 65  clist entry (i.e
377b0 2e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 70 61  . immediately pa
377b1 73 74 20 74 68 65 20 64 6f 63 69 64 20 76 61 72  st the docid var
377b2 69 6e 74 29 2e 0a 2a 2a 20 2a 70 6e 4f 66 66 73  int)..** *pnOffs
377b3 65 74 4c 69 73 74 20 69 73 20 73 65 74 20 74 6f  etList is set to
377b4 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
377b5 68 65 20 73 65 74 20 6f 66 20 63 6f 6c 75 6d 6e  he set of column
377b6 2d 6f 66 66 73 65 74 0a 2a 2a 20 6c 69 73 74 73  -offset.** lists
377b7 2c 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20  , not including 
377b8 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  the nul-terminat
377b9 6f 72 20 62 79 74 65 2e 20 46 6f 72 20 65 78 61  or byte. For exa
377ba 6d 70 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20  mple:.*/.static 
377bb 76 6f 69 64 20 66 74 73 33 53 65 67 52 65 61 64  void fts3SegRead
377bc 65 72 4e 65 78 74 44 6f 63 69 64 28 0a 20 20 46  erNextDocid(.  F
377bd 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52  ts3SegReader *pR
377be 65 61 64 65 72 2c 0a 20 20 63 68 61 72 20 2a 2a  eader,.  char **
377bf 70 70 4f 66 66 73 65 74 4c 69 73 74 2c 0a 20 20  ppOffsetList,.  
377c0 69 6e 74 20 2a 70 6e 4f 66 66 73 65 74 4c 69 73  int *pnOffsetLis
377c1 74 0a 29 7b 0a 20 20 63 68 61 72 20 2a 70 20 3d  t.){.  char *p =
377c2 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65   pReader->pOffse
377c3 74 4c 69 73 74 3b 0a 20 20 63 68 61 72 20 63 20  tList;.  char c 
377c4 3d 20 30 3b 0a 0a 20 20 2f 2a 20 50 6f 69 6e 74  = 0;..  /* Point
377c5 65 72 20 70 20 63 75 72 72 65 6e 74 6c 79 20 70  er p currently p
377c6 6f 69 6e 74 73 20 61 74 20 74 68 65 20 66 69 72  oints at the fir
377c7 73 74 20 62 79 74 65 20 6f 66 20 61 6e 20 6f 66  st byte of an of
377c8 66 73 65 74 20 6c 69 73 74 2e 20 54 68 65 0a 20  fset list. The. 
377c9 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77   ** following tw
377ca 6f 20 6c 69 6e 65 73 20 61 64 76 61 6e 63 65 20  o lines advance 
377cb 69 74 20 74 6f 20 70 6f 69 6e 74 20 6f 6e 65 20  it to point one 
377cc 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  byte past the en
377cd 64 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 61  d of.  ** the sa
377ce 6d 65 20 6f 66 66 73 65 74 20 6c 69 73 74 2e 0a  me offset list..
377cf 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 2a 70    */.  while( *p
377d0 20 7c 20 63 20 29 20 63 20 3d 20 2a 70 2b 2b 20   | c ) c = *p++ 
377d1 26 20 30 78 38 30 3b 0a 20 20 70 2b 2b 3b 0a 0a  & 0x80;.  p++;..
377d2 20 20 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64    /* If required
377d3 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f  , populate the o
377d4 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73 20  utput variables 
377d5 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74  with a pointer t
377d6 6f 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 73  o and the.  ** s
377d7 69 7a 65 20 6f 66 20 74 68 65 20 70 72 65 76 69  ize of the previ
377d8 6f 75 73 20 6f 66 66 73 65 74 2d 6c 69 73 74 2e  ous offset-list.
377d9 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 70 4f 66  .  */.  if( ppOf
377da 66 73 65 74 4c 69 73 74 20 29 7b 0a 20 20 20 20  fsetList ){.    
377db 2a 70 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20  *ppOffsetList = 
377dc 70 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74  pReader->pOffset
377dd 4c 69 73 74 3b 0a 20 20 20 20 2a 70 6e 4f 66 66  List;.    *pnOff
377de 73 65 74 4c 69 73 74 20 3d 20 70 20 2d 20 70 52  setList = p - pR
377df 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69  eader->pOffsetLi
377e0 73 74 20 2d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f  st - 1;.  }..  /
377e1 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e  * If there are n
377e2 6f 20 6d 6f 72 65 20 65 6e 74 72 69 65 73 20 69  o more entries i
377e3 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 2c 20 73  n the doclist, s
377e4 65 74 20 70 4f 66 66 73 65 74 4c 69 73 74 20 74  et pOffsetList t
377e5 6f 0a 20 20 2a 2a 20 4e 55 4c 4c 2e 20 4f 74 68  o.  ** NULL. Oth
377e6 65 72 77 69 73 65 2c 20 73 65 74 20 46 74 73 33  erwise, set Fts3
377e7 53 65 67 52 65 61 64 65 72 2e 69 44 6f 63 69 64  SegReader.iDocid
377e8 20 74 6f 20 74 68 65 20 6e 65 78 74 20 64 6f 63   to the next doc
377e9 69 64 20 61 6e 64 0a 20 20 2a 2a 20 46 74 73 33  id and.  ** Fts3
377ea 53 65 67 52 65 61 64 65 72 2e 70 4f 66 66 73 65  SegReader.pOffse
377eb 74 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74 20 74  tList to point t
377ec 6f 20 74 68 65 20 6e 65 78 74 20 6f 66 66 73 65  o the next offse
377ed 74 20 6c 69 73 74 20 62 65 66 6f 72 65 0a 20 20  t list before.  
377ee 2a 2a 20 72 65 74 75 72 6e 69 6e 67 2e 0a 20 20  ** returning..  
377ef 2a 2f 0a 20 20 69 66 28 20 70 3e 3d 26 70 52 65  */.  if( p>=&pRe
377f0 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 5b 70  ader->aDoclist[p
377f1 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c 69 73 74  Reader->nDoclist
377f2 5d 20 29 7b 0a 20 20 20 20 70 52 65 61 64 65 72  ] ){.    pReader
377f3 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20  ->pOffsetList = 
377f4 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
377f5 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44  sqlite3_int64 iD
377f6 65 6c 74 61 3b 0a 20 20 20 20 70 52 65 61 64 65  elta;.    pReade
377f7 72 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 20 3d  r->pOffsetList =
377f8 20 70 20 2b 20 73 71 6c 69 74 65 33 46 74 73 33   p + sqlite3Fts3
377f9 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 69 44  GetVarint(p, &iD
377fa 65 6c 74 61 29 3b 0a 20 20 20 20 70 52 65 61 64  elta);.    pRead
377fb 65 72 2d 3e 69 44 6f 63 69 64 20 2b 3d 20 69 44  er->iDocid += iD
377fc 65 6c 74 61 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  elta;.  }.}../*.
377fd 2a 2a 20 46 72 65 65 20 61 6c 6c 20 61 6c 6c 6f  ** Free all allo
377fe 63 61 74 69 6f 6e 73 20 61 73 73 6f 63 69 61 74  cations associat
377ff 65 64 20 77 69 74 68 20 74 68 65 20 69 74 65 72  ed with the iter
37800 61 74 6f 72 20 70 61 73 73 65 64 20 61 73 20 74  ator passed as t
37801 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72  he .** second ar
37802 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
37803 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
37804 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
37805 64 65 72 46 72 65 65 28 46 74 73 33 54 61 62 6c  derFree(Fts3Tabl
37806 65 20 2a 70 2c 20 46 74 73 33 53 65 67 52 65 61  e *p, Fts3SegRea
37807 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
37808 20 69 66 28 20 70 52 65 61 64 65 72 20 29 7b 0a   if( pReader ){.
37809 20 20 20 20 69 66 28 20 70 52 65 61 64 65 72 2d      if( pReader-
3780a 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20  >pStmt ){.      
3780b 2f 2a 20 4d 6f 76 65 20 74 68 65 20 6c 65 61 66  /* Move the leaf
3780c 2d 72 61 6e 67 65 20 53 45 4c 45 43 54 20 73 74  -range SELECT st
3780d 61 74 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 61  atement to the a
3780e 4c 65 61 76 65 73 53 74 6d 74 5b 5d 20 61 72 72  LeavesStmt[] arr
3780f 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20 73 6f 20  ay,.      ** so 
37810 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72  that it can be r
37811 65 75 73 65 64 20 77 68 65 6e 20 72 65 71 75 69  eused when requi
37812 72 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 71  red by another q
37813 75 65 72 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  uery..      */. 
37814 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
37815 6e 4c 65 61 76 65 73 53 74 6d 74 3c 70 2d 3e 6e  nLeavesStmt<p->n
37816 4c 65 61 76 65 73 54 6f 74 61 6c 20 29 3b 0a 20  LeavesTotal );. 
37817 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
37818 65 74 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d  et(pReader->pStm
37819 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4c 65  t);.      p->aLe
3781a 61 76 65 73 53 74 6d 74 5b 70 2d 3e 6e 4c 65 61  avesStmt[p->nLea
3781b 76 65 73 53 74 6d 74 2b 2b 5d 20 3d 20 70 52 65  vesStmt++] = pRe
3781c 61 64 65 72 2d 3e 70 53 74 6d 74 3b 0a 20 20 20  ader->pStmt;.   
3781d 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
3781e 72 65 65 28 70 52 65 61 64 65 72 2d 3e 7a 54 65  ree(pReader->zTe
3781f 72 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rm);.    sqlite3
37820 5f 66 72 65 65 28 70 52 65 61 64 65 72 29 3b 0a  _free(pReader);.
37821 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c    }.}../*.** All
37822 6f 63 61 74 65 20 61 20 6e 65 77 20 53 65 67 52  ocate a new SegR
37823 65 61 64 65 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f  eader object..*/
37824 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
37825 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 53  int sqlite3Fts3S
37826 65 67 52 65 61 64 65 72 4e 65 77 28 0a 20 20 46  egReaderNew(.  F
37827 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20  ts3Table *p,    
37828 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37829 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * Virtual table 
3782a 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  handle */.  int 
3782b 69 41 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  iAge,           
3782c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
3782d 65 67 6d 65 6e 74 20 22 61 67 65 22 2e 20 2a 2f  egment "age". */
3782e 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
3782f 20 69 53 74 61 72 74 4c 65 61 66 2c 20 20 20 20   iStartLeaf,    
37830 20 20 20 2f 2a 20 46 69 72 73 74 20 6c 65 61 66     /* First leaf
37831 20 74 6f 20 74 72 61 76 65 72 73 65 20 2a 2f 0a   to traverse */.
37832 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
37833 69 45 6e 64 4c 65 61 66 2c 20 20 20 20 20 20 20  iEndLeaf,       
37834 20 20 2f 2a 20 46 69 6e 61 6c 20 6c 65 61 66 20    /* Final leaf 
37835 74 6f 20 74 72 61 76 65 72 73 65 20 2a 2f 0a 20  to traverse */. 
37836 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
37837 45 6e 64 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20  EndBlock,       
37838 20 2f 2a 20 46 69 6e 61 6c 20 62 6c 6f 63 6b 20   /* Final block 
37839 6f 66 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 20 20  of segment */.  
3783a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 6f 6f  const char *zRoo
3783b 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
3783c 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e 74 61 69  /* Buffer contai
3783d 6e 69 6e 67 20 72 6f 6f 74 20 6e 6f 64 65 20 2a  ning root node *
3783e 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20  /.  int nRoot,  
3783f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37840 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62      /* Size of b
37841 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
37842 20 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0a 20 20   root node */.  
37843 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a  Fts3SegReader **
37844 70 70 52 65 61 64 65 72 20 20 20 20 20 20 20 20  ppReader        
37845 2f 2a 20 4f 55 54 3a 20 41 6c 6c 6f 63 61 74 65  /* OUT: Allocate
37846 64 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20  d Fts3SegReader 
37847 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
37848 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
37849 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
3784a 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 46 74 73 33  n code */.  Fts3
3784b 53 65 67 52 65 61 64 65 72 20 2a 70 52 65 61 64  SegReader *pRead
3784c 65 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  er;         /* N
3784d 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 53  ewly allocated S
3784e 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 20  egReader object 
3784f 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 20  */.  int nExtra 
37850 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
37851 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f       /* Bytes to
37852 20 61 6c 6c 6f 63 61 74 65 20 73 65 67 6d 65 6e   allocate segmen
37853 74 20 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0a 0a  t root node */..
37854 20 20 69 66 28 20 69 53 74 61 72 74 4c 65 61 66    if( iStartLeaf
37855 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 45 78 74 72  ==0 ){.    nExtr
37856 61 20 3d 20 6e 52 6f 6f 74 3b 0a 20 20 7d 0a 0a  a = nRoot;.  }..
37857 20 20 70 52 65 61 64 65 72 20 3d 20 28 46 74 73    pReader = (Fts
37858 33 53 65 67 52 65 61 64 65 72 20 2a 29 73 71 6c  3SegReader *)sql
37859 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
3785a 6f 66 28 46 74 73 33 53 65 67 52 65 61 64 65 72  of(Fts3SegReader
3785b 29 20 2b 20 6e 45 78 74 72 61 29 3b 0a 20 20 69  ) + nExtra);.  i
3785c 66 28 20 21 70 52 65 61 64 65 72 20 29 7b 0a 20  f( !pReader ){. 
3785d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3785e 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65  _NOMEM;.  }.  me
3785f 6d 73 65 74 28 70 52 65 61 64 65 72 2c 20 30 2c  mset(pReader, 0,
37860 20 73 69 7a 65 6f 66 28 46 74 73 33 53 65 67 52   sizeof(Fts3SegR
37861 65 61 64 65 72 29 29 3b 0a 20 20 70 52 65 61 64  eader));.  pRead
37862 65 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 20  er->iStartBlock 
37863 3d 20 69 53 74 61 72 74 4c 65 61 66 3b 0a 20 20  = iStartLeaf;.  
37864 70 52 65 61 64 65 72 2d 3e 69 49 64 78 20 3d 20  pReader->iIdx = 
37865 69 41 67 65 3b 0a 20 20 70 52 65 61 64 65 72 2d  iAge;.  pReader-
37866 3e 69 45 6e 64 42 6c 6f 63 6b 20 3d 20 69 45 6e  >iEndBlock = iEn
37867 64 42 6c 6f 63 6b 3b 0a 0a 20 20 69 66 28 20 6e  dBlock;..  if( n
37868 45 78 74 72 61 20 29 7b 0a 20 20 20 20 2f 2a 20  Extra ){.    /* 
37869 54 68 65 20 65 6e 74 69 72 65 20 73 65 67 6d 65  The entire segme
3786a 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  nt is stored in 
3786b 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20 2a  the root node. *
3786c 2f 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 61  /.    pReader->a
3786d 4e 6f 64 65 20 3d 20 28 63 68 61 72 20 2a 29 26  Node = (char *)&
3786e 70 52 65 61 64 65 72 5b 31 5d 3b 0a 20 20 20 20  pReader[1];.    
3786f 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 20 3d  pReader->nNode =
37870 20 6e 52 6f 6f 74 3b 0a 20 20 20 20 6d 65 6d 63   nRoot;.    memc
37871 70 79 28 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64  py(pReader->aNod
37872 65 2c 20 7a 52 6f 6f 74 2c 20 6e 52 6f 6f 74 29  e, zRoot, nRoot)
37873 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
37874 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
37875 6d 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  mt;..    /* If t
37876 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 53  he text of the S
37877 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  QL statement to 
37878 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
37879 61 20 63 6f 6e 74 69 67 75 6f 75 73 0a 20 20 20  a contiguous.   
3787a 20 2a 2a 20 73 65 74 20 6f 66 20 65 6e 74 72 69   ** set of entri
3787b 65 73 20 69 6e 20 74 68 65 20 25 5f 73 65 67 6d  es in the %_segm
3787c 65 6e 74 73 20 74 61 62 6c 65 20 68 61 73 20 6e  ents table has n
3787d 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f 6d 70  ot yet been comp
3787e 6f 73 65 64 2c 0a 20 20 20 20 2a 2a 20 63 6f 6d  osed,.    ** com
3787f 70 6f 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 20  pose it now..   
37880 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 2d 3e   */.    if( !p->
37881 7a 53 65 6c 65 63 74 4c 65 61 76 65 73 20 29 7b  zSelectLeaves ){
37882 0a 20 20 20 20 20 20 70 2d 3e 7a 53 65 6c 65 63  .      p->zSelec
37883 74 4c 65 61 76 65 73 20 3d 20 73 71 6c 69 74 65  tLeaves = sqlite
37884 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20  3_mprintf(.     
37885 20 20 20 20 20 22 53 45 4c 45 43 54 20 62 6c 6f       "SELECT blo
37886 63 6b 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73  ck FROM %Q.'%q_s
37887 65 67 6d 65 6e 74 73 27 20 57 48 45 52 45 20 62  egments' WHERE b
37888 6c 6f 63 6b 69 64 20 42 45 54 57 45 45 4e 20 3f  lockid BETWEEN ?
37889 20 41 4e 44 20 3f 20 22 0a 20 20 20 20 20 20 20   AND ? ".       
3788a 20 20 20 22 4f 52 44 45 52 20 42 59 20 62 6c 6f     "ORDER BY blo
3788b 63 6b 69 64 22 2c 20 70 2d 3e 7a 44 62 2c 20 70  ckid", p->zDb, p
3788c 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b  ->zName.      );
3788d 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 7a  .      if( !p->z
3788e 53 65 6c 65 63 74 4c 65 61 76 65 73 20 29 7b 0a  SelectLeaves ){.
3788f 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
37890 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
37891 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 65 64     goto finished
37892 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
37893 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
37894 20 61 72 65 20 6e 6f 20 66 72 65 65 20 73 74 61   are no free sta
37895 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61  tements in the a
37896 4c 65 61 76 65 73 53 74 6d 74 5b 5d 20 61 72 72  LeavesStmt[] arr
37897 61 79 2c 20 70 72 65 70 61 72 65 0a 20 20 20 20  ay, prepare.    
37898 2a 2a 20 61 20 6e 65 77 20 73 74 61 74 65 6d 65  ** a new stateme
37899 6e 74 20 6e 6f 77 2e 20 4f 74 68 65 72 77 69 73  nt now. Otherwis
3789a 65 2c 20 72 65 75 73 65 20 61 20 70 72 65 70 61  e, reuse a prepa
3789b 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 66 72  red statement fr
3789c 6f 6d 0a 20 20 20 20 2a 2a 20 61 4c 65 61 76 65  om.    ** aLeave
3789d 73 53 74 6d 74 5b 5d 2e 0a 20 20 20 20 2a 2f 0a  sStmt[]..    */.
3789e 20 20 20 20 69 66 28 20 70 2d 3e 6e 4c 65 61 76      if( p->nLeav
3789f 65 73 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20  esStmt==0 ){.   
378a0 20 20 20 69 66 28 20 70 2d 3e 6e 4c 65 61 76 65     if( p->nLeave
378a1 73 54 6f 74 61 6c 3d 3d 70 2d 3e 6e 4c 65 61 76  sTotal==p->nLeav
378a2 65 73 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  esAlloc ){.     
378a3 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 70 2d     int nNew = p-
378a4 3e 6e 4c 65 61 76 65 73 41 6c 6c 6f 63 20 2b 20  >nLeavesAlloc + 
378a5 31 36 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  16;.        sqli
378a6 74 65 33 5f 73 74 6d 74 20 2a 2a 61 4e 65 77 20  te3_stmt **aNew 
378a7 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  = (sqlite3_stmt 
378a8 2a 2a 29 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  **)sqlite3_reall
378a9 6f 63 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  oc(.            
378aa 70 2d 3e 61 4c 65 61 76 65 73 53 74 6d 74 2c 20  p->aLeavesStmt, 
378ab 6e 4e 65 77 2a 73 69 7a 65 6f 66 28 73 71 6c 69  nNew*sizeof(sqli
378ac 74 65 33 5f 73 74 6d 74 20 2a 29 0a 20 20 20 20  te3_stmt *).    
378ad 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69      );.        i
378ae 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 20 20 20  f( !aNew ){.    
378af 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
378b0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20  E_NOMEM;.       
378b1 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 65 64     goto finished
378b2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
378b3 20 20 20 20 70 2d 3e 6e 4c 65 61 76 65 73 41 6c      p->nLeavesAl
378b4 6c 6f 63 20 3d 20 6e 4e 65 77 3b 0a 20 20 20 20  loc = nNew;.    
378b5 20 20 20 20 70 2d 3e 61 4c 65 61 76 65 73 53 74      p->aLeavesSt
378b6 6d 74 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20  mt = aNew;.     
378b7 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   }.      rc = sq
378b8 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
378b9 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 53 65 6c 65  (p->db, p->zSele
378ba 63 74 4c 65 61 76 65 73 2c 20 2d 31 2c 20 26 70  ctLeaves, -1, &p
378bb 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30  Reader->pStmt, 0
378bc 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
378bd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
378be 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73        goto finis
378bf 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  hed;.      }.   
378c0 20 20 20 70 2d 3e 6e 4c 65 61 76 65 73 54 6f 74     p->nLeavesTot
378c1 61 6c 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  al++;.    }else{
378c2 0a 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e  .      pReader->
378c3 70 53 74 6d 74 20 3d 20 70 2d 3e 61 4c 65 61 76  pStmt = p->aLeav
378c4 65 73 53 74 6d 74 5b 2d 2d 70 2d 3e 6e 4c 65 61  esStmt[--p->nLea
378c5 76 65 73 53 74 6d 74 5d 3b 0a 20 20 20 20 7d 0a  vesStmt];.    }.
378c6 0a 20 20 20 20 2f 2a 20 42 69 6e 64 20 74 68 65  .    /* Bind the
378c7 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64 20 6c   start and end l
378c8 65 61 66 20 62 6c 6f 63 6b 69 64 73 20 74 6f 20  eaf blockids to 
378c9 74 68 65 20 70 72 65 70 61 72 65 64 20 53 51 4c  the prepared SQL
378ca 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
378cb 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
378cc 69 6e 74 36 34 28 70 52 65 61 64 65 72 2d 3e 70  int64(pReader->p
378cd 53 74 6d 74 2c 20 31 2c 20 69 53 74 61 72 74 4c  Stmt, 1, iStartL
378ce 65 61 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  eaf);.    sqlite
378cf 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 65  3_bind_int64(pRe
378d0 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 32 2c 20  ader->pStmt, 2, 
378d1 69 45 6e 64 4c 65 61 66 29 3b 0a 20 20 7d 0a 20  iEndLeaf);.  }. 
378d2 20 72 63 20 3d 20 66 74 73 33 53 65 67 52 65 61   rc = fts3SegRea
378d3 64 65 72 4e 65 78 74 28 70 52 65 61 64 65 72 29  derNext(pReader)
378d4 3b 0a 0a 20 66 69 6e 69 73 68 65 64 3a 0a 20 20  ;.. finished:.  
378d5 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
378d6 4b 20 29 7b 0a 20 20 20 20 2a 70 70 52 65 61 64  K ){.    *ppRead
378d7 65 72 20 3d 20 70 52 65 61 64 65 72 3b 0a 20 20  er = pReader;.  
378d8 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
378d9 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 46  e3Fts3SegReaderF
378da 72 65 65 28 70 2c 20 70 52 65 61 64 65 72 29 3b  ree(p, pReader);
378db 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
378dc 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ;.}.../*.** The 
378dd 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
378de 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
378df 20 69 73 20 65 78 70 65 63 74 65 64 20 74 6f 20   is expected to 
378e0 62 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f  be a statement o
378e1 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  f.** the form:.*
378e2 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 0a 2a  *.**   SELECT .*
378e3 2a 20 20 20 20 20 69 64 78 2c 20 20 20 20 20 20  *     idx,      
378e4 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 63              -- c
378e5 6f 6c 20 30 0a 2a 2a 20 20 20 20 20 73 74 61 72  ol 0.**     star
378e6 74 5f 62 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  t_block,        
378e7 20 20 2d 2d 20 63 6f 6c 20 31 0a 2a 2a 20 20 20    -- col 1.**   
378e8 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f    leaves_end_blo
378e9 63 6b 2c 20 20 20 20 20 2d 2d 20 63 6f 6c 20 32  ck,     -- col 2
378ea 0a 2a 2a 20 20 20 20 20 65 6e 64 5f 62 6c 6f 63  .**     end_bloc
378eb 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d  k,            --
378ec 20 63 6f 6c 20 33 0a 2a 2a 20 20 20 20 20 72 6f   col 3.**     ro
378ed 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ot              
378ee 20 20 20 20 2d 2d 20 63 6f 6c 20 34 0a 2a 2a 20      -- col 4.** 
378ef 20 20 46 52 4f 4d 20 25 5f 73 65 67 64 69 72 20    FROM %_segdir 
378f0 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ....**.** This f
378f1 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65  unction allocate
378f2 73 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  s and initialize
378f3 73 20 61 20 46 74 73 33 53 65 67 52 65 61 64 65  s a Fts3SegReade
378f4 72 20 73 74 72 75 63 74 75 72 65 20 74 6f 0a 2a  r structure to.*
378f5 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  * iterate throug
378f6 68 20 74 68 65 20 74 65 72 6d 73 20 73 74 6f 72  h the terms stor
378f7 65 64 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e  ed in the segmen
378f8 74 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  t identified by 
378f9 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 72  the.** current r
378fa 6f 77 20 74 68 61 74 20 70 53 74 6d 74 20 69 73  ow that pStmt is
378fb 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 0a 2a   pointing to. .*
378fc 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
378fd 75 6c 2c 20 74 68 65 20 46 74 73 33 53 65 67 52  ul, the Fts3SegR
378fe 65 61 64 65 72 20 69 73 20 6c 65 66 74 20 70 6f  eader is left po
378ff 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69  inting to the fi
37900 72 73 74 20 74 65 72 6d 0a 2a 2a 20 69 6e 20 74  rst term.** in t
37901 68 65 20 73 65 67 6d 65 6e 74 20 61 6e 64 20 53  he segment and S
37902 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
37903 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
37904 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
37905 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75  .** code is retu
37906 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
37907 69 6e 74 20 66 74 73 33 53 65 67 52 65 61 64 65  int fts3SegReade
37908 72 4e 65 77 28 0a 20 20 46 74 73 33 54 61 62 6c  rNew(.  Fts3Tabl
37909 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
3790a 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75          /* Virtu
3790b 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20  al table handle 
3790c 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
3790d 74 20 2a 70 53 74 6d 74 2c 20 20 20 20 20 20 20  t *pStmt,       
3790e 20 20 20 20 20 2f 2a 20 53 65 65 20 61 62 6f 76       /* See abov
3790f 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 65 2c  e */.  int iAge,
37910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37911 20 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e         /* Segmen
37912 74 20 22 61 67 65 22 2e 20 2a 2f 0a 20 20 46 74  t "age". */.  Ft
37913 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 70 70  s3SegReader **pp
37914 52 65 61 64 65 72 20 20 20 20 20 20 20 20 2f 2a  Reader        /*
37915 20 4f 55 54 3a 20 41 6c 6c 6f 63 61 74 65 64 20   OUT: Allocated 
37916 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2f  Fts3SegReader */
37917 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  .){.  return sql
37918 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
37919 72 4e 65 77 28 70 2c 20 69 41 67 65 2c 20 0a 20  rNew(p, iAge, . 
3791a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
3791b 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c  umn_int64(pStmt,
3791c 20 31 29 2c 0a 20 20 20 20 20 20 73 71 6c 69 74   1),.      sqlit
3791d 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
3791e 70 53 74 6d 74 2c 20 32 29 2c 0a 20 20 20 20 20  pStmt, 2),.     
3791f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
37920 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 33 29 2c  int64(pStmt, 3),
37921 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  .      sqlite3_c
37922 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74  olumn_blob(pStmt
37923 2c 20 34 29 2c 0a 20 20 20 20 20 20 73 71 6c 69  , 4),.      sqli
37924 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
37925 28 70 53 74 6d 74 2c 20 34 29 2c 0a 20 20 20 20  (pStmt, 4),.    
37926 20 20 70 70 52 65 61 64 65 72 0a 20 20 29 3b 0a    ppReader.  );.
37927 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
37928 20 74 68 65 20 65 6e 74 72 69 65 73 20 70 6f 69   the entries poi
37929 6e 74 65 64 20 74 6f 20 62 79 20 74 77 6f 20 46  nted to by two F
3792a 74 73 33 53 65 67 52 65 61 64 65 72 20 73 74 72  ts3SegReader str
3792b 75 63 74 75 72 65 73 2e 20 0a 2a 2a 20 43 6f 6d  uctures. .** Com
3792c 70 61 72 69 73 6f 6e 20 69 73 20 61 73 20 66 6f  parison is as fo
3792d 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  llows:.**.**   1
3792e 29 20 45 4f 46 20 69 73 20 67 72 65 61 74 65 72  ) EOF is greater
3792f 20 74 68 61 6e 20 6e 6f 74 20 45 4f 46 2e 0a 2a   than not EOF..*
37930 2a 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 63 75  *.**   2) The cu
37931 72 72 65 6e 74 20 74 65 72 6d 73 20 28 69 66 20  rrent terms (if 
37932 61 6e 79 29 20 61 72 65 20 63 6f 6d 70 61 72 65  any) are compare
37933 64 20 77 69 74 68 20 6d 65 6d 63 6d 70 28 29 2e  d with memcmp().
37934 20 49 66 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20   If one.**      
37935 74 65 72 6d 20 69 73 20 61 20 70 72 65 66 69 78  term is a prefix
37936 20 6f 66 20 61 6e 6f 74 68 65 72 2c 20 74 68 65   of another, the
37937 20 6c 6f 6e 67 65 72 20 74 65 72 6d 20 69 73 20   longer term is 
37938 63 6f 6e 73 69 64 65 72 65 64 20 74 68 65 0a 2a  considered the.*
37939 2a 20 20 20 20 20 20 6c 61 72 67 65 72 2e 0a 2a  *      larger..*
3793a 2a 0a 2a 2a 20 20 20 33 29 20 42 79 20 73 65 67  *.**   3) By seg
3793b 6d 65 6e 74 20 61 67 65 2e 20 41 6e 20 6f 6c 64  ment age. An old
3793c 65 72 20 73 65 67 6d 65 6e 74 20 69 73 20 63 6f  er segment is co
3793d 6e 73 69 64 65 72 65 64 20 6c 61 72 67 65 72 2e  nsidered larger.
3793e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
3793f 74 73 33 53 65 67 52 65 61 64 65 72 43 6d 70 28  ts3SegReaderCmp(
37940 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70  Fts3SegReader *p
37941 4c 68 73 2c 20 46 74 73 33 53 65 67 52 65 61 64  Lhs, Fts3SegRead
37942 65 72 20 2a 70 52 68 73 29 7b 0a 20 20 69 6e 74  er *pRhs){.  int
37943 20 72 63 3b 0a 20 20 69 66 28 20 70 4c 68 73 2d   rc;.  if( pLhs-
37944 3e 61 4e 6f 64 65 20 26 26 20 70 52 68 73 2d 3e  >aNode && pRhs->
37945 61 4e 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e 74  aNode ){.    int
37946 20 72 63 32 20 3d 20 70 4c 68 73 2d 3e 6e 54 65   rc2 = pLhs->nTe
37947 72 6d 20 2d 20 70 52 68 73 2d 3e 6e 54 65 72 6d  rm - pRhs->nTerm
37948 3b 0a 20 20 20 20 69 66 28 20 72 63 32 3c 30 20  ;.    if( rc2<0 
37949 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 65  ){.      rc = me
3794a 6d 63 6d 70 28 70 4c 68 73 2d 3e 7a 54 65 72 6d  mcmp(pLhs->zTerm
3794b 2c 20 70 52 68 73 2d 3e 7a 54 65 72 6d 2c 20 70  , pRhs->zTerm, p
3794c 4c 68 73 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20 20  Lhs->nTerm);.   
3794d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
3794e 20 3d 20 6d 65 6d 63 6d 70 28 70 4c 68 73 2d 3e   = memcmp(pLhs->
3794f 7a 54 65 72 6d 2c 20 70 52 68 73 2d 3e 7a 54 65  zTerm, pRhs->zTe
37950 72 6d 2c 20 70 52 68 73 2d 3e 6e 54 65 72 6d 29  rm, pRhs->nTerm)
37951 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
37952 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  rc==0 ){.      r
37953 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 20  c = rc2;.    }. 
37954 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
37955 20 28 70 4c 68 73 2d 3e 61 4e 6f 64 65 3d 3d 30   (pLhs->aNode==0
37956 29 20 2d 20 28 70 52 68 73 2d 3e 61 4e 6f 64 65  ) - (pRhs->aNode
37957 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ==0);.  }.  if( 
37958 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  rc==0 ){.    rc 
37959 3d 20 70 52 68 73 2d 3e 69 49 64 78 20 2d 20 70  = pRhs->iIdx - p
3795a 4c 68 73 2d 3e 69 49 64 78 3b 0a 20 20 7d 0a 20  Lhs->iIdx;.  }. 
3795b 20 61 73 73 65 72 74 28 20 72 63 21 3d 30 20 29   assert( rc!=0 )
3795c 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
3795d 0a 0a 2f 2a 0a 2a 2a 20 41 20 64 69 66 66 65 72  ../*.** A differ
3795e 65 6e 74 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  ent comparison f
3795f 75 6e 63 74 69 6f 6e 20 66 6f 72 20 53 65 67 52  unction for SegR
37960 65 61 64 65 72 20 73 74 72 75 63 74 75 72 65 73  eader structures
37961 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 76 65 72  . In this.** ver
37962 73 69 6f 6e 2c 20 69 74 20 69 73 20 61 73 73 75  sion, it is assu
37963 6d 65 64 20 74 68 61 74 20 65 61 63 68 20 53 65  med that each Se
37964 67 52 65 61 64 65 72 20 70 6f 69 6e 74 73 20 74  gReader points t
37965 6f 20 61 6e 20 65 6e 74 72 79 20 69 6e 0a 2a 2a  o an entry in.**
37966 20 61 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 69   a doclist for i
37967 64 65 6e 74 69 63 61 6c 20 74 65 72 6d 73 2e 20  dentical terms. 
37968 43 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d 61  Comparison is ma
37969 64 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  de as follows:.*
3796a 2a 0a 2a 2a 20 20 20 31 29 20 45 4f 46 20 28 65  *.**   1) EOF (e
3796b 6e 64 20 6f 66 20 64 6f 63 6c 69 73 74 20 69 6e  nd of doclist in
3796c 20 74 68 69 73 20 63 61 73 65 29 20 69 73 20 67   this case) is g
3796d 72 65 61 74 65 72 20 74 68 61 6e 20 6e 6f 74 20  reater than not 
3796e 45 4f 46 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20  EOF..**.**   2) 
3796f 42 79 20 63 75 72 72 65 6e 74 20 64 6f 63 69 64  By current docid
37970 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 42 79 20  ..**.**   3) By 
37971 73 65 67 6d 65 6e 74 20 61 67 65 2e 20 41 6e 20  segment age. An 
37972 6f 6c 64 65 72 20 73 65 67 6d 65 6e 74 20 69 73  older segment is
37973 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 61 72 67   considered larg
37974 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
37975 74 20 66 74 73 33 53 65 67 52 65 61 64 65 72 44  t fts3SegReaderD
37976 6f 63 6c 69 73 74 43 6d 70 28 46 74 73 33 53 65  oclistCmp(Fts3Se
37977 67 52 65 61 64 65 72 20 2a 70 4c 68 73 2c 20 46  gReader *pLhs, F
37978 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52  ts3SegReader *pR
37979 68 73 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  hs){.  int rc = 
3797a 28 70 4c 68 73 2d 3e 70 4f 66 66 73 65 74 4c 69  (pLhs->pOffsetLi
3797b 73 74 3d 3d 30 29 2d 28 70 52 68 73 2d 3e 70 4f  st==0)-(pRhs->pO
3797c 66 66 73 65 74 4c 69 73 74 3d 3d 30 29 3b 0a 20  ffsetList==0);. 
3797d 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20   if( rc==0 ){.  
3797e 20 20 69 66 28 20 70 4c 68 73 2d 3e 69 44 6f 63    if( pLhs->iDoc
3797f 69 64 3d 3d 70 52 68 73 2d 3e 69 44 6f 63 69 64  id==pRhs->iDocid
37980 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
37981 52 68 73 2d 3e 69 49 64 78 20 2d 20 70 4c 68 73  Rhs->iIdx - pLhs
37982 2d 3e 69 49 64 78 3b 0a 20 20 20 20 7d 65 6c 73  ->iIdx;.    }els
37983 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 28 70  e{.      rc = (p
37984 4c 68 73 2d 3e 69 44 6f 63 69 64 20 3e 20 70 52  Lhs->iDocid > pR
37985 68 73 2d 3e 69 44 6f 63 69 64 29 20 3f 20 31 20  hs->iDocid) ? 1 
37986 3a 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  : -1;.    }.  }.
37987 20 20 61 73 73 65 72 74 28 20 70 4c 68 73 2d 3e    assert( pLhs->
37988 61 4e 6f 64 65 20 26 26 20 70 52 68 73 2d 3e 61  aNode && pRhs->a
37989 4e 6f 64 65 20 29 3b 0a 20 20 72 65 74 75 72 6e  Node );.  return
3798a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   rc;.}../*.** Co
3798b 6d 70 61 72 65 20 74 68 65 20 74 65 72 6d 20 74  mpare the term t
3798c 68 61 74 20 74 68 65 20 46 74 73 33 53 65 67 52  hat the Fts3SegR
3798d 65 61 64 65 72 20 6f 62 6a 65 63 74 20 70 61 73  eader object pas
3798e 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
3798f 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 70 6f 69   argument.** poi
37990 6e 74 73 20 74 6f 20 77 69 74 68 20 74 68 65 20  nts to with the 
37991 74 65 72 6d 20 73 70 65 63 69 66 69 65 64 20 62  term specified b
37992 79 20 61 72 67 75 6d 65 6e 74 73 20 7a 54 65 72  y arguments zTer
37993 6d 20 61 6e 64 20 6e 54 65 72 6d 2e 20 0a 2a 2a  m and nTerm. .**
37994 0a 2a 2a 20 49 66 20 74 68 65 20 70 53 65 67 20  .** If the pSeg 
37995 69 74 65 72 61 74 6f 72 20 69 73 20 61 6c 72 65  iterator is alre
37996 61 64 79 20 61 74 20 45 4f 46 2c 20 72 65 74 75  ady at EOF, retu
37997 72 6e 20 30 2e 20 4f 74 68 65 72 77 69 73 65 2c  rn 0. Otherwise,
37998 20 72 65 74 75 72 6e 0a 2a 2a 20 2d 76 65 20 69   return.** -ve i
37999 66 20 74 68 65 20 70 53 65 67 20 74 65 72 6d 20  f the pSeg term 
3799a 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 54 65  is less than zTe
3799b 72 6d 2f 6e 54 65 72 6d 2c 20 30 20 69 66 20 74  rm/nTerm, 0 if t
3799c 68 65 20 74 77 6f 20 74 65 72 6d 73 20 61 72 65  he two terms are
3799d 0a 2a 2a 20 65 71 75 61 6c 2c 20 6f 72 20 2b 76  .** equal, or +v
3799e 65 20 69 66 20 74 68 65 20 70 53 65 67 20 74 65  e if the pSeg te
3799f 72 6d 20 69 73 20 67 72 65 61 74 65 72 20 74 68  rm is greater th
379a0 61 6e 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e 0a  an zTerm/nTerm..
379a1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
379a2 73 33 53 65 67 52 65 61 64 65 72 54 65 72 6d 43  s3SegReaderTermC
379a3 6d 70 28 0a 20 20 46 74 73 33 53 65 67 52 65 61  mp(.  Fts3SegRea
379a4 64 65 72 20 2a 70 53 65 67 2c 20 20 20 20 20 20  der *pSeg,      
379a5 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74        /* Segment
379a6 20 72 65 61 64 65 72 20 6f 62 6a 65 63 74 20 2a   reader object *
379a7 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
379a8 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20  zTerm,          
379a9 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 63      /* Term to c
379aa 6f 6d 70 61 72 65 20 74 6f 20 2a 2f 0a 20 20 69  ompare to */.  i
379ab 6e 74 20 6e 54 65 72 6d 20 20 20 20 20 20 20 20  nt nTerm        
379ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
379ad 2a 20 53 69 7a 65 20 6f 66 20 74 65 72 6d 20 7a  * Size of term z
379ae 54 65 72 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f  Term in bytes */
379af 0a 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20  .){.  int res = 
379b0 30 3b 0a 20 20 69 66 28 20 70 53 65 67 2d 3e 61  0;.  if( pSeg->a
379b1 4e 6f 64 65 20 29 7b 0a 20 20 20 20 69 66 28 20  Node ){.    if( 
379b2 70 53 65 67 2d 3e 6e 54 65 72 6d 3e 6e 54 65 72  pSeg->nTerm>nTer
379b3 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d  m ){.      res =
379b4 20 6d 65 6d 63 6d 70 28 70 53 65 67 2d 3e 7a 54   memcmp(pSeg->zT
379b5 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  erm, zTerm, nTer
379b6 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  m);.    }else{. 
379b7 20 20 20 20 20 72 65 73 20 3d 20 6d 65 6d 63 6d       res = memcm
379b8 70 28 70 53 65 67 2d 3e 7a 54 65 72 6d 2c 20 7a  p(pSeg->zTerm, z
379b9 54 65 72 6d 2c 20 70 53 65 67 2d 3e 6e 54 65 72  Term, pSeg->nTer
379ba 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  m);.    }.    if
379bb 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20  ( res==0 ){.    
379bc 20 20 72 65 73 20 3d 20 70 53 65 67 2d 3e 6e 54    res = pSeg->nT
379bd 65 72 6d 2d 6e 54 65 72 6d 3b 0a 20 20 20 20 7d  erm-nTerm;.    }
379be 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65  .  }.  return re
379bf 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75  s;.}../*.** Argu
379c0 6d 65 6e 74 20 61 70 53 65 67 6d 65 6e 74 20 69  ment apSegment i
379c1 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 6e 53  s an array of nS
379c2 65 67 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 2e  egment elements.
379c3 20 49 74 20 69 73 20 6b 6e 6f 77 6e 20 74 68 61   It is known tha
379c4 74 0a 2a 2a 20 74 68 65 20 66 69 6e 61 6c 20 28  t.** the final (
379c5 6e 53 65 67 6d 65 6e 74 2d 6e 53 75 73 70 65 63  nSegment-nSuspec
379c6 74 29 20 6d 65 6d 62 65 72 73 20 61 72 65 20 61  t) members are a
379c7 6c 72 65 61 64 79 20 69 6e 20 73 6f 72 74 65 64  lready in sorted
379c8 20 6f 72 64 65 72 0a 2a 2a 20 28 61 63 63 6f 72   order.** (accor
379c9 64 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 70  ding to the comp
379ca 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  arison function 
379cb 70 72 6f 76 69 64 65 64 29 2e 20 54 68 69 73 20  provided). This 
379cc 66 75 6e 63 74 69 6f 6e 20 73 68 75 66 66 6c 65  function shuffle
379cd 73 0a 2a 2a 20 74 68 65 20 61 72 72 61 79 20 61  s.** the array a
379ce 72 6f 75 6e 64 20 75 6e 74 69 6c 20 61 6c 6c 20  round until all 
379cf 65 6e 74 72 69 65 73 20 61 72 65 20 69 6e 20 73  entries are in s
379d0 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2f 0a  orted order..*/.
379d1 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
379d2 53 65 67 52 65 61 64 65 72 53 6f 72 74 28 0a 20  SegReaderSort(. 
379d3 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
379d4 2a 61 70 53 65 67 6d 65 6e 74 2c 20 20 20 20 20  *apSegment,     
379d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
379d6 2f 2a 20 41 72 72 61 79 20 74 6f 20 73 6f 72 74  /* Array to sort
379d7 20 65 6e 74 72 69 65 73 20 6f 66 20 2a 2f 0a 20   entries of */. 
379d8 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74 2c 20 20   int nSegment,  
379d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
379da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
379db 2f 2a 20 53 69 7a 65 20 6f 66 20 61 70 53 65 67  /* Size of apSeg
379dc 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20  ment array */.  
379dd 69 6e 74 20 6e 53 75 73 70 65 63 74 2c 20 20 20  int nSuspect,   
379de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
379df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
379e0 2a 20 55 6e 73 6f 72 74 65 64 20 65 6e 74 72 79  * Unsorted entry
379e1 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 69 6e 74 20   count */.  int 
379e2 28 2a 78 43 6d 70 29 28 46 74 73 33 53 65 67 52  (*xCmp)(Fts3SegR
379e3 65 61 64 65 72 20 2a 2c 20 46 74 73 33 53 65 67  eader *, Fts3Seg
379e4 52 65 61 64 65 72 20 2a 29 20 20 2f 2a 20 43 6f  Reader *)  /* Co
379e5 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
379e6 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  n */.){.  int i;
379e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
379e8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
379e9 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
379ea 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 75  /..  assert( nSu
379eb 73 70 65 63 74 3c 3d 6e 53 65 67 6d 65 6e 74 20  spect<=nSegment 
379ec 29 3b 0a 0a 20 20 69 66 28 20 6e 53 75 73 70 65  );..  if( nSuspe
379ed 63 74 3d 3d 6e 53 65 67 6d 65 6e 74 20 29 20 6e  ct==nSegment ) n
379ee 53 75 73 70 65 63 74 2d 2d 3b 0a 20 20 66 6f 72  Suspect--;.  for
379ef 28 69 3d 6e 53 75 73 70 65 63 74 2d 31 3b 20 69  (i=nSuspect-1; i
379f0 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69  >=0; i--){.    i
379f1 6e 74 20 6a 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  nt j;.    for(j=
379f2 69 3b 20 6a 3c 28 6e 53 65 67 6d 65 6e 74 2d 31  i; j<(nSegment-1
379f3 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 46  ); j++){.      F
379f4 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 54  ts3SegReader *pT
379f5 6d 70 3b 0a 20 20 20 20 20 20 69 66 28 20 78 43  mp;.      if( xC
379f6 6d 70 28 61 70 53 65 67 6d 65 6e 74 5b 6a 5d 2c  mp(apSegment[j],
379f7 20 61 70 53 65 67 6d 65 6e 74 5b 6a 2b 31 5d 29   apSegment[j+1])
379f8 3c 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  <0 ) break;.    
379f9 20 20 70 54 6d 70 20 3d 20 61 70 53 65 67 6d 65    pTmp = apSegme
379fa 6e 74 5b 6a 2b 31 5d 3b 0a 20 20 20 20 20 20 61  nt[j+1];.      a
379fb 70 53 65 67 6d 65 6e 74 5b 6a 2b 31 5d 20 3d 20  pSegment[j+1] = 
379fc 61 70 53 65 67 6d 65 6e 74 5b 6a 5d 3b 0a 20 20  apSegment[j];.  
379fd 20 20 20 20 61 70 53 65 67 6d 65 6e 74 5b 6a 5d      apSegment[j]
379fe 20 3d 20 70 54 6d 70 3b 0a 20 20 20 20 7d 0a 20   = pTmp;.    }. 
379ff 20 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42   }..#ifndef NDEB
37a00 55 47 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  UG.  /* Check th
37a01 61 74 20 74 68 65 20 6c 69 73 74 20 72 65 61 6c  at the list real
37a02 6c 79 20 69 73 20 73 6f 72 74 65 64 20 6e 6f 77  ly is sorted now
37a03 2e 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  . */.  for(i=0; 
37a04 69 3c 28 6e 53 75 73 70 65 63 74 2d 31 29 3b 20  i<(nSuspect-1); 
37a05 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  i++){.    assert
37a06 28 20 78 43 6d 70 28 61 70 53 65 67 6d 65 6e 74  ( xCmp(apSegment
37a07 5b 69 5d 2c 20 61 70 53 65 67 6d 65 6e 74 5b 69  [i], apSegment[i
37a08 2b 31 5d 29 3c 30 20 29 3b 0a 20 20 7d 0a 23 65  +1])<0 );.  }.#e
37a09 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49  ndif.}../* .** I
37a0a 6e 73 65 72 74 20 61 20 72 65 63 6f 72 64 20 69  nsert a record i
37a0b 6e 74 6f 20 74 68 65 20 25 5f 73 65 67 6d 65 6e  nto the %_segmen
37a0c 74 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ts table..*/.sta
37a0d 74 69 63 20 69 6e 74 20 66 74 73 33 57 72 69 74  tic int fts3Writ
37a0e 65 53 65 67 6d 65 6e 74 28 0a 20 20 46 74 73 33  eSegment(.  Fts3
37a0f 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20  Table *p,       
37a10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
37a11 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e  irtual table han
37a12 64 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  dle */.  sqlite3
37a13 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 2c 20 20  _int64 iBlock,  
37a14 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63           /* Bloc
37a15 6b 20 69 64 20 66 6f 72 20 6e 65 77 20 62 6c 6f  k id for new blo
37a16 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 2c  ck */.  char *z,
37a17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a18 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
37a19 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e  er to buffer con
37a1a 74 61 69 6e 69 6e 67 20 62 6c 6f 63 6b 20 64 61  taining block da
37a1b 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 20 20 20  ta */.  int n   
37a1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a1d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
37a1e 6f 66 20 62 75 66 66 65 72 20 7a 20 69 6e 20 62  of buffer z in b
37a1f 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  ytes */.){.  sql
37a20 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
37a21 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 74 73  ;.  int rc = fts
37a22 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
37a23 49 4e 53 45 52 54 5f 53 45 47 4d 45 4e 54 53 2c  INSERT_SEGMENTS,
37a24 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69   &pStmt, 0);.  i
37a25 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
37a26 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
37a27 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74  bind_int64(pStmt
37a28 2c 20 31 2c 20 69 42 6c 6f 63 6b 29 3b 0a 20 20  , 1, iBlock);.  
37a29 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
37a2a 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  ind_blob(pStmt, 
37a2b 32 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  2, z, n, SQLITE_
37a2c 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69 66 28  STATIC);.    if(
37a2d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
37a2e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
37a2f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a 20 20 20  step(pStmt);.   
37a30 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
37a31 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20  reset(pStmt);.  
37a32 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
37a33 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49   rc;.}../* .** I
37a34 6e 73 65 72 74 20 61 20 72 65 63 6f 72 64 20 69  nsert a record i
37a35 6e 74 6f 20 74 68 65 20 25 5f 73 65 67 64 69 72  nto the %_segdir
37a36 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
37a37 63 20 69 6e 74 20 66 74 73 33 57 72 69 74 65 53  c int fts3WriteS
37a38 65 67 64 69 72 28 0a 20 20 46 74 73 33 54 61 62  egdir(.  Fts3Tab
37a39 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
37a3a 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
37a3b 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
37a3c 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c   */.  int iLevel
37a3d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
37a3e 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66        /* Value f
37a3f 6f 72 20 22 6c 65 76 65 6c 22 20 66 69 65 6c 64  or "level" field
37a40 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 2c 20   */.  int iIdx, 
37a41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a42 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66        /* Value f
37a43 6f 72 20 22 69 64 78 22 20 66 69 65 6c 64 20 2a  or "idx" field *
37a44 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
37a45 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 2c 20 20  4 iStartBlock,  
37a46 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72      /* Value for
37a47 20 22 73 74 61 72 74 5f 62 6c 6f 63 6b 22 20 66   "start_block" f
37a48 69 65 6c 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ield */.  sqlite
37a49 33 5f 69 6e 74 36 34 20 69 4c 65 61 66 45 6e 64  3_int64 iLeafEnd
37a4a 42 6c 6f 63 6b 2c 20 20 20 20 2f 2a 20 56 61 6c  Block,    /* Val
37a4b 75 65 20 66 6f 72 20 22 6c 65 61 76 65 73 5f 65  ue for "leaves_e
37a4c 6e 64 5f 62 6c 6f 63 6b 22 20 66 69 65 6c 64 20  nd_block" field 
37a4d 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
37a4e 36 34 20 69 45 6e 64 42 6c 6f 63 6b 2c 20 20 20  64 iEndBlock,   
37a4f 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f       /* Value fo
37a50 72 20 22 65 6e 64 5f 62 6c 6f 63 6b 22 20 66 69  r "end_block" fi
37a51 65 6c 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  eld */.  char *z
37a52 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20  Root,           
37a53 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 62           /* Blob
37a54 20 76 61 6c 75 65 20 66 6f 72 20 22 72 6f 6f 74   value for "root
37a55 22 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74  " field */.  int
37a56 20 6e 52 6f 6f 74 20 20 20 20 20 20 20 20 20 20   nRoot          
37a57 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37a58 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
37a59 69 6e 20 62 75 66 66 65 72 20 7a 52 6f 6f 74 20  in buffer zRoot 
37a5a 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  */.){.  sqlite3_
37a5b 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69  stmt *pStmt;.  i
37a5c 6e 74 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53  nt rc = fts3SqlS
37a5d 74 6d 74 28 70 2c 20 53 51 4c 5f 49 4e 53 45 52  tmt(p, SQL_INSER
37a5e 54 5f 53 45 47 44 49 52 2c 20 26 70 53 74 6d 74  T_SEGDIR, &pStmt
37a5f 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
37a60 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
37a61 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
37a62 74 28 70 53 74 6d 74 2c 20 31 2c 20 69 4c 65 76  t(pStmt, 1, iLev
37a63 65 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  el);.    sqlite3
37a64 5f 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d 74 2c  _bind_int(pStmt,
37a65 20 32 2c 20 69 49 64 78 29 3b 0a 20 20 20 20 73   2, iIdx);.    s
37a66 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
37a67 34 28 70 53 74 6d 74 2c 20 33 2c 20 69 53 74 61  4(pStmt, 3, iSta
37a68 72 74 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 73 71  rtBlock);.    sq
37a69 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
37a6a 28 70 53 74 6d 74 2c 20 34 2c 20 69 4c 65 61 66  (pStmt, 4, iLeaf
37a6b 45 6e 64 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 73  EndBlock);.    s
37a6c 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
37a6d 34 28 70 53 74 6d 74 2c 20 35 2c 20 69 45 6e 64  4(pStmt, 5, iEnd
37a6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d  Block);.    rc =
37a6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
37a70 6f 62 28 70 53 74 6d 74 2c 20 36 2c 20 7a 52 6f  ob(pStmt, 6, zRo
37a71 6f 74 2c 20 6e 52 6f 6f 74 2c 20 53 51 4c 49 54  ot, nRoot, SQLIT
37a72 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69  E_STATIC);.    i
37a73 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
37a74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
37a75 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a 20  3_step(pStmt);. 
37a76 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
37a77 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a  3_reset(pStmt);.
37a78 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
37a79 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
37a7a 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
37a7b 6f 66 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72  of the common pr
37a7c 65 66 69 78 20 28 69 66 20 61 6e 79 29 20 73 68  efix (if any) sh
37a7d 61 72 65 64 20 62 79 20 7a 50 72 65 76 20 61 6e  ared by zPrev an
37a7e 64 0a 2a 2a 20 7a 4e 65 78 74 2c 20 69 6e 20 62  d.** zNext, in b
37a7f 79 74 65 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c  ytes. For exampl
37a80 65 2c 20 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73 33  e, .**.**   fts3
37a81 50 72 65 66 69 78 43 6f 6d 70 72 65 73 73 28 22  PrefixCompress("
37a82 61 62 63 22 2c 20 33 2c 20 22 61 62 63 64 65 66  abc", 3, "abcdef
37a83 22 2c 20 36 29 20 20 20 2f 2f 20 72 65 74 75 72  ", 6)   // retur
37a84 6e 73 20 33 0a 2a 2a 20 20 20 66 74 73 33 50 72  ns 3.**   fts3Pr
37a85 65 66 69 78 43 6f 6d 70 72 65 73 73 28 22 61 62  efixCompress("ab
37a86 58 22 2c 20 33 2c 20 22 61 62 63 64 65 66 22 2c  X", 3, "abcdef",
37a87 20 36 29 20 20 20 2f 2f 20 72 65 74 75 72 6e 73   6)   // returns
37a88 20 32 0a 2a 2a 20 20 20 66 74 73 33 50 72 65 66   2.**   fts3Pref
37a89 69 78 43 6f 6d 70 72 65 73 73 28 22 61 62 58 22  ixCompress("abX"
37a8a 2c 20 33 2c 20 22 58 62 63 64 65 66 22 2c 20 36  , 3, "Xbcdef", 6
37a8b 29 20 20 20 2f 2f 20 72 65 74 75 72 6e 73 20 30  )   // returns 0
37a8c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
37a8d 74 73 33 50 72 65 66 69 78 43 6f 6d 70 72 65 73  ts3PrefixCompres
37a8e 73 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  s(.  const char 
37a8f 2a 7a 50 72 65 76 2c 20 20 20 20 20 20 20 20 20  *zPrev,         
37a90 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 63       /* Buffer c
37a91 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65 76 69 6f  ontaining previo
37a92 75 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74  us term */.  int
37a93 20 6e 50 72 65 76 2c 20 20 20 20 20 20 20 20 20   nPrev,         
37a94 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37a95 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 7a  Size of buffer z
37a96 50 72 65 76 20 69 6e 20 62 79 74 65 73 20 2a 2f  Prev in bytes */
37a97 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
37a98 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20  Next,           
37a99 20 20 20 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e     /* Buffer con
37a9a 74 61 69 6e 69 6e 67 20 6e 65 78 74 20 74 65 72  taining next ter
37a9b 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 78 74  m */.  int nNext
37a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a9d 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
37a9e 66 20 62 75 66 66 65 72 20 7a 4e 65 78 74 20 69  f buffer zNext i
37a9f 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20  n bytes */.){.  
37aa0 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28 6e 3d 30  int n;.  for(n=0
37aa1 3b 20 6e 3c 6e 50 72 65 76 20 26 26 20 7a 50 72  ; n<nPrev && zPr
37aa2 65 76 5b 6e 5d 3d 3d 7a 4e 65 78 74 5b 6e 5d 3b  ev[n]==zNext[n];
37aa3 20 6e 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20   n++);.  return 
37aa4 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  n;.}../*.** Add 
37aa5 74 65 72 6d 20 7a 54 65 72 6d 20 74 6f 20 74 68  term zTerm to th
37aa6 65 20 53 65 67 6d 65 6e 74 4e 6f 64 65 2e 20 49  e SegmentNode. I
37aa7 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  t is guaranteed 
37aa8 74 68 61 74 20 7a 54 65 72 6d 20 69 73 20 6c 61  that zTerm is la
37aa9 72 67 65 72 0a 2a 2a 20 28 61 63 63 6f 72 64 69  rger.** (accordi
37aaa 6e 67 20 74 6f 20 6d 65 6d 63 6d 70 29 20 74 68  ng to memcmp) th
37aab 61 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  an the previous 
37aac 74 65 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  term..*/.static 
37aad 69 6e 74 20 66 74 73 33 4e 6f 64 65 41 64 64 54  int fts3NodeAddT
37aae 65 72 6d 28 0a 20 20 46 74 73 33 54 61 62 6c 65  erm(.  Fts3Table
37aaf 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
37ab0 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
37ab1 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ble handle */.  
37ab2 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 2a 70 70  SegmentNode **pp
37ab3 54 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20  Tree,           
37ab4 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 67 6d 65  /* IN/OUT: Segme
37ab5 6e 74 4e 6f 64 65 20 68 61 6e 64 6c 65 20 2a 2f  ntNode handle */
37ab6 20 0a 20 20 69 6e 74 20 69 73 43 6f 70 79 54 65   .  int isCopyTe
37ab7 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rm,             
37ab8 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a      /* True if z
37ab9 54 65 72 6d 2f 6e 54 65 72 6d 20 69 73 20 74 72  Term/nTerm is tr
37aba 61 6e 73 69 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e  ansient */.  con
37abb 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d 2c 20  st char *zTerm, 
37abc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37abd 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65  Pointer to buffe
37abe 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 65 72  r containing ter
37abf 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d  m */.  int nTerm
37ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ac1 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
37ac2 66 20 74 65 72 6d 20 69 6e 20 62 79 74 65 73 20  f term in bytes 
37ac3 2a 2f 0a 29 7b 0a 20 20 53 65 67 6d 65 6e 74 4e  */.){.  SegmentN
37ac4 6f 64 65 20 2a 70 54 72 65 65 20 3d 20 2a 70 70  ode *pTree = *pp
37ac5 54 72 65 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Tree;.  int rc;.
37ac6 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70    SegmentNode *p
37ac7 4e 65 77 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74  New;..  /* First
37ac8 20 74 72 79 20 74 6f 20 61 70 70 65 6e 64 20 74   try to append t
37ac9 68 65 20 74 65 72 6d 20 74 6f 20 74 68 65 20 63  he term to the c
37aca 75 72 72 65 6e 74 20 6e 6f 64 65 2e 20 52 65 74  urrent node. Ret
37acb 75 72 6e 20 65 61 72 6c 79 20 69 66 20 0a 20 20  urn early if .  
37acc 2a 2a 20 74 68 69 73 20 69 73 20 70 6f 73 73 69  ** this is possi
37acd 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ble..  */.  if( 
37ace 70 54 72 65 65 20 29 7b 0a 20 20 20 20 69 6e 74  pTree ){.    int
37acf 20 6e 44 61 74 61 20 3d 20 70 54 72 65 65 2d 3e   nData = pTree->
37ad0 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 43 75  nData;     /* Cu
37ad1 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 6e 6f  rrent size of no
37ad2 64 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  de in bytes */. 
37ad3 20 20 20 69 6e 74 20 6e 52 65 71 20 3d 20 6e 44     int nReq = nD
37ad4 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
37ad5 20 2f 2a 20 52 65 71 75 69 72 65 64 20 73 70 61   /* Required spa
37ad6 63 65 20 61 66 74 65 72 20 61 64 64 69 6e 67 20  ce after adding 
37ad7 7a 54 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74  zTerm */.    int
37ad8 20 6e 50 72 65 66 69 78 3b 20 20 20 20 20 20 20   nPrefix;       
37ad9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
37ada 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
37adb 20 70 72 65 66 69 78 20 63 6f 6d 70 72 65 73 73   prefix compress
37adc 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ion */.    int n
37add 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20  Suffix;         
37ade 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 66 66           /* Suff
37adf 69 78 20 6c 65 6e 67 74 68 20 2a 2f 0a 0a 20 20  ix length */..  
37ae0 20 20 6e 50 72 65 66 69 78 20 3d 20 66 74 73 33    nPrefix = fts3
37ae1 50 72 65 66 69 78 43 6f 6d 70 72 65 73 73 28 70  PrefixCompress(p
37ae2 54 72 65 65 2d 3e 7a 54 65 72 6d 2c 20 70 54 72  Tree->zTerm, pTr
37ae3 65 65 2d 3e 6e 54 65 72 6d 2c 20 7a 54 65 72 6d  ee->nTerm, zTerm
37ae4 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 6e 53  , nTerm);.    nS
37ae5 75 66 66 69 78 20 3d 20 6e 54 65 72 6d 2d 6e 50  uffix = nTerm-nP
37ae6 72 65 66 69 78 3b 0a 0a 20 20 20 20 6e 52 65 71  refix;..    nReq
37ae7 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 56   += sqlite3Fts3V
37ae8 61 72 69 6e 74 4c 65 6e 28 6e 50 72 65 66 69 78  arintLen(nPrefix
37ae9 29 2b 73 71 6c 69 74 65 33 46 74 73 33 56 61 72  )+sqlite3Fts3Var
37aea 69 6e 74 4c 65 6e 28 6e 53 75 66 66 69 78 29 2b  intLen(nSuffix)+
37aeb 6e 53 75 66 66 69 78 3b 0a 20 20 20 20 69 66 28  nSuffix;.    if(
37aec 20 6e 52 65 71 3c 3d 49 4e 54 45 52 49 4f 52 5f   nReq<=INTERIOR_
37aed 4d 41 58 20 7c 7c 20 21 70 54 72 65 65 2d 3e 7a  MAX || !pTree->z
37aee 54 65 72 6d 20 29 7b 0a 0a 20 20 20 20 20 20 69  Term ){..      i
37aef 66 28 20 6e 52 65 71 3e 49 4e 54 45 52 49 4f 52  f( nReq>INTERIOR
37af0 5f 4d 41 58 20 29 7b 0a 20 20 20 20 20 20 20 20  _MAX ){.        
37af1 2f 2a 20 41 6e 20 75 6e 75 73 75 61 6c 20 63 61  /* An unusual ca
37af2 73 65 3a 20 74 68 69 73 20 69 73 20 74 68 65 20  se: this is the 
37af3 66 69 72 73 74 20 74 65 72 6d 20 74 6f 20 62 65  first term to be
37af4 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6e 6f   added to the no
37af5 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  de.        ** an
37af6 64 20 74 68 65 20 73 74 61 74 69 63 20 6e 6f 64  d the static nod
37af7 65 20 62 75 66 66 65 72 20 28 49 4e 54 45 52 49  e buffer (INTERI
37af8 4f 52 5f 4d 41 58 20 62 79 74 65 73 29 20 69 73  OR_MAX bytes) is
37af9 20 6e 6f 74 20 6c 61 72 67 65 0a 20 20 20 20 20   not large.     
37afa 20 20 20 2a 2a 20 65 6e 6f 75 67 68 2e 20 55 73     ** enough. Us
37afb 65 20 61 20 73 65 70 61 72 61 74 65 6c 79 20 6d  e a separately m
37afc 61 6c 6c 6f 63 65 64 20 62 75 66 66 65 72 20 69  alloced buffer i
37afd 6e 73 74 65 61 64 20 54 68 69 73 20 77 61 73 74  nstead This wast
37afe 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 4e  es.        ** IN
37aff 54 45 52 49 4f 52 5f 4d 41 58 20 62 79 74 65 73  TERIOR_MAX bytes
37b00 2c 20 62 75 74 20 73 69 6e 63 65 20 74 68 69 73  , but since this
37b01 20 73 63 65 6e 61 72 69 6f 20 6f 6e 6c 79 20 63   scenario only c
37b02 6f 6d 65 73 20 61 62 6f 75 74 20 77 68 65 6e 0a  omes about when.
37b03 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64          ** the d
37b04 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 20  atabase contain 
37b05 74 77 6f 20 74 65 72 6d 73 20 74 68 61 74 20 73  two terms that s
37b06 68 61 72 65 20 61 20 70 72 65 66 69 78 20 6f 66  hare a prefix of
37b07 20 61 6c 6d 6f 73 74 20 32 4b 42 2c 20 0a 20 20   almost 2KB, .  
37b08 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73        ** this is
37b09 20 6e 6f 74 20 65 78 70 65 63 74 65 64 20 74 6f   not expected to
37b0a 20 62 65 20 61 20 73 65 72 69 6f 75 73 20 70 72   be a serious pr
37b0b 6f 62 6c 65 6d 2e 20 0a 20 20 20 20 20 20 20 20  oblem. .        
37b0c 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
37b0d 74 28 20 70 54 72 65 65 2d 3e 61 44 61 74 61 3d  t( pTree->aData=
37b0e 3d 28 63 68 61 72 20 2a 29 26 70 54 72 65 65 5b  =(char *)&pTree[
37b0f 31 5d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 54  1] );.        pT
37b10 72 65 65 2d 3e 61 44 61 74 61 20 3d 20 28 63 68  ree->aData = (ch
37b11 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar *)sqlite3_mal
37b12 6c 6f 63 28 6e 52 65 71 29 3b 0a 20 20 20 20 20  loc(nReq);.     
37b13 20 20 20 69 66 28 20 21 70 54 72 65 65 2d 3e 61     if( !pTree->a
37b14 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20  Data ){.        
37b15 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
37b16 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 7d  NOMEM;.        }
37b17 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
37b18 69 66 28 20 70 54 72 65 65 2d 3e 7a 54 65 72 6d  if( pTree->zTerm
37b19 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
37b1a 68 65 72 65 20 69 73 20 6e 6f 20 70 72 65 66 69  here is no prefi
37b1b 78 2d 6c 65 6e 67 74 68 20 66 69 65 6c 64 20 66  x-length field f
37b1c 6f 72 20 66 69 72 73 74 20 74 65 72 6d 20 69 6e  or first term in
37b1d 20 61 20 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 20   a node */.     
37b1e 20 20 20 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69     nData += sqli
37b1f 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e 74  te3Fts3PutVarint
37b20 28 26 70 54 72 65 65 2d 3e 61 44 61 74 61 5b 6e  (&pTree->aData[n
37b21 44 61 74 61 5d 2c 20 6e 50 72 65 66 69 78 29 3b  Data], nPrefix);
37b22 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
37b23 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65 33  nData += sqlite3
37b24 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70  Fts3PutVarint(&p
37b25 54 72 65 65 2d 3e 61 44 61 74 61 5b 6e 44 61 74  Tree->aData[nDat
37b26 61 5d 2c 20 6e 53 75 66 66 69 78 29 3b 0a 20 20  a], nSuffix);.  
37b27 20 20 20 20 6d 65 6d 63 70 79 28 26 70 54 72 65      memcpy(&pTre
37b28 65 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61 5d 2c  e->aData[nData],
37b29 20 26 7a 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d   &zTerm[nPrefix]
37b2a 2c 20 6e 53 75 66 66 69 78 29 3b 0a 20 20 20 20  , nSuffix);.    
37b2b 20 20 70 54 72 65 65 2d 3e 6e 44 61 74 61 20 3d    pTree->nData =
37b2c 20 6e 44 61 74 61 20 2b 20 6e 53 75 66 66 69 78   nData + nSuffix
37b2d 3b 0a 20 20 20 20 20 20 70 54 72 65 65 2d 3e 6e  ;.      pTree->n
37b2e 45 6e 74 72 79 2b 2b 3b 0a 0a 20 20 20 20 20 20  Entry++;..      
37b2f 69 66 28 20 69 73 43 6f 70 79 54 65 72 6d 20 29  if( isCopyTerm )
37b30 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54  {.        if( pT
37b31 72 65 65 2d 3e 6e 4d 61 6c 6c 6f 63 3c 6e 54 65  ree->nMalloc<nTe
37b32 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rm ){.          
37b33 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c  char *zNew = sql
37b34 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 54 72  ite3_realloc(pTr
37b35 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 6e 54 65  ee->zMalloc, nTe
37b36 72 6d 2a 32 29 3b 0a 20 20 20 20 20 20 20 20 20  rm*2);.         
37b37 20 69 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20   if( !zNew ){.  
37b38 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
37b39 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
37b3a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
37b3b 20 20 20 20 20 70 54 72 65 65 2d 3e 6e 4d 61 6c       pTree->nMal
37b3c 6c 6f 63 20 3d 20 6e 54 65 72 6d 2a 32 3b 0a 20  loc = nTerm*2;. 
37b3d 20 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e           pTree->
37b3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4e 65 77 3b 0a  zMalloc = zNew;.
37b3f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
37b40 20 20 70 54 72 65 65 2d 3e 7a 54 65 72 6d 20 3d    pTree->zTerm =
37b41 20 70 54 72 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 3b   pTree->zMalloc;
37b42 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
37b43 70 54 72 65 65 2d 3e 7a 54 65 72 6d 2c 20 7a 54  pTree->zTerm, zT
37b44 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  erm, nTerm);.   
37b45 20 20 20 20 20 70 54 72 65 65 2d 3e 6e 54 65 72       pTree->nTer
37b46 6d 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 20  m = nTerm;.     
37b47 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
37b48 70 54 72 65 65 2d 3e 7a 54 65 72 6d 20 3d 20 28  pTree->zTerm = (
37b49 63 68 61 72 20 2a 29 7a 54 65 72 6d 3b 0a 20 20  char *)zTerm;.  
37b4a 20 20 20 20 20 20 70 54 72 65 65 2d 3e 6e 54 65        pTree->nTe
37b4b 72 6d 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20  rm = nTerm;.    
37b4c 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
37b4d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
37b4e 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63  }.  }..  /* If c
37b4f 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20  ontrol flows to 
37b50 68 65 72 65 2c 20 69 74 20 77 61 73 20 6e 6f 74  here, it was not
37b51 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 70 70   possible to app
37b52 65 6e 64 20 7a 54 65 72 6d 20 74 6f 20 74 68 65  end zTerm to the
37b53 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 6e 6f  .  ** current no
37b54 64 65 2e 20 43 72 65 61 74 65 20 61 20 6e 65 77  de. Create a new
37b55 20 6e 6f 64 65 20 28 61 20 72 69 67 68 74 2d 73   node (a right-s
37b56 69 62 6c 69 6e 67 20 6f 66 20 74 68 65 20 63 75  ibling of the cu
37b57 72 72 65 6e 74 20 6e 6f 64 65 29 2e 0a 20 20 2a  rrent node)..  *
37b58 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  * If this is the
37b59 20 66 69 72 73 74 20 6e 6f 64 65 20 69 6e 20 74   first node in t
37b5a 68 65 20 74 72 65 65 2c 20 74 68 65 20 74 65 72  he tree, the ter
37b5b 6d 20 69 73 20 61 64 64 65 64 20 74 6f 20 69 74  m is added to it
37b5c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65  ..  **.  ** Othe
37b5d 72 77 69 73 65 2c 20 74 68 65 20 74 65 72 6d 20  rwise, the term 
37b5e 69 73 20 6e 6f 74 20 61 64 64 65 64 20 74 6f 20  is not added to 
37b5f 74 68 65 20 6e 65 77 20 6e 6f 64 65 2c 20 69 74  the new node, it
37b60 20 69 73 20 6c 65 66 74 20 65 6d 70 74 79 20 66   is left empty f
37b61 6f 72 0a 20 20 2a 2a 20 6e 6f 77 2e 20 49 6e 73  or.  ** now. Ins
37b62 74 65 61 64 2c 20 74 68 65 20 74 65 72 6d 20 69  tead, the term i
37b63 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  s inserted into 
37b64 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 70 54  the parent of pT
37b65 72 65 65 2e 20 49 66 20 70 54 72 65 65 20 0a 20  ree. If pTree . 
37b66 20 2a 2a 20 68 61 73 20 6e 6f 20 70 61 72 65 6e   ** has no paren
37b67 74 2c 20 6f 6e 65 20 69 73 20 63 72 65 61 74 65  t, one is create
37b68 64 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70  d here..  */.  p
37b69 4e 65 77 20 3d 20 28 53 65 67 6d 65 6e 74 4e 6f  New = (SegmentNo
37b6a 64 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  de *)sqlite3_mal
37b6b 6c 6f 63 28 73 69 7a 65 6f 66 28 53 65 67 6d 65  loc(sizeof(Segme
37b6c 6e 74 4e 6f 64 65 29 20 2b 20 49 4e 54 45 52 49  ntNode) + INTERI
37b6d 4f 52 5f 4d 41 58 29 3b 0a 20 20 69 66 28 20 21  OR_MAX);.  if( !
37b6e 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74 75  pNew ){.    retu
37b6f 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
37b70 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e  .  }.  memset(pN
37b71 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 53 65  ew, 0, sizeof(Se
37b72 67 6d 65 6e 74 4e 6f 64 65 29 29 3b 0a 20 20 70  gmentNode));.  p
37b73 4e 65 77 2d 3e 6e 44 61 74 61 20 3d 20 31 20 2b  New->nData = 1 +
37b74 20 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58   FTS3_VARINT_MAX
37b75 3b 0a 20 20 70 4e 65 77 2d 3e 61 44 61 74 61 20  ;.  pNew->aData 
37b76 3d 20 28 63 68 61 72 20 2a 29 26 70 4e 65 77 5b  = (char *)&pNew[
37b77 31 5d 3b 0a 0a 20 20 69 66 28 20 70 54 72 65 65  1];..  if( pTree
37b78 20 29 7b 0a 20 20 20 20 53 65 67 6d 65 6e 74 4e   ){.    SegmentN
37b79 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
37b7a 54 72 65 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  Tree->pParent;. 
37b7b 20 20 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65     rc = fts3Node
37b7c 41 64 64 54 65 72 6d 28 70 2c 20 26 70 50 61 72  AddTerm(p, &pPar
37b7d 65 6e 74 2c 20 69 73 43 6f 70 79 54 65 72 6d 2c  ent, isCopyTerm,
37b7e 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   zTerm, nTerm);.
37b7f 20 20 20 20 69 66 28 20 70 54 72 65 65 2d 3e 70      if( pTree->p
37b80 50 61 72 65 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  Parent==0 ){.   
37b81 20 20 20 70 54 72 65 65 2d 3e 70 50 61 72 65 6e     pTree->pParen
37b82 74 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 20  t = pParent;.   
37b83 20 7d 0a 20 20 20 20 70 54 72 65 65 2d 3e 70 52   }.    pTree->pR
37b84 69 67 68 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20  ight = pNew;.   
37b85 20 70 4e 65 77 2d 3e 70 4c 65 66 74 6d 6f 73 74   pNew->pLeftmost
37b86 20 3d 20 70 54 72 65 65 2d 3e 70 4c 65 66 74 6d   = pTree->pLeftm
37b87 6f 73 74 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70  ost;.    pNew->p
37b88 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74  Parent = pParent
37b89 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 7a 4d 61 6c  ;.    pNew->zMal
37b8a 6c 6f 63 20 3d 20 70 54 72 65 65 2d 3e 7a 4d 61  loc = pTree->zMa
37b8b 6c 6c 6f 63 3b 0a 20 20 20 20 70 4e 65 77 2d 3e  lloc;.    pNew->
37b8c 6e 4d 61 6c 6c 6f 63 20 3d 20 70 54 72 65 65 2d  nMalloc = pTree-
37b8d 3e 6e 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 70 54  >nMalloc;.    pT
37b8e 72 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30  ree->zMalloc = 0
37b8f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
37b90 4e 65 77 2d 3e 70 4c 65 66 74 6d 6f 73 74 20 3d  New->pLeftmost =
37b91 20 70 4e 65 77 3b 0a 20 20 20 20 72 63 20 3d 20   pNew;.    rc = 
37b92 66 74 73 33 4e 6f 64 65 41 64 64 54 65 72 6d 28  fts3NodeAddTerm(
37b93 70 2c 20 26 70 4e 65 77 2c 20 69 73 43 6f 70 79  p, &pNew, isCopy
37b94 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65  Term, zTerm, nTe
37b95 72 6d 29 3b 20 0a 20 20 7d 0a 0a 20 20 2a 70 70  rm); .  }..  *pp
37b96 54 72 65 65 20 3d 20 70 4e 65 77 3b 0a 20 20 72  Tree = pNew;.  r
37b97 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
37b98 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69  ** Helper functi
37b99 6f 6e 20 66 6f 72 20 66 74 73 33 4e 6f 64 65 57  on for fts3NodeW
37b9a 72 69 74 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  rite()..*/.stati
37b9b 63 20 69 6e 74 20 66 74 73 33 54 72 65 65 46 69  c int fts3TreeFi
37b9c 6e 69 73 68 4e 6f 64 65 28 0a 20 20 53 65 67 6d  nishNode(.  Segm
37b9d 65 6e 74 4e 6f 64 65 20 2a 70 54 72 65 65 2c 20  entNode *pTree, 
37b9e 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 2c 20  .  int iHeight, 
37b9f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
37ba0 20 69 4c 65 66 74 43 68 69 6c 64 0a 29 7b 0a 20   iLeftChild.){. 
37ba1 20 69 6e 74 20 6e 53 74 61 72 74 3b 0a 20 20 61   int nStart;.  a
37ba2 73 73 65 72 74 28 20 69 48 65 69 67 68 74 3e 3d  ssert( iHeight>=
37ba3 31 20 26 26 20 69 48 65 69 67 68 74 3c 31 32 38  1 && iHeight<128
37ba4 20 29 3b 0a 20 20 6e 53 74 61 72 74 20 3d 20 46   );.  nStart = F
37ba5 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 20 2d  TS3_VARINT_MAX -
37ba6 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72 69   sqlite3Fts3Vari
37ba7 6e 74 4c 65 6e 28 69 4c 65 66 74 43 68 69 6c 64  ntLen(iLeftChild
37ba8 29 3b 0a 20 20 70 54 72 65 65 2d 3e 61 44 61 74  );.  pTree->aDat
37ba9 61 5b 6e 53 74 61 72 74 5d 20 3d 20 28 63 68 61  a[nStart] = (cha
37baa 72 29 69 48 65 69 67 68 74 3b 0a 20 20 73 71 6c  r)iHeight;.  sql
37bab 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e  ite3Fts3PutVarin
37bac 74 28 26 70 54 72 65 65 2d 3e 61 44 61 74 61 5b  t(&pTree->aData[
37bad 6e 53 74 61 72 74 2b 31 5d 2c 20 69 4c 65 66 74  nStart+1], iLeft
37bae 43 68 69 6c 64 29 3b 0a 20 20 72 65 74 75 72 6e  Child);.  return
37baf 20 6e 53 74 61 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a   nStart;.}../*.*
37bb0 2a 20 57 72 69 74 65 20 74 68 65 20 62 75 66 66  * Write the buff
37bb1 65 72 20 66 6f 72 20 74 68 65 20 73 65 67 6d 65  er for the segme
37bb2 6e 74 20 6e 6f 64 65 20 70 54 72 65 65 20 61 6e  nt node pTree an
37bb3 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 70 65 65  d all of its pee
37bb4 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74  rs to the.** dat
37bb5 61 62 61 73 65 2e 20 54 68 65 6e 20 63 61 6c 6c  abase. Then call
37bb6 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
37bb7 65 63 75 72 73 69 76 65 6c 79 20 74 6f 20 77 72  ecursively to wr
37bb8 69 74 65 20 74 68 65 20 70 61 72 65 6e 74 20 6f  ite the parent o
37bb9 66 20 0a 2a 2a 20 70 54 72 65 65 20 61 6e 64 20  f .** pTree and 
37bba 69 74 73 20 70 65 65 72 73 20 74 6f 20 74 68 65  its peers to the
37bbb 20 64 61 74 61 62 61 73 65 2e 20 0a 2a 2a 0a 2a   database. .**.*
37bbc 2a 20 45 78 63 65 70 74 2c 20 69 66 20 70 54 72  * Except, if pTr
37bbd 65 65 20 69 73 20 61 20 72 6f 6f 74 20 6e 6f 64  ee is a root nod
37bbe 65 2c 20 64 6f 20 6e 6f 74 20 77 72 69 74 65 20  e, do not write 
37bbf 69 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  it to the databa
37bc0 73 65 2e 20 49 6e 73 74 65 61 64 2c 0a 2a 2a 20  se. Instead,.** 
37bc1 73 65 74 20 6f 75 74 70 75 74 20 76 61 72 69 61  set output varia
37bc2 62 6c 65 73 20 2a 70 61 52 6f 6f 74 20 61 6e 64  bles *paRoot and
37bc3 20 2a 70 6e 52 6f 6f 74 20 74 6f 20 63 6f 6e 74   *pnRoot to cont
37bc4 61 69 6e 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64  ain the root nod
37bc5 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63  e..**.** If succ
37bc6 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
37bc7 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  K is returned an
37bc8 64 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  d output variabl
37bc9 65 20 2a 70 69 4c 61 73 74 20 69 73 0a 2a 2a 20  e *piLast is.** 
37bca 73 65 74 20 74 6f 20 74 68 65 20 6c 61 72 67 65  set to the large
37bcb 73 74 20 62 6c 6f 63 6b 69 64 20 77 72 69 74 74  st blockid writt
37bcc 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  en to the databa
37bcd 73 65 20 28 6f 72 20 7a 65 72 6f 20 69 66 20 6e  se (or zero if n
37bce 6f 0a 2a 2a 20 62 6c 6f 63 6b 73 20 77 65 72 65  o.** blocks were
37bcf 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
37bd0 64 62 29 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  db). Otherwise, 
37bd1 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
37bd2 63 6f 64 65 20 69 73 20 0a 2a 2a 20 72 65 74 75  code is .** retu
37bd3 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
37bd4 69 6e 74 20 66 74 73 33 4e 6f 64 65 57 72 69 74  int fts3NodeWrit
37bd5 65 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  e(.  Fts3Table *
37bd6 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
37bd7 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
37bd8 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  table handle */.
37bd9 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70    SegmentNode *p
37bda 54 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20  Tree,           
37bdb 20 20 2f 2a 20 53 65 67 6d 65 6e 74 4e 6f 64 65    /* SegmentNode
37bdc 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74   handle */.  int
37bdd 20 69 48 65 69 67 68 74 2c 20 20 20 20 20 20 20   iHeight,       
37bde 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37bdf 48 65 69 67 68 74 20 6f 66 20 74 68 69 73 20 6e  Height of this n
37be0 6f 64 65 20 69 6e 20 74 72 65 65 20 2a 2f 0a 20  ode in tree */. 
37be1 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
37be2 4c 65 61 66 2c 20 20 20 20 20 20 20 20 20 20 20  Leaf,           
37be3 20 2f 2a 20 42 6c 6f 63 6b 20 69 64 20 6f 66 20   /* Block id of 
37be4 66 69 72 73 74 20 6c 65 61 66 20 6e 6f 64 65 20  first leaf node 
37be5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
37be6 36 34 20 69 46 72 65 65 2c 20 20 20 20 20 20 20  64 iFree,       
37be7 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 69 64       /* Block id
37be8 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 73 6c   of next free sl
37be9 6f 74 20 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73  ot in %_segments
37bea 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
37beb 74 36 34 20 2a 70 69 4c 61 73 74 2c 20 20 20 20  t64 *piLast,    
37bec 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 42 6c        /* OUT: Bl
37bed 6f 63 6b 20 69 64 20 6f 66 20 6c 61 73 74 20 65  ock id of last e
37bee 6e 74 72 79 20 77 72 69 74 74 65 6e 20 2a 2f 0a  ntry written */.
37bef 20 20 63 68 61 72 20 2a 2a 70 61 52 6f 6f 74 2c    char **paRoot,
37bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37bf1 20 20 2f 2a 20 4f 55 54 3a 20 44 61 74 61 20 66    /* OUT: Data f
37bf2 6f 72 20 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0a  or root node */.
37bf3 20 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 20 20 20    int *pnRoot   
37bf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37bf5 20 20 2f 2a 20 4f 55 54 3a 20 53 69 7a 65 20 6f    /* OUT: Size o
37bf6 66 20 72 6f 6f 74 20 6e 6f 64 65 20 69 6e 20 62  f root node in b
37bf7 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ytes */.){.  int
37bf8 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
37bf9 0a 0a 20 20 69 66 28 20 21 70 54 72 65 65 2d 3e  ..  if( !pTree->
37bfa 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 2f  pParent ){.    /
37bfb 2a 20 52 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 74  * Root node of t
37bfc 68 65 20 74 72 65 65 2e 20 2a 2f 0a 20 20 20 20  he tree. */.    
37bfd 69 6e 74 20 6e 53 74 61 72 74 20 3d 20 66 74 73  int nStart = fts
37bfe 33 54 72 65 65 46 69 6e 69 73 68 4e 6f 64 65 28  3TreeFinishNode(
37bff 70 54 72 65 65 2c 20 69 48 65 69 67 68 74 2c 20  pTree, iHeight, 
37c00 69 4c 65 61 66 29 3b 0a 20 20 20 20 2a 70 69 4c  iLeaf);.    *piL
37c01 61 73 74 20 3d 20 69 46 72 65 65 2d 31 3b 0a 20  ast = iFree-1;. 
37c02 20 20 20 2a 70 6e 52 6f 6f 74 20 3d 20 70 54 72     *pnRoot = pTr
37c03 65 65 2d 3e 6e 44 61 74 61 20 2d 20 6e 53 74 61  ee->nData - nSta
37c04 72 74 3b 0a 20 20 20 20 2a 70 61 52 6f 6f 74 20  rt;.    *paRoot 
37c05 3d 20 26 70 54 72 65 65 2d 3e 61 44 61 74 61 5b  = &pTree->aData[
37c06 6e 53 74 61 72 74 5d 3b 0a 20 20 7d 65 6c 73 65  nStart];.  }else
37c07 7b 0a 20 20 20 20 53 65 67 6d 65 6e 74 4e 6f 64  {.    SegmentNod
37c08 65 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 73 71  e *pIter;.    sq
37c09 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 65 78  lite3_int64 iNex
37c0a 74 46 72 65 65 20 3d 20 69 46 72 65 65 3b 0a 20  tFree = iFree;. 
37c0b 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
37c0c 20 69 4e 65 78 74 4c 65 61 66 20 3d 20 69 4c 65   iNextLeaf = iLe
37c0d 61 66 3b 0a 20 20 20 20 66 6f 72 28 70 49 74 65  af;.    for(pIte
37c0e 72 3d 70 54 72 65 65 2d 3e 70 4c 65 66 74 6d 6f  r=pTree->pLeftmo
37c0f 73 74 3b 20 70 49 74 65 72 20 26 26 20 72 63 3d  st; pIter && rc=
37c10 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70 49 74 65  =SQLITE_OK; pIte
37c11 72 3d 70 49 74 65 72 2d 3e 70 52 69 67 68 74 29  r=pIter->pRight)
37c12 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 53 74 61  {.      int nSta
37c13 72 74 20 3d 20 66 74 73 33 54 72 65 65 46 69 6e  rt = fts3TreeFin
37c14 69 73 68 4e 6f 64 65 28 70 49 74 65 72 2c 20 69  ishNode(pIter, i
37c15 48 65 69 67 68 74 2c 20 69 4e 65 78 74 4c 65 61  Height, iNextLea
37c16 66 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 57  f);.      int nW
37c17 72 69 74 65 20 3d 20 70 49 74 65 72 2d 3e 6e 44  rite = pIter->nD
37c18 61 74 61 20 2d 20 6e 53 74 61 72 74 3b 0a 20 20  ata - nStart;.  
37c19 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
37c1a 57 72 69 74 65 53 65 67 6d 65 6e 74 28 70 2c 20  WriteSegment(p, 
37c1b 69 4e 65 78 74 46 72 65 65 2c 20 26 70 49 74 65  iNextFree, &pIte
37c1c 72 2d 3e 61 44 61 74 61 5b 6e 53 74 61 72 74 5d  r->aData[nStart]
37c1d 2c 20 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 20  , nWrite);.     
37c1e 20 69 4e 65 78 74 46 72 65 65 2b 2b 3b 0a 20 20   iNextFree++;.  
37c1f 20 20 20 20 69 4e 65 78 74 4c 65 61 66 20 2b 3d      iNextLeaf +=
37c20 20 28 70 49 74 65 72 2d 3e 6e 45 6e 74 72 79 2b   (pIter->nEntry+
37c21 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  1);.    }.    if
37c22 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
37c23 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
37c24 20 69 4e 65 78 74 4c 65 61 66 3d 3d 69 46 72 65   iNextLeaf==iFre
37c25 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  e );.      rc = 
37c26 66 74 73 33 4e 6f 64 65 57 72 69 74 65 28 0a 20  fts3NodeWrite(. 
37c27 20 20 20 20 20 20 20 20 20 70 2c 20 70 54 72 65           p, pTre
37c28 65 2d 3e 70 50 61 72 65 6e 74 2c 20 69 48 65 69  e->pParent, iHei
37c29 67 68 74 2b 31 2c 20 69 46 72 65 65 2c 20 69 4e  ght+1, iFree, iN
37c2a 65 78 74 46 72 65 65 2c 20 70 69 4c 61 73 74 2c  extFree, piLast,
37c2b 20 70 61 52 6f 6f 74 2c 20 70 6e 52 6f 6f 74 0a   paRoot, pnRoot.
37c2c 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20        );.    }. 
37c2d 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
37c2e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
37c2f 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ll memory alloca
37c30 74 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64  tions associated
37c31 20 77 69 74 68 20 74 68 65 20 74 72 65 65 20 70   with the tree p
37c32 54 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Tree..*/.static 
37c33 76 6f 69 64 20 66 74 73 33 4e 6f 64 65 46 72 65  void fts3NodeFre
37c34 65 28 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70  e(SegmentNode *p
37c35 54 72 65 65 29 7b 0a 20 20 69 66 28 20 70 54 72  Tree){.  if( pTr
37c36 65 65 20 29 7b 0a 20 20 20 20 53 65 67 6d 65 6e  ee ){.    Segmen
37c37 74 4e 6f 64 65 20 2a 70 20 3d 20 70 54 72 65 65  tNode *p = pTree
37c38 2d 3e 70 4c 65 66 74 6d 6f 73 74 3b 0a 20 20 20  ->pLeftmost;.   
37c39 20 66 74 73 33 4e 6f 64 65 46 72 65 65 28 70 2d   fts3NodeFree(p-
37c3a 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 77  >pParent);.    w
37c3b 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 20  hile( p ){.     
37c3c 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 52   SegmentNode *pR
37c3d 69 67 68 74 20 3d 20 70 2d 3e 70 52 69 67 68 74  ight = p->pRight
37c3e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 61  ;.      if( p->a
37c3f 44 61 74 61 21 3d 28 63 68 61 72 20 2a 29 26 70  Data!=(char *)&p
37c40 5b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73  [1] ){.        s
37c41 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61  qlite3_free(p->a
37c42 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Data);.      }. 
37c43 20 20 20 20 20 61 73 73 65 72 74 28 20 70 52 69       assert( pRi
37c44 67 68 74 3d 3d 30 20 7c 7c 20 70 2d 3e 7a 4d 61  ght==0 || p->zMa
37c45 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lloc==0 );.     
37c46 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d   sqlite3_free(p-
37c47 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20  >zMalloc);.     
37c48 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
37c49 3b 0a 20 20 20 20 20 20 70 20 3d 20 70 52 69 67  ;.      p = pRig
37c4a 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ht;.    }.  }.}.
37c4b 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 74 65 72  ./*.** Add a ter
37c4c 6d 20 74 6f 20 74 68 65 20 73 65 67 6d 65 6e 74  m to the segment
37c4d 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74   being construct
37c4e 65 64 20 62 79 20 74 68 65 20 53 65 67 6d 65 6e  ed by the Segmen
37c4f 74 57 72 69 74 65 72 20 6f 62 6a 65 63 74 0a 2a  tWriter object.*
37c50 2a 20 2a 70 70 57 72 69 74 65 72 2e 20 57 68 65  * *ppWriter. Whe
37c51 6e 20 61 64 64 69 6e 67 20 74 68 65 20 66 69 72  n adding the fir
37c52 73 74 20 74 65 72 6d 20 74 6f 20 61 20 73 65 67  st term to a seg
37c53 6d 65 6e 74 2c 20 2a 70 70 57 72 69 74 65 72 20  ment, *ppWriter 
37c54 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 70 61 73  should.** be pas
37c55 73 65 64 20 4e 55 4c 4c 2e 20 54 68 69 73 20 66  sed NULL. This f
37c56 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 61 6c 6c  unction will all
37c57 6f 63 61 74 65 20 61 20 6e 65 77 20 53 65 67 6d  ocate a new Segm
37c58 65 6e 74 57 72 69 74 65 72 20 6f 62 6a 65 63 74  entWriter object
37c59 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 69  .** and return i
37c5a 74 20 76 69 61 20 74 68 65 20 69 6e 70 75 74 2f  t via the input/
37c5b 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20  output variable 
37c5c 2a 70 70 57 72 69 74 65 72 20 69 6e 20 74 68 69  *ppWriter in thi
37c5d 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  s case..**.** If
37c5e 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
37c5f 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
37c60 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61  ed. Otherwise, a
37c61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
37c62 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ode..*/.static i
37c63 6e 74 20 66 74 73 33 53 65 67 57 72 69 74 65 72  nt fts3SegWriter
37c64 41 64 64 28 0a 20 20 46 74 73 33 54 61 62 6c 65  Add(.  Fts3Table
37c65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
37c66 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
37c67 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
37c68 2f 0a 20 20 53 65 67 6d 65 6e 74 57 72 69 74 65  /.  SegmentWrite
37c69 72 20 2a 2a 70 70 57 72 69 74 65 72 2c 20 20 20  r **ppWriter,   
37c6a 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53      /* IN/OUT: S
37c6b 65 67 6d 65 6e 74 57 72 69 74 65 72 20 68 61 6e  egmentWriter han
37c6c 64 6c 65 20 2a 2f 20 0a 20 20 69 6e 74 20 69 73  dle */ .  int is
37c6d 43 6f 70 79 54 65 72 6d 2c 20 20 20 20 20 20 20  CopyTerm,       
37c6e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
37c6f 65 20 69 66 20 62 75 66 66 65 72 20 7a 54 65 72  e if buffer zTer
37c70 6d 20 6d 75 73 74 20 62 65 20 63 6f 70 69 65 64  m must be copied
37c71 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
37c72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20   *zTerm,        
37c73 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
37c74 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61   to buffer conta
37c75 69 6e 69 6e 67 20 74 65 72 6d 20 2a 2f 0a 20 20  ining term */.  
37c76 69 6e 74 20 6e 54 65 72 6d 2c 20 20 20 20 20 20  int nTerm,      
37c77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c78 2f 2a 20 53 69 7a 65 20 6f 66 20 74 65 72 6d 20  /* Size of term 
37c79 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 6f  in bytes */.  co
37c7a 6e 73 74 20 63 68 61 72 20 2a 61 44 6f 63 6c 69  nst char *aDocli
37c7b 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  st,           /*
37c7c 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66   Pointer to buff
37c7d 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64 6f  er containing do
37c7e 63 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  clist */.  int n
37c7f 44 6f 63 6c 69 73 74 20 20 20 20 20 20 20 20 20  Doclist         
37c80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
37c81 7a 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 69 6e  ze of doclist in
37c82 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 69   bytes */.){.  i
37c83 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20 20 20  nt nPrefix;     
37c84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37c85 2a 20 53 69 7a 65 20 6f 66 20 74 65 72 6d 20 70  * Size of term p
37c86 72 65 66 69 78 20 69 6e 20 62 79 74 65 73 20 2a  refix in bytes *
37c87 2f 0a 20 20 69 6e 74 20 6e 53 75 66 66 69 78 3b  /.  int nSuffix;
37c88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c89 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
37c8a 65 72 6d 20 73 75 66 66 69 78 20 69 6e 20 62 79  erm suffix in by
37c8b 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  tes */.  int nRe
37c8c 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
37c8d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
37c8e 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75  er of bytes requ
37c8f 69 72 65 64 20 6f 6e 20 6c 65 61 66 20 70 61 67  ired on leaf pag
37c90 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61  e */.  int nData
37c91 3b 0a 20 20 53 65 67 6d 65 6e 74 57 72 69 74 65  ;.  SegmentWrite
37c92 72 20 2a 70 57 72 69 74 65 72 20 3d 20 2a 70 70  r *pWriter = *pp
37c93 57 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20 21  Writer;..  if( !
37c94 70 57 72 69 74 65 72 20 29 7b 0a 20 20 20 20 69  pWriter ){.    i
37c95 6e 74 20 72 63 3b 0a 20 20 20 20 73 71 6c 69 74  nt rc;.    sqlit
37c96 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a  e3_stmt *pStmt;.
37c97 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
37c98 20 74 68 65 20 53 65 67 6d 65 6e 74 57 72 69 74   the SegmentWrit
37c99 65 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  er structure */.
37c9a 20 20 20 20 70 57 72 69 74 65 72 20 3d 20 28 53      pWriter = (S
37c9b 65 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 29 73  egmentWriter *)s
37c9c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
37c9d 7a 65 6f 66 28 53 65 67 6d 65 6e 74 57 72 69 74  zeof(SegmentWrit
37c9e 65 72 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70  er));.    if( !p
37c9f 57 72 69 74 65 72 20 29 20 72 65 74 75 72 6e 20  Writer ) return 
37ca0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
37ca1 20 20 6d 65 6d 73 65 74 28 70 57 72 69 74 65 72    memset(pWriter
37ca2 2c 20 30 2c 20 73 69 7a 65 6f 66 28 53 65 67 6d  , 0, sizeof(Segm
37ca3 65 6e 74 57 72 69 74 65 72 29 29 3b 0a 20 20 20  entWriter));.   
37ca4 20 2a 70 70 57 72 69 74 65 72 20 3d 20 70 57 72   *ppWriter = pWr
37ca5 69 74 65 72 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c  iter;..    /* Al
37ca6 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65 72 20  locate a buffer 
37ca7 69 6e 20 77 68 69 63 68 20 74 6f 20 61 63 63 75  in which to accu
37ca8 6d 75 6c 61 74 65 20 64 61 74 61 20 2a 2f 0a 20  mulate data */. 
37ca9 20 20 20 70 57 72 69 74 65 72 2d 3e 61 44 61 74     pWriter->aDat
37caa 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  a = (char *)sqli
37cab 74 65 33 5f 6d 61 6c 6c 6f 63 28 4c 45 41 46 5f  te3_malloc(LEAF_
37cac 4d 41 58 29 3b 0a 20 20 20 20 69 66 28 20 21 70  MAX);.    if( !p
37cad 57 72 69 74 65 72 2d 3e 61 44 61 74 61 20 29 20  Writer->aData ) 
37cae 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
37caf 4d 45 4d 3b 0a 20 20 20 20 70 57 72 69 74 65 72  MEM;.    pWriter
37cb0 2d 3e 6e 53 69 7a 65 20 3d 20 4c 45 41 46 5f 4d  ->nSize = LEAF_M
37cb1 41 58 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64  AX;..    /* Find
37cb2 20 74 68 65 20 6e 65 78 74 20 66 72 65 65 20 62   the next free b
37cb3 6c 6f 63 6b 69 64 20 69 6e 20 74 68 65 20 25 5f  lockid in the %_
37cb4 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 2a  segments table *
37cb5 2f 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53  /.    rc = fts3S
37cb6 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 4e 45  qlStmt(p, SQL_NE
37cb7 58 54 5f 53 45 47 4d 45 4e 54 53 5f 49 44 2c 20  XT_SEGMENTS_ID, 
37cb8 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20  &pStmt, 0);.    
37cb9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
37cba 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
37cbb 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f     if( SQLITE_RO
37cbc 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
37cbd 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 20 20  pStmt) ){.      
37cbe 70 57 72 69 74 65 72 2d 3e 69 46 72 65 65 20 3d  pWriter->iFree =
37cbf 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
37cc0 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 30 29 3b  int64(pStmt, 0);
37cc1 0a 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e  .      pWriter->
37cc2 69 46 69 72 73 74 20 3d 20 70 57 72 69 74 65 72  iFirst = pWriter
37cc3 2d 3e 69 46 72 65 65 3b 0a 20 20 20 20 7d 0a 20  ->iFree;.    }. 
37cc4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
37cc5 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20  reset(pStmt);.  
37cc6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
37cc7 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
37cc8 0a 20 20 7d 0a 20 20 6e 44 61 74 61 20 3d 20 70  .  }.  nData = p
37cc9 57 72 69 74 65 72 2d 3e 6e 44 61 74 61 3b 0a 0a  Writer->nData;..
37cca 20 20 6e 50 72 65 66 69 78 20 3d 20 66 74 73 33    nPrefix = fts3
37ccb 50 72 65 66 69 78 43 6f 6d 70 72 65 73 73 28 70  PrefixCompress(p
37ccc 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 2c 20 70  Writer->zTerm, p
37ccd 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 2c 20 7a  Writer->nTerm, z
37cce 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  Term, nTerm);.  
37ccf 6e 53 75 66 66 69 78 20 3d 20 6e 54 65 72 6d 2d  nSuffix = nTerm-
37cd0 6e 50 72 65 66 69 78 3b 0a 0a 20 20 2f 2a 20 46  nPrefix;..  /* F
37cd1 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61  igure out how ma
37cd2 6e 79 20 62 79 74 65 73 20 61 72 65 20 72 65 71  ny bytes are req
37cd3 75 69 72 65 64 20 62 79 20 74 68 69 73 20 6e 65  uired by this ne
37cd4 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 6e 52 65  w entry */.  nRe
37cd5 71 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 56  q = sqlite3Fts3V
37cd6 61 72 69 6e 74 4c 65 6e 28 6e 50 72 65 66 69 78  arintLen(nPrefix
37cd7 29 20 2b 20 20 20 20 2f 2a 20 76 61 72 69 6e 74  ) +    /* varint
37cd8 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65 66   containing pref
37cd9 69 78 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 73  ix size */.    s
37cda 71 6c 69 74 65 33 46 74 73 33 56 61 72 69 6e 74  qlite3Fts3Varint
37cdb 4c 65 6e 28 6e 53 75 66 66 69 78 29 20 2b 20 20  Len(nSuffix) +  
37cdc 20 20 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74         /* varint
37cdd 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73 75 66 66   containing suff
37cde 69 78 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 6e  ix size */.    n
37cdf 53 75 66 66 69 78 20 2b 20 20 20 20 20 20 20 20  Suffix +        
37ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ce1 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 73         /* Term s
37ce2 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 73 71 6c  uffix */.    sql
37ce3 69 74 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65  ite3Fts3VarintLe
37ce4 6e 28 6e 44 6f 63 6c 69 73 74 29 20 2b 20 20 20  n(nDoclist) +   
37ce5 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
37ce6 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 20 20 6e  doclist */.    n
37ce7 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20  Doclist;        
37ce8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ce9 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73         /* Doclis
37cea 74 20 64 61 74 61 20 2a 2f 0a 0a 20 20 69 66 28  t data */..  if(
37ceb 20 6e 44 61 74 61 3e 30 20 26 26 20 6e 44 61 74   nData>0 && nDat
37cec 61 2b 6e 52 65 71 3e 4c 45 41 46 5f 4d 41 58 20  a+nReq>LEAF_MAX 
37ced 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 0a  ){.    int rc;..
37cee 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 72 65      /* The curre
37cef 6e 74 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 20  nt leaf node is 
37cf0 66 75 6c 6c 2e 20 57 72 69 74 65 20 69 74 20 6f  full. Write it o
37cf1 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
37cf2 73 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  se. */.    rc = 
37cf3 66 74 73 33 57 72 69 74 65 53 65 67 6d 65 6e 74  fts3WriteSegment
37cf4 28 70 2c 20 70 57 72 69 74 65 72 2d 3e 69 46 72  (p, pWriter->iFr
37cf5 65 65 2b 2b 2c 20 70 57 72 69 74 65 72 2d 3e 61  ee++, pWriter->a
37cf6 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20  Data, nData);.  
37cf7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
37cf8 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
37cf9 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65  ..    /* Add the
37cfa 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 74 6f   current term to
37cfb 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f   the interior no
37cfc 64 65 20 74 72 65 65 2e 20 54 68 65 20 74 65 72  de tree. The ter
37cfd 6d 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 2a  m added to.    *
37cfe 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 74  * the interior t
37cff 72 65 65 20 6d 75 73 74 3a 0a 20 20 20 20 2a 2a  ree must:.    **
37d00 0a 20 20 20 20 2a 2a 20 20 20 61 29 20 62 65 20  .    **   a) be 
37d01 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
37d02 20 6c 61 72 67 65 73 74 20 74 65 72 6d 20 6f 6e   largest term on
37d03 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6a   the leaf node j
37d04 75 73 74 20 77 72 69 74 74 65 6e 0a 20 20 20 20  ust written.    
37d05 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 64  **      to the d
37d06 61 74 61 62 61 73 65 20 28 73 74 69 6c 6c 20 61  atabase (still a
37d07 76 61 69 6c 61 62 6c 65 20 69 6e 20 70 57 72 69  vailable in pWri
37d08 74 65 72 2d 3e 7a 54 65 72 6d 29 2c 20 61 6e 64  ter->zTerm), and
37d09 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
37d0a 20 62 29 20 62 65 20 6c 65 73 73 20 74 68 61 6e   b) be less than
37d0b 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
37d0c 20 74 65 72 6d 20 61 62 6f 75 74 20 74 6f 20 62   term about to b
37d0d 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6e  e added to the n
37d0e 65 77 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6c  ew.    **      l
37d0f 65 61 66 20 6e 6f 64 65 20 28 7a 54 65 72 6d 2f  eaf node (zTerm/
37d10 6e 54 65 72 6d 29 2e 0a 20 20 20 20 2a 2a 0a 20  nTerm)..    **. 
37d11 20 20 20 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77     ** In other w
37d12 6f 72 64 73 2c 20 69 74 20 6d 75 73 74 20 62 65  ords, it must be
37d13 20 74 68 65 20 70 72 65 66 69 78 20 6f 66 20 7a   the prefix of z
37d14 54 65 72 6d 20 31 20 62 79 74 65 20 6c 6f 6e 67  Term 1 byte long
37d15 65 72 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 74  er than.    ** t
37d16 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78  he common prefix
37d17 20 28 69 66 20 61 6e 79 29 20 6f 66 20 7a 54 65   (if any) of zTe
37d18 72 6d 20 61 6e 64 20 70 57 72 69 74 65 72 2d 3e  rm and pWriter->
37d19 7a 54 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20  zTerm..    */.  
37d1a 20 20 61 73 73 65 72 74 28 20 6e 50 72 65 66 69    assert( nPrefi
37d1b 78 3c 6e 54 65 72 6d 20 29 3b 0a 20 20 20 20 72  x<nTerm );.    r
37d1c 63 20 3d 20 66 74 73 33 4e 6f 64 65 41 64 64 54  c = fts3NodeAddT
37d1d 65 72 6d 28 70 2c 20 26 70 57 72 69 74 65 72 2d  erm(p, &pWriter-
37d1e 3e 70 54 72 65 65 2c 20 69 73 43 6f 70 79 54 65  >pTree, isCopyTe
37d1f 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 50 72 65 66  rm, zTerm, nPref
37d20 69 78 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 72  ix+1);.    if( r
37d21 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
37d22 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 6e  eturn rc;..    n
37d23 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 57  Data = 0;.    pW
37d24 72 69 74 65 72 2d 3e 6e 54 65 72 6d 20 3d 20 30  riter->nTerm = 0
37d25 3b 0a 0a 20 20 20 20 6e 50 72 65 66 69 78 20 3d  ;..    nPrefix =
37d26 20 30 3b 0a 20 20 20 20 6e 53 75 66 66 69 78 20   0;.    nSuffix 
37d27 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 6e 52 65  = nTerm;.    nRe
37d28 71 20 3d 20 31 20 2b 20 20 20 20 20 20 20 20 20  q = 1 +         
37d29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d2a 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63       /* varint c
37d2b 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65 66 69 78  ontaining prefix
37d2c 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 73   size */.      s
37d2d 71 6c 69 74 65 33 46 74 73 33 56 61 72 69 6e 74  qlite3Fts3Varint
37d2e 4c 65 6e 28 6e 54 65 72 6d 29 20 2b 20 20 20 20  Len(nTerm) +    
37d2f 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63       /* varint c
37d30 6f 6e 74 61 69 6e 69 6e 67 20 73 75 66 66 69 78  ontaining suffix
37d31 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 6e   size */.      n
37d32 54 65 72 6d 20 2b 20 20 20 20 20 20 20 20 20 20  Term +          
37d33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d34 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 73 75 66       /* Term suf
37d35 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  fix */.      sql
37d36 69 74 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65  ite3Fts3VarintLe
37d37 6e 28 6e 44 6f 63 6c 69 73 74 29 20 2b 20 20 20  n(nDoclist) +   
37d38 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f     /* Size of do
37d39 63 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 6e  clist */.      n
37d3a 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20  Doclist;        
37d3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d3c 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20       /* Doclist 
37d3d 64 61 74 61 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f  data */.  }..  /
37d3e 2a 20 49 66 20 74 68 65 20 62 75 66 66 65 72 20  * If the buffer 
37d3f 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
37d40 74 65 64 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c  ted is too small
37d41 20 66 6f 72 20 74 68 69 73 20 65 6e 74 72 79 2c   for this entry,
37d42 20 72 65 61 6c 6c 6f 63 0a 20 20 2a 2a 20 74 68   realloc.  ** th
37d43 65 20 62 75 66 66 65 72 20 74 6f 20 6d 61 6b 65  e buffer to make
37d44 20 69 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68   it large enough
37d45 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 52 65  ..  */.  if( nRe
37d46 71 3e 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65  q>pWriter->nSize
37d47 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 61 4e   ){.    char *aN
37d48 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
37d49 6c 6c 6f 63 28 70 57 72 69 74 65 72 2d 3e 61 44  lloc(pWriter->aD
37d4a 61 74 61 2c 20 6e 52 65 71 29 3b 0a 20 20 20 20  ata, nReq);.    
37d4b 69 66 28 20 21 61 4e 65 77 20 29 20 72 65 74 75  if( !aNew ) retu
37d4c 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
37d4d 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 61 44  .    pWriter->aD
37d4e 61 74 61 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20  ata = aNew;.    
37d4f 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65 20 3d  pWriter->nSize =
37d50 20 6e 52 65 71 3b 0a 20 20 7d 0a 20 20 61 73 73   nReq;.  }.  ass
37d51 65 72 74 28 20 6e 44 61 74 61 2b 6e 52 65 71 3c  ert( nData+nReq<
37d52 3d 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65 20  =pWriter->nSize 
37d53 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20  );..  /* Append 
37d54 74 68 65 20 70 72 65 66 69 78 2d 63 6f 6d 70 72  the prefix-compr
37d55 65 73 73 65 64 20 74 65 72 6d 20 61 6e 64 20 64  essed term and d
37d56 6f 63 6c 69 73 74 20 74 6f 20 74 68 65 20 62 75  oclist to the bu
37d57 66 66 65 72 2e 20 2a 2f 0a 20 20 6e 44 61 74 61  ffer. */.  nData
37d58 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50   += sqlite3Fts3P
37d59 75 74 56 61 72 69 6e 74 28 26 70 57 72 69 74 65  utVarint(&pWrite
37d5a 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61 5d 2c  r->aData[nData],
37d5b 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 6e 44 61   nPrefix);.  nDa
37d5c 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  ta += sqlite3Fts
37d5d 33 50 75 74 56 61 72 69 6e 74 28 26 70 57 72 69  3PutVarint(&pWri
37d5e 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61  ter->aData[nData
37d5f 5d 2c 20 6e 53 75 66 66 69 78 29 3b 0a 20 20 6d  ], nSuffix);.  m
37d60 65 6d 63 70 79 28 26 70 57 72 69 74 65 72 2d 3e  emcpy(&pWriter->
37d61 61 44 61 74 61 5b 6e 44 61 74 61 5d 2c 20 26 7a  aData[nData], &z
37d62 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 2c 20 6e  Term[nPrefix], n
37d63 53 75 66 66 69 78 29 3b 0a 20 20 6e 44 61 74 61  Suffix);.  nData
37d64 20 2b 3d 20 6e 53 75 66 66 69 78 3b 0a 20 20 6e   += nSuffix;.  n
37d65 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46  Data += sqlite3F
37d66 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70 57  ts3PutVarint(&pW
37d67 72 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61  riter->aData[nDa
37d68 74 61 5d 2c 20 6e 44 6f 63 6c 69 73 74 29 3b 0a  ta], nDoclist);.
37d69 20 20 6d 65 6d 63 70 79 28 26 70 57 72 69 74 65    memcpy(&pWrite
37d6a 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61 5d 2c  r->aData[nData],
37d6b 20 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f 63 6c   aDoclist, nDocl
37d6c 69 73 74 29 3b 0a 20 20 70 57 72 69 74 65 72 2d  ist);.  pWriter-
37d6d 3e 6e 44 61 74 61 20 3d 20 6e 44 61 74 61 20 2b  >nData = nData +
37d6e 20 6e 44 6f 63 6c 69 73 74 3b 0a 0a 20 20 2f 2a   nDoclist;..  /*
37d6f 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e   Save the curren
37d70 74 20 74 65 72 6d 20 73 6f 20 74 68 61 74 20 69  t term so that i
37d71 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  t can be used to
37d72 20 70 72 65 66 69 78 2d 63 6f 6d 70 72 65 73 73   prefix-compress
37d73 20 74 68 65 20 6e 65 78 74 2e 0a 20 20 2a 2a 20   the next..  ** 
37d74 49 66 20 74 68 65 20 69 73 43 6f 70 79 54 65 72  If the isCopyTer
37d75 6d 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  m parameter is t
37d76 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 62 75  rue, then the bu
37d77 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20  ffer pointed to 
37d78 62 79 0a 20 20 2a 2a 20 7a 54 65 72 6d 20 69 73  by.  ** zTerm is
37d79 20 74 72 61 6e 73 69 65 6e 74 2c 20 73 6f 20 74   transient, so t
37d7a 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
37d7b 65 20 74 65 72 6d 20 64 61 74 61 2e 20 4f 74 68  e term data. Oth
37d7c 65 72 77 69 73 65 2c 20 6a 75 73 74 0a 20 20 2a  erwise, just.  *
37d7d 2a 20 73 74 6f 72 65 20 61 20 63 6f 70 79 20 6f  * store a copy o
37d7e 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2e 0a 20  f the pointer.. 
37d7f 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 70 79   */.  if( isCopy
37d80 54 65 72 6d 20 29 7b 0a 20 20 20 20 69 66 28 20  Term ){.    if( 
37d81 6e 54 65 72 6d 3e 70 57 72 69 74 65 72 2d 3e 6e  nTerm>pWriter->n
37d82 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20  Malloc ){.      
37d83 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c  char *zNew = sql
37d84 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 57 72  ite3_realloc(pWr
37d85 69 74 65 72 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 6e  iter->zMalloc, n
37d86 54 65 72 6d 2a 32 29 3b 0a 20 20 20 20 20 20 69  Term*2);.      i
37d87 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20 20  f( !zNew ){.    
37d88 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
37d89 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
37d8a 0a 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e  .      pWriter->
37d8b 6e 4d 61 6c 6c 6f 63 20 3d 20 6e 54 65 72 6d 2a  nMalloc = nTerm*
37d8c 32 3b 0a 20 20 20 20 20 20 70 57 72 69 74 65 72  2;.      pWriter
37d8d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4e 65 77  ->zMalloc = zNew
37d8e 3b 0a 20 20 20 20 20 20 70 57 72 69 74 65 72 2d  ;.      pWriter-
37d8f 3e 7a 54 65 72 6d 20 3d 20 7a 4e 65 77 3b 0a 20  >zTerm = zNew;. 
37d90 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
37d91 20 70 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 3d   pWriter->zTerm=
37d92 3d 70 57 72 69 74 65 72 2d 3e 7a 4d 61 6c 6c 6f  =pWriter->zMallo
37d93 63 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  c );.    memcpy(
37d94 70 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 2c 20  pWriter->zTerm, 
37d95 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20  zTerm, nTerm);. 
37d96 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 57 72 69   }else{.    pWri
37d97 74 65 72 2d 3e 7a 54 65 72 6d 20 3d 20 28 63 68  ter->zTerm = (ch
37d98 61 72 20 2a 29 7a 54 65 72 6d 3b 0a 20 20 7d 0a  ar *)zTerm;.  }.
37d99 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d    pWriter->nTerm
37d9a 20 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20 72 65 74   = nTerm;..  ret
37d9b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
37d9c 0a 0a 2f 2a 0a 2a 2a 20 46 6c 75 73 68 20 61 6c  ../*.** Flush al
37d9d 6c 20 64 61 74 61 20 61 73 73 6f 63 69 61 74 65  l data associate
37d9e 64 20 77 69 74 68 20 74 68 65 20 53 65 67 6d 65  d with the Segme
37d9f 6e 74 57 72 69 74 65 72 20 6f 62 6a 65 63 74 20  ntWriter object 
37da0 70 57 72 69 74 65 72 20 74 6f 20 74 68 65 0a 2a  pWriter to the.*
37da1 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73  * database. This
37da2 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62   function must b
37da3 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  e called after a
37da4 6c 6c 20 74 65 72 6d 73 20 68 61 76 65 20 62 65  ll terms have be
37da5 65 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20 74  en added.** to t
37da6 68 65 20 73 65 67 6d 65 6e 74 20 75 73 69 6e 67  he segment using
37da7 20 66 74 73 33 53 65 67 57 72 69 74 65 72 41 64   fts3SegWriterAd
37da8 64 28 29 2e 20 49 66 20 73 75 63 63 65 73 73 66  d(). If successf
37da9 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  ul, SQLITE_OK is
37daa 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74  .** returned. Ot
37dab 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51 4c 69  herwise, an SQLi
37dac 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  te error code..*
37dad 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
37dae 33 53 65 67 57 72 69 74 65 72 46 6c 75 73 68 28  3SegWriterFlush(
37daf 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
37db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37db1 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
37db2 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ble handle */.  
37db3 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 70  SegmentWriter *p
37db4 57 72 69 74 65 72 2c 20 20 20 20 20 20 20 20 20  Writer,         
37db5 2f 2a 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72  /* SegmentWriter
37db6 20 74 6f 20 66 6c 75 73 68 20 74 6f 20 74 68 65   to flush to the
37db7 20 64 62 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65   db */.  int iLe
37db8 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  vel,            
37db9 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
37dba 65 20 66 6f 72 20 27 6c 65 76 65 6c 27 20 63 6f  e for 'level' co
37dbb 6c 75 6d 6e 20 6f 66 20 25 5f 73 65 67 64 69 72  lumn of %_segdir
37dbc 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 20   */.  int iIdx  
37dbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37dbe 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66        /* Value f
37dbf 6f 72 20 27 69 64 78 27 20 63 6f 6c 75 6d 6e 20  or 'idx' column 
37dc0 6f 66 20 25 5f 73 65 67 64 69 72 20 2a 2f 0a 29  of %_segdir */.)
37dc1 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
37dc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37dc3 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
37dc4 64 65 20 2a 2f 0a 20 20 69 66 28 20 70 57 72 69  de */.  if( pWri
37dc5 74 65 72 2d 3e 70 54 72 65 65 20 29 7b 0a 20 20  ter->pTree ){.  
37dc6 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
37dc7 69 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20  iLast;          
37dc8 2f 2a 20 4c 61 72 67 65 73 74 20 62 6c 6f 63 6b  /* Largest block
37dc9 20 69 64 20 77 72 69 74 74 65 6e 20 74 6f 20 64   id written to d
37dca 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 73  atabase */.    s
37dcb 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4c 61  qlite3_int64 iLa
37dcc 73 74 4c 65 61 66 3b 20 20 20 20 20 20 2f 2a 20  stLeaf;      /* 
37dcd 4c 61 72 67 65 73 74 20 6c 65 61 66 20 62 6c 6f  Largest leaf blo
37dce 63 6b 20 69 64 20 77 72 69 74 74 65 6e 20 74 6f  ck id written to
37dcf 20 64 62 20 2a 2f 0a 20 20 20 20 63 68 61 72 20   db */.    char 
37dd0 2a 7a 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20  *zRoot;         
37dd1 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
37dd2 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f  ter to buffer co
37dd3 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6e 6f  ntaining root no
37dd4 64 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 52  de */.    int nR
37dd5 6f 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  oot;            
37dd6 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
37dd7 6f 66 20 62 75 66 66 65 72 20 7a 52 6f 6f 74 20  of buffer zRoot 
37dd8 2a 2f 0a 0a 20 20 20 20 69 4c 61 73 74 4c 65 61  */..    iLastLea
37dd9 66 20 3d 20 70 57 72 69 74 65 72 2d 3e 69 46 72  f = pWriter->iFr
37dda 65 65 3b 0a 20 20 20 20 72 63 20 3d 20 66 74 73  ee;.    rc = fts
37ddb 33 57 72 69 74 65 53 65 67 6d 65 6e 74 28 70 2c  3WriteSegment(p,
37ddc 20 70 57 72 69 74 65 72 2d 3e 69 46 72 65 65 2b   pWriter->iFree+
37ddd 2b 2c 20 70 57 72 69 74 65 72 2d 3e 61 44 61 74  +, pWriter->aDat
37dde 61 2c 20 70 57 72 69 74 65 72 2d 3e 6e 44 61 74  a, pWriter->nDat
37ddf 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  a);.    if( rc==
37de0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
37de1 20 20 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65     rc = fts3Node
37de2 57 72 69 74 65 28 70 2c 20 70 57 72 69 74 65 72  Write(p, pWriter
37de3 2d 3e 70 54 72 65 65 2c 20 31 2c 0a 20 20 20 20  ->pTree, 1,.    
37de4 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69        pWriter->i
37de5 46 69 72 73 74 2c 20 70 57 72 69 74 65 72 2d 3e  First, pWriter->
37de6 69 46 72 65 65 2c 20 26 69 4c 61 73 74 2c 20 26  iFree, &iLast, &
37de7 7a 52 6f 6f 74 2c 20 26 6e 52 6f 6f 74 29 3b 0a  zRoot, &nRoot);.
37de8 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
37de9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
37dea 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 57 72       rc = fts3Wr
37deb 69 74 65 53 65 67 64 69 72 28 0a 20 20 20 20 20  iteSegdir(.     
37dec 20 20 20 20 20 70 2c 20 69 4c 65 76 65 6c 2c 20       p, iLevel, 
37ded 69 49 64 78 2c 20 70 57 72 69 74 65 72 2d 3e 69  iIdx, pWriter->i
37dee 46 69 72 73 74 2c 20 69 4c 61 73 74 4c 65 61 66  First, iLastLeaf
37def 2c 20 69 4c 61 73 74 2c 20 7a 52 6f 6f 74 2c 20  , iLast, zRoot, 
37df0 6e 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20  nRoot);.    }.  
37df1 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
37df2 65 20 65 6e 74 69 72 65 20 74 72 65 65 20 66 69  e entire tree fi
37df3 74 73 20 6f 6e 20 74 68 65 20 72 6f 6f 74 20 6e  ts on the root n
37df4 6f 64 65 2e 20 57 72 69 74 65 20 69 74 20 74 6f  ode. Write it to
37df5 20 74 68 65 20 73 65 67 64 69 72 20 74 61 62 6c   the segdir tabl
37df6 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 66  e. */.    rc = f
37df7 74 73 33 57 72 69 74 65 53 65 67 64 69 72 28 0a  ts3WriteSegdir(.
37df8 20 20 20 20 20 20 20 20 70 2c 20 69 4c 65 76 65          p, iLeve
37df9 6c 2c 20 69 49 64 78 2c 20 30 2c 20 30 2c 20 30  l, iIdx, 0, 0, 0
37dfa 2c 20 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61  , pWriter->aData
37dfb 2c 20 70 57 72 69 74 65 72 2d 3e 6e 44 61 74 61  , pWriter->nData
37dfc 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
37dfd 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c  rc;.}../*.** Rel
37dfe 65 61 73 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  ease all memory 
37dff 68 65 6c 64 20 62 79 20 74 68 65 20 53 65 67 6d  held by the Segm
37e00 65 6e 74 57 72 69 74 65 72 20 6f 62 6a 65 63 74  entWriter object
37e01 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 0a   passed as the .
37e02 2a 2a 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  ** first argumen
37e03 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
37e04 64 20 66 74 73 33 53 65 67 57 72 69 74 65 72 46  d fts3SegWriterF
37e05 72 65 65 28 53 65 67 6d 65 6e 74 57 72 69 74 65  ree(SegmentWrite
37e06 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69  r *pWriter){.  i
37e07 66 28 20 70 57 72 69 74 65 72 20 29 7b 0a 20 20  f( pWriter ){.  
37e08 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
37e09 57 72 69 74 65 72 2d 3e 61 44 61 74 61 29 3b 0a  Writer->aData);.
37e0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
37e0b 28 70 57 72 69 74 65 72 2d 3e 7a 4d 61 6c 6c 6f  (pWriter->zMallo
37e0c 63 29 3b 0a 20 20 20 20 66 74 73 33 4e 6f 64 65  c);.    fts3Node
37e0d 46 72 65 65 28 70 57 72 69 74 65 72 2d 3e 70 54  Free(pWriter->pT
37e0e 72 65 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ree);.    sqlite
37e0f 33 5f 66 72 65 65 28 70 57 72 69 74 65 72 29 3b  3_free(pWriter);
37e10 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
37e11 65 20 66 69 72 73 74 20 76 61 6c 75 65 20 69 6e  e first value in
37e12 20 74 68 65 20 61 70 56 61 6c 5b 5d 20 61 72 72   the apVal[] arr
37e13 61 79 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  ay is assumed to
37e14 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65   contain an inte
37e15 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ger..** This fun
37e16 63 74 69 6f 6e 20 74 65 73 74 73 20 69 66 20 74  ction tests if t
37e17 68 65 72 65 20 65 78 69 73 74 20 61 6e 79 20 64  here exist any d
37e18 6f 63 75 6d 65 6e 74 73 20 77 69 74 68 20 64 6f  ocuments with do
37e19 63 69 64 20 76 61 6c 75 65 73 20 74 68 61 74 0a  cid values that.
37e1a 2a 2a 20 61 72 65 20 64 69 66 66 65 72 65 6e 74  ** are different
37e1b 20 66 72 6f 6d 20 74 68 61 74 20 69 6e 74 65 67   from that integ
37e1c 65 72 2e 20 69 2e 65 2e 20 69 66 20 64 65 6c 65  er. i.e. if dele
37e1d 74 69 6e 67 20 74 68 65 20 64 6f 63 75 6d 65 6e  ting the documen
37e1e 74 20 77 69 74 68 20 64 6f 63 69 64 0a 2a 2a 20  t with docid.** 
37e1f 61 70 56 61 6c 5b 30 5d 20 77 6f 75 6c 64 20 6d  apVal[0] would m
37e20 65 61 6e 20 74 68 65 20 46 54 53 33 20 74 61 62  ean the FTS3 tab
37e21 6c 65 20 77 65 72 65 20 65 6d 70 74 79 2e 0a 2a  le were empty..*
37e22 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
37e23 75 6c 2c 20 2a 70 69 73 45 6d 70 74 79 20 69 73  ul, *pisEmpty is
37e24 20 73 65 74 20 74 6f 20 74 72 75 65 20 69 66 20   set to true if 
37e25 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
37e26 74 79 20 65 78 63 65 70 74 20 66 6f 72 0a 2a 2a  ty except for.**
37e27 20 64 6f 63 75 6d 65 6e 74 20 61 70 56 61 6c 5b   document apVal[
37e28 30 5d 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68  0], or false oth
37e29 65 72 77 69 73 65 2c 20 61 6e 64 20 53 51 4c 49  erwise, and SQLI
37e2a 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
37e2b 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f  d. If an.** erro
37e2c 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c  r occurs, an SQL
37e2d 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ite error code i
37e2e 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  s returned..*/.s
37e2f 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 49 73  tatic int fts3Is
37e30 45 6d 70 74 79 28 46 74 73 33 54 61 62 6c 65 20  Empty(Fts3Table 
37e31 2a 70 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  *p, sqlite3_valu
37e32 65 20 2a 2a 61 70 56 61 6c 2c 20 69 6e 74 20 2a  e **apVal, int *
37e33 70 69 73 45 6d 70 74 79 29 7b 0a 20 20 73 71 6c  pisEmpty){.  sql
37e34 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
37e35 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63  ;.  int rc;.  rc
37e36 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70   = fts3SqlStmt(p
37e37 2c 20 53 51 4c 5f 49 53 5f 45 4d 50 54 59 2c 20  , SQL_IS_EMPTY, 
37e38 26 70 53 74 6d 74 2c 20 61 70 56 61 6c 29 3b 0a  &pStmt, apVal);.
37e39 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
37e3a 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 53  _OK ){.    if( S
37e3b 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
37e3c 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
37e3d 7b 0a 20 20 20 20 20 20 2a 70 69 73 45 6d 70 74  {.      *pisEmpt
37e3e 79 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  y = sqlite3_colu
37e3f 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30 29  mn_int(pStmt, 0)
37e40 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
37e41 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
37e42 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Stmt);.  }.  ret
37e43 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
37e44 20 53 65 74 20 2a 70 6e 53 65 67 6d 65 6e 74 20   Set *pnSegment 
37e45 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
37e46 20 73 65 67 6d 65 6e 74 73 20 6f 66 20 6c 65 76   segments of lev
37e47 65 6c 20 69 4c 65 76 65 6c 20 69 6e 20 74 68 65  el iLevel in the
37e48 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
37e49 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
37e4a 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c  K if successful,
37e4b 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72   or an SQLite er
37e4c 72 6f 72 20 63 6f 64 65 20 69 66 20 6e 6f 74 2e  ror code if not.
37e4d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
37e4e 74 73 33 53 65 67 6d 65 6e 74 43 6f 75 6e 74 28  ts3SegmentCount(
37e4f 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 69 6e  Fts3Table *p, in
37e50 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 2a 70  t iLevel, int *p
37e51 6e 53 65 67 6d 65 6e 74 29 7b 0a 20 20 73 71 6c  nSegment){.  sql
37e52 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
37e53 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
37e54 73 73 65 72 74 28 20 69 4c 65 76 65 6c 3e 3d 30  ssert( iLevel>=0
37e55 20 29 3b 0a 20 20 72 63 20 3d 20 66 74 73 33 53   );.  rc = fts3S
37e56 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45  qlStmt(p, SQL_SE
37e57 4c 45 43 54 5f 4c 45 56 45 4c 5f 43 4f 55 4e 54  LECT_LEVEL_COUNT
37e58 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  , &pStmt, 0);.  
37e59 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
37e5a 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
37e5b 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
37e5c 74 28 70 53 74 6d 74 2c 20 31 2c 20 69 4c 65 76  t(pStmt, 1, iLev
37e5d 65 6c 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  el);.  if( SQLIT
37e5e 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
37e5f 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20  tep(pStmt) ){.  
37e60 20 20 2a 70 6e 53 65 67 6d 65 6e 74 20 3d 20 73    *pnSegment = s
37e61 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
37e62 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 7d  t(pStmt, 0);.  }
37e63 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
37e64 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a  3_reset(pStmt);.
37e65 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 6e  }../*.** Set *pn
37e66 53 65 67 6d 65 6e 74 20 74 6f 20 74 68 65 20 74  Segment to the t
37e67 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73  otal number of s
37e68 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 64  egments in the d
37e69 61 74 61 62 61 73 65 2e 20 53 65 74 0a 2a 2a 20  atabase. Set.** 
37e6a 2a 70 6e 4d 61 78 20 74 6f 20 74 68 65 20 6c 61  *pnMax to the la
37e6b 72 67 65 73 74 20 73 65 67 6d 65 6e 74 20 6c 65  rgest segment le
37e6c 76 65 6c 20 69 6e 20 74 68 65 20 64 61 74 61 62  vel in the datab
37e6d 61 73 65 20 28 73 65 67 6d 65 6e 74 20 6c 65 76  ase (segment lev
37e6e 65 6c 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65  els.** are store
37e6f 64 20 69 6e 20 74 68 65 20 27 6c 65 76 65 6c 27  d in the 'level'
37e70 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 25   column of the %
37e71 5f 73 65 67 64 69 72 20 74 61 62 6c 65 29 2e 0a  _segdir table)..
37e72 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
37e73 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73  ITE_OK if succes
37e74 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69  sful, or an SQLi
37e75 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  te error code if
37e76 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   not..*/.static 
37e77 69 6e 74 20 66 74 73 33 53 65 67 6d 65 6e 74 43  int fts3SegmentC
37e78 6f 75 6e 74 4d 61 78 28 46 74 73 33 54 61 62 6c  ountMax(Fts3Tabl
37e79 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 53 65 67  e *p, int *pnSeg
37e7a 6d 65 6e 74 2c 20 69 6e 74 20 2a 70 6e 4d 61 78  ment, int *pnMax
37e7b 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
37e7c 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20  t *pStmt;.  int 
37e7d 72 63 3b 0a 0a 20 20 72 63 20 3d 20 66 74 73 33  rc;..  rc = fts3
37e7e 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53  SqlStmt(p, SQL_S
37e7f 45 4c 45 43 54 5f 53 45 47 44 49 52 5f 43 4f 55  ELECT_SEGDIR_COU
37e80 4e 54 5f 4d 41 58 2c 20 26 70 53 74 6d 74 2c 20  NT_MAX, &pStmt, 
37e81 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  0);.  if( rc!=SQ
37e82 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
37e83 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c 49 54   rc;.  if( SQLIT
37e84 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
37e85 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20  tep(pStmt) ){.  
37e86 20 20 2a 70 6e 53 65 67 6d 65 6e 74 20 3d 20 73    *pnSegment = s
37e87 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
37e88 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20  t(pStmt, 0);.   
37e89 20 2a 70 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65   *pnMax = sqlite
37e8a 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53 74  3_column_int(pSt
37e8b 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65  mt, 1);.  }.  re
37e8c 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73  turn sqlite3_res
37e8d 65 74 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a  et(pStmt);.}../*
37e8e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
37e8f 6e 20 69 73 20 75 73 65 64 20 61 66 74 65 72 20  n is used after 
37e90 6d 65 72 67 69 6e 67 20 6d 75 6c 74 69 70 6c 65  merging multiple
37e91 20 73 65 67 6d 65 6e 74 73 20 69 6e 74 6f 20 61   segments into a
37e92 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 0a 2a 2a   single large.**
37e93 20 73 65 67 6d 65 6e 74 20 74 6f 20 64 65 6c 65   segment to dele
37e94 74 65 20 74 68 65 20 6f 6c 64 2c 20 6e 6f 77 20  te the old, now 
37e95 72 65 64 75 6e 64 61 6e 74 2c 20 73 65 67 6d 65  redundant, segme
37e96 6e 74 20 62 2d 74 72 65 65 73 2e 20 53 70 65 63  nt b-trees. Spec
37e97 69 66 69 63 61 6c 6c 79 2c 0a 2a 2a 20 69 74 3a  ifically,.** it:
37e98 0a 2a 2a 20 0a 2a 2a 20 20 20 31 29 20 44 65 6c  .** .**   1) Del
37e99 65 74 65 73 20 61 6c 6c 20 25 5f 73 65 67 6d 65  etes all %_segme
37e9a 6e 74 73 20 65 6e 74 72 69 65 73 20 66 6f 72 20  nts entries for 
37e9b 74 68 65 20 73 65 67 6d 65 6e 74 73 20 61 73 73  the segments ass
37e9c 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a  ociated with .**
37e9d 20 20 20 20 20 20 65 61 63 68 20 6f 66 20 74 68        each of th
37e9e 65 20 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65  e SegReader obje
37e9f 63 74 73 20 69 6e 20 74 68 65 20 61 72 72 61 79  cts in the array
37ea0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74   passed as the t
37ea1 68 69 72 64 20 0a 2a 2a 20 20 20 20 20 20 61 72  hird .**      ar
37ea2 67 75 6d 65 6e 74 2c 20 61 6e 64 0a 2a 2a 0a 2a  gument, and.**.*
37ea3 2a 20 20 20 32 29 20 64 65 6c 65 74 65 73 20 61  *   2) deletes a
37ea4 6c 6c 20 25 5f 73 65 67 64 69 72 20 65 6e 74 72  ll %_segdir entr
37ea5 69 65 73 20 77 69 74 68 20 6c 65 76 65 6c 20 69  ies with level i
37ea6 4c 65 76 65 6c 2c 20 6f 72 20 61 6c 6c 20 25 5f  Level, or all %_
37ea7 73 65 67 64 69 72 0a 2a 2a 20 20 20 20 20 20 65  segdir.**      e
37ea8 6e 74 72 69 65 73 20 72 65 67 61 72 64 6c 65 73  ntries regardles
37ea9 73 20 6f 66 20 6c 65 76 65 6c 20 69 66 20 28 69  s of level if (i
37eaa 4c 65 76 65 6c 3c 30 29 2e 0a 2a 2a 0a 2a 2a 20  Level<0)..**.** 
37eab 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
37eac 75 72 6e 65 64 20 69 66 20 73 75 63 63 65 73 73  urned if success
37ead 66 75 6c 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ful, otherwise a
37eae 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
37eaf 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ode..*/.static i
37eb0 6e 74 20 66 74 73 33 44 65 6c 65 74 65 53 65 67  nt fts3DeleteSeg
37eb1 64 69 72 28 0a 20 20 46 74 73 33 54 61 62 6c 65  dir(.  Fts3Table
37eb2 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
37eb3 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
37eb4 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
37eb5 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20  /.  int iLevel, 
37eb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37eb7 20 20 20 20 2f 2a 20 4c 65 76 65 6c 20 6f 66 20      /* Level of 
37eb8 25 5f 73 65 67 64 69 72 20 65 6e 74 72 69 65 73  %_segdir entries
37eb9 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
37eba 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a  Fts3SegReader **
37ebb 61 70 53 65 67 6d 65 6e 74 2c 20 20 20 20 20 20  apSegment,      
37ebc 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 65 67 52  /* Array of SegR
37ebd 65 61 64 65 72 20 6f 62 6a 65 63 74 73 20 2a 2f  eader objects */
37ebe 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 20 20  .  int nReader  
37ebf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ec0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72     /* Size of ar
37ec1 72 61 79 20 61 70 53 65 67 6d 65 6e 74 20 2a 2f  ray apSegment */
37ec2 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
37ec3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ec4 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
37ec5 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  Code */.  int i;
37ec6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ec7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
37ec8 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
37ec9 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
37eca 20 2a 70 44 65 6c 65 74 65 3b 20 20 20 20 20 20   *pDelete;      
37ecb 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65      /* SQL state
37ecc 6d 65 6e 74 20 74 6f 20 64 65 6c 65 74 65 20 72  ment to delete r
37ecd 6f 77 73 20 2a 2f 0a 0a 20 20 72 63 20 3d 20 66  ows */..  rc = f
37ece 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51  ts3SqlStmt(p, SQ
37ecf 4c 5f 44 45 4c 45 54 45 5f 53 45 47 4d 45 4e 54  L_DELETE_SEGMENT
37ed0 53 5f 52 41 4e 47 45 2c 20 26 70 44 65 6c 65 74  S_RANGE, &pDelet
37ed1 65 2c 20 30 29 3b 0a 20 20 66 6f 72 28 69 3d 30  e, 0);.  for(i=0
37ed2 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
37ed3 26 26 20 69 3c 6e 52 65 61 64 65 72 3b 20 69 2b  && i<nReader; i+
37ed4 2b 29 7b 0a 20 20 20 20 46 74 73 33 53 65 67 52  +){.    Fts3SegR
37ed5 65 61 64 65 72 20 2a 70 53 65 67 6d 65 6e 74 20  eader *pSegment 
37ed6 3d 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d 3b 0a  = apSegment[i];.
37ed7 20 20 20 20 69 66 28 20 70 53 65 67 6d 65 6e 74      if( pSegment
37ed8 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 20 29 7b  ->iStartBlock ){
37ed9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62  .      sqlite3_b
37eda 69 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c 65 74  ind_int64(pDelet
37edb 65 2c 20 31 2c 20 70 53 65 67 6d 65 6e 74 2d 3e  e, 1, pSegment->
37edc 69 53 74 61 72 74 42 6c 6f 63 6b 29 3b 0a 20 20  iStartBlock);.  
37edd 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
37ede 5f 69 6e 74 36 34 28 70 44 65 6c 65 74 65 2c 20  _int64(pDelete, 
37edf 32 2c 20 70 53 65 67 6d 65 6e 74 2d 3e 69 45 6e  2, pSegment->iEn
37ee0 64 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 73  dBlock);.      s
37ee1 71 6c 69 74 65 33 5f 73 74 65 70 28 70 44 65 6c  qlite3_step(pDel
37ee2 65 74 65 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ete);.      rc =
37ee3 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
37ee4 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 0a 20  Delete);.    }. 
37ee5 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
37ee6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
37ee7 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
37ee8 69 66 28 20 69 4c 65 76 65 6c 3e 3d 30 20 29 7b  if( iLevel>=0 ){
37ee9 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71  .    rc = fts3Sq
37eea 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 44 45 4c  lStmt(p, SQL_DEL
37eeb 45 54 45 5f 53 45 47 44 49 52 5f 42 59 5f 4c 45  ETE_SEGDIR_BY_LE
37eec 56 45 4c 2c 20 26 70 44 65 6c 65 74 65 2c 20 30  VEL, &pDelete, 0
37eed 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
37eee 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
37eef 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
37ef0 6e 74 28 70 44 65 6c 65 74 65 2c 20 31 2c 20 69  nt(pDelete, 1, i
37ef1 4c 65 76 65 6c 29 3b 0a 20 20 20 20 20 20 73 71  Level);.      sq
37ef2 6c 69 74 65 33 5f 73 74 65 70 28 70 44 65 6c 65  lite3_step(pDele
37ef3 74 65 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  te);.      rc = 
37ef4 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 44  sqlite3_reset(pD
37ef5 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 0a 20 20  elete);.    }.  
37ef6 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
37ef7 66 74 73 33 53 71 6c 45 78 65 63 28 70 2c 20 53  fts3SqlExec(p, S
37ef8 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 45  QL_DELETE_ALL_SE
37ef9 47 44 49 52 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  GDIR, 0);.  }.. 
37efa 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
37efb 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66  *.** When this f
37efc 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
37efd 64 2c 20 62 75 66 66 65 72 20 2a 70 70 4c 69 73  d, buffer *ppLis
37efe 74 20 28 73 69 7a 65 20 2a 70 6e 4c 69 73 74 20  t (size *pnList 
37eff 62 79 74 65 73 29 20 63 6f 6e 74 61 69 6e 73 20  bytes) contains 
37f00 0a 2a 2a 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c  .** a position l
37f01 69 73 74 20 74 68 61 74 20 6d 61 79 20 28 6f 72  ist that may (or
37f02 20 6d 61 79 20 6e 6f 74 29 20 66 65 61 74 75 72   may not) featur
37f03 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6c 75 6d  e multiple colum
37f04 6e 73 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63  ns. This.** func
37f05 74 69 6f 6e 20 61 64 6a 75 73 74 73 20 74 68 65  tion adjusts the
37f06 20 70 6f 69 6e 74 65 72 20 2a 70 70 4c 69 73 74   pointer *ppList
37f07 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74 68 20   and the length 
37f08 2a 70 6e 4c 69 73 74 20 73 6f 20 74 68 61 74 20  *pnList so that 
37f09 74 68 65 79 0a 2a 2a 20 69 64 65 6e 74 69 66 79  they.** identify
37f0a 20 74 68 65 20 73 75 62 73 65 74 20 6f 66 20 74   the subset of t
37f0b 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74  he position list
37f0c 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
37f0d 73 20 74 6f 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c  s to column iCol
37f0e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
37f0f 20 61 72 65 20 6e 6f 20 65 6e 74 72 69 65 73 20   are no entries 
37f10 69 6e 20 74 68 65 20 69 6e 70 75 74 20 70 6f 73  in the input pos
37f11 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 63  ition list for c
37f12 6f 6c 75 6d 6e 20 69 43 6f 6c 2c 20 74 68 65 6e  olumn iCol, then
37f13 0a 2a 2a 20 2a 70 6e 4c 69 73 74 20 69 73 20 73  .** *pnList is s
37f14 65 74 20 74 6f 20 7a 65 72 6f 20 62 65 66 6f 72  et to zero befor
37f15 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a  e returning..*/.
37f16 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
37f17 43 6f 6c 75 6d 6e 46 69 6c 74 65 72 28 0a 20 20  ColumnFilter(.  
37f18 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20  int iCol,       
37f19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f1a 2f 2a 20 43 6f 6c 75 6d 6e 20 74 6f 20 66 69 6c  /* Column to fil
37f1b 74 65 72 20 6f 6e 20 2a 2f 0a 20 20 63 68 61 72  ter on */.  char
37f1c 20 2a 2a 70 70 4c 69 73 74 2c 20 20 20 20 20 20   **ppList,      
37f1d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
37f1e 4e 2f 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74  N/OUT: Pointer t
37f1f 6f 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20  o position list 
37f20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4c 69 73 74  */.  int *pnList
37f21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f22 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
37f23 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 2a  Size of buffer *
37f24 70 70 4c 69 73 74 20 69 6e 20 62 79 74 65 73 20  ppList in bytes 
37f25 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 70 4c  */.){.  char *pL
37f26 69 73 74 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20  ist = *ppList;. 
37f27 20 69 6e 74 20 6e 4c 69 73 74 20 3d 20 2a 70 6e   int nList = *pn
37f28 4c 69 73 74 3b 0a 20 20 63 68 61 72 20 2a 70 45  List;.  char *pE
37f29 6e 64 20 3d 20 26 70 4c 69 73 74 5b 6e 4c 69 73  nd = &pList[nLis
37f2a 74 5d 3b 0a 20 20 69 6e 74 20 69 43 75 72 72 65  t];.  int iCurre
37f2b 6e 74 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  nt = 0;.  char *
37f2c 70 20 3d 20 70 4c 69 73 74 3b 0a 0a 20 20 61 73  p = pList;..  as
37f2d 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 29 3b  sert( iCol>=0 );
37f2e 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20  .  while( 1 ){. 
37f2f 20 20 20 63 68 61 72 20 63 20 3d 20 30 3b 0a 20     char c = 0;. 
37f30 20 20 20 77 68 69 6c 65 28 20 70 3c 70 45 6e 64     while( p<pEnd
37f31 20 26 26 20 28 63 20 7c 20 2a 70 29 26 30 78 46   && (c | *p)&0xF
37f32 45 20 29 20 63 20 3d 20 2a 70 2b 2b 20 26 20 30  E ) c = *p++ & 0
37f33 78 38 30 3b 0a 20 20 0a 20 20 20 20 69 66 28 20  x80;.  .    if( 
37f34 69 43 6f 6c 3d 3d 69 43 75 72 72 65 6e 74 20 29  iCol==iCurrent )
37f35 7b 0a 20 20 20 20 20 20 6e 4c 69 73 74 20 3d 20  {.      nList = 
37f36 28 70 20 2d 20 70 4c 69 73 74 29 3b 0a 20 20 20  (p - pList);.   
37f37 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
37f38 0a 20 20 20 20 6e 4c 69 73 74 20 2d 3d 20 28 70  .    nList -= (p
37f39 20 2d 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 70   - pList);.    p
37f3a 4c 69 73 74 20 3d 20 70 3b 0a 20 20 20 20 69 66  List = p;.    if
37f3b 28 20 6e 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( nList==0 ){.  
37f3c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
37f3d 0a 20 20 20 20 70 20 3d 20 26 70 4c 69 73 74 5b  .    p = &pList[
37f3e 31 5d 3b 0a 20 20 20 20 70 20 2b 3d 20 73 71 6c  1];.    p += sql
37f3f 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
37f40 74 33 32 28 70 2c 20 26 69 43 75 72 72 65 6e 74  t32(p, &iCurrent
37f41 29 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4c 69 73  );.  }..  *ppLis
37f42 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 2a 70 6e  t = pList;.  *pn
37f43 4c 69 73 74 20 3d 20 6e 4c 69 73 74 3b 0a 7d 0a  List = nList;.}.
37f44 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 46 74  ./*.** sqlite3Ft
37f45 73 33 53 65 67 52 65 61 64 65 72 49 74 65 72 61  s3SegReaderItera
37f46 74 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 75 73  te() callback us
37f47 65 64 20 77 68 65 6e 20 6d 65 72 67 69 6e 67 20  ed when merging 
37f48 6d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 73 65 67  multiple .** seg
37f49 6d 65 6e 74 73 20 74 6f 20 63 72 65 61 74 65 20  ments to create 
37f4a 61 20 73 69 6e 67 6c 65 2c 20 6c 61 72 67 65 72  a single, larger
37f4b 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61   segment..*/.sta
37f4c 74 69 63 20 69 6e 74 20 66 74 73 33 4d 65 72 67  tic int fts3Merg
37f4d 65 43 61 6c 6c 62 61 63 6b 28 0a 20 20 46 74 73  eCallback(.  Fts
37f4e 33 54 61 62 6c 65 20 2a 70 2c 0a 20 20 76 6f 69  3Table *p,.  voi
37f4f 64 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 63  d *pContext,.  c
37f50 68 61 72 20 2a 7a 54 65 72 6d 2c 0a 20 20 69 6e  har *zTerm,.  in
37f51 74 20 6e 54 65 72 6d 2c 0a 20 20 63 68 61 72 20  t nTerm,.  char 
37f52 2a 61 44 6f 63 6c 69 73 74 2c 0a 20 20 69 6e 74  *aDoclist,.  int
37f53 20 6e 44 6f 63 6c 69 73 74 0a 29 7b 0a 20 20 53   nDoclist.){.  S
37f54 65 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 2a 70  egmentWriter **p
37f55 70 57 20 3d 20 28 53 65 67 6d 65 6e 74 57 72 69  pW = (SegmentWri
37f56 74 65 72 20 2a 2a 29 70 43 6f 6e 74 65 78 74 3b  ter **)pContext;
37f57 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33 53 65  .  return fts3Se
37f58 67 57 72 69 74 65 72 41 64 64 28 70 2c 20 70 70  gWriterAdd(p, pp
37f59 57 2c 20 31 2c 20 7a 54 65 72 6d 2c 20 6e 54 65  W, 1, zTerm, nTe
37f5a 72 6d 2c 20 61 44 6f 63 6c 69 73 74 2c 20 6e 44  rm, aDoclist, nD
37f5b 6f 63 6c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  oclist);.}../*.*
37f5c 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
37f5d 69 73 20 75 73 65 64 20 74 6f 20 69 74 65 72 61  is used to itera
37f5e 74 65 20 74 68 72 6f 75 67 68 20 61 20 63 6f 6e  te through a con
37f5f 74 69 67 75 6f 75 73 20 73 65 74 20 6f 66 20 74  tiguous set of t
37f60 65 72 6d 73 20 0a 2a 2a 20 73 74 6f 72 65 64 20  erms .** stored 
37f61 69 6e 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74  in the full-text
37f62 20 69 6e 64 65 78 2e 20 49 74 20 6d 65 72 67 65   index. It merge
37f63 73 20 64 61 74 61 20 63 6f 6e 74 61 69 6e 65 64  s data contained
37f64 20 69 6e 20 6f 6e 65 20 6f 72 20 0a 2a 2a 20 6d   in one or .** m
37f65 6f 72 65 20 73 65 67 6d 65 6e 74 73 20 74 6f 20  ore segments to 
37f66 73 75 70 70 6f 72 74 20 74 68 69 73 2e 0a 2a 2a  support this..**
37f67 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
37f68 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73 73 65  rgument is passe
37f69 64 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f  d an array of po
37f6a 69 6e 74 65 72 73 20 74 6f 20 53 65 67 52 65 61  inters to SegRea
37f6b 64 65 72 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 61  der objects.** a
37f6c 6c 6c 6f 63 61 74 65 64 20 77 69 74 68 20 73 71  llocated with sq
37f6d 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
37f6e 65 72 4e 65 77 28 29 2e 20 54 68 69 73 20 66 75  erNew(). This fu
37f6f 6e 63 74 69 6f 6e 20 6d 65 72 67 65 73 20 74 68  nction merges th
37f70 65 20 72 61 6e 67 65 20 0a 2a 2a 20 6f 66 20 74  e range .** of t
37f71 65 72 6d 73 20 73 65 6c 65 63 74 65 64 20 62 79  erms selected by
37f72 20 65 61 63 68 20 53 65 67 52 65 61 64 65 72 2e   each SegReader.
37f73 20 49 66 20 61 20 73 69 6e 67 6c 65 20 74 65 72   If a single ter
37f74 6d 20 69 73 20 70 72 65 73 65 6e 74 20 69 6e 0a  m is present in.
37f75 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ** more than one
37f76 20 73 65 67 6d 65 6e 74 2c 20 74 68 65 20 61 73   segment, the as
37f77 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74  sociated doclist
37f78 73 20 61 72 65 20 6d 65 72 67 65 64 2e 20 46 6f  s are merged. Fo
37f79 72 20 65 61 63 68 0a 2a 2a 20 74 65 72 6d 20 61  r each.** term a
37f7a 6e 64 20 28 70 6f 73 73 69 62 6c 79 20 6d 65 72  nd (possibly mer
37f7b 67 65 64 29 20 64 6f 63 6c 69 73 74 20 69 6e 20  ged) doclist in 
37f7c 74 68 65 20 6d 65 72 67 65 64 20 72 61 6e 67 65  the merged range
37f7d 2c 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a  , the callback.*
37f7e 2a 20 66 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63  * function xFunc
37f7f 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 69 74 68   is invoked with
37f80 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 20 73   its arguments s
37f81 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 2e 0a 2a  et as follows..*
37f82 2a 0a 2a 2a 20 20 20 61 72 67 20 30 3a 20 43 6f  *.**   arg 0: Co
37f83 70 79 20 6f 66 20 27 70 27 20 70 61 72 61 6d 65  py of 'p' parame
37f84 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68  ter passed to th
37f85 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20  is function.**  
37f86 20 61 72 67 20 31 3a 20 43 6f 70 79 20 6f 66 20   arg 1: Copy of 
37f87 27 70 43 6f 6e 74 65 78 74 27 20 70 61 72 61 6d  'pContext' param
37f88 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 74  eter passed to t
37f89 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
37f8a 20 20 61 72 67 20 32 3a 20 50 6f 69 6e 74 65 72    arg 2: Pointer
37f8b 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61   to buffer conta
37f8c 69 6e 69 6e 67 20 74 65 72 6d 0a 2a 2a 20 20 20  ining term.**   
37f8d 61 72 67 20 33 3a 20 53 69 7a 65 20 6f 66 20 61  arg 3: Size of a
37f8e 72 67 20 32 20 62 75 66 66 65 72 20 69 6e 20 62  rg 2 buffer in b
37f8f 79 74 65 73 0a 2a 2a 20 20 20 61 72 67 20 34 3a  ytes.**   arg 4:
37f90 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66   Pointer to buff
37f91 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64 6f  er containing do
37f92 63 6c 69 73 74 0a 2a 2a 20 20 20 61 72 67 20 35  clist.**   arg 5
37f93 3a 20 53 69 7a 65 20 6f 66 20 61 72 67 20 32 20  : Size of arg 2 
37f94 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 0a  buffer in bytes.
37f95 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68 20 61 72  **.** The 4th ar
37f96 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
37f97 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 70 6f 69  unction is a poi
37f98 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 74  nter to a struct
37f99 75 72 65 20 6f 66 20 74 79 70 65 0a 2a 2a 20 46  ure of type.** F
37f9a 74 73 33 53 65 67 46 69 6c 74 65 72 2c 20 64 65  ts3SegFilter, de
37f9b 66 69 6e 65 64 20 69 6e 20 66 74 73 33 49 6e 74  fined in fts3Int
37f9c 2e 68 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73  .h. The contents
37f9d 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
37f9e 72 65 0a 2a 2a 20 66 75 72 74 68 65 72 20 72 65  re.** further re
37f9f 73 74 72 69 63 74 20 74 68 65 20 72 61 6e 67 65  strict the range
37fa0 20 6f 66 20 74 65 72 6d 73 20 74 68 61 74 20 63   of terms that c
37fa1 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 6d 61 64  allbacks are mad
37fa2 65 20 66 6f 72 20 61 6e 64 0a 2a 2a 20 6d 6f 64  e for and.** mod
37fa3 69 66 79 20 74 68 65 20 62 65 68 61 76 69 6f 75  ify the behaviou
37fa4 72 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  r of this functi
37fa5 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73  on. See comments
37fa6 20 61 62 6f 76 65 20 73 74 72 75 63 74 75 72 65   above structure
37fa7 0a 2a 2a 20 64 65 66 69 6e 69 74 69 6f 6e 20 66  .** definition f
37fa8 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 53  or details..*/.S
37fa9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
37faa 74 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67  t sqlite3Fts3Seg
37fab 52 65 61 64 65 72 49 74 65 72 61 74 65 28 0a 20  ReaderIterate(. 
37fac 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20   Fts3Table *p,  
37fad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fae 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
37faf 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74  e handle */.  Ft
37fb0 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 61 70  s3SegReader **ap
37fb1 53 65 67 6d 65 6e 74 2c 20 20 20 20 20 20 2f 2a  Segment,      /*
37fb2 20 41 72 72 61 79 20 6f 66 20 46 74 73 33 53 65   Array of Fts3Se
37fb3 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 73 20  gReader objects 
37fb4 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 67 6d 65 6e  */.  int nSegmen
37fb5 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
37fb6 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
37fb7 61 70 53 65 67 6d 65 6e 74 20 61 72 72 61 79 20  apSegment array 
37fb8 2a 2f 0a 20 20 46 74 73 33 53 65 67 46 69 6c 74  */.  Fts3SegFilt
37fb9 65 72 20 2a 70 46 69 6c 74 65 72 2c 20 20 20 20  er *pFilter,    
37fba 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
37fbb 69 6f 6e 73 20 6f 6e 20 72 61 6e 67 65 20 6f 66  ions on range of
37fbc 20 69 74 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   iteration */.  
37fbd 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 46 74 73  int (*xFunc)(Fts
37fbe 33 54 61 62 6c 65 20 2a 2c 20 76 6f 69 64 20 2a  3Table *, void *
37fbf 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63  , char *, int, c
37fc0 68 61 72 20 2a 2c 20 69 6e 74 29 2c 20 20 2f 2a  har *, int),  /*
37fc1 20 43 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76   Callback */.  v
37fc2 6f 69 64 20 2a 70 43 6f 6e 74 65 78 74 20 20 20  oid *pContext   
37fc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37fc4 2a 20 43 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 65  * Callback conte
37fc5 78 74 20 28 32 6e 64 20 61 72 67 75 6d 65 6e 74  xt (2nd argument
37fc6 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  ) */.){.  int i;
37fc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fc8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
37fc9 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
37fca 2f 0a 20 20 63 68 61 72 20 2a 61 42 75 66 66 65  /.  char *aBuffe
37fcb 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
37fcc 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f      /* Buffer to
37fcd 20 6d 65 72 67 65 20 64 6f 63 6c 69 73 74 73 20   merge doclists 
37fce 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c  in */.  int nAll
37fcf 6f 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  oc = 0;         
37fd0 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63          /* Alloc
37fd1 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 42 75  ated size of aBu
37fd2 66 66 65 72 20 62 75 66 66 65 72 20 2a 2f 0a 20  ffer buffer */. 
37fd3 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
37fd4 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
37fd5 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
37fd6 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 49 67 6e 6f  */..  int isIgno
37fd7 72 65 45 6d 70 74 79 20 3d 20 20 28 70 46 69 6c  reEmpty =  (pFil
37fd8 74 65 72 2d 3e 66 6c 61 67 73 20 26 20 46 54 53  ter->flags & FTS
37fd9 33 5f 53 45 47 4d 45 4e 54 5f 49 47 4e 4f 52 45  3_SEGMENT_IGNORE
37fda 5f 45 4d 50 54 59 29 3b 0a 20 20 69 6e 74 20 69  _EMPTY);.  int i
37fdb 73 52 65 71 75 69 72 65 50 6f 73 20 3d 20 20 20  sRequirePos =   
37fdc 28 70 46 69 6c 74 65 72 2d 3e 66 6c 61 67 73 20  (pFilter->flags 
37fdd 26 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 52  & FTS3_SEGMENT_R
37fde 45 51 55 49 52 45 5f 50 4f 53 29 3b 0a 20 20 69  EQUIRE_POS);.  i
37fdf 6e 74 20 69 73 43 6f 6c 46 69 6c 74 65 72 20 3d  nt isColFilter =
37fe0 20 20 20 20 28 70 46 69 6c 74 65 72 2d 3e 66 6c      (pFilter->fl
37fe1 61 67 73 20 26 20 46 54 53 33 5f 53 45 47 4d 45  ags & FTS3_SEGME
37fe2 4e 54 5f 43 4f 4c 55 4d 4e 5f 46 49 4c 54 45 52  NT_COLUMN_FILTER
37fe3 29 3b 0a 20 20 69 6e 74 20 69 73 50 72 65 66 69  );.  int isPrefi
37fe4 78 20 3d 20 20 20 20 20 20 20 28 70 46 69 6c 74  x =       (pFilt
37fe5 65 72 2d 3e 66 6c 61 67 73 20 26 20 46 54 53 33  er->flags & FTS3
37fe6 5f 53 45 47 4d 45 4e 54 5f 50 52 45 46 49 58 29  _SEGMENT_PREFIX)
37fe7 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46  ;..  /* If the F
37fe8 74 73 33 53 65 67 46 69 6c 74 65 72 20 64 65 66  ts3SegFilter def
37fe9 69 6e 65 73 20 61 20 73 70 65 63 69 66 69 63 20  ines a specific 
37fea 74 65 72 6d 20 28 6f 72 20 74 65 72 6d 20 70 72  term (or term pr
37feb 65 66 69 78 29 20 74 6f 20 73 65 61 72 63 68 20  efix) to search 
37fec 0a 20 20 2a 2a 20 66 6f 72 2c 20 74 68 65 6e 20  .  ** for, then 
37fed 61 64 76 61 6e 63 65 20 65 61 63 68 20 73 65 67  advance each seg
37fee 6d 65 6e 74 20 69 74 65 72 61 74 6f 72 20 75 6e  ment iterator un
37fef 74 69 6c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  til it points to
37ff0 20 61 20 74 65 72 6d 20 6f 66 0a 20 20 2a 2a 20   a term of.  ** 
37ff1 65 71 75 61 6c 20 6f 72 20 67 72 65 61 74 65 72  equal or greater
37ff2 20 76 61 6c 75 65 20 74 68 61 6e 20 74 68 65 20   value than the 
37ff3 73 70 65 63 69 66 69 65 64 20 74 65 72 6d 2e 20  specified term. 
37ff4 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 6d 61  This prevents ma
37ff5 6e 79 0a 20 20 2a 2a 20 75 6e 6e 65 63 65 73 73  ny.  ** unnecess
37ff6 61 72 79 20 6d 65 72 67 65 2f 73 6f 72 74 20 6f  ary merge/sort o
37ff7 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 74 68  perations for th
37ff8 65 20 63 61 73 65 20 77 68 65 72 65 20 73 69 6e  e case where sin
37ff9 67 6c 65 20 73 65 67 6d 65 6e 74 0a 20 20 2a 2a  gle segment.  **
37ffa 20 62 2d 74 72 65 65 20 6c 65 61 66 20 6e 6f 64   b-tree leaf nod
37ffb 65 73 20 63 6f 6e 74 61 69 6e 20 6d 6f 72 65 20  es contain more 
37ffc 74 68 61 6e 20 6f 6e 65 20 74 65 72 6d 2e 0a 20  than one term.. 
37ffd 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 74 65   */.  if( pFilte
37ffe 72 2d 3e 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20  r->zTerm ){.    
37fff 69 6e 74 20 6e 54 65 72 6d 20 3d 20 70 46 69 6c  int nTerm = pFil
38000 74 65 72 2d 3e 6e 54 65 72 6d 3b 0a 20 20 20 20  ter->nTerm;.    
38001 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72  const char *zTer
38002 6d 20 3d 20 70 46 69 6c 74 65 72 2d 3e 7a 54 65  m = pFilter->zTe
38003 72 6d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  rm;.    for(i=0;
38004 20 69 3c 6e 53 65 67 6d 65 6e 74 3b 20 69 2b 2b   i<nSegment; i++
38005 29 7b 0a 20 20 20 20 20 20 46 74 73 33 53 65 67  ){.      Fts3Seg
38006 52 65 61 64 65 72 20 2a 70 53 65 67 20 3d 20 61  Reader *pSeg = a
38007 70 53 65 67 6d 65 6e 74 5b 69 5d 3b 0a 20 20 20  pSegment[i];.   
38008 20 20 20 77 68 69 6c 65 28 20 66 74 73 33 53 65     while( fts3Se
38009 67 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 70  gReaderTermCmp(p
3800a 53 65 67 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  Seg, zTerm, nTer
3800b 6d 29 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  m)<0 ){.        
3800c 72 63 20 3d 20 66 74 73 33 53 65 67 52 65 61 64  rc = fts3SegRead
3800d 65 72 4e 65 78 74 28 70 53 65 67 29 3b 0a 20 20  erNext(pSeg);.  
3800e 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
3800f 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66  LITE_OK ) goto f
38010 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 20 20 7d  inished;.      }
38011 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 74  .    }.  }..  ft
38012 73 33 53 65 67 52 65 61 64 65 72 53 6f 72 74 28  s3SegReaderSort(
38013 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65 67 6d  apSegment, nSegm
38014 65 6e 74 2c 20 6e 53 65 67 6d 65 6e 74 2c 20 66  ent, nSegment, f
38015 74 73 33 53 65 67 52 65 61 64 65 72 43 6d 70 29  ts3SegReaderCmp)
38016 3b 0a 20 20 77 68 69 6c 65 28 20 61 70 53 65 67  ;.  while( apSeg
38017 6d 65 6e 74 5b 30 5d 2d 3e 61 4e 6f 64 65 20 29  ment[0]->aNode )
38018 7b 0a 20 20 20 20 69 6e 74 20 6e 54 65 72 6d 20  {.    int nTerm 
38019 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e  = apSegment[0]->
3801a 6e 54 65 72 6d 3b 0a 20 20 20 20 63 68 61 72 20  nTerm;.    char 
3801b 2a 7a 54 65 72 6d 20 3d 20 61 70 53 65 67 6d 65  *zTerm = apSegme
3801c 6e 74 5b 30 5d 2d 3e 7a 54 65 72 6d 3b 0a 20 20  nt[0]->zTerm;.  
3801d 20 20 69 6e 74 20 6e 4d 65 72 67 65 20 3d 20 31    int nMerge = 1
3801e 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ;..    /* If thi
3801f 73 20 69 73 20 61 20 70 72 65 66 69 78 2d 73 65  s is a prefix-se
38020 61 72 63 68 2c 20 61 6e 64 20 69 66 20 74 68 65  arch, and if the
38021 20 74 65 72 6d 20 74 68 61 74 20 61 70 53 65 67   term that apSeg
38022 6d 65 6e 74 5b 30 5d 20 70 6f 69 6e 74 73 0a 20  ment[0] points. 
38023 20 20 20 2a 2a 20 74 6f 20 64 6f 65 73 20 6e 6f     ** to does no
38024 74 20 73 68 61 72 65 20 61 20 73 75 66 66 69 78  t share a suffix
38025 20 77 69 74 68 20 70 46 69 6c 74 65 72 2d 3e 7a   with pFilter->z
38026 54 65 72 6d 2f 6e 54 65 72 6d 2c 20 74 68 65 6e  Term/nTerm, then
38027 20 61 6c 6c 20 0a 20 20 20 20 2a 2a 20 72 65 71   all .    ** req
38028 75 69 72 65 64 20 63 61 6c 6c 62 61 63 6b 73 20  uired callbacks 
38029 68 61 76 65 20 62 65 65 6e 20 6d 61 64 65 2e 20  have been made. 
3802a 49 6e 20 74 68 69 73 20 63 61 73 65 20 65 78 69  In this case exi
3802b 74 20 65 61 72 6c 79 2e 0a 20 20 20 20 2a 2a 0a  t early..    **.
3802c 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c 79      ** Similarly
3802d 2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 73  , if this is a s
3802e 65 61 72 63 68 20 66 6f 72 20 61 6e 20 65 78 61  earch for an exa
3802f 63 74 20 6d 61 74 63 68 2c 20 61 6e 64 20 74 68  ct match, and th
38030 65 20 66 69 72 73 74 20 74 65 72 6d 0a 20 20 20  e first term.   
38031 20 2a 2a 20 6f 66 20 73 65 67 6d 65 6e 74 20 61   ** of segment a
38032 70 53 65 67 6d 65 6e 74 5b 30 5d 20 69 73 20 6e  pSegment[0] is n
38033 6f 74 20 61 20 6d 61 74 63 68 2c 20 65 78 69 74  ot a match, exit
38034 20 65 61 72 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20   early..    */. 
38035 20 20 20 69 66 28 20 70 46 69 6c 74 65 72 2d 3e     if( pFilter->
38036 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 69  zTerm ){.      i
38037 66 28 20 6e 54 65 72 6d 3c 70 46 69 6c 74 65 72  f( nTerm<pFilter
38038 2d 3e 6e 54 65 72 6d 20 0a 20 20 20 20 20 20 20  ->nTerm .       
38039 7c 7c 20 28 21 69 73 50 72 65 66 69 78 20 26 26  || (!isPrefix &&
3803a 20 6e 54 65 72 6d 3e 70 46 69 6c 74 65 72 2d 3e   nTerm>pFilter->
3803b 6e 54 65 72 6d 29 0a 20 20 20 20 20 20 20 7c 7c  nTerm).       ||
3803c 20 6d 65 6d 63 6d 70 28 7a 54 65 72 6d 2c 20 70   memcmp(zTerm, p
3803d 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 2c 20 70  Filter->zTerm, p
3803e 46 69 6c 74 65 72 2d 3e 6e 54 65 72 6d 29 20 0a  Filter->nTerm) .
3803f 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 67      ){.        g
38040 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20  oto finished;.  
38041 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
38042 20 77 68 69 6c 65 28 20 6e 4d 65 72 67 65 3c 6e   while( nMerge<n
38043 53 65 67 6d 65 6e 74 20 0a 20 20 20 20 20 20 20  Segment .       
38044 20 26 26 20 61 70 53 65 67 6d 65 6e 74 5b 6e 4d   && apSegment[nM
38045 65 72 67 65 5d 2d 3e 61 4e 6f 64 65 0a 20 20 20  erge]->aNode.   
38046 20 20 20 20 20 26 26 20 61 70 53 65 67 6d 65 6e       && apSegmen
38047 74 5b 6e 4d 65 72 67 65 5d 2d 3e 6e 54 65 72 6d  t[nMerge]->nTerm
38048 3d 3d 6e 54 65 72 6d 20 0a 20 20 20 20 20 20 20  ==nTerm .       
38049 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 54   && 0==memcmp(zT
3804a 65 72 6d 2c 20 61 70 53 65 67 6d 65 6e 74 5b 6e  erm, apSegment[n
3804b 4d 65 72 67 65 5d 2d 3e 7a 54 65 72 6d 2c 20 6e  Merge]->zTerm, n
3804c 54 65 72 6d 29 0a 20 20 20 20 29 7b 0a 20 20 20  Term).    ){.   
3804d 20 20 20 6e 4d 65 72 67 65 2b 2b 3b 0a 20 20 20     nMerge++;.   
3804e 20 7d 0a 0a 20 20 20 20 69 66 28 20 6e 4d 65 72   }..    if( nMer
3804f 67 65 3d 3d 31 20 26 26 20 21 69 73 49 67 6e 6f  ge==1 && !isIgno
38050 72 65 45 6d 70 74 79 20 26 26 20 21 69 73 43 6f  reEmpty && !isCo
38051 6c 46 69 6c 74 65 72 20 26 26 20 69 73 52 65 71  lFilter && isReq
38052 75 69 72 65 50 6f 73 20 29 7b 0a 20 20 20 20 20  uirePos ){.     
38053 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
38054 70 30 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30  p0 = apSegment[0
38055 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 78 46  ];.      rc = xF
38056 75 6e 63 28 70 2c 20 70 43 6f 6e 74 65 78 74 2c  unc(p, pContext,
38057 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 70   zTerm, nTerm, p
38058 30 2d 3e 61 44 6f 63 6c 69 73 74 2c 20 70 30 2d  0->aDoclist, p0-
38059 3e 6e 44 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20  >nDoclist);.    
3805a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3805b 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69 73  _OK ) goto finis
3805c 68 65 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  hed;.    }else{.
3805d 20 20 20 20 20 20 69 6e 74 20 6e 44 6f 63 6c 69        int nDocli
3805e 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  st = 0;         
3805f 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f 63    /* Size of doc
38060 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 73 71  list */.      sq
38061 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72 65  lite3_int64 iPre
38062 76 20 3d 20 30 3b 20 20 20 20 2f 2a 20 50 72 65  v = 0;    /* Pre
38063 76 69 6f 75 73 20 64 6f 63 69 64 20 73 74 6f 72  vious docid stor
38064 65 64 20 69 6e 20 64 6f 63 6c 69 73 74 20 2a 2f  ed in doclist */
38065 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  ..      /* The c
38066 75 72 72 65 6e 74 20 74 65 72 6d 20 6f 66 20 74  urrent term of t
38067 68 65 20 66 69 72 73 74 20 6e 4d 65 72 67 65 20  he first nMerge 
38068 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 61  entries in the a
38069 72 72 61 79 0a 20 20 20 20 20 20 2a 2a 20 6f 66  rray.      ** of
3806a 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 6f   Fts3SegReader o
3806b 62 6a 65 63 74 73 20 69 73 20 74 68 65 20 73 61  bjects is the sa
3806c 6d 65 2e 20 54 68 65 20 64 6f 63 6c 69 73 74 73  me. The doclists
3806d 20 6d 75 73 74 20 62 65 20 6d 65 72 67 65 64 0a   must be merged.
3806e 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 20 73        ** and a s
3806f 69 6e 67 6c 65 20 74 65 72 6d 20 61 64 64 65 64  ingle term added
38070 20 74 6f 20 74 68 65 20 6e 65 77 20 73 65 67 6d   to the new segm
38071 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ent..      */.  
38072 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
38073 4d 65 72 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  Merge; i++){.   
38074 20 20 20 20 20 66 74 73 33 53 65 67 52 65 61 64       fts3SegRead
38075 65 72 46 69 72 73 74 44 6f 63 69 64 28 61 70 53  erFirstDocid(apS
38076 65 67 6d 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20  egment[i]);.    
38077 20 20 7d 0a 20 20 20 20 20 20 66 74 73 33 53 65    }.      fts3Se
38078 67 52 65 61 64 65 72 53 6f 72 74 28 61 70 53 65  gReaderSort(apSe
38079 67 6d 65 6e 74 2c 20 6e 4d 65 72 67 65 2c 20 6e  gment, nMerge, n
3807a 4d 65 72 67 65 2c 20 66 74 73 33 53 65 67 52 65  Merge, fts3SegRe
3807b 61 64 65 72 44 6f 63 6c 69 73 74 43 6d 70 29 3b  aderDoclistCmp);
3807c 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 61 70  .      while( ap
3807d 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e 70 4f 66 66  Segment[0]->pOff
3807e 73 65 74 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  setList ){.     
3807f 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20     int j;       
38080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38081 4e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e  Number of segmen
38082 74 73 20 74 68 61 74 20 73 68 61 72 65 20 61 20  ts that share a 
38083 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 20 20 20  docid */.       
38084 20 63 68 61 72 20 2a 70 4c 69 73 74 3b 0a 20 20   char *pList;.  
38085 20 20 20 20 20 20 69 6e 74 20 6e 4c 69 73 74 3b        int nList;
38086 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 42 79  .        int nBy
38087 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  te;.        sqli
38088 74 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  te3_int64 iDocid
38089 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2d   = apSegment[0]-
3808a 3e 69 44 6f 63 69 64 3b 0a 20 20 20 20 20 20 20  >iDocid;.       
3808b 20 66 74 73 33 53 65 67 52 65 61 64 65 72 4e 65   fts3SegReaderNe
3808c 78 74 44 6f 63 69 64 28 61 70 53 65 67 6d 65 6e  xtDocid(apSegmen
3808d 74 5b 30 5d 2c 20 26 70 4c 69 73 74 2c 20 26 6e  t[0], &pList, &n
3808e 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 6a  List);.        j
3808f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 77 68   = 1;.        wh
38090 69 6c 65 28 20 6a 3c 6e 4d 65 72 67 65 20 0a 20  ile( j<nMerge . 
38091 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61 70             && ap
38092 53 65 67 6d 65 6e 74 5b 6a 5d 2d 3e 70 4f 66 66  Segment[j]->pOff
38093 73 65 74 4c 69 73 74 20 0a 20 20 20 20 20 20 20  setList .       
38094 20 20 20 20 20 26 26 20 61 70 53 65 67 6d 65 6e       && apSegmen
38095 74 5b 6a 5d 2d 3e 69 44 6f 63 69 64 3d 3d 69 44  t[j]->iDocid==iD
38096 6f 63 69 64 20 0a 20 20 20 20 20 20 20 20 29 7b  ocid .        ){
38097 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 53  .          fts3S
38098 65 67 52 65 61 64 65 72 4e 65 78 74 44 6f 63 69  egReaderNextDoci
38099 64 28 61 70 53 65 67 6d 65 6e 74 5b 6a 5d 2c 20  d(apSegment[j], 
3809a 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  0, 0);.         
3809b 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a   j++;.        }.
3809c 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 43  .        if( isC
3809d 6f 6c 46 69 6c 74 65 72 20 29 7b 0a 20 20 20 20  olFilter ){.    
3809e 20 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e        fts3Column
3809f 46 69 6c 74 65 72 28 70 46 69 6c 74 65 72 2d 3e  Filter(pFilter->
380a0 69 43 6f 6c 2c 20 26 70 4c 69 73 74 2c 20 26 6e  iCol, &pList, &n
380a1 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  List);.        }
380a2 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69  ..        if( !i
380a3 73 49 67 6e 6f 72 65 45 6d 70 74 79 20 7c 7c 20  sIgnoreEmpty || 
380a4 6e 4c 69 73 74 3e 30 20 29 7b 0a 20 20 20 20 20  nList>0 ){.     
380a5 20 20 20 20 20 6e 42 79 74 65 20 3d 20 73 71 6c       nByte = sql
380a6 69 74 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65  ite3Fts3VarintLe
380a7 6e 28 69 44 6f 63 69 64 2d 69 50 72 65 76 29 20  n(iDocid-iPrev) 
380a8 2b 20 28 69 73 52 65 71 75 69 72 65 50 6f 73 3f  + (isRequirePos?
380a9 6e 4c 69 73 74 2b 31 3a 30 29 3b 0a 20 20 20 20  nList+1:0);.    
380aa 20 20 20 20 20 20 69 66 28 20 6e 44 6f 63 6c 69        if( nDocli
380ab 73 74 2b 6e 42 79 74 65 3e 6e 41 6c 6c 6f 63 20  st+nByte>nAlloc 
380ac 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
380ad 68 61 72 20 2a 61 4e 65 77 3b 0a 20 20 20 20 20  har *aNew;.     
380ae 20 20 20 20 20 20 20 6e 41 6c 6c 6f 63 20 3d 20         nAlloc = 
380af 6e 44 6f 63 6c 69 73 74 2b 6e 42 79 74 65 2a 32  nDoclist+nByte*2
380b0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 4e  ;.            aN
380b1 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
380b2 6c 6c 6f 63 28 61 42 75 66 66 65 72 2c 20 6e 41  lloc(aBuffer, nA
380b3 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 20  lloc);.         
380b4 20 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b 0a     if( !aNew ){.
380b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63                rc
380b6 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
380b7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67  .              g
380b8 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20  oto finished;.  
380b9 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
380ba 20 20 20 20 20 20 20 20 61 42 75 66 66 65 72 20          aBuffer 
380bb 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  = aNew;.        
380bc 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6e 44    }.          nD
380bd 6f 63 6c 69 73 74 20 2b 3d 20 73 71 6c 69 74 65  oclist += sqlite
380be 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26  3Fts3PutVarint(&
380bf 61 42 75 66 66 65 72 5b 6e 44 6f 63 6c 69 73 74  aBuffer[nDoclist
380c0 5d 2c 20 69 44 6f 63 69 64 2d 69 50 72 65 76 29  ], iDocid-iPrev)
380c1 3b 0a 20 20 20 20 20 20 20 20 20 20 69 50 72 65  ;.          iPre
380c2 76 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20 20 20  v = iDocid;.    
380c3 20 20 20 20 20 20 69 66 28 20 69 73 52 65 71 75        if( isRequ
380c4 69 72 65 50 6f 73 20 29 7b 0a 20 20 20 20 20 20  irePos ){.      
380c5 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 42        memcpy(&aB
380c6 75 66 66 65 72 5b 6e 44 6f 63 6c 69 73 74 5d 2c  uffer[nDoclist],
380c7 20 70 4c 69 73 74 2c 20 6e 4c 69 73 74 29 3b 0a   pList, nList);.
380c8 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 6f 63              nDoc
380c9 6c 69 73 74 20 2b 3d 20 6e 4c 69 73 74 3b 0a 20  list += nList;. 
380ca 20 20 20 20 20 20 20 20 20 20 20 61 42 75 66 66             aBuff
380cb 65 72 5b 6e 44 6f 63 6c 69 73 74 2b 2b 5d 20 3d  er[nDoclist++] =
380cc 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20   '\0';.         
380cd 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20   }.        }..  
380ce 20 20 20 20 20 20 66 74 73 33 53 65 67 52 65 61        fts3SegRea
380cf 64 65 72 53 6f 72 74 28 61 70 53 65 67 6d 65 6e  derSort(apSegmen
380d0 74 2c 20 6e 4d 65 72 67 65 2c 20 6a 2c 20 66 74  t, nMerge, j, ft
380d1 73 33 53 65 67 52 65 61 64 65 72 44 6f 63 6c 69  s3SegReaderDocli
380d2 73 74 43 6d 70 29 3b 0a 20 20 20 20 20 20 7d 0a  stCmp);.      }.
380d3 0a 20 20 20 20 20 20 69 66 28 20 6e 44 6f 63 6c  .      if( nDocl
380d4 69 73 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ist>0 ){.       
380d5 20 72 63 20 3d 20 78 46 75 6e 63 28 70 2c 20 70   rc = xFunc(p, p
380d6 43 6f 6e 74 65 78 74 2c 20 7a 54 65 72 6d 2c 20  Context, zTerm, 
380d7 6e 54 65 72 6d 2c 20 61 42 75 66 66 65 72 2c 20  nTerm, aBuffer, 
380d8 6e 44 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20  nDoclist);.     
380d9 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
380da 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69  E_OK ) goto fini
380db 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20  shed;.      }.  
380dc 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
380dd 68 65 72 65 20 69 73 20 61 20 74 65 72 6d 20 73  here is a term s
380de 70 65 63 69 66 69 65 64 20 74 6f 20 66 69 6c 74  pecified to filt
380df 65 72 20 6f 6e 2c 20 61 6e 64 20 74 68 69 73 20  er on, and this 
380e0 69 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78 0a  is not a prefix.
380e1 20 20 20 20 2a 2a 20 73 65 61 72 63 68 2c 20 72      ** search, r
380e2 65 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 63  eturn now. The c
380e3 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 63 6f 72  allback that cor
380e4 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20  responds to the 
380e5 72 65 71 75 69 72 65 64 0a 20 20 20 20 2a 2a 20  required.    ** 
380e6 74 65 72 6d 20 28 69 66 20 73 75 63 68 20 61 20  term (if such a 
380e7 74 65 72 6d 20 65 78 69 73 74 73 20 69 6e 20 74  term exists in t
380e8 68 65 20 69 6e 64 65 78 29 20 68 61 73 20 61 6c  he index) has al
380e9 72 65 61 64 79 20 62 65 65 6e 20 6d 61 64 65 2e  ready been made.
380ea 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
380eb 70 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 20 26  pFilter->zTerm &
380ec 26 20 21 69 73 50 72 65 66 69 78 20 29 7b 0a 20  & !isPrefix ){. 
380ed 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68       goto finish
380ee 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66  ed;.    }..    f
380ef 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 65 72 67 65  or(i=0; i<nMerge
380f0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63  ; i++){.      rc
380f1 20 3d 20 66 74 73 33 53 65 67 52 65 61 64 65 72   = fts3SegReader
380f2 4e 65 78 74 28 61 70 53 65 67 6d 65 6e 74 5b 69  Next(apSegment[i
380f3 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  ]);.      if( rc
380f4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
380f5 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  to finished;.   
380f6 20 7d 0a 20 20 20 20 66 74 73 33 53 65 67 52 65   }.    fts3SegRe
380f7 61 64 65 72 53 6f 72 74 28 61 70 53 65 67 6d 65  aderSort(apSegme
380f8 6e 74 2c 20 6e 53 65 67 6d 65 6e 74 2c 20 6e 4d  nt, nSegment, nM
380f9 65 72 67 65 2c 20 66 74 73 33 53 65 67 52 65 61  erge, fts3SegRea
380fa 64 65 72 43 6d 70 29 3b 0a 20 20 7d 0a 0a 20 66  derCmp);.  }.. f
380fb 69 6e 69 73 68 65 64 3a 0a 20 20 73 71 6c 69 74  inished:.  sqlit
380fc 65 33 5f 66 72 65 65 28 61 42 75 66 66 65 72 29  e3_free(aBuffer)
380fd 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
380fe 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 61 6c  ../*.** Merge al
380ff 6c 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20 73  l level iLevel s
38100 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 64  egments in the d
38101 61 74 61 62 61 73 65 20 69 6e 74 6f 20 61 20 73  atabase into a s
38102 69 6e 67 6c 65 20 0a 2a 2a 20 69 4c 65 76 65 6c  ingle .** iLevel
38103 2b 31 20 73 65 67 6d 65 6e 74 2e 20 4f 72 2c 20  +1 segment. Or, 
38104 69 66 20 69 4c 65 76 65 6c 3c 30 2c 20 6d 65 72  if iLevel<0, mer
38105 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20  ge all segments 
38106 69 6e 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  into a.** single
38107 20 73 65 67 6d 65 6e 74 20 77 69 74 68 20 61 20   segment with a 
38108 6c 65 76 65 6c 20 65 71 75 61 6c 20 74 6f 20 74  level equal to t
38109 68 65 20 6e 75 6d 65 72 69 63 61 6c 6c 79 20 6c  he numerically l
3810a 61 72 67 65 73 74 20 6c 65 76 65 6c 20 0a 2a 2a  argest level .**
3810b 20 63 75 72 72 65 6e 74 6c 79 20 70 72 65 73 65   currently prese
3810c 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
3810d 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69  se..**.** If thi
3810e 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
3810f 6c 6c 65 64 20 77 69 74 68 20 69 4c 65 76 65 6c  lled with iLevel
38110 3c 30 2c 20 62 75 74 20 74 68 65 72 65 20 69 73  <0, but there is
38111 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a 20 73 65 67   only one.** seg
38112 6d 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61  ment in the data
38113 62 61 73 65 2c 20 53 51 4c 49 54 45 5f 44 4f 4e  base, SQLITE_DON
38114 45 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d  E is returned im
38115 6d 65 64 69 61 74 65 6c 79 2e 20 0a 2a 2a 20 4f  mediately. .** O
38116 74 68 65 72 77 69 73 65 2c 20 69 66 20 73 75 63  therwise, if suc
38117 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f  cessful, SQLITE_
38118 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OK is returned. 
38119 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
3811a 72 73 2c 20 0a 2a 2a 20 61 6e 20 53 51 4c 69 74  rs, .** an SQLit
3811b 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
3811c 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
3811d 74 69 63 20 69 6e 74 20 66 74 73 33 53 65 67 6d  tic int fts3Segm
3811e 65 6e 74 4d 65 72 67 65 28 46 74 73 33 54 61 62  entMerge(Fts3Tab
3811f 6c 65 20 2a 70 2c 20 69 6e 74 20 69 4c 65 76 65  le *p, int iLeve
38120 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  l){.  int i;    
38121 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38122 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
38123 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
38124 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
38125 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38126 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
38127 2f 0a 20 20 69 6e 74 20 69 49 64 78 3b 20 20 20  /.  int iIdx;   
38128 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38129 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
3812a 6e 65 77 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 20  new segment */. 
3812b 20 69 6e 74 20 69 4e 65 77 4c 65 76 65 6c 3b 20   int iNewLevel; 
3812c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3812d 20 2f 2a 20 4c 65 76 65 6c 20 74 6f 20 63 72 65   /* Level to cre
3812e 61 74 65 20 6e 65 77 20 73 65 67 6d 65 6e 74 20  ate new segment 
3812f 61 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  at */.  sqlite3_
38130 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 53  stmt *pStmt;.  S
38131 65 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 70 57  egmentWriter *pW
38132 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74  riter = 0;.  int
38133 20 6e 53 65 67 6d 65 6e 74 20 3d 20 30 3b 20 20   nSegment = 0;  
38134 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38135 4e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e  Number of segmen
38136 74 73 20 62 65 69 6e 67 20 6d 65 72 67 65 64 20  ts being merged 
38137 2a 2f 0a 20 20 46 74 73 33 53 65 67 52 65 61 64  */.  Fts3SegRead
38138 65 72 20 2a 2a 61 70 53 65 67 6d 65 6e 74 20 3d  er **apSegment =
38139 20 30 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66   0;  /* Array of
3813a 20 53 65 67 6d 65 6e 74 20 69 74 65 72 61 74 6f   Segment iterato
3813b 72 73 20 2a 2f 0a 20 20 46 74 73 33 53 65 67 46  rs */.  Fts3SegF
3813c 69 6c 74 65 72 20 66 69 6c 74 65 72 3b 20 20 20  ilter filter;   
3813d 20 20 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65          /* Segme
3813e 6e 74 20 74 65 72 6d 20 66 69 6c 74 65 72 20 63  nt term filter c
3813f 6f 6e 64 69 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69  ondition */..  i
38140 66 28 20 69 4c 65 76 65 6c 3c 30 20 29 7b 0a 20  f( iLevel<0 ){. 
38141 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20     /* This call 
38142 69 73 20 74 6f 20 6d 65 72 67 65 20 61 6c 6c 20  is to merge all 
38143 73 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  segments in the 
38144 64 61 74 61 62 61 73 65 20 74 6f 20 61 20 73 69  database to a si
38145 6e 67 6c 65 0a 20 20 20 20 2a 2a 20 73 65 67 6d  ngle.    ** segm
38146 65 6e 74 2e 20 54 68 65 20 6c 65 76 65 6c 20 6f  ent. The level o
38147 66 20 74 68 65 20 6e 65 77 20 73 65 67 6d 65 6e  f the new segmen
38148 74 20 69 73 20 65 71 75 61 6c 20 74 6f 20 74 68  t is equal to th
38149 65 20 74 68 65 20 6e 75 6d 65 72 69 63 61 6c 6c  e the numericall
3814a 79 20 0a 20 20 20 20 2a 2a 20 67 72 65 61 74 65  y .    ** greate
3814b 73 74 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c  st segment level
3814c 20 63 75 72 72 65 6e 74 6c 79 20 70 72 65 73 65   currently prese
3814d 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
3814e 73 65 2e 20 54 68 65 20 69 6e 64 65 78 0a 20 20  se. The index.  
3814f 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20    ** of the new 
38150 73 65 67 6d 65 6e 74 20 69 73 20 61 6c 77 61 79  segment is alway
38151 73 20 30 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  s 0..    */.    
38152 69 49 64 78 20 3d 20 30 3b 0a 20 20 20 20 72 63  iIdx = 0;.    rc
38153 20 3d 20 66 74 73 33 53 65 67 6d 65 6e 74 43 6f   = fts3SegmentCo
38154 75 6e 74 4d 61 78 28 70 2c 20 26 6e 53 65 67 6d  untMax(p, &nSegm
38155 65 6e 74 2c 20 26 69 4e 65 77 4c 65 76 65 6c 29  ent, &iNewLevel)
38156 3b 0a 20 20 20 20 69 66 28 20 6e 53 65 67 6d 65  ;.    if( nSegme
38157 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 72  nt==1 ){.      r
38158 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
38159 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  E;.    }.  }else
3815a 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61  {.    /* This ca
3815b 6c 6c 20 69 73 20 74 6f 20 6d 65 72 67 65 20 61  ll is to merge a
3815c 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 6c  ll segments at l
3815d 65 76 65 6c 20 69 4c 65 76 65 6c 2e 20 46 69 6e  evel iLevel. Fin
3815e 64 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20 2a  d the next.    *
3815f 2a 20 61 76 61 69 6c 61 62 6c 65 20 73 65 67 6d  * available segm
38160 65 6e 74 20 69 6e 64 65 78 20 61 74 20 6c 65 76  ent index at lev
38161 65 6c 20 69 4c 65 76 65 6c 2b 31 2e 20 54 68 65  el iLevel+1. The
38162 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20   call to.    ** 
38163 66 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67 64  fts3AllocateSegd
38164 69 72 49 64 78 28 29 20 77 69 6c 6c 20 6d 65 72  irIdx() will mer
38165 67 65 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20  ge the segments 
38166 61 74 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 2b  at level iLevel+
38167 31 20 74 6f 20 0a 20 20 20 20 2a 2a 20 61 20 73  1 to .    ** a s
38168 69 6e 67 6c 65 20 69 4c 65 76 65 6c 2b 32 20 73  ingle iLevel+2 s
38169 65 67 6d 65 6e 74 20 69 66 20 6e 65 63 65 73 73  egment if necess
3816a 61 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ary..    */.    
3816b 69 4e 65 77 4c 65 76 65 6c 20 3d 20 69 4c 65 76  iNewLevel = iLev
3816c 65 6c 2b 31 3b 0a 20 20 20 20 72 63 20 3d 20 66  el+1;.    rc = f
3816d 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67 64 69  ts3AllocateSegdi
3816e 72 49 64 78 28 70 2c 20 69 4e 65 77 4c 65 76 65  rIdx(p, iNewLeve
3816f 6c 2c 20 26 69 49 64 78 29 3b 0a 20 20 20 20 69  l, &iIdx);.    i
38170 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
38171 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
38172 20 20 72 63 20 3d 20 66 74 73 33 53 65 67 6d 65    rc = fts3Segme
38173 6e 74 43 6f 75 6e 74 28 70 2c 20 69 4c 65 76 65  ntCount(p, iLeve
38174 6c 2c 20 26 6e 53 65 67 6d 65 6e 74 29 3b 0a 20  l, &nSegment);. 
38175 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
38176 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
38177 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 53  rc;.  assert( nS
38178 65 67 6d 65 6e 74 3e 30 20 29 3b 0a 20 20 61 73  egment>0 );.  as
38179 73 65 72 74 28 20 69 4e 65 77 4c 65 76 65 6c 3e  sert( iNewLevel>
3817a 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  =0 );..  /* Allo
3817b 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 61  cate space for a
3817c 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74  n array of point
3817d 65 72 73 20 74 6f 20 73 65 67 6d 65 6e 74 20 69  ers to segment i
3817e 74 65 72 61 74 6f 72 73 2e 20 2a 2f 0a 20 20 61  terators. */.  a
3817f 70 53 65 67 6d 65 6e 74 20 3d 20 28 46 74 73 33  pSegment = (Fts3
38180 53 65 67 52 65 61 64 65 72 2a 2a 29 73 71 6c 69  SegReader**)sqli
38181 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
38182 66 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20  f(Fts3SegReader 
38183 2a 29 2a 6e 53 65 67 6d 65 6e 74 29 3b 0a 20 20  *)*nSegment);.  
38184 69 66 28 20 21 61 70 53 65 67 6d 65 6e 74 20 29  if( !apSegment )
38185 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
38186 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
38187 20 6d 65 6d 73 65 74 28 61 70 53 65 67 6d 65 6e   memset(apSegmen
38188 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73  t, 0, sizeof(Fts
38189 33 53 65 67 52 65 61 64 65 72 20 2a 29 2a 6e 53  3SegReader *)*nS
3818a 65 67 6d 65 6e 74 29 3b 0a 0a 20 20 2f 2a 20 41  egment);..  /* A
3818b 6c 6c 6f 63 61 74 65 20 61 20 46 74 73 33 53 65  llocate a Fts3Se
3818c 67 52 65 61 64 65 72 20 73 74 72 75 63 74 75 72  gReader structur
3818d 65 20 66 6f 72 20 65 61 63 68 20 73 65 67 6d 65  e for each segme
3818e 6e 74 20 62 65 69 6e 67 20 6d 65 72 67 65 64 2e  nt being merged.
3818f 20 41 20 0a 20 20 2a 2a 20 46 74 73 33 53 65 67   A .  ** Fts3Seg
38190 52 65 61 64 65 72 20 73 74 6f 72 65 73 20 74 68  Reader stores th
38191 65 20 73 74 61 74 65 20 64 61 74 61 20 72 65 71  e state data req
38192 75 69 72 65 64 20 74 6f 20 69 74 65 72 61 74 65  uired to iterate
38193 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 0a 20 20   through all .  
38194 2a 2a 20 65 6e 74 72 69 65 73 20 6f 6e 20 61 6c  ** entries on al
38195 6c 20 6c 65 61 76 65 73 20 6f 66 20 61 20 73 69  l leaves of a si
38196 6e 67 6c 65 20 73 65 67 6d 65 6e 74 2e 20 0a 20  ngle segment. . 
38197 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 53 51   */.  assert( SQ
38198 4c 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 2b 31  L_SELECT_LEVEL+1
38199 3d 3d 53 51 4c 5f 53 45 4c 45 43 54 5f 41 4c 4c  ==SQL_SELECT_ALL
3819a 5f 4c 45 56 45 4c 29 3b 0a 20 20 72 63 20 3d 20  _LEVEL);.  rc = 
3819b 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53  fts3SqlStmt(p, S
3819c 51 4c 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 2b  QL_SELECT_LEVEL+
3819d 28 69 4c 65 76 65 6c 3c 30 29 2c 20 26 70 53 74  (iLevel<0), &pSt
3819e 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
3819f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
381a0 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 73  to finished;.  s
381a1 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
381a2 70 53 74 6d 74 2c 20 31 2c 20 69 4c 65 76 65 6c  pStmt, 1, iLevel
381a3 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 53 51  );.  for(i=0; SQ
381a4 4c 49 54 45 5f 52 4f 57 3d 3d 28 73 71 6c 69 74  LITE_ROW==(sqlit
381a5 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 29 3b  e3_step(pStmt));
381a6 20 69 2b 2b 29 7b 0a 20 20 20 20 72 63 20 3d 20   i++){.    rc = 
381a7 66 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 77  fts3SegReaderNew
381a8 28 70 2c 20 70 53 74 6d 74 2c 20 69 2c 20 26 61  (p, pStmt, i, &a
381a9 70 53 65 67 6d 65 6e 74 5b 69 5d 29 3b 0a 20 20  pSegment[i]);.  
381aa 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
381ab 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  _OK ){.      got
381ac 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20  o finished;.    
381ad 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  }.  }.  rc = sql
381ae 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74  ite3_reset(pStmt
381af 29 3b 0a 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a  );.  pStmt = 0;.
381b0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
381b1 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69 73  _OK ) goto finis
381b2 68 65 64 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26  hed;..  memset(&
381b3 66 69 6c 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f  filter, 0, sizeo
381b4 66 28 46 74 73 33 53 65 67 46 69 6c 74 65 72 29  f(Fts3SegFilter)
381b5 29 3b 0a 20 20 66 69 6c 74 65 72 2e 66 6c 61 67  );.  filter.flag
381b6 73 20 3d 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  s = FTS3_SEGMENT
381b7 5f 52 45 51 55 49 52 45 5f 50 4f 53 3b 0a 20 20  _REQUIRE_POS;.  
381b8 66 69 6c 74 65 72 2e 66 6c 61 67 73 20 7c 3d 20  filter.flags |= 
381b9 28 69 4c 65 76 65 6c 3c 30 20 3f 20 46 54 53 33  (iLevel<0 ? FTS3
381ba 5f 53 45 47 4d 45 4e 54 5f 49 47 4e 4f 52 45 5f  _SEGMENT_IGNORE_
381bb 45 4d 50 54 59 20 3a 20 30 29 3b 0a 20 20 72 63  EMPTY : 0);.  rc
381bc 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53 65   = sqlite3Fts3Se
381bd 67 52 65 61 64 65 72 49 74 65 72 61 74 65 28 70  gReaderIterate(p
381be 2c 20 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65  , apSegment, nSe
381bf 67 6d 65 6e 74 2c 0a 20 20 20 20 20 20 26 66 69  gment,.      &fi
381c0 6c 74 65 72 2c 20 66 74 73 33 4d 65 72 67 65 43  lter, fts3MergeC
381c1 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64 20 2a  allback, (void *
381c2 29 26 70 57 72 69 74 65 72 0a 20 20 29 3b 0a 20  )&pWriter.  );. 
381c3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
381c4 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69 73 68  OK ) goto finish
381c5 65 64 3b 0a 0a 20 20 72 63 20 3d 20 66 74 73 33  ed;..  rc = fts3
381c6 44 65 6c 65 74 65 53 65 67 64 69 72 28 70 2c 20  DeleteSegdir(p, 
381c7 69 4c 65 76 65 6c 2c 20 61 70 53 65 67 6d 65 6e  iLevel, apSegmen
381c8 74 2c 20 6e 53 65 67 6d 65 6e 74 29 3b 0a 20 20  t, nSegment);.  
381c9 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
381ca 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74  K ){.    rc = ft
381cb 73 33 53 65 67 57 72 69 74 65 72 46 6c 75 73 68  s3SegWriterFlush
381cc 28 70 2c 20 70 57 72 69 74 65 72 2c 20 69 4e 65  (p, pWriter, iNe
381cd 77 4c 65 76 65 6c 2c 20 69 49 64 78 29 3b 0a 20  wLevel, iIdx);. 
381ce 20 7d 0a 0a 20 66 69 6e 69 73 68 65 64 3a 0a 20   }.. finished:. 
381cf 20 66 74 73 33 53 65 67 57 72 69 74 65 72 46 72   fts3SegWriterFr
381d0 65 65 28 70 57 72 69 74 65 72 29 3b 0a 20 20 69  ee(pWriter);.  i
381d1 66 28 20 61 70 53 65 67 6d 65 6e 74 20 29 7b 0a  f( apSegment ){.
381d2 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
381d3 53 65 67 6d 65 6e 74 3b 20 69 2b 2b 29 7b 0a 20  Segment; i++){. 
381d4 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33       sqlite3Fts3
381d5 53 65 67 52 65 61 64 65 72 46 72 65 65 28 70 2c  SegReaderFree(p,
381d6 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d 29 3b 0a   apSegment[i]);.
381d7 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
381d8 33 5f 66 72 65 65 28 61 70 53 65 67 6d 65 6e 74  3_free(apSegment
381d9 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
381da 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20  _reset(pStmt);. 
381db 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
381dc 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 63  *.** This is a c
381dd 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
381de 6f 6e 20 75 73 65 64 20 61 73 20 61 20 71 73 6f  on used as a qso
381df 72 74 28 29 20 63 61 6c 6c 62 61 63 6b 20 77 68  rt() callback wh
381e0 65 6e 20 73 6f 72 74 69 6e 67 0a 2a 2a 20 61 6e  en sorting.** an
381e1 20 61 72 72 61 79 20 6f 66 20 70 65 6e 64 69 6e   array of pendin
381e2 67 20 74 65 72 6d 73 20 62 79 20 74 65 72 6d 2e  g terms by term.
381e3 20 54 68 69 73 20 6f 63 63 75 72 73 20 61 73 20   This occurs as 
381e4 70 61 72 74 20 6f 66 20 66 6c 75 73 68 69 6e 67  part of flushing
381e5 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  .** the contents
381e6 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67 2d   of the pending-
381e7 74 65 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65  terms hash table
381e8 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
381e9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
381ea 71 73 6f 72 74 43 6f 6d 70 61 72 65 28 63 6f 6e  qsortCompare(con
381eb 73 74 20 76 6f 69 64 20 2a 6c 68 73 2c 20 63 6f  st void *lhs, co
381ec 6e 73 74 20 76 6f 69 64 20 2a 72 68 73 29 7b 0a  nst void *rhs){.
381ed 20 20 63 68 61 72 20 2a 7a 31 20 3d 20 66 74 73    char *z1 = fts
381ee 33 48 61 73 68 4b 65 79 28 2a 28 46 74 73 33 48  3HashKey(*(Fts3H
381ef 61 73 68 45 6c 65 6d 20 2a 2a 29 6c 68 73 29 3b  ashElem **)lhs);
381f0 0a 20 20 63 68 61 72 20 2a 7a 32 20 3d 20 66 74  .  char *z2 = ft
381f1 73 33 48 61 73 68 4b 65 79 28 2a 28 46 74 73 33  s3HashKey(*(Fts3
381f2 48 61 73 68 45 6c 65 6d 20 2a 2a 29 72 68 73 29  HashElem **)rhs)
381f3 3b 0a 20 20 69 6e 74 20 6e 31 20 3d 20 66 74 73  ;.  int n1 = fts
381f4 33 48 61 73 68 4b 65 79 73 69 7a 65 28 2a 28 46  3HashKeysize(*(F
381f5 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 2a 29 6c  ts3HashElem **)l
381f6 68 73 29 3b 0a 20 20 69 6e 74 20 6e 32 20 3d 20  hs);.  int n2 = 
381f7 66 74 73 33 48 61 73 68 4b 65 79 73 69 7a 65 28  fts3HashKeysize(
381f8 2a 28 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a  *(Fts3HashElem *
381f9 2a 29 72 68 73 29 3b 0a 0a 20 20 69 6e 74 20 6e  *)rhs);..  int n
381fa 20 3d 20 28 6e 31 3c 6e 32 20 3f 20 6e 31 20 3a   = (n1<n2 ? n1 :
381fb 20 6e 32 29 3b 0a 20 20 69 6e 74 20 63 20 3d 20   n2);.  int c = 
381fc 6d 65 6d 63 6d 70 28 7a 31 2c 20 7a 32 2c 20 6e  memcmp(z1, z2, n
381fd 29 3b 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b  );.  if( c==0 ){
381fe 0a 20 20 20 20 63 20 3d 20 6e 31 20 2d 20 6e 32  .    c = n1 - n2
381ff 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
38200 3b 0a 7d 0a 0a 0a 2f 2a 20 0a 2a 2a 20 46 6c 75  ;.}.../* .** Flu
38201 73 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  sh the contents 
38202 6f 66 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  of pendingTerms 
38203 74 6f 20 61 20 6c 65 76 65 6c 20 30 20 73 65 67  to a level 0 seg
38204 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
38205 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
38206 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67 54 65  te3Fts3PendingTe
38207 72 6d 73 46 6c 75 73 68 28 46 74 73 33 54 61 62  rmsFlush(Fts3Tab
38208 6c 65 20 2a 70 29 7b 0a 20 20 46 74 73 33 48 61  le *p){.  Fts3Ha
38209 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20  shElem *pElem;. 
3820a 20 69 6e 74 20 69 64 78 2c 20 72 63 2c 20 69 3b   int idx, rc, i;
3820b 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20  .  Fts3HashElem 
3820c 2a 2a 61 70 45 6c 65 6d 3b 20 20 20 20 20 20 20  **apElem;       
3820d 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 70     /* Array of p
3820e 6f 69 6e 74 65 72 73 20 74 6f 20 68 61 73 68 20  ointers to hash 
3820f 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  elements */.  in
38210 74 20 6e 45 6c 65 6d 3b 20 20 20 20 20 20 20 20  t nElem;        
38211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38212 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
38213 20 69 6e 20 6e 65 77 20 73 65 67 6d 65 6e 74 20   in new segment 
38214 2a 2f 0a 20 20 53 65 67 6d 65 6e 74 57 72 69 74  */.  SegmentWrit
38215 65 72 20 2a 70 57 72 69 74 65 72 20 3d 20 30 3b  er *pWriter = 0;
38216 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20       /* Used to 
38217 77 72 69 74 65 20 74 68 65 20 73 65 67 6d 65 6e  write the segmen
38218 74 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 6e 64 20  t */..  /* Find 
38219 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  the number of te
3821a 72 6d 73 20 74 68 61 74 20 77 69 6c 6c 20 6d 61  rms that will ma
3821b 6b 65 20 75 70 20 74 68 65 20 6e 65 77 20 73 65  ke up the new se
3821c 67 6d 65 6e 74 2e 20 49 66 20 74 68 65 72 65 0a  gment. If there.
3821d 20 20 2a 2a 20 61 72 65 20 6e 6f 20 74 65 72 6d    ** are no term
3821e 73 2c 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20  s, return early 
3821f 28 64 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 74  (do not bother t
38220 6f 20 77 72 69 74 65 20 61 6e 20 65 6d 70 74 79  o write an empty
38221 20 73 65 67 6d 65 6e 74 29 2e 0a 20 20 2a 2f 0a   segment)..  */.
38222 20 20 6e 45 6c 65 6d 20 3d 20 66 74 73 33 48 61    nElem = fts3Ha
38223 73 68 43 6f 75 6e 74 28 26 70 2d 3e 70 65 6e 64  shCount(&p->pend
38224 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20 69 66 28  ingTerms);.  if(
38225 20 6e 45 6c 65 6d 3d 3d 30 20 29 7b 0a 20 20 20   nElem==0 ){.   
38226 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 50 65 6e   assert( p->nPen
38227 64 69 6e 67 44 61 74 61 3d 3d 30 20 29 3b 0a 20  dingData==0 );. 
38228 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
38229 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44  _OK;.  }..  /* D
3822a 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78  etermine the nex
3822b 74 20 69 6e 64 65 78 20 61 74 20 6c 65 76 65 6c  t index at level
3822c 20 30 2c 20 6d 65 72 67 69 6e 67 20 61 73 20 6e   0, merging as n
3822d 65 63 65 73 73 61 72 79 2e 20 2a 2f 0a 20 20 72  ecessary. */.  r
3822e 63 20 3d 20 66 74 73 33 41 6c 6c 6f 63 61 74 65  c = fts3Allocate
3822f 53 65 67 64 69 72 49 64 78 28 70 2c 20 30 2c 20  SegdirIdx(p, 0, 
38230 26 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21  &idx);.  if( rc!
38231 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
38232 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
38233 20 0a 0a 20 20 61 70 45 6c 65 6d 20 3d 20 73 71   ..  apElem = sq
38234 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 45 6c  lite3_malloc(nEl
38235 65 6d 2a 73 69 7a 65 6f 66 28 46 74 73 33 48 61  em*sizeof(Fts3Ha
38236 73 68 45 6c 65 6d 20 2a 29 29 3b 0a 20 20 69 66  shElem *));.  if
38237 28 20 21 61 70 45 6c 65 6d 20 29 7b 0a 20 20 20  ( !apElem ){.   
38238 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
38239 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 20 3d  OMEM;.  }..  i =
3823a 20 30 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d   0;.  for(pElem=
3823b 66 74 73 33 48 61 73 68 46 69 72 73 74 28 26 70  fts3HashFirst(&p
3823c 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b  ->pendingTerms);
3823d 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 66 74   pElem; pElem=ft
3823e 73 33 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d  s3HashNext(pElem
3823f 29 29 7b 0a 20 20 20 20 61 70 45 6c 65 6d 5b 69  )){.    apElem[i
38240 2b 2b 5d 20 3d 20 70 45 6c 65 6d 3b 0a 20 20 7d  ++] = pElem;.  }
38241 0a 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 45  .  assert( i==nE
38242 6c 65 6d 20 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44  lem );..  /* TOD
38243 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64 20  O(shess) Should 
38244 77 65 20 61 6c 6c 6f 77 20 75 73 65 72 2d 64 65  we allow user-de
38245 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  fined collation 
38246 73 65 71 75 65 6e 63 65 73 2c 0a 20 20 2a 2a 20  sequences,.  ** 
38247 68 65 72 65 3f 20 20 49 20 74 68 69 6e 6b 20 77  here?  I think w
38248 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 68 61 74  e only need that
38249 20 6f 6e 63 65 20 77 65 20 73 75 70 70 6f 72 74   once we support
3824a 20 70 72 65 66 69 78 20 73 65 61 72 63 68 65 73   prefix searches
3824b 2e 0a 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 68 6f  ..  ** Also, sho
3824c 75 6c 64 20 77 65 20 62 65 20 75 73 69 6e 67 20  uld we be using 
3824d 71 73 6f 72 74 28 29 3f 0a 20 20 2a 2f 0a 20 20  qsort()?.  */.  
3824e 69 66 28 20 6e 45 6c 65 6d 3e 31 20 29 7b 0a 20  if( nElem>1 ){. 
3824f 20 20 20 71 73 6f 72 74 28 61 70 45 6c 65 6d 2c     qsort(apElem,
38250 20 6e 45 6c 65 6d 2c 20 73 69 7a 65 6f 66 28 46   nElem, sizeof(F
38251 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 29 2c 20  ts3HashElem *), 
38252 71 73 6f 72 74 43 6f 6d 70 61 72 65 29 3b 0a 20  qsortCompare);. 
38253 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20   }...  /* Write 
38254 74 68 65 20 73 65 67 6d 65 6e 74 20 74 72 65 65  the segment tree
38255 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
38256 73 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  se. */.  for(i=0
38257 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
38258 26 26 20 69 3c 6e 45 6c 65 6d 3b 20 69 2b 2b 29  && i<nElem; i++)
38259 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
3825a 20 2a 7a 20 3d 20 66 74 73 33 48 61 73 68 4b 65   *z = fts3HashKe
3825b 79 28 61 70 45 6c 65 6d 5b 69 5d 29 3b 0a 20 20  y(apElem[i]);.  
3825c 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 48 61    int n = fts3Ha
3825d 73 68 4b 65 79 73 69 7a 65 28 61 70 45 6c 65 6d  shKeysize(apElem
3825e 5b 69 5d 29 3b 0a 20 20 20 20 50 65 6e 64 69 6e  [i]);.    Pendin
3825f 67 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 66  gList *pList = f
38260 74 73 33 48 61 73 68 44 61 74 61 28 61 70 45 6c  ts3HashData(apEl
38261 65 6d 5b 69 5d 29 3b 0a 20 20 20 20 72 63 20 3d  em[i]);.    rc =
38262 20 66 74 73 33 53 65 67 57 72 69 74 65 72 41 64   fts3SegWriterAd
38263 64 28 70 2c 20 26 70 57 72 69 74 65 72 2c 20 30  d(p, &pWriter, 0
38264 2c 20 7a 2c 20 6e 2c 20 70 4c 69 73 74 2d 3e 61  , z, n, pList->a
38265 44 61 74 61 2c 20 70 4c 69 73 74 2d 3e 6e 44 61  Data, pList->nDa
38266 74 61 2b 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28  ta+1);.  }.  if(
38267 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
38268 7b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53  {.    rc = fts3S
38269 65 67 57 72 69 74 65 72 46 6c 75 73 68 28 70 2c  egWriterFlush(p,
3826a 20 70 57 72 69 74 65 72 2c 20 30 2c 20 69 64 78   pWriter, 0, idx
3826b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65  );.  }..  /* Fre
3826c 65 20 61 6c 6c 20 61 6c 6c 6f 63 61 74 65 64 20  e all allocated 
3826d 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 65  resources before
3826e 20 72 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20   returning */.  
3826f 66 74 73 33 53 65 67 57 72 69 74 65 72 46 72 65  fts3SegWriterFre
38270 65 28 70 57 72 69 74 65 72 29 3b 0a 20 20 73 71  e(pWriter);.  sq
38271 6c 69 74 65 33 5f 66 72 65 65 28 61 70 45 6c 65  lite3_free(apEle
38272 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73  m);.  sqlite3Fts
38273 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 43 6c 65  3PendingTermsCle
38274 61 72 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ar(p);.  return 
38275 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
38276 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20  s function does 
38277 74 68 65 20 77 6f 72 6b 20 66 6f 72 20 74 68 65  the work for the
38278 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20   xUpdate method 
38279 6f 66 20 46 54 53 33 20 76 69 72 74 75 61 6c 0a  of FTS3 virtual.
3827a 2a 2a 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51  ** tables..*/.SQ
3827b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
3827c 20 73 71 6c 69 74 65 33 46 74 73 33 55 70 64 61   sqlite3Fts3Upda
3827d 74 65 4d 65 74 68 6f 64 28 0a 20 20 73 71 6c 69  teMethod(.  sqli
3827e 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c  te3_vtab *pVtab,
3827f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
38280 54 53 33 20 76 74 61 62 20 6f 62 6a 65 63 74 20  TS3 vtab object 
38281 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20  */.  int nArg,  
38282 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38283 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
38284 61 72 67 75 6d 65 6e 74 20 61 72 72 61 79 20 2a  argument array *
38285 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  /.  sqlite3_valu
38286 65 20 2a 2a 61 70 56 61 6c 2c 20 20 20 20 20 20  e **apVal,      
38287 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
38288 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 73  arguments */.  s
38289 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f  qlite_int64 *pRo
3828a 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 2f  wid            /
3828b 2a 20 4f 55 54 3a 20 54 68 65 20 61 66 66 65 63  * OUT: The affec
3828c 74 65 64 20 28 6f 72 20 65 66 66 65 63 74 65 64  ted (or effected
3828d 29 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20  ) rowid */.){.  
3828e 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28  Fts3Table *p = (
3828f 46 74 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61  Fts3Table *)pVta
38290 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  b;.  int rc = SQ
38291 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
38292 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43       /* Return C
38293 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52  ode */.  int isR
38294 65 6d 6f 76 65 20 3d 20 30 3b 20 20 20 20 20 20  emove = 0;      
38295 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
38296 20 66 6f 72 20 61 6e 20 55 50 44 41 54 45 20 6f   for an UPDATE o
38297 72 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 73 71  r DELETE */.  sq
38298 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 65 6d  lite3_int64 iRem
38299 6f 76 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ove;          /*
3829a 20 52 6f 77 69 64 20 72 65 6d 6f 76 65 64 20 62   Rowid removed b
3829b 79 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  y UPDATE or DELE
3829c 54 45 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  TE */..  /* If t
3829d 68 69 73 20 69 73 20 61 20 44 45 4c 45 54 45 20  his is a DELETE 
3829e 6f 72 20 55 50 44 41 54 45 20 6f 70 65 72 61 74  or UPDATE operat
3829f 69 6f 6e 2c 20 72 65 6d 6f 76 65 20 74 68 65 20  ion, remove the 
382a0 6f 6c 64 20 72 65 63 6f 72 64 2e 20 2a 2f 0a 20  old record. */. 
382a1 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
382a2 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b 30 5d  ue_type(apVal[0]
382a3 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )!=SQLITE_NULL )
382a4 7b 0a 20 20 20 20 69 6e 74 20 69 73 45 6d 70 74  {.    int isEmpt
382a5 79 3b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  y;.    rc = fts3
382a6 49 73 45 6d 70 74 79 28 70 2c 20 61 70 56 61 6c  IsEmpty(p, apVal
382a7 2c 20 26 69 73 45 6d 70 74 79 29 3b 0a 20 20 20  , &isEmpty);.   
382a8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
382a9 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  OK ){.      if( 
382aa 69 73 45 6d 70 74 79 20 29 7b 0a 20 20 20 20 20  isEmpty ){.     
382ab 20 20 20 2f 2a 20 44 65 6c 65 74 69 6e 67 20 74     /* Deleting t
382ac 68 69 73 20 72 6f 77 20 6d 65 61 6e 73 20 74 68  his row means th
382ad 65 20 77 68 6f 6c 65 20 74 61 62 6c 65 20 69 73  e whole table is
382ae 20 65 6d 70 74 79 2e 20 49 6e 20 74 68 69 73 20   empty. In this 
382af 63 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  case.        ** 
382b0 64 65 6c 65 74 65 20 74 68 65 20 63 6f 6e 74 65  delete the conte
382b1 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68 72 65 65  nts of all three
382b2 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68 72 6f   tables and thro
382b3 77 20 61 77 61 79 20 61 6e 79 0a 20 20 20 20 20  w away any.     
382b4 20 20 20 2a 2a 20 64 61 74 61 20 69 6e 20 74 68     ** data in th
382b5 65 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 68  e pendingTerms h
382b6 61 73 68 20 74 61 62 6c 65 2e 0a 20 20 20 20 20  ash table..     
382b7 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63     */.        rc
382b8 20 3d 20 66 74 73 33 44 65 6c 65 74 65 41 6c 6c   = fts3DeleteAll
382b9 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  (p);.      }else
382ba 7b 0a 20 20 20 20 20 20 20 20 69 73 52 65 6d 6f  {.        isRemo
382bb 76 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ve = 1;.        
382bc 69 52 65 6d 6f 76 65 20 3d 20 73 71 6c 69 74 65  iRemove = sqlite
382bd 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 70  3_value_int64(ap
382be 56 61 6c 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20  Val[0]);.       
382bf 20 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e   rc = fts3Pendin
382c0 67 54 65 72 6d 73 44 6f 63 69 64 28 70 2c 20 69  gTermsDocid(p, i
382c1 52 65 6d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20  Remove);.       
382c2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
382c3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
382c4 72 63 20 3d 20 66 74 73 33 44 65 6c 65 74 65 54  rc = fts3DeleteT
382c5 65 72 6d 73 28 70 2c 20 61 70 56 61 6c 29 3b 0a  erms(p, apVal);.
382c6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
382c7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
382c8 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
382c9 66 74 73 33 53 71 6c 45 78 65 63 28 70 2c 20 53  fts3SqlExec(p, S
382ca 51 4c 5f 44 45 4c 45 54 45 5f 43 4f 4e 54 45 4e  QL_DELETE_CONTEN
382cb 54 2c 20 61 70 56 61 6c 29 3b 0a 20 20 20 20 20  T, apVal);.     
382cc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
382cd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
382ce 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20 74 68   }.  .  /* If th
382cf 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
382d0 6f 72 20 55 50 44 41 54 45 20 6f 70 65 72 61 74  or UPDATE operat
382d1 69 6f 6e 2c 20 69 6e 73 65 72 74 20 74 68 65 20  ion, insert the 
382d2 6e 65 77 20 72 65 63 6f 72 64 2e 20 2a 2f 0a 20  new record. */. 
382d3 20 69 66 28 20 6e 41 72 67 3e 31 20 26 26 20 72   if( nArg>1 && r
382d4 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
382d5 20 20 20 20 72 63 20 3d 20 66 74 73 33 49 6e 73      rc = fts3Ins
382d6 65 72 74 44 61 74 61 28 70 2c 20 61 70 56 61 6c  ertData(p, apVal
382d7 2c 20 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 69  , pRowid);.    i
382d8 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
382d9 20 26 26 20 28 21 69 73 52 65 6d 6f 76 65 20 7c   && (!isRemove |
382da 7c 20 2a 70 52 6f 77 69 64 21 3d 69 52 65 6d 6f  | *pRowid!=iRemo
382db 76 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ve) ){.      rc 
382dc 3d 20 66 74 73 33 50 65 6e 64 69 6e 67 54 65 72  = fts3PendingTer
382dd 6d 73 44 6f 63 69 64 28 70 2c 20 2a 70 52 6f 77  msDocid(p, *pRow
382de 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  id);.    }.    i
382df 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
382e0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66   ){.      rc = f
382e1 74 73 33 49 6e 73 65 72 74 54 65 72 6d 73 28 70  ts3InsertTerms(p
382e2 2c 20 61 70 56 61 6c 29 3b 0a 20 20 20 20 7d 0a  , apVal);.    }.
382e3 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
382e4 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 46 6c 75 73  ;.}../* .** Flus
382e5 68 20 61 6e 79 20 64 61 74 61 20 69 6e 20 74 68  h any data in th
382e6 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20  e pending-terms 
382e7 68 61 73 68 20 74 61 62 6c 65 20 74 6f 20 64 69  hash table to di
382e8 73 6b 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  sk. If successfu
382e9 6c 2c 0a 2a 2a 20 6d 65 72 67 65 20 61 6c 6c 20  l,.** merge all 
382ea 73 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  segments in the 
382eb 64 61 74 61 62 61 73 65 20 28 69 6e 63 6c 75 64  database (includ
382ec 69 6e 67 20 74 68 65 20 6e 65 77 20 73 65 67 6d  ing the new segm
382ed 65 6e 74 2c 20 69 66 20 0a 2a 2a 20 74 68 65 72  ent, if .** ther
382ee 65 20 77 61 73 20 61 6e 79 20 64 61 74 61 20 74  e was any data t
382ef 6f 20 66 6c 75 73 68 29 20 69 6e 74 6f 20 61 20  o flush) into a 
382f0 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74 2e 20  single segment. 
382f1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
382f2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
382f3 73 33 4f 70 74 69 6d 69 7a 65 28 46 74 73 33 54  s3Optimize(Fts3T
382f4 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  able *p){.  int 
382f5 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
382f6 50 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73  PendingTermsFlus
382f7 68 28 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  h(p);.  if( rc==
382f8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
382f9 20 72 63 20 3d 20 66 74 73 33 53 65 67 6d 65 6e   rc = fts3Segmen
382fa 74 4d 65 72 67 65 28 70 2c 20 2d 31 29 3b 0a 20  tMerge(p, -1);. 
382fb 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
382fc 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  }..#endif../****
382fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
382fe 66 20 66 74 73 33 5f 77 72 69 74 65 2e 63 20 2a  f fts3_write.c *
382ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38301 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
38302 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
38303 20 66 69 6c 65 20 66 74 73 33 5f 73 6e 69 70 70   file fts3_snipp
38304 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  et.c ***********
38305 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38306 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
38307 20 32 30 30 39 20 4f 63 74 20 32 33 0a 2a 2a 0a   2009 Oct 23.**.
38308 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
38309 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
3830a 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
3830b 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
3830c 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
3830d 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
3830e 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
3830f 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
38310 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
38311 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
38312 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
38313 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
38314 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
38315 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
38316 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
38317 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
38318 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
38319 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
3831a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3831b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3831c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3831d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3831e 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  .*/..#if !define
3831f 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
38320 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
38321 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a  _ENABLE_FTS3)...
38322 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
38323 6e 69 70 70 65 74 20 53 6e 69 70 70 65 74 3b 0a  nippet Snippet;.
38324 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
38325 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
38326 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65  ing structure ke
38327 65 70 73 20 74 72 61 63 6b 20 6f 66 20 67 65 6e  eps track of gen
38328 65 72 61 74 65 64 0a 2a 2a 20 6d 61 74 63 68 69  erated.** matchi
38329 6e 67 2d 77 6f 72 64 20 6f 66 66 73 65 74 20 69  ng-word offset i
3832a 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 73  nformation and s
3832b 6e 69 70 70 65 74 73 2e 0a 2a 2f 0a 73 74 72 75  nippets..*/.stru
3832c 63 74 20 53 6e 69 70 70 65 74 20 7b 0a 20 20 69  ct Snippet {.  i
3832d 6e 74 20 6e 4d 61 74 63 68 3b 20 20 20 20 20 20  nt nMatch;      
3832e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3832f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
38330 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20 20 69  f matches */.  i
38331 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20  nt nAlloc;      
38332 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38333 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  * Space allocate
38334 64 20 66 6f 72 20 61 4d 61 74 63 68 5b 5d 20 2a  d for aMatch[] *
38335 2f 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70  /.  struct snipp
38336 65 74 4d 61 74 63 68 20 7b 20 20 2f 2a 20 4f 6e  etMatch {  /* On
38337 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68  e entry for each
38338 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20 2a   matching term *
38339 2f 0a 20 20 20 20 63 68 61 72 20 73 6e 53 74 61  /.    char snSta
3833a 74 75 73 3b 20 20 20 20 20 20 20 2f 2a 20 53 74  tus;       /* St
3833b 61 74 75 73 20 66 6c 61 67 20 66 6f 72 20 75 73  atus flag for us
3833c 65 20 77 68 69 6c 65 20 63 6f 6e 73 74 72 75 63  e while construc
3833d 74 69 6e 67 20 73 6e 69 70 70 65 74 73 20 2a 2f  ting snippets */
3833e 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20 69  .    short int i
3833f 43 6f 6c 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  Col;      /* The
38340 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 63 6f 6e   column that con
38341 74 61 69 6e 73 20 74 68 65 20 6d 61 74 63 68 20  tains the match 
38342 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74  */.    short int
38343 20 69 54 65 72 6d 3b 20 20 20 20 20 2f 2a 20 54   iTerm;     /* T
38344 68 65 20 69 6e 64 65 78 20 69 6e 20 51 75 65 72  he index in Quer
38345 79 2e 70 54 65 72 6d 73 5b 5d 20 6f 66 20 74 68  y.pTerms[] of th
38346 65 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20  e matching term 
38347 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 6f 6b 65  */.    int iToke
38348 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  n;          /* T
38349 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
3834a 6d 61 74 63 68 69 6e 67 20 64 6f 63 75 6d 65 6e  matching documen
3834b 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 73  t token */.    s
3834c 68 6f 72 74 20 69 6e 74 20 6e 42 79 74 65 3b 20  hort int nByte; 
3834d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3834e 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 74 65   bytes in the te
3834f 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53  rm */.    int iS
38350 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 2f  tart;          /
38351 2a 20 54 68 65 20 6f 66 66 73 65 74 20 74 6f 20  * The offset to 
38352 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63  the first charac
38353 74 65 72 20 6f 66 20 74 68 65 20 74 65 72 6d 20  ter of the term 
38354 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74 63 68 3b 20  */.  } *aMatch; 
38355 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38356 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 73 20 74       /* Points t
38357 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64  o space obtained
38358 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 2a 2f 0a   from malloc */.
38359 20 20 63 68 61 72 20 2a 7a 4f 66 66 73 65 74 3b    char *zOffset;
3835a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3835b 20 20 2f 2a 20 54 65 78 74 20 72 65 6e 64 65 72    /* Text render
3835c 69 6e 67 20 6f 66 20 61 4d 61 74 63 68 5b 5d 20  ing of aMatch[] 
3835d 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66 66 73 65 74  */.  int nOffset
3835e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3835f 20 20 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a       /* strlen(z
38360 4f 66 66 73 65 74 29 20 2a 2f 0a 20 20 63 68 61  Offset) */.  cha
38361 72 20 2a 7a 53 6e 69 70 70 65 74 3b 20 20 20 20  r *zSnippet;    
38362 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38363 53 6e 69 70 70 65 74 20 74 65 78 74 20 2a 2f 0a  Snippet text */.
38364 20 20 69 6e 74 20 6e 53 6e 69 70 70 65 74 3b 20    int nSnippet; 
38365 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38366 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 53 6e 69    /* strlen(zSni
38367 70 70 65 74 29 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a  ppet) */.};.../*
38368 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20   It is not safe 
38369 74 6f 20 63 61 6c 6c 20 69 73 73 70 61 63 65 28  to call isspace(
3836a 29 2c 20 74 6f 6c 6f 77 65 72 28 29 2c 20 6f 72  ), tolower(), or
3836b 20 69 73 61 6c 6e 75 6d 28 29 20 6f 6e 0a 2a 2a   isalnum() on.**
3836c 20 68 69 2d 62 69 74 2d 73 65 74 20 63 68 61 72   hi-bit-set char
3836d 61 63 74 65 72 73 2e 20 20 54 68 69 73 20 69 73  acters.  This is
3836e 20 74 68 65 20 73 61 6d 65 20 73 6f 6c 75 74 69   the same soluti
3836f 6f 6e 20 75 73 65 64 20 69 6e 20 74 68 65 0a 2a  on used in the.*
38370 2a 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a  * tokenizer..*/.
38371 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54  /* TODO(shess) T
38372 68 65 20 73 6e 69 70 70 65 74 2d 67 65 6e 65 72  he snippet-gener
38373 61 74 69 6f 6e 20 63 6f 64 65 20 73 68 6f 75 6c  ation code shoul
38374 64 20 62 65 20 75 73 69 6e 67 20 74 68 65 0a 2a  d be using the.*
38375 2a 20 74 6f 6b 65 6e 69 7a 65 72 2d 67 65 6e 65  * tokenizer-gene
38376 72 61 74 65 64 20 74 6f 6b 65 6e 73 20 72 61 74  rated tokens rat
38377 68 65 72 20 74 68 61 6e 20 64 6f 69 6e 67 20 69  her than doing i
38378 74 73 20 6f 77 6e 20 6c 6f 63 61 6c 0a 2a 2a 20  ts own local.** 
38379 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2f  tokenization..*/
3837a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
3837b 49 73 20 5f 5f 69 73 61 73 63 69 69 28 29 20 61  Is __isascii() a
3837c 20 70 6f 72 74 61 62 6c 65 20 76 65 72 73 69 6f   portable versio
3837d 6e 20 6f 66 20 28 63 26 30 78 38 30 29 3d 3d 30  n of (c&0x80)==0
3837e 3f 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ? */.static int 
3837f 73 61 66 65 5f 69 73 73 70 61 63 65 28 63 68 61  safe_isspace(cha
38380 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  r c){.  return (
38381 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73  c&0x80)==0 ? iss
38382 70 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 73  pace(c) : 0;.}.s
38383 74 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f 69  tatic int safe_i
38384 73 61 6c 6e 75 6d 28 63 68 61 72 20 63 29 7b 0a  salnum(char c){.
38385 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38 30    return (c&0x80
38386 29 3d 3d 30 20 3f 20 69 73 61 6c 6e 75 6d 28 63  )==0 ? isalnum(c
38387 29 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  ) : 0;.}../*****
38388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3838a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3838b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
3838c 2f 2a 20 44 61 74 61 42 75 66 66 65 72 20 69 73  /* DataBuffer is
3838d 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74   used to collect
3838e 20 64 61 74 61 20 69 6e 74 6f 20 61 20 62 75 66   data into a buf
3838f 66 65 72 20 69 6e 20 70 69 65 63 65 6d 65 61 6c  fer in piecemeal
38390 0a 2a 2a 20 66 61 73 68 69 6f 6e 2e 20 20 49 74  .** fashion.  It
38391 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
38392 75 73 75 61 6c 20 64 69 73 74 69 6e 63 74 69 6f  usual distinctio
38393 6e 20 62 65 74 77 65 65 6e 20 61 6d 6f 75 6e 74  n between amount
38394 20 6f 66 0a 2a 2a 20 64 61 74 61 20 63 75 72 72   of.** data curr
38395 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 28 6e 44  ently stored (nD
38396 61 74 61 29 20 61 6e 64 20 62 75 66 66 65 72 20  ata) and buffer 
38397 63 61 70 61 63 69 74 79 20 28 6e 43 61 70 61 63  capacity (nCapac
38398 69 74 79 29 2e 0a 2a 2a 0a 2a 2a 20 64 61 74 61  ity)..**.** data
38399 42 75 66 66 65 72 49 6e 69 74 20 2d 20 63 72 65  BufferInit - cre
3839a 61 74 65 20 61 20 62 75 66 66 65 72 20 77 69 74  ate a buffer wit
3839b 68 20 67 69 76 65 6e 20 69 6e 69 74 69 61 6c 20  h given initial 
3839c 63 61 70 61 63 69 74 79 2e 0a 2a 2a 20 64 61 74  capacity..** dat
3839d 61 42 75 66 66 65 72 52 65 73 65 74 20 2d 20 66  aBufferReset - f
3839e 6f 72 67 65 74 20 62 75 66 66 65 72 27 73 20 64  orget buffer's d
3839f 61 74 61 2c 20 72 65 74 61 69 6e 69 6e 67 20 63  ata, retaining c
383a0 61 70 61 63 69 74 79 2e 0a 2a 2a 20 64 61 74 61  apacity..** data
383a1 42 75 66 66 65 72 53 77 61 70 20 2d 20 73 77 61  BufferSwap - swa
383a2 70 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77  p contents of tw
383a3 6f 20 62 75 66 66 65 72 73 2e 0a 2a 2a 20 64 61  o buffers..** da
383a4 74 61 42 75 66 66 65 72 45 78 70 61 6e 64 20 2d  taBufferExpand -
383a5 20 65 78 70 61 6e 64 20 63 61 70 61 63 69 74 79   expand capacity
383a6 20 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20   without adding 
383a7 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42 75 66  data..** dataBuf
383a8 66 65 72 41 70 70 65 6e 64 20 2d 20 61 70 70 65  ferAppend - appe
383a9 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61  nd data..** data
383aa 42 75 66 66 65 72 41 70 70 65 6e 64 32 20 2d 20  BufferAppend2 - 
383ab 61 70 70 65 6e 64 20 74 77 6f 20 70 69 65 63 65  append two piece
383ac 73 20 6f 66 20 64 61 74 61 20 61 74 20 6f 6e 63  s of data at onc
383ad 65 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72  e..** dataBuffer
383ae 52 65 70 6c 61 63 65 20 2d 20 72 65 70 6c 61 63  Replace - replac
383af 65 20 62 75 66 66 65 72 27 73 20 64 61 74 61 2e  e buffer's data.
383b0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
383b1 63 74 20 44 61 74 61 42 75 66 66 65 72 20 7b 0a  ct DataBuffer {.
383b2 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20    char *pData;  
383b3 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
383b4 65 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 65 64 20  er to malloc'ed 
383b5 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 6e 74  buffer. */.  int
383b6 20 6e 43 61 70 61 63 69 74 79 3b 20 20 20 20 20   nCapacity;     
383b7 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 44     /* Size of pD
383b8 61 74 61 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20  ata buffer. */. 
383b9 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20   int nData;     
383ba 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66         /* End of
383bb 20 64 61 74 61 20 6c 6f 61 64 65 64 20 69 6e 74   data loaded int
383bc 6f 20 70 44 61 74 61 2e 20 2a 2f 0a 7d 20 44 61  o pData. */.} Da
383bd 74 61 42 75 66 66 65 72 3b 0a 0a 73 74 61 74 69  taBuffer;..stati
383be 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
383bf 72 49 6e 69 74 28 44 61 74 61 42 75 66 66 65 72  rInit(DataBuffer
383c0 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e   *pBuffer, int n
383c1 43 61 70 61 63 69 74 79 29 7b 0a 20 20 61 73 73  Capacity){.  ass
383c2 65 72 74 28 20 6e 43 61 70 61 63 69 74 79 3e 3d  ert( nCapacity>=
383c3 30 20 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e  0 );.  pBuffer->
383c4 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 70 42 75  nData = 0;.  pBu
383c5 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20  ffer->nCapacity 
383c6 3d 20 6e 43 61 70 61 63 69 74 79 3b 0a 20 20 70  = nCapacity;.  p
383c7 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20  Buffer->pData = 
383c8 6e 43 61 70 61 63 69 74 79 3d 3d 30 20 3f 20 4e  nCapacity==0 ? N
383c9 55 4c 4c 20 3a 20 73 71 6c 69 74 65 33 5f 6d 61  ULL : sqlite3_ma
383ca 6c 6c 6f 63 28 6e 43 61 70 61 63 69 74 79 29 3b  lloc(nCapacity);
383cb 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
383cc 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28 44  ataBufferReset(D
383cd 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
383ce 65 72 29 7b 0a 20 20 70 42 75 66 66 65 72 2d 3e  er){.  pBuffer->
383cf 6e 44 61 74 61 20 3d 20 30 3b 0a 7d 0a 73 74 61  nData = 0;.}.sta
383d0 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
383d1 66 65 72 45 78 70 61 6e 64 28 44 61 74 61 42 75  ferExpand(DataBu
383d2 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69  ffer *pBuffer, i
383d3 6e 74 20 6e 41 64 64 43 61 70 61 63 69 74 79 29  nt nAddCapacity)
383d4 7b 0a 20 20 61 73 73 65 72 74 28 20 6e 41 64 64  {.  assert( nAdd
383d5 43 61 70 61 63 69 74 79 3e 30 20 29 3b 0a 20 20  Capacity>0 );.  
383d6 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43  /* TODO(shess) C
383d7 6f 6e 73 69 64 65 72 20 65 78 70 61 6e 64 69 6e  onsider expandin
383d8 67 20 6d 6f 72 65 20 61 67 67 72 65 73 73 69 76  g more aggressiv
383d9 65 6c 79 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  ely.  Note that 
383da 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79  the.  ** underly
383db 69 6e 67 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65  ing malloc imple
383dc 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79 20 74 61  mentation may ta
383dd 6b 65 20 63 61 72 65 20 6f 66 20 73 75 63 68 20  ke care of such 
383de 74 68 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20  things for.  ** 
383df 75 73 20 61 6c 72 65 61 64 79 2e 0a 20 20 2a 2f  us already..  */
383e0 0a 20 20 69 66 28 20 70 42 75 66 66 65 72 2d 3e  .  if( pBuffer->
383e1 6e 44 61 74 61 2b 6e 41 64 64 43 61 70 61 63 69  nData+nAddCapaci
383e2 74 79 3e 70 42 75 66 66 65 72 2d 3e 6e 43 61 70  ty>pBuffer->nCap
383e3 61 63 69 74 79 20 29 7b 0a 20 20 20 20 70 42 75  acity ){.    pBu
383e4 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20  ffer->nCapacity 
383e5 3d 20 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61  = pBuffer->nData
383e6 2b 6e 41 64 64 43 61 70 61 63 69 74 79 3b 0a 20  +nAddCapacity;. 
383e7 20 20 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74     pBuffer->pDat
383e8 61 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  a = sqlite3_real
383e9 6c 6f 63 28 70 42 75 66 66 65 72 2d 3e 70 44 61  loc(pBuffer->pDa
383ea 74 61 2c 20 70 42 75 66 66 65 72 2d 3e 6e 43 61  ta, pBuffer->nCa
383eb 70 61 63 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 73  pacity);.  }.}.s
383ec 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
383ed 75 66 66 65 72 41 70 70 65 6e 64 28 44 61 74 61  ufferAppend(Data
383ee 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c  Buffer *pBuffer,
383ef 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
383f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
383f1 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63  nst char *pSourc
383f2 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b  e, int nSource){
383f3 0a 20 20 61 73 73 65 72 74 28 20 6e 53 6f 75 72  .  assert( nSour
383f4 63 65 3e 30 20 26 26 20 70 53 6f 75 72 63 65 21  ce>0 && pSource!
383f5 3d 4e 55 4c 4c 20 29 3b 0a 20 20 64 61 74 61 42  =NULL );.  dataB
383f6 75 66 66 65 72 45 78 70 61 6e 64 28 70 42 75 66  ufferExpand(pBuf
383f7 66 65 72 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 20  fer, nSource);. 
383f8 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d   memcpy(pBuffer-
383f9 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e  >pData+pBuffer->
383fa 6e 44 61 74 61 2c 20 70 53 6f 75 72 63 65 2c 20  nData, pSource, 
383fb 6e 53 6f 75 72 63 65 29 3b 0a 20 20 70 42 75 66  nSource);.  pBuf
383fc 66 65 72 2d 3e 6e 44 61 74 61 20 2b 3d 20 6e 53  fer->nData += nS
383fd 6f 75 72 63 65 3b 0a 7d 0a 73 74 61 74 69 63 20  ource;.}.static 
383fe 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 41  void dataBufferA
383ff 70 70 65 6e 64 32 28 44 61 74 61 42 75 66 66 65  ppend2(DataBuffe
38400 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20 20 20  r *pBuffer,.    
38401 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38402 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
38403 63 68 61 72 20 2a 70 53 6f 75 72 63 65 31 2c 20  char *pSource1, 
38404 69 6e 74 20 6e 53 6f 75 72 63 65 31 2c 0a 20 20  int nSource1,.  
38405 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38406 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
38407 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65 32  t char *pSource2
38408 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65 32 29 7b  , int nSource2){
38409 0a 20 20 61 73 73 65 72 74 28 20 6e 53 6f 75 72  .  assert( nSour
3840a 63 65 31 3e 30 20 26 26 20 70 53 6f 75 72 63 65  ce1>0 && pSource
3840b 31 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73  1!=NULL );.  ass
3840c 65 72 74 28 20 6e 53 6f 75 72 63 65 32 3e 30 20  ert( nSource2>0 
3840d 26 26 20 70 53 6f 75 72 63 65 32 21 3d 4e 55 4c  && pSource2!=NUL
3840e 4c 20 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  L );.  dataBuffe
3840f 72 45 78 70 61 6e 64 28 70 42 75 66 66 65 72 2c  rExpand(pBuffer,
38410 20 6e 53 6f 75 72 63 65 31 2b 6e 53 6f 75 72 63   nSource1+nSourc
38411 65 32 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42  e2);.  memcpy(pB
38412 75 66 66 65 72 2d 3e 70 44 61 74 61 2b 70 42 75  uffer->pData+pBu
38413 66 66 65 72 2d 3e 6e 44 61 74 61 2c 20 70 53 6f  ffer->nData, pSo
38414 75 72 63 65 31 2c 20 6e 53 6f 75 72 63 65 31 29  urce1, nSource1)
38415 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66  ;.  memcpy(pBuff
38416 65 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65  er->pData+pBuffe
38417 72 2d 3e 6e 44 61 74 61 2b 6e 53 6f 75 72 63 65  r->nData+nSource
38418 31 2c 20 70 53 6f 75 72 63 65 32 2c 20 6e 53 6f  1, pSource2, nSo
38419 75 72 63 65 32 29 3b 0a 20 20 70 42 75 66 66 65  urce2);.  pBuffe
3841a 72 2d 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75  r->nData += nSou
3841b 72 63 65 31 2b 6e 53 6f 75 72 63 65 32 3b 0a 7d  rce1+nSource2;.}
3841c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
3841d 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 44  aBufferReplace(D
3841e 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
3841f 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
38420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38421 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53    const char *pS
38422 6f 75 72 63 65 2c 20 69 6e 74 20 6e 53 6f 75 72  ource, int nSour
38423 63 65 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65  ce){.  dataBuffe
38424 72 52 65 73 65 74 28 70 42 75 66 66 65 72 29 3b  rReset(pBuffer);
38425 0a 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70  .  dataBufferApp
38426 65 6e 64 28 70 42 75 66 66 65 72 2c 20 70 53 6f  end(pBuffer, pSo
38427 75 72 63 65 2c 20 6e 53 6f 75 72 63 65 29 3b 0a  urce, nSource);.
38428 7d 0a 0a 0a 2f 2a 20 53 74 72 69 6e 67 42 75 66  }.../* StringBuf
38429 66 65 72 20 69 73 20 61 20 6e 75 6c 6c 2d 74 65  fer is a null-te
3842a 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f 6e  rminated version
3842b 20 6f 66 20 44 61 74 61 42 75 66 66 65 72 2e 20   of DataBuffer. 
3842c 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
3842d 74 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 7b  t StringBuffer {
3842e 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 62 3b  .  DataBuffer b;
3842f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
38430 6e 63 6c 75 64 65 73 20 6e 75 6c 6c 20 74 65 72  ncludes null ter
38431 6d 69 6e 61 74 6f 72 2e 20 2a 2f 0a 7d 20 53 74  minator. */.} St
38432 72 69 6e 67 42 75 66 66 65 72 3b 0a 0a 73 74 61  ringBuffer;..sta
38433 74 69 63 20 76 6f 69 64 20 69 6e 69 74 53 74 72  tic void initStr
38434 69 6e 67 42 75 66 66 65 72 28 53 74 72 69 6e 67  ingBuffer(String
38435 42 75 66 66 65 72 20 2a 73 62 29 7b 0a 20 20 64  Buffer *sb){.  d
38436 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 73  ataBufferInit(&s
38437 62 2d 3e 62 2c 20 31 30 30 29 3b 0a 20 20 64 61  b->b, 100);.  da
38438 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
38439 26 73 62 2d 3e 62 2c 20 22 22 2c 20 31 29 3b 0a  &sb->b, "", 1);.
3843a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 72  }.static int str
3843b 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28  ingBufferLength(
3843c 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62  StringBuffer *sb
3843d 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 62 2d 3e  ){.  return sb->
3843e 62 2e 6e 44 61 74 61 2d 31 3b 0a 7d 0a 73 74 61  b.nData-1;.}.sta
3843f 74 69 63 20 63 68 61 72 20 2a 73 74 72 69 6e 67  tic char *string
38440 42 75 66 66 65 72 44 61 74 61 28 53 74 72 69 6e  BufferData(Strin
38441 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a 20 20  gBuffer *sb){.  
38442 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 70 44 61  return sb->b.pDa
38443 74 61 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  ta;.}..static vo
38444 69 64 20 6e 61 70 70 65 6e 64 28 53 74 72 69 6e  id nappend(Strin
38445 67 42 75 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e  gBuffer *sb, con
38446 73 74 20 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20  st char *zFrom, 
38447 69 6e 74 20 6e 46 72 6f 6d 29 7b 0a 20 20 61 73  int nFrom){.  as
38448 73 65 72 74 28 20 73 62 2d 3e 62 2e 6e 44 61 74  sert( sb->b.nDat
38449 61 3e 30 20 29 3b 0a 20 20 69 66 28 20 6e 46 72  a>0 );.  if( nFr
3844a 6f 6d 3e 30 20 29 7b 0a 20 20 20 20 73 62 2d 3e  om>0 ){.    sb->
3844b 62 2e 6e 44 61 74 61 2d 2d 3b 0a 20 20 20 20 64  b.nData--;.    d
3844c 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32  ataBufferAppend2
3844d 28 26 73 62 2d 3e 62 2c 20 7a 46 72 6f 6d 2c 20  (&sb->b, zFrom, 
3844e 6e 46 72 6f 6d 2c 20 22 22 2c 20 31 29 3b 0a 20  nFrom, "", 1);. 
3844f 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
38450 20 61 70 70 65 6e 64 28 53 74 72 69 6e 67 42 75   append(StringBu
38451 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20  ffer *sb, const 
38452 63 68 61 72 20 2a 7a 46 72 6f 6d 29 7b 0a 20 20  char *zFrom){.  
38453 6e 61 70 70 65 6e 64 28 73 62 2c 20 7a 46 72 6f  nappend(sb, zFro
38454 6d 2c 20 73 74 72 6c 65 6e 28 7a 46 72 6f 6d 29  m, strlen(zFrom)
38455 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  );.}..static int
38456 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70 61 63   endsInWhiteSpac
38457 65 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a  e(StringBuffer *
38458 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 74 72  p){.  return str
38459 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28  ingBufferLength(
3845a 70 29 3e 30 20 26 26 0a 20 20 20 20 73 61 66 65  p)>0 &&.    safe
3845b 5f 69 73 73 70 61 63 65 28 73 74 72 69 6e 67 42  _isspace(stringB
3845c 75 66 66 65 72 44 61 74 61 28 70 29 5b 73 74 72  ufferData(p)[str
3845d 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28  ingBufferLength(
3845e 70 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f 2a 20 49 66  p)-1]);.}../* If
3845f 20 74 68 65 20 53 74 72 69 6e 67 42 75 66 66 65   the StringBuffe
38460 72 20 65 6e 64 73 20 69 6e 20 73 6f 6d 65 74 68  r ends in someth
38461 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 77  ing other than w
38462 68 69 74 65 20 73 70 61 63 65 2c 20 61 64 64 20  hite space, add 
38463 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 73 70 61 63  a.** single spac
38464 65 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 74  e character to t
38465 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69  he end..*/.stati
38466 63 20 76 6f 69 64 20 61 70 70 65 6e 64 57 68 69  c void appendWhi
38467 74 65 53 70 61 63 65 28 53 74 72 69 6e 67 42 75  teSpace(StringBu
38468 66 66 65 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  ffer *p){.  if( 
38469 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67  stringBufferLeng
3846a 74 68 28 70 29 3d 3d 30 20 29 20 72 65 74 75 72  th(p)==0 ) retur
3846b 6e 3b 0a 20 20 69 66 28 20 21 65 6e 64 73 49 6e  n;.  if( !endsIn
3846c 57 68 69 74 65 53 70 61 63 65 28 70 29 20 29 20  WhiteSpace(p) ) 
3846d 61 70 70 65 6e 64 28 70 2c 20 22 20 22 29 3b 0a  append(p, " ");.
3846e 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 77 68 69  }../* Remove whi
3846f 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68  te space from th
38470 65 20 65 6e 64 20 6f 66 20 74 68 65 20 53 74 72  e end of the Str
38471 69 6e 67 42 75 66 66 65 72 20 2a 2f 0a 73 74 61  ingBuffer */.sta
38472 74 69 63 20 76 6f 69 64 20 74 72 69 6d 57 68 69  tic void trimWhi
38473 74 65 53 70 61 63 65 28 53 74 72 69 6e 67 42 75  teSpace(StringBu
38474 66 66 65 72 20 2a 70 29 7b 0a 20 20 77 68 69 6c  ffer *p){.  whil
38475 65 28 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70  e( endsInWhiteSp
38476 61 63 65 28 70 29 20 29 7b 0a 20 20 20 20 70 2d  ace(p) ){.    p-
38477 3e 62 2e 70 44 61 74 61 5b 2d 2d 70 2d 3e 62 2e  >b.pData[--p->b.
38478 6e 44 61 74 61 2d 31 5d 20 3d 20 27 5c 30 27 3b  nData-1] = '\0';
38479 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 0a 2a 2a 20  .  }.}.../* .** 
3847a 52 65 6c 65 61 73 65 20 61 6c 6c 20 6d 65 6d 6f  Release all memo
3847b 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
3847c 74 68 20 74 68 65 20 53 6e 69 70 70 65 74 20 73  th the Snippet s
3847d 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20  tructure passed 
3847e 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d 65 6e  as.** an argumen
3847f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
38480 64 20 66 74 73 33 53 6e 69 70 70 65 74 46 72 65  d fts3SnippetFre
38481 65 28 53 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20  e(Snippet *p){. 
38482 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d   sqlite3_free(p-
38483 3e 61 4d 61 74 63 68 29 3b 0a 20 20 73 71 6c 69  >aMatch);.  sqli
38484 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 4f 66 66  te3_free(p->zOff
38485 73 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  set);.  sqlite3_
38486 66 72 65 65 28 70 2d 3e 7a 53 6e 69 70 70 65 74  free(p->zSnippet
38487 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
38488 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  e(p);.}../*.** A
38489 70 70 65 6e 64 20 61 20 73 69 6e 67 6c 65 20 65  ppend a single e
3848a 6e 74 72 79 20 74 6f 20 74 68 65 20 70 2d 3e 61  ntry to the p->a
3848b 4d 61 74 63 68 5b 5d 20 6c 6f 67 2e 0a 2a 2f 0a  Match[] log..*/.
3848c 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70  static void snip
3848d 70 65 74 41 70 70 65 6e 64 4d 61 74 63 68 28 0a  petAppendMatch(.
3848e 20 20 53 6e 69 70 70 65 74 20 2a 70 2c 20 20 20    Snippet *p,   
3848f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
38490 70 70 65 6e 64 20 74 68 65 20 65 6e 74 72 79 20  ppend the entry 
38491 74 6f 20 74 68 69 73 20 73 6e 69 70 70 65 74 20  to this snippet 
38492 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 69  */.  int iCol, i
38493 6e 74 20 69 54 65 72 6d 2c 20 20 20 20 20 20 2f  nt iTerm,      /
38494 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64  * The column and
38495 20 71 75 65 72 79 20 74 65 72 6d 20 2a 2f 0a 20   query term */. 
38496 20 69 6e 74 20 69 54 6f 6b 65 6e 2c 20 20 20 20   int iToken,    
38497 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
38498 74 63 68 69 6e 67 20 74 6f 6b 65 6e 20 69 6e 20  tching token in 
38499 64 6f 63 75 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e  document */.  in
3849a 74 20 69 53 74 61 72 74 2c 20 69 6e 74 20 6e 42  t iStart, int nB
3849b 79 74 65 20 20 20 20 20 2f 2a 20 4f 66 66 73 65  yte     /* Offse
3849c 74 20 61 6e 64 20 73 69 7a 65 20 6f 66 20 74 68  t and size of th
3849d 65 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20  e match */.){.  
3849e 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
3849f 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 4d  snippetMatch *pM
384a0 61 74 63 68 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  atch;.  if( p->n
384a1 4d 61 74 63 68 2b 31 3e 3d 70 2d 3e 6e 41 6c 6c  Match+1>=p->nAll
384a2 6f 63 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 41 6c  oc ){.    p->nAl
384a3 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a  loc = p->nAlloc*
384a4 32 20 2b 20 31 30 3b 0a 20 20 20 20 70 2d 3e 61  2 + 10;.    p->a
384a5 4d 61 74 63 68 20 3d 20 73 71 6c 69 74 65 33 5f  Match = sqlite3_
384a6 72 65 61 6c 6c 6f 63 28 70 2d 3e 61 4d 61 74 63  realloc(p->aMatc
384a7 68 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 73 69 7a  h, p->nAlloc*siz
384a8 65 6f 66 28 70 2d 3e 61 4d 61 74 63 68 5b 30 5d  eof(p->aMatch[0]
384a9 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ) );.    if( p->
384aa 61 4d 61 74 63 68 3d 3d 30 20 29 7b 0a 20 20 20  aMatch==0 ){.   
384ab 20 20 20 70 2d 3e 6e 4d 61 74 63 68 20 3d 20 30     p->nMatch = 0
384ac 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f  ;.      p->nAllo
384ad 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74  c = 0;.      ret
384ae 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
384af 20 69 20 3d 20 70 2d 3e 6e 4d 61 74 63 68 2b 2b   i = p->nMatch++
384b0 3b 0a 20 20 70 4d 61 74 63 68 20 3d 20 26 70 2d  ;.  pMatch = &p-
384b1 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 70 4d  >aMatch[i];.  pM
384b2 61 74 63 68 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f  atch->iCol = iCo
384b3 6c 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69 54 65  l;.  pMatch->iTe
384b4 72 6d 20 3d 20 69 54 65 72 6d 3b 0a 20 20 70 4d  rm = iTerm;.  pM
384b5 61 74 63 68 2d 3e 69 54 6f 6b 65 6e 20 3d 20 69  atch->iToken = i
384b6 54 6f 6b 65 6e 3b 0a 20 20 70 4d 61 74 63 68 2d  Token;.  pMatch-
384b7 3e 69 53 74 61 72 74 20 3d 20 69 53 74 61 72 74  >iStart = iStart
384b8 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 6e 42 79 74  ;.  pMatch->nByt
384b9 65 20 3d 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a  e = nByte;.}../*
384ba 0a 2a 2a 20 53 69 7a 69 6e 67 20 69 6e 66 6f 72  .** Sizing infor
384bb 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 63  mation for the c
384bc 69 72 63 75 6c 61 72 20 62 75 66 66 65 72 20 75  ircular buffer u
384bd 73 65 64 20 69 6e 20 73 6e 69 70 70 65 74 4f 66  sed in snippetOf
384be 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 29 0a  fsetsOfColumn().
384bf 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f  */.#define FTS3_
384c0 52 4f 54 4f 52 5f 53 5a 20 20 20 28 33 32 29 0a  ROTOR_SZ   (32).
384c1 23 64 65 66 69 6e 65 20 46 54 53 33 5f 52 4f 54  #define FTS3_ROT
384c2 4f 52 5f 4d 41 53 4b 20 28 46 54 53 33 5f 52 4f  OR_MASK (FTS3_RO
384c3 54 4f 52 5f 53 5a 2d 31 29 0a 0a 2f 2a 0a 2a 2a  TOR_SZ-1)../*.**
384c4 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69 74 65   Function to ite
384c5 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65  rate through the
384c6 20 74 6f 6b 65 6e 73 20 6f 66 20 61 20 63 6f 6d   tokens of a com
384c7 70 69 6c 65 64 20 65 78 70 72 65 73 73 69 6f 6e  piled expression
384c8 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20  ..**.** Except, 
384c9 73 6b 69 70 20 61 6c 6c 20 74 6f 6b 65 6e 73 20  skip all tokens 
384ca 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  on the right-han
384cb 64 20 73 69 64 65 20 6f 66 20 61 20 4e 4f 54 20  d side of a NOT 
384cc 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69  operator..** Thi
384cd 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
384ce 65 64 20 74 6f 20 66 69 6e 64 20 74 6f 6b 65 6e  ed to find token
384cf 73 20 61 73 20 70 61 72 74 20 6f 66 20 73 6e 69  s as part of sni
384d0 70 70 65 74 20 61 6e 64 20 6f 66 66 73 65 74 0a  ppet and offset.
384d1 2a 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20 61 6e  ** generation an
384d2 64 20 77 65 20 64 6f 20 6e 74 20 77 61 6e 74 20  d we do nt want 
384d3 73 6e 69 70 70 65 74 73 20 61 6e 64 20 6f 66 66  snippets and off
384d4 73 65 74 73 20 74 6f 20 72 65 70 6f 72 74 20 6d  sets to report m
384d5 61 74 63 68 65 73 0a 2a 2a 20 66 6f 72 20 74 6f  atches.** for to
384d6 6b 65 6e 73 20 6f 6e 20 74 68 65 20 52 48 53 20  kens on the RHS 
384d7 6f 66 20 61 20 4e 4f 54 2e 0a 2a 2f 0a 73 74 61  of a NOT..*/.sta
384d8 74 69 63 20 69 6e 74 20 66 74 73 33 4e 65 78 74  tic int fts3Next
384d9 45 78 70 72 54 6f 6b 65 6e 28 46 74 73 33 45 78  ExprToken(Fts3Ex
384da 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 69 6e 74  pr **ppExpr, int
384db 20 2a 70 69 54 6f 6b 65 6e 29 7b 0a 20 20 46 74   *piToken){.  Ft
384dc 73 33 45 78 70 72 20 2a 70 20 3d 20 2a 70 70 45  s3Expr *p = *ppE
384dd 78 70 72 3b 0a 20 20 69 6e 74 20 69 54 6f 6b 65  xpr;.  int iToke
384de 6e 20 3d 20 2a 70 69 54 6f 6b 65 6e 3b 0a 20 20  n = *piToken;.  
384df 69 66 28 20 69 54 6f 6b 65 6e 3c 30 20 29 7b 0a  if( iToken<0 ){.
384e0 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63      /* In this c
384e1 61 73 65 20 74 68 65 20 65 78 70 72 65 73 73 69  ase the expressi
384e2 6f 6e 20 70 20 69 73 20 74 68 65 20 72 6f 6f 74  on p is the root
384e3 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   of an expressio
384e4 6e 20 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20 4d  n tree..    ** M
384e5 6f 76 65 20 74 6f 20 74 68 65 20 66 69 72 73 74  ove to the first
384e6 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78   token in the ex
384e7 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 20  pression tree.. 
384e8 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28     */.    while(
384e9 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20   p->pLeft ){.   
384ea 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b     p = p->pLeft;
384eb 0a 20 20 20 20 7d 0a 20 20 20 20 69 54 6f 6b 65  .    }.    iToke
384ec 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  n = 0;.  }else{.
384ed 20 20 20 20 61 73 73 65 72 74 28 70 20 26 26 20      assert(p && 
384ee 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
384ef 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 20  RY_PHRASE );.   
384f0 20 69 66 28 20 69 54 6f 6b 65 6e 3c 28 70 2d 3e   if( iToken<(p->
384f1 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 2d  pPhrase->nToken-
384f2 31 29 20 29 7b 0a 20 20 20 20 20 20 69 54 6f 6b  1) ){.      iTok
384f3 65 6e 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  en++;.    }else{
384f4 0a 20 20 20 20 20 20 69 54 6f 6b 65 6e 20 3d 20  .      iToken = 
384f5 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  0;.      while( 
384f6 70 2d 3e 70 50 61 72 65 6e 74 20 26 26 20 70 2d  p->pParent && p-
384f7 3e 70 50 61 72 65 6e 74 2d 3e 70 4c 65 66 74 21  >pParent->pLeft!
384f8 3d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  =p ){.        as
384f9 73 65 72 74 28 20 70 2d 3e 70 50 61 72 65 6e 74  sert( p->pParent
384fa 2d 3e 70 52 69 67 68 74 3d 3d 70 20 29 3b 0a 20  ->pRight==p );. 
384fb 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 50         p = p->pP
384fc 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20  arent;.      }. 
384fd 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 61 72       p = p->pPar
384fe 65 6e 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ent;.      if( p
384ff 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
38500 72 74 28 20 70 2d 3e 70 52 69 67 68 74 21 3d 30  rt( p->pRight!=0
38501 20 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20   );.        p = 
38502 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20  p->pRight;.     
38503 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4c 65     while( p->pLe
38504 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
38505 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20  p = p->pLeft;.  
38506 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
38507 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70      }.  }..  *pp
38508 45 78 70 72 20 3d 20 70 3b 0a 20 20 2a 70 69 54  Expr = p;.  *piT
38509 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b 0a 20  oken = iToken;. 
3850a 20 72 65 74 75 72 6e 20 70 3f 31 3a 30 3b 0a 7d   return p?1:0;.}
3850b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
3850c 52 55 45 20 69 66 20 74 68 65 20 65 78 70 72 65  RUE if the expre
3850d 73 73 69 6f 6e 20 6e 6f 64 65 20 70 45 78 70 72  ssion node pExpr
3850e 20 69 73 20 6c 6f 63 61 74 65 64 20 62 65 6e 65   is located bene
3850f 61 74 68 20 74 68 65 0a 2a 2a 20 52 48 53 20 6f  ath the.** RHS o
38510 66 20 61 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72  f a NOT operator
38511 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
38512 66 74 73 33 45 78 70 72 42 65 6e 65 61 74 68 4e  fts3ExprBeneathN
38513 6f 74 28 46 74 73 33 45 78 70 72 20 2a 70 29 7b  ot(Fts3Expr *p){
38514 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 50 61  .  Fts3Expr *pPa
38515 72 65 6e 74 3b 0a 20 20 77 68 69 6c 65 28 20 70  rent;.  while( p
38516 20 29 7b 0a 20 20 20 20 70 50 61 72 65 6e 74 20   ){.    pParent 
38517 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  = p->pParent;.  
38518 20 20 69 66 28 20 70 50 61 72 65 6e 74 20 26 26    if( pParent &&
38519 20 70 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d   pParent->eType=
3851a 3d 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 26 26  =FTSQUERY_NOT &&
3851b 20 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68 74   pParent->pRight
3851c 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==p ){.      ret
3851d 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
3851e 20 70 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20   p = pParent;.  
3851f 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
38520 0a 2f 2a 0a 2a 2a 20 41 64 64 20 65 6e 74 72 69  ./*.** Add entri
38521 65 73 20 74 6f 20 70 53 6e 69 70 70 65 74 2d 3e  es to pSnippet->
38522 61 4d 61 74 63 68 5b 5d 20 66 6f 72 20 65 76 65  aMatch[] for eve
38523 72 79 20 6d 61 74 63 68 20 74 68 61 74 20 6f 63  ry match that oc
38524 63 75 72 73 20 61 67 61 69 6e 73 74 0a 2a 2a 20  curs against.** 
38525 64 6f 63 75 6d 65 6e 74 20 7a 44 6f 63 5b 30 2e  document zDoc[0.
38526 2e 6e 44 6f 63 2d 31 5d 20 77 68 69 63 68 20 69  .nDoc-1] which i
38527 73 20 73 74 6f 72 65 64 20 69 6e 20 63 6f 6c 75  s stored in colu
38528 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73  mn iColumn..*/.s
38529 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70  tatic void snipp
3852a 65 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d  etOffsetsOfColum
3852b 6e 28 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  n(.  Fts3Cursor 
3852c 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 2f  *pCur,         /
3852d 2a 20 54 68 65 20 66 75 6c 6c 74 65 73 74 20 73  * The fulltest s
3852e 65 61 72 63 68 20 63 75 72 73 6f 72 20 2a 2f 0a  earch cursor */.
3852f 20 20 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70    Snippet *pSnip
38530 70 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  pet,            
38531 20 2f 2a 20 54 68 65 20 53 6e 69 70 70 65 74 20   /* The Snippet 
38532 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 66 69 6c  object to be fil
38533 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  led in */.  int 
38534 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20  iColumn,        
38535 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
38536 64 65 78 20 6f 66 20 66 75 6c 6c 74 65 78 74 20  dex of fulltext 
38537 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  table column */.
38538 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
38539 6f 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  oc,             
3853a 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
3853b 66 75 6c 6c 74 65 78 74 20 74 61 62 6c 65 20 63  fulltext table c
3853c 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  olumn */.  int n
3853d 44 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 20  Doc             
3853e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
3853f 67 74 68 20 6f 66 20 7a 44 6f 63 20 69 6e 20 62  gth of zDoc in b
38540 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e  ytes */.){.  con
38541 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
38542 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 54 4d  izer_module *pTM
38543 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74  odule;  /* The t
38544 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20  okenizer module 
38545 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
38546 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
38547 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
38548 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20  /* The specific 
38549 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 73  tokenizer */.  s
3854a 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3854b 5f 63 75 72 73 6f 72 20 2a 70 54 43 75 72 73 6f  _cursor *pTCurso
3854c 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b  r;        /* Tok
3854d 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f  enizer cursor */
3854e 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 56  .  Fts3Table *pV
3854f 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  tab;            
38550 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c 20      /* The full 
38551 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20  text index */.  
38552 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
38553 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38554 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
38555 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
38556 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
38557 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
38558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38559 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
3855a 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  s */.  int rc;  
3855b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3855c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
3855d 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
3855e 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 61 74  unsigned int mat
3855f 63 68 2c 20 70 72 65 76 4d 61 74 63 68 3b 20 20  ch, prevMatch;  
38560 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 73       /* Phrase s
38561 65 61 72 63 68 20 62 69 74 6d 61 73 6b 73 20 2a  earch bitmasks *
38562 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
38563 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  zToken;         
38564 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
38565 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20   token from the 
38566 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 69  tokenizer */.  i
38567 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20  nt nToken;      
38568 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38569 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a      /* Size of z
3856a 54 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  Token */.  int i
3856b 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f  Begin, iEnd, iPo
3856c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
3856d 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20 62 65  /* Offsets of be
3856e 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20  ginning and end 
3856f 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c  */..  /* The fol
38570 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73  lowing variables
38571 20 6b 65 65 70 20 61 20 63 69 72 63 75 6c 61 72   keep a circular
38572 20 62 75 66 66 65 72 20 6f 66 20 74 68 65 20 6c   buffer of the l
38573 61 73 74 0a 20 20 2a 2a 20 66 65 77 20 74 6f 6b  ast.  ** few tok
38574 65 6e 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ens */.  unsigne
38575 64 20 69 6e 74 20 69 52 6f 74 6f 72 20 3d 20 30  d int iRotor = 0
38576 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
38577 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e   Index of curren
38578 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  t token */.  int
38579 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 46 54 53   iRotorBegin[FTS
3857a 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20 20 20  3_ROTOR_SZ];    
3857b 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f    /* Beginning o
3857c 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  ffset of token *
3857d 2f 0a 20 20 69 6e 74 20 69 52 6f 74 6f 72 4c 65  /.  int iRotorLe
3857e 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d  n[FTS3_ROTOR_SZ]
3857f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67  ;        /* Leng
38580 74 68 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 0a  th of token */..
38581 20 20 70 56 74 61 62 20 3d 20 20 28 46 74 73 33    pVtab =  (Fts3
38582 54 61 62 6c 65 20 2a 29 70 43 75 72 2d 3e 62 61  Table *)pCur->ba
38583 73 65 2e 70 56 74 61 62 3b 0a 20 20 6e 43 6f 6c  se.pVtab;.  nCol
38584 75 6d 6e 20 3d 20 70 56 74 61 62 2d 3e 6e 43 6f  umn = pVtab->nCo
38585 6c 75 6d 6e 3b 0a 20 20 70 54 6f 6b 65 6e 69 7a  lumn;.  pTokeniz
38586 65 72 20 3d 20 70 56 74 61 62 2d 3e 70 54 6f 6b  er = pVtab->pTok
38587 65 6e 69 7a 65 72 3b 0a 20 20 70 54 4d 6f 64 75  enizer;.  pTModu
38588 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d  le = pTokenizer-
38589 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 72 63 20 3d  >pModule;.  rc =
3858a 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e   pTModule->xOpen
3858b 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 44 6f  (pTokenizer, zDo
3858c 63 2c 20 6e 44 6f 63 2c 20 26 70 54 43 75 72 73  c, nDoc, &pTCurs
3858d 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  or);.  if( rc ) 
3858e 72 65 74 75 72 6e 3b 0a 20 20 70 54 43 75 72 73  return;.  pTCurs
3858f 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  or->pTokenizer =
38590 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20   pTokenizer;..  
38591 70 72 65 76 4d 61 74 63 68 20 3d 20 30 3b 0a 20  prevMatch = 0;. 
38592 20 77 68 69 6c 65 28 20 21 70 54 4d 6f 64 75 6c   while( !pTModul
38593 65 2d 3e 78 4e 65 78 74 28 70 54 43 75 72 73 6f  e->xNext(pTCurso
38594 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f  r, &zToken, &nTo
38595 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c 20 26 69  ken, &iBegin, &i
38596 45 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20  End, &iPos) ){. 
38597 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 49 74     Fts3Expr *pIt
38598 65 72 20 3d 20 70 43 75 72 2d 3e 70 45 78 70 72  er = pCur->pExpr
38599 3b 0a 20 20 20 20 69 6e 74 20 69 49 74 65 72 20  ;.    int iIter 
3859a 3d 20 2d 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72  = -1;.    iRotor
3859b 42 65 67 69 6e 5b 69 52 6f 74 6f 72 26 46 54 53  Begin[iRotor&FTS
3859c 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20  3_ROTOR_MASK] = 
3859d 69 42 65 67 69 6e 3b 0a 20 20 20 20 69 52 6f 74  iBegin;.    iRot
3859e 6f 72 4c 65 6e 5b 69 52 6f 74 6f 72 26 46 54 53  orLen[iRotor&FTS
3859f 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20  3_ROTOR_MASK] = 
385a0 69 45 6e 64 2d 69 42 65 67 69 6e 3b 0a 20 20 20  iEnd-iBegin;.   
385a1 20 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20   match = 0;.    
385a2 66 6f 72 28 69 3d 30 3b 20 69 3c 28 46 54 53 33  for(i=0; i<(FTS3
385a3 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 20 26 26 20  _ROTOR_SZ-1) && 
385a4 66 74 73 33 4e 65 78 74 45 78 70 72 54 6f 6b 65  fts3NextExprToke
385a5 6e 28 26 70 49 74 65 72 2c 20 26 69 49 74 65 72  n(&pIter, &iIter
385a6 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ); i++){.      i
385a7 6e 74 20 6e 50 68 72 61 73 65 3b 20 20 20 20 20  nt nPhrase;     
385a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
385a9 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  * Number of toke
385aa 6e 73 20 69 6e 20 63 75 72 72 65 6e 74 20 70 68  ns in current ph
385ab 72 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 73 74  rase */.      st
385ac 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e  ruct PhraseToken
385ad 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20 2f 2a   *pToken;     /*
385ae 20 43 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a   Current token *
385af 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  /.      int iCol
385b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
385b1 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
385b2 6e 20 69 6e 64 65 78 20 2a 2f 0a 0a 20 20 20 20  n index */..    
385b3 20 20 69 66 28 20 66 74 73 33 45 78 70 72 42 65    if( fts3ExprBe
385b4 6e 65 61 74 68 4e 6f 74 28 70 49 74 65 72 29 20  neathNot(pIter) 
385b5 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
385b6 20 20 6e 50 68 72 61 73 65 20 3d 20 70 49 74 65    nPhrase = pIte
385b7 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  r->pPhrase->nTok
385b8 65 6e 3b 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e  en;.      pToken
385b9 20 3d 20 26 70 49 74 65 72 2d 3e 70 50 68 72 61   = &pIter->pPhra
385ba 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 49 74 65 72  se->aToken[iIter
385bb 5d 3b 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20  ];.      iCol = 
385bc 70 49 74 65 72 2d 3e 70 50 68 72 61 73 65 2d 3e  pIter->pPhrase->
385bd 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69  iColumn;.      i
385be 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  f( iCol>=0 && iC
385bf 6f 6c 3c 6e 43 6f 6c 75 6d 6e 20 26 26 20 69 43  ol<nColumn && iC
385c0 6f 6c 21 3d 69 43 6f 6c 75 6d 6e 20 29 20 63 6f  ol!=iColumn ) co
385c1 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66  ntinue;.      if
385c2 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 6e 54 6f 6b  ( pToken->n>nTok
385c3 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  en ) continue;. 
385c4 20 20 20 20 20 69 66 28 20 21 70 54 6f 6b 65 6e       if( !pToken
385c5 2d 3e 69 73 50 72 65 66 69 78 20 26 26 20 70 54  ->isPrefix && pT
385c6 6f 6b 65 6e 2d 3e 6e 3c 6e 54 6f 6b 65 6e 20 29  oken->n<nToken )
385c7 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
385c8 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d   assert( pToken-
385c9 3e 6e 3c 3d 6e 54 6f 6b 65 6e 20 29 3b 0a 20 20  >n<=nToken );.  
385ca 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70      if( memcmp(p
385cb 54 6f 6b 65 6e 2d 3e 7a 2c 20 7a 54 6f 6b 65 6e  Token->z, zToken
385cc 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 20 29 20 63  , pToken->n) ) c
385cd 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69  ontinue;.      i
385ce 66 28 20 69 49 74 65 72 3e 30 20 26 26 20 28 70  f( iIter>0 && (p
385cf 72 65 76 4d 61 74 63 68 20 26 20 28 31 3c 3c 69  revMatch & (1<<i
385d0 29 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  ))==0 ) continue
385d1 3b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 7c 3d  ;.      match |=
385d2 20 31 3c 3c 69 3b 0a 20 20 20 20 20 20 69 66 28   1<<i;.      if(
385d3 20 69 3d 3d 28 46 54 53 33 5f 52 4f 54 4f 52 5f   i==(FTS3_ROTOR_
385d4 53 5a 2d 32 29 20 7c 7c 20 6e 50 68 72 61 73 65  SZ-2) || nPhrase
385d5 3d 3d 69 49 74 65 72 2b 31 20 29 7b 0a 20 20 20  ==iIter+1 ){.   
385d6 20 20 20 20 20 66 6f 72 28 6a 3d 6e 50 68 72 61       for(j=nPhra
385d7 73 65 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 29  se-1; j>=0; j--)
385d8 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
385d9 6b 20 3d 20 28 69 52 6f 74 6f 72 2d 6a 29 20 26  k = (iRotor-j) &
385da 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b   FTS3_ROTOR_MASK
385db 3b 0a 20 20 20 20 20 20 20 20 20 20 73 6e 69 70  ;.          snip
385dc 70 65 74 41 70 70 65 6e 64 4d 61 74 63 68 28 70  petAppendMatch(p
385dd 53 6e 69 70 70 65 74 2c 20 69 43 6f 6c 75 6d 6e  Snippet, iColumn
385de 2c 20 69 2d 6a 2c 20 69 50 6f 73 2d 6a 2c 0a 20  , i-j, iPos-j,. 
385df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
385e0 52 6f 74 6f 72 42 65 67 69 6e 5b 6b 5d 2c 20 69  RotorBegin[k], i
385e1 52 6f 74 6f 72 4c 65 6e 5b 6b 5d 29 3b 0a 20 20  RotorLen[k]);.  
385e2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
385e3 20 20 20 20 7d 0a 20 20 20 20 70 72 65 76 4d 61      }.    prevMa
385e4 74 63 68 20 3d 20 6d 61 74 63 68 3c 3c 31 3b 0a  tch = match<<1;.
385e5 20 20 20 20 69 52 6f 74 6f 72 2b 2b 3b 0a 20 20      iRotor++;.  
385e6 7d 0a 20 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 43  }.  pTModule->xC
385e7 6c 6f 73 65 28 70 54 43 75 72 73 6f 72 29 3b 20  lose(pTCursor); 
385e8 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76   .}../*.** Remov
385e9 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74  e entries from t
385ea 68 65 20 70 53 6e 69 70 70 65 74 20 73 74 72 75  he pSnippet stru
385eb 63 74 75 72 65 20 74 6f 20 61 63 63 6f 75 6e 74  cture to account
385ec 20 66 6f 72 20 74 68 65 20 4e 45 41 52 0a 2a 2a   for the NEAR.**
385ed 20 6f 70 65 72 61 74 6f 72 2e 20 57 68 65 6e 20   operator. When 
385ee 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20  this is called, 
385ef 70 53 6e 69 70 70 65 74 20 63 6f 6e 74 61 69 6e  pSnippet contain
385f0 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 74 6f  s the list of to
385f1 6b 65 6e 20 0a 2a 2a 20 6f 66 66 73 65 74 73 20  ken .** offsets 
385f2 70 72 6f 64 75 63 65 64 20 62 79 20 74 72 65 61  produced by trea
385f3 74 69 6e 67 20 61 6c 6c 20 4e 45 41 52 20 6f 70  ting all NEAR op
385f4 65 72 61 74 6f 72 73 20 61 73 20 41 4e 44 20 6f  erators as AND o
385f5 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 20 54 68 69  perators..** Thi
385f6 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76  s function remov
385f7 65 73 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74  es any entries t
385f8 68 61 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  hat should not b
385f9 65 20 70 72 65 73 65 6e 74 20 61 66 74 65 72 0a  e present after.
385fa 2a 2a 20 61 63 63 6f 75 6e 74 69 6e 67 20 66 6f  ** accounting fo
385fb 72 20 74 68 65 20 4e 45 41 52 20 72 65 73 74 72  r the NEAR restr
385fc 69 63 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d  iction. For exam
385fd 70 6c 65 2c 20 69 66 20 74 68 65 20 71 75 65 72  ple, if the quer
385fe 69 65 64 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20  ied.** document 
385ff 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 41  is:.**.**     "A
38600 20 42 20 43 20 44 20 45 20 41 22 0a 2a 2a 0a 2a   B C D E A".**.*
38601 2a 20 61 6e 64 20 74 68 65 20 71 75 65 72 79 20  * and the query 
38602 69 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 41  is:.** .**     A
38603 20 4e 45 41 52 2f 30 20 45 0a 2a 2a 0a 2a 2a 20   NEAR/0 E.**.** 
38604 74 68 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66  then when this f
38605 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
38606 64 20 74 68 65 20 53 6e 69 70 70 65 74 20 63 6f  d the Snippet co
38607 6e 74 61 69 6e 73 20 74 6f 6b 65 6e 20 6f 66 66  ntains token off
38608 73 65 74 73 0a 2a 2a 20 30 2c 20 34 20 61 6e 64  sets.** 0, 4 and
38609 20 35 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f   5. This functio
3860a 6e 20 72 65 6d 6f 76 65 73 20 74 68 65 20 22 30  n removes the "0
3860b 22 20 65 6e 74 72 79 20 28 62 65 63 61 75 73 65  " entry (because
3860c 20 74 68 65 20 66 69 72 73 74 20 41 0a 2a 2a 20   the first A.** 
3860d 69 73 20 6e 6f 74 20 6e 65 61 72 20 65 6e 6f 75  is not near enou
3860e 67 68 20 74 6f 20 61 6e 20 45 29 2e 0a 2a 2a 0a  gh to an E)..**.
3860f 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e  ** When this fun
38610 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
38611 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74   the value point
38612 65 64 20 74 6f 20 62 79 20 70 61 72 61 6d 65 74  ed to by paramet
38613 65 72 20 70 69 4c 65 66 74 20 69 73 0a 2a 2a 20  er piLeft is.** 
38614 74 68 65 20 69 6e 74 65 67 65 72 20 69 64 20 6f  the integer id o
38615 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  f the left-most 
38616 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70  token in the exp
38617 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65 61  ression tree hea
38618 64 65 64 20 62 79 0a 2a 2a 20 70 45 78 70 72 2e  ded by.** pExpr.
38619 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
3861a 6e 63 72 65 6d 65 6e 74 73 20 2a 70 69 4c 65 66  ncrements *piLef
3861b 74 20 62 79 20 74 68 65 20 74 6f 74 61 6c 20 6e  t by the total n
3861c 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 0a  umber of tokens.
3861d 2a 2a 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  ** in the expres
3861e 73 69 6f 6e 20 74 72 65 65 20 68 65 61 64 65 64  sion tree headed
3861f 20 62 79 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a   by pExpr..**.**
38620 20 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79   Return 1 if any
38621 20 74 72 69 6d 6d 69 6e 67 20 6f 63 63 75 72 73   trimming occurs
38622 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e  .  Return 0 if n
38623 6f 20 74 72 69 6d 6d 69 6e 67 20 69 73 20 72 65  o trimming is re
38624 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  quired..*/.stati
38625 63 20 69 6e 74 20 74 72 69 6d 53 6e 69 70 70 65  c int trimSnippe
38626 74 4f 66 66 73 65 74 73 28 0a 20 20 46 74 73 33  tOffsets(.  Fts3
38627 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
38628 20 20 2f 2a 20 54 68 65 20 73 65 61 72 63 68 20    /* The search 
38629 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
3862a 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
3862b 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 73 65 74  t,    /* The set
3862c 20 6f 66 20 73 6e 69 70 70 65 74 20 6f 66 66 73   of snippet offs
3862d 65 74 73 20 74 6f 20 62 65 20 74 72 69 6d 6d 65  ets to be trimme
3862e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 4c 65  d */.  int *piLe
3862f 66 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ft           /* 
38630 49 6e 64 65 78 20 6f 66 20 6c 65 66 74 2d 6d 6f  Index of left-mo
38631 73 74 20 74 6f 6b 65 6e 20 69 6e 20 70 45 78 70  st token in pExp
38632 72 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45  r */.){.  if( pE
38633 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 74  xpr ){.    if( t
38634 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74  rimSnippetOffset
38635 73 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  s(pExpr->pLeft, 
38636 70 53 6e 69 70 70 65 74 2c 20 70 69 4c 65 66 74  pSnippet, piLeft
38637 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
38638 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  n 1;.    }..    
38639 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65  switch( pExpr->e
3863a 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61  Type ){.      ca
3863b 73 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41  se FTSQUERY_PHRA
3863c 53 45 3a 0a 20 20 20 20 20 20 20 20 2a 70 69 4c  SE:.        *piL
3863d 65 66 74 20 2b 3d 20 70 45 78 70 72 2d 3e 70 50  eft += pExpr->pP
3863e 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20  hrase->nToken;. 
3863f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
38640 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
38641 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20  Y_NEAR: {.      
38642 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68    /* The right-h
38643 61 6e 64 2d 73 69 64 65 20 6f 66 20 61 20 4e 45  and-side of a NE
38644 41 52 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61  AR operator is a
38645 6c 77 61 79 73 20 61 20 70 68 72 61 73 65 2e 20  lways a phrase. 
38646 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c  The.        ** l
38647 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 69 73  eft-hand-side is
38648 20 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65   either a phrase
38649 20 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f   or an expressio
3864a 6e 20 74 72 65 65 20 74 68 61 74 20 69 73 20 0a  n tree that is .
3864b 20 20 20 20 20 20 20 20 2a 2a 20 69 74 73 65 6c          ** itsel
3864c 66 20 68 65 61 64 65 64 20 62 79 20 61 20 4e 45  f headed by a NE
3864d 41 52 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 65  AR operator. The
3864e 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 69 74 69   following initi
3864f 61 6c 69 7a 61 74 69 6f 6e 73 0a 20 20 20 20 20  alizations.     
38650 20 20 20 2a 2a 20 73 65 74 20 6c 6f 63 61 6c 20     ** set local 
38651 76 61 72 69 61 62 6c 65 20 69 4c 65 66 74 20 74  variable iLeft t
38652 6f 20 74 68 65 20 74 6f 6b 65 6e 20 6e 75 6d 62  o the token numb
38653 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 6d  er of the left-m
38654 6f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ost.        ** t
38655 6f 6b 65 6e 20 69 6e 20 74 68 65 20 72 69 67 68  oken in the righ
38656 74 2d 68 61 6e 64 20 70 68 72 61 73 65 2c 20 61  t-hand phrase, a
38657 6e 64 20 69 52 69 67 68 74 20 74 6f 20 74 68 65  nd iRight to the
38658 20 72 69 67 68 74 20 6d 6f 73 74 0a 20 20 20 20   right most.    
38659 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20      ** token in 
3865a 74 68 65 20 73 61 6d 65 20 70 68 72 61 73 65 2e  the same phrase.
3865b 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
3865c 20 77 65 20 68 61 64 3a 0a 20 20 20 20 20 20 20   we had:.       
3865d 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   **.        **  
3865e 20 20 20 3c 63 6f 6c 3e 20 4d 41 54 43 48 20 27     <col> MATCH '
3865f 22 61 62 63 20 64 65 66 22 20 4e 45 41 52 2f 32  "abc def" NEAR/2
38660 20 22 67 68 69 20 6a 6b 6c 22 27 0a 20 20 20 20   "ghi jkl"'.    
38661 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
38662 2a 20 74 68 65 6e 20 69 4c 65 66 74 20 77 69 6c  * then iLeft wil
38663 6c 20 62 65 20 73 65 74 20 74 6f 20 32 20 28 74  l be set to 2 (t
38664 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 6f 66 20 67  oken number of g
38665 68 69 29 20 61 6e 64 20 6e 54 6f 6b 65 6e 20 77  hi) and nToken w
38666 69 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 62  ill.        ** b
38667 65 20 73 65 74 20 74 6f 20 34 2e 0a 20 20 20 20  e set to 4..    
38668 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 46      */.        F
38669 74 73 33 45 78 70 72 20 2a 70 4c 65 66 74 20 3d  ts3Expr *pLeft =
3866a 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
3866b 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20         Fts3Expr 
3866c 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  *pRight = pExpr-
3866d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  >pRight;.       
3866e 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 2a 70 69   int iLeft = *pi
3866f 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 6e  Left;.        in
38670 74 20 6e 4e 65 61 72 20 3d 20 70 45 78 70 72 2d  t nNear = pExpr-
38671 3e 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20 20 20  >nNear;.        
38672 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 70 52 69  int nToken = pRi
38673 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  ght->pPhrase->nT
38674 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e  oken;.        in
38675 74 20 6a 6a 2c 20 69 69 3b 0a 20 20 20 20 20 20  t jj, ii;.      
38676 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 65 54 79    if( pLeft->eTy
38677 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41  pe==FTSQUERY_NEA
38678 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  R ){.          p
38679 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 52  Left = pLeft->pR
3867a 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ight;.        }.
3867b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
3867c 70 52 69 67 68 74 2d 3e 65 54 79 70 65 3d 3d 46  pRight->eType==F
3867d 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
3867e 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
3867f 28 20 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d  ( pLeft->eType==
38680 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
38681 29 3b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65  );.        nToke
38682 6e 20 2b 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72  n += pLeft->pPhr
38683 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 0a 20 20  ase->nToken;..  
38684 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20        for(ii=0; 
38685 69 69 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61  ii<pSnippet->nMa
38686 74 63 68 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  tch; ii++){.    
38687 20 20 20 20 20 20 73 74 72 75 63 74 20 73 6e 69        struct sni
38688 70 70 65 74 4d 61 74 63 68 20 2a 70 20 3d 20 26  ppetMatch *p = &
38689 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
3868a 5b 69 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  [ii];.          
3868b 69 66 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 69 4c  if( p->iTerm==iL
3868c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  eft ){.         
3868d 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30 3b     int isOk = 0;
3868e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
3868f 53 6e 69 70 70 65 74 20 69 69 20 69 73 20 61 6e  Snippet ii is an
38690 20 6f 63 63 75 72 65 6e 63 65 20 6f 66 20 71 75   occurence of qu
38691 65 72 79 20 74 65 72 6d 20 69 4c 65 66 74 20 69  ery term iLeft i
38692 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e 0a  n the document..
38693 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49              ** I
38694 74 20 6f 63 63 75 72 73 20 61 74 20 70 6f 73 69  t occurs at posi
38695 74 69 6f 6e 20 28 70 2d 3e 69 54 6f 6b 65 6e 29  tion (p->iToken)
38696 20 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e 74   of the document
38697 2e 20 57 65 20 6e 6f 77 0a 20 20 20 20 20 20 20  . We now.       
38698 20 20 20 20 20 2a 2a 20 73 65 61 72 63 68 20 66       ** search f
38699 6f 72 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  or an instance o
3869a 66 20 74 6f 6b 65 6e 20 28 69 4c 65 66 74 2d 31  f token (iLeft-1
3869b 29 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74  ) somewhere in t
3869c 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  he .            
3869d 2a 2a 20 72 61 6e 67 65 20 28 70 2d 3e 69 54 6f  ** range (p->iTo
3869e 6b 65 6e 20 2d 20 6e 4e 65 61 72 29 2e 2e 2e 28  ken - nNear)...(
3869f 70 2d 3e 69 54 6f 6b 65 6e 20 2b 20 6e 4e 65 61  p->iToken + nNea
386a0 72 20 2b 20 6e 54 6f 6b 65 6e 29 20 77 69 74 68  r + nToken) with
386a1 69 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  in .            
386a2 2a 2a 20 74 68 65 20 73 65 74 20 6f 66 20 73 6e  ** the set of sn
386a3 69 70 70 65 74 4d 61 74 63 68 20 73 74 72 75 63  ippetMatch struc
386a4 74 75 72 65 73 2e 20 49 66 20 6f 6e 65 20 69 73  tures. If one is
386a5 20 66 6f 75 6e 64 2c 20 70 72 6f 63 65 65 64 2e   found, proceed.
386a6 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a   .            **
386a7 20 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62   If one cannot b
386a8 65 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 72 65  e found, then re
386a9 6d 6f 76 65 20 73 6e 69 70 70 65 74 73 20 69 69  move snippets ii
386aa 2e 2e 28 69 69 2b 4e 2d 31 29 20 0a 20 20 20 20  ..(ii+N-1) .    
386ab 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20          ** from 
386ac 74 68 65 20 6d 61 74 63 68 69 6e 67 20 73 6e 69  the matching sni
386ad 70 70 65 74 73 2c 20 77 68 65 72 65 20 4e 20 69  ppets, where N i
386ae 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
386af 74 6f 6b 65 6e 73 20 0a 20 20 20 20 20 20 20 20  tokens .        
386b0 20 20 20 20 2a 2a 20 69 6e 20 70 68 72 61 73 65      ** in phrase
386b1 20 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65   pRight->pPhrase
386b2 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  ..            */
386b3 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72  .            for
386b4 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20 26  (jj=0; isOk==0 &
386b5 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e 6e  & jj<pSnippet->n
386b6 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20 20  Match; jj++){.  
386b7 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72 75              stru
386b8 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20  ct snippetMatch 
386b9 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74 2d  *p2 = &pSnippet-
386ba 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20 20  >aMatch[jj];.   
386bb 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
386bc 32 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66 74  2->iTerm==(iLeft
386bd 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  -1) ){.         
386be 20 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69         if( p2->i
386bf 54 6f 6b 65 6e 3e 3d 28 70 2d 3e 69 54 6f 6b 65  Token>=(p->iToke
386c0 6e 2d 6e 4e 65 61 72 2d 31 29 20 0a 20 20 20 20  n-nNear-1) .    
386c1 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
386c2 70 32 2d 3e 69 54 6f 6b 65 6e 3c 28 70 2d 3e 69  p2->iToken<(p->i
386c3 54 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 6e 54 6f 6b  Token+nNear+nTok
386c4 65 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  en) .           
386c5 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
386c6 20 20 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d            isOk =
386c7 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
386c8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
386c9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
386ca 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
386cb 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20  if( !isOk ){.   
386cc 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b             int k
386cd 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
386ce 20 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 52   for(kk=0; kk<pR
386cf 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  ight->pPhrase->n
386d0 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20  Token; kk++){.  
386d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53                pS
386d2 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6b  nippet->aMatch[k
386d3 6b 2b 69 69 5d 2e 69 54 65 72 6d 20 3d 20 2d 32  k+ii].iTerm = -2
386d4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
386d5 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
386d6 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
386d7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
386d8 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
386d9 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65  ( p->iTerm==(iLe
386da 66 74 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  ft-1) ){.       
386db 20 20 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20       int isOk = 
386dc 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  0;.            f
386dd 6f 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30  or(jj=0; isOk==0
386de 20 26 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d   && jj<pSnippet-
386df 3e 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a  >nMatch; jj++){.
386e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
386e1 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
386e2 68 20 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65  h *p2 = &pSnippe
386e3 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20  t->aMatch[jj];. 
386e4 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
386e5 20 70 32 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65 66   p2->iTerm==iLef
386e6 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
386e7 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54 6f       if( p2->iTo
386e8 6b 65 6e 3c 3d 28 70 2d 3e 69 54 6f 6b 65 6e 2b  ken<=(p->iToken+
386e9 6e 4e 65 61 72 2b 31 29 20 0a 20 20 20 20 20 20  nNear+1) .      
386ea 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 32             && p2
386eb 2d 3e 69 54 6f 6b 65 6e 3e 28 70 2d 3e 69 54 6f  ->iToken>(p->iTo
386ec 6b 65 6e 2d 6e 4e 65 61 72 2d 6e 54 6f 6b 65 6e  ken-nNear-nToken
386ed 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ) .             
386ee 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
386ef 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20 31          isOk = 1
386f0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
386f1 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
386f2 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
386f3 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  }.            if
386f4 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20 20  ( !isOk ){.     
386f5 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b           int kk;
386f6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66  .              f
386f7 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 4c 65 66  or(kk=0; kk<pLef
386f8 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  t->pPhrase->nTok
386f9 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20  en; kk++){.     
386fa 20 20 20 20 20 20 20 20 20 20 20 70 53 6e 69 70             pSnip
386fb 70 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 2d 6b  pet->aMatch[ii-k
386fc 6b 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b 0a 20  k].iTerm = -2;. 
386fd 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
386fe 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
386ff 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20  urn 1;.         
38700 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
38701 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
38702 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
38703 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  }.    }..    if(
38704 20 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73   trimSnippetOffs
38705 65 74 73 28 70 45 78 70 72 2d 3e 70 52 69 67 68  ets(pExpr->pRigh
38706 74 2c 20 70 53 6e 69 70 70 65 74 2c 20 70 69 4c  t, pSnippet, piL
38707 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  eft) ){.      re
38708 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
38709 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
3870a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61  ./*.** Compute a
3870b 6c 6c 20 6f 66 66 73 65 74 73 20 66 6f 72 20 74  ll offsets for t
3870c 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f  he current row o
3870d 66 20 74 68 65 20 71 75 65 72 79 2e 20 20 0a 2a  f the query.  .*
3870e 2a 20 49 66 20 74 68 65 20 6f 66 66 73 65 74 73  * If the offsets
3870f 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
38710 65 6e 20 63 6f 6d 70 75 74 65 64 2c 20 74 68 69  en computed, thi
38711 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
38712 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
38713 69 6e 74 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66  int snippetAllOf
38714 66 73 65 74 73 28 46 74 73 33 43 75 72 73 6f 72  fsets(Fts3Cursor
38715 20 2a 70 43 73 72 2c 20 53 6e 69 70 70 65 74 20   *pCsr, Snippet 
38716 2a 2a 70 70 53 6e 69 70 70 65 74 29 7b 0a 20 20  **ppSnippet){.  
38717 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28  Fts3Table *p = (
38718 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72  Fts3Table *)pCsr
38719 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20  ->base.pVtab;.  
3871a 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69  int nColumn;.  i
3871b 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69 3b 0a 20  nt iColumn, i;. 
3871c 20 69 6e 74 20 69 46 69 72 73 74 2c 20 69 4c 61   int iFirst, iLa
3871d 73 74 3b 0a 20 20 69 6e 74 20 69 54 65 72 6d 20  st;.  int iTerm 
3871e 3d 20 30 3b 0a 20 20 53 6e 69 70 70 65 74 20 2a  = 0;.  Snippet *
3871f 70 53 6e 69 70 70 65 74 3b 0a 0a 20 20 69 66 28  pSnippet;..  if(
38720 20 70 43 73 72 2d 3e 70 45 78 70 72 3d 3d 30 20   pCsr->pExpr==0 
38721 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
38722 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
38723 70 53 6e 69 70 70 65 74 20 3d 20 28 53 6e 69 70  pSnippet = (Snip
38724 70 65 74 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  pet *)sqlite3_ma
38725 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 53 6e 69 70  lloc(sizeof(Snip
38726 70 65 74 29 29 3b 0a 20 20 2a 70 70 53 6e 69 70  pet));.  *ppSnip
38727 70 65 74 20 3d 20 70 53 6e 69 70 70 65 74 3b 0a  pet = pSnippet;.
38728 20 20 69 66 28 20 21 70 53 6e 69 70 70 65 74 20    if( !pSnippet 
38729 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3872a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3872b 20 20 6d 65 6d 73 65 74 28 70 53 6e 69 70 70 65    memset(pSnippe
3872c 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 53 6e 69  t, 0, sizeof(Sni
3872d 70 70 65 74 29 29 3b 0a 0a 20 20 6e 43 6f 6c 75  ppet));..  nColu
3872e 6d 6e 20 3d 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b  mn = p->nColumn;
3872f 0a 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 28 70 43  .  iColumn = (pC
38730 73 72 2d 3e 65 54 79 70 65 20 2d 20 32 29 3b 0a  sr->eType - 2);.
38731 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20    if( iColumn<0 
38732 7c 7c 20 69 43 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c  || iColumn>=nCol
38733 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 6f  umn ){.    /* Lo
38734 6f 6b 20 66 6f 72 20 6d 61 74 63 68 65 73 20 6f  ok for matches o
38735 76 65 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20  ver all columns 
38736 6f 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74  of the full-text
38737 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 46   index */.    iF
38738 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 4c  irst = 0;.    iL
38739 61 73 74 20 3d 20 6e 43 6f 6c 75 6d 6e 2d 31 3b  ast = nColumn-1;
3873a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
3873b 20 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74 63 68 65   Look for matche
3873c 73 20 69 6e 20 74 68 65 20 69 43 6f 6c 75 6d 6e  s in the iColumn
3873d 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  -th column of th
3873e 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2a 2f 0a  e index only */.
3873f 20 20 20 20 69 46 69 72 73 74 20 3d 20 69 43 6f      iFirst = iCo
38740 6c 75 6d 6e 3b 0a 20 20 20 20 69 4c 61 73 74 20  lumn;.    iLast 
38741 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20  = iColumn;.  }. 
38742 20 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20 69   for(i=iFirst; i
38743 3c 3d 69 4c 61 73 74 3b 20 69 2b 2b 29 7b 0a 20  <=iLast; i++){. 
38744 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
38745 44 6f 63 3b 0a 20 20 20 20 69 6e 74 20 6e 44 6f  Doc;.    int nDo
38746 63 3b 0a 20 20 20 20 7a 44 6f 63 20 3d 20 28 63  c;.    zDoc = (c
38747 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74  onst char*)sqlit
38748 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70  e3_column_text(p
38749 43 73 72 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29  Csr->pStmt, i+1)
3874a 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c  ;.    nDoc = sql
3874b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
3874c 73 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 69  s(pCsr->pStmt, i
3874d 2b 31 29 3b 0a 20 20 20 20 73 6e 69 70 70 65 74  +1);.    snippet
3874e 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28  OffsetsOfColumn(
3874f 70 43 73 72 2c 20 70 53 6e 69 70 70 65 74 2c 20  pCsr, pSnippet, 
38750 69 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 29 3b 0a  i, zDoc, nDoc);.
38751 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 74 72    }..  while( tr
38752 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73  imSnippetOffsets
38753 28 70 43 73 72 2d 3e 70 45 78 70 72 2c 20 70 53  (pCsr->pExpr, pS
38754 6e 69 70 70 65 74 2c 20 26 69 54 65 72 6d 29 20  nippet, &iTerm) 
38755 29 7b 0a 20 20 20 20 69 54 65 72 6d 20 3d 20 30  ){.    iTerm = 0
38756 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
38757 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
38758 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20  .** Convert the 
38759 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74  information in t
3875a 68 65 20 61 4d 61 74 63 68 5b 5d 20 61 72 72 61  he aMatch[] arra
3875b 79 20 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74  y of the snippet
3875c 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 74 72  .** into the str
3875d 69 6e 67 20 7a 4f 66 66 73 65 74 5b 30 2e 2e 6e  ing zOffset[0..n
3875e 4f 66 66 73 65 74 2d 31 5d 2e 20 54 68 69 73 20  Offset-1]. This 
3875f 73 74 72 69 6e 67 20 69 73 20 75 73 65 64 20 61  string is used a
38760 73 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20  s.** the return 
38761 6f 66 20 74 68 65 20 53 51 4c 20 6f 66 66 73 65  of the SQL offse
38762 74 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ts() function..*
38763 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e  /.static void sn
38764 69 70 70 65 74 4f 66 66 73 65 74 54 65 78 74 28  ippetOffsetText(
38765 53 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20 20 69  Snippet *p){.  i
38766 6e 74 20 69 3b 0a 20 20 69 6e 74 20 63 6e 74 20  nt i;.  int cnt 
38767 3d 20 30 3b 0a 20 20 53 74 72 69 6e 67 42 75 66  = 0;.  StringBuf
38768 66 65 72 20 73 62 3b 0a 20 20 63 68 61 72 20 7a  fer sb;.  char z
38769 42 75 66 5b 32 30 30 5d 3b 0a 20 20 69 66 28 20  Buf[200];.  if( 
3876a 70 2d 3e 7a 4f 66 66 73 65 74 20 29 20 72 65 74  p->zOffset ) ret
3876b 75 72 6e 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e  urn;.  initStrin
3876c 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20  gBuffer(&sb);.  
3876d 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d  for(i=0; i<p->nM
3876e 61 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  atch; i++){.    
3876f 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61  struct snippetMa
38770 74 63 68 20 2a 70 4d 61 74 63 68 20 3d 20 26 70  tch *pMatch = &p
38771 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 20  ->aMatch[i];.   
38772 20 69 66 28 20 70 4d 61 74 63 68 2d 3e 69 54 65   if( pMatch->iTe
38773 72 6d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  rm>=0 ){.      /
38774 2a 20 49 66 20 73 6e 69 70 70 65 74 4d 61 74 63  * If snippetMatc
38775 68 2e 69 54 65 72 6d 20 69 73 20 6c 65 73 73 20  h.iTerm is less 
38776 74 68 61 6e 20 30 2c 20 74 68 65 6e 20 74 68 65  than 0, then the
38777 20 6d 61 74 63 68 20 77 61 73 20 0a 20 20 20 20   match was .    
38778 20 20 2a 2a 20 64 69 73 63 61 72 64 65 64 20 61    ** discarded a
38779 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73  s part of proces
3877a 73 69 6e 67 20 74 68 65 20 4e 45 41 52 20 6f 70  sing the NEAR op
3877b 65 72 61 74 6f 72 20 28 73 65 65 20 74 68 65 20  erator (see the 
3877c 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 6d 53 6e  .      ** trimSn
3877d 69 70 70 65 74 4f 66 66 73 65 74 73 46 6f 72 4e  ippetOffsetsForN
3877e 65 61 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ear() function f
3877f 6f 72 20 64 65 74 61 69 6c 73 29 2e 20 49 67 6e  or details). Ign
38780 6f 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 74  ore .      ** it
38781 20 69 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20   in this case.  
38782 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 7a 42 75      */.      zBu
38783 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20  f[0] = ' ';.    
38784 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
38785 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d  tf(sizeof(zBuf)-
38786 31 2c 20 26 7a 42 75 66 5b 63 6e 74 3e 30 5d 2c  1, &zBuf[cnt>0],
38787 20 22 25 64 20 25 64 20 25 64 20 25 64 22 2c 0a   "%d %d %d %d",.
38788 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
38789 2d 3e 69 43 6f 6c 2c 20 70 4d 61 74 63 68 2d 3e  ->iCol, pMatch->
3878a 69 54 65 72 6d 2c 20 70 4d 61 74 63 68 2d 3e 69  iTerm, pMatch->i
3878b 53 74 61 72 74 2c 20 70 4d 61 74 63 68 2d 3e 6e  Start, pMatch->n
3878c 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 70 70  Byte);.      app
3878d 65 6e 64 28 26 73 62 2c 20 7a 42 75 66 29 3b 0a  end(&sb, zBuf);.
3878e 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
3878f 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 7a 4f 66 66   }.  }.  p->zOff
38790 73 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66  set = stringBuff
38791 65 72 44 61 74 61 28 26 73 62 29 3b 0a 20 20 70  erData(&sb);.  p
38792 2d 3e 6e 4f 66 66 73 65 74 20 3d 20 73 74 72 69  ->nOffset = stri
38793 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 26  ngBufferLength(&
38794 73 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 7a 44  sb);.}../*.** zD
38795 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 69 73  oc[0..nDoc-1] is
38796 20 70 68 72 61 73 65 20 6f 66 20 74 65 78 74 2e   phrase of text.
38797 20 20 61 4d 61 74 63 68 5b 30 2e 2e 6e 4d 61 74    aMatch[0..nMat
38798 63 68 2d 31 5d 20 61 72 65 20 61 20 73 65 74 0a  ch-1] are a set.
38799 2a 2a 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 77  ** of matching w
3879a 6f 72 64 73 20 73 6f 6d 65 20 6f 66 20 77 68 69  ords some of whi
3879b 63 68 20 6d 69 67 68 74 20 62 65 20 69 6e 20 7a  ch might be in z
3879c 44 6f 63 2e 20 20 7a 44 6f 63 20 69 73 20 63 6f  Doc.  zDoc is co
3879d 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20 69  lumn.** number i
3879e 43 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 69 42 72 65 61  Col..**.** iBrea
3879f 6b 20 69 73 20 73 75 67 67 65 73 74 65 64 20 73  k is suggested s
387a0 70 6f 74 20 69 6e 20 7a 44 6f 63 20 77 68 65 72  pot in zDoc wher
387a1 65 20 77 65 20 63 6f 75 6c 64 20 62 65 67 69 6e  e we could begin
387a2 20 6f 72 20 65 6e 64 20 61 6e 0a 2a 2a 20 65 78   or end an.** ex
387a3 63 65 72 70 74 2e 20 20 52 65 74 75 72 6e 20 61  cerpt.  Return a
387a4 20 76 61 6c 75 65 20 73 69 6d 69 6c 61 72 20 74   value similar t
387a5 6f 20 69 42 72 65 61 6b 20 62 75 74 20 70 6f 73  o iBreak but pos
387a6 73 69 62 6c 79 20 61 64 6a 75 73 74 65 64 0a 2a  sibly adjusted.*
387a7 2a 20 74 6f 20 62 65 20 61 20 6c 69 74 74 6c 65  * to be a little
387a8 20 6c 65 66 74 20 6f 72 20 72 69 67 68 74 20 73   left or right s
387a9 6f 20 74 68 61 74 20 74 68 65 20 62 72 65 61 6b  o that the break
387aa 20 70 6f 69 6e 74 20 69 73 20 62 65 74 74 65 72   point is better
387ab 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
387ac 77 6f 72 64 42 6f 75 6e 64 61 72 79 28 0a 20 20  wordBoundary(.  
387ad 69 6e 74 20 69 42 72 65 61 6b 2c 20 20 20 20 20  int iBreak,     
387ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
387af 20 54 68 65 20 73 75 67 67 65 73 74 65 64 20 62   The suggested b
387b0 72 65 61 6b 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  reak point */.  
387b1 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63  const char *zDoc
387b2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
387b3 20 44 6f 63 75 6d 65 6e 74 20 74 65 78 74 20 2a   Document text *
387b4 2f 0a 20 20 69 6e 74 20 6e 44 6f 63 2c 20 20 20  /.  int nDoc,   
387b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
387b6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
387b7 79 74 65 73 20 69 6e 20 7a 44 6f 63 5b 5d 20 2a  ytes in zDoc[] *
387b8 2f 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70  /.  struct snipp
387b9 65 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 2c  etMatch *aMatch,
387ba 20 20 2f 2a 20 4d 61 74 63 68 69 6e 67 20 77 6f    /* Matching wo
387bb 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61  rds */.  int nMa
387bc 74 63 68 2c 20 20 20 20 20 20 20 20 20 20 20 20  tch,            
387bd 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
387be 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
387bf 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74  Match[] */.  int
387c0 20 69 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20   iCol           
387c1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
387c2 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  e column number 
387c3 66 6f 72 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 29 7b  for zDoc[] */.){
387c4 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
387c5 69 42 72 65 61 6b 3c 3d 31 30 20 29 7b 0a 20 20  iBreak<=10 ){.  
387c6 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
387c7 20 20 69 66 28 20 69 42 72 65 61 6b 3e 3d 6e 44    if( iBreak>=nD
387c8 6f 63 2d 31 30 20 29 7b 0a 20 20 20 20 72 65 74  oc-10 ){.    ret
387c9 75 72 6e 20 6e 44 6f 63 3b 0a 20 20 7d 0a 20 20  urn nDoc;.  }.  
387ca 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63  for(i=0; i<nMatc
387cb 68 20 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e 69  h && aMatch[i].i
387cc 43 6f 6c 3c 69 43 6f 6c 3b 20 69 2b 2b 29 7b 7d  Col<iCol; i++){}
387cd 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4d 61 74  .  while( i<nMat
387ce 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e  ch && aMatch[i].
387cf 69 53 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 5d  iStart+aMatch[i]
387d0 2e 6e 42 79 74 65 3c 69 42 72 65 61 6b 20 29 7b  .nByte<iBreak ){
387d1 20 69 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 69 3c   i++; }.  if( i<
387d2 6e 4d 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66  nMatch ){.    if
387d3 28 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  ( aMatch[i].iSta
387d4 72 74 3c 69 42 72 65 61 6b 2b 31 30 20 29 7b 0a  rt<iBreak+10 ){.
387d5 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61        return aMa
387d6 74 63 68 5b 69 5d 2e 69 53 74 61 72 74 3b 0a 20  tch[i].iStart;. 
387d7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 30     }.    if( i>0
387d8 20 26 26 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e   && aMatch[i-1].
387d9 69 53 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 2d  iStart+aMatch[i-
387da 31 5d 2e 6e 42 79 74 65 3e 3d 69 42 72 65 61 6b  1].nByte>=iBreak
387db 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
387dc 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 69 53 74   aMatch[i-1].iSt
387dd 61 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  art;.    }.  }. 
387de 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 31 30 3b   for(i=1; i<=10;
387df 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   i++){.    if( s
387e0 61 66 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63  afe_isspace(zDoc
387e1 5b 69 42 72 65 61 6b 2d 69 5d 29 20 29 7b 0a 20  [iBreak-i]) ){. 
387e2 20 20 20 20 20 72 65 74 75 72 6e 20 69 42 72 65       return iBre
387e3 61 6b 20 2d 20 69 20 2b 20 31 3b 0a 20 20 20 20  ak - i + 1;.    
387e4 7d 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 69  }.    if( safe_i
387e5 73 73 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65  sspace(zDoc[iBre
387e6 61 6b 2b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  ak+i]) ){.      
387e7 72 65 74 75 72 6e 20 69 42 72 65 61 6b 20 2b 20  return iBreak + 
387e8 69 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  i + 1;.    }.  }
387e9 0a 20 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b  .  return iBreak
387ea 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  ;.}..../*.** All
387eb 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20  owed values for 
387ec 53 6e 69 70 70 65 74 2e 61 4d 61 74 63 68 5b 5d  Snippet.aMatch[]
387ed 2e 73 6e 53 74 61 74 75 73 0a 2a 2f 0a 23 64 65  .snStatus.*/.#de
387ee 66 69 6e 65 20 53 4e 49 50 50 45 54 5f 49 47 4e  fine SNIPPET_IGN
387ef 4f 52 45 20 20 30 20 20 20 2f 2a 20 49 74 20 69  ORE  0   /* It i
387f0 73 20 6f 6b 20 74 6f 20 6f 6d 69 74 20 74 68 69  s ok to omit thi
387f1 73 20 6d 61 74 63 68 20 66 72 6f 6d 20 74 68 65  s match from the
387f2 20 73 6e 69 70 70 65 74 20 2a 2f 0a 23 64 65 66   snippet */.#def
387f3 69 6e 65 20 53 4e 49 50 50 45 54 5f 44 45 53 49  ine SNIPPET_DESI
387f4 52 45 44 20 31 20 20 20 2f 2a 20 57 65 20 77 61  RED 1   /* We wa
387f5 6e 74 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68  nt to include th
387f6 69 73 20 6d 61 74 63 68 20 69 6e 20 74 68 65 20  is match in the 
387f7 73 6e 69 70 70 65 74 20 2a 2f 0a 0a 2f 2a 0a 2a  snippet */../*.*
387f8 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74  * Generate the t
387f9 65 78 74 20 6f 66 20 61 20 73 6e 69 70 70 65 74  ext of a snippet
387fa 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
387fb 20 73 6e 69 70 70 65 74 54 65 78 74 28 0a 20 20   snippetText(.  
387fc 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 75 72  Fts3Cursor *pCur
387fd 73 6f 72 2c 20 20 20 2f 2a 20 54 68 65 20 63 75  sor,   /* The cu
387fe 72 73 6f 72 20 77 65 20 6e 65 65 64 20 74 68 65  rsor we need the
387ff 20 73 6e 69 70 70 65 74 20 66 6f 72 20 2a 2f 0a   snippet for */.
38800 20 20 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70    Snippet *pSnip
38801 70 65 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  pet,.  const cha
38802 72 20 2a 7a 53 74 61 72 74 4d 61 72 6b 2c 20 20  r *zStartMark,  
38803 20 20 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f 20     /* Markup to 
38804 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 65 61  appear before ea
38805 63 68 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f  ch match */.  co
38806 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 4d 61  nst char *zEndMa
38807 72 6b 2c 20 20 20 20 20 20 20 2f 2a 20 4d 61 72  rk,       /* Mar
38808 6b 75 70 20 74 6f 20 61 70 70 65 61 72 20 61 66  kup to appear af
38809 74 65 72 20 65 61 63 68 20 6d 61 74 63 68 20 2a  ter each match *
3880a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
3880b 7a 45 6c 6c 69 70 73 69 73 20 20 20 20 20 20 20  zEllipsis       
3880c 2f 2a 20 45 6c 6c 69 70 73 69 73 20 6d 61 72 6b  /* Ellipsis mark
3880d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
3880e 6a 3b 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70  j;.  struct snip
3880f 70 65 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68  petMatch *aMatch
38810 3b 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 0a  ;.  int nMatch;.
38811 20 20 69 6e 74 20 6e 44 65 73 69 72 65 64 3b 0a    int nDesired;.
38812 20 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73    StringBuffer s
38813 62 3b 0a 20 20 69 6e 74 20 74 61 69 6c 43 6f 6c  b;.  int tailCol
38814 3b 0a 20 20 69 6e 74 20 74 61 69 6c 4f 66 66 73  ;.  int tailOffs
38815 65 74 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a  et;.  int iCol;.
38816 20 20 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 63 6f    int nDoc;.  co
38817 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a  nst char *zDoc;.
38818 20 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 45    int iStart, iE
38819 6e 64 3b 0a 20 20 69 6e 74 20 74 61 69 6c 45 6c  nd;.  int tailEl
3881a 6c 69 70 73 69 73 20 3d 20 30 3b 0a 20 20 69 6e  lipsis = 0;.  in
3881b 74 20 69 4d 61 74 63 68 3b 0a 20 20 0a 0a 20 20  t iMatch;.  ..  
3881c 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 53 6e  sqlite3_free(pSn
3881d 69 70 70 65 74 2d 3e 7a 53 6e 69 70 70 65 74 29  ippet->zSnippet)
3881e 3b 0a 20 20 70 53 6e 69 70 70 65 74 2d 3e 7a 53  ;.  pSnippet->zS
3881f 6e 69 70 70 65 74 20 3d 20 30 3b 0a 20 20 61 4d  nippet = 0;.  aM
38820 61 74 63 68 20 3d 20 70 53 6e 69 70 70 65 74 2d  atch = pSnippet-
38821 3e 61 4d 61 74 63 68 3b 0a 20 20 6e 4d 61 74 63  >aMatch;.  nMatc
38822 68 20 3d 20 70 53 6e 69 70 70 65 74 2d 3e 6e 4d  h = pSnippet->nM
38823 61 74 63 68 3b 0a 20 20 69 6e 69 74 53 74 72 69  atch;.  initStri
38824 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 0a  ngBuffer(&sb);..
38825 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61    for(i=0; i<nMa
38826 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61  tch; i++){.    a
38827 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75  Match[i].snStatu
38828 73 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f  s = SNIPPET_IGNO
38829 52 45 3b 0a 20 20 7d 0a 20 20 6e 44 65 73 69 72  RE;.  }.  nDesir
3882a 65 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  ed = 0;.  for(i=
3882b 30 3b 20 69 3c 46 54 53 33 5f 52 4f 54 4f 52 5f  0; i<FTS3_ROTOR_
3882c 53 5a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f  SZ; i++){.    fo
3882d 72 28 6a 3d 30 3b 20 6a 3c 6e 4d 61 74 63 68 3b  r(j=0; j<nMatch;
3882e 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   j++){.      if(
3882f 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72 6d   aMatch[j].iTerm
38830 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==i ){.        a
38831 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75  Match[j].snStatu
38832 73 20 3d 20 53 4e 49 50 50 45 54 5f 44 45 53 49  s = SNIPPET_DESI
38833 52 45 44 3b 0a 20 20 20 20 20 20 20 20 6e 44 65  RED;.        nDe
38834 73 69 72 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20  sired++;.       
38835 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
38836 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 4d 61      }.  }..  iMa
38837 74 63 68 20 3d 20 30 3b 0a 20 20 74 61 69 6c 43  tch = 0;.  tailC
38838 6f 6c 20 3d 20 2d 31 3b 0a 20 20 74 61 69 6c 4f  ol = -1;.  tailO
38839 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72  ffset = 0;.  for
3883a 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26  (i=0; i<nMatch &
3883b 26 20 6e 44 65 73 69 72 65 64 3e 30 3b 20 69 2b  & nDesired>0; i+
3883c 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74  +){.    if( aMat
3883d 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73 21 3d  ch[i].snStatus!=
3883e 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44 20  SNIPPET_DESIRED 
3883f 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
38840 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20  nDesired--;.    
38841 69 43 6f 6c 20 3d 20 61 4d 61 74 63 68 5b 69 5d  iCol = aMatch[i]
38842 2e 69 43 6f 6c 3b 0a 20 20 20 20 7a 44 6f 63 20  .iCol;.    zDoc 
38843 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73  = (const char*)s
38844 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
38845 78 74 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d  xt(pCursor->pStm
38846 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20  t, iCol+1);.    
38847 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63  nDoc = sqlite3_c
38848 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 75 72  olumn_bytes(pCur
38849 73 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c  sor->pStmt, iCol
3884a 2b 31 29 3b 0a 20 20 20 20 69 53 74 61 72 74 20  +1);.    iStart 
3884b 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  = aMatch[i].iSta
3884c 72 74 20 2d 20 34 30 3b 0a 20 20 20 20 69 53 74  rt - 40;.    iSt
3884d 61 72 74 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61  art = wordBounda
3884e 72 79 28 69 53 74 61 72 74 2c 20 7a 44 6f 63 2c  ry(iStart, zDoc,
3884f 20 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e   nDoc, aMatch, n
38850 4d 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20  Match, iCol);.  
38851 20 20 69 66 28 20 69 53 74 61 72 74 3c 3d 31 30    if( iStart<=10
38852 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74   ){.      iStart
38853 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
38854 69 66 28 20 69 43 6f 6c 3d 3d 74 61 69 6c 43 6f  if( iCol==tailCo
38855 6c 20 26 26 20 69 53 74 61 72 74 3c 3d 74 61 69  l && iStart<=tai
38856 6c 4f 66 66 73 65 74 2b 32 30 20 29 7b 0a 20 20  lOffset+20 ){.  
38857 20 20 20 20 69 53 74 61 72 74 20 3d 20 74 61 69      iStart = tai
38858 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20  lOffset;.    }. 
38859 20 20 20 69 66 28 20 28 69 43 6f 6c 21 3d 74 61     if( (iCol!=ta
3885a 69 6c 43 6f 6c 20 26 26 20 74 61 69 6c 43 6f 6c  ilCol && tailCol
3885b 3e 3d 30 29 20 7c 7c 20 69 53 74 61 72 74 21 3d  >=0) || iStart!=
3885c 74 61 69 6c 4f 66 66 73 65 74 20 29 7b 0a 20 20  tailOffset ){.  
3885d 20 20 20 20 74 72 69 6d 57 68 69 74 65 53 70 61      trimWhiteSpa
3885e 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20 20 61  ce(&sb);.      a
3885f 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65 28  ppendWhiteSpace(
38860 26 73 62 29 3b 0a 20 20 20 20 20 20 61 70 70 65  &sb);.      appe
38861 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69  nd(&sb, zEllipsi
38862 73 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64  s);.      append
38863 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
38864 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 20  .    }.    iEnd 
38865 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  = aMatch[i].iSta
38866 72 74 20 2b 20 61 4d 61 74 63 68 5b 69 5d 2e 6e  rt + aMatch[i].n
38867 42 79 74 65 20 2b 20 34 30 3b 0a 20 20 20 20 69  Byte + 40;.    i
38868 45 6e 64 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61  End = wordBounda
38869 72 79 28 69 45 6e 64 2c 20 7a 44 6f 63 2c 20 6e  ry(iEnd, zDoc, n
3886a 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d 61  Doc, aMatch, nMa
3886b 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20  tch, iCol);.    
3886c 69 66 28 20 69 45 6e 64 3e 3d 6e 44 6f 63 2d 31  if( iEnd>=nDoc-1
3886d 30 20 29 7b 0a 20 20 20 20 20 20 69 45 6e 64 20  0 ){.      iEnd 
3886e 3d 20 6e 44 6f 63 3b 0a 20 20 20 20 20 20 74 61  = nDoc;.      ta
3886f 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b 0a  ilEllipsis = 0;.
38870 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
38871 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20   tailEllipsis = 
38872 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69  1;.    }.    whi
38873 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63  le( iMatch<nMatc
38874 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74  h && aMatch[iMat
38875 63 68 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 20 29 7b  ch].iCol<iCol ){
38876 20 69 4d 61 74 63 68 2b 2b 3b 20 7d 0a 20 20 20   iMatch++; }.   
38877 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69   while( iStart<i
38878 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 77 68 69  End ){.      whi
38879 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63  le( iMatch<nMatc
3887a 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74  h && aMatch[iMat
3887b 63 68 5d 2e 69 53 74 61 72 74 3c 69 53 74 61 72  ch].iStart<iStar
3887c 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26  t.             &
3887d 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  & aMatch[iMatch]
3887e 2e 69 43 6f 6c 3c 3d 69 43 6f 6c 20 29 7b 0a 20  .iCol<=iCol ){. 
3887f 20 20 20 20 20 20 20 69 4d 61 74 63 68 2b 2b 3b         iMatch++;
38880 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
38881 66 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68  f( iMatch<nMatch
38882 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63   && aMatch[iMatc
38883 68 5d 2e 69 53 74 61 72 74 3c 69 45 6e 64 0a 20  h].iStart<iEnd. 
38884 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61              && a
38885 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43  Match[iMatch].iC
38886 6f 6c 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20  ol==iCol ){.    
38887 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c      nappend(&sb,
38888 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20   &zDoc[iStart], 
38889 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
3888a 53 74 61 72 74 20 2d 20 69 53 74 61 72 74 29 3b  Start - iStart);
3888b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20  .        iStart 
3888c 3d 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  = aMatch[iMatch]
3888d 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20 20  .iStart;.       
3888e 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 53 74   append(&sb, zSt
3888f 61 72 74 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20  artMark);.      
38890 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c 20 26    nappend(&sb, &
38891 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20 61 4d  zDoc[iStart], aM
38892 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42 79  atch[iMatch].nBy
38893 74 65 29 3b 0a 20 20 20 20 20 20 20 20 61 70 70  te);.        app
38894 65 6e 64 28 26 73 62 2c 20 7a 45 6e 64 4d 61 72  end(&sb, zEndMar
38895 6b 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61  k);.        iSta
38896 72 74 20 2b 3d 20 61 4d 61 74 63 68 5b 69 4d 61  rt += aMatch[iMa
38897 74 63 68 5d 2e 6e 42 79 74 65 3b 0a 20 20 20 20  tch].nByte;.    
38898 20 20 20 20 66 6f 72 28 6a 3d 69 4d 61 74 63 68      for(j=iMatch
38899 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a 2b  +1; j<nMatch; j+
3889a 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
3889b 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72  ( aMatch[j].iTer
3889c 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  m==aMatch[iMatch
3889d 5d 2e 69 54 65 72 6d 0a 20 20 20 20 20 20 20 20  ].iTerm.        
3889e 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68 5b        && aMatch[
3889f 6a 5d 2e 73 6e 53 74 61 74 75 73 3d 3d 53 4e 49  j].snStatus==SNI
388a0 50 50 45 54 5f 44 45 53 49 52 45 44 20 29 7b 0a  PPET_DESIRED ){.
388a1 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 73              nDes
388a2 69 72 65 64 2d 2d 3b 0a 20 20 20 20 20 20 20 20  ired--;.        
388a3 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e      aMatch[j].sn
388a4 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45 54  Status = SNIPPET
388a5 5f 49 47 4e 4f 52 45 3b 0a 20 20 20 20 20 20 20  _IGNORE;.       
388a6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
388a7 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
388a8 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c      nappend(&sb,
388a9 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20   &zDoc[iStart], 
388aa 69 45 6e 64 20 2d 20 69 53 74 61 72 74 29 3b 0a  iEnd - iStart);.
388ab 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d          iStart =
388ac 20 69 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20   iEnd;.      }. 
388ad 20 20 20 7d 0a 20 20 20 20 74 61 69 6c 43 6f 6c     }.    tailCol
388ae 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 74 61 69   = iCol;.    tai
388af 6c 4f 66 66 73 65 74 20 3d 20 69 45 6e 64 3b 0a  lOffset = iEnd;.
388b0 20 20 7d 0a 20 20 74 72 69 6d 57 68 69 74 65 53    }.  trimWhiteS
388b1 70 61 63 65 28 26 73 62 29 3b 0a 20 20 69 66 28  pace(&sb);.  if(
388b2 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 29 7b   tailEllipsis ){
388b3 0a 20 20 20 20 61 70 70 65 6e 64 57 68 69 74 65  .    appendWhite
388b4 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20  Space(&sb);.    
388b5 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6c 6c  append(&sb, zEll
388b6 69 70 73 69 73 29 3b 0a 20 20 7d 0a 20 20 70 53  ipsis);.  }.  pS
388b7 6e 69 70 70 65 74 2d 3e 7a 53 6e 69 70 70 65 74  nippet->zSnippet
388b8 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65 72 44   = stringBufferD
388b9 61 74 61 28 26 73 62 29 3b 0a 20 20 70 53 6e 69  ata(&sb);.  pSni
388ba 70 70 65 74 2d 3e 6e 53 6e 69 70 70 65 74 20 3d  ppet->nSnippet =
388bb 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e   stringBufferLen
388bc 67 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 53 51 4c  gth(&sb);.}..SQL
388bd 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
388be 20 73 71 6c 69 74 65 33 46 74 73 33 4f 66 66 73   sqlite3Fts3Offs
388bf 65 74 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ets(.  sqlite3_c
388c0 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20  ontext *pCtx,   
388c1 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65         /* SQLite
388c2 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 63   function call c
388c3 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 46 74 73 33  ontext */.  Fts3
388c4 43 75 72 73 6f 72 20 2a 70 43 73 72 20 20 20 20  Cursor *pCsr    
388c5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
388c6 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  ursor object */.
388c7 29 7b 0a 20 20 53 6e 69 70 70 65 74 20 2a 70 3b  ){.  Snippet *p;
388c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
388c9 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65 74 20       /* Snippet 
388ca 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69  structure */.  i
388cb 6e 74 20 72 63 20 3d 20 73 6e 69 70 70 65 74 41  nt rc = snippetA
388cc 6c 6c 4f 66 66 73 65 74 73 28 70 43 73 72 2c 20  llOffsets(pCsr, 
388cd 26 70 29 3b 0a 20 20 73 6e 69 70 70 65 74 4f 66  &p);.  snippetOf
388ce 66 73 65 74 54 65 78 74 28 70 29 3b 0a 20 20 73  fsetText(p);.  s
388cf 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
388d0 78 74 28 70 43 74 78 2c 20 70 2d 3e 7a 4f 66 66  xt(pCtx, p->zOff
388d1 73 65 74 2c 20 70 2d 3e 6e 4f 66 66 73 65 74 2c  set, p->nOffset,
388d2 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
388d3 54 29 3b 0a 20 20 66 74 73 33 53 6e 69 70 70 65  T);.  fts3Snippe
388d4 74 46 72 65 65 28 70 29 3b 0a 7d 0a 0a 53 51 4c  tFree(p);.}..SQL
388d5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
388d6 20 73 71 6c 69 74 65 33 46 74 73 33 53 6e 69 70   sqlite3Fts3Snip
388d7 70 65 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  pet(.  sqlite3_c
388d8 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20  ontext *pCtx,   
388d9 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65         /* SQLite
388da 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 63   function call c
388db 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 46 74 73 33  ontext */.  Fts3
388dc 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20  Cursor *pCsr,   
388dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
388de 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  ursor object */.
388df 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
388e0 74 61 72 74 2c 20 20 20 20 20 20 20 20 20 20 20  tart,           
388e1 20 20 2f 2a 20 53 6e 69 70 70 65 74 20 73 74 61    /* Snippet sta
388e2 72 74 20 74 65 78 74 20 2d 20 22 3c 62 3e 22 20  rt text - "<b>" 
388e3 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
388e4 2a 7a 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20  *zEnd,          
388e5 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65 74 20       /* Snippet 
388e6 65 6e 64 20 74 65 78 74 20 2d 20 22 3c 2f 62 3e  end text - "</b>
388e7 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  " */.  const cha
388e8 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20 20 20 20  r *zEllipsis    
388e9 20 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65         /* Snippe
388ea 74 20 65 6c 6c 69 70 73 69 73 20 74 65 78 74 20  t ellipsis text 
388eb 2d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 20 2a  - "<b>...</b>" *
388ec 2f 0a 29 7b 0a 20 20 53 6e 69 70 70 65 74 20 2a  /.){.  Snippet *
388ed 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
388ee 20 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65         /* Snippe
388ef 74 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  t structure */. 
388f0 20 69 6e 74 20 72 63 20 3d 20 73 6e 69 70 70 65   int rc = snippe
388f1 74 41 6c 6c 4f 66 66 73 65 74 73 28 70 43 73 72  tAllOffsets(pCsr
388f2 2c 20 26 70 29 3b 0a 20 20 73 6e 69 70 70 65 74  , &p);.  snippet
388f3 54 65 78 74 28 70 43 73 72 2c 20 70 2c 20 7a 53  Text(pCsr, p, zS
388f4 74 61 72 74 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c  tart, zEnd, zEll
388f5 69 70 73 69 73 29 3b 0a 20 20 73 71 6c 69 74 65  ipsis);.  sqlite
388f6 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
388f7 74 78 2c 20 70 2d 3e 7a 53 6e 69 70 70 65 74 2c  tx, p->zSnippet,
388f8 20 70 2d 3e 6e 53 6e 69 70 70 65 74 2c 20 53 51   p->nSnippet, SQ
388f9 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
388fa 0a 20 20 66 74 73 33 53 6e 69 70 70 65 74 46 72  .  fts3SnippetFr
388fb 65 65 28 70 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  ee(p);.}..#endif
388fc 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
388fd 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 73 6e  * End of fts3_sn
388fe 69 70 70 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ippet.c ********
388ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38901 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
38902 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 74 72  * Begin file rtr
38903 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ee.c ***********
38904 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38906 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
38907 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
38908 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
38909 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
3890a 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
3890b 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
3890c 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
3890d 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
3890e 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
3890f 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
38910 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
38911 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
38912 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
38913 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
38914 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
38915 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
38916 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
38917 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
38918 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
38919 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3891a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3891b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3891c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3891d 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
3891e 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
3891f 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e  ode for implemen
38920 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 72  tations of the r
38921 2d 74 72 65 65 20 61 6e 64 20 72 2a 2d 74 72 65  -tree and r*-tre
38922 65 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 73 20  e.** algorithms 
38923 70 61 63 6b 61 67 65 64 20 61 73 20 61 6e 20 53  packaged as an S
38924 51 4c 69 74 65 20 76 69 72 74 75 61 6c 20 74 61  QLite virtual ta
38925 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 0a  ble module..*/..
38926 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
38927 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
38928 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
38929 4c 45 5f 52 54 52 45 45 29 0a 0a 2f 2a 0a 2a 2a  LE_RTREE)../*.**
3892a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
3892b 69 6e 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74  ins an implement
3892c 61 74 69 6f 6e 20 6f 66 20 61 20 63 6f 75 70 6c  ation of a coupl
3892d 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 76  e of different v
3892e 61 72 69 61 6e 74 73 0a 2a 2a 20 6f 66 20 74 68  ariants.** of th
3892f 65 20 72 2d 74 72 65 65 20 61 6c 67 6f 72 69 74  e r-tree algorit
38930 68 6d 2e 20 53 65 65 20 74 68 65 20 52 45 41 44  hm. See the READ
38931 4d 45 20 66 69 6c 65 20 66 6f 72 20 66 75 72 74  ME file for furt
38932 68 65 72 20 64 65 74 61 69 6c 73 2e 20 54 68 65  her details. The
38933 20 0a 2a 2a 20 73 61 6d 65 20 64 61 74 61 2d 73   .** same data-s
38934 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
38935 20 66 6f 72 20 61 6c 6c 2c 20 62 75 74 20 74 68   for all, but th
38936 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 66 6f 72  e algorithms for
38937 20 69 6e 73 65 72 74 20 61 6e 64 0a 2a 2a 20 64   insert and.** d
38938 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 73  elete operations
38939 20 76 61 72 79 2e 20 54 68 65 20 76 61 72 69 61   vary. The varia
3893a 6e 74 73 20 75 73 65 64 20 61 72 65 20 73 65 6c  nts used are sel
3893b 65 63 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  ected at compile
3893c 20 74 69 6d 65 20 0a 2a 2a 20 62 79 20 64 65 66   time .** by def
3893d 69 6e 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77  ining the follow
3893e 69 6e 67 20 73 79 6d 62 6f 6c 73 3a 0a 2a 2f 0a  ing symbols:.*/.
3893f 0a 2f 2a 20 45 69 74 68 65 72 2c 20 62 6f 74 68  ./* Either, both
38940 20 6f 72 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   or none of the 
38941 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 79 20 62 65  following may be
38942 20 73 65 74 20 74 6f 20 61 63 74 69 76 61 74 65   set to activate
38943 20 0a 2a 2a 20 72 2a 74 72 65 65 20 76 61 72 69   .** r*tree vari
38944 61 6e 74 20 61 6c 67 6f 72 69 74 68 6d 73 2e 0a  ant algorithms..
38945 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41  */.#define VARIA
38946 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f  NT_RSTARTREE_CHO
38947 4f 53 45 53 55 42 54 52 45 45 20 30 0a 23 64 65  OSESUBTREE 0.#de
38948 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54  fine VARIANT_RST
38949 41 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 20  ARTREE_REINSERT 
3894a 20 20 20 20 20 31 0a 0a 2f 2a 20 0a 2a 2a 20 45       1../* .** E
3894b 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68  xactly one of th
3894c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 75 73 74  e following must
3894d 20 62 65 20 73 65 74 20 74 6f 20 31 2e 0a 2a 2f   be set to 1..*/
3894e 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54  .#define VARIANT
3894f 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54  _GUTTMAN_QUADRAT
38950 49 43 5f 53 50 4c 49 54 20 30 0a 23 64 65 66 69  IC_SPLIT 0.#defi
38951 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  ne VARIANT_GUTTM
38952 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20  AN_LINEAR_SPLIT 
38953 20 20 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52     0.#define VAR
38954 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53  IANT_RSTARTREE_S
38955 50 4c 49 54 20 20 20 20 20 20 20 20 20 31 0a 0a  PLIT         1..
38956 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f  #define VARIANT_
38957 47 55 54 54 4d 41 4e 5f 53 50 4c 49 54 20 5c 0a  GUTTMAN_SPLIT \.
38958 20 20 20 20 20 20 20 20 28 56 41 52 49 41 4e 54          (VARIANT
38959 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f  _GUTTMAN_LINEAR_
3895a 53 50 4c 49 54 7c 7c 56 41 52 49 41 4e 54 5f 47  SPLIT||VARIANT_G
3895b 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
3895c 5f 53 50 4c 49 54 29 0a 0a 23 69 66 20 56 41 52  _SPLIT)..#if VAR
3895d 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
3895e 44 52 41 54 49 43 5f 53 50 4c 49 54 0a 20 20 23  DRATIC_SPLIT.  #
3895f 64 65 66 69 6e 65 20 50 69 63 6b 4e 65 78 74 20  define PickNext 
38960 51 75 61 64 72 61 74 69 63 50 69 63 6b 4e 65 78  QuadraticPickNex
38961 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  t.  #define Pick
38962 53 65 65 64 73 20 51 75 61 64 72 61 74 69 63 50  Seeds QuadraticP
38963 69 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69  ickSeeds.  #defi
38964 6e 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73  ne AssignCells s
38965 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a  plitNodeGuttman.
38966 23 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41  #endif.#if VARIA
38967 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41  NT_GUTTMAN_LINEA
38968 52 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e  R_SPLIT.  #defin
38969 65 20 50 69 63 6b 4e 65 78 74 20 4c 69 6e 65 61  e PickNext Linea
3896a 72 50 69 63 6b 4e 65 78 74 0a 20 20 23 64 65 66  rPickNext.  #def
3896b 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20 4c 69  ine PickSeeds Li
3896c 6e 65 61 72 50 69 63 6b 53 65 65 64 73 0a 20 20  nearPickSeeds.  
3896d 23 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65  #define AssignCe
3896e 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65 47 75 74  lls splitNodeGut
3896f 74 6d 61 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20  tman.#endif.#if 
38970 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
38971 45 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e  E_SPLIT.  #defin
38972 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70  e AssignCells sp
38973 6c 69 74 4e 6f 64 65 53 74 61 72 74 72 65 65 0a  litNodeStartree.
38974 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66  #endif...#ifndef
38975 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53   SQLITE_CORE.  S
38976 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f  QLITE_EXTENSION_
38977 49 4e 49 54 31 0a 23 65 6c 73 65 0a 23 65 6e 64  INIT1.#else.#end
38978 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  if...#ifndef SQL
38979 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
3897a 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 33  .typedef sqlite3
3897b 5f 69 6e 74 36 34 20 69 36 34 3b 0a 74 79 70 65  _int64 i64;.type
3897c 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61  def unsigned cha
3897d 72 20 75 38 3b 0a 74 79 70 65 64 65 66 20 75 6e  r u8;.typedef un
3897e 73 69 67 6e 65 64 20 69 6e 74 20 75 33 32 3b 0a  signed int u32;.
3897f 23 65 6e 64 69 66 0a 0a 74 79 70 65 64 65 66 20  #endif..typedef 
38980 73 74 72 75 63 74 20 52 74 72 65 65 20 52 74 72  struct Rtree Rtr
38981 65 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ee;.typedef stru
38982 63 74 20 52 74 72 65 65 43 75 72 73 6f 72 20 52  ct RtreeCursor R
38983 74 72 65 65 43 75 72 73 6f 72 3b 0a 74 79 70 65  treeCursor;.type
38984 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65  def struct Rtree
38985 4e 6f 64 65 20 52 74 72 65 65 4e 6f 64 65 3b 0a  Node RtreeNode;.
38986 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52  typedef struct R
38987 74 72 65 65 43 65 6c 6c 20 52 74 72 65 65 43 65  treeCell RtreeCe
38988 6c 6c 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ll;.typedef stru
38989 63 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  ct RtreeConstrai
3898a 6e 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  nt RtreeConstrai
3898b 6e 74 3b 0a 74 79 70 65 64 65 66 20 75 6e 69 6f  nt;.typedef unio
3898c 6e 20 52 74 72 65 65 43 6f 6f 72 64 20 52 74 72  n RtreeCoord Rtr
3898d 65 65 43 6f 6f 72 64 3b 0a 0a 2f 2a 20 54 68 65  eeCoord;../* The
3898e 20 72 74 72 65 65 20 6d 61 79 20 68 61 76 65 20   rtree may have 
3898f 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 52 54  between 1 and RT
38990 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f  REE_MAX_DIMENSIO
38991 4e 53 20 64 69 6d 65 6e 73 69 6f 6e 73 2e 20 2a  NS dimensions. *
38992 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
38993 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 35  MAX_DIMENSIONS 5
38994 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 68 61 73  ../* Size of has
38995 68 20 74 61 62 6c 65 20 52 74 72 65 65 2e 61 48  h table Rtree.aH
38996 61 73 68 2e 20 54 68 69 73 20 68 61 73 68 20 74  ash. This hash t
38997 61 62 6c 65 20 69 73 20 6e 6f 74 20 65 78 70 65  able is not expe
38998 63 74 65 64 20 74 6f 0a 2a 2a 20 65 76 65 72 20  cted to.** ever 
38999 63 6f 6e 74 61 69 6e 20 76 65 72 79 20 6d 61 6e  contain very man
3899a 79 20 65 6e 74 72 69 65 73 2c 20 73 6f 20 61 20  y entries, so a 
3899b 66 69 78 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  fixed number of 
3899c 62 75 63 6b 65 74 73 20 69 73 20 0a 2a 2a 20 75  buckets is .** u
3899d 73 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  sed..*/.#define 
3899e 48 41 53 48 53 49 5a 45 20 31 32 38 0a 0a 2f 2a  HASHSIZE 128../*
3899f 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 76 69   .** An rtree vi
389a0 72 74 75 61 6c 2d 74 61 62 6c 65 20 6f 62 6a 65  rtual-table obje
389a1 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  ct..*/.struct Rt
389a2 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ree {.  sqlite3_
389a3 76 74 61 62 20 62 61 73 65 3b 0a 20 20 73 71 6c  vtab base;.  sql
389a4 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
389a5 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 73 74           /* Host
389a6 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
389a7 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4e  tion */.  int iN
389a8 6f 64 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  odeSize;        
389a9 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 69 6e        /* Size in
389aa 20 62 79 74 65 73 20 6f 66 20 65 61 63 68 20 6e   bytes of each n
389ab 6f 64 65 20 69 6e 20 74 68 65 20 6e 6f 64 65 20  ode in the node 
389ac 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
389ad 44 69 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Dim;            
389ae 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
389af 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 73 20 2a   of dimensions *
389b0 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 50 65  /.  int nBytesPe
389b1 72 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  rCell;          
389b2 2f 2a 20 42 79 74 65 73 20 63 6f 6e 73 75 6d 65  /* Bytes consume
389b3 64 20 70 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20  d per cell */.  
389b4 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20 20  int iDepth;     
389b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
389b6 75 72 72 65 6e 74 20 64 65 70 74 68 20 6f 66 20  urrent depth of 
389b7 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75 63  the r-tree struc
389b8 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ture */.  char *
389b9 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  zDb;            
389ba 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
389bb 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
389bc 6e 69 6e 67 20 72 2d 74 72 65 65 20 74 61 62 6c  ning r-tree tabl
389bd 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  e */.  char *zNa
389be 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
389bf 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 72 2d     /* Name of r-
389c0 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 20 0a 20  tree table */ . 
389c1 20 52 74 72 65 65 4e 6f 64 65 20 2a 61 48 61 73   RtreeNode *aHas
389c2 68 5b 48 41 53 48 53 49 5a 45 5d 3b 20 2f 2a 20  h[HASHSIZE]; /* 
389c3 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 69 6e  Hash table of in
389c4 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65 73 2e 20 2a  -memory nodes. *
389c5 2f 20 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20  / .  int nBusy; 
389c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
389c7 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62   /* Current numb
389c8 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 74  er of users of t
389c9 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
389ca 0a 0a 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6e  ..  /* List of n
389cb 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 64 75 72  odes removed dur
389cc 69 6e 67 20 61 20 43 6f 6e 64 65 6e 73 65 54 72  ing a CondenseTr
389cd 65 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 4c 69  ee operation. Li
389ce 73 74 20 69 73 0a 20 20 2a 2a 20 6c 69 6e 6b 65  st is.  ** linke
389cf 64 20 74 6f 67 65 74 68 65 72 20 76 69 61 20 74  d together via t
389d0 68 65 20 70 6f 69 6e 74 65 72 20 6e 6f 72 6d 61  he pointer norma
389d1 6c 6c 79 20 75 73 65 64 20 66 6f 72 20 68 61 73  lly used for has
389d2 68 20 63 68 61 69 6e 73 20 2d 0a 20 20 2a 2a 20  h chains -.  ** 
389d3 52 74 72 65 65 4e 6f 64 65 2e 70 4e 65 78 74 2e  RtreeNode.pNext.
389d4 20 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65   RtreeNode.iNode
389d5 20 73 74 6f 72 65 73 20 74 68 65 20 64 65 70 74   stores the dept
389d6 68 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65  h of the sub-tre
389d7 65 20 0a 20 20 2a 2a 20 68 65 61 64 65 64 20 62  e .  ** headed b
389d8 79 20 74 68 65 20 6e 6f 64 65 20 28 6c 65 61 66  y the node (leaf
389d9 20 6e 6f 64 65 73 20 68 61 76 65 20 52 74 72 65   nodes have Rtre
389da 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2e  eNode.iNode==0).
389db 0a 20 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  .  */.  RtreeNod
389dc 65 20 2a 70 44 65 6c 65 74 65 64 3b 0a 20 20 69  e *pDeleted;.  i
389dd 6e 74 20 69 52 65 69 6e 73 65 72 74 48 65 69 67  nt iReinsertHeig
389de 68 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 48 65  ht;        /* He
389df 69 67 68 74 20 6f 66 20 73 75 62 2d 74 72 65 65  ight of sub-tree
389e0 73 20 52 65 69 6e 73 65 72 74 28 29 20 68 61 73  s Reinsert() has
389e1 20 72 75 6e 20 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a   run on */..  /*
389e2 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72   Statements to r
389e3 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65  ead/write/delete
389e4 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78   a record from x
389e5 78 78 5f 6e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  xx_node */.  sql
389e6 69 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64  ite3_stmt *pRead
389e7 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Node;.  sqlite3_
389e8 73 74 6d 74 20 2a 70 57 72 69 74 65 4e 6f 64 65  stmt *pWriteNode
389e9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
389ea 20 2a 70 44 65 6c 65 74 65 4e 6f 64 65 3b 0a 0a   *pDeleteNode;..
389eb 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73 20    /* Statements 
389ec 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64 65  to read/write/de
389ed 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66 72  lete a record fr
389ee 6f 6d 20 78 78 78 5f 72 6f 77 69 64 20 2a 2f 0a  om xxx_rowid */.
389ef 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
389f0 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20 73 71  pReadRowid;.  sq
389f1 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69  lite3_stmt *pWri
389f2 74 65 52 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74  teRowid;.  sqlit
389f3 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65  e3_stmt *pDelete
389f4 52 6f 77 69 64 3b 0a 0a 20 20 2f 2a 20 53 74 61  Rowid;..  /* Sta
389f5 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f  tements to read/
389f6 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72  write/delete a r
389f7 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 70  ecord from xxx_p
389f8 61 72 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74  arent */.  sqlit
389f9 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 50 61  e3_stmt *pReadPa
389fa 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f  rent;.  sqlite3_
389fb 73 74 6d 74 20 2a 70 57 72 69 74 65 50 61 72 65  stmt *pWritePare
389fc 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  nt;.  sqlite3_st
389fd 6d 74 20 2a 70 44 65 6c 65 74 65 50 61 72 65 6e  mt *pDeleteParen
389fe 74 3b 0a 0a 20 20 69 6e 74 20 65 43 6f 6f 72 64  t;..  int eCoord
389ff 54 79 70 65 3b 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73  Type;.};../* Pos
38a00 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72  sible values for
38a01 20 65 43 6f 6f 72 64 54 79 70 65 3a 20 2a 2f 0a   eCoordType: */.
38a02 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 43 4f  #define RTREE_CO
38a03 4f 52 44 5f 52 45 41 4c 33 32 20 30 0a 23 64 65  ORD_REAL32 0.#de
38a04 66 69 6e 65 20 52 54 52 45 45 5f 43 4f 4f 52 44  fine RTREE_COORD
38a05 5f 49 4e 54 33 32 20 20 31 0a 0a 2f 2a 0a 2a 2a  _INT32  1../*.**
38a06 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 6e 75 6d   The minimum num
38a07 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 61 6c 6c  ber of cells all
38a08 6f 77 65 64 20 66 6f 72 20 61 20 6e 6f 64 65 20  owed for a node 
38a09 69 73 20 61 20 74 68 69 72 64 20 6f 66 20 74 68  is a third of th
38a0a 65 20 0a 2a 2a 20 6d 61 78 69 6d 75 6d 2e 20 49  e .** maximum. I
38a0b 6e 20 47 75 74 6d 61 6e 27 73 20 6e 6f 74 61 74  n Gutman's notat
38a0c 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6d  ion:.**.**     m
38a0d 20 3d 20 4d 2f 33 0a 2a 2a 0a 2a 2a 20 49 66 20   = M/3.**.** If 
38a0e 61 6e 20 52 2a 2d 74 72 65 65 20 22 52 65 69 6e  an R*-tree "Rein
38a0f 73 65 72 74 22 20 6f 70 65 72 61 74 69 6f 6e 20  sert" operation 
38a10 69 73 20 72 65 71 75 69 72 65 64 2c 20 74 68 65  is required, the
38a11 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 0a   same number of.
38a12 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 72 65 6d  ** cells are rem
38a13 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 76  oved from the ov
38a14 65 72 66 75 6c 6c 20 6e 6f 64 65 20 61 6e 64 20  erfull node and 
38a15 72 65 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  reinserted into 
38a16 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 23 64 65  the tree..*/.#de
38a17 66 69 6e 65 20 52 54 52 45 45 5f 4d 49 4e 43 45  fine RTREE_MINCE
38a18 4c 4c 53 28 70 29 20 28 28 28 28 70 29 2d 3e 69  LLS(p) ((((p)->i
38a19 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 28 70 29 2d  NodeSize-4)/(p)-
38a1a 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 29 2f  >nBytesPerCell)/
38a1b 33 29 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  3).#define RTREE
38a1c 5f 52 45 49 4e 53 45 52 54 28 70 29 20 52 54 52  _REINSERT(p) RTR
38a1d 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 29 0a 23  EE_MINCELLS(p).#
38a1e 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4d 41 58  define RTREE_MAX
38a1f 43 45 4c 4c 53 20 35 31 0a 0a 2f 2a 20 0a 2a 2a  CELLS 51../* .**
38a20 20 41 6e 20 72 74 72 65 65 20 63 75 72 73 6f 72   An rtree cursor
38a21 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75   object..*/.stru
38a22 63 74 20 52 74 72 65 65 43 75 72 73 6f 72 20 7b  ct RtreeCursor {
38a23 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
38a24 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 52  cursor base;.  R
38a25 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b  treeNode *pNode;
38a26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a27 20 2f 2a 20 4e 6f 64 65 20 63 75 72 73 6f 72 20   /* Node cursor 
38a28 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
38a29 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 69 6e  nting at */.  in
38a2a 74 20 69 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  t iCell;        
38a2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a2c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72  /* Index of curr
38a2d 65 6e 74 20 63 65 6c 6c 20 69 6e 20 70 4e 6f 64  ent cell in pNod
38a2e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 72 61  e */.  int iStra
38a2f 74 65 67 79 3b 20 20 20 20 20 20 20 20 20 20 20  tegy;           
38a30 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
38a31 20 6f 66 20 69 64 78 4e 75 6d 20 73 65 61 72 63   of idxNum searc
38a32 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  h parameter */. 
38a33 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74   int nConstraint
38a34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
38a35 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
38a36 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73  entries in aCons
38a37 74 72 61 69 6e 74 20 2a 2f 0a 20 20 52 74 72 65  traint */.  Rtre
38a38 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 61 43 6f  eConstraint *aCo
38a39 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 2f 2a  nstraint;     /*
38a3a 20 53 65 61 72 63 68 20 63 6f 6e 73 74 72 61 69   Search constrai
38a3b 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a 0a 75 6e 69 6f  nts. */.};..unio
38a3c 6e 20 52 74 72 65 65 43 6f 6f 72 64 20 7b 0a 20  n RtreeCoord {. 
38a3d 20 66 6c 6f 61 74 20 66 3b 0a 20 20 69 6e 74 20   float f;.  int 
38a3e 69 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  i;.};../*.** The
38a3f 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
38a40 52 74 72 65 65 43 6f 6f 72 64 2e 20 52 65 74 75  RtreeCoord. Retu
38a41 72 6e 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  rn the value sto
38a42 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52  red within the R
38a43 74 72 65 65 43 6f 6f 72 64 0a 2a 2a 20 66 6f 72  treeCoord.** for
38a44 6d 61 74 74 65 64 20 61 73 20 61 20 64 6f 75 62  matted as a doub
38a45 6c 65 2e 20 54 68 69 73 20 6d 61 63 72 6f 20 61  le. This macro a
38a46 73 73 75 6d 65 73 20 74 68 61 74 20 6c 6f 63 61  ssumes that loca
38a47 6c 20 76 61 72 69 61 62 6c 65 20 70 52 74 72 65  l variable pRtre
38a48 65 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74  e points.** to t
38a49 68 65 20 52 74 72 65 65 20 73 74 72 75 63 74 75  he Rtree structu
38a4a 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  re associated wi
38a4b 74 68 20 74 68 65 20 52 74 72 65 65 43 6f 6f 72  th the RtreeCoor
38a4c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 43  d..*/.#define DC
38a4d 4f 4f 52 44 28 63 6f 6f 72 64 29 20 28 20 20 20  OORD(coord) (   
38a4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a4f 20 20 20 20 20 20 20 20 5c 0a 20 20 28 70 52 74          \.  (pRt
38a50 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
38a51 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41  =RTREE_COORD_REA
38a52 4c 33 32 29 20 3f 20 20 20 20 20 20 5c 0a 20 20  L32) ?      \.  
38a53 20 20 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64    ((double)coord
38a54 2e 66 29 20 3a 20 20 20 20 20 20 20 20 20 20 20  .f) :           
38a55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a56 5c 0a 20 20 20 20 28 28 64 6f 75 62 6c 65 29 63  \.    ((double)c
38a57 6f 6f 72 64 2e 69 29 20 20 20 20 20 20 20 20 20  oord.i)         
38a58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a59 20 20 20 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a 20 41      \.)../*.** A
38a5a 20 73 65 61 72 63 68 20 63 6f 6e 73 74 72 61 69   search constrai
38a5b 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  nt..*/.struct Rt
38a5c 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 7b 0a  reeConstraint {.
38a5d 20 20 69 6e 74 20 69 43 6f 6f 72 64 3b 20 20 20    int iCoord;   
38a5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a5f 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
38a60 63 6f 6e 73 74 72 61 69 6e 65 64 20 63 6f 6f 72  constrained coor
38a61 64 69 6e 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20  dinate */.  int 
38a62 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
38a63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38a64 20 43 6f 6e 73 74 72 61 69 6e 69 6e 67 20 6f 70   Constraining op
38a65 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 64 6f 75  eration */.  dou
38a66 62 6c 65 20 72 56 61 6c 75 65 3b 20 20 20 20 20  ble rValue;     
38a67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38a68 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c  * Constraint val
38a69 75 65 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 50 6f  ue. */.};../* Po
38a6a 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
38a6b 72 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e  r RtreeConstrain
38a6c 74 2e 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20  t.op */.#define 
38a6d 52 54 52 45 45 5f 45 51 20 30 78 34 31 0a 23 64  RTREE_EQ 0x41.#d
38a6e 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 45 20 30  efine RTREE_LE 0
38a6f 78 34 32 0a 23 64 65 66 69 6e 65 20 52 54 52 45  x42.#define RTRE
38a70 45 5f 4c 54 20 30 78 34 33 0a 23 64 65 66 69 6e  E_LT 0x43.#defin
38a71 65 20 52 54 52 45 45 5f 47 45 20 30 78 34 34 0a  e RTREE_GE 0x44.
38a72 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 47 54  #define RTREE_GT
38a73 20 30 78 34 35 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e   0x45../* .** An
38a74 20 72 74 72 65 65 20 73 74 72 75 63 74 75 72 65   rtree structure
38a75 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 44 61 74   node..**.** Dat
38a76 61 20 66 6f 72 6d 61 74 20 28 52 74 72 65 65 4e  a format (RtreeN
38a77 6f 64 65 2e 7a 44 61 74 61 29 3a 0a 2a 2a 0a 2a  ode.zData):.**.*
38a78 2a 20 20 20 31 2e 20 49 66 20 74 68 65 20 6e 6f  *   1. If the no
38a79 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6e  de is the root n
38a7a 6f 64 65 20 28 6e 6f 64 65 20 31 29 2c 20 74 68  ode (node 1), th
38a7b 65 6e 20 74 68 65 20 66 69 72 73 74 20 32 20 62  en the first 2 b
38a7c 79 74 65 73 0a 2a 2a 20 20 20 20 20 20 6f 66 20  ytes.**      of 
38a7d 74 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e  the node contain
38a7e 20 74 68 65 20 74 72 65 65 20 64 65 70 74 68 20   the tree depth 
38a7f 61 73 20 61 20 62 69 67 2d 65 6e 64 69 61 6e 20  as a big-endian 
38a80 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 20 20 20 20  integer..**     
38a81 20 46 6f 72 20 6e 6f 6e 2d 72 6f 6f 74 20 6e 6f   For non-root no
38a82 64 65 73 2c 20 74 68 65 20 66 69 72 73 74 20 32  des, the first 2
38a83 20 62 79 74 65 73 20 61 72 65 20 6c 65 66 74 20   bytes are left 
38a84 75 6e 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  unused..**.**   
38a85 32 2e 20 54 68 65 20 6e 65 78 74 20 32 20 62 79  2. The next 2 by
38a86 74 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  tes contain the 
38a87 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
38a88 73 20 63 75 72 72 65 6e 74 6c 79 20 0a 2a 2a 20  s currently .** 
38a89 20 20 20 20 20 73 74 6f 72 65 64 20 69 6e 20 74       stored in t
38a8a 68 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 20  he node..**.**  
38a8b 20 33 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65   3. The remainde
38a8c 72 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 63 6f  r of the node co
38a8d 6e 74 61 69 6e 73 20 74 68 65 20 6e 6f 64 65 20  ntains the node 
38a8e 65 6e 74 72 69 65 73 2e 20 45 61 63 68 20 65 6e  entries. Each en
38a8f 74 72 79 0a 2a 2a 20 20 20 20 20 20 63 6f 6e 73  try.**      cons
38a90 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65  ists of a single
38a91 20 38 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20   8-byte integer 
38a92 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 20 65  followed by an e
38a93 76 65 6e 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20  ven number.**   
38a94 20 20 20 6f 66 20 34 2d 62 79 74 65 20 63 6f 6f     of 4-byte coo
38a95 72 64 69 6e 61 74 65 73 2e 20 46 6f 72 20 6c 65  rdinates. For le
38a96 61 66 20 6e 6f 64 65 73 20 74 68 65 20 69 6e 74  af nodes the int
38a97 65 67 65 72 20 69 73 20 74 68 65 20 72 6f 77 69  eger is the rowi
38a98 64 0a 2a 2a 20 20 20 20 20 20 6f 66 20 61 20 72  d.**      of a r
38a99 65 63 6f 72 64 2e 20 46 6f 72 20 69 6e 74 65 72  ecord. For inter
38a9a 6e 61 6c 20 6e 6f 64 65 73 20 69 74 20 69 73 20  nal nodes it is 
38a9b 74 68 65 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20  the node number 
38a9c 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20 63 68 69  of a.**      chi
38a9d 6c 64 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 72 75  ld page..*/.stru
38a9e 63 74 20 52 74 72 65 65 4e 6f 64 65 20 7b 0a 20  ct RtreeNode {. 
38a9f 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
38aa0 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ent;            
38aa1 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6e 6f 64     /* Parent nod
38aa2 65 20 2a 2f 0a 20 20 69 36 34 20 69 4e 6f 64 65  e */.  i64 iNode
38aa3 3b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20  ;.  int nRef;.  
38aa4 69 6e 74 20 69 73 44 69 72 74 79 3b 0a 20 20 75  int isDirty;.  u
38aa5 38 20 2a 7a 44 61 74 61 3b 0a 20 20 52 74 72 65  8 *zData;.  Rtre
38aa6 65 4e 6f 64 65 20 2a 70 4e 65 78 74 3b 20 20 20  eNode *pNext;   
38aa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38aa8 20 4e 65 78 74 20 6e 6f 64 65 20 69 6e 20 74 68   Next node in th
38aa9 69 73 20 68 61 73 68 20 63 68 61 69 6e 20 2a 2f  is hash chain */
38aaa 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 4e 43 45 4c  .};.#define NCEL
38aab 4c 28 70 4e 6f 64 65 29 20 72 65 61 64 49 6e 74  L(pNode) readInt
38aac 31 36 28 26 28 70 4e 6f 64 65 29 2d 3e 7a 44 61  16(&(pNode)->zDa
38aad 74 61 5b 32 5d 29 0a 0a 2f 2a 20 0a 2a 2a 20 53  ta[2])../* .** S
38aae 74 72 75 63 74 75 72 65 20 74 6f 20 73 74 6f 72  tructure to stor
38aaf 65 20 61 20 64 65 73 65 72 69 61 6c 69 7a 65 64  e a deserialized
38ab0 20 72 74 72 65 65 20 72 65 63 6f 72 64 2e 0a 2a   rtree record..*
38ab1 2f 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 65  /.struct RtreeCe
38ab2 6c 6c 20 7b 0a 20 20 69 36 34 20 69 52 6f 77 69  ll {.  i64 iRowi
38ab3 64 3b 0a 20 20 52 74 72 65 65 43 6f 6f 72 64 20  d;.  RtreeCoord 
38ab4 61 43 6f 6f 72 64 5b 52 54 52 45 45 5f 4d 41 58  aCoord[RTREE_MAX
38ab5 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 32 5d 3b 0a  _DIMENSIONS*2];.
38ab6 7d 3b 0a 0a 23 69 66 6e 64 65 66 20 4d 41 58 0a  };..#ifndef MAX.
38ab7 23 20 64 65 66 69 6e 65 20 4d 41 58 28 78 2c 79  # define MAX(x,y
38ab8 29 20 28 28 78 29 20 3c 20 28 79 29 20 3f 20 28  ) ((x) < (y) ? (
38ab9 79 29 20 3a 20 28 78 29 29 0a 23 65 6e 64 69 66  y) : (x)).#endif
38aba 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64  .#ifndef MIN.# d
38abb 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28  efine MIN(x,y) (
38abc 28 78 29 20 3e 20 28 79 29 20 3f 20 28 79 29 20  (x) > (y) ? (y) 
38abd 3a 20 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f  : (x)).#endif../
38abe 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74  *.** Functions t
38abf 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 61 20  o deserialize a 
38ac0 31 36 20 62 69 74 20 69 6e 74 65 67 65 72 2c 20  16 bit integer, 
38ac1 33 32 20 62 69 74 20 72 65 61 6c 20 6e 75 6d 62  32 bit real numb
38ac2 65 72 20 61 6e 64 0a 2a 2a 20 36 34 20 62 69 74  er and.** 64 bit
38ac3 20 69 6e 74 65 67 65 72 2e 20 54 68 65 20 64 65   integer. The de
38ac4 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65  serialized value
38ac5 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
38ac6 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
38ac7 49 6e 74 31 36 28 75 38 20 2a 70 29 7b 0a 20 20  Int16(u8 *p){.  
38ac8 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 38 29  return (p[0]<<8)
38ac9 20 2b 20 70 5b 31 5d 3b 0a 7d 0a 73 74 61 74 69   + p[1];.}.stati
38aca 63 20 76 6f 69 64 20 72 65 61 64 43 6f 6f 72 64  c void readCoord
38acb 28 75 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f  (u8 *p, RtreeCoo
38acc 72 64 20 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75  rd *pCoord){.  u
38acd 33 32 20 69 20 3d 20 28 0a 20 20 20 20 28 28 28  32 i = (.    (((
38ace 75 33 32 29 70 5b 30 5d 29 20 3c 3c 20 32 34 29  u32)p[0]) << 24)
38acf 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
38ad0 5b 31 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20  [1]) << 16) + . 
38ad1 20 20 20 28 28 28 75 33 32 29 70 5b 32 5d 29 20     (((u32)p[2]) 
38ad2 3c 3c 20 20 38 29 20 2b 20 0a 20 20 20 20 28 28  <<  8) + .    ((
38ad3 28 75 33 32 29 70 5b 33 5d 29 20 3c 3c 20 20 30  (u32)p[3]) <<  0
38ad4 29 0a 20 20 29 3b 0a 20 20 2a 28 75 33 32 20 2a  ).  );.  *(u32 *
38ad5 29 70 43 6f 6f 72 64 20 3d 20 69 3b 0a 7d 0a 73  )pCoord = i;.}.s
38ad6 74 61 74 69 63 20 69 36 34 20 72 65 61 64 49 6e  tatic i64 readIn
38ad7 74 36 34 28 75 38 20 2a 70 29 7b 0a 20 20 72 65  t64(u8 *p){.  re
38ad8 74 75 72 6e 20 28 0a 20 20 20 20 28 28 28 69 36  turn (.    (((i6
38ad9 34 29 70 5b 30 5d 29 20 3c 3c 20 35 36 29 20 2b  4)p[0]) << 56) +
38ada 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 31   .    (((i64)p[1
38adb 5d 29 20 3c 3c 20 34 38 29 20 2b 20 0a 20 20 20  ]) << 48) + .   
38adc 20 28 28 28 69 36 34 29 70 5b 32 5d 29 20 3c 3c   (((i64)p[2]) <<
38add 20 34 30 29 20 2b 20 0a 20 20 20 20 28 28 28 69   40) + .    (((i
38ade 36 34 29 70 5b 33 5d 29 20 3c 3c 20 33 32 29 20  64)p[3]) << 32) 
38adf 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
38ae0 34 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20  4]) << 24) + .  
38ae1 20 20 28 28 28 69 36 34 29 70 5b 35 5d 29 20 3c    (((i64)p[5]) <
38ae2 3c 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28  < 16) + .    (((
38ae3 69 36 34 29 70 5b 36 5d 29 20 3c 3c 20 20 38 29  i64)p[6]) <<  8)
38ae4 20 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70   + .    (((i64)p
38ae5 5b 37 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b  [7]) <<  0).  );
38ae6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  .}../*.** Functi
38ae7 6f 6e 73 20 74 6f 20 73 65 72 69 61 6c 69 7a 65  ons to serialize
38ae8 20 61 20 31 36 20 62 69 74 20 69 6e 74 65 67 65   a 16 bit intege
38ae9 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c 20 6e  r, 32 bit real n
38aea 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36 34 20  umber and.** 64 
38aeb 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54 68 65  bit integer. The
38aec 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
38aed 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
38aee 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 0a 2a   bytes written.*
38aef 2a 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e  * to the argumen
38af0 74 20 62 75 66 66 65 72 20 28 61 6c 77 61 79 73  t buffer (always
38af1 20 32 2c 20 34 20 61 6e 64 20 38 20 72 65 73 70   2, 4 and 8 resp
38af2 65 63 74 69 76 65 6c 79 29 2e 0a 2a 2f 0a 73 74  ectively)..*/.st
38af3 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e  atic int writeIn
38af4 74 31 36 28 75 38 20 2a 70 2c 20 69 6e 74 20 69  t16(u8 *p, int i
38af5 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e  ){.  p[0] = (i>>
38af6 20 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d   8)&0xFF;.  p[1]
38af7 20 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b   = (i>> 0)&0xFF;
38af8 0a 20 20 72 65 74 75 72 6e 20 32 3b 0a 7d 0a 73  .  return 2;.}.s
38af9 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 43  tatic int writeC
38afa 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72 65  oord(u8 *p, Rtre
38afb 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29 7b  eCoord *pCoord){
38afc 0a 20 20 75 33 32 20 69 3b 0a 20 20 61 73 73 65  .  u32 i;.  asse
38afd 72 74 28 20 73 69 7a 65 6f 66 28 52 74 72 65 65  rt( sizeof(Rtree
38afe 43 6f 6f 72 64 29 3d 3d 34 20 29 3b 0a 20 20 61  Coord)==4 );.  a
38aff 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33  ssert( sizeof(u3
38b00 32 29 3d 3d 34 20 29 3b 0a 20 20 69 20 3d 20 2a  2)==4 );.  i = *
38b01 28 75 33 32 20 2a 29 70 43 6f 6f 72 64 3b 0a 20  (u32 *)pCoord;. 
38b02 20 70 5b 30 5d 20 3d 20 28 69 3e 3e 32 34 29 26   p[0] = (i>>24)&
38b03 30 78 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28  0xFF;.  p[1] = (
38b04 69 3e 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70  i>>16)&0xFF;.  p
38b05 5b 32 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [2] = (i>> 8)&0x
38b06 46 46 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 69 3e  FF;.  p[3] = (i>
38b07 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
38b08 75 72 6e 20 34 3b 0a 7d 0a 73 74 61 74 69 63 20  urn 4;.}.static 
38b09 69 6e 74 20 77 72 69 74 65 49 6e 74 36 34 28 75  int writeInt64(u
38b0a 38 20 2a 70 2c 20 69 36 34 20 69 29 7b 0a 20 20  8 *p, i64 i){.  
38b0b 70 5b 30 5d 20 3d 20 28 69 3e 3e 35 36 29 26 30  p[0] = (i>>56)&0
38b0c 78 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69  xFF;.  p[1] = (i
38b0d 3e 3e 34 38 29 26 30 78 46 46 3b 0a 20 20 70 5b  >>48)&0xFF;.  p[
38b0e 32 5d 20 3d 20 28 69 3e 3e 34 30 29 26 30 78 46  2] = (i>>40)&0xF
38b0f 46 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 69 3e 3e  F;.  p[3] = (i>>
38b10 33 32 29 26 30 78 46 46 3b 0a 20 20 70 5b 34 5d  32)&0xFF;.  p[4]
38b11 20 3d 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b   = (i>>24)&0xFF;
38b12 0a 20 20 70 5b 35 5d 20 3d 20 28 69 3e 3e 31 36  .  p[5] = (i>>16
38b13 29 26 30 78 46 46 3b 0a 20 20 70 5b 36 5d 20 3d  )&0xFF;.  p[6] =
38b14 20 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20   (i>> 8)&0xFF;. 
38b15 20 70 5b 37 5d 20 3d 20 28 69 3e 3e 20 30 29 26   p[7] = (i>> 0)&
38b16 30 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 38  0xFF;.  return 8
38b17 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65  ;.}../*.** Incre
38b18 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e  ment the referen
38b19 63 65 20 63 6f 75 6e 74 20 6f 66 20 6e 6f 64 65  ce count of node
38b1a 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f   p..*/.static vo
38b1b 69 64 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65  id nodeReference
38b1c 28 52 74 72 65 65 4e 6f 64 65 20 2a 70 29 7b 0a  (RtreeNode *p){.
38b1d 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70    if( p ){.    p
38b1e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 7d 0a  ->nRef++;.  }.}.
38b1f 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
38b20 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6e 6f 64 65   content of node
38b21 20 70 20 28 73 65 74 20 61 6c 6c 20 62 79 74 65   p (set all byte
38b22 73 20 74 6f 20 30 78 30 30 29 2e 0a 2a 2f 0a 73  s to 0x00)..*/.s
38b23 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 5a  tatic void nodeZ
38b24 65 72 6f 28 52 74 72 65 65 20 2a 70 52 74 72 65  ero(Rtree *pRtre
38b25 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 29  e, RtreeNode *p)
38b26 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
38b27 20 6d 65 6d 73 65 74 28 26 70 2d 3e 7a 44 61 74   memset(&p->zDat
38b28 61 5b 32 5d 2c 20 30 2c 20 70 52 74 72 65 65 2d  a[2], 0, pRtree-
38b29 3e 69 4e 6f 64 65 53 69 7a 65 2d 32 29 3b 0a 20  >iNodeSize-2);. 
38b2a 20 20 20 70 2d 3e 69 73 44 69 72 74 79 20 3d 20     p->isDirty = 
38b2b 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
38b2c 47 69 76 65 6e 20 61 20 6e 6f 64 65 20 6e 75 6d  Given a node num
38b2d 62 65 72 20 69 4e 6f 64 65 2c 20 72 65 74 75 72  ber iNode, retur
38b2e 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
38b2f 69 6e 67 20 6b 65 79 20 74 6f 20 75 73 65 0a 2a  ing key to use.*
38b30 2a 20 69 6e 20 74 68 65 20 52 74 72 65 65 2e 61  * in the Rtree.a
38b31 48 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  Hash table..*/.s
38b32 74 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 48 61  tatic int nodeHa
38b33 73 68 28 69 36 34 20 69 4e 6f 64 65 29 7b 0a 20  sh(i64 iNode){. 
38b34 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20 28 69   return (.    (i
38b35 4e 6f 64 65 3e 3e 35 36 29 20 5e 20 28 69 4e 6f  Node>>56) ^ (iNo
38b36 64 65 3e 3e 34 38 29 20 5e 20 28 69 4e 6f 64 65  de>>48) ^ (iNode
38b37 3e 3e 34 30 29 20 5e 20 28 69 4e 6f 64 65 3e 3e  >>40) ^ (iNode>>
38b38 33 32 29 20 5e 20 0a 20 20 20 20 28 69 4e 6f 64  32) ^ .    (iNod
38b39 65 3e 3e 32 34 29 20 5e 20 28 69 4e 6f 64 65 3e  e>>24) ^ (iNode>
38b3a 3e 31 36 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 20  >16) ^ (iNode>> 
38b3b 38 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 20 30 29  8) ^ (iNode>> 0)
38b3c 0a 20 20 29 20 25 20 48 41 53 48 53 49 5a 45 3b  .  ) % HASHSIZE;
38b3d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68  .}../*.** Search
38b3e 20 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74   the node hash t
38b3f 61 62 6c 65 20 66 6f 72 20 6e 6f 64 65 20 69 4e  able for node iN
38b40 6f 64 65 2e 20 49 66 20 66 6f 75 6e 64 2c 20 72  ode. If found, r
38b41 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
38b42 2a 2a 20 74 6f 20 69 74 2e 20 4f 74 68 65 72 77  ** to it. Otherw
38b43 69 73 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a  ise, return 0..*
38b44 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f  /.static RtreeNo
38b45 64 65 20 2a 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  de *nodeHashLook
38b46 75 70 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  up(Rtree *pRtree
38b47 2c 20 69 36 34 20 69 4e 6f 64 65 29 7b 0a 20 20  , i64 iNode){.  
38b48 52 74 72 65 65 4e 6f 64 65 20 2a 70 3b 0a 20 20  RtreeNode *p;.  
38b49 61 73 73 65 72 74 28 20 69 4e 6f 64 65 21 3d 30  assert( iNode!=0
38b4a 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 52 74 72   );.  for(p=pRtr
38b4b 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61  ee->aHash[nodeHa
38b4c 73 68 28 69 4e 6f 64 65 29 5d 3b 20 70 20 26 26  sh(iNode)]; p &&
38b4d 20 70 2d 3e 69 4e 6f 64 65 21 3d 69 4e 6f 64 65   p->iNode!=iNode
38b4e 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 3b 0a 20  ; p=p->pNext);. 
38b4f 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
38b50 0a 2a 2a 20 41 64 64 20 6e 6f 64 65 20 70 4e 6f  .** Add node pNo
38b51 64 65 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 68  de to the node h
38b52 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ash table..*/.st
38b53 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 48 61  atic void nodeHa
38b54 73 68 49 6e 73 65 72 74 28 52 74 72 65 65 20 2a  shInsert(Rtree *
38b55 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
38b56 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 66 28  e *pNode){.  if(
38b57 20 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e   pNode ){.    in
38b58 74 20 69 48 61 73 68 3b 0a 20 20 20 20 61 73 73  t iHash;.    ass
38b59 65 72 74 28 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  ert( pNode->pNex
38b5a 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69 48 61 73  t==0 );.    iHas
38b5b 68 20 3d 20 6e 6f 64 65 48 61 73 68 28 70 4e 6f  h = nodeHash(pNo
38b5c 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
38b5d 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70  pNode->pNext = p
38b5e 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61  Rtree->aHash[iHa
38b5f 73 68 5d 3b 0a 20 20 20 20 70 52 74 72 65 65 2d  sh];.    pRtree-
38b60 3e 61 48 61 73 68 5b 69 48 61 73 68 5d 20 3d 20  >aHash[iHash] = 
38b61 70 4e 6f 64 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  pNode;.  }.}../*
38b62 0a 2a 2a 20 52 65 6d 6f 76 65 20 6e 6f 64 65 20  .** Remove node 
38b63 70 4e 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 6e  pNode from the n
38b64 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ode hash table..
38b65 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
38b66 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28 52 74  odeHashDelete(Rt
38b67 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
38b68 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
38b69 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70 70    RtreeNode **pp
38b6a 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69  ;.  if( pNode->i
38b6b 4e 6f 64 65 21 3d 30 20 29 7b 0a 20 20 20 20 70  Node!=0 ){.    p
38b6c 70 20 3d 20 26 70 52 74 72 65 65 2d 3e 61 48 61  p = &pRtree->aHa
38b6d 73 68 5b 6e 6f 64 65 48 61 73 68 28 70 4e 6f 64  sh[nodeHash(pNod
38b6e 65 2d 3e 69 4e 6f 64 65 29 5d 3b 0a 20 20 20 20  e->iNode)];.    
38b6f 66 6f 72 28 20 3b 20 28 2a 70 70 29 21 3d 70 4e  for( ; (*pp)!=pN
38b70 6f 64 65 3b 20 70 70 20 3d 20 26 28 2a 70 70 29  ode; pp = &(*pp)
38b71 2d 3e 70 4e 65 78 74 29 7b 20 61 73 73 65 72 74  ->pNext){ assert
38b72 28 2a 70 70 29 3b 20 7d 0a 20 20 20 20 2a 70 70  (*pp); }.    *pp
38b73 20 3d 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 3b   = pNode->pNext;
38b74 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  .    pNode->pNex
38b75 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  t = 0;.  }.}../*
38b76 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64  .** Allocate and
38b77 20 72 65 74 75 72 6e 20 6e 65 77 20 72 2d 74 72   return new r-tr
38b78 65 65 20 6e 6f 64 65 2e 20 49 6e 69 74 69 61 6c  ee node. Initial
38b79 6c 79 2c 20 28 52 74 72 65 65 4e 6f 64 65 2e 69  ly, (RtreeNode.i
38b7a 4e 6f 64 65 3d 3d 30 29 2c 0a 2a 2a 20 69 6e 64  Node==0),.** ind
38b7b 69 63 61 74 69 6e 67 20 74 68 61 74 20 6e 6f 64  icating that nod
38b7c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  e has not yet be
38b7d 65 6e 20 61 73 73 69 67 6e 65 64 20 61 20 6e 6f  en assigned a no
38b7e 64 65 20 6e 75 6d 62 65 72 2e 20 49 74 20 69 73  de number. It is
38b7f 0a 2a 2a 20 61 73 73 69 67 6e 65 64 20 61 20 6e  .** assigned a n
38b80 6f 64 65 20 6e 75 6d 62 65 72 20 77 68 65 6e 20  ode number when 
38b81 6e 6f 64 65 57 72 69 74 65 28 29 20 69 73 20 63  nodeWrite() is c
38b82 61 6c 6c 65 64 20 74 6f 20 77 72 69 74 65 20 74  alled to write t
38b83 68 65 0a 2a 2a 20 6e 6f 64 65 20 63 6f 6e 74 65  he.** node conte
38b84 6e 74 73 20 6f 75 74 20 74 6f 20 74 68 65 20 64  nts out to the d
38b85 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74  atabase..*/.stat
38b86 69 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f  ic RtreeNode *no
38b87 64 65 4e 65 77 28 52 74 72 65 65 20 2a 70 52 74  deNew(Rtree *pRt
38b88 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a  ree, RtreeNode *
38b89 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 7a 65 72  pParent, int zer
38b8a 6f 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  o){.  RtreeNode 
38b8b 2a 70 4e 6f 64 65 3b 0a 20 20 70 4e 6f 64 65 20  *pNode;.  pNode 
38b8c 3d 20 28 52 74 72 65 65 4e 6f 64 65 20 2a 29 73  = (RtreeNode *)s
38b8d 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
38b8e 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 20  zeof(RtreeNode) 
38b8f 2b 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  + pRtree->iNodeS
38b90 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 4e 6f 64  ize);.  if( pNod
38b91 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  e ){.    memset(
38b92 70 4e 6f 64 65 2c 20 30 2c 20 73 69 7a 65 6f 66  pNode, 0, sizeof
38b93 28 52 74 72 65 65 4e 6f 64 65 29 20 2b 20 28 7a  (RtreeNode) + (z
38b94 65 72 6f 3f 70 52 74 72 65 65 2d 3e 69 4e 6f 64  ero?pRtree->iNod
38b95 65 53 69 7a 65 3a 30 29 29 3b 0a 20 20 20 20 70  eSize:0));.    p
38b96 4e 6f 64 65 2d 3e 7a 44 61 74 61 20 3d 20 28 75  Node->zData = (u
38b97 38 20 2a 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a 20  8 *)&pNode[1];. 
38b98 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 20 3d     pNode->nRef =
38b99 20 31 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70   1;.    pNode->p
38b9a 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74  Parent = pParent
38b9b 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44  ;.    pNode->isD
38b9c 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 6e 6f  irty = 1;.    no
38b9d 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61 72  deReference(pPar
38b9e 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ent);.  }.  retu
38b9f 72 6e 20 70 4e 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a  rn pNode;.}../*.
38ba0 2a 2a 20 4f 62 74 61 69 6e 20 61 20 72 65 66 65  ** Obtain a refe
38ba1 72 65 6e 63 65 20 74 6f 20 61 6e 20 72 2d 74 72  rence to an r-tr
38ba2 65 65 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  ee node..*/.stat
38ba3 69 63 20 69 6e 74 0a 6e 6f 64 65 41 63 71 75 69  ic int.nodeAcqui
38ba4 72 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  re(.  Rtree *pRt
38ba5 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ree,            
38ba6 20 2f 2a 20 52 2d 74 72 65 65 20 73 74 72 75 63   /* R-tree struc
38ba7 74 75 72 65 20 2a 2f 0a 20 20 69 36 34 20 69 4e  ture */.  i64 iN
38ba8 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ode,            
38ba9 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6e 75 6d       /* Node num
38baa 62 65 72 20 74 6f 20 6c 6f 61 64 20 2a 2f 0a 20  ber to load */. 
38bab 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
38bac 65 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 45  ent,        /* E
38bad 69 74 68 65 72 20 74 68 65 20 70 61 72 65 6e 74  ither the parent
38bae 20 6e 6f 64 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f   node or NULL */
38baf 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  .  RtreeNode **p
38bb0 70 4e 6f 64 65 20 20 20 20 20 20 20 20 20 2f 2a  pNode         /*
38bb1 20 4f 55 54 3a 20 41 63 71 75 69 72 65 64 20 6e   OUT: Acquired n
38bb2 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ode */.){.  int 
38bb3 72 63 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  rc;.  RtreeNode 
38bb4 2a 70 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20 43 68  *pNode;..  /* Ch
38bb5 65 63 6b 20 69 66 20 74 68 65 20 72 65 71 75 65  eck if the reque
38bb6 73 74 65 64 20 6e 6f 64 65 20 69 73 20 61 6c 72  sted node is alr
38bb7 65 61 64 79 20 69 6e 20 74 68 65 20 68 61 73 68  eady in the hash
38bb8 20 74 61 62 6c 65 2e 20 49 66 20 73 6f 2c 0a 20   table. If so,. 
38bb9 20 2a 2a 20 69 6e 63 72 65 61 73 65 20 69 74 73   ** increase its
38bba 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
38bbb 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 2e 0a   and return it..
38bbc 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 4e 6f 64    */.  if( (pNod
38bbd 65 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  e = nodeHashLook
38bbe 75 70 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65  up(pRtree, iNode
38bbf 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  )) ){.    assert
38bc0 28 20 21 70 50 61 72 65 6e 74 20 7c 7c 20 21 70  ( !pParent || !p
38bc1 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 7c 7c  Node->pParent ||
38bc2 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3d   pNode->pParent=
38bc3 3d 70 50 61 72 65 6e 74 20 29 3b 0a 20 20 20 20  =pParent );.    
38bc4 69 66 28 20 70 50 61 72 65 6e 74 20 26 26 20 21  if( pParent && !
38bc5 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 29  pNode->pParent )
38bc6 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65  {.      nodeRefe
38bc7 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
38bc8 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e 70 50 61        pNode->pPa
38bc9 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74 3b 0a  rent = pParent;.
38bca 20 20 20 20 7d 0a 20 20 20 20 70 4e 6f 64 65 2d      }.    pNode-
38bcb 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 2a 70 70  >nRef++;.    *pp
38bcc 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  Node = pNode;.  
38bcd 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
38bce 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4e 6f 64 65  OK;.  }..  pNode
38bcf 20 3d 20 28 52 74 72 65 65 4e 6f 64 65 20 2a 29   = (RtreeNode *)
38bd0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
38bd1 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29  izeof(RtreeNode)
38bd2 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65   + pRtree->iNode
38bd3 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 70 4e  Size);.  if( !pN
38bd4 6f 64 65 20 29 7b 0a 20 20 20 20 2a 70 70 4e 6f  ode ){.    *ppNo
38bd5 64 65 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  de = 0;.    retu
38bd6 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
38bd7 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 70 50  .  }.  pNode->pP
38bd8 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74 3b  arent = pParent;
38bd9 0a 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 20  .  pNode->zData 
38bda 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b 31  = (u8 *)&pNode[1
38bdb 5d 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  ];.  pNode->nRef
38bdc 20 3d 20 31 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69   = 1;.  pNode->i
38bdd 4e 6f 64 65 20 3d 20 69 4e 6f 64 65 3b 0a 20 20  Node = iNode;.  
38bde 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
38bdf 20 30 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 4e 65   0;.  pNode->pNe
38be0 78 74 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74  xt = 0;..  sqlit
38be1 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
38be2 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 2c  tree->pReadNode,
38be3 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 72 63   1, iNode);.  rc
38be4 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
38be5 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
38be6 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
38be7 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
38be8 63 6f 6e 73 74 20 75 38 20 2a 7a 42 6c 6f 62 20  const u8 *zBlob 
38be9 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
38bea 5f 62 6c 6f 62 28 70 52 74 72 65 65 2d 3e 70 52  _blob(pRtree->pR
38beb 65 61 64 4e 6f 64 65 2c 20 30 29 3b 0a 20 20 20  eadNode, 0);.   
38bec 20 6d 65 6d 63 70 79 28 70 4e 6f 64 65 2d 3e 7a   memcpy(pNode->z
38bed 44 61 74 61 2c 20 7a 42 6c 6f 62 2c 20 70 52 74  Data, zBlob, pRt
38bee 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
38bef 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
38bf0 63 65 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d  ce(pParent);.  }
38bf1 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
38bf2 33 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a 20  3_free(pNode);. 
38bf3 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20     pNode = 0;.  
38bf4 7d 0a 0a 20 20 2a 70 70 4e 6f 64 65 20 3d 20 70  }..  *ppNode = p
38bf5 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Node;.  rc = sql
38bf6 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
38bf7 65 2d 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 0a  e->pReadNode);..
38bf8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
38bf9 5f 4f 4b 20 26 26 20 69 4e 6f 64 65 3d 3d 31 20  _OK && iNode==1 
38bfa 29 7b 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69  ){.    pRtree->i
38bfb 44 65 70 74 68 20 3d 20 72 65 61 64 49 6e 74 31  Depth = readInt1
38bfc 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 29 3b  6(pNode->zData);
38bfd 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
38bfe 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26  (rc==SQLITE_OK &
38bff 26 20 70 4e 6f 64 65 29 20 7c 7c 20 28 70 4e 6f  & pNode) || (pNo
38c00 64 65 3d 3d 30 20 26 26 20 72 63 21 3d 53 51 4c  de==0 && rc!=SQL
38c01 49 54 45 5f 4f 4b 29 20 29 3b 0a 20 20 6e 6f 64  ITE_OK) );.  nod
38c02 65 48 61 73 68 49 6e 73 65 72 74 28 70 52 74 72  eHashInsert(pRtr
38c03 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 72  ee, pNode);..  r
38c04 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
38c05 2a 2a 20 4f 76 65 72 77 72 69 74 65 20 63 65 6c  ** Overwrite cel
38c06 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20  l iCell of node 
38c07 70 4e 6f 64 65 20 77 69 74 68 20 74 68 65 20 63  pNode with the c
38c08 6f 6e 74 65 6e 74 73 20 6f 66 20 70 43 65 6c 6c  ontents of pCell
38c09 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
38c0a 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65   nodeOverwriteCe
38c0b 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ll(.  Rtree *pRt
38c0c 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64  ree, .  RtreeNod
38c0d 65 20 2a 70 4e 6f 64 65 2c 20 20 0a 20 20 52 74  e *pNode,  .  Rt
38c0e 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20  reeCell *pCell, 
38c0f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a 29 7b 0a  .  int iCell.){.
38c10 20 20 69 6e 74 20 69 69 3b 0a 20 20 75 38 20 2a    int ii;.  u8 *
38c11 70 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  p = &pNode->zDat
38c12 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  a[4 + pRtree->nB
38c13 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
38c14 6c 5d 3b 0a 20 20 70 20 2b 3d 20 77 72 69 74 65  l];.  p += write
38c15 49 6e 74 36 34 28 70 2c 20 70 43 65 6c 6c 2d 3e  Int64(p, pCell->
38c16 69 52 6f 77 69 64 29 3b 0a 20 20 66 6f 72 28 69  iRowid);.  for(i
38c17 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d  i=0; ii<(pRtree-
38c18 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 2b 29 7b  >nDim*2); ii++){
38c19 0a 20 20 20 20 70 20 2b 3d 20 77 72 69 74 65 43  .    p += writeC
38c1a 6f 6f 72 64 28 70 2c 20 26 70 43 65 6c 6c 2d 3e  oord(p, &pCell->
38c1b 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d  aCoord[ii]);.  }
38c1c 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74  .  pNode->isDirt
38c1d 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  y = 1;.}../*.** 
38c1e 52 65 6d 6f 76 65 20 63 65 6c 6c 20 74 68 65 20  Remove cell the 
38c1f 63 65 6c 6c 20 77 69 74 68 20 69 6e 64 65 78 20  cell with index 
38c20 69 43 65 6c 6c 20 66 72 6f 6d 20 6e 6f 64 65 20  iCell from node 
38c21 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  pNode..*/.static
38c22 20 76 6f 69 64 20 6e 6f 64 65 44 65 6c 65 74 65   void nodeDelete
38c23 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52 74 72  Cell(Rtree *pRtr
38c24 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
38c25 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29  Node, int iCell)
38c26 7b 0a 20 20 75 38 20 2a 70 44 73 74 20 3d 20 26  {.  u8 *pDst = &
38c27 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b  pNode->zData[4 +
38c28 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
38c29 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20  erCell*iCell];. 
38c2a 20 75 38 20 2a 70 53 72 63 20 3d 20 26 70 44 73   u8 *pSrc = &pDs
38c2b 74 5b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73  t[pRtree->nBytes
38c2c 50 65 72 43 65 6c 6c 5d 3b 0a 20 20 69 6e 74 20  PerCell];.  int 
38c2d 6e 42 79 74 65 20 3d 20 28 4e 43 45 4c 4c 28 70  nByte = (NCELL(p
38c2e 4e 6f 64 65 29 20 2d 20 69 43 65 6c 6c 20 2d 20  Node) - iCell - 
38c2f 31 29 20 2a 20 70 52 74 72 65 65 2d 3e 6e 42 79  1) * pRtree->nBy
38c30 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6d 65  tesPerCell;.  me
38c31 6d 6d 6f 76 65 28 70 44 73 74 2c 20 70 53 72 63  mmove(pDst, pSrc
38c32 2c 20 6e 42 79 74 65 29 3b 0a 20 20 77 72 69 74  , nByte);.  writ
38c33 65 49 6e 74 31 36 28 26 70 4e 6f 64 65 2d 3e 7a  eInt16(&pNode->z
38c34 44 61 74 61 5b 32 5d 2c 20 4e 43 45 4c 4c 28 70  Data[2], NCELL(p
38c35 4e 6f 64 65 29 2d 31 29 3b 0a 20 20 70 4e 6f 64  Node)-1);.  pNod
38c36 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a  e->isDirty = 1;.
38c37 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
38c38 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
38c39 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e 74 6f 20  cell pCell into 
38c3a 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20 74  node pNode. If t
38c3b 68 65 20 69 6e 73 65 72 74 0a 2a 2a 20 69 73 20  he insert.** is 
38c3c 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75  successful, retu
38c3d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  rn SQLITE_OK..**
38c3e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
38c3f 6e 6f 74 20 65 6e 6f 75 67 68 20 66 72 65 65 20  not enough free 
38c40 73 70 61 63 65 20 69 6e 20 70 4e 6f 64 65 2c 20  space in pNode, 
38c41 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
38c42 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  LL..*/.static in
38c43 74 0a 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c  t.nodeInsertCell
38c44 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
38c45 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e, .  RtreeNode 
38c46 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74 72 65 65  *pNode, .  Rtree
38c47 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 0a 29 7b 0a  Cell *pCell .){.
38c48 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20    int nCell;    
38c49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38c4a 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65  /* Current numbe
38c4b 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 70 4e  r of cells in pN
38c4c 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61  ode */.  int nMa
38c4d 78 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  xCell;          
38c4e 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
38c4f 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  m number of cell
38c50 73 20 66 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a 0a  s for pNode */..
38c51 20 20 6e 4d 61 78 43 65 6c 6c 20 3d 20 28 70 52    nMaxCell = (pR
38c52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d  tree->iNodeSize-
38c53 34 29 2f 70 52 74 72 65 65 2d 3e 6e 42 79 74 65  4)/pRtree->nByte
38c54 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6e 43 65 6c  sPerCell;.  nCel
38c55 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
38c56 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 43 65 6c  ;..  assert(nCel
38c57 6c 3c 3d 6e 4d 61 78 43 65 6c 6c 29 3b 0a 0a 20  l<=nMaxCell);.. 
38c58 20 69 66 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43   if( nCell<nMaxC
38c59 65 6c 6c 20 29 7b 0a 20 20 20 20 6e 6f 64 65 4f  ell ){.    nodeO
38c5a 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74  verwriteCell(pRt
38c5b 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
38c5c 6c 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 20 20 77  l, nCell);.    w
38c5d 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f 64 65  riteInt16(&pNode
38c5e 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 6e 43 65 6c  ->zData[2], nCel
38c5f 6c 2b 31 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  l+1);.    pNode-
38c60 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20  >isDirty = 1;.  
38c61 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 6e 43 65  }..  return (nCe
38c62 6c 6c 3d 3d 6e 4d 61 78 43 65 6c 6c 29 3b 0a 7d  ll==nMaxCell);.}
38c63 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ../*.** If the n
38c64 6f 64 65 20 69 73 20 64 69 72 74 79 2c 20 77 72  ode is dirty, wr
38c65 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 74 68  ite it out to th
38c66 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73  e database..*/.s
38c67 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65 57 72  tatic int.nodeWr
38c68 69 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65  ite(Rtree *pRtre
38c69 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  e, RtreeNode *pN
38c6a 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ode){.  int rc =
38c6b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
38c6c 28 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79  ( pNode->isDirty
38c6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
38c6e 73 74 6d 74 20 2a 70 20 3d 20 70 52 74 72 65 65  stmt *p = pRtree
38c6f 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20  ->pWriteNode;.  
38c70 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
38c71 64 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  de ){.      sqli
38c72 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
38c73 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  , 1, pNode->iNod
38c74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
38c75 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e       sqlite3_bin
38c76 64 5f 6e 75 6c 6c 28 70 2c 20 31 29 3b 0a 20 20  d_null(p, 1);.  
38c77 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
38c78 62 69 6e 64 5f 62 6c 6f 62 28 70 2c 20 32 2c 20  bind_blob(p, 2, 
38c79 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 70 52  pNode->zData, pR
38c7a 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2c  tree->iNodeSize,
38c7b 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
38c7c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65  .    sqlite3_ste
38c7d 70 28 70 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  p(p);.    pNode-
38c7e 3e 69 73 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  >isDirty = 0;.  
38c7f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
38c80 65 73 65 74 28 70 29 3b 0a 20 20 20 20 69 66 28  eset(p);.    if(
38c81 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 30   pNode->iNode==0
38c82 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
38c83 4b 20 29 7b 0a 20 20 20 20 20 20 70 4e 6f 64 65  K ){.      pNode
38c84 2d 3e 69 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65  ->iNode = sqlite
38c85 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
38c86 77 69 64 28 70 52 74 72 65 65 2d 3e 64 62 29 3b  wid(pRtree->db);
38c87 0a 20 20 20 20 20 20 6e 6f 64 65 48 61 73 68 49  .      nodeHashI
38c88 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
38c89 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ode);.    }.  }.
38c8a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
38c8b 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20  /*.** Release a 
38c8c 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 6e  reference to a n
38c8d 6f 64 65 2e 20 49 66 20 74 68 65 20 6e 6f 64 65  ode. If the node
38c8e 20 69 73 20 64 69 72 74 79 20 61 6e 64 20 74 68   is dirty and th
38c8f 65 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 63  e reference.** c
38c90 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f 20 7a 65  ount drops to ze
38c91 72 6f 2c 20 74 68 65 20 6e 6f 64 65 20 64 61 74  ro, the node dat
38c92 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  a is written to 
38c93 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  the database..*/
38c94 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
38c95 52 65 6c 65 61 73 65 28 52 74 72 65 65 20 2a 70  Release(Rtree *p
38c96 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
38c97 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20   *pNode){.  int 
38c98 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
38c99 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
38c9a 20 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65     assert( pNode
38c9b 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20  ->nRef>0 );.    
38c9c 70 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  pNode->nRef--;. 
38c9d 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 6e 52     if( pNode->nR
38c9e 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  ef==0 ){.      i
38c9f 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d  f( pNode->iNode=
38ca0 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52  =1 ){.        pR
38ca1 74 72 65 65 2d 3e 69 44 65 70 74 68 20 3d 20 2d  tree->iDepth = -
38ca2 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
38ca3 20 69 66 28 20 70 4e 6f 64 65 2d 3e 70 50 61 72   if( pNode->pPar
38ca4 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ent ){.        r
38ca5 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
38ca6 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2d 3e 70  pRtree, pNode->p
38ca7 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 7d  Parent);.      }
38ca8 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
38ca9 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38caa 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 57 72 69      rc = nodeWri
38cab 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
38cac 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
38cad 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28   nodeHashDelete(
38cae 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
38caf 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
38cb0 65 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d  ee(pNode);.    }
38cb1 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
38cb2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
38cb3 6e 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74  n the 64-bit int
38cb4 65 67 65 72 20 76 61 6c 75 65 20 61 73 73 6f 63  eger value assoc
38cb5 69 61 74 65 64 20 77 69 74 68 20 63 65 6c 6c 20  iated with cell 
38cb6 69 43 65 6c 6c 20 6f 66 0a 2a 2a 20 6e 6f 64 65  iCell of.** node
38cb7 20 70 4e 6f 64 65 2e 20 49 66 20 70 4e 6f 64 65   pNode. If pNode
38cb8 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c   is a leaf node,
38cb9 20 74 68 69 73 20 69 73 20 61 20 72 6f 77 69 64   this is a rowid
38cba 2e 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 61 6e  . If it is.** an
38cbb 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2c 20   internal node, 
38cbc 74 68 65 6e 20 74 68 65 20 36 34 2d 62 69 74 20  then the 64-bit 
38cbd 69 6e 74 65 67 65 72 20 69 73 20 61 20 63 68 69  integer is a chi
38cbe 6c 64 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a  ld page number..
38cbf 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6e 6f  */.static i64 no
38cc0 64 65 47 65 74 52 6f 77 69 64 28 0a 20 20 52 74  deGetRowid(.  Rt
38cc1 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
38cc2 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
38cc3 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a 29  , .  int iCell.)
38cc4 7b 0a 20 20 61 73 73 65 72 74 28 20 69 43 65 6c  {.  assert( iCel
38cc5 6c 3c 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 29  l<NCELL(pNode) )
38cc6 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 61 64 49  ;.  return readI
38cc7 6e 74 36 34 28 26 70 4e 6f 64 65 2d 3e 7a 44 61  nt64(&pNode->zDa
38cc8 74 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e  ta[4 + pRtree->n
38cc9 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65  BytesPerCell*iCe
38cca 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ll]);.}../*.** R
38ccb 65 74 75 72 6e 20 63 6f 6f 72 64 69 6e 61 74 65  eturn coordinate
38ccc 20 69 43 6f 6f 72 64 20 66 72 6f 6d 20 63 65 6c   iCoord from cel
38ccd 6c 20 69 43 65 6c 6c 20 69 6e 20 6e 6f 64 65 20  l iCell in node 
38cce 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  pNode..*/.static
38ccf 20 76 6f 69 64 20 6e 6f 64 65 47 65 74 43 6f 6f   void nodeGetCoo
38cd0 72 64 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  rd(.  Rtree *pRt
38cd1 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64  ree, .  RtreeNod
38cd2 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74  e *pNode, .  int
38cd3 20 69 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69 43   iCell,.  int iC
38cd4 6f 6f 72 64 2c 0a 20 20 52 74 72 65 65 43 6f 6f  oord,.  RtreeCoo
38cd5 72 64 20 2a 70 43 6f 6f 72 64 20 20 20 20 20 20  rd *pCoord      
38cd6 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f       /* Space to
38cd7 20 77 72 69 74 65 20 72 65 73 75 6c 74 20 74 6f   write result to
38cd8 20 2a 2f 0a 29 7b 0a 20 20 72 65 61 64 43 6f 6f   */.){.  readCoo
38cd9 72 64 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61  rd(&pNode->zData
38cda 5b 31 32 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  [12 + pRtree->nB
38cdb 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
38cdc 6c 20 2b 20 34 2a 69 43 6f 6f 72 64 5d 2c 20 70  l + 4*iCoord], p
38cdd 43 6f 6f 72 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Coord);.}../*.**
38cde 20 44 65 73 65 72 69 61 6c 69 7a 65 20 63 65 6c   Deserialize cel
38cdf 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20  l iCell of node 
38ce0 70 4e 6f 64 65 2e 20 50 6f 70 75 6c 61 74 65 20  pNode. Populate 
38ce1 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70 6f  the structure po
38ce2 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70  inted.** to by p
38ce3 43 65 6c 6c 20 77 69 74 68 20 74 68 65 20 72 65  Cell with the re
38ce4 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  sults..*/.static
38ce5 20 76 6f 69 64 20 6e 6f 64 65 47 65 74 43 65 6c   void nodeGetCel
38ce6 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  l(.  Rtree *pRtr
38ce7 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65  ee, .  RtreeNode
38ce8 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20   *pNode, .  int 
38ce9 69 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 43 65  iCell,.  RtreeCe
38cea 6c 6c 20 2a 70 43 65 6c 6c 0a 29 7b 0a 20 20 69  ll *pCell.){.  i
38ceb 6e 74 20 69 69 3b 0a 20 20 70 43 65 6c 6c 2d 3e  nt ii;.  pCell->
38cec 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74  iRowid = nodeGet
38ced 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 4e  Rowid(pRtree, pN
38cee 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 66  ode, iCell);.  f
38cef 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72  or(ii=0; ii<pRtr
38cf0 65 65 2d 3e 6e 44 69 6d 2a 32 3b 20 69 69 2b 2b  ee->nDim*2; ii++
38cf1 29 7b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 6f  ){.    nodeGetCo
38cf2 6f 72 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ord(pRtree, pNod
38cf3 65 2c 20 69 43 65 6c 6c 2c 20 69 69 2c 20 26 70  e, iCell, ii, &p
38cf4 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d  Cell->aCoord[ii]
38cf5 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 46 6f  );.  }.}.../* Fo
38cf6 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
38cf7 6e 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69  n for the functi
38cf8 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 74 68 65  on that does the
38cf9 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 74 68 65 20   work of.** the 
38cfa 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
38cfb 64 75 6c 65 20 78 43 72 65 61 74 65 28 29 20 61  dule xCreate() a
38cfc 6e 64 20 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65  nd xConnect() me
38cfd 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  thods..*/.static
38cfe 20 69 6e 74 20 72 74 72 65 65 49 6e 69 74 28 0a   int rtreeInit(.
38cff 20 20 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69    sqlite3 *, voi
38d00 64 20 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  d *, int, const 
38d01 63 68 61 72 20 2a 63 6f 6e 73 74 2a 2c 20 73 71  char *const*, sq
38d02 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 2c 20 63  lite3_vtab **, c
38d03 68 61 72 20 2a 2a 2c 20 69 6e 74 0a 29 3b 0a 0a  har **, int.);..
38d04 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  /* .** Rtree vir
38d05 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
38d06 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64  e xCreate method
38d07 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
38d08 72 74 72 65 65 43 72 65 61 74 65 28 0a 20 20 73  rtreeCreate(.  s
38d09 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f  qlite3 *db,.  vo
38d0a 69 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20  id *pAux,.  int 
38d0b 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
38d0c 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20   *const*argv,.  
38d0d 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
38d0e 70 56 74 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a  pVtab,.  char **
38d0f 70 7a 45 72 72 0a 29 7b 0a 20 20 72 65 74 75 72  pzErr.){.  retur
38d10 6e 20 72 74 72 65 65 49 6e 69 74 28 64 62 2c 20  n rtreeInit(db, 
38d11 70 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76  pAux, argc, argv
38d12 2c 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72 2c  , ppVtab, pzErr,
38d13 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52   1);.}../* .** R
38d14 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
38d15 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6f 6e 6e 65  le module xConne
38d16 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74  ct method..*/.st
38d17 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 43 6f  atic int rtreeCo
38d18 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33  nnect(.  sqlite3
38d19 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41   *db,.  void *pA
38d1a 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  ux,.  int argc, 
38d1b 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
38d1c 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65  t*argv,.  sqlite
38d1d 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
38d1e 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a  .  char **pzErr.
38d1f 29 7b 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65  ){.  return rtre
38d20 65 49 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20  eInit(db, pAux, 
38d21 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74  argc, argv, ppVt
38d22 61 62 2c 20 70 7a 45 72 72 2c 20 30 29 3b 0a 7d  ab, pzErr, 0);.}
38d23 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e  ../*.** Incremen
38d24 74 20 74 68 65 20 72 2d 74 72 65 65 20 72 65 66  t the r-tree ref
38d25 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 0a 2a 2f  erence count..*/
38d26 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 74 72  .static void rtr
38d27 65 65 52 65 66 65 72 65 6e 63 65 28 52 74 72 65  eeReference(Rtre
38d28 65 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 70 52  e *pRtree){.  pR
38d29 74 72 65 65 2d 3e 6e 42 75 73 79 2b 2b 3b 0a 7d  tree->nBusy++;.}
38d2a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ../*.** Decremen
38d2b 74 20 74 68 65 20 72 2d 74 72 65 65 20 72 65 66  t the r-tree ref
38d2c 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 57 68  erence count. Wh
38d2d 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  en the reference
38d2e 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 0a 2a   count reaches.*
38d2f 2a 20 7a 65 72 6f 20 74 68 65 20 73 74 72 75 63  * zero the struc
38d30 74 75 72 65 20 69 73 20 64 65 6c 65 74 65 64 2e  ture is deleted.
38d31 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
38d32 72 74 72 65 65 52 65 6c 65 61 73 65 28 52 74 72  rtreeRelease(Rtr
38d33 65 65 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 70  ee *pRtree){.  p
38d34 52 74 72 65 65 2d 3e 6e 42 75 73 79 2d 2d 3b 0a  Rtree->nBusy--;.
38d35 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e 6e 42    if( pRtree->nB
38d36 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  usy==0 ){.    sq
38d37 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
38d38 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65  Rtree->pReadNode
38d39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
38d3a 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
38d3b 70 57 72 69 74 65 4e 6f 64 65 29 3b 0a 20 20 20  pWriteNode);.   
38d3c 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
38d3d 65 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  e(pRtree->pDelet
38d3e 65 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  eNode);.    sqli
38d3f 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74  te3_finalize(pRt
38d40 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29  ree->pReadRowid)
38d41 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
38d42 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
38d43 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 20  WriteRowid);.   
38d44 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
38d45 65 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  e(pRtree->pDelet
38d46 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  eRowid);.    sql
38d47 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
38d48 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
38d49 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
38d4a 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d  finalize(pRtree-
38d4b 3e 70 57 72 69 74 65 50 61 72 65 6e 74 29 3b 0a  >pWriteParent);.
38d4c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
38d4d 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
38d4e 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 20  leteParent);.   
38d4f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 52   sqlite3_free(pR
38d50 74 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  tree);.  }.}../*
38d51 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75   .** Rtree virtu
38d52 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20  al table module 
38d53 78 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 74 68  xDisconnect meth
38d54 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
38d55 74 20 72 74 72 65 65 44 69 73 63 6f 6e 6e 65 63  t rtreeDisconnec
38d56 74 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  t(sqlite3_vtab *
38d57 70 56 74 61 62 29 7b 0a 20 20 72 74 72 65 65 52  pVtab){.  rtreeR
38d58 65 6c 65 61 73 65 28 28 52 74 72 65 65 20 2a 29  elease((Rtree *)
38d59 70 56 74 61 62 29 3b 0a 20 20 72 65 74 75 72 6e  pVtab);.  return
38d5a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
38d5b 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
38d5c 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
38d5d 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64   xDestroy method
38d5e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
38d5f 72 74 72 65 65 44 65 73 74 72 6f 79 28 73 71 6c  rtreeDestroy(sql
38d60 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
38d61 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
38d62 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56  ee = (Rtree *)pV
38d63 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  tab;.  int rc;. 
38d64 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65 20 3d   char *zCreate =
38d65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
38d66 28 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c  (.    "DROP TABL
38d67 45 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27  E '%q'.'%q_node'
38d68 3b 22 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42  ;".    "DROP TAB
38d69 4c 45 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69  LE '%q'.'%q_rowi
38d6a 64 27 3b 22 0a 20 20 20 20 22 44 52 4f 50 20 54  d';".    "DROP T
38d6b 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 70 61  ABLE '%q'.'%q_pa
38d6c 72 65 6e 74 27 3b 22 2c 0a 20 20 20 20 70 52 74  rent';",.    pRt
38d6d 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65  ree->zDb, pRtree
38d6e 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 70 52  ->zName, .    pR
38d6f 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65  tree->zDb, pRtre
38d70 65 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 70 52  e->zName,.    pR
38d71 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65  tree->zDb, pRtre
38d72 65 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b 0a 20 20  e->zName.  );.  
38d73 69 66 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a  if( !zCreate ){.
38d74 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
38d75 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NOMEM;.  }else{.
38d76 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
38d77 5f 65 78 65 63 28 70 52 74 72 65 65 2d 3e 64 62  _exec(pRtree->db
38d78 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c 20 30 2c  , zCreate, 0, 0,
38d79 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
38d7a 5f 66 72 65 65 28 7a 43 72 65 61 74 65 29 3b 0a  _free(zCreate);.
38d7b 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
38d7c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
38d7d 74 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72  treeRelease(pRtr
38d7e 65 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ee);.  }..  retu
38d7f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  rn rc;.}../* .**
38d80 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
38d81 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 4f 70 65  able module xOpe
38d82 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  n method..*/.sta
38d83 74 69 63 20 69 6e 74 20 72 74 72 65 65 4f 70 65  tic int rtreeOpe
38d84 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  n(sqlite3_vtab *
38d85 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76  pVTab, sqlite3_v
38d86 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43  tab_cursor **ppC
38d87 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74 20 72 63  ursor){.  int rc
38d88 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
38d89 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
38d8a 70 43 73 72 3b 0a 0a 20 20 70 43 73 72 20 3d 20  pCsr;..  pCsr = 
38d8b 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 73  (RtreeCursor *)s
38d8c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
38d8d 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73 6f 72  zeof(RtreeCursor
38d8e 29 29 3b 0a 20 20 69 66 28 20 70 43 73 72 20 29  ));.  if( pCsr )
38d8f 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 43 73  {.    memset(pCs
38d90 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72  r, 0, sizeof(Rtr
38d91 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20  eeCursor));.    
38d92 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
38d93 20 3d 20 70 56 54 61 62 3b 0a 20 20 20 20 72 63   = pVTab;.    rc
38d94 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
38d95 7d 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20  }.  *ppCursor = 
38d96 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
38d97 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a 0a 20 20  rsor *)pCsr;..  
38d98 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
38d99 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75   .** Rtree virtu
38d9a 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20  al table module 
38d9b 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 2e 0a 2a  xClose method..*
38d9c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
38d9d 65 65 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  eeClose(sqlite3_
38d9e 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72  vtab_cursor *cur
38d9f 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
38da0 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 28 63  ee = (Rtree *)(c
38da1 75 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20 69 6e  ur->pVtab);.  in
38da2 74 20 72 63 3b 0a 20 20 52 74 72 65 65 43 75 72  t rc;.  RtreeCur
38da3 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72  sor *pCsr = (Rtr
38da4 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b 0a  eeCursor *)cur;.
38da5 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
38da6 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  Csr->aConstraint
38da7 29 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 52 65  );.  rc = nodeRe
38da8 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43  lease(pRtree, pC
38da9 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 73 71  sr->pNode);.  sq
38daa 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 29  lite3_free(pCsr)
38dab 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
38dac 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20 76 69  ../*.** Rtree vi
38dad 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
38dae 6c 65 20 78 45 6f 66 20 6d 65 74 68 6f 64 2e 0a  le xEof method..
38daf 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e  **.** Return non
38db0 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 63 75 72  -zero if the cur
38db1 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 63 75 72  sor does not cur
38db2 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 20  rently point to 
38db3 61 20 76 61 6c 69 64 20 0a 2a 2a 20 72 65 63 6f  a valid .** reco
38db4 72 64 20 28 69 2e 65 20 69 66 20 74 68 65 20 73  rd (i.e if the s
38db5 63 61 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64  can has finished
38db6 29 2c 20 6f 72 20 7a 65 72 6f 20 6f 74 68 65 72  ), or zero other
38db7 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
38db8 69 6e 74 20 72 74 72 65 65 45 6f 66 28 73 71 6c  int rtreeEof(sql
38db9 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
38dba 20 2a 63 75 72 29 7b 0a 20 20 52 74 72 65 65 43   *cur){.  RtreeC
38dbb 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52  ursor *pCsr = (R
38dbc 74 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72  treeCursor *)cur
38dbd 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 43 73 72  ;.  return (pCsr
38dbe 2d 3e 70 4e 6f 64 65 3d 3d 30 29 3b 0a 7d 0a 0a  ->pNode==0);.}..
38dbf 2f 2a 20 0a 2a 2a 20 43 75 72 73 6f 72 20 70 43  /* .** Cursor pC
38dc0 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
38dc1 70 6f 69 6e 74 73 20 74 6f 20 61 20 63 65 6c 6c  points to a cell
38dc2 20 69 6e 20 61 20 6e 6f 6e 2d 6c 65 61 66 20 70   in a non-leaf p
38dc3 61 67 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74  age..** Return t
38dc4 72 75 65 20 69 66 20 74 68 65 20 73 75 62 2d 74  rue if the sub-t
38dc5 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
38dc6 65 20 63 65 6c 6c 20 69 73 20 66 69 6c 74 65 72  e cell is filter
38dc7 65 64 0a 2a 2a 20 28 65 78 63 6c 75 64 65 64 29  ed.** (excluded)
38dc8 20 62 79 20 74 68 65 20 63 6f 6e 73 74 72 61 69   by the constrai
38dc9 6e 74 73 20 69 6e 20 74 68 65 20 70 43 75 72 73  nts in the pCurs
38dca 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  or->aConstraint[
38dcb 5d 20 0a 2a 2a 20 61 72 72 61 79 2c 20 6f 72 20  ] .** array, or 
38dcc 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e  false otherwise.
38dcd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
38dce 65 73 74 52 74 72 65 65 43 65 6c 6c 28 52 74 72  estRtreeCell(Rtr
38dcf 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
38dd0 65 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  eCursor *pCursor
38dd1 29 7b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63  ){.  RtreeCell c
38dd2 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  ell;.  int ii;. 
38dd3 20 69 6e 74 20 62 52 65 73 20 3d 20 30 3b 0a 0a   int bRes = 0;..
38dd4 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
38dd5 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70  tree, pCursor->p
38dd6 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69  Node, pCursor->i
38dd7 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
38dd8 66 6f 72 28 69 69 3d 30 3b 20 62 52 65 73 3d 3d  for(ii=0; bRes==
38dd9 30 20 26 26 20 69 69 3c 70 43 75 72 73 6f 72 2d  0 && ii<pCursor-
38dda 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69  >nConstraint; ii
38ddb 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f  ++){.    RtreeCo
38ddc 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70  nstraint *p = &p
38ddd 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61  Cursor->aConstra
38dde 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75  int[ii];.    dou
38ddf 62 6c 65 20 63 65 6c 6c 5f 6d 69 6e 20 3d 20 44  ble cell_min = D
38de0 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72  COORD(cell.aCoor
38de1 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29  d[(p->iCoord>>1)
38de2 2a 32 5d 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65  *2]);.    double
38de3 20 63 65 6c 6c 5f 6d 61 78 20 3d 20 44 43 4f 4f   cell_max = DCOO
38de4 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28  RD(cell.aCoord[(
38de5 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29 2a 32 2b  p->iCoord>>1)*2+
38de6 31 5d 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  1]);..    assert
38de7 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45  (p->op==RTREE_LE
38de8 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
38de9 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LT || p->op==RT
38dea 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20 20 20  REE_GE .        
38deb 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
38dec 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  GT || p->op==RTR
38ded 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 0a 20 20  EE_EQ.    );..  
38dee 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20    switch( p->op 
38def 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54  ){.      case RT
38df0 52 45 45 5f 4c 45 3a 20 63 61 73 65 20 52 54 52  REE_LE: case RTR
38df1 45 45 5f 4c 54 3a 20 62 52 65 73 20 3d 20 70 2d  EE_LT: bRes = p-
38df2 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e  >rValue<cell_min
38df3 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63  ; break;.      c
38df4 61 73 65 20 52 54 52 45 45 5f 47 45 3a 20 63 61  ase RTREE_GE: ca
38df5 73 65 20 52 54 52 45 45 5f 47 54 3a 20 62 52 65  se RTREE_GT: bRe
38df6 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65 3e 63 65  s = p->rValue>ce
38df7 6c 6c 5f 6d 61 78 3b 20 62 72 65 61 6b 3b 0a 20  ll_max; break;. 
38df8 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
38df9 45 51 3a 20 0a 20 20 20 20 20 20 20 20 62 52 65  EQ: .        bRe
38dfa 73 20 3d 20 28 70 2d 3e 72 56 61 6c 75 65 3e 63  s = (p->rValue>c
38dfb 65 6c 6c 5f 6d 61 78 20 7c 7c 20 70 2d 3e 72 56  ell_max || p->rV
38dfc 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e 29 3b 0a  alue<cell_min);.
38dfd 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
38dfe 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
38dff 72 6e 20 62 52 65 73 3b 0a 7d 0a 0a 2f 2a 20 0a  rn bRes;.}../* .
38e00 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
38e01 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
38e02 63 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20 63  cursor pCursor c
38e03 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20  urrently points 
38e04 74 6f 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20 66  to.** would be f
38e05 69 6c 74 65 72 65 64 20 28 65 78 63 6c 75 64 65  iltered (exclude
38e06 64 29 20 62 79 20 74 68 65 20 63 6f 6e 73 74 72  d) by the constr
38e07 61 69 6e 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a  aints in the .**
38e08 20 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74   pCursor->aConst
38e09 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20 6f  raint[] array, o
38e0a 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73  r false otherwis
38e0b 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  e..**.** This fu
38e0c 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  nction assumes t
38e0d 68 61 74 20 74 68 65 20 63 65 6c 6c 20 69 73 20  hat the cell is 
38e0e 70 61 72 74 20 6f 66 20 61 20 6c 65 61 66 20 6e  part of a leaf n
38e0f 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ode..*/.static i
38e10 6e 74 20 74 65 73 74 52 74 72 65 65 45 6e 74 72  nt testRtreeEntr
38e11 79 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  y(Rtree *pRtree,
38e12 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
38e13 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43  ursor){.  RtreeC
38e14 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20  ell cell;.  int 
38e15 69 69 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65  ii;..  nodeGetCe
38e16 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  ll(pRtree, pCurs
38e17 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73  or->pNode, pCurs
38e18 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  or->iCell, &cell
38e19 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
38e1a 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73  i<pCursor->nCons
38e1b 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  traint; ii++){. 
38e1c 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69     RtreeConstrai
38e1d 6e 74 20 2a 70 20 3d 20 26 70 43 75 72 73 6f 72  nt *p = &pCursor
38e1e 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69  ->aConstraint[ii
38e1f 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 6f  ];.    double co
38e20 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c  ord = DCOORD(cel
38e21 6c 2e 61 43 6f 6f 72 64 5b 70 2d 3e 69 43 6f 6f  l.aCoord[p->iCoo
38e22 72 64 5d 29 3b 0a 20 20 20 20 69 6e 74 20 72 65  rd]);.    int re
38e23 73 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 2d  s;.    assert(p-
38e24 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45 20 7c 7c  >op==RTREE_LE ||
38e25 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 54   p->op==RTREE_LT
38e26 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
38e27 5f 47 45 20 0a 20 20 20 20 20 20 20 20 7c 7c 20  _GE .        || 
38e28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 54 20  p->op==RTREE_GT 
38e29 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
38e2a 45 51 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 77  EQ.    );.    sw
38e2b 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20  itch( p->op ){. 
38e2c 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
38e2d 4c 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64  LE: res = (coord
38e2e 3c 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72  <=p->rValue); br
38e2f 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
38e30 52 54 52 45 45 5f 4c 54 3a 20 72 65 73 20 3d 20  RTREE_LT: res = 
38e31 28 63 6f 6f 72 64 3c 70 2d 3e 72 56 61 6c 75 65  (coord<p->rValue
38e32 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  );  break;.     
38e33 20 63 61 73 65 20 52 54 52 45 45 5f 47 45 3a 20   case RTREE_GE: 
38e34 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e 3d 70 2d  res = (coord>=p-
38e35 3e 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b  >rValue); break;
38e36 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45  .      case RTRE
38e37 45 5f 47 54 3a 20 72 65 73 20 3d 20 28 63 6f 6f  E_GT: res = (coo
38e38 72 64 3e 70 2d 3e 72 56 61 6c 75 65 29 3b 20 20  rd>p->rValue);  
38e39 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
38e3a 65 20 52 54 52 45 45 5f 45 51 3a 20 72 65 73 20  e RTREE_EQ: res 
38e3b 3d 20 28 63 6f 6f 72 64 3d 3d 70 2d 3e 72 56 61  = (coord==p->rVa
38e3c 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20  lue); break;.   
38e3d 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 72 65 73   }..    if( !res
38e3e 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d   ) return 1;.  }
38e3f 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ..  return 0;.}.
38e40 0a 2f 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 70 43  ./*.** Cursor pC
38e41 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
38e42 70 6f 69 6e 74 73 20 61 74 20 61 20 6e 6f 64 65  points at a node
38e43 20 74 68 61 74 20 68 65 61 64 73 20 61 20 73 75   that heads a su
38e44 62 2d 74 72 65 65 20 6f 66 0a 2a 2a 20 68 65 69  b-tree of.** hei
38e45 67 68 74 20 69 48 65 69 67 68 74 20 28 69 66 20  ght iHeight (if 
38e46 69 48 65 69 67 68 74 3d 3d 30 2c 20 74 68 65 6e  iHeight==0, then
38e47 20 74 68 65 20 6e 6f 64 65 20 69 73 20 61 20 6c   the node is a l
38e48 65 61 66 29 2e 20 44 65 73 63 65 6e 64 0a 2a 2a  eaf). Descend.**
38e49 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
38e4a 20 6c 65 66 74 2d 6d 6f 73 74 20 63 65 6c 6c 20   left-most cell 
38e4b 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20  of the sub-tree 
38e4c 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65  that matches the
38e4d 20 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20   .** configured 
38e4e 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2f 0a  constraints..*/.
38e4f 73 74 61 74 69 63 20 69 6e 74 20 64 65 73 63 65  static int desce
38e50 6e 64 54 6f 43 65 6c 6c 28 0a 20 20 52 74 72 65  ndToCell(.  Rtre
38e51 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74  e *pRtree, .  Rt
38e52 72 65 65 43 75 72 73 6f 72 20 2a 70 43 75 72 73  reeCursor *pCurs
38e53 6f 72 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67  or, .  int iHeig
38e54 68 74 2c 0a 20 20 69 6e 74 20 2a 70 45 6f 66 20  ht,.  int *pEof 
38e55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38e56 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74  /* OUT: Set to t
38e57 72 75 65 20 69 66 20 63 61 6e 6e 6f 74 20 64 65  rue if cannot de
38e58 73 63 65 6e 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e  scend */.){.  in
38e59 74 20 69 73 45 6f 66 3b 0a 20 20 69 6e 74 20 72  t isEof;.  int r
38e5a 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 52  c;.  int ii;.  R
38e5b 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64  treeNode *pChild
38e5c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
38e5d 34 20 69 52 6f 77 69 64 3b 0a 0a 20 20 52 74 72  4 iRowid;..  Rtr
38e5e 65 65 4e 6f 64 65 20 2a 70 53 61 76 65 64 4e 6f  eeNode *pSavedNo
38e5f 64 65 20 3d 20 70 43 75 72 73 6f 72 2d 3e 70 4e  de = pCursor->pN
38e60 6f 64 65 3b 0a 20 20 69 6e 74 20 69 53 61 76 65  ode;.  int iSave
38e61 64 43 65 6c 6c 20 3d 20 70 43 75 72 73 6f 72 2d  dCell = pCursor-
38e62 3e 69 43 65 6c 6c 3b 0a 0a 20 20 61 73 73 65 72  >iCell;..  asser
38e63 74 28 20 69 48 65 69 67 68 74 3e 3d 30 20 29 3b  t( iHeight>=0 );
38e64 0a 0a 20 20 69 66 28 20 69 48 65 69 67 68 74 3d  ..  if( iHeight=
38e65 3d 30 20 29 7b 0a 20 20 20 20 69 73 45 6f 66 20  =0 ){.    isEof 
38e66 3d 20 74 65 73 74 52 74 72 65 65 45 6e 74 72 79  = testRtreeEntry
38e67 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
38e68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
38e69 69 73 45 6f 66 20 3d 20 74 65 73 74 52 74 72 65  isEof = testRtre
38e6a 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  eCell(pRtree, pC
38e6b 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66  ursor);.  }.  if
38e6c 28 20 69 73 45 6f 66 20 7c 7c 20 69 48 65 69 67  ( isEof || iHeig
38e6d 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 45  ht==0 ){.    *pE
38e6e 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20 20 20  of = isEof;.    
38e6f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38e70 3b 0a 20 20 7d 0a 0a 20 20 69 52 6f 77 69 64 20  ;.  }..  iRowid 
38e71 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  = nodeGetRowid(p
38e72 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e  Rtree, pCursor->
38e73 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e  pNode, pCursor->
38e74 69 43 65 6c 6c 29 3b 0a 20 20 72 63 20 3d 20 6e  iCell);.  rc = n
38e75 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65  odeAcquire(pRtre
38e76 65 2c 20 69 52 6f 77 69 64 2c 20 70 43 75 72 73  e, iRowid, pCurs
38e77 6f 72 2d 3e 70 4e 6f 64 65 2c 20 26 70 43 68 69  or->pNode, &pChi
38e78 6c 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ld);.  if( rc!=S
38e79 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38e7a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
38e7b 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
38e7c 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70  tree, pCursor->p
38e7d 4e 6f 64 65 29 3b 0a 20 20 70 43 75 72 73 6f 72  Node);.  pCursor
38e7e 2d 3e 70 4e 6f 64 65 20 3d 20 70 43 68 69 6c 64  ->pNode = pChild
38e7f 3b 0a 20 20 69 73 45 6f 66 20 3d 20 31 3b 0a 20  ;.  isEof = 1;. 
38e80 20 66 6f 72 28 69 69 3d 30 3b 20 69 73 45 6f 66   for(ii=0; isEof
38e81 20 26 26 20 69 69 3c 4e 43 45 4c 4c 28 70 43 68   && ii<NCELL(pCh
38e82 69 6c 64 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  ild); ii++){.   
38e83 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 20   pCursor->iCell 
38e84 3d 20 69 69 3b 0a 20 20 20 20 72 63 20 3d 20 64  = ii;.    rc = d
38e85 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52 74  escendToCell(pRt
38e86 72 65 65 2c 20 70 43 75 72 73 6f 72 2c 20 69 48  ree, pCursor, iH
38e87 65 69 67 68 74 2d 31 2c 20 26 69 73 45 6f 66 29  eight-1, &isEof)
38e88 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
38e89 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
38e8a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
38e8b 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 45  }.  }..  if( isE
38e8c 6f 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  of ){.    assert
38e8d 28 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65  ( pCursor->pNode
38e8e 3d 3d 70 43 68 69 6c 64 20 29 3b 0a 20 20 20 20  ==pChild );.    
38e8f 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 53  nodeReference(pS
38e90 61 76 65 64 4e 6f 64 65 29 3b 0a 20 20 20 20 6e  avedNode);.    n
38e91 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
38e92 65 2c 20 70 43 68 69 6c 64 29 3b 0a 20 20 20 20  e, pChild);.    
38e93 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d  pCursor->pNode =
38e94 20 70 53 61 76 65 64 4e 6f 64 65 3b 0a 20 20 20   pSavedNode;.   
38e95 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 20   pCursor->iCell 
38e96 3d 20 69 53 61 76 65 64 43 65 6c 6c 3b 0a 20 20  = iSavedCell;.  
38e97 7d 0a 0a 20 20 2a 70 45 6f 66 20 3d 20 69 73 45  }..  *pEof = isE
38e98 6f 66 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  of;.  return SQL
38e99 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
38e9a 20 4f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c   One of the cell
38e9b 73 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 20  s in node pNode 
38e9c 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
38e9d 20 68 61 76 65 20 61 20 36 34 2d 62 69 74 20 0a   have a 64-bit .
38e9e 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  ** integer value
38e9f 20 65 71 75 61 6c 20 74 6f 20 69 52 6f 77 69 64   equal to iRowid
38ea0 2e 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  . Return the ind
38ea1 65 78 20 6f 66 20 74 68 69 73 20 63 65 6c 6c 2e  ex of this cell.
38ea2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
38ea3 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 52 74  odeRowidIndex(Rt
38ea4 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
38ea5 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69  eeNode *pNode, i
38ea6 36 34 20 69 52 6f 77 69 64 29 7b 0a 20 20 69 6e  64 iRowid){.  in
38ea7 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30  t ii;.  for(ii=0
38ea8 3b 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  ; nodeGetRowid(p
38ea9 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69  Rtree, pNode, ii
38eaa 29 21 3d 69 52 6f 77 69 64 3b 20 69 69 2b 2b 29  )!=iRowid; ii++)
38eab 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 69  {.    assert( ii
38eac 3c 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d 31  <(NCELL(pNode)-1
38ead 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) );.  }.  retur
38eae 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n ii;.}../*.** R
38eaf 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
38eb0 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
38eb1 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72  aining a pointer
38eb2 20 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 0a 2a   to node pNode.*
38eb3 2a 20 69 6e 20 69 74 73 20 70 61 72 65 6e 74 2e  * in its parent.
38eb4 20 49 66 20 70 4e 6f 64 65 20 69 73 20 74 68 65   If pNode is the
38eb5 20 72 6f 6f 74 20 6e 6f 64 65 2c 20 72 65 74 75   root node, retu
38eb6 72 6e 20 2d 31 2e 0a 2a 2f 0a 73 74 61 74 69 63  rn -1..*/.static
38eb7 20 69 6e 74 20 6e 6f 64 65 50 61 72 65 6e 74 49   int nodeParentI
38eb8 6e 64 65 78 28 52 74 72 65 65 20 2a 70 52 74 72  ndex(Rtree *pRtr
38eb9 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
38eba 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f  Node){.  RtreeNo
38ebb 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e  de *pParent = pN
38ebc 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  ode->pParent;.  
38ebd 69 66 28 20 70 50 61 72 65 6e 74 20 29 7b 0a 20  if( pParent ){. 
38ebe 20 20 20 72 65 74 75 72 6e 20 6e 6f 64 65 52 6f     return nodeRo
38ebf 77 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c  widIndex(pRtree,
38ec0 20 70 50 61 72 65 6e 74 2c 20 70 4e 6f 64 65 2d   pParent, pNode-
38ec1 3e 69 4e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72  >iNode);.  }.  r
38ec2 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 20  eturn -1;.}../* 
38ec3 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
38ec4 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
38ec5 4e 65 78 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  Next method..*/.
38ec6 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
38ec7 4e 65 78 74 28 73 71 6c 69 74 65 33 5f 76 74 61  Next(sqlite3_vta
38ec8 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
38ec9 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 20  ursor){.  Rtree 
38eca 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
38ecb 20 2a 29 28 70 56 74 61 62 43 75 72 73 6f 72 2d   *)(pVtabCursor-
38ecc 3e 70 56 74 61 62 29 3b 0a 20 20 52 74 72 65 65  >pVtab);.  Rtree
38ecd 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
38ece 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56  RtreeCursor *)pV
38ecf 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74  tabCursor;.  int
38ed0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
38ed1 0a 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 69 53  ..  if( pCsr->iS
38ed2 74 72 61 74 65 67 79 3d 3d 31 20 29 7b 0a 20 20  trategy==1 ){.  
38ed3 20 20 2f 2a 20 54 68 69 73 20 22 73 63 61 6e 22    /* This "scan"
38ed4 20 69 73 20 61 20 64 69 72 65 63 74 20 6c 6f 6f   is a direct loo
38ed5 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20 54 68  kup by rowid. Th
38ed6 65 72 65 20 69 73 20 6e 6f 20 6e 65 78 74 20 65  ere is no next e
38ed7 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 6e 6f 64  ntry. */.    nod
38ed8 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
38ed9 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20   pCsr->pNode);. 
38eda 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d     pCsr->pNode =
38edb 20 30 3b 0a 20 20 7d 0a 0a 20 20 65 6c 73 65 20   0;.  }..  else 
38edc 69 66 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20  if( pCsr->pNode 
38edd 29 7b 0a 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74  ){.    /* Move t
38ede 6f 20 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79  o the next entry
38edf 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68   that matches th
38ee0 65 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e  e configured con
38ee1 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a 20 20 20  straints. */.   
38ee2 20 69 6e 74 20 69 48 65 69 67 68 74 20 3d 20 30   int iHeight = 0
38ee3 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 73  ;.    while( pCs
38ee4 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20  r->pNode ){.    
38ee5 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
38ee6 64 65 20 3d 20 70 43 73 72 2d 3e 70 4e 6f 64 65  de = pCsr->pNode
38ee7 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 65 6c  ;.      int nCel
38ee8 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
38ee9 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 43 73 72  ;.      for(pCsr
38eea 2d 3e 69 43 65 6c 6c 2b 2b 3b 20 70 43 73 72 2d  ->iCell++; pCsr-
38eeb 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20 70 43  >iCell<nCell; pC
38eec 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20  sr->iCell++){.  
38eed 20 20 20 20 20 20 69 6e 74 20 69 73 45 6f 66 3b        int isEof;
38eee 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 64 65  .        rc = de
38eef 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52 74 72  scendToCell(pRtr
38ef0 65 65 2c 20 70 43 73 72 2c 20 69 48 65 69 67 68  ee, pCsr, iHeigh
38ef1 74 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20  t, &isEof);.    
38ef2 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
38ef3 54 45 5f 4f 4b 20 7c 7c 20 21 69 73 45 6f 66 20  TE_OK || !isEof 
38ef4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
38ef5 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
38ef6 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
38ef7 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4e  pCsr->pNode = pN
38ef8 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  ode->pParent;.  
38ef9 20 20 20 20 70 43 73 72 2d 3e 69 43 65 6c 6c 20      pCsr->iCell 
38efa 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65  = nodeParentInde
38efb 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  x(pRtree, pNode)
38efc 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65  ;.      nodeRefe
38efd 72 65 6e 63 65 28 70 43 73 72 2d 3e 70 4e 6f 64  rence(pCsr->pNod
38efe 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65  e);.      nodeRe
38eff 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e  lease(pRtree, pN
38f00 6f 64 65 29 3b 0a 20 20 20 20 20 20 69 48 65 69  ode);.      iHei
38f01 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ght++;.    }.  }
38f02 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
38f03 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
38f04 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
38f05 75 6c 65 20 78 52 6f 77 69 64 20 6d 65 74 68 6f  ule xRowid metho
38f06 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
38f07 20 72 74 72 65 65 52 6f 77 69 64 28 73 71 6c 69   rtreeRowid(sqli
38f08 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
38f09 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20 73 71  *pVtabCursor, sq
38f0a 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77  lite_int64 *pRow
38f0b 69 64 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52  id){.  Rtree *pR
38f0c 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29  tree = (Rtree *)
38f0d 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
38f0e 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  ab;.  RtreeCurso
38f0f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65  r *pCsr = (Rtree
38f10 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75  Cursor *)pVtabCu
38f11 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  rsor;..  assert(
38f12 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20  pCsr->pNode);.  
38f13 2a 70 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65  *pRowid = nodeGe
38f14 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
38f15 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72  Csr->pNode, pCsr
38f16 2d 3e 69 43 65 6c 6c 29 3b 0a 0a 20 20 72 65 74  ->iCell);..  ret
38f17 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
38f18 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
38f19 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
38f1a 75 6c 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68  ule xColumn meth
38f1b 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
38f1c 74 20 72 74 72 65 65 43 6f 6c 75 6d 6e 28 73 71  t rtreeColumn(sq
38f1d 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
38f1e 72 20 2a 63 75 72 2c 20 73 71 6c 69 74 65 33 5f  r *cur, sqlite3_
38f1f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e  context *ctx, in
38f20 74 20 69 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  t i){.  Rtree *p
38f21 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
38f22 29 63 75 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52  )cur->pVtab;.  R
38f23 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72  treeCursor *pCsr
38f24 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
38f25 2a 29 63 75 72 3b 0a 0a 20 20 69 66 28 20 69 3d  *)cur;..  if( i=
38f26 3d 30 20 29 7b 0a 20 20 20 20 69 36 34 20 69 52  =0 ){.    i64 iR
38f27 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
38f28 77 69 64 28 70 52 74 72 65 65 2c 20 70 43 73 72  wid(pRtree, pCsr
38f29 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69  ->pNode, pCsr->i
38f2a 43 65 6c 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  Cell);.    sqlit
38f2b 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
38f2c 63 74 78 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20  ctx, iRowid);.  
38f2d 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74 72 65 65  }else{.    Rtree
38f2e 43 6f 6f 72 64 20 63 3b 0a 20 20 20 20 6e 6f 64  Coord c;.    nod
38f2f 65 47 65 74 43 6f 6f 72 64 28 70 52 74 72 65 65  eGetCoord(pRtree
38f30 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70  , pCsr->pNode, p
38f31 43 73 72 2d 3e 69 43 65 6c 6c 2c 20 69 2d 31 2c  Csr->iCell, i-1,
38f32 20 26 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52   &c);.    if( pR
38f33 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65  tree->eCoordType
38f34 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  ==RTREE_COORD_RE
38f35 41 4c 33 32 20 29 7b 0a 20 20 20 20 20 20 73 71  AL32 ){.      sq
38f36 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
38f37 62 6c 65 28 63 74 78 2c 20 63 2e 66 29 3b 0a 20  ble(ctx, c.f);. 
38f38 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
38f39 61 73 73 65 72 74 28 20 70 52 74 72 65 65 2d 3e  assert( pRtree->
38f3a 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
38f3b 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 20 29 3b  E_COORD_INT32 );
38f3c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
38f3d 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20 63  esult_int(ctx, c
38f3e 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  .i);.    }.  }..
38f3f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
38f40 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 55 73  OK;.}../* .** Us
38f41 65 20 6e 6f 64 65 41 63 71 75 69 72 65 28 29 20  e nodeAcquire() 
38f42 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 65  to obtain the le
38f43 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69  af node containi
38f44 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 77 69  ng the record wi
38f45 74 68 20 0a 2a 2a 20 72 6f 77 69 64 20 69 52 6f  th .** rowid iRo
38f46 77 69 64 2e 20 49 66 20 73 75 63 63 65 73 73 66  wid. If successf
38f47 75 6c 2c 20 73 65 74 20 2a 70 70 4c 65 61 66 20  ul, set *ppLeaf 
38f48 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
38f49 6e 6f 64 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75  node and.** retu
38f4a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66  rn SQLITE_OK. If
38f4b 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63   there is no suc
38f4c 68 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  h record in the 
38f4d 74 61 62 6c 65 2c 20 73 65 74 0a 2a 2a 20 2a 70  table, set.** *p
38f4e 70 4c 65 61 66 20 74 6f 20 30 20 61 6e 64 20 72  pLeaf to 0 and r
38f4f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
38f50 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
38f51 75 72 73 2c 20 73 65 74 20 2a 70 70 4c 65 61 66  urs, set *ppLeaf
38f52 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 61 6e 64 20  .** to zero and 
38f53 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65  return an SQLite
38f54 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
38f55 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64 4c  static int findL
38f56 65 61 66 4e 6f 64 65 28 52 74 72 65 65 20 2a 70  eafNode(Rtree *p
38f57 52 74 72 65 65 2c 20 69 36 34 20 69 52 6f 77 69  Rtree, i64 iRowi
38f58 64 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  d, RtreeNode **p
38f59 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20 72 63  pLeaf){.  int rc
38f5a 3b 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20 30 3b  ;.  *ppLeaf = 0;
38f5b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
38f5c 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
38f5d 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 69 52 6f  eadRowid, 1, iRo
38f5e 77 69 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  wid);.  if( sqli
38f5f 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d  te3_step(pRtree-
38f60 3e 70 52 65 61 64 52 6f 77 69 64 29 3d 3d 53 51  >pReadRowid)==SQ
38f61 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
38f62 69 36 34 20 69 4e 6f 64 65 20 3d 20 73 71 6c 69  i64 iNode = sqli
38f63 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
38f64 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
38f65 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 72 63 20  wid, 0);.    rc 
38f66 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
38f67 74 72 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20  tree, iNode, 0, 
38f68 70 70 4c 65 61 66 29 3b 0a 20 20 20 20 73 71 6c  ppLeaf);.    sql
38f69 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
38f6a 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
38f6b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
38f6c 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
38f6d 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
38f6e 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  id);.  }.  retur
38f6f 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 20 0a 2a 2a  n rc;.}.../* .**
38f70 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
38f71 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 46 69 6c  able module xFil
38f72 74 65 72 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ter method..*/.s
38f73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 46  tatic int rtreeF
38f74 69 6c 74 65 72 28 0a 20 20 73 71 6c 69 74 65 33  ilter(.  sqlite3
38f75 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
38f76 74 61 62 43 75 72 73 6f 72 2c 20 0a 20 20 69 6e  tabCursor, .  in
38f77 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20  t idxNum, const 
38f78 63 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20 20  char *idxStr,.  
38f79 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
38f7a 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
38f7b 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  {.  Rtree *pRtre
38f7c 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74  e = (Rtree *)pVt
38f7d 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
38f7e 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
38f7f 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72  pCsr = (RtreeCur
38f80 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72 73 6f  sor *)pVtabCurso
38f81 72 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  r;..  RtreeNode 
38f82 2a 70 52 6f 6f 74 20 3d 20 30 3b 0a 20 20 69 6e  *pRoot = 0;.  in
38f83 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t ii;.  int rc =
38f84 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72   SQLITE_OK;..  r
38f85 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 70 52  treeReference(pR
38f86 74 72 65 65 29 3b 0a 0a 20 20 73 71 6c 69 74 65  tree);..  sqlite
38f87 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61 43 6f  3_free(pCsr->aCo
38f88 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 70 43 73  nstraint);.  pCs
38f89 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d  r->aConstraint =
38f8a 20 30 3b 0a 20 20 70 43 73 72 2d 3e 69 53 74 72   0;.  pCsr->iStr
38f8b 61 74 65 67 79 20 3d 20 69 64 78 4e 75 6d 3b 0a  ategy = idxNum;.
38f8c 0a 20 20 69 66 28 20 69 64 78 4e 75 6d 3d 3d 31  .  if( idxNum==1
38f8d 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70 65 63 69   ){.    /* Speci
38f8e 61 6c 20 63 61 73 65 20 2d 20 6c 6f 6f 6b 75 70  al case - lookup
38f8f 20 62 79 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20   by rowid. */.  
38f90 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65    RtreeNode *pLe
38f91 61 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  af;        /* Le
38f92 61 66 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  af on which the 
38f93 72 65 71 75 69 72 65 64 20 63 65 6c 6c 20 72 65  required cell re
38f94 73 69 64 65 73 20 2a 2f 0a 20 20 20 20 69 36 34  sides */.    i64
38f95 20 69 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65   iRowid = sqlite
38f96 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
38f97 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 63 20 3d  gv[0]);.    rc =
38f98 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 70 52   findLeafNode(pR
38f99 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 26 70  tree, iRowid, &p
38f9a 4c 65 61 66 29 3b 0a 20 20 20 20 70 43 73 72 2d  Leaf);.    pCsr-
38f9b 3e 70 4e 6f 64 65 20 3d 20 70 4c 65 61 66 3b 20  >pNode = pLeaf; 
38f9c 0a 20 20 20 20 69 66 28 20 70 4c 65 61 66 20 26  .    if( pLeaf &
38f9d 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
38f9e 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69  ){.      pCsr->i
38f9f 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77 69 64  Cell = nodeRowid
38fa0 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4c  Index(pRtree, pL
38fa1 65 61 66 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20  eaf, iRowid);.  
38fa2 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
38fa3 20 2f 2a 20 4e 6f 72 6d 61 6c 20 63 61 73 65 20   /* Normal case 
38fa4 2d 20 72 2d 74 72 65 65 20 73 63 61 6e 2e 20 53  - r-tree scan. S
38fa5 65 74 20 75 70 20 74 68 65 20 52 74 72 65 65 43  et up the RtreeC
38fa6 75 72 73 6f 72 2e 61 43 6f 6e 73 74 72 61 69 6e  ursor.aConstrain
38fa7 74 20 61 72 72 61 79 20 0a 20 20 20 20 2a 2a 20  t array .    ** 
38fa8 77 69 74 68 20 74 68 65 20 63 6f 6e 66 69 67 75  with the configu
38fa9 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  red constraints.
38faa 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28   .    */.    if(
38fab 20 61 72 67 63 3e 30 20 29 7b 0a 20 20 20 20 20   argc>0 ){.     
38fac 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69   pCsr->aConstrai
38fad 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  nt = sqlite3_mal
38fae 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
38faf 43 6f 6e 73 74 72 61 69 6e 74 29 2a 61 72 67 63  Constraint)*argc
38fb0 29 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 6e  );.      pCsr->n
38fb1 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 61 72 67  Constraint = arg
38fb2 63 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43  c;.      if( !pC
38fb3 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20  sr->aConstraint 
38fb4 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
38fb5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
38fb6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
38fb7 20 20 20 61 73 73 65 72 74 28 20 28 69 64 78 53     assert( (idxS
38fb8 74 72 3d 3d 30 20 26 26 20 61 72 67 63 3d 3d 30  tr==0 && argc==0
38fb9 29 20 7c 7c 20 73 74 72 6c 65 6e 28 69 64 78 53  ) || strlen(idxS
38fba 74 72 29 3d 3d 61 72 67 63 2a 32 20 29 3b 0a 20  tr)==argc*2 );. 
38fbb 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b         for(ii=0;
38fbc 20 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29 7b   ii<argc; ii++){
38fbd 0a 20 20 20 20 20 20 20 20 20 20 52 74 72 65 65  .          Rtree
38fbe 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d 20  Constraint *p = 
38fbf 26 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  &pCsr->aConstrai
38fc0 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 20 20 20 20  nt[ii];.        
38fc1 20 20 70 2d 3e 6f 70 20 3d 20 69 64 78 53 74 72    p->op = idxStr
38fc2 5b 69 69 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20  [ii*2];.        
38fc3 20 20 70 2d 3e 69 43 6f 6f 72 64 20 3d 20 69 64    p->iCoord = id
38fc4 78 53 74 72 5b 69 69 2a 32 2b 31 5d 2d 27 61 27  xStr[ii*2+1]-'a'
38fc5 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72  ;.          p->r
38fc6 56 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  Value = sqlite3_
38fc7 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67  value_double(arg
38fc8 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20 20 20  v[ii]);.        
38fc9 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
38fca 20 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53    .    if( rc==S
38fcb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38fcc 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20    pCsr->pNode = 
38fcd 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  0;.      rc = no
38fce 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
38fcf 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b  , 1, 0, &pRoot);
38fd0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
38fd1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
38fd2 20 20 20 20 20 20 69 6e 74 20 69 73 45 6f 66 20        int isEof 
38fd3 3d 20 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  = 1;.      int n
38fd4 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 52 6f  Cell = NCELL(pRo
38fd5 6f 74 29 3b 0a 20 20 20 20 20 20 70 43 73 72 2d  ot);.      pCsr-
38fd6 3e 70 4e 6f 64 65 20 3d 20 70 52 6f 6f 74 3b 0a  >pNode = pRoot;.
38fd7 20 20 20 20 20 20 66 6f 72 28 70 43 73 72 2d 3e        for(pCsr->
38fd8 69 43 65 6c 6c 3d 30 3b 20 72 63 3d 3d 53 51 4c  iCell=0; rc==SQL
38fd9 49 54 45 5f 4f 4b 20 26 26 20 70 43 73 72 2d 3e  ITE_OK && pCsr->
38fda 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20 70 43 73  iCell<nCell; pCs
38fdb 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20  r->iCell++){.   
38fdc 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73       assert( pCs
38fdd 72 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20  r->pNode==pRoot 
38fde 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
38fdf 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52  descendToCell(pR
38fe0 74 72 65 65 2c 20 70 43 73 72 2c 20 70 52 74 72  tree, pCsr, pRtr
38fe1 65 65 2d 3e 69 44 65 70 74 68 2c 20 26 69 73 45  ee->iDepth, &isE
38fe2 6f 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  of);.        if(
38fe3 20 21 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 20   !isEof ){.     
38fe4 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
38fe5 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
38fe6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
38fe7 54 45 5f 4f 4b 20 26 26 20 69 73 45 6f 66 20 29  TE_OK && isEof )
38fe8 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
38fe9 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70  ( pCsr->pNode==p
38fea 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20  Root );.        
38feb 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
38fec 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20  ee, pRoot);.    
38fed 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20      pCsr->pNode 
38fee 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
38fef 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
38ff0 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 70 43 73  QLITE_OK || !pCs
38ff1 72 2d 3e 70 4e 6f 64 65 20 7c 7c 20 70 43 73 72  r->pNode || pCsr
38ff2 2d 3e 69 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 43  ->iCell<NCELL(pC
38ff3 73 72 2d 3e 70 4e 6f 64 65 29 20 29 3b 0a 20 20  sr->pNode) );.  
38ff4 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 74 72 65 65    }.  }..  rtree
38ff5 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b  Release(pRtree);
38ff6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
38ff7 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  ./*.** Rtree vir
38ff8 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
38ff9 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74  e xBestIndex met
38ffa 68 6f 64 2e 20 54 68 65 72 65 20 61 72 65 20 74  hod. There are t
38ffb 68 72 65 65 0a 2a 2a 20 74 61 62 6c 65 20 73 63  hree.** table sc
38ffc 61 6e 20 73 74 72 61 74 65 67 69 65 73 20 74 6f  an strategies to
38ffd 20 63 68 6f 6f 73 65 20 66 72 6f 6d 20 28 69 6e   choose from (in
38ffe 20 6f 72 64 65 72 20 66 72 6f 6d 20 6d 6f 73 74   order from most
38fff 20 74 6f 20 0a 2a 2a 20 6c 65 61 73 74 20 64 65   to .** least de
39000 73 69 72 61 62 6c 65 29 3a 0a 2a 2a 0a 2a 2a 20  sirable):.**.** 
39001 20 20 69 64 78 4e 75 6d 20 20 20 20 20 69 64 78    idxNum     idx
39002 53 74 72 20 20 20 20 20 20 20 20 53 74 72 61 74  Str        Strat
39003 65 67 79 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  egy.**   -------
39004 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39005 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39006 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20  ---------.**    
39007 20 31 20 20 20 20 20 20 20 20 55 6e 75 73 65 64   1        Unused
39008 20 20 20 20 20 20 20 20 44 69 72 65 63 74 20 6c          Direct l
39009 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 0a  ookup by rowid..
3900a 2a 2a 20 20 20 20 20 32 20 20 20 20 20 20 20 20  **     2        
3900b 53 65 65 20 62 65 6c 6f 77 20 20 20 20 20 52 2d  See below     R-
3900c 74 72 65 65 20 71 75 65 72 79 2e 0a 2a 2a 20 20  tree query..**  
3900d 20 20 20 33 20 20 20 20 20 20 20 20 55 6e 75 73     3        Unus
3900e 65 64 20 20 20 20 20 20 20 20 46 75 6c 6c 20 74  ed        Full t
3900f 61 62 6c 65 20 73 63 61 6e 2e 0a 2a 2a 20 20 20  able scan..**   
39010 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39011 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39012 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39013 0a 2a 2a 0a 2a 2a 20 49 66 20 73 74 72 61 74 65  .**.** If strate
39014 67 79 20 31 20 6f 72 20 33 20 69 73 20 75 73 65  gy 1 or 3 is use
39015 64 2c 20 74 68 65 6e 20 69 64 78 53 74 72 20 69  d, then idxStr i
39016 73 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c  s not meaningful
39017 2e 20 49 66 20 73 74 72 61 74 65 67 79 0a 2a 2a  . If strategy.**
39018 20 32 20 69 73 20 75 73 65 64 2c 20 69 64 78 53   2 is used, idxS
39019 74 72 20 69 73 20 66 6f 72 6d 61 74 74 65 64 20  tr is formatted 
3901a 74 6f 20 63 6f 6e 74 61 69 6e 20 32 20 62 79 74  to contain 2 byt
3901b 65 73 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20  es for each .** 
3901c 63 6f 6e 73 74 72 61 69 6e 74 20 75 73 65 64 2e  constraint used.
3901d 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 62   The first two b
3901e 79 74 65 73 20 6f 66 20 69 64 78 53 74 72 20 63  ytes of idxStr c
3901f 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 0a 2a 2a  orrespond to .**
39020 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
39021 69 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  in sqlite3_index
39022 5f 69 6e 66 6f 2e 61 43 6f 6e 73 74 72 61 69 6e  _info.aConstrain
39023 74 55 73 61 67 65 5b 5d 20 77 69 74 68 0a 2a 2a  tUsage[] with.**
39024 20 28 61 72 67 76 49 6e 64 65 78 3d 3d 31 29 20   (argvIndex==1) 
39025 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  etc..**.** The f
39026 69 72 73 74 20 6f 66 20 65 61 63 68 20 70 61 69  irst of each pai
39027 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 69 64  r of bytes in id
39028 78 53 74 72 20 69 64 65 6e 74 69 66 69 65 73 20  xStr identifies 
39029 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  the constraint.*
3902a 2a 20 6f 70 65 72 61 74 6f 72 20 61 73 20 66 6f  * operator as fo
3902b 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f  llows:.**.**   O
3902c 70 65 72 61 74 6f 72 20 20 20 20 42 79 74 65 20  perator    Byte 
3902d 56 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d  Value.**   -----
3902e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3902f 2d 0a 2a 2a 20 20 20 20 20 20 3d 20 20 20 20 20  -.**      =     
39030 20 20 20 30 78 34 31 20 28 27 41 27 29 0a 2a 2a     0x41 ('A').**
39031 20 20 20 20 20 3c 3d 20 20 20 20 20 20 20 20 30       <=        0
39032 78 34 32 20 28 27 42 27 29 0a 2a 2a 20 20 20 20  x42 ('B').**    
39033 20 20 3c 20 20 20 20 20 20 20 20 30 78 34 33 20    <        0x43 
39034 28 27 43 27 29 0a 2a 2a 20 20 20 20 20 3e 3d 20  ('C').**     >= 
39035 20 20 20 20 20 20 20 30 78 34 34 20 28 27 44 27         0x44 ('D'
39036 29 0a 2a 2a 20 20 20 20 20 20 3e 20 20 20 20 20  ).**      >     
39037 20 20 20 30 78 34 35 20 28 27 45 27 29 0a 2a 2a     0x45 ('E').**
39038 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
39039 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
3903a 54 68 65 20 73 65 63 6f 6e 64 20 6f 66 20 65 61  The second of ea
3903b 63 68 20 70 61 69 72 20 6f 66 20 62 79 74 65 73  ch pair of bytes
3903c 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
3903d 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f 6c 75 6d  coordinate colum
3903e 6e 0a 2a 2a 20 74 6f 20 77 68 69 63 68 20 74 68  n.** to which th
3903f 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 70 70  e constraint app
39040 6c 69 65 73 2e 20 54 68 65 20 6c 65 66 74 6d 6f  lies. The leftmo
39041 73 74 20 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f  st coordinate co
39042 6c 75 6d 6e 0a 2a 2a 20 69 73 20 27 61 27 2c 20  lumn.** is 'a', 
39043 74 68 65 20 73 65 63 6f 6e 64 20 66 72 6f 6d 20  the second from 
39044 74 68 65 20 6c 65 66 74 20 27 62 27 20 65 74 63  the left 'b' etc
39045 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39046 72 74 72 65 65 42 65 73 74 49 6e 64 65 78 28 73  rtreeBestIndex(s
39047 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 74 61 62  qlite3_vtab *tab
39048 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  , sqlite3_index_
39049 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 29 7b  info *pIdxInfo){
3904a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
3904b 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 69 2c  TE_OK;.  int ii,
3904c 20 63 43 6f 6c 3b 0a 0a 20 20 69 6e 74 20 69 49   cCol;..  int iI
3904d 64 78 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a  dx = 0;.  char z
3904e 49 64 78 53 74 72 5b 52 54 52 45 45 5f 4d 41 58  IdxStr[RTREE_MAX
3904f 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 38 2b 31 5d  _DIMENSIONS*8+1]
39050 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 49 64 78 53  ;.  memset(zIdxS
39051 74 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 7a 49  tr, 0, sizeof(zI
39052 64 78 53 74 72 29 29 3b 0a 0a 20 20 61 73 73 65  dxStr));..  asse
39053 72 74 28 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64  rt( pIdxInfo->id
39054 78 53 74 72 3d 3d 30 20 29 3b 0a 20 20 66 6f 72  xStr==0 );.  for
39055 28 69 69 3d 30 3b 20 69 69 3c 70 49 64 78 49 6e  (ii=0; ii<pIdxIn
39056 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  fo->nConstraint;
39057 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75   ii++){.    stru
39058 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
39059 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d  _constraint *p =
3905a 20 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e   &pIdxInfo->aCon
3905b 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 0a 20 20  straint[ii];..  
3905c 20 20 69 66 28 20 70 2d 3e 75 73 61 62 6c 65 20    if( p->usable 
3905d 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30  && p->iColumn==0
3905e 20 26 26 20 70 2d 3e 6f 70 3d 3d 53 51 4c 49 54   && p->op==SQLIT
3905f 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
39060 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 2f  NT_EQ ){.      /
39061 2a 20 57 65 20 68 61 76 65 20 61 6e 20 65 71 75  * We have an equ
39062 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
39063 20 6f 6e 20 74 68 65 20 72 6f 77 69 64 2e 20 55   on the rowid. U
39064 73 65 20 73 74 72 61 74 65 67 79 20 31 2e 20 2a  se strategy 1. *
39065 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  /.      int jj;.
39066 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20        for(jj=0; 
39067 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b 29 7b 0a 20 20  jj<ii; jj++){.  
39068 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
39069 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
3906a 5b 6a 6a 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d  [jj].argvIndex =
3906b 20 30 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78   0;.        pIdx
3906c 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
3906d 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20  tUsage[jj].omit 
3906e 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
3906f 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78     pIdxInfo->idx
39070 4e 75 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  Num = 1;.      p
39071 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
39072 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 61 72  aintUsage[ii].ar
39073 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20 20 20  gvIndex = 1;.   
39074 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f     pIdxInfo->aCo
39075 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a  nstraintUsage[jj
39076 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20  ].omit = 1;..   
39077 20 20 20 2f 2a 20 54 68 69 73 20 73 74 72 61 74     /* This strat
39078 65 67 79 20 69 6e 76 6f 6c 76 65 73 20 61 20 74  egy involves a t
39079 77 6f 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 73  wo rowid lookups
3907a 20 6f 6e 20 61 6e 20 42 2d 54 72 65 65 20 73 74   on an B-Tree st
3907b 72 75 63 74 75 72 65 73 0a 20 20 20 20 20 20 2a  ructures.      *
3907c 2a 20 61 6e 64 20 74 68 65 6e 20 61 20 6c 69 6e  * and then a lin
3907d 65 61 72 20 73 65 61 72 63 68 20 6f 66 20 61 6e  ear search of an
3907e 20 52 2d 54 72 65 65 20 6e 6f 64 65 2e 20 54 68   R-Tree node. Th
3907f 69 73 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 20  is should be .  
39080 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65      ** considere
39081 64 20 61 6c 6d 6f 73 74 20 61 73 20 71 75 69 63  d almost as quic
39082 6b 20 61 73 20 61 20 64 69 72 65 63 74 20 72 6f  k as a direct ro
39083 77 69 64 20 6c 6f 6f 6b 75 70 20 28 66 6f 72 20  wid lookup (for 
39084 77 68 69 63 68 20 0a 20 20 20 20 20 20 2a 2a 20  which .      ** 
39085 73 71 6c 69 74 65 20 75 73 65 73 20 61 6e 20 69  sqlite uses an i
39086 6e 74 65 72 6e 61 6c 20 63 6f 73 74 20 6f 66 20  nternal cost of 
39087 30 2e 30 29 2e 0a 20 20 20 20 20 20 2a 2f 20 0a  0.0)..      */ .
39088 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
39089 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20  estimatedCost = 
3908a 31 30 2e 30 3b 0a 20 20 20 20 20 20 72 65 74 75  10.0;.      retu
3908b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
3908c 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e    }..    if( p->
3908d 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f  usable && p->iCo
3908e 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20  lumn>0 ){.      
3908f 75 38 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20 20  u8 op = 0;.     
39090 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
39091 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53  {.        case S
39092 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
39093 54 52 41 49 4e 54 5f 45 51 3a 20 6f 70 20 3d 20  TRAINT_EQ: op = 
39094 52 54 52 45 45 5f 45 51 3b 20 62 72 65 61 6b 3b  RTREE_EQ; break;
39095 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51  .        case SQ
39096 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
39097 52 41 49 4e 54 5f 47 54 3a 20 6f 70 20 3d 20 52  RAINT_GT: op = R
39098 54 52 45 45 5f 47 54 3b 20 62 72 65 61 6b 3b 0a  TREE_GT; break;.
39099 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c          case SQL
3909a 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
3909b 41 49 4e 54 5f 4c 45 3a 20 6f 70 20 3d 20 52 54  AINT_LE: op = RT
3909c 52 45 45 5f 4c 45 3b 20 62 72 65 61 6b 3b 0a 20  REE_LE; break;. 
3909d 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49         case SQLI
3909e 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
3909f 49 4e 54 5f 4c 54 3a 20 6f 70 20 3d 20 52 54 52  INT_LT: op = RTR
390a0 45 45 5f 4c 54 3b 20 62 72 65 61 6b 3b 0a 20 20  EE_LT; break;.  
390a1 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
390a2 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
390a3 4e 54 5f 47 45 3a 20 6f 70 20 3d 20 52 54 52 45  NT_GE: op = RTRE
390a4 45 5f 47 45 3b 20 62 72 65 61 6b 3b 0a 20 20 20  E_GE; break;.   
390a5 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f     }.      if( o
390a6 70 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  p ){.        /* 
390a7 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 70  Make sure this p
390a8 61 72 74 69 63 75 6c 61 72 20 63 6f 6e 73 74 72  articular constr
390a9 61 69 6e 74 20 68 61 73 20 6e 6f 74 20 62 65 65  aint has not bee
390aa 6e 20 75 73 65 64 20 62 65 66 6f 72 65 2e 0a 20  n used before.. 
390ab 20 20 20 20 20 20 20 2a 2a 20 49 66 20 69 74 20         ** If it 
390ac 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 65  has been used be
390ad 66 6f 72 65 2c 20 69 67 6e 6f 72 65 20 69 74 2e  fore, ignore it.
390ae 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
390af 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c      ** A <= or <
390b0 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 20   can be used if 
390b1 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f 72  there is a prior
390b2 20 3e 3d 20 6f 72 20 3e 2e 0a 20 20 20 20 20 20   >= or >..      
390b3 20 20 2a 2a 20 41 20 3e 3d 20 6f 72 20 3e 20 63    ** A >= or > c
390b4 61 6e 20 62 65 20 75 73 65 64 20 69 66 20 74 68  an be used if th
390b5 65 72 65 20 69 73 20 61 20 70 72 69 6f 72 20 3c  ere is a prior <
390b6 20 6f 72 20 3c 3d 2e 0a 20 20 20 20 20 20 20 20   or <=..        
390b7 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c 20 69 73 20  ** A <= or < is 
390b8 64 69 73 71 75 61 6c 69 66 69 65 64 20 69 66 20  disqualified if 
390b9 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f 72  there is a prior
390ba 20 3c 3d 2c 20 3c 2c 20 6f 72 20 3d 3d 2e 0a 20   <=, <, or ==.. 
390bb 20 20 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f         ** A >= o
390bc 72 20 3e 20 69 73 20 64 69 73 71 75 61 6c 69 66  r > is disqualif
390bd 69 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20  ied if there is 
390be 61 20 70 72 69 6f 72 20 3e 3d 2c 20 3e 2c 20 6f  a prior >=, >, o
390bf 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20 20 2a 2a  r ==..        **
390c0 20 41 20 3d 3d 20 69 73 20 64 69 73 71 75 61 6c   A == is disqual
390c1 69 66 65 64 20 69 66 20 74 68 65 72 65 20 69 73  ifed if there is
390c2 20 61 6e 79 20 70 72 69 6f 72 20 63 6f 6e 73 74   any prior const
390c3 72 61 69 6e 74 2e 0a 20 20 20 20 20 20 20 20 2a  raint..        *
390c4 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 2c  /.        int j,
390c5 20 6f 70 6d 73 6b 3b 0a 20 20 20 20 20 20 20 20   opmsk;.        
390c6 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
390c7 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6d 70 61  igned char compa
390c8 74 69 62 6c 65 5b 5d 20 3d 20 7b 20 30 2c 20 30  tible[] = { 0, 0
390c9 2c 20 31 2c 20 31 2c 20 32 2c 20 32 20 7d 3b 0a  , 1, 1, 2, 2 };.
390ca 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
390cb 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45  compatible[RTREE
390cc 5f 45 51 20 26 20 37 5d 3d 3d 30 20 29 3b 0a 20  _EQ & 7]==0 );. 
390cd 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63         assert( c
390ce 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f  ompatible[RTREE_
390cf 4c 54 20 26 20 37 5d 3d 3d 31 20 29 3b 0a 20 20  LT & 7]==1 );.  
390d0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63 6f        assert( co
390d1 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 4c  mpatible[RTREE_L
390d2 45 20 26 20 37 5d 3d 3d 31 20 29 3b 0a 20 20 20  E & 7]==1 );.   
390d3 20 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6d       assert( com
390d4 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 47 54  patible[RTREE_GT
390d5 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20 20 20   & 7]==2 );.    
390d6 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6d 70      assert( comp
390d7 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 47 45 20  atible[RTREE_GE 
390d8 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20 20 20 20  & 7]==2 );.     
390d9 20 20 20 63 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f     cCol = p->iCo
390da 6c 75 6d 6e 20 2d 20 31 20 2b 20 27 61 27 3b 0a  lumn - 1 + 'a';.
390db 20 20 20 20 20 20 20 20 6f 70 6d 73 6b 20 3d 20          opmsk = 
390dc 63 6f 6d 70 61 74 69 62 6c 65 5b 6f 70 20 26 20  compatible[op & 
390dd 37 5d 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  7];.        for(
390de 6a 3d 30 3b 20 6a 3c 69 49 64 78 3b 20 6a 2b 3d  j=0; j<iIdx; j+=
390df 32 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  2){.          if
390e0 28 20 7a 49 64 78 53 74 72 5b 6a 2b 31 5d 3d 3d  ( zIdxStr[j+1]==
390e1 63 43 6f 6c 20 26 26 20 28 63 6f 6d 70 61 74 69  cCol && (compati
390e2 62 6c 65 5b 7a 49 64 78 53 74 72 5b 6a 5d 20 26  ble[zIdxStr[j] &
390e3 20 37 5d 20 26 20 6f 70 6d 73 6b 29 21 3d 30 20   7] & opmsk)!=0 
390e4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f  ){.            o
390e5 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  p = 0;.         
390e6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
390e7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
390e8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
390e9 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20  ( op ){.        
390ea 61 73 73 65 72 74 28 20 69 49 64 78 3c 73 69 7a  assert( iIdx<siz
390eb 65 6f 66 28 7a 49 64 78 53 74 72 29 2d 31 20 29  eof(zIdxStr)-1 )
390ec 3b 0a 20 20 20 20 20 20 20 20 7a 49 64 78 53 74  ;.        zIdxSt
390ed 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 6f 70 3b 0a  r[iIdx++] = op;.
390ee 20 20 20 20 20 20 20 20 7a 49 64 78 53 74 72 5b          zIdxStr[
390ef 69 49 64 78 2b 2b 5d 20 3d 20 63 43 6f 6c 3b 0a  iIdx++] = cCol;.
390f0 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f          pIdxInfo
390f1 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
390f2 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e 64 65 78  ge[ii].argvIndex
390f3 20 3d 20 28 69 49 64 78 2f 32 29 3b 0a 20 20 20   = (iIdx/2);.   
390f4 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61       pIdxInfo->a
390f5 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
390f6 69 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 20 20  ii].omit = 1;.  
390f7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
390f8 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  .  pIdxInfo->idx
390f9 4e 75 6d 20 3d 20 32 3b 0a 20 20 70 49 64 78 49  Num = 2;.  pIdxI
390fa 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
390fb 64 78 53 74 72 20 3d 20 31 3b 0a 20 20 69 66 28  dxStr = 1;.  if(
390fc 20 69 49 64 78 3e 30 20 26 26 20 30 3d 3d 28 70   iIdx>0 && 0==(p
390fd 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20  IdxInfo->idxStr 
390fe 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
390ff 66 28 22 25 73 22 2c 20 7a 49 64 78 53 74 72 29  f("%s", zIdxStr)
39100 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
39101 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
39102 7d 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 78  }.  assert( iIdx
39103 3e 3d 30 20 29 3b 0a 20 20 70 49 64 78 49 6e 66  >=0 );.  pIdxInf
39104 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
39105 20 3d 20 28 32 30 30 30 30 30 30 2e 30 20 2f 20   = (2000000.0 / 
39106 28 64 6f 75 62 6c 65 29 28 69 49 64 78 20 2b 20  (double)(iIdx + 
39107 31 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  1));.  return rc
39108 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
39109 6e 20 74 68 65 20 4e 2d 64 69 6d 65 6e 73 69 6f  n the N-dimensio
3910a 6e 61 6c 20 76 6f 6c 75 6d 6e 20 6f 66 20 74 68  nal volumn of th
3910b 65 20 63 65 6c 6c 20 73 74 6f 72 65 64 20 69 6e  e cell stored in
3910c 20 2a 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66   *p..*/.static f
3910d 6c 6f 61 74 20 63 65 6c 6c 41 72 65 61 28 52 74  loat cellArea(Rt
3910e 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3910f 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c  eeCell *p){.  fl
39110 6f 61 74 20 61 72 65 61 20 3d 20 31 2e 30 3b 0a  oat area = 1.0;.
39111 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28    int ii;.  for(
39112 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65  ii=0; ii<(pRtree
39113 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32  ->nDim*2); ii+=2
39114 29 7b 0a 20 20 20 20 61 72 65 61 20 3d 20 61 72  ){.    area = ar
39115 65 61 20 2a 20 28 44 43 4f 4f 52 44 28 70 2d 3e  ea * (DCOORD(p->
39116 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20  aCoord[ii+1]) - 
39117 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
39118 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65  [ii]));.  }.  re
39119 74 75 72 6e 20 61 72 65 61 3b 0a 7d 0a 0a 2f 2a  turn area;.}../*
3911a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d  .** Return the m
3911b 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 6f 66 20  argin length of 
3911c 63 65 6c 6c 20 70 2e 20 54 68 65 20 6d 61 72 67  cell p. The marg
3911d 69 6e 20 6c 65 6e 67 74 68 20 69 73 20 74 68 65  in length is the
3911e 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 6f   sum.** of the o
3911f 62 6a 65 63 74 73 20 73 69 7a 65 20 69 6e 20 65  bjects size in e
39120 61 63 68 20 64 69 6d 65 6e 73 69 6f 6e 2e 0a 2a  ach dimension..*
39121 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  /.static float c
39122 65 6c 6c 4d 61 72 67 69 6e 28 52 74 72 65 65 20  ellMargin(Rtree 
39123 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 43 65  *pRtree, RtreeCe
39124 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c 6f 61 74 20  ll *p){.  float 
39125 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20  margin = 0.0;.  
39126 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69  int ii;.  for(ii
39127 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e  =0; ii<(pRtree->
39128 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b  nDim*2); ii+=2){
39129 0a 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20 28  .    margin += (
3912a 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
3912b 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44  [ii+1]) - DCOORD
3912c 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29  (p->aCoord[ii]))
3912d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d  ;.  }.  return m
3912e 61 72 67 69 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  argin;.}../*.** 
3912f 53 74 6f 72 65 20 74 68 65 20 75 6e 69 6f 6e 20  Store the union 
39130 6f 66 20 63 65 6c 6c 73 20 70 31 20 61 6e 64 20  of cells p1 and 
39131 70 32 20 69 6e 20 70 31 2e 0a 2a 2f 0a 73 74 61  p2 in p1..*/.sta
39132 74 69 63 20 76 6f 69 64 20 63 65 6c 6c 55 6e 69  tic void cellUni
39133 6f 6e 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  on(Rtree *pRtree
39134 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c  , RtreeCell *p1,
39135 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b   RtreeCell *p2){
39136 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 66 28  .  int ii;.  if(
39137 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
39138 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
39139 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 66  _REAL32 ){.    f
3913a 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
3913b 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
3913c 2b 3d 32 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e  +=2){.      p1->
3913d 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 4d  aCoord[ii].f = M
3913e 49 4e 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69  IN(p1->aCoord[ii
3913f 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b  ].f, p2->aCoord[
39140 69 69 5d 2e 66 29 3b 0a 20 20 20 20 20 20 70 31  ii].f);.      p1
39141 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66  ->aCoord[ii+1].f
39142 20 3d 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72   = MAX(p1->aCoor
39143 64 5b 69 69 2b 31 5d 2e 66 2c 20 70 32 2d 3e 61  d[ii+1].f, p2->a
39144 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 29 3b 0a  Coord[ii+1].f);.
39145 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
39146 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
39147 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29  (pRtree->nDim*2)
39148 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20  ; ii+=2){.      
39149 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  p1->aCoord[ii].i
3914a 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f 72   = MIN(p1->aCoor
3914b 64 5b 69 69 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f  d[ii].i, p2->aCo
3914c 6f 72 64 5b 69 69 5d 2e 69 29 3b 0a 20 20 20 20  ord[ii].i);.    
3914d 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b    p1->aCoord[ii+
3914e 31 5d 2e 69 20 3d 20 4d 41 58 28 70 31 2d 3e 61  1].i = MAX(p1->a
3914f 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 2c 20 70  Coord[ii+1].i, p
39150 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e  2->aCoord[ii+1].
39151 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  i);.    }.  }.}.
39152 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
39153 75 65 20 69 66 20 74 68 65 20 61 72 65 61 20 63  ue if the area c
39154 6f 76 65 72 65 64 20 62 79 20 70 32 20 69 73 20  overed by p2 is 
39155 61 20 73 75 62 73 65 74 20 6f 66 20 74 68 65 20  a subset of the 
39156 61 72 65 61 20 63 6f 76 65 72 65 64 0a 2a 2a 20  area covered.** 
39157 62 79 20 70 31 2e 20 46 61 6c 73 65 20 6f 74 68  by p1. False oth
39158 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  erwise..*/.stati
39159 63 20 69 6e 74 20 63 65 6c 6c 43 6f 6e 74 61 69  c int cellContai
3915a 6e 73 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ns(Rtree *pRtree
3915b 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c  , RtreeCell *p1,
3915c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b   RtreeCell *p2){
3915d 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
3915e 20 69 73 49 6e 74 20 3d 20 28 70 52 74 72 65 65   isInt = (pRtree
3915f 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
39160 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 29  REE_COORD_INT32)
39161 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
39162 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
39163 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 52  ); ii+=2){.    R
39164 74 72 65 65 43 6f 6f 72 64 20 2a 61 31 20 3d 20  treeCoord *a1 = 
39165 26 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b  &p1->aCoord[ii];
39166 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20  .    RtreeCoord 
39167 2a 61 32 20 3d 20 26 70 32 2d 3e 61 43 6f 6f 72  *a2 = &p2->aCoor
39168 64 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 28  d[ii];.    if( (
39169 21 69 73 49 6e 74 20 26 26 20 28 61 32 5b 30 5d  !isInt && (a2[0]
3916a 2e 66 3c 61 31 5b 30 5d 2e 66 20 7c 7c 20 61 32  .f<a1[0].f || a2
3916b 5b 31 5d 2e 66 3e 61 31 5b 31 5d 2e 66 29 29 20  [1].f>a1[1].f)) 
3916c 0a 20 20 20 20 20 7c 7c 20 28 20 69 73 49 6e 74  .     || ( isInt
3916d 20 26 26 20 28 61 32 5b 30 5d 2e 69 3c 61 31 5b   && (a2[0].i<a1[
3916e 30 5d 2e 69 20 7c 7c 20 61 32 5b 31 5d 2e 69 3e  0].i || a2[1].i>
3916f 61 31 5b 31 5d 2e 69 29 29 20 0a 20 20 20 20 29  a1[1].i)) .    )
39170 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
39171 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
39172 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
39173 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
39174 6e 74 20 63 65 6c 6c 20 70 20 77 6f 75 6c 64 20  nt cell p would 
39175 67 72 6f 77 20 62 79 20 69 66 20 69 74 20 77 65  grow by if it we
39176 72 65 20 75 6e 69 6f 6e 65 64 20 77 69 74 68 20  re unioned with 
39177 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  pCell..*/.static
39178 20 66 6c 6f 61 74 20 63 65 6c 6c 47 72 6f 77 74   float cellGrowt
39179 68 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  h(Rtree *pRtree,
3917a 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 52   RtreeCell *p, R
3917b 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 29  treeCell *pCell)
3917c 7b 0a 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a  {.  float area;.
3917d 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
3917e 3b 0a 20 20 6d 65 6d 63 70 79 28 26 63 65 6c 6c  ;.  memcpy(&cell
3917f 2c 20 70 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  , p, sizeof(Rtre
39180 65 43 65 6c 6c 29 29 3b 0a 20 20 61 72 65 61 20  eCell));.  area 
39181 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65  = cellArea(pRtre
39182 65 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 63 65 6c  e, &cell);.  cel
39183 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
39184 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  cell, pCell);.  
39185 72 65 74 75 72 6e 20 28 63 65 6c 6c 41 72 65 61  return (cellArea
39186 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29 2d  (pRtree, &cell)-
39187 61 72 65 61 29 3b 0a 7d 0a 0a 23 69 66 20 56 41  area);.}..#if VA
39188 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
39189 43 48 4f 4f 53 45 53 55 42 54 52 45 45 20 7c 7c  CHOOSESUBTREE ||
3918a 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
3918b 45 45 5f 53 50 4c 49 54 0a 73 74 61 74 69 63 20  EE_SPLIT.static 
3918c 66 6c 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61  float cellOverla
3918d 70 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  p(.  Rtree *pRtr
3918e 65 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  ee, .  RtreeCell
3918f 20 2a 70 2c 20 0a 20 20 52 74 72 65 65 43 65 6c   *p, .  RtreeCel
39190 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *aCell, .  int
39191 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69   nCell, .  int i
39192 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 69 6e 74  Exclude.){.  int
39193 20 69 69 3b 0a 20 20 66 6c 6f 61 74 20 6f 76 65   ii;.  float ove
39194 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20 66 6f  rlap = 0.0;.  fo
39195 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
39196 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ; ii++){.    if(
39197 20 69 69 21 3d 69 45 78 63 6c 75 64 65 20 29 7b   ii!=iExclude ){
39198 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20  .      int jj;. 
39199 20 20 20 20 20 66 6c 6f 61 74 20 6f 20 3d 20 31       float o = 1
3919a 2e 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a  .0;.      for(jj
3919b 3d 30 3b 20 6a 6a 3c 28 70 52 74 72 65 65 2d 3e  =0; jj<(pRtree->
3919c 6e 44 69 6d 2a 32 29 3b 20 6a 6a 2b 3d 32 29 7b  nDim*2); jj+=2){
3919d 0a 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20  .        double 
3919e 78 31 3b 0a 20 20 20 20 20 20 20 20 64 6f 75 62  x1;.        doub
3919f 6c 65 20 78 32 3b 0a 0a 20 20 20 20 20 20 20 20  le x2;..        
391a0 78 31 20 3d 20 4d 41 58 28 44 43 4f 4f 52 44 28  x1 = MAX(DCOORD(
391a1 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 2c 20  p->aCoord[jj]), 
391a2 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d  DCOORD(aCell[ii]
391a3 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 29 3b 0a 20  .aCoord[jj]));. 
391a4 20 20 20 20 20 20 20 78 32 20 3d 20 4d 49 4e 28         x2 = MIN(
391a5 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
391a6 5b 6a 6a 2b 31 5d 29 2c 20 44 43 4f 4f 52 44 28  [jj+1]), DCOORD(
391a7 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64  aCell[ii].aCoord
391a8 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a 20 20 20 20 20  [jj+1]));..     
391a9 20 20 20 69 66 28 20 78 32 3c 78 31 20 29 7b 0a     if( x2<x1 ){.
391aa 20 20 20 20 20 20 20 20 20 20 6f 20 3d 20 30 2e            o = 0.
391ab 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  0;.          bre
391ac 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
391ad 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 20 3d  e{.          o =
391ae 20 6f 20 2a 20 28 78 32 2d 78 31 29 3b 0a 20 20   o * (x2-x1);.  
391af 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
391b0 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 2b 3d        overlap +=
391b1 20 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   o;.    }.  }.  
391b2 72 65 74 75 72 6e 20 6f 76 65 72 6c 61 70 3b 0a  return overlap;.
391b3 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41  }.#endif..#if VA
391b4 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
391b5 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 73 74  CHOOSESUBTREE.st
391b6 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f  atic float cellO
391b7 76 65 72 6c 61 70 45 6e 6c 61 72 67 65 6d 65 6e  verlapEnlargemen
391b8 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
391b9 65 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  ee, .  RtreeCell
391ba 20 2a 70 2c 20 0a 20 20 52 74 72 65 65 43 65 6c   *p, .  RtreeCel
391bb 6c 20 2a 70 49 6e 73 65 72 74 2c 20 0a 20 20 52  l *pInsert, .  R
391bc 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c  treeCell *aCell,
391bd 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a   .  int nCell, .
391be 20 20 69 6e 74 20 69 45 78 63 6c 75 64 65 0a 29    int iExclude.)
391bf 7b 0a 20 20 66 6c 6f 61 74 20 62 65 66 6f 72 65  {.  float before
391c0 3b 0a 20 20 66 6c 6f 61 74 20 61 66 74 65 72 3b  ;.  float after;
391c1 0a 20 20 62 65 66 6f 72 65 20 3d 20 63 65 6c 6c  .  before = cell
391c2 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20  Overlap(pRtree, 
391c3 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  p, aCell, nCell,
391c4 20 69 45 78 63 6c 75 64 65 29 3b 0a 20 20 63 65   iExclude);.  ce
391c5 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20  llUnion(pRtree, 
391c6 70 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20 61  p, pInsert);.  a
391c7 66 74 65 72 20 3d 20 63 65 6c 6c 4f 76 65 72 6c  fter = cellOverl
391c8 61 70 28 70 52 74 72 65 65 2c 20 70 2c 20 61 43  ap(pRtree, p, aC
391c9 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69 45 78 63  ell, nCell, iExc
391ca 6c 75 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  lude);.  return 
391cb 61 66 74 65 72 2d 62 65 66 6f 72 65 3b 0a 7d 0a  after-before;.}.
391cc 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54  #endif.../*.** T
391cd 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  his function imp
391ce 6c 65 6d 65 6e 74 73 20 74 68 65 20 43 68 6f 6f  lements the Choo
391cf 73 65 4c 65 61 66 20 61 6c 67 6f 72 69 74 68 6d  seLeaf algorithm
391d0 20 66 72 6f 6d 20 47 75 74 6d 61 6e 5b 38 34 5d   from Gutman[84]
391d1 2e 0a 2a 2a 20 43 68 6f 6f 73 65 53 75 62 54 72  ..** ChooseSubTr
391d2 65 65 20 69 6e 20 72 2a 74 72 65 65 20 74 65 72  ee in r*tree ter
391d3 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a 2f 0a 73 74 61  minology..*/.sta
391d4 74 69 63 20 69 6e 74 20 43 68 6f 6f 73 65 4c 65  tic int ChooseLe
391d5 61 66 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  af(.  Rtree *pRt
391d6 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ree,            
391d7 20 20 20 2f 2a 20 52 74 72 65 65 20 74 61 62 6c     /* Rtree tabl
391d8 65 20 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c  e */.  RtreeCell
391d9 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20   *pCell,        
391da 20 20 20 20 2f 2a 20 43 65 6c 6c 20 74 6f 20 69      /* Cell to i
391db 6e 73 65 72 74 20 69 6e 74 6f 20 72 74 72 65 65  nsert into rtree
391dc 20 2a 2f 0a 20 20 69 6e 74 20 69 48 65 69 67 68   */.  int iHeigh
391dd 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
391de 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20     /* Height of 
391df 73 75 62 2d 74 72 65 65 20 72 6f 6f 74 65 64 20  sub-tree rooted 
391e0 61 74 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 52 74  at pCell */.  Rt
391e1 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66  reeNode **ppLeaf
391e2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
391e3 54 3a 20 53 65 6c 65 63 74 65 64 20 6c 65 61 66  T: Selected leaf
391e4 20 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   page */.){.  in
391e5 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  t rc;.  int ii;.
391e6 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
391e7 64 65 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41  de;.  rc = nodeA
391e8 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 31  cquire(pRtree, 1
391e9 2c 20 30 2c 20 26 70 4e 6f 64 65 29 3b 0a 0a 20  , 0, &pNode);.. 
391ea 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53   for(ii=0; rc==S
391eb 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28  QLITE_OK && ii<(
391ec 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 69  pRtree->iDepth-i
391ed 48 65 69 67 68 74 29 3b 20 69 69 2b 2b 29 7b 0a  Height); ii++){.
391ee 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 3b 0a 20      int iCell;. 
391ef 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
391f0 20 69 42 65 73 74 3b 0a 0a 20 20 20 20 66 6c 6f   iBest;..    flo
391f1 61 74 20 66 4d 69 6e 47 72 6f 77 74 68 3b 0a 20  at fMinGrowth;. 
391f2 20 20 20 66 6c 6f 61 74 20 66 4d 69 6e 41 72 65     float fMinAre
391f3 61 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69  a;.    float fMi
391f4 6e 4f 76 65 72 6c 61 70 3b 0a 0a 20 20 20 20 69  nOverlap;..    i
391f5 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  nt nCell = NCELL
391f6 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74 72  (pNode);.    Rtr
391f7 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20  eeCell cell;.   
391f8 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69   RtreeNode *pChi
391f9 6c 64 3b 0a 0a 20 20 20 20 52 74 72 65 65 43 65  ld;..    RtreeCe
391fa 6c 6c 20 2a 61 43 65 6c 6c 20 3d 20 30 3b 0a 0a  ll *aCell = 0;..
391fb 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
391fc 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54  RTREE_CHOOSESUBT
391fd 52 45 45 0a 20 20 20 20 69 66 28 20 69 69 3d 3d  REE.    if( ii==
391fe 28 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d  (pRtree->iDepth-
391ff 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  1) ){.      int 
39200 6a 6a 3b 0a 20 20 20 20 20 20 61 43 65 6c 6c 20  jj;.      aCell 
39201 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
39202 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c  (sizeof(RtreeCel
39203 6c 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 20 20 20  l)*nCell);.     
39204 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20   if( !aCell ){. 
39205 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
39206 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
39207 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
39208 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20  tree, pNode);.  
39209 20 20 20 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b        pNode = 0;
3920a 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
3920b 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
3920c 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43   for(jj=0; jj<nC
3920d 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  ell; jj++){.    
3920e 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
3920f 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 6a  pRtree, pNode, j
39210 6a 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a  j, &aCell[jj]);.
39211 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
39212 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53 65 6c  ndif..    /* Sel
39213 65 63 74 20 74 68 65 20 63 68 69 6c 64 20 6e 6f  ect the child no
39214 64 65 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65  de which will be
39215 20 65 6e 6c 61 72 67 65 64 20 74 68 65 20 6c 65   enlarged the le
39216 61 73 74 20 69 66 20 70 43 65 6c 6c 0a 20 20 20  ast if pCell.   
39217 20 2a 2a 20 69 73 20 69 6e 73 65 72 74 65 64 20   ** is inserted 
39218 69 6e 74 6f 20 69 74 2e 20 52 65 73 6f 6c 76 65  into it. Resolve
39219 20 74 69 65 73 20 62 79 20 63 68 6f 6f 73 69 6e   ties by choosin
3921a 67 20 74 68 65 20 65 6e 74 72 79 20 77 69 74 68  g the entry with
3921b 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 6d 61 6c  .    ** the smal
3921c 6c 65 73 74 20 61 72 65 61 2e 0a 20 20 20 20 2a  lest area..    *
3921d 2f 0a 20 20 20 20 66 6f 72 28 69 43 65 6c 6c 3d  /.    for(iCell=
3921e 30 3b 20 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20  0; iCell<nCell; 
3921f 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20  iCell++){.      
39220 66 6c 6f 61 74 20 67 72 6f 77 74 68 3b 0a 20 20  float growth;.  
39221 20 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a      float area;.
39222 20 20 20 20 20 20 66 6c 6f 61 74 20 6f 76 65 72        float over
39223 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20  lap = 0.0;.     
39224 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
39225 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c  ree, pNode, iCel
39226 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 20  l, &cell);.     
39227 20 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c 47 72   growth = cellGr
39228 6f 77 74 68 28 70 52 74 72 65 65 2c 20 26 63 65  owth(pRtree, &ce
39229 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  ll, pCell);.    
3922a 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72 65    area = cellAre
3922b 61 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29  a(pRtree, &cell)
3922c 3b 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53  ;.#if VARIANT_RS
3922d 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55  TARTREE_CHOOSESU
3922e 42 54 52 45 45 0a 20 20 20 20 20 20 69 66 28 20  BTREE.      if( 
3922f 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44 65  ii==(pRtree->iDe
39230 70 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  pth-1) ){.      
39231 20 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c 6c    overlap = cell
39232 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67 65 6d 65  OverlapEnlargeme
39233 6e 74 28 70 52 74 72 65 65 2c 26 63 65 6c 6c 2c  nt(pRtree,&cell,
39234 70 43 65 6c 6c 2c 61 43 65 6c 6c 2c 6e 43 65 6c  pCell,aCell,nCel
39235 6c 2c 69 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20  l,iCell);.      
39236 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
39237 66 28 20 28 69 43 65 6c 6c 3d 3d 30 29 20 0a 20  f( (iCell==0) . 
39238 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61        || (overla
39239 70 3c 66 4d 69 6e 4f 76 65 72 6c 61 70 29 20 0a  p<fMinOverlap) .
3923a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c         || (overl
3923b 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20  ap==fMinOverlap 
3923c 26 26 20 67 72 6f 77 74 68 3c 66 4d 69 6e 47 72  && growth<fMinGr
3923d 6f 77 74 68 29 0a 20 20 20 20 20 20 20 7c 7c 20  owth).       || 
3923e 28 6f 76 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76  (overlap==fMinOv
3923f 65 72 6c 61 70 20 26 26 20 67 72 6f 77 74 68 3d  erlap && growth=
39240 3d 66 4d 69 6e 47 72 6f 77 74 68 20 26 26 20 61  =fMinGrowth && a
39241 72 65 61 3c 66 4d 69 6e 41 72 65 61 29 0a 20 20  rea<fMinArea).  
39242 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66      ){.        f
39243 4d 69 6e 4f 76 65 72 6c 61 70 20 3d 20 6f 76 65  MinOverlap = ove
39244 72 6c 61 70 3b 0a 20 20 20 20 20 20 20 20 66 4d  rlap;.        fM
39245 69 6e 47 72 6f 77 74 68 20 3d 20 67 72 6f 77 74  inGrowth = growt
39246 68 3b 0a 20 20 20 20 20 20 20 20 66 4d 69 6e 41  h;.        fMinA
39247 72 65 61 20 3d 20 61 72 65 61 3b 0a 20 20 20 20  rea = area;.    
39248 20 20 20 20 69 42 65 73 74 20 3d 20 63 65 6c 6c      iBest = cell
39249 2e 69 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 7d  .iRowid;.      }
3924a 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
3924b 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b  te3_free(aCell);
3924c 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63  .    rc = nodeAc
3924d 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69 42  quire(pRtree, iB
3924e 65 73 74 2c 20 70 4e 6f 64 65 2c 20 26 70 43 68  est, pNode, &pCh
3924f 69 6c 64 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ild);.    nodeRe
39250 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e  lease(pRtree, pN
39251 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 20  ode);.    pNode 
39252 3d 20 70 43 68 69 6c 64 3b 0a 20 20 7d 0a 0a 20  = pChild;.  }.. 
39253 20 2a 70 70 4c 65 61 66 20 3d 20 70 4e 6f 64 65   *ppLeaf = pNode
39254 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
39255 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 65 6c 6c 20 77  ../*.** A cell w
39256 69 74 68 20 74 68 65 20 73 61 6d 65 20 63 6f 6e  ith the same con
39257 74 65 6e 74 20 61 73 20 70 43 65 6c 6c 20 68 61  tent as pCell ha
39258 73 20 6a 75 73 74 20 62 65 65 6e 20 69 6e 73 65  s just been inse
39259 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  rted into.** the
3925a 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 54 68 69   node pNode. Thi
3925b 73 20 66 75 6e 63 74 69 6f 6e 20 75 70 64 61 74  s function updat
3925c 65 73 20 74 68 65 20 62 6f 75 6e 64 69 6e 67 20  es the bounding 
3925d 62 6f 78 20 63 65 6c 6c 73 20 69 6e 0a 2a 2a 20  box cells in.** 
3925e 61 6c 6c 20 61 6e 63 65 73 74 6f 72 20 65 6c 65  all ancestor ele
3925f 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
39260 20 76 6f 69 64 20 41 64 6a 75 73 74 54 72 65 65   void AdjustTree
39261 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
39262 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
39263 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65 20 74        /* Rtree t
39264 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65 65 4e  able */.  RtreeN
39265 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20 20 20 20  ode *pNode,     
39266 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
39267 64 6a 75 73 74 20 61 6e 63 65 73 74 72 79 20 6f  djust ancestry o
39268 66 20 74 68 69 73 20 6e 6f 64 65 2e 20 2a 2f 0a  f this node. */.
39269 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65    RtreeCell *pCe
3926a 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
3926b 20 20 20 20 2f 2a 20 54 68 69 73 20 63 65 6c 6c      /* This cell
3926c 20 77 61 73 20 6a 75 73 74 20 69 6e 73 65 72 74   was just insert
3926d 65 64 20 2a 2f 0a 29 7b 0a 20 20 52 74 72 65 65  ed */.){.  Rtree
3926e 4e 6f 64 65 20 2a 70 20 3d 20 70 4e 6f 64 65 3b  Node *p = pNode;
3926f 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 61  .  while( p->pPa
39270 72 65 6e 74 20 29 7b 0a 20 20 20 20 52 74 72 65  rent ){.    Rtre
39271 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
39272 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65  RtreeNode *pPare
39273 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b  nt = p->pParent;
39274 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 20 3d  .    int iCell =
39275 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78   nodeParentIndex
39276 28 70 52 74 72 65 65 2c 20 70 29 3b 0a 0a 20 20  (pRtree, p);..  
39277 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
39278 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 69  tree, pParent, i
39279 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
3927a 20 20 69 66 28 20 21 63 65 6c 6c 43 6f 6e 74 61    if( !cellConta
3927b 69 6e 73 28 70 52 74 72 65 65 2c 20 26 63 65 6c  ins(pRtree, &cel
3927c 6c 2c 20 70 43 65 6c 6c 29 20 29 7b 0a 20 20 20  l, pCell) ){.   
3927d 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74     cellUnion(pRt
3927e 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c  ree, &cell, pCel
3927f 6c 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 4f 76  l);.      nodeOv
39280 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74 72  erwriteCell(pRtr
39281 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 63 65  ee, pParent, &ce
39282 6c 6c 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20 20  ll, iCell);.    
39283 7d 0a 20 0a 20 20 20 20 70 20 3d 20 70 50 61 72  }. .    p = pPar
39284 65 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ent;.  }.}../*.*
39285 2a 20 57 72 69 74 65 20 6d 61 70 70 69 6e 67 20  * Write mapping 
39286 28 69 52 6f 77 69 64 2d 3e 69 4e 6f 64 65 29 20  (iRowid->iNode) 
39287 74 6f 20 74 68 65 20 3c 72 74 72 65 65 3e 5f 72  to the <rtree>_r
39288 6f 77 69 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  owid table..*/.s
39289 74 61 74 69 63 20 69 6e 74 20 72 6f 77 69 64 57  tatic int rowidW
3928a 72 69 74 65 28 52 74 72 65 65 20 2a 70 52 74 72  rite(Rtree *pRtr
3928b 65 65 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ee, sqlite3_int6
3928c 34 20 69 52 6f 77 69 64 2c 20 73 71 6c 69 74 65  4 iRowid, sqlite
3928d 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65 29 7b 0a  3_int64 iNode){.
3928e 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
3928f 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72  nt64(pRtree->pWr
39290 69 74 65 52 6f 77 69 64 2c 20 31 2c 20 69 52 6f  iteRowid, 1, iRo
39291 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  wid);.  sqlite3_
39292 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
39293 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20  e->pWriteRowid, 
39294 32 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c  2, iNode);.  sql
39295 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
39296 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
39297 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
39298 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
39299 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 7d 0a 0a  WriteRowid);.}..
3929a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70 70  /*.** Write mapp
3929b 69 6e 67 20 28 69 4e 6f 64 65 2d 3e 69 50 61 72  ing (iNode->iPar
3929c 29 20 74 6f 20 74 68 65 20 3c 72 74 72 65 65 3e  ) to the <rtree>
3929d 5f 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 0a 2a  _parent table..*
3929e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
3929f 65 6e 74 57 72 69 74 65 28 52 74 72 65 65 20 2a  entWrite(Rtree *
392a0 70 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33 5f  pRtree, sqlite3_
392a1 69 6e 74 36 34 20 69 4e 6f 64 65 2c 20 73 71 6c  int64 iNode, sql
392a2 69 74 65 33 5f 69 6e 74 36 34 20 69 50 61 72 29  ite3_int64 iPar)
392a3 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  {.  sqlite3_bind
392a4 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
392a5 57 72 69 74 65 50 61 72 65 6e 74 2c 20 31 2c 20  WriteParent, 1, 
392a6 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65  iNode);.  sqlite
392a7 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
392a8 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e  ree->pWriteParen
392a9 74 2c 20 32 2c 20 69 50 61 72 29 3b 0a 20 20 73  t, 2, iPar);.  s
392aa 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
392ab 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
392ac 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
392ad 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
392ae 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74 29 3b  ->pWriteParent);
392af 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .}..static int r
392b0 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 52  treeInsertCell(R
392b1 74 72 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64  tree *, RtreeNod
392b2 65 20 2a 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  e *, RtreeCell *
392b3 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20 56 41 52  , int);..#if VAR
392b4 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
392b5 45 41 52 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20  EAR_SPLIT./*.** 
392b6 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
392b7 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76 61 72  f the linear var
392b8 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
392b9 4e 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  Next() function 
392ba 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
392bb 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52  84]..*/.static R
392bc 74 72 65 65 43 65 6c 6c 20 2a 4c 69 6e 65 61 72  treeCell *Linear
392bd 50 69 63 6b 4e 65 78 74 28 0a 20 20 52 74 72 65  PickNext(.  Rtre
392be 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
392bf 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a  eeCell *aCell, .
392c0 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20    int nCell, .  
392c1 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c 65 66 74  RtreeCell *pLeft
392c2 42 6f 78 2c 20 0a 20 20 52 74 72 65 65 43 65 6c  Box, .  RtreeCel
392c3 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c 0a 20 20  l *pRightBox,.  
392c4 69 6e 74 20 2a 61 69 55 73 65 64 0a 29 7b 0a 20  int *aiUsed.){. 
392c5 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
392c6 69 3d 30 3b 20 61 69 55 73 65 64 5b 69 69 5d 3b  i=0; aiUsed[ii];
392c7 20 69 69 2b 2b 29 3b 0a 20 20 61 69 55 73 65 64   ii++);.  aiUsed
392c8 5b 69 69 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75  [ii] = 1;.  retu
392c9 72 6e 20 26 61 43 65 6c 6c 5b 69 69 5d 3b 0a 7d  rn &aCell[ii];.}
392ca 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
392cb 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  tation of the li
392cc 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f 66 20  near variant of 
392cd 74 68 65 20 50 69 63 6b 53 65 65 64 73 28 29 20  the PickSeeds() 
392ce 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
392cf 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
392d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 4c 69 6e  .static void Lin
392d1 65 61 72 50 69 63 6b 53 65 65 64 73 28 0a 20 20  earPickSeeds(.  
392d2 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20  Rtree *pRtree,. 
392d3 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
392d4 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c  l, .  int nCell,
392d5 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66 74 53   .  int *piLeftS
392d6 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a 70 69 52  eed, .  int *piR
392d7 69 67 68 74 53 65 65 64 0a 29 7b 0a 20 20 69 6e  ightSeed.){.  in
392d8 74 20 69 3b 0a 20 20 69 6e 74 20 69 4c 65 66 74  t i;.  int iLeft
392d9 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Seed = 0;.  int 
392da 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a  iRightSeed = 1;.
392db 20 20 66 6c 6f 61 74 20 6d 61 78 4e 6f 72 6d 61    float maxNorma
392dc 6c 49 6e 6e 65 72 57 69 64 74 68 20 3d 20 30 2e  lInnerWidth = 0.
392dd 30 3b 0a 0a 20 20 2f 2a 20 50 69 63 6b 20 74 77  0;..  /* Pick tw
392de 6f 20 22 73 65 65 64 22 20 63 65 6c 6c 73 20 66  o "seed" cells f
392df 72 6f 6d 20 74 68 65 20 61 72 72 61 79 20 6f 66  rom the array of
392e0 20 63 65 6c 6c 73 2e 20 54 68 65 20 61 6c 67 6f   cells. The algo
392e1 72 69 74 68 6d 20 75 73 65 64 0a 20 20 2a 2a 20  rithm used.  ** 
392e2 68 65 72 65 20 69 73 20 74 68 65 20 4c 69 6e 65  here is the Line
392e3 61 72 50 69 63 6b 53 65 65 64 73 20 61 6c 67 6f  arPickSeeds algo
392e4 72 69 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61  rithm from Gutma
392e5 6e 5b 31 39 38 34 5d 2e 20 54 68 65 20 0a 20 20  n[1984]. The .  
392e6 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68  ** indices of th
392e7 65 20 74 77 6f 20 73 65 65 64 20 63 65 6c 6c 73  e two seed cells
392e8 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 61 72   in the array ar
392e9 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 63 61  e stored in loca
392ea 6c 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73  l.  ** variables
392eb 20 69 4c 65 66 74 53 65 65 6b 20 61 6e 64 20 69   iLeftSeek and i
392ec 52 69 67 68 74 53 65 65 64 2e 0a 20 20 2a 2f 0a  RightSeed..  */.
392ed 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 52 74    for(i=0; i<pRt
392ee 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 2b 2b 29 7b  ree->nDim; i++){
392ef 0a 20 20 20 20 66 6c 6f 61 74 20 78 31 20 3d 20  .    float x1 = 
392f0 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 30 5d 2e  DCOORD(aCell[0].
392f1 61 43 6f 6f 72 64 5b 69 2a 32 5d 29 3b 0a 20 20  aCoord[i*2]);.  
392f2 20 20 66 6c 6f 61 74 20 78 32 20 3d 20 44 43 4f    float x2 = DCO
392f3 4f 52 44 28 61 43 65 6c 6c 5b 30 5d 2e 61 43 6f  ORD(aCell[0].aCo
392f4 6f 72 64 5b 69 2a 32 2b 31 5d 29 3b 0a 20 20 20  ord[i*2+1]);.   
392f5 20 66 6c 6f 61 74 20 78 33 20 3d 20 78 31 3b 0a   float x3 = x1;.
392f6 20 20 20 20 66 6c 6f 61 74 20 78 34 20 3d 20 78      float x4 = x
392f7 32 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a  2;.    int jj;..
392f8 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 4c 65 66      int iCellLef
392f9 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 69  t = 0;.    int i
392fa 43 65 6c 6c 52 69 67 68 74 20 3d 20 30 3b 0a 0a  CellRight = 0;..
392fb 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a      for(jj=1; jj
392fc 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20  <nCell; jj++){. 
392fd 20 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20       float left 
392fe 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 6a  = DCOORD(aCell[j
392ff 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 29 3b  j].aCoord[i*2]);
39300 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69 67  .      float rig
39301 68 74 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c  ht = DCOORD(aCel
39302 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32  l[jj].aCoord[i*2
39303 2b 31 5d 29 3b 0a 0a 20 20 20 20 20 20 69 66 28  +1]);..      if(
39304 20 6c 65 66 74 3c 78 31 20 29 20 78 31 20 3d 20   left<x1 ) x1 = 
39305 6c 65 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20  left;.      if( 
39306 72 69 67 68 74 3e 78 34 20 29 20 78 34 20 3d 20  right>x4 ) x4 = 
39307 72 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28  right;.      if(
39308 20 6c 65 66 74 3e 78 33 20 29 7b 0a 20 20 20 20   left>x3 ){.    
39309 20 20 20 20 78 33 20 3d 20 6c 65 66 74 3b 0a 20      x3 = left;. 
3930a 20 20 20 20 20 20 20 69 43 65 6c 6c 52 69 67 68         iCellRigh
3930b 74 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a  t = jj;.      }.
3930c 20 20 20 20 20 20 69 66 28 20 72 69 67 68 74 3c        if( right<
3930d 78 32 20 29 7b 0a 20 20 20 20 20 20 20 20 78 32  x2 ){.        x2
3930e 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20   = right;.      
3930f 20 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 6a 6a    iCellLeft = jj
39310 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
39311 0a 20 20 20 20 69 66 28 20 78 34 21 3d 78 31 20  .    if( x4!=x1 
39312 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6e  ){.      float n
39313 6f 72 6d 61 6c 77 69 64 74 68 20 3d 20 28 78 33  ormalwidth = (x3
39314 20 2d 20 78 32 29 20 2f 20 28 78 34 20 2d 20 78   - x2) / (x4 - x
39315 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f  1);.      if( no
39316 72 6d 61 6c 77 69 64 74 68 3e 6d 61 78 4e 6f 72  rmalwidth>maxNor
39317 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68 20 29 7b  malInnerWidth ){
39318 0a 20 20 20 20 20 20 20 20 69 4c 65 66 74 53 65  .        iLeftSe
39319 65 64 20 3d 20 69 43 65 6c 6c 4c 65 66 74 3b 0a  ed = iCellLeft;.
3931a 20 20 20 20 20 20 20 20 69 52 69 67 68 74 53 65          iRightSe
3931b 65 64 20 3d 20 69 43 65 6c 6c 52 69 67 68 74 3b  ed = iCellRight;
3931c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
3931d 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65 65   }..  *piLeftSee
3931e 64 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a 20  d = iLeftSeed;. 
3931f 20 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d 20   *piRightSeed = 
39320 69 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23 65  iRightSeed;.}.#e
39321 6e 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54 5f  ndif /* VARIANT_
39322 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53  GUTTMAN_LINEAR_S
39323 50 4c 49 54 20 2a 2f 0a 0a 23 69 66 20 56 41 52  PLIT */..#if VAR
39324 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
39325 44 52 41 54 49 43 5f 53 50 4c 49 54 0a 2f 2a 0a  DRATIC_SPLIT./*.
39326 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
39327 6e 20 6f 66 20 74 68 65 20 71 75 61 64 72 61 74  n of the quadrat
39328 69 63 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68  ic variant of th
39329 65 20 50 69 63 6b 4e 65 78 74 28 29 20 66 75 6e  e PickNext() fun
3932a 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75  ction from.** Gu
3932b 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74  ttman[84]..*/.st
3932c 61 74 69 63 20 52 74 72 65 65 43 65 6c 6c 20 2a  atic RtreeCell *
3932d 51 75 61 64 72 61 74 69 63 50 69 63 6b 4e 65 78  QuadraticPickNex
3932e 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
3932f 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
39330 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
39331 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43 65  Cell, .  RtreeCe
39332 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a 20  ll *pLeftBox, . 
39333 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69 67   RtreeCell *pRig
39334 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61 69  htBox,.  int *ai
39335 55 73 65 64 0a 29 7b 0a 20 20 23 64 65 66 69 6e  Used.){.  #defin
39336 65 20 46 41 42 53 28 61 29 20 28 28 61 29 3c 30  e FABS(a) ((a)<0
39337 2e 30 3f 2d 31 2e 30 2a 28 61 29 3a 28 61 29 29  .0?-1.0*(a):(a))
39338 0a 0a 20 20 69 6e 74 20 69 53 65 6c 65 63 74 20  ..  int iSelect 
39339 3d 20 2d 31 3b 0a 20 20 66 6c 6f 61 74 20 66 44  = -1;.  float fD
3933a 69 66 66 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  iff;.  int ii;. 
3933b 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43   for(ii=0; ii<nC
3933c 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ell; ii++){.    
3933d 69 66 28 20 61 69 55 73 65 64 5b 69 69 5d 3d 3d  if( aiUsed[ii]==
3933e 30 20 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74  0 ){.      float
3933f 20 6c 65 66 74 20 3d 20 63 65 6c 6c 47 72 6f 77   left = cellGrow
39340 74 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  th(pRtree, pLeft
39341 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29  Box, &aCell[ii])
39342 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69  ;.      float ri
39343 67 68 74 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68  ght = cellGrowth
39344 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 42 6f  (pRtree, pLeftBo
39345 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a  x, &aCell[ii]);.
39346 20 20 20 20 20 20 66 6c 6f 61 74 20 64 69 66 66        float diff
39347 20 3d 20 46 41 42 53 28 72 69 67 68 74 2d 6c 65   = FABS(right-le
39348 66 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  ft);.      if( i
39349 53 65 6c 65 63 74 3c 30 20 7c 7c 20 64 69 66 66  Select<0 || diff
3934a 3e 66 44 69 66 66 20 29 7b 0a 20 20 20 20 20 20  >fDiff ){.      
3934b 20 20 66 44 69 66 66 20 3d 20 64 69 66 66 3b 0a    fDiff = diff;.
3934c 20 20 20 20 20 20 20 20 69 53 65 6c 65 63 74 20          iSelect 
3934d 3d 20 69 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = ii;.      }.  
3934e 20 20 7d 0a 20 20 7d 0a 20 20 61 69 55 73 65 64    }.  }.  aiUsed
3934f 5b 69 53 65 6c 65 63 74 5d 20 3d 20 31 3b 0a 20  [iSelect] = 1;. 
39350 20 72 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69   return &aCell[i
39351 53 65 6c 65 63 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  Select];.}../*.*
39352 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
39353 20 6f 66 20 74 68 65 20 71 75 61 64 72 61 74 69   of the quadrati
39354 63 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65  c variant of the
39355 20 50 69 63 6b 53 65 65 64 73 28 29 20 66 75 6e   PickSeeds() fun
39356 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75  ction from.** Gu
39357 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74  ttman[84]..*/.st
39358 61 74 69 63 20 76 6f 69 64 20 51 75 61 64 72 61  atic void Quadra
39359 74 69 63 50 69 63 6b 53 65 65 64 73 28 0a 20 20  ticPickSeeds(.  
3935a 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20  Rtree *pRtree,. 
3935b 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
3935c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c  l, .  int nCell,
3935d 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66 74 53   .  int *piLeftS
3935e 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a 70 69 52  eed, .  int *piR
3935f 69 67 68 74 53 65 65 64 0a 29 7b 0a 20 20 69 6e  ightSeed.){.  in
39360 74 20 69 69 3b 0a 20 20 69 6e 74 20 6a 6a 3b 0a  t ii;.  int jj;.
39361 0a 20 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64  .  int iLeftSeed
39362 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67   = 0;.  int iRig
39363 68 74 53 65 65 64 20 3d 20 31 3b 0a 20 20 66 6c  htSeed = 1;.  fl
39364 6f 61 74 20 66 57 61 73 74 65 20 3d 20 30 2e 30  oat fWaste = 0.0
39365 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
39366 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
39367 20 20 20 20 66 6f 72 28 6a 6a 3d 69 69 2b 31 3b      for(jj=ii+1;
39368 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29   jj<nCell; jj++)
39369 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69  {.      float ri
3936a 67 68 74 20 3d 20 63 65 6c 6c 41 72 65 61 28 70  ght = cellArea(p
3936b 52 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b 6a 6a  Rtree, &aCell[jj
3936c 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  ]);.      float 
3936d 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c 47 72 6f  growth = cellGro
3936e 77 74 68 28 70 52 74 72 65 65 2c 20 26 61 43 65  wth(pRtree, &aCe
3936f 6c 6c 5b 69 69 5d 2c 20 26 61 43 65 6c 6c 5b 6a  ll[ii], &aCell[j
39370 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  j]);.      float
39371 20 77 61 73 74 65 20 3d 20 67 72 6f 77 74 68 20   waste = growth 
39372 2d 20 72 69 67 68 74 3b 0a 0a 20 20 20 20 20 20  - right;..      
39373 69 66 28 20 77 61 73 74 65 3e 66 57 61 73 74 65  if( waste>fWaste
39374 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 65 66   ){.        iLef
39375 74 53 65 65 64 20 3d 20 69 69 3b 0a 20 20 20 20  tSeed = ii;.    
39376 20 20 20 20 69 52 69 67 68 74 53 65 65 64 20 3d      iRightSeed =
39377 20 6a 6a 3b 0a 20 20 20 20 20 20 20 20 66 57 61   jj;.        fWa
39378 73 74 65 20 3d 20 77 61 73 74 65 3b 0a 20 20 20  ste = waste;.   
39379 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
3937a 20 20 2a 70 69 4c 65 66 74 53 65 65 64 20 3d 20    *piLeftSeed = 
3937b 69 4c 65 66 74 53 65 65 64 3b 0a 20 20 2a 70 69  iLeftSeed;.  *pi
3937c 52 69 67 68 74 53 65 65 64 20 3d 20 69 52 69 67  RightSeed = iRig
3937d 68 74 53 65 65 64 3b 0a 7d 0a 23 65 6e 64 69 66  htSeed;.}.#endif
3937e 20 2f 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54   /* VARIANT_GUTT
3937f 4d 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50  MAN_QUADRATIC_SP
39380 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 72  LIT */../*.** Ar
39381 67 75 6d 65 6e 74 73 20 61 49 64 78 2c 20 61 44  guments aIdx, aD
39382 69 73 74 61 6e 63 65 20 61 6e 64 20 61 53 70 61  istance and aSpa
39383 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20  re all point to 
39384 61 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0a 2a  arrays of size.*
39385 2a 20 6e 49 64 78 2e 20 54 68 65 20 61 49 64 78  * nIdx. The aIdx
39386 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
39387 74 68 65 20 73 65 74 20 6f 66 20 69 6e 74 65 67  the set of integ
39388 65 72 73 20 66 72 6f 6d 20 30 20 74 6f 20 0a 2a  ers from 0 to .*
39389 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e 20 6e 6f  * (nIdx-1) in no
3938a 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
3938b 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
3938c 20 73 6f 72 74 73 20 74 68 65 20 76 61 6c 75 65   sorts the value
3938d 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20 61 63 63  s.** in aIdx acc
3938e 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 69 6e  ording to the in
3938f 64 65 78 65 64 20 76 61 6c 75 65 73 20 69 6e 20  dexed values in 
39390 61 44 69 73 74 61 6e 63 65 2e 20 46 6f 72 0a 2a  aDistance. For.*
39391 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d  * example, assum
39392 69 6e 67 20 74 68 65 20 69 6e 70 75 74 73 3a 0a  ing the inputs:.
39393 2a 2a 0a 2a 2a 20 20 20 61 49 64 78 20 20 20 20  **.**   aIdx    
39394 20 20 3d 20 7b 20 30 2c 20 20 20 31 2c 20 20 20    = { 0,   1,   
39395 32 2c 20 20 20 33 20 7d 0a 2a 2a 20 20 20 61 44  2,   3 }.**   aD
39396 69 73 74 61 6e 63 65 20 3d 20 7b 20 35 2e 30 2c  istance = { 5.0,
39397 20 32 2e 30 2c 20 37 2e 30 2c 20 36 2e 30 20 7d   2.0, 7.0, 6.0 }
39398 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  .**.** this func
39399 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 61 49  tion sets the aI
3939a 64 78 20 61 72 72 61 79 20 74 6f 20 63 6f 6e 74  dx array to cont
3939b 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64  ain:.**.**   aId
3939c 78 20 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20  x      = { 0,   
3939d 31 2c 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a  1,   2,   3 }.**
3939e 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20 61  .** The aSpare a
3939f 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73 20  rray is used as 
393a0 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69 6e  temporary workin
393a1 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0a 2a  g space by the.*
393a2 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72 69  * sorting algori
393a3 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  thm..*/.static v
393a4 6f 69 64 20 53 6f 72 74 42 79 44 69 73 74 61 6e  oid SortByDistan
393a5 63 65 28 0a 20 20 69 6e 74 20 2a 61 49 64 78 2c  ce(.  int *aIdx,
393a6 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c 20 0a 20   .  int nIdx, . 
393a7 20 66 6c 6f 61 74 20 2a 61 44 69 73 74 61 6e 63   float *aDistanc
393a8 65 2c 20 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  e, .  int *aSpar
393a9 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49 64 78 3e  e.){.  if( nIdx>
393aa 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4c 65  1 ){.    int iLe
393ab 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  ft = 0;.    int 
393ac 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 20  iRight = 0;..   
393ad 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20 6e 49 64   int nLeft = nId
393ae 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20 6e 52 69  x/2;.    int nRi
393af 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66 74  ght = nIdx-nLeft
393b0 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66 74  ;.    int *aLeft
393b1 20 3d 20 61 49 64 78 3b 0a 20 20 20 20 69 6e 74   = aIdx;.    int
393b2 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64 78   *aRight = &aIdx
393b3 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20 20 53 6f  [nLeft];..    So
393b4 72 74 42 79 44 69 73 74 61 6e 63 65 28 61 4c 65  rtByDistance(aLe
393b5 66 74 2c 20 6e 4c 65 66 74 2c 20 61 44 69 73 74  ft, nLeft, aDist
393b6 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a 20  ance, aSpare);. 
393b7 20 20 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63     SortByDistanc
393b8 65 28 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74  e(aRight, nRight
393b9 2c 20 61 44 69 73 74 61 6e 63 65 2c 20 61 53 70  , aDistance, aSp
393ba 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  are);..    memcp
393bb 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66 74 2c  y(aSpare, aLeft,
393bc 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65   sizeof(int)*nLe
393bd 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d  ft);.    aLeft =
393be 20 61 53 70 61 72 65 3b 0a 0a 20 20 20 20 77 68   aSpare;..    wh
393bf 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66 74  ile( iLeft<nLeft
393c0 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67 68   || iRight<nRigh
393c1 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  t ){.      if( i
393c2 4c 65 66 74 3d 3d 6e 4c 65 66 74 20 29 7b 0a 20  Left==nLeft ){. 
393c3 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66         aIdx[iLef
393c4 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52 69 67  t+iRight] = aRig
393c5 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20 20  ht[iRight];.    
393c6 20 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20      iRight++;.  
393c7 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 52      }else if( iR
393c8 69 67 68 74 3d 3d 6e 52 69 67 68 74 20 29 7b 0a  ight==nRight ){.
393c9 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65          aIdx[iLe
393ca 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 4c 65  ft+iRight] = aLe
393cb 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20  ft[iLeft];.     
393cc 20 20 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20     iLeft++;.    
393cd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
393ce 20 66 6c 6f 61 74 20 66 4c 65 66 74 20 3d 20 61   float fLeft = a
393cf 44 69 73 74 61 6e 63 65 5b 61 4c 65 66 74 5b 69  Distance[aLeft[i
393d0 4c 65 66 74 5d 5d 3b 0a 20 20 20 20 20 20 20 20  Left]];.        
393d1 66 6c 6f 61 74 20 66 52 69 67 68 74 20 3d 20 61  float fRight = a
393d2 44 69 73 74 61 6e 63 65 5b 61 52 69 67 68 74 5b  Distance[aRight[
393d3 69 52 69 67 68 74 5d 5d 3b 0a 20 20 20 20 20 20  iRight]];.      
393d4 20 20 69 66 28 20 66 4c 65 66 74 3c 66 52 69 67    if( fLeft<fRig
393d5 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ht ){.          
393d6 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68  aIdx[iLeft+iRigh
393d7 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74  t] = aLeft[iLeft
393d8 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 4c 65  ];.          iLe
393d9 66 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65  ft++;.        }e
393da 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61  lse{.          a
393db 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
393dc 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68  ] = aRight[iRigh
393dd 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 52  t];.          iR
393de 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ight++;.        
393df 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
393e0 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 43 68  .#if 0.    /* Ch
393e1 65 63 6b 20 74 68 61 74 20 74 68 65 20 73 6f 72  eck that the sor
393e2 74 20 77 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20  t worked */.    
393e3 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  {.      int jj;.
393e4 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20        for(jj=1; 
393e5 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a  jj<nIdx; jj++){.
393e6 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 6c 65          float le
393e7 66 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61  ft = aDistance[a
393e8 49 64 78 5b 6a 6a 2d 31 5d 5d 3b 0a 20 20 20 20  Idx[jj-1]];.    
393e9 20 20 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20      float right 
393ea 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 49 64 78  = aDistance[aIdx
393eb 5b 6a 6a 5d 5d 3b 0a 20 20 20 20 20 20 20 20 61  [jj]];.        a
393ec 73 73 65 72 74 28 20 6c 65 66 74 3c 3d 72 69 67  ssert( left<=rig
393ed 68 74 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ht );.      }.  
393ee 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
393ef 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
393f0 73 20 61 49 64 78 2c 20 61 43 65 6c 6c 20 61 6e  s aIdx, aCell an
393f1 64 20 61 53 70 61 72 65 20 61 6c 6c 20 70 6f 69  d aSpare all poi
393f2 6e 74 20 74 6f 20 61 72 72 61 79 73 20 6f 66 20  nt to arrays of 
393f3 73 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68  size.** nIdx. Th
393f4 65 20 61 49 64 78 20 61 72 72 61 79 20 63 6f 6e  e aIdx array con
393f5 74 61 69 6e 73 20 74 68 65 20 73 65 74 20 6f 66  tains the set of
393f6 20 69 6e 74 65 67 65 72 73 20 66 72 6f 6d 20 30   integers from 0
393f7 20 74 6f 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29   to .** (nIdx-1)
393f8 20 69 6e 20 6e 6f 20 70 61 72 74 69 63 75 6c 61   in no particula
393f9 72 20 6f 72 64 65 72 2e 20 54 68 69 73 20 66 75  r order. This fu
393fa 6e 63 74 69 6f 6e 20 73 6f 72 74 73 20 74 68 65  nction sorts the
393fb 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49   values.** in aI
393fc 64 78 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  dx according to 
393fd 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20 6f  dimension iDim o
393fe 66 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 61  f the cells in a
393ff 43 65 6c 6c 2e 20 54 68 65 0a 2a 2a 20 6d 69 6e  Cell. The.** min
39400 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 64 69  imum value of di
39401 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20 69 73 20  mension iDim is 
39402 63 6f 6e 73 69 64 65 72 65 64 20 66 69 72 73 74  considered first
39403 2c 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d  , the.** maximum
39404 20 75 73 65 64 20 74 6f 20 62 72 65 61 6b 20 74   used to break t
39405 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ies..**.** The a
39406 53 70 61 72 65 20 61 72 72 61 79 20 69 73 20 75  Spare array is u
39407 73 65 64 20 61 73 20 74 65 6d 70 6f 72 61 72 79  sed as temporary
39408 20 77 6f 72 6b 69 6e 67 20 73 70 61 63 65 20 62   working space b
39409 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74 69 6e 67  y the.** sorting
3940a 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73   algorithm..*/.s
3940b 74 61 74 69 63 20 76 6f 69 64 20 53 6f 72 74 42  tatic void SortB
3940c 79 44 69 6d 65 6e 73 69 6f 6e 28 0a 20 20 52 74  yDimension(.  Rt
3940d 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 69  ree *pRtree,.  i
3940e 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e 74  nt *aIdx, .  int
3940f 20 6e 49 64 78 2c 20 0a 20 20 69 6e 74 20 69 44   nIdx, .  int iD
39410 69 6d 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  im, .  RtreeCell
39411 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *aCell, .  int 
39412 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20 69 66 28  *aSpare.){.  if(
39413 20 6e 49 64 78 3e 31 20 29 7b 0a 0a 20 20 20 20   nIdx>1 ){..    
39414 69 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b 0a 20  int iLeft = 0;. 
39415 20 20 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20     int iRight = 
39416 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66  0;..    int nLef
39417 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20 20 20 20  t = nIdx/2;.    
39418 69 6e 74 20 6e 52 69 67 68 74 20 3d 20 6e 49 64  int nRight = nId
39419 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74  x-nLeft;.    int
3941a 20 2a 61 4c 65 66 74 20 3d 20 61 49 64 78 3b 0a   *aLeft = aIdx;.
3941b 20 20 20 20 69 6e 74 20 2a 61 52 69 67 68 74 20      int *aRight 
3941c 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a  = &aIdx[nLeft];.
3941d 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65 6e  .    SortByDimen
3941e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 4c 65  sion(pRtree, aLe
3941f 66 74 2c 20 6e 4c 65 66 74 2c 20 69 44 69 6d 2c  ft, nLeft, iDim,
39420 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29 3b   aCell, aSpare);
39421 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65 6e  .    SortByDimen
39422 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 52 69  sion(pRtree, aRi
39423 67 68 74 2c 20 6e 52 69 67 68 74 2c 20 69 44 69  ght, nRight, iDi
39424 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65  m, aCell, aSpare
39425 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 61  );..    memcpy(a
39426 53 70 61 72 65 2c 20 61 4c 65 66 74 2c 20 73 69  Spare, aLeft, si
39427 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65 66 74 29  zeof(int)*nLeft)
39428 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d 20 61 53  ;.    aLeft = aS
39429 70 61 72 65 3b 0a 20 20 20 20 77 68 69 6c 65 28  pare;.    while(
3942a 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20   iLeft<nLeft || 
3942b 69 52 69 67 68 74 3c 6e 52 69 67 68 74 20 29 7b  iRight<nRight ){
3942c 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 6c  .      double xl
3942d 65 66 74 31 20 3d 20 44 43 4f 4f 52 44 28 61 43  eft1 = DCOORD(aC
3942e 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d  ell[aLeft[iLeft]
3942f 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d  ].aCoord[iDim*2]
39430 29 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  );.      double 
39431 78 6c 65 66 74 32 20 3d 20 44 43 4f 4f 52 44 28  xleft2 = DCOORD(
39432 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65 66  aCell[aLeft[iLef
39433 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a  t]].aCoord[iDim*
39434 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75  2+1]);.      dou
39435 62 6c 65 20 78 72 69 67 68 74 31 20 3d 20 44 43  ble xright1 = DC
39436 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68  OORD(aCell[aRigh
39437 74 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72  t[iRight]].aCoor
39438 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20  d[iDim*2]);.    
39439 20 20 64 6f 75 62 6c 65 20 78 72 69 67 68 74 32    double xright2
3943a 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
3943b 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e  aRight[iRight]].
3943c 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
3943d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 4c  );.      if( (iL
3943e 65 66 74 21 3d 6e 4c 65 66 74 29 20 26 26 20 28  eft!=nLeft) && (
3943f 28 69 52 69 67 68 74 3d 3d 6e 52 69 67 68 74 29  (iRight==nRight)
39440 0a 20 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66  .       || (xlef
39441 74 31 3c 78 72 69 67 68 74 31 29 0a 20 20 20 20  t1<xright1).    
39442 20 20 20 7c 7c 20 28 78 6c 65 66 74 31 3d 3d 78     || (xleft1==x
39443 72 69 67 68 74 31 20 26 26 20 78 6c 65 66 74 32  right1 && xleft2
39444 3c 78 72 69 67 68 74 32 29 0a 20 20 20 20 20 20  <xright2).      
39445 29 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78  )){.        aIdx
39446 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d  [iLeft+iRight] =
39447 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20   aLeft[iLeft];. 
39448 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a         iLeft++;.
39449 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3944a 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
3944b 69 52 69 67 68 74 5d 20 3d 20 61 52 69 67 68 74  iRight] = aRight
3944c 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20 20 20 20  [iRight];.      
3944d 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20 20    iRight++;.    
3944e 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30    }.    }..#if 0
3944f 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
39450 61 74 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b  at the sort work
39451 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20  ed */.    {.    
39452 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
39453 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64  for(jj=1; jj<nId
39454 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  x; jj++){.      
39455 20 20 66 6c 6f 61 74 20 78 6c 65 66 74 31 20 3d    float xleft1 =
39456 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31   aCell[aIdx[jj-1
39457 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
39458 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ];.        float
39459 20 78 6c 65 66 74 32 20 3d 20 61 43 65 6c 6c 5b   xleft2 = aCell[
3945a 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f 6f  aIdx[jj-1]].aCoo
3945b 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b 0a 20 20  rd[iDim*2+1];.  
3945c 20 20 20 20 20 20 66 6c 6f 61 74 20 78 72 69 67        float xrig
3945d 68 74 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78  ht1 = aCell[aIdx
3945e 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  [jj]].aCoord[iDi
3945f 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  m*2];.        fl
39460 6f 61 74 20 78 72 69 67 68 74 32 20 3d 20 61 43  oat xright2 = aC
39461 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43  ell[aIdx[jj]].aC
39462 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b 0a  oord[iDim*2+1];.
39463 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
39464 78 6c 65 66 74 31 3c 3d 78 72 69 67 68 74 31 20  xleft1<=xright1 
39465 26 26 20 28 78 6c 65 66 74 31 3c 78 72 69 67 68  && (xleft1<xrigh
39466 74 31 20 7c 7c 20 78 6c 65 66 74 32 3c 3d 78 72  t1 || xleft2<=xr
39467 69 67 68 74 32 29 20 29 3b 0a 20 20 20 20 20 20  ight2) );.      
39468 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
39469 20 7d 0a 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e   }.}..#if VARIAN
3946a 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
3946b 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  T./*.** Implemen
3946c 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 52 2a  tation of the R*
3946d 2d 74 72 65 65 20 76 61 72 69 61 6e 74 20 6f 66  -tree variant of
3946e 20 53 70 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20   SplitNode from 
3946f 42 65 63 6b 6d 61 6e 5b 31 39 39 30 5d 2e 0a 2a  Beckman[1990]..*
39470 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 70 6c  /.static int spl
39471 69 74 4e 6f 64 65 53 74 61 72 74 72 65 65 28 0a  itNodeStartree(.
39472 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
39473 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43  .  RtreeCell *aC
39474 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ell,.  int nCell
39475 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ,.  RtreeNode *p
39476 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 4e 6f 64  Left,.  RtreeNod
39477 65 20 2a 70 52 69 67 68 74 2c 0a 20 20 52 74 72  e *pRight,.  Rtr
39478 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 4c 65 66  eeCell *pBboxLef
39479 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  t,.  RtreeCell *
3947a 70 42 62 6f 78 52 69 67 68 74 0a 29 7b 0a 20 20  pBboxRight.){.  
3947b 69 6e 74 20 2a 2a 61 61 53 6f 72 74 65 64 3b 0a  int **aaSorted;.
3947c 20 20 69 6e 74 20 2a 61 53 70 61 72 65 3b 0a 20    int *aSpare;. 
3947d 20 69 6e 74 20 69 69 3b 0a 0a 20 20 69 6e 74 20   int ii;..  int 
3947e 69 42 65 73 74 44 69 6d 3b 0a 20 20 69 6e 74 20  iBestDim;.  int 
3947f 69 42 65 73 74 53 70 6c 69 74 3b 0a 20 20 66 6c  iBestSplit;.  fl
39480 6f 61 74 20 66 42 65 73 74 4d 61 72 67 69 6e 3b  oat fBestMargin;
39481 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  ..  int nByte = 
39482 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2b 31 29  (pRtree->nDim+1)
39483 2a 28 73 69 7a 65 6f 66 28 69 6e 74 2a 29 2b 6e  *(sizeof(int*)+n
39484 43 65 6c 6c 2a 73 69 7a 65 6f 66 28 69 6e 74 29  Cell*sizeof(int)
39485 29 3b 0a 0a 20 20 61 61 53 6f 72 74 65 64 20 3d  );..  aaSorted =
39486 20 28 69 6e 74 20 2a 2a 29 73 71 6c 69 74 65 33   (int **)sqlite3
39487 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a  _malloc(nByte);.
39488 20 20 69 66 28 20 21 61 61 53 6f 72 74 65 64 20    if( !aaSorted 
39489 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3948a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3948b 0a 20 20 61 53 70 61 72 65 20 3d 20 26 28 28 69  .  aSpare = &((i
3948c 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64 5b 70  nt *)&aaSorted[p
3948d 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b 70 52  Rtree->nDim])[pR
3948e 74 72 65 65 2d 3e 6e 44 69 6d 2a 6e 43 65 6c 6c  tree->nDim*nCell
3948f 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 61 53 6f  ];.  memset(aaSo
39490 72 74 65 64 2c 20 30 2c 20 6e 42 79 74 65 29 3b  rted, 0, nByte);
39491 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
39492 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 69  pRtree->nDim; ii
39493 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b  ++){.    int jj;
39494 0a 20 20 20 20 61 61 53 6f 72 74 65 64 5b 69 69  .    aaSorted[ii
39495 5d 20 3d 20 26 28 28 69 6e 74 20 2a 29 26 61 61  ] = &((int *)&aa
39496 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d 3e 6e  Sorted[pRtree->n
39497 44 69 6d 5d 29 5b 69 69 2a 6e 43 65 6c 6c 5d 3b  Dim])[ii*nCell];
39498 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a  .    for(jj=0; j
39499 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a  j<nCell; jj++){.
3949a 20 20 20 20 20 20 61 61 53 6f 72 74 65 64 5b 69        aaSorted[i
3949b 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a 3b 0a 20 20 20  i][jj] = jj;.   
3949c 20 7d 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d   }.    SortByDim
3949d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61  ension(pRtree, a
3949e 61 53 6f 72 74 65 64 5b 69 69 5d 2c 20 6e 43 65  aSorted[ii], nCe
3949f 6c 6c 2c 20 69 69 2c 20 61 43 65 6c 6c 2c 20 61  ll, ii, aCell, a
394a0 53 70 61 72 65 29 3b 0a 20 20 7d 0a 0a 20 20 66  Spare);.  }..  f
394a1 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72  or(ii=0; ii<pRtr
394a2 65 65 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b  ee->nDim; ii++){
394a3 0a 20 20 20 20 66 6c 6f 61 74 20 6d 61 72 67 69  .    float margi
394a4 6e 20 3d 20 30 2e 30 3b 0a 20 20 20 20 66 6c 6f  n = 0.0;.    flo
394a5 61 74 20 66 42 65 73 74 4f 76 65 72 6c 61 70 3b  at fBestOverlap;
394a6 0a 20 20 20 20 66 6c 6f 61 74 20 66 42 65 73 74  .    float fBest
394a7 41 72 65 61 3b 0a 20 20 20 20 69 6e 74 20 69 42  Area;.    int iB
394a8 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74  estLeft;.    int
394a9 20 6e 4c 65 66 74 3b 0a 0a 20 20 20 20 66 6f 72   nLeft;..    for
394aa 28 0a 20 20 20 20 20 20 6e 4c 65 66 74 3d 52 54  (.      nLeft=RT
394ab 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
394ac 72 65 65 29 3b 20 0a 20 20 20 20 20 20 6e 4c 65  ree); .      nLe
394ad 66 74 3c 3d 28 6e 43 65 6c 6c 2d 52 54 52 45 45  ft<=(nCell-RTREE
394ae 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
394af 29 29 3b 20 0a 20 20 20 20 20 20 6e 4c 65 66 74  )); .      nLeft
394b0 2b 2b 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  ++.    ){.      
394b1 52 74 72 65 65 43 65 6c 6c 20 6c 65 66 74 3b 0a  RtreeCell left;.
394b2 20 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20        RtreeCell 
394b3 72 69 67 68 74 3b 0a 20 20 20 20 20 20 69 6e 74  right;.      int
394b4 20 6b 6b 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74   kk;.      float
394b5 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20   overlap;.      
394b6 66 6c 6f 61 74 20 61 72 65 61 3b 0a 0a 20 20 20  float area;..   
394b7 20 20 20 6d 65 6d 63 70 79 28 26 6c 65 66 74 2c     memcpy(&left,
394b8 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64   &aCell[aaSorted
394b9 5b 69 69 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66  [ii][0]], sizeof
394ba 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
394bb 20 20 20 20 6d 65 6d 63 70 79 28 26 72 69 67 68      memcpy(&righ
394bc 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74  t, &aCell[aaSort
394bd 65 64 5b 69 69 5d 5b 6e 43 65 6c 6c 2d 31 5d 5d  ed[ii][nCell-1]]
394be 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65  , sizeof(RtreeCe
394bf 6c 6c 29 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  ll));.      for(
394c0 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e 43 65 6c 6c 2d  kk=1; kk<(nCell-
394c1 31 29 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20  1); kk++){.     
394c2 20 20 20 69 66 28 20 6b 6b 3c 6e 4c 65 66 74 20     if( kk<nLeft 
394c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 65 6c  ){.          cel
394c4 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
394c5 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  left, &aCell[aaS
394c6 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29 3b  orted[ii][kk]]);
394c7 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
394c8 20 20 20 20 20 20 20 20 20 20 63 65 6c 6c 55 6e            cellUn
394c9 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 72 69 67  ion(pRtree, &rig
394ca 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  ht, &aCell[aaSor
394cb 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20  ted[ii][kk]]);. 
394cc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
394cd 0a 20 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d  .      margin +=
394ce 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72   cellMargin(pRtr
394cf 65 65 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20 20  ee, &left);.    
394d0 20 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c    margin += cell
394d1 4d 61 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26  Margin(pRtree, &
394d2 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 6f 76  right);.      ov
394d3 65 72 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72  erlap = cellOver
394d4 6c 61 70 28 70 52 74 72 65 65 2c 20 26 6c 65 66  lap(pRtree, &lef
394d5 74 2c 20 26 72 69 67 68 74 2c 20 31 2c 20 2d 31  t, &right, 1, -1
394d6 29 3b 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20  );.      area = 
394d7 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
394d8 20 26 6c 65 66 74 29 20 2b 20 63 65 6c 6c 41 72   &left) + cellAr
394d9 65 61 28 70 52 74 72 65 65 2c 20 26 72 69 67 68  ea(pRtree, &righ
394da 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 6e  t);.      if( (n
394db 4c 65 66 74 3d 3d 52 54 52 45 45 5f 4d 49 4e 43  Left==RTREE_MINC
394dc 45 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20 20  ELLS(pRtree)).  
394dd 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70       || (overlap
394de 3c 66 42 65 73 74 4f 76 65 72 6c 61 70 29 0a 20  <fBestOverlap). 
394df 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61        || (overla
394e0 70 3d 3d 66 42 65 73 74 4f 76 65 72 6c 61 70 20  p==fBestOverlap 
394e1 26 26 20 61 72 65 61 3c 66 42 65 73 74 41 72 65  && area<fBestAre
394e2 61 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  a).      ){.    
394e3 20 20 20 20 69 42 65 73 74 4c 65 66 74 20 3d 20      iBestLeft = 
394e4 6e 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 66  nLeft;.        f
394e5 42 65 73 74 4f 76 65 72 6c 61 70 20 3d 20 6f 76  BestOverlap = ov
394e6 65 72 6c 61 70 3b 0a 20 20 20 20 20 20 20 20 66  erlap;.        f
394e7 42 65 73 74 41 72 65 61 20 3d 20 61 72 65 61 3b  BestArea = area;
394e8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
394e9 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20 7c 7c      if( ii==0 ||
394ea 20 6d 61 72 67 69 6e 3c 66 42 65 73 74 4d 61 72   margin<fBestMar
394eb 67 69 6e 20 29 7b 0a 20 20 20 20 20 20 69 42 65  gin ){.      iBe
394ec 73 74 44 69 6d 20 3d 20 69 69 3b 0a 20 20 20 20  stDim = ii;.    
394ed 20 20 66 42 65 73 74 4d 61 72 67 69 6e 20 3d 20    fBestMargin = 
394ee 6d 61 72 67 69 6e 3b 0a 20 20 20 20 20 20 69 42  margin;.      iB
394ef 65 73 74 53 70 6c 69 74 20 3d 20 69 42 65 73 74  estSplit = iBest
394f0 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Left;.    }.  }.
394f1 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c  .  memcpy(pBboxL
394f2 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f  eft, &aCell[aaSo
394f3 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b 30  rted[iBestDim][0
394f4 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ]], sizeof(Rtree
394f5 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79  Cell));.  memcpy
394f6 28 70 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43  (pBboxRight, &aC
394f7 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42 65  ell[aaSorted[iBe
394f8 73 74 44 69 6d 5d 5b 69 42 65 73 74 53 70 6c 69  stDim][iBestSpli
394f9 74 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  t]], sizeof(Rtre
394fa 65 43 65 6c 6c 29 29 3b 0a 20 20 66 6f 72 28 69  eCell));.  for(i
394fb 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
394fc 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  i++){.    RtreeN
394fd 6f 64 65 20 2a 70 54 61 72 67 65 74 20 3d 20 28  ode *pTarget = (
394fe 69 69 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70  ii<iBestSplit)?p
394ff 4c 65 66 74 3a 70 52 69 67 68 74 3b 0a 20 20 20  Left:pRight;.   
39500 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f   RtreeCell *pBbo
39501 78 20 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c  x = (ii<iBestSpl
39502 69 74 29 3f 70 42 62 6f 78 4c 65 66 74 3a 70 42  it)?pBboxLeft:pB
39503 62 6f 78 52 69 67 68 74 3b 0a 20 20 20 20 52 74  boxRight;.    Rt
39504 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 3d  reeCell *pCell =
39505 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64   &aCell[aaSorted
39506 5b 69 42 65 73 74 44 69 6d 5d 5b 69 69 5d 5d 3b  [iBestDim][ii]];
39507 0a 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43  .    nodeInsertC
39508 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 54 61 72  ell(pRtree, pTar
39509 67 65 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  get, pCell);.   
3950a 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
3950b 65 2c 20 70 42 62 6f 78 2c 20 70 43 65 6c 6c 29  e, pBbox, pCell)
3950c 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
3950d 5f 66 72 65 65 28 61 61 53 6f 72 74 65 64 29 3b  _free(aaSorted);
3950e 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3950f 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  _OK;.}.#endif..#
39510 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  if VARIANT_GUTTM
39511 41 4e 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49  AN_SPLIT./*.** I
39512 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
39513 20 74 68 65 20 72 65 67 75 6c 61 72 20 52 2d 74   the regular R-t
39514 72 65 65 20 53 70 6c 69 74 4e 6f 64 65 20 66 72  ree SplitNode fr
39515 6f 6d 20 47 75 74 74 6d 61 6e 5b 31 39 38 34 5d  om Guttman[1984]
39516 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39517 73 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e  splitNodeGuttman
39518 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
39519 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e,.  RtreeCell *
3951a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43 65  aCell,.  int nCe
3951b 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ll,.  RtreeNode 
3951c 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 4e  *pLeft,.  RtreeN
3951d 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a 20 20 52  ode *pRight,.  R
3951e 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 4c  treeCell *pBboxL
3951f 65 66 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  eft,.  RtreeCell
39520 20 2a 70 42 62 6f 78 52 69 67 68 74 0a 29 7b 0a   *pBboxRight.){.
39521 20 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20    int iLeftSeed 
39522 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68  = 0;.  int iRigh
39523 74 53 65 65 64 20 3d 20 31 3b 0a 20 20 69 6e 74  tSeed = 1;.  int
39524 20 2a 61 69 55 73 65 64 3b 0a 20 20 69 6e 74 20   *aiUsed;.  int 
39525 69 3b 0a 0a 20 20 61 69 55 73 65 64 20 3d 20 73  i;..  aiUsed = s
39526 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
39527 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29  zeof(int)*nCell)
39528 3b 0a 20 20 69 66 28 20 21 61 69 55 73 65 64 20  ;.  if( !aiUsed 
39529 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3952a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3952b 20 20 6d 65 6d 73 65 74 28 61 69 55 73 65 64 2c    memset(aiUsed,
3952c 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a   0, sizeof(int)*
3952d 6e 43 65 6c 6c 29 3b 0a 0a 20 20 50 69 63 6b 53  nCell);..  PickS
3952e 65 65 64 73 28 70 52 74 72 65 65 2c 20 61 43 65  eeds(pRtree, aCe
3952f 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 26 69 4c 65 66  ll, nCell, &iLef
39530 74 53 65 65 64 2c 20 26 69 52 69 67 68 74 53 65  tSeed, &iRightSe
39531 65 64 29 3b 0a 0a 20 20 6d 65 6d 63 70 79 28 70  ed);..  memcpy(p
39532 42 62 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c  BboxLeft, &aCell
39533 5b 69 4c 65 66 74 53 65 65 64 5d 2c 20 73 69 7a  [iLeftSeed], siz
39534 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
39535 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 52  .  memcpy(pBboxR
39536 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 69 52 69  ight, &aCell[iRi
39537 67 68 74 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66  ghtSeed], sizeof
39538 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
39539 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
3953a 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20 26 61  Rtree, pLeft, &a
3953b 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65 64 5d 29  Cell[iLeftSeed])
3953c 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65  ;.  nodeInsertCe
3953d 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68  ll(pRtree, pRigh
3953e 74 2c 20 26 61 43 65 6c 6c 5b 69 52 69 67 68 74  t, &aCell[iRight
3953f 53 65 65 64 5d 29 3b 0a 20 20 61 69 55 73 65 64  Seed]);.  aiUsed
39540 5b 69 4c 65 66 74 53 65 65 64 5d 20 3d 20 31 3b  [iLeftSeed] = 1;
39541 0a 20 20 61 69 55 73 65 64 5b 69 52 69 67 68 74  .  aiUsed[iRight
39542 53 65 65 64 5d 20 3d 20 31 3b 0a 0a 20 20 66 6f  Seed] = 1;..  fo
39543 72 28 69 3d 6e 43 65 6c 6c 2d 32 3b 20 69 3e 30  r(i=nCell-2; i>0
39544 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 52 74 72 65  ; i--){.    Rtre
39545 65 43 65 6c 6c 20 2a 70 4e 65 78 74 3b 0a 20 20  eCell *pNext;.  
39546 20 20 70 4e 65 78 74 20 3d 20 50 69 63 6b 4e 65    pNext = PickNe
39547 78 74 28 70 52 74 72 65 65 2c 20 61 43 65 6c 6c  xt(pRtree, aCell
39548 2c 20 6e 43 65 6c 6c 2c 20 70 42 62 6f 78 4c 65  , nCell, pBboxLe
39549 66 74 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20  ft, pBboxRight, 
3954a 61 69 55 73 65 64 29 3b 0a 20 20 20 20 66 6c 6f  aiUsed);.    flo
3954b 61 74 20 64 69 66 66 20 3d 20 20 0a 20 20 20 20  at diff =  .    
3954c 20 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74    cellGrowth(pRt
3954d 72 65 65 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20  ree, pBboxLeft, 
3954e 70 4e 65 78 74 29 20 2d 20 0a 20 20 20 20 20 20  pNext) - .      
3954f 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65  cellGrowth(pRtre
39550 65 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20 70  e, pBboxRight, p
39551 4e 65 78 74 29 0a 20 20 20 20 3b 0a 20 20 20 20  Next).    ;.    
39552 69 66 28 20 28 52 54 52 45 45 5f 4d 49 4e 43 45  if( (RTREE_MINCE
39553 4c 4c 53 28 70 52 74 72 65 65 29 2d 4e 43 45 4c  LLS(pRtree)-NCEL
39554 4c 28 70 52 69 67 68 74 29 3d 3d 69 29 0a 20 20  L(pRight)==i).  
39555 20 20 20 7c 7c 20 28 64 69 66 66 3e 30 2e 30 20     || (diff>0.0 
39556 26 26 20 28 52 54 52 45 45 5f 4d 49 4e 43 45 4c  && (RTREE_MINCEL
39557 4c 53 28 70 52 74 72 65 65 29 2d 4e 43 45 4c 4c  LS(pRtree)-NCELL
39558 28 70 4c 65 66 74 29 21 3d 69 29 29 0a 20 20 20  (pLeft)!=i)).   
39559 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65 49 6e   ){.      nodeIn
3955a 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
3955b 20 70 52 69 67 68 74 2c 20 70 4e 65 78 74 29 3b   pRight, pNext);
3955c 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e  .      cellUnion
3955d 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52 69  (pRtree, pBboxRi
3955e 67 68 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20  ght, pNext);.   
3955f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f   }else{.      no
39560 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
39561 72 65 65 2c 20 70 4c 65 66 74 2c 20 70 4e 65 78  ree, pLeft, pNex
39562 74 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e  t);.      cellUn
39563 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62 6f  ion(pRtree, pBbo
39564 78 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b 0a 20  xLeft, pNext);. 
39565 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
39566 74 65 33 5f 66 72 65 65 28 61 69 55 73 65 64 29  te3_free(aiUsed)
39567 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
39568 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  E_OK;.}.#endif..
39569 73 74 61 74 69 63 20 69 6e 74 20 75 70 64 61 74  static int updat
3956a 65 4d 61 70 70 69 6e 67 28 0a 20 20 52 74 72 65  eMapping(.  Rtre
3956b 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 69 36  e *pRtree, .  i6
3956c 34 20 69 52 6f 77 69 64 2c 20 0a 20 20 52 74 72  4 iRowid, .  Rtr
3956d 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a  eeNode *pNode, .
3956e 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b    int iHeight.){
3956f 0a 20 20 69 6e 74 20 28 2a 78 53 65 74 4d 61 70  .  int (*xSetMap
39570 70 69 6e 67 29 28 52 74 72 65 65 20 2a 2c 20 73  ping)(Rtree *, s
39571 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 73 71  qlite3_int64, sq
39572 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 20 20  lite3_int64);.  
39573 78 53 65 74 4d 61 70 70 69 6e 67 20 3d 20 28 28  xSetMapping = ((
39574 69 48 65 69 67 68 74 3d 3d 30 29 3f 72 6f 77 69  iHeight==0)?rowi
39575 64 57 72 69 74 65 3a 70 61 72 65 6e 74 57 72 69  dWrite:parentWri
39576 74 65 29 3b 0a 20 20 69 66 28 20 69 48 65 69 67  te);.  if( iHeig
39577 68 74 3e 30 20 29 7b 0a 20 20 20 20 52 74 72 65  ht>0 ){.    Rtre
39578 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64 20 3d 20  eNode *pChild = 
39579 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70  nodeHashLookup(p
3957a 52 74 72 65 65 2c 20 69 52 6f 77 69 64 29 3b 0a  Rtree, iRowid);.
3957b 20 20 20 20 69 66 28 20 70 43 68 69 6c 64 20 29      if( pChild )
3957c 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65  {.      nodeRele
3957d 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68 69  ase(pRtree, pChi
3957e 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  ld->pParent);.  
3957f 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63      nodeReferenc
39580 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  e(pNode);.      
39581 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 20  pChild->pParent 
39582 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 7d 0a 20  = pNode;.    }. 
39583 20 7d 0a 20 20 72 65 74 75 72 6e 20 78 53 65 74   }.  return xSet
39584 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20  Mapping(pRtree, 
39585 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69  iRowid, pNode->i
39586 4e 6f 64 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  Node);.}..static
39587 20 69 6e 74 20 53 70 6c 69 74 4e 6f 64 65 28 0a   int SplitNode(.
39588 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
39589 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
3958a 6f 64 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ode,.  RtreeCell
3958b 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69   *pCell,.  int i
3958c 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  Height.){.  int 
3958d 69 3b 0a 20 20 69 6e 74 20 6e 65 77 43 65 6c 6c  i;.  int newCell
3958e 49 73 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20  IsRight = 0;..  
3958f 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
39590 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20  OK;.  int nCell 
39591 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a  = NCELL(pNode);.
39592 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
39593 6c 6c 3b 0a 20 20 69 6e 74 20 2a 61 69 55 73 65  ll;.  int *aiUse
39594 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  d;..  RtreeNode 
39595 2a 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 52 74  *pLeft = 0;.  Rt
39596 72 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 20  reeNode *pRight 
39597 3d 20 30 3b 0a 0a 20 20 52 74 72 65 65 43 65 6c  = 0;..  RtreeCel
39598 6c 20 6c 65 66 74 62 62 6f 78 3b 0a 20 20 52 74  l leftbbox;.  Rt
39599 72 65 65 43 65 6c 6c 20 72 69 67 68 74 62 62 6f  reeCell rightbbo
3959a 78 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  x;..  /* Allocat
3959b 65 20 61 6e 20 61 72 72 61 79 20 61 6e 64 20 70  e an array and p
3959c 6f 70 75 6c 61 74 65 20 69 74 20 77 69 74 68 20  opulate it with 
3959d 61 20 63 6f 70 79 20 6f 66 20 70 43 65 6c 6c 20  a copy of pCell 
3959e 61 6e 64 20 0a 20 20 2a 2a 20 61 6c 6c 20 63 65  and .  ** all ce
3959f 6c 6c 73 20 66 72 6f 6d 20 6e 6f 64 65 20 70 4c  lls from node pL
395a0 65 66 74 2e 20 54 68 65 6e 20 7a 65 72 6f 20 74  eft. Then zero t
395a1 68 65 20 6f 72 69 67 69 6e 61 6c 20 6e 6f 64 65  he original node
395a2 2e 0a 20 20 2a 2f 0a 20 20 61 43 65 6c 6c 20 3d  ..  */.  aCell =
395a3 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
395a4 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c  (sizeof(RtreeCel
395a5 6c 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29 29 2a  l)+sizeof(int))*
395a6 28 6e 43 65 6c 6c 2b 31 29 29 3b 0a 20 20 69 66  (nCell+1));.  if
395a7 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20  ( !aCell ){.    
395a8 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
395a9 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  M;.    goto spli
395aa 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20  tnode_out;.  }. 
395ab 20 61 69 55 73 65 64 20 3d 20 28 69 6e 74 20 2a   aiUsed = (int *
395ac 29 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 2b 31 5d  )&aCell[nCell+1]
395ad 3b 0a 20 20 6d 65 6d 73 65 74 28 61 69 55 73 65  ;.  memset(aiUse
395ae 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e 74  d, 0, sizeof(int
395af 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b 0a 20 20  )*(nCell+1));.  
395b0 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c  for(i=0; i<nCell
395b1 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 64 65  ; i++){.    node
395b2 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
395b3 70 4e 6f 64 65 2c 20 69 2c 20 26 61 43 65 6c 6c  pNode, i, &aCell
395b4 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6e 6f 64 65  [i]);.  }.  node
395b5 5a 65 72 6f 28 70 52 74 72 65 65 2c 20 70 4e 6f  Zero(pRtree, pNo
395b6 64 65 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 61  de);.  memcpy(&a
395b7 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 70 43 65  Cell[nCell], pCe
395b8 6c 6c 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ll, sizeof(Rtree
395b9 43 65 6c 6c 29 29 3b 0a 20 20 6e 43 65 6c 6c 2b  Cell));.  nCell+
395ba 2b 3b 0a 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d  +;..  if( pNode-
395bb 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20  >iNode==1 ){.   
395bc 20 70 52 69 67 68 74 20 3d 20 6e 6f 64 65 4e 65   pRight = nodeNe
395bd 77 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  w(pRtree, pNode,
395be 20 31 29 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d   1);.    pLeft =
395bf 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c   nodeNew(pRtree,
395c0 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20 20 20 20   pNode, 1);.    
395c1 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2b 2b  pRtree->iDepth++
395c2 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44  ;.    pNode->isD
395c3 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 77 72  irty = 1;.    wr
395c4 69 74 65 49 6e 74 31 36 28 70 4e 6f 64 65 2d 3e  iteInt16(pNode->
395c5 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e 69  zData, pRtree->i
395c6 44 65 70 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b  Depth);.  }else{
395c7 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4e 6f  .    pLeft = pNo
395c8 64 65 3b 0a 20 20 20 20 70 52 69 67 68 74 20 3d  de;.    pRight =
395c9 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c   nodeNew(pRtree,
395ca 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 2c   pLeft->pParent,
395cb 20 31 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66   1);.    nodeRef
395cc 65 72 65 6e 63 65 28 70 4c 65 66 74 29 3b 0a 20  erence(pLeft);. 
395cd 20 7d 0a 0a 20 20 69 66 28 20 21 70 4c 65 66 74   }..  if( !pLeft
395ce 20 7c 7c 20 21 70 52 69 67 68 74 20 29 7b 0a 20   || !pRight ){. 
395cf 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
395d0 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 73  OMEM;.    goto s
395d1 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
395d2 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 70 4c 65 66  }..  memset(pLef
395d3 74 2d 3e 7a 44 61 74 61 2c 20 30 2c 20 70 52 74  t->zData, 0, pRt
395d4 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
395d5 0a 20 20 6d 65 6d 73 65 74 28 70 52 69 67 68 74  .  memset(pRight
395d6 2d 3e 7a 44 61 74 61 2c 20 30 2c 20 70 52 74 72  ->zData, 0, pRtr
395d7 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a  ee->iNodeSize);.
395d8 0a 20 20 72 63 20 3d 20 41 73 73 69 67 6e 43 65  .  rc = AssignCe
395d9 6c 6c 73 28 70 52 74 72 65 65 2c 20 61 43 65 6c  lls(pRtree, aCel
395da 6c 2c 20 6e 43 65 6c 6c 2c 20 70 4c 65 66 74 2c  l, nCell, pLeft,
395db 20 70 52 69 67 68 74 2c 20 26 6c 65 66 74 62 62   pRight, &leftbb
395dc 6f 78 2c 20 26 72 69 67 68 74 62 62 6f 78 29 3b  ox, &rightbbox);
395dd 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
395de 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
395df 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
395e0 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65    }..  /* Ensure
395e1 20 62 6f 74 68 20 63 68 69 6c 64 20 6e 6f 64 65   both child node
395e2 73 20 68 61 76 65 20 6e 6f 64 65 20 6e 75 6d 62  s have node numb
395e3 65 72 73 20 61 73 73 69 67 6e 65 64 20 74 6f 20  ers assigned to 
395e4 74 68 65 6d 2e 20 2a 2f 0a 20 20 69 66 28 20 28  them. */.  if( (
395e5 30 3d 3d 70 52 69 67 68 74 2d 3e 69 4e 6f 64 65  0==pRight->iNode
395e6 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   && SQLITE_OK!=(
395e7 72 63 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70  rc = nodeWrite(p
395e8 52 74 72 65 65 2c 20 70 52 69 67 68 74 29 29 29  Rtree, pRight)))
395e9 0a 20 20 20 7c 7c 20 28 30 3d 3d 70 4c 65 66 74  .   || (0==pLeft
395ea 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51 4c 49 54  ->iNode && SQLIT
395eb 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65  E_OK!=(rc = node
395ec 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 4c  Write(pRtree, pL
395ed 65 66 74 29 29 29 0a 20 20 29 7b 0a 20 20 20 20  eft))).  ){.    
395ee 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f  goto splitnode_o
395ef 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 69 67 68 74  ut;.  }..  right
395f0 62 62 6f 78 2e 69 52 6f 77 69 64 20 3d 20 70 52  bbox.iRowid = pR
395f1 69 67 68 74 2d 3e 69 4e 6f 64 65 3b 0a 20 20 6c  ight->iNode;.  l
395f2 65 66 74 62 62 6f 78 2e 69 52 6f 77 69 64 20 3d  eftbbox.iRowid =
395f3 20 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 3b 0a 0a   pLeft->iNode;..
395f4 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
395f5 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 72 63 20  de==1 ){.    rc 
395f6 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  = rtreeInsertCel
395f7 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2d  l(pRtree, pLeft-
395f8 3e 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62  >pParent, &leftb
395f9 62 6f 78 2c 20 69 48 65 69 67 68 74 2b 31 29 3b  box, iHeight+1);
395fa 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
395fb 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
395fc 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f  goto splitnode_o
395fd 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ut;.    }.  }els
395fe 65 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  e{.    RtreeNode
395ff 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4c 65 66   *pParent = pLef
39600 74 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20  t->pParent;.    
39601 69 6e 74 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  int iCell = node
39602 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72  ParentIndex(pRtr
39603 65 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  ee, pLeft);.    
39604 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c  nodeOverwriteCel
39605 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  l(pRtree, pParen
39606 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 69 43  t, &leftbbox, iC
39607 65 6c 6c 29 3b 0a 20 20 20 20 41 64 6a 75 73 74  ell);.    Adjust
39608 54 72 65 65 28 70 52 74 72 65 65 2c 20 70 50 61  Tree(pRtree, pPa
39609 72 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 29  rent, &leftbbox)
3960a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 72 63 20  ;.  }.  if( (rc 
3960b 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  = rtreeInsertCel
3960c 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74  l(pRtree, pRight
3960d 2d 3e 70 50 61 72 65 6e 74 2c 20 26 72 69 67 68  ->pParent, &righ
3960e 74 62 62 6f 78 2c 20 69 48 65 69 67 68 74 2b 31  tbbox, iHeight+1
3960f 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73  )) ){.    goto s
39610 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39611 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  }..  for(i=0; i<
39612 4e 43 45 4c 4c 28 70 52 69 67 68 74 29 3b 20 69  NCELL(pRight); i
39613 2b 2b 29 7b 0a 20 20 20 20 69 36 34 20 69 52 6f  ++){.    i64 iRo
39614 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77  wid = nodeGetRow
39615 69 64 28 70 52 74 72 65 65 2c 20 70 52 69 67 68  id(pRtree, pRigh
39616 74 2c 20 69 29 3b 0a 20 20 20 20 72 63 20 3d 20  t, i);.    rc = 
39617 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70 52  updateMapping(pR
39618 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 52  tree, iRowid, pR
39619 69 67 68 74 2c 20 69 48 65 69 67 68 74 29 3b 0a  ight, iHeight);.
3961a 20 20 20 20 69 66 28 20 69 52 6f 77 69 64 3d 3d      if( iRowid==
3961b 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 29 7b  pCell->iRowid ){
3961c 0a 20 20 20 20 20 20 6e 65 77 43 65 6c 6c 49 73  .      newCellIs
3961d 52 69 67 68 74 20 3d 20 31 3b 0a 20 20 20 20 7d  Right = 1;.    }
3961e 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
3961f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39620 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f  goto splitnode_o
39621 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ut;.    }.  }.  
39622 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
39623 3d 3d 31 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ==1 ){.    for(i
39624 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 4c 65 66  =0; i<NCELL(pLef
39625 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  t); i++){.      
39626 69 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64  i64 iRowid = nod
39627 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
39628 2c 20 70 4c 65 66 74 2c 20 69 29 3b 0a 20 20 20  , pLeft, i);.   
39629 20 20 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61     rc = updateMa
3962a 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20 69 52  pping(pRtree, iR
3962b 6f 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48 65  owid, pLeft, iHe
3962c 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28  ight);.      if(
3962d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3962e 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73  {.        goto s
3962f 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39630 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
39631 6c 73 65 20 69 66 28 20 6e 65 77 43 65 6c 6c 49  lse if( newCellI
39632 73 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  sRight==0 ){.   
39633 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61 70 70   rc = updateMapp
39634 69 6e 67 28 70 52 74 72 65 65 2c 20 70 43 65 6c  ing(pRtree, pCel
39635 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4c 65 66 74  l->iRowid, pLeft
39636 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a  , iHeight);.  }.
39637 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
39638 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
39639 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
3963a 72 65 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20  ree, pRight);.  
3963b 20 20 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20    pRight = 0;.  
3963c 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
3963d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
3963e 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52  = nodeRelease(pR
3963f 74 72 65 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20  tree, pLeft);.  
39640 20 20 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 7d    pLeft = 0;.  }
39641 0a 0a 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3a  ..splitnode_out:
39642 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70  .  nodeRelease(p
39643 52 74 72 65 65 2c 20 70 52 69 67 68 74 29 3b 0a  Rtree, pRight);.
39644 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
39645 74 72 65 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20  tree, pLeft);.  
39646 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65  sqlite3_free(aCe
39647 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ll);.  return rc
39648 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
39649 66 69 78 4c 65 61 66 50 61 72 65 6e 74 28 52 74  fixLeafParent(Rt
3964a 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3964b 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 29 7b 0a  eeNode *pLeaf){.
3964c 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3964d 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4c 65 61  E_OK;.  if( pLea
3964e 66 2d 3e 69 4e 6f 64 65 21 3d 31 20 26 26 20 70  f->iNode!=1 && p
3964f 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 3d 3d 30  Leaf->pParent==0
39650 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
39651 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
39652 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 2c 20  e->pReadParent, 
39653 31 2c 20 70 4c 65 61 66 2d 3e 69 4e 6f 64 65 29  1, pLeaf->iNode)
39654 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
39655 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70  3_step(pRtree->p
39656 52 65 61 64 50 61 72 65 6e 74 29 3d 3d 53 51 4c  ReadParent)==SQL
39657 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
39658 20 69 36 34 20 69 4e 6f 64 65 20 3d 20 73 71 6c   i64 iNode = sql
39659 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
3965a 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50  4(pRtree->pReadP
3965b 61 72 65 6e 74 2c 20 30 29 3b 0a 20 20 20 20 20  arent, 0);.     
3965c 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72   rc = nodeAcquir
3965d 65 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65 2c  e(pRtree, iNode,
3965e 20 30 2c 20 26 70 4c 65 61 66 2d 3e 70 50 61 72   0, &pLeaf->pPar
3965f 65 6e 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ent);.    }else{
39660 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
39661 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
39662 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65      sqlite3_rese
39663 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50  t(pRtree->pReadP
39664 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 20  arent);.    if( 
39665 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
39666 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 78 4c  .      rc = fixL
39667 65 61 66 50 61 72 65 6e 74 28 70 52 74 72 65 65  eafParent(pRtree
39668 2c 20 70 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74  , pLeaf->pParent
39669 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
3966a 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
3966b 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65 43 65  tic int deleteCe
3966c 6c 6c 28 52 74 72 65 65 20 2a 2c 20 52 74 72 65  ll(Rtree *, Rtre
3966d 65 4e 6f 64 65 20 2a 2c 20 69 6e 74 2c 20 69 6e  eNode *, int, in
3966e 74 29 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  t);..static int 
3966f 72 65 6d 6f 76 65 4e 6f 64 65 28 52 74 72 65 65  removeNode(Rtree
39670 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
39671 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20  ode *pNode, int 
39672 69 48 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20  iHeight){.  int 
39673 72 63 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  rc;.  RtreeNode 
39674 2a 70 50 61 72 65 6e 74 3b 0a 20 20 69 6e 74 20  *pParent;.  int 
39675 69 43 65 6c 6c 3b 0a 0a 20 20 61 73 73 65 72 74  iCell;..  assert
39676 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 31  ( pNode->nRef==1
39677 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65   );..  /* Remove
39678 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68   the entry in th
39679 65 20 70 61 72 65 6e 74 20 63 65 6c 6c 2e 20 2a  e parent cell. *
3967a 2f 0a 20 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  /.  iCell = node
3967b 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72  ParentIndex(pRtr
3967c 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 70 50  ee, pNode);.  pP
3967d 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70  arent = pNode->p
3967e 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d  Parent;.  pNode-
3967f 3e 70 50 61 72 65 6e 74 20 3d 20 30 3b 0a 20 20  >pParent = 0;.  
39680 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
39681 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c 6c 28  rc = deleteCell(
39682 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c  pRtree, pParent,
39683 20 69 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 2b   iCell, iHeight+
39684 31 29 29 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54  1)) .   || SQLIT
39685 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65  E_OK!=(rc = node
39686 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39687 70 50 61 72 65 6e 74 29 29 0a 20 20 29 7b 0a 20  pParent)).  ){. 
39688 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
39689 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  }..  /* Remove t
3968a 68 65 20 78 78 78 5f 6e 6f 64 65 20 65 6e 74 72  he xxx_node entr
3968b 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  y. */.  sqlite3_
3968c 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
3968d 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 2c 20  e->pDeleteNode, 
3968e 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29  1, pNode->iNode)
3968f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ;.  sqlite3_step
39690 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  (pRtree->pDelete
39691 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 53 51 4c  Node);.  if( SQL
39692 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
39693 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
39694 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29  ee->pDeleteNode)
39695 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
39696 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  rc;.  }..  /* Re
39697 6d 6f 76 65 20 74 68 65 20 78 78 78 5f 70 61 72  move the xxx_par
39698 65 6e 74 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20  ent entry. */.  
39699 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3969a 36 34 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  64(pRtree->pDele
3969b 74 65 50 61 72 65 6e 74 2c 20 31 2c 20 70 4e 6f  teParent, 1, pNo
3969c 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 73 71  de->iNode);.  sq
3969d 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65  lite3_step(pRtre
3969e 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74  e->pDeleteParent
3969f 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
396a0 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
396a1 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e  3_reset(pRtree->
396a2 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29 29 20  pDeleteParent)) 
396a3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
396a4 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 52 65  ;.  }.  .  /* Re
396a5 6d 6f 76 65 20 74 68 65 20 6e 6f 64 65 20 66 72  move the node fr
396a6 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  om the in-memory
396a7 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20   hash table and 
396a8 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 0a 20 20 2a  link it into.  *
396a9 2a 20 74 68 65 20 52 74 72 65 65 2e 70 44 65 6c  * the Rtree.pDel
396aa 65 74 65 64 20 6c 69 73 74 2e 20 49 74 73 20 63  eted list. Its c
396ab 6f 6e 74 65 6e 74 73 20 77 69 6c 6c 20 62 65 20  ontents will be 
396ac 72 65 2d 69 6e 73 65 72 74 65 64 20 6c 61 74 65  re-inserted late
396ad 72 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 64  r on..  */.  nod
396ae 65 48 61 73 68 44 65 6c 65 74 65 28 70 52 74 72  eHashDelete(pRtr
396af 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 70 4e  ee, pNode);.  pN
396b0 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 69 48 65  ode->iNode = iHe
396b1 69 67 68 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70  ight;.  pNode->p
396b2 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d 3e 70  Next = pRtree->p
396b3 44 65 6c 65 74 65 64 3b 0a 20 20 70 4e 6f 64 65  Deleted;.  pNode
396b4 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 70 52 74 72  ->nRef++;.  pRtr
396b5 65 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d 20 70  ee->pDeleted = p
396b6 4e 6f 64 65 3b 0a 0a 20 20 72 65 74 75 72 6e 20  Node;..  return 
396b7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74  SQLITE_OK;.}..st
396b8 61 74 69 63 20 76 6f 69 64 20 66 69 78 42 6f 75  atic void fixBou
396b9 6e 64 69 6e 67 42 6f 78 28 52 74 72 65 65 20 2a  ndingBox(Rtree *
396ba 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
396bb 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72  e *pNode){.  Rtr
396bc 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20  eeNode *pParent 
396bd 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  = pNode->pParent
396be 3b 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74 20  ;.  if( pParent 
396bf 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 0a  ){.    int ii; .
396c0 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20      int nCell = 
396c1 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20  NCELL(pNode);.  
396c2 20 20 52 74 72 65 65 43 65 6c 6c 20 62 6f 78 3b    RtreeCell box;
396c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
396c4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
396c5 6f 75 6e 64 69 6e 67 20 62 6f 78 20 66 6f 72 20  ounding box for 
396c6 70 4e 6f 64 65 20 2a 2f 0a 20 20 20 20 6e 6f 64  pNode */.    nod
396c7 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  eGetCell(pRtree,
396c8 20 70 4e 6f 64 65 2c 20 30 2c 20 26 62 6f 78 29   pNode, 0, &box)
396c9 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20  ;.    for(ii=1; 
396ca 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
396cb 0a 20 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c  .      RtreeCell
396cc 20 63 65 6c 6c 3b 0a 20 20 20 20 20 20 6e 6f 64   cell;.      nod
396cd 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  eGetCell(pRtree,
396ce 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c   pNode, ii, &cel
396cf 6c 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e  l);.      cellUn
396d0 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 62 6f 78  ion(pRtree, &box
396d1 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a  , &cell);.    }.
396d2 20 20 20 20 62 6f 78 2e 69 52 6f 77 69 64 20 3d      box.iRowid =
396d3 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3b 0a 20   pNode->iNode;. 
396d4 20 20 20 69 69 20 3d 20 6e 6f 64 65 50 61 72 65     ii = nodePare
396d5 6e 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20  ntIndex(pRtree, 
396d6 70 4e 6f 64 65 29 3b 0a 20 20 20 20 6e 6f 64 65  pNode);.    node
396d7 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52  OverwriteCell(pR
396d8 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26  tree, pParent, &
396d9 62 6f 78 2c 20 69 69 29 3b 0a 20 20 20 20 66 69  box, ii);.    fi
396da 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52 74  xBoundingBox(pRt
396db 72 65 65 2c 20 70 50 61 72 65 6e 74 29 3b 0a 20  ree, pParent);. 
396dc 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65   }.}../*.** Dele
396dd 74 65 20 74 68 65 20 63 65 6c 6c 20 61 74 20 69  te the cell at i
396de 6e 64 65 78 20 69 43 65 6c 6c 20 6f 66 20 6e 6f  ndex iCell of no
396df 64 65 20 70 4e 6f 64 65 2e 20 41 66 74 65 72 20  de pNode. After 
396e0 72 65 6d 6f 76 69 6e 67 20 74 68 65 0a 2a 2a 20  removing the.** 
396e1 63 65 6c 6c 2c 20 61 64 6a 75 73 74 20 74 68 65  cell, adjust the
396e2 20 72 2d 74 72 65 65 20 64 61 74 61 20 73 74 72   r-tree data str
396e3 75 63 74 75 72 65 20 69 66 20 72 65 71 75 69 72  ucture if requir
396e4 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
396e5 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52 74 72  t deleteCell(Rtr
396e6 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
396e7 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e  eNode *pNode, in
396e8 74 20 69 43 65 6c 6c 2c 20 69 6e 74 20 69 48 65  t iCell, int iHe
396e9 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b  ight){.  int rc;
396ea 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
396eb 4b 21 3d 28 72 63 20 3d 20 66 69 78 4c 65 61 66  K!=(rc = fixLeaf
396ec 50 61 72 65 6e 74 28 70 52 74 72 65 65 2c 20 70  Parent(pRtree, p
396ed 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 72 65  Node)) ){.    re
396ee 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
396ef 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 63 65  /* Remove the ce
396f0 6c 6c 20 66 72 6f 6d 20 74 68 65 20 6e 6f 64 65  ll from the node
396f1 2e 20 54 68 69 73 20 63 61 6c 6c 20 6a 75 73 74  . This call just
396f2 20 6d 6f 76 65 73 20 62 79 74 65 73 20 61 72 6f   moves bytes aro
396f3 75 6e 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e 2d  und.  ** the in-
396f4 6d 65 6d 6f 72 79 20 6e 6f 64 65 20 69 6d 61 67  memory node imag
396f5 65 2c 20 73 6f 20 69 74 20 63 61 6e 6e 6f 74 20  e, so it cannot 
396f6 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 64  fail..  */.  nod
396f7 65 44 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72  eDeleteCell(pRtr
396f8 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c  ee, pNode, iCell
396f9 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
396fa 6e 6f 64 65 20 69 73 20 6e 6f 74 20 74 68 65 20  node is not the 
396fb 74 72 65 65 20 72 6f 6f 74 20 61 6e 64 20 6e 6f  tree root and no
396fc 77 20 68 61 73 20 6c 65 73 73 20 74 68 61 6e 20  w has less than 
396fd 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 2a 2a  the minimum.  **
396fe 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   number of cells
396ff 2c 20 72 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d  , remove it from
39700 20 74 68 65 20 74 72 65 65 2e 20 4f 74 68 65 72   the tree. Other
39701 77 69 73 65 2c 20 75 70 64 61 74 65 20 74 68 65  wise, update the
39702 0a 20 20 2a 2a 20 63 65 6c 6c 20 69 6e 20 74 68  .  ** cell in th
39703 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20 73 6f  e parent node so
39704 20 74 68 61 74 20 69 74 20 74 69 67 68 74 6c 79   that it tightly
39705 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 75 70   contains the up
39706 64 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 64 65 2e  dated.  ** node.
39707 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4e 6f 64  .  */.  if( pNod
39708 65 2d 3e 69 4e 6f 64 65 21 3d 31 20 29 7b 0a 20  e->iNode!=1 ){. 
39709 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50     RtreeNode *pP
3970a 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70  arent = pNode->p
3970b 50 61 72 65 6e 74 3b 0a 20 20 20 20 69 66 28 20  Parent;.    if( 
3970c 28 70 50 61 72 65 6e 74 2d 3e 69 4e 6f 64 65 21  (pParent->iNode!
3970d 3d 31 20 7c 7c 20 4e 43 45 4c 4c 28 70 50 61 72  =1 || NCELL(pPar
3970e 65 6e 74 29 21 3d 31 29 20 0a 20 20 20 20 20 26  ent)!=1) .     &
3970f 26 20 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3c  & (NCELL(pNode)<
39710 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
39711 52 74 72 65 65 29 29 0a 20 20 20 20 29 7b 0a 20  Rtree)).    ){. 
39712 20 20 20 20 20 72 63 20 3d 20 72 65 6d 6f 76 65       rc = remove
39713 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Node(pRtree, pNo
39714 64 65 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  de, iHeight);.  
39715 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
39716 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52  ixBoundingBox(pR
39717 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20  tree, pNode);.  
39718 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
39719 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
3971a 69 6e 74 20 52 65 69 6e 73 65 72 74 28 0a 20 20  int Reinsert(.  
3971b 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
3971c 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
3971d 64 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  de, .  RtreeCell
3971e 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *pCell, .  int 
3971f 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74  iHeight.){.  int
39720 20 2a 61 4f 72 64 65 72 3b 0a 20 20 69 6e 74 20   *aOrder;.  int 
39721 2a 61 53 70 61 72 65 3b 0a 20 20 52 74 72 65 65  *aSpare;.  Rtree
39722 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20 66  Cell *aCell;.  f
39723 6c 6f 61 74 20 2a 61 44 69 73 74 61 6e 63 65 3b  loat *aDistance;
39724 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20  .  int nCell;.  
39725 66 6c 6f 61 74 20 61 43 65 6e 74 65 72 43 6f 6f  float aCenterCoo
39726 72 64 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d  rd[RTREE_MAX_DIM
39727 45 4e 53 49 4f 4e 53 5d 3b 0a 20 20 69 6e 74 20  ENSIONS];.  int 
39728 69 44 69 6d 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  iDim;.  int ii;.
39729 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3972a 45 5f 4f 4b 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  E_OK;..  memset(
3972b 61 43 65 6e 74 65 72 43 6f 6f 72 64 2c 20 30 2c  aCenterCoord, 0,
3972c 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74 29 2a 52   sizeof(float)*R
3972d 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49  TREE_MAX_DIMENSI
3972e 4f 4e 53 29 3b 0a 0a 20 20 6e 43 65 6c 6c 20 3d  ONS);..  nCell =
3972f 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2b 31 3b   NCELL(pNode)+1;
39730 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
39731 74 68 65 20 62 75 66 66 65 72 73 20 75 73 65 64  the buffers used
39732 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69   by this operati
39733 6f 6e 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69  on. The allocati
39734 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 6c 69 6e  on is.  ** relin
39735 71 75 69 73 68 65 64 20 62 65 66 6f 72 65 20 74  quished before t
39736 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
39737 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 43 65  urns..  */.  aCe
39738 6c 6c 20 3d 20 28 52 74 72 65 65 43 65 6c 6c 20  ll = (RtreeCell 
39739 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
3973a 28 6e 43 65 6c 6c 20 2a 20 28 0a 20 20 20 20 73  (nCell * (.    s
3973b 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
3973c 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61 43   +         /* aC
3973d 65 6c 6c 20 61 72 72 61 79 20 2a 2f 0a 20 20 20  ell array */.   
3973e 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 20 20 20   sizeof(int)    
3973f 20 20 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20     +         /* 
39740 61 4f 72 64 65 72 20 61 72 72 61 79 20 2a 2f 0a  aOrder array */.
39741 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 20      sizeof(int) 
39742 20 20 20 20 20 20 2b 20 20 20 20 20 20 20 20 20        +         
39743 2f 2a 20 61 53 70 61 72 65 20 61 72 72 61 79 20  /* aSpare array 
39744 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 66 6c  */.    sizeof(fl
39745 6f 61 74 29 20 20 20 20 20 20 20 20 20 20 20 20  oat)            
39746 20 20 20 2f 2a 20 61 44 69 73 74 61 6e 63 65 20     /* aDistance 
39747 61 72 72 61 79 20 2a 2f 0a 20 20 29 29 3b 0a 20  array */.  ));. 
39748 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20   if( !aCell ){. 
39749 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3974a 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 4f  _NOMEM;.  }.  aO
3974b 72 64 65 72 20 20 20 20 3d 20 28 69 6e 74 20 2a  rder    = (int *
3974c 29 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3b 0a  )&aCell[nCell];.
3974d 20 20 61 53 70 61 72 65 20 20 20 20 3d 20 28 69    aSpare    = (i
3974e 6e 74 20 2a 29 26 61 4f 72 64 65 72 5b 6e 43 65  nt *)&aOrder[nCe
3974f 6c 6c 5d 3b 0a 20 20 61 44 69 73 74 61 6e 63 65  ll];.  aDistance
39750 20 3d 20 28 66 6c 6f 61 74 20 2a 29 26 61 53 70   = (float *)&aSp
39751 61 72 65 5b 6e 43 65 6c 6c 5d 3b 0a 0a 20 20 66  are[nCell];..  f
39752 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
39753 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66  l; ii++){.    if
39754 28 20 69 69 3d 3d 28 6e 43 65 6c 6c 2d 31 29 20  ( ii==(nCell-1) 
39755 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
39756 26 61 43 65 6c 6c 5b 69 69 5d 2c 20 70 43 65 6c  &aCell[ii], pCel
39757 6c 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43  l, sizeof(RtreeC
39758 65 6c 6c 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ell));.    }else
39759 7b 0a 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43  {.      nodeGetC
3975a 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ell(pRtree, pNod
3975b 65 2c 20 69 69 2c 20 26 61 43 65 6c 6c 5b 69 69  e, ii, &aCell[ii
3975c 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 4f  ]);.    }.    aO
3975d 72 64 65 72 5b 69 69 5d 20 3d 20 69 69 3b 0a 20  rder[ii] = ii;. 
3975e 20 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69     for(iDim=0; i
3975f 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d  Dim<pRtree->nDim
39760 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 20  ; iDim++){.     
39761 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44   aCenterCoord[iD
39762 69 6d 5d 20 2b 3d 20 44 43 4f 4f 52 44 28 61 43  im] += DCOORD(aC
39763 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69  ell[ii].aCoord[i
39764 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20 20 61  Dim*2]);.      a
39765 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d  CenterCoord[iDim
39766 5d 20 2b 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c  ] += DCOORD(aCel
39767 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  l[ii].aCoord[iDi
39768 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  m*2+1]);.    }. 
39769 20 7d 0a 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b   }.  for(iDim=0;
3976a 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44   iDim<pRtree->nD
3976b 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20 20  im; iDim++){.   
3976c 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44   aCenterCoord[iD
3976d 69 6d 5d 20 3d 20 61 43 65 6e 74 65 72 43 6f 6f  im] = aCenterCoo
3976e 72 64 5b 69 44 69 6d 5d 2f 28 28 66 6c 6f 61 74  rd[iDim]/((float
3976f 29 6e 43 65 6c 6c 2a 32 2e 30 29 3b 0a 20 20 7d  )nCell*2.0);.  }
39770 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
39771 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
39772 20 20 20 61 44 69 73 74 61 6e 63 65 5b 69 69 5d     aDistance[ii]
39773 20 3d 20 30 2e 30 3b 0a 20 20 20 20 66 6f 72 28   = 0.0;.    for(
39774 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74  iDim=0; iDim<pRt
39775 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b  ree->nDim; iDim+
39776 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
39777 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28 61  coord = DCOORD(a
39778 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b  Cell[ii].aCoord[
39779 69 44 69 6d 2a 32 2b 31 5d 29 20 2d 20 0a 20 20  iDim*2+1]) - .  
3977a 20 20 20 20 20 20 20 20 44 43 4f 4f 52 44 28 61          DCOORD(a
3977b 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b  Cell[ii].aCoord[
3977c 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20 20  iDim*2]);.      
3977d 61 44 69 73 74 61 6e 63 65 5b 69 69 5d 20 2b 3d  aDistance[ii] +=
3977e 20 28 63 6f 6f 72 64 2d 61 43 65 6e 74 65 72 43   (coord-aCenterC
3977f 6f 6f 72 64 5b 69 44 69 6d 5d 29 2a 28 63 6f 6f  oord[iDim])*(coo
39780 72 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b  rd-aCenterCoord[
39781 69 44 69 6d 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  iDim]);.    }.  
39782 7d 0a 0a 20 20 53 6f 72 74 42 79 44 69 73 74 61  }..  SortByDista
39783 6e 63 65 28 61 4f 72 64 65 72 2c 20 6e 43 65 6c  nce(aOrder, nCel
39784 6c 2c 20 61 44 69 73 74 61 6e 63 65 2c 20 61 53  l, aDistance, aS
39785 70 61 72 65 29 3b 0a 20 20 6e 6f 64 65 5a 65 72  pare);.  nodeZer
39786 6f 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  o(pRtree, pNode)
39787 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 72  ;..  for(ii=0; r
39788 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
39789 69 69 3c 28 6e 43 65 6c 6c 2d 28 52 54 52 45 45  ii<(nCell-(RTREE
3978a 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
3978b 29 2b 31 29 29 3b 20 69 69 2b 2b 29 7b 0a 20 20  )+1)); ii++){.  
3978c 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 20 3d    RtreeCell *p =
3978d 20 26 61 43 65 6c 6c 5b 61 4f 72 64 65 72 5b 69   &aCell[aOrder[i
3978e 69 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49 6e 73  i]];.    nodeIns
3978f 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  ertCell(pRtree, 
39790 70 4e 6f 64 65 2c 20 70 29 3b 0a 20 20 20 20 69  pNode, p);.    i
39791 66 28 20 70 2d 3e 69 52 6f 77 69 64 3d 3d 70 43  f( p->iRowid==pC
39792 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 29 7b 0a 20  ell->iRowid ){. 
39793 20 20 20 20 20 69 66 28 20 69 48 65 69 67 68 74       if( iHeight
39794 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
39795 63 20 3d 20 72 6f 77 69 64 57 72 69 74 65 28 70  c = rowidWrite(p
39796 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77 69 64  Rtree, p->iRowid
39797 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  , pNode->iNode);
39798 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
39799 20 20 20 20 20 20 72 63 20 3d 20 70 61 72 65 6e        rc = paren
3979a 74 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70  tWrite(pRtree, p
3979b 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d  ->iRowid, pNode-
3979c 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d  >iNode);.      }
3979d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
3979e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3979f 7b 0a 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e  {.    fixBoundin
397a0 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  gBox(pRtree, pNo
397a1 64 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 3b  de);.  }.  for(;
397a2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
397a3 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  & ii<nCell; ii++
397a4 29 7b 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  ){.    /* Find a
397a5 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74   node to store t
397a6 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f  his cell in. pNo
397a7 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72 72 65 6e  de->iNode curren
397a8 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20  tly contains.   
397a9 20 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 6f   ** the height o
397aa 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68  f the sub-tree h
397ab 65 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c  eaded by the cel
397ac 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 52 74  l..    */.    Rt
397ad 72 65 65 4e 6f 64 65 20 2a 70 49 6e 73 65 72 74  reeNode *pInsert
397ae 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  ;.    RtreeCell 
397af 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61 4f 72 64  *p = &aCell[aOrd
397b0 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20 72 63 20  er[ii]];.    rc 
397b1 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74  = ChooseLeaf(pRt
397b2 72 65 65 2c 20 70 2c 20 69 48 65 69 67 68 74 2c  ree, p, iHeight,
397b3 20 26 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20   &pInsert);.    
397b4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
397b5 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  K ){.      int r
397b6 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72  c2;.      rc = r
397b7 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  treeInsertCell(p
397b8 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c 20  Rtree, pInsert, 
397b9 70 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20  p, iHeight);.   
397ba 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c     rc2 = nodeRel
397bb 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 49 6e  ease(pRtree, pIn
397bc 73 65 72 74 29 3b 0a 20 20 20 20 20 20 69 66 28  sert);.      if(
397bd 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
397be 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
397bf 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c2;.      }.    
397c0 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  }.  }..  sqlite3
397c1 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20  _free(aCell);.  
397c2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
397c3 0a 2a 2a 20 49 6e 73 65 72 74 20 63 65 6c 6c 20  .** Insert cell 
397c4 70 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20  pCell into node 
397c5 70 4e 6f 64 65 2e 20 4e 6f 64 65 20 70 4e 6f 64  pNode. Node pNod
397c6 65 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66  e is the head of
397c7 20 61 20 0a 2a 2a 20 73 75 62 74 72 65 65 20 69   a .** subtree i
397c8 48 65 69 67 68 74 20 68 69 67 68 20 28 6c 65 61  Height high (lea
397c9 66 20 6e 6f 64 65 73 20 68 61 76 65 20 69 48 65  f nodes have iHe
397ca 69 67 68 74 3d 3d 30 29 2e 0a 2a 2f 0a 73 74 61  ight==0)..*/.sta
397cb 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 73  tic int rtreeIns
397cc 65 72 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65  ertCell(.  Rtree
397cd 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65   *pRtree,.  Rtre
397ce 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20 20  eNode *pNode,.  
397cf 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c  RtreeCell *pCell
397d0 2c 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a  ,.  int iHeight.
397d1 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
397d2 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69  LITE_OK;.  if( i
397d3 48 65 69 67 68 74 3e 30 20 29 7b 0a 20 20 20 20  Height>0 ){.    
397d4 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c  RtreeNode *pChil
397d5 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  d = nodeHashLook
397d6 75 70 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c  up(pRtree, pCell
397d7 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 69  ->iRowid);.    i
397d8 66 28 20 70 43 68 69 6c 64 20 29 7b 0a 20 20 20  f( pChild ){.   
397d9 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
397da 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2d 3e 70  Rtree, pChild->p
397db 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 6e  Parent);.      n
397dc 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 4e 6f  odeReference(pNo
397dd 64 65 29 3b 0a 20 20 20 20 20 20 70 43 68 69 6c  de);.      pChil
397de 64 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  d->pParent = pNo
397df 64 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  de;.    }.  }.  
397e0 69 66 28 20 6e 6f 64 65 49 6e 73 65 72 74 43 65  if( nodeInsertCe
397e1 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
397e2 2c 20 70 43 65 6c 6c 29 20 29 7b 0a 23 69 66 20  , pCell) ){.#if 
397e3 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
397e4 45 5f 52 45 49 4e 53 45 52 54 0a 20 20 20 20 69  E_REINSERT.    i
397e5 66 28 20 69 48 65 69 67 68 74 3c 3d 70 52 74 72  f( iHeight<=pRtr
397e6 65 65 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69  ee->iReinsertHei
397e7 67 68 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 69 4e  ght || pNode->iN
397e8 6f 64 65 3d 3d 31 29 7b 0a 20 20 20 20 20 20 72  ode==1){.      r
397e9 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70 52  c = SplitNode(pR
397ea 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65  tree, pNode, pCe
397eb 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  ll, iHeight);.  
397ec 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
397ed 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65 72 74  Rtree->iReinsert
397ee 48 65 69 67 68 74 20 3d 20 69 48 65 69 67 68 74  Height = iHeight
397ef 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 52 65 69  ;.      rc = Rei
397f0 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
397f1 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69  ode, pCell, iHei
397f2 67 68 74 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73  ght);.    }.#els
397f3 65 0a 20 20 20 20 72 63 20 3d 20 53 70 6c 69 74  e.    rc = Split
397f4 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Node(pRtree, pNo
397f5 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69 67  de, pCell, iHeig
397f6 68 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65  ht);.#endif.  }e
397f7 6c 73 65 7b 0a 20 20 20 20 41 64 6a 75 73 74 54  lse{.    AdjustT
397f8 72 65 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ree(pRtree, pNod
397f9 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69  e, pCell);.    i
397fa 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b  f( iHeight==0 ){
397fb 0a 20 20 20 20 20 20 72 63 20 3d 20 72 6f 77 69  .      rc = rowi
397fc 64 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70  dWrite(pRtree, p
397fd 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e  Cell->iRowid, pN
397fe 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20  ode->iNode);.   
397ff 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
39800 20 3d 20 70 61 72 65 6e 74 57 72 69 74 65 28 70   = parentWrite(p
39801 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52  Rtree, pCell->iR
39802 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f  owid, pNode->iNo
39803 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  de);.    }.  }. 
39804 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
39805 74 61 74 69 63 20 69 6e 74 20 72 65 69 6e 73 65  tatic int reinse
39806 72 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 52 74  rtNodeContent(Rt
39807 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
39808 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
39809 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
3980a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
3980b 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
3980c 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 66  ELL(pNode);..  f
3980d 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  or(ii=0; rc==SQL
3980e 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65  ITE_OK && ii<nCe
3980f 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52  ll; ii++){.    R
39810 74 72 65 65 4e 6f 64 65 20 2a 70 49 6e 73 65 72  treeNode *pInser
39811 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  t;.    RtreeCell
39812 20 63 65 6c 6c 3b 0a 20 20 20 20 6e 6f 64 65 47   cell;.    nodeG
39813 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
39814 4e 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29  Node, ii, &cell)
39815 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  ;..    /* Find a
39816 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74   node to store t
39817 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f  his cell in. pNo
39818 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72 72 65 6e  de->iNode curren
39819 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20  tly contains.   
3981a 20 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 6f   ** the height o
3981b 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68  f the sub-tree h
3981c 65 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c  eaded by the cel
3981d 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  l..    */.    rc
3981e 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28 70 52   = ChooseLeaf(pR
3981f 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 4e 6f  tree, &cell, pNo
39820 64 65 2d 3e 69 4e 6f 64 65 2c 20 26 70 49 6e 73  de->iNode, &pIns
39821 65 72 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ert);.    if( rc
39822 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39823 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
39824 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e      rc = rtreeIn
39825 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
39826 20 70 49 6e 73 65 72 74 2c 20 26 63 65 6c 6c 2c   pInsert, &cell,
39827 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a   pNode->iNode);.
39828 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65        rc2 = node
39829 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
3982a 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20 20 20  pInsert);.      
3982b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3982c 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
3982d 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20  = rc2;.      }. 
3982e 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
3982f 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
39830 65 6c 65 63 74 20 61 20 63 75 72 72 65 6e 74 6c  elect a currentl
39831 79 20 75 6e 75 73 65 64 20 72 6f 77 69 64 20 66  y unused rowid f
39832 6f 72 20 61 20 6e 65 77 20 72 2d 74 72 65 65 20  or a new r-tree 
39833 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 69  record..*/.stati
39834 63 20 69 6e 74 20 6e 65 77 52 6f 77 69 64 28 52  c int newRowid(R
39835 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36  tree *pRtree, i6
39836 34 20 2a 70 69 52 6f 77 69 64 29 7b 0a 20 20 69  4 *piRowid){.  i
39837 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
39838 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 52 74 72 65  _bind_null(pRtre
39839 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20  e->pWriteRowid, 
3983a 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  1);.  sqlite3_bi
3983b 6e 64 5f 6e 75 6c 6c 28 70 52 74 72 65 65 2d 3e  nd_null(pRtree->
3983c 70 57 72 69 74 65 52 6f 77 69 64 2c 20 32 29 3b  pWriteRowid, 2);
3983d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
3983e 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
3983f 77 69 64 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  wid);.  rc = sql
39840 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
39841 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b  e->pWriteRowid);
39842 0a 20 20 2a 70 69 52 6f 77 69 64 20 3d 20 73 71  .  *piRowid = sq
39843 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
39844 74 5f 72 6f 77 69 64 28 70 52 74 72 65 65 2d 3e  t_rowid(pRtree->
39845 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  db);.  return rc
39846 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  ;.}..#ifndef NDE
39847 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20 68  BUG.static int h
39848 61 73 68 49 73 45 6d 70 74 79 28 52 74 72 65 65  ashIsEmpty(Rtree
39849 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 69 6e 74   *pRtree){.  int
3984a 20 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b   ii;.  for(ii=0;
3984b 20 69 69 3c 48 41 53 48 53 49 5a 45 3b 20 69 69   ii<HASHSIZE; ii
3984c 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
3984d 20 21 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b   !pRtree->aHash[
3984e 69 69 5d 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ii] );.  }.  ret
3984f 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 1;.}.#endif.
39850 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 61  ./*.** The xUpda
39851 74 65 20 6d 65 74 68 6f 64 20 66 6f 72 20 72 74  te method for rt
39852 72 65 65 20 6d 6f 64 75 6c 65 20 76 69 72 74 75  ree module virtu
39853 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 73 74  al tables..*/.st
39854 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 55 70  atic int rtreeUp
39855 64 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  date(.  sqlite3_
39856 76 74 61 62 20 2a 70 56 74 61 62 2c 20 0a 20 20  vtab *pVtab, .  
39857 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 73 71  int nData, .  sq
39858 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 7a  lite3_value **az
39859 44 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 5f  Data, .  sqlite_
3985a 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 0a 29 7b  int64 *pRowid.){
3985b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
3985c 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74 61   = (Rtree *)pVta
3985d 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  b;.  int rc = SQ
3985e 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65  LITE_OK;..  rtre
3985f 65 52 65 66 65 72 65 6e 63 65 28 70 52 74 72 65  eReference(pRtre
39860 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 44  e);..  assert(nD
39861 61 74 61 3e 3d 31 29 3b 0a 20 20 61 73 73 65 72  ata>=1);.  asser
39862 74 28 68 61 73 68 49 73 45 6d 70 74 79 28 70 52  t(hashIsEmpty(pR
39863 74 72 65 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  tree));..  /* If
39864 20 61 7a 44 61 74 61 5b 30 5d 20 69 73 20 6e 6f   azData[0] is no
39865 74 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61  t an SQL NULL va
39866 6c 75 65 2c 20 69 74 20 69 73 20 74 68 65 20 72  lue, it is the r
39867 6f 77 69 64 20 6f 66 20 61 0a 20 20 2a 2a 20 72  owid of a.  ** r
39868 65 63 6f 72 64 20 74 6f 20 64 65 6c 65 74 65 20  ecord to delete 
39869 66 72 6f 6d 20 74 68 65 20 72 2d 74 72 65 65 20  from the r-tree 
3986a 74 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f  table. The follo
3986b 77 69 6e 67 20 62 6c 6f 63 6b 20 64 6f 65 73 0a  wing block does.
3986c 20 20 2a 2a 20 6a 75 73 74 20 74 68 61 74 2e 0a    ** just that..
3986d 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
3986e 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a  e3_value_type(az
3986f 44 61 74 61 5b 30 5d 29 21 3d 53 51 4c 49 54 45  Data[0])!=SQLITE
39870 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 36 34  _NULL ){.    i64
39871 20 69 44 65 6c 65 74 65 3b 20 20 20 20 20 20 20   iDelete;       
39872 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
39873 72 6f 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20  rowid to delete 
39874 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  */.    RtreeNode
39875 20 2a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20   *pLeaf;        
39876 20 20 20 2f 2a 20 4c 65 61 66 20 6e 6f 64 65 20     /* Leaf node 
39877 63 6f 6e 74 61 69 6e 69 6e 67 20 72 65 63 6f 72  containing recor
39878 64 20 69 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20  d iDelete */.   
39879 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20 20   int iCell;     
3987a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3987b 49 6e 64 65 78 20 6f 66 20 69 44 65 6c 65 74 65  Index of iDelete
3987c 20 63 65 6c 6c 20 69 6e 20 70 4c 65 61 66 20 2a   cell in pLeaf *
3987d 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  /.    RtreeNode 
3987e 2a 70 52 6f 6f 74 3b 0a 0a 20 20 20 20 2f 2a 20  *pRoot;..    /* 
3987f 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  Obtain a referen
39880 63 65 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e  ce to the root n
39881 6f 64 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 73  ode to initialis
39882 65 20 52 74 72 65 65 2e 69 44 65 70 74 68 20 2a  e Rtree.iDepth *
39883 2f 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41  /.    rc = nodeA
39884 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 31  cquire(pRtree, 1
39885 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b 0a 0a 20  , 0, &pRoot);.. 
39886 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72     /* Obtain a r
39887 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
39888 6c 65 61 66 20 6e 6f 64 65 20 74 68 61 74 20 63  leaf node that c
39889 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72  ontains the entr
3988a 79 20 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20  y .    ** about 
3988b 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 0a  to be deleted. .
3988c 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
3988d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3988e 20 20 20 20 20 20 69 44 65 6c 65 74 65 20 3d 20        iDelete = 
3988f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
39890 74 36 34 28 61 7a 44 61 74 61 5b 30 5d 29 3b 0a  t64(azData[0]);.
39891 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c        rc = findL
39892 65 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  eafNode(pRtree, 
39893 69 44 65 6c 65 74 65 2c 20 26 70 4c 65 61 66 29  iDelete, &pLeaf)
39894 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
39895 44 65 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20  Delete the cell 
39896 69 6e 20 71 75 65 73 74 69 6f 6e 20 66 72 6f 6d  in question from
39897 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20   the leaf node. 
39898 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
39899 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3989a 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
3989b 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77   iCell = nodeRow
3989c 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20  idIndex(pRtree, 
3989d 70 4c 65 61 66 2c 20 69 44 65 6c 65 74 65 29 3b  pLeaf, iDelete);
3989e 0a 20 20 20 20 20 20 72 63 20 3d 20 64 65 6c 65  .      rc = dele
3989f 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
398a0 4c 65 61 66 2c 20 69 43 65 6c 6c 2c 20 30 29 3b  Leaf, iCell, 0);
398a1 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64  .      rc2 = nod
398a2 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
398a3 20 70 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 69   pLeaf);.      i
398a4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
398a5 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
398a6 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20   rc2;.      }.  
398a7 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65    }..    /* Dele
398a8 74 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  te the correspon
398a9 64 69 6e 67 20 65 6e 74 72 79 20 69 6e 20 74 68  ding entry in th
398aa 65 20 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20  e <rtree>_rowid 
398ab 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66  table. */.    if
398ac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
398ad 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
398ae 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
398af 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
398b0 2c 20 31 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20  , 1, iDelete);. 
398b1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65       sqlite3_ste
398b2 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  p(pRtree->pDelet
398b3 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 72  eRowid);.      r
398b4 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
398b5 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  t(pRtree->pDelet
398b6 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a  eRowid);.    }..
398b7 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
398b8 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6e 6f  the root node no
398b9 77 20 68 61 73 20 65 78 61 63 74 6c 79 20 6f 6e  w has exactly on
398ba 65 20 63 68 69 6c 64 2e 20 49 66 20 73 6f 2c 20  e child. If so, 
398bb 72 65 6d 6f 76 65 0a 20 20 20 20 2a 2a 20 69 74  remove.    ** it
398bc 2c 20 73 63 68 65 64 75 6c 65 20 74 68 65 20 63  , schedule the c
398bd 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
398be 68 69 6c 64 20 66 6f 72 20 72 65 69 6e 73 65 72  hild for reinser
398bf 74 69 6f 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a  tion and .    **
398c0 20 72 65 64 75 63 65 20 74 68 65 20 74 72 65 65   reduce the tree
398c1 20 68 65 69 67 68 74 20 62 79 20 6f 6e 65 2e 0a   height by one..
398c2 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
398c3 69 73 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  is is equivalent
398c4 20 74 6f 20 63 6f 70 79 69 6e 67 20 74 68 65 20   to copying the 
398c5 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
398c6 63 68 69 6c 64 20 69 6e 74 6f 0a 20 20 20 20 2a  child into.    *
398c7 2a 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20  * the root node 
398c8 28 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74  (the operation t
398c9 68 61 74 20 47 75 74 6d 61 6e 27 73 20 70 61 70  hat Gutman's pap
398ca 65 72 20 73 61 79 73 20 74 6f 20 70 65 72 66 6f  er says to perfo
398cb 72 6d 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68  rm .    ** in th
398cc 69 73 20 73 63 65 6e 61 72 69 6f 29 2e 0a 20 20  is scenario)..  
398cd 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
398ce 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 52  =SQLITE_OK && pR
398cf 74 72 65 65 2d 3e 69 44 65 70 74 68 3e 30 20 29  tree->iDepth>0 )
398d0 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  {.      if( rc==
398d1 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 4e 43 45  SQLITE_OK && NCE
398d2 4c 4c 28 70 52 6f 6f 74 29 3d 3d 31 20 29 7b 0a  LL(pRoot)==1 ){.
398d3 20 20 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64          RtreeNod
398d4 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 20 20 20  e *pChild;.     
398d5 20 20 20 69 36 34 20 69 43 68 69 6c 64 20 3d 20     i64 iChild = 
398d6 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
398d7 72 65 65 2c 20 70 52 6f 6f 74 2c 20 30 29 3b 0a  ree, pRoot, 0);.
398d8 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64          rc = nod
398d9 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
398da 20 69 43 68 69 6c 64 2c 20 70 52 6f 6f 74 2c 20   iChild, pRoot, 
398db 26 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20  &pChild);.      
398dc 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
398dd 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
398de 20 72 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65   rc = removeNode
398df 28 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2c  (pRtree, pChild,
398e0 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d   pRtree->iDepth-
398e1 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
398e2 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
398e3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
398e4 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65       pRtree->iDe
398e5 70 74 68 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20  pth--;.         
398e6 20 77 72 69 74 65 49 6e 74 31 36 28 70 52 6f 6f   writeInt16(pRoo
398e7 74 2d 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65  t->zData, pRtree
398e8 2d 3e 69 44 65 70 74 68 29 3b 0a 20 20 20 20 20  ->iDepth);.     
398e9 20 20 20 20 20 70 52 6f 6f 74 2d 3e 69 73 44 69       pRoot->isDi
398ea 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  rty = 1;.       
398eb 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
398ec 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 69 6e 73 65  ..    /* Re-inse
398ed 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rt the contents 
398ee 6f 66 20 61 6e 79 20 75 6e 64 65 72 66 75 6c 6c  of any underfull
398ef 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 66   nodes removed f
398f0 72 6f 6d 20 74 68 65 20 74 72 65 65 2e 20 2a 2f  rom the tree. */
398f1 0a 20 20 20 20 66 6f 72 28 70 4c 65 61 66 3d 70  .    for(pLeaf=p
398f2 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b  Rtree->pDeleted;
398f3 20 70 4c 65 61 66 3b 20 70 4c 65 61 66 3d 70 52   pLeaf; pLeaf=pR
398f4 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 29 7b  tree->pDeleted){
398f5 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
398f6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
398f7 20 20 20 20 72 63 20 3d 20 72 65 69 6e 73 65 72      rc = reinser
398f8 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 74  tNodeContent(pRt
398f9 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20  ree, pLeaf);.   
398fa 20 20 20 7d 0a 20 20 20 20 20 20 70 52 74 72 65     }.      pRtre
398fb 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d 20 70 4c  e->pDeleted = pL
398fc 65 61 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  eaf->pNext;.    
398fd 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
398fe 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Leaf);.    }..  
398ff 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65    /* Release the
39900 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
39901 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20 2a 2f 0a  e root node. */.
39902 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
39903 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
39904 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
39905 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a  pRtree, pRoot);.
39906 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
39907 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
39908 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20  ree, pRoot);.   
39909 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
3990a 74 68 65 20 61 7a 44 61 74 61 5b 5d 20 61 72 72  the azData[] arr
3990b 61 79 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65  ay contains more
3990c 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e   than one elemen
3990d 74 2c 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a  t, elements.  **
3990e 20 28 61 7a 44 61 74 61 5b 32 5d 2e 2e 61 7a 44   (azData[2]..azD
3990f 61 74 61 5b 61 72 67 63 2d 31 5d 29 20 63 6f 6e  ata[argc-1]) con
39910 74 61 69 6e 20 61 20 6e 65 77 20 72 65 63 6f 72  tain a new recor
39911 64 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f  d to insert into
39912 0a 20 20 2a 2a 20 74 68 65 20 72 2d 74 72 65 65  .  ** the r-tree
39913 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f   structure..  */
39914 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
39915 45 5f 4f 4b 20 26 26 20 6e 44 61 74 61 3e 31 20  E_OK && nData>1 
39916 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  ){.    /* Insert
39917 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e   a new record in
39918 74 6f 20 74 68 65 20 72 2d 74 72 65 65 20 2a 2f  to the r-tree */
39919 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63  .    RtreeCell c
3991a 65 6c 6c 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b  ell;.    int ii;
3991b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
3991c 70 4c 65 61 66 3b 0a 0a 20 20 20 20 2f 2a 20 50  pLeaf;..    /* P
3991d 6f 70 75 6c 61 74 65 20 74 68 65 20 63 65 6c 6c  opulate the cell
3991e 2e 61 43 6f 6f 72 64 5b 5d 20 61 72 72 61 79 2e  .aCoord[] array.
3991f 20 54 68 65 20 66 69 72 73 74 20 63 6f 6f 72 64   The first coord
39920 69 6e 61 74 65 20 69 73 20 61 7a 44 61 74 61 5b  inate is azData[
39921 33 5d 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  3]. */.    asser
39922 74 28 20 6e 44 61 74 61 3d 3d 28 70 52 74 72 65  t( nData==(pRtre
39923 65 2d 3e 6e 44 69 6d 2a 32 20 2b 20 33 29 20 29  e->nDim*2 + 3) )
39924 3b 0a 20 20 20 20 69 66 28 20 70 52 74 72 65 65  ;.    if( pRtree
39925 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
39926 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
39927 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 69   ){.      for(ii
39928 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e  =0; ii<(pRtree->
39929 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b  nDim*2); ii+=2){
3992a 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43  .        cell.aC
3992b 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 28 66 6c  oord[ii].f = (fl
3992c 6f 61 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  oat)sqlite3_valu
3992d 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b  e_double(azData[
3992e 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20  ii+3]);.        
3992f 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31  cell.aCoord[ii+1
39930 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73 71 6c  ].f = (float)sql
39931 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
39932 65 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b  e(azData[ii+4]);
39933 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 65 6c  .        if( cel
39934 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 3e 63  l.aCoord[ii].f>c
39935 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  ell.aCoord[ii+1]
39936 2e 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  .f ){.          
39937 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53  rc = SQLITE_CONS
39938 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20  TRAINT;.        
39939 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e    goto constrain
3993a 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
3993b 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
3993c 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20        for(ii=0; 
3993d 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
3993e 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
3993f 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64       cell.aCoord
39940 5b 69 69 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33  [ii].i = sqlite3
39941 5f 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74  _value_int(azDat
39942 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20  a[ii+3]);.      
39943 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69    cell.aCoord[ii
39944 2b 31 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f  +1].i = sqlite3_
39945 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61  value_int(azData
39946 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20  [ii+4]);.       
39947 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64   if( cell.aCoord
39948 5b 69 69 5d 2e 69 3e 63 65 6c 6c 2e 61 43 6f 6f  [ii].i>cell.aCoo
39949 72 64 5b 69 69 2b 31 5d 2e 69 20 29 7b 0a 20 20  rd[ii+1].i ){.  
3994a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
3994b 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
3994c 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63            goto c
3994d 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20  onstraint;.     
3994e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
3994f 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72   }..    /* Figur
39950 65 20 6f 75 74 20 74 68 65 20 72 6f 77 69 64 20  e out the rowid 
39951 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 20  of the new row. 
39952 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
39953 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a  e3_value_type(az
39954 44 61 74 61 5b 32 5d 29 3d 3d 53 51 4c 49 54 45  Data[2])==SQLITE
39955 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72  _NULL ){.      r
39956 63 20 3d 20 6e 65 77 52 6f 77 69 64 28 70 52 74  c = newRowid(pRt
39957 72 65 65 2c 20 26 63 65 6c 6c 2e 69 52 6f 77 69  ree, &cell.iRowi
39958 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
39959 20 20 20 20 20 63 65 6c 6c 2e 69 52 6f 77 69 64       cell.iRowid
3995a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3995b 5f 69 6e 74 36 34 28 61 7a 44 61 74 61 5b 32 5d  _int64(azData[2]
3995c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3995d 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
3995e 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20  ee->pReadRowid, 
3995f 31 2c 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b  1, cell.iRowid);
39960 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
39961 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
39962 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61  tep(pRtree->pRea
39963 64 52 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 20  dRowid) ){.     
39964 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
39965 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
39966 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 72 63  wid);.        rc
39967 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52   = SQLITE_CONSTR
39968 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 67 6f  AINT;.        go
39969 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20  to constraint;. 
3996a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
3996b 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
3996c 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
3996d 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  id);.    }..    
3996e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3996f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
39970 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65  ChooseLeaf(pRtre
39971 65 2c 20 26 63 65 6c 6c 2c 20 30 2c 20 26 70 4c  e, &cell, 0, &pL
39972 65 61 66 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  eaf);.    }.    
39973 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
39974 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  K ){.      int r
39975 63 32 3b 0a 20 20 20 20 20 20 70 52 74 72 65 65  c2;.      pRtree
39976 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67 68  ->iReinsertHeigh
39977 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 63  t = -1;.      rc
39978 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65   = rtreeInsertCe
39979 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  ll(pRtree, pLeaf
3997a 2c 20 26 63 65 6c 6c 2c 20 30 29 3b 0a 20 20 20  , &cell, 0);.   
3997b 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c     rc2 = nodeRel
3997c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
3997d 61 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  af);.      if( r
3997e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3997f 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32          rc = rc2
39980 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
39981 20 20 7d 0a 0a 63 6f 6e 73 74 72 61 69 6e 74 3a    }..constraint:
39982 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  .  rtreeRelease(
39983 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74 75 72  pRtree);.  retur
39984 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
39985 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f  he xRename metho
39986 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75  d for rtree modu
39987 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  le virtual table
39988 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
39989 20 72 74 72 65 65 52 65 6e 61 6d 65 28 73 71 6c   rtreeRename(sql
3998a 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
3998b 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
3998c 65 77 4e 61 6d 65 29 7b 0a 20 20 52 74 72 65 65  ewName){.  Rtree
3998d 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65   *pRtree = (Rtre
3998e 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74  e *)pVtab;.  int
3998f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
39990 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  EM;.  char *zSql
39991 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
39992 74 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54  tf(.    "ALTER T
39993 41 42 4c 45 20 25 51 2e 27 25 71 5f 6e 6f 64 65  ABLE %Q.'%q_node
39994 27 20 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22  '   RENAME TO \"
39995 25 77 5f 6e 6f 64 65 5c 22 3b 22 0a 20 20 20 20  %w_node\";".    
39996 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e  "ALTER TABLE %Q.
39997 27 25 71 5f 70 61 72 65 6e 74 27 20 52 45 4e 41  '%q_parent' RENA
39998 4d 45 20 54 4f 20 5c 22 25 77 5f 70 61 72 65 6e  ME TO \"%w_paren
39999 74 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52  t\";".    "ALTER
3999a 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 72 6f   TABLE %Q.'%q_ro
3999b 77 69 64 27 20 20 52 45 4e 41 4d 45 20 54 4f 20  wid'  RENAME TO 
3999c 5c 22 25 77 5f 72 6f 77 69 64 5c 22 3b 22 0a 20  \"%w_rowid\";". 
3999d 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62     , pRtree->zDb
3999e 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c  , pRtree->zName,
3999f 20 7a 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20 2c   zNewName .    ,
399a0 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
399a1 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65  tree->zName, zNe
399a2 77 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74  wName .    , pRt
399a3 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65  ree->zDb, pRtree
399a4 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d  ->zName, zNewNam
399a5 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71  e.  );.  if( zSq
399a6 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
399a7 6c 69 74 65 33 5f 65 78 65 63 28 70 52 74 72 65  lite3_exec(pRtre
399a8 65 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20  e->db, zSql, 0, 
399a9 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
399aa 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
399ab 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
399ac 7d 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  }..static sqlite
399ad 33 5f 6d 6f 64 75 6c 65 20 72 74 72 65 65 4d 6f  3_module rtreeMo
399ae 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20  dule = {.  0,   
399af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
399b0 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
399b1 6e 20 2a 2f 0a 20 20 72 74 72 65 65 43 72 65 61  n */.  rtreeCrea
399b2 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
399b3 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2d 20     /* xCreate - 
399b4 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 2a  create a table *
399b5 2f 0a 20 20 72 74 72 65 65 43 6f 6e 6e 65 63 74  /.  rtreeConnect
399b6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
399b7 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 2d 20 63 6f  /* xConnect - co
399b8 6e 6e 65 63 74 20 74 6f 20 61 6e 20 65 78 69 73  nnect to an exis
399b9 74 69 6e 67 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ting table */.  
399ba 72 74 72 65 65 42 65 73 74 49 6e 64 65 78 2c 20  rtreeBestIndex, 
399bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
399bc 42 65 73 74 49 6e 64 65 78 20 2d 20 44 65 74 65  BestIndex - Dete
399bd 72 6d 69 6e 65 20 73 65 61 72 63 68 20 73 74 72  rmine search str
399be 61 74 65 67 79 20 2a 2f 0a 20 20 72 74 72 65 65  ategy */.  rtree
399bf 44 69 73 63 6f 6e 6e 65 63 74 2c 20 20 20 20 20  Disconnect,     
399c0 20 20 20 20 20 20 20 2f 2a 20 78 44 69 73 63 6f         /* xDisco
399c1 6e 6e 65 63 74 20 2d 20 44 69 73 63 6f 6e 6e 65  nnect - Disconne
399c2 63 74 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  ct from a table 
399c3 2a 2f 0a 20 20 72 74 72 65 65 44 65 73 74 72 6f  */.  rtreeDestro
399c4 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
399c5 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2d 20 44   /* xDestroy - D
399c6 72 6f 70 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20  rop a table */. 
399c7 20 72 74 72 65 65 4f 70 65 6e 2c 20 20 20 20 20   rtreeOpen,     
399c8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
399c9 78 4f 70 65 6e 20 2d 20 6f 70 65 6e 20 61 20 63  xOpen - open a c
399ca 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65  ursor */.  rtree
399cb 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
399cc 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
399cd 20 2d 20 63 6c 6f 73 65 20 61 20 63 75 72 73 6f   - close a curso
399ce 72 20 2a 2f 0a 20 20 72 74 72 65 65 46 69 6c 74  r */.  rtreeFilt
399cf 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
399d0 20 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 2d 20     /* xFilter - 
399d1 63 6f 6e 66 69 67 75 72 65 20 73 63 61 6e 20 63  configure scan c
399d2 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
399d3 72 74 72 65 65 4e 65 78 74 2c 20 20 20 20 20 20  rtreeNext,      
399d4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
399d5 4e 65 78 74 20 2d 20 61 64 76 61 6e 63 65 20 61  Next - advance a
399d6 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72   cursor */.  rtr
399d7 65 65 45 6f 66 2c 20 20 20 20 20 20 20 20 20 20  eeEof,          
399d8 20 20 20 20 20 20 20 20 20 2f 2a 20 78 45 6f 66           /* xEof
399d9 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f 6c 75 6d   */.  rtreeColum
399da 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
399db 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 2d 20 72    /* xColumn - r
399dc 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20 72 74  ead data */.  rt
399dd 72 65 65 52 6f 77 69 64 2c 20 20 20 20 20 20 20  reeRowid,       
399de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 6f            /* xRo
399df 77 69 64 20 2d 20 72 65 61 64 20 64 61 74 61 20  wid - read data 
399e0 2a 2f 0a 20 20 72 74 72 65 65 55 70 64 61 74 65  */.  rtreeUpdate
399e1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
399e2 20 2f 2a 20 78 55 70 64 61 74 65 20 2d 20 77 72   /* xUpdate - wr
399e3 69 74 65 20 64 61 74 61 20 2a 2f 0a 20 20 30 2c  ite data */.  0,
399e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
399e5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 42 65            /* xBe
399e6 67 69 6e 20 2d 20 62 65 67 69 6e 20 74 72 61 6e  gin - begin tran
399e7 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20  saction */.  0, 
399e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
399e9 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e           /* xSyn
399ea 63 20 2d 20 73 79 6e 63 20 74 72 61 6e 73 61 63  c - sync transac
399eb 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20  tion */.  0,    
399ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
399ed 20 20 20 20 20 20 2f 2a 20 78 43 6f 6d 6d 69 74        /* xCommit
399ee 20 2d 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61   - commit transa
399ef 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20  ction */.  0,   
399f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
399f1 20 20 20 20 20 20 20 2f 2a 20 78 52 6f 6c 6c 62         /* xRollb
399f2 61 63 6b 20 2d 20 72 6f 6c 6c 62 61 63 6b 20 74  ack - rollback t
399f3 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
399f4 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
399f5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
399f6 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2d 20 66  FindFunction - f
399f7 75 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64  unction overload
399f8 69 6e 67 20 2a 2f 0a 20 20 72 74 72 65 65 52 65  ing */.  rtreeRe
399f9 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
399fa 20 20 20 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20       /* xRename 
399fb 2d 20 72 65 6e 61 6d 65 20 74 68 65 20 74 61 62  - rename the tab
399fc 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 61 74 69 63  le */.};..static
399fd 20 69 6e 74 20 72 74 72 65 65 53 71 6c 49 6e 69   int rtreeSqlIni
399fe 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
399ff 65 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a  ee, .  sqlite3 *
39a00 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
39a01 72 20 2a 7a 44 62 2c 20 0a 20 20 63 6f 6e 73 74  r *zDb, .  const
39a02 20 63 68 61 72 20 2a 7a 50 72 65 66 69 78 2c 20   char *zPrefix, 
39a03 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 0a  .  int isCreate.
39a04 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
39a05 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 23 64 65 66  LITE_OK;..  #def
39a06 69 6e 65 20 4e 5f 53 54 41 54 45 4d 45 4e 54 20  ine N_STATEMENT 
39a07 39 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  9.  static const
39a08 20 63 68 61 72 20 2a 61 7a 53 71 6c 5b 4e 5f 53   char *azSql[N_S
39a09 54 41 54 45 4d 45 4e 54 5d 20 3d 20 7b 0a 20 20  TATEMENT] = {.  
39a0a 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
39a0b 69 74 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65  ite the xxx_node
39a0c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53   table */.    "S
39a0d 45 4c 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20  ELECT data FROM 
39a0e 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57  '%q'.'%q_node' W
39a0f 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31  HERE nodeno = :1
39a10 22 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f  ",.    "INSERT O
39a11 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27  R REPLACE INTO '
39a12 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41  %q'.'%q_node' VA
39a13 4c 55 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20  LUES(:1, :2)",. 
39a14 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
39a15 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57  '%q'.'%q_node' W
39a16 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31  HERE nodeno = :1
39a17 22 2c 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  ",..    /* Read 
39a18 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 78 78  and write the xx
39a19 78 5f 72 6f 77 69 64 20 74 61 62 6c 65 20 2a 2f  x_rowid table */
39a1a 0a 20 20 20 20 22 53 45 4c 45 43 54 20 6e 6f 64  .    "SELECT nod
39a1b 65 6e 6f 20 46 52 4f 4d 20 27 25 71 27 2e 27 25  eno FROM '%q'.'%
39a1c 71 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20 72  q_rowid' WHERE r
39a1d 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 20 20 20 20  owid = :1",.    
39a1e 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41  "INSERT OR REPLA
39a1f 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71  CE INTO '%q'.'%q
39a20 5f 72 6f 77 69 64 27 20 56 41 4c 55 45 53 28 3a  _rowid' VALUES(:
39a21 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
39a22 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
39a23 25 71 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20  %q_rowid' WHERE 
39a24 72 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 0a 20 20  rowid = :1",..  
39a25 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
39a26 69 74 65 20 74 68 65 20 78 78 78 5f 70 61 72 65  ite the xxx_pare
39a27 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  nt table */.    
39a28 22 53 45 4c 45 43 54 20 70 61 72 65 6e 74 6e 6f  "SELECT parentno
39a29 64 65 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71  de FROM '%q'.'%q
39a2a 5f 70 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e  _parent' WHERE n
39a2b 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20  odeno = :1",.   
39a2c 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c   "INSERT OR REPL
39a2d 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25  ACE INTO '%q'.'%
39a2e 71 5f 70 61 72 65 6e 74 27 20 56 41 4c 55 45 53  q_parent' VALUES
39a2f 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22  (:1, :2)",.    "
39a30 44 45 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27  DELETE FROM '%q'
39a31 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 57 48 45  .'%q_parent' WHE
39a32 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 0a  RE nodeno = :1".
39a33 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73    };.  sqlite3_s
39a34 74 6d 74 20 2a 2a 61 70 70 53 74 6d 74 5b 4e 5f  tmt **appStmt[N_
39a35 53 54 41 54 45 4d 45 4e 54 5d 3b 0a 20 20 69 6e  STATEMENT];.  in
39a36 74 20 69 3b 0a 0a 20 20 70 52 74 72 65 65 2d 3e  t i;..  pRtree->
39a37 64 62 20 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20  db = db;..  if( 
39a38 69 73 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20  isCreate ){.    
39a39 63 68 61 72 20 2a 7a 43 72 65 61 74 65 20 3d 20  char *zCreate = 
39a3a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
39a3b 0a 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c  ."CREATE TABLE \
39a3c 22 25 77 5c 22 2e 5c 22 25 77 5f 6e 6f 64 65 5c  "%w\".\"%w_node\
39a3d 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52  "(nodeno INTEGER
39a3e 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 64 61   PRIMARY KEY, da
39a3f 74 61 20 42 4c 4f 42 29 3b 22 0a 22 43 52 45 41  ta BLOB);"."CREA
39a40 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e  TE TABLE \"%w\".
39a41 5c 22 25 77 5f 72 6f 77 69 64 5c 22 28 72 6f 77  \"%w_rowid\"(row
39a42 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
39a43 52 59 20 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49  RY KEY, nodeno I
39a44 4e 54 45 47 45 52 29 3b 22 0a 22 43 52 45 41 54  NTEGER);"."CREAT
39a45 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c  E TABLE \"%w\".\
39a46 22 25 77 5f 70 61 72 65 6e 74 5c 22 28 6e 6f 64  "%w_parent\"(nod
39a47 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d  eno INTEGER PRIM
39a48 41 52 59 20 4b 45 59 2c 20 70 61 72 65 6e 74 6e  ARY KEY, parentn
39a49 6f 64 65 20 49 4e 54 45 47 45 52 29 3b 22 0a 22  ode INTEGER);"."
39a4a 49 4e 53 45 52 54 20 49 4e 54 4f 20 27 25 71 27  INSERT INTO '%q'
39a4b 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45  .'%q_node' VALUE
39a4c 53 28 31 2c 20 7a 65 72 6f 62 6c 6f 62 28 25 64  S(1, zeroblob(%d
39a4d 29 29 22 2c 0a 20 20 20 20 20 20 7a 44 62 2c 20  ))",.      zDb, 
39a4e 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50  zPrefix, zDb, zP
39a4f 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65  refix, zDb, zPre
39a50 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69  fix, zDb, zPrefi
39a51 78 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  x, pRtree->iNode
39a52 53 69 7a 65 0a 20 20 20 20 29 3b 0a 20 20 20 20  Size.    );.    
39a53 69 66 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a  if( !zCreate ){.
39a54 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
39a55 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
39a56 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
39a57 33 5f 65 78 65 63 28 64 62 2c 20 7a 43 72 65 61  3_exec(db, zCrea
39a58 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  te, 0, 0, 0);.  
39a59 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
39a5a 43 72 65 61 74 65 29 3b 0a 20 20 20 20 69 66 28  Create);.    if(
39a5b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
39a5c 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
39a5d 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
39a5e 61 70 70 53 74 6d 74 5b 30 5d 20 3d 20 26 70 52  appStmt[0] = &pR
39a5f 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 3b  tree->pReadNode;
39a60 0a 20 20 61 70 70 53 74 6d 74 5b 31 5d 20 3d 20  .  appStmt[1] = 
39a61 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e  &pRtree->pWriteN
39a62 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 32  ode;.  appStmt[2
39a63 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65  ] = &pRtree->pDe
39a64 6c 65 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53  leteNode;.  appS
39a65 74 6d 74 5b 33 5d 20 3d 20 26 70 52 74 72 65 65  tmt[3] = &pRtree
39a66 2d 3e 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20  ->pReadRowid;.  
39a67 61 70 70 53 74 6d 74 5b 34 5d 20 3d 20 26 70 52  appStmt[4] = &pR
39a68 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
39a69 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 35 5d 20  d;.  appStmt[5] 
39a6a 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  = &pRtree->pDele
39a6b 74 65 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74  teRowid;.  appSt
39a6c 6d 74 5b 36 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[6] = &pRtree-
39a6d 3e 70 52 65 61 64 50 61 72 65 6e 74 3b 0a 20 20  >pReadParent;.  
39a6e 61 70 70 53 74 6d 74 5b 37 5d 20 3d 20 26 70 52  appStmt[7] = &pR
39a6f 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65  tree->pWritePare
39a70 6e 74 3b 0a 20 20 61 70 70 53 74 6d 74 5b 38 5d  nt;.  appStmt[8]
39a71 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c   = &pRtree->pDel
39a72 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20 66 6f  eteParent;..  fo
39a73 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 54 41 54 45  r(i=0; i<N_STATE
39a74 4d 45 4e 54 20 26 26 20 72 63 3d 3d 53 51 4c 49  MENT && rc==SQLI
39a75 54 45 5f 4f 4b 3b 20 69 2b 2b 29 7b 0a 20 20 20  TE_OK; i++){.   
39a76 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71   char *zSql = sq
39a77 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a  lite3_mprintf(az
39a78 53 71 6c 5b 69 5d 2c 20 7a 44 62 2c 20 7a 50 72  Sql[i], zDb, zPr
39a79 65 66 69 78 29 3b 0a 20 20 20 20 69 66 28 20 7a  efix);.    if( z
39a7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Sql ){.      rc 
39a7b 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
39a7c 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
39a7d 31 2c 20 61 70 70 53 74 6d 74 5b 69 5d 2c 20 30  1, appStmt[i], 0
39a7e 29 3b 20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ); .    }else{. 
39a7f 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
39a80 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
39a81 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
39a82 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Sql);.  }..  ret
39a83 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
39a84 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 71 75   This routine qu
39a85 65 72 69 65 73 20 64 61 74 61 62 61 73 65 20 68  eries database h
39a86 61 6e 64 6c 65 20 64 62 20 66 6f 72 20 74 68 65  andle db for the
39a87 20 70 61 67 65 2d 73 69 7a 65 20 75 73 65 64 20   page-size used 
39a88 62 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 7a  by.** database z
39a89 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  Db. If successfu
39a8a 6c 2c 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  l, the page-size
39a8b 20 69 6e 20 62 79 74 65 73 20 69 73 20 77 72 69   in bytes is wri
39a8c 74 74 65 6e 20 74 6f 0a 2a 2a 20 2a 70 69 50 61  tten to.** *piPa
39a8d 67 65 53 69 7a 65 20 61 6e 64 20 53 51 4c 49 54  geSize and SQLIT
39a8e 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 4f  E_OK returned. O
39a8f 74 68 65 72 77 69 73 65 2c 20 61 6e 64 20 61 6e  therwise, and an
39a90 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a   SQLite error .*
39a91 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  * code is return
39a92 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
39a93 74 20 67 65 74 50 61 67 65 53 69 7a 65 28 73 71  t getPageSize(sq
39a94 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
39a95 20 63 68 61 72 20 2a 7a 44 62 2c 20 69 6e 74 20   char *zDb, int 
39a96 2a 70 69 50 61 67 65 53 69 7a 65 29 7b 0a 20 20  *piPageSize){.  
39a97 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
39a98 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a  NOMEM;.  char *z
39a99 53 71 6c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  Sql;.  sqlite3_s
39a9a 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a  tmt *pStmt = 0;.
39a9b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
39a9c 33 5f 6d 70 72 69 6e 74 66 28 22 50 52 41 47 4d  3_mprintf("PRAGM
39a9d 41 20 25 51 2e 70 61 67 65 5f 73 69 7a 65 22 2c  A %Q.page_size",
39a9e 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 21 7a 53   zDb);.  if( !zS
39a9f 71 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ql ){.    return
39aa0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
39aa1 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
39aa2 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
39aa3 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
39aa4 6d 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  mt, 0);.  sqlite
39aa5 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
39aa6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
39aa7 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
39aa8 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53  rc;.  }..  if( S
39aa9 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
39aaa 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
39aab 7b 0a 20 20 20 20 2a 70 69 50 61 67 65 53 69 7a  {.    *piPageSiz
39aac 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
39aad 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30 29  mn_int(pStmt, 0)
39aae 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
39aaf 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
39ab0 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  pStmt);.}../* .*
39ab1 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
39ab2 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
39ab3 61 74 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74 68  ation of both th
39ab4 65 20 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20 78  e xConnect and x
39ab5 43 72 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f 64  Create.** method
39ab6 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  s of the r-tree 
39ab7 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
39ab8 2a 0a 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20  *.**   argv[0]  
39ab9 20 2d 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a   -> module name.
39aba 2a 2a 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d  **   argv[1]   -
39abb 3e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a  > database name.
39abc 2a 2a 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d  **   argv[2]   -
39abd 3e 20 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a 20  > table name.** 
39abe 20 20 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 63    argv[...] -> c
39abf 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 2e 2e 0a 2a  olumn names....*
39ac0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
39ac1 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  eeInit(.  sqlite
39ac2 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
39ac3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39ac4 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
39ac5 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
39ac6 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20  pAux,           
39ac7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39ac8 20 4f 6e 65 20 6f 66 20 74 68 65 20 52 54 52 45   One of the RTRE
39ac9 45 5f 43 4f 4f 52 44 5f 2a 20 63 6f 6e 73 74 61  E_COORD_* consta
39aca 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67  nts */.  int arg
39acb 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
39acc 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20 2f 2a 20  onst*argv,   /* 
39acd 50 61 72 61 6d 65 74 65 72 73 20 74 6f 20 43 52  Parameters to CR
39ace 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
39acf 6d 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ment */.  sqlite
39ad0 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
39ad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39ad2 20 4f 55 54 3a 20 4e 65 77 20 76 69 72 74 75 61   OUT: New virtua
39ad3 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  l table */.  cha
39ad4 72 20 2a 2a 70 7a 45 72 72 2c 20 20 20 20 20 20  r **pzErr,      
39ad5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39ad6 20 2f 2a 20 4f 55 54 3a 20 45 72 72 6f 72 20 6d   /* OUT: Error m
39ad7 65 73 73 61 67 65 2c 20 69 66 20 61 6e 79 20 2a  essage, if any *
39ad8 2f 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65  /.  int isCreate
39ad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39ada 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
39adb 66 6f 72 20 78 43 72 65 61 74 65 2c 20 66 61 6c  for xCreate, fal
39adc 73 65 20 66 6f 72 20 78 43 6f 6e 6e 65 63 74 20  se for xConnect 
39add 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
39ade 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
39adf 74 20 69 50 61 67 65 53 69 7a 65 20 3d 20 30 3b  t iPageSize = 0;
39ae0 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
39ae1 3b 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20  ;.  int nDb;    
39ae2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
39ae3 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 61 72  gth of string ar
39ae4 67 76 5b 31 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  gv[1] */.  int n
39ae5 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
39ae6 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74   /* Length of st
39ae7 72 69 6e 67 20 61 72 67 76 5b 32 5d 20 2a 2f 0a  ring argv[2] */.
39ae8 20 20 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65    int eCoordType
39ae9 20 3d 20 28 69 6e 74 29 70 41 75 78 3b 0a 0a 20   = (int)pAux;.. 
39aea 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 45 72   const char *aEr
39aeb 72 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  rMsg[] = {.    0
39aec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
39aed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39aee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39aef 20 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20       /* 0 */.   
39af0 20 22 57 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f   "Wrong number o
39af1 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e  f columns for an
39af2 20 72 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20   rtree table",  
39af3 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20         /* 1 */. 
39af4 20 20 20 22 54 6f 6f 20 66 65 77 20 63 6f 6c 75     "Too few colu
39af5 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65  mns for an rtree
39af6 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20   table",        
39af7 20 20 20 20 20 20 20 20 20 2f 2a 20 32 20 2a 2f           /* 2 */
39af8 0a 20 20 20 20 22 54 6f 6f 20 6d 61 6e 79 20 63  .    "Too many c
39af9 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74  olumns for an rt
39afa 72 65 65 20 74 61 62 6c 65 22 20 20 20 20 20 20  ree table"      
39afb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 20             /* 3 
39afc 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69  */.  };..  int i
39afd 45 72 72 20 3d 20 28 61 72 67 63 3c 36 29 20 3f  Err = (argc<6) ?
39afe 20 32 20 3a 20 61 72 67 63 3e 28 52 54 52 45 45   2 : argc>(RTREE
39aff 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a  _MAX_DIMENSIONS*
39b00 32 2b 34 29 20 3f 20 33 20 3a 20 61 72 67 63 25  2+4) ? 3 : argc%
39b01 32 3b 0a 20 20 69 66 28 20 61 45 72 72 4d 73 67  2;.  if( aErrMsg
39b02 5b 69 45 72 72 5d 20 29 7b 0a 20 20 20 20 2a 70  [iErr] ){.    *p
39b03 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
39b04 70 72 69 6e 74 66 28 22 25 73 22 2c 20 61 45 72  printf("%s", aEr
39b05 72 4d 73 67 5b 69 45 72 72 5d 29 3b 0a 20 20 20  rMsg[iErr]);.   
39b06 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
39b07 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  RROR;.  }..  rc 
39b08 3d 20 67 65 74 50 61 67 65 53 69 7a 65 28 64 62  = getPageSize(db
39b09 2c 20 61 72 67 76 5b 31 5d 2c 20 26 69 50 61 67  , argv[1], &iPag
39b0a 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63  eSize);.  if( rc
39b0b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
39b0c 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
39b0d 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
39b0e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61   the sqlite3_vta
39b0f 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  b structure */. 
39b10 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e 28 61 72   nDb = strlen(ar
39b11 67 76 5b 31 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20  gv[1]);.  nName 
39b12 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 32 5d  = strlen(argv[2]
39b13 29 3b 0a 20 20 70 52 74 72 65 65 20 3d 20 28 52  );.  pRtree = (R
39b14 74 72 65 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d  tree *)sqlite3_m
39b15 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72  alloc(sizeof(Rtr
39b16 65 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29  ee)+nDb+nName+2)
39b17 3b 0a 20 20 69 66 28 20 21 70 52 74 72 65 65 20  ;.  if( !pRtree 
39b18 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
39b19 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
39b1a 20 20 6d 65 6d 73 65 74 28 70 52 74 72 65 65 2c    memset(pRtree,
39b1b 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
39b1c 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a  )+nDb+nName+2);.
39b1d 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 20    pRtree->nBusy 
39b1e 3d 20 31 3b 0a 20 20 70 52 74 72 65 65 2d 3e 62  = 1;.  pRtree->b
39b1f 61 73 65 2e 70 4d 6f 64 75 6c 65 20 3d 20 26 72  ase.pModule = &r
39b20 74 72 65 65 4d 6f 64 75 6c 65 3b 0a 20 20 70 52  treeModule;.  pR
39b21 74 72 65 65 2d 3e 7a 44 62 20 3d 20 28 63 68 61  tree->zDb = (cha
39b22 72 20 2a 29 26 70 52 74 72 65 65 5b 31 5d 3b 0a  r *)&pRtree[1];.
39b23 20 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 20    pRtree->zName 
39b24 3d 20 26 70 52 74 72 65 65 2d 3e 7a 44 62 5b 6e  = &pRtree->zDb[n
39b25 44 62 2b 31 5d 3b 0a 20 20 70 52 74 72 65 65 2d  Db+1];.  pRtree-
39b26 3e 6e 44 69 6d 20 3d 20 28 61 72 67 63 2d 34 29  >nDim = (argc-4)
39b27 2f 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42  /2;.  pRtree->nB
39b28 79 74 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20  ytesPerCell = 8 
39b29 2b 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 34  + pRtree->nDim*4
39b2a 2a 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 65 43  *2;.  pRtree->eC
39b2b 6f 6f 72 64 54 79 70 65 20 3d 20 65 43 6f 6f 72  oordType = eCoor
39b2c 64 54 79 70 65 3b 0a 20 20 6d 65 6d 63 70 79 28  dType;.  memcpy(
39b2d 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 61 72 67  pRtree->zDb, arg
39b2e 76 5b 31 5d 2c 20 6e 44 62 29 3b 0a 20 20 6d 65  v[1], nDb);.  me
39b2f 6d 63 70 79 28 70 52 74 72 65 65 2d 3e 7a 4e 61  mcpy(pRtree->zNa
39b30 6d 65 2c 20 61 72 67 76 5b 32 5d 2c 20 6e 4e 61  me, argv[2], nNa
39b31 6d 65 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  me);..  /* Figur
39b32 65 20 6f 75 74 20 74 68 65 20 6e 6f 64 65 20 73  e out the node s
39b33 69 7a 65 20 74 6f 20 75 73 65 2e 20 42 79 20 64  ize to use. By d
39b34 65 66 61 75 6c 74 2c 20 75 73 65 20 36 34 20 62  efault, use 64 b
39b35 79 74 65 73 20 6c 65 73 73 20 74 68 61 6e 0a 20  ytes less than. 
39b36 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
39b37 20 70 61 67 65 2d 73 69 7a 65 2e 20 54 68 69 73   page-size. This
39b38 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 65 61   ensures that ea
39b39 63 68 20 6e 6f 64 65 20 69 73 20 73 74 6f 72 65  ch node is store
39b3a 64 20 6f 6e 0a 20 20 2a 2a 20 61 20 73 69 6e 67  d on.  ** a sing
39b3b 6c 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  le database page
39b3c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
39b3d 68 65 20 64 61 74 61 62 61 73 64 20 70 61 67 65  he databasd page
39b3e 2d 73 69 7a 65 20 69 73 20 73 6f 20 6c 61 72 67  -size is so larg
39b3f 65 20 74 68 61 74 20 6d 6f 72 65 20 74 68 61 6e  e that more than
39b40 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 0a   RTREE_MAXCELLS.
39b41 20 20 2a 2a 20 65 6e 74 72 69 65 73 20 77 6f 75    ** entries wou
39b42 6c 64 20 66 69 74 20 69 6e 20 61 20 73 69 6e 67  ld fit in a sing
39b43 6c 65 20 6e 6f 64 65 2c 20 75 73 65 20 61 20 73  le node, use a s
39b44 6d 61 6c 6c 65 72 20 6e 6f 64 65 2d 73 69 7a 65  maller node-size
39b45 2e 0a 20 20 2a 2f 0a 20 20 70 52 74 72 65 65 2d  ..  */.  pRtree-
39b46 3e 69 4e 6f 64 65 53 69 7a 65 20 3d 20 69 50 61  >iNodeSize = iPa
39b47 67 65 53 69 7a 65 2d 36 34 3b 0a 20 20 69 66 28  geSize-64;.  if(
39b48 20 28 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74   (4+pRtree->nByt
39b49 65 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f  esPerCell*RTREE_
39b4a 4d 41 58 43 45 4c 4c 53 29 3c 70 52 74 72 65 65  MAXCELLS)<pRtree
39b4b 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20  ->iNodeSize ){. 
39b4c 20 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65     pRtree->iNode
39b4d 53 69 7a 65 20 3d 20 34 2b 70 52 74 72 65 65 2d  Size = 4+pRtree-
39b4e 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52  >nBytesPerCell*R
39b4f 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 3b 0a 20  TREE_MAXCELLS;. 
39b50 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f   }..  /* Create/
39b51 43 6f 6e 6e 65 63 74 20 74 6f 20 74 68 65 20 75  Connect to the u
39b52 6e 64 65 72 6c 79 69 6e 67 20 72 65 6c 61 74 69  nderlying relati
39b53 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73 63  onal database sc
39b54 68 65 6d 61 2e 20 49 66 0a 20 20 2a 2a 20 74 68  hema. If.  ** th
39b55 61 74 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  at is successful
39b56 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 64  , call sqlite3_d
39b57 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 74 6f  eclare_vtab() to
39b58 20 63 6f 6e 66 69 67 75 72 65 0a 20 20 2a 2a 20   configure.  ** 
39b59 74 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65  the r-tree table
39b5a 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2f 0a 20 20   schema..  */.  
39b5b 69 66 28 20 28 72 63 20 3d 20 72 74 72 65 65 53  if( (rc = rtreeS
39b5c 71 6c 49 6e 69 74 28 70 52 74 72 65 65 2c 20 64  qlInit(pRtree, d
39b5d 62 2c 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 76  b, argv[1], argv
39b5e 5b 32 5d 2c 20 69 73 43 72 65 61 74 65 29 29 20  [2], isCreate)) 
39b5f 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  ){.    *pzErr = 
39b60 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
39b61 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72  "%s", sqlite3_er
39b62 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 7d 65 6c  rmsg(db));.  }el
39b63 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53  se{.    char *zS
39b64 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
39b65 69 6e 74 66 28 22 43 52 45 41 54 45 20 54 41 42  intf("CREATE TAB
39b66 4c 45 20 78 28 25 73 22 2c 20 61 72 67 76 5b 33  LE x(%s", argv[3
39b67 5d 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  ]);.    char *zT
39b68 6d 70 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a  mp;.    int ii;.
39b69 20 20 20 20 66 6f 72 28 69 69 3d 34 3b 20 7a 53      for(ii=4; zS
39b6a 71 6c 20 26 26 20 69 69 3c 61 72 67 63 3b 20 69  ql && ii<argc; i
39b6b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 54 6d 70  i++){.      zTmp
39b6c 20 3d 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 7a   = zSql;.      z
39b6d 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
39b6e 72 69 6e 74 66 28 22 25 73 2c 20 25 73 22 2c 20  rintf("%s, %s", 
39b6f 7a 54 6d 70 2c 20 61 72 67 76 5b 69 69 5d 29 3b  zTmp, argv[ii]);
39b70 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
39b71 72 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d  ree(zTmp);.    }
39b72 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  .    if( zSql ){
39b73 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53  .      zTmp = zS
39b74 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d  ql;.      zSql =
39b75 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
39b76 28 22 25 73 29 3b 22 2c 20 7a 54 6d 70 29 3b 0a  ("%s);", zTmp);.
39b77 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
39b78 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d 0a  ee(zTmp);.    }.
39b79 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b      if( !zSql ){
39b7a 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
39b7b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
39b7c 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  lse if( SQLITE_O
39b7d 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
39b7e 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 64 62  _declare_vtab(db
39b7f 2c 20 7a 53 71 6c 29 29 20 29 7b 0a 20 20 20 20  , zSql)) ){.    
39b80 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
39b81 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
39b82 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
39b83 64 62 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  db));.    }.    
39b84 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
39b85 6c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  l);.  }..  if( r
39b86 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
39b87 20 20 20 20 2a 70 70 56 74 61 62 20 3d 20 28 73      *ppVtab = (s
39b88 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 70 52  qlite3_vtab *)pR
39b89 74 72 65 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tree;.  }else{. 
39b8a 20 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28     rtreeRelease(
39b8b 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72  pRtree);.  }.  r
39b8c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
39b8d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
39b8e 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61 72 20 66  on of a scalar f
39b8f 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65 63  unction that dec
39b90 6f 64 65 73 20 72 2d 74 72 65 65 20 6e 6f 64 65  odes r-tree node
39b91 73 20 74 6f 0a 2a 2a 20 68 75 6d 61 6e 20 72 65  s to.** human re
39b92 61 64 61 62 6c 65 20 73 74 72 69 6e 67 73 2e 20  adable strings. 
39b93 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
39b94 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 61   for debugging a
39b95 6e 64 20 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a  nd analysis..**.
39b96 2a 2a 20 54 68 65 20 73 63 61 6c 61 72 20 66 75  ** The scalar fu
39b97 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 74 77 6f  nction takes two
39b98 20 61 72 67 75 6d 65 6e 74 73 2c 20 61 20 62 6c   arguments, a bl
39b99 6f 62 20 6f 66 20 64 61 74 61 20 63 6f 6e 74 61  ob of data conta
39b9a 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 72 2d 74 72  ining.** an r-tr
39b9b 65 65 20 6e 6f 64 65 2c 20 61 6e 64 20 74 68 65  ee node, and the
39b9c 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 6d 65 6e   number of dimen
39b9d 73 69 6f 6e 73 20 74 68 65 20 72 2d 74 72 65 65  sions the r-tree
39b9e 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 46 6f 72   indexes..** For
39b9f 20 61 20 74 77 6f 2d 64 69 6d 65 6e 73 69 6f 6e   a two-dimension
39ba0 61 6c 20 72 2d 74 72 65 65 20 73 74 72 75 63 74  al r-tree struct
39ba1 75 72 65 20 63 61 6c 6c 65 64 20 22 72 74 22 2c  ure called "rt",
39ba2 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 0a   to deserialize.
39ba3 2a 2a 20 61 6c 6c 20 6e 6f 64 65 73 2c 20 61 20  ** all nodes, a 
39ba4 73 74 61 74 65 6d 65 6e 74 20 6c 69 6b 65 3a 0a  statement like:.
39ba5 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 72  **.**   SELECT r
39ba6 74 72 65 65 6e 6f 64 65 28 32 2c 20 64 61 74 61  treenode(2, data
39ba7 29 20 46 52 4f 4d 20 72 74 5f 6e 6f 64 65 3b 0a  ) FROM rt_node;.
39ba8 2a 2a 0a 2a 2a 20 54 68 65 20 68 75 6d 61 6e 20  **.** The human 
39ba9 72 65 61 64 61 62 6c 65 20 73 74 72 69 6e 67 20  readable string 
39baa 74 61 6b 65 73 20 74 68 65 20 66 6f 72 6d 20 6f  takes the form o
39bab 66 20 61 20 54 63 6c 20 6c 69 73 74 20 77 69 74  f a Tcl list wit
39bac 68 20 6f 6e 65 0a 2a 2a 20 65 6e 74 72 79 20 66  h one.** entry f
39bad 6f 72 20 65 61 63 68 20 63 65 6c 6c 20 69 6e 20  or each cell in 
39bae 74 68 65 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e  the r-tree node.
39baf 20 45 61 63 68 20 65 6e 74 72 79 20 69 73 20 69   Each entry is i
39bb0 74 73 65 6c 66 20 61 0a 2a 2a 20 6c 69 73 74 2c  tself a.** list,
39bb1 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
39bb2 38 2d 62 79 74 65 20 72 6f 77 69 64 2f 70 61 67  8-byte rowid/pag
39bb3 65 6e 6f 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  eno followed by 
39bb4 74 68 65 20 0a 2a 2a 20 3c 6e 75 6d 2d 64 69 6d  the .** <num-dim
39bb5 65 6e 73 69 6f 6e 3e 2a 32 20 63 6f 6f 72 64 69  ension>*2 coordi
39bb6 6e 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  nates..*/.static
39bb7 20 76 6f 69 64 20 72 74 72 65 65 6e 6f 64 65 28   void rtreenode(
39bb8 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
39bb9 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72 67 2c 20  *ctx, int nArg, 
39bba 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
39bbb 61 70 41 72 67 29 7b 0a 20 20 63 68 61 72 20 2a  apArg){.  char *
39bbc 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20 52 74 72  zText = 0;.  Rtr
39bbd 65 65 4e 6f 64 65 20 6e 6f 64 65 3b 0a 20 20 52  eeNode node;.  R
39bbe 74 72 65 65 20 74 72 65 65 3b 0a 20 20 69 6e 74  tree tree;.  int
39bbf 20 69 69 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26   ii;..  memset(&
39bc0 6e 6f 64 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  node, 0, sizeof(
39bc1 52 74 72 65 65 4e 6f 64 65 29 29 3b 0a 20 20 6d  RtreeNode));.  m
39bc2 65 6d 73 65 74 28 26 74 72 65 65 2c 20 30 2c 20  emset(&tree, 0, 
39bc3 73 69 7a 65 6f 66 28 52 74 72 65 65 29 29 3b 0a  sizeof(Rtree));.
39bc4 20 20 74 72 65 65 2e 6e 44 69 6d 20 3d 20 73 71    tree.nDim = sq
39bc5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
39bc6 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 74 72 65  apArg[0]);.  tre
39bc7 65 2e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 20  e.nBytesPerCell 
39bc8 3d 20 38 20 2b 20 38 20 2a 20 74 72 65 65 2e 6e  = 8 + 8 * tree.n
39bc9 44 69 6d 3b 0a 20 20 6e 6f 64 65 2e 7a 44 61 74  Dim;.  node.zDat
39bca 61 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  a = (u8 *)sqlite
39bcb 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 70 41  3_value_blob(apA
39bcc 72 67 5b 31 5d 29 3b 0a 0a 20 20 66 6f 72 28 69  rg[1]);..  for(i
39bcd 69 3d 30 3b 20 69 69 3c 4e 43 45 4c 4c 28 26 6e  i=0; ii<NCELL(&n
39bce 6f 64 65 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  ode); ii++){.   
39bcf 20 63 68 61 72 20 7a 43 65 6c 6c 5b 35 31 32 5d   char zCell[512]
39bd0 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20  ;.    int nCell 
39bd1 3d 20 30 3b 0a 20 20 20 20 52 74 72 65 65 43 65  = 0;.    RtreeCe
39bd2 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 69 6e 74  ll cell;.    int
39bd3 20 6a 6a 3b 0a 0a 20 20 20 20 6e 6f 64 65 47 65   jj;..    nodeGe
39bd4 74 43 65 6c 6c 28 26 74 72 65 65 2c 20 26 6e 6f  tCell(&tree, &no
39bd5 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a  de, ii, &cell);.
39bd6 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
39bd7 69 6e 74 66 28 35 31 32 2d 6e 43 65 6c 6c 2c 26  intf(512-nCell,&
39bd8 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 25 64  zCell[nCell],"%d
39bd9 22 2c 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b  ", cell.iRowid);
39bda 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 73 74 72  .    nCell = str
39bdb 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a 20 20 20 20  len(zCell);.    
39bdc 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 74 72 65  for(jj=0; jj<tre
39bdd 65 2e 6e 44 69 6d 2a 32 3b 20 6a 6a 2b 2b 29 7b  e.nDim*2; jj++){
39bde 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
39bdf 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e 43 65 6c  nprintf(512-nCel
39be0 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c  l,&zCell[nCell],
39be1 22 20 25 66 22 2c 28 64 6f 75 62 6c 65 29 63 65  " %f",(double)ce
39be2 6c 6c 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 2e 66 29  ll.aCoord[jj].f)
39be3 3b 0a 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20  ;.      nCell = 
39be4 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a 20  strlen(zCell);. 
39be5 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a 54     }..    if( zT
39be6 65 78 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61  ext ){.      cha
39be7 72 20 2a 7a 54 65 78 74 4e 65 77 20 3d 20 73 71  r *zTextNew = sq
39be8 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
39be9 73 20 7b 25 73 7d 22 2c 20 7a 54 65 78 74 2c 20  s {%s}", zText, 
39bea 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71  zCell);.      sq
39beb 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 78 74  lite3_free(zText
39bec 29 3b 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d  );.      zText =
39bed 20 7a 54 65 78 74 4e 65 77 3b 0a 20 20 20 20 7d   zTextNew;.    }
39bee 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 54 65 78  else{.      zTex
39bef 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  t = sqlite3_mpri
39bf0 6e 74 66 28 22 7b 25 73 7d 22 2c 20 7a 43 65 6c  ntf("{%s}", zCel
39bf1 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  l);.    }.  }.  
39bf2 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
39bf3 74 5f 74 65 78 74 28 63 74 78 2c 20 7a 54 65 78  t_text(ctx, zTex
39bf4 74 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66  t, -1, sqlite3_f
39bf5 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  ree);.}..static 
39bf6 76 6f 69 64 20 72 74 72 65 65 64 65 70 74 68 28  void rtreedepth(
39bf7 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
39bf8 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72 67 2c 20  *ctx, int nArg, 
39bf9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
39bfa 61 70 41 72 67 29 7b 0a 20 20 69 66 28 20 73 71  apArg){.  if( sq
39bfb 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
39bfc 28 61 70 41 72 67 5b 30 5d 29 21 3d 53 51 4c 49  (apArg[0])!=SQLI
39bfd 54 45 5f 42 4c 4f 42 20 0a 20 20 20 7c 7c 20 73  TE_BLOB .   || s
39bfe 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
39bff 65 73 28 61 70 41 72 67 5b 30 5d 29 3c 32 0a 20  es(apArg[0])<2. 
39c00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
39c01 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 74 78  result_error(ctx
39c02 2c 20 22 49 6e 76 61 6c 69 64 20 61 72 67 75 6d  , "Invalid argum
39c03 65 6e 74 20 74 6f 20 72 74 72 65 65 64 65 70 74  ent to rtreedept
39c04 68 28 29 22 2c 20 2d 31 29 3b 20 0a 20 20 7d 65  h()", -1); .  }e
39c05 6c 73 65 7b 0a 20 20 20 20 75 38 20 2a 7a 42 6c  lse{.    u8 *zBl
39c06 6f 62 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  ob = (u8 *)sqlit
39c07 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 70  e3_value_blob(ap
39c08 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 73 71 6c  Arg[0]);.    sql
39c09 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
39c0a 63 74 78 2c 20 72 65 61 64 49 6e 74 31 36 28 7a  ctx, readInt16(z
39c0b 42 6c 6f 62 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Blob));.  }.}../
39c0c 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68  *.** Register th
39c0d 65 20 72 2d 74 72 65 65 20 6d 6f 64 75 6c 65 20  e r-tree module 
39c0e 77 69 74 68 20 64 61 74 61 62 61 73 65 20 68 61  with database ha
39c0f 6e 64 6c 65 20 64 62 2e 20 54 68 69 73 20 63 72  ndle db. This cr
39c10 65 61 74 65 73 20 74 68 65 0a 2a 2a 20 76 69 72  eates the.** vir
39c11 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
39c12 65 20 22 72 74 72 65 65 22 20 61 6e 64 20 74 68  e "rtree" and th
39c13 65 20 64 65 62 75 67 67 69 6e 67 2f 61 6e 61 6c  e debugging/anal
39c14 79 73 69 73 20 73 63 61 6c 61 72 20 0a 2a 2a 20  ysis scalar .** 
39c15 66 75 6e 63 74 69 6f 6e 20 22 72 74 72 65 65 6e  function "rtreen
39c16 6f 64 65 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ode"..*/.SQLITE_
39c17 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
39c18 74 65 33 52 74 72 65 65 49 6e 69 74 28 73 71 6c  te3RtreeInit(sql
39c19 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
39c1a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
39c1b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
39c1c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74  TE_OK ){.    int
39c1d 20 75 74 66 38 20 3d 20 53 51 4c 49 54 45 5f 55   utf8 = SQLITE_U
39c1e 54 46 38 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  TF8;.    rc = sq
39c1f 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
39c20 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72 65 65  ction(db, "rtree
39c21 6e 6f 64 65 22 2c 20 32 2c 20 75 74 66 38 2c 20  node", 2, utf8, 
39c22 30 2c 20 72 74 72 65 65 6e 6f 64 65 2c 20 30 2c  0, rtreenode, 0,
39c23 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72   0);.  }.  if( r
39c24 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
39c25 20 20 20 20 69 6e 74 20 75 74 66 38 20 3d 20 53      int utf8 = S
39c26 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
39c27 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
39c28 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  ate_function(db,
39c29 20 22 72 74 72 65 65 64 65 70 74 68 22 2c 20 31   "rtreedepth", 1
39c2a 2c 20 75 74 66 38 2c 20 30 2c 72 74 72 65 65 64  , utf8, 0,rtreed
39c2b 65 70 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  epth, 0, 0);.  }
39c2c 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
39c2d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76 6f 69 64  E_OK ){.    void
39c2e 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29 52 54   *c = (void *)RT
39c2f 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
39c30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
39c31 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
39c32 5f 76 32 28 64 62 2c 20 22 72 74 72 65 65 22 2c  _v2(db, "rtree",
39c33 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 2c 20 63   &rtreeModule, c
39c34 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 0);.  }.  if( 
39c35 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
39c36 0a 20 20 20 20 76 6f 69 64 20 2a 63 20 3d 20 28  .    void *c = (
39c37 76 6f 69 64 20 2a 29 52 54 52 45 45 5f 43 4f 4f  void *)RTREE_COO
39c38 52 44 5f 49 4e 54 33 32 3b 0a 20 20 20 20 72 63  RD_INT32;.    rc
39c39 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
39c3a 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64 62 2c 20  e_module_v2(db, 
39c3b 22 72 74 72 65 65 5f 69 33 32 22 2c 20 26 72 74  "rtree_i32", &rt
39c3c 72 65 65 4d 6f 64 75 6c 65 2c 20 63 2c 20 30 29  reeModule, c, 0)
39c3d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
39c3e 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49  rc;.}..#if !SQLI
39c3f 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41  TE_CORE.SQLITE_A
39c40 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
39c41 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20  xtension_init(. 
39c42 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
39c43 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
39c44 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
39c45 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70  _api_routines *p
39c46 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f  Api.){.  SQLITE_
39c47 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28  EXTENSION_INIT2(
39c48 70 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73  pApi).  return s
39c49 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74 28  qlite3RtreeInit(
39c4a 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  db);.}.#endif..#
39c4b 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
39c4c 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 74  ****** End of rt
39c4d 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.c **********
39c4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39c4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39c50 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
39c51 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
39c52 65 20 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  e icu.c ********
39c53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39c54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39c55 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
39c56 37 20 4d 61 79 20 36 0a 2a 2a 0a 2a 2a 20 54 68  7 May 6.**.** Th
39c57 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
39c58 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
39c59 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
39c5a 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
39c5b 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
39c5c 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
39c5d 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
39c5e 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
39c5f 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
39c60 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
39c61 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
39c62 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
39c63 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
39c64 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
39c65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
39c66 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
39c67 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
39c68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39c69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39c6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39c6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39c6c 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 69  ******.** $Id: i
39c6d 63 75 2e 63 2c 76 20 31 2e 37 20 32 30 30 37 2f  cu.c,v 1.7 2007/
39c6e 31 32 2f 31 33 20 32 31 3a 35 34 3a 31 31 20 64  12/13 21:54:11 d
39c6f 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54  rh Exp $.**.** T
39c70 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
39c71 6e 74 73 20 61 6e 20 69 6e 74 65 67 72 61 74 69  nts an integrati
39c72 6f 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 49  on between the I
39c73 43 55 20 6c 69 62 72 61 72 79 20 0a 2a 2a 20 28  CU library .** (
39c74 22 49 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 20 43  "International C
39c75 6f 6d 70 6f 6e 65 6e 74 73 20 66 6f 72 20 55 6e  omponents for Un
39c76 69 63 6f 64 65 22 2c 20 61 6e 20 6f 70 65 6e 2d  icode", an open-
39c77 73 6f 75 72 63 65 20 6c 69 62 72 61 72 79 20 0a  source library .
39c78 2a 2a 20 66 6f 72 20 68 61 6e 64 6c 69 6e 67 20  ** for handling 
39c79 75 6e 69 63 6f 64 65 20 64 61 74 61 29 20 61 6e  unicode data) an
39c7a 64 20 53 51 4c 69 74 65 2e 20 54 68 65 20 69 6e  d SQLite. The in
39c7b 74 65 67 72 61 74 69 6f 6e 20 75 73 65 73 20 0a  tegration uses .
39c7c 2a 2a 20 49 43 55 20 74 6f 20 70 72 6f 76 69 64  ** ICU to provid
39c7d 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
39c7e 74 6f 20 53 51 4c 69 74 65 3a 0a 2a 2a 0a 2a 2a  to SQLite:.**.**
39c7f 20 20 20 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e     * An implemen
39c80 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51  tation of the SQ
39c81 4c 20 72 65 67 65 78 70 28 29 20 66 75 6e 63 74  L regexp() funct
39c82 69 6f 6e 20 28 61 6e 64 20 68 65 6e 63 65 20 52  ion (and hence R
39c83 45 47 45 58 50 0a 2a 2a 20 20 20 20 20 6f 70 65  EGEXP.**     ope
39c84 72 61 74 6f 72 29 20 75 73 69 6e 67 20 74 68 65  rator) using the
39c85 20 49 43 55 20 75 72 65 67 65 78 5f 58 58 28 29   ICU uregex_XX()
39c86 20 41 50 49 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a   APIs..**.**   *
39c87 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   Implementations
39c88 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61 6c   of the SQL scal
39c89 61 72 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c  ar upper() and l
39c8a 6f 77 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 73  ower() functions
39c8b 0a 2a 2a 20 20 20 20 20 66 6f 72 20 63 61 73 65  .**     for case
39c8c 20 6d 61 70 70 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   mapping..**.** 
39c8d 20 20 2a 20 49 6e 74 65 67 72 61 74 69 6f 6e 20    * Integration 
39c8e 6f 66 20 49 43 55 20 61 6e 64 20 53 51 4c 69 74  of ICU and SQLit
39c8f 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 65  e collation seqe
39c90 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  nces..**.**   * 
39c91 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  An implementatio
39c92 6e 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 70  n of the LIKE op
39c93 65 72 61 74 6f 72 20 74 68 61 74 20 75 73 65 73  erator that uses
39c94 20 49 43 55 20 74 6f 20 0a 2a 2a 20 20 20 20 20   ICU to .**     
39c95 70 72 6f 76 69 64 65 20 63 61 73 65 2d 69 6e 64  provide case-ind
39c96 65 70 65 6e 64 65 6e 74 20 6d 61 74 63 68 69 6e  ependent matchin
39c97 67 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  g..*/..#if !defi
39c98 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
39c99 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
39c9a 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 29 0a 0a  TE_ENABLE_ICU)..
39c9b 2f 2a 20 49 6e 63 6c 75 64 65 20 49 43 55 20 68  /* Include ICU h
39c9c 65 61 64 65 72 73 20 2a 2f 0a 23 69 6e 63 6c 75  eaders */.#inclu
39c9d 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 74 79 70  de <unicode/utyp
39c9e 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  es.h>.#include <
39c9f 75 6e 69 63 6f 64 65 2f 75 72 65 67 65 78 2e 68  unicode/uregex.h
39ca0 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63  >.#include <unic
39ca1 6f 64 65 2f 75 73 74 72 69 6e 67 2e 68 3e 0a 23  ode/ustring.h>.#
39ca2 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65  include <unicode
39ca3 2f 75 63 6f 6c 2e 68 3e 0a 0a 0a 23 69 66 6e 64  /ucol.h>...#ifnd
39ca4 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20  ef SQLITE_CORE. 
39ca5 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f   SQLITE_EXTENSIO
39ca6 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65 0a 23 65  N_INIT1.#else.#e
39ca7 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69  ndif../*.** Maxi
39ca8 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69 6e 20 62  mum length (in b
39ca9 79 74 65 73 29 20 6f 66 20 74 68 65 20 70 61 74  ytes) of the pat
39caa 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b 45 20 6f  tern in a LIKE o
39cab 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65 72 61 74  r GLOB.** operat
39cac 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  or..*/.#ifndef S
39cad 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
39cae 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0a 23 20  ATTERN_LENGTH.# 
39caf 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
39cb0 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
39cb1 45 4e 47 54 48 20 35 30 30 30 30 0a 23 65 6e 64  ENGTH 50000.#end
39cb2 69 66 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 73 69 6f  if../*.** Versio
39cb3 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 72 65  n of sqlite3_fre
39cb4 65 28 29 20 74 68 61 74 20 69 73 20 61 6c 77 61  e() that is alwa
39cb5 79 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 6e  ys a function, n
39cb6 65 76 65 72 20 61 20 6d 61 63 72 6f 2e 0a 2a 2f  ever a macro..*/
39cb7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 78 46 72  .static void xFr
39cb8 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73  ee(void *p){.  s
39cb9 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
39cba 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
39cbb 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69 6e   two UTF-8 strin
39cbc 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79 20  gs for equality 
39cbd 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 20  where the first 
39cbe 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 20 22  string is.** a "
39cbf 4c 49 4b 45 22 20 65 78 70 72 65 73 73 69 6f 6e  LIKE" expression
39cc0 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 31  . Return true (1
39cc1 29 20 69 66 20 74 68 65 79 20 61 72 65 20 74 68  ) if they are th
39cc2 65 20 73 61 6d 65 20 61 6e 64 20 0a 2a 2a 20 66  e same and .** f
39cc3 61 6c 73 65 20 28 30 29 20 69 66 20 74 68 65 79  alse (0) if they
39cc4 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0a   are different..
39cc5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63  */.static int ic
39cc6 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28 0a 20 20  uLikeCompare(.  
39cc7 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74 20 2a 7a  const uint8_t *z
39cc8 50 61 74 74 65 72 6e 2c 20 20 20 2f 2a 20 4c 49  Pattern,   /* LI
39cc9 4b 45 20 70 61 74 74 65 72 6e 20 2a 2f 0a 20 20  KE pattern */.  
39cca 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74 20 2a 7a  const uint8_t *z
39ccb 53 74 72 69 6e 67 2c 20 20 20 20 2f 2a 20 54 68  String,    /* Th
39ccc 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74  e UTF-8 string t
39ccd 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73  o compare agains
39cce 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 55 43 68  t */.  const UCh
39ccf 61 72 33 32 20 75 45 73 63 20 20 20 20 20 20 20  ar32 uEsc       
39cd0 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20    /* The escape 
39cd1 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 29 7b 0a  character */.){.
39cd2 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
39cd3 6e 74 20 4d 41 54 43 48 5f 4f 4e 45 20 3d 20 28  nt MATCH_ONE = (
39cd4 55 43 68 61 72 33 32 29 27 5f 27 3b 0a 20 20 73  UChar32)'_';.  s
39cd5 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20  tatic const int 
39cd6 4d 41 54 43 48 5f 41 4c 4c 20 3d 20 28 55 43 68  MATCH_ALL = (UCh
39cd7 61 72 33 32 29 27 25 27 3b 0a 0a 20 20 69 6e 74  ar32)'%';..  int
39cd8 20 69 50 61 74 74 65 72 6e 20 3d 20 30 3b 20 20   iPattern = 0;  
39cd9 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
39cda 62 79 74 65 20 69 6e 64 65 78 20 69 6e 20 7a 50  byte index in zP
39cdb 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  attern */.  int 
39cdc 69 53 74 72 69 6e 67 20 3d 20 30 3b 20 20 20 20  iString = 0;    
39cdd 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 62      /* Current b
39cde 79 74 65 20 69 6e 64 65 78 20 69 6e 20 7a 53 74  yte index in zSt
39cdf 72 69 6e 67 20 2a 2f 0a 0a 20 20 69 6e 74 20 70  ring */..  int p
39ce0 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 20 20  revEscape = 0;  
39ce1 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
39ce2 65 20 70 72 65 76 69 6f 75 73 20 63 68 61 72 61  e previous chara
39ce3 63 74 65 72 20 77 61 73 20 75 45 73 63 20 2a 2f  cter was uEsc */
39ce4 0a 0a 20 20 77 68 69 6c 65 28 20 7a 50 61 74 74  ..  while( zPatt
39ce5 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 21 3d 30  ern[iPattern]!=0
39ce6 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64   ){..    /* Read
39ce7 20 28 61 6e 64 20 63 6f 6e 73 75 6d 65 29 20 74   (and consume) t
39ce8 68 65 20 6e 65 78 74 20 63 68 61 72 61 63 74 65  he next characte
39ce9 72 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74  r from the input
39cea 20 70 61 74 74 65 72 6e 2e 20 2a 2f 0a 20 20 20   pattern. */.   
39ceb 20 55 43 68 61 72 33 32 20 75 50 61 74 74 65 72   UChar32 uPatter
39cec 6e 3b 0a 20 20 20 20 55 38 5f 4e 45 58 54 5f 55  n;.    U8_NEXT_U
39ced 4e 53 41 46 45 28 7a 50 61 74 74 65 72 6e 2c 20  NSAFE(zPattern, 
39cee 69 50 61 74 74 65 72 6e 2c 20 75 50 61 74 74 65  iPattern, uPatte
39cef 72 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  rn);.    assert(
39cf0 75 50 61 74 74 65 72 6e 21 3d 30 29 3b 0a 0a 20  uPattern!=0);.. 
39cf1 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20     /* There are 
39cf2 6e 6f 77 20 34 20 70 6f 73 73 69 62 69 6c 69 74  now 4 possibilit
39cf3 69 65 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ies:.    **.    
39cf4 2a 2a 20 20 20 20 20 31 2e 20 75 50 61 74 74 65  **     1. uPatte
39cf5 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61 70  rn is an unescap
39cf6 65 64 20 6d 61 74 63 68 2d 61 6c 6c 20 63 68 61  ed match-all cha
39cf7 72 61 63 74 65 72 20 22 25 22 2c 0a 20 20 20 20  racter "%",.    
39cf8 2a 2a 20 20 20 20 20 32 2e 20 75 50 61 74 74 65  **     2. uPatte
39cf9 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61 70  rn is an unescap
39cfa 65 64 20 6d 61 74 63 68 2d 6f 6e 65 20 63 68 61  ed match-one cha
39cfb 72 61 63 74 65 72 20 22 5f 22 2c 0a 20 20 20 20  racter "_",.    
39cfc 2a 2a 20 20 20 20 20 33 2e 20 75 50 61 74 74 65  **     3. uPatte
39cfd 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61 70  rn is an unescap
39cfe 65 64 20 65 73 63 61 70 65 20 63 68 61 72 61 63  ed escape charac
39cff 74 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 20  ter, or.    **  
39d00 20 20 20 34 2e 20 75 50 61 74 74 65 72 6e 20 69     4. uPattern i
39d01 73 20 74 6f 20 62 65 20 68 61 6e 64 6c 65 64 20  s to be handled 
39d02 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 63  as an ordinary c
39d03 68 61 72 61 63 74 65 72 0a 20 20 20 20 2a 2f 0a  haracter.    */.
39d04 20 20 20 20 69 66 28 20 21 70 72 65 76 45 73 63      if( !prevEsc
39d05 61 70 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d  ape && uPattern=
39d06 3d 4d 41 54 43 48 5f 41 4c 4c 20 29 7b 0a 20 20  =MATCH_ALL ){.  
39d07 20 20 20 20 2f 2a 20 43 61 73 65 20 31 2e 20 2a      /* Case 1. *
39d08 2f 0a 20 20 20 20 20 20 75 69 6e 74 38 5f 74 20  /.      uint8_t 
39d09 63 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 6b 69  c;..      /* Ski
39d0a 70 20 61 6e 79 20 4d 41 54 43 48 5f 41 4c 4c 20  p any MATCH_ALL 
39d0b 6f 72 20 4d 41 54 43 48 5f 4f 4e 45 20 63 68 61  or MATCH_ONE cha
39d0c 72 61 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c  racters that fol
39d0d 6c 6f 77 20 61 0a 20 20 20 20 20 20 2a 2a 20 4d  low a.      ** M
39d0e 41 54 43 48 5f 41 4c 4c 2e 20 46 6f 72 20 65 61  ATCH_ALL. For ea
39d0f 63 68 20 4d 41 54 43 48 5f 4f 4e 45 2c 20 73 6b  ch MATCH_ONE, sk
39d10 69 70 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  ip one character
39d11 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a   in the .      *
39d12 2a 20 74 65 73 74 20 73 74 72 69 6e 67 2e 0a 20  * test string.. 
39d13 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 77 68       */.      wh
39d14 69 6c 65 28 20 28 63 3d 7a 50 61 74 74 65 72 6e  ile( (c=zPattern
39d15 5b 69 50 61 74 74 65 72 6e 5d 29 20 3d 3d 20 4d  [iPattern]) == M
39d16 41 54 43 48 5f 41 4c 4c 20 7c 7c 20 63 20 3d 3d  ATCH_ALL || c ==
39d17 20 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20   MATCH_ONE ){.  
39d18 20 20 20 20 20 20 69 66 28 20 63 3d 3d 4d 41 54        if( c==MAT
39d19 43 48 5f 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  CH_ONE ){.      
39d1a 20 20 20 20 69 66 28 20 7a 53 74 72 69 6e 67 5b      if( zString[
39d1b 69 53 74 72 69 6e 67 5d 3d 3d 30 20 29 20 72 65  iString]==0 ) re
39d1c 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
39d1d 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46    U8_FWD_1_UNSAF
39d1e 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69  E(zString, iStri
39d1f 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ng);.        }. 
39d20 20 20 20 20 20 20 20 69 50 61 74 74 65 72 6e 2b         iPattern+
39d21 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  +;.      }..    
39d22 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e 5b 69    if( zPattern[i
39d23 50 61 74 74 65 72 6e 5d 3d 3d 30 20 29 20 72 65  Pattern]==0 ) re
39d24 74 75 72 6e 20 31 3b 0a 0a 20 20 20 20 20 20 77  turn 1;..      w
39d25 68 69 6c 65 28 20 7a 53 74 72 69 6e 67 5b 69 53  hile( zString[iS
39d26 74 72 69 6e 67 5d 20 29 7b 0a 20 20 20 20 20 20  tring] ){.      
39d27 20 20 69 66 28 20 69 63 75 4c 69 6b 65 43 6f 6d    if( icuLikeCom
39d28 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e 5b 69  pare(&zPattern[i
39d29 50 61 74 74 65 72 6e 5d 2c 20 26 7a 53 74 72 69  Pattern], &zStri
39d2a 6e 67 5b 69 53 74 72 69 6e 67 5d 2c 20 75 45 73  ng[iString], uEs
39d2b 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c) ){.          
39d2c 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
39d2d 20 20 7d 0a 20 20 20 20 20 20 20 20 55 38 5f 46    }.        U8_F
39d2e 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72  WD_1_UNSAFE(zStr
39d2f 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 20  ing, iString);. 
39d30 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
39d31 75 72 6e 20 30 3b 0a 0a 20 20 20 20 7d 65 6c 73  urn 0;..    }els
39d32 65 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70  e if( !prevEscap
39d33 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d 3d 4d  e && uPattern==M
39d34 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20 20 20  ATCH_ONE ){.    
39d35 20 20 2f 2a 20 43 61 73 65 20 32 2e 20 2a 2f 0a    /* Case 2. */.
39d36 20 20 20 20 20 20 69 66 28 20 7a 53 74 72 69 6e        if( zStrin
39d37 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30 20 29 20  g[iString]==0 ) 
39d38 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
39d39 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46 45 28  U8_FWD_1_UNSAFE(
39d3a 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67  zString, iString
39d3b 29 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  );..    }else if
39d3c 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
39d3d 20 75 50 61 74 74 65 72 6e 3d 3d 75 45 73 63 29   uPattern==uEsc)
39d3e 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
39d3f 33 2e 20 2a 2f 0a 20 20 20 20 20 20 70 72 65 76  3. */.      prev
39d40 45 73 63 61 70 65 20 3d 20 31 3b 0a 0a 20 20 20  Escape = 1;..   
39d41 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
39d42 20 43 61 73 65 20 34 2e 20 2a 2f 0a 20 20 20 20   Case 4. */.    
39d43 20 20 55 43 68 61 72 33 32 20 75 53 74 72 69 6e    UChar32 uStrin
39d44 67 3b 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54  g;.      U8_NEXT
39d45 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c  _UNSAFE(zString,
39d46 20 69 53 74 72 69 6e 67 2c 20 75 53 74 72 69 6e   iString, uStrin
39d47 67 29 3b 0a 20 20 20 20 20 20 75 53 74 72 69 6e  g);.      uStrin
39d48 67 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 75  g = u_foldCase(u
39d49 53 74 72 69 6e 67 2c 20 55 5f 46 4f 4c 44 5f 43  String, U_FOLD_C
39d4a 41 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a 20 20  ASE_DEFAULT);.  
39d4b 20 20 20 20 75 50 61 74 74 65 72 6e 20 3d 20 75      uPattern = u
39d4c 5f 66 6f 6c 64 43 61 73 65 28 75 50 61 74 74 65  _foldCase(uPatte
39d4d 72 6e 2c 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f  rn, U_FOLD_CASE_
39d4e 44 45 46 41 55 4c 54 29 3b 0a 20 20 20 20 20 20  DEFAULT);.      
39d4f 69 66 28 20 75 53 74 72 69 6e 67 21 3d 75 50 61  if( uString!=uPa
39d50 74 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20 20  ttern ){.       
39d51 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
39d52 20 7d 0a 20 20 20 20 20 20 70 72 65 76 45 73 63   }.      prevEsc
39d53 61 70 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ape = 0;.    }. 
39d54 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 7a 53 74   }..  return zSt
39d55 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30  ring[iString]==0
39d56 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
39d57 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
39d58 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63   like() SQL func
39d59 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63  tion.  This func
39d5a 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a  tion implements.
39d5b 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20  ** the build-in 
39d5c 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20  LIKE operator.  
39d5d 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
39d5e 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
39d5f 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 74  on is the.** pat
39d60 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63  tern and the sec
39d61 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
39d62 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c  the string.  So,
39d63 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
39d64 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  nts:.**.**      
39d65 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20   A LIKE B.**.** 
39d66 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  is implemented a
39d67 73 20 6c 69 6b 65 28 42 2c 20 41 29 2e 20 49 66  s like(B, A). If
39d68 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 73 63   there is an esc
39d69 61 70 65 20 63 68 61 72 61 63 74 65 72 20 45 2c  ape character E,
39d6a 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20   .**.**       A 
39d6b 4c 49 4b 45 20 42 20 45 53 43 41 50 45 20 45 0a  LIKE B ESCAPE E.
39d6c 2a 2a 0a 2a 2a 20 69 73 20 6d 61 70 70 65 64 20  **.** is mapped 
39d6d 74 6f 20 6c 69 6b 65 28 42 2c 20 41 2c 20 45 29  to like(B, A, E)
39d6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
39d6f 20 69 63 75 4c 69 6b 65 46 75 6e 63 28 0a 20 20   icuLikeFunc(.  
39d70 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
39d71 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74  *context, .  int
39d72 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65   argc, .  sqlite
39d73 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
39d74 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
39d75 65 64 20 63 68 61 72 20 2a 7a 41 20 3d 20 73 71  ed char *zA = sq
39d76 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
39d77 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e  (argv[0]);.  con
39d78 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
39d79 20 2a 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76   *zB = sqlite3_v
39d7a 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
39d7b 5d 29 3b 0a 20 20 55 43 68 61 72 33 32 20 75 45  ]);.  UChar32 uE
39d7c 73 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c 69  sc = 0;..  /* Li
39d7d 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  mit the length o
39d7e 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c  f the LIKE or GL
39d7f 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76  OB pattern to av
39d80 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a  oid problems.  *
39d81 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73  * of deep recurs
39d82 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61  ion and N*N beha
39d83 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43  vior in patternC
39d84 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20  ompare()..  */. 
39d85 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
39d86 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
39d87 29 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  )>SQLITE_MAX_LIK
39d88 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
39d89 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
39d8a 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
39d8b 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47  text, "LIKE or G
39d8c 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20  LOB pattern too 
39d8d 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20  complex", -1);. 
39d8e 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
39d8f 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29  .  if( argc==3 )
39d90 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63  {.    /* The esc
39d91 61 70 65 20 63 68 61 72 61 63 74 65 72 20 73 74  ape character st
39d92 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 73  ring must consis
39d93 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 54  t of a single UT
39d94 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20  F-8 character.. 
39d95 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c     ** Otherwise,
39d96 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
39d97 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
39d98 20 6e 45 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   nE= sqlite3_val
39d99 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d  ue_bytes(argv[2]
39d9a 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  );.    const uns
39d9b 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 20 3d  igned char *zE =
39d9c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
39d9d 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  ext(argv[2]);.  
39d9e 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 20    int i = 0;.   
39d9f 20 69 66 28 20 7a 45 3d 3d 30 20 29 20 72 65 74   if( zE==0 ) ret
39da0 75 72 6e 3b 0a 20 20 20 20 55 38 5f 4e 45 58 54  urn;.    U8_NEXT
39da1 28 7a 45 2c 20 69 2c 20 6e 45 2c 20 75 45 73 63  (zE, i, nE, uEsc
39da2 29 3b 0a 20 20 20 20 69 66 28 20 69 21 3d 6e 45  );.    if( i!=nE
39da3 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
39da4 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
39da5 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20  ntext, .        
39da6 20 20 22 45 53 43 41 50 45 20 65 78 70 72 65 73    "ESCAPE expres
39da7 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 20 73  sion must be a s
39da8 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 22  ingle character"
39da9 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74  , -1);.      ret
39daa 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  urn;.    }.  }..
39dab 20 20 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29    if( zA && zB )
39dac 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
39dad 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
39dae 2c 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65  , icuLikeCompare
39daf 28 7a 41 2c 20 7a 42 2c 20 75 45 73 63 29 29 3b  (zA, zB, uEsc));
39db0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
39db1 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
39db2 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 43  alled when an IC
39db3 55 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65  U function calle
39db4 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a  d from within.**
39db5 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
39db6 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 73 63  ion of an SQL sc
39db7 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 72 65  alar function re
39db8 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e 0a  turns an error..
39db9 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61 72  **.** The scalar
39dba 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78   function contex
39dbb 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  t passed as the 
39dbc 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
39dbd 73 20 0a 2a 2a 20 6c 6f 61 64 65 64 20 77 69 74  s .** loaded wit
39dbe 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  h an error messa
39dbf 67 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  ge based on the 
39dc0 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72  following two ar
39dc1 67 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  gs..*/.static vo
39dc2 69 64 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72  id icuFunctionEr
39dc3 72 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ror(.  sqlite3_c
39dc4 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20  ontext *pCtx,   
39dc5 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 73 63      /* SQLite sc
39dc6 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 63 6f  alar function co
39dc7 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ntext */.  const
39dc8 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
39dc9 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
39dca 6f 66 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20  of ICU function 
39dcb 74 68 61 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20  that failed */. 
39dcc 20 55 45 72 72 6f 72 43 6f 64 65 20 65 20 20 20   UErrorCode e   
39dcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39dce 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75   Error code retu
39dcf 72 6e 65 64 20 62 79 20 49 43 55 20 66 75 6e 63  rned by ICU func
39dd0 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61  tion */.){.  cha
39dd1 72 20 7a 42 75 66 5b 31 32 38 5d 3b 0a 20 20 73  r zBuf[128];.  s
39dd2 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
39dd3 31 32 38 2c 20 7a 42 75 66 2c 20 22 49 43 55 20  128, zBuf, "ICU 
39dd4 65 72 72 6f 72 3a 20 25 73 28 29 3a 20 25 73 22  error: %s(): %s"
39dd5 2c 20 7a 4e 61 6d 65 2c 20 75 5f 65 72 72 6f 72  , zName, u_error
39dd6 4e 61 6d 65 28 65 29 29 3b 0a 20 20 7a 42 75 66  Name(e));.  zBuf
39dd7 5b 31 32 37 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  [127] = '\0';.  
39dd8 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
39dd9 72 72 6f 72 28 70 43 74 78 2c 20 7a 42 75 66 2c  rror(pCtx, zBuf,
39dda 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46   -1);.}../*.** F
39ddb 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65 74  unction to delet
39ddc 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67 65 78  e compiled regex
39ddd 70 20 6f 62 6a 65 63 74 73 2e 20 52 65 67 69 73  p objects. Regis
39dde 74 65 72 65 64 20 61 73 0a 2a 2a 20 61 20 64 65  tered as.** a de
39ddf 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
39de0 6e 20 77 69 74 68 20 73 71 6c 69 74 65 33 5f 73  n with sqlite3_s
39de1 65 74 5f 61 75 78 64 61 74 61 28 29 2e 0a 2a 2f  et_auxdata()..*/
39de2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75  .static void icu
39de3 52 65 67 65 78 70 44 65 6c 65 74 65 28 76 6f 69  RegexpDelete(voi
39de4 64 20 2a 70 29 7b 0a 20 20 55 52 65 67 75 6c 61  d *p){.  URegula
39de5 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 70 45 78  rExpression *pEx
39de6 70 72 20 3d 20 28 55 52 65 67 75 6c 61 72 45 78  pr = (URegularEx
39de7 70 72 65 73 73 69 6f 6e 20 2a 29 70 3b 0a 20 20  pression *)p;.  
39de8 75 72 65 67 65 78 5f 63 6c 6f 73 65 28 70 45 78  uregex_close(pEx
39de9 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  pr);.}../*.** Im
39dea 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
39deb 53 51 4c 69 74 65 20 52 45 47 45 58 50 20 6f 70  SQLite REGEXP op
39dec 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 63 61  erator. This sca
39ded 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  lar function tak
39dee 65 73 0a 2a 2a 20 74 77 6f 20 61 72 67 75 6d 65  es.** two argume
39def 6e 74 73 2e 20 54 68 65 20 66 69 72 73 74 20 69  nts. The first i
39df0 73 20 61 20 72 65 67 75 6c 61 72 20 65 78 70 72  s a regular expr
39df1 65 73 73 69 6f 6e 20 70 61 74 74 65 72 6e 20 74  ession pattern t
39df2 6f 20 63 6f 6d 70 69 6c 65 0a 2a 2a 20 74 68 65  o compile.** the
39df3 20 73 65 63 6f 6e 64 20 69 73 20 61 20 73 74 72   second is a str
39df4 69 6e 67 20 74 6f 20 6d 61 74 63 68 20 61 67 61  ing to match aga
39df5 69 6e 73 74 20 74 68 61 74 20 70 61 74 74 65 72  inst that patter
39df6 6e 2e 20 49 66 20 65 69 74 68 65 72 20 0a 2a 2a  n. If either .**
39df7 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
39df8 53 51 4c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 4e  SQL NULL, then N
39df9 55 4c 4c 20 49 73 20 72 65 74 75 72 6e 65 64 2e  ULL Is returned.
39dfa 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
39dfb 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 31 20 69  result.** is 1 i
39dfc 66 20 74 68 65 20 73 74 72 69 6e 67 20 6d 61 74  f the string mat
39dfd 63 68 65 73 20 74 68 65 20 70 61 74 74 65 72 6e  ches the pattern
39dfe 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65  , or 0 otherwise
39dff 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d  ..**.** SQLite m
39e00 61 70 73 20 74 68 65 20 72 65 67 65 78 70 28 29  aps the regexp()
39e01 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65   function to the
39e02 20 72 65 67 65 78 70 28 29 20 6f 70 65 72 61 74   regexp() operat
39e03 6f 72 20 73 75 63 68 0a 2a 2a 20 74 68 61 74 20  or such.** that 
39e04 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
39e05 6f 20 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74  o are equivalent
39e06 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 7a 53 74 72  :.**.**     zStr
39e07 69 6e 67 20 52 45 47 45 58 50 20 7a 50 61 74 74  ing REGEXP zPatt
39e08 65 72 6e 0a 2a 2a 20 20 20 20 20 72 65 67 65 78  ern.**     regex
39e09 70 28 7a 50 61 74 74 65 72 6e 2c 20 7a 53 74 72  p(zPattern, zStr
39e0a 69 6e 67 29 0a 2a 2a 0a 2a 2a 20 55 73 65 73 20  ing).**.** Uses 
39e0b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 49 43  the following IC
39e0c 55 20 72 65 67 65 78 70 20 41 50 49 73 3a 0a 2a  U regexp APIs:.*
39e0d 2a 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f  *.**     uregex_
39e0e 6f 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 75 72  open().**     ur
39e0f 65 67 65 78 5f 6d 61 74 63 68 65 73 28 29 0a 2a  egex_matches().*
39e10 2a 20 20 20 20 20 75 72 65 67 65 78 5f 63 6c 6f  *     uregex_clo
39e11 73 65 28 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76  se().*/.static v
39e12 6f 69 64 20 69 63 75 52 65 67 65 78 70 46 75 6e  oid icuRegexpFun
39e13 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
39e14 74 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20  t *p, int nArg, 
39e15 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
39e16 61 70 41 72 67 29 7b 0a 20 20 55 45 72 72 6f 72  apArg){.  UError
39e17 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f  Code status = U_
39e18 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 52  ZERO_ERROR;.  UR
39e19 65 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e  egularExpression
39e1a 20 2a 70 45 78 70 72 3b 0a 20 20 55 42 6f 6f 6c   *pExpr;.  UBool
39e1b 20 72 65 73 3b 0a 20 20 63 6f 6e 73 74 20 55 43   res;.  const UC
39e1c 68 61 72 20 2a 7a 53 74 72 69 6e 67 20 3d 20 73  har *zString = s
39e1d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
39e1e 74 31 36 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a  t16(apArg[1]);..
39e1f 20 20 2f 2a 20 49 66 20 74 68 65 20 6c 65 66 74    /* If the left
39e20 20 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68   hand side of th
39e21 65 20 72 65 67 65 78 70 20 6f 70 65 72 61 74 6f  e regexp operato
39e22 72 20 69 73 20 4e 55 4c 4c 2c 20 0a 20 20 2a 2a  r is NULL, .  **
39e23 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
39e24 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 0a   is also NULL. .
39e25 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 53 74 72    */.  if( !zStr
39e26 69 6e 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ing ){.    retur
39e27 6e 3b 0a 20 20 7d 0a 0a 20 20 70 45 78 70 72 20  n;.  }..  pExpr 
39e28 3d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  = sqlite3_get_au
39e29 78 64 61 74 61 28 70 2c 20 30 29 3b 0a 20 20 69  xdata(p, 0);.  i
39e2a 66 28 20 21 70 45 78 70 72 20 29 7b 0a 20 20 20  f( !pExpr ){.   
39e2b 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a 50   const UChar *zP
39e2c 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33  attern = sqlite3
39e2d 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61 70  _value_text16(ap
39e2e 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  Arg[0]);.    if(
39e2f 20 21 7a 50 61 74 74 65 72 6e 20 29 7b 0a 20 20   !zPattern ){.  
39e30 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
39e31 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 75 72  }.    pExpr = ur
39e32 65 67 65 78 5f 6f 70 65 6e 28 7a 50 61 74 74 65  egex_open(zPatte
39e33 72 6e 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 26 73  rn, -1, 0, 0, &s
39e34 74 61 74 75 73 29 3b 0a 0a 20 20 20 20 69 66 28  tatus);..    if(
39e35 20 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75   U_SUCCESS(statu
39e36 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  s) ){.      sqli
39e37 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28  te3_set_auxdata(
39e38 70 2c 20 30 2c 20 70 45 78 70 72 2c 20 69 63 75  p, 0, pExpr, icu
39e39 52 65 67 65 78 70 44 65 6c 65 74 65 29 3b 0a 20  RegexpDelete);. 
39e3a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
39e3b 61 73 73 65 72 74 28 21 70 45 78 70 72 29 3b 0a  assert(!pExpr);.
39e3c 20 20 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f        icuFunctio
39e3d 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65 67 65  nError(p, "urege
39e3e 78 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73 29  x_open", status)
39e3f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
39e40 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
39e41 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 74 65  Configure the te
39e42 78 74 20 74 68 61 74 20 74 68 65 20 72 65 67 75  xt that the regu
39e43 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  lar expression o
39e44 70 65 72 61 74 65 73 20 6f 6e 2e 20 2a 2f 0a 20  perates on. */. 
39e45 20 75 72 65 67 65 78 5f 73 65 74 54 65 78 74 28   uregex_setText(
39e46 70 45 78 70 72 2c 20 7a 53 74 72 69 6e 67 2c 20  pExpr, zString, 
39e47 2d 31 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20  -1, &status);.  
39e48 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73  if( !U_SUCCESS(s
39e49 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63  tatus) ){.    ic
39e4a 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70  uFunctionError(p
39e4b 2c 20 22 75 72 65 67 65 78 5f 73 65 74 54 65 78  , "uregex_setTex
39e4c 74 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  t", status);.   
39e4d 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
39e4e 2f 2a 20 41 74 74 65 6d 70 74 20 74 68 65 20 6d  /* Attempt the m
39e4f 61 74 63 68 20 2a 2f 0a 20 20 72 65 73 20 3d 20  atch */.  res = 
39e50 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73 28 70  uregex_matches(p
39e51 45 78 70 72 2c 20 30 2c 20 26 73 74 61 74 75 73  Expr, 0, &status
39e52 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43  );.  if( !U_SUCC
39e53 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20  ESS(status) ){. 
39e54 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72     icuFunctionEr
39e55 72 6f 72 28 70 2c 20 22 75 72 65 67 65 78 5f 6d  ror(p, "uregex_m
39e56 61 74 63 68 65 73 22 2c 20 73 74 61 74 75 73 29  atches", status)
39e57 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
39e58 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  }..  /* Set the 
39e59 74 65 78 74 20 74 68 61 74 20 74 68 65 20 72 65  text that the re
39e5a 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
39e5b 20 6f 70 65 72 61 74 65 73 20 6f 6e 20 74 6f 20   operates on to 
39e5c 61 20 4e 55 4c 4c 0a 20 20 2a 2a 20 70 6f 69 6e  a NULL.  ** poin
39e5d 74 65 72 2e 20 54 68 69 73 20 69 73 20 6e 6f 74  ter. This is not
39e5e 20 72 65 61 6c 6c 79 20 6e 65 63 65 73 73 61 72   really necessar
39e5f 79 2c 20 62 75 74 20 69 74 20 69 73 20 74 69 64  y, but it is tid
39e60 69 65 72 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6c  ier than .  ** l
39e61 65 61 76 69 6e 67 20 74 68 65 20 72 65 67 75 6c  eaving the regul
39e62 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 62  ar expression ob
39e63 6a 65 63 74 20 63 6f 6e 66 69 67 75 72 65 64 20  ject configured 
39e64 77 69 74 68 20 61 6e 20 69 6e 76 61 6c 69 64 0a  with an invalid.
39e65 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 61 66 74    ** pointer aft
39e66 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  er this function
39e67 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20   returns..  */. 
39e68 20 75 72 65 67 65 78 5f 73 65 74 54 65 78 74 28   uregex_setText(
39e69 70 45 78 70 72 2c 20 30 2c 20 30 2c 20 26 73 74  pExpr, 0, 0, &st
39e6a 61 74 75 73 29 3b 0a 0a 20 20 2f 2a 20 52 65 74  atus);..  /* Ret
39e6b 75 72 6e 20 31 20 6f 72 20 30 2e 20 2a 2f 0a 20  urn 1 or 0. */. 
39e6c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
39e6d 69 6e 74 28 70 2c 20 72 65 73 20 3f 20 31 20 3a  int(p, res ? 1 :
39e6e 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d   0);.}../*.** Im
39e6f 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
39e70 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
39e71 73 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70 69  s for case mappi
39e72 6e 67 20 2d 20 75 70 70 65 72 28 29 20 61 6e 64  ng - upper() and
39e73 20 0a 2a 2a 20 6c 6f 77 65 72 28 29 2e 20 46 75   .** lower(). Fu
39e74 6e 63 74 69 6f 6e 20 75 70 70 65 72 28 29 20 63  nction upper() c
39e75 6f 6e 76 65 72 74 73 20 69 74 73 20 69 6e 70 75  onverts its inpu
39e76 74 20 74 6f 20 75 70 70 65 72 2d 63 61 73 65 20  t to upper-case 
39e77 28 41 42 43 29 2e 0a 2a 2a 20 46 75 6e 63 74 69  (ABC)..** Functi
39e78 6f 6e 20 6c 6f 77 65 72 28 29 20 63 6f 6e 76 65  on lower() conve
39e79 72 74 73 20 74 6f 20 6c 6f 77 65 72 2d 63 61 73  rts to lower-cas
39e7a 65 20 28 61 62 63 29 2e 0a 2a 2a 0a 2a 2a 20 49  e (abc)..**.** I
39e7b 43 55 20 70 72 6f 76 69 64 65 73 20 74 77 6f 20  CU provides two 
39e7c 74 79 70 65 73 20 6f 66 20 63 61 73 65 20 6d 61  types of case ma
39e7d 70 70 69 6e 67 2c 20 22 67 65 6e 65 72 61 6c 22  pping, "general"
39e7e 20 63 61 73 65 20 6d 61 70 70 69 6e 67 20 61 6e   case mapping an
39e7f 64 0a 2a 2a 20 22 6c 61 6e 67 75 61 67 65 20 73  d.** "language s
39e80 70 65 63 69 66 69 63 22 2e 20 52 65 66 65 72 20  pecific". Refer 
39e81 74 6f 20 49 43 55 20 64 6f 63 75 6d 65 6e 74 61  to ICU documenta
39e82 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 64 69 66  tion for the dif
39e83 66 65 72 65 6e 63 65 73 0a 2a 2a 20 62 65 74 77  ferences.** betw
39e84 65 65 6e 20 74 68 65 20 74 77 6f 2e 0a 2a 2a 0a  een the two..**.
39e85 2a 2a 20 54 6f 20 75 74 69 6c 69 73 65 20 22 67  ** To utilise "g
39e86 65 6e 65 72 61 6c 22 20 63 61 73 65 20 6d 61 70  eneral" case map
39e87 70 69 6e 67 2c 20 74 68 65 20 75 70 70 65 72 28  ping, the upper(
39e88 29 20 6f 72 20 6c 6f 77 65 72 28 29 20 73 63 61  ) or lower() sca
39e89 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  lar .** function
39e8a 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 77 69  s are invoked wi
39e8b 74 68 20 6f 6e 65 20 61 72 67 75 6d 65 6e 74 3a  th one argument:
39e8c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75 70 70 65 72  .**.**     upper
39e8d 28 27 41 42 43 27 29 20 2d 3e 20 27 61 62 63 27  ('ABC') -> 'abc'
39e8e 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27 61  .**     lower('a
39e8f 62 63 27 29 20 2d 3e 20 27 41 42 43 27 0a 2a 2a  bc') -> 'ABC'.**
39e90 0a 2a 2a 20 54 6f 20 61 63 63 65 73 73 20 49 43  .** To access IC
39e91 55 20 22 6c 61 6e 67 75 61 67 65 20 73 70 65 63  U "language spec
39e92 69 66 69 63 22 20 63 61 73 65 20 6d 61 70 70 69  ific" case mappi
39e93 6e 67 2c 20 75 70 70 65 72 28 29 20 6f 72 20 6c  ng, upper() or l
39e94 6f 77 65 72 28 29 0a 2a 2a 20 73 68 6f 75 6c 64  ower().** should
39e95 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68   be invoked with
39e96 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20   two arguments. 
39e97 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
39e98 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d 65 0a  ent is the name.
39e99 2a 2a 20 6f 66 20 74 68 65 20 6c 6f 63 61 6c 65  ** of the locale
39e9a 20 74 6f 20 75 73 65 2e 20 50 61 73 73 69 6e 67   to use. Passing
39e9b 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67   an empty string
39e9c 20 28 22 22 29 20 6f 72 20 53 51 4c 20 4e 55 4c   ("") or SQL NUL
39e9d 4c 20 76 61 6c 75 65 0a 2a 2a 20 61 73 20 74 68  L value.** as th
39e9e 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
39e9f 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  t is the same as
39ea0 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 31 20   invoking the 1 
39ea1 61 72 67 75 6d 65 6e 74 20 76 65 72 73 69 6f 6e  argument version
39ea2 0a 2a 2a 20 6f 66 20 75 70 70 65 72 28 29 20 6f  .** of upper() o
39ea3 72 20 6c 6f 77 65 72 28 29 2e 0a 2a 2a 0a 2a 2a  r lower()..**.**
39ea4 20 20 20 20 20 6c 6f 77 65 72 28 27 49 27 2c 20       lower('I', 
39ea5 27 65 6e 5f 75 73 27 29 20 2d 3e 20 27 69 27 0a  'en_us') -> 'i'.
39ea6 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27 49 27  **     lower('I'
39ea7 2c 20 27 74 72 5f 74 72 27 29 20 2d 3e 20 27 c4  , 'tr_tr') -> '.
39ea8 b1 27 20 28 73 6d 61 6c 6c 20 64 6f 74 6c 65 73  .' (small dotles
39ea9 73 20 69 29 0a 2a 2a 0a 2a 2a 20 68 74 74 70 3a  s i).**.** http:
39eaa 2f 2f 77 77 77 2e 69 63 75 2d 70 72 6f 6a 65 63  //www.icu-projec
39eab 74 2e 6f 72 67 2f 75 73 65 72 67 75 69 64 65 2f  t.org/userguide/
39eac 70 6f 73 69 78 2e 68 74 6d 6c 23 63 61 73 65 5f  posix.html#case_
39ead 6d 61 70 70 69 6e 67 73 0a 2a 2f 0a 73 74 61 74  mappings.*/.stat
39eae 69 63 20 76 6f 69 64 20 69 63 75 43 61 73 65 46  ic void icuCaseF
39eaf 75 6e 63 31 36 28 73 71 6c 69 74 65 33 5f 63 6f  unc16(sqlite3_co
39eb0 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 41  ntext *p, int nA
39eb1 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  rg, sqlite3_valu
39eb2 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 63 6f  e **apArg){.  co
39eb3 6e 73 74 20 55 43 68 61 72 20 2a 7a 49 6e 70 75  nst UChar *zInpu
39eb4 74 3b 0a 20 20 55 43 68 61 72 20 2a 7a 4f 75 74  t;.  UChar *zOut
39eb5 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49 6e 70 75  put;.  int nInpu
39eb6 74 3b 0a 20 20 69 6e 74 20 6e 4f 75 74 70 75 74  t;.  int nOutput
39eb7 3b 0a 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20  ;..  UErrorCode 
39eb8 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f  status = U_ZERO_
39eb9 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73 74 20 63  ERROR;.  const c
39eba 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 20 3d 20 30  har *zLocale = 0
39ebb 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 41 72 67  ;..  assert(nArg
39ebc 3d 3d 31 20 7c 7c 20 6e 41 72 67 3d 3d 32 29 3b  ==1 || nArg==2);
39ebd 0a 20 20 69 66 28 20 6e 41 72 67 3d 3d 32 20 29  .  if( nArg==2 )
39ebe 7b 0a 20 20 20 20 7a 4c 6f 63 61 6c 65 20 3d 20  {.    zLocale = 
39ebf 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
39ec0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
39ec1 28 61 70 41 72 67 5b 31 5d 29 3b 0a 20 20 7d 0a  (apArg[1]);.  }.
39ec2 0a 20 20 7a 49 6e 70 75 74 20 3d 20 73 71 6c 69  .  zInput = sqli
39ec3 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
39ec4 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 69 66  (apArg[0]);.  if
39ec5 28 20 21 7a 49 6e 70 75 74 20 29 7b 0a 20 20 20  ( !zInput ){.   
39ec6 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e   return;.  }.  n
39ec7 49 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f  Input = sqlite3_
39ec8 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 61 70  value_bytes16(ap
39ec9 41 72 67 5b 30 5d 29 3b 0a 0a 20 20 6e 4f 75 74  Arg[0]);..  nOut
39eca 70 75 74 20 3d 20 6e 49 6e 70 75 74 20 2a 20 32  put = nInput * 2
39ecb 20 2b 20 32 3b 0a 20 20 7a 4f 75 74 70 75 74 20   + 2;.  zOutput 
39ecc 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
39ecd 28 6e 4f 75 74 70 75 74 29 3b 0a 20 20 69 66 28  (nOutput);.  if(
39ece 20 21 7a 4f 75 74 70 75 74 20 29 7b 0a 20 20 20   !zOutput ){.   
39ecf 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
39ed0 69 66 28 20 73 71 6c 69 74 65 33 5f 75 73 65 72  if( sqlite3_user
39ed1 5f 64 61 74 61 28 70 29 20 29 7b 0a 20 20 20 20  _data(p) ){.    
39ed2 75 5f 73 74 72 54 6f 55 70 70 65 72 28 7a 4f 75  u_strToUpper(zOu
39ed3 74 70 75 74 2c 20 6e 4f 75 74 70 75 74 2f 32 2c  tput, nOutput/2,
39ed4 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2f   zInput, nInput/
39ed5 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61  2, zLocale, &sta
39ed6 74 75 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tus);.  }else{. 
39ed7 20 20 20 75 5f 73 74 72 54 6f 4c 6f 77 65 72 28     u_strToLower(
39ed8 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74 70 75 74  zOutput, nOutput
39ed9 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70  /2, zInput, nInp
39eda 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20 26  ut/2, zLocale, &
39edb 73 74 61 74 75 73 29 3b 0a 20 20 7d 0a 0a 20 20  status);.  }..  
39edc 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73  if( !U_SUCCESS(s
39edd 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63  tatus) ){.    ic
39ede 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70  uFunctionError(p
39edf 2c 20 22 75 5f 73 74 72 54 6f 4c 6f 77 65 72 28  , "u_strToLower(
39ee0 29 2f 75 5f 73 74 72 54 6f 55 70 70 65 72 22 2c  )/u_strToUpper",
39ee1 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65   status);.    re
39ee2 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  turn;.  }..  sql
39ee3 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
39ee4 31 36 28 70 2c 20 7a 4f 75 74 70 75 74 2c 20 2d  16(p, zOutput, -
39ee5 31 2c 20 78 46 72 65 65 29 3b 0a 7d 0a 0a 2f 2a  1, xFree);.}../*
39ee6 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65  .** Collation se
39ee7 71 75 65 6e 63 65 20 64 65 73 74 72 75 63 74 6f  quence destructo
39ee8 72 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  r function. The 
39ee9 70 43 74 78 20 61 72 67 75 6d 65 6e 74 20 70 6f  pCtx argument po
39eea 69 6e 74 73 20 74 6f 0a 2a 2a 20 61 20 55 43 6f  ints to.** a UCo
39eeb 6c 6c 61 74 6f 72 20 73 74 72 75 63 74 75 72 65  llator structure
39eec 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
39eed 63 61 74 65 64 20 75 73 69 6e 67 20 75 63 6f 6c  cated using ucol
39eee 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74  _open()..*/.stat
39eef 69 63 20 76 6f 69 64 20 69 63 75 43 6f 6c 6c 61  ic void icuColla
39ef0 74 69 6f 6e 44 65 6c 28 76 6f 69 64 20 2a 70 43  tionDel(void *pC
39ef1 74 78 29 7b 0a 20 20 55 43 6f 6c 6c 61 74 6f 72  tx){.  UCollator
39ef2 20 2a 70 20 3d 20 28 55 43 6f 6c 6c 61 74 6f 72   *p = (UCollator
39ef3 20 2a 29 70 43 74 78 3b 0a 20 20 75 63 6f 6c 5f   *)pCtx;.  ucol_
39ef4 63 6c 6f 73 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  close(p);.}../*.
39ef5 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ** Collation seq
39ef6 75 65 6e 63 65 20 63 6f 6d 70 61 72 69 73 6f 6e  uence comparison
39ef7 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 70   function. The p
39ef8 43 74 78 20 61 72 67 75 6d 65 6e 74 20 70 6f 69  Ctx argument poi
39ef9 6e 74 73 20 74 6f 0a 2a 2a 20 61 20 55 43 6f 6c  nts to.** a UCol
39efa 6c 61 74 6f 72 20 73 74 72 75 63 74 75 72 65 20  lator structure 
39efb 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63  previously alloc
39efc 61 74 65 64 20 75 73 69 6e 67 20 75 63 6f 6c 5f  ated using ucol_
39efd 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  open()..*/.stati
39efe 63 20 69 6e 74 20 69 63 75 43 6f 6c 6c 61 74 69  c int icuCollati
39eff 6f 6e 43 6f 6c 6c 28 0a 20 20 76 6f 69 64 20 2a  onColl(.  void *
39f00 70 43 74 78 2c 0a 20 20 69 6e 74 20 6e 4c 65 66  pCtx,.  int nLef
39f01 74 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  t,.  const void 
39f02 2a 7a 4c 65 66 74 2c 0a 20 20 69 6e 74 20 6e 52  *zLeft,.  int nR
39f03 69 67 68 74 2c 0a 20 20 63 6f 6e 73 74 20 76 6f  ight,.  const vo
39f04 69 64 20 2a 7a 52 69 67 68 74 0a 29 7b 0a 20 20  id *zRight.){.  
39f05 55 43 6f 6c 6c 61 74 69 6f 6e 52 65 73 75 6c 74  UCollationResult
39f06 20 72 65 73 3b 0a 20 20 55 43 6f 6c 6c 61 74 6f   res;.  UCollato
39f07 72 20 2a 70 20 3d 20 28 55 43 6f 6c 6c 61 74 6f  r *p = (UCollato
39f08 72 20 2a 29 70 43 74 78 3b 0a 20 20 72 65 73 20  r *)pCtx;.  res 
39f09 3d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28 70  = ucol_strcoll(p
39f0a 2c 20 28 55 43 68 61 72 20 2a 29 7a 4c 65 66 74  , (UChar *)zLeft
39f0b 2c 20 6e 4c 65 66 74 2f 32 2c 20 28 55 43 68 61  , nLeft/2, (UCha
39f0c 72 20 2a 29 7a 52 69 67 68 74 2c 20 6e 52 69 67  r *)zRight, nRig
39f0d 68 74 2f 32 29 3b 0a 20 20 73 77 69 74 63 68 28  ht/2);.  switch(
39f0e 20 72 65 73 20 29 7b 0a 20 20 20 20 63 61 73 65   res ){.    case
39f0f 20 55 43 4f 4c 5f 4c 45 53 53 3a 20 20 20 20 72   UCOL_LESS:    r
39f10 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 63 61  eturn -1;.    ca
39f11 73 65 20 55 43 4f 4c 5f 47 52 45 41 54 45 52 3a  se UCOL_GREATER:
39f12 20 72 65 74 75 72 6e 20 2b 31 3b 0a 20 20 20 20   return +1;.    
39f13 63 61 73 65 20 55 43 4f 4c 5f 45 51 55 41 4c 3a  case UCOL_EQUAL:
39f14 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
39f15 0a 20 20 61 73 73 65 72 74 28 21 22 55 6e 65 78  .  assert(!"Unex
39f16 70 65 63 74 65 64 20 72 65 74 75 72 6e 20 76 61  pected return va
39f17 6c 75 65 20 66 72 6f 6d 20 75 63 6f 6c 5f 73 74  lue from ucol_st
39f18 72 63 6f 6c 6c 28 29 22 29 3b 0a 20 20 72 65 74  rcoll()");.  ret
39f19 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
39f1a 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
39f1b 66 20 74 68 65 20 73 63 61 6c 61 72 20 66 75 6e  f the scalar fun
39f1c 63 74 69 6f 6e 20 69 63 75 5f 6c 6f 61 64 5f 63  ction icu_load_c
39f1d 6f 6c 6c 61 74 69 6f 6e 28 29 2e 0a 2a 2a 0a 2a  ollation()..**.*
39f1e 2a 20 54 68 69 73 20 73 63 61 6c 61 72 20 66 75  * This scalar fu
39f1f 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
39f20 6f 20 61 64 64 20 49 43 55 20 63 6f 6c 6c 61 74  o add ICU collat
39f21 69 6f 6e 20 62 61 73 65 64 20 63 6f 6c 6c 61 74  ion based collat
39f22 69 6f 6e 20 0a 2a 2a 20 74 79 70 65 73 20 74 6f  ion .** types to
39f23 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62   an SQLite datab
39f24 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ase connection. 
39f25 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  It is intended t
39f26 6f 20 62 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 61  o be called.** a
39f27 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
39f28 20 20 20 20 20 53 45 4c 45 43 54 20 69 63 75 5f       SELECT icu_
39f29 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28 3c  load_collation(<
39f2a 6c 6f 63 61 6c 65 3e 2c 20 3c 63 6f 6c 6c 61 74  locale>, <collat
39f2b 69 6f 6e 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a 0a 2a  ion-name>);.**.*
39f2c 2a 20 57 68 65 72 65 20 3c 6c 6f 63 61 6c 65 3e  * Where <locale>
39f2d 20 69 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6e   is a string con
39f2e 74 61 69 6e 69 6e 67 20 61 6e 20 49 43 55 20 6c  taining an ICU l
39f2f 6f 63 61 6c 65 20 69 64 65 6e 74 69 66 69 65 72  ocale identifier
39f30 20 28 69 2e 65 2e 0a 2a 2a 20 22 65 6e 5f 41 55   (i.e..** "en_AU
39f31 22 2c 20 22 74 72 5f 54 52 22 20 65 74 63 2e 29  ", "tr_TR" etc.)
39f32 20 61 6e 64 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d   and <collation-
39f33 6e 61 6d 65 3e 20 69 73 20 74 68 65 20 6e 61 6d  name> is the nam
39f34 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c  e of the.** coll
39f35 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74  ation sequence t
39f36 6f 20 63 72 65 61 74 65 2e 0a 2a 2f 0a 73 74 61  o create..*/.sta
39f37 74 69 63 20 76 6f 69 64 20 69 63 75 4c 6f 61 64  tic void icuLoad
39f38 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c  Collation(.  sql
39f39 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c  ite3_context *p,
39f3a 20 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 0a 20   .  int nArg, . 
39f3b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
39f3c 2a 61 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c 69  *apArg.){.  sqli
39f3d 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74  te3 *db = (sqlit
39f3e 65 33 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65  e3 *)sqlite3_use
39f3f 72 5f 64 61 74 61 28 70 29 3b 0a 20 20 55 45 72  r_data(p);.  UEr
39f40 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d  rorCode status =
39f41 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20   U_ZERO_ERROR;. 
39f42 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f   const char *zLo
39f43 63 61 6c 65 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  cale;      /* Lo
39f44 63 61 6c 65 20 69 64 65 6e 74 69 66 69 65 72 20  cale identifier 
39f45 2d 20 28 65 67 2e 20 22 6a 70 5f 4a 50 22 29 20  - (eg. "jp_JP") 
39f46 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
39f47 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 2f  *zName;        /
39f48 2a 20 53 51 4c 20 43 6f 6c 6c 61 74 69 6f 6e 20  * SQL Collation 
39f49 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 28 65  sequence name (e
39f4a 67 2e 20 22 6a 61 70 61 6e 65 73 65 22 29 20 2a  g. "japanese") *
39f4b 2f 0a 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70  /.  UCollator *p
39f4c 55 43 6f 6c 6c 61 74 6f 72 3b 20 20 20 20 2f 2a  UCollator;    /*
39f4d 20 49 43 55 20 6c 69 62 72 61 72 79 20 63 6f 6c   ICU library col
39f4e 6c 61 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f  lation object */
39f4f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
39f50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39f51 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
39f52 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
39f53 63 6f 6c 6c 61 74 69 6f 6e 5f 78 28 29 20 2a 2f  collation_x() */
39f54 0a 0a 20 20 61 73 73 65 72 74 28 6e 41 72 67 3d  ..  assert(nArg=
39f55 3d 32 29 3b 0a 20 20 7a 4c 6f 63 61 6c 65 20 3d  =2);.  zLocale =
39f56 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
39f57 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
39f58 74 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 7a  t(apArg[0]);.  z
39f59 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  Name = (const ch
39f5a 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
39f5b 75 65 5f 74 65 78 74 28 61 70 41 72 67 5b 31 5d  ue_text(apArg[1]
39f5c 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4c 6f 63 61  );..  if( !zLoca
39f5d 6c 65 20 7c 7c 20 21 7a 4e 61 6d 65 20 29 7b 0a  le || !zName ){.
39f5e 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
39f5f 0a 20 20 70 55 43 6f 6c 6c 61 74 6f 72 20 3d 20  .  pUCollator = 
39f60 75 63 6f 6c 5f 6f 70 65 6e 28 7a 4c 6f 63 61 6c  ucol_open(zLocal
39f61 65 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69  e, &status);.  i
39f62 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73 74  f( !U_SUCCESS(st
39f63 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63 75  atus) ){.    icu
39f64 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c  FunctionError(p,
39f65 20 22 75 63 6f 6c 5f 6f 70 65 6e 22 2c 20 73 74   "ucol_open", st
39f66 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72  atus);.    retur
39f67 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
39f68 70 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  p);..  rc = sqli
39f69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
39f6a 74 69 6f 6e 5f 76 32 28 64 62 2c 20 7a 4e 61 6d  tion_v2(db, zNam
39f6b 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c  e, SQLITE_UTF16,
39f6c 20 28 76 6f 69 64 20 2a 29 70 55 43 6f 6c 6c 61   (void *)pUColla
39f6d 74 6f 72 2c 20 0a 20 20 20 20 20 20 69 63 75 43  tor, .      icuC
39f6e 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c 2c 20 69 63  ollationColl, ic
39f6f 75 43 6f 6c 6c 61 74 69 6f 6e 44 65 6c 0a 20 20  uCollationDel.  
39f70 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
39f71 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 63  ITE_OK ){.    uc
39f72 6f 6c 5f 63 6c 6f 73 65 28 70 55 43 6f 6c 6c 61  ol_close(pUColla
39f73 74 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tor);.    sqlite
39f74 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
39f75 2c 20 22 45 72 72 6f 72 20 72 65 67 69 73 74 65  , "Error registe
39f76 72 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 66  ring collation f
39f77 75 6e 63 74 69 6f 6e 22 2c 20 2d 31 29 3b 0a 20  unction", -1);. 
39f78 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69   }.}../*.** Regi
39f79 73 74 65 72 20 74 68 65 20 49 43 55 20 65 78 74  ster the ICU ext
39f7a 65 6e 73 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  ension functions
39f7b 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20 64   with database d
39f7c 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  b..*/.SQLITE_PRI
39f7d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
39f7e 49 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33 20  IcuInit(sqlite3 
39f7f 2a 64 62 29 7b 0a 20 20 73 74 72 75 63 74 20 49  *db){.  struct I
39f80 63 75 53 63 61 6c 61 72 20 7b 0a 20 20 20 20 63  cuScalar {.    c
39f81 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
39f82 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
39f83 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63           /* Func
39f84 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  tion name */.   
39f85 20 69 6e 74 20 6e 41 72 67 3b 20 20 20 20 20 20   int nArg;      
39f86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f87 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
39f88 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
39f89 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 6e 63  s */.    int enc
39f8a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
39f8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f8c 20 20 20 2f 2a 20 4f 70 74 69 6d 61 6c 20 74 65     /* Optimal te
39f8d 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  xt encoding */. 
39f8e 20 20 20 76 6f 69 64 20 2a 70 43 6f 6e 74 65 78     void *pContex
39f8f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
39f90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39f91 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
39f92 61 28 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  a() context */. 
39f93 20 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29     void (*xFunc)
39f94 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
39f95 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
39f96 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20 73 63 61 6c  lue**);.  } scal
39f97 61 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 22  ars[] = {.    {"
39f98 72 65 67 65 78 70 22 2c 2d 31 2c 20 53 51 4c 49  regexp",-1, SQLI
39f99 54 45 5f 41 4e 59 2c 20 20 20 20 20 20 20 20 20  TE_ANY,         
39f9a 20 30 2c 20 69 63 75 52 65 67 65 78 70 46 75 6e   0, icuRegexpFun
39f9b 63 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f 77 65 72  c},..    {"lower
39f9c 22 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54  ",  1, SQLITE_UT
39f9d 46 31 36 2c 20 20 20 20 20 20 20 20 30 2c 20 69  F16,        0, i
39f9e 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20  cuCaseFunc16},. 
39f9f 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 32 2c     {"lower",  2,
39fa0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 20   SQLITE_UTF16,  
39fa1 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65        0, icuCase
39fa2 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75  Func16},.    {"u
39fa3 70 70 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54  pper",  1, SQLIT
39fa4 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64 2a 29  E_UTF16, (void*)
39fa5 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  1, icuCaseFunc16
39fa6 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c  },.    {"upper",
39fa7 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 31    2, SQLITE_UTF1
39fa8 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75  6, (void*)1, icu
39fa9 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 0a 20 20  CaseFunc16},..  
39faa 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31 2c 20    {"lower",  1, 
39fab 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
39fac 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65 46       0, icuCaseF
39fad 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 6c 6f  unc16},.    {"lo
39fae 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45  wer",  2, SQLITE
39faf 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30  _UTF8,         0
39fb0 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
39fb1 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c 20  ,.    {"upper", 
39fb2 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   1, SQLITE_UTF8,
39fb3 20 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75 43    (void*)1, icuC
39fb4 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
39fb5 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20 53 51  {"upper",  2, SQ
39fb6 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f 69  LITE_UTF8,  (voi
39fb7 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e  d*)1, icuCaseFun
39fb8 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c 69 6b  c16},..    {"lik
39fb9 65 22 2c 20 20 20 32 2c 20 53 51 4c 49 54 45 5f  e",   2, SQLITE_
39fba 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30 2c  UTF8,         0,
39fbb 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d 2c 0a 20   icuLikeFunc},. 
39fbc 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20 33 2c     {"like",   3,
39fbd 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
39fbe 20 20 20 20 20 20 30 2c 20 69 63 75 4c 69 6b 65        0, icuLike
39fbf 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b 22 69 63  Func},..    {"ic
39fc0 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e  u_load_collation
39fc1 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  ",  2, SQLITE_UT
39fc2 46 38 2c 20 28 76 6f 69 64 2a 29 64 62 2c 20 69  F8, (void*)db, i
39fc3 63 75 4c 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e 7d  cuLoadCollation}
39fc4 2c 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 72 63  ,.  };..  int rc
39fc5 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
39fc6 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 28 69 3d  int i;..  for(i=
39fc7 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
39fc8 20 26 26 20 69 3c 28 73 69 7a 65 6f 66 28 73 63   && i<(sizeof(sc
39fc9 61 6c 61 72 73 29 2f 73 69 7a 65 6f 66 28 73 74  alars)/sizeof(st
39fca 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 29 29  ruct IcuScalar))
39fcb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
39fcc 63 74 20 49 63 75 53 63 61 6c 61 72 20 2a 70 20  ct IcuScalar *p 
39fcd 3d 20 26 73 63 61 6c 61 72 73 5b 69 5d 3b 0a 20  = &scalars[i];. 
39fce 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
39fcf 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
39fd0 0a 20 20 20 20 20 20 20 20 64 62 2c 20 70 2d 3e  .        db, p->
39fd1 7a 4e 61 6d 65 2c 20 70 2d 3e 6e 41 72 67 2c 20  zName, p->nArg, 
39fd2 70 2d 3e 65 6e 63 2c 20 70 2d 3e 70 43 6f 6e 74  p->enc, p->pCont
39fd3 65 78 74 2c 20 70 2d 3e 78 46 75 6e 63 2c 20 30  ext, p->xFunc, 0
39fd4 2c 20 30 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a  , 0.    );.  }..
39fd5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
39fd6 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52 45  #if !SQLITE_CORE
39fd7 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
39fd8 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
39fd9 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  n_init(.  sqlite
39fda 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a  3 *db, .  char *
39fdb 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e  *pzErrMsg,.  con
39fdc 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
39fdd 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b  outines *pApi.){
39fde 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
39fdf 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a  ION_INIT2(pApi).
39fe0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
39fe1 49 63 75 49 6e 69 74 28 64 62 29 3b 0a 7d 0a 23  IcuInit(db);.}.#
39fe2 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f  endif..#endif../
39fe3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
39fe4 6e 64 20 6f 66 20 69 63 75 2e 63 20 2a 2a 2a 2a  nd of icu.c ****
39fe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39fe6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39fe7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
39fe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
39fe9 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 69  egin file fts3_i
39fea 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cu.c ***********
39feb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39fec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
39fed 2a 0a 2a 2a 20 32 30 30 37 20 4a 75 6e 65 20 32  *.** 2007 June 2
39fee 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
39fef 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
39ff0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
39ff1 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
39ff2 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
39ff3 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
39ff4 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
39ff5 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
39ff6 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
39ff7 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
39ff8 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
39ff9 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
39ffa 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
39ffb 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
39ffc 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
39ffd 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
39ffe 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
39fff 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
3a000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a001 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a002 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a003 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a004 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
3a005 70 6c 65 6d 65 6e 74 73 20 61 20 74 6f 6b 65 6e  plements a token
3a006 69 7a 65 72 20 66 6f 72 20 66 74 73 33 20 62 61  izer for fts3 ba
3a007 73 65 64 20 6f 6e 20 74 68 65 20 49 43 55 20 6c  sed on the ICU l
3a008 69 62 72 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 24  ibrary..** .** $
3a009 49 64 3a 20 66 74 73 33 5f 69 63 75 2e 63 2c 76  Id: fts3_icu.c,v
3a00a 20 31 2e 33 20 32 30 30 38 2f 30 39 2f 30 31 20   1.3 2008/09/01 
3a00b 31 38 3a 33 34 3a 32 30 20 64 61 6e 69 65 6c 6b  18:34:20 danielk
3a00c 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23  1977 Exp $.*/..#
3a00d 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
3a00e 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
3a00f 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
3a010 45 5f 46 54 53 33 29 0a 23 69 66 64 65 66 20 53  E_FTS3).#ifdef S
3a011 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
3a012 0a 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  ...#include <uni
3a013 63 6f 64 65 2f 75 62 72 6b 2e 68 3e 0a 23 69 6e  code/ubrk.h>.#in
3a014 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
3a015 74 66 31 36 2e 68 3e 0a 0a 74 79 70 65 64 65 66  tf16.h>..typedef
3a016 20 73 74 72 75 63 74 20 49 63 75 54 6f 6b 65 6e   struct IcuToken
3a017 69 7a 65 72 20 49 63 75 54 6f 6b 65 6e 69 7a 65  izer IcuTokenize
3a018 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
3a019 74 20 49 63 75 43 75 72 73 6f 72 20 49 63 75 43  t IcuCursor IcuC
3a01a 75 72 73 6f 72 3b 0a 0a 73 74 72 75 63 74 20 49  ursor;..struct I
3a01b 63 75 54 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20  cuTokenizer {.  
3a01c 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3a01d 72 20 62 61 73 65 3b 0a 20 20 63 68 61 72 20 2a  r base;.  char *
3a01e 7a 4c 6f 63 61 6c 65 3b 0a 7d 3b 0a 0a 73 74 72  zLocale;.};..str
3a01f 75 63 74 20 49 63 75 43 75 72 73 6f 72 20 7b 0a  uct IcuCursor {.
3a020 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3a021 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  zer_cursor base;
3a022 0a 0a 20 20 55 42 72 65 61 6b 49 74 65 72 61 74  ..  UBreakIterat
3a023 6f 72 20 2a 70 49 74 65 72 3b 20 20 20 20 20 20  or *pIter;      
3a024 2f 2a 20 49 43 55 20 62 72 65 61 6b 2d 69 74 65  /* ICU break-ite
3a025 72 61 74 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  rator object */.
3a026 20 20 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20    int nChar;    
3a027 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a028 20 4e 75 6d 62 65 72 20 6f 66 20 55 43 68 61 72   Number of UChar
3a029 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 49 6e   elements in pIn
3a02a 70 75 74 20 2a 2f 0a 20 20 55 43 68 61 72 20 2a  put */.  UChar *
3a02b 61 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20  aChar;          
3a02c 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
3a02d 69 6e 70 75 74 20 75 73 69 6e 67 20 75 74 66 2d  input using utf-
3a02e 31 36 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  16 encoding */. 
3a02f 20 69 6e 74 20 2a 61 4f 66 66 73 65 74 3b 20 20   int *aOffset;  
3a030 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a031 4f 66 66 73 65 74 73 20 6f 66 20 65 61 63 68 20  Offsets of each 
3a032 63 68 61 72 61 63 74 65 72 20 69 6e 20 75 74 66  character in utf
3a033 2d 38 20 69 6e 70 75 74 20 2a 2f 0a 0a 20 20 69  -8 input */..  i
3a034 6e 74 20 6e 42 75 66 66 65 72 3b 0a 20 20 63 68  nt nBuffer;.  ch
3a035 61 72 20 2a 7a 42 75 66 66 65 72 3b 0a 0a 20 20  ar *zBuffer;..  
3a036 69 6e 74 20 69 54 6f 6b 65 6e 3b 0a 7d 3b 0a 0a  int iToken;.};..
3a037 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
3a038 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73  ew tokenizer ins
3a039 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  tance..*/.static
3a03a 20 69 6e 74 20 69 63 75 43 72 65 61 74 65 28 0a   int icuCreate(.
3a03b 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20    int argc,     
3a03c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a03d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
3a03e 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
3a03f 72 67 76 5b 5d 20 2a 2f 0a 20 20 63 6f 6e 73 74  rgv[] */.  const
3a040 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61   char * const *a
3a041 72 67 76 2c 20 20 20 20 20 20 20 20 20 20 20 20  rgv,            
3a042 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 63 72 65  /* Tokenizer cre
3a043 61 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ation arguments 
3a044 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
3a045 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e  enizer **ppToken
3a046 69 7a 65 72 20 20 20 20 20 20 2f 2a 20 4f 55 54  izer      /* OUT
3a047 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65 6e 69  : Created tokeni
3a048 7a 65 72 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 54  zer */.){.  IcuT
3a049 6f 6b 65 6e 69 7a 65 72 20 2a 70 3b 0a 20 20 69  okenizer *p;.  i
3a04a 6e 74 20 6e 20 3d 20 30 3b 0a 0a 20 20 69 66 28  nt n = 0;..  if(
3a04b 20 61 72 67 63 3e 30 20 29 7b 0a 20 20 20 20 6e   argc>0 ){.    n
3a04c 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 30   = strlen(argv[0
3a04d 5d 29 2b 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20  ])+1;.  }.  p = 
3a04e 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29  (IcuTokenizer *)
3a04f 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
3a050 69 7a 65 6f 66 28 49 63 75 54 6f 6b 65 6e 69 7a  izeof(IcuTokeniz
3a051 65 72 29 2b 6e 29 3b 0a 20 20 69 66 28 20 21 70  er)+n);.  if( !p
3a052 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
3a053 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
3a054 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
3a055 73 69 7a 65 6f 66 28 49 63 75 54 6f 6b 65 6e 69  sizeof(IcuTokeni
3a056 7a 65 72 29 29 3b 0a 0a 20 20 69 66 28 20 6e 20  zer));..  if( n 
3a057 29 7b 0a 20 20 20 20 70 2d 3e 7a 4c 6f 63 61 6c  ){.    p->zLocal
3a058 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 5b 31  e = (char *)&p[1
3a059 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2d  ];.    memcpy(p-
3a05a 3e 7a 4c 6f 63 61 6c 65 2c 20 61 72 67 76 5b 30  >zLocale, argv[0
3a05b 5d 2c 20 6e 29 3b 0a 20 20 7d 0a 0a 20 20 2a 70  ], n);.  }..  *p
3a05c 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 28 73 71  pTokenizer = (sq
3a05d 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3a05e 2a 29 70 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  *)p;..  return S
3a05f 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3a060 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b  ** Destroy a tok
3a061 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63  enizer.*/.static
3a062 20 69 6e 74 20 69 63 75 44 65 73 74 72 6f 79 28   int icuDestroy(
3a063 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3a064 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a  r *pTokenizer){.
3a065 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a    IcuTokenizer *
3a066 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65  p = (IcuTokenize
3a067 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  r *)pTokenizer;.
3a068 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3a069 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
3a06a 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
3a06b 50 72 65 70 61 72 65 20 74 6f 20 62 65 67 69 6e  Prepare to begin
3a06c 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70 61   tokenizing a pa
3a06d 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e 67 2e  rticular string.
3a06e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20 73    The input.** s
3a06f 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65  tring to be toke
3a070 6e 69 7a 65 64 20 69 73 20 70 49 6e 70 75 74 5b  nized is pInput[
3a071 30 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e 20 20 41  0..nBytes-1].  A
3a072 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64 20   cursor.** used 
3a073 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79  to incrementally
3a074 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20 73   tokenize this s
3a075 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65  tring is returne
3a076 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72 73  d in .** *ppCurs
3a077 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
3a078 74 20 69 63 75 4f 70 65 6e 28 0a 20 20 73 71 6c  t icuOpen(.  sql
3a079 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
3a07a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20  pTokenizer,     
3a07b 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e      /* The token
3a07c 69 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  izer */.  const 
3a07d 63 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 20 20  char *zInput,   
3a07e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a07f 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
3a080 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74   */.  int nInput
3a081 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3a082 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a083 4c 65 6e 67 74 68 20 6f 66 20 7a 49 6e 70 75 74  Length of zInput
3a084 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 73   in bytes */.  s
3a085 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3a086 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
3a087 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f  or    /* OUT: To
3a088 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
3a089 72 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 54 6f 6b  r */.){.  IcuTok
3a08a 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28 49 63 75  enizer *p = (Icu
3a08b 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54 6f 6b  Tokenizer *)pTok
3a08c 65 6e 69 7a 65 72 3b 0a 20 20 49 63 75 43 75 72  enizer;.  IcuCur
3a08d 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20 20 63 6f  sor *pCsr;..  co
3a08e 6e 73 74 20 69 6e 74 33 32 5f 74 20 6f 70 74 20  nst int32_t opt 
3a08f 3d 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f 44 45  = U_FOLD_CASE_DE
3a090 46 41 55 4c 54 3b 0a 20 20 55 45 72 72 6f 72 43  FAULT;.  UErrorC
3a091 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a  ode status = U_Z
3a092 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 69 6e 74  ERO_ERROR;.  int
3a093 20 6e 43 68 61 72 3b 0a 0a 20 20 55 43 68 61 72   nChar;..  UChar
3a094 33 32 20 63 3b 0a 20 20 69 6e 74 20 69 49 6e 70  32 c;.  int iInp
3a095 75 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4f  ut = 0;.  int iO
3a096 75 74 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43 75  ut = 0;..  *ppCu
3a097 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20 69 66 28  rsor = 0;..  if(
3a098 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a 20 20 20   nInput<0 ){.   
3a099 20 6e 49 6e 70 75 74 20 3d 20 73 74 72 6c 65 6e   nInput = strlen
3a09a 28 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 0a 20 20  (zInput);.  }.  
3a09b 6e 43 68 61 72 20 3d 20 6e 49 6e 70 75 74 2b 31  nChar = nInput+1
3a09c 3b 0a 20 20 70 43 73 72 20 3d 20 28 49 63 75 43  ;.  pCsr = (IcuC
3a09d 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33 5f  ursor *)sqlite3_
3a09e 6d 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 73 69  malloc(.      si
3a09f 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72 29 20  zeof(IcuCursor) 
3a0a0 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +               
3a0a1 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 20 2a 2f   /* IcuCursor */
3a0a2 0a 20 20 20 20 20 20 6e 43 68 61 72 20 2a 20 73  .      nChar * s
3a0a3 69 7a 65 6f 66 28 55 43 68 61 72 29 20 2b 20 20  izeof(UChar) +  
3a0a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 63 75            /* Icu
3a0a5 43 75 72 73 6f 72 2e 61 43 68 61 72 5b 5d 20 2a  Cursor.aChar[] *
3a0a6 2f 0a 20 20 20 20 20 20 28 6e 43 68 61 72 2b 31  /.      (nChar+1
3a0a7 29 20 2a 20 73 69 7a 65 6f 66 28 69 6e 74 29 20  ) * sizeof(int) 
3a0a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 63             /* Ic
3a0a9 75 43 75 72 73 6f 72 2e 61 4f 66 66 73 65 74 5b  uCursor.aOffset[
3a0aa 5d 20 2a 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20  ] */.  );.  if( 
3a0ab 21 70 43 73 72 20 29 7b 0a 20 20 20 20 72 65 74  !pCsr ){.    ret
3a0ac 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
3a0ad 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70  ;.  }.  memset(p
3a0ae 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 49  Csr, 0, sizeof(I
3a0af 63 75 43 75 72 73 6f 72 29 29 3b 0a 20 20 70 43  cuCursor));.  pC
3a0b0 73 72 2d 3e 61 43 68 61 72 20 3d 20 28 55 43 68  sr->aChar = (UCh
3a0b1 61 72 20 2a 29 26 70 43 73 72 5b 31 5d 3b 0a 20  ar *)&pCsr[1];. 
3a0b2 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 20 3d   pCsr->aOffset =
3a0b3 20 28 69 6e 74 20 2a 29 26 70 43 73 72 2d 3e 61   (int *)&pCsr->a
3a0b4 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 0a 20 20  Char[nChar];..  
3a0b5 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 4f  pCsr->aOffset[iO
3a0b6 75 74 5d 20 3d 20 69 49 6e 70 75 74 3b 0a 20 20  ut] = iInput;.  
3a0b7 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75 74 2c 20  U8_NEXT(zInput, 
3a0b8 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  iInput, nInput, 
3a0b9 63 29 3b 20 0a 20 20 77 68 69 6c 65 28 20 63 3e  c); .  while( c>
3a0ba 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 45  0 ){.    int isE
3a0bb 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 63 20  rror = 0;.    c 
3a0bc 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 63 2c 20  = u_foldCase(c, 
3a0bd 6f 70 74 29 3b 0a 20 20 20 20 55 31 36 5f 41 50  opt);.    U16_AP
3a0be 50 45 4e 44 28 70 43 73 72 2d 3e 61 43 68 61 72  PEND(pCsr->aChar
3a0bf 2c 20 69 4f 75 74 2c 20 6e 43 68 61 72 2c 20 63  , iOut, nChar, c
3a0c0 2c 20 69 73 45 72 72 6f 72 29 3b 0a 20 20 20 20  , isError);.    
3a0c1 69 66 28 20 69 73 45 72 72 6f 72 20 29 7b 0a 20  if( isError ){. 
3a0c2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
3a0c3 65 28 70 43 73 72 29 3b 0a 20 20 20 20 20 20 72  e(pCsr);.      r
3a0c4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
3a0c5 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43  OR;.    }.    pC
3a0c6 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 4f 75 74  sr->aOffset[iOut
3a0c7 5d 20 3d 20 69 49 6e 70 75 74 3b 0a 0a 20 20 20  ] = iInput;..   
3a0c8 20 69 66 28 20 69 49 6e 70 75 74 3c 6e 49 6e 70   if( iInput<nInp
3a0c9 75 74 20 29 7b 0a 20 20 20 20 20 20 55 38 5f 4e  ut ){.      U8_N
3a0ca 45 58 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e 70  EXT(zInput, iInp
3a0cb 75 74 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b 0a  ut, nInput, c);.
3a0cc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3a0cd 20 63 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20   c = 0;.    }.  
3a0ce 7d 0a 0a 20 20 70 43 73 72 2d 3e 70 49 74 65 72  }..  pCsr->pIter
3a0cf 20 3d 20 75 62 72 6b 5f 6f 70 65 6e 28 55 42 52   = ubrk_open(UBR
3a0d0 4b 5f 57 4f 52 44 2c 20 70 2d 3e 7a 4c 6f 63 61  K_WORD, p->zLoca
3a0d1 6c 65 2c 20 70 43 73 72 2d 3e 61 43 68 61 72 2c  le, pCsr->aChar,
3a0d2 20 69 4f 75 74 2c 20 26 73 74 61 74 75 73 29 3b   iOut, &status);
3a0d3 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53  .  if( !U_SUCCES
3a0d4 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20  S(status) ){.   
3a0d5 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
3a0d6 73 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  sr);.    return 
3a0d7 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3a0d8 7d 0a 20 20 70 43 73 72 2d 3e 6e 43 68 61 72 20  }.  pCsr->nChar 
3a0d9 3d 20 69 4f 75 74 3b 0a 0a 20 20 75 62 72 6b 5f  = iOut;..  ubrk_
3a0da 66 69 72 73 74 28 70 43 73 72 2d 3e 70 49 74 65  first(pCsr->pIte
3a0db 72 29 3b 0a 20 20 2a 70 70 43 75 72 73 6f 72 20  r);.  *ppCursor 
3a0dc 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  = (sqlite3_token
3a0dd 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 70 43  izer_cursor *)pC
3a0de 73 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  sr;.  return SQL
3a0df 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3a0e0 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a   Close a tokeniz
3a0e1 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65  ation cursor pre
3a0e2 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62  viously opened b
3a0e3 79 20 61 20 63 61 6c 6c 20 74 6f 20 69 63 75 4f  y a call to icuO
3a0e4 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  pen()..*/.static
3a0e5 20 69 6e 74 20 69 63 75 43 6c 6f 73 65 28 73 71   int icuClose(sq
3a0e6 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3a0e7 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  cursor *pCursor)
3a0e8 7b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a 70  {.  IcuCursor *p
3a0e9 43 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72  Csr = (IcuCursor
3a0ea 20 2a 29 70 43 75 72 73 6f 72 3b 0a 20 20 75 62   *)pCursor;.  ub
3a0eb 72 6b 5f 63 6c 6f 73 65 28 70 43 73 72 2d 3e 70  rk_close(pCsr->p
3a0ec 49 74 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Iter);.  sqlite3
3a0ed 5f 66 72 65 65 28 70 43 73 72 2d 3e 7a 42 75 66  _free(pCsr->zBuf
3a0ee 66 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  fer);.  sqlite3_
3a0ef 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65  free(pCsr);.  re
3a0f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3a0f1 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
3a0f2 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20   the next token 
3a0f3 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74  from a tokenizat
3a0f4 69 6f 6e 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 73  ion cursor..*/.s
3a0f5 74 61 74 69 63 20 69 6e 74 20 69 63 75 4e 65 78  tatic int icuNex
3a0f6 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  t(.  sqlite3_tok
3a0f7 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
3a0f8 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73  Cursor,  /* Curs
3a0f9 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  or returned by s
3a0fa 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63  impleOpen */.  c
3a0fb 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f  onst char **ppTo
3a0fc 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  ken,            
3a0fd 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f     /* OUT: *ppTo
3a0fe 6b 65 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e  ken is the token
3a0ff 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a   text */.  int *
3a100 70 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20  pnBytes,        
3a101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a102 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
3a103 20 62 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20   bytes in token 
3a104 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72  */.  int *piStar
3a105 74 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20  tOffset,        
3a106 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3a107 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74   Starting offset
3a108 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   of token */.  i
3a109 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c  nt *piEndOffset,
3a10a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a10b 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e     /* OUT: Endin
3a10c 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65  g offset of toke
3a10d 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f  n */.  int *piPo
3a10e 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20  sition          
3a10f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3a110 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65  T: Position inte
3a111 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  ger of token */.
3a112 29 7b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a  ){.  IcuCursor *
3a113 70 43 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f  pCsr = (IcuCurso
3a114 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a 0a 20 20  r *)pCursor;..  
3a115 69 6e 74 20 69 53 74 61 72 74 20 3d 20 30 3b 0a  int iStart = 0;.
3a116 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 30 3b 0a    int iEnd = 0;.
3a117 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b    int nByte = 0;
3a118 0a 0a 20 20 77 68 69 6c 65 28 20 69 53 74 61 72  ..  while( iStar
3a119 74 3d 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20 55  t==iEnd ){.    U
3a11a 43 68 61 72 33 32 20 63 3b 0a 0a 20 20 20 20 69  Char32 c;..    i
3a11b 53 74 61 72 74 20 3d 20 75 62 72 6b 5f 63 75 72  Start = ubrk_cur
3a11c 72 65 6e 74 28 70 43 73 72 2d 3e 70 49 74 65 72  rent(pCsr->pIter
3a11d 29 3b 0a 20 20 20 20 69 45 6e 64 20 3d 20 75 62  );.    iEnd = ub
3a11e 72 6b 5f 6e 65 78 74 28 70 43 73 72 2d 3e 70 49  rk_next(pCsr->pI
3a11f 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 69 45  ter);.    if( iE
3a120 6e 64 3d 3d 55 42 52 4b 5f 44 4f 4e 45 20 29 7b  nd==UBRK_DONE ){
3a121 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
3a122 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d  LITE_DONE;.    }
3a123 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53 74  ..    while( iSt
3a124 61 72 74 3c 69 45 6e 64 20 29 7b 0a 20 20 20 20  art<iEnd ){.    
3a125 20 20 69 6e 74 20 69 57 68 69 74 65 20 3d 20 69    int iWhite = i
3a126 53 74 61 72 74 3b 0a 20 20 20 20 20 20 55 38 5f  Start;.      U8_
3a127 4e 45 58 54 28 70 43 73 72 2d 3e 61 43 68 61 72  NEXT(pCsr->aChar
3a128 2c 20 69 57 68 69 74 65 2c 20 70 43 73 72 2d 3e  , iWhite, pCsr->
3a129 6e 43 68 61 72 2c 20 63 29 3b 0a 20 20 20 20 20  nChar, c);.     
3a12a 20 69 66 28 20 75 5f 69 73 73 70 61 63 65 28 63   if( u_isspace(c
3a12b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 53 74  ) ){.        iSt
3a12c 61 72 74 20 3d 20 69 57 68 69 74 65 3b 0a 20 20  art = iWhite;.  
3a12d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3a12e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3a12f 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
3a130 72 74 28 69 53 74 61 72 74 3c 3d 69 45 6e 64 29  rt(iStart<=iEnd)
3a131 3b 0a 20 20 7d 0a 0a 20 20 64 6f 20 7b 0a 20 20  ;.  }..  do {.  
3a132 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61    UErrorCode sta
3a133 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52  tus = U_ZERO_ERR
3a134 4f 52 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74  OR;.    if( nByt
3a135 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  e ){.      char 
3a136 2a 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f  *zNew = sqlite3_
3a137 72 65 61 6c 6c 6f 63 28 70 43 73 72 2d 3e 7a 42  realloc(pCsr->zB
3a138 75 66 66 65 72 2c 20 6e 42 79 74 65 29 3b 0a 20  uffer, nByte);. 
3a139 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77 20 29       if( !zNew )
3a13a 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
3a13b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3a13c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 73       }.      pCs
3a13d 72 2d 3e 7a 42 75 66 66 65 72 20 3d 20 7a 4e 65  r->zBuffer = zNe
3a13e 77 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 6e  w;.      pCsr->n
3a13f 42 75 66 66 65 72 20 3d 20 6e 42 79 74 65 3b 0a  Buffer = nByte;.
3a140 20 20 20 20 7d 0a 0a 20 20 20 20 75 5f 73 74 72      }..    u_str
3a141 54 6f 55 54 46 38 28 0a 20 20 20 20 20 20 20 20  ToUTF8(.        
3a142 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 2c 20 70  pCsr->zBuffer, p
3a143 43 73 72 2d 3e 6e 42 75 66 66 65 72 2c 20 26 6e  Csr->nBuffer, &n
3a144 42 79 74 65 2c 20 20 20 20 2f 2a 20 4f 75 74 70  Byte,    /* Outp
3a145 75 74 20 76 61 72 73 20 2a 2f 0a 20 20 20 20 20  ut vars */.     
3a146 20 20 20 26 70 43 73 72 2d 3e 61 43 68 61 72 5b     &pCsr->aChar[
3a147 69 53 74 61 72 74 5d 2c 20 69 45 6e 64 2d 69 53  iStart], iEnd-iS
3a148 74 61 72 74 2c 20 20 20 20 20 20 20 2f 2a 20 49  tart,       /* I
3a149 6e 70 75 74 20 76 61 72 73 20 2a 2f 0a 20 20 20  nput vars */.   
3a14a 20 20 20 20 20 26 73 74 61 74 75 73 20 20 20 20       &status    
3a14b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a14c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a14d 20 4f 75 74 70 75 74 20 73 75 63 63 65 73 73 2f   Output success/
3a14e 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 20 20 29  failure */.    )
3a14f 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 42 79  ;.  } while( nBy
3a150 74 65 3e 70 43 73 72 2d 3e 6e 42 75 66 66 65 72  te>pCsr->nBuffer
3a151 20 29 3b 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e 20   );..  *ppToken 
3a152 3d 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 3b  = pCsr->zBuffer;
3a153 0a 20 20 2a 70 6e 42 79 74 65 73 20 3d 20 6e 42  .  *pnBytes = nB
3a154 79 74 65 3b 0a 20 20 2a 70 69 53 74 61 72 74 4f  yte;.  *piStartO
3a155 66 66 73 65 74 20 3d 20 70 43 73 72 2d 3e 61 4f  ffset = pCsr->aO
3a156 66 66 73 65 74 5b 69 53 74 61 72 74 5d 3b 0a 20  ffset[iStart];. 
3a157 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20   *piEndOffset = 
3a158 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 45  pCsr->aOffset[iE
3a159 6e 64 5d 3b 0a 20 20 2a 70 69 50 6f 73 69 74 69  nd];.  *piPositi
3a15a 6f 6e 20 3d 20 70 43 73 72 2d 3e 69 54 6f 6b 65  on = pCsr->iToke
3a15b 6e 2b 2b 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  n++;..  return S
3a15c 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3a15d 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f  ** The set of ro
3a15e 75 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c  utines that impl
3a15f 65 6d 65 6e 74 20 74 68 65 20 73 69 6d 70 6c 65  ement the simple
3a160 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74   tokenizer.*/.st
3a161 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
3a162 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3a163 75 6c 65 20 69 63 75 54 6f 6b 65 6e 69 7a 65 72  ule icuTokenizer
3a164 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 20  Module = {.  0, 
3a165 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a166 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
3a167 72 73 69 6f 6e 20 2a 2f 0a 20 20 69 63 75 43 72  rsion */.  icuCr
3a168 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  eate,           
3a169 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61          /* xCrea
3a16a 74 65 20 20 2a 2f 0a 20 20 69 63 75 44 65 73 74  te  */.  icuDest
3a16b 72 6f 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  roy,            
3a16c 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65        /* xCreate
3a16d 20 20 2a 2f 0a 20 20 69 63 75 4f 70 65 6e 2c 20    */.  icuOpen, 
3a16e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a16f 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20      /* xOpen    
3a170 2a 2f 0a 20 20 69 63 75 43 6c 6f 73 65 2c 20 20  */.  icuClose,  
3a171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a172 20 20 2f 2a 20 78 43 6c 6f 73 65 20 20 20 2a 2f    /* xClose   */
3a173 0a 20 20 69 63 75 4e 65 78 74 2c 20 20 20 20 20  .  icuNext,     
3a174 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a175 2f 2a 20 78 4e 65 78 74 20 20 20 20 2a 2f 0a 7d  /* xNext    */.}
3a176 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 70  ;../*.** Set *pp
3a177 4d 6f 64 75 6c 65 20 74 6f 20 70 6f 69 6e 74 20  Module to point 
3a178 61 74 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  at the implement
3a179 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 43 55  ation of the ICU
3a17a 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 53   tokenizer..*/.S
3a17b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3a17c 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 49 63  id sqlite3Fts3Ic
3a17d 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
3a17e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
3a17f 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
3a180 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a  st**ppModule.){.
3a181 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 69    *ppModule = &i
3a182 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  cuTokenizerModul
3a183 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  e;.}..#endif /* 
3a184 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3a185 4e 41 42 4c 45 5f 49 43 55 29 20 2a 2f 0a 23 65  NABLE_ICU) */.#e
3a186 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
3a187 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
3a188 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3a189 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
3a18a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3a18b 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 69 63 75   End of fts3_icu
3a18c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
3a18d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a18e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3a18f 0a                                               .